分享

x264代码剖析(十一):核心算法之宏块分析函数x264_macroblock_analyse()

 托尼虎 2018-12-25

x264代码剖析(十一):核心算法之宏块分析函数x264_macroblock_analyse()

版权声明:本文为博主原创文章,转载请标注转载网址:http://blog.csdn.net/frd2009041510 https://blog.csdn.net/FRD2009041510/article/details/50877029

x264代码剖析(十一):核心算法之宏块分析函数x264_macroblock_analyse()

 

        x264的 x264_slice_write()函数中调用了宏块分析函数x264_macroblock_analyse(),该模块主要完成2大任务:一是对于帧内宏块,分析帧内预测模式;二是对于帧间宏块,进行运动估计,分析帧间预测模式。

 

        如下图所示是x264_macroblock_analyse()的函数关系图。




        从图中可以总结出x264_macroblock_analyse()函数调用了如下几个主要的函数:

x264_ratecontrol_mb_qp( ):通过码率控制方法获取本宏块QP

x264_mb_analyse_init()Analysis模块初始化。

x264_mb_analyse_intra()Intra宏块帧内预测模式分析。

x264_macroblock_probe_pskip():分析是否是skip模式。

x264_mb_analyse_inter_p16x16()P16x16宏块帧间预测模式分析。

x264_mb_analyse_inter_p8x8()P8x8宏块帧间预测模式分析。

x264_mb_analyse_inter_p16x8()P16x8宏块帧间预测模式分析。

x264_mb_analyse_inter_b16x16()B16x16宏块帧间预测模式分析。

x264_mb_analyse_inter_b8x8()B8x8宏块帧间预测模式分析。

x264_mb_analyse_inter_b16x8()B16x8宏块帧间预测模式分析。

 

        x264_macroblock_analyse()用于分析宏块的预测模式。该函数的定义位于encoder\analyse.c,对应的代码分析如下:


  1. /******************************************************************/
  2. /******************************************************************/
  3. /*
  4. ======Analysed by RuiDong Fang
  5. ======Csdn Blog:http://blog.csdn.net/frd2009041510
  6. ======Date:2016.03.13(今天当舅舅啦!!!哈哈哈)
  7. */
  8. /******************************************************************/
  9. /******************************************************************/
  10. /************====== x264_macroblock_analyse()函数 ======************/
  11. /*
  12. 功能:分析函数,调用了帧内预测与帧间预测
  13. */
  14. /*****************************************************************************
  15. * x264_macroblock_analyse:
  16. *****************************************************************************/
  17. void x264_macroblock_analyse( x264_t *h )
  18. {
  19. x264_mb_analysis_t analysis;
  20. int i_cost = COST_MAX;
  21. h->mb.i_qp = x264_ratecontrol_mb_qp( h ); ///////////////////通过码率控制方法,获取本宏块QP
  22. /* If the QP of this MB is within 1 of the previous MB, code the same QP as the previous MB,
  23. * to lower the bit cost of the qp_delta. Don't do this if QPRD is enabled. */
  24. if( h->param.rc.i_aq_mode && h->param.analyse.i_subpel_refine < 10 )
  25. h->mb.i_qp = abs(h->mb.i_qp - h->mb.i_last_qp) == 1 ? h->mb.i_last_qp : h->mb.i_qp;
  26. if( h->param.analyse.b_mb_info )
  27. h->fdec->effective_qp[h->mb.i_mb_xy] = h->mb.i_qp; /* Store the real analysis QP. */
  28. x264_mb_analyse_init( h, &analysis, h->mb.i_qp ); ///////////////////Analysis模块初始化
  29. /*--------------------------- Do the analysis ---------------------------*/
  30. /*******************************************************/
  31. /*
  32. I帧:只使用帧内预测,分别计算亮度16x16(4种)和4x4(9种)所有模式的代价值,选出代价最小的模式
  33. */
  34. /*******************************************************/
  35. if( h->sh.i_type == SLICE_TYPE_I )
  36. {
  37. //I slice
  38. //通过一系列帧内预测模式(16x16的4种,4x4的9种)代价的计算得出代价最小的最优模式
  39. intra_analysis:
  40. if( analysis.i_mbrd )
  41. x264_mb_init_fenc_cache( h, analysis.i_mbrd >= 2 );
  42. //帧内预测分析
  43. //从16×16的SAD,4个8×8的SAD和,16个4×4SAD中选出最优方式
  44. x264_mb_analyse_intra( h, &analysis, COST_MAX ); ///////////////////Intra宏块帧内预测模式分析
  45. if( analysis.i_mbrd )
  46. x264_intra_rd( h, &analysis, COST_MAX );
  47. //分析结果(开销)都存储在analysis结构体中
  48. i_cost = analysis.i_satd_i16x16;
  49. h->mb.i_type = I_16x16;
  50. //如果I4x4或者I8x8开销更小的话就拷贝
  51. //copy if little
  52. COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, h->mb.i_type, I_4x4 );
  53. COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, h->mb.i_type, I_8x8 );
  54. //画面极其特殊的时候,才有可能用到PCM
  55. if( analysis.i_satd_pcm < i_cost )
  56. h->mb.i_type = I_PCM;
  57. else if( analysis.i_mbrd >= 2 )
  58. x264_intra_rd_refine( h, &analysis );
  59. }
  60. /*******************************************************/
  61. /*
  62. P帧:计算帧内模式和帧间模式( P Slice允许有Intra宏块和P宏块;同理B帧也支持Intra宏块)。
  63. 对P帧的每一种分割进行帧间预测,得到最佳的运动矢量及最佳匹配块。
  64. 帧间预测过程:选出最佳矢量——>找到最佳的整像素点——>找到最佳的二分之一像素点——>找到最佳的1/4像素点
  65. 然后取代价最小的为最佳MV和分割方式
  66. 最后从帧内模式和帧间模式中选择代价比较小的方式(有可能没有找到很好的匹配块,这时候就直接使用帧内预测而不是帧间预测)。
  67. */
  68. /*******************************************************/
  69. else if( h->sh.i_type == SLICE_TYPE_P )
  70. {
  71. int b_skip = 0;
  72. h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
  73. analysis.b_try_skip = 0;
  74. if( analysis.b_force_intra )
  75. {
  76. if( !h->param.analyse.b_psy )
  77. {
  78. x264_mb_analyse_init_qp( h, &analysis, X264_MAX( h->mb.i_qp - h->mb.ip_offset, h->param.rc.i_qp_min ) );
  79. goto intra_analysis;
  80. }
  81. }
  82. else
  83. {
  84. /* Special fast-skip logic using information from mb_info. */
  85. if( h->fdec->mb_info && (h->fdec->mb_info[h->mb.i_mb_xy]&X264_MBINFO_CONSTANT) )
  86. {
  87. if( !SLICE_MBAFF && (h->fdec->i_frame - h->fref[0][0]->i_frame) == 1 && !h->sh.b_weighted_pred &&
  88. h->fref[0][0]->effective_qp[h->mb.i_mb_xy] <= h->mb.i_qp )
  89. {
  90. h->mb.i_partition = D_16x16;
  91. /* Use the P-SKIP MV if we can... */
  92. if( !M32(h->mb.cache.pskip_mv) )
  93. {
  94. b_skip = 1;
  95. h->mb.i_type = P_SKIP;
  96. }
  97. /* Otherwise, just force a 16x16 block. */
  98. else
  99. {
  100. h->mb.i_type = P_L0;
  101. analysis.l0.me16x16.i_ref = 0;
  102. M32( analysis.l0.me16x16.mv ) = 0;
  103. }
  104. goto skip_analysis;
  105. }
  106. /* Reset the information accordingly */
  107. else if( h->param.analyse.b_mb_info_update )
  108. h->fdec->mb_info[h->mb.i_mb_xy] &= ~X264_MBINFO_CONSTANT;
  109. }
  110. int skip_invalid = h->i_thread_frames > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1];
  111. /* If the current macroblock is off the frame, just skip it. */
  112. if( HAVE_INTERLACED && !MB_INTERLACED && h->mb.i_mb_y * 16 >= h->param.i_height && !skip_invalid )
  113. b_skip = 1;
  114. /* Fast P_SKIP detection */
  115. else if( h->param.analyse.b_fast_pskip )
  116. {
  117. if( skip_invalid )
  118. // FIXME don't need to check this if the reference frame is done
  119. {}
  120. else if( h->param.analyse.i_subpel_refine >= 3 )
  121. analysis.b_try_skip = 1;
  122. else if( h->mb.i_mb_type_left[0] == P_SKIP ||
  123. h->mb.i_mb_type_top == P_SKIP ||
  124. h->mb.i_mb_type_topleft == P_SKIP ||
  125. h->mb.i_mb_type_topright == P_SKIP )
  126. b_skip = x264_macroblock_probe_pskip( h ); ///////////////////分析是否是skip模式--P
  127. }
  128. }
  129. h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
  130. if( b_skip )
  131. {
  132. h->mb.i_type = P_SKIP;
  133. h->mb.i_partition = D_16x16;
  134. assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->i_thread_frames == 1 );
  135. skip_analysis:
  136. /* Set up MVs for future predictors */
  137. for( int i = 0; i < h->mb.pic.i_fref[0]; i++ )
  138. M32( h->mb.mvr[0][i][h->mb.i_mb_xy] ) = 0;
  139. }
  140. else
  141. {
  142. const unsigned int flags = h->param.analyse.inter;
  143. int i_type;
  144. int i_partition;
  145. int i_satd_inter, i_satd_intra;
  146. x264_mb_analyse_load_costs( h, &analysis );
  147. x264_mb_analyse_inter_p16x16( h, &analysis ); ///////////////////16x16 帧间预测宏块分析--P
  148. if( h->mb.i_type == P_SKIP )
  149. {
  150. for( int i = 1; i < h->mb.pic.i_fref[0]; i++ )
  151. M32( h->mb.mvr[0][i][h->mb.i_mb_xy] ) = 0;
  152. return;
  153. }
  154. if( flags & X264_ANALYSE_PSUB16x16 )
  155. {
  156. if( h->param.analyse.b_mixed_references )
  157. x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
  158. else
  159. x264_mb_analyse_inter_p8x8( h, &analysis ); ///////////////////8x8帧间预测宏块分析--P
  160. }
  161. /* Select best inter mode */
  162. i_type = P_L0;
  163. i_partition = D_16x16;
  164. i_cost = analysis.l0.me16x16.cost;
  165. //如果8x8的代价值小于16x16
  166. //则进行8x8子块分割的处理
  167. //处理的数据源自于l0?
  168. if( ( flags & X264_ANALYSE_PSUB16x16 ) && (!analysis.b_early_terminate ||
  169. analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost) )
  170. {
  171. i_type = P_8x8;
  172. i_partition = D_8x8;
  173. i_cost = analysis.l0.i_cost8x8;
  174. /* Do sub 8x8 */
  175. if( flags & X264_ANALYSE_PSUB8x8 )
  176. {
  177. for( int i = 0; i < 4; i++ )//8x8块的子块的分析
  178. {
  179. x264_mb_analyse_inter_p4x4( h, &analysis, i ); ///////////////////4x4帧间预测宏块分析--P
  180. int i_thresh8x4 = analysis.l0.me4x4[i][1].cost_mv + analysis.l0.me4x4[i][2].cost_mv;
  181. //如果4x4小于8x8,则再分析8x4,4x8的代价
  182. if( !analysis.b_early_terminate || analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost + i_thresh8x4 )
  183. {
  184. int i_cost8x8 = analysis.l0.i_cost4x4[i];
  185. h->mb.i_sub_partition[i] = D_L0_4x4;
  186. x264_mb_analyse_inter_p8x4( h, &analysis, i ); ///////////////////8x4帧间预测宏块分析--P
  187. COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
  188. h->mb.i_sub_partition[i], D_L0_8x4 );//如果8x4小于8x8
  189. x264_mb_analyse_inter_p4x8( h, &analysis, i ); ///////////////////4x8帧间预测宏块分析--P
  190. COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
  191. h->mb.i_sub_partition[i], D_L0_4x8 );//如果4x8小于8x8
  192. i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
  193. }
  194. x264_mb_cache_mv_p8x8( h, &analysis, i );
  195. }
  196. analysis.l0.i_cost8x8 = i_cost;
  197. }
  198. }
  199. /* Now do 16x8/8x16 */
  200. int i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
  201. //前提要求8x8的代价值小于16x16
  202. if( ( flags & X264_ANALYSE_PSUB16x16 ) && (!analysis.b_early_terminate ||
  203. analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8) )
  204. {
  205. int i_avg_mv_ref_cost = (analysis.l0.me8x8[2].cost_mv + analysis.l0.me8x8[2].i_ref_cost
  206. + analysis.l0.me8x8[3].cost_mv + analysis.l0.me8x8[3].i_ref_cost + 1) >> 1;
  207. analysis.i_cost_est16x8[1] = analysis.i_satd8x8[0][2] + analysis.i_satd8x8[0][3] + i_avg_mv_ref_cost;
  208. x264_mb_analyse_inter_p16x8( h, &analysis, i_cost ); ///////////////////16x8帧间预测宏块分析--P
  209. COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
  210. i_avg_mv_ref_cost = (analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[1].i_ref_cost
  211. + analysis.l0.me8x8[3].cost_mv + analysis.l0.me8x8[3].i_ref_cost + 1) >> 1;
  212. analysis.i_cost_est8x16[1] = analysis.i_satd8x8[0][1] + analysis.i_satd8x8[0][3] + i_avg_mv_ref_cost;
  213. x264_mb_analyse_inter_p8x16( h, &analysis, i_cost ); ///////////////////8x16帧间预测宏块分析--P
  214. COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
  215. }
  216. h->mb.i_partition = i_partition;
  217. /* refine qpel */
  218. //亚像素精度搜索
  219. //FIXME mb_type costs?
  220. if( analysis.i_mbrd || !h->mb.i_subpel_refine )
  221. {
  222. /* refine later */
  223. }
  224. else if( i_partition == D_16x16 )
  225. {
  226. x264_me_refine_qpel( h, &analysis.l0.me16x16 ); ///////////////////////亚像素精度搜索
  227. i_cost = analysis.l0.me16x16.cost;
  228. }
  229. else if( i_partition == D_16x8 )
  230. {
  231. x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
  232. x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
  233. i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
  234. }
  235. else if( i_partition == D_8x16 )
  236. {
  237. x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
  238. x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
  239. i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
  240. }
  241. else if( i_partition == D_8x8 )
  242. {
  243. i_cost = 0;
  244. for( int i8x8 = 0; i8x8 < 4; i8x8++ )
  245. {
  246. switch( h->mb.i_sub_partition[i8x8] )
  247. {
  248. case D_L0_8x8:
  249. x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
  250. i_cost += analysis.l0.me8x8[i8x8].cost;
  251. break;
  252. case D_L0_8x4:
  253. x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
  254. x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
  255. i_cost += analysis.l0.me8x4[i8x8][0].cost +
  256. analysis.l0.me8x4[i8x8][1].cost;
  257. break;
  258. case D_L0_4x8:
  259. x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
  260. x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
  261. i_cost += analysis.l0.me4x8[i8x8][0].cost +
  262. analysis.l0.me4x8[i8x8][1].cost;
  263. break;
  264. case D_L0_4x4:
  265. x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
  266. x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
  267. x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
  268. x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
  269. i_cost += analysis.l0.me4x4[i8x8][0].cost +
  270. analysis.l0.me4x4[i8x8][1].cost +
  271. analysis.l0.me4x4[i8x8][2].cost +
  272. analysis.l0.me4x4[i8x8][3].cost;
  273. break;
  274. default:
  275. x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)\n" );
  276. break;
  277. }
  278. }
  279. }
  280. if( h->mb.b_chroma_me )
  281. {
  282. if( CHROMA444 )
  283. {
  284. x264_mb_analyse_intra( h, &analysis, i_cost );
  285. x264_mb_analyse_intra_chroma( h, &analysis );
  286. }
  287. else
  288. {
  289. x264_mb_analyse_intra_chroma( h, &analysis );
  290. x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_chroma );
  291. }
  292. analysis.i_satd_i16x16 += analysis.i_satd_chroma;
  293. analysis.i_satd_i8x8 += analysis.i_satd_chroma;
  294. analysis.i_satd_i4x4 += analysis.i_satd_chroma;
  295. }
  296. else
  297. x264_mb_analyse_intra( h, &analysis, i_cost );//P Slice中也允许有Intra宏块,所以也要进行分析
  298. i_satd_inter = i_cost;
  299. i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
  300. analysis.i_satd_i8x8,
  301. analysis.i_satd_i4x4 );
  302. if( analysis.i_mbrd )
  303. {
  304. x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
  305. i_type = P_L0;
  306. i_partition = D_16x16;
  307. i_cost = analysis.l0.i_rd16x16;
  308. COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
  309. COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
  310. COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
  311. h->mb.i_type = i_type;
  312. h->mb.i_partition = i_partition;
  313. if( i_cost < COST_MAX )
  314. x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
  315. x264_intra_rd( h, &analysis, i_satd_inter * 5/4 + 1 );
  316. }
  317. //获取最小的代价
  318. COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
  319. COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
  320. COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
  321. COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
  322. h->mb.i_type = i_type;
  323. if( analysis.b_force_intra && !IS_INTRA(i_type) )
  324. {
  325. /* Intra masking: copy fdec to fenc and re-encode the block as intra in order to make it appear as if
  326. * it was an inter block. */
  327. x264_analyse_update_cache( h, &analysis ); /////////////////////
  328. x264_macroblock_encode( h ); /////////////////////
  329. for( int p = 0; p < (CHROMA444 ? 3 : 1); p++ )
  330. h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fenc[p], FENC_STRIDE, h->mb.pic.p_fdec[p], FDEC_STRIDE, 16 );
  331. if( !CHROMA444 )
  332. {
  333. int height = 16 >> CHROMA_V_SHIFT;
  334. h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fenc[1], FENC_STRIDE, h->mb.pic.p_fdec[1], FDEC_STRIDE, height );
  335. h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fenc[2], FENC_STRIDE, h->mb.pic.p_fdec[2], FDEC_STRIDE, height );
  336. }
  337. x264_mb_analyse_init_qp( h, &analysis, X264_MAX( h->mb.i_qp - h->mb.ip_offset, h->param.rc.i_qp_min ) );
  338. goto intra_analysis;
  339. }
  340. if( analysis.i_mbrd >= 2 && h->mb.i_type != I_PCM )
  341. {
  342. if( IS_INTRA( h->mb.i_type ) )
  343. {
  344. x264_intra_rd_refine( h, &analysis );
  345. }
  346. else if( i_partition == D_16x16 )
  347. {
  348. x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
  349. analysis.l0.me16x16.cost = i_cost;
  350. x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
  351. }
  352. else if( i_partition == D_16x8 )
  353. {
  354. h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
  355. h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
  356. x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
  357. x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
  358. x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0, 0 );
  359. x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 8, 0 );
  360. }
  361. else if( i_partition == D_8x16 )
  362. {
  363. h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
  364. h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
  365. x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
  366. x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
  367. x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0, 0 );
  368. x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 4, 0 );
  369. }
  370. else if( i_partition == D_8x8 )
  371. {
  372. x264_analyse_update_cache( h, &analysis );
  373. for( int i8x8 = 0; i8x8 < 4; i8x8++ )
  374. {
  375. if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
  376. {
  377. x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8*4, 0 );
  378. }
  379. else if( h->mb.i_sub_partition[i8x8] == D_L0_8x4 )
  380. {
  381. x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
  382. x264_me_refine_qpel_rd( h, &analysis.l0.me8x4[i8x8][1], analysis.i_lambda2, i8x8*4+2, 0 );
  383. }
  384. else if( h->mb.i_sub_partition[i8x8] == D_L0_4x8 )
  385. {
  386. x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
  387. x264_me_refine_qpel_rd( h, &analysis.l0.me4x8[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
  388. }
  389. else if( h->mb.i_sub_partition[i8x8] == D_L0_4x4 )
  390. {
  391. x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][0], analysis.i_lambda2, i8x8*4+0, 0 );
  392. x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][1], analysis.i_lambda2, i8x8*4+1, 0 );
  393. x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][2], analysis.i_lambda2, i8x8*4+2, 0 );
  394. x264_me_refine_qpel_rd( h, &analysis.l0.me4x4[i8x8][3], analysis.i_lambda2, i8x8*4+3, 0 );
  395. }
  396. }
  397. }
  398. }
  399. }
  400. }
  401. /*******************************************************/
  402. /*
  403. B Slice的时候
  404. */
  405. /*******************************************************/
  406. else if( h->sh.i_type == SLICE_TYPE_B )
  407. {
  408. int i_bskip_cost = COST_MAX;
  409. int b_skip = 0;
  410. if( analysis.i_mbrd )
  411. x264_mb_init_fenc_cache( h, analysis.i_mbrd >= 2 );
  412. h->mb.i_type = B_SKIP;
  413. if( h->mb.b_direct_auto_write )
  414. {
  415. /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
  416. for( int i = 0; i < 2; i++ )
  417. {
  418. int b_changed = 1;
  419. h->sh.b_direct_spatial_mv_pred ^= 1;
  420. analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
  421. if( analysis.b_direct_available )
  422. {
  423. if( b_changed )
  424. {
  425. x264_mb_mc( h );
  426. b_skip = x264_macroblock_probe_bskip( h ); ///////////////////////分析是否是skip模式--B
  427. }
  428. h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
  429. }
  430. else
  431. b_skip = 0;
  432. }
  433. }
  434. else
  435. analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
  436. analysis.b_try_skip = 0;
  437. if( analysis.b_direct_available )
  438. {
  439. if( !h->mb.b_direct_auto_write )
  440. x264_mb_mc( h );
  441. /* If the current macroblock is off the frame, just skip it. */
  442. if( HAVE_INTERLACED && !MB_INTERLACED && h->mb.i_mb_y * 16 >= h->param.i_height )
  443. b_skip = 1;
  444. else if( analysis.i_mbrd )
  445. {
  446. i_bskip_cost = ssd_mb( h );
  447. /* 6 = minimum cavlc cost of a non-skipped MB */
  448. b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
  449. }
  450. else if( !h->mb.b_direct_auto_write )
  451. {
  452. /* Conditioning the probe on neighboring block types
  453. * doesn't seem to help speed or quality. */
  454. analysis.b_try_skip = x264_macroblock_probe_bskip( h );
  455. if( h->param.analyse.i_subpel_refine < 3 )
  456. b_skip = analysis.b_try_skip;
  457. }
  458. /* Set up MVs for future predictors */
  459. if( b_skip )
  460. {
  461. for( int i = 0; i < h->mb.pic.i_fref[0]; i++ )
  462. M32( h->mb.mvr[0][i][h->mb.i_mb_xy] ) = 0;
  463. for( int i = 0; i < h->mb.pic.i_fref[1]; i++ )
  464. M32( h->mb.mvr[1][i][h->mb.i_mb_xy] ) = 0;
  465. }
  466. }
  467. if( !b_skip )
  468. {
  469. const unsigned int flags = h->param.analyse.inter;
  470. int i_type;
  471. int i_partition;
  472. int i_satd_inter;
  473. h->mb.b_skip_mc = 0;
  474. h->mb.i_type = B_DIRECT;
  475. x264_mb_analyse_load_costs( h, &analysis );
  476. /* select best inter mode */
  477. /* direct must be first */
  478. if( analysis.b_direct_available )
  479. x264_mb_analyse_inter_direct( h, &analysis );
  480. x264_mb_analyse_inter_b16x16( h, &analysis ); ///////////////////////16x16 帧间预测宏块分析--B
  481. if( h->mb.i_type == B_SKIP )
  482. {
  483. for( int i = 1; i < h->mb.pic.i_fref[0]; i++ )
  484. M32( h->mb.mvr[0][i][h->mb.i_mb_xy] ) = 0;
  485. for( int i = 1; i < h->mb.pic.i_fref[1]; i++ )
  486. M32( h->mb.mvr[1][i][h->mb.i_mb_xy] ) = 0;
  487. return;
  488. }
  489. i_type = B_L0_L0;
  490. i_partition = D_16x16;
  491. i_cost = analysis.l0.me16x16.cost;
  492. COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
  493. COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
  494. COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
  495. if( analysis.i_mbrd && analysis.b_early_terminate && analysis.i_cost16x16direct <= i_cost * 33/32 )
  496. {
  497. x264_mb_analyse_b_rd( h, &analysis, i_cost );
  498. if( i_bskip_cost < analysis.i_rd16x16direct &&
  499. i_bskip_cost < analysis.i_rd16x16bi &&
  500. i_bskip_cost < analysis.l0.i_rd16x16 &&
  501. i_bskip_cost < analysis.l1.i_rd16x16 )
  502. {
  503. h->mb.i_type = B_SKIP;
  504. x264_analyse_update_cache( h, &analysis );
  505. return;
  506. }
  507. }
  508. if( flags & X264_ANALYSE_BSUB16x16 )
  509. {
  510. if( h->param.analyse.b_mixed_references )
  511. x264_mb_analyse_inter_b8x8_mixed_ref( h, &analysis );
  512. else
  513. x264_mb_analyse_inter_b8x8( h, &analysis ); ///////////////////////8x8 帧间预测宏块分析--B
  514. COPY3_IF_LT( i_cost, analysis.i_cost8x8bi, i_type, B_8x8, i_partition, D_8x8 );
  515. /* Try to estimate the cost of b16x8/b8x16 based on the satd scores of the b8x8 modes */
  516. int i_cost_est16x8bi_total = 0, i_cost_est8x16bi_total = 0;
  517. int i_mb_type, i_partition16x8[2], i_partition8x16[2];
  518. for( int i = 0; i < 2; i++ )
  519. {
  520. int avg_l0_mv_ref_cost, avg_l1_mv_ref_cost;
  521. int i_l0_satd, i_l1_satd, i_bi_satd, i_best_cost;
  522. // 16x8
  523. i_best_cost = COST_MAX;
  524. i_l0_satd = analysis.i_satd8x8[0][i*2] + analysis.i_satd8x8[0][i*2+1];
  525. i_l1_satd = analysis.i_satd8x8[1][i*2] + analysis.i_satd8x8[1][i*2+1];
  526. i_bi_satd = analysis.i_satd8x8[2][i*2] + analysis.i_satd8x8[2][i*2+1];
  527. avg_l0_mv_ref_cost = ( analysis.l0.me8x8[i*2].cost_mv + analysis.l0.me8x8[i*2].i_ref_cost
  528. + analysis.l0.me8x8[i*2+1].cost_mv + analysis.l0.me8x8[i*2+1].i_ref_cost + 1 ) >> 1;
  529. avg_l1_mv_ref_cost = ( analysis.l1.me8x8[i*2].cost_mv + analysis.l1.me8x8[i*2].i_ref_cost
  530. + analysis.l1.me8x8[i*2+1].cost_mv + analysis.l1.me8x8[i*2+1].i_ref_cost + 1 ) >> 1;
  531. COPY2_IF_LT( i_best_cost, i_l0_satd + avg_l0_mv_ref_cost, i_partition16x8[i], D_L0_8x8 );
  532. COPY2_IF_LT( i_best_cost, i_l1_satd + avg_l1_mv_ref_cost, i_partition16x8[i], D_L1_8x8 );
  533. COPY2_IF_LT( i_best_cost, i_bi_satd + avg_l0_mv_ref_cost + avg_l1_mv_ref_cost, i_partition16x8[i], D_BI_8x8 );
  534. analysis.i_cost_est16x8[i] = i_best_cost;
  535. // 8x16
  536. i_best_cost = COST_MAX;
  537. i_l0_satd = analysis.i_satd8x8[0][i] + analysis.i_satd8x8[0][i+2];
  538. i_l1_satd = analysis.i_satd8x8[1][i] + analysis.i_satd8x8[1][i+2];
  539. i_bi_satd = analysis.i_satd8x8[2][i] + analysis.i_satd8x8[2][i+2];
  540. avg_l0_mv_ref_cost = ( analysis.l0.me8x8[i].cost_mv + analysis.l0.me8x8[i].i_ref_cost
  541. + analysis.l0.me8x8[i+2].cost_mv + analysis.l0.me8x8[i+2].i_ref_cost + 1 ) >> 1;
  542. avg_l1_mv_ref_cost = ( analysis.l1.me8x8[i].cost_mv + analysis.l1.me8x8[i].i_ref_cost
  543. + analysis.l1.me8x8[i+2].cost_mv + analysis.l1.me8x8[i+2].i_ref_cost + 1 ) >> 1;
  544. COPY2_IF_LT( i_best_cost, i_l0_satd + avg_l0_mv_ref_cost, i_partition8x16[i], D_L0_8x8 );
  545. COPY2_IF_LT( i_best_cost, i_l1_satd + avg_l1_mv_ref_cost, i_partition8x16[i], D_L1_8x8 );
  546. COPY2_IF_LT( i_best_cost, i_bi_satd + avg_l0_mv_ref_cost + avg_l1_mv_ref_cost, i_partition8x16[i], D_BI_8x8 );
  547. analysis.i_cost_est8x16[i] = i_best_cost;
  548. }
  549. i_mb_type = B_L0_L0 + (i_partition16x8[0]>>2) * 3 + (i_partition16x8[1]>>2);
  550. analysis.i_cost_est16x8[1] += analysis.i_lambda * i_mb_b16x8_cost_table[i_mb_type];
  551. i_cost_est16x8bi_total = analysis.i_cost_est16x8[0] + analysis.i_cost_est16x8[1];
  552. i_mb_type = B_L0_L0 + (i_partition8x16[0]>>2) * 3 + (i_partition8x16[1]>>2);
  553. analysis.i_cost_est8x16[1] += analysis.i_lambda * i_mb_b16x8_cost_table[i_mb_type];
  554. i_cost_est8x16bi_total = analysis.i_cost_est8x16[0] + analysis.i_cost_est8x16[1];
  555. /* We can gain a little speed by checking the mode with the lowest estimated cost first */
  556. int try_16x8_first = i_cost_est16x8bi_total < i_cost_est8x16bi_total;
  557. if( try_16x8_first && (!analysis.b_early_terminate || i_cost_est16x8bi_total < i_cost) )
  558. {
  559. x264_mb_analyse_inter_b16x8( h, &analysis, i_cost ); ///////////////////////16x8 帧间预测宏块分析--B
  560. COPY3_IF_LT( i_cost, analysis.i_cost16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
  561. }
  562. if( !analysis.b_early_terminate || i_cost_est8x16bi_total < i_cost )
  563. {
  564. x264_mb_analyse_inter_b8x16( h, &analysis, i_cost ); ///////////////////////8x16 帧间预测宏块分析--B
  565. COPY3_IF_LT( i_cost, analysis.i_cost8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
  566. }
  567. if( !try_16x8_first && (!analysis.b_early_terminate || i_cost_est16x8bi_total < i_cost) )
  568. {
  569. x264_mb_analyse_inter_b16x8( h, &analysis, i_cost ); ///////////////////////16x8 帧间预测宏块分析--B
  570. COPY3_IF_LT( i_cost, analysis.i_cost16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
  571. }
  572. }
  573. if( analysis.i_mbrd || !h->mb.i_subpel_refine )
  574. {
  575. /* refine later */
  576. }
  577. /* refine qpel */
  578. else if( i_partition == D_16x16 )
  579. {
  580. analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
  581. analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
  582. if( i_type == B_L0_L0 )
  583. {
  584. x264_me_refine_qpel( h, &analysis.l0.me16x16 ); /////////////////////亚像素精度搜索
  585. i_cost = analysis.l0.me16x16.cost
  586. + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
  587. }
  588. else if( i_type == B_L1_L1 )
  589. {
  590. x264_me_refine_qpel( h, &analysis.l1.me16x16 );
  591. i_cost = analysis.l1.me16x16.cost
  592. + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
  593. }
  594. else if( i_type == B_BI_BI )
  595. {
  596. x264_me_refine_qpel( h, &analysis.l0.bi16x16 );
  597. x264_me_refine_qpel( h, &analysis.l1.bi16x16 );
  598. }
  599. }
  600. else if( i_partition == D_16x8 )
  601. {
  602. for( int i = 0; i < 2; i++ )
  603. {
  604. if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
  605. x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
  606. if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
  607. x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
  608. }
  609. }
  610. else if( i_partition == D_8x16 )
  611. {
  612. for( int i = 0; i < 2; i++ )
  613. {
  614. if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
  615. x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
  616. if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
  617. x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
  618. }
  619. }
  620. else if( i_partition == D_8x8 )
  621. {
  622. for( int i = 0; i < 4; i++ )
  623. {
  624. x264_me_t *m;
  625. int i_part_cost_old;
  626. int i_type_cost;
  627. int i_part_type = h->mb.i_sub_partition[i];
  628. int b_bidir = (i_part_type == D_BI_8x8);
  629. if( i_part_type == D_DIRECT_8x8 )
  630. continue;
  631. if( x264_mb_partition_listX_table[0][i_part_type] )
  632. {
  633. m = &analysis.l0.me8x8[i];
  634. i_part_cost_old = m->cost;
  635. i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
  636. m->cost -= i_type_cost;
  637. x264_me_refine_qpel( h, m );
  638. if( !b_bidir )
  639. analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
  640. }
  641. if( x264_mb_partition_listX_table[1][i_part_type] )
  642. {
  643. m = &analysis.l1.me8x8[i];
  644. i_part_cost_old = m->cost;
  645. i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
  646. m->cost -= i_type_cost;
  647. x264_me_refine_qpel( h, m );
  648. if( !b_bidir )
  649. analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
  650. }
  651. /* TODO: update mvp? */
  652. }
  653. }
  654. i_satd_inter = i_cost;
  655. if( analysis.i_mbrd )
  656. {
  657. x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
  658. i_type = B_SKIP;
  659. i_cost = i_bskip_cost;
  660. i_partition = D_16x16;
  661. COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
  662. COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
  663. COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
  664. COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
  665. COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
  666. COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
  667. COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
  668. h->mb.i_type = i_type;
  669. h->mb.i_partition = i_partition;
  670. }
  671. if( h->mb.b_chroma_me )
  672. {
  673. if( CHROMA444 )
  674. {
  675. x264_mb_analyse_intra( h, &analysis, i_satd_inter );
  676. x264_mb_analyse_intra_chroma( h, &analysis );
  677. }
  678. else
  679. {
  680. x264_mb_analyse_intra_chroma( h, &analysis );
  681. x264_mb_analyse_intra( h, &analysis, i_satd_inter - analysis.i_satd_chroma );
  682. }
  683. analysis.i_satd_i16x16 += analysis.i_satd_chroma;
  684. analysis.i_satd_i8x8 += analysis.i_satd_chroma;
  685. analysis.i_satd_i4x4 += analysis.i_satd_chroma;
  686. }
  687. else
  688. x264_mb_analyse_intra( h, &analysis, i_satd_inter );//B Slice中也允许有Intra宏块,所以也要进行分析
  689. if( analysis.i_mbrd )
  690. {
  691. x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
  692. x264_intra_rd( h, &analysis, i_satd_inter * 17/16 + 1 );
  693. }
  694. COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
  695. COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
  696. COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
  697. COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
  698. h->mb.i_type = i_type;
  699. h->mb.i_partition = i_partition;
  700. if( analysis.i_mbrd >= 2 && IS_INTRA( i_type ) && i_type != I_PCM )
  701. x264_intra_rd_refine( h, &analysis );
  702. if( h->mb.i_subpel_refine >= 5 )
  703. x264_refine_bidir( h, &analysis );
  704. if( analysis.i_mbrd >= 2 && i_type > B_DIRECT && i_type < B_SKIP )
  705. {
  706. int i_biweight;
  707. x264_analyse_update_cache( h, &analysis ); /////////////////////
  708. if( i_partition == D_16x16 )
  709. {
  710. if( i_type == B_L0_L0 )
  711. {
  712. analysis.l0.me16x16.cost = i_cost;
  713. x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0, 0 );
  714. }
  715. else if( i_type == B_L1_L1 )
  716. {
  717. analysis.l1.me16x16.cost = i_cost;
  718. x264_me_refine_qpel_rd( h, &analysis.l1.me16x16, analysis.i_lambda2, 0, 1 );
  719. }
  720. else if( i_type == B_BI_BI )
  721. {
  722. i_biweight = h->mb.bipred_weight[analysis.l0.bi16x16.i_ref][analysis.l1.bi16x16.i_ref];
  723. x264_me_refine_bidir_rd( h, &analysis.l0.bi16x16, &analysis.l1.bi16x16, i_biweight, 0, analysis.i_lambda2 );
  724. }
  725. }
  726. else if( i_partition == D_16x8 )
  727. {
  728. for( int i = 0; i < 2; i++ )
  729. {
  730. h->mb.i_sub_partition[i*2] = h->mb.i_sub_partition[i*2+1] = analysis.i_mb_partition16x8[i];
  731. if( analysis.i_mb_partition16x8[i] == D_L0_8x8 )
  732. x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[i], analysis.i_lambda2, i*8, 0 );
  733. else if( analysis.i_mb_partition16x8[i] == D_L1_8x8 )
  734. x264_me_refine_qpel_rd( h, &analysis.l1.me16x8[i], analysis.i_lambda2, i*8, 1 );
  735. else if( analysis.i_mb_partition16x8[i] == D_BI_8x8 )
  736. {
  737. i_biweight = h->mb.bipred_weight[analysis.l0.me16x8[i].i_ref][analysis.l1.me16x8[i].i_ref];
  738. x264_me_refine_bidir_rd( h, &analysis.l0.me16x8[i], &analysis.l1.me16x8[i], i_biweight, i*2, analysis.i_lambda2 );
  739. }
  740. }
  741. }
  742. else if( i_partition == D_8x16 )
  743. {
  744. for( int i = 0; i < 2; i++ )
  745. {
  746. h->mb.i_sub_partition[i] = h->mb.i_sub_partition[i+2] = analysis.i_mb_partition8x16[i];
  747. if( analysis.i_mb_partition8x16[i] == D_L0_8x8 )
  748. x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[i], analysis.i_lambda2, i*4, 0 );
  749. else if( analysis.i_mb_partition8x16[i] == D_L1_8x8 )
  750. x264_me_refine_qpel_rd( h, &analysis.l1.me8x16[i], analysis.i_lambda2, i*4, 1 );
  751. else if( analysis.i_mb_partition8x16[i] == D_BI_8x8 )
  752. {
  753. i_biweight = h->mb.bipred_weight[analysis.l0.me8x16[i].i_ref][analysis.l1.me8x16[i].i_ref];
  754. x264_me_refine_bidir_rd( h, &analysis.l0.me8x16[i], &analysis.l1.me8x16[i], i_biweight, i, analysis.i_lambda2 );
  755. }
  756. }
  757. }
  758. else if( i_partition == D_8x8 )
  759. {
  760. for( int i = 0; i < 4; i++ )
  761. {
  762. if( h->mb.i_sub_partition[i] == D_L0_8x8 )
  763. x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i], analysis.i_lambda2, i*4, 0 );
  764. else if( h->mb.i_sub_partition[i] == D_L1_8x8 )
  765. x264_me_refine_qpel_rd( h, &analysis.l1.me8x8[i], analysis.i_lambda2, i*4, 1 );
  766. else if( h->mb.i_sub_partition[i] == D_BI_8x8 )
  767. {
  768. i_biweight = h->mb.bipred_weight[analysis.l0.me8x8[i].i_ref][analysis.l1.me8x8[i].i_ref];
  769. x264_me_refine_bidir_rd( h, &analysis.l0.me8x8[i], &analysis.l1.me8x8[i], i_biweight, i, analysis.i_lambda2 );
  770. }
  771. }
  772. }
  773. }
  774. }
  775. }
  776. x264_analyse_update_cache( h, &analysis ); ///////////////////////
  777. /* In rare cases we can end up qpel-RDing our way back to a larger partition size
  778. * without realizing it. Check for this and account for it if necessary. */
  779. if( analysis.i_mbrd >= 2 )
  780. {
  781. /* Don't bother with bipred or 8x8-and-below, the odds are incredibly low. */
  782. static const uint8_t check_mv_lists[X264_MBTYPE_MAX] = {[P_L0]=1, [B_L0_L0]=1, [B_L1_L1]=2};
  783. int list = check_mv_lists[h->mb.i_type] - 1;
  784. if( list >= 0 && h->mb.i_partition != D_16x16 &&
  785. M32( &h->mb.cache.mv[list][x264_scan8[0]] ) == M32( &h->mb.cache.mv[list][x264_scan8[12]] ) &&
  786. h->mb.cache.ref[list][x264_scan8[0]] == h->mb.cache.ref[list][x264_scan8[12]] )
  787. h->mb.i_partition = D_16x16;
  788. }
  789. if( !analysis.i_mbrd )
  790. x264_mb_analyse_transform( h ); ///////////////////////
  791. if( analysis.i_mbrd == 3 && !IS_SKIP(h->mb.i_type) )
  792. x264_mb_analyse_qp_rd( h, &analysis ); ///////////////////////
  793. h->mb.b_trellis = h->param.analyse.i_trellis;
  794. h->mb.b_noise_reduction = h->mb.b_noise_reduction || (!!h->param.analyse.i_noise_reduction && !IS_INTRA( h->mb.i_type ));
  795. if( !IS_SKIP(h->mb.i_type) && h->mb.i_psy_trellis && h->param.analyse.i_trellis == 1 )
  796. x264_psy_trellis_init( h, 0 );
  797. if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
  798. h->mb.i_skip_intra = 0;
  799. }


        尽管x264_macroblock_analyse()的源代码比较长,但是它的逻辑比较清晰,如下所示:

1)、如果当前是I Slice,调用x264_mb_analyse_intra()进行Intra宏块的帧内预测模式分析。


2)、如果当前是P Slice,则进行下面流程的分析:

    a)、调用x264_macroblock_probe_pskip()分析是否为Skip宏块,如果是的话则不再进行下面分析。

    b)、调用x264_mb_analyse_inter_p16x16()分析P16x16帧间预测的代价。

    c)、调用x264_mb_analyse_inter_p8x8()分析P8x8帧间预测的代价。

    d)、如果P8x8代价值小于P16x16,则依次对48x8的子宏块分割进行判断:

        i、调用x264_mb_analyse_inter_p4x4()分析P4x4帧间预测的代价。

        ii、如果P4x4代价值小于P8x8,则调用 x264_mb_analyse_inter_p8x4()x264_mb_analyse_inter_p4x8()分析P8x4P4x8帧间预测的代价。

    e)、如果P8x8代价值小于P16x16,调用x264_mb_analyse_inter_p16x8()x264_mb_analyse_inter_p8x16()分析P16x8P8x16帧间预测的代价。

    f)、此外还要调用x264_mb_analyse_intra(),检查当前宏块作为Intra宏块编码的代价是否小于作为P宏块编码的代价(P Slice中也允许有Intra宏块)。


3)、如果当前是B Slice,则进行和P Slice类似的处理。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多