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
35
37
44
46
47 /* The output bit rate in bit/s */
48 #define OUTPUT_BIT_RATE 96000
49 /* The number of output channels */
50 #define OUTPUT_CHANNELS 2
51
52 /**
53 * Open an input file and the required decoder.
54 * @param filename File to be opened
55 * @param[out] input_format_context Format context of opened file
56 * @param[out] input_codec_context Codec context of opened file
57 * @return Error code (0 if successful)
58 */
62 {
67
68 /* Open the input file to read from it. */
71 fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
73 *input_format_context =
NULL;
75 }
76
77 /* Get information on the input file (number of streams etc.). */
79 fprintf(stderr, "Could not open find stream info (error '%s')\n",
83 }
84
85 /* Make sure that there is only one stream in the input file. */
86 if ((*input_format_context)->nb_streams != 1) {
87 fprintf(stderr, "Expected one audio input stream, but found %d\n",
88 (*input_format_context)->nb_streams);
91 }
92
93 stream = (*input_format_context)->streams[0];
94
95 /* Find a decoder for the audio stream. */
97 fprintf(stderr, "Could not find input codec\n");
100 }
101
102 /* Allocate a new decoding context. */
104 if (!avctx) {
105 fprintf(stderr, "Could not allocate a decoding context\n");
108 }
109
110 /* Initialize the stream parameters with demuxer information. */
116 }
117
118 /* Open the decoder for the audio stream to use it later. */
120 fprintf(stderr, "Could not open input codec (error '%s')\n",
125 }
126
127 /* Set the packet timebase for the decoder. */
129
130 /* Save the decoder context for easier access later. */
131 *input_codec_context = avctx;
132
133 return 0;
134 }
135
136 /**
137 * Open an output file and the required encoder.
138 * Also set some basic encoder parameters.
139 * Some of these parameters are based on the input file's parameters.
140 * @param filename File to be opened
141 * @param input_codec_context Codec context of input file
142 * @param[out] output_format_context Format context of output file
143 * @param[out] output_codec_context Codec context of output file
144 * @return Error code (0 if successful)
145 */
150 {
156
157 /* Open the output file to write to it. */
160 fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
163 }
164
165 /* Create a new format context for the output container format. */
167 fprintf(stderr, "Could not allocate output format context\n");
169 }
170
171 /* Associate the output file (pointer) with the container format context. */
172 (*output_format_context)->pb = output_io_context;
173
174 /* Guess the desired container format based on the file extension. */
177 fprintf(stderr, "Could not find output file format\n");
179 }
180
181 if (!((*output_format_context)->url =
av_strdup(filename))) {
182 fprintf(stderr, "Could not allocate url.\n");
185 }
186
187 /* Find the encoder to be used by its name. */
189 fprintf(stderr, "Could not find an AAC encoder.\n");
191 }
192
193 /* Create a new audio stream in the output file container. */
195 fprintf(stderr, "Could not create new stream\n");
198 }
199
201 if (!avctx) {
202 fprintf(stderr, "Could not allocate an encoding context\n");
205 }
206
207 /* Set the basic encoder parameters.
208 * The input file's sample rate is used to avoid a sample rate conversion. */
213
214 /* Set the sample rate for the container. */
217
218 /* Some container formats (like MP4) require global headers to be present.
219 * Mark the encoder so that it behaves accordingly. */
222
223 /* Open the encoder for the audio stream to use it later. */
225 fprintf(stderr, "Could not open output codec (error '%s')\n",
228 }
229
232 fprintf(stderr, "Could not initialize stream parameters\n");
234 }
235
236 /* Save the encoder context for easier access later. */
237 *output_codec_context = avctx;
238
239 return 0;
240
245 *output_format_context =
NULL;
247 }
248
249 /**
250 * Initialize one data packet for reading or writing.
251 * @param[out] packet Packet to be initialized
252 * @return Error code (0 if successful)
253 */
255 {
257 fprintf(stderr, "Could not allocate packet\n");
259 }
260 return 0;
261 }
262
263 /**
264 * Initialize one audio frame for reading from the input file.
265 * @param[out] frame Frame to be initialized
266 * @return Error code (0 if successful)
267 */
269 {
271 fprintf(stderr, "Could not allocate input frame\n");
273 }
274 return 0;
275 }
276
277 /**
278 * Initialize the audio resampler based on the input and output codec settings.
279 * If the input and output sample formats differ, a conversion is required
280 * libswresample takes care of this, but requires initialization.
281 * @param input_codec_context Codec context of the input file
282 * @param output_codec_context Codec context of the output file
283 * @param[out] resample_context Resample context for the required conversion
284 * @return Error code (0 if successful)
285 */
289 {
291
292 /*
293 * Create a resampler context for the conversion.
294 * Set the conversion parameters.
295 */
305 fprintf(stderr, "Could not allocate resample context\n");
307 }
308 /*
309 * Perform a sanity check so that the number of converted samples is
310 * not greater than the number of samples to be converted.
311 * If the sample rates differ, this case has to be handled differently
312 */
314
315 /* Open the resampler with the specified parameters. */
317 fprintf(stderr, "Could not open resample context\n");
320 }
321 return 0;
322 }
323
324 /**
325 * Initialize a FIFO buffer for the audio samples to be encoded.
326 * @param[out] fifo Sample buffer
327 * @param output_codec_context Codec context of the output file
328 * @return Error code (0 if successful)
329 */
331 {
332 /* Create the FIFO buffer based on the specified output sample format. */
335 fprintf(stderr, "Could not allocate FIFO\n");
337 }
338 return 0;
339 }
340
341 /**
342 * Write the header of the output file container.
343 * @param output_format_context Format context of the output file
344 * @return Error code (0 if successful)
345 */
347 {
350 fprintf(stderr, "Could not write output file header (error '%s')\n",
353 }
354 return 0;
355 }
356
357 /**
358 * Decode one audio frame from the input file.
359 * @param frame Audio frame to be decoded
360 * @param input_format_context Format context of the input file
361 * @param input_codec_context Codec context of the input file
362 * @param[out] data_present Indicates whether data has been decoded
363 * @param[out] finished Indicates whether the end of file has
364 * been reached and all data has been
365 * decoded. If this flag is false, there
366 * is more data to be decoded, i.e., this
367 * function has to be called again.
368 * @return Error code (0 if successful)
369 */
373 int *data_present, int *finished)
374 {
375 /* Packet used for temporary storage. */
378
382
383 *data_present = 0;
384 *finished = 0;
385 /* Read one audio frame from the input file into a temporary packet. */
387 /* If we are at the end of the file, flush the decoder below. */
389 *finished = 1;
390 else {
391 fprintf(stderr, "Could not read frame (error '%s')\n",
394 }
395 }
396
397 /* Send the audio frame stored in the temporary packet to the decoder.
398 * The input audio stream decoder is used to do this. */
400 fprintf(stderr, "Could not send packet for decoding (error '%s')\n",
403 }
404
405 /* Receive one frame from the decoder. */
407 /* If the decoder asks for more data to be able to decode a frame,
408 * return indicating that no data is present. */
412 /* If the end of the input file is reached, stop decoding. */
414 *finished = 1;
417 }
else if (
error < 0) {
418 fprintf(stderr, "Could not decode frame (error '%s')\n",
421 /* Default case: Return decoded data. */
422 } else {
423 *data_present = 1;
425 }
426
430 }
431
432 /**
433 * Initialize a temporary storage for the specified number of audio samples.
434 * The conversion requires temporary storage due to the different format.
435 * The number of audio samples to be allocated is specified in frame_size.
436 * @param[out] converted_input_samples Array of converted samples. The
437 * dimensions are reference, channel
438 * (for multi-channel audio), sample.
439 * @param output_codec_context Codec context of the output file
440 * @param frame_size Number of samples to be converted in
441 * each round
442 * @return Error code (0 if successful)
443 */
447 {
449
450 /* Allocate as many pointers as there are audio channels.
451 * Each pointer will point to the audio samples of the corresponding
452 * channels (although it may be NULL for interleaved formats).
453 * Allocate memory for the samples of all channels in one consecutive
454 * block for convenience. */
459 fprintf(stderr,
460 "Could not allocate converted input samples (error '%s')\n",
463 }
464 return 0;
465 }
466
467 /**
468 * Convert the input audio samples into the output sample format.
469 * The conversion happens on a per-frame basis, the size of which is
470 * specified by frame_size.
471 * @param input_data Samples to be decoded. The dimensions are
472 * channel (for multi-channel audio), sample.
473 * @param[out] converted_data Converted samples. The dimensions are channel
474 * (for multi-channel audio), sample.
475 * @param frame_size Number of samples to be converted
476 * @param resample_context Resample context for the conversion
477 * @return Error code (0 if successful)
478 */
480 uint8_t **converted_data,
const int frame_size,
482 {
484
485 /* Convert the samples using the resampler. */
489 fprintf(stderr, "Could not convert input samples (error '%s')\n",
492 }
493
494 return 0;
495 }
496
497 /**
498 * Add converted input audio samples to the FIFO buffer for later processing.
499 * @param fifo Buffer to add the samples to
500 * @param converted_input_samples Samples to be added. The dimensions are channel
501 * (for multi-channel audio), sample.
502 * @param frame_size Number of samples to be converted
503 * @return Error code (0 if successful)
504 */
506 uint8_t **converted_input_samples,
508 {
510
511 /* Make the FIFO as large as it needs to be to hold both,
512 * the old and the new samples. */
514 fprintf(stderr, "Could not reallocate FIFO\n");
516 }
517
518 /* Store the new samples in the FIFO buffer. */
521 fprintf(stderr, "Could not write data to FIFO\n");
523 }
524 return 0;
525 }
526
527 /**
528 * Read one audio frame from the input file, decode, convert and store
529 * it in the FIFO buffer.
530 * @param fifo Buffer used for temporary storage
531 * @param input_format_context Format context of the input file
532 * @param input_codec_context Codec context of the input file
533 * @param output_codec_context Codec context of the output file
534 * @param resampler_context Resample context for the conversion
535 * @param[out] finished Indicates whether the end of file has
536 * been reached and all data has been
537 * decoded. If this flag is false,
538 * there is more data to be decoded,
539 * i.e., this function has to be called
540 * again.
541 * @return Error code (0 if successful)
542 */
548 int *finished)
549 {
550 /* Temporary storage of the input samples of the frame read from the file. */
552 /* Temporary storage for the converted input samples. */
553 uint8_t **converted_input_samples =
NULL;
554 int data_present;
556
557 /* Initialize temporary storage for one input frame. */
560 /* Decode one frame worth of audio samples. */
562 input_codec_context, &data_present, finished))
564 /* If we are at the end of the file and there are no more samples
565 * in the decoder which are delayed, we are actually finished.
566 * This must not be treated as an error. */
567 if (*finished) {
570 }
571 /* If there is decoded data, convert and store it. */
572 if (data_present) {
573 /* Initialize the temporary storage for the converted input samples. */
577
578 /* Convert the input samples to the desired output sample format.
579 * This requires a temporary storage provided by converted_input_samples. */
583
584 /* Add the converted input samples to the FIFO buffer for later processing. */
589 }
591
593 if (converted_input_samples)
594 av_freep(&converted_input_samples[0]);
597
599 }
600
601 /**
602 * Initialize one input frame for writing to the output file.
603 * The frame will be exactly frame_size samples large.
604 * @param[out] frame Frame to be initialized
605 * @param output_codec_context Codec context of the output file
606 * @param frame_size Size of the frame
607 * @return Error code (0 if successful)
608 */
612 {
614
615 /* Create a new frame to store the audio samples. */
617 fprintf(stderr, "Could not allocate output frame\n");
619 }
620
621 /* Set the frame's parameters, especially its size and format.
622 * av_frame_get_buffer needs this to allocate memory for the
623 * audio samples of the frame.
624 * Default channel layouts based on the number of channels
625 * are assumed for simplicity. */
628 (*frame)->format = output_codec_context->
sample_fmt;
629 (*frame)->sample_rate = output_codec_context->
sample_rate;
630
631 /* Allocate the samples of the created frame. This call will make
632 * sure that the audio frame can hold as many samples as specified. */
634 fprintf(stderr, "Could not allocate output frame samples (error '%s')\n",
638 }
639
640 return 0;
641 }
642
643 /* Global timestamp for the audio frames. */
645
646 /**
647 * Encode one frame worth of audio to the output file.
648 * @param frame Samples to be encoded
649 * @param output_format_context Format context of the output file
650 * @param output_codec_context Codec context of the output file
651 * @param[out] data_present Indicates whether data has been
652 * encoded
653 * @return Error code (0 if successful)
654 */
658 int *data_present)
659 {
660 /* Packet used for temporary storage. */
663
667
668 /* Set a timestamp based on the sample rate for the container. */
672 }
673
674 *data_present = 0;
675 /* Send the audio frame stored in the temporary packet to the encoder.
676 * The output audio stream encoder is used to do this. */
678 /* Check for errors, but proceed with fetching encoded samples if the
679 * encoder signals that it has nothing more to encode. */
681 fprintf(stderr, "Could not send packet for encoding (error '%s')\n",
684 }
685
686 /* Receive one encoded frame from the encoder. */
688 /* If the encoder asks for more data to be able to provide an
689 * encoded frame, return indicating that no data is present. */
693 /* If the last frame has been encoded, stop encoding. */
697 }
else if (
error < 0) {
698 fprintf(stderr, "Could not encode frame (error '%s')\n",
701 /* Default case: Return encoded data. */
702 } else {
703 *data_present = 1;
704 }
705
706 /* Write one audio frame from the temporary packet to the output file. */
707 if (*data_present &&
709 fprintf(stderr, "Could not write frame (error '%s')\n",
712 }
713
717 }
718
719 /**
720 * Load one audio frame from the FIFO buffer, encode and write it to the
721 * output file.
722 * @param fifo Buffer used for temporary storage
723 * @param output_format_context Format context of the output file
724 * @param output_codec_context Codec context of the output file
725 * @return Error code (0 if successful)
726 */
730 {
731 /* Temporary storage of the output samples of the frame written to the file. */
733 /* Use the maximum number of possible samples per frame.
734 * If there is less than the maximum possible frame size in the FIFO
735 * buffer use this number. Otherwise, use the maximum possible frame size. */
738 int data_written;
739
740 /* Initialize temporary storage for one output frame. */
743
744 /* Read as many samples from the FIFO buffer as required to fill the frame.
745 * The samples are stored in the frame temporarily. */
747 fprintf(stderr, "Could not read data from FIFO\n");
750 }
751
752 /* Encode one frame worth of audio samples. */
754 output_codec_context, &data_written)) {
757 }
759 return 0;
760 }
761
762 /**
763 * Write the trailer of the output file container.
764 * @param output_format_context Format context of the output file
765 * @return Error code (0 if successful)
766 */
768 {
771 fprintf(stderr, "Could not write output file trailer (error '%s')\n",
774 }
775 return 0;
776 }
777
778 int main(
int argc,
char **argv)
779 {
785
786 if (argc != 3) {
787 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
788 exit(1);
789 }
790
791 /* Open the input file for reading. */
793 &input_codec_context))
795 /* Open the output file for writing. */
797 &output_format_context, &output_codec_context))
799 /* Initialize the resampler to be able to convert audio sample formats. */
801 &resample_context))
803 /* Initialize the FIFO buffer to store audio samples to be encoded. */
804 if (
init_fifo(&fifo, output_codec_context))
806 /* Write the header of the output file container. */
809
810 /* Loop as long as we have input samples to read or output samples
811 * to write; abort as soon as we have neither. */
812 while (1) {
813 /* Use the encoder's desired frame size for processing. */
814 const int output_frame_size = output_codec_context->frame_size;
815 int finished = 0;
816
817 /* Make sure that there is one frame worth of samples in the FIFO
818 * buffer so that the encoder can do its work.
819 * Since the decoder's and the encoder's frame size may differ, we
820 * need to FIFO buffer to store as many frames worth of input samples
821 * that they make up at least one frame worth of output samples. */
823 /* Decode one frame worth of audio samples, convert it to the
824 * output sample format and put it into the FIFO buffer. */
826 input_codec_context,
827 output_codec_context,
828 resample_context, &finished))
830
831 /* If we are at the end of the input file, we continue
832 * encoding the remaining audio samples to the output file. */
833 if (finished)
834 break;
835 }
836
837 /* If we have enough samples for the encoder, we encode them.
838 * At the end of the file, we pass the remaining samples to
839 * the encoder. */
842 /* Take one frame worth of audio samples from the FIFO buffer,
843 * encode it and write it to the output file. */
845 output_codec_context))
847
848 /* If we are at the end of the input file and have encoded
849 * all remaining samples, we can exit this loop and finish. */
850 if (finished) {
851 int data_written;
852 /* Flush the encoder as it may have delayed frames. */
853 do {
855 output_codec_context, &data_written))
857 } while (data_written);
858 break;
859 }
860 }
861
862 /* Write the trailer of the output file container. */
866
868 if (fifo)
871 if (output_codec_context)
873 if (output_format_context) {
876 }
877 if (input_codec_context)
879 if (input_format_context)
881
883 }