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