Home | History | Annotate | Download | only in encoder
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 
     12 #include "vpx_config.h"
     13 #include "vp8_rtcd.h"
     14 #include "encodemb.h"
     15 #include "encodemv.h"
     16 #include "vp8/common/common.h"
     17 #include "onyx_int.h"
     18 #include "vp8/common/extend.h"
     19 #include "vp8/common/entropymode.h"
     20 #include "vp8/common/quant_common.h"
     21 #include "segmentation.h"
     22 #include "vp8/common/setupintrarecon.h"
     23 #include "encodeintra.h"
     24 #include "vp8/common/reconinter.h"
     25 #include "rdopt.h"
     26 #include "pickinter.h"
     27 #include "vp8/common/findnearmv.h"
     28 #include <stdio.h>
     29 #include <limits.h>
     30 #include "vp8/common/invtrans.h"
     31 #include "vpx_ports/vpx_timer.h"
     32 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
     33 #include "bitstream.h"
     34 #endif
     35 #include "encodeframe.h"
     36 
     37 extern void vp8_stuff_mb(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t) ;
     38 extern void vp8_calc_ref_frame_costs(int *ref_frame_cost,
     39                                      int prob_intra,
     40                                      int prob_last,
     41                                      int prob_garf
     42                                     );
     43 extern void vp8_convert_rfct_to_prob(VP8_COMP *const cpi);
     44 extern void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex);
     45 extern void vp8_auto_select_speed(VP8_COMP *cpi);
     46 extern void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
     47                                       MACROBLOCK *x,
     48                                       MB_ROW_COMP *mbr_ei,
     49                                       int count);
     50 static void adjust_act_zbin( VP8_COMP *cpi, MACROBLOCK *x );
     51 
     52 #ifdef MODE_STATS
     53 unsigned int inter_y_modes[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
     54 unsigned int inter_uv_modes[4] = {0, 0, 0, 0};
     55 unsigned int inter_b_modes[15]  = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
     56 unsigned int y_modes[5]   = {0, 0, 0, 0, 0};
     57 unsigned int uv_modes[4]  = {0, 0, 0, 0};
     58 unsigned int b_modes[14]  = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
     59 #endif
     60 
     61 
     62 /* activity_avg must be positive, or flat regions could get a zero weight
     63  *  (infinite lambda), which confounds analysis.
     64  * This also avoids the need for divide by zero checks in
     65  *  vp8_activity_masking().
     66  */
     67 #define VP8_ACTIVITY_AVG_MIN (64)
     68 
     69 /* This is used as a reference when computing the source variance for the
     70  *  purposes of activity masking.
     71  * Eventually this should be replaced by custom no-reference routines,
     72  *  which will be faster.
     73  */
     74 static const unsigned char VP8_VAR_OFFS[16]=
     75 {
     76     128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128
     77 };
     78 
     79 
     80 /* Original activity measure from Tim T's code. */
     81 static unsigned int tt_activity_measure( VP8_COMP *cpi, MACROBLOCK *x )
     82 {
     83     unsigned int act;
     84     unsigned int sse;
     85     /* TODO: This could also be done over smaller areas (8x8), but that would
     86      *  require extensive changes elsewhere, as lambda is assumed to be fixed
     87      *  over an entire MB in most of the code.
     88      * Another option is to compute four 8x8 variances, and pick a single
     89      *  lambda using a non-linear combination (e.g., the smallest, or second
     90      *  smallest, etc.).
     91      */
     92     act =  vp8_variance16x16(x->src.y_buffer,
     93                     x->src.y_stride, VP8_VAR_OFFS, 0, &sse);
     94     act = act<<4;
     95 
     96     /* If the region is flat, lower the activity some more. */
     97     if (act < 8<<12)
     98         act = act < 5<<12 ? act : 5<<12;
     99 
    100     return act;
    101 }
    102 
    103 /* Stub for alternative experimental activity measures. */
    104 static unsigned int alt_activity_measure( VP8_COMP *cpi,
    105                                           MACROBLOCK *x, int use_dc_pred )
    106 {
    107     return vp8_encode_intra(cpi,x, use_dc_pred);
    108 }
    109 
    110 
    111 /* Measure the activity of the current macroblock
    112  * What we measure here is TBD so abstracted to this function
    113  */
    114 #define ALT_ACT_MEASURE 1
    115 static unsigned int mb_activity_measure( VP8_COMP *cpi, MACROBLOCK *x,
    116                                   int mb_row, int mb_col)
    117 {
    118     unsigned int mb_activity;
    119 
    120     if  ( ALT_ACT_MEASURE )
    121     {
    122         int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
    123 
    124         /* Or use and alternative. */
    125         mb_activity = alt_activity_measure( cpi, x, use_dc_pred );
    126     }
    127     else
    128     {
    129         /* Original activity measure from Tim T's code. */
    130         mb_activity = tt_activity_measure( cpi, x );
    131     }
    132 
    133     if ( mb_activity < VP8_ACTIVITY_AVG_MIN )
    134         mb_activity = VP8_ACTIVITY_AVG_MIN;
    135 
    136     return mb_activity;
    137 }
    138 
    139 /* Calculate an "average" mb activity value for the frame */
    140 #define ACT_MEDIAN 0
    141 static void calc_av_activity( VP8_COMP *cpi, int64_t activity_sum )
    142 {
    143 #if ACT_MEDIAN
    144     /* Find median: Simple n^2 algorithm for experimentation */
    145     {
    146         unsigned int median;
    147         unsigned int i,j;
    148         unsigned int * sortlist;
    149         unsigned int tmp;
    150 
    151         /* Create a list to sort to */
    152         CHECK_MEM_ERROR(sortlist,
    153                         vpx_calloc(sizeof(unsigned int),
    154                         cpi->common.MBs));
    155 
    156         /* Copy map to sort list */
    157         vpx_memcpy( sortlist, cpi->mb_activity_map,
    158                     sizeof(unsigned int) * cpi->common.MBs );
    159 
    160 
    161         /* Ripple each value down to its correct position */
    162         for ( i = 1; i < cpi->common.MBs; i ++ )
    163         {
    164             for ( j = i; j > 0; j -- )
    165             {
    166                 if ( sortlist[j] < sortlist[j-1] )
    167                 {
    168                     /* Swap values */
    169                     tmp = sortlist[j-1];
    170                     sortlist[j-1] = sortlist[j];
    171                     sortlist[j] = tmp;
    172                 }
    173                 else
    174                     break;
    175             }
    176         }
    177 
    178         /* Even number MBs so estimate median as mean of two either side. */
    179         median = ( 1 + sortlist[cpi->common.MBs >> 1] +
    180                    sortlist[(cpi->common.MBs >> 1) + 1] ) >> 1;
    181 
    182         cpi->activity_avg = median;
    183 
    184         vpx_free(sortlist);
    185     }
    186 #else
    187     /* Simple mean for now */
    188     cpi->activity_avg = (unsigned int)(activity_sum/cpi->common.MBs);
    189 #endif
    190 
    191     if (cpi->activity_avg < VP8_ACTIVITY_AVG_MIN)
    192         cpi->activity_avg = VP8_ACTIVITY_AVG_MIN;
    193 
    194     /* Experimental code: return fixed value normalized for several clips */
    195     if  ( ALT_ACT_MEASURE )
    196         cpi->activity_avg = 100000;
    197 }
    198 
    199 #define USE_ACT_INDEX   0
    200 #define OUTPUT_NORM_ACT_STATS   0
    201 
    202 #if USE_ACT_INDEX
    203 /* Calculate and activity index for each mb */
    204 static void calc_activity_index( VP8_COMP *cpi, MACROBLOCK *x )
    205 {
    206     VP8_COMMON *const cm = & cpi->common;
    207     int mb_row, mb_col;
    208 
    209     int64_t act;
    210     int64_t a;
    211     int64_t b;
    212 
    213 #if OUTPUT_NORM_ACT_STATS
    214     FILE *f = fopen("norm_act.stt", "a");
    215     fprintf(f, "\n%12d\n", cpi->activity_avg );
    216 #endif
    217 
    218     /* Reset pointers to start of activity map */
    219     x->mb_activity_ptr = cpi->mb_activity_map;
    220 
    221     /* Calculate normalized mb activity number. */
    222     for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
    223     {
    224         /* for each macroblock col in image */
    225         for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
    226         {
    227             /* Read activity from the map */
    228             act = *(x->mb_activity_ptr);
    229 
    230             /* Calculate a normalized activity number */
    231             a = act + 4*cpi->activity_avg;
    232             b = 4*act + cpi->activity_avg;
    233 
    234             if ( b >= a )
    235                 *(x->activity_ptr) = (int)((b + (a>>1))/a) - 1;
    236             else
    237                 *(x->activity_ptr) = 1 - (int)((a + (b>>1))/b);
    238 
    239 #if OUTPUT_NORM_ACT_STATS
    240             fprintf(f, " %6d", *(x->mb_activity_ptr));
    241 #endif
    242             /* Increment activity map pointers */
    243             x->mb_activity_ptr++;
    244         }
    245 
    246 #if OUTPUT_NORM_ACT_STATS
    247         fprintf(f, "\n");
    248 #endif
    249 
    250     }
    251 
    252 #if OUTPUT_NORM_ACT_STATS
    253     fclose(f);
    254 #endif
    255 
    256 }
    257 #endif
    258 
    259 /* Loop through all MBs. Note activity of each, average activity and
    260  * calculate a normalized activity for each
    261  */
    262 static void build_activity_map( VP8_COMP *cpi )
    263 {
    264     MACROBLOCK *const x = & cpi->mb;
    265     MACROBLOCKD *xd = &x->e_mbd;
    266     VP8_COMMON *const cm = & cpi->common;
    267 
    268 #if ALT_ACT_MEASURE
    269     YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
    270     int recon_yoffset;
    271     int recon_y_stride = new_yv12->y_stride;
    272 #endif
    273 
    274     int mb_row, mb_col;
    275     unsigned int mb_activity;
    276     int64_t activity_sum = 0;
    277 
    278     /* for each macroblock row in image */
    279     for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
    280     {
    281 #if ALT_ACT_MEASURE
    282         /* reset above block coeffs */
    283         xd->up_available = (mb_row != 0);
    284         recon_yoffset = (mb_row * recon_y_stride * 16);
    285 #endif
    286         /* for each macroblock col in image */
    287         for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
    288         {
    289 #if ALT_ACT_MEASURE
    290             xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
    291             xd->left_available = (mb_col != 0);
    292             recon_yoffset += 16;
    293 #endif
    294             /* Copy current mb to a buffer */
    295             vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
    296 
    297             /* measure activity */
    298             mb_activity = mb_activity_measure( cpi, x, mb_row, mb_col );
    299 
    300             /* Keep frame sum */
    301             activity_sum += mb_activity;
    302 
    303             /* Store MB level activity details. */
    304             *x->mb_activity_ptr = mb_activity;
    305 
    306             /* Increment activity map pointer */
    307             x->mb_activity_ptr++;
    308 
    309             /* adjust to the next column of source macroblocks */
    310             x->src.y_buffer += 16;
    311         }
    312 
    313 
    314         /* adjust to the next row of mbs */
    315         x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
    316 
    317 #if ALT_ACT_MEASURE
    318         /* extend the recon for intra prediction */
    319         vp8_extend_mb_row(new_yv12, xd->dst.y_buffer + 16,
    320                           xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
    321 #endif
    322 
    323     }
    324 
    325     /* Calculate an "average" MB activity */
    326     calc_av_activity(cpi, activity_sum);
    327 
    328 #if USE_ACT_INDEX
    329     /* Calculate an activity index number of each mb */
    330     calc_activity_index( cpi, x );
    331 #endif
    332 
    333 }
    334 
    335 /* Macroblock activity masking */
    336 void vp8_activity_masking(VP8_COMP *cpi, MACROBLOCK *x)
    337 {
    338 #if USE_ACT_INDEX
    339     x->rdmult += *(x->mb_activity_ptr) * (x->rdmult >> 2);
    340     x->errorperbit = x->rdmult * 100 /(110 * x->rddiv);
    341     x->errorperbit += (x->errorperbit==0);
    342 #else
    343     int64_t a;
    344     int64_t b;
    345     int64_t act = *(x->mb_activity_ptr);
    346 
    347     /* Apply the masking to the RD multiplier. */
    348     a = act + (2*cpi->activity_avg);
    349     b = (2*act) + cpi->activity_avg;
    350 
    351     x->rdmult = (unsigned int)(((int64_t)x->rdmult*b + (a>>1))/a);
    352     x->errorperbit = x->rdmult * 100 /(110 * x->rddiv);
    353     x->errorperbit += (x->errorperbit==0);
    354 #endif
    355 
    356     /* Activity based Zbin adjustment */
    357     adjust_act_zbin(cpi, x);
    358 }
    359 
    360 static
    361 void encode_mb_row(VP8_COMP *cpi,
    362                    VP8_COMMON *cm,
    363                    int mb_row,
    364                    MACROBLOCK  *x,
    365                    MACROBLOCKD *xd,
    366                    TOKENEXTRA **tp,
    367                    int *segment_counts,
    368                    int *totalrate)
    369 {
    370     int recon_yoffset, recon_uvoffset;
    371     int mb_col;
    372     int ref_fb_idx = cm->lst_fb_idx;
    373     int dst_fb_idx = cm->new_fb_idx;
    374     int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
    375     int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
    376     int map_index = (mb_row * cpi->common.mb_cols);
    377 
    378 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
    379     const int num_part = (1 << cm->multi_token_partition);
    380     TOKENEXTRA * tp_start = cpi->tok;
    381     vp8_writer *w;
    382 #endif
    383 
    384 #if CONFIG_MULTITHREAD
    385     const int nsync = cpi->mt_sync_range;
    386     const int rightmost_col = cm->mb_cols + nsync;
    387     volatile const int *last_row_current_mb_col;
    388     volatile int *current_mb_col = &cpi->mt_current_mb_col[mb_row];
    389 
    390     if ((cpi->b_multi_threaded != 0) && (mb_row != 0))
    391         last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
    392     else
    393         last_row_current_mb_col = &rightmost_col;
    394 #endif
    395 
    396 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
    397     if(num_part > 1)
    398         w= &cpi->bc[1 + (mb_row % num_part)];
    399     else
    400         w = &cpi->bc[1];
    401 #endif
    402 
    403     /* reset above block coeffs */
    404     xd->above_context = cm->above_context;
    405 
    406     xd->up_available = (mb_row != 0);
    407     recon_yoffset = (mb_row * recon_y_stride * 16);
    408     recon_uvoffset = (mb_row * recon_uv_stride * 8);
    409 
    410     cpi->tplist[mb_row].start = *tp;
    411     /* printf("Main mb_row = %d\n", mb_row); */
    412 
    413     /* Distance of Mb to the top & bottom edges, specified in 1/8th pel
    414      * units as they are always compared to values that are in 1/8th pel
    415      */
    416     xd->mb_to_top_edge = -((mb_row * 16) << 3);
    417     xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
    418 
    419     /* Set up limit values for vertical motion vector components
    420      * to prevent them extending beyond the UMV borders
    421      */
    422     x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
    423     x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
    424                         + (VP8BORDERINPIXELS - 16);
    425 
    426     /* Set the mb activity pointer to the start of the row. */
    427     x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
    428 
    429     /* for each macroblock col in image */
    430     for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
    431     {
    432 
    433 #if  (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
    434         *tp = cpi->tok;
    435 #endif
    436         /* Distance of Mb to the left & right edges, specified in
    437          * 1/8th pel units as they are always compared to values
    438          * that are in 1/8th pel units
    439          */
    440         xd->mb_to_left_edge = -((mb_col * 16) << 3);
    441         xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
    442 
    443         /* Set up limit values for horizontal motion vector components
    444          * to prevent them extending beyond the UMV borders
    445          */
    446         x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
    447         x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16)
    448                             + (VP8BORDERINPIXELS - 16);
    449 
    450         xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
    451         xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
    452         xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
    453         xd->left_available = (mb_col != 0);
    454 
    455         x->rddiv = cpi->RDDIV;
    456         x->rdmult = cpi->RDMULT;
    457 
    458         /* Copy current mb to a buffer */
    459         vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
    460 
    461 #if CONFIG_MULTITHREAD
    462         if (cpi->b_multi_threaded != 0)
    463         {
    464             *current_mb_col = mb_col - 1; /* set previous MB done */
    465 
    466             if ((mb_col & (nsync - 1)) == 0)
    467             {
    468                 while (mb_col > (*last_row_current_mb_col - nsync))
    469                 {
    470                     x86_pause_hint();
    471                     thread_sleep(0);
    472                 }
    473             }
    474         }
    475 #endif
    476 
    477         if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
    478             vp8_activity_masking(cpi, x);
    479 
    480         /* Is segmentation enabled */
    481         /* MB level adjustment to quantizer */
    482         if (xd->segmentation_enabled)
    483         {
    484             /* Code to set segment id in xd->mbmi.segment_id for current MB
    485              * (with range checking)
    486              */
    487             if (cpi->segmentation_map[map_index+mb_col] <= 3)
    488                 xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[map_index+mb_col];
    489             else
    490                 xd->mode_info_context->mbmi.segment_id = 0;
    491 
    492             vp8cx_mb_init_quantizer(cpi, x, 1);
    493         }
    494         else
    495             /* Set to Segment 0 by default */
    496             xd->mode_info_context->mbmi.segment_id = 0;
    497 
    498         x->active_ptr = cpi->active_map + map_index + mb_col;
    499 
    500         if (cm->frame_type == KEY_FRAME)
    501         {
    502             *totalrate += vp8cx_encode_intra_macroblock(cpi, x, tp);
    503 #ifdef MODE_STATS
    504             y_modes[xd->mbmi.mode] ++;
    505 #endif
    506         }
    507         else
    508         {
    509             *totalrate += vp8cx_encode_inter_macroblock(cpi, x, tp, recon_yoffset, recon_uvoffset, mb_row, mb_col);
    510 
    511 #ifdef MODE_STATS
    512             inter_y_modes[xd->mbmi.mode] ++;
    513 
    514             if (xd->mbmi.mode == SPLITMV)
    515             {
    516                 int b;
    517 
    518                 for (b = 0; b < xd->mbmi.partition_count; b++)
    519                 {
    520                     inter_b_modes[x->partition->bmi[b].mode] ++;
    521                 }
    522             }
    523 
    524 #endif
    525 
    526             /* Special case code for cyclic refresh
    527              * If cyclic update enabled then copy xd->mbmi.segment_id; (which
    528              * may have been updated based on mode during
    529              * vp8cx_encode_inter_macroblock()) back into the global
    530              * segmentation map
    531              */
    532             if ((cpi->current_layer == 0) &&
    533                 (cpi->cyclic_refresh_mode_enabled &&
    534                  xd->segmentation_enabled))
    535             {
    536                 cpi->segmentation_map[map_index+mb_col] = xd->mode_info_context->mbmi.segment_id;
    537 
    538                 /* If the block has been refreshed mark it as clean (the
    539                  * magnitude of the -ve influences how long it will be before
    540                  * we consider another refresh):
    541                  * Else if it was coded (last frame 0,0) and has not already
    542                  * been refreshed then mark it as a candidate for cleanup
    543                  * next time (marked 0) else mark it as dirty (1).
    544                  */
    545                 if (xd->mode_info_context->mbmi.segment_id)
    546                     cpi->cyclic_refresh_map[map_index+mb_col] = -1;
    547                 else if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
    548                 {
    549                     if (cpi->cyclic_refresh_map[map_index+mb_col] == 1)
    550                         cpi->cyclic_refresh_map[map_index+mb_col] = 0;
    551                 }
    552                 else
    553                     cpi->cyclic_refresh_map[map_index+mb_col] = 1;
    554 
    555             }
    556         }
    557 
    558         cpi->tplist[mb_row].stop = *tp;
    559 
    560 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
    561         /* pack tokens for this MB */
    562         {
    563             int tok_count = *tp - tp_start;
    564             pack_tokens(w, tp_start, tok_count);
    565         }
    566 #endif
    567         /* Increment pointer into gf usage flags structure. */
    568         x->gf_active_ptr++;
    569 
    570         /* Increment the activity mask pointers. */
    571         x->mb_activity_ptr++;
    572 
    573         /* adjust to the next column of macroblocks */
    574         x->src.y_buffer += 16;
    575         x->src.u_buffer += 8;
    576         x->src.v_buffer += 8;
    577 
    578         recon_yoffset += 16;
    579         recon_uvoffset += 8;
    580 
    581         /* Keep track of segment usage */
    582         segment_counts[xd->mode_info_context->mbmi.segment_id] ++;
    583 
    584         /* skip to next mb */
    585         xd->mode_info_context++;
    586         x->partition_info++;
    587         xd->above_context++;
    588     }
    589 
    590     /* extend the recon for intra prediction */
    591     vp8_extend_mb_row( &cm->yv12_fb[dst_fb_idx],
    592                         xd->dst.y_buffer + 16,
    593                         xd->dst.u_buffer + 8,
    594                         xd->dst.v_buffer + 8);
    595 
    596 #if CONFIG_MULTITHREAD
    597     if (cpi->b_multi_threaded != 0)
    598         *current_mb_col = rightmost_col;
    599 #endif
    600 
    601     /* this is to account for the border */
    602     xd->mode_info_context++;
    603     x->partition_info++;
    604 }
    605 
    606 static void init_encode_frame_mb_context(VP8_COMP *cpi)
    607 {
    608     MACROBLOCK *const x = & cpi->mb;
    609     VP8_COMMON *const cm = & cpi->common;
    610     MACROBLOCKD *const xd = & x->e_mbd;
    611 
    612     /* GF active flags data structure */
    613     x->gf_active_ptr = (signed char *)cpi->gf_active_flags;
    614 
    615     /* Activity map pointer */
    616     x->mb_activity_ptr = cpi->mb_activity_map;
    617 
    618     x->act_zbin_adj = 0;
    619 
    620     x->partition_info = x->pi;
    621 
    622     xd->mode_info_context = cm->mi;
    623     xd->mode_info_stride = cm->mode_info_stride;
    624 
    625     xd->frame_type = cm->frame_type;
    626 
    627     /* reset intra mode contexts */
    628     if (cm->frame_type == KEY_FRAME)
    629         vp8_init_mbmode_probs(cm);
    630 
    631     /* Copy data over into macro block data structures. */
    632     x->src = * cpi->Source;
    633     xd->pre = cm->yv12_fb[cm->lst_fb_idx];
    634     xd->dst = cm->yv12_fb[cm->new_fb_idx];
    635 
    636     /* set up frame for intra coded blocks */
    637     vp8_setup_intra_recon(&cm->yv12_fb[cm->new_fb_idx]);
    638 
    639     vp8_build_block_offsets(x);
    640 
    641     xd->mode_info_context->mbmi.mode = DC_PRED;
    642     xd->mode_info_context->mbmi.uv_mode = DC_PRED;
    643 
    644     xd->left_context = &cm->left_context;
    645 
    646     x->mvc = cm->fc.mvc;
    647 
    648     vpx_memset(cm->above_context, 0,
    649                sizeof(ENTROPY_CONTEXT_PLANES) * cm->mb_cols);
    650 
    651     /* Special case treatment when GF and ARF are not sensible options
    652      * for reference
    653      */
    654     if (cpi->ref_frame_flags == VP8_LAST_FRAME)
    655         vp8_calc_ref_frame_costs(x->ref_frame_cost,
    656                                  cpi->prob_intra_coded,255,128);
    657     else if ((cpi->oxcf.number_of_layers > 1) &&
    658                (cpi->ref_frame_flags == VP8_GOLD_FRAME))
    659         vp8_calc_ref_frame_costs(x->ref_frame_cost,
    660                                  cpi->prob_intra_coded,1,255);
    661     else if ((cpi->oxcf.number_of_layers > 1) &&
    662                 (cpi->ref_frame_flags == VP8_ALTR_FRAME))
    663         vp8_calc_ref_frame_costs(x->ref_frame_cost,
    664                                  cpi->prob_intra_coded,1,1);
    665     else
    666         vp8_calc_ref_frame_costs(x->ref_frame_cost,
    667                                  cpi->prob_intra_coded,
    668                                  cpi->prob_last_coded,
    669                                  cpi->prob_gf_coded);
    670 
    671     xd->fullpixel_mask = 0xffffffff;
    672     if(cm->full_pixel)
    673         xd->fullpixel_mask = 0xfffffff8;
    674 
    675     vp8_zero(x->coef_counts);
    676     vp8_zero(x->ymode_count);
    677     vp8_zero(x->uv_mode_count)
    678     x->prediction_error = 0;
    679     x->intra_error = 0;
    680     vp8_zero(x->count_mb_ref_frame_usage);
    681 }
    682 
    683 static void sum_coef_counts(MACROBLOCK *x, MACROBLOCK *x_thread)
    684 {
    685     int i = 0;
    686     do
    687     {
    688         int j = 0;
    689         do
    690         {
    691             int k = 0;
    692             do
    693             {
    694                 /* at every context */
    695 
    696                 /* calc probs and branch cts for this frame only */
    697                 int t = 0;      /* token/prob index */
    698 
    699                 do
    700                 {
    701                     x->coef_counts [i][j][k][t] +=
    702                         x_thread->coef_counts [i][j][k][t];
    703                 }
    704                 while (++t < ENTROPY_NODES);
    705             }
    706             while (++k < PREV_COEF_CONTEXTS);
    707         }
    708         while (++j < COEF_BANDS);
    709     }
    710     while (++i < BLOCK_TYPES);
    711 }
    712 
    713 void vp8_encode_frame(VP8_COMP *cpi)
    714 {
    715     int mb_row;
    716     MACROBLOCK *const x = & cpi->mb;
    717     VP8_COMMON *const cm = & cpi->common;
    718     MACROBLOCKD *const xd = & x->e_mbd;
    719     TOKENEXTRA *tp = cpi->tok;
    720     int segment_counts[MAX_MB_SEGMENTS];
    721     int totalrate;
    722 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
    723     BOOL_CODER * bc = &cpi->bc[1]; /* bc[0] is for control partition */
    724     const int num_part = (1 << cm->multi_token_partition);
    725 #endif
    726 
    727     vpx_memset(segment_counts, 0, sizeof(segment_counts));
    728     totalrate = 0;
    729 
    730     if (cpi->compressor_speed == 2)
    731     {
    732         if (cpi->oxcf.cpu_used < 0)
    733             cpi->Speed = -(cpi->oxcf.cpu_used);
    734         else
    735             vp8_auto_select_speed(cpi);
    736     }
    737 
    738     /* Functions setup for all frame types so we can use MC in AltRef */
    739     if(!cm->use_bilinear_mc_filter)
    740     {
    741         xd->subpixel_predict        = vp8_sixtap_predict4x4;
    742         xd->subpixel_predict8x4     = vp8_sixtap_predict8x4;
    743         xd->subpixel_predict8x8     = vp8_sixtap_predict8x8;
    744         xd->subpixel_predict16x16   = vp8_sixtap_predict16x16;
    745     }
    746     else
    747     {
    748         xd->subpixel_predict        = vp8_bilinear_predict4x4;
    749         xd->subpixel_predict8x4     = vp8_bilinear_predict8x4;
    750         xd->subpixel_predict8x8     = vp8_bilinear_predict8x8;
    751         xd->subpixel_predict16x16   = vp8_bilinear_predict16x16;
    752     }
    753 
    754     cpi->mb.skip_true_count = 0;
    755     cpi->tok_count = 0;
    756 
    757 #if 0
    758     /* Experimental code */
    759     cpi->frame_distortion = 0;
    760     cpi->last_mb_distortion = 0;
    761 #endif
    762 
    763     xd->mode_info_context = cm->mi;
    764 
    765     vp8_zero(cpi->mb.MVcount);
    766 
    767     vp8cx_frame_init_quantizer(cpi);
    768 
    769     vp8_initialize_rd_consts(cpi, x,
    770                              vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
    771 
    772     vp8cx_initialize_me_consts(cpi, cm->base_qindex);
    773 
    774     if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
    775     {
    776         /* Initialize encode frame context. */
    777         init_encode_frame_mb_context(cpi);
    778 
    779         /* Build a frame level activity map */
    780         build_activity_map(cpi);
    781     }
    782 
    783     /* re-init encode frame context. */
    784     init_encode_frame_mb_context(cpi);
    785 
    786 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
    787     {
    788         int i;
    789         for(i = 0; i < num_part; i++)
    790         {
    791             vp8_start_encode(&bc[i], cpi->partition_d[i + 1],
    792                     cpi->partition_d_end[i + 1]);
    793             bc[i].error = &cm->error;
    794         }
    795     }
    796 
    797 #endif
    798 
    799     {
    800         struct vpx_usec_timer  emr_timer;
    801         vpx_usec_timer_start(&emr_timer);
    802 
    803 #if CONFIG_MULTITHREAD
    804         if (cpi->b_multi_threaded)
    805         {
    806             int i;
    807 
    808             vp8cx_init_mbrthread_data(cpi, x, cpi->mb_row_ei,
    809                                       cpi->encoding_thread_count);
    810 
    811             for (i = 0; i < cm->mb_rows; i++)
    812                 cpi->mt_current_mb_col[i] = -1;
    813 
    814             for (i = 0; i < cpi->encoding_thread_count; i++)
    815             {
    816                 sem_post(&cpi->h_event_start_encoding[i]);
    817             }
    818 
    819             for (mb_row = 0; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1))
    820             {
    821                 vp8_zero(cm->left_context)
    822 
    823 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
    824                 tp = cpi->tok;
    825 #else
    826                 tp = cpi->tok + mb_row * (cm->mb_cols * 16 * 24);
    827 #endif
    828 
    829                 encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate);
    830 
    831                 /* adjust to the next row of mbs */
    832                 x->src.y_buffer += 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - 16 * cm->mb_cols;
    833                 x->src.u_buffer +=  8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
    834                 x->src.v_buffer +=  8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
    835 
    836                 xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count;
    837                 x->partition_info  += xd->mode_info_stride * cpi->encoding_thread_count;
    838                 x->gf_active_ptr   += cm->mb_cols * cpi->encoding_thread_count;
    839 
    840                 if(mb_row == cm->mb_rows - 1)
    841                 {
    842                     sem_post(&cpi->h_event_end_encoding); /* signal frame encoding end */
    843                 }
    844             }
    845 
    846             sem_wait(&cpi->h_event_end_encoding); /* wait for other threads to finish */
    847 
    848             for (mb_row = 0; mb_row < cm->mb_rows; mb_row ++)
    849             {
    850                 cpi->tok_count += (unsigned int)
    851                   (cpi->tplist[mb_row].stop - cpi->tplist[mb_row].start);
    852             }
    853 
    854             if (xd->segmentation_enabled)
    855             {
    856                 int j;
    857 
    858                 if (xd->segmentation_enabled)
    859                 {
    860                     for (i = 0; i < cpi->encoding_thread_count; i++)
    861                     {
    862                         for (j = 0; j < 4; j++)
    863                             segment_counts[j] += cpi->mb_row_ei[i].segment_counts[j];
    864                     }
    865                 }
    866             }
    867 
    868             for (i = 0; i < cpi->encoding_thread_count; i++)
    869             {
    870                 int mode_count;
    871                 int c_idx;
    872                 totalrate += cpi->mb_row_ei[i].totalrate;
    873 
    874                 cpi->mb.skip_true_count += cpi->mb_row_ei[i].mb.skip_true_count;
    875 
    876                 for(mode_count = 0; mode_count < VP8_YMODES; mode_count++)
    877                     cpi->mb.ymode_count[mode_count] +=
    878                         cpi->mb_row_ei[i].mb.ymode_count[mode_count];
    879 
    880                 for(mode_count = 0; mode_count < VP8_UV_MODES; mode_count++)
    881                     cpi->mb.uv_mode_count[mode_count] +=
    882                         cpi->mb_row_ei[i].mb.uv_mode_count[mode_count];
    883 
    884                 for(c_idx = 0; c_idx < MVvals; c_idx++)
    885                 {
    886                     cpi->mb.MVcount[0][c_idx] +=
    887                         cpi->mb_row_ei[i].mb.MVcount[0][c_idx];
    888                     cpi->mb.MVcount[1][c_idx] +=
    889                         cpi->mb_row_ei[i].mb.MVcount[1][c_idx];
    890                 }
    891 
    892                 cpi->mb.prediction_error +=
    893                     cpi->mb_row_ei[i].mb.prediction_error;
    894                 cpi->mb.intra_error += cpi->mb_row_ei[i].mb.intra_error;
    895 
    896                 for(c_idx = 0; c_idx < MAX_REF_FRAMES; c_idx++)
    897                     cpi->mb.count_mb_ref_frame_usage[c_idx] +=
    898                         cpi->mb_row_ei[i].mb.count_mb_ref_frame_usage[c_idx];
    899 
    900                 for(c_idx = 0; c_idx < MAX_ERROR_BINS; c_idx++)
    901                     cpi->mb.error_bins[c_idx] +=
    902                         cpi->mb_row_ei[i].mb.error_bins[c_idx];
    903 
    904                 /* add up counts for each thread */
    905                 sum_coef_counts(x, &cpi->mb_row_ei[i].mb);
    906             }
    907 
    908         }
    909         else
    910 #endif
    911         {
    912 
    913             /* for each macroblock row in image */
    914             for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
    915             {
    916                 vp8_zero(cm->left_context)
    917 
    918 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
    919                 tp = cpi->tok;
    920 #endif
    921 
    922                 encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate);
    923 
    924                 /* adjust to the next row of mbs */
    925                 x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
    926                 x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
    927                 x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
    928             }
    929 
    930             cpi->tok_count = (unsigned int)(tp - cpi->tok);
    931         }
    932 
    933 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
    934         {
    935             int i;
    936             for(i = 0; i < num_part; i++)
    937             {
    938                 vp8_stop_encode(&bc[i]);
    939                 cpi->partition_sz[i+1] = bc[i].pos;
    940             }
    941         }
    942 #endif
    943 
    944         vpx_usec_timer_mark(&emr_timer);
    945         cpi->time_encode_mb_row += vpx_usec_timer_elapsed(&emr_timer);
    946     }
    947 
    948 
    949     // Work out the segment probabilities if segmentation is enabled
    950     // and needs to be updated
    951     if (xd->segmentation_enabled && xd->update_mb_segmentation_map)
    952     {
    953         int tot_count;
    954         int i;
    955 
    956         /* Set to defaults */
    957         vpx_memset(xd->mb_segment_tree_probs, 255 , sizeof(xd->mb_segment_tree_probs));
    958 
    959         tot_count = segment_counts[0] + segment_counts[1] + segment_counts[2] + segment_counts[3];
    960 
    961         if (tot_count)
    962         {
    963             xd->mb_segment_tree_probs[0] = ((segment_counts[0] + segment_counts[1]) * 255) / tot_count;
    964 
    965             tot_count = segment_counts[0] + segment_counts[1];
    966 
    967             if (tot_count > 0)
    968             {
    969                 xd->mb_segment_tree_probs[1] = (segment_counts[0] * 255) / tot_count;
    970             }
    971 
    972             tot_count = segment_counts[2] + segment_counts[3];
    973 
    974             if (tot_count > 0)
    975                 xd->mb_segment_tree_probs[2] = (segment_counts[2] * 255) / tot_count;
    976 
    977             /* Zero probabilities not allowed */
    978             for (i = 0; i < MB_FEATURE_TREE_PROBS; i ++)
    979             {
    980                 if (xd->mb_segment_tree_probs[i] == 0)
    981                     xd->mb_segment_tree_probs[i] = 1;
    982             }
    983         }
    984     }
    985 
    986     /* projected_frame_size in units of BYTES */
    987     cpi->projected_frame_size = totalrate >> 8;
    988 
    989     /* Make a note of the percentage MBs coded Intra. */
    990     if (cm->frame_type == KEY_FRAME)
    991     {
    992         cpi->this_frame_percent_intra = 100;
    993     }
    994     else
    995     {
    996         int tot_modes;
    997 
    998         tot_modes = cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME]
    999                     + cpi->mb.count_mb_ref_frame_usage[LAST_FRAME]
   1000                     + cpi->mb.count_mb_ref_frame_usage[GOLDEN_FRAME]
   1001                     + cpi->mb.count_mb_ref_frame_usage[ALTREF_FRAME];
   1002 
   1003         if (tot_modes)
   1004             cpi->this_frame_percent_intra =
   1005                 cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME] * 100 / tot_modes;
   1006 
   1007     }
   1008 
   1009 #if ! CONFIG_REALTIME_ONLY
   1010     /* Adjust the projected reference frame usage probability numbers to
   1011      * reflect what we have just seen. This may be useful when we make
   1012      * multiple iterations of the recode loop rather than continuing to use
   1013      * values from the previous frame.
   1014      */
   1015     if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) ||
   1016         (!cm->refresh_alt_ref_frame && !cm->refresh_golden_frame)))
   1017     {
   1018       vp8_convert_rfct_to_prob(cpi);
   1019     }
   1020 #endif
   1021 }
   1022 void vp8_setup_block_ptrs(MACROBLOCK *x)
   1023 {
   1024     int r, c;
   1025     int i;
   1026 
   1027     for (r = 0; r < 4; r++)
   1028     {
   1029         for (c = 0; c < 4; c++)
   1030         {
   1031             x->block[r*4+c].src_diff = x->src_diff + r * 4 * 16 + c * 4;
   1032         }
   1033     }
   1034 
   1035     for (r = 0; r < 2; r++)
   1036     {
   1037         for (c = 0; c < 2; c++)
   1038         {
   1039             x->block[16 + r*2+c].src_diff = x->src_diff + 256 + r * 4 * 8 + c * 4;
   1040         }
   1041     }
   1042 
   1043 
   1044     for (r = 0; r < 2; r++)
   1045     {
   1046         for (c = 0; c < 2; c++)
   1047         {
   1048             x->block[20 + r*2+c].src_diff = x->src_diff + 320 + r * 4 * 8 + c * 4;
   1049         }
   1050     }
   1051 
   1052     x->block[24].src_diff = x->src_diff + 384;
   1053 
   1054 
   1055     for (i = 0; i < 25; i++)
   1056     {
   1057         x->block[i].coeff = x->coeff + i * 16;
   1058     }
   1059 }
   1060 
   1061 void vp8_build_block_offsets(MACROBLOCK *x)
   1062 {
   1063     int block = 0;
   1064     int br, bc;
   1065 
   1066     vp8_build_block_doffsets(&x->e_mbd);
   1067 
   1068     /* y blocks */
   1069     x->thismb_ptr = &x->thismb[0];
   1070     for (br = 0; br < 4; br++)
   1071     {
   1072         for (bc = 0; bc < 4; bc++)
   1073         {
   1074             BLOCK *this_block = &x->block[block];
   1075             this_block->base_src = &x->thismb_ptr;
   1076             this_block->src_stride = 16;
   1077             this_block->src = 4 * br * 16 + 4 * bc;
   1078             ++block;
   1079         }
   1080     }
   1081 
   1082     /* u blocks */
   1083     for (br = 0; br < 2; br++)
   1084     {
   1085         for (bc = 0; bc < 2; bc++)
   1086         {
   1087             BLOCK *this_block = &x->block[block];
   1088             this_block->base_src = &x->src.u_buffer;
   1089             this_block->src_stride = x->src.uv_stride;
   1090             this_block->src = 4 * br * this_block->src_stride + 4 * bc;
   1091             ++block;
   1092         }
   1093     }
   1094 
   1095     /* v blocks */
   1096     for (br = 0; br < 2; br++)
   1097     {
   1098         for (bc = 0; bc < 2; bc++)
   1099         {
   1100             BLOCK *this_block = &x->block[block];
   1101             this_block->base_src = &x->src.v_buffer;
   1102             this_block->src_stride = x->src.uv_stride;
   1103             this_block->src = 4 * br * this_block->src_stride + 4 * bc;
   1104             ++block;
   1105         }
   1106     }
   1107 }
   1108 
   1109 static void sum_intra_stats(VP8_COMP *cpi, MACROBLOCK *x)
   1110 {
   1111     const MACROBLOCKD *xd = & x->e_mbd;
   1112     const MB_PREDICTION_MODE m = xd->mode_info_context->mbmi.mode;
   1113     const MB_PREDICTION_MODE uvm = xd->mode_info_context->mbmi.uv_mode;
   1114 
   1115 #ifdef MODE_STATS
   1116     const int is_key = cpi->common.frame_type == KEY_FRAME;
   1117 
   1118     ++ (is_key ? uv_modes : inter_uv_modes)[uvm];
   1119 
   1120     if (m == B_PRED)
   1121     {
   1122         unsigned int *const bct = is_key ? b_modes : inter_b_modes;
   1123 
   1124         int b = 0;
   1125 
   1126         do
   1127         {
   1128             ++ bct[xd->block[b].bmi.mode];
   1129         }
   1130         while (++b < 16);
   1131     }
   1132 
   1133 #endif
   1134 
   1135     ++x->ymode_count[m];
   1136     ++x->uv_mode_count[uvm];
   1137 
   1138 }
   1139 
   1140 /* Experimental stub function to create a per MB zbin adjustment based on
   1141  * some previously calculated measure of MB activity.
   1142  */
   1143 static void adjust_act_zbin( VP8_COMP *cpi, MACROBLOCK *x )
   1144 {
   1145 #if USE_ACT_INDEX
   1146     x->act_zbin_adj = *(x->mb_activity_ptr);
   1147 #else
   1148     int64_t a;
   1149     int64_t b;
   1150     int64_t act = *(x->mb_activity_ptr);
   1151 
   1152     /* Apply the masking to the RD multiplier. */
   1153     a = act + 4*cpi->activity_avg;
   1154     b = 4*act + cpi->activity_avg;
   1155 
   1156     if ( act > cpi->activity_avg )
   1157         x->act_zbin_adj = (int)(((int64_t)b + (a>>1))/a) - 1;
   1158     else
   1159         x->act_zbin_adj = 1 - (int)(((int64_t)a + (b>>1))/b);
   1160 #endif
   1161 }
   1162 
   1163 int vp8cx_encode_intra_macroblock(VP8_COMP *cpi, MACROBLOCK *x,
   1164                                   TOKENEXTRA **t)
   1165 {
   1166     MACROBLOCKD *xd = &x->e_mbd;
   1167     int rate;
   1168 
   1169     if (cpi->sf.RD && cpi->compressor_speed != 2)
   1170         vp8_rd_pick_intra_mode(x, &rate);
   1171     else
   1172         vp8_pick_intra_mode(x, &rate);
   1173 
   1174     if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
   1175     {
   1176         adjust_act_zbin( cpi, x );
   1177         vp8_update_zbin_extra(cpi, x);
   1178     }
   1179 
   1180     if (x->e_mbd.mode_info_context->mbmi.mode == B_PRED)
   1181         vp8_encode_intra4x4mby(x);
   1182     else
   1183         vp8_encode_intra16x16mby(x);
   1184 
   1185     vp8_encode_intra16x16mbuv(x);
   1186 
   1187     sum_intra_stats(cpi, x);
   1188 
   1189     vp8_tokenize_mb(cpi, x, t);
   1190 
   1191     if (xd->mode_info_context->mbmi.mode != B_PRED)
   1192         vp8_inverse_transform_mby(xd);
   1193 
   1194     vp8_dequant_idct_add_uv_block
   1195                     (xd->qcoeff+16*16, xd->dequant_uv,
   1196                      xd->dst.u_buffer, xd->dst.v_buffer,
   1197                      xd->dst.uv_stride, xd->eobs+16);
   1198     return rate;
   1199 }
   1200 #ifdef SPEEDSTATS
   1201 extern int cnt_pm;
   1202 #endif
   1203 
   1204 extern void vp8_fix_contexts(MACROBLOCKD *x);
   1205 
   1206 int vp8cx_encode_inter_macroblock
   1207 (
   1208     VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t,
   1209     int recon_yoffset, int recon_uvoffset,
   1210     int mb_row, int mb_col
   1211 )
   1212 {
   1213     MACROBLOCKD *const xd = &x->e_mbd;
   1214     int intra_error = 0;
   1215     int rate;
   1216     int distortion;
   1217 
   1218     x->skip = 0;
   1219 
   1220     if (xd->segmentation_enabled)
   1221         x->encode_breakout = cpi->segment_encode_breakout[xd->mode_info_context->mbmi.segment_id];
   1222     else
   1223         x->encode_breakout = cpi->oxcf.encode_breakout;
   1224 
   1225 #if CONFIG_TEMPORAL_DENOISING
   1226     /* Reset the best sse mode/mv for each macroblock. */
   1227     x->best_reference_frame = INTRA_FRAME;
   1228     x->best_zeromv_reference_frame = INTRA_FRAME;
   1229     x->best_sse_inter_mode = 0;
   1230     x->best_sse_mv.as_int = 0;
   1231     x->need_to_clamp_best_mvs = 0;
   1232 #endif
   1233 
   1234     if (cpi->sf.RD)
   1235     {
   1236         int zbin_mode_boost_enabled = x->zbin_mode_boost_enabled;
   1237 
   1238         /* Are we using the fast quantizer for the mode selection? */
   1239         if(cpi->sf.use_fastquant_for_pick)
   1240         {
   1241             x->quantize_b      = vp8_fast_quantize_b;
   1242             x->quantize_b_pair = vp8_fast_quantize_b_pair;
   1243 
   1244             /* the fast quantizer does not use zbin_extra, so
   1245              * do not recalculate */
   1246             x->zbin_mode_boost_enabled = 0;
   1247         }
   1248         vp8_rd_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate,
   1249                                &distortion, &intra_error);
   1250 
   1251         /* switch back to the regular quantizer for the encode */
   1252         if (cpi->sf.improved_quant)
   1253         {
   1254             x->quantize_b      = vp8_regular_quantize_b;
   1255             x->quantize_b_pair = vp8_regular_quantize_b_pair;
   1256         }
   1257 
   1258         /* restore cpi->zbin_mode_boost_enabled */
   1259         x->zbin_mode_boost_enabled = zbin_mode_boost_enabled;
   1260 
   1261     }
   1262     else
   1263     {
   1264         vp8_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate,
   1265                             &distortion, &intra_error, mb_row, mb_col);
   1266     }
   1267 
   1268     x->prediction_error += distortion;
   1269     x->intra_error += intra_error;
   1270 
   1271     if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
   1272     {
   1273         /* Adjust the zbin based on this MB rate. */
   1274         adjust_act_zbin( cpi, x );
   1275     }
   1276 
   1277 #if 0
   1278     /* Experimental RD code */
   1279     cpi->frame_distortion += distortion;
   1280     cpi->last_mb_distortion = distortion;
   1281 #endif
   1282 
   1283     /* MB level adjutment to quantizer setup */
   1284     if (xd->segmentation_enabled)
   1285     {
   1286         /* If cyclic update enabled */
   1287         if (cpi->current_layer == 0 && cpi->cyclic_refresh_mode_enabled)
   1288         {
   1289             /* Clear segment_id back to 0 if not coded (last frame 0,0) */
   1290             if ((xd->mode_info_context->mbmi.segment_id == 1) &&
   1291                 ((xd->mode_info_context->mbmi.ref_frame != LAST_FRAME) || (xd->mode_info_context->mbmi.mode != ZEROMV)))
   1292             {
   1293                 xd->mode_info_context->mbmi.segment_id = 0;
   1294 
   1295                 /* segment_id changed, so update */
   1296                 vp8cx_mb_init_quantizer(cpi, x, 1);
   1297             }
   1298         }
   1299     }
   1300 
   1301     {
   1302         /* Experimental code.
   1303          * Special case for gf and arf zeromv modes, for 1 temporal layer.
   1304          * Increase zbin size to supress noise.
   1305          */
   1306         x->zbin_mode_boost = 0;
   1307         if (x->zbin_mode_boost_enabled)
   1308         {
   1309             if ( xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME )
   1310             {
   1311                 if (xd->mode_info_context->mbmi.mode == ZEROMV)
   1312                 {
   1313                     if (xd->mode_info_context->mbmi.ref_frame != LAST_FRAME &&
   1314                         cpi->oxcf.number_of_layers == 1)
   1315                         x->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
   1316                     else
   1317                         x->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
   1318                 }
   1319                 else if (xd->mode_info_context->mbmi.mode == SPLITMV)
   1320                     x->zbin_mode_boost = 0;
   1321                 else
   1322                     x->zbin_mode_boost = MV_ZBIN_BOOST;
   1323             }
   1324         }
   1325 
   1326         /* The fast quantizer doesn't use zbin_extra, only do so with
   1327          * the regular quantizer. */
   1328         if (cpi->sf.improved_quant)
   1329             vp8_update_zbin_extra(cpi, x);
   1330     }
   1331 
   1332     x->count_mb_ref_frame_usage[xd->mode_info_context->mbmi.ref_frame] ++;
   1333 
   1334     if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
   1335     {
   1336         vp8_encode_intra16x16mbuv(x);
   1337 
   1338         if (xd->mode_info_context->mbmi.mode == B_PRED)
   1339         {
   1340             vp8_encode_intra4x4mby(x);
   1341         }
   1342         else
   1343         {
   1344             vp8_encode_intra16x16mby(x);
   1345         }
   1346 
   1347         sum_intra_stats(cpi, x);
   1348     }
   1349     else
   1350     {
   1351         int ref_fb_idx;
   1352 
   1353         if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)
   1354             ref_fb_idx = cpi->common.lst_fb_idx;
   1355         else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
   1356             ref_fb_idx = cpi->common.gld_fb_idx;
   1357         else
   1358             ref_fb_idx = cpi->common.alt_fb_idx;
   1359 
   1360         xd->pre.y_buffer = cpi->common.yv12_fb[ref_fb_idx].y_buffer + recon_yoffset;
   1361         xd->pre.u_buffer = cpi->common.yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset;
   1362         xd->pre.v_buffer = cpi->common.yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset;
   1363 
   1364         if (!x->skip)
   1365         {
   1366             vp8_encode_inter16x16(x);
   1367         }
   1368         else
   1369             vp8_build_inter16x16_predictors_mb(xd, xd->dst.y_buffer,
   1370                                            xd->dst.u_buffer, xd->dst.v_buffer,
   1371                                            xd->dst.y_stride, xd->dst.uv_stride);
   1372 
   1373     }
   1374 
   1375     if (!x->skip)
   1376     {
   1377         vp8_tokenize_mb(cpi, x, t);
   1378 
   1379         if (xd->mode_info_context->mbmi.mode != B_PRED)
   1380             vp8_inverse_transform_mby(xd);
   1381 
   1382         vp8_dequant_idct_add_uv_block
   1383                         (xd->qcoeff+16*16, xd->dequant_uv,
   1384                          xd->dst.u_buffer, xd->dst.v_buffer,
   1385                          xd->dst.uv_stride, xd->eobs+16);
   1386     }
   1387     else
   1388     {
   1389         /* always set mb_skip_coeff as it is needed by the loopfilter */
   1390         xd->mode_info_context->mbmi.mb_skip_coeff = 1;
   1391 
   1392         if (cpi->common.mb_no_coeff_skip)
   1393         {
   1394             x->skip_true_count ++;
   1395             vp8_fix_contexts(xd);
   1396         }
   1397         else
   1398         {
   1399             vp8_stuff_mb(cpi, x, t);
   1400         }
   1401     }
   1402 
   1403     return rate;
   1404 }
   1405