2 * This file is part of FFmpeg.
4 * FFmpeg 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.
9 * FFmpeg 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * simple audio converter
23 * @example transcode_aac.c
24 * Convert an input audio file to AAC in an MP4 container using FFmpeg.
25 * @author Andreas Unterweger (dustsigns@gmail.com)
30 #include "libavformat/avformat.h"
31 #include "libavformat/avio.h"
33 #include "libavcodec/avcodec.h"
35 #include "libavutil/audio_fifo.h"
36 #include "libavutil/avassert.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/frame.h"
39 #include "libavutil/opt.h"
41 #include "libswresample/swresample.h"
43 /** The output bit rate in kbit/s */
44 #define OUTPUT_BIT_RATE 96000
45 /** The number of output channels */
46 #define OUTPUT_CHANNELS 2
48 /** Open an input file and the required decoder. */
49 static int open_input_file(const char *filename,
50 AVFormatContext **input_format_context,
51 AVCodecContext **input_codec_context)
53 AVCodecContext *avctx;
57 /** Open the input file to read from it. */
58 if ((error = avformat_open_input(input_format_context, filename, NULL,
60 fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
61 filename, av_err2str(error));
62 *input_format_context = NULL;
66 /** Get information on the input file (number of streams etc.). */
67 if ((error = avformat_find_stream_info(*input_format_context, NULL)) < 0) {
68 fprintf(stderr, "Could not open find stream info (error '%s')\n",
70 avformat_close_input(input_format_context);
74 /** Make sure that there is only one stream in the input file. */
75 if ((*input_format_context)->nb_streams != 1) {
76 fprintf(stderr, "Expected one audio input stream, but found %d\n",
77 (*input_format_context)->nb_streams);
78 avformat_close_input(input_format_context);
82 /** Find a decoder for the audio stream. */
83 if (!(input_codec = avcodec_find_decoder((*input_format_context)->streams[0]->codecpar->codec_id))) {
84 fprintf(stderr, "Could not find input codec\n");
85 avformat_close_input(input_format_context);
89 /** allocate a new decoding context */
90 avctx = avcodec_alloc_context3(input_codec);
92 fprintf(stderr, "Could not allocate a decoding context\n");
93 avformat_close_input(input_format_context);
94 return AVERROR(ENOMEM);
97 /** initialize the stream parameters with demuxer information */
98 error = avcodec_parameters_to_context(avctx, (*input_format_context)->streams[0]->codecpar);
100 avformat_close_input(input_format_context);
101 avcodec_free_context(&avctx);
105 /** Open the decoder for the audio stream to use it later. */
106 if ((error = avcodec_open2(avctx, input_codec, NULL)) < 0) {
107 fprintf(stderr, "Could not open input codec (error '%s')\n",
109 avcodec_free_context(&avctx);
110 avformat_close_input(input_format_context);
114 /** Save the decoder context for easier access later. */
115 *input_codec_context = avctx;
121 * Open an output file and the required encoder.
122 * Also set some basic encoder parameters.
123 * Some of these parameters are based on the input file's parameters.
125 static int open_output_file(const char *filename,
126 AVCodecContext *input_codec_context,
127 AVFormatContext **output_format_context,
128 AVCodecContext **output_codec_context)
130 AVCodecContext *avctx = NULL;
131 AVIOContext *output_io_context = NULL;
132 AVStream *stream = NULL;
133 AVCodec *output_codec = NULL;
136 /** Open the output file to write to it. */
137 if ((error = avio_open(&output_io_context, filename,
138 AVIO_FLAG_WRITE)) < 0) {
139 fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
140 filename, av_err2str(error));
144 /** Create a new format context for the output container format. */
145 if (!(*output_format_context = avformat_alloc_context())) {
146 fprintf(stderr, "Could not allocate output format context\n");
147 return AVERROR(ENOMEM);
150 /** Associate the output file (pointer) with the container format context. */
151 (*output_format_context)->pb = output_io_context;
153 /** Guess the desired container format based on the file extension. */
154 if (!((*output_format_context)->oformat = av_guess_format(NULL, filename,
156 fprintf(stderr, "Could not find output file format\n");
160 av_strlcpy((*output_format_context)->filename, filename,
161 sizeof((*output_format_context)->filename));
163 /** Find the encoder to be used by its name. */
164 if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) {
165 fprintf(stderr, "Could not find an AAC encoder.\n");
169 /** Create a new audio stream in the output file container. */
170 if (!(stream = avformat_new_stream(*output_format_context, NULL))) {
171 fprintf(stderr, "Could not create new stream\n");
172 error = AVERROR(ENOMEM);
176 avctx = avcodec_alloc_context3(output_codec);
178 fprintf(stderr, "Could not allocate an encoding context\n");
179 error = AVERROR(ENOMEM);
184 * Set the basic encoder parameters.
185 * The input file's sample rate is used to avoid a sample rate conversion.
187 avctx->channels = OUTPUT_CHANNELS;
188 avctx->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
189 avctx->sample_rate = input_codec_context->sample_rate;
190 avctx->sample_fmt = output_codec->sample_fmts[0];
191 avctx->bit_rate = OUTPUT_BIT_RATE;
193 /** Allow the use of the experimental AAC encoder */
194 avctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
196 /** Set the sample rate for the container. */
197 stream->time_base.den = input_codec_context->sample_rate;
198 stream->time_base.num = 1;
201 * Some container formats (like MP4) require global headers to be present
202 * Mark the encoder so that it behaves accordingly.
204 if ((*output_format_context)->oformat->flags & AVFMT_GLOBALHEADER)
205 avctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
207 /** Open the encoder for the audio stream to use it later. */
208 if ((error = avcodec_open2(avctx, output_codec, NULL)) < 0) {
209 fprintf(stderr, "Could not open output codec (error '%s')\n",
214 error = avcodec_parameters_from_context(stream->codecpar, avctx);
216 fprintf(stderr, "Could not initialize stream parameters\n");
220 /** Save the encoder context for easier access later. */
221 *output_codec_context = avctx;
226 avcodec_free_context(&avctx);
227 avio_closep(&(*output_format_context)->pb);
228 avformat_free_context(*output_format_context);
229 *output_format_context = NULL;
230 return error < 0 ? error : AVERROR_EXIT;
233 /** Initialize one data packet for reading or writing. */
234 static void init_packet(AVPacket *packet)
236 av_init_packet(packet);
237 /** Set the packet data and size so that it is recognized as being empty. */
242 /** Initialize one audio frame for reading from the input file */
243 static int init_input_frame(AVFrame **frame)
245 if (!(*frame = av_frame_alloc())) {
246 fprintf(stderr, "Could not allocate input frame\n");
247 return AVERROR(ENOMEM);
253 * Initialize the audio resampler based on the input and output codec settings.
254 * If the input and output sample formats differ, a conversion is required
255 * libswresample takes care of this, but requires initialization.
257 static int init_resampler(AVCodecContext *input_codec_context,
258 AVCodecContext *output_codec_context,
259 SwrContext **resample_context)
264 * Create a resampler context for the conversion.
265 * Set the conversion parameters.
266 * Default channel layouts based on the number of channels
267 * are assumed for simplicity (they are sometimes not detected
268 * properly by the demuxer and/or decoder).
270 *resample_context = swr_alloc_set_opts(NULL,
271 av_get_default_channel_layout(output_codec_context->channels),
272 output_codec_context->sample_fmt,
273 output_codec_context->sample_rate,
274 av_get_default_channel_layout(input_codec_context->channels),
275 input_codec_context->sample_fmt,
276 input_codec_context->sample_rate,
278 if (!*resample_context) {
279 fprintf(stderr, "Could not allocate resample context\n");
280 return AVERROR(ENOMEM);
283 * Perform a sanity check so that the number of converted samples is
284 * not greater than the number of samples to be converted.
285 * If the sample rates differ, this case has to be handled differently
287 av_assert0(output_codec_context->sample_rate == input_codec_context->sample_rate);
289 /** Open the resampler with the specified parameters. */
290 if ((error = swr_init(*resample_context)) < 0) {
291 fprintf(stderr, "Could not open resample context\n");
292 swr_free(resample_context);
298 /** Initialize a FIFO buffer for the audio samples to be encoded. */
299 static int init_fifo(AVAudioFifo **fifo, AVCodecContext *output_codec_context)
301 /** Create the FIFO buffer based on the specified output sample format. */
302 if (!(*fifo = av_audio_fifo_alloc(output_codec_context->sample_fmt,
303 output_codec_context->channels, 1))) {
304 fprintf(stderr, "Could not allocate FIFO\n");
305 return AVERROR(ENOMEM);
310 /** Write the header of the output file container. */
311 static int write_output_file_header(AVFormatContext *output_format_context)
314 if ((error = avformat_write_header(output_format_context, NULL)) < 0) {
315 fprintf(stderr, "Could not write output file header (error '%s')\n",
322 /** Decode one audio frame from the input file. */
323 static int decode_audio_frame(AVFrame *frame,
324 AVFormatContext *input_format_context,
325 AVCodecContext *input_codec_context,
326 int *data_present, int *finished)
328 /** Packet used for temporary storage. */
329 AVPacket input_packet;
331 init_packet(&input_packet);
333 /** Read one audio frame from the input file into a temporary packet. */
334 if ((error = av_read_frame(input_format_context, &input_packet)) < 0) {
335 /** If we are at the end of the file, flush the decoder below. */
336 if (error == AVERROR_EOF)
339 fprintf(stderr, "Could not read frame (error '%s')\n",
346 * Decode the audio frame stored in the temporary packet.
347 * The input audio stream decoder is used to do this.
348 * If we are at the end of the file, pass an empty packet to the decoder
351 if ((error = avcodec_decode_audio4(input_codec_context, frame,
352 data_present, &input_packet)) < 0) {
353 fprintf(stderr, "Could not decode frame (error '%s')\n",
355 av_packet_unref(&input_packet);
360 * If the decoder has not been flushed completely, we are not finished,
361 * so that this function has to be called again.
363 if (*finished && *data_present)
365 av_packet_unref(&input_packet);
370 * Initialize a temporary storage for the specified number of audio samples.
371 * The conversion requires temporary storage due to the different format.
372 * The number of audio samples to be allocated is specified in frame_size.
374 static int init_converted_samples(uint8_t ***converted_input_samples,
375 AVCodecContext *output_codec_context,
381 * Allocate as many pointers as there are audio channels.
382 * Each pointer will later point to the audio samples of the corresponding
383 * channels (although it may be NULL for interleaved formats).
385 if (!(*converted_input_samples = calloc(output_codec_context->channels,
386 sizeof(**converted_input_samples)))) {
387 fprintf(stderr, "Could not allocate converted input sample pointers\n");
388 return AVERROR(ENOMEM);
392 * Allocate memory for the samples of all channels in one consecutive
393 * block for convenience.
395 if ((error = av_samples_alloc(*converted_input_samples, NULL,
396 output_codec_context->channels,
398 output_codec_context->sample_fmt, 0)) < 0) {
400 "Could not allocate converted input samples (error '%s')\n",
402 av_freep(&(*converted_input_samples)[0]);
403 free(*converted_input_samples);
410 * Convert the input audio samples into the output sample format.
411 * The conversion happens on a per-frame basis, the size of which is specified
414 static int convert_samples(const uint8_t **input_data,
415 uint8_t **converted_data, const int frame_size,
416 SwrContext *resample_context)
420 /** Convert the samples using the resampler. */
421 if ((error = swr_convert(resample_context,
422 converted_data, frame_size,
423 input_data , frame_size)) < 0) {
424 fprintf(stderr, "Could not convert input samples (error '%s')\n",
432 /** Add converted input audio samples to the FIFO buffer for later processing. */
433 static int add_samples_to_fifo(AVAudioFifo *fifo,
434 uint8_t **converted_input_samples,
435 const int frame_size)
440 * Make the FIFO as large as it needs to be to hold both,
441 * the old and the new samples.
443 if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
444 fprintf(stderr, "Could not reallocate FIFO\n");
448 /** Store the new samples in the FIFO buffer. */
449 if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
450 frame_size) < frame_size) {
451 fprintf(stderr, "Could not write data to FIFO\n");
458 * Read one audio frame from the input file, decodes, converts and stores
459 * it in the FIFO buffer.
461 static int read_decode_convert_and_store(AVAudioFifo *fifo,
462 AVFormatContext *input_format_context,
463 AVCodecContext *input_codec_context,
464 AVCodecContext *output_codec_context,
465 SwrContext *resampler_context,
468 /** Temporary storage of the input samples of the frame read from the file. */
469 AVFrame *input_frame = NULL;
470 /** Temporary storage for the converted input samples. */
471 uint8_t **converted_input_samples = NULL;
473 int ret = AVERROR_EXIT;
475 /** Initialize temporary storage for one input frame. */
476 if (init_input_frame(&input_frame))
478 /** Decode one frame worth of audio samples. */
479 if (decode_audio_frame(input_frame, input_format_context,
480 input_codec_context, &data_present, finished))
483 * If we are at the end of the file and there are no more samples
484 * in the decoder which are delayed, we are actually finished.
485 * This must not be treated as an error.
487 if (*finished && !data_present) {
491 /** If there is decoded data, convert and store it */
493 /** Initialize the temporary storage for the converted input samples. */
494 if (init_converted_samples(&converted_input_samples, output_codec_context,
495 input_frame->nb_samples))
499 * Convert the input samples to the desired output sample format.
500 * This requires a temporary storage provided by converted_input_samples.
502 if (convert_samples((const uint8_t**)input_frame->extended_data, converted_input_samples,
503 input_frame->nb_samples, resampler_context))
506 /** Add the converted input samples to the FIFO buffer for later processing. */
507 if (add_samples_to_fifo(fifo, converted_input_samples,
508 input_frame->nb_samples))
515 if (converted_input_samples) {
516 av_freep(&converted_input_samples[0]);
517 free(converted_input_samples);
519 av_frame_free(&input_frame);
525 * Initialize one input frame for writing to the output file.
526 * The frame will be exactly frame_size samples large.
528 static int init_output_frame(AVFrame **frame,
529 AVCodecContext *output_codec_context,
534 /** Create a new frame to store the audio samples. */
535 if (!(*frame = av_frame_alloc())) {
536 fprintf(stderr, "Could not allocate output frame\n");
541 * Set the frame's parameters, especially its size and format.
542 * av_frame_get_buffer needs this to allocate memory for the
543 * audio samples of the frame.
544 * Default channel layouts based on the number of channels
545 * are assumed for simplicity.
547 (*frame)->nb_samples = frame_size;
548 (*frame)->channel_layout = output_codec_context->channel_layout;
549 (*frame)->format = output_codec_context->sample_fmt;
550 (*frame)->sample_rate = output_codec_context->sample_rate;
553 * Allocate the samples of the created frame. This call will make
554 * sure that the audio frame can hold as many samples as specified.
556 if ((error = av_frame_get_buffer(*frame, 0)) < 0) {
557 fprintf(stderr, "Could not allocate output frame samples (error '%s')\n",
559 av_frame_free(frame);
566 /** Global timestamp for the audio frames */
567 static int64_t pts = 0;
569 /** Encode one frame worth of audio to the output file. */
570 static int encode_audio_frame(AVFrame *frame,
571 AVFormatContext *output_format_context,
572 AVCodecContext *output_codec_context,
575 /** Packet used for temporary storage. */
576 AVPacket output_packet;
578 init_packet(&output_packet);
580 /** Set a timestamp based on the sample rate for the container. */
583 pts += frame->nb_samples;
587 * Encode the audio frame and store it in the temporary packet.
588 * The output audio stream encoder is used to do this.
590 if ((error = avcodec_encode_audio2(output_codec_context, &output_packet,
591 frame, data_present)) < 0) {
592 fprintf(stderr, "Could not encode frame (error '%s')\n",
594 av_packet_unref(&output_packet);
598 /** Write one audio frame from the temporary packet to the output file. */
600 if ((error = av_write_frame(output_format_context, &output_packet)) < 0) {
601 fprintf(stderr, "Could not write frame (error '%s')\n",
603 av_packet_unref(&output_packet);
607 av_packet_unref(&output_packet);
614 * Load one audio frame from the FIFO buffer, encode and write it to the
617 static int load_encode_and_write(AVAudioFifo *fifo,
618 AVFormatContext *output_format_context,
619 AVCodecContext *output_codec_context)
621 /** Temporary storage of the output samples of the frame written to the file. */
622 AVFrame *output_frame;
624 * Use the maximum number of possible samples per frame.
625 * If there is less than the maximum possible frame size in the FIFO
626 * buffer use this number. Otherwise, use the maximum possible frame size
628 const int frame_size = FFMIN(av_audio_fifo_size(fifo),
629 output_codec_context->frame_size);
632 /** Initialize temporary storage for one output frame. */
633 if (init_output_frame(&output_frame, output_codec_context, frame_size))
637 * Read as many samples from the FIFO buffer as required to fill the frame.
638 * The samples are stored in the frame temporarily.
640 if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) {
641 fprintf(stderr, "Could not read data from FIFO\n");
642 av_frame_free(&output_frame);
646 /** Encode one frame worth of audio samples. */
647 if (encode_audio_frame(output_frame, output_format_context,
648 output_codec_context, &data_written)) {
649 av_frame_free(&output_frame);
652 av_frame_free(&output_frame);
656 /** Write the trailer of the output file container. */
657 static int write_output_file_trailer(AVFormatContext *output_format_context)
660 if ((error = av_write_trailer(output_format_context)) < 0) {
661 fprintf(stderr, "Could not write output file trailer (error '%s')\n",
668 /** Convert an audio file to an AAC file in an MP4 container. */
669 int main(int argc, char **argv)
671 AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
672 AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
673 SwrContext *resample_context = NULL;
674 AVAudioFifo *fifo = NULL;
675 int ret = AVERROR_EXIT;
678 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
682 /** Register all codecs and formats so that they can be used. */
684 /** Open the input file for reading. */
685 if (open_input_file(argv[1], &input_format_context,
686 &input_codec_context))
688 /** Open the output file for writing. */
689 if (open_output_file(argv[2], input_codec_context,
690 &output_format_context, &output_codec_context))
692 /** Initialize the resampler to be able to convert audio sample formats. */
693 if (init_resampler(input_codec_context, output_codec_context,
696 /** Initialize the FIFO buffer to store audio samples to be encoded. */
697 if (init_fifo(&fifo, output_codec_context))
699 /** Write the header of the output file container. */
700 if (write_output_file_header(output_format_context))
704 * Loop as long as we have input samples to read or output samples
705 * to write; abort as soon as we have neither.
708 /** Use the encoder's desired frame size for processing. */
709 const int output_frame_size = output_codec_context->frame_size;
713 * Make sure that there is one frame worth of samples in the FIFO
714 * buffer so that the encoder can do its work.
715 * Since the decoder's and the encoder's frame size may differ, we
716 * need to FIFO buffer to store as many frames worth of input samples
717 * that they make up at least one frame worth of output samples.
719 while (av_audio_fifo_size(fifo) < output_frame_size) {
721 * Decode one frame worth of audio samples, convert it to the
722 * output sample format and put it into the FIFO buffer.
724 if (read_decode_convert_and_store(fifo, input_format_context,
726 output_codec_context,
727 resample_context, &finished))
731 * If we are at the end of the input file, we continue
732 * encoding the remaining audio samples to the output file.
739 * If we have enough samples for the encoder, we encode them.
740 * At the end of the file, we pass the remaining samples to
743 while (av_audio_fifo_size(fifo) >= output_frame_size ||
744 (finished && av_audio_fifo_size(fifo) > 0))
746 * Take one frame worth of audio samples from the FIFO buffer,
747 * encode it and write it to the output file.
749 if (load_encode_and_write(fifo, output_format_context,
750 output_codec_context))
754 * If we are at the end of the input file and have encoded
755 * all remaining samples, we can exit this loop and finish.
759 /** Flush the encoder as it may have delayed frames. */
761 if (encode_audio_frame(NULL, output_format_context,
762 output_codec_context, &data_written))
764 } while (data_written);
769 /** Write the trailer of the output file container. */
770 if (write_output_file_trailer(output_format_context))
776 av_audio_fifo_free(fifo);
777 swr_free(&resample_context);
778 if (output_codec_context)
779 avcodec_free_context(&output_codec_context);
780 if (output_format_context) {
781 avio_closep(&output_format_context->pb);
782 avformat_free_context(output_format_context);
784 if (input_codec_context)
785 avcodec_free_context(&input_codec_context);
786 if (input_format_context)
787 avformat_close_input(&input_format_context);