FFMPEG more samples than frame size (avcodec_encode_audio2) 的解决方案

时间:2022-11-30 17:34:49

在实际的项目中,从音频设备采集到的音频的类型和编码器类型(aac ,amr)通常是不一致的。

那么我们首先需要做重采样的过程。利用swr_convert 重新采样。

这时候我们可能会遇到另外一个问题。就是在encode_audio的时候遇到

more samples than frame size (avcodec_encode_audio2)  的问题。

问题的原因在于 我们编码器的frame_size 比采集到的fram->nb_samples小

  1. /* check for valid frame size */
  2. if (frame) {
  3. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  4. if (frame->nb_samples > avctx->frame_size) {
  5. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  6. return AVERROR(EINVAL);
  7. }
  8. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  9. if (frame->nb_samples < avctx->frame_size &&
  10. !avctx->internal->last_audio_frame) {
  11. ret = pad_last_frame(avctx, &padded_frame, frame);
  12. if (ret < 0)
  13. return ret;
  14. frame = padded_frame;
  15. avctx->internal->last_audio_frame = 1;
  16. }
  17. if (frame->nb_samples != avctx->frame_size) {
  18. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  19. ret = AVERROR(EINVAL);
  20. goto end;
  21. }
  22. }
  23. }

解决的方案是:每次采集到的frame数据保存到一个AUDIO FIFO中,每次等凑齐刚好frame_size大小的数据(请注意这里是刚好,网上有人说拆分AVframe,本人在ffmpeg2.1用这个办法没办法解决),接着把这些数据一口气encode。

如果有剩余的数据就存储到下一次encode。可以使用audio_fifo_来操作数据

http://www.ffmpeg.org/doxygen/1.0/audio__fifo_8c-source.html

http://blog.csdn.net/zsc09_leaf/article/details/16858193

参考transcode_aac.c即可