今日在进行音频转码wav时, 程序运行后生成带有MP3后缀的文件,但是文件为仅带MP3文件头的空文件,查看代码后发现 ret = avcodec_send_packet(dec_acodec, &dec_packet); 发生返回-22的错误, FFMPEG的-22错误代码一般代表为配置有问题, 遂查看FFMPEG错误日志输出,然后发现如标题的错误Inconsistent channel configuration. 不一致的频道配置?
并且发现dec_acodec->channel_layout 此时值为1,dec_acode->channels值为2. 即布局为单声道,但是声道数为双声道, 难怪会配置不一致。
channel_layout_map[]
{ "mono", 1, AV_CH_LAYOUT_MONO },
{ "stereo", 2, AV_CH_LAYOUT_STEREO },
{ "2.1", 3, AV_CH_LAYOUT_2POINT1 },
{ "3.0", 3, AV_CH_LAYOUT_SURROUND },
{ "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
{ "4.0", 4, AV_CH_LAYOUT_4POINT0 },
{ "quad", 4, AV_CH_LAYOUT_QUAD },
{ "quad(side)", 4, AV_CH_LAYOUT_2_2 },
{ "3.1", 4, AV_CH_LAYOUT_3POINT1 },
{ "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
{ "5.0(side)", 5, AV_CH_LAYOUT_5POINT0 },
{ "4.1", 5, AV_CH_LAYOUT_4POINT1 },
{ "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
{ "5.1(side)", 6, AV_CH_LAYOUT_5POINT1 },
{ "6.0", 6, AV_CH_LAYOUT_6POINT0 },
{ "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
{ "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
{ "6.1", 7, AV_CH_LAYOUT_6POINT1 },
{ "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
{ "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
{ "7.0", 7, AV_CH_LAYOUT_7POINT0 },
{ "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
{ "7.1", 8, AV_CH_LAYOUT_7POINT1 },
{ "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
{ "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
{ "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
问题既然发现了, 寻找问题的原因自然也得安排上, dec_acodec很明显是音频的解码结构体, 那自然要去找打开文件的相关代码(avformat_open_input), 我发现PCM格式的音频在打开音频解码器前, dec_ctx->channel_layout竟然没有值, 但是可以得到音频的声道数, 因此可以通过音频的声道数来获取布局, 至此问题解决。