analyse.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:99k
源码类别:

Audio

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  * analyse.c: h264 encoder library
  3.  *****************************************************************************
  4.  * Copyright (C) 2003-2008 x264 project
  5.  *
  6.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  7.  *          Loren Merritt <lorenm@u.washington.edu>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. #include <math.h>
  24. #include <limits.h>
  25. #ifndef _MSC_VER
  26. #include <unistd.h>
  27. #endif
  28. #include "common/common.h"
  29. #include "macroblock.h"
  30. #include "me.h"
  31. #include "ratecontrol.h"
  32. #include "analyse.h"
  33. #include "rdo.c"
  34. typedef struct
  35. {
  36.     /* 16x16 */
  37.     int i_ref;
  38.     int       i_rd16x16;
  39.     x264_me_t me16x16;
  40.     /* 8x8 */
  41.     int       i_cost8x8;
  42.     /* [ref][0] is 16x16 mv, [ref][1..4] are 8x8 mv from partition [0..3] */
  43.     DECLARE_ALIGNED_4( int16_t mvc[32][5][2] );
  44.     x264_me_t me8x8[4];
  45.     /* Sub 4x4 */
  46.     int       i_cost4x4[4]; /* cost per 8x8 partition */
  47.     x264_me_t me4x4[4][4];
  48.     /* Sub 8x4 */
  49.     int       i_cost8x4[4]; /* cost per 8x8 partition */
  50.     x264_me_t me8x4[4][2];
  51.     /* Sub 4x8 */
  52.     int       i_cost4x8[4]; /* cost per 8x8 partition */
  53.     x264_me_t me4x8[4][2];
  54.     /* 16x8 */
  55.     int       i_cost16x8;
  56.     x264_me_t me16x8[2];
  57.     /* 8x16 */
  58.     int       i_cost8x16;
  59.     x264_me_t me8x16[2];
  60. } x264_mb_analysis_list_t;
  61. typedef struct
  62. {
  63.     /* conduct the analysis using this lamda and QP */
  64.     int i_lambda;
  65.     int i_lambda2;
  66.     int i_qp;
  67.     int16_t *p_cost_mv;
  68.     int b_mbrd;
  69.     /* I: Intra part */
  70.     /* Take some shortcuts in intra search if intra is deemed unlikely */
  71.     int b_fast_intra;
  72.     int b_try_pskip;
  73.     /* Luma part */
  74.     int i_satd_i16x16;
  75.     int i_satd_i16x16_dir[7];
  76.     int i_predict16x16;
  77.     int i_satd_i8x8;
  78.     int i_satd_i8x8_dir[12][4];
  79.     int i_predict8x8[4];
  80.     int i_satd_i4x4;
  81.     int i_predict4x4[16];
  82.     int i_satd_pcm;
  83.     /* Chroma part */
  84.     int i_satd_i8x8chroma;
  85.     int i_satd_i8x8chroma_dir[4];
  86.     int i_predict8x8chroma;
  87.     /* II: Inter part P/B frame */
  88.     x264_mb_analysis_list_t l0;
  89.     x264_mb_analysis_list_t l1;
  90.     int i_cost16x16bi; /* used the same ref and mv as l0 and l1 (at least for now) */
  91.     int i_cost16x16direct;
  92.     int i_cost8x8bi;
  93.     int i_cost8x8direct[4];
  94.     int i_cost16x8bi;
  95.     int i_cost8x16bi;
  96.     int i_rd16x16bi;
  97.     int i_rd16x16direct;
  98.     int i_rd16x8bi;
  99.     int i_rd8x16bi;
  100.     int i_rd8x8bi;
  101.     int i_mb_partition16x8[2]; /* mb_partition_e */
  102.     int i_mb_partition8x16[2];
  103.     int i_mb_type16x8; /* mb_class_e */
  104.     int i_mb_type8x16;
  105.     int b_direct_available;
  106. } x264_mb_analysis_t;
  107. /* lambda = pow(2,qp/6-2) */
  108. const int x264_lambda_tab[52] = {
  109.    1, 1, 1, 1, 1, 1, 1, 1,  /*  0-7 */
  110.    1, 1, 1, 1,              /*  8-11 */
  111.    1, 1, 1, 1, 2, 2, 2, 2,  /* 12-19 */
  112.    3, 3, 3, 4, 4, 4, 5, 6,  /* 20-27 */
  113.    6, 7, 8, 9,10,11,13,14,  /* 28-35 */
  114.   16,18,20,23,25,29,32,36,  /* 36-43 */
  115.   40,45,51,57,64,72,81,91   /* 44-51 */
  116. };
  117. /* lambda2 = pow(lambda,2) * .9 * 256 */
  118. const int x264_lambda2_tab[52] = {
  119.     14,      18,      22,      28,     36,     45,     57,     72, /*  0 -  7 */
  120.     91,     115,     145,     182,    230,    290,    365,    460, /*  8 - 15 */
  121.    580,     731,     921,    1161,   1462,   1843,   2322,   2925, /* 16 - 23 */
  122.   3686,    4644,    5851,    7372,   9289,  11703,  14745,  18578, /* 24 - 31 */
  123.  23407,   29491,   37156,   46814,  58982,  74313,  93628, 117964, /* 32 - 39 */
  124. 148626,  187257,  235929,  297252, 374514, 471859, 594505, 749029, /* 40 - 47 */
  125. 943718, 1189010, 1498059, 1887436                                  /* 48 - 51 */
  126. };
  127. /* TODO: calculate CABAC costs */
  128. static const int i_mb_b_cost_table[X264_MBTYPE_MAX] = {
  129.     9, 9, 9, 9, 0, 0, 0, 1, 3, 7, 7, 7, 3, 7, 7, 7, 5, 9, 0
  130. };
  131. static const int i_mb_b16x8_cost_table[17] = {
  132.     0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 7, 7, 5, 7, 9, 9, 9
  133. };
  134. static const int i_sub_mb_b_cost_table[13] = {
  135.     7, 5, 5, 3, 7, 5, 7, 3, 7, 7, 7, 5, 1
  136. };
  137. static const int i_sub_mb_p_cost_table[4] = {
  138.     5, 3, 3, 1
  139. };
  140. static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a );
  141. uint16_t *x264_cost_mv_fpel[52][4];
  142. /* initialize an array of lambda*nbits for all possible mvs */
  143. static void x264_mb_analyse_load_costs( x264_t *h, x264_mb_analysis_t *a )
  144. {
  145.     static int16_t *p_cost_mv[52];
  146.     int i, j;
  147.     if( !p_cost_mv[a->i_qp] )
  148.     {
  149.         /* could be faster, but isn't called many times */
  150.         /* factor of 4 from qpel, 2 from sign, and 2 because mv can be opposite from mvp */
  151.         p_cost_mv[a->i_qp] = x264_malloc( (4*4*2048 + 1) * sizeof(int16_t) );
  152.         p_cost_mv[a->i_qp] += 2*4*2048;
  153.         for( i = 0; i <= 2*4*2048; i++ )
  154.         {
  155.             p_cost_mv[a->i_qp][-i] =
  156.             p_cost_mv[a->i_qp][i]  = a->i_lambda * bs_size_se( i );
  157.         }
  158.     }
  159.     a->p_cost_mv = p_cost_mv[a->i_qp];
  160.     /* FIXME is this useful for all me methods? */
  161.     if( h->param.analyse.i_me_method >= X264_ME_ESA && !x264_cost_mv_fpel[a->i_qp][0] )
  162.     {
  163.         for( j=0; j<4; j++ )
  164.         {
  165.             x264_cost_mv_fpel[a->i_qp][j] = x264_malloc( (4*2048 + 1) * sizeof(int16_t) );
  166.             x264_cost_mv_fpel[a->i_qp][j] += 2*2048;
  167.             for( i = -2*2048; i < 2*2048; i++ )
  168.                 x264_cost_mv_fpel[a->i_qp][j][i] = p_cost_mv[a->i_qp][i*4+j];
  169.         }
  170.     }
  171. }
  172. static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp )
  173. {
  174.     /* conduct the analysis using this lamda and QP */
  175.     a->i_qp = h->mb.i_qp = i_qp;
  176.     h->mb.i_chroma_qp = h->chroma_qp_table[i_qp];
  177.     a->i_lambda = x264_lambda_tab[i_qp];
  178.     a->i_lambda2 = x264_lambda2_tab[i_qp];
  179.     a->b_mbrd = h->param.analyse.i_subpel_refine >= 6 &&
  180.                 ( h->sh.i_type != SLICE_TYPE_B || h->param.analyse.b_bframe_rdo );
  181.     h->mb.i_me_method = h->param.analyse.i_me_method;
  182.     h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
  183.     h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
  184.                         && h->mb.i_subpel_refine >= 5;
  185.     h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->b_mbrd;
  186.     h->mb.b_transform_8x8 = 0;
  187.     h->mb.b_noise_reduction = 0;
  188.     /* I: Intra part */
  189.     a->i_satd_i16x16 =
  190.     a->i_satd_i8x8   =
  191.     a->i_satd_i4x4   =
  192.     a->i_satd_i8x8chroma = COST_MAX;
  193.     /* non-RD PCM decision is inaccurate, so don't do it */
  194.     a->i_satd_pcm = a->b_mbrd ? ((uint64_t)X264_PCM_COST*a->i_lambda2 + 128) >> 8 : COST_MAX;
  195.     a->b_fast_intra = 0;
  196.     h->mb.i_skip_intra =
  197.         h->mb.b_lossless ? 0 :
  198.         a->b_mbrd ? 2 :
  199.         !h->param.analyse.i_trellis && !h->param.analyse.i_noise_reduction;
  200.     /* II: Inter part P/B frame */
  201.     if( h->sh.i_type != SLICE_TYPE_I )
  202.     {
  203.         int i, j;
  204.         int i_fmv_range = 4 * h->param.analyse.i_mv_range;
  205.         // limit motion search to a slightly smaller range than the theoretical limit,
  206.         // since the search may go a few iterations past its given range
  207.         int i_fpel_border = 5; // umh unconditional radius
  208.         int i_spel_border = 8; // 1.5 for subpel_satd, 1.5 for subpel_rd, 2 for bime, round up
  209.         /* Calculate max allowed MV range */
  210. #define CLIP_FMV(mv) x264_clip3( mv, -i_fmv_range, i_fmv_range-1 )
  211.         h->mb.mv_min[0] = 4*( -16*h->mb.i_mb_x - 24 );
  212.         h->mb.mv_max[0] = 4*( 16*( h->sps->i_mb_width - h->mb.i_mb_x - 1 ) + 24 );
  213.         h->mb.mv_min_spel[0] = CLIP_FMV( h->mb.mv_min[0] );
  214.         h->mb.mv_max_spel[0] = CLIP_FMV( h->mb.mv_max[0] );
  215.         h->mb.mv_min_fpel[0] = (h->mb.mv_min_spel[0]>>2) + i_fpel_border;
  216.         h->mb.mv_max_fpel[0] = (h->mb.mv_max_spel[0]>>2) - i_fpel_border;
  217.         if( h->mb.i_mb_x == 0)
  218.         {
  219.             int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff;
  220.             int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff;
  221.             int thread_mvy_range = i_fmv_range;
  222.             if( h->param.i_threads > 1 )
  223.             {
  224.                 int pix_y = (h->mb.i_mb_y | h->mb.b_interlaced) * 16;
  225.                 int thresh = pix_y + h->param.analyse.i_mv_range_thread;
  226.                 for( i = (h->sh.i_type == SLICE_TYPE_B); i >= 0; i-- )
  227.                 {
  228.                     x264_frame_t **fref = i ? h->fref1 : h->fref0;
  229.                     int i_ref = i ? h->i_ref1 : h->i_ref0;
  230.                     for( j=0; j<i_ref; j++ )
  231.                     {
  232.                         x264_frame_cond_wait( fref[j], thresh );
  233.                         thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y );
  234.                     }
  235.                 }
  236.                 if( h->param.b_deterministic )
  237.                     thread_mvy_range = h->param.analyse.i_mv_range_thread;
  238.                 if( h->mb.b_interlaced )
  239.                     thread_mvy_range >>= 1;
  240.             }
  241.             h->mb.mv_min[1] = 4*( -16*mb_y - 24 );
  242.             h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 );
  243.             h->mb.mv_min_spel[1] = x264_clip3( h->mb.mv_min[1], X264_MAX(4*(-512+i_spel_border), -i_fmv_range), i_fmv_range );
  244.             h->mb.mv_max_spel[1] = CLIP_FMV( h->mb.mv_max[1] );
  245.             h->mb.mv_max_spel[1] = X264_MIN( h->mb.mv_max_spel[1], thread_mvy_range*4 );
  246.             h->mb.mv_min_fpel[1] = (h->mb.mv_min_spel[1]>>2) + i_fpel_border;
  247.             h->mb.mv_max_fpel[1] = (h->mb.mv_max_spel[1]>>2) - i_fpel_border;
  248.         }
  249. #undef CLIP_FMV
  250.         a->l0.me16x16.cost =
  251.         a->l0.i_rd16x16    =
  252.         a->l0.i_cost8x8    = COST_MAX;
  253.         for( i = 0; i < 4; i++ )
  254.         {
  255.             a->l0.i_cost4x4[i] =
  256.             a->l0.i_cost8x4[i] =
  257.             a->l0.i_cost4x8[i] = COST_MAX;
  258.         }
  259.         a->l0.i_cost16x8   =
  260.         a->l0.i_cost8x16   = COST_MAX;
  261.         if( h->sh.i_type == SLICE_TYPE_B )
  262.         {
  263.             a->l1.me16x16.cost =
  264.             a->l1.i_rd16x16    =
  265.             a->l1.i_cost8x8    = COST_MAX;
  266.             for( i = 0; i < 4; i++ )
  267.             {
  268.                 a->l1.i_cost4x4[i] =
  269.                 a->l1.i_cost8x4[i] =
  270.                 a->l1.i_cost4x8[i] =
  271.                 a->i_cost8x8direct[i] = COST_MAX;
  272.             }
  273.             a->l1.i_cost16x8   =
  274.             a->l1.i_cost8x16   =
  275.             a->i_rd16x16bi     =
  276.             a->i_rd16x16direct =
  277.             a->i_rd8x8bi       =
  278.             a->i_rd16x8bi      =
  279.             a->i_rd8x16bi      =
  280.             a->i_cost16x16bi   =
  281.             a->i_cost16x16direct =
  282.             a->i_cost8x8bi     =
  283.             a->i_cost16x8bi    =
  284.             a->i_cost8x16bi    = COST_MAX;
  285.         }
  286.         /* Fast intra decision */
  287.         if( h->mb.i_mb_xy - h->sh.i_first_mb > 4 )
  288.         {
  289.             if(   IS_INTRA( h->mb.i_mb_type_left )
  290.                || IS_INTRA( h->mb.i_mb_type_top )
  291.                || IS_INTRA( h->mb.i_mb_type_topleft )
  292.                || IS_INTRA( h->mb.i_mb_type_topright )
  293.                || (h->sh.i_type == SLICE_TYPE_P && IS_INTRA( h->fref0[0]->mb_type[h->mb.i_mb_xy] ))
  294.                || (h->mb.i_mb_xy - h->sh.i_first_mb < 3*(h->stat.frame.i_mb_count[I_4x4] + h->stat.frame.i_mb_count[I_8x8] + h->stat.frame.i_mb_count[I_16x16])) )
  295.             { /* intra is likely */ }
  296.             else
  297.             {
  298.                 a->b_fast_intra = 1;
  299.             }
  300.         }
  301.         h->mb.b_skip_mc = 0;
  302.     }
  303. }
  304. /*
  305.  * Handle intra mb
  306.  */
  307. /* Max = 4 */
  308. static void predict_16x16_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
  309. {
  310.     if( i_neighbour & MB_TOPLEFT )
  311.     {
  312.         /* top and left available */
  313.         *mode++ = I_PRED_16x16_V;
  314.         *mode++ = I_PRED_16x16_H;
  315.         *mode++ = I_PRED_16x16_DC;
  316.         *mode++ = I_PRED_16x16_P;
  317.         *pi_count = 4;
  318.     }
  319.     else if( i_neighbour & MB_LEFT )
  320.     {
  321.         /* left available*/
  322.         *mode++ = I_PRED_16x16_DC_LEFT;
  323.         *mode++ = I_PRED_16x16_H;
  324.         *pi_count = 2;
  325.     }
  326.     else if( i_neighbour & MB_TOP )
  327.     {
  328.         /* top available*/
  329.         *mode++ = I_PRED_16x16_DC_TOP;
  330.         *mode++ = I_PRED_16x16_V;
  331.         *pi_count = 2;
  332.     }
  333.     else
  334.     {
  335.         /* none available */
  336.         *mode = I_PRED_16x16_DC_128;
  337.         *pi_count = 1;
  338.     }
  339. }
  340. /* Max = 4 */
  341. static void predict_8x8chroma_mode_available( unsigned int i_neighbour, int *mode, int *pi_count )
  342. {
  343.     if( i_neighbour & MB_TOPLEFT )
  344.     {
  345.         /* top and left available */
  346.         *mode++ = I_PRED_CHROMA_V;
  347.         *mode++ = I_PRED_CHROMA_H;
  348.         *mode++ = I_PRED_CHROMA_DC;
  349.         *mode++ = I_PRED_CHROMA_P;
  350.         *pi_count = 4;
  351.     }
  352.     else if( i_neighbour & MB_LEFT )
  353.     {
  354.         /* left available*/
  355.         *mode++ = I_PRED_CHROMA_DC_LEFT;
  356.         *mode++ = I_PRED_CHROMA_H;
  357.         *pi_count = 2;
  358.     }
  359.     else if( i_neighbour & MB_TOP )
  360.     {
  361.         /* top available*/
  362.         *mode++ = I_PRED_CHROMA_DC_TOP;
  363.         *mode++ = I_PRED_CHROMA_V;
  364.         *pi_count = 2;
  365.     }
  366.     else
  367.     {
  368.         /* none available */
  369.         *mode = I_PRED_CHROMA_DC_128;
  370.         *pi_count = 1;
  371.     }
  372. }
  373. /* MAX = 9 */
  374. static void predict_4x4_mode_available( unsigned int i_neighbour,
  375.                                         int *mode, int *pi_count )
  376. {
  377.     int b_l = i_neighbour & MB_LEFT;
  378.     int b_t = i_neighbour & MB_TOP;
  379.     if( b_l && b_t )
  380.     {
  381.         *pi_count = 6;
  382.         *mode++ = I_PRED_4x4_DC;
  383.         *mode++ = I_PRED_4x4_H;
  384.         *mode++ = I_PRED_4x4_V;
  385.         *mode++ = I_PRED_4x4_DDL;
  386.         if( i_neighbour & MB_TOPLEFT )
  387.         {
  388.             *mode++ = I_PRED_4x4_DDR;
  389.             *mode++ = I_PRED_4x4_VR;
  390.             *mode++ = I_PRED_4x4_HD;
  391.             *pi_count += 3;
  392.         }
  393.         *mode++ = I_PRED_4x4_VL;
  394.         *mode++ = I_PRED_4x4_HU;
  395.     }
  396.     else if( b_l )
  397.     {
  398.         *mode++ = I_PRED_4x4_DC_LEFT;
  399.         *mode++ = I_PRED_4x4_H;
  400.         *mode++ = I_PRED_4x4_HU;
  401.         *pi_count = 3;
  402.     }
  403.     else if( b_t )
  404.     {
  405.         *mode++ = I_PRED_4x4_DC_TOP;
  406.         *mode++ = I_PRED_4x4_V;
  407.         *mode++ = I_PRED_4x4_DDL;
  408.         *mode++ = I_PRED_4x4_VL;
  409.         *pi_count = 4;
  410.     }
  411.     else
  412.     {
  413.         *mode++ = I_PRED_4x4_DC_128;
  414.         *pi_count = 1;
  415.     }
  416. }
  417. static void x264_mb_analyse_intra_chroma( x264_t *h, x264_mb_analysis_t *a )
  418. {
  419.     int i;
  420.     int i_max;
  421.     int predict_mode[4];
  422.     uint8_t *p_dstc[2], *p_srcc[2];
  423.     if( a->i_satd_i8x8chroma < COST_MAX )
  424.         return;
  425.     /* 8x8 prediction selection for chroma */
  426.     p_dstc[0] = h->mb.pic.p_fdec[1];
  427.     p_dstc[1] = h->mb.pic.p_fdec[2];
  428.     p_srcc[0] = h->mb.pic.p_fenc[1];
  429.     p_srcc[1] = h->mb.pic.p_fenc[2];
  430.     predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
  431.     a->i_satd_i8x8chroma = COST_MAX;
  432.     if( i_max == 4 && h->pixf.intra_satd_x3_8x8c && h->pixf.mbcmp[0] == h->pixf.satd[0] )
  433.     {
  434.         int satdu[4], satdv[4];
  435.         h->pixf.intra_satd_x3_8x8c( p_srcc[0], p_dstc[0], satdu );
  436.         h->pixf.intra_satd_x3_8x8c( p_srcc[1], p_dstc[1], satdv );
  437.         h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[0] );
  438.         h->predict_8x8c[I_PRED_CHROMA_P]( p_dstc[1] );
  439.         satdu[I_PRED_CHROMA_P] =
  440.             h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE, p_srcc[0], FENC_STRIDE );
  441.         satdv[I_PRED_CHROMA_P] =
  442.             h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE, p_srcc[1], FENC_STRIDE );
  443.         for( i=0; i<i_max; i++ )
  444.         {
  445.             int i_mode = predict_mode[i];
  446.             int i_satd = satdu[i_mode] + satdv[i_mode]
  447.                        + a->i_lambda * bs_size_ue(i_mode);
  448.             a->i_satd_i8x8chroma_dir[i] = i_satd;
  449.             COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
  450.         }
  451.     }
  452.     else
  453.     {
  454.         for( i=0; i<i_max; i++ )
  455.         {
  456.             int i_satd;
  457.             int i_mode = predict_mode[i];
  458.             /* we do the prediction */
  459.             h->predict_8x8c[i_mode]( p_dstc[0] );
  460.             h->predict_8x8c[i_mode]( p_dstc[1] );
  461.             /* we calculate the cost */
  462.             i_satd = h->pixf.mbcmp[PIXEL_8x8]( p_dstc[0], FDEC_STRIDE,
  463.                                                p_srcc[0], FENC_STRIDE ) +
  464.                      h->pixf.mbcmp[PIXEL_8x8]( p_dstc[1], FDEC_STRIDE,
  465.                                                p_srcc[1], FENC_STRIDE ) +
  466.                      a->i_lambda * bs_size_ue( x264_mb_pred_mode8x8c_fix[i_mode] );
  467.             a->i_satd_i8x8chroma_dir[i] = i_satd;
  468.             COPY2_IF_LT( a->i_satd_i8x8chroma, i_satd, a->i_predict8x8chroma, i_mode );
  469.         }
  470.     }
  471.     h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
  472. }
  473. static void x264_mb_analyse_intra( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
  474. {
  475.     const unsigned int flags = h->sh.i_type == SLICE_TYPE_I ? h->param.analyse.intra : h->param.analyse.inter;
  476.     uint8_t  *p_src = h->mb.pic.p_fenc[0];
  477.     uint8_t  *p_dst = h->mb.pic.p_fdec[0];
  478.     int i, idx;
  479.     int i_max;
  480.     int predict_mode[9];
  481.     int b_merged_satd = !!h->pixf.intra_mbcmp_x3_16x16;
  482.     /*---------------- Try all mode and calculate their score ---------------*/
  483.     /* 16x16 prediction selection */
  484.     predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
  485.     if( b_merged_satd && i_max == 4 )
  486.     {
  487.         h->pixf.intra_mbcmp_x3_16x16( p_src, p_dst, a->i_satd_i16x16_dir );
  488.         h->predict_16x16[I_PRED_16x16_P]( p_dst );
  489.         a->i_satd_i16x16_dir[I_PRED_16x16_P] =
  490.             h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
  491.         for( i=0; i<4; i++ )
  492.         {
  493.             int cost = a->i_satd_i16x16_dir[i] += a->i_lambda * bs_size_ue(i);
  494.             COPY2_IF_LT( a->i_satd_i16x16, cost, a->i_predict16x16, i );
  495.         }
  496.     }
  497.     else
  498.     {
  499.         for( i = 0; i < i_max; i++ )
  500.         {
  501.             int i_satd;
  502.             int i_mode = predict_mode[i];
  503.             h->predict_16x16[i_mode]( p_dst );
  504.             i_satd = h->pixf.mbcmp[PIXEL_16x16]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) +
  505.                     a->i_lambda * bs_size_ue( x264_mb_pred_mode16x16_fix[i_mode] );
  506.             COPY2_IF_LT( a->i_satd_i16x16, i_satd, a->i_predict16x16, i_mode );
  507.             a->i_satd_i16x16_dir[i_mode] = i_satd;
  508.         }
  509.     }
  510.     if( h->sh.i_type == SLICE_TYPE_B )
  511.         /* cavlc mb type prefix */
  512.         a->i_satd_i16x16 += a->i_lambda * i_mb_b_cost_table[I_16x16];
  513.     if( a->b_fast_intra && a->i_satd_i16x16 > 2*i_satd_inter )
  514.         return;
  515.     /* 8x8 prediction selection */
  516.     if( flags & X264_ANALYSE_I8x8 )
  517.     {
  518.         DECLARE_ALIGNED_16( uint8_t edge[33] );
  519.         x264_pixel_cmp_t sa8d = (h->pixf.mbcmp[0] == h->pixf.satd[0]) ? h->pixf.sa8d[PIXEL_8x8] : h->pixf.mbcmp[PIXEL_8x8];
  520.         int i_satd_thresh = a->b_mbrd ? COST_MAX : X264_MIN( i_satd_inter, a->i_satd_i16x16 );
  521.         int i_cost = 0;
  522.         b_merged_satd = h->pixf.intra_sa8d_x3_8x8 && h->pixf.mbcmp[0] == h->pixf.satd[0];
  523.         // FIXME some bias like in i4x4?
  524.         if( h->sh.i_type == SLICE_TYPE_B )
  525.             i_cost += a->i_lambda * i_mb_b_cost_table[I_8x8];
  526.         for( idx = 0;; idx++ )
  527.         {
  528.             int x = idx&1;
  529.             int y = idx>>1;
  530.             uint8_t *p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
  531.             uint8_t *p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
  532.             int i_best = COST_MAX;
  533.             int i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
  534.             predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
  535.             x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
  536.             if( b_merged_satd && i_max == 9 )
  537.             {
  538.                 int satd[9];
  539.                 h->pixf.intra_sa8d_x3_8x8( p_src_by, edge, satd );
  540.                 satd[i_pred_mode] -= 3 * a->i_lambda;
  541.                 for( i=2; i>=0; i-- )
  542.                 {
  543.                     int cost = a->i_satd_i8x8_dir[i][idx] = satd[i] + 4 * a->i_lambda;
  544.                     COPY2_IF_LT( i_best, cost, a->i_predict8x8[idx], i );
  545.                 }
  546.                 i = 3;
  547.             }
  548.             else
  549.                 i = 0;
  550.             for( ; i<i_max; i++ )
  551.             {
  552.                 int i_satd;
  553.                 int i_mode = predict_mode[i];
  554.                 h->predict_8x8[i_mode]( p_dst_by, edge );
  555.                 i_satd = sa8d( p_dst_by, FDEC_STRIDE, p_src_by, FENC_STRIDE )
  556.                        + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
  557.                 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8[idx], i_mode );
  558.                 a->i_satd_i8x8_dir[i_mode][idx] = i_satd;
  559.             }
  560.             i_cost += i_best;
  561.             if( idx == 3 || i_cost > i_satd_thresh )
  562.                 break;
  563.             /* we need to encode this block now (for next ones) */
  564.             h->predict_8x8[a->i_predict8x8[idx]]( p_dst_by, edge );
  565.             x264_mb_encode_i8x8( h, idx, a->i_qp );
  566.             x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
  567.         }
  568.         if( idx == 3 )
  569.         {
  570.             a->i_satd_i8x8 = i_cost;
  571.             if( h->mb.i_skip_intra )
  572.             {
  573.                 h->mc.copy[PIXEL_16x16]( h->mb.pic.i8x8_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
  574.                 if( h->mb.i_skip_intra == 2 )
  575.                     h->mc.memcpy_aligned( h->mb.pic.i8x8_dct_buf, h->dct.luma8x8, sizeof(h->mb.pic.i8x8_dct_buf) );
  576.             }
  577.         }
  578.         else
  579.         {
  580.             a->i_satd_i8x8 = COST_MAX;
  581.             i_cost = i_cost * 4/(idx+1);
  582.         }
  583.         if( X264_MIN(i_cost, a->i_satd_i16x16) > i_satd_inter*(5+a->b_mbrd)/4 )
  584.             return;
  585.     }
  586.     /* 4x4 prediction selection */
  587.     if( flags & X264_ANALYSE_I4x4 )
  588.     {
  589.         int i_cost;
  590.         int i_satd_thresh = X264_MIN3( i_satd_inter, a->i_satd_i16x16, a->i_satd_i8x8 );
  591.         b_merged_satd = h->pixf.intra_satd_x3_4x4 && h->pixf.mbcmp[0] == h->pixf.satd[0];
  592.         if( a->b_mbrd )
  593.             i_satd_thresh = i_satd_thresh * (10-a->b_fast_intra)/8;
  594.         i_cost = a->i_lambda * 24;    /* from JVT (SATD0) */
  595.         if( h->sh.i_type == SLICE_TYPE_B )
  596.             i_cost += a->i_lambda * i_mb_b_cost_table[I_4x4];
  597.         for( idx = 0;; idx++ )
  598.         {
  599.             uint8_t *p_src_by = p_src + block_idx_xy_fenc[idx];
  600.             uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
  601.             int i_best = COST_MAX;
  602.             int i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
  603.             predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
  604.             if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
  605.                 /* emulate missing topright samples */
  606.                 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
  607.             if( b_merged_satd && i_max >= 6 )
  608.             {
  609.                 int satd[9];
  610.                 h->pixf.intra_satd_x3_4x4( p_src_by, p_dst_by, satd );
  611.                 satd[i_pred_mode] -= 3 * a->i_lambda;
  612.                 for( i=2; i>=0; i-- )
  613.                     COPY2_IF_LT( i_best, satd[i] + 4 * a->i_lambda,
  614.                                  a->i_predict4x4[idx], i );
  615.                 i = 3;
  616.             }
  617.             else
  618.                 i = 0;
  619.             for( ; i<i_max; i++ )
  620.             {
  621.                 int i_satd;
  622.                 int i_mode = predict_mode[i];
  623.                 h->predict_4x4[i_mode]( p_dst_by );
  624.                 i_satd = h->pixf.mbcmp[PIXEL_4x4]( p_dst_by, FDEC_STRIDE,
  625.                                                    p_src_by, FENC_STRIDE )
  626.                        + a->i_lambda * (i_pred_mode == x264_mb_pred_mode4x4_fix(i_mode) ? 1 : 4);
  627.                 COPY2_IF_LT( i_best, i_satd, a->i_predict4x4[idx], i_mode );
  628.             }
  629.             i_cost += i_best;
  630.             if( i_cost > i_satd_thresh || idx == 15 )
  631.                 break;
  632.             /* we need to encode this block now (for next ones) */
  633.             h->predict_4x4[a->i_predict4x4[idx]]( p_dst_by );
  634.             x264_mb_encode_i4x4( h, idx, a->i_qp );
  635.             h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
  636.         }
  637.         if( idx == 15 )
  638.         {
  639.             a->i_satd_i4x4 = i_cost;
  640.             if( h->mb.i_skip_intra )
  641.             {
  642.                 h->mc.copy[PIXEL_16x16]( h->mb.pic.i4x4_fdec_buf, 16, p_dst, FDEC_STRIDE, 16 );
  643.                 if( h->mb.i_skip_intra == 2 )
  644.                     h->mc.memcpy_aligned( h->mb.pic.i4x4_dct_buf, h->dct.luma4x4, sizeof(h->mb.pic.i4x4_dct_buf) );
  645.             }
  646.         }
  647.         else
  648.             a->i_satd_i4x4 = COST_MAX;
  649.     }
  650. }
  651. static void x264_intra_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_thresh )
  652. {
  653.     if( a->i_satd_i16x16 <= i_satd_thresh )
  654.     {
  655.         h->mb.i_type = I_16x16;
  656.         x264_analyse_update_cache( h, a );
  657.         a->i_satd_i16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
  658.     }
  659.     else
  660.         a->i_satd_i16x16 = COST_MAX;
  661.     if( a->i_satd_i4x4 <= i_satd_thresh && a->i_satd_i4x4 < COST_MAX )
  662.     {
  663.         h->mb.i_type = I_4x4;
  664.         x264_analyse_update_cache( h, a );
  665.         a->i_satd_i4x4 = x264_rd_cost_mb( h, a->i_lambda2 );
  666.     }
  667.     else
  668.         a->i_satd_i4x4 = COST_MAX;
  669.     if( a->i_satd_i8x8 <= i_satd_thresh && a->i_satd_i8x8 < COST_MAX )
  670.     {
  671.         h->mb.i_type = I_8x8;
  672.         x264_analyse_update_cache( h, a );
  673.         a->i_satd_i8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
  674.     }
  675.     else
  676.         a->i_satd_i8x8 = COST_MAX;
  677. }
  678. static void x264_intra_rd_refine( x264_t *h, x264_mb_analysis_t *a )
  679. {
  680.     uint8_t  *p_src = h->mb.pic.p_fenc[0];
  681.     uint8_t  *p_dst = h->mb.pic.p_fdec[0];
  682.     int i, j, idx, x, y;
  683.     int i_max, i_mode, i_thresh;
  684.     uint64_t i_satd, i_best;
  685.     int i_pred_mode;
  686.     int predict_mode[9];
  687.     h->mb.i_skip_intra = 0;
  688.     if( h->mb.i_type == I_16x16 )
  689.     {
  690.         int old_pred_mode = a->i_predict16x16;
  691.         i_thresh = a->i_satd_i16x16_dir[old_pred_mode] * 9/8;
  692.         i_best = a->i_satd_i16x16;
  693.         predict_16x16_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
  694.         for( i = 0; i < i_max; i++ )
  695.         {
  696.             int i_mode = predict_mode[i];
  697.             if( i_mode == old_pred_mode || a->i_satd_i16x16_dir[i_mode] > i_thresh )
  698.                 continue;
  699.             h->mb.i_intra16x16_pred_mode = i_mode;
  700.             i_satd = x264_rd_cost_mb( h, a->i_lambda2 );
  701.             COPY2_IF_LT( i_best, i_satd, a->i_predict16x16, i_mode );
  702.         }
  703.     }
  704.     else if( h->mb.i_type == I_4x4 )
  705.     {
  706.         uint32_t pels[4] = {0}; // doesn't need initting, just shuts up a gcc warning
  707.         int i_nnz = 0;
  708.         for( idx = 0; idx < 16; idx++ )
  709.         {
  710.             uint8_t *p_dst_by = p_dst + block_idx_xy_fdec[idx];
  711.             i_best = COST_MAX64;
  712.             i_pred_mode = x264_mb_predict_intra4x4_mode( h, idx );
  713.             predict_4x4_mode_available( h->mb.i_neighbour4[idx], predict_mode, &i_max );
  714.             if( (h->mb.i_neighbour4[idx] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
  715.                 /* emulate missing topright samples */
  716.                 *(uint32_t*) &p_dst_by[4 - FDEC_STRIDE] = p_dst_by[3 - FDEC_STRIDE] * 0x01010101U;
  717.             for( i = 0; i < i_max; i++ )
  718.             {
  719.                 i_mode = predict_mode[i];
  720.                 h->predict_4x4[i_mode]( p_dst_by );
  721.                 i_satd = x264_rd_cost_i4x4( h, a->i_lambda2, idx, i_mode );
  722.                 if( i_best > i_satd )
  723.                 {
  724.                     a->i_predict4x4[idx] = i_mode;
  725.                     i_best = i_satd;
  726.                     pels[0] = *(uint32_t*)(p_dst_by+0*FDEC_STRIDE);
  727.                     pels[1] = *(uint32_t*)(p_dst_by+1*FDEC_STRIDE);
  728.                     pels[2] = *(uint32_t*)(p_dst_by+2*FDEC_STRIDE);
  729.                     pels[3] = *(uint32_t*)(p_dst_by+3*FDEC_STRIDE);
  730.                     i_nnz = h->mb.cache.non_zero_count[x264_scan8[idx]];
  731.                 }
  732.             }
  733.             *(uint32_t*)(p_dst_by+0*FDEC_STRIDE) = pels[0];
  734.             *(uint32_t*)(p_dst_by+1*FDEC_STRIDE) = pels[1];
  735.             *(uint32_t*)(p_dst_by+2*FDEC_STRIDE) = pels[2];
  736.             *(uint32_t*)(p_dst_by+3*FDEC_STRIDE) = pels[3];
  737.             h->mb.cache.non_zero_count[x264_scan8[idx]] = i_nnz;
  738.             h->mb.cache.intra4x4_pred_mode[x264_scan8[idx]] = a->i_predict4x4[idx];
  739.         }
  740.     }
  741.     else if( h->mb.i_type == I_8x8 )
  742.     {
  743.         DECLARE_ALIGNED_16( uint8_t edge[33] );
  744.         for( idx = 0; idx < 4; idx++ )
  745.         {
  746.             uint64_t pels_h = 0;
  747.             uint8_t pels_v[7];
  748.             int i_nnz[3];
  749.             uint8_t *p_src_by;
  750.             uint8_t *p_dst_by;
  751.             int j;
  752.             i_thresh = a->i_satd_i8x8_dir[a->i_predict8x8[idx]][idx] * 11/8;
  753.             i_best = COST_MAX64;
  754.             i_pred_mode = x264_mb_predict_intra4x4_mode( h, 4*idx );
  755.             x = idx&1;
  756.             y = idx>>1;
  757.             p_src_by = p_src + 8*x + 8*y*FENC_STRIDE;
  758.             p_dst_by = p_dst + 8*x + 8*y*FDEC_STRIDE;
  759.             predict_4x4_mode_available( h->mb.i_neighbour8[idx], predict_mode, &i_max );
  760.             x264_predict_8x8_filter( p_dst_by, edge, h->mb.i_neighbour8[idx], ALL_NEIGHBORS );
  761.             for( i = 0; i < i_max; i++ )
  762.             {
  763.                 i_mode = predict_mode[i];
  764.                 if( a->i_satd_i8x8_dir[i_mode][idx] > i_thresh )
  765.                     continue;
  766.                 h->predict_8x8[i_mode]( p_dst_by, edge );
  767.                 i_satd = x264_rd_cost_i8x8( h, a->i_lambda2, idx, i_mode );
  768.                 if( i_best > i_satd )
  769.                 {
  770.                     a->i_predict8x8[idx] = i_mode;
  771.                     i_best = i_satd;
  772.                     pels_h = *(uint64_t*)(p_dst_by+7*FDEC_STRIDE);
  773.                     if( !(idx&1) )
  774.                         for( j=0; j<7; j++ )
  775.                             pels_v[j] = p_dst_by[7+j*FDEC_STRIDE];
  776.                     for( j=0; j<3; j++ )
  777.                         i_nnz[j] = h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]];
  778.                 }
  779.             }
  780.             *(uint64_t*)(p_dst_by+7*FDEC_STRIDE) = pels_h;
  781.             if( !(idx&1) )
  782.                 for( j=0; j<7; j++ )
  783.                     p_dst_by[7+j*FDEC_STRIDE] = pels_v[j];
  784.             for( j=0; j<3; j++ )
  785.                 h->mb.cache.non_zero_count[x264_scan8[4*idx+j+1]] = i_nnz[j];
  786.             x264_macroblock_cache_intra8x8_pred( h, 2*x, 2*y, a->i_predict8x8[idx] );
  787.         }
  788.     }
  789.     /* RD selection for chroma prediction */
  790.     predict_8x8chroma_mode_available( h->mb.i_neighbour, predict_mode, &i_max );
  791.     if( i_max > 1 )
  792.     {
  793.         i_thresh = a->i_satd_i8x8chroma * 5/4;
  794.         for( i = j = 0; i < i_max; i++ )
  795.             if( a->i_satd_i8x8chroma_dir[i] < i_thresh &&
  796.                 predict_mode[i] != a->i_predict8x8chroma )
  797.             {
  798.                 predict_mode[j++] = predict_mode[i];
  799.             }
  800.         i_max = j;
  801.         if( i_max > 0 )
  802.         {
  803.             int i_chroma_lambda = x264_lambda2_tab[h->mb.i_chroma_qp];
  804.             /* the previous thing encoded was x264_intra_rd(), so the pixels and
  805.              * coefs for the current chroma mode are still around, so we only
  806.              * have to recount the bits. */
  807.             i_best = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, a->i_predict8x8chroma, 0 );
  808.             for( i = 0; i < i_max; i++ )
  809.             {
  810.                 i_mode = predict_mode[i];
  811.                 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
  812.                 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
  813.                 /* if we've already found a mode that needs no residual, then
  814.                  * probably any mode with a residual will be worse.
  815.                  * so avoid dct on the remaining modes to improve speed. */
  816.                 i_satd = x264_rd_cost_i8x8_chroma( h, i_chroma_lambda, i_mode, h->mb.i_cbp_chroma != 0x00 );
  817.                 COPY2_IF_LT( i_best, i_satd, a->i_predict8x8chroma, i_mode );
  818.             }
  819.             h->mb.i_chroma_pred_mode = a->i_predict8x8chroma;
  820.         }
  821.     }
  822. }
  823. #define LOAD_FENC( m, src, xoff, yoff) 
  824.     (m)->i_stride[0] = h->mb.pic.i_stride[0]; 
  825.     (m)->i_stride[1] = h->mb.pic.i_stride[1]; 
  826.     (m)->p_fenc[0] = &(src)[0][(xoff)+(yoff)*FENC_STRIDE]; 
  827.     (m)->p_fenc[1] = &(src)[1][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE]; 
  828.     (m)->p_fenc[2] = &(src)[2][((xoff)>>1)+((yoff)>>1)*FENC_STRIDE];
  829. #define LOAD_HPELS(m, src, list, ref, xoff, yoff) 
  830.     (m)->p_fref[0] = &(src)[0][(xoff)+(yoff)*(m)->i_stride[0]]; 
  831.     (m)->p_fref[1] = &(src)[1][(xoff)+(yoff)*(m)->i_stride[0]]; 
  832.     (m)->p_fref[2] = &(src)[2][(xoff)+(yoff)*(m)->i_stride[0]]; 
  833.     (m)->p_fref[3] = &(src)[3][(xoff)+(yoff)*(m)->i_stride[0]]; 
  834.     (m)->p_fref[4] = &(src)[4][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; 
  835.     (m)->p_fref[5] = &(src)[5][((xoff)>>1)+((yoff)>>1)*(m)->i_stride[1]]; 
  836.     (m)->integral = &h->mb.pic.p_integral[list][ref][(xoff)+(yoff)*(m)->i_stride[0]];
  837. #define REF_COST(list, ref) 
  838.     (a->i_lambda * bs_size_te( h->sh.i_num_ref_idx_l##list##_active - 1, ref ))
  839. static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
  840. {
  841.     x264_me_t m;
  842.     int i_ref, i_mvc;
  843.     DECLARE_ALIGNED_4( int16_t mvc[7][2] );
  844.     int i_halfpel_thresh = INT_MAX;
  845.     int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
  846.     /* 16x16 Search on all ref frame */
  847.     m.i_pixel = PIXEL_16x16;
  848.     m.p_cost_mv = a->p_cost_mv;
  849.     LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
  850.     a->l0.me16x16.cost = INT_MAX;
  851.     for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
  852.     {
  853.         const int i_ref_cost = REF_COST( 0, i_ref );
  854.         i_halfpel_thresh -= i_ref_cost;
  855.         m.i_ref_cost = i_ref_cost;
  856.         m.i_ref = i_ref;
  857.         /* search with ref */
  858.         LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
  859.         x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
  860.         x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
  861.         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
  862.         /* early termination
  863.          * SSD threshold would probably be better than SATD */
  864.         if( i_ref == 0
  865.             && a->b_try_pskip
  866.             && m.cost-m.cost_mv < 300*a->i_lambda
  867.             &&  abs(m.mv[0]-h->mb.cache.pskip_mv[0])
  868.               + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
  869.             && x264_macroblock_probe_pskip( h ) )
  870.         {
  871.             h->mb.i_type = P_SKIP;
  872.             x264_analyse_update_cache( h, a );
  873.             assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
  874.             return;
  875.         }
  876.         m.cost += i_ref_cost;
  877.         i_halfpel_thresh += i_ref_cost;
  878.         if( m.cost < a->l0.me16x16.cost )
  879.             h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
  880.         /* save mv for predicting neighbors */
  881.         *(uint32_t*)a->l0.mvc[i_ref][0] =
  882.         *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
  883.     }
  884.     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
  885.     assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
  886.     h->mb.i_type = P_L0;
  887.     if( a->b_mbrd && a->l0.me16x16.i_ref == 0
  888.         && *(uint32_t*)a->l0.me16x16.mv == *(uint32_t*)h->mb.cache.pskip_mv )
  889.     {
  890.         h->mb.i_partition = D_16x16;
  891.         x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
  892.         a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
  893.     }
  894. }
  895. static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t *a )
  896. {
  897.     x264_me_t m;
  898.     int i_ref;
  899.     uint8_t  **p_fenc = h->mb.pic.p_fenc;
  900.     int i_halfpel_thresh = INT_MAX;
  901.     int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL;
  902.     int i;
  903.     int i_maxref = h->mb.pic.i_fref[0]-1;
  904.     h->mb.i_partition = D_8x8;
  905.     /* early termination: if 16x16 chose ref 0, then evalute no refs older
  906.      * than those used by the neighbors */
  907.     if( i_maxref > 0 && a->l0.me16x16.i_ref == 0 &&
  908.         h->mb.i_mb_type_top && h->mb.i_mb_type_left )
  909.     {
  910.         i_maxref = 0;
  911.         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 - 1 ] );
  912.         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 0 ] );
  913.         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 2 ] );
  914.         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 - 8 + 4 ] );
  915.         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 0 - 1 ] );
  916.         i_maxref = X264_MAX( i_maxref, h->mb.cache.ref[0][ X264_SCAN8_0 + 2*8 - 1 ] );
  917.     }
  918.     for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
  919.          *(uint32_t*)a->l0.mvc[i_ref][0] = *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy];
  920.     for( i = 0; i < 4; i++ )
  921.     {
  922.         x264_me_t *l0m = &a->l0.me8x8[i];
  923.         const int x8 = i%2;
  924.         const int y8 = i/2;
  925.         m.i_pixel = PIXEL_8x8;
  926.         m.p_cost_mv = a->p_cost_mv;
  927.         LOAD_FENC( &m, p_fenc, 8*x8, 8*y8 );
  928.         l0m->cost = INT_MAX;
  929.         for( i_ref = 0; i_ref <= i_maxref; i_ref++ )
  930.         {
  931.             const int i_ref_cost = REF_COST( 0, i_ref );
  932.             i_halfpel_thresh -= i_ref_cost;
  933.             m.i_ref_cost = i_ref_cost;
  934.             m.i_ref = i_ref;
  935.             LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*x8, 8*y8 );
  936.             x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
  937.             x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
  938.             x264_me_search_ref( h, &m, a->l0.mvc[i_ref], i+1, p_halfpel_thresh );
  939.             m.cost += i_ref_cost;
  940.             i_halfpel_thresh += i_ref_cost;
  941.             *(uint32_t*)a->l0.mvc[i_ref][i+1] = *(uint32_t*)m.mv;
  942.             if( m.cost < l0m->cost )
  943.                 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
  944.         }
  945.         x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, l0m->mv );
  946.         x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, l0m->i_ref );
  947.         /* mb type cost */
  948.         l0m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
  949.     }
  950.     a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
  951.                       a->l0.me8x8[2].cost + a->l0.me8x8[3].cost;
  952.     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
  953.     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
  954. }
  955. static void x264_mb_analyse_inter_p8x8( x264_t *h, x264_mb_analysis_t *a )
  956. {
  957.     const int i_ref = a->l0.me16x16.i_ref;
  958.     const int i_ref_cost = REF_COST( 0, i_ref );
  959.     uint8_t  **p_fref = h->mb.pic.p_fref[0][i_ref];
  960.     uint8_t  **p_fenc = h->mb.pic.p_fenc;
  961.     int i_mvc;
  962.     int16_t (*mvc)[2] = a->l0.mvc[i_ref];
  963.     int i;
  964.     /* XXX Needed for x264_mb_predict_mv */
  965.     h->mb.i_partition = D_8x8;
  966.     i_mvc = 1;
  967.     *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.me16x16.mv;
  968.     for( i = 0; i < 4; i++ )
  969.     {
  970.         x264_me_t *m = &a->l0.me8x8[i];
  971.         const int x8 = i%2;
  972.         const int y8 = i/2;
  973.         m->i_pixel = PIXEL_8x8;
  974.         m->p_cost_mv = a->p_cost_mv;
  975.         m->i_ref_cost = i_ref_cost;
  976.         m->i_ref = i_ref;
  977.         LOAD_FENC( m, p_fenc, 8*x8, 8*y8 );
  978.         LOAD_HPELS( m, p_fref, 0, i_ref, 8*x8, 8*y8 );
  979.         x264_mb_predict_mv( h, 0, 4*i, 2, m->mvp );
  980.         x264_me_search( h, m, mvc, i_mvc );
  981.         x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, 0, m->mv );
  982.         *(uint32_t*)mvc[i_mvc] = *(uint32_t*)m->mv;
  983.         i_mvc++;
  984.         /* mb type cost */
  985.         m->cost += i_ref_cost;
  986.         m->cost += a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x8];
  987.     }
  988.     /* theoretically this should include 4*ref_cost,
  989.      * but 3 seems a better approximation of cabac. */
  990.     a->l0.i_cost8x8 = a->l0.me8x8[0].cost + a->l0.me8x8[1].cost +
  991.                       a->l0.me8x8[2].cost + a->l0.me8x8[3].cost -
  992.                       REF_COST( 0, a->l0.me16x16.i_ref );
  993.     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
  994.     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
  995. }
  996. static void x264_mb_analyse_inter_p16x8( x264_t *h, x264_mb_analysis_t *a )
  997. {
  998.     x264_me_t m;
  999.     uint8_t  **p_fenc = h->mb.pic.p_fenc;
  1000.     DECLARE_ALIGNED_4( int16_t mvc[3][2] );
  1001.     int i, j;
  1002.     /* XXX Needed for x264_mb_predict_mv */
  1003.     h->mb.i_partition = D_16x8;
  1004.     for( i = 0; i < 2; i++ )
  1005.     {
  1006.         x264_me_t *l0m = &a->l0.me16x8[i];
  1007.         const int ref8[2] = { a->l0.me8x8[2*i].i_ref, a->l0.me8x8[2*i+1].i_ref };
  1008.         const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
  1009.         m.i_pixel = PIXEL_16x8;
  1010.         m.p_cost_mv = a->p_cost_mv;
  1011.         LOAD_FENC( &m, p_fenc, 0, 8*i );
  1012.         l0m->cost = INT_MAX;
  1013.         for( j = 0; j < i_ref8s; j++ )
  1014.         {
  1015.             const int i_ref = ref8[j];
  1016.             const int i_ref_cost = REF_COST( 0, i_ref );
  1017.             m.i_ref_cost = i_ref_cost;
  1018.             m.i_ref = i_ref;
  1019.             /* if we skipped the 16x16 predictor, we wouldn't have to copy anything... */
  1020.             *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
  1021.             *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][2*i+1];
  1022.             *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][2*i+2];
  1023.             LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 8*i );
  1024.             x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, i_ref );
  1025.             x264_mb_predict_mv( h, 0, 8*i, 4, m.mvp );
  1026.             x264_me_search( h, &m, mvc, 3 );
  1027.             m.cost += i_ref_cost;
  1028.             if( m.cost < l0m->cost )
  1029.                 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
  1030.         }
  1031.         x264_macroblock_cache_mv_ptr( h, 0, 2*i, 4, 2, 0, l0m->mv );
  1032.         x264_macroblock_cache_ref( h, 0, 2*i, 4, 2, 0, l0m->i_ref );
  1033.     }
  1034.     a->l0.i_cost16x8 = a->l0.me16x8[0].cost + a->l0.me16x8[1].cost;
  1035. }
  1036. static void x264_mb_analyse_inter_p8x16( x264_t *h, x264_mb_analysis_t *a )
  1037. {
  1038.     x264_me_t m;
  1039.     uint8_t  **p_fenc = h->mb.pic.p_fenc;
  1040.     DECLARE_ALIGNED_4( int16_t mvc[3][2] );
  1041.     int i, j;
  1042.     /* XXX Needed for x264_mb_predict_mv */
  1043.     h->mb.i_partition = D_8x16;
  1044.     for( i = 0; i < 2; i++ )
  1045.     {
  1046.         x264_me_t *l0m = &a->l0.me8x16[i];
  1047.         const int ref8[2] = { a->l0.me8x8[i].i_ref, a->l0.me8x8[i+2].i_ref };
  1048.         const int i_ref8s = ( ref8[0] == ref8[1] ) ? 1 : 2;
  1049.         m.i_pixel = PIXEL_8x16;
  1050.         m.p_cost_mv = a->p_cost_mv;
  1051.         LOAD_FENC( &m, p_fenc, 8*i, 0 );
  1052.         l0m->cost = INT_MAX;
  1053.         for( j = 0; j < i_ref8s; j++ )
  1054.         {
  1055.             const int i_ref = ref8[j];
  1056.             const int i_ref_cost = REF_COST( 0, i_ref );
  1057.             m.i_ref_cost = i_ref_cost;
  1058.             m.i_ref = i_ref;
  1059.             *(uint32_t*)mvc[0] = *(uint32_t*)a->l0.mvc[i_ref][0];
  1060.             *(uint32_t*)mvc[1] = *(uint32_t*)a->l0.mvc[i_ref][i+1];
  1061.             *(uint32_t*)mvc[2] = *(uint32_t*)a->l0.mvc[i_ref][i+3];
  1062.             LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 8*i, 0 );
  1063.             x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, i_ref );
  1064.             x264_mb_predict_mv( h, 0, 4*i, 2, m.mvp );
  1065.             x264_me_search( h, &m, mvc, 3 );
  1066.             m.cost += i_ref_cost;
  1067.             if( m.cost < l0m->cost )
  1068.                 h->mc.memcpy_aligned( l0m, &m, sizeof(x264_me_t) );
  1069.         }
  1070.         x264_macroblock_cache_mv_ptr( h, 2*i, 0, 2, 4, 0, l0m->mv );
  1071.         x264_macroblock_cache_ref( h, 2*i, 0, 2, 4, 0, l0m->i_ref );
  1072.     }
  1073.     a->l0.i_cost8x16 = a->l0.me8x16[0].cost + a->l0.me8x16[1].cost;
  1074. }
  1075. static int x264_mb_analyse_inter_p4x4_chroma( x264_t *h, x264_mb_analysis_t *a, uint8_t **p_fref, int i8x8, int pixel )
  1076. {
  1077.     DECLARE_ALIGNED_8( uint8_t pix1[16*8] );
  1078.     uint8_t *pix2 = pix1+8;
  1079.     const int i_stride = h->mb.pic.i_stride[1];
  1080.     const int or = 4*(i8x8&1) + 2*(i8x8&2)*i_stride;
  1081.     const int oe = 4*(i8x8&1) + 2*(i8x8&2)*FENC_STRIDE;
  1082. #define CHROMA4x4MC( width, height, me, x, y ) 
  1083.     h->mc.mc_chroma( &pix1[x+y*16], 16, &p_fref[4][or+x+y*i_stride], i_stride, (me).mv[0], (me).mv[1], width, height ); 
  1084.     h->mc.mc_chroma( &pix2[x+y*16], 16, &p_fref[5][or+x+y*i_stride], i_stride, (me).mv[0], (me).mv[1], width, height );
  1085.     if( pixel == PIXEL_4x4 )
  1086.     {
  1087.         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][0], 0,0 );
  1088.         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][1], 2,0 );
  1089.         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][2], 0,2 );
  1090.         CHROMA4x4MC( 2,2, a->l0.me4x4[i8x8][3], 2,2 );
  1091.     }
  1092.     else if( pixel == PIXEL_8x4 )
  1093.     {
  1094.         CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][0], 0,0 );
  1095.         CHROMA4x4MC( 4,2, a->l0.me8x4[i8x8][1], 0,2 );
  1096.     }
  1097.     else
  1098.     {
  1099.         CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][0], 0,0 );
  1100.         CHROMA4x4MC( 2,4, a->l0.me4x8[i8x8][1], 2,0 );
  1101.     }
  1102.     return h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[1][oe], FENC_STRIDE, pix1, 16 )
  1103.          + h->pixf.mbcmp[PIXEL_4x4]( &h->mb.pic.p_fenc[2][oe], FENC_STRIDE, pix2, 16 );
  1104. }
  1105. static void x264_mb_analyse_inter_p4x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
  1106. {
  1107.     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
  1108.     uint8_t  **p_fenc = h->mb.pic.p_fenc;
  1109.     const int i_ref = a->l0.me8x8[i8x8].i_ref;
  1110.     int i4x4;
  1111.     /* XXX Needed for x264_mb_predict_mv */
  1112.     h->mb.i_partition = D_8x8;
  1113.     for( i4x4 = 0; i4x4 < 4; i4x4++ )
  1114.     {
  1115.         const int idx = 4*i8x8 + i4x4;
  1116.         const int x4 = block_idx_x[idx];
  1117.         const int y4 = block_idx_y[idx];
  1118.         const int i_mvc = (i4x4 == 0);
  1119.         x264_me_t *m = &a->l0.me4x4[i8x8][i4x4];
  1120.         m->i_pixel = PIXEL_4x4;
  1121.         m->p_cost_mv = a->p_cost_mv;
  1122.         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
  1123.         LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
  1124.         x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
  1125.         x264_me_search( h, m, &a->l0.me8x8[i8x8].mv, i_mvc );
  1126.         x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 1, 0, m->mv );
  1127.     }
  1128.     a->l0.i_cost4x4[i8x8] = a->l0.me4x4[i8x8][0].cost +
  1129.                             a->l0.me4x4[i8x8][1].cost +
  1130.                             a->l0.me4x4[i8x8][2].cost +
  1131.                             a->l0.me4x4[i8x8][3].cost +
  1132.                             REF_COST( 0, i_ref ) +
  1133.                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x4];
  1134.     if( h->mb.b_chroma_me )
  1135.         a->l0.i_cost4x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x4 );
  1136. }
  1137. static void x264_mb_analyse_inter_p8x4( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
  1138. {
  1139.     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
  1140.     uint8_t  **p_fenc = h->mb.pic.p_fenc;
  1141.     const int i_ref = a->l0.me8x8[i8x8].i_ref;
  1142.     int i8x4;
  1143.     /* XXX Needed for x264_mb_predict_mv */
  1144.     h->mb.i_partition = D_8x8;
  1145.     for( i8x4 = 0; i8x4 < 2; i8x4++ )
  1146.     {
  1147.         const int idx = 4*i8x8 + 2*i8x4;
  1148.         const int x4 = block_idx_x[idx];
  1149.         const int y4 = block_idx_y[idx];
  1150.         const int i_mvc = (i8x4 == 0);
  1151.         x264_me_t *m = &a->l0.me8x4[i8x8][i8x4];
  1152.         m->i_pixel = PIXEL_8x4;
  1153.         m->p_cost_mv = a->p_cost_mv;
  1154.         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
  1155.         LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
  1156.         x264_mb_predict_mv( h, 0, idx, 2, m->mvp );
  1157.         x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
  1158.         x264_macroblock_cache_mv_ptr( h, x4, y4, 2, 1, 0, m->mv );
  1159.     }
  1160.     a->l0.i_cost8x4[i8x8] = a->l0.me8x4[i8x8][0].cost + a->l0.me8x4[i8x8][1].cost +
  1161.                             REF_COST( 0, i_ref ) +
  1162.                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_8x4];
  1163.     if( h->mb.b_chroma_me )
  1164.         a->l0.i_cost8x4[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_8x4 );
  1165. }
  1166. static void x264_mb_analyse_inter_p4x8( x264_t *h, x264_mb_analysis_t *a, int i8x8 )
  1167. {
  1168.     uint8_t  **p_fref = h->mb.pic.p_fref[0][a->l0.me8x8[i8x8].i_ref];
  1169.     uint8_t  **p_fenc = h->mb.pic.p_fenc;
  1170.     const int i_ref = a->l0.me8x8[i8x8].i_ref;
  1171.     int i4x8;
  1172.     /* XXX Needed for x264_mb_predict_mv */
  1173.     h->mb.i_partition = D_8x8;
  1174.     for( i4x8 = 0; i4x8 < 2; i4x8++ )
  1175.     {
  1176.         const int idx = 4*i8x8 + i4x8;
  1177.         const int x4 = block_idx_x[idx];
  1178.         const int y4 = block_idx_y[idx];
  1179.         const int i_mvc = (i4x8 == 0);
  1180.         x264_me_t *m = &a->l0.me4x8[i8x8][i4x8];
  1181.         m->i_pixel = PIXEL_4x8;
  1182.         m->p_cost_mv = a->p_cost_mv;
  1183.         LOAD_FENC( m, p_fenc, 4*x4, 4*y4 );
  1184.         LOAD_HPELS( m, p_fref, 0, i_ref, 4*x4, 4*y4 );
  1185.         x264_mb_predict_mv( h, 0, idx, 1, m->mvp );
  1186.         x264_me_search( h, m, &a->l0.me4x4[i8x8][0].mv, i_mvc );
  1187.         x264_macroblock_cache_mv_ptr( h, x4, y4, 1, 2, 0, m->mv );
  1188.     }
  1189.     a->l0.i_cost4x8[i8x8] = a->l0.me4x8[i8x8][0].cost + a->l0.me4x8[i8x8][1].cost +
  1190.                             REF_COST( 0, i_ref ) +
  1191.                             a->i_lambda * i_sub_mb_p_cost_table[D_L0_4x8];
  1192.     if( h->mb.b_chroma_me )
  1193.         a->l0.i_cost4x8[i8x8] += x264_mb_analyse_inter_p4x4_chroma( h, a, p_fref, i8x8, PIXEL_4x8 );
  1194. }
  1195. static void x264_mb_analyse_inter_direct( x264_t *h, x264_mb_analysis_t *a )
  1196. {
  1197.     /* Assumes that fdec still contains the results of
  1198.      * x264_mb_predict_mv_direct16x16 and x264_mb_mc */
  1199.     uint8_t **p_fenc = h->mb.pic.p_fenc;
  1200.     uint8_t **p_fdec = h->mb.pic.p_fdec;
  1201.     int i;
  1202.     a->i_cost16x16direct = a->i_lambda * i_mb_b_cost_table[B_DIRECT];
  1203.     for( i = 0; i < 4; i++ )
  1204.     {
  1205.         const int x = (i&1)*8;
  1206.         const int y = (i>>1)*8;
  1207.         a->i_cost16x16direct +=
  1208.         a->i_cost8x8direct[i] =
  1209.             h->pixf.mbcmp[PIXEL_8x8]( &p_fenc[0][x+y*FENC_STRIDE], FENC_STRIDE, &p_fdec[0][x+y*FDEC_STRIDE], FDEC_STRIDE );
  1210.         /* mb type cost */
  1211.         a->i_cost8x8direct[i] += a->i_lambda * i_sub_mb_b_cost_table[D_DIRECT_8x8];
  1212.     }
  1213. }
  1214. #define WEIGHTED_AVG( size, pix1, stride1, src2, stride2 ) 
  1215.     { 
  1216.         if( h->param.analyse.b_weighted_bipred ) 
  1217.             h->mc.avg_weight[size]( pix1, stride1, src2, stride2, 
  1218.                     h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref] ); 
  1219.         else 
  1220.             h->mc.avg[size]( pix1, stride1, src2, stride2 ); 
  1221.     }
  1222. static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a )
  1223. {
  1224.     DECLARE_ALIGNED_16( uint8_t pix1[16*16] );
  1225.     DECLARE_ALIGNED_16( uint8_t pix2[16*16] );
  1226.     uint8_t *src2;
  1227.     int stride2 = 16;
  1228.     int weight;
  1229.     x264_me_t m;
  1230.     int i_ref, i_mvc;
  1231.     DECLARE_ALIGNED_4( int16_t mvc[8][2] );
  1232.     int i_halfpel_thresh = INT_MAX;
  1233.     int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL;
  1234.     /* 16x16 Search on all ref frame */
  1235.     m.i_pixel = PIXEL_16x16;
  1236.     m.p_cost_mv = a->p_cost_mv;
  1237.     LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
  1238.     /* ME for List 0 */
  1239.     a->l0.me16x16.cost = INT_MAX;
  1240.     for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
  1241.     {
  1242.         /* search with ref */
  1243.         LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
  1244.         x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
  1245.         x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
  1246.         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
  1247.         /* add ref cost */
  1248.         m.cost += REF_COST( 0, i_ref );
  1249.         if( m.cost < a->l0.me16x16.cost )
  1250.         {
  1251.             a->l0.i_ref = i_ref;
  1252.             h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
  1253.         }
  1254.         /* save mv for predicting neighbors */
  1255.         *(uint32_t*)h->mb.mvr[0][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
  1256.     }
  1257.     /* subtract ref cost, so we don't have to add it for the other MB types */
  1258.     a->l0.me16x16.cost -= REF_COST( 0, a->l0.i_ref );
  1259.     /* ME for list 1 */
  1260.     i_halfpel_thresh = INT_MAX;
  1261.     p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL;
  1262.     a->l1.me16x16.cost = INT_MAX;
  1263.     for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ )
  1264.     {
  1265.         /* search with ref */
  1266.         LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 );
  1267.         x264_mb_predict_mv_16x16( h, 1, i_ref, m.mvp );
  1268.         x264_mb_predict_mv_ref16x16( h, 1, i_ref, mvc, &i_mvc );
  1269.         x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
  1270.         /* add ref cost */
  1271.         m.cost += REF_COST( 1, i_ref );
  1272.         if( m.cost < a->l1.me16x16.cost )
  1273.         {
  1274.             a->l1.i_ref = i_ref;
  1275.             h->mc.memcpy_aligned( &a->l1.me16x16, &m, sizeof(x264_me_t) );
  1276.         }
  1277.         /* save mv for predicting neighbors */
  1278.         *(uint32_t*)h->mb.mvr[1][i_ref][h->mb.i_mb_xy] = *(uint32_t*)m.mv;
  1279.     }
  1280.     /* subtract ref cost, so we don't have to add it for the other MB types */
  1281.     a->l1.me16x16.cost -= REF_COST( 1, a->l1.i_ref );
  1282.     /* Set global ref, needed for other modes? */
  1283.     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
  1284.     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
  1285.     /* get cost of BI mode */
  1286.     weight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
  1287.     if ( (*(uint32_t*)a->l0.me16x16.mv & 0x10001) == 0 )
  1288.     {
  1289.         /* l0 reference is halfpel, so get_ref on it will make it faster */
  1290.         src2 =
  1291.         h->mc.get_ref( pix2, &stride2,
  1292.                        h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
  1293.                        a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
  1294.                        16, 16 );
  1295.         h->mc.mc_luma( pix1, 16,
  1296.                        h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
  1297.                        a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
  1298.                        16, 16 );
  1299.         weight = 64 - weight;
  1300.     }
  1301.     else
  1302.     {
  1303.         /* if l0 was qpel, we'll use get_ref on l1 instead */
  1304.         h->mc.mc_luma( pix1, 16,
  1305.                        h->mb.pic.p_fref[0][a->l0.i_ref], h->mb.pic.i_stride[0],
  1306.                        a->l0.me16x16.mv[0], a->l0.me16x16.mv[1],
  1307.                        16, 16 );
  1308.         src2 =
  1309.         h->mc.get_ref( pix2, &stride2,
  1310.                        h->mb.pic.p_fref[1][a->l1.i_ref], h->mb.pic.i_stride[0],
  1311.                        a->l1.me16x16.mv[0], a->l1.me16x16.mv[1],
  1312.                        16, 16 );
  1313.     }
  1314.     if( h->param.analyse.b_weighted_bipred )
  1315.         h->mc.avg_weight[PIXEL_16x16]( pix1, 16, src2, stride2, weight );
  1316.     else
  1317.         h->mc.avg[PIXEL_16x16]( pix1, 16, src2, stride2 );
  1318.     a->i_cost16x16bi = h->pixf.mbcmp[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE, pix1, 16 )
  1319.                      + REF_COST( 0, a->l0.i_ref )
  1320.                      + REF_COST( 1, a->l1.i_ref )
  1321.                      + a->l0.me16x16.cost_mv
  1322.                      + a->l1.me16x16.cost_mv;
  1323.     /* mb type cost */
  1324.     a->i_cost16x16bi   += a->i_lambda * i_mb_b_cost_table[B_BI_BI];
  1325.     a->l0.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L0_L0];
  1326.     a->l1.me16x16.cost += a->i_lambda * i_mb_b_cost_table[B_L1_L1];
  1327. }
  1328. static inline void x264_mb_cache_mv_p8x8( x264_t *h, x264_mb_analysis_t *a, int i )
  1329. {
  1330.     const int x = 2*(i%2);
  1331.     const int y = 2*(i/2);
  1332.     switch( h->mb.i_sub_partition[i] )
  1333.     {
  1334.         case D_L0_8x8:
  1335.             x264_macroblock_cache_mv_ptr( h, x, y, 2, 2, 0, a->l0.me8x8[i].mv );
  1336.             break;
  1337.         case D_L0_8x4:
  1338.             x264_macroblock_cache_mv_ptr( h, x, y+0, 2, 1, 0, a->l0.me8x4[i][0].mv );
  1339.             x264_macroblock_cache_mv_ptr( h, x, y+1, 2, 1, 0, a->l0.me8x4[i][1].mv );
  1340.             break;
  1341.         case D_L0_4x8:
  1342.             x264_macroblock_cache_mv_ptr( h, x+0, y, 1, 2, 0, a->l0.me4x8[i][0].mv );
  1343.             x264_macroblock_cache_mv_ptr( h, x+1, y, 1, 2, 0, a->l0.me4x8[i][1].mv );
  1344.             break;
  1345.         case D_L0_4x4:
  1346.             x264_macroblock_cache_mv_ptr( h, x+0, y+0, 1, 1, 0, a->l0.me4x4[i][0].mv );
  1347.             x264_macroblock_cache_mv_ptr( h, x+1, y+0, 1, 1, 0, a->l0.me4x4[i][1].mv );
  1348.             x264_macroblock_cache_mv_ptr( h, x+0, y+1, 1, 1, 0, a->l0.me4x4[i][2].mv );
  1349.             x264_macroblock_cache_mv_ptr( h, x+1, y+1, 1, 1, 0, a->l0.me4x4[i][3].mv );
  1350.             break;
  1351.         default:
  1352.             x264_log( h, X264_LOG_ERROR, "internal errorn" );
  1353.             break;
  1354.     }
  1355. }
  1356. #define CACHE_MV_BI(x,y,dx,dy,me0,me1,part) 
  1357.     if( x264_mb_partition_listX_table[0][part] ) 
  1358.     { 
  1359.         x264_macroblock_cache_ref( h, x,y,dx,dy, 0, a->l0.i_ref ); 
  1360.         x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 0, me0.mv ); 
  1361.     } 
  1362.     else 
  1363.     { 
  1364.         x264_macroblock_cache_ref( h, x,y,dx,dy, 0, -1 ); 
  1365.         x264_macroblock_cache_mv(  h, x,y,dx,dy, 0, 0 ); 
  1366.         if( b_mvd ) 
  1367.             x264_macroblock_cache_mvd( h, x,y,dx,dy, 0, 0 ); 
  1368.     } 
  1369.     if( x264_mb_partition_listX_table[1][part] ) 
  1370.     { 
  1371.         x264_macroblock_cache_ref( h, x,y,dx,dy, 1, a->l1.i_ref ); 
  1372.         x264_macroblock_cache_mv_ptr( h, x,y,dx,dy, 1, me1.mv ); 
  1373.     } 
  1374.     else 
  1375.     { 
  1376.         x264_macroblock_cache_ref( h, x,y,dx,dy, 1, -1 ); 
  1377.         x264_macroblock_cache_mv(  h, x,y,dx,dy, 1, 0 ); 
  1378.         if( b_mvd ) 
  1379.             x264_macroblock_cache_mvd( h, x,y,dx,dy, 1, 0 ); 
  1380.     }
  1381. static inline void x264_mb_cache_mv_b8x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
  1382. {
  1383.     int x = (i%2)*2;
  1384.     int y = (i/2)*2;
  1385.     if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
  1386.     {
  1387.         x264_mb_load_mv_direct8x8( h, i );
  1388.         if( b_mvd )
  1389.         {
  1390.             x264_macroblock_cache_mvd(  h, x, y, 2, 2, 0, 0 );
  1391.             x264_macroblock_cache_mvd(  h, x, y, 2, 2, 1, 0 );
  1392.             x264_macroblock_cache_skip( h, x, y, 2, 2, 1 );
  1393.         }
  1394.     }
  1395.     else
  1396.     {
  1397.         CACHE_MV_BI( x, y, 2, 2, a->l0.me8x8[i], a->l1.me8x8[i], h->mb.i_sub_partition[i] );
  1398.     }
  1399. }
  1400. static inline void x264_mb_cache_mv_b16x8( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
  1401. {
  1402.     CACHE_MV_BI( 0, 2*i, 4, 2, a->l0.me16x8[i], a->l1.me16x8[i], a->i_mb_partition16x8[i] );
  1403. }
  1404. static inline void x264_mb_cache_mv_b8x16( x264_t *h, x264_mb_analysis_t *a, int i, int b_mvd )
  1405. {
  1406.     CACHE_MV_BI( 2*i, 0, 2, 4, a->l0.me8x16[i], a->l1.me8x16[i], a->i_mb_partition8x16[i] );
  1407. }
  1408. #undef CACHE_MV_BI
  1409. static void x264_mb_analyse_inter_b8x8( x264_t *h, x264_mb_analysis_t *a )
  1410. {
  1411.     uint8_t **p_fref[2] =
  1412.         { h->mb.pic.p_fref[0][a->l0.i_ref],
  1413.           h->mb.pic.p_fref[1][a->l1.i_ref] };
  1414.     DECLARE_ALIGNED_8( uint8_t pix[2][8*8] );
  1415.     int i, l;
  1416.     /* XXX Needed for x264_mb_predict_mv */
  1417.     h->mb.i_partition = D_8x8;
  1418.     a->i_cost8x8bi = 0;
  1419.     for( i = 0; i < 4; i++ )
  1420.     {
  1421.         const int x8 = i%2;
  1422.         const int y8 = i/2;
  1423.         int i_part_cost;
  1424.         int i_part_cost_bi = 0;
  1425.         for( l = 0; l < 2; l++ )
  1426.         {
  1427.             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
  1428.             x264_me_t *m = &lX->me8x8[i];
  1429.             m->i_pixel = PIXEL_8x8;
  1430.             m->p_cost_mv = a->p_cost_mv;
  1431.             LOAD_FENC( m, h->mb.pic.p_fenc, 8*x8, 8*y8 );
  1432.             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*x8, 8*y8 );
  1433.             x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
  1434.             x264_me_search( h, m, &lX->me16x16.mv, 1 );
  1435.             x264_macroblock_cache_mv_ptr( h, 2*x8, 2*y8, 2, 2, l, m->mv );
  1436.             /* BI mode */
  1437.             h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
  1438.                            m->mv[0], m->mv[1], 8, 8 );
  1439.             i_part_cost_bi += m->cost_mv;
  1440.             /* FIXME: ref cost */
  1441.         }
  1442.         WEIGHTED_AVG( PIXEL_8x8, pix[0], 8, pix[1], 8 );
  1443.         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x8]( a->l0.me8x8[i].p_fenc[0], FENC_STRIDE, pix[0], 8 )
  1444.                         + a->i_lambda * i_sub_mb_b_cost_table[D_BI_8x8];
  1445.         a->l0.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
  1446.         a->l1.me8x8[i].cost += a->i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
  1447.         i_part_cost = a->l0.me8x8[i].cost;
  1448.         h->mb.i_sub_partition[i] = D_L0_8x8;
  1449.         COPY2_IF_LT( i_part_cost, a->l1.me8x8[i].cost, h->mb.i_sub_partition[i], D_L1_8x8 );
  1450.         COPY2_IF_LT( i_part_cost, i_part_cost_bi, h->mb.i_sub_partition[i], D_BI_8x8 );
  1451.         COPY2_IF_LT( i_part_cost, a->i_cost8x8direct[i], h->mb.i_sub_partition[i], D_DIRECT_8x8 );
  1452.         a->i_cost8x8bi += i_part_cost;
  1453.         /* XXX Needed for x264_mb_predict_mv */
  1454.         x264_mb_cache_mv_b8x8( h, a, i, 0 );
  1455.     }
  1456.     /* mb type cost */
  1457.     a->i_cost8x8bi += a->i_lambda * i_mb_b_cost_table[B_8x8];
  1458. }
  1459. static void x264_mb_analyse_inter_b16x8( x264_t *h, x264_mb_analysis_t *a )
  1460. {
  1461.     uint8_t **p_fref[2] =
  1462.         { h->mb.pic.p_fref[0][a->l0.i_ref],
  1463.           h->mb.pic.p_fref[1][a->l1.i_ref] };
  1464.     DECLARE_ALIGNED_16( uint8_t  pix[2][16*8] );
  1465.     DECLARE_ALIGNED_4( int16_t mvc[2][2] );
  1466.     int i, l;
  1467.     h->mb.i_partition = D_16x8;
  1468.     a->i_cost16x8bi = 0;
  1469.     for( i = 0; i < 2; i++ )
  1470.     {
  1471.         int i_part_cost;
  1472.         int i_part_cost_bi = 0;
  1473.         /* TODO: check only the list(s) that were used in b8x8? */
  1474.         for( l = 0; l < 2; l++ )
  1475.         {
  1476.             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
  1477.             x264_me_t *m = &lX->me16x8[i];
  1478.             m->i_pixel = PIXEL_16x8;
  1479.             m->p_cost_mv = a->p_cost_mv;
  1480.             LOAD_FENC( m, h->mb.pic.p_fenc, 0, 8*i );
  1481.             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 0, 8*i );
  1482.             *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[2*i].mv;
  1483.             *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[2*i+1].mv;
  1484.             x264_mb_predict_mv( h, l, 8*i, 2, m->mvp );
  1485.             x264_me_search( h, m, mvc, 2 );
  1486.             /* BI mode */
  1487.             h->mc.mc_luma( pix[l], 16, m->p_fref, m->i_stride[0],
  1488.                            m->mv[0], m->mv[1], 16, 8 );
  1489.             /* FIXME: ref cost */
  1490.             i_part_cost_bi += m->cost_mv;
  1491.         }
  1492.         WEIGHTED_AVG( PIXEL_16x8, pix[0], 16, pix[1], 16 );
  1493.         i_part_cost_bi += h->pixf.mbcmp[PIXEL_16x8]( a->l0.me16x8[i].p_fenc[0], FENC_STRIDE, pix[0], 16 );
  1494.         i_part_cost = a->l0.me16x8[i].cost;
  1495.         a->i_mb_partition16x8[i] = D_L0_8x8; /* not actually 8x8, only the L0 matters */
  1496.         if( a->l1.me16x8[i].cost < i_part_cost )
  1497.         {
  1498.             i_part_cost = a->l1.me16x8[i].cost;
  1499.             a->i_mb_partition16x8[i] = D_L1_8x8;
  1500.         }
  1501.         if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
  1502.         {
  1503.             i_part_cost = i_part_cost_bi;
  1504.             a->i_mb_partition16x8[i] = D_BI_8x8;
  1505.         }
  1506.         a->i_cost16x8bi += i_part_cost;
  1507.         x264_mb_cache_mv_b16x8( h, a, i, 0 );
  1508.     }
  1509.     /* mb type cost */
  1510.     a->i_mb_type16x8 = B_L0_L0
  1511.         + (a->i_mb_partition16x8[0]>>2) * 3
  1512.         + (a->i_mb_partition16x8[1]>>2);
  1513.     a->i_cost16x8bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type16x8];
  1514. }
  1515. static void x264_mb_analyse_inter_b8x16( x264_t *h, x264_mb_analysis_t *a )
  1516. {
  1517.     uint8_t **p_fref[2] =
  1518.         { h->mb.pic.p_fref[0][a->l0.i_ref],
  1519.           h->mb.pic.p_fref[1][a->l1.i_ref] };
  1520.     DECLARE_ALIGNED_8( uint8_t pix[2][8*16] );
  1521.     DECLARE_ALIGNED_4( int16_t mvc[2][2] );
  1522.     int i, l;
  1523.     h->mb.i_partition = D_8x16;
  1524.     a->i_cost8x16bi = 0;
  1525.     for( i = 0; i < 2; i++ )
  1526.     {
  1527.         int i_part_cost;
  1528.         int i_part_cost_bi = 0;
  1529.         for( l = 0; l < 2; l++ )
  1530.         {
  1531.             x264_mb_analysis_list_t *lX = l ? &a->l1 : &a->l0;
  1532.             x264_me_t *m = &lX->me8x16[i];
  1533.             m->i_pixel = PIXEL_8x16;
  1534.             m->p_cost_mv = a->p_cost_mv;
  1535.             LOAD_FENC( m, h->mb.pic.p_fenc, 8*i, 0 );
  1536.             LOAD_HPELS( m, p_fref[l], l, lX->i_ref, 8*i, 0 );
  1537.             *(uint32_t*)mvc[0] = *(uint32_t*)lX->me8x8[i].mv;
  1538.             *(uint32_t*)mvc[1] = *(uint32_t*)lX->me8x8[i+2].mv;
  1539.             x264_mb_predict_mv( h, l, 4*i, 2, m->mvp );
  1540.             x264_me_search( h, m, mvc, 2 );
  1541.             /* BI mode */
  1542.             h->mc.mc_luma( pix[l], 8, m->p_fref, m->i_stride[0],
  1543.                            m->mv[0], m->mv[1], 8, 16 );
  1544.             /* FIXME: ref cost */
  1545.             i_part_cost_bi += m->cost_mv;
  1546.         }
  1547.         WEIGHTED_AVG( PIXEL_8x16, pix[0], 8, pix[1], 8 );
  1548.         i_part_cost_bi += h->pixf.mbcmp[PIXEL_8x16]( a->l0.me8x16[i].p_fenc[0], FENC_STRIDE, pix[0], 8 );
  1549.         i_part_cost = a->l0.me8x16[i].cost;
  1550.         a->i_mb_partition8x16[i] = D_L0_8x8;
  1551.         if( a->l1.me8x16[i].cost < i_part_cost )
  1552.         {
  1553.             i_part_cost = a->l1.me8x16[i].cost;
  1554.             a->i_mb_partition8x16[i] = D_L1_8x8;
  1555.         }
  1556.         if( i_part_cost_bi + a->i_lambda * 1 < i_part_cost )
  1557.         {
  1558.             i_part_cost = i_part_cost_bi;
  1559.             a->i_mb_partition8x16[i] = D_BI_8x8;
  1560.         }
  1561.         a->i_cost8x16bi += i_part_cost;
  1562.         x264_mb_cache_mv_b8x16( h, a, i, 0 );
  1563.     }
  1564.     /* mb type cost */
  1565.     a->i_mb_type8x16 = B_L0_L0
  1566.         + (a->i_mb_partition8x16[0]>>2) * 3
  1567.         + (a->i_mb_partition8x16[1]>>2);
  1568.     a->i_cost8x16bi += a->i_lambda * i_mb_b16x8_cost_table[a->i_mb_type8x16];
  1569. }
  1570. static void x264_mb_analyse_p_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd )
  1571. {
  1572.     int thresh = i_satd * 5/4;
  1573.     h->mb.i_type = P_L0;
  1574.     if( a->l0.i_rd16x16 == COST_MAX && a->l0.me16x16.cost <= i_satd * 3/2 )
  1575.     {
  1576.         h->mb.i_partition = D_16x16;
  1577.         x264_analyse_update_cache( h, a );
  1578.         a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
  1579.     }
  1580.     a->l0.me16x16.cost = a->l0.i_rd16x16;
  1581.     if( a->l0.i_cost16x8 <= thresh )
  1582.     {
  1583.         h->mb.i_partition = D_16x8;
  1584.         x264_analyse_update_cache( h, a );
  1585.         a->l0.i_cost16x8 = x264_rd_cost_mb( h, a->i_lambda2 );
  1586.     }
  1587.     else
  1588.         a->l0.i_cost16x8 = COST_MAX;
  1589.     if( a->l0.i_cost8x16 <= thresh )
  1590.     {
  1591.         h->mb.i_partition = D_8x16;
  1592.         x264_analyse_update_cache( h, a );
  1593.         a->l0.i_cost8x16 = x264_rd_cost_mb( h, a->i_lambda2 );
  1594.     }
  1595.     else
  1596.         a->l0.i_cost8x16 = COST_MAX;
  1597.     if( a->l0.i_cost8x8 <= thresh )
  1598.     {
  1599.         h->mb.i_type = P_8x8;
  1600.         h->mb.i_partition = D_8x8;
  1601.         x264_analyse_update_cache( h, a );
  1602.         a->l0.i_cost8x8 = x264_rd_cost_mb( h, a->i_lambda2 );
  1603.         if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
  1604.         {
  1605.             /* FIXME: RD per subpartition */
  1606.             int part_bak[4];
  1607.             int i, i_cost;
  1608.             int b_sub8x8 = 0;
  1609.             for( i=0; i<4; i++ )
  1610.             {
  1611.                 part_bak[i] = h->mb.i_sub_partition[i];
  1612.                 b_sub8x8 |= (part_bak[i] != D_L0_8x8);
  1613.             }
  1614.             if( b_sub8x8 )
  1615.             {
  1616.                 h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
  1617.                 h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
  1618.                 x264_analyse_update_cache( h, a );
  1619.                 i_cost = x264_rd_cost_mb( h, a->i_lambda2 );
  1620.                 if( a->l0.i_cost8x8 < i_cost )
  1621.                 {
  1622.                     for( i=0; i<4; i++ )
  1623.                         h->mb.i_sub_partition[i] = part_bak[i];
  1624.                 }
  1625.                 else
  1626.                    a->l0.i_cost8x8 = i_cost;
  1627.             }
  1628.         }
  1629.     }
  1630.     else
  1631.         a->l0.i_cost8x8 = COST_MAX;
  1632. }
  1633. static void x264_mb_analyse_b_rd( x264_t *h, x264_mb_analysis_t *a, int i_satd_inter )
  1634. {
  1635.     int thresh = i_satd_inter * 17/16;
  1636.     if( a->b_direct_available && a->i_rd16x16direct == COST_MAX )
  1637.     {
  1638.         h->mb.i_type = B_DIRECT;
  1639.         /* Assumes direct/skip MC is still in fdec */
  1640.         /* Requires b-rdo to be done before intra analysis */
  1641.         h->mb.b_skip_mc = 1;
  1642.         x264_analyse_update_cache( h, a );
  1643.         a->i_rd16x16direct = x264_rd_cost_mb( h, a->i_lambda2 );
  1644.         h->mb.b_skip_mc = 0;
  1645.     }
  1646.     //FIXME not all the update_cache calls are needed
  1647.     h->mb.i_partition = D_16x16;
  1648.     /* L0 */
  1649.     if( a->l0.me16x16.cost <= thresh && a->l0.i_rd16x16 == COST_MAX )
  1650.     {
  1651.         h->mb.i_type = B_L0_L0;
  1652.         x264_analyse_update_cache( h, a );
  1653.         a->l0.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
  1654.     }
  1655.     /* L1 */
  1656.     if( a->l1.me16x16.cost <= thresh && a->l1.i_rd16x16 == COST_MAX )
  1657.     {
  1658.         h->mb.i_type = B_L1_L1;
  1659.         x264_analyse_update_cache( h, a );
  1660.         a->l1.i_rd16x16 = x264_rd_cost_mb( h, a->i_lambda2 );
  1661.     }
  1662.     /* BI */
  1663.     if( a->i_cost16x16bi <= thresh && a->i_rd16x16bi == COST_MAX )
  1664.     {
  1665.         h->mb.i_type = B_BI_BI;
  1666.         x264_analyse_update_cache( h, a );
  1667.         a->i_rd16x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
  1668.     }
  1669.     /* 8x8 */
  1670.     if( a->i_cost8x8bi <= thresh && a->i_rd8x8bi == COST_MAX )
  1671.     {
  1672.         h->mb.i_type = B_8x8;
  1673.         h->mb.i_partition = D_8x8;
  1674.         x264_analyse_update_cache( h, a );
  1675.         a->i_rd8x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
  1676.         x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
  1677.     }
  1678.     /* 16x8 */
  1679.     if( a->i_cost16x8bi <= thresh && a->i_rd16x8bi == COST_MAX )
  1680.     {
  1681.         h->mb.i_type = a->i_mb_type16x8;
  1682.         h->mb.i_partition = D_16x8;
  1683.         x264_analyse_update_cache( h, a );
  1684.         a->i_rd16x8bi = x264_rd_cost_mb( h, a->i_lambda2 );
  1685.     }
  1686.     /* 8x16 */
  1687.     if( a->i_cost8x16bi <= thresh && a->i_rd8x16bi == COST_MAX )
  1688.     {
  1689.         h->mb.i_type = a->i_mb_type8x16;
  1690.         h->mb.i_partition = D_8x16;
  1691.         x264_analyse_update_cache( h, a );
  1692.         a->i_rd8x16bi = x264_rd_cost_mb( h, a->i_lambda2 );
  1693.     }
  1694. }
  1695. static void refine_bidir( x264_t *h, x264_mb_analysis_t *a )
  1696. {
  1697.     const int i_biweight = h->mb.bipred_weight[a->l0.i_ref][a->l1.i_ref];
  1698.     int i;
  1699.     switch( h->mb.i_partition )
  1700.     {
  1701.     case D_16x16:
  1702.         if( h->mb.i_type == B_BI_BI )
  1703.             x264_me_refine_bidir( h, &a->l0.me16x16, &a->l1.me16x16, i_biweight );
  1704.         break;
  1705.     case D_16x8:
  1706.         for( i=0; i<2; i++ )
  1707.             if( a->i_mb_partition16x8[i] == D_BI_8x8 )
  1708.                 x264_me_refine_bidir( h, &a->l0.me16x8[i], &a->l1.me16x8[i], i_biweight );
  1709.         break;
  1710.     case D_8x16:
  1711.         for( i=0; i<2; i++ )
  1712.             if( a->i_mb_partition8x16[i] == D_BI_8x8 )
  1713.                 x264_me_refine_bidir( h, &a->l0.me8x16[i], &a->l1.me8x16[i], i_biweight );
  1714.         break;
  1715.     case D_8x8:
  1716.         for( i=0; i<4; i++ )
  1717.             if( h->mb.i_sub_partition[i] == D_BI_8x8 )
  1718.                 x264_me_refine_bidir( h, &a->l0.me8x8[i], &a->l1.me8x8[i], i_biweight );
  1719.         break;
  1720.     }
  1721. }
  1722. static inline void x264_mb_analyse_transform( x264_t *h )
  1723. {
  1724.     if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
  1725.     {
  1726.         int i_cost4, i_cost8;
  1727.         /* Only luma MC is really needed, but the full MC is re-used in macroblock_encode. */
  1728.         x264_mb_mc( h );
  1729.         i_cost8 = h->pixf.sa8d[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
  1730.                                              h->mb.pic.p_fdec[0], FDEC_STRIDE );
  1731.         i_cost4 = h->pixf.satd[PIXEL_16x16]( h->mb.pic.p_fenc[0], FENC_STRIDE,
  1732.                                              h->mb.pic.p_fdec[0], FDEC_STRIDE );
  1733.         h->mb.b_transform_8x8 = i_cost8 < i_cost4;
  1734.         h->mb.b_skip_mc = 1;
  1735.     }
  1736. }
  1737. static inline void x264_mb_analyse_transform_rd( x264_t *h, x264_mb_analysis_t *a, int *i_satd, int *i_rd )
  1738. {
  1739.     if( x264_mb_transform_8x8_allowed( h ) && h->param.analyse.b_transform_8x8 )
  1740.     {
  1741.         int i_rd8;
  1742.         x264_analyse_update_cache( h, a );
  1743.         h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
  1744.         /* FIXME only luma is needed, but the score for comparison already includes chroma */
  1745.         i_rd8 = x264_rd_cost_mb( h, a->i_lambda2 );
  1746.         if( *i_rd >= i_rd8 )
  1747.         {
  1748.             if( *i_rd > 0 )
  1749.                 *i_satd = (int64_t)(*i_satd) * i_rd8 / *i_rd;
  1750.             /* prevent a rare division by zero in estimated intra cost */
  1751.             if( *i_satd == 0 )
  1752.                 *i_satd = 1;
  1753.             *i_rd = i_rd8;
  1754.         }
  1755.         else
  1756.             h->mb.b_transform_8x8 = !h->mb.b_transform_8x8;
  1757.     }
  1758. }
  1759. /*****************************************************************************
  1760.  * x264_macroblock_analyse:
  1761.  *****************************************************************************/
  1762. void x264_macroblock_analyse( x264_t *h )
  1763. {
  1764.     x264_mb_analysis_t analysis;
  1765.     int i_cost = COST_MAX;
  1766.     int i;
  1767.     h->mb.i_qp = x264_ratecontrol_qp( h );
  1768.     if( h->param.rc.i_aq_mode )
  1769.         x264_adaptive_quant( h );
  1770.     x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
  1771.     /*--------------------------- Do the analysis ---------------------------*/
  1772.     if( h->sh.i_type == SLICE_TYPE_I )
  1773.     {
  1774.         x264_mb_analyse_intra( h, &analysis, COST_MAX );
  1775.         if( analysis.b_mbrd )
  1776.             x264_intra_rd( h, &analysis, COST_MAX );
  1777.         i_cost = analysis.i_satd_i16x16;
  1778.         h->mb.i_type = I_16x16;
  1779.         COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, h->mb.i_type, I_4x4 );
  1780.         COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, h->mb.i_type, I_8x8 );
  1781.         if( analysis.i_satd_pcm < i_cost )
  1782.             h->mb.i_type = I_PCM;
  1783.         else if( h->mb.i_subpel_refine >= 7 )
  1784.             x264_intra_rd_refine( h, &analysis );
  1785.     }
  1786.     else if( h->sh.i_type == SLICE_TYPE_P )
  1787.     {
  1788.         int b_skip = 0;
  1789.         int i_intra_cost, i_intra_type;
  1790.         h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 0 );
  1791.         /* Fast P_SKIP detection */
  1792.         analysis.b_try_pskip = 0;
  1793.         if( h->param.analyse.b_fast_pskip )
  1794.         {
  1795.             if( h->param.i_threads > 1 && h->mb.cache.pskip_mv[1] > h->mb.mv_max_spel[1] )
  1796.                 // FIXME don't need to check this if the reference frame is done
  1797.                 {}
  1798.             else if( h->param.analyse.i_subpel_refine >= 3 )
  1799.                 analysis.b_try_pskip = 1;
  1800.             else if( h->mb.i_mb_type_left == P_SKIP ||
  1801.                      h->mb.i_mb_type_top == P_SKIP ||
  1802.                      h->mb.i_mb_type_topleft == P_SKIP ||
  1803.                      h->mb.i_mb_type_topright == P_SKIP )
  1804.                 b_skip = x264_macroblock_probe_pskip( h );
  1805.         }
  1806.         h->mc.prefetch_ref( h->mb.pic.p_fref[0][0][h->mb.i_mb_x&3], h->mb.pic.i_stride[0], 1 );
  1807.         if( b_skip )
  1808.         {
  1809.             h->mb.i_type = P_SKIP;
  1810.             h->mb.i_partition = D_16x16;
  1811.             assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->param.i_threads == 1 );
  1812.         }
  1813.         else
  1814.         {
  1815.             const unsigned int flags = h->param.analyse.inter;
  1816.             int i_type;
  1817.             int i_partition;
  1818.             int i_thresh16x8;
  1819.             int i_satd_inter, i_satd_intra;
  1820.             x264_mb_analyse_load_costs( h, &analysis );
  1821.             x264_mb_analyse_inter_p16x16( h, &analysis );
  1822.             if( h->mb.i_type == P_SKIP )
  1823.                 return;
  1824.             if( flags & X264_ANALYSE_PSUB16x16 )
  1825.             {
  1826.                 if( h->param.analyse.b_mixed_references )
  1827.                     x264_mb_analyse_inter_p8x8_mixed_ref( h, &analysis );
  1828.                 else
  1829.                     x264_mb_analyse_inter_p8x8( h, &analysis );
  1830.             }
  1831.             /* Select best inter mode */
  1832.             i_type = P_L0;
  1833.             i_partition = D_16x16;
  1834.             i_cost = analysis.l0.me16x16.cost;
  1835.             if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
  1836.                 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost )
  1837.             {
  1838.                 i_type = P_8x8;
  1839.                 i_partition = D_8x8;
  1840.                 i_cost = analysis.l0.i_cost8x8;
  1841.                 /* Do sub 8x8 */
  1842.                 if( flags & X264_ANALYSE_PSUB8x8 )
  1843.                 {
  1844.                     for( i = 0; i < 4; i++ )
  1845.                     {
  1846.                         x264_mb_analyse_inter_p4x4( h, &analysis, i );
  1847.                         if( analysis.l0.i_cost4x4[i] < analysis.l0.me8x8[i].cost )
  1848.                         {
  1849.                             int i_cost8x8 = analysis.l0.i_cost4x4[i];
  1850.                             h->mb.i_sub_partition[i] = D_L0_4x4;
  1851.                             x264_mb_analyse_inter_p8x4( h, &analysis, i );
  1852.                             COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost8x4[i],
  1853.                                          h->mb.i_sub_partition[i], D_L0_8x4 );
  1854.                             x264_mb_analyse_inter_p4x8( h, &analysis, i );
  1855.                             COPY2_IF_LT( i_cost8x8, analysis.l0.i_cost4x8[i],
  1856.                                          h->mb.i_sub_partition[i], D_L0_4x8 );
  1857.                             i_cost += i_cost8x8 - analysis.l0.me8x8[i].cost;
  1858.                         }
  1859.                         x264_mb_cache_mv_p8x8( h, &analysis, i );
  1860.                     }
  1861.                     analysis.l0.i_cost8x8 = i_cost;
  1862.                 }
  1863.             }
  1864.             /* Now do 16x8/8x16 */
  1865.             i_thresh16x8 = analysis.l0.me8x8[1].cost_mv + analysis.l0.me8x8[2].cost_mv;
  1866.             if( ( flags & X264_ANALYSE_PSUB16x16 ) &&
  1867.                 analysis.l0.i_cost8x8 < analysis.l0.me16x16.cost + i_thresh16x8 )
  1868.             {
  1869.                 x264_mb_analyse_inter_p16x8( h, &analysis );
  1870.                 COPY3_IF_LT( i_cost, analysis.l0.i_cost16x8, i_type, P_L0, i_partition, D_16x8 );
  1871.                 x264_mb_analyse_inter_p8x16( h, &analysis );
  1872.                 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x16, i_type, P_L0, i_partition, D_8x16 );
  1873.             }
  1874.             h->mb.i_partition = i_partition;
  1875.             /* refine qpel */
  1876.             //FIXME mb_type costs?
  1877.             if( analysis.b_mbrd )
  1878.             {
  1879.                 /* refine later */
  1880.             }
  1881.             else if( i_partition == D_16x16 )
  1882.             {
  1883.                 x264_me_refine_qpel( h, &analysis.l0.me16x16 );
  1884.                 i_cost = analysis.l0.me16x16.cost;
  1885.             }
  1886.             else if( i_partition == D_16x8 )
  1887.             {
  1888.                 x264_me_refine_qpel( h, &analysis.l0.me16x8[0] );
  1889.                 x264_me_refine_qpel( h, &analysis.l0.me16x8[1] );
  1890.                 i_cost = analysis.l0.me16x8[0].cost + analysis.l0.me16x8[1].cost;
  1891.             }
  1892.             else if( i_partition == D_8x16 )
  1893.             {
  1894.                 x264_me_refine_qpel( h, &analysis.l0.me8x16[0] );
  1895.                 x264_me_refine_qpel( h, &analysis.l0.me8x16[1] );
  1896.                 i_cost = analysis.l0.me8x16[0].cost + analysis.l0.me8x16[1].cost;
  1897.             }
  1898.             else if( i_partition == D_8x8 )
  1899.             {
  1900.                 int i8x8;
  1901.                 i_cost = 0;
  1902.                 for( i8x8 = 0; i8x8 < 4; i8x8++ )
  1903.                 {
  1904.                     switch( h->mb.i_sub_partition[i8x8] )
  1905.                     {
  1906.                         case D_L0_8x8:
  1907.                             x264_me_refine_qpel( h, &analysis.l0.me8x8[i8x8] );
  1908.                             i_cost += analysis.l0.me8x8[i8x8].cost;
  1909.                             break;
  1910.                         case D_L0_8x4:
  1911.                             x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][0] );
  1912.                             x264_me_refine_qpel( h, &analysis.l0.me8x4[i8x8][1] );
  1913.                             i_cost += analysis.l0.me8x4[i8x8][0].cost +
  1914.                                       analysis.l0.me8x4[i8x8][1].cost;
  1915.                             break;
  1916.                         case D_L0_4x8:
  1917.                             x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][0] );
  1918.                             x264_me_refine_qpel( h, &analysis.l0.me4x8[i8x8][1] );
  1919.                             i_cost += analysis.l0.me4x8[i8x8][0].cost +
  1920.                                       analysis.l0.me4x8[i8x8][1].cost;
  1921.                             break;
  1922.                         case D_L0_4x4:
  1923.                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][0] );
  1924.                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][1] );
  1925.                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][2] );
  1926.                             x264_me_refine_qpel( h, &analysis.l0.me4x4[i8x8][3] );
  1927.                             i_cost += analysis.l0.me4x4[i8x8][0].cost +
  1928.                                       analysis.l0.me4x4[i8x8][1].cost +
  1929.                                       analysis.l0.me4x4[i8x8][2].cost +
  1930.                                       analysis.l0.me4x4[i8x8][3].cost;
  1931.                             break;
  1932.                         default:
  1933.                             x264_log( h, X264_LOG_ERROR, "internal error (!8x8 && !4x4)n" );
  1934.                             break;
  1935.                     }
  1936.                 }
  1937.             }
  1938.             if( h->mb.b_chroma_me )
  1939.             {
  1940.                 x264_mb_analyse_intra_chroma( h, &analysis );
  1941.                 x264_mb_analyse_intra( h, &analysis, i_cost - analysis.i_satd_i8x8chroma );
  1942.                 analysis.i_satd_i16x16 += analysis.i_satd_i8x8chroma;
  1943.                 analysis.i_satd_i8x8 += analysis.i_satd_i8x8chroma;
  1944.                 analysis.i_satd_i4x4 += analysis.i_satd_i8x8chroma;
  1945.             }
  1946.             else
  1947.                 x264_mb_analyse_intra( h, &analysis, i_cost );
  1948.             i_satd_inter = i_cost;
  1949.             i_satd_intra = X264_MIN3( analysis.i_satd_i16x16,
  1950.                                       analysis.i_satd_i8x8,
  1951.                                       analysis.i_satd_i4x4 );
  1952.             if( analysis.b_mbrd )
  1953.             {
  1954.                 x264_mb_analyse_p_rd( h, &analysis, X264_MIN(i_satd_inter, i_satd_intra) );
  1955.                 i_type = P_L0;
  1956.                 i_partition = D_16x16;
  1957.                 i_cost = analysis.l0.me16x16.cost;
  1958.                 COPY2_IF_LT( i_cost, analysis.l0.i_cost16x8, i_partition, D_16x8 );
  1959.                 COPY2_IF_LT( i_cost, analysis.l0.i_cost8x16, i_partition, D_8x16 );
  1960.                 COPY3_IF_LT( i_cost, analysis.l0.i_cost8x8, i_partition, D_8x8, i_type, P_8x8 );
  1961.                 h->mb.i_type = i_type;
  1962.                 h->mb.i_partition = i_partition;
  1963.                 if( i_cost < COST_MAX )
  1964.                     x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
  1965.                 x264_intra_rd( h, &analysis, i_satd_inter * 5/4 );
  1966.             }
  1967.             i_intra_type = I_16x16;
  1968.             i_intra_cost = analysis.i_satd_i16x16;
  1969.             COPY2_IF_LT( i_intra_cost, analysis.i_satd_i8x8, i_intra_type, I_8x8 );
  1970.             COPY2_IF_LT( i_intra_cost, analysis.i_satd_i4x4, i_intra_type, I_4x4 );
  1971.             COPY2_IF_LT( i_intra_cost, analysis.i_satd_pcm, i_intra_type, I_PCM );
  1972.             COPY2_IF_LT( i_cost, i_intra_cost, i_type, i_intra_type );
  1973.             if( i_intra_cost == COST_MAX )
  1974.                 i_intra_cost = i_cost * i_satd_intra / i_satd_inter + 1;
  1975.             h->mb.i_type = i_type;
  1976.             h->stat.frame.i_intra_cost += i_intra_cost;
  1977.             h->stat.frame.i_inter_cost += i_cost;
  1978.             h->stat.frame.i_mbs_analysed++;
  1979.             if( h->mb.i_subpel_refine >= 7 && h->mb.i_type != I_PCM )
  1980.             {
  1981.                 if( IS_INTRA( h->mb.i_type ) )
  1982.                 {
  1983.                     x264_intra_rd_refine( h, &analysis );
  1984.                 }
  1985.                 else if( i_partition == D_16x16 )
  1986.                 {
  1987.                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, analysis.l0.me16x16.i_ref );
  1988.                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x16, analysis.i_lambda2, 0 );
  1989.                 }
  1990.                 else if( i_partition == D_16x8 )
  1991.                 {
  1992.                     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
  1993.                     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
  1994.                     x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, analysis.l0.me16x8[0].i_ref );
  1995.                     x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, analysis.l0.me16x8[1].i_ref );
  1996.                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[0], analysis.i_lambda2, 0 );
  1997.                     x264_me_refine_qpel_rd( h, &analysis.l0.me16x8[1], analysis.i_lambda2, 2 );
  1998.                 }
  1999.                 else if( i_partition == D_8x16 )
  2000.                 {
  2001.                     h->mb.i_sub_partition[0] = h->mb.i_sub_partition[1] =
  2002.                     h->mb.i_sub_partition[2] = h->mb.i_sub_partition[3] = D_L0_8x8;
  2003.                     x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, analysis.l0.me8x16[0].i_ref );
  2004.                     x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, analysis.l0.me8x16[1].i_ref );
  2005.                     x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[0], analysis.i_lambda2, 0 );
  2006.                     x264_me_refine_qpel_rd( h, &analysis.l0.me8x16[1], analysis.i_lambda2, 1 );
  2007.                 }
  2008.                 else if( i_partition == D_8x8 )
  2009.                 {
  2010.                     int i8x8;
  2011.                     x264_analyse_update_cache( h, &analysis );
  2012.                     for( i8x8 = 0; i8x8 < 4; i8x8++ )
  2013.                          if( h->mb.i_sub_partition[i8x8] == D_L0_8x8 )
  2014.                              x264_me_refine_qpel_rd( h, &analysis.l0.me8x8[i8x8], analysis.i_lambda2, i8x8 );
  2015.                 }
  2016.             }
  2017.         }
  2018.     }
  2019.     else if( h->sh.i_type == SLICE_TYPE_B )
  2020.     {
  2021.         int i_bskip_cost = COST_MAX;
  2022.         int b_skip = 0;
  2023.         h->mb.i_type = B_SKIP;
  2024.         if( h->mb.b_direct_auto_write )
  2025.         {
  2026.             /* direct=auto heuristic: prefer whichever mode allows more Skip macroblocks */
  2027.             for( i = 0; i < 2; i++ )
  2028.             {
  2029.                 int b_changed = 1;
  2030.                 h->sh.b_direct_spatial_mv_pred ^= 1;
  2031.                 analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, i && analysis.b_direct_available ? &b_changed : NULL );
  2032.                 if( analysis.b_direct_available )
  2033.                 {
  2034.                     if( b_changed )
  2035.                     {
  2036.                         x264_mb_mc( h );
  2037.                         b_skip = x264_macroblock_probe_bskip( h );
  2038.                     }
  2039.                     h->stat.frame.i_direct_score[ h->sh.b_direct_spatial_mv_pred ] += b_skip;
  2040.                 }
  2041.                 else
  2042.                     b_skip = 0;
  2043.             }
  2044.         }
  2045.         else
  2046.             analysis.b_direct_available = x264_mb_predict_mv_direct16x16( h, NULL );
  2047.         if( analysis.b_direct_available )
  2048.         {
  2049.             if( !h->mb.b_direct_auto_write )
  2050.                 x264_mb_mc( h );
  2051.             if( h->mb.b_lossless )
  2052.             {
  2053.                 /* chance of skip is too small to bother */
  2054.             }
  2055.             else if( analysis.b_mbrd )
  2056.             {
  2057.                 i_bskip_cost = ssd_mb( h );
  2058.                 /* 6 = minimum cavlc cost of a non-skipped MB */
  2059.                 b_skip = h->mb.b_skip_mc = i_bskip_cost <= ((6 * analysis.i_lambda2 + 128) >> 8);
  2060.             }
  2061.             else if( !h->mb.b_direct_auto_write )
  2062.             {
  2063.                 /* Conditioning the probe on neighboring block types
  2064.                  * doesn't seem to help speed or quality. */
  2065.                 b_skip = x264_macroblock_probe_bskip( h );
  2066.             }
  2067.         }
  2068.         if( !b_skip )
  2069.         {
  2070.             const unsigned int flags = h->param.analyse.inter;
  2071.             int i_type;
  2072.             int i_partition;
  2073.             int i_satd_inter = 0; // shut up uninitialized warning
  2074.             h->mb.b_skip_mc = 0;
  2075.             x264_mb_analyse_load_costs( h, &analysis );
  2076.             /* select best inter mode */
  2077.             /* direct must be first */
  2078.             if( analysis.b_direct_available )
  2079.                 x264_mb_analyse_inter_direct( h, &analysis );
  2080.             x264_mb_analyse_inter_b16x16( h, &analysis );
  2081.             i_type = B_L0_L0;
  2082.             i_partition = D_16x16;
  2083.             i_cost = analysis.l0.me16x16.cost;
  2084.             COPY2_IF_LT( i_cost, analysis.l1.me16x16.cost, i_type, B_L1_L1 );
  2085.             COPY2_IF_LT( i_cost, analysis.i_cost16x16bi, i_type, B_BI_BI );
  2086.             COPY2_IF_LT( i_cost, analysis.i_cost16x16direct, i_type, B_DIRECT );
  2087.             if( analysis.b_mbrd && analysis.i_cost16x16direct <= i_cost * 33/32 )
  2088.             {
  2089.                 x264_mb_analyse_b_rd( h, &analysis, i_cost );
  2090.                 if( i_bskip_cost < analysis.i_rd16x16direct &&
  2091.                     i_bskip_cost < analysis.i_rd16x16bi &&
  2092.                     i_bskip_cost < analysis.l0.i_rd16x16 &&
  2093.                     i_bskip_cost < analysis.l1.i_rd16x16 )
  2094.                 {
  2095.                     h->mb.i_type = B_SKIP;
  2096.                     x264_analyse_update_cache( h, &analysis );
  2097.                     return;
  2098.                 }
  2099.             }
  2100.             if( flags & X264_ANALYSE_BSUB16x16 )
  2101.             {
  2102.                 x264_mb_analyse_inter_b8x8( h, &analysis );
  2103.                 if( analysis.i_cost8x8bi < i_cost )
  2104.                 {
  2105.                     i_type = B_8x8;
  2106.                     i_partition = D_8x8;
  2107.                     i_cost = analysis.i_cost8x8bi;
  2108.                     if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[1] ||
  2109.                         h->mb.i_sub_partition[2] == h->mb.i_sub_partition[3] )
  2110.                     {
  2111.                         x264_mb_analyse_inter_b16x8( h, &analysis );
  2112.                         COPY3_IF_LT( i_cost, analysis.i_cost16x8bi,
  2113.                                      i_type, analysis.i_mb_type16x8,
  2114.                                      i_partition, D_16x8 );
  2115.                     }
  2116.                     if( h->mb.i_sub_partition[0] == h->mb.i_sub_partition[2] ||
  2117.                         h->mb.i_sub_partition[1] == h->mb.i_sub_partition[3] )
  2118.                     {
  2119.                         x264_mb_analyse_inter_b8x16( h, &analysis );
  2120.                         COPY3_IF_LT( i_cost, analysis.i_cost8x16bi,
  2121.                                      i_type, analysis.i_mb_type8x16,
  2122.                                      i_partition, D_8x16 );
  2123.                     }
  2124.                 }
  2125.             }
  2126.             if( analysis.b_mbrd )
  2127.             {
  2128.                 /* refine later */
  2129.             }
  2130.             /* refine qpel */
  2131.             else if( i_partition == D_16x16 )
  2132.             {
  2133.                 analysis.l0.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
  2134.                 analysis.l1.me16x16.cost -= analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
  2135.                 if( i_type == B_L0_L0 )
  2136.                 {
  2137.                     x264_me_refine_qpel( h, &analysis.l0.me16x16 );
  2138.                     i_cost = analysis.l0.me16x16.cost
  2139.                            + analysis.i_lambda * i_mb_b_cost_table[B_L0_L0];
  2140.                 }
  2141.                 else if( i_type == B_L1_L1 )
  2142.                 {
  2143.                     x264_me_refine_qpel( h, &analysis.l1.me16x16 );
  2144.                     i_cost = analysis.l1.me16x16.cost
  2145.                            + analysis.i_lambda * i_mb_b_cost_table[B_L1_L1];
  2146.                 }
  2147.                 else if( i_type == B_BI_BI )
  2148.                 {
  2149.                     x264_me_refine_qpel( h, &analysis.l0.me16x16 );
  2150.                     x264_me_refine_qpel( h, &analysis.l1.me16x16 );
  2151.                 }
  2152.             }
  2153.             else if( i_partition == D_16x8 )
  2154.             {
  2155.                 for( i=0; i<2; i++ )
  2156.                 {
  2157.                     if( analysis.i_mb_partition16x8[i] != D_L1_8x8 )
  2158.                         x264_me_refine_qpel( h, &analysis.l0.me16x8[i] );
  2159.                     if( analysis.i_mb_partition16x8[i] != D_L0_8x8 )
  2160.                         x264_me_refine_qpel( h, &analysis.l1.me16x8[i] );
  2161.                 }
  2162.             }
  2163.             else if( i_partition == D_8x16 )
  2164.             {
  2165.                 for( i=0; i<2; i++ )
  2166.                 {
  2167.                     if( analysis.i_mb_partition8x16[i] != D_L1_8x8 )
  2168.                         x264_me_refine_qpel( h, &analysis.l0.me8x16[i] );
  2169.                     if( analysis.i_mb_partition8x16[i] != D_L0_8x8 )
  2170.                         x264_me_refine_qpel( h, &analysis.l1.me8x16[i] );
  2171.                 }
  2172.             }
  2173.             else if( i_partition == D_8x8 )
  2174.             {
  2175.                 for( i=0; i<4; i++ )
  2176.                 {
  2177.                     x264_me_t *m;
  2178.                     int i_part_cost_old;
  2179.                     int i_type_cost;
  2180.                     int i_part_type = h->mb.i_sub_partition[i];
  2181.                     int b_bidir = (i_part_type == D_BI_8x8);
  2182.                     if( i_part_type == D_DIRECT_8x8 )
  2183.                         continue;
  2184.                     if( x264_mb_partition_listX_table[0][i_part_type] )
  2185.                     {
  2186.                         m = &analysis.l0.me8x8[i];
  2187.                         i_part_cost_old = m->cost;
  2188.                         i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L0_8x8];
  2189.                         m->cost -= i_type_cost;
  2190.                         x264_me_refine_qpel( h, m );
  2191.                         if( !b_bidir )
  2192.                             analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
  2193.                     }
  2194.                     if( x264_mb_partition_listX_table[1][i_part_type] )
  2195.                     {
  2196.                         m = &analysis.l1.me8x8[i];
  2197.                         i_part_cost_old = m->cost;
  2198.                         i_type_cost = analysis.i_lambda * i_sub_mb_b_cost_table[D_L1_8x8];
  2199.                         m->cost -= i_type_cost;
  2200.                         x264_me_refine_qpel( h, m );
  2201.                         if( !b_bidir )
  2202.                             analysis.i_cost8x8bi += m->cost + i_type_cost - i_part_cost_old;
  2203.                     }
  2204.                     /* TODO: update mvp? */
  2205.                 }
  2206.             }
  2207.             if( analysis.b_mbrd )
  2208.             {
  2209.                 i_satd_inter = i_cost;
  2210.                 x264_mb_analyse_b_rd( h, &analysis, i_satd_inter );
  2211.                 i_type = B_SKIP;
  2212.                 i_cost = i_bskip_cost;
  2213.                 i_partition = D_16x16;
  2214.                 COPY2_IF_LT( i_cost, analysis.l0.i_rd16x16, i_type, B_L0_L0 );
  2215.                 COPY2_IF_LT( i_cost, analysis.l1.i_rd16x16, i_type, B_L1_L1 );
  2216.                 COPY2_IF_LT( i_cost, analysis.i_rd16x16bi, i_type, B_BI_BI );
  2217.                 COPY2_IF_LT( i_cost, analysis.i_rd16x16direct, i_type, B_DIRECT );
  2218.                 COPY3_IF_LT( i_cost, analysis.i_rd16x8bi, i_type, analysis.i_mb_type16x8, i_partition, D_16x8 );
  2219.                 COPY3_IF_LT( i_cost, analysis.i_rd8x16bi, i_type, analysis.i_mb_type8x16, i_partition, D_8x16 );
  2220.                 COPY3_IF_LT( i_cost, analysis.i_rd8x8bi, i_type, B_8x8, i_partition, D_8x8 );
  2221.                 h->mb.i_type = i_type;
  2222.                 h->mb.i_partition = i_partition;
  2223.             }
  2224.             x264_mb_analyse_intra( h, &analysis, i_satd_inter );
  2225.             if( analysis.b_mbrd )
  2226.             {
  2227.                 x264_mb_analyse_transform_rd( h, &analysis, &i_satd_inter, &i_cost );
  2228.                 x264_intra_rd( h, &analysis, i_satd_inter * 17/16 );
  2229.             }
  2230.             COPY2_IF_LT( i_cost, analysis.i_satd_i16x16, i_type, I_16x16 );
  2231.             COPY2_IF_LT( i_cost, analysis.i_satd_i8x8, i_type, I_8x8 );
  2232.             COPY2_IF_LT( i_cost, analysis.i_satd_i4x4, i_type, I_4x4 );
  2233.             COPY2_IF_LT( i_cost, analysis.i_satd_pcm, i_type, I_PCM );
  2234.             h->mb.i_type = i_type;
  2235.             h->mb.i_partition = i_partition;
  2236.             if( analysis.b_mbrd && h->mb.i_subpel_refine >= 7 && IS_INTRA( i_type ) && i_type != I_PCM )
  2237.                 x264_intra_rd_refine( h, &analysis );
  2238.             else if( h->param.analyse.b_bidir_me )
  2239.                 refine_bidir( h, &analysis );
  2240.         }
  2241.     }
  2242.     x264_analyse_update_cache( h, &analysis );
  2243.     if( !analysis.b_mbrd )
  2244.         x264_mb_analyse_transform( h );
  2245.     h->mb.b_trellis = h->param.analyse.i_trellis;
  2246.     h->mb.b_noise_reduction = !!h->param.analyse.i_noise_reduction;
  2247.     if( h->mb.b_trellis == 1 || h->mb.b_noise_reduction )
  2248.         h->mb.i_skip_intra = 0;
  2249. }
  2250. /*-------------------- Update MB from the analysis ----------------------*/
  2251. static void x264_analyse_update_cache( x264_t *h, x264_mb_analysis_t *a  )
  2252. {
  2253.     int i;
  2254.     switch( h->mb.i_type )
  2255.     {
  2256.         case I_4x4:
  2257.             for( i = 0; i < 16; i++ )
  2258.                 h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] = a->i_predict4x4[i];
  2259.             x264_mb_analyse_intra_chroma( h, a );
  2260.             break;
  2261.         case I_8x8:
  2262.             for( i = 0; i < 4; i++ )
  2263.                 x264_macroblock_cache_intra8x8_pred( h, 2*(i&1), 2*(i>>1), a->i_predict8x8[i] );
  2264.             x264_mb_analyse_intra_chroma( h, a );
  2265.             break;
  2266.         case I_16x16:
  2267.             h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
  2268.             x264_mb_analyse_intra_chroma( h, a );
  2269.             break;
  2270.         case I_PCM:
  2271.             break;
  2272.         case P_L0:
  2273.             switch( h->mb.i_partition )
  2274.             {
  2275.                 case D_16x16:
  2276.                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
  2277.                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
  2278.                     break;
  2279.                 case D_16x8:
  2280.                     x264_macroblock_cache_ref( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].i_ref );
  2281.                     x264_macroblock_cache_ref( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].i_ref );
  2282.                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 2, 0, a->l0.me16x8[0].mv );
  2283.                     x264_macroblock_cache_mv_ptr( h, 0, 2, 4, 2, 0, a->l0.me16x8[1].mv );
  2284.                     break;
  2285.                 case D_8x16:
  2286.                     x264_macroblock_cache_ref( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].i_ref );
  2287.                     x264_macroblock_cache_ref( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].i_ref );
  2288.                     x264_macroblock_cache_mv_ptr( h, 0, 0, 2, 4, 0, a->l0.me8x16[0].mv );
  2289.                     x264_macroblock_cache_mv_ptr( h, 2, 0, 2, 4, 0, a->l0.me8x16[1].mv );
  2290.                     break;
  2291.                 default:
  2292.                     x264_log( h, X264_LOG_ERROR, "internal error P_L0 and partition=%dn", h->mb.i_partition );
  2293.                     break;
  2294.             }
  2295.             break;
  2296.         case P_8x8:
  2297.             x264_macroblock_cache_ref( h, 0, 0, 2, 2, 0, a->l0.me8x8[0].i_ref );
  2298.             x264_macroblock_cache_ref( h, 2, 0, 2, 2, 0, a->l0.me8x8[1].i_ref );
  2299.             x264_macroblock_cache_ref( h, 0, 2, 2, 2, 0, a->l0.me8x8[2].i_ref );
  2300.             x264_macroblock_cache_ref( h, 2, 2, 2, 2, 0, a->l0.me8x8[3].i_ref );
  2301.             for( i = 0; i < 4; i++ )
  2302.                 x264_mb_cache_mv_p8x8( h, a, i );
  2303.             break;
  2304.         case P_SKIP:
  2305.         {
  2306.             h->mb.i_partition = D_16x16;
  2307.             x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
  2308.             x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, h->mb.cache.pskip_mv );
  2309.             break;
  2310.         }
  2311.         case B_SKIP:
  2312.         case B_DIRECT:
  2313.             x264_mb_load_mv_direct8x8( h, 0 );
  2314.             x264_mb_load_mv_direct8x8( h, 1 );
  2315.             x264_mb_load_mv_direct8x8( h, 2 );
  2316.             x264_mb_load_mv_direct8x8( h, 3 );
  2317.             break;
  2318.         case B_8x8:
  2319.             /* optimize: cache might not need to be rewritten */
  2320.             for( i = 0; i < 4; i++ )
  2321.                 x264_mb_cache_mv_b8x8( h, a, i, 1 );
  2322.             break;
  2323.         default: /* the rest of the B types */
  2324.             switch( h->mb.i_partition )
  2325.             {
  2326.             case D_16x16:
  2327.                 switch( h->mb.i_type )
  2328.                 {
  2329.                 case B_L0_L0:
  2330.                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
  2331.                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
  2332.                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, -1 );
  2333.                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 1, 0 );
  2334.                     x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 1, 0 );
  2335.                     break;
  2336.                 case B_L1_L1:
  2337.                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, -1 );
  2338.                     x264_macroblock_cache_mv ( h, 0, 0, 4, 4, 0, 0 );
  2339.                     x264_macroblock_cache_mvd( h, 0, 0, 4, 4, 0, 0 );
  2340.                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
  2341.                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
  2342.                     break;
  2343.                 case B_BI_BI:
  2344.                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.i_ref );
  2345.                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
  2346.                     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, a->l1.i_ref );
  2347.                     x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, a->l1.me16x16.mv );
  2348.                     break;
  2349.                 }
  2350.                 break;
  2351.             case D_16x8:
  2352.                 x264_mb_cache_mv_b16x8( h, a, 0, 1 );
  2353.                 x264_mb_cache_mv_b16x8( h, a, 1, 1 );
  2354.                 break;
  2355.             case D_8x16:
  2356.                 x264_mb_cache_mv_b8x16( h, a, 0, 1 );
  2357.                 x264_mb_cache_mv_b8x16( h, a, 1, 1 );
  2358.                 break;
  2359.             default:
  2360.                 x264_log( h, X264_LOG_ERROR, "internal error (invalid MB type)n" );
  2361.                 break;
  2362.             }
  2363.     }
  2364. #ifndef NDEBUG
  2365.     if( h->param.i_threads > 1 && !IS_INTRA(h->mb.i_type) )
  2366.     {
  2367.         int l;
  2368.         for( l=0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
  2369.         {
  2370.             int completed;
  2371.             int ref = h->mb.cache.ref[l][x264_scan8[0]];
  2372.             if( ref < 0 )
  2373.                 continue;
  2374.             completed = (l ? h->fref1 : h->fref0)[ ref >> h->mb.b_interlaced ]->i_lines_completed;
  2375.             if( (h->mb.cache.mv[l][x264_scan8[15]][1] >> (2 - h->mb.b_interlaced)) + h->mb.i_mb_y*16 > completed )
  2376.             {
  2377.                 x264_log( h, X264_LOG_WARNING, "internal error (MV out of thread range)n");
  2378.                 fprintf(stderr, "mb type: %d n", h->mb.i_type);
  2379.                 fprintf(stderr, "mv: l%dr%d (%d,%d) n", l, ref,
  2380.                                 h->mb.cache.mv[l][x264_scan8[15]][0],
  2381.                                 h->mb.cache.mv[l][x264_scan8[15]][1] );
  2382.                 fprintf(stderr, "limit: %d n", h->mb.mv_max_spel[1]);
  2383.                 fprintf(stderr, "mb_xy: %d,%d n", h->mb.i_mb_x, h->mb.i_mb_y);
  2384.                 fprintf(stderr, "completed: %d n", completed );
  2385.                 x264_log( h, X264_LOG_WARNING, "recovering by using intra moden");
  2386.                 x264_mb_analyse_intra( h, a, COST_MAX );
  2387.                 h->mb.i_type = I_16x16;
  2388.                 h->mb.i_intra16x16_pred_mode = a->i_predict16x16;
  2389.                 x264_mb_analyse_intra_chroma( h, a );
  2390.             }
  2391.         }
  2392.     }
  2393. #endif
  2394. }
  2395. #include "slicetype.c"