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


在實際的項目中,從音頻設備采集到的音頻的類型和編碼器類型(aac ,amr)通常是不一致的。

那么我們首先需要做重采樣的過程。利用swr_convert 重新采樣。

 

這時候我們可能會遇到另外一個問題。就是在encode_audio的時候遇到

more samples than frame size (avcodec_encode_audio2)  的問題。

 

問題的原因在於 我們編碼器的frame_size 比采集到的fram->nb_samples小

 

[cpp]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  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.   
  15.                 frame = padded_frame;  
  16.                 avctx->internal->last_audio_frame = 1;  
  17.             }  
  18.   
  19.             if (frame->nb_samples != avctx->frame_size) {  
  20.                 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);  
  21.                 ret = AVERROR(EINVAL);  
  22.                 goto end;  
  23.             }  
  24.         }  
  25.     }  


解決的方案是:每次采集到的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即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM