<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_1_1490572074077_11307">Hello, </div><div id="yui_3_16_0_1_1490572074077_11307"><br></div><div id="yui_3_16_0_1_1490572074077_11308">during my last job's project I had to use very often the AV library for many purposes. Then, I created many snippets of code which are aligned to the ffmpeg's 3.2 version: they don't use deprecated functions (no warnings from compiler) and can be useful as API usage examples, considering that the current state of the doc/examples directory seems not good and a bit messy. All the snippets that I wrote are short, and they cover many audio+video tasks, from grabbing from audio/video devices to network streaming. If the FFMPEG developers think that they can be pushed in the doc/examples directory, I can spend time in re-organizing all the material and send it progressively to the FFMPEG project. For now, I send an example which converts a raw audio file to float-planar and encodes it to adts-aac. Please, give me some feedback and I'll go on in contributing to the project by sending other examples.</div><div id="yui_3_16_0_1_1490572074077_11308"><br></div><div id="yui_3_16_0_1_1490572074077_11308"><br></div><div id="yui_3_16_0_1_1490572074077_11308"><br></div><div id="yui_3_16_0_1_1490572074077_13513">/*</div><div id="yui_3_16_0_1_1490572074077_13514"> * Copyright (c) 2017 Paolo Prete (p4olo_prete@yahoo.it)</div><div id="yui_3_16_0_1_1490572074077_13515"> *</div><div id="yui_3_16_0_1_1490572074077_13516"> * Permission is hereby granted, free of charge, to any person obtaining a copy</div><div id="yui_3_16_0_1_1490572074077_13517"> * of this software and associated documentation files (the "Software"), to deal</div><div id="yui_3_16_0_1_1490572074077_13518"> * in the Software without restriction, including without limitation the rights</div><div id="yui_3_16_0_1_1490572074077_13519"> * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</div><div id="yui_3_16_0_1_1490572074077_13520"> * copies of the Software, and to permit persons to whom the Software is</div><div id="yui_3_16_0_1_1490572074077_13521"> * furnished to do so, subject to the following conditions:</div><div id="yui_3_16_0_1_1490572074077_13522"> *</div><div id="yui_3_16_0_1_1490572074077_13523"> * The above copyright notice and this permission notice shall be included in</div><div id="yui_3_16_0_1_1490572074077_13524"> * all copies or substantial portions of the Software.</div><div id="yui_3_16_0_1_1490572074077_13525"> *</div><div id="yui_3_16_0_1_1490572074077_13526"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</div><div id="yui_3_16_0_1_1490572074077_13527"> * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</div><div id="yui_3_16_0_1_1490572074077_13528"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL</div><div id="yui_3_16_0_1_1490572074077_13529"> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</div><div id="yui_3_16_0_1_1490572074077_13530"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</div><div id="yui_3_16_0_1_1490572074077_13531"> * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</div><div id="yui_3_16_0_1_1490572074077_13532"> * THE SOFTWARE.</div><div id="yui_3_16_0_1_1490572074077_13533"> */</div><div id="yui_3_16_0_1_1490572074077_13534"><br id="yui_3_16_0_1_1490572074077_13535"></div><div id="yui_3_16_0_1_1490572074077_13536">/**</div><div id="yui_3_16_0_1_1490572074077_13537"> * @file</div><div id="yui_3_16_0_1_1490572074077_13538"> * API example for adts-aac encoding raw audio files. </div><div id="yui_3_16_0_1_1490572074077_13539"> * This example reads a raw audio input file, converts it to float-planar format, performs aac encoding and puts the encoded frames into an ADTS container. The encoded stream is written to </div><div id="yui_3_16_0_1_1490572074077_13540"> * a file named "out.aac"</div><div id="yui_3_16_0_1_1490572074077_13541"> * The raw input audio file can be created with: ffmpeg -i some_audio_file -f f32le -acodec pcm_f32le -ac 2 -ar 16000 raw_audio_file.raw</div><div id="yui_3_16_0_1_1490572074077_13542"> * </div><div id="yui_3_16_0_1_1490572074077_13543"> * @example encode_raw_audio_file_to_aac.c</div><div id="yui_3_16_0_1_1490572074077_13544"> */</div><div id="yui_3_16_0_1_1490572074077_13545"><br id="yui_3_16_0_1_1490572074077_13546"></div><div id="yui_3_16_0_1_1490572074077_13547">#include <libavcodec/avcodec.h></div><div id="yui_3_16_0_1_1490572074077_13548">#include <libavformat/avformat.h></div><div id="yui_3_16_0_1_1490572074077_13549">#include <libavutil/timestamp.h></div><div id="yui_3_16_0_1_1490572074077_13550">#include <libswresample/swresample.h></div><div id="yui_3_16_0_1_1490572074077_13551"><br id="yui_3_16_0_1_1490572074077_13552"></div><div id="yui_3_16_0_1_1490572074077_13553"><br id="yui_3_16_0_1_1490572074077_13554"></div><div id="yui_3_16_0_1_1490572074077_13555">#define ENCODER_BITRATE 64000</div><div id="yui_3_16_0_1_1490572074077_13556">#define SAMPLE_RATE 16000</div><div id="yui_3_16_0_1_1490572074077_13557">#define INPUT_SAMPLE_FMT AV_SAMPLE_FMT_FLT</div><div id="yui_3_16_0_1_1490572074077_13558">#define CHANNELS 2</div><div id="yui_3_16_0_1_1490572074077_13559"><br id="yui_3_16_0_1_1490572074077_13560"></div><div id="yui_3_16_0_1_1490572074077_13561"><br id="yui_3_16_0_1_1490572074077_13562"></div><div id="yui_3_16_0_1_1490572074077_13563">static char *const get_error_text(const int error)</div><div id="yui_3_16_0_1_1490572074077_13564">{</div><div id="yui_3_16_0_1_1490572074077_13565">    static char error_buffer[255];</div><div id="yui_3_16_0_1_1490572074077_13566">    av_strerror(error, error_buffer, sizeof(error_buffer));</div><div id="yui_3_16_0_1_1490572074077_13567">    return error_buffer;</div><div id="yui_3_16_0_1_1490572074077_13568">}</div><div id="yui_3_16_0_1_1490572074077_13569"><br id="yui_3_16_0_1_1490572074077_13570"></div><div id="yui_3_16_0_1_1490572074077_13571"><br id="yui_3_16_0_1_1490572074077_13572"></div><div id="yui_3_16_0_1_1490572074077_13573">static int write_adts_muxed_data (void *opaque, uint8_t *adts_data, int size)</div><div id="yui_3_16_0_1_1490572074077_13574">{</div><div id="yui_3_16_0_1_1490572074077_13575">    FILE *encoded_audio_file = (FILE *)opaque;</div><div id="yui_3_16_0_1_1490572074077_13576">    fwrite(adts_data, 1, size, encoded_audio_file); //(f)</div><div id="yui_3_16_0_1_1490572074077_13577">    return size;</div><div id="yui_3_16_0_1_1490572074077_13578">}</div><div id="yui_3_16_0_1_1490572074077_13579"><br id="yui_3_16_0_1_1490572074077_13580"></div><div id="yui_3_16_0_1_1490572074077_13581"><br id="yui_3_16_0_1_1490572074077_13582"></div><div id="yui_3_16_0_1_1490572074077_13583">int main(int argc, char **argv)</div><div id="yui_3_16_0_1_1490572074077_13584">{</div><div id="yui_3_16_0_1_1490572074077_13585">    </div><div id="yui_3_16_0_1_1490572074077_13586">    </div><div id="yui_3_16_0_1_1490572074077_13587">    if (argc != 2) {</div><div id="yui_3_16_0_1_1490572074077_13588">        av_log(NULL, AV_LOG_ERROR, "Usage: %s <raw audio input file (CHANNELS, INPUT_SAMPLE_FMT, SAMPLE_RATE)>\n", argv[0]);</div><div id="yui_3_16_0_1_1490572074077_13589">        return 1;</div><div id="yui_3_16_0_1_1490572074077_13590">    }    </div><div id="yui_3_16_0_1_1490572074077_13591">    </div><div id="yui_3_16_0_1_1490572074077_13592">    </div><div id="yui_3_16_0_1_1490572074077_13593">    int ret_val = 0;</div><div id="yui_3_16_0_1_1490572074077_13594">    int cleanup_step = 1;    </div><div id="yui_3_16_0_1_1490572074077_13595">    </div><div id="yui_3_16_0_1_1490572074077_13596">    </div><div id="yui_3_16_0_1_1490572074077_13597">    </div><div id="yui_3_16_0_1_1490572074077_13598">    FILE *input_audio_file = fopen(argv[1], "rb");</div><div id="yui_3_16_0_1_1490572074077_13599">    if(!input_audio_file){</div><div id="yui_3_16_0_1_1490572074077_13600">        av_log(NULL, AV_LOG_ERROR, "Could not open input audio file\n");</div><div id="yui_3_16_0_1_1490572074077_13601">        return AVERROR_EXIT;</div><div id="yui_3_16_0_1_1490572074077_13602">    }</div><div id="yui_3_16_0_1_1490572074077_13603">    </div><div id="yui_3_16_0_1_1490572074077_13604">    FILE *encoded_audio_file = fopen("out.aac", "wb");  </div><div id="yui_3_16_0_1_1490572074077_13605">    if(!encoded_audio_file){</div><div id="yui_3_16_0_1_1490572074077_13606">        av_log(NULL, AV_LOG_ERROR, "Could not open output audio file\n");</div><div id="yui_3_16_0_1_1490572074077_13607">        ret_val = AVERROR_EXIT;</div><div id="yui_3_16_0_1_1490572074077_13608">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13609">    }     </div><div id="yui_3_16_0_1_1490572074077_13610">    ++cleanup_step;    </div><div id="yui_3_16_0_1_1490572074077_13611"><br id="yui_3_16_0_1_1490572074077_13612"></div><div id="yui_3_16_0_1_1490572074077_13613">    </div><div id="yui_3_16_0_1_1490572074077_13614">    </div><div id="yui_3_16_0_1_1490572074077_13615">    av_register_all();</div><div id="yui_3_16_0_1_1490572074077_13616"><br id="yui_3_16_0_1_1490572074077_13617"></div><div id="yui_3_16_0_1_1490572074077_13618">    </div><div id="yui_3_16_0_1_1490572074077_13619">    </div><div id="yui_3_16_0_1_1490572074077_13620">    //</div><div id="yui_3_16_0_1_1490572074077_13621">    // Allocate the encoder's context and open the encoder</div><div id="yui_3_16_0_1_1490572074077_13622">    //</div><div id="yui_3_16_0_1_1490572074077_13623">    AVCodec *audio_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);</div><div id="yui_3_16_0_1_1490572074077_13624">    if(!audio_codec){</div><div id="yui_3_16_0_1_1490572074077_13625">        av_log(NULL, AV_LOG_ERROR, "Could not find aac codec\n");</div><div id="yui_3_16_0_1_1490572074077_13626">        ret_val = AVERROR_EXIT;</div><div id="yui_3_16_0_1_1490572074077_13627">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13628">    }</div><div id="yui_3_16_0_1_1490572074077_13629">    AVCodecContext *audio_encoder_ctx = avcodec_alloc_context3(audio_codec);</div><div id="yui_3_16_0_1_1490572074077_13630">    if(!audio_codec){</div><div id="yui_3_16_0_1_1490572074077_13631">        av_log(NULL, AV_LOG_ERROR, "Could not allocate the encoding context\n");</div><div id="yui_3_16_0_1_1490572074077_13632">        ret_val = AVERROR_EXIT;</div><div id="yui_3_16_0_1_1490572074077_13633">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13634">    }    </div><div id="yui_3_16_0_1_1490572074077_13635">    ++cleanup_step;</div><div id="yui_3_16_0_1_1490572074077_13636">    audio_encoder_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;</div><div id="yui_3_16_0_1_1490572074077_13637">    audio_encoder_ctx->bit_rate = ENCODER_BITRATE;</div><div id="yui_3_16_0_1_1490572074077_13638">    audio_encoder_ctx->sample_rate = SAMPLE_RATE; // You can use any other sample rate provided by the input file on condition that it is supported by the codec (use AVCodec::supported_samplerates for listing supported sample rates)</div><div id="yui_3_16_0_1_1490572074077_13639">    audio_encoder_ctx->channels = CHANNELS;</div><div id="yui_3_16_0_1_1490572074077_13640">    audio_encoder_ctx->channel_layout = av_get_default_channel_layout(CHANNELS);</div><div id="yui_3_16_0_1_1490572074077_13641">    audio_encoder_ctx->time_base = (AVRational){1, SAMPLE_RATE};</div><div id="yui_3_16_0_1_1490572074077_13642">    audio_encoder_ctx->codec_type = AVMEDIA_TYPE_AUDIO ;</div><div id="yui_3_16_0_1_1490572074077_13643">    if ((ret_val = avcodec_open2(audio_encoder_ctx, audio_codec, NULL)) < 0) {</div><div id="yui_3_16_0_1_1490572074077_13644">        av_log(NULL, AV_LOG_ERROR, "Could not open input codec (error '%s')\n", get_error_text(ret_val));</div><div id="yui_3_16_0_1_1490572074077_13645">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13646">    }</div><div id="yui_3_16_0_1_1490572074077_13647">    ++cleanup_step;</div><div id="yui_3_16_0_1_1490572074077_13648">    </div><div id="yui_3_16_0_1_1490572074077_13649">    </div><div id="yui_3_16_0_1_1490572074077_13650">    //</div><div id="yui_3_16_0_1_1490572074077_13651">    // Allocate an AVFrame which will be filled with the input file's data. </div><div id="yui_3_16_0_1_1490572074077_13652">    //</div><div id="yui_3_16_0_1_1490572074077_13653">    AVFrame *input_audio_frame;</div><div id="yui_3_16_0_1_1490572074077_13654">    if (!(input_audio_frame = av_frame_alloc())) {</div><div id="yui_3_16_0_1_1490572074077_13655">        av_log(NULL, AV_LOG_ERROR, "Could not allocate input frame\n");</div><div id="yui_3_16_0_1_1490572074077_13656">        ret_val = AVERROR(ENOMEM);</div><div id="yui_3_16_0_1_1490572074077_13657">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13658">    }    </div><div id="yui_3_16_0_1_1490572074077_13659">    input_audio_frame->nb_samples     = audio_encoder_ctx->frame_size;</div><div id="yui_3_16_0_1_1490572074077_13660">    input_audio_frame->format         = INPUT_SAMPLE_FMT;</div><div id="yui_3_16_0_1_1490572074077_13661">    input_audio_frame->channels       = CHANNELS;</div><div id="yui_3_16_0_1_1490572074077_13662">    input_audio_frame->sample_rate    = SAMPLE_RATE;</div><div id="yui_3_16_0_1_1490572074077_13663">    input_audio_frame->channel_layout = av_get_default_channel_layout(CHANNELS);</div><div id="yui_3_16_0_1_1490572074077_13664">    // Allocate the frame's data buffer </div><div id="yui_3_16_0_1_1490572074077_13665">    if ((ret_val = av_frame_get_buffer(input_audio_frame, 0)) < 0) {</div><div id="yui_3_16_0_1_1490572074077_13666">        av_log(NULL, AV_LOG_ERROR, "Could not allocate container for input frame samples (error '%s')\n", get_error_text(ret_val));</div><div id="yui_3_16_0_1_1490572074077_13667">        ret_val = AVERROR(ENOMEM);</div><div id="yui_3_16_0_1_1490572074077_13668">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13669">    }    </div><div id="yui_3_16_0_1_1490572074077_13670">    </div><div id="yui_3_16_0_1_1490572074077_13671">    </div><div id="yui_3_16_0_1_1490572074077_13672">    </div><div id="yui_3_16_0_1_1490572074077_13673">    //</div><div id="yui_3_16_0_1_1490572074077_13674">    // Input data must be converted to float-planar format, which is the format required by the AAC encoder. We allocate a SwrContext and an AVFrame (which will contain the converted samples)</div><div id="yui_3_16_0_1_1490572074077_13675">    // for this task. The AVFrame will feed the encoding function (avcodec_send_frame())</div><div id="yui_3_16_0_1_1490572074077_13676">    //</div><div id="yui_3_16_0_1_1490572074077_13677">    SwrContext *audio_convert_context = swr_alloc_set_opts(NULL, av_get_default_channel_layout(CHANNELS), AV_SAMPLE_FMT_FLTP, SAMPLE_RATE, av_get_default_channel_layout(CHANNELS), INPUT_SAMPLE_FMT, SAMPLE_RATE, 0, NULL);</div><div id="yui_3_16_0_1_1490572074077_13678">    if (!audio_convert_context) {</div><div id="yui_3_16_0_1_1490572074077_13679">        av_log(NULL, AV_LOG_ERROR, "Could not allocate resample context\n");                 </div><div id="yui_3_16_0_1_1490572074077_13680">        ret_val = AVERROR(ENOMEM);</div><div id="yui_3_16_0_1_1490572074077_13681">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13682">    }    </div><div id="yui_3_16_0_1_1490572074077_13683">    ++cleanup_step;</div><div id="yui_3_16_0_1_1490572074077_13684">    AVFrame *converted_audio_frame;</div><div id="yui_3_16_0_1_1490572074077_13685">    if (!(converted_audio_frame = av_frame_alloc())) {</div><div id="yui_3_16_0_1_1490572074077_13686">        av_log(NULL, AV_LOG_ERROR, "Could not allocate resampled frame\n");</div><div id="yui_3_16_0_1_1490572074077_13687">        ret_val = AVERROR(ENOMEM);</div><div id="yui_3_16_0_1_1490572074077_13688">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13689">    }     </div><div id="yui_3_16_0_1_1490572074077_13690">    ++cleanup_step;</div><div id="yui_3_16_0_1_1490572074077_13691">    converted_audio_frame->nb_samples     = audio_encoder_ctx->frame_size;</div><div id="yui_3_16_0_1_1490572074077_13692">    converted_audio_frame->format         = audio_encoder_ctx->sample_fmt;</div><div id="yui_3_16_0_1_1490572074077_13693">    converted_audio_frame->channels       = audio_encoder_ctx->channels;</div><div id="yui_3_16_0_1_1490572074077_13694">    converted_audio_frame->channel_layout = audio_encoder_ctx->channel_layout;</div><div id="yui_3_16_0_1_1490572074077_13695">    converted_audio_frame->sample_rate    = SAMPLE_RATE;     </div><div id="yui_3_16_0_1_1490572074077_13696">    if ((ret_val = av_frame_get_buffer(converted_audio_frame, 0)) < 0) {</div><div id="yui_3_16_0_1_1490572074077_13697">        av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for resampled frame samples (error '%s')\n", get_error_text(ret_val));</div><div id="yui_3_16_0_1_1490572074077_13698">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13699">    }    </div><div id="yui_3_16_0_1_1490572074077_13700">    </div><div id="yui_3_16_0_1_1490572074077_13701">    </div><div id="yui_3_16_0_1_1490572074077_13702">    </div><div id="yui_3_16_0_1_1490572074077_13703">    //</div><div id="yui_3_16_0_1_1490572074077_13704">    // Create the ADTS container for the encoded frames</div><div id="yui_3_16_0_1_1490572074077_13705">    //</div><div id="yui_3_16_0_1_1490572074077_13706">    AVOutputFormat *adts_container = av_guess_format("adts", NULL, NULL);</div><div id="yui_3_16_0_1_1490572074077_13707">    if (!adts_container) {</div><div id="yui_3_16_0_1_1490572074077_13708">        av_log(NULL, AV_LOG_ERROR, "Could not find adts output format\n");       </div><div id="yui_3_16_0_1_1490572074077_13709">        ret_val = AVERROR_EXIT;</div><div id="yui_3_16_0_1_1490572074077_13710">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13711">    }     </div><div id="yui_3_16_0_1_1490572074077_13712">    AVFormatContext *adts_container_ctx;</div><div id="yui_3_16_0_1_1490572074077_13713">    if ((ret_val = avformat_alloc_output_context2(&adts_container_ctx, adts_container, "", NULL)) < 0){</div><div id="yui_3_16_0_1_1490572074077_13714">        av_log(NULL, AV_LOG_ERROR, "Could not create output context (error '%s')\n", get_error_text(ret_val));</div><div id="yui_3_16_0_1_1490572074077_13715">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13716">    }</div><div id="yui_3_16_0_1_1490572074077_13717">    ++cleanup_step;</div><div id="yui_3_16_0_1_1490572074077_13718">    size_t adts_container_buffer_size = 4096;</div><div id="yui_3_16_0_1_1490572074077_13719">    uint8_t *adts_container_buffer;</div><div id="yui_3_16_0_1_1490572074077_13720">    if(!(adts_container_buffer = (uint8_t* )av_malloc(adts_container_buffer_size))){</div><div id="yui_3_16_0_1_1490572074077_13721">        av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for the I/O output context\n");       </div><div id="yui_3_16_0_1_1490572074077_13722">        ret_val = AVERROR(ENOMEM);</div><div id="yui_3_16_0_1_1490572074077_13723">        goto cleanup; </div><div id="yui_3_16_0_1_1490572074077_13724">    }</div><div id="yui_3_16_0_1_1490572074077_13725">    ++cleanup_step;</div><div id="yui_3_16_0_1_1490572074077_13726">    // Create an I/O context for the adts container with a write callback (write_adts_muxed_data()), so that muxed data will be accessed through this function.</div><div id="yui_3_16_0_1_1490572074077_13727">    AVIOContext *adts_avio_ctx;</div><div id="yui_3_16_0_1_1490572074077_13728">    if (!(adts_avio_ctx = avio_alloc_context(adts_container_buffer, adts_container_buffer_size, 1, encoded_audio_file, NULL , &write_adts_muxed_data, NULL))) {</div><div id="yui_3_16_0_1_1490572074077_13729">        av_log(NULL, AV_LOG_ERROR, "Could not create I/O output context\n");</div><div id="yui_3_16_0_1_1490572074077_13730">        ret_val = AVERROR_EXIT;</div><div id="yui_3_16_0_1_1490572074077_13731">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13732">    }</div><div id="yui_3_16_0_1_1490572074077_13733">    ++cleanup_step;</div><div id="yui_3_16_0_1_1490572074077_13734">    // Link the container's context to the previous I/O context</div><div id="yui_3_16_0_1_1490572074077_13735">    adts_container_ctx->pb = adts_avio_ctx;</div><div id="yui_3_16_0_1_1490572074077_13736">    AVStream *adts_stream;</div><div id="yui_3_16_0_1_1490572074077_13737">    if (!(adts_stream = avformat_new_stream(adts_container_ctx, NULL))) {</div><div id="yui_3_16_0_1_1490572074077_13738">        av_log(NULL, AV_LOG_ERROR, "Could not create new stream\n");       </div><div id="yui_3_16_0_1_1490572074077_13739">        ret_val = AVERROR(ENOMEM);</div><div id="yui_3_16_0_1_1490572074077_13740">        goto cleanup;        </div><div id="yui_3_16_0_1_1490572074077_13741">    }    </div><div id="yui_3_16_0_1_1490572074077_13742">    adts_stream->id = adts_container_ctx->nb_streams-1;</div><div id="yui_3_16_0_1_1490572074077_13743">    // Copy the encoder's parameters </div><div id="yui_3_16_0_1_1490572074077_13744">    avcodec_parameters_from_context(adts_stream->codecpar, audio_encoder_ctx);    </div><div id="yui_3_16_0_1_1490572074077_13745">    // Allocate the stream private data and write the stream header</div><div id="yui_3_16_0_1_1490572074077_13746">    if(avformat_write_header(adts_container_ctx, NULL) < 0){</div><div id="yui_3_16_0_1_1490572074077_13747">        av_log(NULL, AV_LOG_ERROR, "avformat_write_header() error\n");</div><div id="yui_3_16_0_1_1490572074077_13748">        ret_val = AVERROR_EXIT;</div><div id="yui_3_16_0_1_1490572074077_13749">        goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13750">    }        </div><div id="yui_3_16_0_1_1490572074077_13751">    ++cleanup_step;</div><div id="yui_3_16_0_1_1490572074077_13752">    </div><div id="yui_3_16_0_1_1490572074077_13753">    </div><div id="yui_3_16_0_1_1490572074077_13754">    </div><div id="yui_3_16_0_1_1490572074077_13755">    //</div><div id="yui_3_16_0_1_1490572074077_13756">    // Fill the input frame's data buffer with input file data (a), </div><div id="yui_3_16_0_1_1490572074077_13757">    // Convert the input frame to float-planar format (b), </div><div id="yui_3_16_0_1_1490572074077_13758">    // Send the converted frame to the encoder (c), </div><div id="yui_3_16_0_1_1490572074077_13759">    // Get the encoded packet (d),</div><div id="yui_3_16_0_1_1490572074077_13760">    // Send the encoded packet to the adts muxer (e). </div><div id="yui_3_16_0_1_1490572074077_13761">    // Muxed data is caught in write_adts_muxed_data() callback and it is written to the output audio file ( (f) : see above)</div><div id="yui_3_16_0_1_1490572074077_13762">    //</div><div id="yui_3_16_0_1_1490572074077_13763">    AVPacket encoded_audio_packet;</div><div id="yui_3_16_0_1_1490572074077_13764">    av_init_packet(&encoded_audio_packet);</div><div id="yui_3_16_0_1_1490572074077_13765">    int encoded_pkt_counter = 1;</div><div id="yui_3_16_0_1_1490572074077_13766">    while(1) {</div><div id="yui_3_16_0_1_1490572074077_13767">        int audio_bytes_to_encode = fread(input_audio_frame->data[0], 1, input_audio_frame->linesize[0], input_audio_file); //(a)</div><div id="yui_3_16_0_1_1490572074077_13768">        swr_convert_frame(audio_convert_context, converted_audio_frame, (const AVFrame *)input_audio_frame); //(b)</div><div id="yui_3_16_0_1_1490572074077_13769">        if(audio_bytes_to_encode != input_audio_frame->linesize[0]){            </div><div id="yui_3_16_0_1_1490572074077_13770">            break;</div><div id="yui_3_16_0_1_1490572074077_13771">        }</div><div id="yui_3_16_0_1_1490572074077_13772">        else {</div><div id="yui_3_16_0_1_1490572074077_13773">            // Do encode</div><div id="yui_3_16_0_1_1490572074077_13774">            ret_val = avcodec_send_frame(audio_encoder_ctx, converted_audio_frame);  //(c)</div><div id="yui_3_16_0_1_1490572074077_13775">            if(ret_val == 0) </div><div id="yui_3_16_0_1_1490572074077_13776">                ret_val = avcodec_receive_packet(audio_encoder_ctx, &encoded_audio_packet); //(d)</div><div id="yui_3_16_0_1_1490572074077_13777">            else{</div><div id="yui_3_16_0_1_1490572074077_13778">                av_log(NULL, AV_LOG_ERROR, "Error encoding frame (error '%s')\n", get_error_text(ret_val));</div><div id="yui_3_16_0_1_1490572074077_13779">                goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13780">            }</div><div id="yui_3_16_0_1_1490572074077_13781">            </div><div id="yui_3_16_0_1_1490572074077_13782">            if(ret_val == 0){                </div><div id="yui_3_16_0_1_1490572074077_13783">                int64_t pts = converted_audio_frame->nb_samples*(encoded_pkt_counter-1);</div><div id="yui_3_16_0_1_1490572074077_13784">                encoded_audio_packet.pts = encoded_audio_packet.dts = pts;           </div><div id="yui_3_16_0_1_1490572074077_13785">                if((ret_val == av_write_frame(adts_container_ctx, &encoded_audio_packet)) < 0){ //(e)</div><div id="yui_3_16_0_1_1490572074077_13786">                    av_log(NULL, AV_LOG_ERROR, "Error calling av_write_frame() (error '%s')\n", get_error_text(ret_val));</div><div id="yui_3_16_0_1_1490572074077_13787">                    goto cleanup;</div><div id="yui_3_16_0_1_1490572074077_13788">                }</div><div id="yui_3_16_0_1_1490572074077_13789">                else{</div><div id="yui_3_16_0_1_1490572074077_13790">                    av_log(NULL, AV_LOG_INFO, "Encoded AAC packet %d, size=%d, pts_time=%s\n", encoded_pkt_counter, encoded_audio_packet.size, av_ts2timestr(encoded_audio_packet.pts, &audio_encoder_ctx->time_base));</div><div id="yui_3_16_0_1_1490572074077_13791">                    ++encoded_pkt_counter;</div><div id="yui_3_16_0_1_1490572074077_13792">                }</div><div id="yui_3_16_0_1_1490572074077_13793">            }</div><div id="yui_3_16_0_1_1490572074077_13794">        }            </div><div id="yui_3_16_0_1_1490572074077_13795">    }</div><div id="yui_3_16_0_1_1490572074077_13796">    // Flush delayed packets</div><div id="yui_3_16_0_1_1490572074077_13797">    int still_pkts_to_flush = 1;</div><div id="yui_3_16_0_1_1490572074077_13798">    int delayed_pkt_counter = 1;    </div><div id="yui_3_16_0_1_1490572074077_13799">    while(still_pkts_to_flush){</div><div id="yui_3_16_0_1_1490572074077_13800">        int ret = avcodec_send_frame(audio_encoder_ctx, NULL);</div><div id="yui_3_16_0_1_1490572074077_13801">        if(ret != 0)</div><div id="yui_3_16_0_1_1490572074077_13802">            still_pkts_to_flush = 0;</div><div id="yui_3_16_0_1_1490572074077_13803">        ret = avcodec_receive_packet(audio_encoder_ctx, &encoded_audio_packet);</div><div id="yui_3_16_0_1_1490572074077_13804">        if(ret == 0){</div><div id="yui_3_16_0_1_1490572074077_13805">            int64_t pts = converted_audio_frame->nb_samples*(encoded_pkt_counter-1);</div><div id="yui_3_16_0_1_1490572074077_13806">            encoded_audio_packet.pts = encoded_audio_packet.dts = pts; </div><div id="yui_3_16_0_1_1490572074077_13807">            av_write_frame(adts_container_ctx, &encoded_audio_packet);</div><div id="yui_3_16_0_1_1490572074077_13808">            av_log(NULL, AV_LOG_INFO, "Flushed encoded AAC delayed packet %d, size=%d, pts_time=%s\n", delayed_pkt_counter, encoded_audio_packet.size, av_ts2timestr(encoded_audio_packet.pts, &audio_encoder_ctx->time_base));</div><div id="yui_3_16_0_1_1490572074077_13809">            ++delayed_pkt_counter;</div><div id="yui_3_16_0_1_1490572074077_13810">            ++encoded_pkt_counter;</div><div id="yui_3_16_0_1_1490572074077_13811">        }        </div><div id="yui_3_16_0_1_1490572074077_13812">    }</div><div id="yui_3_16_0_1_1490572074077_13813"><br id="yui_3_16_0_1_1490572074077_13814"></div><div id="yui_3_16_0_1_1490572074077_13815">    </div><div id="yui_3_16_0_1_1490572074077_13816">    av_write_trailer(adts_container_ctx);  </div><div id="yui_3_16_0_1_1490572074077_13817"><br id="yui_3_16_0_1_1490572074077_13818"></div><div id="yui_3_16_0_1_1490572074077_13819">    </div><div id="yui_3_16_0_1_1490572074077_13820">    </div><div id="yui_3_16_0_1_1490572074077_13821">    </div><div id="yui_3_16_0_1_1490572074077_13822">cleanup:    </div><div id="yui_3_16_0_1_1490572074077_13823"><br id="yui_3_16_0_1_1490572074077_13824"></div><div id="yui_3_16_0_1_1490572074077_13825"><br id="yui_3_16_0_1_1490572074077_13826"></div><div id="yui_3_16_0_1_1490572074077_13827">    if(cleanup_step > 0)</div><div id="yui_3_16_0_1_1490572074077_13828">        fclose(input_audio_file);</div><div id="yui_3_16_0_1_1490572074077_13829">    if(cleanup_step > 1)</div><div id="yui_3_16_0_1_1490572074077_13830">        fclose(encoded_audio_file); </div><div id="yui_3_16_0_1_1490572074077_13831">    if(cleanup_step > 2)    </div><div id="yui_3_16_0_1_1490572074077_13832">        avcodec_free_context(&audio_encoder_ctx);</div><div id="yui_3_16_0_1_1490572074077_13833">    if(cleanup_step > 3)     </div><div id="yui_3_16_0_1_1490572074077_13834">        av_frame_free(&input_audio_frame);</div><div id="yui_3_16_0_1_1490572074077_13835">    if(cleanup_step > 4)     </div><div id="yui_3_16_0_1_1490572074077_13836">        swr_free(&audio_convert_context);   </div><div id="yui_3_16_0_1_1490572074077_13837">    if(cleanup_step > 5)     </div><div id="yui_3_16_0_1_1490572074077_13838">        av_frame_free(&converted_audio_frame);</div><div id="yui_3_16_0_1_1490572074077_13839">    if(cleanup_step > 6)    </div><div id="yui_3_16_0_1_1490572074077_13840">        avformat_free_context(adts_container_ctx);</div><div id="yui_3_16_0_1_1490572074077_13841">    if(cleanup_step > 7)    </div><div id="yui_3_16_0_1_1490572074077_13842">        av_free(adts_container_buffer);</div><div id="yui_3_16_0_1_1490572074077_13843">    if(cleanup_step > 8)    </div><div id="yui_3_16_0_1_1490572074077_13844">        av_free(adts_avio_ctx);  </div><div id="yui_3_16_0_1_1490572074077_13845">    if(cleanup_step > 9)    </div><div id="yui_3_16_0_1_1490572074077_13846">        av_packet_unref(&encoded_audio_packet);    </div><div id="yui_3_16_0_1_1490572074077_13847">    </div><div id="yui_3_16_0_1_1490572074077_13848">    </div><div id="yui_3_16_0_1_1490572074077_13849">    return ret_val;</div><div id="yui_3_16_0_1_1490572074077_13850">    </div><div id="yui_3_16_0_1_1490572074077_13851">}</div><div id="yui_3_16_0_1_1490572074077_13852"><br id="yui_3_16_0_1_1490572074077_13853"></div><div id="yui_3_16_0_1_1490572074077_13854"><br id="yui_3_16_0_1_1490572074077_13855"></div><div dir="ltr" id="yui_3_16_0_1_1490572074077_13856"><br id="yui_3_16_0_1_1490572074077_13857"></div><div dir="ltr" id="yui_3_16_0_1_1490572074077_11309"><br id="yui_3_16_0_1_1490572074077_11310"></div><div dir="ltr" id="yui_3_16_0_1_1490572074077_11309"><br></div><div dir="ltr" id="yui_3_16_0_1_1490572074077_11309"><br></div><div dir="ltr" id="yui_3_16_0_1_1490572074077_11309"><br></div><div dir="ltr" id="yui_3_16_0_1_1490572074077_9362"><br></div><div dir="ltr" id="yui_3_16_0_1_1490572074077_9362"><br></div><div dir="ltr" id="yui_3_16_0_1_1490572074077_9362"><br></div><div dir="ltr" id="yui_3_16_0_1_1490572074077_9362"><br></div></div></body></html>