2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
15 #include "./vp9_rtcd.h"
16 #include "./vpx_config.h"
18 #include "vpx_ports/vpx_timer.h"
20 #include "vp9/common/vp9_common.h"
21 #include "vp9/common/vp9_entropy.h"
22 #include "vp9/common/vp9_entropymode.h"
23 #include "vp9/common/vp9_extend.h"
24 #include "vp9/common/vp9_findnearmv.h"
25 #include "vp9/common/vp9_idct.h"
26 #include "vp9/common/vp9_mvref_common.h"
27 #include "vp9/common/vp9_pred_common.h"
28 #include "vp9/common/vp9_quant_common.h"
29 #include "vp9/common/vp9_reconintra.h"
30 #include "vp9/common/vp9_reconinter.h"
31 #include "vp9/common/vp9_seg_common.h"
32 #include "vp9/common/vp9_tile_common.h"
33 #include "vp9/encoder/vp9_encodeframe.h"
34 #include "vp9/encoder/vp9_encodeintra.h"
35 #include "vp9/encoder/vp9_encodemb.h"
36 #include "vp9/encoder/vp9_encodemv.h"
37 #include "vp9/encoder/vp9_onyx_int.h"
38 #include "vp9/encoder/vp9_rdopt.h"
39 #include "vp9/encoder/vp9_segmentation.h"
40 #include "vp9/common/vp9_systemdependent.h"
41 #include "vp9/encoder/vp9_tokenize.h"
42 #include "vp9/encoder/vp9_vaq.h"
45 #define DBG_PRNT_SEGMAP 0
53 static INLINE uint8_t *get_sb_index(MACROBLOCK *x, BLOCK_SIZE subsize) {
78 static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled,
79 int mi_row, int mi_col, BLOCK_SIZE bsize);
81 static void adjust_act_zbin(VP9_COMP *cpi, MACROBLOCK *x);
83 /* activity_avg must be positive, or flat regions could get a zero weight
84 * (infinite lambda), which confounds analysis.
85 * This also avoids the need for divide by zero checks in
86 * vp9_activity_masking().
88 #define ACTIVITY_AVG_MIN (64)
90 /* Motion vector component magnitude threshold for defining fast motion. */
91 #define FAST_MOTION_MV_THRESH (24)
93 /* This is used as a reference when computing the source variance for the
94 * purposes of activity masking.
95 * Eventually this should be replaced by custom no-reference routines,
96 * which will be faster.
98 static const uint8_t VP9_VAR_OFFS[64] = {
99 128, 128, 128, 128, 128, 128, 128, 128,
100 128, 128, 128, 128, 128, 128, 128, 128,
101 128, 128, 128, 128, 128, 128, 128, 128,
102 128, 128, 128, 128, 128, 128, 128, 128,
103 128, 128, 128, 128, 128, 128, 128, 128,
104 128, 128, 128, 128, 128, 128, 128, 128,
105 128, 128, 128, 128, 128, 128, 128, 128,
106 128, 128, 128, 128, 128, 128, 128, 128
109 static unsigned int get_sby_perpixel_variance(VP9_COMP *cpi, MACROBLOCK *x,
111 unsigned int var, sse;
112 var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
113 x->plane[0].src.stride,
114 VP9_VAR_OFFS, 0, &sse);
115 return (var + (1 << (num_pels_log2_lookup[bs] - 1))) >>
116 num_pels_log2_lookup[bs];
119 // Original activity measure from Tim T's code.
120 static unsigned int tt_activity_measure(MACROBLOCK *x) {
123 /* TODO: This could also be done over smaller areas (8x8), but that would
124 * require extensive changes elsewhere, as lambda is assumed to be fixed
125 * over an entire MB in most of the code.
126 * Another option is to compute four 8x8 variances, and pick a single
127 * lambda using a non-linear combination (e.g., the smallest, or second
130 act = vp9_variance16x16(x->plane[0].src.buf, x->plane[0].src.stride,
131 VP9_VAR_OFFS, 0, &sse);
134 /* If the region is flat, lower the activity some more. */
136 act = act < 5 << 12 ? act : 5 << 12;
141 // Stub for alternative experimental activity measures.
142 static unsigned int alt_activity_measure(MACROBLOCK *x, int use_dc_pred) {
143 return vp9_encode_intra(x, use_dc_pred);
146 // Measure the activity of the current macroblock
147 // What we measure here is TBD so abstracted to this function
148 #define ALT_ACT_MEASURE 1
149 static unsigned int mb_activity_measure(MACROBLOCK *x, int mb_row, int mb_col) {
150 unsigned int mb_activity;
152 if (ALT_ACT_MEASURE) {
153 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
155 // Or use and alternative.
156 mb_activity = alt_activity_measure(x, use_dc_pred);
158 // Original activity measure from Tim T's code.
159 mb_activity = tt_activity_measure(x);
162 if (mb_activity < ACTIVITY_AVG_MIN)
163 mb_activity = ACTIVITY_AVG_MIN;
168 // Calculate an "average" mb activity value for the frame
170 static void calc_av_activity(VP9_COMP *cpi, int64_t activity_sum) {
172 // Find median: Simple n^2 algorithm for experimentation
176 unsigned int *sortlist;
179 // Create a list to sort to
180 CHECK_MEM_ERROR(&cpi->common, sortlist, vpx_calloc(sizeof(unsigned int),
183 // Copy map to sort list
184 vpx_memcpy(sortlist, cpi->mb_activity_map,
185 sizeof(unsigned int) * cpi->common.MBs);
187 // Ripple each value down to its correct position
188 for (i = 1; i < cpi->common.MBs; i ++) {
189 for (j = i; j > 0; j --) {
190 if (sortlist[j] < sortlist[j - 1]) {
192 tmp = sortlist[j - 1];
193 sortlist[j - 1] = sortlist[j];
201 // Even number MBs so estimate median as mean of two either side.
202 median = (1 + sortlist[cpi->common.MBs >> 1] +
203 sortlist[(cpi->common.MBs >> 1) + 1]) >> 1;
205 cpi->activity_avg = median;
210 // Simple mean for now
211 cpi->activity_avg = (unsigned int) (activity_sum / cpi->common.MBs);
214 if (cpi->activity_avg < ACTIVITY_AVG_MIN)
215 cpi->activity_avg = ACTIVITY_AVG_MIN;
217 // Experimental code: return fixed value normalized for several clips
219 cpi->activity_avg = 100000;
222 #define USE_ACT_INDEX 0
223 #define OUTPUT_NORM_ACT_STATS 0
226 // Calculate an activity index for each mb
227 static void calc_activity_index(VP9_COMP *cpi, MACROBLOCK *x) {
228 VP9_COMMON *const cm = &cpi->common;
235 #if OUTPUT_NORM_ACT_STATS
236 FILE *f = fopen("norm_act.stt", "a");
237 fprintf(f, "\n%12d\n", cpi->activity_avg);
240 // Reset pointers to start of activity map
241 x->mb_activity_ptr = cpi->mb_activity_map;
243 // Calculate normalized mb activity number.
244 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
245 // for each macroblock col in image
246 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
247 // Read activity from the map
248 act = *(x->mb_activity_ptr);
250 // Calculate a normalized activity number
251 a = act + 4 * cpi->activity_avg;
252 b = 4 * act + cpi->activity_avg;
255 *(x->activity_ptr) = (int)((b + (a >> 1)) / a) - 1;
257 *(x->activity_ptr) = 1 - (int)((a + (b >> 1)) / b);
259 #if OUTPUT_NORM_ACT_STATS
260 fprintf(f, " %6d", *(x->mb_activity_ptr));
262 // Increment activity map pointers
263 x->mb_activity_ptr++;
266 #if OUTPUT_NORM_ACT_STATS
271 #if OUTPUT_NORM_ACT_STATS
275 #endif // USE_ACT_INDEX
277 // Loop through all MBs. Note activity of each, average activity and
278 // calculate a normalized activity for each
279 static void build_activity_map(VP9_COMP *cpi) {
280 MACROBLOCK * const x = &cpi->mb;
281 MACROBLOCKD *xd = &x->e_mbd;
282 VP9_COMMON * const cm = &cpi->common;
285 YV12_BUFFER_CONFIG *new_yv12 = get_frame_new_buffer(cm);
287 int recon_y_stride = new_yv12->y_stride;
291 unsigned int mb_activity;
292 int64_t activity_sum = 0;
294 x->mb_activity_ptr = cpi->mb_activity_map;
296 // for each macroblock row in image
297 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
299 // reset above block coeffs
300 xd->up_available = (mb_row != 0);
301 recon_yoffset = (mb_row * recon_y_stride * 16);
303 // for each macroblock col in image
304 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
306 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
307 xd->left_available = (mb_col != 0);
312 mb_activity = mb_activity_measure(x, mb_row, mb_col);
315 activity_sum += mb_activity;
317 // Store MB level activity details.
318 *x->mb_activity_ptr = mb_activity;
320 // Increment activity map pointer
321 x->mb_activity_ptr++;
323 // adjust to the next column of source macroblocks
324 x->plane[0].src.buf += 16;
327 // adjust to the next row of mbs
328 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
331 // Calculate an "average" MB activity
332 calc_av_activity(cpi, activity_sum);
335 // Calculate an activity index number of each mb
336 calc_activity_index(cpi, x);
340 // Macroblock activity masking
341 void vp9_activity_masking(VP9_COMP *cpi, MACROBLOCK *x) {
343 x->rdmult += *(x->mb_activity_ptr) * (x->rdmult >> 2);
344 x->errorperbit = x->rdmult * 100 / (110 * x->rddiv);
345 x->errorperbit += (x->errorperbit == 0);
349 int64_t act = *(x->mb_activity_ptr);
351 // Apply the masking to the RD multiplier.
352 a = act + (2 * cpi->activity_avg);
353 b = (2 * act) + cpi->activity_avg;
355 x->rdmult = (unsigned int) (((int64_t) x->rdmult * b + (a >> 1)) / a);
356 x->errorperbit = x->rdmult * 100 / (110 * x->rddiv);
357 x->errorperbit += (x->errorperbit == 0);
360 // Activity based Zbin adjustment
361 adjust_act_zbin(cpi, x);
364 static void update_state(VP9_COMP *cpi, PICK_MODE_CONTEXT *ctx,
365 BLOCK_SIZE bsize, int output_enabled) {
367 VP9_COMMON *const cm = &cpi->common;
368 MACROBLOCK *const x = &cpi->mb;
369 MACROBLOCKD *const xd = &x->e_mbd;
370 struct macroblock_plane *const p = x->plane;
371 struct macroblockd_plane *const pd = xd->plane;
372 MODE_INFO *mi = &ctx->mic;
373 MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi;
374 MODE_INFO *mi_addr = xd->mi_8x8[0];
376 int mb_mode_index = ctx->best_mode_index;
377 const int mis = cm->mode_info_stride;
378 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
379 const int mi_height = num_8x8_blocks_high_lookup[bsize];
381 assert(mi->mbmi.mode < MB_MODE_COUNT);
382 assert(mi->mbmi.ref_frame[0] < MAX_REF_FRAMES);
383 assert(mi->mbmi.ref_frame[1] < MAX_REF_FRAMES);
384 assert(mi->mbmi.sb_type == bsize);
388 for (i = 0; i < MAX_MB_PLANE; ++i) {
389 p[i].coeff = ctx->coeff_pbuf[i][1];
390 pd[i].qcoeff = ctx->qcoeff_pbuf[i][1];
391 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
392 pd[i].eobs = ctx->eobs_pbuf[i][1];
395 // Restore the coding context of the MB to that that was in place
396 // when the mode was picked for it
397 for (y = 0; y < mi_height; y++)
398 for (x_idx = 0; x_idx < mi_width; x_idx++)
399 if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx
400 && (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y)
401 xd->mi_8x8[x_idx + y * mis] = mi_addr;
403 if (cpi->sf.variance_adaptive_quantization) {
404 vp9_mb_init_quantizer(cpi, x);
407 // FIXME(rbultje) I'm pretty sure this should go to the end of this block
408 // (i.e. after the output_enabled)
409 if (bsize < BLOCK_32X32) {
410 if (bsize < BLOCK_16X16)
411 ctx->tx_rd_diff[ALLOW_16X16] = ctx->tx_rd_diff[ALLOW_8X8];
412 ctx->tx_rd_diff[ALLOW_32X32] = ctx->tx_rd_diff[ALLOW_16X16];
415 if (is_inter_block(mbmi) && mbmi->sb_type < BLOCK_8X8) {
416 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
417 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
421 vpx_memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk,
422 sizeof(uint8_t) * ctx->num_4x4_blk);
427 if (!vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
428 for (i = 0; i < TX_MODES; i++)
429 cpi->rd_tx_select_diff[i] += ctx->tx_rd_diff[i];
432 if (frame_is_intra_only(cm)) {
433 #if CONFIG_INTERNAL_STATS
434 static const int kf_mode_index[] = {
436 THR_V_PRED /*V_PRED*/,
437 THR_H_PRED /*H_PRED*/,
438 THR_D45_PRED /*D45_PRED*/,
439 THR_D135_PRED /*D135_PRED*/,
440 THR_D117_PRED /*D117_PRED*/,
441 THR_D153_PRED /*D153_PRED*/,
442 THR_D207_PRED /*D207_PRED*/,
443 THR_D63_PRED /*D63_PRED*/,
446 cpi->mode_chosen_counts[kf_mode_index[mi->mbmi.mode]]++;
449 // Note how often each mode chosen as best
450 cpi->mode_chosen_counts[mb_mode_index]++;
451 if (is_inter_block(mbmi)
452 && (mbmi->sb_type < BLOCK_8X8 || mbmi->mode == NEWMV)) {
454 const MV_REFERENCE_FRAME rf1 = mbmi->ref_frame[0];
455 const MV_REFERENCE_FRAME rf2 = mbmi->ref_frame[1];
456 best_mv[0].as_int = ctx->best_ref_mv.as_int;
457 best_mv[1].as_int = ctx->second_best_ref_mv.as_int;
458 if (mbmi->mode == NEWMV) {
459 best_mv[0].as_int = mbmi->ref_mvs[rf1][0].as_int;
461 best_mv[1].as_int = mbmi->ref_mvs[rf2][0].as_int;
463 mbmi->best_mv[0].as_int = best_mv[0].as_int;
464 mbmi->best_mv[1].as_int = best_mv[1].as_int;
465 vp9_update_mv_count(cpi, x, best_mv);
468 if (cm->mcomp_filter_type == SWITCHABLE && is_inter_mode(mbmi->mode)) {
469 const int ctx = vp9_get_pred_context_switchable_interp(xd);
470 ++cm->counts.switchable_interp[ctx][mbmi->interp_filter];
473 cpi->rd_comp_pred_diff[SINGLE_PREDICTION_ONLY] += ctx->single_pred_diff;
474 cpi->rd_comp_pred_diff[COMP_PREDICTION_ONLY] += ctx->comp_pred_diff;
475 cpi->rd_comp_pred_diff[HYBRID_PREDICTION] += ctx->hybrid_pred_diff;
477 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
478 cpi->rd_filter_diff[i] += ctx->best_filter_diff[i];
482 void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
483 int mi_row, int mi_col) {
484 uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
486 const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
490 for (i = 0; i < MAX_MB_PLANE; i++)
491 setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
492 NULL, x->e_mbd.plane[i].subsampling_x,
493 x->e_mbd.plane[i].subsampling_y);
496 static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile,
497 int mi_row, int mi_col, BLOCK_SIZE bsize) {
498 MACROBLOCK *const x = &cpi->mb;
499 VP9_COMMON *const cm = &cpi->common;
500 MACROBLOCKD *const xd = &x->e_mbd;
502 const int dst_fb_idx = cm->new_fb_idx;
503 const int idx_str = xd->mode_info_stride * mi_row + mi_col;
504 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
505 const int mi_height = num_8x8_blocks_high_lookup[bsize];
506 const int mb_row = mi_row >> 1;
507 const int mb_col = mi_col >> 1;
508 const int idx_map = mb_row * cm->mb_cols + mb_col;
509 const struct segmentation *const seg = &cm->seg;
511 set_skip_context(xd, cpi->above_context, cpi->left_context, mi_row, mi_col);
513 // Activity map pointer
514 x->mb_activity_ptr = &cpi->mb_activity_map[idx_map];
515 x->active_ptr = cpi->active_map + idx_map;
517 xd->mi_8x8 = cm->mi_grid_visible + idx_str;
518 xd->prev_mi_8x8 = cm->prev_mi_grid_visible + idx_str;
520 // Special case: if prev_mi is NULL, the previous mode info context
522 xd->last_mi = cm->prev_mi ? xd->prev_mi_8x8[0] : NULL;
524 xd->mi_8x8[0] = cm->mi + idx_str;
526 mbmi = &xd->mi_8x8[0]->mbmi;
528 // Set up destination pointers
529 setup_dst_planes(xd, &cm->yv12_fb[dst_fb_idx], mi_row, mi_col);
531 // Set up limit values for MV components
532 // mv beyond the range do not produce new/different prediction block
533 x->mv_row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
534 x->mv_col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
535 x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
536 x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;
538 // Set up distance of MB to edge of frame in 1/8th pel units
539 assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
540 set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
541 cm->mi_rows, cm->mi_cols);
543 /* set up source buffers */
544 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
547 x->rddiv = cpi->RDDIV;
548 x->rdmult = cpi->RDMULT;
552 if (!cpi->sf.variance_adaptive_quantization) {
553 uint8_t *map = seg->update_map ? cpi->segmentation_map
554 : cm->last_frame_seg_map;
555 mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
557 vp9_mb_init_quantizer(cpi, x);
559 if (seg->enabled && cpi->seg0_cnt > 0
560 && !vp9_segfeature_active(seg, 0, SEG_LVL_REF_FRAME)
561 && vp9_segfeature_active(seg, 1, SEG_LVL_REF_FRAME)) {
562 cpi->seg0_progress = (cpi->seg0_idx << 16) / cpi->seg0_cnt;
564 const int y = mb_row & ~3;
565 const int x = mb_col & ~3;
566 const int p16 = ((mb_row & 1) << 1) + (mb_col & 1);
567 const int p32 = ((mb_row & 2) << 2) + ((mb_col & 2) << 1);
568 const int tile_progress = tile->mi_col_start * cm->mb_rows >> 1;
569 const int mb_cols = (tile->mi_col_end - tile->mi_col_start) >> 1;
571 cpi->seg0_progress = ((y * mb_cols + x * 4 + p32 + p16 + tile_progress)
575 x->encode_breakout = cpi->segment_encode_breakout[mbmi->segment_id];
577 mbmi->segment_id = 0;
578 x->encode_breakout = cpi->oxcf.encode_breakout;
582 static void pick_sb_modes(VP9_COMP *cpi, const TileInfo *const tile,
583 int mi_row, int mi_col,
584 int *totalrate, int64_t *totaldist,
585 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
587 VP9_COMMON *const cm = &cpi->common;
588 MACROBLOCK *const x = &cpi->mb;
589 MACROBLOCKD *const xd = &x->e_mbd;
590 struct macroblock_plane *const p = x->plane;
591 struct macroblockd_plane *const pd = xd->plane;
593 int orig_rdmult = x->rdmult;
596 vp9_clear_system_state(); // __asm emms;
597 rdmult_ratio = 1.0; // avoid uninitialized warnings
599 // Use the lower precision, but faster, 32x32 fdct for mode selection.
600 x->use_lp32x32fdct = 1;
602 if (bsize < BLOCK_8X8) {
603 // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
604 // there is nothing to be done.
605 if (x->ab_index != 0) {
612 set_offsets(cpi, tile, mi_row, mi_col, bsize);
613 xd->mi_8x8[0]->mbmi.sb_type = bsize;
615 for (i = 0; i < MAX_MB_PLANE; ++i) {
616 p[i].coeff = ctx->coeff_pbuf[i][0];
617 pd[i].qcoeff = ctx->qcoeff_pbuf[i][0];
618 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
619 pd[i].eobs = ctx->eobs_pbuf[i][0];
623 // Set to zero to make sure we do not use the previous encoded frame stats
624 xd->mi_8x8[0]->mbmi.skip_coeff = 0;
626 x->source_variance = get_sby_perpixel_variance(cpi, x, bsize);
628 if (cpi->sf.variance_adaptive_quantization) {
630 if (bsize <= BLOCK_16X16) {
631 energy = x->mb_energy;
633 energy = vp9_block_energy(cpi, x, bsize);
636 xd->mi_8x8[0]->mbmi.segment_id = vp9_vaq_segment_id(energy);
637 rdmult_ratio = vp9_vaq_rdmult_ratio(energy);
638 vp9_mb_init_quantizer(cpi, x);
641 if (cpi->oxcf.tuning == VP8_TUNE_SSIM)
642 vp9_activity_masking(cpi, x);
644 if (cpi->sf.variance_adaptive_quantization) {
645 vp9_clear_system_state(); // __asm emms;
646 x->rdmult = round(x->rdmult * rdmult_ratio);
649 // Find best coding mode & reconstruct the MB so it is available
650 // as a predictor for MBs that follow in the SB
651 if (frame_is_intra_only(cm)) {
652 vp9_rd_pick_intra_mode_sb(cpi, x, totalrate, totaldist, bsize, ctx,
655 if (bsize >= BLOCK_8X8)
656 vp9_rd_pick_inter_mode_sb(cpi, x, tile, mi_row, mi_col,
657 totalrate, totaldist, bsize, ctx, best_rd);
659 vp9_rd_pick_inter_mode_sub8x8(cpi, x, tile, mi_row, mi_col, totalrate,
660 totaldist, bsize, ctx, best_rd);
663 if (cpi->sf.variance_adaptive_quantization) {
664 x->rdmult = orig_rdmult;
665 if (*totalrate != INT_MAX) {
666 vp9_clear_system_state(); // __asm emms;
667 *totalrate = round(*totalrate * rdmult_ratio);
672 static void update_stats(VP9_COMP *cpi) {
673 VP9_COMMON *const cm = &cpi->common;
674 MACROBLOCK *const x = &cpi->mb;
675 MACROBLOCKD *const xd = &x->e_mbd;
676 MODE_INFO *mi = xd->mi_8x8[0];
677 MB_MODE_INFO *const mbmi = &mi->mbmi;
679 if (!frame_is_intra_only(cm)) {
680 const int seg_ref_active = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
684 cpi->intra_inter_count[vp9_get_pred_context_intra_inter(xd)]
685 [is_inter_block(mbmi)]++;
687 // If the segment reference feature is enabled we have only a single
688 // reference frame allowed for the segment so exclude it from
689 // the reference frame counts used to work out probabilities.
690 if (is_inter_block(mbmi) && !seg_ref_active) {
691 if (cm->comp_pred_mode == HYBRID_PREDICTION)
692 cpi->comp_inter_count[vp9_get_pred_context_comp_inter_inter(cm, xd)]
693 [has_second_ref(mbmi)]++;
695 if (has_second_ref(mbmi)) {
696 cpi->comp_ref_count[vp9_get_pred_context_comp_ref_p(cm, xd)]
697 [mbmi->ref_frame[0] == GOLDEN_FRAME]++;
699 cpi->single_ref_count[vp9_get_pred_context_single_ref_p1(xd)][0]
700 [mbmi->ref_frame[0] != LAST_FRAME]++;
701 if (mbmi->ref_frame[0] != LAST_FRAME)
702 cpi->single_ref_count[vp9_get_pred_context_single_ref_p2(xd)][1]
703 [mbmi->ref_frame[0] != GOLDEN_FRAME]++;
709 static BLOCK_SIZE *get_sb_partitioning(MACROBLOCK *x, BLOCK_SIZE bsize) {
712 return &x->sb64_partitioning;
714 return &x->sb_partitioning[x->sb_index];
716 return &x->mb_partitioning[x->sb_index][x->mb_index];
718 return &x->b_partitioning[x->sb_index][x->mb_index][x->b_index];
725 static void restore_context(VP9_COMP *cpi, int mi_row, int mi_col,
726 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
727 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
728 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
730 MACROBLOCK *const x = &cpi->mb;
731 MACROBLOCKD *const xd = &x->e_mbd;
733 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
734 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
735 int mi_width = num_8x8_blocks_wide_lookup[bsize];
736 int mi_height = num_8x8_blocks_high_lookup[bsize];
737 for (p = 0; p < MAX_MB_PLANE; p++) {
739 cpi->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
740 a + num_4x4_blocks_wide * p,
741 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
742 xd->plane[p].subsampling_x);
745 + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
746 l + num_4x4_blocks_high * p,
747 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
748 xd->plane[p].subsampling_y);
750 vpx_memcpy(cpi->above_seg_context + mi_col, sa,
751 sizeof(*cpi->above_seg_context) * mi_width);
752 vpx_memcpy(cpi->left_seg_context + (mi_row & MI_MASK), sl,
753 sizeof(cpi->left_seg_context[0]) * mi_height);
755 static void save_context(VP9_COMP *cpi, int mi_row, int mi_col,
756 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
757 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
758 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
760 const MACROBLOCK *const x = &cpi->mb;
761 const MACROBLOCKD *const xd = &x->e_mbd;
763 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
764 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
765 int mi_width = num_8x8_blocks_wide_lookup[bsize];
766 int mi_height = num_8x8_blocks_high_lookup[bsize];
768 // buffer the above/left context information of the block in search.
769 for (p = 0; p < MAX_MB_PLANE; ++p) {
771 a + num_4x4_blocks_wide * p,
772 cpi->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
773 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
774 xd->plane[p].subsampling_x);
776 l + num_4x4_blocks_high * p,
778 + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
779 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
780 xd->plane[p].subsampling_y);
782 vpx_memcpy(sa, cpi->above_seg_context + mi_col,
783 sizeof(*cpi->above_seg_context) * mi_width);
784 vpx_memcpy(sl, cpi->left_seg_context + (mi_row & MI_MASK),
785 sizeof(cpi->left_seg_context[0]) * mi_height);
788 static void encode_b(VP9_COMP *cpi, const TileInfo *const tile,
789 TOKENEXTRA **tp, int mi_row, int mi_col,
790 int output_enabled, BLOCK_SIZE bsize, int sub_index) {
791 VP9_COMMON *const cm = &cpi->common;
792 MACROBLOCK *const x = &cpi->mb;
794 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
798 *get_sb_index(x, bsize) = sub_index;
800 if (bsize < BLOCK_8X8) {
801 // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
802 // there is nothing to be done.
806 set_offsets(cpi, tile, mi_row, mi_col, bsize);
807 update_state(cpi, get_block_context(x, bsize), bsize, output_enabled);
808 encode_superblock(cpi, tp, output_enabled, mi_row, mi_col, bsize);
810 if (output_enabled) {
813 (*tp)->token = EOSB_TOKEN;
818 static void encode_sb(VP9_COMP *cpi, const TileInfo *const tile,
819 TOKENEXTRA **tp, int mi_row, int mi_col,
820 int output_enabled, BLOCK_SIZE bsize) {
821 VP9_COMMON *const cm = &cpi->common;
822 MACROBLOCK *const x = &cpi->mb;
823 BLOCK_SIZE c1 = BLOCK_8X8;
824 const int bsl = b_width_log2(bsize), bs = (1 << bsl) / 4;
826 PARTITION_TYPE partition;
830 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
834 if (bsize >= BLOCK_8X8) {
835 pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
836 mi_row, mi_col, bsize);
837 c1 = *(get_sb_partitioning(x, bsize));
839 partition = partition_lookup[bsl][c1];
843 if (output_enabled && bsize >= BLOCK_8X8)
844 cpi->partition_count[pl][PARTITION_NONE]++;
845 encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, -1);
849 cpi->partition_count[pl][PARTITION_VERT]++;
850 encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, 0);
851 encode_b(cpi, tile, tp, mi_row, mi_col + bs, output_enabled, c1, 1);
855 cpi->partition_count[pl][PARTITION_HORZ]++;
856 encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, c1, 0);
857 encode_b(cpi, tile, tp, mi_row + bs, mi_col, output_enabled, c1, 1);
859 case PARTITION_SPLIT:
860 subsize = get_subsize(bsize, PARTITION_SPLIT);
863 cpi->partition_count[pl][PARTITION_SPLIT]++;
865 for (i = 0; i < 4; i++) {
866 const int x_idx = i & 1, y_idx = i >> 1;
868 *get_sb_index(x, subsize) = i;
869 encode_sb(cpi, tile, tp, mi_row + y_idx * bs, mi_col + x_idx * bs,
870 output_enabled, subsize);
878 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
879 update_partition_context(cpi->above_seg_context, cpi->left_seg_context,
880 mi_row, mi_col, c1, bsize);
883 // Check to see if the given partition size is allowed for a specified number
884 // of 8x8 block rows and columns remaining in the image.
885 // If not then return the largest allowed partition size
886 static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize,
887 int rows_left, int cols_left,
889 if ((rows_left <= 0) || (cols_left <= 0)) {
890 return MIN(bsize, BLOCK_8X8);
892 for (; bsize > 0; --bsize) {
893 *bh = num_8x8_blocks_high_lookup[bsize];
894 *bw = num_8x8_blocks_wide_lookup[bsize];
895 if ((*bh <= rows_left) && (*bw <= cols_left)) {
903 // This function attempts to set all mode info entries in a given SB64
904 // to the same block partition size.
905 // However, at the bottom and right borders of the image the requested size
906 // may not be allowed in which case this code attempts to choose the largest
907 // allowable partition.
908 static void set_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
909 MODE_INFO **mi_8x8, int mi_row, int mi_col) {
910 VP9_COMMON *const cm = &cpi->common;
911 BLOCK_SIZE bsize = cpi->sf.always_this_block_size;
912 const int mis = cm->mode_info_stride;
913 int row8x8_remaining = tile->mi_row_end - mi_row;
914 int col8x8_remaining = tile->mi_col_end - mi_col;
915 int block_row, block_col;
916 MODE_INFO * mi_upper_left = cm->mi + mi_row * mis + mi_col;
917 int bh = num_8x8_blocks_high_lookup[bsize];
918 int bw = num_8x8_blocks_wide_lookup[bsize];
920 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
922 // Apply the requested partition size to the SB64 if it is all "in image"
923 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
924 (row8x8_remaining >= MI_BLOCK_SIZE)) {
925 for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
926 for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
927 int index = block_row * mis + block_col;
928 mi_8x8[index] = mi_upper_left + index;
929 mi_8x8[index]->mbmi.sb_type = bsize;
933 // Else this is a partial SB64.
934 for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
935 for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
936 int index = block_row * mis + block_col;
937 // Find a partition size that fits
938 bsize = find_partition_size(cpi->sf.always_this_block_size,
939 (row8x8_remaining - block_row),
940 (col8x8_remaining - block_col), &bh, &bw);
941 mi_8x8[index] = mi_upper_left + index;
942 mi_8x8[index]->mbmi.sb_type = bsize;
948 static void copy_partitioning(VP9_COMP *cpi, MODE_INFO **mi_8x8,
949 MODE_INFO **prev_mi_8x8) {
950 VP9_COMMON *const cm = &cpi->common;
951 const int mis = cm->mode_info_stride;
952 int block_row, block_col;
954 for (block_row = 0; block_row < 8; ++block_row) {
955 for (block_col = 0; block_col < 8; ++block_col) {
956 MODE_INFO * prev_mi = prev_mi_8x8[block_row * mis + block_col];
957 BLOCK_SIZE sb_type = prev_mi ? prev_mi->mbmi.sb_type : 0;
961 offset = prev_mi - cm->prev_mi;
962 mi_8x8[block_row * mis + block_col] = cm->mi + offset;
963 mi_8x8[block_row * mis + block_col]->mbmi.sb_type = sb_type;
969 static int sb_has_motion(VP9_COMP *cpi, MODE_INFO **prev_mi_8x8) {
970 VP9_COMMON *const cm = &cpi->common;
971 const int mis = cm->mode_info_stride;
972 int block_row, block_col;
975 for (block_row = 0; block_row < 8; ++block_row) {
976 for (block_col = 0; block_col < 8; ++block_col) {
977 MODE_INFO * prev_mi = prev_mi_8x8[block_row * mis + block_col];
979 if (abs(prev_mi->mbmi.mv[0].as_mv.row) >= 8 ||
980 abs(prev_mi->mbmi.mv[0].as_mv.col) >= 8)
989 static void rd_use_partition(VP9_COMP *cpi,
990 const TileInfo *const tile,
992 TOKENEXTRA **tp, int mi_row, int mi_col,
993 BLOCK_SIZE bsize, int *rate, int64_t *dist,
995 VP9_COMMON *const cm = &cpi->common;
996 MACROBLOCK *const x = &cpi->mb;
997 const int mis = cm->mode_info_stride;
998 int bsl = b_width_log2(bsize);
999 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1000 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1001 int ms = num_4x4_blocks_wide / 2;
1002 int mh = num_4x4_blocks_high / 2;
1003 int bss = (1 << bsl) / 4;
1005 PARTITION_TYPE partition = PARTITION_NONE;
1007 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1008 PARTITION_CONTEXT sl[8], sa[8];
1009 int last_part_rate = INT_MAX;
1010 int64_t last_part_dist = INT_MAX;
1011 int split_rate = INT_MAX;
1012 int64_t split_dist = INT_MAX;
1013 int none_rate = INT_MAX;
1014 int64_t none_dist = INT_MAX;
1015 int chosen_rate = INT_MAX;
1016 int64_t chosen_dist = INT_MAX;
1017 BLOCK_SIZE sub_subsize = BLOCK_4X4;
1018 int splits_below = 0;
1019 BLOCK_SIZE bs_type = mi_8x8[0]->mbmi.sb_type;
1021 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1024 partition = partition_lookup[bsl][bs_type];
1026 subsize = get_subsize(bsize, partition);
1028 if (bsize < BLOCK_8X8) {
1029 // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
1030 // there is nothing to be done.
1031 if (x->ab_index != 0) {
1037 *(get_sb_partitioning(x, bsize)) = subsize;
1039 save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1041 if (bsize == BLOCK_16X16) {
1042 set_offsets(cpi, tile, mi_row, mi_col, bsize);
1043 x->mb_energy = vp9_block_energy(cpi, x, bsize);
1047 x->subblock_ref = 0;
1049 if (cpi->sf.adjust_partitioning_from_last_frame) {
1050 // Check if any of the sub blocks are further split.
1051 if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
1052 sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
1054 for (i = 0; i < 4; i++) {
1055 int jj = i >> 1, ii = i & 0x01;
1056 MODE_INFO * this_mi = mi_8x8[jj * bss * mis + ii * bss];
1057 if (this_mi && this_mi->mbmi.sb_type >= sub_subsize) {
1063 // If partition is not none try none unless each of the 4 splits are split
1065 if (partition != PARTITION_NONE && !splits_below &&
1066 mi_row + (ms >> 1) < cm->mi_rows &&
1067 mi_col + (ms >> 1) < cm->mi_cols) {
1068 *(get_sb_partitioning(x, bsize)) = bsize;
1069 pick_sb_modes(cpi, tile, mi_row, mi_col, &none_rate, &none_dist, bsize,
1070 get_block_context(x, bsize), INT64_MAX);
1072 pl = partition_plane_context(cpi->above_seg_context,
1073 cpi->left_seg_context,
1074 mi_row, mi_col, bsize);
1075 none_rate += x->partition_cost[pl][PARTITION_NONE];
1077 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1078 mi_8x8[0]->mbmi.sb_type = bs_type;
1079 *(get_sb_partitioning(x, bsize)) = subsize;
1083 switch (partition) {
1084 case PARTITION_NONE:
1085 pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
1086 bsize, get_block_context(x, bsize), INT64_MAX);
1088 case PARTITION_HORZ:
1089 *get_sb_index(x, subsize) = 0;
1090 pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
1091 subsize, get_block_context(x, subsize), INT64_MAX);
1092 if (last_part_rate != INT_MAX &&
1093 bsize >= BLOCK_8X8 && mi_row + (mh >> 1) < cm->mi_rows) {
1096 update_state(cpi, get_block_context(x, subsize), subsize, 0);
1097 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1098 *get_sb_index(x, subsize) = 1;
1099 pick_sb_modes(cpi, tile, mi_row + (ms >> 1), mi_col, &rt, &dt, subsize,
1100 get_block_context(x, subsize), INT64_MAX);
1101 if (rt == INT_MAX || dt == INT_MAX) {
1102 last_part_rate = INT_MAX;
1103 last_part_dist = INT_MAX;
1107 last_part_rate += rt;
1108 last_part_dist += dt;
1111 case PARTITION_VERT:
1112 *get_sb_index(x, subsize) = 0;
1113 pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
1114 subsize, get_block_context(x, subsize), INT64_MAX);
1115 if (last_part_rate != INT_MAX &&
1116 bsize >= BLOCK_8X8 && mi_col + (ms >> 1) < cm->mi_cols) {
1119 update_state(cpi, get_block_context(x, subsize), subsize, 0);
1120 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1121 *get_sb_index(x, subsize) = 1;
1122 pick_sb_modes(cpi, tile, mi_row, mi_col + (ms >> 1), &rt, &dt, subsize,
1123 get_block_context(x, subsize), INT64_MAX);
1124 if (rt == INT_MAX || dt == INT_MAX) {
1125 last_part_rate = INT_MAX;
1126 last_part_dist = INT_MAX;
1129 last_part_rate += rt;
1130 last_part_dist += dt;
1133 case PARTITION_SPLIT:
1137 for (i = 0; i < 4; i++) {
1138 int x_idx = (i & 1) * (ms >> 1);
1139 int y_idx = (i >> 1) * (ms >> 1);
1140 int jj = i >> 1, ii = i & 0x01;
1144 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1147 *get_sb_index(x, subsize) = i;
1149 rd_use_partition(cpi, tile, mi_8x8 + jj * bss * mis + ii * bss, tp,
1150 mi_row + y_idx, mi_col + x_idx, subsize, &rt, &dt,
1152 if (rt == INT_MAX || dt == INT_MAX) {
1153 last_part_rate = INT_MAX;
1154 last_part_dist = INT_MAX;
1157 last_part_rate += rt;
1158 last_part_dist += dt;
1165 pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
1166 mi_row, mi_col, bsize);
1167 if (last_part_rate < INT_MAX)
1168 last_part_rate += x->partition_cost[pl][partition];
1170 if (cpi->sf.adjust_partitioning_from_last_frame
1171 && partition != PARTITION_SPLIT && bsize > BLOCK_8X8
1172 && (mi_row + ms < cm->mi_rows || mi_row + (ms >> 1) == cm->mi_rows)
1173 && (mi_col + ms < cm->mi_cols || mi_col + (ms >> 1) == cm->mi_cols)) {
1174 BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
1177 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1180 for (i = 0; i < 4; i++) {
1181 int x_idx = (i & 1) * (num_4x4_blocks_wide >> 2);
1182 int y_idx = (i >> 1) * (num_4x4_blocks_wide >> 2);
1185 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1186 PARTITION_CONTEXT sl[8], sa[8];
1188 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1191 *get_sb_index(x, split_subsize) = i;
1192 *get_sb_partitioning(x, bsize) = split_subsize;
1193 *get_sb_partitioning(x, split_subsize) = split_subsize;
1195 save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1197 pick_sb_modes(cpi, tile, mi_row + y_idx, mi_col + x_idx, &rt, &dt,
1198 split_subsize, get_block_context(x, split_subsize),
1201 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1203 if (rt == INT_MAX || dt == INT_MAX) {
1204 split_rate = INT_MAX;
1205 split_dist = INT_MAX;
1210 encode_sb(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx, 0,
1215 pl = partition_plane_context(cpi->above_seg_context,
1216 cpi->left_seg_context,
1217 mi_row + y_idx, mi_col + x_idx, bsize);
1218 split_rate += x->partition_cost[pl][PARTITION_NONE];
1220 pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
1221 mi_row, mi_col, bsize);
1222 if (split_rate < INT_MAX) {
1223 split_rate += x->partition_cost[pl][PARTITION_SPLIT];
1225 chosen_rate = split_rate;
1226 chosen_dist = split_dist;
1230 // If last_part is better set the partitioning to that...
1231 if (RDCOST(x->rdmult, x->rddiv, last_part_rate, last_part_dist)
1232 < RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)) {
1233 mi_8x8[0]->mbmi.sb_type = bsize;
1234 if (bsize >= BLOCK_8X8)
1235 *(get_sb_partitioning(x, bsize)) = subsize;
1236 chosen_rate = last_part_rate;
1237 chosen_dist = last_part_dist;
1239 // If none was better set the partitioning to that...
1240 if (RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)
1241 > RDCOST(x->rdmult, x->rddiv, none_rate, none_dist)) {
1242 if (bsize >= BLOCK_8X8)
1243 *(get_sb_partitioning(x, bsize)) = bsize;
1244 chosen_rate = none_rate;
1245 chosen_dist = none_dist;
1248 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1250 // We must have chosen a partitioning and encoding or we'll fail later on.
1251 // No other opportunities for success.
1252 if ( bsize == BLOCK_64X64)
1253 assert(chosen_rate < INT_MAX && chosen_dist < INT_MAX);
1256 encode_sb(cpi, tile, tp, mi_row, mi_col, bsize == BLOCK_64X64, bsize);
1258 *rate = chosen_rate;
1259 *dist = chosen_dist;
1262 static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
1263 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
1264 BLOCK_4X4, BLOCK_4X4, BLOCK_8X8, BLOCK_8X8,
1265 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16
1268 static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
1269 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
1270 BLOCK_32X32, BLOCK_32X32, BLOCK_32X32, BLOCK_64X64,
1271 BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64
1274 // Look at all the mode_info entries for blocks that are part of this
1275 // partition and find the min and max values for sb_type.
1276 // At the moment this is designed to work on a 64x64 SB but could be
1277 // adjusted to use a size parameter.
1279 // The min and max are assumed to have been initialized prior to calling this
1280 // function so repeat calls can accumulate a min and max of more than one sb64.
1281 static void get_sb_partition_size_range(VP9_COMP *cpi, MODE_INFO ** mi_8x8,
1282 BLOCK_SIZE * min_block_size,
1283 BLOCK_SIZE * max_block_size ) {
1284 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1285 int sb_width_in_blocks = MI_BLOCK_SIZE;
1286 int sb_height_in_blocks = MI_BLOCK_SIZE;
1290 // Check the sb_type for each block that belongs to this region.
1291 for (i = 0; i < sb_height_in_blocks; ++i) {
1292 for (j = 0; j < sb_width_in_blocks; ++j) {
1293 MODE_INFO * mi = mi_8x8[index+j];
1294 BLOCK_SIZE sb_type = mi ? mi->mbmi.sb_type : 0;
1295 *min_block_size = MIN(*min_block_size, sb_type);
1296 *max_block_size = MAX(*max_block_size, sb_type);
1298 index += xd->mode_info_stride;
1302 // Look at neighboring blocks and set a min and max partition size based on
1304 static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
1306 BLOCK_SIZE *min_block_size,
1307 BLOCK_SIZE *max_block_size) {
1308 VP9_COMMON * const cm = &cpi->common;
1309 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1310 MODE_INFO ** mi_8x8 = xd->mi_8x8;
1311 MODE_INFO ** prev_mi_8x8 = xd->prev_mi_8x8;
1313 const int left_in_image = xd->left_available && mi_8x8[-1];
1314 const int above_in_image = xd->up_available &&
1315 mi_8x8[-xd->mode_info_stride];
1316 MODE_INFO ** above_sb64_mi_8x8;
1317 MODE_INFO ** left_sb64_mi_8x8;
1319 int row8x8_remaining = tile->mi_row_end - row;
1320 int col8x8_remaining = tile->mi_col_end - col;
1323 // Trap case where we do not have a prediction.
1324 if (!left_in_image && !above_in_image &&
1325 ((cm->frame_type == KEY_FRAME) || !cm->prev_mi)) {
1326 *min_block_size = BLOCK_4X4;
1327 *max_block_size = BLOCK_64X64;
1329 // Default "min to max" and "max to min"
1330 *min_block_size = BLOCK_64X64;
1331 *max_block_size = BLOCK_4X4;
1333 // NOTE: each call to get_sb_partition_size_range() uses the previous
1334 // passed in values for min and max as a starting point.
1336 // Find the min and max partition used in previous frame at this location
1337 if (cm->prev_mi && (cm->frame_type != KEY_FRAME)) {
1338 get_sb_partition_size_range(cpi, prev_mi_8x8,
1339 min_block_size, max_block_size);
1342 // Find the min and max partition sizes used in the left SB64
1343 if (left_in_image) {
1344 left_sb64_mi_8x8 = &mi_8x8[-MI_BLOCK_SIZE];
1345 get_sb_partition_size_range(cpi, left_sb64_mi_8x8,
1346 min_block_size, max_block_size);
1349 // Find the min and max partition sizes used in the above SB64.
1350 if (above_in_image) {
1351 above_sb64_mi_8x8 = &mi_8x8[-xd->mode_info_stride * MI_BLOCK_SIZE];
1352 get_sb_partition_size_range(cpi, above_sb64_mi_8x8,
1353 min_block_size, max_block_size);
1357 // Give a bit of leaway either side of the observed min and max
1358 *min_block_size = min_partition_size[*min_block_size];
1359 *max_block_size = max_partition_size[*max_block_size];
1361 // Check border cases where max and min from neighbours may not be legal.
1362 *max_block_size = find_partition_size(*max_block_size,
1363 row8x8_remaining, col8x8_remaining,
1365 *min_block_size = MIN(*min_block_size, *max_block_size);
1368 static void compute_fast_motion_search_level(VP9_COMP *cpi, BLOCK_SIZE bsize) {
1369 VP9_COMMON *const cm = &cpi->common;
1370 MACROBLOCK *const x = &cpi->mb;
1372 // Only use 8x8 result for non HD videos.
1373 // int use_8x8 = (MIN(cpi->common.width, cpi->common.height) < 720) ? 1 : 0;
1376 if (cm->frame_type && !cpi->is_src_frame_alt_ref &&
1377 ((use_8x8 && bsize == BLOCK_16X16) ||
1378 bsize == BLOCK_32X32 || bsize == BLOCK_64X64)) {
1379 int ref0 = 0, ref1 = 0, ref2 = 0, ref3 = 0;
1380 PICK_MODE_CONTEXT *block_context = NULL;
1382 if (bsize == BLOCK_16X16) {
1383 block_context = x->sb8x8_context[x->sb_index][x->mb_index];
1384 } else if (bsize == BLOCK_32X32) {
1385 block_context = x->mb_context[x->sb_index];
1386 } else if (bsize == BLOCK_64X64) {
1387 block_context = x->sb32_context;
1390 if (block_context) {
1391 ref0 = block_context[0].mic.mbmi.ref_frame[0];
1392 ref1 = block_context[1].mic.mbmi.ref_frame[0];
1393 ref2 = block_context[2].mic.mbmi.ref_frame[0];
1394 ref3 = block_context[3].mic.mbmi.ref_frame[0];
1397 // Currently, only consider 4 inter reference frames.
1398 if (ref0 && ref1 && ref2 && ref3) {
1399 int d01, d23, d02, d13;
1401 // Motion vectors for the four subblocks.
1402 int16_t mvr0 = block_context[0].mic.mbmi.mv[0].as_mv.row;
1403 int16_t mvc0 = block_context[0].mic.mbmi.mv[0].as_mv.col;
1404 int16_t mvr1 = block_context[1].mic.mbmi.mv[0].as_mv.row;
1405 int16_t mvc1 = block_context[1].mic.mbmi.mv[0].as_mv.col;
1406 int16_t mvr2 = block_context[2].mic.mbmi.mv[0].as_mv.row;
1407 int16_t mvc2 = block_context[2].mic.mbmi.mv[0].as_mv.col;
1408 int16_t mvr3 = block_context[3].mic.mbmi.mv[0].as_mv.row;
1409 int16_t mvc3 = block_context[3].mic.mbmi.mv[0].as_mv.col;
1411 // Adjust sign if ref is alt_ref.
1412 if (cm->ref_frame_sign_bias[ref0]) {
1417 if (cm->ref_frame_sign_bias[ref1]) {
1422 if (cm->ref_frame_sign_bias[ref2]) {
1427 if (cm->ref_frame_sign_bias[ref3]) {
1432 // Calculate mv distances.
1433 d01 = MAX(abs(mvr0 - mvr1), abs(mvc0 - mvc1));
1434 d23 = MAX(abs(mvr2 - mvr3), abs(mvc2 - mvc3));
1435 d02 = MAX(abs(mvr0 - mvr2), abs(mvc0 - mvc2));
1436 d13 = MAX(abs(mvr1 - mvr3), abs(mvc1 - mvc3));
1438 if (d01 < FAST_MOTION_MV_THRESH && d23 < FAST_MOTION_MV_THRESH &&
1439 d02 < FAST_MOTION_MV_THRESH && d13 < FAST_MOTION_MV_THRESH) {
1440 // Set fast motion search level.
1443 if (ref0 == ref1 && ref1 == ref2 && ref2 == ref3 &&
1444 d01 < 2 && d23 < 2 && d02 < 2 && d13 < 2) {
1445 // Set fast motion search level.
1448 if (!d01 && !d23 && !d02 && !d13) {
1450 x->subblock_ref = ref0;
1458 static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
1459 vpx_memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
1462 static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
1463 vpx_memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
1466 // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
1467 // unlikely to be selected depending on previous rate-distortion optimization
1468 // results, for encoding speed-up.
1469 static void rd_pick_partition(VP9_COMP *cpi, const TileInfo *const tile,
1470 TOKENEXTRA **tp, int mi_row,
1471 int mi_col, BLOCK_SIZE bsize, int *rate,
1472 int64_t *dist, int do_recon, int64_t best_rd) {
1473 VP9_COMMON *const cm = &cpi->common;
1474 MACROBLOCK *const x = &cpi->mb;
1475 const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
1476 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1477 PARTITION_CONTEXT sl[8], sa[8];
1478 TOKENEXTRA *tp_orig = *tp;
1481 int this_rate, sum_rate = 0, best_rate = INT_MAX;
1482 int64_t this_dist, sum_dist = 0, best_dist = INT64_MAX;
1484 int do_split = bsize >= BLOCK_8X8;
1486 // Override skipping rectangular partition operations for edge blocks
1487 const int force_horz_split = (mi_row + ms >= cm->mi_rows);
1488 const int force_vert_split = (mi_col + ms >= cm->mi_cols);
1490 int partition_none_allowed = !force_horz_split && !force_vert_split;
1491 int partition_horz_allowed = !force_vert_split && bsize >= BLOCK_8X8;
1492 int partition_vert_allowed = !force_horz_split && bsize >= BLOCK_8X8;
1494 int partition_split_done = 0;
1497 if (bsize < BLOCK_8X8) {
1498 // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
1499 // there is nothing to be done.
1500 if (x->ab_index != 0) {
1506 assert(num_8x8_blocks_wide_lookup[bsize] ==
1507 num_8x8_blocks_high_lookup[bsize]);
1509 if (bsize == BLOCK_16X16) {
1510 set_offsets(cpi, tile, mi_row, mi_col, bsize);
1511 x->mb_energy = vp9_block_energy(cpi, x, bsize);
1514 // Determine partition types in search according to the speed features.
1515 // The threshold set here has to be of square block size.
1516 if (cpi->sf.auto_min_max_partition_size) {
1517 partition_none_allowed &= (bsize <= cpi->sf.max_partition_size &&
1518 bsize >= cpi->sf.min_partition_size);
1519 partition_horz_allowed &= ((bsize <= cpi->sf.max_partition_size &&
1520 bsize > cpi->sf.min_partition_size) ||
1522 partition_vert_allowed &= ((bsize <= cpi->sf.max_partition_size &&
1523 bsize > cpi->sf.min_partition_size) ||
1525 do_split &= bsize > cpi->sf.min_partition_size;
1527 if (cpi->sf.use_square_partition_only) {
1528 partition_horz_allowed &= force_horz_split;
1529 partition_vert_allowed &= force_vert_split;
1532 save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1534 if (cpi->sf.disable_split_var_thresh && partition_none_allowed) {
1535 unsigned int source_variancey;
1536 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
1537 source_variancey = get_sby_perpixel_variance(cpi, x, bsize);
1538 if (source_variancey < cpi->sf.disable_split_var_thresh) {
1540 if (source_variancey < cpi->sf.disable_split_var_thresh / 2)
1546 if (partition_none_allowed) {
1547 pick_sb_modes(cpi, tile, mi_row, mi_col, &this_rate, &this_dist, bsize,
1548 get_block_context(x, bsize), best_rd);
1549 if (this_rate != INT_MAX) {
1550 if (bsize >= BLOCK_8X8) {
1551 pl = partition_plane_context(cpi->above_seg_context,
1552 cpi->left_seg_context,
1553 mi_row, mi_col, bsize);
1554 this_rate += x->partition_cost[pl][PARTITION_NONE];
1556 sum_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_dist);
1557 if (sum_rd < best_rd) {
1558 int64_t stop_thresh = 2048;
1560 best_rate = this_rate;
1561 best_dist = this_dist;
1563 if (bsize >= BLOCK_8X8)
1564 *(get_sb_partitioning(x, bsize)) = bsize;
1566 // Adjust threshold according to partition size.
1567 stop_thresh >>= 8 - (b_width_log2_lookup[bsize] +
1568 b_height_log2_lookup[bsize]);
1570 // If obtained distortion is very small, choose current partition
1571 // and stop splitting.
1572 if (this_dist < stop_thresh) {
1578 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1581 // store estimated motion vector
1582 if (cpi->sf.adaptive_motion_search)
1583 store_pred_mv(x, get_block_context(x, bsize));
1587 // TODO(jingning): use the motion vectors given by the above search as
1588 // the starting point of motion search in the following partition type check.
1590 subsize = get_subsize(bsize, PARTITION_SPLIT);
1591 for (i = 0; i < 4 && sum_rd < best_rd; ++i) {
1592 const int x_idx = (i & 1) * ms;
1593 const int y_idx = (i >> 1) * ms;
1595 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
1598 *get_sb_index(x, subsize) = i;
1599 if (cpi->sf.adaptive_motion_search)
1600 load_pred_mv(x, get_block_context(x, bsize));
1601 rd_pick_partition(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx, subsize,
1602 &this_rate, &this_dist, i != 3, best_rd - sum_rd);
1604 if (this_rate == INT_MAX) {
1607 sum_rate += this_rate;
1608 sum_dist += this_dist;
1609 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1612 if (sum_rd < best_rd && i == 4) {
1613 pl = partition_plane_context(cpi->above_seg_context,
1614 cpi->left_seg_context,
1615 mi_row, mi_col, bsize);
1616 sum_rate += x->partition_cost[pl][PARTITION_SPLIT];
1617 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1618 if (sum_rd < best_rd) {
1619 best_rate = sum_rate;
1620 best_dist = sum_dist;
1622 *(get_sb_partitioning(x, bsize)) = subsize;
1625 // skip rectangular partition test when larger block size
1626 // gives better rd cost
1627 if (cpi->sf.less_rectangular_check)
1628 do_rect &= !partition_none_allowed;
1630 partition_split_done = 1;
1631 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1635 x->subblock_ref = 0;
1637 if (partition_split_done &&
1638 cpi->sf.using_small_partition_info) {
1639 compute_fast_motion_search_level(cpi, bsize);
1643 if (partition_horz_allowed && do_rect) {
1644 subsize = get_subsize(bsize, PARTITION_HORZ);
1645 *get_sb_index(x, subsize) = 0;
1646 if (cpi->sf.adaptive_motion_search)
1647 load_pred_mv(x, get_block_context(x, bsize));
1648 pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
1649 get_block_context(x, subsize), best_rd);
1650 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1652 if (sum_rd < best_rd && mi_row + ms < cm->mi_rows) {
1653 update_state(cpi, get_block_context(x, subsize), subsize, 0);
1654 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1656 *get_sb_index(x, subsize) = 1;
1657 if (cpi->sf.adaptive_motion_search)
1658 load_pred_mv(x, get_block_context(x, bsize));
1659 pick_sb_modes(cpi, tile, mi_row + ms, mi_col, &this_rate,
1660 &this_dist, subsize, get_block_context(x, subsize),
1662 if (this_rate == INT_MAX) {
1665 sum_rate += this_rate;
1666 sum_dist += this_dist;
1667 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1670 if (sum_rd < best_rd) {
1671 pl = partition_plane_context(cpi->above_seg_context,
1672 cpi->left_seg_context,
1673 mi_row, mi_col, bsize);
1674 sum_rate += x->partition_cost[pl][PARTITION_HORZ];
1675 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1676 if (sum_rd < best_rd) {
1678 best_rate = sum_rate;
1679 best_dist = sum_dist;
1680 *(get_sb_partitioning(x, bsize)) = subsize;
1683 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1687 if (partition_vert_allowed && do_rect) {
1688 subsize = get_subsize(bsize, PARTITION_VERT);
1690 *get_sb_index(x, subsize) = 0;
1691 if (cpi->sf.adaptive_motion_search)
1692 load_pred_mv(x, get_block_context(x, bsize));
1693 pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
1694 get_block_context(x, subsize), best_rd);
1695 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1696 if (sum_rd < best_rd && mi_col + ms < cm->mi_cols) {
1697 update_state(cpi, get_block_context(x, subsize), subsize, 0);
1698 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1700 *get_sb_index(x, subsize) = 1;
1701 if (cpi->sf.adaptive_motion_search)
1702 load_pred_mv(x, get_block_context(x, bsize));
1703 pick_sb_modes(cpi, tile, mi_row, mi_col + ms, &this_rate,
1704 &this_dist, subsize, get_block_context(x, subsize),
1706 if (this_rate == INT_MAX) {
1709 sum_rate += this_rate;
1710 sum_dist += this_dist;
1711 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1714 if (sum_rd < best_rd) {
1715 pl = partition_plane_context(cpi->above_seg_context,
1716 cpi->left_seg_context,
1717 mi_row, mi_col, bsize);
1718 sum_rate += x->partition_cost[pl][PARTITION_VERT];
1719 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1720 if (sum_rd < best_rd) {
1721 best_rate = sum_rate;
1722 best_dist = sum_dist;
1724 *(get_sb_partitioning(x, bsize)) = subsize;
1727 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1734 if (best_rate < INT_MAX && best_dist < INT64_MAX && do_recon)
1735 encode_sb(cpi, tile, tp, mi_row, mi_col, bsize == BLOCK_64X64, bsize);
1736 if (bsize == BLOCK_64X64) {
1737 assert(tp_orig < *tp);
1738 assert(best_rate < INT_MAX);
1739 assert(best_dist < INT_MAX);
1741 assert(tp_orig == *tp);
1745 // Examines 64x64 block and chooses a best reference frame
1746 static void rd_pick_reference_frame(VP9_COMP *cpi, const TileInfo *const tile,
1747 int mi_row, int mi_col) {
1748 VP9_COMMON * const cm = &cpi->common;
1749 MACROBLOCK * const x = &cpi->mb;
1750 int bsl = b_width_log2(BLOCK_64X64), bs = 1 << bsl;
1752 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1753 PARTITION_CONTEXT sl[8], sa[8];
1758 save_context(cpi, mi_row, mi_col, a, l, sa, sl, BLOCK_64X64);
1760 // Default is non mask (all reference frames allowed.
1761 cpi->ref_frame_mask = 0;
1763 // Do RD search for 64x64.
1764 if ((mi_row + (ms >> 1) < cm->mi_rows) &&
1765 (mi_col + (ms >> 1) < cm->mi_cols)) {
1766 cpi->set_ref_frame_mask = 1;
1767 pick_sb_modes(cpi, tile, mi_row, mi_col, &r, &d, BLOCK_64X64,
1768 get_block_context(x, BLOCK_64X64), INT64_MAX);
1769 pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
1770 mi_row, mi_col, BLOCK_64X64);
1771 r += x->partition_cost[pl][PARTITION_NONE];
1773 *(get_sb_partitioning(x, BLOCK_64X64)) = BLOCK_64X64;
1774 cpi->set_ref_frame_mask = 0;
1777 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, BLOCK_64X64);
1780 static void encode_sb_row(VP9_COMP *cpi, const TileInfo *const tile,
1781 int mi_row, TOKENEXTRA **tp) {
1782 VP9_COMMON * const cm = &cpi->common;
1785 // Initialize the left context for the new SB row
1786 vpx_memset(&cpi->left_context, 0, sizeof(cpi->left_context));
1787 vpx_memset(cpi->left_seg_context, 0, sizeof(cpi->left_seg_context));
1789 // Code each SB in the row
1790 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
1791 mi_col += MI_BLOCK_SIZE) {
1795 vp9_zero(cpi->mb.pred_mv);
1797 if (cpi->sf.reference_masking)
1798 rd_pick_reference_frame(cpi, tile, mi_row, mi_col);
1800 if (cpi->sf.use_lastframe_partitioning ||
1801 cpi->sf.use_one_partition_size_always ) {
1802 const int idx_str = cm->mode_info_stride * mi_row + mi_col;
1803 MODE_INFO **mi_8x8 = cm->mi_grid_visible + idx_str;
1804 MODE_INFO **prev_mi_8x8 = cm->prev_mi_grid_visible + idx_str;
1806 cpi->mb.source_variance = UINT_MAX;
1807 if (cpi->sf.use_one_partition_size_always) {
1808 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
1809 set_partitioning(cpi, tile, mi_8x8, mi_row, mi_col);
1810 rd_use_partition(cpi, tile, mi_8x8, tp, mi_row, mi_col, BLOCK_64X64,
1811 &dummy_rate, &dummy_dist, 1);
1813 if ((cpi->common.current_video_frame
1814 % cpi->sf.last_partitioning_redo_frequency) == 0
1816 || cpi->common.show_frame == 0
1817 || cpi->common.frame_type == KEY_FRAME
1818 || cpi->is_src_frame_alt_ref
1819 || ((cpi->sf.use_lastframe_partitioning ==
1820 LAST_FRAME_PARTITION_LOW_MOTION) &&
1821 sb_has_motion(cpi, prev_mi_8x8))) {
1822 // If required set upper and lower partition size limits
1823 if (cpi->sf.auto_min_max_partition_size) {
1824 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
1825 rd_auto_partition_range(cpi, tile, mi_row, mi_col,
1826 &cpi->sf.min_partition_size,
1827 &cpi->sf.max_partition_size);
1829 rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
1830 &dummy_rate, &dummy_dist, 1, INT64_MAX);
1832 copy_partitioning(cpi, mi_8x8, prev_mi_8x8);
1833 rd_use_partition(cpi, tile, mi_8x8, tp, mi_row, mi_col, BLOCK_64X64,
1834 &dummy_rate, &dummy_dist, 1);
1838 // If required set upper and lower partition size limits
1839 if (cpi->sf.auto_min_max_partition_size) {
1840 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
1841 rd_auto_partition_range(cpi, tile, mi_row, mi_col,
1842 &cpi->sf.min_partition_size,
1843 &cpi->sf.max_partition_size);
1845 rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
1846 &dummy_rate, &dummy_dist, 1, INT64_MAX);
1851 static void init_encode_frame_mb_context(VP9_COMP *cpi) {
1852 MACROBLOCK *const x = &cpi->mb;
1853 VP9_COMMON *const cm = &cpi->common;
1854 MACROBLOCKD *const xd = &x->e_mbd;
1855 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1857 x->act_zbin_adj = 0;
1860 xd->mode_info_stride = cm->mode_info_stride;
1862 // reset intra mode contexts
1863 if (frame_is_intra_only(cm))
1864 vp9_init_mbmode_probs(cm);
1866 // Copy data over into macro block data structures.
1867 vp9_setup_src_planes(x, cpi->Source, 0, 0);
1869 // TODO(jkoleszar): are these initializations required?
1870 setup_pre_planes(xd, 0, &cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]],
1872 setup_dst_planes(xd, get_frame_new_buffer(cm), 0, 0);
1874 setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
1876 xd->mi_8x8[0]->mbmi.mode = DC_PRED;
1877 xd->mi_8x8[0]->mbmi.uv_mode = DC_PRED;
1879 vp9_zero(cpi->y_mode_count);
1880 vp9_zero(cpi->y_uv_mode_count);
1881 vp9_zero(cm->counts.inter_mode);
1882 vp9_zero(cpi->partition_count);
1883 vp9_zero(cpi->intra_inter_count);
1884 vp9_zero(cpi->comp_inter_count);
1885 vp9_zero(cpi->single_ref_count);
1886 vp9_zero(cpi->comp_ref_count);
1887 vp9_zero(cm->counts.tx);
1888 vp9_zero(cm->counts.mbskip);
1890 // Note: this memset assumes above_context[0], [1] and [2]
1891 // are allocated as part of the same buffer.
1892 vpx_memset(cpi->above_context[0], 0,
1893 sizeof(*cpi->above_context[0]) *
1894 2 * aligned_mi_cols * MAX_MB_PLANE);
1895 vpx_memset(cpi->above_seg_context, 0,
1896 sizeof(*cpi->above_seg_context) * aligned_mi_cols);
1899 static void switch_lossless_mode(VP9_COMP *cpi, int lossless) {
1901 // printf("Switching to lossless\n");
1902 cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
1903 cpi->mb.e_mbd.itxm_add = vp9_iwht4x4_add;
1904 cpi->mb.optimize = 0;
1905 cpi->common.lf.filter_level = 0;
1906 cpi->zbin_mode_boost_enabled = 0;
1907 cpi->common.tx_mode = ONLY_4X4;
1909 // printf("Not lossless\n");
1910 cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
1911 cpi->mb.e_mbd.itxm_add = vp9_idct4x4_add;
1915 static void switch_tx_mode(VP9_COMP *cpi) {
1916 if (cpi->sf.tx_size_search_method == USE_LARGESTALL &&
1917 cpi->common.tx_mode >= ALLOW_32X32)
1918 cpi->common.tx_mode = ALLOW_32X32;
1921 static void encode_frame_internal(VP9_COMP *cpi) {
1923 MACROBLOCK * const x = &cpi->mb;
1924 VP9_COMMON * const cm = &cpi->common;
1925 MACROBLOCKD * const xd = &x->e_mbd;
1927 // fprintf(stderr, "encode_frame_internal frame %d (%d) type %d\n",
1928 // cpi->common.current_video_frame, cpi->common.show_frame,
1935 statsfile = fopen("segmap2.stt", "a");
1936 fprintf(statsfile, "\n");
1941 vp9_zero(cm->counts.switchable_interp);
1942 vp9_zero(cpi->tx_stepdown_count);
1944 xd->mi_8x8 = cm->mi_grid_visible;
1945 // required for vp9_frame_init_quantizer
1946 xd->mi_8x8[0] = cm->mi;
1948 xd->last_mi = cm->prev_mi;
1950 vp9_zero(cpi->NMVcount);
1951 vp9_zero(cpi->coef_counts);
1952 vp9_zero(cm->counts.eob_branch);
1954 cpi->mb.e_mbd.lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0
1955 && cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
1956 switch_lossless_mode(cpi, cpi->mb.e_mbd.lossless);
1958 vp9_frame_init_quantizer(cpi);
1960 vp9_initialize_rd_consts(cpi);
1961 vp9_initialize_me_consts(cpi, cm->base_qindex);
1962 switch_tx_mode(cpi);
1964 if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
1965 // Initialize encode frame context.
1966 init_encode_frame_mb_context(cpi);
1968 // Build a frame level activity map
1969 build_activity_map(cpi);
1972 // Re-initialize encode frame context.
1973 init_encode_frame_mb_context(cpi);
1975 vp9_zero(cpi->rd_comp_pred_diff);
1976 vp9_zero(cpi->rd_filter_diff);
1977 vp9_zero(cpi->rd_tx_select_diff);
1978 vp9_zero(cpi->rd_tx_select_threshes);
1983 struct vpx_usec_timer emr_timer;
1984 vpx_usec_timer_start(&emr_timer);
1987 // Take tiles into account and give start/end MB
1988 int tile_col, tile_row;
1989 TOKENEXTRA *tp = cpi->tok;
1990 const int tile_cols = 1 << cm->log2_tile_cols;
1991 const int tile_rows = 1 << cm->log2_tile_rows;
1993 for (tile_row = 0; tile_row < tile_rows; tile_row++) {
1994 for (tile_col = 0; tile_col < tile_cols; tile_col++) {
1996 TOKENEXTRA *tp_old = tp;
1998 // For each row of SBs in the frame
1999 vp9_tile_init(&tile, cm, tile_row, tile_col);
2000 for (mi_row = tile.mi_row_start;
2001 mi_row < tile.mi_row_end; mi_row += 8)
2002 encode_sb_row(cpi, &tile, mi_row, &tp);
2004 cpi->tok_count[tile_row][tile_col] = (unsigned int)(tp - tp_old);
2005 assert(tp - cpi->tok <= get_token_alloc(cm->mb_rows, cm->mb_cols));
2010 vpx_usec_timer_mark(&emr_timer);
2011 cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
2014 if (cpi->sf.skip_encode_sb) {
2016 unsigned int intra_count = 0, inter_count = 0;
2017 for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
2018 intra_count += cpi->intra_inter_count[j][0];
2019 inter_count += cpi->intra_inter_count[j][1];
2021 cpi->sf.skip_encode_frame = ((intra_count << 2) < inter_count);
2022 cpi->sf.skip_encode_frame &= (cm->frame_type != KEY_FRAME);
2023 cpi->sf.skip_encode_frame &= cm->show_frame;
2025 cpi->sf.skip_encode_frame = 0;
2029 // Keep record of the total distortion this time around for future use
2030 cpi->last_frame_distortion = cpi->frame_distortion;
2034 static int check_dual_ref_flags(VP9_COMP *cpi) {
2035 const int ref_flags = cpi->ref_frame_flags;
2037 if (vp9_segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
2040 return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG)
2041 + !!(ref_flags & VP9_ALT_FLAG)) >= 2;
2045 static int get_skip_flag(MODE_INFO **mi_8x8, int mis, int ymbs, int xmbs) {
2048 for (y = 0; y < ymbs; y++) {
2049 for (x = 0; x < xmbs; x++) {
2050 if (!mi_8x8[y * mis + x]->mbmi.skip_coeff)
2058 static void set_txfm_flag(MODE_INFO **mi_8x8, int mis, int ymbs, int xmbs,
2062 for (y = 0; y < ymbs; y++) {
2063 for (x = 0; x < xmbs; x++)
2064 mi_8x8[y * mis + x]->mbmi.tx_size = tx_size;
2068 static void reset_skip_txfm_size_b(VP9_COMP *cpi, MODE_INFO **mi_8x8,
2069 int mis, TX_SIZE max_tx_size, int bw, int bh,
2070 int mi_row, int mi_col, BLOCK_SIZE bsize) {
2071 VP9_COMMON * const cm = &cpi->common;
2073 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) {
2076 MB_MODE_INFO * const mbmi = &mi_8x8[0]->mbmi;
2077 if (mbmi->tx_size > max_tx_size) {
2078 const int ymbs = MIN(bh, cm->mi_rows - mi_row);
2079 const int xmbs = MIN(bw, cm->mi_cols - mi_col);
2081 assert(vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
2082 get_skip_flag(mi_8x8, mis, ymbs, xmbs));
2083 set_txfm_flag(mi_8x8, mis, ymbs, xmbs, max_tx_size);
2088 static void reset_skip_txfm_size_sb(VP9_COMP *cpi, MODE_INFO **mi_8x8,
2089 TX_SIZE max_tx_size, int mi_row, int mi_col,
2091 VP9_COMMON * const cm = &cpi->common;
2092 const int mis = cm->mode_info_stride;
2094 const int bs = num_8x8_blocks_wide_lookup[bsize], hbs = bs / 2;
2096 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
2099 bw = num_8x8_blocks_wide_lookup[mi_8x8[0]->mbmi.sb_type];
2100 bh = num_8x8_blocks_high_lookup[mi_8x8[0]->mbmi.sb_type];
2102 if (bw == bs && bh == bs) {
2103 reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, bs, bs, mi_row,
2105 } else if (bw == bs && bh < bs) {
2106 reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, bs, hbs, mi_row,
2108 reset_skip_txfm_size_b(cpi, mi_8x8 + hbs * mis, mis, max_tx_size, bs, hbs,
2109 mi_row + hbs, mi_col, bsize);
2110 } else if (bw < bs && bh == bs) {
2111 reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, hbs, bs, mi_row,
2113 reset_skip_txfm_size_b(cpi, mi_8x8 + hbs, mis, max_tx_size, hbs, bs, mi_row,
2114 mi_col + hbs, bsize);
2117 const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize];
2120 assert(bw < bs && bh < bs);
2122 for (n = 0; n < 4; n++) {
2123 const int mi_dc = hbs * (n & 1);
2124 const int mi_dr = hbs * (n >> 1);
2126 reset_skip_txfm_size_sb(cpi, &mi_8x8[mi_dr * mis + mi_dc], max_tx_size,
2127 mi_row + mi_dr, mi_col + mi_dc, subsize);
2132 static void reset_skip_txfm_size(VP9_COMP *cpi, TX_SIZE txfm_max) {
2133 VP9_COMMON * const cm = &cpi->common;
2135 const int mis = cm->mode_info_stride;
2136 // MODE_INFO *mi, *mi_ptr = cm->mi;
2137 MODE_INFO **mi_8x8, **mi_ptr = cm->mi_grid_visible;
2139 for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8, mi_ptr += 8 * mis) {
2141 for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 8, mi_8x8 += 8) {
2142 reset_skip_txfm_size_sb(cpi, mi_8x8, txfm_max, mi_row, mi_col,
2148 static int get_frame_type(VP9_COMP *cpi) {
2150 if (frame_is_intra_only(&cpi->common))
2152 else if (cpi->is_src_frame_alt_ref && cpi->refresh_golden_frame)
2154 else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
2161 static void select_tx_mode(VP9_COMP *cpi) {
2162 if (cpi->oxcf.lossless) {
2163 cpi->common.tx_mode = ONLY_4X4;
2164 } else if (cpi->common.current_video_frame == 0) {
2165 cpi->common.tx_mode = TX_MODE_SELECT;
2167 if (cpi->sf.tx_size_search_method == USE_LARGESTALL) {
2168 cpi->common.tx_mode = ALLOW_32X32;
2169 } else if (cpi->sf.tx_size_search_method == USE_FULL_RD) {
2170 int frame_type = get_frame_type(cpi);
2171 cpi->common.tx_mode =
2172 cpi->rd_tx_select_threshes[frame_type][ALLOW_32X32]
2173 > cpi->rd_tx_select_threshes[frame_type][TX_MODE_SELECT] ?
2174 ALLOW_32X32 : TX_MODE_SELECT;
2176 unsigned int total = 0;
2178 for (i = 0; i < TX_SIZES; ++i)
2179 total += cpi->tx_stepdown_count[i];
2181 double fraction = (double)cpi->tx_stepdown_count[0] / total;
2182 cpi->common.tx_mode = fraction > 0.90 ? ALLOW_32X32 : TX_MODE_SELECT;
2183 // printf("fraction = %f\n", fraction);
2184 } // else keep unchanged
2189 void vp9_encode_frame(VP9_COMP *cpi) {
2190 VP9_COMMON * const cm = &cpi->common;
2192 // In the longer term the encoder should be generalized to match the
2193 // decoder such that we allow compound where one of the 3 buffers has a
2194 // different sign bias and that buffer is then the fixed ref. However, this
2195 // requires further work in the rd loop. For now the only supported encoder
2196 // side behavior is where the ALT ref buffer has opposite sign bias to
2198 if (!frame_is_intra_only(cm)) {
2199 if ((cm->ref_frame_sign_bias[ALTREF_FRAME]
2200 == cm->ref_frame_sign_bias[GOLDEN_FRAME])
2201 || (cm->ref_frame_sign_bias[ALTREF_FRAME]
2202 == cm->ref_frame_sign_bias[LAST_FRAME])) {
2203 cm->allow_comp_inter_inter = 0;
2205 cm->allow_comp_inter_inter = 1;
2206 cm->comp_fixed_ref = ALTREF_FRAME;
2207 cm->comp_var_ref[0] = LAST_FRAME;
2208 cm->comp_var_ref[1] = GOLDEN_FRAME;
2214 INTERPOLATION_TYPE filter_type;
2216 * This code does a single RD pass over the whole frame assuming
2217 * either compound, single or hybrid prediction as per whatever has
2218 * worked best for that type of frame in the past.
2219 * It also predicts whether another coding mode would have worked
2220 * better that this coding mode. If that is the case, it remembers
2221 * that for subsequent frames.
2222 * It does the same analysis for transform size selection also.
2224 int frame_type = get_frame_type(cpi);
2226 /* prediction (compound, single or hybrid) mode selection */
2227 if (frame_type == 3 || !cm->allow_comp_inter_inter)
2228 pred_type = SINGLE_PREDICTION_ONLY;
2229 else if (cpi->rd_prediction_type_threshes[frame_type][1]
2230 > cpi->rd_prediction_type_threshes[frame_type][0]
2231 && cpi->rd_prediction_type_threshes[frame_type][1]
2232 > cpi->rd_prediction_type_threshes[frame_type][2]
2233 && check_dual_ref_flags(cpi) && cpi->static_mb_pct == 100)
2234 pred_type = COMP_PREDICTION_ONLY;
2235 else if (cpi->rd_prediction_type_threshes[frame_type][0]
2236 > cpi->rd_prediction_type_threshes[frame_type][2])
2237 pred_type = SINGLE_PREDICTION_ONLY;
2239 pred_type = HYBRID_PREDICTION;
2241 /* filter type selection */
2242 // FIXME(rbultje) for some odd reason, we often select smooth_filter
2243 // as default filter for ARF overlay frames. This is a REALLY BAD
2244 // IDEA so we explicitly disable it here.
2245 if (frame_type != 3 &&
2246 cpi->rd_filter_threshes[frame_type][1] >
2247 cpi->rd_filter_threshes[frame_type][0] &&
2248 cpi->rd_filter_threshes[frame_type][1] >
2249 cpi->rd_filter_threshes[frame_type][2] &&
2250 cpi->rd_filter_threshes[frame_type][1] >
2251 cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) {
2252 filter_type = EIGHTTAP_SMOOTH;
2253 } else if (cpi->rd_filter_threshes[frame_type][2] >
2254 cpi->rd_filter_threshes[frame_type][0] &&
2255 cpi->rd_filter_threshes[frame_type][2] >
2256 cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) {
2257 filter_type = EIGHTTAP_SHARP;
2258 } else if (cpi->rd_filter_threshes[frame_type][0] >
2259 cpi->rd_filter_threshes[frame_type][SWITCHABLE_FILTERS]) {
2260 filter_type = EIGHTTAP;
2262 filter_type = SWITCHABLE;
2265 cpi->mb.e_mbd.lossless = 0;
2266 if (cpi->oxcf.lossless) {
2267 cpi->mb.e_mbd.lossless = 1;
2270 /* transform size selection (4x4, 8x8, 16x16 or select-per-mb) */
2271 select_tx_mode(cpi);
2272 cpi->common.comp_pred_mode = pred_type;
2273 cpi->common.mcomp_filter_type = filter_type;
2274 encode_frame_internal(cpi);
2276 for (i = 0; i < NB_PREDICTION_TYPES; ++i) {
2277 const int diff = (int) (cpi->rd_comp_pred_diff[i] / cpi->common.MBs);
2278 cpi->rd_prediction_type_threshes[frame_type][i] += diff;
2279 cpi->rd_prediction_type_threshes[frame_type][i] >>= 1;
2282 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
2283 const int64_t diff = cpi->rd_filter_diff[i] / cpi->common.MBs;
2284 cpi->rd_filter_threshes[frame_type][i] =
2285 (cpi->rd_filter_threshes[frame_type][i] + diff) / 2;
2288 for (i = 0; i < TX_MODES; ++i) {
2289 int64_t pd = cpi->rd_tx_select_diff[i];
2291 if (i == TX_MODE_SELECT)
2292 pd -= RDCOST(cpi->mb.rdmult, cpi->mb.rddiv,
2293 2048 * (TX_SIZES - 1), 0);
2294 diff = (int) (pd / cpi->common.MBs);
2295 cpi->rd_tx_select_threshes[frame_type][i] += diff;
2296 cpi->rd_tx_select_threshes[frame_type][i] /= 2;
2299 if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) {
2300 int single_count_zero = 0;
2301 int comp_count_zero = 0;
2303 for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
2304 single_count_zero += cpi->comp_inter_count[i][0];
2305 comp_count_zero += cpi->comp_inter_count[i][1];
2308 if (comp_count_zero == 0) {
2309 cpi->common.comp_pred_mode = SINGLE_PREDICTION_ONLY;
2310 vp9_zero(cpi->comp_inter_count);
2311 } else if (single_count_zero == 0) {
2312 cpi->common.comp_pred_mode = COMP_PREDICTION_ONLY;
2313 vp9_zero(cpi->comp_inter_count);
2317 if (cpi->common.tx_mode == TX_MODE_SELECT) {
2319 int count8x8_lp = 0, count8x8_8x8p = 0;
2320 int count16x16_16x16p = 0, count16x16_lp = 0;
2323 for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
2324 count4x4 += cm->counts.tx.p32x32[i][TX_4X4];
2325 count4x4 += cm->counts.tx.p16x16[i][TX_4X4];
2326 count4x4 += cm->counts.tx.p8x8[i][TX_4X4];
2328 count8x8_lp += cm->counts.tx.p32x32[i][TX_8X8];
2329 count8x8_lp += cm->counts.tx.p16x16[i][TX_8X8];
2330 count8x8_8x8p += cm->counts.tx.p8x8[i][TX_8X8];
2332 count16x16_16x16p += cm->counts.tx.p16x16[i][TX_16X16];
2333 count16x16_lp += cm->counts.tx.p32x32[i][TX_16X16];
2334 count32x32 += cm->counts.tx.p32x32[i][TX_32X32];
2337 if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0
2338 && count32x32 == 0) {
2339 cpi->common.tx_mode = ALLOW_8X8;
2340 reset_skip_txfm_size(cpi, TX_8X8);
2341 } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0
2342 && count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
2343 cpi->common.tx_mode = ONLY_4X4;
2344 reset_skip_txfm_size(cpi, TX_4X4);
2345 } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
2346 cpi->common.tx_mode = ALLOW_32X32;
2347 } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
2348 cpi->common.tx_mode = ALLOW_16X16;
2349 reset_skip_txfm_size(cpi, TX_16X16);
2353 encode_frame_internal(cpi);
2357 static void sum_intra_stats(VP9_COMP *cpi, const MODE_INFO *mi) {
2358 const MB_PREDICTION_MODE y_mode = mi->mbmi.mode;
2359 const MB_PREDICTION_MODE uv_mode = mi->mbmi.uv_mode;
2360 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
2362 ++cpi->y_uv_mode_count[y_mode][uv_mode];
2364 if (bsize < BLOCK_8X8) {
2366 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
2367 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
2368 for (idy = 0; idy < 2; idy += num_4x4_blocks_high)
2369 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide)
2370 ++cpi->y_mode_count[0][mi->bmi[idy * 2 + idx].as_mode];
2372 ++cpi->y_mode_count[size_group_lookup[bsize]][y_mode];
2376 // Experimental stub function to create a per MB zbin adjustment based on
2377 // some previously calculated measure of MB activity.
2378 static void adjust_act_zbin(VP9_COMP *cpi, MACROBLOCK *x) {
2380 x->act_zbin_adj = *(x->mb_activity_ptr);
2384 int64_t act = *(x->mb_activity_ptr);
2386 // Apply the masking to the RD multiplier.
2387 a = act + 4 * cpi->activity_avg;
2388 b = 4 * act + cpi->activity_avg;
2390 if (act > cpi->activity_avg)
2391 x->act_zbin_adj = (int) (((int64_t) b + (a >> 1)) / a) - 1;
2393 x->act_zbin_adj = 1 - (int) (((int64_t) a + (b >> 1)) / b);
2396 static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled,
2397 int mi_row, int mi_col, BLOCK_SIZE bsize) {
2398 VP9_COMMON * const cm = &cpi->common;
2399 MACROBLOCK * const x = &cpi->mb;
2400 MACROBLOCKD * const xd = &x->e_mbd;
2401 MODE_INFO **mi_8x8 = xd->mi_8x8;
2402 MODE_INFO *mi = mi_8x8[0];
2403 MB_MODE_INFO *mbmi = &mi->mbmi;
2404 PICK_MODE_CONTEXT *ctx = get_block_context(x, bsize);
2405 unsigned int segment_id = mbmi->segment_id;
2406 const int mis = cm->mode_info_stride;
2407 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
2408 const int mi_height = num_8x8_blocks_high_lookup[bsize];
2409 x->skip_optimize = ctx->is_coded;
2411 x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
2412 x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
2413 xd->q_index < QIDX_SKIP_THRESH);
2417 if (cm->frame_type == KEY_FRAME) {
2418 if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
2419 adjust_act_zbin(cpi, x);
2420 vp9_update_zbin_extra(cpi, x);
2423 vp9_setup_interp_filters(xd, mbmi->interp_filter, cm);
2425 if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
2426 // Adjust the zbin based on this MB rate.
2427 adjust_act_zbin(cpi, x);
2430 // Experimental code. Special case for gf and arf zeromv modes.
2431 // Increase zbin size to suppress noise
2432 cpi->zbin_mode_boost = 0;
2433 if (cpi->zbin_mode_boost_enabled) {
2434 if (is_inter_block(mbmi)) {
2435 if (mbmi->mode == ZEROMV) {
2436 if (mbmi->ref_frame[0] != LAST_FRAME)
2437 cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
2439 cpi->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
2440 } else if (mbmi->sb_type < BLOCK_8X8) {
2441 cpi->zbin_mode_boost = SPLIT_MV_ZBIN_BOOST;
2443 cpi->zbin_mode_boost = MV_ZBIN_BOOST;
2446 cpi->zbin_mode_boost = INTRA_ZBIN_BOOST;
2450 vp9_update_zbin_extra(cpi, x);
2453 if (!is_inter_block(mbmi)) {
2454 vp9_encode_intra_block_y(x, MAX(bsize, BLOCK_8X8));
2455 vp9_encode_intra_block_uv(x, MAX(bsize, BLOCK_8X8));
2457 sum_intra_stats(cpi, mi);
2459 int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, mbmi->ref_frame[0])];
2460 YV12_BUFFER_CONFIG *ref_fb = &cm->yv12_fb[idx];
2461 YV12_BUFFER_CONFIG *second_ref_fb = NULL;
2462 if (has_second_ref(mbmi)) {
2463 idx = cm->ref_frame_map[get_ref_frame_idx(cpi, mbmi->ref_frame[1])];
2464 second_ref_fb = &cm->yv12_fb[idx];
2467 assert(cm->frame_type != KEY_FRAME);
2469 setup_pre_planes(xd, 0, ref_fb, mi_row, mi_col,
2470 &xd->scale_factor[0]);
2471 setup_pre_planes(xd, 1, second_ref_fb, mi_row, mi_col,
2472 &xd->scale_factor[1]);
2474 vp9_build_inter_predictors_sb(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8));
2477 if (!is_inter_block(mbmi)) {
2478 vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8));
2479 } else if (!x->skip) {
2480 vp9_encode_sb(x, MAX(bsize, BLOCK_8X8));
2481 vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8));
2483 int mb_skip_context = xd->left_available ? mi_8x8[-1]->mbmi.skip_coeff : 0;
2484 mb_skip_context += mi_8x8[-mis] ? mi_8x8[-mis]->mbmi.skip_coeff : 0;
2486 mbmi->skip_coeff = 1;
2488 cm->counts.mbskip[mb_skip_context][1]++;
2489 reset_skip_context(xd, MAX(bsize, BLOCK_8X8));
2492 if (output_enabled) {
2493 if (cm->tx_mode == TX_MODE_SELECT &&
2494 mbmi->sb_type >= BLOCK_8X8 &&
2495 !(is_inter_block(mbmi) &&
2496 (mbmi->skip_coeff ||
2497 vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)))) {
2498 const uint8_t context = vp9_get_pred_context_tx_size(xd);
2499 ++get_tx_counts(max_txsize_lookup[bsize],
2500 context, &cm->counts.tx)[mbmi->tx_size];
2503 TX_SIZE sz = tx_mode_to_biggest_tx_size[cm->tx_mode];
2504 assert(sizeof(tx_mode_to_biggest_tx_size) /
2505 sizeof(tx_mode_to_biggest_tx_size[0]) == TX_MODES);
2506 // The new intra coding scheme requires no change of transform size
2507 if (is_inter_block(&mi->mbmi)) {
2508 if (sz == TX_32X32 && bsize < BLOCK_32X32)
2510 if (sz == TX_16X16 && bsize < BLOCK_16X16)
2512 if (sz == TX_8X8 && bsize < BLOCK_8X8)
2514 } else if (bsize >= BLOCK_8X8) {
2520 for (y = 0; y < mi_height; y++)
2521 for (x = 0; x < mi_width; x++)
2522 if (mi_col + x < cm->mi_cols && mi_row + y < cm->mi_rows)
2523 mi_8x8[mis * y + x]->mbmi.tx_size = sz;