---

To be squashed into the parent patch.

 doc/examples/mp3_aac.c |  155 ++++++++++++++++++++++++++++++------------------
 1 file changed, 98 insertions(+), 57 deletions(-)

diff --git a/doc/examples/mp3_aac.c b/doc/examples/mp3_aac.c
index e7131ca..511ab4d 100644
--- a/doc/examples/mp3_aac.c
+++ b/doc/examples/mp3_aac.c
@@ -67,7 +67,8 @@ static int open_input_file(const char *filename,
     int error;
 
     /** Open the input file to read from it. */
-    if ((error = avformat_open_input(input_format_context, filename, NULL, 
NULL)) < 0) {
+    if ((error = avformat_open_input(input_format_context, filename, NULL,
+                                     NULL)) < 0) {
         fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
                 filename, get_error_text(error));
         *input_format_context = NULL;
@@ -98,8 +99,10 @@ static int open_input_file(const char *filename,
     }
 
     /** Open the decoder for the audio stream to use it later. */
-    if ((error = avcodec_open2((*input_format_context)->streams[0]->codec, 
input_codec, NULL)) < 0) {
-        fprintf(stderr, "Could not open input codec (error '%s')\n", 
get_error_text(error));
+    if ((error = avcodec_open2((*input_format_context)->streams[0]->codec,
+                               input_codec, NULL)) < 0) {
+        fprintf(stderr, "Could not open input codec (error '%s')\n",
+                get_error_text(error));
         avformat_close_input(input_format_context);
         return error;
     }
@@ -126,7 +129,8 @@ static int open_output_file(const char *filename,
     int error;
 
     /** Open the output file to write to it. */
-    if ((error = avio_open(&output_io_context, filename, AVIO_FLAG_WRITE)) < 
0) {
+    if ((error = avio_open(&output_io_context, filename,
+                           AVIO_FLAG_WRITE)) < 0) {
         fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
                 filename, get_error_text(error));
         return error;
@@ -142,14 +146,16 @@ static int open_output_file(const char *filename,
     (*output_format_context)->pb = output_io_context;
 
     /** Guess the desired container format based on the file extension. */
-    if (!((*output_format_context)->oformat = av_guess_format(NULL, filename, 
NULL))) {
+    if (!((*output_format_context)->oformat = av_guess_format(NULL, filename,
+                                                              NULL))) {
         fprintf(stderr, "Could not find output file format\n");
         goto cleanup;
     }
 
     /** Find the encoder to be used by its name. */
     if (!(output_codec = avcodec_find_encoder_by_name(OUTPUT_CODEC_NAME))) {
-        fprintf(stderr, "Could not find output codec '%s'\n", 
OUTPUT_CODEC_NAME);
+        fprintf(stderr, "Could not find output codec '%s'\n",
+                OUTPUT_CODEC_NAME);
         goto cleanup;
     }
 
@@ -181,11 +187,13 @@ static int open_output_file(const char *filename,
 
     /** Open the encoder for the audio stream to use it later. */
     if ((error = avcodec_open2(*output_codec_context, output_codec, NULL)) < 
0) {
-        fprintf(stderr, "Could not open output codec (error '%s')\n", 
get_error_text(error));
+        fprintf(stderr, "Could not open output codec (error '%s')\n",
+                get_error_text(error));
         goto cleanup;
     }
 
     return 0;
+
 cleanup:
     avio_close((*output_format_context)->pb);
     avformat_free_context(*output_format_context);
@@ -242,17 +250,17 @@ static int init_resampler(AVCodecContext 
*input_codec_context,
          * properly by the demuxer and/or decoder).
          */
         av_opt_set_int(*resample_context, "in_channel_layout",
-            av_get_default_channel_layout(input_codec_context->channels), 0);
+                       
av_get_default_channel_layout(input_codec_context->channels), 0);
         av_opt_set_int(*resample_context, "out_channel_layout",
-            av_get_default_channel_layout(output_codec_context->channels), 0);
+                       
av_get_default_channel_layout(output_codec_context->channels), 0);
         av_opt_set_int(*resample_context, "in_sample_rate",
-            input_codec_context->sample_rate, 0);
+                       input_codec_context->sample_rate, 0);
         av_opt_set_int(*resample_context, "out_sample_rate",
-            output_codec_context->sample_rate, 0);
+                       output_codec_context->sample_rate, 0);
         av_opt_set_int(*resample_context, "in_sample_fmt",
-            input_codec_context->sample_fmt, 0);
+                       input_codec_context->sample_fmt, 0);
         av_opt_set_int(*resample_context, "out_sample_fmt",
-            output_codec_context->sample_fmt, 0);
+                       output_codec_context->sample_fmt, 0);
 
         /** Open the resampler with the specified parameters. */
         if ((error = avresample_open(*resample_context)) < 0) {
@@ -280,7 +288,8 @@ static int write_output_file_header(AVFormatContext 
*output_format_context)
 {
     int error;
     if ((error = avformat_write_header(output_format_context, NULL)) < 0) {
-        fprintf(stderr, "Could not write output file header (error '%s')\n", 
get_error_text(error));
+        fprintf(stderr, "Could not write output file header (error '%s')\n",
+                get_error_text(error));
         return error;
     }
     return 0;
@@ -303,7 +312,8 @@ static int decode_audio_frame(AVFrame *frame,
         if (error == AVERROR_EOF)
             *finished = 1;
         else {
-            fprintf(stderr, "Could not read frame (error '%s')\n", 
get_error_text(error));
+            fprintf(stderr, "Could not read frame (error '%s')\n",
+                    get_error_text(error));
             return error;
         }
     }
@@ -314,16 +324,18 @@ static int decode_audio_frame(AVFrame *frame,
      * If we are at the end of the file, pass an empty packet to the decoder
      * to flush it.
      */
-    if ((error = avcodec_decode_audio4(input_codec_context, frame, 
data_present, &input_packet)) < 0) {
-        fprintf(stderr, "Could not decode frame (error '%s')\n", 
get_error_text(error));
+    if ((error = avcodec_decode_audio4(input_codec_context, frame,
+                                       data_present, &input_packet)) < 0) {
+        fprintf(stderr, "Could not decode frame (error '%s')\n",
+                get_error_text(error));
         av_free_packet(&input_packet);
         return error;
     }
 
     /**
-      * If the decoder has not been flushed completely, we are not finished,
-      * so that this function has to be called again.
-      */
+     * If the decoder has not been flushed completely, we are not finished,
+     * so that this function has to be called again.
+     */
     if (*finished && *data_present)
         *finished = 0;
     av_free_packet(&input_packet);
@@ -346,7 +358,8 @@ static int init_converted_samples(uint8_t 
***converted_input_samples,
      * Each pointer will later point to the audio samples of the corresponding
      * channels (although it may be NULL for interleaved formats).
      */
-    if (!(*converted_input_samples = calloc(output_codec_context->channels, 
sizeof(**converted_input_samples)))) {
+    if (!(*converted_input_samples = calloc(output_codec_context->channels,
+                                            
sizeof(**converted_input_samples)))) {
         fprintf(stderr, "Could not allocate converted input sample 
pointers\n");
         return AVERROR(ENOMEM);
     }
@@ -355,9 +368,13 @@ static int init_converted_samples(uint8_t 
***converted_input_samples,
      * Allocate memory for the samples of all channels in one consecutive
      * block for convenience.
      */
-    if ((error = av_samples_alloc(*converted_input_samples, NULL, 
output_codec_context->channels,
-                                  frame_size, 
output_codec_context->sample_fmt, 0)) < 0) {
-        fprintf(stderr, "Could not allocate converted input samples (error 
'%s')\n", get_error_text(error));
+    if ((error = av_samples_alloc(*converted_input_samples, NULL,
+                                  output_codec_context->channels,
+                                  frame_size,
+                                  output_codec_context->sample_fmt, 0)) < 0) {
+        fprintf(stderr,
+                "Could not allocate converted input samples (error '%s')\n",
+                get_error_text(error));
         av_freep(&converted_input_samples[0]);
         free(converted_input_samples);
         return error;
@@ -414,7 +431,8 @@ static int add_samples_to_fifo(AVAudioFifo *fifo,
     }
 
     /** Store the new samples in the FIFO buffer. */
-    if (av_audio_fifo_write(fifo, (void **)converted_input_samples, 
frame_size) < frame_size) {
+    if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
+                            frame_size) < frame_size) {
         fprintf(stderr, "Could not write data to FIFO\n");
         return AVERROR_EXIT;
     }
@@ -425,10 +443,12 @@ static int add_samples_to_fifo(AVAudioFifo *fifo,
  * Read one audio frame from the input file, decodes, converts and stores
  * it in the FIFO buffer.
  */
-static int read_decode_convert_and_store(AVAudioFifo *fifo, AVFormatContext 
*input_format_context,
+static int read_decode_convert_and_store(AVAudioFifo *fifo,
+                                         AVFormatContext *input_format_context,
                                          AVCodecContext *input_codec_context,
                                          AVCodecContext *output_codec_context,
-                                         AVAudioResampleContext 
*resampler_context, int *finished)
+                                         AVAudioResampleContext 
*resampler_context,
+                                         int *finished)
 {
     /** Temporary storage of the input samples of the frame read from the 
file. */
     AVFrame *input_frame = NULL;
@@ -441,13 +461,14 @@ static int read_decode_convert_and_store(AVAudioFifo 
*fifo, AVFormatContext *inp
     if (init_input_frame(&input_frame))
         goto cleanup;
     /** Decode one frame worth of audio samples. */
-    if (decode_audio_frame(input_frame, input_format_context, 
input_codec_context, &data_present, finished))
+    if (decode_audio_frame(input_frame, input_format_context,
+                           input_codec_context, &data_present, finished))
         goto cleanup;
     /**
-      * If we are at the end of the file and there are no more samples
-      * in the decoder which are delayed, we are actually finished.
-      * This must not be treated as an error.
-      */
+     * If we are at the end of the file and there are no more samples
+     * in the decoder which are delayed, we are actually finished.
+     * This must not be treated as an error.
+     */
     if (*finished && !data_present) {
         ret = 0;
         goto cleanup;
@@ -468,7 +489,8 @@ static int read_decode_convert_and_store(AVAudioFifo *fifo, 
AVFormatContext *inp
             goto cleanup;
 
         /** Add the converted input samples to the FIFO buffer for later 
processing. */
-        if (add_samples_to_fifo(fifo, converted_input_samples, 
input_frame->nb_samples))
+        if (add_samples_to_fifo(fifo, converted_input_samples,
+                                input_frame->nb_samples))
             goto cleanup;
         ret = 0;
     }
@@ -507,9 +529,9 @@ static int init_output_frame(AVFrame **frame,
      * Default channel layouts based on the number of channels
      * are assumed for simplicity.
      */
-    (*frame)->nb_samples       = frame_size;
-    (*frame)->channel_layout   = 
av_get_default_channel_layout(output_codec_context->channels);
-    (*frame)->format           = output_codec_context->sample_fmt;
+    (*frame)->nb_samples     = frame_size;
+    (*frame)->channel_layout = 
av_get_default_channel_layout(output_codec_context->channels);
+    (*frame)->format         = output_codec_context->sample_fmt;
 
     /**
      * Allocate the samples of the created frame. This call will make
@@ -526,8 +548,10 @@ static int init_output_frame(AVFrame **frame,
 }
 
 /** Encode one frame worth of audio to the output file. */
-static int encode_audio_frame(AVFrame *frame, AVFormatContext 
*output_format_context,
-                              AVCodecContext *output_codec_context, int 
*data_present)
+static int encode_audio_frame(AVFrame *frame,
+                              AVFormatContext *output_format_context,
+                              AVCodecContext *output_codec_context,
+                              int *data_present)
 {
     /** Packet used for temporary storage. */
     AVPacket output_packet;
@@ -538,8 +562,10 @@ static int encode_audio_frame(AVFrame *frame, 
AVFormatContext *output_format_con
      * Encode the audio frame and store it in the temporary packet.
      * The output audio stream encoder is used to do this.
      */
-    if ((error = avcodec_encode_audio2(output_codec_context, &output_packet, 
frame, data_present)) < 0) {
-        fprintf(stderr, "Could not encode frame (error '%s')\n", 
get_error_text(error));
+    if ((error = avcodec_encode_audio2(output_codec_context, &output_packet,
+                                       frame, data_present)) < 0) {
+        fprintf(stderr, "Could not encode frame (error '%s')\n",
+                get_error_text(error));
         av_free_packet(&output_packet);
         return error;
     }
@@ -547,7 +573,8 @@ static int encode_audio_frame(AVFrame *frame, 
AVFormatContext *output_format_con
     /** Write one audio frame from the temporary packet to the output file. */
     if (*data_present) {
         if ((error = av_write_frame(output_format_context, &output_packet)) < 
0) {
-            fprintf(stderr, "Could not write frame (error '%s')\n", 
get_error_text(error));
+            fprintf(stderr, "Could not write frame (error '%s')\n",
+                    get_error_text(error));
             av_free_packet(&output_packet);
             return error;
         }
@@ -562,7 +589,8 @@ static int encode_audio_frame(AVFrame *frame, 
AVFormatContext *output_format_con
  * Load one audio frame from the FIFO buffer, encode and write it to the
  * output file.
  */
-static int load_encode_and_write(AVAudioFifo *fifo, AVFormatContext 
*output_format_context,
+static int load_encode_and_write(AVAudioFifo *fifo,
+                                 AVFormatContext *output_format_context,
                                  AVCodecContext *output_codec_context)
 {
     /** Temporary storage of the output samples of the frame written to the 
file. */
@@ -572,7 +600,8 @@ static int load_encode_and_write(AVAudioFifo *fifo, 
AVFormatContext *output_form
      * If there is less than the maximum possible frame size in the FIFO
      * buffer use this number. Otherwise, use the maximum possible frame size
      */
-    const int frame_size = FFMIN(av_audio_fifo_size(fifo), 
output_codec_context->frame_size);
+    const int frame_size = FFMIN(av_audio_fifo_size(fifo),
+                                 output_codec_context->frame_size);
     int data_written;
 
     /** Initialize temporary storage for one output frame. */
@@ -590,7 +619,8 @@ static int load_encode_and_write(AVAudioFifo *fifo, 
AVFormatContext *output_form
     }
 
     /** Encode one frame worth of audio samples. */
-    if (encode_audio_frame(output_frame, output_format_context, 
output_codec_context, &data_written)) {
+    if (encode_audio_frame(output_frame, output_format_context,
+                           output_codec_context, &data_written)) {
         av_frame_free(&output_frame);
         return AVERROR_EXIT;
     }
@@ -603,7 +633,8 @@ static int write_output_file_trailer(AVFormatContext 
*output_format_context)
 {
     int error;
     if ((error = av_write_trailer(output_format_context)) < 0) {
-        fprintf(stderr, "Could not write output file trailer (error '%s')\n", 
get_error_text(error));
+        fprintf(stderr, "Could not write output file trailer (error '%s')\n",
+                get_error_text(error));
         return error;
     }
     return 0;
@@ -612,22 +643,25 @@ static int write_output_file_trailer(AVFormatContext 
*output_format_context)
 /** Convert an MP3 to an AAC file in an MP4 container. */
 int main(void)
 {
-    AVFormatContext *input_format_context    = NULL, *output_format_context = 
NULL;
-    AVCodecContext *input_codec_context      = NULL, *output_codec_context = 
NULL;
+    AVFormatContext *input_format_context = NULL, *output_format_context = 
NULL;
+    AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
     AVAudioResampleContext *resample_context = NULL;
-    AVAudioFifo *fifo                        = NULL;
-    int ret                                  = AVERROR_EXIT;
+    AVAudioFifo *fifo = NULL;
+    int ret = AVERROR_EXIT;
 
     /** Register all codecs and formats so that they can be used. */
     av_register_all();
     /** Open the input file for reading. */
-    if (open_input_file(INPUT_FILENAME, &input_format_context, 
&input_codec_context))
+    if (open_input_file(INPUT_FILENAME, &input_format_context,
+                        &input_codec_context))
         goto cleanup;
     /** Open the output file for writing. */
-    if (open_output_file(OUTPUT_FILENAME, input_codec_context, 
&output_format_context, &output_codec_context))
+    if (open_output_file(OUTPUT_FILENAME, input_codec_context,
+                         &output_format_context, &output_codec_context))
         goto cleanup;
     /** Initialize the resampler to be able to convert audio sample formats. */
-    if (init_resampler(input_codec_context, output_codec_context, 
&resample_context))
+    if (init_resampler(input_codec_context, output_codec_context,
+                       &resample_context))
         goto cleanup;
     /** Initialize the FIFO buffer to store audio samples to be encoded. */
     if (init_fifo(&fifo))
@@ -657,7 +691,10 @@ int main(void)
              * Decode one frame worth of audio samples, convert it to the
              * output sample format and put it into the FIFO buffer.
              */
-            if (read_decode_convert_and_store(fifo, input_format_context, 
input_codec_context, output_codec_context, resample_context, &finished))
+            if (read_decode_convert_and_store(fifo, input_format_context,
+                                              input_codec_context,
+                                              output_codec_context,
+                                              resample_context, &finished))
                 goto cleanup;
 
             /**
@@ -673,12 +710,14 @@ int main(void)
          * At the end of the file, we pass the remaining samples to
          * the encoder.
          */
-        while (av_audio_fifo_size(fifo) >= output_frame_size || (finished && 
av_audio_fifo_size(fifo) > 0))
+        while (av_audio_fifo_size(fifo) >= output_frame_size ||
+               (finished && av_audio_fifo_size(fifo) > 0))
             /**
              * Take one frame worth of audio samples from the FIFO buffer,
              * encode it and write it to the output file.
              */
-            if (load_encode_and_write(fifo, output_format_context, 
output_codec_context))
+            if (load_encode_and_write(fifo, output_format_context,
+                                      output_codec_context))
                 goto cleanup;
 
         /**
@@ -689,8 +728,9 @@ int main(void)
             int data_written;
             /** Flush the encoder as it may have delayed frames. */
             do {
-               if (encode_audio_frame(NULL, output_format_context, 
output_codec_context, &data_written))
-                   goto cleanup;
+                if (encode_audio_frame(NULL, output_format_context,
+                                       output_codec_context, &data_written))
+                    goto cleanup;
             } while (data_written);
             break;
         }
@@ -718,5 +758,6 @@ cleanup:
         avcodec_close(input_codec_context);
     if (input_format_context)
         avformat_close_input(&input_format_context);
+
     return ret;
 }
-- 
1.7.9.5

_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to