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_config.h"
     14 #include "onyx_int.h"
     15 #include "modecosts.h"
     16 #include "encodeintra.h"
     17 #include "vp8/common/entropymode.h"
     18 #include "pickinter.h"
     19 #include "vp8/common/findnearmv.h"
     20 #include "encodemb.h"
     21 #include "vp8/common/reconinter.h"
     22 #include "vp8/common/reconintra4x4.h"
     23 #include "vp8/common/variance.h"
     24 #include "mcomp.h"
     25 #include "rdopt.h"
     26 #include "vpx_mem/vpx_mem.h"
     27 #if CONFIG_TEMPORAL_DENOISING
     28 #include "denoising.h"
     29 #endif
     30 
     31 extern int VP8_UVSSE(MACROBLOCK *x);
     32 
     33 #ifdef SPEEDSTATS
     34 extern unsigned int cnt_pm;
     35 #endif
     36 
     37 extern const int vp8_ref_frame_order[MAX_MODES];
     38 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
     39 
     40 extern int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]);
     41 
     42 
     43 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
     44                                 int_mv *bestmv, int_mv *ref_mv,
     45                                 int error_per_bit,
     46                                 const vp8_variance_fn_ptr_t *vfp,
     47                                 int *mvcost[2], int *distortion,
     48                                 unsigned int *sse)
     49 {
     50     (void) b;
     51     (void) d;
     52     (void) ref_mv;
     53     (void) error_per_bit;
     54     (void) vfp;
     55     (void) mvcost;
     56     (void) distortion;
     57     (void) sse;
     58     bestmv->as_mv.row <<= 3;
     59     bestmv->as_mv.col <<= 3;
     60     return 0;
     61 }
     62 
     63 
     64 int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
     65                                   const vp8_variance_fn_ptr_t *vfp,
     66                                   unsigned int *sse,
     67                                   int_mv this_mv)
     68 {
     69 
     70     BLOCK *b = &mb->block[0];
     71     BLOCKD *d = &mb->e_mbd.block[0];
     72     unsigned char *what = (*(b->base_src) + b->src);
     73     int what_stride = b->src_stride;
     74     int pre_stride = mb->e_mbd.pre.y_stride;
     75     unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset ;
     76     int in_what_stride = pre_stride;
     77     int xoffset = this_mv.as_mv.col & 7;
     78     int yoffset = this_mv.as_mv.row & 7;
     79 
     80     in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
     81 
     82     if (xoffset | yoffset)
     83     {
     84         return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
     85     }
     86     else
     87     {
     88         return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
     89     }
     90 
     91 }
     92 
     93 
     94 unsigned int vp8_get4x4sse_cs_c
     95 (
     96     const unsigned char *src_ptr,
     97     int  source_stride,
     98     const unsigned char *ref_ptr,
     99     int  recon_stride
    100 )
    101 {
    102     int distortion = 0;
    103     int r, c;
    104 
    105     for (r = 0; r < 4; r++)
    106     {
    107         for (c = 0; c < 4; c++)
    108         {
    109             int diff = src_ptr[c] - ref_ptr[c];
    110             distortion += diff * diff;
    111         }
    112 
    113         src_ptr += source_stride;
    114         ref_ptr += recon_stride;
    115     }
    116 
    117     return distortion;
    118 }
    119 
    120 static int get_prediction_error(BLOCK *be, BLOCKD *b)
    121 {
    122     unsigned char *sptr;
    123     unsigned char *dptr;
    124     sptr = (*(be->base_src) + be->src);
    125     dptr = b->predictor;
    126 
    127     return vp8_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
    128 
    129 }
    130 
    131 static int pick_intra4x4block(
    132     MACROBLOCK *x,
    133     int ib,
    134     B_PREDICTION_MODE *best_mode,
    135     const int *mode_costs,
    136 
    137     int *bestrate,
    138     int *bestdistortion)
    139 {
    140 
    141     BLOCKD *b = &x->e_mbd.block[ib];
    142     BLOCK *be = &x->block[ib];
    143     int dst_stride = x->e_mbd.dst.y_stride;
    144     unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
    145     B_PREDICTION_MODE mode;
    146     int best_rd = INT_MAX;
    147     int rate;
    148     int distortion;
    149 
    150     unsigned char *Above = dst - dst_stride;
    151     unsigned char *yleft = dst - 1;
    152     unsigned char top_left = Above[-1];
    153 
    154     for (mode = B_DC_PRED; mode <= B_HE_PRED; mode++)
    155     {
    156         int this_rd;
    157 
    158         rate = mode_costs[mode];
    159 
    160         vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
    161                              b->predictor, 16, top_left);
    162         distortion = get_prediction_error(be, b);
    163         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
    164 
    165         if (this_rd < best_rd)
    166         {
    167             *bestrate = rate;
    168             *bestdistortion = distortion;
    169             best_rd = this_rd;
    170             *best_mode = mode;
    171         }
    172     }
    173 
    174     b->bmi.as_mode = *best_mode;
    175     vp8_encode_intra4x4block(x, ib);
    176     return best_rd;
    177 }
    178 
    179 
    180 static int pick_intra4x4mby_modes
    181 (
    182     MACROBLOCK *mb,
    183     int *Rate,
    184     int *best_dist
    185 )
    186 {
    187     MACROBLOCKD *const xd = &mb->e_mbd;
    188     int i;
    189     int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
    190     int error;
    191     int distortion = 0;
    192     const int *bmode_costs;
    193 
    194     intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
    195 
    196     bmode_costs = mb->inter_bmode_costs;
    197 
    198     for (i = 0; i < 16; i++)
    199     {
    200         MODE_INFO *const mic = xd->mode_info_context;
    201         const int mis = xd->mode_info_stride;
    202 
    203         B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
    204         int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
    205 
    206         if (mb->e_mbd.frame_type == KEY_FRAME)
    207         {
    208             const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
    209             const B_PREDICTION_MODE L = left_block_mode(mic, i);
    210 
    211             bmode_costs  = mb->bmode_costs[A][L];
    212         }
    213 
    214 
    215         pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
    216 
    217         cost += r;
    218         distortion += d;
    219         mic->bmi[i].as_mode = best_mode;
    220 
    221         /* Break out case where we have already exceeded best so far value
    222          * that was passed in
    223          */
    224         if (distortion > *best_dist)
    225             break;
    226     }
    227 
    228     *Rate = cost;
    229 
    230     if (i == 16)
    231     {
    232         *best_dist = distortion;
    233         error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
    234     }
    235     else
    236     {
    237         *best_dist = INT_MAX;
    238         error = INT_MAX;
    239     }
    240 
    241     return error;
    242 }
    243 
    244 static void pick_intra_mbuv_mode(MACROBLOCK *mb)
    245 {
    246 
    247     MACROBLOCKD *x = &mb->e_mbd;
    248     unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
    249     unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
    250     unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
    251     unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
    252     int uvsrc_stride = mb->block[16].src_stride;
    253     unsigned char uleft_col[8];
    254     unsigned char vleft_col[8];
    255     unsigned char utop_left = uabove_row[-1];
    256     unsigned char vtop_left = vabove_row[-1];
    257     int i, j;
    258     int expected_udc;
    259     int expected_vdc;
    260     int shift;
    261     int Uaverage = 0;
    262     int Vaverage = 0;
    263     int diff;
    264     int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
    265     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
    266 
    267 
    268     for (i = 0; i < 8; i++)
    269     {
    270         uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
    271         vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
    272     }
    273 
    274     if (!x->up_available && !x->left_available)
    275     {
    276         expected_udc = 128;
    277         expected_vdc = 128;
    278     }
    279     else
    280     {
    281         shift = 2;
    282 
    283         if (x->up_available)
    284         {
    285 
    286             for (i = 0; i < 8; i++)
    287             {
    288                 Uaverage += uabove_row[i];
    289                 Vaverage += vabove_row[i];
    290             }
    291 
    292             shift ++;
    293 
    294         }
    295 
    296         if (x->left_available)
    297         {
    298             for (i = 0; i < 8; i++)
    299             {
    300                 Uaverage += uleft_col[i];
    301                 Vaverage += vleft_col[i];
    302             }
    303 
    304             shift ++;
    305 
    306         }
    307 
    308         expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
    309         expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
    310     }
    311 
    312 
    313     for (i = 0; i < 8; i++)
    314     {
    315         for (j = 0; j < 8; j++)
    316         {
    317 
    318             int predu = uleft_col[i] + uabove_row[j] - utop_left;
    319             int predv = vleft_col[i] + vabove_row[j] - vtop_left;
    320             int u_p, v_p;
    321 
    322             u_p = usrc_ptr[j];
    323             v_p = vsrc_ptr[j];
    324 
    325             if (predu < 0)
    326                 predu = 0;
    327 
    328             if (predu > 255)
    329                 predu = 255;
    330 
    331             if (predv < 0)
    332                 predv = 0;
    333 
    334             if (predv > 255)
    335                 predv = 255;
    336 
    337 
    338             diff = u_p - expected_udc;
    339             pred_error[DC_PRED] += diff * diff;
    340             diff = v_p - expected_vdc;
    341             pred_error[DC_PRED] += diff * diff;
    342 
    343 
    344             diff = u_p - uabove_row[j];
    345             pred_error[V_PRED] += diff * diff;
    346             diff = v_p - vabove_row[j];
    347             pred_error[V_PRED] += diff * diff;
    348 
    349 
    350             diff = u_p - uleft_col[i];
    351             pred_error[H_PRED] += diff * diff;
    352             diff = v_p - vleft_col[i];
    353             pred_error[H_PRED] += diff * diff;
    354 
    355 
    356             diff = u_p - predu;
    357             pred_error[TM_PRED] += diff * diff;
    358             diff = v_p - predv;
    359             pred_error[TM_PRED] += diff * diff;
    360 
    361 
    362         }
    363 
    364         usrc_ptr += uvsrc_stride;
    365         vsrc_ptr += uvsrc_stride;
    366 
    367         if (i == 3)
    368         {
    369             usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
    370             vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
    371         }
    372 
    373 
    374 
    375     }
    376 
    377 
    378     for (i = DC_PRED; i <= TM_PRED; i++)
    379     {
    380         if (best_error > pred_error[i])
    381         {
    382             best_error = pred_error[i];
    383             best_mode = (MB_PREDICTION_MODE)i;
    384         }
    385     }
    386 
    387 
    388     mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
    389 
    390 }
    391 
    392 static void update_mvcount(VP8_COMP *cpi, MACROBLOCK *x, int_mv *best_ref_mv)
    393 {
    394     MACROBLOCKD *xd = &x->e_mbd;
    395     /* Split MV modes currently not supported when RD is nopt enabled,
    396      * therefore, only need to modify MVcount in NEWMV mode. */
    397     if (xd->mode_info_context->mbmi.mode == NEWMV)
    398     {
    399         x->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
    400                                       best_ref_mv->as_mv.row) >> 1)]++;
    401         x->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
    402                                       best_ref_mv->as_mv.col) >> 1)]++;
    403     }
    404 }
    405 
    406 
    407 #if CONFIG_MULTI_RES_ENCODING
    408 static
    409 void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd, int *dissim,
    410                                int *parent_ref_frame,
    411                                MB_PREDICTION_MODE *parent_mode,
    412                                int_mv *parent_ref_mv, int mb_row, int mb_col)
    413 {
    414     LOWER_RES_MB_INFO* store_mode_info
    415                           = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
    416     unsigned int parent_mb_index;
    417 
    418     /* Consider different down_sampling_factor.  */
    419     {
    420         /* TODO: Removed the loop that supports special down_sampling_factor
    421          * such as 2, 4, 8. Will revisit it if needed.
    422          * Should also try using a look-up table to see if it helps
    423          * performance. */
    424         int parent_mb_row, parent_mb_col;
    425 
    426         parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
    427                     /cpi->oxcf.mr_down_sampling_factor.num;
    428         parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
    429                     /cpi->oxcf.mr_down_sampling_factor.num;
    430         parent_mb_index = parent_mb_row*cpi->mr_low_res_mb_cols + parent_mb_col;
    431     }
    432 
    433     /* Read lower-resolution mode & motion result from memory.*/
    434     *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
    435     *parent_mode =  store_mode_info[parent_mb_index].mode;
    436     *dissim = store_mode_info[parent_mb_index].dissim;
    437 
    438     /* For highest-resolution encoder, adjust dissim value. Lower its quality
    439      * for good performance. */
    440     if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
    441         *dissim>>=1;
    442 
    443     if(*parent_ref_frame != INTRA_FRAME)
    444     {
    445         /* Consider different down_sampling_factor.
    446          * The result can be rounded to be more precise, but it takes more time.
    447          */
    448         (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row
    449                                   *cpi->oxcf.mr_down_sampling_factor.num
    450                                   /cpi->oxcf.mr_down_sampling_factor.den;
    451         (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col
    452                                   *cpi->oxcf.mr_down_sampling_factor.num
    453                                   /cpi->oxcf.mr_down_sampling_factor.den;
    454 
    455         vp8_clamp_mv2(parent_ref_mv, xd);
    456     }
    457 }
    458 #endif
    459 
    460 static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
    461 {
    462     MACROBLOCKD *xd = &x->e_mbd;
    463 
    464     unsigned int threshold = (xd->block[0].dequant[1]
    465         * xd->block[0].dequant[1] >>4);
    466 
    467     if(threshold < x->encode_breakout)
    468         threshold = x->encode_breakout;
    469 
    470     if (sse < threshold )
    471     {
    472         /* Check u and v to make sure skip is ok */
    473         unsigned int sse2 = 0;
    474 
    475         sse2 = VP8_UVSSE(x);
    476 
    477         if (sse2 * 2 < x->encode_breakout)
    478             x->skip = 1;
    479         else
    480             x->skip = 0;
    481     }
    482 }
    483 
    484 static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2,
    485                                VP8_COMP *cpi, MACROBLOCK *x, int rd_adj)
    486 {
    487     MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
    488     int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
    489     int this_rd;
    490     /* Exit early and don't compute the distortion if this macroblock
    491      * is marked inactive. */
    492     if (cpi->active_map_enabled && x->active_ptr[0] == 0)
    493     {
    494         *sse = 0;
    495         *distortion2 = 0;
    496         x->skip = 1;
    497         return INT_MAX;
    498     }
    499 
    500     if((this_mode != NEWMV) ||
    501         !(cpi->sf.half_pixel_search) || cpi->common.full_pixel==1)
    502         *distortion2 = vp8_get_inter_mbpred_error(x,
    503                                               &cpi->fn_ptr[BLOCK_16X16],
    504                                               sse, mv);
    505 
    506     this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);
    507 
    508     /* Adjust rd to bias to ZEROMV */
    509     if(this_mode == ZEROMV)
    510     {
    511         /* Bias to ZEROMV on LAST_FRAME reference when it is available. */
    512         if ((cpi->ref_frame_flags & VP8_LAST_FRAME &
    513             cpi->common.refresh_last_frame)
    514             && x->e_mbd.mode_info_context->mbmi.ref_frame != LAST_FRAME)
    515             rd_adj = 100;
    516 
    517         // rd_adj <= 100
    518         this_rd = ((int64_t)this_rd) * rd_adj / 100;
    519     }
    520 
    521     check_for_encode_breakout(*sse, x);
    522     return this_rd;
    523 }
    524 
    525 static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
    526                                     int *rd_adjustment)
    527 {
    528     MODE_INFO *mic = x->e_mbd.mode_info_context;
    529     int_mv mv_l, mv_a, mv_al;
    530     int local_motion_check = 0;
    531 
    532     if (cpi->lf_zeromv_pct > 40)
    533     {
    534         /* left mb */
    535         mic -= 1;
    536         mv_l = mic->mbmi.mv;
    537 
    538         if (mic->mbmi.ref_frame != INTRA_FRAME)
    539             if( abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
    540                 local_motion_check++;
    541 
    542         /* above-left mb */
    543         mic -= x->e_mbd.mode_info_stride;
    544         mv_al = mic->mbmi.mv;
    545 
    546         if (mic->mbmi.ref_frame != INTRA_FRAME)
    547             if( abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
    548                 local_motion_check++;
    549 
    550         /* above mb */
    551         mic += 1;
    552         mv_a = mic->mbmi.mv;
    553 
    554         if (mic->mbmi.ref_frame != INTRA_FRAME)
    555             if( abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
    556                 local_motion_check++;
    557 
    558         if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge)
    559             && local_motion_check >0) ||  local_motion_check >2 )
    560             *rd_adjustment = 80;
    561         else if (local_motion_check > 0)
    562             *rd_adjustment = 90;
    563     }
    564 }
    565 
    566 void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
    567                          int recon_uvoffset, int *returnrate,
    568                          int *returndistortion, int *returnintra, int mb_row,
    569                          int mb_col)
    570 {
    571     BLOCK *b = &x->block[0];
    572     BLOCKD *d = &x->e_mbd.block[0];
    573     MACROBLOCKD *xd = &x->e_mbd;
    574     MB_MODE_INFO best_mbmode;
    575 
    576     int_mv best_ref_mv_sb[2];
    577     int_mv mode_mv_sb[2][MB_MODE_COUNT];
    578     int_mv best_ref_mv;
    579     int_mv *mode_mv;
    580     MB_PREDICTION_MODE this_mode;
    581     int num00;
    582     int mdcounts[4];
    583     int best_rd = INT_MAX;
    584     int rd_adjustment = 100;
    585     int best_intra_rd = INT_MAX;
    586     int mode_index;
    587     int rate;
    588     int rate2;
    589     int distortion2;
    590     int bestsme = INT_MAX;
    591     int best_mode_index = 0;
    592     unsigned int sse = INT_MAX, best_rd_sse = INT_MAX;
    593 #if CONFIG_TEMPORAL_DENOISING
    594     unsigned int zero_mv_sse = INT_MAX, best_sse = INT_MAX;
    595 #endif
    596 
    597     int_mv mvp;
    598 
    599     int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    600     int saddone=0;
    601     /* search range got from mv_pred(). It uses step_param levels. (0-7) */
    602     int sr=0;
    603 
    604     unsigned char *plane[4][3];
    605     int ref_frame_map[4];
    606     int sign_bias = 0;
    607 
    608 #if CONFIG_MULTI_RES_ENCODING
    609     int dissim = INT_MAX;
    610     int parent_ref_frame = 0;
    611     int parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
    612     int_mv parent_ref_mv;
    613     MB_PREDICTION_MODE parent_mode = 0;
    614 
    615     if (parent_ref_valid)
    616     {
    617         int parent_ref_flag;
    618 
    619         get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
    620                                   &parent_mode, &parent_ref_mv, mb_row, mb_col);
    621 
    622         /* TODO(jkoleszar): The references available (ref_frame_flags) to the
    623          * lower res encoder should match those available to this encoder, but
    624          * there seems to be a situation where this mismatch can happen in the
    625          * case of frame dropping and temporal layers. For example,
    626          * GOLD being disallowed in ref_frame_flags, but being returned as
    627          * parent_ref_frame.
    628          *
    629          * In this event, take the conservative approach of disabling the
    630          * lower res info for this MB.
    631          */
    632         parent_ref_flag = 0;
    633         if (parent_ref_frame == LAST_FRAME)
    634             parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
    635         else if (parent_ref_frame == GOLDEN_FRAME)
    636             parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);
    637         else if (parent_ref_frame == ALTREF_FRAME)
    638             parent_ref_flag = (cpi->ref_frame_flags & VP8_ALTR_FRAME);
    639 
    640         //assert(!parent_ref_frame || parent_ref_flag);
    641         if (parent_ref_frame && !parent_ref_flag)
    642             parent_ref_valid = 0;
    643     }
    644 #endif
    645 
    646     mode_mv = mode_mv_sb[sign_bias];
    647     best_ref_mv.as_int = 0;
    648     vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
    649     vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
    650 
    651     /* Setup search priorities */
    652 #if CONFIG_MULTI_RES_ENCODING
    653     if (parent_ref_valid && parent_ref_frame && dissim < 8)
    654     {
    655         ref_frame_map[0] = -1;
    656         ref_frame_map[1] = parent_ref_frame;
    657         ref_frame_map[2] = -1;
    658         ref_frame_map[3] = -1;
    659     } else
    660 #endif
    661     get_reference_search_order(cpi, ref_frame_map);
    662 
    663     /* Check to see if there is at least 1 valid reference frame that we need
    664      * to calculate near_mvs.
    665      */
    666     if (ref_frame_map[1] > 0)
    667     {
    668         sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
    669                                            x->e_mbd.mode_info_context,
    670                                            mode_mv_sb,
    671                                            best_ref_mv_sb,
    672                                            mdcounts,
    673                                            ref_frame_map[1],
    674                                            cpi->common.ref_frame_sign_bias);
    675 
    676         mode_mv = mode_mv_sb[sign_bias];
    677         best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
    678     }
    679 
    680     get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
    681 
    682     /* Count of the number of MBs tested so far this frame */
    683     cpi->mbs_tested_so_far++;
    684 
    685     *returnintra = INT_MAX;
    686     x->skip = 0;
    687 
    688     x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
    689 
    690     /* If the frame has big static background and current MB is in low
    691      * motion area, its mode decision is biased to ZEROMV mode.
    692      */
    693     calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
    694 
    695     /* if we encode a new mv this is important
    696      * find the best new motion vector
    697      */
    698     for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
    699     {
    700         int frame_cost;
    701         int this_rd = INT_MAX;
    702         int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
    703 
    704         if (best_rd <= cpi->rd_threshes[mode_index])
    705             continue;
    706 
    707         if (this_ref_frame < 0)
    708             continue;
    709 
    710         x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
    711 
    712         /* everything but intra */
    713         if (x->e_mbd.mode_info_context->mbmi.ref_frame)
    714         {
    715             x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
    716             x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
    717             x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
    718 
    719             if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
    720             {
    721                 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
    722                 mode_mv = mode_mv_sb[sign_bias];
    723                 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
    724             }
    725 
    726 #if CONFIG_MULTI_RES_ENCODING
    727             if (parent_ref_valid)
    728             {
    729                 if (vp8_mode_order[mode_index] == NEARESTMV &&
    730                     mode_mv[NEARESTMV].as_int ==0)
    731                     continue;
    732                 if (vp8_mode_order[mode_index] == NEARMV &&
    733                     mode_mv[NEARMV].as_int ==0)
    734                     continue;
    735 
    736                 if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV
    737                     && best_ref_mv.as_int==0)
    738                     continue;
    739                 else if(vp8_mode_order[mode_index] == NEWMV && dissim==0
    740                     && best_ref_mv.as_int==parent_ref_mv.as_int)
    741                     continue;
    742             }
    743 #endif
    744         }
    745 
    746         /* Check to see if the testing frequency for this mode is at its max
    747          * If so then prevent it from being tested and increase the threshold
    748          * for its testing */
    749         if (cpi->mode_test_hit_counts[mode_index] &&
    750                                          (cpi->mode_check_freq[mode_index] > 1))
    751         {
    752             if (cpi->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
    753                                          cpi->mode_test_hit_counts[mode_index]))
    754             {
    755                 /* Increase the threshold for coding this mode to make it less
    756                  * likely to be chosen */
    757                 cpi->rd_thresh_mult[mode_index] += 4;
    758 
    759                 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
    760                     cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
    761 
    762                 cpi->rd_threshes[mode_index] =
    763                                  (cpi->rd_baseline_thresh[mode_index] >> 7) *
    764                                  cpi->rd_thresh_mult[mode_index];
    765                 continue;
    766             }
    767         }
    768 
    769         /* We have now reached the point where we are going to test the current
    770          * mode so increment the counter for the number of times it has been
    771          * tested */
    772         cpi->mode_test_hit_counts[mode_index] ++;
    773 
    774         rate2 = 0;
    775         distortion2 = 0;
    776 
    777         this_mode = vp8_mode_order[mode_index];
    778 
    779         x->e_mbd.mode_info_context->mbmi.mode = this_mode;
    780         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
    781 
    782         /* Work out the cost assosciated with selecting the reference frame */
    783         frame_cost =
    784             x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
    785         rate2 += frame_cost;
    786 
    787         /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
    788          * unless ARNR filtering is enabled in which case we want
    789          * an unfiltered alternative */
    790         if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
    791         {
    792             if (this_mode != ZEROMV ||
    793                 x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
    794                 continue;
    795         }
    796 
    797         switch (this_mode)
    798         {
    799         case B_PRED:
    800             /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
    801             distortion2 = best_rd_sse;
    802             pick_intra4x4mby_modes(x, &rate, &distortion2);
    803 
    804             if (distortion2 == INT_MAX)
    805             {
    806                 this_rd = INT_MAX;
    807             }
    808             else
    809             {
    810                 rate2 += rate;
    811                 distortion2 = vp8_variance16x16(
    812                                     *(b->base_src), b->src_stride,
    813                                     x->e_mbd.predictor, 16, &sse);
    814                 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
    815 
    816                 if (this_rd < best_intra_rd)
    817                 {
    818                     best_intra_rd = this_rd;
    819                     *returnintra = distortion2;
    820                 }
    821             }
    822 
    823             break;
    824 
    825         case SPLITMV:
    826 
    827             /* Split MV modes currently not supported when RD is not enabled. */
    828             break;
    829 
    830         case DC_PRED:
    831         case V_PRED:
    832         case H_PRED:
    833         case TM_PRED:
    834             vp8_build_intra_predictors_mby_s(xd,
    835                                              xd->dst.y_buffer - xd->dst.y_stride,
    836                                              xd->dst.y_buffer - 1,
    837                                              xd->dst.y_stride,
    838                                              xd->predictor,
    839                                              16);
    840             distortion2 = vp8_variance16x16
    841                                           (*(b->base_src), b->src_stride,
    842                                           x->e_mbd.predictor, 16, &sse);
    843             rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
    844             this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
    845 
    846             if (this_rd < best_intra_rd)
    847             {
    848                 best_intra_rd = this_rd;
    849                 *returnintra = distortion2;
    850             }
    851             break;
    852 
    853         case NEWMV:
    854         {
    855             int thissme;
    856             int step_param;
    857             int further_steps;
    858             int n = 0;
    859             int sadpb = x->sadperbit16;
    860             int_mv mvp_full;
    861 
    862             int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
    863             int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
    864             int col_max = (best_ref_mv.as_mv.col>>3)
    865                          + MAX_FULL_PEL_VAL;
    866             int row_max = (best_ref_mv.as_mv.row>>3)
    867                          + MAX_FULL_PEL_VAL;
    868 
    869             int tmp_col_min = x->mv_col_min;
    870             int tmp_col_max = x->mv_col_max;
    871             int tmp_row_min = x->mv_row_min;
    872             int tmp_row_max = x->mv_row_max;
    873 
    874             int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
    875 
    876             /* Further step/diamond searches as necessary */
    877             step_param = cpi->sf.first_step + speed_adjust;
    878 
    879 #if CONFIG_MULTI_RES_ENCODING
    880             /* If lower-res drops this frame, then higher-res encoder does
    881                motion search without any previous knowledge. Also, since
    882                last frame motion info is not stored, then we can not
    883                use improved_mv_pred. */
    884             if (cpi->oxcf.mr_encoder_id && !parent_ref_valid)
    885                 cpi->sf.improved_mv_pred = 0;
    886 
    887             if (parent_ref_valid && parent_ref_frame)
    888             {
    889                 /* Use parent MV as predictor. Adjust search range
    890                  * accordingly.
    891                  */
    892                 mvp.as_int = parent_ref_mv.as_int;
    893                 mvp_full.as_mv.col = parent_ref_mv.as_mv.col>>3;
    894                 mvp_full.as_mv.row = parent_ref_mv.as_mv.row>>3;
    895 
    896                 if(dissim <=32) step_param += 3;
    897                 else if(dissim <=128) step_param += 2;
    898                 else step_param += 1;
    899             }else
    900 #endif
    901             {
    902                 if(cpi->sf.improved_mv_pred)
    903                 {
    904                     if(!saddone)
    905                     {
    906                         vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
    907                         saddone = 1;
    908                     }
    909 
    910                     vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context,
    911                                 &mvp,x->e_mbd.mode_info_context->mbmi.ref_frame,
    912                                 cpi->common.ref_frame_sign_bias, &sr,
    913                                 &near_sadidx[0]);
    914 
    915                     sr += speed_adjust;
    916                     /* adjust search range according to sr from mv prediction */
    917                     if(sr > step_param)
    918                         step_param = sr;
    919 
    920                     mvp_full.as_mv.col = mvp.as_mv.col>>3;
    921                     mvp_full.as_mv.row = mvp.as_mv.row>>3;
    922                 }else
    923                 {
    924                     mvp.as_int = best_ref_mv.as_int;
    925                     mvp_full.as_mv.col = best_ref_mv.as_mv.col>>3;
    926                     mvp_full.as_mv.row = best_ref_mv.as_mv.row>>3;
    927                 }
    928             }
    929 
    930 #if CONFIG_MULTI_RES_ENCODING
    931             if (parent_ref_valid && parent_ref_frame && dissim <= 2 &&
    932                 MAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
    933                     abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <= 4)
    934             {
    935                 d->bmi.mv.as_int = mvp_full.as_int;
    936                 mode_mv[NEWMV].as_int = mvp_full.as_int;
    937 
    938                 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
    939                                              x->errorperbit,
    940                                              &cpi->fn_ptr[BLOCK_16X16],
    941                                              cpi->mb.mvcost,
    942                                              &distortion2,&sse);
    943             }else
    944 #endif
    945             {
    946                 /* Get intersection of UMV window and valid MV window to
    947                  * reduce # of checks in diamond search. */
    948                 if (x->mv_col_min < col_min )
    949                     x->mv_col_min = col_min;
    950                 if (x->mv_col_max > col_max )
    951                     x->mv_col_max = col_max;
    952                 if (x->mv_row_min < row_min )
    953                     x->mv_row_min = row_min;
    954                 if (x->mv_row_max > row_max )
    955                     x->mv_row_max = row_max;
    956 
    957                 further_steps = (cpi->Speed >= 8)?
    958                            0: (cpi->sf.max_step_search_steps - 1 - step_param);
    959 
    960                 if (cpi->sf.search_method == HEX)
    961                 {
    962 #if CONFIG_MULTI_RES_ENCODING
    963                 /* TODO: In higher-res pick_inter_mode, step_param is used to
    964                  * modify hex search range. Here, set step_param to 0 not to
    965                  * change the behavior in lowest-resolution encoder.
    966                  * Will improve it later.
    967                  */
    968                  /* Set step_param to 0 to ensure large-range motion search
    969                     when encoder drops this frame at lower-resolution.
    970                   */
    971                 if (!parent_ref_valid)
    972                     step_param = 0;
    973 #endif
    974                     bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv,
    975                                           step_param, sadpb,
    976                                           &cpi->fn_ptr[BLOCK_16X16],
    977                                           x->mvsadcost, x->mvcost, &best_ref_mv);
    978                     mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
    979                 }
    980                 else
    981                 {
    982                     bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full,
    983                                           &d->bmi.mv, step_param, sadpb, &num00,
    984                                           &cpi->fn_ptr[BLOCK_16X16],
    985                                           x->mvcost, &best_ref_mv);
    986                     mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
    987 
    988                     /* Further step/diamond searches as necessary */
    989                     n = num00;
    990                     num00 = 0;
    991 
    992                     while (n < further_steps)
    993                     {
    994                         n++;
    995 
    996                         if (num00)
    997                             num00--;
    998                         else
    999                         {
   1000                             thissme =
   1001                             cpi->diamond_search_sad(x, b, d, &mvp_full,
   1002                                                     &d->bmi.mv,
   1003                                                     step_param + n,
   1004                                                     sadpb, &num00,
   1005                                                     &cpi->fn_ptr[BLOCK_16X16],
   1006                                                     x->mvcost, &best_ref_mv);
   1007                             if (thissme < bestsme)
   1008                             {
   1009                                 bestsme = thissme;
   1010                                 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
   1011                             }
   1012                             else
   1013                             {
   1014                                 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
   1015                             }
   1016                         }
   1017                     }
   1018                 }
   1019 
   1020                 x->mv_col_min = tmp_col_min;
   1021                 x->mv_col_max = tmp_col_max;
   1022                 x->mv_row_min = tmp_row_min;
   1023                 x->mv_row_max = tmp_row_max;
   1024 
   1025                 if (bestsme < INT_MAX)
   1026                     cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
   1027                                              &best_ref_mv, x->errorperbit,
   1028                                              &cpi->fn_ptr[BLOCK_16X16],
   1029                                              cpi->mb.mvcost,
   1030                                              &distortion2,&sse);
   1031             }
   1032 
   1033             mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
   1034 
   1035             /* mv cost; */
   1036             rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
   1037                                      cpi->mb.mvcost, 128);
   1038         }
   1039 
   1040         case NEARESTMV:
   1041         case NEARMV:
   1042 
   1043             if (mode_mv[this_mode].as_int == 0)
   1044                 continue;
   1045 
   1046         case ZEROMV:
   1047 
   1048             /* Trap vectors that reach beyond the UMV borders
   1049              * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
   1050              * through to this point because of the lack of break statements
   1051              * in the previous two cases.
   1052              */
   1053             if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
   1054                 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
   1055                 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
   1056                 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
   1057                 continue;
   1058 
   1059             rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
   1060             x->e_mbd.mode_info_context->mbmi.mv.as_int =
   1061                                                     mode_mv[this_mode].as_int;
   1062             this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
   1063                                           rd_adjustment);
   1064 
   1065             break;
   1066         default:
   1067             break;
   1068         }
   1069 
   1070 #if CONFIG_TEMPORAL_DENOISING
   1071         if (cpi->oxcf.noise_sensitivity)
   1072         {
   1073 
   1074             /* Store for later use by denoiser. */
   1075             if (this_mode == ZEROMV && sse < zero_mv_sse )
   1076             {
   1077                 zero_mv_sse = sse;
   1078                 x->best_zeromv_reference_frame =
   1079                         x->e_mbd.mode_info_context->mbmi.ref_frame;
   1080             }
   1081 
   1082             /* Store the best NEWMV in x for later use in the denoiser. */
   1083             if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
   1084                     sse < best_sse)
   1085             {
   1086                 best_sse = sse;
   1087                 x->best_sse_inter_mode = NEWMV;
   1088                 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
   1089                 x->need_to_clamp_best_mvs =
   1090                     x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
   1091                 x->best_reference_frame =
   1092                     x->e_mbd.mode_info_context->mbmi.ref_frame;
   1093             }
   1094         }
   1095 #endif
   1096 
   1097         if (this_rd < best_rd || x->skip)
   1098         {
   1099             /* Note index of best mode */
   1100             best_mode_index = mode_index;
   1101 
   1102             *returnrate = rate2;
   1103             *returndistortion = distortion2;
   1104             best_rd_sse = sse;
   1105             best_rd = this_rd;
   1106             vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
   1107                        sizeof(MB_MODE_INFO));
   1108 
   1109             /* Testing this mode gave rise to an improvement in best error
   1110              * score. Lower threshold a bit for next time
   1111              */
   1112             cpi->rd_thresh_mult[mode_index] =
   1113                      (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
   1114                      cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
   1115             cpi->rd_threshes[mode_index] =
   1116                                    (cpi->rd_baseline_thresh[mode_index] >> 7) *
   1117                                    cpi->rd_thresh_mult[mode_index];
   1118         }
   1119 
   1120         /* If the mode did not help improve the best error case then raise the
   1121          * threshold for testing that mode next time around.
   1122          */
   1123         else
   1124         {
   1125             cpi->rd_thresh_mult[mode_index] += 4;
   1126 
   1127             if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
   1128                 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
   1129 
   1130             cpi->rd_threshes[mode_index] =
   1131                          (cpi->rd_baseline_thresh[mode_index] >> 7) *
   1132                          cpi->rd_thresh_mult[mode_index];
   1133         }
   1134 
   1135         if (x->skip)
   1136             break;
   1137     }
   1138 
   1139     /* Reduce the activation RD thresholds for the best choice mode */
   1140     if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
   1141     {
   1142         int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 3);
   1143 
   1144         cpi->rd_thresh_mult[best_mode_index] =
   1145                         (cpi->rd_thresh_mult[best_mode_index]
   1146                         >= (MIN_THRESHMULT + best_adjustment)) ?
   1147                         cpi->rd_thresh_mult[best_mode_index] - best_adjustment :
   1148                         MIN_THRESHMULT;
   1149         cpi->rd_threshes[best_mode_index] =
   1150                         (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
   1151                         cpi->rd_thresh_mult[best_mode_index];
   1152     }
   1153 
   1154 
   1155     {
   1156         int this_rdbin = (*returndistortion >> 7);
   1157 
   1158         if (this_rdbin >= 1024)
   1159         {
   1160             this_rdbin = 1023;
   1161         }
   1162 
   1163         cpi->error_bins[this_rdbin] ++;
   1164     }
   1165 
   1166 #if CONFIG_TEMPORAL_DENOISING
   1167     if (cpi->oxcf.noise_sensitivity)
   1168     {
   1169         if (x->best_sse_inter_mode == DC_PRED)
   1170         {
   1171             /* No best MV found. */
   1172             x->best_sse_inter_mode = best_mbmode.mode;
   1173             x->best_sse_mv = best_mbmode.mv;
   1174             x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
   1175             x->best_reference_frame = best_mbmode.ref_frame;
   1176             best_sse = best_rd_sse;
   1177         }
   1178         vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
   1179                                 recon_yoffset, recon_uvoffset);
   1180 
   1181 
   1182         /* Reevaluate ZEROMV after denoising. */
   1183         if (best_mbmode.ref_frame == INTRA_FRAME &&
   1184             x->best_zeromv_reference_frame != INTRA_FRAME)
   1185         {
   1186             int this_rd = 0;
   1187             int this_ref_frame = x->best_zeromv_reference_frame;
   1188             rate2 = x->ref_frame_cost[this_ref_frame] +
   1189                     vp8_cost_mv_ref(ZEROMV, mdcounts);
   1190             distortion2 = 0;
   1191 
   1192             /* set up the proper prediction buffers for the frame */
   1193             x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
   1194             x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
   1195             x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
   1196             x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
   1197 
   1198             x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
   1199             x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
   1200             x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
   1201             this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
   1202                                           rd_adjustment);
   1203 
   1204             if (this_rd < best_rd)
   1205             {
   1206                 vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
   1207                            sizeof(MB_MODE_INFO));
   1208             }
   1209         }
   1210 
   1211     }
   1212 #endif
   1213 
   1214     if (cpi->is_src_frame_alt_ref &&
   1215         (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
   1216     {
   1217         x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
   1218         x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
   1219         x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
   1220         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
   1221         x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
   1222                                         (cpi->common.mb_no_coeff_skip);
   1223         x->e_mbd.mode_info_context->mbmi.partitioning = 0;
   1224 
   1225         return;
   1226     }
   1227 
   1228     /* set to the best mb mode, this copy can be skip if x->skip since it
   1229      * already has the right content */
   1230     if (!x->skip)
   1231         vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode,
   1232                    sizeof(MB_MODE_INFO));
   1233 
   1234     if (best_mbmode.mode <= B_PRED)
   1235     {
   1236         /* set mode_info_context->mbmi.uv_mode */
   1237         pick_intra_mbuv_mode(x);
   1238     }
   1239 
   1240     if (sign_bias
   1241       != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
   1242         best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
   1243 
   1244     update_mvcount(cpi, x, &best_ref_mv);
   1245 }
   1246 
   1247 
   1248 void vp8_pick_intra_mode(MACROBLOCK *x, int *rate_)
   1249 {
   1250     int error4x4, error16x16 = INT_MAX;
   1251     int rate, best_rate = 0, distortion, best_sse;
   1252     MB_PREDICTION_MODE mode, best_mode = DC_PRED;
   1253     int this_rd;
   1254     unsigned int sse;
   1255     BLOCK *b = &x->block[0];
   1256     MACROBLOCKD *xd = &x->e_mbd;
   1257 
   1258     xd->mode_info_context->mbmi.ref_frame = INTRA_FRAME;
   1259 
   1260     pick_intra_mbuv_mode(x);
   1261 
   1262     for (mode = DC_PRED; mode <= TM_PRED; mode ++)
   1263     {
   1264         xd->mode_info_context->mbmi.mode = mode;
   1265         vp8_build_intra_predictors_mby_s(xd,
   1266                                          xd->dst.y_buffer - xd->dst.y_stride,
   1267                                          xd->dst.y_buffer - 1,
   1268                                          xd->dst.y_stride,
   1269                                          xd->predictor,
   1270                                          16);
   1271         distortion = vp8_variance16x16
   1272             (*(b->base_src), b->src_stride, xd->predictor, 16, &sse);
   1273         rate = x->mbmode_cost[xd->frame_type][mode];
   1274         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
   1275 
   1276         if (error16x16 > this_rd)
   1277         {
   1278             error16x16 = this_rd;
   1279             best_mode = mode;
   1280             best_sse = sse;
   1281             best_rate = rate;
   1282         }
   1283     }
   1284     xd->mode_info_context->mbmi.mode = best_mode;
   1285 
   1286     error4x4 = pick_intra4x4mby_modes(x, &rate,
   1287                                       &best_sse);
   1288     if (error4x4 < error16x16)
   1289     {
   1290         xd->mode_info_context->mbmi.mode = B_PRED;
   1291         best_rate = rate;
   1292     }
   1293 
   1294     *rate_ = best_rate;
   1295 }
   1296