Convert AAC to WAV through FFMPEG

Source: Internet
Author: User

This is a very simple small program, but it has also made me a beginner for several days. It is an entry point. I will summarize the learning process and hope it will help beginners.

First, let the source code run. See the FFMPEG provides source code api-example.c, a good entry program, although the video codec is very smooth, but the source code provides audio decoding is problematic, MP2 files cannot be properly decoded, this is frustrating, especially for beginners.

In the single-step debugging of the source program, the problem is located on this statement:

avcodec_decode_audio3(in_ast_cctx, (int16_t *)outbuf, &out_size, &packet);

Let's look at the preceding two statements.

/* decode until eof */    avpkt.data = inbuf;    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);

The meaning of the two lines of code is very clear, that is, the data to be decoded is put into the buffer zone avpkt. Data and decoded through avcodec_decode_audio3. These seem reasonable, but where is the problem?

See the following section in the video_decode_example function:

/* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)           and this is the only method to use them because you cannot           know the compressed data size before analysing it.           BUT some other codecs (msmpeg4, mpeg4) are inherently frame           based, so you must call them with all the data for one           frame exactly. You must also initialize 'width' and           'height' before initializing them. */        /* NOTE2: some codecs allow the raw parameters (frame size,           sample rate) to be changed at any frame. We handle this, so           you should also take care of it */        /* here, we use a stream based decoder (mpeg1video), so we           feed decoder and see if it could decode a frame */

This sentence seems to have nothing to do with our audio decoding, but "codecs are stream based" and "frame based" Give us a good prompt, obviously, the AAC decoder provided by FFmpeg is based on frame decoding. the data filled in the data buffer should be a complete audio frame, not much, rather than a simple data of fread audio_inbuf_size from the file.

The problem is clear, by filling in avpkt. before data is processed, a complete frame needs to be pre-processed, that is, the complete frame can be found through the synchronization word (12bit -- 0 xfff of the frame header) in the AAC frame.
Now, let's write the Preprocessing Program. I thought it would be okay if there are so many lines of FFMPEG code and no AAC frame resolution API is provided. I went on to Google and found the following article:

Http://hi.baidu.com/wg_wang/blog/item/8e7e321796063a0c4b90a770.html

Http://hi.baidu.com/wg_wang/blog/item/f78216f35465655d342acc16.html

Http://hi.baidu.com/wg_wang/blog/item/4d55bd23eb3e2d49ac34de17.html

The title of the article is: Use libavcodec and libavformat to transcode the audio. I attached a very detailed code parsing. Most of the source programs I wrote are based on this article.

The following is the source code in the test. c file.

# Include <stdio. h> # include "libavformat/avformat. H "# include" libavcodec/avcodec. H "# include" audio. H "# include" neaacdec. H "int main (INT argc, char ** argv) {avformatcontext * in_fctx; avcodeccontext * in_ast_cctx; avcodec * in_ast_codec; avpacket packet; audio_file * aufile; char * outbuf = (char *) av_malloc (avcodec_max_audio_frame_size); int out_size; int samples; // char * filename = "C: \ Users \ ajaxhe \ Desktop \ aud Iocodec \ FFMPEG \ myproject \ output_example2 \ debug \ higher1.aac "; char * filename; char * wavfile =" debug_test.wav "; // char * wavfile; int ret = 0; int ast_idx =-1; int I, first_time = 1; if (argc = 3) {filename = argv [1]; wavfile = argv [2];} else {printf ("Usage: output_example.exe infile. aac outfile. AAC \ n "); Return-1;} av_register_all (); in_fctx = av_alloc_format_context (); ret = av_open_input_file (& in_fctx, F Ilename, null, 0, null); If (Ret! = 0) {printf ("Open input audio file [% s] fail", filename); Return-1;} ret = av_find_stream_info (in_fctx); If (Ret <0) {printf ("find stream in audio file [% s] fail", filename); Return-1;} // dump_format (in_fctx, 0, filename, 0 ); // assume that if a file contains multiple audio streams, // only transcode the first audio stream, and ignore for (I = 0; I <(INT) in_fctx-> nb_streams; ++ I) {If (in_fctx-> streams [I]-> codec-> codec_type = codec_type_audio) {ast_idx = I; break;} If (AST _ Idx =-1) {printf ("there is not any audio stream in [% s]", filename); Return 0 ;} else {printf ("find audio stream in [% s] \ n", filename);} in_ast_cctx = in_fctx-> streams [ast_idx]-> codec; in_ast_codec = avcodec_find_decoder (in_ast_cctx-> codec_id); If (! In_ast_codec) {printf ("find decoder for codec_id [% d] fail, file [% s]", in_ast_cctx-> codec_id, filename); Return-1 ;} ret = avcodec_open (in_ast_cctx, in_ast_codec); If (Ret> = 0) {printf ("Open codec [name: % s] for stream [idx: % d] of file [% s] \ n ", in_ast_codec-> name, ast_idx, filename);} av_init_packet (& Packet); While (av_read_frame (in_fctx, & Packet)> = 0) {out_size = avcodec_max_audio_frame_size; ret = avcodec_de Code_audio3 (bytes, (int16_t *) outbuf, & out_size, & Packet); If (first_time) {aufile = open_audio_file (wavfile, audio-> sample_rate, audio-> channels, channels, output_wav, 0); first_time = 0;} samples = in_ast_cctx-> frame_size * in_ast_cctx-> channels; write_audio_file (aufile, outbuf, samples, 0);} If (! First_time) close_audio_file (aufile); avcodec_close (in_ast_cctx); av_free (in_ast_cctx); Return 0 ;}

As it is just a simple small program and only writes a main function, please forgive me.

# Include "audio. H"
# Include "neaacdec. H"

These two header files were obtained from the faad2 source code. The time was too short for me to sort them out. The whole project friend needed to download them directly from the following link.

Http://download.csdn.net/detail/ajaxhe/3585763

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.