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.
13 #include "vpx_config.h"
14 #include "./vpx_dsp_rtcd.h"
16 #include "modecosts.h"
17 #include "encodeintra.h"
18 #include "vp8/common/common.h"
19 #include "vp8/common/entropymode.h"
20 #include "pickinter.h"
21 #include "vp8/common/findnearmv.h"
23 #include "vp8/common/reconinter.h"
24 #include "vp8/common/reconintra.h"
25 #include "vp8/common/reconintra4x4.h"
26 #include "vpx_dsp/variance.h"
29 #include "vpx_dsp/vpx_dsp_common.h"
30 #include "vpx_mem/vpx_mem.h"
31 #if CONFIG_TEMPORAL_DENOISING
32 #include "denoising.h"
36 extern unsigned int cnt_pm;
41 extern const int vp8_ref_frame_order[MAX_MODES];
42 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
44 // Fixed point implementation of a skin color classifier. Skin color
45 // is model by a Gaussian distribution in the CbCr color space.
46 // See ../../test/skin_color_detector_test.cc where the reference
47 // skin color classifier is defined.
49 // Fixed-point skin color model parameters.
50 static const int skin_mean[5][2] =
51 {{7463, 9614}, {6400, 10240}, {7040, 10240}, {8320, 9280}, {6800, 9614}};
52 static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157}; // q16
53 static const int skin_threshold[6] = {1570636, 1400000, 800000, 800000, 800000,
56 // Evaluates the Mahalanobis distance measure for the input CbCr values.
57 static int evaluate_skin_color_difference(int cb, int cr, int idx) {
58 const int cb_q6 = cb << 6;
59 const int cr_q6 = cr << 6;
60 const int cb_diff_q12 =
61 (cb_q6 - skin_mean[idx][0]) * (cb_q6 - skin_mean[idx][0]);
62 const int cbcr_diff_q12 =
63 (cb_q6 - skin_mean[idx][0]) * (cr_q6 - skin_mean[idx][1]);
64 const int cr_diff_q12 =
65 (cr_q6 - skin_mean[idx][1]) * (cr_q6 - skin_mean[idx][1]);
66 const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
67 const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
68 const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
69 const int skin_diff = skin_inv_cov[0] * cb_diff_q2 +
70 skin_inv_cov[1] * cbcr_diff_q2 +
71 skin_inv_cov[2] * cbcr_diff_q2 +
72 skin_inv_cov[3] * cr_diff_q2;
76 // Checks if the input yCbCr values corresponds to skin color.
77 static int is_skin_color(int y, int cb, int cr, int consec_zeromv)
79 if (y < 40 || y > 220)
87 return (evaluate_skin_color_difference(cb, cr, 0) < skin_threshold[0]);
92 // No skin if block has been zero motion for long consecutive time.
93 if (consec_zeromv > 60)
96 if (cb == 128 && cr == 128)
98 // Exit on very strong cb.
99 if (cb > 150 && cr < 110)
102 int skin_color_diff = evaluate_skin_color_difference(cb, cr, i);
103 if (skin_color_diff < skin_threshold[i + 1]) {
104 if (y < 60 && skin_color_diff > 3 * (skin_threshold[i + 1] >> 2))
106 else if (consec_zeromv > 25 &&
107 skin_color_diff > (skin_threshold[i + 1] >> 1))
112 // Exit if difference is much large than the threshold.
113 if (skin_color_diff > (skin_threshold[i + 1] << 3)) {
122 static int macroblock_corner_grad(unsigned char* signal, int stride,
123 int offsetx, int offsety, int sgnx, int sgny)
125 int y1 = signal[offsetx * stride + offsety];
126 int y2 = signal[offsetx * stride + offsety + sgny];
127 int y3 = signal[(offsetx + sgnx) * stride + offsety];
128 int y4 = signal[(offsetx + sgnx) * stride + offsety + sgny];
129 return VPXMAX(VPXMAX(abs(y1 - y2), abs(y1 - y3)), abs(y1 - y4));
132 static int check_dot_artifact_candidate(VP8_COMP *cpi,
134 unsigned char *target_last,
136 unsigned char* last_ref,
143 unsigned int max_num = (cpi->common.MBs) / 10;
146 int index = mb_row * cpi->common.mb_cols + mb_col;
147 // Threshold for #consecutive (base layer) frames using zero_last mode.
153 if (cpi->oxcf.number_of_layers > 1)
157 x->zero_last_dot_suppress = 0;
158 // Blocks on base layer frames that have been using ZEROMV_LAST repeatedly
159 // (i.e, at least |x| consecutive frames are candidates for increasing the
160 // rd adjustment for zero_last mode.
161 // Only allow this for at most |max_num| blocks per frame.
162 // Don't allow this for screen content input.
163 if (cpi->current_layer == 0 &&
164 cpi->consec_zero_last_mvbias[index] > num_frames &&
165 x->mbs_zero_last_dot_suppress < max_num &&
166 !cpi->oxcf.screen_content_mode)
168 // If this block is checked here, label it so we don't check it again until
169 // ~|x| framaes later.
170 x->zero_last_dot_suppress = 1;
171 // Dot artifact is noticeable as strong gradient at corners of macroblock,
172 // for flat areas. As a simple detector for now, we look for a high
173 // corner gradient on last ref, and a smaller gradient on source.
174 // Check 4 corners, return if any satisfy condition.
176 grad_last = macroblock_corner_grad(last_ref, stride, 0, 0, 1, 1);
177 grad_source = macroblock_corner_grad(target_last, stride, 0, 0, 1, 1);
178 if (grad_last >= threshold1 && grad_source <= threshold2)
180 x->mbs_zero_last_dot_suppress++;
184 grad_last = macroblock_corner_grad(last_ref, stride, 0, shift, 1, -1);
185 grad_source = macroblock_corner_grad(target_last, stride, 0, shift, 1, -1);
186 if (grad_last >= threshold1 && grad_source <= threshold2)
188 x->mbs_zero_last_dot_suppress++;
192 grad_last = macroblock_corner_grad(last_ref, stride, shift, 0, -1, 1);
193 grad_source = macroblock_corner_grad(target_last, stride, shift, 0, -1, 1);
194 if (grad_last >= threshold1 && grad_source <= threshold2)
196 x->mbs_zero_last_dot_suppress++;
200 grad_last = macroblock_corner_grad(last_ref, stride, shift, shift, -1, -1);
201 grad_source = macroblock_corner_grad(target_last, stride, shift, shift, -1, -1);
202 if (grad_last >= threshold1 && grad_source <= threshold2)
204 x->mbs_zero_last_dot_suppress++;
212 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
213 int_mv *bestmv, int_mv *ref_mv,
215 const vp8_variance_fn_ptr_t *vfp,
216 int *mvcost[2], int *distortion,
222 (void) error_per_bit;
228 bestmv->as_mv.row <<= 3;
229 bestmv->as_mv.col <<= 3;
234 int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
235 const vp8_variance_fn_ptr_t *vfp,
240 BLOCK *b = &mb->block[0];
241 BLOCKD *d = &mb->e_mbd.block[0];
242 unsigned char *what = (*(b->base_src) + b->src);
243 int what_stride = b->src_stride;
244 int pre_stride = mb->e_mbd.pre.y_stride;
245 unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset ;
246 int in_what_stride = pre_stride;
247 int xoffset = this_mv.as_mv.col & 7;
248 int yoffset = this_mv.as_mv.row & 7;
250 in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
252 if (xoffset | yoffset)
254 return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
258 return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
263 static int get_prediction_error(BLOCK *be, BLOCKD *b)
267 sptr = (*(be->base_src) + be->src);
270 return vpx_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
274 static int pick_intra4x4block(
277 B_PREDICTION_MODE *best_mode,
278 const int *mode_costs,
284 BLOCKD *b = &x->e_mbd.block[ib];
285 BLOCK *be = &x->block[ib];
286 int dst_stride = x->e_mbd.dst.y_stride;
287 unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
288 B_PREDICTION_MODE mode;
289 int best_rd = INT_MAX;
293 unsigned char *Above = dst - dst_stride;
294 unsigned char *yleft = dst - 1;
295 unsigned char top_left = Above[-1];
297 for (mode = B_DC_PRED; mode <= B_HE_PRED; mode++)
301 rate = mode_costs[mode];
303 vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
304 b->predictor, 16, top_left);
305 distortion = get_prediction_error(be, b);
306 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
308 if (this_rd < best_rd)
311 *bestdistortion = distortion;
317 b->bmi.as_mode = *best_mode;
318 vp8_encode_intra4x4block(x, ib);
323 static int pick_intra4x4mby_modes
330 MACROBLOCKD *const xd = &mb->e_mbd;
332 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
335 const int *bmode_costs;
337 intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
339 bmode_costs = mb->inter_bmode_costs;
341 for (i = 0; i < 16; i++)
343 MODE_INFO *const mic = xd->mode_info_context;
344 const int mis = xd->mode_info_stride;
346 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
347 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
349 if (mb->e_mbd.frame_type == KEY_FRAME)
351 const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
352 const B_PREDICTION_MODE L = left_block_mode(mic, i);
354 bmode_costs = mb->bmode_costs[A][L];
358 pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
362 mic->bmi[i].as_mode = best_mode;
364 /* Break out case where we have already exceeded best so far value
367 if (distortion > *best_dist)
375 *best_dist = distortion;
376 error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
380 *best_dist = INT_MAX;
387 static void pick_intra_mbuv_mode(MACROBLOCK *mb)
390 MACROBLOCKD *x = &mb->e_mbd;
391 unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
392 unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
393 unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
394 unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
395 int uvsrc_stride = mb->block[16].src_stride;
396 unsigned char uleft_col[8];
397 unsigned char vleft_col[8];
398 unsigned char utop_left = uabove_row[-1];
399 unsigned char vtop_left = vabove_row[-1];
407 int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
408 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
411 for (i = 0; i < 8; i++)
413 uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
414 vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
417 if (!x->up_available && !x->left_available)
429 for (i = 0; i < 8; i++)
431 Uaverage += uabove_row[i];
432 Vaverage += vabove_row[i];
439 if (x->left_available)
441 for (i = 0; i < 8; i++)
443 Uaverage += uleft_col[i];
444 Vaverage += vleft_col[i];
451 expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
452 expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
456 for (i = 0; i < 8; i++)
458 for (j = 0; j < 8; j++)
461 int predu = uleft_col[i] + uabove_row[j] - utop_left;
462 int predv = vleft_col[i] + vabove_row[j] - vtop_left;
481 diff = u_p - expected_udc;
482 pred_error[DC_PRED] += diff * diff;
483 diff = v_p - expected_vdc;
484 pred_error[DC_PRED] += diff * diff;
487 diff = u_p - uabove_row[j];
488 pred_error[V_PRED] += diff * diff;
489 diff = v_p - vabove_row[j];
490 pred_error[V_PRED] += diff * diff;
493 diff = u_p - uleft_col[i];
494 pred_error[H_PRED] += diff * diff;
495 diff = v_p - vleft_col[i];
496 pred_error[H_PRED] += diff * diff;
500 pred_error[TM_PRED] += diff * diff;
502 pred_error[TM_PRED] += diff * diff;
507 usrc_ptr += uvsrc_stride;
508 vsrc_ptr += uvsrc_stride;
512 usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
513 vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
521 for (i = DC_PRED; i <= TM_PRED; i++)
523 if (best_error > pred_error[i])
525 best_error = pred_error[i];
526 best_mode = (MB_PREDICTION_MODE)i;
531 mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
535 static void update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
537 MACROBLOCKD *xd = &x->e_mbd;
538 /* Split MV modes currently not supported when RD is nopt enabled,
539 * therefore, only need to modify MVcount in NEWMV mode. */
540 if (xd->mode_info_context->mbmi.mode == NEWMV)
542 x->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
543 best_ref_mv->as_mv.row) >> 1)]++;
544 x->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
545 best_ref_mv->as_mv.col) >> 1)]++;
550 #if CONFIG_MULTI_RES_ENCODING
552 void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd, int *dissim,
553 int *parent_ref_frame,
554 MB_PREDICTION_MODE *parent_mode,
555 int_mv *parent_ref_mv, int mb_row, int mb_col)
557 LOWER_RES_MB_INFO* store_mode_info
558 = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
559 unsigned int parent_mb_index;
561 /* Consider different down_sampling_factor. */
563 /* TODO: Removed the loop that supports special down_sampling_factor
564 * such as 2, 4, 8. Will revisit it if needed.
565 * Should also try using a look-up table to see if it helps
567 int parent_mb_row, parent_mb_col;
569 parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
570 /cpi->oxcf.mr_down_sampling_factor.num;
571 parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
572 /cpi->oxcf.mr_down_sampling_factor.num;
573 parent_mb_index = parent_mb_row*cpi->mr_low_res_mb_cols + parent_mb_col;
576 /* Read lower-resolution mode & motion result from memory.*/
577 *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
578 *parent_mode = store_mode_info[parent_mb_index].mode;
579 *dissim = store_mode_info[parent_mb_index].dissim;
581 /* For highest-resolution encoder, adjust dissim value. Lower its quality
582 * for good performance. */
583 if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
586 if(*parent_ref_frame != INTRA_FRAME)
588 /* Consider different down_sampling_factor.
589 * The result can be rounded to be more precise, but it takes more time.
591 (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row
592 *cpi->oxcf.mr_down_sampling_factor.num
593 /cpi->oxcf.mr_down_sampling_factor.den;
594 (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col
595 *cpi->oxcf.mr_down_sampling_factor.num
596 /cpi->oxcf.mr_down_sampling_factor.den;
598 vp8_clamp_mv2(parent_ref_mv, xd);
603 static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
605 MACROBLOCKD *xd = &x->e_mbd;
607 unsigned int threshold = (xd->block[0].dequant[1]
608 * xd->block[0].dequant[1] >>4);
610 if(threshold < x->encode_breakout)
611 threshold = x->encode_breakout;
613 if (sse < threshold )
615 /* Check u and v to make sure skip is ok */
616 unsigned int sse2 = 0;
620 if (sse2 * 2 < x->encode_breakout)
627 static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2,
628 VP8_COMP *cpi, MACROBLOCK *x, int rd_adj)
630 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
631 int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
633 int denoise_aggressive = 0;
634 /* Exit early and don't compute the distortion if this macroblock
635 * is marked inactive. */
636 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
644 if((this_mode != NEWMV) ||
645 !(cpi->sf.half_pixel_search) || cpi->common.full_pixel==1)
646 *distortion2 = vp8_get_inter_mbpred_error(x,
647 &cpi->fn_ptr[BLOCK_16X16],
650 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);
652 #if CONFIG_TEMPORAL_DENOISING
653 if (cpi->oxcf.noise_sensitivity > 0) {
655 (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) ? 1 : 0;
659 // Adjust rd for ZEROMV and LAST, if LAST is the closest reference frame.
660 // TODO: We should also add condition on distance of closest to current.
661 if(!cpi->oxcf.screen_content_mode &&
662 this_mode == ZEROMV &&
663 x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME &&
664 (denoise_aggressive || (cpi->closest_reference_frame == LAST_FRAME)))
666 // No adjustment if block is considered to be skin area.
670 this_rd = ((int64_t)this_rd) * rd_adj / 100;
673 check_for_encode_breakout(*sse, x);
677 static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
680 MODE_INFO *mic = x->e_mbd.mode_info_context;
681 int_mv mv_l, mv_a, mv_al;
682 int local_motion_check = 0;
684 if (cpi->lf_zeromv_pct > 40)
690 if (mic->mbmi.ref_frame != INTRA_FRAME)
691 if( abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
692 local_motion_check++;
695 mic -= x->e_mbd.mode_info_stride;
696 mv_al = mic->mbmi.mv;
698 if (mic->mbmi.ref_frame != INTRA_FRAME)
699 if( abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
700 local_motion_check++;
706 if (mic->mbmi.ref_frame != INTRA_FRAME)
707 if( abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
708 local_motion_check++;
710 if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge)
711 && local_motion_check >0) || local_motion_check >2 )
713 else if (local_motion_check > 0)
718 void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
719 int recon_uvoffset, int *returnrate,
720 int *returndistortion, int *returnintra, int mb_row,
723 BLOCK *b = &x->block[0];
724 BLOCKD *d = &x->e_mbd.block[0];
725 MACROBLOCKD *xd = &x->e_mbd;
726 MB_MODE_INFO best_mbmode;
728 int_mv best_ref_mv_sb[2];
729 int_mv mode_mv_sb[2][MB_MODE_COUNT];
732 MB_PREDICTION_MODE this_mode;
735 int best_rd = INT_MAX;
736 int rd_adjustment = 100;
737 int best_intra_rd = INT_MAX;
742 int bestsme = INT_MAX;
743 int best_mode_index = 0;
744 unsigned int sse = UINT_MAX, best_rd_sse = UINT_MAX;
745 #if CONFIG_TEMPORAL_DENOISING
746 unsigned int zero_mv_sse = UINT_MAX, best_sse = UINT_MAX;
749 int sf_improved_mv_pred = cpi->sf.improved_mv_pred;
751 #if CONFIG_MULTI_RES_ENCODING
752 int dissim = INT_MAX;
753 int parent_ref_frame = 0;
754 int_mv parent_ref_mv;
755 MB_PREDICTION_MODE parent_mode = 0;
756 int parent_ref_valid = 0;
761 int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
763 /* search range got from mv_pred(). It uses step_param levels. (0-7) */
766 unsigned char *plane[4][3];
767 int ref_frame_map[4];
769 int dot_artifact_candidate = 0;
770 get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
772 // If the current frame is using LAST as a reference, check for
773 // biasing the mode selection for dot artifacts.
774 if (cpi->ref_frame_flags & VP8_LAST_FRAME) {
775 unsigned char* target_y = x->src.y_buffer;
776 unsigned char* target_u = x->block[16].src + *x->block[16].base_src;
777 unsigned char* target_v = x->block[20].src + *x->block[20].base_src;
778 int stride = x->src.y_stride;
779 int stride_uv = x->block[16].src_stride;
780 #if CONFIG_TEMPORAL_DENOISING
781 if (cpi->oxcf.noise_sensitivity) {
782 const int uv_denoise = (cpi->oxcf.noise_sensitivity >= 2) ? 1 : 0;
784 cpi->denoiser.yv12_running_avg[LAST_FRAME].y_buffer + recon_yoffset;
785 stride = cpi->denoiser.yv12_running_avg[LAST_FRAME].y_stride;
788 cpi->denoiser.yv12_running_avg[LAST_FRAME].u_buffer +
791 cpi->denoiser.yv12_running_avg[LAST_FRAME].v_buffer +
793 stride_uv = cpi->denoiser.yv12_running_avg[LAST_FRAME].uv_stride;
797 dot_artifact_candidate =
798 check_dot_artifact_candidate(cpi, x, target_y, stride,
799 plane[LAST_FRAME][0], mb_row, mb_col, 0);
800 // If not found in Y channel, check UV channel.
801 if (!dot_artifact_candidate) {
802 dot_artifact_candidate =
803 check_dot_artifact_candidate(cpi, x, target_u, stride_uv,
804 plane[LAST_FRAME][1], mb_row, mb_col, 1);
805 if (!dot_artifact_candidate) {
806 dot_artifact_candidate =
807 check_dot_artifact_candidate(cpi, x, target_v, stride_uv,
808 plane[LAST_FRAME][2], mb_row, mb_col, 2);
813 #if CONFIG_MULTI_RES_ENCODING
814 // |parent_ref_valid| will be set here if potentially we can do mv resue for
815 // this higher resol (|cpi->oxcf.mr_encoder_id| > 0) frame.
816 // |parent_ref_valid| may be reset depending on |parent_ref_frame| for
817 // the current macroblock below.
818 parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
819 if (parent_ref_valid)
823 get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
824 &parent_mode, &parent_ref_mv, mb_row, mb_col);
826 /* TODO(jkoleszar): The references available (ref_frame_flags) to the
827 * lower res encoder should match those available to this encoder, but
828 * there seems to be a situation where this mismatch can happen in the
829 * case of frame dropping and temporal layers. For example,
830 * GOLD being disallowed in ref_frame_flags, but being returned as
833 * In this event, take the conservative approach of disabling the
834 * lower res info for this MB.
838 // Note availability for mv reuse is only based on last and golden.
839 if (parent_ref_frame == LAST_FRAME)
840 parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
841 else if (parent_ref_frame == GOLDEN_FRAME)
842 parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);
844 //assert(!parent_ref_frame || parent_ref_flag);
846 // If |parent_ref_frame| did not match either last or golden then
847 // shut off mv reuse.
848 if (parent_ref_frame && !parent_ref_flag)
849 parent_ref_valid = 0;
851 // Don't do mv reuse since we want to allow for another mode besides
852 // ZEROMV_LAST to remove dot artifact.
853 if (dot_artifact_candidate)
854 parent_ref_valid = 0;
858 // Check if current macroblock is in skin area.
860 const int y = (x->src.y_buffer[7 * x->src.y_stride + 7] +
861 x->src.y_buffer[7 * x->src.y_stride + 8] +
862 x->src.y_buffer[8 * x->src.y_stride + 7] +
863 x->src.y_buffer[8 * x->src.y_stride + 8]) >> 2;
864 const int cb = (x->src.u_buffer[3 * x->src.uv_stride + 3] +
865 x->src.u_buffer[3 * x->src.uv_stride + 4] +
866 x->src.u_buffer[4 * x->src.uv_stride + 3] +
867 x->src.u_buffer[4 * x->src.uv_stride + 4]) >> 2;
868 const int cr = (x->src.v_buffer[3 * x->src.uv_stride + 3] +
869 x->src.v_buffer[3 * x->src.uv_stride + 4] +
870 x->src.v_buffer[4 * x->src.uv_stride + 3] +
871 x->src.v_buffer[4 * x->src.uv_stride + 4]) >> 2;
873 if (!cpi->oxcf.screen_content_mode) {
874 int block_index = mb_row * cpi->common.mb_cols + mb_col;
875 x->is_skin = is_skin_color(y, cb, cr, cpi->consec_zero_last[block_index]);
878 #if CONFIG_TEMPORAL_DENOISING
879 if (cpi->oxcf.noise_sensitivity) {
880 // Under aggressive denoising mode, should we use skin map to reduce denoiser
881 // and ZEROMV bias? Will need to revisit the accuracy of this detection for
882 // very noisy input. For now keep this as is (i.e., don't turn it off).
883 // if (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive)
888 mode_mv = mode_mv_sb[sign_bias];
889 best_ref_mv.as_int = 0;
890 memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
891 memset(&best_mbmode, 0, sizeof(best_mbmode));
893 /* Setup search priorities */
894 #if CONFIG_MULTI_RES_ENCODING
895 if (parent_ref_valid && parent_ref_frame && dissim < 8)
897 ref_frame_map[0] = -1;
898 ref_frame_map[1] = parent_ref_frame;
899 ref_frame_map[2] = -1;
900 ref_frame_map[3] = -1;
903 get_reference_search_order(cpi, ref_frame_map);
905 /* Check to see if there is at least 1 valid reference frame that we need
906 * to calculate near_mvs.
908 if (ref_frame_map[1] > 0)
910 sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
911 x->e_mbd.mode_info_context,
916 cpi->common.ref_frame_sign_bias);
918 mode_mv = mode_mv_sb[sign_bias];
919 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
922 /* Count of the number of MBs tested so far this frame */
923 x->mbs_tested_so_far++;
925 *returnintra = INT_MAX;
928 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
930 /* If the frame has big static background and current MB is in low
931 * motion area, its mode decision is biased to ZEROMV mode.
932 * No adjustment if cpu_used is <= -12 (i.e., cpi->Speed >= 12).
933 * At such speed settings, ZEROMV is already heavily favored.
935 if (cpi->Speed < 12) {
936 calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
939 #if CONFIG_TEMPORAL_DENOISING
940 if (cpi->oxcf.noise_sensitivity) {
941 rd_adjustment = (int)(rd_adjustment *
942 cpi->denoiser.denoise_pars.pickmode_mv_bias / 100);
946 if (dot_artifact_candidate)
948 // Bias against ZEROMV_LAST mode.
953 /* if we encode a new mv this is important
954 * find the best new motion vector
956 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
959 int this_rd = INT_MAX;
960 int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
962 if (best_rd <= x->rd_threshes[mode_index])
965 if (this_ref_frame < 0)
968 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
970 /* everything but intra */
971 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
973 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
974 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
975 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
977 if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
979 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
980 mode_mv = mode_mv_sb[sign_bias];
981 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
984 #if CONFIG_MULTI_RES_ENCODING
985 if (parent_ref_valid)
987 if (vp8_mode_order[mode_index] == NEARESTMV &&
988 mode_mv[NEARESTMV].as_int ==0)
990 if (vp8_mode_order[mode_index] == NEARMV &&
991 mode_mv[NEARMV].as_int ==0)
994 if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV
995 && best_ref_mv.as_int==0)
997 else if(vp8_mode_order[mode_index] == NEWMV && dissim==0
998 && best_ref_mv.as_int==parent_ref_mv.as_int)
1004 /* Check to see if the testing frequency for this mode is at its max
1005 * If so then prevent it from being tested and increase the threshold
1006 * for its testing */
1007 if (x->mode_test_hit_counts[mode_index] &&
1008 (cpi->mode_check_freq[mode_index] > 1))
1010 if (x->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
1011 x->mode_test_hit_counts[mode_index]))
1013 /* Increase the threshold for coding this mode to make it less
1014 * likely to be chosen */
1015 x->rd_thresh_mult[mode_index] += 4;
1017 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1018 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1020 x->rd_threshes[mode_index] =
1021 (cpi->rd_baseline_thresh[mode_index] >> 7) *
1022 x->rd_thresh_mult[mode_index];
1027 /* We have now reached the point where we are going to test the current
1028 * mode so increment the counter for the number of times it has been
1030 x->mode_test_hit_counts[mode_index] ++;
1035 this_mode = vp8_mode_order[mode_index];
1037 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
1038 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1040 /* Work out the cost assosciated with selecting the reference frame */
1042 x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
1043 rate2 += frame_cost;
1045 /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
1046 * unless ARNR filtering is enabled in which case we want
1047 * an unfiltered alternative */
1048 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
1050 if (this_mode != ZEROMV ||
1051 x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
1058 /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
1059 distortion2 = best_rd_sse;
1060 pick_intra4x4mby_modes(x, &rate, &distortion2);
1062 if (distortion2 == INT_MAX)
1069 distortion2 = vpx_variance16x16(
1070 *(b->base_src), b->src_stride,
1071 x->e_mbd.predictor, 16, &sse);
1072 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
1074 if (this_rd < best_intra_rd)
1076 best_intra_rd = this_rd;
1077 *returnintra = distortion2;
1085 /* Split MV modes currently not supported when RD is not enabled. */
1092 vp8_build_intra_predictors_mby_s(xd,
1093 xd->dst.y_buffer - xd->dst.y_stride,
1094 xd->dst.y_buffer - 1,
1098 distortion2 = vpx_variance16x16
1099 (*(b->base_src), b->src_stride,
1100 x->e_mbd.predictor, 16, &sse);
1101 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
1102 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
1104 if (this_rd < best_intra_rd)
1106 best_intra_rd = this_rd;
1107 *returnintra = distortion2;
1117 int sadpb = x->sadperbit16;
1120 int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
1121 int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
1122 int col_max = (best_ref_mv.as_mv.col>>3)
1124 int row_max = (best_ref_mv.as_mv.row>>3)
1127 int tmp_col_min = x->mv_col_min;
1128 int tmp_col_max = x->mv_col_max;
1129 int tmp_row_min = x->mv_row_min;
1130 int tmp_row_max = x->mv_row_max;
1132 int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
1134 /* Further step/diamond searches as necessary */
1135 step_param = cpi->sf.first_step + speed_adjust;
1137 #if CONFIG_MULTI_RES_ENCODING
1138 /* If lower-res frame is not available for mv reuse (because of
1139 frame dropping or different temporal layer pattern), then higher
1140 resol encoder does motion search without any previous knowledge.
1141 Also, since last frame motion info is not stored, then we can not
1142 use improved_mv_pred. */
1143 if (cpi->oxcf.mr_encoder_id)
1144 sf_improved_mv_pred = 0;
1146 // Only use parent MV as predictor if this candidate reference frame
1147 // (|this_ref_frame|) is equal to |parent_ref_frame|.
1148 if (parent_ref_valid && (parent_ref_frame == this_ref_frame))
1150 /* Use parent MV as predictor. Adjust search range
1153 mvp.as_int = parent_ref_mv.as_int;
1154 mvp_full.as_mv.col = parent_ref_mv.as_mv.col>>3;
1155 mvp_full.as_mv.row = parent_ref_mv.as_mv.row>>3;
1157 if(dissim <=32) step_param += 3;
1158 else if(dissim <=128) step_param += 2;
1159 else step_param += 1;
1163 if(sf_improved_mv_pred)
1167 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
1171 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context,
1172 &mvp,x->e_mbd.mode_info_context->mbmi.ref_frame,
1173 cpi->common.ref_frame_sign_bias, &sr,
1177 /* adjust search range according to sr from mv prediction */
1181 mvp_full.as_mv.col = mvp.as_mv.col>>3;
1182 mvp_full.as_mv.row = mvp.as_mv.row>>3;
1185 mvp.as_int = best_ref_mv.as_int;
1186 mvp_full.as_mv.col = best_ref_mv.as_mv.col>>3;
1187 mvp_full.as_mv.row = best_ref_mv.as_mv.row>>3;
1191 #if CONFIG_MULTI_RES_ENCODING
1192 if (parent_ref_valid && (parent_ref_frame == this_ref_frame) &&
1194 VPXMAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
1195 abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <=
1198 d->bmi.mv.as_int = mvp_full.as_int;
1199 mode_mv[NEWMV].as_int = mvp_full.as_int;
1201 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
1203 &cpi->fn_ptr[BLOCK_16X16],
1209 /* Get intersection of UMV window and valid MV window to
1210 * reduce # of checks in diamond search. */
1211 if (x->mv_col_min < col_min )
1212 x->mv_col_min = col_min;
1213 if (x->mv_col_max > col_max )
1214 x->mv_col_max = col_max;
1215 if (x->mv_row_min < row_min )
1216 x->mv_row_min = row_min;
1217 if (x->mv_row_max > row_max )
1218 x->mv_row_max = row_max;
1220 further_steps = (cpi->Speed >= 8)?
1221 0: (cpi->sf.max_step_search_steps - 1 - step_param);
1223 if (cpi->sf.search_method == HEX)
1225 #if CONFIG_MULTI_RES_ENCODING
1226 /* TODO: In higher-res pick_inter_mode, step_param is used to
1227 * modify hex search range. Here, set step_param to 0 not to
1228 * change the behavior in lowest-resolution encoder.
1229 * Will improve it later.
1231 /* Set step_param to 0 to ensure large-range motion search
1232 * when mv reuse if not valid (i.e. |parent_ref_valid| = 0),
1233 * or if this candidate reference frame (|this_ref_frame|) is
1234 * not equal to |parent_ref_frame|.
1236 if (!parent_ref_valid || (parent_ref_frame != this_ref_frame))
1239 bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv,
1241 &cpi->fn_ptr[BLOCK_16X16],
1242 x->mvsadcost, x->mvcost, &best_ref_mv);
1243 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1247 bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full,
1248 &d->bmi.mv, step_param, sadpb, &num00,
1249 &cpi->fn_ptr[BLOCK_16X16],
1250 x->mvcost, &best_ref_mv);
1251 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1253 /* Further step/diamond searches as necessary */
1257 while (n < further_steps)
1266 cpi->diamond_search_sad(x, b, d, &mvp_full,
1270 &cpi->fn_ptr[BLOCK_16X16],
1271 x->mvcost, &best_ref_mv);
1272 if (thissme < bestsme)
1275 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1279 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
1285 x->mv_col_min = tmp_col_min;
1286 x->mv_col_max = tmp_col_max;
1287 x->mv_row_min = tmp_row_min;
1288 x->mv_row_max = tmp_row_max;
1290 if (bestsme < INT_MAX)
1291 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
1292 &best_ref_mv, x->errorperbit,
1293 &cpi->fn_ptr[BLOCK_16X16],
1298 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1299 // The clamp below is not necessary from the perspective
1300 // of VP8 bitstream, but is added to improve ChromeCast
1301 // mirroring's robustness. Please do not remove.
1302 vp8_clamp_mv2(&mode_mv[this_mode], xd);
1304 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
1305 cpi->mb.mvcost, 128);
1310 if (mode_mv[this_mode].as_int == 0)
1315 /* Trap vectors that reach beyond the UMV borders
1316 * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
1317 * through to this point because of the lack of break statements
1318 * in the previous two cases.
1320 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
1321 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
1322 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
1323 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1326 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1327 x->e_mbd.mode_info_context->mbmi.mv.as_int =
1328 mode_mv[this_mode].as_int;
1329 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1337 #if CONFIG_TEMPORAL_DENOISING
1338 if (cpi->oxcf.noise_sensitivity)
1340 /* Store for later use by denoiser. */
1341 // Dont' denoise with GOLDEN OR ALTREF is they are old reference
1342 // frames (greater than MAX_GF_ARF_DENOISE_RANGE frames in past).
1343 int skip_old_reference = ((this_ref_frame != LAST_FRAME) &&
1344 (cpi->common.current_video_frame -
1345 cpi->current_ref_frames[this_ref_frame] >
1346 MAX_GF_ARF_DENOISE_RANGE)) ? 1 : 0;
1347 if (this_mode == ZEROMV && sse < zero_mv_sse &&
1348 !skip_old_reference)
1351 x->best_zeromv_reference_frame =
1352 x->e_mbd.mode_info_context->mbmi.ref_frame;
1355 // Store the best NEWMV in x for later use in the denoiser.
1356 if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
1357 sse < best_sse && !skip_old_reference)
1360 x->best_sse_inter_mode = NEWMV;
1361 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
1362 x->need_to_clamp_best_mvs =
1363 x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
1364 x->best_reference_frame =
1365 x->e_mbd.mode_info_context->mbmi.ref_frame;
1370 if (this_rd < best_rd || x->skip)
1372 /* Note index of best mode */
1373 best_mode_index = mode_index;
1375 *returnrate = rate2;
1376 *returndistortion = distortion2;
1379 memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1380 sizeof(MB_MODE_INFO));
1382 /* Testing this mode gave rise to an improvement in best error
1383 * score. Lower threshold a bit for next time
1385 x->rd_thresh_mult[mode_index] =
1386 (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
1387 x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
1388 x->rd_threshes[mode_index] =
1389 (cpi->rd_baseline_thresh[mode_index] >> 7) *
1390 x->rd_thresh_mult[mode_index];
1393 /* If the mode did not help improve the best error case then raise the
1394 * threshold for testing that mode next time around.
1398 x->rd_thresh_mult[mode_index] += 4;
1400 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1401 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1403 x->rd_threshes[mode_index] =
1404 (cpi->rd_baseline_thresh[mode_index] >> 7) *
1405 x->rd_thresh_mult[mode_index];
1412 /* Reduce the activation RD thresholds for the best choice mode */
1413 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
1415 int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 3);
1417 x->rd_thresh_mult[best_mode_index] =
1418 (x->rd_thresh_mult[best_mode_index]
1419 >= (MIN_THRESHMULT + best_adjustment)) ?
1420 x->rd_thresh_mult[best_mode_index] - best_adjustment :
1422 x->rd_threshes[best_mode_index] =
1423 (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
1424 x->rd_thresh_mult[best_mode_index];
1429 int this_rdbin = (*returndistortion >> 7);
1431 if (this_rdbin >= 1024)
1436 x->error_bins[this_rdbin] ++;
1439 #if CONFIG_TEMPORAL_DENOISING
1440 if (cpi->oxcf.noise_sensitivity)
1442 int block_index = mb_row * cpi->common.mb_cols + mb_col;
1445 if (x->best_sse_inter_mode == DC_PRED)
1447 /* No best MV found. */
1448 x->best_sse_inter_mode = best_mbmode.mode;
1449 x->best_sse_mv = best_mbmode.mv;
1450 x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
1451 x->best_reference_frame = best_mbmode.ref_frame;
1452 best_sse = best_rd_sse;
1454 // For non-skin blocks that have selected ZEROMV for this current frame,
1455 // and have been selecting ZEROMV_LAST (on the base layer frame) at
1456 // least |x~20| consecutive past frames in a row, label the block for
1457 // possible increase in denoising strength. We also condition this
1458 // labeling on there being significant denoising in the scene
1459 if (cpi->oxcf.noise_sensitivity == 4) {
1460 if (cpi->denoiser.nmse_source_diff >
1461 70 * cpi->denoiser.threshold_aggressive_mode / 100)
1464 if (cpi->mse_source_denoised > 1000)
1467 x->increase_denoising = 0;
1469 x->best_sse_inter_mode == ZEROMV &&
1470 (x->best_reference_frame == LAST_FRAME ||
1471 x->best_reference_frame == cpi->closest_reference_frame) &&
1472 cpi->consec_zero_last[block_index] >= 20 &&
1474 x->increase_denoising = 1;
1476 x->denoise_zeromv = 0;
1477 vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
1478 recon_yoffset, recon_uvoffset,
1479 &cpi->common.lf_info, mb_row, mb_col,
1481 cpi->consec_zero_last_mvbias[block_index]);
1483 // Reevaluate ZEROMV after denoising: for large noise content
1484 // (i.e., cpi->mse_source_denoised is above threshold), do this for all
1485 // blocks that did not pick ZEROMV as best mode but are using ZEROMV
1486 // for denoising. Otherwise, always re-evaluate for blocks that picked
1487 // INTRA mode as best mode.
1488 // Avoid blocks that have been biased against ZERO_LAST
1489 // (i.e., dot artifact candidate blocks).
1490 reevaluate = (best_mbmode.ref_frame == INTRA_FRAME) ||
1491 (best_mbmode.mode != ZEROMV &&
1492 x->denoise_zeromv &&
1493 cpi->mse_source_denoised > 2000);
1494 if (!dot_artifact_candidate &&
1496 x->best_zeromv_reference_frame != INTRA_FRAME)
1499 int this_ref_frame = x->best_zeromv_reference_frame;
1500 rd_adjustment = 100;
1501 rate2 = x->ref_frame_cost[this_ref_frame] +
1502 vp8_cost_mv_ref(ZEROMV, mdcounts);
1505 /* set up the proper prediction buffers for the frame */
1506 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
1507 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
1508 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
1509 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
1511 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1512 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1513 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1514 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1517 if (this_rd < best_rd)
1519 memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1520 sizeof(MB_MODE_INFO));
1527 if (cpi->is_src_frame_alt_ref &&
1528 (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
1530 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1531 x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
1532 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1533 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1534 x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
1535 (cpi->common.mb_no_coeff_skip);
1536 x->e_mbd.mode_info_context->mbmi.partitioning = 0;
1541 /* set to the best mb mode, this copy can be skip if x->skip since it
1542 * already has the right content */
1544 memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode,
1545 sizeof(MB_MODE_INFO));
1547 if (best_mbmode.mode <= B_PRED)
1549 /* set mode_info_context->mbmi.uv_mode */
1550 pick_intra_mbuv_mode(x);
1554 != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
1555 best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
1557 update_mvcount(x, &best_ref_mv);
1560 void vp8_pick_intra_mode(MACROBLOCK *x, int *rate_)
1562 int error4x4, error16x16 = INT_MAX;
1563 int rate, best_rate = 0, distortion, best_sse;
1564 MB_PREDICTION_MODE mode, best_mode = DC_PRED;
1567 BLOCK *b = &x->block[0];
1568 MACROBLOCKD *xd = &x->e_mbd;
1570 xd->mode_info_context->mbmi.ref_frame = INTRA_FRAME;
1572 pick_intra_mbuv_mode(x);
1574 for (mode = DC_PRED; mode <= TM_PRED; mode ++)
1576 xd->mode_info_context->mbmi.mode = mode;
1577 vp8_build_intra_predictors_mby_s(xd,
1578 xd->dst.y_buffer - xd->dst.y_stride,
1579 xd->dst.y_buffer - 1,
1583 distortion = vpx_variance16x16
1584 (*(b->base_src), b->src_stride, xd->predictor, 16, &sse);
1585 rate = x->mbmode_cost[xd->frame_type][mode];
1586 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1588 if (error16x16 > this_rd)
1590 error16x16 = this_rd;
1596 xd->mode_info_context->mbmi.mode = best_mode;
1598 error4x4 = pick_intra4x4mby_modes(x, &rate,
1600 if (error4x4 < error16x16)
1602 xd->mode_info_context->mbmi.mode = B_PRED;