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_systemdependent.h"
31 #include "vp9/common/vp9_tile_common.h"
33 #include "vp9/encoder/vp9_aq_complexity.h"
34 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
35 #include "vp9/encoder/vp9_aq_variance.h"
36 #include "vp9/encoder/vp9_encodeframe.h"
37 #include "vp9/encoder/vp9_encodemb.h"
38 #include "vp9/encoder/vp9_encodemv.h"
39 #include "vp9/encoder/vp9_ethread.h"
40 #include "vp9/encoder/vp9_extend.h"
41 #include "vp9/encoder/vp9_pickmode.h"
42 #include "vp9/encoder/vp9_rd.h"
43 #include "vp9/encoder/vp9_rdopt.h"
44 #include "vp9/encoder/vp9_segmentation.h"
45 #include "vp9/encoder/vp9_tokenize.h"
47 static void encode_superblock(VP9_COMP *cpi, ThreadData * td,
48 TOKENEXTRA **t, int output_enabled,
49 int mi_row, int mi_col, BLOCK_SIZE bsize,
50 PICK_MODE_CONTEXT *ctx);
52 // This is used as a reference when computing the source variance for the
53 // purposes of activity masking.
54 // Eventually this should be replaced by custom no-reference routines,
55 // which will be faster.
56 static const uint8_t VP9_VAR_OFFS[64] = {
57 128, 128, 128, 128, 128, 128, 128, 128,
58 128, 128, 128, 128, 128, 128, 128, 128,
59 128, 128, 128, 128, 128, 128, 128, 128,
60 128, 128, 128, 128, 128, 128, 128, 128,
61 128, 128, 128, 128, 128, 128, 128, 128,
62 128, 128, 128, 128, 128, 128, 128, 128,
63 128, 128, 128, 128, 128, 128, 128, 128,
64 128, 128, 128, 128, 128, 128, 128, 128
67 #if CONFIG_VP9_HIGHBITDEPTH
68 static const uint16_t VP9_HIGH_VAR_OFFS_8[64] = {
69 128, 128, 128, 128, 128, 128, 128, 128,
70 128, 128, 128, 128, 128, 128, 128, 128,
71 128, 128, 128, 128, 128, 128, 128, 128,
72 128, 128, 128, 128, 128, 128, 128, 128,
73 128, 128, 128, 128, 128, 128, 128, 128,
74 128, 128, 128, 128, 128, 128, 128, 128,
75 128, 128, 128, 128, 128, 128, 128, 128,
76 128, 128, 128, 128, 128, 128, 128, 128
79 static const uint16_t VP9_HIGH_VAR_OFFS_10[64] = {
80 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
81 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
82 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
83 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
84 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
85 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
86 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
87 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4
90 static const uint16_t VP9_HIGH_VAR_OFFS_12[64] = {
91 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
92 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
93 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
94 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
95 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
96 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
97 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
98 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16
100 #endif // CONFIG_VP9_HIGHBITDEPTH
102 unsigned int vp9_get_sby_perpixel_variance(VP9_COMP *cpi,
103 const struct buf_2d *ref,
106 const unsigned int var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
107 VP9_VAR_OFFS, 0, &sse);
108 return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
111 #if CONFIG_VP9_HIGHBITDEPTH
112 unsigned int vp9_high_get_sby_perpixel_variance(
113 VP9_COMP *cpi, const struct buf_2d *ref, BLOCK_SIZE bs, int bd) {
114 unsigned int var, sse;
117 var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
118 CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10),
122 var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
123 CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12),
128 var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
129 CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8),
133 return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
135 #endif // CONFIG_VP9_HIGHBITDEPTH
137 static unsigned int get_sby_perpixel_diff_variance(VP9_COMP *cpi,
138 const struct buf_2d *ref,
139 int mi_row, int mi_col,
141 unsigned int sse, var;
143 const YV12_BUFFER_CONFIG *last = get_ref_frame_buffer(cpi, LAST_FRAME);
145 assert(last != NULL);
147 &last->y_buffer[mi_row * MI_SIZE * last->y_stride + mi_col * MI_SIZE];
148 var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride, last_y, last->y_stride, &sse);
149 return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
152 static BLOCK_SIZE get_rd_var_based_fixed_partition(VP9_COMP *cpi, MACROBLOCK *x,
155 unsigned int var = get_sby_perpixel_diff_variance(cpi, &x->plane[0].src,
168 // Lighter version of set_offsets that only sets the mode info
170 static INLINE void set_mode_info_offsets(VP9_COMMON *const cm,
171 MACROBLOCKD *const xd,
174 const int idx_str = xd->mi_stride * mi_row + mi_col;
175 xd->mi = cm->mi + idx_str;
176 xd->mi[0].src_mi = &xd->mi[0];
179 static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile,
180 MACROBLOCK *const x, int mi_row, int mi_col,
182 VP9_COMMON *const cm = &cpi->common;
183 MACROBLOCKD *const xd = &x->e_mbd;
185 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
186 const int mi_height = num_8x8_blocks_high_lookup[bsize];
187 const struct segmentation *const seg = &cm->seg;
189 set_skip_context(xd, mi_row, mi_col);
191 set_mode_info_offsets(cm, xd, mi_row, mi_col);
193 mbmi = &xd->mi[0].src_mi->mbmi;
195 // Set up destination pointers.
196 vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
198 // Set up limit values for MV components.
199 // Mv beyond the range do not produce new/different prediction block.
200 x->mv_row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
201 x->mv_col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
202 x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
203 x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;
205 // Set up distance of MB to edge of frame in 1/8th pel units.
206 assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
207 set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
208 cm->mi_rows, cm->mi_cols);
210 // Set up source buffers.
211 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
214 x->rddiv = cpi->rd.RDDIV;
215 x->rdmult = cpi->rd.RDMULT;
219 if (cpi->oxcf.aq_mode != VARIANCE_AQ) {
220 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
221 : cm->last_frame_seg_map;
222 mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
224 vp9_init_plane_quantizers(cpi, x);
226 x->encode_breakout = cpi->segment_encode_breakout[mbmi->segment_id];
228 mbmi->segment_id = 0;
229 x->encode_breakout = cpi->encode_breakout;
233 static void duplicate_mode_info_in_sb(VP9_COMMON *cm, MACROBLOCKD *xd,
234 int mi_row, int mi_col,
236 const int block_width = num_8x8_blocks_wide_lookup[bsize];
237 const int block_height = num_8x8_blocks_high_lookup[bsize];
239 for (j = 0; j < block_height; ++j)
240 for (i = 0; i < block_width; ++i) {
241 if (mi_row + j < cm->mi_rows && mi_col + i < cm->mi_cols)
242 xd->mi[j * xd->mi_stride + i].src_mi = &xd->mi[0];
246 static void set_block_size(VP9_COMP * const cpi,
247 MACROBLOCKD *const xd,
248 int mi_row, int mi_col,
250 if (cpi->common.mi_cols > mi_col && cpi->common.mi_rows > mi_row) {
251 set_mode_info_offsets(&cpi->common, xd, mi_row, mi_col);
252 xd->mi[0].src_mi->mbmi.sb_type = bsize;
257 int64_t sum_square_error;
267 } partition_variance;
270 partition_variance part_variances;
275 partition_variance part_variances;
280 partition_variance part_variances;
285 partition_variance part_variances;
290 partition_variance part_variances;
295 partition_variance *part_variances;
305 static void tree_to_node(void *data, BLOCK_SIZE bsize, variance_node *node) {
307 node->part_variances = NULL;
310 v64x64 *vt = (v64x64 *) data;
311 node->part_variances = &vt->part_variances;
312 for (i = 0; i < 4; i++)
313 node->split[i] = &vt->split[i].part_variances.none;
317 v32x32 *vt = (v32x32 *) data;
318 node->part_variances = &vt->part_variances;
319 for (i = 0; i < 4; i++)
320 node->split[i] = &vt->split[i].part_variances.none;
324 v16x16 *vt = (v16x16 *) data;
325 node->part_variances = &vt->part_variances;
326 for (i = 0; i < 4; i++)
327 node->split[i] = &vt->split[i].part_variances.none;
331 v8x8 *vt = (v8x8 *) data;
332 node->part_variances = &vt->part_variances;
333 for (i = 0; i < 4; i++)
334 node->split[i] = &vt->split[i].part_variances.none;
338 v4x4 *vt = (v4x4 *) data;
339 node->part_variances = &vt->part_variances;
340 for (i = 0; i < 4; i++)
341 node->split[i] = &vt->split[i];
351 // Set variance values given sum square error, sum error, count.
352 static void fill_variance(int64_t s2, int64_t s, int c, var *v) {
353 v->sum_square_error = s2;
358 static void get_variance(var *v) {
359 v->variance = (int)(256 * (v->sum_square_error -
360 ((v->sum_error * v->sum_error) >> v->log2_count)) >> v->log2_count);
363 void sum_2_variances(const var *a, const var *b, var *r) {
364 assert(a->log2_count == b->log2_count);
365 fill_variance(a->sum_square_error + b->sum_square_error,
366 a->sum_error + b->sum_error, a->log2_count + 1, r);
369 static void fill_variance_tree(void *data, BLOCK_SIZE bsize) {
371 tree_to_node(data, bsize, &node);
372 sum_2_variances(node.split[0], node.split[1], &node.part_variances->horz[0]);
373 sum_2_variances(node.split[2], node.split[3], &node.part_variances->horz[1]);
374 sum_2_variances(node.split[0], node.split[2], &node.part_variances->vert[0]);
375 sum_2_variances(node.split[1], node.split[3], &node.part_variances->vert[1]);
376 sum_2_variances(&node.part_variances->vert[0], &node.part_variances->vert[1],
377 &node.part_variances->none);
380 static int set_vt_partitioning(VP9_COMP *cpi,
381 MACROBLOCKD *const xd,
387 BLOCK_SIZE bsize_min,
389 VP9_COMMON * const cm = &cpi->common;
391 const int block_width = num_8x8_blocks_wide_lookup[bsize];
392 const int block_height = num_8x8_blocks_high_lookup[bsize];
394 assert(block_height == block_width);
395 tree_to_node(data, bsize, &vt);
400 // For bsize=bsize_min (16x16/8x8 for 8x8/4x4 downsampling), select if
401 // variance is below threshold, otherwise split will be selected.
402 // No check for vert/horiz split as too few samples for variance.
403 if (bsize == bsize_min) {
404 get_variance(&vt.part_variances->none);
405 if (mi_col + block_width / 2 < cm->mi_cols &&
406 mi_row + block_height / 2 < cm->mi_rows &&
407 vt.part_variances->none.variance < threshold) {
408 set_block_size(cpi, xd, mi_row, mi_col, bsize);
412 } else if (bsize > bsize_min) {
413 // Variance is already computed for 32x32 blocks to set the force_split.
414 if (bsize != BLOCK_32X32)
415 get_variance(&vt.part_variances->none);
416 // For key frame or low_res: for bsize above 32X32 or very high variance,
418 if (cm->frame_type == KEY_FRAME &&
419 (bsize > BLOCK_32X32 ||
420 vt.part_variances->none.variance > (threshold << 4))) {
423 // If variance is low, take the bsize (no split).
424 if (mi_col + block_width / 2 < cm->mi_cols &&
425 mi_row + block_height / 2 < cm->mi_rows &&
426 vt.part_variances->none.variance < threshold) {
427 set_block_size(cpi, xd, mi_row, mi_col, bsize);
431 // Check vertical split.
432 if (mi_row + block_height / 2 < cm->mi_rows) {
433 get_variance(&vt.part_variances->vert[0]);
434 get_variance(&vt.part_variances->vert[1]);
435 if (vt.part_variances->vert[0].variance < threshold &&
436 vt.part_variances->vert[1].variance < threshold) {
437 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_VERT);
438 set_block_size(cpi, xd, mi_row, mi_col, subsize);
439 set_block_size(cpi, xd, mi_row, mi_col + block_width / 2, subsize);
443 // Check horizontal split.
444 if (mi_col + block_width / 2 < cm->mi_cols) {
445 get_variance(&vt.part_variances->horz[0]);
446 get_variance(&vt.part_variances->horz[1]);
447 if (vt.part_variances->horz[0].variance < threshold &&
448 vt.part_variances->horz[1].variance < threshold) {
449 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_HORZ);
450 set_block_size(cpi, xd, mi_row, mi_col, subsize);
451 set_block_size(cpi, xd, mi_row + block_height / 2, mi_col, subsize);
462 void vp9_set_vbp_thresholds(VP9_COMP *cpi, int64_t thresholds[], int q) {
463 SPEED_FEATURES *const sf = &cpi->sf;
464 if (sf->partition_search_type != VAR_BASED_PARTITION &&
465 sf->partition_search_type != REFERENCE_PARTITION) {
468 VP9_COMMON *const cm = &cpi->common;
469 const int is_key_frame = (cm->frame_type == KEY_FRAME);
470 const int threshold_multiplier = is_key_frame ? 20 : 1;
471 const int64_t threshold_base = (int64_t)(threshold_multiplier *
472 cpi->y_dequant[q][1]);
474 // TODO(marpan): Allow 4x4 partitions for inter-frames.
475 // use_4x4_partition = (variance4x4downsample[i2 + j] == 1);
476 // If 4x4 partition is not used, then 8x8 partition will be selected
477 // if variance of 16x16 block is very high, so use larger threshold
478 // for 16x16 (threshold_bsize_min) in that case.
480 // Array index: 0 - threshold_64x64; 1 - threshold_32x32;
481 // 2 - threshold_16x16; 3 - vbp_threshold_8x8;
483 thresholds[0] = threshold_base;
484 thresholds[1] = threshold_base >> 2;
485 thresholds[2] = threshold_base >> 2;
486 thresholds[3] = threshold_base << 2;
487 cpi->vbp_bsize_min = BLOCK_8X8;
489 thresholds[1] = threshold_base;
490 if (cm->width <= 352 && cm->height <= 288) {
491 thresholds[0] = threshold_base >> 2;
492 thresholds[2] = threshold_base << 3;
494 thresholds[0] = threshold_base;
495 thresholds[2] = threshold_base << cpi->oxcf.speed;
497 cpi->vbp_bsize_min = BLOCK_16X16;
502 static void fill_variance_4x4avg(const uint8_t *s, int sp, const uint8_t *d,
503 int dp, int x8_idx, int y8_idx, v8x8 *vst,
504 #if CONFIG_VP9_HIGHBITDEPTH
511 for (k = 0; k < 4; k++) {
512 int x4_idx = x8_idx + ((k & 1) << 2);
513 int y4_idx = y8_idx + ((k >> 1) << 2);
514 unsigned int sse = 0;
516 if (x4_idx < pixels_wide && y4_idx < pixels_high) {
519 #if CONFIG_VP9_HIGHBITDEPTH
520 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
521 s_avg = vp9_highbd_avg_4x4(s + y4_idx * sp + x4_idx, sp);
523 d_avg = vp9_highbd_avg_4x4(d + y4_idx * dp + x4_idx, dp);
525 s_avg = vp9_avg_4x4(s + y4_idx * sp + x4_idx, sp);
527 d_avg = vp9_avg_4x4(d + y4_idx * dp + x4_idx, dp);
530 s_avg = vp9_avg_4x4(s + y4_idx * sp + x4_idx, sp);
532 d_avg = vp9_avg_4x4(d + y4_idx * dp + x4_idx, dp);
537 fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
541 static void fill_variance_8x8avg(const uint8_t *s, int sp, const uint8_t *d,
542 int dp, int x16_idx, int y16_idx, v16x16 *vst,
543 #if CONFIG_VP9_HIGHBITDEPTH
550 for (k = 0; k < 4; k++) {
551 int x8_idx = x16_idx + ((k & 1) << 3);
552 int y8_idx = y16_idx + ((k >> 1) << 3);
553 unsigned int sse = 0;
555 if (x8_idx < pixels_wide && y8_idx < pixels_high) {
558 #if CONFIG_VP9_HIGHBITDEPTH
559 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
560 s_avg = vp9_highbd_avg_8x8(s + y8_idx * sp + x8_idx, sp);
562 d_avg = vp9_highbd_avg_8x8(d + y8_idx * dp + x8_idx, dp);
564 s_avg = vp9_avg_8x8(s + y8_idx * sp + x8_idx, sp);
566 d_avg = vp9_avg_8x8(d + y8_idx * dp + x8_idx, dp);
569 s_avg = vp9_avg_8x8(s + y8_idx * sp + x8_idx, sp);
571 d_avg = vp9_avg_8x8(d + y8_idx * dp + x8_idx, dp);
576 fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
580 // This function chooses partitioning based on the variance between source and
581 // reconstructed last, where variance is computed for down-sampled inputs.
582 static void choose_partitioning(VP9_COMP *cpi,
583 const TileInfo *const tile,
585 int mi_row, int mi_col) {
586 VP9_COMMON * const cm = &cpi->common;
587 MACROBLOCKD *xd = &x->e_mbd;
596 int pixels_wide = 64, pixels_high = 64;
597 int64_t thresholds[4] = {cpi->vbp_thresholds[0], cpi->vbp_thresholds[1],
598 cpi->vbp_thresholds[2], cpi->vbp_thresholds[3]};
600 // Always use 4x4 partition for key frame.
601 const int is_key_frame = (cm->frame_type == KEY_FRAME);
602 const int use_4x4_partition = is_key_frame;
603 const int low_res = (cm->width <= 352 && cm->height <= 288);
604 int variance4x4downsample[16];
606 int segment_id = CR_SEGMENT_ID_BASE;
607 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
608 const uint8_t *const map = cm->seg.update_map ? cpi->segmentation_map :
609 cm->last_frame_seg_map;
610 segment_id = vp9_get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
612 if (cyclic_refresh_segment_id_boosted(segment_id)) {
613 int q = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
614 vp9_set_vbp_thresholds(cpi, thresholds, q);
618 set_offsets(cpi, tile, x, mi_row, mi_col, BLOCK_64X64);
620 if (xd->mb_to_right_edge < 0)
621 pixels_wide += (xd->mb_to_right_edge >> 3);
622 if (xd->mb_to_bottom_edge < 0)
623 pixels_high += (xd->mb_to_bottom_edge >> 3);
625 s = x->plane[0].src.buf;
626 sp = x->plane[0].src.stride;
629 MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
631 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
633 const YV12_BUFFER_CONFIG *yv12_g = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
634 unsigned int y_sad, y_sad_g;
635 const BLOCK_SIZE bsize = BLOCK_32X32
636 + (mi_col + 4 < cm->mi_cols) * 2 + (mi_row + 4 < cm->mi_rows);
638 assert(yv12 != NULL);
639 if (yv12_g && yv12_g != yv12) {
640 vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
641 &cm->frame_refs[GOLDEN_FRAME - 1].sf);
642 y_sad_g = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf,
643 x->plane[0].src.stride,
644 xd->plane[0].pre[0].buf,
645 xd->plane[0].pre[0].stride);
650 vp9_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
651 &cm->frame_refs[LAST_FRAME - 1].sf);
652 mbmi->ref_frame[0] = LAST_FRAME;
653 mbmi->ref_frame[1] = NONE;
654 mbmi->sb_type = BLOCK_64X64;
655 mbmi->mv[0].as_int = 0;
656 mbmi->interp_filter = BILINEAR;
658 y_sad = vp9_int_pro_motion_estimation(cpi, x, bsize);
659 if (y_sad_g < y_sad) {
660 vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
661 &cm->frame_refs[GOLDEN_FRAME - 1].sf);
662 mbmi->ref_frame[0] = GOLDEN_FRAME;
663 mbmi->mv[0].as_int = 0;
666 x->pred_mv[LAST_FRAME] = mbmi->mv[0].as_mv;
669 vp9_build_inter_predictors_sb(xd, mi_row, mi_col, BLOCK_64X64);
671 for (i = 1; i <= 2; ++i) {
672 struct macroblock_plane *p = &x->plane[i];
673 struct macroblockd_plane *pd = &xd->plane[i];
674 const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
676 if (bs == BLOCK_INVALID)
679 uv_sad = cpi->fn_ptr[bs].sdf(p->src.buf, p->src.stride,
680 pd->dst.buf, pd->dst.stride);
682 x->color_sensitivity[i - 1] = uv_sad > (y_sad >> 2);
685 d = xd->plane[0].dst.buf;
686 dp = xd->plane[0].dst.stride;
690 #if CONFIG_VP9_HIGHBITDEPTH
691 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
694 d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10);
697 d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12);
701 d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8);
705 #endif // CONFIG_VP9_HIGHBITDEPTH
708 // Index for force_split: 0 for 64x64, 1-4 for 32x32 blocks,
710 // Fill in the entire tree of 8x8 (or 4x4 under some conditions) variances
712 for (i = 0; i < 4; i++) {
713 const int x32_idx = ((i & 1) << 5);
714 const int y32_idx = ((i >> 1) << 5);
715 const int i2 = i << 2;
716 force_split[i + 1] = 0;
717 for (j = 0; j < 4; j++) {
718 const int x16_idx = x32_idx + ((j & 1) << 4);
719 const int y16_idx = y32_idx + ((j >> 1) << 4);
720 v16x16 *vst = &vt.split[i].split[j];
721 variance4x4downsample[i2 + j] = 0;
723 fill_variance_8x8avg(s, sp, d, dp, x16_idx, y16_idx, vst,
724 #if CONFIG_VP9_HIGHBITDEPTH
730 fill_variance_tree(&vt.split[i].split[j], BLOCK_16X16);
731 // For low-resolution, compute the variance based on 8x8 down-sampling,
732 // and if it is large (above the threshold) we go down for 4x4.
733 // For key frame we always go down to 4x4.
735 get_variance(&vt.split[i].split[j].part_variances.none);
737 if (is_key_frame || (low_res &&
738 vt.split[i].split[j].part_variances.none.variance >
739 (thresholds[1] << 1))) {
740 // Go down to 4x4 down-sampling for variance.
741 variance4x4downsample[i2 + j] = 1;
742 for (k = 0; k < 4; k++) {
743 int x8_idx = x16_idx + ((k & 1) << 3);
744 int y8_idx = y16_idx + ((k >> 1) << 3);
745 v8x8 *vst2 = is_key_frame ? &vst->split[k] :
746 &vt2[i2 + j].split[k];
747 fill_variance_4x4avg(s, sp, d, dp, x8_idx, y8_idx, vst2,
748 #if CONFIG_VP9_HIGHBITDEPTH
759 // Fill the rest of the variance tree by summing split partition values.
760 for (i = 0; i < 4; i++) {
761 const int i2 = i << 2;
762 for (j = 0; j < 4; j++) {
763 if (variance4x4downsample[i2 + j] == 1) {
764 v16x16 *vtemp = (!is_key_frame) ? &vt2[i2 + j] :
765 &vt.split[i].split[j];
766 for (m = 0; m < 4; m++)
767 fill_variance_tree(&vtemp->split[m], BLOCK_8X8);
768 fill_variance_tree(vtemp, BLOCK_16X16);
771 fill_variance_tree(&vt.split[i], BLOCK_32X32);
772 // If variance of this 32x32 block is above the threshold, force the block
773 // to split. This also forces a split on the upper (64x64) level.
774 get_variance(&vt.split[i].part_variances.none);
775 if (vt.split[i].part_variances.none.variance > thresholds[1]) {
776 force_split[i + 1] = 1;
781 fill_variance_tree(&vt, BLOCK_64X64);
783 // Now go through the entire structure, splitting every block size until
784 // we get to one that's got a variance lower than our threshold.
785 if ( mi_col + 8 > cm->mi_cols || mi_row + 8 > cm->mi_rows ||
786 !set_vt_partitioning(cpi, xd, &vt, BLOCK_64X64, mi_row, mi_col,
787 thresholds[0], BLOCK_16X16, force_split[0])) {
788 for (i = 0; i < 4; ++i) {
789 const int x32_idx = ((i & 1) << 2);
790 const int y32_idx = ((i >> 1) << 2);
791 const int i2 = i << 2;
792 if (!set_vt_partitioning(cpi, xd, &vt.split[i], BLOCK_32X32,
793 (mi_row + y32_idx), (mi_col + x32_idx),
794 thresholds[1], BLOCK_16X16,
795 force_split[i + 1])) {
796 for (j = 0; j < 4; ++j) {
797 const int x16_idx = ((j & 1) << 1);
798 const int y16_idx = ((j >> 1) << 1);
799 // For inter frames: if variance4x4downsample[] == 1 for this 16x16
800 // block, then the variance is based on 4x4 down-sampling, so use vt2
801 // in set_vt_partioning(), otherwise use vt.
802 v16x16 *vtemp = (!is_key_frame &&
803 variance4x4downsample[i2 + j] == 1) ?
804 &vt2[i2 + j] : &vt.split[i].split[j];
805 if (!set_vt_partitioning(cpi, xd, vtemp, BLOCK_16X16,
806 mi_row + y32_idx + y16_idx,
807 mi_col + x32_idx + x16_idx,
808 thresholds[2], cpi->vbp_bsize_min, 0)) {
809 for (k = 0; k < 4; ++k) {
810 const int x8_idx = (k & 1);
811 const int y8_idx = (k >> 1);
812 if (use_4x4_partition) {
813 if (!set_vt_partitioning(cpi, xd, &vtemp->split[k],
815 mi_row + y32_idx + y16_idx + y8_idx,
816 mi_col + x32_idx + x16_idx + x8_idx,
817 thresholds[3], BLOCK_8X8, 0)) {
818 set_block_size(cpi, xd,
819 (mi_row + y32_idx + y16_idx + y8_idx),
820 (mi_col + x32_idx + x16_idx + x8_idx),
824 set_block_size(cpi, xd,
825 (mi_row + y32_idx + y16_idx + y8_idx),
826 (mi_col + x32_idx + x16_idx + x8_idx),
837 static void update_state(VP9_COMP *cpi, ThreadData *td,
838 PICK_MODE_CONTEXT *ctx,
839 int mi_row, int mi_col, BLOCK_SIZE bsize,
840 int output_enabled) {
842 VP9_COMMON *const cm = &cpi->common;
843 RD_COUNTS *const rdc = &td->rd_counts;
844 MACROBLOCK *const x = &td->mb;
845 MACROBLOCKD *const xd = &x->e_mbd;
846 struct macroblock_plane *const p = x->plane;
847 struct macroblockd_plane *const pd = xd->plane;
848 MODE_INFO *mi = &ctx->mic;
849 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
850 MODE_INFO *mi_addr = &xd->mi[0];
851 const struct segmentation *const seg = &cm->seg;
852 const int bw = num_8x8_blocks_wide_lookup[mi->mbmi.sb_type];
853 const int bh = num_8x8_blocks_high_lookup[mi->mbmi.sb_type];
854 const int x_mis = MIN(bw, cm->mi_cols - mi_col);
855 const int y_mis = MIN(bh, cm->mi_rows - mi_row);
856 MV_REF *const frame_mvs =
857 cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
860 const int mis = cm->mi_stride;
861 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
862 const int mi_height = num_8x8_blocks_high_lookup[bsize];
865 assert(mi->mbmi.sb_type == bsize);
868 mi_addr->src_mi = mi_addr;
870 // If segmentation in use
872 // For in frame complexity AQ copy the segment id from the segment map.
873 if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
874 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
875 : cm->last_frame_seg_map;
876 mi_addr->mbmi.segment_id =
877 vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
879 // Else for cyclic refresh mode update the segment map, set the segment id
880 // and then update the quantizer.
881 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
882 vp9_cyclic_refresh_update_segment(cpi, &xd->mi[0].src_mi->mbmi, mi_row,
883 mi_col, bsize, ctx->rate, ctx->dist,
888 max_plane = is_inter_block(mbmi) ? MAX_MB_PLANE : 1;
889 for (i = 0; i < max_plane; ++i) {
890 p[i].coeff = ctx->coeff_pbuf[i][1];
891 p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
892 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
893 p[i].eobs = ctx->eobs_pbuf[i][1];
896 for (i = max_plane; i < MAX_MB_PLANE; ++i) {
897 p[i].coeff = ctx->coeff_pbuf[i][2];
898 p[i].qcoeff = ctx->qcoeff_pbuf[i][2];
899 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2];
900 p[i].eobs = ctx->eobs_pbuf[i][2];
903 // Restore the coding context of the MB to that that was in place
904 // when the mode was picked for it
905 for (y = 0; y < mi_height; y++)
906 for (x_idx = 0; x_idx < mi_width; x_idx++)
907 if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx
908 && (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
909 xd->mi[x_idx + y * mis].src_mi = mi_addr;
912 if (cpi->oxcf.aq_mode)
913 vp9_init_plane_quantizers(cpi, x);
915 // FIXME(rbultje) I'm pretty sure this should go to the end of this block
916 // (i.e. after the output_enabled)
917 if (bsize < BLOCK_32X32) {
918 if (bsize < BLOCK_16X16)
919 ctx->tx_rd_diff[ALLOW_16X16] = ctx->tx_rd_diff[ALLOW_8X8];
920 ctx->tx_rd_diff[ALLOW_32X32] = ctx->tx_rd_diff[ALLOW_16X16];
923 if (is_inter_block(mbmi) && mbmi->sb_type < BLOCK_8X8) {
924 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
925 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
929 vpx_memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk,
930 sizeof(uint8_t) * ctx->num_4x4_blk);
935 if (!vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
936 for (i = 0; i < TX_MODES; i++)
937 rdc->tx_select_diff[i] += ctx->tx_rd_diff[i];
940 #if CONFIG_INTERNAL_STATS
941 if (frame_is_intra_only(cm)) {
942 static const int kf_mode_index[] = {
944 THR_V_PRED /*V_PRED*/,
945 THR_H_PRED /*H_PRED*/,
946 THR_D45_PRED /*D45_PRED*/,
947 THR_D135_PRED /*D135_PRED*/,
948 THR_D117_PRED /*D117_PRED*/,
949 THR_D153_PRED /*D153_PRED*/,
950 THR_D207_PRED /*D207_PRED*/,
951 THR_D63_PRED /*D63_PRED*/,
954 ++cpi->mode_chosen_counts[kf_mode_index[mbmi->mode]];
956 // Note how often each mode chosen as best
957 ++cpi->mode_chosen_counts[ctx->best_mode_index];
960 if (!frame_is_intra_only(cm)) {
961 if (is_inter_block(mbmi)) {
962 vp9_update_mv_count(td);
964 if (cm->interp_filter == SWITCHABLE) {
965 const int ctx = vp9_get_pred_context_switchable_interp(xd);
966 ++td->counts->switchable_interp[ctx][mbmi->interp_filter];
970 rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
971 rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
972 rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
974 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
975 rdc->filter_diff[i] += ctx->best_filter_diff[i];
978 for (h = 0; h < y_mis; ++h) {
979 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
980 for (w = 0; w < x_mis; ++w) {
981 MV_REF *const mv = frame_mv + w;
982 mv->ref_frame[0] = mi->src_mi->mbmi.ref_frame[0];
983 mv->ref_frame[1] = mi->src_mi->mbmi.ref_frame[1];
984 mv->mv[0].as_int = mi->src_mi->mbmi.mv[0].as_int;
985 mv->mv[1].as_int = mi->src_mi->mbmi.mv[1].as_int;
990 void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
991 int mi_row, int mi_col) {
992 uint8_t *const buffers[3] = {src->y_buffer, src->u_buffer, src->v_buffer };
993 const int strides[3] = {src->y_stride, src->uv_stride, src->uv_stride };
996 // Set current frame pointer.
997 x->e_mbd.cur_buf = src;
999 for (i = 0; i < MAX_MB_PLANE; i++)
1000 setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
1001 NULL, x->e_mbd.plane[i].subsampling_x,
1002 x->e_mbd.plane[i].subsampling_y);
1005 static void set_mode_info_seg_skip(MACROBLOCK *x, TX_MODE tx_mode,
1006 RD_COST *rd_cost, BLOCK_SIZE bsize) {
1007 MACROBLOCKD *const xd = &x->e_mbd;
1008 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
1009 INTERP_FILTER filter_ref;
1011 if (xd->up_available)
1012 filter_ref = xd->mi[-xd->mi_stride].src_mi->mbmi.interp_filter;
1013 else if (xd->left_available)
1014 filter_ref = xd->mi[-1].src_mi->mbmi.interp_filter;
1016 filter_ref = EIGHTTAP;
1018 mbmi->sb_type = bsize;
1019 mbmi->mode = ZEROMV;
1020 mbmi->tx_size = MIN(max_txsize_lookup[bsize],
1021 tx_mode_to_biggest_tx_size[tx_mode]);
1023 mbmi->uv_mode = DC_PRED;
1024 mbmi->ref_frame[0] = LAST_FRAME;
1025 mbmi->ref_frame[1] = NONE;
1026 mbmi->mv[0].as_int = 0;
1027 mbmi->interp_filter = filter_ref;
1029 xd->mi[0].src_mi->bmi[0].as_mv[0].as_int = 0;
1032 vp9_rd_cost_init(rd_cost);
1035 static int set_segment_rdmult(VP9_COMP *const cpi,
1036 MACROBLOCK *const x,
1037 int8_t segment_id) {
1039 VP9_COMMON *const cm = &cpi->common;
1040 vp9_init_plane_quantizers(cpi, x);
1041 vp9_clear_system_state();
1042 segment_qindex = vp9_get_qindex(&cm->seg, segment_id,
1044 return vp9_compute_rd_mult(cpi, segment_qindex + cm->y_dc_delta_q);
1047 static void rd_pick_sb_modes(VP9_COMP *cpi,
1048 TileDataEnc *tile_data,
1049 MACROBLOCK *const x,
1050 int mi_row, int mi_col, RD_COST *rd_cost,
1051 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
1053 VP9_COMMON *const cm = &cpi->common;
1054 TileInfo *const tile_info = &tile_data->tile_info;
1055 MACROBLOCKD *const xd = &x->e_mbd;
1057 struct macroblock_plane *const p = x->plane;
1058 struct macroblockd_plane *const pd = xd->plane;
1059 const AQ_MODE aq_mode = cpi->oxcf.aq_mode;
1062 vp9_clear_system_state();
1064 // Use the lower precision, but faster, 32x32 fdct for mode selection.
1065 x->use_lp32x32fdct = 1;
1067 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
1068 mbmi = &xd->mi[0].src_mi->mbmi;
1069 mbmi->sb_type = bsize;
1071 for (i = 0; i < MAX_MB_PLANE; ++i) {
1072 p[i].coeff = ctx->coeff_pbuf[i][0];
1073 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
1074 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
1075 p[i].eobs = ctx->eobs_pbuf[i][0];
1079 ctx->pred_pixel_ready = 0;
1082 // Set to zero to make sure we do not use the previous encoded frame stats
1085 #if CONFIG_VP9_HIGHBITDEPTH
1086 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1087 x->source_variance =
1088 vp9_high_get_sby_perpixel_variance(cpi, &x->plane[0].src,
1091 x->source_variance =
1092 vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1095 x->source_variance =
1096 vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1097 #endif // CONFIG_VP9_HIGHBITDEPTH
1099 // Save rdmult before it might be changed, so it can be restored later.
1100 orig_rdmult = x->rdmult;
1102 if (aq_mode == VARIANCE_AQ) {
1103 const int energy = bsize <= BLOCK_16X16 ? x->mb_energy
1104 : vp9_block_energy(cpi, x, bsize);
1105 if (cm->frame_type == KEY_FRAME ||
1106 cpi->refresh_alt_ref_frame ||
1107 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
1108 mbmi->segment_id = vp9_vaq_segment_id(energy);
1110 const uint8_t *const map = cm->seg.update_map ? cpi->segmentation_map
1111 : cm->last_frame_seg_map;
1112 mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
1114 x->rdmult = set_segment_rdmult(cpi, x, mbmi->segment_id);
1115 } else if (aq_mode == COMPLEXITY_AQ) {
1116 x->rdmult = set_segment_rdmult(cpi, x, mbmi->segment_id);
1117 } else if (aq_mode == CYCLIC_REFRESH_AQ) {
1118 const uint8_t *const map = cm->seg.update_map ? cpi->segmentation_map
1119 : cm->last_frame_seg_map;
1120 // If segment is boosted, use rdmult for that segment.
1121 if (cyclic_refresh_segment_id_boosted(
1122 vp9_get_segment_id(cm, map, bsize, mi_row, mi_col)))
1123 x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
1126 // Find best coding mode & reconstruct the MB so it is available
1127 // as a predictor for MBs that follow in the SB
1128 if (frame_is_intra_only(cm)) {
1129 vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, best_rd);
1131 if (bsize >= BLOCK_8X8) {
1132 if (vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
1133 vp9_rd_pick_inter_mode_sb_seg_skip(cpi, tile_data, x, rd_cost, bsize,
1136 vp9_rd_pick_inter_mode_sb(cpi, tile_data, x, mi_row, mi_col,
1137 rd_cost, bsize, ctx, best_rd);
1139 vp9_rd_pick_inter_mode_sub8x8(cpi, tile_data, x, mi_row, mi_col,
1140 rd_cost, bsize, ctx, best_rd);
1145 // Examine the resulting rate and for AQ mode 2 make a segment choice.
1146 if ((rd_cost->rate != INT_MAX) &&
1147 (aq_mode == COMPLEXITY_AQ) && (bsize >= BLOCK_16X16) &&
1148 (cm->frame_type == KEY_FRAME ||
1149 cpi->refresh_alt_ref_frame ||
1150 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref))) {
1151 vp9_caq_select_segment(cpi, x, bsize, mi_row, mi_col, rd_cost->rate);
1154 x->rdmult = orig_rdmult;
1156 // TODO(jingning) The rate-distortion optimization flow needs to be
1157 // refactored to provide proper exit/return handle.
1158 if (rd_cost->rate == INT_MAX)
1159 rd_cost->rdcost = INT64_MAX;
1161 ctx->rate = rd_cost->rate;
1162 ctx->dist = rd_cost->dist;
1165 static void update_stats(VP9_COMMON *cm, ThreadData *td) {
1166 const MACROBLOCK *x = &td->mb;
1167 const MACROBLOCKD *const xd = &x->e_mbd;
1168 const MODE_INFO *const mi = xd->mi[0].src_mi;
1169 const MB_MODE_INFO *const mbmi = &mi->mbmi;
1170 const BLOCK_SIZE bsize = mbmi->sb_type;
1172 if (!frame_is_intra_only(cm)) {
1173 FRAME_COUNTS *const counts = td->counts;
1174 const int inter_block = is_inter_block(mbmi);
1175 const int seg_ref_active = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
1177 if (!seg_ref_active) {
1178 counts->intra_inter[vp9_get_intra_inter_context(xd)][inter_block]++;
1179 // If the segment reference feature is enabled we have only a single
1180 // reference frame allowed for the segment so exclude it from
1181 // the reference frame counts used to work out probabilities.
1183 const MV_REFERENCE_FRAME ref0 = mbmi->ref_frame[0];
1184 if (cm->reference_mode == REFERENCE_MODE_SELECT)
1185 counts->comp_inter[vp9_get_reference_mode_context(cm, xd)]
1186 [has_second_ref(mbmi)]++;
1188 if (has_second_ref(mbmi)) {
1189 counts->comp_ref[vp9_get_pred_context_comp_ref_p(cm, xd)]
1190 [ref0 == GOLDEN_FRAME]++;
1192 counts->single_ref[vp9_get_pred_context_single_ref_p1(xd)][0]
1193 [ref0 != LAST_FRAME]++;
1194 if (ref0 != LAST_FRAME)
1195 counts->single_ref[vp9_get_pred_context_single_ref_p2(xd)][1]
1196 [ref0 != GOLDEN_FRAME]++;
1201 !vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
1202 const int mode_ctx = mbmi->mode_context[mbmi->ref_frame[0]];
1203 if (bsize >= BLOCK_8X8) {
1204 const PREDICTION_MODE mode = mbmi->mode;
1205 ++counts->inter_mode[mode_ctx][INTER_OFFSET(mode)];
1207 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
1208 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
1210 for (idy = 0; idy < 2; idy += num_4x4_h) {
1211 for (idx = 0; idx < 2; idx += num_4x4_w) {
1212 const int j = idy * 2 + idx;
1213 const PREDICTION_MODE b_mode = mi->bmi[j].as_mode;
1214 ++counts->inter_mode[mode_ctx][INTER_OFFSET(b_mode)];
1222 static void restore_context(MACROBLOCK *const x, int mi_row, int mi_col,
1223 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
1224 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
1225 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
1227 MACROBLOCKD *const xd = &x->e_mbd;
1229 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1230 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1231 int mi_width = num_8x8_blocks_wide_lookup[bsize];
1232 int mi_height = num_8x8_blocks_high_lookup[bsize];
1233 for (p = 0; p < MAX_MB_PLANE; p++) {
1235 xd->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
1236 a + num_4x4_blocks_wide * p,
1237 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
1238 xd->plane[p].subsampling_x);
1241 + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
1242 l + num_4x4_blocks_high * p,
1243 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
1244 xd->plane[p].subsampling_y);
1246 vpx_memcpy(xd->above_seg_context + mi_col, sa,
1247 sizeof(*xd->above_seg_context) * mi_width);
1248 vpx_memcpy(xd->left_seg_context + (mi_row & MI_MASK), sl,
1249 sizeof(xd->left_seg_context[0]) * mi_height);
1252 static void save_context(MACROBLOCK *const x, int mi_row, int mi_col,
1253 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
1254 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
1255 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
1257 const MACROBLOCKD *const xd = &x->e_mbd;
1259 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1260 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1261 int mi_width = num_8x8_blocks_wide_lookup[bsize];
1262 int mi_height = num_8x8_blocks_high_lookup[bsize];
1264 // buffer the above/left context information of the block in search.
1265 for (p = 0; p < MAX_MB_PLANE; ++p) {
1267 a + num_4x4_blocks_wide * p,
1268 xd->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
1269 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
1270 xd->plane[p].subsampling_x);
1272 l + num_4x4_blocks_high * p,
1274 + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
1275 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
1276 xd->plane[p].subsampling_y);
1278 vpx_memcpy(sa, xd->above_seg_context + mi_col,
1279 sizeof(*xd->above_seg_context) * mi_width);
1280 vpx_memcpy(sl, xd->left_seg_context + (mi_row & MI_MASK),
1281 sizeof(xd->left_seg_context[0]) * mi_height);
1284 static void encode_b(VP9_COMP *cpi, const TileInfo *const tile,
1286 TOKENEXTRA **tp, int mi_row, int mi_col,
1287 int output_enabled, BLOCK_SIZE bsize,
1288 PICK_MODE_CONTEXT *ctx) {
1289 MACROBLOCK *const x = &td->mb;
1290 set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
1291 update_state(cpi, td, ctx, mi_row, mi_col, bsize, output_enabled);
1292 encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
1294 if (output_enabled) {
1295 update_stats(&cpi->common, td);
1297 (*tp)->token = EOSB_TOKEN;
1302 static void encode_sb(VP9_COMP *cpi, ThreadData *td,
1303 const TileInfo *const tile,
1304 TOKENEXTRA **tp, int mi_row, int mi_col,
1305 int output_enabled, BLOCK_SIZE bsize,
1307 VP9_COMMON *const cm = &cpi->common;
1308 MACROBLOCK *const x = &td->mb;
1309 MACROBLOCKD *const xd = &x->e_mbd;
1311 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
1313 PARTITION_TYPE partition;
1314 BLOCK_SIZE subsize = bsize;
1316 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1319 if (bsize >= BLOCK_8X8) {
1320 ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1321 subsize = get_subsize(bsize, pc_tree->partitioning);
1324 subsize = BLOCK_4X4;
1327 partition = partition_lookup[bsl][subsize];
1328 if (output_enabled && bsize != BLOCK_4X4)
1329 td->counts->partition[ctx][partition]++;
1331 switch (partition) {
1332 case PARTITION_NONE:
1333 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1336 case PARTITION_VERT:
1337 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1338 &pc_tree->vertical[0]);
1339 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
1340 encode_b(cpi, tile, td, tp, mi_row, mi_col + hbs, output_enabled,
1341 subsize, &pc_tree->vertical[1]);
1344 case PARTITION_HORZ:
1345 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1346 &pc_tree->horizontal[0]);
1347 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
1348 encode_b(cpi, tile, td, tp, mi_row + hbs, mi_col, output_enabled,
1349 subsize, &pc_tree->horizontal[1]);
1352 case PARTITION_SPLIT:
1353 if (bsize == BLOCK_8X8) {
1354 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1355 pc_tree->leaf_split[0]);
1357 encode_sb(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1359 encode_sb(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
1360 subsize, pc_tree->split[1]);
1361 encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
1362 subsize, pc_tree->split[2]);
1363 encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs, output_enabled,
1364 subsize, pc_tree->split[3]);
1368 assert(0 && "Invalid partition type.");
1372 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
1373 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1376 // Check to see if the given partition size is allowed for a specified number
1377 // of 8x8 block rows and columns remaining in the image.
1378 // If not then return the largest allowed partition size
1379 static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize,
1380 int rows_left, int cols_left,
1382 if (rows_left <= 0 || cols_left <= 0) {
1383 return MIN(bsize, BLOCK_8X8);
1385 for (; bsize > 0; bsize -= 3) {
1386 *bh = num_8x8_blocks_high_lookup[bsize];
1387 *bw = num_8x8_blocks_wide_lookup[bsize];
1388 if ((*bh <= rows_left) && (*bw <= cols_left)) {
1396 static void set_partial_b64x64_partition(MODE_INFO *mi, int mis,
1397 int bh_in, int bw_in, int row8x8_remaining, int col8x8_remaining,
1398 BLOCK_SIZE bsize, MODE_INFO *mi_8x8) {
1401 for (r = 0; r < MI_BLOCK_SIZE; r += bh) {
1403 for (c = 0; c < MI_BLOCK_SIZE; c += bw) {
1404 const int index = r * mis + c;
1405 mi_8x8[index].src_mi = mi + index;
1406 mi_8x8[index].src_mi->mbmi.sb_type = find_partition_size(bsize,
1407 row8x8_remaining - r, col8x8_remaining - c, &bh, &bw);
1412 // This function attempts to set all mode info entries in a given SB64
1413 // to the same block partition size.
1414 // However, at the bottom and right borders of the image the requested size
1415 // may not be allowed in which case this code attempts to choose the largest
1416 // allowable partition.
1417 static void set_fixed_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
1418 MODE_INFO *mi_8x8, int mi_row, int mi_col,
1420 VP9_COMMON *const cm = &cpi->common;
1421 const int mis = cm->mi_stride;
1422 const int row8x8_remaining = tile->mi_row_end - mi_row;
1423 const int col8x8_remaining = tile->mi_col_end - mi_col;
1424 int block_row, block_col;
1425 MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
1426 int bh = num_8x8_blocks_high_lookup[bsize];
1427 int bw = num_8x8_blocks_wide_lookup[bsize];
1429 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
1431 // Apply the requested partition size to the SB64 if it is all "in image"
1432 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
1433 (row8x8_remaining >= MI_BLOCK_SIZE)) {
1434 for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
1435 for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
1436 int index = block_row * mis + block_col;
1437 mi_8x8[index].src_mi = mi_upper_left + index;
1438 mi_8x8[index].src_mi->mbmi.sb_type = bsize;
1442 // Else this is a partial SB64.
1443 set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
1444 col8x8_remaining, bsize, mi_8x8);
1451 } coord_lookup[16] = {
1453 {0, 0}, {0, 2}, {2, 0}, {2, 2},
1455 {0, 4}, {0, 6}, {2, 4}, {2, 6},
1457 {4, 0}, {4, 2}, {6, 0}, {6, 2},
1459 {4, 4}, {4, 6}, {6, 4}, {6, 6},
1462 static void set_source_var_based_partition(VP9_COMP *cpi,
1463 const TileInfo *const tile,
1464 MACROBLOCK *const x,
1466 int mi_row, int mi_col) {
1467 VP9_COMMON *const cm = &cpi->common;
1468 const int mis = cm->mi_stride;
1469 const int row8x8_remaining = tile->mi_row_end - mi_row;
1470 const int col8x8_remaining = tile->mi_col_end - mi_col;
1471 MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
1473 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
1475 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
1478 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
1479 (row8x8_remaining >= MI_BLOCK_SIZE)) {
1483 const int offset = (mi_row >> 1) * cm->mb_cols + (mi_col >> 1);
1484 int is_larger_better = 0;
1486 unsigned int thr = cpi->source_var_thresh;
1488 vpx_memset(d32, 0, 4 * sizeof(diff));
1490 for (i = 0; i < 4; i++) {
1493 for (j = 0; j < 4; j++) {
1494 int b_mi_row = coord_lookup[i * 4 + j].row;
1495 int b_mi_col = coord_lookup[i * 4 + j].col;
1496 int boffset = b_mi_row / 2 * cm->mb_cols +
1499 d16[j] = cpi->source_diff_var + offset + boffset;
1501 index = b_mi_row * mis + b_mi_col;
1502 mi_8x8[index].src_mi = mi_upper_left + index;
1503 mi_8x8[index].src_mi->mbmi.sb_type = BLOCK_16X16;
1505 // TODO(yunqingwang): If d16[j].var is very large, use 8x8 partition
1506 // size to further improve quality.
1509 is_larger_better = (d16[0]->var < thr) && (d16[1]->var < thr) &&
1510 (d16[2]->var < thr) && (d16[3]->var < thr);
1512 // Use 32x32 partition
1513 if (is_larger_better) {
1516 for (j = 0; j < 4; j++) {
1517 d32[i].sse += d16[j]->sse;
1518 d32[i].sum += d16[j]->sum;
1521 d32[i].var = d32[i].sse - (((int64_t)d32[i].sum * d32[i].sum) >> 10);
1523 index = coord_lookup[i*4].row * mis + coord_lookup[i*4].col;
1524 mi_8x8[index].src_mi = mi_upper_left + index;
1525 mi_8x8[index].src_mi->mbmi.sb_type = BLOCK_32X32;
1529 if (use32x32 == 4) {
1531 is_larger_better = (d32[0].var < thr) && (d32[1].var < thr) &&
1532 (d32[2].var < thr) && (d32[3].var < thr);
1534 // Use 64x64 partition
1535 if (is_larger_better) {
1536 mi_8x8[0].src_mi = mi_upper_left;
1537 mi_8x8[0].src_mi->mbmi.sb_type = BLOCK_64X64;
1540 } else { // partial in-image SB64
1541 int bh = num_8x8_blocks_high_lookup[BLOCK_16X16];
1542 int bw = num_8x8_blocks_wide_lookup[BLOCK_16X16];
1543 set_partial_b64x64_partition(mi_upper_left, mis, bh, bw,
1544 row8x8_remaining, col8x8_remaining, BLOCK_16X16, mi_8x8);
1548 static void update_state_rt(VP9_COMP *cpi, ThreadData *td,
1549 PICK_MODE_CONTEXT *ctx,
1550 int mi_row, int mi_col, int bsize) {
1551 VP9_COMMON *const cm = &cpi->common;
1552 MACROBLOCK *const x = &td->mb;
1553 MACROBLOCKD *const xd = &x->e_mbd;
1554 MODE_INFO *const mi = xd->mi[0].src_mi;
1555 MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
1556 const struct segmentation *const seg = &cm->seg;
1557 const int bw = num_8x8_blocks_wide_lookup[mi->mbmi.sb_type];
1558 const int bh = num_8x8_blocks_high_lookup[mi->mbmi.sb_type];
1559 const int x_mis = MIN(bw, cm->mi_cols - mi_col);
1560 const int y_mis = MIN(bh, cm->mi_rows - mi_row);
1562 xd->mi[0] = ctx->mic;
1563 xd->mi[0].src_mi = &xd->mi[0];
1565 if (seg->enabled && cpi->oxcf.aq_mode) {
1566 // For in frame complexity AQ or variance AQ, copy segment_id from
1567 // segmentation_map.
1568 if (cpi->oxcf.aq_mode == COMPLEXITY_AQ ||
1569 cpi->oxcf.aq_mode == VARIANCE_AQ ) {
1570 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
1571 : cm->last_frame_seg_map;
1572 mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
1574 // Setting segmentation map for cyclic_refresh.
1575 vp9_cyclic_refresh_update_segment(cpi, mbmi, mi_row, mi_col, bsize,
1576 ctx->rate, ctx->dist, x->skip);
1578 vp9_init_plane_quantizers(cpi, x);
1581 if (is_inter_block(mbmi)) {
1582 vp9_update_mv_count(td);
1583 if (cm->interp_filter == SWITCHABLE) {
1584 const int pred_ctx = vp9_get_pred_context_switchable_interp(xd);
1585 ++td->counts->switchable_interp[pred_ctx][mbmi->interp_filter];
1588 if (mbmi->sb_type < BLOCK_8X8) {
1589 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
1590 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
1594 if (cm->use_prev_frame_mvs) {
1595 MV_REF *const frame_mvs =
1596 cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
1599 for (h = 0; h < y_mis; ++h) {
1600 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
1601 for (w = 0; w < x_mis; ++w) {
1602 MV_REF *const mv = frame_mv + w;
1603 mv->ref_frame[0] = mi->src_mi->mbmi.ref_frame[0];
1604 mv->ref_frame[1] = mi->src_mi->mbmi.ref_frame[1];
1605 mv->mv[0].as_int = mi->src_mi->mbmi.mv[0].as_int;
1606 mv->mv[1].as_int = mi->src_mi->mbmi.mv[1].as_int;
1611 x->skip = ctx->skip;
1612 x->skip_txfm[0] = mbmi->segment_id ? 0 : ctx->skip_txfm[0];
1615 static void encode_b_rt(VP9_COMP *cpi, ThreadData *td,
1616 const TileInfo *const tile,
1617 TOKENEXTRA **tp, int mi_row, int mi_col,
1618 int output_enabled, BLOCK_SIZE bsize,
1619 PICK_MODE_CONTEXT *ctx) {
1620 MACROBLOCK *const x = &td->mb;
1621 set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
1622 update_state_rt(cpi, td, ctx, mi_row, mi_col, bsize);
1624 #if CONFIG_VP9_TEMPORAL_DENOISING
1625 if (cpi->oxcf.noise_sensitivity > 0 && output_enabled &&
1626 cpi->common.frame_type != KEY_FRAME) {
1627 vp9_denoiser_denoise(&cpi->denoiser, x, mi_row, mi_col,
1628 MAX(BLOCK_8X8, bsize), ctx);
1632 encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
1633 update_stats(&cpi->common, td);
1635 (*tp)->token = EOSB_TOKEN;
1639 static void encode_sb_rt(VP9_COMP *cpi, ThreadData *td,
1640 const TileInfo *const tile,
1641 TOKENEXTRA **tp, int mi_row, int mi_col,
1642 int output_enabled, BLOCK_SIZE bsize,
1644 VP9_COMMON *const cm = &cpi->common;
1645 MACROBLOCK *const x = &td->mb;
1646 MACROBLOCKD *const xd = &x->e_mbd;
1648 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
1650 PARTITION_TYPE partition;
1653 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1656 if (bsize >= BLOCK_8X8) {
1657 const int idx_str = xd->mi_stride * mi_row + mi_col;
1658 MODE_INFO *mi_8x8 = cm->mi[idx_str].src_mi;
1659 ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1660 subsize = mi_8x8[0].src_mi->mbmi.sb_type;
1663 subsize = BLOCK_4X4;
1666 partition = partition_lookup[bsl][subsize];
1667 if (output_enabled && bsize != BLOCK_4X4)
1668 td->counts->partition[ctx][partition]++;
1670 switch (partition) {
1671 case PARTITION_NONE:
1672 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1675 case PARTITION_VERT:
1676 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1677 &pc_tree->vertical[0]);
1678 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
1679 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
1680 subsize, &pc_tree->vertical[1]);
1683 case PARTITION_HORZ:
1684 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1685 &pc_tree->horizontal[0]);
1686 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
1687 encode_b_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
1688 subsize, &pc_tree->horizontal[1]);
1691 case PARTITION_SPLIT:
1692 subsize = get_subsize(bsize, PARTITION_SPLIT);
1693 encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1695 encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
1696 subsize, pc_tree->split[1]);
1697 encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
1698 subsize, pc_tree->split[2]);
1699 encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs,
1700 output_enabled, subsize, pc_tree->split[3]);
1703 assert(0 && "Invalid partition type.");
1707 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
1708 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1711 static void rd_use_partition(VP9_COMP *cpi,
1713 TileDataEnc *tile_data,
1714 MODE_INFO *mi_8x8, TOKENEXTRA **tp,
1715 int mi_row, int mi_col,
1717 int *rate, int64_t *dist,
1718 int do_recon, PC_TREE *pc_tree) {
1719 VP9_COMMON *const cm = &cpi->common;
1720 TileInfo *const tile_info = &tile_data->tile_info;
1721 MACROBLOCK *const x = &td->mb;
1722 MACROBLOCKD *const xd = &x->e_mbd;
1723 const int mis = cm->mi_stride;
1724 const int bsl = b_width_log2_lookup[bsize];
1725 const int mi_step = num_4x4_blocks_wide_lookup[bsize] / 2;
1726 const int bss = (1 << bsl) / 4;
1728 PARTITION_TYPE partition = PARTITION_NONE;
1730 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1731 PARTITION_CONTEXT sl[8], sa[8];
1732 RD_COST last_part_rdc, none_rdc, chosen_rdc;
1733 BLOCK_SIZE sub_subsize = BLOCK_4X4;
1734 int splits_below = 0;
1735 BLOCK_SIZE bs_type = mi_8x8[0].src_mi->mbmi.sb_type;
1736 int do_partition_search = 1;
1737 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
1739 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1742 assert(num_4x4_blocks_wide_lookup[bsize] ==
1743 num_4x4_blocks_high_lookup[bsize]);
1745 vp9_rd_cost_reset(&last_part_rdc);
1746 vp9_rd_cost_reset(&none_rdc);
1747 vp9_rd_cost_reset(&chosen_rdc);
1749 partition = partition_lookup[bsl][bs_type];
1750 subsize = get_subsize(bsize, partition);
1752 pc_tree->partitioning = partition;
1753 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
1755 if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode) {
1756 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
1757 x->mb_energy = vp9_block_energy(cpi, x, bsize);
1760 if (do_partition_search &&
1761 cpi->sf.partition_search_type == SEARCH_PARTITION &&
1762 cpi->sf.adjust_partitioning_from_last_frame) {
1763 // Check if any of the sub blocks are further split.
1764 if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
1765 sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
1767 for (i = 0; i < 4; i++) {
1768 int jj = i >> 1, ii = i & 0x01;
1769 MODE_INFO *this_mi = mi_8x8[jj * bss * mis + ii * bss].src_mi;
1770 if (this_mi && this_mi->mbmi.sb_type >= sub_subsize) {
1776 // If partition is not none try none unless each of the 4 splits are split
1778 if (partition != PARTITION_NONE && !splits_below &&
1779 mi_row + (mi_step >> 1) < cm->mi_rows &&
1780 mi_col + (mi_step >> 1) < cm->mi_cols) {
1781 pc_tree->partitioning = PARTITION_NONE;
1782 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &none_rdc, bsize,
1785 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
1787 if (none_rdc.rate < INT_MAX) {
1788 none_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
1789 none_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, none_rdc.rate,
1793 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
1794 mi_8x8[0].src_mi->mbmi.sb_type = bs_type;
1795 pc_tree->partitioning = partition;
1799 switch (partition) {
1800 case PARTITION_NONE:
1801 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1802 bsize, ctx, INT64_MAX);
1804 case PARTITION_HORZ:
1805 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1806 subsize, &pc_tree->horizontal[0],
1808 if (last_part_rdc.rate != INT_MAX &&
1809 bsize >= BLOCK_8X8 && mi_row + (mi_step >> 1) < cm->mi_rows) {
1811 PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
1812 vp9_rd_cost_init(&tmp_rdc);
1813 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
1814 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
1815 rd_pick_sb_modes(cpi, tile_data, x,
1816 mi_row + (mi_step >> 1), mi_col, &tmp_rdc,
1817 subsize, &pc_tree->horizontal[1], INT64_MAX);
1818 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
1819 vp9_rd_cost_reset(&last_part_rdc);
1822 last_part_rdc.rate += tmp_rdc.rate;
1823 last_part_rdc.dist += tmp_rdc.dist;
1824 last_part_rdc.rdcost += tmp_rdc.rdcost;
1827 case PARTITION_VERT:
1828 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1829 subsize, &pc_tree->vertical[0], INT64_MAX);
1830 if (last_part_rdc.rate != INT_MAX &&
1831 bsize >= BLOCK_8X8 && mi_col + (mi_step >> 1) < cm->mi_cols) {
1833 PICK_MODE_CONTEXT *ctx = &pc_tree->vertical[0];
1834 vp9_rd_cost_init(&tmp_rdc);
1835 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
1836 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
1837 rd_pick_sb_modes(cpi, tile_data, x,
1838 mi_row, mi_col + (mi_step >> 1), &tmp_rdc,
1839 subsize, &pc_tree->vertical[bsize > BLOCK_8X8],
1841 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
1842 vp9_rd_cost_reset(&last_part_rdc);
1845 last_part_rdc.rate += tmp_rdc.rate;
1846 last_part_rdc.dist += tmp_rdc.dist;
1847 last_part_rdc.rdcost += tmp_rdc.rdcost;
1850 case PARTITION_SPLIT:
1851 if (bsize == BLOCK_8X8) {
1852 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1853 subsize, pc_tree->leaf_split[0], INT64_MAX);
1856 last_part_rdc.rate = 0;
1857 last_part_rdc.dist = 0;
1858 last_part_rdc.rdcost = 0;
1859 for (i = 0; i < 4; i++) {
1860 int x_idx = (i & 1) * (mi_step >> 1);
1861 int y_idx = (i >> 1) * (mi_step >> 1);
1862 int jj = i >> 1, ii = i & 0x01;
1864 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1867 vp9_rd_cost_init(&tmp_rdc);
1868 rd_use_partition(cpi, td, tile_data,
1869 mi_8x8 + jj * bss * mis + ii * bss, tp,
1870 mi_row + y_idx, mi_col + x_idx, subsize,
1871 &tmp_rdc.rate, &tmp_rdc.dist,
1872 i != 3, pc_tree->split[i]);
1873 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
1874 vp9_rd_cost_reset(&last_part_rdc);
1877 last_part_rdc.rate += tmp_rdc.rate;
1878 last_part_rdc.dist += tmp_rdc.dist;
1886 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
1887 if (last_part_rdc.rate < INT_MAX) {
1888 last_part_rdc.rate += cpi->partition_cost[pl][partition];
1889 last_part_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
1890 last_part_rdc.rate, last_part_rdc.dist);
1893 if (do_partition_search
1894 && cpi->sf.adjust_partitioning_from_last_frame
1895 && cpi->sf.partition_search_type == SEARCH_PARTITION
1896 && partition != PARTITION_SPLIT && bsize > BLOCK_8X8
1897 && (mi_row + mi_step < cm->mi_rows ||
1898 mi_row + (mi_step >> 1) == cm->mi_rows)
1899 && (mi_col + mi_step < cm->mi_cols ||
1900 mi_col + (mi_step >> 1) == cm->mi_cols)) {
1901 BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
1902 chosen_rdc.rate = 0;
1903 chosen_rdc.dist = 0;
1904 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
1905 pc_tree->partitioning = PARTITION_SPLIT;
1908 for (i = 0; i < 4; i++) {
1909 int x_idx = (i & 1) * (mi_step >> 1);
1910 int y_idx = (i >> 1) * (mi_step >> 1);
1912 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1913 PARTITION_CONTEXT sl[8], sa[8];
1915 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1918 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
1919 pc_tree->split[i]->partitioning = PARTITION_NONE;
1920 rd_pick_sb_modes(cpi, tile_data, x,
1921 mi_row + y_idx, mi_col + x_idx, &tmp_rdc,
1922 split_subsize, &pc_tree->split[i]->none, INT64_MAX);
1924 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
1926 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
1927 vp9_rd_cost_reset(&chosen_rdc);
1931 chosen_rdc.rate += tmp_rdc.rate;
1932 chosen_rdc.dist += tmp_rdc.dist;
1935 encode_sb(cpi, td, tile_info, tp, mi_row + y_idx, mi_col + x_idx, 0,
1936 split_subsize, pc_tree->split[i]);
1938 pl = partition_plane_context(xd, mi_row + y_idx, mi_col + x_idx,
1940 chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
1942 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
1943 if (chosen_rdc.rate < INT_MAX) {
1944 chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
1945 chosen_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
1946 chosen_rdc.rate, chosen_rdc.dist);
1950 // If last_part is better set the partitioning to that.
1951 if (last_part_rdc.rdcost < chosen_rdc.rdcost) {
1952 mi_8x8[0].src_mi->mbmi.sb_type = bsize;
1953 if (bsize >= BLOCK_8X8)
1954 pc_tree->partitioning = partition;
1955 chosen_rdc = last_part_rdc;
1957 // If none was better set the partitioning to that.
1958 if (none_rdc.rdcost < chosen_rdc.rdcost) {
1959 if (bsize >= BLOCK_8X8)
1960 pc_tree->partitioning = PARTITION_NONE;
1961 chosen_rdc = none_rdc;
1964 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
1966 // We must have chosen a partitioning and encoding or we'll fail later on.
1967 // No other opportunities for success.
1968 if (bsize == BLOCK_64X64)
1969 assert(chosen_rdc.rate < INT_MAX && chosen_rdc.dist < INT64_MAX);
1972 int output_enabled = (bsize == BLOCK_64X64);
1973 encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
1977 *rate = chosen_rdc.rate;
1978 *dist = chosen_rdc.dist;
1981 static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
1982 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
1983 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
1984 BLOCK_8X8, BLOCK_8X8, BLOCK_8X8,
1985 BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
1989 static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
1990 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16,
1991 BLOCK_16X16, BLOCK_32X32, BLOCK_32X32,
1992 BLOCK_32X32, BLOCK_64X64, BLOCK_64X64,
1993 BLOCK_64X64, BLOCK_64X64, BLOCK_64X64,
1997 // Look at all the mode_info entries for blocks that are part of this
1998 // partition and find the min and max values for sb_type.
1999 // At the moment this is designed to work on a 64x64 SB but could be
2000 // adjusted to use a size parameter.
2002 // The min and max are assumed to have been initialized prior to calling this
2003 // function so repeat calls can accumulate a min and max of more than one sb64.
2004 static void get_sb_partition_size_range(MACROBLOCKD *xd, MODE_INFO *mi_8x8,
2005 BLOCK_SIZE *min_block_size,
2006 BLOCK_SIZE *max_block_size,
2007 int bs_hist[BLOCK_SIZES]) {
2008 int sb_width_in_blocks = MI_BLOCK_SIZE;
2009 int sb_height_in_blocks = MI_BLOCK_SIZE;
2013 // Check the sb_type for each block that belongs to this region.
2014 for (i = 0; i < sb_height_in_blocks; ++i) {
2015 for (j = 0; j < sb_width_in_blocks; ++j) {
2016 MODE_INFO *mi = mi_8x8[index+j].src_mi;
2017 BLOCK_SIZE sb_type = mi ? mi->mbmi.sb_type : 0;
2019 *min_block_size = MIN(*min_block_size, sb_type);
2020 *max_block_size = MAX(*max_block_size, sb_type);
2022 index += xd->mi_stride;
2026 // Next square block size less or equal than current block size.
2027 static const BLOCK_SIZE next_square_size[BLOCK_SIZES] = {
2028 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
2029 BLOCK_8X8, BLOCK_8X8, BLOCK_8X8,
2030 BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
2031 BLOCK_32X32, BLOCK_32X32, BLOCK_32X32,
2035 // Look at neighboring blocks and set a min and max partition size based on
2037 static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
2038 MACROBLOCKD *const xd,
2039 int mi_row, int mi_col,
2040 BLOCK_SIZE *min_block_size,
2041 BLOCK_SIZE *max_block_size) {
2042 VP9_COMMON *const cm = &cpi->common;
2043 MODE_INFO *mi = xd->mi[0].src_mi;
2044 const int left_in_image = xd->left_available && mi[-1].src_mi;
2045 const int above_in_image = xd->up_available && mi[-xd->mi_stride].src_mi;
2046 const int row8x8_remaining = tile->mi_row_end - mi_row;
2047 const int col8x8_remaining = tile->mi_col_end - mi_col;
2049 BLOCK_SIZE min_size = BLOCK_4X4;
2050 BLOCK_SIZE max_size = BLOCK_64X64;
2052 int bs_hist[BLOCK_SIZES] = {0};
2054 // Trap case where we do not have a prediction.
2055 if (left_in_image || above_in_image || cm->frame_type != KEY_FRAME) {
2056 // Default "min to max" and "max to min"
2057 min_size = BLOCK_64X64;
2058 max_size = BLOCK_4X4;
2060 // NOTE: each call to get_sb_partition_size_range() uses the previous
2061 // passed in values for min and max as a starting point.
2062 // Find the min and max partition used in previous frame at this location
2063 if (cm->frame_type != KEY_FRAME) {
2064 MODE_INFO *prev_mi =
2065 cm->prev_mip + cm->mi_stride + 1 + mi_row * xd->mi_stride + mi_col;
2067 get_sb_partition_size_range(xd, prev_mi, &min_size, &max_size, bs_hist);
2069 // Find the min and max partition sizes used in the left SB64
2070 if (left_in_image) {
2071 MODE_INFO *left_sb64_mi = mi[-MI_BLOCK_SIZE].src_mi;
2072 get_sb_partition_size_range(xd, left_sb64_mi, &min_size, &max_size,
2075 // Find the min and max partition sizes used in the above SB64.
2076 if (above_in_image) {
2077 MODE_INFO *above_sb64_mi = mi[-xd->mi_stride * MI_BLOCK_SIZE].src_mi;
2078 get_sb_partition_size_range(xd, above_sb64_mi, &min_size, &max_size,
2082 // adjust observed min and max
2083 if (cpi->sf.auto_min_max_partition_size == RELAXED_NEIGHBORING_MIN_MAX) {
2084 min_size = min_partition_size[min_size];
2085 max_size = max_partition_size[max_size];
2086 } else if (cpi->sf.auto_min_max_partition_size ==
2087 CONSTRAIN_NEIGHBORING_MIN_MAX) {
2088 // adjust the search range based on the histogram of the observed
2089 // partition sizes from left, above the previous co-located blocks
2091 int first_moment = 0;
2092 int second_moment = 0;
2093 int var_unnormalized = 0;
2095 for (i = 0; i < BLOCK_SIZES; i++) {
2097 first_moment += bs_hist[i] * i;
2098 second_moment += bs_hist[i] * i * i;
2101 // if variance is small enough,
2102 // adjust the range around its mean size, which gives a tighter range
2103 var_unnormalized = second_moment - first_moment * first_moment / sum;
2104 if (var_unnormalized <= 4 * sum) {
2105 int mean = first_moment / sum;
2106 min_size = min_partition_size[mean];
2107 max_size = max_partition_size[mean];
2109 min_size = min_partition_size[min_size];
2110 max_size = max_partition_size[max_size];
2115 // Check border cases where max and min from neighbors may not be legal.
2116 max_size = find_partition_size(max_size,
2117 row8x8_remaining, col8x8_remaining,
2119 min_size = MIN(min_size, max_size);
2121 // When use_square_partition_only is true, make sure at least one square
2122 // partition is allowed by selecting the next smaller square size as
2124 if (cpi->sf.use_square_partition_only &&
2125 next_square_size[max_size] < min_size) {
2126 min_size = next_square_size[max_size];
2129 *min_block_size = min_size;
2130 *max_block_size = max_size;
2133 static void auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
2134 MACROBLOCKD *const xd,
2135 int mi_row, int mi_col,
2136 BLOCK_SIZE *min_block_size,
2137 BLOCK_SIZE *max_block_size) {
2138 VP9_COMMON *const cm = &cpi->common;
2139 MODE_INFO *mi_8x8 = xd->mi;
2140 const int left_in_image = xd->left_available && mi_8x8[-1].src_mi;
2141 const int above_in_image = xd->up_available &&
2142 mi_8x8[-xd->mi_stride].src_mi;
2143 int row8x8_remaining = tile->mi_row_end - mi_row;
2144 int col8x8_remaining = tile->mi_col_end - mi_col;
2146 BLOCK_SIZE min_size = BLOCK_32X32;
2147 BLOCK_SIZE max_size = BLOCK_8X8;
2148 int bsl = mi_width_log2_lookup[BLOCK_64X64];
2149 const int search_range_ctrl = (((mi_row + mi_col) >> bsl) +
2150 get_chessboard_index(cm->current_video_frame)) & 0x1;
2151 // Trap case where we do not have a prediction.
2152 if (search_range_ctrl &&
2153 (left_in_image || above_in_image || cm->frame_type != KEY_FRAME)) {
2158 // Find the min and max partition sizes used in the left SB64.
2159 if (left_in_image) {
2161 mi = mi_8x8[-1].src_mi;
2162 for (block = 0; block < MI_BLOCK_SIZE; ++block) {
2163 cur_mi = mi[block * xd->mi_stride].src_mi;
2164 sb_type = cur_mi ? cur_mi->mbmi.sb_type : 0;
2165 min_size = MIN(min_size, sb_type);
2166 max_size = MAX(max_size, sb_type);
2169 // Find the min and max partition sizes used in the above SB64.
2170 if (above_in_image) {
2171 mi = mi_8x8[-xd->mi_stride * MI_BLOCK_SIZE].src_mi;
2172 for (block = 0; block < MI_BLOCK_SIZE; ++block) {
2173 sb_type = mi[block].src_mi ? mi[block].src_mi->mbmi.sb_type : 0;
2174 min_size = MIN(min_size, sb_type);
2175 max_size = MAX(max_size, sb_type);
2179 min_size = min_partition_size[min_size];
2180 max_size = find_partition_size(max_size, row8x8_remaining, col8x8_remaining,
2182 min_size = MIN(min_size, max_size);
2183 min_size = MAX(min_size, BLOCK_8X8);
2184 max_size = MIN(max_size, BLOCK_32X32);
2186 min_size = BLOCK_8X8;
2187 max_size = BLOCK_32X32;
2190 *min_block_size = min_size;
2191 *max_block_size = max_size;
2194 // TODO(jingning) refactor functions setting partition search range
2195 static void set_partition_range(VP9_COMMON *cm, MACROBLOCKD *xd,
2196 int mi_row, int mi_col, BLOCK_SIZE bsize,
2197 BLOCK_SIZE *min_bs, BLOCK_SIZE *max_bs) {
2198 int mi_width = num_8x8_blocks_wide_lookup[bsize];
2199 int mi_height = num_8x8_blocks_high_lookup[bsize];
2203 const int idx_str = cm->mi_stride * mi_row + mi_col;
2204 MODE_INFO *prev_mi = (cm->prev_mip + cm->mi_stride + 1 + idx_str)->src_mi;
2207 BLOCK_SIZE bs, min_size, max_size;
2209 min_size = BLOCK_64X64;
2210 max_size = BLOCK_4X4;
2213 for (idy = 0; idy < mi_height; ++idy) {
2214 for (idx = 0; idx < mi_width; ++idx) {
2215 mi = prev_mi[idy * cm->mi_stride + idx].src_mi;
2216 bs = mi ? mi->mbmi.sb_type : bsize;
2217 min_size = MIN(min_size, bs);
2218 max_size = MAX(max_size, bs);
2223 if (xd->left_available) {
2224 for (idy = 0; idy < mi_height; ++idy) {
2225 mi = xd->mi[idy * cm->mi_stride - 1].src_mi;
2226 bs = mi ? mi->mbmi.sb_type : bsize;
2227 min_size = MIN(min_size, bs);
2228 max_size = MAX(max_size, bs);
2232 if (xd->up_available) {
2233 for (idx = 0; idx < mi_width; ++idx) {
2234 mi = xd->mi[idx - cm->mi_stride].src_mi;
2235 bs = mi ? mi->mbmi.sb_type : bsize;
2236 min_size = MIN(min_size, bs);
2237 max_size = MAX(max_size, bs);
2241 if (min_size == max_size) {
2242 min_size = min_partition_size[min_size];
2243 max_size = max_partition_size[max_size];
2250 static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
2251 vpx_memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
2254 static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
2255 vpx_memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
2258 #if CONFIG_FP_MB_STATS
2259 const int num_16x16_blocks_wide_lookup[BLOCK_SIZES] =
2260 {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 4, 4};
2261 const int num_16x16_blocks_high_lookup[BLOCK_SIZES] =
2262 {1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 4, 2, 4};
2263 const int qindex_skip_threshold_lookup[BLOCK_SIZES] =
2264 {0, 10, 10, 30, 40, 40, 60, 80, 80, 90, 100, 100, 120};
2265 const int qindex_split_threshold_lookup[BLOCK_SIZES] =
2266 {0, 3, 3, 7, 15, 15, 30, 40, 40, 60, 80, 80, 120};
2267 const int complexity_16x16_blocks_threshold[BLOCK_SIZES] =
2268 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 6};
2279 static INLINE MOTION_DIRECTION get_motion_direction_fp(uint8_t fp_byte) {
2280 if (fp_byte & FPMB_MOTION_ZERO_MASK) {
2282 } else if (fp_byte & FPMB_MOTION_LEFT_MASK) {
2284 } else if (fp_byte & FPMB_MOTION_RIGHT_MASK) {
2286 } else if (fp_byte & FPMB_MOTION_UP_MASK) {
2293 static INLINE int get_motion_inconsistency(MOTION_DIRECTION this_mv,
2294 MOTION_DIRECTION that_mv) {
2295 if (this_mv == that_mv) {
2298 return abs(this_mv - that_mv) == 2 ? 2 : 1;
2303 // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
2304 // unlikely to be selected depending on previous rate-distortion optimization
2305 // results, for encoding speed-up.
2306 static void rd_pick_partition(VP9_COMP *cpi, ThreadData *td,
2307 TileDataEnc *tile_data,
2308 TOKENEXTRA **tp, int mi_row, int mi_col,
2309 BLOCK_SIZE bsize, RD_COST *rd_cost,
2310 int64_t best_rd, PC_TREE *pc_tree) {
2311 VP9_COMMON *const cm = &cpi->common;
2312 TileInfo *const tile_info = &tile_data->tile_info;
2313 MACROBLOCK *const x = &td->mb;
2314 MACROBLOCKD *const xd = &x->e_mbd;
2315 const int mi_step = num_8x8_blocks_wide_lookup[bsize] / 2;
2316 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2317 PARTITION_CONTEXT sl[8], sa[8];
2318 TOKENEXTRA *tp_orig = *tp;
2319 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
2322 RD_COST this_rdc, sum_rdc, best_rdc;
2323 int do_split = bsize >= BLOCK_8X8;
2326 // Override skipping rectangular partition operations for edge blocks
2327 const int force_horz_split = (mi_row + mi_step >= cm->mi_rows);
2328 const int force_vert_split = (mi_col + mi_step >= cm->mi_cols);
2329 const int xss = x->e_mbd.plane[1].subsampling_x;
2330 const int yss = x->e_mbd.plane[1].subsampling_y;
2332 BLOCK_SIZE min_size = x->min_partition_size;
2333 BLOCK_SIZE max_size = x->max_partition_size;
2335 #if CONFIG_FP_MB_STATS
2336 unsigned int src_diff_var = UINT_MAX;
2337 int none_complexity = 0;
2340 int partition_none_allowed = !force_horz_split && !force_vert_split;
2341 int partition_horz_allowed = !force_vert_split && yss <= xss &&
2343 int partition_vert_allowed = !force_horz_split && xss <= yss &&
2347 assert(num_8x8_blocks_wide_lookup[bsize] ==
2348 num_8x8_blocks_high_lookup[bsize]);
2350 vp9_rd_cost_init(&this_rdc);
2351 vp9_rd_cost_init(&sum_rdc);
2352 vp9_rd_cost_reset(&best_rdc);
2353 best_rdc.rdcost = best_rd;
2355 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2357 if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode)
2358 x->mb_energy = vp9_block_energy(cpi, x, bsize);
2360 if (cpi->sf.cb_partition_search && bsize == BLOCK_16X16) {
2361 int cb_partition_search_ctrl = ((pc_tree->index == 0 || pc_tree->index == 3)
2362 + get_chessboard_index(cm->current_video_frame)) & 0x1;
2364 if (cb_partition_search_ctrl && bsize > min_size && bsize < max_size)
2365 set_partition_range(cm, xd, mi_row, mi_col, bsize, &min_size, &max_size);
2368 // Determine partition types in search according to the speed features.
2369 // The threshold set here has to be of square block size.
2370 if (cpi->sf.auto_min_max_partition_size) {
2371 partition_none_allowed &= (bsize <= max_size && bsize >= min_size);
2372 partition_horz_allowed &= ((bsize <= max_size && bsize > min_size) ||
2374 partition_vert_allowed &= ((bsize <= max_size && bsize > min_size) ||
2376 do_split &= bsize > min_size;
2378 if (cpi->sf.use_square_partition_only) {
2379 partition_horz_allowed &= force_horz_split;
2380 partition_vert_allowed &= force_vert_split;
2383 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2385 #if CONFIG_FP_MB_STATS
2386 if (cpi->use_fp_mb_stats) {
2387 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2388 src_diff_var = get_sby_perpixel_diff_variance(cpi, &x->plane[0].src,
2389 mi_row, mi_col, bsize);
2393 #if CONFIG_FP_MB_STATS
2394 // Decide whether we shall split directly and skip searching NONE by using
2395 // the first pass block statistics
2396 if (cpi->use_fp_mb_stats && bsize >= BLOCK_32X32 && do_split &&
2397 partition_none_allowed && src_diff_var > 4 &&
2398 cm->base_qindex < qindex_split_threshold_lookup[bsize]) {
2399 int mb_row = mi_row >> 1;
2400 int mb_col = mi_col >> 1;
2402 MIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
2404 MIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
2407 // compute a complexity measure, basically measure inconsistency of motion
2408 // vectors obtained from the first pass in the current block
2409 for (r = mb_row; r < mb_row_end ; r++) {
2410 for (c = mb_col; c < mb_col_end; c++) {
2411 const int mb_index = r * cm->mb_cols + c;
2413 MOTION_DIRECTION this_mv;
2414 MOTION_DIRECTION right_mv;
2415 MOTION_DIRECTION bottom_mv;
2418 get_motion_direction_fp(cpi->twopass.this_frame_mb_stats[mb_index]);
2421 if (c != mb_col_end - 1) {
2422 right_mv = get_motion_direction_fp(
2423 cpi->twopass.this_frame_mb_stats[mb_index + 1]);
2424 none_complexity += get_motion_inconsistency(this_mv, right_mv);
2428 if (r != mb_row_end - 1) {
2429 bottom_mv = get_motion_direction_fp(
2430 cpi->twopass.this_frame_mb_stats[mb_index + cm->mb_cols]);
2431 none_complexity += get_motion_inconsistency(this_mv, bottom_mv);
2434 // do not count its left and top neighbors to avoid double counting
2438 if (none_complexity > complexity_16x16_blocks_threshold[bsize]) {
2439 partition_none_allowed = 0;
2445 if (partition_none_allowed) {
2446 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col,
2447 &this_rdc, bsize, ctx, best_rdc.rdcost);
2448 if (this_rdc.rate != INT_MAX) {
2449 if (bsize >= BLOCK_8X8) {
2450 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2451 this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
2452 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
2453 this_rdc.rate, this_rdc.dist);
2456 if (this_rdc.rdcost < best_rdc.rdcost) {
2457 int64_t dist_breakout_thr = cpi->sf.partition_search_breakout_dist_thr;
2458 int rate_breakout_thr = cpi->sf.partition_search_breakout_rate_thr;
2460 best_rdc = this_rdc;
2461 if (bsize >= BLOCK_8X8)
2462 pc_tree->partitioning = PARTITION_NONE;
2464 // Adjust dist breakout threshold according to the partition size.
2465 dist_breakout_thr >>= 8 - (b_width_log2_lookup[bsize] +
2466 b_height_log2_lookup[bsize]);
2468 rate_breakout_thr *= num_pels_log2_lookup[bsize];
2470 // If all y, u, v transform blocks in this partition are skippable, and
2471 // the dist & rate are within the thresholds, the partition search is
2472 // terminated for current branch of the partition search tree.
2473 // The dist & rate thresholds are set to 0 at speed 0 to disable the
2474 // early termination at that speed.
2475 if (!x->e_mbd.lossless &&
2476 (ctx->skippable && best_rdc.dist < dist_breakout_thr &&
2477 best_rdc.rate < rate_breakout_thr)) {
2482 #if CONFIG_FP_MB_STATS
2483 // Check if every 16x16 first pass block statistics has zero
2484 // motion and the corresponding first pass residue is small enough.
2485 // If that is the case, check the difference variance between the
2486 // current frame and the last frame. If the variance is small enough,
2487 // stop further splitting in RD optimization
2488 if (cpi->use_fp_mb_stats && do_split != 0 &&
2489 cm->base_qindex > qindex_skip_threshold_lookup[bsize]) {
2490 int mb_row = mi_row >> 1;
2491 int mb_col = mi_col >> 1;
2493 MIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
2495 MIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
2499 for (r = mb_row; r < mb_row_end; r++) {
2500 for (c = mb_col; c < mb_col_end; c++) {
2501 const int mb_index = r * cm->mb_cols + c;
2502 if (!(cpi->twopass.this_frame_mb_stats[mb_index] &
2503 FPMB_MOTION_ZERO_MASK) ||
2504 !(cpi->twopass.this_frame_mb_stats[mb_index] &
2505 FPMB_ERROR_SMALL_MASK)) {
2515 if (src_diff_var == UINT_MAX) {
2516 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2517 src_diff_var = get_sby_perpixel_diff_variance(
2518 cpi, &x->plane[0].src, mi_row, mi_col, bsize);
2520 if (src_diff_var < 8) {
2529 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2532 // store estimated motion vector
2533 if (cpi->sf.adaptive_motion_search)
2534 store_pred_mv(x, ctx);
2537 // TODO(jingning): use the motion vectors given by the above search as
2538 // the starting point of motion search in the following partition type check.
2540 subsize = get_subsize(bsize, PARTITION_SPLIT);
2541 if (bsize == BLOCK_8X8) {
2543 if (cpi->sf.adaptive_pred_interp_filter && partition_none_allowed)
2544 pc_tree->leaf_split[0]->pred_interp_filter =
2545 ctx->mic.mbmi.interp_filter;
2546 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
2547 pc_tree->leaf_split[0], best_rdc.rdcost);
2548 if (sum_rdc.rate == INT_MAX)
2549 sum_rdc.rdcost = INT64_MAX;
2551 for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
2552 const int x_idx = (i & 1) * mi_step;
2553 const int y_idx = (i >> 1) * mi_step;
2555 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
2558 if (cpi->sf.adaptive_motion_search)
2559 load_pred_mv(x, ctx);
2561 pc_tree->split[i]->index = i;
2562 rd_pick_partition(cpi, td, tile_data, tp,
2563 mi_row + y_idx, mi_col + x_idx,
2565 best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
2567 if (this_rdc.rate == INT_MAX) {
2568 sum_rdc.rdcost = INT64_MAX;
2571 sum_rdc.rate += this_rdc.rate;
2572 sum_rdc.dist += this_rdc.dist;
2573 sum_rdc.rdcost += this_rdc.rdcost;
2578 if (sum_rdc.rdcost < best_rdc.rdcost && i == 4) {
2579 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2580 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
2581 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
2582 sum_rdc.rate, sum_rdc.dist);
2584 if (sum_rdc.rdcost < best_rdc.rdcost) {
2586 pc_tree->partitioning = PARTITION_SPLIT;
2589 // skip rectangular partition test when larger block size
2590 // gives better rd cost
2591 if (cpi->sf.less_rectangular_check)
2592 do_rect &= !partition_none_allowed;
2594 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2598 if (partition_horz_allowed && do_rect) {
2599 subsize = get_subsize(bsize, PARTITION_HORZ);
2600 if (cpi->sf.adaptive_motion_search)
2601 load_pred_mv(x, ctx);
2602 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2603 partition_none_allowed)
2604 pc_tree->horizontal[0].pred_interp_filter =
2605 ctx->mic.mbmi.interp_filter;
2606 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
2607 &pc_tree->horizontal[0], best_rdc.rdcost);
2609 if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + mi_step < cm->mi_rows &&
2610 bsize > BLOCK_8X8) {
2611 PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
2612 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
2613 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
2615 if (cpi->sf.adaptive_motion_search)
2616 load_pred_mv(x, ctx);
2617 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2618 partition_none_allowed)
2619 pc_tree->horizontal[1].pred_interp_filter =
2620 ctx->mic.mbmi.interp_filter;
2621 rd_pick_sb_modes(cpi, tile_data, x, mi_row + mi_step, mi_col,
2622 &this_rdc, subsize, &pc_tree->horizontal[1],
2623 best_rdc.rdcost - sum_rdc.rdcost);
2624 if (this_rdc.rate == INT_MAX) {
2625 sum_rdc.rdcost = INT64_MAX;
2627 sum_rdc.rate += this_rdc.rate;
2628 sum_rdc.dist += this_rdc.dist;
2629 sum_rdc.rdcost += this_rdc.rdcost;
2633 if (sum_rdc.rdcost < best_rdc.rdcost) {
2634 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2635 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
2636 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
2637 if (sum_rdc.rdcost < best_rdc.rdcost) {
2639 pc_tree->partitioning = PARTITION_HORZ;
2642 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2645 if (partition_vert_allowed && do_rect) {
2646 subsize = get_subsize(bsize, PARTITION_VERT);
2648 if (cpi->sf.adaptive_motion_search)
2649 load_pred_mv(x, ctx);
2650 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2651 partition_none_allowed)
2652 pc_tree->vertical[0].pred_interp_filter =
2653 ctx->mic.mbmi.interp_filter;
2654 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
2655 &pc_tree->vertical[0], best_rdc.rdcost);
2656 if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + mi_step < cm->mi_cols &&
2657 bsize > BLOCK_8X8) {
2658 update_state(cpi, td, &pc_tree->vertical[0], mi_row, mi_col, subsize, 0);
2659 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize,
2660 &pc_tree->vertical[0]);
2662 if (cpi->sf.adaptive_motion_search)
2663 load_pred_mv(x, ctx);
2664 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2665 partition_none_allowed)
2666 pc_tree->vertical[1].pred_interp_filter =
2667 ctx->mic.mbmi.interp_filter;
2668 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + mi_step,
2670 &pc_tree->vertical[1], best_rdc.rdcost - sum_rdc.rdcost);
2671 if (this_rdc.rate == INT_MAX) {
2672 sum_rdc.rdcost = INT64_MAX;
2674 sum_rdc.rate += this_rdc.rate;
2675 sum_rdc.dist += this_rdc.dist;
2676 sum_rdc.rdcost += this_rdc.rdcost;
2680 if (sum_rdc.rdcost < best_rdc.rdcost) {
2681 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2682 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
2683 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
2684 sum_rdc.rate, sum_rdc.dist);
2685 if (sum_rdc.rdcost < best_rdc.rdcost) {
2687 pc_tree->partitioning = PARTITION_VERT;
2690 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2693 // TODO(jbb): This code added so that we avoid static analysis
2694 // warning related to the fact that best_rd isn't used after this
2695 // point. This code should be refactored so that the duplicate
2696 // checks occur in some sub function and thus are used...
2698 *rd_cost = best_rdc;
2701 if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX &&
2702 pc_tree->index != 3) {
2703 int output_enabled = (bsize == BLOCK_64X64);
2704 encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
2708 if (bsize == BLOCK_64X64) {
2709 assert(tp_orig < *tp);
2710 assert(best_rdc.rate < INT_MAX);
2711 assert(best_rdc.dist < INT64_MAX);
2713 assert(tp_orig == *tp);
2717 static void encode_rd_sb_row(VP9_COMP *cpi,
2719 TileDataEnc *tile_data,
2722 VP9_COMMON *const cm = &cpi->common;
2723 TileInfo *const tile_info = &tile_data->tile_info;
2724 MACROBLOCK *const x = &td->mb;
2725 MACROBLOCKD *const xd = &x->e_mbd;
2726 SPEED_FEATURES *const sf = &cpi->sf;
2729 // Initialize the left context for the new SB row
2730 vpx_memset(&xd->left_context, 0, sizeof(xd->left_context));
2731 vpx_memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
2733 // Code each SB in the row
2734 for (mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
2735 mi_col += MI_BLOCK_SIZE) {
2736 const struct segmentation *const seg = &cm->seg;
2743 const int idx_str = cm->mi_stride * mi_row + mi_col;
2744 MODE_INFO *mi = cm->mi + idx_str;
2746 if (sf->adaptive_pred_interp_filter) {
2747 for (i = 0; i < 64; ++i)
2748 td->leaf_tree[i].pred_interp_filter = SWITCHABLE;
2750 for (i = 0; i < 64; ++i) {
2751 td->pc_tree[i].vertical[0].pred_interp_filter = SWITCHABLE;
2752 td->pc_tree[i].vertical[1].pred_interp_filter = SWITCHABLE;
2753 td->pc_tree[i].horizontal[0].pred_interp_filter = SWITCHABLE;
2754 td->pc_tree[i].horizontal[1].pred_interp_filter = SWITCHABLE;
2758 vp9_zero(x->pred_mv);
2759 td->pc_root->index = 0;
2762 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
2763 : cm->last_frame_seg_map;
2764 int segment_id = vp9_get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
2765 seg_skip = vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP);
2768 x->source_variance = UINT_MAX;
2769 if (sf->partition_search_type == FIXED_PARTITION || seg_skip) {
2770 const BLOCK_SIZE bsize =
2771 seg_skip ? BLOCK_64X64 : sf->always_this_block_size;
2772 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
2773 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
2774 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
2775 BLOCK_64X64, &dummy_rate, &dummy_dist, 1, td->pc_root);
2776 } else if (cpi->partition_search_skippable_frame) {
2778 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
2779 bsize = get_rd_var_based_fixed_partition(cpi, x, mi_row, mi_col);
2780 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
2781 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
2782 BLOCK_64X64, &dummy_rate, &dummy_dist, 1, td->pc_root);
2783 } else if (sf->partition_search_type == VAR_BASED_PARTITION &&
2784 cm->frame_type != KEY_FRAME) {
2785 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
2786 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
2787 BLOCK_64X64, &dummy_rate, &dummy_dist, 1, td->pc_root);
2789 // If required set upper and lower partition size limits
2790 if (sf->auto_min_max_partition_size) {
2791 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
2792 rd_auto_partition_range(cpi, tile_info, xd, mi_row, mi_col,
2793 &x->min_partition_size,
2794 &x->max_partition_size);
2796 rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, BLOCK_64X64,
2797 &dummy_rdc, INT64_MAX, td->pc_root);
2802 static void init_encode_frame_mb_context(VP9_COMP *cpi) {
2803 MACROBLOCK *const x = &cpi->td.mb;
2804 VP9_COMMON *const cm = &cpi->common;
2805 MACROBLOCKD *const xd = &x->e_mbd;
2806 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
2808 // Copy data over into macro block data structures.
2809 vp9_setup_src_planes(x, cpi->Source, 0, 0);
2811 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
2813 // Note: this memset assumes above_context[0], [1] and [2]
2814 // are allocated as part of the same buffer.
2815 vpx_memset(xd->above_context[0], 0,
2816 sizeof(*xd->above_context[0]) *
2817 2 * aligned_mi_cols * MAX_MB_PLANE);
2818 vpx_memset(xd->above_seg_context, 0,
2819 sizeof(*xd->above_seg_context) * aligned_mi_cols);
2822 static int check_dual_ref_flags(VP9_COMP *cpi) {
2823 const int ref_flags = cpi->ref_frame_flags;
2825 if (vp9_segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
2828 return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG)
2829 + !!(ref_flags & VP9_ALT_FLAG)) >= 2;
2833 static void reset_skip_tx_size(VP9_COMMON *cm, TX_SIZE max_tx_size) {
2835 const int mis = cm->mi_stride;
2836 MODE_INFO *mi_ptr = cm->mi;
2838 for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row, mi_ptr += mis) {
2839 for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
2840 if (mi_ptr[mi_col].src_mi->mbmi.tx_size > max_tx_size)
2841 mi_ptr[mi_col].src_mi->mbmi.tx_size = max_tx_size;
2846 static MV_REFERENCE_FRAME get_frame_type(const VP9_COMP *cpi) {
2847 if (frame_is_intra_only(&cpi->common))
2849 else if (cpi->rc.is_src_frame_alt_ref && cpi->refresh_golden_frame)
2850 return ALTREF_FRAME;
2851 else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
2852 return GOLDEN_FRAME;
2857 static TX_MODE select_tx_mode(const VP9_COMP *cpi, MACROBLOCKD *const xd) {
2860 if (cpi->common.frame_type == KEY_FRAME &&
2861 cpi->sf.use_nonrd_pick_mode &&
2862 cpi->sf.partition_search_type == VAR_BASED_PARTITION)
2864 if (cpi->sf.tx_size_search_method == USE_LARGESTALL)
2866 else if (cpi->sf.tx_size_search_method == USE_FULL_RD||
2867 cpi->sf.tx_size_search_method == USE_TX_8X8)
2868 return TX_MODE_SELECT;
2870 return cpi->common.tx_mode;
2873 static void hybrid_intra_mode_search(VP9_COMP *cpi, MACROBLOCK *const x,
2874 RD_COST *rd_cost, BLOCK_SIZE bsize,
2875 PICK_MODE_CONTEXT *ctx) {
2876 if (bsize < BLOCK_16X16)
2877 vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, INT64_MAX);
2879 vp9_pick_intra_mode(cpi, x, rd_cost, bsize, ctx);
2882 static void nonrd_pick_sb_modes(VP9_COMP *cpi,
2883 TileDataEnc *tile_data, MACROBLOCK *const x,
2884 int mi_row, int mi_col, RD_COST *rd_cost,
2885 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
2886 VP9_COMMON *const cm = &cpi->common;
2887 TileInfo *const tile_info = &tile_data->tile_info;
2888 MACROBLOCKD *const xd = &x->e_mbd;
2890 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2891 mbmi = &xd->mi[0].src_mi->mbmi;
2892 mbmi->sb_type = bsize;
2894 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled)
2895 if (cyclic_refresh_segment_id_boosted(mbmi->segment_id))
2896 x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
2898 if (cm->frame_type == KEY_FRAME)
2899 hybrid_intra_mode_search(cpi, x, rd_cost, bsize, ctx);
2900 else if (vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
2901 set_mode_info_seg_skip(x, cm->tx_mode, rd_cost, bsize);
2902 else if (bsize >= BLOCK_8X8)
2903 vp9_pick_inter_mode(cpi, x, tile_data, mi_row, mi_col,
2904 rd_cost, bsize, ctx);
2906 vp9_pick_inter_mode_sub8x8(cpi, x, tile_data, mi_row, mi_col,
2907 rd_cost, bsize, ctx);
2909 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
2911 if (rd_cost->rate == INT_MAX)
2912 vp9_rd_cost_reset(rd_cost);
2914 ctx->rate = rd_cost->rate;
2915 ctx->dist = rd_cost->dist;
2918 static void fill_mode_info_sb(VP9_COMMON *cm, MACROBLOCK *x,
2919 int mi_row, int mi_col,
2922 MACROBLOCKD *xd = &x->e_mbd;
2923 int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
2924 PARTITION_TYPE partition = pc_tree->partitioning;
2925 BLOCK_SIZE subsize = get_subsize(bsize, partition);
2927 assert(bsize >= BLOCK_8X8);
2929 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
2932 switch (partition) {
2933 case PARTITION_NONE:
2934 set_mode_info_offsets(cm, xd, mi_row, mi_col);
2935 *(xd->mi[0].src_mi) = pc_tree->none.mic;
2936 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
2938 case PARTITION_VERT:
2939 set_mode_info_offsets(cm, xd, mi_row, mi_col);
2940 *(xd->mi[0].src_mi) = pc_tree->vertical[0].mic;
2941 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
2943 if (mi_col + hbs < cm->mi_cols) {
2944 set_mode_info_offsets(cm, xd, mi_row, mi_col + hbs);
2945 *(xd->mi[0].src_mi) = pc_tree->vertical[1].mic;
2946 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col + hbs, subsize);
2949 case PARTITION_HORZ:
2950 set_mode_info_offsets(cm, xd, mi_row, mi_col);
2951 *(xd->mi[0].src_mi) = pc_tree->horizontal[0].mic;
2952 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
2953 if (mi_row + hbs < cm->mi_rows) {
2954 set_mode_info_offsets(cm, xd, mi_row + hbs, mi_col);
2955 *(xd->mi[0].src_mi) = pc_tree->horizontal[1].mic;
2956 duplicate_mode_info_in_sb(cm, xd, mi_row + hbs, mi_col, subsize);
2959 case PARTITION_SPLIT: {
2960 fill_mode_info_sb(cm, x, mi_row, mi_col, subsize, pc_tree->split[0]);
2961 fill_mode_info_sb(cm, x, mi_row, mi_col + hbs, subsize,
2963 fill_mode_info_sb(cm, x, mi_row + hbs, mi_col, subsize,
2965 fill_mode_info_sb(cm, x, mi_row + hbs, mi_col + hbs, subsize,
2974 // Reset the prediction pixel ready flag recursively.
2975 static void pred_pixel_ready_reset(PC_TREE *pc_tree, BLOCK_SIZE bsize) {
2976 pc_tree->none.pred_pixel_ready = 0;
2977 pc_tree->horizontal[0].pred_pixel_ready = 0;
2978 pc_tree->horizontal[1].pred_pixel_ready = 0;
2979 pc_tree->vertical[0].pred_pixel_ready = 0;
2980 pc_tree->vertical[1].pred_pixel_ready = 0;
2982 if (bsize > BLOCK_8X8) {
2983 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_SPLIT);
2985 for (i = 0; i < 4; ++i)
2986 pred_pixel_ready_reset(pc_tree->split[i], subsize);
2990 static void nonrd_pick_partition(VP9_COMP *cpi, ThreadData *td,
2991 TileDataEnc *tile_data,
2992 TOKENEXTRA **tp, int mi_row,
2993 int mi_col, BLOCK_SIZE bsize, RD_COST *rd_cost,
2994 int do_recon, int64_t best_rd,
2996 const SPEED_FEATURES *const sf = &cpi->sf;
2997 VP9_COMMON *const cm = &cpi->common;
2998 TileInfo *const tile_info = &tile_data->tile_info;
2999 MACROBLOCK *const x = &td->mb;
3000 MACROBLOCKD *const xd = &x->e_mbd;
3001 const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
3002 TOKENEXTRA *tp_orig = *tp;
3003 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
3005 BLOCK_SIZE subsize = bsize;
3006 RD_COST this_rdc, sum_rdc, best_rdc;
3007 int do_split = bsize >= BLOCK_8X8;
3009 // Override skipping rectangular partition operations for edge blocks
3010 const int force_horz_split = (mi_row + ms >= cm->mi_rows);
3011 const int force_vert_split = (mi_col + ms >= cm->mi_cols);
3012 const int xss = x->e_mbd.plane[1].subsampling_x;
3013 const int yss = x->e_mbd.plane[1].subsampling_y;
3015 int partition_none_allowed = !force_horz_split && !force_vert_split;
3016 int partition_horz_allowed = !force_vert_split && yss <= xss &&
3018 int partition_vert_allowed = !force_horz_split && xss <= yss &&
3022 assert(num_8x8_blocks_wide_lookup[bsize] ==
3023 num_8x8_blocks_high_lookup[bsize]);
3025 vp9_rd_cost_init(&sum_rdc);
3026 vp9_rd_cost_reset(&best_rdc);
3027 best_rdc.rdcost = best_rd;
3029 // Determine partition types in search according to the speed features.
3030 // The threshold set here has to be of square block size.
3031 if (sf->auto_min_max_partition_size) {
3032 partition_none_allowed &= (bsize <= x->max_partition_size &&
3033 bsize >= x->min_partition_size);
3034 partition_horz_allowed &= ((bsize <= x->max_partition_size &&
3035 bsize > x->min_partition_size) ||
3037 partition_vert_allowed &= ((bsize <= x->max_partition_size &&
3038 bsize > x->min_partition_size) ||
3040 do_split &= bsize > x->min_partition_size;
3042 if (sf->use_square_partition_only) {
3043 partition_horz_allowed &= force_horz_split;
3044 partition_vert_allowed &= force_vert_split;
3047 ctx->pred_pixel_ready = !(partition_vert_allowed ||
3048 partition_horz_allowed ||
3052 if (partition_none_allowed) {
3053 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col,
3054 &this_rdc, bsize, ctx);
3055 ctx->mic.mbmi = xd->mi[0].src_mi->mbmi;
3056 ctx->skip_txfm[0] = x->skip_txfm[0];
3057 ctx->skip = x->skip;
3059 if (this_rdc.rate != INT_MAX) {
3060 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3061 this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
3062 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
3063 this_rdc.rate, this_rdc.dist);
3064 if (this_rdc.rdcost < best_rdc.rdcost) {
3065 int64_t dist_breakout_thr = sf->partition_search_breakout_dist_thr;
3066 int64_t rate_breakout_thr = sf->partition_search_breakout_rate_thr;
3068 dist_breakout_thr >>= 8 - (b_width_log2_lookup[bsize] +
3069 b_height_log2_lookup[bsize]);
3071 rate_breakout_thr *= num_pels_log2_lookup[bsize];
3073 best_rdc = this_rdc;
3074 if (bsize >= BLOCK_8X8)
3075 pc_tree->partitioning = PARTITION_NONE;
3077 if (!x->e_mbd.lossless &&
3078 this_rdc.rate < rate_breakout_thr &&
3079 this_rdc.dist < dist_breakout_thr) {
3087 // store estimated motion vector
3088 store_pred_mv(x, ctx);
3092 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3093 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
3094 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3095 subsize = get_subsize(bsize, PARTITION_SPLIT);
3096 for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
3097 const int x_idx = (i & 1) * ms;
3098 const int y_idx = (i >> 1) * ms;
3100 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
3102 load_pred_mv(x, ctx);
3103 nonrd_pick_partition(cpi, td, tile_data, tp,
3104 mi_row + y_idx, mi_col + x_idx,
3105 subsize, &this_rdc, 0,
3106 best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
3108 if (this_rdc.rate == INT_MAX) {
3109 vp9_rd_cost_reset(&sum_rdc);
3111 sum_rdc.rate += this_rdc.rate;
3112 sum_rdc.dist += this_rdc.dist;
3113 sum_rdc.rdcost += this_rdc.rdcost;
3117 if (sum_rdc.rdcost < best_rdc.rdcost) {
3119 pc_tree->partitioning = PARTITION_SPLIT;
3121 // skip rectangular partition test when larger block size
3122 // gives better rd cost
3123 if (sf->less_rectangular_check)
3124 do_rect &= !partition_none_allowed;
3129 if (partition_horz_allowed && do_rect) {
3130 subsize = get_subsize(bsize, PARTITION_HORZ);
3131 if (sf->adaptive_motion_search)
3132 load_pred_mv(x, ctx);
3133 pc_tree->horizontal[0].pred_pixel_ready = 1;
3134 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3135 &pc_tree->horizontal[0]);
3137 pc_tree->horizontal[0].mic.mbmi = xd->mi[0].src_mi->mbmi;
3138 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3139 pc_tree->horizontal[0].skip = x->skip;
3141 if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + ms < cm->mi_rows) {
3142 load_pred_mv(x, ctx);
3143 pc_tree->horizontal[1].pred_pixel_ready = 1;
3144 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + ms, mi_col,
3146 &pc_tree->horizontal[1]);
3148 pc_tree->horizontal[1].mic.mbmi = xd->mi[0].src_mi->mbmi;
3149 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3150 pc_tree->horizontal[1].skip = x->skip;
3152 if (this_rdc.rate == INT_MAX) {
3153 vp9_rd_cost_reset(&sum_rdc);
3155 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3156 this_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
3157 sum_rdc.rate += this_rdc.rate;
3158 sum_rdc.dist += this_rdc.dist;
3159 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
3160 sum_rdc.rate, sum_rdc.dist);
3164 if (sum_rdc.rdcost < best_rdc.rdcost) {
3166 pc_tree->partitioning = PARTITION_HORZ;
3168 pred_pixel_ready_reset(pc_tree, bsize);
3173 if (partition_vert_allowed && do_rect) {
3174 subsize = get_subsize(bsize, PARTITION_VERT);
3175 if (sf->adaptive_motion_search)
3176 load_pred_mv(x, ctx);
3177 pc_tree->vertical[0].pred_pixel_ready = 1;
3178 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3179 &pc_tree->vertical[0]);
3180 pc_tree->vertical[0].mic.mbmi = xd->mi[0].src_mi->mbmi;
3181 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
3182 pc_tree->vertical[0].skip = x->skip;
3184 if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + ms < cm->mi_cols) {
3185 load_pred_mv(x, ctx);
3186 pc_tree->vertical[1].pred_pixel_ready = 1;
3187 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + ms,
3189 &pc_tree->vertical[1]);
3190 pc_tree->vertical[1].mic.mbmi = xd->mi[0].src_mi->mbmi;
3191 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
3192 pc_tree->vertical[1].skip = x->skip;
3194 if (this_rdc.rate == INT_MAX) {
3195 vp9_rd_cost_reset(&sum_rdc);
3197 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3198 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
3199 sum_rdc.rate += this_rdc.rate;
3200 sum_rdc.dist += this_rdc.dist;
3201 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
3202 sum_rdc.rate, sum_rdc.dist);
3206 if (sum_rdc.rdcost < best_rdc.rdcost) {
3208 pc_tree->partitioning = PARTITION_VERT;
3210 pred_pixel_ready_reset(pc_tree, bsize);
3214 *rd_cost = best_rdc;
3216 if (best_rdc.rate == INT_MAX) {
3217 vp9_rd_cost_reset(rd_cost);
3221 // update mode info array
3222 fill_mode_info_sb(cm, x, mi_row, mi_col, bsize, pc_tree);
3224 if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX && do_recon) {
3225 int output_enabled = (bsize == BLOCK_64X64);
3226 encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
3230 if (bsize == BLOCK_64X64 && do_recon) {
3231 assert(tp_orig < *tp);
3232 assert(best_rdc.rate < INT_MAX);
3233 assert(best_rdc.dist < INT64_MAX);
3235 assert(tp_orig == *tp);
3239 static void nonrd_select_partition(VP9_COMP *cpi,
3241 TileDataEnc *tile_data,
3244 int mi_row, int mi_col,
3245 BLOCK_SIZE bsize, int output_enabled,
3246 RD_COST *rd_cost, PC_TREE *pc_tree) {
3247 VP9_COMMON *const cm = &cpi->common;
3248 TileInfo *const tile_info = &tile_data->tile_info;
3249 MACROBLOCK *const x = &td->mb;
3250 MACROBLOCKD *const xd = &x->e_mbd;
3251 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3252 const int mis = cm->mi_stride;
3253 PARTITION_TYPE partition;
3257 vp9_rd_cost_reset(&this_rdc);
3258 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
3261 subsize = (bsize >= BLOCK_8X8) ? mi[0].src_mi->mbmi.sb_type : BLOCK_4X4;
3262 partition = partition_lookup[bsl][subsize];
3264 if (bsize == BLOCK_32X32 && partition != PARTITION_NONE &&
3265 subsize >= BLOCK_16X16) {
3266 x->max_partition_size = BLOCK_32X32;
3267 x->min_partition_size = BLOCK_8X8;
3268 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize,
3269 rd_cost, 0, INT64_MAX, pc_tree);
3270 } else if (bsize == BLOCK_16X16 && partition != PARTITION_NONE) {
3271 x->max_partition_size = BLOCK_16X16;
3272 x->min_partition_size = BLOCK_8X8;
3273 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize,
3274 rd_cost, 0, INT64_MAX, pc_tree);
3276 switch (partition) {
3277 case PARTITION_NONE:
3278 pc_tree->none.pred_pixel_ready = 1;
3279 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost,
3280 subsize, &pc_tree->none);
3281 pc_tree->none.mic.mbmi = xd->mi[0].src_mi->mbmi;
3282 pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
3283 pc_tree->none.skip = x->skip;
3285 case PARTITION_VERT:
3286 pc_tree->vertical[0].pred_pixel_ready = 1;
3287 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost,
3288 subsize, &pc_tree->vertical[0]);
3289 pc_tree->vertical[0].mic.mbmi = xd->mi[0].src_mi->mbmi;
3290 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
3291 pc_tree->vertical[0].skip = x->skip;
3292 if (mi_col + hbs < cm->mi_cols) {
3293 pc_tree->vertical[1].pred_pixel_ready = 1;
3294 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs,
3295 &this_rdc, subsize, &pc_tree->vertical[1]);
3296 pc_tree->vertical[1].mic.mbmi = xd->mi[0].src_mi->mbmi;
3297 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
3298 pc_tree->vertical[1].skip = x->skip;
3299 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3300 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3301 rd_cost->rate += this_rdc.rate;
3302 rd_cost->dist += this_rdc.dist;
3306 case PARTITION_HORZ:
3307 pc_tree->horizontal[0].pred_pixel_ready = 1;
3308 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost,
3309 subsize, &pc_tree->horizontal[0]);
3310 pc_tree->horizontal[0].mic.mbmi = xd->mi[0].src_mi->mbmi;
3311 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3312 pc_tree->horizontal[0].skip = x->skip;
3313 if (mi_row + hbs < cm->mi_rows) {
3314 pc_tree->horizontal[1].pred_pixel_ready = 1;
3315 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col,
3316 &this_rdc, subsize, &pc_tree->horizontal[1]);
3317 pc_tree->horizontal[1].mic.mbmi = xd->mi[0].src_mi->mbmi;
3318 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3319 pc_tree->horizontal[1].skip = x->skip;
3320 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3321 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3322 rd_cost->rate += this_rdc.rate;
3323 rd_cost->dist += this_rdc.dist;
3327 case PARTITION_SPLIT:
3328 subsize = get_subsize(bsize, PARTITION_SPLIT);
3329 nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3330 subsize, output_enabled, rd_cost,
3332 nonrd_select_partition(cpi, td, tile_data, mi + hbs, tp,
3333 mi_row, mi_col + hbs, subsize, output_enabled,
3334 &this_rdc, pc_tree->split[1]);
3335 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3336 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3337 rd_cost->rate += this_rdc.rate;
3338 rd_cost->dist += this_rdc.dist;
3340 nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis, tp,
3341 mi_row + hbs, mi_col, subsize, output_enabled,
3342 &this_rdc, pc_tree->split[2]);
3343 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3344 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3345 rd_cost->rate += this_rdc.rate;
3346 rd_cost->dist += this_rdc.dist;
3348 nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
3349 mi_row + hbs, mi_col + hbs, subsize,
3350 output_enabled, &this_rdc, pc_tree->split[3]);
3351 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3352 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3353 rd_cost->rate += this_rdc.rate;
3354 rd_cost->dist += this_rdc.dist;
3358 assert(0 && "Invalid partition type.");
3363 if (bsize == BLOCK_64X64 && output_enabled)
3364 encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, 1, bsize, pc_tree);
3368 static void nonrd_use_partition(VP9_COMP *cpi,
3370 TileDataEnc *tile_data,
3373 int mi_row, int mi_col,
3374 BLOCK_SIZE bsize, int output_enabled,
3375 RD_COST *dummy_cost, PC_TREE *pc_tree) {
3376 VP9_COMMON *const cm = &cpi->common;
3377 TileInfo *tile_info = &tile_data->tile_info;
3378 MACROBLOCK *const x = &td->mb;
3379 MACROBLOCKD *const xd = &x->e_mbd;
3380 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3381 const int mis = cm->mi_stride;
3382 PARTITION_TYPE partition;
3385 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
3388 subsize = (bsize >= BLOCK_8X8) ? mi[0].src_mi->mbmi.sb_type : BLOCK_4X4;
3389 partition = partition_lookup[bsl][subsize];
3391 if (output_enabled && bsize != BLOCK_4X4) {
3392 int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
3393 td->counts->partition[ctx][partition]++;
3396 switch (partition) {
3397 case PARTITION_NONE:
3398 pc_tree->none.pred_pixel_ready = 1;
3399 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
3400 subsize, &pc_tree->none);
3401 pc_tree->none.mic.mbmi = xd->mi[0].src_mi->mbmi;
3402 pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
3403 pc_tree->none.skip = x->skip;
3404 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
3405 subsize, &pc_tree->none);
3407 case PARTITION_VERT:
3408 pc_tree->vertical[0].pred_pixel_ready = 1;
3409 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
3410 subsize, &pc_tree->vertical[0]);
3411 pc_tree->vertical[0].mic.mbmi = xd->mi[0].src_mi->mbmi;
3412 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
3413 pc_tree->vertical[0].skip = x->skip;
3414 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
3415 subsize, &pc_tree->vertical[0]);
3416 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
3417 pc_tree->vertical[1].pred_pixel_ready = 1;
3418 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs,
3419 dummy_cost, subsize, &pc_tree->vertical[1]);
3420 pc_tree->vertical[1].mic.mbmi = xd->mi[0].src_mi->mbmi;
3421 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
3422 pc_tree->vertical[1].skip = x->skip;
3423 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col + hbs,
3424 output_enabled, subsize, &pc_tree->vertical[1]);
3427 case PARTITION_HORZ:
3428 pc_tree->horizontal[0].pred_pixel_ready = 1;
3429 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
3430 subsize, &pc_tree->horizontal[0]);
3431 pc_tree->horizontal[0].mic.mbmi = xd->mi[0].src_mi->mbmi;
3432 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3433 pc_tree->horizontal[0].skip = x->skip;
3434 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
3435 subsize, &pc_tree->horizontal[0]);
3437 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
3438 pc_tree->horizontal[1].pred_pixel_ready = 1;
3439 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col,
3440 dummy_cost, subsize, &pc_tree->horizontal[1]);
3441 pc_tree->horizontal[1].mic.mbmi = xd->mi[0].src_mi->mbmi;
3442 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3443 pc_tree->horizontal[1].skip = x->skip;
3444 encode_b_rt(cpi, td, tile_info, tp, mi_row + hbs, mi_col,
3445 output_enabled, subsize, &pc_tree->horizontal[1]);
3448 case PARTITION_SPLIT:
3449 subsize = get_subsize(bsize, PARTITION_SPLIT);
3450 if (bsize == BLOCK_8X8) {
3451 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
3452 subsize, pc_tree->leaf_split[0]);
3453 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col,
3454 output_enabled, subsize, pc_tree->leaf_split[0]);
3456 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3457 subsize, output_enabled, dummy_cost,
3459 nonrd_use_partition(cpi, td, tile_data, mi + hbs, tp,
3460 mi_row, mi_col + hbs, subsize, output_enabled,
3461 dummy_cost, pc_tree->split[1]);
3462 nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis, tp,
3463 mi_row + hbs, mi_col, subsize, output_enabled,
3464 dummy_cost, pc_tree->split[2]);
3465 nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
3466 mi_row + hbs, mi_col + hbs, subsize, output_enabled,
3467 dummy_cost, pc_tree->split[3]);
3471 assert(0 && "Invalid partition type.");
3475 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
3476 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
3479 static void encode_nonrd_sb_row(VP9_COMP *cpi,
3481 TileDataEnc *tile_data,
3484 SPEED_FEATURES *const sf = &cpi->sf;
3485 VP9_COMMON *const cm = &cpi->common;
3486 TileInfo *const tile_info = &tile_data->tile_info;
3487 MACROBLOCK *const x = &td->mb;
3488 MACROBLOCKD *const xd = &x->e_mbd;
3491 // Initialize the left context for the new SB row
3492 vpx_memset(&xd->left_context, 0, sizeof(xd->left_context));
3493 vpx_memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
3495 // Code each SB in the row
3496 for (mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
3497 mi_col += MI_BLOCK_SIZE) {
3498 const struct segmentation *const seg = &cm->seg;
3500 const int idx_str = cm->mi_stride * mi_row + mi_col;
3501 MODE_INFO *mi = cm->mi + idx_str;
3502 PARTITION_SEARCH_TYPE partition_search_type = sf->partition_search_type;
3503 BLOCK_SIZE bsize = BLOCK_64X64;
3505 x->source_variance = UINT_MAX;
3506 vp9_zero(x->pred_mv);
3507 vp9_rd_cost_init(&dummy_rdc);
3508 x->color_sensitivity[0] = 0;
3509 x->color_sensitivity[1] = 0;
3512 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
3513 : cm->last_frame_seg_map;
3514 int segment_id = vp9_get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
3515 seg_skip = vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP);
3517 partition_search_type = FIXED_PARTITION;
3521 // Set the partition type of the 64X64 block
3522 switch (partition_search_type) {
3523 case VAR_BASED_PARTITION:
3524 // TODO(jingning, marpan): The mode decision and encoding process
3525 // support both intra and inter sub8x8 block coding for RTC mode.
3526 // Tune the thresholds accordingly to use sub8x8 block coding for
3527 // coding performance improvement.
3528 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
3529 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3530 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
3532 case SOURCE_VAR_BASED_PARTITION:
3533 set_source_var_based_partition(cpi, tile_info, x, mi, mi_row, mi_col);
3534 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3535 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
3537 case FIXED_PARTITION:
3539 bsize = sf->always_this_block_size;
3540 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
3541 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3542 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
3544 case REFERENCE_PARTITION:
3545 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3546 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
3547 xd->mi[0].src_mi->mbmi.segment_id) {
3548 x->max_partition_size = BLOCK_64X64;
3549 x->min_partition_size = BLOCK_8X8;
3550 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col,
3551 BLOCK_64X64, &dummy_rdc, 1,
3552 INT64_MAX, td->pc_root);
3554 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
3555 nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3556 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
3566 // end RTC play code
3568 static int set_var_thresh_from_histogram(VP9_COMP *cpi) {
3569 const SPEED_FEATURES *const sf = &cpi->sf;
3570 const VP9_COMMON *const cm = &cpi->common;
3572 const uint8_t *src = cpi->Source->y_buffer;
3573 const uint8_t *last_src = cpi->Last_Source->y_buffer;
3574 const int src_stride = cpi->Source->y_stride;
3575 const int last_stride = cpi->Last_Source->y_stride;
3577 // Pick cutoff threshold
3578 const int cutoff = (MIN(cm->width, cm->height) >= 720) ?
3579 (cm->MBs * VAR_HIST_LARGE_CUT_OFF / 100) :
3580 (cm->MBs * VAR_HIST_SMALL_CUT_OFF / 100);
3581 DECLARE_ALIGNED_ARRAY(16, int, hist, VAR_HIST_BINS);
3582 diff *var16 = cpi->source_diff_var;
3587 vpx_memset(hist, 0, VAR_HIST_BINS * sizeof(hist[0]));
3589 for (i = 0; i < cm->mb_rows; i++) {
3590 for (j = 0; j < cm->mb_cols; j++) {
3591 #if CONFIG_VP9_HIGHBITDEPTH
3592 if (cm->use_highbitdepth) {
3593 switch (cm->bit_depth) {
3595 vp9_highbd_get16x16var(src, src_stride, last_src, last_stride,
3596 &var16->sse, &var16->sum);
3599 vp9_highbd_10_get16x16var(src, src_stride, last_src, last_stride,
3600 &var16->sse, &var16->sum);
3603 vp9_highbd_12_get16x16var(src, src_stride, last_src, last_stride,
3604 &var16->sse, &var16->sum);
3607 assert(0 && "cm->bit_depth should be VPX_BITS_8, VPX_BITS_10"
3612 vp9_get16x16var(src, src_stride, last_src, last_stride,
3613 &var16->sse, &var16->sum);
3616 vp9_get16x16var(src, src_stride, last_src, last_stride,
3617 &var16->sse, &var16->sum);
3618 #endif // CONFIG_VP9_HIGHBITDEPTH
3619 var16->var = var16->sse -
3620 (((uint32_t)var16->sum * var16->sum) >> 8);
3622 if (var16->var >= VAR_HIST_MAX_BG_VAR)
3623 hist[VAR_HIST_BINS - 1]++;
3625 hist[var16->var / VAR_HIST_FACTOR]++;
3632 src = src - cm->mb_cols * 16 + 16 * src_stride;
3633 last_src = last_src - cm->mb_cols * 16 + 16 * last_stride;
3636 cpi->source_var_thresh = 0;
3638 if (hist[VAR_HIST_BINS - 1] < cutoff) {
3639 for (i = 0; i < VAR_HIST_BINS - 1; i++) {
3643 cpi->source_var_thresh = (i + 1) * VAR_HIST_FACTOR;
3649 return sf->search_type_check_frequency;
3652 static void source_var_based_partition_search_method(VP9_COMP *cpi) {
3653 VP9_COMMON *const cm = &cpi->common;
3654 SPEED_FEATURES *const sf = &cpi->sf;
3656 if (cm->frame_type == KEY_FRAME) {
3657 // For key frame, use SEARCH_PARTITION.
3658 sf->partition_search_type = SEARCH_PARTITION;
3659 } else if (cm->intra_only) {
3660 sf->partition_search_type = FIXED_PARTITION;
3662 if (cm->last_width != cm->width || cm->last_height != cm->height) {
3663 if (cpi->source_diff_var)
3664 vpx_free(cpi->source_diff_var);
3666 CHECK_MEM_ERROR(cm, cpi->source_diff_var,
3667 vpx_calloc(cm->MBs, sizeof(diff)));
3670 if (!cpi->frames_till_next_var_check)
3671 cpi->frames_till_next_var_check = set_var_thresh_from_histogram(cpi);
3673 if (cpi->frames_till_next_var_check > 0) {
3674 sf->partition_search_type = FIXED_PARTITION;
3675 cpi->frames_till_next_var_check--;
3680 static int get_skip_encode_frame(const VP9_COMMON *cm, ThreadData *const td) {
3681 unsigned int intra_count = 0, inter_count = 0;
3684 for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
3685 intra_count += td->counts->intra_inter[j][0];
3686 inter_count += td->counts->intra_inter[j][1];
3689 return (intra_count << 2) < inter_count &&
3690 cm->frame_type != KEY_FRAME &&
3694 void vp9_init_tile_data(VP9_COMP *cpi) {
3695 VP9_COMMON *const cm = &cpi->common;
3696 const int tile_cols = 1 << cm->log2_tile_cols;
3697 const int tile_rows = 1 << cm->log2_tile_rows;
3698 int tile_col, tile_row;
3699 TOKENEXTRA *pre_tok = cpi->tile_tok[0][0];
3702 if (cpi->tile_data == NULL) {
3703 CHECK_MEM_ERROR(cm, cpi->tile_data,
3704 vpx_malloc(tile_cols * tile_rows * sizeof(*cpi->tile_data)));
3705 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
3706 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
3707 TileDataEnc *tile_data =
3708 &cpi->tile_data[tile_row * tile_cols + tile_col];
3710 for (i = 0; i < BLOCK_SIZES; ++i) {
3711 for (j = 0; j < MAX_MODES; ++j) {
3712 tile_data->thresh_freq_fact[i][j] = 32;
3713 tile_data->mode_map[i][j] = j;
3719 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
3720 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
3721 TileInfo *tile_info =
3722 &cpi->tile_data[tile_row * tile_cols + tile_col].tile_info;
3723 vp9_tile_init(tile_info, cm, tile_row, tile_col);
3725 cpi->tile_tok[tile_row][tile_col] = pre_tok + tile_tok;
3726 pre_tok = cpi->tile_tok[tile_row][tile_col];
3727 tile_tok = allocated_tokens(*tile_info);
3732 void vp9_encode_tile(VP9_COMP *cpi, ThreadData *td,
3733 int tile_row, int tile_col) {
3734 VP9_COMMON *const cm = &cpi->common;
3735 const int tile_cols = 1 << cm->log2_tile_cols;
3736 TileDataEnc *this_tile =
3737 &cpi->tile_data[tile_row * tile_cols + tile_col];
3738 const TileInfo * const tile_info = &this_tile->tile_info;
3739 TOKENEXTRA *tok = cpi->tile_tok[tile_row][tile_col];
3742 for (mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
3743 mi_row += MI_BLOCK_SIZE) {
3744 if (cpi->sf.use_nonrd_pick_mode)
3745 encode_nonrd_sb_row(cpi, td, this_tile, mi_row, &tok);
3747 encode_rd_sb_row(cpi, td, this_tile, mi_row, &tok);
3749 cpi->tok_count[tile_row][tile_col] =
3750 (unsigned int)(tok - cpi->tile_tok[tile_row][tile_col]);
3751 assert(tok - cpi->tile_tok[tile_row][tile_col] <=
3752 allocated_tokens(*tile_info));
3755 static void encode_tiles(VP9_COMP *cpi) {
3756 VP9_COMMON *const cm = &cpi->common;
3757 const int tile_cols = 1 << cm->log2_tile_cols;
3758 const int tile_rows = 1 << cm->log2_tile_rows;
3759 int tile_col, tile_row;
3761 vp9_init_tile_data(cpi);
3763 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
3764 for (tile_col = 0; tile_col < tile_cols; ++tile_col)
3765 vp9_encode_tile(cpi, &cpi->td, tile_row, tile_col);
3768 #if CONFIG_FP_MB_STATS
3769 static int input_fpmb_stats(FIRSTPASS_MB_STATS *firstpass_mb_stats,
3770 VP9_COMMON *cm, uint8_t **this_frame_mb_stats) {
3771 uint8_t *mb_stats_in = firstpass_mb_stats->mb_stats_start +
3772 cm->current_video_frame * cm->MBs * sizeof(uint8_t);
3774 if (mb_stats_in > firstpass_mb_stats->mb_stats_end)
3777 *this_frame_mb_stats = mb_stats_in;
3783 static void encode_frame_internal(VP9_COMP *cpi) {
3784 SPEED_FEATURES *const sf = &cpi->sf;
3785 RD_OPT *const rd_opt = &cpi->rd;
3786 ThreadData *const td = &cpi->td;
3787 MACROBLOCK *const x = &td->mb;
3788 VP9_COMMON *const cm = &cpi->common;
3789 MACROBLOCKD *const xd = &x->e_mbd;
3790 RD_COUNTS *const rdc = &cpi->td.rd_counts;
3793 xd->mi[0].src_mi = &xd->mi[0];
3795 vp9_zero(*td->counts);
3796 vp9_zero(rdc->coef_counts);
3797 vp9_zero(rdc->comp_pred_diff);
3798 vp9_zero(rdc->filter_diff);
3799 vp9_zero(rdc->tx_select_diff);
3800 vp9_zero(rd_opt->tx_select_threshes);
3802 xd->lossless = cm->base_qindex == 0 &&
3803 cm->y_dc_delta_q == 0 &&
3804 cm->uv_dc_delta_q == 0 &&
3805 cm->uv_ac_delta_q == 0;
3807 #if CONFIG_VP9_HIGHBITDEPTH
3808 if (cm->use_highbitdepth)
3809 x->fwd_txm4x4 = xd->lossless ? vp9_highbd_fwht4x4 : vp9_highbd_fdct4x4;
3811 x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3812 x->highbd_itxm_add = xd->lossless ? vp9_highbd_iwht4x4_add :
3813 vp9_highbd_idct4x4_add;
3815 x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3816 #endif // CONFIG_VP9_HIGHBITDEPTH
3817 x->itxm_add = xd->lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
3822 cm->tx_mode = select_tx_mode(cpi, xd);
3824 vp9_frame_init_quantizer(cpi);
3826 vp9_initialize_rd_consts(cpi);
3827 vp9_initialize_me_consts(cpi, x, cm->base_qindex);
3828 init_encode_frame_mb_context(cpi);
3829 cm->use_prev_frame_mvs = !cm->error_resilient_mode &&
3830 cm->width == cm->last_width &&
3831 cm->height == cm->last_height &&
3833 cm->last_show_frame;
3834 // Special case: set prev_mi to NULL when the previous mode info
3835 // context cannot be used.
3836 cm->prev_mi = cm->use_prev_frame_mvs ?
3837 cm->prev_mip + cm->mi_stride + 1 : NULL;
3839 x->quant_fp = cpi->sf.use_quant_fp;
3840 vp9_zero(x->skip_txfm);
3841 if (sf->use_nonrd_pick_mode) {
3842 // Initialize internal buffer pointers for rtc coding, where non-RD
3843 // mode decision is used and hence no buffer pointer swap needed.
3845 struct macroblock_plane *const p = x->plane;
3846 struct macroblockd_plane *const pd = xd->plane;
3847 PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
3849 for (i = 0; i < MAX_MB_PLANE; ++i) {
3850 p[i].coeff = ctx->coeff_pbuf[i][0];
3851 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
3852 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
3853 p[i].eobs = ctx->eobs_pbuf[i][0];
3855 vp9_zero(x->zcoeff_blk);
3857 if (sf->partition_search_type == SOURCE_VAR_BASED_PARTITION)
3858 source_var_based_partition_search_method(cpi);
3862 struct vpx_usec_timer emr_timer;
3863 vpx_usec_timer_start(&emr_timer);
3865 #if CONFIG_FP_MB_STATS
3866 if (cpi->use_fp_mb_stats) {
3867 input_fpmb_stats(&cpi->twopass.firstpass_mb_stats, cm,
3868 &cpi->twopass.this_frame_mb_stats);
3872 // If allowed, encoding tiles in parallel with one thread handling one tile.
3873 if (MIN(cpi->oxcf.max_threads, 1 << cm->log2_tile_cols) > 1)
3874 vp9_encode_tiles_mt(cpi);
3878 vpx_usec_timer_mark(&emr_timer);
3879 cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
3882 sf->skip_encode_frame = sf->skip_encode_sb ?
3883 get_skip_encode_frame(cm, td) : 0;
3886 // Keep record of the total distortion this time around for future use
3887 cpi->last_frame_distortion = cpi->frame_distortion;
3891 static INTERP_FILTER get_interp_filter(
3892 const int64_t threshes[SWITCHABLE_FILTER_CONTEXTS], int is_alt_ref) {
3894 threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP] &&
3895 threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP_SHARP] &&
3896 threshes[EIGHTTAP_SMOOTH] > threshes[SWITCHABLE - 1]) {
3897 return EIGHTTAP_SMOOTH;
3898 } else if (threshes[EIGHTTAP_SHARP] > threshes[EIGHTTAP] &&
3899 threshes[EIGHTTAP_SHARP] > threshes[SWITCHABLE - 1]) {
3900 return EIGHTTAP_SHARP;
3901 } else if (threshes[EIGHTTAP] > threshes[SWITCHABLE - 1]) {
3908 void vp9_encode_frame(VP9_COMP *cpi) {
3909 VP9_COMMON *const cm = &cpi->common;
3911 // In the longer term the encoder should be generalized to match the
3912 // decoder such that we allow compound where one of the 3 buffers has a
3913 // different sign bias and that buffer is then the fixed ref. However, this
3914 // requires further work in the rd loop. For now the only supported encoder
3915 // side behavior is where the ALT ref buffer has opposite sign bias to
3917 if (!frame_is_intra_only(cm)) {
3918 if ((cm->ref_frame_sign_bias[ALTREF_FRAME] ==
3919 cm->ref_frame_sign_bias[GOLDEN_FRAME]) ||
3920 (cm->ref_frame_sign_bias[ALTREF_FRAME] ==
3921 cm->ref_frame_sign_bias[LAST_FRAME])) {
3922 cpi->allow_comp_inter_inter = 0;
3924 cpi->allow_comp_inter_inter = 1;
3925 cm->comp_fixed_ref = ALTREF_FRAME;
3926 cm->comp_var_ref[0] = LAST_FRAME;
3927 cm->comp_var_ref[1] = GOLDEN_FRAME;
3931 if (cpi->sf.frame_parameter_update) {
3933 RD_OPT *const rd_opt = &cpi->rd;
3934 FRAME_COUNTS *counts = cpi->td.counts;
3935 RD_COUNTS *const rdc = &cpi->td.rd_counts;
3937 // This code does a single RD pass over the whole frame assuming
3938 // either compound, single or hybrid prediction as per whatever has
3939 // worked best for that type of frame in the past.
3940 // It also predicts whether another coding mode would have worked
3941 // better that this coding mode. If that is the case, it remembers
3942 // that for subsequent frames.
3943 // It does the same analysis for transform size selection also.
3944 const MV_REFERENCE_FRAME frame_type = get_frame_type(cpi);
3945 int64_t *const mode_thrs = rd_opt->prediction_type_threshes[frame_type];
3946 int64_t *const filter_thrs = rd_opt->filter_threshes[frame_type];
3947 int *const tx_thrs = rd_opt->tx_select_threshes[frame_type];
3948 const int is_alt_ref = frame_type == ALTREF_FRAME;
3950 /* prediction (compound, single or hybrid) mode selection */
3951 if (is_alt_ref || !cpi->allow_comp_inter_inter)
3952 cm->reference_mode = SINGLE_REFERENCE;
3953 else if (mode_thrs[COMPOUND_REFERENCE] > mode_thrs[SINGLE_REFERENCE] &&
3954 mode_thrs[COMPOUND_REFERENCE] >
3955 mode_thrs[REFERENCE_MODE_SELECT] &&
3956 check_dual_ref_flags(cpi) &&
3957 cpi->static_mb_pct == 100)
3958 cm->reference_mode = COMPOUND_REFERENCE;
3959 else if (mode_thrs[SINGLE_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT])
3960 cm->reference_mode = SINGLE_REFERENCE;
3962 cm->reference_mode = REFERENCE_MODE_SELECT;
3964 if (cm->interp_filter == SWITCHABLE)
3965 cm->interp_filter = get_interp_filter(filter_thrs, is_alt_ref);
3967 encode_frame_internal(cpi);
3969 for (i = 0; i < REFERENCE_MODES; ++i)
3970 mode_thrs[i] = (mode_thrs[i] + rdc->comp_pred_diff[i] / cm->MBs) / 2;
3972 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
3973 filter_thrs[i] = (filter_thrs[i] + rdc->filter_diff[i] / cm->MBs) / 2;
3975 for (i = 0; i < TX_MODES; ++i) {
3976 int64_t pd = rdc->tx_select_diff[i];
3977 if (i == TX_MODE_SELECT)
3978 pd -= RDCOST(cpi->td.mb.rdmult, cpi->td.mb.rddiv, 2048 * (TX_SIZES - 1),
3980 tx_thrs[i] = (tx_thrs[i] + (int)(pd / cm->MBs)) / 2;
3983 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
3984 int single_count_zero = 0;
3985 int comp_count_zero = 0;
3987 for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
3988 single_count_zero += counts->comp_inter[i][0];
3989 comp_count_zero += counts->comp_inter[i][1];
3992 if (comp_count_zero == 0) {
3993 cm->reference_mode = SINGLE_REFERENCE;
3994 vp9_zero(counts->comp_inter);
3995 } else if (single_count_zero == 0) {
3996 cm->reference_mode = COMPOUND_REFERENCE;
3997 vp9_zero(counts->comp_inter);
4001 if (cm->tx_mode == TX_MODE_SELECT) {
4003 int count8x8_lp = 0, count8x8_8x8p = 0;
4004 int count16x16_16x16p = 0, count16x16_lp = 0;
4007 for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
4008 count4x4 += counts->tx.p32x32[i][TX_4X4];
4009 count4x4 += counts->tx.p16x16[i][TX_4X4];
4010 count4x4 += counts->tx.p8x8[i][TX_4X4];
4012 count8x8_lp += counts->tx.p32x32[i][TX_8X8];
4013 count8x8_lp += counts->tx.p16x16[i][TX_8X8];
4014 count8x8_8x8p += counts->tx.p8x8[i][TX_8X8];
4016 count16x16_16x16p += counts->tx.p16x16[i][TX_16X16];
4017 count16x16_lp += counts->tx.p32x32[i][TX_16X16];
4018 count32x32 += counts->tx.p32x32[i][TX_32X32];
4020 if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0 &&
4022 cm->tx_mode = ALLOW_8X8;
4023 reset_skip_tx_size(cm, TX_8X8);
4024 } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0 &&
4025 count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
4026 cm->tx_mode = ONLY_4X4;
4027 reset_skip_tx_size(cm, TX_4X4);
4028 } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
4029 cm->tx_mode = ALLOW_32X32;
4030 } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
4031 cm->tx_mode = ALLOW_16X16;
4032 reset_skip_tx_size(cm, TX_16X16);
4036 cm->reference_mode = SINGLE_REFERENCE;
4037 encode_frame_internal(cpi);
4041 static void sum_intra_stats(FRAME_COUNTS *counts, const MODE_INFO *mi) {
4042 const PREDICTION_MODE y_mode = mi->mbmi.mode;
4043 const PREDICTION_MODE uv_mode = mi->mbmi.uv_mode;
4044 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
4046 if (bsize < BLOCK_8X8) {
4048 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
4049 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
4050 for (idy = 0; idy < 2; idy += num_4x4_h)
4051 for (idx = 0; idx < 2; idx += num_4x4_w)
4052 ++counts->y_mode[0][mi->bmi[idy * 2 + idx].as_mode];
4054 ++counts->y_mode[size_group_lookup[bsize]][y_mode];
4057 ++counts->uv_mode[y_mode][uv_mode];
4060 static void encode_superblock(VP9_COMP *cpi, ThreadData *td,
4061 TOKENEXTRA **t, int output_enabled,
4062 int mi_row, int mi_col, BLOCK_SIZE bsize,
4063 PICK_MODE_CONTEXT *ctx) {
4064 VP9_COMMON *const cm = &cpi->common;
4065 MACROBLOCK *const x = &td->mb;
4066 MACROBLOCKD *const xd = &x->e_mbd;
4067 MODE_INFO *mi_8x8 = xd->mi;
4068 MODE_INFO *mi = mi_8x8;
4069 MB_MODE_INFO *mbmi = &mi->mbmi;
4070 const int seg_skip = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
4072 const int mis = cm->mi_stride;
4073 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
4074 const int mi_height = num_8x8_blocks_high_lookup[bsize];
4076 x->skip_recode = !x->select_tx_size && mbmi->sb_type >= BLOCK_8X8 &&
4077 cpi->oxcf.aq_mode != COMPLEXITY_AQ &&
4078 cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ &&
4079 cpi->sf.allow_skip_recode;
4081 if (!x->skip_recode && !cpi->sf.use_nonrd_pick_mode)
4082 vpx_memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
4084 x->skip_optimize = ctx->is_coded;
4086 x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
4087 x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
4088 x->q_index < QIDX_SKIP_THRESH);
4093 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
4095 if (!is_inter_block(mbmi)) {
4098 for (plane = 0; plane < MAX_MB_PLANE; ++plane)
4099 vp9_encode_intra_block_plane(x, MAX(bsize, BLOCK_8X8), plane);
4101 sum_intra_stats(td->counts, mi);
4102 vp9_tokenize_sb(cpi, td, t, !output_enabled, MAX(bsize, BLOCK_8X8));
4105 const int is_compound = has_second_ref(mbmi);
4106 for (ref = 0; ref < 1 + is_compound; ++ref) {
4107 YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi,
4108 mbmi->ref_frame[ref]);
4109 assert(cfg != NULL);
4110 vp9_setup_pre_planes(xd, ref, cfg, mi_row, mi_col,
4111 &xd->block_refs[ref]->sf);
4113 if (!(cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready) || seg_skip)
4114 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8));
4116 vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8));
4118 vp9_encode_sb(x, MAX(bsize, BLOCK_8X8));
4119 vp9_tokenize_sb(cpi, td, t, !output_enabled, MAX(bsize, BLOCK_8X8));
4122 if (output_enabled) {
4123 if (cm->tx_mode == TX_MODE_SELECT &&
4124 mbmi->sb_type >= BLOCK_8X8 &&
4125 !(is_inter_block(mbmi) && (mbmi->skip || seg_skip))) {
4126 ++get_tx_counts(max_txsize_lookup[bsize], vp9_get_tx_size_context(xd),
4127 &td->counts->tx)[mbmi->tx_size];
4131 // The new intra coding scheme requires no change of transform size
4132 if (is_inter_block(&mi->mbmi)) {
4133 tx_size = MIN(tx_mode_to_biggest_tx_size[cm->tx_mode],
4134 max_txsize_lookup[bsize]);
4136 tx_size = (bsize >= BLOCK_8X8) ? mbmi->tx_size : TX_4X4;
4139 for (y = 0; y < mi_height; y++)
4140 for (x = 0; x < mi_width; x++)
4141 if (mi_col + x < cm->mi_cols && mi_row + y < cm->mi_rows)
4142 mi_8x8[mis * y + x].src_mi->mbmi.tx_size = tx_size;
4144 ++td->counts->tx.tx_totals[mbmi->tx_size];
4145 ++td->counts->tx.tx_totals[get_uv_tx_size(mbmi, &xd->plane[1])];