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 return (5 * threshold_base) >> 2;
500 } else if (speed == 7) {
501 if ((content_state == kLowSadLowSumdiff) ||
502 (content_state == kHighSadLowSumdiff)) {
503 return (5 * threshold_base) >> 2;
506 return threshold_base;
509 // Set the variance split thresholds for following the block sizes:
510 // 0 - threshold_64x64, 1 - threshold_32x32, 2 - threshold_16x16,
511 // 3 - vbp_threshold_8x8. vbp_threshold_8x8 (to split to 4x4 partition) is
512 // currently only used on key frame.
513 static void set_vbp_thresholds(VP9_COMP *cpi, int64_t thresholds[], int q,
515 VP9_COMMON *const cm = &cpi->common;
516 const int is_key_frame = (cm->frame_type == KEY_FRAME);
517 const int threshold_multiplier = is_key_frame ? 20 : 1;
518 int64_t threshold_base =
519 (int64_t)(threshold_multiplier * cpi->y_dequant[q][1]);
522 thresholds[0] = threshold_base;
523 thresholds[1] = threshold_base >> 2;
524 thresholds[2] = threshold_base >> 2;
525 thresholds[3] = threshold_base << 2;
527 // Increase base variance threshold based on estimated noise level.
528 if (cpi->noise_estimate.enabled && cm->width >= 640 && cm->height >= 480) {
529 NOISE_LEVEL noise_level =
530 vp9_noise_estimate_extract_level(&cpi->noise_estimate);
531 if (noise_level == kHigh)
532 threshold_base = 3 * threshold_base;
533 else if (noise_level == kMedium)
534 threshold_base = threshold_base << 1;
535 else if (noise_level < kLow)
536 threshold_base = (7 * threshold_base) >> 3;
538 #if CONFIG_VP9_TEMPORAL_DENOISING
539 if (cpi->oxcf.noise_sensitivity > 0 && cpi->oxcf.speed > 5 &&
540 cpi->denoiser.denoising_level >= kDenLow)
541 threshold_base = vp9_scale_part_thresh(
542 threshold_base, cpi->denoiser.denoising_level, content_state);
545 scale_part_thresh_sumdiff(threshold_base, cpi->oxcf.speed, cm->width,
546 cm->height, content_state);
548 // Increase base variance threshold based on content_state/sum_diff level.
549 threshold_base = scale_part_thresh_sumdiff(
550 threshold_base, cpi->oxcf.speed, cm->width, cm->height, content_state);
552 thresholds[0] = threshold_base;
553 thresholds[2] = threshold_base << cpi->oxcf.speed;
554 if (cm->width <= 352 && cm->height <= 288) {
555 thresholds[0] = threshold_base >> 3;
556 thresholds[1] = threshold_base >> 1;
557 thresholds[2] = threshold_base << 3;
558 } else if (cm->width < 1280 && cm->height < 720) {
559 thresholds[1] = (5 * threshold_base) >> 2;
560 } else if (cm->width < 1920 && cm->height < 1080) {
561 thresholds[1] = threshold_base << 1;
563 thresholds[1] = (5 * threshold_base) >> 1;
568 void vp9_set_variance_partition_thresholds(VP9_COMP *cpi, int q,
570 VP9_COMMON *const cm = &cpi->common;
571 SPEED_FEATURES *const sf = &cpi->sf;
572 const int is_key_frame = (cm->frame_type == KEY_FRAME);
573 if (sf->partition_search_type != VAR_BASED_PARTITION &&
574 sf->partition_search_type != REFERENCE_PARTITION) {
577 set_vbp_thresholds(cpi, cpi->vbp_thresholds, q, content_state);
578 // The thresholds below are not changed locally.
580 cpi->vbp_threshold_sad = 0;
581 cpi->vbp_threshold_copy = 0;
582 cpi->vbp_bsize_min = BLOCK_8X8;
584 if (cm->width <= 352 && cm->height <= 288)
585 cpi->vbp_threshold_sad = 10;
587 cpi->vbp_threshold_sad = (cpi->y_dequant[q][1] << 1) > 1000
588 ? (cpi->y_dequant[q][1] << 1)
590 cpi->vbp_bsize_min = BLOCK_16X16;
591 if (cm->width <= 352 && cm->height <= 288)
592 cpi->vbp_threshold_copy = 4000;
593 else if (cm->width <= 640 && cm->height <= 360)
594 cpi->vbp_threshold_copy = 8000;
596 cpi->vbp_threshold_copy = (cpi->y_dequant[q][1] << 3) > 8000
597 ? (cpi->y_dequant[q][1] << 3)
600 cpi->vbp_threshold_minmax = 15 + (q >> 3);
604 // Compute the minmax over the 8x8 subblocks.
605 static int compute_minmax_8x8(const uint8_t *s, int sp, const uint8_t *d,
606 int dp, int x16_idx, int y16_idx,
607 #if CONFIG_VP9_HIGHBITDEPTH
610 int pixels_wide, int pixels_high) {
613 int minmax_min = 255;
614 // Loop over the 4 8x8 subblocks.
615 for (k = 0; k < 4; k++) {
616 int x8_idx = x16_idx + ((k & 1) << 3);
617 int y8_idx = y16_idx + ((k >> 1) << 3);
620 if (x8_idx < pixels_wide && y8_idx < pixels_high) {
621 #if CONFIG_VP9_HIGHBITDEPTH
622 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
623 vpx_highbd_minmax_8x8(s + y8_idx * sp + x8_idx, sp,
624 d + y8_idx * dp + x8_idx, dp, &min, &max);
626 vpx_minmax_8x8(s + y8_idx * sp + x8_idx, sp, d + y8_idx * dp + x8_idx,
630 vpx_minmax_8x8(s + y8_idx * sp + x8_idx, sp, d + y8_idx * dp + x8_idx, dp,
633 if ((max - min) > minmax_max) minmax_max = (max - min);
634 if ((max - min) < minmax_min) minmax_min = (max - min);
637 return (minmax_max - minmax_min);
640 static void fill_variance_4x4avg(const uint8_t *s, int sp, const uint8_t *d,
641 int dp, int x8_idx, int y8_idx, v8x8 *vst,
642 #if CONFIG_VP9_HIGHBITDEPTH
645 int pixels_wide, int pixels_high,
648 for (k = 0; k < 4; k++) {
649 int x4_idx = x8_idx + ((k & 1) << 2);
650 int y4_idx = y8_idx + ((k >> 1) << 2);
651 unsigned int sse = 0;
653 if (x4_idx < pixels_wide && y4_idx < pixels_high) {
656 #if CONFIG_VP9_HIGHBITDEPTH
657 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
658 s_avg = vpx_highbd_avg_4x4(s + y4_idx * sp + x4_idx, sp);
660 d_avg = vpx_highbd_avg_4x4(d + y4_idx * dp + x4_idx, dp);
662 s_avg = vpx_avg_4x4(s + y4_idx * sp + x4_idx, sp);
663 if (!is_key_frame) d_avg = vpx_avg_4x4(d + y4_idx * dp + x4_idx, dp);
666 s_avg = vpx_avg_4x4(s + y4_idx * sp + x4_idx, sp);
667 if (!is_key_frame) d_avg = vpx_avg_4x4(d + y4_idx * dp + x4_idx, dp);
672 fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
676 static void fill_variance_8x8avg(const uint8_t *s, int sp, const uint8_t *d,
677 int dp, int x16_idx, int y16_idx, v16x16 *vst,
678 #if CONFIG_VP9_HIGHBITDEPTH
681 int pixels_wide, int pixels_high,
684 for (k = 0; k < 4; k++) {
685 int x8_idx = x16_idx + ((k & 1) << 3);
686 int y8_idx = y16_idx + ((k >> 1) << 3);
687 unsigned int sse = 0;
689 if (x8_idx < pixels_wide && y8_idx < pixels_high) {
692 #if CONFIG_VP9_HIGHBITDEPTH
693 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
694 s_avg = vpx_highbd_avg_8x8(s + y8_idx * sp + x8_idx, sp);
696 d_avg = vpx_highbd_avg_8x8(d + y8_idx * dp + x8_idx, dp);
698 s_avg = vpx_avg_8x8(s + y8_idx * sp + x8_idx, sp);
699 if (!is_key_frame) d_avg = vpx_avg_8x8(d + y8_idx * dp + x8_idx, dp);
702 s_avg = vpx_avg_8x8(s + y8_idx * sp + x8_idx, sp);
703 if (!is_key_frame) d_avg = vpx_avg_8x8(d + y8_idx * dp + x8_idx, dp);
708 fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
712 // Check if most of the superblock is skin content, and if so, force split to
713 // 32x32, and set x->sb_is_skin for use in mode selection.
714 static int skin_sb_split(VP9_COMP *cpi, MACROBLOCK *x, const int low_res,
715 int mi_row, int mi_col, int *force_split) {
716 VP9_COMMON *const cm = &cpi->common;
717 #if CONFIG_VP9_HIGHBITDEPTH
718 if (cm->use_highbitdepth) return 0;
720 // Avoid checking superblocks on/near boundary and avoid low resolutions.
721 // Note superblock may still pick 64X64 if y_sad is very small
722 // (i.e., y_sad < cpi->vbp_threshold_sad) below. For now leave this as is.
723 if (!low_res && (mi_col >= 8 && mi_col + 8 < cm->mi_cols && mi_row >= 8 &&
724 mi_row + 8 < cm->mi_rows)) {
725 int num_16x16_skin = 0;
726 int num_16x16_nonskin = 0;
727 uint8_t *ysignal = x->plane[0].src.buf;
728 uint8_t *usignal = x->plane[1].src.buf;
729 uint8_t *vsignal = x->plane[2].src.buf;
730 int sp = x->plane[0].src.stride;
731 int spuv = x->plane[1].src.stride;
732 const int block_index = mi_row * cm->mi_cols + mi_col;
733 const int bw = num_8x8_blocks_wide_lookup[BLOCK_64X64];
734 const int bh = num_8x8_blocks_high_lookup[BLOCK_64X64];
735 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
736 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
737 // Loop through the 16x16 sub-blocks.
739 for (i = 0; i < ymis; i += 2) {
740 for (j = 0; j < xmis; j += 2) {
741 int bl_index = block_index + i * cm->mi_cols + j;
742 int bl_index1 = bl_index + 1;
743 int bl_index2 = bl_index + cm->mi_cols;
744 int bl_index3 = bl_index2 + 1;
746 VPXMIN(cpi->consec_zero_mv[bl_index],
747 VPXMIN(cpi->consec_zero_mv[bl_index1],
748 VPXMIN(cpi->consec_zero_mv[bl_index2],
749 cpi->consec_zero_mv[bl_index3])));
750 int is_skin = vp9_compute_skin_block(
751 ysignal, usignal, vsignal, sp, spuv, BLOCK_16X16, consec_zeromv, 0);
752 num_16x16_skin += is_skin;
753 num_16x16_nonskin += (1 - is_skin);
754 if (num_16x16_nonskin > 3) {
755 // Exit loop if at least 4 of the 16x16 blocks are not skin.
763 ysignal += (sp << 4) - 64;
764 usignal += (spuv << 3) - 32;
765 vsignal += (spuv << 3) - 32;
767 if (num_16x16_skin > 12) {
775 static void set_low_temp_var_flag(VP9_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
776 v64x64 *vt, int64_t thresholds[],
777 MV_REFERENCE_FRAME ref_frame_partition,
778 int mi_col, int mi_row) {
780 VP9_COMMON *const cm = &cpi->common;
781 const int mv_thr = cm->width > 640 ? 8 : 4;
782 // Check temporal variance for bsize >= 16x16, if LAST_FRAME was selected and
783 // int_pro mv is small. If the temporal variance is small set the flag
784 // variance_low for the block. The variance threshold can be adjusted, the
785 // higher the more aggressive.
786 if (ref_frame_partition == LAST_FRAME &&
787 (cpi->sf.short_circuit_low_temp_var == 1 ||
788 (xd->mi[0]->mv[0].as_mv.col < mv_thr &&
789 xd->mi[0]->mv[0].as_mv.col > -mv_thr &&
790 xd->mi[0]->mv[0].as_mv.row < mv_thr &&
791 xd->mi[0]->mv[0].as_mv.row > -mv_thr))) {
792 if (xd->mi[0]->sb_type == BLOCK_64X64) {
793 if ((vt->part_variances).none.variance < (thresholds[0] >> 1))
794 x->variance_low[0] = 1;
795 } else if (xd->mi[0]->sb_type == BLOCK_64X32) {
796 for (i = 0; i < 2; i++) {
797 if (vt->part_variances.horz[i].variance < (thresholds[0] >> 2))
798 x->variance_low[i + 1] = 1;
800 } else if (xd->mi[0]->sb_type == BLOCK_32X64) {
801 for (i = 0; i < 2; i++) {
802 if (vt->part_variances.vert[i].variance < (thresholds[0] >> 2))
803 x->variance_low[i + 3] = 1;
806 for (i = 0; i < 4; i++) {
807 const int idx[4][2] = { { 0, 0 }, { 0, 4 }, { 4, 0 }, { 4, 4 } };
809 cm->mi_stride * (mi_row + idx[i][0]) + mi_col + idx[i][1];
810 MODE_INFO **this_mi = cm->mi_grid_visible + idx_str;
812 if (cm->mi_cols <= mi_col + idx[i][1] ||
813 cm->mi_rows <= mi_row + idx[i][0])
816 if ((*this_mi)->sb_type == BLOCK_32X32) {
817 int64_t threshold_32x32 = (cpi->sf.short_circuit_low_temp_var == 1 ||
818 cpi->sf.short_circuit_low_temp_var == 3)
819 ? ((5 * thresholds[1]) >> 3)
820 : (thresholds[1] >> 1);
821 if (vt->split[i].part_variances.none.variance < threshold_32x32)
822 x->variance_low[i + 5] = 1;
823 } else if (cpi->sf.short_circuit_low_temp_var >= 2) {
824 // For 32x16 and 16x32 blocks, the flag is set on each 16x16 block
826 if ((*this_mi)->sb_type == BLOCK_16X16 ||
827 (*this_mi)->sb_type == BLOCK_32X16 ||
828 (*this_mi)->sb_type == BLOCK_16X32) {
829 for (j = 0; j < 4; j++) {
830 if (vt->split[i].split[j].part_variances.none.variance <
831 (thresholds[2] >> 8))
832 x->variance_low[(i << 2) + j + 9] = 1;
841 static void copy_partitioning_helper(VP9_COMP *cpi, BLOCK_SIZE bsize,
842 int mi_row, int mi_col) {
843 VP9_COMMON *const cm = &cpi->common;
844 BLOCK_SIZE *prev_part = cpi->prev_partition;
845 int start_pos = mi_row * cm->mi_stride + mi_col;
847 const int bsl = b_width_log2_lookup[bsize];
848 const int bs = (1 << bsl) / 4;
850 PARTITION_TYPE partition;
851 MODE_INFO *mi = NULL;
853 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
855 partition = partition_lookup[bsl][prev_part[start_pos]];
856 subsize = get_subsize(bsize, partition);
857 mi = cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col];
859 if (subsize < BLOCK_8X8) {
863 case PARTITION_NONE: mi->sb_type = bsize; break;
865 mi->sb_type = subsize;
866 if (mi_row + bs < cm->mi_rows)
867 cm->mi_grid_visible[(mi_row + bs) * cm->mi_stride + mi_col]->sb_type =
871 mi->sb_type = subsize;
872 if (mi_col + bs < cm->mi_cols)
873 cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col + bs]->sb_type =
876 case PARTITION_SPLIT:
877 copy_partitioning_helper(cpi, subsize, mi_row, mi_col);
878 copy_partitioning_helper(cpi, subsize, mi_row + bs, mi_col);
879 copy_partitioning_helper(cpi, subsize, mi_row, mi_col + bs);
880 copy_partitioning_helper(cpi, subsize, mi_row + bs, mi_col + bs);
887 static int copy_partitioning(VP9_COMP *cpi, MACROBLOCK *x, int mi_row,
888 int mi_col, int segment_id, int sb_offset) {
889 if (cpi->rc.frames_since_key > 1 && segment_id == CR_SEGMENT_ID_BASE &&
890 cpi->prev_segment_id[sb_offset] == CR_SEGMENT_ID_BASE &&
891 cpi->copied_frame_cnt[sb_offset] < cpi->max_copied_frame) {
892 if (cpi->prev_partition != NULL) {
893 copy_partitioning_helper(cpi, BLOCK_64X64, mi_row, mi_col);
894 cpi->copied_frame_cnt[sb_offset] += 1;
895 memcpy(x->variance_low, &(cpi->prev_variance_low[sb_offset * 25]),
896 sizeof(x->variance_low));
904 static void update_prev_partition(VP9_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
906 VP9_COMMON *const cm = &cpi->common;
907 BLOCK_SIZE *prev_part = cpi->prev_partition;
908 int start_pos = mi_row * cm->mi_stride + mi_col;
909 const int bsl = b_width_log2_lookup[bsize];
910 const int bs = (1 << bsl) / 4;
912 PARTITION_TYPE partition;
913 const MODE_INFO *mi = NULL;
915 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
917 mi = cm->mi_grid_visible[start_pos];
918 partition = partition_lookup[bsl][mi->sb_type];
919 subsize = get_subsize(bsize, partition);
920 if (subsize < BLOCK_8X8) {
921 prev_part[start_pos] = bsize;
924 case PARTITION_NONE: prev_part[start_pos] = bsize; break;
926 prev_part[start_pos] = subsize;
927 if (mi_row + bs < cm->mi_rows)
928 prev_part[start_pos + bs * cm->mi_stride] = subsize;
931 prev_part[start_pos] = subsize;
932 if (mi_col + bs < cm->mi_cols) prev_part[start_pos + bs] = subsize;
934 case PARTITION_SPLIT:
935 update_prev_partition(cpi, subsize, mi_row, mi_col);
936 update_prev_partition(cpi, subsize, mi_row + bs, mi_col);
937 update_prev_partition(cpi, subsize, mi_row, mi_col + bs);
938 update_prev_partition(cpi, subsize, mi_row + bs, mi_col + bs);
945 static void chroma_check(VP9_COMP *cpi, MACROBLOCK *x, int bsize,
946 unsigned int y_sad, int is_key_frame) {
948 MACROBLOCKD *xd = &x->e_mbd;
949 // For speed >= 8, avoid the chroma check if y_sad is above threshold.
950 if (is_key_frame || (cpi->oxcf.speed >= 8 && y_sad > cpi->vbp_thresholds[1]))
953 for (i = 1; i <= 2; ++i) {
954 unsigned int uv_sad = UINT_MAX;
955 struct macroblock_plane *p = &x->plane[i];
956 struct macroblockd_plane *pd = &xd->plane[i];
957 const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
959 if (bs != BLOCK_INVALID)
960 uv_sad = cpi->fn_ptr[bs].sdf(p->src.buf, p->src.stride, pd->dst.buf,
963 // TODO(marpan): Investigate if we should lower this threshold if
964 // superblock is detected as skin.
965 x->color_sensitivity[i - 1] = uv_sad > (y_sad >> 2);
969 static void avg_source_sad(VP9_COMP *cpi, MACROBLOCK *x, int shift,
971 unsigned int tmp_sse;
973 unsigned int tmp_variance;
974 const BLOCK_SIZE bsize = BLOCK_64X64;
975 uint8_t *src_y = cpi->Source->y_buffer;
976 int src_ystride = cpi->Source->y_stride;
977 uint8_t *last_src_y = cpi->Last_Source->y_buffer;
978 int last_src_ystride = cpi->Last_Source->y_stride;
979 uint64_t avg_source_sad_threshold = 10000;
980 uint64_t avg_source_sad_threshold2 = 12000;
981 #if CONFIG_VP9_HIGHBITDEPTH
982 if (cpi->common.use_highbitdepth) return;
987 cpi->fn_ptr[bsize].sdf(src_y, src_ystride, last_src_y, last_src_ystride);
988 tmp_variance = vpx_variance64x64(src_y, src_ystride, last_src_y,
989 last_src_ystride, &tmp_sse);
990 // Note: tmp_sse - tmp_variance = ((sum * sum) >> 12)
991 if (tmp_sad < avg_source_sad_threshold)
992 x->content_state_sb = ((tmp_sse - tmp_variance) < 25) ? kLowSadLowSumdiff
993 : kLowSadHighSumdiff;
995 x->content_state_sb = ((tmp_sse - tmp_variance) < 25) ? kHighSadLowSumdiff
996 : kHighSadHighSumdiff;
997 if (cpi->content_state_sb_fd != NULL) {
998 if (tmp_sad < avg_source_sad_threshold2) {
999 // Cap the increment to 255.
1000 if (cpi->content_state_sb_fd[sb_offset] < 255)
1001 cpi->content_state_sb_fd[sb_offset]++;
1003 cpi->content_state_sb_fd[sb_offset] = 0;
1009 // This function chooses partitioning based on the variance between source and
1010 // reconstructed last, where variance is computed for down-sampled inputs.
1011 static int choose_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
1012 MACROBLOCK *x, int mi_row, int mi_col) {
1013 VP9_COMMON *const cm = &cpi->common;
1014 MACROBLOCKD *xd = &x->e_mbd;
1018 int force_split[21];
1020 int max_var_32x32 = 0;
1021 int min_var_32x32 = INT_MAX;
1024 int maxvar_16x16[4];
1025 int minvar_16x16[4];
1026 int64_t threshold_4x4avg;
1027 NOISE_LEVEL noise_level = kLow;
1028 int content_state = 0;
1033 unsigned int y_sad = UINT_MAX;
1034 BLOCK_SIZE bsize = BLOCK_64X64;
1035 // Ref frame used in partitioning.
1036 MV_REFERENCE_FRAME ref_frame_partition = LAST_FRAME;
1037 int pixels_wide = 64, pixels_high = 64;
1038 int64_t thresholds[4] = { cpi->vbp_thresholds[0], cpi->vbp_thresholds[1],
1039 cpi->vbp_thresholds[2], cpi->vbp_thresholds[3] };
1041 // For the variance computation under SVC mode, we treat the frame as key if
1042 // the reference (base layer frame) is key frame (i.e., is_key_frame == 1).
1043 const int is_key_frame =
1044 (cm->frame_type == KEY_FRAME ||
1045 (is_one_pass_cbr_svc(cpi) &&
1046 cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame));
1047 // Always use 4x4 partition for key frame.
1048 const int use_4x4_partition = cm->frame_type == KEY_FRAME;
1049 const int low_res = (cm->width <= 352 && cm->height <= 288);
1050 int variance4x4downsample[16];
1052 int sb_offset = (cm->mi_stride >> 3) * (mi_row >> 3) + (mi_col >> 3);
1054 set_offsets(cpi, tile, x, mi_row, mi_col, BLOCK_64X64);
1055 segment_id = xd->mi[0]->segment_id;
1057 if (cpi->sf.use_source_sad && !is_key_frame) {
1058 int sb_offset2 = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
1059 content_state = x->content_state_sb;
1060 x->skip_low_source_sad = (content_state == kLowSadLowSumdiff ||
1061 content_state == kLowSadHighSumdiff)
1064 if (cpi->content_state_sb_fd != NULL)
1065 x->last_sb_high_content = cpi->content_state_sb_fd[sb_offset2];
1066 // If source_sad is low copy the partition without computing the y_sad.
1067 if (x->skip_low_source_sad && cpi->sf.copy_partition_flag &&
1068 copy_partitioning(cpi, x, mi_row, mi_col, segment_id, sb_offset)) {
1073 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
1074 cyclic_refresh_segment_id_boosted(segment_id)) {
1075 int q = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
1076 set_vbp_thresholds(cpi, thresholds, q, content_state);
1078 set_vbp_thresholds(cpi, thresholds, cm->base_qindex, content_state);
1081 // For non keyframes, disable 4x4 average for low resolution when speed = 8
1082 threshold_4x4avg = (cpi->oxcf.speed < 8) ? thresholds[1] << 1 : INT64_MAX;
1084 memset(x->variance_low, 0, sizeof(x->variance_low));
1086 if (xd->mb_to_right_edge < 0) pixels_wide += (xd->mb_to_right_edge >> 3);
1087 if (xd->mb_to_bottom_edge < 0) pixels_high += (xd->mb_to_bottom_edge >> 3);
1089 s = x->plane[0].src.buf;
1090 sp = x->plane[0].src.stride;
1092 // Index for force_split: 0 for 64x64, 1-4 for 32x32 blocks,
1093 // 5-20 for the 16x16 blocks.
1096 if (!is_key_frame) {
1097 // In the case of spatial/temporal scalable coding, the assumption here is
1098 // that the temporal reference frame will always be of type LAST_FRAME.
1099 // TODO(marpan): If that assumption is broken, we need to revisit this code.
1100 MODE_INFO *mi = xd->mi[0];
1101 YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
1103 const YV12_BUFFER_CONFIG *yv12_g = NULL;
1104 unsigned int y_sad_g, y_sad_thr, y_sad_last;
1105 bsize = BLOCK_32X32 + (mi_col + 4 < cm->mi_cols) * 2 +
1106 (mi_row + 4 < cm->mi_rows);
1108 assert(yv12 != NULL);
1110 if (!(is_one_pass_cbr_svc(cpi) && cpi->svc.spatial_layer_id)) {
1111 // For now, GOLDEN will not be used for non-zero spatial layers, since
1112 // it may not be a temporal reference.
1113 yv12_g = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
1116 // Only compute y_sad_g (sad for golden reference) for speed < 8.
1117 if (cpi->oxcf.speed < 8 && yv12_g && yv12_g != yv12 &&
1118 (cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
1119 vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
1120 &cm->frame_refs[GOLDEN_FRAME - 1].sf);
1121 y_sad_g = cpi->fn_ptr[bsize].sdf(
1122 x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].pre[0].buf,
1123 xd->plane[0].pre[0].stride);
1128 if (cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR &&
1129 cpi->rc.is_src_frame_alt_ref) {
1130 yv12 = get_ref_frame_buffer(cpi, ALTREF_FRAME);
1131 vp9_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
1132 &cm->frame_refs[ALTREF_FRAME - 1].sf);
1133 mi->ref_frame[0] = ALTREF_FRAME;
1136 vp9_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
1137 &cm->frame_refs[LAST_FRAME - 1].sf);
1138 mi->ref_frame[0] = LAST_FRAME;
1140 mi->ref_frame[1] = NONE;
1141 mi->sb_type = BLOCK_64X64;
1142 mi->mv[0].as_int = 0;
1143 mi->interp_filter = BILINEAR;
1145 if (cpi->oxcf.speed >= 8 && !low_res)
1146 y_sad = cpi->fn_ptr[bsize].sdf(
1147 x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].pre[0].buf,
1148 xd->plane[0].pre[0].stride);
1150 y_sad = vp9_int_pro_motion_estimation(cpi, x, bsize, mi_row, mi_col);
1153 // Pick ref frame for partitioning, bias last frame when y_sad_g and y_sad
1154 // are close if short_circuit_low_temp_var is on.
1155 y_sad_thr = cpi->sf.short_circuit_low_temp_var ? (y_sad * 7) >> 3 : y_sad;
1156 if (y_sad_g < y_sad_thr) {
1157 vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
1158 &cm->frame_refs[GOLDEN_FRAME - 1].sf);
1159 mi->ref_frame[0] = GOLDEN_FRAME;
1160 mi->mv[0].as_int = 0;
1162 ref_frame_partition = GOLDEN_FRAME;
1164 x->pred_mv[LAST_FRAME] = mi->mv[0].as_mv;
1165 ref_frame_partition = LAST_FRAME;
1168 set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
1169 vp9_build_inter_predictors_sb(xd, mi_row, mi_col, BLOCK_64X64);
1171 x->sb_is_skin = skin_sb_split(cpi, x, low_res, mi_row, mi_col, force_split);
1173 d = xd->plane[0].dst.buf;
1174 dp = xd->plane[0].dst.stride;
1176 // If the y_sad is very small, take 64x64 as partition and exit.
1177 // Don't check on boosted segment for now, as 64x64 is suppressed there.
1178 if (segment_id == CR_SEGMENT_ID_BASE && y_sad < cpi->vbp_threshold_sad) {
1179 const int block_width = num_8x8_blocks_wide_lookup[BLOCK_64X64];
1180 const int block_height = num_8x8_blocks_high_lookup[BLOCK_64X64];
1181 if (mi_col + block_width / 2 < cm->mi_cols &&
1182 mi_row + block_height / 2 < cm->mi_rows) {
1183 set_block_size(cpi, x, xd, mi_row, mi_col, BLOCK_64X64);
1184 x->variance_low[0] = 1;
1185 chroma_check(cpi, x, bsize, y_sad, is_key_frame);
1190 // If the y_sad is small enough, copy the partition of the superblock in the
1191 // last frame to current frame only if the last frame is not a keyframe.
1192 // Stop the copy every cpi->max_copied_frame to refresh the partition.
1193 // TODO(jianj) : tune the threshold.
1194 if (cpi->sf.copy_partition_flag && y_sad_last < cpi->vbp_threshold_copy &&
1195 copy_partitioning(cpi, x, mi_row, mi_col, segment_id, sb_offset)) {
1196 chroma_check(cpi, x, bsize, y_sad, is_key_frame);
1202 #if CONFIG_VP9_HIGHBITDEPTH
1203 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1205 case 10: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10); break;
1206 case 12: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12); break;
1208 default: d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8); break;
1211 #endif // CONFIG_VP9_HIGHBITDEPTH
1214 // Fill in the entire tree of 8x8 (or 4x4 under some conditions) variances
1216 for (i = 0; i < 4; i++) {
1217 const int x32_idx = ((i & 1) << 5);
1218 const int y32_idx = ((i >> 1) << 5);
1219 const int i2 = i << 2;
1220 force_split[i + 1] = 0;
1222 maxvar_16x16[i] = 0;
1223 minvar_16x16[i] = INT_MAX;
1224 for (j = 0; j < 4; j++) {
1225 const int x16_idx = x32_idx + ((j & 1) << 4);
1226 const int y16_idx = y32_idx + ((j >> 1) << 4);
1227 const int split_index = 5 + i2 + j;
1228 v16x16 *vst = &vt.split[i].split[j];
1229 force_split[split_index] = 0;
1230 variance4x4downsample[i2 + j] = 0;
1231 if (!is_key_frame) {
1232 fill_variance_8x8avg(s, sp, d, dp, x16_idx, y16_idx, vst,
1233 #if CONFIG_VP9_HIGHBITDEPTH
1236 pixels_wide, pixels_high, is_key_frame);
1237 fill_variance_tree(&vt.split[i].split[j], BLOCK_16X16);
1238 get_variance(&vt.split[i].split[j].part_variances.none);
1239 avg_16x16[i] += vt.split[i].split[j].part_variances.none.variance;
1240 if (vt.split[i].split[j].part_variances.none.variance < minvar_16x16[i])
1241 minvar_16x16[i] = vt.split[i].split[j].part_variances.none.variance;
1242 if (vt.split[i].split[j].part_variances.none.variance > maxvar_16x16[i])
1243 maxvar_16x16[i] = vt.split[i].split[j].part_variances.none.variance;
1244 if (vt.split[i].split[j].part_variances.none.variance > thresholds[2]) {
1245 // 16X16 variance is above threshold for split, so force split to 8x8
1246 // for this 16x16 block (this also forces splits for upper levels).
1247 force_split[split_index] = 1;
1248 force_split[i + 1] = 1;
1250 } else if (cpi->oxcf.speed < 8 &&
1251 vt.split[i].split[j].part_variances.none.variance >
1253 !cyclic_refresh_segment_id_boosted(segment_id)) {
1254 // We have some nominal amount of 16x16 variance (based on average),
1255 // compute the minmax over the 8x8 sub-blocks, and if above threshold,
1256 // force split to 8x8 block for this 16x16 block.
1257 int minmax = compute_minmax_8x8(s, sp, d, dp, x16_idx, y16_idx,
1258 #if CONFIG_VP9_HIGHBITDEPTH
1261 pixels_wide, pixels_high);
1262 if (minmax > cpi->vbp_threshold_minmax) {
1263 force_split[split_index] = 1;
1264 force_split[i + 1] = 1;
1269 if (is_key_frame || (low_res &&
1270 vt.split[i].split[j].part_variances.none.variance >
1271 threshold_4x4avg)) {
1272 force_split[split_index] = 0;
1273 // Go down to 4x4 down-sampling for variance.
1274 variance4x4downsample[i2 + j] = 1;
1275 for (k = 0; k < 4; k++) {
1276 int x8_idx = x16_idx + ((k & 1) << 3);
1277 int y8_idx = y16_idx + ((k >> 1) << 3);
1278 v8x8 *vst2 = is_key_frame ? &vst->split[k] : &vt2[i2 + j].split[k];
1279 fill_variance_4x4avg(s, sp, d, dp, x8_idx, y8_idx, vst2,
1280 #if CONFIG_VP9_HIGHBITDEPTH
1283 pixels_wide, pixels_high, is_key_frame);
1288 if (cpi->noise_estimate.enabled)
1289 noise_level = vp9_noise_estimate_extract_level(&cpi->noise_estimate);
1290 // Fill the rest of the variance tree by summing split partition values.
1292 for (i = 0; i < 4; i++) {
1293 const int i2 = i << 2;
1294 for (j = 0; j < 4; j++) {
1295 if (variance4x4downsample[i2 + j] == 1) {
1296 v16x16 *vtemp = (!is_key_frame) ? &vt2[i2 + j] : &vt.split[i].split[j];
1297 for (m = 0; m < 4; m++) fill_variance_tree(&vtemp->split[m], BLOCK_8X8);
1298 fill_variance_tree(vtemp, BLOCK_16X16);
1299 // If variance of this 16x16 block is above the threshold, force block
1300 // to split. This also forces a split on the upper levels.
1301 get_variance(&vtemp->part_variances.none);
1302 if (vtemp->part_variances.none.variance > thresholds[2]) {
1303 force_split[5 + i2 + j] = 1;
1304 force_split[i + 1] = 1;
1309 fill_variance_tree(&vt.split[i], BLOCK_32X32);
1310 // If variance of this 32x32 block is above the threshold, or if its above
1311 // (some threshold of) the average variance over the sub-16x16 blocks, then
1312 // force this block to split. This also forces a split on the upper
1314 if (!force_split[i + 1]) {
1315 get_variance(&vt.split[i].part_variances.none);
1316 var_32x32 = vt.split[i].part_variances.none.variance;
1317 max_var_32x32 = VPXMAX(var_32x32, max_var_32x32);
1318 min_var_32x32 = VPXMIN(var_32x32, min_var_32x32);
1319 if (vt.split[i].part_variances.none.variance > thresholds[1] ||
1321 vt.split[i].part_variances.none.variance > (thresholds[1] >> 1) &&
1322 vt.split[i].part_variances.none.variance > (avg_16x16[i] >> 1))) {
1323 force_split[i + 1] = 1;
1325 } else if (!is_key_frame && noise_level < kLow && cm->height <= 360 &&
1326 (maxvar_16x16[i] - minvar_16x16[i]) > (thresholds[1] >> 1) &&
1327 maxvar_16x16[i] > thresholds[1]) {
1328 force_split[i + 1] = 1;
1331 avg_32x32 += var_32x32;
1334 if (!force_split[0]) {
1335 fill_variance_tree(&vt, BLOCK_64X64);
1336 get_variance(&vt.part_variances.none);
1337 // If variance of this 64x64 block is above (some threshold of) the average
1338 // variance over the sub-32x32 blocks, then force this block to split.
1339 // Only checking this for noise level >= medium for now.
1340 if (!is_key_frame && noise_level >= kMedium &&
1341 vt.part_variances.none.variance > (9 * avg_32x32) >> 5)
1343 // Else if the maximum 32x32 variance minus the miniumum 32x32 variance in
1344 // a 64x64 block is greater than threshold and the maximum 32x32 variance is
1345 // above a miniumum threshold, then force the split of a 64x64 block
1346 // Only check this for low noise.
1347 else if (!is_key_frame && noise_level < kMedium &&
1348 (max_var_32x32 - min_var_32x32) > 3 * (thresholds[0] >> 3) &&
1349 max_var_32x32 > thresholds[0] >> 1)
1353 // Now go through the entire structure, splitting every block size until
1354 // we get to one that's got a variance lower than our threshold.
1355 if (mi_col + 8 > cm->mi_cols || mi_row + 8 > cm->mi_rows ||
1356 !set_vt_partitioning(cpi, x, xd, &vt, BLOCK_64X64, mi_row, mi_col,
1357 thresholds[0], BLOCK_16X16, force_split[0])) {
1358 for (i = 0; i < 4; ++i) {
1359 const int x32_idx = ((i & 1) << 2);
1360 const int y32_idx = ((i >> 1) << 2);
1361 const int i2 = i << 2;
1362 if (!set_vt_partitioning(cpi, x, xd, &vt.split[i], BLOCK_32X32,
1363 (mi_row + y32_idx), (mi_col + x32_idx),
1364 thresholds[1], BLOCK_16X16,
1365 force_split[i + 1])) {
1366 for (j = 0; j < 4; ++j) {
1367 const int x16_idx = ((j & 1) << 1);
1368 const int y16_idx = ((j >> 1) << 1);
1369 // For inter frames: if variance4x4downsample[] == 1 for this 16x16
1370 // block, then the variance is based on 4x4 down-sampling, so use vt2
1371 // in set_vt_partioning(), otherwise use vt.
1372 v16x16 *vtemp = (!is_key_frame && variance4x4downsample[i2 + j] == 1)
1374 : &vt.split[i].split[j];
1375 if (!set_vt_partitioning(
1376 cpi, x, xd, vtemp, BLOCK_16X16, mi_row + y32_idx + y16_idx,
1377 mi_col + x32_idx + x16_idx, thresholds[2], cpi->vbp_bsize_min,
1378 force_split[5 + i2 + j])) {
1379 for (k = 0; k < 4; ++k) {
1380 const int x8_idx = (k & 1);
1381 const int y8_idx = (k >> 1);
1382 if (use_4x4_partition) {
1383 if (!set_vt_partitioning(cpi, x, xd, &vtemp->split[k],
1385 mi_row + y32_idx + y16_idx + y8_idx,
1386 mi_col + x32_idx + x16_idx + x8_idx,
1387 thresholds[3], BLOCK_8X8, 0)) {
1389 cpi, x, xd, (mi_row + y32_idx + y16_idx + y8_idx),
1390 (mi_col + x32_idx + x16_idx + x8_idx), BLOCK_4X4);
1394 cpi, x, xd, (mi_row + y32_idx + y16_idx + y8_idx),
1395 (mi_col + x32_idx + x16_idx + x8_idx), BLOCK_8X8);
1404 if (cm->frame_type != KEY_FRAME && cpi->sf.copy_partition_flag) {
1405 update_prev_partition(cpi, BLOCK_64X64, mi_row, mi_col);
1406 cpi->prev_segment_id[sb_offset] = segment_id;
1407 memcpy(&(cpi->prev_variance_low[sb_offset * 25]), x->variance_low,
1408 sizeof(x->variance_low));
1409 // Reset the counter for copy partitioning
1410 if (cpi->copied_frame_cnt[sb_offset] == cpi->max_copied_frame)
1411 cpi->copied_frame_cnt[sb_offset] = 0;
1414 if (cpi->sf.short_circuit_low_temp_var) {
1415 set_low_temp_var_flag(cpi, x, xd, &vt, thresholds, ref_frame_partition,
1419 chroma_check(cpi, x, bsize, y_sad, is_key_frame);
1423 static void update_state(VP9_COMP *cpi, ThreadData *td, PICK_MODE_CONTEXT *ctx,
1424 int mi_row, int mi_col, BLOCK_SIZE bsize,
1425 int output_enabled) {
1427 VP9_COMMON *const cm = &cpi->common;
1428 RD_COUNTS *const rdc = &td->rd_counts;
1429 MACROBLOCK *const x = &td->mb;
1430 MACROBLOCKD *const xd = &x->e_mbd;
1431 struct macroblock_plane *const p = x->plane;
1432 struct macroblockd_plane *const pd = xd->plane;
1433 MODE_INFO *mi = &ctx->mic;
1434 MODE_INFO *const xdmi = xd->mi[0];
1435 MODE_INFO *mi_addr = xd->mi[0];
1436 const struct segmentation *const seg = &cm->seg;
1437 const int bw = num_8x8_blocks_wide_lookup[mi->sb_type];
1438 const int bh = num_8x8_blocks_high_lookup[mi->sb_type];
1439 const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
1440 const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
1441 MV_REF *const frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
1444 const int mis = cm->mi_stride;
1445 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
1446 const int mi_height = num_8x8_blocks_high_lookup[bsize];
1449 assert(mi->sb_type == bsize);
1452 *x->mbmi_ext = ctx->mbmi_ext;
1454 // If segmentation in use
1456 // For in frame complexity AQ copy the segment id from the segment map.
1457 if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1458 const uint8_t *const map =
1459 seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1460 mi_addr->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1462 // Else for cyclic refresh mode update the segment map, set the segment id
1463 // and then update the quantizer.
1464 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
1465 vp9_cyclic_refresh_update_segment(cpi, xd->mi[0], mi_row, mi_col, bsize,
1466 ctx->rate, ctx->dist, x->skip, p);
1470 max_plane = is_inter_block(xdmi) ? MAX_MB_PLANE : 1;
1471 for (i = 0; i < max_plane; ++i) {
1472 p[i].coeff = ctx->coeff_pbuf[i][1];
1473 p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
1474 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
1475 p[i].eobs = ctx->eobs_pbuf[i][1];
1478 for (i = max_plane; i < MAX_MB_PLANE; ++i) {
1479 p[i].coeff = ctx->coeff_pbuf[i][2];
1480 p[i].qcoeff = ctx->qcoeff_pbuf[i][2];
1481 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2];
1482 p[i].eobs = ctx->eobs_pbuf[i][2];
1485 // Restore the coding context of the MB to that that was in place
1486 // when the mode was picked for it
1487 for (y = 0; y < mi_height; y++)
1488 for (x_idx = 0; x_idx < mi_width; x_idx++)
1489 if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx &&
1490 (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
1491 xd->mi[x_idx + y * mis] = mi_addr;
1494 if (cpi->oxcf.aq_mode != NO_AQ) vp9_init_plane_quantizers(cpi, x);
1496 if (is_inter_block(xdmi) && xdmi->sb_type < BLOCK_8X8) {
1497 xdmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
1498 xdmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
1501 x->skip = ctx->skip;
1502 memcpy(x->zcoeff_blk[xdmi->tx_size], ctx->zcoeff_blk,
1503 sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk);
1505 if (!output_enabled) return;
1507 #if CONFIG_INTERNAL_STATS
1508 if (frame_is_intra_only(cm)) {
1509 static const int kf_mode_index[] = {
1510 THR_DC /*DC_PRED*/, THR_V_PRED /*V_PRED*/,
1511 THR_H_PRED /*H_PRED*/, THR_D45_PRED /*D45_PRED*/,
1512 THR_D135_PRED /*D135_PRED*/, THR_D117_PRED /*D117_PRED*/,
1513 THR_D153_PRED /*D153_PRED*/, THR_D207_PRED /*D207_PRED*/,
1514 THR_D63_PRED /*D63_PRED*/, THR_TM /*TM_PRED*/,
1516 ++cpi->mode_chosen_counts[kf_mode_index[xdmi->mode]];
1518 // Note how often each mode chosen as best
1519 ++cpi->mode_chosen_counts[ctx->best_mode_index];
1522 if (!frame_is_intra_only(cm)) {
1523 if (is_inter_block(xdmi)) {
1524 vp9_update_mv_count(td);
1526 if (cm->interp_filter == SWITCHABLE) {
1527 const int ctx = get_pred_context_switchable_interp(xd);
1528 ++td->counts->switchable_interp[ctx][xdmi->interp_filter];
1532 rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
1533 rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
1534 rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
1536 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
1537 rdc->filter_diff[i] += ctx->best_filter_diff[i];
1540 for (h = 0; h < y_mis; ++h) {
1541 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
1542 for (w = 0; w < x_mis; ++w) {
1543 MV_REF *const mv = frame_mv + w;
1544 mv->ref_frame[0] = mi->ref_frame[0];
1545 mv->ref_frame[1] = mi->ref_frame[1];
1546 mv->mv[0].as_int = mi->mv[0].as_int;
1547 mv->mv[1].as_int = mi->mv[1].as_int;
1552 void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
1553 int mi_row, int mi_col) {
1554 uint8_t *const buffers[3] = { src->y_buffer, src->u_buffer, src->v_buffer };
1555 const int strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
1558 // Set current frame pointer.
1559 x->e_mbd.cur_buf = src;
1561 for (i = 0; i < MAX_MB_PLANE; i++)
1562 setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
1563 NULL, x->e_mbd.plane[i].subsampling_x,
1564 x->e_mbd.plane[i].subsampling_y);
1567 static void set_mode_info_seg_skip(MACROBLOCK *x, TX_MODE tx_mode,
1568 RD_COST *rd_cost, BLOCK_SIZE bsize) {
1569 MACROBLOCKD *const xd = &x->e_mbd;
1570 MODE_INFO *const mi = xd->mi[0];
1571 INTERP_FILTER filter_ref;
1573 filter_ref = get_pred_context_switchable_interp(xd);
1574 if (filter_ref == SWITCHABLE_FILTERS) filter_ref = EIGHTTAP;
1576 mi->sb_type = bsize;
1579 VPXMIN(max_txsize_lookup[bsize], tx_mode_to_biggest_tx_size[tx_mode]);
1581 mi->uv_mode = DC_PRED;
1582 mi->ref_frame[0] = LAST_FRAME;
1583 mi->ref_frame[1] = NONE;
1584 mi->mv[0].as_int = 0;
1585 mi->interp_filter = filter_ref;
1587 xd->mi[0]->bmi[0].as_mv[0].as_int = 0;
1590 vp9_rd_cost_init(rd_cost);
1593 static int set_segment_rdmult(VP9_COMP *const cpi, MACROBLOCK *const x,
1594 int8_t segment_id) {
1596 VP9_COMMON *const cm = &cpi->common;
1597 vp9_init_plane_quantizers(cpi, x);
1598 vpx_clear_system_state();
1599 segment_qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
1600 return vp9_compute_rd_mult(cpi, segment_qindex + cm->y_dc_delta_q);
1603 static void rd_pick_sb_modes(VP9_COMP *cpi, TileDataEnc *tile_data,
1604 MACROBLOCK *const x, int mi_row, int mi_col,
1605 RD_COST *rd_cost, BLOCK_SIZE bsize,
1606 PICK_MODE_CONTEXT *ctx, int64_t best_rd) {
1607 VP9_COMMON *const cm = &cpi->common;
1608 TileInfo *const tile_info = &tile_data->tile_info;
1609 MACROBLOCKD *const xd = &x->e_mbd;
1611 struct macroblock_plane *const p = x->plane;
1612 struct macroblockd_plane *const pd = xd->plane;
1613 const AQ_MODE aq_mode = cpi->oxcf.aq_mode;
1616 vpx_clear_system_state();
1618 // Use the lower precision, but faster, 32x32 fdct for mode selection.
1619 x->use_lp32x32fdct = 1;
1621 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
1623 mi->sb_type = bsize;
1625 for (i = 0; i < MAX_MB_PLANE; ++i) {
1626 p[i].coeff = ctx->coeff_pbuf[i][0];
1627 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
1628 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
1629 p[i].eobs = ctx->eobs_pbuf[i][0];
1633 ctx->pred_pixel_ready = 0;
1636 // Set to zero to make sure we do not use the previous encoded frame stats
1639 #if CONFIG_VP9_HIGHBITDEPTH
1640 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1641 x->source_variance = vp9_high_get_sby_perpixel_variance(
1642 cpi, &x->plane[0].src, bsize, xd->bd);
1644 x->source_variance =
1645 vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1648 x->source_variance =
1649 vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1650 #endif // CONFIG_VP9_HIGHBITDEPTH
1652 // Save rdmult before it might be changed, so it can be restored later.
1653 orig_rdmult = x->rdmult;
1655 if ((cpi->sf.tx_domain_thresh > 0.0) || (cpi->sf.quant_opt_thresh > 0.0)) {
1656 double logvar = vp9_log_block_var(cpi, x, bsize);
1657 // Check block complexity as part of descision on using pixel or transform
1658 // domain distortion in rd tests.
1659 x->block_tx_domain = cpi->sf.allow_txfm_domain_distortion &&
1660 (logvar >= cpi->sf.tx_domain_thresh);
1662 // Check block complexity as part of descision on using quantized
1663 // coefficient optimisation inside the rd loop.
1664 x->block_qcoeff_opt =
1665 cpi->sf.allow_quant_coeff_opt && (logvar <= cpi->sf.quant_opt_thresh);
1667 x->block_tx_domain = cpi->sf.allow_txfm_domain_distortion;
1668 x->block_qcoeff_opt = cpi->sf.allow_quant_coeff_opt;
1671 if (aq_mode == VARIANCE_AQ) {
1673 bsize <= BLOCK_16X16 ? x->mb_energy : vp9_block_energy(cpi, x, bsize);
1675 if (cm->frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
1676 cpi->force_update_segmentation ||
1677 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
1678 mi->segment_id = vp9_vaq_segment_id(energy);
1680 const uint8_t *const map =
1681 cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1682 mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1684 x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
1685 } else if (aq_mode == LOOKAHEAD_AQ) {
1686 const uint8_t *const map = cpi->segmentation_map;
1688 // I do not change rdmult here consciously.
1689 mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1690 } else if (aq_mode == EQUATOR360_AQ) {
1691 if (cm->frame_type == KEY_FRAME || cpi->force_update_segmentation) {
1692 mi->segment_id = vp9_360aq_segment_id(mi_row, cm->mi_rows);
1694 const uint8_t *const map =
1695 cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1696 mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
1698 x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
1699 } else if (aq_mode == COMPLEXITY_AQ) {
1700 x->rdmult = set_segment_rdmult(cpi, x, mi->segment_id);
1701 } else if (aq_mode == CYCLIC_REFRESH_AQ) {
1702 const uint8_t *const map =
1703 cm->seg.update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
1704 // If segment is boosted, use rdmult for that segment.
1705 if (cyclic_refresh_segment_id_boosted(
1706 get_segment_id(cm, map, bsize, mi_row, mi_col)))
1707 x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
1710 // Find best coding mode & reconstruct the MB so it is available
1711 // as a predictor for MBs that follow in the SB
1712 if (frame_is_intra_only(cm)) {
1713 vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, best_rd);
1715 if (bsize >= BLOCK_8X8) {
1716 if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP))
1717 vp9_rd_pick_inter_mode_sb_seg_skip(cpi, tile_data, x, rd_cost, bsize,
1720 vp9_rd_pick_inter_mode_sb(cpi, tile_data, x, mi_row, mi_col, rd_cost,
1721 bsize, ctx, best_rd);
1723 vp9_rd_pick_inter_mode_sub8x8(cpi, tile_data, x, mi_row, mi_col, rd_cost,
1724 bsize, ctx, best_rd);
1728 // Examine the resulting rate and for AQ mode 2 make a segment choice.
1729 if ((rd_cost->rate != INT_MAX) && (aq_mode == COMPLEXITY_AQ) &&
1730 (bsize >= BLOCK_16X16) &&
1731 (cm->frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
1732 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref))) {
1733 vp9_caq_select_segment(cpi, x, bsize, mi_row, mi_col, rd_cost->rate);
1736 x->rdmult = orig_rdmult;
1738 // TODO(jingning) The rate-distortion optimization flow needs to be
1739 // refactored to provide proper exit/return handle.
1740 if (rd_cost->rate == INT_MAX) rd_cost->rdcost = INT64_MAX;
1742 ctx->rate = rd_cost->rate;
1743 ctx->dist = rd_cost->dist;
1746 static void update_stats(VP9_COMMON *cm, ThreadData *td) {
1747 const MACROBLOCK *x = &td->mb;
1748 const MACROBLOCKD *const xd = &x->e_mbd;
1749 const MODE_INFO *const mi = xd->mi[0];
1750 const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
1751 const BLOCK_SIZE bsize = mi->sb_type;
1753 if (!frame_is_intra_only(cm)) {
1754 FRAME_COUNTS *const counts = td->counts;
1755 const int inter_block = is_inter_block(mi);
1756 const int seg_ref_active =
1757 segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_REF_FRAME);
1758 if (!seg_ref_active) {
1759 counts->intra_inter[get_intra_inter_context(xd)][inter_block]++;
1760 // If the segment reference feature is enabled we have only a single
1761 // reference frame allowed for the segment so exclude it from
1762 // the reference frame counts used to work out probabilities.
1764 const MV_REFERENCE_FRAME ref0 = mi->ref_frame[0];
1765 if (cm->reference_mode == REFERENCE_MODE_SELECT)
1766 counts->comp_inter[vp9_get_reference_mode_context(cm, xd)]
1767 [has_second_ref(mi)]++;
1769 if (has_second_ref(mi)) {
1770 counts->comp_ref[vp9_get_pred_context_comp_ref_p(cm, xd)]
1771 [ref0 == GOLDEN_FRAME]++;
1773 counts->single_ref[vp9_get_pred_context_single_ref_p1(xd)][0]
1774 [ref0 != LAST_FRAME]++;
1775 if (ref0 != LAST_FRAME)
1776 counts->single_ref[vp9_get_pred_context_single_ref_p2(xd)][1]
1777 [ref0 != GOLDEN_FRAME]++;
1782 !segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP)) {
1783 const int mode_ctx = mbmi_ext->mode_context[mi->ref_frame[0]];
1784 if (bsize >= BLOCK_8X8) {
1785 const PREDICTION_MODE mode = mi->mode;
1786 ++counts->inter_mode[mode_ctx][INTER_OFFSET(mode)];
1788 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
1789 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
1791 for (idy = 0; idy < 2; idy += num_4x4_h) {
1792 for (idx = 0; idx < 2; idx += num_4x4_w) {
1793 const int j = idy * 2 + idx;
1794 const PREDICTION_MODE b_mode = mi->bmi[j].as_mode;
1795 ++counts->inter_mode[mode_ctx][INTER_OFFSET(b_mode)];
1803 static void restore_context(MACROBLOCK *const x, int mi_row, int mi_col,
1804 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
1805 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
1806 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
1808 MACROBLOCKD *const xd = &x->e_mbd;
1810 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1811 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1812 int mi_width = num_8x8_blocks_wide_lookup[bsize];
1813 int mi_height = num_8x8_blocks_high_lookup[bsize];
1814 for (p = 0; p < MAX_MB_PLANE; p++) {
1815 memcpy(xd->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
1816 a + num_4x4_blocks_wide * p,
1817 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
1818 xd->plane[p].subsampling_x);
1819 memcpy(xd->left_context[p] +
1820 ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
1821 l + num_4x4_blocks_high * p,
1822 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
1823 xd->plane[p].subsampling_y);
1825 memcpy(xd->above_seg_context + mi_col, sa,
1826 sizeof(*xd->above_seg_context) * mi_width);
1827 memcpy(xd->left_seg_context + (mi_row & MI_MASK), sl,
1828 sizeof(xd->left_seg_context[0]) * mi_height);
1831 static void save_context(MACROBLOCK *const x, int mi_row, int mi_col,
1832 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
1833 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
1834 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
1836 const MACROBLOCKD *const xd = &x->e_mbd;
1838 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1839 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1840 int mi_width = num_8x8_blocks_wide_lookup[bsize];
1841 int mi_height = num_8x8_blocks_high_lookup[bsize];
1843 // buffer the above/left context information of the block in search.
1844 for (p = 0; p < MAX_MB_PLANE; ++p) {
1845 memcpy(a + num_4x4_blocks_wide * p,
1846 xd->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
1847 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
1848 xd->plane[p].subsampling_x);
1849 memcpy(l + num_4x4_blocks_high * p,
1850 xd->left_context[p] +
1851 ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
1852 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
1853 xd->plane[p].subsampling_y);
1855 memcpy(sa, xd->above_seg_context + mi_col,
1856 sizeof(*xd->above_seg_context) * mi_width);
1857 memcpy(sl, xd->left_seg_context + (mi_row & MI_MASK),
1858 sizeof(xd->left_seg_context[0]) * mi_height);
1861 static void encode_b(VP9_COMP *cpi, const TileInfo *const tile, ThreadData *td,
1862 TOKENEXTRA **tp, int mi_row, int mi_col,
1863 int output_enabled, BLOCK_SIZE bsize,
1864 PICK_MODE_CONTEXT *ctx) {
1865 MACROBLOCK *const x = &td->mb;
1866 set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
1867 update_state(cpi, td, ctx, mi_row, mi_col, bsize, output_enabled);
1868 encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
1870 if (output_enabled) {
1871 update_stats(&cpi->common, td);
1873 (*tp)->token = EOSB_TOKEN;
1878 static void encode_sb(VP9_COMP *cpi, ThreadData *td, const TileInfo *const tile,
1879 TOKENEXTRA **tp, int mi_row, int mi_col,
1880 int output_enabled, BLOCK_SIZE bsize, PC_TREE *pc_tree) {
1881 VP9_COMMON *const cm = &cpi->common;
1882 MACROBLOCK *const x = &td->mb;
1883 MACROBLOCKD *const xd = &x->e_mbd;
1885 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
1887 PARTITION_TYPE partition;
1888 BLOCK_SIZE subsize = bsize;
1890 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
1892 if (bsize >= BLOCK_8X8) {
1893 ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1894 subsize = get_subsize(bsize, pc_tree->partitioning);
1897 subsize = BLOCK_4X4;
1900 partition = partition_lookup[bsl][subsize];
1901 if (output_enabled && bsize != BLOCK_4X4)
1902 td->counts->partition[ctx][partition]++;
1904 switch (partition) {
1905 case PARTITION_NONE:
1906 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1909 case PARTITION_VERT:
1910 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1911 &pc_tree->vertical[0]);
1912 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
1913 encode_b(cpi, tile, td, tp, mi_row, mi_col + hbs, output_enabled,
1914 subsize, &pc_tree->vertical[1]);
1917 case PARTITION_HORZ:
1918 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1919 &pc_tree->horizontal[0]);
1920 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
1921 encode_b(cpi, tile, td, tp, mi_row + hbs, mi_col, output_enabled,
1922 subsize, &pc_tree->horizontal[1]);
1925 case PARTITION_SPLIT:
1926 if (bsize == BLOCK_8X8) {
1927 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1928 pc_tree->leaf_split[0]);
1930 encode_sb(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1932 encode_sb(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
1933 subsize, pc_tree->split[1]);
1934 encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
1935 subsize, pc_tree->split[2]);
1936 encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs, output_enabled,
1937 subsize, pc_tree->split[3]);
1940 default: assert(0 && "Invalid partition type."); break;
1943 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
1944 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1947 // Check to see if the given partition size is allowed for a specified number
1948 // of 8x8 block rows and columns remaining in the image.
1949 // If not then return the largest allowed partition size
1950 static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize, int rows_left,
1951 int cols_left, int *bh, int *bw) {
1952 if (rows_left <= 0 || cols_left <= 0) {
1953 return VPXMIN(bsize, BLOCK_8X8);
1955 for (; bsize > 0; bsize -= 3) {
1956 *bh = num_8x8_blocks_high_lookup[bsize];
1957 *bw = num_8x8_blocks_wide_lookup[bsize];
1958 if ((*bh <= rows_left) && (*bw <= cols_left)) {
1966 static void set_partial_b64x64_partition(MODE_INFO *mi, int mis, int bh_in,
1967 int bw_in, int row8x8_remaining,
1968 int col8x8_remaining, BLOCK_SIZE bsize,
1969 MODE_INFO **mi_8x8) {
1972 for (r = 0; r < MI_BLOCK_SIZE; r += bh) {
1974 for (c = 0; c < MI_BLOCK_SIZE; c += bw) {
1975 const int index = r * mis + c;
1976 mi_8x8[index] = mi + index;
1977 mi_8x8[index]->sb_type = find_partition_size(
1978 bsize, row8x8_remaining - r, col8x8_remaining - c, &bh, &bw);
1983 // This function attempts to set all mode info entries in a given SB64
1984 // to the same block partition size.
1985 // However, at the bottom and right borders of the image the requested size
1986 // may not be allowed in which case this code attempts to choose the largest
1987 // allowable partition.
1988 static void set_fixed_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
1989 MODE_INFO **mi_8x8, int mi_row, int mi_col,
1991 VP9_COMMON *const cm = &cpi->common;
1992 const int mis = cm->mi_stride;
1993 const int row8x8_remaining = tile->mi_row_end - mi_row;
1994 const int col8x8_remaining = tile->mi_col_end - mi_col;
1995 int block_row, block_col;
1996 MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
1997 int bh = num_8x8_blocks_high_lookup[bsize];
1998 int bw = num_8x8_blocks_wide_lookup[bsize];
2000 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
2002 // Apply the requested partition size to the SB64 if it is all "in image"
2003 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
2004 (row8x8_remaining >= MI_BLOCK_SIZE)) {
2005 for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
2006 for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
2007 int index = block_row * mis + block_col;
2008 mi_8x8[index] = mi_upper_left + index;
2009 mi_8x8[index]->sb_type = bsize;
2013 // Else this is a partial SB64.
2014 set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
2015 col8x8_remaining, bsize, mi_8x8);
2019 static const struct {
2022 } coord_lookup[16] = {
2045 static void set_source_var_based_partition(VP9_COMP *cpi,
2046 const TileInfo *const tile,
2047 MACROBLOCK *const x,
2048 MODE_INFO **mi_8x8, int mi_row,
2050 VP9_COMMON *const cm = &cpi->common;
2051 const int mis = cm->mi_stride;
2052 const int row8x8_remaining = tile->mi_row_end - mi_row;
2053 const int col8x8_remaining = tile->mi_col_end - mi_col;
2054 MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
2056 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
2058 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
2061 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
2062 (row8x8_remaining >= MI_BLOCK_SIZE)) {
2066 const int offset = (mi_row >> 1) * cm->mb_cols + (mi_col >> 1);
2067 int is_larger_better = 0;
2069 unsigned int thr = cpi->source_var_thresh;
2071 memset(d32, 0, 4 * sizeof(diff));
2073 for (i = 0; i < 4; i++) {
2076 for (j = 0; j < 4; j++) {
2077 int b_mi_row = coord_lookup[i * 4 + j].row;
2078 int b_mi_col = coord_lookup[i * 4 + j].col;
2079 int boffset = b_mi_row / 2 * cm->mb_cols + b_mi_col / 2;
2081 d16[j] = cpi->source_diff_var + offset + boffset;
2083 index = b_mi_row * mis + b_mi_col;
2084 mi_8x8[index] = mi_upper_left + index;
2085 mi_8x8[index]->sb_type = BLOCK_16X16;
2087 // TODO(yunqingwang): If d16[j].var is very large, use 8x8 partition
2088 // size to further improve quality.
2091 is_larger_better = (d16[0]->var < thr) && (d16[1]->var < thr) &&
2092 (d16[2]->var < thr) && (d16[3]->var < thr);
2094 // Use 32x32 partition
2095 if (is_larger_better) {
2098 for (j = 0; j < 4; j++) {
2099 d32[i].sse += d16[j]->sse;
2100 d32[i].sum += d16[j]->sum;
2104 (unsigned int)(d32[i].sse -
2105 (unsigned int)(((int64_t)d32[i].sum * d32[i].sum) >>
2108 index = coord_lookup[i * 4].row * mis + coord_lookup[i * 4].col;
2109 mi_8x8[index] = mi_upper_left + index;
2110 mi_8x8[index]->sb_type = BLOCK_32X32;
2114 if (use32x32 == 4) {
2116 is_larger_better = (d32[0].var < thr) && (d32[1].var < thr) &&
2117 (d32[2].var < thr) && (d32[3].var < thr);
2119 // Use 64x64 partition
2120 if (is_larger_better) {
2121 mi_8x8[0] = mi_upper_left;
2122 mi_8x8[0]->sb_type = BLOCK_64X64;
2125 } else { // partial in-image SB64
2126 int bh = num_8x8_blocks_high_lookup[BLOCK_16X16];
2127 int bw = num_8x8_blocks_wide_lookup[BLOCK_16X16];
2128 set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
2129 col8x8_remaining, BLOCK_16X16, mi_8x8);
2133 static void update_state_rt(VP9_COMP *cpi, ThreadData *td,
2134 PICK_MODE_CONTEXT *ctx, int mi_row, int mi_col,
2136 VP9_COMMON *const cm = &cpi->common;
2137 MACROBLOCK *const x = &td->mb;
2138 MACROBLOCKD *const xd = &x->e_mbd;
2139 MODE_INFO *const mi = xd->mi[0];
2140 struct macroblock_plane *const p = x->plane;
2141 const struct segmentation *const seg = &cm->seg;
2142 const int bw = num_8x8_blocks_wide_lookup[mi->sb_type];
2143 const int bh = num_8x8_blocks_high_lookup[mi->sb_type];
2144 const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
2145 const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
2147 *(xd->mi[0]) = ctx->mic;
2148 *(x->mbmi_ext) = ctx->mbmi_ext;
2150 if (seg->enabled && cpi->oxcf.aq_mode != NO_AQ) {
2151 // For in frame complexity AQ or variance AQ, copy segment_id from
2152 // segmentation_map.
2153 if (cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ) {
2154 const uint8_t *const map =
2155 seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
2156 mi->segment_id = get_segment_id(cm, map, bsize, mi_row, mi_col);
2158 // Setting segmentation map for cyclic_refresh.
2159 vp9_cyclic_refresh_update_segment(cpi, mi, mi_row, mi_col, bsize,
2160 ctx->rate, ctx->dist, x->skip, p);
2162 vp9_init_plane_quantizers(cpi, x);
2165 if (is_inter_block(mi)) {
2166 vp9_update_mv_count(td);
2167 if (cm->interp_filter == SWITCHABLE) {
2168 const int pred_ctx = get_pred_context_switchable_interp(xd);
2169 ++td->counts->switchable_interp[pred_ctx][mi->interp_filter];
2172 if (mi->sb_type < BLOCK_8X8) {
2173 mi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
2174 mi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
2178 if (cm->use_prev_frame_mvs || !cm->error_resilient_mode ||
2179 (cpi->svc.use_base_mv && cpi->svc.number_spatial_layers > 1 &&
2180 cpi->svc.spatial_layer_id != cpi->svc.number_spatial_layers - 1)) {
2181 MV_REF *const frame_mvs =
2182 cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
2185 for (h = 0; h < y_mis; ++h) {
2186 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
2187 for (w = 0; w < x_mis; ++w) {
2188 MV_REF *const mv = frame_mv + w;
2189 mv->ref_frame[0] = mi->ref_frame[0];
2190 mv->ref_frame[1] = mi->ref_frame[1];
2191 mv->mv[0].as_int = mi->mv[0].as_int;
2192 mv->mv[1].as_int = mi->mv[1].as_int;
2197 x->skip = ctx->skip;
2198 x->skip_txfm[0] = mi->segment_id ? 0 : ctx->skip_txfm[0];
2201 static void encode_b_rt(VP9_COMP *cpi, ThreadData *td,
2202 const TileInfo *const tile, TOKENEXTRA **tp, int mi_row,
2203 int mi_col, int output_enabled, BLOCK_SIZE bsize,
2204 PICK_MODE_CONTEXT *ctx) {
2205 MACROBLOCK *const x = &td->mb;
2206 set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
2207 update_state_rt(cpi, td, ctx, mi_row, mi_col, bsize);
2209 encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
2210 update_stats(&cpi->common, td);
2212 (*tp)->token = EOSB_TOKEN;
2216 static void encode_sb_rt(VP9_COMP *cpi, ThreadData *td,
2217 const TileInfo *const tile, TOKENEXTRA **tp,
2218 int mi_row, int mi_col, int output_enabled,
2219 BLOCK_SIZE bsize, PC_TREE *pc_tree) {
2220 VP9_COMMON *const cm = &cpi->common;
2221 MACROBLOCK *const x = &td->mb;
2222 MACROBLOCKD *const xd = &x->e_mbd;
2224 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
2226 PARTITION_TYPE partition;
2229 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2231 if (bsize >= BLOCK_8X8) {
2232 const int idx_str = xd->mi_stride * mi_row + mi_col;
2233 MODE_INFO **mi_8x8 = cm->mi_grid_visible + idx_str;
2234 ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
2235 subsize = mi_8x8[0]->sb_type;
2238 subsize = BLOCK_4X4;
2241 partition = partition_lookup[bsl][subsize];
2242 if (output_enabled && bsize != BLOCK_4X4)
2243 td->counts->partition[ctx][partition]++;
2245 switch (partition) {
2246 case PARTITION_NONE:
2247 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2250 case PARTITION_VERT:
2251 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2252 &pc_tree->vertical[0]);
2253 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
2254 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
2255 subsize, &pc_tree->vertical[1]);
2258 case PARTITION_HORZ:
2259 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2260 &pc_tree->horizontal[0]);
2261 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
2262 encode_b_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
2263 subsize, &pc_tree->horizontal[1]);
2266 case PARTITION_SPLIT:
2267 subsize = get_subsize(bsize, PARTITION_SPLIT);
2268 encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
2270 encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
2271 subsize, pc_tree->split[1]);
2272 encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
2273 subsize, pc_tree->split[2]);
2274 encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs,
2275 output_enabled, subsize, pc_tree->split[3]);
2277 default: assert(0 && "Invalid partition type."); break;
2280 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
2281 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
2284 static void rd_use_partition(VP9_COMP *cpi, ThreadData *td,
2285 TileDataEnc *tile_data, MODE_INFO **mi_8x8,
2286 TOKENEXTRA **tp, int mi_row, int mi_col,
2287 BLOCK_SIZE bsize, int *rate, int64_t *dist,
2288 int do_recon, PC_TREE *pc_tree) {
2289 VP9_COMMON *const cm = &cpi->common;
2290 TileInfo *const tile_info = &tile_data->tile_info;
2291 MACROBLOCK *const x = &td->mb;
2292 MACROBLOCKD *const xd = &x->e_mbd;
2293 const int mis = cm->mi_stride;
2294 const int bsl = b_width_log2_lookup[bsize];
2295 const int mi_step = num_4x4_blocks_wide_lookup[bsize] / 2;
2296 const int bss = (1 << bsl) / 4;
2298 PARTITION_TYPE partition = PARTITION_NONE;
2300 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2301 PARTITION_CONTEXT sl[8], sa[8];
2302 RD_COST last_part_rdc, none_rdc, chosen_rdc;
2303 BLOCK_SIZE sub_subsize = BLOCK_4X4;
2304 int splits_below = 0;
2305 BLOCK_SIZE bs_type = mi_8x8[0]->sb_type;
2306 int do_partition_search = 1;
2307 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
2309 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
2311 assert(num_4x4_blocks_wide_lookup[bsize] ==
2312 num_4x4_blocks_high_lookup[bsize]);
2314 vp9_rd_cost_reset(&last_part_rdc);
2315 vp9_rd_cost_reset(&none_rdc);
2316 vp9_rd_cost_reset(&chosen_rdc);
2318 partition = partition_lookup[bsl][bs_type];
2319 subsize = get_subsize(bsize, partition);
2321 pc_tree->partitioning = partition;
2322 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2324 if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode != NO_AQ) {
2325 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2326 x->mb_energy = vp9_block_energy(cpi, x, bsize);
2329 if (do_partition_search &&
2330 cpi->sf.partition_search_type == SEARCH_PARTITION &&
2331 cpi->sf.adjust_partitioning_from_last_frame) {
2332 // Check if any of the sub blocks are further split.
2333 if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
2334 sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
2336 for (i = 0; i < 4; i++) {
2337 int jj = i >> 1, ii = i & 0x01;
2338 MODE_INFO *this_mi = mi_8x8[jj * bss * mis + ii * bss];
2339 if (this_mi && this_mi->sb_type >= sub_subsize) {
2345 // If partition is not none try none unless each of the 4 splits are split
2347 if (partition != PARTITION_NONE && !splits_below &&
2348 mi_row + (mi_step >> 1) < cm->mi_rows &&
2349 mi_col + (mi_step >> 1) < cm->mi_cols) {
2350 pc_tree->partitioning = PARTITION_NONE;
2351 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &none_rdc, bsize, ctx,
2354 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2356 if (none_rdc.rate < INT_MAX) {
2357 none_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
2359 RDCOST(x->rdmult, x->rddiv, none_rdc.rate, none_rdc.dist);
2362 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2363 mi_8x8[0]->sb_type = bs_type;
2364 pc_tree->partitioning = partition;
2368 switch (partition) {
2369 case PARTITION_NONE:
2370 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc, bsize,
2373 case PARTITION_HORZ:
2374 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
2375 subsize, &pc_tree->horizontal[0], INT64_MAX);
2376 if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
2377 mi_row + (mi_step >> 1) < cm->mi_rows) {
2379 PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
2380 vp9_rd_cost_init(&tmp_rdc);
2381 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
2382 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
2383 rd_pick_sb_modes(cpi, tile_data, x, mi_row + (mi_step >> 1), mi_col,
2384 &tmp_rdc, subsize, &pc_tree->horizontal[1], INT64_MAX);
2385 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2386 vp9_rd_cost_reset(&last_part_rdc);
2389 last_part_rdc.rate += tmp_rdc.rate;
2390 last_part_rdc.dist += tmp_rdc.dist;
2391 last_part_rdc.rdcost += tmp_rdc.rdcost;
2394 case PARTITION_VERT:
2395 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
2396 subsize, &pc_tree->vertical[0], INT64_MAX);
2397 if (last_part_rdc.rate != INT_MAX && bsize >= BLOCK_8X8 &&
2398 mi_col + (mi_step >> 1) < cm->mi_cols) {
2400 PICK_MODE_CONTEXT *ctx = &pc_tree->vertical[0];
2401 vp9_rd_cost_init(&tmp_rdc);
2402 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
2403 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
2404 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + (mi_step >> 1),
2406 &pc_tree->vertical[bsize > BLOCK_8X8], INT64_MAX);
2407 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2408 vp9_rd_cost_reset(&last_part_rdc);
2411 last_part_rdc.rate += tmp_rdc.rate;
2412 last_part_rdc.dist += tmp_rdc.dist;
2413 last_part_rdc.rdcost += tmp_rdc.rdcost;
2416 case PARTITION_SPLIT:
2417 if (bsize == BLOCK_8X8) {
2418 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
2419 subsize, pc_tree->leaf_split[0], INT64_MAX);
2422 last_part_rdc.rate = 0;
2423 last_part_rdc.dist = 0;
2424 last_part_rdc.rdcost = 0;
2425 for (i = 0; i < 4; i++) {
2426 int x_idx = (i & 1) * (mi_step >> 1);
2427 int y_idx = (i >> 1) * (mi_step >> 1);
2428 int jj = i >> 1, ii = i & 0x01;
2430 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
2433 vp9_rd_cost_init(&tmp_rdc);
2434 rd_use_partition(cpi, td, tile_data, mi_8x8 + jj * bss * mis + ii * bss,
2435 tp, mi_row + y_idx, mi_col + x_idx, subsize,
2436 &tmp_rdc.rate, &tmp_rdc.dist, i != 3,
2438 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2439 vp9_rd_cost_reset(&last_part_rdc);
2442 last_part_rdc.rate += tmp_rdc.rate;
2443 last_part_rdc.dist += tmp_rdc.dist;
2446 default: assert(0); break;
2449 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2450 if (last_part_rdc.rate < INT_MAX) {
2451 last_part_rdc.rate += cpi->partition_cost[pl][partition];
2452 last_part_rdc.rdcost =
2453 RDCOST(x->rdmult, x->rddiv, last_part_rdc.rate, last_part_rdc.dist);
2456 if (do_partition_search && cpi->sf.adjust_partitioning_from_last_frame &&
2457 cpi->sf.partition_search_type == SEARCH_PARTITION &&
2458 partition != PARTITION_SPLIT && bsize > BLOCK_8X8 &&
2459 (mi_row + mi_step < cm->mi_rows ||
2460 mi_row + (mi_step >> 1) == cm->mi_rows) &&
2461 (mi_col + mi_step < cm->mi_cols ||
2462 mi_col + (mi_step >> 1) == cm->mi_cols)) {
2463 BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
2464 chosen_rdc.rate = 0;
2465 chosen_rdc.dist = 0;
2466 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2467 pc_tree->partitioning = PARTITION_SPLIT;
2470 for (i = 0; i < 4; i++) {
2471 int x_idx = (i & 1) * (mi_step >> 1);
2472 int y_idx = (i >> 1) * (mi_step >> 1);
2474 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2475 PARTITION_CONTEXT sl[8], sa[8];
2477 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
2480 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2481 pc_tree->split[i]->partitioning = PARTITION_NONE;
2482 rd_pick_sb_modes(cpi, tile_data, x, mi_row + y_idx, mi_col + x_idx,
2483 &tmp_rdc, split_subsize, &pc_tree->split[i]->none,
2486 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2488 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2489 vp9_rd_cost_reset(&chosen_rdc);
2493 chosen_rdc.rate += tmp_rdc.rate;
2494 chosen_rdc.dist += tmp_rdc.dist;
2497 encode_sb(cpi, td, tile_info, tp, mi_row + y_idx, mi_col + x_idx, 0,
2498 split_subsize, pc_tree->split[i]);
2500 pl = partition_plane_context(xd, mi_row + y_idx, mi_col + x_idx,
2502 chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
2504 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2505 if (chosen_rdc.rate < INT_MAX) {
2506 chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
2508 RDCOST(x->rdmult, x->rddiv, chosen_rdc.rate, chosen_rdc.dist);
2512 // If last_part is better set the partitioning to that.
2513 if (last_part_rdc.rdcost < chosen_rdc.rdcost) {
2514 mi_8x8[0]->sb_type = bsize;
2515 if (bsize >= BLOCK_8X8) pc_tree->partitioning = partition;
2516 chosen_rdc = last_part_rdc;
2518 // If none was better set the partitioning to that.
2519 if (none_rdc.rdcost < chosen_rdc.rdcost) {
2520 if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
2521 chosen_rdc = none_rdc;
2524 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2526 // We must have chosen a partitioning and encoding or we'll fail later on.
2527 // No other opportunities for success.
2528 if (bsize == BLOCK_64X64)
2529 assert(chosen_rdc.rate < INT_MAX && chosen_rdc.dist < INT64_MAX);
2532 int output_enabled = (bsize == BLOCK_64X64);
2533 encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
2537 *rate = chosen_rdc.rate;
2538 *dist = chosen_rdc.dist;
2541 static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
2542 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
2543 BLOCK_4X4, BLOCK_8X8, BLOCK_8X8, BLOCK_8X8, BLOCK_16X16,
2544 BLOCK_16X16, BLOCK_16X16, BLOCK_16X16
2547 static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
2548 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_32X32,
2549 BLOCK_32X32, BLOCK_32X32, BLOCK_64X64, BLOCK_64X64, BLOCK_64X64,
2550 BLOCK_64X64, BLOCK_64X64, BLOCK_64X64
2553 // Look at all the mode_info entries for blocks that are part of this
2554 // partition and find the min and max values for sb_type.
2555 // At the moment this is designed to work on a 64x64 SB but could be
2556 // adjusted to use a size parameter.
2558 // The min and max are assumed to have been initialized prior to calling this
2559 // function so repeat calls can accumulate a min and max of more than one sb64.
2560 static void get_sb_partition_size_range(MACROBLOCKD *xd, MODE_INFO **mi_8x8,
2561 BLOCK_SIZE *min_block_size,
2562 BLOCK_SIZE *max_block_size,
2563 int bs_hist[BLOCK_SIZES]) {
2564 int sb_width_in_blocks = MI_BLOCK_SIZE;
2565 int sb_height_in_blocks = MI_BLOCK_SIZE;
2569 // Check the sb_type for each block that belongs to this region.
2570 for (i = 0; i < sb_height_in_blocks; ++i) {
2571 for (j = 0; j < sb_width_in_blocks; ++j) {
2572 MODE_INFO *mi = mi_8x8[index + j];
2573 BLOCK_SIZE sb_type = mi ? mi->sb_type : 0;
2575 *min_block_size = VPXMIN(*min_block_size, sb_type);
2576 *max_block_size = VPXMAX(*max_block_size, sb_type);
2578 index += xd->mi_stride;
2582 // Next square block size less or equal than current block size.
2583 static const BLOCK_SIZE next_square_size[BLOCK_SIZES] = {
2584 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4, BLOCK_8X8, BLOCK_8X8,
2585 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16, BLOCK_16X16, BLOCK_32X32,
2586 BLOCK_32X32, BLOCK_32X32, BLOCK_64X64
2589 // Look at neighboring blocks and set a min and max partition size based on
2591 static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
2592 MACROBLOCKD *const xd, int mi_row,
2593 int mi_col, BLOCK_SIZE *min_block_size,
2594 BLOCK_SIZE *max_block_size) {
2595 VP9_COMMON *const cm = &cpi->common;
2596 MODE_INFO **mi = xd->mi;
2597 const int left_in_image = !!xd->left_mi;
2598 const int above_in_image = !!xd->above_mi;
2599 const int row8x8_remaining = tile->mi_row_end - mi_row;
2600 const int col8x8_remaining = tile->mi_col_end - mi_col;
2602 BLOCK_SIZE min_size = BLOCK_4X4;
2603 BLOCK_SIZE max_size = BLOCK_64X64;
2604 int bs_hist[BLOCK_SIZES] = { 0 };
2606 // Trap case where we do not have a prediction.
2607 if (left_in_image || above_in_image || cm->frame_type != KEY_FRAME) {
2608 // Default "min to max" and "max to min"
2609 min_size = BLOCK_64X64;
2610 max_size = BLOCK_4X4;
2612 // NOTE: each call to get_sb_partition_size_range() uses the previous
2613 // passed in values for min and max as a starting point.
2614 // Find the min and max partition used in previous frame at this location
2615 if (cm->frame_type != KEY_FRAME) {
2616 MODE_INFO **prev_mi =
2617 &cm->prev_mi_grid_visible[mi_row * xd->mi_stride + mi_col];
2618 get_sb_partition_size_range(xd, prev_mi, &min_size, &max_size, bs_hist);
2620 // Find the min and max partition sizes used in the left SB64
2621 if (left_in_image) {
2622 MODE_INFO **left_sb64_mi = &mi[-MI_BLOCK_SIZE];
2623 get_sb_partition_size_range(xd, left_sb64_mi, &min_size, &max_size,
2626 // Find the min and max partition sizes used in the above SB64.
2627 if (above_in_image) {
2628 MODE_INFO **above_sb64_mi = &mi[-xd->mi_stride * MI_BLOCK_SIZE];
2629 get_sb_partition_size_range(xd, above_sb64_mi, &min_size, &max_size,
2633 // Adjust observed min and max for "relaxed" auto partition case.
2634 if (cpi->sf.auto_min_max_partition_size == RELAXED_NEIGHBORING_MIN_MAX) {
2635 min_size = min_partition_size[min_size];
2636 max_size = max_partition_size[max_size];
2640 // Check border cases where max and min from neighbors may not be legal.
2641 max_size = find_partition_size(max_size, row8x8_remaining, col8x8_remaining,
2643 // Test for blocks at the edge of the active image.
2644 // This may be the actual edge of the image or where there are formatting
2646 if (vp9_active_edge_sb(cpi, mi_row, mi_col)) {
2647 min_size = BLOCK_4X4;
2650 VPXMIN(cpi->sf.rd_auto_partition_min_limit, VPXMIN(min_size, max_size));
2653 // When use_square_partition_only is true, make sure at least one square
2654 // partition is allowed by selecting the next smaller square size as
2656 if (cpi->sf.use_square_partition_only &&
2657 next_square_size[max_size] < min_size) {
2658 min_size = next_square_size[max_size];
2661 *min_block_size = min_size;
2662 *max_block_size = max_size;
2665 // TODO(jingning) refactor functions setting partition search range
2666 static void set_partition_range(VP9_COMMON *cm, MACROBLOCKD *xd, int mi_row,
2667 int mi_col, BLOCK_SIZE bsize,
2668 BLOCK_SIZE *min_bs, BLOCK_SIZE *max_bs) {
2669 int mi_width = num_8x8_blocks_wide_lookup[bsize];
2670 int mi_height = num_8x8_blocks_high_lookup[bsize];
2674 const int idx_str = cm->mi_stride * mi_row + mi_col;
2675 MODE_INFO **prev_mi = &cm->prev_mi_grid_visible[idx_str];
2676 BLOCK_SIZE bs, min_size, max_size;
2678 min_size = BLOCK_64X64;
2679 max_size = BLOCK_4X4;
2682 for (idy = 0; idy < mi_height; ++idy) {
2683 for (idx = 0; idx < mi_width; ++idx) {
2684 mi = prev_mi[idy * cm->mi_stride + idx];
2685 bs = mi ? mi->sb_type : bsize;
2686 min_size = VPXMIN(min_size, bs);
2687 max_size = VPXMAX(max_size, bs);
2693 for (idy = 0; idy < mi_height; ++idy) {
2694 mi = xd->mi[idy * cm->mi_stride - 1];
2695 bs = mi ? mi->sb_type : bsize;
2696 min_size = VPXMIN(min_size, bs);
2697 max_size = VPXMAX(max_size, bs);
2702 for (idx = 0; idx < mi_width; ++idx) {
2703 mi = xd->mi[idx - cm->mi_stride];
2704 bs = mi ? mi->sb_type : bsize;
2705 min_size = VPXMIN(min_size, bs);
2706 max_size = VPXMAX(max_size, bs);
2710 if (min_size == max_size) {
2711 min_size = min_partition_size[min_size];
2712 max_size = max_partition_size[max_size];
2719 static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
2720 memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
2723 static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
2724 memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
2727 #if CONFIG_FP_MB_STATS
2728 const int num_16x16_blocks_wide_lookup[BLOCK_SIZES] = { 1, 1, 1, 1, 1, 1, 1,
2730 const int num_16x16_blocks_high_lookup[BLOCK_SIZES] = { 1, 1, 1, 1, 1, 1, 1,
2732 const int qindex_skip_threshold_lookup[BLOCK_SIZES] = {
2733 0, 10, 10, 30, 40, 40, 60, 80, 80, 90, 100, 100, 120
2735 const int qindex_split_threshold_lookup[BLOCK_SIZES] = {
2736 0, 3, 3, 7, 15, 15, 30, 40, 40, 60, 80, 80, 120
2738 const int complexity_16x16_blocks_threshold[BLOCK_SIZES] = {
2739 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 6
2751 static INLINE MOTION_DIRECTION get_motion_direction_fp(uint8_t fp_byte) {
2752 if (fp_byte & FPMB_MOTION_ZERO_MASK) {
2754 } else if (fp_byte & FPMB_MOTION_LEFT_MASK) {
2756 } else if (fp_byte & FPMB_MOTION_RIGHT_MASK) {
2758 } else if (fp_byte & FPMB_MOTION_UP_MASK) {
2765 static INLINE int get_motion_inconsistency(MOTION_DIRECTION this_mv,
2766 MOTION_DIRECTION that_mv) {
2767 if (this_mv == that_mv) {
2770 return abs(this_mv - that_mv) == 2 ? 2 : 1;
2775 // Calculate the score used in machine-learning based partition search early
2777 static double compute_score(VP9_COMMON *const cm, MACROBLOCKD *const xd,
2778 PICK_MODE_CONTEXT *ctx, int mi_row, int mi_col,
2784 abs(ctx->mic.mv[0].as_mv.col) + abs(ctx->mic.mv[0].as_mv.row);
2785 const int left_in_image = !!xd->left_mi;
2786 const int above_in_image = !!xd->above_mi;
2787 MODE_INFO **prev_mi =
2788 &cm->prev_mi_grid_visible[mi_col + cm->mi_stride * mi_row];
2789 int above_par = 0; // above_partitioning
2790 int left_par = 0; // left_partitioning
2791 int last_par = 0; // last_partitioning
2792 BLOCK_SIZE context_size;
2796 assert(b_width_log2_lookup[bsize] == b_height_log2_lookup[bsize]);
2798 if (above_in_image) {
2799 context_size = xd->above_mi->sb_type;
2800 if (context_size < bsize)
2802 else if (context_size == bsize)
2806 if (left_in_image) {
2807 context_size = xd->left_mi->sb_type;
2808 if (context_size < bsize)
2810 else if (context_size == bsize)
2815 context_size = prev_mi[0]->sb_type;
2816 if (context_size < bsize)
2818 else if (context_size == bsize)
2822 if (bsize == BLOCK_64X64)
2824 else if (bsize == BLOCK_32X32)
2826 else if (bsize == BLOCK_16X16)
2829 // early termination score calculation
2830 clf = &classifiers[offset];
2831 mean = &train_mean[offset];
2832 sd = &train_stdm[offset];
2833 score = clf[0] * (((double)ctx->rate - mean[0]) / sd[0]) +
2834 clf[1] * (((double)ctx->dist - mean[1]) / sd[1]) +
2835 clf[2] * (((double)mag_mv / 2 - mean[2]) * sd[2]) +
2836 clf[3] * (((double)(left_par + above_par) / 2 - mean[3]) * sd[3]) +
2837 clf[4] * (((double)ctx->sum_y_eobs - mean[4]) / sd[4]) +
2838 clf[5] * (((double)cm->base_qindex - mean[5]) * sd[5]) +
2839 clf[6] * (((double)last_par - mean[6]) * sd[6]) + clf[7];
2843 // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
2844 // unlikely to be selected depending on previous rate-distortion optimization
2845 // results, for encoding speed-up.
2846 static void rd_pick_partition(VP9_COMP *cpi, ThreadData *td,
2847 TileDataEnc *tile_data, TOKENEXTRA **tp,
2848 int mi_row, int mi_col, BLOCK_SIZE bsize,
2849 RD_COST *rd_cost, int64_t best_rd,
2851 VP9_COMMON *const cm = &cpi->common;
2852 TileInfo *const tile_info = &tile_data->tile_info;
2853 MACROBLOCK *const x = &td->mb;
2854 MACROBLOCKD *const xd = &x->e_mbd;
2855 const int mi_step = num_8x8_blocks_wide_lookup[bsize] / 2;
2856 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2857 PARTITION_CONTEXT sl[8], sa[8];
2858 TOKENEXTRA *tp_orig = *tp;
2859 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
2861 const int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2863 RD_COST this_rdc, sum_rdc, best_rdc;
2864 int do_split = bsize >= BLOCK_8X8;
2866 INTERP_FILTER pred_interp_filter;
2868 // Override skipping rectangular partition operations for edge blocks
2869 const int force_horz_split = (mi_row + mi_step >= cm->mi_rows);
2870 const int force_vert_split = (mi_col + mi_step >= cm->mi_cols);
2871 const int xss = x->e_mbd.plane[1].subsampling_x;
2872 const int yss = x->e_mbd.plane[1].subsampling_y;
2874 BLOCK_SIZE min_size = x->min_partition_size;
2875 BLOCK_SIZE max_size = x->max_partition_size;
2877 #if CONFIG_FP_MB_STATS
2878 unsigned int src_diff_var = UINT_MAX;
2879 int none_complexity = 0;
2882 int partition_none_allowed = !force_horz_split && !force_vert_split;
2883 int partition_horz_allowed =
2884 !force_vert_split && yss <= xss && bsize >= BLOCK_8X8;
2885 int partition_vert_allowed =
2886 !force_horz_split && xss <= yss && bsize >= BLOCK_8X8;
2888 int64_t dist_breakout_thr = cpi->sf.partition_search_breakout_thr.dist;
2889 int rate_breakout_thr = cpi->sf.partition_search_breakout_thr.rate;
2893 assert(num_8x8_blocks_wide_lookup[bsize] ==
2894 num_8x8_blocks_high_lookup[bsize]);
2896 // Adjust dist breakout threshold according to the partition size.
2897 dist_breakout_thr >>=
2898 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
2899 rate_breakout_thr *= num_pels_log2_lookup[bsize];
2901 vp9_rd_cost_init(&this_rdc);
2902 vp9_rd_cost_init(&sum_rdc);
2903 vp9_rd_cost_reset(&best_rdc);
2904 best_rdc.rdcost = best_rd;
2906 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2908 if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode != NO_AQ &&
2909 cpi->oxcf.aq_mode != LOOKAHEAD_AQ)
2910 x->mb_energy = vp9_block_energy(cpi, x, bsize);
2912 if (cpi->sf.cb_partition_search && bsize == BLOCK_16X16) {
2913 int cb_partition_search_ctrl =
2914 ((pc_tree->index == 0 || pc_tree->index == 3) +
2915 get_chessboard_index(cm->current_video_frame)) &
2918 if (cb_partition_search_ctrl && bsize > min_size && bsize < max_size)
2919 set_partition_range(cm, xd, mi_row, mi_col, bsize, &min_size, &max_size);
2922 // Determine partition types in search according to the speed features.
2923 // The threshold set here has to be of square block size.
2924 if (cpi->sf.auto_min_max_partition_size) {
2925 partition_none_allowed &= (bsize <= max_size && bsize >= min_size);
2926 partition_horz_allowed &=
2927 ((bsize <= max_size && bsize > min_size) || force_horz_split);
2928 partition_vert_allowed &=
2929 ((bsize <= max_size && bsize > min_size) || force_vert_split);
2930 do_split &= bsize > min_size;
2933 if (cpi->sf.use_square_partition_only &&
2934 bsize > cpi->sf.use_square_only_threshold) {
2936 if (!vp9_active_h_edge(cpi, mi_row, mi_step) || x->e_mbd.lossless)
2937 partition_horz_allowed &= force_horz_split;
2938 if (!vp9_active_v_edge(cpi, mi_row, mi_step) || x->e_mbd.lossless)
2939 partition_vert_allowed &= force_vert_split;
2941 partition_horz_allowed &= force_horz_split;
2942 partition_vert_allowed &= force_vert_split;
2946 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2948 #if CONFIG_FP_MB_STATS
2949 if (cpi->use_fp_mb_stats) {
2950 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2951 src_diff_var = get_sby_perpixel_diff_variance(cpi, &x->plane[0].src, mi_row,
2956 #if CONFIG_FP_MB_STATS
2957 // Decide whether we shall split directly and skip searching NONE by using
2958 // the first pass block statistics
2959 if (cpi->use_fp_mb_stats && bsize >= BLOCK_32X32 && do_split &&
2960 partition_none_allowed && src_diff_var > 4 &&
2961 cm->base_qindex < qindex_split_threshold_lookup[bsize]) {
2962 int mb_row = mi_row >> 1;
2963 int mb_col = mi_col >> 1;
2965 VPXMIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
2967 VPXMIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
2970 // compute a complexity measure, basically measure inconsistency of motion
2971 // vectors obtained from the first pass in the current block
2972 for (r = mb_row; r < mb_row_end; r++) {
2973 for (c = mb_col; c < mb_col_end; c++) {
2974 const int mb_index = r * cm->mb_cols + c;
2976 MOTION_DIRECTION this_mv;
2977 MOTION_DIRECTION right_mv;
2978 MOTION_DIRECTION bottom_mv;
2981 get_motion_direction_fp(cpi->twopass.this_frame_mb_stats[mb_index]);
2984 if (c != mb_col_end - 1) {
2985 right_mv = get_motion_direction_fp(
2986 cpi->twopass.this_frame_mb_stats[mb_index + 1]);
2987 none_complexity += get_motion_inconsistency(this_mv, right_mv);
2991 if (r != mb_row_end - 1) {
2992 bottom_mv = get_motion_direction_fp(
2993 cpi->twopass.this_frame_mb_stats[mb_index + cm->mb_cols]);
2994 none_complexity += get_motion_inconsistency(this_mv, bottom_mv);
2997 // do not count its left and top neighbors to avoid double counting
3001 if (none_complexity > complexity_16x16_blocks_threshold[bsize]) {
3002 partition_none_allowed = 0;
3008 if (partition_none_allowed) {
3009 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &this_rdc, bsize, ctx,
3011 if (this_rdc.rate != INT_MAX) {
3012 if (bsize >= BLOCK_8X8) {
3013 this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
3015 RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
3018 if (this_rdc.rdcost < best_rdc.rdcost) {
3019 MODE_INFO *mi = xd->mi[0];
3021 best_rdc = this_rdc;
3022 if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
3024 if (!cpi->sf.ml_partition_search_early_termination) {
3025 // If all y, u, v transform blocks in this partition are skippable,
3026 // and the dist & rate are within the thresholds, the partition search
3027 // is terminated for current branch of the partition search tree.
3028 if (!x->e_mbd.lossless && ctx->skippable &&
3029 ((best_rdc.dist < (dist_breakout_thr >> 2)) ||
3030 (best_rdc.dist < dist_breakout_thr &&
3031 best_rdc.rate < rate_breakout_thr))) {
3036 // Currently, the machine-learning based partition search early
3037 // termination is only used while bsize is 16x16, 32x32 or 64x64,
3038 // VPXMIN(cm->width, cm->height) >= 480, and speed = 0.
3039 if (!x->e_mbd.lossless &&
3040 !segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP) &&
3041 ctx->mic.mode >= INTRA_MODES && bsize >= BLOCK_16X16) {
3042 if (compute_score(cm, xd, ctx, mi_row, mi_col, bsize) < 0.0) {
3049 #if CONFIG_FP_MB_STATS
3050 // Check if every 16x16 first pass block statistics has zero
3051 // motion and the corresponding first pass residue is small enough.
3052 // If that is the case, check the difference variance between the
3053 // current frame and the last frame. If the variance is small enough,
3054 // stop further splitting in RD optimization
3055 if (cpi->use_fp_mb_stats && do_split != 0 &&
3056 cm->base_qindex > qindex_skip_threshold_lookup[bsize]) {
3057 int mb_row = mi_row >> 1;
3058 int mb_col = mi_col >> 1;
3060 VPXMIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
3062 VPXMIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
3066 for (r = mb_row; r < mb_row_end; r++) {
3067 for (c = mb_col; c < mb_col_end; c++) {
3068 const int mb_index = r * cm->mb_cols + c;
3069 if (!(cpi->twopass.this_frame_mb_stats[mb_index] &
3070 FPMB_MOTION_ZERO_MASK) ||
3071 !(cpi->twopass.this_frame_mb_stats[mb_index] &
3072 FPMB_ERROR_SMALL_MASK)) {
3083 if (src_diff_var == UINT_MAX) {
3084 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
3085 src_diff_var = get_sby_perpixel_diff_variance(
3086 cpi, &x->plane[0].src, mi_row, mi_col, bsize);
3088 if (src_diff_var < 8) {
3097 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3100 // store estimated motion vector
3101 if (cpi->sf.adaptive_motion_search) store_pred_mv(x, ctx);
3103 // If the interp_filter is marked as SWITCHABLE_FILTERS, it was for an
3104 // intra block and used for context purposes.
3105 if (ctx->mic.interp_filter == SWITCHABLE_FILTERS) {
3106 pred_interp_filter = EIGHTTAP;
3108 pred_interp_filter = ctx->mic.interp_filter;
3112 // TODO(jingning): use the motion vectors given by the above search as
3113 // the starting point of motion search in the following partition type check.
3115 subsize = get_subsize(bsize, PARTITION_SPLIT);
3116 if (bsize == BLOCK_8X8) {
3118 if (cpi->sf.adaptive_pred_interp_filter && partition_none_allowed)
3119 pc_tree->leaf_split[0]->pred_interp_filter = pred_interp_filter;
3120 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3121 pc_tree->leaf_split[0], best_rdc.rdcost);
3123 if (sum_rdc.rate == INT_MAX) sum_rdc.rdcost = INT64_MAX;
3125 for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
3126 const int x_idx = (i & 1) * mi_step;
3127 const int y_idx = (i >> 1) * mi_step;
3129 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
3132 if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3134 pc_tree->split[i]->index = i;
3135 rd_pick_partition(cpi, td, tile_data, tp, mi_row + y_idx,
3136 mi_col + x_idx, subsize, &this_rdc,
3137 best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
3139 if (this_rdc.rate == INT_MAX) {
3140 sum_rdc.rdcost = INT64_MAX;
3143 sum_rdc.rate += this_rdc.rate;
3144 sum_rdc.dist += this_rdc.dist;
3145 sum_rdc.rdcost += this_rdc.rdcost;
3150 if (sum_rdc.rdcost < best_rdc.rdcost && i == 4) {
3151 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
3152 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3154 if (sum_rdc.rdcost < best_rdc.rdcost) {
3156 pc_tree->partitioning = PARTITION_SPLIT;
3158 // Rate and distortion based partition search termination clause.
3159 if (!cpi->sf.ml_partition_search_early_termination &&
3160 !x->e_mbd.lossless && ((best_rdc.dist < (dist_breakout_thr >> 2)) ||
3161 (best_rdc.dist < dist_breakout_thr &&
3162 best_rdc.rate < rate_breakout_thr))) {
3167 // skip rectangular partition test when larger block size
3168 // gives better rd cost
3169 if ((cpi->sf.less_rectangular_check) &&
3170 ((bsize > cpi->sf.use_square_only_threshold) ||
3171 (best_rdc.dist < dist_breakout_thr)))
3172 do_rect &= !partition_none_allowed;
3174 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3178 if (partition_horz_allowed &&
3179 (do_rect || vp9_active_h_edge(cpi, mi_row, mi_step))) {
3180 subsize = get_subsize(bsize, PARTITION_HORZ);
3181 if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3182 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3183 partition_none_allowed)
3184 pc_tree->horizontal[0].pred_interp_filter = pred_interp_filter;
3185 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3186 &pc_tree->horizontal[0], best_rdc.rdcost);
3188 if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + mi_step < cm->mi_rows &&
3189 bsize > BLOCK_8X8) {
3190 PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
3191 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
3192 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
3194 if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3195 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3196 partition_none_allowed)
3197 pc_tree->horizontal[1].pred_interp_filter = pred_interp_filter;
3198 rd_pick_sb_modes(cpi, tile_data, x, mi_row + mi_step, mi_col, &this_rdc,
3199 subsize, &pc_tree->horizontal[1],
3200 best_rdc.rdcost - sum_rdc.rdcost);
3201 if (this_rdc.rate == INT_MAX) {
3202 sum_rdc.rdcost = INT64_MAX;
3204 sum_rdc.rate += this_rdc.rate;
3205 sum_rdc.dist += this_rdc.dist;
3206 sum_rdc.rdcost += this_rdc.rdcost;
3210 if (sum_rdc.rdcost < best_rdc.rdcost) {
3211 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
3212 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3213 if (sum_rdc.rdcost < best_rdc.rdcost) {
3215 pc_tree->partitioning = PARTITION_HORZ;
3217 if ((cpi->sf.less_rectangular_check) &&
3218 (bsize > cpi->sf.use_square_only_threshold))
3222 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3226 if (partition_vert_allowed &&
3227 (do_rect || vp9_active_v_edge(cpi, mi_col, mi_step))) {
3228 subsize = get_subsize(bsize, PARTITION_VERT);
3230 if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3231 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3232 partition_none_allowed)
3233 pc_tree->vertical[0].pred_interp_filter = pred_interp_filter;
3234 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3235 &pc_tree->vertical[0], best_rdc.rdcost);
3236 if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + mi_step < cm->mi_cols &&
3237 bsize > BLOCK_8X8) {
3238 update_state(cpi, td, &pc_tree->vertical[0], mi_row, mi_col, subsize, 0);
3239 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize,
3240 &pc_tree->vertical[0]);
3242 if (cpi->sf.adaptive_motion_search) load_pred_mv(x, ctx);
3243 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
3244 partition_none_allowed)
3245 pc_tree->vertical[1].pred_interp_filter = pred_interp_filter;
3246 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + mi_step, &this_rdc,
3247 subsize, &pc_tree->vertical[1],
3248 best_rdc.rdcost - sum_rdc.rdcost);
3249 if (this_rdc.rate == INT_MAX) {
3250 sum_rdc.rdcost = INT64_MAX;
3252 sum_rdc.rate += this_rdc.rate;
3253 sum_rdc.dist += this_rdc.dist;
3254 sum_rdc.rdcost += this_rdc.rdcost;
3258 if (sum_rdc.rdcost < best_rdc.rdcost) {
3259 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
3260 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3261 if (sum_rdc.rdcost < best_rdc.rdcost) {
3263 pc_tree->partitioning = PARTITION_VERT;
3266 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
3269 // TODO(jbb): This code added so that we avoid static analysis
3270 // warning related to the fact that best_rd isn't used after this
3271 // point. This code should be refactored so that the duplicate
3272 // checks occur in some sub function and thus are used...
3274 *rd_cost = best_rdc;
3276 if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX &&
3277 pc_tree->index != 3) {
3278 int output_enabled = (bsize == BLOCK_64X64);
3279 encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
3283 if (bsize == BLOCK_64X64) {
3284 assert(tp_orig < *tp);
3285 assert(best_rdc.rate < INT_MAX);
3286 assert(best_rdc.dist < INT64_MAX);
3288 assert(tp_orig == *tp);
3292 static void encode_rd_sb_row(VP9_COMP *cpi, ThreadData *td,
3293 TileDataEnc *tile_data, int mi_row,
3295 VP9_COMMON *const cm = &cpi->common;
3296 TileInfo *const tile_info = &tile_data->tile_info;
3297 MACROBLOCK *const x = &td->mb;
3298 MACROBLOCKD *const xd = &x->e_mbd;
3299 SPEED_FEATURES *const sf = &cpi->sf;
3300 const int mi_col_start = tile_info->mi_col_start;
3301 const int mi_col_end = tile_info->mi_col_end;
3303 const int sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
3304 const int num_sb_cols =
3305 get_num_cols(tile_data->tile_info, MI_BLOCK_SIZE_LOG2);
3308 // Initialize the left context for the new SB row
3309 memset(&xd->left_context, 0, sizeof(xd->left_context));
3310 memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
3312 // Code each SB in the row
3313 for (mi_col = mi_col_start, sb_col_in_tile = 0; mi_col < mi_col_end;
3314 mi_col += MI_BLOCK_SIZE, sb_col_in_tile++) {
3315 const struct segmentation *const seg = &cm->seg;
3322 const int idx_str = cm->mi_stride * mi_row + mi_col;
3323 MODE_INFO **mi = cm->mi_grid_visible + idx_str;
3325 (*(cpi->row_mt_sync_read_ptr))(&tile_data->row_mt_sync, sb_row,
3328 if (sf->adaptive_pred_interp_filter) {
3329 for (i = 0; i < 64; ++i) td->leaf_tree[i].pred_interp_filter = SWITCHABLE;
3331 for (i = 0; i < 64; ++i) {
3332 td->pc_tree[i].vertical[0].pred_interp_filter = SWITCHABLE;
3333 td->pc_tree[i].vertical[1].pred_interp_filter = SWITCHABLE;
3334 td->pc_tree[i].horizontal[0].pred_interp_filter = SWITCHABLE;
3335 td->pc_tree[i].horizontal[1].pred_interp_filter = SWITCHABLE;
3339 vp9_zero(x->pred_mv);
3340 td->pc_root->index = 0;
3343 const uint8_t *const map =
3344 seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
3345 int segment_id = get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
3346 seg_skip = segfeature_active(seg, segment_id, SEG_LVL_SKIP);
3349 x->source_variance = UINT_MAX;
3350 if (sf->partition_search_type == FIXED_PARTITION || seg_skip) {
3351 const BLOCK_SIZE bsize =
3352 seg_skip ? BLOCK_64X64 : sf->always_this_block_size;
3353 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3354 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
3355 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
3356 &dummy_rate, &dummy_dist, 1, td->pc_root);
3357 } else if (cpi->partition_search_skippable_frame) {
3359 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3360 bsize = get_rd_var_based_fixed_partition(cpi, x, mi_row, mi_col);
3361 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
3362 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
3363 &dummy_rate, &dummy_dist, 1, td->pc_root);
3364 } else if (sf->partition_search_type == VAR_BASED_PARTITION &&
3365 cm->frame_type != KEY_FRAME) {
3366 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
3367 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, BLOCK_64X64,
3368 &dummy_rate, &dummy_dist, 1, td->pc_root);
3370 // If required set upper and lower partition size limits
3371 if (sf->auto_min_max_partition_size) {
3372 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3373 rd_auto_partition_range(cpi, tile_info, xd, mi_row, mi_col,
3374 &x->min_partition_size, &x->max_partition_size);
3376 rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, BLOCK_64X64,
3377 &dummy_rdc, INT64_MAX, td->pc_root);
3379 (*(cpi->row_mt_sync_write_ptr))(&tile_data->row_mt_sync, sb_row,
3380 sb_col_in_tile, num_sb_cols);
3384 static void init_encode_frame_mb_context(VP9_COMP *cpi) {
3385 MACROBLOCK *const x = &cpi->td.mb;
3386 VP9_COMMON *const cm = &cpi->common;
3387 MACROBLOCKD *const xd = &x->e_mbd;
3388 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
3390 // Copy data over into macro block data structures.
3391 vp9_setup_src_planes(x, cpi->Source, 0, 0);
3393 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
3395 // Note: this memset assumes above_context[0], [1] and [2]
3396 // are allocated as part of the same buffer.
3397 memset(xd->above_context[0], 0,
3398 sizeof(*xd->above_context[0]) * 2 * aligned_mi_cols * MAX_MB_PLANE);
3399 memset(xd->above_seg_context, 0,
3400 sizeof(*xd->above_seg_context) * aligned_mi_cols);
3403 static int check_dual_ref_flags(VP9_COMP *cpi) {
3404 const int ref_flags = cpi->ref_frame_flags;
3406 if (segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
3409 return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG) +
3410 !!(ref_flags & VP9_ALT_FLAG)) >= 2;
3414 static void reset_skip_tx_size(VP9_COMMON *cm, TX_SIZE max_tx_size) {
3416 const int mis = cm->mi_stride;
3417 MODE_INFO **mi_ptr = cm->mi_grid_visible;
3419 for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row, mi_ptr += mis) {
3420 for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
3421 if (mi_ptr[mi_col]->tx_size > max_tx_size)
3422 mi_ptr[mi_col]->tx_size = max_tx_size;
3427 static MV_REFERENCE_FRAME get_frame_type(const VP9_COMP *cpi) {
3428 if (frame_is_intra_only(&cpi->common))
3430 else if (cpi->rc.is_src_frame_alt_ref && cpi->refresh_golden_frame)
3431 return ALTREF_FRAME;
3432 else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
3433 return GOLDEN_FRAME;
3438 static TX_MODE select_tx_mode(const VP9_COMP *cpi, MACROBLOCKD *const xd) {
3439 if (xd->lossless) return ONLY_4X4;
3440 if (cpi->common.frame_type == KEY_FRAME && cpi->sf.use_nonrd_pick_mode)
3442 if (cpi->sf.tx_size_search_method == USE_LARGESTALL)
3444 else if (cpi->sf.tx_size_search_method == USE_FULL_RD ||
3445 cpi->sf.tx_size_search_method == USE_TX_8X8)
3446 return TX_MODE_SELECT;
3448 return cpi->common.tx_mode;
3451 static void hybrid_intra_mode_search(VP9_COMP *cpi, MACROBLOCK *const x,
3452 RD_COST *rd_cost, BLOCK_SIZE bsize,
3453 PICK_MODE_CONTEXT *ctx) {
3454 if (bsize < BLOCK_16X16)
3455 vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, INT64_MAX);
3457 vp9_pick_intra_mode(cpi, x, rd_cost, bsize, ctx);
3460 static void nonrd_pick_sb_modes(VP9_COMP *cpi, TileDataEnc *tile_data,
3461 MACROBLOCK *const x, int mi_row, int mi_col,
3462 RD_COST *rd_cost, BLOCK_SIZE bsize,
3463 PICK_MODE_CONTEXT *ctx) {
3464 VP9_COMMON *const cm = &cpi->common;
3465 TileInfo *const tile_info = &tile_data->tile_info;
3466 MACROBLOCKD *const xd = &x->e_mbd;
3468 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
3469 BLOCK_SIZE bs = VPXMAX(bsize, BLOCK_8X8); // processing unit block size
3470 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bs];
3471 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bs];
3474 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
3476 mi->sb_type = bsize;
3478 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
3479 struct macroblockd_plane *pd = &xd->plane[plane];
3480 memcpy(a + num_4x4_blocks_wide * plane, pd->above_context,
3481 (sizeof(a[0]) * num_4x4_blocks_wide) >> pd->subsampling_x);
3482 memcpy(l + num_4x4_blocks_high * plane, pd->left_context,
3483 (sizeof(l[0]) * num_4x4_blocks_high) >> pd->subsampling_y);
3486 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled)
3487 if (cyclic_refresh_segment_id_boosted(mi->segment_id))
3488 x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
3490 if (cm->frame_type == KEY_FRAME)
3491 hybrid_intra_mode_search(cpi, x, rd_cost, bsize, ctx);
3492 else if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP))
3493 set_mode_info_seg_skip(x, cm->tx_mode, rd_cost, bsize);
3494 else if (bsize >= BLOCK_8X8)
3495 vp9_pick_inter_mode(cpi, x, tile_data, mi_row, mi_col, rd_cost, bsize, ctx);
3497 vp9_pick_inter_mode_sub8x8(cpi, x, mi_row, mi_col, rd_cost, bsize, ctx);
3499 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
3501 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
3502 struct macroblockd_plane *pd = &xd->plane[plane];
3503 memcpy(pd->above_context, a + num_4x4_blocks_wide * plane,
3504 (sizeof(a[0]) * num_4x4_blocks_wide) >> pd->subsampling_x);
3505 memcpy(pd->left_context, l + num_4x4_blocks_high * plane,
3506 (sizeof(l[0]) * num_4x4_blocks_high) >> pd->subsampling_y);
3509 if (rd_cost->rate == INT_MAX) vp9_rd_cost_reset(rd_cost);
3511 ctx->rate = rd_cost->rate;
3512 ctx->dist = rd_cost->dist;
3515 static void fill_mode_info_sb(VP9_COMMON *cm, MACROBLOCK *x, int mi_row,
3516 int mi_col, BLOCK_SIZE bsize, PC_TREE *pc_tree) {
3517 MACROBLOCKD *xd = &x->e_mbd;
3518 int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3519 PARTITION_TYPE partition = pc_tree->partitioning;
3520 BLOCK_SIZE subsize = get_subsize(bsize, partition);
3522 assert(bsize >= BLOCK_8X8);
3524 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
3526 switch (partition) {
3527 case PARTITION_NONE:
3528 set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
3529 *(xd->mi[0]) = pc_tree->none.mic;
3530 *(x->mbmi_ext) = pc_tree->none.mbmi_ext;
3531 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
3533 case PARTITION_VERT:
3534 set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
3535 *(xd->mi[0]) = pc_tree->vertical[0].mic;
3536 *(x->mbmi_ext) = pc_tree->vertical[0].mbmi_ext;
3537 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
3539 if (mi_col + hbs < cm->mi_cols) {
3540 set_mode_info_offsets(cm, x, xd, mi_row, mi_col + hbs);
3541 *(xd->mi[0]) = pc_tree->vertical[1].mic;
3542 *(x->mbmi_ext) = pc_tree->vertical[1].mbmi_ext;
3543 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col + hbs, subsize);
3546 case PARTITION_HORZ:
3547 set_mode_info_offsets(cm, x, xd, mi_row, mi_col);
3548 *(xd->mi[0]) = pc_tree->horizontal[0].mic;
3549 *(x->mbmi_ext) = pc_tree->horizontal[0].mbmi_ext;
3550 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
3551 if (mi_row + hbs < cm->mi_rows) {
3552 set_mode_info_offsets(cm, x, xd, mi_row + hbs, mi_col);
3553 *(xd->mi[0]) = pc_tree->horizontal[1].mic;
3554 *(x->mbmi_ext) = pc_tree->horizontal[1].mbmi_ext;
3555 duplicate_mode_info_in_sb(cm, xd, mi_row + hbs, mi_col, subsize);
3558 case PARTITION_SPLIT: {
3559 fill_mode_info_sb(cm, x, mi_row, mi_col, subsize, pc_tree->split[0]);
3560 fill_mode_info_sb(cm, x, mi_row, mi_col + hbs, subsize,
3562 fill_mode_info_sb(cm, x, mi_row + hbs, mi_col, subsize,
3564 fill_mode_info_sb(cm, x, mi_row + hbs, mi_col + hbs, subsize,
3572 // Reset the prediction pixel ready flag recursively.
3573 static void pred_pixel_ready_reset(PC_TREE *pc_tree, BLOCK_SIZE bsize) {
3574 pc_tree->none.pred_pixel_ready = 0;
3575 pc_tree->horizontal[0].pred_pixel_ready = 0;
3576 pc_tree->horizontal[1].pred_pixel_ready = 0;
3577 pc_tree->vertical[0].pred_pixel_ready = 0;
3578 pc_tree->vertical[1].pred_pixel_ready = 0;
3580 if (bsize > BLOCK_8X8) {
3581 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_SPLIT);
3583 for (i = 0; i < 4; ++i) pred_pixel_ready_reset(pc_tree->split[i], subsize);
3587 static void nonrd_pick_partition(VP9_COMP *cpi, ThreadData *td,
3588 TileDataEnc *tile_data, TOKENEXTRA **tp,
3589 int mi_row, int mi_col, BLOCK_SIZE bsize,
3590 RD_COST *rd_cost, int do_recon,
3591 int64_t best_rd, PC_TREE *pc_tree) {
3592 const SPEED_FEATURES *const sf = &cpi->sf;
3593 VP9_COMMON *const cm = &cpi->common;
3594 TileInfo *const tile_info = &tile_data->tile_info;
3595 MACROBLOCK *const x = &td->mb;
3596 MACROBLOCKD *const xd = &x->e_mbd;
3597 const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
3598 TOKENEXTRA *tp_orig = *tp;
3599 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
3601 BLOCK_SIZE subsize = bsize;
3602 RD_COST this_rdc, sum_rdc, best_rdc;
3603 int do_split = bsize >= BLOCK_8X8;
3605 // Override skipping rectangular partition operations for edge blocks
3606 const int force_horz_split = (mi_row + ms >= cm->mi_rows);
3607 const int force_vert_split = (mi_col + ms >= cm->mi_cols);
3608 const int xss = x->e_mbd.plane[1].subsampling_x;
3609 const int yss = x->e_mbd.plane[1].subsampling_y;
3611 int partition_none_allowed = !force_horz_split && !force_vert_split;
3612 int partition_horz_allowed =
3613 !force_vert_split && yss <= xss && bsize >= BLOCK_8X8;
3614 int partition_vert_allowed =
3615 !force_horz_split && xss <= yss && bsize >= BLOCK_8X8;
3618 assert(num_8x8_blocks_wide_lookup[bsize] ==
3619 num_8x8_blocks_high_lookup[bsize]);
3621 vp9_rd_cost_init(&sum_rdc);
3622 vp9_rd_cost_reset(&best_rdc);
3623 best_rdc.rdcost = best_rd;
3625 // Determine partition types in search according to the speed features.
3626 // The threshold set here has to be of square block size.
3627 if (sf->auto_min_max_partition_size) {
3628 partition_none_allowed &=
3629 (bsize <= x->max_partition_size && bsize >= x->min_partition_size);
3630 partition_horz_allowed &=
3631 ((bsize <= x->max_partition_size && bsize > x->min_partition_size) ||
3633 partition_vert_allowed &=
3634 ((bsize <= x->max_partition_size && bsize > x->min_partition_size) ||
3636 do_split &= bsize > x->min_partition_size;
3638 if (sf->use_square_partition_only) {
3639 partition_horz_allowed &= force_horz_split;
3640 partition_vert_allowed &= force_vert_split;
3643 ctx->pred_pixel_ready =
3644 !(partition_vert_allowed || partition_horz_allowed || do_split);
3647 if (partition_none_allowed) {
3648 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &this_rdc, bsize,
3650 ctx->mic = *xd->mi[0];
3651 ctx->mbmi_ext = *x->mbmi_ext;
3652 ctx->skip_txfm[0] = x->skip_txfm[0];
3653 ctx->skip = x->skip;
3655 if (this_rdc.rate != INT_MAX) {
3656 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3657 this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
3659 RDCOST(x->rdmult, x->rddiv, this_rdc.rate, this_rdc.dist);
3660 if (this_rdc.rdcost < best_rdc.rdcost) {
3661 int64_t dist_breakout_thr = sf->partition_search_breakout_thr.dist;
3662 int64_t rate_breakout_thr = sf->partition_search_breakout_thr.rate;
3664 dist_breakout_thr >>=
3665 8 - (b_width_log2_lookup[bsize] + b_height_log2_lookup[bsize]);
3667 rate_breakout_thr *= num_pels_log2_lookup[bsize];
3669 best_rdc = this_rdc;
3670 if (bsize >= BLOCK_8X8) pc_tree->partitioning = PARTITION_NONE;
3672 if (!x->e_mbd.lossless && this_rdc.rate < rate_breakout_thr &&
3673 this_rdc.dist < dist_breakout_thr) {
3681 // store estimated motion vector
3682 store_pred_mv(x, ctx);
3686 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3687 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
3688 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3689 subsize = get_subsize(bsize, PARTITION_SPLIT);
3690 for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
3691 const int x_idx = (i & 1) * ms;
3692 const int y_idx = (i >> 1) * ms;
3694 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
3696 load_pred_mv(x, ctx);
3697 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row + y_idx,
3698 mi_col + x_idx, subsize, &this_rdc, 0,
3699 best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
3701 if (this_rdc.rate == INT_MAX) {
3702 vp9_rd_cost_reset(&sum_rdc);
3704 sum_rdc.rate += this_rdc.rate;
3705 sum_rdc.dist += this_rdc.dist;
3706 sum_rdc.rdcost += this_rdc.rdcost;
3710 if (sum_rdc.rdcost < best_rdc.rdcost) {
3712 pc_tree->partitioning = PARTITION_SPLIT;
3714 // skip rectangular partition test when larger block size
3715 // gives better rd cost
3716 if (sf->less_rectangular_check) do_rect &= !partition_none_allowed;
3721 if (partition_horz_allowed && do_rect) {
3722 subsize = get_subsize(bsize, PARTITION_HORZ);
3723 if (sf->adaptive_motion_search) load_pred_mv(x, ctx);
3724 pc_tree->horizontal[0].pred_pixel_ready = 1;
3725 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3726 &pc_tree->horizontal[0]);
3728 pc_tree->horizontal[0].mic = *xd->mi[0];
3729 pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
3730 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3731 pc_tree->horizontal[0].skip = x->skip;
3733 if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + ms < cm->mi_rows) {
3734 load_pred_mv(x, ctx);
3735 pc_tree->horizontal[1].pred_pixel_ready = 1;
3736 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + ms, mi_col, &this_rdc,
3737 subsize, &pc_tree->horizontal[1]);
3739 pc_tree->horizontal[1].mic = *xd->mi[0];
3740 pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
3741 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3742 pc_tree->horizontal[1].skip = x->skip;
3744 if (this_rdc.rate == INT_MAX) {
3745 vp9_rd_cost_reset(&sum_rdc);
3747 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3748 this_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
3749 sum_rdc.rate += this_rdc.rate;
3750 sum_rdc.dist += this_rdc.dist;
3752 RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3756 if (sum_rdc.rdcost < best_rdc.rdcost) {
3758 pc_tree->partitioning = PARTITION_HORZ;
3760 pred_pixel_ready_reset(pc_tree, bsize);
3765 if (partition_vert_allowed && do_rect) {
3766 subsize = get_subsize(bsize, PARTITION_VERT);
3767 if (sf->adaptive_motion_search) load_pred_mv(x, ctx);
3768 pc_tree->vertical[0].pred_pixel_ready = 1;
3769 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3770 &pc_tree->vertical[0]);
3771 pc_tree->vertical[0].mic = *xd->mi[0];
3772 pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
3773 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
3774 pc_tree->vertical[0].skip = x->skip;
3776 if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + ms < cm->mi_cols) {
3777 load_pred_mv(x, ctx);
3778 pc_tree->vertical[1].pred_pixel_ready = 1;
3779 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + ms, &this_rdc,
3780 subsize, &pc_tree->vertical[1]);
3781 pc_tree->vertical[1].mic = *xd->mi[0];
3782 pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
3783 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
3784 pc_tree->vertical[1].skip = x->skip;
3786 if (this_rdc.rate == INT_MAX) {
3787 vp9_rd_cost_reset(&sum_rdc);
3789 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3790 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
3791 sum_rdc.rate += this_rdc.rate;
3792 sum_rdc.dist += this_rdc.dist;
3794 RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3798 if (sum_rdc.rdcost < best_rdc.rdcost) {
3800 pc_tree->partitioning = PARTITION_VERT;
3802 pred_pixel_ready_reset(pc_tree, bsize);
3806 *rd_cost = best_rdc;
3808 if (best_rdc.rate == INT_MAX) {
3809 vp9_rd_cost_reset(rd_cost);
3813 // update mode info array
3814 fill_mode_info_sb(cm, x, mi_row, mi_col, bsize, pc_tree);
3816 if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX && do_recon) {
3817 int output_enabled = (bsize == BLOCK_64X64);
3818 encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
3822 if (bsize == BLOCK_64X64 && do_recon) {
3823 assert(tp_orig < *tp);
3824 assert(best_rdc.rate < INT_MAX);
3825 assert(best_rdc.dist < INT64_MAX);
3827 assert(tp_orig == *tp);
3831 static void nonrd_select_partition(VP9_COMP *cpi, ThreadData *td,
3832 TileDataEnc *tile_data, MODE_INFO **mi,
3833 TOKENEXTRA **tp, int mi_row, int mi_col,
3834 BLOCK_SIZE bsize, int output_enabled,
3835 RD_COST *rd_cost, PC_TREE *pc_tree) {
3836 VP9_COMMON *const cm = &cpi->common;
3837 TileInfo *const tile_info = &tile_data->tile_info;
3838 MACROBLOCK *const x = &td->mb;
3839 MACROBLOCKD *const xd = &x->e_mbd;
3840 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3841 const int mis = cm->mi_stride;
3842 PARTITION_TYPE partition;
3846 vp9_rd_cost_reset(&this_rdc);
3847 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
3849 subsize = (bsize >= BLOCK_8X8) ? mi[0]->sb_type : BLOCK_4X4;
3850 partition = partition_lookup[bsl][subsize];
3852 if (bsize == BLOCK_32X32 && subsize == BLOCK_32X32) {
3853 x->max_partition_size = BLOCK_32X32;
3854 x->min_partition_size = BLOCK_16X16;
3855 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
3856 0, INT64_MAX, pc_tree);
3857 } else if (bsize == BLOCK_32X32 && partition != PARTITION_NONE &&
3858 subsize >= BLOCK_16X16) {
3859 x->max_partition_size = BLOCK_32X32;
3860 x->min_partition_size = BLOCK_8X8;
3861 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
3862 0, INT64_MAX, pc_tree);
3863 } else if (bsize == BLOCK_16X16 && partition != PARTITION_NONE) {
3864 x->max_partition_size = BLOCK_16X16;
3865 x->min_partition_size = BLOCK_8X8;
3866 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize, rd_cost,
3867 0, INT64_MAX, pc_tree);
3869 switch (partition) {
3870 case PARTITION_NONE:
3871 pc_tree->none.pred_pixel_ready = 1;
3872 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
3874 pc_tree->none.mic = *xd->mi[0];
3875 pc_tree->none.mbmi_ext = *x->mbmi_ext;
3876 pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
3877 pc_tree->none.skip = x->skip;
3879 case PARTITION_VERT:
3880 pc_tree->vertical[0].pred_pixel_ready = 1;
3881 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
3882 &pc_tree->vertical[0]);
3883 pc_tree->vertical[0].mic = *xd->mi[0];
3884 pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
3885 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
3886 pc_tree->vertical[0].skip = x->skip;
3887 if (mi_col + hbs < cm->mi_cols) {
3888 pc_tree->vertical[1].pred_pixel_ready = 1;
3889 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs,
3890 &this_rdc, subsize, &pc_tree->vertical[1]);
3891 pc_tree->vertical[1].mic = *xd->mi[0];
3892 pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
3893 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
3894 pc_tree->vertical[1].skip = x->skip;
3895 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3896 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3897 rd_cost->rate += this_rdc.rate;
3898 rd_cost->dist += this_rdc.dist;
3902 case PARTITION_HORZ:
3903 pc_tree->horizontal[0].pred_pixel_ready = 1;
3904 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost, subsize,
3905 &pc_tree->horizontal[0]);
3906 pc_tree->horizontal[0].mic = *xd->mi[0];
3907 pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
3908 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3909 pc_tree->horizontal[0].skip = x->skip;
3910 if (mi_row + hbs < cm->mi_rows) {
3911 pc_tree->horizontal[1].pred_pixel_ready = 1;
3912 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col,
3913 &this_rdc, subsize, &pc_tree->horizontal[1]);
3914 pc_tree->horizontal[1].mic = *xd->mi[0];
3915 pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
3916 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3917 pc_tree->horizontal[1].skip = x->skip;
3918 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3919 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3920 rd_cost->rate += this_rdc.rate;
3921 rd_cost->dist += this_rdc.dist;
3925 case PARTITION_SPLIT:
3926 subsize = get_subsize(bsize, PARTITION_SPLIT);
3927 nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3928 subsize, output_enabled, rd_cost,
3930 nonrd_select_partition(cpi, td, tile_data, mi + hbs, tp, mi_row,
3931 mi_col + hbs, subsize, output_enabled, &this_rdc,
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;
3938 nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis, tp,
3939 mi_row + hbs, mi_col, subsize, output_enabled,
3940 &this_rdc, pc_tree->split[2]);
3941 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3942 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3943 rd_cost->rate += this_rdc.rate;
3944 rd_cost->dist += this_rdc.dist;
3946 nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
3947 mi_row + hbs, mi_col + hbs, subsize,
3948 output_enabled, &this_rdc, pc_tree->split[3]);
3949 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3950 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3951 rd_cost->rate += this_rdc.rate;
3952 rd_cost->dist += this_rdc.dist;
3955 default: assert(0 && "Invalid partition type."); break;
3959 if (bsize == BLOCK_64X64 && output_enabled)
3960 encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, 1, bsize, pc_tree);
3963 static void nonrd_use_partition(VP9_COMP *cpi, ThreadData *td,
3964 TileDataEnc *tile_data, MODE_INFO **mi,
3965 TOKENEXTRA **tp, int mi_row, int mi_col,
3966 BLOCK_SIZE bsize, int output_enabled,
3967 RD_COST *dummy_cost, PC_TREE *pc_tree) {
3968 VP9_COMMON *const cm = &cpi->common;
3969 TileInfo *tile_info = &tile_data->tile_info;
3970 MACROBLOCK *const x = &td->mb;
3971 MACROBLOCKD *const xd = &x->e_mbd;
3972 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3973 const int mis = cm->mi_stride;
3974 PARTITION_TYPE partition;
3977 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
3979 subsize = (bsize >= BLOCK_8X8) ? mi[0]->sb_type : BLOCK_4X4;
3980 partition = partition_lookup[bsl][subsize];
3982 if (output_enabled && bsize != BLOCK_4X4) {
3983 int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
3984 td->counts->partition[ctx][partition]++;
3987 switch (partition) {
3988 case PARTITION_NONE:
3989 pc_tree->none.pred_pixel_ready = 1;
3990 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
3991 subsize, &pc_tree->none);
3992 pc_tree->none.mic = *xd->mi[0];
3993 pc_tree->none.mbmi_ext = *x->mbmi_ext;
3994 pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
3995 pc_tree->none.skip = x->skip;
3996 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
3997 subsize, &pc_tree->none);
3999 case PARTITION_VERT:
4000 pc_tree->vertical[0].pred_pixel_ready = 1;
4001 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4002 subsize, &pc_tree->vertical[0]);
4003 pc_tree->vertical[0].mic = *xd->mi[0];
4004 pc_tree->vertical[0].mbmi_ext = *x->mbmi_ext;
4005 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
4006 pc_tree->vertical[0].skip = x->skip;
4007 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4008 subsize, &pc_tree->vertical[0]);
4009 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
4010 pc_tree->vertical[1].pred_pixel_ready = 1;
4011 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs, dummy_cost,
4012 subsize, &pc_tree->vertical[1]);
4013 pc_tree->vertical[1].mic = *xd->mi[0];
4014 pc_tree->vertical[1].mbmi_ext = *x->mbmi_ext;
4015 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
4016 pc_tree->vertical[1].skip = x->skip;
4017 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col + hbs,
4018 output_enabled, subsize, &pc_tree->vertical[1]);
4021 case PARTITION_HORZ:
4022 pc_tree->horizontal[0].pred_pixel_ready = 1;
4023 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4024 subsize, &pc_tree->horizontal[0]);
4025 pc_tree->horizontal[0].mic = *xd->mi[0];
4026 pc_tree->horizontal[0].mbmi_ext = *x->mbmi_ext;
4027 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
4028 pc_tree->horizontal[0].skip = x->skip;
4029 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4030 subsize, &pc_tree->horizontal[0]);
4032 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
4033 pc_tree->horizontal[1].pred_pixel_ready = 1;
4034 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col, dummy_cost,
4035 subsize, &pc_tree->horizontal[1]);
4036 pc_tree->horizontal[1].mic = *xd->mi[0];
4037 pc_tree->horizontal[1].mbmi_ext = *x->mbmi_ext;
4038 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
4039 pc_tree->horizontal[1].skip = x->skip;
4040 encode_b_rt(cpi, td, tile_info, tp, mi_row + hbs, mi_col,
4041 output_enabled, subsize, &pc_tree->horizontal[1]);
4044 case PARTITION_SPLIT:
4045 subsize = get_subsize(bsize, PARTITION_SPLIT);
4046 if (bsize == BLOCK_8X8) {
4047 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
4048 subsize, pc_tree->leaf_split[0]);
4049 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
4050 subsize, pc_tree->leaf_split[0]);
4052 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, subsize,
4053 output_enabled, dummy_cost, pc_tree->split[0]);
4054 nonrd_use_partition(cpi, td, tile_data, mi + hbs, tp, mi_row,
4055 mi_col + hbs, subsize, output_enabled, dummy_cost,
4057 nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis, tp,
4058 mi_row + hbs, mi_col, subsize, output_enabled,
4059 dummy_cost, pc_tree->split[2]);
4060 nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
4061 mi_row + hbs, mi_col + hbs, subsize, output_enabled,
4062 dummy_cost, pc_tree->split[3]);
4065 default: assert(0 && "Invalid partition type."); break;
4068 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
4069 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
4072 static void encode_nonrd_sb_row(VP9_COMP *cpi, ThreadData *td,
4073 TileDataEnc *tile_data, int mi_row,
4075 SPEED_FEATURES *const sf = &cpi->sf;
4076 VP9_COMMON *const cm = &cpi->common;
4077 TileInfo *const tile_info = &tile_data->tile_info;
4078 MACROBLOCK *const x = &td->mb;
4079 MACROBLOCKD *const xd = &x->e_mbd;
4080 const int mi_col_start = tile_info->mi_col_start;
4081 const int mi_col_end = tile_info->mi_col_end;
4083 const int sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
4084 const int num_sb_cols =
4085 get_num_cols(tile_data->tile_info, MI_BLOCK_SIZE_LOG2);
4088 // Initialize the left context for the new SB row
4089 memset(&xd->left_context, 0, sizeof(xd->left_context));
4090 memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
4092 // Code each SB in the row
4093 for (mi_col = mi_col_start, sb_col_in_tile = 0; mi_col < mi_col_end;
4094 mi_col += MI_BLOCK_SIZE, ++sb_col_in_tile) {
4095 const struct segmentation *const seg = &cm->seg;
4097 const int idx_str = cm->mi_stride * mi_row + mi_col;
4098 MODE_INFO **mi = cm->mi_grid_visible + idx_str;
4099 PARTITION_SEARCH_TYPE partition_search_type = sf->partition_search_type;
4100 BLOCK_SIZE bsize = BLOCK_64X64;
4103 (*(cpi->row_mt_sync_read_ptr))(&tile_data->row_mt_sync, sb_row,
4106 x->source_variance = UINT_MAX;
4107 vp9_zero(x->pred_mv);
4108 vp9_rd_cost_init(&dummy_rdc);
4109 x->color_sensitivity[0] = 0;
4110 x->color_sensitivity[1] = 0;
4112 x->skip_low_source_sad = 0;
4113 x->content_state_sb = 0;
4116 const uint8_t *const map =
4117 seg->update_map ? cpi->segmentation_map : cm->last_frame_seg_map;
4118 int segment_id = get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
4119 seg_skip = segfeature_active(seg, segment_id, SEG_LVL_SKIP);
4121 partition_search_type = FIXED_PARTITION;
4125 if (cpi->compute_source_sad_onepass && cpi->sf.use_source_sad) {
4126 int shift = cpi->Source->y_stride * (mi_row << 3) + (mi_col << 3);
4127 int sb_offset2 = ((cm->mi_cols + 7) >> 3) * (mi_row >> 3) + (mi_col >> 3);
4128 avg_source_sad(cpi, x, shift, sb_offset2);
4131 // Set the partition type of the 64X64 block
4132 switch (partition_search_type) {
4133 case VAR_BASED_PARTITION:
4134 // TODO(jingning, marpan): The mode decision and encoding process
4135 // support both intra and inter sub8x8 block coding for RTC mode.
4136 // Tune the thresholds accordingly to use sub8x8 block coding for
4137 // coding performance improvement.
4138 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
4139 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4140 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4142 case SOURCE_VAR_BASED_PARTITION:
4143 set_source_var_based_partition(cpi, tile_info, x, mi, mi_row, mi_col);
4144 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4145 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4147 case FIXED_PARTITION:
4148 if (!seg_skip) bsize = sf->always_this_block_size;
4149 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
4150 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4151 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4153 case REFERENCE_PARTITION:
4154 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
4155 // Use nonrd_pick_partition on scene-cut for VBR mode.
4156 // nonrd_pick_partition does not support 4x4 partition, so avoid it
4157 // on key frame for now.
4158 if ((cpi->oxcf.rc_mode == VPX_VBR && cpi->rc.high_source_sad &&
4159 cm->frame_type != KEY_FRAME)) {
4160 // Use lower max_partition_size for low resoultions.
4161 if (cm->width <= 352 && cm->height <= 288)
4162 x->max_partition_size = BLOCK_32X32;
4164 x->max_partition_size = BLOCK_64X64;
4165 x->min_partition_size = BLOCK_8X8;
4166 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col,
4167 BLOCK_64X64, &dummy_rdc, 1, INT64_MAX,
4170 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
4171 // TODO(marpan): Seems like nonrd_select_partition does not support
4172 // 4x4 partition. Since 4x4 is used on key frame, use this switch
4174 if (cm->frame_type == KEY_FRAME)
4175 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4176 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4178 nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
4179 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
4183 default: assert(0); break;
4186 (*(cpi->row_mt_sync_write_ptr))(&tile_data->row_mt_sync, sb_row,
4187 sb_col_in_tile, num_sb_cols);
4190 // end RTC play code
4192 static int set_var_thresh_from_histogram(VP9_COMP *cpi) {
4193 const SPEED_FEATURES *const sf = &cpi->sf;
4194 const VP9_COMMON *const cm = &cpi->common;
4196 const uint8_t *src = cpi->Source->y_buffer;
4197 const uint8_t *last_src = cpi->Last_Source->y_buffer;
4198 const int src_stride = cpi->Source->y_stride;
4199 const int last_stride = cpi->Last_Source->y_stride;
4201 // Pick cutoff threshold
4202 const int cutoff = (VPXMIN(cm->width, cm->height) >= 720)
4203 ? (cm->MBs * VAR_HIST_LARGE_CUT_OFF / 100)
4204 : (cm->MBs * VAR_HIST_SMALL_CUT_OFF / 100);
4205 DECLARE_ALIGNED(16, int, hist[VAR_HIST_BINS]);
4206 diff *var16 = cpi->source_diff_var;
4211 memset(hist, 0, VAR_HIST_BINS * sizeof(hist[0]));
4213 for (i = 0; i < cm->mb_rows; i++) {
4214 for (j = 0; j < cm->mb_cols; j++) {
4215 #if CONFIG_VP9_HIGHBITDEPTH
4216 if (cm->use_highbitdepth) {
4217 switch (cm->bit_depth) {
4219 vpx_highbd_8_get16x16var(src, src_stride, last_src, last_stride,
4220 &var16->sse, &var16->sum);
4223 vpx_highbd_10_get16x16var(src, src_stride, last_src, last_stride,
4224 &var16->sse, &var16->sum);
4227 vpx_highbd_12_get16x16var(src, src_stride, last_src, last_stride,
4228 &var16->sse, &var16->sum);
4232 "cm->bit_depth should be VPX_BITS_8, VPX_BITS_10"
4237 vpx_get16x16var(src, src_stride, last_src, last_stride, &var16->sse,
4241 vpx_get16x16var(src, src_stride, last_src, last_stride, &var16->sse,
4243 #endif // CONFIG_VP9_HIGHBITDEPTH
4244 var16->var = var16->sse - (((uint32_t)var16->sum * var16->sum) >> 8);
4246 if (var16->var >= VAR_HIST_MAX_BG_VAR)
4247 hist[VAR_HIST_BINS - 1]++;
4249 hist[var16->var / VAR_HIST_FACTOR]++;
4256 src = src - cm->mb_cols * 16 + 16 * src_stride;
4257 last_src = last_src - cm->mb_cols * 16 + 16 * last_stride;
4260 cpi->source_var_thresh = 0;
4262 if (hist[VAR_HIST_BINS - 1] < cutoff) {
4263 for (i = 0; i < VAR_HIST_BINS - 1; i++) {
4267 cpi->source_var_thresh = (i + 1) * VAR_HIST_FACTOR;
4273 return sf->search_type_check_frequency;
4276 static void source_var_based_partition_search_method(VP9_COMP *cpi) {
4277 VP9_COMMON *const cm = &cpi->common;
4278 SPEED_FEATURES *const sf = &cpi->sf;
4280 if (cm->frame_type == KEY_FRAME) {
4281 // For key frame, use SEARCH_PARTITION.
4282 sf->partition_search_type = SEARCH_PARTITION;
4283 } else if (cm->intra_only) {
4284 sf->partition_search_type = FIXED_PARTITION;
4286 if (cm->last_width != cm->width || cm->last_height != cm->height) {
4287 if (cpi->source_diff_var) vpx_free(cpi->source_diff_var);
4289 CHECK_MEM_ERROR(cm, cpi->source_diff_var,
4290 vpx_calloc(cm->MBs, sizeof(diff)));
4293 if (!cpi->frames_till_next_var_check)
4294 cpi->frames_till_next_var_check = set_var_thresh_from_histogram(cpi);
4296 if (cpi->frames_till_next_var_check > 0) {
4297 sf->partition_search_type = FIXED_PARTITION;
4298 cpi->frames_till_next_var_check--;
4303 static int get_skip_encode_frame(const VP9_COMMON *cm, ThreadData *const td) {
4304 unsigned int intra_count = 0, inter_count = 0;
4307 for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
4308 intra_count += td->counts->intra_inter[j][0];
4309 inter_count += td->counts->intra_inter[j][1];
4312 return (intra_count << 2) < inter_count && cm->frame_type != KEY_FRAME &&
4316 void vp9_init_tile_data(VP9_COMP *cpi) {
4317 VP9_COMMON *const cm = &cpi->common;
4318 const int tile_cols = 1 << cm->log2_tile_cols;
4319 const int tile_rows = 1 << cm->log2_tile_rows;
4320 int tile_col, tile_row;
4321 TOKENEXTRA *pre_tok = cpi->tile_tok[0][0];
4322 TOKENLIST *tplist = cpi->tplist[0][0];
4324 int tplist_count = 0;
4326 if (cpi->tile_data == NULL || cpi->allocated_tiles < tile_cols * tile_rows) {
4327 if (cpi->tile_data != NULL) vpx_free(cpi->tile_data);
4328 CHECK_MEM_ERROR(cm, cpi->tile_data, vpx_malloc(tile_cols * tile_rows *
4329 sizeof(*cpi->tile_data)));
4330 cpi->allocated_tiles = tile_cols * tile_rows;
4332 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
4333 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
4334 TileDataEnc *tile_data =
4335 &cpi->tile_data[tile_row * tile_cols + tile_col];
4337 for (i = 0; i < BLOCK_SIZES; ++i) {
4338 for (j = 0; j < MAX_MODES; ++j) {
4339 tile_data->thresh_freq_fact[i][j] = RD_THRESH_INIT_FACT;
4340 tile_data->mode_map[i][j] = j;
4343 #if CONFIG_MULTITHREAD
4344 tile_data->search_count_mutex = NULL;
4345 tile_data->enc_row_mt_mutex = NULL;
4346 tile_data->row_base_thresh_freq_fact = NULL;
4351 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
4352 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
4353 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
4354 TileInfo *tile_info = &this_tile->tile_info;
4355 vp9_tile_init(tile_info, cm, tile_row, tile_col);
4357 cpi->tile_tok[tile_row][tile_col] = pre_tok + tile_tok;
4358 pre_tok = cpi->tile_tok[tile_row][tile_col];
4359 tile_tok = allocated_tokens(*tile_info);
4361 cpi->tplist[tile_row][tile_col] = tplist + tplist_count;
4362 tplist = cpi->tplist[tile_row][tile_col];
4363 tplist_count = get_num_vert_units(*tile_info, MI_BLOCK_SIZE_LOG2);
4365 // Set up pointers to per thread motion search counters.
4366 this_tile->m_search_count = 0; // Count of motion search hits.
4367 this_tile->ex_search_count = 0; // Exhaustive mesh search hits.
4372 void vp9_encode_sb_row(VP9_COMP *cpi, ThreadData *td, int tile_row,
4373 int tile_col, int mi_row) {
4374 VP9_COMMON *const cm = &cpi->common;
4375 const int tile_cols = 1 << cm->log2_tile_cols;
4376 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
4377 const TileInfo *const tile_info = &this_tile->tile_info;
4378 TOKENEXTRA *tok = NULL;
4380 int tile_mb_cols = (tile_info->mi_col_end - tile_info->mi_col_start + 1) >> 1;
4382 tile_sb_row = mi_cols_aligned_to_sb(mi_row - tile_info->mi_row_start) >>
4384 get_start_tok(cpi, tile_row, tile_col, mi_row, &tok);
4385 cpi->tplist[tile_row][tile_col][tile_sb_row].start = tok;
4387 if (cpi->sf.use_nonrd_pick_mode)
4388 encode_nonrd_sb_row(cpi, td, this_tile, mi_row, &tok);
4390 encode_rd_sb_row(cpi, td, this_tile, mi_row, &tok);
4392 cpi->tplist[tile_row][tile_col][tile_sb_row].stop = tok;
4393 cpi->tplist[tile_row][tile_col][tile_sb_row].count =
4394 (unsigned int)(cpi->tplist[tile_row][tile_col][tile_sb_row].stop -
4395 cpi->tplist[tile_row][tile_col][tile_sb_row].start);
4396 assert(tok - cpi->tplist[tile_row][tile_col][tile_sb_row].start <=
4397 get_token_alloc(MI_BLOCK_SIZE >> 1, tile_mb_cols));
4402 void vp9_encode_tile(VP9_COMP *cpi, ThreadData *td, int tile_row,
4404 VP9_COMMON *const cm = &cpi->common;
4405 const int tile_cols = 1 << cm->log2_tile_cols;
4406 TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
4407 const TileInfo *const tile_info = &this_tile->tile_info;
4408 const int mi_row_start = tile_info->mi_row_start;
4409 const int mi_row_end = tile_info->mi_row_end;
4412 // Set up pointers to per thread motion search counters.
4413 td->mb.m_search_count_ptr = &this_tile->m_search_count;
4414 td->mb.ex_search_count_ptr = &this_tile->ex_search_count;
4415 #if CONFIG_MULTITHREAD
4416 td->mb.search_count_mutex = this_tile->search_count_mutex;
4419 for (mi_row = mi_row_start; mi_row < mi_row_end; mi_row += MI_BLOCK_SIZE)
4420 vp9_encode_sb_row(cpi, td, tile_row, tile_col, mi_row);
4423 static void encode_tiles(VP9_COMP *cpi) {
4424 VP9_COMMON *const cm = &cpi->common;
4425 const int tile_cols = 1 << cm->log2_tile_cols;
4426 const int tile_rows = 1 << cm->log2_tile_rows;
4427 int tile_col, tile_row;
4429 vp9_init_tile_data(cpi);
4431 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
4432 for (tile_col = 0; tile_col < tile_cols; ++tile_col)
4433 vp9_encode_tile(cpi, &cpi->td, tile_row, tile_col);
4436 #if CONFIG_FP_MB_STATS
4437 static int input_fpmb_stats(FIRSTPASS_MB_STATS *firstpass_mb_stats,
4438 VP9_COMMON *cm, uint8_t **this_frame_mb_stats) {
4439 uint8_t *mb_stats_in = firstpass_mb_stats->mb_stats_start +
4440 cm->current_video_frame * cm->MBs * sizeof(uint8_t);
4442 if (mb_stats_in > firstpass_mb_stats->mb_stats_end) return EOF;
4444 *this_frame_mb_stats = mb_stats_in;
4450 static void encode_frame_internal(VP9_COMP *cpi) {
4451 SPEED_FEATURES *const sf = &cpi->sf;
4452 ThreadData *const td = &cpi->td;
4453 MACROBLOCK *const x = &td->mb;
4454 VP9_COMMON *const cm = &cpi->common;
4455 MACROBLOCKD *const xd = &x->e_mbd;
4457 xd->mi = cm->mi_grid_visible;
4460 vp9_zero(*td->counts);
4461 vp9_zero(cpi->td.rd_counts);
4463 xd->lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 &&
4464 cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
4466 #if CONFIG_VP9_HIGHBITDEPTH
4467 if (cm->use_highbitdepth)
4468 x->fwd_txm4x4 = xd->lossless ? vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
4470 x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4471 x->highbd_itxm_add =
4472 xd->lossless ? vp9_highbd_iwht4x4_add : vp9_highbd_idct4x4_add;
4474 x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4475 #endif // CONFIG_VP9_HIGHBITDEPTH
4476 x->itxm_add = xd->lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
4478 if (xd->lossless) x->optimize = 0;
4480 cm->tx_mode = select_tx_mode(cpi, xd);
4482 vp9_frame_init_quantizer(cpi);
4484 vp9_initialize_rd_consts(cpi);
4485 vp9_initialize_me_consts(cpi, x, cm->base_qindex);
4486 init_encode_frame_mb_context(cpi);
4487 cm->use_prev_frame_mvs =
4488 !cm->error_resilient_mode && cm->width == cm->last_width &&
4489 cm->height == cm->last_height && !cm->intra_only && cm->last_show_frame;
4490 // Special case: set prev_mi to NULL when the previous mode info
4491 // context cannot be used.
4493 cm->use_prev_frame_mvs ? cm->prev_mip + cm->mi_stride + 1 : NULL;
4495 x->quant_fp = cpi->sf.use_quant_fp;
4496 vp9_zero(x->skip_txfm);
4497 if (sf->use_nonrd_pick_mode) {
4498 // Initialize internal buffer pointers for rtc coding, where non-RD
4499 // mode decision is used and hence no buffer pointer swap needed.
4501 struct macroblock_plane *const p = x->plane;
4502 struct macroblockd_plane *const pd = xd->plane;
4503 PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
4505 for (i = 0; i < MAX_MB_PLANE; ++i) {
4506 p[i].coeff = ctx->coeff_pbuf[i][0];
4507 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
4508 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
4509 p[i].eobs = ctx->eobs_pbuf[i][0];
4511 vp9_zero(x->zcoeff_blk);
4513 if (cm->frame_type != KEY_FRAME && cpi->rc.frames_since_golden == 0 &&
4514 !(cpi->oxcf.lag_in_frames > 0 && cpi->oxcf.rc_mode == VPX_VBR) &&
4516 cpi->ref_frame_flags &= (~VP9_GOLD_FLAG);
4518 if (sf->partition_search_type == SOURCE_VAR_BASED_PARTITION)
4519 source_var_based_partition_search_method(cpi);
4523 struct vpx_usec_timer emr_timer;
4524 vpx_usec_timer_start(&emr_timer);
4526 #if CONFIG_FP_MB_STATS
4527 if (cpi->use_fp_mb_stats) {
4528 input_fpmb_stats(&cpi->twopass.firstpass_mb_stats, cm,
4529 &cpi->twopass.this_frame_mb_stats);
4534 cpi->row_mt_sync_read_ptr = vp9_row_mt_sync_read_dummy;
4535 cpi->row_mt_sync_write_ptr = vp9_row_mt_sync_write_dummy;
4536 // If allowed, encoding tiles in parallel with one thread handling one
4537 // tile when row based multi-threading is disabled.
4538 if (VPXMIN(cpi->oxcf.max_threads, 1 << cm->log2_tile_cols) > 1)
4539 vp9_encode_tiles_mt(cpi);
4543 cpi->row_mt_sync_read_ptr = vp9_row_mt_sync_read;
4544 cpi->row_mt_sync_write_ptr = vp9_row_mt_sync_write;
4545 vp9_encode_tiles_row_mt(cpi);
4548 vpx_usec_timer_mark(&emr_timer);
4549 cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
4552 sf->skip_encode_frame =
4553 sf->skip_encode_sb ? get_skip_encode_frame(cm, td) : 0;
4556 // Keep record of the total distortion this time around for future use
4557 cpi->last_frame_distortion = cpi->frame_distortion;
4561 static INTERP_FILTER get_interp_filter(
4562 const int64_t threshes[SWITCHABLE_FILTER_CONTEXTS], int is_alt_ref) {
4563 if (!is_alt_ref && threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP] &&
4564 threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP_SHARP] &&
4565 threshes[EIGHTTAP_SMOOTH] > threshes[SWITCHABLE - 1]) {
4566 return EIGHTTAP_SMOOTH;
4567 } else if (threshes[EIGHTTAP_SHARP] > threshes[EIGHTTAP] &&
4568 threshes[EIGHTTAP_SHARP] > threshes[SWITCHABLE - 1]) {
4569 return EIGHTTAP_SHARP;
4570 } else if (threshes[EIGHTTAP] > threshes[SWITCHABLE - 1]) {
4577 static int compute_frame_aq_offset(struct VP9_COMP *cpi) {
4578 VP9_COMMON *const cm = &cpi->common;
4579 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
4580 struct segmentation *const seg = &cm->seg;
4588 for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
4589 MODE_INFO **mi_8x8 = mi_8x8_ptr;
4590 for (mi_col = 0; mi_col < cm->mi_cols; mi_col++, mi_8x8++) {
4591 segment_id = mi_8x8[0]->segment_id;
4592 qdelta_index = get_segdata(seg, segment_id, SEG_LVL_ALT_Q);
4593 sum_delta += qdelta_index;
4596 mi_8x8_ptr += cm->mi_stride;
4599 return sum_delta / (cm->mi_rows * cm->mi_cols);
4602 void vp9_encode_frame(VP9_COMP *cpi) {
4603 VP9_COMMON *const cm = &cpi->common;
4605 // In the longer term the encoder should be generalized to match the
4606 // decoder such that we allow compound where one of the 3 buffers has a
4607 // different sign bias and that buffer is then the fixed ref. However, this
4608 // requires further work in the rd loop. For now the only supported encoder
4609 // side behavior is where the ALT ref buffer has opposite sign bias to
4611 if (!frame_is_intra_only(cm)) {
4612 if ((cm->ref_frame_sign_bias[ALTREF_FRAME] ==
4613 cm->ref_frame_sign_bias[GOLDEN_FRAME]) ||
4614 (cm->ref_frame_sign_bias[ALTREF_FRAME] ==
4615 cm->ref_frame_sign_bias[LAST_FRAME])) {
4616 cpi->allow_comp_inter_inter = 0;
4618 cpi->allow_comp_inter_inter = 1;
4619 cm->comp_fixed_ref = ALTREF_FRAME;
4620 cm->comp_var_ref[0] = LAST_FRAME;
4621 cm->comp_var_ref[1] = GOLDEN_FRAME;
4625 if (cpi->sf.frame_parameter_update) {
4627 RD_OPT *const rd_opt = &cpi->rd;
4628 FRAME_COUNTS *counts = cpi->td.counts;
4629 RD_COUNTS *const rdc = &cpi->td.rd_counts;
4631 // This code does a single RD pass over the whole frame assuming
4632 // either compound, single or hybrid prediction as per whatever has
4633 // worked best for that type of frame in the past.
4634 // It also predicts whether another coding mode would have worked
4635 // better than this coding mode. If that is the case, it remembers
4636 // that for subsequent frames.
4637 // It also does the same analysis for transform size selection.
4638 const MV_REFERENCE_FRAME frame_type = get_frame_type(cpi);
4639 int64_t *const mode_thrs = rd_opt->prediction_type_threshes[frame_type];
4640 int64_t *const filter_thrs = rd_opt->filter_threshes[frame_type];
4641 const int is_alt_ref = frame_type == ALTREF_FRAME;
4643 /* prediction (compound, single or hybrid) mode selection */
4644 if (is_alt_ref || !cpi->allow_comp_inter_inter)
4645 cm->reference_mode = SINGLE_REFERENCE;
4646 else if (mode_thrs[COMPOUND_REFERENCE] > mode_thrs[SINGLE_REFERENCE] &&
4647 mode_thrs[COMPOUND_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT] &&
4648 check_dual_ref_flags(cpi) && cpi->static_mb_pct == 100)
4649 cm->reference_mode = COMPOUND_REFERENCE;
4650 else if (mode_thrs[SINGLE_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT])
4651 cm->reference_mode = SINGLE_REFERENCE;
4653 cm->reference_mode = REFERENCE_MODE_SELECT;
4655 if (cm->interp_filter == SWITCHABLE)
4656 cm->interp_filter = get_interp_filter(filter_thrs, is_alt_ref);
4658 encode_frame_internal(cpi);
4660 for (i = 0; i < REFERENCE_MODES; ++i)
4661 mode_thrs[i] = (mode_thrs[i] + rdc->comp_pred_diff[i] / cm->MBs) / 2;
4663 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
4664 filter_thrs[i] = (filter_thrs[i] + rdc->filter_diff[i] / cm->MBs) / 2;
4666 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
4667 int single_count_zero = 0;
4668 int comp_count_zero = 0;
4670 for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
4671 single_count_zero += counts->comp_inter[i][0];
4672 comp_count_zero += counts->comp_inter[i][1];
4675 if (comp_count_zero == 0) {
4676 cm->reference_mode = SINGLE_REFERENCE;
4677 vp9_zero(counts->comp_inter);
4678 } else if (single_count_zero == 0) {
4679 cm->reference_mode = COMPOUND_REFERENCE;
4680 vp9_zero(counts->comp_inter);
4684 if (cm->tx_mode == TX_MODE_SELECT) {
4686 int count8x8_lp = 0, count8x8_8x8p = 0;
4687 int count16x16_16x16p = 0, count16x16_lp = 0;
4690 for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
4691 count4x4 += counts->tx.p32x32[i][TX_4X4];
4692 count4x4 += counts->tx.p16x16[i][TX_4X4];
4693 count4x4 += counts->tx.p8x8[i][TX_4X4];
4695 count8x8_lp += counts->tx.p32x32[i][TX_8X8];
4696 count8x8_lp += counts->tx.p16x16[i][TX_8X8];
4697 count8x8_8x8p += counts->tx.p8x8[i][TX_8X8];
4699 count16x16_16x16p += counts->tx.p16x16[i][TX_16X16];
4700 count16x16_lp += counts->tx.p32x32[i][TX_16X16];
4701 count32x32 += counts->tx.p32x32[i][TX_32X32];
4703 if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0 &&
4705 cm->tx_mode = ALLOW_8X8;
4706 reset_skip_tx_size(cm, TX_8X8);
4707 } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0 &&
4708 count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
4709 cm->tx_mode = ONLY_4X4;
4710 reset_skip_tx_size(cm, TX_4X4);
4711 } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
4712 cm->tx_mode = ALLOW_32X32;
4713 } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
4714 cm->tx_mode = ALLOW_16X16;
4715 reset_skip_tx_size(cm, TX_16X16);
4719 cm->reference_mode = SINGLE_REFERENCE;
4720 encode_frame_internal(cpi);
4723 // If segmented AQ is enabled compute the average AQ weighting.
4724 if (cm->seg.enabled && (cpi->oxcf.aq_mode != NO_AQ) &&
4725 (cm->seg.update_map || cm->seg.update_data)) {
4726 cm->seg.aq_av_offset = compute_frame_aq_offset(cpi);
4730 static void sum_intra_stats(FRAME_COUNTS *counts, const MODE_INFO *mi) {
4731 const PREDICTION_MODE y_mode = mi->mode;
4732 const PREDICTION_MODE uv_mode = mi->uv_mode;
4733 const BLOCK_SIZE bsize = mi->sb_type;
4735 if (bsize < BLOCK_8X8) {
4737 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
4738 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
4739 for (idy = 0; idy < 2; idy += num_4x4_h)
4740 for (idx = 0; idx < 2; idx += num_4x4_w)
4741 ++counts->y_mode[0][mi->bmi[idy * 2 + idx].as_mode];
4743 ++counts->y_mode[size_group_lookup[bsize]][y_mode];
4746 ++counts->uv_mode[y_mode][uv_mode];
4749 static void update_zeromv_cnt(VP9_COMP *const cpi, const MODE_INFO *const mi,
4750 int mi_row, int mi_col, BLOCK_SIZE bsize) {
4751 const VP9_COMMON *const cm = &cpi->common;
4752 MV mv = mi->mv[0].as_mv;
4753 const int bw = num_8x8_blocks_wide_lookup[bsize];
4754 const int bh = num_8x8_blocks_high_lookup[bsize];
4755 const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
4756 const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
4757 const int block_index = mi_row * cm->mi_cols + mi_col;
4759 for (y = 0; y < ymis; y++)
4760 for (x = 0; x < xmis; x++) {
4761 int map_offset = block_index + y * cm->mi_cols + x;
4762 if (is_inter_block(mi) && mi->segment_id <= CR_SEGMENT_ID_BOOST2) {
4763 if (abs(mv.row) < 8 && abs(mv.col) < 8) {
4764 if (cpi->consec_zero_mv[map_offset] < 255)
4765 cpi->consec_zero_mv[map_offset]++;
4767 cpi->consec_zero_mv[map_offset] = 0;
4773 static void encode_superblock(VP9_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
4774 int output_enabled, int mi_row, int mi_col,
4775 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
4776 VP9_COMMON *const cm = &cpi->common;
4777 MACROBLOCK *const x = &td->mb;
4778 MACROBLOCKD *const xd = &x->e_mbd;
4779 MODE_INFO *mi = xd->mi[0];
4780 const int seg_skip =
4781 segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP);
4782 x->skip_recode = !x->select_tx_size && mi->sb_type >= BLOCK_8X8 &&
4783 cpi->oxcf.aq_mode != COMPLEXITY_AQ &&
4784 cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ &&
4785 cpi->sf.allow_skip_recode;
4787 if (!x->skip_recode && !cpi->sf.use_nonrd_pick_mode)
4788 memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
4790 x->skip_optimize = ctx->is_coded;
4792 x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
4793 x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
4794 x->q_index < QIDX_SKIP_THRESH);
4796 if (x->skip_encode) return;
4798 if (!is_inter_block(mi)) {
4800 #if CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
4801 if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) &&
4802 (xd->above_mi == NULL || xd->left_mi == NULL) &&
4803 need_top_left[mi->uv_mode])
4805 #endif // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
4807 for (plane = 0; plane < MAX_MB_PLANE; ++plane)
4808 vp9_encode_intra_block_plane(x, VPXMAX(bsize, BLOCK_8X8), plane, 1);
4809 if (output_enabled) sum_intra_stats(td->counts, mi);
4810 vp9_tokenize_sb(cpi, td, t, !output_enabled, seg_skip,
4811 VPXMAX(bsize, BLOCK_8X8));
4814 const int is_compound = has_second_ref(mi);
4815 set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
4816 for (ref = 0; ref < 1 + is_compound; ++ref) {
4817 YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, mi->ref_frame[ref]);
4818 assert(cfg != NULL);
4819 vp9_setup_pre_planes(xd, ref, cfg, mi_row, mi_col,
4820 &xd->block_refs[ref]->sf);
4822 if (!(cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready) || seg_skip)
4823 vp9_build_inter_predictors_sby(xd, mi_row, mi_col,
4824 VPXMAX(bsize, BLOCK_8X8));
4826 vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col,
4827 VPXMAX(bsize, BLOCK_8X8));
4829 vp9_encode_sb(x, VPXMAX(bsize, BLOCK_8X8));
4830 vp9_tokenize_sb(cpi, td, t, !output_enabled, seg_skip,
4831 VPXMAX(bsize, BLOCK_8X8));
4838 if (output_enabled) {
4839 if (cm->tx_mode == TX_MODE_SELECT && mi->sb_type >= BLOCK_8X8 &&
4840 !(is_inter_block(mi) && mi->skip)) {
4841 ++get_tx_counts(max_txsize_lookup[bsize], get_tx_size_context(xd),
4842 &td->counts->tx)[mi->tx_size];
4844 // The new intra coding scheme requires no change of transform size
4845 if (is_inter_block(mi)) {
4846 mi->tx_size = VPXMIN(tx_mode_to_biggest_tx_size[cm->tx_mode],
4847 max_txsize_lookup[bsize]);
4849 mi->tx_size = (bsize >= BLOCK_8X8) ? mi->tx_size : TX_4X4;
4853 ++td->counts->tx.tx_totals[mi->tx_size];
4854 ++td->counts->tx.tx_totals[get_uv_tx_size(mi, &xd->plane[1])];
4855 if (cm->seg.enabled && cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
4856 vp9_cyclic_refresh_update_sb_postencode(cpi, mi, mi_row, mi_col, bsize);
4857 if (cpi->oxcf.pass == 0 && cpi->svc.temporal_layer_id == 0)
4858 update_zeromv_cnt(cpi, mi, mi_row, mi_col, bsize);