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