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