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
21 * simple audio converter
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)
30 #include "libavformat/avformat.h"
31 #include "libavformat/avio.h"
33 #include "libavcodec/avcodec.h"
35 #include "libavutil/audio_fifo.h"
36 #include "libavutil/avstring.h"
37 #include "libavutil/frame.h"
38 #include "libavutil/opt.h"
40 #include "libavresample/avresample.h"
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
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)
54 static char *const get_error_text(const int error)
56 static char error_buffer[255];
57 av_strerror(error, error_buffer, sizeof(error_buffer));
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)
69 /** Open the input file to read from it. */
70 if ((error = avformat_open_input(input_format_context, filename, NULL,
72 fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
73 filename, get_error_text(error));
74 *input_format_context = NULL;
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);
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);
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);
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);
110 /** Save the decoder context for easier access later. */
111 *input_codec_context = (*input_format_context)->streams[0]->codec;
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.
121 static int open_output_file(const char *filename,
122 AVCodecContext *input_codec_context,
123 AVFormatContext **output_format_context,
124 AVCodecContext **output_codec_context)
126 AVIOContext *output_io_context = NULL;
127 AVStream *stream = NULL;
128 AVCodec *output_codec = NULL;
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));
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);
145 /** Associate the output file (pointer) with the container format context. */
146 (*output_format_context)->pb = output_io_context;
148 /** Guess the desired container format based on the file extension. */
149 if (!((*output_format_context)->oformat = av_guess_format(NULL, filename,
151 fprintf(stderr, "Could not find output file format\n");
155 av_strlcpy((*output_format_context)->filename, filename,
156 sizeof((*output_format_context)->filename));
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");
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);
171 /** Save the encoder context for easiert access later. */
172 *output_codec_context = stream->codec;
175 * Set the basic encoder parameters.
176 * The input file's sample rate is used to avoid a sample rate conversion.
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;
185 * Some container formats (like MP4) require global headers to be present
186 * Mark the encoder so that it behaves accordingly.
188 if ((*output_format_context)->oformat->flags & AVFMT_GLOBALHEADER)
189 (*output_codec_context)->flags |= CODEC_FLAG_GLOBAL_HEADER;
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));
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;
207 /** Initialize one data packet for reading or writing. */
208 static void init_packet(AVPacket *packet)
210 av_init_packet(packet);
211 /** Set the packet data and size so that it is recognized as being empty. */
216 /** Initialize one audio frame for reading from the input file */
217 static int init_input_frame(AVFrame **frame)
219 if (!(*frame = av_frame_alloc())) {
220 fprintf(stderr, "Could not allocate input frame\n");
221 return AVERROR(ENOMEM);
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.
231 static int init_resampler(AVCodecContext *input_codec_context,
232 AVCodecContext *output_codec_context,
233 AVAudioResampleContext **resample_context)
236 * Only initialize the resampler if it is necessary, i.e.,
237 * if and only if the sample formats differ.
239 if (input_codec_context->sample_fmt != output_codec_context->sample_fmt ||
240 input_codec_context->channels != output_codec_context->channels) {
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);
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).
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);
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);
278 /** Initialize a FIFO buffer for the audio samples to be encoded. */
279 static int init_fifo(AVAudioFifo **fifo)
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);
289 /** Write the header of the output file container. */
290 static int write_output_file_header(AVFormatContext *output_format_context)
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));
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)
307 /** Packet used for temporary storage. */
308 AVPacket input_packet;
310 init_packet(&input_packet);
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)
318 fprintf(stderr, "Could not read frame (error '%s')\n",
319 get_error_text(error));
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
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);
339 * If the decoder has not been flushed completely, we are not finished,
340 * so that this function has to be called again.
342 if (*finished && *data_present)
344 av_free_packet(&input_packet);
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.
353 static int init_converted_samples(uint8_t ***converted_input_samples,
354 AVCodecContext *output_codec_context,
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).
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);
371 * Allocate memory for the samples of all channels in one consecutive
372 * block for convenience.
374 if ((error = av_samples_alloc(*converted_input_samples, NULL,
375 output_codec_context->channels,
377 output_codec_context->sample_fmt, 0)) < 0) {
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);
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
393 static int convert_samples(uint8_t **input_data,
394 uint8_t **converted_data, const int frame_size,
395 AVAudioResampleContext *resample_context)
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));
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
412 if (avresample_available(resample_context)) {
413 fprintf(stderr, "Converted samples left over\n");
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)
428 * Make the FIFO as large as it needs to be to hold both,
429 * the old and the new samples.
431 if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
432 fprintf(stderr, "Could not reallocate FIFO\n");
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");
446 * Read one audio frame from the input file, decodes, converts and stores
447 * it in the FIFO buffer.
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,
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;
461 int ret = AVERROR_EXIT;
463 /** Initialize temporary storage for one input frame. */
464 if (init_input_frame(&input_frame))
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))
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.
475 if (*finished && !data_present) {
479 /** If there is decoded data, convert and store it */
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))
487 * Convert the input samples to the desired output sample format.
488 * This requires a temporary storage provided by converted_input_samples.
490 if (convert_samples(input_frame->extended_data, converted_input_samples,
491 input_frame->nb_samples, resampler_context))
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))
503 if (converted_input_samples) {
504 av_freep(&converted_input_samples[0]);
505 free(converted_input_samples);
507 av_frame_free(&input_frame);
513 * Initialize one input frame for writing to the output file.
514 * The frame will be exactly frame_size samples large.
516 static int init_output_frame(AVFrame **frame,
517 AVCodecContext *output_codec_context,
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");
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.
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;
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.
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);
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,
560 /** Packet used for temporary storage. */
561 AVPacket output_packet;
563 init_packet(&output_packet);
566 * Encode the audio frame and store it in the temporary packet.
567 * The output audio stream encoder is used to do this.
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);
577 /** Write one audio frame from the temporary packet to the output file. */
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);
586 av_free_packet(&output_packet);
593 * Load one audio frame from the FIFO buffer, encode and write it to the
596 static int load_encode_and_write(AVAudioFifo *fifo,
597 AVFormatContext *output_format_context,
598 AVCodecContext *output_codec_context)
600 /** Temporary storage of the output samples of the frame written to the file. */
601 AVFrame *output_frame;
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
607 const int frame_size = FFMIN(av_audio_fifo_size(fifo),
608 output_codec_context->frame_size);
611 /** Initialize temporary storage for one output frame. */
612 if (init_output_frame(&output_frame, output_codec_context, frame_size))
616 * Read as many samples from the FIFO buffer as required to fill the frame.
617 * The samples are stored in the frame temporarily.
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);
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);
631 av_frame_free(&output_frame);
635 /** Write the trailer of the output file container. */
636 static int write_output_file_trailer(AVFormatContext *output_format_context)
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));
647 /** Convert an audio file to an AAC file in an MP4 container. */
648 int main(int argc, char **argv)
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;
657 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
661 /** Register all codecs and formats so that they can be used. */
663 /** Open the input file for reading. */
664 if (open_input_file(argv[1], &input_format_context,
665 &input_codec_context))
667 /** Open the output file for writing. */
668 if (open_output_file(argv[2], input_codec_context,
669 &output_format_context, &output_codec_context))
671 /** Initialize the resampler to be able to convert audio sample formats. */
672 if (init_resampler(input_codec_context, output_codec_context,
675 /** Initialize the FIFO buffer to store audio samples to be encoded. */
676 if (init_fifo(&fifo))
678 /** Write the header of the output file container. */
679 if (write_output_file_header(output_format_context))
683 * Loop as long as we have input samples to read or output samples
684 * to write; abort as soon as we have neither.
687 /** Use the encoder's desired frame size for processing. */
688 const int output_frame_size = output_codec_context->frame_size;
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.
698 while (av_audio_fifo_size(fifo) < output_frame_size) {
700 * Decode one frame worth of audio samples, convert it to the
701 * output sample format and put it into the FIFO buffer.
703 if (read_decode_convert_and_store(fifo, input_format_context,
705 output_codec_context,
706 resample_context, &finished))
710 * If we are at the end of the input file, we continue
711 * encoding the remaining audio samples to the output file.
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
722 while (av_audio_fifo_size(fifo) >= output_frame_size ||
723 (finished && av_audio_fifo_size(fifo) > 0))
725 * Take one frame worth of audio samples from the FIFO buffer,
726 * encode it and write it to the output file.
728 if (load_encode_and_write(fifo, output_format_context,
729 output_codec_context))
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.
738 /** Flush the encoder as it may have delayed frames. */
740 if (encode_audio_frame(NULL, output_format_context,
741 output_codec_context, &data_written))
743 } while (data_written);
748 /** Write the trailer of the output file container. */
749 if (write_output_file_trailer(output_format_context))
755 av_audio_fifo_free(fifo);
756 if (resample_context) {
757 avresample_close(resample_context);
758 avresample_free(&resample_context);
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);
766 if (input_codec_context)
767 avcodec_close(input_codec_context);
768 if (input_format_context)
769 avformat_close_input(&input_format_context);