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_dsp_rtcd.h"
17 #include "./vpx_config.h"
19 #include "vpx_dsp/vpx_dsp_common.h"
20 #include "vpx_ports/mem.h"
21 #include "vpx_ports/vpx_timer.h"
22 #include "vpx_ports/system_state.h"
24 #include "vp9/common/vp9_common.h"
25 #include "vp9/common/vp9_entropy.h"
26 #include "vp9/common/vp9_entropymode.h"
27 #include "vp9/common/vp9_idct.h"
28 #include "vp9/common/vp9_mvref_common.h"
29 #include "vp9/common/vp9_pred_common.h"
30 #include "vp9/common/vp9_quant_common.h"
31 #include "vp9/common/vp9_reconintra.h"
32 #include "vp9/common/vp9_reconinter.h"
33 #include "vp9/common/vp9_seg_common.h"
34 #include "vp9/common/vp9_tile_common.h"
36 #include "vp9/encoder/vp9_aq_360.h"
37 #include "vp9/encoder/vp9_aq_complexity.h"
38 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
39 #include "vp9/encoder/vp9_aq_variance.h"
40 #include "vp9/encoder/vp9_encodeframe.h"
41 #include "vp9/encoder/vp9_encodemb.h"
42 #include "vp9/encoder/vp9_encodemv.h"
43 #include "vp9/encoder/vp9_ethread.h"
44 #include "vp9/encoder/vp9_extend.h"
45 #include "vp9/encoder/vp9_pickmode.h"
46 #include "vp9/encoder/vp9_rd.h"
47 #include "vp9/encoder/vp9_rdopt.h"
48 #include "vp9/encoder/vp9_segmentation.h"
49 #include "vp9/encoder/vp9_tokenize.h"
51 static void encode_superblock(VP9_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
52 int output_enabled, int mi_row, int mi_col,
53 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx);
55 // Machine learning-based early termination parameters.
56 static const double train_mean[24] = {
57 303501.697372, 3042630.372158, 24.694696, 1.392182,
58 689.413511, 162.027012, 1.478213, 0.0,
59 135382.260230, 912738.513263, 28.845217, 1.515230,
60 544.158492, 131.807995, 1.436863, 0.0,
61 43682.377587, 208131.711766, 28.084737, 1.356677,
62 138.254122, 119.522553, 1.252322, 0.0
65 static const double train_stdm[24] = {
66 673689.212982, 5996652.516628, 0.024449, 1.989792,
67 985.880847, 0.014638, 2.001898, 0.0,
68 208798.775332, 1812548.443284, 0.018693, 1.838009,
69 396.986910, 0.015657, 1.332541, 0.0,
70 55888.847031, 448587.962714, 0.017900, 1.904776,
71 98.652832, 0.016598, 1.320992, 0.0
74 // Error tolerance: 0.01%-0.0.05%-0.1%
75 static const double classifiers[24] = {
76 0.111736, 0.289977, 0.042219, 0.204765, 0.120410, -0.143863,
77 0.282376, 0.847811, 0.637161, 0.131570, 0.018636, 0.202134,
78 0.112797, 0.028162, 0.182450, 1.124367, 0.386133, 0.083700,
79 0.050028, 0.150873, 0.061119, 0.109318, 0.127255, 0.625211
82 // This is used as a reference when computing the source variance for the
83 // purpose of activity masking.
84 // Eventually this should be replaced by custom no-reference routines,
85 // which will be faster.
86 static const uint8_t VP9_VAR_OFFS[64] = {
87 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
88 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
89 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
90 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
91 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
94 #if CONFIG_VP9_HIGHBITDEPTH
95 static const uint16_t VP9_HIGH_VAR_OFFS_8[64] = {
96 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
97 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
98 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
99 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
100 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
103 static const uint16_t VP9_HIGH_VAR_OFFS_10[64] = {
104 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
105 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
106 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
107 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
108 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
109 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
110 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
111 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4
114 static const uint16_t VP9_HIGH_VAR_OFFS_12[64] = {
115 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
116 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
117 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
118 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
119 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
120 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
121 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
122 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
123 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
126 #endif // CONFIG_VP9_HIGHBITDEPTH
128 unsigned int vp9_get_sby_perpixel_variance(VP9_COMP *cpi,
129 const struct buf_2d *ref,
132 const unsigned int var =
133 cpi->fn_ptr[bs].vf(ref->buf, ref->stride, VP9_VAR_OFFS, 0, &sse);
134 return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
137 #if CONFIG_VP9_HIGHBITDEPTH
138 unsigned int vp9_high_get_sby_perpixel_variance(VP9_COMP *cpi,
139 const struct buf_2d *ref,
140 BLOCK_SIZE bs, int bd) {
141 unsigned int var, sse;
145 cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
146 CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10), 0, &sse);
150 cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
151 CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12), 0, &sse);
156 cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
157 CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8), 0, &sse);
160 return (unsigned int)ROUND64_POWER_OF_TWO((int64_t)var,
161 num_pels_log2_lookup[bs]);
163 #endif // CONFIG_VP9_HIGHBITDEPTH
165 static unsigned int get_sby_perpixel_diff_variance(VP9_COMP *cpi,
166 const struct buf_2d *ref,
167 int mi_row, int mi_col,
169 unsigned int sse, var;
171 const YV12_BUFFER_CONFIG *last = get_ref_frame_buffer(cpi, LAST_FRAME);
173 assert(last != NULL);
175 &last->y_buffer[mi_row * MI_SIZE * last->y_stride + mi_col * MI_SIZE];
176 var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride, last_y, last->y_stride, &sse);
177 return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
180 static BLOCK_SIZE get_rd_var_based_fixed_partition(VP9_COMP *cpi, MACROBLOCK *x,
181 int mi_row, int mi_col) {
182 unsigned int var = get_sby_perpixel_diff_variance(
183 cpi, &x->plane[0].src, mi_row, mi_col, BLOCK_64X64);
194 // Lighter version of set_offsets that only sets the mode info
196 static INLINE void set_mode_info_offsets(VP9_COMMON *const cm,
198 MACROBLOCKD *const xd, int mi_row,
200 const int idx_str = xd->mi_stride * mi_row + mi_col;
201 xd->mi = cm->mi_grid_visible + idx_str;
202 xd->mi[0] = cm->mi + idx_str;
203 x->mbmi_ext = x->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col);
206 static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile,
207 MACROBLOCK *const x, int mi_row, int mi_col,
209 VP9_COMMON *const cm = &cpi->common;
210 MACROBLOCKD *const xd = &x->e_mbd;
212 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
213 const int mi_height = num_8x8_blocks_high_lookup[bsize];
214 const struct segmentation *const seg = &cm->seg;
215 MvLimits *const mv_limits = &x->mv_limits;
217 set_skip_context(xd, mi_row, mi_col);
219 set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
223 // Set up destination pointers.
224 vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
226 // Set up limit values for MV components.
227 // Mv beyond the range do not produce new/different prediction block.
228 mv_limits->row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
229 mv_limits->col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
230 mv_limits->row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
231 mv_limits->col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;
233 // Set up distance of MB to edge of frame in 1/8th pel units.
234 assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
235 set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width, cm->mi_rows,
238 // Set up source buffers.
239 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
242 x->rddiv = cpi->rd.RDDIV;
243 x->rdmult = cpi->rd.RDMULT;
247 if (cpi->oxcf.aq_mode != VARIANCE_AQ && cpi->oxcf.aq_mode != LOOKAHEAD_AQ &&
248 cpi->oxcf.aq_mode != EQUATOR360_AQ) {
249 const uint8_t *const map =
250 seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
251 mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
253 vp9_init_plane_quantizers(cpi, x);
255 x->encode_breakout = cpi->segment_encode_breakout[mi->segment_id];
258 x->encode_breakout = cpi->encode_breakout;
261 // required by vp9_append_sub8x8_mvs_for_idx() and vp9_find_best_ref_mvs()
265 static void duplicate_mode_info_in_sb(VP9_COMMON *cm, MACROBLOCKD *xd,
266 int mi_row, int mi_col,
268 const int block_width =
269 VPXMIN(num_8x8_blocks_wide_lookup[bsize], cm->mi_cols - mi_col);
270 const int block_height =
271 VPXMIN(num_8x8_blocks_high_lookup[bsize], cm->mi_rows - mi_row);
272 const int mi_stride = xd->mi_stride;
273 MODE_INFO *const src_mi = xd->mi[0];
276 for (j = 0; j < block_height; ++j)
277 for (i = 0; i < block_width; ++i) xd->mi[j * mi_stride + i] = src_mi;
280 static void set_block_size(VP9_COMP *const cpi, MACROBLOCK *const x,
281 MACROBLOCKD *const xd, int mi_row, int mi_col,
283 if (cpi->common.mi_cols > mi_col && cpi->common.mi_rows > mi_row) {
284 set_mode_info_offsets(&cpi->common, x, xd, mi_row, mi_col);
285 xd->mi[0]->sb_type = bsize;
290 int64_t sum_square_error;
300 } partition_variance;
303 partition_variance part_variances;
308 partition_variance part_variances;
313 partition_variance part_variances;
318 partition_variance part_variances;
323 partition_variance part_variances;
328 partition_variance *part_variances;
338 static void tree_to_node(void *data, BLOCK_SIZE bsize, variance_node *node) {
340 node->part_variances = NULL;
343 v64x64 *vt = (v64x64 *)data;
344 node->part_variances = &vt->part_variances;
345 for (i = 0; i < 4; i++)
346 node->split[i] = &vt->split[i].part_variances.none;
350 v32x32 *vt = (v32x32 *)data;
351 node->part_variances = &vt->part_variances;
352 for (i = 0; i < 4; i++)
353 node->split[i] = &vt->split[i].part_variances.none;
357 v16x16 *vt = (v16x16 *)data;
358 node->part_variances = &vt->part_variances;
359 for (i = 0; i < 4; i++)
360 node->split[i] = &vt->split[i].part_variances.none;
364 v8x8 *vt = (v8x8 *)data;
365 node->part_variances = &vt->part_variances;
366 for (i = 0; i < 4; i++)
367 node->split[i] = &vt->split[i].part_variances.none;
371 v4x4 *vt = (v4x4 *)data;
372 node->part_variances = &vt->part_variances;
373 for (i = 0; i < 4; i++) node->split[i] = &vt->split[i];
383 // Set variance values given sum square error, sum error, count.
384 static void fill_variance(int64_t s2, int64_t s, int c, var *v) {
385 v->sum_square_error = s2;
390 static void get_variance(var *v) {
392 (int)(256 * (v->sum_square_error -
393 ((v->sum_error * v->sum_error) >> v->log2_count)) >>
397 static void sum_2_variances(const var *a, const var *b, var *r) {
398 assert(a->log2_count == b->log2_count);
399 fill_variance(a->sum_square_error + b->sum_square_error,
400 a->sum_error + b->sum_error, a->log2_count + 1, r);
403 static void fill_variance_tree(void *data, BLOCK_SIZE bsize) {
405 memset(&node, 0, sizeof(node));
406 tree_to_node(data, bsize, &node);
407 sum_2_variances(node.split[0], node.split[1], &node.part_variances->horz[0]);
408 sum_2_variances(node.split[2], node.split[3], &node.part_variances->horz[1]);
409 sum_2_variances(node.split[0], node.split[2], &node.part_variances->vert[0]);
410 sum_2_variances(node.split[1], node.split[3], &node.part_variances->vert[1]);
411 sum_2_variances(&node.part_variances->vert[0], &node.part_variances->vert[1],
412 &node.part_variances->none);
415 static int set_vt_partitioning(VP9_COMP *cpi, MACROBLOCK *const x,
416 MACROBLOCKD *const xd, void *data,
417 BLOCK_SIZE bsize, int mi_row, int mi_col,
418 int64_t threshold, BLOCK_SIZE bsize_min,
420 VP9_COMMON *const cm = &cpi->common;
422 const int block_width = num_8x8_blocks_wide_lookup[bsize];
423 const int block_height = num_8x8_blocks_high_lookup[bsize];
425 assert(block_height == block_width);
426 tree_to_node(data, bsize, &vt);
428 if (force_split == 1) return 0;
430 // For bsize=bsize_min (16x16/8x8 for 8x8/4x4 downsampling), select if
431 // variance is below threshold, otherwise split will be selected.
432 // No check for vert/horiz split as too few samples for variance.
433 if (bsize == bsize_min) {
434 // Variance already computed to set the force_split.
435 if (cm->frame_type == KEY_FRAME) get_variance(&vt.part_variances->none);
436 if (mi_col + block_width / 2 < cm->mi_cols &&
437 mi_row + block_height / 2 < cm->mi_rows &&
438 vt.part_variances->none.variance < threshold) {
439 set_block_size(cpi, x, xd, mi_row, mi_col, bsize);
443 } else if (bsize > bsize_min) {
444 // Variance already computed to set the force_split.
445 if (cm->frame_type == KEY_FRAME) get_variance(&vt.part_variances->none);
446 // For key frame: take split for bsize above 32X32 or very high variance.
447 if (cm->frame_type == KEY_FRAME &&
448 (bsize > BLOCK_32X32 ||
449 vt.part_variances->none.variance > (threshold << 4))) {
452 // If variance is low, take the bsize (no split).
453 if (mi_col + block_width / 2 < cm->mi_cols &&
454 mi_row + block_height / 2 < cm->mi_rows &&
455 vt.part_variances->none.variance < threshold) {
456 set_block_size(cpi, x, xd, mi_row, mi_col, bsize);
460 // Check vertical split.
461 if (mi_row + block_height / 2 < cm->mi_rows) {
462 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_VERT);
463 get_variance(&vt.part_variances->vert[0]);
464 get_variance(&vt.part_variances->vert[1]);
465 if (vt.part_variances->vert[0].variance < threshold &&
466 vt.part_variances->vert[1].variance < threshold &&
467 get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
468 set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
469 set_block_size(cpi, x, xd, mi_row, mi_col + block_width / 2, subsize);
473 // Check horizontal split.
474 if (mi_col + block_width / 2 < cm->mi_cols) {
475 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_HORZ);
476 get_variance(&vt.part_variances->horz[0]);
477 get_variance(&vt.part_variances->horz[1]);
478 if (vt.part_variances->horz[0].variance < threshold &&
479 vt.part_variances->horz[1].variance < threshold &&
480 get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
481 set_block_size(cpi, x, xd, mi_row, mi_col, subsize);
482 set_block_size(cpi, x, xd, mi_row + block_height / 2, mi_col, subsize);
492 int64_t scale_part_thresh_sumdiff(int64_t threshold_base, int speed, int width,
493 int height, int content_state) {
495 if (width <= 640 && height <= 480)
496 return (5 * threshold_base) >> 2;
497 else if ((content_state == kLowSadLowSumdiff) ||
498 (content_state == kHighSadLowSumdiff) ||
499 (content_state == kLowVarHighSumdiff))
500 return (5 * threshold_base) >> 2;
501 } else if (speed == 7) {
502 if ((content_state == kLowSadLowSumdiff) ||
503 (content_state == kHighSadLowSumdiff) ||
504 (content_state == kLowVarHighSumdiff)) {
505 return (5 * threshold_base) >> 2;
508 return threshold_base;
511 // Set the variance split thresholds for following the block sizes:
512 // 0 - threshold_64x64, 1 - threshold_32x32, 2 - threshold_16x16,
513 // 3 - vbp_threshold_8x8. vbp_threshold_8x8 (to split to 4x4 partition) is
514 // currently only used on key frame.
515 static void set_vbp_thresholds(VP9_COMP *cpi, int64_t thresholds[], int q,
517 VP9_COMMON *const cm = &cpi->common;
518 const int is_key_frame = (cm->frame_type == KEY_FRAME);
519 const int threshold_multiplier = is_key_frame ? 20 : 1;
520 int64_t threshold_base =
521 (int64_t)(threshold_multiplier * cpi->y_dequant[q][1]);
524 thresholds[0] = threshold_base;
525 thresholds[1] = threshold_base >> 2;
526 thresholds[2] = threshold_base >> 2;
527 thresholds[3] = threshold_base << 2;
529 // Increase base variance threshold based on estimated noise level.
530 if (cpi->noise_estimate.enabled && cm->width >= 640 && cm->height >= 480) {
531 NOISE_LEVEL noise_level =
532 vp9_noise_estimate_extract_level(&cpi->noise_estimate);
533 if (noise_level == kHigh)
534 threshold_base = 3 * threshold_base;
535 else if (noise_level == kMedium)
536 threshold_base = threshold_base << 1;
537 else if (noise_level < kLow)
538 threshold_base = (7 * threshold_base) >> 3;
540 #if CONFIG_VP9_TEMPORAL_DENOISING
541 if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
542 cpi->oxcf.speed > 5 && cpi->denoiser.denoising_level >= kDenLow)
543 threshold_base = vp9_scale_part_thresh(
544 threshold_base, cpi->denoiser.denoising_level, content_state);
547 scale_part_thresh_sumdiff(threshold_base, cpi->oxcf.speed, cm->width,
548 cm->height, content_state);
550 // Increase base variance threshold based on content_state/sum_diff level.
551 threshold_base = scale_part_thresh_sumdiff(
552 threshold_base, cpi->oxcf.speed, cm->width, cm->height, content_state);
554 thresholds[0] = threshold_base;
555 thresholds[2] = threshold_base << cpi->oxcf.speed;
556 if (cm->width <= 352 && cm->height <= 288) {
557 thresholds[0] = threshold_base >> 3;
558 thresholds[1] = threshold_base >> 1;
559 thresholds[2] = threshold_base << 3;
560 } else if (cm->width < 1280 && cm->height < 720) {
561 thresholds[1] = (5 * threshold_base) >> 2;
562 } else if (cm->width < 1920 && cm->height < 1080) {
563 thresholds[1] = threshold_base << 1;
565 thresholds[1] = (5 * threshold_base) >> 1;
570 void vp9_set_variance_partition_thresholds(VP9_COMP *cpi, int q,
572 VP9_COMMON *const cm = &cpi->common;
573 SPEED_FEATURES *const sf = &cpi->sf;
574 const int is_key_frame = (cm->frame_type == KEY_FRAME);
575 if (sf->partition_search_type != VAR_BASED_PARTITION &&
576 sf->partition_search_type != REFERENCE_PARTITION) {
579 set_vbp_thresholds(cpi, cpi->vbp_thresholds, q, content_state);
580 // The thresholds below are not changed locally.
582 cpi->vbp_threshold_sad = 0;
583 cpi->vbp_threshold_copy = 0;
584 cpi->vbp_bsize_min = BLOCK_8X8;
586 if (cm->width <= 352 && cm->height <= 288)
587 cpi->vbp_threshold_sad = 10;
589 cpi->vbp_threshold_sad = (cpi->y_dequant[q][1] << 1) > 1000
590 ? (cpi->y_dequant[q][1] << 1)
592 cpi->vbp_bsize_min = BLOCK_16X16;
593 if (cm->width <= 352 && cm->height <= 288)
594 cpi->vbp_threshold_copy = 4000;
595 else if (cm->width <= 640 && cm->height <= 360)
596 cpi->vbp_threshold_copy = 8000;
598 cpi->vbp_threshold_copy = (cpi->y_dequant[q][1] << 3) > 8000
599 ? (cpi->y_dequant[q][1] << 3)
602 cpi->vbp_threshold_minmax = 15 + (q >> 3);
606 // Compute the minmax over the 8x8 subblocks.
607 static int compute_minmax_8x8(const uint8_t *s, int sp, const uint8_t *d,
608 int dp, int x16_idx, int y16_idx,
609 #if CONFIG_VP9_HIGHBITDEPTH
612 int pixels_wide, int pixels_high) {
615 int minmax_min = 255;
616 // Loop over the 4 8x8 subblocks.
617 for (k = 0; k < 4; k++) {
618 int x8_idx = x16_idx + ((k & 1) << 3);
619 int y8_idx = y16_idx + ((k >> 1) << 3);
622 if (x8_idx < pixels_wide && y8_idx < pixels_high) {
623 #if CONFIG_VP9_HIGHBITDEPTH
624 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
625 vpx_highbd_minmax_8x8(s + y8_idx * sp + x8_idx, sp,
626 d + y8_idx * dp + x8_idx, dp, &min, &max);
628 vpx_minmax_8x8(s + y8_idx * sp + x8_idx, sp, d + y8_idx * dp + x8_idx,
632 vpx_minmax_8x8(s + y8_idx * sp + x8_idx, sp, d + y8_idx * dp + x8_idx, dp,
635 if ((max - min) > minmax_max) minmax_max = (max - min);
636 if ((max - min) < minmax_min) minmax_min = (max - min);
639 return (minmax_max - minmax_min);
642 static void fill_variance_4x4avg(const uint8_t *s, int sp, const uint8_t *d,
643 int dp, int x8_idx, int y8_idx, v8x8 *vst,
644 #if CONFIG_VP9_HIGHBITDEPTH
647 int pixels_wide, int pixels_high,
650 for (k = 0; k < 4; k++) {
651 int x4_idx = x8_idx + ((k & 1) << 2);
652 int y4_idx = y8_idx + ((k >> 1) << 2);
653 unsigned int sse = 0;
655 if (x4_idx < pixels_wide && y4_idx < pixels_high) {
658 #if CONFIG_VP9_HIGHBITDEPTH
659 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
660 s_avg = vpx_highbd_avg_4x4(s + y4_idx * sp + x4_idx, sp);
662 d_avg = vpx_highbd_avg_4x4(d + y4_idx * dp + x4_idx, dp);
664 s_avg = vpx_avg_4x4(s + y4_idx * sp + x4_idx, sp);
665 if (!is_key_frame) d_avg = vpx_avg_4x4(d + y4_idx * dp + x4_idx, dp);
668 s_avg = vpx_avg_4x4(s + y4_idx * sp + x4_idx, sp);
669 if (!is_key_frame) d_avg = vpx_avg_4x4(d + y4_idx * dp + x4_idx, dp);
674 fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
678 static void fill_variance_8x8avg(const uint8_t *s, int sp, const uint8_t *d,
679 int dp, int x16_idx, int y16_idx, v16x16 *vst,
680 #if CONFIG_VP9_HIGHBITDEPTH
683 int pixels_wide, int pixels_high,
686 for (k = 0; k < 4; k++) {
687 int x8_idx = x16_idx + ((k & 1) << 3);
688 int y8_idx = y16_idx + ((k >> 1) << 3);
689 unsigned int sse = 0;
691 if (x8_idx < pixels_wide && y8_idx < pixels_high) {
694 #if CONFIG_VP9_HIGHBITDEPTH
695 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
696 s_avg = vpx_highbd_avg_8x8(s + y8_idx * sp + x8_idx, sp);
698 d_avg = vpx_highbd_avg_8x8(d + y8_idx * dp + x8_idx, dp);
700 s_avg = vpx_avg_8x8(s + y8_idx * sp + x8_idx, sp);
701 if (!is_key_frame) d_avg = vpx_avg_8x8(d + y8_idx * dp + x8_idx, dp);
704 s_avg = vpx_avg_8x8(s + y8_idx * sp + x8_idx, sp);
705 if (!is_key_frame) d_avg = vpx_avg_8x8(d + y8_idx * dp + x8_idx, dp);
710 fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
714 // Check if most of the superblock is skin content, and if so, force split to
715 // 32x32, and set x->sb_is_skin for use in mode selection.
716 static int skin_sb_split(VP9_COMP *cpi, MACROBLOCK *x, const int low_res,
717 int mi_row, int mi_col, int *force_split) {
718 VP9_COMMON *const cm = &cpi->common;
719 #if CONFIG_VP9_HIGHBITDEPTH
720 if (cm->use_highbitdepth) return 0;
722 // Avoid checking superblocks on/near boundary and avoid low resolutions.
723 // Note superblock may still pick 64X64 if y_sad is very small
724 // (i.e., y_sad < cpi->vbp_threshold_sad) below. For now leave this as is.
725 if (!low_res && (mi_col >= 8 && mi_col + 8 < cm->mi_cols && mi_row >= 8 &&
726 mi_row + 8 < cm->mi_rows)) {
727 int num_16x16_skin = 0;
728 int num_16x16_nonskin = 0;
729 uint8_t *ysignal = x->plane[0].src.buf;
730 uint8_t *usignal = x->plane[1].src.buf;
731 uint8_t *vsignal = x->plane[2].src.buf;
732 int sp = x->plane[0].src.stride;
733 int spuv = x->plane[1].src.stride;
734 const int block_index = mi_row * cm->mi_cols + mi_col;
735 const int bw = num_8x8_blocks_wide_lookup[BLOCK_64X64];
736 const int bh = num_8x8_blocks_high_lookup[BLOCK_64X64];
737 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
738 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
739 // Loop through the 16x16 sub-blocks.
741 for (i = 0; i < ymis; i += 2) {
742 for (j = 0; j < xmis; j += 2) {
743 int bl_index = block_index + i * cm->mi_cols + j;
744 int bl_index1 = bl_index + 1;
745 int bl_index2 = bl_index + cm->mi_cols;
746 int bl_index3 = bl_index2 + 1;
748 VPXMIN(cpi->consec_zero_mv[bl_index],
749 VPXMIN(cpi->consec_zero_mv[bl_index1],
750 VPXMIN(cpi->consec_zero_mv[bl_index2],
751 cpi->consec_zero_mv[bl_index3])));
752 int is_skin = vp9_compute_skin_block(
753 ysignal, usignal, vsignal, sp, spuv, BLOCK_16X16, consec_zeromv, 0);
754 num_16x16_skin += is_skin;
755 num_16x16_nonskin += (1 - is_skin);
756 if (num_16x16_nonskin > 3) {
757 // Exit loop if at least 4 of the 16x16 blocks are not skin.
765 ysignal += (sp << 4) - 64;
766 usignal += (spuv << 3) - 32;
767 vsignal += (spuv << 3) - 32;
769 if (num_16x16_skin > 12) {
777 static void set_low_temp_var_flag(VP9_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
778 v64x64 *vt, int64_t thresholds[],
779 MV_REFERENCE_FRAME ref_frame_partition,
780 int mi_col, int mi_row) {
782 VP9_COMMON *const cm = &cpi->common;
783 const int mv_thr = cm->width > 640 ? 8 : 4;
784 // Check temporal variance for bsize >= 16x16, if LAST_FRAME was selected and
785 // int_pro mv is small. If the temporal variance is small set the flag
786 // variance_low for the block. The variance threshold can be adjusted, the
787 // higher the more aggressive.
788 if (ref_frame_partition == LAST_FRAME &&
789 (cpi->sf.short_circuit_low_temp_var == 1 ||
790 (xd->mi[0]->mv[0].as_mv.col < mv_thr &&
791 xd->mi[0]->mv[0].as_mv.col > -mv_thr &&
792 xd->mi[0]->mv[0].as_mv.row < mv_thr &&
793 xd->mi[0]->mv[0].as_mv.row > -mv_thr))) {
794 if (xd->mi[0]->sb_type == BLOCK_64X64) {
795 if ((vt->part_variances).none.variance < (thresholds[0] >> 1))
796 x->variance_low[0] = 1;
797 } else if (xd->mi[0]->sb_type == BLOCK_64X32) {
798 for (i = 0; i < 2; i++) {
799 if (vt->part_variances.horz[i].variance < (thresholds[0] >> 2))
800 x->variance_low[i + 1] = 1;
802 } else if (xd->mi[0]->sb_type == BLOCK_32X64) {
803 for (i = 0; i < 2; i++) {
804 if (vt->part_variances.vert[i].variance < (thresholds[0] >> 2))
805 x->variance_low[i + 3] = 1;
808 for (i = 0; i < 4; i++) {
809 const int idx[4][2] = { { 0, 0 }, { 0, 4 }, { 4, 0 }, { 4, 4 } };
811 cm->mi_stride * (mi_row + idx[i][0]) + mi_col + idx[i][1];
812 MODE_INFO **this_mi = cm->mi_grid_visible + idx_str;
814 if (cm->mi_cols <= mi_col + idx[i][1] ||
815 cm->mi_rows <= mi_row + idx[i][0])
818 if ((*this_mi)->sb_type == BLOCK_32X32) {
819 int64_t threshold_32x32 = (cpi->sf.short_circuit_low_temp_var == 1 ||
820 cpi->sf.short_circuit_low_temp_var == 3)
821 ? ((5 * thresholds[1]) >> 3)
822 : (thresholds[1] >> 1);
823 if (vt->split[i].part_variances.none.variance < threshold_32x32)
824 x->variance_low[i + 5] = 1;
825 } else if (cpi->sf.short_circuit_low_temp_var >= 2) {
826 // For 32x16 and 16x32 blocks, the flag is set on each 16x16 block
828 if ((*this_mi)->sb_type == BLOCK_16X16 ||
829 (*this_mi)->sb_type == BLOCK_32X16 ||
830 (*this_mi)->sb_type == BLOCK_16X32) {
831 for (j = 0; j < 4; j++) {
832 if (vt->split[i].split[j].part_variances.none.variance <
833 (thresholds[2] >> 8))
834 x->variance_low[(i << 2) + j + 9] = 1;
843 static void copy_partitioning_helper(VP9_COMP *cpi, BLOCK_SIZE bsize,
844 int mi_row, int mi_col) {
845 VP9_COMMON *const cm = &cpi->common;
846 BLOCK_SIZE *prev_part = cpi->prev_partition;
847 int start_pos = mi_row * cm->mi_stride + mi_col;
849 const int bsl = b_width_log2_lookup[bsize];
850 const int bs = (1 << bsl) / 4;
852 PARTITION_TYPE partition;
853 MODE_INFO *mi = NULL;
855 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
857 partition = partition_lookup[bsl][prev_part[start_pos]];
858 subsize = get_subsize(bsize, partition);
859 mi = cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col];
861 if (subsize < BLOCK_8X8) {
865 case PARTITION_NONE: mi->sb_type = bsize; break;
867 mi->sb_type = subsize;
868 if (mi_row + bs < cm->mi_rows)
869 cm->mi_grid_visible[(mi_row + bs) * cm->mi_stride + mi_col]->sb_type =
873 mi->sb_type = subsize;
874 if (mi_col + bs < cm->mi_cols)
875 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col + bs]->sb_type =
878 case PARTITION_SPLIT:
879 copy_partitioning_helper(cpi, subsize, mi_row, mi_col);
880 copy_partitioning_helper(cpi, subsize, mi_row + bs, mi_col);
881 copy_partitioning_helper(cpi, subsize, mi_row, mi_col + bs);
882 copy_partitioning_helper(cpi, subsize, mi_row + bs, mi_col + bs);
889 static int copy_partitioning(VP9_COMP *cpi, MACROBLOCK *x, int mi_row,
890 int mi_col, int segment_id, int sb_offset) {
891 if (cpi->rc.frames_since_key > 1 && segment_id == CR_SEGMENT_ID_BASE &&
892 cpi->prev_segment_id[sb_offset] == CR_SEGMENT_ID_BASE &&
893 cpi->copied_frame_cnt[sb_offset] < cpi->max_copied_frame) {
894 if (cpi->prev_partition != NULL) {
895 copy_partitioning_helper(cpi, BLOCK_64X64, mi_row, mi_col);
896 cpi->copied_frame_cnt[sb_offset] += 1;
897 memcpy(x->variance_low, &(cpi->prev_variance_low[sb_offset * 25]),
898 sizeof(x->variance_low));
906 static void update_prev_partition(VP9_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
908 VP9_COMMON *const cm = &cpi->common;
909 BLOCK_SIZE *prev_part = cpi->prev_partition;
910 int start_pos = mi_row * cm->mi_stride + mi_col;
911 const int bsl = b_width_log2_lookup[bsize];
912 const int bs = (1 << bsl) / 4;
914 PARTITION_TYPE partition;
915 const MODE_INFO *mi = NULL;
917 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
919 mi = cm->mi_grid_visible[start_pos];
920 partition = partition_lookup[bsl][mi->sb_type];
921 subsize = get_subsize(bsize, partition);
922 if (subsize < BLOCK_8X8) {
923 prev_part[start_pos] = bsize;
926 case PARTITION_NONE: prev_part[start_pos] = bsize; break;
928 prev_part[start_pos] = subsize;
929 if (mi_row + bs < cm->mi_rows)
930 prev_part[start_pos + bs * cm->mi_stride] = subsize;
933 prev_part[start_pos] = subsize;
934 if (mi_col + bs < cm->mi_cols) prev_part[start_pos + bs] = subsize;
936 case PARTITION_SPLIT:
937 update_prev_partition(cpi, subsize, mi_row, mi_col);
938 update_prev_partition(cpi, subsize, mi_row + bs, mi_col);
939 update_prev_partition(cpi, subsize, mi_row, mi_col + bs);
940 update_prev_partition(cpi, subsize, mi_row + bs, mi_col + bs);
947 static void chroma_check(VP9_COMP *cpi, MACROBLOCK *x, int bsize,
948 unsigned int y_sad, int is_key_frame) {
950 MACROBLOCKD *xd = &x->e_mbd;
952 if (is_key_frame) return;
954 // For speed >= 8, avoid the chroma check if y_sad is above threshold.
955 if (cpi->oxcf.speed >= 8) {
956 if (y_sad > cpi->vbp_thresholds[1] &&
957 (!cpi->noise_estimate.enabled ||
958 vp9_noise_estimate_extract_level(&cpi->noise_estimate) < kMedium))
962 for (i = 1; i <= 2; ++i) {
963 unsigned int uv_sad = UINT_MAX;
964 struct macroblock_plane *p = &x->plane[i];
965 struct macroblockd_plane *pd = &xd->plane[i];
966 const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
968 if (bs != BLOCK_INVALID)
969 uv_sad = cpi->fn_ptr[bs].sdf(p->src.buf, p->src.stride, pd->dst.buf,
972 // TODO(marpan): Investigate if we should lower this threshold if
973 // superblock is detected as skin.
974 x->color_sensitivity[i - 1] = uv_sad > (y_sad >> 2);
978 static void avg_source_sad(VP9_COMP *cpi, MACROBLOCK *x, int shift,
980 unsigned int tmp_sse;
982 unsigned int tmp_variance;
983 const BLOCK_SIZE bsize = BLOCK_64X64;
984 uint8_t *src_y = cpi->Source->y_buffer;
985 int src_ystride = cpi->Source->y_stride;
986 uint8_t *last_src_y = cpi->Last_Source->y_buffer;
987 int last_src_ystride = cpi->Last_Source->y_stride;
988 uint64_t avg_source_sad_threshold = 10000;
989 uint64_t avg_source_sad_threshold2 = 12000;
990 #if CONFIG_VP9_HIGHBITDEPTH
991 if (cpi->common.use_highbitdepth) return;
996 cpi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y, last_src_ystride);
997 tmp_variance = vpx_variance64x64(src_y, src_ystride, last_src_y,
998 last_src_ystride, &tmp_sse);
999 // Note: tmp_sse - tmp_variance = ((sum * sum) >> 12)
1000 if (tmp_sad < avg_source_sad_threshold)
1001 x->content_state_sb = ((tmp_sse - tmp_variance) < 25) ? kLowSadLowSumdiff
1002 : kLowSadHighSumdiff;
1004 x->content_state_sb = ((tmp_sse - tmp_variance) < 25) ? kHighSadLowSumdiff
1005 : kHighSadHighSumdiff;
1007 // Detect large lighting change.
1008 if (tmp_variance < (tmp_sse >> 3) && (tmp_sse - tmp_variance) > 10000)
1009 x->content_state_sb = kLowVarHighSumdiff;
1011 if (cpi->content_state_sb_fd != NULL) {
1012 if (tmp_sad < avg_source_sad_threshold2) {
1013 // Cap the increment to 255.
1014 if (cpi->content_state_sb_fd[sb_offset] < 255)
1015 cpi->content_state_sb_fd[sb_offset]++;
1017 cpi->content_state_sb_fd[sb_offset] = 0;
1023 // This function chooses partitioning based on the variance between source and
1024 // reconstructed last, where variance is computed for down-sampled inputs.
1025 static int choose_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
1026 MACROBLOCK *x, int mi_row, int mi_col) {
1027 VP9_COMMON *const cm = &cpi->common;
1028 MACROBLOCKD *xd = &x->e_mbd;
1032 int force_split[21];
1034 int max_var_32x32 = 0;
1035 int min_var_32x32 = INT_MAX;
1038 int maxvar_16x16[4];
1039 int minvar_16x16[4];
1040 int64_t threshold_4x4avg;
1041 NOISE_LEVEL noise_level = kLow;
1042 int content_state = 0;
1047 unsigned int y_sad = UINT_MAX;
1048 BLOCK_SIZE bsize = BLOCK_64X64;
1049 // Ref frame used in partitioning.
1050 MV_REFERENCE_FRAME ref_frame_partition = LAST_FRAME;
1051 int pixels_wide = 64, pixels_high = 64;
1052 int64_t thresholds[4] = { cpi->vbp_thresholds[0], cpi->vbp_thresholds[1],
1053 cpi->vbp_thresholds[2], cpi->vbp_thresholds[3] };
1055 // For the variance computation under SVC mode, we treat the frame as key if
1056 // the reference (base layer frame) is key frame (i.e., is_key_frame == 1).
1057 const int is_key_frame =
1058 (cm->frame_type == KEY_FRAME ||
1059 (is_one_pass_cbr_svc(cpi) &&
1060 cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame));
1061 // Always use 4x4 partition for key frame.
1062 const int use_4x4_partition = cm->frame_type == KEY_FRAME;
1063 const int low_res = (cm->width <= 352 && cm->height <= 288);
1064 int variance4x4downsample[16];
1066 int sb_offset = (cm->mi_stride >> 3) * (mi_row >> 3) + (mi_col >> 3);
1068 set_offsets(cpi, tile, x, mi_row, mi_col, BLOCK_64X64);
1069 segment_id = xd->mi[0]->segment_id;
1071 if (cpi->sf.use_source_sad && !is_key_frame) {
1072 int sb_offset2 = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
1073 content_state = x->content_state_sb;
1074 x->skip_low_source_sad = (content_state == kLowSadLowSumdiff ||
1075 content_state == kLowSadHighSumdiff)
1078 x->lowvar_highsumdiff = (content_state == kLowVarHighSumdiff) ? 1 : 0;
1079 if (cpi->content_state_sb_fd != NULL)
1080 x->last_sb_high_content = cpi->content_state_sb_fd[sb_offset2];
1081 // If source_sad is low copy the partition without computing the y_sad.
1082 if (x->skip_low_source_sad && cpi->sf.copy_partition_flag &&
1083 copy_partitioning(cpi, x, mi_row, mi_col, segment_id, sb_offset)) {
1088 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
1089 cyclic_refresh_segment_id_boosted(segment_id)) {
1090 int q = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
1091 set_vbp_thresholds(cpi, thresholds, q, content_state);
1093 set_vbp_thresholds(cpi, thresholds, cm->base_qindex, content_state);
1096 // For non keyframes, disable 4x4 average for low resolution when speed = 8
1097 threshold_4x4avg = (cpi->oxcf.speed < 8) ? thresholds[1] << 1 : INT64_MAX;
1099 memset(x->variance_low, 0, sizeof(x->variance_low));
1101 if (xd->mb_to_right_edge < 0) pixels_wide += (xd->mb_to_right_edge >> 3);
1102 if (xd->mb_to_bottom_edge < 0) pixels_high += (xd->mb_to_bottom_edge >> 3);
1104 s = x->plane[0].src.buf;
1105 sp = x->plane[0].src.stride;
1107 // Index for force_split: 0 for 64x64, 1-4 for 32x32 blocks,
1108 // 5-20 for the 16x16 blocks.
1111 if (!is_key_frame) {
1112 // In the case of spatial/temporal scalable coding, the assumption here is
1113 // that the temporal reference frame will always be of type LAST_FRAME.
1114 // TODO(marpan): If that assumption is broken, we need to revisit this code.
1115 MODE_INFO *mi = xd->mi[0];
1116 YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
1118 const YV12_BUFFER_CONFIG *yv12_g = NULL;
1119 unsigned int y_sad_g, y_sad_thr, y_sad_last;
1120 bsize = BLOCK_32X32 + (mi_col + 4 < cm->mi_cols) * 2 +
1121 (mi_row + 4 < cm->mi_rows);
1123 assert(yv12 != NULL);
1125 if (!(is_one_pass_cbr_svc(cpi) && cpi->svc.spatial_layer_id)) {
1126 // For now, GOLDEN will not be used for non-zero spatial layers, since
1127 // it may not be a temporal reference.
1128 yv12_g = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
1131 // Only compute y_sad_g (sad for golden reference) for speed < 8.
1132 if (cpi->oxcf.speed < 8 && yv12_g && yv12_g != yv12 &&
1133 (cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
1134 vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
1135 &cm->frame_refs[GOLDEN_FRAME - 1].sf);
1136 y_sad_g = cpi->fn_ptr[bsize].sdf(
1137 x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].pre[0].buf,
1138 xd->plane[0].pre[0].stride);
1143 if (cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR &&
1144 cpi->rc.is_src_frame_alt_ref) {
1145 yv12 = get_ref_frame_buffer(cpi, ALTREF_FRAME);
1146 vp9_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
1147 &cm->frame_refs[ALTREF_FRAME - 1].sf);
1148 mi->ref_frame[0] = ALTREF_FRAME;
1151 vp9_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
1152 &cm->frame_refs[LAST_FRAME - 1].sf);
1153 mi->ref_frame[0] = LAST_FRAME;
1155 mi->ref_frame[1] = NONE;
1156 mi->sb_type = BLOCK_64X64;
1157 mi->mv[0].as_int = 0;
1158 mi->interp_filter = BILINEAR;
1160 if (cpi->oxcf.speed >= 8 && !low_res)
1161 y_sad = cpi->fn_ptr[bsize].sdf(
1162 x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].pre[0].buf,
1163 xd->plane[0].pre[0].stride);
1165 y_sad = vp9_int_pro_motion_estimation(cpi, x, bsize, mi_row, mi_col);
1168 // Pick ref frame for partitioning, bias last frame when y_sad_g and y_sad
1169 // are close if short_circuit_low_temp_var is on.
1170 y_sad_thr = cpi->sf.short_circuit_low_temp_var ? (y_sad * 7) >> 3 : y_sad;
1171 if (y_sad_g < y_sad_thr) {
1172 vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
1173 &cm->frame_refs[GOLDEN_FRAME - 1].sf);
1174 mi->ref_frame[0] = GOLDEN_FRAME;
1175 mi->mv[0].as_int = 0;
1177 ref_frame_partition = GOLDEN_FRAME;
1179 x->pred_mv[LAST_FRAME] = mi->mv[0].as_mv;
1180 ref_frame_partition = LAST_FRAME;
1183 set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
1184 vp9_build_inter_predictors_sb(xd, mi_row, mi_col, BLOCK_64X64);
1186 x->sb_is_skin = skin_sb_split(cpi, x, low_res, mi_row, mi_col, force_split);
1188 d = xd->plane[0].dst.buf;
1189 dp = xd->plane[0].dst.stride;
1191 // If the y_sad is very small, take 64x64 as partition and exit.
1192 // Don't check on boosted segment for now, as 64x64 is suppressed there.
1193 if (segment_id == CR_SEGMENT_ID_BASE && y_sad < cpi->vbp_threshold_sad) {
1194 const int block_width = num_8x8_blocks_wide_lookup[BLOCK_64X64];
1195 const int block_height = num_8x8_blocks_high_lookup[BLOCK_64X64];
1196 if (mi_col + block_width / 2 < cm->mi_cols &&
1197 mi_row + block_height / 2 < cm->mi_rows) {
1198 set_block_size(cpi, x, xd, mi_row, mi_col, BLOCK_64X64);
1199 x->variance_low[0] = 1;
1200 chroma_check(cpi, x, bsize, y_sad, is_key_frame);
1205 // If the y_sad is small enough, copy the partition of the superblock in the
1206 // last frame to current frame only if the last frame is not a keyframe.
1207 // Stop the copy every cpi->max_copied_frame to refresh the partition.
1208 // TODO(jianj) : tune the threshold.
1209 if (cpi->sf.copy_partition_flag && y_sad_last < cpi->vbp_threshold_copy &&
1210 copy_partitioning(cpi, x, mi_row, mi_col, segment_id, sb_offset)) {
1211 chroma_check(cpi, x, bsize, y_sad, is_key_frame);
1217 #if CONFIG_VP9_HIGHBITDEPTH
1218 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1220 case 10: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10); break;
1221 case 12: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12); break;
1223 default: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8); break;
1226 #endif // CONFIG_VP9_HIGHBITDEPTH
1229 // Fill in the entire tree of 8x8 (or 4x4 under some conditions) variances
1231 for (i = 0; i < 4; i++) {
1232 const int x32_idx = ((i & 1) << 5);
1233 const int y32_idx = ((i >> 1) << 5);
1234 const int i2 = i << 2;
1235 force_split[i + 1] = 0;
1237 maxvar_16x16[i] = 0;
1238 minvar_16x16[i] = INT_MAX;
1239 for (j = 0; j < 4; j++) {
1240 const int x16_idx = x32_idx + ((j & 1) << 4);
1241 const int y16_idx = y32_idx + ((j >> 1) << 4);
1242 const int split_index = 5 + i2 + j;
1243 v16x16 *vst = &vt.split[i].split[j];
1244 force_split[split_index] = 0;
1245 variance4x4downsample[i2 + j] = 0;
1246 if (!is_key_frame) {
1247 fill_variance_8x8avg(s, sp, d, dp, x16_idx, y16_idx, vst,
1248 #if CONFIG_VP9_HIGHBITDEPTH
1251 pixels_wide, pixels_high, is_key_frame);
1252 fill_variance_tree(&vt.split[i].split[j], BLOCK_16X16);
1253 get_variance(&vt.split[i].split[j].part_variances.none);
1254 avg_16x16[i] += vt.split[i].split[j].part_variances.none.variance;
1255 if (vt.split[i].split[j].part_variances.none.variance < minvar_16x16[i])
1256 minvar_16x16[i] = vt.split[i].split[j].part_variances.none.variance;
1257 if (vt.split[i].split[j].part_variances.none.variance > maxvar_16x16[i])
1258 maxvar_16x16[i] = vt.split[i].split[j].part_variances.none.variance;
1259 if (vt.split[i].split[j].part_variances.none.variance > thresholds[2]) {
1260 // 16X16 variance is above threshold for split, so force split to 8x8
1261 // for this 16x16 block (this also forces splits for upper levels).
1262 force_split[split_index] = 1;
1263 force_split[i + 1] = 1;
1265 } else if (cpi->oxcf.speed < 8 &&
1266 vt.split[i].split[j].part_variances.none.variance >
1268 !cyclic_refresh_segment_id_boosted(segment_id)) {
1269 // We have some nominal amount of 16x16 variance (based on average),
1270 // compute the minmax over the 8x8 sub-blocks, and if above threshold,
1271 // force split to 8x8 block for this 16x16 block.
1272 int minmax = compute_minmax_8x8(s, sp, d, dp, x16_idx, y16_idx,
1273 #if CONFIG_VP9_HIGHBITDEPTH
1276 pixels_wide, pixels_high);
1277 if (minmax > cpi->vbp_threshold_minmax) {
1278 force_split[split_index] = 1;
1279 force_split[i + 1] = 1;
1284 if (is_key_frame || (low_res &&
1285 vt.split[i].split[j].part_variances.none.variance >
1286 threshold_4x4avg)) {
1287 force_split[split_index] = 0;
1288 // Go down to 4x4 down-sampling for variance.
1289 variance4x4downsample[i2 + j] = 1;
1290 for (k = 0; k < 4; k++) {
1291 int x8_idx = x16_idx + ((k & 1) << 3);
1292 int y8_idx = y16_idx + ((k >> 1) << 3);
1293 v8x8 *vst2 = is_key_frame ? &vst->split[k] : &vt2[i2 + j].split[k];
1294 fill_variance_4x4avg(s, sp, d, dp, x8_idx, y8_idx, vst2,
1295 #if CONFIG_VP9_HIGHBITDEPTH
1298 pixels_wide, pixels_high, is_key_frame);
1303 if (cpi->noise_estimate.enabled)
1304 noise_level = vp9_noise_estimate_extract_level(&cpi->noise_estimate);
1305 // Fill the rest of the variance tree by summing split partition values.
1307 for (i = 0; i < 4; i++) {
1308 const int i2 = i << 2;
1309 for (j = 0; j < 4; j++) {
1310 if (variance4x4downsample[i2 + j] == 1) {
1311 v16x16 *vtemp = (!is_key_frame) ? &vt2[i2 + j] : &vt.split[i].split[j];
1312 for (m = 0; m < 4; m++) fill_variance_tree(&vtemp->split[m], BLOCK_8X8);
1313 fill_variance_tree(vtemp, BLOCK_16X16);
1314 // If variance of this 16x16 block is above the threshold, force block
1315 // to split. This also forces a split on the upper levels.
1316 get_variance(&vtemp->part_variances.none);
1317 if (vtemp->part_variances.none.variance > thresholds[2]) {
1318 force_split[5 + i2 + j] = 1;
1319 force_split[i + 1] = 1;
1324 fill_variance_tree(&vt.split[i], BLOCK_32X32);
1325 // If variance of this 32x32 block is above the threshold, or if its above
1326 // (some threshold of) the average variance over the sub-16x16 blocks, then
1327 // force this block to split. This also forces a split on the upper
1329 if (!force_split[i + 1]) {
1330 get_variance(&vt.split[i].part_variances.none);
1331 var_32x32 = vt.split[i].part_variances.none.variance;
1332 max_var_32x32 = VPXMAX(var_32x32, max_var_32x32);
1333 min_var_32x32 = VPXMIN(var_32x32, min_var_32x32);
1334 if (vt.split[i].part_variances.none.variance > thresholds[1] ||
1336 vt.split[i].part_variances.none.variance > (thresholds[1] >> 1) &&
1337 vt.split[i].part_variances.none.variance > (avg_16x16[i] >> 1))) {
1338 force_split[i + 1] = 1;
1340 } else if (!is_key_frame && noise_level < kLow && cm->height <= 360 &&
1341 (maxvar_16x16[i] - minvar_16x16[i]) > (thresholds[1] >> 1) &&
1342 maxvar_16x16[i] > thresholds[1]) {
1343 force_split[i + 1] = 1;
1346 avg_32x32 += var_32x32;
1349 if (!force_split[0]) {
1350 fill_variance_tree(&vt, BLOCK_64X64);
1351 get_variance(&vt.part_variances.none);
1352 // If variance of this 64x64 block is above (some threshold of) the average
1353 // variance over the sub-32x32 blocks, then force this block to split.
1354 // Only checking this for noise level >= medium for now.
1355 if (!is_key_frame && noise_level >= kMedium &&
1356 vt.part_variances.none.variance > (9 * avg_32x32) >> 5)
1358 // Else if the maximum 32x32 variance minus the miniumum 32x32 variance in
1359 // a 64x64 block is greater than threshold and the maximum 32x32 variance is
1360 // above a miniumum threshold, then force the split of a 64x64 block
1361 // Only check this for low noise.
1362 else if (!is_key_frame && noise_level < kMedium &&
1363 (max_var_32x32 - min_var_32x32) > 3 * (thresholds[0] >> 3) &&
1364 max_var_32x32 > thresholds[0] >> 1)
1368 // Now go through the entire structure, splitting every block size until
1369 // we get to one that's got a variance lower than our threshold.
1370 if (mi_col + 8 > cm->mi_cols || mi_row + 8 > cm->mi_rows ||
1371 !set_vt_partitioning(cpi, x, xd, &vt, BLOCK_64X64, mi_row, mi_col,
1372 thresholds[0], BLOCK_16X16, force_split[0])) {
1373 for (i = 0; i < 4; ++i) {
1374 const int x32_idx = ((i & 1) << 2);
1375 const int y32_idx = ((i >> 1) << 2);
1376 const int i2 = i << 2;
1377 if (!set_vt_partitioning(cpi, x, xd, &vt.split[i], BLOCK_32X32,
1378 (mi_row + y32_idx), (mi_col + x32_idx),
1379 thresholds[1], BLOCK_16X16,
1380 force_split[i + 1])) {
1381 for (j = 0; j < 4; ++j) {
1382 const int x16_idx = ((j & 1) << 1);
1383 const int y16_idx = ((j >> 1) << 1);
1384 // For inter frames: if variance4x4downsample[] == 1 for this 16x16
1385 // block, then the variance is based on 4x4 down-sampling, so use vt2
1386 // in set_vt_partioning(), otherwise use vt.
1387 v16x16 *vtemp = (!is_key_frame && variance4x4downsample[i2 + j] == 1)
1389 : &vt.split[i].split[j];
1390 if (!set_vt_partitioning(
1391 cpi, x, xd, vtemp, BLOCK_16X16, mi_row + y32_idx + y16_idx,
1392 mi_col + x32_idx + x16_idx, thresholds[2], cpi->vbp_bsize_min,
1393 force_split[5 + i2 + j])) {
1394 for (k = 0; k < 4; ++k) {
1395 const int x8_idx = (k & 1);
1396 const int y8_idx = (k >> 1);
1397 if (use_4x4_partition) {
1398 if (!set_vt_partitioning(cpi, x, xd, &vtemp->split[k],
1400 mi_row + y32_idx + y16_idx + y8_idx,
1401 mi_col + x32_idx + x16_idx + x8_idx,
1402 thresholds[3], BLOCK_8X8, 0)) {
1404 cpi, x, xd, (mi_row + y32_idx + y16_idx + y8_idx),
1405 (mi_col + x32_idx + x16_idx + x8_idx), BLOCK_4X4);
1409 cpi, x, xd, (mi_row + y32_idx + y16_idx + y8_idx),
1410 (mi_col + x32_idx + x16_idx + x8_idx), BLOCK_8X8);
1419 if (cm->frame_type != KEY_FRAME && cpi->sf.copy_partition_flag) {
1420 update_prev_partition(cpi, BLOCK_64X64, mi_row, mi_col);
1421 cpi->prev_segment_id[sb_offset] = segment_id;
1422 memcpy(&(cpi->prev_variance_low[sb_offset * 25]), x->variance_low,
1423 sizeof(x->variance_low));
1424 // Reset the counter for copy partitioning
1425 if (cpi->copied_frame_cnt[sb_offset] == cpi->max_copied_frame)
1426 cpi->copied_frame_cnt[sb_offset] = 0;
1429 if (cpi->sf.short_circuit_low_temp_var) {
1430 set_low_temp_var_flag(cpi, x, xd, &vt, thresholds, ref_frame_partition,
1434 chroma_check(cpi, x, bsize, y_sad, is_key_frame);
1438 static void update_state(VP9_COMP *cpi, ThreadData *td, PICK_MODE_CONTEXT *ctx,
1439 int mi_row, int mi_col, BLOCK_SIZE bsize,
1440 int output_enabled) {
1442 VP9_COMMON *const cm = &cpi->common;
1443 RD_COUNTS *const rdc = &td->rd_counts;
1444 MACROBLOCK *const x = &td->mb;
1445 MACROBLOCKD *const xd = &x->e_mbd;
1446 struct macroblock_plane *const p = x->plane;
1447 struct macroblockd_plane *const pd = xd->plane;
1448 MODE_INFO *mi = &ctx->mic;
1449 MODE_INFO *const xdmi = xd->mi[0];
1450 MODE_INFO *mi_addr = xd->mi[0];
1451 const struct segmentation *const seg = &cm->seg;
1452 const int bw = num_8x8_blocks_wide_lookup[mi->sb_type];
1453 const int bh = num_8x8_blocks_high_lookup[mi->sb_type];
1454 const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
1455 const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
1456 MV_REF *const frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
1459 const int mis = cm->mi_stride;
1460 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
1461 const int mi_height = num_8x8_blocks_high_lookup[bsize];
1464 assert(mi->sb_type == bsize);
1467 *x->mbmi_ext = ctx->mbmi_ext;
1469 // If segmentation in use
1471 // For in frame complexity AQ copy the segment id from the segment map.
1472 if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1473 const uint8_t *const map =
1474 seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1475 mi_addr->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1477 // Else for cyclic refresh mode update the segment map, set the segment id
1478 // and then update the quantizer.
1479 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
1480 vp9_cyclic_refresh_update_segment(cpi, xd->mi[0], mi_row, mi_col, bsize,
1481 ctx->rate, ctx->dist, x->skip, p);
1485 max_plane = is_inter_block(xdmi) ? MAX_MB_PLANE : 1;
1486 for (i = 0; i < max_plane; ++i) {
1487 p[i].coeff = ctx->coeff_pbuf[i][1];
1488 p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
1489 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
1490 p[i].eobs = ctx->eobs_pbuf[i][1];
1493 for (i = max_plane; i < MAX_MB_PLANE; ++i) {
1494 p[i].coeff = ctx->coeff_pbuf[i][2];
1495 p[i].qcoeff = ctx->qcoeff_pbuf[i][2];
1496 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2];
1497 p[i].eobs = ctx->eobs_pbuf[i][2];
1500 // Restore the coding context of the MB to that that was in place
1501 // when the mode was picked for it
1502 for (y = 0; y < mi_height; y++)
1503 for (x_idx = 0; x_idx < mi_width; x_idx++)
1504 if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx &&
1505 (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
1506 xd->mi[x_idx + y * mis] = mi_addr;
1509 if (cpi->oxcf.aq_mode != NO_AQ) vp9_init_plane_quantizers(cpi, x);
1511 if (is_inter_block(xdmi) && xdmi->sb_type < BLOCK_8X8) {
1512 xdmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
1513 xdmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
1516 x->skip = ctx->skip;
1517 memcpy(x->zcoeff_blk[xdmi->tx_size], ctx->zcoeff_blk,
1518 sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk);
1520 if (!output_enabled) return;
1522 #if CONFIG_INTERNAL_STATS
1523 if (frame_is_intra_only(cm)) {
1524 static const int kf_mode_index[] = {
1525 THR_DC /*DC_PRED*/, THR_V_PRED /*V_PRED*/,
1526 THR_H_PRED /*H_PRED*/, THR_D45_PRED /*D45_PRED*/,
1527 THR_D135_PRED /*D135_PRED*/, THR_D117_PRED /*D117_PRED*/,
1528 THR_D153_PRED /*D153_PRED*/, THR_D207_PRED /*D207_PRED*/,
1529 THR_D63_PRED /*D63_PRED*/, THR_TM /*TM_PRED*/,
1531 ++cpi->mode_chosen_counts[kf_mode_index[xdmi->mode]];
1533 // Note how often each mode chosen as best
1534 ++cpi->mode_chosen_counts[ctx->best_mode_index];
1537 if (!frame_is_intra_only(cm)) {
1538 if (is_inter_block(xdmi)) {
1539 vp9_update_mv_count(td);
1541 if (cm->interp_filter == SWITCHABLE) {
1542 const int ctx = get_pred_context_switchable_interp(xd);
1543 ++td->counts->switchable_interp[ctx][xdmi->interp_filter];
1547 rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
1548 rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
1549 rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
1551 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
1552 rdc->filter_diff[i] += ctx->best_filter_diff[i];
1555 for (h = 0; h < y_mis; ++h) {
1556 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
1557 for (w = 0; w < x_mis; ++w) {
1558 MV_REF *const mv = frame_mv + w;
1559 mv->ref_frame[0] = mi->ref_frame[0];
1560 mv->ref_frame[1] = mi->ref_frame[1];
1561 mv->mv[0].as_int = mi->mv[0].as_int;
1562 mv->mv[1].as_int = mi->mv[1].as_int;
1567 void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
1568 int mi_row, int mi_col) {
1569 uint8_t *const buffers[3] = { src->y_buffer, src->u_buffer, src->v_buffer };
1570 const int strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
1573 // Set current frame pointer.
1574 x->e_mbd.cur_buf = src;
1576 for (i = 0; i < MAX_MB_PLANE; i++)
1577 setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
1578 NULL, x->e_mbd.plane[i].subsampling_x,
1579 x->e_mbd.plane[i].subsampling_y);
1582 static void set_mode_info_seg_skip(MACROBLOCK *x, TX_MODE tx_mode,
1583 RD_COST *rd_cost, BLOCK_SIZE bsize) {
1584 MACROBLOCKD *const xd = &x->e_mbd;
1585 MODE_INFO *const mi = xd->mi[0];
1586 INTERP_FILTER filter_ref;
1588 filter_ref = get_pred_context_switchable_interp(xd);
1589 if (filter_ref == SWITCHABLE_FILTERS) filter_ref = EIGHTTAP;
1591 mi->sb_type = bsize;
1594 VPXMIN(max_txsize_lookup[bsize], tx_mode_to_biggest_tx_size[tx_mode]);
1596 mi->uv_mode = DC_PRED;
1597 mi->ref_frame[0] = LAST_FRAME;
1598 mi->ref_frame[1] = NONE;
1599 mi->mv[0].as_int = 0;
1600 mi->interp_filter = filter_ref;
1602 xd->mi[0]->bmi[0].as_mv[0].as_int = 0;
1605 vp9_rd_cost_init(rd_cost);
1608 static int set_segment_rdmult(VP9_COMP *const cpi, MACROBLOCK *const x,
1609 int8_t segment_id) {
1611 VP9_COMMON *const cm = &cpi->common;
1612 vp9_init_plane_quantizers(cpi, x);
1613 vpx_clear_system_state();
1614 segment_qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
1615 return vp9_compute_rd_mult(cpi, segment_qindex + cm->y_dc_delta_q);
1618 static void rd_pick_sb_modes(VP9_COMP *cpi, TileDataEnc *tile_data,
1619 MACROBLOCK *const x, int mi_row, int mi_col,
1620 RD_COST *rd_cost, BLOCK_SIZE bsize,
1621 PICK_MODE_CONTEXT *ctx, int64_t best_rd) {
1622 VP9_COMMON *const cm = &cpi->common;
1623 TileInfo *const tile_info = &tile_data->tile_info;
1624 MACROBLOCKD *const xd = &x->e_mbd;
1626 struct macroblock_plane *const p = x->plane;
1627 struct macroblockd_plane *const pd = xd->plane;
1628 const AQ_MODE aq_mode = cpi->oxcf.aq_mode;
1631 vpx_clear_system_state();
1633 // Use the lower precision, but faster, 32x32 fdct for mode selection.
1634 x->use_lp32x32fdct = 1;
1636 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
1638 mi->sb_type = bsize;
1640 for (i = 0; i < MAX_MB_PLANE; ++i) {
1641 p[i].coeff = ctx->coeff_pbuf[i][0];
1642 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
1643 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
1644 p[i].eobs = ctx->eobs_pbuf[i][0];
1648 ctx->pred_pixel_ready = 0;
1651 // Set to zero to make sure we do not use the previous encoded frame stats
1654 #if CONFIG_VP9_HIGHBITDEPTH
1655 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1656 x->source_variance = vp9_high_get_sby_perpixel_variance(
1657 cpi, &x->plane[0].src, bsize, xd->bd);
1659 x->source_variance =
1660 vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1663 x->source_variance =
1664 vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1665 #endif // CONFIG_VP9_HIGHBITDEPTH
1667 // Save rdmult before it might be changed, so it can be restored later.
1668 orig_rdmult = x->rdmult;
1670 if ((cpi->sf.tx_domain_thresh > 0.0) || (cpi->sf.quant_opt_thresh > 0.0)) {
1671 double logvar = vp9_log_block_var(cpi, x, bsize);
1672 // Check block complexity as part of descision on using pixel or transform
1673 // domain distortion in rd tests.
1674 x->block_tx_domain = cpi->sf.allow_txfm_domain_distortion &&
1675 (logvar >= cpi->sf.tx_domain_thresh);
1677 // Check block complexity as part of descision on using quantized
1678 // coefficient optimisation inside the rd loop.
1679 x->block_qcoeff_opt =
1680 cpi->sf.allow_quant_coeff_opt && (logvar <= cpi->sf.quant_opt_thresh);
1682 x->block_tx_domain = cpi->sf.allow_txfm_domain_distortion;
1683 x->block_qcoeff_opt = cpi->sf.allow_quant_coeff_opt;
1686 if (aq_mode == VARIANCE_AQ) {
1688 bsize <= BLOCK_16X16 ? x->mb_energy : vp9_block_energy(cpi, x, bsize);
1690 if (cm->frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
1691 cpi->force_update_segmentation ||
1692 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
1693 mi->segment_id = vp9_vaq_segment_id(energy);
1695 const uint8_t *const map =
1696 cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1697 mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1699 x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
1700 } else if (aq_mode == LOOKAHEAD_AQ) {
1701 const uint8_t *const map = cpi->segmentation_map;
1703 // I do not change rdmult here consciously.
1704 mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1705 } else if (aq_mode == EQUATOR360_AQ) {
1706 if (cm->frame_type == KEY_FRAME || cpi->force_update_segmentation) {
1707 mi->segment_id = vp9_360aq_segment_id(mi_row, cm->mi_rows);
1709 const uint8_t *const map =
1710 cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1711 mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1713 x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
1714 } else if (aq_mode == COMPLEXITY_AQ) {
1715 x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
1716 } else if (aq_mode == CYCLIC_REFRESH_AQ) {
1717 const uint8_t *const map =
1718 cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1719 // If segment is boosted, use rdmult for that segment.
1720 if (cyclic_refresh_segment_id_boosted(
1721 get_segment_id(cm, map, bsize, mi_row, mi_col)))
1722 x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
1725 // Find best coding mode & reconstruct the MB so it is available
1726 // as a predictor for MBs that follow in the SB
1727 if (frame_is_intra_only(cm)) {
1728 vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, best_rd);
1730 if (bsize >= BLOCK_8X8) {
1731 if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP))
1732 vp9_rd_pick_inter_mode_sb_seg_skip(cpi, tile_data, x, rd_cost, bsize,
1735 vp9_rd_pick_inter_mode_sb(cpi, tile_data, x, mi_row, mi_col, rd_cost,
1736 bsize, ctx, best_rd);
1738 vp9_rd_pick_inter_mode_sub8x8(cpi, tile_data, x, mi_row, mi_col, rd_cost,
1739 bsize, ctx, best_rd);
1743 // Examine the resulting rate and for AQ mode 2 make a segment choice.
1744 if ((rd_cost->rate != INT_MAX) && (aq_mode == COMPLEXITY_AQ) &&
1745 (bsize >= BLOCK_16X16) &&
1746 (cm->frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
1747 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref))) {
1748 vp9_caq_select_segment(cpi, x, bsize, mi_row, mi_col, rd_cost->rate);
1751 x->rdmult = orig_rdmult;
1753 // TODO(jingning) The rate-distortion optimization flow needs to be
1754 // refactored to provide proper exit/return handle.
1755 if (rd_cost->rate == INT_MAX) rd_cost->rdcost = INT64_MAX;
1757 ctx->rate = rd_cost->rate;
1758 ctx->dist = rd_cost->dist;
1761 static void update_stats(VP9_COMMON *cm, ThreadData *td) {
1762 const MACROBLOCK *x = &td->mb;
1763 const MACROBLOCKD *const xd = &x->e_mbd;
1764 const MODE_INFO *const mi = xd->mi[0];
1765 const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
1766 const BLOCK_SIZE bsize = mi->sb_type;
1768 if (!frame_is_intra_only(cm)) {
1769 FRAME_COUNTS *const counts = td->counts;
1770 const int inter_block = is_inter_block(mi);
1771 const int seg_ref_active =
1772 segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_REF_FRAME);
1773 if (!seg_ref_active) {
1774 counts->intra_inter[get_intra_inter_context(xd)][inter_block]++;
1775 // If the segment reference feature is enabled we have only a single
1776 // reference frame allowed for the segment so exclude it from
1777 // the reference frame counts used to work out probabilities.
1779 const MV_REFERENCE_FRAME ref0 = mi->ref_frame[0];
1780 if (cm->reference_mode == REFERENCE_MODE_SELECT)
1781 counts->comp_inter[vp9_get_reference_mode_context(cm, xd)]
1782 [has_second_ref(mi)]++;
1784 if (has_second_ref(mi)) {
1785 counts->comp_ref[vp9_get_pred_context_comp_ref_p(cm, xd)]
1786 [ref0 == GOLDEN_FRAME]++;
1788 counts->single_ref[vp9_get_pred_context_single_ref_p1(xd)][0]
1789 [ref0 != LAST_FRAME]++;
1790 if (ref0 != LAST_FRAME)
1791 counts->single_ref[vp9_get_pred_context_single_ref_p2(xd)][1]
1792 [ref0 != GOLDEN_FRAME]++;
1797 !segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP)) {
1798 const int mode_ctx = mbmi_ext->mode_context[mi->ref_frame[0]];
1799 if (bsize >= BLOCK_8X8) {
1800 const PREDICTION_MODE mode = mi->mode;
1801 ++counts->inter_mode[mode_ctx][INTER_OFFSET(mode)];
1803 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
1804 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
1806 for (idy = 0; idy < 2; idy += num_4x4_h) {
1807 for (idx = 0; idx < 2; idx += num_4x4_w) {
1808 const int j = idy * 2 + idx;
1809 const PREDICTION_MODE b_mode = mi->bmi[j].as_mode;
1810 ++counts->inter_mode[mode_ctx][INTER_OFFSET(b_mode)];
1818 static void restore_context(MACROBLOCK *const x, int mi_row, int mi_col,
1819 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
1820 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
1821 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
1823 MACROBLOCKD *const xd = &x->e_mbd;
1825 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1826 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1827 int mi_width = num_8x8_blocks_wide_lookup[bsize];
1828 int mi_height = num_8x8_blocks_high_lookup[bsize];
1829 for (p = 0; p < MAX_MB_PLANE; p++) {
1830 memcpy(xd->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
1831 a + num_4x4_blocks_wide * p,
1832 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
1833 xd->plane[p].subsampling_x);
1834 memcpy(xd->left_context[p] +
1835 ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
1836 l + num_4x4_blocks_high * p,
1837 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
1838 xd->plane[p].subsampling_y);
1840 memcpy(xd->above_seg_context + mi_col, sa,
1841 sizeof(*xd->above_seg_context) * mi_width);
1842 memcpy(xd->left_seg_context + (mi_row & MI_MASK), sl,
1843 sizeof(xd->left_seg_context[0]) * mi_height);
1846 static void save_context(MACROBLOCK *const x, int mi_row, int mi_col,
1847 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
1848 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
1849 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
1851 const MACROBLOCKD *const xd = &x->e_mbd;
1853 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1854 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1855 int mi_width = num_8x8_blocks_wide_lookup[bsize];
1856 int mi_height = num_8x8_blocks_high_lookup[bsize];
1858 // buffer the above/left context information of the block in search.
1859 for (p = 0; p < MAX_MB_PLANE; ++p) {
1860 memcpy(a + num_4x4_blocks_wide * p,
1861 xd->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
1862 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
1863 xd->plane[p].subsampling_x);
1864 memcpy(l + num_4x4_blocks_high * p,
1865 xd->left_context[p] +
1866 ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
1867 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
1868 xd->plane[p].subsampling_y);
1870 memcpy(sa, xd->above_seg_context + mi_col,
1871 sizeof(*xd->above_seg_context) * mi_width);
1872 memcpy(sl, xd->left_seg_context + (mi_row & MI_MASK),
1873 sizeof(xd->left_seg_context[0]) * mi_height);
1876 static void encode_b(VP9_COMP *cpi, const TileInfo *const tile, ThreadData *td,
1877 TOKENEXTRA **tp, int mi_row, int mi_col,
1878 int output_enabled, BLOCK_SIZE bsize,
1879 PICK_MODE_CONTEXT *ctx) {
1880 MACROBLOCK *const x = &td->mb;
1881 set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
1882 update_state(cpi, td, ctx, mi_row, mi_col, bsize, output_enabled);
1883 encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
1885 if (output_enabled) {
1886 update_stats(&cpi->common, td);
1888 (*tp)->token = EOSB_TOKEN;
1893 static void encode_sb(VP9_COMP *cpi, ThreadData *td, const TileInfo *const tile,
1894 TOKENEXTRA **tp, int mi_row, int mi_col,
1895 int output_enabled, BLOCK_SIZE bsize, PC_TREE *pc_tree) {
1896 VP9_COMMON *const cm = &cpi->common;
1897 MACROBLOCK *const x = &td->mb;
1898 MACROBLOCKD *const xd = &x->e_mbd;
1900 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
1902 PARTITION_TYPE partition;
1903 BLOCK_SIZE subsize = bsize;
1905 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1907 if (bsize >= BLOCK_8X8) {
1908 ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1909 subsize = get_subsize(bsize, pc_tree->partitioning);
1912 subsize = BLOCK_4X4;
1915 partition = partition_lookup[bsl][subsize];
1916 if (output_enabled && bsize != BLOCK_4X4)
1917 td->counts->partition[ctx][partition]++;
1919 switch (partition) {
1920 case PARTITION_NONE:
1921 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1924 case PARTITION_VERT:
1925 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1926 &pc_tree->vertical[0]);
1927 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
1928 encode_b(cpi, tile, td, tp, mi_row, mi_col + hbs, output_enabled,
1929 subsize, &pc_tree->vertical[1]);
1932 case PARTITION_HORZ:
1933 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1934 &pc_tree->horizontal[0]);
1935 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
1936 encode_b(cpi, tile, td, tp, mi_row + hbs, mi_col, output_enabled,
1937 subsize, &pc_tree->horizontal[1]);
1940 case PARTITION_SPLIT:
1941 if (bsize == BLOCK_8X8) {
1942 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1943 pc_tree->leaf_split[0]);
1945 encode_sb(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1947 encode_sb(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
1948 subsize, pc_tree->split[1]);
1949 encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
1950 subsize, pc_tree->split[2]);
1951 encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs, output_enabled,
1952 subsize, pc_tree->split[3]);
1955 default: assert(0 && "Invalid partition type."); break;
1958 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
1959 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1962 // Check to see if the given partition size is allowed for a specified number
1963 // of 8x8 block rows and columns remaining in the image.
1964 // If not then return the largest allowed partition size
1965 static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize, int rows_left,
1966 int cols_left, int *bh, int *bw) {
1967 if (rows_left <= 0 || cols_left <= 0) {
1968 return VPXMIN(bsize, BLOCK_8X8);
1970 for (; bsize > 0; bsize -= 3) {
1971 *bh = num_8x8_blocks_high_lookup[bsize];
1972 *bw = num_8x8_blocks_wide_lookup[bsize];
1973 if ((*bh <= rows_left) && (*bw <= cols_left)) {
1981 static void set_partial_b64x64_partition(MODE_INFO *mi, int mis, int bh_in,
1982 int bw_in, int row8x8_remaining,
1983 int col8x8_remaining, BLOCK_SIZE bsize,
1984 MODE_INFO **mi_8x8) {
1987 for (r = 0; r < MI_BLOCK_SIZE; r += bh) {
1989 for (c = 0; c < MI_BLOCK_SIZE; c += bw) {
1990 const int index = r * mis + c;
1991 mi_8x8[index] = mi + index;
1992 mi_8x8[index]->sb_type = find_partition_size(
1993 bsize, row8x8_remaining - r, col8x8_remaining - c, &bh, &bw);
1998 // This function attempts to set all mode info entries in a given SB64
1999 // to the same block partition size.
2000 // However, at the bottom and right borders of the image the requested size
2001 // may not be allowed in which case this code attempts to choose the largest
2002 // allowable partition.
2003 static void set_fixed_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
2004 MODE_INFO **mi_8x8, int mi_row, int mi_col,
2006 VP9_COMMON *const cm = &cpi->common;
2007 const int mis = cm->mi_stride;
2008 const int row8x8_remaining = tile->mi_row_end - mi_row;
2009 const int col8x8_remaining = tile->mi_col_end - mi_col;
2010 int block_row, block_col;
2011 MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
2012 int bh = num_8x8_blocks_high_lookup[bsize];
2013 int bw = num_8x8_blocks_wide_lookup[bsize];
2015 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
2017 // Apply the requested partition size to the SB64 if it is all "in image"
2018 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
2019 (row8x8_remaining >= MI_BLOCK_SIZE)) {
2020 for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
2021 for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
2022 int index = block_row * mis + block_col;
2023 mi_8x8[index] = mi_upper_left + index;
2024 mi_8x8[index]->sb_type = bsize;
2028 // Else this is a partial SB64.
2029 set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
2030 col8x8_remaining, bsize, mi_8x8);
2034 static const struct {
2037 } coord_lookup[16] = {
2060 static void set_source_var_based_partition(VP9_COMP *cpi,
2061 const TileInfo *const tile,
2062 MACROBLOCK *const x,
2063 MODE_INFO **mi_8x8, int mi_row,
2065 VP9_COMMON *const cm = &cpi->common;
2066 const int mis = cm->mi_stride;
2067 const int row8x8_remaining = tile->mi_row_end - mi_row;
2068 const int col8x8_remaining = tile->mi_col_end - mi_col;
2069 MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
2071 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
2073 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
2076 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
2077 (row8x8_remaining >= MI_BLOCK_SIZE)) {
2081 const int offset = (mi_row >> 1) * cm->mb_cols + (mi_col >> 1);
2082 int is_larger_better = 0;
2084 unsigned int thr = cpi->source_var_thresh;
2086 memset(d32, 0, 4 * sizeof(diff));
2088 for (i = 0; i < 4; i++) {
2091 for (j = 0; j < 4; j++) {
2092 int b_mi_row = coord_lookup[i * 4 + j].row;
2093 int b_mi_col = coord_lookup[i * 4 + j].col;
2094 int boffset = b_mi_row / 2 * cm->mb_cols + b_mi_col / 2;
2096 d16[j] = cpi->source_diff_var + offset + boffset;
2098 index = b_mi_row * mis + b_mi_col;
2099 mi_8x8[index] = mi_upper_left + index;
2100 mi_8x8[index]->sb_type = BLOCK_16X16;
2102 // TODO(yunqingwang): If d16[j].var is very large, use 8x8 partition
2103 // size to further improve quality.
2106 is_larger_better = (d16[0]->var < thr) && (d16[1]->var < thr) &&
2107 (d16[2]->var < thr) && (d16[3]->var < thr);
2109 // Use 32x32 partition
2110 if (is_larger_better) {
2113 for (j = 0; j < 4; j++) {
2114 d32[i].sse += d16[j]->sse;
2115 d32[i].sum += d16[j]->sum;
2119 (unsigned int)(d32[i].sse -
2120 (unsigned int)(((int64_t)d32[i].sum * d32[i].sum) >>
2123 index = coord_lookup[i * 4].row * mis + coord_lookup[i * 4].col;
2124 mi_8x8[index] = mi_upper_left + index;
2125 mi_8x8[index]->sb_type = BLOCK_32X32;
2129 if (use32x32 == 4) {
2131 is_larger_better = (d32[0].var < thr) && (d32[1].var < thr) &&
2132 (d32[2].var < thr) && (d32[3].var < thr);
2134 // Use 64x64 partition
2135 if (is_larger_better) {
2136 mi_8x8[0] = mi_upper_left;
2137 mi_8x8[0]->sb_type = BLOCK_64X64;
2140 } else { // partial in-image SB64
2141 int bh = num_8x8_blocks_high_lookup[BLOCK_16X16];
2142 int bw = num_8x8_blocks_wide_lookup[BLOCK_16X16];
2143 set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
2144 col8x8_remaining, BLOCK_16X16, mi_8x8);
2148 static void update_state_rt(VP9_COMP *cpi, ThreadData *td,
2149 PICK_MODE_CONTEXT *ctx, int mi_row, int mi_col,
2151 VP9_COMMON *const cm = &cpi->common;
2152 MACROBLOCK *const x = &td->mb;
2153 MACROBLOCKD *const xd = &x->e_mbd;
2154 MODE_INFO *const mi = xd->mi[0];
2155 struct macroblock_plane *const p = x->plane;
2156 const struct segmentation *const seg = &cm->seg;
2157 const int bw = num_8x8_blocks_wide_lookup[mi->sb_type];
2158 const int bh = num_8x8_blocks_high_lookup[mi->sb_type];
2159 const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
2160 const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
2162 *(xd->mi[0]) = ctx->mic;
2163 *(x->mbmi_ext) = ctx->mbmi_ext;
2165 if (seg->enabled && cpi->oxcf.aq_mode != NO_AQ) {
2166 // For in frame complexity AQ or variance AQ, copy segment_id from
2167 // segmentation_map.
2168 if (cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ) {
2169 const uint8_t *const map =
2170 seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
2171 mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
2173 // Setting segmentation map for cyclic_refresh.
2174 vp9_cyclic_refresh_update_segment(cpi, mi, mi_row, mi_col, bsize,
2175 ctx->rate, ctx->dist, x->skip, p);
2177 vp9_init_plane_quantizers(cpi, x);
2180 if (is_inter_block(mi)) {
2181 vp9_update_mv_count(td);
2182 if (cm->interp_filter == SWITCHABLE) {
2183 const int pred_ctx = get_pred_context_switchable_interp(xd);
2184 ++td->counts->switchable_interp[pred_ctx][mi->interp_filter];
2187 if (mi->sb_type < BLOCK_8X8) {
2188 mi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
2189 mi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
2193 if (cm->use_prev_frame_mvs || !cm->error_resilient_mode ||
2194 (cpi->svc.use_base_mv && cpi->svc.number_spatial_layers > 1 &&
2195 cpi->svc.spatial_layer_id != cpi->svc.number_spatial_layers - 1)) {
2196 MV_REF *const frame_mvs =
2197 cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
2200 for (h = 0; h < y_mis; ++h) {
2201 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
2202 for (w = 0; w < x_mis; ++w) {
2203 MV_REF *const mv = frame_mv + w;
2204 mv->ref_frame[0] = mi->ref_frame[0];
2205 mv->ref_frame[1] = mi->ref_frame[1];
2206 mv->mv[0].as_int = mi->mv[0].as_int;
2207 mv->mv[1].as_int = mi->mv[1].as_int;
2212 x->skip = ctx->skip;
2213 x->skip_txfm[0] = mi->segment_id ? 0 : ctx->skip_txfm[0];
2216 static void encode_b_rt(VP9_COMP *cpi, ThreadData *td,
2217 const TileInfo *const tile, TOKENEXTRA **tp, int mi_row,
2218 int mi_col, int output_enabled, BLOCK_SIZE bsize,
2219 PICK_MODE_CONTEXT *ctx) {
2220 MACROBLOCK *const x = &td->mb;
2221 set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
2222 update_state_rt(cpi, td, ctx, mi_row, mi_col, bsize);
2224 encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
2225 update_stats(&cpi->common, td);
2227 (*tp)->token = EOSB_TOKEN;
2231 static void encode_sb_rt(VP9_COMP *cpi, ThreadData *td,
2232 const TileInfo *const tile, TOKENEXTRA **tp,
2233 int mi_row, int mi_col, int output_enabled,
2234 BLOCK_SIZE bsize, PC_TREE *pc_tree) {
2235 VP9_COMMON *const cm = &cpi->common;
2236 MACROBLOCK *const x = &td->mb;
2237 MACROBLOCKD *const xd = &x->e_mbd;
2239 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
2241 PARTITION_TYPE partition;
2244 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2246 if (bsize >= BLOCK_8X8) {
2247 const int idx_str = xd->mi_stride * mi_row + mi_col;
2248 MODE_INFO **mi_8x8 = cm->mi_grid_visible + idx_str;
2249 ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
2250 subsize = mi_8x8[0]->sb_type;
2253 subsize = BLOCK_4X4;
2256 partition = partition_lookup[bsl][subsize];
2257 if (output_enabled && bsize != BLOCK_4X4)
2258 td->counts->partition[ctx][partition]++;
2260 switch (partition) {
2261 case PARTITION_NONE:
2262 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2265 case PARTITION_VERT:
2266 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2267 &pc_tree->vertical[0]);
2268 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
2269 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
2270 subsize, &pc_tree->vertical[1]);
2273 case PARTITION_HORZ:
2274 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2275 &pc_tree->horizontal[0]);
2276 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
2277 encode_b_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
2278 subsize, &pc_tree->horizontal[1]);
2281 case PARTITION_SPLIT:
2282 subsize = get_subsize(bsize, PARTITION_SPLIT);
2283 encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2285 encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
2286 subsize, pc_tree->split[1]);
2287 encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
2288 subsize, pc_tree->split[2]);
2289 encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs,
2290 output_enabled, subsize, pc_tree->split[3]);
2292 default: assert(0 && "Invalid partition type."); break;
2295 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
2296 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2299 static void rd_use_partition(VP9_COMP *cpi, ThreadData *td,
2300 TileDataEnc *tile_data, MODE_INFO **mi_8x8,
2301 TOKENEXTRA **tp, int mi_row, int mi_col,
2302 BLOCK_SIZE bsize, int *rate, int64_t *dist,
2303 int do_recon, PC_TREE *pc_tree) {
2304 VP9_COMMON *const cm = &cpi->common;
2305 TileInfo *const tile_info = &tile_data->tile_info;
2306 MACROBLOCK *const x = &td->mb;
2307 MACROBLOCKD *const xd = &x->e_mbd;
2308 const int mis = cm->mi_stride;
2309 const int bsl = b_width_log2_lookup[bsize];
2310 const int mi_step = num_4x4_blocks_wide_lookup[bsize] / 2;
2311 const int bss = (1 << bsl) / 4;
2313 PARTITION_TYPE partition = PARTITION_NONE;
2315 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2316 PARTITION_CONTEXT sl[8], sa[8];
2317 RD_COST last_part_rdc, none_rdc, chosen_rdc;
2318 BLOCK_SIZE sub_subsize = BLOCK_4X4;
2319 int splits_below = 0;
2320 BLOCK_SIZE bs_type = mi_8x8[0]->sb_type;
2321 int do_partition_search = 1;
2322 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
2324 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2326 assert(num_4x4_blocks_wide_lookup[bsize] ==
2327 num_4x4_blocks_high_lookup[bsize]);
2329 vp9_rd_cost_reset(&last_part_rdc);
2330 vp9_rd_cost_reset(&none_rdc);
2331 vp9_rd_cost_reset(&chosen_rdc);
2333 partition = partition_lookup[bsl][bs_type];
2334 subsize = get_subsize(bsize, partition);
2336 pc_tree->partitioning = partition;
2337 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2339 if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode != NO_AQ) {
2340 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2341 x->mb_energy = vp9_block_energy(cpi, x, bsize);
2344 if (do_partition_search &&
2345 cpi->sf.partition_search_type == SEARCH_PARTITION &&
2346 cpi->sf.adjust_partitioning_from_last_frame) {
2347 // Check if any of the sub blocks are further split.
2348 if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
2349 sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
2351 for (i = 0; i < 4; i++) {
2352 int jj = i >> 1, ii = i & 0x01;
2353 MODE_INFO *this_mi = mi_8x8[jj * bss * mis + ii * bss];
2354 if (this_mi && this_mi->sb_type >= sub_subsize) {
2360 // If partition is not none try none unless each of the 4 splits are split
2362 if (partition != PARTITION_NONE && !splits_below &&
2363 mi_row + (mi_step >> 1) < cm->mi_rows &&
2364 mi_col + (mi_step >> 1) < cm->mi_cols) {
2365 pc_tree->partitioning = PARTITION_NONE;
2366 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &none_rdc, bsize, ctx,
2369 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2371 if (none_rdc.rate < INT_MAX) {
2372 none_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
2374 RDCOST(x->rdmult, x->rddiv, none_rdc.rate, none_rdc.dist);
2377 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2378 mi_8x8[0]->sb_type = bs_type;
2379 pc_tree->partitioning = partition;
2383 switch (partition) {
2384 case PARTITION_NONE:
2385 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc, bsize,
2388 case PARTITION_HORZ:
2389 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
2390 subsize, &pc_tree->horizontal[0], INT64_MAX);
2391 if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
2392 mi_row + (mi_step >> 1) < cm->mi_rows) {
2394 PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
2395 vp9_rd_cost_init(&tmp_rdc);
2396 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
2397 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
2398 rd_pick_sb_modes(cpi, tile_data, x, mi_row + (mi_step >> 1), mi_col,
2399 &tmp_rdc, subsize, &pc_tree->horizontal[1], INT64_MAX);
2400 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2401 vp9_rd_cost_reset(&last_part_rdc);
2404 last_part_rdc.rate += tmp_rdc.rate;
2405 last_part_rdc.dist += tmp_rdc.dist;
2406 last_part_rdc.rdcost += tmp_rdc.rdcost;
2409 case PARTITION_VERT:
2410 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
2411 subsize, &pc_tree->vertical[0], INT64_MAX);
2412 if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
2413 mi_col + (mi_step >> 1) < cm->mi_cols) {
2415 PICK_MODE_CONTEXT *ctx = &pc_tree->vertical[0];
2416 vp9_rd_cost_init(&tmp_rdc);
2417 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
2418 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
2419 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + (mi_step >> 1),
2421 &pc_tree->vertical[bsize > BLOCK_8X8], INT64_MAX);
2422 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2423 vp9_rd_cost_reset(&last_part_rdc);
2426 last_part_rdc.rate += tmp_rdc.rate;
2427 last_part_rdc.dist += tmp_rdc.dist;
2428 last_part_rdc.rdcost += tmp_rdc.rdcost;
2431 case PARTITION_SPLIT:
2432 if (bsize == BLOCK_8X8) {
2433 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
2434 subsize, pc_tree->leaf_split[0], INT64_MAX);
2437 last_part_rdc.rate = 0;
2438 last_part_rdc.dist = 0;
2439 last_part_rdc.rdcost = 0;
2440 for (i = 0; i < 4; i++) {
2441 int x_idx = (i & 1) * (mi_step >> 1);
2442 int y_idx = (i >> 1) * (mi_step >> 1);
2443 int jj = i >> 1, ii = i & 0x01;
2445 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
2448 vp9_rd_cost_init(&tmp_rdc);
2449 rd_use_partition(cpi, td, tile_data, mi_8x8 + jj * bss * mis + ii * bss,
2450 tp, mi_row + y_idx, mi_col + x_idx, subsize,
2451 &tmp_rdc.rate, &tmp_rdc.dist, i != 3,
2453 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2454 vp9_rd_cost_reset(&last_part_rdc);
2457 last_part_rdc.rate += tmp_rdc.rate;
2458 last_part_rdc.dist += tmp_rdc.dist;
2461 default: assert(0); break;
2464 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2465 if (last_part_rdc.rate < INT_MAX) {
2466 last_part_rdc.rate += cpi->partition_cost[pl][partition];
2467 last_part_rdc.rdcost =
2468 RDCOST(x->rdmult, x->rddiv, last_part_rdc.rate, last_part_rdc.dist);
2471 if (do_partition_search && cpi->sf.adjust_partitioning_from_last_frame &&
2472 cpi->sf.partition_search_type == SEARCH_PARTITION &&
2473 partition != PARTITION_SPLIT && bsize > BLOCK_8X8 &&
2474 (mi_row + mi_step < cm->mi_rows ||
2475 mi_row + (mi_step >> 1) == cm->mi_rows) &&
2476 (mi_col + mi_step < cm->mi_cols ||
2477 mi_col + (mi_step >> 1) == cm->mi_cols)) {
2478 BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
2479 chosen_rdc.rate = 0;
2480 chosen_rdc.dist = 0;
2481 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2482 pc_tree->partitioning = PARTITION_SPLIT;
2485 for (i = 0; i < 4; i++) {
2486 int x_idx = (i & 1) * (mi_step >> 1);
2487 int y_idx = (i >> 1) * (mi_step >> 1);
2489 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2490 PARTITION_CONTEXT sl[8], sa[8];
2492 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
2495 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2496 pc_tree->split[i]->partitioning = PARTITION_NONE;
2497 rd_pick_sb_modes(cpi, tile_data, x, mi_row + y_idx, mi_col + x_idx,
2498 &tmp_rdc, split_subsize, &pc_tree->split[i]->none,
2501 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2503 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2504 vp9_rd_cost_reset(&chosen_rdc);
2508 chosen_rdc.rate += tmp_rdc.rate;
2509 chosen_rdc.dist += tmp_rdc.dist;
2512 encode_sb(cpi, td, tile_info, tp, mi_row + y_idx, mi_col + x_idx, 0,
2513 split_subsize, pc_tree->split[i]);
2515 pl = partition_plane_context(xd, mi_row + y_idx, mi_col + x_idx,
2517 chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
2519 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2520 if (chosen_rdc.rate < INT_MAX) {
2521 chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
2523 RDCOST(x->rdmult, x->rddiv, chosen_rdc.rate, chosen_rdc.dist);
2527 // If last_part is better set the partitioning to that.
2528 if (last_part_rdc.rdcost < chosen_rdc.rdcost) {
2529 mi_8x8[0]->sb_type = bsize;
2530 if (bsize >= BLOCK_8X8) pc_tree->partitioning = partition;
2531 chosen_rdc = last_part_rdc;
2533 // If none was better set the partitioning to that.
2534 if (none_rdc.rdcost < chosen_rdc.rdcost) {
2535 if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
2536 chosen_rdc = none_rdc;
2539 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2541 // We must have chosen a partitioning and encoding or we'll fail later on.
2542 // No other opportunities for success.
2543 if (bsize == BLOCK_64X64)
2544 assert(chosen_rdc.rate < INT_MAX && chosen_rdc.dist < INT64_MAX);
2547 int output_enabled = (bsize == BLOCK_64X64);
2548 encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
2552 *rate = chosen_rdc.rate;
2553 *dist = chosen_rdc.dist;
2556 static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
2557 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
2558 BLOCK_4X4, BLOCK_8X8, BLOCK_8X8, BLOCK_8X8, BLOCK_16X16,
2559 BLOCK_16X16, BLOCK_16X16, BLOCK_16X16
2562 static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
2563 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_32X32,
2564 BLOCK_32X32, BLOCK_32X32, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64,
2565 BLOCK_64X64, BLOCK_64X64, BLOCK_64X64
2568 // Look at all the mode_info entries for blocks that are part of this
2569 // partition and find the min and max values for sb_type.
2570 // At the moment this is designed to work on a 64x64 SB but could be
2571 // adjusted to use a size parameter.
2573 // The min and max are assumed to have been initialized prior to calling this
2574 // function so repeat calls can accumulate a min and max of more than one sb64.
2575 static void get_sb_partition_size_range(MACROBLOCKD *xd, MODE_INFO **mi_8x8,
2576 BLOCK_SIZE *min_block_size,
2577 BLOCK_SIZE *max_block_size,
2578 int bs_hist[BLOCK_SIZES]) {
2579 int sb_width_in_blocks = MI_BLOCK_SIZE;
2580 int sb_height_in_blocks = MI_BLOCK_SIZE;
2584 // Check the sb_type for each block that belongs to this region.
2585 for (i = 0; i < sb_height_in_blocks; ++i) {
2586 for (j = 0; j < sb_width_in_blocks; ++j) {
2587 MODE_INFO *mi = mi_8x8[index + j];
2588 BLOCK_SIZE sb_type = mi ? mi->sb_type : 0;
2590 *min_block_size = VPXMIN(*min_block_size, sb_type);
2591 *max_block_size = VPXMAX(*max_block_size, sb_type);
2593 index += xd->mi_stride;
2597 // Next square block size less or equal than current block size.
2598 static const BLOCK_SIZE next_square_size[BLOCK_SIZES] = {
2599 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_8X8, BLOCK_8X8,
2600 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_32X32,
2601 BLOCK_32X32, BLOCK_32X32, BLOCK_64X64
2604 // Look at neighboring blocks and set a min and max partition size based on
2606 static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
2607 MACROBLOCKD *const xd, int mi_row,
2608 int mi_col, BLOCK_SIZE *min_block_size,
2609 BLOCK_SIZE *max_block_size) {
2610 VP9_COMMON *const cm = &cpi->common;
2611 MODE_INFO **mi = xd->mi;
2612 const int left_in_image = !!xd->left_mi;
2613 const int above_in_image = !!xd->above_mi;
2614 const int row8x8_remaining = tile->mi_row_end - mi_row;
2615 const int col8x8_remaining = tile->mi_col_end - mi_col;
2617 BLOCK_SIZE min_size = BLOCK_4X4;
2618 BLOCK_SIZE max_size = BLOCK_64X64;
2619 int bs_hist[BLOCK_SIZES] = { 0 };
2621 // Trap case where we do not have a prediction.
2622 if (left_in_image || above_in_image || cm->frame_type != KEY_FRAME) {
2623 // Default "min to max" and "max to min"
2624 min_size = BLOCK_64X64;
2625 max_size = BLOCK_4X4;
2627 // NOTE: each call to get_sb_partition_size_range() uses the previous
2628 // passed in values for min and max as a starting point.
2629 // Find the min and max partition used in previous frame at this location
2630 if (cm->frame_type != KEY_FRAME) {
2631 MODE_INFO **prev_mi =
2632 &cm->prev_mi_grid_visible[mi_row * xd->mi_stride + mi_col];
2633 get_sb_partition_size_range(xd, prev_mi, &min_size, &max_size, bs_hist);
2635 // Find the min and max partition sizes used in the left SB64
2636 if (left_in_image) {
2637 MODE_INFO **left_sb64_mi = &mi[-MI_BLOCK_SIZE];
2638 get_sb_partition_size_range(xd, left_sb64_mi, &min_size, &max_size,
2641 // Find the min and max partition sizes used in the above SB64.
2642 if (above_in_image) {
2643 MODE_INFO **above_sb64_mi = &mi[-xd->mi_stride * MI_BLOCK_SIZE];
2644 get_sb_partition_size_range(xd, above_sb64_mi, &min_size, &max_size,
2648 // Adjust observed min and max for "relaxed" auto partition case.
2649 if (cpi->sf.auto_min_max_partition_size == RELAXED_NEIGHBORING_MIN_MAX) {
2650 min_size = min_partition_size[min_size];
2651 max_size = max_partition_size[max_size];
2655 // Check border cases where max and min from neighbors may not be legal.
2656 max_size = find_partition_size(max_size, row8x8_remaining, col8x8_remaining,
2658 // Test for blocks at the edge of the active image.
2659 // This may be the actual edge of the image or where there are formatting
2661 if (vp9_active_edge_sb(cpi, mi_row, mi_col)) {
2662 min_size = BLOCK_4X4;
2665 VPXMIN(cpi->sf.rd_auto_partition_min_limit, VPXMIN(min_size, max_size));
2668 // When use_square_partition_only is true, make sure at least one square
2669 // partition is allowed by selecting the next smaller square size as
2671 if (cpi->sf.use_square_partition_only &&
2672 next_square_size[max_size] < min_size) {
2673 min_size = next_square_size[max_size];
2676 *min_block_size = min_size;
2677 *max_block_size = max_size;
2680 // TODO(jingning) refactor functions setting partition search range
2681 static void set_partition_range(VP9_COMMON *cm, MACROBLOCKD *xd, int mi_row,
2682 int mi_col, BLOCK_SIZE bsize,
2683 BLOCK_SIZE *min_bs, BLOCK_SIZE *max_bs) {
2684 int mi_width = num_8x8_blocks_wide_lookup[bsize];
2685 int mi_height = num_8x8_blocks_high_lookup[bsize];
2689 const int idx_str = cm->mi_stride * mi_row + mi_col;
2690 MODE_INFO **prev_mi = &cm->prev_mi_grid_visible[idx_str];
2691 BLOCK_SIZE bs, min_size, max_size;
2693 min_size = BLOCK_64X64;
2694 max_size = BLOCK_4X4;
2697 for (idy = 0; idy < mi_height; ++idy) {
2698 for (idx = 0; idx < mi_width; ++idx) {
2699 mi = prev_mi[idy * cm->mi_stride + idx];
2700 bs = mi ? mi->sb_type : bsize;
2701 min_size = VPXMIN(min_size, bs);
2702 max_size = VPXMAX(max_size, bs);
2708 for (idy = 0; idy < mi_height; ++idy) {
2709 mi = xd->mi[idy * cm->mi_stride - 1];
2710 bs = mi ? mi->sb_type : bsize;
2711 min_size = VPXMIN(min_size, bs);
2712 max_size = VPXMAX(max_size, bs);
2717 for (idx = 0; idx < mi_width; ++idx) {
2718 mi = xd->mi[idx - cm->mi_stride];
2719 bs = mi ? mi->sb_type : bsize;
2720 min_size = VPXMIN(min_size, bs);
2721 max_size = VPXMAX(max_size, bs);
2725 if (min_size == max_size) {
2726 min_size = min_partition_size[min_size];
2727 max_size = max_partition_size[max_size];
2734 static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
2735 memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
2738 static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
2739 memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
2742 #if CONFIG_FP_MB_STATS
2743 const int num_16x16_blocks_wide_lookup[BLOCK_SIZES] = { 1, 1, 1, 1, 1, 1, 1,
2745 const int num_16x16_blocks_high_lookup[BLOCK_SIZES] = { 1, 1, 1, 1, 1, 1, 1,
2747 const int qindex_skip_threshold_lookup[BLOCK_SIZES] = {
2748 0, 10, 10, 30, 40, 40, 60, 80, 80, 90, 100, 100, 120
2750 const int qindex_split_threshold_lookup[BLOCK_SIZES] = {
2751 0, 3, 3, 7, 15, 15, 30, 40, 40, 60, 80, 80, 120
2753 const int complexity_16x16_blocks_threshold[BLOCK_SIZES] = {
2754 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 6
2766 static INLINE MOTION_DIRECTION get_motion_direction_fp(uint8_t fp_byte) {
2767 if (fp_byte & FPMB_MOTION_ZERO_MASK) {
2769 } else if (fp_byte & FPMB_MOTION_LEFT_MASK) {
2771 } else if (fp_byte & FPMB_MOTION_RIGHT_MASK) {
2773 } else if (fp_byte & FPMB_MOTION_UP_MASK) {
2780 static INLINE int get_motion_inconsistency(MOTION_DIRECTION this_mv,
2781 MOTION_DIRECTION that_mv) {
2782 if (this_mv == that_mv) {
2785 return abs(this_mv - that_mv) == 2 ? 2 : 1;
2790 // Calculate the score used in machine-learning based partition search early
2792 static double compute_score(VP9_COMMON *const cm, MACROBLOCKD *const xd,
2793 PICK_MODE_CONTEXT *ctx, int mi_row, int mi_col,
2799 abs(ctx->mic.mv[0].as_mv.col) + abs(ctx->mic.mv[0].as_mv.row);
2800 const int left_in_image = !!xd->left_mi;
2801 const int above_in_image = !!xd->above_mi;
2802 MODE_INFO **prev_mi =
2803 &cm->prev_mi_grid_visible[mi_col + cm->mi_stride * mi_row];
2804 int above_par = 0; // above_partitioning
2805 int left_par = 0; // left_partitioning
2806 int last_par = 0; // last_partitioning
2807 BLOCK_SIZE context_size;
2811 assert(b_width_log2_lookup[bsize] == b_height_log2_lookup[bsize]);
2813 if (above_in_image) {
2814 context_size = xd->above_mi->sb_type;
2815 if (context_size < bsize)
2817 else if (context_size == bsize)
2821 if (left_in_image) {
2822 context_size = xd->left_mi->sb_type;
2823 if (context_size < bsize)
2825 else if (context_size == bsize)
2830 context_size = prev_mi[0]->sb_type;
2831 if (context_size < bsize)
2833 else if (context_size == bsize)
2837 if (bsize == BLOCK_64X64)
2839 else if (bsize == BLOCK_32X32)
2841 else if (bsize == BLOCK_16X16)
2844 // early termination score calculation
2845 clf = &classifiers[offset];
2846 mean = &train_mean[offset];
2847 sd = &train_stdm[offset];
2848 score = clf[0] * (((double)ctx->rate - mean[0]) / sd[0]) +
2849 clf[1] * (((double)ctx->dist - mean[1]) / sd[1]) +
2850 clf[2] * (((double)mag_mv / 2 - mean[2]) * sd[2]) +
2851 clf[3] * (((double)(left_par + above_par) / 2 - mean[3]) * sd[3]) +
2852 clf[4] * (((double)ctx->sum_y_eobs - mean[4]) / sd[4]) +
2853 clf[5] * (((double)cm->base_qindex - mean[5]) * sd[5]) +
2854 clf[6] * (((double)last_par - mean[6]) * sd[6]) + clf[7];
2858 // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
2859 // unlikely to be selected depending on previous rate-distortion optimization
2860 // results, for encoding speed-up.
2861 static void rd_pick_partition(VP9_COMP *cpi, ThreadData *td,
2862 TileDataEnc *tile_data, TOKENEXTRA **tp,
2863 int mi_row, int mi_col, BLOCK_SIZE bsize,
2864 RD_COST *rd_cost, int64_t best_rd,
2866 VP9_COMMON *const cm = &cpi->common;
2867 TileInfo *const tile_info = &tile_data->tile_info;
2868 MACROBLOCK *const x = &td->mb;
2869 MACROBLOCKD *const xd = &x->e_mbd;
2870 const int mi_step = num_8x8_blocks_wide_lookup[bsize] / 2;
2871 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2872 PARTITION_CONTEXT sl[8], sa[8];
2873 TOKENEXTRA *tp_orig = *tp;
2874 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
2876 const int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2878 RD_COST this_rdc, sum_rdc, best_rdc;
2879 int do_split = bsize >= BLOCK_8X8;
2881 INTERP_FILTER pred_interp_filter;
2883 // Override skipping rectangular partition operations for edge blocks
2884 const int force_horz_split = (mi_row + mi_step >= cm->mi_rows);
2885 const int force_vert_split = (mi_col + mi_step >= cm->mi_cols);
2886 const int xss = x->e_mbd.plane[1].subsampling_x;
2887 const int yss = x->e_mbd.plane[1].subsampling_y;
2889 BLOCK_SIZE min_size = x->min_partition_size;
2890 BLOCK_SIZE max_size = x->max_partition_size;
2892 #if CONFIG_FP_MB_STATS
2893 unsigned int src_diff_var = UINT_MAX;
2894 int none_complexity = 0;
2897 int partition_none_allowed = !force_horz_split && !force_vert_split;
2898 int partition_horz_allowed =
2899 !force_vert_split && yss <= xss && bsize >= BLOCK_8X8;
2900 int partition_vert_allowed =
2901 !force_horz_split && xss <= yss && bsize >= BLOCK_8X8;
2903 int64_t dist_breakout_thr = cpi->sf.partition_search_breakout_thr.dist;
2904 int rate_breakout_thr = cpi->sf.partition_search_breakout_thr.rate;
2908 assert(num_8x8_blocks_wide_lookup[bsize] ==
2909 num_8x8_blocks_high_lookup[bsize]);
2911 // Adjust dist breakout threshold according to the partition size.
2912 dist_breakout_thr >>=
2913 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
2914 rate_breakout_thr *= num_pels_log2_lookup[bsize];
2916 vp9_rd_cost_init(&this_rdc);
2917 vp9_rd_cost_init(&sum_rdc);
2918 vp9_rd_cost_reset(&best_rdc);
2919 best_rdc.rdcost = best_rd;
2921 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2923 if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode != NO_AQ &&
2924 cpi->oxcf.aq_mode != LOOKAHEAD_AQ)
2925 x->mb_energy = vp9_block_energy(cpi, x, bsize);
2927 if (cpi->sf.cb_partition_search && bsize == BLOCK_16X16) {
2928 int cb_partition_search_ctrl =
2929 ((pc_tree->index == 0 || pc_tree->index == 3) +
2930 get_chessboard_index(cm->current_video_frame)) &
2933 if (cb_partition_search_ctrl && bsize > min_size && bsize < max_size)
2934 set_partition_range(cm, xd, mi_row, mi_col, bsize, &min_size, &max_size);
2937 // Determine partition types in search according to the speed features.
2938 // The threshold set here has to be of square block size.
2939 if (cpi->sf.auto_min_max_partition_size) {
2940 partition_none_allowed &= (bsize <= max_size && bsize >= min_size);
2941 partition_horz_allowed &=
2942 ((bsize <= max_size && bsize > min_size) || force_horz_split);
2943 partition_vert_allowed &=
2944 ((bsize <= max_size && bsize > min_size) || force_vert_split);
2945 do_split &= bsize > min_size;
2948 if (cpi->sf.use_square_partition_only &&
2949 bsize > cpi->sf.use_square_only_threshold) {
2951 if (!vp9_active_h_edge(cpi, mi_row, mi_step) || x->e_mbd.lossless)
2952 partition_horz_allowed &= force_horz_split;
2953 if (!vp9_active_v_edge(cpi, mi_row, mi_step) || x->e_mbd.lossless)
2954 partition_vert_allowed &= force_vert_split;
2956 partition_horz_allowed &= force_horz_split;
2957 partition_vert_allowed &= force_vert_split;
2961 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2963 #if CONFIG_FP_MB_STATS
2964 if (cpi->use_fp_mb_stats) {
2965 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2966 src_diff_var = get_sby_perpixel_diff_variance(cpi, &x->plane[0].src, mi_row,
2971 #if CONFIG_FP_MB_STATS
2972 // Decide whether we shall split directly and skip searching NONE by using
2973 // the first pass block statistics
2974 if (cpi->use_fp_mb_stats && bsize >= BLOCK_32X32 && do_split &&
2975 partition_none_allowed && src_diff_var > 4 &&
2976 cm->base_qindex < qindex_split_threshold_lookup[bsize]) {
2977 int mb_row = mi_row >> 1;
2978 int mb_col = mi_col >> 1;
2980 VPXMIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
2982 VPXMIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
2985 // compute a complexity measure, basically measure inconsistency of motion
2986 // vectors obtained from the first pass in the current block
2987 for (r = mb_row; r < mb_row_end; r++) {
2988 for (c = mb_col; c < mb_col_end; c++) {
2989 const int mb_index = r * cm->mb_cols + c;
2991 MOTION_DIRECTION this_mv;
2992 MOTION_DIRECTION right_mv;
2993 MOTION_DIRECTION bottom_mv;
2996 get_motion_direction_fp(cpi->twopass.this_frame_mb_stats[mb_index]);
2999 if (c != mb_col_end - 1) {
3000 right_mv = get_motion_direction_fp(
3001 cpi->twopass.this_frame_mb_stats[mb_index + 1]);
3002 none_complexity += get_motion_inconsistency(this_mv, right_mv);
3006 if (r != mb_row_end - 1) {
3007 bottom_mv = get_motion_direction_fp(
3008 cpi->twopass.this_frame_mb_stats[mb_index + cm->mb_cols]);
3009 none_complexity += get_motion_inconsistency(this_mv, bottom_mv);
3012 // do not count its left and top neighbors to avoid double counting
3016 if (none_complexity > complexity_16x16_blocks_threshold[bsize]) {
3017 partition_none_allowed = 0;
3023 if (partition_none_allowed) {
3024 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &this_rdc, bsize, ctx,
3026 if (this_rdc.rate != INT_MAX) {
3027 if (bsize >= BLOCK_8X8) {
3028 this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
3030 RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
3033 if (this_rdc.rdcost < best_rdc.rdcost) {
3034 MODE_INFO *mi = xd->mi[0];
3036 best_rdc = this_rdc;
3037 if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
3039 if (!cpi->sf.ml_partition_search_early_termination) {
3040 // If all y, u, v transform blocks in this partition are skippable,
3041 // and the dist & rate are within the thresholds, the partition search
3042 // is terminated for current branch of the partition search tree.
3043 if (!x->e_mbd.lossless && ctx->skippable &&
3044 ((best_rdc.dist < (dist_breakout_thr >> 2)) ||
3045 (best_rdc.dist < dist_breakout_thr &&
3046 best_rdc.rate < rate_breakout_thr))) {
3051 // Currently, the machine-learning based partition search early
3052 // termination is only used while bsize is 16x16, 32x32 or 64x64,
3053 // VPXMIN(cm->width, cm->height) >= 480, and speed = 0.
3054 if (!x->e_mbd.lossless &&
3055 !segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP) &&
3056 ctx->mic.mode >= INTRA_MODES && bsize >= BLOCK_16X16) {
3057 if (compute_score(cm, xd, ctx, mi_row, mi_col, bsize) < 0.0) {
3064 #if CONFIG_FP_MB_STATS
3065 // Check if every 16x16 first pass block statistics has zero
3066 // motion and the corresponding first pass residue is small enough.
3067 // If that is the case, check the difference variance between the
3068 // current frame and the last frame. If the variance is small enough,
3069 // stop further splitting in RD optimization
3070 if (cpi->use_fp_mb_stats && do_split != 0 &&
3071 cm->base_qindex > qindex_skip_threshold_lookup[bsize]) {
3072 int mb_row = mi_row >> 1;
3073 int mb_col = mi_col >> 1;
3075 VPXMIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
3077 VPXMIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
3081 for (r = mb_row; r < mb_row_end; r++) {
3082 for (c = mb_col; c < mb_col_end; c++) {
3083 const int mb_index = r * cm->mb_cols + c;
3084 if (!(cpi->twopass.this_frame_mb_stats[mb_index] &
3085 FPMB_MOTION_ZERO_MASK) ||
3086 !(cpi->twopass.this_frame_mb_stats[mb_index] &
3087 FPMB_ERROR_SMALL_MASK)) {
3098 if (src_diff_var == UINT_MAX) {
3099 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
3100 src_diff_var = get_sby_perpixel_diff_variance(
3101 cpi, &x->plane[0].src, mi_row, mi_col, bsize);
3103 if (src_diff_var < 8) {
3112 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3115 // store estimated motion vector
3116 if (cpi->sf.adaptive_motion_search) store_pred_mv(x, ctx);
3118 // If the interp_filter is marked as SWITCHABLE_FILTERS, it was for an
3119 // intra block and used for context purposes.
3120 if (ctx->mic.interp_filter == SWITCHABLE_FILTERS) {
3121 pred_interp_filter = EIGHTTAP;
3123 pred_interp_filter = ctx->mic.interp_filter;
3127 // TODO(jingning): use the motion vectors given by the above search as
3128 // the starting point of motion search in the following partition type check.
3130 subsize = get_subsize(bsize, PARTITION_SPLIT);
3131 if (bsize == BLOCK_8X8) {
3133 if (cpi->sf.adaptive_pred_interp_filter && partition_none_allowed)
3134 pc_tree->leaf_split[0]->pred_interp_filter = pred_interp_filter;
3135 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3136 pc_tree->leaf_split[0], best_rdc.rdcost);
3138 if (sum_rdc.rate == INT_MAX) sum_rdc.rdcost = INT64_MAX;
3140 for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
3141 const int x_idx = (i & 1) * mi_step;
3142 const int y_idx = (i >> 1) * mi_step;
3144 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
3147 if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3149 pc_tree->split[i]->index = i;
3150 rd_pick_partition(cpi, td, tile_data, tp, mi_row + y_idx,
3151 mi_col + x_idx, subsize, &this_rdc,
3152 best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
3154 if (this_rdc.rate == INT_MAX) {
3155 sum_rdc.rdcost = INT64_MAX;
3158 sum_rdc.rate += this_rdc.rate;
3159 sum_rdc.dist += this_rdc.dist;
3160 sum_rdc.rdcost += this_rdc.rdcost;
3165 if (sum_rdc.rdcost < best_rdc.rdcost && i == 4) {
3166 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
3167 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3169 if (sum_rdc.rdcost < best_rdc.rdcost) {
3171 pc_tree->partitioning = PARTITION_SPLIT;
3173 // Rate and distortion based partition search termination clause.
3174 if (!cpi->sf.ml_partition_search_early_termination &&
3175 !x->e_mbd.lossless && ((best_rdc.dist < (dist_breakout_thr >> 2)) ||
3176 (best_rdc.dist < dist_breakout_thr &&
3177 best_rdc.rate < rate_breakout_thr))) {
3182 // skip rectangular partition test when larger block size
3183 // gives better rd cost
3184 if ((cpi->sf.less_rectangular_check) &&
3185 ((bsize > cpi->sf.use_square_only_threshold) ||
3186 (best_rdc.dist < dist_breakout_thr)))
3187 do_rect &= !partition_none_allowed;
3189 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3193 if (partition_horz_allowed &&
3194 (do_rect || vp9_active_h_edge(cpi, mi_row, mi_step))) {
3195 subsize = get_subsize(bsize, PARTITION_HORZ);
3196 if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3197 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3198 partition_none_allowed)
3199 pc_tree->horizontal[0].pred_interp_filter = pred_interp_filter;
3200 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3201 &pc_tree->horizontal[0], best_rdc.rdcost);
3203 if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + mi_step < cm->mi_rows &&
3204 bsize > BLOCK_8X8) {
3205 PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
3206 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
3207 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
3209 if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3210 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3211 partition_none_allowed)
3212 pc_tree->horizontal[1].pred_interp_filter = pred_interp_filter;
3213 rd_pick_sb_modes(cpi, tile_data, x, mi_row + mi_step, mi_col, &this_rdc,
3214 subsize, &pc_tree->horizontal[1],
3215 best_rdc.rdcost - sum_rdc.rdcost);
3216 if (this_rdc.rate == INT_MAX) {
3217 sum_rdc.rdcost = INT64_MAX;
3219 sum_rdc.rate += this_rdc.rate;
3220 sum_rdc.dist += this_rdc.dist;
3221 sum_rdc.rdcost += this_rdc.rdcost;
3225 if (sum_rdc.rdcost < best_rdc.rdcost) {
3226 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
3227 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3228 if (sum_rdc.rdcost < best_rdc.rdcost) {
3230 pc_tree->partitioning = PARTITION_HORZ;
3232 if ((cpi->sf.less_rectangular_check) &&
3233 (bsize > cpi->sf.use_square_only_threshold))
3237 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3241 if (partition_vert_allowed &&
3242 (do_rect || vp9_active_v_edge(cpi, mi_col, mi_step))) {
3243 subsize = get_subsize(bsize, PARTITION_VERT);
3245 if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3246 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3247 partition_none_allowed)
3248 pc_tree->vertical[0].pred_interp_filter = pred_interp_filter;
3249 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3250 &pc_tree->vertical[0], best_rdc.rdcost);
3251 if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + mi_step < cm->mi_cols &&
3252 bsize > BLOCK_8X8) {
3253 update_state(cpi, td, &pc_tree->vertical[0], mi_row, mi_col, subsize, 0);
3254 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize,
3255 &pc_tree->vertical[0]);
3257 if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3258 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3259 partition_none_allowed)
3260 pc_tree->vertical[1].pred_interp_filter = pred_interp_filter;
3261 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + mi_step, &this_rdc,
3262 subsize, &pc_tree->vertical[1],
3263 best_rdc.rdcost - sum_rdc.rdcost);
3264 if (this_rdc.rate == INT_MAX) {
3265 sum_rdc.rdcost = INT64_MAX;
3267 sum_rdc.rate += this_rdc.rate;
3268 sum_rdc.dist += this_rdc.dist;
3269 sum_rdc.rdcost += this_rdc.rdcost;
3273 if (sum_rdc.rdcost < best_rdc.rdcost) {
3274 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
3275 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3276 if (sum_rdc.rdcost < best_rdc.rdcost) {
3278 pc_tree->partitioning = PARTITION_VERT;
3281 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3284 // TODO(jbb): This code added so that we avoid static analysis
3285 // warning related to the fact that best_rd isn't used after this
3286 // point. This code should be refactored so that the duplicate
3287 // checks occur in some sub function and thus are used...
3289 *rd_cost = best_rdc;
3291 if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX &&
3292 pc_tree->index != 3) {
3293 int output_enabled = (bsize == BLOCK_64X64);
3294 encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
3298 if (bsize == BLOCK_64X64) {
3299 assert(tp_orig < *tp);
3300 assert(best_rdc.rate < INT_MAX);
3301 assert(best_rdc.dist < INT64_MAX);
3303 assert(tp_orig == *tp);
3307 static void encode_rd_sb_row(VP9_COMP *cpi, ThreadData *td,
3308 TileDataEnc *tile_data, int mi_row,
3310 VP9_COMMON *const cm = &cpi->common;
3311 TileInfo *const tile_info = &tile_data->tile_info;
3312 MACROBLOCK *const x = &td->mb;
3313 MACROBLOCKD *const xd = &x->e_mbd;
3314 SPEED_FEATURES *const sf = &cpi->sf;
3315 const int mi_col_start = tile_info->mi_col_start;
3316 const int mi_col_end = tile_info->mi_col_end;
3318 const int sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
3319 const int num_sb_cols =
3320 get_num_cols(tile_data->tile_info, MI_BLOCK_SIZE_LOG2);
3323 // Initialize the left context for the new SB row
3324 memset(&xd->left_context, 0, sizeof(xd->left_context));
3325 memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
3327 // Code each SB in the row
3328 for (mi_col = mi_col_start, sb_col_in_tile = 0; mi_col < mi_col_end;
3329 mi_col += MI_BLOCK_SIZE, sb_col_in_tile++) {
3330 const struct segmentation *const seg = &cm->seg;
3337 const int idx_str = cm->mi_stride * mi_row + mi_col;
3338 MODE_INFO **mi = cm->mi_grid_visible + idx_str;
3340 (*(cpi->row_mt_sync_read_ptr))(&tile_data->row_mt_sync, sb_row,
3343 if (sf->adaptive_pred_interp_filter) {
3344 for (i = 0; i < 64; ++i) td->leaf_tree[i].pred_interp_filter = SWITCHABLE;
3346 for (i = 0; i < 64; ++i) {
3347 td->pc_tree[i].vertical[0].pred_interp_filter = SWITCHABLE;
3348 td->pc_tree[i].vertical[1].pred_interp_filter = SWITCHABLE;
3349 td->pc_tree[i].horizontal[0].pred_interp_filter = SWITCHABLE;
3350 td->pc_tree[i].horizontal[1].pred_interp_filter = SWITCHABLE;
3354 vp9_zero(x->pred_mv);
3355 td->pc_root->index = 0;
3358 const uint8_t *const map =
3359 seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
3360 int segment_id = get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
3361 seg_skip = segfeature_active(seg, segment_id, SEG_LVL_SKIP);
3364 x->source_variance = UINT_MAX;
3365 if (sf->partition_search_type == FIXED_PARTITION || seg_skip) {
3366 const BLOCK_SIZE bsize =
3367 seg_skip ? BLOCK_64X64 : sf->always_this_block_size;
3368 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3369 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
3370 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
3371 &dummy_rate, &dummy_dist, 1, td->pc_root);
3372 } else if (cpi->partition_search_skippable_frame) {
3374 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3375 bsize = get_rd_var_based_fixed_partition(cpi, x, mi_row, mi_col);
3376 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
3377 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
3378 &dummy_rate, &dummy_dist, 1, td->pc_root);
3379 } else if (sf->partition_search_type == VAR_BASED_PARTITION &&
3380 cm->frame_type != KEY_FRAME) {
3381 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
3382 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
3383 &dummy_rate, &dummy_dist, 1, td->pc_root);
3385 // If required set upper and lower partition size limits
3386 if (sf->auto_min_max_partition_size) {
3387 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3388 rd_auto_partition_range(cpi, tile_info, xd, mi_row, mi_col,
3389 &x->min_partition_size, &x->max_partition_size);
3391 rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, BLOCK_64X64,
3392 &dummy_rdc, INT64_MAX, td->pc_root);
3394 (*(cpi->row_mt_sync_write_ptr))(&tile_data->row_mt_sync, sb_row,
3395 sb_col_in_tile, num_sb_cols);
3399 static void init_encode_frame_mb_context(VP9_COMP *cpi) {
3400 MACROBLOCK *const x = &cpi->td.mb;
3401 VP9_COMMON *const cm = &cpi->common;
3402 MACROBLOCKD *const xd = &x->e_mbd;
3403 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
3405 // Copy data over into macro block data structures.
3406 vp9_setup_src_planes(x, cpi->Source, 0, 0);
3408 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
3410 // Note: this memset assumes above_context[0], [1] and [2]
3411 // are allocated as part of the same buffer.
3412 memset(xd->above_context[0], 0,
3413 sizeof(*xd->above_context[0]) * 2 * aligned_mi_cols * MAX_MB_PLANE);
3414 memset(xd->above_seg_context, 0,
3415 sizeof(*xd->above_seg_context) * aligned_mi_cols);
3418 static int check_dual_ref_flags(VP9_COMP *cpi) {
3419 const int ref_flags = cpi->ref_frame_flags;
3421 if (segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
3424 return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG) +
3425 !!(ref_flags & VP9_ALT_FLAG)) >= 2;
3429 static void reset_skip_tx_size(VP9_COMMON *cm, TX_SIZE max_tx_size) {
3431 const int mis = cm->mi_stride;
3432 MODE_INFO **mi_ptr = cm->mi_grid_visible;
3434 for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row, mi_ptr += mis) {
3435 for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
3436 if (mi_ptr[mi_col]->tx_size > max_tx_size)
3437 mi_ptr[mi_col]->tx_size = max_tx_size;
3442 static MV_REFERENCE_FRAME get_frame_type(const VP9_COMP *cpi) {
3443 if (frame_is_intra_only(&cpi->common))
3445 else if (cpi->rc.is_src_frame_alt_ref && cpi->refresh_golden_frame)
3446 return ALTREF_FRAME;
3447 else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
3448 return GOLDEN_FRAME;
3453 static TX_MODE select_tx_mode(const VP9_COMP *cpi, MACROBLOCKD *const xd) {
3454 if (xd->lossless) return ONLY_4X4;
3455 if (cpi->common.frame_type == KEY_FRAME && cpi->sf.use_nonrd_pick_mode)
3457 if (cpi->sf.tx_size_search_method == USE_LARGESTALL)
3459 else if (cpi->sf.tx_size_search_method == USE_FULL_RD ||
3460 cpi->sf.tx_size_search_method == USE_TX_8X8)
3461 return TX_MODE_SELECT;
3463 return cpi->common.tx_mode;
3466 static void hybrid_intra_mode_search(VP9_COMP *cpi, MACROBLOCK *const x,
3467 RD_COST *rd_cost, BLOCK_SIZE bsize,
3468 PICK_MODE_CONTEXT *ctx) {
3469 if (bsize < BLOCK_16X16)
3470 vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, INT64_MAX);
3472 vp9_pick_intra_mode(cpi, x, rd_cost, bsize, ctx);
3475 static void nonrd_pick_sb_modes(VP9_COMP *cpi, TileDataEnc *tile_data,
3476 MACROBLOCK *const x, int mi_row, int mi_col,
3477 RD_COST *rd_cost, BLOCK_SIZE bsize,
3478 PICK_MODE_CONTEXT *ctx) {
3479 VP9_COMMON *const cm = &cpi->common;
3480 TileInfo *const tile_info = &tile_data->tile_info;
3481 MACROBLOCKD *const xd = &x->e_mbd;
3483 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
3484 BLOCK_SIZE bs = VPXMAX(bsize, BLOCK_8X8); // processing unit block size
3485 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bs];
3486 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bs];
3489 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
3491 mi->sb_type = bsize;
3493 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
3494 struct macroblockd_plane *pd = &xd->plane[plane];
3495 memcpy(a + num_4x4_blocks_wide * plane, pd->above_context,
3496 (sizeof(a[0]) * num_4x4_blocks_wide) >> pd->subsampling_x);
3497 memcpy(l + num_4x4_blocks_high * plane, pd->left_context,
3498 (sizeof(l[0]) * num_4x4_blocks_high) >> pd->subsampling_y);
3501 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled)
3502 if (cyclic_refresh_segment_id_boosted(mi->segment_id))
3503 x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
3505 if (cm->frame_type == KEY_FRAME)
3506 hybrid_intra_mode_search(cpi, x, rd_cost, bsize, ctx);
3507 else if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP))
3508 set_mode_info_seg_skip(x, cm->tx_mode, rd_cost, bsize);
3509 else if (bsize >= BLOCK_8X8)
3510 vp9_pick_inter_mode(cpi, x, tile_data, mi_row, mi_col, rd_cost, bsize, ctx);
3512 vp9_pick_inter_mode_sub8x8(cpi, x, mi_row, mi_col, rd_cost, bsize, ctx);
3514 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
3516 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
3517 struct macroblockd_plane *pd = &xd->plane[plane];
3518 memcpy(pd->above_context, a + num_4x4_blocks_wide * plane,
3519 (sizeof(a[0]) * num_4x4_blocks_wide) >> pd->subsampling_x);
3520 memcpy(pd->left_context, l + num_4x4_blocks_high * plane,
3521 (sizeof(l[0]) * num_4x4_blocks_high) >> pd->subsampling_y);
3524 if (rd_cost->rate == INT_MAX) vp9_rd_cost_reset(rd_cost);
3526 ctx->rate = rd_cost->rate;
3527 ctx->dist = rd_cost->dist;
3530 static void fill_mode_info_sb(VP9_COMMON *cm, MACROBLOCK *x, int mi_row,
3531 int mi_col, BLOCK_SIZE bsize, PC_TREE *pc_tree) {
3532 MACROBLOCKD *xd = &x->e_mbd;
3533 int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3534 PARTITION_TYPE partition = pc_tree->partitioning;
3535 BLOCK_SIZE subsize = get_subsize(bsize, partition);
3537 assert(bsize >= BLOCK_8X8);
3539 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
3541 switch (partition) {
3542 case PARTITION_NONE:
3543 set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
3544 *(xd->mi[0]) = pc_tree->none.mic;
3545 *(x->mbmi_ext) = pc_tree->none.mbmi_ext;
3546 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
3548 case PARTITION_VERT:
3549 set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
3550 *(xd->mi[0]) = pc_tree->vertical[0].mic;
3551 *(x->mbmi_ext) = pc_tree->vertical[0].mbmi_ext;
3552 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
3554 if (mi_col + hbs < cm->mi_cols) {
3555 set_mode_info_offsets(cm, x, xd, mi_row, mi_col + hbs);
3556 *(xd->mi[0]) = pc_tree->vertical[1].mic;
3557 *(x->mbmi_ext) = pc_tree->vertical[1].mbmi_ext;
3558 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col + hbs, subsize);
3561 case PARTITION_HORZ:
3562 set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
3563 *(xd->mi[0]) = pc_tree->horizontal[0].mic;
3564 *(x->mbmi_ext) = pc_tree->horizontal[0].mbmi_ext;
3565 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
3566 if (mi_row + hbs < cm->mi_rows) {
3567 set_mode_info_offsets(cm, x, xd, mi_row + hbs, mi_col);
3568 *(xd->mi[0]) = pc_tree->horizontal[1].mic;
3569 *(x->mbmi_ext) = pc_tree->horizontal[1].mbmi_ext;
3570 duplicate_mode_info_in_sb(cm, xd, mi_row + hbs, mi_col, subsize);
3573 case PARTITION_SPLIT: {
3574 fill_mode_info_sb(cm, x, mi_row, mi_col, subsize, pc_tree->split[0]);
3575 fill_mode_info_sb(cm, x, mi_row, mi_col + hbs, subsize,
3577 fill_mode_info_sb(cm, x, mi_row + hbs, mi_col, subsize,
3579 fill_mode_info_sb(cm, x, mi_row + hbs, mi_col + hbs, subsize,
3587 // Reset the prediction pixel ready flag recursively.
3588 static void pred_pixel_ready_reset(PC_TREE *pc_tree, BLOCK_SIZE bsize) {
3589 pc_tree->none.pred_pixel_ready = 0;
3590 pc_tree->horizontal[0].pred_pixel_ready = 0;
3591 pc_tree->horizontal[1].pred_pixel_ready = 0;
3592 pc_tree->vertical[0].pred_pixel_ready = 0;
3593 pc_tree->vertical[1].pred_pixel_ready = 0;
3595 if (bsize > BLOCK_8X8) {
3596 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_SPLIT);
3598 for (i = 0; i < 4; ++i) pred_pixel_ready_reset(pc_tree->split[i], subsize);
3602 static void nonrd_pick_partition(VP9_COMP *cpi, ThreadData *td,
3603 TileDataEnc *tile_data, TOKENEXTRA **tp,
3604 int mi_row, int mi_col, BLOCK_SIZE bsize,
3605 RD_COST *rd_cost, int do_recon,
3606 int64_t best_rd, PC_TREE *pc_tree) {
3607 const SPEED_FEATURES *const sf = &cpi->sf;
3608 VP9_COMMON *const cm = &cpi->common;
3609 TileInfo *const tile_info = &tile_data->tile_info;
3610 MACROBLOCK *const x = &td->mb;
3611 MACROBLOCKD *const xd = &x->e_mbd;
3612 const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
3613 TOKENEXTRA *tp_orig = *tp;
3614 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
3616 BLOCK_SIZE subsize = bsize;
3617 RD_COST this_rdc, sum_rdc, best_rdc;
3618 int do_split = bsize >= BLOCK_8X8;
3620 // Override skipping rectangular partition operations for edge blocks
3621 const int force_horz_split = (mi_row + ms >= cm->mi_rows);
3622 const int force_vert_split = (mi_col + ms >= cm->mi_cols);
3623 const int xss = x->e_mbd.plane[1].subsampling_x;
3624 const int yss = x->e_mbd.plane[1].subsampling_y;
3626 int partition_none_allowed = !force_horz_split && !force_vert_split;
3627 int partition_horz_allowed =
3628 !force_vert_split && yss <= xss && bsize >= BLOCK_8X8;
3629 int partition_vert_allowed =
3630 !force_horz_split && xss <= yss && bsize >= BLOCK_8X8;
3633 assert(num_8x8_blocks_wide_lookup[bsize] ==
3634 num_8x8_blocks_high_lookup[bsize]);
3636 vp9_rd_cost_init(&sum_rdc);
3637 vp9_rd_cost_reset(&best_rdc);
3638 best_rdc.rdcost = best_rd;
3640 // Determine partition types in search according to the speed features.
3641 // The threshold set here has to be of square block size.
3642 if (sf->auto_min_max_partition_size) {
3643 partition_none_allowed &=
3644 (bsize <= x->max_partition_size && bsize >= x->min_partition_size);
3645 partition_horz_allowed &=
3646 ((bsize <= x->max_partition_size && bsize > x->min_partition_size) ||
3648 partition_vert_allowed &=
3649 ((bsize <= x->max_partition_size && bsize > x->min_partition_size) ||
3651 do_split &= bsize > x->min_partition_size;
3653 if (sf->use_square_partition_only) {
3654 partition_horz_allowed &= force_horz_split;
3655 partition_vert_allowed &= force_vert_split;
3658 ctx->pred_pixel_ready =
3659 !(partition_vert_allowed || partition_horz_allowed || do_split);
3662 if (partition_none_allowed) {
3663 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &this_rdc, bsize,
3665 ctx->mic = *xd->mi[0];
3666 ctx->mbmi_ext = *x->mbmi_ext;
3667 ctx->skip_txfm[0] = x->skip_txfm[0];
3668 ctx->skip = x->skip;
3670 if (this_rdc.rate != INT_MAX) {
3671 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3672 this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
3674 RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
3675 if (this_rdc.rdcost < best_rdc.rdcost) {
3676 int64_t dist_breakout_thr = sf->partition_search_breakout_thr.dist;
3677 int64_t rate_breakout_thr = sf->partition_search_breakout_thr.rate;
3679 dist_breakout_thr >>=
3680 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
3682 rate_breakout_thr *= num_pels_log2_lookup[bsize];
3684 best_rdc = this_rdc;
3685 if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
3687 if (!x->e_mbd.lossless && this_rdc.rate < rate_breakout_thr &&
3688 this_rdc.dist < dist_breakout_thr) {
3696 // store estimated motion vector
3697 store_pred_mv(x, ctx);
3701 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3702 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
3703 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3704 subsize = get_subsize(bsize, PARTITION_SPLIT);
3705 for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
3706 const int x_idx = (i & 1) * ms;
3707 const int y_idx = (i >> 1) * ms;
3709 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
3711 load_pred_mv(x, ctx);
3712 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row + y_idx,
3713 mi_col + x_idx, subsize, &this_rdc, 0,
3714 best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
3716 if (this_rdc.rate == INT_MAX) {
3717 vp9_rd_cost_reset(&sum_rdc);
3719 sum_rdc.rate += this_rdc.rate;
3720 sum_rdc.dist += this_rdc.dist;
3721 sum_rdc.rdcost += this_rdc.rdcost;
3725 if (sum_rdc.rdcost < best_rdc.rdcost) {
3727 pc_tree->partitioning = PARTITION_SPLIT;
3729 // skip rectangular partition test when larger block size
3730 // gives better rd cost
3731 if (sf->less_rectangular_check) do_rect &= !partition_none_allowed;
3736 if (partition_horz_allowed && do_rect) {
3737 subsize = get_subsize(bsize, PARTITION_HORZ);
3738 if (sf->adaptive_motion_search) load_pred_mv(x, ctx);
3739 pc_tree->horizontal[0].pred_pixel_ready = 1;
3740 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3741 &pc_tree->horizontal[0]);
3743 pc_tree->horizontal[0].mic = *xd->mi[0];
3744 pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
3745 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3746 pc_tree->horizontal[0].skip = x->skip;
3748 if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + ms < cm->mi_rows) {
3749 load_pred_mv(x, ctx);
3750 pc_tree->horizontal[1].pred_pixel_ready = 1;
3751 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + ms, mi_col, &this_rdc,
3752 subsize, &pc_tree->horizontal[1]);
3754 pc_tree->horizontal[1].mic = *xd->mi[0];
3755 pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
3756 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3757 pc_tree->horizontal[1].skip = x->skip;
3759 if (this_rdc.rate == INT_MAX) {
3760 vp9_rd_cost_reset(&sum_rdc);
3762 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3763 this_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
3764 sum_rdc.rate += this_rdc.rate;
3765 sum_rdc.dist += this_rdc.dist;
3767 RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3771 if (sum_rdc.rdcost < best_rdc.rdcost) {
3773 pc_tree->partitioning = PARTITION_HORZ;
3775 pred_pixel_ready_reset(pc_tree, bsize);
3780 if (partition_vert_allowed && do_rect) {
3781 subsize = get_subsize(bsize, PARTITION_VERT);
3782 if (sf->adaptive_motion_search) load_pred_mv(x, ctx);
3783 pc_tree->vertical[0].pred_pixel_ready = 1;
3784 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3785 &pc_tree->vertical[0]);
3786 pc_tree->vertical[0].mic = *xd->mi[0];
3787 pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
3788 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
3789 pc_tree->vertical[0].skip = x->skip;
3791 if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + ms < cm->mi_cols) {
3792 load_pred_mv(x, ctx);
3793 pc_tree->vertical[1].pred_pixel_ready = 1;
3794 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + ms, &this_rdc,
3795 subsize, &pc_tree->vertical[1]);
3796 pc_tree->vertical[1].mic = *xd->mi[0];
3797 pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
3798 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
3799 pc_tree->vertical[1].skip = x->skip;
3801 if (this_rdc.rate == INT_MAX) {
3802 vp9_rd_cost_reset(&sum_rdc);
3804 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3805 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
3806 sum_rdc.rate += this_rdc.rate;
3807 sum_rdc.dist += this_rdc.dist;
3809 RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3813 if (sum_rdc.rdcost < best_rdc.rdcost) {
3815 pc_tree->partitioning = PARTITION_VERT;
3817 pred_pixel_ready_reset(pc_tree, bsize);
3821 *rd_cost = best_rdc;
3823 if (best_rdc.rate == INT_MAX) {
3824 vp9_rd_cost_reset(rd_cost);
3828 // update mode info array
3829 fill_mode_info_sb(cm, x, mi_row, mi_col, bsize, pc_tree);
3831 if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX && do_recon) {
3832 int output_enabled = (bsize == BLOCK_64X64);
3833 encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
3837 if (bsize == BLOCK_64X64 && do_recon) {
3838 assert(tp_orig < *tp);
3839 assert(best_rdc.rate < INT_MAX);
3840 assert(best_rdc.dist < INT64_MAX);
3842 assert(tp_orig == *tp);
3846 static void nonrd_select_partition(VP9_COMP *cpi, ThreadData *td,
3847 TileDataEnc *tile_data, MODE_INFO **mi,
3848 TOKENEXTRA **tp, int mi_row, int mi_col,
3849 BLOCK_SIZE bsize, int output_enabled,
3850 RD_COST *rd_cost, PC_TREE *pc_tree) {
3851 VP9_COMMON *const cm = &cpi->common;
3852 TileInfo *const tile_info = &tile_data->tile_info;
3853 MACROBLOCK *const x = &td->mb;
3854 MACROBLOCKD *const xd = &x->e_mbd;
3855 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3856 const int mis = cm->mi_stride;
3857 PARTITION_TYPE partition;
3861 vp9_rd_cost_reset(&this_rdc);
3862 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
3864 subsize = (bsize >= BLOCK_8X8) ? mi[0]->sb_type : BLOCK_4X4;
3865 partition = partition_lookup[bsl][subsize];
3867 if (bsize == BLOCK_32X32 && subsize == BLOCK_32X32) {
3868 x->max_partition_size = BLOCK_32X32;
3869 x->min_partition_size = BLOCK_16X16;
3870 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
3871 0, INT64_MAX, pc_tree);
3872 } else if (bsize == BLOCK_32X32 && partition != PARTITION_NONE &&
3873 subsize >= BLOCK_16X16) {
3874 x->max_partition_size = BLOCK_32X32;
3875 x->min_partition_size = BLOCK_8X8;
3876 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
3877 0, INT64_MAX, pc_tree);
3878 } else if (bsize == BLOCK_16X16 && partition != PARTITION_NONE) {
3879 x->max_partition_size = BLOCK_16X16;
3880 x->min_partition_size = BLOCK_8X8;
3881 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
3882 0, INT64_MAX, pc_tree);
3884 switch (partition) {
3885 case PARTITION_NONE:
3886 pc_tree->none.pred_pixel_ready = 1;
3887 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
3889 pc_tree->none.mic = *xd->mi[0];
3890 pc_tree->none.mbmi_ext = *x->mbmi_ext;
3891 pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
3892 pc_tree->none.skip = x->skip;
3894 case PARTITION_VERT:
3895 pc_tree->vertical[0].pred_pixel_ready = 1;
3896 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
3897 &pc_tree->vertical[0]);
3898 pc_tree->vertical[0].mic = *xd->mi[0];
3899 pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
3900 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
3901 pc_tree->vertical[0].skip = x->skip;
3902 if (mi_col + hbs < cm->mi_cols) {
3903 pc_tree->vertical[1].pred_pixel_ready = 1;
3904 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs,
3905 &this_rdc, subsize, &pc_tree->vertical[1]);
3906 pc_tree->vertical[1].mic = *xd->mi[0];
3907 pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
3908 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
3909 pc_tree->vertical[1].skip = x->skip;
3910 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3911 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3912 rd_cost->rate += this_rdc.rate;
3913 rd_cost->dist += this_rdc.dist;
3917 case PARTITION_HORZ:
3918 pc_tree->horizontal[0].pred_pixel_ready = 1;
3919 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
3920 &pc_tree->horizontal[0]);
3921 pc_tree->horizontal[0].mic = *xd->mi[0];
3922 pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
3923 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3924 pc_tree->horizontal[0].skip = x->skip;
3925 if (mi_row + hbs < cm->mi_rows) {
3926 pc_tree->horizontal[1].pred_pixel_ready = 1;
3927 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col,
3928 &this_rdc, subsize, &pc_tree->horizontal[1]);
3929 pc_tree->horizontal[1].mic = *xd->mi[0];
3930 pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
3931 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3932 pc_tree->horizontal[1].skip = x->skip;
3933 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3934 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3935 rd_cost->rate += this_rdc.rate;
3936 rd_cost->dist += this_rdc.dist;
3940 case PARTITION_SPLIT:
3941 subsize = get_subsize(bsize, PARTITION_SPLIT);
3942 nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3943 subsize, output_enabled, rd_cost,
3945 nonrd_select_partition(cpi, td, tile_data, mi + hbs, tp, mi_row,
3946 mi_col + hbs, subsize, output_enabled, &this_rdc,
3948 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3949 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3950 rd_cost->rate += this_rdc.rate;
3951 rd_cost->dist += this_rdc.dist;
3953 nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis, tp,
3954 mi_row + hbs, mi_col, subsize, output_enabled,
3955 &this_rdc, pc_tree->split[2]);
3956 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3957 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3958 rd_cost->rate += this_rdc.rate;
3959 rd_cost->dist += this_rdc.dist;
3961 nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
3962 mi_row + hbs, mi_col + hbs, subsize,
3963 output_enabled, &this_rdc, pc_tree->split[3]);
3964 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3965 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3966 rd_cost->rate += this_rdc.rate;
3967 rd_cost->dist += this_rdc.dist;
3970 default: assert(0 && "Invalid partition type."); break;
3974 if (bsize == BLOCK_64X64 && output_enabled)
3975 encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, 1, bsize, pc_tree);
3978 static void nonrd_use_partition(VP9_COMP *cpi, ThreadData *td,
3979 TileDataEnc *tile_data, MODE_INFO **mi,
3980 TOKENEXTRA **tp, int mi_row, int mi_col,
3981 BLOCK_SIZE bsize, int output_enabled,
3982 RD_COST *dummy_cost, PC_TREE *pc_tree) {
3983 VP9_COMMON *const cm = &cpi->common;
3984 TileInfo *tile_info = &tile_data->tile_info;
3985 MACROBLOCK *const x = &td->mb;
3986 MACROBLOCKD *const xd = &x->e_mbd;
3987 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3988 const int mis = cm->mi_stride;
3989 PARTITION_TYPE partition;
3992 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
3994 subsize = (bsize >= BLOCK_8X8) ? mi[0]->sb_type : BLOCK_4X4;
3995 partition = partition_lookup[bsl][subsize];
3997 if (output_enabled && bsize != BLOCK_4X4) {
3998 int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
3999 td->counts->partition[ctx][partition]++;
4002 switch (partition) {
4003 case PARTITION_NONE:
4004 pc_tree->none.pred_pixel_ready = 1;
4005 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4006 subsize, &pc_tree->none);
4007 pc_tree->none.mic = *xd->mi[0];
4008 pc_tree->none.mbmi_ext = *x->mbmi_ext;
4009 pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
4010 pc_tree->none.skip = x->skip;
4011 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4012 subsize, &pc_tree->none);
4014 case PARTITION_VERT:
4015 pc_tree->vertical[0].pred_pixel_ready = 1;
4016 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4017 subsize, &pc_tree->vertical[0]);
4018 pc_tree->vertical[0].mic = *xd->mi[0];
4019 pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
4020 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
4021 pc_tree->vertical[0].skip = x->skip;
4022 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4023 subsize, &pc_tree->vertical[0]);
4024 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
4025 pc_tree->vertical[1].pred_pixel_ready = 1;
4026 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs, dummy_cost,
4027 subsize, &pc_tree->vertical[1]);
4028 pc_tree->vertical[1].mic = *xd->mi[0];
4029 pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
4030 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
4031 pc_tree->vertical[1].skip = x->skip;
4032 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col + hbs,
4033 output_enabled, subsize, &pc_tree->vertical[1]);
4036 case PARTITION_HORZ:
4037 pc_tree->horizontal[0].pred_pixel_ready = 1;
4038 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4039 subsize, &pc_tree->horizontal[0]);
4040 pc_tree->horizontal[0].mic = *xd->mi[0];
4041 pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
4042 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
4043 pc_tree->horizontal[0].skip = x->skip;
4044 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4045 subsize, &pc_tree->horizontal[0]);
4047 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
4048 pc_tree->horizontal[1].pred_pixel_ready = 1;
4049 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col, dummy_cost,
4050 subsize, &pc_tree->horizontal[1]);
4051 pc_tree->horizontal[1].mic = *xd->mi[0];
4052 pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
4053 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
4054 pc_tree->horizontal[1].skip = x->skip;
4055 encode_b_rt(cpi, td, tile_info, tp, mi_row + hbs, mi_col,
4056 output_enabled, subsize, &pc_tree->horizontal[1]);
4059 case PARTITION_SPLIT:
4060 subsize = get_subsize(bsize, PARTITION_SPLIT);
4061 if (bsize == BLOCK_8X8) {
4062 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4063 subsize, pc_tree->leaf_split[0]);
4064 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4065 subsize, pc_tree->leaf_split[0]);
4067 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, subsize,
4068 output_enabled, dummy_cost, pc_tree->split[0]);
4069 nonrd_use_partition(cpi, td, tile_data, mi + hbs, tp, mi_row,
4070 mi_col + hbs, subsize, output_enabled, dummy_cost,
4072 nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis, tp,
4073 mi_row + hbs, mi_col, subsize, output_enabled,
4074 dummy_cost, pc_tree->split[2]);
4075 nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
4076 mi_row + hbs, mi_col + hbs, subsize, output_enabled,
4077 dummy_cost, pc_tree->split[3]);
4080 default: assert(0 && "Invalid partition type."); break;
4083 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
4084 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
4087 static void encode_nonrd_sb_row(VP9_COMP *cpi, ThreadData *td,
4088 TileDataEnc *tile_data, int mi_row,
4090 SPEED_FEATURES *const sf = &cpi->sf;
4091 VP9_COMMON *const cm = &cpi->common;
4092 TileInfo *const tile_info = &tile_data->tile_info;
4093 MACROBLOCK *const x = &td->mb;
4094 MACROBLOCKD *const xd = &x->e_mbd;
4095 const int mi_col_start = tile_info->mi_col_start;
4096 const int mi_col_end = tile_info->mi_col_end;
4098 const int sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
4099 const int num_sb_cols =
4100 get_num_cols(tile_data->tile_info, MI_BLOCK_SIZE_LOG2);
4103 // Initialize the left context for the new SB row
4104 memset(&xd->left_context, 0, sizeof(xd->left_context));
4105 memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
4107 // Code each SB in the row
4108 for (mi_col = mi_col_start, sb_col_in_tile = 0; mi_col < mi_col_end;
4109 mi_col += MI_BLOCK_SIZE, ++sb_col_in_tile) {
4110 const struct segmentation *const seg = &cm->seg;
4112 const int idx_str = cm->mi_stride * mi_row + mi_col;
4113 MODE_INFO **mi = cm->mi_grid_visible + idx_str;
4114 PARTITION_SEARCH_TYPE partition_search_type = sf->partition_search_type;
4115 BLOCK_SIZE bsize = BLOCK_64X64;
4118 (*(cpi->row_mt_sync_read_ptr))(&tile_data->row_mt_sync, sb_row,
4121 x->source_variance = UINT_MAX;
4122 vp9_zero(x->pred_mv);
4123 vp9_rd_cost_init(&dummy_rdc);
4124 x->color_sensitivity[0] = 0;
4125 x->color_sensitivity[1] = 0;
4127 x->skip_low_source_sad = 0;
4128 x->lowvar_highsumdiff = 0;
4129 x->content_state_sb = 0;
4132 const uint8_t *const map =
4133 seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
4134 int segment_id = get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
4135 seg_skip = segfeature_active(seg, segment_id, SEG_LVL_SKIP);
4137 partition_search_type = FIXED_PARTITION;
4141 if (cpi->compute_source_sad_onepass && cpi->sf.use_source_sad) {
4142 int shift = cpi->Source->y_stride * (mi_row << 3) + (mi_col << 3);
4143 int sb_offset2 = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
4144 avg_source_sad(cpi, x, shift, sb_offset2);
4147 // Set the partition type of the 64X64 block
4148 switch (partition_search_type) {
4149 case VAR_BASED_PARTITION:
4150 // TODO(jingning, marpan): The mode decision and encoding process
4151 // support both intra and inter sub8x8 block coding for RTC mode.
4152 // Tune the thresholds accordingly to use sub8x8 block coding for
4153 // coding performance improvement.
4154 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
4155 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4156 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4158 case SOURCE_VAR_BASED_PARTITION:
4159 set_source_var_based_partition(cpi, tile_info, x, mi, mi_row, mi_col);
4160 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4161 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4163 case FIXED_PARTITION:
4164 if (!seg_skip) bsize = sf->always_this_block_size;
4165 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
4166 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4167 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4169 case REFERENCE_PARTITION:
4170 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
4171 // Use nonrd_pick_partition on scene-cut for VBR mode.
4172 // nonrd_pick_partition does not support 4x4 partition, so avoid it
4173 // on key frame for now.
4174 if ((cpi->oxcf.rc_mode == VPX_VBR && cpi->rc.high_source_sad &&
4175 cm->frame_type != KEY_FRAME)) {
4176 // Use lower max_partition_size for low resoultions.
4177 if (cm->width <= 352 && cm->height <= 288)
4178 x->max_partition_size = BLOCK_32X32;
4180 x->max_partition_size = BLOCK_64X64;
4181 x->min_partition_size = BLOCK_8X8;
4182 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col,
4183 BLOCK_64X64, &dummy_rdc, 1, INT64_MAX,
4186 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
4187 // TODO(marpan): Seems like nonrd_select_partition does not support
4188 // 4x4 partition. Since 4x4 is used on key frame, use this switch
4190 if (cm->frame_type == KEY_FRAME)
4191 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4192 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4194 nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4195 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4199 default: assert(0); break;
4202 (*(cpi->row_mt_sync_write_ptr))(&tile_data->row_mt_sync, sb_row,
4203 sb_col_in_tile, num_sb_cols);
4206 // end RTC play code
4208 static int set_var_thresh_from_histogram(VP9_COMP *cpi) {
4209 const SPEED_FEATURES *const sf = &cpi->sf;
4210 const VP9_COMMON *const cm = &cpi->common;
4212 const uint8_t *src = cpi->Source->y_buffer;
4213 const uint8_t *last_src = cpi->Last_Source->y_buffer;
4214 const int src_stride = cpi->Source->y_stride;
4215 const int last_stride = cpi->Last_Source->y_stride;
4217 // Pick cutoff threshold
4218 const int cutoff = (VPXMIN(cm->width, cm->height) >= 720)
4219 ? (cm->MBs * VAR_HIST_LARGE_CUT_OFF / 100)
4220 : (cm->MBs * VAR_HIST_SMALL_CUT_OFF / 100);
4221 DECLARE_ALIGNED(16, int, hist[VAR_HIST_BINS]);
4222 diff *var16 = cpi->source_diff_var;
4227 memset(hist, 0, VAR_HIST_BINS * sizeof(hist[0]));
4229 for (i = 0; i < cm->mb_rows; i++) {
4230 for (j = 0; j < cm->mb_cols; j++) {
4231 #if CONFIG_VP9_HIGHBITDEPTH
4232 if (cm->use_highbitdepth) {
4233 switch (cm->bit_depth) {
4235 vpx_highbd_8_get16x16var(src, src_stride, last_src, last_stride,
4236 &var16->sse, &var16->sum);
4239 vpx_highbd_10_get16x16var(src, src_stride, last_src, last_stride,
4240 &var16->sse, &var16->sum);
4243 vpx_highbd_12_get16x16var(src, src_stride, last_src, last_stride,
4244 &var16->sse, &var16->sum);
4248 "cm->bit_depth should be VPX_BITS_8, VPX_BITS_10"
4253 vpx_get16x16var(src, src_stride, last_src, last_stride, &var16->sse,
4257 vpx_get16x16var(src, src_stride, last_src, last_stride, &var16->sse,
4259 #endif // CONFIG_VP9_HIGHBITDEPTH
4260 var16->var = var16->sse - (((uint32_t)var16->sum * var16->sum) >> 8);
4262 if (var16->var >= VAR_HIST_MAX_BG_VAR)
4263 hist[VAR_HIST_BINS - 1]++;
4265 hist[var16->var / VAR_HIST_FACTOR]++;
4272 src = src - cm->mb_cols * 16 + 16 * src_stride;
4273 last_src = last_src - cm->mb_cols * 16 + 16 * last_stride;
4276 cpi->source_var_thresh = 0;
4278 if (hist[VAR_HIST_BINS - 1] < cutoff) {
4279 for (i = 0; i < VAR_HIST_BINS - 1; i++) {
4283 cpi->source_var_thresh = (i + 1) * VAR_HIST_FACTOR;
4289 return sf->search_type_check_frequency;
4292 static void source_var_based_partition_search_method(VP9_COMP *cpi) {
4293 VP9_COMMON *const cm = &cpi->common;
4294 SPEED_FEATURES *const sf = &cpi->sf;
4296 if (cm->frame_type == KEY_FRAME) {
4297 // For key frame, use SEARCH_PARTITION.
4298 sf->partition_search_type = SEARCH_PARTITION;
4299 } else if (cm->intra_only) {
4300 sf->partition_search_type = FIXED_PARTITION;
4302 if (cm->last_width != cm->width || cm->last_height != cm->height) {
4303 if (cpi->source_diff_var) vpx_free(cpi->source_diff_var);
4305 CHECK_MEM_ERROR(cm, cpi->source_diff_var,
4306 vpx_calloc(cm->MBs, sizeof(diff)));
4309 if (!cpi->frames_till_next_var_check)
4310 cpi->frames_till_next_var_check = set_var_thresh_from_histogram(cpi);
4312 if (cpi->frames_till_next_var_check > 0) {
4313 sf->partition_search_type = FIXED_PARTITION;
4314 cpi->frames_till_next_var_check--;
4319 static int get_skip_encode_frame(const VP9_COMMON *cm, ThreadData *const td) {
4320 unsigned int intra_count = 0, inter_count = 0;
4323 for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
4324 intra_count += td->counts->intra_inter[j][0];
4325 inter_count += td->counts->intra_inter[j][1];
4328 return (intra_count << 2) < inter_count && cm->frame_type != KEY_FRAME &&
4332 void vp9_init_tile_data(VP9_COMP *cpi) {
4333 VP9_COMMON *const cm = &cpi->common;
4334 const int tile_cols = 1 << cm->log2_tile_cols;
4335 const int tile_rows = 1 << cm->log2_tile_rows;
4336 int tile_col, tile_row;
4337 TOKENEXTRA *pre_tok = cpi->tile_tok[0][0];
4338 TOKENLIST *tplist = cpi->tplist[0][0];
4340 int tplist_count = 0;
4342 if (cpi->tile_data == NULL || cpi->allocated_tiles < tile_cols * tile_rows) {
4343 if (cpi->tile_data != NULL) vpx_free(cpi->tile_data);
4344 CHECK_MEM_ERROR(cm, cpi->tile_data, vpx_malloc(tile_cols * tile_rows *
4345 sizeof(*cpi->tile_data)));
4346 cpi->allocated_tiles = tile_cols * tile_rows;
4348 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
4349 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
4350 TileDataEnc *tile_data =
4351 &cpi->tile_data[tile_row * tile_cols + tile_col];
4353 for (i = 0; i < BLOCK_SIZES; ++i) {
4354 for (j = 0; j < MAX_MODES; ++j) {
4355 tile_data->thresh_freq_fact[i][j] = RD_THRESH_INIT_FACT;
4356 tile_data->mode_map[i][j] = j;
4359 #if CONFIG_MULTITHREAD
4360 tile_data->enc_row_mt_mutex = NULL;
4361 tile_data->row_base_thresh_freq_fact = NULL;
4366 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
4367 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
4368 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
4369 TileInfo *tile_info = &this_tile->tile_info;
4370 vp9_tile_init(tile_info, cm, tile_row, tile_col);
4372 cpi->tile_tok[tile_row][tile_col] = pre_tok + tile_tok;
4373 pre_tok = cpi->tile_tok[tile_row][tile_col];
4374 tile_tok = allocated_tokens(*tile_info);
4376 cpi->tplist[tile_row][tile_col] = tplist + tplist_count;
4377 tplist = cpi->tplist[tile_row][tile_col];
4378 tplist_count = get_num_vert_units(*tile_info, MI_BLOCK_SIZE_LOG2);
4383 void vp9_encode_sb_row(VP9_COMP *cpi, ThreadData *td, int tile_row,
4384 int tile_col, int mi_row) {
4385 VP9_COMMON *const cm = &cpi->common;
4386 const int tile_cols = 1 << cm->log2_tile_cols;
4387 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
4388 const TileInfo *const tile_info = &this_tile->tile_info;
4389 TOKENEXTRA *tok = NULL;
4391 int tile_mb_cols = (tile_info->mi_col_end - tile_info->mi_col_start + 1) >> 1;
4393 tile_sb_row = mi_cols_aligned_to_sb(mi_row - tile_info->mi_row_start) >>
4395 get_start_tok(cpi, tile_row, tile_col, mi_row, &tok);
4396 cpi->tplist[tile_row][tile_col][tile_sb_row].start = tok;
4398 if (cpi->sf.use_nonrd_pick_mode)
4399 encode_nonrd_sb_row(cpi, td, this_tile, mi_row, &tok);
4401 encode_rd_sb_row(cpi, td, this_tile, mi_row, &tok);
4403 cpi->tplist[tile_row][tile_col][tile_sb_row].stop = tok;
4404 cpi->tplist[tile_row][tile_col][tile_sb_row].count =
4405 (unsigned int)(cpi->tplist[tile_row][tile_col][tile_sb_row].stop -
4406 cpi->tplist[tile_row][tile_col][tile_sb_row].start);
4407 assert(tok - cpi->tplist[tile_row][tile_col][tile_sb_row].start <=
4408 get_token_alloc(MI_BLOCK_SIZE >> 1, tile_mb_cols));
4413 void vp9_encode_tile(VP9_COMP *cpi, ThreadData *td, int tile_row,
4415 VP9_COMMON *const cm = &cpi->common;
4416 const int tile_cols = 1 << cm->log2_tile_cols;
4417 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
4418 const TileInfo *const tile_info = &this_tile->tile_info;
4419 const int mi_row_start = tile_info->mi_row_start;
4420 const int mi_row_end = tile_info->mi_row_end;
4423 for (mi_row = mi_row_start; mi_row < mi_row_end; mi_row += MI_BLOCK_SIZE)
4424 vp9_encode_sb_row(cpi, td, tile_row, tile_col, mi_row);
4427 static void encode_tiles(VP9_COMP *cpi) {
4428 VP9_COMMON *const cm = &cpi->common;
4429 const int tile_cols = 1 << cm->log2_tile_cols;
4430 const int tile_rows = 1 << cm->log2_tile_rows;
4431 int tile_col, tile_row;
4433 vp9_init_tile_data(cpi);
4435 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
4436 for (tile_col = 0; tile_col < tile_cols; ++tile_col)
4437 vp9_encode_tile(cpi, &cpi->td, tile_row, tile_col);
4440 #if CONFIG_FP_MB_STATS
4441 static int input_fpmb_stats(FIRSTPASS_MB_STATS *firstpass_mb_stats,
4442 VP9_COMMON *cm, uint8_t **this_frame_mb_stats) {
4443 uint8_t *mb_stats_in = firstpass_mb_stats->mb_stats_start +
4444 cm->current_video_frame * cm->MBs * sizeof(uint8_t);
4446 if (mb_stats_in > firstpass_mb_stats->mb_stats_end) return EOF;
4448 *this_frame_mb_stats = mb_stats_in;
4454 static void encode_frame_internal(VP9_COMP *cpi) {
4455 SPEED_FEATURES *const sf = &cpi->sf;
4456 ThreadData *const td = &cpi->td;
4457 MACROBLOCK *const x = &td->mb;
4458 VP9_COMMON *const cm = &cpi->common;
4459 MACROBLOCKD *const xd = &x->e_mbd;
4461 xd->mi = cm->mi_grid_visible;
4464 vp9_zero(*td->counts);
4465 vp9_zero(cpi->td.rd_counts);
4467 xd->lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 &&
4468 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4470 #if CONFIG_VP9_HIGHBITDEPTH
4471 if (cm->use_highbitdepth)
4472 x->fwd_txm4x4 = xd->lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
4474 x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4475 x->highbd_itxm_add =
4476 xd->lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
4478 x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4479 #endif // CONFIG_VP9_HIGHBITDEPTH
4480 x->itxm_add = xd->lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
4482 if (xd->lossless) x->optimize = 0;
4484 cm->tx_mode = select_tx_mode(cpi, xd);
4486 vp9_frame_init_quantizer(cpi);
4488 vp9_initialize_rd_consts(cpi);
4489 vp9_initialize_me_consts(cpi, x, cm->base_qindex);
4490 init_encode_frame_mb_context(cpi);
4491 cm->use_prev_frame_mvs =
4492 !cm->error_resilient_mode && cm->width == cm->last_width &&
4493 cm->height == cm->last_height && !cm->intra_only && cm->last_show_frame;
4494 // Special case: set prev_mi to NULL when the previous mode info
4495 // context cannot be used.
4497 cm->use_prev_frame_mvs ? cm->prev_mip + cm->mi_stride + 1 : NULL;
4499 x->quant_fp = cpi->sf.use_quant_fp;
4500 vp9_zero(x->skip_txfm);
4501 if (sf->use_nonrd_pick_mode) {
4502 // Initialize internal buffer pointers for rtc coding, where non-RD
4503 // mode decision is used and hence no buffer pointer swap needed.
4505 struct macroblock_plane *const p = x->plane;
4506 struct macroblockd_plane *const pd = xd->plane;
4507 PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
4509 for (i = 0; i < MAX_MB_PLANE; ++i) {
4510 p[i].coeff = ctx->coeff_pbuf[i][0];
4511 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
4512 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
4513 p[i].eobs = ctx->eobs_pbuf[i][0];
4515 vp9_zero(x->zcoeff_blk);
4517 if (cm->frame_type != KEY_FRAME && cpi->rc.frames_since_golden == 0 &&
4518 !(cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR) &&
4520 cpi->ref_frame_flags &= (~VP9_GOLD_FLAG);
4522 if (sf->partition_search_type == SOURCE_VAR_BASED_PARTITION)
4523 source_var_based_partition_search_method(cpi);
4527 struct vpx_usec_timer emr_timer;
4528 vpx_usec_timer_start(&emr_timer);
4530 #if CONFIG_FP_MB_STATS
4531 if (cpi->use_fp_mb_stats) {
4532 input_fpmb_stats(&cpi->twopass.firstpass_mb_stats, cm,
4533 &cpi->twopass.this_frame_mb_stats);
4538 cpi->row_mt_sync_read_ptr = vp9_row_mt_sync_read_dummy;
4539 cpi->row_mt_sync_write_ptr = vp9_row_mt_sync_write_dummy;
4540 // If allowed, encoding tiles in parallel with one thread handling one
4541 // tile when row based multi-threading is disabled.
4542 if (VPXMIN(cpi->oxcf.max_threads, 1 << cm->log2_tile_cols) > 1)
4543 vp9_encode_tiles_mt(cpi);
4547 cpi->row_mt_sync_read_ptr = vp9_row_mt_sync_read;
4548 cpi->row_mt_sync_write_ptr = vp9_row_mt_sync_write;
4549 vp9_encode_tiles_row_mt(cpi);
4552 vpx_usec_timer_mark(&emr_timer);
4553 cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
4556 sf->skip_encode_frame =
4557 sf->skip_encode_sb ? get_skip_encode_frame(cm, td) : 0;
4560 // Keep record of the total distortion this time around for future use
4561 cpi->last_frame_distortion = cpi->frame_distortion;
4565 static INTERP_FILTER get_interp_filter(
4566 const int64_t threshes[SWITCHABLE_FILTER_CONTEXTS], int is_alt_ref) {
4567 if (!is_alt_ref && threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP] &&
4568 threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP_SHARP] &&
4569 threshes[EIGHTTAP_SMOOTH] > threshes[SWITCHABLE - 1]) {
4570 return EIGHTTAP_SMOOTH;
4571 } else if (threshes[EIGHTTAP_SHARP] > threshes[EIGHTTAP] &&
4572 threshes[EIGHTTAP_SHARP] > threshes[SWITCHABLE - 1]) {
4573 return EIGHTTAP_SHARP;
4574 } else if (threshes[EIGHTTAP] > threshes[SWITCHABLE - 1]) {
4581 static int compute_frame_aq_offset(struct VP9_COMP *cpi) {
4582 VP9_COMMON *const cm = &cpi->common;
4583 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
4584 struct segmentation *const seg = &cm->seg;
4592 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
4593 MODE_INFO **mi_8x8 = mi_8x8_ptr;
4594 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++, mi_8x8++) {
4595 segment_id = mi_8x8[0]->segment_id;
4596 qdelta_index = get_segdata(seg, segment_id, SEG_LVL_ALT_Q);
4597 sum_delta += qdelta_index;
4600 mi_8x8_ptr += cm->mi_stride;
4603 return sum_delta / (cm->mi_rows * cm->mi_cols);
4606 void vp9_encode_frame(VP9_COMP *cpi) {
4607 VP9_COMMON *const cm = &cpi->common;
4609 // In the longer term the encoder should be generalized to match the
4610 // decoder such that we allow compound where one of the 3 buffers has a
4611 // different sign bias and that buffer is then the fixed ref. However, this
4612 // requires further work in the rd loop. For now the only supported encoder
4613 // side behavior is where the ALT ref buffer has opposite sign bias to
4615 if (!frame_is_intra_only(cm)) {
4616 if ((cm->ref_frame_sign_bias[ALTREF_FRAME] ==
4617 cm->ref_frame_sign_bias[GOLDEN_FRAME]) ||
4618 (cm->ref_frame_sign_bias[ALTREF_FRAME] ==
4619 cm->ref_frame_sign_bias[LAST_FRAME])) {
4620 cpi->allow_comp_inter_inter = 0;
4622 cpi->allow_comp_inter_inter = 1;
4623 cm->comp_fixed_ref = ALTREF_FRAME;
4624 cm->comp_var_ref[0] = LAST_FRAME;
4625 cm->comp_var_ref[1] = GOLDEN_FRAME;
4629 if (cpi->sf.frame_parameter_update) {
4631 RD_OPT *const rd_opt = &cpi->rd;
4632 FRAME_COUNTS *counts = cpi->td.counts;
4633 RD_COUNTS *const rdc = &cpi->td.rd_counts;
4635 // This code does a single RD pass over the whole frame assuming
4636 // either compound, single or hybrid prediction as per whatever has
4637 // worked best for that type of frame in the past.
4638 // It also predicts whether another coding mode would have worked
4639 // better than this coding mode. If that is the case, it remembers
4640 // that for subsequent frames.
4641 // It also does the same analysis for transform size selection.
4642 const MV_REFERENCE_FRAME frame_type = get_frame_type(cpi);
4643 int64_t *const mode_thrs = rd_opt->prediction_type_threshes[frame_type];
4644 int64_t *const filter_thrs = rd_opt->filter_threshes[frame_type];
4645 const int is_alt_ref = frame_type == ALTREF_FRAME;
4647 /* prediction (compound, single or hybrid) mode selection */
4648 if (is_alt_ref || !cpi->allow_comp_inter_inter)
4649 cm->reference_mode = SINGLE_REFERENCE;
4650 else if (mode_thrs[COMPOUND_REFERENCE] > mode_thrs[SINGLE_REFERENCE] &&
4651 mode_thrs[COMPOUND_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT] &&
4652 check_dual_ref_flags(cpi) && cpi->static_mb_pct == 100)
4653 cm->reference_mode = COMPOUND_REFERENCE;
4654 else if (mode_thrs[SINGLE_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT])
4655 cm->reference_mode = SINGLE_REFERENCE;
4657 cm->reference_mode = REFERENCE_MODE_SELECT;
4659 if (cm->interp_filter == SWITCHABLE)
4660 cm->interp_filter = get_interp_filter(filter_thrs, is_alt_ref);
4662 encode_frame_internal(cpi);
4664 for (i = 0; i < REFERENCE_MODES; ++i)
4665 mode_thrs[i] = (mode_thrs[i] + rdc->comp_pred_diff[i] / cm->MBs) / 2;
4667 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
4668 filter_thrs[i] = (filter_thrs[i] + rdc->filter_diff[i] / cm->MBs) / 2;
4670 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
4671 int single_count_zero = 0;
4672 int comp_count_zero = 0;
4674 for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
4675 single_count_zero += counts->comp_inter[i][0];
4676 comp_count_zero += counts->comp_inter[i][1];
4679 if (comp_count_zero == 0) {
4680 cm->reference_mode = SINGLE_REFERENCE;
4681 vp9_zero(counts->comp_inter);
4682 } else if (single_count_zero == 0) {
4683 cm->reference_mode = COMPOUND_REFERENCE;
4684 vp9_zero(counts->comp_inter);
4688 if (cm->tx_mode == TX_MODE_SELECT) {
4690 int count8x8_lp = 0, count8x8_8x8p = 0;
4691 int count16x16_16x16p = 0, count16x16_lp = 0;
4694 for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
4695 count4x4 += counts->tx.p32x32[i][TX_4X4];
4696 count4x4 += counts->tx.p16x16[i][TX_4X4];
4697 count4x4 += counts->tx.p8x8[i][TX_4X4];
4699 count8x8_lp += counts->tx.p32x32[i][TX_8X8];
4700 count8x8_lp += counts->tx.p16x16[i][TX_8X8];
4701 count8x8_8x8p += counts->tx.p8x8[i][TX_8X8];
4703 count16x16_16x16p += counts->tx.p16x16[i][TX_16X16];
4704 count16x16_lp += counts->tx.p32x32[i][TX_16X16];
4705 count32x32 += counts->tx.p32x32[i][TX_32X32];
4707 if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0 &&
4709 cm->tx_mode = ALLOW_8X8;
4710 reset_skip_tx_size(cm, TX_8X8);
4711 } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0 &&
4712 count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
4713 cm->tx_mode = ONLY_4X4;
4714 reset_skip_tx_size(cm, TX_4X4);
4715 } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
4716 cm->tx_mode = ALLOW_32X32;
4717 } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
4718 cm->tx_mode = ALLOW_16X16;
4719 reset_skip_tx_size(cm, TX_16X16);
4723 cm->reference_mode = SINGLE_REFERENCE;
4724 encode_frame_internal(cpi);
4727 // If segmented AQ is enabled compute the average AQ weighting.
4728 if (cm->seg.enabled && (cpi->oxcf.aq_mode != NO_AQ) &&
4729 (cm->seg.update_map || cm->seg.update_data)) {
4730 cm->seg.aq_av_offset = compute_frame_aq_offset(cpi);
4734 static void sum_intra_stats(FRAME_COUNTS *counts, const MODE_INFO *mi) {
4735 const PREDICTION_MODE y_mode = mi->mode;
4736 const PREDICTION_MODE uv_mode = mi->uv_mode;
4737 const BLOCK_SIZE bsize = mi->sb_type;
4739 if (bsize < BLOCK_8X8) {
4741 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
4742 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
4743 for (idy = 0; idy < 2; idy += num_4x4_h)
4744 for (idx = 0; idx < 2; idx += num_4x4_w)
4745 ++counts->y_mode[0][mi->bmi[idy * 2 + idx].as_mode];
4747 ++counts->y_mode[size_group_lookup[bsize]][y_mode];
4750 ++counts->uv_mode[y_mode][uv_mode];
4753 static void update_zeromv_cnt(VP9_COMP *const cpi, const MODE_INFO *const mi,
4754 int mi_row, int mi_col, BLOCK_SIZE bsize) {
4755 const VP9_COMMON *const cm = &cpi->common;
4756 MV mv = mi->mv[0].as_mv;
4757 const int bw = num_8x8_blocks_wide_lookup[bsize];
4758 const int bh = num_8x8_blocks_high_lookup[bsize];
4759 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
4760 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
4761 const int block_index = mi_row * cm->mi_cols + mi_col;
4763 for (y = 0; y < ymis; y++)
4764 for (x = 0; x < xmis; x++) {
4765 int map_offset = block_index + y * cm->mi_cols + x;
4766 if (is_inter_block(mi) && mi->segment_id <= CR_SEGMENT_ID_BOOST2) {
4767 if (abs(mv.row) < 8 && abs(mv.col) < 8) {
4768 if (cpi->consec_zero_mv[map_offset] < 255)
4769 cpi->consec_zero_mv[map_offset]++;
4771 cpi->consec_zero_mv[map_offset] = 0;
4777 static void encode_superblock(VP9_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
4778 int output_enabled, int mi_row, int mi_col,
4779 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
4780 VP9_COMMON *const cm = &cpi->common;
4781 MACROBLOCK *const x = &td->mb;
4782 MACROBLOCKD *const xd = &x->e_mbd;
4783 MODE_INFO *mi = xd->mi[0];
4784 const int seg_skip =
4785 segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP);
4786 x->skip_recode = !x->select_tx_size && mi->sb_type >= BLOCK_8X8 &&
4787 cpi->oxcf.aq_mode != COMPLEXITY_AQ &&
4788 cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ &&
4789 cpi->sf.allow_skip_recode;
4791 if (!x->skip_recode && !cpi->sf.use_nonrd_pick_mode)
4792 memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
4794 x->skip_optimize = ctx->is_coded;
4796 x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
4797 x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
4798 x->q_index < QIDX_SKIP_THRESH);
4800 if (x->skip_encode) return;
4802 if (!is_inter_block(mi)) {
4804 #if CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
4805 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) &&
4806 (xd->above_mi == NULL || xd->left_mi == NULL) &&
4807 need_top_left[mi->uv_mode])
4809 #endif // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
4811 for (plane = 0; plane < MAX_MB_PLANE; ++plane)
4812 vp9_encode_intra_block_plane(x, VPXMAX(bsize, BLOCK_8X8), plane, 1);
4813 if (output_enabled) sum_intra_stats(td->counts, mi);
4814 vp9_tokenize_sb(cpi, td, t, !output_enabled, seg_skip,
4815 VPXMAX(bsize, BLOCK_8X8));
4818 const int is_compound = has_second_ref(mi);
4819 set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
4820 for (ref = 0; ref < 1 + is_compound; ++ref) {
4821 YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, mi->ref_frame[ref]);
4822 assert(cfg != NULL);
4823 vp9_setup_pre_planes(xd, ref, cfg, mi_row, mi_col,
4824 &xd->block_refs[ref]->sf);
4826 if (!(cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready) || seg_skip)
4827 vp9_build_inter_predictors_sby(xd, mi_row, mi_col,
4828 VPXMAX(bsize, BLOCK_8X8));
4830 vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col,
4831 VPXMAX(bsize, BLOCK_8X8));
4833 vp9_encode_sb(x, VPXMAX(bsize, BLOCK_8X8));
4834 vp9_tokenize_sb(cpi, td, t, !output_enabled, seg_skip,
4835 VPXMAX(bsize, BLOCK_8X8));
4842 if (output_enabled) {
4843 if (cm->tx_mode == TX_MODE_SELECT && mi->sb_type >= BLOCK_8X8 &&
4844 !(is_inter_block(mi) && mi->skip)) {
4845 ++get_tx_counts(max_txsize_lookup[bsize], get_tx_size_context(xd),
4846 &td->counts->tx)[mi->tx_size];
4848 // The new intra coding scheme requires no change of transform size
4849 if (is_inter_block(mi)) {
4850 mi->tx_size = VPXMIN(tx_mode_to_biggest_tx_size[cm->tx_mode],
4851 max_txsize_lookup[bsize]);
4853 mi->tx_size = (bsize >= BLOCK_8X8) ? mi->tx_size : TX_4X4;
4857 ++td->counts->tx.tx_totals[mi->tx_size];
4858 ++td->counts->tx.tx_totals[get_uv_tx_size(mi, &xd->plane[1])];
4859 if (cm->seg.enabled && cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
4860 vp9_cyclic_refresh_update_sb_postencode(cpi, mi, mi_row, mi_col, bsize);
4861 if (cpi->oxcf.pass == 0 && cpi->svc.temporal_layer_id == 0)
4862 update_zeromv_cnt(cpi, mi, mi_row, mi_col, bsize);