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