错误原因
- 该函数的主要功能:根据音频编码器和音频frame数据编码成音频packt数据。
- 返回-22主要原因:参数错误,具体如下
- 音频编码器的参数(声道数,采样率,采样格式,位宽,采样数)和frame的参数不一致。
- 编码格式对应的采样数和frame的采样数不一致。
- 只要声道数,采样率,采样格式,位宽,采样数,任何一个参数不一致,都需要进行重采样。
- AAC对应的采样数(nb_samples)和frame大小(frame_size)都是1024.
- AAC对应的采样数(nb_samples)和frame大小(frame_size)都是1152.
错误案例
我的frame采样数是2048,直接编码aac格式会失败,采用重采样的方式,把一个frame拆分成两个采样数1024的frame,不需要依赖缓冲。
// 2014个重采样为1024个
int count = swr_convert(swr, outs, AAC_ENCODE_SIZE, (const uint8_t **)pFrameOut->extended_data, pFrameOut->nb_samples);
if (count < 0){
LOGD("swr_convert error 1 ret = %d, %s", count, av_err2str(count));
} else {
LOGD("count = %d, outs[0] = %s, outs[1] = %s", count, NULL, NULL/*outs[0], outs[1]*/);
pFrameOut->nb_samples = count;//aac 为1024
pFrameOut->data[0] = (uint8_t*)outs[0];
pFrameOut->data[1] = (uint8_t*)outs[1];
}
// 进行编码
encode:
ret = avcodec_encode_audio2(pOutFormatCtx->streams[audioIndex]->codec, &enc_pkt, pFrameOut, &enc_got_frame);
****
//重采样剩余的1024个采样,不需要输入数据
count = swr_convert(swr, outs, AAC_ENCODE_SIZE, NULL, 0);
LOGD("cache count = %d, outs[0] = %s, outs[1] = %s", count, NULL, NULL/*outs[0], outs[1]*/);
if (count > 0){
pFrameOut->nb_samples = count;
pFrameOut->data[0] = (uint8_t*)outs[0];
pFrameOut->data[1] = (uint8_t*)outs[1];
}
这种情况下需要注意的是,第二次重采样1024个数据时,不需要输入数据。