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/reconintra4x4.h"
25 #include "vpx_dsp/variance.h"
28 #include "vpx_mem/vpx_mem.h"
29 #if CONFIG_TEMPORAL_DENOISING
30 #include "denoising.h"
34 extern unsigned int cnt_pm;
37 extern const int vp8_ref_frame_order[MAX_MODES];
38 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
40 // Fixed point implementation of a skin color classifier. Skin color
41 // is model by a Gaussian distribution in the CbCr color space.
42 // See ../../test/skin_color_detector_test.cc where the reference
43 // skin color classifier is defined.
45 // Fixed-point skin color model parameters.
46 static const int skin_mean[2] = {7463, 9614}; // q6
47 static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157}; // q16
48 static const int skin_threshold = 1570636; // q18
50 // Evaluates the Mahalanobis distance measure for the input CbCr values.
51 static int evaluate_skin_color_difference(int cb, int cr)
53 const int cb_q6 = cb << 6;
54 const int cr_q6 = cr << 6;
55 const int cb_diff_q12 = (cb_q6 - skin_mean[0]) * (cb_q6 - skin_mean[0]);
56 const int cbcr_diff_q12 = (cb_q6 - skin_mean[0]) * (cr_q6 - skin_mean[1]);
57 const int cr_diff_q12 = (cr_q6 - skin_mean[1]) * (cr_q6 - skin_mean[1]);
58 const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
59 const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
60 const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
61 const int skin_diff = skin_inv_cov[0] * cb_diff_q2 +
62 skin_inv_cov[1] * cbcr_diff_q2 +
63 skin_inv_cov[2] * cbcr_diff_q2 +
64 skin_inv_cov[3] * cr_diff_q2;
68 static int macroblock_corner_grad(unsigned char* signal, int stride,
69 int offsetx, int offsety, int sgnx, int sgny)
71 int y1 = signal[offsetx * stride + offsety];
72 int y2 = signal[offsetx * stride + offsety + sgny];
73 int y3 = signal[(offsetx + sgnx) * stride + offsety];
74 int y4 = signal[(offsetx + sgnx) * stride + offsety + sgny];
75 return MAX(MAX(abs(y1 - y2), abs(y1 - y3)), abs(y1 - y4));
78 static int check_dot_artifact_candidate(VP8_COMP *cpi,
80 unsigned char *target_last,
82 unsigned char* last_ref,
89 unsigned int max_num = (cpi->common.MBs) / 10;
92 int index = mb_row * cpi->common.mb_cols + mb_col;
93 // Threshold for #consecutive (base layer) frames using zero_last mode.
99 if (cpi->oxcf.number_of_layers > 1)
103 x->zero_last_dot_suppress = 0;
104 // Blocks on base layer frames that have been using ZEROMV_LAST repeatedly
105 // (i.e, at least |x| consecutive frames are candidates for increasing the
106 // rd adjustment for zero_last mode.
107 // Only allow this for at most |max_num| blocks per frame.
108 // Don't allow this for screen content input.
109 if (cpi->current_layer == 0 &&
110 cpi->consec_zero_last_mvbias[index] > num_frames &&
111 x->mbs_zero_last_dot_suppress < max_num &&
112 !cpi->oxcf.screen_content_mode)
114 // If this block is checked here, label it so we don't check it again until
115 // ~|x| framaes later.
116 x->zero_last_dot_suppress = 1;
117 // Dot artifact is noticeable as strong gradient at corners of macroblock,
118 // for flat areas. As a simple detector for now, we look for a high
119 // corner gradient on last ref, and a smaller gradient on source.
120 // Check 4 corners, return if any satisfy condition.
122 grad_last = macroblock_corner_grad(last_ref, stride, 0, 0, 1, 1);
123 grad_source = macroblock_corner_grad(target_last, stride, 0, 0, 1, 1);
124 if (grad_last >= threshold1 && grad_source <= threshold2)
126 x->mbs_zero_last_dot_suppress++;
130 grad_last = macroblock_corner_grad(last_ref, stride, 0, shift, 1, -1);
131 grad_source = macroblock_corner_grad(target_last, stride, 0, shift, 1, -1);
132 if (grad_last >= threshold1 && grad_source <= threshold2)
134 x->mbs_zero_last_dot_suppress++;
138 grad_last = macroblock_corner_grad(last_ref, stride, shift, 0, -1, 1);
139 grad_source = macroblock_corner_grad(target_last, stride, shift, 0, -1, 1);
140 if (grad_last >= threshold1 && grad_source <= threshold2)
142 x->mbs_zero_last_dot_suppress++;
146 grad_last = macroblock_corner_grad(last_ref, stride, shift, shift, -1, -1);
147 grad_source = macroblock_corner_grad(target_last, stride, shift, shift, -1, -1);
148 if (grad_last >= threshold1 && grad_source <= threshold2)
150 x->mbs_zero_last_dot_suppress++;
158 // Checks if the input yCbCr values corresponds to skin color.
159 static int is_skin_color(int y, int cb, int cr)
161 if (y < 40 || y > 220)
165 return (evaluate_skin_color_difference(cb, cr) < skin_threshold);
168 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
169 int_mv *bestmv, int_mv *ref_mv,
171 const vp8_variance_fn_ptr_t *vfp,
172 int *mvcost[2], int *distortion,
178 (void) error_per_bit;
184 bestmv->as_mv.row <<= 3;
185 bestmv->as_mv.col <<= 3;
190 int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
191 const vp8_variance_fn_ptr_t *vfp,
196 BLOCK *b = &mb->block[0];
197 BLOCKD *d = &mb->e_mbd.block[0];
198 unsigned char *what = (*(b->base_src) + b->src);
199 int what_stride = b->src_stride;
200 int pre_stride = mb->e_mbd.pre.y_stride;
201 unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset ;
202 int in_what_stride = pre_stride;
203 int xoffset = this_mv.as_mv.col & 7;
204 int yoffset = this_mv.as_mv.row & 7;
206 in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
208 if (xoffset | yoffset)
210 return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
214 return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
219 static int get_prediction_error(BLOCK *be, BLOCKD *b)
223 sptr = (*(be->base_src) + be->src);
226 return vpx_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
230 static int pick_intra4x4block(
233 B_PREDICTION_MODE *best_mode,
234 const int *mode_costs,
240 BLOCKD *b = &x->e_mbd.block[ib];
241 BLOCK *be = &x->block[ib];
242 int dst_stride = x->e_mbd.dst.y_stride;
243 unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
244 B_PREDICTION_MODE mode;
245 int best_rd = INT_MAX;
249 unsigned char *Above = dst - dst_stride;
250 unsigned char *yleft = dst - 1;
251 unsigned char top_left = Above[-1];
253 for (mode = B_DC_PRED; mode <= B_HE_PRED; mode++)
257 rate = mode_costs[mode];
259 vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
260 b->predictor, 16, top_left);
261 distortion = get_prediction_error(be, b);
262 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
264 if (this_rd < best_rd)
267 *bestdistortion = distortion;
273 b->bmi.as_mode = *best_mode;
274 vp8_encode_intra4x4block(x, ib);
279 static int pick_intra4x4mby_modes
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++)
299 MODE_INFO *const mic = xd->mode_info_context;
300 const int mis = xd->mode_info_stride;
302 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
303 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
305 if (mb->e_mbd.frame_type == KEY_FRAME)
307 const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
308 const B_PREDICTION_MODE L = left_block_mode(mic, i);
310 bmode_costs = mb->bmode_costs[A][L];
314 pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
318 mic->bmi[i].as_mode = best_mode;
320 /* Break out case where we have already exceeded best so far value
323 if (distortion > *best_dist)
331 *best_dist = distortion;
332 error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
336 *best_dist = INT_MAX;
343 static void pick_intra_mbuv_mode(MACROBLOCK *mb)
346 MACROBLOCKD *x = &mb->e_mbd;
347 unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
348 unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
349 unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
350 unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
351 int uvsrc_stride = mb->block[16].src_stride;
352 unsigned char uleft_col[8];
353 unsigned char vleft_col[8];
354 unsigned char utop_left = uabove_row[-1];
355 unsigned char vtop_left = vabove_row[-1];
363 int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
364 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
367 for (i = 0; i < 8; i++)
369 uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
370 vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
373 if (!x->up_available && !x->left_available)
385 for (i = 0; i < 8; i++)
387 Uaverage += uabove_row[i];
388 Vaverage += vabove_row[i];
395 if (x->left_available)
397 for (i = 0; i < 8; i++)
399 Uaverage += uleft_col[i];
400 Vaverage += vleft_col[i];
407 expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
408 expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
412 for (i = 0; i < 8; i++)
414 for (j = 0; j < 8; j++)
417 int predu = uleft_col[i] + uabove_row[j] - utop_left;
418 int predv = vleft_col[i] + vabove_row[j] - vtop_left;
437 diff = u_p - expected_udc;
438 pred_error[DC_PRED] += diff * diff;
439 diff = v_p - expected_vdc;
440 pred_error[DC_PRED] += diff * diff;
443 diff = u_p - uabove_row[j];
444 pred_error[V_PRED] += diff * diff;
445 diff = v_p - vabove_row[j];
446 pred_error[V_PRED] += diff * diff;
449 diff = u_p - uleft_col[i];
450 pred_error[H_PRED] += diff * diff;
451 diff = v_p - vleft_col[i];
452 pred_error[H_PRED] += diff * diff;
456 pred_error[TM_PRED] += diff * diff;
458 pred_error[TM_PRED] += diff * diff;
463 usrc_ptr += uvsrc_stride;
464 vsrc_ptr += uvsrc_stride;
468 usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
469 vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
477 for (i = DC_PRED; i <= TM_PRED; i++)
479 if (best_error > pred_error[i])
481 best_error = pred_error[i];
482 best_mode = (MB_PREDICTION_MODE)i;
487 mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
491 static void update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
493 MACROBLOCKD *xd = &x->e_mbd;
494 /* Split MV modes currently not supported when RD is nopt enabled,
495 * therefore, only need to modify MVcount in NEWMV mode. */
496 if (xd->mode_info_context->mbmi.mode == NEWMV)
498 x->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
499 best_ref_mv->as_mv.row) >> 1)]++;
500 x->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
501 best_ref_mv->as_mv.col) >> 1)]++;
506 #if CONFIG_MULTI_RES_ENCODING
508 void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd, int *dissim,
509 int *parent_ref_frame,
510 MB_PREDICTION_MODE *parent_mode,
511 int_mv *parent_ref_mv, int mb_row, int mb_col)
513 LOWER_RES_MB_INFO* store_mode_info
514 = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
515 unsigned int parent_mb_index;
517 /* Consider different down_sampling_factor. */
519 /* TODO: Removed the loop that supports special down_sampling_factor
520 * such as 2, 4, 8. Will revisit it if needed.
521 * Should also try using a look-up table to see if it helps
523 int parent_mb_row, parent_mb_col;
525 parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
526 /cpi->oxcf.mr_down_sampling_factor.num;
527 parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
528 /cpi->oxcf.mr_down_sampling_factor.num;
529 parent_mb_index = parent_mb_row*cpi->mr_low_res_mb_cols + parent_mb_col;
532 /* Read lower-resolution mode & motion result from memory.*/
533 *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
534 *parent_mode = store_mode_info[parent_mb_index].mode;
535 *dissim = store_mode_info[parent_mb_index].dissim;
537 /* For highest-resolution encoder, adjust dissim value. Lower its quality
538 * for good performance. */
539 if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
542 if(*parent_ref_frame != INTRA_FRAME)
544 /* Consider different down_sampling_factor.
545 * The result can be rounded to be more precise, but it takes more time.
547 (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row
548 *cpi->oxcf.mr_down_sampling_factor.num
549 /cpi->oxcf.mr_down_sampling_factor.den;
550 (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col
551 *cpi->oxcf.mr_down_sampling_factor.num
552 /cpi->oxcf.mr_down_sampling_factor.den;
554 vp8_clamp_mv2(parent_ref_mv, xd);
559 static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
561 MACROBLOCKD *xd = &x->e_mbd;
563 unsigned int threshold = (xd->block[0].dequant[1]
564 * xd->block[0].dequant[1] >>4);
566 if(threshold < x->encode_breakout)
567 threshold = x->encode_breakout;
569 if (sse < threshold )
571 /* Check u and v to make sure skip is ok */
572 unsigned int sse2 = 0;
576 if (sse2 * 2 < x->encode_breakout)
583 static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2,
584 VP8_COMP *cpi, MACROBLOCK *x, int rd_adj)
586 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
587 int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
589 int denoise_aggressive = 0;
590 /* Exit early and don't compute the distortion if this macroblock
591 * is marked inactive. */
592 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
600 if((this_mode != NEWMV) ||
601 !(cpi->sf.half_pixel_search) || cpi->common.full_pixel==1)
602 *distortion2 = vp8_get_inter_mbpred_error(x,
603 &cpi->fn_ptr[BLOCK_16X16],
606 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);
608 #if CONFIG_TEMPORAL_DENOISING
609 if (cpi->oxcf.noise_sensitivity > 0) {
611 (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) ? 1 : 0;
615 // Adjust rd for ZEROMV and LAST, if LAST is the closest reference frame.
616 // TODO: We should also add condition on distance of closest to current.
617 if(!cpi->oxcf.screen_content_mode &&
618 this_mode == ZEROMV &&
619 x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME &&
620 (denoise_aggressive || (cpi->closest_reference_frame == LAST_FRAME)))
622 // No adjustment if block is considered to be skin area.
626 this_rd = ((int64_t)this_rd) * rd_adj / 100;
629 check_for_encode_breakout(*sse, x);
633 static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
636 MODE_INFO *mic = x->e_mbd.mode_info_context;
637 int_mv mv_l, mv_a, mv_al;
638 int local_motion_check = 0;
640 if (cpi->lf_zeromv_pct > 40)
646 if (mic->mbmi.ref_frame != INTRA_FRAME)
647 if( abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
648 local_motion_check++;
651 mic -= x->e_mbd.mode_info_stride;
652 mv_al = mic->mbmi.mv;
654 if (mic->mbmi.ref_frame != INTRA_FRAME)
655 if( abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
656 local_motion_check++;
662 if (mic->mbmi.ref_frame != INTRA_FRAME)
663 if( abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
664 local_motion_check++;
666 if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge)
667 && local_motion_check >0) || local_motion_check >2 )
669 else if (local_motion_check > 0)
674 void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
675 int recon_uvoffset, int *returnrate,
676 int *returndistortion, int *returnintra, int mb_row,
679 BLOCK *b = &x->block[0];
680 BLOCKD *d = &x->e_mbd.block[0];
681 MACROBLOCKD *xd = &x->e_mbd;
682 MB_MODE_INFO best_mbmode;
684 int_mv best_ref_mv_sb[2];
685 int_mv mode_mv_sb[2][MB_MODE_COUNT];
688 MB_PREDICTION_MODE this_mode;
691 int best_rd = INT_MAX;
692 int rd_adjustment = 100;
693 int best_intra_rd = INT_MAX;
698 int bestsme = INT_MAX;
699 int best_mode_index = 0;
700 unsigned int sse = UINT_MAX, best_rd_sse = UINT_MAX;
701 #if CONFIG_TEMPORAL_DENOISING
702 unsigned int zero_mv_sse = UINT_MAX, best_sse = UINT_MAX;
705 int sf_improved_mv_pred = cpi->sf.improved_mv_pred;
707 #if CONFIG_MULTI_RES_ENCODING
708 int dissim = INT_MAX;
709 int parent_ref_frame = 0;
710 int_mv parent_ref_mv;
711 MB_PREDICTION_MODE parent_mode = 0;
712 int parent_ref_valid = 0;
717 int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
719 /* search range got from mv_pred(). It uses step_param levels. (0-7) */
722 unsigned char *plane[4][3];
723 int ref_frame_map[4];
725 int dot_artifact_candidate = 0;
726 get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
728 // If the current frame is using LAST as a reference, check for
729 // biasing the mode selection for dot artifacts.
730 if (cpi->ref_frame_flags & VP8_LAST_FRAME) {
731 unsigned char* target_y = x->src.y_buffer;
732 unsigned char* target_u = x->block[16].src + *x->block[16].base_src;
733 unsigned char* target_v = x->block[20].src + *x->block[20].base_src;
734 int stride = x->src.y_stride;
735 int stride_uv = x->block[16].src_stride;
736 #if CONFIG_TEMPORAL_DENOISING
737 if (cpi->oxcf.noise_sensitivity) {
738 const int uv_denoise = (cpi->oxcf.noise_sensitivity >= 2) ? 1 : 0;
740 cpi->denoiser.yv12_running_avg[LAST_FRAME].y_buffer + recon_yoffset;
741 stride = cpi->denoiser.yv12_running_avg[LAST_FRAME].y_stride;
744 cpi->denoiser.yv12_running_avg[LAST_FRAME].u_buffer +
747 cpi->denoiser.yv12_running_avg[LAST_FRAME].v_buffer +
749 stride_uv = cpi->denoiser.yv12_running_avg[LAST_FRAME].uv_stride;
753 dot_artifact_candidate =
754 check_dot_artifact_candidate(cpi, x, target_y, stride,
755 plane[LAST_FRAME][0], mb_row, mb_col, 0);
756 // If not found in Y channel, check UV channel.
757 if (!dot_artifact_candidate) {
758 dot_artifact_candidate =
759 check_dot_artifact_candidate(cpi, x, target_u, stride_uv,
760 plane[LAST_FRAME][1], mb_row, mb_col, 1);
761 if (!dot_artifact_candidate) {
762 dot_artifact_candidate =
763 check_dot_artifact_candidate(cpi, x, target_v, stride_uv,
764 plane[LAST_FRAME][2], mb_row, mb_col, 2);
769 #if CONFIG_MULTI_RES_ENCODING
770 // |parent_ref_valid| will be set here if potentially we can do mv resue for
771 // this higher resol (|cpi->oxcf.mr_encoder_id| > 0) frame.
772 // |parent_ref_valid| may be reset depending on |parent_ref_frame| for
773 // the current macroblock below.
774 parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
775 if (parent_ref_valid)
779 get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
780 &parent_mode, &parent_ref_mv, mb_row, mb_col);
782 /* TODO(jkoleszar): The references available (ref_frame_flags) to the
783 * lower res encoder should match those available to this encoder, but
784 * there seems to be a situation where this mismatch can happen in the
785 * case of frame dropping and temporal layers. For example,
786 * GOLD being disallowed in ref_frame_flags, but being returned as
789 * In this event, take the conservative approach of disabling the
790 * lower res info for this MB.
794 // Note availability for mv reuse is only based on last and golden.
795 if (parent_ref_frame == LAST_FRAME)
796 parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
797 else if (parent_ref_frame == GOLDEN_FRAME)
798 parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);
800 //assert(!parent_ref_frame || parent_ref_flag);
802 // If |parent_ref_frame| did not match either last or golden then
803 // shut off mv reuse.
804 if (parent_ref_frame && !parent_ref_flag)
805 parent_ref_valid = 0;
807 // Don't do mv reuse since we want to allow for another mode besides
808 // ZEROMV_LAST to remove dot artifact.
809 if (dot_artifact_candidate)
810 parent_ref_valid = 0;
814 // Check if current macroblock is in skin area.
816 const int y = x->src.y_buffer[7 * x->src.y_stride + 7];
817 const int cb = x->src.u_buffer[3 * x->src.uv_stride + 3];
818 const int cr = x->src.v_buffer[3 * x->src.uv_stride + 3];
820 if (!cpi->oxcf.screen_content_mode)
821 x->is_skin = is_skin_color(y, cb, cr);
823 #if CONFIG_TEMPORAL_DENOISING
824 if (cpi->oxcf.noise_sensitivity) {
825 // Under aggressive denoising mode, should we use skin map to reduce denoiser
826 // and ZEROMV bias? Will need to revisit the accuracy of this detection for
827 // very noisy input. For now keep this as is (i.e., don't turn it off).
828 // if (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive)
833 mode_mv = mode_mv_sb[sign_bias];
834 best_ref_mv.as_int = 0;
835 memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
836 memset(&best_mbmode, 0, sizeof(best_mbmode));
838 /* Setup search priorities */
839 #if CONFIG_MULTI_RES_ENCODING
840 if (parent_ref_valid && parent_ref_frame && dissim < 8)
842 ref_frame_map[0] = -1;
843 ref_frame_map[1] = parent_ref_frame;
844 ref_frame_map[2] = -1;
845 ref_frame_map[3] = -1;
848 get_reference_search_order(cpi, ref_frame_map);
850 /* Check to see if there is at least 1 valid reference frame that we need
851 * to calculate near_mvs.
853 if (ref_frame_map[1] > 0)
855 sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
856 x->e_mbd.mode_info_context,
861 cpi->common.ref_frame_sign_bias);
863 mode_mv = mode_mv_sb[sign_bias];
864 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
867 /* Count of the number of MBs tested so far this frame */
868 x->mbs_tested_so_far++;
870 *returnintra = INT_MAX;
873 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
875 /* If the frame has big static background and current MB is in low
876 * motion area, its mode decision is biased to ZEROMV mode.
877 * No adjustment if cpu_used is <= -12 (i.e., cpi->Speed >= 12).
878 * At such speed settings, ZEROMV is already heavily favored.
880 if (cpi->Speed < 12) {
881 calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
884 #if CONFIG_TEMPORAL_DENOISING
885 if (cpi->oxcf.noise_sensitivity) {
886 rd_adjustment = (int)(rd_adjustment *
887 cpi->denoiser.denoise_pars.pickmode_mv_bias / 100);
891 if (dot_artifact_candidate)
893 // Bias against ZEROMV_LAST mode.
898 /* if we encode a new mv this is important
899 * find the best new motion vector
901 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
904 int this_rd = INT_MAX;
905 int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
907 if (best_rd <= x->rd_threshes[mode_index])
910 if (this_ref_frame < 0)
913 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
915 /* everything but intra */
916 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
918 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
919 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
920 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
922 if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
924 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
925 mode_mv = mode_mv_sb[sign_bias];
926 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
929 #if CONFIG_MULTI_RES_ENCODING
930 if (parent_ref_valid)
932 if (vp8_mode_order[mode_index] == NEARESTMV &&
933 mode_mv[NEARESTMV].as_int ==0)
935 if (vp8_mode_order[mode_index] == NEARMV &&
936 mode_mv[NEARMV].as_int ==0)
939 if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV
940 && best_ref_mv.as_int==0)
942 else if(vp8_mode_order[mode_index] == NEWMV && dissim==0
943 && best_ref_mv.as_int==parent_ref_mv.as_int)
949 /* Check to see if the testing frequency for this mode is at its max
950 * If so then prevent it from being tested and increase the threshold
952 if (x->mode_test_hit_counts[mode_index] &&
953 (cpi->mode_check_freq[mode_index] > 1))
955 if (x->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
956 x->mode_test_hit_counts[mode_index]))
958 /* Increase the threshold for coding this mode to make it less
959 * likely to be chosen */
960 x->rd_thresh_mult[mode_index] += 4;
962 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
963 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
965 x->rd_threshes[mode_index] =
966 (cpi->rd_baseline_thresh[mode_index] >> 7) *
967 x->rd_thresh_mult[mode_index];
972 /* We have now reached the point where we are going to test the current
973 * mode so increment the counter for the number of times it has been
975 x->mode_test_hit_counts[mode_index] ++;
980 this_mode = vp8_mode_order[mode_index];
982 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
983 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
985 /* Work out the cost assosciated with selecting the reference frame */
987 x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
990 /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
991 * unless ARNR filtering is enabled in which case we want
992 * an unfiltered alternative */
993 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
995 if (this_mode != ZEROMV ||
996 x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
1003 /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
1004 distortion2 = best_rd_sse;
1005 pick_intra4x4mby_modes(x, &rate, &distortion2);
1007 if (distortion2 == INT_MAX)
1014 distortion2 = vpx_variance16x16(
1015 *(b->base_src), b->src_stride,
1016 x->e_mbd.predictor, 16, &sse);
1017 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
1019 if (this_rd < best_intra_rd)
1021 best_intra_rd = this_rd;
1022 *returnintra = distortion2;
1030 /* Split MV modes currently not supported when RD is not enabled. */
1037 vp8_build_intra_predictors_mby_s(xd,
1038 xd->dst.y_buffer - xd->dst.y_stride,
1039 xd->dst.y_buffer - 1,
1043 distortion2 = vpx_variance16x16
1044 (*(b->base_src), b->src_stride,
1045 x->e_mbd.predictor, 16, &sse);
1046 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
1047 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
1049 if (this_rd < best_intra_rd)
1051 best_intra_rd = this_rd;
1052 *returnintra = distortion2;
1062 int sadpb = x->sadperbit16;
1065 int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
1066 int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
1067 int col_max = (best_ref_mv.as_mv.col>>3)
1069 int row_max = (best_ref_mv.as_mv.row>>3)
1072 int tmp_col_min = x->mv_col_min;
1073 int tmp_col_max = x->mv_col_max;
1074 int tmp_row_min = x->mv_row_min;
1075 int tmp_row_max = x->mv_row_max;
1077 int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
1079 /* Further step/diamond searches as necessary */
1080 step_param = cpi->sf.first_step + speed_adjust;
1082 #if CONFIG_MULTI_RES_ENCODING
1083 /* If lower-res frame is not available for mv reuse (because of
1084 frame dropping or different temporal layer pattern), then higher
1085 resol encoder does motion search without any previous knowledge.
1086 Also, since last frame motion info is not stored, then we can not
1087 use improved_mv_pred. */
1088 if (cpi->oxcf.mr_encoder_id)
1089 sf_improved_mv_pred = 0;
1091 // Only use parent MV as predictor if this candidate reference frame
1092 // (|this_ref_frame|) is equal to |parent_ref_frame|.
1093 if (parent_ref_valid && (parent_ref_frame == this_ref_frame))
1095 /* Use parent MV as predictor. Adjust search range
1098 mvp.as_int = parent_ref_mv.as_int;
1099 mvp_full.as_mv.col = parent_ref_mv.as_mv.col>>3;
1100 mvp_full.as_mv.row = parent_ref_mv.as_mv.row>>3;
1102 if(dissim <=32) step_param += 3;
1103 else if(dissim <=128) step_param += 2;
1104 else step_param += 1;
1108 if(sf_improved_mv_pred)
1112 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
1116 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context,
1117 &mvp,x->e_mbd.mode_info_context->mbmi.ref_frame,
1118 cpi->common.ref_frame_sign_bias, &sr,
1122 /* adjust search range according to sr from mv prediction */
1126 mvp_full.as_mv.col = mvp.as_mv.col>>3;
1127 mvp_full.as_mv.row = mvp.as_mv.row>>3;
1130 mvp.as_int = best_ref_mv.as_int;
1131 mvp_full.as_mv.col = best_ref_mv.as_mv.col>>3;
1132 mvp_full.as_mv.row = best_ref_mv.as_mv.row>>3;
1136 #if CONFIG_MULTI_RES_ENCODING
1137 if (parent_ref_valid && (parent_ref_frame == this_ref_frame) &&
1139 MAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
1140 abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <= 4)
1142 d->bmi.mv.as_int = mvp_full.as_int;
1143 mode_mv[NEWMV].as_int = mvp_full.as_int;
1145 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
1147 &cpi->fn_ptr[BLOCK_16X16],
1153 /* Get intersection of UMV window and valid MV window to
1154 * reduce # of checks in diamond search. */
1155 if (x->mv_col_min < col_min )
1156 x->mv_col_min = col_min;
1157 if (x->mv_col_max > col_max )
1158 x->mv_col_max = col_max;
1159 if (x->mv_row_min < row_min )
1160 x->mv_row_min = row_min;
1161 if (x->mv_row_max > row_max )
1162 x->mv_row_max = row_max;
1164 further_steps = (cpi->Speed >= 8)?
1165 0: (cpi->sf.max_step_search_steps - 1 - step_param);
1167 if (cpi->sf.search_method == HEX)
1169 #if CONFIG_MULTI_RES_ENCODING
1170 /* TODO: In higher-res pick_inter_mode, step_param is used to
1171 * modify hex search range. Here, set step_param to 0 not to
1172 * change the behavior in lowest-resolution encoder.
1173 * Will improve it later.
1175 /* Set step_param to 0 to ensure large-range motion search
1176 * when mv reuse if not valid (i.e. |parent_ref_valid| = 0),
1177 * or if this candidate reference frame (|this_ref_frame|) is
1178 * not equal to |parent_ref_frame|.
1180 if (!parent_ref_valid || (parent_ref_frame != this_ref_frame))
1183 bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv,
1185 &cpi->fn_ptr[BLOCK_16X16],
1186 x->mvsadcost, x->mvcost, &best_ref_mv);
1187 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1191 bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full,
1192 &d->bmi.mv, step_param, sadpb, &num00,
1193 &cpi->fn_ptr[BLOCK_16X16],
1194 x->mvcost, &best_ref_mv);
1195 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1197 /* Further step/diamond searches as necessary */
1201 while (n < further_steps)
1210 cpi->diamond_search_sad(x, b, d, &mvp_full,
1214 &cpi->fn_ptr[BLOCK_16X16],
1215 x->mvcost, &best_ref_mv);
1216 if (thissme < bestsme)
1219 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1223 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
1229 x->mv_col_min = tmp_col_min;
1230 x->mv_col_max = tmp_col_max;
1231 x->mv_row_min = tmp_row_min;
1232 x->mv_row_max = tmp_row_max;
1234 if (bestsme < INT_MAX)
1235 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
1236 &best_ref_mv, x->errorperbit,
1237 &cpi->fn_ptr[BLOCK_16X16],
1242 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1245 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
1246 cpi->mb.mvcost, 128);
1252 if (mode_mv[this_mode].as_int == 0)
1257 /* Trap vectors that reach beyond the UMV borders
1258 * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
1259 * through to this point because of the lack of break statements
1260 * in the previous two cases.
1262 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
1263 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
1264 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
1265 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1268 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1269 x->e_mbd.mode_info_context->mbmi.mv.as_int =
1270 mode_mv[this_mode].as_int;
1271 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1279 #if CONFIG_TEMPORAL_DENOISING
1280 if (cpi->oxcf.noise_sensitivity)
1282 /* Store for later use by denoiser. */
1283 // Dont' denoise with GOLDEN OR ALTREF is they are old reference
1284 // frames (greater than MAX_GF_ARF_DENOISE_RANGE frames in past).
1285 int skip_old_reference = ((this_ref_frame != LAST_FRAME) &&
1286 (cpi->common.current_video_frame -
1287 cpi->current_ref_frames[this_ref_frame] >
1288 MAX_GF_ARF_DENOISE_RANGE)) ? 1 : 0;
1289 if (this_mode == ZEROMV && sse < zero_mv_sse &&
1290 !skip_old_reference)
1293 x->best_zeromv_reference_frame =
1294 x->e_mbd.mode_info_context->mbmi.ref_frame;
1297 // Store the best NEWMV in x for later use in the denoiser.
1298 if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
1299 sse < best_sse && !skip_old_reference)
1302 x->best_sse_inter_mode = NEWMV;
1303 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
1304 x->need_to_clamp_best_mvs =
1305 x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
1306 x->best_reference_frame =
1307 x->e_mbd.mode_info_context->mbmi.ref_frame;
1312 if (this_rd < best_rd || x->skip)
1314 /* Note index of best mode */
1315 best_mode_index = mode_index;
1317 *returnrate = rate2;
1318 *returndistortion = distortion2;
1321 memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1322 sizeof(MB_MODE_INFO));
1324 /* Testing this mode gave rise to an improvement in best error
1325 * score. Lower threshold a bit for next time
1327 x->rd_thresh_mult[mode_index] =
1328 (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
1329 x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
1330 x->rd_threshes[mode_index] =
1331 (cpi->rd_baseline_thresh[mode_index] >> 7) *
1332 x->rd_thresh_mult[mode_index];
1335 /* If the mode did not help improve the best error case then raise the
1336 * threshold for testing that mode next time around.
1340 x->rd_thresh_mult[mode_index] += 4;
1342 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1343 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1345 x->rd_threshes[mode_index] =
1346 (cpi->rd_baseline_thresh[mode_index] >> 7) *
1347 x->rd_thresh_mult[mode_index];
1354 /* Reduce the activation RD thresholds for the best choice mode */
1355 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
1357 int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 3);
1359 x->rd_thresh_mult[best_mode_index] =
1360 (x->rd_thresh_mult[best_mode_index]
1361 >= (MIN_THRESHMULT + best_adjustment)) ?
1362 x->rd_thresh_mult[best_mode_index] - best_adjustment :
1364 x->rd_threshes[best_mode_index] =
1365 (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
1366 x->rd_thresh_mult[best_mode_index];
1371 int this_rdbin = (*returndistortion >> 7);
1373 if (this_rdbin >= 1024)
1378 x->error_bins[this_rdbin] ++;
1381 #if CONFIG_TEMPORAL_DENOISING
1382 if (cpi->oxcf.noise_sensitivity)
1384 int block_index = mb_row * cpi->common.mb_cols + mb_col;
1387 if (x->best_sse_inter_mode == DC_PRED)
1389 /* No best MV found. */
1390 x->best_sse_inter_mode = best_mbmode.mode;
1391 x->best_sse_mv = best_mbmode.mv;
1392 x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
1393 x->best_reference_frame = best_mbmode.ref_frame;
1394 best_sse = best_rd_sse;
1396 // For non-skin blocks that have selected ZEROMV for this current frame,
1397 // and have been selecting ZEROMV_LAST (on the base layer frame) at
1398 // least |x~20| consecutive past frames in a row, label the block for
1399 // possible increase in denoising strength. We also condition this
1400 // labeling on there being significant denoising in the scene
1401 if (cpi->oxcf.noise_sensitivity == 4) {
1402 if (cpi->denoiser.nmse_source_diff >
1403 70 * cpi->denoiser.threshold_aggressive_mode / 100)
1406 if (cpi->mse_source_denoised > 1000)
1409 x->increase_denoising = 0;
1411 x->best_sse_inter_mode == ZEROMV &&
1412 (x->best_reference_frame == LAST_FRAME ||
1413 x->best_reference_frame == cpi->closest_reference_frame) &&
1414 cpi->consec_zero_last[block_index] >= 20 &&
1416 x->increase_denoising = 1;
1418 x->denoise_zeromv = 0;
1419 vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
1420 recon_yoffset, recon_uvoffset,
1421 &cpi->common.lf_info, mb_row, mb_col,
1424 // Reevaluate ZEROMV after denoising: for large noise content
1425 // (i.e., cpi->mse_source_denoised is above threshold), do this for all
1426 // blocks that did not pick ZEROMV as best mode but are using ZEROMV
1427 // for denoising. Otherwise, always re-evaluate for blocks that picked
1428 // INTRA mode as best mode.
1429 // Avoid blocks that have been biased against ZERO_LAST
1430 // (i.e., dot artifact candidate blocks).
1431 reevaluate = (best_mbmode.ref_frame == INTRA_FRAME) ||
1432 (best_mbmode.mode != ZEROMV &&
1433 x->denoise_zeromv &&
1434 cpi->mse_source_denoised > 2000);
1435 if (!dot_artifact_candidate &&
1437 x->best_zeromv_reference_frame != INTRA_FRAME)
1440 int this_ref_frame = x->best_zeromv_reference_frame;
1441 rd_adjustment = 100;
1442 rate2 = x->ref_frame_cost[this_ref_frame] +
1443 vp8_cost_mv_ref(ZEROMV, mdcounts);
1446 /* set up the proper prediction buffers for the frame */
1447 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
1448 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
1449 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
1450 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
1452 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1453 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1454 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1455 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1458 if (this_rd < best_rd)
1460 memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1461 sizeof(MB_MODE_INFO));
1468 if (cpi->is_src_frame_alt_ref &&
1469 (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
1471 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1472 x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
1473 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1474 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1475 x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
1476 (cpi->common.mb_no_coeff_skip);
1477 x->e_mbd.mode_info_context->mbmi.partitioning = 0;
1482 /* set to the best mb mode, this copy can be skip if x->skip since it
1483 * already has the right content */
1485 memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode,
1486 sizeof(MB_MODE_INFO));
1488 if (best_mbmode.mode <= B_PRED)
1490 /* set mode_info_context->mbmi.uv_mode */
1491 pick_intra_mbuv_mode(x);
1495 != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
1496 best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
1498 update_mvcount(x, &best_ref_mv);
1501 void vp8_pick_intra_mode(MACROBLOCK *x, int *rate_)
1503 int error4x4, error16x16 = INT_MAX;
1504 int rate, best_rate = 0, distortion, best_sse;
1505 MB_PREDICTION_MODE mode, best_mode = DC_PRED;
1508 BLOCK *b = &x->block[0];
1509 MACROBLOCKD *xd = &x->e_mbd;
1511 xd->mode_info_context->mbmi.ref_frame = INTRA_FRAME;
1513 pick_intra_mbuv_mode(x);
1515 for (mode = DC_PRED; mode <= TM_PRED; mode ++)
1517 xd->mode_info_context->mbmi.mode = mode;
1518 vp8_build_intra_predictors_mby_s(xd,
1519 xd->dst.y_buffer - xd->dst.y_stride,
1520 xd->dst.y_buffer - 1,
1524 distortion = vpx_variance16x16
1525 (*(b->base_src), b->src_stride, xd->predictor, 16, &sse);
1526 rate = x->mbmode_cost[xd->frame_type][mode];
1527 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1529 if (error16x16 > this_rd)
1531 error16x16 = this_rd;
1537 xd->mode_info_context->mbmi.mode = best_mode;
1539 error4x4 = pick_intra4x4mby_modes(x, &rate,
1541 if (error4x4 < error16x16)
1543 xd->mode_info_context->mbmi.mode = B_PRED;