2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 #include "vpx_config.h"
13 #include "./vpx_dsp_rtcd.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"
22 #include "vp8/common/reconinter.h"
23 #include "vp8/common/reconintra.h"
24 #include "vp8/common/reconintra4x4.h"
25 #include "vpx_dsp/variance.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"
35 extern unsigned int cnt_pm;
40 extern const int vp8_ref_frame_order[MAX_MODES];
41 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
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.
48 // Fixed-point skin color model parameters.
49 static const int skin_mean[5][2] = { { 7463, 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
58 // Evaluates the Mahalanobis distance measure for the input CbCr values.
59 static int evaluate_skin_color_difference(int cb, int cr, int idx) {
60 const int cb_q6 = cb << 6;
61 const int cr_q6 = cr << 6;
62 const int cb_diff_q12 =
63 (cb_q6 - skin_mean[idx][0]) * (cb_q6 - skin_mean[idx][0]);
64 const int cbcr_diff_q12 =
65 (cb_q6 - skin_mean[idx][0]) * (cr_q6 - skin_mean[idx][1]);
66 const int cr_diff_q12 =
67 (cr_q6 - skin_mean[idx][1]) * (cr_q6 - skin_mean[idx][1]);
68 const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
69 const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
70 const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
72 skin_inv_cov[0] * cb_diff_q2 + skin_inv_cov[1] * cbcr_diff_q2 +
73 skin_inv_cov[2] * cbcr_diff_q2 + skin_inv_cov[3] * cr_diff_q2;
77 // Checks if the input yCbCr values corresponds to skin color.
78 static int is_skin_color(int y, int cb, int cr, int consec_zeromv) {
79 if (y < 40 || y > 220) {
82 if (MODEL_MODE == 0) {
83 return (evaluate_skin_color_difference(cb, cr, 0) < skin_threshold[0]);
86 // No skin if block has been zero motion for long consecutive time.
87 if (consec_zeromv > 60) return 0;
89 if (cb == 128 && cr == 128) return 0;
90 // Exit on very strong cb.
91 if (cb > 150 && cr < 110) return 0;
93 int skin_color_diff = evaluate_skin_color_difference(cb, cr, i);
94 if (skin_color_diff < skin_threshold[i + 1]) {
95 if (y < 60 && skin_color_diff > 3 * (skin_threshold[i + 1] >> 2))
97 else if (consec_zeromv > 25 &&
98 skin_color_diff > (skin_threshold[i + 1] >> 1))
103 // Exit if difference is much large than the threshold.
104 if (skin_color_diff > (skin_threshold[i + 1] << 3)) {
113 static int macroblock_corner_grad(unsigned char *signal, int stride,
114 int offsetx, int offsety, int sgnx,
116 int y1 = signal[offsetx * stride + offsety];
117 int y2 = signal[offsetx * stride + offsety + sgny];
118 int y3 = signal[(offsetx + sgnx) * stride + offsety];
119 int y4 = signal[(offsetx + sgnx) * stride + offsety + sgny];
120 return VPXMAX(VPXMAX(abs(y1 - y2), abs(y1 - y3)), abs(y1 - y4));
123 static int check_dot_artifact_candidate(VP8_COMP *cpi, MACROBLOCK *x,
124 unsigned char *target_last, int stride,
125 unsigned char *last_ref, int mb_row,
126 int mb_col, int channel) {
129 unsigned int max_num = (cpi->common.MBs) / 10;
132 int index = mb_row * cpi->common.mb_cols + mb_col;
133 // Threshold for #consecutive (base layer) frames using zero_last mode.
139 if (cpi->oxcf.number_of_layers > 1) {
142 x->zero_last_dot_suppress = 0;
143 // Blocks on base layer frames that have been using ZEROMV_LAST repeatedly
144 // (i.e, at least |x| consecutive frames are candidates for increasing the
145 // rd adjustment for zero_last mode.
146 // Only allow this for at most |max_num| blocks per frame.
147 // Don't allow this for screen content input.
148 if (cpi->current_layer == 0 &&
149 cpi->consec_zero_last_mvbias[index] > num_frames &&
150 x->mbs_zero_last_dot_suppress < max_num &&
151 !cpi->oxcf.screen_content_mode) {
152 // If this block is checked here, label it so we don't check it again until
153 // ~|x| framaes later.
154 x->zero_last_dot_suppress = 1;
155 // Dot artifact is noticeable as strong gradient at corners of macroblock,
156 // for flat areas. As a simple detector for now, we look for a high
157 // corner gradient on last ref, and a smaller gradient on source.
158 // Check 4 corners, return if any satisfy condition.
160 grad_last = macroblock_corner_grad(last_ref, stride, 0, 0, 1, 1);
161 grad_source = macroblock_corner_grad(target_last, stride, 0, 0, 1, 1);
162 if (grad_last >= threshold1 && grad_source <= threshold2) {
163 x->mbs_zero_last_dot_suppress++;
167 grad_last = macroblock_corner_grad(last_ref, stride, 0, shift, 1, -1);
168 grad_source = macroblock_corner_grad(target_last, stride, 0, shift, 1, -1);
169 if (grad_last >= threshold1 && grad_source <= threshold2) {
170 x->mbs_zero_last_dot_suppress++;
174 grad_last = macroblock_corner_grad(last_ref, stride, shift, 0, -1, 1);
175 grad_source = macroblock_corner_grad(target_last, stride, shift, 0, -1, 1);
176 if (grad_last >= threshold1 && grad_source <= threshold2) {
177 x->mbs_zero_last_dot_suppress++;
181 grad_last = macroblock_corner_grad(last_ref, stride, shift, shift, -1, -1);
183 macroblock_corner_grad(target_last, stride, shift, shift, -1, -1);
184 if (grad_last >= threshold1 && grad_source <= threshold2) {
185 x->mbs_zero_last_dot_suppress++;
193 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
194 int_mv *bestmv, int_mv *ref_mv,
196 const vp8_variance_fn_ptr_t *vfp,
197 int *mvcost[2], int *distortion,
208 bestmv->as_mv.row <<= 3;
209 bestmv->as_mv.col <<= 3;
213 int vp8_get_inter_mbpred_error(MACROBLOCK *mb, const vp8_variance_fn_ptr_t *vfp,
214 unsigned int *sse, int_mv this_mv) {
215 BLOCK *b = &mb->block[0];
216 BLOCKD *d = &mb->e_mbd.block[0];
217 unsigned char *what = (*(b->base_src) + b->src);
218 int what_stride = b->src_stride;
219 int pre_stride = mb->e_mbd.pre.y_stride;
220 unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset;
221 int in_what_stride = pre_stride;
222 int xoffset = this_mv.as_mv.col & 7;
223 int yoffset = this_mv.as_mv.row & 7;
225 in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
227 if (xoffset | yoffset) {
228 return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what,
231 return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
235 static int get_prediction_error(BLOCK *be, BLOCKD *b) {
238 sptr = (*(be->base_src) + be->src);
241 return vpx_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
244 static int pick_intra4x4block(MACROBLOCK *x, int ib,
245 B_PREDICTION_MODE *best_mode,
246 const int *mode_costs,
248 int *bestrate, int *bestdistortion) {
249 BLOCKD *b = &x->e_mbd.block[ib];
250 BLOCK *be = &x->block[ib];
251 int dst_stride = x->e_mbd.dst.y_stride;
252 unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
253 B_PREDICTION_MODE mode;
254 int best_rd = INT_MAX;
258 unsigned char *Above = dst - dst_stride;
259 unsigned char *yleft = dst - 1;
260 unsigned char top_left = Above[-1];
262 for (mode = B_DC_PRED; mode <= B_HE_PRED; ++mode) {
265 rate = mode_costs[mode];
267 vp8_intra4x4_predict(Above, yleft, dst_stride, mode, b->predictor, 16,
269 distortion = get_prediction_error(be, b);
270 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
272 if (this_rd < best_rd) {
274 *bestdistortion = distortion;
280 b->bmi.as_mode = *best_mode;
281 vp8_encode_intra4x4block(x, ib);
285 static int pick_intra4x4mby_modes(MACROBLOCK *mb, int *Rate, int *best_dist) {
286 MACROBLOCKD *const xd = &mb->e_mbd;
288 int cost = mb->mbmode_cost[xd->frame_type][B_PRED];
291 const int *bmode_costs;
293 intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
295 bmode_costs = mb->inter_bmode_costs;
297 for (i = 0; i < 16; ++i) {
298 MODE_INFO *const mic = xd->mode_info_context;
299 const int mis = xd->mode_info_stride;
301 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
302 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
304 if (mb->e_mbd.frame_type == KEY_FRAME) {
305 const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
306 const B_PREDICTION_MODE L = left_block_mode(mic, i);
308 bmode_costs = mb->bmode_costs[A][L];
311 pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
315 mic->bmi[i].as_mode = best_mode;
317 /* Break out case where we have already exceeded best so far value
320 if (distortion > *best_dist) break;
326 *best_dist = distortion;
327 error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
329 *best_dist = INT_MAX;
336 static void pick_intra_mbuv_mode(MACROBLOCK *mb) {
337 MACROBLOCKD *x = &mb->e_mbd;
338 unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
339 unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
340 unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
341 unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
342 int uvsrc_stride = mb->block[16].src_stride;
343 unsigned char uleft_col[8];
344 unsigned char vleft_col[8];
345 unsigned char utop_left = uabove_row[-1];
346 unsigned char vtop_left = vabove_row[-1];
354 int pred_error[4] = { 0, 0, 0, 0 }, best_error = INT_MAX;
355 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
357 for (i = 0; i < 8; ++i) {
358 uleft_col[i] = x->dst.u_buffer[i * x->dst.uv_stride - 1];
359 vleft_col[i] = x->dst.v_buffer[i * x->dst.uv_stride - 1];
362 if (!x->up_available && !x->left_available) {
368 if (x->up_available) {
369 for (i = 0; i < 8; ++i) {
370 Uaverage += uabove_row[i];
371 Vaverage += vabove_row[i];
377 if (x->left_available) {
378 for (i = 0; i < 8; ++i) {
379 Uaverage += uleft_col[i];
380 Vaverage += vleft_col[i];
386 expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
387 expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
390 for (i = 0; i < 8; ++i) {
391 for (j = 0; j < 8; ++j) {
392 int predu = uleft_col[i] + uabove_row[j] - utop_left;
393 int predv = vleft_col[i] + vabove_row[j] - vtop_left;
399 if (predu < 0) predu = 0;
401 if (predu > 255) predu = 255;
403 if (predv < 0) predv = 0;
405 if (predv > 255) predv = 255;
407 diff = u_p - expected_udc;
408 pred_error[DC_PRED] += diff * diff;
409 diff = v_p - expected_vdc;
410 pred_error[DC_PRED] += diff * diff;
412 diff = u_p - uabove_row[j];
413 pred_error[V_PRED] += diff * diff;
414 diff = v_p - vabove_row[j];
415 pred_error[V_PRED] += diff * diff;
417 diff = u_p - uleft_col[i];
418 pred_error[H_PRED] += diff * diff;
419 diff = v_p - vleft_col[i];
420 pred_error[H_PRED] += diff * diff;
423 pred_error[TM_PRED] += diff * diff;
425 pred_error[TM_PRED] += diff * diff;
428 usrc_ptr += uvsrc_stride;
429 vsrc_ptr += uvsrc_stride;
432 usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
433 vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
437 for (i = DC_PRED; i <= TM_PRED; ++i) {
438 if (best_error > pred_error[i]) {
439 best_error = pred_error[i];
440 best_mode = (MB_PREDICTION_MODE)i;
444 mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
447 static void update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv) {
448 MACROBLOCKD *xd = &x->e_mbd;
449 /* Split MV modes currently not supported when RD is nopt enabled,
450 * therefore, only need to modify MVcount in NEWMV mode. */
451 if (xd->mode_info_context->mbmi.mode == NEWMV) {
452 x->MVcount[0][mv_max + ((xd->mode_info_context->mbmi.mv.as_mv.row -
453 best_ref_mv->as_mv.row) >>
455 x->MVcount[1][mv_max + ((xd->mode_info_context->mbmi.mv.as_mv.col -
456 best_ref_mv->as_mv.col) >>
461 #if CONFIG_MULTI_RES_ENCODING
462 static void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd,
463 int *dissim, int *parent_ref_frame,
464 MB_PREDICTION_MODE *parent_mode,
465 int_mv *parent_ref_mv, int mb_row,
467 LOWER_RES_MB_INFO *store_mode_info =
468 ((LOWER_RES_FRAME_INFO *)cpi->oxcf.mr_low_res_mode_info)->mb_info;
469 unsigned int parent_mb_index;
471 /* Consider different down_sampling_factor. */
473 /* TODO: Removed the loop that supports special down_sampling_factor
474 * such as 2, 4, 8. Will revisit it if needed.
475 * Should also try using a look-up table to see if it helps
477 int parent_mb_row, parent_mb_col;
479 parent_mb_row = mb_row * cpi->oxcf.mr_down_sampling_factor.den /
480 cpi->oxcf.mr_down_sampling_factor.num;
481 parent_mb_col = mb_col * cpi->oxcf.mr_down_sampling_factor.den /
482 cpi->oxcf.mr_down_sampling_factor.num;
483 parent_mb_index = parent_mb_row * cpi->mr_low_res_mb_cols + parent_mb_col;
486 /* Read lower-resolution mode & motion result from memory.*/
487 *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
488 *parent_mode = store_mode_info[parent_mb_index].mode;
489 *dissim = store_mode_info[parent_mb_index].dissim;
491 /* For highest-resolution encoder, adjust dissim value. Lower its quality
492 * for good performance. */
493 if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
496 if (*parent_ref_frame != INTRA_FRAME) {
497 /* Consider different down_sampling_factor.
498 * The result can be rounded to be more precise, but it takes more time.
500 (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row *
501 cpi->oxcf.mr_down_sampling_factor.num /
502 cpi->oxcf.mr_down_sampling_factor.den;
503 (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col *
504 cpi->oxcf.mr_down_sampling_factor.num /
505 cpi->oxcf.mr_down_sampling_factor.den;
507 vp8_clamp_mv2(parent_ref_mv, xd);
512 static void check_for_encode_breakout(unsigned int sse, MACROBLOCK *x) {
513 MACROBLOCKD *xd = &x->e_mbd;
515 unsigned int threshold =
516 (xd->block[0].dequant[1] * xd->block[0].dequant[1] >> 4);
518 if (threshold < x->encode_breakout) threshold = x->encode_breakout;
520 if (sse < threshold) {
521 /* Check u and v to make sure skip is ok */
522 unsigned int sse2 = 0;
526 if (sse2 * 2 < x->encode_breakout)
533 static int evaluate_inter_mode(unsigned int *sse, int rate2, int *distortion2,
534 VP8_COMP *cpi, MACROBLOCK *x, int rd_adj) {
535 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
536 int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
538 int denoise_aggressive = 0;
539 /* Exit early and don't compute the distortion if this macroblock
540 * is marked inactive. */
541 if (cpi->active_map_enabled && x->active_ptr[0] == 0) {
548 if ((this_mode != NEWMV) || !(cpi->sf.half_pixel_search) ||
549 cpi->common.full_pixel == 1)
551 vp8_get_inter_mbpred_error(x, &cpi->fn_ptr[BLOCK_16X16], sse, mv);
553 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);
555 #if CONFIG_TEMPORAL_DENOISING
556 if (cpi->oxcf.noise_sensitivity > 0) {
558 (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) ? 1 : 0;
562 // Adjust rd for ZEROMV and LAST, if LAST is the closest reference frame.
563 // TODO: We should also add condition on distance of closest to current.
564 if (!cpi->oxcf.screen_content_mode && this_mode == ZEROMV &&
565 x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME &&
566 (denoise_aggressive || (cpi->closest_reference_frame == LAST_FRAME))) {
567 // No adjustment if block is considered to be skin area.
568 if (x->is_skin) rd_adj = 100;
570 this_rd = ((int64_t)this_rd) * rd_adj / 100;
573 check_for_encode_breakout(*sse, x);
577 static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
578 int *rd_adjustment) {
579 MODE_INFO *mic = x->e_mbd.mode_info_context;
580 int_mv mv_l, mv_a, mv_al;
581 int local_motion_check = 0;
583 if (cpi->lf_zeromv_pct > 40) {
588 if (mic->mbmi.ref_frame != INTRA_FRAME)
589 if (abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
590 local_motion_check++;
593 mic -= x->e_mbd.mode_info_stride;
594 mv_al = mic->mbmi.mv;
596 if (mic->mbmi.ref_frame != INTRA_FRAME)
597 if (abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
598 local_motion_check++;
604 if (mic->mbmi.ref_frame != INTRA_FRAME)
605 if (abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
606 local_motion_check++;
608 if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge) &&
609 local_motion_check > 0) ||
610 local_motion_check > 2)
612 else if (local_motion_check > 0)
617 void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
618 int recon_uvoffset, int *returnrate,
619 int *returndistortion, int *returnintra, int mb_row,
621 BLOCK *b = &x->block[0];
622 BLOCKD *d = &x->e_mbd.block[0];
623 MACROBLOCKD *xd = &x->e_mbd;
624 MB_MODE_INFO best_mbmode;
626 int_mv best_ref_mv_sb[2];
627 int_mv mode_mv_sb[2][MB_MODE_COUNT];
630 MB_PREDICTION_MODE this_mode;
633 int best_rd = INT_MAX;
634 int rd_adjustment = 100;
635 int best_intra_rd = INT_MAX;
640 int bestsme = INT_MAX;
641 int best_mode_index = 0;
642 unsigned int sse = UINT_MAX, best_rd_sse = UINT_MAX;
643 #if CONFIG_TEMPORAL_DENOISING
644 unsigned int zero_mv_sse = UINT_MAX, best_sse = UINT_MAX;
647 int sf_improved_mv_pred = cpi->sf.improved_mv_pred;
649 #if CONFIG_MULTI_RES_ENCODING
650 int dissim = INT_MAX;
651 int parent_ref_frame = 0;
652 int_mv parent_ref_mv;
653 MB_PREDICTION_MODE parent_mode = 0;
654 int parent_ref_valid = 0;
659 int near_sadidx[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
661 /* search range got from mv_pred(). It uses step_param levels. (0-7) */
664 unsigned char *plane[4][3];
665 int ref_frame_map[4];
667 int dot_artifact_candidate = 0;
668 get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
670 // If the current frame is using LAST as a reference, check for
671 // biasing the mode selection for dot artifacts.
672 if (cpi->ref_frame_flags & VP8_LAST_FRAME) {
673 unsigned char *target_y = x->src.y_buffer;
674 unsigned char *target_u = x->block[16].src + *x->block[16].base_src;
675 unsigned char *target_v = x->block[20].src + *x->block[20].base_src;
676 int stride = x->src.y_stride;
677 int stride_uv = x->block[16].src_stride;
678 #if CONFIG_TEMPORAL_DENOISING
679 if (cpi->oxcf.noise_sensitivity) {
680 const int uv_denoise = (cpi->oxcf.noise_sensitivity >= 2) ? 1 : 0;
682 cpi->denoiser.yv12_running_avg[LAST_FRAME].y_buffer + recon_yoffset;
683 stride = cpi->denoiser.yv12_running_avg[LAST_FRAME].y_stride;
685 target_u = cpi->denoiser.yv12_running_avg[LAST_FRAME].u_buffer +
687 target_v = cpi->denoiser.yv12_running_avg[LAST_FRAME].v_buffer +
689 stride_uv = cpi->denoiser.yv12_running_avg[LAST_FRAME].uv_stride;
693 dot_artifact_candidate = check_dot_artifact_candidate(
694 cpi, x, target_y, stride, plane[LAST_FRAME][0], mb_row, mb_col, 0);
695 // If not found in Y channel, check UV channel.
696 if (!dot_artifact_candidate) {
697 dot_artifact_candidate = check_dot_artifact_candidate(
698 cpi, x, target_u, stride_uv, plane[LAST_FRAME][1], mb_row, mb_col, 1);
699 if (!dot_artifact_candidate) {
700 dot_artifact_candidate = check_dot_artifact_candidate(
701 cpi, x, target_v, stride_uv, plane[LAST_FRAME][2], mb_row, mb_col,
707 #if CONFIG_MULTI_RES_ENCODING
708 // |parent_ref_valid| will be set here if potentially we can do mv resue for
709 // this higher resol (|cpi->oxcf.mr_encoder_id| > 0) frame.
710 // |parent_ref_valid| may be reset depending on |parent_ref_frame| for
711 // the current macroblock below.
712 parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
713 if (parent_ref_valid) {
716 get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame, &parent_mode,
717 &parent_ref_mv, mb_row, mb_col);
719 /* TODO(jkoleszar): The references available (ref_frame_flags) to the
720 * lower res encoder should match those available to this encoder, but
721 * there seems to be a situation where this mismatch can happen in the
722 * case of frame dropping and temporal layers. For example,
723 * GOLD being disallowed in ref_frame_flags, but being returned as
726 * In this event, take the conservative approach of disabling the
727 * lower res info for this MB.
731 // Note availability for mv reuse is only based on last and golden.
732 if (parent_ref_frame == LAST_FRAME)
733 parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
734 else if (parent_ref_frame == GOLDEN_FRAME)
735 parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);
737 // assert(!parent_ref_frame || parent_ref_flag);
739 // If |parent_ref_frame| did not match either last or golden then
740 // shut off mv reuse.
741 if (parent_ref_frame && !parent_ref_flag) parent_ref_valid = 0;
743 // Don't do mv reuse since we want to allow for another mode besides
744 // ZEROMV_LAST to remove dot artifact.
745 if (dot_artifact_candidate) parent_ref_valid = 0;
749 // Check if current macroblock is in skin area.
751 const int y = (x->src.y_buffer[7 * x->src.y_stride + 7] +
752 x->src.y_buffer[7 * x->src.y_stride + 8] +
753 x->src.y_buffer[8 * x->src.y_stride + 7] +
754 x->src.y_buffer[8 * x->src.y_stride + 8]) >>
756 const int cb = (x->src.u_buffer[3 * x->src.uv_stride + 3] +
757 x->src.u_buffer[3 * x->src.uv_stride + 4] +
758 x->src.u_buffer[4 * x->src.uv_stride + 3] +
759 x->src.u_buffer[4 * x->src.uv_stride + 4]) >>
761 const int cr = (x->src.v_buffer[3 * x->src.uv_stride + 3] +
762 x->src.v_buffer[3 * x->src.uv_stride + 4] +
763 x->src.v_buffer[4 * x->src.uv_stride + 3] +
764 x->src.v_buffer[4 * x->src.uv_stride + 4]) >>
767 if (!cpi->oxcf.screen_content_mode) {
768 int block_index = mb_row * cpi->common.mb_cols + mb_col;
769 x->is_skin = is_skin_color(y, cb, cr, cpi->consec_zero_last[block_index]);
772 #if CONFIG_TEMPORAL_DENOISING
773 if (cpi->oxcf.noise_sensitivity) {
774 // Under aggressive denoising mode, should we use skin map to reduce
776 // and ZEROMV bias? Will need to revisit the accuracy of this detection for
777 // very noisy input. For now keep this as is (i.e., don't turn it off).
778 // if (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive)
783 mode_mv = mode_mv_sb[sign_bias];
784 best_ref_mv.as_int = 0;
785 memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
786 memset(&best_mbmode, 0, sizeof(best_mbmode));
788 /* Setup search priorities */
789 #if CONFIG_MULTI_RES_ENCODING
790 if (parent_ref_valid && parent_ref_frame && dissim < 8) {
791 ref_frame_map[0] = -1;
792 ref_frame_map[1] = parent_ref_frame;
793 ref_frame_map[2] = -1;
794 ref_frame_map[3] = -1;
797 get_reference_search_order(cpi, ref_frame_map);
799 /* Check to see if there is at least 1 valid reference frame that we need
800 * to calculate near_mvs.
802 if (ref_frame_map[1] > 0) {
803 sign_bias = vp8_find_near_mvs_bias(
804 &x->e_mbd, x->e_mbd.mode_info_context, mode_mv_sb, best_ref_mv_sb,
805 mdcounts, ref_frame_map[1], cpi->common.ref_frame_sign_bias);
807 mode_mv = mode_mv_sb[sign_bias];
808 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
811 /* Count of the number of MBs tested so far this frame */
812 x->mbs_tested_so_far++;
814 *returnintra = INT_MAX;
817 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
819 /* If the frame has big static background and current MB is in low
820 * motion area, its mode decision is biased to ZEROMV mode.
821 * No adjustment if cpu_used is <= -12 (i.e., cpi->Speed >= 12).
822 * At such speed settings, ZEROMV is already heavily favored.
824 if (cpi->Speed < 12) {
825 calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
828 #if CONFIG_TEMPORAL_DENOISING
829 if (cpi->oxcf.noise_sensitivity) {
830 rd_adjustment = (int)(rd_adjustment *
831 cpi->denoiser.denoise_pars.pickmode_mv_bias / 100);
835 if (dot_artifact_candidate) {
836 // Bias against ZEROMV_LAST mode.
840 /* if we encode a new mv this is important
841 * find the best new motion vector
843 for (mode_index = 0; mode_index < MAX_MODES; ++mode_index) {
845 int this_rd = INT_MAX;
846 int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
848 if (best_rd <= x->rd_threshes[mode_index]) continue;
850 if (this_ref_frame < 0) continue;
852 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
854 /* everything but intra */
855 if (x->e_mbd.mode_info_context->mbmi.ref_frame) {
856 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
857 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
858 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
860 if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame]) {
861 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
862 mode_mv = mode_mv_sb[sign_bias];
863 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
866 #if CONFIG_MULTI_RES_ENCODING
867 if (parent_ref_valid) {
868 if (vp8_mode_order[mode_index] == NEARESTMV &&
869 mode_mv[NEARESTMV].as_int == 0)
871 if (vp8_mode_order[mode_index] == NEARMV && mode_mv[NEARMV].as_int == 0)
874 if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV &&
875 best_ref_mv.as_int == 0)
877 else if (vp8_mode_order[mode_index] == NEWMV && dissim == 0 &&
878 best_ref_mv.as_int == parent_ref_mv.as_int)
884 /* Check to see if the testing frequency for this mode is at its max
885 * If so then prevent it from being tested and increase the threshold
887 if (x->mode_test_hit_counts[mode_index] &&
888 (cpi->mode_check_freq[mode_index] > 1)) {
889 if (x->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
890 x->mode_test_hit_counts[mode_index])) {
891 /* Increase the threshold for coding this mode to make it less
892 * likely to be chosen */
893 x->rd_thresh_mult[mode_index] += 4;
895 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
896 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
898 x->rd_threshes[mode_index] =
899 (cpi->rd_baseline_thresh[mode_index] >> 7) *
900 x->rd_thresh_mult[mode_index];
905 /* We have now reached the point where we are going to test the current
906 * mode so increment the counter for the number of times it has been
908 x->mode_test_hit_counts[mode_index]++;
913 this_mode = vp8_mode_order[mode_index];
915 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
916 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
918 /* Work out the cost assosciated with selecting the reference frame */
919 frame_cost = x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
922 /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
923 * unless ARNR filtering is enabled in which case we want
924 * an unfiltered alternative */
925 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) {
926 if (this_mode != ZEROMV ||
927 x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
933 /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
934 distortion2 = best_rd_sse;
935 pick_intra4x4mby_modes(x, &rate, &distortion2);
937 if (distortion2 == INT_MAX) {
941 distortion2 = vpx_variance16x16(*(b->base_src), b->src_stride,
942 x->e_mbd.predictor, 16, &sse);
943 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
945 if (this_rd < best_intra_rd) {
946 best_intra_rd = this_rd;
947 *returnintra = distortion2;
955 /* Split MV modes currently not supported when RD is not enabled. */
962 vp8_build_intra_predictors_mby_s(
963 xd, xd->dst.y_buffer - xd->dst.y_stride, xd->dst.y_buffer - 1,
964 xd->dst.y_stride, xd->predictor, 16);
965 distortion2 = vpx_variance16x16(*(b->base_src), b->src_stride,
966 x->e_mbd.predictor, 16, &sse);
967 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context
969 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
971 if (this_rd < best_intra_rd) {
972 best_intra_rd = this_rd;
973 *returnintra = distortion2;
982 int sadpb = x->sadperbit16;
985 int col_min = ((best_ref_mv.as_mv.col + 7) >> 3) - MAX_FULL_PEL_VAL;
986 int row_min = ((best_ref_mv.as_mv.row + 7) >> 3) - MAX_FULL_PEL_VAL;
987 int col_max = (best_ref_mv.as_mv.col >> 3) + MAX_FULL_PEL_VAL;
988 int row_max = (best_ref_mv.as_mv.row >> 3) + MAX_FULL_PEL_VAL;
990 int tmp_col_min = x->mv_col_min;
991 int tmp_col_max = x->mv_col_max;
992 int tmp_row_min = x->mv_row_min;
993 int tmp_row_max = x->mv_row_max;
995 int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8) ? 3 : 2) : 1;
997 /* Further step/diamond searches as necessary */
998 step_param = cpi->sf.first_step + speed_adjust;
1000 #if CONFIG_MULTI_RES_ENCODING
1001 /* If lower-res frame is not available for mv reuse (because of
1002 frame dropping or different temporal layer pattern), then higher
1003 resol encoder does motion search without any previous knowledge.
1004 Also, since last frame motion info is not stored, then we can not
1005 use improved_mv_pred. */
1006 if (cpi->oxcf.mr_encoder_id) sf_improved_mv_pred = 0;
1008 // Only use parent MV as predictor if this candidate reference frame
1009 // (|this_ref_frame|) is equal to |parent_ref_frame|.
1010 if (parent_ref_valid && (parent_ref_frame == this_ref_frame)) {
1011 /* Use parent MV as predictor. Adjust search range
1014 mvp.as_int = parent_ref_mv.as_int;
1015 mvp_full.as_mv.col = parent_ref_mv.as_mv.col >> 3;
1016 mvp_full.as_mv.row = parent_ref_mv.as_mv.row >> 3;
1020 else if (dissim <= 128)
1027 if (sf_improved_mv_pred) {
1029 vp8_cal_sad(cpi, xd, x, recon_yoffset, &near_sadidx[0]);
1033 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
1034 x->e_mbd.mode_info_context->mbmi.ref_frame,
1035 cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
1038 /* adjust search range according to sr from mv prediction */
1039 if (sr > step_param) step_param = sr;
1041 mvp_full.as_mv.col = mvp.as_mv.col >> 3;
1042 mvp_full.as_mv.row = mvp.as_mv.row >> 3;
1044 mvp.as_int = best_ref_mv.as_int;
1045 mvp_full.as_mv.col = best_ref_mv.as_mv.col >> 3;
1046 mvp_full.as_mv.row = best_ref_mv.as_mv.row >> 3;
1050 #if CONFIG_MULTI_RES_ENCODING
1051 if (parent_ref_valid && (parent_ref_frame == this_ref_frame) &&
1053 VPXMAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
1054 abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <= 4) {
1055 d->bmi.mv.as_int = mvp_full.as_int;
1056 mode_mv[NEWMV].as_int = mvp_full.as_int;
1058 cpi->find_fractional_mv_step(
1059 x, b, d, &d->bmi.mv, &best_ref_mv, x->errorperbit,
1060 &cpi->fn_ptr[BLOCK_16X16], cpi->mb.mvcost, &distortion2, &sse);
1064 /* Get intersection of UMV window and valid MV window to
1065 * reduce # of checks in diamond search. */
1066 if (x->mv_col_min < col_min) x->mv_col_min = col_min;
1067 if (x->mv_col_max > col_max) x->mv_col_max = col_max;
1068 if (x->mv_row_min < row_min) x->mv_row_min = row_min;
1069 if (x->mv_row_max > row_max) x->mv_row_max = row_max;
1074 : (cpi->sf.max_step_search_steps - 1 - step_param);
1076 if (cpi->sf.search_method == HEX) {
1077 #if CONFIG_MULTI_RES_ENCODING
1078 /* TODO: In higher-res pick_inter_mode, step_param is used to
1079 * modify hex search range. Here, set step_param to 0 not to
1080 * change the behavior in lowest-resolution encoder.
1081 * Will improve it later.
1083 /* Set step_param to 0 to ensure large-range motion search
1084 * when mv reuse if not valid (i.e. |parent_ref_valid| = 0),
1085 * or if this candidate reference frame (|this_ref_frame|) is
1086 * not equal to |parent_ref_frame|.
1088 if (!parent_ref_valid || (parent_ref_frame != this_ref_frame))
1091 bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv, step_param,
1092 sadpb, &cpi->fn_ptr[BLOCK_16X16],
1093 x->mvsadcost, x->mvcost, &best_ref_mv);
1094 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1096 bestsme = cpi->diamond_search_sad(
1097 x, b, d, &mvp_full, &d->bmi.mv, step_param, sadpb, &num00,
1098 &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv);
1099 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1101 /* Further step/diamond searches as necessary */
1105 while (n < further_steps) {
1111 thissme = cpi->diamond_search_sad(
1112 x, b, d, &mvp_full, &d->bmi.mv, step_param + n, sadpb,
1113 &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv);
1114 if (thissme < bestsme) {
1116 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1118 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
1124 x->mv_col_min = tmp_col_min;
1125 x->mv_col_max = tmp_col_max;
1126 x->mv_row_min = tmp_row_min;
1127 x->mv_row_max = tmp_row_max;
1129 if (bestsme < INT_MAX)
1130 cpi->find_fractional_mv_step(
1131 x, b, d, &d->bmi.mv, &best_ref_mv, x->errorperbit,
1132 &cpi->fn_ptr[BLOCK_16X16], cpi->mb.mvcost, &distortion2, &sse);
1135 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1136 // The clamp below is not necessary from the perspective
1137 // of VP8 bitstream, but is added to improve ChromeCast
1138 // mirroring's robustness. Please do not remove.
1139 vp8_clamp_mv2(&mode_mv[this_mode], xd);
1142 vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, cpi->mb.mvcost, 128);
1147 if (mode_mv[this_mode].as_int == 0) continue;
1151 /* Trap vectors that reach beyond the UMV borders
1152 * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
1153 * through to this point because of the lack of break statements
1154 * in the previous two cases.
1156 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
1157 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
1158 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
1159 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1162 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1163 x->e_mbd.mode_info_context->mbmi.mv.as_int = mode_mv[this_mode].as_int;
1164 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1171 #if CONFIG_TEMPORAL_DENOISING
1172 if (cpi->oxcf.noise_sensitivity) {
1173 /* Store for later use by denoiser. */
1174 // Dont' denoise with GOLDEN OR ALTREF is they are old reference
1175 // frames (greater than MAX_GF_ARF_DENOISE_RANGE frames in past).
1176 int skip_old_reference = ((this_ref_frame != LAST_FRAME) &&
1177 (cpi->common.current_video_frame -
1178 cpi->current_ref_frames[this_ref_frame] >
1179 MAX_GF_ARF_DENOISE_RANGE))
1182 if (this_mode == ZEROMV && sse < zero_mv_sse && !skip_old_reference) {
1184 x->best_zeromv_reference_frame =
1185 x->e_mbd.mode_info_context->mbmi.ref_frame;
1188 // Store the best NEWMV in x for later use in the denoiser.
1189 if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV && sse < best_sse &&
1190 !skip_old_reference) {
1192 x->best_sse_inter_mode = NEWMV;
1193 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
1194 x->need_to_clamp_best_mvs =
1195 x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
1196 x->best_reference_frame = x->e_mbd.mode_info_context->mbmi.ref_frame;
1201 if (this_rd < best_rd || x->skip) {
1202 /* Note index of best mode */
1203 best_mode_index = mode_index;
1205 *returnrate = rate2;
1206 *returndistortion = distortion2;
1209 memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1210 sizeof(MB_MODE_INFO));
1212 /* Testing this mode gave rise to an improvement in best error
1213 * score. Lower threshold a bit for next time
1215 x->rd_thresh_mult[mode_index] =
1216 (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2))
1217 ? x->rd_thresh_mult[mode_index] - 2
1219 x->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) *
1220 x->rd_thresh_mult[mode_index];
1223 /* If the mode did not help improve the best error case then raise the
1224 * threshold for testing that mode next time around.
1227 x->rd_thresh_mult[mode_index] += 4;
1229 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1230 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1232 x->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) *
1233 x->rd_thresh_mult[mode_index];
1239 /* Reduce the activation RD thresholds for the best choice mode */
1240 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) &&
1241 (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2))) {
1242 int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 3);
1244 x->rd_thresh_mult[best_mode_index] =
1245 (x->rd_thresh_mult[best_mode_index] >=
1246 (MIN_THRESHMULT + best_adjustment))
1247 ? x->rd_thresh_mult[best_mode_index] - best_adjustment
1249 x->rd_threshes[best_mode_index] =
1250 (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
1251 x->rd_thresh_mult[best_mode_index];
1255 int this_rdbin = (*returndistortion >> 7);
1257 if (this_rdbin >= 1024) {
1261 x->error_bins[this_rdbin]++;
1264 #if CONFIG_TEMPORAL_DENOISING
1265 if (cpi->oxcf.noise_sensitivity) {
1266 int block_index = mb_row * cpi->common.mb_cols + mb_col;
1269 if (x->best_sse_inter_mode == DC_PRED) {
1270 /* No best MV found. */
1271 x->best_sse_inter_mode = best_mbmode.mode;
1272 x->best_sse_mv = best_mbmode.mv;
1273 x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
1274 x->best_reference_frame = best_mbmode.ref_frame;
1275 best_sse = best_rd_sse;
1277 // For non-skin blocks that have selected ZEROMV for this current frame,
1278 // and have been selecting ZEROMV_LAST (on the base layer frame) at
1279 // least |x~20| consecutive past frames in a row, label the block for
1280 // possible increase in denoising strength. We also condition this
1281 // labeling on there being significant denoising in the scene
1282 if (cpi->oxcf.noise_sensitivity == 4) {
1283 if (cpi->denoiser.nmse_source_diff >
1284 70 * cpi->denoiser.threshold_aggressive_mode / 100)
1287 if (cpi->mse_source_denoised > 1000) is_noisy = 1;
1289 x->increase_denoising = 0;
1290 if (!x->is_skin && x->best_sse_inter_mode == ZEROMV &&
1291 (x->best_reference_frame == LAST_FRAME ||
1292 x->best_reference_frame == cpi->closest_reference_frame) &&
1293 cpi->consec_zero_last[block_index] >= 20 && is_noisy) {
1294 x->increase_denoising = 1;
1296 x->denoise_zeromv = 0;
1297 vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
1298 recon_yoffset, recon_uvoffset, &cpi->common.lf_info,
1299 mb_row, mb_col, block_index,
1300 cpi->consec_zero_last_mvbias[block_index]);
1302 // Reevaluate ZEROMV after denoising: for large noise content
1303 // (i.e., cpi->mse_source_denoised is above threshold), do this for all
1304 // blocks that did not pick ZEROMV as best mode but are using ZEROMV
1305 // for denoising. Otherwise, always re-evaluate for blocks that picked
1306 // INTRA mode as best mode.
1307 // Avoid blocks that have been biased against ZERO_LAST
1308 // (i.e., dot artifact candidate blocks).
1309 reevaluate = (best_mbmode.ref_frame == INTRA_FRAME) ||
1310 (best_mbmode.mode != ZEROMV && x->denoise_zeromv &&
1311 cpi->mse_source_denoised > 2000);
1312 if (!dot_artifact_candidate && reevaluate &&
1313 x->best_zeromv_reference_frame != INTRA_FRAME) {
1315 int this_ref_frame = x->best_zeromv_reference_frame;
1316 rd_adjustment = 100;
1318 x->ref_frame_cost[this_ref_frame] + vp8_cost_mv_ref(ZEROMV, mdcounts);
1321 /* set up the proper prediction buffers for the frame */
1322 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
1323 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
1324 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
1325 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
1327 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1328 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1329 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1331 evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x, rd_adjustment);
1333 if (this_rd < best_rd) {
1334 memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1335 sizeof(MB_MODE_INFO));
1341 if (cpi->is_src_frame_alt_ref &&
1342 (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME)) {
1343 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1344 x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
1345 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1346 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1347 x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
1348 (cpi->common.mb_no_coeff_skip);
1349 x->e_mbd.mode_info_context->mbmi.partitioning = 0;
1354 /* set to the best mb mode, this copy can be skip if x->skip since it
1355 * already has the right content */
1357 memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode,
1358 sizeof(MB_MODE_INFO));
1360 if (best_mbmode.mode <= B_PRED) {
1361 /* set mode_info_context->mbmi.uv_mode */
1362 pick_intra_mbuv_mode(x);
1366 cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
1367 best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
1369 update_mvcount(x, &best_ref_mv);
1372 void vp8_pick_intra_mode(MACROBLOCK *x, int *rate_) {
1373 int error4x4, error16x16 = INT_MAX;
1374 int rate, best_rate = 0, distortion, best_sse;
1375 MB_PREDICTION_MODE mode, best_mode = DC_PRED;
1378 BLOCK *b = &x->block[0];
1379 MACROBLOCKD *xd = &x->e_mbd;
1381 xd->mode_info_context->mbmi.ref_frame = INTRA_FRAME;
1383 pick_intra_mbuv_mode(x);
1385 for (mode = DC_PRED; mode <= TM_PRED; ++mode) {
1386 xd->mode_info_context->mbmi.mode = mode;
1387 vp8_build_intra_predictors_mby_s(xd, xd->dst.y_buffer - xd->dst.y_stride,
1388 xd->dst.y_buffer - 1, xd->dst.y_stride,
1390 distortion = vpx_variance16x16(*(b->base_src), b->src_stride, xd->predictor,
1392 rate = x->mbmode_cost[xd->frame_type][mode];
1393 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1395 if (error16x16 > this_rd) {
1396 error16x16 = this_rd;
1402 xd->mode_info_context->mbmi.mode = best_mode;
1404 error4x4 = pick_intra4x4mby_modes(x, &rate, &best_sse);
1405 if (error4x4 < error16x16) {
1406 xd->mode_info_context->mbmi.mode = B_PRED;