1. 程式人生 > >X264編碼---基本引數設定

X264編碼---基本引數設定

x264編碼有3個重要的相關引數,preset-tune-profile

引數設定相關函式:

x264_param_t* pParam = (x264_param_t*)malloc(sizeof(x264_param_t));

x264_param_default(pParam);   //給引數結構體pParam 賦預設值
x264_param_default_preset(pParam, "fast" , "zerolatency" );  //設定preset和tune

x264_param_apply_profile(pParam, "baseline");  //設定profile

preset是編碼速度

可選項"ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow", "placebo",從最快到最慢。無特殊要求選fast即可。

tune是編碼質量和畫面細節相關的引數。

可選項"film"電影, "animation"動畫, "grain"顆粒, "stillimage"靜態影象, "psnr"PSNR測試, "ssim"SSIM測試, "fastdecode"快速解碼, "zerolatency"零延遲這幾種。

profile是約束條件

從多到少排的"baseline", "main", "high", "high10", "high422", "high444",下面是從其他地方找的,介紹的有點老了。

H.264有四種畫質級別,分別是baseline, extended, main, high:

  • 1、Baseline Profile:基本畫質。支援I/P 幀,只支援無交錯(Progressive)和CAVLC;
  • 2、Extended profile:進階畫質。支援I/P/B/SP/SI 幀,只支援無交錯(Progressive)和CAVLC;(用的少)
  • 3、Main profile:主流畫質。提供I/P/B 幀,支援無交錯(Progressive)和交錯(Interlaced), 也支援CAVLC 和CABAC 的支援;
  • 4、High profile:高階畫質。在main Profile 的基礎上增加了8x8內部預測、自定義量化、 無損視訊編碼和更多的YUV 格式;

  H.264 Baseline profile、Extended profile和Main profile都是針對8位樣本資料、4:2:0格式(YUV)的視訊序列。在相同配置情況下,High profile(HP)可以比Main profile(MP)降低10%的位元速率。 根據應用領域的不同,Baseline profile多應用於實時通訊領域,Main profile多應用於流媒體領域,High profile則多應用於廣電和儲存領域

位元速率控制:(檔案大小=位元速率x時長/8)

三種方式:CQP(恆定質量)、CRF(恆定位元速率)、ABQ(平均位元速率) 。預設方法是CRF,優先順序是ABR > CQP > CRF。

bitrate和QP都沒有預設值,一旦設定他們就表示要按照相應的位元速率控制方法進行編碼,CRF有預設值23,沒有任何關於編碼控制的設定時就按照CRF預設值23來編碼。

一般的使用建議:    
CQP – 一般不推薦使用,在一些演算法驗證工作中會使用這種模式     
CRF – 適合在關注一遍編碼質量而輸出檔案大小或位元速率不是太緊要的場景下使用,一般網路壓片使用CRF。     
1 pass ABR – 適用於流媒體或者目標位元速率受限的實時應用場景。    
2 pass VBR – 適用於有目標位元速率限制而又有時間可以進行二次編碼的非實時應用。


ABR, 恆定平均目標位元速率。想要選擇這種位元速率控制方法,必須先設定bitrate
       X264中bitrate的單位是Kbps(K bit per second).

       關係:編碼後的檔案大小=位元速率x時長/8.(比如位元速率為600kbps,視訊時長為20s,那麼編碼後的h264碼流檔案大小等於1500kb(600x20/8))

別人寫的對x264結構體的說明

typedef struct x264_param_t
{
  /* CPU 標誌位 */
  unsigned int cpu;
  int i_threads; /* 並行編碼多幀 */
  int b_deterministic; /*是否允許非確定性時執行緒優化*/
  int i_sync_lookahead; /* 執行緒超前緩衝 */

  /* 視訊屬性 */
  int i_width; /* 寬度*/
  int i_height; /* 高度*/
  int i_csp; /* 編碼位元流的CSP,僅支援i420,色彩空間設定 */
  int i_level_idc; /* level值的設定*/
  int i_frame_total; /* 編碼幀的總數, 預設 0 */
/*Vui引數集視訊可用性資訊視訊標準化選項 */
  struct
  {
  /* they will be reduced to be 0 < x <= 65535 and prime */
  int i_sar_height;
  int i_sar_width; /* 設定長寬比 */

  int i_overscan; /* 0=undef, 1=no overscan, 2=overscan 過掃描線,預設"undef"(不設定),可選項:show(觀看)/crop(去除)*/

  /*見以下的值h264附件E */
  Int i_vidformat;/* 視訊格式,預設"undef",component/pal/ntsc/secam/mac/undef*/
  int b_fullrange; /*Specify full range samples setting,預設"off",可選項:off/on*/
  int i_colorprim; /*原始色度格式,預設"undef",可選項:undef/bt709/bt470m/bt470bg,smpte170m/smpte240m/film*/
  int i_transfer; /*轉換方式,預設"undef",可選項:undef/bt709/bt470m/bt470bg/linear,log100/log316/smpte170m/smpte240m*/
  int i_colmatrix; /*色度矩陣設定,預設"undef",undef/bt709/fcc/bt470bg,smpte170m/smpte240m/GBR/YCgCo*/
  int i_chroma_loc; /* both top & bottom色度樣本指定,範圍0~5,預設0 */
  } vui;

  int i_fps_num;
  int i_fps_den;
/*這兩個引數是由fps幀率確定的,賦值的過程見下:
{ float fps;   
if( sscanf( value, "%d/%d", &p->i_fps_num, &p->i_fps_den ) == 2 )
  ;
  else if( sscanf( value, "%f", &fps ) )
  {
  p->i_fps_num = (int)(fps * 1000 + .5);
  p->i_fps_den = 1000;
  }
  else
  b_error = 1;
  }
Value的值就是fps。*/

  /*流引數 */
  int i_frame_reference; /* 參考幀最大數目 */
  int i_keyint_max; /* 在此間隔設定IDR關鍵幀 */
  int i_keyint_min; /* 場景切換少於次值編碼位I, 而不是 IDR. */
  int i_scenecut_threshold; /*如何積極地插入額外的I幀 */
  int i_bframe; /*兩個相關影象間P幀的數目 */
  int i_bframe_adaptive; /*自適應B幀判定*/
  int i_bframe_bias; /*控制插入B幀判定,範圍-100~+100,越高越容易插入B幀,預設0*/
  int b_bframe_pyramid; /*允許部分B為參考幀 */
/*去塊濾波器需要的引數*/
  int b_deblocking_filter;
  int i_deblocking_filter_alphac0; /* [-6, 6] -6 light filter, 6 strong */
  int i_deblocking_filter_beta; /* [-6, 6] idem */
  /*熵編碼 */
  int b_cabac;
  int i_cabac_init_idc;

  int b_interlaced; /* 隔行掃描 */
  /*量化 */
  int i_cqm_preset; /*自定義量化矩陣(CQM),初始化量化模式為flat*/
  char *psz_cqm_file; /* JM format讀取JM格式的外部量化矩陣檔案,自動忽略其他—cqm 選項*/
  uint8_t cqm_4iy[16]; /* used only if i_cqm_preset == X264_CQM_CUSTOM */
  uint8_t cqm_4ic[16];
  uint8_t cqm_4py[16];
  uint8_t cqm_4pc[16];
  uint8_t cqm_8iy[64];
  uint8_t cqm_8py[64];

  /* 日誌 */
  void (*pf_log)( void *, int i_level, const char *psz, va_list );
  void *p_log_private;
  int i_log_level;
  int b_visualize;
  char *psz_dump_yuv; /* 重建幀的名字 */

  /* 編碼分析引數*/
  struct
  {
  unsigned int intra; /* 幀間分割槽*/
  unsigned int inter; /* 幀內分割槽 */

  int b_transform_8x8; /* 幀間分割槽*/
  int b_weighted_bipred; /*為b幀隱式加權 */
  int i_direct_mv_pred; /*時間空間隊運動預測 */
  int i_chroma_qp_offset; /*色度量化步長偏移量 */

  int i_me_method; /* 運動估計演算法 (X264_ME_*) */
  int i_me_range; /* 整畫素運動估計搜尋範圍 (from predicted mv) */
  int i_mv_range; /* 運動向量最大長度(in pixels). -1 = auto, based on level */
  int i_mv_range_thread; /* 執行緒之間的最小空間. -1 = auto, based on number of threads. */
  int i_subpel_refine; /* 亞畫素運動估計質量 */
  int b_chroma_me; /* 亞畫素色度運動估計和P幀的模式選擇 */
  int b_mixed_references; /*允許每個巨集塊的分割槽在P幀有它自己的參考號*/
  int i_trellis; /* Trellis量化,對每個8x8的塊尋找合適的量化值,需要CABAC,預設0 0:關閉1:只在最後編碼時使用2:一直使用*/
  int b_fast_pskip; /*快速P幀跳過檢測*/
  int b_dct_decimate; /* 在P-frames轉換引數域 */
  int i_noise_reduction; /*自適應偽盲區 */
  float f_psy_rd; /* Psy RD strength */
  float f_psy_trellis; /* Psy trellis strength */
  int b_psy; /* Toggle all psy optimizations */

  /*,亮度量化中使用的無效區大小*/
  int i_luma_deadzone[2]; /* {幀間, 幀內} */

  int b_psnr; /* 計算和列印PSNR資訊 */
  int b_ssim; /*計算和列印SSIM資訊*/
  } analyse;

  /* 位元速率控制引數 */
  struct
  {
  int i_rc_method; /* X264_RC_* */

  int i_qp_constant; /* 0-51 */
  int i_qp_min; /*允許的最小量化值 */
  int i_qp_max; /*允許的最大量化值*/
  int i_qp_step; /*幀間最大量化步長 */

  int i_bitrate; /*設定平均位元速率 */
  float f_rf_constant; /* 1pass VBR, nominal QP */
  float f_rate_tolerance;
  int i_vbv_max_bitrate; /*平均位元速率模式下,最大瞬時位元速率,預設0(與-B設定相同) */
  int i_vbv_buffer_size; /*位元速率控制緩衝區的大小,單位kbit,預設0 */
  float f_vbv_buffer_init; /* <=1: fraction of buffer_size. >1: kbit位元速率控制緩衝區資料保留的最大資料量與緩衝區大小之比,範圍0~1.0,預設0.9*/
  float f_ip_factor;
  float f_pb_factor;

  int i_aq_mode; /* psy adaptive QP. (X264_AQ_*) */
  float f_aq_strength;
  int b_mb_tree; /* Macroblock-tree ratecontrol. */
  int i_lookahead;

  /* 2pass 多次壓縮位元速率控制 */
  int b_stat_write; /* Enable stat writing in psz_stat_out */
  char *psz_stat_out;
  int b_stat_read; /* Read stat from psz_stat_in and use it */
  char *psz_stat_in;

  /* 2pass params (same as ffmpeg ones) */
  float f_qcompress; /* 0.0 => cbr, 1.0 => constant qp */
  float f_qblur; /*時間上模糊量化 */
  float f_complexity_blur; /* 時間上模糊複雜性 */
  x264_zone_t *zones; /* 位元速率控制覆蓋 */
  int i_zones; /* number of zone_t's */
  char *psz_zones; /*指定區的另一種方法*/
  } rc;

  /* Muxing parameters */
  int b_aud; /*生成訪問單元分隔符*/
  int b_repeat_headers; /* 在每個關鍵幀前放置SPS/PPS*/
  int i_sps_id; /* SPS 和 PPS id 號 */

  /*切片(像條)引數 */
  int i_slice_max_size; /* 每片位元組的最大數,包括預計的NAL開銷. */
  int i_slice_max_mbs; /* 每片巨集塊的最大數,重寫 i_slice_count */
  int i_slice_count; /* 每幀的像條數目: 設定矩形像條. */

  /* Optional callback for freeing this x264_param_t when it is done being used.
  * Only used when the x264_param_t sits in memory for an indefinite period of time,
  * i.e. when an x264_param_t is passed to x264_t in an x264_picture_t or in zones.
  * Not used when x264_encoder_reconfig is called directly. */
  void (*param_free)( void* );
} x264_param_t;

這個是老版本的,新的x264好像新增了一些引數。