Home | History | Annotate | Download | only in encoder
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 
     12 #include <stdio.h>
     13 #include <math.h>
     14 #include <limits.h>
     15 #include <assert.h>
     16 #include "pragmas.h"
     17 
     18 #include "tokenize.h"
     19 #include "treewriter.h"
     20 #include "onyx_int.h"
     21 #include "modecosts.h"
     22 #include "encodeintra.h"
     23 #include "entropymode.h"
     24 #include "reconinter.h"
     25 #include "reconintra.h"
     26 #include "reconintra4x4.h"
     27 #include "findnearmv.h"
     28 #include "encodemb.h"
     29 #include "quantize.h"
     30 #include "idct.h"
     31 #include "g_common.h"
     32 #include "variance.h"
     33 #include "mcomp.h"
     34 
     35 #include "vpx_mem/vpx_mem.h"
     36 #include "dct.h"
     37 #include "systemdependent.h"
     38 
     39 #define DIAMONDSEARCH 1
     40 #if CONFIG_RUNTIME_CPU_DETECT
     41 #define IF_RTCD(x)  (x)
     42 #else
     43 #define IF_RTCD(x)  NULL
     44 #endif
     45 
     46 
     47 void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
     48 
     49 
     50 #define RDFUNC(RM,DM,R,D,target_rd) ( ((128+(R)*(RM)) >> 8) + (DM)*(D) )
     51 /*int  RDFUNC( int RM,int DM, int R, int D, int target_r )
     52 {
     53     int rd_value;
     54 
     55     rd_value =  ( ((128+(R)*(RM)) >> 8) + (DM)*(D) );
     56 
     57     return rd_value;
     58 }*/
     59 
     60 #define UVRDFUNC(RM,DM,R,D,target_r)  RDFUNC(RM,DM,R,D,target_r)
     61 
     62 #define RDCOST(RM,DM,R,D) ( ((128+(R)*(RM)) >> 8) + (DM)*(D) )
     63 
     64 #define MAXF(a,b)            (((a) > (b)) ? (a) : (b))
     65 
     66 
     67 
     68 const int vp8_auto_speed_thresh[17] =
     69 {
     70     1000,
     71     200,
     72     150,
     73     130,
     74     150,
     75     125,
     76     120,
     77     115,
     78     115,
     79     115,
     80     115,
     81     115,
     82     115,
     83     115,
     84     115,
     85     115,
     86     105
     87 };
     88 
     89 const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
     90 {
     91     ZEROMV,
     92     DC_PRED,
     93 
     94     NEARESTMV,
     95     NEARMV,
     96 
     97     ZEROMV,
     98     NEARESTMV,
     99 
    100     ZEROMV,
    101     NEARESTMV,
    102 
    103     NEARMV,
    104     NEARMV,
    105 
    106     V_PRED,
    107     H_PRED,
    108     TM_PRED,
    109 
    110     NEWMV,
    111     NEWMV,
    112     NEWMV,
    113 
    114     SPLITMV,
    115     SPLITMV,
    116     SPLITMV,
    117 
    118     B_PRED,
    119 };
    120 
    121 const MV_REFERENCE_FRAME vp8_ref_frame_order[MAX_MODES] =
    122 {
    123     LAST_FRAME,
    124     INTRA_FRAME,
    125 
    126     LAST_FRAME,
    127     LAST_FRAME,
    128 
    129     GOLDEN_FRAME,
    130     GOLDEN_FRAME,
    131 
    132     ALTREF_FRAME,
    133     ALTREF_FRAME,
    134 
    135     GOLDEN_FRAME,
    136     ALTREF_FRAME,
    137 
    138     INTRA_FRAME,
    139     INTRA_FRAME,
    140     INTRA_FRAME,
    141 
    142     LAST_FRAME,
    143     GOLDEN_FRAME,
    144     ALTREF_FRAME,
    145 
    146     LAST_FRAME,
    147     GOLDEN_FRAME,
    148     ALTREF_FRAME,
    149 
    150     INTRA_FRAME,
    151 };
    152 
    153 static void fill_token_costs(
    154     unsigned int c      [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens],
    155     const vp8_prob p    [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1]
    156 )
    157 {
    158     int i, j, k;
    159 
    160 
    161     for (i = 0; i < BLOCK_TYPES; i++)
    162         for (j = 0; j < COEF_BANDS; j++)
    163             for (k = 0; k < PREV_COEF_CONTEXTS; k++)
    164 
    165                 vp8_cost_tokens((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
    166 
    167 }
    168 
    169 static int rd_iifactor [ 32 ] =  {    4,   4,   3,   2,   1,   0,   0,   0,
    170                                       0,   0,   0,   0,   0,   0,   0,   0,
    171                                       0,   0,   0,   0,   0,   0,   0,   0,
    172                                       0,   0,   0,   0,   0,   0,   0,   0,
    173                                  };
    174 
    175 
    176 // The values in this table should be reviewed
    177 static int sad_per_bit16lut[128] =
    178 {
    179     4,  4, 4, 4,  4, 4, 4, 4,   // 4
    180     4,  4, 4, 4,  4, 4, 4, 4,   // 1
    181     4,  4, 4, 4,  4, 4, 4, 4,   // 2
    182     4,  4, 4, 4,  4, 4, 4, 4,   // 3
    183     4,  4, 4, 4,  4, 4, 4, 4,   // 4
    184     4,  4, 12, 12, 13, 13, 14, 14, // 5
    185     14, 14, 14, 15, 15, 15, 15, 15, // 6
    186     15, 15, 15, 15, 15, 15, 15, 15, // 7
    187     15, 15, 15, 15, 15, 16, 16, 16, // 8
    188     16, 16, 18, 18, 18, 18, 19, 19, // 9
    189     19, 19, 19, 19, 19, 19, 19, 19, // 10
    190     20, 20, 22, 22, 22, 22, 21, 21, // 11
    191     22, 22, 22, 22, 22, 22, 22, 22, // 12
    192     22, 22, 22, 22, 22, 22, 22, 22, // 13
    193     22, 22, 22, 22, 22, 22, 22, 22, // 14
    194     22, 22, 22, 22, 22, 22, 22, 22, // 15
    195 };
    196 
    197 static int sad_per_bit4lut[128] =
    198 {
    199     4,  4, 4, 4,  4, 4, 4, 4,   // 4
    200     4,  4, 4, 4,  4, 4, 4, 4,   // 1
    201     4,  4, 4, 4,  4, 4, 4, 4,   // 2
    202     4,  4, 4, 4,  4, 4, 4, 4,   // 3
    203     4,  4, 4, 4,  4, 4, 4, 4,   // 4
    204     4,  4, 15, 15, 15, 15, 16, 16, // 5
    205     16, 17, 17, 17, 17, 17, 17, 17, // 6
    206     17, 17, 19, 19, 22, 22, 21, 21, // 7
    207     23, 23, 23, 23, 23, 24, 24, 24, // 8
    208     25, 25, 27, 27, 27, 27, 28, 28, // 9
    209     28, 28, 29, 29, 29, 29, 29, 29, // 10
    210     30, 30, 31, 31, 31, 31, 32, 32, // 11
    211     34, 34, 34, 34, 34, 34, 34, 34, // 12
    212     34, 34, 34, 34, 34, 34, 34, 34, // 13
    213     34, 34, 34, 34, 34, 34, 34, 34, // 14
    214     34, 34, 34, 34, 34, 34, 34, 34, // 15
    215 };
    216 
    217 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
    218 {
    219     cpi->mb.sadperbit16 =  sad_per_bit16lut[QIndex];
    220     cpi->mb.sadperbit4  =  sad_per_bit4lut[QIndex];
    221 }
    222 
    223 void vp8_initialize_rd_consts(VP8_COMP *cpi, int Qvalue)
    224 {
    225     int q;
    226     int i;
    227     int *thresh;
    228     int threshmult;
    229     double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
    230     double rdconst = 3.00;
    231 
    232     vp8_clear_system_state();  //__asm emms;
    233 
    234     // Further tests required to see if optimum is different
    235     // for key frames, golden frames and arf frames.
    236     // if (cpi->common.refresh_golden_frame ||
    237     //     cpi->common.refresh_alt_ref_frame)
    238     cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
    239 
    240     // Extend rate multiplier along side quantizer zbin increases
    241     if (cpi->zbin_over_quant  > 0)
    242     {
    243         double oq_factor;
    244         double modq;
    245 
    246         // Experimental code using the same basic equation as used for Q above
    247         // The units of cpi->zbin_over_quant are 1/128 of Q bin size
    248         oq_factor = 1.0 + ((double)0.0015625 * cpi->zbin_over_quant);
    249         modq = (int)((double)capped_q * oq_factor);
    250         cpi->RDMULT = (int)(rdconst * (modq * modq));
    251     }
    252 
    253     if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
    254     {
    255         if (cpi->next_iiratio > 31)
    256             cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
    257         else
    258             cpi->RDMULT += (cpi->RDMULT * rd_iifactor[cpi->next_iiratio]) >> 4;
    259     }
    260 
    261     if (cpi->RDMULT < 125)
    262         cpi->RDMULT = 125;
    263 
    264     cpi->mb.errorperbit = (cpi->RDMULT / 100);
    265 
    266     if (cpi->mb.errorperbit < 1)
    267         cpi->mb.errorperbit = 1;
    268 
    269     vp8_set_speed_features(cpi);
    270 
    271     if (cpi->common.simpler_lpf)
    272         cpi->common.filter_type = SIMPLE_LOOPFILTER;
    273 
    274     q = (int)pow(Qvalue, 1.25);
    275 
    276     if (q < 8)
    277         q = 8;
    278 
    279     if (cpi->ref_frame_flags == VP8_ALT_FLAG)
    280     {
    281         thresh      = &cpi->rd_threshes[THR_NEWA];
    282         threshmult  = cpi->sf.thresh_mult[THR_NEWA];
    283     }
    284     else if (cpi->ref_frame_flags == VP8_GOLD_FLAG)
    285     {
    286         thresh      = &cpi->rd_threshes[THR_NEWG];
    287         threshmult  = cpi->sf.thresh_mult[THR_NEWG];
    288     }
    289     else
    290     {
    291         thresh      = &cpi->rd_threshes[THR_NEWMV];
    292         threshmult  = cpi->sf.thresh_mult[THR_NEWMV];
    293     }
    294 
    295     if (cpi->RDMULT > 1000)
    296     {
    297         cpi->RDDIV = 1;
    298         cpi->RDMULT /= 100;
    299 
    300         for (i = 0; i < MAX_MODES; i++)
    301         {
    302             if (cpi->sf.thresh_mult[i] < INT_MAX)
    303             {
    304                 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
    305             }
    306             else
    307             {
    308                 cpi->rd_threshes[i] = INT_MAX;
    309             }
    310 
    311             cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
    312         }
    313     }
    314     else
    315     {
    316         cpi->RDDIV = 100;
    317 
    318         for (i = 0; i < MAX_MODES; i++)
    319         {
    320             if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
    321             {
    322                 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
    323             }
    324             else
    325             {
    326                 cpi->rd_threshes[i] = INT_MAX;
    327             }
    328 
    329             cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
    330         }
    331     }
    332 
    333     fill_token_costs(
    334         cpi->mb.token_costs,
    335         (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs
    336     );
    337 
    338     vp8_init_mode_costs(cpi);
    339 
    340 }
    341 
    342 void vp8_auto_select_speed(VP8_COMP *cpi)
    343 {
    344     int used = cpi->oxcf.cpu_used;
    345 
    346     int milliseconds_for_compress = (int)(1000000 / cpi->oxcf.frame_rate);
    347 
    348     milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
    349 
    350 #if 0
    351 
    352     if (0)
    353     {
    354         FILE *f;
    355 
    356         f = fopen("speed.stt", "a");
    357         fprintf(f, " %8ld %10ld %10ld %10ld\n",
    358                 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
    359         fclose(f);
    360     }
    361 
    362 #endif
    363 
    364     /*
    365     // this is done during parameter valid check
    366     if( used > 16)
    367         used = 16;
    368     if( used < -16)
    369         used = -16;
    370     */
    371 
    372     if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
    373     {
    374         if (cpi->avg_pick_mode_time == 0)
    375         {
    376             cpi->Speed = 4;
    377         }
    378         else
    379         {
    380             if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
    381             {
    382                 cpi->Speed          += 2;
    383                 cpi->avg_pick_mode_time = 0;
    384                 cpi->avg_encode_time = 0;
    385 
    386                 if (cpi->Speed > 16)
    387                 {
    388                     cpi->Speed = 16;
    389                 }
    390             }
    391 
    392             if (milliseconds_for_compress * 100 > cpi->avg_encode_time * vp8_auto_speed_thresh[cpi->Speed])
    393             {
    394                 cpi->Speed          -= 1;
    395                 cpi->avg_pick_mode_time = 0;
    396                 cpi->avg_encode_time = 0;
    397 
    398                 // In real-time mode, cpi->speed is in [4, 16].
    399                 if (cpi->Speed < 4)        //if ( cpi->Speed < 0 )
    400                 {
    401                     cpi->Speed = 4;        //cpi->Speed = 0;
    402                 }
    403             }
    404         }
    405     }
    406     else
    407     {
    408         cpi->Speed += 4;
    409 
    410         if (cpi->Speed > 16)
    411             cpi->Speed = 16;
    412 
    413 
    414         cpi->avg_pick_mode_time = 0;
    415         cpi->avg_encode_time = 0;
    416     }
    417 }
    418 
    419 int vp8_block_error_c(short *coeff, short *dqcoeff)
    420 {
    421     int i;
    422     int error = 0;
    423 
    424     for (i = 0; i < 16; i++)
    425     {
    426         int this_diff = coeff[i] - dqcoeff[i];
    427         error += this_diff * this_diff;
    428     }
    429 
    430     return error;
    431 }
    432 
    433 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
    434 {
    435     BLOCK  *be;
    436     BLOCKD *bd;
    437     int i, j;
    438     int berror, error = 0;
    439 
    440     for (i = 0; i < 16; i++)
    441     {
    442         be = &mb->block[i];
    443         bd = &mb->e_mbd.block[i];
    444 
    445         berror = 0;
    446 
    447         for (j = dc; j < 16; j++)
    448         {
    449             int this_diff = be->coeff[j] - bd->dqcoeff[j];
    450             berror += this_diff * this_diff;
    451         }
    452 
    453         error += berror;
    454     }
    455 
    456     return error;
    457 }
    458 
    459 int vp8_mbuverror_c(MACROBLOCK *mb)
    460 {
    461 
    462     BLOCK  *be;
    463     BLOCKD *bd;
    464 
    465 
    466     int i;
    467     int error = 0;
    468 
    469     for (i = 16; i < 24; i++)
    470     {
    471         be = &mb->block[i];
    472         bd = &mb->e_mbd.block[i];
    473 
    474         error += vp8_block_error_c(be->coeff, bd->dqcoeff);
    475     }
    476 
    477     return error;
    478 }
    479 
    480 #if !(CONFIG_REALTIME_ONLY)
    481 static int macro_block_max_error(MACROBLOCK *mb)
    482 {
    483     int error = 0;
    484     int dc = 0;
    485     BLOCK  *be;
    486     int i, j;
    487     int berror;
    488 
    489     dc = !(mb->e_mbd.mode_info_context->mbmi.mode == B_PRED || mb->e_mbd.mode_info_context->mbmi.mode == SPLITMV);
    490 
    491     for (i = 0; i < 16; i++)
    492     {
    493         be = &mb->block[i];
    494 
    495         berror = 0;
    496 
    497         for (j = dc; j < 16; j++)
    498         {
    499             int this_diff = be->coeff[j];
    500             berror += this_diff * this_diff;
    501         }
    502 
    503         error += berror;
    504     }
    505 
    506     for (i = 16; i < 24; i++)
    507     {
    508         be = &mb->block[i];
    509         berror = 0;
    510 
    511         for (j = 0; j < 16; j++)
    512         {
    513             int this_diff = be->coeff[j];
    514             berror += this_diff * this_diff;
    515         }
    516 
    517         error += berror;
    518     }
    519 
    520     error <<= 2;
    521 
    522     if (dc)
    523     {
    524         be = &mb->block[24];
    525         berror = 0;
    526 
    527         for (j = 0; j < 16; j++)
    528         {
    529             int this_diff = be->coeff[j];
    530             berror += this_diff * this_diff;
    531         }
    532 
    533         error += berror;
    534     }
    535 
    536     error >>= 4;
    537     return error;
    538 }
    539 #endif
    540 
    541 int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd)
    542 {
    543     unsigned char *uptr, *vptr;
    544     unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
    545     unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
    546     int uv_stride = x->block[16].src_stride;
    547 
    548     unsigned int sse1 = 0;
    549     unsigned int sse2 = 0;
    550     int mv_row;
    551     int mv_col;
    552     int offset;
    553     int pre_stride = x->e_mbd.block[16].pre_stride;
    554 
    555     vp8_build_uvmvs(&x->e_mbd, 0);
    556     mv_row = x->e_mbd.block[16].bmi.mv.as_mv.row;
    557     mv_col = x->e_mbd.block[16].bmi.mv.as_mv.col;
    558 
    559     offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
    560     uptr = x->e_mbd.pre.u_buffer + offset;
    561     vptr = x->e_mbd.pre.v_buffer + offset;
    562 
    563     if ((mv_row | mv_col) & 7)
    564     {
    565         VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
    566         VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
    567         sse2 += sse1;
    568     }
    569     else
    570     {
    571         VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
    572         VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
    573         sse2 += sse1;
    574     }
    575 
    576     return sse2;
    577 
    578 }
    579 
    580 #if !(CONFIG_REALTIME_ONLY)
    581 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
    582 {
    583     int c = !type;              /* start at coef 0, unless Y with Y2 */
    584     int eob = b->eob;
    585     int pt ;    /* surrounding block/prev coef predictor */
    586     int cost = 0;
    587     short *qcoeff_ptr = b->qcoeff;
    588 
    589     VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
    590 
    591 # define QC( I)  ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )
    592 
    593     for (; c < eob; c++)
    594     {
    595         int v = QC(c);
    596         int t = vp8_dct_value_tokens_ptr[v].Token;
    597         cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
    598         cost += vp8_dct_value_cost_ptr[v];
    599         pt = vp8_prev_token_class[t];
    600     }
    601 
    602 # undef QC
    603 
    604     if (c < 16)
    605         cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
    606 
    607     pt = (c != !type); // is eob first coefficient;
    608     *a = *l = pt;
    609 
    610     return cost;
    611 }
    612 
    613 int vp8_rdcost_mby(MACROBLOCK *mb)
    614 {
    615     int cost = 0;
    616     int b;
    617     int type = 0;
    618     MACROBLOCKD *x = &mb->e_mbd;
    619     ENTROPY_CONTEXT_PLANES t_above, t_left;
    620     ENTROPY_CONTEXT *ta;
    621     ENTROPY_CONTEXT *tl;
    622 
    623     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    624     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
    625 
    626     ta = (ENTROPY_CONTEXT *)&t_above;
    627     tl = (ENTROPY_CONTEXT *)&t_left;
    628 
    629     if (x->mode_info_context->mbmi.mode == SPLITMV)
    630         type = 3;
    631 
    632     for (b = 0; b < 16; b++)
    633         cost += cost_coeffs(mb, x->block + b, type,
    634                     ta + vp8_block2above[b], tl + vp8_block2left[b]);
    635 
    636     if (x->mode_info_context->mbmi.mode != SPLITMV)
    637         cost += cost_coeffs(mb, x->block + 24, 1,
    638                     ta + vp8_block2above[24], tl + vp8_block2left[24]);
    639 
    640     return cost;
    641 }
    642 
    643 
    644 static void rd_pick_intra4x4block(
    645     VP8_COMP *cpi,
    646     MACROBLOCK *x,
    647     BLOCK *be,
    648     BLOCKD *b,
    649     B_PREDICTION_MODE *best_mode,
    650     B_PREDICTION_MODE above,
    651     B_PREDICTION_MODE left,
    652     ENTROPY_CONTEXT *a,
    653     ENTROPY_CONTEXT *l,
    654 
    655     int *bestrate,
    656     int *bestratey,
    657     int *bestdistortion)
    658 {
    659     B_PREDICTION_MODE mode;
    660     int best_rd = INT_MAX;       // 1<<30
    661     int rate = 0;
    662     int distortion;
    663     unsigned int *mode_costs;
    664 
    665     ENTROPY_CONTEXT ta = *a, tempa = *a;
    666     ENTROPY_CONTEXT tl = *l, templ = *l;
    667 
    668 
    669     if (x->e_mbd.frame_type == KEY_FRAME)
    670     {
    671         mode_costs  = x->bmode_costs[above][left];
    672     }
    673     else
    674     {
    675         mode_costs = x->inter_bmode_costs;
    676     }
    677 
    678     for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
    679     {
    680         int this_rd;
    681         int ratey;
    682 
    683         rate = mode_costs[mode];
    684         vp8_encode_intra4x4block_rd(IF_RTCD(&cpi->rtcd), x, be, b, mode);
    685 
    686         tempa = ta;
    687         templ = tl;
    688 
    689         ratey = cost_coeffs(x, b, 3, &tempa, &templ);
    690         rate += ratey;
    691         distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(be->coeff, b->dqcoeff) >> 2;
    692 
    693         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
    694 
    695         if (this_rd < best_rd)
    696         {
    697             *bestrate = rate;
    698             *bestratey = ratey;
    699             *bestdistortion = distortion;
    700             best_rd = this_rd;
    701             *best_mode = mode;
    702             *a = tempa;
    703             *l = templ;
    704         }
    705     }
    706 
    707     b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
    708     vp8_encode_intra4x4block_rd(IF_RTCD(&cpi->rtcd), x, be, b, b->bmi.mode);
    709 
    710 }
    711 
    712 
    713 int vp8_rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate, int *rate_y, int *Distortion)
    714 {
    715     MACROBLOCKD *const xd = &mb->e_mbd;
    716     int i;
    717     int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
    718     int distortion = 0;
    719     int tot_rate_y = 0;
    720     ENTROPY_CONTEXT_PLANES t_above, t_left;
    721     ENTROPY_CONTEXT *ta;
    722     ENTROPY_CONTEXT *tl;
    723 
    724     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    725     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
    726 
    727     ta = (ENTROPY_CONTEXT *)&t_above;
    728     tl = (ENTROPY_CONTEXT *)&t_left;
    729 
    730     vp8_intra_prediction_down_copy(xd);
    731 
    732     for (i = 0; i < 16; i++)
    733     {
    734         MODE_INFO *const mic = xd->mode_info_context;
    735         const int mis = xd->mode_info_stride;
    736         const B_PREDICTION_MODE A = vp8_above_bmi(mic, i, mis)->mode;
    737         const B_PREDICTION_MODE L = vp8_left_bmi(mic, i)->mode;
    738         B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
    739         int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
    740 
    741         rd_pick_intra4x4block(
    742             cpi, mb, mb->block + i, xd->block + i, &best_mode, A, L,
    743             ta + vp8_block2above[i],
    744             tl + vp8_block2left[i], &r, &ry, &d);
    745 
    746         cost += r;
    747         distortion += d;
    748         tot_rate_y += ry;
    749         mic->bmi[i].mode = xd->block[i].bmi.mode = best_mode;
    750     }
    751 
    752     *Rate = cost;
    753     *rate_y += tot_rate_y;
    754     *Distortion = distortion;
    755 
    756     return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
    757 }
    758 
    759 int vp8_rd_pick_intra16x16mby_mode(VP8_COMP *cpi, MACROBLOCK *x, int *Rate, int *rate_y, int *Distortion)
    760 {
    761 
    762     MB_PREDICTION_MODE mode;
    763     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
    764     int rate, ratey;
    765     unsigned int distortion;
    766     int best_rd = INT_MAX;
    767 
    768     //Y Search for 16x16 intra prediction mode
    769     for (mode = DC_PRED; mode <= TM_PRED; mode++)
    770     {
    771         int this_rd;
    772         int dummy;
    773         rate = 0;
    774 
    775         x->e_mbd.mode_info_context->mbmi.mode = mode;
    776 
    777         rate += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
    778 
    779         vp8_encode_intra16x16mbyrd(IF_RTCD(&cpi->rtcd), x);
    780 
    781         ratey = vp8_rdcost_mby(x);
    782 
    783         rate += ratey;
    784 
    785         VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)(x->src.y_buffer, x->src.y_stride, x->e_mbd.dst.y_buffer, x->e_mbd.dst.y_stride, &distortion, &dummy);
    786 
    787         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
    788 
    789         if (this_rd < best_rd)
    790         {
    791             mode_selected = mode;
    792             best_rd = this_rd;
    793             *Rate = rate;
    794             *rate_y = ratey;
    795             *Distortion = (int)distortion;
    796         }
    797     }
    798 
    799     x->e_mbd.mode_info_context->mbmi.mode = mode_selected;
    800     return best_rd;
    801 }
    802 
    803 
    804 static int rd_cost_mbuv(MACROBLOCK *mb)
    805 {
    806     int b;
    807     int cost = 0;
    808     MACROBLOCKD *x = &mb->e_mbd;
    809     ENTROPY_CONTEXT_PLANES t_above, t_left;
    810     ENTROPY_CONTEXT *ta;
    811     ENTROPY_CONTEXT *tl;
    812 
    813     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    814     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
    815 
    816     ta = (ENTROPY_CONTEXT *)&t_above;
    817     tl = (ENTROPY_CONTEXT *)&t_left;
    818 
    819     for (b = 16; b < 20; b++)
    820         cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
    821                     ta + vp8_block2above[b], tl + vp8_block2left[b]);
    822 
    823     for (b = 20; b < 24; b++)
    824         cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
    825                     ta + vp8_block2above[b], tl + vp8_block2left[b]);
    826 
    827     return cost;
    828 }
    829 
    830 
    831 unsigned int vp8_get_mbuvrecon_error(const vp8_variance_rtcd_vtable_t *rtcd, const MACROBLOCK *x) // sum of squares
    832 {
    833     unsigned int sse0, sse1;
    834     int sum0, sum1;
    835     VARIANCE_INVOKE(rtcd, get8x8var)(x->src.u_buffer, x->src.uv_stride, x->e_mbd.dst.u_buffer, x->e_mbd.dst.uv_stride, &sse0, &sum0);
    836     VARIANCE_INVOKE(rtcd, get8x8var)(x->src.v_buffer, x->src.uv_stride, x->e_mbd.dst.v_buffer, x->e_mbd.dst.uv_stride, &sse1, &sum1);
    837     return (sse0 + sse1);
    838 }
    839 
    840 static int vp8_rd_inter_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *distortion, int fullpixel)
    841 {
    842     vp8_build_uvmvs(&x->e_mbd, fullpixel);
    843     vp8_encode_inter16x16uvrd(IF_RTCD(&cpi->rtcd), x);
    844 
    845 
    846     *rate       = rd_cost_mbuv(x);
    847     *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
    848 
    849     return UVRDFUNC(x->rdmult, x->rddiv, *rate, *distortion, cpi->target_bits_per_mb);
    850 }
    851 
    852 int vp8_rd_pick_intra_mbuv_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, int *distortion)
    853 {
    854     MB_PREDICTION_MODE mode;
    855     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
    856     int best_rd = INT_MAX;
    857     int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
    858     int rate_to;
    859 
    860     for (mode = DC_PRED; mode <= TM_PRED; mode++)
    861     {
    862         int rate;
    863         int distortion;
    864         int this_rd;
    865 
    866         x->e_mbd.mode_info_context->mbmi.uv_mode = mode;
    867         vp8_encode_intra16x16mbuvrd(IF_RTCD(&cpi->rtcd), x);
    868 
    869         rate_to = rd_cost_mbuv(x);
    870         rate = rate_to + x->intra_uv_mode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.uv_mode];
    871 
    872         distortion = vp8_get_mbuvrecon_error(IF_RTCD(&cpi->rtcd.variance), x);
    873 
    874         this_rd = UVRDFUNC(x->rdmult, x->rddiv, rate, distortion, cpi->target_bits_per_mb);
    875 
    876         if (this_rd < best_rd)
    877         {
    878             best_rd = this_rd;
    879             d = distortion;
    880             r = rate;
    881             *rate_tokenonly = rate_to;
    882             mode_selected = mode;
    883         }
    884     }
    885 
    886     *rate = r;
    887     *distortion = d;
    888 
    889     x->e_mbd.mode_info_context->mbmi.uv_mode = mode_selected;
    890     return best_rd;
    891 }
    892 #endif
    893 
    894 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
    895 {
    896     vp8_prob p [VP8_MVREFS-1];
    897     assert(NEARESTMV <= m  &&  m <= SPLITMV);
    898     vp8_mv_ref_probs(p, near_mv_ref_ct);
    899     return vp8_cost_token(vp8_mv_ref_tree, p,
    900                           vp8_mv_ref_encoding_array - NEARESTMV + m);
    901 }
    902 
    903 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv)
    904 {
    905     int i;
    906 
    907     x->e_mbd.mode_info_context->mbmi.mode = mb;
    908     x->e_mbd.mode_info_context->mbmi.mv.as_mv.row = mv->row;
    909     x->e_mbd.mode_info_context->mbmi.mv.as_mv.col = mv->col;
    910 
    911     for (i = 0; i < 16; i++)
    912     {
    913         B_MODE_INFO *bmi = &x->e_mbd.block[i].bmi;
    914         bmi->mode = (B_PREDICTION_MODE) mb;
    915         bmi->mv.as_mv.row = mv->row;
    916         bmi->mv.as_mv.col = mv->col;
    917     }
    918 }
    919 
    920 #if !(CONFIG_REALTIME_ONLY)
    921 int vp8_count_labels(int const *labelings)
    922 {
    923     int i;
    924     int count = 0;
    925 
    926     for (i = 0; i < 16; i++)
    927     {
    928         if (labelings[i] > count)
    929             count = labelings[i];
    930     }
    931 
    932     return count + 1;
    933 }
    934 
    935 
    936 static int labels2mode(
    937     MACROBLOCK *x,
    938     int const *labelings, int which_label,
    939     B_PREDICTION_MODE this_mode,
    940     MV *this_mv, MV *best_ref_mv,
    941     int *mvcost[2]
    942 )
    943 {
    944     MACROBLOCKD *const xd = & x->e_mbd;
    945     MODE_INFO *const mic = xd->mode_info_context;
    946     const int mis = xd->mode_info_stride;
    947 
    948     int cost = 0;
    949     int thismvcost = 0;
    950 
    951     /* We have to be careful retrieving previously-encoded motion vectors.
    952        Ones from this macroblock have to be pulled from the BLOCKD array
    953        as they have not yet made it to the bmi array in our MB_MODE_INFO. */
    954 
    955     int i = 0;
    956 
    957     do
    958     {
    959         BLOCKD *const d = xd->block + i;
    960         const int row = i >> 2,  col = i & 3;
    961 
    962         B_PREDICTION_MODE m;
    963 
    964         if (labelings[i] != which_label)
    965             continue;
    966 
    967         if (col  &&  labelings[i] == labelings[i-1])
    968             m = LEFT4X4;
    969         else if (row  &&  labelings[i] == labelings[i-4])
    970             m = ABOVE4X4;
    971         else
    972         {
    973             // the only time we should do costing for new motion vector or mode
    974             // is when we are on a new label  (jbb May 08, 2007)
    975             switch (m = this_mode)
    976             {
    977             case NEW4X4 :
    978                 thismvcost  = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
    979                 break;
    980             case LEFT4X4:
    981                 *this_mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
    982                 break;
    983             case ABOVE4X4:
    984                 *this_mv = row ? d[-4].bmi.mv.as_mv : vp8_above_bmi(mic, i, mis)->mv.as_mv;
    985                 break;
    986             case ZERO4X4:
    987                 this_mv->row = this_mv->col = 0;
    988                 break;
    989             default:
    990                 break;
    991             }
    992 
    993             if (m == ABOVE4X4)  // replace above with left if same
    994             {
    995                 const MV mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
    996 
    997                 if (mv.row == this_mv->row  &&  mv.col == this_mv->col)
    998                     m = LEFT4X4;
    999             }
   1000 
   1001             cost = x->inter_bmode_costs[ m];
   1002         }
   1003 
   1004         d->bmi.mode = m;
   1005         d->bmi.mv.as_mv = *this_mv;
   1006 
   1007     }
   1008     while (++i < 16);
   1009 
   1010     cost += thismvcost ;
   1011     return cost;
   1012 }
   1013 
   1014 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels,
   1015                               int which_label, ENTROPY_CONTEXT *ta,
   1016                               ENTROPY_CONTEXT *tl)
   1017 {
   1018     int cost = 0;
   1019     int b;
   1020     MACROBLOCKD *x = &mb->e_mbd;
   1021 
   1022     for (b = 0; b < 16; b++)
   1023         if (labels[ b] == which_label)
   1024             cost += cost_coeffs(mb, x->block + b, 3,
   1025                                 ta + vp8_block2above[b],
   1026                                 tl + vp8_block2left[b]);
   1027 
   1028     return cost;
   1029 
   1030 }
   1031 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label, const vp8_encodemb_rtcd_vtable_t *rtcd)
   1032 {
   1033     int i;
   1034     unsigned int distortion = 0;
   1035 
   1036     for (i = 0; i < 16; i++)
   1037     {
   1038         if (labels[i] == which_label)
   1039         {
   1040             BLOCKD *bd = &x->e_mbd.block[i];
   1041             BLOCK *be = &x->block[i];
   1042 
   1043 
   1044             vp8_build_inter_predictors_b(bd, 16, x->e_mbd.subpixel_predict);
   1045             ENCODEMB_INVOKE(rtcd, subb)(be, bd, 16);
   1046             x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
   1047 
   1048             // set to 0 no way to account for 2nd order DC so discount
   1049             //be->coeff[0] = 0;
   1050             x->quantize_b(be, bd);
   1051 
   1052             distortion += ENCODEMB_INVOKE(rtcd, berr)(be->coeff, bd->dqcoeff);
   1053         }
   1054     }
   1055 
   1056     return distortion;
   1057 }
   1058 
   1059 static void macro_block_yrd(MACROBLOCK *mb, int *Rate, int *Distortion, const vp8_encodemb_rtcd_vtable_t *rtcd)
   1060 {
   1061     int b;
   1062     MACROBLOCKD *const x = &mb->e_mbd;
   1063     BLOCK   *const mb_y2 = mb->block + 24;
   1064     BLOCKD *const x_y2  = x->block + 24;
   1065     short *Y2DCPtr = mb_y2->src_diff;
   1066     BLOCK *beptr;
   1067     int d;
   1068 
   1069     ENCODEMB_INVOKE(rtcd, submby)(mb->src_diff, mb->src.y_buffer, mb->e_mbd.predictor, mb->src.y_stride);
   1070 
   1071     // Fdct and building the 2nd order block
   1072     for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
   1073     {
   1074         mb->vp8_short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
   1075         *Y2DCPtr++ = beptr->coeff[0];
   1076         *Y2DCPtr++ = beptr->coeff[16];
   1077     }
   1078 
   1079     // 2nd order fdct
   1080     if (x->mode_info_context->mbmi.mode != SPLITMV)
   1081     {
   1082         mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
   1083     }
   1084 
   1085     // Quantization
   1086     for (b = 0; b < 16; b++)
   1087     {
   1088         mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
   1089     }
   1090 
   1091     // DC predication and Quantization of 2nd Order block
   1092     if (x->mode_info_context->mbmi.mode != SPLITMV)
   1093     {
   1094 
   1095         {
   1096             mb->quantize_b(mb_y2, x_y2);
   1097         }
   1098     }
   1099 
   1100     // Distortion
   1101     if (x->mode_info_context->mbmi.mode == SPLITMV)
   1102         d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 0) << 2;
   1103     else
   1104     {
   1105         d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 1) << 2;
   1106         d += ENCODEMB_INVOKE(rtcd, berr)(mb_y2->coeff, x_y2->dqcoeff);
   1107     }
   1108 
   1109     *Distortion = (d >> 4);
   1110 
   1111     // rate
   1112     *Rate = vp8_rdcost_mby(mb);
   1113 }
   1114 
   1115 static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *best_ref_mv, int best_rd, int *mdcounts, int *returntotrate, int *returnyrate, int *returndistortion, int compressor_speed, int *mvcost[2], int mvthresh, int fullpixel)
   1116 {
   1117     int i, segmentation;
   1118     B_PREDICTION_MODE this_mode;
   1119     MACROBLOCKD *xc = &x->e_mbd;
   1120     BLOCK *b = &x->block[0];
   1121     BLOCKD *d = &x->e_mbd.block[0];
   1122     BLOCK *c = &x->block[0];
   1123     BLOCKD *e = &x->e_mbd.block[0];
   1124     int const *labels;
   1125     int best_segment_rd = INT_MAX;
   1126     int best_seg = 0;
   1127     int br = 0;
   1128     int bd = 0;
   1129     int bsr = 0;
   1130     int bsd = 0;
   1131     int bestsegmentyrate = 0;
   1132 
   1133     // FIX TO Rd error outrange bug PGW 9 june 2004
   1134     B_PREDICTION_MODE bmodes[16] = {ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
   1135                                     ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
   1136                                     ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
   1137                                     ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4
   1138                                    };
   1139 
   1140     MV bmvs[16];
   1141     int beobs[16];
   1142 
   1143     vpx_memset(beobs, 0, sizeof(beobs));
   1144 
   1145 
   1146     for (segmentation = 0; segmentation < VP8_NUMMBSPLITS; segmentation++)
   1147     {
   1148         int label_count;
   1149         int this_segment_rd = 0;
   1150         int label_mv_thresh;
   1151         int rate = 0;
   1152         int sbr = 0;
   1153         int sbd = 0;
   1154         int UNINITIALIZED_IS_SAFE(sseshift);
   1155         int segmentyrate = 0;
   1156 
   1157         vp8_variance_fn_ptr_t v_fn_ptr;
   1158 
   1159         ENTROPY_CONTEXT_PLANES t_above, t_left;
   1160         ENTROPY_CONTEXT *ta;
   1161         ENTROPY_CONTEXT *tl;
   1162         ENTROPY_CONTEXT_PLANES t_above_b, t_left_b;
   1163         ENTROPY_CONTEXT *ta_b;
   1164         ENTROPY_CONTEXT *tl_b;
   1165 
   1166         vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
   1167         vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
   1168 
   1169         ta = (ENTROPY_CONTEXT *)&t_above;
   1170         tl = (ENTROPY_CONTEXT *)&t_left;
   1171         ta_b = (ENTROPY_CONTEXT *)&t_above_b;
   1172         tl_b = (ENTROPY_CONTEXT *)&t_left_b;
   1173 
   1174         br = 0;
   1175         bd = 0;
   1176 
   1177         switch (segmentation)
   1178         {
   1179         case 0:
   1180             v_fn_ptr.vf    = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
   1181             v_fn_ptr.svf   = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
   1182             v_fn_ptr.sdf   = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
   1183             v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
   1184             v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
   1185             sseshift = 3;
   1186             break;
   1187         case 1:
   1188             v_fn_ptr.vf    = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
   1189             v_fn_ptr.svf   = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
   1190             v_fn_ptr.sdf   = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
   1191             v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
   1192             v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
   1193             sseshift = 3;
   1194             break;
   1195         case 2:
   1196             v_fn_ptr.vf    = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
   1197             v_fn_ptr.svf   = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
   1198             v_fn_ptr.sdf   = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
   1199             v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
   1200             v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
   1201             sseshift = 2;
   1202             break;
   1203         case 3:
   1204             v_fn_ptr.vf    = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
   1205             v_fn_ptr.svf   = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
   1206             v_fn_ptr.sdf   = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
   1207             v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
   1208             v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
   1209             sseshift = 0;
   1210             break;
   1211         }
   1212 
   1213         labels = vp8_mbsplits[segmentation];
   1214         label_count = vp8_count_labels(labels);
   1215 
   1216         // 64 makes this threshold really big effectively
   1217         // making it so that we very rarely check mvs on
   1218         // segments.   setting this to 1 would make mv thresh
   1219         // roughly equal to what it is for macroblocks
   1220         label_mv_thresh = 1 * mvthresh / label_count ;
   1221 
   1222         // Segmentation method overheads
   1223         rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
   1224 
   1225         rate += vp8_cost_mv_ref(SPLITMV, mdcounts);
   1226 
   1227         this_segment_rd += RDFUNC(x->rdmult, x->rddiv, rate, 0, cpi->target_bits_per_mb);
   1228         br += rate;
   1229 
   1230         for (i = 0; i < label_count; i++)
   1231         {
   1232             MV mode_mv[B_MODE_COUNT];
   1233             int best_label_rd = INT_MAX;
   1234             B_PREDICTION_MODE mode_selected = ZERO4X4;
   1235             int j;
   1236             int bestlabelyrate = 0;
   1237 
   1238             b = &x->block[0];
   1239             d = &x->e_mbd.block[0];
   1240 
   1241 
   1242             // find first label
   1243             for (j = 0; j < 16; j++)
   1244                 if (labels[j] == i)
   1245                     break;
   1246 
   1247             c = &x->block[j];
   1248             e = &x->e_mbd.block[j];
   1249 
   1250             // search for the best motion vector on this segment
   1251             for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
   1252             {
   1253                 int distortion;
   1254                 int this_rd;
   1255                 int num00;
   1256                 int labelyrate;
   1257                 ENTROPY_CONTEXT_PLANES t_above_s, t_left_s;
   1258                 ENTROPY_CONTEXT *ta_s;
   1259                 ENTROPY_CONTEXT *tl_s;
   1260 
   1261                 vpx_memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES));
   1262                 vpx_memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES));
   1263 
   1264                 ta_s = (ENTROPY_CONTEXT *)&t_above_s;
   1265                 tl_s = (ENTROPY_CONTEXT *)&t_left_s;
   1266 
   1267                 if (this_mode == NEW4X4)
   1268                 {
   1269                     int step_param = 0;
   1270                     int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
   1271                     int n;
   1272                     int thissme;
   1273                     int bestsme = INT_MAX;
   1274                     MV  temp_mv;
   1275 
   1276                     // Is the best so far sufficiently good that we cant justify doing and new motion search.
   1277                     if (best_label_rd < label_mv_thresh)
   1278                         break;
   1279 
   1280                     {
   1281                         int sadpb = x->sadperbit4;
   1282 
   1283                         if (cpi->sf.search_method == HEX)
   1284                             bestsme = vp8_hex_search(x, c, e, best_ref_mv, &mode_mv[NEW4X4], step_param, sadpb/*x->errorperbit*/, &num00, v_fn_ptr.vf, v_fn_ptr.sdf, x->mvsadcost, mvcost);
   1285                         else
   1286                         {
   1287                             bestsme = cpi->diamond_search_sad(x, c, e, best_ref_mv, &mode_mv[NEW4X4], step_param, sadpb / 2/*x->errorperbit*/, &num00, &v_fn_ptr, x->mvsadcost, mvcost);
   1288 
   1289                             n = num00;
   1290                             num00 = 0;
   1291 
   1292                             while (n < further_steps)
   1293                             {
   1294                                 n++;
   1295 
   1296                                 if (num00)
   1297                                     num00--;
   1298                                 else
   1299                                 {
   1300                                     thissme = cpi->diamond_search_sad(x, c, e, best_ref_mv, &temp_mv, step_param + n, sadpb / 2/*x->errorperbit*/, &num00, &v_fn_ptr, x->mvsadcost, mvcost);
   1301 
   1302                                     if (thissme < bestsme)
   1303                                     {
   1304                                         bestsme = thissme;
   1305                                         mode_mv[NEW4X4].row = temp_mv.row;
   1306                                         mode_mv[NEW4X4].col = temp_mv.col;
   1307                                     }
   1308                                 }
   1309                             }
   1310                         }
   1311 
   1312                         // Should we do a full search (best quality only)
   1313                         if ((compressor_speed == 0) && (bestsme >> sseshift) > 4000)
   1314                         {
   1315                             thissme = cpi->full_search_sad(x, c, e, best_ref_mv, sadpb / 4, 16, &v_fn_ptr, x->mvcost, x->mvsadcost);
   1316 
   1317                             if (thissme < bestsme)
   1318                             {
   1319                                 bestsme = thissme;
   1320                                 mode_mv[NEW4X4] = e->bmi.mv.as_mv;
   1321                             }
   1322                             else
   1323                             {
   1324                                 // The full search result is actually worse so re-instate the previous best vector
   1325                                 e->bmi.mv.as_mv = mode_mv[NEW4X4];
   1326                             }
   1327                         }
   1328                     }
   1329 
   1330                     if (bestsme < INT_MAX)
   1331                     {
   1332                         if (!fullpixel)
   1333                             cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], best_ref_mv, x->errorperbit / 2, v_fn_ptr.svf, v_fn_ptr.vf, mvcost);
   1334                         else
   1335                             vp8_skip_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], best_ref_mv, x->errorperbit, v_fn_ptr.svf, v_fn_ptr.vf, mvcost);
   1336                     }
   1337                 }
   1338 
   1339                 rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode], best_ref_mv, mvcost);
   1340 
   1341                 // Trap vectors that reach beyond the UMV borders
   1342                 if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
   1343                     ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
   1344                 {
   1345                     continue;
   1346                 }
   1347 
   1348                 distortion = vp8_encode_inter_mb_segment(x, labels, i, IF_RTCD(&cpi->rtcd.encodemb)) / 4;
   1349 
   1350                 labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s);
   1351                 rate += labelyrate;
   1352 
   1353                 this_rd = RDFUNC(x->rdmult, x->rddiv, rate, distortion, cpi->target_bits_per_mb);
   1354 
   1355                 if (this_rd < best_label_rd)
   1356                 {
   1357                     sbr = rate;
   1358                     sbd = distortion;
   1359                     bestlabelyrate = labelyrate;
   1360                     mode_selected = this_mode;
   1361                     best_label_rd = this_rd;
   1362 
   1363                     vpx_memcpy(ta_b, ta_s, sizeof(ENTROPY_CONTEXT_PLANES));
   1364                     vpx_memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES));
   1365 
   1366                 }
   1367             }
   1368 
   1369             vpx_memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES));
   1370             vpx_memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES));
   1371 
   1372             labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected], best_ref_mv, mvcost);
   1373 
   1374             br += sbr;
   1375             bd += sbd;
   1376             segmentyrate += bestlabelyrate;
   1377             this_segment_rd += best_label_rd;
   1378 
   1379             if ((this_segment_rd > best_rd) || (this_segment_rd > best_segment_rd))
   1380                 break;
   1381         }
   1382 
   1383         if ((this_segment_rd <= best_rd) && (this_segment_rd < best_segment_rd))
   1384         {
   1385             bsr = br;
   1386             bsd = bd;
   1387             bestsegmentyrate = segmentyrate;
   1388             best_segment_rd = this_segment_rd;
   1389             best_seg = segmentation;
   1390 
   1391             // store everything needed to come back to this!!
   1392             for (i = 0; i < 16; i++)
   1393             {
   1394                 BLOCKD *bd = &x->e_mbd.block[i];
   1395 
   1396                 bmvs[i] = bd->bmi.mv.as_mv;
   1397                 bmodes[i] = bd->bmi.mode;
   1398                 beobs[i] = bd->eob;
   1399             }
   1400         }
   1401     }
   1402 
   1403     // set it to the best
   1404     for (i = 0; i < 16; i++)
   1405     {
   1406         BLOCKD *bd = &x->e_mbd.block[i];
   1407 
   1408         bd->bmi.mv.as_mv = bmvs[i];
   1409         bd->bmi.mode = bmodes[i];
   1410         bd->eob = beobs[i];
   1411     }
   1412 
   1413     // Trap cases where the best split mode has all vectors coded 0,0 (or all the same)
   1414     if (FALSE)
   1415     {
   1416         int allsame = 1;
   1417 
   1418         for (i = 1; i < 16; i++)
   1419         {
   1420             if ((bmvs[i].col != bmvs[i-1].col) || (bmvs[i].row != bmvs[i-1].row))
   1421             {
   1422                 allsame = 0;
   1423                 break;
   1424             }
   1425         }
   1426 
   1427         if (allsame)
   1428         {
   1429             best_segment_rd = INT_MAX;
   1430         }
   1431     }
   1432 
   1433     *returntotrate = bsr;
   1434     *returndistortion = bsd;
   1435     *returnyrate = bestsegmentyrate;
   1436 
   1437 
   1438 
   1439     // save partitions
   1440     labels = vp8_mbsplits[best_seg];
   1441     x->e_mbd.mode_info_context->mbmi.partitioning = best_seg;
   1442     x->partition_info->count = vp8_count_labels(labels);
   1443 
   1444     for (i = 0; i < x->partition_info->count; i++)
   1445     {
   1446         int j;
   1447 
   1448         for (j = 0; j < 16; j++)
   1449         {
   1450             if (labels[j] == i)
   1451                 break;
   1452         }
   1453 
   1454         x->partition_info->bmi[i].mode = x->e_mbd.block[j].bmi.mode;
   1455         x->partition_info->bmi[i].mv.as_mv = x->e_mbd.block[j].bmi.mv.as_mv;
   1456     }
   1457 
   1458     return best_segment_rd;
   1459 }
   1460 
   1461 
   1462 int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
   1463 {
   1464     BLOCK *b = &x->block[0];
   1465     BLOCKD *d = &x->e_mbd.block[0];
   1466     MACROBLOCKD *xd = &x->e_mbd;
   1467     B_MODE_INFO best_bmodes[16];
   1468     MB_MODE_INFO best_mbmode;
   1469     PARTITION_INFO best_partition;
   1470     MV best_ref_mv;
   1471     MV mode_mv[MB_MODE_COUNT];
   1472     MB_PREDICTION_MODE this_mode;
   1473     int num00;
   1474     int best_mode_index = 0;
   1475 
   1476     int i;
   1477     int mode_index;
   1478     int mdcounts[4];
   1479     int rate;
   1480     int distortion;
   1481     int best_rd = INT_MAX; // 1 << 30;
   1482     int ref_frame_cost[MAX_REF_FRAMES];
   1483     int rate2, distortion2;
   1484     int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
   1485     int rate_y, UNINITIALIZED_IS_SAFE(rate_uv);
   1486 
   1487     //int all_rds[MAX_MODES];        // Experimental debug code.
   1488     //int all_rates[MAX_MODES];
   1489     //int all_dist[MAX_MODES];
   1490     //int intermodecost[MAX_MODES];
   1491 
   1492     MB_PREDICTION_MODE uv_intra_mode;
   1493     int sse;
   1494     int sum;
   1495     int uvintra_eob = 0;
   1496     int tteob = 0;
   1497     int force_no_skip = 0;
   1498 
   1499     *returnintra = INT_MAX;
   1500 
   1501     vpx_memset(&best_mbmode, 0, sizeof(best_mbmode)); // clean
   1502 
   1503     cpi->mbs_tested_so_far++;          // Count of the number of MBs tested so far this frame
   1504 
   1505     x->skip = 0;
   1506 
   1507     ref_frame_cost[INTRA_FRAME]   = vp8_cost_zero(cpi->prob_intra_coded);
   1508 
   1509     // Experimental code
   1510     // Adjust the RD multiplier based on the best case distortion we saw in the most recently coded mb
   1511     //if ( (cpi->last_mb_distortion) > 0 && (cpi->target_bits_per_mb > 0) )
   1512     /*{
   1513         int tmprdmult;
   1514 
   1515         //tmprdmult = (cpi->last_mb_distortion * 256) / ((cpi->av_per_frame_bandwidth*256)/cpi->common.MBs);
   1516         tmprdmult = (cpi->last_mb_distortion * 256) / cpi->target_bits_per_mb;
   1517         //tmprdmult = tmprdmult;
   1518 
   1519         //if ( tmprdmult > cpi->RDMULT * 2 )
   1520         //  tmprdmult = cpi->RDMULT * 2;
   1521         //else if ( tmprdmult < cpi->RDMULT / 2 )
   1522         //  tmprdmult = cpi->RDMULT / 2;
   1523 
   1524         //tmprdmult = (tmprdmult < 25) ? 25 : tmprdmult;
   1525 
   1526         //x->rdmult = tmprdmult;
   1527 
   1528     }*/
   1529 
   1530     // Special case treatment when GF and ARF are not sensible options for reference
   1531     if (cpi->ref_frame_flags == VP8_LAST_FLAG)
   1532     {
   1533         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
   1534                                         + vp8_cost_zero(255);
   1535         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
   1536                                         + vp8_cost_one(255)
   1537                                         + vp8_cost_zero(128);
   1538         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
   1539                                         + vp8_cost_one(255)
   1540                                         + vp8_cost_one(128);
   1541     }
   1542     else
   1543     {
   1544         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
   1545                                         + vp8_cost_zero(cpi->prob_last_coded);
   1546         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
   1547                                         + vp8_cost_one(cpi->prob_last_coded)
   1548                                         + vp8_cost_zero(cpi->prob_gf_coded);
   1549         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
   1550                                         + vp8_cost_one(cpi->prob_last_coded)
   1551                                         + vp8_cost_one(cpi->prob_gf_coded);
   1552     }
   1553 
   1554     vpx_memset(mode_mv, 0, sizeof(mode_mv));
   1555 
   1556     x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
   1557     vp8_rd_pick_intra_mbuv_mode(cpi, x, &uv_intra_rate, &uv_intra_rate_tokenonly, &uv_intra_distortion);
   1558     uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
   1559     {
   1560         uvintra_eob = 0;
   1561 
   1562         for (i = 16; i < 24; i++)
   1563             uvintra_eob += x->e_mbd.block[i].eob;
   1564     }
   1565 
   1566     for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
   1567     {
   1568         int frame_cost;
   1569         int this_rd = INT_MAX;
   1570         int lf_or_gf = 0;           // Lat Frame (01) or gf/arf (1)
   1571         int disable_skip = 0;
   1572 
   1573         force_no_skip = 0;
   1574 
   1575         // Experimental debug code.
   1576         // Record of rd values recorded for this MB. -1 indicates not measured
   1577         //all_rds[mode_index] = -1;
   1578         //all_rates[mode_index] = -1;
   1579         //all_dist[mode_index] = -1;
   1580         //intermodecost[mode_index] = -1;
   1581 
   1582         // Test best rd so far against threshold for trying this mode.
   1583         if (best_rd <= cpi->rd_threshes[mode_index])
   1584             continue;
   1585 
   1586 
   1587 
   1588         // These variables hold are rolling total cost and distortion for this mode
   1589         rate2 = 0;
   1590         distortion2 = 0;
   1591 
   1592         // Where skip is allowable add in the default per mb cost for the no skip case.
   1593         // where we then decide to skip we have to delete this and replace it with the
   1594         // cost of signallying a skip
   1595         if (cpi->common.mb_no_coeff_skip)
   1596         {
   1597             rate2 += vp8_cost_bit(cpi->prob_skip_false, 0);
   1598         }
   1599 
   1600         this_mode = vp8_mode_order[mode_index];
   1601 
   1602         x->e_mbd.mode_info_context->mbmi.mode = this_mode;
   1603         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
   1604         x->e_mbd.mode_info_context->mbmi.ref_frame = vp8_ref_frame_order[mode_index];
   1605 
   1606         //Only consider ZEROMV/ALTREF_FRAME for alt ref frame.
   1607         if (cpi->is_src_frame_alt_ref)
   1608         {
   1609             if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
   1610                 continue;
   1611         }
   1612 
   1613         if (x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME)
   1614         {
   1615             YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
   1616 
   1617             if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
   1618                 continue;
   1619 
   1620             lf_or_gf = 0;  // Local last frame vs Golden frame flag
   1621 
   1622             // Set up pointers for this macro block into the previous frame recon buffer
   1623             x->e_mbd.pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
   1624             x->e_mbd.pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
   1625             x->e_mbd.pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
   1626         }
   1627         else if (x->e_mbd.mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
   1628         {
   1629             YV12_BUFFER_CONFIG *gld_yv12 = &cpi->common.yv12_fb[cpi->common.gld_fb_idx];
   1630 
   1631             // not supposed to reference gold frame
   1632             if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
   1633                 continue;
   1634 
   1635             lf_or_gf = 1;  // Local last frame vs Golden frame flag
   1636 
   1637             // Set up pointers for this macro block into the previous frame recon buffer
   1638             x->e_mbd.pre.y_buffer = gld_yv12->y_buffer + recon_yoffset;
   1639             x->e_mbd.pre.u_buffer = gld_yv12->u_buffer + recon_uvoffset;
   1640             x->e_mbd.pre.v_buffer = gld_yv12->v_buffer + recon_uvoffset;
   1641         }
   1642         else if (x->e_mbd.mode_info_context->mbmi.ref_frame == ALTREF_FRAME)
   1643         {
   1644             YV12_BUFFER_CONFIG *alt_yv12 = &cpi->common.yv12_fb[cpi->common.alt_fb_idx];
   1645 
   1646             // not supposed to reference alt ref frame
   1647             if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
   1648                 continue;
   1649 
   1650             //if ( !cpi->source_alt_ref_active )
   1651             //  continue;
   1652 
   1653             lf_or_gf = 1;  // Local last frame vs Golden frame flag
   1654 
   1655             // Set up pointers for this macro block into the previous frame recon buffer
   1656             x->e_mbd.pre.y_buffer = alt_yv12->y_buffer + recon_yoffset;
   1657             x->e_mbd.pre.u_buffer = alt_yv12->u_buffer + recon_uvoffset;
   1658             x->e_mbd.pre.v_buffer = alt_yv12->v_buffer + recon_uvoffset;
   1659         }
   1660 
   1661         vp8_find_near_mvs(&x->e_mbd,
   1662                           x->e_mbd.mode_info_context,
   1663                           &mode_mv[NEARESTMV], &mode_mv[NEARMV], &best_ref_mv,
   1664                           mdcounts, x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias);
   1665 
   1666 
   1667         // Estimate the reference frame signaling cost and add it to the rolling cost variable.
   1668         frame_cost = ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
   1669         rate2 += frame_cost;
   1670 
   1671         if (this_mode <= B_PRED)
   1672         {
   1673             for (i = 0; i < 16; i++)
   1674             {
   1675                 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
   1676             }
   1677         }
   1678 
   1679         // Check to see if the testing frequency for this mode is at its max
   1680         // If so then prevent it from being tested and increase the threshold for its testing
   1681         if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
   1682         {
   1683             if (cpi->mbs_tested_so_far  <= cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index])
   1684             {
   1685                 // Increase the threshold for coding this mode to make it less likely to be chosen
   1686                 cpi->rd_thresh_mult[mode_index] += 4;
   1687 
   1688                 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
   1689                     cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
   1690 
   1691                 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
   1692 
   1693                 continue;
   1694             }
   1695         }
   1696 
   1697         // We have now reached the point where we are going to test the current mode so increment the counter for the number of times it has been tested
   1698         cpi->mode_test_hit_counts[mode_index] ++;
   1699 
   1700         // Experimental code. Special case for gf and arf zeromv modes. Increase zbin size to supress noise
   1701         if (cpi->zbin_mode_boost_enabled)
   1702         {
   1703             if ((vp8_mode_order[mode_index] == ZEROMV) && (vp8_ref_frame_order[mode_index] != LAST_FRAME))
   1704                 cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
   1705             else
   1706                 cpi->zbin_mode_boost = 0;
   1707 
   1708             vp8cx_mb_init_quantizer(cpi, x);
   1709         }
   1710 
   1711         switch (this_mode)
   1712         {
   1713         case B_PRED:
   1714 
   1715             // Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
   1716             vp8_rd_pick_intra4x4mby_modes(cpi, x, &rate, &rate_y, &distortion);
   1717             rate2 += rate;
   1718             //rate_y = rate;
   1719             distortion2 += distortion;
   1720             rate2 += uv_intra_rate;
   1721             rate_uv = uv_intra_rate_tokenonly;
   1722             distortion2 += uv_intra_distortion;
   1723             break;
   1724 
   1725         case SPLITMV:
   1726         {
   1727             int frame_cost_rd = RDFUNC(x->rdmult, x->rddiv, frame_cost, 0, cpi->target_bits_per_mb);
   1728             int saved_rate = rate2;
   1729 
   1730             // vp8_rd_pick_best_mbsegmentation looks only at Y and does not account for frame_cost.
   1731             // (best_rd - frame_cost_rd) is thus a conservative breakout number.
   1732             int breakout_rd = best_rd - frame_cost_rd;
   1733             int tmp_rd;
   1734 
   1735             if (x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME)
   1736                 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWMV], cpi->common.full_pixel) ;
   1737             else if (x->e_mbd.mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
   1738                 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWG], cpi->common.full_pixel) ;
   1739             else
   1740                 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWA], cpi->common.full_pixel) ;
   1741 
   1742             rate2 += rate;
   1743             distortion2 += distortion;
   1744 
   1745             // If even the 'Y' rd value of split is higher than best so far then dont bother looking at UV
   1746             if (tmp_rd < breakout_rd)
   1747             {
   1748                 // Now work out UV cost and add it in
   1749                 vp8_rd_inter_uv(cpi, x, &rate, &distortion, cpi->common.full_pixel);
   1750                 rate2 += rate;
   1751                 rate_uv = rate;
   1752                 distortion2 += distortion;
   1753 
   1754             }
   1755             else
   1756             {
   1757                 this_rd = INT_MAX;
   1758                 disable_skip = 1;
   1759             }
   1760 
   1761             // Trap cases where the best split mode has all vectors coded 0,0 (or all the same)
   1762             if (0)
   1763             {
   1764                 int allsame = 1;
   1765 
   1766                 for (i = 1; i < 16; i++)
   1767                 {
   1768                     BLOCKD *bd = &x->e_mbd.block[i];
   1769 
   1770                     if (bd->bmi.mv.as_int != x->e_mbd.block[0].bmi.mv.as_int)   //(bmvs[i].col != bmvs[i-1].col) || (bmvs[i].row != bmvs[i-1].row ) )
   1771                     {
   1772                         allsame = 0;
   1773                         break;
   1774                     }
   1775                 }
   1776 
   1777                 if (allsame)
   1778                 {
   1779                     // reset mode and mv and jump to newmv
   1780                     this_mode = NEWMV;
   1781                     distortion2 = 0;
   1782                     rate2 = saved_rate;
   1783                     mode_mv[NEWMV].row = x->e_mbd.block[0].bmi.mv.as_mv.row;
   1784                     mode_mv[NEWMV].col = x->e_mbd.block[0].bmi.mv.as_mv.col;
   1785                     rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
   1786                     goto mv_selected;
   1787                 }
   1788             }
   1789 
   1790             // trap cases where the 8x8s can be promoted to 8x16s or 16x8s
   1791             if (0)//x->partition_info->count == 4)
   1792             {
   1793 
   1794                 if (x->partition_info->bmi[0].mv.as_int == x->partition_info->bmi[1].mv.as_int
   1795                     && x->partition_info->bmi[2].mv.as_int == x->partition_info->bmi[3].mv.as_int)
   1796                 {
   1797                     const int *labels = vp8_mbsplits[2];
   1798                     x->e_mbd.mode_info_context->mbmi.partitioning = 0;
   1799                     rate -= vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + 2);
   1800                     rate += vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings);
   1801                     //rate -=  x->inter_bmode_costs[  x->partition_info->bmi[1]];
   1802                     //rate -=  x->inter_bmode_costs[  x->partition_info->bmi[3]];
   1803                     x->partition_info->bmi[1] = x->partition_info->bmi[2];
   1804                 }
   1805             }
   1806 
   1807         }
   1808         break;
   1809         case DC_PRED:
   1810         case V_PRED:
   1811         case H_PRED:
   1812         case TM_PRED:
   1813             x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
   1814             vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
   1815             {
   1816                 macro_block_yrd(x, &rate, &distortion, IF_RTCD(&cpi->rtcd.encodemb)) ;
   1817                 rate2 += rate;
   1818                 rate_y = rate;
   1819                 distortion2 += distortion;
   1820                 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
   1821                 rate2 += uv_intra_rate;
   1822                 rate_uv = uv_intra_rate_tokenonly;
   1823                 distortion2 += uv_intra_distortion;
   1824             }
   1825             break;
   1826 
   1827         case NEWMV:
   1828 
   1829             // Decrement full search counter
   1830             if (cpi->check_freq[lf_or_gf] > 0)
   1831                 cpi->check_freq[lf_or_gf] --;
   1832 
   1833             {
   1834                 int thissme;
   1835                 int bestsme = INT_MAX;
   1836                 int step_param = cpi->sf.first_step;
   1837                 int search_range;
   1838                 int further_steps;
   1839                 int n;
   1840 
   1841                 // Work out how long a search we should do
   1842                 search_range = MAXF(abs(best_ref_mv.col), abs(best_ref_mv.row)) >> 3;
   1843 
   1844                 if (search_range >= x->vector_range)
   1845                     x->vector_range = search_range;
   1846                 else if (x->vector_range > cpi->sf.min_fs_radius)
   1847                     x->vector_range--;
   1848 
   1849                 // Initial step/diamond search
   1850                 {
   1851                     int sadpb = x->sadperbit16;
   1852 
   1853                     if (cpi->sf.search_method == HEX)
   1854                     {
   1855                         bestsme = vp8_hex_search(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param, sadpb/*x->errorperbit*/, &num00, cpi->fn_ptr.vf, cpi->fn_ptr.sdf, x->mvsadcost, x->mvcost);
   1856                         mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
   1857                         mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
   1858                     }
   1859                     else
   1860                     {
   1861                         bestsme = cpi->diamond_search_sad(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param, sadpb / 2/*x->errorperbit*/, &num00, &cpi->fn_ptr, x->mvsadcost, x->mvcost); //sadpb < 9
   1862                         mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
   1863                         mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
   1864 
   1865                         // Further step/diamond searches as necessary
   1866                         n = 0;
   1867                         further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
   1868 
   1869                         n = num00;
   1870                         num00 = 0;
   1871 
   1872                         while (n < further_steps)
   1873                         {
   1874                             n++;
   1875 
   1876                             if (num00)
   1877                                 num00--;
   1878                             else
   1879                             {
   1880                                 thissme = cpi->diamond_search_sad(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param + n, sadpb / 4/*x->errorperbit*/, &num00, &cpi->fn_ptr, x->mvsadcost, x->mvcost); //sadpb = 9
   1881 
   1882                                 if (thissme < bestsme)
   1883                                 {
   1884                                     bestsme = thissme;
   1885                                     mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
   1886                                     mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
   1887                                 }
   1888                                 else
   1889                                 {
   1890                                     d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
   1891                                     d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
   1892                                 }
   1893                             }
   1894                         }
   1895                     }
   1896 
   1897                 }
   1898 
   1899                 // Should we do a full search
   1900                 if (!cpi->check_freq[lf_or_gf] || cpi->do_full[lf_or_gf])
   1901                 {
   1902                     int thissme;
   1903                     int full_flag_thresh = 0;
   1904 
   1905                     // Update x->vector_range based on best vector found in step search
   1906                     search_range = MAXF(abs(d->bmi.mv.as_mv.row), abs(d->bmi.mv.as_mv.col));
   1907 
   1908                     if (search_range > x->vector_range)
   1909                         x->vector_range = search_range;
   1910                     else
   1911                         search_range = x->vector_range;
   1912 
   1913                     // Apply limits
   1914                     search_range = (search_range > cpi->sf.max_fs_radius) ? cpi->sf.max_fs_radius : search_range;
   1915                     {
   1916                         int sadpb = x->sadperbit16 >> 2;
   1917                         thissme = cpi->full_search_sad(x, b, d, &best_ref_mv, sadpb, search_range, &cpi->fn_ptr, x->mvcost, x->mvsadcost);
   1918                     }
   1919 
   1920                     // Barrier threshold to initiating full search
   1921                     // full_flag_thresh = 10 + (thissme >> 7);
   1922                     if ((thissme + full_flag_thresh) < bestsme)
   1923                     {
   1924                         cpi->do_full[lf_or_gf] ++;
   1925                         bestsme = thissme;
   1926                     }
   1927                     else if (thissme < bestsme)
   1928                         bestsme = thissme;
   1929                     else
   1930                     {
   1931                         cpi->do_full[lf_or_gf] = cpi->do_full[lf_or_gf] >> 1;
   1932                         cpi->check_freq[lf_or_gf] = cpi->sf.full_freq[lf_or_gf];
   1933 
   1934                         // The full search result is actually worse so re-instate the previous best vector
   1935                         d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
   1936                         d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
   1937                     }
   1938                 }
   1939 
   1940                 if (bestsme < INT_MAX)
   1941                     // cpi->find_fractional_mv_step(x,b,d,&d->bmi.mv.as_mv,&best_ref_mv,x->errorperbit/2,cpi->fn_ptr.svf,cpi->fn_ptr.vf,x->mvcost);  // normal mvc=11
   1942                     cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv.as_mv, &best_ref_mv, x->errorperbit / 4, cpi->fn_ptr.svf, cpi->fn_ptr.vf, x->mvcost);
   1943 
   1944                 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
   1945                 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
   1946 
   1947                 // Add the new motion vector cost to our rolling cost variable
   1948                 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
   1949 
   1950             }
   1951 
   1952         case NEARESTMV:
   1953         case NEARMV:
   1954 
   1955             // Clip "next_nearest" so that it does not extend to far out of image
   1956             if (mode_mv[this_mode].col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN))
   1957                 mode_mv[this_mode].col = xd->mb_to_left_edge - LEFT_TOP_MARGIN;
   1958             else if (mode_mv[this_mode].col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN)
   1959                 mode_mv[this_mode].col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN;
   1960 
   1961             if (mode_mv[this_mode].row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN))
   1962                 mode_mv[this_mode].row = xd->mb_to_top_edge - LEFT_TOP_MARGIN;
   1963             else if (mode_mv[this_mode].row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN)
   1964                 mode_mv[this_mode].row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN;
   1965 
   1966             // Do not bother proceeding if the vector (from newmv,nearest or near) is 0,0 as this should then be coded using the zeromv mode.
   1967             if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) &&
   1968                 ((mode_mv[this_mode].row == 0) && (mode_mv[this_mode].col == 0)))
   1969                 continue;
   1970 
   1971         case ZEROMV:
   1972 
   1973         mv_selected:
   1974 
   1975             // Trap vectors that reach beyond the UMV borders
   1976             // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
   1977             // because of the lack of break statements in the previous two cases.
   1978             if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
   1979                 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
   1980                 continue;
   1981 
   1982             vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
   1983             vp8_build_inter_predictors_mby(&x->e_mbd);
   1984             VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)(x->src.y_buffer, x->src.y_stride, x->e_mbd.predictor, 16, (unsigned int *)(&sse), &sum);
   1985 
   1986             if (cpi->active_map_enabled && x->active_ptr[0] == 0)
   1987             {
   1988                 x->skip = 1;
   1989             }
   1990             else if (sse < x->encode_breakout)
   1991             {
   1992                 // Check u and v to make sure skip is ok
   1993                 int sse2 = 0;
   1994 
   1995                 sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
   1996 
   1997                 if (sse2 * 2 < x->encode_breakout)
   1998                 {
   1999                     x->skip = 1;
   2000                     distortion2 = sse;
   2001                     rate2 = 500;
   2002 
   2003                     disable_skip = 1;    // We have no real rate data so trying to adjust for rate_y and rate_uv below will cause problems.
   2004                     this_rd = RDFUNC(x->rdmult, x->rddiv, rate2, distortion2, cpi->target_bits_per_mb);
   2005 
   2006                     break;              // (PGW) Move break here from below - for now at least
   2007                 }
   2008                 else
   2009                     x->skip = 0;
   2010             }
   2011 
   2012             //intermodecost[mode_index] = vp8_cost_mv_ref(this_mode, mdcounts);   // Experimental debug code
   2013 
   2014             // Add in the Mv/mode cost
   2015             rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
   2016 
   2017             // Y cost and distortion
   2018             macro_block_yrd(x, &rate, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
   2019             rate2 += rate;
   2020             rate_y = rate;
   2021             distortion2 += distortion;
   2022 
   2023             // UV cost and distortion
   2024             vp8_rd_inter_uv(cpi, x, &rate, &distortion, cpi->common.full_pixel);
   2025             rate2 += rate;
   2026             rate_uv = rate;
   2027             distortion2 += distortion;
   2028             break;
   2029 
   2030         default:
   2031             break;
   2032         }
   2033 
   2034         if (!disable_skip)
   2035         {
   2036             // Test for the condition where skip block will be activated because there are no non zero coefficients and make any necessary adjustment for rate
   2037             if (cpi->common.mb_no_coeff_skip)
   2038             {
   2039                 tteob = 0;
   2040 
   2041                 for (i = 0; i <= 24; i++)
   2042                 {
   2043                     tteob += x->e_mbd.block[i].eob;
   2044                 }
   2045 
   2046                 if (tteob == 0)
   2047                 {
   2048 #if 1
   2049                     rate2 -= (rate_y + rate_uv);
   2050 
   2051                     // Back out no skip flag costing and add in skip flag costing
   2052                     if (cpi->prob_skip_false)
   2053                     {
   2054                         rate2 += vp8_cost_bit(cpi->prob_skip_false, 1);
   2055                         rate2 -= vp8_cost_bit(cpi->prob_skip_false, 0);
   2056                     }
   2057 
   2058 #else
   2059                     int rateuseskip;
   2060                     int ratenotuseskip;
   2061 
   2062 
   2063 
   2064                     ratenotuseskip = rate_y + rate_uv + vp8_cost_bit(cpi->prob_skip_false, 0);
   2065                     rateuseskip    = vp8_cost_bit(cpi->prob_skip_false, 1);
   2066 
   2067                     if (1) // rateuseskip<ratenotuseskip)
   2068                     {
   2069                         rate2 -= ratenotuseskip;
   2070                         rate2 += rateuseskip;
   2071                         force_no_skip = 0;
   2072                     }
   2073                     else
   2074                     {
   2075                         force_no_skip = 1;
   2076                     }
   2077 
   2078 #endif
   2079                 }
   2080 
   2081 #if             0
   2082                 else
   2083                 {
   2084                     int rateuseskip;
   2085                     int ratenotuseskip;
   2086                     int maxdistortion;
   2087                     int minrate;
   2088                     int skip_rd;
   2089 
   2090                     // distortion when no coeff is encoded
   2091                     maxdistortion = macro_block_max_error(x);
   2092 
   2093                     ratenotuseskip = rate_y + rate_uv + vp8_cost_bit(cpi->prob_skip_false, 0);
   2094                     rateuseskip    = vp8_cost_bit(cpi->prob_skip_false, 1);
   2095 
   2096                     minrate         = rateuseskip - ratenotuseskip;
   2097 
   2098                     skip_rd = RDFUNC(x->rdmult, x->rddiv, minrate, maxdistortion - distortion2, cpi->target_bits_per_mb);
   2099 
   2100                     if (skip_rd + 50 < 0 && x->e_mbd.mbmi.ref_frame != INTRA_FRAME && rate_y + rate_uv < 4000)
   2101                     {
   2102                         force_no_skip = 1;
   2103                         rate2       = rate2 + rateuseskip - ratenotuseskip;
   2104                         distortion2 =  maxdistortion;
   2105                     }
   2106                     else
   2107                     {
   2108                         force_no_skip = 0;
   2109                     }
   2110 
   2111                 }
   2112 
   2113 #endif
   2114 
   2115             }
   2116 
   2117             // Calculate the final RD estimate for this mode
   2118             this_rd = RDFUNC(x->rdmult, x->rddiv, rate2, distortion2, cpi->target_bits_per_mb);
   2119         }
   2120 
   2121         // Experimental debug code.
   2122         //all_rds[mode_index] = this_rd;
   2123         //all_rates[mode_index] = rate2;
   2124         //all_dist[mode_index] = distortion2;
   2125 
   2126         if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME)  && (this_rd < *returnintra))
   2127         {
   2128             *returnintra = this_rd ;
   2129         }
   2130 
   2131         // Did this mode help.. i.i is it the new best mode
   2132         if (this_rd < best_rd || x->skip)
   2133         {
   2134             // Note index of best mode so far
   2135             best_mode_index = mode_index;
   2136             x->e_mbd.mode_info_context->mbmi.force_no_skip = force_no_skip;
   2137 
   2138             if (this_mode <= B_PRED)
   2139             {
   2140                 x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode;
   2141             }
   2142 
   2143             *returnrate = rate2;
   2144             *returndistortion = distortion2;
   2145             best_rd = this_rd;
   2146             vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
   2147             vpx_memcpy(&best_partition, x->partition_info, sizeof(PARTITION_INFO));
   2148 
   2149             for (i = 0; i < 16; i++)
   2150             {
   2151                 vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
   2152             }
   2153 
   2154             // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
   2155             cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
   2156             cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
   2157         }
   2158 
   2159         // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
   2160         else
   2161         {
   2162             cpi->rd_thresh_mult[mode_index] += 4;
   2163 
   2164             if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
   2165                 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
   2166 
   2167             cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
   2168         }
   2169 
   2170         if (x->skip)
   2171             break;
   2172     }
   2173 
   2174     // Reduce the activation RD thresholds for the best choice mode
   2175     if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
   2176     {
   2177         int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 2);
   2178 
   2179         cpi->rd_thresh_mult[best_mode_index] = (cpi->rd_thresh_mult[best_mode_index] >= (MIN_THRESHMULT + best_adjustment)) ? cpi->rd_thresh_mult[best_mode_index] - best_adjustment : MIN_THRESHMULT;
   2180         cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
   2181 
   2182         // If we chose a split mode then reset the new MV thresholds as well
   2183         /*if ( vp8_mode_order[best_mode_index] == SPLITMV )
   2184         {
   2185             best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWMV] >> 4);
   2186             cpi->rd_thresh_mult[THR_NEWMV] = (cpi->rd_thresh_mult[THR_NEWMV] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWMV]-best_adjustment: MIN_THRESHMULT;
   2187             cpi->rd_threshes[THR_NEWMV] = (cpi->rd_baseline_thresh[THR_NEWMV] >> 7) * cpi->rd_thresh_mult[THR_NEWMV];
   2188 
   2189             best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWG] >> 4);
   2190             cpi->rd_thresh_mult[THR_NEWG] = (cpi->rd_thresh_mult[THR_NEWG] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWG]-best_adjustment: MIN_THRESHMULT;
   2191             cpi->rd_threshes[THR_NEWG] = (cpi->rd_baseline_thresh[THR_NEWG] >> 7) * cpi->rd_thresh_mult[THR_NEWG];
   2192 
   2193             best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWA] >> 4);
   2194             cpi->rd_thresh_mult[THR_NEWA] = (cpi->rd_thresh_mult[THR_NEWA] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWA]-best_adjustment: MIN_THRESHMULT;
   2195             cpi->rd_threshes[THR_NEWA] = (cpi->rd_baseline_thresh[THR_NEWA] >> 7) * cpi->rd_thresh_mult[THR_NEWA];
   2196         }*/
   2197 
   2198     }
   2199 
   2200     // If we have chosen new mv or split then decay the full search check count more quickly.
   2201     if ((vp8_mode_order[best_mode_index] == NEWMV) || (vp8_mode_order[best_mode_index] == SPLITMV))
   2202     {
   2203         int lf_or_gf = (vp8_ref_frame_order[best_mode_index] == LAST_FRAME) ? 0 : 1;
   2204 
   2205         if (cpi->check_freq[lf_or_gf] && !cpi->do_full[lf_or_gf])
   2206         {
   2207             cpi->check_freq[lf_or_gf] --;
   2208         }
   2209     }
   2210 
   2211     // Keep a record of best mode index that we chose
   2212     cpi->last_best_mode_index = best_mode_index;
   2213 
   2214     // Note how often each mode chosen as best
   2215     cpi->mode_chosen_counts[best_mode_index] ++;
   2216 
   2217 
   2218     if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
   2219     {
   2220         best_mbmode.mode = ZEROMV;
   2221         best_mbmode.ref_frame = ALTREF_FRAME;
   2222         best_mbmode.mv.as_int = 0;
   2223         best_mbmode.uv_mode = 0;
   2224         best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
   2225         best_mbmode.partitioning = 0;
   2226         best_mbmode.dc_diff = 0;
   2227 
   2228         vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
   2229         vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
   2230 
   2231         for (i = 0; i < 16; i++)
   2232         {
   2233             vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
   2234         }
   2235 
   2236         x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
   2237 
   2238         return best_rd;
   2239     }
   2240 
   2241 
   2242     // macroblock modes
   2243     vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
   2244     vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
   2245 
   2246     for (i = 0; i < 16; i++)
   2247     {
   2248         vpx_memcpy(&x->e_mbd.block[i].bmi, &best_bmodes[i], sizeof(B_MODE_INFO));
   2249     }
   2250 
   2251     x->e_mbd.mode_info_context->mbmi.mv.as_mv = x->e_mbd.block[15].bmi.mv.as_mv;
   2252 
   2253     return best_rd;
   2254 }
   2255 #endif
   2256 
   2257