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_idct.h"
24 #include "vp9/common/vp9_mvref_common.h"
25 #include "vp9/common/vp9_pred_common.h"
26 #include "vp9/common/vp9_quant_common.h"
27 #include "vp9/common/vp9_reconintra.h"
28 #include "vp9/common/vp9_reconinter.h"
29 #include "vp9/common/vp9_seg_common.h"
30 #include "vp9/common/vp9_tile_common.h"
31 #include "vp9/encoder/vp9_encodeframe.h"
32 #include "vp9/encoder/vp9_encodemb.h"
33 #include "vp9/encoder/vp9_encodemv.h"
34 #include "vp9/encoder/vp9_extend.h"
35 #include "vp9/encoder/vp9_onyx_int.h"
36 #include "vp9/encoder/vp9_rdopt.h"
37 #include "vp9/encoder/vp9_segmentation.h"
38 #include "vp9/common/vp9_systemdependent.h"
39 #include "vp9/encoder/vp9_tokenize.h"
40 #include "vp9/encoder/vp9_vaq.h"
43 #define DBG_PRNT_SEGMAP 0
51 static INLINE uint8_t *get_sb_index(MACROBLOCK *x, BLOCK_SIZE subsize) {
76 static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled,
77 int mi_row, int mi_col, BLOCK_SIZE bsize);
79 static void adjust_act_zbin(VP9_COMP *cpi, MACROBLOCK *x);
81 /* activity_avg must be positive, or flat regions could get a zero weight
82 * (infinite lambda), which confounds analysis.
83 * This also avoids the need for divide by zero checks in
84 * vp9_activity_masking().
86 #define ACTIVITY_AVG_MIN (64)
88 /* Motion vector component magnitude threshold for defining fast motion. */
89 #define FAST_MOTION_MV_THRESH (24)
91 /* This is used as a reference when computing the source variance for the
92 * purposes of activity masking.
93 * Eventually this should be replaced by custom no-reference routines,
94 * which will be faster.
96 static const uint8_t VP9_VAR_OFFS[64] = {
97 128, 128, 128, 128, 128, 128, 128, 128,
98 128, 128, 128, 128, 128, 128, 128, 128,
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
107 static unsigned int get_sby_perpixel_variance(VP9_COMP *cpi, MACROBLOCK *x,
109 unsigned int var, sse;
110 var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf,
111 x->plane[0].src.stride,
112 VP9_VAR_OFFS, 0, &sse);
113 return (var + (1 << (num_pels_log2_lookup[bs] - 1))) >>
114 num_pels_log2_lookup[bs];
117 // Original activity measure from Tim T's code.
118 static unsigned int tt_activity_measure(MACROBLOCK *x) {
121 /* TODO: This could also be done over smaller areas (8x8), but that would
122 * require extensive changes elsewhere, as lambda is assumed to be fixed
123 * over an entire MB in most of the code.
124 * Another option is to compute four 8x8 variances, and pick a single
125 * lambda using a non-linear combination (e.g., the smallest, or second
128 act = vp9_variance16x16(x->plane[0].src.buf, x->plane[0].src.stride,
129 VP9_VAR_OFFS, 0, &sse);
132 /* If the region is flat, lower the activity some more. */
134 act = act < 5 << 12 ? act : 5 << 12;
139 // Stub for alternative experimental activity measures.
140 static unsigned int alt_activity_measure(MACROBLOCK *x, int use_dc_pred) {
141 return vp9_encode_intra(x, use_dc_pred);
144 // Measure the activity of the current macroblock
145 // What we measure here is TBD so abstracted to this function
146 #define ALT_ACT_MEASURE 1
147 static unsigned int mb_activity_measure(MACROBLOCK *x, int mb_row, int mb_col) {
148 unsigned int mb_activity;
150 if (ALT_ACT_MEASURE) {
151 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
153 // Or use and alternative.
154 mb_activity = alt_activity_measure(x, use_dc_pred);
156 // Original activity measure from Tim T's code.
157 mb_activity = tt_activity_measure(x);
160 if (mb_activity < ACTIVITY_AVG_MIN)
161 mb_activity = ACTIVITY_AVG_MIN;
166 // Calculate an "average" mb activity value for the frame
168 static void calc_av_activity(VP9_COMP *cpi, int64_t activity_sum) {
170 // Find median: Simple n^2 algorithm for experimentation
174 unsigned int *sortlist;
177 // Create a list to sort to
178 CHECK_MEM_ERROR(&cpi->common, sortlist, vpx_calloc(sizeof(unsigned int),
181 // Copy map to sort list
182 vpx_memcpy(sortlist, cpi->mb_activity_map,
183 sizeof(unsigned int) * cpi->common.MBs);
185 // Ripple each value down to its correct position
186 for (i = 1; i < cpi->common.MBs; i ++) {
187 for (j = i; j > 0; j --) {
188 if (sortlist[j] < sortlist[j - 1]) {
190 tmp = sortlist[j - 1];
191 sortlist[j - 1] = sortlist[j];
199 // Even number MBs so estimate median as mean of two either side.
200 median = (1 + sortlist[cpi->common.MBs >> 1] +
201 sortlist[(cpi->common.MBs >> 1) + 1]) >> 1;
203 cpi->activity_avg = median;
208 // Simple mean for now
209 cpi->activity_avg = (unsigned int) (activity_sum / cpi->common.MBs);
212 if (cpi->activity_avg < ACTIVITY_AVG_MIN)
213 cpi->activity_avg = ACTIVITY_AVG_MIN;
215 // Experimental code: return fixed value normalized for several clips
217 cpi->activity_avg = 100000;
220 #define USE_ACT_INDEX 0
221 #define OUTPUT_NORM_ACT_STATS 0
224 // Calculate an activity index for each mb
225 static void calc_activity_index(VP9_COMP *cpi, MACROBLOCK *x) {
226 VP9_COMMON *const cm = &cpi->common;
233 #if OUTPUT_NORM_ACT_STATS
234 FILE *f = fopen("norm_act.stt", "a");
235 fprintf(f, "\n%12d\n", cpi->activity_avg);
238 // Reset pointers to start of activity map
239 x->mb_activity_ptr = cpi->mb_activity_map;
241 // Calculate normalized mb activity number.
242 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
243 // for each macroblock col in image
244 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
245 // Read activity from the map
246 act = *(x->mb_activity_ptr);
248 // Calculate a normalized activity number
249 a = act + 4 * cpi->activity_avg;
250 b = 4 * act + cpi->activity_avg;
253 *(x->activity_ptr) = (int)((b + (a >> 1)) / a) - 1;
255 *(x->activity_ptr) = 1 - (int)((a + (b >> 1)) / b);
257 #if OUTPUT_NORM_ACT_STATS
258 fprintf(f, " %6d", *(x->mb_activity_ptr));
260 // Increment activity map pointers
261 x->mb_activity_ptr++;
264 #if OUTPUT_NORM_ACT_STATS
269 #if OUTPUT_NORM_ACT_STATS
273 #endif // USE_ACT_INDEX
275 // Loop through all MBs. Note activity of each, average activity and
276 // calculate a normalized activity for each
277 static void build_activity_map(VP9_COMP *cpi) {
278 MACROBLOCK * const x = &cpi->mb;
279 MACROBLOCKD *xd = &x->e_mbd;
280 VP9_COMMON * const cm = &cpi->common;
283 YV12_BUFFER_CONFIG *new_yv12 = get_frame_new_buffer(cm);
285 int recon_y_stride = new_yv12->y_stride;
289 unsigned int mb_activity;
290 int64_t activity_sum = 0;
292 x->mb_activity_ptr = cpi->mb_activity_map;
294 // for each macroblock row in image
295 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
297 // reset above block coeffs
298 xd->up_available = (mb_row != 0);
299 recon_yoffset = (mb_row * recon_y_stride * 16);
301 // for each macroblock col in image
302 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
304 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
305 xd->left_available = (mb_col != 0);
310 mb_activity = mb_activity_measure(x, mb_row, mb_col);
313 activity_sum += mb_activity;
315 // Store MB level activity details.
316 *x->mb_activity_ptr = mb_activity;
318 // Increment activity map pointer
319 x->mb_activity_ptr++;
321 // adjust to the next column of source macroblocks
322 x->plane[0].src.buf += 16;
325 // adjust to the next row of mbs
326 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
329 // Calculate an "average" MB activity
330 calc_av_activity(cpi, activity_sum);
333 // Calculate an activity index number of each mb
334 calc_activity_index(cpi, x);
338 // Macroblock activity masking
339 void vp9_activity_masking(VP9_COMP *cpi, MACROBLOCK *x) {
341 x->rdmult += *(x->mb_activity_ptr) * (x->rdmult >> 2);
342 x->errorperbit = x->rdmult * 100 / (110 * x->rddiv);
343 x->errorperbit += (x->errorperbit == 0);
347 int64_t act = *(x->mb_activity_ptr);
349 // Apply the masking to the RD multiplier.
350 a = act + (2 * cpi->activity_avg);
351 b = (2 * act) + cpi->activity_avg;
353 x->rdmult = (unsigned int) (((int64_t) x->rdmult * b + (a >> 1)) / a);
354 x->errorperbit = x->rdmult * 100 / (110 * x->rddiv);
355 x->errorperbit += (x->errorperbit == 0);
358 // Activity based Zbin adjustment
359 adjust_act_zbin(cpi, x);
362 // Select a segment for the current SB64
363 static void select_in_frame_q_segment(VP9_COMP *cpi,
364 int mi_row, int mi_col,
365 int output_enabled, int projected_rate) {
366 VP9_COMMON * const cm = &cpi->common;
367 int target_rate = cpi->rc.sb64_target_rate << 8; // convert to bits << 8
369 const int mi_offset = mi_row * cm->mi_cols + mi_col;
370 const int bw = num_8x8_blocks_wide_lookup[BLOCK_64X64];
371 const int bh = num_8x8_blocks_high_lookup[BLOCK_64X64];
372 const int xmis = MIN(cm->mi_cols - mi_col, bw);
373 const int ymis = MIN(cm->mi_rows - mi_row, bh);
374 int complexity_metric = 64;
377 unsigned char segment;
379 if (!output_enabled) {
382 // Rate depends on fraction of a SB64 in frame (xmis * ymis / bw * bh).
383 // It is converted to bits * 256 units
384 target_rate = (cpi->rc.sb64_target_rate * xmis * ymis * 256) / (bw * bh);
386 if (projected_rate < (target_rate / 4)) {
388 } else if (projected_rate < (target_rate / 2)) {
395 clamp((int)((projected_rate * 64) / target_rate), 16, 255);
398 // Fill in the entires in the segment map corresponding to this SB64
399 for (y = 0; y < ymis; y++) {
400 for (x = 0; x < xmis; x++) {
401 cpi->segmentation_map[mi_offset + y * cm->mi_cols + x] = segment;
402 cpi->complexity_map[mi_offset + y * cm->mi_cols + x] =
403 (unsigned char)complexity_metric;
408 static void update_state(VP9_COMP *cpi, PICK_MODE_CONTEXT *ctx,
409 BLOCK_SIZE bsize, int output_enabled) {
411 VP9_COMMON *const cm = &cpi->common;
412 MACROBLOCK *const x = &cpi->mb;
413 MACROBLOCKD *const xd = &x->e_mbd;
414 struct macroblock_plane *const p = x->plane;
415 struct macroblockd_plane *const pd = xd->plane;
416 MODE_INFO *mi = &ctx->mic;
417 MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi;
418 MODE_INFO *mi_addr = xd->mi_8x8[0];
420 int mb_mode_index = ctx->best_mode_index;
421 const int mis = cm->mode_info_stride;
422 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
423 const int mi_height = num_8x8_blocks_high_lookup[bsize];
426 assert(mi->mbmi.mode < MB_MODE_COUNT);
427 assert(mi->mbmi.ref_frame[0] < MAX_REF_FRAMES);
428 assert(mi->mbmi.ref_frame[1] < MAX_REF_FRAMES);
429 assert(mi->mbmi.sb_type == bsize);
431 // For in frame adaptive Q copy over the chosen segment id into the
432 // mode innfo context for the chosen mode / partition.
433 if ((cpi->oxcf.aq_mode == COMPLEXITY_AQ) && output_enabled)
434 mi->mbmi.segment_id = xd->mi_8x8[0]->mbmi.segment_id;
438 max_plane = is_inter_block(mbmi) ? MAX_MB_PLANE : 1;
439 for (i = 0; i < max_plane; ++i) {
440 p[i].coeff = ctx->coeff_pbuf[i][1];
441 p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
442 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
443 p[i].eobs = ctx->eobs_pbuf[i][1];
446 for (i = max_plane; i < MAX_MB_PLANE; ++i) {
447 p[i].coeff = ctx->coeff_pbuf[i][2];
448 p[i].qcoeff = ctx->qcoeff_pbuf[i][2];
449 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2];
450 p[i].eobs = ctx->eobs_pbuf[i][2];
453 // Restore the coding context of the MB to that that was in place
454 // when the mode was picked for it
455 for (y = 0; y < mi_height; y++)
456 for (x_idx = 0; x_idx < mi_width; x_idx++)
457 if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx
458 && (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
459 xd->mi_8x8[x_idx + y * mis] = mi_addr;
462 if ((cpi->oxcf.aq_mode == VARIANCE_AQ) ||
463 (cpi->oxcf.aq_mode == COMPLEXITY_AQ)) {
464 vp9_mb_init_quantizer(cpi, x);
467 // FIXME(rbultje) I'm pretty sure this should go to the end of this block
468 // (i.e. after the output_enabled)
469 if (bsize < BLOCK_32X32) {
470 if (bsize < BLOCK_16X16)
471 ctx->tx_rd_diff[ALLOW_16X16] = ctx->tx_rd_diff[ALLOW_8X8];
472 ctx->tx_rd_diff[ALLOW_32X32] = ctx->tx_rd_diff[ALLOW_16X16];
475 if (is_inter_block(mbmi) && mbmi->sb_type < BLOCK_8X8) {
476 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
477 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
481 vpx_memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk,
482 sizeof(uint8_t) * ctx->num_4x4_blk);
487 if (!vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
488 for (i = 0; i < TX_MODES; i++)
489 cpi->rd_tx_select_diff[i] += ctx->tx_rd_diff[i];
492 if (frame_is_intra_only(cm)) {
493 #if CONFIG_INTERNAL_STATS
494 static const int kf_mode_index[] = {
496 THR_V_PRED /*V_PRED*/,
497 THR_H_PRED /*H_PRED*/,
498 THR_D45_PRED /*D45_PRED*/,
499 THR_D135_PRED /*D135_PRED*/,
500 THR_D117_PRED /*D117_PRED*/,
501 THR_D153_PRED /*D153_PRED*/,
502 THR_D207_PRED /*D207_PRED*/,
503 THR_D63_PRED /*D63_PRED*/,
506 cpi->mode_chosen_counts[kf_mode_index[mi->mbmi.mode]]++;
509 // Note how often each mode chosen as best
510 cpi->mode_chosen_counts[mb_mode_index]++;
511 if (is_inter_block(mbmi)
512 && (mbmi->sb_type < BLOCK_8X8 || mbmi->mode == NEWMV)) {
514 const MV_REFERENCE_FRAME rf1 = mbmi->ref_frame[0];
515 const MV_REFERENCE_FRAME rf2 = mbmi->ref_frame[1];
516 best_mv[0].as_int = ctx->best_ref_mv.as_int;
517 best_mv[1].as_int = ctx->second_best_ref_mv.as_int;
518 if (mbmi->mode == NEWMV) {
519 best_mv[0].as_int = mbmi->ref_mvs[rf1][0].as_int;
521 best_mv[1].as_int = mbmi->ref_mvs[rf2][0].as_int;
523 mbmi->best_mv[0].as_int = best_mv[0].as_int;
524 mbmi->best_mv[1].as_int = best_mv[1].as_int;
525 vp9_update_mv_count(cpi, x, best_mv);
528 if (cm->mcomp_filter_type == SWITCHABLE && is_inter_mode(mbmi->mode)) {
529 const int ctx = vp9_get_pred_context_switchable_interp(xd);
530 ++cm->counts.switchable_interp[ctx][mbmi->interp_filter];
533 cpi->rd_comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
534 cpi->rd_comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
535 cpi->rd_comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
537 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
538 cpi->rd_filter_diff[i] += ctx->best_filter_diff[i];
542 void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
543 int mi_row, int mi_col) {
544 uint8_t *const buffers[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
546 const int strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
550 // Set current frame pointer.
551 x->e_mbd.cur_buf = src;
553 for (i = 0; i < MAX_MB_PLANE; i++)
554 setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
555 NULL, x->e_mbd.plane[i].subsampling_x,
556 x->e_mbd.plane[i].subsampling_y);
559 static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile,
560 int mi_row, int mi_col, BLOCK_SIZE bsize) {
561 MACROBLOCK *const x = &cpi->mb;
562 VP9_COMMON *const cm = &cpi->common;
563 MACROBLOCKD *const xd = &x->e_mbd;
565 const int dst_fb_idx = cm->new_fb_idx;
566 const int idx_str = xd->mode_info_stride * mi_row + mi_col;
567 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
568 const int mi_height = num_8x8_blocks_high_lookup[bsize];
569 const int mb_row = mi_row >> 1;
570 const int mb_col = mi_col >> 1;
571 const int idx_map = mb_row * cm->mb_cols + mb_col;
572 const struct segmentation *const seg = &cm->seg;
574 set_skip_context(xd, cpi->above_context, cpi->left_context, mi_row, mi_col);
576 // Activity map pointer
577 x->mb_activity_ptr = &cpi->mb_activity_map[idx_map];
578 x->active_ptr = cpi->active_map + idx_map;
580 xd->mi_8x8 = cm->mi_grid_visible + idx_str;
581 xd->prev_mi_8x8 = cm->prev_mi_grid_visible + idx_str;
583 // Special case: if prev_mi is NULL, the previous mode info context
585 xd->last_mi = cm->prev_mi ? xd->prev_mi_8x8[0] : NULL;
587 xd->mi_8x8[0] = cm->mi + idx_str;
589 mbmi = &xd->mi_8x8[0]->mbmi;
591 // Set up destination pointers
592 setup_dst_planes(xd, &cm->yv12_fb[dst_fb_idx], mi_row, mi_col);
594 // Set up limit values for MV components
595 // mv beyond the range do not produce new/different prediction block
596 x->mv_row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
597 x->mv_col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
598 x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
599 x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;
601 // Set up distance of MB to edge of frame in 1/8th pel units
602 assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
603 set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
604 cm->mi_rows, cm->mi_cols);
606 /* set up source buffers */
607 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
610 x->rddiv = cpi->RDDIV;
611 x->rdmult = cpi->RDMULT;
615 if (cpi->oxcf.aq_mode != VARIANCE_AQ) {
616 uint8_t *map = seg->update_map ? cpi->segmentation_map
617 : cm->last_frame_seg_map;
618 mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
620 vp9_mb_init_quantizer(cpi, x);
622 if (seg->enabled && cpi->seg0_cnt > 0
623 && !vp9_segfeature_active(seg, 0, SEG_LVL_REF_FRAME)
624 && vp9_segfeature_active(seg, 1, SEG_LVL_REF_FRAME)) {
625 cpi->seg0_progress = (cpi->seg0_idx << 16) / cpi->seg0_cnt;
627 const int y = mb_row & ~3;
628 const int x = mb_col & ~3;
629 const int p16 = ((mb_row & 1) << 1) + (mb_col & 1);
630 const int p32 = ((mb_row & 2) << 2) + ((mb_col & 2) << 1);
631 const int tile_progress = tile->mi_col_start * cm->mb_rows >> 1;
632 const int mb_cols = (tile->mi_col_end - tile->mi_col_start) >> 1;
634 cpi->seg0_progress = ((y * mb_cols + x * 4 + p32 + p16 + tile_progress)
638 x->encode_breakout = cpi->segment_encode_breakout[mbmi->segment_id];
640 mbmi->segment_id = 0;
641 x->encode_breakout = cpi->oxcf.encode_breakout;
645 static void pick_sb_modes(VP9_COMP *cpi, const TileInfo *const tile,
646 int mi_row, int mi_col,
647 int *totalrate, int64_t *totaldist,
648 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
650 VP9_COMMON *const cm = &cpi->common;
651 MACROBLOCK *const x = &cpi->mb;
652 MACROBLOCKD *const xd = &x->e_mbd;
653 struct macroblock_plane *const p = x->plane;
654 struct macroblockd_plane *const pd = xd->plane;
656 int orig_rdmult = x->rdmult;
659 vp9_clear_system_state(); // __asm emms;
660 rdmult_ratio = 1.0; // avoid uninitialized warnings
662 // Use the lower precision, but faster, 32x32 fdct for mode selection.
663 x->use_lp32x32fdct = 1;
665 if (bsize < BLOCK_8X8) {
666 // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
667 // there is nothing to be done.
668 if (x->ab_index != 0) {
675 set_offsets(cpi, tile, mi_row, mi_col, bsize);
676 xd->mi_8x8[0]->mbmi.sb_type = bsize;
678 for (i = 0; i < MAX_MB_PLANE; ++i) {
679 p[i].coeff = ctx->coeff_pbuf[i][0];
680 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
681 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
682 p[i].eobs = ctx->eobs_pbuf[i][0];
687 // Set to zero to make sure we do not use the previous encoded frame stats
688 xd->mi_8x8[0]->mbmi.skip_coeff = 0;
690 x->source_variance = get_sby_perpixel_variance(cpi, x, bsize);
692 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
694 if (bsize <= BLOCK_16X16) {
695 energy = x->mb_energy;
697 energy = vp9_block_energy(cpi, x, bsize);
700 xd->mi_8x8[0]->mbmi.segment_id = vp9_vaq_segment_id(energy);
701 rdmult_ratio = vp9_vaq_rdmult_ratio(energy);
702 vp9_mb_init_quantizer(cpi, x);
705 if (cpi->oxcf.tuning == VP8_TUNE_SSIM)
706 vp9_activity_masking(cpi, x);
708 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
709 vp9_clear_system_state(); // __asm emms;
710 x->rdmult = round(x->rdmult * rdmult_ratio);
711 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
712 const int mi_offset = mi_row * cm->mi_cols + mi_col;
713 unsigned char complexity = cpi->complexity_map[mi_offset];
714 const int is_edge = (mi_row == 0) || (mi_row == (cm->mi_rows - 1)) ||
715 (mi_col == 0) || (mi_col == (cm->mi_cols - 1));
717 if (!is_edge && (complexity > 128))
718 x->rdmult = x->rdmult + ((x->rdmult * (complexity - 128)) / 256);
721 // Find best coding mode & reconstruct the MB so it is available
722 // as a predictor for MBs that follow in the SB
723 if (frame_is_intra_only(cm)) {
724 vp9_rd_pick_intra_mode_sb(cpi, x, totalrate, totaldist, bsize, ctx,
727 if (bsize >= BLOCK_8X8)
728 vp9_rd_pick_inter_mode_sb(cpi, x, tile, mi_row, mi_col,
729 totalrate, totaldist, bsize, ctx, best_rd);
731 vp9_rd_pick_inter_mode_sub8x8(cpi, x, tile, mi_row, mi_col, totalrate,
732 totaldist, bsize, ctx, best_rd);
735 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
736 x->rdmult = orig_rdmult;
737 if (*totalrate != INT_MAX) {
738 vp9_clear_system_state(); // __asm emms;
739 *totalrate = round(*totalrate * rdmult_ratio);
744 static void update_stats(VP9_COMP *cpi) {
745 VP9_COMMON *const cm = &cpi->common;
746 MACROBLOCK *const x = &cpi->mb;
747 MACROBLOCKD *const xd = &x->e_mbd;
748 MODE_INFO *mi = xd->mi_8x8[0];
749 MB_MODE_INFO *const mbmi = &mi->mbmi;
751 if (!frame_is_intra_only(cm)) {
752 const int seg_ref_active = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
756 cm->counts.intra_inter[vp9_get_intra_inter_context(xd)]
757 [is_inter_block(mbmi)]++;
759 // If the segment reference feature is enabled we have only a single
760 // reference frame allowed for the segment so exclude it from
761 // the reference frame counts used to work out probabilities.
762 if (is_inter_block(mbmi) && !seg_ref_active) {
763 if (cm->reference_mode == REFERENCE_MODE_SELECT)
764 cm->counts.comp_inter[vp9_get_reference_mode_context(cm, xd)]
765 [has_second_ref(mbmi)]++;
767 if (has_second_ref(mbmi)) {
768 cm->counts.comp_ref[vp9_get_pred_context_comp_ref_p(cm, xd)]
769 [mbmi->ref_frame[0] == GOLDEN_FRAME]++;
771 cm->counts.single_ref[vp9_get_pred_context_single_ref_p1(xd)][0]
772 [mbmi->ref_frame[0] != LAST_FRAME]++;
773 if (mbmi->ref_frame[0] != LAST_FRAME)
774 cm->counts.single_ref[vp9_get_pred_context_single_ref_p2(xd)][1]
775 [mbmi->ref_frame[0] != GOLDEN_FRAME]++;
781 static BLOCK_SIZE *get_sb_partitioning(MACROBLOCK *x, BLOCK_SIZE bsize) {
784 return &x->sb64_partitioning;
786 return &x->sb_partitioning[x->sb_index];
788 return &x->mb_partitioning[x->sb_index][x->mb_index];
790 return &x->b_partitioning[x->sb_index][x->mb_index][x->b_index];
797 static void restore_context(VP9_COMP *cpi, int mi_row, int mi_col,
798 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
799 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
800 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
802 MACROBLOCK *const x = &cpi->mb;
803 MACROBLOCKD *const xd = &x->e_mbd;
805 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
806 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
807 int mi_width = num_8x8_blocks_wide_lookup[bsize];
808 int mi_height = num_8x8_blocks_high_lookup[bsize];
809 for (p = 0; p < MAX_MB_PLANE; p++) {
811 cpi->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
812 a + num_4x4_blocks_wide * p,
813 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
814 xd->plane[p].subsampling_x);
817 + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
818 l + num_4x4_blocks_high * p,
819 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
820 xd->plane[p].subsampling_y);
822 vpx_memcpy(cpi->above_seg_context + mi_col, sa,
823 sizeof(*cpi->above_seg_context) * mi_width);
824 vpx_memcpy(cpi->left_seg_context + (mi_row & MI_MASK), sl,
825 sizeof(cpi->left_seg_context[0]) * mi_height);
827 static void save_context(VP9_COMP *cpi, int mi_row, int mi_col,
828 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
829 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
830 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
832 const MACROBLOCK *const x = &cpi->mb;
833 const MACROBLOCKD *const xd = &x->e_mbd;
835 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
836 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
837 int mi_width = num_8x8_blocks_wide_lookup[bsize];
838 int mi_height = num_8x8_blocks_high_lookup[bsize];
840 // buffer the above/left context information of the block in search.
841 for (p = 0; p < MAX_MB_PLANE; ++p) {
843 a + num_4x4_blocks_wide * p,
844 cpi->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
845 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
846 xd->plane[p].subsampling_x);
848 l + num_4x4_blocks_high * p,
850 + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
851 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
852 xd->plane[p].subsampling_y);
854 vpx_memcpy(sa, cpi->above_seg_context + mi_col,
855 sizeof(*cpi->above_seg_context) * mi_width);
856 vpx_memcpy(sl, cpi->left_seg_context + (mi_row & MI_MASK),
857 sizeof(cpi->left_seg_context[0]) * mi_height);
860 static void encode_b(VP9_COMP *cpi, const TileInfo *const tile,
861 TOKENEXTRA **tp, int mi_row, int mi_col,
862 int output_enabled, BLOCK_SIZE bsize) {
863 MACROBLOCK *const x = &cpi->mb;
865 if (bsize < BLOCK_8X8) {
866 // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
867 // there is nothing to be done.
871 set_offsets(cpi, tile, mi_row, mi_col, bsize);
872 update_state(cpi, get_block_context(x, bsize), bsize, output_enabled);
873 encode_superblock(cpi, tp, output_enabled, mi_row, mi_col, bsize);
875 if (output_enabled) {
878 (*tp)->token = EOSB_TOKEN;
883 static void encode_sb(VP9_COMP *cpi, const TileInfo *const tile,
884 TOKENEXTRA **tp, int mi_row, int mi_col,
885 int output_enabled, BLOCK_SIZE bsize) {
886 VP9_COMMON *const cm = &cpi->common;
887 MACROBLOCK *const x = &cpi->mb;
888 const int bsl = b_width_log2(bsize), hbs = (1 << bsl) / 4;
890 PARTITION_TYPE partition;
893 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
896 if (bsize >= BLOCK_8X8) {
897 ctx = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
898 mi_row, mi_col, bsize);
899 subsize = *get_sb_partitioning(x, bsize);
905 partition = partition_lookup[bsl][subsize];
909 if (output_enabled && bsize >= BLOCK_8X8)
910 cm->counts.partition[ctx][PARTITION_NONE]++;
911 encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize);
915 cm->counts.partition[ctx][PARTITION_VERT]++;
916 *get_sb_index(x, subsize) = 0;
917 encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize);
918 if (mi_col + hbs < cm->mi_cols) {
919 *get_sb_index(x, subsize) = 1;
920 encode_b(cpi, tile, tp, mi_row, mi_col + hbs, output_enabled, subsize);
925 cm->counts.partition[ctx][PARTITION_HORZ]++;
926 *get_sb_index(x, subsize) = 0;
927 encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize);
928 if (mi_row + hbs < cm->mi_rows) {
929 *get_sb_index(x, subsize) = 1;
930 encode_b(cpi, tile, tp, mi_row + hbs, mi_col, output_enabled, subsize);
933 case PARTITION_SPLIT:
934 subsize = get_subsize(bsize, PARTITION_SPLIT);
936 cm->counts.partition[ctx][PARTITION_SPLIT]++;
938 *get_sb_index(x, subsize) = 0;
939 encode_sb(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize);
940 *get_sb_index(x, subsize) = 1;
941 encode_sb(cpi, tile, tp, mi_row, mi_col + hbs, output_enabled, subsize);
942 *get_sb_index(x, subsize) = 2;
943 encode_sb(cpi, tile, tp, mi_row + hbs, mi_col, output_enabled, subsize);
944 *get_sb_index(x, subsize) = 3;
945 encode_sb(cpi, tile, tp, mi_row + hbs, mi_col + hbs, output_enabled,
949 assert("Invalid partition type.");
952 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
953 update_partition_context(cpi->above_seg_context, cpi->left_seg_context,
954 mi_row, mi_col, subsize, bsize);
957 // Check to see if the given partition size is allowed for a specified number
958 // of 8x8 block rows and columns remaining in the image.
959 // If not then return the largest allowed partition size
960 static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize,
961 int rows_left, int cols_left,
963 if ((rows_left <= 0) || (cols_left <= 0)) {
964 return MIN(bsize, BLOCK_8X8);
966 for (; bsize > 0; --bsize) {
967 *bh = num_8x8_blocks_high_lookup[bsize];
968 *bw = num_8x8_blocks_wide_lookup[bsize];
969 if ((*bh <= rows_left) && (*bw <= cols_left)) {
977 // This function attempts to set all mode info entries in a given SB64
978 // to the same block partition size.
979 // However, at the bottom and right borders of the image the requested size
980 // may not be allowed in which case this code attempts to choose the largest
981 // allowable partition.
982 static void set_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
983 MODE_INFO **mi_8x8, int mi_row, int mi_col) {
984 VP9_COMMON *const cm = &cpi->common;
985 BLOCK_SIZE bsize = cpi->sf.always_this_block_size;
986 const int mis = cm->mode_info_stride;
987 int row8x8_remaining = tile->mi_row_end - mi_row;
988 int col8x8_remaining = tile->mi_col_end - mi_col;
989 int block_row, block_col;
990 MODE_INFO * mi_upper_left = cm->mi + mi_row * mis + mi_col;
991 int bh = num_8x8_blocks_high_lookup[bsize];
992 int bw = num_8x8_blocks_wide_lookup[bsize];
994 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
996 // Apply the requested partition size to the SB64 if it is all "in image"
997 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
998 (row8x8_remaining >= MI_BLOCK_SIZE)) {
999 for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
1000 for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
1001 int index = block_row * mis + block_col;
1002 mi_8x8[index] = mi_upper_left + index;
1003 mi_8x8[index]->mbmi.sb_type = bsize;
1007 // Else this is a partial SB64.
1008 for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
1009 for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
1010 int index = block_row * mis + block_col;
1011 // Find a partition size that fits
1012 bsize = find_partition_size(cpi->sf.always_this_block_size,
1013 (row8x8_remaining - block_row),
1014 (col8x8_remaining - block_col), &bh, &bw);
1015 mi_8x8[index] = mi_upper_left + index;
1016 mi_8x8[index]->mbmi.sb_type = bsize;
1022 static void copy_partitioning(VP9_COMP *cpi, MODE_INFO **mi_8x8,
1023 MODE_INFO **prev_mi_8x8) {
1024 VP9_COMMON *const cm = &cpi->common;
1025 const int mis = cm->mode_info_stride;
1026 int block_row, block_col;
1028 for (block_row = 0; block_row < 8; ++block_row) {
1029 for (block_col = 0; block_col < 8; ++block_col) {
1030 MODE_INFO * prev_mi = prev_mi_8x8[block_row * mis + block_col];
1031 BLOCK_SIZE sb_type = prev_mi ? prev_mi->mbmi.sb_type : 0;
1035 offset = prev_mi - cm->prev_mi;
1036 mi_8x8[block_row * mis + block_col] = cm->mi + offset;
1037 mi_8x8[block_row * mis + block_col]->mbmi.sb_type = sb_type;
1043 static int sb_has_motion(VP9_COMP *cpi, MODE_INFO **prev_mi_8x8) {
1044 VP9_COMMON *const cm = &cpi->common;
1045 const int mis = cm->mode_info_stride;
1046 int block_row, block_col;
1049 for (block_row = 0; block_row < 8; ++block_row) {
1050 for (block_col = 0; block_col < 8; ++block_col) {
1051 MODE_INFO * prev_mi = prev_mi_8x8[block_row * mis + block_col];
1053 if (abs(prev_mi->mbmi.mv[0].as_mv.row) >= 8 ||
1054 abs(prev_mi->mbmi.mv[0].as_mv.col) >= 8)
1063 static void rd_use_partition(VP9_COMP *cpi,
1064 const TileInfo *const tile,
1066 TOKENEXTRA **tp, int mi_row, int mi_col,
1067 BLOCK_SIZE bsize, int *rate, int64_t *dist,
1069 VP9_COMMON *const cm = &cpi->common;
1070 MACROBLOCK *const x = &cpi->mb;
1071 const int mis = cm->mode_info_stride;
1072 int bsl = b_width_log2(bsize);
1073 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1074 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1075 int ms = num_4x4_blocks_wide / 2;
1076 int mh = num_4x4_blocks_high / 2;
1077 int bss = (1 << bsl) / 4;
1079 PARTITION_TYPE partition = PARTITION_NONE;
1081 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1082 PARTITION_CONTEXT sl[8], sa[8];
1083 int last_part_rate = INT_MAX;
1084 int64_t last_part_dist = INT_MAX;
1085 int split_rate = INT_MAX;
1086 int64_t split_dist = INT_MAX;
1087 int none_rate = INT_MAX;
1088 int64_t none_dist = INT_MAX;
1089 int chosen_rate = INT_MAX;
1090 int64_t chosen_dist = INT_MAX;
1091 BLOCK_SIZE sub_subsize = BLOCK_4X4;
1092 int splits_below = 0;
1093 BLOCK_SIZE bs_type = mi_8x8[0]->mbmi.sb_type;
1095 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1098 partition = partition_lookup[bsl][bs_type];
1100 subsize = get_subsize(bsize, partition);
1102 if (bsize < BLOCK_8X8) {
1103 // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
1104 // there is nothing to be done.
1105 if (x->ab_index != 0) {
1111 *(get_sb_partitioning(x, bsize)) = subsize;
1113 save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1115 if (bsize == BLOCK_16X16) {
1116 set_offsets(cpi, tile, mi_row, mi_col, bsize);
1117 x->mb_energy = vp9_block_energy(cpi, x, bsize);
1121 x->subblock_ref = 0;
1123 if (cpi->sf.adjust_partitioning_from_last_frame) {
1124 // Check if any of the sub blocks are further split.
1125 if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
1126 sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
1128 for (i = 0; i < 4; i++) {
1129 int jj = i >> 1, ii = i & 0x01;
1130 MODE_INFO * this_mi = mi_8x8[jj * bss * mis + ii * bss];
1131 if (this_mi && this_mi->mbmi.sb_type >= sub_subsize) {
1137 // If partition is not none try none unless each of the 4 splits are split
1139 if (partition != PARTITION_NONE && !splits_below &&
1140 mi_row + (ms >> 1) < cm->mi_rows &&
1141 mi_col + (ms >> 1) < cm->mi_cols) {
1142 *(get_sb_partitioning(x, bsize)) = bsize;
1143 pick_sb_modes(cpi, tile, mi_row, mi_col, &none_rate, &none_dist, bsize,
1144 get_block_context(x, bsize), INT64_MAX);
1146 pl = partition_plane_context(cpi->above_seg_context,
1147 cpi->left_seg_context,
1148 mi_row, mi_col, bsize);
1149 none_rate += x->partition_cost[pl][PARTITION_NONE];
1151 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1152 mi_8x8[0]->mbmi.sb_type = bs_type;
1153 *(get_sb_partitioning(x, bsize)) = subsize;
1157 switch (partition) {
1158 case PARTITION_NONE:
1159 pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
1160 bsize, get_block_context(x, bsize), INT64_MAX);
1162 case PARTITION_HORZ:
1163 *get_sb_index(x, subsize) = 0;
1164 pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
1165 subsize, get_block_context(x, subsize), INT64_MAX);
1166 if (last_part_rate != INT_MAX &&
1167 bsize >= BLOCK_8X8 && mi_row + (mh >> 1) < cm->mi_rows) {
1170 update_state(cpi, get_block_context(x, subsize), subsize, 0);
1171 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1172 *get_sb_index(x, subsize) = 1;
1173 pick_sb_modes(cpi, tile, mi_row + (ms >> 1), mi_col, &rt, &dt, subsize,
1174 get_block_context(x, subsize), INT64_MAX);
1175 if (rt == INT_MAX || dt == INT_MAX) {
1176 last_part_rate = INT_MAX;
1177 last_part_dist = INT_MAX;
1181 last_part_rate += rt;
1182 last_part_dist += dt;
1185 case PARTITION_VERT:
1186 *get_sb_index(x, subsize) = 0;
1187 pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate, &last_part_dist,
1188 subsize, get_block_context(x, subsize), INT64_MAX);
1189 if (last_part_rate != INT_MAX &&
1190 bsize >= BLOCK_8X8 && mi_col + (ms >> 1) < cm->mi_cols) {
1193 update_state(cpi, get_block_context(x, subsize), subsize, 0);
1194 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1195 *get_sb_index(x, subsize) = 1;
1196 pick_sb_modes(cpi, tile, mi_row, mi_col + (ms >> 1), &rt, &dt, subsize,
1197 get_block_context(x, subsize), INT64_MAX);
1198 if (rt == INT_MAX || dt == INT_MAX) {
1199 last_part_rate = INT_MAX;
1200 last_part_dist = INT_MAX;
1203 last_part_rate += rt;
1204 last_part_dist += dt;
1207 case PARTITION_SPLIT:
1211 for (i = 0; i < 4; i++) {
1212 int x_idx = (i & 1) * (ms >> 1);
1213 int y_idx = (i >> 1) * (ms >> 1);
1214 int jj = i >> 1, ii = i & 0x01;
1218 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1221 *get_sb_index(x, subsize) = i;
1223 rd_use_partition(cpi, tile, mi_8x8 + jj * bss * mis + ii * bss, tp,
1224 mi_row + y_idx, mi_col + x_idx, subsize, &rt, &dt,
1226 if (rt == INT_MAX || dt == INT_MAX) {
1227 last_part_rate = INT_MAX;
1228 last_part_dist = INT_MAX;
1231 last_part_rate += rt;
1232 last_part_dist += dt;
1239 pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
1240 mi_row, mi_col, bsize);
1241 if (last_part_rate < INT_MAX)
1242 last_part_rate += x->partition_cost[pl][partition];
1244 if (cpi->sf.adjust_partitioning_from_last_frame
1245 && partition != PARTITION_SPLIT && bsize > BLOCK_8X8
1246 && (mi_row + ms < cm->mi_rows || mi_row + (ms >> 1) == cm->mi_rows)
1247 && (mi_col + ms < cm->mi_cols || mi_col + (ms >> 1) == cm->mi_cols)) {
1248 BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
1251 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1254 for (i = 0; i < 4; i++) {
1255 int x_idx = (i & 1) * (num_4x4_blocks_wide >> 2);
1256 int y_idx = (i >> 1) * (num_4x4_blocks_wide >> 2);
1259 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1260 PARTITION_CONTEXT sl[8], sa[8];
1262 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1265 *get_sb_index(x, split_subsize) = i;
1266 *get_sb_partitioning(x, bsize) = split_subsize;
1267 *get_sb_partitioning(x, split_subsize) = split_subsize;
1269 save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1271 pick_sb_modes(cpi, tile, mi_row + y_idx, mi_col + x_idx, &rt, &dt,
1272 split_subsize, get_block_context(x, split_subsize),
1275 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1277 if (rt == INT_MAX || dt == INT_MAX) {
1278 split_rate = INT_MAX;
1279 split_dist = INT_MAX;
1284 encode_sb(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx, 0,
1289 pl = partition_plane_context(cpi->above_seg_context,
1290 cpi->left_seg_context,
1291 mi_row + y_idx, mi_col + x_idx,
1293 split_rate += x->partition_cost[pl][PARTITION_NONE];
1295 pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
1296 mi_row, mi_col, bsize);
1297 if (split_rate < INT_MAX) {
1298 split_rate += x->partition_cost[pl][PARTITION_SPLIT];
1300 chosen_rate = split_rate;
1301 chosen_dist = split_dist;
1305 // If last_part is better set the partitioning to that...
1306 if (RDCOST(x->rdmult, x->rddiv, last_part_rate, last_part_dist)
1307 < RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)) {
1308 mi_8x8[0]->mbmi.sb_type = bsize;
1309 if (bsize >= BLOCK_8X8)
1310 *(get_sb_partitioning(x, bsize)) = subsize;
1311 chosen_rate = last_part_rate;
1312 chosen_dist = last_part_dist;
1314 // If none was better set the partitioning to that...
1315 if (RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist)
1316 > RDCOST(x->rdmult, x->rddiv, none_rate, none_dist)) {
1317 if (bsize >= BLOCK_8X8)
1318 *(get_sb_partitioning(x, bsize)) = bsize;
1319 chosen_rate = none_rate;
1320 chosen_dist = none_dist;
1323 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1325 // We must have chosen a partitioning and encoding or we'll fail later on.
1326 // No other opportunities for success.
1327 if ( bsize == BLOCK_64X64)
1328 assert(chosen_rate < INT_MAX && chosen_dist < INT_MAX);
1331 int output_enabled = (bsize == BLOCK_64X64);
1333 // Check the projected output rate for this SB against it's target
1334 // and and if necessary apply a Q delta using segmentation to get
1335 // closer to the target.
1336 if ((cpi->oxcf.aq_mode == COMPLEXITY_AQ) && cm->seg.update_map) {
1337 select_in_frame_q_segment(cpi, mi_row, mi_col,
1338 output_enabled, chosen_rate);
1341 encode_sb(cpi, tile, tp, mi_row, mi_col, output_enabled, bsize);
1344 *rate = chosen_rate;
1345 *dist = chosen_dist;
1348 static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
1349 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
1350 BLOCK_4X4, BLOCK_4X4, BLOCK_8X8, BLOCK_8X8,
1351 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16
1354 static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
1355 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
1356 BLOCK_32X32, BLOCK_32X32, BLOCK_32X32, BLOCK_64X64,
1357 BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64
1360 // Look at all the mode_info entries for blocks that are part of this
1361 // partition and find the min and max values for sb_type.
1362 // At the moment this is designed to work on a 64x64 SB but could be
1363 // adjusted to use a size parameter.
1365 // The min and max are assumed to have been initialized prior to calling this
1366 // function so repeat calls can accumulate a min and max of more than one sb64.
1367 static void get_sb_partition_size_range(VP9_COMP *cpi, MODE_INFO ** mi_8x8,
1368 BLOCK_SIZE * min_block_size,
1369 BLOCK_SIZE * max_block_size ) {
1370 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1371 int sb_width_in_blocks = MI_BLOCK_SIZE;
1372 int sb_height_in_blocks = MI_BLOCK_SIZE;
1376 // Check the sb_type for each block that belongs to this region.
1377 for (i = 0; i < sb_height_in_blocks; ++i) {
1378 for (j = 0; j < sb_width_in_blocks; ++j) {
1379 MODE_INFO * mi = mi_8x8[index+j];
1380 BLOCK_SIZE sb_type = mi ? mi->mbmi.sb_type : 0;
1381 *min_block_size = MIN(*min_block_size, sb_type);
1382 *max_block_size = MAX(*max_block_size, sb_type);
1384 index += xd->mode_info_stride;
1388 // Look at neighboring blocks and set a min and max partition size based on
1390 static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
1392 BLOCK_SIZE *min_block_size,
1393 BLOCK_SIZE *max_block_size) {
1394 VP9_COMMON * const cm = &cpi->common;
1395 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1396 MODE_INFO ** mi_8x8 = xd->mi_8x8;
1397 MODE_INFO ** prev_mi_8x8 = xd->prev_mi_8x8;
1399 const int left_in_image = xd->left_available && mi_8x8[-1];
1400 const int above_in_image = xd->up_available &&
1401 mi_8x8[-xd->mode_info_stride];
1402 MODE_INFO ** above_sb64_mi_8x8;
1403 MODE_INFO ** left_sb64_mi_8x8;
1405 int row8x8_remaining = tile->mi_row_end - row;
1406 int col8x8_remaining = tile->mi_col_end - col;
1409 // Trap case where we do not have a prediction.
1410 if (!left_in_image && !above_in_image &&
1411 ((cm->frame_type == KEY_FRAME) || !cm->prev_mi)) {
1412 *min_block_size = BLOCK_4X4;
1413 *max_block_size = BLOCK_64X64;
1415 // Default "min to max" and "max to min"
1416 *min_block_size = BLOCK_64X64;
1417 *max_block_size = BLOCK_4X4;
1419 // NOTE: each call to get_sb_partition_size_range() uses the previous
1420 // passed in values for min and max as a starting point.
1422 // Find the min and max partition used in previous frame at this location
1423 if (cm->prev_mi && (cm->frame_type != KEY_FRAME)) {
1424 get_sb_partition_size_range(cpi, prev_mi_8x8,
1425 min_block_size, max_block_size);
1428 // Find the min and max partition sizes used in the left SB64
1429 if (left_in_image) {
1430 left_sb64_mi_8x8 = &mi_8x8[-MI_BLOCK_SIZE];
1431 get_sb_partition_size_range(cpi, left_sb64_mi_8x8,
1432 min_block_size, max_block_size);
1435 // Find the min and max partition sizes used in the above SB64.
1436 if (above_in_image) {
1437 above_sb64_mi_8x8 = &mi_8x8[-xd->mode_info_stride * MI_BLOCK_SIZE];
1438 get_sb_partition_size_range(cpi, above_sb64_mi_8x8,
1439 min_block_size, max_block_size);
1443 // Give a bit of leaway either side of the observed min and max
1444 *min_block_size = min_partition_size[*min_block_size];
1445 *max_block_size = max_partition_size[*max_block_size];
1447 // Check border cases where max and min from neighbours may not be legal.
1448 *max_block_size = find_partition_size(*max_block_size,
1449 row8x8_remaining, col8x8_remaining,
1451 *min_block_size = MIN(*min_block_size, *max_block_size);
1454 static void compute_fast_motion_search_level(VP9_COMP *cpi, BLOCK_SIZE bsize) {
1455 VP9_COMMON *const cm = &cpi->common;
1456 MACROBLOCK *const x = &cpi->mb;
1458 // Only use 8x8 result for non HD videos.
1459 // int use_8x8 = (MIN(cpi->common.width, cpi->common.height) < 720) ? 1 : 0;
1462 if (cm->frame_type && !cpi->rc.is_src_frame_alt_ref &&
1463 ((use_8x8 && bsize == BLOCK_16X16) ||
1464 bsize == BLOCK_32X32 || bsize == BLOCK_64X64)) {
1465 int ref0 = 0, ref1 = 0, ref2 = 0, ref3 = 0;
1466 PICK_MODE_CONTEXT *block_context = NULL;
1468 if (bsize == BLOCK_16X16) {
1469 block_context = x->sb8x8_context[x->sb_index][x->mb_index];
1470 } else if (bsize == BLOCK_32X32) {
1471 block_context = x->mb_context[x->sb_index];
1472 } else if (bsize == BLOCK_64X64) {
1473 block_context = x->sb32_context;
1476 if (block_context) {
1477 ref0 = block_context[0].mic.mbmi.ref_frame[0];
1478 ref1 = block_context[1].mic.mbmi.ref_frame[0];
1479 ref2 = block_context[2].mic.mbmi.ref_frame[0];
1480 ref3 = block_context[3].mic.mbmi.ref_frame[0];
1483 // Currently, only consider 4 inter reference frames.
1484 if (ref0 && ref1 && ref2 && ref3) {
1485 int d01, d23, d02, d13;
1487 // Motion vectors for the four subblocks.
1488 int16_t mvr0 = block_context[0].mic.mbmi.mv[0].as_mv.row;
1489 int16_t mvc0 = block_context[0].mic.mbmi.mv[0].as_mv.col;
1490 int16_t mvr1 = block_context[1].mic.mbmi.mv[0].as_mv.row;
1491 int16_t mvc1 = block_context[1].mic.mbmi.mv[0].as_mv.col;
1492 int16_t mvr2 = block_context[2].mic.mbmi.mv[0].as_mv.row;
1493 int16_t mvc2 = block_context[2].mic.mbmi.mv[0].as_mv.col;
1494 int16_t mvr3 = block_context[3].mic.mbmi.mv[0].as_mv.row;
1495 int16_t mvc3 = block_context[3].mic.mbmi.mv[0].as_mv.col;
1497 // Adjust sign if ref is alt_ref.
1498 if (cm->ref_frame_sign_bias[ref0]) {
1503 if (cm->ref_frame_sign_bias[ref1]) {
1508 if (cm->ref_frame_sign_bias[ref2]) {
1513 if (cm->ref_frame_sign_bias[ref3]) {
1518 // Calculate mv distances.
1519 d01 = MAX(abs(mvr0 - mvr1), abs(mvc0 - mvc1));
1520 d23 = MAX(abs(mvr2 - mvr3), abs(mvc2 - mvc3));
1521 d02 = MAX(abs(mvr0 - mvr2), abs(mvc0 - mvc2));
1522 d13 = MAX(abs(mvr1 - mvr3), abs(mvc1 - mvc3));
1524 if (d01 < FAST_MOTION_MV_THRESH && d23 < FAST_MOTION_MV_THRESH &&
1525 d02 < FAST_MOTION_MV_THRESH && d13 < FAST_MOTION_MV_THRESH) {
1526 // Set fast motion search level.
1529 if (ref0 == ref1 && ref1 == ref2 && ref2 == ref3 &&
1530 d01 < 2 && d23 < 2 && d02 < 2 && d13 < 2) {
1531 // Set fast motion search level.
1534 if (!d01 && !d23 && !d02 && !d13) {
1536 x->subblock_ref = ref0;
1544 static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
1545 vpx_memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
1548 static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
1549 vpx_memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
1552 // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
1553 // unlikely to be selected depending on previous rate-distortion optimization
1554 // results, for encoding speed-up.
1555 static void rd_pick_partition(VP9_COMP *cpi, const TileInfo *const tile,
1556 TOKENEXTRA **tp, int mi_row,
1557 int mi_col, BLOCK_SIZE bsize, int *rate,
1558 int64_t *dist, int do_recon, int64_t best_rd) {
1559 VP9_COMMON *const cm = &cpi->common;
1560 MACROBLOCK *const x = &cpi->mb;
1561 const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
1562 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1563 PARTITION_CONTEXT sl[8], sa[8];
1564 TOKENEXTRA *tp_orig = *tp;
1567 int this_rate, sum_rate = 0, best_rate = INT_MAX;
1568 int64_t this_dist, sum_dist = 0, best_dist = INT64_MAX;
1570 int do_split = bsize >= BLOCK_8X8;
1572 // Override skipping rectangular partition operations for edge blocks
1573 const int force_horz_split = (mi_row + ms >= cm->mi_rows);
1574 const int force_vert_split = (mi_col + ms >= cm->mi_cols);
1575 const int xss = x->e_mbd.plane[1].subsampling_x;
1576 const int yss = x->e_mbd.plane[1].subsampling_y;
1578 int partition_none_allowed = !force_horz_split && !force_vert_split;
1579 int partition_horz_allowed = !force_vert_split && yss <= xss &&
1581 int partition_vert_allowed = !force_horz_split && xss <= yss &&
1584 int partition_split_done = 0;
1587 if (bsize < BLOCK_8X8) {
1588 // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
1589 // there is nothing to be done.
1590 if (x->ab_index != 0) {
1596 assert(num_8x8_blocks_wide_lookup[bsize] ==
1597 num_8x8_blocks_high_lookup[bsize]);
1599 if (bsize == BLOCK_16X16) {
1600 set_offsets(cpi, tile, mi_row, mi_col, bsize);
1601 x->mb_energy = vp9_block_energy(cpi, x, bsize);
1604 // Determine partition types in search according to the speed features.
1605 // The threshold set here has to be of square block size.
1606 if (cpi->sf.auto_min_max_partition_size) {
1607 partition_none_allowed &= (bsize <= cpi->sf.max_partition_size &&
1608 bsize >= cpi->sf.min_partition_size);
1609 partition_horz_allowed &= ((bsize <= cpi->sf.max_partition_size &&
1610 bsize > cpi->sf.min_partition_size) ||
1612 partition_vert_allowed &= ((bsize <= cpi->sf.max_partition_size &&
1613 bsize > cpi->sf.min_partition_size) ||
1615 do_split &= bsize > cpi->sf.min_partition_size;
1617 if (cpi->sf.use_square_partition_only) {
1618 partition_horz_allowed &= force_horz_split;
1619 partition_vert_allowed &= force_vert_split;
1622 save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1624 if (cpi->sf.disable_split_var_thresh && partition_none_allowed) {
1625 unsigned int source_variancey;
1626 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
1627 source_variancey = get_sby_perpixel_variance(cpi, x, bsize);
1628 if (source_variancey < cpi->sf.disable_split_var_thresh) {
1630 if (source_variancey < cpi->sf.disable_split_var_thresh / 2)
1636 if (partition_none_allowed) {
1637 pick_sb_modes(cpi, tile, mi_row, mi_col, &this_rate, &this_dist, bsize,
1638 get_block_context(x, bsize), best_rd);
1639 if (this_rate != INT_MAX) {
1640 if (bsize >= BLOCK_8X8) {
1641 pl = partition_plane_context(cpi->above_seg_context,
1642 cpi->left_seg_context,
1643 mi_row, mi_col, bsize);
1644 this_rate += x->partition_cost[pl][PARTITION_NONE];
1646 sum_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_dist);
1647 if (sum_rd < best_rd) {
1648 int64_t stop_thresh = 4096;
1649 int64_t stop_thresh_rd;
1651 best_rate = this_rate;
1652 best_dist = this_dist;
1654 if (bsize >= BLOCK_8X8)
1655 *(get_sb_partitioning(x, bsize)) = bsize;
1657 // Adjust threshold according to partition size.
1658 stop_thresh >>= 8 - (b_width_log2_lookup[bsize] +
1659 b_height_log2_lookup[bsize]);
1661 stop_thresh_rd = RDCOST(x->rdmult, x->rddiv, 0, stop_thresh);
1662 // If obtained distortion is very small, choose current partition
1663 // and stop splitting.
1664 if (!x->e_mbd.lossless && best_rd < stop_thresh_rd) {
1670 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1673 // store estimated motion vector
1674 if (cpi->sf.adaptive_motion_search)
1675 store_pred_mv(x, get_block_context(x, bsize));
1679 // TODO(jingning): use the motion vectors given by the above search as
1680 // the starting point of motion search in the following partition type check.
1682 subsize = get_subsize(bsize, PARTITION_SPLIT);
1683 for (i = 0; i < 4 && sum_rd < best_rd; ++i) {
1684 const int x_idx = (i & 1) * ms;
1685 const int y_idx = (i >> 1) * ms;
1687 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
1690 *get_sb_index(x, subsize) = i;
1691 if (cpi->sf.adaptive_motion_search)
1692 load_pred_mv(x, get_block_context(x, bsize));
1693 if (cpi->sf.adaptive_pred_filter_type && bsize == BLOCK_8X8 &&
1694 partition_none_allowed)
1695 get_block_context(x, subsize)->pred_filter_type =
1696 get_block_context(x, bsize)->mic.mbmi.interp_filter;
1697 rd_pick_partition(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx, subsize,
1698 &this_rate, &this_dist, i != 3, best_rd - sum_rd);
1700 if (this_rate == INT_MAX) {
1703 sum_rate += this_rate;
1704 sum_dist += this_dist;
1705 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1708 if (sum_rd < best_rd && i == 4) {
1709 pl = partition_plane_context(cpi->above_seg_context,
1710 cpi->left_seg_context,
1711 mi_row, mi_col, bsize);
1712 sum_rate += x->partition_cost[pl][PARTITION_SPLIT];
1713 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1714 if (sum_rd < best_rd) {
1715 best_rate = sum_rate;
1716 best_dist = sum_dist;
1718 *(get_sb_partitioning(x, bsize)) = subsize;
1721 // skip rectangular partition test when larger block size
1722 // gives better rd cost
1723 if (cpi->sf.less_rectangular_check)
1724 do_rect &= !partition_none_allowed;
1726 partition_split_done = 1;
1727 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1731 x->subblock_ref = 0;
1733 if (partition_split_done &&
1734 cpi->sf.using_small_partition_info) {
1735 compute_fast_motion_search_level(cpi, bsize);
1739 if (partition_horz_allowed && do_rect) {
1740 subsize = get_subsize(bsize, PARTITION_HORZ);
1741 *get_sb_index(x, subsize) = 0;
1742 if (cpi->sf.adaptive_motion_search)
1743 load_pred_mv(x, get_block_context(x, bsize));
1744 if (cpi->sf.adaptive_pred_filter_type && bsize == BLOCK_8X8 &&
1745 partition_none_allowed)
1746 get_block_context(x, subsize)->pred_filter_type =
1747 get_block_context(x, bsize)->mic.mbmi.interp_filter;
1748 pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
1749 get_block_context(x, subsize), best_rd);
1750 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1752 if (sum_rd < best_rd && mi_row + ms < cm->mi_rows) {
1753 update_state(cpi, get_block_context(x, subsize), subsize, 0);
1754 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1756 *get_sb_index(x, subsize) = 1;
1757 if (cpi->sf.adaptive_motion_search)
1758 load_pred_mv(x, get_block_context(x, bsize));
1759 if (cpi->sf.adaptive_pred_filter_type && bsize == BLOCK_8X8 &&
1760 partition_none_allowed)
1761 get_block_context(x, subsize)->pred_filter_type =
1762 get_block_context(x, bsize)->mic.mbmi.interp_filter;
1763 pick_sb_modes(cpi, tile, mi_row + ms, mi_col, &this_rate,
1764 &this_dist, subsize, get_block_context(x, subsize),
1766 if (this_rate == INT_MAX) {
1769 sum_rate += this_rate;
1770 sum_dist += this_dist;
1771 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1774 if (sum_rd < best_rd) {
1775 pl = partition_plane_context(cpi->above_seg_context,
1776 cpi->left_seg_context,
1777 mi_row, mi_col, bsize);
1778 sum_rate += x->partition_cost[pl][PARTITION_HORZ];
1779 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1780 if (sum_rd < best_rd) {
1782 best_rate = sum_rate;
1783 best_dist = sum_dist;
1784 *(get_sb_partitioning(x, bsize)) = subsize;
1787 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1791 if (partition_vert_allowed && do_rect) {
1792 subsize = get_subsize(bsize, PARTITION_VERT);
1794 *get_sb_index(x, subsize) = 0;
1795 if (cpi->sf.adaptive_motion_search)
1796 load_pred_mv(x, get_block_context(x, bsize));
1797 if (cpi->sf.adaptive_pred_filter_type && bsize == BLOCK_8X8 &&
1798 partition_none_allowed)
1799 get_block_context(x, subsize)->pred_filter_type =
1800 get_block_context(x, bsize)->mic.mbmi.interp_filter;
1801 pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
1802 get_block_context(x, subsize), best_rd);
1803 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1804 if (sum_rd < best_rd && mi_col + ms < cm->mi_cols) {
1805 update_state(cpi, get_block_context(x, subsize), subsize, 0);
1806 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize);
1808 *get_sb_index(x, subsize) = 1;
1809 if (cpi->sf.adaptive_motion_search)
1810 load_pred_mv(x, get_block_context(x, bsize));
1811 if (cpi->sf.adaptive_pred_filter_type && bsize == BLOCK_8X8 &&
1812 partition_none_allowed)
1813 get_block_context(x, subsize)->pred_filter_type =
1814 get_block_context(x, bsize)->mic.mbmi.interp_filter;
1815 pick_sb_modes(cpi, tile, mi_row, mi_col + ms, &this_rate,
1816 &this_dist, subsize, get_block_context(x, subsize),
1818 if (this_rate == INT_MAX) {
1821 sum_rate += this_rate;
1822 sum_dist += this_dist;
1823 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1826 if (sum_rd < best_rd) {
1827 pl = partition_plane_context(cpi->above_seg_context,
1828 cpi->left_seg_context,
1829 mi_row, mi_col, bsize);
1830 sum_rate += x->partition_cost[pl][PARTITION_VERT];
1831 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
1832 if (sum_rd < best_rd) {
1833 best_rate = sum_rate;
1834 best_dist = sum_dist;
1836 *(get_sb_partitioning(x, bsize)) = subsize;
1839 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1846 if (best_rate < INT_MAX && best_dist < INT64_MAX && do_recon) {
1847 int output_enabled = (bsize == BLOCK_64X64);
1849 // Check the projected output rate for this SB against it's target
1850 // and and if necessary apply a Q delta using segmentation to get
1851 // closer to the target.
1852 if ((cpi->oxcf.aq_mode == COMPLEXITY_AQ) && cm->seg.update_map) {
1853 select_in_frame_q_segment(cpi, mi_row, mi_col, output_enabled, best_rate);
1855 encode_sb(cpi, tile, tp, mi_row, mi_col, output_enabled, bsize);
1857 if (bsize == BLOCK_64X64) {
1858 assert(tp_orig < *tp);
1859 assert(best_rate < INT_MAX);
1860 assert(best_dist < INT_MAX);
1862 assert(tp_orig == *tp);
1866 // Examines 64x64 block and chooses a best reference frame
1867 static void rd_pick_reference_frame(VP9_COMP *cpi, const TileInfo *const tile,
1868 int mi_row, int mi_col) {
1869 VP9_COMMON * const cm = &cpi->common;
1870 MACROBLOCK * const x = &cpi->mb;
1871 int bsl = b_width_log2(BLOCK_64X64), bs = 1 << bsl;
1873 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1874 PARTITION_CONTEXT sl[8], sa[8];
1879 save_context(cpi, mi_row, mi_col, a, l, sa, sl, BLOCK_64X64);
1881 // Default is non mask (all reference frames allowed.
1882 cpi->ref_frame_mask = 0;
1884 // Do RD search for 64x64.
1885 if ((mi_row + (ms >> 1) < cm->mi_rows) &&
1886 (mi_col + (ms >> 1) < cm->mi_cols)) {
1887 cpi->set_ref_frame_mask = 1;
1888 pick_sb_modes(cpi, tile, mi_row, mi_col, &r, &d, BLOCK_64X64,
1889 get_block_context(x, BLOCK_64X64), INT64_MAX);
1890 pl = partition_plane_context(cpi->above_seg_context, cpi->left_seg_context,
1891 mi_row, mi_col, BLOCK_64X64);
1892 r += x->partition_cost[pl][PARTITION_NONE];
1894 *(get_sb_partitioning(x, BLOCK_64X64)) = BLOCK_64X64;
1895 cpi->set_ref_frame_mask = 0;
1898 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, BLOCK_64X64);
1901 static void encode_sb_row(VP9_COMP *cpi, const TileInfo *const tile,
1902 int mi_row, TOKENEXTRA **tp) {
1903 VP9_COMMON * const cm = &cpi->common;
1906 // Initialize the left context for the new SB row
1907 vpx_memset(&cpi->left_context, 0, sizeof(cpi->left_context));
1908 vpx_memset(cpi->left_seg_context, 0, sizeof(cpi->left_seg_context));
1910 // Code each SB in the row
1911 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
1912 mi_col += MI_BLOCK_SIZE) {
1917 MACROBLOCK *x = &cpi->mb;
1918 for (i = BLOCK_4X4; i < BLOCK_8X8; ++i) {
1919 const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
1920 const int num_4x4_h = num_4x4_blocks_high_lookup[i];
1921 const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
1922 for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index)
1923 for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index)
1924 for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index)
1925 get_block_context(x, i)->pred_filter_type = SWITCHABLE;
1928 vp9_zero(cpi->mb.pred_mv);
1930 if (cpi->sf.reference_masking)
1931 rd_pick_reference_frame(cpi, tile, mi_row, mi_col);
1933 if (cpi->sf.use_lastframe_partitioning ||
1934 cpi->sf.use_one_partition_size_always ) {
1935 const int idx_str = cm->mode_info_stride * mi_row + mi_col;
1936 MODE_INFO **mi_8x8 = cm->mi_grid_visible + idx_str;
1937 MODE_INFO **prev_mi_8x8 = cm->prev_mi_grid_visible + idx_str;
1939 cpi->mb.source_variance = UINT_MAX;
1940 if (cpi->sf.use_one_partition_size_always) {
1941 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
1942 set_partitioning(cpi, tile, mi_8x8, mi_row, mi_col);
1943 rd_use_partition(cpi, tile, mi_8x8, tp, mi_row, mi_col, BLOCK_64X64,
1944 &dummy_rate, &dummy_dist, 1);
1946 if ((cpi->common.current_video_frame
1947 % cpi->sf.last_partitioning_redo_frequency) == 0
1949 || cpi->common.show_frame == 0
1950 || cpi->common.frame_type == KEY_FRAME
1951 || cpi->rc.is_src_frame_alt_ref
1952 || ((cpi->sf.use_lastframe_partitioning ==
1953 LAST_FRAME_PARTITION_LOW_MOTION) &&
1954 sb_has_motion(cpi, prev_mi_8x8))) {
1955 // If required set upper and lower partition size limits
1956 if (cpi->sf.auto_min_max_partition_size) {
1957 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
1958 rd_auto_partition_range(cpi, tile, mi_row, mi_col,
1959 &cpi->sf.min_partition_size,
1960 &cpi->sf.max_partition_size);
1962 rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
1963 &dummy_rate, &dummy_dist, 1, INT64_MAX);
1965 copy_partitioning(cpi, mi_8x8, prev_mi_8x8);
1966 rd_use_partition(cpi, tile, mi_8x8, tp, mi_row, mi_col, BLOCK_64X64,
1967 &dummy_rate, &dummy_dist, 1);
1971 // If required set upper and lower partition size limits
1972 if (cpi->sf.auto_min_max_partition_size) {
1973 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
1974 rd_auto_partition_range(cpi, tile, mi_row, mi_col,
1975 &cpi->sf.min_partition_size,
1976 &cpi->sf.max_partition_size);
1978 rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
1979 &dummy_rate, &dummy_dist, 1, INT64_MAX);
1984 static void init_encode_frame_mb_context(VP9_COMP *cpi) {
1985 MACROBLOCK *const x = &cpi->mb;
1986 VP9_COMMON *const cm = &cpi->common;
1987 MACROBLOCKD *const xd = &x->e_mbd;
1988 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1990 x->act_zbin_adj = 0;
1993 xd->mode_info_stride = cm->mode_info_stride;
1995 // Copy data over into macro block data structures.
1996 vp9_setup_src_planes(x, cpi->Source, 0, 0);
1998 // TODO(jkoleszar): are these initializations required?
1999 setup_pre_planes(xd, 0, &cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]],
2001 setup_dst_planes(xd, get_frame_new_buffer(cm), 0, 0);
2003 setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
2005 xd->mi_8x8[0]->mbmi.mode = DC_PRED;
2006 xd->mi_8x8[0]->mbmi.uv_mode = DC_PRED;
2008 vp9_zero(cm->counts.y_mode);
2009 vp9_zero(cm->counts.uv_mode);
2010 vp9_zero(cm->counts.inter_mode);
2011 vp9_zero(cm->counts.partition);
2012 vp9_zero(cm->counts.intra_inter);
2013 vp9_zero(cm->counts.comp_inter);
2014 vp9_zero(cm->counts.single_ref);
2015 vp9_zero(cm->counts.comp_ref);
2016 vp9_zero(cm->counts.tx);
2017 vp9_zero(cm->counts.mbskip);
2019 // Note: this memset assumes above_context[0], [1] and [2]
2020 // are allocated as part of the same buffer.
2021 vpx_memset(cpi->above_context[0], 0,
2022 sizeof(*cpi->above_context[0]) *
2023 2 * aligned_mi_cols * MAX_MB_PLANE);
2024 vpx_memset(cpi->above_seg_context, 0,
2025 sizeof(*cpi->above_seg_context) * aligned_mi_cols);
2028 static void switch_lossless_mode(VP9_COMP *cpi, int lossless) {
2030 // printf("Switching to lossless\n");
2031 cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
2032 cpi->mb.e_mbd.itxm_add = vp9_iwht4x4_add;
2033 cpi->mb.optimize = 0;
2034 cpi->common.lf.filter_level = 0;
2035 cpi->zbin_mode_boost_enabled = 0;
2036 cpi->common.tx_mode = ONLY_4X4;
2038 // printf("Not lossless\n");
2039 cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
2040 cpi->mb.e_mbd.itxm_add = vp9_idct4x4_add;
2044 static void switch_tx_mode(VP9_COMP *cpi) {
2045 if (cpi->sf.tx_size_search_method == USE_LARGESTALL &&
2046 cpi->common.tx_mode >= ALLOW_32X32)
2047 cpi->common.tx_mode = ALLOW_32X32;
2050 static void encode_frame_internal(VP9_COMP *cpi) {
2052 MACROBLOCK * const x = &cpi->mb;
2053 VP9_COMMON * const cm = &cpi->common;
2054 MACROBLOCKD * const xd = &x->e_mbd;
2056 // fprintf(stderr, "encode_frame_internal frame %d (%d) type %d\n",
2057 // cpi->common.current_video_frame, cpi->common.show_frame,
2064 statsfile = fopen("segmap2.stt", "a");
2065 fprintf(statsfile, "\n");
2070 vp9_zero(cm->counts.switchable_interp);
2071 vp9_zero(cpi->tx_stepdown_count);
2073 xd->mi_8x8 = cm->mi_grid_visible;
2074 // required for vp9_frame_init_quantizer
2075 xd->mi_8x8[0] = cm->mi;
2077 xd->last_mi = cm->prev_mi;
2079 vp9_zero(cpi->common.counts.mv);
2080 vp9_zero(cpi->coef_counts);
2081 vp9_zero(cm->counts.eob_branch);
2083 cpi->mb.e_mbd.lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0
2084 && cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
2085 switch_lossless_mode(cpi, cpi->mb.e_mbd.lossless);
2087 vp9_frame_init_quantizer(cpi);
2089 vp9_initialize_rd_consts(cpi);
2090 vp9_initialize_me_consts(cpi, cm->base_qindex);
2091 switch_tx_mode(cpi);
2093 if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
2094 // Initialize encode frame context.
2095 init_encode_frame_mb_context(cpi);
2097 // Build a frame level activity map
2098 build_activity_map(cpi);
2101 // Re-initialize encode frame context.
2102 init_encode_frame_mb_context(cpi);
2104 vp9_zero(cpi->rd_comp_pred_diff);
2105 vp9_zero(cpi->rd_filter_diff);
2106 vp9_zero(cpi->rd_tx_select_diff);
2107 vp9_zero(cpi->rd_tx_select_threshes);
2112 struct vpx_usec_timer emr_timer;
2113 vpx_usec_timer_start(&emr_timer);
2116 // Take tiles into account and give start/end MB
2117 int tile_col, tile_row;
2118 TOKENEXTRA *tp = cpi->tok;
2119 const int tile_cols = 1 << cm->log2_tile_cols;
2120 const int tile_rows = 1 << cm->log2_tile_rows;
2122 for (tile_row = 0; tile_row < tile_rows; tile_row++) {
2123 for (tile_col = 0; tile_col < tile_cols; tile_col++) {
2125 TOKENEXTRA *tp_old = tp;
2127 // For each row of SBs in the frame
2128 vp9_tile_init(&tile, cm, tile_row, tile_col);
2129 for (mi_row = tile.mi_row_start;
2130 mi_row < tile.mi_row_end; mi_row += 8)
2131 encode_sb_row(cpi, &tile, mi_row, &tp);
2133 cpi->tok_count[tile_row][tile_col] = (unsigned int)(tp - tp_old);
2134 assert(tp - cpi->tok <= get_token_alloc(cm->mb_rows, cm->mb_cols));
2139 vpx_usec_timer_mark(&emr_timer);
2140 cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
2143 if (cpi->sf.skip_encode_sb) {
2145 unsigned int intra_count = 0, inter_count = 0;
2146 for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
2147 intra_count += cm->counts.intra_inter[j][0];
2148 inter_count += cm->counts.intra_inter[j][1];
2150 cpi->sf.skip_encode_frame = ((intra_count << 2) < inter_count);
2151 cpi->sf.skip_encode_frame &= (cm->frame_type != KEY_FRAME);
2152 cpi->sf.skip_encode_frame &= cm->show_frame;
2154 cpi->sf.skip_encode_frame = 0;
2158 // Keep record of the total distortion this time around for future use
2159 cpi->last_frame_distortion = cpi->frame_distortion;
2163 static int check_dual_ref_flags(VP9_COMP *cpi) {
2164 const int ref_flags = cpi->ref_frame_flags;
2166 if (vp9_segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
2169 return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG)
2170 + !!(ref_flags & VP9_ALT_FLAG)) >= 2;
2174 static int get_skip_flag(MODE_INFO **mi_8x8, int mis, int ymbs, int xmbs) {
2177 for (y = 0; y < ymbs; y++) {
2178 for (x = 0; x < xmbs; x++) {
2179 if (!mi_8x8[y * mis + x]->mbmi.skip_coeff)
2187 static void set_txfm_flag(MODE_INFO **mi_8x8, int mis, int ymbs, int xmbs,
2191 for (y = 0; y < ymbs; y++) {
2192 for (x = 0; x < xmbs; x++)
2193 mi_8x8[y * mis + x]->mbmi.tx_size = tx_size;
2197 static void reset_skip_txfm_size_b(VP9_COMP *cpi, MODE_INFO **mi_8x8,
2198 int mis, TX_SIZE max_tx_size, int bw, int bh,
2199 int mi_row, int mi_col, BLOCK_SIZE bsize) {
2200 VP9_COMMON * const cm = &cpi->common;
2202 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) {
2205 MB_MODE_INFO * const mbmi = &mi_8x8[0]->mbmi;
2206 if (mbmi->tx_size > max_tx_size) {
2207 const int ymbs = MIN(bh, cm->mi_rows - mi_row);
2208 const int xmbs = MIN(bw, cm->mi_cols - mi_col);
2210 assert(vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
2211 get_skip_flag(mi_8x8, mis, ymbs, xmbs));
2212 set_txfm_flag(mi_8x8, mis, ymbs, xmbs, max_tx_size);
2217 static void reset_skip_txfm_size_sb(VP9_COMP *cpi, MODE_INFO **mi_8x8,
2218 TX_SIZE max_tx_size, int mi_row, int mi_col,
2220 VP9_COMMON * const cm = &cpi->common;
2221 const int mis = cm->mode_info_stride;
2223 const int bs = num_8x8_blocks_wide_lookup[bsize], hbs = bs / 2;
2225 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
2228 bw = num_8x8_blocks_wide_lookup[mi_8x8[0]->mbmi.sb_type];
2229 bh = num_8x8_blocks_high_lookup[mi_8x8[0]->mbmi.sb_type];
2231 if (bw == bs && bh == bs) {
2232 reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, bs, bs, mi_row,
2234 } else if (bw == bs && bh < bs) {
2235 reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, bs, hbs, mi_row,
2237 reset_skip_txfm_size_b(cpi, mi_8x8 + hbs * mis, mis, max_tx_size, bs, hbs,
2238 mi_row + hbs, mi_col, bsize);
2239 } else if (bw < bs && bh == bs) {
2240 reset_skip_txfm_size_b(cpi, mi_8x8, mis, max_tx_size, hbs, bs, mi_row,
2242 reset_skip_txfm_size_b(cpi, mi_8x8 + hbs, mis, max_tx_size, hbs, bs, mi_row,
2243 mi_col + hbs, bsize);
2246 const BLOCK_SIZE subsize = subsize_lookup[PARTITION_SPLIT][bsize];
2249 assert(bw < bs && bh < bs);
2251 for (n = 0; n < 4; n++) {
2252 const int mi_dc = hbs * (n & 1);
2253 const int mi_dr = hbs * (n >> 1);
2255 reset_skip_txfm_size_sb(cpi, &mi_8x8[mi_dr * mis + mi_dc], max_tx_size,
2256 mi_row + mi_dr, mi_col + mi_dc, subsize);
2261 static void reset_skip_txfm_size(VP9_COMP *cpi, TX_SIZE txfm_max) {
2262 VP9_COMMON * const cm = &cpi->common;
2264 const int mis = cm->mode_info_stride;
2265 // MODE_INFO *mi, *mi_ptr = cm->mi;
2266 MODE_INFO **mi_8x8, **mi_ptr = cm->mi_grid_visible;
2268 for (mi_row = 0; mi_row < cm->mi_rows; mi_row += 8, mi_ptr += 8 * mis) {
2270 for (mi_col = 0; mi_col < cm->mi_cols; mi_col += 8, mi_8x8 += 8) {
2271 reset_skip_txfm_size_sb(cpi, mi_8x8, txfm_max, mi_row, mi_col,
2277 static int get_frame_type(VP9_COMP *cpi) {
2279 if (frame_is_intra_only(&cpi->common))
2281 else if (cpi->rc.is_src_frame_alt_ref && cpi->refresh_golden_frame)
2283 else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
2290 static void select_tx_mode(VP9_COMP *cpi) {
2291 if (cpi->oxcf.lossless) {
2292 cpi->common.tx_mode = ONLY_4X4;
2293 } else if (cpi->common.current_video_frame == 0) {
2294 cpi->common.tx_mode = TX_MODE_SELECT;
2296 if (cpi->sf.tx_size_search_method == USE_LARGESTALL) {
2297 cpi->common.tx_mode = ALLOW_32X32;
2298 } else if (cpi->sf.tx_size_search_method == USE_FULL_RD) {
2299 int frame_type = get_frame_type(cpi);
2300 cpi->common.tx_mode =
2301 cpi->rd_tx_select_threshes[frame_type][ALLOW_32X32]
2302 > cpi->rd_tx_select_threshes[frame_type][TX_MODE_SELECT] ?
2303 ALLOW_32X32 : TX_MODE_SELECT;
2305 unsigned int total = 0;
2307 for (i = 0; i < TX_SIZES; ++i)
2308 total += cpi->tx_stepdown_count[i];
2310 double fraction = (double)cpi->tx_stepdown_count[0] / total;
2311 cpi->common.tx_mode = fraction > 0.90 ? ALLOW_32X32 : TX_MODE_SELECT;
2312 // printf("fraction = %f\n", fraction);
2313 } // else keep unchanged
2318 void vp9_encode_frame(VP9_COMP *cpi) {
2319 VP9_COMMON * const cm = &cpi->common;
2321 // In the longer term the encoder should be generalized to match the
2322 // decoder such that we allow compound where one of the 3 buffers has a
2323 // different sign bias and that buffer is then the fixed ref. However, this
2324 // requires further work in the rd loop. For now the only supported encoder
2325 // side behavior is where the ALT ref buffer has opposite sign bias to
2327 if (!frame_is_intra_only(cm)) {
2328 if ((cm->ref_frame_sign_bias[ALTREF_FRAME]
2329 == cm->ref_frame_sign_bias[GOLDEN_FRAME])
2330 || (cm->ref_frame_sign_bias[ALTREF_FRAME]
2331 == cm->ref_frame_sign_bias[LAST_FRAME])) {
2332 cm->allow_comp_inter_inter = 0;
2334 cm->allow_comp_inter_inter = 1;
2335 cm->comp_fixed_ref = ALTREF_FRAME;
2336 cm->comp_var_ref[0] = LAST_FRAME;
2337 cm->comp_var_ref[1] = GOLDEN_FRAME;
2343 REFERENCE_MODE reference_mode;
2344 INTERPOLATION_TYPE filter_type;
2346 * This code does a single RD pass over the whole frame assuming
2347 * either compound, single or hybrid prediction as per whatever has
2348 * worked best for that type of frame in the past.
2349 * It also predicts whether another coding mode would have worked
2350 * better that this coding mode. If that is the case, it remembers
2351 * that for subsequent frames.
2352 * It does the same analysis for transform size selection also.
2354 const int frame_type = get_frame_type(cpi);
2355 const int64_t *mode_thresh = cpi->rd_prediction_type_threshes[frame_type];
2356 const int64_t *filter_thresh = cpi->rd_filter_threshes[frame_type];
2358 /* prediction (compound, single or hybrid) mode selection */
2359 if (frame_type == 3 || !cm->allow_comp_inter_inter)
2360 reference_mode = SINGLE_REFERENCE;
2361 else if (mode_thresh[COMPOUND_REFERENCE] > mode_thresh[SINGLE_REFERENCE] &&
2362 mode_thresh[COMPOUND_REFERENCE] >
2363 mode_thresh[REFERENCE_MODE_SELECT] &&
2364 check_dual_ref_flags(cpi) &&
2365 cpi->static_mb_pct == 100)
2366 reference_mode = COMPOUND_REFERENCE;
2367 else if (mode_thresh[SINGLE_REFERENCE] > mode_thresh[REFERENCE_MODE_SELECT])
2368 reference_mode = SINGLE_REFERENCE;
2370 reference_mode = REFERENCE_MODE_SELECT;
2372 /* filter type selection */
2373 // FIXME(rbultje) for some odd reason, we often select smooth_filter
2374 // as default filter for ARF overlay frames. This is a REALLY BAD
2375 // IDEA so we explicitly disable it here.
2376 if (frame_type != 3 &&
2377 filter_thresh[EIGHTTAP_SMOOTH] > filter_thresh[EIGHTTAP] &&
2378 filter_thresh[EIGHTTAP_SMOOTH] > filter_thresh[EIGHTTAP_SHARP] &&
2379 filter_thresh[EIGHTTAP_SMOOTH] > filter_thresh[SWITCHABLE - 1]) {
2380 filter_type = EIGHTTAP_SMOOTH;
2381 } else if (filter_thresh[EIGHTTAP_SHARP] > filter_thresh[EIGHTTAP] &&
2382 filter_thresh[EIGHTTAP_SHARP] > filter_thresh[SWITCHABLE - 1]) {
2383 filter_type = EIGHTTAP_SHARP;
2384 } else if (filter_thresh[EIGHTTAP] > filter_thresh[SWITCHABLE - 1]) {
2385 filter_type = EIGHTTAP;
2387 filter_type = SWITCHABLE;
2390 cpi->mb.e_mbd.lossless = cpi->oxcf.lossless;
2392 /* transform size selection (4x4, 8x8, 16x16 or select-per-mb) */
2393 select_tx_mode(cpi);
2394 cpi->common.reference_mode = reference_mode;
2395 cpi->common.mcomp_filter_type = filter_type;
2396 encode_frame_internal(cpi);
2398 for (i = 0; i < REFERENCE_MODES; ++i) {
2399 const int diff = (int) (cpi->rd_comp_pred_diff[i] / cpi->common.MBs);
2400 cpi->rd_prediction_type_threshes[frame_type][i] += diff;
2401 cpi->rd_prediction_type_threshes[frame_type][i] >>= 1;
2404 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) {
2405 const int64_t diff = cpi->rd_filter_diff[i] / cpi->common.MBs;
2406 cpi->rd_filter_threshes[frame_type][i] =
2407 (cpi->rd_filter_threshes[frame_type][i] + diff) / 2;
2410 for (i = 0; i < TX_MODES; ++i) {
2411 int64_t pd = cpi->rd_tx_select_diff[i];
2413 if (i == TX_MODE_SELECT)
2414 pd -= RDCOST(cpi->mb.rdmult, cpi->mb.rddiv,
2415 2048 * (TX_SIZES - 1), 0);
2416 diff = (int) (pd / cpi->common.MBs);
2417 cpi->rd_tx_select_threshes[frame_type][i] += diff;
2418 cpi->rd_tx_select_threshes[frame_type][i] /= 2;
2421 if (cpi->common.reference_mode == REFERENCE_MODE_SELECT) {
2422 int single_count_zero = 0;
2423 int comp_count_zero = 0;
2425 for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
2426 single_count_zero += cm->counts.comp_inter[i][0];
2427 comp_count_zero += cm->counts.comp_inter[i][1];
2430 if (comp_count_zero == 0) {
2431 cpi->common.reference_mode = SINGLE_REFERENCE;
2432 vp9_zero(cm->counts.comp_inter);
2433 } else if (single_count_zero == 0) {
2434 cpi->common.reference_mode = COMPOUND_REFERENCE;
2435 vp9_zero(cm->counts.comp_inter);
2439 if (cpi->common.tx_mode == TX_MODE_SELECT) {
2441 int count8x8_lp = 0, count8x8_8x8p = 0;
2442 int count16x16_16x16p = 0, count16x16_lp = 0;
2445 for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
2446 count4x4 += cm->counts.tx.p32x32[i][TX_4X4];
2447 count4x4 += cm->counts.tx.p16x16[i][TX_4X4];
2448 count4x4 += cm->counts.tx.p8x8[i][TX_4X4];
2450 count8x8_lp += cm->counts.tx.p32x32[i][TX_8X8];
2451 count8x8_lp += cm->counts.tx.p16x16[i][TX_8X8];
2452 count8x8_8x8p += cm->counts.tx.p8x8[i][TX_8X8];
2454 count16x16_16x16p += cm->counts.tx.p16x16[i][TX_16X16];
2455 count16x16_lp += cm->counts.tx.p32x32[i][TX_16X16];
2456 count32x32 += cm->counts.tx.p32x32[i][TX_32X32];
2459 if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0
2460 && count32x32 == 0) {
2461 cpi->common.tx_mode = ALLOW_8X8;
2462 reset_skip_txfm_size(cpi, TX_8X8);
2463 } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0
2464 && count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
2465 cpi->common.tx_mode = ONLY_4X4;
2466 reset_skip_txfm_size(cpi, TX_4X4);
2467 } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
2468 cpi->common.tx_mode = ALLOW_32X32;
2469 } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
2470 cpi->common.tx_mode = ALLOW_16X16;
2471 reset_skip_txfm_size(cpi, TX_16X16);
2475 encode_frame_internal(cpi);
2479 static void sum_intra_stats(VP9_COMMON *cm, const MODE_INFO *mi) {
2480 const MB_PREDICTION_MODE y_mode = mi->mbmi.mode;
2481 const MB_PREDICTION_MODE uv_mode = mi->mbmi.uv_mode;
2482 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
2484 ++cm->counts.uv_mode[y_mode][uv_mode];
2486 if (bsize < BLOCK_8X8) {
2488 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
2489 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
2490 for (idy = 0; idy < 2; idy += num_4x4_blocks_high)
2491 for (idx = 0; idx < 2; idx += num_4x4_blocks_wide)
2492 ++cm->counts.y_mode[0][mi->bmi[idy * 2 + idx].as_mode];
2494 ++cm->counts.y_mode[size_group_lookup[bsize]][y_mode];
2498 // Experimental stub function to create a per MB zbin adjustment based on
2499 // some previously calculated measure of MB activity.
2500 static void adjust_act_zbin(VP9_COMP *cpi, MACROBLOCK *x) {
2502 x->act_zbin_adj = *(x->mb_activity_ptr);
2506 int64_t act = *(x->mb_activity_ptr);
2508 // Apply the masking to the RD multiplier.
2509 a = act + 4 * cpi->activity_avg;
2510 b = 4 * act + cpi->activity_avg;
2512 if (act > cpi->activity_avg)
2513 x->act_zbin_adj = (int) (((int64_t) b + (a >> 1)) / a) - 1;
2515 x->act_zbin_adj = 1 - (int) (((int64_t) a + (b >> 1)) / b);
2519 static int get_zbin_mode_boost(MB_MODE_INFO *mbmi, int enabled) {
2521 if (is_inter_block(mbmi)) {
2522 if (mbmi->mode == ZEROMV) {
2523 return mbmi->ref_frame[0] != LAST_FRAME ? GF_ZEROMV_ZBIN_BOOST
2524 : LF_ZEROMV_ZBIN_BOOST;
2526 return mbmi->sb_type < BLOCK_8X8 ? SPLIT_MV_ZBIN_BOOST
2530 return INTRA_ZBIN_BOOST;
2537 static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled,
2538 int mi_row, int mi_col, BLOCK_SIZE bsize) {
2539 VP9_COMMON * const cm = &cpi->common;
2540 MACROBLOCK * const x = &cpi->mb;
2541 MACROBLOCKD * const xd = &x->e_mbd;
2542 MODE_INFO **mi_8x8 = xd->mi_8x8;
2543 MODE_INFO *mi = mi_8x8[0];
2544 MB_MODE_INFO *mbmi = &mi->mbmi;
2545 PICK_MODE_CONTEXT *ctx = get_block_context(x, bsize);
2546 unsigned int segment_id = mbmi->segment_id;
2547 const int mis = cm->mode_info_stride;
2548 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
2549 const int mi_height = num_8x8_blocks_high_lookup[bsize];
2550 x->skip_recode = !x->select_txfm_size && mbmi->sb_type >= BLOCK_8X8 &&
2551 (cpi->oxcf.aq_mode != COMPLEXITY_AQ);
2552 x->skip_optimize = ctx->is_coded;
2554 x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
2555 x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
2556 x->q_index < QIDX_SKIP_THRESH);
2560 if (cm->frame_type == KEY_FRAME) {
2561 if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
2562 adjust_act_zbin(cpi, x);
2563 vp9_update_zbin_extra(cpi, x);
2566 vp9_setup_interp_filters(xd, mbmi->interp_filter, cm);
2568 if (cpi->oxcf.tuning == VP8_TUNE_SSIM) {
2569 // Adjust the zbin based on this MB rate.
2570 adjust_act_zbin(cpi, x);
2573 // Experimental code. Special case for gf and arf zeromv modes.
2574 // Increase zbin size to suppress noise
2575 cpi->zbin_mode_boost = get_zbin_mode_boost(mbmi,
2576 cpi->zbin_mode_boost_enabled);
2577 vp9_update_zbin_extra(cpi, x);
2580 if (!is_inter_block(mbmi)) {
2581 vp9_encode_intra_block_y(x, MAX(bsize, BLOCK_8X8));
2582 vp9_encode_intra_block_uv(x, MAX(bsize, BLOCK_8X8));
2584 sum_intra_stats(cm, mi);
2586 int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, mbmi->ref_frame[0])];
2587 YV12_BUFFER_CONFIG *ref_fb = &cm->yv12_fb[idx];
2588 YV12_BUFFER_CONFIG *second_ref_fb = NULL;
2589 if (has_second_ref(mbmi)) {
2590 idx = cm->ref_frame_map[get_ref_frame_idx(cpi, mbmi->ref_frame[1])];
2591 second_ref_fb = &cm->yv12_fb[idx];
2594 assert(cm->frame_type != KEY_FRAME);
2596 setup_pre_planes(xd, 0, ref_fb, mi_row, mi_col, xd->scale_factors[0]);
2597 setup_pre_planes(xd, 1, second_ref_fb, mi_row, mi_col,
2598 xd->scale_factors[1]);
2600 vp9_build_inter_predictors_sb(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8));
2603 if (!is_inter_block(mbmi)) {
2604 vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8));
2605 } else if (!x->skip) {
2606 vp9_encode_sb(x, MAX(bsize, BLOCK_8X8));
2607 vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8));
2609 int mb_skip_context = xd->left_available ? mi_8x8[-1]->mbmi.skip_coeff : 0;
2610 mb_skip_context += mi_8x8[-mis] ? mi_8x8[-mis]->mbmi.skip_coeff : 0;
2612 mbmi->skip_coeff = 1;
2614 cm->counts.mbskip[mb_skip_context][1]++;
2615 reset_skip_context(xd, MAX(bsize, BLOCK_8X8));
2618 if (output_enabled) {
2619 if (cm->tx_mode == TX_MODE_SELECT &&
2620 mbmi->sb_type >= BLOCK_8X8 &&
2621 !(is_inter_block(mbmi) &&
2622 (mbmi->skip_coeff ||
2623 vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)))) {
2624 ++get_tx_counts(max_txsize_lookup[bsize], vp9_get_tx_size_context(xd),
2625 &cm->counts.tx)[mbmi->tx_size];
2629 // The new intra coding scheme requires no change of transform size
2630 if (is_inter_block(&mi->mbmi)) {
2631 tx_size = MIN(tx_mode_to_biggest_tx_size[cm->tx_mode],
2632 max_txsize_lookup[bsize]);
2634 tx_size = (bsize >= BLOCK_8X8) ? mbmi->tx_size : TX_4X4;
2637 for (y = 0; y < mi_height; y++)
2638 for (x = 0; x < mi_width; x++)
2639 if (mi_col + x < cm->mi_cols && mi_row + y < cm->mi_rows)
2640 mi_8x8[mis * y + x]->mbmi.tx_size = tx_size;