[Libav-user] Setting audio stream parameters for PCM

Seungyong Kwon sykwon66 at gmail.com
Wed Nov 9 13:09:06 EET 2016


Hi,

I'm writing a RTSP stream capturing application using FFmpeg and other libraries.
I managed to successfully capture video and AAC audio and now am trying to add
PCM audio support. However during testing I found it was not going as I intended.

Following is part of my code which sets stream and codec parameters.

   AVFormatContext *av_ctx = (AVFormatContext *)ctx;
   AVOutputFormat  *ofmt = (AVOutputFormat *)av_ctx->oformat;
   AVStream        *st;
   AVCodecContext  *c_ctx;

   if ((st = avformat_new_stream(av_ctx, NULL)) == NULL)
     return NULL;

   st->id = 1;
   st->time_base.den = sampleRate;
   st->time_base.num = 1;

   c_ctx = st->codec;
   c_ctx->codec_id = ofmt->audio_codec;
   c_ctx->codec_type = AVMEDIA_TYPE_AUDIO;
   c_ctx->sample_fmt = AV_SAMPLE_FMT_S16;
   c_ctx->block_align = 0;                 /* Must be 0 to fill skipped frames */
   c_ctx->sample_rate = sampleRate;
   c_ctx->time_base = st->time_base;

   if (c_ctx->codec_id == AV_CODEC_ID_AAC) {
     int idx         = getSampleRateIndex(sampleRate);

     c_ctx->frame_size = 1024;
     c_ctx->channels = channels;
     c_ctx->extradata_size = 2;
     c_ctx->extradata = (uint8_t *)av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE);
     c_ctx->extradata[0] = (fmtp->fProfile + 1) << 3 | (idx >> 1);
     c_ctx->extradata[1] = (idx << 7) | (channels << 3);
   } else {
     c_ctx->bit_rate = 64000;        /* 8000 samples * 8 bit * 1 channel */
     c_ctx->frame_size = 0;          /* automatically set */
     c_ctx->channels = 1;
     c_ctx->channel_layout = AV_CH_LAYOUT_MONO;
   }

Above code works fine for AAC audio. However in case of PCM audio the output
AVI file looked very strange ie. total audio duration is way too long compared
to actual audio frames stored (3 times more?). It also showed incorrect
interleave duration from MediaInfo.

Can somebody give some help what is missing in my code?

Thanks in advance.

SY Kwon








More information about the Libav-user mailing list