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