You must set some additional settings for AVCodecContext
before you call avcodec_open2
.
I usually set these required settings (variables beginning with 'k' mean predefined constatns):
avCtx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
avCtx->codec_type = AVMEDIA_TYPE_AUDIO;
avCtx->channels = kChannelsCount; // for example, 2
avCtx->sample_fmt = kSampleFmt; // AV_SAMPLE_FMT_S16
avCtx->sample_rate = kSampleRate; // 44100
avCtx->channel_layout = kSampleLayout; // 3
const AVRational timeBase = {1, avCtx->sample_rate};
avCtx->time_base = timeBase;
UPD
I'm sorry, I wrote the parameters that must be set for audio encoding. For audio decoding usually sufficient to set avCtx->channels
, ctx->sample_rate
or set avCtx->extrdata
and avCtx->extradata_size
.