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 <limits.h>
     13 #include "vpx_ports/config.h"
     14 #include "onyx_int.h"
     15 #include "modecosts.h"
     16 #include "encodeintra.h"
     17 #include "entropymode.h"
     18 #include "pickinter.h"
     19 #include "findnearmv.h"
     20 #include "encodemb.h"
     21 #include "reconinter.h"
     22 #include "reconintra.h"
     23 #include "reconintra4x4.h"
     24 #include "g_common.h"
     25 #include "variance.h"
     26 #include "mcomp.h"
     27 
     28 #include "vpx_mem/vpx_mem.h"
     29 
     30 #if CONFIG_RUNTIME_CPU_DETECT
     31 #define IF_RTCD(x) (x)
     32 #else
     33 #define IF_RTCD(x)  NULL
     34 #endif
     35 
     36 extern int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd);
     37 
     38 #ifdef SPEEDSTATS
     39 extern unsigned int cnt_pm;
     40 #endif
     41 
     42 extern const MV_REFERENCE_FRAME vp8_ref_frame_order[MAX_MODES];
     43 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
     44 
     45 
     46 extern unsigned int (*vp8_get16x16pred_error)(unsigned char *src_ptr, int src_stride, unsigned char *ref_ptr, int ref_stride);
     47 extern unsigned int (*vp8_get4x4sse_cs)(unsigned char *src_ptr, int  source_stride, unsigned char *ref_ptr, int  recon_stride);
     48 extern int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *best_ref_mv, int best_rd, int *, int *, int *, int, int *mvcost[2], int, int fullpixel);
     49 extern int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]);
     50 extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv);
     51 
     52 
     53 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d, MV *bestmv, MV *ref_mv, int error_per_bit, vp8_subpixvariance_fn_t svf, vp8_variance_fn_t vf, int *mvcost[2])
     54 {
     55     (void) b;
     56     (void) d;
     57     (void) ref_mv;
     58     (void) error_per_bit;
     59     (void) svf;
     60     (void) vf;
     61     (void) mvcost;
     62     bestmv->row <<= 3;
     63     bestmv->col <<= 3;
     64     return 0;
     65 }
     66 
     67 
     68 static int get_inter_mbpred_error(MACROBLOCK *mb, vp8_subpixvariance_fn_t svf, vp8_variance_fn_t vf, unsigned int *sse)
     69 {
     70 
     71     BLOCK *b = &mb->block[0];
     72     BLOCKD *d = &mb->e_mbd.block[0];
     73     unsigned char *what = (*(b->base_src) + b->src);
     74     int what_stride = b->src_stride;
     75     unsigned char *in_what = *(d->base_pre) + d->pre ;
     76     int in_what_stride = d->pre_stride;
     77     int xoffset = d->bmi.mv.as_mv.col & 7;
     78     int yoffset = d->bmi.mv.as_mv.row & 7;
     79 
     80     in_what += (d->bmi.mv.as_mv.row >> 3) * d->pre_stride + (d->bmi.mv.as_mv.col >> 3);
     81 
     82     if (xoffset | yoffset)
     83     {
     84         return svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
     85     }
     86     else
     87     {
     88         return vf(what, what_stride, in_what, in_what_stride, sse);
     89     }
     90 
     91 }
     92 
     93 unsigned int vp8_get16x16pred_error_c
     94 (
     95     unsigned char *src_ptr,
     96     int src_stride,
     97     unsigned char *ref_ptr,
     98     int ref_stride,
     99     int max_sad
    100 )
    101 {
    102     unsigned pred_error = 0;
    103     int i, j;
    104     int sum = 0;
    105 
    106     for (i = 0; i < 16; i++)
    107     {
    108         int diff;
    109 
    110         for (j = 0; j < 16; j++)
    111         {
    112             diff = src_ptr[j] - ref_ptr[j];
    113             sum += diff;
    114             pred_error += diff * diff;
    115         }
    116 
    117         src_ptr += src_stride;
    118         ref_ptr += ref_stride;
    119     }
    120 
    121     pred_error -= sum * sum / 256;
    122     return pred_error;
    123 }
    124 
    125 
    126 unsigned int vp8_get4x4sse_cs_c
    127 (
    128     unsigned char *src_ptr,
    129     int  source_stride,
    130     unsigned char *ref_ptr,
    131     int  recon_stride,
    132     int max_sad
    133 )
    134 {
    135     int distortion = 0;
    136     int r, c;
    137 
    138     for (r = 0; r < 4; r++)
    139     {
    140         for (c = 0; c < 4; c++)
    141         {
    142             int diff = src_ptr[c] - ref_ptr[c];
    143             distortion += diff * diff;
    144         }
    145 
    146         src_ptr += source_stride;
    147         ref_ptr += recon_stride;
    148     }
    149 
    150     return distortion;
    151 }
    152 
    153 static int get_prediction_error(BLOCK *be, BLOCKD *b, const vp8_variance_rtcd_vtable_t *rtcd)
    154 {
    155     unsigned char *sptr;
    156     unsigned char *dptr;
    157     sptr = (*(be->base_src) + be->src);
    158     dptr = b->predictor;
    159 
    160     return VARIANCE_INVOKE(rtcd, get4x4sse_cs)(sptr, be->src_stride, dptr, 16, 0x7fffffff);
    161 
    162 }
    163 
    164 static int pick_intra4x4block(
    165     const VP8_ENCODER_RTCD *rtcd,
    166     MACROBLOCK *x,
    167     BLOCK *be,
    168     BLOCKD *b,
    169     B_PREDICTION_MODE *best_mode,
    170     B_PREDICTION_MODE above,
    171     B_PREDICTION_MODE left,
    172     ENTROPY_CONTEXT *a,
    173     ENTROPY_CONTEXT *l,
    174 
    175     int *bestrate,
    176     int *bestdistortion)
    177 {
    178     B_PREDICTION_MODE mode;
    179     int best_rd = INT_MAX;       // 1<<30
    180     int rate;
    181     int distortion;
    182     unsigned int *mode_costs;
    183     (void) l;
    184     (void) a;
    185 
    186     if (x->e_mbd.frame_type == KEY_FRAME)
    187     {
    188         mode_costs = x->bmode_costs[above][left];
    189     }
    190     else
    191     {
    192         mode_costs = x->inter_bmode_costs;
    193     }
    194 
    195     for (mode = B_DC_PRED; mode <= B_HE_PRED /*B_HU_PRED*/; mode++)
    196     {
    197         int this_rd;
    198 
    199         rate = mode_costs[mode];
    200         vp8_predict_intra4x4(b, mode, b->predictor);
    201         distortion = get_prediction_error(be, b, &rtcd->variance);
    202         this_rd = RD_ESTIMATE(x->rdmult, x->rddiv, rate, distortion);
    203 
    204         if (this_rd < best_rd)
    205         {
    206             *bestrate = rate;
    207             *bestdistortion = distortion;
    208             best_rd = this_rd;
    209             *best_mode = mode;
    210         }
    211     }
    212 
    213     b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
    214     vp8_encode_intra4x4block(rtcd, x, be, b, b->bmi.mode);
    215     return best_rd;
    216 }
    217 
    218 
    219 int vp8_pick_intra4x4mby_modes(const VP8_ENCODER_RTCD *rtcd, MACROBLOCK *mb, int *Rate, int *best_dist)
    220 {
    221     MACROBLOCKD *const xd = &mb->e_mbd;
    222     int i;
    223     int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
    224     int error = RD_ESTIMATE(mb->rdmult, mb->rddiv, cost, 0); // Rd estimate for the cost of the block prediction mode
    225     int distortion = 0;
    226     ENTROPY_CONTEXT_PLANES t_above, t_left;
    227     ENTROPY_CONTEXT *ta;
    228     ENTROPY_CONTEXT *tl;
    229 
    230     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
    231     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
    232 
    233     ta = (ENTROPY_CONTEXT *)&t_above;
    234     tl = (ENTROPY_CONTEXT *)&t_left;
    235 
    236     vp8_intra_prediction_down_copy(xd);
    237 
    238     for (i = 0; i < 16; i++)
    239     {
    240         MODE_INFO *const mic = xd->mode_info_context;
    241         const int mis = xd->mode_info_stride;
    242         const B_PREDICTION_MODE A = vp8_above_bmi(mic, i, mis)->mode;
    243         const B_PREDICTION_MODE L = vp8_left_bmi(mic, i)->mode;
    244         B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
    245         int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
    246 
    247         error += pick_intra4x4block(rtcd,
    248                                     mb, mb->block + i, xd->block + i, &best_mode, A, L,
    249                                     ta + vp8_block2above[i],
    250                                     tl + vp8_block2left[i], &r, &d);
    251 
    252         cost += r;
    253         distortion += d;
    254 
    255         mic->bmi[i].mode = xd->block[i].bmi.mode = best_mode;
    256 
    257         // Break out case where we have already exceeded best so far value that was bassed in
    258         if (distortion > *best_dist)
    259             break;
    260     }
    261 
    262     for (i = 0; i < 16; i++)
    263         xd->block[i].bmi.mv.as_int = 0;
    264 
    265     *Rate = cost;
    266 
    267     if (i == 16)
    268         *best_dist = distortion;
    269     else
    270         *best_dist = INT_MAX;
    271 
    272 
    273     return error;
    274 }
    275 
    276 int vp8_pick_intra_mbuv_mode(MACROBLOCK *mb)
    277 {
    278 
    279     MACROBLOCKD *x = &mb->e_mbd;
    280     unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
    281     unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
    282     unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
    283     unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
    284     int uvsrc_stride = mb->block[16].src_stride;
    285     unsigned char uleft_col[8];
    286     unsigned char vleft_col[8];
    287     unsigned char utop_left = uabove_row[-1];
    288     unsigned char vtop_left = vabove_row[-1];
    289     int i, j;
    290     int expected_udc;
    291     int expected_vdc;
    292     int shift;
    293     int Uaverage = 0;
    294     int Vaverage = 0;
    295     int diff;
    296     int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
    297     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
    298 
    299 
    300     for (i = 0; i < 8; i++)
    301     {
    302         uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
    303         vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
    304     }
    305 
    306     if (!x->up_available && !x->left_available)
    307     {
    308         expected_udc = 128;
    309         expected_vdc = 128;
    310     }
    311     else
    312     {
    313         shift = 2;
    314 
    315         if (x->up_available)
    316         {
    317 
    318             for (i = 0; i < 8; i++)
    319             {
    320                 Uaverage += uabove_row[i];
    321                 Vaverage += vabove_row[i];
    322             }
    323 
    324             shift ++;
    325 
    326         }
    327 
    328         if (x->left_available)
    329         {
    330             for (i = 0; i < 8; i++)
    331             {
    332                 Uaverage += uleft_col[i];
    333                 Vaverage += vleft_col[i];
    334             }
    335 
    336             shift ++;
    337 
    338         }
    339 
    340         expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
    341         expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
    342     }
    343 
    344 
    345     for (i = 0; i < 8; i++)
    346     {
    347         for (j = 0; j < 8; j++)
    348         {
    349 
    350             int predu = uleft_col[i] + uabove_row[j] - utop_left;
    351             int predv = vleft_col[i] + vabove_row[j] - vtop_left;
    352             int u_p, v_p;
    353 
    354             u_p = usrc_ptr[j];
    355             v_p = vsrc_ptr[j];
    356 
    357             if (predu < 0)
    358                 predu = 0;
    359 
    360             if (predu > 255)
    361                 predu = 255;
    362 
    363             if (predv < 0)
    364                 predv = 0;
    365 
    366             if (predv > 255)
    367                 predv = 255;
    368 
    369 
    370             diff = u_p - expected_udc;
    371             pred_error[DC_PRED] += diff * diff;
    372             diff = v_p - expected_vdc;
    373             pred_error[DC_PRED] += diff * diff;
    374 
    375 
    376             diff = u_p - uabove_row[j];
    377             pred_error[V_PRED] += diff * diff;
    378             diff = v_p - vabove_row[j];
    379             pred_error[V_PRED] += diff * diff;
    380 
    381 
    382             diff = u_p - uleft_col[i];
    383             pred_error[H_PRED] += diff * diff;
    384             diff = v_p - vleft_col[i];
    385             pred_error[H_PRED] += diff * diff;
    386 
    387 
    388             diff = u_p - predu;
    389             pred_error[TM_PRED] += diff * diff;
    390             diff = v_p - predv;
    391             pred_error[TM_PRED] += diff * diff;
    392 
    393 
    394         }
    395 
    396         usrc_ptr += uvsrc_stride;
    397         vsrc_ptr += uvsrc_stride;
    398 
    399         if (i == 3)
    400         {
    401             usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
    402             vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
    403         }
    404 
    405 
    406 
    407     }
    408 
    409 
    410     for (i = DC_PRED; i <= TM_PRED; i++)
    411     {
    412         if (best_error > pred_error[i])
    413         {
    414             best_error = pred_error[i];
    415             best_mode = (MB_PREDICTION_MODE)i;
    416         }
    417     }
    418 
    419 
    420     mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
    421     return best_error;
    422 
    423 }
    424 
    425 
    426 int vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
    427 {
    428     BLOCK *b = &x->block[0];
    429     BLOCKD *d = &x->e_mbd.block[0];
    430     MACROBLOCKD *xd = &x->e_mbd;
    431     B_MODE_INFO best_bmodes[16];
    432     MB_MODE_INFO best_mbmode;
    433     PARTITION_INFO best_partition;
    434     MV best_ref_mv1;
    435     MV mode_mv[MB_MODE_COUNT];
    436     MB_PREDICTION_MODE this_mode;
    437     int num00;
    438     int i;
    439     int mdcounts[4];
    440     int best_rd = INT_MAX; // 1 << 30;
    441     int best_intra_rd = INT_MAX;
    442     int mode_index;
    443     int ref_frame_cost[MAX_REF_FRAMES];
    444     int rate;
    445     int rate2;
    446     int distortion2;
    447     int bestsme;
    448     //int all_rds[MAX_MODES];         // Experimental debug code.
    449     int best_mode_index = 0;
    450     int sse = INT_MAX;
    451 
    452     MV nearest_mv[4];
    453     MV near_mv[4];
    454     MV best_ref_mv[4];
    455     int MDCounts[4][4];
    456     unsigned char *y_buffer[4];
    457     unsigned char *u_buffer[4];
    458     unsigned char *v_buffer[4];
    459 
    460     int skip_mode[4] = {0, 0, 0, 0};
    461 
    462     vpx_memset(mode_mv, 0, sizeof(mode_mv));
    463     vpx_memset(nearest_mv, 0, sizeof(nearest_mv));
    464     vpx_memset(near_mv, 0, sizeof(near_mv));
    465     vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
    466 
    467 
    468     // set up all the refframe dependent pointers.
    469     if (cpi->ref_frame_flags & VP8_LAST_FLAG)
    470     {
    471         YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
    472 
    473         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[LAST_FRAME], &near_mv[LAST_FRAME],
    474                           &best_ref_mv[LAST_FRAME], MDCounts[LAST_FRAME], LAST_FRAME, cpi->common.ref_frame_sign_bias);
    475 
    476         y_buffer[LAST_FRAME] = lst_yv12->y_buffer + recon_yoffset;
    477         u_buffer[LAST_FRAME] = lst_yv12->u_buffer + recon_uvoffset;
    478         v_buffer[LAST_FRAME] = lst_yv12->v_buffer + recon_uvoffset;
    479     }
    480     else
    481         skip_mode[LAST_FRAME] = 1;
    482 
    483     if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
    484     {
    485         YV12_BUFFER_CONFIG *gld_yv12 = &cpi->common.yv12_fb[cpi->common.gld_fb_idx];
    486 
    487         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[GOLDEN_FRAME], &near_mv[GOLDEN_FRAME],
    488                           &best_ref_mv[GOLDEN_FRAME], MDCounts[GOLDEN_FRAME], GOLDEN_FRAME, cpi->common.ref_frame_sign_bias);
    489 
    490         y_buffer[GOLDEN_FRAME] = gld_yv12->y_buffer + recon_yoffset;
    491         u_buffer[GOLDEN_FRAME] = gld_yv12->u_buffer + recon_uvoffset;
    492         v_buffer[GOLDEN_FRAME] = gld_yv12->v_buffer + recon_uvoffset;
    493     }
    494     else
    495         skip_mode[GOLDEN_FRAME] = 1;
    496 
    497     if (cpi->ref_frame_flags & VP8_ALT_FLAG && cpi->source_alt_ref_active)
    498     {
    499         YV12_BUFFER_CONFIG *alt_yv12 = &cpi->common.yv12_fb[cpi->common.alt_fb_idx];
    500 
    501         vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[ALTREF_FRAME], &near_mv[ALTREF_FRAME],
    502                           &best_ref_mv[ALTREF_FRAME], MDCounts[ALTREF_FRAME], ALTREF_FRAME, cpi->common.ref_frame_sign_bias);
    503 
    504         y_buffer[ALTREF_FRAME] = alt_yv12->y_buffer + recon_yoffset;
    505         u_buffer[ALTREF_FRAME] = alt_yv12->u_buffer + recon_uvoffset;
    506         v_buffer[ALTREF_FRAME] = alt_yv12->v_buffer + recon_uvoffset;
    507     }
    508     else
    509         skip_mode[ALTREF_FRAME] = 1;
    510 
    511     cpi->mbs_tested_so_far++;          // Count of the number of MBs tested so far this frame
    512 
    513     *returnintra = best_intra_rd;
    514     x->skip = 0;
    515 
    516     ref_frame_cost[INTRA_FRAME]   = vp8_cost_zero(cpi->prob_intra_coded);
    517 
    518     // Special case treatment when GF and ARF are not sensible options for reference
    519     if (cpi->ref_frame_flags == VP8_LAST_FLAG)
    520     {
    521         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
    522                                         + vp8_cost_zero(255);
    523         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
    524                                         + vp8_cost_one(255)
    525                                         + vp8_cost_zero(128);
    526         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
    527                                         + vp8_cost_one(255)
    528                                         + vp8_cost_one(128);
    529     }
    530     else
    531     {
    532         ref_frame_cost[LAST_FRAME]    = vp8_cost_one(cpi->prob_intra_coded)
    533                                         + vp8_cost_zero(cpi->prob_last_coded);
    534         ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
    535                                         + vp8_cost_one(cpi->prob_last_coded)
    536                                         + vp8_cost_zero(cpi->prob_gf_coded);
    537         ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(cpi->prob_intra_coded)
    538                                         + vp8_cost_one(cpi->prob_last_coded)
    539                                         + vp8_cost_one(cpi->prob_gf_coded);
    540     }
    541 
    542 
    543 
    544     best_rd = INT_MAX;
    545 
    546     x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
    547 
    548     // if we encode a new mv this is important
    549     // find the best new motion vector
    550     for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
    551     {
    552         int frame_cost;
    553         int this_rd = INT_MAX;
    554 
    555         if (best_rd <= cpi->rd_threshes[mode_index])
    556             continue;
    557 
    558         x->e_mbd.mode_info_context->mbmi.ref_frame = vp8_ref_frame_order[mode_index];
    559 
    560         if (skip_mode[x->e_mbd.mode_info_context->mbmi.ref_frame])
    561             continue;
    562 
    563         // Check to see if the testing frequency for this mode is at its max
    564         // If so then prevent it from being tested and increase the threshold for its testing
    565         if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
    566         {
    567             //if ( (cpi->mbs_tested_so_far / cpi->mode_test_hit_counts[mode_index]) <= cpi->mode_check_freq[mode_index] )
    568             if (cpi->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index]))
    569             {
    570                 // Increase the threshold for coding this mode to make it less likely to be chosen
    571                 cpi->rd_thresh_mult[mode_index] += 4;
    572 
    573                 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
    574                     cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
    575 
    576                 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
    577 
    578                 continue;
    579             }
    580         }
    581 
    582         // 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
    583         cpi->mode_test_hit_counts[mode_index] ++;
    584 
    585         rate2 = 0;
    586         distortion2 = 0;
    587 
    588         this_mode = vp8_mode_order[mode_index];
    589 
    590         // Experimental debug code.
    591         //all_rds[mode_index] = -1;
    592 
    593         x->e_mbd.mode_info_context->mbmi.mode = this_mode;
    594         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
    595 
    596         // Work out the cost assosciated with selecting the reference frame
    597         frame_cost = ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
    598         rate2 += frame_cost;
    599 
    600         // everything but intra
    601         if (x->e_mbd.mode_info_context->mbmi.ref_frame)
    602         {
    603             x->e_mbd.pre.y_buffer = y_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
    604             x->e_mbd.pre.u_buffer = u_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
    605             x->e_mbd.pre.v_buffer = v_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
    606             mode_mv[NEARESTMV] = nearest_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
    607             mode_mv[NEARMV] = near_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
    608             best_ref_mv1 = best_ref_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
    609             memcpy(mdcounts, MDCounts[x->e_mbd.mode_info_context->mbmi.ref_frame], sizeof(mdcounts));
    610         }
    611 
    612         //Only consider ZEROMV/ALTREF_FRAME for alt ref frame.
    613         if (cpi->is_src_frame_alt_ref)
    614         {
    615             if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
    616                 continue;
    617         }
    618 
    619         switch (this_mode)
    620         {
    621         case B_PRED:
    622             distortion2 = *returndistortion;                    // Best so far passed in as breakout value to vp8_pick_intra4x4mby_modes
    623             vp8_pick_intra4x4mby_modes(IF_RTCD(&cpi->rtcd), x, &rate, &distortion2);
    624             rate2 += rate;
    625             distortion2 = VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16prederror)(x->src.y_buffer, x->src.y_stride, x->e_mbd.predictor, 16, 0x7fffffff);
    626 
    627             if (distortion2 == INT_MAX)
    628             {
    629                 this_rd = INT_MAX;
    630             }
    631             else
    632             {
    633                 this_rd = RD_ESTIMATE(x->rdmult, x->rddiv, rate2, distortion2);
    634 
    635                 if (this_rd < best_intra_rd)
    636                 {
    637                     best_intra_rd = this_rd;
    638                     *returnintra = best_intra_rd ;
    639                 }
    640             }
    641 
    642             break;
    643 
    644         case SPLITMV:
    645 
    646             // Split MV modes currently not supported when RD is nopt enabled.
    647             break;
    648 
    649         case DC_PRED:
    650         case V_PRED:
    651         case H_PRED:
    652         case TM_PRED:
    653             vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
    654             distortion2 = VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16prederror)(x->src.y_buffer, x->src.y_stride, x->e_mbd.predictor, 16, 0x7fffffff);
    655             rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
    656             this_rd = RD_ESTIMATE(x->rdmult, x->rddiv, rate2, distortion2);
    657 
    658             if (this_rd < best_intra_rd)
    659             {
    660                 best_intra_rd = this_rd;
    661                 *returnintra = best_intra_rd ;
    662             }
    663 
    664             break;
    665 
    666         case NEWMV:
    667         {
    668             int thissme;
    669             int step_param;
    670             int further_steps;
    671             int n = 0;
    672             int sadpb = x->sadperbit16;
    673 
    674             // Further step/diamond searches as necessary
    675             if (cpi->Speed < 8)
    676             {
    677                 step_param = cpi->sf.first_step + ((cpi->Speed > 5) ? 1 : 0);
    678                 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
    679             }
    680             else
    681             {
    682                 step_param = cpi->sf.first_step + 2;
    683                 further_steps = 0;
    684             }
    685 
    686 #if 0
    687 
    688             // Initial step Search
    689             bestsme = vp8_diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param, x->errorperbit, &num00, &cpi->fn_ptr, cpi->mb.mvsadcost, cpi->mb.mvcost);
    690             mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
    691             mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
    692 
    693             // Further step searches
    694             while (n < further_steps)
    695             {
    696                 n++;
    697 
    698                 if (num00)
    699                     num00--;
    700                 else
    701                 {
    702                     thissme = vp8_diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param + n, x->errorperbit, &num00, &cpi->fn_ptr, cpi->mb.mvsadcost, x->mvcost);
    703 
    704                     if (thissme < bestsme)
    705                     {
    706                         bestsme = thissme;
    707                         mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
    708                         mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
    709                     }
    710                     else
    711                     {
    712                         d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
    713                         d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
    714                     }
    715                 }
    716             }
    717 
    718 #else
    719 
    720             if (cpi->sf.search_method == HEX)
    721             {
    722                 bestsme = vp8_hex_search(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param, sadpb/*x->errorperbit*/, &num00, cpi->fn_ptr.vf, cpi->fn_ptr.sdf, x->mvsadcost, x->mvcost);
    723                 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
    724                 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
    725             }
    726             else
    727             {
    728                 bestsme = cpi->diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param, sadpb / 2/*x->errorperbit*/, &num00, &cpi->fn_ptr, x->mvsadcost, x->mvcost); //sadpb < 9
    729                 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
    730                 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
    731 
    732                 // Further step/diamond searches as necessary
    733                 n = 0;
    734                 //further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
    735 
    736                 n = num00;
    737                 num00 = 0;
    738 
    739                 while (n < further_steps)
    740                 {
    741                     n++;
    742 
    743                     if (num00)
    744                         num00--;
    745                     else
    746                     {
    747                         thissme = cpi->diamond_search_sad(x, b, d, &best_ref_mv1, &d->bmi.mv.as_mv, step_param + n, sadpb / 4/*x->errorperbit*/, &num00, &cpi->fn_ptr, x->mvsadcost, x->mvcost); //sadpb = 9
    748 
    749                         if (thissme < bestsme)
    750                         {
    751                             bestsme = thissme;
    752                             mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
    753                             mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
    754                         }
    755                         else
    756                         {
    757                             d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
    758                             d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
    759                         }
    760                     }
    761                 }
    762             }
    763 
    764 #endif
    765         }
    766 
    767         if (bestsme < INT_MAX)
    768             cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv.as_mv, &best_ref_mv1, x->errorperbit, cpi->fn_ptr.svf, cpi->fn_ptr.vf, cpi->mb.mvcost);
    769 
    770         mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
    771         mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
    772 
    773         // mv cost;
    774         rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv1, cpi->mb.mvcost, 128);
    775 
    776 
    777         case NEARESTMV:
    778         case NEARMV:
    779 
    780             if (mode_mv[this_mode].row == 0 && mode_mv[this_mode].col == 0)
    781                 continue;
    782 
    783         case ZEROMV:
    784 
    785             // Trap vectors that reach beyond the UMV borders
    786             // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
    787             // because of the lack of break statements in the previous two cases.
    788             if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
    789                 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
    790                 continue;
    791 
    792             rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
    793             x->e_mbd.mode_info_context->mbmi.mode = this_mode;
    794             x->e_mbd.mode_info_context->mbmi.mv.as_mv = mode_mv[this_mode];
    795             x->e_mbd.block[0].bmi.mode = this_mode;
    796             x->e_mbd.block[0].bmi.mv.as_int = x->e_mbd.mode_info_context->mbmi.mv.as_int;
    797 
    798             distortion2 = get_inter_mbpred_error(x, cpi->fn_ptr.svf, cpi->fn_ptr.vf, (unsigned int *)(&sse));
    799 
    800             this_rd = RD_ESTIMATE(x->rdmult, x->rddiv, rate2, distortion2);
    801 
    802             if (cpi->active_map_enabled && x->active_ptr[0] == 0)
    803             {
    804                 x->skip = 1;
    805             }
    806             else if (sse < x->encode_breakout)
    807             {
    808                 // Check u and v to make sure skip is ok
    809                 int sse2 = 0;
    810 
    811                 sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
    812 
    813                 if (sse2 * 2 < x->encode_breakout)
    814                     x->skip = 1;
    815                 else
    816                     x->skip = 0;
    817             }
    818 
    819             break;
    820         default:
    821             break;
    822         }
    823 
    824         // Experimental debug code.
    825         //all_rds[mode_index] = this_rd;
    826 
    827         if (this_rd < best_rd || x->skip)
    828         {
    829             // Note index of best mode
    830             best_mode_index = mode_index;
    831 
    832             *returnrate = rate2;
    833             *returndistortion = distortion2;
    834             best_rd = this_rd;
    835             vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
    836             vpx_memcpy(&best_partition, x->partition_info, sizeof(PARTITION_INFO));
    837 
    838             if (this_mode == B_PRED || this_mode == SPLITMV)
    839                 for (i = 0; i < 16; i++)
    840                 {
    841                     vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
    842                 }
    843             else
    844             {
    845                 best_bmodes[0].mv = x->e_mbd.block[0].bmi.mv;
    846             }
    847 
    848             // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
    849             cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
    850             cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
    851         }
    852 
    853         // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
    854         else
    855         {
    856             cpi->rd_thresh_mult[mode_index] += 4;
    857 
    858             if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
    859                 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
    860 
    861             cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
    862         }
    863 
    864         if (x->skip)
    865             break;
    866     }
    867 
    868     // Reduce the activation RD thresholds for the best choice mode
    869     if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
    870     {
    871         int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 3);
    872 
    873         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;
    874         cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
    875     }
    876 
    877     // Keep a record of best mode index for use in next loop
    878     cpi->last_best_mode_index = best_mode_index;
    879 
    880     if (best_mbmode.mode <= B_PRED)
    881     {
    882         x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
    883         vp8_pick_intra_mbuv_mode(x);
    884         best_mbmode.uv_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
    885     }
    886 
    887 
    888     {
    889         int this_rdbin = (*returndistortion >> 7);
    890 
    891         if (this_rdbin >= 1024)
    892         {
    893             this_rdbin = 1023;
    894         }
    895 
    896         cpi->error_bins[this_rdbin] ++;
    897     }
    898 
    899 
    900     if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
    901     {
    902         best_mbmode.mode = ZEROMV;
    903         best_mbmode.ref_frame = ALTREF_FRAME;
    904         best_mbmode.mv.as_int = 0;
    905         best_mbmode.uv_mode = 0;
    906         best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
    907         best_mbmode.partitioning = 0;
    908         best_mbmode.dc_diff = 0;
    909 
    910         vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
    911         vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
    912 
    913         for (i = 0; i < 16; i++)
    914         {
    915             vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
    916         }
    917 
    918         x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
    919 
    920         return best_rd;
    921     }
    922 
    923 
    924     // macroblock modes
    925     vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
    926     vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
    927 
    928     if (x->e_mbd.mode_info_context->mbmi.mode == B_PRED || x->e_mbd.mode_info_context->mbmi.mode == SPLITMV)
    929         for (i = 0; i < 16; i++)
    930         {
    931             vpx_memcpy(&x->e_mbd.block[i].bmi, &best_bmodes[i], sizeof(B_MODE_INFO));
    932 
    933         }
    934     else
    935     {
    936         vp8_set_mbmode_and_mvs(x, x->e_mbd.mode_info_context->mbmi.mode, &best_bmodes[0].mv.as_mv);
    937     }
    938 
    939     x->e_mbd.mode_info_context->mbmi.mv.as_mv = x->e_mbd.block[15].bmi.mv.as_mv;
    940 
    941     return best_rd;
    942 }
    943