I keep having a strange issue lately. Depending on how I set up my audio configuration in windows ( stereo/quad/5.1 ), a ffmpeg call to avcodec_open2() fails with error -22 or just works. Not being able to find much about that error, I thought I should ask about it here. The main flow goes like this:
c = st->codec;
avformat_alloc_output_context2(&oc, NULL, NULL, "video.mpeg");
oc->fmt->audio_codec = AV_CODEC_ID_MP2;
AVDictionary* dict = NULL;
ret = av_dict_set(&dict, "ac", "2", 0);
c->request_channels = 2;
ret = avcodec_open2(c, codec, &dict); //HERE IT FAILS WITH -22 if speaker configuration is not stereo
The codec context 'c' is set up like this in a stream:
st = avformat_new_stream(oc, *codec);
c = st->codec;
c->channels = 2;
c->channel_layout = AV_CH_LAYOUT_STEREO;
c->sample_fmt = AV_SAMPLE_FMT_S16;
c->codec_id = codec_id;
Most of it is copied from their one of the muxing examples found in the documentation. Everything works as expected if in windows I have set the output to stereo.
If I set my speaker configuration to 5.1 ( 6 channels ), avcodec_open2 fails with error -22.
So I have a hard time understanding what am I doing wrong. Normally it should not be any relationship between my speaker configuration and the result of avcodec_open2.
Are there some other parameters that I need to set ?
av_log_set_level(AV_LOG_VERBOSE);to understand the error more detailed. thenvoid my_log_callback(void *ptr, int level, const char *fmt, va_list vargs) { printf ("\n%s",fmt);}thenav_log_set_callback(my_log_callback);-22is really error22because any possible number means how much bytes it should return so.. error 22 is#define EINVAL 22 /* Invalid argument */which means the argument is invalid.AV_CH_LAYOUT_5POINT1instead ofAV_CH_LAYOUT_STEREOfor 6 channels I guess.