LCOV - code coverage report
Current view: top level - media/libvpx/libvpx/vp8/encoder - rdopt.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 1147 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 35 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13