2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
15 #include "./vp9_rtcd.h"
16 #include "./vpx_config.h"
18 #include "vpx_ports/vpx_timer.h"
20 #include "vp9/common/vp9_common.h"
21 #include "vp9/common/vp9_entropy.h"
22 #include "vp9/common/vp9_entropymode.h"
23 #include "vp9/common/vp9_idct.h"
24 #include "vp9/common/vp9_mvref_common.h"
25 #include "vp9/common/vp9_pred_common.h"
26 #include "vp9/common/vp9_quant_common.h"
27 #include "vp9/common/vp9_reconintra.h"
28 #include "vp9/common/vp9_reconinter.h"
29 #include "vp9/common/vp9_seg_common.h"
30 #include "vp9/common/vp9_systemdependent.h"
31 #include "vp9/common/vp9_tile_common.h"
33 #include "vp9/encoder/vp9_aq_complexity.h"
34 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
35 #include "vp9/encoder/vp9_aq_variance.h"
36 #include "vp9/encoder/vp9_encodeframe.h"
37 #include "vp9/encoder/vp9_encodemb.h"
38 #include "vp9/encoder/vp9_encodemv.h"
39 #include "vp9/encoder/vp9_ethread.h"
40 #include "vp9/encoder/vp9_extend.h"
41 #include "vp9/encoder/vp9_pickmode.h"
42 #include "vp9/encoder/vp9_rd.h"
43 #include "vp9/encoder/vp9_rdopt.h"
44 #include "vp9/encoder/vp9_segmentation.h"
45 #include "vp9/encoder/vp9_tokenize.h"
47 static void encode_superblock(VP9_COMP *cpi, ThreadData * td,
48 TOKENEXTRA **t, int output_enabled,
49 int mi_row, int mi_col, BLOCK_SIZE bsize,
50 PICK_MODE_CONTEXT *ctx);
52 // This is used as a reference when computing the source variance for the
53 // purposes of activity masking.
54 // Eventually this should be replaced by custom no-reference routines,
55 // which will be faster.
56 static const uint8_t VP9_VAR_OFFS[64] = {
57 128, 128, 128, 128, 128, 128, 128, 128,
58 128, 128, 128, 128, 128, 128, 128, 128,
59 128, 128, 128, 128, 128, 128, 128, 128,
60 128, 128, 128, 128, 128, 128, 128, 128,
61 128, 128, 128, 128, 128, 128, 128, 128,
62 128, 128, 128, 128, 128, 128, 128, 128,
63 128, 128, 128, 128, 128, 128, 128, 128,
64 128, 128, 128, 128, 128, 128, 128, 128
67 #if CONFIG_VP9_HIGHBITDEPTH
68 static const uint16_t VP9_HIGH_VAR_OFFS_8[64] = {
69 128, 128, 128, 128, 128, 128, 128, 128,
70 128, 128, 128, 128, 128, 128, 128, 128,
71 128, 128, 128, 128, 128, 128, 128, 128,
72 128, 128, 128, 128, 128, 128, 128, 128,
73 128, 128, 128, 128, 128, 128, 128, 128,
74 128, 128, 128, 128, 128, 128, 128, 128,
75 128, 128, 128, 128, 128, 128, 128, 128,
76 128, 128, 128, 128, 128, 128, 128, 128
79 static const uint16_t VP9_HIGH_VAR_OFFS_10[64] = {
80 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
81 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
82 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
83 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
84 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
85 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
86 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4,
87 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4, 128*4
90 static const uint16_t VP9_HIGH_VAR_OFFS_12[64] = {
91 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
92 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
93 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
94 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
95 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
96 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
97 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16,
98 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16, 128*16
100 #endif // CONFIG_VP9_HIGHBITDEPTH
102 unsigned int vp9_get_sby_perpixel_variance(VP9_COMP *cpi,
103 const struct buf_2d *ref,
106 const unsigned int var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
107 VP9_VAR_OFFS, 0, &sse);
108 return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
111 #if CONFIG_VP9_HIGHBITDEPTH
112 unsigned int vp9_high_get_sby_perpixel_variance(
113 VP9_COMP *cpi, const struct buf_2d *ref, BLOCK_SIZE bs, int bd) {
114 unsigned int var, sse;
117 var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
118 CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10),
122 var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
123 CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12),
128 var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
129 CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8),
133 return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
135 #endif // CONFIG_VP9_HIGHBITDEPTH
137 static unsigned int get_sby_perpixel_diff_variance(VP9_COMP *cpi,
138 const struct buf_2d *ref,
139 int mi_row, int mi_col,
141 unsigned int sse, var;
143 const YV12_BUFFER_CONFIG *last = get_ref_frame_buffer(cpi, LAST_FRAME);
145 assert(last != NULL);
147 &last->y_buffer[mi_row * MI_SIZE * last->y_stride + mi_col * MI_SIZE];
148 var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride, last_y, last->y_stride, &sse);
149 return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
152 static BLOCK_SIZE get_rd_var_based_fixed_partition(VP9_COMP *cpi, MACROBLOCK *x,
155 unsigned int var = get_sby_perpixel_diff_variance(cpi, &x->plane[0].src,
168 // Lighter version of set_offsets that only sets the mode info
170 static INLINE void set_mode_info_offsets(VP9_COMMON *const cm,
171 MACROBLOCKD *const xd,
174 const int idx_str = xd->mi_stride * mi_row + mi_col;
175 xd->mi = cm->mi_grid_visible + idx_str;
176 xd->mi[0] = cm->mi + idx_str;
179 static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile,
180 MACROBLOCK *const x, int mi_row, int mi_col,
182 VP9_COMMON *const cm = &cpi->common;
183 MACROBLOCKD *const xd = &x->e_mbd;
185 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
186 const int mi_height = num_8x8_blocks_high_lookup[bsize];
187 const struct segmentation *const seg = &cm->seg;
189 set_skip_context(xd, mi_row, mi_col);
191 set_mode_info_offsets(cm, xd, mi_row, mi_col);
193 mbmi = &xd->mi[0]->mbmi;
195 // Set up destination pointers.
196 vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
198 // Set up limit values for MV components.
199 // Mv beyond the range do not produce new/different prediction block.
200 x->mv_row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
201 x->mv_col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
202 x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
203 x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;
205 // Set up distance of MB to edge of frame in 1/8th pel units.
206 assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
207 set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
208 cm->mi_rows, cm->mi_cols);
210 // Set up source buffers.
211 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
214 x->rddiv = cpi->rd.RDDIV;
215 x->rdmult = cpi->rd.RDMULT;
219 if (cpi->oxcf.aq_mode != VARIANCE_AQ) {
220 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
221 : cm->last_frame_seg_map;
222 mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
224 vp9_init_plane_quantizers(cpi, x);
226 x->encode_breakout = cpi->segment_encode_breakout[mbmi->segment_id];
228 mbmi->segment_id = 0;
229 x->encode_breakout = cpi->encode_breakout;
233 static void duplicate_mode_info_in_sb(VP9_COMMON *cm, MACROBLOCKD *xd,
234 int mi_row, int mi_col,
236 const int block_width = num_8x8_blocks_wide_lookup[bsize];
237 const int block_height = num_8x8_blocks_high_lookup[bsize];
239 for (j = 0; j < block_height; ++j)
240 for (i = 0; i < block_width; ++i) {
241 if (mi_row + j < cm->mi_rows && mi_col + i < cm->mi_cols)
242 xd->mi[j * xd->mi_stride + i] = xd->mi[0];
246 static void set_block_size(VP9_COMP * const cpi,
247 MACROBLOCKD *const xd,
248 int mi_row, int mi_col,
250 if (cpi->common.mi_cols > mi_col && cpi->common.mi_rows > mi_row) {
251 set_mode_info_offsets(&cpi->common, xd, mi_row, mi_col);
252 xd->mi[0]->mbmi.sb_type = bsize;
257 int64_t sum_square_error;
267 } partition_variance;
270 partition_variance part_variances;
275 partition_variance part_variances;
280 partition_variance part_variances;
285 partition_variance part_variances;
290 partition_variance part_variances;
295 partition_variance *part_variances;
305 static void tree_to_node(void *data, BLOCK_SIZE bsize, variance_node *node) {
307 node->part_variances = NULL;
310 v64x64 *vt = (v64x64 *) data;
311 node->part_variances = &vt->part_variances;
312 for (i = 0; i < 4; i++)
313 node->split[i] = &vt->split[i].part_variances.none;
317 v32x32 *vt = (v32x32 *) data;
318 node->part_variances = &vt->part_variances;
319 for (i = 0; i < 4; i++)
320 node->split[i] = &vt->split[i].part_variances.none;
324 v16x16 *vt = (v16x16 *) data;
325 node->part_variances = &vt->part_variances;
326 for (i = 0; i < 4; i++)
327 node->split[i] = &vt->split[i].part_variances.none;
331 v8x8 *vt = (v8x8 *) data;
332 node->part_variances = &vt->part_variances;
333 for (i = 0; i < 4; i++)
334 node->split[i] = &vt->split[i].part_variances.none;
338 v4x4 *vt = (v4x4 *) data;
339 node->part_variances = &vt->part_variances;
340 for (i = 0; i < 4; i++)
341 node->split[i] = &vt->split[i];
351 // Set variance values given sum square error, sum error, count.
352 static void fill_variance(int64_t s2, int64_t s, int c, var *v) {
353 v->sum_square_error = s2;
358 static void get_variance(var *v) {
359 v->variance = (int)(256 * (v->sum_square_error -
360 ((v->sum_error * v->sum_error) >> v->log2_count)) >> v->log2_count);
363 void sum_2_variances(const var *a, const var *b, var *r) {
364 assert(a->log2_count == b->log2_count);
365 fill_variance(a->sum_square_error + b->sum_square_error,
366 a->sum_error + b->sum_error, a->log2_count + 1, r);
369 static void fill_variance_tree(void *data, BLOCK_SIZE bsize) {
371 tree_to_node(data, bsize, &node);
372 sum_2_variances(node.split[0], node.split[1], &node.part_variances->horz[0]);
373 sum_2_variances(node.split[2], node.split[3], &node.part_variances->horz[1]);
374 sum_2_variances(node.split[0], node.split[2], &node.part_variances->vert[0]);
375 sum_2_variances(node.split[1], node.split[3], &node.part_variances->vert[1]);
376 sum_2_variances(&node.part_variances->vert[0], &node.part_variances->vert[1],
377 &node.part_variances->none);
380 static int set_vt_partitioning(VP9_COMP *cpi,
381 MACROBLOCKD *const xd,
387 BLOCK_SIZE bsize_min,
389 VP9_COMMON * const cm = &cpi->common;
391 const int block_width = num_8x8_blocks_wide_lookup[bsize];
392 const int block_height = num_8x8_blocks_high_lookup[bsize];
393 const int low_res = (cm->width <= 352 && cm->height <= 288);
395 assert(block_height == block_width);
396 tree_to_node(data, bsize, &vt);
398 if (force_split == 1)
401 // For bsize=bsize_min (16x16/8x8 for 8x8/4x4 downsampling), select if
402 // variance is below threshold, otherwise split will be selected.
403 // No check for vert/horiz split as too few samples for variance.
404 if (bsize == bsize_min) {
405 // Variance already computed to set the force_split.
406 if (low_res || cm->frame_type == KEY_FRAME)
407 get_variance(&vt.part_variances->none);
408 if (mi_col + block_width / 2 < cm->mi_cols &&
409 mi_row + block_height / 2 < cm->mi_rows &&
410 vt.part_variances->none.variance < threshold) {
411 set_block_size(cpi, xd, mi_row, mi_col, bsize);
415 } else if (bsize > bsize_min) {
416 // Variance already computed to set the force_split.
417 if (low_res || cm->frame_type == KEY_FRAME)
418 get_variance(&vt.part_variances->none);
419 // For key frame: take split for bsize above 32X32 or very high variance.
420 if (cm->frame_type == KEY_FRAME &&
421 (bsize > BLOCK_32X32 ||
422 vt.part_variances->none.variance > (threshold << 4))) {
425 // If variance is low, take the bsize (no split).
426 if (mi_col + block_width / 2 < cm->mi_cols &&
427 mi_row + block_height / 2 < cm->mi_rows &&
428 vt.part_variances->none.variance < threshold) {
429 set_block_size(cpi, xd, mi_row, mi_col, bsize);
433 // Check vertical split.
434 if (mi_row + block_height / 2 < cm->mi_rows) {
435 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_VERT);
436 get_variance(&vt.part_variances->vert[0]);
437 get_variance(&vt.part_variances->vert[1]);
438 if (vt.part_variances->vert[0].variance < threshold &&
439 vt.part_variances->vert[1].variance < threshold &&
440 get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
441 set_block_size(cpi, xd, mi_row, mi_col, subsize);
442 set_block_size(cpi, xd, mi_row, mi_col + block_width / 2, subsize);
446 // Check horizontal split.
447 if (mi_col + block_width / 2 < cm->mi_cols) {
448 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_HORZ);
449 get_variance(&vt.part_variances->horz[0]);
450 get_variance(&vt.part_variances->horz[1]);
451 if (vt.part_variances->horz[0].variance < threshold &&
452 vt.part_variances->horz[1].variance < threshold &&
453 get_plane_block_size(subsize, &xd->plane[1]) < BLOCK_INVALID) {
454 set_block_size(cpi, xd, mi_row, mi_col, subsize);
455 set_block_size(cpi, xd, mi_row + block_height / 2, mi_col, subsize);
465 void vp9_set_vbp_thresholds(VP9_COMP *cpi, int q) {
466 SPEED_FEATURES *const sf = &cpi->sf;
467 if (sf->partition_search_type != VAR_BASED_PARTITION &&
468 sf->partition_search_type != REFERENCE_PARTITION) {
471 VP9_COMMON *const cm = &cpi->common;
472 const int is_key_frame = (cm->frame_type == KEY_FRAME);
473 const int threshold_multiplier = is_key_frame ? 20 : 1;
474 const int64_t threshold_base = (int64_t)(threshold_multiplier *
475 cpi->y_dequant[q][1]);
477 // TODO(marpan): Allow 4x4 partitions for inter-frames.
478 // use_4x4_partition = (variance4x4downsample[i2 + j] == 1);
479 // If 4x4 partition is not used, then 8x8 partition will be selected
480 // if variance of 16x16 block is very high, so use larger threshold
481 // for 16x16 (threshold_bsize_min) in that case.
483 // Array index: 0 - threshold_64x64; 1 - threshold_32x32;
484 // 2 - threshold_16x16; 3 - vbp_threshold_8x8;
486 cpi->vbp_thresholds[0] = threshold_base;
487 cpi->vbp_thresholds[1] = threshold_base >> 2;
488 cpi->vbp_thresholds[2] = threshold_base >> 2;
489 cpi->vbp_thresholds[3] = threshold_base << 2;
490 cpi->vbp_threshold_sad = 0;
491 cpi->vbp_bsize_min = BLOCK_8X8;
493 cpi->vbp_thresholds[1] = threshold_base;
494 if (cm->width <= 352 && cm->height <= 288) {
495 cpi->vbp_thresholds[0] = threshold_base >> 2;
496 cpi->vbp_thresholds[2] = threshold_base << 3;
497 cpi->vbp_threshold_sad = 100;
499 cpi->vbp_thresholds[0] = threshold_base;
500 cpi->vbp_thresholds[1] = (5 * threshold_base) >> 2;
501 cpi->vbp_thresholds[2] = threshold_base << cpi->oxcf.speed;
502 cpi->vbp_threshold_sad = (cpi->y_dequant[q][1] << 1) > 1000 ?
503 (cpi->y_dequant[q][1] << 1) : 1000;
505 cpi->vbp_bsize_min = BLOCK_16X16;
507 cpi->vbp_threshold_minmax = 15 + (q >> 3);
511 // Compute the minmax over the 8x8 subblocks.
512 static int compute_minmax_8x8(const uint8_t *s, int sp, const uint8_t *d,
513 int dp, int x16_idx, int y16_idx,
514 #if CONFIG_VP9_HIGHBITDEPTH
521 int minmax_min = 255;
522 // Loop over the 4 8x8 subblocks.
523 for (k = 0; k < 4; k++) {
524 int x8_idx = x16_idx + ((k & 1) << 3);
525 int y8_idx = y16_idx + ((k >> 1) << 3);
528 if (x8_idx < pixels_wide && y8_idx < pixels_high) {
529 #if CONFIG_VP9_HIGHBITDEPTH
530 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
531 vp9_highbd_minmax_8x8(s + y8_idx * sp + x8_idx, sp,
532 d + y8_idx * dp + x8_idx, dp,
535 vp9_minmax_8x8(s + y8_idx * sp + x8_idx, sp,
536 d + y8_idx * dp + x8_idx, dp,
540 vp9_minmax_8x8(s + y8_idx * sp + x8_idx, sp,
541 d + y8_idx * dp + x8_idx, dp,
544 if ((max - min) > minmax_max)
545 minmax_max = (max - min);
546 if ((max - min) < minmax_min)
547 minmax_min = (max - min);
550 return (minmax_max - minmax_min);
553 static void modify_vbp_thresholds(VP9_COMP *cpi, int64_t thresholds[], int q) {
554 VP9_COMMON *const cm = &cpi->common;
555 const int64_t threshold_base = (int64_t)(cpi->y_dequant[q][1]);
557 // Array index: 0 - threshold_64x64; 1 - threshold_32x32;
558 // 2 - threshold_16x16; 3 - vbp_threshold_8x8;
559 thresholds[1] = threshold_base;
560 if (cm->width <= 352 && cm->height <= 288) {
561 thresholds[0] = threshold_base >> 2;
562 thresholds[2] = threshold_base << 3;
564 thresholds[0] = threshold_base;
565 thresholds[1] = (5 * threshold_base) >> 2;
566 thresholds[2] = threshold_base << cpi->oxcf.speed;
570 static void fill_variance_4x4avg(const uint8_t *s, int sp, const uint8_t *d,
571 int dp, int x8_idx, int y8_idx, v8x8 *vst,
572 #if CONFIG_VP9_HIGHBITDEPTH
579 for (k = 0; k < 4; k++) {
580 int x4_idx = x8_idx + ((k & 1) << 2);
581 int y4_idx = y8_idx + ((k >> 1) << 2);
582 unsigned int sse = 0;
584 if (x4_idx < pixels_wide && y4_idx < pixels_high) {
587 #if CONFIG_VP9_HIGHBITDEPTH
588 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
589 s_avg = vp9_highbd_avg_4x4(s + y4_idx * sp + x4_idx, sp);
591 d_avg = vp9_highbd_avg_4x4(d + y4_idx * dp + x4_idx, dp);
593 s_avg = vp9_avg_4x4(s + y4_idx * sp + x4_idx, sp);
595 d_avg = vp9_avg_4x4(d + y4_idx * dp + x4_idx, dp);
598 s_avg = vp9_avg_4x4(s + y4_idx * sp + x4_idx, sp);
600 d_avg = vp9_avg_4x4(d + y4_idx * dp + x4_idx, dp);
605 fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
609 static void fill_variance_8x8avg(const uint8_t *s, int sp, const uint8_t *d,
610 int dp, int x16_idx, int y16_idx, v16x16 *vst,
611 #if CONFIG_VP9_HIGHBITDEPTH
618 for (k = 0; k < 4; k++) {
619 int x8_idx = x16_idx + ((k & 1) << 3);
620 int y8_idx = y16_idx + ((k >> 1) << 3);
621 unsigned int sse = 0;
623 if (x8_idx < pixels_wide && y8_idx < pixels_high) {
626 #if CONFIG_VP9_HIGHBITDEPTH
627 if (highbd_flag & YV12_FLAG_HIGHBITDEPTH) {
628 s_avg = vp9_highbd_avg_8x8(s + y8_idx * sp + x8_idx, sp);
630 d_avg = vp9_highbd_avg_8x8(d + y8_idx * dp + x8_idx, dp);
632 s_avg = vp9_avg_8x8(s + y8_idx * sp + x8_idx, sp);
634 d_avg = vp9_avg_8x8(d + y8_idx * dp + x8_idx, dp);
637 s_avg = vp9_avg_8x8(s + y8_idx * sp + x8_idx, sp);
639 d_avg = vp9_avg_8x8(d + y8_idx * dp + x8_idx, dp);
644 fill_variance(sse, sum, 0, &vst->split[k].part_variances.none);
648 // This function chooses partitioning based on the variance between source and
649 // reconstructed last, where variance is computed for down-sampled inputs.
650 static int choose_partitioning(VP9_COMP *cpi,
651 const TileInfo *const tile,
653 int mi_row, int mi_col) {
654 VP9_COMMON * const cm = &cpi->common;
655 MACROBLOCKD *xd = &x->e_mbd;
664 int pixels_wide = 64, pixels_high = 64;
665 int64_t thresholds[4] = {cpi->vbp_thresholds[0], cpi->vbp_thresholds[1],
666 cpi->vbp_thresholds[2], cpi->vbp_thresholds[3]};
668 // Always use 4x4 partition for key frame.
669 const int is_key_frame = (cm->frame_type == KEY_FRAME);
670 const int use_4x4_partition = is_key_frame;
671 const int low_res = (cm->width <= 352 && cm->height <= 288);
672 int variance4x4downsample[16];
674 int segment_id = CR_SEGMENT_ID_BASE;
675 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) {
676 const uint8_t *const map = cm->seg.update_map ? cpi->segmentation_map :
677 cm->last_frame_seg_map;
678 segment_id = vp9_get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
680 if (cyclic_refresh_segment_id_boosted(segment_id)) {
681 int q = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex);
682 modify_vbp_thresholds(cpi, thresholds, q);
686 set_offsets(cpi, tile, x, mi_row, mi_col, BLOCK_64X64);
688 if (xd->mb_to_right_edge < 0)
689 pixels_wide += (xd->mb_to_right_edge >> 3);
690 if (xd->mb_to_bottom_edge < 0)
691 pixels_high += (xd->mb_to_bottom_edge >> 3);
693 s = x->plane[0].src.buf;
694 sp = x->plane[0].src.stride;
697 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
699 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
701 const YV12_BUFFER_CONFIG *yv12_g = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
702 unsigned int y_sad, y_sad_g;
703 const BLOCK_SIZE bsize = BLOCK_32X32
704 + (mi_col + 4 < cm->mi_cols) * 2 + (mi_row + 4 < cm->mi_rows);
706 assert(yv12 != NULL);
707 if (yv12_g && yv12_g != yv12) {
708 vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
709 &cm->frame_refs[GOLDEN_FRAME - 1].sf);
710 y_sad_g = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf,
711 x->plane[0].src.stride,
712 xd->plane[0].pre[0].buf,
713 xd->plane[0].pre[0].stride);
718 vp9_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
719 &cm->frame_refs[LAST_FRAME - 1].sf);
720 mbmi->ref_frame[0] = LAST_FRAME;
721 mbmi->ref_frame[1] = NONE;
722 mbmi->sb_type = BLOCK_64X64;
723 mbmi->mv[0].as_int = 0;
724 mbmi->interp_filter = BILINEAR;
726 y_sad = vp9_int_pro_motion_estimation(cpi, x, bsize);
727 if (y_sad_g < y_sad) {
728 vp9_setup_pre_planes(xd, 0, yv12_g, mi_row, mi_col,
729 &cm->frame_refs[GOLDEN_FRAME - 1].sf);
730 mbmi->ref_frame[0] = GOLDEN_FRAME;
731 mbmi->mv[0].as_int = 0;
734 x->pred_mv[LAST_FRAME] = mbmi->mv[0].as_mv;
737 vp9_build_inter_predictors_sb(xd, mi_row, mi_col, BLOCK_64X64);
739 for (i = 1; i <= 2; ++i) {
740 struct macroblock_plane *p = &x->plane[i];
741 struct macroblockd_plane *pd = &xd->plane[i];
742 const BLOCK_SIZE bs = get_plane_block_size(bsize, pd);
744 if (bs == BLOCK_INVALID)
747 uv_sad = cpi->fn_ptr[bs].sdf(p->src.buf, p->src.stride,
748 pd->dst.buf, pd->dst.stride);
750 x->color_sensitivity[i - 1] = uv_sad > (y_sad >> 2);
753 d = xd->plane[0].dst.buf;
754 dp = xd->plane[0].dst.stride;
756 // If the y_sad is very small, take 64x64 as partition and exit.
757 // Don't check on boosted segment for now, as 64x64 is suppressed there.
758 if (segment_id == CR_SEGMENT_ID_BASE &&
759 y_sad < cpi->vbp_threshold_sad) {
760 const int block_width = num_8x8_blocks_wide_lookup[BLOCK_64X64];
761 const int block_height = num_8x8_blocks_high_lookup[BLOCK_64X64];
762 if (mi_col + block_width / 2 < cm->mi_cols &&
763 mi_row + block_height / 2 < cm->mi_rows) {
764 set_block_size(cpi, xd, mi_row, mi_col, BLOCK_64X64);
771 #if CONFIG_VP9_HIGHBITDEPTH
772 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
775 d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_10);
778 d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_12);
782 d = CONVERT_TO_BYTEPTR(VP9_HIGH_VAR_OFFS_8);
786 #endif // CONFIG_VP9_HIGHBITDEPTH
789 // Index for force_split: 0 for 64x64, 1-4 for 32x32 blocks,
790 // 5-20 for the 16x16 blocks.
792 // Fill in the entire tree of 8x8 (or 4x4 under some conditions) variances
794 for (i = 0; i < 4; i++) {
795 const int x32_idx = ((i & 1) << 5);
796 const int y32_idx = ((i >> 1) << 5);
797 const int i2 = i << 2;
798 force_split[i + 1] = 0;
799 for (j = 0; j < 4; j++) {
800 const int x16_idx = x32_idx + ((j & 1) << 4);
801 const int y16_idx = y32_idx + ((j >> 1) << 4);
802 const int split_index = 5 + i2 + j;
803 v16x16 *vst = &vt.split[i].split[j];
804 force_split[split_index] = 0;
805 variance4x4downsample[i2 + j] = 0;
807 fill_variance_8x8avg(s, sp, d, dp, x16_idx, y16_idx, vst,
808 #if CONFIG_VP9_HIGHBITDEPTH
814 fill_variance_tree(&vt.split[i].split[j], BLOCK_16X16);
815 get_variance(&vt.split[i].split[j].part_variances.none);
816 if (vt.split[i].split[j].part_variances.none.variance >
818 // 16X16 variance is above threshold for split, so force split to 8x8
819 // for this 16x16 block (this also forces splits for upper levels).
820 force_split[split_index] = 1;
821 force_split[i + 1] = 1;
823 } else if (vt.split[i].split[j].part_variances.none.variance >
825 !cyclic_refresh_segment_id_boosted(segment_id)) {
826 // We have some nominal amount of 16x16 variance (based on average),
827 // compute the minmax over the 8x8 sub-blocks, and if above threshold,
828 // force split to 8x8 block for this 16x16 block.
829 int minmax = compute_minmax_8x8(s, sp, d, dp, x16_idx, y16_idx,
830 #if CONFIG_VP9_HIGHBITDEPTH
833 pixels_wide, pixels_high);
834 if (minmax > cpi->vbp_threshold_minmax) {
835 force_split[split_index] = 1;
836 force_split[i + 1] = 1;
841 if (is_key_frame || (low_res &&
842 vt.split[i].split[j].part_variances.none.variance >
843 (thresholds[1] << 1))) {
844 force_split[split_index] = 0;
845 // Go down to 4x4 down-sampling for variance.
846 variance4x4downsample[i2 + j] = 1;
847 for (k = 0; k < 4; k++) {
848 int x8_idx = x16_idx + ((k & 1) << 3);
849 int y8_idx = y16_idx + ((k >> 1) << 3);
850 v8x8 *vst2 = is_key_frame ? &vst->split[k] :
851 &vt2[i2 + j].split[k];
852 fill_variance_4x4avg(s, sp, d, dp, x8_idx, y8_idx, vst2,
853 #if CONFIG_VP9_HIGHBITDEPTH
864 // Fill the rest of the variance tree by summing split partition values.
865 for (i = 0; i < 4; i++) {
866 const int i2 = i << 2;
867 for (j = 0; j < 4; j++) {
868 if (variance4x4downsample[i2 + j] == 1) {
869 v16x16 *vtemp = (!is_key_frame) ? &vt2[i2 + j] :
870 &vt.split[i].split[j];
871 for (m = 0; m < 4; m++)
872 fill_variance_tree(&vtemp->split[m], BLOCK_8X8);
873 fill_variance_tree(vtemp, BLOCK_16X16);
876 fill_variance_tree(&vt.split[i], BLOCK_32X32);
877 // If variance of this 32x32 block is above the threshold, force the block
878 // to split. This also forces a split on the upper (64x64) level.
879 if (!force_split[i + 1]) {
880 get_variance(&vt.split[i].part_variances.none);
881 if (vt.split[i].part_variances.none.variance > thresholds[1]) {
882 force_split[i + 1] = 1;
887 if (!force_split[0]) {
888 fill_variance_tree(&vt, BLOCK_64X64);
889 get_variance(&vt.part_variances.none);
892 // Now go through the entire structure, splitting every block size until
893 // we get to one that's got a variance lower than our threshold.
894 if ( mi_col + 8 > cm->mi_cols || mi_row + 8 > cm->mi_rows ||
895 !set_vt_partitioning(cpi, xd, &vt, BLOCK_64X64, mi_row, mi_col,
896 thresholds[0], BLOCK_16X16, force_split[0])) {
897 for (i = 0; i < 4; ++i) {
898 const int x32_idx = ((i & 1) << 2);
899 const int y32_idx = ((i >> 1) << 2);
900 const int i2 = i << 2;
901 if (!set_vt_partitioning(cpi, xd, &vt.split[i], BLOCK_32X32,
902 (mi_row + y32_idx), (mi_col + x32_idx),
903 thresholds[1], BLOCK_16X16,
904 force_split[i + 1])) {
905 for (j = 0; j < 4; ++j) {
906 const int x16_idx = ((j & 1) << 1);
907 const int y16_idx = ((j >> 1) << 1);
908 // For inter frames: if variance4x4downsample[] == 1 for this 16x16
909 // block, then the variance is based on 4x4 down-sampling, so use vt2
910 // in set_vt_partioning(), otherwise use vt.
911 v16x16 *vtemp = (!is_key_frame &&
912 variance4x4downsample[i2 + j] == 1) ?
913 &vt2[i2 + j] : &vt.split[i].split[j];
914 if (!set_vt_partitioning(cpi, xd, vtemp, BLOCK_16X16,
915 mi_row + y32_idx + y16_idx,
916 mi_col + x32_idx + x16_idx,
919 force_split[5 + i2 + j])) {
920 for (k = 0; k < 4; ++k) {
921 const int x8_idx = (k & 1);
922 const int y8_idx = (k >> 1);
923 if (use_4x4_partition) {
924 if (!set_vt_partitioning(cpi, xd, &vtemp->split[k],
926 mi_row + y32_idx + y16_idx + y8_idx,
927 mi_col + x32_idx + x16_idx + x8_idx,
928 thresholds[3], BLOCK_8X8, 0)) {
929 set_block_size(cpi, xd,
930 (mi_row + y32_idx + y16_idx + y8_idx),
931 (mi_col + x32_idx + x16_idx + x8_idx),
935 set_block_size(cpi, xd,
936 (mi_row + y32_idx + y16_idx + y8_idx),
937 (mi_col + x32_idx + x16_idx + x8_idx),
949 static void update_state(VP9_COMP *cpi, ThreadData *td,
950 PICK_MODE_CONTEXT *ctx,
951 int mi_row, int mi_col, BLOCK_SIZE bsize,
952 int output_enabled) {
954 VP9_COMMON *const cm = &cpi->common;
955 RD_COUNTS *const rdc = &td->rd_counts;
956 MACROBLOCK *const x = &td->mb;
957 MACROBLOCKD *const xd = &x->e_mbd;
958 struct macroblock_plane *const p = x->plane;
959 struct macroblockd_plane *const pd = xd->plane;
960 MODE_INFO *mi = &ctx->mic;
961 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
962 MODE_INFO *mi_addr = xd->mi[0];
963 const struct segmentation *const seg = &cm->seg;
964 const int bw = num_8x8_blocks_wide_lookup[mi->mbmi.sb_type];
965 const int bh = num_8x8_blocks_high_lookup[mi->mbmi.sb_type];
966 const int x_mis = MIN(bw, cm->mi_cols - mi_col);
967 const int y_mis = MIN(bh, cm->mi_rows - mi_row);
968 MV_REF *const frame_mvs =
969 cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
972 const int mis = cm->mi_stride;
973 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
974 const int mi_height = num_8x8_blocks_high_lookup[bsize];
977 assert(mi->mbmi.sb_type == bsize);
981 // If segmentation in use
983 // For in frame complexity AQ copy the segment id from the segment map.
984 if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
985 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
986 : cm->last_frame_seg_map;
987 mi_addr->mbmi.segment_id =
988 vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
990 // Else for cyclic refresh mode update the segment map, set the segment id
991 // and then update the quantizer.
992 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
993 vp9_cyclic_refresh_update_segment(cpi, &xd->mi[0]->mbmi, mi_row,
994 mi_col, bsize, ctx->rate, ctx->dist,
999 max_plane = is_inter_block(mbmi) ? MAX_MB_PLANE : 1;
1000 for (i = 0; i < max_plane; ++i) {
1001 p[i].coeff = ctx->coeff_pbuf[i][1];
1002 p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
1003 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
1004 p[i].eobs = ctx->eobs_pbuf[i][1];
1007 for (i = max_plane; i < MAX_MB_PLANE; ++i) {
1008 p[i].coeff = ctx->coeff_pbuf[i][2];
1009 p[i].qcoeff = ctx->qcoeff_pbuf[i][2];
1010 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2];
1011 p[i].eobs = ctx->eobs_pbuf[i][2];
1014 // Restore the coding context of the MB to that that was in place
1015 // when the mode was picked for it
1016 for (y = 0; y < mi_height; y++)
1017 for (x_idx = 0; x_idx < mi_width; x_idx++)
1018 if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx
1019 && (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
1020 xd->mi[x_idx + y * mis] = mi_addr;
1023 if (cpi->oxcf.aq_mode)
1024 vp9_init_plane_quantizers(cpi, x);
1026 // FIXME(rbultje) I'm pretty sure this should go to the end of this block
1027 // (i.e. after the output_enabled)
1028 if (bsize < BLOCK_32X32) {
1029 if (bsize < BLOCK_16X16)
1030 ctx->tx_rd_diff[ALLOW_16X16] = ctx->tx_rd_diff[ALLOW_8X8];
1031 ctx->tx_rd_diff[ALLOW_32X32] = ctx->tx_rd_diff[ALLOW_16X16];
1034 if (is_inter_block(mbmi) && mbmi->sb_type < BLOCK_8X8) {
1035 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
1036 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
1039 x->skip = ctx->skip;
1040 memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk,
1041 sizeof(uint8_t) * ctx->num_4x4_blk);
1043 if (!output_enabled)
1046 if (!vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
1047 for (i = 0; i < TX_MODES; i++)
1048 rdc->tx_select_diff[i] += ctx->tx_rd_diff[i];
1051 #if CONFIG_INTERNAL_STATS
1052 if (frame_is_intra_only(cm)) {
1053 static const int kf_mode_index[] = {
1055 THR_V_PRED /*V_PRED*/,
1056 THR_H_PRED /*H_PRED*/,
1057 THR_D45_PRED /*D45_PRED*/,
1058 THR_D135_PRED /*D135_PRED*/,
1059 THR_D117_PRED /*D117_PRED*/,
1060 THR_D153_PRED /*D153_PRED*/,
1061 THR_D207_PRED /*D207_PRED*/,
1062 THR_D63_PRED /*D63_PRED*/,
1065 ++cpi->mode_chosen_counts[kf_mode_index[mbmi->mode]];
1067 // Note how often each mode chosen as best
1068 ++cpi->mode_chosen_counts[ctx->best_mode_index];
1071 if (!frame_is_intra_only(cm)) {
1072 if (is_inter_block(mbmi)) {
1073 vp9_update_mv_count(td);
1075 if (cm->interp_filter == SWITCHABLE) {
1076 const int ctx = vp9_get_pred_context_switchable_interp(xd);
1077 ++td->counts->switchable_interp[ctx][mbmi->interp_filter];
1081 rdc->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
1082 rdc->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
1083 rdc->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
1085 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
1086 rdc->filter_diff[i] += ctx->best_filter_diff[i];
1089 for (h = 0; h < y_mis; ++h) {
1090 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
1091 for (w = 0; w < x_mis; ++w) {
1092 MV_REF *const mv = frame_mv + w;
1093 mv->ref_frame[0] = mi->mbmi.ref_frame[0];
1094 mv->ref_frame[1] = mi->mbmi.ref_frame[1];
1095 mv->mv[0].as_int = mi->mbmi.mv[0].as_int;
1096 mv->mv[1].as_int = mi->mbmi.mv[1].as_int;
1101 void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
1102 int mi_row, int mi_col) {
1103 uint8_t *const buffers[3] = {src->y_buffer, src->u_buffer, src->v_buffer };
1104 const int strides[3] = {src->y_stride, src->uv_stride, src->uv_stride };
1107 // Set current frame pointer.
1108 x->e_mbd.cur_buf = src;
1110 for (i = 0; i < MAX_MB_PLANE; i++)
1111 setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
1112 NULL, x->e_mbd.plane[i].subsampling_x,
1113 x->e_mbd.plane[i].subsampling_y);
1116 static void set_mode_info_seg_skip(MACROBLOCK *x, TX_MODE tx_mode,
1117 RD_COST *rd_cost, BLOCK_SIZE bsize) {
1118 MACROBLOCKD *const xd = &x->e_mbd;
1119 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
1120 INTERP_FILTER filter_ref;
1122 if (xd->up_available)
1123 filter_ref = xd->mi[-xd->mi_stride]->mbmi.interp_filter;
1124 else if (xd->left_available)
1125 filter_ref = xd->mi[-1]->mbmi.interp_filter;
1127 filter_ref = EIGHTTAP;
1129 mbmi->sb_type = bsize;
1130 mbmi->mode = ZEROMV;
1131 mbmi->tx_size = MIN(max_txsize_lookup[bsize],
1132 tx_mode_to_biggest_tx_size[tx_mode]);
1134 mbmi->uv_mode = DC_PRED;
1135 mbmi->ref_frame[0] = LAST_FRAME;
1136 mbmi->ref_frame[1] = NONE;
1137 mbmi->mv[0].as_int = 0;
1138 mbmi->interp_filter = filter_ref;
1140 xd->mi[0]->bmi[0].as_mv[0].as_int = 0;
1143 vp9_rd_cost_init(rd_cost);
1146 static int set_segment_rdmult(VP9_COMP *const cpi,
1147 MACROBLOCK *const x,
1148 int8_t segment_id) {
1150 VP9_COMMON *const cm = &cpi->common;
1151 vp9_init_plane_quantizers(cpi, x);
1152 vp9_clear_system_state();
1153 segment_qindex = vp9_get_qindex(&cm->seg, segment_id,
1155 return vp9_compute_rd_mult(cpi, segment_qindex + cm->y_dc_delta_q);
1158 static void rd_pick_sb_modes(VP9_COMP *cpi,
1159 TileDataEnc *tile_data,
1160 MACROBLOCK *const x,
1161 int mi_row, int mi_col, RD_COST *rd_cost,
1162 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
1164 VP9_COMMON *const cm = &cpi->common;
1165 TileInfo *const tile_info = &tile_data->tile_info;
1166 MACROBLOCKD *const xd = &x->e_mbd;
1168 struct macroblock_plane *const p = x->plane;
1169 struct macroblockd_plane *const pd = xd->plane;
1170 const AQ_MODE aq_mode = cpi->oxcf.aq_mode;
1173 vp9_clear_system_state();
1175 // Use the lower precision, but faster, 32x32 fdct for mode selection.
1176 x->use_lp32x32fdct = 1;
1178 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
1179 mbmi = &xd->mi[0]->mbmi;
1180 mbmi->sb_type = bsize;
1182 for (i = 0; i < MAX_MB_PLANE; ++i) {
1183 p[i].coeff = ctx->coeff_pbuf[i][0];
1184 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
1185 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
1186 p[i].eobs = ctx->eobs_pbuf[i][0];
1190 ctx->pred_pixel_ready = 0;
1193 // Set to zero to make sure we do not use the previous encoded frame stats
1196 #if CONFIG_VP9_HIGHBITDEPTH
1197 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1198 x->source_variance =
1199 vp9_high_get_sby_perpixel_variance(cpi, &x->plane[0].src,
1202 x->source_variance =
1203 vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1206 x->source_variance =
1207 vp9_get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
1208 #endif // CONFIG_VP9_HIGHBITDEPTH
1210 // Save rdmult before it might be changed, so it can be restored later.
1211 orig_rdmult = x->rdmult;
1213 if (aq_mode == VARIANCE_AQ) {
1214 const int energy = bsize <= BLOCK_16X16 ? x->mb_energy
1215 : vp9_block_energy(cpi, x, bsize);
1216 if (cm->frame_type == KEY_FRAME ||
1217 cpi->refresh_alt_ref_frame ||
1218 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
1219 mbmi->segment_id = vp9_vaq_segment_id(energy);
1221 const uint8_t *const map = cm->seg.update_map ? cpi->segmentation_map
1222 : cm->last_frame_seg_map;
1223 mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
1225 x->rdmult = set_segment_rdmult(cpi, x, mbmi->segment_id);
1226 } else if (aq_mode == COMPLEXITY_AQ) {
1227 x->rdmult = set_segment_rdmult(cpi, x, mbmi->segment_id);
1228 } else if (aq_mode == CYCLIC_REFRESH_AQ) {
1229 const uint8_t *const map = cm->seg.update_map ? cpi->segmentation_map
1230 : cm->last_frame_seg_map;
1231 // If segment is boosted, use rdmult for that segment.
1232 if (cyclic_refresh_segment_id_boosted(
1233 vp9_get_segment_id(cm, map, bsize, mi_row, mi_col)))
1234 x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
1237 // Find best coding mode & reconstruct the MB so it is available
1238 // as a predictor for MBs that follow in the SB
1239 if (frame_is_intra_only(cm)) {
1240 vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, best_rd);
1242 if (bsize >= BLOCK_8X8) {
1243 if (vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
1244 vp9_rd_pick_inter_mode_sb_seg_skip(cpi, tile_data, x, rd_cost, bsize,
1247 vp9_rd_pick_inter_mode_sb(cpi, tile_data, x, mi_row, mi_col,
1248 rd_cost, bsize, ctx, best_rd);
1250 vp9_rd_pick_inter_mode_sub8x8(cpi, tile_data, x, mi_row, mi_col,
1251 rd_cost, bsize, ctx, best_rd);
1256 // Examine the resulting rate and for AQ mode 2 make a segment choice.
1257 if ((rd_cost->rate != INT_MAX) &&
1258 (aq_mode == COMPLEXITY_AQ) && (bsize >= BLOCK_16X16) &&
1259 (cm->frame_type == KEY_FRAME ||
1260 cpi->refresh_alt_ref_frame ||
1261 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref))) {
1262 vp9_caq_select_segment(cpi, x, bsize, mi_row, mi_col, rd_cost->rate);
1265 x->rdmult = orig_rdmult;
1267 // TODO(jingning) The rate-distortion optimization flow needs to be
1268 // refactored to provide proper exit/return handle.
1269 if (rd_cost->rate == INT_MAX)
1270 rd_cost->rdcost = INT64_MAX;
1272 ctx->rate = rd_cost->rate;
1273 ctx->dist = rd_cost->dist;
1276 static void update_stats(VP9_COMMON *cm, ThreadData *td) {
1277 const MACROBLOCK *x = &td->mb;
1278 const MACROBLOCKD *const xd = &x->e_mbd;
1279 const MODE_INFO *const mi = xd->mi[0];
1280 const MB_MODE_INFO *const mbmi = &mi->mbmi;
1281 const BLOCK_SIZE bsize = mbmi->sb_type;
1283 if (!frame_is_intra_only(cm)) {
1284 FRAME_COUNTS *const counts = td->counts;
1285 const int inter_block = is_inter_block(mbmi);
1286 const int seg_ref_active = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
1288 if (!seg_ref_active) {
1289 counts->intra_inter[vp9_get_intra_inter_context(xd)][inter_block]++;
1290 // If the segment reference feature is enabled we have only a single
1291 // reference frame allowed for the segment so exclude it from
1292 // the reference frame counts used to work out probabilities.
1294 const MV_REFERENCE_FRAME ref0 = mbmi->ref_frame[0];
1295 if (cm->reference_mode == REFERENCE_MODE_SELECT)
1296 counts->comp_inter[vp9_get_reference_mode_context(cm, xd)]
1297 [has_second_ref(mbmi)]++;
1299 if (has_second_ref(mbmi)) {
1300 counts->comp_ref[vp9_get_pred_context_comp_ref_p(cm, xd)]
1301 [ref0 == GOLDEN_FRAME]++;
1303 counts->single_ref[vp9_get_pred_context_single_ref_p1(xd)][0]
1304 [ref0 != LAST_FRAME]++;
1305 if (ref0 != LAST_FRAME)
1306 counts->single_ref[vp9_get_pred_context_single_ref_p2(xd)][1]
1307 [ref0 != GOLDEN_FRAME]++;
1312 !vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
1313 const int mode_ctx = mbmi->mode_context[mbmi->ref_frame[0]];
1314 if (bsize >= BLOCK_8X8) {
1315 const PREDICTION_MODE mode = mbmi->mode;
1316 ++counts->inter_mode[mode_ctx][INTER_OFFSET(mode)];
1318 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
1319 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
1321 for (idy = 0; idy < 2; idy += num_4x4_h) {
1322 for (idx = 0; idx < 2; idx += num_4x4_w) {
1323 const int j = idy * 2 + idx;
1324 const PREDICTION_MODE b_mode = mi->bmi[j].as_mode;
1325 ++counts->inter_mode[mode_ctx][INTER_OFFSET(b_mode)];
1333 static void restore_context(MACROBLOCK *const x, int mi_row, int mi_col,
1334 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
1335 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
1336 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
1338 MACROBLOCKD *const xd = &x->e_mbd;
1340 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1341 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1342 int mi_width = num_8x8_blocks_wide_lookup[bsize];
1343 int mi_height = num_8x8_blocks_high_lookup[bsize];
1344 for (p = 0; p < MAX_MB_PLANE; p++) {
1346 xd->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
1347 a + num_4x4_blocks_wide * p,
1348 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
1349 xd->plane[p].subsampling_x);
1352 + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
1353 l + num_4x4_blocks_high * p,
1354 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
1355 xd->plane[p].subsampling_y);
1357 memcpy(xd->above_seg_context + mi_col, sa,
1358 sizeof(*xd->above_seg_context) * mi_width);
1359 memcpy(xd->left_seg_context + (mi_row & MI_MASK), sl,
1360 sizeof(xd->left_seg_context[0]) * mi_height);
1363 static void save_context(MACROBLOCK *const x, int mi_row, int mi_col,
1364 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
1365 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
1366 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
1368 const MACROBLOCKD *const xd = &x->e_mbd;
1370 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
1371 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
1372 int mi_width = num_8x8_blocks_wide_lookup[bsize];
1373 int mi_height = num_8x8_blocks_high_lookup[bsize];
1375 // buffer the above/left context information of the block in search.
1376 for (p = 0; p < MAX_MB_PLANE; ++p) {
1378 a + num_4x4_blocks_wide * p,
1379 xd->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
1380 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
1381 xd->plane[p].subsampling_x);
1383 l + num_4x4_blocks_high * p,
1385 + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
1386 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
1387 xd->plane[p].subsampling_y);
1389 memcpy(sa, xd->above_seg_context + mi_col,
1390 sizeof(*xd->above_seg_context) * mi_width);
1391 memcpy(sl, xd->left_seg_context + (mi_row & MI_MASK),
1392 sizeof(xd->left_seg_context[0]) * mi_height);
1395 static void encode_b(VP9_COMP *cpi, const TileInfo *const tile,
1397 TOKENEXTRA **tp, int mi_row, int mi_col,
1398 int output_enabled, BLOCK_SIZE bsize,
1399 PICK_MODE_CONTEXT *ctx) {
1400 MACROBLOCK *const x = &td->mb;
1401 set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
1402 update_state(cpi, td, ctx, mi_row, mi_col, bsize, output_enabled);
1403 encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
1405 if (output_enabled) {
1406 update_stats(&cpi->common, td);
1408 (*tp)->token = EOSB_TOKEN;
1413 static void encode_sb(VP9_COMP *cpi, ThreadData *td,
1414 const TileInfo *const tile,
1415 TOKENEXTRA **tp, int mi_row, int mi_col,
1416 int output_enabled, BLOCK_SIZE bsize,
1418 VP9_COMMON *const cm = &cpi->common;
1419 MACROBLOCK *const x = &td->mb;
1420 MACROBLOCKD *const xd = &x->e_mbd;
1422 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
1424 PARTITION_TYPE partition;
1425 BLOCK_SIZE subsize = bsize;
1427 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1430 if (bsize >= BLOCK_8X8) {
1431 ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1432 subsize = get_subsize(bsize, pc_tree->partitioning);
1435 subsize = BLOCK_4X4;
1438 partition = partition_lookup[bsl][subsize];
1439 if (output_enabled && bsize != BLOCK_4X4)
1440 td->counts->partition[ctx][partition]++;
1442 switch (partition) {
1443 case PARTITION_NONE:
1444 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1447 case PARTITION_VERT:
1448 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1449 &pc_tree->vertical[0]);
1450 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
1451 encode_b(cpi, tile, td, tp, mi_row, mi_col + hbs, output_enabled,
1452 subsize, &pc_tree->vertical[1]);
1455 case PARTITION_HORZ:
1456 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1457 &pc_tree->horizontal[0]);
1458 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
1459 encode_b(cpi, tile, td, tp, mi_row + hbs, mi_col, output_enabled,
1460 subsize, &pc_tree->horizontal[1]);
1463 case PARTITION_SPLIT:
1464 if (bsize == BLOCK_8X8) {
1465 encode_b(cpi, tile, td, tp, mi_row, mi_col, output_enabled, subsize,
1466 pc_tree->leaf_split[0]);
1468 encode_sb(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1470 encode_sb(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
1471 subsize, pc_tree->split[1]);
1472 encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
1473 subsize, pc_tree->split[2]);
1474 encode_sb(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs, output_enabled,
1475 subsize, pc_tree->split[3]);
1479 assert(0 && "Invalid partition type.");
1483 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
1484 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1487 // Check to see if the given partition size is allowed for a specified number
1488 // of 8x8 block rows and columns remaining in the image.
1489 // If not then return the largest allowed partition size
1490 static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize,
1491 int rows_left, int cols_left,
1493 if (rows_left <= 0 || cols_left <= 0) {
1494 return MIN(bsize, BLOCK_8X8);
1496 for (; bsize > 0; bsize -= 3) {
1497 *bh = num_8x8_blocks_high_lookup[bsize];
1498 *bw = num_8x8_blocks_wide_lookup[bsize];
1499 if ((*bh <= rows_left) && (*bw <= cols_left)) {
1507 static void set_partial_b64x64_partition(MODE_INFO *mi, int mis,
1508 int bh_in, int bw_in, int row8x8_remaining, int col8x8_remaining,
1509 BLOCK_SIZE bsize, MODE_INFO **mi_8x8) {
1512 for (r = 0; r < MI_BLOCK_SIZE; r += bh) {
1514 for (c = 0; c < MI_BLOCK_SIZE; c += bw) {
1515 const int index = r * mis + c;
1516 mi_8x8[index] = mi + index;
1517 mi_8x8[index]->mbmi.sb_type = find_partition_size(bsize,
1518 row8x8_remaining - r, col8x8_remaining - c, &bh, &bw);
1523 // This function attempts to set all mode info entries in a given SB64
1524 // to the same block partition size.
1525 // However, at the bottom and right borders of the image the requested size
1526 // may not be allowed in which case this code attempts to choose the largest
1527 // allowable partition.
1528 static void set_fixed_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
1529 MODE_INFO **mi_8x8, int mi_row, int mi_col,
1531 VP9_COMMON *const cm = &cpi->common;
1532 const int mis = cm->mi_stride;
1533 const int row8x8_remaining = tile->mi_row_end - mi_row;
1534 const int col8x8_remaining = tile->mi_col_end - mi_col;
1535 int block_row, block_col;
1536 MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
1537 int bh = num_8x8_blocks_high_lookup[bsize];
1538 int bw = num_8x8_blocks_wide_lookup[bsize];
1540 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
1542 // Apply the requested partition size to the SB64 if it is all "in image"
1543 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
1544 (row8x8_remaining >= MI_BLOCK_SIZE)) {
1545 for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
1546 for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
1547 int index = block_row * mis + block_col;
1548 mi_8x8[index] = mi_upper_left + index;
1549 mi_8x8[index]->mbmi.sb_type = bsize;
1553 // Else this is a partial SB64.
1554 set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
1555 col8x8_remaining, bsize, mi_8x8);
1562 } coord_lookup[16] = {
1564 {0, 0}, {0, 2}, {2, 0}, {2, 2},
1566 {0, 4}, {0, 6}, {2, 4}, {2, 6},
1568 {4, 0}, {4, 2}, {6, 0}, {6, 2},
1570 {4, 4}, {4, 6}, {6, 4}, {6, 6},
1573 static void set_source_var_based_partition(VP9_COMP *cpi,
1574 const TileInfo *const tile,
1575 MACROBLOCK *const x,
1577 int mi_row, int mi_col) {
1578 VP9_COMMON *const cm = &cpi->common;
1579 const int mis = cm->mi_stride;
1580 const int row8x8_remaining = tile->mi_row_end - mi_row;
1581 const int col8x8_remaining = tile->mi_col_end - mi_col;
1582 MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
1584 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
1586 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
1589 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
1590 (row8x8_remaining >= MI_BLOCK_SIZE)) {
1594 const int offset = (mi_row >> 1) * cm->mb_cols + (mi_col >> 1);
1595 int is_larger_better = 0;
1597 unsigned int thr = cpi->source_var_thresh;
1599 memset(d32, 0, 4 * sizeof(diff));
1601 for (i = 0; i < 4; i++) {
1604 for (j = 0; j < 4; j++) {
1605 int b_mi_row = coord_lookup[i * 4 + j].row;
1606 int b_mi_col = coord_lookup[i * 4 + j].col;
1607 int boffset = b_mi_row / 2 * cm->mb_cols +
1610 d16[j] = cpi->source_diff_var + offset + boffset;
1612 index = b_mi_row * mis + b_mi_col;
1613 mi_8x8[index] = mi_upper_left + index;
1614 mi_8x8[index]->mbmi.sb_type = BLOCK_16X16;
1616 // TODO(yunqingwang): If d16[j].var is very large, use 8x8 partition
1617 // size to further improve quality.
1620 is_larger_better = (d16[0]->var < thr) && (d16[1]->var < thr) &&
1621 (d16[2]->var < thr) && (d16[3]->var < thr);
1623 // Use 32x32 partition
1624 if (is_larger_better) {
1627 for (j = 0; j < 4; j++) {
1628 d32[i].sse += d16[j]->sse;
1629 d32[i].sum += d16[j]->sum;
1632 d32[i].var = d32[i].sse - (((int64_t)d32[i].sum * d32[i].sum) >> 10);
1634 index = coord_lookup[i*4].row * mis + coord_lookup[i*4].col;
1635 mi_8x8[index] = mi_upper_left + index;
1636 mi_8x8[index]->mbmi.sb_type = BLOCK_32X32;
1640 if (use32x32 == 4) {
1642 is_larger_better = (d32[0].var < thr) && (d32[1].var < thr) &&
1643 (d32[2].var < thr) && (d32[3].var < thr);
1645 // Use 64x64 partition
1646 if (is_larger_better) {
1647 mi_8x8[0] = mi_upper_left;
1648 mi_8x8[0]->mbmi.sb_type = BLOCK_64X64;
1651 } else { // partial in-image SB64
1652 int bh = num_8x8_blocks_high_lookup[BLOCK_16X16];
1653 int bw = num_8x8_blocks_wide_lookup[BLOCK_16X16];
1654 set_partial_b64x64_partition(mi_upper_left, mis, bh, bw,
1655 row8x8_remaining, col8x8_remaining, BLOCK_16X16, mi_8x8);
1659 static void update_state_rt(VP9_COMP *cpi, ThreadData *td,
1660 PICK_MODE_CONTEXT *ctx,
1661 int mi_row, int mi_col, int bsize) {
1662 VP9_COMMON *const cm = &cpi->common;
1663 MACROBLOCK *const x = &td->mb;
1664 MACROBLOCKD *const xd = &x->e_mbd;
1665 MODE_INFO *const mi = xd->mi[0];
1666 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
1667 const struct segmentation *const seg = &cm->seg;
1668 const int bw = num_8x8_blocks_wide_lookup[mi->mbmi.sb_type];
1669 const int bh = num_8x8_blocks_high_lookup[mi->mbmi.sb_type];
1670 const int x_mis = MIN(bw, cm->mi_cols - mi_col);
1671 const int y_mis = MIN(bh, cm->mi_rows - mi_row);
1673 *(xd->mi[0]) = ctx->mic;
1675 if (seg->enabled && cpi->oxcf.aq_mode) {
1676 // For in frame complexity AQ or variance AQ, copy segment_id from
1677 // segmentation_map.
1678 if (cpi->oxcf.aq_mode == COMPLEXITY_AQ ||
1679 cpi->oxcf.aq_mode == VARIANCE_AQ ) {
1680 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
1681 : cm->last_frame_seg_map;
1682 mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
1684 // Setting segmentation map for cyclic_refresh.
1685 vp9_cyclic_refresh_update_segment(cpi, mbmi, mi_row, mi_col, bsize,
1686 ctx->rate, ctx->dist, x->skip);
1688 vp9_init_plane_quantizers(cpi, x);
1691 if (is_inter_block(mbmi)) {
1692 vp9_update_mv_count(td);
1693 if (cm->interp_filter == SWITCHABLE) {
1694 const int pred_ctx = vp9_get_pred_context_switchable_interp(xd);
1695 ++td->counts->switchable_interp[pred_ctx][mbmi->interp_filter];
1698 if (mbmi->sb_type < BLOCK_8X8) {
1699 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
1700 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
1704 if (cm->use_prev_frame_mvs) {
1705 MV_REF *const frame_mvs =
1706 cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
1709 for (h = 0; h < y_mis; ++h) {
1710 MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
1711 for (w = 0; w < x_mis; ++w) {
1712 MV_REF *const mv = frame_mv + w;
1713 mv->ref_frame[0] = mi->mbmi.ref_frame[0];
1714 mv->ref_frame[1] = mi->mbmi.ref_frame[1];
1715 mv->mv[0].as_int = mi->mbmi.mv[0].as_int;
1716 mv->mv[1].as_int = mi->mbmi.mv[1].as_int;
1721 x->skip = ctx->skip;
1722 x->skip_txfm[0] = mbmi->segment_id ? 0 : ctx->skip_txfm[0];
1725 static void encode_b_rt(VP9_COMP *cpi, ThreadData *td,
1726 const TileInfo *const tile,
1727 TOKENEXTRA **tp, int mi_row, int mi_col,
1728 int output_enabled, BLOCK_SIZE bsize,
1729 PICK_MODE_CONTEXT *ctx) {
1730 MACROBLOCK *const x = &td->mb;
1731 set_offsets(cpi, tile, x, mi_row, mi_col, bsize);
1732 update_state_rt(cpi, td, ctx, mi_row, mi_col, bsize);
1734 #if CONFIG_VP9_TEMPORAL_DENOISING
1735 if (cpi->oxcf.noise_sensitivity > 0 && output_enabled &&
1736 cpi->common.frame_type != KEY_FRAME) {
1737 vp9_denoiser_denoise(&cpi->denoiser, x, mi_row, mi_col,
1738 MAX(BLOCK_8X8, bsize), ctx);
1742 encode_superblock(cpi, td, tp, output_enabled, mi_row, mi_col, bsize, ctx);
1743 update_stats(&cpi->common, td);
1745 (*tp)->token = EOSB_TOKEN;
1749 static void encode_sb_rt(VP9_COMP *cpi, ThreadData *td,
1750 const TileInfo *const tile,
1751 TOKENEXTRA **tp, int mi_row, int mi_col,
1752 int output_enabled, BLOCK_SIZE bsize,
1754 VP9_COMMON *const cm = &cpi->common;
1755 MACROBLOCK *const x = &td->mb;
1756 MACROBLOCKD *const xd = &x->e_mbd;
1758 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
1760 PARTITION_TYPE partition;
1763 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1766 if (bsize >= BLOCK_8X8) {
1767 const int idx_str = xd->mi_stride * mi_row + mi_col;
1768 MODE_INFO ** mi_8x8 = cm->mi_grid_visible + idx_str;
1769 ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1770 subsize = mi_8x8[0]->mbmi.sb_type;
1773 subsize = BLOCK_4X4;
1776 partition = partition_lookup[bsl][subsize];
1777 if (output_enabled && bsize != BLOCK_4X4)
1778 td->counts->partition[ctx][partition]++;
1780 switch (partition) {
1781 case PARTITION_NONE:
1782 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1785 case PARTITION_VERT:
1786 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1787 &pc_tree->vertical[0]);
1788 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
1789 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
1790 subsize, &pc_tree->vertical[1]);
1793 case PARTITION_HORZ:
1794 encode_b_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1795 &pc_tree->horizontal[0]);
1796 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
1797 encode_b_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
1798 subsize, &pc_tree->horizontal[1]);
1801 case PARTITION_SPLIT:
1802 subsize = get_subsize(bsize, PARTITION_SPLIT);
1803 encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col, output_enabled, subsize,
1805 encode_sb_rt(cpi, td, tile, tp, mi_row, mi_col + hbs, output_enabled,
1806 subsize, pc_tree->split[1]);
1807 encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col, output_enabled,
1808 subsize, pc_tree->split[2]);
1809 encode_sb_rt(cpi, td, tile, tp, mi_row + hbs, mi_col + hbs,
1810 output_enabled, subsize, pc_tree->split[3]);
1813 assert(0 && "Invalid partition type.");
1817 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
1818 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1821 static void rd_use_partition(VP9_COMP *cpi,
1823 TileDataEnc *tile_data,
1824 MODE_INFO **mi_8x8, TOKENEXTRA **tp,
1825 int mi_row, int mi_col,
1827 int *rate, int64_t *dist,
1828 int do_recon, PC_TREE *pc_tree) {
1829 VP9_COMMON *const cm = &cpi->common;
1830 TileInfo *const tile_info = &tile_data->tile_info;
1831 MACROBLOCK *const x = &td->mb;
1832 MACROBLOCKD *const xd = &x->e_mbd;
1833 const int mis = cm->mi_stride;
1834 const int bsl = b_width_log2_lookup[bsize];
1835 const int mi_step = num_4x4_blocks_wide_lookup[bsize] / 2;
1836 const int bss = (1 << bsl) / 4;
1838 PARTITION_TYPE partition = PARTITION_NONE;
1840 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1841 PARTITION_CONTEXT sl[8], sa[8];
1842 RD_COST last_part_rdc, none_rdc, chosen_rdc;
1843 BLOCK_SIZE sub_subsize = BLOCK_4X4;
1844 int splits_below = 0;
1845 BLOCK_SIZE bs_type = mi_8x8[0]->mbmi.sb_type;
1846 int do_partition_search = 1;
1847 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
1849 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1852 assert(num_4x4_blocks_wide_lookup[bsize] ==
1853 num_4x4_blocks_high_lookup[bsize]);
1855 vp9_rd_cost_reset(&last_part_rdc);
1856 vp9_rd_cost_reset(&none_rdc);
1857 vp9_rd_cost_reset(&chosen_rdc);
1859 partition = partition_lookup[bsl][bs_type];
1860 subsize = get_subsize(bsize, partition);
1862 pc_tree->partitioning = partition;
1863 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
1865 if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode) {
1866 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
1867 x->mb_energy = vp9_block_energy(cpi, x, bsize);
1870 if (do_partition_search &&
1871 cpi->sf.partition_search_type == SEARCH_PARTITION &&
1872 cpi->sf.adjust_partitioning_from_last_frame) {
1873 // Check if any of the sub blocks are further split.
1874 if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
1875 sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
1877 for (i = 0; i < 4; i++) {
1878 int jj = i >> 1, ii = i & 0x01;
1879 MODE_INFO *this_mi = mi_8x8[jj * bss * mis + ii * bss];
1880 if (this_mi && this_mi->mbmi.sb_type >= sub_subsize) {
1886 // If partition is not none try none unless each of the 4 splits are split
1888 if (partition != PARTITION_NONE && !splits_below &&
1889 mi_row + (mi_step >> 1) < cm->mi_rows &&
1890 mi_col + (mi_step >> 1) < cm->mi_cols) {
1891 pc_tree->partitioning = PARTITION_NONE;
1892 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &none_rdc, bsize,
1895 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
1897 if (none_rdc.rate < INT_MAX) {
1898 none_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
1899 none_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, none_rdc.rate,
1903 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
1904 mi_8x8[0]->mbmi.sb_type = bs_type;
1905 pc_tree->partitioning = partition;
1909 switch (partition) {
1910 case PARTITION_NONE:
1911 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1912 bsize, ctx, INT64_MAX);
1914 case PARTITION_HORZ:
1915 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1916 subsize, &pc_tree->horizontal[0],
1918 if (last_part_rdc.rate != INT_MAX &&
1919 bsize >= BLOCK_8X8 && mi_row + (mi_step >> 1) < cm->mi_rows) {
1921 PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
1922 vp9_rd_cost_init(&tmp_rdc);
1923 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
1924 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
1925 rd_pick_sb_modes(cpi, tile_data, x,
1926 mi_row + (mi_step >> 1), mi_col, &tmp_rdc,
1927 subsize, &pc_tree->horizontal[1], INT64_MAX);
1928 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
1929 vp9_rd_cost_reset(&last_part_rdc);
1932 last_part_rdc.rate += tmp_rdc.rate;
1933 last_part_rdc.dist += tmp_rdc.dist;
1934 last_part_rdc.rdcost += tmp_rdc.rdcost;
1937 case PARTITION_VERT:
1938 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1939 subsize, &pc_tree->vertical[0], INT64_MAX);
1940 if (last_part_rdc.rate != INT_MAX &&
1941 bsize >= BLOCK_8X8 && mi_col + (mi_step >> 1) < cm->mi_cols) {
1943 PICK_MODE_CONTEXT *ctx = &pc_tree->vertical[0];
1944 vp9_rd_cost_init(&tmp_rdc);
1945 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
1946 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
1947 rd_pick_sb_modes(cpi, tile_data, x,
1948 mi_row, mi_col + (mi_step >> 1), &tmp_rdc,
1949 subsize, &pc_tree->vertical[bsize > BLOCK_8X8],
1951 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
1952 vp9_rd_cost_reset(&last_part_rdc);
1955 last_part_rdc.rate += tmp_rdc.rate;
1956 last_part_rdc.dist += tmp_rdc.dist;
1957 last_part_rdc.rdcost += tmp_rdc.rdcost;
1960 case PARTITION_SPLIT:
1961 if (bsize == BLOCK_8X8) {
1962 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &last_part_rdc,
1963 subsize, pc_tree->leaf_split[0], INT64_MAX);
1966 last_part_rdc.rate = 0;
1967 last_part_rdc.dist = 0;
1968 last_part_rdc.rdcost = 0;
1969 for (i = 0; i < 4; i++) {
1970 int x_idx = (i & 1) * (mi_step >> 1);
1971 int y_idx = (i >> 1) * (mi_step >> 1);
1972 int jj = i >> 1, ii = i & 0x01;
1974 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1977 vp9_rd_cost_init(&tmp_rdc);
1978 rd_use_partition(cpi, td, tile_data,
1979 mi_8x8 + jj * bss * mis + ii * bss, tp,
1980 mi_row + y_idx, mi_col + x_idx, subsize,
1981 &tmp_rdc.rate, &tmp_rdc.dist,
1982 i != 3, pc_tree->split[i]);
1983 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
1984 vp9_rd_cost_reset(&last_part_rdc);
1987 last_part_rdc.rate += tmp_rdc.rate;
1988 last_part_rdc.dist += tmp_rdc.dist;
1996 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
1997 if (last_part_rdc.rate < INT_MAX) {
1998 last_part_rdc.rate += cpi->partition_cost[pl][partition];
1999 last_part_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
2000 last_part_rdc.rate, last_part_rdc.dist);
2003 if (do_partition_search
2004 && cpi->sf.adjust_partitioning_from_last_frame
2005 && cpi->sf.partition_search_type == SEARCH_PARTITION
2006 && partition != PARTITION_SPLIT && bsize > BLOCK_8X8
2007 && (mi_row + mi_step < cm->mi_rows ||
2008 mi_row + (mi_step >> 1) == cm->mi_rows)
2009 && (mi_col + mi_step < cm->mi_cols ||
2010 mi_col + (mi_step >> 1) == cm->mi_cols)) {
2011 BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
2012 chosen_rdc.rate = 0;
2013 chosen_rdc.dist = 0;
2014 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2015 pc_tree->partitioning = PARTITION_SPLIT;
2018 for (i = 0; i < 4; i++) {
2019 int x_idx = (i & 1) * (mi_step >> 1);
2020 int y_idx = (i >> 1) * (mi_step >> 1);
2022 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2023 PARTITION_CONTEXT sl[8], sa[8];
2025 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
2028 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2029 pc_tree->split[i]->partitioning = PARTITION_NONE;
2030 rd_pick_sb_modes(cpi, tile_data, x,
2031 mi_row + y_idx, mi_col + x_idx, &tmp_rdc,
2032 split_subsize, &pc_tree->split[i]->none, INT64_MAX);
2034 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2036 if (tmp_rdc.rate == INT_MAX || tmp_rdc.dist == INT64_MAX) {
2037 vp9_rd_cost_reset(&chosen_rdc);
2041 chosen_rdc.rate += tmp_rdc.rate;
2042 chosen_rdc.dist += tmp_rdc.dist;
2045 encode_sb(cpi, td, tile_info, tp, mi_row + y_idx, mi_col + x_idx, 0,
2046 split_subsize, pc_tree->split[i]);
2048 pl = partition_plane_context(xd, mi_row + y_idx, mi_col + x_idx,
2050 chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
2052 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2053 if (chosen_rdc.rate < INT_MAX) {
2054 chosen_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
2055 chosen_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
2056 chosen_rdc.rate, chosen_rdc.dist);
2060 // If last_part is better set the partitioning to that.
2061 if (last_part_rdc.rdcost < chosen_rdc.rdcost) {
2062 mi_8x8[0]->mbmi.sb_type = bsize;
2063 if (bsize >= BLOCK_8X8)
2064 pc_tree->partitioning = partition;
2065 chosen_rdc = last_part_rdc;
2067 // If none was better set the partitioning to that.
2068 if (none_rdc.rdcost < chosen_rdc.rdcost) {
2069 if (bsize >= BLOCK_8X8)
2070 pc_tree->partitioning = PARTITION_NONE;
2071 chosen_rdc = none_rdc;
2074 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2076 // We must have chosen a partitioning and encoding or we'll fail later on.
2077 // No other opportunities for success.
2078 if (bsize == BLOCK_64X64)
2079 assert(chosen_rdc.rate < INT_MAX && chosen_rdc.dist < INT64_MAX);
2082 int output_enabled = (bsize == BLOCK_64X64);
2083 encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled, bsize,
2087 *rate = chosen_rdc.rate;
2088 *dist = chosen_rdc.dist;
2091 static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
2092 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
2093 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
2094 BLOCK_8X8, BLOCK_8X8, BLOCK_8X8,
2095 BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
2099 static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
2100 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16,
2101 BLOCK_16X16, BLOCK_32X32, BLOCK_32X32,
2102 BLOCK_32X32, BLOCK_64X64, BLOCK_64X64,
2103 BLOCK_64X64, BLOCK_64X64, BLOCK_64X64,
2107 // Look at all the mode_info entries for blocks that are part of this
2108 // partition and find the min and max values for sb_type.
2109 // At the moment this is designed to work on a 64x64 SB but could be
2110 // adjusted to use a size parameter.
2112 // The min and max are assumed to have been initialized prior to calling this
2113 // function so repeat calls can accumulate a min and max of more than one sb64.
2114 static void get_sb_partition_size_range(MACROBLOCKD *xd, MODE_INFO **mi_8x8,
2115 BLOCK_SIZE *min_block_size,
2116 BLOCK_SIZE *max_block_size,
2117 int bs_hist[BLOCK_SIZES]) {
2118 int sb_width_in_blocks = MI_BLOCK_SIZE;
2119 int sb_height_in_blocks = MI_BLOCK_SIZE;
2123 // Check the sb_type for each block that belongs to this region.
2124 for (i = 0; i < sb_height_in_blocks; ++i) {
2125 for (j = 0; j < sb_width_in_blocks; ++j) {
2126 MODE_INFO *mi = mi_8x8[index+j];
2127 BLOCK_SIZE sb_type = mi ? mi->mbmi.sb_type : 0;
2129 *min_block_size = MIN(*min_block_size, sb_type);
2130 *max_block_size = MAX(*max_block_size, sb_type);
2132 index += xd->mi_stride;
2136 // Next square block size less or equal than current block size.
2137 static const BLOCK_SIZE next_square_size[BLOCK_SIZES] = {
2138 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
2139 BLOCK_8X8, BLOCK_8X8, BLOCK_8X8,
2140 BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
2141 BLOCK_32X32, BLOCK_32X32, BLOCK_32X32,
2145 // Look at neighboring blocks and set a min and max partition size based on
2147 static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
2148 MACROBLOCKD *const xd,
2149 int mi_row, int mi_col,
2150 BLOCK_SIZE *min_block_size,
2151 BLOCK_SIZE *max_block_size) {
2152 VP9_COMMON *const cm = &cpi->common;
2153 MODE_INFO **mi = xd->mi;
2154 const int left_in_image = xd->left_available && mi[-1];
2155 const int above_in_image = xd->up_available && mi[-xd->mi_stride];
2156 const int row8x8_remaining = tile->mi_row_end - mi_row;
2157 const int col8x8_remaining = tile->mi_col_end - mi_col;
2159 BLOCK_SIZE min_size = BLOCK_4X4;
2160 BLOCK_SIZE max_size = BLOCK_64X64;
2161 int bs_hist[BLOCK_SIZES] = {0};
2163 // Trap case where we do not have a prediction.
2164 if (left_in_image || above_in_image || cm->frame_type != KEY_FRAME) {
2165 // Default "min to max" and "max to min"
2166 min_size = BLOCK_64X64;
2167 max_size = BLOCK_4X4;
2169 // NOTE: each call to get_sb_partition_size_range() uses the previous
2170 // passed in values for min and max as a starting point.
2171 // Find the min and max partition used in previous frame at this location
2172 if (cm->frame_type != KEY_FRAME) {
2173 MODE_INFO **prev_mi =
2174 &cm->prev_mi_grid_visible[mi_row * xd->mi_stride + mi_col];
2175 get_sb_partition_size_range(xd, prev_mi, &min_size, &max_size, bs_hist);
2177 // Find the min and max partition sizes used in the left SB64
2178 if (left_in_image) {
2179 MODE_INFO **left_sb64_mi = &mi[-MI_BLOCK_SIZE];
2180 get_sb_partition_size_range(xd, left_sb64_mi, &min_size, &max_size,
2183 // Find the min and max partition sizes used in the above SB64.
2184 if (above_in_image) {
2185 MODE_INFO **above_sb64_mi = &mi[-xd->mi_stride * MI_BLOCK_SIZE];
2186 get_sb_partition_size_range(xd, above_sb64_mi, &min_size, &max_size,
2190 // Adjust observed min and max for "relaxed" auto partition case.
2191 if (cpi->sf.auto_min_max_partition_size == RELAXED_NEIGHBORING_MIN_MAX) {
2192 min_size = min_partition_size[min_size];
2193 max_size = max_partition_size[max_size];
2197 // Check border cases where max and min from neighbors may not be legal.
2198 max_size = find_partition_size(max_size,
2199 row8x8_remaining, col8x8_remaining,
2201 min_size = MIN(min_size, max_size);
2203 // When use_square_partition_only is true, make sure at least one square
2204 // partition is allowed by selecting the next smaller square size as
2206 if (cpi->sf.use_square_partition_only &&
2207 next_square_size[max_size] < min_size) {
2208 min_size = next_square_size[max_size];
2211 *min_block_size = min_size;
2212 *max_block_size = max_size;
2215 static void auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
2216 MACROBLOCKD *const xd,
2217 int mi_row, int mi_col,
2218 BLOCK_SIZE *min_block_size,
2219 BLOCK_SIZE *max_block_size) {
2220 VP9_COMMON *const cm = &cpi->common;
2221 MODE_INFO **mi_8x8 = xd->mi;
2222 const int left_in_image = xd->left_available && mi_8x8[-1];
2223 const int above_in_image = xd->up_available && mi_8x8[-xd->mi_stride];
2224 int row8x8_remaining = tile->mi_row_end - mi_row;
2225 int col8x8_remaining = tile->mi_col_end - mi_col;
2227 BLOCK_SIZE min_size = BLOCK_32X32;
2228 BLOCK_SIZE max_size = BLOCK_8X8;
2229 int bsl = mi_width_log2_lookup[BLOCK_64X64];
2230 const int search_range_ctrl = (((mi_row + mi_col) >> bsl) +
2231 get_chessboard_index(cm->current_video_frame)) & 0x1;
2232 // Trap case where we do not have a prediction.
2233 if (search_range_ctrl &&
2234 (left_in_image || above_in_image || cm->frame_type != KEY_FRAME)) {
2239 // Find the min and max partition sizes used in the left SB64.
2240 if (left_in_image) {
2243 for (block = 0; block < MI_BLOCK_SIZE; ++block) {
2244 cur_mi = mi[block * xd->mi_stride];
2245 sb_type = cur_mi ? cur_mi->mbmi.sb_type : 0;
2246 min_size = MIN(min_size, sb_type);
2247 max_size = MAX(max_size, sb_type);
2250 // Find the min and max partition sizes used in the above SB64.
2251 if (above_in_image) {
2252 mi = &mi_8x8[-xd->mi_stride * MI_BLOCK_SIZE];
2253 for (block = 0; block < MI_BLOCK_SIZE; ++block) {
2254 sb_type = mi[block] ? mi[block]->mbmi.sb_type : 0;
2255 min_size = MIN(min_size, sb_type);
2256 max_size = MAX(max_size, sb_type);
2260 min_size = min_partition_size[min_size];
2261 max_size = find_partition_size(max_size, row8x8_remaining, col8x8_remaining,
2263 min_size = MIN(min_size, max_size);
2264 min_size = MAX(min_size, BLOCK_8X8);
2265 max_size = MIN(max_size, BLOCK_32X32);
2267 min_size = BLOCK_8X8;
2268 max_size = BLOCK_32X32;
2271 *min_block_size = min_size;
2272 *max_block_size = max_size;
2275 // TODO(jingning) refactor functions setting partition search range
2276 static void set_partition_range(VP9_COMMON *cm, MACROBLOCKD *xd,
2277 int mi_row, int mi_col, BLOCK_SIZE bsize,
2278 BLOCK_SIZE *min_bs, BLOCK_SIZE *max_bs) {
2279 int mi_width = num_8x8_blocks_wide_lookup[bsize];
2280 int mi_height = num_8x8_blocks_high_lookup[bsize];
2284 const int idx_str = cm->mi_stride * mi_row + mi_col;
2285 MODE_INFO **prev_mi = &cm->prev_mi_grid_visible[idx_str];
2286 BLOCK_SIZE bs, min_size, max_size;
2288 min_size = BLOCK_64X64;
2289 max_size = BLOCK_4X4;
2292 for (idy = 0; idy < mi_height; ++idy) {
2293 for (idx = 0; idx < mi_width; ++idx) {
2294 mi = prev_mi[idy * cm->mi_stride + idx];
2295 bs = mi ? mi->mbmi.sb_type : bsize;
2296 min_size = MIN(min_size, bs);
2297 max_size = MAX(max_size, bs);
2302 if (xd->left_available) {
2303 for (idy = 0; idy < mi_height; ++idy) {
2304 mi = xd->mi[idy * cm->mi_stride - 1];
2305 bs = mi ? mi->mbmi.sb_type : bsize;
2306 min_size = MIN(min_size, bs);
2307 max_size = MAX(max_size, bs);
2311 if (xd->up_available) {
2312 for (idx = 0; idx < mi_width; ++idx) {
2313 mi = xd->mi[idx - cm->mi_stride];
2314 bs = mi ? mi->mbmi.sb_type : bsize;
2315 min_size = MIN(min_size, bs);
2316 max_size = MAX(max_size, bs);
2320 if (min_size == max_size) {
2321 min_size = min_partition_size[min_size];
2322 max_size = max_partition_size[max_size];
2329 static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
2330 memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
2333 static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
2334 memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
2337 #if CONFIG_FP_MB_STATS
2338 const int num_16x16_blocks_wide_lookup[BLOCK_SIZES] =
2339 {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 4, 4};
2340 const int num_16x16_blocks_high_lookup[BLOCK_SIZES] =
2341 {1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 4, 2, 4};
2342 const int qindex_skip_threshold_lookup[BLOCK_SIZES] =
2343 {0, 10, 10, 30, 40, 40, 60, 80, 80, 90, 100, 100, 120};
2344 const int qindex_split_threshold_lookup[BLOCK_SIZES] =
2345 {0, 3, 3, 7, 15, 15, 30, 40, 40, 60, 80, 80, 120};
2346 const int complexity_16x16_blocks_threshold[BLOCK_SIZES] =
2347 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 6};
2358 static INLINE MOTION_DIRECTION get_motion_direction_fp(uint8_t fp_byte) {
2359 if (fp_byte & FPMB_MOTION_ZERO_MASK) {
2361 } else if (fp_byte & FPMB_MOTION_LEFT_MASK) {
2363 } else if (fp_byte & FPMB_MOTION_RIGHT_MASK) {
2365 } else if (fp_byte & FPMB_MOTION_UP_MASK) {
2372 static INLINE int get_motion_inconsistency(MOTION_DIRECTION this_mv,
2373 MOTION_DIRECTION that_mv) {
2374 if (this_mv == that_mv) {
2377 return abs(this_mv - that_mv) == 2 ? 2 : 1;
2382 // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
2383 // unlikely to be selected depending on previous rate-distortion optimization
2384 // results, for encoding speed-up.
2385 static void rd_pick_partition(VP9_COMP *cpi, ThreadData *td,
2386 TileDataEnc *tile_data,
2387 TOKENEXTRA **tp, int mi_row, int mi_col,
2388 BLOCK_SIZE bsize, RD_COST *rd_cost,
2389 int64_t best_rd, PC_TREE *pc_tree) {
2390 VP9_COMMON *const cm = &cpi->common;
2391 TileInfo *const tile_info = &tile_data->tile_info;
2392 MACROBLOCK *const x = &td->mb;
2393 MACROBLOCKD *const xd = &x->e_mbd;
2394 const int mi_step = num_8x8_blocks_wide_lookup[bsize] / 2;
2395 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
2396 PARTITION_CONTEXT sl[8], sa[8];
2397 TOKENEXTRA *tp_orig = *tp;
2398 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
2401 RD_COST this_rdc, sum_rdc, best_rdc;
2402 int do_split = bsize >= BLOCK_8X8;
2405 // Override skipping rectangular partition operations for edge blocks
2406 const int force_horz_split = (mi_row + mi_step >= cm->mi_rows);
2407 const int force_vert_split = (mi_col + mi_step >= cm->mi_cols);
2408 const int xss = x->e_mbd.plane[1].subsampling_x;
2409 const int yss = x->e_mbd.plane[1].subsampling_y;
2411 BLOCK_SIZE min_size = x->min_partition_size;
2412 BLOCK_SIZE max_size = x->max_partition_size;
2414 #if CONFIG_FP_MB_STATS
2415 unsigned int src_diff_var = UINT_MAX;
2416 int none_complexity = 0;
2419 int partition_none_allowed = !force_horz_split && !force_vert_split;
2420 int partition_horz_allowed = !force_vert_split && yss <= xss &&
2422 int partition_vert_allowed = !force_horz_split && xss <= yss &&
2426 assert(num_8x8_blocks_wide_lookup[bsize] ==
2427 num_8x8_blocks_high_lookup[bsize]);
2429 vp9_rd_cost_init(&this_rdc);
2430 vp9_rd_cost_init(&sum_rdc);
2431 vp9_rd_cost_reset(&best_rdc);
2432 best_rdc.rdcost = best_rd;
2434 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2436 if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode)
2437 x->mb_energy = vp9_block_energy(cpi, x, bsize);
2439 if (cpi->sf.cb_partition_search && bsize == BLOCK_16X16) {
2440 int cb_partition_search_ctrl = ((pc_tree->index == 0 || pc_tree->index == 3)
2441 + get_chessboard_index(cm->current_video_frame)) & 0x1;
2443 if (cb_partition_search_ctrl && bsize > min_size && bsize < max_size)
2444 set_partition_range(cm, xd, mi_row, mi_col, bsize, &min_size, &max_size);
2447 // Determine partition types in search according to the speed features.
2448 // The threshold set here has to be of square block size.
2449 if (cpi->sf.auto_min_max_partition_size) {
2450 partition_none_allowed &= (bsize <= max_size && bsize >= min_size);
2451 partition_horz_allowed &= ((bsize <= max_size && bsize > min_size) ||
2453 partition_vert_allowed &= ((bsize <= max_size && bsize > min_size) ||
2455 do_split &= bsize > min_size;
2457 if (cpi->sf.use_square_partition_only) {
2458 partition_horz_allowed &= force_horz_split;
2459 partition_vert_allowed &= force_vert_split;
2462 save_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2464 #if CONFIG_FP_MB_STATS
2465 if (cpi->use_fp_mb_stats) {
2466 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2467 src_diff_var = get_sby_perpixel_diff_variance(cpi, &x->plane[0].src,
2468 mi_row, mi_col, bsize);
2472 #if CONFIG_FP_MB_STATS
2473 // Decide whether we shall split directly and skip searching NONE by using
2474 // the first pass block statistics
2475 if (cpi->use_fp_mb_stats && bsize >= BLOCK_32X32 && do_split &&
2476 partition_none_allowed && src_diff_var > 4 &&
2477 cm->base_qindex < qindex_split_threshold_lookup[bsize]) {
2478 int mb_row = mi_row >> 1;
2479 int mb_col = mi_col >> 1;
2481 MIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
2483 MIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
2486 // compute a complexity measure, basically measure inconsistency of motion
2487 // vectors obtained from the first pass in the current block
2488 for (r = mb_row; r < mb_row_end ; r++) {
2489 for (c = mb_col; c < mb_col_end; c++) {
2490 const int mb_index = r * cm->mb_cols + c;
2492 MOTION_DIRECTION this_mv;
2493 MOTION_DIRECTION right_mv;
2494 MOTION_DIRECTION bottom_mv;
2497 get_motion_direction_fp(cpi->twopass.this_frame_mb_stats[mb_index]);
2500 if (c != mb_col_end - 1) {
2501 right_mv = get_motion_direction_fp(
2502 cpi->twopass.this_frame_mb_stats[mb_index + 1]);
2503 none_complexity += get_motion_inconsistency(this_mv, right_mv);
2507 if (r != mb_row_end - 1) {
2508 bottom_mv = get_motion_direction_fp(
2509 cpi->twopass.this_frame_mb_stats[mb_index + cm->mb_cols]);
2510 none_complexity += get_motion_inconsistency(this_mv, bottom_mv);
2513 // do not count its left and top neighbors to avoid double counting
2517 if (none_complexity > complexity_16x16_blocks_threshold[bsize]) {
2518 partition_none_allowed = 0;
2524 if (partition_none_allowed) {
2525 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col,
2526 &this_rdc, bsize, ctx, best_rdc.rdcost);
2527 if (this_rdc.rate != INT_MAX) {
2528 if (bsize >= BLOCK_8X8) {
2529 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2530 this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
2531 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
2532 this_rdc.rate, this_rdc.dist);
2535 if (this_rdc.rdcost < best_rdc.rdcost) {
2536 int64_t dist_breakout_thr = cpi->sf.partition_search_breakout_dist_thr;
2537 int rate_breakout_thr = cpi->sf.partition_search_breakout_rate_thr;
2539 best_rdc = this_rdc;
2540 if (bsize >= BLOCK_8X8)
2541 pc_tree->partitioning = PARTITION_NONE;
2543 // Adjust dist breakout threshold according to the partition size.
2544 dist_breakout_thr >>= 8 - (b_width_log2_lookup[bsize] +
2545 b_height_log2_lookup[bsize]);
2547 rate_breakout_thr *= num_pels_log2_lookup[bsize];
2549 // If all y, u, v transform blocks in this partition are skippable, and
2550 // the dist & rate are within the thresholds, the partition search is
2551 // terminated for current branch of the partition search tree.
2552 // The dist & rate thresholds are set to 0 at speed 0 to disable the
2553 // early termination at that speed.
2554 if (!x->e_mbd.lossless &&
2555 (ctx->skippable && best_rdc.dist < dist_breakout_thr &&
2556 best_rdc.rate < rate_breakout_thr)) {
2561 #if CONFIG_FP_MB_STATS
2562 // Check if every 16x16 first pass block statistics has zero
2563 // motion and the corresponding first pass residue is small enough.
2564 // If that is the case, check the difference variance between the
2565 // current frame and the last frame. If the variance is small enough,
2566 // stop further splitting in RD optimization
2567 if (cpi->use_fp_mb_stats && do_split != 0 &&
2568 cm->base_qindex > qindex_skip_threshold_lookup[bsize]) {
2569 int mb_row = mi_row >> 1;
2570 int mb_col = mi_col >> 1;
2572 MIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
2574 MIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
2578 for (r = mb_row; r < mb_row_end; r++) {
2579 for (c = mb_col; c < mb_col_end; c++) {
2580 const int mb_index = r * cm->mb_cols + c;
2581 if (!(cpi->twopass.this_frame_mb_stats[mb_index] &
2582 FPMB_MOTION_ZERO_MASK) ||
2583 !(cpi->twopass.this_frame_mb_stats[mb_index] &
2584 FPMB_ERROR_SMALL_MASK)) {
2594 if (src_diff_var == UINT_MAX) {
2595 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2596 src_diff_var = get_sby_perpixel_diff_variance(
2597 cpi, &x->plane[0].src, mi_row, mi_col, bsize);
2599 if (src_diff_var < 8) {
2608 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2611 // store estimated motion vector
2612 if (cpi->sf.adaptive_motion_search)
2613 store_pred_mv(x, ctx);
2616 // TODO(jingning): use the motion vectors given by the above search as
2617 // the starting point of motion search in the following partition type check.
2619 subsize = get_subsize(bsize, PARTITION_SPLIT);
2620 if (bsize == BLOCK_8X8) {
2622 if (cpi->sf.adaptive_pred_interp_filter && partition_none_allowed)
2623 pc_tree->leaf_split[0]->pred_interp_filter =
2624 ctx->mic.mbmi.interp_filter;
2625 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
2626 pc_tree->leaf_split[0], best_rdc.rdcost);
2627 if (sum_rdc.rate == INT_MAX)
2628 sum_rdc.rdcost = INT64_MAX;
2630 for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
2631 const int x_idx = (i & 1) * mi_step;
2632 const int y_idx = (i >> 1) * mi_step;
2634 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
2637 if (cpi->sf.adaptive_motion_search)
2638 load_pred_mv(x, ctx);
2640 pc_tree->split[i]->index = i;
2641 rd_pick_partition(cpi, td, tile_data, tp,
2642 mi_row + y_idx, mi_col + x_idx,
2644 best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
2646 if (this_rdc.rate == INT_MAX) {
2647 sum_rdc.rdcost = INT64_MAX;
2650 sum_rdc.rate += this_rdc.rate;
2651 sum_rdc.dist += this_rdc.dist;
2652 sum_rdc.rdcost += this_rdc.rdcost;
2657 if (sum_rdc.rdcost < best_rdc.rdcost && i == 4) {
2658 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2659 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
2660 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
2661 sum_rdc.rate, sum_rdc.dist);
2663 if (sum_rdc.rdcost < best_rdc.rdcost) {
2665 pc_tree->partitioning = PARTITION_SPLIT;
2668 // skip rectangular partition test when larger block size
2669 // gives better rd cost
2670 if (cpi->sf.less_rectangular_check)
2671 do_rect &= !partition_none_allowed;
2673 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2677 if (partition_horz_allowed && do_rect) {
2678 subsize = get_subsize(bsize, PARTITION_HORZ);
2679 if (cpi->sf.adaptive_motion_search)
2680 load_pred_mv(x, ctx);
2681 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2682 partition_none_allowed)
2683 pc_tree->horizontal[0].pred_interp_filter =
2684 ctx->mic.mbmi.interp_filter;
2685 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
2686 &pc_tree->horizontal[0], best_rdc.rdcost);
2688 if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + mi_step < cm->mi_rows &&
2689 bsize > BLOCK_8X8) {
2690 PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
2691 update_state(cpi, td, ctx, mi_row, mi_col, subsize, 0);
2692 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize, ctx);
2694 if (cpi->sf.adaptive_motion_search)
2695 load_pred_mv(x, ctx);
2696 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2697 partition_none_allowed)
2698 pc_tree->horizontal[1].pred_interp_filter =
2699 ctx->mic.mbmi.interp_filter;
2700 rd_pick_sb_modes(cpi, tile_data, x, mi_row + mi_step, mi_col,
2701 &this_rdc, subsize, &pc_tree->horizontal[1],
2702 best_rdc.rdcost - sum_rdc.rdcost);
2703 if (this_rdc.rate == INT_MAX) {
2704 sum_rdc.rdcost = INT64_MAX;
2706 sum_rdc.rate += this_rdc.rate;
2707 sum_rdc.dist += this_rdc.dist;
2708 sum_rdc.rdcost += this_rdc.rdcost;
2712 if (sum_rdc.rdcost < best_rdc.rdcost) {
2713 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2714 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
2715 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
2716 if (sum_rdc.rdcost < best_rdc.rdcost) {
2718 pc_tree->partitioning = PARTITION_HORZ;
2721 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2724 if (partition_vert_allowed && do_rect) {
2725 subsize = get_subsize(bsize, PARTITION_VERT);
2727 if (cpi->sf.adaptive_motion_search)
2728 load_pred_mv(x, ctx);
2729 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2730 partition_none_allowed)
2731 pc_tree->vertical[0].pred_interp_filter =
2732 ctx->mic.mbmi.interp_filter;
2733 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
2734 &pc_tree->vertical[0], best_rdc.rdcost);
2735 if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + mi_step < cm->mi_cols &&
2736 bsize > BLOCK_8X8) {
2737 update_state(cpi, td, &pc_tree->vertical[0], mi_row, mi_col, subsize, 0);
2738 encode_superblock(cpi, td, tp, 0, mi_row, mi_col, subsize,
2739 &pc_tree->vertical[0]);
2741 if (cpi->sf.adaptive_motion_search)
2742 load_pred_mv(x, ctx);
2743 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2744 partition_none_allowed)
2745 pc_tree->vertical[1].pred_interp_filter =
2746 ctx->mic.mbmi.interp_filter;
2747 rd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + mi_step,
2749 &pc_tree->vertical[1], best_rdc.rdcost - sum_rdc.rdcost);
2750 if (this_rdc.rate == INT_MAX) {
2751 sum_rdc.rdcost = INT64_MAX;
2753 sum_rdc.rate += this_rdc.rate;
2754 sum_rdc.dist += this_rdc.dist;
2755 sum_rdc.rdcost += this_rdc.rdcost;
2759 if (sum_rdc.rdcost < best_rdc.rdcost) {
2760 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2761 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
2762 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
2763 sum_rdc.rate, sum_rdc.dist);
2764 if (sum_rdc.rdcost < best_rdc.rdcost) {
2766 pc_tree->partitioning = PARTITION_VERT;
2769 restore_context(x, mi_row, mi_col, a, l, sa, sl, bsize);
2772 // TODO(jbb): This code added so that we avoid static analysis
2773 // warning related to the fact that best_rd isn't used after this
2774 // point. This code should be refactored so that the duplicate
2775 // checks occur in some sub function and thus are used...
2777 *rd_cost = best_rdc;
2780 if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX &&
2781 pc_tree->index != 3) {
2782 int output_enabled = (bsize == BLOCK_64X64);
2783 encode_sb(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
2787 if (bsize == BLOCK_64X64) {
2788 assert(tp_orig < *tp);
2789 assert(best_rdc.rate < INT_MAX);
2790 assert(best_rdc.dist < INT64_MAX);
2792 assert(tp_orig == *tp);
2796 static void encode_rd_sb_row(VP9_COMP *cpi,
2798 TileDataEnc *tile_data,
2801 VP9_COMMON *const cm = &cpi->common;
2802 TileInfo *const tile_info = &tile_data->tile_info;
2803 MACROBLOCK *const x = &td->mb;
2804 MACROBLOCKD *const xd = &x->e_mbd;
2805 SPEED_FEATURES *const sf = &cpi->sf;
2808 // Initialize the left context for the new SB row
2809 memset(&xd->left_context, 0, sizeof(xd->left_context));
2810 memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
2812 // Code each SB in the row
2813 for (mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
2814 mi_col += MI_BLOCK_SIZE) {
2815 const struct segmentation *const seg = &cm->seg;
2822 const int idx_str = cm->mi_stride * mi_row + mi_col;
2823 MODE_INFO **mi = cm->mi_grid_visible + idx_str;
2825 if (sf->adaptive_pred_interp_filter) {
2826 for (i = 0; i < 64; ++i)
2827 td->leaf_tree[i].pred_interp_filter = SWITCHABLE;
2829 for (i = 0; i < 64; ++i) {
2830 td->pc_tree[i].vertical[0].pred_interp_filter = SWITCHABLE;
2831 td->pc_tree[i].vertical[1].pred_interp_filter = SWITCHABLE;
2832 td->pc_tree[i].horizontal[0].pred_interp_filter = SWITCHABLE;
2833 td->pc_tree[i].horizontal[1].pred_interp_filter = SWITCHABLE;
2837 vp9_zero(x->pred_mv);
2838 td->pc_root->index = 0;
2841 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
2842 : cm->last_frame_seg_map;
2843 int segment_id = vp9_get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
2844 seg_skip = vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP);
2847 x->source_variance = UINT_MAX;
2848 if (sf->partition_search_type == FIXED_PARTITION || seg_skip) {
2849 const BLOCK_SIZE bsize =
2850 seg_skip ? BLOCK_64X64 : sf->always_this_block_size;
2851 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
2852 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
2853 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
2854 BLOCK_64X64, &dummy_rate, &dummy_dist, 1, td->pc_root);
2855 } else if (cpi->partition_search_skippable_frame) {
2857 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
2858 bsize = get_rd_var_based_fixed_partition(cpi, x, mi_row, mi_col);
2859 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
2860 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
2861 BLOCK_64X64, &dummy_rate, &dummy_dist, 1, td->pc_root);
2862 } else if (sf->partition_search_type == VAR_BASED_PARTITION &&
2863 cm->frame_type != KEY_FRAME) {
2864 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
2865 rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
2866 BLOCK_64X64, &dummy_rate, &dummy_dist, 1, td->pc_root);
2868 // If required set upper and lower partition size limits
2869 if (sf->auto_min_max_partition_size) {
2870 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
2871 rd_auto_partition_range(cpi, tile_info, xd, mi_row, mi_col,
2872 &x->min_partition_size,
2873 &x->max_partition_size);
2875 rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, BLOCK_64X64,
2876 &dummy_rdc, INT64_MAX, td->pc_root);
2881 static void init_encode_frame_mb_context(VP9_COMP *cpi) {
2882 MACROBLOCK *const x = &cpi->td.mb;
2883 VP9_COMMON *const cm = &cpi->common;
2884 MACROBLOCKD *const xd = &x->e_mbd;
2885 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
2887 // Copy data over into macro block data structures.
2888 vp9_setup_src_planes(x, cpi->Source, 0, 0);
2890 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
2892 // Note: this memset assumes above_context[0], [1] and [2]
2893 // are allocated as part of the same buffer.
2894 memset(xd->above_context[0], 0,
2895 sizeof(*xd->above_context[0]) *
2896 2 * aligned_mi_cols * MAX_MB_PLANE);
2897 memset(xd->above_seg_context, 0,
2898 sizeof(*xd->above_seg_context) * aligned_mi_cols);
2901 static int check_dual_ref_flags(VP9_COMP *cpi) {
2902 const int ref_flags = cpi->ref_frame_flags;
2904 if (vp9_segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
2907 return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG)
2908 + !!(ref_flags & VP9_ALT_FLAG)) >= 2;
2912 static void reset_skip_tx_size(VP9_COMMON *cm, TX_SIZE max_tx_size) {
2914 const int mis = cm->mi_stride;
2915 MODE_INFO **mi_ptr = cm->mi_grid_visible;
2917 for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row, mi_ptr += mis) {
2918 for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
2919 if (mi_ptr[mi_col]->mbmi.tx_size > max_tx_size)
2920 mi_ptr[mi_col]->mbmi.tx_size = max_tx_size;
2925 static MV_REFERENCE_FRAME get_frame_type(const VP9_COMP *cpi) {
2926 if (frame_is_intra_only(&cpi->common))
2928 else if (cpi->rc.is_src_frame_alt_ref && cpi->refresh_golden_frame)
2929 return ALTREF_FRAME;
2930 else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
2931 return GOLDEN_FRAME;
2936 static TX_MODE select_tx_mode(const VP9_COMP *cpi, MACROBLOCKD *const xd) {
2939 if (cpi->common.frame_type == KEY_FRAME &&
2940 cpi->sf.use_nonrd_pick_mode &&
2941 cpi->sf.partition_search_type == VAR_BASED_PARTITION)
2943 if (cpi->sf.tx_size_search_method == USE_LARGESTALL)
2945 else if (cpi->sf.tx_size_search_method == USE_FULL_RD||
2946 cpi->sf.tx_size_search_method == USE_TX_8X8)
2947 return TX_MODE_SELECT;
2949 return cpi->common.tx_mode;
2952 static void hybrid_intra_mode_search(VP9_COMP *cpi, MACROBLOCK *const x,
2953 RD_COST *rd_cost, BLOCK_SIZE bsize,
2954 PICK_MODE_CONTEXT *ctx) {
2955 if (bsize < BLOCK_16X16)
2956 vp9_rd_pick_intra_mode_sb(cpi, x, rd_cost, bsize, ctx, INT64_MAX);
2958 vp9_pick_intra_mode(cpi, x, rd_cost, bsize, ctx);
2961 static void nonrd_pick_sb_modes(VP9_COMP *cpi,
2962 TileDataEnc *tile_data, MACROBLOCK *const x,
2963 int mi_row, int mi_col, RD_COST *rd_cost,
2964 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
2965 VP9_COMMON *const cm = &cpi->common;
2966 TileInfo *const tile_info = &tile_data->tile_info;
2967 MACROBLOCKD *const xd = &x->e_mbd;
2969 set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
2970 mbmi = &xd->mi[0]->mbmi;
2971 mbmi->sb_type = bsize;
2973 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled)
2974 if (cyclic_refresh_segment_id_boosted(mbmi->segment_id))
2975 x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
2977 if (cm->frame_type == KEY_FRAME)
2978 hybrid_intra_mode_search(cpi, x, rd_cost, bsize, ctx);
2979 else if (vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
2980 set_mode_info_seg_skip(x, cm->tx_mode, rd_cost, bsize);
2981 else if (bsize >= BLOCK_8X8)
2982 vp9_pick_inter_mode(cpi, x, tile_data, mi_row, mi_col,
2983 rd_cost, bsize, ctx);
2985 vp9_pick_inter_mode_sub8x8(cpi, x, tile_data, mi_row, mi_col,
2986 rd_cost, bsize, ctx);
2988 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
2990 if (rd_cost->rate == INT_MAX)
2991 vp9_rd_cost_reset(rd_cost);
2993 ctx->rate = rd_cost->rate;
2994 ctx->dist = rd_cost->dist;
2997 static void fill_mode_info_sb(VP9_COMMON *cm, MACROBLOCK *x,
2998 int mi_row, int mi_col,
3001 MACROBLOCKD *xd = &x->e_mbd;
3002 int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3003 PARTITION_TYPE partition = pc_tree->partitioning;
3004 BLOCK_SIZE subsize = get_subsize(bsize, partition);
3006 assert(bsize >= BLOCK_8X8);
3008 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
3011 switch (partition) {
3012 case PARTITION_NONE:
3013 set_mode_info_offsets(cm, xd, mi_row, mi_col);
3014 *(xd->mi[0]) = pc_tree->none.mic;
3015 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
3017 case PARTITION_VERT:
3018 set_mode_info_offsets(cm, xd, mi_row, mi_col);
3019 *(xd->mi[0]) = pc_tree->vertical[0].mic;
3020 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
3022 if (mi_col + hbs < cm->mi_cols) {
3023 set_mode_info_offsets(cm, xd, mi_row, mi_col + hbs);
3024 *(xd->mi[0]) = pc_tree->vertical[1].mic;
3025 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col + hbs, subsize);
3028 case PARTITION_HORZ:
3029 set_mode_info_offsets(cm, xd, mi_row, mi_col);
3030 *(xd->mi[0]) = pc_tree->horizontal[0].mic;
3031 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, subsize);
3032 if (mi_row + hbs < cm->mi_rows) {
3033 set_mode_info_offsets(cm, xd, mi_row + hbs, mi_col);
3034 *(xd->mi[0]) = pc_tree->horizontal[1].mic;
3035 duplicate_mode_info_in_sb(cm, xd, mi_row + hbs, mi_col, subsize);
3038 case PARTITION_SPLIT: {
3039 fill_mode_info_sb(cm, x, mi_row, mi_col, subsize, pc_tree->split[0]);
3040 fill_mode_info_sb(cm, x, mi_row, mi_col + hbs, subsize,
3042 fill_mode_info_sb(cm, x, mi_row + hbs, mi_col, subsize,
3044 fill_mode_info_sb(cm, x, mi_row + hbs, mi_col + hbs, subsize,
3053 // Reset the prediction pixel ready flag recursively.
3054 static void pred_pixel_ready_reset(PC_TREE *pc_tree, BLOCK_SIZE bsize) {
3055 pc_tree->none.pred_pixel_ready = 0;
3056 pc_tree->horizontal[0].pred_pixel_ready = 0;
3057 pc_tree->horizontal[1].pred_pixel_ready = 0;
3058 pc_tree->vertical[0].pred_pixel_ready = 0;
3059 pc_tree->vertical[1].pred_pixel_ready = 0;
3061 if (bsize > BLOCK_8X8) {
3062 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_SPLIT);
3064 for (i = 0; i < 4; ++i)
3065 pred_pixel_ready_reset(pc_tree->split[i], subsize);
3069 static void nonrd_pick_partition(VP9_COMP *cpi, ThreadData *td,
3070 TileDataEnc *tile_data,
3071 TOKENEXTRA **tp, int mi_row,
3072 int mi_col, BLOCK_SIZE bsize, RD_COST *rd_cost,
3073 int do_recon, int64_t best_rd,
3075 const SPEED_FEATURES *const sf = &cpi->sf;
3076 VP9_COMMON *const cm = &cpi->common;
3077 TileInfo *const tile_info = &tile_data->tile_info;
3078 MACROBLOCK *const x = &td->mb;
3079 MACROBLOCKD *const xd = &x->e_mbd;
3080 const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
3081 TOKENEXTRA *tp_orig = *tp;
3082 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
3084 BLOCK_SIZE subsize = bsize;
3085 RD_COST this_rdc, sum_rdc, best_rdc;
3086 int do_split = bsize >= BLOCK_8X8;
3088 // Override skipping rectangular partition operations for edge blocks
3089 const int force_horz_split = (mi_row + ms >= cm->mi_rows);
3090 const int force_vert_split = (mi_col + ms >= cm->mi_cols);
3091 const int xss = x->e_mbd.plane[1].subsampling_x;
3092 const int yss = x->e_mbd.plane[1].subsampling_y;
3094 int partition_none_allowed = !force_horz_split && !force_vert_split;
3095 int partition_horz_allowed = !force_vert_split && yss <= xss &&
3097 int partition_vert_allowed = !force_horz_split && xss <= yss &&
3101 assert(num_8x8_blocks_wide_lookup[bsize] ==
3102 num_8x8_blocks_high_lookup[bsize]);
3104 vp9_rd_cost_init(&sum_rdc);
3105 vp9_rd_cost_reset(&best_rdc);
3106 best_rdc.rdcost = best_rd;
3108 // Determine partition types in search according to the speed features.
3109 // The threshold set here has to be of square block size.
3110 if (sf->auto_min_max_partition_size) {
3111 partition_none_allowed &= (bsize <= x->max_partition_size &&
3112 bsize >= x->min_partition_size);
3113 partition_horz_allowed &= ((bsize <= x->max_partition_size &&
3114 bsize > x->min_partition_size) ||
3116 partition_vert_allowed &= ((bsize <= x->max_partition_size &&
3117 bsize > x->min_partition_size) ||
3119 do_split &= bsize > x->min_partition_size;
3121 if (sf->use_square_partition_only) {
3122 partition_horz_allowed &= force_horz_split;
3123 partition_vert_allowed &= force_vert_split;
3126 ctx->pred_pixel_ready = !(partition_vert_allowed ||
3127 partition_horz_allowed ||
3131 if (partition_none_allowed) {
3132 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col,
3133 &this_rdc, bsize, ctx);
3134 ctx->mic.mbmi = xd->mi[0]->mbmi;
3135 ctx->skip_txfm[0] = x->skip_txfm[0];
3136 ctx->skip = x->skip;
3138 if (this_rdc.rate != INT_MAX) {
3139 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3140 this_rdc.rate += cpi->partition_cost[pl][PARTITION_NONE];
3141 this_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
3142 this_rdc.rate, this_rdc.dist);
3143 if (this_rdc.rdcost < best_rdc.rdcost) {
3144 int64_t dist_breakout_thr = sf->partition_search_breakout_dist_thr;
3145 int64_t rate_breakout_thr = sf->partition_search_breakout_rate_thr;
3147 dist_breakout_thr >>= 8 - (b_width_log2_lookup[bsize] +
3148 b_height_log2_lookup[bsize]);
3150 rate_breakout_thr *= num_pels_log2_lookup[bsize];
3152 best_rdc = this_rdc;
3153 if (bsize >= BLOCK_8X8)
3154 pc_tree->partitioning = PARTITION_NONE;
3156 if (!x->e_mbd.lossless &&
3157 this_rdc.rate < rate_breakout_thr &&
3158 this_rdc.dist < dist_breakout_thr) {
3166 // store estimated motion vector
3167 store_pred_mv(x, ctx);
3171 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3172 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_SPLIT];
3173 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv, sum_rdc.rate, sum_rdc.dist);
3174 subsize = get_subsize(bsize, PARTITION_SPLIT);
3175 for (i = 0; i < 4 && sum_rdc.rdcost < best_rdc.rdcost; ++i) {
3176 const int x_idx = (i & 1) * ms;
3177 const int y_idx = (i >> 1) * ms;
3179 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
3181 load_pred_mv(x, ctx);
3182 nonrd_pick_partition(cpi, td, tile_data, tp,
3183 mi_row + y_idx, mi_col + x_idx,
3184 subsize, &this_rdc, 0,
3185 best_rdc.rdcost - sum_rdc.rdcost, pc_tree->split[i]);
3187 if (this_rdc.rate == INT_MAX) {
3188 vp9_rd_cost_reset(&sum_rdc);
3190 sum_rdc.rate += this_rdc.rate;
3191 sum_rdc.dist += this_rdc.dist;
3192 sum_rdc.rdcost += this_rdc.rdcost;
3196 if (sum_rdc.rdcost < best_rdc.rdcost) {
3198 pc_tree->partitioning = PARTITION_SPLIT;
3200 // skip rectangular partition test when larger block size
3201 // gives better rd cost
3202 if (sf->less_rectangular_check)
3203 do_rect &= !partition_none_allowed;
3208 if (partition_horz_allowed && do_rect) {
3209 subsize = get_subsize(bsize, PARTITION_HORZ);
3210 if (sf->adaptive_motion_search)
3211 load_pred_mv(x, ctx);
3212 pc_tree->horizontal[0].pred_pixel_ready = 1;
3213 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3214 &pc_tree->horizontal[0]);
3216 pc_tree->horizontal[0].mic.mbmi = xd->mi[0]->mbmi;
3217 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3218 pc_tree->horizontal[0].skip = x->skip;
3220 if (sum_rdc.rdcost < best_rdc.rdcost && mi_row + ms < cm->mi_rows) {
3221 load_pred_mv(x, ctx);
3222 pc_tree->horizontal[1].pred_pixel_ready = 1;
3223 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + ms, mi_col,
3225 &pc_tree->horizontal[1]);
3227 pc_tree->horizontal[1].mic.mbmi = xd->mi[0]->mbmi;
3228 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3229 pc_tree->horizontal[1].skip = x->skip;
3231 if (this_rdc.rate == INT_MAX) {
3232 vp9_rd_cost_reset(&sum_rdc);
3234 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3235 this_rdc.rate += cpi->partition_cost[pl][PARTITION_HORZ];
3236 sum_rdc.rate += this_rdc.rate;
3237 sum_rdc.dist += this_rdc.dist;
3238 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
3239 sum_rdc.rate, sum_rdc.dist);
3243 if (sum_rdc.rdcost < best_rdc.rdcost) {
3245 pc_tree->partitioning = PARTITION_HORZ;
3247 pred_pixel_ready_reset(pc_tree, bsize);
3252 if (partition_vert_allowed && do_rect) {
3253 subsize = get_subsize(bsize, PARTITION_VERT);
3254 if (sf->adaptive_motion_search)
3255 load_pred_mv(x, ctx);
3256 pc_tree->vertical[0].pred_pixel_ready = 1;
3257 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, &sum_rdc, subsize,
3258 &pc_tree->vertical[0]);
3259 pc_tree->vertical[0].mic.mbmi = xd->mi[0]->mbmi;
3260 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
3261 pc_tree->vertical[0].skip = x->skip;
3263 if (sum_rdc.rdcost < best_rdc.rdcost && mi_col + ms < cm->mi_cols) {
3264 load_pred_mv(x, ctx);
3265 pc_tree->vertical[1].pred_pixel_ready = 1;
3266 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + ms,
3268 &pc_tree->vertical[1]);
3269 pc_tree->vertical[1].mic.mbmi = xd->mi[0]->mbmi;
3270 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
3271 pc_tree->vertical[1].skip = x->skip;
3273 if (this_rdc.rate == INT_MAX) {
3274 vp9_rd_cost_reset(&sum_rdc);
3276 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
3277 sum_rdc.rate += cpi->partition_cost[pl][PARTITION_VERT];
3278 sum_rdc.rate += this_rdc.rate;
3279 sum_rdc.dist += this_rdc.dist;
3280 sum_rdc.rdcost = RDCOST(x->rdmult, x->rddiv,
3281 sum_rdc.rate, sum_rdc.dist);
3285 if (sum_rdc.rdcost < best_rdc.rdcost) {
3287 pc_tree->partitioning = PARTITION_VERT;
3289 pred_pixel_ready_reset(pc_tree, bsize);
3293 *rd_cost = best_rdc;
3295 if (best_rdc.rate == INT_MAX) {
3296 vp9_rd_cost_reset(rd_cost);
3300 // update mode info array
3301 fill_mode_info_sb(cm, x, mi_row, mi_col, bsize, pc_tree);
3303 if (best_rdc.rate < INT_MAX && best_rdc.dist < INT64_MAX && do_recon) {
3304 int output_enabled = (bsize == BLOCK_64X64);
3305 encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
3309 if (bsize == BLOCK_64X64 && do_recon) {
3310 assert(tp_orig < *tp);
3311 assert(best_rdc.rate < INT_MAX);
3312 assert(best_rdc.dist < INT64_MAX);
3314 assert(tp_orig == *tp);
3318 static void nonrd_select_partition(VP9_COMP *cpi,
3320 TileDataEnc *tile_data,
3323 int mi_row, int mi_col,
3324 BLOCK_SIZE bsize, int output_enabled,
3325 RD_COST *rd_cost, PC_TREE *pc_tree) {
3326 VP9_COMMON *const cm = &cpi->common;
3327 TileInfo *const tile_info = &tile_data->tile_info;
3328 MACROBLOCK *const x = &td->mb;
3329 MACROBLOCKD *const xd = &x->e_mbd;
3330 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3331 const int mis = cm->mi_stride;
3332 PARTITION_TYPE partition;
3336 vp9_rd_cost_reset(&this_rdc);
3337 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
3340 subsize = (bsize >= BLOCK_8X8) ? mi[0]->mbmi.sb_type : BLOCK_4X4;
3341 partition = partition_lookup[bsl][subsize];
3343 if (bsize == BLOCK_32X32 && partition != PARTITION_NONE &&
3344 subsize >= BLOCK_16X16) {
3345 x->max_partition_size = BLOCK_32X32;
3346 x->min_partition_size = BLOCK_8X8;
3347 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize,
3348 rd_cost, 0, INT64_MAX, pc_tree);
3349 } else if (bsize == BLOCK_16X16 && partition != PARTITION_NONE) {
3350 x->max_partition_size = BLOCK_16X16;
3351 x->min_partition_size = BLOCK_8X8;
3352 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize,
3353 rd_cost, 0, INT64_MAX, pc_tree);
3355 switch (partition) {
3356 case PARTITION_NONE:
3357 pc_tree->none.pred_pixel_ready = 1;
3358 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost,
3359 subsize, &pc_tree->none);
3360 pc_tree->none.mic.mbmi = xd->mi[0]->mbmi;
3361 pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
3362 pc_tree->none.skip = x->skip;
3364 case PARTITION_VERT:
3365 pc_tree->vertical[0].pred_pixel_ready = 1;
3366 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost,
3367 subsize, &pc_tree->vertical[0]);
3368 pc_tree->vertical[0].mic.mbmi = xd->mi[0]->mbmi;
3369 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
3370 pc_tree->vertical[0].skip = x->skip;
3371 if (mi_col + hbs < cm->mi_cols) {
3372 pc_tree->vertical[1].pred_pixel_ready = 1;
3373 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs,
3374 &this_rdc, subsize, &pc_tree->vertical[1]);
3375 pc_tree->vertical[1].mic.mbmi = xd->mi[0]->mbmi;
3376 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
3377 pc_tree->vertical[1].skip = x->skip;
3378 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3379 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3380 rd_cost->rate += this_rdc.rate;
3381 rd_cost->dist += this_rdc.dist;
3385 case PARTITION_HORZ:
3386 pc_tree->horizontal[0].pred_pixel_ready = 1;
3387 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, rd_cost,
3388 subsize, &pc_tree->horizontal[0]);
3389 pc_tree->horizontal[0].mic.mbmi = xd->mi[0]->mbmi;
3390 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3391 pc_tree->horizontal[0].skip = x->skip;
3392 if (mi_row + hbs < cm->mi_rows) {
3393 pc_tree->horizontal[1].pred_pixel_ready = 1;
3394 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col,
3395 &this_rdc, subsize, &pc_tree->horizontal[1]);
3396 pc_tree->horizontal[1].mic.mbmi = xd->mi[0]->mbmi;
3397 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3398 pc_tree->horizontal[1].skip = x->skip;
3399 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3400 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3401 rd_cost->rate += this_rdc.rate;
3402 rd_cost->dist += this_rdc.dist;
3406 case PARTITION_SPLIT:
3407 subsize = get_subsize(bsize, PARTITION_SPLIT);
3408 nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3409 subsize, output_enabled, rd_cost,
3411 nonrd_select_partition(cpi, td, tile_data, mi + hbs, tp,
3412 mi_row, mi_col + hbs, subsize, output_enabled,
3413 &this_rdc, pc_tree->split[1]);
3414 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3415 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3416 rd_cost->rate += this_rdc.rate;
3417 rd_cost->dist += this_rdc.dist;
3419 nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis, tp,
3420 mi_row + hbs, mi_col, subsize, output_enabled,
3421 &this_rdc, pc_tree->split[2]);
3422 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3423 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3424 rd_cost->rate += this_rdc.rate;
3425 rd_cost->dist += this_rdc.dist;
3427 nonrd_select_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
3428 mi_row + hbs, mi_col + hbs, subsize,
3429 output_enabled, &this_rdc, pc_tree->split[3]);
3430 if (this_rdc.rate != INT_MAX && this_rdc.dist != INT64_MAX &&
3431 rd_cost->rate != INT_MAX && rd_cost->dist != INT64_MAX) {
3432 rd_cost->rate += this_rdc.rate;
3433 rd_cost->dist += this_rdc.dist;
3437 assert(0 && "Invalid partition type.");
3442 if (bsize == BLOCK_64X64 && output_enabled)
3443 encode_sb_rt(cpi, td, tile_info, tp, mi_row, mi_col, 1, bsize, pc_tree);
3447 static void nonrd_use_partition(VP9_COMP *cpi,
3449 TileDataEnc *tile_data,
3452 int mi_row, int mi_col,
3453 BLOCK_SIZE bsize, int output_enabled,
3454 RD_COST *dummy_cost, PC_TREE *pc_tree) {
3455 VP9_COMMON *const cm = &cpi->common;
3456 TileInfo *tile_info = &tile_data->tile_info;
3457 MACROBLOCK *const x = &td->mb;
3458 MACROBLOCKD *const xd = &x->e_mbd;
3459 const int bsl = b_width_log2_lookup[bsize], hbs = (1 << bsl) / 4;
3460 const int mis = cm->mi_stride;
3461 PARTITION_TYPE partition;
3464 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
3467 subsize = (bsize >= BLOCK_8X8) ? mi[0]->mbmi.sb_type : BLOCK_4X4;
3468 partition = partition_lookup[bsl][subsize];
3470 if (output_enabled && bsize != BLOCK_4X4) {
3471 int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
3472 td->counts->partition[ctx][partition]++;
3475 switch (partition) {
3476 case PARTITION_NONE:
3477 pc_tree->none.pred_pixel_ready = 1;
3478 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
3479 subsize, &pc_tree->none);
3480 pc_tree->none.mic.mbmi = xd->mi[0]->mbmi;
3481 pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
3482 pc_tree->none.skip = x->skip;
3483 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
3484 subsize, &pc_tree->none);
3486 case PARTITION_VERT:
3487 pc_tree->vertical[0].pred_pixel_ready = 1;
3488 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
3489 subsize, &pc_tree->vertical[0]);
3490 pc_tree->vertical[0].mic.mbmi = xd->mi[0]->mbmi;
3491 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
3492 pc_tree->vertical[0].skip = x->skip;
3493 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
3494 subsize, &pc_tree->vertical[0]);
3495 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
3496 pc_tree->vertical[1].pred_pixel_ready = 1;
3497 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col + hbs,
3498 dummy_cost, subsize, &pc_tree->vertical[1]);
3499 pc_tree->vertical[1].mic.mbmi = xd->mi[0]->mbmi;
3500 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
3501 pc_tree->vertical[1].skip = x->skip;
3502 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col + hbs,
3503 output_enabled, subsize, &pc_tree->vertical[1]);
3506 case PARTITION_HORZ:
3507 pc_tree->horizontal[0].pred_pixel_ready = 1;
3508 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
3509 subsize, &pc_tree->horizontal[0]);
3510 pc_tree->horizontal[0].mic.mbmi = xd->mi[0]->mbmi;
3511 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
3512 pc_tree->horizontal[0].skip = x->skip;
3513 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col, output_enabled,
3514 subsize, &pc_tree->horizontal[0]);
3516 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
3517 pc_tree->horizontal[1].pred_pixel_ready = 1;
3518 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row + hbs, mi_col,
3519 dummy_cost, subsize, &pc_tree->horizontal[1]);
3520 pc_tree->horizontal[1].mic.mbmi = xd->mi[0]->mbmi;
3521 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
3522 pc_tree->horizontal[1].skip = x->skip;
3523 encode_b_rt(cpi, td, tile_info, tp, mi_row + hbs, mi_col,
3524 output_enabled, subsize, &pc_tree->horizontal[1]);
3527 case PARTITION_SPLIT:
3528 subsize = get_subsize(bsize, PARTITION_SPLIT);
3529 if (bsize == BLOCK_8X8) {
3530 nonrd_pick_sb_modes(cpi, tile_data, x, mi_row, mi_col, dummy_cost,
3531 subsize, pc_tree->leaf_split[0]);
3532 encode_b_rt(cpi, td, tile_info, tp, mi_row, mi_col,
3533 output_enabled, subsize, pc_tree->leaf_split[0]);
3535 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3536 subsize, output_enabled, dummy_cost,
3538 nonrd_use_partition(cpi, td, tile_data, mi + hbs, tp,
3539 mi_row, mi_col + hbs, subsize, output_enabled,
3540 dummy_cost, pc_tree->split[1]);
3541 nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis, tp,
3542 mi_row + hbs, mi_col, subsize, output_enabled,
3543 dummy_cost, pc_tree->split[2]);
3544 nonrd_use_partition(cpi, td, tile_data, mi + hbs * mis + hbs, tp,
3545 mi_row + hbs, mi_col + hbs, subsize, output_enabled,
3546 dummy_cost, pc_tree->split[3]);
3550 assert(0 && "Invalid partition type.");
3554 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
3555 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
3558 static void encode_nonrd_sb_row(VP9_COMP *cpi,
3560 TileDataEnc *tile_data,
3563 SPEED_FEATURES *const sf = &cpi->sf;
3564 VP9_COMMON *const cm = &cpi->common;
3565 TileInfo *const tile_info = &tile_data->tile_info;
3566 MACROBLOCK *const x = &td->mb;
3567 MACROBLOCKD *const xd = &x->e_mbd;
3570 // Initialize the left context for the new SB row
3571 memset(&xd->left_context, 0, sizeof(xd->left_context));
3572 memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
3574 // Code each SB in the row
3575 for (mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
3576 mi_col += MI_BLOCK_SIZE) {
3577 const struct segmentation *const seg = &cm->seg;
3579 const int idx_str = cm->mi_stride * mi_row + mi_col;
3580 MODE_INFO **mi = cm->mi_grid_visible + idx_str;
3581 PARTITION_SEARCH_TYPE partition_search_type = sf->partition_search_type;
3582 BLOCK_SIZE bsize = BLOCK_64X64;
3584 x->source_variance = UINT_MAX;
3585 vp9_zero(x->pred_mv);
3586 vp9_rd_cost_init(&dummy_rdc);
3587 x->color_sensitivity[0] = 0;
3588 x->color_sensitivity[1] = 0;
3591 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
3592 : cm->last_frame_seg_map;
3593 int segment_id = vp9_get_segment_id(cm, map, BLOCK_64X64, mi_row, mi_col);
3594 seg_skip = vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP);
3596 partition_search_type = FIXED_PARTITION;
3600 // Set the partition type of the 64X64 block
3601 switch (partition_search_type) {
3602 case VAR_BASED_PARTITION:
3603 // TODO(jingning, marpan): The mode decision and encoding process
3604 // support both intra and inter sub8x8 block coding for RTC mode.
3605 // Tune the thresholds accordingly to use sub8x8 block coding for
3606 // coding performance improvement.
3607 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
3608 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3609 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
3611 case SOURCE_VAR_BASED_PARTITION:
3612 set_source_var_based_partition(cpi, tile_info, x, mi, mi_row, mi_col);
3613 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3614 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
3616 case FIXED_PARTITION:
3618 bsize = sf->always_this_block_size;
3619 set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
3620 nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3621 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
3623 case REFERENCE_PARTITION:
3624 set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
3625 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled &&
3626 xd->mi[0]->mbmi.segment_id) {
3627 x->max_partition_size = BLOCK_64X64;
3628 x->min_partition_size = BLOCK_8X8;
3629 nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col,
3630 BLOCK_64X64, &dummy_rdc, 1,
3631 INT64_MAX, td->pc_root);
3633 choose_partitioning(cpi, tile_info, x, mi_row, mi_col);
3634 nonrd_select_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col,
3635 BLOCK_64X64, 1, &dummy_rdc, td->pc_root);
3645 // end RTC play code
3647 static int set_var_thresh_from_histogram(VP9_COMP *cpi) {
3648 const SPEED_FEATURES *const sf = &cpi->sf;
3649 const VP9_COMMON *const cm = &cpi->common;
3651 const uint8_t *src = cpi->Source->y_buffer;
3652 const uint8_t *last_src = cpi->Last_Source->y_buffer;
3653 const int src_stride = cpi->Source->y_stride;
3654 const int last_stride = cpi->Last_Source->y_stride;
3656 // Pick cutoff threshold
3657 const int cutoff = (MIN(cm->width, cm->height) >= 720) ?
3658 (cm->MBs * VAR_HIST_LARGE_CUT_OFF / 100) :
3659 (cm->MBs * VAR_HIST_SMALL_CUT_OFF / 100);
3660 DECLARE_ALIGNED_ARRAY(16, int, hist, VAR_HIST_BINS);
3661 diff *var16 = cpi->source_diff_var;
3666 memset(hist, 0, VAR_HIST_BINS * sizeof(hist[0]));
3668 for (i = 0; i < cm->mb_rows; i++) {
3669 for (j = 0; j < cm->mb_cols; j++) {
3670 #if CONFIG_VP9_HIGHBITDEPTH
3671 if (cm->use_highbitdepth) {
3672 switch (cm->bit_depth) {
3674 vp9_highbd_get16x16var(src, src_stride, last_src, last_stride,
3675 &var16->sse, &var16->sum);
3678 vp9_highbd_10_get16x16var(src, src_stride, last_src, last_stride,
3679 &var16->sse, &var16->sum);
3682 vp9_highbd_12_get16x16var(src, src_stride, last_src, last_stride,
3683 &var16->sse, &var16->sum);
3686 assert(0 && "cm->bit_depth should be VPX_BITS_8, VPX_BITS_10"
3691 vp9_get16x16var(src, src_stride, last_src, last_stride,
3692 &var16->sse, &var16->sum);
3695 vp9_get16x16var(src, src_stride, last_src, last_stride,
3696 &var16->sse, &var16->sum);
3697 #endif // CONFIG_VP9_HIGHBITDEPTH
3698 var16->var = var16->sse -
3699 (((uint32_t)var16->sum * var16->sum) >> 8);
3701 if (var16->var >= VAR_HIST_MAX_BG_VAR)
3702 hist[VAR_HIST_BINS - 1]++;
3704 hist[var16->var / VAR_HIST_FACTOR]++;
3711 src = src - cm->mb_cols * 16 + 16 * src_stride;
3712 last_src = last_src - cm->mb_cols * 16 + 16 * last_stride;
3715 cpi->source_var_thresh = 0;
3717 if (hist[VAR_HIST_BINS - 1] < cutoff) {
3718 for (i = 0; i < VAR_HIST_BINS - 1; i++) {
3722 cpi->source_var_thresh = (i + 1) * VAR_HIST_FACTOR;
3728 return sf->search_type_check_frequency;
3731 static void source_var_based_partition_search_method(VP9_COMP *cpi) {
3732 VP9_COMMON *const cm = &cpi->common;
3733 SPEED_FEATURES *const sf = &cpi->sf;
3735 if (cm->frame_type == KEY_FRAME) {
3736 // For key frame, use SEARCH_PARTITION.
3737 sf->partition_search_type = SEARCH_PARTITION;
3738 } else if (cm->intra_only) {
3739 sf->partition_search_type = FIXED_PARTITION;
3741 if (cm->last_width != cm->width || cm->last_height != cm->height) {
3742 if (cpi->source_diff_var)
3743 vpx_free(cpi->source_diff_var);
3745 CHECK_MEM_ERROR(cm, cpi->source_diff_var,
3746 vpx_calloc(cm->MBs, sizeof(diff)));
3749 if (!cpi->frames_till_next_var_check)
3750 cpi->frames_till_next_var_check = set_var_thresh_from_histogram(cpi);
3752 if (cpi->frames_till_next_var_check > 0) {
3753 sf->partition_search_type = FIXED_PARTITION;
3754 cpi->frames_till_next_var_check--;
3759 static int get_skip_encode_frame(const VP9_COMMON *cm, ThreadData *const td) {
3760 unsigned int intra_count = 0, inter_count = 0;
3763 for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
3764 intra_count += td->counts->intra_inter[j][0];
3765 inter_count += td->counts->intra_inter[j][1];
3768 return (intra_count << 2) < inter_count &&
3769 cm->frame_type != KEY_FRAME &&
3773 void vp9_init_tile_data(VP9_COMP *cpi) {
3774 VP9_COMMON *const cm = &cpi->common;
3775 const int tile_cols = 1 << cm->log2_tile_cols;
3776 const int tile_rows = 1 << cm->log2_tile_rows;
3777 int tile_col, tile_row;
3778 TOKENEXTRA *pre_tok = cpi->tile_tok[0][0];
3781 if (cpi->tile_data == NULL) {
3782 CHECK_MEM_ERROR(cm, cpi->tile_data,
3783 vpx_malloc(tile_cols * tile_rows * sizeof(*cpi->tile_data)));
3784 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
3785 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
3786 TileDataEnc *tile_data =
3787 &cpi->tile_data[tile_row * tile_cols + tile_col];
3789 for (i = 0; i < BLOCK_SIZES; ++i) {
3790 for (j = 0; j < MAX_MODES; ++j) {
3791 tile_data->thresh_freq_fact[i][j] = 32;
3792 tile_data->mode_map[i][j] = j;
3798 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
3799 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
3800 TileInfo *tile_info =
3801 &cpi->tile_data[tile_row * tile_cols + tile_col].tile_info;
3802 vp9_tile_init(tile_info, cm, tile_row, tile_col);
3804 cpi->tile_tok[tile_row][tile_col] = pre_tok + tile_tok;
3805 pre_tok = cpi->tile_tok[tile_row][tile_col];
3806 tile_tok = allocated_tokens(*tile_info);
3811 void vp9_encode_tile(VP9_COMP *cpi, ThreadData *td,
3812 int tile_row, int tile_col) {
3813 VP9_COMMON *const cm = &cpi->common;
3814 const int tile_cols = 1 << cm->log2_tile_cols;
3815 TileDataEnc *this_tile =
3816 &cpi->tile_data[tile_row * tile_cols + tile_col];
3817 const TileInfo * const tile_info = &this_tile->tile_info;
3818 TOKENEXTRA *tok = cpi->tile_tok[tile_row][tile_col];
3821 for (mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
3822 mi_row += MI_BLOCK_SIZE) {
3823 if (cpi->sf.use_nonrd_pick_mode)
3824 encode_nonrd_sb_row(cpi, td, this_tile, mi_row, &tok);
3826 encode_rd_sb_row(cpi, td, this_tile, mi_row, &tok);
3828 cpi->tok_count[tile_row][tile_col] =
3829 (unsigned int)(tok - cpi->tile_tok[tile_row][tile_col]);
3830 assert(tok - cpi->tile_tok[tile_row][tile_col] <=
3831 allocated_tokens(*tile_info));
3834 static void encode_tiles(VP9_COMP *cpi) {
3835 VP9_COMMON *const cm = &cpi->common;
3836 const int tile_cols = 1 << cm->log2_tile_cols;
3837 const int tile_rows = 1 << cm->log2_tile_rows;
3838 int tile_col, tile_row;
3840 vp9_init_tile_data(cpi);
3842 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
3843 for (tile_col = 0; tile_col < tile_cols; ++tile_col)
3844 vp9_encode_tile(cpi, &cpi->td, tile_row, tile_col);
3847 #if CONFIG_FP_MB_STATS
3848 static int input_fpmb_stats(FIRSTPASS_MB_STATS *firstpass_mb_stats,
3849 VP9_COMMON *cm, uint8_t **this_frame_mb_stats) {
3850 uint8_t *mb_stats_in = firstpass_mb_stats->mb_stats_start +
3851 cm->current_video_frame * cm->MBs * sizeof(uint8_t);
3853 if (mb_stats_in > firstpass_mb_stats->mb_stats_end)
3856 *this_frame_mb_stats = mb_stats_in;
3862 static void encode_frame_internal(VP9_COMP *cpi) {
3863 SPEED_FEATURES *const sf = &cpi->sf;
3864 RD_OPT *const rd_opt = &cpi->rd;
3865 ThreadData *const td = &cpi->td;
3866 MACROBLOCK *const x = &td->mb;
3867 VP9_COMMON *const cm = &cpi->common;
3868 MACROBLOCKD *const xd = &x->e_mbd;
3869 RD_COUNTS *const rdc = &cpi->td.rd_counts;
3871 xd->mi = cm->mi_grid_visible;
3874 vp9_zero(*td->counts);
3875 vp9_zero(rdc->coef_counts);
3876 vp9_zero(rdc->comp_pred_diff);
3877 vp9_zero(rdc->filter_diff);
3878 vp9_zero(rdc->tx_select_diff);
3879 vp9_zero(rd_opt->tx_select_threshes);
3881 xd->lossless = cm->base_qindex == 0 &&
3882 cm->y_dc_delta_q == 0 &&
3883 cm->uv_dc_delta_q == 0 &&
3884 cm->uv_ac_delta_q == 0;
3886 #if CONFIG_VP9_HIGHBITDEPTH
3887 if (cm->use_highbitdepth)
3888 x->fwd_txm4x4 = xd->lossless ? vp9_highbd_fwht4x4 : vp9_highbd_fdct4x4;
3890 x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3891 x->highbd_itxm_add = xd->lossless ? vp9_highbd_iwht4x4_add :
3892 vp9_highbd_idct4x4_add;
3894 x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3895 #endif // CONFIG_VP9_HIGHBITDEPTH
3896 x->itxm_add = xd->lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
3901 cm->tx_mode = select_tx_mode(cpi, xd);
3903 vp9_frame_init_quantizer(cpi);
3905 vp9_initialize_rd_consts(cpi);
3906 vp9_initialize_me_consts(cpi, x, cm->base_qindex);
3907 init_encode_frame_mb_context(cpi);
3908 cm->use_prev_frame_mvs = !cm->error_resilient_mode &&
3909 cm->width == cm->last_width &&
3910 cm->height == cm->last_height &&
3912 cm->last_show_frame;
3913 // Special case: set prev_mi to NULL when the previous mode info
3914 // context cannot be used.
3915 cm->prev_mi = cm->use_prev_frame_mvs ?
3916 cm->prev_mip + cm->mi_stride + 1 : NULL;
3918 x->quant_fp = cpi->sf.use_quant_fp;
3919 vp9_zero(x->skip_txfm);
3920 if (sf->use_nonrd_pick_mode) {
3921 // Initialize internal buffer pointers for rtc coding, where non-RD
3922 // mode decision is used and hence no buffer pointer swap needed.
3924 struct macroblock_plane *const p = x->plane;
3925 struct macroblockd_plane *const pd = xd->plane;
3926 PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
3928 for (i = 0; i < MAX_MB_PLANE; ++i) {
3929 p[i].coeff = ctx->coeff_pbuf[i][0];
3930 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
3931 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
3932 p[i].eobs = ctx->eobs_pbuf[i][0];
3934 vp9_zero(x->zcoeff_blk);
3936 if (cm->frame_type != KEY_FRAME && cpi->rc.frames_since_golden == 0)
3937 cpi->ref_frame_flags &= (~VP9_GOLD_FLAG);
3939 if (sf->partition_search_type == SOURCE_VAR_BASED_PARTITION)
3940 source_var_based_partition_search_method(cpi);
3944 struct vpx_usec_timer emr_timer;
3945 vpx_usec_timer_start(&emr_timer);
3947 #if CONFIG_FP_MB_STATS
3948 if (cpi->use_fp_mb_stats) {
3949 input_fpmb_stats(&cpi->twopass.firstpass_mb_stats, cm,
3950 &cpi->twopass.this_frame_mb_stats);
3954 // If allowed, encoding tiles in parallel with one thread handling one tile.
3955 if (MIN(cpi->oxcf.max_threads, 1 << cm->log2_tile_cols) > 1)
3956 vp9_encode_tiles_mt(cpi);
3960 vpx_usec_timer_mark(&emr_timer);
3961 cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
3964 sf->skip_encode_frame = sf->skip_encode_sb ?
3965 get_skip_encode_frame(cm, td) : 0;
3968 // Keep record of the total distortion this time around for future use
3969 cpi->last_frame_distortion = cpi->frame_distortion;
3973 static INTERP_FILTER get_interp_filter(
3974 const int64_t threshes[SWITCHABLE_FILTER_CONTEXTS], int is_alt_ref) {
3976 threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP] &&
3977 threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP_SHARP] &&
3978 threshes[EIGHTTAP_SMOOTH] > threshes[SWITCHABLE - 1]) {
3979 return EIGHTTAP_SMOOTH;
3980 } else if (threshes[EIGHTTAP_SHARP] > threshes[EIGHTTAP] &&
3981 threshes[EIGHTTAP_SHARP] > threshes[SWITCHABLE - 1]) {
3982 return EIGHTTAP_SHARP;
3983 } else if (threshes[EIGHTTAP] > threshes[SWITCHABLE - 1]) {
3990 void vp9_encode_frame(VP9_COMP *cpi) {
3991 VP9_COMMON *const cm = &cpi->common;
3993 // In the longer term the encoder should be generalized to match the
3994 // decoder such that we allow compound where one of the 3 buffers has a
3995 // different sign bias and that buffer is then the fixed ref. However, this
3996 // requires further work in the rd loop. For now the only supported encoder
3997 // side behavior is where the ALT ref buffer has opposite sign bias to
3999 if (!frame_is_intra_only(cm)) {
4000 if ((cm->ref_frame_sign_bias[ALTREF_FRAME] ==
4001 cm->ref_frame_sign_bias[GOLDEN_FRAME]) ||
4002 (cm->ref_frame_sign_bias[ALTREF_FRAME] ==
4003 cm->ref_frame_sign_bias[LAST_FRAME])) {
4004 cpi->allow_comp_inter_inter = 0;
4006 cpi->allow_comp_inter_inter = 1;
4007 cm->comp_fixed_ref = ALTREF_FRAME;
4008 cm->comp_var_ref[0] = LAST_FRAME;
4009 cm->comp_var_ref[1] = GOLDEN_FRAME;
4013 if (cpi->sf.frame_parameter_update) {
4015 RD_OPT *const rd_opt = &cpi->rd;
4016 FRAME_COUNTS *counts = cpi->td.counts;
4017 RD_COUNTS *const rdc = &cpi->td.rd_counts;
4019 // This code does a single RD pass over the whole frame assuming
4020 // either compound, single or hybrid prediction as per whatever has
4021 // worked best for that type of frame in the past.
4022 // It also predicts whether another coding mode would have worked
4023 // better that this coding mode. If that is the case, it remembers
4024 // that for subsequent frames.
4025 // It does the same analysis for transform size selection also.
4026 const MV_REFERENCE_FRAME frame_type = get_frame_type(cpi);
4027 int64_t *const mode_thrs = rd_opt->prediction_type_threshes[frame_type];
4028 int64_t *const filter_thrs = rd_opt->filter_threshes[frame_type];
4029 int *const tx_thrs = rd_opt->tx_select_threshes[frame_type];
4030 const int is_alt_ref = frame_type == ALTREF_FRAME;
4032 /* prediction (compound, single or hybrid) mode selection */
4033 if (is_alt_ref || !cpi->allow_comp_inter_inter)
4034 cm->reference_mode = SINGLE_REFERENCE;
4035 else if (mode_thrs[COMPOUND_REFERENCE] > mode_thrs[SINGLE_REFERENCE] &&
4036 mode_thrs[COMPOUND_REFERENCE] >
4037 mode_thrs[REFERENCE_MODE_SELECT] &&
4038 check_dual_ref_flags(cpi) &&
4039 cpi->static_mb_pct == 100)
4040 cm->reference_mode = COMPOUND_REFERENCE;
4041 else if (mode_thrs[SINGLE_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT])
4042 cm->reference_mode = SINGLE_REFERENCE;
4044 cm->reference_mode = REFERENCE_MODE_SELECT;
4046 if (cm->interp_filter == SWITCHABLE)
4047 cm->interp_filter = get_interp_filter(filter_thrs, is_alt_ref);
4049 encode_frame_internal(cpi);
4051 for (i = 0; i < REFERENCE_MODES; ++i)
4052 mode_thrs[i] = (mode_thrs[i] + rdc->comp_pred_diff[i] / cm->MBs) / 2;
4054 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
4055 filter_thrs[i] = (filter_thrs[i] + rdc->filter_diff[i] / cm->MBs) / 2;
4057 for (i = 0; i < TX_MODES; ++i) {
4058 int64_t pd = rdc->tx_select_diff[i];
4059 if (i == TX_MODE_SELECT)
4060 pd -= RDCOST(cpi->td.mb.rdmult, cpi->td.mb.rddiv, 2048 * (TX_SIZES - 1),
4062 tx_thrs[i] = (tx_thrs[i] + (int)(pd / cm->MBs)) / 2;
4065 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
4066 int single_count_zero = 0;
4067 int comp_count_zero = 0;
4069 for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
4070 single_count_zero += counts->comp_inter[i][0];
4071 comp_count_zero += counts->comp_inter[i][1];
4074 if (comp_count_zero == 0) {
4075 cm->reference_mode = SINGLE_REFERENCE;
4076 vp9_zero(counts->comp_inter);
4077 } else if (single_count_zero == 0) {
4078 cm->reference_mode = COMPOUND_REFERENCE;
4079 vp9_zero(counts->comp_inter);
4083 if (cm->tx_mode == TX_MODE_SELECT) {
4085 int count8x8_lp = 0, count8x8_8x8p = 0;
4086 int count16x16_16x16p = 0, count16x16_lp = 0;
4089 for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
4090 count4x4 += counts->tx.p32x32[i][TX_4X4];
4091 count4x4 += counts->tx.p16x16[i][TX_4X4];
4092 count4x4 += counts->tx.p8x8[i][TX_4X4];
4094 count8x8_lp += counts->tx.p32x32[i][TX_8X8];
4095 count8x8_lp += counts->tx.p16x16[i][TX_8X8];
4096 count8x8_8x8p += counts->tx.p8x8[i][TX_8X8];
4098 count16x16_16x16p += counts->tx.p16x16[i][TX_16X16];
4099 count16x16_lp += counts->tx.p32x32[i][TX_16X16];
4100 count32x32 += counts->tx.p32x32[i][TX_32X32];
4102 if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0 &&
4104 cm->tx_mode = ALLOW_8X8;
4105 reset_skip_tx_size(cm, TX_8X8);
4106 } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0 &&
4107 count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
4108 cm->tx_mode = ONLY_4X4;
4109 reset_skip_tx_size(cm, TX_4X4);
4110 } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
4111 cm->tx_mode = ALLOW_32X32;
4112 } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
4113 cm->tx_mode = ALLOW_16X16;
4114 reset_skip_tx_size(cm, TX_16X16);
4118 cm->reference_mode = SINGLE_REFERENCE;
4119 encode_frame_internal(cpi);
4123 static void sum_intra_stats(FRAME_COUNTS *counts, const MODE_INFO *mi) {
4124 const PREDICTION_MODE y_mode = mi->mbmi.mode;
4125 const PREDICTION_MODE uv_mode = mi->mbmi.uv_mode;
4126 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
4128 if (bsize < BLOCK_8X8) {
4130 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
4131 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
4132 for (idy = 0; idy < 2; idy += num_4x4_h)
4133 for (idx = 0; idx < 2; idx += num_4x4_w)
4134 ++counts->y_mode[0][mi->bmi[idy * 2 + idx].as_mode];
4136 ++counts->y_mode[size_group_lookup[bsize]][y_mode];
4139 ++counts->uv_mode[y_mode][uv_mode];
4142 static void encode_superblock(VP9_COMP *cpi, ThreadData *td,
4143 TOKENEXTRA **t, int output_enabled,
4144 int mi_row, int mi_col, BLOCK_SIZE bsize,
4145 PICK_MODE_CONTEXT *ctx) {
4146 VP9_COMMON *const cm = &cpi->common;
4147 MACROBLOCK *const x = &td->mb;
4148 MACROBLOCKD *const xd = &x->e_mbd;
4149 MODE_INFO **mi_8x8 = xd->mi;
4150 MODE_INFO *mi = mi_8x8[0];
4151 MB_MODE_INFO *mbmi = &mi->mbmi;
4152 const int seg_skip = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
4154 const int mis = cm->mi_stride;
4155 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
4156 const int mi_height = num_8x8_blocks_high_lookup[bsize];
4158 x->skip_recode = !x->select_tx_size && mbmi->sb_type >= BLOCK_8X8 &&
4159 cpi->oxcf.aq_mode != COMPLEXITY_AQ &&
4160 cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ &&
4161 cpi->sf.allow_skip_recode;
4163 if (!x->skip_recode && !cpi->sf.use_nonrd_pick_mode)
4164 memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
4166 x->skip_optimize = ctx->is_coded;
4168 x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
4169 x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
4170 x->q_index < QIDX_SKIP_THRESH);
4175 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
4177 if (!is_inter_block(mbmi)) {
4180 for (plane = 0; plane < MAX_MB_PLANE; ++plane)
4181 vp9_encode_intra_block_plane(x, MAX(bsize, BLOCK_8X8), plane);
4183 sum_intra_stats(td->counts, mi);
4184 vp9_tokenize_sb(cpi, td, t, !output_enabled, MAX(bsize, BLOCK_8X8));
4187 const int is_compound = has_second_ref(mbmi);
4188 for (ref = 0; ref < 1 + is_compound; ++ref) {
4189 YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi,
4190 mbmi->ref_frame[ref]);
4191 assert(cfg != NULL);
4192 vp9_setup_pre_planes(xd, ref, cfg, mi_row, mi_col,
4193 &xd->block_refs[ref]->sf);
4195 if (!(cpi->sf.reuse_inter_pred_sby && ctx->pred_pixel_ready) || seg_skip)
4196 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8));
4198 vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8));
4200 vp9_encode_sb(x, MAX(bsize, BLOCK_8X8));
4201 vp9_tokenize_sb(cpi, td, t, !output_enabled, MAX(bsize, BLOCK_8X8));
4204 if (output_enabled) {
4205 if (cm->tx_mode == TX_MODE_SELECT &&
4206 mbmi->sb_type >= BLOCK_8X8 &&
4207 !(is_inter_block(mbmi) && (mbmi->skip || seg_skip))) {
4208 ++get_tx_counts(max_txsize_lookup[bsize], vp9_get_tx_size_context(xd),
4209 &td->counts->tx)[mbmi->tx_size];
4213 // The new intra coding scheme requires no change of transform size
4214 if (is_inter_block(&mi->mbmi)) {
4215 tx_size = MIN(tx_mode_to_biggest_tx_size[cm->tx_mode],
4216 max_txsize_lookup[bsize]);
4218 tx_size = (bsize >= BLOCK_8X8) ? mbmi->tx_size : TX_4X4;
4221 for (y = 0; y < mi_height; y++)
4222 for (x = 0; x < mi_width; x++)
4223 if (mi_col + x < cm->mi_cols && mi_row + y < cm->mi_rows)
4224 mi_8x8[mis * y + x]->mbmi.tx_size = tx_size;
4226 ++td->counts->tx.tx_totals[mbmi->tx_size];
4227 ++td->counts->tx.tx_totals[get_uv_tx_size(mbmi, &xd->plane[1])];