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 <assert.h> 12 #include <stdio.h> 13 #include <math.h> 14 #include <limits.h> 15 #include <assert.h> 16 #include "vpx_config.h" 17 #include "vp8_rtcd.h" 18 #include "./vpx_dsp_rtcd.h" 19 #include "tokenize.h" 20 #include "treewriter.h" 21 #include "onyx_int.h" 22 #include "modecosts.h" 23 #include "encodeintra.h" 24 #include "pickinter.h" 25 #include "vp8/common/entropymode.h" 26 #include "vp8/common/reconinter.h" 27 #include "vp8/common/reconintra.h" 28 #include "vp8/common/reconintra4x4.h" 29 #include "vp8/common/findnearmv.h" 30 #include "vp8/common/quant_common.h" 31 #include "encodemb.h" 32 #include "vp8/encoder/quantize.h" 33 #include "vpx_dsp/variance.h" 34 #include "vpx_ports/system_state.h" 35 #include "mcomp.h" 36 #include "rdopt.h" 37 #include "vpx_mem/vpx_mem.h" 38 #include "vp8/common/systemdependent.h" 39 #if CONFIG_TEMPORAL_DENOISING 40 #include "denoising.h" 41 #endif 42 extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x); 43 44 #define MAXF(a, b) (((a) > (b)) ? (a) : (b)) 45 46 typedef struct rate_distortion_struct { 47 int rate2; 48 int rate_y; 49 int rate_uv; 50 int distortion2; 51 int distortion_uv; 52 } RATE_DISTORTION; 53 54 typedef struct best_mode_struct { 55 int yrd; 56 int rd; 57 int intra_rd; 58 MB_MODE_INFO mbmode; 59 union b_mode_info bmodes[16]; 60 PARTITION_INFO partition; 61 } BEST_MODE; 62 63 static const int auto_speed_thresh[17] = { 1000, 200, 150, 130, 150, 125, 64 120, 115, 115, 115, 115, 115, 65 115, 115, 115, 115, 105 }; 66 67 const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] = { 68 ZEROMV, DC_PRED, 69 70 NEARESTMV, NEARMV, 71 72 ZEROMV, NEARESTMV, 73 74 ZEROMV, NEARESTMV, 75 76 NEARMV, NEARMV, 77 78 V_PRED, H_PRED, TM_PRED, 79 80 NEWMV, NEWMV, NEWMV, 81 82 SPLITMV, SPLITMV, SPLITMV, 83 84 B_PRED, 85 }; 86 87 /* This table determines the search order in reference frame priority order, 88 * which may not necessarily match INTRA,LAST,GOLDEN,ARF 89 */ 90 const int vp8_ref_frame_order[MAX_MODES] = { 91 1, 0, 92 93 1, 1, 94 95 2, 2, 96 97 3, 3, 98 99 2, 3, 100 101 0, 0, 0, 102 103 1, 2, 3, 104 105 1, 2, 3, 106 107 0, 108 }; 109 110 static void fill_token_costs( 111 int c[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS], 112 const vp8_prob p[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] 113 [ENTROPY_NODES]) { 114 int i, j, k; 115 116 for (i = 0; i < BLOCK_TYPES; ++i) { 117 for (j = 0; j < COEF_BANDS; ++j) { 118 for (k = 0; k < PREV_COEF_CONTEXTS; ++k) { 119 /* check for pt=0 and band > 1 if block type 0 120 * and 0 if blocktype 1 121 */ 122 if (k == 0 && j > (i == 0)) { 123 vp8_cost_tokens2(c[i][j][k], p[i][j][k], vp8_coef_tree, 2); 124 } else { 125 vp8_cost_tokens(c[i][j][k], p[i][j][k], vp8_coef_tree); 126 } 127 } 128 } 129 } 130 } 131 132 static const int rd_iifactor[32] = { 4, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 135 136 /* values are now correlated to quantizer */ 137 static const int sad_per_bit16lut[QINDEX_RANGE] = { 138 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 139 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 140 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 141 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 142 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 143 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 144 11, 11, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14 145 }; 146 static const int sad_per_bit4lut[QINDEX_RANGE] = { 147 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 148 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 149 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 150 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 151 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 152 12, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 153 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 154 }; 155 156 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex) { 157 cpi->mb.sadperbit16 = sad_per_bit16lut[QIndex]; 158 cpi->mb.sadperbit4 = sad_per_bit4lut[QIndex]; 159 } 160 161 void vp8_initialize_rd_consts(VP8_COMP *cpi, MACROBLOCK *x, int Qvalue) { 162 int q; 163 int i; 164 double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0; 165 double rdconst = 2.80; 166 167 vpx_clear_system_state(); 168 169 /* Further tests required to see if optimum is different 170 * for key frames, golden frames and arf frames. 171 */ 172 cpi->RDMULT = (int)(rdconst * (capped_q * capped_q)); 173 174 /* Extend rate multiplier along side quantizer zbin increases */ 175 if (cpi->mb.zbin_over_quant > 0) { 176 double oq_factor; 177 double modq; 178 179 /* Experimental code using the same basic equation as used for Q above 180 * The units of cpi->mb.zbin_over_quant are 1/128 of Q bin size 181 */ 182 oq_factor = 1.0 + ((double)0.0015625 * cpi->mb.zbin_over_quant); 183 modq = (int)((double)capped_q * oq_factor); 184 cpi->RDMULT = (int)(rdconst * (modq * modq)); 185 } 186 187 if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME)) { 188 if (cpi->twopass.next_iiratio > 31) { 189 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4; 190 } else { 191 cpi->RDMULT += 192 (cpi->RDMULT * rd_iifactor[cpi->twopass.next_iiratio]) >> 4; 193 } 194 } 195 196 cpi->mb.errorperbit = (cpi->RDMULT / 110); 197 cpi->mb.errorperbit += (cpi->mb.errorperbit == 0); 198 199 vp8_set_speed_features(cpi); 200 201 for (i = 0; i < MAX_MODES; ++i) { 202 x->mode_test_hit_counts[i] = 0; 203 } 204 205 q = (int)pow(Qvalue, 1.25); 206 207 if (q < 8) q = 8; 208 209 if (cpi->RDMULT > 1000) { 210 cpi->RDDIV = 1; 211 cpi->RDMULT /= 100; 212 213 for (i = 0; i < MAX_MODES; ++i) { 214 if (cpi->sf.thresh_mult[i] < INT_MAX) { 215 x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100; 216 } else { 217 x->rd_threshes[i] = INT_MAX; 218 } 219 220 cpi->rd_baseline_thresh[i] = x->rd_threshes[i]; 221 } 222 } else { 223 cpi->RDDIV = 100; 224 225 for (i = 0; i < MAX_MODES; ++i) { 226 if (cpi->sf.thresh_mult[i] < (INT_MAX / q)) { 227 x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q; 228 } else { 229 x->rd_threshes[i] = INT_MAX; 230 } 231 232 cpi->rd_baseline_thresh[i] = x->rd_threshes[i]; 233 } 234 } 235 236 { 237 /* build token cost array for the type of frame we have now */ 238 FRAME_CONTEXT *l = &cpi->lfc_n; 239 240 if (cpi->common.refresh_alt_ref_frame) { 241 l = &cpi->lfc_a; 242 } else if (cpi->common.refresh_golden_frame) { 243 l = &cpi->lfc_g; 244 } 245 246 fill_token_costs(cpi->mb.token_costs, 247 (const vp8_prob(*)[8][3][11])l->coef_probs); 248 /* 249 fill_token_costs( 250 cpi->mb.token_costs, 251 (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs); 252 */ 253 254 /* TODO make these mode costs depend on last,alt or gold too. (jbb) */ 255 vp8_init_mode_costs(cpi); 256 } 257 } 258 259 void vp8_auto_select_speed(VP8_COMP *cpi) { 260 int milliseconds_for_compress = (int)(1000000 / cpi->framerate); 261 262 milliseconds_for_compress = 263 milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16; 264 265 #if 0 266 267 if (0) 268 { 269 FILE *f; 270 271 f = fopen("speed.stt", "a"); 272 fprintf(f, " %8ld %10ld %10ld %10ld\n", 273 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time); 274 fclose(f); 275 } 276 277 #endif 278 279 if (cpi->avg_pick_mode_time < milliseconds_for_compress && 280 (cpi->avg_encode_time - cpi->avg_pick_mode_time) < 281 milliseconds_for_compress) { 282 if (cpi->avg_pick_mode_time == 0) { 283 cpi->Speed = 4; 284 } else { 285 if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95) { 286 cpi->Speed += 2; 287 cpi->avg_pick_mode_time = 0; 288 cpi->avg_encode_time = 0; 289 290 if (cpi->Speed > 16) { 291 cpi->Speed = 16; 292 } 293 } 294 295 if (milliseconds_for_compress * 100 > 296 cpi->avg_encode_time * auto_speed_thresh[cpi->Speed]) { 297 cpi->Speed -= 1; 298 cpi->avg_pick_mode_time = 0; 299 cpi->avg_encode_time = 0; 300 301 /* In real-time mode, cpi->speed is in [4, 16]. */ 302 if (cpi->Speed < 4) { 303 cpi->Speed = 4; 304 } 305 } 306 } 307 } else { 308 cpi->Speed += 4; 309 310 if (cpi->Speed > 16) cpi->Speed = 16; 311 312 cpi->avg_pick_mode_time = 0; 313 cpi->avg_encode_time = 0; 314 } 315 } 316 317 int vp8_block_error_c(short *coeff, short *dqcoeff) { 318 int i; 319 int error = 0; 320 321 for (i = 0; i < 16; ++i) { 322 int this_diff = coeff[i] - dqcoeff[i]; 323 error += this_diff * this_diff; 324 } 325 326 return error; 327 } 328 329 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc) { 330 BLOCK *be; 331 BLOCKD *bd; 332 int i, j; 333 int berror, error = 0; 334 335 for (i = 0; i < 16; ++i) { 336 be = &mb->block[i]; 337 bd = &mb->e_mbd.block[i]; 338 339 berror = 0; 340 341 for (j = dc; j < 16; ++j) { 342 int this_diff = be->coeff[j] - bd->dqcoeff[j]; 343 berror += this_diff * this_diff; 344 } 345 346 error += berror; 347 } 348 349 return error; 350 } 351 352 int vp8_mbuverror_c(MACROBLOCK *mb) { 353 BLOCK *be; 354 BLOCKD *bd; 355 356 int i; 357 int error = 0; 358 359 for (i = 16; i < 24; ++i) { 360 be = &mb->block[i]; 361 bd = &mb->e_mbd.block[i]; 362 363 error += vp8_block_error_c(be->coeff, bd->dqcoeff); 364 } 365 366 return error; 367 } 368 369 int VP8_UVSSE(MACROBLOCK *x) { 370 unsigned char *uptr, *vptr; 371 unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src); 372 unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src); 373 int uv_stride = x->block[16].src_stride; 374 375 unsigned int sse1 = 0; 376 unsigned int sse2 = 0; 377 int mv_row = x->e_mbd.mode_info_context->mbmi.mv.as_mv.row; 378 int mv_col = x->e_mbd.mode_info_context->mbmi.mv.as_mv.col; 379 int offset; 380 int pre_stride = x->e_mbd.pre.uv_stride; 381 382 if (mv_row < 0) { 383 mv_row -= 1; 384 } else { 385 mv_row += 1; 386 } 387 388 if (mv_col < 0) { 389 mv_col -= 1; 390 } else { 391 mv_col += 1; 392 } 393 394 mv_row /= 2; 395 mv_col /= 2; 396 397 offset = (mv_row >> 3) * pre_stride + (mv_col >> 3); 398 uptr = x->e_mbd.pre.u_buffer + offset; 399 vptr = x->e_mbd.pre.v_buffer + offset; 400 401 if ((mv_row | mv_col) & 7) { 402 vpx_sub_pixel_variance8x8(uptr, pre_stride, mv_col & 7, mv_row & 7, 403 upred_ptr, uv_stride, &sse2); 404 vpx_sub_pixel_variance8x8(vptr, pre_stride, mv_col & 7, mv_row & 7, 405 vpred_ptr, uv_stride, &sse1); 406 sse2 += sse1; 407 } else { 408 vpx_variance8x8(uptr, pre_stride, upred_ptr, uv_stride, &sse2); 409 vpx_variance8x8(vptr, pre_stride, vpred_ptr, uv_stride, &sse1); 410 sse2 += sse1; 411 } 412 return sse2; 413 } 414 415 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, 416 ENTROPY_CONTEXT *l) { 417 int c = !type; /* start at coef 0, unless Y with Y2 */ 418 int eob = (int)(*b->eob); 419 int pt; /* surrounding block/prev coef predictor */ 420 int cost = 0; 421 short *qcoeff_ptr = b->qcoeff; 422 423 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l); 424 425 assert(eob <= 16); 426 for (; c < eob; ++c) { 427 const int v = qcoeff_ptr[vp8_default_zig_zag1d[c]]; 428 const int t = vp8_dct_value_tokens_ptr[v].Token; 429 cost += mb->token_costs[type][vp8_coef_bands[c]][pt][t]; 430 cost += vp8_dct_value_cost_ptr[v]; 431 pt = vp8_prev_token_class[t]; 432 } 433 434 if (c < 16) { 435 cost += mb->token_costs[type][vp8_coef_bands[c]][pt][DCT_EOB_TOKEN]; 436 } 437 438 pt = (c != !type); /* is eob first coefficient; */ 439 *a = *l = pt; 440 441 return cost; 442 } 443 444 static int vp8_rdcost_mby(MACROBLOCK *mb) { 445 int cost = 0; 446 int b; 447 MACROBLOCKD *x = &mb->e_mbd; 448 ENTROPY_CONTEXT_PLANES t_above, t_left; 449 ENTROPY_CONTEXT *ta; 450 ENTROPY_CONTEXT *tl; 451 452 memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES)); 453 memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES)); 454 455 ta = (ENTROPY_CONTEXT *)&t_above; 456 tl = (ENTROPY_CONTEXT *)&t_left; 457 458 for (b = 0; b < 16; ++b) { 459 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC, 460 ta + vp8_block2above[b], tl + vp8_block2left[b]); 461 } 462 463 cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2, 464 ta + vp8_block2above[24], tl + vp8_block2left[24]); 465 466 return cost; 467 } 468 469 static void macro_block_yrd(MACROBLOCK *mb, int *Rate, int *Distortion) { 470 int b; 471 MACROBLOCKD *const x = &mb->e_mbd; 472 BLOCK *const mb_y2 = mb->block + 24; 473 BLOCKD *const x_y2 = x->block + 24; 474 short *Y2DCPtr = mb_y2->src_diff; 475 BLOCK *beptr; 476 int d; 477 478 vp8_subtract_mby(mb->src_diff, *(mb->block[0].base_src), 479 mb->block[0].src_stride, mb->e_mbd.predictor, 16); 480 481 /* Fdct and building the 2nd order block */ 482 for (beptr = mb->block; beptr < mb->block + 16; beptr += 2) { 483 mb->short_fdct8x4(beptr->src_diff, beptr->coeff, 32); 484 *Y2DCPtr++ = beptr->coeff[0]; 485 *Y2DCPtr++ = beptr->coeff[16]; 486 } 487 488 /* 2nd order fdct */ 489 mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8); 490 491 /* Quantization */ 492 for (b = 0; b < 16; ++b) { 493 mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]); 494 } 495 496 /* DC predication and Quantization of 2nd Order block */ 497 mb->quantize_b(mb_y2, x_y2); 498 499 /* Distortion */ 500 d = vp8_mbblock_error(mb, 1) << 2; 501 d += vp8_block_error(mb_y2->coeff, x_y2->dqcoeff); 502 503 *Distortion = (d >> 4); 504 505 /* rate */ 506 *Rate = vp8_rdcost_mby(mb); 507 } 508 509 static void copy_predictor(unsigned char *dst, const unsigned char *predictor) { 510 const unsigned int *p = (const unsigned int *)predictor; 511 unsigned int *d = (unsigned int *)dst; 512 d[0] = p[0]; 513 d[4] = p[4]; 514 d[8] = p[8]; 515 d[12] = p[12]; 516 } 517 static int rd_pick_intra4x4block(MACROBLOCK *x, BLOCK *be, BLOCKD *b, 518 B_PREDICTION_MODE *best_mode, 519 const int *bmode_costs, ENTROPY_CONTEXT *a, 520 ENTROPY_CONTEXT *l, 521 522 int *bestrate, int *bestratey, 523 int *bestdistortion) { 524 B_PREDICTION_MODE mode; 525 int best_rd = INT_MAX; 526 int rate = 0; 527 int distortion; 528 529 ENTROPY_CONTEXT ta = *a, tempa = *a; 530 ENTROPY_CONTEXT tl = *l, templ = *l; 531 /* 532 * The predictor buffer is a 2d buffer with a stride of 16. Create 533 * a temp buffer that meets the stride requirements, but we are only 534 * interested in the left 4x4 block 535 * */ 536 DECLARE_ALIGNED(16, unsigned char, best_predictor[16 * 4]); 537 DECLARE_ALIGNED(16, short, best_dqcoeff[16]); 538 int dst_stride = x->e_mbd.dst.y_stride; 539 unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset; 540 541 unsigned char *Above = dst - dst_stride; 542 unsigned char *yleft = dst - 1; 543 unsigned char top_left = Above[-1]; 544 545 for (mode = B_DC_PRED; mode <= B_HU_PRED; ++mode) { 546 int this_rd; 547 int ratey; 548 549 rate = bmode_costs[mode]; 550 551 vp8_intra4x4_predict(Above, yleft, dst_stride, mode, b->predictor, 16, 552 top_left); 553 vp8_subtract_b(be, b, 16); 554 x->short_fdct4x4(be->src_diff, be->coeff, 32); 555 x->quantize_b(be, b); 556 557 tempa = ta; 558 templ = tl; 559 560 ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ); 561 rate += ratey; 562 distortion = vp8_block_error(be->coeff, b->dqcoeff) >> 2; 563 564 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion); 565 566 if (this_rd < best_rd) { 567 *bestrate = rate; 568 *bestratey = ratey; 569 *bestdistortion = distortion; 570 best_rd = this_rd; 571 *best_mode = mode; 572 *a = tempa; 573 *l = templ; 574 copy_predictor(best_predictor, b->predictor); 575 memcpy(best_dqcoeff, b->dqcoeff, 32); 576 } 577 } 578 b->bmi.as_mode = *best_mode; 579 580 vp8_short_idct4x4llm(best_dqcoeff, best_predictor, 16, dst, dst_stride); 581 582 return best_rd; 583 } 584 585 static int rd_pick_intra4x4mby_modes(MACROBLOCK *mb, int *Rate, int *rate_y, 586 int *Distortion, int best_rd) { 587 MACROBLOCKD *const xd = &mb->e_mbd; 588 int i; 589 int cost = mb->mbmode_cost[xd->frame_type][B_PRED]; 590 int distortion = 0; 591 int tot_rate_y = 0; 592 int64_t total_rd = 0; 593 ENTROPY_CONTEXT_PLANES t_above, t_left; 594 ENTROPY_CONTEXT *ta; 595 ENTROPY_CONTEXT *tl; 596 const int *bmode_costs; 597 598 memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES)); 599 memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES)); 600 601 ta = (ENTROPY_CONTEXT *)&t_above; 602 tl = (ENTROPY_CONTEXT *)&t_left; 603 604 intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16); 605 606 bmode_costs = mb->inter_bmode_costs; 607 608 for (i = 0; i < 16; ++i) { 609 MODE_INFO *const mic = xd->mode_info_context; 610 const int mis = xd->mode_info_stride; 611 B_PREDICTION_MODE best_mode = B_MODE_COUNT; 612 int r = 0, ry = 0, d = 0; 613 614 if (mb->e_mbd.frame_type == KEY_FRAME) { 615 const B_PREDICTION_MODE A = above_block_mode(mic, i, mis); 616 const B_PREDICTION_MODE L = left_block_mode(mic, i); 617 618 bmode_costs = mb->bmode_costs[A][L]; 619 } 620 621 total_rd += rd_pick_intra4x4block( 622 mb, mb->block + i, xd->block + i, &best_mode, bmode_costs, 623 ta + vp8_block2above[i], tl + vp8_block2left[i], &r, &ry, &d); 624 625 cost += r; 626 distortion += d; 627 tot_rate_y += ry; 628 629 assert(best_mode != B_MODE_COUNT); 630 mic->bmi[i].as_mode = best_mode; 631 632 if (total_rd >= (int64_t)best_rd) break; 633 } 634 635 if (total_rd >= (int64_t)best_rd) return INT_MAX; 636 637 *Rate = cost; 638 *rate_y = tot_rate_y; 639 *Distortion = distortion; 640 641 return RDCOST(mb->rdmult, mb->rddiv, cost, distortion); 642 } 643 644 static int rd_pick_intra16x16mby_mode(MACROBLOCK *x, int *Rate, int *rate_y, 645 int *Distortion) { 646 MB_PREDICTION_MODE mode; 647 MB_PREDICTION_MODE mode_selected = MB_MODE_COUNT; 648 int rate, ratey; 649 int distortion; 650 int best_rd = INT_MAX; 651 int this_rd; 652 MACROBLOCKD *xd = &x->e_mbd; 653 654 /* Y Search for 16x16 intra prediction mode */ 655 for (mode = DC_PRED; mode <= TM_PRED; ++mode) { 656 xd->mode_info_context->mbmi.mode = mode; 657 658 vp8_build_intra_predictors_mby_s(xd, xd->dst.y_buffer - xd->dst.y_stride, 659 xd->dst.y_buffer - 1, xd->dst.y_stride, 660 xd->predictor, 16); 661 662 macro_block_yrd(x, &ratey, &distortion); 663 rate = ratey + 664 x->mbmode_cost[xd->frame_type][xd->mode_info_context->mbmi.mode]; 665 666 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion); 667 668 if (this_rd < best_rd) { 669 mode_selected = mode; 670 best_rd = this_rd; 671 *Rate = rate; 672 *rate_y = ratey; 673 *Distortion = distortion; 674 } 675 } 676 677 assert(mode_selected != MB_MODE_COUNT); 678 xd->mode_info_context->mbmi.mode = mode_selected; 679 return best_rd; 680 } 681 682 static int rd_cost_mbuv(MACROBLOCK *mb) { 683 int b; 684 int cost = 0; 685 MACROBLOCKD *x = &mb->e_mbd; 686 ENTROPY_CONTEXT_PLANES t_above, t_left; 687 ENTROPY_CONTEXT *ta; 688 ENTROPY_CONTEXT *tl; 689 690 memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES)); 691 memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES)); 692 693 ta = (ENTROPY_CONTEXT *)&t_above; 694 tl = (ENTROPY_CONTEXT *)&t_left; 695 696 for (b = 16; b < 24; ++b) { 697 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_UV, 698 ta + vp8_block2above[b], tl + vp8_block2left[b]); 699 } 700 701 return cost; 702 } 703 704 static int rd_inter16x16_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, 705 int *distortion, int fullpixel) { 706 (void)cpi; 707 (void)fullpixel; 708 709 vp8_build_inter16x16_predictors_mbuv(&x->e_mbd); 710 vp8_subtract_mbuv(x->src_diff, x->src.u_buffer, x->src.v_buffer, 711 x->src.uv_stride, &x->e_mbd.predictor[256], 712 &x->e_mbd.predictor[320], 8); 713 714 vp8_transform_mbuv(x); 715 vp8_quantize_mbuv(x); 716 717 *rate = rd_cost_mbuv(x); 718 *distortion = vp8_mbuverror(x) / 4; 719 720 return RDCOST(x->rdmult, x->rddiv, *rate, *distortion); 721 } 722 723 static int rd_inter4x4_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, 724 int *distortion, int fullpixel) { 725 (void)cpi; 726 (void)fullpixel; 727 728 vp8_build_inter4x4_predictors_mbuv(&x->e_mbd); 729 vp8_subtract_mbuv(x->src_diff, x->src.u_buffer, x->src.v_buffer, 730 x->src.uv_stride, &x->e_mbd.predictor[256], 731 &x->e_mbd.predictor[320], 8); 732 733 vp8_transform_mbuv(x); 734 vp8_quantize_mbuv(x); 735 736 *rate = rd_cost_mbuv(x); 737 *distortion = vp8_mbuverror(x) / 4; 738 739 return RDCOST(x->rdmult, x->rddiv, *rate, *distortion); 740 } 741 742 static void rd_pick_intra_mbuv_mode(MACROBLOCK *x, int *rate, 743 int *rate_tokenonly, int *distortion) { 744 MB_PREDICTION_MODE mode; 745 MB_PREDICTION_MODE mode_selected = MB_MODE_COUNT; 746 int best_rd = INT_MAX; 747 int d = 0, r = 0; 748 int rate_to; 749 MACROBLOCKD *xd = &x->e_mbd; 750 751 for (mode = DC_PRED; mode <= TM_PRED; ++mode) { 752 int this_rate; 753 int this_distortion; 754 int this_rd; 755 756 xd->mode_info_context->mbmi.uv_mode = mode; 757 758 vp8_build_intra_predictors_mbuv_s( 759 xd, xd->dst.u_buffer - xd->dst.uv_stride, 760 xd->dst.v_buffer - xd->dst.uv_stride, xd->dst.u_buffer - 1, 761 xd->dst.v_buffer - 1, xd->dst.uv_stride, &xd->predictor[256], 762 &xd->predictor[320], 8); 763 764 vp8_subtract_mbuv(x->src_diff, x->src.u_buffer, x->src.v_buffer, 765 x->src.uv_stride, &xd->predictor[256], 766 &xd->predictor[320], 8); 767 vp8_transform_mbuv(x); 768 vp8_quantize_mbuv(x); 769 770 rate_to = rd_cost_mbuv(x); 771 this_rate = rate_to + 772 x->intra_uv_mode_cost[xd->frame_type] 773 [xd->mode_info_context->mbmi.uv_mode]; 774 775 this_distortion = vp8_mbuverror(x) / 4; 776 777 this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); 778 779 if (this_rd < best_rd) { 780 best_rd = this_rd; 781 d = this_distortion; 782 r = this_rate; 783 *rate_tokenonly = rate_to; 784 mode_selected = mode; 785 } 786 } 787 788 *rate = r; 789 *distortion = d; 790 791 assert(mode_selected != MB_MODE_COUNT); 792 xd->mode_info_context->mbmi.uv_mode = mode_selected; 793 } 794 795 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]) { 796 vp8_prob p[VP8_MVREFS - 1]; 797 assert(NEARESTMV <= m && m <= SPLITMV); 798 vp8_mv_ref_probs(p, near_mv_ref_ct); 799 return vp8_cost_token(vp8_mv_ref_tree, p, 800 vp8_mv_ref_encoding_array + (m - NEARESTMV)); 801 } 802 803 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv) { 804 x->e_mbd.mode_info_context->mbmi.mode = mb; 805 x->e_mbd.mode_info_context->mbmi.mv.as_int = mv->as_int; 806 } 807 808 static int labels2mode(MACROBLOCK *x, int const *labelings, int which_label, 809 B_PREDICTION_MODE this_mode, int_mv *this_mv, 810 int_mv *best_ref_mv, int *mvcost[2]) { 811 MACROBLOCKD *const xd = &x->e_mbd; 812 MODE_INFO *const mic = xd->mode_info_context; 813 const int mis = xd->mode_info_stride; 814 815 int cost = 0; 816 int thismvcost = 0; 817 818 /* We have to be careful retrieving previously-encoded motion vectors. 819 Ones from this macroblock have to be pulled from the BLOCKD array 820 as they have not yet made it to the bmi array in our MB_MODE_INFO. */ 821 822 int i = 0; 823 824 do { 825 BLOCKD *const d = xd->block + i; 826 const int row = i >> 2, col = i & 3; 827 828 B_PREDICTION_MODE m; 829 830 if (labelings[i] != which_label) continue; 831 832 if (col && labelings[i] == labelings[i - 1]) { 833 m = LEFT4X4; 834 } else if (row && labelings[i] == labelings[i - 4]) { 835 m = ABOVE4X4; 836 } else { 837 /* the only time we should do costing for new motion vector 838 * or mode is when we are on a new label (jbb May 08, 2007) 839 */ 840 switch (m = this_mode) { 841 case NEW4X4: 842 thismvcost = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102); 843 break; 844 case LEFT4X4: 845 this_mv->as_int = col ? d[-1].bmi.mv.as_int : left_block_mv(mic, i); 846 break; 847 case ABOVE4X4: 848 this_mv->as_int = 849 row ? d[-4].bmi.mv.as_int : above_block_mv(mic, i, mis); 850 break; 851 case ZERO4X4: this_mv->as_int = 0; break; 852 default: break; 853 } 854 855 if (m == ABOVE4X4) /* replace above with left if same */ 856 { 857 int_mv left_mv; 858 859 left_mv.as_int = col ? d[-1].bmi.mv.as_int : left_block_mv(mic, i); 860 861 if (left_mv.as_int == this_mv->as_int) m = LEFT4X4; 862 } 863 864 cost = x->inter_bmode_costs[m]; 865 } 866 867 d->bmi.mv.as_int = this_mv->as_int; 868 869 x->partition_info->bmi[i].mode = m; 870 x->partition_info->bmi[i].mv.as_int = this_mv->as_int; 871 872 } while (++i < 16); 873 874 cost += thismvcost; 875 return cost; 876 } 877 878 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels, 879 int which_label, ENTROPY_CONTEXT *ta, 880 ENTROPY_CONTEXT *tl) { 881 int cost = 0; 882 int b; 883 MACROBLOCKD *x = &mb->e_mbd; 884 885 for (b = 0; b < 16; ++b) { 886 if (labels[b] == which_label) { 887 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_WITH_DC, 888 ta + vp8_block2above[b], tl + vp8_block2left[b]); 889 } 890 } 891 892 return cost; 893 } 894 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, 895 int const *labels, 896 int which_label) { 897 int i; 898 unsigned int distortion = 0; 899 int pre_stride = x->e_mbd.pre.y_stride; 900 unsigned char *base_pre = x->e_mbd.pre.y_buffer; 901 902 for (i = 0; i < 16; ++i) { 903 if (labels[i] == which_label) { 904 BLOCKD *bd = &x->e_mbd.block[i]; 905 BLOCK *be = &x->block[i]; 906 907 vp8_build_inter_predictors_b(bd, 16, base_pre, pre_stride, 908 x->e_mbd.subpixel_predict); 909 vp8_subtract_b(be, bd, 16); 910 x->short_fdct4x4(be->src_diff, be->coeff, 32); 911 x->quantize_b(be, bd); 912 913 distortion += vp8_block_error(be->coeff, bd->dqcoeff); 914 } 915 } 916 917 return distortion; 918 } 919 920 static const unsigned int segmentation_to_sseshift[4] = { 3, 3, 2, 0 }; 921 922 typedef struct { 923 int_mv *ref_mv; 924 int_mv mvp; 925 926 int segment_rd; 927 int segment_num; 928 int r; 929 int d; 930 int segment_yrate; 931 B_PREDICTION_MODE modes[16]; 932 int_mv mvs[16]; 933 unsigned char eobs[16]; 934 935 int mvthresh; 936 int *mdcounts; 937 938 int_mv sv_mvp[4]; /* save 4 mvp from 8x8 */ 939 int sv_istep[2]; /* save 2 initial step_param for 16x8/8x16 */ 940 941 } BEST_SEG_INFO; 942 943 static void rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x, BEST_SEG_INFO *bsi, 944 unsigned int segmentation) { 945 int i; 946 int const *labels; 947 int br = 0; 948 int bd = 0; 949 B_PREDICTION_MODE this_mode; 950 951 int label_count; 952 int this_segment_rd = 0; 953 int label_mv_thresh; 954 int rate = 0; 955 int sbr = 0; 956 int sbd = 0; 957 int segmentyrate = 0; 958 959 vp8_variance_fn_ptr_t *v_fn_ptr; 960 961 ENTROPY_CONTEXT_PLANES t_above, t_left; 962 ENTROPY_CONTEXT *ta; 963 ENTROPY_CONTEXT *tl; 964 ENTROPY_CONTEXT_PLANES t_above_b, t_left_b; 965 ENTROPY_CONTEXT *ta_b; 966 ENTROPY_CONTEXT *tl_b; 967 968 memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES)); 969 memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES)); 970 971 ta = (ENTROPY_CONTEXT *)&t_above; 972 tl = (ENTROPY_CONTEXT *)&t_left; 973 ta_b = (ENTROPY_CONTEXT *)&t_above_b; 974 tl_b = (ENTROPY_CONTEXT *)&t_left_b; 975 976 br = 0; 977 bd = 0; 978 979 v_fn_ptr = &cpi->fn_ptr[segmentation]; 980 labels = vp8_mbsplits[segmentation]; 981 label_count = vp8_mbsplit_count[segmentation]; 982 983 /* 64 makes this threshold really big effectively making it so that we 984 * very rarely check mvs on segments. setting this to 1 would make mv 985 * thresh roughly equal to what it is for macroblocks 986 */ 987 label_mv_thresh = 1 * bsi->mvthresh / label_count; 988 989 /* Segmentation method overheads */ 990 rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, 991 vp8_mbsplit_encodings + segmentation); 992 rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts); 993 this_segment_rd += RDCOST(x->rdmult, x->rddiv, rate, 0); 994 br += rate; 995 996 for (i = 0; i < label_count; ++i) { 997 int_mv mode_mv[B_MODE_COUNT]; 998 int best_label_rd = INT_MAX; 999 B_PREDICTION_MODE mode_selected = ZERO4X4; 1000 int bestlabelyrate = 0; 1001 1002 /* search for the best motion vector on this segment */ 1003 for (this_mode = LEFT4X4; this_mode <= NEW4X4; ++this_mode) { 1004 int this_rd; 1005 int distortion; 1006 int labelyrate; 1007 ENTROPY_CONTEXT_PLANES t_above_s, t_left_s; 1008 ENTROPY_CONTEXT *ta_s; 1009 ENTROPY_CONTEXT *tl_s; 1010 1011 memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES)); 1012 memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES)); 1013 1014 ta_s = (ENTROPY_CONTEXT *)&t_above_s; 1015 tl_s = (ENTROPY_CONTEXT *)&t_left_s; 1016 1017 if (this_mode == NEW4X4) { 1018 int sseshift; 1019 int num00; 1020 int step_param = 0; 1021 int further_steps; 1022 int n; 1023 int thissme; 1024 int bestsme = INT_MAX; 1025 int_mv temp_mv; 1026 BLOCK *c; 1027 BLOCKD *e; 1028 1029 /* Is the best so far sufficiently good that we cant justify 1030 * doing a new motion search. 1031 */ 1032 if (best_label_rd < label_mv_thresh) break; 1033 1034 if (cpi->compressor_speed) { 1035 if (segmentation == BLOCK_8X16 || segmentation == BLOCK_16X8) { 1036 bsi->mvp.as_int = bsi->sv_mvp[i].as_int; 1037 if (i == 1 && segmentation == BLOCK_16X8) { 1038 bsi->mvp.as_int = bsi->sv_mvp[2].as_int; 1039 } 1040 1041 step_param = bsi->sv_istep[i]; 1042 } 1043 1044 /* use previous block's result as next block's MV 1045 * predictor. 1046 */ 1047 if (segmentation == BLOCK_4X4 && i > 0) { 1048 bsi->mvp.as_int = x->e_mbd.block[i - 1].bmi.mv.as_int; 1049 if (i == 4 || i == 8 || i == 12) { 1050 bsi->mvp.as_int = x->e_mbd.block[i - 4].bmi.mv.as_int; 1051 } 1052 step_param = 2; 1053 } 1054 } 1055 1056 further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; 1057 1058 { 1059 int sadpb = x->sadperbit4; 1060 int_mv mvp_full; 1061 1062 mvp_full.as_mv.row = bsi->mvp.as_mv.row >> 3; 1063 mvp_full.as_mv.col = bsi->mvp.as_mv.col >> 3; 1064 1065 /* find first label */ 1066 n = vp8_mbsplit_offset[segmentation][i]; 1067 1068 c = &x->block[n]; 1069 e = &x->e_mbd.block[n]; 1070 1071 { 1072 bestsme = cpi->diamond_search_sad( 1073 x, c, e, &mvp_full, &mode_mv[NEW4X4], step_param, sadpb, &num00, 1074 v_fn_ptr, x->mvcost, bsi->ref_mv); 1075 1076 n = num00; 1077 num00 = 0; 1078 1079 while (n < further_steps) { 1080 n++; 1081 1082 if (num00) { 1083 num00--; 1084 } else { 1085 thissme = cpi->diamond_search_sad( 1086 x, c, e, &mvp_full, &temp_mv, step_param + n, sadpb, &num00, 1087 v_fn_ptr, x->mvcost, bsi->ref_mv); 1088 1089 if (thissme < bestsme) { 1090 bestsme = thissme; 1091 mode_mv[NEW4X4].as_int = temp_mv.as_int; 1092 } 1093 } 1094 } 1095 } 1096 1097 sseshift = segmentation_to_sseshift[segmentation]; 1098 1099 /* Should we do a full search (best quality only) */ 1100 if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000) { 1101 /* Check if mvp_full is within the range. */ 1102 vp8_clamp_mv(&mvp_full, x->mv_col_min, x->mv_col_max, x->mv_row_min, 1103 x->mv_row_max); 1104 1105 thissme = cpi->full_search_sad(x, c, e, &mvp_full, sadpb, 16, 1106 v_fn_ptr, x->mvcost, bsi->ref_mv); 1107 1108 if (thissme < bestsme) { 1109 bestsme = thissme; 1110 mode_mv[NEW4X4].as_int = e->bmi.mv.as_int; 1111 } else { 1112 /* The full search result is actually worse so 1113 * re-instate the previous best vector 1114 */ 1115 e->bmi.mv.as_int = mode_mv[NEW4X4].as_int; 1116 } 1117 } 1118 } 1119 1120 if (bestsme < INT_MAX) { 1121 int disto; 1122 unsigned int sse; 1123 cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], bsi->ref_mv, 1124 x->errorperbit, v_fn_ptr, x->mvcost, 1125 &disto, &sse); 1126 } 1127 } /* NEW4X4 */ 1128 1129 rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode], 1130 bsi->ref_mv, x->mvcost); 1131 1132 /* Trap vectors that reach beyond the UMV borders */ 1133 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || 1134 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) || 1135 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || 1136 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max)) { 1137 continue; 1138 } 1139 1140 distortion = vp8_encode_inter_mb_segment(x, labels, i) / 4; 1141 1142 labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s); 1143 rate += labelyrate; 1144 1145 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion); 1146 1147 if (this_rd < best_label_rd) { 1148 sbr = rate; 1149 sbd = distortion; 1150 bestlabelyrate = labelyrate; 1151 mode_selected = this_mode; 1152 best_label_rd = this_rd; 1153 1154 memcpy(ta_b, ta_s, sizeof(ENTROPY_CONTEXT_PLANES)); 1155 memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES)); 1156 } 1157 } /*for each 4x4 mode*/ 1158 1159 memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES)); 1160 memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES)); 1161 1162 labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected], 1163 bsi->ref_mv, x->mvcost); 1164 1165 br += sbr; 1166 bd += sbd; 1167 segmentyrate += bestlabelyrate; 1168 this_segment_rd += best_label_rd; 1169 1170 if (this_segment_rd >= bsi->segment_rd) break; 1171 1172 } /* for each label */ 1173 1174 if (this_segment_rd < bsi->segment_rd) { 1175 bsi->r = br; 1176 bsi->d = bd; 1177 bsi->segment_yrate = segmentyrate; 1178 bsi->segment_rd = this_segment_rd; 1179 bsi->segment_num = segmentation; 1180 1181 /* store everything needed to come back to this!! */ 1182 for (i = 0; i < 16; ++i) { 1183 bsi->mvs[i].as_mv = x->partition_info->bmi[i].mv.as_mv; 1184 bsi->modes[i] = x->partition_info->bmi[i].mode; 1185 bsi->eobs[i] = x->e_mbd.eobs[i]; 1186 } 1187 } 1188 } 1189 1190 static void vp8_cal_step_param(int sr, int *sp) { 1191 int step = 0; 1192 1193 if (sr > MAX_FIRST_STEP) { 1194 sr = MAX_FIRST_STEP; 1195 } else if (sr < 1) { 1196 sr = 1; 1197 } 1198 1199 while (sr >>= 1) step++; 1200 1201 *sp = MAX_MVSEARCH_STEPS - 1 - step; 1202 } 1203 1204 static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, 1205 int_mv *best_ref_mv, int best_rd, 1206 int *mdcounts, int *returntotrate, 1207 int *returnyrate, 1208 int *returndistortion, 1209 int mvthresh) { 1210 int i; 1211 BEST_SEG_INFO bsi; 1212 1213 memset(&bsi, 0, sizeof(bsi)); 1214 1215 bsi.segment_rd = best_rd; 1216 bsi.ref_mv = best_ref_mv; 1217 bsi.mvp.as_int = best_ref_mv->as_int; 1218 bsi.mvthresh = mvthresh; 1219 bsi.mdcounts = mdcounts; 1220 1221 for (i = 0; i < 16; ++i) { 1222 bsi.modes[i] = ZERO4X4; 1223 } 1224 1225 if (cpi->compressor_speed == 0) { 1226 /* for now, we will keep the original segmentation order 1227 when in best quality mode */ 1228 rd_check_segment(cpi, x, &bsi, BLOCK_16X8); 1229 rd_check_segment(cpi, x, &bsi, BLOCK_8X16); 1230 rd_check_segment(cpi, x, &bsi, BLOCK_8X8); 1231 rd_check_segment(cpi, x, &bsi, BLOCK_4X4); 1232 } else { 1233 int sr; 1234 1235 rd_check_segment(cpi, x, &bsi, BLOCK_8X8); 1236 1237 if (bsi.segment_rd < best_rd) { 1238 int col_min = ((best_ref_mv->as_mv.col + 7) >> 3) - MAX_FULL_PEL_VAL; 1239 int row_min = ((best_ref_mv->as_mv.row + 7) >> 3) - MAX_FULL_PEL_VAL; 1240 int col_max = (best_ref_mv->as_mv.col >> 3) + MAX_FULL_PEL_VAL; 1241 int row_max = (best_ref_mv->as_mv.row >> 3) + MAX_FULL_PEL_VAL; 1242 1243 int tmp_col_min = x->mv_col_min; 1244 int tmp_col_max = x->mv_col_max; 1245 int tmp_row_min = x->mv_row_min; 1246 int tmp_row_max = x->mv_row_max; 1247 1248 /* Get intersection of UMV window and valid MV window to reduce # of 1249 * checks in diamond search. */ 1250 if (x->mv_col_min < col_min) x->mv_col_min = col_min; 1251 if (x->mv_col_max > col_max) x->mv_col_max = col_max; 1252 if (x->mv_row_min < row_min) x->mv_row_min = row_min; 1253 if (x->mv_row_max > row_max) x->mv_row_max = row_max; 1254 1255 /* Get 8x8 result */ 1256 bsi.sv_mvp[0].as_int = bsi.mvs[0].as_int; 1257 bsi.sv_mvp[1].as_int = bsi.mvs[2].as_int; 1258 bsi.sv_mvp[2].as_int = bsi.mvs[8].as_int; 1259 bsi.sv_mvp[3].as_int = bsi.mvs[10].as_int; 1260 1261 /* Use 8x8 result as 16x8/8x16's predictor MV. Adjust search range 1262 * according to the closeness of 2 MV. */ 1263 /* block 8X16 */ 1264 { 1265 sr = 1266 MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[2].as_mv.row)) >> 3, 1267 (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[2].as_mv.col)) >> 3); 1268 vp8_cal_step_param(sr, &bsi.sv_istep[0]); 1269 1270 sr = 1271 MAXF((abs(bsi.sv_mvp[1].as_mv.row - bsi.sv_mvp[3].as_mv.row)) >> 3, 1272 (abs(bsi.sv_mvp[1].as_mv.col - bsi.sv_mvp[3].as_mv.col)) >> 3); 1273 vp8_cal_step_param(sr, &bsi.sv_istep[1]); 1274 1275 rd_check_segment(cpi, x, &bsi, BLOCK_8X16); 1276 } 1277 1278 /* block 16X8 */ 1279 { 1280 sr = 1281 MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[1].as_mv.row)) >> 3, 1282 (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[1].as_mv.col)) >> 3); 1283 vp8_cal_step_param(sr, &bsi.sv_istep[0]); 1284 1285 sr = 1286 MAXF((abs(bsi.sv_mvp[2].as_mv.row - bsi.sv_mvp[3].as_mv.row)) >> 3, 1287 (abs(bsi.sv_mvp[2].as_mv.col - bsi.sv_mvp[3].as_mv.col)) >> 3); 1288 vp8_cal_step_param(sr, &bsi.sv_istep[1]); 1289 1290 rd_check_segment(cpi, x, &bsi, BLOCK_16X8); 1291 } 1292 1293 /* If 8x8 is better than 16x8/8x16, then do 4x4 search */ 1294 /* Not skip 4x4 if speed=0 (good quality) */ 1295 if (cpi->sf.no_skip_block4x4_search || bsi.segment_num == BLOCK_8X8) 1296 /* || (sv_segment_rd8x8-bsi.segment_rd) < sv_segment_rd8x8>>5) */ 1297 { 1298 bsi.mvp.as_int = bsi.sv_mvp[0].as_int; 1299 rd_check_segment(cpi, x, &bsi, BLOCK_4X4); 1300 } 1301 1302 /* restore UMV window */ 1303 x->mv_col_min = tmp_col_min; 1304 x->mv_col_max = tmp_col_max; 1305 x->mv_row_min = tmp_row_min; 1306 x->mv_row_max = tmp_row_max; 1307 } 1308 } 1309 1310 /* set it to the best */ 1311 for (i = 0; i < 16; ++i) { 1312 BLOCKD *bd = &x->e_mbd.block[i]; 1313 1314 bd->bmi.mv.as_int = bsi.mvs[i].as_int; 1315 *bd->eob = bsi.eobs[i]; 1316 } 1317 1318 *returntotrate = bsi.r; 1319 *returndistortion = bsi.d; 1320 *returnyrate = bsi.segment_yrate; 1321 1322 /* save partitions */ 1323 x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num; 1324 x->partition_info->count = vp8_mbsplit_count[bsi.segment_num]; 1325 1326 for (i = 0; i < x->partition_info->count; ++i) { 1327 int j; 1328 1329 j = vp8_mbsplit_offset[bsi.segment_num][i]; 1330 1331 x->partition_info->bmi[i].mode = bsi.modes[j]; 1332 x->partition_info->bmi[i].mv.as_mv = bsi.mvs[j].as_mv; 1333 } 1334 /* 1335 * used to set x->e_mbd.mode_info_context->mbmi.mv.as_int 1336 */ 1337 x->partition_info->bmi[15].mv.as_int = bsi.mvs[15].as_int; 1338 1339 return bsi.segment_rd; 1340 } 1341 1342 /* The improved MV prediction */ 1343 void vp8_mv_pred(VP8_COMP *cpi, MACROBLOCKD *xd, const MODE_INFO *here, 1344 int_mv *mvp, int refframe, int *ref_frame_sign_bias, int *sr, 1345 int near_sadidx[]) { 1346 const MODE_INFO *above = here - xd->mode_info_stride; 1347 const MODE_INFO *left = here - 1; 1348 const MODE_INFO *aboveleft = above - 1; 1349 int_mv near_mvs[8]; 1350 int near_ref[8]; 1351 int_mv mv; 1352 int vcnt = 0; 1353 int find = 0; 1354 int mb_offset; 1355 1356 int mvx[8]; 1357 int mvy[8]; 1358 int i; 1359 1360 mv.as_int = 0; 1361 1362 if (here->mbmi.ref_frame != INTRA_FRAME) { 1363 near_mvs[0].as_int = near_mvs[1].as_int = near_mvs[2].as_int = 1364 near_mvs[3].as_int = near_mvs[4].as_int = near_mvs[5].as_int = 1365 near_mvs[6].as_int = near_mvs[7].as_int = 0; 1366 near_ref[0] = near_ref[1] = near_ref[2] = near_ref[3] = near_ref[4] = 1367 near_ref[5] = near_ref[6] = near_ref[7] = 0; 1368 1369 /* read in 3 nearby block's MVs from current frame as prediction 1370 * candidates. 1371 */ 1372 if (above->mbmi.ref_frame != INTRA_FRAME) { 1373 near_mvs[vcnt].as_int = above->mbmi.mv.as_int; 1374 mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, 1375 &near_mvs[vcnt], ref_frame_sign_bias); 1376 near_ref[vcnt] = above->mbmi.ref_frame; 1377 } 1378 vcnt++; 1379 if (left->mbmi.ref_frame != INTRA_FRAME) { 1380 near_mvs[vcnt].as_int = left->mbmi.mv.as_int; 1381 mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, 1382 &near_mvs[vcnt], ref_frame_sign_bias); 1383 near_ref[vcnt] = left->mbmi.ref_frame; 1384 } 1385 vcnt++; 1386 if (aboveleft->mbmi.ref_frame != INTRA_FRAME) { 1387 near_mvs[vcnt].as_int = aboveleft->mbmi.mv.as_int; 1388 mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, 1389 &near_mvs[vcnt], ref_frame_sign_bias); 1390 near_ref[vcnt] = aboveleft->mbmi.ref_frame; 1391 } 1392 vcnt++; 1393 1394 /* read in 5 nearby block's MVs from last frame. */ 1395 if (cpi->common.last_frame_type != KEY_FRAME) { 1396 mb_offset = (-xd->mb_to_top_edge / 128 + 1) * (xd->mode_info_stride + 1) + 1397 (-xd->mb_to_left_edge / 128 + 1); 1398 1399 /* current in last frame */ 1400 if (cpi->lf_ref_frame[mb_offset] != INTRA_FRAME) { 1401 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset].as_int; 1402 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset], refframe, 1403 &near_mvs[vcnt], ref_frame_sign_bias); 1404 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset]; 1405 } 1406 vcnt++; 1407 1408 /* above in last frame */ 1409 if (cpi->lf_ref_frame[mb_offset - xd->mode_info_stride - 1] != 1410 INTRA_FRAME) { 1411 near_mvs[vcnt].as_int = 1412 cpi->lfmv[mb_offset - xd->mode_info_stride - 1].as_int; 1413 mv_bias( 1414 cpi->lf_ref_frame_sign_bias[mb_offset - xd->mode_info_stride - 1], 1415 refframe, &near_mvs[vcnt], ref_frame_sign_bias); 1416 near_ref[vcnt] = 1417 cpi->lf_ref_frame[mb_offset - xd->mode_info_stride - 1]; 1418 } 1419 vcnt++; 1420 1421 /* left in last frame */ 1422 if (cpi->lf_ref_frame[mb_offset - 1] != INTRA_FRAME) { 1423 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset - 1].as_int; 1424 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset - 1], refframe, 1425 &near_mvs[vcnt], ref_frame_sign_bias); 1426 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - 1]; 1427 } 1428 vcnt++; 1429 1430 /* right in last frame */ 1431 if (cpi->lf_ref_frame[mb_offset + 1] != INTRA_FRAME) { 1432 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset + 1].as_int; 1433 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset + 1], refframe, 1434 &near_mvs[vcnt], ref_frame_sign_bias); 1435 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset + 1]; 1436 } 1437 vcnt++; 1438 1439 /* below in last frame */ 1440 if (cpi->lf_ref_frame[mb_offset + xd->mode_info_stride + 1] != 1441 INTRA_FRAME) { 1442 near_mvs[vcnt].as_int = 1443 cpi->lfmv[mb_offset + xd->mode_info_stride + 1].as_int; 1444 mv_bias( 1445 cpi->lf_ref_frame_sign_bias[mb_offset + xd->mode_info_stride + 1], 1446 refframe, &near_mvs[vcnt], ref_frame_sign_bias); 1447 near_ref[vcnt] = 1448 cpi->lf_ref_frame[mb_offset + xd->mode_info_stride + 1]; 1449 } 1450 vcnt++; 1451 } 1452 1453 for (i = 0; i < vcnt; ++i) { 1454 if (near_ref[near_sadidx[i]] != INTRA_FRAME) { 1455 if (here->mbmi.ref_frame == near_ref[near_sadidx[i]]) { 1456 mv.as_int = near_mvs[near_sadidx[i]].as_int; 1457 find = 1; 1458 if (i < 3) { 1459 *sr = 3; 1460 } else { 1461 *sr = 2; 1462 } 1463 break; 1464 } 1465 } 1466 } 1467 1468 if (!find) { 1469 for (i = 0; i < vcnt; ++i) { 1470 mvx[i] = near_mvs[i].as_mv.row; 1471 mvy[i] = near_mvs[i].as_mv.col; 1472 } 1473 1474 insertsortmv(mvx, vcnt); 1475 insertsortmv(mvy, vcnt); 1476 mv.as_mv.row = mvx[vcnt / 2]; 1477 mv.as_mv.col = mvy[vcnt / 2]; 1478 1479 /* sr is set to 0 to allow calling function to decide the search 1480 * range. 1481 */ 1482 *sr = 0; 1483 } 1484 } 1485 1486 /* Set up return values */ 1487 mvp->as_int = mv.as_int; 1488 vp8_clamp_mv2(mvp, xd); 1489 } 1490 1491 void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, 1492 int recon_yoffset, int near_sadidx[]) { 1493 /* near_sad indexes: 1494 * 0-cf above, 1-cf left, 2-cf aboveleft, 1495 * 3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below 1496 */ 1497 int near_sad[8] = { 0 }; 1498 BLOCK *b = &x->block[0]; 1499 unsigned char *src_y_ptr = *(b->base_src); 1500 1501 /* calculate sad for current frame 3 nearby MBs. */ 1502 if (xd->mb_to_top_edge == 0 && xd->mb_to_left_edge == 0) { 1503 near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX; 1504 } else if (xd->mb_to_top_edge == 1505 0) { /* only has left MB for sad calculation. */ 1506 near_sad[0] = near_sad[2] = INT_MAX; 1507 near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf( 1508 src_y_ptr, b->src_stride, xd->dst.y_buffer - 16, xd->dst.y_stride); 1509 } else if (xd->mb_to_left_edge == 1510 0) { /* only has left MB for sad calculation. */ 1511 near_sad[1] = near_sad[2] = INT_MAX; 1512 near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf( 1513 src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride * 16, 1514 xd->dst.y_stride); 1515 } else { 1516 near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf( 1517 src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride * 16, 1518 xd->dst.y_stride); 1519 near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf( 1520 src_y_ptr, b->src_stride, xd->dst.y_buffer - 16, xd->dst.y_stride); 1521 near_sad[2] = cpi->fn_ptr[BLOCK_16X16].sdf( 1522 src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride * 16 - 16, 1523 xd->dst.y_stride); 1524 } 1525 1526 if (cpi->common.last_frame_type != KEY_FRAME) { 1527 /* calculate sad for last frame 5 nearby MBs. */ 1528 unsigned char *pre_y_buffer = 1529 cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset; 1530 int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride; 1531 1532 if (xd->mb_to_top_edge == 0) near_sad[4] = INT_MAX; 1533 if (xd->mb_to_left_edge == 0) near_sad[5] = INT_MAX; 1534 if (xd->mb_to_right_edge == 0) near_sad[6] = INT_MAX; 1535 if (xd->mb_to_bottom_edge == 0) near_sad[7] = INT_MAX; 1536 1537 if (near_sad[4] != INT_MAX) { 1538 near_sad[4] = cpi->fn_ptr[BLOCK_16X16].sdf( 1539 src_y_ptr, b->src_stride, pre_y_buffer - pre_y_stride * 16, 1540 pre_y_stride); 1541 } 1542 if (near_sad[5] != INT_MAX) { 1543 near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf( 1544 src_y_ptr, b->src_stride, pre_y_buffer - 16, pre_y_stride); 1545 } 1546 near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, 1547 pre_y_buffer, pre_y_stride); 1548 if (near_sad[6] != INT_MAX) { 1549 near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf( 1550 src_y_ptr, b->src_stride, pre_y_buffer + 16, pre_y_stride); 1551 } 1552 if (near_sad[7] != INT_MAX) { 1553 near_sad[7] = cpi->fn_ptr[BLOCK_16X16].sdf( 1554 src_y_ptr, b->src_stride, pre_y_buffer + pre_y_stride * 16, 1555 pre_y_stride); 1556 } 1557 } 1558 1559 if (cpi->common.last_frame_type != KEY_FRAME) { 1560 insertsortsad(near_sad, near_sadidx, 8); 1561 } else { 1562 insertsortsad(near_sad, near_sadidx, 3); 1563 } 1564 } 1565 1566 static void rd_update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv) { 1567 if (x->e_mbd.mode_info_context->mbmi.mode == SPLITMV) { 1568 int i; 1569 1570 for (i = 0; i < x->partition_info->count; ++i) { 1571 if (x->partition_info->bmi[i].mode == NEW4X4) { 1572 x->MVcount[0][mv_max + ((x->partition_info->bmi[i].mv.as_mv.row - 1573 best_ref_mv->as_mv.row) >> 1574 1)]++; 1575 x->MVcount[1][mv_max + ((x->partition_info->bmi[i].mv.as_mv.col - 1576 best_ref_mv->as_mv.col) >> 1577 1)]++; 1578 } 1579 } 1580 } else if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV) { 1581 x->MVcount[0][mv_max + ((x->e_mbd.mode_info_context->mbmi.mv.as_mv.row - 1582 best_ref_mv->as_mv.row) >> 1583 1)]++; 1584 x->MVcount[1][mv_max + ((x->e_mbd.mode_info_context->mbmi.mv.as_mv.col - 1585 best_ref_mv->as_mv.col) >> 1586 1)]++; 1587 } 1588 } 1589 1590 static int evaluate_inter_mode_rd(int mdcounts[4], RATE_DISTORTION *rd, 1591 int *disable_skip, VP8_COMP *cpi, 1592 MACROBLOCK *x) { 1593 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode; 1594 BLOCK *b = &x->block[0]; 1595 MACROBLOCKD *xd = &x->e_mbd; 1596 int distortion; 1597 vp8_build_inter16x16_predictors_mby(&x->e_mbd, x->e_mbd.predictor, 16); 1598 1599 if (cpi->active_map_enabled && x->active_ptr[0] == 0) { 1600 x->skip = 1; 1601 } else if (x->encode_breakout) { 1602 unsigned int sse; 1603 unsigned int var; 1604 unsigned int threshold = 1605 (xd->block[0].dequant[1] * xd->block[0].dequant[1] >> 4); 1606 1607 if (threshold < x->encode_breakout) threshold = x->encode_breakout; 1608 1609 var = vpx_variance16x16(*(b->base_src), b->src_stride, x->e_mbd.predictor, 1610 16, &sse); 1611 1612 if (sse < threshold) { 1613 unsigned int q2dc = xd->block[24].dequant[0]; 1614 /* If theres is no codeable 2nd order dc 1615 or a very small uniform pixel change change */ 1616 if ((sse - var<q2dc * q2dc>> 4) || (sse / 2 > var && sse - var < 64)) { 1617 /* Check u and v to make sure skip is ok */ 1618 unsigned int sse2 = VP8_UVSSE(x); 1619 if (sse2 * 2 < threshold) { 1620 x->skip = 1; 1621 rd->distortion2 = sse + sse2; 1622 rd->rate2 = 500; 1623 1624 /* for best_yrd calculation */ 1625 rd->rate_uv = 0; 1626 rd->distortion_uv = sse2; 1627 1628 *disable_skip = 1; 1629 return RDCOST(x->rdmult, x->rddiv, rd->rate2, rd->distortion2); 1630 } 1631 } 1632 } 1633 } 1634 1635 /* Add in the Mv/mode cost */ 1636 rd->rate2 += vp8_cost_mv_ref(this_mode, mdcounts); 1637 1638 /* Y cost and distortion */ 1639 macro_block_yrd(x, &rd->rate_y, &distortion); 1640 rd->rate2 += rd->rate_y; 1641 rd->distortion2 += distortion; 1642 1643 /* UV cost and distortion */ 1644 rd_inter16x16_uv(cpi, x, &rd->rate_uv, &rd->distortion_uv, 1645 cpi->common.full_pixel); 1646 rd->rate2 += rd->rate_uv; 1647 rd->distortion2 += rd->distortion_uv; 1648 return INT_MAX; 1649 } 1650 1651 static int calculate_final_rd_costs(int this_rd, RATE_DISTORTION *rd, 1652 int *other_cost, int disable_skip, 1653 int uv_intra_tteob, int intra_rd_penalty, 1654 VP8_COMP *cpi, MACROBLOCK *x) { 1655 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode; 1656 1657 /* Where skip is allowable add in the default per mb cost for the no 1658 * skip case. where we then decide to skip we have to delete this and 1659 * replace it with the cost of signalling a skip 1660 */ 1661 if (cpi->common.mb_no_coeff_skip) { 1662 *other_cost += vp8_cost_bit(cpi->prob_skip_false, 0); 1663 rd->rate2 += *other_cost; 1664 } 1665 1666 /* Estimate the reference frame signaling cost and add it 1667 * to the rolling cost variable. 1668 */ 1669 rd->rate2 += x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame]; 1670 1671 if (!disable_skip) { 1672 /* Test for the condition where skip block will be activated 1673 * because there are no non zero coefficients and make any 1674 * necessary adjustment for rate 1675 */ 1676 if (cpi->common.mb_no_coeff_skip) { 1677 int i; 1678 int tteob; 1679 int has_y2_block = (this_mode != SPLITMV && this_mode != B_PRED); 1680 1681 tteob = 0; 1682 if (has_y2_block) tteob += x->e_mbd.eobs[24]; 1683 1684 for (i = 0; i < 16; ++i) tteob += (x->e_mbd.eobs[i] > has_y2_block); 1685 1686 if (x->e_mbd.mode_info_context->mbmi.ref_frame) { 1687 for (i = 16; i < 24; ++i) tteob += x->e_mbd.eobs[i]; 1688 } else { 1689 tteob += uv_intra_tteob; 1690 } 1691 1692 if (tteob == 0) { 1693 rd->rate2 -= (rd->rate_y + rd->rate_uv); 1694 /* for best_yrd calculation */ 1695 rd->rate_uv = 0; 1696 1697 /* Back out no skip flag costing and add in skip flag costing */ 1698 if (cpi->prob_skip_false) { 1699 int prob_skip_cost; 1700 1701 prob_skip_cost = vp8_cost_bit(cpi->prob_skip_false, 1); 1702 prob_skip_cost -= (int)vp8_cost_bit(cpi->prob_skip_false, 0); 1703 rd->rate2 += prob_skip_cost; 1704 *other_cost += prob_skip_cost; 1705 } 1706 } 1707 } 1708 /* Calculate the final RD estimate for this mode */ 1709 this_rd = RDCOST(x->rdmult, x->rddiv, rd->rate2, rd->distortion2); 1710 if (this_rd < INT_MAX && 1711 x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) { 1712 this_rd += intra_rd_penalty; 1713 } 1714 } 1715 return this_rd; 1716 } 1717 1718 static void update_best_mode(BEST_MODE *best_mode, int this_rd, 1719 RATE_DISTORTION *rd, int other_cost, 1720 MACROBLOCK *x) { 1721 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode; 1722 1723 other_cost += x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame]; 1724 1725 /* Calculate the final y RD estimate for this mode */ 1726 best_mode->yrd = 1727 RDCOST(x->rdmult, x->rddiv, (rd->rate2 - rd->rate_uv - other_cost), 1728 (rd->distortion2 - rd->distortion_uv)); 1729 1730 best_mode->rd = this_rd; 1731 memcpy(&best_mode->mbmode, &x->e_mbd.mode_info_context->mbmi, 1732 sizeof(MB_MODE_INFO)); 1733 memcpy(&best_mode->partition, x->partition_info, sizeof(PARTITION_INFO)); 1734 1735 if ((this_mode == B_PRED) || (this_mode == SPLITMV)) { 1736 int i; 1737 for (i = 0; i < 16; ++i) { 1738 best_mode->bmodes[i] = x->e_mbd.block[i].bmi; 1739 } 1740 } 1741 } 1742 1743 void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, 1744 int recon_uvoffset, int *returnrate, 1745 int *returndistortion, int *returnintra, int mb_row, 1746 int mb_col) { 1747 BLOCK *b = &x->block[0]; 1748 BLOCKD *d = &x->e_mbd.block[0]; 1749 MACROBLOCKD *xd = &x->e_mbd; 1750 int_mv best_ref_mv_sb[2]; 1751 int_mv mode_mv_sb[2][MB_MODE_COUNT]; 1752 int_mv best_ref_mv; 1753 int_mv *mode_mv; 1754 MB_PREDICTION_MODE this_mode; 1755 int num00; 1756 int best_mode_index = 0; 1757 BEST_MODE best_mode; 1758 1759 int i; 1760 int mode_index; 1761 int mdcounts[4]; 1762 int rate; 1763 RATE_DISTORTION rd; 1764 int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly; 1765 int uv_intra_tteob = 0; 1766 int uv_intra_done = 0; 1767 1768 MB_PREDICTION_MODE uv_intra_mode = 0; 1769 int_mv mvp; 1770 int near_sadidx[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; 1771 int saddone = 0; 1772 /* search range got from mv_pred(). It uses step_param levels. (0-7) */ 1773 int sr = 0; 1774 1775 unsigned char *plane[4][3]; 1776 int ref_frame_map[4]; 1777 int sign_bias = 0; 1778 1779 int intra_rd_penalty = 1780 10 * vp8_dc_quant(cpi->common.base_qindex, cpi->common.y1dc_delta_q); 1781 1782 #if CONFIG_TEMPORAL_DENOISING 1783 unsigned int zero_mv_sse = UINT_MAX, best_sse = UINT_MAX, 1784 best_rd_sse = UINT_MAX; 1785 #endif 1786 1787 mode_mv = mode_mv_sb[sign_bias]; 1788 best_ref_mv.as_int = 0; 1789 best_mode.rd = INT_MAX; 1790 best_mode.yrd = INT_MAX; 1791 best_mode.intra_rd = INT_MAX; 1792 memset(mode_mv_sb, 0, sizeof(mode_mv_sb)); 1793 memset(&best_mode.mbmode, 0, sizeof(best_mode.mbmode)); 1794 memset(&best_mode.bmodes, 0, sizeof(best_mode.bmodes)); 1795 1796 /* Setup search priorities */ 1797 get_reference_search_order(cpi, ref_frame_map); 1798 1799 /* Check to see if there is at least 1 valid reference frame that we need 1800 * to calculate near_mvs. 1801 */ 1802 if (ref_frame_map[1] > 0) { 1803 sign_bias = vp8_find_near_mvs_bias( 1804 &x->e_mbd, x->e_mbd.mode_info_context, mode_mv_sb, best_ref_mv_sb, 1805 mdcounts, ref_frame_map[1], cpi->common.ref_frame_sign_bias); 1806 1807 mode_mv = mode_mv_sb[sign_bias]; 1808 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int; 1809 } 1810 1811 get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset); 1812 1813 *returnintra = INT_MAX; 1814 /* Count of the number of MBs tested so far this frame */ 1815 x->mbs_tested_so_far++; 1816 1817 x->skip = 0; 1818 1819 for (mode_index = 0; mode_index < MAX_MODES; ++mode_index) { 1820 int this_rd = INT_MAX; 1821 int disable_skip = 0; 1822 int other_cost = 0; 1823 int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]]; 1824 1825 /* Test best rd so far against threshold for trying this mode. */ 1826 if (best_mode.rd <= x->rd_threshes[mode_index]) continue; 1827 1828 if (this_ref_frame < 0) continue; 1829 1830 /* These variables hold are rolling total cost and distortion for 1831 * this mode 1832 */ 1833 rd.rate2 = 0; 1834 rd.distortion2 = 0; 1835 1836 this_mode = vp8_mode_order[mode_index]; 1837 1838 x->e_mbd.mode_info_context->mbmi.mode = this_mode; 1839 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame; 1840 1841 /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame, 1842 * unless ARNR filtering is enabled in which case we want 1843 * an unfiltered alternative 1844 */ 1845 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) { 1846 if (this_mode != ZEROMV || 1847 x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME) { 1848 continue; 1849 } 1850 } 1851 1852 /* everything but intra */ 1853 if (x->e_mbd.mode_info_context->mbmi.ref_frame) { 1854 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0]; 1855 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1]; 1856 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2]; 1857 1858 if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame]) { 1859 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame]; 1860 mode_mv = mode_mv_sb[sign_bias]; 1861 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int; 1862 } 1863 } 1864 1865 /* Check to see if the testing frequency for this mode is at its 1866 * max If so then prevent it from being tested and increase the 1867 * threshold for its testing 1868 */ 1869 if (x->mode_test_hit_counts[mode_index] && 1870 (cpi->mode_check_freq[mode_index] > 1)) { 1871 if (x->mbs_tested_so_far <= cpi->mode_check_freq[mode_index] * 1872 x->mode_test_hit_counts[mode_index]) { 1873 /* Increase the threshold for coding this mode to make it 1874 * less likely to be chosen 1875 */ 1876 x->rd_thresh_mult[mode_index] += 4; 1877 1878 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT) { 1879 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT; 1880 } 1881 1882 x->rd_threshes[mode_index] = 1883 (cpi->rd_baseline_thresh[mode_index] >> 7) * 1884 x->rd_thresh_mult[mode_index]; 1885 1886 continue; 1887 } 1888 } 1889 1890 /* We have now reached the point where we are going to test the 1891 * current mode so increment the counter for the number of times 1892 * it has been tested 1893 */ 1894 x->mode_test_hit_counts[mode_index]++; 1895 1896 /* Experimental code. Special case for gf and arf zeromv modes. 1897 * Increase zbin size to supress noise 1898 */ 1899 if (x->zbin_mode_boost_enabled) { 1900 if (this_ref_frame == INTRA_FRAME) { 1901 x->zbin_mode_boost = 0; 1902 } else { 1903 if (vp8_mode_order[mode_index] == ZEROMV) { 1904 if (this_ref_frame != LAST_FRAME) { 1905 x->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST; 1906 } else { 1907 x->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST; 1908 } 1909 } else if (vp8_mode_order[mode_index] == SPLITMV) { 1910 x->zbin_mode_boost = 0; 1911 } else { 1912 x->zbin_mode_boost = MV_ZBIN_BOOST; 1913 } 1914 } 1915 1916 vp8_update_zbin_extra(cpi, x); 1917 } 1918 1919 if (!uv_intra_done && this_ref_frame == INTRA_FRAME) { 1920 rd_pick_intra_mbuv_mode(x, &uv_intra_rate, &uv_intra_rate_tokenonly, 1921 &uv_intra_distortion); 1922 uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode; 1923 1924 /* 1925 * Total of the eobs is used later to further adjust rate2. Since uv 1926 * block's intra eobs will be overwritten when we check inter modes, 1927 * we need to save uv_intra_tteob here. 1928 */ 1929 for (i = 16; i < 24; ++i) uv_intra_tteob += x->e_mbd.eobs[i]; 1930 1931 uv_intra_done = 1; 1932 } 1933 1934 switch (this_mode) { 1935 case B_PRED: { 1936 int tmp_rd; 1937 1938 /* Note the rate value returned here includes the cost of 1939 * coding the BPRED mode: x->mbmode_cost[x->e_mbd.frame_type][BPRED] 1940 */ 1941 int distortion; 1942 tmp_rd = rd_pick_intra4x4mby_modes(x, &rate, &rd.rate_y, &distortion, 1943 best_mode.yrd); 1944 rd.rate2 += rate; 1945 rd.distortion2 += distortion; 1946 1947 if (tmp_rd < best_mode.yrd) { 1948 rd.rate2 += uv_intra_rate; 1949 rd.rate_uv = uv_intra_rate_tokenonly; 1950 rd.distortion2 += uv_intra_distortion; 1951 rd.distortion_uv = uv_intra_distortion; 1952 } else { 1953 this_rd = INT_MAX; 1954 disable_skip = 1; 1955 } 1956 break; 1957 } 1958 1959 case SPLITMV: { 1960 int tmp_rd; 1961 int this_rd_thresh; 1962 int distortion; 1963 1964 this_rd_thresh = (vp8_ref_frame_order[mode_index] == 1) 1965 ? x->rd_threshes[THR_NEW1] 1966 : x->rd_threshes[THR_NEW3]; 1967 this_rd_thresh = (vp8_ref_frame_order[mode_index] == 2) 1968 ? x->rd_threshes[THR_NEW2] 1969 : this_rd_thresh; 1970 1971 tmp_rd = vp8_rd_pick_best_mbsegmentation( 1972 cpi, x, &best_ref_mv, best_mode.yrd, mdcounts, &rate, &rd.rate_y, 1973 &distortion, this_rd_thresh); 1974 1975 rd.rate2 += rate; 1976 rd.distortion2 += distortion; 1977 1978 /* If even the 'Y' rd value of split is higher than best so far 1979 * then dont bother looking at UV 1980 */ 1981 if (tmp_rd < best_mode.yrd) { 1982 /* Now work out UV cost and add it in */ 1983 rd_inter4x4_uv(cpi, x, &rd.rate_uv, &rd.distortion_uv, 1984 cpi->common.full_pixel); 1985 rd.rate2 += rd.rate_uv; 1986 rd.distortion2 += rd.distortion_uv; 1987 } else { 1988 this_rd = INT_MAX; 1989 disable_skip = 1; 1990 } 1991 break; 1992 } 1993 case DC_PRED: 1994 case V_PRED: 1995 case H_PRED: 1996 case TM_PRED: { 1997 int distortion; 1998 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME; 1999 2000 vp8_build_intra_predictors_mby_s( 2001 xd, xd->dst.y_buffer - xd->dst.y_stride, xd->dst.y_buffer - 1, 2002 xd->dst.y_stride, xd->predictor, 16); 2003 macro_block_yrd(x, &rd.rate_y, &distortion); 2004 rd.rate2 += rd.rate_y; 2005 rd.distortion2 += distortion; 2006 rd.rate2 += x->mbmode_cost[x->e_mbd.frame_type] 2007 [x->e_mbd.mode_info_context->mbmi.mode]; 2008 rd.rate2 += uv_intra_rate; 2009 rd.rate_uv = uv_intra_rate_tokenonly; 2010 rd.distortion2 += uv_intra_distortion; 2011 rd.distortion_uv = uv_intra_distortion; 2012 break; 2013 } 2014 2015 case NEWMV: { 2016 int thissme; 2017 int bestsme = INT_MAX; 2018 int step_param = cpi->sf.first_step; 2019 int further_steps; 2020 int n; 2021 /* If last step (1-away) of n-step search doesn't pick the center point 2022 as the best match, we will do a final 1-away diamond refining search 2023 */ 2024 int do_refine = 1; 2025 2026 int sadpb = x->sadperbit16; 2027 int_mv mvp_full; 2028 2029 int col_min = ((best_ref_mv.as_mv.col + 7) >> 3) - MAX_FULL_PEL_VAL; 2030 int row_min = ((best_ref_mv.as_mv.row + 7) >> 3) - MAX_FULL_PEL_VAL; 2031 int col_max = (best_ref_mv.as_mv.col >> 3) + MAX_FULL_PEL_VAL; 2032 int row_max = (best_ref_mv.as_mv.row >> 3) + MAX_FULL_PEL_VAL; 2033 2034 int tmp_col_min = x->mv_col_min; 2035 int tmp_col_max = x->mv_col_max; 2036 int tmp_row_min = x->mv_row_min; 2037 int tmp_row_max = x->mv_row_max; 2038 2039 if (!saddone) { 2040 vp8_cal_sad(cpi, xd, x, recon_yoffset, &near_sadidx[0]); 2041 saddone = 1; 2042 } 2043 2044 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp, 2045 x->e_mbd.mode_info_context->mbmi.ref_frame, 2046 cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]); 2047 2048 mvp_full.as_mv.col = mvp.as_mv.col >> 3; 2049 mvp_full.as_mv.row = mvp.as_mv.row >> 3; 2050 2051 /* Get intersection of UMV window and valid MV window to 2052 * reduce # of checks in diamond search. 2053 */ 2054 if (x->mv_col_min < col_min) x->mv_col_min = col_min; 2055 if (x->mv_col_max > col_max) x->mv_col_max = col_max; 2056 if (x->mv_row_min < row_min) x->mv_row_min = row_min; 2057 if (x->mv_row_max > row_max) x->mv_row_max = row_max; 2058 2059 /* adjust search range according to sr from mv prediction */ 2060 if (sr > step_param) step_param = sr; 2061 2062 /* Initial step/diamond search */ 2063 { 2064 bestsme = cpi->diamond_search_sad( 2065 x, b, d, &mvp_full, &d->bmi.mv, step_param, sadpb, &num00, 2066 &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv); 2067 mode_mv[NEWMV].as_int = d->bmi.mv.as_int; 2068 2069 /* Further step/diamond searches as necessary */ 2070 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param; 2071 2072 n = num00; 2073 num00 = 0; 2074 2075 /* If there won't be more n-step search, check to see if refining 2076 * search is needed. */ 2077 if (n > further_steps) do_refine = 0; 2078 2079 while (n < further_steps) { 2080 n++; 2081 2082 if (num00) { 2083 num00--; 2084 } else { 2085 thissme = cpi->diamond_search_sad( 2086 x, b, d, &mvp_full, &d->bmi.mv, step_param + n, sadpb, &num00, 2087 &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv); 2088 2089 /* check to see if refining search is needed. */ 2090 if (num00 > (further_steps - n)) do_refine = 0; 2091 2092 if (thissme < bestsme) { 2093 bestsme = thissme; 2094 mode_mv[NEWMV].as_int = d->bmi.mv.as_int; 2095 } else { 2096 d->bmi.mv.as_int = mode_mv[NEWMV].as_int; 2097 } 2098 } 2099 } 2100 } 2101 2102 /* final 1-away diamond refining search */ 2103 if (do_refine == 1) { 2104 int search_range; 2105 2106 search_range = 8; 2107 2108 thissme = cpi->refining_search_sad( 2109 x, b, d, &d->bmi.mv, sadpb, search_range, 2110 &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv); 2111 2112 if (thissme < bestsme) { 2113 bestsme = thissme; 2114 mode_mv[NEWMV].as_int = d->bmi.mv.as_int; 2115 } else { 2116 d->bmi.mv.as_int = mode_mv[NEWMV].as_int; 2117 } 2118 } 2119 2120 x->mv_col_min = tmp_col_min; 2121 x->mv_col_max = tmp_col_max; 2122 x->mv_row_min = tmp_row_min; 2123 x->mv_row_max = tmp_row_max; 2124 2125 if (bestsme < INT_MAX) { 2126 int dis; /* TODO: use dis in distortion calculation later. */ 2127 unsigned int sse; 2128 cpi->find_fractional_mv_step( 2129 x, b, d, &d->bmi.mv, &best_ref_mv, x->errorperbit, 2130 &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &dis, &sse); 2131 } 2132 2133 mode_mv[NEWMV].as_int = d->bmi.mv.as_int; 2134 2135 /* Add the new motion vector cost to our rolling cost variable */ 2136 rd.rate2 += 2137 vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96); 2138 } 2139 2140 case NEARESTMV: 2141 case NEARMV: 2142 /* Clip "next_nearest" so that it does not extend to far out 2143 * of image 2144 */ 2145 vp8_clamp_mv2(&mode_mv[this_mode], xd); 2146 2147 /* Do not bother proceeding if the vector (from newmv, nearest 2148 * or near) is 0,0 as this should then be coded using the zeromv 2149 * mode. 2150 */ 2151 if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) && 2152 (mode_mv[this_mode].as_int == 0)) { 2153 continue; 2154 } 2155 2156 case ZEROMV: 2157 2158 /* Trap vectors that reach beyond the UMV borders 2159 * Note that ALL New MV, Nearest MV Near MV and Zero MV code 2160 * drops through to this point because of the lack of break 2161 * statements in the previous two cases. 2162 */ 2163 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || 2164 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) || 2165 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || 2166 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max)) { 2167 continue; 2168 } 2169 2170 vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]); 2171 this_rd = evaluate_inter_mode_rd(mdcounts, &rd, &disable_skip, cpi, x); 2172 break; 2173 2174 default: break; 2175 } 2176 2177 this_rd = 2178 calculate_final_rd_costs(this_rd, &rd, &other_cost, disable_skip, 2179 uv_intra_tteob, intra_rd_penalty, cpi, x); 2180 2181 /* Keep record of best intra distortion */ 2182 if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) && 2183 (this_rd < best_mode.intra_rd)) { 2184 best_mode.intra_rd = this_rd; 2185 *returnintra = rd.distortion2; 2186 } 2187 #if CONFIG_TEMPORAL_DENOISING 2188 if (cpi->oxcf.noise_sensitivity) { 2189 unsigned int sse; 2190 vp8_get_inter_mbpred_error(x, &cpi->fn_ptr[BLOCK_16X16], &sse, 2191 mode_mv[this_mode]); 2192 2193 if (sse < best_rd_sse) best_rd_sse = sse; 2194 2195 /* Store for later use by denoiser. */ 2196 if (this_mode == ZEROMV && sse < zero_mv_sse) { 2197 zero_mv_sse = sse; 2198 x->best_zeromv_reference_frame = 2199 x->e_mbd.mode_info_context->mbmi.ref_frame; 2200 } 2201 2202 /* Store the best NEWMV in x for later use in the denoiser. */ 2203 if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV && sse < best_sse) { 2204 best_sse = sse; 2205 vp8_get_inter_mbpred_error(x, &cpi->fn_ptr[BLOCK_16X16], &best_sse, 2206 mode_mv[this_mode]); 2207 x->best_sse_inter_mode = NEWMV; 2208 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv; 2209 x->need_to_clamp_best_mvs = 2210 x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs; 2211 x->best_reference_frame = x->e_mbd.mode_info_context->mbmi.ref_frame; 2212 } 2213 } 2214 #endif 2215 2216 /* Did this mode help.. i.i is it the new best mode */ 2217 if (this_rd < best_mode.rd || x->skip) { 2218 /* Note index of best mode so far */ 2219 best_mode_index = mode_index; 2220 *returnrate = rd.rate2; 2221 *returndistortion = rd.distortion2; 2222 if (this_mode <= B_PRED) { 2223 x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode; 2224 /* required for left and above block mv */ 2225 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0; 2226 } 2227 update_best_mode(&best_mode, this_rd, &rd, other_cost, x); 2228 2229 /* Testing this mode gave rise to an improvement in best error 2230 * score. Lower threshold a bit for next time 2231 */ 2232 x->rd_thresh_mult[mode_index] = 2233 (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) 2234 ? x->rd_thresh_mult[mode_index] - 2 2235 : MIN_THRESHMULT; 2236 } 2237 2238 /* If the mode did not help improve the best error case then raise 2239 * the threshold for testing that mode next time around. 2240 */ 2241 else { 2242 x->rd_thresh_mult[mode_index] += 4; 2243 2244 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT) { 2245 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT; 2246 } 2247 } 2248 x->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * 2249 x->rd_thresh_mult[mode_index]; 2250 2251 if (x->skip) break; 2252 } 2253 2254 /* Reduce the activation RD thresholds for the best choice mode */ 2255 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && 2256 (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2))) { 2257 int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 2); 2258 2259 x->rd_thresh_mult[best_mode_index] = 2260 (x->rd_thresh_mult[best_mode_index] >= 2261 (MIN_THRESHMULT + best_adjustment)) 2262 ? x->rd_thresh_mult[best_mode_index] - best_adjustment 2263 : MIN_THRESHMULT; 2264 x->rd_threshes[best_mode_index] = 2265 (cpi->rd_baseline_thresh[best_mode_index] >> 7) * 2266 x->rd_thresh_mult[best_mode_index]; 2267 } 2268 2269 #if CONFIG_TEMPORAL_DENOISING 2270 if (cpi->oxcf.noise_sensitivity) { 2271 int block_index = mb_row * cpi->common.mb_cols + mb_col; 2272 if (x->best_sse_inter_mode == DC_PRED) { 2273 /* No best MV found. */ 2274 x->best_sse_inter_mode = best_mode.mbmode.mode; 2275 x->best_sse_mv = best_mode.mbmode.mv; 2276 x->need_to_clamp_best_mvs = best_mode.mbmode.need_to_clamp_mvs; 2277 x->best_reference_frame = best_mode.mbmode.ref_frame; 2278 best_sse = best_rd_sse; 2279 } 2280 vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse, 2281 recon_yoffset, recon_uvoffset, &cpi->common.lf_info, 2282 mb_row, mb_col, block_index, 0); 2283 2284 /* Reevaluate ZEROMV after denoising. */ 2285 if (best_mode.mbmode.ref_frame == INTRA_FRAME && 2286 x->best_zeromv_reference_frame != INTRA_FRAME) { 2287 int this_rd = INT_MAX; 2288 int disable_skip = 0; 2289 int other_cost = 0; 2290 int this_ref_frame = x->best_zeromv_reference_frame; 2291 rd.rate2 = 2292 x->ref_frame_cost[this_ref_frame] + vp8_cost_mv_ref(ZEROMV, mdcounts); 2293 rd.distortion2 = 0; 2294 2295 /* set up the proper prediction buffers for the frame */ 2296 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame; 2297 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0]; 2298 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1]; 2299 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2]; 2300 2301 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV; 2302 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED; 2303 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0; 2304 2305 this_rd = evaluate_inter_mode_rd(mdcounts, &rd, &disable_skip, cpi, x); 2306 this_rd = 2307 calculate_final_rd_costs(this_rd, &rd, &other_cost, disable_skip, 2308 uv_intra_tteob, intra_rd_penalty, cpi, x); 2309 if (this_rd < best_mode.rd || x->skip) { 2310 *returnrate = rd.rate2; 2311 *returndistortion = rd.distortion2; 2312 update_best_mode(&best_mode, this_rd, &rd, other_cost, x); 2313 } 2314 } 2315 } 2316 #endif 2317 2318 if (cpi->is_src_frame_alt_ref && 2319 (best_mode.mbmode.mode != ZEROMV || 2320 best_mode.mbmode.ref_frame != ALTREF_FRAME)) { 2321 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV; 2322 x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME; 2323 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0; 2324 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED; 2325 x->e_mbd.mode_info_context->mbmi.mb_skip_coeff = 2326 (cpi->common.mb_no_coeff_skip); 2327 x->e_mbd.mode_info_context->mbmi.partitioning = 0; 2328 return; 2329 } 2330 2331 /* macroblock modes */ 2332 memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mode.mbmode, 2333 sizeof(MB_MODE_INFO)); 2334 2335 if (best_mode.mbmode.mode == B_PRED) { 2336 for (i = 0; i < 16; ++i) { 2337 xd->mode_info_context->bmi[i].as_mode = best_mode.bmodes[i].as_mode; 2338 } 2339 } 2340 2341 if (best_mode.mbmode.mode == SPLITMV) { 2342 for (i = 0; i < 16; ++i) { 2343 xd->mode_info_context->bmi[i].mv.as_int = best_mode.bmodes[i].mv.as_int; 2344 } 2345 2346 memcpy(x->partition_info, &best_mode.partition, sizeof(PARTITION_INFO)); 2347 2348 x->e_mbd.mode_info_context->mbmi.mv.as_int = 2349 x->partition_info->bmi[15].mv.as_int; 2350 } 2351 2352 if (sign_bias != 2353 cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame]) { 2354 best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int; 2355 } 2356 2357 rd_update_mvcount(x, &best_ref_mv); 2358 } 2359 2360 void vp8_rd_pick_intra_mode(MACROBLOCK *x, int *rate_) { 2361 int error4x4, error16x16; 2362 int rate4x4, rate16x16 = 0, rateuv; 2363 int dist4x4, dist16x16, distuv; 2364 int rate; 2365 int rate4x4_tokenonly = 0; 2366 int rate16x16_tokenonly = 0; 2367 int rateuv_tokenonly = 0; 2368 2369 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME; 2370 2371 rd_pick_intra_mbuv_mode(x, &rateuv, &rateuv_tokenonly, &distuv); 2372 rate = rateuv; 2373 2374 error16x16 = rd_pick_intra16x16mby_mode(x, &rate16x16, &rate16x16_tokenonly, 2375 &dist16x16); 2376 2377 error4x4 = rd_pick_intra4x4mby_modes(x, &rate4x4, &rate4x4_tokenonly, 2378 &dist4x4, error16x16); 2379 2380 if (error4x4 < error16x16) { 2381 x->e_mbd.mode_info_context->mbmi.mode = B_PRED; 2382 rate += rate4x4; 2383 } else { 2384 rate += rate16x16; 2385 } 2386 2387 *rate_ = rate; 2388 } 2389