1 /*
2 * Copyright (c) 2013-2022 Andreas Unterweger
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file audio transcoding to MPEG/AAC API usage example
23 * @example transcode_aac.c
24 *
25 * Convert an input audio file to AAC in an MP4 container. Formats other than
26 * MP4 are supported based on the output file extension.
27 * @author Andreas Unterweger (dustsigns@gmail.com)
28 */
29
30 #include <stdio.h>
31
34
36
43
45
46 /* The output bit rate in bit/s */
47 #define OUTPUT_BIT_RATE 96000
48 /* The number of output channels */
49 #define OUTPUT_CHANNELS 2
50
51 /**
52 * Open an input file and the required decoder.
53 * @param filename File to be opened
54 * @param[out] input_format_context Format context of opened file
55 * @param[out] input_codec_context Codec context of opened file
56 * @return Error code (0 if successful)
57 */
61 {
66
67 /* Open the input file to read from it. */
70 fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
72 *input_format_context =
NULL;
74 }
75
76 /* Get information on the input file (number of streams etc.). */
78 fprintf(stderr, "Could not open find stream info (error '%s')\n",
82 }
83
84 /* Make sure that there is only one stream in the input file. */
85 if ((*input_format_context)->nb_streams != 1) {
86 fprintf(stderr, "Expected one audio input stream, but found %d\n",
87 (*input_format_context)->nb_streams);
90 }
91
92 stream = (*input_format_context)->streams[0];
93
94 /* Find a decoder for the audio stream. */
96 fprintf(stderr, "Could not find input codec\n");
99 }
100
101 /* Allocate a new decoding context. */
103 if (!avctx) {
104 fprintf(stderr, "Could not allocate a decoding context\n");
107 }
108
109 /* Initialize the stream parameters with demuxer information. */
115 }
116
117 /* Open the decoder for the audio stream to use it later. */
119 fprintf(stderr, "Could not open input codec (error '%s')\n",
124 }
125
126 /* Set the packet timebase for the decoder. */
128
129 /* Save the decoder context for easier access later. */
130 *input_codec_context = avctx;
131
132 return 0;
133 }
134
135 /**
136 * Open an output file and the required encoder.
137 * Also set some basic encoder parameters.
138 * Some of these parameters are based on the input file's parameters.
139 * @param filename File to be opened
140 * @param input_codec_context Codec context of input file
141 * @param[out] output_format_context Format context of output file
142 * @param[out] output_codec_context Codec context of output file
143 * @return Error code (0 if successful)
144 */
149 {
155
156 /* Open the output file to write to it. */
159 fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
162 }
163
164 /* Create a new format context for the output container format. */
166 fprintf(stderr, "Could not allocate output format context\n");
168 }
169
170 /* Associate the output file (pointer) with the container format context. */
171 (*output_format_context)->pb = output_io_context;
172
173 /* Guess the desired container format based on the file extension. */
176 fprintf(stderr, "Could not find output file format\n");
178 }
179
180 if (!((*output_format_context)->url =
av_strdup(filename))) {
181 fprintf(stderr, "Could not allocate url.\n");
184 }
185
186 /* Find the encoder to be used by its name. */
188 fprintf(stderr, "Could not find an AAC encoder.\n");
190 }
191
192 /* Create a new audio stream in the output file container. */
194 fprintf(stderr, "Could not create new stream\n");
197 }
198
200 if (!avctx) {
201 fprintf(stderr, "Could not allocate an encoding context\n");
204 }
205
206 /* Set the basic encoder parameters.
207 * The input file's sample rate is used to avoid a sample rate conversion. */
212
213 /* Set the sample rate for the container. */
216
217 /* Some container formats (like MP4) require global headers to be present.
218 * Mark the encoder so that it behaves accordingly. */
221
222 /* Open the encoder for the audio stream to use it later. */
224 fprintf(stderr, "Could not open output codec (error '%s')\n",
227 }
228
231 fprintf(stderr, "Could not initialize stream parameters\n");
233 }
234
235 /* Save the encoder context for easier access later. */
236 *output_codec_context = avctx;
237
238 return 0;
239
244 *output_format_context =
NULL;
246 }
247
248 /**
249 * Initialize one data packet for reading or writing.
250 * @param[out] packet Packet to be initialized
251 * @return Error code (0 if successful)
252 */
254 {
256 fprintf(stderr, "Could not allocate packet\n");
258 }
259 return 0;
260 }
261
262 /**
263 * Initialize one audio frame for reading from the input file.
264 * @param[out] frame Frame to be initialized
265 * @return Error code (0 if successful)
266 */
268 {
270 fprintf(stderr, "Could not allocate input frame\n");
272 }
273 return 0;
274 }
275
276 /**
277 * Initialize the audio resampler based on the input and output codec settings.
278 * If the input and output sample formats differ, a conversion is required
279 * libswresample takes care of this, but requires initialization.
280 * @param input_codec_context Codec context of the input file
281 * @param output_codec_context Codec context of the output file
282 * @param[out] resample_context Resample context for the required conversion
283 * @return Error code (0 if successful)
284 */
288 {
290
291 /*
292 * Create a resampler context for the conversion.
293 * Set the conversion parameters.
294 */
304 fprintf(stderr, "Could not allocate resample context\n");
306 }
307 /*
308 * Perform a sanity check so that the number of converted samples is
309 * not greater than the number of samples to be converted.
310 * If the sample rates differ, this case has to be handled differently
311 */
313
314 /* Open the resampler with the specified parameters. */
316 fprintf(stderr, "Could not open resample context\n");
319 }
320 return 0;
321 }
322
323 /**
324 * Initialize a FIFO buffer for the audio samples to be encoded.
325 * @param[out] fifo Sample buffer
326 * @param output_codec_context Codec context of the output file
327 * @return Error code (0 if successful)
328 */
330 {
331 /* Create the FIFO buffer based on the specified output sample format. */
334 fprintf(stderr, "Could not allocate FIFO\n");
336 }
337 return 0;
338 }
339
340 /**
341 * Write the header of the output file container.
342 * @param output_format_context Format context of the output file
343 * @return Error code (0 if successful)
344 */
346 {
349 fprintf(stderr, "Could not write output file header (error '%s')\n",
352 }
353 return 0;
354 }
355
356 /**
357 * Decode one audio frame from the input file.
358 * @param frame Audio frame to be decoded
359 * @param input_format_context Format context of the input file
360 * @param input_codec_context Codec context of the input file
361 * @param[out] data_present Indicates whether data has been decoded
362 * @param[out] finished Indicates whether the end of file has
363 * been reached and all data has been
364 * decoded. If this flag is false, there
365 * is more data to be decoded, i.e., this
366 * function has to be called again.
367 * @return Error code (0 if successful)
368 */
372 int *data_present, int *finished)
373 {
374 /* Packet used for temporary storage. */
377
381
382 *data_present = 0;
383 *finished = 0;
384 /* Read one audio frame from the input file into a temporary packet. */
386 /* If we are at the end of the file, flush the decoder below. */
388 *finished = 1;
389 else {
390 fprintf(stderr, "Could not read frame (error '%s')\n",
393 }
394 }
395
396 /* Send the audio frame stored in the temporary packet to the decoder.
397 * The input audio stream decoder is used to do this. */
399 fprintf(stderr, "Could not send packet for decoding (error '%s')\n",
402 }
403
404 /* Receive one frame from the decoder. */
406 /* If the decoder asks for more data to be able to decode a frame,
407 * return indicating that no data is present. */
411 /* If the end of the input file is reached, stop decoding. */
413 *finished = 1;
416 }
else if (
error < 0) {
417 fprintf(stderr, "Could not decode frame (error '%s')\n",
420 /* Default case: Return decoded data. */
421 } else {
422 *data_present = 1;
424 }
425
429 }
430
431 /**
432 * Initialize a temporary storage for the specified number of audio samples.
433 * The conversion requires temporary storage due to the different format.
434 * The number of audio samples to be allocated is specified in frame_size.
435 * @param[out] converted_input_samples Array of converted samples. The
436 * dimensions are reference, channel
437 * (for multi-channel audio), sample.
438 * @param output_codec_context Codec context of the output file
439 * @param frame_size Number of samples to be converted in
440 * each round
441 * @return Error code (0 if successful)
442 */
446 {
448
449 /* Allocate as many pointers as there are audio channels.
450 * Each pointer will later point to the audio samples of the corresponding
451 * channels (although it may be NULL for interleaved formats).
452 */
454 sizeof(**converted_input_samples)))) {
455 fprintf(stderr, "Could not allocate converted input sample pointers\n");
457 }
458
459 /* Allocate memory for the samples of all channels in one consecutive
460 * block for convenience. */
465 fprintf(stderr,
466 "Could not allocate converted input samples (error '%s')\n",
468 av_freep(&(*converted_input_samples)[0]);
469 free(*converted_input_samples);
471 }
472 return 0;
473 }
474
475 /**
476 * Convert the input audio samples into the output sample format.
477 * The conversion happens on a per-frame basis, the size of which is
478 * specified by frame_size.
479 * @param input_data Samples to be decoded. The dimensions are
480 * channel (for multi-channel audio), sample.
481 * @param[out] converted_data Converted samples. The dimensions are channel
482 * (for multi-channel audio), sample.
483 * @param frame_size Number of samples to be converted
484 * @param resample_context Resample context for the conversion
485 * @return Error code (0 if successful)
486 */
488 uint8_t **converted_data,
const int frame_size,
490 {
492
493 /* Convert the samples using the resampler. */
497 fprintf(stderr, "Could not convert input samples (error '%s')\n",
500 }
501
502 return 0;
503 }
504
505 /**
506 * Add converted input audio samples to the FIFO buffer for later processing.
507 * @param fifo Buffer to add the samples to
508 * @param converted_input_samples Samples to be added. The dimensions are channel
509 * (for multi-channel audio), sample.
510 * @param frame_size Number of samples to be converted
511 * @return Error code (0 if successful)
512 */
514 uint8_t **converted_input_samples,
516 {
518
519 /* Make the FIFO as large as it needs to be to hold both,
520 * the old and the new samples. */
522 fprintf(stderr, "Could not reallocate FIFO\n");
524 }
525
526 /* Store the new samples in the FIFO buffer. */
529 fprintf(stderr, "Could not write data to FIFO\n");
531 }
532 return 0;
533 }
534
535 /**
536 * Read one audio frame from the input file, decode, convert and store
537 * it in the FIFO buffer.
538 * @param fifo Buffer used for temporary storage
539 * @param input_format_context Format context of the input file
540 * @param input_codec_context Codec context of the input file
541 * @param output_codec_context Codec context of the output file
542 * @param resampler_context Resample context for the conversion
543 * @param[out] finished Indicates whether the end of file has
544 * been reached and all data has been
545 * decoded. If this flag is false,
546 * there is more data to be decoded,
547 * i.e., this function has to be called
548 * again.
549 * @return Error code (0 if successful)
550 */
556 int *finished)
557 {
558 /* Temporary storage of the input samples of the frame read from the file. */
560 /* Temporary storage for the converted input samples. */
561 uint8_t **converted_input_samples =
NULL;
562 int data_present;
564
565 /* Initialize temporary storage for one input frame. */
568 /* Decode one frame worth of audio samples. */
570 input_codec_context, &data_present, finished))
572 /* If we are at the end of the file and there are no more samples
573 * in the decoder which are delayed, we are actually finished.
574 * This must not be treated as an error. */
575 if (*finished) {
578 }
579 /* If there is decoded data, convert and store it. */
580 if (data_present) {
581 /* Initialize the temporary storage for the converted input samples. */
585
586 /* Convert the input samples to the desired output sample format.
587 * This requires a temporary storage provided by converted_input_samples. */
591
592 /* Add the converted input samples to the FIFO buffer for later processing. */
597 }
599
601 if (converted_input_samples) {
602 av_freep(&converted_input_samples[0]);
603 free(converted_input_samples);
604 }
606
608 }
609
610 /**
611 * Initialize one input frame for writing to the output file.
612 * The frame will be exactly frame_size samples large.
613 * @param[out] frame Frame to be initialized
614 * @param output_codec_context Codec context of the output file
615 * @param frame_size Size of the frame
616 * @return Error code (0 if successful)
617 */
621 {
623
624 /* Create a new frame to store the audio samples. */
626 fprintf(stderr, "Could not allocate output frame\n");
628 }
629
630 /* Set the frame's parameters, especially its size and format.
631 * av_frame_get_buffer needs this to allocate memory for the
632 * audio samples of the frame.
633 * Default channel layouts based on the number of channels
634 * are assumed for simplicity. */
637 (*frame)->format = output_codec_context->
sample_fmt;
638 (*frame)->sample_rate = output_codec_context->
sample_rate;
639
640 /* Allocate the samples of the created frame. This call will make
641 * sure that the audio frame can hold as many samples as specified. */
643 fprintf(stderr, "Could not allocate output frame samples (error '%s')\n",
647 }
648
649 return 0;
650 }
651
652 /* Global timestamp for the audio frames. */
654
655 /**
656 * Encode one frame worth of audio to the output file.
657 * @param frame Samples to be encoded
658 * @param output_format_context Format context of the output file
659 * @param output_codec_context Codec context of the output file
660 * @param[out] data_present Indicates whether data has been
661 * encoded
662 * @return Error code (0 if successful)
663 */
667 int *data_present)
668 {
669 /* Packet used for temporary storage. */
672
676
677 /* Set a timestamp based on the sample rate for the container. */
681 }
682
683 *data_present = 0;
684 /* Send the audio frame stored in the temporary packet to the encoder.
685 * The output audio stream encoder is used to do this. */
687 /* Check for errors, but proceed with fetching encoded samples if the
688 * encoder signals that it has nothing more to encode. */
690 fprintf(stderr, "Could not send packet for encoding (error '%s')\n",
693 }
694
695 /* Receive one encoded frame from the encoder. */
697 /* If the encoder asks for more data to be able to provide an
698 * encoded frame, return indicating that no data is present. */
702 /* If the last frame has been encoded, stop encoding. */
706 }
else if (
error < 0) {
707 fprintf(stderr, "Could not encode frame (error '%s')\n",
710 /* Default case: Return encoded data. */
711 } else {
712 *data_present = 1;
713 }
714
715 /* Write one audio frame from the temporary packet to the output file. */
716 if (*data_present &&
718 fprintf(stderr, "Could not write frame (error '%s')\n",
721 }
722
726 }
727
728 /**
729 * Load one audio frame from the FIFO buffer, encode and write it to the
730 * output file.
731 * @param fifo Buffer used for temporary storage
732 * @param output_format_context Format context of the output file
733 * @param output_codec_context Codec context of the output file
734 * @return Error code (0 if successful)
735 */
739 {
740 /* Temporary storage of the output samples of the frame written to the file. */
742 /* Use the maximum number of possible samples per frame.
743 * If there is less than the maximum possible frame size in the FIFO
744 * buffer use this number. Otherwise, use the maximum possible frame size. */
747 int data_written;
748
749 /* Initialize temporary storage for one output frame. */
752
753 /* Read as many samples from the FIFO buffer as required to fill the frame.
754 * The samples are stored in the frame temporarily. */
756 fprintf(stderr, "Could not read data from FIFO\n");
759 }
760
761 /* Encode one frame worth of audio samples. */
763 output_codec_context, &data_written)) {
766 }
768 return 0;
769 }
770
771 /**
772 * Write the trailer of the output file container.
773 * @param output_format_context Format context of the output file
774 * @return Error code (0 if successful)
775 */
777 {
780 fprintf(stderr, "Could not write output file trailer (error '%s')\n",
783 }
784 return 0;
785 }
786
787 int main(
int argc,
char **argv)
788 {
794
795 if (argc != 3) {
796 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
797 exit(1);
798 }
799
800 /* Open the input file for reading. */
802 &input_codec_context))
804 /* Open the output file for writing. */
806 &output_format_context, &output_codec_context))
808 /* Initialize the resampler to be able to convert audio sample formats. */
810 &resample_context))
812 /* Initialize the FIFO buffer to store audio samples to be encoded. */
813 if (
init_fifo(&fifo, output_codec_context))
815 /* Write the header of the output file container. */
818
819 /* Loop as long as we have input samples to read or output samples
820 * to write; abort as soon as we have neither. */
821 while (1) {
822 /* Use the encoder's desired frame size for processing. */
823 const int output_frame_size = output_codec_context->frame_size;
824 int finished = 0;
825
826 /* Make sure that there is one frame worth of samples in the FIFO
827 * buffer so that the encoder can do its work.
828 * Since the decoder's and the encoder's frame size may differ, we
829 * need to FIFO buffer to store as many frames worth of input samples
830 * that they make up at least one frame worth of output samples. */
832 /* Decode one frame worth of audio samples, convert it to the
833 * output sample format and put it into the FIFO buffer. */
835 input_codec_context,
836 output_codec_context,
837 resample_context, &finished))
839
840 /* If we are at the end of the input file, we continue
841 * encoding the remaining audio samples to the output file. */
842 if (finished)
843 break;
844 }
845
846 /* If we have enough samples for the encoder, we encode them.
847 * At the end of the file, we pass the remaining samples to
848 * the encoder. */
851 /* Take one frame worth of audio samples from the FIFO buffer,
852 * encode it and write it to the output file. */
854 output_codec_context))
856
857 /* If we are at the end of the input file and have encoded
858 * all remaining samples, we can exit this loop and finish. */
859 if (finished) {
860 int data_written;
861 /* Flush the encoder as it may have delayed frames. */
862 do {
864 output_codec_context, &data_written))
866 } while (data_written);
867 break;
868 }
869 }
870
871 /* Write the trailer of the output file container. */
875
877 if (fifo)
880 if (output_codec_context)
882 if (output_format_context) {
885 }
886 if (input_codec_context)
888 if (input_format_context)
890
892 }