media/libvpx/vp8/encoder/rdopt.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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  */
    12 #include <stdio.h>
    13 #include <math.h>
    14 #include <limits.h>
    15 #include <assert.h>
    16 #include "vpx_config.h"
    17 #include "vp8_rtcd.h"
    18 #include "vp8/common/pragmas.h"
    19 #include "tokenize.h"
    20 #include "treewriter.h"
    21 #include "onyx_int.h"
    22 #include "modecosts.h"
    23 #include "encodeintra.h"
    24 #include "pickinter.h"
    25 #include "vp8/common/entropymode.h"
    26 #include "vp8/common/reconinter.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 "quantize.h"
    32 #include "vp8/common/variance.h"
    33 #include "mcomp.h"
    34 #include "rdopt.h"
    35 #include "vpx_mem/vpx_mem.h"
    36 #include "vp8/common/systemdependent.h"
    37 #if CONFIG_TEMPORAL_DENOISING
    38 #include "denoising.h"
    39 #endif
    40 extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);
    42 #define MAXF(a,b)            (((a) > (b)) ? (a) : (b))
    44 typedef struct rate_distortion_struct
    45 {
    46     int rate2;
    47     int rate_y;
    48     int rate_uv;
    49     int distortion2;
    50     int distortion_uv;
    51 } RATE_DISTORTION;
    53 typedef struct best_mode_struct
    54 {
    55   int yrd;
    56   int rd;
    57   int intra_rd;
    58   MB_MODE_INFO mbmode;
    59   union b_mode_info bmodes[16];
    60   PARTITION_INFO partition;
    61 } BEST_MODE;
    63 static const int auto_speed_thresh[17] =
    64 {
    65     1000,
    66     200,
    67     150,
    68     130,
    69     150,
    70     125,
    71     120,
    72     115,
    73     115,
    74     115,
    75     115,
    76     115,
    77     115,
    78     115,
    79     115,
    80     115,
    81     105
    82 };
    84 const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
    85 {
    86     ZEROMV,
    87     DC_PRED,
    89     NEARESTMV,
    90     NEARMV,
    92     ZEROMV,
    93     NEARESTMV,
    95     ZEROMV,
    96     NEARESTMV,
    98     NEARMV,
    99     NEARMV,
   101     V_PRED,
   102     H_PRED,
   103     TM_PRED,
   105     NEWMV,
   106     NEWMV,
   107     NEWMV,
   109     SPLITMV,
   110     SPLITMV,
   111     SPLITMV,
   113     B_PRED,
   114 };
   116 /* This table determines the search order in reference frame priority order,
   117  * which may not necessarily match INTRA,LAST,GOLDEN,ARF
   118  */
   119 const int vp8_ref_frame_order[MAX_MODES] =
   120 {
   121     1,
   122     0,
   124     1,
   125     1,
   127     2,
   128     2,
   130     3,
   131     3,
   133     2,
   134     3,
   136     0,
   137     0,
   138     0,
   140     1,
   141     2,
   142     3,
   144     1,
   145     2,
   146     3,
   148     0,
   149 };
   151 static void fill_token_costs(
   152     int c[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
   153     const vp8_prob p[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES]
   154 )
   155 {
   156     int i, j, k;
   159     for (i = 0; i < BLOCK_TYPES; i++)
   160         for (j = 0; j < COEF_BANDS; j++)
   161             for (k = 0; k < PREV_COEF_CONTEXTS; k++)
   163                 /* check for pt=0 and band > 1 if block type 0
   164                  * and 0 if blocktype 1
   165                  */
   166                 if (k == 0 && j > (i == 0))
   167                     vp8_cost_tokens2(c[i][j][k], p [i][j][k], vp8_coef_tree, 2);
   168                 else
   169                     vp8_cost_tokens(c[i][j][k], p [i][j][k], vp8_coef_tree);
   170 }
   172 static const int rd_iifactor[32] =
   173 {
   174     4, 4, 3, 2, 1, 0, 0, 0,
   175     0, 0, 0, 0, 0, 0, 0, 0,
   176     0, 0, 0, 0, 0, 0, 0, 0,
   177     0, 0, 0, 0, 0, 0, 0, 0
   178 };
   180 /* values are now correlated to quantizer */
   181 static const int sad_per_bit16lut[QINDEX_RANGE] =
   182 {
   183     2,  2,  2,  2,  2,  2,  2,  2,
   184     2,  2,  2,  2,  2,  2,  2,  2,
   185     3,  3,  3,  3,  3,  3,  3,  3,
   186     3,  3,  3,  3,  3,  3,  4,  4,
   187     4,  4,  4,  4,  4,  4,  4,  4,
   188     4,  4,  5,  5,  5,  5,  5,  5,
   189     5,  5,  5,  5,  5,  5,  6,  6,
   190     6,  6,  6,  6,  6,  6,  6,  6,
   191     6,  6,  7,  7,  7,  7,  7,  7,
   192     7,  7,  7,  7,  7,  7,  8,  8,
   193     8,  8,  8,  8,  8,  8,  8,  8,
   194     8,  8,  9,  9,  9,  9,  9,  9,
   195     9,  9,  9,  9,  9,  9,  10, 10,
   196     10, 10, 10, 10, 10, 10, 11, 11,
   197     11, 11, 11, 11, 12, 12, 12, 12,
   198     12, 12, 13, 13, 13, 13, 14, 14
   199 };
   200 static const int sad_per_bit4lut[QINDEX_RANGE] =
   201 {
   202     2,  2,  2,  2,  2,  2,  3,  3,
   203     3,  3,  3,  3,  3,  3,  3,  3,
   204     3,  3,  3,  3,  4,  4,  4,  4,
   205     4,  4,  4,  4,  4,  4,  5,  5,
   206     5,  5,  5,  5,  6,  6,  6,  6,
   207     6,  6,  6,  6,  6,  6,  6,  6,
   208     7,  7,  7,  7,  7,  7,  7,  7,
   209     7,  7,  7,  7,  7,  8,  8,  8,
   210     8,  8,  9,  9,  9,  9,  9,  9,
   211     10, 10, 10, 10, 10, 10, 10, 10,
   212     11, 11, 11, 11, 11, 11, 11, 11,
   213     12, 12, 12, 12, 12, 12, 12, 12,
   214     13, 13, 13, 13, 13, 13, 13, 14,
   215     14, 14, 14, 14, 15, 15, 15, 15,
   216     16, 16, 16, 16, 17, 17, 17, 18,
   217     18, 18, 19, 19, 19, 20, 20, 20,
   218 };
   220 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
   221 {
   222     cpi->mb.sadperbit16 =  sad_per_bit16lut[QIndex];
   223     cpi->mb.sadperbit4  =  sad_per_bit4lut[QIndex];
   224 }
   226 void vp8_initialize_rd_consts(VP8_COMP *cpi, MACROBLOCK *x, int Qvalue)
   227 {
   228     int q;
   229     int i;
   230     double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
   231     double rdconst = 2.80;
   233     vp8_clear_system_state();
   235     /* Further tests required to see if optimum is different
   236      * for key frames, golden frames and arf frames.
   237      */
   238     cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
   240     /* Extend rate multiplier along side quantizer zbin increases */
   241     if (cpi->mb.zbin_over_quant  > 0)
   242     {
   243         double oq_factor;
   244         double modq;
   246         /* Experimental code using the same basic equation as used for Q above
   247          * The units of cpi->mb.zbin_over_quant are 1/128 of Q bin size
   248          */
   249         oq_factor = 1.0 + ((double)0.0015625 * cpi->mb.zbin_over_quant);
   250         modq = (int)((double)capped_q * oq_factor);
   251         cpi->RDMULT = (int)(rdconst * (modq * modq));
   252     }
   254     if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
   255     {
   256         if (cpi->twopass.next_iiratio > 31)
   257             cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
   258         else
   259             cpi->RDMULT +=
   260                 (cpi->RDMULT * rd_iifactor[cpi->twopass.next_iiratio]) >> 4;
   261     }
   263     cpi->mb.errorperbit = (cpi->RDMULT / 110);
   264     cpi->mb.errorperbit += (cpi->mb.errorperbit==0);
   266     vp8_set_speed_features(cpi);
   268     for (i = 0; i < MAX_MODES; i++)
   269     {
   270         x->mode_test_hit_counts[i] = 0;
   271     }
   273     q = (int)pow(Qvalue, 1.25);
   275     if (q < 8)
   276         q = 8;
   278     if (cpi->RDMULT > 1000)
   279     {
   280         cpi->RDDIV = 1;
   281         cpi->RDMULT /= 100;
   283         for (i = 0; i < MAX_MODES; i++)
   284         {
   285             if (cpi->sf.thresh_mult[i] < INT_MAX)
   286             {
   287                 x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
   288             }
   289             else
   290             {
   291                 x->rd_threshes[i] = INT_MAX;
   292             }
   294             cpi->rd_baseline_thresh[i] = x->rd_threshes[i];
   295         }
   296     }
   297     else
   298     {
   299         cpi->RDDIV = 100;
   301         for (i = 0; i < MAX_MODES; i++)
   302         {
   303             if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
   304             {
   305                 x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
   306             }
   307             else
   308             {
   309                 x->rd_threshes[i] = INT_MAX;
   310             }
   312             cpi->rd_baseline_thresh[i] = x->rd_threshes[i];
   313         }
   314     }
   316     {
   317       /* build token cost array for the type of frame we have now */
   318       FRAME_CONTEXT *l = &cpi->lfc_n;
   320       if(cpi->common.refresh_alt_ref_frame)
   321           l = &cpi->lfc_a;
   322       else if(cpi->common.refresh_golden_frame)
   323           l = &cpi->lfc_g;
   325       fill_token_costs(
   326           cpi->mb.token_costs,
   327           (const vp8_prob( *)[8][3][11]) l->coef_probs
   328       );
   329       /*
   330       fill_token_costs(
   331           cpi->mb.token_costs,
   332           (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs);
   333       */
   336       /* TODO make these mode costs depend on last,alt or gold too.  (jbb) */
   337       vp8_init_mode_costs(cpi);
   338     }
   340 }
   342 void vp8_auto_select_speed(VP8_COMP *cpi)
   343 {
   344     int milliseconds_for_compress = (int)(1000000 / cpi->framerate);
   346     milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
   348 #if 0
   350     if (0)
   351     {
   352         FILE *f;
   354         f = fopen("speed.stt", "a");
   355         fprintf(f, " %8ld %10ld %10ld %10ld\n",
   356                 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
   357         fclose(f);
   358     }
   360 #endif
   362     if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
   363     {
   364         if (cpi->avg_pick_mode_time == 0)
   365         {
   366             cpi->Speed = 4;
   367         }
   368         else
   369         {
   370             if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
   371             {
   372                 cpi->Speed          += 2;
   373                 cpi->avg_pick_mode_time = 0;
   374                 cpi->avg_encode_time = 0;
   376                 if (cpi->Speed > 16)
   377                 {
   378                     cpi->Speed = 16;
   379                 }
   380             }
   382             if (milliseconds_for_compress * 100 > cpi->avg_encode_time * auto_speed_thresh[cpi->Speed])
   383             {
   384                 cpi->Speed          -= 1;
   385                 cpi->avg_pick_mode_time = 0;
   386                 cpi->avg_encode_time = 0;
   388                 /* In real-time mode, cpi->speed is in [4, 16]. */
   389                 if (cpi->Speed < 4)
   390                 {
   391                     cpi->Speed = 4;
   392                 }
   393             }
   394         }
   395     }
   396     else
   397     {
   398         cpi->Speed += 4;
   400         if (cpi->Speed > 16)
   401             cpi->Speed = 16;
   404         cpi->avg_pick_mode_time = 0;
   405         cpi->avg_encode_time = 0;
   406     }
   407 }
   409 int vp8_block_error_c(short *coeff, short *dqcoeff)
   410 {
   411     int i;
   412     int error = 0;
   414     for (i = 0; i < 16; i++)
   415     {
   416         int this_diff = coeff[i] - dqcoeff[i];
   417         error += this_diff * this_diff;
   418     }
   420     return error;
   421 }
   423 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
   424 {
   425     BLOCK  *be;
   426     BLOCKD *bd;
   427     int i, j;
   428     int berror, error = 0;
   430     for (i = 0; i < 16; i++)
   431     {
   432         be = &mb->block[i];
   433         bd = &mb->e_mbd.block[i];
   435         berror = 0;
   437         for (j = dc; j < 16; j++)
   438         {
   439             int this_diff = be->coeff[j] - bd->dqcoeff[j];
   440             berror += this_diff * this_diff;
   441         }
   443         error += berror;
   444     }
   446     return error;
   447 }
   449 int vp8_mbuverror_c(MACROBLOCK *mb)
   450 {
   452     BLOCK  *be;
   453     BLOCKD *bd;
   456     int i;
   457     int error = 0;
   459     for (i = 16; i < 24; i++)
   460     {
   461         be = &mb->block[i];
   462         bd = &mb->e_mbd.block[i];
   464         error += vp8_block_error_c(be->coeff, bd->dqcoeff);
   465     }
   467     return error;
   468 }
   470 int VP8_UVSSE(MACROBLOCK *x)
   471 {
   472     unsigned char *uptr, *vptr;
   473     unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
   474     unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
   475     int uv_stride = x->block[16].src_stride;
   477     unsigned int sse1 = 0;
   478     unsigned int sse2 = 0;
   479     int mv_row = x->e_mbd.mode_info_context->mbmi.mv.as_mv.row;
   480     int mv_col = x->e_mbd.mode_info_context->mbmi.mv.as_mv.col;
   481     int offset;
   482     int pre_stride = x->e_mbd.pre.uv_stride;
   484     if (mv_row < 0)
   485         mv_row -= 1;
   486     else
   487         mv_row += 1;
   489     if (mv_col < 0)
   490         mv_col -= 1;
   491     else
   492         mv_col += 1;
   494     mv_row /= 2;
   495     mv_col /= 2;
   497     offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
   498     uptr = x->e_mbd.pre.u_buffer + offset;
   499     vptr = x->e_mbd.pre.v_buffer + offset;
   501     if ((mv_row | mv_col) & 7)
   502     {
   503         vp8_sub_pixel_variance8x8(uptr, pre_stride,
   504             mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
   505         vp8_sub_pixel_variance8x8(vptr, pre_stride,
   506             mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
   507         sse2 += sse1;
   508     }
   509     else
   510     {
   511         vp8_variance8x8(uptr, pre_stride,
   512             upred_ptr, uv_stride, &sse2);
   513         vp8_variance8x8(vptr, pre_stride,
   514             vpred_ptr, uv_stride, &sse1);
   515         sse2 += sse1;
   516     }
   517     return sse2;
   519 }
   521 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
   522 {
   523     int c = !type;              /* start at coef 0, unless Y with Y2 */
   524     int eob = (int)(*b->eob);
   525     int pt ;    /* surrounding block/prev coef predictor */
   526     int cost = 0;
   527     short *qcoeff_ptr = b->qcoeff;
   529     VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
   531 # define QC( I)  ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )
   533     for (; c < eob; c++)
   534     {
   535         int v = QC(c);
   536         int t = vp8_dct_value_tokens_ptr[v].Token;
   537         cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
   538         cost += vp8_dct_value_cost_ptr[v];
   539         pt = vp8_prev_token_class[t];
   540     }
   542 # undef QC
   544     if (c < 16)
   545         cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
   547     pt = (c != !type); /* is eob first coefficient; */
   548     *a = *l = pt;
   550     return cost;
   551 }
   553 static int vp8_rdcost_mby(MACROBLOCK *mb)
   554 {
   555     int cost = 0;
   556     int b;
   557     MACROBLOCKD *x = &mb->e_mbd;
   558     ENTROPY_CONTEXT_PLANES t_above, t_left;
   559     ENTROPY_CONTEXT *ta;
   560     ENTROPY_CONTEXT *tl;
   562     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
   563     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
   565     ta = (ENTROPY_CONTEXT *)&t_above;
   566     tl = (ENTROPY_CONTEXT *)&t_left;
   568     for (b = 0; b < 16; b++)
   569         cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
   570                     ta + vp8_block2above[b], tl + vp8_block2left[b]);
   572     cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2,
   573                 ta + vp8_block2above[24], tl + vp8_block2left[24]);
   575     return cost;
   576 }
   578 static void macro_block_yrd( MACROBLOCK *mb,
   579                              int *Rate,
   580                              int *Distortion)
   581 {
   582     int b;
   583     MACROBLOCKD *const x = &mb->e_mbd;
   584     BLOCK   *const mb_y2 = mb->block + 24;
   585     BLOCKD *const x_y2  = x->block + 24;
   586     short *Y2DCPtr = mb_y2->src_diff;
   587     BLOCK *beptr;
   588     int d;
   590     vp8_subtract_mby( mb->src_diff, *(mb->block[0].base_src),
   591         mb->block[0].src_stride,  mb->e_mbd.predictor, 16);
   593     /* Fdct and building the 2nd order block */
   594     for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
   595     {
   596         mb->short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
   597         *Y2DCPtr++ = beptr->coeff[0];
   598         *Y2DCPtr++ = beptr->coeff[16];
   599     }
   601     /* 2nd order fdct */
   602     mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
   604     /* Quantization */
   605     for (b = 0; b < 16; b++)
   606     {
   607         mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
   608     }
   610     /* DC predication and Quantization of 2nd Order block */
   611     mb->quantize_b(mb_y2, x_y2);
   613     /* Distortion */
   614     d = vp8_mbblock_error(mb, 1) << 2;
   615     d += vp8_block_error(mb_y2->coeff, x_y2->dqcoeff);
   617     *Distortion = (d >> 4);
   619     /* rate */
   620     *Rate = vp8_rdcost_mby(mb);
   621 }
   623 static void copy_predictor(unsigned char *dst, const unsigned char *predictor)
   624 {
   625     const unsigned int *p = (const unsigned int *)predictor;
   626     unsigned int *d = (unsigned int *)dst;
   627     d[0] = p[0];
   628     d[4] = p[4];
   629     d[8] = p[8];
   630     d[12] = p[12];
   631 }
   632 static int rd_pick_intra4x4block(
   633     MACROBLOCK *x,
   634     BLOCK *be,
   635     BLOCKD *b,
   636     B_PREDICTION_MODE *best_mode,
   637     const int *bmode_costs,
   638     ENTROPY_CONTEXT *a,
   639     ENTROPY_CONTEXT *l,
   641     int *bestrate,
   642     int *bestratey,
   643     int *bestdistortion)
   644 {
   645     B_PREDICTION_MODE mode;
   646     int best_rd = INT_MAX;
   647     int rate = 0;
   648     int distortion;
   650     ENTROPY_CONTEXT ta = *a, tempa = *a;
   651     ENTROPY_CONTEXT tl = *l, templ = *l;
   652     /*
   653      * The predictor buffer is a 2d buffer with a stride of 16.  Create
   654      * a temp buffer that meets the stride requirements, but we are only
   655      * interested in the left 4x4 block
   656      * */
   657     DECLARE_ALIGNED_ARRAY(16, unsigned char,  best_predictor, 16*4);
   658     DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16);
   659     int dst_stride = x->e_mbd.dst.y_stride;
   660     unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
   662     unsigned char *Above = dst - dst_stride;
   663     unsigned char *yleft = dst - 1;
   664     unsigned char top_left = Above[-1];
   666     for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
   667     {
   668         int this_rd;
   669         int ratey;
   671         rate = bmode_costs[mode];
   673         vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
   674                              b->predictor, 16, top_left);
   675         vp8_subtract_b(be, b, 16);
   676         x->short_fdct4x4(be->src_diff, be->coeff, 32);
   677         x->quantize_b(be, b);
   679         tempa = ta;
   680         templ = tl;
   682         ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ);
   683         rate += ratey;
   684         distortion = vp8_block_error(be->coeff, b->dqcoeff) >> 2;
   686         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
   688         if (this_rd < best_rd)
   689         {
   690             *bestrate = rate;
   691             *bestratey = ratey;
   692             *bestdistortion = distortion;
   693             best_rd = this_rd;
   694             *best_mode = mode;
   695             *a = tempa;
   696             *l = templ;
   697             copy_predictor(best_predictor, b->predictor);
   698             vpx_memcpy(best_dqcoeff, b->dqcoeff, 32);
   699         }
   700     }
   701     b->bmi.as_mode = *best_mode;
   703     vp8_short_idct4x4llm(best_dqcoeff, best_predictor, 16, dst, dst_stride);
   705     return best_rd;
   706 }
   708 static int rd_pick_intra4x4mby_modes(MACROBLOCK *mb, int *Rate,
   709                                      int *rate_y, int *Distortion, int best_rd)
   710 {
   711     MACROBLOCKD *const xd = &mb->e_mbd;
   712     int i;
   713     int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
   714     int distortion = 0;
   715     int tot_rate_y = 0;
   716     int64_t total_rd = 0;
   717     ENTROPY_CONTEXT_PLANES t_above, t_left;
   718     ENTROPY_CONTEXT *ta;
   719     ENTROPY_CONTEXT *tl;
   720     const int *bmode_costs;
   722     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
   723     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
   725     ta = (ENTROPY_CONTEXT *)&t_above;
   726     tl = (ENTROPY_CONTEXT *)&t_left;
   728     intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
   730     bmode_costs = mb->inter_bmode_costs;
   732     for (i = 0; i < 16; i++)
   733     {
   734         MODE_INFO *const mic = xd->mode_info_context;
   735         const int mis = xd->mode_info_stride;
   736         B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
   737         int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
   739         if (mb->e_mbd.frame_type == KEY_FRAME)
   740         {
   741             const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
   742             const B_PREDICTION_MODE L = left_block_mode(mic, i);
   744             bmode_costs  = mb->bmode_costs[A][L];
   745         }
   747         total_rd += rd_pick_intra4x4block(
   748             mb, mb->block + i, xd->block + i, &best_mode, bmode_costs,
   749             ta + vp8_block2above[i],
   750             tl + vp8_block2left[i], &r, &ry, &d);
   752         cost += r;
   753         distortion += d;
   754         tot_rate_y += ry;
   756         mic->bmi[i].as_mode = best_mode;
   758         if(total_rd >= (int64_t)best_rd)
   759             break;
   760     }
   762     if(total_rd >= (int64_t)best_rd)
   763         return INT_MAX;
   765     *Rate = cost;
   766     *rate_y = tot_rate_y;
   767     *Distortion = distortion;
   769     return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
   770 }
   773 static int rd_pick_intra16x16mby_mode(MACROBLOCK *x,
   774                                       int *Rate,
   775                                       int *rate_y,
   776                                       int *Distortion)
   777 {
   778     MB_PREDICTION_MODE mode;
   779     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
   780     int rate, ratey;
   781     int distortion;
   782     int best_rd = INT_MAX;
   783     int this_rd;
   784     MACROBLOCKD *xd = &x->e_mbd;
   786     /* Y Search for 16x16 intra prediction mode */
   787     for (mode = DC_PRED; mode <= TM_PRED; mode++)
   788     {
   789         xd->mode_info_context->mbmi.mode = mode;
   791         vp8_build_intra_predictors_mby_s(xd,
   792                                          xd->dst.y_buffer - xd->dst.y_stride,
   793                                          xd->dst.y_buffer - 1,
   794                                          xd->dst.y_stride,
   795                                          xd->predictor,
   796                                          16);
   798         macro_block_yrd(x, &ratey, &distortion);
   799         rate = ratey + x->mbmode_cost[xd->frame_type]
   800                                      [xd->mode_info_context->mbmi.mode];
   802         this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
   804         if (this_rd < best_rd)
   805         {
   806             mode_selected = mode;
   807             best_rd = this_rd;
   808             *Rate = rate;
   809             *rate_y = ratey;
   810             *Distortion = distortion;
   811         }
   812     }
   814     xd->mode_info_context->mbmi.mode = mode_selected;
   815     return best_rd;
   816 }
   818 static int rd_cost_mbuv(MACROBLOCK *mb)
   819 {
   820     int b;
   821     int cost = 0;
   822     MACROBLOCKD *x = &mb->e_mbd;
   823     ENTROPY_CONTEXT_PLANES t_above, t_left;
   824     ENTROPY_CONTEXT *ta;
   825     ENTROPY_CONTEXT *tl;
   827     vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
   828     vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
   830     ta = (ENTROPY_CONTEXT *)&t_above;
   831     tl = (ENTROPY_CONTEXT *)&t_left;
   833     for (b = 16; b < 24; b++)
   834         cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_UV,
   835                     ta + vp8_block2above[b], tl + vp8_block2left[b]);
   837     return cost;
   838 }
   841 static int rd_inter16x16_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
   842                             int *distortion, int fullpixel)
   843 {
   844     vp8_build_inter16x16_predictors_mbuv(&x->e_mbd);
   845     vp8_subtract_mbuv(x->src_diff,
   846         x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
   847         &x->e_mbd.predictor[256], &x->e_mbd.predictor[320], 8);
   849     vp8_transform_mbuv(x);
   850     vp8_quantize_mbuv(x);
   852     *rate       = rd_cost_mbuv(x);
   853     *distortion = vp8_mbuverror(x) / 4;
   855     return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
   856 }
   858 static int rd_inter4x4_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
   859                           int *distortion, int fullpixel)
   860 {
   861     vp8_build_inter4x4_predictors_mbuv(&x->e_mbd);
   862     vp8_subtract_mbuv(x->src_diff,
   863         x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
   864         &x->e_mbd.predictor[256], &x->e_mbd.predictor[320], 8);
   866     vp8_transform_mbuv(x);
   867     vp8_quantize_mbuv(x);
   869     *rate       = rd_cost_mbuv(x);
   870     *distortion = vp8_mbuverror(x) / 4;
   872     return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
   873 }
   875 static void rd_pick_intra_mbuv_mode(MACROBLOCK *x, int *rate,
   876                                     int *rate_tokenonly, int *distortion)
   877 {
   878     MB_PREDICTION_MODE mode;
   879     MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
   880     int best_rd = INT_MAX;
   881     int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
   882     int rate_to;
   883     MACROBLOCKD *xd = &x->e_mbd;
   885     for (mode = DC_PRED; mode <= TM_PRED; mode++)
   886     {
   887         int this_rate;
   888         int this_distortion;
   889         int this_rd;
   891         xd->mode_info_context->mbmi.uv_mode = mode;
   893         vp8_build_intra_predictors_mbuv_s(xd,
   894                                           xd->dst.u_buffer - xd->dst.uv_stride,
   895                                           xd->dst.v_buffer - xd->dst.uv_stride,
   896                                           xd->dst.u_buffer - 1,
   897                                           xd->dst.v_buffer - 1,
   898                                           xd->dst.uv_stride,
   899                                           &xd->predictor[256], &xd->predictor[320],
   900                                           8);
   903         vp8_subtract_mbuv(x->src_diff,
   904                       x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
   905                       &xd->predictor[256], &xd->predictor[320], 8);
   906         vp8_transform_mbuv(x);
   907         vp8_quantize_mbuv(x);
   909         rate_to = rd_cost_mbuv(x);
   910         this_rate = rate_to + x->intra_uv_mode_cost[xd->frame_type][xd->mode_info_context->mbmi.uv_mode];
   912         this_distortion = vp8_mbuverror(x) / 4;
   914         this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
   916         if (this_rd < best_rd)
   917         {
   918             best_rd = this_rd;
   919             d = this_distortion;
   920             r = this_rate;
   921             *rate_tokenonly = rate_to;
   922             mode_selected = mode;
   923         }
   924     }
   926     *rate = r;
   927     *distortion = d;
   929     xd->mode_info_context->mbmi.uv_mode = mode_selected;
   930 }
   932 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
   933 {
   934     vp8_prob p [VP8_MVREFS-1];
   935     assert(NEARESTMV <= m  &&  m <= SPLITMV);
   936     vp8_mv_ref_probs(p, near_mv_ref_ct);
   937     return vp8_cost_token(vp8_mv_ref_tree, p,
   938                           vp8_mv_ref_encoding_array + (m - NEARESTMV));
   939 }
   941 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv)
   942 {
   943     x->e_mbd.mode_info_context->mbmi.mode = mb;
   944     x->e_mbd.mode_info_context->mbmi.mv.as_int = mv->as_int;
   945 }
   947 static int labels2mode(
   948     MACROBLOCK *x,
   949     int const *labelings, int which_label,
   950     B_PREDICTION_MODE this_mode,
   951     int_mv *this_mv, int_mv *best_ref_mv,
   952     int *mvcost[2]
   953 )
   954 {
   955     MACROBLOCKD *const xd = & x->e_mbd;
   956     MODE_INFO *const mic = xd->mode_info_context;
   957     const int mis = xd->mode_info_stride;
   959     int cost = 0;
   960     int thismvcost = 0;
   962     /* We have to be careful retrieving previously-encoded motion vectors.
   963        Ones from this macroblock have to be pulled from the BLOCKD array
   964        as they have not yet made it to the bmi array in our MB_MODE_INFO. */
   966     int i = 0;
   968     do
   969     {
   970         BLOCKD *const d = xd->block + i;
   971         const int row = i >> 2,  col = i & 3;
   973         B_PREDICTION_MODE m;
   975         if (labelings[i] != which_label)
   976             continue;
   978         if (col  &&  labelings[i] == labelings[i-1])
   979             m = LEFT4X4;
   980         else if (row  &&  labelings[i] == labelings[i-4])
   981             m = ABOVE4X4;
   982         else
   983         {
   984             /* the only time we should do costing for new motion vector
   985              * or mode is when we are on a new label  (jbb May 08, 2007)
   986              */
   987             switch (m = this_mode)
   988             {
   989             case NEW4X4 :
   990                 thismvcost  = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
   991                 break;
   992             case LEFT4X4:
   993                 this_mv->as_int = col ? d[-1].bmi.mv.as_int : left_block_mv(mic, i);
   994                 break;
   995             case ABOVE4X4:
   996                 this_mv->as_int = row ? d[-4].bmi.mv.as_int : above_block_mv(mic, i, mis);
   997                 break;
   998             case ZERO4X4:
   999                 this_mv->as_int = 0;
  1000                 break;
  1001             default:
  1002                 break;
  1005             if (m == ABOVE4X4)  /* replace above with left if same */
  1007                 int_mv left_mv;
  1009                 left_mv.as_int = col ? d[-1].bmi.mv.as_int :
  1010                                         left_block_mv(mic, i);
  1012                 if (left_mv.as_int == this_mv->as_int)
  1013                     m = LEFT4X4;
  1016             cost = x->inter_bmode_costs[ m];
  1019         d->bmi.mv.as_int = this_mv->as_int;
  1021         x->partition_info->bmi[i].mode = m;
  1022         x->partition_info->bmi[i].mv.as_int = this_mv->as_int;
  1025     while (++i < 16);
  1027     cost += thismvcost ;
  1028     return cost;
  1031 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels,
  1032                               int which_label, ENTROPY_CONTEXT *ta,
  1033                               ENTROPY_CONTEXT *tl)
  1035     int cost = 0;
  1036     int b;
  1037     MACROBLOCKD *x = &mb->e_mbd;
  1039     for (b = 0; b < 16; b++)
  1040         if (labels[ b] == which_label)
  1041             cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_WITH_DC,
  1042                                 ta + vp8_block2above[b],
  1043                                 tl + vp8_block2left[b]);
  1045     return cost;
  1048 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label)
  1050     int i;
  1051     unsigned int distortion = 0;
  1052     int pre_stride = x->e_mbd.pre.y_stride;
  1053     unsigned char *base_pre = x->e_mbd.pre.y_buffer;
  1056     for (i = 0; i < 16; i++)
  1058         if (labels[i] == which_label)
  1060             BLOCKD *bd = &x->e_mbd.block[i];
  1061             BLOCK *be = &x->block[i];
  1063             vp8_build_inter_predictors_b(bd, 16, base_pre, pre_stride, x->e_mbd.subpixel_predict);
  1064             vp8_subtract_b(be, bd, 16);
  1065             x->short_fdct4x4(be->src_diff, be->coeff, 32);
  1066             x->quantize_b(be, bd);
  1068             distortion += vp8_block_error(be->coeff, bd->dqcoeff);
  1072     return distortion;
  1076 static const unsigned int segmentation_to_sseshift[4] = {3, 3, 2, 0};
  1079 typedef struct
  1081   int_mv *ref_mv;
  1082   int_mv mvp;
  1084   int segment_rd;
  1085   int segment_num;
  1086   int r;
  1087   int d;
  1088   int segment_yrate;
  1089   B_PREDICTION_MODE modes[16];
  1090   int_mv mvs[16];
  1091   unsigned char eobs[16];
  1093   int mvthresh;
  1094   int *mdcounts;
  1096   int_mv sv_mvp[4]; /* save 4 mvp from 8x8 */
  1097   int sv_istep[2];  /* save 2 initial step_param for 16x8/8x16 */
  1099 } BEST_SEG_INFO;
  1102 static void rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x,
  1103                              BEST_SEG_INFO *bsi, unsigned int segmentation)
  1105     int i;
  1106     int const *labels;
  1107     int br = 0;
  1108     int bd = 0;
  1109     B_PREDICTION_MODE this_mode;
  1112     int label_count;
  1113     int this_segment_rd = 0;
  1114     int label_mv_thresh;
  1115     int rate = 0;
  1116     int sbr = 0;
  1117     int sbd = 0;
  1118     int segmentyrate = 0;
  1120     vp8_variance_fn_ptr_t *v_fn_ptr;
  1122     ENTROPY_CONTEXT_PLANES t_above, t_left;
  1123     ENTROPY_CONTEXT *ta;
  1124     ENTROPY_CONTEXT *tl;
  1125     ENTROPY_CONTEXT_PLANES t_above_b, t_left_b;
  1126     ENTROPY_CONTEXT *ta_b;
  1127     ENTROPY_CONTEXT *tl_b;
  1129     vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
  1130     vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
  1132     ta = (ENTROPY_CONTEXT *)&t_above;
  1133     tl = (ENTROPY_CONTEXT *)&t_left;
  1134     ta_b = (ENTROPY_CONTEXT *)&t_above_b;
  1135     tl_b = (ENTROPY_CONTEXT *)&t_left_b;
  1137     br = 0;
  1138     bd = 0;
  1140     v_fn_ptr = &cpi->fn_ptr[segmentation];
  1141     labels = vp8_mbsplits[segmentation];
  1142     label_count = vp8_mbsplit_count[segmentation];
  1144     /* 64 makes this threshold really big effectively making it so that we
  1145      * very rarely check mvs on segments.   setting this to 1 would make mv
  1146      * thresh roughly equal to what it is for macroblocks
  1147      */
  1148     label_mv_thresh = 1 * bsi->mvthresh / label_count ;
  1150     /* Segmentation method overheads */
  1151     rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
  1152     rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts);
  1153     this_segment_rd += RDCOST(x->rdmult, x->rddiv, rate, 0);
  1154     br += rate;
  1156     for (i = 0; i < label_count; i++)
  1158         int_mv mode_mv[B_MODE_COUNT];
  1159         int best_label_rd = INT_MAX;
  1160         B_PREDICTION_MODE mode_selected = ZERO4X4;
  1161         int bestlabelyrate = 0;
  1163         /* search for the best motion vector on this segment */
  1164         for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
  1166             int this_rd;
  1167             int distortion;
  1168             int labelyrate;
  1169             ENTROPY_CONTEXT_PLANES t_above_s, t_left_s;
  1170             ENTROPY_CONTEXT *ta_s;
  1171             ENTROPY_CONTEXT *tl_s;
  1173             vpx_memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES));
  1174             vpx_memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES));
  1176             ta_s = (ENTROPY_CONTEXT *)&t_above_s;
  1177             tl_s = (ENTROPY_CONTEXT *)&t_left_s;
  1179             if (this_mode == NEW4X4)
  1181                 int sseshift;
  1182                 int num00;
  1183                 int step_param = 0;
  1184                 int further_steps;
  1185                 int n;
  1186                 int thissme;
  1187                 int bestsme = INT_MAX;
  1188                 int_mv  temp_mv;
  1189                 BLOCK *c;
  1190                 BLOCKD *e;
  1192                 /* Is the best so far sufficiently good that we cant justify
  1193                  * doing a new motion search.
  1194                  */
  1195                 if (best_label_rd < label_mv_thresh)
  1196                     break;
  1198                 if(cpi->compressor_speed)
  1200                     if (segmentation == BLOCK_8X16 || segmentation == BLOCK_16X8)
  1202                         bsi->mvp.as_int = bsi->sv_mvp[i].as_int;
  1203                         if (i==1 && segmentation == BLOCK_16X8)
  1204                           bsi->mvp.as_int = bsi->sv_mvp[2].as_int;
  1206                         step_param = bsi->sv_istep[i];
  1209                     /* use previous block's result as next block's MV
  1210                      * predictor.
  1211                      */
  1212                     if (segmentation == BLOCK_4X4 && i>0)
  1214                         bsi->mvp.as_int = x->e_mbd.block[i-1].bmi.mv.as_int;
  1215                         if (i==4 || i==8 || i==12)
  1216                             bsi->mvp.as_int = x->e_mbd.block[i-4].bmi.mv.as_int;
  1217                         step_param = 2;
  1221                 further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
  1224                     int sadpb = x->sadperbit4;
  1225                     int_mv mvp_full;
  1227                     mvp_full.as_mv.row = bsi->mvp.as_mv.row >>3;
  1228                     mvp_full.as_mv.col = bsi->mvp.as_mv.col >>3;
  1230                     /* find first label */
  1231                     n = vp8_mbsplit_offset[segmentation][i];
  1233                     c = &x->block[n];
  1234                     e = &x->e_mbd.block[n];
  1237                         bestsme = cpi->diamond_search_sad(x, c, e, &mvp_full,
  1238                                                 &mode_mv[NEW4X4], step_param,
  1239                                                 sadpb, &num00, v_fn_ptr,
  1240                                                 x->mvcost, bsi->ref_mv);
  1242                         n = num00;
  1243                         num00 = 0;
  1245                         while (n < further_steps)
  1247                             n++;
  1249                             if (num00)
  1250                                 num00--;
  1251                             else
  1253                                 thissme = cpi->diamond_search_sad(x, c, e,
  1254                                                     &mvp_full, &temp_mv,
  1255                                                     step_param + n, sadpb,
  1256                                                     &num00, v_fn_ptr,
  1257                                                     x->mvcost, bsi->ref_mv);
  1259                                 if (thissme < bestsme)
  1261                                     bestsme = thissme;
  1262                                     mode_mv[NEW4X4].as_int = temp_mv.as_int;
  1268                     sseshift = segmentation_to_sseshift[segmentation];
  1270                     /* Should we do a full search (best quality only) */
  1271                     if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000)
  1273                         /* Check if mvp_full is within the range. */
  1274                         vp8_clamp_mv(&mvp_full, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
  1276                         thissme = cpi->full_search_sad(x, c, e, &mvp_full,
  1277                                                        sadpb, 16, v_fn_ptr,
  1278                                                        x->mvcost, bsi->ref_mv);
  1280                         if (thissme < bestsme)
  1282                             bestsme = thissme;
  1283                             mode_mv[NEW4X4].as_int = e->bmi.mv.as_int;
  1285                         else
  1287                             /* The full search result is actually worse so
  1288                              * re-instate the previous best vector
  1289                              */
  1290                             e->bmi.mv.as_int = mode_mv[NEW4X4].as_int;
  1295                 if (bestsme < INT_MAX)
  1297                     int disto;
  1298                     unsigned int sse;
  1299                     cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
  1300                         bsi->ref_mv, x->errorperbit, v_fn_ptr, x->mvcost,
  1301                         &disto, &sse);
  1303             } /* NEW4X4 */
  1305             rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode],
  1306                                bsi->ref_mv, x->mvcost);
  1308             /* Trap vectors that reach beyond the UMV borders */
  1309             if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
  1310                 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
  1312                 continue;
  1315             distortion = vp8_encode_inter_mb_segment(x, labels, i) / 4;
  1317             labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s);
  1318             rate += labelyrate;
  1320             this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
  1322             if (this_rd < best_label_rd)
  1324                 sbr = rate;
  1325                 sbd = distortion;
  1326                 bestlabelyrate = labelyrate;
  1327                 mode_selected = this_mode;
  1328                 best_label_rd = this_rd;
  1330                 vpx_memcpy(ta_b, ta_s, sizeof(ENTROPY_CONTEXT_PLANES));
  1331                 vpx_memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES));
  1334         } /*for each 4x4 mode*/
  1336         vpx_memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES));
  1337         vpx_memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES));
  1339         labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected],
  1340                     bsi->ref_mv, x->mvcost);
  1342         br += sbr;
  1343         bd += sbd;
  1344         segmentyrate += bestlabelyrate;
  1345         this_segment_rd += best_label_rd;
  1347         if (this_segment_rd >= bsi->segment_rd)
  1348             break;
  1350     } /* for each label */
  1352     if (this_segment_rd < bsi->segment_rd)
  1354         bsi->r = br;
  1355         bsi->d = bd;
  1356         bsi->segment_yrate = segmentyrate;
  1357         bsi->segment_rd = this_segment_rd;
  1358         bsi->segment_num = segmentation;
  1360         /* store everything needed to come back to this!! */
  1361         for (i = 0; i < 16; i++)
  1363             bsi->mvs[i].as_mv = x->partition_info->bmi[i].mv.as_mv;
  1364             bsi->modes[i] = x->partition_info->bmi[i].mode;
  1365             bsi->eobs[i] = x->e_mbd.eobs[i];
  1370 static
  1371 void vp8_cal_step_param(int sr, int *sp)
  1373     int step = 0;
  1375     if (sr > MAX_FIRST_STEP) sr = MAX_FIRST_STEP;
  1376     else if (sr < 1) sr = 1;
  1378     while (sr>>=1)
  1379         step++;
  1381     *sp = MAX_MVSEARCH_STEPS - 1 - step;
  1384 static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x,
  1385                                            int_mv *best_ref_mv, int best_rd,
  1386                                            int *mdcounts, int *returntotrate,
  1387                                            int *returnyrate, int *returndistortion,
  1388                                            int mvthresh)
  1390     int i;
  1391     BEST_SEG_INFO bsi;
  1393     vpx_memset(&bsi, 0, sizeof(bsi));
  1395     bsi.segment_rd = best_rd;
  1396     bsi.ref_mv = best_ref_mv;
  1397     bsi.mvp.as_int = best_ref_mv->as_int;
  1398     bsi.mvthresh = mvthresh;
  1399     bsi.mdcounts = mdcounts;
  1401     for(i = 0; i < 16; i++)
  1403         bsi.modes[i] = ZERO4X4;
  1406     if(cpi->compressor_speed == 0)
  1408         /* for now, we will keep the original segmentation order
  1409            when in best quality mode */
  1410         rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
  1411         rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
  1412         rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
  1413         rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
  1415     else
  1417         int sr;
  1419         rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
  1421         if (bsi.segment_rd < best_rd)
  1423             int col_min = ((best_ref_mv->as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
  1424             int row_min = ((best_ref_mv->as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
  1425             int col_max = (best_ref_mv->as_mv.col>>3) + MAX_FULL_PEL_VAL;
  1426             int row_max = (best_ref_mv->as_mv.row>>3) + MAX_FULL_PEL_VAL;
  1428             int tmp_col_min = x->mv_col_min;
  1429             int tmp_col_max = x->mv_col_max;
  1430             int tmp_row_min = x->mv_row_min;
  1431             int tmp_row_max = x->mv_row_max;
  1433             /* Get intersection of UMV window and valid MV window to reduce # of checks in diamond search. */
  1434             if (x->mv_col_min < col_min )
  1435                 x->mv_col_min = col_min;
  1436             if (x->mv_col_max > col_max )
  1437                 x->mv_col_max = col_max;
  1438             if (x->mv_row_min < row_min )
  1439                 x->mv_row_min = row_min;
  1440             if (x->mv_row_max > row_max )
  1441                 x->mv_row_max = row_max;
  1443             /* Get 8x8 result */
  1444             bsi.sv_mvp[0].as_int = bsi.mvs[0].as_int;
  1445             bsi.sv_mvp[1].as_int = bsi.mvs[2].as_int;
  1446             bsi.sv_mvp[2].as_int = bsi.mvs[8].as_int;
  1447             bsi.sv_mvp[3].as_int = bsi.mvs[10].as_int;
  1449             /* Use 8x8 result as 16x8/8x16's predictor MV. Adjust search range according to the closeness of 2 MV. */
  1450             /* block 8X16 */
  1452                 sr = MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[2].as_mv.row))>>3, (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[2].as_mv.col))>>3);
  1453                 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
  1455                 sr = MAXF((abs(bsi.sv_mvp[1].as_mv.row - bsi.sv_mvp[3].as_mv.row))>>3, (abs(bsi.sv_mvp[1].as_mv.col - bsi.sv_mvp[3].as_mv.col))>>3);
  1456                 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
  1458                 rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
  1461             /* block 16X8 */
  1463                 sr = MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[1].as_mv.row))>>3, (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[1].as_mv.col))>>3);
  1464                 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
  1466                 sr = MAXF((abs(bsi.sv_mvp[2].as_mv.row - bsi.sv_mvp[3].as_mv.row))>>3, (abs(bsi.sv_mvp[2].as_mv.col - bsi.sv_mvp[3].as_mv.col))>>3);
  1467                 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
  1469                 rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
  1472             /* If 8x8 is better than 16x8/8x16, then do 4x4 search */
  1473             /* Not skip 4x4 if speed=0 (good quality) */
  1474             if (cpi->sf.no_skip_block4x4_search || bsi.segment_num == BLOCK_8X8)  /* || (sv_segment_rd8x8-bsi.segment_rd) < sv_segment_rd8x8>>5) */
  1476                 bsi.mvp.as_int = bsi.sv_mvp[0].as_int;
  1477                 rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
  1480             /* restore UMV window */
  1481             x->mv_col_min = tmp_col_min;
  1482             x->mv_col_max = tmp_col_max;
  1483             x->mv_row_min = tmp_row_min;
  1484             x->mv_row_max = tmp_row_max;
  1488     /* set it to the best */
  1489     for (i = 0; i < 16; i++)
  1491         BLOCKD *bd = &x->e_mbd.block[i];
  1493         bd->bmi.mv.as_int = bsi.mvs[i].as_int;
  1494         *bd->eob = bsi.eobs[i];
  1497     *returntotrate = bsi.r;
  1498     *returndistortion = bsi.d;
  1499     *returnyrate = bsi.segment_yrate;
  1501     /* save partitions */
  1502     x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num;
  1503     x->partition_info->count = vp8_mbsplit_count[bsi.segment_num];
  1505     for (i = 0; i < x->partition_info->count; i++)
  1507         int j;
  1509         j = vp8_mbsplit_offset[bsi.segment_num][i];
  1511         x->partition_info->bmi[i].mode = bsi.modes[j];
  1512         x->partition_info->bmi[i].mv.as_mv = bsi.mvs[j].as_mv;
  1514     /*
  1515      * used to set x->e_mbd.mode_info_context->mbmi.mv.as_int
  1516      */
  1517     x->partition_info->bmi[15].mv.as_int = bsi.mvs[15].as_int;
  1519     return bsi.segment_rd;
  1522 /* The improved MV prediction */
  1523 void vp8_mv_pred
  1525     VP8_COMP *cpi,
  1526     MACROBLOCKD *xd,
  1527     const MODE_INFO *here,
  1528     int_mv *mvp,
  1529     int refframe,
  1530     int *ref_frame_sign_bias,
  1531     int *sr,
  1532     int near_sadidx[]
  1535     const MODE_INFO *above = here - xd->mode_info_stride;
  1536     const MODE_INFO *left = here - 1;
  1537     const MODE_INFO *aboveleft = above - 1;
  1538     int_mv           near_mvs[8];
  1539     int              near_ref[8];
  1540     int_mv           mv;
  1541     int              vcnt=0;
  1542     int              find=0;
  1543     int              mb_offset;
  1545     int              mvx[8];
  1546     int              mvy[8];
  1547     int              i;
  1549     mv.as_int = 0;
  1551     if(here->mbmi.ref_frame != INTRA_FRAME)
  1553         near_mvs[0].as_int = near_mvs[1].as_int = near_mvs[2].as_int = near_mvs[3].as_int = near_mvs[4].as_int = near_mvs[5].as_int = near_mvs[6].as_int = near_mvs[7].as_int = 0;
  1554         near_ref[0] = near_ref[1] = near_ref[2] = near_ref[3] = near_ref[4] = near_ref[5] = near_ref[6] = near_ref[7] = 0;
  1556         /* read in 3 nearby block's MVs from current frame as prediction
  1557          * candidates.
  1558          */
  1559         if (above->mbmi.ref_frame != INTRA_FRAME)
  1561             near_mvs[vcnt].as_int = above->mbmi.mv.as_int;
  1562             mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
  1563             near_ref[vcnt] =  above->mbmi.ref_frame;
  1565         vcnt++;
  1566         if (left->mbmi.ref_frame != INTRA_FRAME)
  1568             near_mvs[vcnt].as_int = left->mbmi.mv.as_int;
  1569             mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
  1570             near_ref[vcnt] =  left->mbmi.ref_frame;
  1572         vcnt++;
  1573         if (aboveleft->mbmi.ref_frame != INTRA_FRAME)
  1575             near_mvs[vcnt].as_int = aboveleft->mbmi.mv.as_int;
  1576             mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
  1577             near_ref[vcnt] =  aboveleft->mbmi.ref_frame;
  1579         vcnt++;
  1581         /* read in 5 nearby block's MVs from last frame. */
  1582         if(cpi->common.last_frame_type != KEY_FRAME)
  1584             mb_offset = (-xd->mb_to_top_edge/128 + 1) * (xd->mode_info_stride +1) + (-xd->mb_to_left_edge/128 +1) ;
  1586             /* current in last frame */
  1587             if (cpi->lf_ref_frame[mb_offset] != INTRA_FRAME)
  1589                 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset].as_int;
  1590                 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
  1591                 near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset];
  1593             vcnt++;
  1595             /* above in last frame */
  1596             if (cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1] != INTRA_FRAME)
  1598                 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset - xd->mode_info_stride-1].as_int;
  1599                 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset - xd->mode_info_stride-1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
  1600                 near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1];
  1602             vcnt++;
  1604             /* left in last frame */
  1605             if (cpi->lf_ref_frame[mb_offset-1] != INTRA_FRAME)
  1607                 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset -1].as_int;
  1608                 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset -1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
  1609                 near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset - 1];
  1611             vcnt++;
  1613             /* right in last frame */
  1614             if (cpi->lf_ref_frame[mb_offset +1] != INTRA_FRAME)
  1616                 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset +1].as_int;
  1617                 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
  1618                 near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset +1];
  1620             vcnt++;
  1622             /* below in last frame */
  1623             if (cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1] != INTRA_FRAME)
  1625                 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset + xd->mode_info_stride +1].as_int;
  1626                 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset + xd->mode_info_stride +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
  1627                 near_ref[vcnt] =  cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1];
  1629             vcnt++;
  1632         for(i=0; i< vcnt; i++)
  1634             if(near_ref[near_sadidx[i]] != INTRA_FRAME)
  1636                 if(here->mbmi.ref_frame == near_ref[near_sadidx[i]])
  1638                     mv.as_int = near_mvs[near_sadidx[i]].as_int;
  1639                     find = 1;
  1640                     if (i < 3)
  1641                         *sr = 3;
  1642                     else
  1643                         *sr = 2;
  1644                     break;
  1649         if(!find)
  1651             for(i=0; i<vcnt; i++)
  1653                 mvx[i] = near_mvs[i].as_mv.row;
  1654                 mvy[i] = near_mvs[i].as_mv.col;
  1657             insertsortmv(mvx, vcnt);
  1658             insertsortmv(mvy, vcnt);
  1659             mv.as_mv.row = mvx[vcnt/2];
  1660             mv.as_mv.col = mvy[vcnt/2];
  1662             find = 1;
  1663             /* sr is set to 0 to allow calling function to decide the search
  1664              * range.
  1665              */
  1666             *sr = 0;
  1670     /* Set up return values */
  1671     mvp->as_int = mv.as_int;
  1672     vp8_clamp_mv2(mvp, xd);
  1675 void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, int recon_yoffset, int near_sadidx[])
  1677     /* near_sad indexes:
  1678      *   0-cf above, 1-cf left, 2-cf aboveleft,
  1679      *   3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below
  1680      */
  1681     int near_sad[8] = {0};
  1682     BLOCK *b = &x->block[0];
  1683     unsigned char *src_y_ptr = *(b->base_src);
  1685     /* calculate sad for current frame 3 nearby MBs. */
  1686     if( xd->mb_to_top_edge==0 && xd->mb_to_left_edge ==0)
  1688         near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX;
  1689     }else if(xd->mb_to_top_edge==0)
  1690     {   /* only has left MB for sad calculation. */
  1691         near_sad[0] = near_sad[2] = INT_MAX;
  1692         near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, UINT_MAX);
  1693     }else if(xd->mb_to_left_edge ==0)
  1694     {   /* only has left MB for sad calculation. */
  1695         near_sad[1] = near_sad[2] = INT_MAX;
  1696         near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, UINT_MAX);
  1697     }else
  1699         near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, UINT_MAX);
  1700         near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, UINT_MAX);
  1701         near_sad[2] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride *16 -16,xd->dst.y_stride, UINT_MAX);
  1704     if(cpi->common.last_frame_type != KEY_FRAME)
  1706         /* calculate sad for last frame 5 nearby MBs. */
  1707         unsigned char *pre_y_buffer = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset;
  1708         int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride;
  1710         if(xd->mb_to_top_edge==0) near_sad[4] = INT_MAX;
  1711         if(xd->mb_to_left_edge ==0) near_sad[5] = INT_MAX;
  1712         if(xd->mb_to_right_edge ==0) near_sad[6] = INT_MAX;
  1713         if(xd->mb_to_bottom_edge==0) near_sad[7] = INT_MAX;
  1715         if(near_sad[4] != INT_MAX)
  1716             near_sad[4] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer - pre_y_stride *16, pre_y_stride, UINT_MAX);
  1717         if(near_sad[5] != INT_MAX)
  1718             near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer - 16, pre_y_stride, UINT_MAX);
  1719         near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer, pre_y_stride, UINT_MAX);
  1720         if(near_sad[6] != INT_MAX)
  1721             near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer + 16, pre_y_stride, UINT_MAX);
  1722         if(near_sad[7] != INT_MAX)
  1723             near_sad[7] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer + pre_y_stride *16, pre_y_stride, UINT_MAX);
  1726     if(cpi->common.last_frame_type != KEY_FRAME)
  1728         insertsortsad(near_sad, near_sadidx, 8);
  1729     }else
  1731         insertsortsad(near_sad, near_sadidx, 3);
  1735 static void rd_update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
  1737     if (x->e_mbd.mode_info_context->mbmi.mode == SPLITMV)
  1739         int i;
  1741         for (i = 0; i < x->partition_info->count; i++)
  1743             if (x->partition_info->bmi[i].mode == NEW4X4)
  1745                 x->MVcount[0][mv_max+((x->partition_info->bmi[i].mv.as_mv.row
  1746                                           - best_ref_mv->as_mv.row) >> 1)]++;
  1747                 x->MVcount[1][mv_max+((x->partition_info->bmi[i].mv.as_mv.col
  1748                                           - best_ref_mv->as_mv.col) >> 1)]++;
  1752     else if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
  1754         x->MVcount[0][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.row
  1755                                           - best_ref_mv->as_mv.row) >> 1)]++;
  1756         x->MVcount[1][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.col
  1757                                           - best_ref_mv->as_mv.col) >> 1)]++;
  1761 static int evaluate_inter_mode_rd(int mdcounts[4],
  1762                                   RATE_DISTORTION* rd,
  1763                                   int* disable_skip,
  1764                                   VP8_COMP *cpi, MACROBLOCK *x)
  1766     MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
  1767     BLOCK *b = &x->block[0];
  1768     MACROBLOCKD *xd = &x->e_mbd;
  1769     int distortion;
  1770     vp8_build_inter16x16_predictors_mby(&x->e_mbd, x->e_mbd.predictor, 16);
  1772     if (cpi->active_map_enabled && x->active_ptr[0] == 0) {
  1773         x->skip = 1;
  1775     else if (x->encode_breakout)
  1777         unsigned int sse;
  1778         unsigned int var;
  1779         unsigned int threshold = (xd->block[0].dequant[1]
  1780                     * xd->block[0].dequant[1] >>4);
  1782         if(threshold < x->encode_breakout)
  1783             threshold = x->encode_breakout;
  1785         var = vp8_variance16x16
  1786                 (*(b->base_src), b->src_stride,
  1787                 x->e_mbd.predictor, 16, &sse);
  1789         if (sse < threshold)
  1791              unsigned int q2dc = xd->block[24].dequant[0];
  1792             /* If theres is no codeable 2nd order dc
  1793                or a very small uniform pixel change change */
  1794             if ((sse - var < q2dc * q2dc >>4) ||
  1795                 (sse /2 > var && sse-var < 64))
  1797                 /* Check u and v to make sure skip is ok */
  1798                 unsigned int sse2 = VP8_UVSSE(x);
  1799                 if (sse2 * 2 < threshold)
  1801                     x->skip = 1;
  1802                     rd->distortion2 = sse + sse2;
  1803                     rd->rate2 = 500;
  1805                     /* for best_yrd calculation */
  1806                     rd->rate_uv = 0;
  1807                     rd->distortion_uv = sse2;
  1809                     *disable_skip = 1;
  1810                     return RDCOST(x->rdmult, x->rddiv, rd->rate2,
  1811                                   rd->distortion2);
  1818     /* Add in the Mv/mode cost */
  1819     rd->rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
  1821     /* Y cost and distortion */
  1822     macro_block_yrd(x, &rd->rate_y, &distortion);
  1823     rd->rate2 += rd->rate_y;
  1824     rd->distortion2 += distortion;
  1826     /* UV cost and distortion */
  1827     rd_inter16x16_uv(cpi, x, &rd->rate_uv, &rd->distortion_uv,
  1828                      cpi->common.full_pixel);
  1829     rd->rate2 += rd->rate_uv;
  1830     rd->distortion2 += rd->distortion_uv;
  1831     return INT_MAX;
  1834 static int calculate_final_rd_costs(int this_rd,
  1835                                     RATE_DISTORTION* rd,
  1836                                     int* other_cost,
  1837                                     int disable_skip,
  1838                                     int uv_intra_tteob,
  1839                                     int intra_rd_penalty,
  1840                                     VP8_COMP *cpi, MACROBLOCK *x)
  1842     MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
  1844     /* Where skip is allowable add in the default per mb cost for the no
  1845      * skip case. where we then decide to skip we have to delete this and
  1846      * replace it with the cost of signalling a skip
  1847      */
  1848     if (cpi->common.mb_no_coeff_skip)
  1850         *other_cost += vp8_cost_bit(cpi->prob_skip_false, 0);
  1851         rd->rate2 += *other_cost;
  1854     /* Estimate the reference frame signaling cost and add it
  1855      * to the rolling cost variable.
  1856      */
  1857     rd->rate2 +=
  1858         x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
  1860     if (!disable_skip)
  1862         /* Test for the condition where skip block will be activated
  1863          * because there are no non zero coefficients and make any
  1864          * necessary adjustment for rate
  1865          */
  1866         if (cpi->common.mb_no_coeff_skip)
  1868             int i;
  1869             int tteob;
  1870             int has_y2_block = (this_mode!=SPLITMV && this_mode!=B_PRED);
  1872             tteob = 0;
  1873             if(has_y2_block)
  1874                 tteob += x->e_mbd.eobs[24];
  1876             for (i = 0; i < 16; i++)
  1877                 tteob += (x->e_mbd.eobs[i] > has_y2_block);
  1879             if (x->e_mbd.mode_info_context->mbmi.ref_frame)
  1881                 for (i = 16; i < 24; i++)
  1882                     tteob += x->e_mbd.eobs[i];
  1884             else
  1885                 tteob += uv_intra_tteob;
  1887             if (tteob == 0)
  1889                 rd->rate2 -= (rd->rate_y + rd->rate_uv);
  1890                 /* for best_yrd calculation */
  1891                 rd->rate_uv = 0;
  1893                 /* Back out no skip flag costing and add in skip flag costing */
  1894                 if (cpi->prob_skip_false)
  1896                     int prob_skip_cost;
  1898                     prob_skip_cost = vp8_cost_bit(cpi->prob_skip_false, 1);
  1899                     prob_skip_cost -= vp8_cost_bit(cpi->prob_skip_false, 0);
  1900                     rd->rate2 += prob_skip_cost;
  1901                     *other_cost += prob_skip_cost;
  1905         /* Calculate the final RD estimate for this mode */
  1906         this_rd = RDCOST(x->rdmult, x->rddiv, rd->rate2, rd->distortion2);
  1907         if (this_rd < INT_MAX && x->e_mbd.mode_info_context->mbmi.ref_frame
  1908                                  == INTRA_FRAME)
  1909             this_rd += intra_rd_penalty;
  1911     return this_rd;
  1914 static void update_best_mode(BEST_MODE* best_mode, int this_rd,
  1915                              RATE_DISTORTION* rd, int other_cost, MACROBLOCK *x)
  1917     MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
  1919     other_cost +=
  1920     x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
  1922     /* Calculate the final y RD estimate for this mode */
  1923     best_mode->yrd = RDCOST(x->rdmult, x->rddiv, (rd->rate2-rd->rate_uv-other_cost),
  1924                       (rd->distortion2-rd->distortion_uv));
  1926     best_mode->rd = this_rd;
  1927     vpx_memcpy(&best_mode->mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
  1928     vpx_memcpy(&best_mode->partition, x->partition_info, sizeof(PARTITION_INFO));
  1930     if ((this_mode == B_PRED) || (this_mode == SPLITMV))
  1932         int i;
  1933         for (i = 0; i < 16; i++)
  1935             best_mode->bmodes[i] = x->e_mbd.block[i].bmi;
  1940 void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
  1941                             int recon_uvoffset, int *returnrate,
  1942                             int *returndistortion, int *returnintra)
  1944     BLOCK *b = &x->block[0];
  1945     BLOCKD *d = &x->e_mbd.block[0];
  1946     MACROBLOCKD *xd = &x->e_mbd;
  1947     int_mv best_ref_mv_sb[2];
  1948     int_mv mode_mv_sb[2][MB_MODE_COUNT];
  1949     int_mv best_ref_mv;
  1950     int_mv *mode_mv;
  1951     MB_PREDICTION_MODE this_mode;
  1952     int num00;
  1953     int best_mode_index = 0;
  1954     BEST_MODE best_mode;
  1956     int i;
  1957     int mode_index;
  1958     int mdcounts[4];
  1959     int rate;
  1960     RATE_DISTORTION rd;
  1961     int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
  1962     int uv_intra_tteob = 0;
  1963     int uv_intra_done = 0;
  1965     MB_PREDICTION_MODE uv_intra_mode = 0;
  1966     int_mv mvp;
  1967     int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  1968     int saddone=0;
  1969     /* search range got from mv_pred(). It uses step_param levels. (0-7) */
  1970     int sr=0;
  1972     unsigned char *plane[4][3];
  1973     int ref_frame_map[4];
  1974     int sign_bias = 0;
  1976     int intra_rd_penalty =  10* vp8_dc_quant(cpi->common.base_qindex,
  1977                                              cpi->common.y1dc_delta_q);
  1979 #if CONFIG_TEMPORAL_DENOISING
  1980     unsigned int zero_mv_sse = INT_MAX, best_sse = INT_MAX,
  1981             best_rd_sse = INT_MAX;
  1982 #endif
  1984     mode_mv = mode_mv_sb[sign_bias];
  1985     best_ref_mv.as_int = 0;
  1986     best_mode.rd = INT_MAX;
  1987     best_mode.yrd = INT_MAX;
  1988     best_mode.intra_rd = INT_MAX;
  1989     vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
  1990     vpx_memset(&best_mode.mbmode, 0, sizeof(best_mode.mbmode));
  1991     vpx_memset(&best_mode.bmodes, 0, sizeof(best_mode.bmodes));
  1993     /* Setup search priorities */
  1994     get_reference_search_order(cpi, ref_frame_map);
  1996     /* Check to see if there is at least 1 valid reference frame that we need
  1997      * to calculate near_mvs.
  1998      */
  1999     if (ref_frame_map[1] > 0)
  2001         sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
  2002                                            x->e_mbd.mode_info_context,
  2003                                            mode_mv_sb,
  2004                                            best_ref_mv_sb,
  2005                                            mdcounts,
  2006                                            ref_frame_map[1],
  2007                                            cpi->common.ref_frame_sign_bias);
  2009         mode_mv = mode_mv_sb[sign_bias];
  2010         best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
  2013     get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
  2015     *returnintra = INT_MAX;
  2016     /* Count of the number of MBs tested so far this frame */
  2017     x->mbs_tested_so_far++;
  2019     x->skip = 0;
  2021     for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
  2023         int this_rd = INT_MAX;
  2024         int disable_skip = 0;
  2025         int other_cost = 0;
  2026         int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
  2028         /* Test best rd so far against threshold for trying this mode. */
  2029         if (best_mode.rd <= x->rd_threshes[mode_index])
  2030             continue;
  2032         if (this_ref_frame < 0)
  2033             continue;
  2035         /* These variables hold are rolling total cost and distortion for
  2036          * this mode
  2037          */
  2038         rd.rate2 = 0;
  2039         rd.distortion2 = 0;
  2041         this_mode = vp8_mode_order[mode_index];
  2043         x->e_mbd.mode_info_context->mbmi.mode = this_mode;
  2044         x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
  2046         /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
  2047          * unless ARNR filtering is enabled in which case we want
  2048          * an unfiltered alternative
  2049          */
  2050         if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
  2052             if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
  2053                 continue;
  2056         /* everything but intra */
  2057         if (x->e_mbd.mode_info_context->mbmi.ref_frame)
  2059             x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
  2060             x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
  2061             x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
  2063             if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
  2065                 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
  2066                 mode_mv = mode_mv_sb[sign_bias];
  2067                 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
  2071         /* Check to see if the testing frequency for this mode is at its
  2072          * max If so then prevent it from being tested and increase the
  2073          * threshold for its testing
  2074          */
  2075         if (x->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
  2077             if (x->mbs_tested_so_far  <= cpi->mode_check_freq[mode_index] * x->mode_test_hit_counts[mode_index])
  2079                 /* Increase the threshold for coding this mode to make it
  2080                  * less likely to be chosen
  2081                  */
  2082                 x->rd_thresh_mult[mode_index] += 4;
  2084                 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
  2085                     x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
  2087                 x->rd_threshes[mode_index] =
  2088                     (cpi->rd_baseline_thresh[mode_index] >> 7) *
  2089                     x->rd_thresh_mult[mode_index];
  2091                 continue;
  2095         /* We have now reached the point where we are going to test the
  2096          * current mode so increment the counter for the number of times
  2097          * it has been tested
  2098          */
  2099         x->mode_test_hit_counts[mode_index] ++;
  2101         /* Experimental code. Special case for gf and arf zeromv modes.
  2102          * Increase zbin size to supress noise
  2103          */
  2104         if (x->zbin_mode_boost_enabled)
  2106             if ( this_ref_frame == INTRA_FRAME )
  2107                 x->zbin_mode_boost = 0;
  2108             else
  2110                 if (vp8_mode_order[mode_index] == ZEROMV)
  2112                     if (this_ref_frame != LAST_FRAME)
  2113                         x->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
  2114                     else
  2115                         x->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
  2117                 else if (vp8_mode_order[mode_index] == SPLITMV)
  2118                     x->zbin_mode_boost = 0;
  2119                 else
  2120                     x->zbin_mode_boost = MV_ZBIN_BOOST;
  2123             vp8_update_zbin_extra(cpi, x);
  2126         if(!uv_intra_done && this_ref_frame == INTRA_FRAME)
  2128             rd_pick_intra_mbuv_mode(x, &uv_intra_rate,
  2129                                     &uv_intra_rate_tokenonly,
  2130                                     &uv_intra_distortion);
  2131             uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
  2133             /*
  2134              * Total of the eobs is used later to further adjust rate2. Since uv
  2135              * block's intra eobs will be overwritten when we check inter modes,
  2136              * we need to save uv_intra_tteob here.
  2137              */
  2138             for (i = 16; i < 24; i++)
  2139                 uv_intra_tteob += x->e_mbd.eobs[i];
  2141             uv_intra_done = 1;
  2144         switch (this_mode)
  2146         case B_PRED:
  2148             int tmp_rd;
  2150             /* Note the rate value returned here includes the cost of
  2151              * coding the BPRED mode: x->mbmode_cost[x->e_mbd.frame_type][BPRED]
  2152              */
  2153             int distortion;
  2154             tmp_rd = rd_pick_intra4x4mby_modes(x, &rate, &rd.rate_y, &distortion, best_mode.yrd);
  2155             rd.rate2 += rate;
  2156             rd.distortion2 += distortion;
  2158             if(tmp_rd < best_mode.yrd)
  2160                 rd.rate2 += uv_intra_rate;
  2161                 rd.rate_uv = uv_intra_rate_tokenonly;
  2162                 rd.distortion2 += uv_intra_distortion;
  2163                 rd.distortion_uv = uv_intra_distortion;
  2165             else
  2167                 this_rd = INT_MAX;
  2168                 disable_skip = 1;
  2171         break;
  2173         case SPLITMV:
  2175             int tmp_rd;
  2176             int this_rd_thresh;
  2177             int distortion;
  2179             this_rd_thresh = (vp8_ref_frame_order[mode_index] == 1) ?
  2180                 x->rd_threshes[THR_NEW1] : x->rd_threshes[THR_NEW3];
  2181             this_rd_thresh = (vp8_ref_frame_order[mode_index] == 2) ?
  2182                 x->rd_threshes[THR_NEW2] : this_rd_thresh;
  2184             tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv,
  2185                                                      best_mode.yrd, mdcounts,
  2186                                                      &rate, &rd.rate_y, &distortion, this_rd_thresh) ;
  2188             rd.rate2 += rate;
  2189             rd.distortion2 += distortion;
  2191             /* If even the 'Y' rd value of split is higher than best so far
  2192              * then dont bother looking at UV
  2193              */
  2194             if (tmp_rd < best_mode.yrd)
  2196                 /* Now work out UV cost and add it in */
  2197                 rd_inter4x4_uv(cpi, x, &rd.rate_uv, &rd.distortion_uv, cpi->common.full_pixel);
  2198                 rd.rate2 += rd.rate_uv;
  2199                 rd.distortion2 += rd.distortion_uv;
  2201             else
  2203                 this_rd = INT_MAX;
  2204                 disable_skip = 1;
  2207         break;
  2208         case DC_PRED:
  2209         case V_PRED:
  2210         case H_PRED:
  2211         case TM_PRED:
  2213             int distortion;
  2214             x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
  2216             vp8_build_intra_predictors_mby_s(xd,
  2217                                              xd->dst.y_buffer - xd->dst.y_stride,
  2218                                              xd->dst.y_buffer - 1,
  2219                                              xd->dst.y_stride,
  2220                                              xd->predictor,
  2221                                              16);
  2222             macro_block_yrd(x, &rd.rate_y, &distortion) ;
  2223             rd.rate2 += rd.rate_y;
  2224             rd.distortion2 += distortion;
  2225             rd.rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
  2226             rd.rate2 += uv_intra_rate;
  2227             rd.rate_uv = uv_intra_rate_tokenonly;
  2228             rd.distortion2 += uv_intra_distortion;
  2229             rd.distortion_uv = uv_intra_distortion;
  2231         break;
  2233         case NEWMV:
  2235             int thissme;
  2236             int bestsme = INT_MAX;
  2237             int step_param = cpi->sf.first_step;
  2238             int further_steps;
  2239             int n;
  2240             int do_refine=1;   /* If last step (1-away) of n-step search doesn't pick the center point as the best match,
  2241                                   we will do a final 1-away diamond refining search  */
  2243             int sadpb = x->sadperbit16;
  2244             int_mv mvp_full;
  2246             int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
  2247             int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
  2248             int col_max = (best_ref_mv.as_mv.col>>3) + MAX_FULL_PEL_VAL;
  2249             int row_max = (best_ref_mv.as_mv.row>>3) + MAX_FULL_PEL_VAL;
  2251             int tmp_col_min = x->mv_col_min;
  2252             int tmp_col_max = x->mv_col_max;
  2253             int tmp_row_min = x->mv_row_min;
  2254             int tmp_row_max = x->mv_row_max;
  2256             if(!saddone)
  2258                 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
  2259                 saddone = 1;
  2262             vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
  2263                         x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
  2265             mvp_full.as_mv.col = mvp.as_mv.col>>3;
  2266             mvp_full.as_mv.row = mvp.as_mv.row>>3;
  2268             /* Get intersection of UMV window and valid MV window to
  2269              * reduce # of checks in diamond search.
  2270              */
  2271             if (x->mv_col_min < col_min )
  2272                 x->mv_col_min = col_min;
  2273             if (x->mv_col_max > col_max )
  2274                 x->mv_col_max = col_max;
  2275             if (x->mv_row_min < row_min )
  2276                 x->mv_row_min = row_min;
  2277             if (x->mv_row_max > row_max )
  2278                 x->mv_row_max = row_max;
  2280             /* adjust search range according to sr from mv prediction */
  2281             if(sr > step_param)
  2282                 step_param = sr;
  2284             /* Initial step/diamond search */
  2286                 bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full, &d->bmi.mv,
  2287                                         step_param, sadpb, &num00,
  2288                                         &cpi->fn_ptr[BLOCK_16X16],
  2289                                         x->mvcost, &best_ref_mv);
  2290                 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
  2292                 /* Further step/diamond searches as necessary */
  2293                 n = 0;
  2294                 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
  2296                 n = num00;
  2297                 num00 = 0;
  2299                 /* If there won't be more n-step search, check to see if refining search is needed. */
  2300                 if (n > further_steps)
  2301                     do_refine = 0;
  2303                 while (n < further_steps)
  2305                     n++;
  2307                     if (num00)
  2308                         num00--;
  2309                     else
  2311                         thissme = cpi->diamond_search_sad(x, b, d, &mvp_full,
  2312                                     &d->bmi.mv, step_param + n, sadpb, &num00,
  2313                                     &cpi->fn_ptr[BLOCK_16X16], x->mvcost,
  2314                                     &best_ref_mv);
  2316                         /* check to see if refining search is needed. */
  2317                         if (num00 > (further_steps-n))
  2318                             do_refine = 0;
  2320                         if (thissme < bestsme)
  2322                             bestsme = thissme;
  2323                             mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
  2325                         else
  2327                             d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
  2333             /* final 1-away diamond refining search */
  2334             if (do_refine == 1)
  2336                 int search_range;
  2338                 search_range = 8;
  2340                 thissme = cpi->refining_search_sad(x, b, d, &d->bmi.mv, sadpb,
  2341                                        search_range, &cpi->fn_ptr[BLOCK_16X16],
  2342                                        x->mvcost, &best_ref_mv);
  2344                 if (thissme < bestsme)
  2346                     bestsme = thissme;
  2347                     mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
  2349                 else
  2351                     d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
  2355             x->mv_col_min = tmp_col_min;
  2356             x->mv_col_max = tmp_col_max;
  2357             x->mv_row_min = tmp_row_min;
  2358             x->mv_row_max = tmp_row_max;
  2360             if (bestsme < INT_MAX)
  2362                 int dis; /* TODO: use dis in distortion calculation later. */
  2363                 unsigned int sse;
  2364                 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
  2365                                              x->errorperbit,
  2366                                              &cpi->fn_ptr[BLOCK_16X16],
  2367                                              x->mvcost, &dis, &sse);
  2370             mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
  2372             /* Add the new motion vector cost to our rolling cost variable */
  2373             rd.rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
  2376         case NEARESTMV:
  2377         case NEARMV:
  2378             /* Clip "next_nearest" so that it does not extend to far out
  2379              * of image
  2380              */
  2381             vp8_clamp_mv2(&mode_mv[this_mode], xd);
  2383             /* Do not bother proceeding if the vector (from newmv, nearest
  2384              * or near) is 0,0 as this should then be coded using the zeromv
  2385              * mode.
  2386              */
  2387             if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) && (mode_mv[this_mode].as_int == 0))
  2388                 continue;
  2390         case ZEROMV:
  2392             /* Trap vectors that reach beyond the UMV borders
  2393              * Note that ALL New MV, Nearest MV Near MV and Zero MV code
  2394              * drops through to this point because of the lack of break
  2395              * statements in the previous two cases.
  2396              */
  2397             if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
  2398                 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
  2399                 continue;
  2401             vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
  2402             this_rd = evaluate_inter_mode_rd(mdcounts, &rd,
  2403                                              &disable_skip, cpi, x);
  2404             break;
  2406         default:
  2407             break;
  2410         this_rd = calculate_final_rd_costs(this_rd, &rd, &other_cost,
  2411                                            disable_skip, uv_intra_tteob,
  2412                                            intra_rd_penalty, cpi, x);
  2414         /* Keep record of best intra distortion */
  2415         if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) &&
  2416             (this_rd < best_mode.intra_rd) )
  2418           best_mode.intra_rd = this_rd;
  2419             *returnintra = rd.distortion2 ;
  2421 #if CONFIG_TEMPORAL_DENOISING
  2422         if (cpi->oxcf.noise_sensitivity)
  2424             unsigned int sse;
  2425             vp8_get_inter_mbpred_error(x,&cpi->fn_ptr[BLOCK_16X16],&sse,
  2426                                    mode_mv[this_mode]);
  2428             if (sse < best_rd_sse)
  2429                 best_rd_sse = sse;
  2431             /* Store for later use by denoiser. */
  2432             if (this_mode == ZEROMV && sse < zero_mv_sse )
  2434                 zero_mv_sse = sse;
  2435                 x->best_zeromv_reference_frame =
  2436                         x->e_mbd.mode_info_context->mbmi.ref_frame;
  2439             /* Store the best NEWMV in x for later use in the denoiser. */
  2440             if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
  2441                     sse < best_sse)
  2443                 best_sse = sse;
  2444                 vp8_get_inter_mbpred_error(x,&cpi->fn_ptr[BLOCK_16X16],&best_sse,
  2445                                        mode_mv[this_mode]);
  2446                 x->best_sse_inter_mode = NEWMV;
  2447                 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
  2448                 x->need_to_clamp_best_mvs =
  2449                     x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
  2450                 x->best_reference_frame =
  2451                     x->e_mbd.mode_info_context->mbmi.ref_frame;
  2454 #endif
  2456         /* Did this mode help.. i.i is it the new best mode */
  2457         if (this_rd < best_mode.rd || x->skip)
  2459             /* Note index of best mode so far */
  2460             best_mode_index = mode_index;
  2461             *returnrate = rd.rate2;
  2462             *returndistortion = rd.distortion2;
  2463             if (this_mode <= B_PRED)
  2465                 x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode;
  2466                 /* required for left and above block mv */
  2467                 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
  2469             update_best_mode(&best_mode, this_rd, &rd, other_cost, x);
  2472             /* Testing this mode gave rise to an improvement in best error
  2473              * score. Lower threshold a bit for next time
  2474              */
  2475             x->rd_thresh_mult[mode_index] =
  2476                 (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
  2477                     x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
  2480         /* If the mode did not help improve the best error case then raise
  2481          * the threshold for testing that mode next time around.
  2482          */
  2483         else
  2485             x->rd_thresh_mult[mode_index] += 4;
  2487             if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
  2488                 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
  2490         x->rd_threshes[mode_index] =
  2491             (cpi->rd_baseline_thresh[mode_index] >> 7) *
  2492                 x->rd_thresh_mult[mode_index];
  2494         if (x->skip)
  2495             break;
  2499     /* Reduce the activation RD thresholds for the best choice mode */
  2500     if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
  2502         int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 2);
  2504         x->rd_thresh_mult[best_mode_index] =
  2505             (x->rd_thresh_mult[best_mode_index] >=
  2506                 (MIN_THRESHMULT + best_adjustment)) ?
  2507                     x->rd_thresh_mult[best_mode_index] - best_adjustment :
  2508                     MIN_THRESHMULT;
  2509         x->rd_threshes[best_mode_index] =
  2510             (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
  2511                 x->rd_thresh_mult[best_mode_index];
  2514 #if CONFIG_TEMPORAL_DENOISING
  2515     if (cpi->oxcf.noise_sensitivity)
  2517         if (x->best_sse_inter_mode == DC_PRED)
  2519             /* No best MV found. */
  2520             x->best_sse_inter_mode = best_mode.mbmode.mode;
  2521             x->best_sse_mv = best_mode.mbmode.mv;
  2522             x->need_to_clamp_best_mvs = best_mode.mbmode.need_to_clamp_mvs;
  2523             x->best_reference_frame = best_mode.mbmode.ref_frame;
  2524             best_sse = best_rd_sse;
  2526         vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
  2527                                 recon_yoffset, recon_uvoffset);
  2530         /* Reevaluate ZEROMV after denoising. */
  2531         if (best_mode.mbmode.ref_frame == INTRA_FRAME &&
  2532             x->best_zeromv_reference_frame != INTRA_FRAME)
  2534             int this_rd = INT_MAX;
  2535             int disable_skip = 0;
  2536             int other_cost = 0;
  2537             int this_ref_frame = x->best_zeromv_reference_frame;
  2538             rd.rate2 = x->ref_frame_cost[this_ref_frame] +
  2539                     vp8_cost_mv_ref(ZEROMV, mdcounts);
  2540             rd.distortion2 = 0;
  2542             /* set up the proper prediction buffers for the frame */
  2543             x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
  2544             x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
  2545             x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
  2546             x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
  2548             x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
  2549             x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
  2550             x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
  2552             this_rd = evaluate_inter_mode_rd(mdcounts, &rd, &disable_skip, cpi, x);
  2553             this_rd = calculate_final_rd_costs(this_rd, &rd, &other_cost,
  2554                                                disable_skip, uv_intra_tteob,
  2555                                                intra_rd_penalty, cpi, x);
  2556             if (this_rd < best_mode.rd || x->skip)
  2558                 /* Note index of best mode so far */
  2559                 best_mode_index = mode_index;
  2560                 *returnrate = rd.rate2;
  2561                 *returndistortion = rd.distortion2;
  2562                 update_best_mode(&best_mode, this_rd, &rd, other_cost, x);
  2567 #endif
  2569     if (cpi->is_src_frame_alt_ref &&
  2570         (best_mode.mbmode.mode != ZEROMV || best_mode.mbmode.ref_frame != ALTREF_FRAME))
  2572         x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
  2573         x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
  2574         x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
  2575         x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
  2576         x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
  2577                                         (cpi->common.mb_no_coeff_skip);
  2578         x->e_mbd.mode_info_context->mbmi.partitioning = 0;
  2579         return;
  2583     /* macroblock modes */
  2584     vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mode.mbmode, sizeof(MB_MODE_INFO));
  2586     if (best_mode.mbmode.mode == B_PRED)
  2588         for (i = 0; i < 16; i++)
  2589             xd->mode_info_context->bmi[i].as_mode = best_mode.bmodes[i].as_mode;
  2592     if (best_mode.mbmode.mode == SPLITMV)
  2594         for (i = 0; i < 16; i++)
  2595             xd->mode_info_context->bmi[i].mv.as_int = best_mode.bmodes[i].mv.as_int;
  2597         vpx_memcpy(x->partition_info, &best_mode.partition, sizeof(PARTITION_INFO));
  2599         x->e_mbd.mode_info_context->mbmi.mv.as_int =
  2600                                       x->partition_info->bmi[15].mv.as_int;
  2603     if (sign_bias
  2604         != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
  2605         best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
  2607     rd_update_mvcount(x, &best_ref_mv);
  2610 void vp8_rd_pick_intra_mode(MACROBLOCK *x, int *rate_)
  2612     int error4x4, error16x16;
  2613     int rate4x4, rate16x16 = 0, rateuv;
  2614     int dist4x4, dist16x16, distuv;
  2615     int rate;
  2616     int rate4x4_tokenonly = 0;
  2617     int rate16x16_tokenonly = 0;
  2618     int rateuv_tokenonly = 0;
  2620     x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
  2622     rd_pick_intra_mbuv_mode(x, &rateuv, &rateuv_tokenonly, &distuv);
  2623     rate = rateuv;
  2625     error16x16 = rd_pick_intra16x16mby_mode(x, &rate16x16, &rate16x16_tokenonly,
  2626                                             &dist16x16);
  2628     error4x4 = rd_pick_intra4x4mby_modes(x, &rate4x4, &rate4x4_tokenonly,
  2629                                          &dist4x4, error16x16);
  2631     if (error4x4 < error16x16)
  2633         x->e_mbd.mode_info_context->mbmi.mode = B_PRED;
  2634         rate += rate4x4;
  2636     else
  2638         rate += rate16x16;
  2641     *rate_ = rate;

mercurial