]> git.sesse.net Git - ffmpeg/blob - doc/examples/transcode_aac.c
http: Add support for selecting a request range
[ffmpeg] / doc / examples / transcode_aac.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /**
20  * @file
21  * simple audio converter
22  *
23  * @example transcode_aac.c
24  * Convert an input audio file to AAC in an MP4 container using Libav.
25  * @author Andreas Unterweger (dustsigns@gmail.com)
26  */
27
28 #include <stdio.h>
29
30 #include "libavformat/avformat.h"
31 #include "libavformat/avio.h"
32
33 #include "libavcodec/avcodec.h"
34
35 #include "libavutil/audio_fifo.h"
36 #include "libavutil/avstring.h"
37 #include "libavutil/frame.h"
38 #include "libavutil/opt.h"
39
40 #include "libavresample/avresample.h"
41
42 /** The output bit rate in kbit/s */
43 #define OUTPUT_BIT_RATE 48000
44 /** The number of output channels */
45 #define OUTPUT_CHANNELS 2
46 /** The audio sample output format */
47 #define OUTPUT_SAMPLE_FORMAT AV_SAMPLE_FMT_S16
48
49 /**
50  * Convert an error code into a text message.
51  * @param error Error code to be converted
52  * @return Corresponding error text (not thread-safe)
53  */
54 static char *const get_error_text(const int error)
55 {
56     static char error_buffer[255];
57     av_strerror(error, error_buffer, sizeof(error_buffer));
58     return error_buffer;
59 }
60
61 /** Open an input file and the required decoder. */
62 static int open_input_file(const char *filename,
63                            AVFormatContext **input_format_context,
64                            AVCodecContext **input_codec_context)
65 {
66     AVCodec *input_codec;
67     int error;
68
69     /** Open the input file to read from it. */
70     if ((error = avformat_open_input(input_format_context, filename, NULL,
71                                      NULL)) < 0) {
72         fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
73                 filename, get_error_text(error));
74         *input_format_context = NULL;
75         return error;
76     }
77
78     /** Get information on the input file (number of streams etc.). */
79     if ((error = avformat_find_stream_info(*input_format_context, NULL)) < 0) {
80         fprintf(stderr, "Could not open find stream info (error '%s')\n",
81                 get_error_text(error));
82         avformat_close_input(input_format_context);
83         return error;
84     }
85
86     /** Make sure that there is only one stream in the input file. */
87     if ((*input_format_context)->nb_streams != 1) {
88         fprintf(stderr, "Expected one audio input stream, but found %d\n",
89                 (*input_format_context)->nb_streams);
90         avformat_close_input(input_format_context);
91         return AVERROR_EXIT;
92     }
93
94     /** Find a decoder for the audio stream. */
95     if (!(input_codec = avcodec_find_decoder((*input_format_context)->streams[0]->codec->codec_id))) {
96         fprintf(stderr, "Could not find input codec\n");
97         avformat_close_input(input_format_context);
98         return AVERROR_EXIT;
99     }
100
101     /** Open the decoder for the audio stream to use it later. */
102     if ((error = avcodec_open2((*input_format_context)->streams[0]->codec,
103                                input_codec, NULL)) < 0) {
104         fprintf(stderr, "Could not open input codec (error '%s')\n",
105                 get_error_text(error));
106         avformat_close_input(input_format_context);
107         return error;
108     }
109
110     /** Save the decoder context for easier access later. */
111     *input_codec_context = (*input_format_context)->streams[0]->codec;
112
113     return 0;
114 }
115
116 /**
117  * Open an output file and the required encoder.
118  * Also set some basic encoder parameters.
119  * Some of these parameters are based on the input file's parameters.
120  */
121 static int open_output_file(const char *filename,
122                             AVCodecContext *input_codec_context,
123                             AVFormatContext **output_format_context,
124                             AVCodecContext **output_codec_context)
125 {
126     AVIOContext *output_io_context = NULL;
127     AVStream *stream               = NULL;
128     AVCodec *output_codec          = NULL;
129     int error;
130
131     /** Open the output file to write to it. */
132     if ((error = avio_open(&output_io_context, filename,
133                            AVIO_FLAG_WRITE)) < 0) {
134         fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
135                 filename, get_error_text(error));
136         return error;
137     }
138
139     /** Create a new format context for the output container format. */
140     if (!(*output_format_context = avformat_alloc_context())) {
141         fprintf(stderr, "Could not allocate output format context\n");
142         return AVERROR(ENOMEM);
143     }
144
145     /** Associate the output file (pointer) with the container format context. */
146     (*output_format_context)->pb = output_io_context;
147
148     /** Guess the desired container format based on the file extension. */
149     if (!((*output_format_context)->oformat = av_guess_format(NULL, filename,
150                                                               NULL))) {
151         fprintf(stderr, "Could not find output file format\n");
152         goto cleanup;
153     }
154
155     av_strlcpy((*output_format_context)->filename, filename,
156                sizeof((*output_format_context)->filename));
157
158     /** Find the encoder to be used by its name. */
159     if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) {
160         fprintf(stderr, "Could not find an AAC encoder.\n");
161         goto cleanup;
162     }
163
164     /** Create a new audio stream in the output file container. */
165     if (!(stream = avformat_new_stream(*output_format_context, output_codec))) {
166         fprintf(stderr, "Could not create new stream\n");
167         error = AVERROR(ENOMEM);
168         goto cleanup;
169     }
170
171     /** Save the encoder context for easiert access later. */
172     *output_codec_context = stream->codec;
173
174     /**
175      * Set the basic encoder parameters.
176      * The input file's sample rate is used to avoid a sample rate conversion.
177      */
178     (*output_codec_context)->channels       = OUTPUT_CHANNELS;
179     (*output_codec_context)->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
180     (*output_codec_context)->sample_rate    = input_codec_context->sample_rate;
181     (*output_codec_context)->sample_fmt     = AV_SAMPLE_FMT_S16;
182     (*output_codec_context)->bit_rate       = OUTPUT_BIT_RATE;
183
184     /**
185      * Some container formats (like MP4) require global headers to be present
186      * Mark the encoder so that it behaves accordingly.
187      */
188     if ((*output_format_context)->oformat->flags & AVFMT_GLOBALHEADER)
189         (*output_codec_context)->flags |= CODEC_FLAG_GLOBAL_HEADER;
190
191     /** Open the encoder for the audio stream to use it later. */
192     if ((error = avcodec_open2(*output_codec_context, output_codec, NULL)) < 0) {
193         fprintf(stderr, "Could not open output codec (error '%s')\n",
194                 get_error_text(error));
195         goto cleanup;
196     }
197
198     return 0;
199
200 cleanup:
201     avio_close((*output_format_context)->pb);
202     avformat_free_context(*output_format_context);
203     *output_format_context = NULL;
204     return error < 0 ? error : AVERROR_EXIT;
205 }
206
207 /** Initialize one data packet for reading or writing. */
208 static void init_packet(AVPacket *packet)
209 {
210     av_init_packet(packet);
211     /** Set the packet data and size so that it is recognized as being empty. */
212     packet->data = NULL;
213     packet->size = 0;
214 }
215
216 /** Initialize one audio frame for reading from the input file */
217 static int init_input_frame(AVFrame **frame)
218 {
219     if (!(*frame = av_frame_alloc())) {
220         fprintf(stderr, "Could not allocate input frame\n");
221         return AVERROR(ENOMEM);
222     }
223     return 0;
224 }
225
226 /**
227  * Initialize the audio resampler based on the input and output codec settings.
228  * If the input and output sample formats differ, a conversion is required
229  * libavresample takes care of this, but requires initialization.
230  */
231 static int init_resampler(AVCodecContext *input_codec_context,
232                           AVCodecContext *output_codec_context,
233                           AVAudioResampleContext **resample_context)
234 {
235     /**
236      * Only initialize the resampler if it is necessary, i.e.,
237      * if and only if the sample formats differ.
238      */
239     if (input_codec_context->sample_fmt != output_codec_context->sample_fmt ||
240         input_codec_context->channels != output_codec_context->channels) {
241         int error;
242
243         /** Create a resampler context for the conversion. */
244         if (!(*resample_context = avresample_alloc_context())) {
245             fprintf(stderr, "Could not allocate resample context\n");
246             return AVERROR(ENOMEM);
247         }
248
249         /**
250          * Set the conversion parameters.
251          * Default channel layouts based on the number of channels
252          * are assumed for simplicity (they are sometimes not detected
253          * properly by the demuxer and/or decoder).
254          */
255         av_opt_set_int(*resample_context, "in_channel_layout",
256                        av_get_default_channel_layout(input_codec_context->channels), 0);
257         av_opt_set_int(*resample_context, "out_channel_layout",
258                        av_get_default_channel_layout(output_codec_context->channels), 0);
259         av_opt_set_int(*resample_context, "in_sample_rate",
260                        input_codec_context->sample_rate, 0);
261         av_opt_set_int(*resample_context, "out_sample_rate",
262                        output_codec_context->sample_rate, 0);
263         av_opt_set_int(*resample_context, "in_sample_fmt",
264                        input_codec_context->sample_fmt, 0);
265         av_opt_set_int(*resample_context, "out_sample_fmt",
266                        output_codec_context->sample_fmt, 0);
267
268         /** Open the resampler with the specified parameters. */
269         if ((error = avresample_open(*resample_context)) < 0) {
270             fprintf(stderr, "Could not open resample context\n");
271             avresample_free(resample_context);
272             return error;
273         }
274     }
275     return 0;
276 }
277
278 /** Initialize a FIFO buffer for the audio samples to be encoded. */
279 static int init_fifo(AVAudioFifo **fifo)
280 {
281     /** Create the FIFO buffer based on the specified output sample format. */
282     if (!(*fifo = av_audio_fifo_alloc(OUTPUT_SAMPLE_FORMAT, OUTPUT_CHANNELS, 1))) {
283         fprintf(stderr, "Could not allocate FIFO\n");
284         return AVERROR(ENOMEM);
285     }
286     return 0;
287 }
288
289 /** Write the header of the output file container. */
290 static int write_output_file_header(AVFormatContext *output_format_context)
291 {
292     int error;
293     if ((error = avformat_write_header(output_format_context, NULL)) < 0) {
294         fprintf(stderr, "Could not write output file header (error '%s')\n",
295                 get_error_text(error));
296         return error;
297     }
298     return 0;
299 }
300
301 /** Decode one audio frame from the input file. */
302 static int decode_audio_frame(AVFrame *frame,
303                               AVFormatContext *input_format_context,
304                               AVCodecContext *input_codec_context,
305                               int *data_present, int *finished)
306 {
307     /** Packet used for temporary storage. */
308     AVPacket input_packet;
309     int error;
310     init_packet(&input_packet);
311
312     /** Read one audio frame from the input file into a temporary packet. */
313     if ((error = av_read_frame(input_format_context, &input_packet)) < 0) {
314         /** If we are the the end of the file, flush the decoder below. */
315         if (error == AVERROR_EOF)
316             *finished = 1;
317         else {
318             fprintf(stderr, "Could not read frame (error '%s')\n",
319                     get_error_text(error));
320             return error;
321         }
322     }
323
324     /**
325      * Decode the audio frame stored in the temporary packet.
326      * The input audio stream decoder is used to do this.
327      * If we are at the end of the file, pass an empty packet to the decoder
328      * to flush it.
329      */
330     if ((error = avcodec_decode_audio4(input_codec_context, frame,
331                                        data_present, &input_packet)) < 0) {
332         fprintf(stderr, "Could not decode frame (error '%s')\n",
333                 get_error_text(error));
334         av_free_packet(&input_packet);
335         return error;
336     }
337
338     /**
339      * If the decoder has not been flushed completely, we are not finished,
340      * so that this function has to be called again.
341      */
342     if (*finished && *data_present)
343         *finished = 0;
344     av_free_packet(&input_packet);
345     return 0;
346 }
347
348 /**
349  * Initialize a temporary storage for the specified number of audio samples.
350  * The conversion requires temporary storage due to the different format.
351  * The number of audio samples to be allocated is specified in frame_size.
352  */
353 static int init_converted_samples(uint8_t ***converted_input_samples,
354                                   AVCodecContext *output_codec_context,
355                                   int frame_size)
356 {
357     int error;
358
359     /**
360      * Allocate as many pointers as there are audio channels.
361      * Each pointer will later point to the audio samples of the corresponding
362      * channels (although it may be NULL for interleaved formats).
363      */
364     if (!(*converted_input_samples = calloc(output_codec_context->channels,
365                                             sizeof(**converted_input_samples)))) {
366         fprintf(stderr, "Could not allocate converted input sample pointers\n");
367         return AVERROR(ENOMEM);
368     }
369
370     /**
371      * Allocate memory for the samples of all channels in one consecutive
372      * block for convenience.
373      */
374     if ((error = av_samples_alloc(*converted_input_samples, NULL,
375                                   output_codec_context->channels,
376                                   frame_size,
377                                   output_codec_context->sample_fmt, 0)) < 0) {
378         fprintf(stderr,
379                 "Could not allocate converted input samples (error '%s')\n",
380                 get_error_text(error));
381         av_freep(&(*converted_input_samples)[0]);
382         free(*converted_input_samples);
383         return error;
384     }
385     return 0;
386 }
387
388 /**
389  * Convert the input audio samples into the output sample format.
390  * The conversion happens on a per-frame basis, the size of which is specified
391  * by frame_size.
392  */
393 static int convert_samples(uint8_t **input_data,
394                            uint8_t **converted_data, const int frame_size,
395                            AVAudioResampleContext *resample_context)
396 {
397     int error;
398
399     /** Convert the samples using the resampler. */
400     if ((error = avresample_convert(resample_context, converted_data, 0,
401                                     frame_size, input_data, 0, frame_size)) < 0) {
402         fprintf(stderr, "Could not convert input samples (error '%s')\n",
403                 get_error_text(error));
404         return error;
405     }
406
407     /**
408      * Perform a sanity check so that the number of converted samples is
409      * not greater than the number of samples to be converted.
410      * If the sample rates differ, this case has to be handled differently
411      */
412     if (avresample_available(resample_context)) {
413         fprintf(stderr, "Converted samples left over\n");
414         return AVERROR_EXIT;
415     }
416
417     return 0;
418 }
419
420 /** Add converted input audio samples to the FIFO buffer for later processing. */
421 static int add_samples_to_fifo(AVAudioFifo *fifo,
422                                uint8_t **converted_input_samples,
423                                const int frame_size)
424 {
425     int error;
426
427     /**
428      * Make the FIFO as large as it needs to be to hold both,
429      * the old and the new samples.
430      */
431     if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
432         fprintf(stderr, "Could not reallocate FIFO\n");
433         return error;
434     }
435
436     /** Store the new samples in the FIFO buffer. */
437     if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
438                             frame_size) < frame_size) {
439         fprintf(stderr, "Could not write data to FIFO\n");
440         return AVERROR_EXIT;
441     }
442     return 0;
443 }
444
445 /**
446  * Read one audio frame from the input file, decodes, converts and stores
447  * it in the FIFO buffer.
448  */
449 static int read_decode_convert_and_store(AVAudioFifo *fifo,
450                                          AVFormatContext *input_format_context,
451                                          AVCodecContext *input_codec_context,
452                                          AVCodecContext *output_codec_context,
453                                          AVAudioResampleContext *resampler_context,
454                                          int *finished)
455 {
456     /** Temporary storage of the input samples of the frame read from the file. */
457     AVFrame *input_frame = NULL;
458     /** Temporary storage for the converted input samples. */
459     uint8_t **converted_input_samples = NULL;
460     int data_present;
461     int ret = AVERROR_EXIT;
462
463     /** Initialize temporary storage for one input frame. */
464     if (init_input_frame(&input_frame))
465         goto cleanup;
466     /** Decode one frame worth of audio samples. */
467     if (decode_audio_frame(input_frame, input_format_context,
468                            input_codec_context, &data_present, finished))
469         goto cleanup;
470     /**
471      * If we are at the end of the file and there are no more samples
472      * in the decoder which are delayed, we are actually finished.
473      * This must not be treated as an error.
474      */
475     if (*finished && !data_present) {
476         ret = 0;
477         goto cleanup;
478     }
479     /** If there is decoded data, convert and store it */
480     if (data_present) {
481         /** Initialize the temporary storage for the converted input samples. */
482         if (init_converted_samples(&converted_input_samples, output_codec_context,
483                                    input_frame->nb_samples))
484             goto cleanup;
485
486         /**
487          * Convert the input samples to the desired output sample format.
488          * This requires a temporary storage provided by converted_input_samples.
489          */
490         if (convert_samples(input_frame->extended_data, converted_input_samples,
491                             input_frame->nb_samples, resampler_context))
492             goto cleanup;
493
494         /** Add the converted input samples to the FIFO buffer for later processing. */
495         if (add_samples_to_fifo(fifo, converted_input_samples,
496                                 input_frame->nb_samples))
497             goto cleanup;
498         ret = 0;
499     }
500     ret = 0;
501
502 cleanup:
503     if (converted_input_samples) {
504         av_freep(&converted_input_samples[0]);
505         free(converted_input_samples);
506     }
507     av_frame_free(&input_frame);
508
509     return ret;
510 }
511
512 /**
513  * Initialize one input frame for writing to the output file.
514  * The frame will be exactly frame_size samples large.
515  */
516 static int init_output_frame(AVFrame **frame,
517                              AVCodecContext *output_codec_context,
518                              int frame_size)
519 {
520     int error;
521
522     /** Create a new frame to store the audio samples. */
523     if (!(*frame = av_frame_alloc())) {
524         fprintf(stderr, "Could not allocate output frame\n");
525         return AVERROR_EXIT;
526     }
527
528     /**
529      * Set the frame's parameters, especially its size and format.
530      * av_frame_get_buffer needs this to allocate memory for the
531      * audio samples of the frame.
532      * Default channel layouts based on the number of channels
533      * are assumed for simplicity.
534      */
535     (*frame)->nb_samples     = frame_size;
536     (*frame)->channel_layout = output_codec_context->channel_layout;
537     (*frame)->format         = output_codec_context->sample_fmt;
538     (*frame)->sample_rate    = output_codec_context->sample_rate;
539
540     /**
541      * Allocate the samples of the created frame. This call will make
542      * sure that the audio frame can hold as many samples as specified.
543      */
544     if ((error = av_frame_get_buffer(*frame, 0)) < 0) {
545         fprintf(stderr, "Could allocate output frame samples (error '%s')\n",
546                 get_error_text(error));
547         av_frame_free(frame);
548         return error;
549     }
550
551     return 0;
552 }
553
554 /** Encode one frame worth of audio to the output file. */
555 static int encode_audio_frame(AVFrame *frame,
556                               AVFormatContext *output_format_context,
557                               AVCodecContext *output_codec_context,
558                               int *data_present)
559 {
560     /** Packet used for temporary storage. */
561     AVPacket output_packet;
562     int error;
563     init_packet(&output_packet);
564
565     /**
566      * Encode the audio frame and store it in the temporary packet.
567      * The output audio stream encoder is used to do this.
568      */
569     if ((error = avcodec_encode_audio2(output_codec_context, &output_packet,
570                                        frame, data_present)) < 0) {
571         fprintf(stderr, "Could not encode frame (error '%s')\n",
572                 get_error_text(error));
573         av_free_packet(&output_packet);
574         return error;
575     }
576
577     /** Write one audio frame from the temporary packet to the output file. */
578     if (*data_present) {
579         if ((error = av_write_frame(output_format_context, &output_packet)) < 0) {
580             fprintf(stderr, "Could not write frame (error '%s')\n",
581                     get_error_text(error));
582             av_free_packet(&output_packet);
583             return error;
584         }
585
586         av_free_packet(&output_packet);
587     }
588
589     return 0;
590 }
591
592 /**
593  * Load one audio frame from the FIFO buffer, encode and write it to the
594  * output file.
595  */
596 static int load_encode_and_write(AVAudioFifo *fifo,
597                                  AVFormatContext *output_format_context,
598                                  AVCodecContext *output_codec_context)
599 {
600     /** Temporary storage of the output samples of the frame written to the file. */
601     AVFrame *output_frame;
602     /**
603      * Use the maximum number of possible samples per frame.
604      * If there is less than the maximum possible frame size in the FIFO
605      * buffer use this number. Otherwise, use the maximum possible frame size
606      */
607     const int frame_size = FFMIN(av_audio_fifo_size(fifo),
608                                  output_codec_context->frame_size);
609     int data_written;
610
611     /** Initialize temporary storage for one output frame. */
612     if (init_output_frame(&output_frame, output_codec_context, frame_size))
613         return AVERROR_EXIT;
614
615     /**
616      * Read as many samples from the FIFO buffer as required to fill the frame.
617      * The samples are stored in the frame temporarily.
618      */
619     if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) {
620         fprintf(stderr, "Could not read data from FIFO\n");
621         av_frame_free(&output_frame);
622         return AVERROR_EXIT;
623     }
624
625     /** Encode one frame worth of audio samples. */
626     if (encode_audio_frame(output_frame, output_format_context,
627                            output_codec_context, &data_written)) {
628         av_frame_free(&output_frame);
629         return AVERROR_EXIT;
630     }
631     av_frame_free(&output_frame);
632     return 0;
633 }
634
635 /** Write the trailer of the output file container. */
636 static int write_output_file_trailer(AVFormatContext *output_format_context)
637 {
638     int error;
639     if ((error = av_write_trailer(output_format_context)) < 0) {
640         fprintf(stderr, "Could not write output file trailer (error '%s')\n",
641                 get_error_text(error));
642         return error;
643     }
644     return 0;
645 }
646
647 /** Convert an audio file to an AAC file in an MP4 container. */
648 int main(int argc, char **argv)
649 {
650     AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
651     AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
652     AVAudioResampleContext *resample_context = NULL;
653     AVAudioFifo *fifo = NULL;
654     int ret = AVERROR_EXIT;
655
656     if (argc < 3) {
657         fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
658         exit(1);
659     }
660
661     /** Register all codecs and formats so that they can be used. */
662     av_register_all();
663     /** Open the input file for reading. */
664     if (open_input_file(argv[1], &input_format_context,
665                         &input_codec_context))
666         goto cleanup;
667     /** Open the output file for writing. */
668     if (open_output_file(argv[2], input_codec_context,
669                          &output_format_context, &output_codec_context))
670         goto cleanup;
671     /** Initialize the resampler to be able to convert audio sample formats. */
672     if (init_resampler(input_codec_context, output_codec_context,
673                        &resample_context))
674         goto cleanup;
675     /** Initialize the FIFO buffer to store audio samples to be encoded. */
676     if (init_fifo(&fifo))
677         goto cleanup;
678     /** Write the header of the output file container. */
679     if (write_output_file_header(output_format_context))
680         goto cleanup;
681
682     /**
683      * Loop as long as we have input samples to read or output samples
684      * to write; abort as soon as we have neither.
685      */
686     while (1) {
687         /** Use the encoder's desired frame size for processing. */
688         const int output_frame_size = output_codec_context->frame_size;
689         int finished                = 0;
690
691         /**
692          * Make sure that there is one frame worth of samples in the FIFO
693          * buffer so that the encoder can do its work.
694          * Since the decoder's and the encoder's frame size may differ, we
695          * need to FIFO buffer to store as many frames worth of input samples
696          * that they make up at least one frame worth of output samples.
697          */
698         while (av_audio_fifo_size(fifo) < output_frame_size) {
699             /**
700              * Decode one frame worth of audio samples, convert it to the
701              * output sample format and put it into the FIFO buffer.
702              */
703             if (read_decode_convert_and_store(fifo, input_format_context,
704                                               input_codec_context,
705                                               output_codec_context,
706                                               resample_context, &finished))
707                 goto cleanup;
708
709             /**
710              * If we are at the end of the input file, we continue
711              * encoding the remaining audio samples to the output file.
712              */
713             if (finished)
714                 break;
715         }
716
717         /**
718          * If we have enough samples for the encoder, we encode them.
719          * At the end of the file, we pass the remaining samples to
720          * the encoder.
721          */
722         while (av_audio_fifo_size(fifo) >= output_frame_size ||
723                (finished && av_audio_fifo_size(fifo) > 0))
724             /**
725              * Take one frame worth of audio samples from the FIFO buffer,
726              * encode it and write it to the output file.
727              */
728             if (load_encode_and_write(fifo, output_format_context,
729                                       output_codec_context))
730                 goto cleanup;
731
732         /**
733          * If we are at the end of the input file and have encoded
734          * all remaining samples, we can exit this loop and finish.
735          */
736         if (finished) {
737             int data_written;
738             /** Flush the encoder as it may have delayed frames. */
739             do {
740                 if (encode_audio_frame(NULL, output_format_context,
741                                        output_codec_context, &data_written))
742                     goto cleanup;
743             } while (data_written);
744             break;
745         }
746     }
747
748     /** Write the trailer of the output file container. */
749     if (write_output_file_trailer(output_format_context))
750         goto cleanup;
751     ret = 0;
752
753 cleanup:
754     if (fifo)
755         av_audio_fifo_free(fifo);
756     if (resample_context) {
757         avresample_close(resample_context);
758         avresample_free(&resample_context);
759     }
760     if (output_codec_context)
761         avcodec_close(output_codec_context);
762     if (output_format_context) {
763         avio_close(output_format_context->pb);
764         avformat_free_context(output_format_context);
765     }
766     if (input_codec_context)
767         avcodec_close(input_codec_context);
768     if (input_format_context)
769         avformat_close_input(&input_format_context);
770
771     return ret;
772 }