1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11 #include <limits.h>
12 #include <math.h>
13 #include <stdio.h>
14
15 #include "./vp9_rtcd.h"
16 #include "./vpx_config.h"
17
18 #include "vpx_ports/vpx_timer.h"
19
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"
32
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_extend.h"
40 #include "vp9/encoder/vp9_pickmode.h"
41 #include "vp9/encoder/vp9_rd.h"
42 #include "vp9/encoder/vp9_rdopt.h"
43 #include "vp9/encoder/vp9_segmentation.h"
44 #include "vp9/encoder/vp9_tokenize.h"
45
46 #define GF_ZEROMV_ZBIN_BOOST 0
47 #define LF_ZEROMV_ZBIN_BOOST 0
48 #define MV_ZBIN_BOOST 0
49 #define SPLIT_MV_ZBIN_BOOST 0
50 #define INTRA_ZBIN_BOOST 0
51
52 static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled,
53 int mi_row, int mi_col, BLOCK_SIZE bsize,
54 PICK_MODE_CONTEXT *ctx);
55
56 // Motion vector component magnitude threshold for defining fast motion.
57 #define FAST_MOTION_MV_THRESH 24
58
59 // This is used as a reference when computing the source variance for the
60 // purposes of activity masking.
61 // Eventually this should be replaced by custom no-reference routines,
62 // which will be faster.
63 static const uint8_t VP9_VAR_OFFS[64] = {
64 128, 128, 128, 128, 128, 128, 128, 128,
65 128, 128, 128, 128, 128, 128, 128, 128,
66 128, 128, 128, 128, 128, 128, 128, 128,
67 128, 128, 128, 128, 128, 128, 128, 128,
68 128, 128, 128, 128, 128, 128, 128, 128,
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 };
73
get_sby_perpixel_variance(VP9_COMP * cpi,const struct buf_2d * ref,BLOCK_SIZE bs)74 static unsigned int get_sby_perpixel_variance(VP9_COMP *cpi,
75 const struct buf_2d *ref,
76 BLOCK_SIZE bs) {
77 unsigned int sse;
78 const unsigned int var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
79 VP9_VAR_OFFS, 0, &sse);
80 return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
81 }
82
get_sby_perpixel_diff_variance(VP9_COMP * cpi,const struct buf_2d * ref,int mi_row,int mi_col,BLOCK_SIZE bs)83 static unsigned int get_sby_perpixel_diff_variance(VP9_COMP *cpi,
84 const struct buf_2d *ref,
85 int mi_row, int mi_col,
86 BLOCK_SIZE bs) {
87 const YV12_BUFFER_CONFIG *last = get_ref_frame_buffer(cpi, LAST_FRAME);
88 const uint8_t* last_y = &last->y_buffer[mi_row * MI_SIZE * last->y_stride +
89 mi_col * MI_SIZE];
90 unsigned int sse;
91 const unsigned int var = cpi->fn_ptr[bs].vf(ref->buf, ref->stride,
92 last_y, last->y_stride, &sse);
93 return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[bs]);
94 }
95
get_rd_var_based_fixed_partition(VP9_COMP * cpi,int mi_row,int mi_col)96 static BLOCK_SIZE get_rd_var_based_fixed_partition(VP9_COMP *cpi,
97 int mi_row,
98 int mi_col) {
99 unsigned int var = get_sby_perpixel_diff_variance(cpi, &cpi->mb.plane[0].src,
100 mi_row, mi_col,
101 BLOCK_64X64);
102 if (var < 8)
103 return BLOCK_64X64;
104 else if (var < 128)
105 return BLOCK_32X32;
106 else if (var < 2048)
107 return BLOCK_16X16;
108 else
109 return BLOCK_8X8;
110 }
111
get_nonrd_var_based_fixed_partition(VP9_COMP * cpi,int mi_row,int mi_col)112 static BLOCK_SIZE get_nonrd_var_based_fixed_partition(VP9_COMP *cpi,
113 int mi_row,
114 int mi_col) {
115 unsigned int var = get_sby_perpixel_diff_variance(cpi, &cpi->mb.plane[0].src,
116 mi_row, mi_col,
117 BLOCK_64X64);
118 if (var < 4)
119 return BLOCK_64X64;
120 else if (var < 10)
121 return BLOCK_32X32;
122 else
123 return BLOCK_16X16;
124 }
125
126 // Lighter version of set_offsets that only sets the mode info
127 // pointers.
set_modeinfo_offsets(VP9_COMMON * const cm,MACROBLOCKD * const xd,int mi_row,int mi_col)128 static INLINE void set_modeinfo_offsets(VP9_COMMON *const cm,
129 MACROBLOCKD *const xd,
130 int mi_row,
131 int mi_col) {
132 const int idx_str = xd->mi_stride * mi_row + mi_col;
133 xd->mi = cm->mi_grid_visible + idx_str;
134 xd->mi[0] = cm->mi + idx_str;
135 }
136
set_offsets(VP9_COMP * cpi,const TileInfo * const tile,int mi_row,int mi_col,BLOCK_SIZE bsize)137 static void set_offsets(VP9_COMP *cpi, const TileInfo *const tile,
138 int mi_row, int mi_col, BLOCK_SIZE bsize) {
139 MACROBLOCK *const x = &cpi->mb;
140 VP9_COMMON *const cm = &cpi->common;
141 MACROBLOCKD *const xd = &x->e_mbd;
142 MB_MODE_INFO *mbmi;
143 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
144 const int mi_height = num_8x8_blocks_high_lookup[bsize];
145 const struct segmentation *const seg = &cm->seg;
146
147 set_skip_context(xd, mi_row, mi_col);
148
149 set_modeinfo_offsets(cm, xd, mi_row, mi_col);
150
151 mbmi = &xd->mi[0]->mbmi;
152
153 // Set up destination pointers.
154 vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
155
156 // Set up limit values for MV components.
157 // Mv beyond the range do not produce new/different prediction block.
158 x->mv_row_min = -(((mi_row + mi_height) * MI_SIZE) + VP9_INTERP_EXTEND);
159 x->mv_col_min = -(((mi_col + mi_width) * MI_SIZE) + VP9_INTERP_EXTEND);
160 x->mv_row_max = (cm->mi_rows - mi_row) * MI_SIZE + VP9_INTERP_EXTEND;
161 x->mv_col_max = (cm->mi_cols - mi_col) * MI_SIZE + VP9_INTERP_EXTEND;
162
163 // Set up distance of MB to edge of frame in 1/8th pel units.
164 assert(!(mi_col & (mi_width - 1)) && !(mi_row & (mi_height - 1)));
165 set_mi_row_col(xd, tile, mi_row, mi_height, mi_col, mi_width,
166 cm->mi_rows, cm->mi_cols);
167
168 // Set up source buffers.
169 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
170
171 // R/D setup.
172 x->rddiv = cpi->rd.RDDIV;
173 x->rdmult = cpi->rd.RDMULT;
174
175 // Setup segment ID.
176 if (seg->enabled) {
177 if (cpi->oxcf.aq_mode != VARIANCE_AQ) {
178 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
179 : cm->last_frame_seg_map;
180 mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
181 }
182 vp9_init_plane_quantizers(cpi, x);
183
184 x->encode_breakout = cpi->segment_encode_breakout[mbmi->segment_id];
185 } else {
186 mbmi->segment_id = 0;
187 x->encode_breakout = cpi->encode_breakout;
188 }
189 }
190
duplicate_mode_info_in_sb(VP9_COMMON * cm,MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize)191 static void duplicate_mode_info_in_sb(VP9_COMMON *cm, MACROBLOCKD *xd,
192 int mi_row, int mi_col,
193 BLOCK_SIZE bsize) {
194 const int block_width = num_8x8_blocks_wide_lookup[bsize];
195 const int block_height = num_8x8_blocks_high_lookup[bsize];
196 int i, j;
197 for (j = 0; j < block_height; ++j)
198 for (i = 0; i < block_width; ++i) {
199 if (mi_row + j < cm->mi_rows && mi_col + i < cm->mi_cols)
200 xd->mi[j * xd->mi_stride + i] = xd->mi[0];
201 }
202 }
203
set_block_size(VP9_COMP * const cpi,int mi_row,int mi_col,BLOCK_SIZE bsize)204 static void set_block_size(VP9_COMP * const cpi,
205 int mi_row, int mi_col,
206 BLOCK_SIZE bsize) {
207 if (cpi->common.mi_cols > mi_col && cpi->common.mi_rows > mi_row) {
208 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
209 set_modeinfo_offsets(&cpi->common, xd, mi_row, mi_col);
210 xd->mi[0]->mbmi.sb_type = bsize;
211 duplicate_mode_info_in_sb(&cpi->common, xd, mi_row, mi_col, bsize);
212 }
213 }
214
215 typedef struct {
216 int64_t sum_square_error;
217 int64_t sum_error;
218 int count;
219 int variance;
220 } var;
221
222 typedef struct {
223 var none;
224 var horz[2];
225 var vert[2];
226 } partition_variance;
227
228 typedef struct {
229 partition_variance part_variances;
230 var split[4];
231 } v8x8;
232
233 typedef struct {
234 partition_variance part_variances;
235 v8x8 split[4];
236 } v16x16;
237
238 typedef struct {
239 partition_variance part_variances;
240 v16x16 split[4];
241 } v32x32;
242
243 typedef struct {
244 partition_variance part_variances;
245 v32x32 split[4];
246 } v64x64;
247
248 typedef struct {
249 partition_variance *part_variances;
250 var *split[4];
251 } variance_node;
252
253 typedef enum {
254 V16X16,
255 V32X32,
256 V64X64,
257 } TREE_LEVEL;
258
tree_to_node(void * data,BLOCK_SIZE bsize,variance_node * node)259 static void tree_to_node(void *data, BLOCK_SIZE bsize, variance_node *node) {
260 int i;
261 node->part_variances = NULL;
262 vpx_memset(node->split, 0, sizeof(node->split));
263 switch (bsize) {
264 case BLOCK_64X64: {
265 v64x64 *vt = (v64x64 *) data;
266 node->part_variances = &vt->part_variances;
267 for (i = 0; i < 4; i++)
268 node->split[i] = &vt->split[i].part_variances.none;
269 break;
270 }
271 case BLOCK_32X32: {
272 v32x32 *vt = (v32x32 *) data;
273 node->part_variances = &vt->part_variances;
274 for (i = 0; i < 4; i++)
275 node->split[i] = &vt->split[i].part_variances.none;
276 break;
277 }
278 case BLOCK_16X16: {
279 v16x16 *vt = (v16x16 *) data;
280 node->part_variances = &vt->part_variances;
281 for (i = 0; i < 4; i++)
282 node->split[i] = &vt->split[i].part_variances.none;
283 break;
284 }
285 case BLOCK_8X8: {
286 v8x8 *vt = (v8x8 *) data;
287 node->part_variances = &vt->part_variances;
288 for (i = 0; i < 4; i++)
289 node->split[i] = &vt->split[i];
290 break;
291 }
292 default: {
293 assert(0);
294 break;
295 }
296 }
297 }
298
299 // Set variance values given sum square error, sum error, count.
fill_variance(int64_t s2,int64_t s,int c,var * v)300 static void fill_variance(int64_t s2, int64_t s, int c, var *v) {
301 v->sum_square_error = s2;
302 v->sum_error = s;
303 v->count = c;
304 if (c > 0)
305 v->variance = (int)(256 *
306 (v->sum_square_error - v->sum_error * v->sum_error /
307 v->count) / v->count);
308 else
309 v->variance = 0;
310 }
311
sum_2_variances(const var * a,const var * b,var * r)312 void sum_2_variances(const var *a, const var *b, var *r) {
313 fill_variance(a->sum_square_error + b->sum_square_error,
314 a->sum_error + b->sum_error, a->count + b->count, r);
315 }
316
fill_variance_tree(void * data,BLOCK_SIZE bsize)317 static void fill_variance_tree(void *data, BLOCK_SIZE bsize) {
318 variance_node node;
319 tree_to_node(data, bsize, &node);
320 sum_2_variances(node.split[0], node.split[1], &node.part_variances->horz[0]);
321 sum_2_variances(node.split[2], node.split[3], &node.part_variances->horz[1]);
322 sum_2_variances(node.split[0], node.split[2], &node.part_variances->vert[0]);
323 sum_2_variances(node.split[1], node.split[3], &node.part_variances->vert[1]);
324 sum_2_variances(&node.part_variances->vert[0], &node.part_variances->vert[1],
325 &node.part_variances->none);
326 }
327
set_vt_partitioning(VP9_COMP * cpi,void * data,BLOCK_SIZE bsize,int mi_row,int mi_col)328 static int set_vt_partitioning(VP9_COMP *cpi,
329 void *data,
330 BLOCK_SIZE bsize,
331 int mi_row,
332 int mi_col) {
333 VP9_COMMON * const cm = &cpi->common;
334 variance_node vt;
335 const int block_width = num_8x8_blocks_wide_lookup[bsize];
336 const int block_height = num_8x8_blocks_high_lookup[bsize];
337 // TODO(debargha): Choose this more intelligently.
338 const int64_t threshold_multiplier = 25;
339 int64_t threshold = threshold_multiplier * cpi->common.base_qindex;
340 assert(block_height == block_width);
341
342 tree_to_node(data, bsize, &vt);
343
344 // Split none is available only if we have more than half a block size
345 // in width and height inside the visible image.
346 if (mi_col + block_width / 2 < cm->mi_cols &&
347 mi_row + block_height / 2 < cm->mi_rows &&
348 vt.part_variances->none.variance < threshold) {
349 set_block_size(cpi, mi_row, mi_col, bsize);
350 return 1;
351 }
352
353 // Vertical split is available on all but the bottom border.
354 if (mi_row + block_height / 2 < cm->mi_rows &&
355 vt.part_variances->vert[0].variance < threshold &&
356 vt.part_variances->vert[1].variance < threshold) {
357 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_VERT);
358 set_block_size(cpi, mi_row, mi_col, subsize);
359 set_block_size(cpi, mi_row, mi_col + block_width / 2, subsize);
360 return 1;
361 }
362
363 // Horizontal split is available on all but the right border.
364 if (mi_col + block_width / 2 < cm->mi_cols &&
365 vt.part_variances->horz[0].variance < threshold &&
366 vt.part_variances->horz[1].variance < threshold) {
367 BLOCK_SIZE subsize = get_subsize(bsize, PARTITION_HORZ);
368 set_block_size(cpi, mi_row, mi_col, subsize);
369 set_block_size(cpi, mi_row + block_height / 2, mi_col, subsize);
370 return 1;
371 }
372 return 0;
373 }
374
375 // TODO(debargha): Fix this function and make it work as expected.
choose_partitioning(VP9_COMP * cpi,const TileInfo * const tile,int mi_row,int mi_col)376 static void choose_partitioning(VP9_COMP *cpi,
377 const TileInfo *const tile,
378 int mi_row, int mi_col) {
379 VP9_COMMON * const cm = &cpi->common;
380 MACROBLOCK *x = &cpi->mb;
381 MACROBLOCKD *xd = &cpi->mb.e_mbd;
382
383 int i, j, k;
384 v64x64 vt;
385 uint8_t *s;
386 const uint8_t *d;
387 int sp;
388 int dp;
389 int pixels_wide = 64, pixels_high = 64;
390 int_mv nearest_mv, near_mv;
391 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
392 const struct scale_factors *const sf = &cm->frame_refs[LAST_FRAME - 1].sf;
393
394 vp9_zero(vt);
395 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
396
397 if (xd->mb_to_right_edge < 0)
398 pixels_wide += (xd->mb_to_right_edge >> 3);
399 if (xd->mb_to_bottom_edge < 0)
400 pixels_high += (xd->mb_to_bottom_edge >> 3);
401
402 s = x->plane[0].src.buf;
403 sp = x->plane[0].src.stride;
404
405 if (cm->frame_type != KEY_FRAME) {
406 vp9_setup_pre_planes(xd, 0, yv12, mi_row, mi_col, sf);
407
408 xd->mi[0]->mbmi.ref_frame[0] = LAST_FRAME;
409 xd->mi[0]->mbmi.sb_type = BLOCK_64X64;
410 vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv,
411 xd->mi[0]->mbmi.ref_mvs[LAST_FRAME],
412 &nearest_mv, &near_mv);
413
414 xd->mi[0]->mbmi.mv[0] = nearest_mv;
415 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, BLOCK_64X64);
416
417 d = xd->plane[0].dst.buf;
418 dp = xd->plane[0].dst.stride;
419 } else {
420 d = VP9_VAR_OFFS;
421 dp = 0;
422 }
423
424 // Fill in the entire tree of 8x8 variances for splits.
425 for (i = 0; i < 4; i++) {
426 const int x32_idx = ((i & 1) << 5);
427 const int y32_idx = ((i >> 1) << 5);
428 for (j = 0; j < 4; j++) {
429 const int x16_idx = x32_idx + ((j & 1) << 4);
430 const int y16_idx = y32_idx + ((j >> 1) << 4);
431 v16x16 *vst = &vt.split[i].split[j];
432 for (k = 0; k < 4; k++) {
433 int x_idx = x16_idx + ((k & 1) << 3);
434 int y_idx = y16_idx + ((k >> 1) << 3);
435 unsigned int sse = 0;
436 int sum = 0;
437 if (x_idx < pixels_wide && y_idx < pixels_high)
438 vp9_get8x8var(s + y_idx * sp + x_idx, sp,
439 d + y_idx * dp + x_idx, dp, &sse, &sum);
440 fill_variance(sse, sum, 64, &vst->split[k].part_variances.none);
441 }
442 }
443 }
444 // Fill the rest of the variance tree by summing split partition values.
445 for (i = 0; i < 4; i++) {
446 for (j = 0; j < 4; j++) {
447 fill_variance_tree(&vt.split[i].split[j], BLOCK_16X16);
448 }
449 fill_variance_tree(&vt.split[i], BLOCK_32X32);
450 }
451 fill_variance_tree(&vt, BLOCK_64X64);
452
453 // Now go through the entire structure, splitting every block size until
454 // we get to one that's got a variance lower than our threshold, or we
455 // hit 8x8.
456 if (!set_vt_partitioning(cpi, &vt, BLOCK_64X64,
457 mi_row, mi_col)) {
458 for (i = 0; i < 4; ++i) {
459 const int x32_idx = ((i & 1) << 2);
460 const int y32_idx = ((i >> 1) << 2);
461 if (!set_vt_partitioning(cpi, &vt.split[i], BLOCK_32X32,
462 (mi_row + y32_idx), (mi_col + x32_idx))) {
463 for (j = 0; j < 4; ++j) {
464 const int x16_idx = ((j & 1) << 1);
465 const int y16_idx = ((j >> 1) << 1);
466 // NOTE: This is a temporary hack to disable 8x8 partitions,
467 // since it works really bad - possibly due to a bug
468 #define DISABLE_8X8_VAR_BASED_PARTITION
469 #ifdef DISABLE_8X8_VAR_BASED_PARTITION
470 if (mi_row + y32_idx + y16_idx + 1 < cm->mi_rows &&
471 mi_row + x32_idx + x16_idx + 1 < cm->mi_cols) {
472 set_block_size(cpi,
473 (mi_row + y32_idx + y16_idx),
474 (mi_col + x32_idx + x16_idx),
475 BLOCK_16X16);
476 } else {
477 for (k = 0; k < 4; ++k) {
478 const int x8_idx = (k & 1);
479 const int y8_idx = (k >> 1);
480 set_block_size(cpi,
481 (mi_row + y32_idx + y16_idx + y8_idx),
482 (mi_col + x32_idx + x16_idx + x8_idx),
483 BLOCK_8X8);
484 }
485 }
486 #else
487 if (!set_vt_partitioning(cpi, &vt.split[i].split[j], tile,
488 BLOCK_16X16,
489 (mi_row + y32_idx + y16_idx),
490 (mi_col + x32_idx + x16_idx), 2)) {
491 for (k = 0; k < 4; ++k) {
492 const int x8_idx = (k & 1);
493 const int y8_idx = (k >> 1);
494 set_block_size(cpi,
495 (mi_row + y32_idx + y16_idx + y8_idx),
496 (mi_col + x32_idx + x16_idx + x8_idx),
497 BLOCK_8X8);
498 }
499 }
500 #endif
501 }
502 }
503 }
504 }
505 }
506
update_state(VP9_COMP * cpi,PICK_MODE_CONTEXT * ctx,int mi_row,int mi_col,BLOCK_SIZE bsize,int output_enabled)507 static void update_state(VP9_COMP *cpi, PICK_MODE_CONTEXT *ctx,
508 int mi_row, int mi_col, BLOCK_SIZE bsize,
509 int output_enabled) {
510 int i, x_idx, y;
511 VP9_COMMON *const cm = &cpi->common;
512 RD_OPT *const rd_opt = &cpi->rd;
513 MACROBLOCK *const x = &cpi->mb;
514 MACROBLOCKD *const xd = &x->e_mbd;
515 struct macroblock_plane *const p = x->plane;
516 struct macroblockd_plane *const pd = xd->plane;
517 MODE_INFO *mi = &ctx->mic;
518 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
519 MODE_INFO *mi_addr = xd->mi[0];
520 const struct segmentation *const seg = &cm->seg;
521
522 const int mis = cm->mi_stride;
523 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
524 const int mi_height = num_8x8_blocks_high_lookup[bsize];
525 int max_plane;
526
527 assert(mi->mbmi.sb_type == bsize);
528
529 *mi_addr = *mi;
530
531 // If segmentation in use
532 if (seg->enabled && output_enabled) {
533 // For in frame complexity AQ copy the segment id from the segment map.
534 if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
535 const uint8_t *const map = seg->update_map ? cpi->segmentation_map
536 : cm->last_frame_seg_map;
537 mi_addr->mbmi.segment_id =
538 vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
539 }
540 // Else for cyclic refresh mode update the segment map, set the segment id
541 // and then update the quantizer.
542 else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
543 vp9_cyclic_refresh_update_segment(cpi, &xd->mi[0]->mbmi,
544 mi_row, mi_col, bsize, 1);
545 vp9_init_plane_quantizers(cpi, x);
546 }
547 }
548
549 max_plane = is_inter_block(mbmi) ? MAX_MB_PLANE : 1;
550 for (i = 0; i < max_plane; ++i) {
551 p[i].coeff = ctx->coeff_pbuf[i][1];
552 p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
553 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
554 p[i].eobs = ctx->eobs_pbuf[i][1];
555 }
556
557 for (i = max_plane; i < MAX_MB_PLANE; ++i) {
558 p[i].coeff = ctx->coeff_pbuf[i][2];
559 p[i].qcoeff = ctx->qcoeff_pbuf[i][2];
560 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2];
561 p[i].eobs = ctx->eobs_pbuf[i][2];
562 }
563
564 // Restore the coding context of the MB to that that was in place
565 // when the mode was picked for it
566 for (y = 0; y < mi_height; y++)
567 for (x_idx = 0; x_idx < mi_width; x_idx++)
568 if ((xd->mb_to_right_edge >> (3 + MI_SIZE_LOG2)) + mi_width > x_idx
569 && (xd->mb_to_bottom_edge >> (3 + MI_SIZE_LOG2)) + mi_height > y) {
570 xd->mi[x_idx + y * mis] = mi_addr;
571 }
572
573 if (cpi->oxcf.aq_mode)
574 vp9_init_plane_quantizers(cpi, x);
575
576 // FIXME(rbultje) I'm pretty sure this should go to the end of this block
577 // (i.e. after the output_enabled)
578 if (bsize < BLOCK_32X32) {
579 if (bsize < BLOCK_16X16)
580 ctx->tx_rd_diff[ALLOW_16X16] = ctx->tx_rd_diff[ALLOW_8X8];
581 ctx->tx_rd_diff[ALLOW_32X32] = ctx->tx_rd_diff[ALLOW_16X16];
582 }
583
584 if (is_inter_block(mbmi) && mbmi->sb_type < BLOCK_8X8) {
585 mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
586 mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
587 }
588
589 x->skip = ctx->skip;
590 vpx_memcpy(x->zcoeff_blk[mbmi->tx_size], ctx->zcoeff_blk,
591 sizeof(uint8_t) * ctx->num_4x4_blk);
592
593 if (!output_enabled)
594 return;
595
596 if (!vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
597 for (i = 0; i < TX_MODES; i++)
598 rd_opt->tx_select_diff[i] += ctx->tx_rd_diff[i];
599 }
600
601 #if CONFIG_INTERNAL_STATS
602 if (frame_is_intra_only(cm)) {
603 static const int kf_mode_index[] = {
604 THR_DC /*DC_PRED*/,
605 THR_V_PRED /*V_PRED*/,
606 THR_H_PRED /*H_PRED*/,
607 THR_D45_PRED /*D45_PRED*/,
608 THR_D135_PRED /*D135_PRED*/,
609 THR_D117_PRED /*D117_PRED*/,
610 THR_D153_PRED /*D153_PRED*/,
611 THR_D207_PRED /*D207_PRED*/,
612 THR_D63_PRED /*D63_PRED*/,
613 THR_TM /*TM_PRED*/,
614 };
615 ++cpi->mode_chosen_counts[kf_mode_index[mbmi->mode]];
616 } else {
617 // Note how often each mode chosen as best
618 ++cpi->mode_chosen_counts[ctx->best_mode_index];
619 }
620 #endif
621 if (!frame_is_intra_only(cm)) {
622 if (is_inter_block(mbmi)) {
623 vp9_update_mv_count(cm, xd);
624
625 if (cm->interp_filter == SWITCHABLE) {
626 const int ctx = vp9_get_pred_context_switchable_interp(xd);
627 ++cm->counts.switchable_interp[ctx][mbmi->interp_filter];
628 }
629 }
630
631 rd_opt->comp_pred_diff[SINGLE_REFERENCE] += ctx->single_pred_diff;
632 rd_opt->comp_pred_diff[COMPOUND_REFERENCE] += ctx->comp_pred_diff;
633 rd_opt->comp_pred_diff[REFERENCE_MODE_SELECT] += ctx->hybrid_pred_diff;
634
635 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
636 rd_opt->filter_diff[i] += ctx->best_filter_diff[i];
637 }
638 }
639
vp9_setup_src_planes(MACROBLOCK * x,const YV12_BUFFER_CONFIG * src,int mi_row,int mi_col)640 void vp9_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
641 int mi_row, int mi_col) {
642 uint8_t *const buffers[3] = {src->y_buffer, src->u_buffer, src->v_buffer };
643 const int strides[3] = {src->y_stride, src->uv_stride, src->uv_stride };
644 int i;
645
646 // Set current frame pointer.
647 x->e_mbd.cur_buf = src;
648
649 for (i = 0; i < MAX_MB_PLANE; i++)
650 setup_pred_plane(&x->plane[i].src, buffers[i], strides[i], mi_row, mi_col,
651 NULL, x->e_mbd.plane[i].subsampling_x,
652 x->e_mbd.plane[i].subsampling_y);
653 }
654
set_mode_info_seg_skip(MACROBLOCK * x,TX_MODE tx_mode,int * rate,int64_t * dist,BLOCK_SIZE bsize)655 static void set_mode_info_seg_skip(MACROBLOCK *x, TX_MODE tx_mode, int *rate,
656 int64_t *dist, BLOCK_SIZE bsize) {
657 MACROBLOCKD *const xd = &x->e_mbd;
658 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
659 INTERP_FILTER filter_ref;
660
661 if (xd->up_available)
662 filter_ref = xd->mi[-xd->mi_stride]->mbmi.interp_filter;
663 else if (xd->left_available)
664 filter_ref = xd->mi[-1]->mbmi.interp_filter;
665 else
666 filter_ref = EIGHTTAP;
667
668 mbmi->sb_type = bsize;
669 mbmi->mode = ZEROMV;
670 mbmi->tx_size = MIN(max_txsize_lookup[bsize],
671 tx_mode_to_biggest_tx_size[tx_mode]);
672 mbmi->skip = 1;
673 mbmi->uv_mode = DC_PRED;
674 mbmi->ref_frame[0] = LAST_FRAME;
675 mbmi->ref_frame[1] = NONE;
676 mbmi->mv[0].as_int = 0;
677 mbmi->interp_filter = filter_ref;
678
679 xd->mi[0]->bmi[0].as_mv[0].as_int = 0;
680 x->skip = 1;
681
682 *rate = 0;
683 *dist = 0;
684 }
685
rd_pick_sb_modes(VP9_COMP * cpi,const TileInfo * const tile,int mi_row,int mi_col,int * totalrate,int64_t * totaldist,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx,int64_t best_rd,int block)686 static void rd_pick_sb_modes(VP9_COMP *cpi, const TileInfo *const tile,
687 int mi_row, int mi_col,
688 int *totalrate, int64_t *totaldist,
689 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
690 int64_t best_rd, int block) {
691 VP9_COMMON *const cm = &cpi->common;
692 MACROBLOCK *const x = &cpi->mb;
693 MACROBLOCKD *const xd = &x->e_mbd;
694 MB_MODE_INFO *mbmi;
695 struct macroblock_plane *const p = x->plane;
696 struct macroblockd_plane *const pd = xd->plane;
697 const AQ_MODE aq_mode = cpi->oxcf.aq_mode;
698 int i, orig_rdmult;
699 double rdmult_ratio;
700
701 vp9_clear_system_state();
702 rdmult_ratio = 1.0; // avoid uninitialized warnings
703
704 // Use the lower precision, but faster, 32x32 fdct for mode selection.
705 x->use_lp32x32fdct = 1;
706
707 // TODO(JBB): Most other places in the code instead of calling the function
708 // and then checking if its not the first 8x8 we put the check in the
709 // calling function. Do that here.
710 if (bsize < BLOCK_8X8) {
711 // When ab_index = 0 all sub-blocks are handled, so for ab_index != 0
712 // there is nothing to be done.
713 if (block != 0) {
714 *totalrate = 0;
715 *totaldist = 0;
716 return;
717 }
718 }
719
720 set_offsets(cpi, tile, mi_row, mi_col, bsize);
721 mbmi = &xd->mi[0]->mbmi;
722 mbmi->sb_type = bsize;
723
724 for (i = 0; i < MAX_MB_PLANE; ++i) {
725 p[i].coeff = ctx->coeff_pbuf[i][0];
726 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
727 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
728 p[i].eobs = ctx->eobs_pbuf[i][0];
729 }
730 ctx->is_coded = 0;
731 x->skip_recode = 0;
732
733 // Set to zero to make sure we do not use the previous encoded frame stats
734 mbmi->skip = 0;
735
736 x->source_variance = get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
737
738 // Save rdmult before it might be changed, so it can be restored later.
739 orig_rdmult = x->rdmult;
740
741 if (aq_mode == VARIANCE_AQ) {
742 const int energy = bsize <= BLOCK_16X16 ? x->mb_energy
743 : vp9_block_energy(cpi, x, bsize);
744 if (cm->frame_type == KEY_FRAME ||
745 cpi->refresh_alt_ref_frame ||
746 (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
747 mbmi->segment_id = vp9_vaq_segment_id(energy);
748 } else {
749 const uint8_t *const map = cm->seg.update_map ? cpi->segmentation_map
750 : cm->last_frame_seg_map;
751 mbmi->segment_id = vp9_get_segment_id(cm, map, bsize, mi_row, mi_col);
752 }
753
754 rdmult_ratio = vp9_vaq_rdmult_ratio(energy);
755 vp9_init_plane_quantizers(cpi, x);
756 vp9_clear_system_state();
757 x->rdmult = (int)round(x->rdmult * rdmult_ratio);
758 } else if (aq_mode == COMPLEXITY_AQ) {
759 const int mi_offset = mi_row * cm->mi_cols + mi_col;
760 unsigned char complexity = cpi->complexity_map[mi_offset];
761 const int is_edge = (mi_row <= 1) || (mi_row >= (cm->mi_rows - 2)) ||
762 (mi_col <= 1) || (mi_col >= (cm->mi_cols - 2));
763 if (!is_edge && (complexity > 128))
764 x->rdmult += ((x->rdmult * (complexity - 128)) / 256);
765 } else if (aq_mode == CYCLIC_REFRESH_AQ) {
766 const uint8_t *const map = cm->seg.update_map ? cpi->segmentation_map
767 : cm->last_frame_seg_map;
768 // If segment 1, use rdmult for that segment.
769 if (vp9_get_segment_id(cm, map, bsize, mi_row, mi_col))
770 x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
771 }
772
773 // Find best coding mode & reconstruct the MB so it is available
774 // as a predictor for MBs that follow in the SB
775 if (frame_is_intra_only(cm)) {
776 vp9_rd_pick_intra_mode_sb(cpi, x, totalrate, totaldist, bsize, ctx,
777 best_rd);
778 } else {
779 if (bsize >= BLOCK_8X8) {
780 if (vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
781 vp9_rd_pick_inter_mode_sb_seg_skip(cpi, x, totalrate, totaldist, bsize,
782 ctx, best_rd);
783 else
784 vp9_rd_pick_inter_mode_sb(cpi, x, tile, mi_row, mi_col,
785 totalrate, totaldist, bsize, ctx, best_rd);
786 } else {
787 vp9_rd_pick_inter_mode_sub8x8(cpi, x, tile, mi_row, mi_col, totalrate,
788 totaldist, bsize, ctx, best_rd);
789 }
790 }
791
792 x->rdmult = orig_rdmult;
793
794 if (aq_mode == VARIANCE_AQ && *totalrate != INT_MAX) {
795 vp9_clear_system_state();
796 *totalrate = (int)round(*totalrate * rdmult_ratio);
797 }
798 }
799
update_stats(VP9_COMMON * cm,const MACROBLOCK * x)800 static void update_stats(VP9_COMMON *cm, const MACROBLOCK *x) {
801 const MACROBLOCKD *const xd = &x->e_mbd;
802 const MODE_INFO *const mi = xd->mi[0];
803 const MB_MODE_INFO *const mbmi = &mi->mbmi;
804
805 if (!frame_is_intra_only(cm)) {
806 const int seg_ref_active = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
807 SEG_LVL_REF_FRAME);
808 if (!seg_ref_active) {
809 FRAME_COUNTS *const counts = &cm->counts;
810 const int inter_block = is_inter_block(mbmi);
811
812 counts->intra_inter[vp9_get_intra_inter_context(xd)][inter_block]++;
813
814 // If the segment reference feature is enabled we have only a single
815 // reference frame allowed for the segment so exclude it from
816 // the reference frame counts used to work out probabilities.
817 if (inter_block) {
818 const MV_REFERENCE_FRAME ref0 = mbmi->ref_frame[0];
819
820 if (cm->reference_mode == REFERENCE_MODE_SELECT)
821 counts->comp_inter[vp9_get_reference_mode_context(cm, xd)]
822 [has_second_ref(mbmi)]++;
823
824 if (has_second_ref(mbmi)) {
825 counts->comp_ref[vp9_get_pred_context_comp_ref_p(cm, xd)]
826 [ref0 == GOLDEN_FRAME]++;
827 } else {
828 counts->single_ref[vp9_get_pred_context_single_ref_p1(xd)][0]
829 [ref0 != LAST_FRAME]++;
830 if (ref0 != LAST_FRAME)
831 counts->single_ref[vp9_get_pred_context_single_ref_p2(xd)][1]
832 [ref0 != GOLDEN_FRAME]++;
833 }
834 }
835 }
836 }
837 }
838
restore_context(VP9_COMP * cpi,int mi_row,int mi_col,ENTROPY_CONTEXT a[16* MAX_MB_PLANE],ENTROPY_CONTEXT l[16* MAX_MB_PLANE],PARTITION_CONTEXT sa[8],PARTITION_CONTEXT sl[8],BLOCK_SIZE bsize)839 static void restore_context(VP9_COMP *cpi, int mi_row, int mi_col,
840 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
841 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
842 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
843 BLOCK_SIZE bsize) {
844 MACROBLOCK *const x = &cpi->mb;
845 MACROBLOCKD *const xd = &x->e_mbd;
846 int p;
847 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
848 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
849 int mi_width = num_8x8_blocks_wide_lookup[bsize];
850 int mi_height = num_8x8_blocks_high_lookup[bsize];
851 for (p = 0; p < MAX_MB_PLANE; p++) {
852 vpx_memcpy(
853 xd->above_context[p] + ((mi_col * 2) >> xd->plane[p].subsampling_x),
854 a + num_4x4_blocks_wide * p,
855 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
856 xd->plane[p].subsampling_x);
857 vpx_memcpy(
858 xd->left_context[p]
859 + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
860 l + num_4x4_blocks_high * p,
861 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
862 xd->plane[p].subsampling_y);
863 }
864 vpx_memcpy(xd->above_seg_context + mi_col, sa,
865 sizeof(*xd->above_seg_context) * mi_width);
866 vpx_memcpy(xd->left_seg_context + (mi_row & MI_MASK), sl,
867 sizeof(xd->left_seg_context[0]) * mi_height);
868 }
869
save_context(VP9_COMP * cpi,int mi_row,int mi_col,ENTROPY_CONTEXT a[16* MAX_MB_PLANE],ENTROPY_CONTEXT l[16* MAX_MB_PLANE],PARTITION_CONTEXT sa[8],PARTITION_CONTEXT sl[8],BLOCK_SIZE bsize)870 static void save_context(VP9_COMP *cpi, int mi_row, int mi_col,
871 ENTROPY_CONTEXT a[16 * MAX_MB_PLANE],
872 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE],
873 PARTITION_CONTEXT sa[8], PARTITION_CONTEXT sl[8],
874 BLOCK_SIZE bsize) {
875 const MACROBLOCK *const x = &cpi->mb;
876 const MACROBLOCKD *const xd = &x->e_mbd;
877 int p;
878 const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize];
879 const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize];
880 int mi_width = num_8x8_blocks_wide_lookup[bsize];
881 int mi_height = num_8x8_blocks_high_lookup[bsize];
882
883 // buffer the above/left context information of the block in search.
884 for (p = 0; p < MAX_MB_PLANE; ++p) {
885 vpx_memcpy(
886 a + num_4x4_blocks_wide * p,
887 xd->above_context[p] + (mi_col * 2 >> xd->plane[p].subsampling_x),
888 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_wide) >>
889 xd->plane[p].subsampling_x);
890 vpx_memcpy(
891 l + num_4x4_blocks_high * p,
892 xd->left_context[p]
893 + ((mi_row & MI_MASK) * 2 >> xd->plane[p].subsampling_y),
894 (sizeof(ENTROPY_CONTEXT) * num_4x4_blocks_high) >>
895 xd->plane[p].subsampling_y);
896 }
897 vpx_memcpy(sa, xd->above_seg_context + mi_col,
898 sizeof(*xd->above_seg_context) * mi_width);
899 vpx_memcpy(sl, xd->left_seg_context + (mi_row & MI_MASK),
900 sizeof(xd->left_seg_context[0]) * mi_height);
901 }
902
encode_b(VP9_COMP * cpi,const TileInfo * const tile,TOKENEXTRA ** tp,int mi_row,int mi_col,int output_enabled,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)903 static void encode_b(VP9_COMP *cpi, const TileInfo *const tile,
904 TOKENEXTRA **tp, int mi_row, int mi_col,
905 int output_enabled, BLOCK_SIZE bsize,
906 PICK_MODE_CONTEXT *ctx) {
907 set_offsets(cpi, tile, mi_row, mi_col, bsize);
908 update_state(cpi, ctx, mi_row, mi_col, bsize, output_enabled);
909 encode_superblock(cpi, tp, output_enabled, mi_row, mi_col, bsize, ctx);
910
911 if (output_enabled) {
912 update_stats(&cpi->common, &cpi->mb);
913
914 (*tp)->token = EOSB_TOKEN;
915 (*tp)++;
916 }
917 }
918
encode_sb(VP9_COMP * cpi,const TileInfo * const tile,TOKENEXTRA ** tp,int mi_row,int mi_col,int output_enabled,BLOCK_SIZE bsize,PC_TREE * pc_tree)919 static void encode_sb(VP9_COMP *cpi, const TileInfo *const tile,
920 TOKENEXTRA **tp, int mi_row, int mi_col,
921 int output_enabled, BLOCK_SIZE bsize,
922 PC_TREE *pc_tree) {
923 VP9_COMMON *const cm = &cpi->common;
924 MACROBLOCK *const x = &cpi->mb;
925 MACROBLOCKD *const xd = &x->e_mbd;
926
927 const int bsl = b_width_log2(bsize), hbs = (1 << bsl) / 4;
928 int ctx;
929 PARTITION_TYPE partition;
930 BLOCK_SIZE subsize = bsize;
931
932 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
933 return;
934
935 if (bsize >= BLOCK_8X8) {
936 ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
937 subsize = get_subsize(bsize, pc_tree->partitioning);
938 } else {
939 ctx = 0;
940 subsize = BLOCK_4X4;
941 }
942
943 partition = partition_lookup[bsl][subsize];
944 if (output_enabled && bsize != BLOCK_4X4)
945 cm->counts.partition[ctx][partition]++;
946
947 switch (partition) {
948 case PARTITION_NONE:
949 encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize,
950 &pc_tree->none);
951 break;
952 case PARTITION_VERT:
953 encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize,
954 &pc_tree->vertical[0]);
955 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
956 encode_b(cpi, tile, tp, mi_row, mi_col + hbs, output_enabled, subsize,
957 &pc_tree->vertical[1]);
958 }
959 break;
960 case PARTITION_HORZ:
961 encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize,
962 &pc_tree->horizontal[0]);
963 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
964 encode_b(cpi, tile, tp, mi_row + hbs, mi_col, output_enabled, subsize,
965 &pc_tree->horizontal[1]);
966 }
967 break;
968 case PARTITION_SPLIT:
969 if (bsize == BLOCK_8X8) {
970 encode_b(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize,
971 pc_tree->leaf_split[0]);
972 } else {
973 encode_sb(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize,
974 pc_tree->split[0]);
975 encode_sb(cpi, tile, tp, mi_row, mi_col + hbs, output_enabled, subsize,
976 pc_tree->split[1]);
977 encode_sb(cpi, tile, tp, mi_row + hbs, mi_col, output_enabled, subsize,
978 pc_tree->split[2]);
979 encode_sb(cpi, tile, tp, mi_row + hbs, mi_col + hbs, output_enabled,
980 subsize, pc_tree->split[3]);
981 }
982 break;
983 default:
984 assert("Invalid partition type.");
985 break;
986 }
987
988 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
989 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
990 }
991
992 // Check to see if the given partition size is allowed for a specified number
993 // of 8x8 block rows and columns remaining in the image.
994 // If not then return the largest allowed partition size
find_partition_size(BLOCK_SIZE bsize,int rows_left,int cols_left,int * bh,int * bw)995 static BLOCK_SIZE find_partition_size(BLOCK_SIZE bsize,
996 int rows_left, int cols_left,
997 int *bh, int *bw) {
998 if (rows_left <= 0 || cols_left <= 0) {
999 return MIN(bsize, BLOCK_8X8);
1000 } else {
1001 for (; bsize > 0; bsize -= 3) {
1002 *bh = num_8x8_blocks_high_lookup[bsize];
1003 *bw = num_8x8_blocks_wide_lookup[bsize];
1004 if ((*bh <= rows_left) && (*bw <= cols_left)) {
1005 break;
1006 }
1007 }
1008 }
1009 return bsize;
1010 }
1011
set_partial_b64x64_partition(MODE_INFO * mi,int mis,int bh_in,int bw_in,int row8x8_remaining,int col8x8_remaining,BLOCK_SIZE bsize,MODE_INFO ** mi_8x8)1012 static void set_partial_b64x64_partition(MODE_INFO *mi, int mis,
1013 int bh_in, int bw_in, int row8x8_remaining, int col8x8_remaining,
1014 BLOCK_SIZE bsize, MODE_INFO **mi_8x8) {
1015 int bh = bh_in;
1016 int r, c;
1017 for (r = 0; r < MI_BLOCK_SIZE; r += bh) {
1018 int bw = bw_in;
1019 for (c = 0; c < MI_BLOCK_SIZE; c += bw) {
1020 const int index = r * mis + c;
1021 mi_8x8[index] = mi + index;
1022 mi_8x8[index]->mbmi.sb_type = find_partition_size(bsize,
1023 row8x8_remaining - r, col8x8_remaining - c, &bh, &bw);
1024 }
1025 }
1026 }
1027
1028 // This function attempts to set all mode info entries in a given SB64
1029 // to the same block partition size.
1030 // However, at the bottom and right borders of the image the requested size
1031 // may not be allowed in which case this code attempts to choose the largest
1032 // allowable partition.
set_fixed_partitioning(VP9_COMP * cpi,const TileInfo * const tile,MODE_INFO ** mi_8x8,int mi_row,int mi_col,BLOCK_SIZE bsize)1033 static void set_fixed_partitioning(VP9_COMP *cpi, const TileInfo *const tile,
1034 MODE_INFO **mi_8x8, int mi_row, int mi_col,
1035 BLOCK_SIZE bsize) {
1036 VP9_COMMON *const cm = &cpi->common;
1037 const int mis = cm->mi_stride;
1038 const int row8x8_remaining = tile->mi_row_end - mi_row;
1039 const int col8x8_remaining = tile->mi_col_end - mi_col;
1040 int block_row, block_col;
1041 MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
1042 int bh = num_8x8_blocks_high_lookup[bsize];
1043 int bw = num_8x8_blocks_wide_lookup[bsize];
1044
1045 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
1046
1047 // Apply the requested partition size to the SB64 if it is all "in image"
1048 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
1049 (row8x8_remaining >= MI_BLOCK_SIZE)) {
1050 for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
1051 for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
1052 int index = block_row * mis + block_col;
1053 mi_8x8[index] = mi_upper_left + index;
1054 mi_8x8[index]->mbmi.sb_type = bsize;
1055 }
1056 }
1057 } else {
1058 // Else this is a partial SB64.
1059 set_partial_b64x64_partition(mi_upper_left, mis, bh, bw, row8x8_remaining,
1060 col8x8_remaining, bsize, mi_8x8);
1061 }
1062 }
1063
copy_partitioning(VP9_COMMON * cm,MODE_INFO ** mi_8x8,MODE_INFO ** prev_mi_8x8)1064 static void copy_partitioning(VP9_COMMON *cm, MODE_INFO **mi_8x8,
1065 MODE_INFO **prev_mi_8x8) {
1066 const int mis = cm->mi_stride;
1067 int block_row, block_col;
1068
1069 for (block_row = 0; block_row < 8; ++block_row) {
1070 for (block_col = 0; block_col < 8; ++block_col) {
1071 MODE_INFO *const prev_mi = prev_mi_8x8[block_row * mis + block_col];
1072 const BLOCK_SIZE sb_type = prev_mi ? prev_mi->mbmi.sb_type : 0;
1073
1074 if (prev_mi) {
1075 const ptrdiff_t offset = prev_mi - cm->prev_mi;
1076 mi_8x8[block_row * mis + block_col] = cm->mi + offset;
1077 mi_8x8[block_row * mis + block_col]->mbmi.sb_type = sb_type;
1078 }
1079 }
1080 }
1081 }
1082
constrain_copy_partitioning(VP9_COMP * const cpi,const TileInfo * const tile,MODE_INFO ** mi_8x8,MODE_INFO ** prev_mi_8x8,int mi_row,int mi_col,BLOCK_SIZE bsize)1083 static void constrain_copy_partitioning(VP9_COMP *const cpi,
1084 const TileInfo *const tile,
1085 MODE_INFO **mi_8x8,
1086 MODE_INFO **prev_mi_8x8,
1087 int mi_row, int mi_col,
1088 BLOCK_SIZE bsize) {
1089 VP9_COMMON *const cm = &cpi->common;
1090 const int mis = cm->mi_stride;
1091 const int row8x8_remaining = tile->mi_row_end - mi_row;
1092 const int col8x8_remaining = tile->mi_col_end - mi_col;
1093 MODE_INFO *const mi_upper_left = cm->mi + mi_row * mis + mi_col;
1094 const int bh = num_8x8_blocks_high_lookup[bsize];
1095 const int bw = num_8x8_blocks_wide_lookup[bsize];
1096 int block_row, block_col;
1097
1098 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
1099
1100 // If the SB64 if it is all "in image".
1101 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
1102 (row8x8_remaining >= MI_BLOCK_SIZE)) {
1103 for (block_row = 0; block_row < MI_BLOCK_SIZE; block_row += bh) {
1104 for (block_col = 0; block_col < MI_BLOCK_SIZE; block_col += bw) {
1105 const int index = block_row * mis + block_col;
1106 MODE_INFO *prev_mi = prev_mi_8x8[index];
1107 const BLOCK_SIZE sb_type = prev_mi ? prev_mi->mbmi.sb_type : 0;
1108 // Use previous partition if block size is not larger than bsize.
1109 if (prev_mi && sb_type <= bsize) {
1110 int block_row2, block_col2;
1111 for (block_row2 = 0; block_row2 < bh; ++block_row2) {
1112 for (block_col2 = 0; block_col2 < bw; ++block_col2) {
1113 const int index2 = (block_row + block_row2) * mis +
1114 block_col + block_col2;
1115 prev_mi = prev_mi_8x8[index2];
1116 if (prev_mi) {
1117 const ptrdiff_t offset = prev_mi - cm->prev_mi;
1118 mi_8x8[index2] = cm->mi + offset;
1119 mi_8x8[index2]->mbmi.sb_type = prev_mi->mbmi.sb_type;
1120 }
1121 }
1122 }
1123 } else {
1124 // Otherwise, use fixed partition of size bsize.
1125 mi_8x8[index] = mi_upper_left + index;
1126 mi_8x8[index]->mbmi.sb_type = bsize;
1127 }
1128 }
1129 }
1130 } else {
1131 // Else this is a partial SB64, copy previous partition.
1132 copy_partitioning(cm, mi_8x8, prev_mi_8x8);
1133 }
1134 }
1135
1136 const struct {
1137 int row;
1138 int col;
1139 } coord_lookup[16] = {
1140 // 32x32 index = 0
1141 {0, 0}, {0, 2}, {2, 0}, {2, 2},
1142 // 32x32 index = 1
1143 {0, 4}, {0, 6}, {2, 4}, {2, 6},
1144 // 32x32 index = 2
1145 {4, 0}, {4, 2}, {6, 0}, {6, 2},
1146 // 32x32 index = 3
1147 {4, 4}, {4, 6}, {6, 4}, {6, 6},
1148 };
1149
set_source_var_based_partition(VP9_COMP * cpi,const TileInfo * const tile,MODE_INFO ** mi_8x8,int mi_row,int mi_col)1150 static void set_source_var_based_partition(VP9_COMP *cpi,
1151 const TileInfo *const tile,
1152 MODE_INFO **mi_8x8,
1153 int mi_row, int mi_col) {
1154 VP9_COMMON *const cm = &cpi->common;
1155 MACROBLOCK *const x = &cpi->mb;
1156 const int mis = cm->mi_stride;
1157 const int row8x8_remaining = tile->mi_row_end - mi_row;
1158 const int col8x8_remaining = tile->mi_col_end - mi_col;
1159 MODE_INFO *mi_upper_left = cm->mi + mi_row * mis + mi_col;
1160
1161 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
1162
1163 assert((row8x8_remaining > 0) && (col8x8_remaining > 0));
1164
1165 // In-image SB64
1166 if ((col8x8_remaining >= MI_BLOCK_SIZE) &&
1167 (row8x8_remaining >= MI_BLOCK_SIZE)) {
1168 int i, j;
1169 int index;
1170 diff d32[4];
1171 const int offset = (mi_row >> 1) * cm->mb_cols + (mi_col >> 1);
1172 int is_larger_better = 0;
1173 int use32x32 = 0;
1174 unsigned int thr = cpi->source_var_thresh;
1175
1176 vpx_memset(d32, 0, 4 * sizeof(diff));
1177
1178 for (i = 0; i < 4; i++) {
1179 diff *d16[4];
1180
1181 for (j = 0; j < 4; j++) {
1182 int b_mi_row = coord_lookup[i * 4 + j].row;
1183 int b_mi_col = coord_lookup[i * 4 + j].col;
1184 int boffset = b_mi_row / 2 * cm->mb_cols +
1185 b_mi_col / 2;
1186
1187 d16[j] = cpi->source_diff_var + offset + boffset;
1188
1189 index = b_mi_row * mis + b_mi_col;
1190 mi_8x8[index] = mi_upper_left + index;
1191 mi_8x8[index]->mbmi.sb_type = BLOCK_16X16;
1192
1193 // TODO(yunqingwang): If d16[j].var is very large, use 8x8 partition
1194 // size to further improve quality.
1195 }
1196
1197 is_larger_better = (d16[0]->var < thr) && (d16[1]->var < thr) &&
1198 (d16[2]->var < thr) && (d16[3]->var < thr);
1199
1200 // Use 32x32 partition
1201 if (is_larger_better) {
1202 use32x32 += 1;
1203
1204 for (j = 0; j < 4; j++) {
1205 d32[i].sse += d16[j]->sse;
1206 d32[i].sum += d16[j]->sum;
1207 }
1208
1209 d32[i].var = d32[i].sse - (((int64_t)d32[i].sum * d32[i].sum) >> 10);
1210
1211 index = coord_lookup[i*4].row * mis + coord_lookup[i*4].col;
1212 mi_8x8[index] = mi_upper_left + index;
1213 mi_8x8[index]->mbmi.sb_type = BLOCK_32X32;
1214 }
1215 }
1216
1217 if (use32x32 == 4) {
1218 thr <<= 1;
1219 is_larger_better = (d32[0].var < thr) && (d32[1].var < thr) &&
1220 (d32[2].var < thr) && (d32[3].var < thr);
1221
1222 // Use 64x64 partition
1223 if (is_larger_better) {
1224 mi_8x8[0] = mi_upper_left;
1225 mi_8x8[0]->mbmi.sb_type = BLOCK_64X64;
1226 }
1227 }
1228 } else { // partial in-image SB64
1229 int bh = num_8x8_blocks_high_lookup[BLOCK_16X16];
1230 int bw = num_8x8_blocks_wide_lookup[BLOCK_16X16];
1231 set_partial_b64x64_partition(mi_upper_left, mis, bh, bw,
1232 row8x8_remaining, col8x8_remaining, BLOCK_16X16, mi_8x8);
1233 }
1234 }
1235
is_background(VP9_COMP * cpi,const TileInfo * const tile,int mi_row,int mi_col)1236 static int is_background(VP9_COMP *cpi, const TileInfo *const tile,
1237 int mi_row, int mi_col) {
1238 MACROBLOCK *x = &cpi->mb;
1239 uint8_t *src, *pre;
1240 int src_stride, pre_stride;
1241
1242 const int row8x8_remaining = tile->mi_row_end - mi_row;
1243 const int col8x8_remaining = tile->mi_col_end - mi_col;
1244
1245 int this_sad = 0;
1246 int threshold = 0;
1247
1248 // This assumes the input source frames are of the same dimension.
1249 src_stride = cpi->Source->y_stride;
1250 src = cpi->Source->y_buffer + (mi_row * MI_SIZE) * src_stride +
1251 (mi_col * MI_SIZE);
1252 pre_stride = cpi->Last_Source->y_stride;
1253 pre = cpi->Last_Source->y_buffer + (mi_row * MI_SIZE) * pre_stride +
1254 (mi_col * MI_SIZE);
1255
1256 if (row8x8_remaining >= MI_BLOCK_SIZE &&
1257 col8x8_remaining >= MI_BLOCK_SIZE) {
1258 this_sad = cpi->fn_ptr[BLOCK_64X64].sdf(src, src_stride,
1259 pre, pre_stride);
1260 threshold = (1 << 12);
1261 } else {
1262 int r, c;
1263 for (r = 0; r < row8x8_remaining; r += 2)
1264 for (c = 0; c < col8x8_remaining; c += 2)
1265 this_sad += cpi->fn_ptr[BLOCK_16X16].sdf(src, src_stride,
1266 pre, pre_stride);
1267 threshold = (row8x8_remaining * col8x8_remaining) << 6;
1268 }
1269
1270 x->in_static_area = (this_sad < 2 * threshold);
1271 return x->in_static_area;
1272 }
1273
sb_has_motion(const VP9_COMMON * cm,MODE_INFO ** prev_mi_8x8,const int motion_thresh)1274 static int sb_has_motion(const VP9_COMMON *cm, MODE_INFO **prev_mi_8x8,
1275 const int motion_thresh) {
1276 const int mis = cm->mi_stride;
1277 int block_row, block_col;
1278
1279 if (cm->prev_mi) {
1280 for (block_row = 0; block_row < 8; ++block_row) {
1281 for (block_col = 0; block_col < 8; ++block_col) {
1282 const MODE_INFO *prev_mi = prev_mi_8x8[block_row * mis + block_col];
1283 if (prev_mi) {
1284 if (abs(prev_mi->mbmi.mv[0].as_mv.row) > motion_thresh ||
1285 abs(prev_mi->mbmi.mv[0].as_mv.col) > motion_thresh)
1286 return 1;
1287 }
1288 }
1289 }
1290 }
1291 return 0;
1292 }
1293
update_state_rt(VP9_COMP * cpi,PICK_MODE_CONTEXT * ctx,int mi_row,int mi_col,int bsize)1294 static void update_state_rt(VP9_COMP *cpi, PICK_MODE_CONTEXT *ctx,
1295 int mi_row, int mi_col, int bsize) {
1296 VP9_COMMON *const cm = &cpi->common;
1297 MACROBLOCK *const x = &cpi->mb;
1298 MACROBLOCKD *const xd = &x->e_mbd;
1299 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
1300 const struct segmentation *const seg = &cm->seg;
1301
1302 *(xd->mi[0]) = ctx->mic;
1303
1304 // For in frame adaptive Q, check for reseting the segment_id and updating
1305 // the cyclic refresh map.
1306 if ((cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) && seg->enabled) {
1307 vp9_cyclic_refresh_update_segment(cpi, &xd->mi[0]->mbmi,
1308 mi_row, mi_col, bsize, 1);
1309 vp9_init_plane_quantizers(cpi, x);
1310 }
1311
1312 if (is_inter_block(mbmi)) {
1313 vp9_update_mv_count(cm, xd);
1314
1315 if (cm->interp_filter == SWITCHABLE) {
1316 const int pred_ctx = vp9_get_pred_context_switchable_interp(xd);
1317 ++cm->counts.switchable_interp[pred_ctx][mbmi->interp_filter];
1318 }
1319 }
1320
1321 x->skip = ctx->skip;
1322 x->skip_txfm[0] = mbmi->segment_id ? 0 : ctx->skip_txfm[0];
1323 }
1324
encode_b_rt(VP9_COMP * cpi,const TileInfo * const tile,TOKENEXTRA ** tp,int mi_row,int mi_col,int output_enabled,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)1325 static void encode_b_rt(VP9_COMP *cpi, const TileInfo *const tile,
1326 TOKENEXTRA **tp, int mi_row, int mi_col,
1327 int output_enabled, BLOCK_SIZE bsize,
1328 PICK_MODE_CONTEXT *ctx) {
1329 set_offsets(cpi, tile, mi_row, mi_col, bsize);
1330 update_state_rt(cpi, ctx, mi_row, mi_col, bsize);
1331
1332 #if CONFIG_VP9_TEMPORAL_DENOISING
1333 if (cpi->oxcf.noise_sensitivity > 0 && output_enabled) {
1334 vp9_denoiser_denoise(&cpi->denoiser, &cpi->mb, mi_row, mi_col,
1335 MAX(BLOCK_8X8, bsize), ctx);
1336 }
1337 #endif
1338
1339 encode_superblock(cpi, tp, output_enabled, mi_row, mi_col, bsize, ctx);
1340 update_stats(&cpi->common, &cpi->mb);
1341
1342 (*tp)->token = EOSB_TOKEN;
1343 (*tp)++;
1344 }
1345
encode_sb_rt(VP9_COMP * cpi,const TileInfo * const tile,TOKENEXTRA ** tp,int mi_row,int mi_col,int output_enabled,BLOCK_SIZE bsize,PC_TREE * pc_tree)1346 static void encode_sb_rt(VP9_COMP *cpi, const TileInfo *const tile,
1347 TOKENEXTRA **tp, int mi_row, int mi_col,
1348 int output_enabled, BLOCK_SIZE bsize,
1349 PC_TREE *pc_tree) {
1350 VP9_COMMON *const cm = &cpi->common;
1351 MACROBLOCK *const x = &cpi->mb;
1352 MACROBLOCKD *const xd = &x->e_mbd;
1353
1354 const int bsl = b_width_log2(bsize), hbs = (1 << bsl) / 4;
1355 int ctx;
1356 PARTITION_TYPE partition;
1357 BLOCK_SIZE subsize;
1358
1359 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1360 return;
1361
1362 if (bsize >= BLOCK_8X8) {
1363 const int idx_str = xd->mi_stride * mi_row + mi_col;
1364 MODE_INFO ** mi_8x8 = cm->mi_grid_visible + idx_str;
1365 ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1366 subsize = mi_8x8[0]->mbmi.sb_type;
1367 } else {
1368 ctx = 0;
1369 subsize = BLOCK_4X4;
1370 }
1371
1372 partition = partition_lookup[bsl][subsize];
1373 if (output_enabled && bsize != BLOCK_4X4)
1374 cm->counts.partition[ctx][partition]++;
1375
1376 switch (partition) {
1377 case PARTITION_NONE:
1378 encode_b_rt(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize,
1379 &pc_tree->none);
1380 break;
1381 case PARTITION_VERT:
1382 encode_b_rt(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize,
1383 &pc_tree->vertical[0]);
1384 if (mi_col + hbs < cm->mi_cols && bsize > BLOCK_8X8) {
1385 encode_b_rt(cpi, tile, tp, mi_row, mi_col + hbs, output_enabled,
1386 subsize, &pc_tree->vertical[1]);
1387 }
1388 break;
1389 case PARTITION_HORZ:
1390 encode_b_rt(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize,
1391 &pc_tree->horizontal[0]);
1392 if (mi_row + hbs < cm->mi_rows && bsize > BLOCK_8X8) {
1393 encode_b_rt(cpi, tile, tp, mi_row + hbs, mi_col, output_enabled,
1394 subsize, &pc_tree->horizontal[1]);
1395 }
1396 break;
1397 case PARTITION_SPLIT:
1398 subsize = get_subsize(bsize, PARTITION_SPLIT);
1399 encode_sb_rt(cpi, tile, tp, mi_row, mi_col, output_enabled, subsize,
1400 pc_tree->split[0]);
1401 encode_sb_rt(cpi, tile, tp, mi_row, mi_col + hbs, output_enabled,
1402 subsize, pc_tree->split[1]);
1403 encode_sb_rt(cpi, tile, tp, mi_row + hbs, mi_col, output_enabled,
1404 subsize, pc_tree->split[2]);
1405 encode_sb_rt(cpi, tile, tp, mi_row + hbs, mi_col + hbs, output_enabled,
1406 subsize, pc_tree->split[3]);
1407 break;
1408 default:
1409 assert("Invalid partition type.");
1410 break;
1411 }
1412
1413 if (partition != PARTITION_SPLIT || bsize == BLOCK_8X8)
1414 update_partition_context(xd, mi_row, mi_col, subsize, bsize);
1415 }
1416
rd_use_partition(VP9_COMP * cpi,const TileInfo * const tile,MODE_INFO ** mi_8x8,TOKENEXTRA ** tp,int mi_row,int mi_col,BLOCK_SIZE bsize,int * rate,int64_t * dist,int do_recon,PC_TREE * pc_tree)1417 static void rd_use_partition(VP9_COMP *cpi,
1418 const TileInfo *const tile,
1419 MODE_INFO **mi_8x8,
1420 TOKENEXTRA **tp, int mi_row, int mi_col,
1421 BLOCK_SIZE bsize, int *rate, int64_t *dist,
1422 int do_recon, PC_TREE *pc_tree) {
1423 VP9_COMMON *const cm = &cpi->common;
1424 MACROBLOCK *const x = &cpi->mb;
1425 MACROBLOCKD *const xd = &x->e_mbd;
1426 const int mis = cm->mi_stride;
1427 const int bsl = b_width_log2(bsize);
1428 const int mi_step = num_4x4_blocks_wide_lookup[bsize] / 2;
1429 const int bss = (1 << bsl) / 4;
1430 int i, pl;
1431 PARTITION_TYPE partition = PARTITION_NONE;
1432 BLOCK_SIZE subsize;
1433 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1434 PARTITION_CONTEXT sl[8], sa[8];
1435 int last_part_rate = INT_MAX;
1436 int64_t last_part_dist = INT64_MAX;
1437 int64_t last_part_rd = INT64_MAX;
1438 int none_rate = INT_MAX;
1439 int64_t none_dist = INT64_MAX;
1440 int64_t none_rd = INT64_MAX;
1441 int chosen_rate = INT_MAX;
1442 int64_t chosen_dist = INT64_MAX;
1443 int64_t chosen_rd = INT64_MAX;
1444 BLOCK_SIZE sub_subsize = BLOCK_4X4;
1445 int splits_below = 0;
1446 BLOCK_SIZE bs_type = mi_8x8[0]->mbmi.sb_type;
1447 int do_partition_search = 1;
1448 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
1449
1450 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
1451 return;
1452
1453 assert(num_4x4_blocks_wide_lookup[bsize] ==
1454 num_4x4_blocks_high_lookup[bsize]);
1455
1456 partition = partition_lookup[bsl][bs_type];
1457 subsize = get_subsize(bsize, partition);
1458
1459 pc_tree->partitioning = partition;
1460 save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1461
1462 if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode) {
1463 set_offsets(cpi, tile, mi_row, mi_col, bsize);
1464 x->mb_energy = vp9_block_energy(cpi, x, bsize);
1465 }
1466
1467 if (do_partition_search &&
1468 cpi->sf.partition_search_type == SEARCH_PARTITION &&
1469 cpi->sf.adjust_partitioning_from_last_frame) {
1470 // Check if any of the sub blocks are further split.
1471 if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
1472 sub_subsize = get_subsize(subsize, PARTITION_SPLIT);
1473 splits_below = 1;
1474 for (i = 0; i < 4; i++) {
1475 int jj = i >> 1, ii = i & 0x01;
1476 MODE_INFO * this_mi = mi_8x8[jj * bss * mis + ii * bss];
1477 if (this_mi && this_mi->mbmi.sb_type >= sub_subsize) {
1478 splits_below = 0;
1479 }
1480 }
1481 }
1482
1483 // If partition is not none try none unless each of the 4 splits are split
1484 // even further..
1485 if (partition != PARTITION_NONE && !splits_below &&
1486 mi_row + (mi_step >> 1) < cm->mi_rows &&
1487 mi_col + (mi_step >> 1) < cm->mi_cols) {
1488 pc_tree->partitioning = PARTITION_NONE;
1489 rd_pick_sb_modes(cpi, tile, mi_row, mi_col, &none_rate, &none_dist, bsize,
1490 ctx, INT64_MAX, 0);
1491
1492 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
1493
1494 if (none_rate < INT_MAX) {
1495 none_rate += cpi->partition_cost[pl][PARTITION_NONE];
1496 none_rd = RDCOST(x->rdmult, x->rddiv, none_rate, none_dist);
1497 }
1498
1499 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1500 mi_8x8[0]->mbmi.sb_type = bs_type;
1501 pc_tree->partitioning = partition;
1502 }
1503 }
1504
1505 switch (partition) {
1506 case PARTITION_NONE:
1507 rd_pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate,
1508 &last_part_dist, bsize, ctx, INT64_MAX, 0);
1509 break;
1510 case PARTITION_HORZ:
1511 rd_pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate,
1512 &last_part_dist, subsize, &pc_tree->horizontal[0],
1513 INT64_MAX, 0);
1514 if (last_part_rate != INT_MAX &&
1515 bsize >= BLOCK_8X8 && mi_row + (mi_step >> 1) < cm->mi_rows) {
1516 int rt = 0;
1517 int64_t dt = 0;
1518 PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
1519 update_state(cpi, ctx, mi_row, mi_col, subsize, 0);
1520 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize, ctx);
1521 rd_pick_sb_modes(cpi, tile, mi_row + (mi_step >> 1), mi_col, &rt, &dt,
1522 subsize, &pc_tree->horizontal[1], INT64_MAX, 1);
1523 if (rt == INT_MAX || dt == INT64_MAX) {
1524 last_part_rate = INT_MAX;
1525 last_part_dist = INT64_MAX;
1526 break;
1527 }
1528
1529 last_part_rate += rt;
1530 last_part_dist += dt;
1531 }
1532 break;
1533 case PARTITION_VERT:
1534 rd_pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate,
1535 &last_part_dist, subsize, &pc_tree->vertical[0],
1536 INT64_MAX, 0);
1537 if (last_part_rate != INT_MAX &&
1538 bsize >= BLOCK_8X8 && mi_col + (mi_step >> 1) < cm->mi_cols) {
1539 int rt = 0;
1540 int64_t dt = 0;
1541 PICK_MODE_CONTEXT *ctx = &pc_tree->vertical[0];
1542 update_state(cpi, ctx, mi_row, mi_col, subsize, 0);
1543 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize, ctx);
1544 rd_pick_sb_modes(cpi, tile, mi_row, mi_col + (mi_step >> 1), &rt, &dt,
1545 subsize, &pc_tree->vertical[bsize > BLOCK_8X8],
1546 INT64_MAX, 1);
1547 if (rt == INT_MAX || dt == INT64_MAX) {
1548 last_part_rate = INT_MAX;
1549 last_part_dist = INT64_MAX;
1550 break;
1551 }
1552 last_part_rate += rt;
1553 last_part_dist += dt;
1554 }
1555 break;
1556 case PARTITION_SPLIT:
1557 if (bsize == BLOCK_8X8) {
1558 rd_pick_sb_modes(cpi, tile, mi_row, mi_col, &last_part_rate,
1559 &last_part_dist, subsize, pc_tree->leaf_split[0],
1560 INT64_MAX, 0);
1561 break;
1562 }
1563 last_part_rate = 0;
1564 last_part_dist = 0;
1565 for (i = 0; i < 4; i++) {
1566 int x_idx = (i & 1) * (mi_step >> 1);
1567 int y_idx = (i >> 1) * (mi_step >> 1);
1568 int jj = i >> 1, ii = i & 0x01;
1569 int rt;
1570 int64_t dt;
1571
1572 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1573 continue;
1574
1575 rd_use_partition(cpi, tile, mi_8x8 + jj * bss * mis + ii * bss, tp,
1576 mi_row + y_idx, mi_col + x_idx, subsize, &rt, &dt,
1577 i != 3, pc_tree->split[i]);
1578 if (rt == INT_MAX || dt == INT64_MAX) {
1579 last_part_rate = INT_MAX;
1580 last_part_dist = INT64_MAX;
1581 break;
1582 }
1583 last_part_rate += rt;
1584 last_part_dist += dt;
1585 }
1586 break;
1587 default:
1588 assert(0);
1589 break;
1590 }
1591
1592 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
1593 if (last_part_rate < INT_MAX) {
1594 last_part_rate += cpi->partition_cost[pl][partition];
1595 last_part_rd = RDCOST(x->rdmult, x->rddiv, last_part_rate, last_part_dist);
1596 }
1597
1598 if (do_partition_search
1599 && cpi->sf.adjust_partitioning_from_last_frame
1600 && cpi->sf.partition_search_type == SEARCH_PARTITION
1601 && partition != PARTITION_SPLIT && bsize > BLOCK_8X8
1602 && (mi_row + mi_step < cm->mi_rows ||
1603 mi_row + (mi_step >> 1) == cm->mi_rows)
1604 && (mi_col + mi_step < cm->mi_cols ||
1605 mi_col + (mi_step >> 1) == cm->mi_cols)) {
1606 BLOCK_SIZE split_subsize = get_subsize(bsize, PARTITION_SPLIT);
1607 chosen_rate = 0;
1608 chosen_dist = 0;
1609 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1610 pc_tree->partitioning = PARTITION_SPLIT;
1611
1612 // Split partition.
1613 for (i = 0; i < 4; i++) {
1614 int x_idx = (i & 1) * (mi_step >> 1);
1615 int y_idx = (i >> 1) * (mi_step >> 1);
1616 int rt = 0;
1617 int64_t dt = 0;
1618 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1619 PARTITION_CONTEXT sl[8], sa[8];
1620
1621 if ((mi_row + y_idx >= cm->mi_rows) || (mi_col + x_idx >= cm->mi_cols))
1622 continue;
1623
1624 save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1625 pc_tree->split[i]->partitioning = PARTITION_NONE;
1626 rd_pick_sb_modes(cpi, tile, mi_row + y_idx, mi_col + x_idx, &rt, &dt,
1627 split_subsize, &pc_tree->split[i]->none,
1628 INT64_MAX, i);
1629
1630 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1631
1632 if (rt == INT_MAX || dt == INT64_MAX) {
1633 chosen_rate = INT_MAX;
1634 chosen_dist = INT64_MAX;
1635 break;
1636 }
1637
1638 chosen_rate += rt;
1639 chosen_dist += dt;
1640
1641 if (i != 3)
1642 encode_sb(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx, 0,
1643 split_subsize, pc_tree->split[i]);
1644
1645 pl = partition_plane_context(xd, mi_row + y_idx, mi_col + x_idx,
1646 split_subsize);
1647 chosen_rate += cpi->partition_cost[pl][PARTITION_NONE];
1648 }
1649 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
1650 if (chosen_rate < INT_MAX) {
1651 chosen_rate += cpi->partition_cost[pl][PARTITION_SPLIT];
1652 chosen_rd = RDCOST(x->rdmult, x->rddiv, chosen_rate, chosen_dist);
1653 }
1654 }
1655
1656 // If last_part is better set the partitioning to that.
1657 if (last_part_rd < chosen_rd) {
1658 mi_8x8[0]->mbmi.sb_type = bsize;
1659 if (bsize >= BLOCK_8X8)
1660 pc_tree->partitioning = partition;
1661 chosen_rate = last_part_rate;
1662 chosen_dist = last_part_dist;
1663 chosen_rd = last_part_rd;
1664 }
1665 // If none was better set the partitioning to that.
1666 if (none_rd < chosen_rd) {
1667 if (bsize >= BLOCK_8X8)
1668 pc_tree->partitioning = PARTITION_NONE;
1669 chosen_rate = none_rate;
1670 chosen_dist = none_dist;
1671 }
1672
1673 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
1674
1675 // We must have chosen a partitioning and encoding or we'll fail later on.
1676 // No other opportunities for success.
1677 if ( bsize == BLOCK_64X64)
1678 assert(chosen_rate < INT_MAX && chosen_dist < INT64_MAX);
1679
1680 if (do_recon) {
1681 int output_enabled = (bsize == BLOCK_64X64);
1682
1683 // Check the projected output rate for this SB against it's target
1684 // and and if necessary apply a Q delta using segmentation to get
1685 // closer to the target.
1686 if ((cpi->oxcf.aq_mode == COMPLEXITY_AQ) && cm->seg.update_map) {
1687 vp9_select_in_frame_q_segment(cpi, mi_row, mi_col,
1688 output_enabled, chosen_rate);
1689 }
1690
1691 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
1692 vp9_cyclic_refresh_set_rate_and_dist_sb(cpi->cyclic_refresh,
1693 chosen_rate, chosen_dist);
1694 encode_sb(cpi, tile, tp, mi_row, mi_col, output_enabled, bsize,
1695 pc_tree);
1696 }
1697
1698 *rate = chosen_rate;
1699 *dist = chosen_dist;
1700 }
1701
1702 static const BLOCK_SIZE min_partition_size[BLOCK_SIZES] = {
1703 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
1704 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
1705 BLOCK_8X8, BLOCK_8X8, BLOCK_8X8,
1706 BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
1707 BLOCK_16X16
1708 };
1709
1710 static const BLOCK_SIZE max_partition_size[BLOCK_SIZES] = {
1711 BLOCK_8X8, BLOCK_16X16, BLOCK_16X16,
1712 BLOCK_16X16, BLOCK_32X32, BLOCK_32X32,
1713 BLOCK_32X32, BLOCK_64X64, BLOCK_64X64,
1714 BLOCK_64X64, BLOCK_64X64, BLOCK_64X64,
1715 BLOCK_64X64
1716 };
1717
1718 // Look at all the mode_info entries for blocks that are part of this
1719 // partition and find the min and max values for sb_type.
1720 // At the moment this is designed to work on a 64x64 SB but could be
1721 // adjusted to use a size parameter.
1722 //
1723 // The min and max are assumed to have been initialized prior to calling this
1724 // function so repeat calls can accumulate a min and max of more than one sb64.
get_sb_partition_size_range(MACROBLOCKD * xd,MODE_INFO ** mi_8x8,BLOCK_SIZE * min_block_size,BLOCK_SIZE * max_block_size)1725 static void get_sb_partition_size_range(MACROBLOCKD *xd, MODE_INFO **mi_8x8,
1726 BLOCK_SIZE *min_block_size,
1727 BLOCK_SIZE *max_block_size ) {
1728 int sb_width_in_blocks = MI_BLOCK_SIZE;
1729 int sb_height_in_blocks = MI_BLOCK_SIZE;
1730 int i, j;
1731 int index = 0;
1732
1733 // Check the sb_type for each block that belongs to this region.
1734 for (i = 0; i < sb_height_in_blocks; ++i) {
1735 for (j = 0; j < sb_width_in_blocks; ++j) {
1736 MODE_INFO * mi = mi_8x8[index+j];
1737 BLOCK_SIZE sb_type = mi ? mi->mbmi.sb_type : 0;
1738 *min_block_size = MIN(*min_block_size, sb_type);
1739 *max_block_size = MAX(*max_block_size, sb_type);
1740 }
1741 index += xd->mi_stride;
1742 }
1743 }
1744
1745 // Next square block size less or equal than current block size.
1746 static const BLOCK_SIZE next_square_size[BLOCK_SIZES] = {
1747 BLOCK_4X4, BLOCK_4X4, BLOCK_4X4,
1748 BLOCK_8X8, BLOCK_8X8, BLOCK_8X8,
1749 BLOCK_16X16, BLOCK_16X16, BLOCK_16X16,
1750 BLOCK_32X32, BLOCK_32X32, BLOCK_32X32,
1751 BLOCK_64X64
1752 };
1753
1754 // Look at neighboring blocks and set a min and max partition size based on
1755 // what they chose.
rd_auto_partition_range(VP9_COMP * cpi,const TileInfo * const tile,int mi_row,int mi_col,BLOCK_SIZE * min_block_size,BLOCK_SIZE * max_block_size)1756 static void rd_auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
1757 int mi_row, int mi_col,
1758 BLOCK_SIZE *min_block_size,
1759 BLOCK_SIZE *max_block_size) {
1760 VP9_COMMON *const cm = &cpi->common;
1761 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1762 MODE_INFO **mi = xd->mi;
1763 const int left_in_image = xd->left_available && mi[-1];
1764 const int above_in_image = xd->up_available && mi[-xd->mi_stride];
1765 const int row8x8_remaining = tile->mi_row_end - mi_row;
1766 const int col8x8_remaining = tile->mi_col_end - mi_col;
1767 int bh, bw;
1768 BLOCK_SIZE min_size = BLOCK_4X4;
1769 BLOCK_SIZE max_size = BLOCK_64X64;
1770 // Trap case where we do not have a prediction.
1771 if (left_in_image || above_in_image || cm->frame_type != KEY_FRAME) {
1772 // Default "min to max" and "max to min"
1773 min_size = BLOCK_64X64;
1774 max_size = BLOCK_4X4;
1775
1776 // NOTE: each call to get_sb_partition_size_range() uses the previous
1777 // passed in values for min and max as a starting point.
1778 // Find the min and max partition used in previous frame at this location
1779 if (cm->frame_type != KEY_FRAME) {
1780 MODE_INFO **const prev_mi =
1781 &cm->prev_mi_grid_visible[mi_row * xd->mi_stride + mi_col];
1782 get_sb_partition_size_range(xd, prev_mi, &min_size, &max_size);
1783 }
1784 // Find the min and max partition sizes used in the left SB64
1785 if (left_in_image) {
1786 MODE_INFO **left_sb64_mi = &mi[-MI_BLOCK_SIZE];
1787 get_sb_partition_size_range(xd, left_sb64_mi, &min_size, &max_size);
1788 }
1789 // Find the min and max partition sizes used in the above SB64.
1790 if (above_in_image) {
1791 MODE_INFO **above_sb64_mi = &mi[-xd->mi_stride * MI_BLOCK_SIZE];
1792 get_sb_partition_size_range(xd, above_sb64_mi, &min_size, &max_size);
1793 }
1794 // adjust observed min and max
1795 if (cpi->sf.auto_min_max_partition_size == RELAXED_NEIGHBORING_MIN_MAX) {
1796 min_size = min_partition_size[min_size];
1797 max_size = max_partition_size[max_size];
1798 }
1799 }
1800
1801 // Check border cases where max and min from neighbors may not be legal.
1802 max_size = find_partition_size(max_size,
1803 row8x8_remaining, col8x8_remaining,
1804 &bh, &bw);
1805 min_size = MIN(min_size, max_size);
1806
1807 // When use_square_partition_only is true, make sure at least one square
1808 // partition is allowed by selecting the next smaller square size as
1809 // *min_block_size.
1810 if (cpi->sf.use_square_partition_only &&
1811 next_square_size[max_size] < min_size) {
1812 min_size = next_square_size[max_size];
1813 }
1814 *min_block_size = min_size;
1815 *max_block_size = max_size;
1816 }
1817
auto_partition_range(VP9_COMP * cpi,const TileInfo * const tile,int mi_row,int mi_col,BLOCK_SIZE * min_block_size,BLOCK_SIZE * max_block_size)1818 static void auto_partition_range(VP9_COMP *cpi, const TileInfo *const tile,
1819 int mi_row, int mi_col,
1820 BLOCK_SIZE *min_block_size,
1821 BLOCK_SIZE *max_block_size) {
1822 VP9_COMMON *const cm = &cpi->common;
1823 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1824 MODE_INFO **mi_8x8 = xd->mi;
1825 const int left_in_image = xd->left_available && mi_8x8[-1];
1826 const int above_in_image = xd->up_available &&
1827 mi_8x8[-xd->mi_stride];
1828 int row8x8_remaining = tile->mi_row_end - mi_row;
1829 int col8x8_remaining = tile->mi_col_end - mi_col;
1830 int bh, bw;
1831 BLOCK_SIZE min_size = BLOCK_32X32;
1832 BLOCK_SIZE max_size = BLOCK_8X8;
1833 int bsl = mi_width_log2(BLOCK_64X64);
1834 const int search_range_ctrl = (((mi_row + mi_col) >> bsl) +
1835 get_chessboard_index(cm->current_video_frame)) & 0x1;
1836 // Trap case where we do not have a prediction.
1837 if (search_range_ctrl &&
1838 (left_in_image || above_in_image || cm->frame_type != KEY_FRAME)) {
1839 int block;
1840 MODE_INFO **mi;
1841 BLOCK_SIZE sb_type;
1842
1843 // Find the min and max partition sizes used in the left SB64.
1844 if (left_in_image) {
1845 MODE_INFO *cur_mi;
1846 mi = &mi_8x8[-1];
1847 for (block = 0; block < MI_BLOCK_SIZE; ++block) {
1848 cur_mi = mi[block * xd->mi_stride];
1849 sb_type = cur_mi ? cur_mi->mbmi.sb_type : 0;
1850 min_size = MIN(min_size, sb_type);
1851 max_size = MAX(max_size, sb_type);
1852 }
1853 }
1854 // Find the min and max partition sizes used in the above SB64.
1855 if (above_in_image) {
1856 mi = &mi_8x8[-xd->mi_stride * MI_BLOCK_SIZE];
1857 for (block = 0; block < MI_BLOCK_SIZE; ++block) {
1858 sb_type = mi[block] ? mi[block]->mbmi.sb_type : 0;
1859 min_size = MIN(min_size, sb_type);
1860 max_size = MAX(max_size, sb_type);
1861 }
1862 }
1863
1864 min_size = min_partition_size[min_size];
1865 max_size = find_partition_size(max_size, row8x8_remaining, col8x8_remaining,
1866 &bh, &bw);
1867 min_size = MIN(min_size, max_size);
1868 min_size = MAX(min_size, BLOCK_8X8);
1869 max_size = MIN(max_size, BLOCK_32X32);
1870 } else {
1871 min_size = BLOCK_8X8;
1872 max_size = BLOCK_32X32;
1873 }
1874
1875 *min_block_size = min_size;
1876 *max_block_size = max_size;
1877 }
1878
1879 // TODO(jingning) refactor functions setting partition search range
set_partition_range(VP9_COMMON * cm,MACROBLOCKD * xd,int mi_row,int mi_col,BLOCK_SIZE bsize,BLOCK_SIZE * min_bs,BLOCK_SIZE * max_bs)1880 static void set_partition_range(VP9_COMMON *cm, MACROBLOCKD *xd,
1881 int mi_row, int mi_col, BLOCK_SIZE bsize,
1882 BLOCK_SIZE *min_bs, BLOCK_SIZE *max_bs) {
1883 int mi_width = num_8x8_blocks_wide_lookup[bsize];
1884 int mi_height = num_8x8_blocks_high_lookup[bsize];
1885 int idx, idy;
1886
1887 MODE_INFO *mi;
1888 MODE_INFO **prev_mi =
1889 &cm->prev_mi_grid_visible[mi_row * cm->mi_stride + mi_col];
1890 BLOCK_SIZE bs, min_size, max_size;
1891
1892 min_size = BLOCK_64X64;
1893 max_size = BLOCK_4X4;
1894
1895 if (prev_mi) {
1896 for (idy = 0; idy < mi_height; ++idy) {
1897 for (idx = 0; idx < mi_width; ++idx) {
1898 mi = prev_mi[idy * cm->mi_stride + idx];
1899 bs = mi ? mi->mbmi.sb_type : bsize;
1900 min_size = MIN(min_size, bs);
1901 max_size = MAX(max_size, bs);
1902 }
1903 }
1904 }
1905
1906 if (xd->left_available) {
1907 for (idy = 0; idy < mi_height; ++idy) {
1908 mi = xd->mi[idy * cm->mi_stride - 1];
1909 bs = mi ? mi->mbmi.sb_type : bsize;
1910 min_size = MIN(min_size, bs);
1911 max_size = MAX(max_size, bs);
1912 }
1913 }
1914
1915 if (xd->up_available) {
1916 for (idx = 0; idx < mi_width; ++idx) {
1917 mi = xd->mi[idx - cm->mi_stride];
1918 bs = mi ? mi->mbmi.sb_type : bsize;
1919 min_size = MIN(min_size, bs);
1920 max_size = MAX(max_size, bs);
1921 }
1922 }
1923
1924 if (min_size == max_size) {
1925 min_size = min_partition_size[min_size];
1926 max_size = max_partition_size[max_size];
1927 }
1928
1929 *min_bs = min_size;
1930 *max_bs = max_size;
1931 }
1932
store_pred_mv(MACROBLOCK * x,PICK_MODE_CONTEXT * ctx)1933 static INLINE void store_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
1934 vpx_memcpy(ctx->pred_mv, x->pred_mv, sizeof(x->pred_mv));
1935 }
1936
load_pred_mv(MACROBLOCK * x,PICK_MODE_CONTEXT * ctx)1937 static INLINE void load_pred_mv(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx) {
1938 vpx_memcpy(x->pred_mv, ctx->pred_mv, sizeof(x->pred_mv));
1939 }
1940
1941 #if CONFIG_FP_MB_STATS
1942 const int num_16x16_blocks_wide_lookup[BLOCK_SIZES] =
1943 {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 4, 4};
1944 const int num_16x16_blocks_high_lookup[BLOCK_SIZES] =
1945 {1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 4, 2, 4};
1946 const int qindex_skip_threshold_lookup[BLOCK_SIZES] =
1947 {0, 10, 10, 30, 40, 40, 60, 80, 80, 90, 100, 100, 120};
1948 const int qindex_split_threshold_lookup[BLOCK_SIZES] =
1949 {0, 3, 3, 7, 15, 15, 30, 40, 40, 60, 80, 80, 120};
1950 const int complexity_16x16_blocks_threshold[BLOCK_SIZES] =
1951 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 6};
1952
1953 typedef enum {
1954 MV_ZERO = 0,
1955 MV_LEFT = 1,
1956 MV_UP = 2,
1957 MV_RIGHT = 3,
1958 MV_DOWN = 4,
1959 MV_INVALID
1960 } MOTION_DIRECTION;
1961
get_motion_direction_fp(uint8_t fp_byte)1962 static INLINE MOTION_DIRECTION get_motion_direction_fp(uint8_t fp_byte) {
1963 if (fp_byte & FPMB_MOTION_ZERO_MASK) {
1964 return MV_ZERO;
1965 } else if (fp_byte & FPMB_MOTION_LEFT_MASK) {
1966 return MV_LEFT;
1967 } else if (fp_byte & FPMB_MOTION_RIGHT_MASK) {
1968 return MV_RIGHT;
1969 } else if (fp_byte & FPMB_MOTION_UP_MASK) {
1970 return MV_UP;
1971 } else {
1972 return MV_DOWN;
1973 }
1974 }
1975
get_motion_inconsistency(MOTION_DIRECTION this_mv,MOTION_DIRECTION that_mv)1976 static INLINE int get_motion_inconsistency(MOTION_DIRECTION this_mv,
1977 MOTION_DIRECTION that_mv) {
1978 if (this_mv == that_mv) {
1979 return 0;
1980 } else {
1981 return abs(this_mv - that_mv) == 2 ? 2 : 1;
1982 }
1983 }
1984 #endif
1985
1986 // TODO(jingning,jimbankoski,rbultje): properly skip partition types that are
1987 // unlikely to be selected depending on previous rate-distortion optimization
1988 // results, for encoding speed-up.
rd_pick_partition(VP9_COMP * cpi,const TileInfo * const tile,TOKENEXTRA ** tp,int mi_row,int mi_col,BLOCK_SIZE bsize,int * rate,int64_t * dist,int64_t best_rd,PC_TREE * pc_tree)1989 static void rd_pick_partition(VP9_COMP *cpi, const TileInfo *const tile,
1990 TOKENEXTRA **tp, int mi_row,
1991 int mi_col, BLOCK_SIZE bsize, int *rate,
1992 int64_t *dist, int64_t best_rd,
1993 PC_TREE *pc_tree) {
1994 VP9_COMMON *const cm = &cpi->common;
1995 MACROBLOCK *const x = &cpi->mb;
1996 MACROBLOCKD *const xd = &x->e_mbd;
1997 const int mi_step = num_8x8_blocks_wide_lookup[bsize] / 2;
1998 ENTROPY_CONTEXT l[16 * MAX_MB_PLANE], a[16 * MAX_MB_PLANE];
1999 PARTITION_CONTEXT sl[8], sa[8];
2000 TOKENEXTRA *tp_orig = *tp;
2001 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
2002 int i, pl;
2003 BLOCK_SIZE subsize;
2004 int this_rate, sum_rate = 0, best_rate = INT_MAX;
2005 int64_t this_dist, sum_dist = 0, best_dist = INT64_MAX;
2006 int64_t sum_rd = 0;
2007 int do_split = bsize >= BLOCK_8X8;
2008 int do_rect = 1;
2009
2010 // Override skipping rectangular partition operations for edge blocks
2011 const int force_horz_split = (mi_row + mi_step >= cm->mi_rows);
2012 const int force_vert_split = (mi_col + mi_step >= cm->mi_cols);
2013 const int xss = x->e_mbd.plane[1].subsampling_x;
2014 const int yss = x->e_mbd.plane[1].subsampling_y;
2015
2016 BLOCK_SIZE min_size = cpi->sf.min_partition_size;
2017 BLOCK_SIZE max_size = cpi->sf.max_partition_size;
2018
2019 #if CONFIG_FP_MB_STATS
2020 unsigned int src_diff_var = UINT_MAX;
2021 int none_complexity = 0;
2022 #endif
2023
2024 int partition_none_allowed = !force_horz_split && !force_vert_split;
2025 int partition_horz_allowed = !force_vert_split && yss <= xss &&
2026 bsize >= BLOCK_8X8;
2027 int partition_vert_allowed = !force_horz_split && xss <= yss &&
2028 bsize >= BLOCK_8X8;
2029 (void) *tp_orig;
2030
2031 assert(num_8x8_blocks_wide_lookup[bsize] ==
2032 num_8x8_blocks_high_lookup[bsize]);
2033
2034 set_offsets(cpi, tile, mi_row, mi_col, bsize);
2035
2036 if (bsize == BLOCK_16X16 && cpi->oxcf.aq_mode)
2037 x->mb_energy = vp9_block_energy(cpi, x, bsize);
2038
2039 if (cpi->sf.cb_partition_search && bsize == BLOCK_16X16) {
2040 int cb_partition_search_ctrl = ((pc_tree->index == 0 || pc_tree->index == 3)
2041 + get_chessboard_index(cm->current_video_frame)) & 0x1;
2042
2043 if (cb_partition_search_ctrl && bsize > min_size && bsize < max_size)
2044 set_partition_range(cm, xd, mi_row, mi_col, bsize, &min_size, &max_size);
2045 }
2046
2047 // Determine partition types in search according to the speed features.
2048 // The threshold set here has to be of square block size.
2049 if (cpi->sf.auto_min_max_partition_size) {
2050 partition_none_allowed &= (bsize <= max_size && bsize >= min_size);
2051 partition_horz_allowed &= ((bsize <= max_size && bsize > min_size) ||
2052 force_horz_split);
2053 partition_vert_allowed &= ((bsize <= max_size && bsize > min_size) ||
2054 force_vert_split);
2055 do_split &= bsize > min_size;
2056 }
2057 if (cpi->sf.use_square_partition_only) {
2058 partition_horz_allowed &= force_horz_split;
2059 partition_vert_allowed &= force_vert_split;
2060 }
2061
2062 save_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
2063
2064 if (cpi->sf.disable_split_var_thresh && partition_none_allowed) {
2065 unsigned int source_variancey;
2066 vp9_setup_src_planes(x, cpi->Source, mi_row, mi_col);
2067 source_variancey = get_sby_perpixel_variance(cpi, &x->plane[0].src, bsize);
2068 if (source_variancey < cpi->sf.disable_split_var_thresh) {
2069 do_split = 0;
2070 if (source_variancey < cpi->sf.disable_split_var_thresh / 2)
2071 do_rect = 0;
2072 }
2073 }
2074
2075 #if CONFIG_FP_MB_STATS
2076 if (cpi->use_fp_mb_stats) {
2077 set_offsets(cpi, tile, mi_row, mi_col, bsize);
2078 src_diff_var = get_sby_perpixel_diff_variance(cpi, &cpi->mb.plane[0].src,
2079 mi_row, mi_col, bsize);
2080 }
2081 #endif
2082
2083 #if CONFIG_FP_MB_STATS
2084 // Decide whether we shall split directly and skip searching NONE by using
2085 // the first pass block statistics
2086 if (cpi->use_fp_mb_stats && bsize >= BLOCK_32X32 && do_split &&
2087 partition_none_allowed && src_diff_var > 4 &&
2088 cm->base_qindex < qindex_split_threshold_lookup[bsize]) {
2089 int mb_row = mi_row >> 1;
2090 int mb_col = mi_col >> 1;
2091 int mb_row_end =
2092 MIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
2093 int mb_col_end =
2094 MIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
2095 int r, c;
2096
2097 // compute a complexity measure, basically measure inconsistency of motion
2098 // vectors obtained from the first pass in the current block
2099 for (r = mb_row; r < mb_row_end ; r++) {
2100 for (c = mb_col; c < mb_col_end; c++) {
2101 const int mb_index = r * cm->mb_cols + c;
2102
2103 MOTION_DIRECTION this_mv;
2104 MOTION_DIRECTION right_mv;
2105 MOTION_DIRECTION bottom_mv;
2106
2107 this_mv =
2108 get_motion_direction_fp(cpi->twopass.this_frame_mb_stats[mb_index]);
2109
2110 // to its right
2111 if (c != mb_col_end - 1) {
2112 right_mv = get_motion_direction_fp(
2113 cpi->twopass.this_frame_mb_stats[mb_index + 1]);
2114 none_complexity += get_motion_inconsistency(this_mv, right_mv);
2115 }
2116
2117 // to its bottom
2118 if (r != mb_row_end - 1) {
2119 bottom_mv = get_motion_direction_fp(
2120 cpi->twopass.this_frame_mb_stats[mb_index + cm->mb_cols]);
2121 none_complexity += get_motion_inconsistency(this_mv, bottom_mv);
2122 }
2123
2124 // do not count its left and top neighbors to avoid double counting
2125 }
2126 }
2127
2128 if (none_complexity > complexity_16x16_blocks_threshold[bsize]) {
2129 partition_none_allowed = 0;
2130 }
2131 }
2132 #endif
2133
2134 // PARTITION_NONE
2135 if (partition_none_allowed) {
2136 rd_pick_sb_modes(cpi, tile, mi_row, mi_col, &this_rate, &this_dist, bsize,
2137 ctx, best_rd, 0);
2138 if (this_rate != INT_MAX) {
2139 if (bsize >= BLOCK_8X8) {
2140 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2141 this_rate += cpi->partition_cost[pl][PARTITION_NONE];
2142 }
2143 sum_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_dist);
2144
2145 if (sum_rd < best_rd) {
2146 int64_t stop_thresh = 4096;
2147 int64_t stop_thresh_rd;
2148
2149 best_rate = this_rate;
2150 best_dist = this_dist;
2151 best_rd = sum_rd;
2152 if (bsize >= BLOCK_8X8)
2153 pc_tree->partitioning = PARTITION_NONE;
2154
2155 // Adjust threshold according to partition size.
2156 stop_thresh >>= 8 - (b_width_log2(bsize) +
2157 b_height_log2(bsize));
2158
2159 stop_thresh_rd = RDCOST(x->rdmult, x->rddiv, 0, stop_thresh);
2160 // If obtained distortion is very small, choose current partition
2161 // and stop splitting.
2162 if (!x->e_mbd.lossless && best_rd < stop_thresh_rd) {
2163 do_split = 0;
2164 do_rect = 0;
2165 }
2166
2167 #if CONFIG_FP_MB_STATS
2168 // Check if every 16x16 first pass block statistics has zero
2169 // motion and the corresponding first pass residue is small enough.
2170 // If that is the case, check the difference variance between the
2171 // current frame and the last frame. If the variance is small enough,
2172 // stop further splitting in RD optimization
2173 if (cpi->use_fp_mb_stats && do_split != 0 &&
2174 cm->base_qindex > qindex_skip_threshold_lookup[bsize]) {
2175 int mb_row = mi_row >> 1;
2176 int mb_col = mi_col >> 1;
2177 int mb_row_end =
2178 MIN(mb_row + num_16x16_blocks_high_lookup[bsize], cm->mb_rows);
2179 int mb_col_end =
2180 MIN(mb_col + num_16x16_blocks_wide_lookup[bsize], cm->mb_cols);
2181 int r, c;
2182
2183 int skip = 1;
2184 for (r = mb_row; r < mb_row_end; r++) {
2185 for (c = mb_col; c < mb_col_end; c++) {
2186 const int mb_index = r * cm->mb_cols + c;
2187 if (!(cpi->twopass.this_frame_mb_stats[mb_index] &
2188 FPMB_MOTION_ZERO_MASK) ||
2189 !(cpi->twopass.this_frame_mb_stats[mb_index] &
2190 FPMB_ERROR_SMALL_MASK)) {
2191 skip = 0;
2192 break;
2193 }
2194 }
2195 if (skip == 0) {
2196 break;
2197 }
2198 }
2199 if (skip) {
2200 if (src_diff_var == UINT_MAX) {
2201 set_offsets(cpi, tile, mi_row, mi_col, bsize);
2202 src_diff_var = get_sby_perpixel_diff_variance(
2203 cpi, &cpi->mb.plane[0].src, mi_row, mi_col, bsize);
2204 }
2205 if (src_diff_var < 8) {
2206 do_split = 0;
2207 do_rect = 0;
2208 }
2209 }
2210 }
2211 #endif
2212 }
2213 }
2214 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
2215 }
2216
2217 // store estimated motion vector
2218 if (cpi->sf.adaptive_motion_search)
2219 store_pred_mv(x, ctx);
2220
2221 // PARTITION_SPLIT
2222 sum_rd = 0;
2223 // TODO(jingning): use the motion vectors given by the above search as
2224 // the starting point of motion search in the following partition type check.
2225 if (do_split) {
2226 subsize = get_subsize(bsize, PARTITION_SPLIT);
2227 if (bsize == BLOCK_8X8) {
2228 i = 4;
2229 if (cpi->sf.adaptive_pred_interp_filter && partition_none_allowed)
2230 pc_tree->leaf_split[0]->pred_interp_filter =
2231 ctx->mic.mbmi.interp_filter;
2232 rd_pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
2233 pc_tree->leaf_split[0], best_rd, 0);
2234 if (sum_rate == INT_MAX)
2235 sum_rd = INT64_MAX;
2236 else
2237 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2238 } else {
2239 for (i = 0; i < 4 && sum_rd < best_rd; ++i) {
2240 const int x_idx = (i & 1) * mi_step;
2241 const int y_idx = (i >> 1) * mi_step;
2242
2243 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
2244 continue;
2245
2246 if (cpi->sf.adaptive_motion_search)
2247 load_pred_mv(x, ctx);
2248
2249 pc_tree->split[i]->index = i;
2250 rd_pick_partition(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx,
2251 subsize, &this_rate, &this_dist,
2252 best_rd - sum_rd, pc_tree->split[i]);
2253
2254 if (this_rate == INT_MAX) {
2255 sum_rd = INT64_MAX;
2256 } else {
2257 sum_rate += this_rate;
2258 sum_dist += this_dist;
2259 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2260 }
2261 }
2262 }
2263
2264 if (sum_rd < best_rd && i == 4) {
2265 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2266 sum_rate += cpi->partition_cost[pl][PARTITION_SPLIT];
2267 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2268
2269 if (sum_rd < best_rd) {
2270 best_rate = sum_rate;
2271 best_dist = sum_dist;
2272 best_rd = sum_rd;
2273 pc_tree->partitioning = PARTITION_SPLIT;
2274 }
2275 } else {
2276 // skip rectangular partition test when larger block size
2277 // gives better rd cost
2278 if (cpi->sf.less_rectangular_check)
2279 do_rect &= !partition_none_allowed;
2280 }
2281 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
2282 }
2283
2284 // PARTITION_HORZ
2285 if (partition_horz_allowed && do_rect) {
2286 subsize = get_subsize(bsize, PARTITION_HORZ);
2287 if (cpi->sf.adaptive_motion_search)
2288 load_pred_mv(x, ctx);
2289 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2290 partition_none_allowed)
2291 pc_tree->horizontal[0].pred_interp_filter =
2292 ctx->mic.mbmi.interp_filter;
2293 rd_pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
2294 &pc_tree->horizontal[0], best_rd, 0);
2295 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2296
2297 if (sum_rd < best_rd && mi_row + mi_step < cm->mi_rows) {
2298 PICK_MODE_CONTEXT *ctx = &pc_tree->horizontal[0];
2299 update_state(cpi, ctx, mi_row, mi_col, subsize, 0);
2300 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize, ctx);
2301
2302 if (cpi->sf.adaptive_motion_search)
2303 load_pred_mv(x, ctx);
2304 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2305 partition_none_allowed)
2306 pc_tree->horizontal[1].pred_interp_filter =
2307 ctx->mic.mbmi.interp_filter;
2308 rd_pick_sb_modes(cpi, tile, mi_row + mi_step, mi_col, &this_rate,
2309 &this_dist, subsize, &pc_tree->horizontal[1],
2310 best_rd - sum_rd, 1);
2311 if (this_rate == INT_MAX) {
2312 sum_rd = INT64_MAX;
2313 } else {
2314 sum_rate += this_rate;
2315 sum_dist += this_dist;
2316 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2317 }
2318 }
2319 if (sum_rd < best_rd) {
2320 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2321 sum_rate += cpi->partition_cost[pl][PARTITION_HORZ];
2322 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2323 if (sum_rd < best_rd) {
2324 best_rd = sum_rd;
2325 best_rate = sum_rate;
2326 best_dist = sum_dist;
2327 pc_tree->partitioning = PARTITION_HORZ;
2328 }
2329 }
2330 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
2331 }
2332 // PARTITION_VERT
2333 if (partition_vert_allowed && do_rect) {
2334 subsize = get_subsize(bsize, PARTITION_VERT);
2335
2336 if (cpi->sf.adaptive_motion_search)
2337 load_pred_mv(x, ctx);
2338 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2339 partition_none_allowed)
2340 pc_tree->vertical[0].pred_interp_filter =
2341 ctx->mic.mbmi.interp_filter;
2342 rd_pick_sb_modes(cpi, tile, mi_row, mi_col, &sum_rate, &sum_dist, subsize,
2343 &pc_tree->vertical[0], best_rd, 0);
2344 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2345 if (sum_rd < best_rd && mi_col + mi_step < cm->mi_cols) {
2346 update_state(cpi, &pc_tree->vertical[0], mi_row, mi_col, subsize, 0);
2347 encode_superblock(cpi, tp, 0, mi_row, mi_col, subsize,
2348 &pc_tree->vertical[0]);
2349
2350 if (cpi->sf.adaptive_motion_search)
2351 load_pred_mv(x, ctx);
2352 if (cpi->sf.adaptive_pred_interp_filter && bsize == BLOCK_8X8 &&
2353 partition_none_allowed)
2354 pc_tree->vertical[1].pred_interp_filter =
2355 ctx->mic.mbmi.interp_filter;
2356 rd_pick_sb_modes(cpi, tile, mi_row, mi_col + mi_step, &this_rate,
2357 &this_dist, subsize,
2358 &pc_tree->vertical[1], best_rd - sum_rd,
2359 1);
2360 if (this_rate == INT_MAX) {
2361 sum_rd = INT64_MAX;
2362 } else {
2363 sum_rate += this_rate;
2364 sum_dist += this_dist;
2365 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2366 }
2367 }
2368 if (sum_rd < best_rd) {
2369 pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2370 sum_rate += cpi->partition_cost[pl][PARTITION_VERT];
2371 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2372 if (sum_rd < best_rd) {
2373 best_rate = sum_rate;
2374 best_dist = sum_dist;
2375 best_rd = sum_rd;
2376 pc_tree->partitioning = PARTITION_VERT;
2377 }
2378 }
2379 restore_context(cpi, mi_row, mi_col, a, l, sa, sl, bsize);
2380 }
2381
2382 // TODO(jbb): This code added so that we avoid static analysis
2383 // warning related to the fact that best_rd isn't used after this
2384 // point. This code should be refactored so that the duplicate
2385 // checks occur in some sub function and thus are used...
2386 (void) best_rd;
2387 *rate = best_rate;
2388 *dist = best_dist;
2389
2390 if (best_rate < INT_MAX && best_dist < INT64_MAX && pc_tree->index != 3) {
2391 int output_enabled = (bsize == BLOCK_64X64);
2392
2393 // Check the projected output rate for this SB against it's target
2394 // and and if necessary apply a Q delta using segmentation to get
2395 // closer to the target.
2396 if ((cpi->oxcf.aq_mode == COMPLEXITY_AQ) && cm->seg.update_map)
2397 vp9_select_in_frame_q_segment(cpi, mi_row, mi_col, output_enabled,
2398 best_rate);
2399 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
2400 vp9_cyclic_refresh_set_rate_and_dist_sb(cpi->cyclic_refresh,
2401 best_rate, best_dist);
2402
2403 encode_sb(cpi, tile, tp, mi_row, mi_col, output_enabled, bsize, pc_tree);
2404 }
2405
2406 if (bsize == BLOCK_64X64) {
2407 assert(tp_orig < *tp);
2408 assert(best_rate < INT_MAX);
2409 assert(best_dist < INT64_MAX);
2410 } else {
2411 assert(tp_orig == *tp);
2412 }
2413 }
2414
encode_rd_sb_row(VP9_COMP * cpi,const TileInfo * const tile,int mi_row,TOKENEXTRA ** tp)2415 static void encode_rd_sb_row(VP9_COMP *cpi, const TileInfo *const tile,
2416 int mi_row, TOKENEXTRA **tp) {
2417 VP9_COMMON *const cm = &cpi->common;
2418 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
2419 SPEED_FEATURES *const sf = &cpi->sf;
2420 int mi_col;
2421
2422 // Initialize the left context for the new SB row
2423 vpx_memset(&xd->left_context, 0, sizeof(xd->left_context));
2424 vpx_memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
2425
2426 // Code each SB in the row
2427 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
2428 mi_col += MI_BLOCK_SIZE) {
2429 int dummy_rate;
2430 int64_t dummy_dist;
2431
2432 int i;
2433
2434 if (sf->adaptive_pred_interp_filter) {
2435 for (i = 0; i < 64; ++i)
2436 cpi->leaf_tree[i].pred_interp_filter = SWITCHABLE;
2437
2438 for (i = 0; i < 64; ++i) {
2439 cpi->pc_tree[i].vertical[0].pred_interp_filter = SWITCHABLE;
2440 cpi->pc_tree[i].vertical[1].pred_interp_filter = SWITCHABLE;
2441 cpi->pc_tree[i].horizontal[0].pred_interp_filter = SWITCHABLE;
2442 cpi->pc_tree[i].horizontal[1].pred_interp_filter = SWITCHABLE;
2443 }
2444 }
2445
2446 vp9_zero(cpi->mb.pred_mv);
2447 cpi->pc_root->index = 0;
2448
2449 if ((sf->partition_search_type == SEARCH_PARTITION &&
2450 sf->use_lastframe_partitioning) ||
2451 sf->partition_search_type == FIXED_PARTITION ||
2452 sf->partition_search_type == VAR_BASED_PARTITION ||
2453 sf->partition_search_type == VAR_BASED_FIXED_PARTITION) {
2454 const int idx_str = cm->mi_stride * mi_row + mi_col;
2455 MODE_INFO **mi = cm->mi_grid_visible + idx_str;
2456 MODE_INFO **prev_mi = cm->prev_mi_grid_visible + idx_str;
2457 cpi->mb.source_variance = UINT_MAX;
2458 if (sf->partition_search_type == FIXED_PARTITION) {
2459 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
2460 set_fixed_partitioning(cpi, tile, mi, mi_row, mi_col,
2461 sf->always_this_block_size);
2462 rd_use_partition(cpi, tile, mi, tp, mi_row, mi_col, BLOCK_64X64,
2463 &dummy_rate, &dummy_dist, 1, cpi->pc_root);
2464 } else if (cpi->skippable_frame ||
2465 sf->partition_search_type == VAR_BASED_FIXED_PARTITION) {
2466 BLOCK_SIZE bsize;
2467 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
2468 bsize = get_rd_var_based_fixed_partition(cpi, mi_row, mi_col);
2469 set_fixed_partitioning(cpi, tile, mi, mi_row, mi_col, bsize);
2470 rd_use_partition(cpi, tile, mi, tp, mi_row, mi_col, BLOCK_64X64,
2471 &dummy_rate, &dummy_dist, 1, cpi->pc_root);
2472 } else if (sf->partition_search_type == VAR_BASED_PARTITION) {
2473 choose_partitioning(cpi, tile, mi_row, mi_col);
2474 rd_use_partition(cpi, tile, mi, tp, mi_row, mi_col, BLOCK_64X64,
2475 &dummy_rate, &dummy_dist, 1, cpi->pc_root);
2476 } else {
2477 GF_GROUP * gf_grp = &cpi->twopass.gf_group;
2478 int last_was_mid_sequence_overlay = 0;
2479 if ((cpi->oxcf.pass == 2) && (gf_grp->index)) {
2480 if (gf_grp->update_type[gf_grp->index - 1] == OVERLAY_UPDATE)
2481 last_was_mid_sequence_overlay = 1;
2482 }
2483 if ((cpi->rc.frames_since_key
2484 % sf->last_partitioning_redo_frequency) == 0
2485 || last_was_mid_sequence_overlay
2486 || cm->prev_mi == 0
2487 || cm->show_frame == 0
2488 || cm->frame_type == KEY_FRAME
2489 || cpi->rc.is_src_frame_alt_ref
2490 || ((sf->use_lastframe_partitioning ==
2491 LAST_FRAME_PARTITION_LOW_MOTION) &&
2492 sb_has_motion(cm, prev_mi, sf->lf_motion_threshold))) {
2493 // If required set upper and lower partition size limits
2494 if (sf->auto_min_max_partition_size) {
2495 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
2496 rd_auto_partition_range(cpi, tile, mi_row, mi_col,
2497 &sf->min_partition_size,
2498 &sf->max_partition_size);
2499 }
2500 rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
2501 &dummy_rate, &dummy_dist, INT64_MAX,
2502 cpi->pc_root);
2503 } else {
2504 if (sf->constrain_copy_partition &&
2505 sb_has_motion(cm, prev_mi, sf->lf_motion_threshold))
2506 constrain_copy_partitioning(cpi, tile, mi, prev_mi,
2507 mi_row, mi_col, BLOCK_16X16);
2508 else
2509 copy_partitioning(cm, mi, prev_mi);
2510 rd_use_partition(cpi, tile, mi, tp, mi_row, mi_col, BLOCK_64X64,
2511 &dummy_rate, &dummy_dist, 1, cpi->pc_root);
2512 }
2513 }
2514 } else {
2515 // If required set upper and lower partition size limits
2516 if (sf->auto_min_max_partition_size) {
2517 set_offsets(cpi, tile, mi_row, mi_col, BLOCK_64X64);
2518 rd_auto_partition_range(cpi, tile, mi_row, mi_col,
2519 &sf->min_partition_size,
2520 &sf->max_partition_size);
2521 }
2522 rd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
2523 &dummy_rate, &dummy_dist, INT64_MAX, cpi->pc_root);
2524 }
2525 }
2526 }
2527
init_encode_frame_mb_context(VP9_COMP * cpi)2528 static void init_encode_frame_mb_context(VP9_COMP *cpi) {
2529 MACROBLOCK *const x = &cpi->mb;
2530 VP9_COMMON *const cm = &cpi->common;
2531 MACROBLOCKD *const xd = &x->e_mbd;
2532 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
2533
2534 // Copy data over into macro block data structures.
2535 vp9_setup_src_planes(x, cpi->Source, 0, 0);
2536
2537 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
2538
2539 // Note: this memset assumes above_context[0], [1] and [2]
2540 // are allocated as part of the same buffer.
2541 vpx_memset(xd->above_context[0], 0,
2542 sizeof(*xd->above_context[0]) *
2543 2 * aligned_mi_cols * MAX_MB_PLANE);
2544 vpx_memset(xd->above_seg_context, 0,
2545 sizeof(*xd->above_seg_context) * aligned_mi_cols);
2546 }
2547
check_dual_ref_flags(VP9_COMP * cpi)2548 static int check_dual_ref_flags(VP9_COMP *cpi) {
2549 const int ref_flags = cpi->ref_frame_flags;
2550
2551 if (vp9_segfeature_active(&cpi->common.seg, 1, SEG_LVL_REF_FRAME)) {
2552 return 0;
2553 } else {
2554 return (!!(ref_flags & VP9_GOLD_FLAG) + !!(ref_flags & VP9_LAST_FLAG)
2555 + !!(ref_flags & VP9_ALT_FLAG)) >= 2;
2556 }
2557 }
2558
reset_skip_tx_size(VP9_COMMON * cm,TX_SIZE max_tx_size)2559 static void reset_skip_tx_size(VP9_COMMON *cm, TX_SIZE max_tx_size) {
2560 int mi_row, mi_col;
2561 const int mis = cm->mi_stride;
2562 MODE_INFO **mi_ptr = cm->mi_grid_visible;
2563
2564 for (mi_row = 0; mi_row < cm->mi_rows; ++mi_row, mi_ptr += mis) {
2565 for (mi_col = 0; mi_col < cm->mi_cols; ++mi_col) {
2566 if (mi_ptr[mi_col]->mbmi.tx_size > max_tx_size)
2567 mi_ptr[mi_col]->mbmi.tx_size = max_tx_size;
2568 }
2569 }
2570 }
2571
get_frame_type(const VP9_COMP * cpi)2572 static MV_REFERENCE_FRAME get_frame_type(const VP9_COMP *cpi) {
2573 if (frame_is_intra_only(&cpi->common))
2574 return INTRA_FRAME;
2575 else if (cpi->rc.is_src_frame_alt_ref && cpi->refresh_golden_frame)
2576 return ALTREF_FRAME;
2577 else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
2578 return GOLDEN_FRAME;
2579 else
2580 return LAST_FRAME;
2581 }
2582
select_tx_mode(const VP9_COMP * cpi)2583 static TX_MODE select_tx_mode(const VP9_COMP *cpi) {
2584 if (cpi->mb.e_mbd.lossless)
2585 return ONLY_4X4;
2586 if (cpi->common.frame_type == KEY_FRAME)
2587 return TX_MODE_SELECT;
2588 if (cpi->sf.tx_size_search_method == USE_LARGESTALL)
2589 return ALLOW_32X32;
2590 else if (cpi->sf.tx_size_search_method == USE_FULL_RD||
2591 cpi->sf.tx_size_search_method == USE_TX_8X8)
2592 return TX_MODE_SELECT;
2593 else
2594 return cpi->common.tx_mode;
2595 }
2596
nonrd_pick_sb_modes(VP9_COMP * cpi,const TileInfo * const tile,int mi_row,int mi_col,int * rate,int64_t * dist,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)2597 static void nonrd_pick_sb_modes(VP9_COMP *cpi, const TileInfo *const tile,
2598 int mi_row, int mi_col,
2599 int *rate, int64_t *dist,
2600 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx) {
2601 VP9_COMMON *const cm = &cpi->common;
2602 MACROBLOCK *const x = &cpi->mb;
2603 MACROBLOCKD *const xd = &x->e_mbd;
2604 MB_MODE_INFO *mbmi;
2605 set_offsets(cpi, tile, mi_row, mi_col, bsize);
2606 mbmi = &xd->mi[0]->mbmi;
2607 mbmi->sb_type = bsize;
2608
2609 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled)
2610 if (mbmi->segment_id && x->in_static_area)
2611 x->rdmult = vp9_cyclic_refresh_get_rdmult(cpi->cyclic_refresh);
2612
2613 if (vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
2614 set_mode_info_seg_skip(x, cm->tx_mode, rate, dist, bsize);
2615 else
2616 vp9_pick_inter_mode(cpi, x, tile, mi_row, mi_col, rate, dist, bsize, ctx);
2617
2618 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
2619 }
2620
fill_mode_info_sb(VP9_COMMON * cm,MACROBLOCK * x,int mi_row,int mi_col,BLOCK_SIZE bsize,BLOCK_SIZE subsize,PC_TREE * pc_tree)2621 static void fill_mode_info_sb(VP9_COMMON *cm, MACROBLOCK *x,
2622 int mi_row, int mi_col,
2623 BLOCK_SIZE bsize, BLOCK_SIZE subsize,
2624 PC_TREE *pc_tree) {
2625 MACROBLOCKD *xd = &x->e_mbd;
2626 int bsl = b_width_log2(bsize), hbs = (1 << bsl) / 4;
2627 PARTITION_TYPE partition = pc_tree->partitioning;
2628
2629 assert(bsize >= BLOCK_8X8);
2630
2631 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
2632 return;
2633
2634 switch (partition) {
2635 case PARTITION_NONE:
2636 set_modeinfo_offsets(cm, xd, mi_row, mi_col);
2637 *(xd->mi[0]) = pc_tree->none.mic;
2638 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
2639 break;
2640 case PARTITION_VERT:
2641 set_modeinfo_offsets(cm, xd, mi_row, mi_col);
2642 *(xd->mi[0]) = pc_tree->vertical[0].mic;
2643 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
2644
2645 if (mi_col + hbs < cm->mi_cols) {
2646 set_modeinfo_offsets(cm, xd, mi_row, mi_col + hbs);
2647 *(xd->mi[0]) = pc_tree->vertical[1].mic;
2648 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col + hbs, bsize);
2649 }
2650 break;
2651 case PARTITION_HORZ:
2652 set_modeinfo_offsets(cm, xd, mi_row, mi_col);
2653 *(xd->mi[0]) = pc_tree->horizontal[0].mic;
2654 duplicate_mode_info_in_sb(cm, xd, mi_row, mi_col, bsize);
2655 if (mi_row + hbs < cm->mi_rows) {
2656 set_modeinfo_offsets(cm, xd, mi_row + hbs, mi_col);
2657 *(xd->mi[0]) = pc_tree->horizontal[1].mic;
2658 duplicate_mode_info_in_sb(cm, xd, mi_row + hbs, mi_col, bsize);
2659 }
2660 break;
2661 case PARTITION_SPLIT: {
2662 BLOCK_SIZE subsubsize = get_subsize(subsize, PARTITION_SPLIT);
2663 fill_mode_info_sb(cm, x, mi_row, mi_col, subsize,
2664 subsubsize, pc_tree->split[0]);
2665 fill_mode_info_sb(cm, x, mi_row, mi_col + hbs, subsize,
2666 subsubsize, pc_tree->split[1]);
2667 fill_mode_info_sb(cm, x, mi_row + hbs, mi_col, subsize,
2668 subsubsize, pc_tree->split[2]);
2669 fill_mode_info_sb(cm, x, mi_row + hbs, mi_col + hbs, subsize,
2670 subsubsize, pc_tree->split[3]);
2671 break;
2672 }
2673 default:
2674 break;
2675 }
2676 }
2677
nonrd_pick_partition(VP9_COMP * cpi,const TileInfo * const tile,TOKENEXTRA ** tp,int mi_row,int mi_col,BLOCK_SIZE bsize,int * rate,int64_t * dist,int do_recon,int64_t best_rd,PC_TREE * pc_tree)2678 static void nonrd_pick_partition(VP9_COMP *cpi, const TileInfo *const tile,
2679 TOKENEXTRA **tp, int mi_row,
2680 int mi_col, BLOCK_SIZE bsize, int *rate,
2681 int64_t *dist, int do_recon, int64_t best_rd,
2682 PC_TREE *pc_tree) {
2683 const SPEED_FEATURES *const sf = &cpi->sf;
2684 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2685 VP9_COMMON *const cm = &cpi->common;
2686 MACROBLOCK *const x = &cpi->mb;
2687 MACROBLOCKD *const xd = &x->e_mbd;
2688 const int ms = num_8x8_blocks_wide_lookup[bsize] / 2;
2689 TOKENEXTRA *tp_orig = *tp;
2690 PICK_MODE_CONTEXT *ctx = &pc_tree->none;
2691 int i;
2692 BLOCK_SIZE subsize = bsize;
2693 int this_rate, sum_rate = 0, best_rate = INT_MAX;
2694 int64_t this_dist, sum_dist = 0, best_dist = INT64_MAX;
2695 int64_t sum_rd = 0;
2696 int do_split = bsize >= BLOCK_8X8;
2697 int do_rect = 1;
2698 // Override skipping rectangular partition operations for edge blocks
2699 const int force_horz_split = (mi_row + ms >= cm->mi_rows);
2700 const int force_vert_split = (mi_col + ms >= cm->mi_cols);
2701 const int xss = x->e_mbd.plane[1].subsampling_x;
2702 const int yss = x->e_mbd.plane[1].subsampling_y;
2703
2704 int partition_none_allowed = !force_horz_split && !force_vert_split;
2705 int partition_horz_allowed = !force_vert_split && yss <= xss &&
2706 bsize >= BLOCK_8X8;
2707 int partition_vert_allowed = !force_horz_split && xss <= yss &&
2708 bsize >= BLOCK_8X8;
2709 (void) *tp_orig;
2710
2711 assert(num_8x8_blocks_wide_lookup[bsize] ==
2712 num_8x8_blocks_high_lookup[bsize]);
2713
2714 // Determine partition types in search according to the speed features.
2715 // The threshold set here has to be of square block size.
2716 if (sf->auto_min_max_partition_size) {
2717 partition_none_allowed &= (bsize <= sf->max_partition_size &&
2718 bsize >= sf->min_partition_size);
2719 partition_horz_allowed &= ((bsize <= sf->max_partition_size &&
2720 bsize > sf->min_partition_size) ||
2721 force_horz_split);
2722 partition_vert_allowed &= ((bsize <= sf->max_partition_size &&
2723 bsize > sf->min_partition_size) ||
2724 force_vert_split);
2725 do_split &= bsize > sf->min_partition_size;
2726 }
2727 if (sf->use_square_partition_only) {
2728 partition_horz_allowed &= force_horz_split;
2729 partition_vert_allowed &= force_vert_split;
2730 }
2731
2732 // PARTITION_NONE
2733 if (partition_none_allowed) {
2734 nonrd_pick_sb_modes(cpi, tile, mi_row, mi_col,
2735 &this_rate, &this_dist, bsize, ctx);
2736 ctx->mic.mbmi = xd->mi[0]->mbmi;
2737 ctx->skip_txfm[0] = x->skip_txfm[0];
2738 ctx->skip = x->skip;
2739
2740 if (this_rate != INT_MAX) {
2741 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2742 this_rate += cpi->partition_cost[pl][PARTITION_NONE];
2743 sum_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_dist);
2744 if (sum_rd < best_rd) {
2745 int64_t stop_thresh = 4096;
2746 int64_t stop_thresh_rd;
2747
2748 best_rate = this_rate;
2749 best_dist = this_dist;
2750 best_rd = sum_rd;
2751 if (bsize >= BLOCK_8X8)
2752 pc_tree->partitioning = PARTITION_NONE;
2753
2754 // Adjust threshold according to partition size.
2755 stop_thresh >>= 8 - (b_width_log2(bsize) +
2756 b_height_log2(bsize));
2757
2758 stop_thresh_rd = RDCOST(x->rdmult, x->rddiv, 0, stop_thresh);
2759 // If obtained distortion is very small, choose current partition
2760 // and stop splitting.
2761 if (!x->e_mbd.lossless && best_rd < stop_thresh_rd) {
2762 do_split = 0;
2763 do_rect = 0;
2764 }
2765 }
2766 }
2767 }
2768
2769 // store estimated motion vector
2770 store_pred_mv(x, ctx);
2771
2772 // PARTITION_SPLIT
2773 sum_rd = 0;
2774 if (do_split) {
2775 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2776 sum_rate += cpi->partition_cost[pl][PARTITION_SPLIT];
2777 subsize = get_subsize(bsize, PARTITION_SPLIT);
2778 for (i = 0; i < 4 && sum_rd < best_rd; ++i) {
2779 const int x_idx = (i & 1) * ms;
2780 const int y_idx = (i >> 1) * ms;
2781
2782 if (mi_row + y_idx >= cm->mi_rows || mi_col + x_idx >= cm->mi_cols)
2783 continue;
2784 load_pred_mv(x, ctx);
2785 nonrd_pick_partition(cpi, tile, tp, mi_row + y_idx, mi_col + x_idx,
2786 subsize, &this_rate, &this_dist, 0,
2787 best_rd - sum_rd, pc_tree->split[i]);
2788
2789 if (this_rate == INT_MAX) {
2790 sum_rd = INT64_MAX;
2791 } else {
2792 sum_rate += this_rate;
2793 sum_dist += this_dist;
2794 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2795 }
2796 }
2797
2798 if (sum_rd < best_rd) {
2799 best_rate = sum_rate;
2800 best_dist = sum_dist;
2801 best_rd = sum_rd;
2802 pc_tree->partitioning = PARTITION_SPLIT;
2803 } else {
2804 // skip rectangular partition test when larger block size
2805 // gives better rd cost
2806 if (sf->less_rectangular_check)
2807 do_rect &= !partition_none_allowed;
2808 }
2809 }
2810
2811 // PARTITION_HORZ
2812 if (partition_horz_allowed && do_rect) {
2813 subsize = get_subsize(bsize, PARTITION_HORZ);
2814 if (sf->adaptive_motion_search)
2815 load_pred_mv(x, ctx);
2816
2817 nonrd_pick_sb_modes(cpi, tile, mi_row, mi_col,
2818 &this_rate, &this_dist, subsize,
2819 &pc_tree->horizontal[0]);
2820
2821 pc_tree->horizontal[0].mic.mbmi = xd->mi[0]->mbmi;
2822 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
2823 pc_tree->horizontal[0].skip = x->skip;
2824
2825 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2826
2827 if (sum_rd < best_rd && mi_row + ms < cm->mi_rows) {
2828 load_pred_mv(x, ctx);
2829 nonrd_pick_sb_modes(cpi, tile, mi_row + ms, mi_col,
2830 &this_rate, &this_dist, subsize,
2831 &pc_tree->horizontal[1]);
2832
2833 pc_tree->horizontal[1].mic.mbmi = xd->mi[0]->mbmi;
2834 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
2835 pc_tree->horizontal[1].skip = x->skip;
2836
2837 if (this_rate == INT_MAX) {
2838 sum_rd = INT64_MAX;
2839 } else {
2840 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2841 this_rate += cpi->partition_cost[pl][PARTITION_HORZ];
2842 sum_rate += this_rate;
2843 sum_dist += this_dist;
2844 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2845 }
2846 }
2847 if (sum_rd < best_rd) {
2848 best_rd = sum_rd;
2849 best_rate = sum_rate;
2850 best_dist = sum_dist;
2851 pc_tree->partitioning = PARTITION_HORZ;
2852 }
2853 }
2854
2855 // PARTITION_VERT
2856 if (partition_vert_allowed && do_rect) {
2857 subsize = get_subsize(bsize, PARTITION_VERT);
2858
2859 if (sf->adaptive_motion_search)
2860 load_pred_mv(x, ctx);
2861
2862 nonrd_pick_sb_modes(cpi, tile, mi_row, mi_col,
2863 &this_rate, &this_dist, subsize,
2864 &pc_tree->vertical[0]);
2865 pc_tree->vertical[0].mic.mbmi = xd->mi[0]->mbmi;
2866 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
2867 pc_tree->vertical[0].skip = x->skip;
2868 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2869 if (sum_rd < best_rd && mi_col + ms < cm->mi_cols) {
2870 load_pred_mv(x, ctx);
2871 nonrd_pick_sb_modes(cpi, tile, mi_row, mi_col + ms,
2872 &this_rate, &this_dist, subsize,
2873 &pc_tree->vertical[1]);
2874 pc_tree->vertical[1].mic.mbmi = xd->mi[0]->mbmi;
2875 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
2876 pc_tree->vertical[1].skip = x->skip;
2877 if (this_rate == INT_MAX) {
2878 sum_rd = INT64_MAX;
2879 } else {
2880 int pl = partition_plane_context(xd, mi_row, mi_col, bsize);
2881 this_rate += cpi->partition_cost[pl][PARTITION_VERT];
2882 sum_rate += this_rate;
2883 sum_dist += this_dist;
2884 sum_rd = RDCOST(x->rdmult, x->rddiv, sum_rate, sum_dist);
2885 }
2886 }
2887 if (sum_rd < best_rd) {
2888 best_rate = sum_rate;
2889 best_dist = sum_dist;
2890 best_rd = sum_rd;
2891 pc_tree->partitioning = PARTITION_VERT;
2892 }
2893 }
2894 // TODO(JBB): The following line is here just to avoid a static warning
2895 // that occurs because at this point we never again reuse best_rd
2896 // despite setting it here. The code should be refactored to avoid this.
2897 (void) best_rd;
2898
2899 *rate = best_rate;
2900 *dist = best_dist;
2901
2902 if (best_rate == INT_MAX)
2903 return;
2904
2905 // update mode info array
2906 subsize = get_subsize(bsize, pc_tree->partitioning);
2907 fill_mode_info_sb(cm, x, mi_row, mi_col, bsize, subsize,
2908 pc_tree);
2909
2910 if (best_rate < INT_MAX && best_dist < INT64_MAX && do_recon) {
2911 int output_enabled = (bsize == BLOCK_64X64);
2912
2913 // Check the projected output rate for this SB against it's target
2914 // and and if necessary apply a Q delta using segmentation to get
2915 // closer to the target.
2916 if ((oxcf->aq_mode == COMPLEXITY_AQ) && cm->seg.update_map) {
2917 vp9_select_in_frame_q_segment(cpi, mi_row, mi_col, output_enabled,
2918 best_rate);
2919 }
2920
2921 if (oxcf->aq_mode == CYCLIC_REFRESH_AQ)
2922 vp9_cyclic_refresh_set_rate_and_dist_sb(cpi->cyclic_refresh,
2923 best_rate, best_dist);
2924
2925 encode_sb_rt(cpi, tile, tp, mi_row, mi_col, output_enabled, bsize, pc_tree);
2926 }
2927
2928 if (bsize == BLOCK_64X64) {
2929 assert(tp_orig < *tp);
2930 assert(best_rate < INT_MAX);
2931 assert(best_dist < INT64_MAX);
2932 } else {
2933 assert(tp_orig == *tp);
2934 }
2935 }
2936
nonrd_use_partition(VP9_COMP * cpi,const TileInfo * const tile,MODE_INFO ** mi,TOKENEXTRA ** tp,int mi_row,int mi_col,BLOCK_SIZE bsize,int output_enabled,int * totrate,int64_t * totdist,PC_TREE * pc_tree)2937 static void nonrd_use_partition(VP9_COMP *cpi,
2938 const TileInfo *const tile,
2939 MODE_INFO **mi,
2940 TOKENEXTRA **tp,
2941 int mi_row, int mi_col,
2942 BLOCK_SIZE bsize, int output_enabled,
2943 int *totrate, int64_t *totdist,
2944 PC_TREE *pc_tree) {
2945 VP9_COMMON *const cm = &cpi->common;
2946 MACROBLOCK *const x = &cpi->mb;
2947 MACROBLOCKD *const xd = &x->e_mbd;
2948 const int bsl = b_width_log2(bsize), hbs = (1 << bsl) / 4;
2949 const int mis = cm->mi_stride;
2950 PARTITION_TYPE partition;
2951 BLOCK_SIZE subsize;
2952 int rate = INT_MAX;
2953 int64_t dist = INT64_MAX;
2954
2955 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
2956 return;
2957
2958 subsize = (bsize >= BLOCK_8X8) ? mi[0]->mbmi.sb_type : BLOCK_4X4;
2959 partition = partition_lookup[bsl][subsize];
2960
2961 switch (partition) {
2962 case PARTITION_NONE:
2963 nonrd_pick_sb_modes(cpi, tile, mi_row, mi_col, totrate, totdist,
2964 subsize, &pc_tree->none);
2965 pc_tree->none.mic.mbmi = xd->mi[0]->mbmi;
2966 pc_tree->none.skip_txfm[0] = x->skip_txfm[0];
2967 pc_tree->none.skip = x->skip;
2968 break;
2969 case PARTITION_VERT:
2970 nonrd_pick_sb_modes(cpi, tile, mi_row, mi_col, totrate, totdist,
2971 subsize, &pc_tree->vertical[0]);
2972 pc_tree->vertical[0].mic.mbmi = xd->mi[0]->mbmi;
2973 pc_tree->vertical[0].skip_txfm[0] = x->skip_txfm[0];
2974 pc_tree->vertical[0].skip = x->skip;
2975 if (mi_col + hbs < cm->mi_cols) {
2976 nonrd_pick_sb_modes(cpi, tile, mi_row, mi_col + hbs,
2977 &rate, &dist, subsize, &pc_tree->vertical[1]);
2978 pc_tree->vertical[1].mic.mbmi = xd->mi[0]->mbmi;
2979 pc_tree->vertical[1].skip_txfm[0] = x->skip_txfm[0];
2980 pc_tree->vertical[1].skip = x->skip;
2981 if (rate != INT_MAX && dist != INT64_MAX &&
2982 *totrate != INT_MAX && *totdist != INT64_MAX) {
2983 *totrate += rate;
2984 *totdist += dist;
2985 }
2986 }
2987 break;
2988 case PARTITION_HORZ:
2989 nonrd_pick_sb_modes(cpi, tile, mi_row, mi_col, totrate, totdist,
2990 subsize, &pc_tree->horizontal[0]);
2991 pc_tree->horizontal[0].mic.mbmi = xd->mi[0]->mbmi;
2992 pc_tree->horizontal[0].skip_txfm[0] = x->skip_txfm[0];
2993 pc_tree->horizontal[0].skip = x->skip;
2994 if (mi_row + hbs < cm->mi_rows) {
2995 nonrd_pick_sb_modes(cpi, tile, mi_row + hbs, mi_col,
2996 &rate, &dist, subsize, &pc_tree->horizontal[0]);
2997 pc_tree->horizontal[1].mic.mbmi = xd->mi[0]->mbmi;
2998 pc_tree->horizontal[1].skip_txfm[0] = x->skip_txfm[0];
2999 pc_tree->horizontal[1].skip = x->skip;
3000 if (rate != INT_MAX && dist != INT64_MAX &&
3001 *totrate != INT_MAX && *totdist != INT64_MAX) {
3002 *totrate += rate;
3003 *totdist += dist;
3004 }
3005 }
3006 break;
3007 case PARTITION_SPLIT:
3008 subsize = get_subsize(bsize, PARTITION_SPLIT);
3009 nonrd_use_partition(cpi, tile, mi, tp, mi_row, mi_col,
3010 subsize, output_enabled, totrate, totdist,
3011 pc_tree->split[0]);
3012 nonrd_use_partition(cpi, tile, mi + hbs, tp,
3013 mi_row, mi_col + hbs, subsize, output_enabled,
3014 &rate, &dist, pc_tree->split[1]);
3015 if (rate != INT_MAX && dist != INT64_MAX &&
3016 *totrate != INT_MAX && *totdist != INT64_MAX) {
3017 *totrate += rate;
3018 *totdist += dist;
3019 }
3020 nonrd_use_partition(cpi, tile, mi + hbs * mis, tp,
3021 mi_row + hbs, mi_col, subsize, output_enabled,
3022 &rate, &dist, pc_tree->split[2]);
3023 if (rate != INT_MAX && dist != INT64_MAX &&
3024 *totrate != INT_MAX && *totdist != INT64_MAX) {
3025 *totrate += rate;
3026 *totdist += dist;
3027 }
3028 nonrd_use_partition(cpi, tile, mi + hbs * mis + hbs, tp,
3029 mi_row + hbs, mi_col + hbs, subsize, output_enabled,
3030 &rate, &dist, pc_tree->split[3]);
3031 if (rate != INT_MAX && dist != INT64_MAX &&
3032 *totrate != INT_MAX && *totdist != INT64_MAX) {
3033 *totrate += rate;
3034 *totdist += dist;
3035 }
3036 break;
3037 default:
3038 assert("Invalid partition type.");
3039 break;
3040 }
3041
3042 if (bsize == BLOCK_64X64 && output_enabled) {
3043 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
3044 vp9_cyclic_refresh_set_rate_and_dist_sb(cpi->cyclic_refresh,
3045 *totrate, *totdist);
3046 encode_sb_rt(cpi, tile, tp, mi_row, mi_col, 1, bsize, pc_tree);
3047 }
3048 }
3049
encode_nonrd_sb_row(VP9_COMP * cpi,const TileInfo * const tile,int mi_row,TOKENEXTRA ** tp)3050 static void encode_nonrd_sb_row(VP9_COMP *cpi, const TileInfo *const tile,
3051 int mi_row, TOKENEXTRA **tp) {
3052 SPEED_FEATURES *const sf = &cpi->sf;
3053 VP9_COMMON *const cm = &cpi->common;
3054 MACROBLOCK *const x = &cpi->mb;
3055 MACROBLOCKD *const xd = &x->e_mbd;
3056 int mi_col;
3057
3058 // Initialize the left context for the new SB row
3059 vpx_memset(&xd->left_context, 0, sizeof(xd->left_context));
3060 vpx_memset(xd->left_seg_context, 0, sizeof(xd->left_seg_context));
3061
3062 // Code each SB in the row
3063 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
3064 mi_col += MI_BLOCK_SIZE) {
3065 int dummy_rate = 0;
3066 int64_t dummy_dist = 0;
3067 const int idx_str = cm->mi_stride * mi_row + mi_col;
3068 MODE_INFO **mi = cm->mi_grid_visible + idx_str;
3069 MODE_INFO **prev_mi = cm->prev_mi_grid_visible + idx_str;
3070 BLOCK_SIZE bsize;
3071
3072 x->in_static_area = 0;
3073 x->source_variance = UINT_MAX;
3074 vp9_zero(x->pred_mv);
3075
3076 // Set the partition type of the 64X64 block
3077 switch (sf->partition_search_type) {
3078 case VAR_BASED_PARTITION:
3079 choose_partitioning(cpi, tile, mi_row, mi_col);
3080 nonrd_use_partition(cpi, tile, mi, tp, mi_row, mi_col, BLOCK_64X64,
3081 1, &dummy_rate, &dummy_dist, cpi->pc_root);
3082 break;
3083 case SOURCE_VAR_BASED_PARTITION:
3084 set_source_var_based_partition(cpi, tile, mi, mi_row, mi_col);
3085 nonrd_use_partition(cpi, tile, mi, tp, mi_row, mi_col, BLOCK_64X64,
3086 1, &dummy_rate, &dummy_dist, cpi->pc_root);
3087 break;
3088 case VAR_BASED_FIXED_PARTITION:
3089 case FIXED_PARTITION:
3090 bsize = sf->partition_search_type == FIXED_PARTITION ?
3091 sf->always_this_block_size :
3092 get_nonrd_var_based_fixed_partition(cpi, mi_row, mi_col);
3093 set_fixed_partitioning(cpi, tile, mi, mi_row, mi_col, bsize);
3094 nonrd_use_partition(cpi, tile, mi, tp, mi_row, mi_col, BLOCK_64X64,
3095 1, &dummy_rate, &dummy_dist, cpi->pc_root);
3096 break;
3097 case REFERENCE_PARTITION:
3098 if (sf->partition_check ||
3099 !is_background(cpi, tile, mi_row, mi_col)) {
3100 set_modeinfo_offsets(cm, xd, mi_row, mi_col);
3101 auto_partition_range(cpi, tile, mi_row, mi_col,
3102 &sf->min_partition_size,
3103 &sf->max_partition_size);
3104 nonrd_pick_partition(cpi, tile, tp, mi_row, mi_col, BLOCK_64X64,
3105 &dummy_rate, &dummy_dist, 1, INT64_MAX,
3106 cpi->pc_root);
3107 } else {
3108 copy_partitioning(cm, mi, prev_mi);
3109 nonrd_use_partition(cpi, tile, mi, tp, mi_row, mi_col,
3110 BLOCK_64X64, 1, &dummy_rate, &dummy_dist,
3111 cpi->pc_root);
3112 }
3113 break;
3114 default:
3115 assert(0);
3116 break;
3117 }
3118 }
3119 }
3120 // end RTC play code
3121
set_var_thresh_from_histogram(VP9_COMP * cpi)3122 static int set_var_thresh_from_histogram(VP9_COMP *cpi) {
3123 const SPEED_FEATURES *const sf = &cpi->sf;
3124 const VP9_COMMON *const cm = &cpi->common;
3125
3126 const uint8_t *src = cpi->Source->y_buffer;
3127 const uint8_t *last_src = cpi->Last_Source->y_buffer;
3128 const int src_stride = cpi->Source->y_stride;
3129 const int last_stride = cpi->Last_Source->y_stride;
3130
3131 // Pick cutoff threshold
3132 const int cutoff = (MIN(cm->width, cm->height) >= 720) ?
3133 (cm->MBs * VAR_HIST_LARGE_CUT_OFF / 100) :
3134 (cm->MBs * VAR_HIST_SMALL_CUT_OFF / 100);
3135 DECLARE_ALIGNED_ARRAY(16, int, hist, VAR_HIST_BINS);
3136 diff *var16 = cpi->source_diff_var;
3137
3138 int sum = 0;
3139 int i, j;
3140
3141 vpx_memset(hist, 0, VAR_HIST_BINS * sizeof(hist[0]));
3142
3143 for (i = 0; i < cm->mb_rows; i++) {
3144 for (j = 0; j < cm->mb_cols; j++) {
3145 vp9_get16x16var(src, src_stride, last_src, last_stride,
3146 &var16->sse, &var16->sum);
3147
3148 var16->var = var16->sse -
3149 (((uint32_t)var16->sum * var16->sum) >> 8);
3150
3151 if (var16->var >= VAR_HIST_MAX_BG_VAR)
3152 hist[VAR_HIST_BINS - 1]++;
3153 else
3154 hist[var16->var / VAR_HIST_FACTOR]++;
3155
3156 src += 16;
3157 last_src += 16;
3158 var16++;
3159 }
3160
3161 src = src - cm->mb_cols * 16 + 16 * src_stride;
3162 last_src = last_src - cm->mb_cols * 16 + 16 * last_stride;
3163 }
3164
3165 cpi->source_var_thresh = 0;
3166
3167 if (hist[VAR_HIST_BINS - 1] < cutoff) {
3168 for (i = 0; i < VAR_HIST_BINS - 1; i++) {
3169 sum += hist[i];
3170
3171 if (sum > cutoff) {
3172 cpi->source_var_thresh = (i + 1) * VAR_HIST_FACTOR;
3173 return 0;
3174 }
3175 }
3176 }
3177
3178 return sf->search_type_check_frequency;
3179 }
3180
source_var_based_partition_search_method(VP9_COMP * cpi)3181 static void source_var_based_partition_search_method(VP9_COMP *cpi) {
3182 VP9_COMMON *const cm = &cpi->common;
3183 SPEED_FEATURES *const sf = &cpi->sf;
3184
3185 if (cm->frame_type == KEY_FRAME) {
3186 // For key frame, use SEARCH_PARTITION.
3187 sf->partition_search_type = SEARCH_PARTITION;
3188 } else if (cm->intra_only) {
3189 sf->partition_search_type = FIXED_PARTITION;
3190 } else {
3191 if (cm->last_width != cm->width || cm->last_height != cm->height) {
3192 if (cpi->source_diff_var)
3193 vpx_free(cpi->source_diff_var);
3194
3195 CHECK_MEM_ERROR(cm, cpi->source_diff_var,
3196 vpx_calloc(cm->MBs, sizeof(diff)));
3197 }
3198
3199 if (!cpi->frames_till_next_var_check)
3200 cpi->frames_till_next_var_check = set_var_thresh_from_histogram(cpi);
3201
3202 if (cpi->frames_till_next_var_check > 0) {
3203 sf->partition_search_type = FIXED_PARTITION;
3204 cpi->frames_till_next_var_check--;
3205 }
3206 }
3207 }
3208
get_skip_encode_frame(const VP9_COMMON * cm)3209 static int get_skip_encode_frame(const VP9_COMMON *cm) {
3210 unsigned int intra_count = 0, inter_count = 0;
3211 int j;
3212
3213 for (j = 0; j < INTRA_INTER_CONTEXTS; ++j) {
3214 intra_count += cm->counts.intra_inter[j][0];
3215 inter_count += cm->counts.intra_inter[j][1];
3216 }
3217
3218 return (intra_count << 2) < inter_count &&
3219 cm->frame_type != KEY_FRAME &&
3220 cm->show_frame;
3221 }
3222
encode_tiles(VP9_COMP * cpi)3223 static void encode_tiles(VP9_COMP *cpi) {
3224 const VP9_COMMON *const cm = &cpi->common;
3225 const int tile_cols = 1 << cm->log2_tile_cols;
3226 const int tile_rows = 1 << cm->log2_tile_rows;
3227 int tile_col, tile_row;
3228 TOKENEXTRA *tok = cpi->tok;
3229
3230 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
3231 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
3232 TileInfo tile;
3233 TOKENEXTRA *old_tok = tok;
3234 int mi_row;
3235
3236 vp9_tile_init(&tile, cm, tile_row, tile_col);
3237 for (mi_row = tile.mi_row_start; mi_row < tile.mi_row_end;
3238 mi_row += MI_BLOCK_SIZE) {
3239 if (cpi->sf.use_nonrd_pick_mode && !frame_is_intra_only(cm))
3240 encode_nonrd_sb_row(cpi, &tile, mi_row, &tok);
3241 else
3242 encode_rd_sb_row(cpi, &tile, mi_row, &tok);
3243 }
3244 cpi->tok_count[tile_row][tile_col] = (unsigned int)(tok - old_tok);
3245 assert(tok - cpi->tok <= get_token_alloc(cm->mb_rows, cm->mb_cols));
3246 }
3247 }
3248 }
3249
3250 #if CONFIG_FP_MB_STATS
input_fpmb_stats(FIRSTPASS_MB_STATS * firstpass_mb_stats,VP9_COMMON * cm,uint8_t ** this_frame_mb_stats)3251 static int input_fpmb_stats(FIRSTPASS_MB_STATS *firstpass_mb_stats,
3252 VP9_COMMON *cm, uint8_t **this_frame_mb_stats) {
3253 uint8_t *mb_stats_in = firstpass_mb_stats->mb_stats_start +
3254 cm->current_video_frame * cm->MBs * sizeof(uint8_t);
3255
3256 if (mb_stats_in > firstpass_mb_stats->mb_stats_end)
3257 return EOF;
3258
3259 *this_frame_mb_stats = mb_stats_in;
3260
3261 return 1;
3262 }
3263 #endif
3264
encode_frame_internal(VP9_COMP * cpi)3265 static void encode_frame_internal(VP9_COMP *cpi) {
3266 SPEED_FEATURES *const sf = &cpi->sf;
3267 RD_OPT *const rd_opt = &cpi->rd;
3268 MACROBLOCK *const x = &cpi->mb;
3269 VP9_COMMON *const cm = &cpi->common;
3270 MACROBLOCKD *const xd = &x->e_mbd;
3271
3272 xd->mi = cm->mi_grid_visible;
3273 xd->mi[0] = cm->mi;
3274
3275 vp9_zero(cm->counts);
3276 vp9_zero(cpi->coef_counts);
3277 vp9_zero(cpi->tx_stepdown_count);
3278 vp9_zero(rd_opt->comp_pred_diff);
3279 vp9_zero(rd_opt->filter_diff);
3280 vp9_zero(rd_opt->tx_select_diff);
3281 vp9_zero(rd_opt->tx_select_threshes);
3282
3283 xd->lossless = cm->base_qindex == 0 &&
3284 cm->y_dc_delta_q == 0 &&
3285 cm->uv_dc_delta_q == 0 &&
3286 cm->uv_ac_delta_q == 0;
3287
3288 cm->tx_mode = select_tx_mode(cpi);
3289
3290 x->fwd_txm4x4 = xd->lossless ? vp9_fwht4x4 : vp9_fdct4x4;
3291 x->itxm_add = xd->lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
3292
3293 if (xd->lossless) {
3294 x->optimize = 0;
3295 cm->lf.filter_level = 0;
3296 cpi->zbin_mode_boost_enabled = 0;
3297 }
3298
3299 vp9_frame_init_quantizer(cpi);
3300
3301 vp9_initialize_rd_consts(cpi);
3302 vp9_initialize_me_consts(cpi, cm->base_qindex);
3303 init_encode_frame_mb_context(cpi);
3304 set_prev_mi(cm);
3305
3306 x->quant_fp = cpi->sf.use_quant_fp;
3307 vp9_zero(x->skip_txfm);
3308 if (sf->use_nonrd_pick_mode) {
3309 // Initialize internal buffer pointers for rtc coding, where non-RD
3310 // mode decision is used and hence no buffer pointer swap needed.
3311 int i;
3312 struct macroblock_plane *const p = x->plane;
3313 struct macroblockd_plane *const pd = xd->plane;
3314 PICK_MODE_CONTEXT *ctx = &cpi->pc_root->none;
3315
3316 for (i = 0; i < MAX_MB_PLANE; ++i) {
3317 p[i].coeff = ctx->coeff_pbuf[i][0];
3318 p[i].qcoeff = ctx->qcoeff_pbuf[i][0];
3319 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][0];
3320 p[i].eobs = ctx->eobs_pbuf[i][0];
3321 }
3322 vp9_zero(x->zcoeff_blk);
3323
3324 if (sf->partition_search_type == SOURCE_VAR_BASED_PARTITION)
3325 source_var_based_partition_search_method(cpi);
3326 }
3327
3328 {
3329 struct vpx_usec_timer emr_timer;
3330 vpx_usec_timer_start(&emr_timer);
3331
3332 #if CONFIG_FP_MB_STATS
3333 if (cpi->use_fp_mb_stats) {
3334 input_fpmb_stats(&cpi->twopass.firstpass_mb_stats, cm,
3335 &cpi->twopass.this_frame_mb_stats);
3336 }
3337 #endif
3338
3339 encode_tiles(cpi);
3340
3341 vpx_usec_timer_mark(&emr_timer);
3342 cpi->time_encode_sb_row += vpx_usec_timer_elapsed(&emr_timer);
3343 }
3344
3345 sf->skip_encode_frame = sf->skip_encode_sb ? get_skip_encode_frame(cm) : 0;
3346
3347 #if 0
3348 // Keep record of the total distortion this time around for future use
3349 cpi->last_frame_distortion = cpi->frame_distortion;
3350 #endif
3351 }
3352
get_interp_filter(const int64_t threshes[SWITCHABLE_FILTER_CONTEXTS],int is_alt_ref)3353 static INTERP_FILTER get_interp_filter(
3354 const int64_t threshes[SWITCHABLE_FILTER_CONTEXTS], int is_alt_ref) {
3355 if (!is_alt_ref &&
3356 threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP] &&
3357 threshes[EIGHTTAP_SMOOTH] > threshes[EIGHTTAP_SHARP] &&
3358 threshes[EIGHTTAP_SMOOTH] > threshes[SWITCHABLE - 1]) {
3359 return EIGHTTAP_SMOOTH;
3360 } else if (threshes[EIGHTTAP_SHARP] > threshes[EIGHTTAP] &&
3361 threshes[EIGHTTAP_SHARP] > threshes[SWITCHABLE - 1]) {
3362 return EIGHTTAP_SHARP;
3363 } else if (threshes[EIGHTTAP] > threshes[SWITCHABLE - 1]) {
3364 return EIGHTTAP;
3365 } else {
3366 return SWITCHABLE;
3367 }
3368 }
3369
vp9_encode_frame(VP9_COMP * cpi)3370 void vp9_encode_frame(VP9_COMP *cpi) {
3371 VP9_COMMON *const cm = &cpi->common;
3372 RD_OPT *const rd_opt = &cpi->rd;
3373
3374 // In the longer term the encoder should be generalized to match the
3375 // decoder such that we allow compound where one of the 3 buffers has a
3376 // different sign bias and that buffer is then the fixed ref. However, this
3377 // requires further work in the rd loop. For now the only supported encoder
3378 // side behavior is where the ALT ref buffer has opposite sign bias to
3379 // the other two.
3380 if (!frame_is_intra_only(cm)) {
3381 if ((cm->ref_frame_sign_bias[ALTREF_FRAME] ==
3382 cm->ref_frame_sign_bias[GOLDEN_FRAME]) ||
3383 (cm->ref_frame_sign_bias[ALTREF_FRAME] ==
3384 cm->ref_frame_sign_bias[LAST_FRAME])) {
3385 cm->allow_comp_inter_inter = 0;
3386 } else {
3387 cm->allow_comp_inter_inter = 1;
3388 cm->comp_fixed_ref = ALTREF_FRAME;
3389 cm->comp_var_ref[0] = LAST_FRAME;
3390 cm->comp_var_ref[1] = GOLDEN_FRAME;
3391 }
3392 }
3393
3394 if (cpi->sf.frame_parameter_update) {
3395 int i;
3396
3397 // This code does a single RD pass over the whole frame assuming
3398 // either compound, single or hybrid prediction as per whatever has
3399 // worked best for that type of frame in the past.
3400 // It also predicts whether another coding mode would have worked
3401 // better that this coding mode. If that is the case, it remembers
3402 // that for subsequent frames.
3403 // It does the same analysis for transform size selection also.
3404 const MV_REFERENCE_FRAME frame_type = get_frame_type(cpi);
3405 int64_t *const mode_thrs = rd_opt->prediction_type_threshes[frame_type];
3406 int64_t *const filter_thrs = rd_opt->filter_threshes[frame_type];
3407 int *const tx_thrs = rd_opt->tx_select_threshes[frame_type];
3408 const int is_alt_ref = frame_type == ALTREF_FRAME;
3409
3410 /* prediction (compound, single or hybrid) mode selection */
3411 if (is_alt_ref || !cm->allow_comp_inter_inter)
3412 cm->reference_mode = SINGLE_REFERENCE;
3413 else if (mode_thrs[COMPOUND_REFERENCE] > mode_thrs[SINGLE_REFERENCE] &&
3414 mode_thrs[COMPOUND_REFERENCE] >
3415 mode_thrs[REFERENCE_MODE_SELECT] &&
3416 check_dual_ref_flags(cpi) &&
3417 cpi->static_mb_pct == 100)
3418 cm->reference_mode = COMPOUND_REFERENCE;
3419 else if (mode_thrs[SINGLE_REFERENCE] > mode_thrs[REFERENCE_MODE_SELECT])
3420 cm->reference_mode = SINGLE_REFERENCE;
3421 else
3422 cm->reference_mode = REFERENCE_MODE_SELECT;
3423
3424 if (cm->interp_filter == SWITCHABLE)
3425 cm->interp_filter = get_interp_filter(filter_thrs, is_alt_ref);
3426
3427 encode_frame_internal(cpi);
3428
3429 for (i = 0; i < REFERENCE_MODES; ++i)
3430 mode_thrs[i] = (mode_thrs[i] + rd_opt->comp_pred_diff[i] / cm->MBs) / 2;
3431
3432 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
3433 filter_thrs[i] = (filter_thrs[i] + rd_opt->filter_diff[i] / cm->MBs) / 2;
3434
3435 for (i = 0; i < TX_MODES; ++i) {
3436 int64_t pd = rd_opt->tx_select_diff[i];
3437 if (i == TX_MODE_SELECT)
3438 pd -= RDCOST(cpi->mb.rdmult, cpi->mb.rddiv, 2048 * (TX_SIZES - 1), 0);
3439 tx_thrs[i] = (tx_thrs[i] + (int)(pd / cm->MBs)) / 2;
3440 }
3441
3442 if (cm->reference_mode == REFERENCE_MODE_SELECT) {
3443 int single_count_zero = 0;
3444 int comp_count_zero = 0;
3445
3446 for (i = 0; i < COMP_INTER_CONTEXTS; i++) {
3447 single_count_zero += cm->counts.comp_inter[i][0];
3448 comp_count_zero += cm->counts.comp_inter[i][1];
3449 }
3450
3451 if (comp_count_zero == 0) {
3452 cm->reference_mode = SINGLE_REFERENCE;
3453 vp9_zero(cm->counts.comp_inter);
3454 } else if (single_count_zero == 0) {
3455 cm->reference_mode = COMPOUND_REFERENCE;
3456 vp9_zero(cm->counts.comp_inter);
3457 }
3458 }
3459
3460 if (cm->tx_mode == TX_MODE_SELECT) {
3461 int count4x4 = 0;
3462 int count8x8_lp = 0, count8x8_8x8p = 0;
3463 int count16x16_16x16p = 0, count16x16_lp = 0;
3464 int count32x32 = 0;
3465
3466 for (i = 0; i < TX_SIZE_CONTEXTS; ++i) {
3467 count4x4 += cm->counts.tx.p32x32[i][TX_4X4];
3468 count4x4 += cm->counts.tx.p16x16[i][TX_4X4];
3469 count4x4 += cm->counts.tx.p8x8[i][TX_4X4];
3470
3471 count8x8_lp += cm->counts.tx.p32x32[i][TX_8X8];
3472 count8x8_lp += cm->counts.tx.p16x16[i][TX_8X8];
3473 count8x8_8x8p += cm->counts.tx.p8x8[i][TX_8X8];
3474
3475 count16x16_16x16p += cm->counts.tx.p16x16[i][TX_16X16];
3476 count16x16_lp += cm->counts.tx.p32x32[i][TX_16X16];
3477 count32x32 += cm->counts.tx.p32x32[i][TX_32X32];
3478 }
3479
3480 if (count4x4 == 0 && count16x16_lp == 0 && count16x16_16x16p == 0 &&
3481 count32x32 == 0) {
3482 cm->tx_mode = ALLOW_8X8;
3483 reset_skip_tx_size(cm, TX_8X8);
3484 } else if (count8x8_8x8p == 0 && count16x16_16x16p == 0 &&
3485 count8x8_lp == 0 && count16x16_lp == 0 && count32x32 == 0) {
3486 cm->tx_mode = ONLY_4X4;
3487 reset_skip_tx_size(cm, TX_4X4);
3488 } else if (count8x8_lp == 0 && count16x16_lp == 0 && count4x4 == 0) {
3489 cm->tx_mode = ALLOW_32X32;
3490 } else if (count32x32 == 0 && count8x8_lp == 0 && count4x4 == 0) {
3491 cm->tx_mode = ALLOW_16X16;
3492 reset_skip_tx_size(cm, TX_16X16);
3493 }
3494 }
3495 } else {
3496 cm->reference_mode = SINGLE_REFERENCE;
3497 encode_frame_internal(cpi);
3498 }
3499 }
3500
sum_intra_stats(FRAME_COUNTS * counts,const MODE_INFO * mi)3501 static void sum_intra_stats(FRAME_COUNTS *counts, const MODE_INFO *mi) {
3502 const PREDICTION_MODE y_mode = mi->mbmi.mode;
3503 const PREDICTION_MODE uv_mode = mi->mbmi.uv_mode;
3504 const BLOCK_SIZE bsize = mi->mbmi.sb_type;
3505
3506 if (bsize < BLOCK_8X8) {
3507 int idx, idy;
3508 const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
3509 const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
3510 for (idy = 0; idy < 2; idy += num_4x4_h)
3511 for (idx = 0; idx < 2; idx += num_4x4_w)
3512 ++counts->y_mode[0][mi->bmi[idy * 2 + idx].as_mode];
3513 } else {
3514 ++counts->y_mode[size_group_lookup[bsize]][y_mode];
3515 }
3516
3517 ++counts->uv_mode[y_mode][uv_mode];
3518 }
3519
get_zbin_mode_boost(const MB_MODE_INFO * mbmi,int enabled)3520 static int get_zbin_mode_boost(const MB_MODE_INFO *mbmi, int enabled) {
3521 if (enabled) {
3522 if (is_inter_block(mbmi)) {
3523 if (mbmi->mode == ZEROMV) {
3524 return mbmi->ref_frame[0] != LAST_FRAME ? GF_ZEROMV_ZBIN_BOOST
3525 : LF_ZEROMV_ZBIN_BOOST;
3526 } else {
3527 return mbmi->sb_type < BLOCK_8X8 ? SPLIT_MV_ZBIN_BOOST
3528 : MV_ZBIN_BOOST;
3529 }
3530 } else {
3531 return INTRA_ZBIN_BOOST;
3532 }
3533 } else {
3534 return 0;
3535 }
3536 }
3537
encode_superblock(VP9_COMP * cpi,TOKENEXTRA ** t,int output_enabled,int mi_row,int mi_col,BLOCK_SIZE bsize,PICK_MODE_CONTEXT * ctx)3538 static void encode_superblock(VP9_COMP *cpi, TOKENEXTRA **t, int output_enabled,
3539 int mi_row, int mi_col, BLOCK_SIZE bsize,
3540 PICK_MODE_CONTEXT *ctx) {
3541 VP9_COMMON *const cm = &cpi->common;
3542 MACROBLOCK *const x = &cpi->mb;
3543 MACROBLOCKD *const xd = &x->e_mbd;
3544 MODE_INFO **mi_8x8 = xd->mi;
3545 MODE_INFO *mi = mi_8x8[0];
3546 MB_MODE_INFO *mbmi = &mi->mbmi;
3547 const int seg_skip = vp9_segfeature_active(&cm->seg, mbmi->segment_id,
3548 SEG_LVL_SKIP);
3549 const int mis = cm->mi_stride;
3550 const int mi_width = num_8x8_blocks_wide_lookup[bsize];
3551 const int mi_height = num_8x8_blocks_high_lookup[bsize];
3552
3553 x->skip_recode = !x->select_tx_size && mbmi->sb_type >= BLOCK_8X8 &&
3554 cpi->oxcf.aq_mode != COMPLEXITY_AQ &&
3555 cpi->oxcf.aq_mode != CYCLIC_REFRESH_AQ &&
3556 cpi->sf.allow_skip_recode;
3557
3558 if (!x->skip_recode && !cpi->sf.use_nonrd_pick_mode)
3559 vpx_memset(x->skip_txfm, 0, sizeof(x->skip_txfm));
3560
3561 x->skip_optimize = ctx->is_coded;
3562 ctx->is_coded = 1;
3563 x->use_lp32x32fdct = cpi->sf.use_lp32x32fdct;
3564 x->skip_encode = (!output_enabled && cpi->sf.skip_encode_frame &&
3565 x->q_index < QIDX_SKIP_THRESH);
3566
3567 if (x->skip_encode)
3568 return;
3569
3570 set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
3571
3572 // Experimental code. Special case for gf and arf zeromv modes.
3573 // Increase zbin size to suppress noise
3574 cpi->zbin_mode_boost = get_zbin_mode_boost(mbmi,
3575 cpi->zbin_mode_boost_enabled);
3576 vp9_update_zbin_extra(cpi, x);
3577
3578 if (!is_inter_block(mbmi)) {
3579 int plane;
3580 mbmi->skip = 1;
3581 for (plane = 0; plane < MAX_MB_PLANE; ++plane)
3582 vp9_encode_intra_block_plane(x, MAX(bsize, BLOCK_8X8), plane);
3583 if (output_enabled)
3584 sum_intra_stats(&cm->counts, mi);
3585 vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8));
3586 } else {
3587 int ref;
3588 const int is_compound = has_second_ref(mbmi);
3589 for (ref = 0; ref < 1 + is_compound; ++ref) {
3590 YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi,
3591 mbmi->ref_frame[ref]);
3592 vp9_setup_pre_planes(xd, ref, cfg, mi_row, mi_col,
3593 &xd->block_refs[ref]->sf);
3594 }
3595 if (!cpi->sf.reuse_inter_pred_sby || seg_skip)
3596 vp9_build_inter_predictors_sby(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8));
3597
3598 vp9_build_inter_predictors_sbuv(xd, mi_row, mi_col, MAX(bsize, BLOCK_8X8));
3599
3600 if (!x->skip) {
3601 mbmi->skip = 1;
3602 vp9_encode_sb(x, MAX(bsize, BLOCK_8X8));
3603 vp9_tokenize_sb(cpi, t, !output_enabled, MAX(bsize, BLOCK_8X8));
3604 } else {
3605 mbmi->skip = 1;
3606 if (output_enabled && !seg_skip)
3607 cm->counts.skip[vp9_get_skip_context(xd)][1]++;
3608 reset_skip_context(xd, MAX(bsize, BLOCK_8X8));
3609 }
3610 }
3611
3612 if (output_enabled) {
3613 if (cm->tx_mode == TX_MODE_SELECT &&
3614 mbmi->sb_type >= BLOCK_8X8 &&
3615 !(is_inter_block(mbmi) && (mbmi->skip || seg_skip))) {
3616 ++get_tx_counts(max_txsize_lookup[bsize], vp9_get_tx_size_context(xd),
3617 &cm->counts.tx)[mbmi->tx_size];
3618 } else {
3619 int x, y;
3620 TX_SIZE tx_size;
3621 // The new intra coding scheme requires no change of transform size
3622 if (is_inter_block(&mi->mbmi)) {
3623 tx_size = MIN(tx_mode_to_biggest_tx_size[cm->tx_mode],
3624 max_txsize_lookup[bsize]);
3625 } else {
3626 tx_size = (bsize >= BLOCK_8X8) ? mbmi->tx_size : TX_4X4;
3627 }
3628
3629 for (y = 0; y < mi_height; y++)
3630 for (x = 0; x < mi_width; x++)
3631 if (mi_col + x < cm->mi_cols && mi_row + y < cm->mi_rows)
3632 mi_8x8[mis * y + x]->mbmi.tx_size = tx_size;
3633 }
3634 }
3635 }
3636