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
12 #include <limits.h>
13 #include "vpx_config.h"
14 #include "./vpx_dsp_rtcd.h"
15 #include "onyx_int.h"
16 #include "modecosts.h"
17 #include "encodeintra.h"
18 #include "vp8/common/common.h"
19 #include "vp8/common/entropymode.h"
20 #include "pickinter.h"
21 #include "vp8/common/findnearmv.h"
22 #include "encodemb.h"
23 #include "vp8/common/reconinter.h"
24 #include "vp8/common/reconintra4x4.h"
25 #include "vp8/common/variance.h"
26 #include "mcomp.h"
27 #include "rdopt.h"
28 #include "vpx_mem/vpx_mem.h"
29 #if CONFIG_TEMPORAL_DENOISING
30 #include "denoising.h"
31 #endif
32
33 #ifdef SPEEDSTATS
34 extern unsigned int cnt_pm;
35 #endif
36
37 extern const int vp8_ref_frame_order[MAX_MODES];
38 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
39
40 // Fixed point implementation of a skin color classifier. Skin color
41 // is model by a Gaussian distribution in the CbCr color space.
42 // See ../../test/skin_color_detector_test.cc where the reference
43 // skin color classifier is defined.
44
45 // Fixed-point skin color model parameters.
46 static const int skin_mean[2] = {7463, 9614}; // q6
47 static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157}; // q16
48 static const int skin_threshold = 1570636; // q18
49
50 // Evaluates the Mahalanobis distance measure for the input CbCr values.
evaluate_skin_color_difference(int cb,int cr)51 static int evaluate_skin_color_difference(int cb, int cr)
52 {
53 const int cb_q6 = cb << 6;
54 const int cr_q6 = cr << 6;
55 const int cb_diff_q12 = (cb_q6 - skin_mean[0]) * (cb_q6 - skin_mean[0]);
56 const int cbcr_diff_q12 = (cb_q6 - skin_mean[0]) * (cr_q6 - skin_mean[1]);
57 const int cr_diff_q12 = (cr_q6 - skin_mean[1]) * (cr_q6 - skin_mean[1]);
58 const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
59 const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
60 const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
61 const int skin_diff = skin_inv_cov[0] * cb_diff_q2 +
62 skin_inv_cov[1] * cbcr_diff_q2 +
63 skin_inv_cov[2] * cbcr_diff_q2 +
64 skin_inv_cov[3] * cr_diff_q2;
65 return skin_diff;
66 }
67
macroblock_corner_grad(unsigned char * signal,int stride,int offsetx,int offsety,int sgnx,int sgny)68 static int macroblock_corner_grad(unsigned char* signal, int stride,
69 int offsetx, int offsety, int sgnx, int sgny)
70 {
71 int y1 = signal[offsetx * stride + offsety];
72 int y2 = signal[offsetx * stride + offsety + sgny];
73 int y3 = signal[(offsetx + sgnx) * stride + offsety];
74 int y4 = signal[(offsetx + sgnx) * stride + offsety + sgny];
75 return MAX(MAX(abs(y1 - y2), abs(y1 - y3)), abs(y1 - y4));
76 }
77
check_dot_artifact_candidate(VP8_COMP * cpi,MACROBLOCK * x,unsigned char * target_last,int stride,unsigned char * last_ref,int mb_row,int mb_col,int channel)78 static int check_dot_artifact_candidate(VP8_COMP *cpi,
79 MACROBLOCK *x,
80 unsigned char *target_last,
81 int stride,
82 unsigned char* last_ref,
83 int mb_row,
84 int mb_col,
85 int channel)
86 {
87 int threshold1 = 6;
88 int threshold2 = 3;
89 unsigned int max_num = (cpi->common.MBs) / 10;
90 int grad_last = 0;
91 int grad_source = 0;
92 int index = mb_row * cpi->common.mb_cols + mb_col;
93 // Threshold for #consecutive (base layer) frames using zero_last mode.
94 int num_frames = 30;
95 int shift = 15;
96 if (channel > 0) {
97 shift = 7;
98 }
99 if (cpi->oxcf.number_of_layers > 1)
100 {
101 num_frames = 20;
102 }
103 x->zero_last_dot_suppress = 0;
104 // Blocks on base layer frames that have been using ZEROMV_LAST repeatedly
105 // (i.e, at least |x| consecutive frames are candidates for increasing the
106 // rd adjustment for zero_last mode.
107 // Only allow this for at most |max_num| blocks per frame.
108 // Don't allow this for screen content input.
109 if (cpi->current_layer == 0 &&
110 cpi->consec_zero_last_mvbias[index] > num_frames &&
111 x->mbs_zero_last_dot_suppress < max_num &&
112 !cpi->oxcf.screen_content_mode)
113 {
114 // If this block is checked here, label it so we don't check it again until
115 // ~|x| framaes later.
116 x->zero_last_dot_suppress = 1;
117 // Dot artifact is noticeable as strong gradient at corners of macroblock,
118 // for flat areas. As a simple detector for now, we look for a high
119 // corner gradient on last ref, and a smaller gradient on source.
120 // Check 4 corners, return if any satisfy condition.
121 // Top-left:
122 grad_last = macroblock_corner_grad(last_ref, stride, 0, 0, 1, 1);
123 grad_source = macroblock_corner_grad(target_last, stride, 0, 0, 1, 1);
124 if (grad_last >= threshold1 && grad_source <= threshold2)
125 {
126 x->mbs_zero_last_dot_suppress++;
127 return 1;
128 }
129 // Top-right:
130 grad_last = macroblock_corner_grad(last_ref, stride, 0, shift, 1, -1);
131 grad_source = macroblock_corner_grad(target_last, stride, 0, shift, 1, -1);
132 if (grad_last >= threshold1 && grad_source <= threshold2)
133 {
134 x->mbs_zero_last_dot_suppress++;
135 return 1;
136 }
137 // Bottom-left:
138 grad_last = macroblock_corner_grad(last_ref, stride, shift, 0, -1, 1);
139 grad_source = macroblock_corner_grad(target_last, stride, shift, 0, -1, 1);
140 if (grad_last >= threshold1 && grad_source <= threshold2)
141 {
142 x->mbs_zero_last_dot_suppress++;
143 return 1;
144 }
145 // Bottom-right:
146 grad_last = macroblock_corner_grad(last_ref, stride, shift, shift, -1, -1);
147 grad_source = macroblock_corner_grad(target_last, stride, shift, shift, -1, -1);
148 if (grad_last >= threshold1 && grad_source <= threshold2)
149 {
150 x->mbs_zero_last_dot_suppress++;
151 return 1;
152 }
153 return 0;
154 }
155 return 0;
156 }
157
158 // Checks if the input yCbCr values corresponds to skin color.
is_skin_color(int y,int cb,int cr)159 static int is_skin_color(int y, int cb, int cr)
160 {
161 if (y < 40 || y > 220)
162 {
163 return 0;
164 }
165 return (evaluate_skin_color_difference(cb, cr) < skin_threshold);
166 }
167
vp8_skip_fractional_mv_step(MACROBLOCK * mb,BLOCK * b,BLOCKD * d,int_mv * bestmv,int_mv * ref_mv,int error_per_bit,const vp8_variance_fn_ptr_t * vfp,int * mvcost[2],int * distortion,unsigned int * sse)168 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
169 int_mv *bestmv, int_mv *ref_mv,
170 int error_per_bit,
171 const vp8_variance_fn_ptr_t *vfp,
172 int *mvcost[2], int *distortion,
173 unsigned int *sse)
174 {
175 (void) b;
176 (void) d;
177 (void) ref_mv;
178 (void) error_per_bit;
179 (void) vfp;
180 (void) mb;
181 (void) mvcost;
182 (void) distortion;
183 (void) sse;
184 bestmv->as_mv.row <<= 3;
185 bestmv->as_mv.col <<= 3;
186 return 0;
187 }
188
189
vp8_get_inter_mbpred_error(MACROBLOCK * mb,const vp8_variance_fn_ptr_t * vfp,unsigned int * sse,int_mv this_mv)190 int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
191 const vp8_variance_fn_ptr_t *vfp,
192 unsigned int *sse,
193 int_mv this_mv)
194 {
195
196 BLOCK *b = &mb->block[0];
197 BLOCKD *d = &mb->e_mbd.block[0];
198 unsigned char *what = (*(b->base_src) + b->src);
199 int what_stride = b->src_stride;
200 int pre_stride = mb->e_mbd.pre.y_stride;
201 unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset ;
202 int in_what_stride = pre_stride;
203 int xoffset = this_mv.as_mv.col & 7;
204 int yoffset = this_mv.as_mv.row & 7;
205
206 in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
207
208 if (xoffset | yoffset)
209 {
210 return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
211 }
212 else
213 {
214 return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
215 }
216
217 }
218
get_prediction_error(BLOCK * be,BLOCKD * b)219 static int get_prediction_error(BLOCK *be, BLOCKD *b)
220 {
221 unsigned char *sptr;
222 unsigned char *dptr;
223 sptr = (*(be->base_src) + be->src);
224 dptr = b->predictor;
225
226 return vpx_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
227
228 }
229
pick_intra4x4block(MACROBLOCK * x,int ib,B_PREDICTION_MODE * best_mode,const int * mode_costs,int * bestrate,int * bestdistortion)230 static int pick_intra4x4block(
231 MACROBLOCK *x,
232 int ib,
233 B_PREDICTION_MODE *best_mode,
234 const int *mode_costs,
235
236 int *bestrate,
237 int *bestdistortion)
238 {
239
240 BLOCKD *b = &x->e_mbd.block[ib];
241 BLOCK *be = &x->block[ib];
242 int dst_stride = x->e_mbd.dst.y_stride;
243 unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
244 B_PREDICTION_MODE mode;
245 int best_rd = INT_MAX;
246 int rate;
247 int distortion;
248
249 unsigned char *Above = dst - dst_stride;
250 unsigned char *yleft = dst - 1;
251 unsigned char top_left = Above[-1];
252
253 for (mode = B_DC_PRED; mode <= B_HE_PRED; mode++)
254 {
255 int this_rd;
256
257 rate = mode_costs[mode];
258
259 vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
260 b->predictor, 16, top_left);
261 distortion = get_prediction_error(be, b);
262 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
263
264 if (this_rd < best_rd)
265 {
266 *bestrate = rate;
267 *bestdistortion = distortion;
268 best_rd = this_rd;
269 *best_mode = mode;
270 }
271 }
272
273 b->bmi.as_mode = *best_mode;
274 vp8_encode_intra4x4block(x, ib);
275 return best_rd;
276 }
277
278
pick_intra4x4mby_modes(MACROBLOCK * mb,int * Rate,int * best_dist)279 static int pick_intra4x4mby_modes
280 (
281 MACROBLOCK *mb,
282 int *Rate,
283 int *best_dist
284 )
285 {
286 MACROBLOCKD *const xd = &mb->e_mbd;
287 int i;
288 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
289 int error;
290 int distortion = 0;
291 const int *bmode_costs;
292
293 intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
294
295 bmode_costs = mb->inter_bmode_costs;
296
297 for (i = 0; i < 16; i++)
298 {
299 MODE_INFO *const mic = xd->mode_info_context;
300 const int mis = xd->mode_info_stride;
301
302 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
303 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
304
305 if (mb->e_mbd.frame_type == KEY_FRAME)
306 {
307 const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
308 const B_PREDICTION_MODE L = left_block_mode(mic, i);
309
310 bmode_costs = mb->bmode_costs[A][L];
311 }
312
313
314 pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
315
316 cost += r;
317 distortion += d;
318 mic->bmi[i].as_mode = best_mode;
319
320 /* Break out case where we have already exceeded best so far value
321 * that was passed in
322 */
323 if (distortion > *best_dist)
324 break;
325 }
326
327 *Rate = cost;
328
329 if (i == 16)
330 {
331 *best_dist = distortion;
332 error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
333 }
334 else
335 {
336 *best_dist = INT_MAX;
337 error = INT_MAX;
338 }
339
340 return error;
341 }
342
pick_intra_mbuv_mode(MACROBLOCK * mb)343 static void pick_intra_mbuv_mode(MACROBLOCK *mb)
344 {
345
346 MACROBLOCKD *x = &mb->e_mbd;
347 unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
348 unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
349 unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
350 unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
351 int uvsrc_stride = mb->block[16].src_stride;
352 unsigned char uleft_col[8];
353 unsigned char vleft_col[8];
354 unsigned char utop_left = uabove_row[-1];
355 unsigned char vtop_left = vabove_row[-1];
356 int i, j;
357 int expected_udc;
358 int expected_vdc;
359 int shift;
360 int Uaverage = 0;
361 int Vaverage = 0;
362 int diff;
363 int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
364 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
365
366
367 for (i = 0; i < 8; i++)
368 {
369 uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
370 vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
371 }
372
373 if (!x->up_available && !x->left_available)
374 {
375 expected_udc = 128;
376 expected_vdc = 128;
377 }
378 else
379 {
380 shift = 2;
381
382 if (x->up_available)
383 {
384
385 for (i = 0; i < 8; i++)
386 {
387 Uaverage += uabove_row[i];
388 Vaverage += vabove_row[i];
389 }
390
391 shift ++;
392
393 }
394
395 if (x->left_available)
396 {
397 for (i = 0; i < 8; i++)
398 {
399 Uaverage += uleft_col[i];
400 Vaverage += vleft_col[i];
401 }
402
403 shift ++;
404
405 }
406
407 expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
408 expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
409 }
410
411
412 for (i = 0; i < 8; i++)
413 {
414 for (j = 0; j < 8; j++)
415 {
416
417 int predu = uleft_col[i] + uabove_row[j] - utop_left;
418 int predv = vleft_col[i] + vabove_row[j] - vtop_left;
419 int u_p, v_p;
420
421 u_p = usrc_ptr[j];
422 v_p = vsrc_ptr[j];
423
424 if (predu < 0)
425 predu = 0;
426
427 if (predu > 255)
428 predu = 255;
429
430 if (predv < 0)
431 predv = 0;
432
433 if (predv > 255)
434 predv = 255;
435
436
437 diff = u_p - expected_udc;
438 pred_error[DC_PRED] += diff * diff;
439 diff = v_p - expected_vdc;
440 pred_error[DC_PRED] += diff * diff;
441
442
443 diff = u_p - uabove_row[j];
444 pred_error[V_PRED] += diff * diff;
445 diff = v_p - vabove_row[j];
446 pred_error[V_PRED] += diff * diff;
447
448
449 diff = u_p - uleft_col[i];
450 pred_error[H_PRED] += diff * diff;
451 diff = v_p - vleft_col[i];
452 pred_error[H_PRED] += diff * diff;
453
454
455 diff = u_p - predu;
456 pred_error[TM_PRED] += diff * diff;
457 diff = v_p - predv;
458 pred_error[TM_PRED] += diff * diff;
459
460
461 }
462
463 usrc_ptr += uvsrc_stride;
464 vsrc_ptr += uvsrc_stride;
465
466 if (i == 3)
467 {
468 usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
469 vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
470 }
471
472
473
474 }
475
476
477 for (i = DC_PRED; i <= TM_PRED; i++)
478 {
479 if (best_error > pred_error[i])
480 {
481 best_error = pred_error[i];
482 best_mode = (MB_PREDICTION_MODE)i;
483 }
484 }
485
486
487 mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
488
489 }
490
update_mvcount(MACROBLOCK * x,int_mv * best_ref_mv)491 static void update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
492 {
493 MACROBLOCKD *xd = &x->e_mbd;
494 /* Split MV modes currently not supported when RD is nopt enabled,
495 * therefore, only need to modify MVcount in NEWMV mode. */
496 if (xd->mode_info_context->mbmi.mode == NEWMV)
497 {
498 x->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
499 best_ref_mv->as_mv.row) >> 1)]++;
500 x->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
501 best_ref_mv->as_mv.col) >> 1)]++;
502 }
503 }
504
505
506 #if CONFIG_MULTI_RES_ENCODING
507 static
get_lower_res_motion_info(VP8_COMP * cpi,MACROBLOCKD * xd,int * dissim,int * parent_ref_frame,MB_PREDICTION_MODE * parent_mode,int_mv * parent_ref_mv,int mb_row,int mb_col)508 void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd, int *dissim,
509 int *parent_ref_frame,
510 MB_PREDICTION_MODE *parent_mode,
511 int_mv *parent_ref_mv, int mb_row, int mb_col)
512 {
513 LOWER_RES_MB_INFO* store_mode_info
514 = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
515 unsigned int parent_mb_index;
516
517 /* Consider different down_sampling_factor. */
518 {
519 /* TODO: Removed the loop that supports special down_sampling_factor
520 * such as 2, 4, 8. Will revisit it if needed.
521 * Should also try using a look-up table to see if it helps
522 * performance. */
523 int parent_mb_row, parent_mb_col;
524
525 parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
526 /cpi->oxcf.mr_down_sampling_factor.num;
527 parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
528 /cpi->oxcf.mr_down_sampling_factor.num;
529 parent_mb_index = parent_mb_row*cpi->mr_low_res_mb_cols + parent_mb_col;
530 }
531
532 /* Read lower-resolution mode & motion result from memory.*/
533 *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
534 *parent_mode = store_mode_info[parent_mb_index].mode;
535 *dissim = store_mode_info[parent_mb_index].dissim;
536
537 /* For highest-resolution encoder, adjust dissim value. Lower its quality
538 * for good performance. */
539 if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
540 *dissim>>=1;
541
542 if(*parent_ref_frame != INTRA_FRAME)
543 {
544 /* Consider different down_sampling_factor.
545 * The result can be rounded to be more precise, but it takes more time.
546 */
547 (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row
548 *cpi->oxcf.mr_down_sampling_factor.num
549 /cpi->oxcf.mr_down_sampling_factor.den;
550 (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col
551 *cpi->oxcf.mr_down_sampling_factor.num
552 /cpi->oxcf.mr_down_sampling_factor.den;
553
554 vp8_clamp_mv2(parent_ref_mv, xd);
555 }
556 }
557 #endif
558
check_for_encode_breakout(unsigned int sse,MACROBLOCK * x)559 static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
560 {
561 MACROBLOCKD *xd = &x->e_mbd;
562
563 unsigned int threshold = (xd->block[0].dequant[1]
564 * xd->block[0].dequant[1] >>4);
565
566 if(threshold < x->encode_breakout)
567 threshold = x->encode_breakout;
568
569 if (sse < threshold )
570 {
571 /* Check u and v to make sure skip is ok */
572 unsigned int sse2 = 0;
573
574 sse2 = VP8_UVSSE(x);
575
576 if (sse2 * 2 < x->encode_breakout)
577 x->skip = 1;
578 else
579 x->skip = 0;
580 }
581 }
582
evaluate_inter_mode(unsigned int * sse,int rate2,int * distortion2,VP8_COMP * cpi,MACROBLOCK * x,int rd_adj)583 static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2,
584 VP8_COMP *cpi, MACROBLOCK *x, int rd_adj)
585 {
586 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
587 int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
588 int this_rd;
589 int denoise_aggressive = 0;
590 /* Exit early and don't compute the distortion if this macroblock
591 * is marked inactive. */
592 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
593 {
594 *sse = 0;
595 *distortion2 = 0;
596 x->skip = 1;
597 return INT_MAX;
598 }
599
600 if((this_mode != NEWMV) ||
601 !(cpi->sf.half_pixel_search) || cpi->common.full_pixel==1)
602 *distortion2 = vp8_get_inter_mbpred_error(x,
603 &cpi->fn_ptr[BLOCK_16X16],
604 sse, mv);
605
606 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);
607
608 #if CONFIG_TEMPORAL_DENOISING
609 if (cpi->oxcf.noise_sensitivity > 0) {
610 denoise_aggressive =
611 (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) ? 1 : 0;
612 }
613 #endif
614
615 // Adjust rd for ZEROMV and LAST, if LAST is the closest reference frame.
616 // TODO: We should also add condition on distance of closest to current.
617 if(!cpi->oxcf.screen_content_mode &&
618 this_mode == ZEROMV &&
619 x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME &&
620 (denoise_aggressive || (cpi->closest_reference_frame == LAST_FRAME)))
621 {
622 // No adjustment if block is considered to be skin area.
623 if(x->is_skin)
624 rd_adj = 100;
625
626 this_rd = ((int64_t)this_rd) * rd_adj / 100;
627 }
628
629 check_for_encode_breakout(*sse, x);
630 return this_rd;
631 }
632
calculate_zeromv_rd_adjustment(VP8_COMP * cpi,MACROBLOCK * x,int * rd_adjustment)633 static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
634 int *rd_adjustment)
635 {
636 MODE_INFO *mic = x->e_mbd.mode_info_context;
637 int_mv mv_l, mv_a, mv_al;
638 int local_motion_check = 0;
639
640 if (cpi->lf_zeromv_pct > 40)
641 {
642 /* left mb */
643 mic -= 1;
644 mv_l = mic->mbmi.mv;
645
646 if (mic->mbmi.ref_frame != INTRA_FRAME)
647 if( abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
648 local_motion_check++;
649
650 /* above-left mb */
651 mic -= x->e_mbd.mode_info_stride;
652 mv_al = mic->mbmi.mv;
653
654 if (mic->mbmi.ref_frame != INTRA_FRAME)
655 if( abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
656 local_motion_check++;
657
658 /* above mb */
659 mic += 1;
660 mv_a = mic->mbmi.mv;
661
662 if (mic->mbmi.ref_frame != INTRA_FRAME)
663 if( abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
664 local_motion_check++;
665
666 if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge)
667 && local_motion_check >0) || local_motion_check >2 )
668 *rd_adjustment = 80;
669 else if (local_motion_check > 0)
670 *rd_adjustment = 90;
671 }
672 }
673
vp8_pick_inter_mode(VP8_COMP * cpi,MACROBLOCK * x,int recon_yoffset,int recon_uvoffset,int * returnrate,int * returndistortion,int * returnintra,int mb_row,int mb_col)674 void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
675 int recon_uvoffset, int *returnrate,
676 int *returndistortion, int *returnintra, int mb_row,
677 int mb_col)
678 {
679 BLOCK *b = &x->block[0];
680 BLOCKD *d = &x->e_mbd.block[0];
681 MACROBLOCKD *xd = &x->e_mbd;
682 MB_MODE_INFO best_mbmode;
683
684 int_mv best_ref_mv_sb[2];
685 int_mv mode_mv_sb[2][MB_MODE_COUNT];
686 int_mv best_ref_mv;
687 int_mv *mode_mv;
688 MB_PREDICTION_MODE this_mode;
689 int num00;
690 int mdcounts[4];
691 int best_rd = INT_MAX;
692 int rd_adjustment = 100;
693 int best_intra_rd = INT_MAX;
694 int mode_index;
695 int rate;
696 int rate2;
697 int distortion2;
698 int bestsme = INT_MAX;
699 int best_mode_index = 0;
700 unsigned int sse = UINT_MAX, best_rd_sse = UINT_MAX;
701 #if CONFIG_TEMPORAL_DENOISING
702 unsigned int zero_mv_sse = UINT_MAX, best_sse = UINT_MAX;
703 #endif
704
705 int sf_improved_mv_pred = cpi->sf.improved_mv_pred;
706
707 #if CONFIG_MULTI_RES_ENCODING
708 int dissim = INT_MAX;
709 int parent_ref_frame = 0;
710 int_mv parent_ref_mv;
711 MB_PREDICTION_MODE parent_mode = 0;
712 int parent_ref_valid = 0;
713 #endif
714
715 int_mv mvp;
716
717 int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
718 int saddone=0;
719 /* search range got from mv_pred(). It uses step_param levels. (0-7) */
720 int sr=0;
721
722 unsigned char *plane[4][3];
723 int ref_frame_map[4];
724 int sign_bias = 0;
725 int dot_artifact_candidate = 0;
726 get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
727
728 // If the current frame is using LAST as a reference, check for
729 // biasing the mode selection for dot artifacts.
730 if (cpi->ref_frame_flags & VP8_LAST_FRAME) {
731 unsigned char* target_y = x->src.y_buffer;
732 unsigned char* target_u = x->block[16].src + *x->block[16].base_src;
733 unsigned char* target_v = x->block[20].src + *x->block[20].base_src;
734 int stride = x->src.y_stride;
735 int stride_uv = x->block[16].src_stride;
736 #if CONFIG_TEMPORAL_DENOISING
737 if (cpi->oxcf.noise_sensitivity) {
738 const int uv_denoise = (cpi->oxcf.noise_sensitivity >= 2) ? 1 : 0;
739 target_y =
740 cpi->denoiser.yv12_running_avg[LAST_FRAME].y_buffer + recon_yoffset;
741 stride = cpi->denoiser.yv12_running_avg[LAST_FRAME].y_stride;
742 if (uv_denoise) {
743 target_u =
744 cpi->denoiser.yv12_running_avg[LAST_FRAME].u_buffer +
745 recon_uvoffset;
746 target_v =
747 cpi->denoiser.yv12_running_avg[LAST_FRAME].v_buffer +
748 recon_uvoffset;
749 stride_uv = cpi->denoiser.yv12_running_avg[LAST_FRAME].uv_stride;
750 }
751 }
752 #endif
753 dot_artifact_candidate =
754 check_dot_artifact_candidate(cpi, x, target_y, stride,
755 plane[LAST_FRAME][0], mb_row, mb_col, 0);
756 // If not found in Y channel, check UV channel.
757 if (!dot_artifact_candidate) {
758 dot_artifact_candidate =
759 check_dot_artifact_candidate(cpi, x, target_u, stride_uv,
760 plane[LAST_FRAME][1], mb_row, mb_col, 1);
761 if (!dot_artifact_candidate) {
762 dot_artifact_candidate =
763 check_dot_artifact_candidate(cpi, x, target_v, stride_uv,
764 plane[LAST_FRAME][2], mb_row, mb_col, 2);
765 }
766 }
767 }
768
769 #if CONFIG_MULTI_RES_ENCODING
770 // |parent_ref_valid| will be set here if potentially we can do mv resue for
771 // this higher resol (|cpi->oxcf.mr_encoder_id| > 0) frame.
772 // |parent_ref_valid| may be reset depending on |parent_ref_frame| for
773 // the current macroblock below.
774 parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
775 if (parent_ref_valid)
776 {
777 int parent_ref_flag;
778
779 get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
780 &parent_mode, &parent_ref_mv, mb_row, mb_col);
781
782 /* TODO(jkoleszar): The references available (ref_frame_flags) to the
783 * lower res encoder should match those available to this encoder, but
784 * there seems to be a situation where this mismatch can happen in the
785 * case of frame dropping and temporal layers. For example,
786 * GOLD being disallowed in ref_frame_flags, but being returned as
787 * parent_ref_frame.
788 *
789 * In this event, take the conservative approach of disabling the
790 * lower res info for this MB.
791 */
792
793 parent_ref_flag = 0;
794 // Note availability for mv reuse is only based on last and golden.
795 if (parent_ref_frame == LAST_FRAME)
796 parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
797 else if (parent_ref_frame == GOLDEN_FRAME)
798 parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);
799
800 //assert(!parent_ref_frame || parent_ref_flag);
801
802 // If |parent_ref_frame| did not match either last or golden then
803 // shut off mv reuse.
804 if (parent_ref_frame && !parent_ref_flag)
805 parent_ref_valid = 0;
806
807 // Don't do mv reuse since we want to allow for another mode besides
808 // ZEROMV_LAST to remove dot artifact.
809 if (dot_artifact_candidate)
810 parent_ref_valid = 0;
811 }
812 #endif
813
814 // Check if current macroblock is in skin area.
815 {
816 const int y = x->src.y_buffer[7 * x->src.y_stride + 7];
817 const int cb = x->src.u_buffer[3 * x->src.uv_stride + 3];
818 const int cr = x->src.v_buffer[3 * x->src.uv_stride + 3];
819 x->is_skin = 0;
820 if (!cpi->oxcf.screen_content_mode)
821 x->is_skin = is_skin_color(y, cb, cr);
822 }
823 #if CONFIG_TEMPORAL_DENOISING
824 if (cpi->oxcf.noise_sensitivity) {
825 // Under aggressive denoising mode, should we use skin map to reduce denoiser
826 // and ZEROMV bias? Will need to revisit the accuracy of this detection for
827 // very noisy input. For now keep this as is (i.e., don't turn it off).
828 // if (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive)
829 // x->is_skin = 0;
830 }
831 #endif
832
833 mode_mv = mode_mv_sb[sign_bias];
834 best_ref_mv.as_int = 0;
835 memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
836 memset(&best_mbmode, 0, sizeof(best_mbmode));
837
838 /* Setup search priorities */
839 #if CONFIG_MULTI_RES_ENCODING
840 if (parent_ref_valid && parent_ref_frame && dissim < 8)
841 {
842 ref_frame_map[0] = -1;
843 ref_frame_map[1] = parent_ref_frame;
844 ref_frame_map[2] = -1;
845 ref_frame_map[3] = -1;
846 } else
847 #endif
848 get_reference_search_order(cpi, ref_frame_map);
849
850 /* Check to see if there is at least 1 valid reference frame that we need
851 * to calculate near_mvs.
852 */
853 if (ref_frame_map[1] > 0)
854 {
855 sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
856 x->e_mbd.mode_info_context,
857 mode_mv_sb,
858 best_ref_mv_sb,
859 mdcounts,
860 ref_frame_map[1],
861 cpi->common.ref_frame_sign_bias);
862
863 mode_mv = mode_mv_sb[sign_bias];
864 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
865 }
866
867 /* Count of the number of MBs tested so far this frame */
868 x->mbs_tested_so_far++;
869
870 *returnintra = INT_MAX;
871 x->skip = 0;
872
873 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
874
875 /* If the frame has big static background and current MB is in low
876 * motion area, its mode decision is biased to ZEROMV mode.
877 * No adjustment if cpu_used is <= -12 (i.e., cpi->Speed >= 12).
878 * At such speed settings, ZEROMV is already heavily favored.
879 */
880 if (cpi->Speed < 12) {
881 calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
882 }
883
884 #if CONFIG_TEMPORAL_DENOISING
885 if (cpi->oxcf.noise_sensitivity) {
886 rd_adjustment = (int)(rd_adjustment *
887 cpi->denoiser.denoise_pars.pickmode_mv_bias / 100);
888 }
889 #endif
890
891 if (dot_artifact_candidate)
892 {
893 // Bias against ZEROMV_LAST mode.
894 rd_adjustment = 150;
895 }
896
897
898 /* if we encode a new mv this is important
899 * find the best new motion vector
900 */
901 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
902 {
903 int frame_cost;
904 int this_rd = INT_MAX;
905 int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
906
907 if (best_rd <= x->rd_threshes[mode_index])
908 continue;
909
910 if (this_ref_frame < 0)
911 continue;
912
913 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
914
915 /* everything but intra */
916 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
917 {
918 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
919 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
920 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
921
922 if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
923 {
924 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
925 mode_mv = mode_mv_sb[sign_bias];
926 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
927 }
928
929 #if CONFIG_MULTI_RES_ENCODING
930 if (parent_ref_valid)
931 {
932 if (vp8_mode_order[mode_index] == NEARESTMV &&
933 mode_mv[NEARESTMV].as_int ==0)
934 continue;
935 if (vp8_mode_order[mode_index] == NEARMV &&
936 mode_mv[NEARMV].as_int ==0)
937 continue;
938
939 if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV
940 && best_ref_mv.as_int==0)
941 continue;
942 else if(vp8_mode_order[mode_index] == NEWMV && dissim==0
943 && best_ref_mv.as_int==parent_ref_mv.as_int)
944 continue;
945 }
946 #endif
947 }
948
949 /* Check to see if the testing frequency for this mode is at its max
950 * If so then prevent it from being tested and increase the threshold
951 * for its testing */
952 if (x->mode_test_hit_counts[mode_index] &&
953 (cpi->mode_check_freq[mode_index] > 1))
954 {
955 if (x->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
956 x->mode_test_hit_counts[mode_index]))
957 {
958 /* Increase the threshold for coding this mode to make it less
959 * likely to be chosen */
960 x->rd_thresh_mult[mode_index] += 4;
961
962 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
963 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
964
965 x->rd_threshes[mode_index] =
966 (cpi->rd_baseline_thresh[mode_index] >> 7) *
967 x->rd_thresh_mult[mode_index];
968 continue;
969 }
970 }
971
972 /* We have now reached the point where we are going to test the current
973 * mode so increment the counter for the number of times it has been
974 * tested */
975 x->mode_test_hit_counts[mode_index] ++;
976
977 rate2 = 0;
978 distortion2 = 0;
979
980 this_mode = vp8_mode_order[mode_index];
981
982 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
983 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
984
985 /* Work out the cost assosciated with selecting the reference frame */
986 frame_cost =
987 x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
988 rate2 += frame_cost;
989
990 /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
991 * unless ARNR filtering is enabled in which case we want
992 * an unfiltered alternative */
993 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
994 {
995 if (this_mode != ZEROMV ||
996 x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
997 continue;
998 }
999
1000 switch (this_mode)
1001 {
1002 case B_PRED:
1003 /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
1004 distortion2 = best_rd_sse;
1005 pick_intra4x4mby_modes(x, &rate, &distortion2);
1006
1007 if (distortion2 == INT_MAX)
1008 {
1009 this_rd = INT_MAX;
1010 }
1011 else
1012 {
1013 rate2 += rate;
1014 distortion2 = vpx_variance16x16(
1015 *(b->base_src), b->src_stride,
1016 x->e_mbd.predictor, 16, &sse);
1017 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
1018
1019 if (this_rd < best_intra_rd)
1020 {
1021 best_intra_rd = this_rd;
1022 *returnintra = distortion2;
1023 }
1024 }
1025
1026 break;
1027
1028 case SPLITMV:
1029
1030 /* Split MV modes currently not supported when RD is not enabled. */
1031 break;
1032
1033 case DC_PRED:
1034 case V_PRED:
1035 case H_PRED:
1036 case TM_PRED:
1037 vp8_build_intra_predictors_mby_s(xd,
1038 xd->dst.y_buffer - xd->dst.y_stride,
1039 xd->dst.y_buffer - 1,
1040 xd->dst.y_stride,
1041 xd->predictor,
1042 16);
1043 distortion2 = vpx_variance16x16
1044 (*(b->base_src), b->src_stride,
1045 x->e_mbd.predictor, 16, &sse);
1046 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
1047 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
1048
1049 if (this_rd < best_intra_rd)
1050 {
1051 best_intra_rd = this_rd;
1052 *returnintra = distortion2;
1053 }
1054 break;
1055
1056 case NEWMV:
1057 {
1058 int thissme;
1059 int step_param;
1060 int further_steps;
1061 int n = 0;
1062 int sadpb = x->sadperbit16;
1063 int_mv mvp_full;
1064
1065 int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
1066 int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
1067 int col_max = (best_ref_mv.as_mv.col>>3)
1068 + MAX_FULL_PEL_VAL;
1069 int row_max = (best_ref_mv.as_mv.row>>3)
1070 + MAX_FULL_PEL_VAL;
1071
1072 int tmp_col_min = x->mv_col_min;
1073 int tmp_col_max = x->mv_col_max;
1074 int tmp_row_min = x->mv_row_min;
1075 int tmp_row_max = x->mv_row_max;
1076
1077 int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
1078
1079 /* Further step/diamond searches as necessary */
1080 step_param = cpi->sf.first_step + speed_adjust;
1081
1082 #if CONFIG_MULTI_RES_ENCODING
1083 /* If lower-res frame is not available for mv reuse (because of
1084 frame dropping or different temporal layer pattern), then higher
1085 resol encoder does motion search without any previous knowledge.
1086 Also, since last frame motion info is not stored, then we can not
1087 use improved_mv_pred. */
1088 if (cpi->oxcf.mr_encoder_id)
1089 sf_improved_mv_pred = 0;
1090
1091 // Only use parent MV as predictor if this candidate reference frame
1092 // (|this_ref_frame|) is equal to |parent_ref_frame|.
1093 if (parent_ref_valid && (parent_ref_frame == this_ref_frame))
1094 {
1095 /* Use parent MV as predictor. Adjust search range
1096 * accordingly.
1097 */
1098 mvp.as_int = parent_ref_mv.as_int;
1099 mvp_full.as_mv.col = parent_ref_mv.as_mv.col>>3;
1100 mvp_full.as_mv.row = parent_ref_mv.as_mv.row>>3;
1101
1102 if(dissim <=32) step_param += 3;
1103 else if(dissim <=128) step_param += 2;
1104 else step_param += 1;
1105 }else
1106 #endif
1107 {
1108 if(sf_improved_mv_pred)
1109 {
1110 if(!saddone)
1111 {
1112 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
1113 saddone = 1;
1114 }
1115
1116 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context,
1117 &mvp,x->e_mbd.mode_info_context->mbmi.ref_frame,
1118 cpi->common.ref_frame_sign_bias, &sr,
1119 &near_sadidx[0]);
1120
1121 sr += speed_adjust;
1122 /* adjust search range according to sr from mv prediction */
1123 if(sr > step_param)
1124 step_param = sr;
1125
1126 mvp_full.as_mv.col = mvp.as_mv.col>>3;
1127 mvp_full.as_mv.row = mvp.as_mv.row>>3;
1128 }else
1129 {
1130 mvp.as_int = best_ref_mv.as_int;
1131 mvp_full.as_mv.col = best_ref_mv.as_mv.col>>3;
1132 mvp_full.as_mv.row = best_ref_mv.as_mv.row>>3;
1133 }
1134 }
1135
1136 #if CONFIG_MULTI_RES_ENCODING
1137 if (parent_ref_valid && (parent_ref_frame == this_ref_frame) &&
1138 dissim <= 2 &&
1139 MAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
1140 abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <= 4)
1141 {
1142 d->bmi.mv.as_int = mvp_full.as_int;
1143 mode_mv[NEWMV].as_int = mvp_full.as_int;
1144
1145 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
1146 x->errorperbit,
1147 &cpi->fn_ptr[BLOCK_16X16],
1148 cpi->mb.mvcost,
1149 &distortion2,&sse);
1150 }else
1151 #endif
1152 {
1153 /* Get intersection of UMV window and valid MV window to
1154 * reduce # of checks in diamond search. */
1155 if (x->mv_col_min < col_min )
1156 x->mv_col_min = col_min;
1157 if (x->mv_col_max > col_max )
1158 x->mv_col_max = col_max;
1159 if (x->mv_row_min < row_min )
1160 x->mv_row_min = row_min;
1161 if (x->mv_row_max > row_max )
1162 x->mv_row_max = row_max;
1163
1164 further_steps = (cpi->Speed >= 8)?
1165 0: (cpi->sf.max_step_search_steps - 1 - step_param);
1166
1167 if (cpi->sf.search_method == HEX)
1168 {
1169 #if CONFIG_MULTI_RES_ENCODING
1170 /* TODO: In higher-res pick_inter_mode, step_param is used to
1171 * modify hex search range. Here, set step_param to 0 not to
1172 * change the behavior in lowest-resolution encoder.
1173 * Will improve it later.
1174 */
1175 /* Set step_param to 0 to ensure large-range motion search
1176 * when mv reuse if not valid (i.e. |parent_ref_valid| = 0),
1177 * or if this candidate reference frame (|this_ref_frame|) is
1178 * not equal to |parent_ref_frame|.
1179 */
1180 if (!parent_ref_valid || (parent_ref_frame != this_ref_frame))
1181 step_param = 0;
1182 #endif
1183 bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv,
1184 step_param, sadpb,
1185 &cpi->fn_ptr[BLOCK_16X16],
1186 x->mvsadcost, x->mvcost, &best_ref_mv);
1187 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1188 }
1189 else
1190 {
1191 bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full,
1192 &d->bmi.mv, step_param, sadpb, &num00,
1193 &cpi->fn_ptr[BLOCK_16X16],
1194 x->mvcost, &best_ref_mv);
1195 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1196
1197 /* Further step/diamond searches as necessary */
1198 n = num00;
1199 num00 = 0;
1200
1201 while (n < further_steps)
1202 {
1203 n++;
1204
1205 if (num00)
1206 num00--;
1207 else
1208 {
1209 thissme =
1210 cpi->diamond_search_sad(x, b, d, &mvp_full,
1211 &d->bmi.mv,
1212 step_param + n,
1213 sadpb, &num00,
1214 &cpi->fn_ptr[BLOCK_16X16],
1215 x->mvcost, &best_ref_mv);
1216 if (thissme < bestsme)
1217 {
1218 bestsme = thissme;
1219 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1220 }
1221 else
1222 {
1223 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
1224 }
1225 }
1226 }
1227 }
1228
1229 x->mv_col_min = tmp_col_min;
1230 x->mv_col_max = tmp_col_max;
1231 x->mv_row_min = tmp_row_min;
1232 x->mv_row_max = tmp_row_max;
1233
1234 if (bestsme < INT_MAX)
1235 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
1236 &best_ref_mv, x->errorperbit,
1237 &cpi->fn_ptr[BLOCK_16X16],
1238 cpi->mb.mvcost,
1239 &distortion2,&sse);
1240 }
1241
1242 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1243
1244 /* mv cost; */
1245 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
1246 cpi->mb.mvcost, 128);
1247 }
1248
1249 case NEARESTMV:
1250 case NEARMV:
1251
1252 if (mode_mv[this_mode].as_int == 0)
1253 continue;
1254
1255 case ZEROMV:
1256
1257 /* Trap vectors that reach beyond the UMV borders
1258 * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
1259 * through to this point because of the lack of break statements
1260 * in the previous two cases.
1261 */
1262 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
1263 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
1264 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
1265 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1266 continue;
1267
1268 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1269 x->e_mbd.mode_info_context->mbmi.mv.as_int =
1270 mode_mv[this_mode].as_int;
1271 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1272 rd_adjustment);
1273
1274 break;
1275 default:
1276 break;
1277 }
1278
1279 #if CONFIG_TEMPORAL_DENOISING
1280 if (cpi->oxcf.noise_sensitivity)
1281 {
1282 /* Store for later use by denoiser. */
1283 // Dont' denoise with GOLDEN OR ALTREF is they are old reference
1284 // frames (greater than MAX_GF_ARF_DENOISE_RANGE frames in past).
1285 int skip_old_reference = ((this_ref_frame != LAST_FRAME) &&
1286 (cpi->common.current_video_frame -
1287 cpi->current_ref_frames[this_ref_frame] >
1288 MAX_GF_ARF_DENOISE_RANGE)) ? 1 : 0;
1289 if (this_mode == ZEROMV && sse < zero_mv_sse &&
1290 !skip_old_reference)
1291 {
1292 zero_mv_sse = sse;
1293 x->best_zeromv_reference_frame =
1294 x->e_mbd.mode_info_context->mbmi.ref_frame;
1295 }
1296
1297 // Store the best NEWMV in x for later use in the denoiser.
1298 if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
1299 sse < best_sse && !skip_old_reference)
1300 {
1301 best_sse = sse;
1302 x->best_sse_inter_mode = NEWMV;
1303 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
1304 x->need_to_clamp_best_mvs =
1305 x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
1306 x->best_reference_frame =
1307 x->e_mbd.mode_info_context->mbmi.ref_frame;
1308 }
1309 }
1310 #endif
1311
1312 if (this_rd < best_rd || x->skip)
1313 {
1314 /* Note index of best mode */
1315 best_mode_index = mode_index;
1316
1317 *returnrate = rate2;
1318 *returndistortion = distortion2;
1319 best_rd_sse = sse;
1320 best_rd = this_rd;
1321 memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1322 sizeof(MB_MODE_INFO));
1323
1324 /* Testing this mode gave rise to an improvement in best error
1325 * score. Lower threshold a bit for next time
1326 */
1327 x->rd_thresh_mult[mode_index] =
1328 (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
1329 x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
1330 x->rd_threshes[mode_index] =
1331 (cpi->rd_baseline_thresh[mode_index] >> 7) *
1332 x->rd_thresh_mult[mode_index];
1333 }
1334
1335 /* If the mode did not help improve the best error case then raise the
1336 * threshold for testing that mode next time around.
1337 */
1338 else
1339 {
1340 x->rd_thresh_mult[mode_index] += 4;
1341
1342 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1343 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1344
1345 x->rd_threshes[mode_index] =
1346 (cpi->rd_baseline_thresh[mode_index] >> 7) *
1347 x->rd_thresh_mult[mode_index];
1348 }
1349
1350 if (x->skip)
1351 break;
1352 }
1353
1354 /* Reduce the activation RD thresholds for the best choice mode */
1355 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
1356 {
1357 int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 3);
1358
1359 x->rd_thresh_mult[best_mode_index] =
1360 (x->rd_thresh_mult[best_mode_index]
1361 >= (MIN_THRESHMULT + best_adjustment)) ?
1362 x->rd_thresh_mult[best_mode_index] - best_adjustment :
1363 MIN_THRESHMULT;
1364 x->rd_threshes[best_mode_index] =
1365 (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
1366 x->rd_thresh_mult[best_mode_index];
1367 }
1368
1369
1370 {
1371 int this_rdbin = (*returndistortion >> 7);
1372
1373 if (this_rdbin >= 1024)
1374 {
1375 this_rdbin = 1023;
1376 }
1377
1378 x->error_bins[this_rdbin] ++;
1379 }
1380
1381 #if CONFIG_TEMPORAL_DENOISING
1382 if (cpi->oxcf.noise_sensitivity)
1383 {
1384 int block_index = mb_row * cpi->common.mb_cols + mb_col;
1385 int reevaluate = 0;
1386 int is_noisy = 0;
1387 if (x->best_sse_inter_mode == DC_PRED)
1388 {
1389 /* No best MV found. */
1390 x->best_sse_inter_mode = best_mbmode.mode;
1391 x->best_sse_mv = best_mbmode.mv;
1392 x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
1393 x->best_reference_frame = best_mbmode.ref_frame;
1394 best_sse = best_rd_sse;
1395 }
1396 // For non-skin blocks that have selected ZEROMV for this current frame,
1397 // and have been selecting ZEROMV_LAST (on the base layer frame) at
1398 // least |x~20| consecutive past frames in a row, label the block for
1399 // possible increase in denoising strength. We also condition this
1400 // labeling on there being significant denoising in the scene
1401 if (cpi->oxcf.noise_sensitivity == 4) {
1402 if (cpi->denoiser.nmse_source_diff >
1403 70 * cpi->denoiser.threshold_aggressive_mode / 100)
1404 is_noisy = 1;
1405 } else {
1406 if (cpi->mse_source_denoised > 1000)
1407 is_noisy = 1;
1408 }
1409 x->increase_denoising = 0;
1410 if (!x->is_skin &&
1411 x->best_sse_inter_mode == ZEROMV &&
1412 (x->best_reference_frame == LAST_FRAME ||
1413 x->best_reference_frame == cpi->closest_reference_frame) &&
1414 cpi->consec_zero_last[block_index] >= 20 &&
1415 is_noisy) {
1416 x->increase_denoising = 1;
1417 }
1418 x->denoise_zeromv = 0;
1419 vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
1420 recon_yoffset, recon_uvoffset,
1421 &cpi->common.lf_info, mb_row, mb_col,
1422 block_index);
1423
1424 // Reevaluate ZEROMV after denoising: for large noise content
1425 // (i.e., cpi->mse_source_denoised is above threshold), do this for all
1426 // blocks that did not pick ZEROMV as best mode but are using ZEROMV
1427 // for denoising. Otherwise, always re-evaluate for blocks that picked
1428 // INTRA mode as best mode.
1429 // Avoid blocks that have been biased against ZERO_LAST
1430 // (i.e., dot artifact candidate blocks).
1431 reevaluate = (best_mbmode.ref_frame == INTRA_FRAME) ||
1432 (best_mbmode.mode != ZEROMV &&
1433 x->denoise_zeromv &&
1434 cpi->mse_source_denoised > 2000);
1435 if (!dot_artifact_candidate &&
1436 reevaluate &&
1437 x->best_zeromv_reference_frame != INTRA_FRAME)
1438 {
1439 int this_rd = 0;
1440 int this_ref_frame = x->best_zeromv_reference_frame;
1441 rd_adjustment = 100;
1442 rate2 = x->ref_frame_cost[this_ref_frame] +
1443 vp8_cost_mv_ref(ZEROMV, mdcounts);
1444 distortion2 = 0;
1445
1446 /* set up the proper prediction buffers for the frame */
1447 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
1448 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
1449 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
1450 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
1451
1452 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1453 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1454 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1455 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1456 rd_adjustment);
1457
1458 if (this_rd < best_rd)
1459 {
1460 memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1461 sizeof(MB_MODE_INFO));
1462 }
1463 }
1464
1465 }
1466 #endif
1467
1468 if (cpi->is_src_frame_alt_ref &&
1469 (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
1470 {
1471 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1472 x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
1473 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1474 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1475 x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
1476 (cpi->common.mb_no_coeff_skip);
1477 x->e_mbd.mode_info_context->mbmi.partitioning = 0;
1478
1479 return;
1480 }
1481
1482 /* set to the best mb mode, this copy can be skip if x->skip since it
1483 * already has the right content */
1484 if (!x->skip)
1485 memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode,
1486 sizeof(MB_MODE_INFO));
1487
1488 if (best_mbmode.mode <= B_PRED)
1489 {
1490 /* set mode_info_context->mbmi.uv_mode */
1491 pick_intra_mbuv_mode(x);
1492 }
1493
1494 if (sign_bias
1495 != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
1496 best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
1497
1498 update_mvcount(x, &best_ref_mv);
1499 }
1500
vp8_pick_intra_mode(MACROBLOCK * x,int * rate_)1501 void vp8_pick_intra_mode(MACROBLOCK *x, int *rate_)
1502 {
1503 int error4x4, error16x16 = INT_MAX;
1504 int rate, best_rate = 0, distortion, best_sse;
1505 MB_PREDICTION_MODE mode, best_mode = DC_PRED;
1506 int this_rd;
1507 unsigned int sse;
1508 BLOCK *b = &x->block[0];
1509 MACROBLOCKD *xd = &x->e_mbd;
1510
1511 xd->mode_info_context->mbmi.ref_frame = INTRA_FRAME;
1512
1513 pick_intra_mbuv_mode(x);
1514
1515 for (mode = DC_PRED; mode <= TM_PRED; mode ++)
1516 {
1517 xd->mode_info_context->mbmi.mode = mode;
1518 vp8_build_intra_predictors_mby_s(xd,
1519 xd->dst.y_buffer - xd->dst.y_stride,
1520 xd->dst.y_buffer - 1,
1521 xd->dst.y_stride,
1522 xd->predictor,
1523 16);
1524 distortion = vpx_variance16x16
1525 (*(b->base_src), b->src_stride, xd->predictor, 16, &sse);
1526 rate = x->mbmode_cost[xd->frame_type][mode];
1527 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1528
1529 if (error16x16 > this_rd)
1530 {
1531 error16x16 = this_rd;
1532 best_mode = mode;
1533 best_sse = sse;
1534 best_rate = rate;
1535 }
1536 }
1537 xd->mode_info_context->mbmi.mode = best_mode;
1538
1539 error4x4 = pick_intra4x4mby_modes(x, &rate,
1540 &best_sse);
1541 if (error4x4 < error16x16)
1542 {
1543 xd->mode_info_context->mbmi.mode = B_PRED;
1544 best_rate = rate;
1545 }
1546
1547 *rate_ = best_rate;
1548 }
1549