在翻閱了無數的文章後,發現其原因是在於x264內部的參數檢測,歸要到底還是由於自己在ffmpeg中設定的參數不正確導致的。以下是x264針對此判斷代碼如下:
相關判斷位於encoder/encoder.c 393行:
/* Detect default ffmpeg settings and terminate with an error. */ { int score = 0; score += h->param.analyse.i_me_range == 0; score += h->param.rc.i_qp_step == 3; score += h->param.i_keyint_max == 12; score += h->param.rc.i_qp_min == 2; score += h->param.rc.i_qp_max == 31; score += h->param.rc.f_qcompress == 0.5; score += fabs(h->param.rc.f_ip_factor - 1.25) < 0.01; score += fabs(h->param.rc.f_pb_factor - 1.25) < 0.01; score += h->param.analyse.inter == 0 && h->param.analyse.i_subpel_refine == 8; if( score >= 5 ) { x264_log( h, X264_LOG_ERROR, "broken ffmpeg default settings detected\n" ); x264_log( h, X264_LOG_ERROR, "use an encoding preset (vpre)\n" ); return -1; } }
應用X264進行H.264編碼,如果編碼不能成功,大部分情況是編碼參數選擇不正確
使用ffmpeg x264進行編碼的時候,avcodec_open報錯:
[libx264 @ 00021bb0]broken ffmpeg default settings detected
[libx264 @ 00021bb0]use an encoding preset (vpre)
解決方案:在 x264 的source file encoder/encoder.c 中找到該報錯的地方/* Detect default ffmpeg settings and terminate with an error. */ { int score = 0; score += h->param.analyse.i_me_range == 0; score += h->param.rc.i_qp_step == 3; score += h->param.i_keyint_max == 12; score += h->param.rc.i_qp_min == 2; score += h->param.rc.i_qp_max == 31; score += h->param.rc.f_qcompress == 0.5; score += fabs(h->param.rc.f_ip_factor - 1.25) < 0.01; score += fabs(h->param.rc.f_pb_factor - 1.25) < 0.01; score += h->param.analyse.inter == 0 && h->param.analyse.i_subpel_refine == 8; if( score >= 5 ) { x264_log( h, X264_LOG_ERROR, "broken ffmpeg default settings detected\n" ); x264_log( h, X264_LOG_ERROR, "use an encoding preset (vpre)\n" ); return -1; } }We can see that if score >= 5,the function to open the codec will fail.We must at least set 4 param of the AVCodecContext before open it.在avcodec_open函數之間增加如下幾個AVCodecContext 的初始化:/*default settings for x264*/ ctx->me_range = 16; ctx->max_qdiff = 4; ctx->qmin = 10; ctx->qmax = 51; ctx->qcompress = 0.6;
OK,解決。