media/libvpx/vp8/encoder/pickinter.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10
michael@0 11
michael@0 12 #include <limits.h>
michael@0 13 #include "vpx_config.h"
michael@0 14 #include "onyx_int.h"
michael@0 15 #include "modecosts.h"
michael@0 16 #include "encodeintra.h"
michael@0 17 #include "vp8/common/entropymode.h"
michael@0 18 #include "pickinter.h"
michael@0 19 #include "vp8/common/findnearmv.h"
michael@0 20 #include "encodemb.h"
michael@0 21 #include "vp8/common/reconinter.h"
michael@0 22 #include "vp8/common/reconintra4x4.h"
michael@0 23 #include "vp8/common/variance.h"
michael@0 24 #include "mcomp.h"
michael@0 25 #include "rdopt.h"
michael@0 26 #include "vpx_mem/vpx_mem.h"
michael@0 27 #if CONFIG_TEMPORAL_DENOISING
michael@0 28 #include "denoising.h"
michael@0 29 #endif
michael@0 30
michael@0 31 extern int VP8_UVSSE(MACROBLOCK *x);
michael@0 32
michael@0 33 #ifdef SPEEDSTATS
michael@0 34 extern unsigned int cnt_pm;
michael@0 35 #endif
michael@0 36
michael@0 37 extern const int vp8_ref_frame_order[MAX_MODES];
michael@0 38 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
michael@0 39
michael@0 40 extern int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]);
michael@0 41
michael@0 42
michael@0 43 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
michael@0 44 int_mv *bestmv, int_mv *ref_mv,
michael@0 45 int error_per_bit,
michael@0 46 const vp8_variance_fn_ptr_t *vfp,
michael@0 47 int *mvcost[2], int *distortion,
michael@0 48 unsigned int *sse)
michael@0 49 {
michael@0 50 (void) b;
michael@0 51 (void) d;
michael@0 52 (void) ref_mv;
michael@0 53 (void) error_per_bit;
michael@0 54 (void) vfp;
michael@0 55 (void) mvcost;
michael@0 56 (void) distortion;
michael@0 57 (void) sse;
michael@0 58 bestmv->as_mv.row <<= 3;
michael@0 59 bestmv->as_mv.col <<= 3;
michael@0 60 return 0;
michael@0 61 }
michael@0 62
michael@0 63
michael@0 64 int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
michael@0 65 const vp8_variance_fn_ptr_t *vfp,
michael@0 66 unsigned int *sse,
michael@0 67 int_mv this_mv)
michael@0 68 {
michael@0 69
michael@0 70 BLOCK *b = &mb->block[0];
michael@0 71 BLOCKD *d = &mb->e_mbd.block[0];
michael@0 72 unsigned char *what = (*(b->base_src) + b->src);
michael@0 73 int what_stride = b->src_stride;
michael@0 74 int pre_stride = mb->e_mbd.pre.y_stride;
michael@0 75 unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset ;
michael@0 76 int in_what_stride = pre_stride;
michael@0 77 int xoffset = this_mv.as_mv.col & 7;
michael@0 78 int yoffset = this_mv.as_mv.row & 7;
michael@0 79
michael@0 80 in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
michael@0 81
michael@0 82 if (xoffset | yoffset)
michael@0 83 {
michael@0 84 return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
michael@0 85 }
michael@0 86 else
michael@0 87 {
michael@0 88 return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
michael@0 89 }
michael@0 90
michael@0 91 }
michael@0 92
michael@0 93
michael@0 94 unsigned int vp8_get4x4sse_cs_c
michael@0 95 (
michael@0 96 const unsigned char *src_ptr,
michael@0 97 int source_stride,
michael@0 98 const unsigned char *ref_ptr,
michael@0 99 int recon_stride
michael@0 100 )
michael@0 101 {
michael@0 102 int distortion = 0;
michael@0 103 int r, c;
michael@0 104
michael@0 105 for (r = 0; r < 4; r++)
michael@0 106 {
michael@0 107 for (c = 0; c < 4; c++)
michael@0 108 {
michael@0 109 int diff = src_ptr[c] - ref_ptr[c];
michael@0 110 distortion += diff * diff;
michael@0 111 }
michael@0 112
michael@0 113 src_ptr += source_stride;
michael@0 114 ref_ptr += recon_stride;
michael@0 115 }
michael@0 116
michael@0 117 return distortion;
michael@0 118 }
michael@0 119
michael@0 120 static int get_prediction_error(BLOCK *be, BLOCKD *b)
michael@0 121 {
michael@0 122 unsigned char *sptr;
michael@0 123 unsigned char *dptr;
michael@0 124 sptr = (*(be->base_src) + be->src);
michael@0 125 dptr = b->predictor;
michael@0 126
michael@0 127 return vp8_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
michael@0 128
michael@0 129 }
michael@0 130
michael@0 131 static int pick_intra4x4block(
michael@0 132 MACROBLOCK *x,
michael@0 133 int ib,
michael@0 134 B_PREDICTION_MODE *best_mode,
michael@0 135 const int *mode_costs,
michael@0 136
michael@0 137 int *bestrate,
michael@0 138 int *bestdistortion)
michael@0 139 {
michael@0 140
michael@0 141 BLOCKD *b = &x->e_mbd.block[ib];
michael@0 142 BLOCK *be = &x->block[ib];
michael@0 143 int dst_stride = x->e_mbd.dst.y_stride;
michael@0 144 unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
michael@0 145 B_PREDICTION_MODE mode;
michael@0 146 int best_rd = INT_MAX;
michael@0 147 int rate;
michael@0 148 int distortion;
michael@0 149
michael@0 150 unsigned char *Above = dst - dst_stride;
michael@0 151 unsigned char *yleft = dst - 1;
michael@0 152 unsigned char top_left = Above[-1];
michael@0 153
michael@0 154 for (mode = B_DC_PRED; mode <= B_HE_PRED; mode++)
michael@0 155 {
michael@0 156 int this_rd;
michael@0 157
michael@0 158 rate = mode_costs[mode];
michael@0 159
michael@0 160 vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
michael@0 161 b->predictor, 16, top_left);
michael@0 162 distortion = get_prediction_error(be, b);
michael@0 163 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
michael@0 164
michael@0 165 if (this_rd < best_rd)
michael@0 166 {
michael@0 167 *bestrate = rate;
michael@0 168 *bestdistortion = distortion;
michael@0 169 best_rd = this_rd;
michael@0 170 *best_mode = mode;
michael@0 171 }
michael@0 172 }
michael@0 173
michael@0 174 b->bmi.as_mode = *best_mode;
michael@0 175 vp8_encode_intra4x4block(x, ib);
michael@0 176 return best_rd;
michael@0 177 }
michael@0 178
michael@0 179
michael@0 180 static int pick_intra4x4mby_modes
michael@0 181 (
michael@0 182 MACROBLOCK *mb,
michael@0 183 int *Rate,
michael@0 184 int *best_dist
michael@0 185 )
michael@0 186 {
michael@0 187 MACROBLOCKD *const xd = &mb->e_mbd;
michael@0 188 int i;
michael@0 189 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
michael@0 190 int error;
michael@0 191 int distortion = 0;
michael@0 192 const int *bmode_costs;
michael@0 193
michael@0 194 intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
michael@0 195
michael@0 196 bmode_costs = mb->inter_bmode_costs;
michael@0 197
michael@0 198 for (i = 0; i < 16; i++)
michael@0 199 {
michael@0 200 MODE_INFO *const mic = xd->mode_info_context;
michael@0 201 const int mis = xd->mode_info_stride;
michael@0 202
michael@0 203 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
michael@0 204 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
michael@0 205
michael@0 206 if (mb->e_mbd.frame_type == KEY_FRAME)
michael@0 207 {
michael@0 208 const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
michael@0 209 const B_PREDICTION_MODE L = left_block_mode(mic, i);
michael@0 210
michael@0 211 bmode_costs = mb->bmode_costs[A][L];
michael@0 212 }
michael@0 213
michael@0 214
michael@0 215 pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
michael@0 216
michael@0 217 cost += r;
michael@0 218 distortion += d;
michael@0 219 mic->bmi[i].as_mode = best_mode;
michael@0 220
michael@0 221 /* Break out case where we have already exceeded best so far value
michael@0 222 * that was passed in
michael@0 223 */
michael@0 224 if (distortion > *best_dist)
michael@0 225 break;
michael@0 226 }
michael@0 227
michael@0 228 *Rate = cost;
michael@0 229
michael@0 230 if (i == 16)
michael@0 231 {
michael@0 232 *best_dist = distortion;
michael@0 233 error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
michael@0 234 }
michael@0 235 else
michael@0 236 {
michael@0 237 *best_dist = INT_MAX;
michael@0 238 error = INT_MAX;
michael@0 239 }
michael@0 240
michael@0 241 return error;
michael@0 242 }
michael@0 243
michael@0 244 static void pick_intra_mbuv_mode(MACROBLOCK *mb)
michael@0 245 {
michael@0 246
michael@0 247 MACROBLOCKD *x = &mb->e_mbd;
michael@0 248 unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
michael@0 249 unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
michael@0 250 unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
michael@0 251 unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
michael@0 252 int uvsrc_stride = mb->block[16].src_stride;
michael@0 253 unsigned char uleft_col[8];
michael@0 254 unsigned char vleft_col[8];
michael@0 255 unsigned char utop_left = uabove_row[-1];
michael@0 256 unsigned char vtop_left = vabove_row[-1];
michael@0 257 int i, j;
michael@0 258 int expected_udc;
michael@0 259 int expected_vdc;
michael@0 260 int shift;
michael@0 261 int Uaverage = 0;
michael@0 262 int Vaverage = 0;
michael@0 263 int diff;
michael@0 264 int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
michael@0 265 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
michael@0 266
michael@0 267
michael@0 268 for (i = 0; i < 8; i++)
michael@0 269 {
michael@0 270 uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
michael@0 271 vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
michael@0 272 }
michael@0 273
michael@0 274 if (!x->up_available && !x->left_available)
michael@0 275 {
michael@0 276 expected_udc = 128;
michael@0 277 expected_vdc = 128;
michael@0 278 }
michael@0 279 else
michael@0 280 {
michael@0 281 shift = 2;
michael@0 282
michael@0 283 if (x->up_available)
michael@0 284 {
michael@0 285
michael@0 286 for (i = 0; i < 8; i++)
michael@0 287 {
michael@0 288 Uaverage += uabove_row[i];
michael@0 289 Vaverage += vabove_row[i];
michael@0 290 }
michael@0 291
michael@0 292 shift ++;
michael@0 293
michael@0 294 }
michael@0 295
michael@0 296 if (x->left_available)
michael@0 297 {
michael@0 298 for (i = 0; i < 8; i++)
michael@0 299 {
michael@0 300 Uaverage += uleft_col[i];
michael@0 301 Vaverage += vleft_col[i];
michael@0 302 }
michael@0 303
michael@0 304 shift ++;
michael@0 305
michael@0 306 }
michael@0 307
michael@0 308 expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
michael@0 309 expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
michael@0 310 }
michael@0 311
michael@0 312
michael@0 313 for (i = 0; i < 8; i++)
michael@0 314 {
michael@0 315 for (j = 0; j < 8; j++)
michael@0 316 {
michael@0 317
michael@0 318 int predu = uleft_col[i] + uabove_row[j] - utop_left;
michael@0 319 int predv = vleft_col[i] + vabove_row[j] - vtop_left;
michael@0 320 int u_p, v_p;
michael@0 321
michael@0 322 u_p = usrc_ptr[j];
michael@0 323 v_p = vsrc_ptr[j];
michael@0 324
michael@0 325 if (predu < 0)
michael@0 326 predu = 0;
michael@0 327
michael@0 328 if (predu > 255)
michael@0 329 predu = 255;
michael@0 330
michael@0 331 if (predv < 0)
michael@0 332 predv = 0;
michael@0 333
michael@0 334 if (predv > 255)
michael@0 335 predv = 255;
michael@0 336
michael@0 337
michael@0 338 diff = u_p - expected_udc;
michael@0 339 pred_error[DC_PRED] += diff * diff;
michael@0 340 diff = v_p - expected_vdc;
michael@0 341 pred_error[DC_PRED] += diff * diff;
michael@0 342
michael@0 343
michael@0 344 diff = u_p - uabove_row[j];
michael@0 345 pred_error[V_PRED] += diff * diff;
michael@0 346 diff = v_p - vabove_row[j];
michael@0 347 pred_error[V_PRED] += diff * diff;
michael@0 348
michael@0 349
michael@0 350 diff = u_p - uleft_col[i];
michael@0 351 pred_error[H_PRED] += diff * diff;
michael@0 352 diff = v_p - vleft_col[i];
michael@0 353 pred_error[H_PRED] += diff * diff;
michael@0 354
michael@0 355
michael@0 356 diff = u_p - predu;
michael@0 357 pred_error[TM_PRED] += diff * diff;
michael@0 358 diff = v_p - predv;
michael@0 359 pred_error[TM_PRED] += diff * diff;
michael@0 360
michael@0 361
michael@0 362 }
michael@0 363
michael@0 364 usrc_ptr += uvsrc_stride;
michael@0 365 vsrc_ptr += uvsrc_stride;
michael@0 366
michael@0 367 if (i == 3)
michael@0 368 {
michael@0 369 usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
michael@0 370 vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
michael@0 371 }
michael@0 372
michael@0 373
michael@0 374
michael@0 375 }
michael@0 376
michael@0 377
michael@0 378 for (i = DC_PRED; i <= TM_PRED; i++)
michael@0 379 {
michael@0 380 if (best_error > pred_error[i])
michael@0 381 {
michael@0 382 best_error = pred_error[i];
michael@0 383 best_mode = (MB_PREDICTION_MODE)i;
michael@0 384 }
michael@0 385 }
michael@0 386
michael@0 387
michael@0 388 mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
michael@0 389
michael@0 390 }
michael@0 391
michael@0 392 static void update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
michael@0 393 {
michael@0 394 MACROBLOCKD *xd = &x->e_mbd;
michael@0 395 /* Split MV modes currently not supported when RD is nopt enabled,
michael@0 396 * therefore, only need to modify MVcount in NEWMV mode. */
michael@0 397 if (xd->mode_info_context->mbmi.mode == NEWMV)
michael@0 398 {
michael@0 399 x->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
michael@0 400 best_ref_mv->as_mv.row) >> 1)]++;
michael@0 401 x->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
michael@0 402 best_ref_mv->as_mv.col) >> 1)]++;
michael@0 403 }
michael@0 404 }
michael@0 405
michael@0 406
michael@0 407 #if CONFIG_MULTI_RES_ENCODING
michael@0 408 static
michael@0 409 void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd, int *dissim,
michael@0 410 int *parent_ref_frame,
michael@0 411 MB_PREDICTION_MODE *parent_mode,
michael@0 412 int_mv *parent_ref_mv, int mb_row, int mb_col)
michael@0 413 {
michael@0 414 LOWER_RES_MB_INFO* store_mode_info
michael@0 415 = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
michael@0 416 unsigned int parent_mb_index;
michael@0 417
michael@0 418 /* Consider different down_sampling_factor. */
michael@0 419 {
michael@0 420 /* TODO: Removed the loop that supports special down_sampling_factor
michael@0 421 * such as 2, 4, 8. Will revisit it if needed.
michael@0 422 * Should also try using a look-up table to see if it helps
michael@0 423 * performance. */
michael@0 424 int parent_mb_row, parent_mb_col;
michael@0 425
michael@0 426 parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
michael@0 427 /cpi->oxcf.mr_down_sampling_factor.num;
michael@0 428 parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
michael@0 429 /cpi->oxcf.mr_down_sampling_factor.num;
michael@0 430 parent_mb_index = parent_mb_row*cpi->mr_low_res_mb_cols + parent_mb_col;
michael@0 431 }
michael@0 432
michael@0 433 /* Read lower-resolution mode & motion result from memory.*/
michael@0 434 *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
michael@0 435 *parent_mode = store_mode_info[parent_mb_index].mode;
michael@0 436 *dissim = store_mode_info[parent_mb_index].dissim;
michael@0 437
michael@0 438 /* For highest-resolution encoder, adjust dissim value. Lower its quality
michael@0 439 * for good performance. */
michael@0 440 if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
michael@0 441 *dissim>>=1;
michael@0 442
michael@0 443 if(*parent_ref_frame != INTRA_FRAME)
michael@0 444 {
michael@0 445 /* Consider different down_sampling_factor.
michael@0 446 * The result can be rounded to be more precise, but it takes more time.
michael@0 447 */
michael@0 448 (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row
michael@0 449 *cpi->oxcf.mr_down_sampling_factor.num
michael@0 450 /cpi->oxcf.mr_down_sampling_factor.den;
michael@0 451 (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col
michael@0 452 *cpi->oxcf.mr_down_sampling_factor.num
michael@0 453 /cpi->oxcf.mr_down_sampling_factor.den;
michael@0 454
michael@0 455 vp8_clamp_mv2(parent_ref_mv, xd);
michael@0 456 }
michael@0 457 }
michael@0 458 #endif
michael@0 459
michael@0 460 static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
michael@0 461 {
michael@0 462 MACROBLOCKD *xd = &x->e_mbd;
michael@0 463
michael@0 464 unsigned int threshold = (xd->block[0].dequant[1]
michael@0 465 * xd->block[0].dequant[1] >>4);
michael@0 466
michael@0 467 if(threshold < x->encode_breakout)
michael@0 468 threshold = x->encode_breakout;
michael@0 469
michael@0 470 if (sse < threshold )
michael@0 471 {
michael@0 472 /* Check u and v to make sure skip is ok */
michael@0 473 unsigned int sse2 = 0;
michael@0 474
michael@0 475 sse2 = VP8_UVSSE(x);
michael@0 476
michael@0 477 if (sse2 * 2 < x->encode_breakout)
michael@0 478 x->skip = 1;
michael@0 479 else
michael@0 480 x->skip = 0;
michael@0 481 }
michael@0 482 }
michael@0 483
michael@0 484 static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2,
michael@0 485 VP8_COMP *cpi, MACROBLOCK *x, int rd_adj)
michael@0 486 {
michael@0 487 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
michael@0 488 int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
michael@0 489 int this_rd;
michael@0 490 /* Exit early and don't compute the distortion if this macroblock
michael@0 491 * is marked inactive. */
michael@0 492 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
michael@0 493 {
michael@0 494 *sse = 0;
michael@0 495 *distortion2 = 0;
michael@0 496 x->skip = 1;
michael@0 497 return INT_MAX;
michael@0 498 }
michael@0 499
michael@0 500 if((this_mode != NEWMV) ||
michael@0 501 !(cpi->sf.half_pixel_search) || cpi->common.full_pixel==1)
michael@0 502 *distortion2 = vp8_get_inter_mbpred_error(x,
michael@0 503 &cpi->fn_ptr[BLOCK_16X16],
michael@0 504 sse, mv);
michael@0 505
michael@0 506 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);
michael@0 507
michael@0 508 /* Adjust rd to bias to ZEROMV */
michael@0 509 if(this_mode == ZEROMV)
michael@0 510 {
michael@0 511 /* Bias to ZEROMV on LAST_FRAME reference when it is available. */
michael@0 512 if ((cpi->ref_frame_flags & VP8_LAST_FRAME &
michael@0 513 cpi->common.refresh_last_frame)
michael@0 514 && x->e_mbd.mode_info_context->mbmi.ref_frame != LAST_FRAME)
michael@0 515 rd_adj = 100;
michael@0 516
michael@0 517 // rd_adj <= 100
michael@0 518 this_rd = ((int64_t)this_rd) * rd_adj / 100;
michael@0 519 }
michael@0 520
michael@0 521 check_for_encode_breakout(*sse, x);
michael@0 522 return this_rd;
michael@0 523 }
michael@0 524
michael@0 525 static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
michael@0 526 int *rd_adjustment)
michael@0 527 {
michael@0 528 MODE_INFO *mic = x->e_mbd.mode_info_context;
michael@0 529 int_mv mv_l, mv_a, mv_al;
michael@0 530 int local_motion_check = 0;
michael@0 531
michael@0 532 if (cpi->lf_zeromv_pct > 40)
michael@0 533 {
michael@0 534 /* left mb */
michael@0 535 mic -= 1;
michael@0 536 mv_l = mic->mbmi.mv;
michael@0 537
michael@0 538 if (mic->mbmi.ref_frame != INTRA_FRAME)
michael@0 539 if( abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
michael@0 540 local_motion_check++;
michael@0 541
michael@0 542 /* above-left mb */
michael@0 543 mic -= x->e_mbd.mode_info_stride;
michael@0 544 mv_al = mic->mbmi.mv;
michael@0 545
michael@0 546 if (mic->mbmi.ref_frame != INTRA_FRAME)
michael@0 547 if( abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
michael@0 548 local_motion_check++;
michael@0 549
michael@0 550 /* above mb */
michael@0 551 mic += 1;
michael@0 552 mv_a = mic->mbmi.mv;
michael@0 553
michael@0 554 if (mic->mbmi.ref_frame != INTRA_FRAME)
michael@0 555 if( abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
michael@0 556 local_motion_check++;
michael@0 557
michael@0 558 if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge)
michael@0 559 && local_motion_check >0) || local_motion_check >2 )
michael@0 560 *rd_adjustment = 80;
michael@0 561 else if (local_motion_check > 0)
michael@0 562 *rd_adjustment = 90;
michael@0 563 }
michael@0 564 }
michael@0 565
michael@0 566 void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
michael@0 567 int recon_uvoffset, int *returnrate,
michael@0 568 int *returndistortion, int *returnintra, int mb_row,
michael@0 569 int mb_col)
michael@0 570 {
michael@0 571 BLOCK *b = &x->block[0];
michael@0 572 BLOCKD *d = &x->e_mbd.block[0];
michael@0 573 MACROBLOCKD *xd = &x->e_mbd;
michael@0 574 MB_MODE_INFO best_mbmode;
michael@0 575
michael@0 576 int_mv best_ref_mv_sb[2];
michael@0 577 int_mv mode_mv_sb[2][MB_MODE_COUNT];
michael@0 578 int_mv best_ref_mv;
michael@0 579 int_mv *mode_mv;
michael@0 580 MB_PREDICTION_MODE this_mode;
michael@0 581 int num00;
michael@0 582 int mdcounts[4];
michael@0 583 int best_rd = INT_MAX;
michael@0 584 int rd_adjustment = 100;
michael@0 585 int best_intra_rd = INT_MAX;
michael@0 586 int mode_index;
michael@0 587 int rate;
michael@0 588 int rate2;
michael@0 589 int distortion2;
michael@0 590 int bestsme = INT_MAX;
michael@0 591 int best_mode_index = 0;
michael@0 592 unsigned int sse = INT_MAX, best_rd_sse = INT_MAX;
michael@0 593 #if CONFIG_TEMPORAL_DENOISING
michael@0 594 unsigned int zero_mv_sse = INT_MAX, best_sse = INT_MAX;
michael@0 595 #endif
michael@0 596
michael@0 597 int sf_improved_mv_pred = cpi->sf.improved_mv_pred;
michael@0 598 int_mv mvp;
michael@0 599
michael@0 600 int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
michael@0 601 int saddone=0;
michael@0 602 /* search range got from mv_pred(). It uses step_param levels. (0-7) */
michael@0 603 int sr=0;
michael@0 604
michael@0 605 unsigned char *plane[4][3];
michael@0 606 int ref_frame_map[4];
michael@0 607 int sign_bias = 0;
michael@0 608
michael@0 609 #if CONFIG_MULTI_RES_ENCODING
michael@0 610 int dissim = INT_MAX;
michael@0 611 int parent_ref_frame = 0;
michael@0 612 int parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
michael@0 613 int_mv parent_ref_mv;
michael@0 614 MB_PREDICTION_MODE parent_mode = 0;
michael@0 615
michael@0 616 if (parent_ref_valid)
michael@0 617 {
michael@0 618 int parent_ref_flag;
michael@0 619
michael@0 620 get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
michael@0 621 &parent_mode, &parent_ref_mv, mb_row, mb_col);
michael@0 622
michael@0 623 /* TODO(jkoleszar): The references available (ref_frame_flags) to the
michael@0 624 * lower res encoder should match those available to this encoder, but
michael@0 625 * there seems to be a situation where this mismatch can happen in the
michael@0 626 * case of frame dropping and temporal layers. For example,
michael@0 627 * GOLD being disallowed in ref_frame_flags, but being returned as
michael@0 628 * parent_ref_frame.
michael@0 629 *
michael@0 630 * In this event, take the conservative approach of disabling the
michael@0 631 * lower res info for this MB.
michael@0 632 */
michael@0 633 parent_ref_flag = 0;
michael@0 634 if (parent_ref_frame == LAST_FRAME)
michael@0 635 parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
michael@0 636 else if (parent_ref_frame == GOLDEN_FRAME)
michael@0 637 parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);
michael@0 638 else if (parent_ref_frame == ALTREF_FRAME)
michael@0 639 parent_ref_flag = (cpi->ref_frame_flags & VP8_ALTR_FRAME);
michael@0 640
michael@0 641 //assert(!parent_ref_frame || parent_ref_flag);
michael@0 642 if (parent_ref_frame && !parent_ref_flag)
michael@0 643 parent_ref_valid = 0;
michael@0 644 }
michael@0 645 #endif
michael@0 646
michael@0 647 mode_mv = mode_mv_sb[sign_bias];
michael@0 648 best_ref_mv.as_int = 0;
michael@0 649 vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
michael@0 650 vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
michael@0 651
michael@0 652 /* Setup search priorities */
michael@0 653 #if CONFIG_MULTI_RES_ENCODING
michael@0 654 if (parent_ref_valid && parent_ref_frame && dissim < 8)
michael@0 655 {
michael@0 656 ref_frame_map[0] = -1;
michael@0 657 ref_frame_map[1] = parent_ref_frame;
michael@0 658 ref_frame_map[2] = -1;
michael@0 659 ref_frame_map[3] = -1;
michael@0 660 } else
michael@0 661 #endif
michael@0 662 get_reference_search_order(cpi, ref_frame_map);
michael@0 663
michael@0 664 /* Check to see if there is at least 1 valid reference frame that we need
michael@0 665 * to calculate near_mvs.
michael@0 666 */
michael@0 667 if (ref_frame_map[1] > 0)
michael@0 668 {
michael@0 669 sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
michael@0 670 x->e_mbd.mode_info_context,
michael@0 671 mode_mv_sb,
michael@0 672 best_ref_mv_sb,
michael@0 673 mdcounts,
michael@0 674 ref_frame_map[1],
michael@0 675 cpi->common.ref_frame_sign_bias);
michael@0 676
michael@0 677 mode_mv = mode_mv_sb[sign_bias];
michael@0 678 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
michael@0 679 }
michael@0 680
michael@0 681 get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
michael@0 682
michael@0 683 /* Count of the number of MBs tested so far this frame */
michael@0 684 x->mbs_tested_so_far++;
michael@0 685
michael@0 686 *returnintra = INT_MAX;
michael@0 687 x->skip = 0;
michael@0 688
michael@0 689 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
michael@0 690
michael@0 691 /* If the frame has big static background and current MB is in low
michael@0 692 * motion area, its mode decision is biased to ZEROMV mode.
michael@0 693 */
michael@0 694 calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
michael@0 695
michael@0 696 /* if we encode a new mv this is important
michael@0 697 * find the best new motion vector
michael@0 698 */
michael@0 699 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
michael@0 700 {
michael@0 701 int frame_cost;
michael@0 702 int this_rd = INT_MAX;
michael@0 703 int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
michael@0 704
michael@0 705 if (best_rd <= x->rd_threshes[mode_index])
michael@0 706 continue;
michael@0 707
michael@0 708 if (this_ref_frame < 0)
michael@0 709 continue;
michael@0 710
michael@0 711 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
michael@0 712
michael@0 713 /* everything but intra */
michael@0 714 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
michael@0 715 {
michael@0 716 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
michael@0 717 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
michael@0 718 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
michael@0 719
michael@0 720 if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
michael@0 721 {
michael@0 722 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
michael@0 723 mode_mv = mode_mv_sb[sign_bias];
michael@0 724 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
michael@0 725 }
michael@0 726
michael@0 727 #if CONFIG_MULTI_RES_ENCODING
michael@0 728 if (parent_ref_valid)
michael@0 729 {
michael@0 730 if (vp8_mode_order[mode_index] == NEARESTMV &&
michael@0 731 mode_mv[NEARESTMV].as_int ==0)
michael@0 732 continue;
michael@0 733 if (vp8_mode_order[mode_index] == NEARMV &&
michael@0 734 mode_mv[NEARMV].as_int ==0)
michael@0 735 continue;
michael@0 736
michael@0 737 if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV
michael@0 738 && best_ref_mv.as_int==0)
michael@0 739 continue;
michael@0 740 else if(vp8_mode_order[mode_index] == NEWMV && dissim==0
michael@0 741 && best_ref_mv.as_int==parent_ref_mv.as_int)
michael@0 742 continue;
michael@0 743 }
michael@0 744 #endif
michael@0 745 }
michael@0 746
michael@0 747 /* Check to see if the testing frequency for this mode is at its max
michael@0 748 * If so then prevent it from being tested and increase the threshold
michael@0 749 * for its testing */
michael@0 750 if (x->mode_test_hit_counts[mode_index] &&
michael@0 751 (cpi->mode_check_freq[mode_index] > 1))
michael@0 752 {
michael@0 753 if (x->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
michael@0 754 x->mode_test_hit_counts[mode_index]))
michael@0 755 {
michael@0 756 /* Increase the threshold for coding this mode to make it less
michael@0 757 * likely to be chosen */
michael@0 758 x->rd_thresh_mult[mode_index] += 4;
michael@0 759
michael@0 760 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
michael@0 761 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
michael@0 762
michael@0 763 x->rd_threshes[mode_index] =
michael@0 764 (cpi->rd_baseline_thresh[mode_index] >> 7) *
michael@0 765 x->rd_thresh_mult[mode_index];
michael@0 766 continue;
michael@0 767 }
michael@0 768 }
michael@0 769
michael@0 770 /* We have now reached the point where we are going to test the current
michael@0 771 * mode so increment the counter for the number of times it has been
michael@0 772 * tested */
michael@0 773 x->mode_test_hit_counts[mode_index] ++;
michael@0 774
michael@0 775 rate2 = 0;
michael@0 776 distortion2 = 0;
michael@0 777
michael@0 778 this_mode = vp8_mode_order[mode_index];
michael@0 779
michael@0 780 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
michael@0 781 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
michael@0 782
michael@0 783 /* Work out the cost assosciated with selecting the reference frame */
michael@0 784 frame_cost =
michael@0 785 x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
michael@0 786 rate2 += frame_cost;
michael@0 787
michael@0 788 /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
michael@0 789 * unless ARNR filtering is enabled in which case we want
michael@0 790 * an unfiltered alternative */
michael@0 791 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
michael@0 792 {
michael@0 793 if (this_mode != ZEROMV ||
michael@0 794 x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
michael@0 795 continue;
michael@0 796 }
michael@0 797
michael@0 798 switch (this_mode)
michael@0 799 {
michael@0 800 case B_PRED:
michael@0 801 /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
michael@0 802 distortion2 = best_rd_sse;
michael@0 803 pick_intra4x4mby_modes(x, &rate, &distortion2);
michael@0 804
michael@0 805 if (distortion2 == INT_MAX)
michael@0 806 {
michael@0 807 this_rd = INT_MAX;
michael@0 808 }
michael@0 809 else
michael@0 810 {
michael@0 811 rate2 += rate;
michael@0 812 distortion2 = vp8_variance16x16(
michael@0 813 *(b->base_src), b->src_stride,
michael@0 814 x->e_mbd.predictor, 16, &sse);
michael@0 815 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
michael@0 816
michael@0 817 if (this_rd < best_intra_rd)
michael@0 818 {
michael@0 819 best_intra_rd = this_rd;
michael@0 820 *returnintra = distortion2;
michael@0 821 }
michael@0 822 }
michael@0 823
michael@0 824 break;
michael@0 825
michael@0 826 case SPLITMV:
michael@0 827
michael@0 828 /* Split MV modes currently not supported when RD is not enabled. */
michael@0 829 break;
michael@0 830
michael@0 831 case DC_PRED:
michael@0 832 case V_PRED:
michael@0 833 case H_PRED:
michael@0 834 case TM_PRED:
michael@0 835 vp8_build_intra_predictors_mby_s(xd,
michael@0 836 xd->dst.y_buffer - xd->dst.y_stride,
michael@0 837 xd->dst.y_buffer - 1,
michael@0 838 xd->dst.y_stride,
michael@0 839 xd->predictor,
michael@0 840 16);
michael@0 841 distortion2 = vp8_variance16x16
michael@0 842 (*(b->base_src), b->src_stride,
michael@0 843 x->e_mbd.predictor, 16, &sse);
michael@0 844 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
michael@0 845 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
michael@0 846
michael@0 847 if (this_rd < best_intra_rd)
michael@0 848 {
michael@0 849 best_intra_rd = this_rd;
michael@0 850 *returnintra = distortion2;
michael@0 851 }
michael@0 852 break;
michael@0 853
michael@0 854 case NEWMV:
michael@0 855 {
michael@0 856 int thissme;
michael@0 857 int step_param;
michael@0 858 int further_steps;
michael@0 859 int n = 0;
michael@0 860 int sadpb = x->sadperbit16;
michael@0 861 int_mv mvp_full;
michael@0 862
michael@0 863 int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
michael@0 864 int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
michael@0 865 int col_max = (best_ref_mv.as_mv.col>>3)
michael@0 866 + MAX_FULL_PEL_VAL;
michael@0 867 int row_max = (best_ref_mv.as_mv.row>>3)
michael@0 868 + MAX_FULL_PEL_VAL;
michael@0 869
michael@0 870 int tmp_col_min = x->mv_col_min;
michael@0 871 int tmp_col_max = x->mv_col_max;
michael@0 872 int tmp_row_min = x->mv_row_min;
michael@0 873 int tmp_row_max = x->mv_row_max;
michael@0 874
michael@0 875 int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
michael@0 876
michael@0 877 /* Further step/diamond searches as necessary */
michael@0 878 step_param = cpi->sf.first_step + speed_adjust;
michael@0 879
michael@0 880 #if CONFIG_MULTI_RES_ENCODING
michael@0 881 /* If lower-res drops this frame, then higher-res encoder does
michael@0 882 motion search without any previous knowledge. Also, since
michael@0 883 last frame motion info is not stored, then we can not
michael@0 884 use improved_mv_pred. */
michael@0 885 if (cpi->oxcf.mr_encoder_id && !parent_ref_valid)
michael@0 886 sf_improved_mv_pred = 0;
michael@0 887
michael@0 888 if (parent_ref_valid && parent_ref_frame)
michael@0 889 {
michael@0 890 /* Use parent MV as predictor. Adjust search range
michael@0 891 * accordingly.
michael@0 892 */
michael@0 893 mvp.as_int = parent_ref_mv.as_int;
michael@0 894 mvp_full.as_mv.col = parent_ref_mv.as_mv.col>>3;
michael@0 895 mvp_full.as_mv.row = parent_ref_mv.as_mv.row>>3;
michael@0 896
michael@0 897 if(dissim <=32) step_param += 3;
michael@0 898 else if(dissim <=128) step_param += 2;
michael@0 899 else step_param += 1;
michael@0 900 }else
michael@0 901 #endif
michael@0 902 {
michael@0 903 if(sf_improved_mv_pred)
michael@0 904 {
michael@0 905 if(!saddone)
michael@0 906 {
michael@0 907 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
michael@0 908 saddone = 1;
michael@0 909 }
michael@0 910
michael@0 911 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context,
michael@0 912 &mvp,x->e_mbd.mode_info_context->mbmi.ref_frame,
michael@0 913 cpi->common.ref_frame_sign_bias, &sr,
michael@0 914 &near_sadidx[0]);
michael@0 915
michael@0 916 sr += speed_adjust;
michael@0 917 /* adjust search range according to sr from mv prediction */
michael@0 918 if(sr > step_param)
michael@0 919 step_param = sr;
michael@0 920
michael@0 921 mvp_full.as_mv.col = mvp.as_mv.col>>3;
michael@0 922 mvp_full.as_mv.row = mvp.as_mv.row>>3;
michael@0 923 }else
michael@0 924 {
michael@0 925 mvp.as_int = best_ref_mv.as_int;
michael@0 926 mvp_full.as_mv.col = best_ref_mv.as_mv.col>>3;
michael@0 927 mvp_full.as_mv.row = best_ref_mv.as_mv.row>>3;
michael@0 928 }
michael@0 929 }
michael@0 930
michael@0 931 #if CONFIG_MULTI_RES_ENCODING
michael@0 932 if (parent_ref_valid && parent_ref_frame && dissim <= 2 &&
michael@0 933 MAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
michael@0 934 abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <= 4)
michael@0 935 {
michael@0 936 d->bmi.mv.as_int = mvp_full.as_int;
michael@0 937 mode_mv[NEWMV].as_int = mvp_full.as_int;
michael@0 938
michael@0 939 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
michael@0 940 x->errorperbit,
michael@0 941 &cpi->fn_ptr[BLOCK_16X16],
michael@0 942 cpi->mb.mvcost,
michael@0 943 &distortion2,&sse);
michael@0 944 }else
michael@0 945 #endif
michael@0 946 {
michael@0 947 /* Get intersection of UMV window and valid MV window to
michael@0 948 * reduce # of checks in diamond search. */
michael@0 949 if (x->mv_col_min < col_min )
michael@0 950 x->mv_col_min = col_min;
michael@0 951 if (x->mv_col_max > col_max )
michael@0 952 x->mv_col_max = col_max;
michael@0 953 if (x->mv_row_min < row_min )
michael@0 954 x->mv_row_min = row_min;
michael@0 955 if (x->mv_row_max > row_max )
michael@0 956 x->mv_row_max = row_max;
michael@0 957
michael@0 958 further_steps = (cpi->Speed >= 8)?
michael@0 959 0: (cpi->sf.max_step_search_steps - 1 - step_param);
michael@0 960
michael@0 961 if (cpi->sf.search_method == HEX)
michael@0 962 {
michael@0 963 #if CONFIG_MULTI_RES_ENCODING
michael@0 964 /* TODO: In higher-res pick_inter_mode, step_param is used to
michael@0 965 * modify hex search range. Here, set step_param to 0 not to
michael@0 966 * change the behavior in lowest-resolution encoder.
michael@0 967 * Will improve it later.
michael@0 968 */
michael@0 969 /* Set step_param to 0 to ensure large-range motion search
michael@0 970 when encoder drops this frame at lower-resolution.
michael@0 971 */
michael@0 972 if (!parent_ref_valid)
michael@0 973 step_param = 0;
michael@0 974 #endif
michael@0 975 bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv,
michael@0 976 step_param, sadpb,
michael@0 977 &cpi->fn_ptr[BLOCK_16X16],
michael@0 978 x->mvsadcost, x->mvcost, &best_ref_mv);
michael@0 979 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
michael@0 980 }
michael@0 981 else
michael@0 982 {
michael@0 983 bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full,
michael@0 984 &d->bmi.mv, step_param, sadpb, &num00,
michael@0 985 &cpi->fn_ptr[BLOCK_16X16],
michael@0 986 x->mvcost, &best_ref_mv);
michael@0 987 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
michael@0 988
michael@0 989 /* Further step/diamond searches as necessary */
michael@0 990 n = num00;
michael@0 991 num00 = 0;
michael@0 992
michael@0 993 while (n < further_steps)
michael@0 994 {
michael@0 995 n++;
michael@0 996
michael@0 997 if (num00)
michael@0 998 num00--;
michael@0 999 else
michael@0 1000 {
michael@0 1001 thissme =
michael@0 1002 cpi->diamond_search_sad(x, b, d, &mvp_full,
michael@0 1003 &d->bmi.mv,
michael@0 1004 step_param + n,
michael@0 1005 sadpb, &num00,
michael@0 1006 &cpi->fn_ptr[BLOCK_16X16],
michael@0 1007 x->mvcost, &best_ref_mv);
michael@0 1008 if (thissme < bestsme)
michael@0 1009 {
michael@0 1010 bestsme = thissme;
michael@0 1011 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
michael@0 1012 }
michael@0 1013 else
michael@0 1014 {
michael@0 1015 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
michael@0 1016 }
michael@0 1017 }
michael@0 1018 }
michael@0 1019 }
michael@0 1020
michael@0 1021 x->mv_col_min = tmp_col_min;
michael@0 1022 x->mv_col_max = tmp_col_max;
michael@0 1023 x->mv_row_min = tmp_row_min;
michael@0 1024 x->mv_row_max = tmp_row_max;
michael@0 1025
michael@0 1026 if (bestsme < INT_MAX)
michael@0 1027 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
michael@0 1028 &best_ref_mv, x->errorperbit,
michael@0 1029 &cpi->fn_ptr[BLOCK_16X16],
michael@0 1030 cpi->mb.mvcost,
michael@0 1031 &distortion2,&sse);
michael@0 1032 }
michael@0 1033
michael@0 1034 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
michael@0 1035
michael@0 1036 /* mv cost; */
michael@0 1037 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
michael@0 1038 cpi->mb.mvcost, 128);
michael@0 1039 }
michael@0 1040
michael@0 1041 case NEARESTMV:
michael@0 1042 case NEARMV:
michael@0 1043
michael@0 1044 if (mode_mv[this_mode].as_int == 0)
michael@0 1045 continue;
michael@0 1046
michael@0 1047 case ZEROMV:
michael@0 1048
michael@0 1049 /* Trap vectors that reach beyond the UMV borders
michael@0 1050 * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
michael@0 1051 * through to this point because of the lack of break statements
michael@0 1052 * in the previous two cases.
michael@0 1053 */
michael@0 1054 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
michael@0 1055 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
michael@0 1056 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
michael@0 1057 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
michael@0 1058 continue;
michael@0 1059
michael@0 1060 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
michael@0 1061 x->e_mbd.mode_info_context->mbmi.mv.as_int =
michael@0 1062 mode_mv[this_mode].as_int;
michael@0 1063 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
michael@0 1064 rd_adjustment);
michael@0 1065
michael@0 1066 break;
michael@0 1067 default:
michael@0 1068 break;
michael@0 1069 }
michael@0 1070
michael@0 1071 #if CONFIG_TEMPORAL_DENOISING
michael@0 1072 if (cpi->oxcf.noise_sensitivity)
michael@0 1073 {
michael@0 1074
michael@0 1075 /* Store for later use by denoiser. */
michael@0 1076 if (this_mode == ZEROMV && sse < zero_mv_sse )
michael@0 1077 {
michael@0 1078 zero_mv_sse = sse;
michael@0 1079 x->best_zeromv_reference_frame =
michael@0 1080 x->e_mbd.mode_info_context->mbmi.ref_frame;
michael@0 1081 }
michael@0 1082
michael@0 1083 /* Store the best NEWMV in x for later use in the denoiser. */
michael@0 1084 if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
michael@0 1085 sse < best_sse)
michael@0 1086 {
michael@0 1087 best_sse = sse;
michael@0 1088 x->best_sse_inter_mode = NEWMV;
michael@0 1089 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
michael@0 1090 x->need_to_clamp_best_mvs =
michael@0 1091 x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
michael@0 1092 x->best_reference_frame =
michael@0 1093 x->e_mbd.mode_info_context->mbmi.ref_frame;
michael@0 1094 }
michael@0 1095 }
michael@0 1096 #endif
michael@0 1097
michael@0 1098 if (this_rd < best_rd || x->skip)
michael@0 1099 {
michael@0 1100 /* Note index of best mode */
michael@0 1101 best_mode_index = mode_index;
michael@0 1102
michael@0 1103 *returnrate = rate2;
michael@0 1104 *returndistortion = distortion2;
michael@0 1105 best_rd_sse = sse;
michael@0 1106 best_rd = this_rd;
michael@0 1107 vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
michael@0 1108 sizeof(MB_MODE_INFO));
michael@0 1109
michael@0 1110 /* Testing this mode gave rise to an improvement in best error
michael@0 1111 * score. Lower threshold a bit for next time
michael@0 1112 */
michael@0 1113 x->rd_thresh_mult[mode_index] =
michael@0 1114 (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
michael@0 1115 x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
michael@0 1116 x->rd_threshes[mode_index] =
michael@0 1117 (cpi->rd_baseline_thresh[mode_index] >> 7) *
michael@0 1118 x->rd_thresh_mult[mode_index];
michael@0 1119 }
michael@0 1120
michael@0 1121 /* If the mode did not help improve the best error case then raise the
michael@0 1122 * threshold for testing that mode next time around.
michael@0 1123 */
michael@0 1124 else
michael@0 1125 {
michael@0 1126 x->rd_thresh_mult[mode_index] += 4;
michael@0 1127
michael@0 1128 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
michael@0 1129 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
michael@0 1130
michael@0 1131 x->rd_threshes[mode_index] =
michael@0 1132 (cpi->rd_baseline_thresh[mode_index] >> 7) *
michael@0 1133 x->rd_thresh_mult[mode_index];
michael@0 1134 }
michael@0 1135
michael@0 1136 if (x->skip)
michael@0 1137 break;
michael@0 1138 }
michael@0 1139
michael@0 1140 /* Reduce the activation RD thresholds for the best choice mode */
michael@0 1141 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
michael@0 1142 {
michael@0 1143 int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 3);
michael@0 1144
michael@0 1145 x->rd_thresh_mult[best_mode_index] =
michael@0 1146 (x->rd_thresh_mult[best_mode_index]
michael@0 1147 >= (MIN_THRESHMULT + best_adjustment)) ?
michael@0 1148 x->rd_thresh_mult[best_mode_index] - best_adjustment :
michael@0 1149 MIN_THRESHMULT;
michael@0 1150 x->rd_threshes[best_mode_index] =
michael@0 1151 (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
michael@0 1152 x->rd_thresh_mult[best_mode_index];
michael@0 1153 }
michael@0 1154
michael@0 1155
michael@0 1156 {
michael@0 1157 int this_rdbin = (*returndistortion >> 7);
michael@0 1158
michael@0 1159 if (this_rdbin >= 1024)
michael@0 1160 {
michael@0 1161 this_rdbin = 1023;
michael@0 1162 }
michael@0 1163
michael@0 1164 x->error_bins[this_rdbin] ++;
michael@0 1165 }
michael@0 1166
michael@0 1167 #if CONFIG_TEMPORAL_DENOISING
michael@0 1168 if (cpi->oxcf.noise_sensitivity)
michael@0 1169 {
michael@0 1170 if (x->best_sse_inter_mode == DC_PRED)
michael@0 1171 {
michael@0 1172 /* No best MV found. */
michael@0 1173 x->best_sse_inter_mode = best_mbmode.mode;
michael@0 1174 x->best_sse_mv = best_mbmode.mv;
michael@0 1175 x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
michael@0 1176 x->best_reference_frame = best_mbmode.ref_frame;
michael@0 1177 best_sse = best_rd_sse;
michael@0 1178 }
michael@0 1179 vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
michael@0 1180 recon_yoffset, recon_uvoffset);
michael@0 1181
michael@0 1182
michael@0 1183 /* Reevaluate ZEROMV after denoising. */
michael@0 1184 if (best_mbmode.ref_frame == INTRA_FRAME &&
michael@0 1185 x->best_zeromv_reference_frame != INTRA_FRAME)
michael@0 1186 {
michael@0 1187 int this_rd = 0;
michael@0 1188 int this_ref_frame = x->best_zeromv_reference_frame;
michael@0 1189 rate2 = x->ref_frame_cost[this_ref_frame] +
michael@0 1190 vp8_cost_mv_ref(ZEROMV, mdcounts);
michael@0 1191 distortion2 = 0;
michael@0 1192
michael@0 1193 /* set up the proper prediction buffers for the frame */
michael@0 1194 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
michael@0 1195 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
michael@0 1196 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
michael@0 1197 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
michael@0 1198
michael@0 1199 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
michael@0 1200 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
michael@0 1201 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
michael@0 1202 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
michael@0 1203 rd_adjustment);
michael@0 1204
michael@0 1205 if (this_rd < best_rd)
michael@0 1206 {
michael@0 1207 vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
michael@0 1208 sizeof(MB_MODE_INFO));
michael@0 1209 }
michael@0 1210 }
michael@0 1211
michael@0 1212 }
michael@0 1213 #endif
michael@0 1214
michael@0 1215 if (cpi->is_src_frame_alt_ref &&
michael@0 1216 (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
michael@0 1217 {
michael@0 1218 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
michael@0 1219 x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
michael@0 1220 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
michael@0 1221 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
michael@0 1222 x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
michael@0 1223 (cpi->common.mb_no_coeff_skip);
michael@0 1224 x->e_mbd.mode_info_context->mbmi.partitioning = 0;
michael@0 1225
michael@0 1226 return;
michael@0 1227 }
michael@0 1228
michael@0 1229 /* set to the best mb mode, this copy can be skip if x->skip since it
michael@0 1230 * already has the right content */
michael@0 1231 if (!x->skip)
michael@0 1232 vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode,
michael@0 1233 sizeof(MB_MODE_INFO));
michael@0 1234
michael@0 1235 if (best_mbmode.mode <= B_PRED)
michael@0 1236 {
michael@0 1237 /* set mode_info_context->mbmi.uv_mode */
michael@0 1238 pick_intra_mbuv_mode(x);
michael@0 1239 }
michael@0 1240
michael@0 1241 if (sign_bias
michael@0 1242 != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
michael@0 1243 best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
michael@0 1244
michael@0 1245 update_mvcount(x, &best_ref_mv);
michael@0 1246 }
michael@0 1247
michael@0 1248
michael@0 1249 void vp8_pick_intra_mode(MACROBLOCK *x, int *rate_)
michael@0 1250 {
michael@0 1251 int error4x4, error16x16 = INT_MAX;
michael@0 1252 int rate, best_rate = 0, distortion, best_sse;
michael@0 1253 MB_PREDICTION_MODE mode, best_mode = DC_PRED;
michael@0 1254 int this_rd;
michael@0 1255 unsigned int sse;
michael@0 1256 BLOCK *b = &x->block[0];
michael@0 1257 MACROBLOCKD *xd = &x->e_mbd;
michael@0 1258
michael@0 1259 xd->mode_info_context->mbmi.ref_frame = INTRA_FRAME;
michael@0 1260
michael@0 1261 pick_intra_mbuv_mode(x);
michael@0 1262
michael@0 1263 for (mode = DC_PRED; mode <= TM_PRED; mode ++)
michael@0 1264 {
michael@0 1265 xd->mode_info_context->mbmi.mode = mode;
michael@0 1266 vp8_build_intra_predictors_mby_s(xd,
michael@0 1267 xd->dst.y_buffer - xd->dst.y_stride,
michael@0 1268 xd->dst.y_buffer - 1,
michael@0 1269 xd->dst.y_stride,
michael@0 1270 xd->predictor,
michael@0 1271 16);
michael@0 1272 distortion = vp8_variance16x16
michael@0 1273 (*(b->base_src), b->src_stride, xd->predictor, 16, &sse);
michael@0 1274 rate = x->mbmode_cost[xd->frame_type][mode];
michael@0 1275 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
michael@0 1276
michael@0 1277 if (error16x16 > this_rd)
michael@0 1278 {
michael@0 1279 error16x16 = this_rd;
michael@0 1280 best_mode = mode;
michael@0 1281 best_sse = sse;
michael@0 1282 best_rate = rate;
michael@0 1283 }
michael@0 1284 }
michael@0 1285 xd->mode_info_context->mbmi.mode = best_mode;
michael@0 1286
michael@0 1287 error4x4 = pick_intra4x4mby_modes(x, &rate,
michael@0 1288 &best_sse);
michael@0 1289 if (error4x4 < error16x16)
michael@0 1290 {
michael@0 1291 xd->mode_info_context->mbmi.mode = B_PRED;
michael@0 1292 best_rate = rate;
michael@0 1293 }
michael@0 1294
michael@0 1295 *rate_ = best_rate;
michael@0 1296 }

mercurial