media/libvpx/vp8/encoder/encodeframe.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*
     2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
     3  *
     4  *  Use of this source code is governed by a BSD-style license
     5  *  that can be found in the LICENSE file in the root of the source
     6  *  tree. An additional intellectual property rights grant can be found
     7  *  in the file PATENTS.  All contributing project authors may
     8  *  be found in the AUTHORS file in the root of the source tree.
     9  */
    12 #include "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"
    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 );
    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
    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)
    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 };
    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;
    96     /* If the region is flat, lower the activity some more. */
    97     if (act < 8<<12)
    98         act = act < 5<<12 ? act : 5<<12;
   100     return act;
   101 }
   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 }
   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;
   120     if  ( ALT_ACT_MEASURE )
   121     {
   122         int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
   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     }
   133     if ( mb_activity < VP8_ACTIVITY_AVG_MIN )
   134         mb_activity = VP8_ACTIVITY_AVG_MIN;
   136     return mb_activity;
   137 }
   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;
   151         /* Create a list to sort to */
   152         CHECK_MEM_ERROR(sortlist,
   153                         vpx_calloc(sizeof(unsigned int),
   154                         cpi->common.MBs));
   156         /* Copy map to sort list */
   157         vpx_memcpy( sortlist, cpi->mb_activity_map,
   158                     sizeof(unsigned int) * cpi->common.MBs );
   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         }
   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;
   182         cpi->activity_avg = median;
   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
   191     if (cpi->activity_avg < VP8_ACTIVITY_AVG_MIN)
   192         cpi->activity_avg = VP8_ACTIVITY_AVG_MIN;
   194     /* Experimental code: return fixed value normalized for several clips */
   195     if  ( ALT_ACT_MEASURE )
   196         cpi->activity_avg = 100000;
   197 }
   199 #define USE_ACT_INDEX   0
   200 #define OUTPUT_NORM_ACT_STATS   0
   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;
   209     int64_t act;
   210     int64_t a;
   211     int64_t b;
   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
   218     /* Reset pointers to start of activity map */
   219     x->mb_activity_ptr = cpi->mb_activity_map;
   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);
   230             /* Calculate a normalized activity number */
   231             a = act + 4*cpi->activity_avg;
   232             b = 4*act + cpi->activity_avg;
   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);
   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         }
   246 #if OUTPUT_NORM_ACT_STATS
   247         fprintf(f, "\n");
   248 #endif
   250     }
   252 #if OUTPUT_NORM_ACT_STATS
   253     fclose(f);
   254 #endif
   256 }
   257 #endif
   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;
   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
   274     int mb_row, mb_col;
   275     unsigned int mb_activity;
   276     int64_t activity_sum = 0;
   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);
   297             /* measure activity */
   298             mb_activity = mb_activity_measure( cpi, x, mb_row, mb_col );
   300             /* Keep frame sum */
   301             activity_sum += mb_activity;
   303             /* Store MB level activity details. */
   304             *x->mb_activity_ptr = mb_activity;
   306             /* Increment activity map pointer */
   307             x->mb_activity_ptr++;
   309             /* adjust to the next column of source macroblocks */
   310             x->src.y_buffer += 16;
   311         }
   314         /* adjust to the next row of mbs */
   315         x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
   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
   323     }
   325     /* Calculate an "average" MB activity */
   326     calc_av_activity(cpi, activity_sum);
   328 #if USE_ACT_INDEX
   329     /* Calculate an activity index number of each mb */
   330     calc_activity_index( cpi, x );
   331 #endif
   333 }
   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);
   347     /* Apply the masking to the RD multiplier. */
   348     a = act + (2*cpi->activity_avg);
   349     b = (2*act) + cpi->activity_avg;
   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
   356     /* Activity based Zbin adjustment */
   357     adjust_act_zbin(cpi, x);
   358 }
   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);
   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
   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];
   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
   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
   403     /* reset above block coeffs */
   404     xd->above_context = cm->above_context;
   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);
   410     cpi->tplist[mb_row].start = *tp;
   411     /* printf("Main mb_row = %d\n", mb_row); */
   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;
   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);
   426     /* Set the mb activity pointer to the start of the row. */
   427     x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
   429     /* for each macroblock col in image */
   430     for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
   431     {
   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;
   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);
   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);
   455         x->rddiv = cpi->RDDIV;
   456         x->rdmult = cpi->RDMULT;
   458         /* Copy current mb to a buffer */
   459         vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
   461 #if CONFIG_MULTITHREAD
   462         if (cpi->b_multi_threaded != 0)
   463         {
   464             *current_mb_col = mb_col - 1; /* set previous MB done */
   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
   477         if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
   478             vp8_activity_masking(cpi, x);
   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;
   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;
   498         x->active_ptr = cpi->active_map + map_index + mb_col;
   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);
   511 #ifdef MODE_STATS
   512             inter_y_modes[xd->mbmi.mode] ++;
   514             if (xd->mbmi.mode == SPLITMV)
   515             {
   516                 int b;
   518                 for (b = 0; b < xd->mbmi.partition_count; b++)
   519                 {
   520                     inter_b_modes[x->partition->bmi[b].mode] ++;
   521                 }
   522             }
   524 #endif
   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;
   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;
   555             }
   556         }
   558         cpi->tplist[mb_row].stop = *tp;
   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++;
   570         /* Increment the activity mask pointers. */
   571         x->mb_activity_ptr++;
   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;
   578         recon_yoffset += 16;
   579         recon_uvoffset += 8;
   581         /* Keep track of segment usage */
   582         segment_counts[xd->mode_info_context->mbmi.segment_id] ++;
   584         /* skip to next mb */
   585         xd->mode_info_context++;
   586         x->partition_info++;
   587         xd->above_context++;
   588     }
   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);
   596 #if CONFIG_MULTITHREAD
   597     if (cpi->b_multi_threaded != 0)
   598         *current_mb_col = rightmost_col;
   599 #endif
   601     /* this is to account for the border */
   602     xd->mode_info_context++;
   603     x->partition_info++;
   604 }
   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;
   612     /* GF active flags data structure */
   613     x->gf_active_ptr = (signed char *)cpi->gf_active_flags;
   615     /* Activity map pointer */
   616     x->mb_activity_ptr = cpi->mb_activity_map;
   618     x->act_zbin_adj = 0;
   620     x->partition_info = x->pi;
   622     xd->mode_info_context = cm->mi;
   623     xd->mode_info_stride = cm->mode_info_stride;
   625     xd->frame_type = cm->frame_type;
   627     /* reset intra mode contexts */
   628     if (cm->frame_type == KEY_FRAME)
   629         vp8_init_mbmode_probs(cm);
   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];
   636     /* set up frame for intra coded blocks */
   637     vp8_setup_intra_recon(&cm->yv12_fb[cm->new_fb_idx]);
   639     vp8_build_block_offsets(x);
   641     xd->mode_info_context->mbmi.mode = DC_PRED;
   642     xd->mode_info_context->mbmi.uv_mode = DC_PRED;
   644     xd->left_context = &cm->left_context;
   646     x->mvc = cm->fc.mvc;
   648     vpx_memset(cm->above_context, 0,
   649                sizeof(ENTROPY_CONTEXT_PLANES) * cm->mb_cols);
   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);
   671     xd->fullpixel_mask = 0xffffffff;
   672     if(cm->full_pixel)
   673         xd->fullpixel_mask = 0xfffffff8;
   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 }
   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 */
   696                 /* calc probs and branch cts for this frame only */
   697                 int t = 0;      /* token/prob index */
   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 }
   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
   727     vpx_memset(segment_counts, 0, sizeof(segment_counts));
   728     totalrate = 0;
   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     }
   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     }
   754     cpi->mb.skip_true_count = 0;
   755     cpi->tok_count = 0;
   757 #if 0
   758     /* Experimental code */
   759     cpi->frame_distortion = 0;
   760     cpi->last_mb_distortion = 0;
   761 #endif
   763     xd->mode_info_context = cm->mi;
   765     vp8_zero(cpi->mb.MVcount);
   767     vp8cx_frame_init_quantizer(cpi);
   769     vp8_initialize_rd_consts(cpi, x,
   770                              vp8_dc_quant(cm->base_qindex, cm->y1dc_delta_q));
   772     vp8cx_initialize_me_consts(cpi, cm->base_qindex);
   774     if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
   775     {
   776         /* Initialize encode frame context. */
   777         init_encode_frame_mb_context(cpi);
   779         /* Build a frame level activity map */
   780         build_activity_map(cpi);
   781     }
   783     /* re-init encode frame context. */
   784     init_encode_frame_mb_context(cpi);
   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     }
   797 #endif
   799     {
   800         struct vpx_usec_timer  emr_timer;
   801         vpx_usec_timer_start(&emr_timer);
   803 #if CONFIG_MULTITHREAD
   804         if (cpi->b_multi_threaded)
   805         {
   806             int i;
   808             vp8cx_init_mbrthread_data(cpi, x, cpi->mb_row_ei,
   809                                       cpi->encoding_thread_count);
   811             for (i = 0; i < cm->mb_rows; i++)
   812                 cpi->mt_current_mb_col[i] = -1;
   814             for (i = 0; i < cpi->encoding_thread_count; i++)
   815             {
   816                 sem_post(&cpi->h_event_start_encoding[i]);
   817             }
   819             for (mb_row = 0; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1))
   820             {
   821                 vp8_zero(cm->left_context)
   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
   829                 encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate);
   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;
   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;
   840                 if(mb_row == cm->mb_rows - 1)
   841                 {
   842                     sem_post(&cpi->h_event_end_encoding); /* signal frame encoding end */
   843                 }
   844             }
   846             sem_wait(&cpi->h_event_end_encoding); /* wait for other threads to finish */
   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             }
   854             if (xd->segmentation_enabled)
   855             {
   856                 int j;
   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             }
   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;
   874                 cpi->mb.skip_true_count += cpi->mb_row_ei[i].mb.skip_true_count;
   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];
   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];
   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                 }
   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;
   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];
   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];
   904                 /* add up counts for each thread */
   905                 sum_coef_counts(x, &cpi->mb_row_ei[i].mb);
   906             }
   908         }
   909         else
   910 #endif
   911         {
   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)
   918 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
   919                 tp = cpi->tok;
   920 #endif
   922                 encode_mb_row(cpi, cm, mb_row, x, xd, &tp, segment_counts, &totalrate);
   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             }
   930             cpi->tok_count = (unsigned int)(tp - cpi->tok);
   931         }
   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
   944         vpx_usec_timer_mark(&emr_timer);
   945         cpi->time_encode_mb_row += vpx_usec_timer_elapsed(&emr_timer);
   946     }
   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;
   956         /* Set to defaults */
   957         vpx_memset(xd->mb_segment_tree_probs, 255 , sizeof(xd->mb_segment_tree_probs));
   959         tot_count = segment_counts[0] + segment_counts[1] + segment_counts[2] + segment_counts[3];
   961         if (tot_count)
   962         {
   963             xd->mb_segment_tree_probs[0] = ((segment_counts[0] + segment_counts[1]) * 255) / tot_count;
   965             tot_count = segment_counts[0] + segment_counts[1];
   967             if (tot_count > 0)
   968             {
   969                 xd->mb_segment_tree_probs[1] = (segment_counts[0] * 255) / tot_count;
   970             }
   972             tot_count = segment_counts[2] + segment_counts[3];
   974             if (tot_count > 0)
   975                 xd->mb_segment_tree_probs[2] = (segment_counts[2] * 255) / tot_count;
   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     }
   986     /* projected_frame_size in units of BYTES */
   987     cpi->projected_frame_size = totalrate >> 8;
   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;
   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];
  1003         if (tot_modes)
  1004             cpi->this_frame_percent_intra =
  1005                 cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME] * 100 / tot_modes;
  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)))
  1018       vp8_convert_rfct_to_prob(cpi);
  1020 #endif
  1022 void vp8_setup_block_ptrs(MACROBLOCK *x)
  1024     int r, c;
  1025     int i;
  1027     for (r = 0; r < 4; r++)
  1029         for (c = 0; c < 4; c++)
  1031             x->block[r*4+c].src_diff = x->src_diff + r * 4 * 16 + c * 4;
  1035     for (r = 0; r < 2; r++)
  1037         for (c = 0; c < 2; c++)
  1039             x->block[16 + r*2+c].src_diff = x->src_diff + 256 + r * 4 * 8 + c * 4;
  1044     for (r = 0; r < 2; r++)
  1046         for (c = 0; c < 2; c++)
  1048             x->block[20 + r*2+c].src_diff = x->src_diff + 320 + r * 4 * 8 + c * 4;
  1052     x->block[24].src_diff = x->src_diff + 384;
  1055     for (i = 0; i < 25; i++)
  1057         x->block[i].coeff = x->coeff + i * 16;
  1061 void vp8_build_block_offsets(MACROBLOCK *x)
  1063     int block = 0;
  1064     int br, bc;
  1066     vp8_build_block_doffsets(&x->e_mbd);
  1068     /* y blocks */
  1069     x->thismb_ptr = &x->thismb[0];
  1070     for (br = 0; br < 4; br++)
  1072         for (bc = 0; bc < 4; bc++)
  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;
  1082     /* u blocks */
  1083     for (br = 0; br < 2; br++)
  1085         for (bc = 0; bc < 2; bc++)
  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;
  1095     /* v blocks */
  1096     for (br = 0; br < 2; br++)
  1098         for (bc = 0; bc < 2; bc++)
  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;
  1109 static void sum_intra_stats(VP8_COMP *cpi, MACROBLOCK *x)
  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;
  1115 #ifdef MODE_STATS
  1116     const int is_key = cpi->common.frame_type == KEY_FRAME;
  1118     ++ (is_key ? uv_modes : inter_uv_modes)[uvm];
  1120     if (m == B_PRED)
  1122         unsigned int *const bct = is_key ? b_modes : inter_b_modes;
  1124         int b = 0;
  1126         do
  1128             ++ bct[xd->block[b].bmi.mode];
  1130         while (++b < 16);
  1133 #endif
  1135     ++x->ymode_count[m];
  1136     ++x->uv_mode_count[uvm];
  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 )
  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);
  1152     /* Apply the masking to the RD multiplier. */
  1153     a = act + 4*cpi->activity_avg;
  1154     b = 4*act + cpi->activity_avg;
  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
  1163 int vp8cx_encode_intra_macroblock(VP8_COMP *cpi, MACROBLOCK *x,
  1164                                   TOKENEXTRA **t)
  1166     MACROBLOCKD *xd = &x->e_mbd;
  1167     int rate;
  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);
  1174     if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
  1176         adjust_act_zbin( cpi, x );
  1177         vp8_update_zbin_extra(cpi, x);
  1180     if (x->e_mbd.mode_info_context->mbmi.mode == B_PRED)
  1181         vp8_encode_intra4x4mby(x);
  1182     else
  1183         vp8_encode_intra16x16mby(x);
  1185     vp8_encode_intra16x16mbuv(x);
  1187     sum_intra_stats(cpi, x);
  1189     vp8_tokenize_mb(cpi, x, t);
  1191     if (xd->mode_info_context->mbmi.mode != B_PRED)
  1192         vp8_inverse_transform_mby(xd);
  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;
  1200 #ifdef SPEEDSTATS
  1201 extern int cnt_pm;
  1202 #endif
  1204 extern void vp8_fix_contexts(MACROBLOCKD *x);
  1206 int vp8cx_encode_inter_macroblock
  1208     VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t,
  1209     int recon_yoffset, int recon_uvoffset,
  1210     int mb_row, int mb_col
  1213     MACROBLOCKD *const xd = &x->e_mbd;
  1214     int intra_error = 0;
  1215     int rate;
  1216     int distortion;
  1218     x->skip = 0;
  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;
  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
  1234     if (cpi->sf.RD)
  1236         int zbin_mode_boost_enabled = x->zbin_mode_boost_enabled;
  1238         /* Are we using the fast quantizer for the mode selection? */
  1239         if(cpi->sf.use_fastquant_for_pick)
  1241             x->quantize_b      = vp8_fast_quantize_b;
  1242             x->quantize_b_pair = vp8_fast_quantize_b_pair;
  1244             /* the fast quantizer does not use zbin_extra, so
  1245              * do not recalculate */
  1246             x->zbin_mode_boost_enabled = 0;
  1248         vp8_rd_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate,
  1249                                &distortion, &intra_error);
  1251         /* switch back to the regular quantizer for the encode */
  1252         if (cpi->sf.improved_quant)
  1254             x->quantize_b      = vp8_regular_quantize_b;
  1255             x->quantize_b_pair = vp8_regular_quantize_b_pair;
  1258         /* restore cpi->zbin_mode_boost_enabled */
  1259         x->zbin_mode_boost_enabled = zbin_mode_boost_enabled;
  1262     else
  1264         vp8_pick_inter_mode(cpi, x, recon_yoffset, recon_uvoffset, &rate,
  1265                             &distortion, &intra_error, mb_row, mb_col);
  1268     x->prediction_error += distortion;
  1269     x->intra_error += intra_error;
  1271     if(cpi->oxcf.tuning == VP8_TUNE_SSIM)
  1273         /* Adjust the zbin based on this MB rate. */
  1274         adjust_act_zbin( cpi, x );
  1277 #if 0
  1278     /* Experimental RD code */
  1279     cpi->frame_distortion += distortion;
  1280     cpi->last_mb_distortion = distortion;
  1281 #endif
  1283     /* MB level adjutment to quantizer setup */
  1284     if (xd->segmentation_enabled)
  1286         /* If cyclic update enabled */
  1287         if (cpi->current_layer == 0 && cpi->cyclic_refresh_mode_enabled)
  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)))
  1293                 xd->mode_info_context->mbmi.segment_id = 0;
  1295                 /* segment_id changed, so update */
  1296                 vp8cx_mb_init_quantizer(cpi, x, 1);
  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)
  1309             if ( xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME )
  1311                 if (xd->mode_info_context->mbmi.mode == ZEROMV)
  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;
  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;
  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);
  1332     x->count_mb_ref_frame_usage[xd->mode_info_context->mbmi.ref_frame] ++;
  1334     if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
  1336         vp8_encode_intra16x16mbuv(x);
  1338         if (xd->mode_info_context->mbmi.mode == B_PRED)
  1340             vp8_encode_intra4x4mby(x);
  1342         else
  1344             vp8_encode_intra16x16mby(x);
  1347         sum_intra_stats(cpi, x);
  1349     else
  1351         int ref_fb_idx;
  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;
  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;
  1364         if (!x->skip)
  1366             vp8_encode_inter16x16(x);
  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);
  1375     if (!x->skip)
  1377         vp8_tokenize_mb(cpi, x, t);
  1379         if (xd->mode_info_context->mbmi.mode != B_PRED)
  1380             vp8_inverse_transform_mby(xd);
  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);
  1387     else
  1389         /* always set mb_skip_coeff as it is needed by the loopfilter */
  1390         xd->mode_info_context->mbmi.mb_skip_coeff = 1;
  1392         if (cpi->common.mb_no_coeff_skip)
  1394             x->skip_true_count ++;
  1395             vp8_fix_contexts(xd);
  1397         else
  1399             vp8_stuff_mb(cpi, x, t);
  1403     return rate;

mercurial