avcodec_encode_audio2返回-22

错误原因

  • 该函数的主要功能:根据音频编码器音频frame数据编码成音频packt数据。
  • 返回-22主要原因:参数错误,具体如下
    1. 音频编码器的参数(声道数,采样率,采样格式,位宽,采样数)和frame的参数不一致。
    2. 编码格式对应的采样数和frame的采样数不一致。
  • 只要声道数,采样率,采样格式,位宽,采样数,任何一个参数不一致,都需要进行重采样
    1. AAC对应的采样数(nb_samples)和frame大小(frame_size)都是1024.
    2. 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个数据时,不需要输入数据。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值