2 * This file is part of Libav.
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.
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.
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
20 * @file simple audio converter
21 * Convert an input audio file to AAC in an MP4 container using Libav.
22 * @author Andreas Unterweger (dustsigns@gmail.com)
27 #include "libavformat/avformat.h"
28 #include "libavformat/avio.h"
30 #include "libavcodec/avcodec.h"
32 #include "libavutil/audio_fifo.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/opt.h"
37 #include "libavresample/avresample.h"
39 /** The output bit rate in kbit/s */
40 #define OUTPUT_BIT_RATE 48000
41 /** The number of output channels */
42 #define OUTPUT_CHANNELS 2
43 /** The audio sample output format */
44 #define OUTPUT_SAMPLE_FORMAT AV_SAMPLE_FMT_S16
47 * Convert an error code into a text message.
48 * @param error Error code to be converted
49 * @return Corresponding error text (not thread-safe)
51 static char *const get_error_text(const int error)
53 static char error_buffer[255];
54 av_strerror(error, error_buffer, sizeof(error_buffer));
58 /** Open an input file and the required decoder. */
59 static int open_input_file(const char *filename,
60 AVFormatContext **input_format_context,
61 AVCodecContext **input_codec_context)
66 /** Open the input file to read from it. */
67 if ((error = avformat_open_input(input_format_context, filename, NULL,
69 fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
70 filename, get_error_text(error));
71 *input_format_context = NULL;
75 /** Get information on the input file (number of streams etc.). */
76 if ((error = avformat_find_stream_info(*input_format_context, NULL)) < 0) {
77 fprintf(stderr, "Could not open find stream info (error '%s')\n",
78 get_error_text(error));
79 avformat_close_input(input_format_context);
83 /** Make sure that there is only one stream in the input file. */
84 if ((*input_format_context)->nb_streams != 1) {
85 fprintf(stderr, "Expected one audio input stream, but found %d\n",
86 (*input_format_context)->nb_streams);
87 avformat_close_input(input_format_context);
91 /** Find a decoder for the audio stream. */
92 if (!(input_codec = avcodec_find_decoder((*input_format_context)->streams[0]->codec->codec_id))) {
93 fprintf(stderr, "Could not find input codec\n");
94 avformat_close_input(input_format_context);
98 /** Open the decoder for the audio stream to use it later. */
99 if ((error = avcodec_open2((*input_format_context)->streams[0]->codec,
100 input_codec, NULL)) < 0) {
101 fprintf(stderr, "Could not open input codec (error '%s')\n",
102 get_error_text(error));
103 avformat_close_input(input_format_context);
107 /** Save the decoder context for easier access later. */
108 *input_codec_context = (*input_format_context)->streams[0]->codec;
114 * Open an output file and the required encoder.
115 * Also set some basic encoder parameters.
116 * Some of these parameters are based on the input file's parameters.
118 static int open_output_file(const char *filename,
119 AVCodecContext *input_codec_context,
120 AVFormatContext **output_format_context,
121 AVCodecContext **output_codec_context)
123 AVIOContext *output_io_context = NULL;
124 AVStream *stream = NULL;
125 AVCodec *output_codec = NULL;
128 /** Open the output file to write to it. */
129 if ((error = avio_open(&output_io_context, filename,
130 AVIO_FLAG_WRITE)) < 0) {
131 fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
132 filename, get_error_text(error));
136 /** Create a new format context for the output container format. */
137 if (!(*output_format_context = avformat_alloc_context())) {
138 fprintf(stderr, "Could not allocate output format context\n");
139 return AVERROR(ENOMEM);
142 /** Associate the output file (pointer) with the container format context. */
143 (*output_format_context)->pb = output_io_context;
145 /** Guess the desired container format based on the file extension. */
146 if (!((*output_format_context)->oformat = av_guess_format(NULL, filename,
148 fprintf(stderr, "Could not find output file format\n");
152 av_strlcpy((*output_format_context)->filename, filename,
153 sizeof((*output_format_context)->filename));
155 /** Find the encoder to be used by its name. */
156 if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) {
157 fprintf(stderr, "Could not find an AAC encoder.\n");
161 /** Create a new audio stream in the output file container. */
162 if (!(stream = avformat_new_stream(*output_format_context, output_codec))) {
163 fprintf(stderr, "Could not create new stream\n");
164 error = AVERROR(ENOMEM);
168 /** Save the encoder context for easiert access later. */
169 *output_codec_context = stream->codec;
172 * Set the basic encoder parameters.
173 * The input file's sample rate is used to avoid a sample rate conversion.
175 (*output_codec_context)->channels = OUTPUT_CHANNELS;
176 (*output_codec_context)->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
177 (*output_codec_context)->sample_rate = input_codec_context->sample_rate;
178 (*output_codec_context)->sample_fmt = AV_SAMPLE_FMT_S16;
179 (*output_codec_context)->bit_rate = OUTPUT_BIT_RATE;
182 * Some container formats (like MP4) require global headers to be present
183 * Mark the encoder so that it behaves accordingly.
185 if ((*output_format_context)->oformat->flags & AVFMT_GLOBALHEADER)
186 (*output_codec_context)->flags |= CODEC_FLAG_GLOBAL_HEADER;
188 /** Open the encoder for the audio stream to use it later. */
189 if ((error = avcodec_open2(*output_codec_context, output_codec, NULL)) < 0) {
190 fprintf(stderr, "Could not open output codec (error '%s')\n",
191 get_error_text(error));
198 avio_close((*output_format_context)->pb);
199 avformat_free_context(*output_format_context);
200 *output_format_context = NULL;
201 return error < 0 ? error : AVERROR_EXIT;
204 /** Initialize one data packet for reading or writing. */
205 static void init_packet(AVPacket *packet)
207 av_init_packet(packet);
208 /** Set the packet data and size so that it is recognized as being empty. */
213 /** Initialize one audio frame for reading from the input file */
214 static int init_input_frame(AVFrame **frame)
216 if (!(*frame = av_frame_alloc())) {
217 fprintf(stderr, "Could not allocate input frame\n");
218 return AVERROR(ENOMEM);
224 * Initialize the audio resampler based on the input and output codec settings.
225 * If the input and output sample formats differ, a conversion is required
226 * libavresample takes care of this, but requires initialization.
228 static int init_resampler(AVCodecContext *input_codec_context,
229 AVCodecContext *output_codec_context,
230 AVAudioResampleContext **resample_context)
233 * Only initialize the resampler if it is necessary, i.e.,
234 * if and only if the sample formats differ.
236 if (input_codec_context->sample_fmt != output_codec_context->sample_fmt ||
237 input_codec_context->channels != output_codec_context->channels) {
240 /** Create a resampler context for the conversion. */
241 if (!(*resample_context = avresample_alloc_context())) {
242 fprintf(stderr, "Could not allocate resample context\n");
243 return AVERROR(ENOMEM);
247 * Set the conversion parameters.
248 * Default channel layouts based on the number of channels
249 * are assumed for simplicity (they are sometimes not detected
250 * properly by the demuxer and/or decoder).
252 av_opt_set_int(*resample_context, "in_channel_layout",
253 av_get_default_channel_layout(input_codec_context->channels), 0);
254 av_opt_set_int(*resample_context, "out_channel_layout",
255 av_get_default_channel_layout(output_codec_context->channels), 0);
256 av_opt_set_int(*resample_context, "in_sample_rate",
257 input_codec_context->sample_rate, 0);
258 av_opt_set_int(*resample_context, "out_sample_rate",
259 output_codec_context->sample_rate, 0);
260 av_opt_set_int(*resample_context, "in_sample_fmt",
261 input_codec_context->sample_fmt, 0);
262 av_opt_set_int(*resample_context, "out_sample_fmt",
263 output_codec_context->sample_fmt, 0);
265 /** Open the resampler with the specified parameters. */
266 if ((error = avresample_open(*resample_context)) < 0) {
267 fprintf(stderr, "Could not open resample context\n");
268 avresample_free(resample_context);
275 /** Initialize a FIFO buffer for the audio samples to be encoded. */
276 static int init_fifo(AVAudioFifo **fifo)
278 /** Create the FIFO buffer based on the specified output sample format. */
279 if (!(*fifo = av_audio_fifo_alloc(OUTPUT_SAMPLE_FORMAT, OUTPUT_CHANNELS, 1))) {
280 fprintf(stderr, "Could not allocate FIFO\n");
281 return AVERROR(ENOMEM);
286 /** Write the header of the output file container. */
287 static int write_output_file_header(AVFormatContext *output_format_context)
290 if ((error = avformat_write_header(output_format_context, NULL)) < 0) {
291 fprintf(stderr, "Could not write output file header (error '%s')\n",
292 get_error_text(error));
298 /** Decode one audio frame from the input file. */
299 static int decode_audio_frame(AVFrame *frame,
300 AVFormatContext *input_format_context,
301 AVCodecContext *input_codec_context,
302 int *data_present, int *finished)
304 /** Packet used for temporary storage. */
305 AVPacket input_packet;
307 init_packet(&input_packet);
309 /** Read one audio frame from the input file into a temporary packet. */
310 if ((error = av_read_frame(input_format_context, &input_packet)) < 0) {
311 /** If we are the the end of the file, flush the decoder below. */
312 if (error == AVERROR_EOF)
315 fprintf(stderr, "Could not read frame (error '%s')\n",
316 get_error_text(error));
322 * Decode the audio frame stored in the temporary packet.
323 * The input audio stream decoder is used to do this.
324 * If we are at the end of the file, pass an empty packet to the decoder
327 if ((error = avcodec_decode_audio4(input_codec_context, frame,
328 data_present, &input_packet)) < 0) {
329 fprintf(stderr, "Could not decode frame (error '%s')\n",
330 get_error_text(error));
331 av_free_packet(&input_packet);
336 * If the decoder has not been flushed completely, we are not finished,
337 * so that this function has to be called again.
339 if (*finished && *data_present)
341 av_free_packet(&input_packet);
346 * Initialize a temporary storage for the specified number of audio samples.
347 * The conversion requires temporary storage due to the different format.
348 * The number of audio samples to be allocated is specified in frame_size.
350 static int init_converted_samples(uint8_t ***converted_input_samples,
351 AVCodecContext *output_codec_context,
357 * Allocate as many pointers as there are audio channels.
358 * Each pointer will later point to the audio samples of the corresponding
359 * channels (although it may be NULL for interleaved formats).
361 if (!(*converted_input_samples = calloc(output_codec_context->channels,
362 sizeof(**converted_input_samples)))) {
363 fprintf(stderr, "Could not allocate converted input sample pointers\n");
364 return AVERROR(ENOMEM);
368 * Allocate memory for the samples of all channels in one consecutive
369 * block for convenience.
371 if ((error = av_samples_alloc(*converted_input_samples, NULL,
372 output_codec_context->channels,
374 output_codec_context->sample_fmt, 0)) < 0) {
376 "Could not allocate converted input samples (error '%s')\n",
377 get_error_text(error));
378 av_freep(&(*converted_input_samples)[0]);
379 free(*converted_input_samples);
386 * Convert the input audio samples into the output sample format.
387 * The conversion happens on a per-frame basis, the size of which is specified
390 static int convert_samples(uint8_t **input_data,
391 uint8_t **converted_data, const int frame_size,
392 AVAudioResampleContext *resample_context)
396 /** Convert the samples using the resampler. */
397 if ((error = avresample_convert(resample_context, converted_data, 0,
398 frame_size, input_data, 0, frame_size)) < 0) {
399 fprintf(stderr, "Could not convert input samples (error '%s')\n",
400 get_error_text(error));
405 * Perform a sanity check so that the number of converted samples is
406 * not greater than the number of samples to be converted.
407 * If the sample rates differ, this case has to be handled differently
409 if (avresample_available(resample_context)) {
410 fprintf(stderr, "Converted samples left over\n");
417 /** Add converted input audio samples to the FIFO buffer for later processing. */
418 static int add_samples_to_fifo(AVAudioFifo *fifo,
419 uint8_t **converted_input_samples,
420 const int frame_size)
425 * Make the FIFO as large as it needs to be to hold both,
426 * the old and the new samples.
428 if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
429 fprintf(stderr, "Could not reallocate FIFO\n");
433 /** Store the new samples in the FIFO buffer. */
434 if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
435 frame_size) < frame_size) {
436 fprintf(stderr, "Could not write data to FIFO\n");
443 * Read one audio frame from the input file, decodes, converts and stores
444 * it in the FIFO buffer.
446 static int read_decode_convert_and_store(AVAudioFifo *fifo,
447 AVFormatContext *input_format_context,
448 AVCodecContext *input_codec_context,
449 AVCodecContext *output_codec_context,
450 AVAudioResampleContext *resampler_context,
453 /** Temporary storage of the input samples of the frame read from the file. */
454 AVFrame *input_frame = NULL;
455 /** Temporary storage for the converted input samples. */
456 uint8_t **converted_input_samples = NULL;
458 int ret = AVERROR_EXIT;
460 /** Initialize temporary storage for one input frame. */
461 if (init_input_frame(&input_frame))
463 /** Decode one frame worth of audio samples. */
464 if (decode_audio_frame(input_frame, input_format_context,
465 input_codec_context, &data_present, finished))
468 * If we are at the end of the file and there are no more samples
469 * in the decoder which are delayed, we are actually finished.
470 * This must not be treated as an error.
472 if (*finished && !data_present) {
476 /** If there is decoded data, convert and store it */
478 /** Initialize the temporary storage for the converted input samples. */
479 if (init_converted_samples(&converted_input_samples, output_codec_context,
480 input_frame->nb_samples))
484 * Convert the input samples to the desired output sample format.
485 * This requires a temporary storage provided by converted_input_samples.
487 if (convert_samples(input_frame->extended_data, converted_input_samples,
488 input_frame->nb_samples, resampler_context))
491 /** Add the converted input samples to the FIFO buffer for later processing. */
492 if (add_samples_to_fifo(fifo, converted_input_samples,
493 input_frame->nb_samples))
500 if (converted_input_samples) {
501 av_freep(&converted_input_samples[0]);
502 free(converted_input_samples);
504 av_frame_free(&input_frame);
510 * Initialize one input frame for writing to the output file.
511 * The frame will be exactly frame_size samples large.
513 static int init_output_frame(AVFrame **frame,
514 AVCodecContext *output_codec_context,
519 /** Create a new frame to store the audio samples. */
520 if (!(*frame = av_frame_alloc())) {
521 fprintf(stderr, "Could not allocate output frame\n");
526 * Set the frame's parameters, especially its size and format.
527 * av_frame_get_buffer needs this to allocate memory for the
528 * audio samples of the frame.
529 * Default channel layouts based on the number of channels
530 * are assumed for simplicity.
532 (*frame)->nb_samples = frame_size;
533 (*frame)->channel_layout = output_codec_context->channel_layout;
534 (*frame)->format = output_codec_context->sample_fmt;
535 (*frame)->sample_rate = output_codec_context->sample_rate;
538 * Allocate the samples of the created frame. This call will make
539 * sure that the audio frame can hold as many samples as specified.
541 if ((error = av_frame_get_buffer(*frame, 0)) < 0) {
542 fprintf(stderr, "Could allocate output frame samples (error '%s')\n",
543 get_error_text(error));
544 av_frame_free(frame);
551 /** Encode one frame worth of audio to the output file. */
552 static int encode_audio_frame(AVFrame *frame,
553 AVFormatContext *output_format_context,
554 AVCodecContext *output_codec_context,
557 /** Packet used for temporary storage. */
558 AVPacket output_packet;
560 init_packet(&output_packet);
563 * Encode the audio frame and store it in the temporary packet.
564 * The output audio stream encoder is used to do this.
566 if ((error = avcodec_encode_audio2(output_codec_context, &output_packet,
567 frame, data_present)) < 0) {
568 fprintf(stderr, "Could not encode frame (error '%s')\n",
569 get_error_text(error));
570 av_free_packet(&output_packet);
574 /** Write one audio frame from the temporary packet to the output file. */
576 if ((error = av_write_frame(output_format_context, &output_packet)) < 0) {
577 fprintf(stderr, "Could not write frame (error '%s')\n",
578 get_error_text(error));
579 av_free_packet(&output_packet);
583 av_free_packet(&output_packet);
590 * Load one audio frame from the FIFO buffer, encode and write it to the
593 static int load_encode_and_write(AVAudioFifo *fifo,
594 AVFormatContext *output_format_context,
595 AVCodecContext *output_codec_context)
597 /** Temporary storage of the output samples of the frame written to the file. */
598 AVFrame *output_frame;
600 * Use the maximum number of possible samples per frame.
601 * If there is less than the maximum possible frame size in the FIFO
602 * buffer use this number. Otherwise, use the maximum possible frame size
604 const int frame_size = FFMIN(av_audio_fifo_size(fifo),
605 output_codec_context->frame_size);
608 /** Initialize temporary storage for one output frame. */
609 if (init_output_frame(&output_frame, output_codec_context, frame_size))
613 * Read as many samples from the FIFO buffer as required to fill the frame.
614 * The samples are stored in the frame temporarily.
616 if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) {
617 fprintf(stderr, "Could not read data from FIFO\n");
618 av_frame_free(&output_frame);
622 /** Encode one frame worth of audio samples. */
623 if (encode_audio_frame(output_frame, output_format_context,
624 output_codec_context, &data_written)) {
625 av_frame_free(&output_frame);
628 av_frame_free(&output_frame);
632 /** Write the trailer of the output file container. */
633 static int write_output_file_trailer(AVFormatContext *output_format_context)
636 if ((error = av_write_trailer(output_format_context)) < 0) {
637 fprintf(stderr, "Could not write output file trailer (error '%s')\n",
638 get_error_text(error));
644 /** Convert an audio file to an AAC file in an MP4 container. */
645 int main(int argc, char **argv)
647 AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
648 AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
649 AVAudioResampleContext *resample_context = NULL;
650 AVAudioFifo *fifo = NULL;
651 int ret = AVERROR_EXIT;
654 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
658 /** Register all codecs and formats so that they can be used. */
660 /** Open the input file for reading. */
661 if (open_input_file(argv[1], &input_format_context,
662 &input_codec_context))
664 /** Open the output file for writing. */
665 if (open_output_file(argv[2], input_codec_context,
666 &output_format_context, &output_codec_context))
668 /** Initialize the resampler to be able to convert audio sample formats. */
669 if (init_resampler(input_codec_context, output_codec_context,
672 /** Initialize the FIFO buffer to store audio samples to be encoded. */
673 if (init_fifo(&fifo))
675 /** Write the header of the output file container. */
676 if (write_output_file_header(output_format_context))
680 * Loop as long as we have input samples to read or output samples
681 * to write; abort as soon as we have neither.
684 /** Use the encoder's desired frame size for processing. */
685 const int output_frame_size = output_codec_context->frame_size;
689 * Make sure that there is one frame worth of samples in the FIFO
690 * buffer so that the encoder can do its work.
691 * Since the decoder's and the encoder's frame size may differ, we
692 * need to FIFO buffer to store as many frames worth of input samples
693 * that they make up at least one frame worth of output samples.
695 while (av_audio_fifo_size(fifo) < output_frame_size) {
697 * Decode one frame worth of audio samples, convert it to the
698 * output sample format and put it into the FIFO buffer.
700 if (read_decode_convert_and_store(fifo, input_format_context,
702 output_codec_context,
703 resample_context, &finished))
707 * If we are at the end of the input file, we continue
708 * encoding the remaining audio samples to the output file.
715 * If we have enough samples for the encoder, we encode them.
716 * At the end of the file, we pass the remaining samples to
719 while (av_audio_fifo_size(fifo) >= output_frame_size ||
720 (finished && av_audio_fifo_size(fifo) > 0))
722 * Take one frame worth of audio samples from the FIFO buffer,
723 * encode it and write it to the output file.
725 if (load_encode_and_write(fifo, output_format_context,
726 output_codec_context))
730 * If we are at the end of the input file and have encoded
731 * all remaining samples, we can exit this loop and finish.
735 /** Flush the encoder as it may have delayed frames. */
737 if (encode_audio_frame(NULL, output_format_context,
738 output_codec_context, &data_written))
740 } while (data_written);
745 /** Write the trailer of the output file container. */
746 if (write_output_file_trailer(output_format_context))
752 av_audio_fifo_free(fifo);
753 if (resample_context) {
754 avresample_close(resample_context);
755 avresample_free(&resample_context);
757 if (output_codec_context)
758 avcodec_close(output_codec_context);
759 if (output_format_context) {
760 avio_close(output_format_context->pb);
761 avformat_free_context(output_format_context);
763 if (input_codec_context)
764 avcodec_close(input_codec_context);
765 if (input_format_context)
766 avformat_close_input(&input_format_context);