x264源代码简单分析:宏块分析(Analysis)部分-帧间宏块(Inter)

来源:互联网 发布:python做数据分析应用 编辑:程序博客网 时间:2024/05/16 12:14

本文转自:http://blog.csdn.net/leixiaohua1020/article/details/45936267  欢迎访问原址!


     本文记录x264的 x264_slice_write()函数中调用的x264_macroblock_analyse()的源代码。x264_macroblock_analyse()对应着x264中的分析模块。分析模块主要完成了下面2个方面的功能:

(1)对于帧内宏块,分析帧内预测模式
(2)对于帧间宏块,进行运动估计,分析帧间预测模式
上一篇文章记录了帧内宏块预测模式的分析,本文继续记录帧间宏块预测模式的分析。

函数调用关系图

宏块分析(Analysis)部分的源代码在整个x264中的位置如下图所示。


单击查看更清晰的图片

宏块分析(Analysis)部分的函数调用关系如下图所示。
 
单击查看更清晰的图片

从图中可以看出,分析模块的x264_macroblock_analyse()调用了如下函数(只列举了几个有代表性的函数):
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宏块帧间预测模式分析。

上一篇文章已经分析了帧内宏块(Intra宏块)的分析函数x264_mb_analyse_intra()。本文重点分析帧间宏块(Inter宏块)的分析函数x264_mb_analyse_inter_p16x16 ()。并简单对比分析x264_mb_analyse_inter_p8x8(),x264_mb_analyse_inter_p16x8()等几个针对不同尺寸帧间宏块的预测函数。



x264_slice_write()

x264_slice_write()是x264项目的核心,它完成了编码了一个Slice的工作。有关该函数的分析可以参考文章《x264源代码简单分析:x264_slice_write()》。


x264_macroblock_analyse()

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

尽管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,则依次对4个8x8的子宏块分割进行判断:
i.调用x264_mb_analyse_inter_p4x4()分析P4x4帧间预测的代价。
ii.如果P4x4代价值小于P8x8,则调用 x264_mb_analyse_inter_p8x4()和x264_mb_analyse_inter_p4x8()分析P8x4和P4x8帧间预测的代价。
e)如果P8x8代价值小于P16x16,调用x264_mb_analyse_inter_p16x8()和x264_mb_analyse_inter_p8x16()分析P16x8和P8x16帧间预测的代价。
f)此外还要调用x264_mb_analyse_intra(),检查当前宏块作为Intra宏块编码的代价是否小于作为P宏块编码的代价(P Slice中也允许有Intra宏块)。
(3)如果当前是B Slice,则进行和P Slice类似的处理。

x264_macroblock_analyse()的流程中出现了多种帧间宏块的划分方式,在这里汇总一下。《H.264标准》中规定,每个16x16的宏块可以划分为16x16,16x8,8x16,8x8四种类型。而如果宏块划分为8x8类型的时候,每个8x8宏块又可以划分为8x8,8x4,4x8,4x4四种小块。它们之间的关系下图所示。
 

上图中这些子宏块都包含了自己的运动矢量和参考帧序号,并且根据这两个信息获得最终的预测数据。总体说来,大的子宏块适合平坦区域,而小的子宏块适合多细节区域。例如下面这张图是一张没有进行运动补偿的残差帧的宏块分割方式图,可以看出平坦区域使用了较大的16x16分割方式,而细节区域使用了相对较小的宏块分割方式。


上一篇文章中已经记录了x264_macroblock_analyse()中Intra宏块的预测模式分析函数x264_mb_analyse_intra();本文继续分析该函数中Inter宏块的预测模式分析的代码。由于Inter宏块的划分模式比较多,每种划分模式都对应这一个函数,因此难以一一分析每种划分模式的帧间预测代码。本文主要以P16x16宏块帧间预测函数264_mb_analyse_inter_p16x16()为例,分析宏块帧间预测方法。


x264_mb_analyse_inter_p16x16()

x264_mb_analyse_inter_p16x16()用于分析P16x16宏块的帧间预测方式。该函数的定义位于encoder\analyse.c,如下所示。
[cpp] view plain copy
  1. /* 
  2.  * 16x16 帧间预测宏块分析 
  3.  * 
  4.  * +--------+--------+ 
  5.  * |                 | 
  6.  * |                 | 
  7.  * |                 | 
  8.  * +        +        + 
  9.  * |                 | 
  10.  * |                 | 
  11.  * |                 | 
  12.  * +--------+--------+ 
  13.  * 
  14.  */  
  15. static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )  
  16. {  
  17.     //运动估计相关的信息  
  18.     //后面的初始化工作主要是对该结构体赋值  
  19.     x264_me_t m;  
  20.     int i_mvc;  
  21.     ALIGNED_4( int16_t mvc[8][2] );  
  22.     int i_halfpel_thresh = INT_MAX;  
  23.     int *p_halfpel_thresh = (a->b_early_terminate && h->mb.pic.i_fref[0]>1) ? &i_halfpel_thresh : NULL;  
  24.   
  25.     /* 16x16 Search on all ref frame */  
  26.     //设定像素分块大小  
  27.     m.i_pixel = PIXEL_16x16;  
  28.     LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );  
  29.   
  30.     a->l0.me16x16.cost = INT_MAX;  
  31.   
  32.     //循环搜索所有的参考帧  
  33.     //i_ref  
  34.     //mb.pic.i_fref[0]存储了参考帧的个数  
  35.     forint i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )  
  36.     {  
  37.         m.i_ref_cost = REF_COST( 0, i_ref );  
  38.         i_halfpel_thresh -= m.i_ref_cost;  
  39.   
  40.         /* search with ref */  
  41.         //加载半像素点的列表  
  42.         //参考列表的4个分量列表,包括yN(整点像素),yH(1/2水平内插),yV(1/2垂直内插), yHV(1/2斜对角内插)  
  43.         LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );  
  44.         LOAD_WPELS( &m, h->mb.pic.p_fref_w[i_ref], 0, i_ref, 0, 0 );  
  45.   
  46.         //获得预测的运动矢量MV(通过取中值)  
  47.         x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );  
  48.   
  49.         if( h->mb.ref_blind_dupe == i_ref )  
  50.         {  
  51.             CP32( m.mv, a->l0.mvc[0][0] );  
  52.             x264_me_refine_qpel_refdupe( h, &m, p_halfpel_thresh );  
  53.         }  
  54.         else  
  55.         {  
  56.             x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );  
  57.             //关键:运动估计(搜索参考帧)  
  58.             x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );  
  59.         }  
  60.   
  61.         /* save mv for predicting neighbors */  
  62.         CP32( h->mb.mvr[0][i_ref][h->mb.i_mb_xy], m.mv );  
  63.         CP32( a->l0.mvc[i_ref][0], m.mv );  
  64.   
  65.         /* early termination 
  66.          * SSD threshold would probably be better than SATD */  
  67.         if( i_ref == 0  
  68.             && a->b_try_skip  
  69.             && m.cost-m.cost_mv < 300*a->i_lambda  
  70.             &&  abs(m.mv[0]-h->mb.cache.pskip_mv[0])  
  71.               + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1  
  72.             && x264_macroblock_probe_pskip( h ) )  
  73.         {  
  74.             h->mb.i_type = P_SKIP;  
  75.             x264_analyse_update_cache( h, a );  
  76.             assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->i_thread_frames == 1 );  
  77.             return;  
  78.         }  
  79.   
  80.         m.cost += m.i_ref_cost;  
  81.         i_halfpel_thresh += m.i_ref_cost;  
  82.   
  83.         if( m.cost < a->l0.me16x16.cost )  
  84.             h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );  
  85.     }  
  86.   
  87.     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );  
  88.     assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->i_thread_frames == 1 );  
  89.   
  90.     h->mb.i_type = P_L0;  
  91.     if( a->i_mbrd )  
  92.     {  
  93.         x264_mb_init_fenc_cache( h, a->i_mbrd >= 2 || h->param.analyse.inter & X264_ANALYSE_PSUB8x8 );  
  94.         if( a->l0.me16x16.i_ref == 0 && M32( a->l0.me16x16.mv ) == M32( h->mb.cache.pskip_mv ) && !a->b_force_intra )  
  95.         {  
  96.             h->mb.i_partition = D_16x16;  
  97.             x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );  
  98.             a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );  
  99.             if( !(h->mb.i_cbp_luma|h->mb.i_cbp_chroma) )  
  100.                 h->mb.i_type = P_SKIP;  
  101.         }  
  102.     }  
  103. }  

从源代码可以看出,x264_mb_analyse_inter_p16x16()首先初始化了x264_me_t结构体相关的信息,然后调用x264_me_search_ref()进行运动估计,最后统计运动估计的开销。其中x264_me_search_ref()完成了运动搜索的流程,相对比较复杂,是帧间预测的重点。在看x264_me_search_ref()之前,简单记录一下运动搜索相关的知识。

运动搜索(运动估计)知识

运动搜索可以分成两种基本类型:
(1)全局搜索算法。该方法是把搜索区域内所有的像素块逐个与当前宏块进行比较,查找具有最小匹配误差的一个像素块为匹配块。这一方法的好处是可以找到最佳的匹配块,坏处是速度太慢。目前全局搜索算法极少使用。

(2)快速搜索算法。该方法按照一定的数学规则进行匹配块的搜索。这一方法的好处是速度快,坏处是可能只能得到次最佳的匹配块。

在X264中包含以下几种运动搜索方法:
(1)菱形搜索算法(DIA)
以搜索起点为中心,采用下图所示的小菱形模板(模板半径为1)搜索。计算各点的匹配误差,得到MBD(最小误差)点。如果MBD点在模板中心,则搜索结束,此时的MBD 点就是最优匹配点,对应的像素块就是最佳匹配块;如果MBD点不在模板中心位置,则以现在MBD点为中心点,继续进行小菱形搜索,直至MBD点落在中心点为止。
 
4x4的像素块采用菱形搜索算法搜索的示意图如下所示。
 
(2)六边形搜索算法(HEX)
该方法采用1个大模板(六边形模板)和2个小模板(小菱形模板和小正方形模板)。具体的搜索步骤如下:
步骤1:以搜索起点为中心,采用图中左边的六边形模板进行搜索。计算区域中心及周围6个点处的匹配误差并比较,如最小MBD 点位于模板中心点,则转至步骤2;否则以上一次的MBD 点作为中心点,以六边形模板为模板进行反复搜索。
步骤2:以上一次的MBD 点为中心点,采用小菱形模板搜索,计算各点的匹配误差,找到MBD 点。然后以MBD点为中心点,采用小正方形模板搜索,得到的MBD点就是最优匹配点。
 
4x4的像素块采用六边形搜索算法搜索的示意图如下所示。
 
(3)非对称十字型多层次六边形格点搜索算法(UMH)
该方法用到了下图所示的多个搜索模板,相对比较复杂,目前还没有仔细研究。记录一下步骤:
步骤0:进行一次小菱形搜索,根据匹配误差值和两个门限值(对于一种尺寸的宏块来说是固定大小的threshold1和threshold2)之间的关系作相应的处理,可能用到中菱形模板或者正八边形模板,也有可能直接跳到步骤1。
步骤1:使用非对称十字模板搜索。“非对称”的原因是一般水平方向运动要比垂直方向运动剧烈,所以将水平方向搜索范围定为W,垂直方向搜索范围定为W/2。
步骤2:使用5x5逐步搜索模板搜索。
步骤3:使用大六边形模板搜索。
步骤4:使用六边形搜索算法找到最优匹配点。
 
(4)连续消除法(ESA、TESA)

该方法是一种全搜索算法,它对搜索区域内的点进行光栅式搜索,逐一计算并比较。

论文《X264的运动估计算法研究》中曾经评测了几种运动搜索算法的性能。文中采用了“Foreman”(运动剧烈)、“Carphone”(中等运动)、“Claire”(运动较小)几个视频作为实验素材,搜索范围为设置为16,实验的结果如下表所示。
 
从结果可以看出,码率不变的前提下,“Dia”、“HEX”、“UMH”、“ESA”编码获得的质量依次提高,速度依次降低。其中快速算法(“Dia”、“HEX”、“UMH”)的编码质量比全搜索算法(“ESA”)低不了太多,但是速度却高了很多倍。



x264_me_search_ref()

x264_me_search_ref()完成了运动搜索的工作。该函数的定义位于encoder\me.c,如下所示。
[cpp] view plain copy
  1. #define BITS_MVD( mx, my )\  
  2.     (p_cost_mvx[(mx)<<2] + p_cost_mvy[(my)<<2])  
  3.   
  4. #define COST_MV( mx, my )\  
  5. do\  
  6. {\  
  7.     int cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE,\  
  8.                    &p_fref_w[(my)*stride+(mx)], stride )\  
  9.              + BITS_MVD(mx,my);\  
  10.     COPY3_IF_LT( bcost, cost, bmx, mx, bmy, my );\  
  11. while(0)  
  12.   
  13. #define COST_MV_HPEL( mx, my, cost )\  
  14. do\  
  15. {\  
  16.     intptr_t stride2 = 16;\  
  17.     pixel *src = h->mc.get_ref( pix, &stride2, m->p_fref, stride, mx, my, bw, bh, &m->weight[0] );\  
  18.     cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE, src, stride2 )\  
  19.          + p_cost_mvx[ mx ] + p_cost_mvy[ my ];\  
  20. while(0)  
  21.   
  22. #define COST_MV_X3_DIR( m0x, m0y, m1x, m1y, m2x, m2y, costs )\  
  23. {\  
  24.     pixel *pix_base = p_fref_w + bmx + bmy*stride;\  
  25.     h->pixf.fpelcmp_x3[i_pixel]( p_fenc,\  
  26.         pix_base + (m0x) + (m0y)*stride,\  
  27.         pix_base + (m1x) + (m1y)*stride,\  
  28.         pix_base + (m2x) + (m2y)*stride,\  
  29.         stride, costs );\  
  30.     (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );\  
  31.     (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );\  
  32.     (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );\  
  33. }  
  34.   
  35. #define COST_MV_X4_DIR( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y, costs )\  
  36. {\  
  37.     pixel *pix_base = p_fref_w + bmx + bmy*stride;\  
  38.     h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\  
  39.         pix_base + (m0x) + (m0y)*stride,\  
  40.         pix_base + (m1x) + (m1y)*stride,\  
  41.         pix_base + (m2x) + (m2y)*stride,\  
  42.         pix_base + (m3x) + (m3y)*stride,\  
  43.         stride, costs );\  
  44.     (costs)[0] += BITS_MVD( bmx+(m0x), bmy+(m0y) );\  
  45.     (costs)[1] += BITS_MVD( bmx+(m1x), bmy+(m1y) );\  
  46.     (costs)[2] += BITS_MVD( bmx+(m2x), bmy+(m2y) );\  
  47.     (costs)[3] += BITS_MVD( bmx+(m3x), bmy+(m3y) );\  
  48. }  
  49.   
  50. #define COST_MV_X4( m0x, m0y, m1x, m1y, m2x, m2y, m3x, m3y )\  
  51. {\  
  52.     pixel *pix_base = p_fref_w + omx + omy*stride;\  
  53.     h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\  
  54.         pix_base + (m0x) + (m0y)*stride,\  
  55.         pix_base + (m1x) + (m1y)*stride,\  
  56.         pix_base + (m2x) + (m2y)*stride,\  
  57.         pix_base + (m3x) + (m3y)*stride,\  
  58.         stride, costs );\  
  59.     costs[0] += BITS_MVD( omx+(m0x), omy+(m0y) );\  
  60.     costs[1] += BITS_MVD( omx+(m1x), omy+(m1y) );\  
  61.     costs[2] += BITS_MVD( omx+(m2x), omy+(m2y) );\  
  62.     costs[3] += BITS_MVD( omx+(m3x), omy+(m3y) );\  
  63.     COPY3_IF_LT( bcost, costs[0], bmx, omx+(m0x), bmy, omy+(m0y) );\  
  64.     COPY3_IF_LT( bcost, costs[1], bmx, omx+(m1x), bmy, omy+(m1y) );\  
  65.     COPY3_IF_LT( bcost, costs[2], bmx, omx+(m2x), bmy, omy+(m2y) );\  
  66.     COPY3_IF_LT( bcost, costs[3], bmx, omx+(m3x), bmy, omy+(m3y) );\  
  67. }  
  68.   
  69. #define COST_MV_X3_ABS( m0x, m0y, m1x, m1y, m2x, m2y )\  
  70. {\  
  71.     h->pixf.fpelcmp_x3[i_pixel]( p_fenc,\  
  72.         p_fref_w + (m0x) + (m0y)*stride,\  
  73.         p_fref_w + (m1x) + (m1y)*stride,\  
  74.         p_fref_w + (m2x) + (m2y)*stride,\  
  75.         stride, costs );\  
  76.     costs[0] += p_cost_mvx[(m0x)<<2]; /* no cost_mvy */\  
  77.     costs[1] += p_cost_mvx[(m1x)<<2];\  
  78.     costs[2] += p_cost_mvx[(m2x)<<2];\  
  79.     COPY3_IF_LT( bcost, costs[0], bmx, m0x, bmy, m0y );\  
  80.     COPY3_IF_LT( bcost, costs[1], bmx, m1x, bmy, m1y );\  
  81.     COPY3_IF_LT( bcost, costs[2], bmx, m2x, bmy, m2y );\  
  82. }  
  83.   
  84. /*  1  */  
  85. /* 101 */  
  86. /*  1  */  
  87. #define DIA1_ITER( mx, my )\  
  88. {\  
  89.     omx = mx; omy = my;\  
  90.     COST_MV_X4( 0,-1, 0,1, -1,0, 1,0 );\  
  91. }  
  92.   
  93. #define CROSS( start, x_max, y_max )\  
  94. {\  
  95.     int i = start;\  
  96.     if( (x_max) <= X264_MIN(mv_x_max-omx, omx-mv_x_min) )\  
  97.         for( ; i < (x_max)-2; i+=4 )\  
  98.             COST_MV_X4( i,0, -i,0, i+2,0, -i-2,0 );\  
  99.     for( ; i < (x_max); i+=2 )\  
  100.     {\  
  101.         if( omx+i <= mv_x_max )\  
  102.             COST_MV( omx+i, omy );\  
  103.         if( omx-i >= mv_x_min )\  
  104.             COST_MV( omx-i, omy );\  
  105.     }\  
  106.     i = start;\  
  107.     if( (y_max) <= X264_MIN(mv_y_max-omy, omy-mv_y_min) )\  
  108.         for( ; i < (y_max)-2; i+=4 )\  
  109.             COST_MV_X4( 0,i, 0,-i, 0,i+2, 0,-i-2 );\  
  110.     for( ; i < (y_max); i+=2 )\  
  111.     {\  
  112.         if( omy+i <= mv_y_max )\  
  113.             COST_MV( omx, omy+i );\  
  114.         if( omy-i >= mv_y_min )\  
  115.             COST_MV( omx, omy-i );\  
  116.     }\  
  117. }  
  118.   
  119. #define FPEL(mv) (((mv)+2)>>2) /* Convert subpel MV to fullpel with rounding... */  
  120. #define SPEL(mv) ((mv)<<2)     /* ... and the reverse. */  
  121. #define SPELx2(mv) (SPEL(mv)&0xFFFCFFFC) /* for two packed MVs */  
  122.   
  123. //关键:运动估计(搜索参考帧)  
  124. void x264_me_search_ref( x264_t *h, x264_me_t *m, int16_t (*mvc)[2], int i_mvc, int *p_halfpel_thresh )  
  125. {  
  126.     const int bw = x264_pixel_size[m->i_pixel].w;  
  127.     const int bh = x264_pixel_size[m->i_pixel].h;  
  128.     const int i_pixel = m->i_pixel;  
  129.     const int stride = m->i_stride[0];  
  130.     int i_me_range = h->param.analyse.i_me_range;  
  131.     int bmx, bmy, bcost = COST_MAX;  
  132.     int bpred_cost = COST_MAX;  
  133.     int omx, omy, pmx, pmy;  
  134.     pixel *p_fenc = m->p_fenc[0];  
  135.     pixel *p_fref_w = m->p_fref_w;  
  136.     ALIGNED_ARRAY_N( pixel, pix,[16*16] );  
  137.     ALIGNED_ARRAY_8( int16_t, mvc_temp,[16],[2] );  
  138.   
  139.     ALIGNED_ARRAY_16( int, costs,[16] );  
  140.   
  141.     int mv_x_min = h->mb.mv_limit_fpel[0][0];  
  142.     int mv_y_min = h->mb.mv_limit_fpel[0][1];  
  143.     int mv_x_max = h->mb.mv_limit_fpel[1][0];  
  144.     int mv_y_max = h->mb.mv_limit_fpel[1][1];  
  145. /* Special version of pack to allow shortcuts in CHECK_MVRANGE */  
  146. #define pack16to32_mask2(mx,my) ((mx<<16)|(my&0x7FFF))  
  147.     uint32_t mv_min = pack16to32_mask2( -mv_x_min, -mv_y_min );  
  148.     uint32_t mv_max = pack16to32_mask2( mv_x_max, mv_y_max )|0x8000;  
  149.     uint32_t pmv, bpred_mv = 0;  
  150.   
  151. #define CHECK_MVRANGE(mx,my) (!(((pack16to32_mask2(mx,my) + mv_min) | (mv_max - pack16to32_mask2(mx,my))) & 0x80004000))  
  152.   
  153.     const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];  
  154.     const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];  
  155.   
  156.     /* Try extra predictors if provided.  If subme >= 3, check subpel predictors, 
  157.      * otherwise round them to fullpel. */  
  158.     if( h->mb.i_subpel_refine >= 3 )//如果精度为1/4  
  159.     {  
  160.         /* Calculate and check the MVP first */  
  161.         int bpred_mx = x264_clip3( m->mvp[0], SPEL(mv_x_min), SPEL(mv_x_max) );  
  162.         int bpred_my = x264_clip3( m->mvp[1], SPEL(mv_y_min), SPEL(mv_y_max) );  
  163.         pmv = pack16to32_mask( bpred_mx, bpred_my );  
  164.         pmx = FPEL( bpred_mx );  
  165.         pmy = FPEL( bpred_my );  
  166.   
  167.         COST_MV_HPEL( bpred_mx, bpred_my, bpred_cost );  
  168.         int pmv_cost = bpred_cost;  
  169.   
  170.         if( i_mvc > 0 )  
  171.         {  
  172.             /* Clip MV candidates and eliminate those equal to zero and pmv. */  
  173.             int valid_mvcs = x264_predictor_clip( mvc_temp+2, mvc, i_mvc, h->mb.mv_limit_fpel, pmv );  
  174.             if( valid_mvcs > 0 )  
  175.             {  
  176.                 int i = 1, cost;  
  177.                 /* We stuff pmv here to branchlessly pick between pmv and the various 
  178.                  * MV candidates. [0] gets skipped in order to maintain alignment for 
  179.                  * x264_predictor_clip. */  
  180.                 M32( mvc_temp[1] ) = pmv;  
  181.                 bpred_cost <<= 4;  
  182.                 do  
  183.                 {  
  184.                     int mx = mvc_temp[i+1][0];  
  185.                     int my = mvc_temp[i+1][1];  
  186.                     COST_MV_HPEL( mx, my, cost );  
  187.                     COPY1_IF_LT( bpred_cost, (cost << 4) + i );  
  188.                 } while( ++i <= valid_mvcs );  
  189.                 bpred_mx = mvc_temp[(bpred_cost&15)+1][0];  
  190.                 bpred_my = mvc_temp[(bpred_cost&15)+1][1];  
  191.                 bpred_cost >>= 4;  
  192.             }  
  193.         }  
  194.   
  195.         /* Round the best predictor back to fullpel and get the cost, since this is where 
  196.          * we'll be starting the fullpel motion search. */  
  197.         //FPEL()宏定义如下  
  198.         //#define FPEL(mv) (((mv)+2)>>2)  
  199.         //即把以1/4像素为基本单位的运动矢量转换为以整像素为基本单位(加2是为了四舍五入)  
  200.         bmx = FPEL( bpred_mx );  
  201.         bmy = FPEL( bpred_my );  
  202.         bpred_mv = pack16to32_mask(bpred_mx, bpred_my);  
  203.         if( bpred_mv&0x00030003 ) /* Only test if the tested predictor is actually subpel... */  
  204.             COST_MV( bmx, bmy );  
  205.         else                          /* Otherwise just copy the cost (we already know it) */  
  206.             bcost = bpred_cost;  
  207.   
  208.         /* Test the zero vector if it hasn't been tested yet. */  
  209.         if( pmv )  
  210.         {  
  211.             if( bmx|bmy ) COST_MV( 0, 0 );  
  212.         }  
  213.         /* If a subpel mv candidate was better than the zero vector, the previous 
  214.          * fullpel check won't have gotten it even if the pmv was zero. So handle 
  215.          * that possibility here. */  
  216.         else  
  217.         {  
  218.             COPY3_IF_LT( bcost, pmv_cost, bmx, 0, bmy, 0 );  
  219.         }  
  220.     }  
  221.     else  
  222.     {  
  223.         /* Calculate and check the fullpel MVP first */  
  224.         //像素点的坐标(bmx,bmy)  
  225.         //FPEL()从四分之一像素MV转换为整像素MV  
  226.         bmx = pmx = x264_clip3( FPEL(m->mvp[0]), mv_x_min, mv_x_max );  
  227.         bmy = pmy = x264_clip3( FPEL(m->mvp[1]), mv_y_min, mv_y_max );  
  228.         pmv = pack16to32_mask( bmx, bmy );  
  229.   
  230.         /* Because we are rounding the predicted motion vector to fullpel, there will be 
  231.          * an extra MV cost in 15 out of 16 cases.  However, when the predicted MV is 
  232.          * chosen as the best predictor, it is often the case that the subpel search will 
  233.          * result in a vector at or next to the predicted motion vector.  Therefore, we omit 
  234.          * the cost of the MV from the rounded MVP to avoid unfairly biasing against use of 
  235.          * the predicted motion vector. 
  236.          * 
  237.          * Disclaimer: this is a post-hoc rationalization for why this hack works. */  
  238.         bcost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE, &p_fref_w[bmy*stride+bmx], stride );  
  239.   
  240.         if( i_mvc > 0 )  
  241.         {  
  242.             /* Like in subme>=3, except we also round the candidates to fullpel. */  
  243.             int valid_mvcs = x264_predictor_roundclip( mvc_temp+2, mvc, i_mvc, h->mb.mv_limit_fpel, pmv );  
  244.             if( valid_mvcs > 0 )  
  245.             {  
  246.                 int i = 1, cost;  
  247.                 M32( mvc_temp[1] ) = pmv;  
  248.                 bcost <<= 4;  
  249.                 do  
  250.                 {  
  251.                     int mx = mvc_temp[i+1][0];  
  252.                     int my = mvc_temp[i+1][1];  
  253.                     cost = h->pixf.fpelcmp[i_pixel]( p_fenc, FENC_STRIDE, &p_fref_w[my*stride+mx], stride ) + BITS_MVD( mx, my );  
  254.                     COPY1_IF_LT( bcost, (cost << 4) + i );  
  255.                 } while( ++i <= valid_mvcs );  
  256.                 bmx = mvc_temp[(bcost&15)+1][0];  
  257.                 bmy = mvc_temp[(bcost&15)+1][1];  
  258.                 bcost >>= 4;  
  259.             }  
  260.         }  
  261.   
  262.         /* Same as above, except the condition is simpler. */  
  263.         if( pmv )  
  264.             COST_MV( 0, 0 );  
  265.     }  
  266.     //不同的运动估计算法作不同的处理  
  267.     switch( h->mb.i_me_method )  
  268.     {  
  269.         //钻石(Diamond)搜索  
  270.         //注意这里是“小钻石”,实际上还有“大钻石”  
  271.         /* 
  272.          *   x 
  273.          * x x x 
  274.          *   x 
  275.          */  
  276.         case X264_ME_DIA:  
  277.         {  
  278.             /* diamond search, radius 1 */  
  279.             bcost <<= 4;  
  280.             //i每次循环减1,  
  281.             //运动搜索的半径  
  282.             int i = i_me_range;  
  283.             //循环  
  284.             do  
  285.             {  
  286.                 //COST_MV_X4_DIR()计算4个点的MV开销  
  287.                 //在这里以bmx,bmy为基点在周围进行其四点的cost计算  
  288.                 //周围4个点为(0,-1),(0,1),(-1,0),(1,0)  
  289.                 //每个点的结果存储于costs[]数组  
  290.                 //  
  291.                 //在这里像素比较函数可能是SAD或者SATD,参考mbcmp_init()函数  
  292.                 //  
  293.                 //COST_MV_X4_DIR( 0,-1, 0,1, -1,0, 1,0, costs )宏展开后代码如下所示  
  294.                 /* 
  295.                  *  { 
  296.                         pixel *pix_base = p_fref_w + bmx + bmy*stride; 
  297.                         //调用像素比较函数 
  298.                         h->pixf.fpelcmp_x4[i_pixel]( p_fenc, 
  299.                             pix_base + (0) + (-1)*stride,   //上 
  300.                             pix_base + (0) + (1)*stride,    //下 
  301.                             pix_base + (-1) + (0)*stride,   //左 
  302.                             pix_base + (1) + (0)*stride,    //右 
  303.                             stride, costs ); 
  304.                         //得到4个点的开销,存储到costs[]数组 
  305.                         (costs)[0] += (p_cost_mvx[(bmx+(0))<<2] + p_cost_mvy[(bmy+(-1))<<2]); 
  306.                         (costs)[1] += (p_cost_mvx[(bmx+(0))<<2] + p_cost_mvy[(bmy+(1))<<2]); 
  307.                         (costs)[2] += (p_cost_mvx[(bmx+(-1))<<2] + p_cost_mvy[(bmy+(0))<<2]); 
  308.                         (costs)[3] += (p_cost_mvx[(bmx+(1))<<2] + p_cost_mvy[(bmy+(0))<<2]); 
  309.                     } 
  310.                  */  
  311.   
  312.                 /* 
  313.                  * 顺序 
  314.                  *   1 
  315.                  * 3 x 4 
  316.                  *   2 
  317.                  */  
  318.                 COST_MV_X4_DIR( 0,-1, 0,1, -1,0, 1,0, costs );  
  319.                 //如果小的话,就拷贝至bcost  
  320.                 //COPY1_IF_LT()宏定义如下  
  321.                 //#define COPY1_IF_LT(x,y)\  
  322.                 //if((y)<(x))\  
  323.                 //      (x)=(y);  
  324.                 //  
  325.                 //这里左移了4位,加上1个数,可以理解为用于记录哪一个点开销小  
  326.                 COPY1_IF_LT( bcost, (costs[0]<<4)+1 ); // 1二进制为0001,单看1-2位,“ 1”,对应“上”像素  
  327.                 COPY1_IF_LT( bcost, (costs[1]<<4)+3 ); // 3二进制为0011,单看1-2位,“-1”,对应“下”像素  
  328.                 COPY1_IF_LT( bcost, (costs[2]<<4)+4 ); // 4二进制为0100,单看3-4位,“ 1”,对应“左”像素  
  329.                 COPY1_IF_LT( bcost, (costs[3]<<4)+12 );//12二进制为1100,单看3-4位,“-1”,对应“右”像素  
  330.                 if( !(bcost&15) )//后4位进行检测,如果后4位是0,就是证明所进行比较的4点开销比原点要大,所以不需要继续搜索了  
  331.                     break;       //提前结束  
  332.                 //注意右移的时候是区分符号位的  
  333.                 //改变bmx,bmy的值-决定了x和y是加1还是减1  
  334.                 bmx -= (bcost<<28)>>30;//注意不等同于除以4。左移28位后,只剩最后4位。右移30位,只剩3-4位  
  335.                 bmy -= (bcost<<30)>>30;//思路同上,只剩1-2位  
  336.                 bcost &= ~15;  
  337.                 //检查运动搜索范围:mv_min和mv_max  
  338.                 //以及i  
  339.             } while( --i && CHECK_MVRANGE(bmx, bmy) ); //检查是否越界  
  340.             //这里右移了4位(之前左移4位)  
  341.             bcost >>= 4;  
  342.             break;  
  343.         }  
  344.         //六边形(Hexagon)搜索  
  345.         /* 
  346.          *    x   x 
  347.          * 
  348.          *  x   x   x 
  349.          * 
  350.          *    x   x 
  351.          */  
  352.         case X264_ME_HEX:  
  353.         {  
  354.     me_hex2:  
  355.             /* hexagon search, radius 2 */  
  356.     #if 0  
  357.             forint i = 0; i < i_me_range/2; i++ )  
  358.             {  
  359.                 omx = bmx; omy = bmy;  
  360.                 COST_MV( omx-2, omy   );  
  361.                 COST_MV( omx-1, omy+2 );  
  362.                 COST_MV( omx+1, omy+2 );  
  363.                 COST_MV( omx+2, omy   );  
  364.                 COST_MV( omx+1, omy-2 );  
  365.                 COST_MV( omx-1, omy-2 );  
  366.                 if( bmx == omx && bmy == omy )  
  367.                     break;  
  368.                 if( !CHECK_MVRANGE(bmx, bmy) )  
  369.                     break;  
  370.             }  
  371.     #else  
  372.             /* equivalent to the above, but eliminates duplicate candidates */  
  373.   
  374.             /* hexagon */  
  375.             //一共计算呈六边形分布的6个点  
  376.             //COST_MV_X3_DIR()计算3个点的MV开销  
  377.             //3个点为(-2,0),(-1,2),(1,2)  
  378.             //开销存入costs[]  
  379.             COST_MV_X3_DIR( -2,0, -1, 2,  1, 2, costs   );  
  380.             //再计算3个点为(2,0),(1,-2),(-1,-2)  
  381.             COST_MV_X3_DIR(  2,0,  1,-2, -1,-2, costs+4 ); /* +4 for 16-byte alignment */  
  382.   
  383.             /* 
  384.              * 顺序 
  385.              *    2   3 
  386.              * 
  387.              *  1   x   4 
  388.              * 
  389.              *    6   5 
  390.              */  
  391.             //这里左移了3位,加上1个数,可以理解为用于记录哪一个点开销小  
  392.             bcost <<= 3;  
  393.             COPY1_IF_LT( bcost, (costs[0]<<3)+2 );  
  394.             COPY1_IF_LT( bcost, (costs[1]<<3)+3 );  
  395.             COPY1_IF_LT( bcost, (costs[2]<<3)+4 );  
  396.             COPY1_IF_LT( bcost, (costs[4]<<3)+5 );  
  397.             COPY1_IF_LT( bcost, (costs[5]<<3)+6 );  
  398.             COPY1_IF_LT( bcost, (costs[6]<<3)+7 );  
  399.   
  400.             if( bcost&7 )  //后3位进行检测,如果后3位是0,就是证明所进行比较的6点开销比原点要大,就跳过这一步  
  401.             {  
  402.                 //int8_t hex2[8][2] = {{-1,-2}, {-2,0}, {-1,2}, {1,2}, {2,0}, {1,-2}, {-1,-2}, {-2,0}};  
  403.   
  404.                 int dir = (bcost&7)-2;  
  405.                 bmx += hex2[dir+1][0];  
  406.                 bmy += hex2[dir+1][1];  
  407.   
  408.                 /* half hexagon, not overlapping the previous iteration */  
  409.                 forint i = (i_me_range>>1) - 1; i > 0 && CHECK_MVRANGE(bmx, bmy); i-- )  
  410.                 {  
  411.                     COST_MV_X3_DIR( hex2[dir+0][0], hex2[dir+0][1],  
  412.                                     hex2[dir+1][0], hex2[dir+1][1],  
  413.                                     hex2[dir+2][0], hex2[dir+2][1],  
  414.                                     costs );  
  415.                     bcost &= ~7;  
  416.                     COPY1_IF_LT( bcost, (costs[0]<<3)+1 );  
  417.                     COPY1_IF_LT( bcost, (costs[1]<<3)+2 );  
  418.                     COPY1_IF_LT( bcost, (costs[2]<<3)+3 );  
  419.                     if( !(bcost&7) )  
  420.                         break;  
  421.                     dir += (bcost&7)-2;  
  422.                     dir = mod6m1[dir+1];  
  423.                     bmx += hex2[dir+1][0];  
  424.                     bmy += hex2[dir+1][1];  
  425.                 }  
  426.             }  
  427.             bcost >>= 3;  
  428.     #endif  
  429.             /* square refine */  
  430.             //正方形细化  
  431.             //六边形搜索之后,再进行正方形细化  
  432.             bcost <<= 4;  
  433.             /* 
  434.              * 分两步,标号如下所示: 
  435.              * 2 1 2 
  436.              * 1 x 1 
  437.              * 2 1 2 
  438.              */  
  439.             COST_MV_X4_DIR(  0,-1,  0,1, -1,0, 1,0, costs );  
  440.             COPY1_IF_LT( bcost, (costs[0]<<4)+1 );  
  441.             COPY1_IF_LT( bcost, (costs[1]<<4)+2 );  
  442.             COPY1_IF_LT( bcost, (costs[2]<<4)+3 );  
  443.             COPY1_IF_LT( bcost, (costs[3]<<4)+4 );  
  444.             COST_MV_X4_DIR( -1,-1, -1,1, 1,-1, 1,1, costs );  
  445.             COPY1_IF_LT( bcost, (costs[0]<<4)+5 );  
  446.             COPY1_IF_LT( bcost, (costs[1]<<4)+6 );  
  447.             COPY1_IF_LT( bcost, (costs[2]<<4)+7 );  
  448.             COPY1_IF_LT( bcost, (costs[3]<<4)+8 );  
  449.             bmx += square1[bcost&15][0];  
  450.             bmy += square1[bcost&15][1];  
  451.             bcost >>= 4;  
  452.             break;  
  453.         }  
  454.         //非对称十字多六边形网格(Uneven Multi-Hex)搜索  
  455.         case X264_ME_UMH:  
  456.         {  
  457.             /* 
  458.              * 主要包含3个步骤 
  459.              * 第1步:进行混合搜索,包括如下: 
  460.              * A,非对称十字搜索。 
  461.              * B,5×5 全搜索。 
  462.              * C,扩展的多层次六边形(六角形)格点搜索。 
  463.              * 第2步:以当前最优点为中心,用六边形(六角形)进行搜索,直至最优点在六边型的中点为止。 
  464.              * 第3步:以当前最优点为中心,用小菱形进行搜索,直至最优点在小菱形的中点为止。 
  465.              */  
  466.             /* Uneven-cross Multi-Hexagon-grid Search 
  467.              * as in JM, except with different early termination */  
  468.   
  469.             static const uint8_t x264_pixel_size_shift[7] = { 0, 1, 1, 2, 3, 3, 4 };  
  470.   
  471.             int ucost1, ucost2;  
  472.             int cross_start = 1;  
  473.   
  474.             /* refine predictors */  
  475.             ucost1 = bcost;  
  476.             DIA1_ITER( pmx, pmy );  
  477.             if( pmx | pmy )  
  478.                 DIA1_ITER( 0, 0 );  
  479.   
  480.             if( i_pixel == PIXEL_4x4 )  
  481.                 goto me_hex2;  
  482.   
  483.             ucost2 = bcost;  
  484.             if( (bmx | bmy) && ((bmx-pmx) | (bmy-pmy)) )  
  485.                 DIA1_ITER( bmx, bmy );  
  486.             if( bcost == ucost2 )  
  487.                 cross_start = 3;  
  488.             omx = bmx; omy = bmy;  
  489.   
  490.             /* early termination */  
  491. #define SAD_THRESH(v) ( bcost < ( v >> x264_pixel_size_shift[i_pixel] ) )  
  492.             if( bcost == ucost2 && SAD_THRESH(2000) )  
  493.             {  
  494.                 COST_MV_X4( 0,-2, -1,-1, 1,-1, -2,0 );  
  495.                 COST_MV_X4( 2, 0, -1, 1, 1, 1,  0,2 );  
  496.                 if( bcost == ucost1 && SAD_THRESH(500) )  
  497.                     break;  
  498.                 if( bcost == ucost2 )  
  499.                 {  
  500.                     int range = (i_me_range>>1) | 1;  
  501.                     CROSS( 3, range, range );  
  502.                     COST_MV_X4( -1,-2, 1,-2, -2,-1, 2,-1 );  
  503.                     COST_MV_X4( -2, 1, 2, 1, -1, 2, 1, 2 );  
  504.                     if( bcost == ucost2 )  
  505.                         break;  
  506.                     cross_start = range + 2;  
  507.                 }  
  508.             }  
  509.   
  510.             /* adaptive search range */  
  511.             if( i_mvc )  
  512.             {  
  513.                 /* range multipliers based on casual inspection of some statistics of 
  514.                  * average distance between current predictor and final mv found by ESA. 
  515.                  * these have not been tuned much by actual encoding. */  
  516.                 static const uint8_t range_mul[4][4] =  
  517.                 {  
  518.                     { 3, 3, 4, 4 },  
  519.                     { 3, 4, 4, 4 },  
  520.                     { 4, 4, 4, 5 },  
  521.                     { 4, 4, 5, 6 },  
  522.                 };  
  523.                 int mvd;  
  524.                 int sad_ctx, mvd_ctx;  
  525.                 int denom = 1;  
  526.   
  527.                 if( i_mvc == 1 )  
  528.                 {  
  529.                     if( i_pixel == PIXEL_16x16 )  
  530.                         /* mvc is probably the same as mvp, so the difference isn't meaningful. 
  531.                          * but prediction usually isn't too bad, so just use medium range */  
  532.                         mvd = 25;  
  533.                     else  
  534.                         mvd = abs( m->mvp[0] - mvc[0][0] )  
  535.                             + abs( m->mvp[1] - mvc[0][1] );  
  536.                 }  
  537.                 else  
  538.                 {  
  539.                     /* calculate the degree of agreement between predictors. */  
  540.                     /* in 16x16, mvc includes all the neighbors used to make mvp, 
  541.                      * so don't count mvp separately. */  
  542.                     denom = i_mvc - 1;  
  543.                     mvd = 0;  
  544.                     if( i_pixel != PIXEL_16x16 )  
  545.                     {  
  546.                         mvd = abs( m->mvp[0] - mvc[0][0] )  
  547.                             + abs( m->mvp[1] - mvc[0][1] );  
  548.                         denom++;  
  549.                     }  
  550.                     mvd += x264_predictor_difference( mvc, i_mvc );  
  551.                 }  
  552.   
  553.                 sad_ctx = SAD_THRESH(1000) ? 0  
  554.                         : SAD_THRESH(2000) ? 1  
  555.                         : SAD_THRESH(4000) ? 2 : 3;  
  556.                 mvd_ctx = mvd < 10*denom ? 0  
  557.                         : mvd < 20*denom ? 1  
  558.                         : mvd < 40*denom ? 2 : 3;  
  559.   
  560.                 i_me_range = i_me_range * range_mul[mvd_ctx][sad_ctx] >> 2;  
  561.             }  
  562.   
  563.             /* FIXME if the above DIA2/OCT2/CROSS found a new mv, it has not updated omx/omy. 
  564.              * we are still centered on the same place as the DIA2. is this desirable? */  
  565.             CROSS( cross_start, i_me_range, i_me_range>>1 );  
  566.   
  567.             COST_MV_X4( -2,-2, -2,2, 2,-2, 2,2 );  
  568.   
  569.             /* hexagon grid */  
  570.             omx = bmx; omy = bmy;  
  571.             const uint16_t *p_cost_omvx = p_cost_mvx + omx*4;  
  572.             const uint16_t *p_cost_omvy = p_cost_mvy + omy*4;  
  573.             int i = 1;  
  574.             do  
  575.             {  
  576.                 static const int8_t hex4[16][2] = {  
  577.                     { 0,-4}, { 0, 4}, {-2,-3}, { 2,-3},  
  578.                     {-4,-2}, { 4,-2}, {-4,-1}, { 4,-1},  
  579.                     {-4, 0}, { 4, 0}, {-4, 1}, { 4, 1},  
  580.                     {-4, 2}, { 4, 2}, {-2, 3}, { 2, 3},  
  581.                 };  
  582.   
  583.                 if( 4*i > X264_MIN4( mv_x_max-omx, omx-mv_x_min,  
  584.                                      mv_y_max-omy, omy-mv_y_min ) )  
  585.                 {  
  586.                     forint j = 0; j < 16; j++ )  
  587.                     {  
  588.                         int mx = omx + hex4[j][0]*i;  
  589.                         int my = omy + hex4[j][1]*i;  
  590.                         if( CHECK_MVRANGE(mx, my) )  
  591.                             COST_MV( mx, my );  
  592.                     }  
  593.                 }  
  594.                 else  
  595.                 {  
  596.                     int dir = 0;  
  597.                     pixel *pix_base = p_fref_w + omx + (omy-4*i)*stride;  
  598.                     int dy = i*stride;  
  599. #define SADS(k,x0,y0,x1,y1,x2,y2,x3,y3)\  
  600.                     h->pixf.fpelcmp_x4[i_pixel]( p_fenc,\  
  601.                             pix_base x0*i+(y0-2*k+4)*dy,\  
  602.                             pix_base x1*i+(y1-2*k+4)*dy,\  
  603.                             pix_base x2*i+(y2-2*k+4)*dy,\  
  604.                             pix_base x3*i+(y3-2*k+4)*dy,\  
  605.                             stride, costs+4*k );\  
  606.                     pix_base += 2*dy;  
  607. #define ADD_MVCOST(k,x,y) costs[k] += p_cost_omvx[x*4*i] + p_cost_omvy[y*4*i]  
  608. #define MIN_MV(k,x,y)     COPY2_IF_LT( bcost, costs[k], dir, x*16+(y&15) )  
  609.                     SADS( 0, +0,-4, +0,+4, -2,-3, +2,-3 );  
  610.                     SADS( 1, -4,-2, +4,-2, -4,-1, +4,-1 );  
  611.                     SADS( 2, -4,+0, +4,+0, -4,+1, +4,+1 );  
  612.                     SADS( 3, -4,+2, +4,+2, -2,+3, +2,+3 );  
  613.                     ADD_MVCOST(  0, 0,-4 );  
  614.                     ADD_MVCOST(  1, 0, 4 );  
  615.                     ADD_MVCOST(  2,-2,-3 );  
  616.                     ADD_MVCOST(  3, 2,-3 );  
  617.                     ADD_MVCOST(  4,-4,-2 );  
  618.                     ADD_MVCOST(  5, 4,-2 );  
  619.                     ADD_MVCOST(  6,-4,-1 );  
  620.                     ADD_MVCOST(  7, 4,-1 );  
  621.                     ADD_MVCOST(  8,-4, 0 );  
  622.                     ADD_MVCOST(  9, 4, 0 );  
  623.                     ADD_MVCOST( 10,-4, 1 );  
  624.                     ADD_MVCOST( 11, 4, 1 );  
  625.                     ADD_MVCOST( 12,-4, 2 );  
  626.                     ADD_MVCOST( 13, 4, 2 );  
  627.                     ADD_MVCOST( 14,-2, 3 );  
  628.                     ADD_MVCOST( 15, 2, 3 );  
  629.                     MIN_MV(  0, 0,-4 );  
  630.                     MIN_MV(  1, 0, 4 );  
  631.                     MIN_MV(  2,-2,-3 );  
  632.                     MIN_MV(  3, 2,-3 );  
  633.                     MIN_MV(  4,-4,-2 );  
  634.                     MIN_MV(  5, 4,-2 );  
  635.                     MIN_MV(  6,-4,-1 );  
  636.                     MIN_MV(  7, 4,-1 );  
  637.                     MIN_MV(  8,-4, 0 );  
  638.                     MIN_MV(  9, 4, 0 );  
  639.                     MIN_MV( 10,-4, 1 );  
  640.                     MIN_MV( 11, 4, 1 );  
  641.                     MIN_MV( 12,-4, 2 );  
  642.                     MIN_MV( 13, 4, 2 );  
  643.                     MIN_MV( 14,-2, 3 );  
  644.                     MIN_MV( 15, 2, 3 );  
  645. #undef SADS  
  646. #undef ADD_MVCOST  
  647. #undef MIN_MV  
  648.                     if(dir)  
  649.                     {  
  650.                         bmx = omx + i*(dir>>4);  
  651.                         bmy = omy + i*((dir<<28)>>28);  
  652.                     }  
  653.                 }  
  654.             } while( ++i <= i_me_range>>2 );  
  655.             if( bmy <= mv_y_max && bmy >= mv_y_min && bmx <= mv_x_max && bmx >= mv_x_min )  
  656.                 goto me_hex2;  
  657.             break;  
  658.         }  
  659.         //穷尽搜索法(Exhaustive),x264已经取消了这种古老的全搜索法,而是采用下面改进的搜索法  
  660.         case X264_ME_ESA:  
  661.         //hadamard全搜索法(Transformed Exhaustive),这个算法和ESA相比主要是在搜索范围上的变化  
  662.         case X264_ME_TESA:  
  663.         {  
  664.             //范围:最小值和最大值  
  665.             const int min_x = X264_MAX( bmx - i_me_range, mv_x_min );  
  666.             const int min_y = X264_MAX( bmy - i_me_range, mv_y_min );  
  667.             const int max_x = X264_MIN( bmx + i_me_range, mv_x_max );  
  668.             const int max_y = X264_MIN( bmy + i_me_range, mv_y_max );  
  669.             /* SEA is fastest in multiples of 4 */  
  670.             const int width = (max_x - min_x + 3) & ~3;  
  671. #if 0  
  672.             /* plain old exhaustive search */  
  673.             forint my = min_y; my <= max_y; my++ )  
  674.                 forint mx = min_x; mx < min_x + width; mx++ )  
  675.                     COST_MV( mx, my );  
  676. #else  
  677.             /* successive elimination by comparing DC before a full SAD, 
  678.              * because sum(abs(diff)) >= abs(diff(sum)). */  
  679.             uint16_t *sums_base = m->integral;  
  680.             ALIGNED_16( static pixel zero[8*FENC_STRIDE] ) = {0};  
  681.             ALIGNED_ARRAY_16( int, enc_dc,[4] );  
  682.             int sad_size = i_pixel <= PIXEL_8x8 ? PIXEL_8x8 : PIXEL_4x4;  
  683.             int delta = x264_pixel_size[sad_size].w;  
  684.             int16_t *xs = h->scratch_buffer;  
  685.             int xn;  
  686.             uint16_t *cost_fpel_mvx = h->cost_mv_fpel[h->mb.i_qp][-m->mvp[0]&3] + (-m->mvp[0]>>2);  
  687.   
  688.             h->pixf.sad_x4[sad_size]( zero, p_fenc, p_fenc+delta,  
  689.                 p_fenc+delta*FENC_STRIDE, p_fenc+delta+delta*FENC_STRIDE,  
  690.                 FENC_STRIDE, enc_dc );  
  691.             if( delta == 4 )  
  692.                 sums_base += stride * (h->fenc->i_lines[0] + PADV*2);  
  693.             if( i_pixel == PIXEL_16x16 || i_pixel == PIXEL_8x16 || i_pixel == PIXEL_4x8 )  
  694.                 delta *= stride;  
  695.             if( i_pixel == PIXEL_8x16 || i_pixel == PIXEL_4x8 )  
  696.                 enc_dc[1] = enc_dc[2];  
  697.   
  698.             if( h->mb.i_me_method == X264_ME_TESA )  
  699.             {  
  700.                 // ADS threshold, then SAD threshold, then keep the best few SADs, then SATD  
  701.                 mvsad_t *mvsads = (mvsad_t *)(xs + ((width+31)&~31) + 4);  
  702.                 int nmvsad = 0, limit;  
  703.                 int sad_thresh = i_me_range <= 16 ? 10 : i_me_range <= 24 ? 11 : 12;  
  704.                 int bsad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref_w+bmy*stride+bmx, stride )  
  705.                          + BITS_MVD( bmx, bmy );  
  706.                 forint my = min_y; my <= max_y; my++ )  
  707.                 {  
  708.                     int i;  
  709.                     int ycost = p_cost_mvy[my<<2];  
  710.                     if( bsad <= ycost )  
  711.                         continue;  
  712.                     bsad -= ycost;  
  713.                     xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta,  
  714.                                                cost_fpel_mvx+min_x, xs, width, bsad * 17 >> 4 );  
  715.                     for( i = 0; i < xn-2; i += 3 )  
  716.                     {  
  717.                         pixel *ref = p_fref_w+min_x+my*stride;  
  718.                         ALIGNED_ARRAY_16( int, sads,[4] ); /* padded to [4] for asm */  
  719.                         h->pixf.sad_x3[i_pixel]( p_fenc, ref+xs[i], ref+xs[i+1], ref+xs[i+2], stride, sads );  
  720.                         forint j = 0; j < 3; j++ )  
  721.                         {  
  722.                             int sad = sads[j] + cost_fpel_mvx[xs[i+j]];  
  723.                             if( sad < bsad*sad_thresh>>3 )  
  724.                             {  
  725.                                 COPY1_IF_LT( bsad, sad );  
  726.                                 mvsads[nmvsad].sad = sad + ycost;  
  727.                                 mvsads[nmvsad].mv[0] = min_x+xs[i+j];  
  728.                                 mvsads[nmvsad].mv[1] = my;  
  729.                                 nmvsad++;  
  730.                             }  
  731.                         }  
  732.                     }  
  733.                     for( ; i < xn; i++ )  
  734.                     {  
  735.                         int mx = min_x+xs[i];  
  736.                         int sad = h->pixf.sad[i_pixel]( p_fenc, FENC_STRIDE, p_fref_w+mx+my*stride, stride )  
  737.                                 + cost_fpel_mvx[xs[i]];  
  738.                         if( sad < bsad*sad_thresh>>3 )  
  739.                         {  
  740.                             COPY1_IF_LT( bsad, sad );  
  741.                             mvsads[nmvsad].sad = sad + ycost;  
  742.                             mvsads[nmvsad].mv[0] = mx;  
  743.                             mvsads[nmvsad].mv[1] = my;  
  744.                             nmvsad++;  
  745.                         }  
  746.                     }  
  747.                     bsad += ycost;  
  748.                 }  
  749.   
  750.                 limit = i_me_range >> 1;  
  751.                 sad_thresh = bsad*sad_thresh>>3;  
  752.                 while( nmvsad > limit*2 && sad_thresh > bsad )  
  753.                 {  
  754.                     int i;  
  755.                     // halve the range if the domain is too large... eh, close enough  
  756.                     sad_thresh = (sad_thresh + bsad) >> 1;  
  757.                     for( i = 0; i < nmvsad && mvsads[i].sad <= sad_thresh; i++ );  
  758.                     forint j = i; j < nmvsad; j++ )  
  759.                     {  
  760.                         uint32_t sad;  
  761.                         if( WORD_SIZE == 8 && sizeof(mvsad_t) == 8 )  
  762.                         {  
  763.                             uint64_t mvsad = M64( &mvsads[i] ) = M64( &mvsads[j] );  
  764. #if WORDS_BIGENDIAN  
  765.                             mvsad >>= 32;  
  766. #endif  
  767.                             sad = mvsad;  
  768.                         }  
  769.                         else  
  770.                         {  
  771.                             sad = mvsads[j].sad;  
  772.                             CP32( mvsads[i].mv, mvsads[j].mv );  
  773.                             mvsads[i].sad = sad;  
  774.                         }  
  775.                         i += (sad - (sad_thresh+1)) >> 31;  
  776.                     }  
  777.                     nmvsad = i;  
  778.                 }  
  779.                 while( nmvsad > limit )  
  780.                 {  
  781.                     int bi = 0;  
  782.                     forint i = 1; i < nmvsad; i++ )  
  783.                         if( mvsads[i].sad > mvsads[bi].sad )  
  784.                             bi = i;  
  785.                     nmvsad--;  
  786.                     ifsizeof( mvsad_t ) == sizeof( uint64_t ) )  
  787.                         CP64( &mvsads[bi], &mvsads[nmvsad] );  
  788.                     else  
  789.                         mvsads[bi] = mvsads[nmvsad];  
  790.                 }  
  791.                 forint i = 0; i < nmvsad; i++ )  
  792.                     COST_MV( mvsads[i].mv[0], mvsads[i].mv[1] );  
  793.             }  
  794.             else  
  795.             {  
  796.                 // just ADS and SAD  
  797.                 forint my = min_y; my <= max_y; my++ )  
  798.                 {  
  799.                     int i;  
  800.                     int ycost = p_cost_mvy[my<<2];  
  801.                     if( bcost <= ycost )  
  802.                         continue;  
  803.                     bcost -= ycost;  
  804.                     xn = h->pixf.ads[i_pixel]( enc_dc, sums_base + min_x + my * stride, delta,  
  805.                                                cost_fpel_mvx+min_x, xs, width, bcost );  
  806.                     for( i = 0; i < xn-2; i += 3 )  
  807.                         COST_MV_X3_ABS( min_x+xs[i],my, min_x+xs[i+1],my, min_x+xs[i+2],my );  
  808.                     bcost += ycost;  
  809.                     for( ; i < xn; i++ )  
  810.                         COST_MV( min_x+xs[i], my );  
  811.                 }  
  812.             }  
  813. #endif  
  814.         }  
  815.         break;  
  816.     }  
  817.     //  
  818.     //后面的代码与子像素精度的运动搜索有关  
  819.     //  
  820.     /* -> qpel mv */  
  821.     uint32_t bmv = pack16to32_mask(bmx,bmy);  
  822.     //用于获得子像素精度的运动矢量的值  
  823.     uint32_t bmv_spel = SPELx2(bmv);  
  824.     if( h->mb.i_subpel_refine < 3 )  
  825.     {  
  826.         m->cost_mv = p_cost_mvx[bmx<<2] + p_cost_mvy[bmy<<2];  
  827.         m->cost = bcost;  
  828.         /* compute the real cost */  
  829.         if( bmv == pmv ) m->cost += m->cost_mv;  
  830.         M32( m->mv ) = bmv_spel;  
  831.     }  
  832.     else  
  833.     {  
  834.         M32(m->mv) = bpred_cost < bcost ? bpred_mv : bmv_spel;  
  835.         m->cost = X264_MIN( bpred_cost, bcost );  
  836.     }  
  837.   
  838.     /* subpel refine */  
  839.     //子像素精度(1/2,1/4)搜索  
  840.     if( h->mb.i_subpel_refine >= 2 )  
  841.     {  
  842.         int hpel = subpel_iterations[h->mb.i_subpel_refine][2];  
  843.         int qpel = subpel_iterations[h->mb.i_subpel_refine][3];  
  844.         refine_subpel( h, m, hpel, qpel, p_halfpel_thresh, 0 );  
  845.     }  
  846. }  
  847. #undef COST_MV  

从源代码可以看出,x264_me_search_ref()的整像素搜索是在一个很长的switch()语句里面完成的,该switch()语句根据配置的的参数进行相应的运动搜索,如下所示。
[cpp] view plain copy
  1. switch( h->mb.i_me_method )  
  2. {  
  3.     case X264_ME_DIA:  
  4.     {  
  5.     //...  
  6.         break;  
  7.     }  
  8.     case X264_ME_HEX:  
  9.     {  
  10.     //...  
  11.         break;  
  12.     }  
  13.     case X264_ME_UMH:  
  14.     {  
  15.     //...  
  16.         break;  
  17.     }  
  18.     case X264_ME_ESA:  
  19.     case X264_ME_TESA:  
  20.     {  
  21.     //...  
  22.         break;  
  23.     }  
  24. }  

在具体的搜索算法中,包含了一些宏例如“COST_MV_X4_DIR()”,“COST_MV_X3_DIR()”用于完成像素比较。上述宏可以一次性完成多个位置的像素块的比较,其中“X3”代表可以1次完成3个位置的像素块的比较;而“X4” 代表可以1次完成4个位置的像素块的比较。在钻石模板搜索的过程中调用1次COST_MV_X4_DIR()完成了比较,而在六边形搜索的过程中调用2次COST_MV_X3_DIR()完成了比较。

在进行完整像素搜索之后,x264_me_search_ref()会继续调用refine_subpel()完成亚像素精度(半像素,1/4像素)的搜索。再看源代码之前,简单记录一下有关亚像素的知识。



亚像素插值知识

基本知识

简单记录一下亚像素插值的知识。《H.264标准》中规定,运动估计为1/4像素精度。因此在H.264编码和解码的过程中,需要将画面中的像素进行插值——简单地说就是把原先的1个像素点拓展成4x4一共16个点。下图显示了H.264编码和解码过程中像素插值情况。可以看出原先的G点的右下方通过插值的方式产生了a、b、c、d等一共16个点。

如图所示,1/4像素内插一般分成两步:
(1)半像素内插。这一步通过6抽头滤波器获得5个半像素点。
(2)线性内插。这一步通过简单的线性内插获得剩余的1/4像素点。
图中半像素内插点为b、m、h、s、j五个点。半像素内插方法是对整像素点进行6 抽头滤波得出,滤波器的权重为(1/32, -5/32, 5/8, 5/8, -5/32, 1/32)。例如b的计算公式为:

b=round( (E - 5F + 20G + 20H - 5I + J ) / 32)

剩下几个半像素点的计算关系如下:
m:由B、D、H、N、S、U计算
h:由A、C、G、M、R、T计算
s:由K、L、M、N、P、Q计算
j:由cc、dd、h、m、ee、ff计算。需要注意j点的运算量比较大,因为cc、dd、ee、ff都需要通过半像素内插方法进行计算。

在获得半像素点之后,就可以通过简单的线性内插获得1/4像素内插点了。1/4像素内插的方式如下图所示。例如图中a点的计算公式如下:

A=round( (G+b)/2 )

在这里有一点需要注意:位于4个角的e、g、p、r四个点并不是通过j点计算计算的,而是通过b、h、s、m四个半像素点计算的。

X264中亚像素的计算方法

X264中,半像素数据是在滤波(Filter)部分的x264_fdec_filter_row()中提前计算出来的,而1/4像素数据则是临时通过半像素数据线性内插得到的。

X264中半像素数据
X264中半像素数据在滤波(Filter)部分的x264_fdec_filter_row()中提前计算出来。经过计算之后,半像素点数据存储于x264_frame_t的filter[3][4]中。其中水平半像素点H存储于filter[][1],垂直半像素点V存储于filter[][2],对角线半像素点C存储于filter[][3],而原本整像素点存储于filter[][0]。下图显示了一个4x4图像块经过半像素内插处理后,得到的半像素与整像素点之间的位置关系。


X264中1/4像素数据
X264中1/4像素数据是临时通过半像素点(包括整像素点)线性内插得到的。下图显示了一个4x4图像块进行1/4像素内插的过程。上面一张图中水平半像素点(存储于filter[][1])和垂直半像素点(存储于filter[][2])线性内插后得到了绿色的1/4像素内插点X。下面一张图中整像素点(存储于filter[][0])和垂直半像素点(存储于filter[][2])线性内插后得到了绿色的1/4像素内插点X。
 


refine_subpel()

refine_subpel()用于进行亚像素的运动搜索。该函数的定义位于encoder\me.c,如下所示。
[cpp] view plain copy
  1. //子像素精度(1/2,1/4)搜索  
  2. //hpel_iters 半像素搜索次数 ,qpel_iters 1/4像素搜索次数  
  3. static void refine_subpel( x264_t *h, x264_me_t *m, int hpel_iters, int qpel_iters, int *p_halfpel_thresh, int b_refine_qpel )  
  4. {  
  5.     const int bw = x264_pixel_size[m->i_pixel].w;  
  6.     const int bh = x264_pixel_size[m->i_pixel].h;  
  7.     const uint16_t *p_cost_mvx = m->p_cost_mv - m->mvp[0];  
  8.     const uint16_t *p_cost_mvy = m->p_cost_mv - m->mvp[1];  
  9.     const int i_pixel = m->i_pixel;  
  10.     const int b_chroma_me = h->mb.b_chroma_me && (i_pixel <= PIXEL_8x8 || CHROMA444);  
  11.     int chromapix = h->luma2chroma_pixel[i_pixel];  
  12.     int chroma_v_shift = CHROMA_V_SHIFT;  
  13.     int mvy_offset = chroma_v_shift & MB_INTERLACED & m->i_ref ? (h->mb.i_mb_y & 1)*4 - 2 : 0;  
  14.   
  15.     ALIGNED_ARRAY_N( pixel, pix,[64*18] ); // really 17x17x2, but round up for alignment  
  16.     ALIGNED_ARRAY_16( int, costs,[4] );  
  17.   
  18.     //做完整像素运动搜索之后预测的运动矢量  
  19.     int bmx = m->mv[0];  
  20.     int bmy = m->mv[1];  
  21.     int bcost = m->cost;  
  22.     int odir = -1, bdir;  
  23.   
  24.     /* halfpel diamond search */  
  25.     //子像素搜索使用钻石法  
  26.     if( hpel_iters )  
  27.     {  
  28.         /* try the subpel component of the predicted mv */  
  29.         if( h->mb.i_subpel_refine < 3 )  
  30.         {  
  31.             int mx = x264_clip3( m->mvp[0], h->mb.mv_min_spel[0]+2, h->mb.mv_max_spel[0]-2 );  
  32.             int my = x264_clip3( m->mvp[1], h->mb.mv_min_spel[1]+2, h->mb.mv_max_spel[1]-2 );  
  33.             if( (mx-bmx)|(my-bmy) )  
  34.                 COST_MV_SAD( mx, my );  
  35.         }  
  36.   
  37.         bcost <<= 6;  
  38.         /* 
  39.          * 半像素的diamond搜索 
  40.          * 数字为src{n}中的n 
  41.          * 
  42.          *         X 
  43.          * 
  44.          *         0 
  45.          * 
  46.          * X   2   X   3   X 
  47.          * 
  48.          *         1 
  49.          * 
  50.          *         X 
  51.          */  
  52.   
  53.         forint i = hpel_iters; i > 0; i-- )  
  54.         {  
  55.             int omx = bmx, omy = bmy;  
  56.             intptr_t stride = 64; // candidates are either all hpel or all qpel, so one stride is enough  
  57.             pixel *src0, *src1, *src2, *src3;  
  58.             //得到 omx,omy周围的半像素4个点的地址  
  59.             //omx和omy以1/4像素为基本单位,+2或者-2取的就是半像素点  
  60.             src0 = h->mc.get_ref( pix,    &stride, m->p_fref, m->i_stride[0], omx, omy-2, bw, bh+1, &m->weight[0] );  
  61.             src2 = h->mc.get_ref( pix+32, &stride, m->p_fref, m->i_stride[0], omx-2, omy, bw+4, bh, &m->weight[0] );  
  62.             //src0下面的点  
  63.             src1 = src0 + stride;//src0为中心点的上方点,src1为中心点的下方点  
  64.             //src2右边的点  
  65.             src3 = src2 + 1;//src2为中心点的左侧点,src3为中心点的右侧点  
  66.             //计算cost  
  67.             //同时计算4个点,结果存入cost[]  
  68.             h->pixf.fpelcmp_x4[i_pixel]( m->p_fenc[0], src0, src1, src2, src3, stride, costs );  
  69.             costs[0] += p_cost_mvx[omx  ] + p_cost_mvy[omy-2];  
  70.             costs[1] += p_cost_mvx[omx  ] + p_cost_mvy[omy+2];  
  71.             costs[2] += p_cost_mvx[omx-2] + p_cost_mvy[omy  ];  
  72.             costs[3] += p_cost_mvx[omx+2] + p_cost_mvy[omy  ];  
  73.             COPY1_IF_LT( bcost, (costs[0]<<6)+2 );  
  74.             COPY1_IF_LT( bcost, (costs[1]<<6)+6 );  
  75.             COPY1_IF_LT( bcost, (costs[2]<<6)+16 );  
  76.             COPY1_IF_LT( bcost, (costs[3]<<6)+48 );  
  77.             if( !(bcost&63) )  
  78.                 break;  
  79.             bmx -= (bcost<<26)>>29;  
  80.             bmy -= (bcost<<29)>>29;  
  81.             bcost &= ~63;  
  82.         }  
  83.         bcost >>= 6;  
  84.     }  
  85.   
  86.     if( !b_refine_qpel && (h->pixf.mbcmp_unaligned[0] != h->pixf.fpelcmp[0] || b_chroma_me) )  
  87.     {  
  88.         bcost = COST_MAX;  
  89.         COST_MV_SATD( bmx, bmy, -1 );  
  90.     }  
  91.   
  92.     /* early termination when examining multiple reference frames */  
  93.     if( p_halfpel_thresh )  
  94.     {  
  95.         if( (bcost*7)>>3 > *p_halfpel_thresh )  
  96.         {  
  97.             m->cost = bcost;  
  98.             m->mv[0] = bmx;  
  99.             m->mv[1] = bmy;  
  100.             // don't need cost_mv  
  101.             return;  
  102.         }  
  103.         else if( bcost < *p_halfpel_thresh )  
  104.             *p_halfpel_thresh = bcost;  
  105.     }  
  106.   
  107.     /* quarterpel diamond search */  
  108.     /* 
  109.      * 1/4像素的搜索 
  110.      * 
  111.      *         X 
  112.      * 
  113.      *         0 
  114.      *     q 
  115.      * X q 2 q X   3   X 
  116.      *     q 
  117.      *         1 
  118.      * 
  119.      *         X 
  120.      */  
  121.     if( h->mb.i_subpel_refine != 1 )  
  122.     {  
  123.         bdir = -1;  
  124.         forint i = qpel_iters; i > 0; i-- )  
  125.         {  
  126.             //判断边界  
  127.             if( bmy <= h->mb.mv_min_spel[1] || bmy >= h->mb.mv_max_spel[1] || bmx <= h->mb.mv_min_spel[0] || bmx >= h->mb.mv_max_spel[0] )  
  128.                 break;  
  129.             odir = bdir;  
  130.             int omx = bmx, omy = bmy;  
  131.             //依然是Diamond搜索  
  132.             COST_MV_SATD( omx, omy - 1, 0 );  
  133.             COST_MV_SATD( omx, omy + 1, 1 );  
  134.             COST_MV_SATD( omx - 1, omy, 2 );  
  135.             COST_MV_SATD( omx + 1, omy, 3 );  
  136.             if( (bmx == omx) & (bmy == omy) )  
  137.                 break;  
  138.         }  
  139.     }  
  140.     /* Special simplified case for subme=1 */  
  141.     //subme=1的特殊算法?据说效果不好  
  142.     else if( bmy > h->mb.mv_min_spel[1] && bmy < h->mb.mv_max_spel[1] && bmx > h->mb.mv_min_spel[0] && bmx < h->mb.mv_max_spel[0] )  
  143.     {  
  144.         int omx = bmx, omy = bmy;  
  145.         /* We have to use mc_luma because all strides must be the same to use fpelcmp_x4 */  
  146.         h->mc.mc_luma( pix   , 64, m->p_fref, m->i_stride[0], omx, omy-1, bw, bh, &m->weight[0] );  
  147.         h->mc.mc_luma( pix+16, 64, m->p_fref, m->i_stride[0], omx, omy+1, bw, bh, &m->weight[0] );  
  148.         h->mc.mc_luma( pix+32, 64, m->p_fref, m->i_stride[0], omx-1, omy, bw, bh, &m->weight[0] );  
  149.         h->mc.mc_luma( pix+48, 64, m->p_fref, m->i_stride[0], omx+1, omy, bw, bh, &m->weight[0] );  
  150.         h->pixf.fpelcmp_x4[i_pixel]( m->p_fenc[0], pix, pix+16, pix+32, pix+48, 64, costs );  
  151.         costs[0] += p_cost_mvx[omx  ] + p_cost_mvy[omy-1];  
  152.         costs[1] += p_cost_mvx[omx  ] + p_cost_mvy[omy+1];  
  153.         costs[2] += p_cost_mvx[omx-1] + p_cost_mvy[omy  ];  
  154.         costs[3] += p_cost_mvx[omx+1] + p_cost_mvy[omy  ];  
  155.         bcost <<= 4;  
  156.         COPY1_IF_LT( bcost, (costs[0]<<4)+1 );  
  157.         COPY1_IF_LT( bcost, (costs[1]<<4)+3 );  
  158.         COPY1_IF_LT( bcost, (costs[2]<<4)+4 );  
  159.         COPY1_IF_LT( bcost, (costs[3]<<4)+12 );  
  160.         bmx -= (bcost<<28)>>30;  
  161.         bmy -= (bcost<<30)>>30;  
  162.         bcost >>= 4;  
  163.     }  
  164.   
  165.     m->cost = bcost;  
  166.     m->mv[0] = bmx;  
  167.     m->mv[1] = bmy;  
  168.     m->cost_mv = p_cost_mvx[bmx] + p_cost_mvy[bmy];  
  169. }  

从源代码可以看出,refine_subpel()首先使用小钻石模板(Diamond)查找当前整像素匹配块周围的4个半像素点的匹配块。获取半像素点数据的时候使用了x264_mc_functions_t中的get_ref()函数(后文进行分析)。获取到的src0、src1、src2、src3分别对应当前整像素点上、下、左、右的半像素点。
在查找到半像素点的最小误差点之后,refine_subpel()继续使用小钻石模板查找当前半像素点周围的4个1/4像素点的匹配块。获取1/4像素点数据的时候同样使用了x264_mc_functions_t中的get_ref()函数。
下图显示了一个4x4图像块的运动搜索过程。图中灰色点为整像素点,黄色点为半像素点,绿色点为1/4像素点,红色箭头代表了一次运动搜索过程,蓝色箭头则代表了运动矢量,虚线边缘块则代表了最后的匹配块。


运动估计相关的源代码

运动估计模块的初始化函数是x264_mc_init()。该函数对x264_mc_functions_t结构体中的函数指针进行了赋值。X264运行的过程中只要调用x264_mc_functions_t的函数指针就可以完成相应的功能。


x264_mc_init()

x264_mc_init()用于初始化运动补偿相关的汇编函数。该函数的定义位于common\mc.c,如下所示。
[cpp] view plain copy
  1. //运动补偿  
  2. void x264_mc_init( int cpu, x264_mc_functions_t *pf, int cpu_independent )  
  3. {  
  4.     //亮度运动补偿  
  5.     pf->mc_luma   = mc_luma;  
  6.     //获得匹配块  
  7.     pf->get_ref   = get_ref;  
  8.   
  9.     pf->mc_chroma = mc_chroma;  
  10.     //求平均  
  11.     pf->avg[PIXEL_16x16]= pixel_avg_16x16;  
  12.     pf->avg[PIXEL_16x8] = pixel_avg_16x8;  
  13.     pf->avg[PIXEL_8x16] = pixel_avg_8x16;  
  14.     pf->avg[PIXEL_8x8]  = pixel_avg_8x8;  
  15.     pf->avg[PIXEL_8x4]  = pixel_avg_8x4;  
  16.     pf->avg[PIXEL_4x16] = pixel_avg_4x16;  
  17.     pf->avg[PIXEL_4x8]  = pixel_avg_4x8;  
  18.     pf->avg[PIXEL_4x4]  = pixel_avg_4x4;  
  19.     pf->avg[PIXEL_4x2]  = pixel_avg_4x2;  
  20.     pf->avg[PIXEL_2x8]  = pixel_avg_2x8;  
  21.     pf->avg[PIXEL_2x4]  = pixel_avg_2x4;  
  22.     pf->avg[PIXEL_2x2]  = pixel_avg_2x2;  
  23.     //加权相关  
  24.     pf->weight    = x264_mc_weight_wtab;  
  25.     pf->offsetadd = x264_mc_weight_wtab;  
  26.     pf->offsetsub = x264_mc_weight_wtab;  
  27.     pf->weight_cache = x264_weight_cache;  
  28.     //赋值-只包含了方形的  
  29.     pf->copy_16x16_unaligned = mc_copy_w16;  
  30.     pf->copy[PIXEL_16x16] = mc_copy_w16;  
  31.     pf->copy[PIXEL_8x8]   = mc_copy_w8;  
  32.     pf->copy[PIXEL_4x4]   = mc_copy_w4;  
  33.   
  34.     pf->store_interleave_chroma       = store_interleave_chroma;  
  35.     pf->load_deinterleave_chroma_fenc = load_deinterleave_chroma_fenc;  
  36.     pf->load_deinterleave_chroma_fdec = load_deinterleave_chroma_fdec;  
  37.     //拷贝像素-不论像素块大小  
  38.     pf->plane_copy = x264_plane_copy_c;  
  39.     pf->plane_copy_interleave = x264_plane_copy_interleave_c;  
  40.     pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_c;  
  41.     pf->plane_copy_deinterleave_rgb = x264_plane_copy_deinterleave_rgb_c;  
  42.     pf->plane_copy_deinterleave_v210 = x264_plane_copy_deinterleave_v210_c;  
  43.     //关键:半像素内插  
  44.     pf->hpel_filter = hpel_filter;  
  45.     //几个空函数  
  46.     pf->prefetch_fenc_420 = prefetch_fenc_null;  
  47.     pf->prefetch_fenc_422 = prefetch_fenc_null;  
  48.     pf->prefetch_ref  = prefetch_ref_null;  
  49.     pf->memcpy_aligned = memcpy;  
  50.     pf->memzero_aligned = memzero_aligned;  
  51.     //降低分辨率-线性内插(不是半像素内插)  
  52.     pf->frame_init_lowres_core = frame_init_lowres_core;  
  53.   
  54.     pf->integral_init4h = integral_init4h;  
  55.     pf->integral_init8h = integral_init8h;  
  56.     pf->integral_init4v = integral_init4v;  
  57.     pf->integral_init8v = integral_init8v;  
  58.   
  59.     pf->mbtree_propagate_cost = mbtree_propagate_cost;  
  60.     pf->mbtree_propagate_list = mbtree_propagate_list;  
  61.     //各种汇编版本  
  62. #if HAVE_MMX  
  63.     x264_mc_init_mmx( cpu, pf );  
  64. #endif  
  65. #if HAVE_ALTIVEC  
  66.     if( cpu&X264_CPU_ALTIVEC )  
  67.         x264_mc_altivec_init( pf );  
  68. #endif  
  69. #if HAVE_ARMV6  
  70.     x264_mc_init_arm( cpu, pf );  
  71. #endif  
  72. #if ARCH_AARCH64  
  73.     x264_mc_init_aarch64( cpu, pf );  
  74. #endif  
  75.   
  76.     if( cpu_independent )  
  77.     {  
  78.         pf->mbtree_propagate_cost = mbtree_propagate_cost;  
  79.         pf->mbtree_propagate_list = mbtree_propagate_list;  
  80.     }  
  81. }  

从源代码可以看出,x264_mc_init()中包含了大量的像素内插、拷贝、求平均的函数。这些函数都是用于在H.264编码过程中进行运动估计和运动补偿的。x264_mc_init()的参数x264_mc_functions_t是一个结构体,其中包含了运动补偿函数相关的函数接口。x264_mc_functions_t的定义如下。
[cpp] view plain copy
  1. typedef struct  
  2. {  
  3.     void (*mc_luma)( pixel *dst, intptr_t i_dst, pixel **src, intptr_t i_src,  
  4.                      int mvx, int mvy, int i_width, int i_height, const x264_weight_t *weight );  
  5.   
  6.     /* may round up the dimensions if they're not a power of 2 */  
  7.     pixel* (*get_ref)( pixel *dst, intptr_t *i_dst, pixel **src, intptr_t i_src,  
  8.                        int mvx, int mvy, int i_width, int i_height, const x264_weight_t *weight );  
  9.   
  10.     /* mc_chroma may write up to 2 bytes of garbage to the right of dst, 
  11.      * so it must be run from left to right. */  
  12.     void (*mc_chroma)( pixel *dstu, pixel *dstv, intptr_t i_dst, pixel *src, intptr_t i_src,  
  13.                        int mvx, int mvy, int i_width, int i_height );  
  14.   
  15.     void (*avg[12])( pixel *dst,  intptr_t dst_stride, pixel *src1, intptr_t src1_stride,  
  16.                      pixel *src2, intptr_t src2_stride, int i_weight );  
  17.   
  18.     /* only 16x16, 8x8, and 4x4 defined */  
  19.     void (*copy[7])( pixel *dst, intptr_t dst_stride, pixel *src, intptr_t src_stride, int i_height );  
  20.     void (*copy_16x16_unaligned)( pixel *dst, intptr_t dst_stride, pixel *src, intptr_t src_stride, int i_height );  
  21.   
  22.     void (*store_interleave_chroma)( pixel *dst, intptr_t i_dst, pixel *srcu, pixel *srcv, int height );  
  23.     void (*load_deinterleave_chroma_fenc)( pixel *dst, pixel *src, intptr_t i_src, int height );  
  24.     void (*load_deinterleave_chroma_fdec)( pixel *dst, pixel *src, intptr_t i_src, int height );  
  25.   
  26.     void (*plane_copy)( pixel *dst, intptr_t i_dst, pixel *src, intptr_t i_src, int w, int h );  
  27.     void (*plane_copy_interleave)( pixel *dst,  intptr_t i_dst, pixel *srcu, intptr_t i_srcu,  
  28.                                    pixel *srcv, intptr_t i_srcv, int w, int h );  
  29.     /* may write up to 15 pixels off the end of each plane */  
  30.     void (*plane_copy_deinterleave)( pixel *dstu, intptr_t i_dstu, pixel *dstv, intptr_t i_dstv,  
  31.                                      pixel *src,  intptr_t i_src, int w, int h );  
  32.     void (*plane_copy_deinterleave_rgb)( pixel *dsta, intptr_t i_dsta, pixel *dstb, intptr_t i_dstb,  
  33.                                          pixel *dstc, intptr_t i_dstc, pixel *src,  intptr_t i_src, int pw, int w, int h );  
  34.     void (*plane_copy_deinterleave_v210)( pixel *dsty, intptr_t i_dsty,  
  35.                                           pixel *dstc, intptr_t i_dstc,  
  36.                                           uint32_t *src, intptr_t i_src, int w, int h );  
  37.     void (*hpel_filter)( pixel *dsth, pixel *dstv, pixel *dstc, pixel *src,  
  38.                          intptr_t i_stride, int i_width, int i_height, int16_t *buf );  
  39.   
  40.     /* prefetch the next few macroblocks of fenc or fdec */  
  41.     void (*prefetch_fenc)    ( pixel *pix_y, intptr_t stride_y, pixel *pix_uv, intptr_t stride_uv, int mb_x );  
  42.     void (*prefetch_fenc_420)( pixel *pix_y, intptr_t stride_y, pixel *pix_uv, intptr_t stride_uv, int mb_x );  
  43.     void (*prefetch_fenc_422)( pixel *pix_y, intptr_t stride_y, pixel *pix_uv, intptr_t stride_uv, int mb_x );  
  44.     /* prefetch the next few macroblocks of a hpel reference frame */  
  45.     void (*prefetch_ref)( pixel *pix, intptr_t stride, int parity );  
  46.   
  47.     void *(*memcpy_aligned)( void *dst, const void *src, size_t n );  
  48.     void (*memzero_aligned)( void *dst, size_t n );  
  49.   
  50.     /* successive elimination prefilter */  
  51.     void (*integral_init4h)( uint16_t *sum, pixel *pix, intptr_t stride );  
  52.     void (*integral_init8h)( uint16_t *sum, pixel *pix, intptr_t stride );  
  53.     void (*integral_init4v)( uint16_t *sum8, uint16_t *sum4, intptr_t stride );  
  54.     void (*integral_init8v)( uint16_t *sum8, intptr_t stride );  
  55.   
  56.     void (*frame_init_lowres_core)( pixel *src0, pixel *dst0, pixel *dsth, pixel *dstv, pixel *dstc,  
  57.                                     intptr_t src_stride, intptr_t dst_stride, int width, int height );  
  58.     weight_fn_t *weight;  
  59.     weight_fn_t *offsetadd;  
  60.     weight_fn_t *offsetsub;  
  61.     void (*weight_cache)( x264_t *, x264_weight_t * );  
  62.   
  63.     void (*mbtree_propagate_cost)( int16_t *dst, uint16_t *propagate_in, uint16_t *intra_costs,  
  64.                                    uint16_t *inter_costs, uint16_t *inv_qscales, float *fps_factor, int len );  
  65.   
  66.     void (*mbtree_propagate_list)( x264_t *h, uint16_t *ref_costs, int16_t (*mvs)[2],  
  67.                                    int16_t *propagate_amount, uint16_t *lowres_costs,  
  68.                                    int bipred_weight, int mb_y, int len, int list );  
  69. } x264_mc_functions_t;  

x264_mc_init()的工作就是对x264_mc_functions_t中的函数指针进行赋值。x264_mc_functions_t的成员变量比较多,难以一一分析。下文主要分析其中最重要的两个函数:半像素内插函数hpel_filter()和获取亚像素数据的函数get_ref()。

hpel_filter()

hpel_filter()用于进行半像素插值。该函数的定义位于common\mc.c,如下所示。
[cpp] view plain copy
  1. //半像素插值公式  
  2. //b= (E - 5F + 20G + 20H - 5I + J)/32  
  3. //              x  
  4. //d取1,水平滤波器;d取stride,垂直滤波器(这里没有除以32)  
  5. #define TAPFILTER(pix, d) ((pix)[x-2*d] + (pix)[x+3*d] - 5*((pix)[x-d] + (pix)[x+2*d]) + 20*((pix)[x] + (pix)[x+d]))  
  6.   
  7. /* 
  8.  * 半像素插值 
  9.  * dsth:水平滤波得到的半像素点(aa,bb,b,s,gg,hh) 
  10.  * dstv:垂直滤波的到的半像素点(cc,dd,h,m,ee,ff) 
  11.  * dstc:“水平+垂直”滤波得到的位于4个像素中间的半像素点(j) 
  12.  * 
  13.  * 半像素插值示意图如下: 
  14.  * 
  15.  *         A aa B 
  16.  * 
  17.  *         C bb D 
  18.  * 
  19.  * E   F   G  b H   I   J 
  20.  * 
  21.  * cc  dd  h  j m  ee  ff 
  22.  * 
  23.  * K   L   M  s N   P   Q 
  24.  * 
  25.  *         R gg S 
  26.  * 
  27.  *         T hh U 
  28.  * 
  29.  * 计算公式如下: 
  30.  * b=round( (E - 5F + 20G + 20H - 5I + J ) / 32) 
  31.  * 
  32.  * 剩下几个半像素点的计算关系如下: 
  33.  * m:由B、D、H、N、S、U计算 
  34.  * h:由A、C、G、M、R、T计算 
  35.  * s:由K、L、M、N、P、Q计算 
  36.  * j:由cc、dd、h、m、ee、ff计算。需要注意j点的运算量比较大,因为cc、dd、ee、ff都需要通过半像素内插方法进行计算。 
  37.  * 
  38.  */  
  39. static void hpel_filter( pixel *dsth, pixel *dstv, pixel *dstc, pixel *src,  
  40.                          intptr_t stride, int width, int height, int16_t *buf )  
  41. {  
  42.     const int pad = (BIT_DEPTH > 9) ? (-10 * PIXEL_MAX) : 0;  
  43.     /* 
  44.      * 几种半像素点之间的位置关系 
  45.      * 
  46.      * X: 像素点 
  47.      * H:水平滤波半像素点 
  48.      * V:垂直滤波半像素点 
  49.      * C: 中间位置半像素点 
  50.      * 
  51.      * X   H   X       X       X 
  52.      * 
  53.      * V   C 
  54.      * 
  55.      * X       X       X       X 
  56.      * 
  57.      * 
  58.      * 
  59.      * X       X       X       X 
  60.      * 
  61.      */  
  62.     //一行一行处理  
  63.     forint y = 0; y < height; y++ )  
  64.     {  
  65.         //一个一个点处理  
  66.         //每个整像素点都对应h,v,c三个半像素点  
  67.         //v  
  68.         forint x = -2; x < width+3; x++ )//(aa,bb,b,s,gg,hh),结果存入buf  
  69.         {  
  70.             //垂直滤波半像素点  
  71.             int v = TAPFILTER(src,stride);  
  72.             dstv[x] = x264_clip_pixel( (v + 16) >> 5 );  
  73.             /* transform v for storage in a 16-bit integer */  
  74.             //这应该是给dstc计算使用的?  
  75.             buf[x+2] = v + pad;  
  76.         }  
  77.         //c  
  78.         forint x = 0; x < width; x++ )  
  79.             dstc[x] = x264_clip_pixel( (TAPFILTER(buf+2,1) - 32*pad + 512) >> 10 );//四个相邻像素中间的半像素点  
  80.         //h  
  81.         forint x = 0; x < width; x++ )  
  82.             dsth[x] = x264_clip_pixel( (TAPFILTER(src,1) + 16) >> 5 );//水平滤波半像素点  
  83.         dsth += stride;  
  84.         dstv += stride;  
  85.         dstc += stride;  
  86.         src += stride;  
  87.     }  
  88. }  

从源代码可以看出,hpel_filter()中包含了一个宏TAPFILTER()用来完成半像素点像素值的计算。在完成半像素插值工作后,dsth中存储的是经过水平插值后的半像素点,dstv中存储的是经过垂直插值后的半像素点,dstc中存储的是位于4个相邻像素点中间位置的半像素点。这三块内存中的点的位置关系如下图所示(灰色的点是整像素点)。
 

get_ref()

get_ref()用于获取亚像素数据。该函数的定义位于common\mc.c,如下所示。
[cpp] view plain copy
  1. /* 
  2.  * hpel_ref0[]记录了亚像素点依赖于哪些点。数组元素共有四个取值:0,1,2,3。这四个值分别代表整数像素,水平半像素,垂直半像素,对角线半像素。 
  3.  * hpel_ref1[]功能是类似的。 
  4.  * 1/4内插点依赖于2个半像素点,所以才存在这2个数组 
  5.  * 
  6.  * 注意对最下1行像素和最右1行像素是需要特殊处理的 
  7.  * 
  8.  * hpel_ref0[qpel_idx]表示了第1次半像素内插使用的滤波器。示意如下(矩阵4个角代表4个整像素点) 
  9.  * 
  10.  * 0 1 1 1 
  11.  * 0 1 1 1 
  12.  * 2 3 3 3 
  13.  * 0 1 1 1 
  14.  * 
  15.  * hpel_ref1[qpel_idx]表示了第2次半像素内插使用的滤波器(只有1/4内插点才需要)。示意如下(矩阵4个角代表4个整像素点) 
  16.  * 0 0 0 0 
  17.  * 2 2 3 2 
  18.  * 2 2 3 2 
  19.  * 2 2 3 2 
  20.  * 
  21.  * 例如 
  22.  * qpel_idx=5的时候 
  23.  * hpel_ref0[5]=1,需要进行水平半像素滤波 
  24.  * hpel_ref1[5]=2,需要进行垂直半像素滤波 
  25.  * 顺序如下(X代表像素点,数字代表顺序) 
  26.  * X   1   X 
  27.  *   3 
  28.  * 2 
  29.  * 
  30.  * X       X 
  31.  * 
  32.  * qpel_idx=1的时候 
  33.  * hpel_ref0[5]=1,需要进行水平半像素滤波 
  34.  * hpel_ref1[5]=0,即直接使用整像素点 
  35.  * 顺序如下(X代表像素点,数字代表顺序) 
  36.  * 2 3 1   X 
  37.  * 
  38.  * 
  39.  * 
  40.  * X       X 
  41.  * 
  42.  * qpel_idx=4的时候 
  43.  * hpel_ref0[5]=0,即直接使用整像素点 
  44.  * hpel_ref1[5]=2,需要进行垂直半像素滤波 
  45.  * 顺序如下(X代表像素点,数字代表顺序) 
  46.  * 1       X 
  47.  * 3 
  48.  * 2 
  49.  * 
  50.  * X       X 
  51.  */  
  52. static const uint8_t hpel_ref0[16] = {0,1,1,1,0,1,1,1,2,3,3,3,0,1,1,1};  
  53. static const uint8_t hpel_ref1[16] = {0,0,0,0,2,2,3,2,2,2,3,2,2,2,3,2};  
  54. //  
  55. //获取运动矢量中亚像素的部分的数据  
  56. //可以是半像素数据或者1/4像素数据  
  57. static pixel *get_ref( pixel *dst,   intptr_t *i_dst_stride,  
  58.                        pixel *src[4], intptr_t i_src_stride,  
  59.                        int mvx, int mvy,  
  60.                        int i_width, int i_height, const x264_weight_t *weight )  
  61. {  
  62.     /* 
  63.      * qpel_idx为hpel_ref0[],hpel_ref1[]的索引值 
  64.      * 
  65.      * 运动矢量(mvy,mvx)位置和qpel_idx对应关系如下 
  66.      *  0pixel |   0p   | 1/4p   | 1/2p   | 3/4p   | 1pixel | 
  67.      * --------+--------+--------+--------+--------+--------+ 
  68.      *      0p | 0<<2+0 | 0<<2+1 | 0<<2+2 | 0<<2+3 |        | 
  69.      * --------+--------+--------+--------+--------+--------+ 
  70.      *    1/4p | 1<<2+0 | 1<<2+1 | 1<<2+2 | 1<<2+3 |        | 
  71.      * --------+--------+--------+--------+--------+--------+ 
  72.      *    1/2p | 2<<2+0 | 2<<2+1 | 2<<2+2 | 2<<2+3 |        | 
  73.      * --------+--------+--------+--------+--------+--------+ 
  74.      *    3/4p | 3<<2+0 | 3<<2+1 | 3<<2+2 | 3<<2+3 |        | 
  75.      * --------+--------+--------+--------+--------+--------+ 
  76.      *  1pixel | 
  77.      * --------+ 
  78.      * 计算出来后 
  79.      *  0pixel |   0p   | 1/4p   | 1/2p   | 3/4p   | 1pixel | 
  80.      * --------+--------+--------+--------+--------+--------+ 
  81.      *      0p |      0 |      1 |      2 |      3 |        | 
  82.      * --------+--------+--------+--------+--------+--------+ 
  83.      *    1/4p |      4 |      5 |      6 |      7 |        | 
  84.      * --------+--------+--------+--------+--------+--------+ 
  85.      *    1/2p |      8 |      9 |     10 |     11 |        | 
  86.      * --------+--------+--------+--------+--------+--------+ 
  87.      *    3/4p |     12 |     13 |     14 |     15 |        | 
  88.      * --------+--------+--------+--------+--------+--------+ 
  89.      *  1pixel | 
  90.      * --------+ 
  91.      * 
  92.      */  
  93.     int qpel_idx = ((mvy&3)<<2) + (mvx&3);  
  94.     //offset是匹配块相对当前宏块的整数偏移量。  
  95.     int offset = (mvy>>2)*i_src_stride + (mvx>>2);  
  96.   
  97.     //src[4]中有4个分量,分别代表:整像素点Full,水平半像素点H,垂直半像素点V,对角线半像素点C的取值(几种半像素点的值已经提前计算出来,而1/4像素点的值则是临时计算)  
  98.     //注意上述几种半像素点是按照“分量”的方式存储的  
  99.   
  100.     //src1[]为选择后的半像素数据  
  101.     //选择了Full,H,V,C几种“分量”中的1种  
  102.     pixel *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;  
  103.     //qpel_idx & 5,5是0101, 代表qpel_idx最后1位(对应x分量)为1或者倒数第3位为1(对应y分量)。  
  104.     //即x或者y中有1/4或者3/4像素点(此时需要1/4像素内插)。  
  105.     //只有需要1/4内插的点才会qpel_idx & 5!=0。这时候需要通过线性内插获得1/4像素点的值  
  106.     if( qpel_idx & 5 ) /* qpel interpolation needed */  
  107.     {  
  108.         //src2[]为用于内插的数据另一组数据  
  109.         pixel *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);  
  110.         //进行1/4像素线性内插  
  111.         pixel_avg( dst, *i_dst_stride, src1, i_src_stride,  
  112.                    src2, i_src_stride, i_width, i_height );  
  113.         if( weight->weightfn )  
  114.             mc_weight( dst, *i_dst_stride, dst, *i_dst_stride, weight, i_width, i_height );  
  115.         return dst;  
  116.     }  
  117.     else if( weight->weightfn )  
  118.     {  
  119.         mc_weight( dst, *i_dst_stride, src1, i_src_stride, weight, i_width, i_height );  
  120.         return dst;  
  121.     }  
  122.     else  
  123.     {  
  124.         //只需要半像素滤波  
  125.         *i_dst_stride = i_src_stride;  
  126.         return src1;  
  127.     }  
  128. }  

get_ref()虽然代码简短,但是却不好理解。函数首先取出输入运动矢量亚像素部分(后两位数据),经过计算后赋值给qpel_idx。换句话说qpel_idx记录了运动矢量在亚像素单位上指向的位置,它的取值和像素位置之间的关系如下表所示。

x point

0p

1/4p

1/2p

3/4p

x+1 point

0p

0

1

2

3

 

1/4p

4

5

6

7

 

1/2p

8

9

10

11

 

3/4p

12

13

14

15

 

x+stride point

 

 

 

 

 


接着get_ref()根据qpel_idx从输入图像数据src[4]中取数据。src[4]中[0]存储是整像素数据,[1]存储是水平半像素数据,[2]存储是垂直半像素数据,[3]存储是对角线半像素数据。在取数据的过程中涉及到两个数组hpel_ref0[16]和hpel_ref1[16],这两个数组记录了相应qpel_idx位置应该从哪个半像素点数组中取数据。例如qpel_idx取值为8的时候,应该从垂直半像素数组中取值,因此hpel_ref0[8]=2;而qpel_idx取值为2的时候,应该从水平半像素数组中取值,因此hpel_ref0[2]=1。如果仅仅取半像素点的的话,使用hpel_ref0[16]就足够了,但是如果想要取1/4像素点,就必须使用hpel_ref1[16]。这是因为1/4像素点需要通过2个半像素点线性内插获得,所以hpel_ref1[16]记录了线性内插需要的另一个点是哪个半像素点。例如qpel_idx取值为5的时候,通过垂直半像素点和水平半像素点内插获得该1/4像素点,因此hpel_ref0[5]=1,而hpel_ref1[5]=2;再例如qpel_idx取值为4的时候,通过整像素点和垂直半像素点内插获得该1/4像素点,因此hpel_ref0[4]=0,而hpel_ref1[4]=2。
get_ref()函数通过“qpel_idx & 5”来断定当前运动矢量是否是1/4像素内插点,如果需要的话才会根据hpel_ref1[]加载另一个半像素点的数据并且调用pixel_avg()函数通过线性内插的方式获取该内插点。

下图演示了hpel_ref0[16]和hpel_ref1[16]在获取亚像素数据时候的作用。图中灰色点代表整像素点,黄色点代表半像素点,绿色点代表1/4像素点;左边是一个4x4图像块,其中蓝色箭头标记了1/4像素点需要的两个半像素点(也可能是整像素点);右上方的图将两个像素点之间的图像放大,并且将1/4像素点需要的两个半像素点以数字的方式表示出来;右下方则是将右上方的数字拆开成了两个矩阵,即对应的是hpel_ref0[16]和hpel_ref1[16]。


 

pixel_avg_wxh()

get_ref()中求1/4像素点的时候调用了一个线性内插函数pixel_avg_wxh()。该函数的定义如下。
[cpp] view plain copy
  1. //像素求平均值  
  2. //在这里用于1/4像素内插  
  3. static inline void pixel_avg( pixel *dst,  intptr_t i_dst_stride,  
  4.                               pixel *src1, intptr_t i_src1_stride,  
  5.                               pixel *src2, intptr_t i_src2_stride, int i_width, int i_height )  
  6. {  
  7.     forint y = 0; y < i_height; y++ )  
  8.     {  
  9.         forint x = 0; x < i_width; x++ )  
  10.             dst[x] = ( src1[x] + src2[x] + 1 ) >> 1;  
  11.         dst  += i_dst_stride;  
  12.         src1 += i_src1_stride;  
  13.         src2 += i_src2_stride;  
  14.     }  
  15. }  
可以看出pixel_avg_wxh()完成了一个简单的求平均值的工作。

至此有关X264帧间预测方面的源代码就基本分析完毕了。前文中以P16x16宏块为例分析的帧间预测的源代码,作为对比下面再看一下P8x8、P16x8、P8x16宏块帧间预测的源代码。实际上这几种宏块的帧间预测的方式是类似的,都调用了x264_me_search_ref()完成了运动搜索的过程,它们的不同主要在于处理的图像块尺寸的不同。


其他划分模式的帧间预测源代码

P8x8宏块帧间预测函数为x264_mb_analyse_inter_p8x8();P16x8 宏块帧间预测函数为x264_mb_analyse_inter_p16x8();P8x16宏块帧间预测函数为x264_mb_analyse_inter_p8x16()。下面简单扫一眼它们的源代码。

x264_mb_analyse_inter_p8x8()

x264_mb_analyse_inter_p8x8()用于分析P8x8宏块的帧间预测模式,该函数的定义如下。
[cpp] view plain copy
  1. /* 
  2.  * 8x8帧间预测宏块分析 
  3.  * +--------+ 
  4.  * |        | 
  5.  * |        | 
  6.  * |        | 
  7.  * +--------+ 
  8.  */  
  9. static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )  
  10. {  
  11.     /* Duplicate refs are rarely useful in p8x8 due to the high cost of the 
  12.      * reference frame flags.  Thus, if we're not doing mixedrefs, just 
  13.      * don't bother analysing the dupes. */  
  14.     const int i_ref = h->mb.ref_blind_dupe == a->l0.me16x16.i_ref ? 0 : a->l0.me16x16.i_ref;  
  15.     const int i_ref_cost = h->param.b_cabac || i_ref ? REF_COST( 0, i_ref ) : 0;  
  16.     pixel **p_fenc = h->mb.pic.p_fenc;  
  17.     int i_mvc;  
  18.     int16_t (*mvc)[2] = a->l0.mvc[i_ref];  
  19.   
  20.     /* XXX Needed for x264_mb_predict_mv */  
  21.     h->mb.i_partition = D_8x8;  
  22.   
  23.     i_mvc = 1;  
  24.     CP32( mvc[0], a->l0.me16x16.mv );  
  25.     //处理4个8x8块  
  26.     forint i = 0; i < 4; i++ )  
  27.     {  
  28.         x264_me_t *m = &a->l0.me8x8[i];  
  29.         int x8 = i&1;  
  30.         int y8 = i>>1;  
  31.         //设定像素分块大小  
  32.         m->i_pixel = PIXEL_8x8;  
  33.         m->i_ref_cost = i_ref_cost;  
  34.   
  35.         LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );  
  36.         LOAD_HPELS( m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );  
  37.         LOAD_WPELS( m, h->mb.pic.p_fref_w[i_ref], 0, i_ref, 8*x8, 8*y8 );  
  38.   
  39.         x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );  
  40.         //调用x264_me_search_ref()  
  41.         //进行运动估计  
  42.         x264_me_search( h, m, mvc, i_mvc );  
  43.   
  44.         x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, m->mv );  
  45.   
  46.         CP32( mvc[i_mvc], m->mv );  
  47.         i_mvc++;  
  48.   
  49.         a->i_satd8x8[0][i] = m->cost - m->cost_mv;  
  50.   
  51.         /* mb type cost */  
  52.         m->cost += i_ref_cost;  
  53.         if( !h->param.b_cabac || (h->param.analyse.inter & X264_ANALYSE_PSUB8x8) )  
  54.             m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];  
  55.     }  
  56.     //保存开销。4个8x8块开销累加  
  57.     a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +  
  58.                       a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;  
  59.     /* theoretically this should include 4*ref_cost, 
  60.      * but 3 seems a better approximation of cabac. */  
  61.     if( h->param.b_cabac )  
  62.         a->l0.i_cost8x8 -= i_ref_cost;  
  63.     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =  
  64.     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;  
  65. }  

从源代码可以看出,x264_mb_analyse_inter_p8x8()中包含一个4次的for()循环,用于分别处理4个8x8的块。在函数的结尾将4个8x8块的开销累加起来作为该宏块的开销。

x264_mb_analyse_inter_p16x8()

x264_mb_analyse_inter_p16x8()用于分析P16x8宏块的帧间预测模式,该函数的定义如下。
[cpp] view plain copy
  1. /* 
  2.  * 16x8 宏块划分 
  3.  * 
  4.  * +--------+--------+ 
  5.  * |        |        | 
  6.  * |        |        | 
  7.  * |        |        | 
  8.  * +--------+--------+ 
  9.  * 
  10.  */  
  11. static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a, int i_best_satd )  
  12. {  
  13.     x264_me_t m;  
  14.     pixel **p_fenc = h->mb.pic.p_fenc;  
  15.     ALIGNED_4( int16_t mvc[3][2] );  
  16.   
  17.     /* XXX Needed for x264_mb_predict_mv */  
  18.     h->mb.i_partition = D_16x8;  
  19.     //轮流处理上下2个块  
  20.     forint i = 0; i < 2; i++ )  
  21.     {  
  22.         x264_me_t *l0m = &a->l0.me16x8[i];  
  23.         const int minref = X264_MIN( a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref );  
  24.         const int maxref = X264_MAX( a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref );  
  25.         const int ref8[2] = { minref, maxref };  
  26.         const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;  
  27.   
  28.         m.i_pixel = PIXEL_16x8;  
  29.   
  30.         LOAD_FENC( &m, p_fenc, 0, 8*i );  
  31.         l0m->cost = INT_MAX;  
  32.         forint j = 0; j < i_ref8s; j++ )  
  33.         {  
  34.             const int i_ref = ref8[j];  
  35.             m.i_ref_cost = REF_COST( 0, i_ref );  
  36.   
  37.             /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */  
  38.             CP32( mvc[0], a->l0.mvc[i_ref][0] );  
  39.             CP32( mvc[1], a->l0.mvc[i_ref][2*i+1] );  
  40.             CP32( mvc[2], a->l0.mvc[i_ref][2*i+2] );  
  41.   
  42.             LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );  
  43.             LOAD_WPELS( &m, h->mb.pic.p_fref_w[i_ref], 0, i_ref, 0, 8*i );  
  44.   
  45.             x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );  
  46.             x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );  
  47.             /* We can only take this shortcut if the first search was performed on ref0. */  
  48.             if( h->mb.ref_blind_dupe == i_ref && !ref8[0] )  
  49.             {  
  50.                 /* We can just leave the MV from the previous ref search. */  
  51.                 x264_me_refine_qpel_refdupe( h, &m, NULL );  
  52.             }  
  53.             else  
  54.                 x264_me_search( h, &m, mvc, 3 );//运动搜索  
  55.   
  56.             m.cost += m.i_ref_cost;  
  57.   
  58.             if( m.cost < l0m->cost )  
  59.                 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );  
  60.         }  
  61.   
  62.         /* Early termination based on the current SATD score of partition[0] 
  63.            plus the estimated SATD score of partition[1] */  
  64.         if( a->b_early_terminate && (!i && l0m->cost + a->i_cost_est16x8[1] > i_best_satd * (4 + !!a->i_mbrd) / 4) )  
  65.         {  
  66.             a->l0.i_cost16x8 = COST_MAX;  
  67.             return;  
  68.         }  
  69.   
  70.         x264_macroblock_cache_mv_ptr( h, 0, 2*i, 4, 2, 0, l0m->mv );  
  71.         x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );  
  72.     }  
  73.     //2个块的开销相加  
  74.     a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;  
  75. }  

从源代码可以看出,x264_mb_analyse_inter_p16x8 ()中包含一个2次的for()循环,用于分别处理2个16x8的块。在函数的结尾将2个16x8块的开销累加起来作为该宏块的开销。

x264_mb_analyse_inter_p8x16()

x264_mb_analyse_inter_p8x16()用于分析P8x16宏块的帧间预测模式,该函数的定义如下。
[cpp] view plain copy
  1. /* 
  2.  * 8x16 宏块划分 
  3.  * 
  4.  * +--------+ 
  5.  * |        | 
  6.  * |        | 
  7.  * |        | 
  8.  * +--------+ 
  9.  * |        | 
  10.  * |        | 
  11.  * |        | 
  12.  * +--------+ 
  13.  * 
  14.  */  
  15. static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a, int i_best_satd )  
  16. {  
  17.     x264_me_t m;  
  18.     pixel **p_fenc = h->mb.pic.p_fenc;  
  19.     ALIGNED_4( int16_t mvc[3][2] );  
  20.   
  21.     /* XXX Needed for x264_mb_predict_mv */  
  22.     h->mb.i_partition = D_8x16;  
  23.     //轮流处理左右2个块  
  24.     forint i = 0; i < 2; i++ )  
  25.     {  
  26.         x264_me_t *l0m = &a->l0.me8x16[i];  
  27.         const int minref = X264_MIN( a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref );  
  28.         const int maxref = X264_MAX( a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref );  
  29.         const int ref8[2] = { minref, maxref };  
  30.         const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;  
  31.   
  32.         m.i_pixel = PIXEL_8x16;  
  33.   
  34.         LOAD_FENC( &m, p_fenc, 8*i, 0 );  
  35.         l0m->cost = INT_MAX;  
  36.         forint j = 0; j < i_ref8s; j++ )  
  37.         {  
  38.             const int i_ref = ref8[j];  
  39.             m.i_ref_cost = REF_COST( 0, i_ref );  
  40.   
  41.             CP32( mvc[0], a->l0.mvc[i_ref][0] );  
  42.             CP32( mvc[1], a->l0.mvc[i_ref][i+1] );  
  43.             CP32( mvc[2], a->l0.mvc[i_ref][i+3] );  
  44.   
  45.             LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );  
  46.             LOAD_WPELS( &m, h->mb.pic.p_fref_w[i_ref], 0, i_ref, 8*i, 0 );  
  47.   
  48.             x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );  
  49.             x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );  
  50.             /* We can only take this shortcut if the first search was performed on ref0. */  
  51.             if( h->mb.ref_blind_dupe == i_ref && !ref8[0] )  
  52.             {  
  53.                 /* We can just leave the MV from the previous ref search. */  
  54.                 x264_me_refine_qpel_refdupe( h, &m, NULL );  
  55.             }  
  56.             else  
  57.                 x264_me_search( h, &m, mvc, 3 );  
  58.   
  59.             m.cost += m.i_ref_cost;  
  60.   
  61.             if( m.cost < l0m->cost )  
  62.                 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );  
  63.         }  
  64.   
  65.         /* Early termination based on the current SATD score of partition[0] 
  66.            plus the estimated SATD score of partition[1] */  
  67.         if( a->b_early_terminate && (!i && l0m->cost + a->i_cost_est8x16[1] > i_best_satd * (4 + !!a->i_mbrd) / 4) )  
  68.         {  
  69.             a->l0.i_cost8x16 = COST_MAX;  
  70.             return;  
  71.         }  
  72.   
  73.         x264_macroblock_cache_mv_ptr( h, 2*i, 0, 2, 4, 0, l0m->mv );  
  74.         x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );  
  75.     }  
  76.     //2个块的开销相加  
  77.     a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;  
  78. }  

从源代码可以看出,x264_mb_analyse_inter_p8x16 ()中包含一个2次的for()循环,用于分别处理2个8x16的块。在函数的结尾将2个8x16块的开销累加起来作为该宏块的开销。
阅读全文
0 0
原创粉丝点击