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"
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/reconintra4x4.h"
24 #include "vp8/common/variance.h"
27 #include "vpx_mem/vpx_mem.h"
28 #if CONFIG_TEMPORAL_DENOISING
29 #include "denoising.h"
32 extern int VP8_UVSSE(MACROBLOCK *x);
35 extern unsigned int cnt_pm;
38 extern const int vp8_ref_frame_order[MAX_MODES];
39 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
41 extern int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]);
43 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
44 int_mv *bestmv, int_mv *ref_mv,
46 const vp8_variance_fn_ptr_t *vfp,
47 int *mvcost[2], int *distortion,
58 bestmv->as_mv.row <<= 3;
59 bestmv->as_mv.col <<= 3;
64 int vp8_get_inter_mbpred_error(MACROBLOCK *mb,
65 const vp8_variance_fn_ptr_t *vfp,
70 BLOCK *b = &mb->block[0];
71 BLOCKD *d = &mb->e_mbd.block[0];
72 unsigned char *what = (*(b->base_src) + b->src);
73 int what_stride = b->src_stride;
74 int pre_stride = mb->e_mbd.pre.y_stride;
75 unsigned char *in_what = mb->e_mbd.pre.y_buffer + d->offset ;
76 int in_what_stride = pre_stride;
77 int xoffset = this_mv.as_mv.col & 7;
78 int yoffset = this_mv.as_mv.row & 7;
80 in_what += (this_mv.as_mv.row >> 3) * pre_stride + (this_mv.as_mv.col >> 3);
82 if (xoffset | yoffset)
84 return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
88 return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
94 unsigned int vp8_get4x4sse_cs_c
96 const unsigned char *src_ptr,
98 const unsigned char *ref_ptr,
105 for (r = 0; r < 4; r++)
107 for (c = 0; c < 4; c++)
109 int diff = src_ptr[c] - ref_ptr[c];
110 distortion += diff * diff;
113 src_ptr += source_stride;
114 ref_ptr += recon_stride;
120 static int get_prediction_error(BLOCK *be, BLOCKD *b)
124 sptr = (*(be->base_src) + be->src);
127 return vp8_get4x4sse_cs(sptr, be->src_stride, dptr, 16);
131 static int pick_intra4x4block(
134 B_PREDICTION_MODE *best_mode,
135 const int *mode_costs,
141 BLOCKD *b = &x->e_mbd.block[ib];
142 BLOCK *be = &x->block[ib];
143 int dst_stride = x->e_mbd.dst.y_stride;
144 unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
145 B_PREDICTION_MODE mode;
146 int best_rd = INT_MAX;
150 unsigned char *Above = dst - dst_stride;
151 unsigned char *yleft = dst - 1;
152 unsigned char top_left = Above[-1];
154 for (mode = B_DC_PRED; mode <= B_HE_PRED; mode++)
158 rate = mode_costs[mode];
160 vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
161 b->predictor, 16, top_left);
162 distortion = get_prediction_error(be, b);
163 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
165 if (this_rd < best_rd)
168 *bestdistortion = distortion;
174 b->bmi.as_mode = *best_mode;
175 vp8_encode_intra4x4block(x, ib);
180 static int pick_intra4x4mby_modes
187 MACROBLOCKD *const xd = &mb->e_mbd;
189 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
192 const int *bmode_costs;
194 intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
196 bmode_costs = mb->inter_bmode_costs;
198 for (i = 0; i < 16; i++)
200 MODE_INFO *const mic = xd->mode_info_context;
201 const int mis = xd->mode_info_stride;
203 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
204 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
206 if (mb->e_mbd.frame_type == KEY_FRAME)
208 const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
209 const B_PREDICTION_MODE L = left_block_mode(mic, i);
211 bmode_costs = mb->bmode_costs[A][L];
215 pick_intra4x4block(mb, i, &best_mode, bmode_costs, &r, &d);
219 mic->bmi[i].as_mode = best_mode;
221 /* Break out case where we have already exceeded best so far value
224 if (distortion > *best_dist)
232 *best_dist = distortion;
233 error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
237 *best_dist = INT_MAX;
244 static void pick_intra_mbuv_mode(MACROBLOCK *mb)
247 MACROBLOCKD *x = &mb->e_mbd;
248 unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
249 unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
250 unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
251 unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
252 int uvsrc_stride = mb->block[16].src_stride;
253 unsigned char uleft_col[8];
254 unsigned char vleft_col[8];
255 unsigned char utop_left = uabove_row[-1];
256 unsigned char vtop_left = vabove_row[-1];
264 int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
265 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
268 for (i = 0; i < 8; i++)
270 uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
271 vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
274 if (!x->up_available && !x->left_available)
286 for (i = 0; i < 8; i++)
288 Uaverage += uabove_row[i];
289 Vaverage += vabove_row[i];
296 if (x->left_available)
298 for (i = 0; i < 8; i++)
300 Uaverage += uleft_col[i];
301 Vaverage += vleft_col[i];
308 expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
309 expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
313 for (i = 0; i < 8; i++)
315 for (j = 0; j < 8; j++)
318 int predu = uleft_col[i] + uabove_row[j] - utop_left;
319 int predv = vleft_col[i] + vabove_row[j] - vtop_left;
338 diff = u_p - expected_udc;
339 pred_error[DC_PRED] += diff * diff;
340 diff = v_p - expected_vdc;
341 pred_error[DC_PRED] += diff * diff;
344 diff = u_p - uabove_row[j];
345 pred_error[V_PRED] += diff * diff;
346 diff = v_p - vabove_row[j];
347 pred_error[V_PRED] += diff * diff;
350 diff = u_p - uleft_col[i];
351 pred_error[H_PRED] += diff * diff;
352 diff = v_p - vleft_col[i];
353 pred_error[H_PRED] += diff * diff;
357 pred_error[TM_PRED] += diff * diff;
359 pred_error[TM_PRED] += diff * diff;
364 usrc_ptr += uvsrc_stride;
365 vsrc_ptr += uvsrc_stride;
369 usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
370 vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
378 for (i = DC_PRED; i <= TM_PRED; i++)
380 if (best_error > pred_error[i])
382 best_error = pred_error[i];
383 best_mode = (MB_PREDICTION_MODE)i;
388 mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
392 static void update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
394 MACROBLOCKD *xd = &x->e_mbd;
395 /* Split MV modes currently not supported when RD is nopt enabled,
396 * therefore, only need to modify MVcount in NEWMV mode. */
397 if (xd->mode_info_context->mbmi.mode == NEWMV)
399 x->MVcount[0][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.row -
400 best_ref_mv->as_mv.row) >> 1)]++;
401 x->MVcount[1][mv_max+((xd->mode_info_context->mbmi.mv.as_mv.col -
402 best_ref_mv->as_mv.col) >> 1)]++;
407 #if CONFIG_MULTI_RES_ENCODING
409 void get_lower_res_motion_info(VP8_COMP *cpi, MACROBLOCKD *xd, int *dissim,
410 int *parent_ref_frame,
411 MB_PREDICTION_MODE *parent_mode,
412 int_mv *parent_ref_mv, int mb_row, int mb_col)
414 LOWER_RES_MB_INFO* store_mode_info
415 = ((LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info)->mb_info;
416 unsigned int parent_mb_index;
418 /* Consider different down_sampling_factor. */
420 /* TODO: Removed the loop that supports special down_sampling_factor
421 * such as 2, 4, 8. Will revisit it if needed.
422 * Should also try using a look-up table to see if it helps
424 int parent_mb_row, parent_mb_col;
426 parent_mb_row = mb_row*cpi->oxcf.mr_down_sampling_factor.den
427 /cpi->oxcf.mr_down_sampling_factor.num;
428 parent_mb_col = mb_col*cpi->oxcf.mr_down_sampling_factor.den
429 /cpi->oxcf.mr_down_sampling_factor.num;
430 parent_mb_index = parent_mb_row*cpi->mr_low_res_mb_cols + parent_mb_col;
433 /* Read lower-resolution mode & motion result from memory.*/
434 *parent_ref_frame = store_mode_info[parent_mb_index].ref_frame;
435 *parent_mode = store_mode_info[parent_mb_index].mode;
436 *dissim = store_mode_info[parent_mb_index].dissim;
438 /* For highest-resolution encoder, adjust dissim value. Lower its quality
439 * for good performance. */
440 if (cpi->oxcf.mr_encoder_id == (cpi->oxcf.mr_total_resolutions - 1))
443 if(*parent_ref_frame != INTRA_FRAME)
445 /* Consider different down_sampling_factor.
446 * The result can be rounded to be more precise, but it takes more time.
448 (*parent_ref_mv).as_mv.row = store_mode_info[parent_mb_index].mv.as_mv.row
449 *cpi->oxcf.mr_down_sampling_factor.num
450 /cpi->oxcf.mr_down_sampling_factor.den;
451 (*parent_ref_mv).as_mv.col = store_mode_info[parent_mb_index].mv.as_mv.col
452 *cpi->oxcf.mr_down_sampling_factor.num
453 /cpi->oxcf.mr_down_sampling_factor.den;
455 vp8_clamp_mv2(parent_ref_mv, xd);
460 static void check_for_encode_breakout(unsigned int sse, MACROBLOCK* x)
462 MACROBLOCKD *xd = &x->e_mbd;
464 unsigned int threshold = (xd->block[0].dequant[1]
465 * xd->block[0].dequant[1] >>4);
467 if(threshold < x->encode_breakout)
468 threshold = x->encode_breakout;
470 if (sse < threshold )
472 /* Check u and v to make sure skip is ok */
473 unsigned int sse2 = 0;
477 if (sse2 * 2 < x->encode_breakout)
484 static int evaluate_inter_mode(unsigned int* sse, int rate2, int* distortion2,
485 VP8_COMP *cpi, MACROBLOCK *x, int rd_adj)
487 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
488 int_mv mv = x->e_mbd.mode_info_context->mbmi.mv;
490 int denoise_aggressive = 0;
491 /* Exit early and don't compute the distortion if this macroblock
492 * is marked inactive. */
493 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
501 if((this_mode != NEWMV) ||
502 !(cpi->sf.half_pixel_search) || cpi->common.full_pixel==1)
503 *distortion2 = vp8_get_inter_mbpred_error(x,
504 &cpi->fn_ptr[BLOCK_16X16],
507 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, *distortion2);
509 #if CONFIG_TEMPORAL_DENOISING
510 if (cpi->oxcf.noise_sensitivity > 0) {
512 (cpi->denoiser.denoiser_mode == kDenoiserOnYUVAggressive) ? 1 : 0;
516 // Adjust rd for ZEROMV and LAST, if LAST is the closest reference frame.
517 if (this_mode == ZEROMV &&
518 x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME &&
519 (denoise_aggressive || cpi->closest_reference_frame == LAST_FRAME)) {
520 this_rd = ((int64_t)this_rd) * rd_adj / 100;
523 check_for_encode_breakout(*sse, x);
527 static void calculate_zeromv_rd_adjustment(VP8_COMP *cpi, MACROBLOCK *x,
530 MODE_INFO *mic = x->e_mbd.mode_info_context;
531 int_mv mv_l, mv_a, mv_al;
532 int local_motion_check = 0;
534 if (cpi->lf_zeromv_pct > 40)
540 if (mic->mbmi.ref_frame != INTRA_FRAME)
541 if( abs(mv_l.as_mv.row) < 8 && abs(mv_l.as_mv.col) < 8)
542 local_motion_check++;
545 mic -= x->e_mbd.mode_info_stride;
546 mv_al = mic->mbmi.mv;
548 if (mic->mbmi.ref_frame != INTRA_FRAME)
549 if( abs(mv_al.as_mv.row) < 8 && abs(mv_al.as_mv.col) < 8)
550 local_motion_check++;
556 if (mic->mbmi.ref_frame != INTRA_FRAME)
557 if( abs(mv_a.as_mv.row) < 8 && abs(mv_a.as_mv.col) < 8)
558 local_motion_check++;
560 if (((!x->e_mbd.mb_to_top_edge || !x->e_mbd.mb_to_left_edge)
561 && local_motion_check >0) || local_motion_check >2 )
563 else if (local_motion_check > 0)
568 void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
569 int recon_uvoffset, int *returnrate,
570 int *returndistortion, int *returnintra, int mb_row,
573 BLOCK *b = &x->block[0];
574 BLOCKD *d = &x->e_mbd.block[0];
575 MACROBLOCKD *xd = &x->e_mbd;
576 MB_MODE_INFO best_mbmode;
578 int_mv best_ref_mv_sb[2];
579 int_mv mode_mv_sb[2][MB_MODE_COUNT];
582 MB_PREDICTION_MODE this_mode;
585 int best_rd = INT_MAX;
586 int rd_adjustment = 100;
587 int best_intra_rd = INT_MAX;
592 int bestsme = INT_MAX;
593 int best_mode_index = 0;
594 unsigned int sse = UINT_MAX, best_rd_sse = UINT_MAX;
595 #if CONFIG_TEMPORAL_DENOISING
596 unsigned int zero_mv_sse = UINT_MAX, best_sse = UINT_MAX;
599 int sf_improved_mv_pred = cpi->sf.improved_mv_pred;
602 int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
604 /* search range got from mv_pred(). It uses step_param levels. (0-7) */
607 unsigned char *plane[4][3];
608 int ref_frame_map[4];
611 #if CONFIG_MULTI_RES_ENCODING
612 int dissim = INT_MAX;
613 int parent_ref_frame = 0;
614 int parent_ref_valid = cpi->oxcf.mr_encoder_id && cpi->mr_low_res_mv_avail;
615 int_mv parent_ref_mv;
616 MB_PREDICTION_MODE parent_mode = 0;
618 if (parent_ref_valid)
622 get_lower_res_motion_info(cpi, xd, &dissim, &parent_ref_frame,
623 &parent_mode, &parent_ref_mv, mb_row, mb_col);
625 /* TODO(jkoleszar): The references available (ref_frame_flags) to the
626 * lower res encoder should match those available to this encoder, but
627 * there seems to be a situation where this mismatch can happen in the
628 * case of frame dropping and temporal layers. For example,
629 * GOLD being disallowed in ref_frame_flags, but being returned as
632 * In this event, take the conservative approach of disabling the
633 * lower res info for this MB.
636 if (parent_ref_frame == LAST_FRAME)
637 parent_ref_flag = (cpi->ref_frame_flags & VP8_LAST_FRAME);
638 else if (parent_ref_frame == GOLDEN_FRAME)
639 parent_ref_flag = (cpi->ref_frame_flags & VP8_GOLD_FRAME);
640 else if (parent_ref_frame == ALTREF_FRAME)
641 parent_ref_flag = (cpi->ref_frame_flags & VP8_ALTR_FRAME);
643 //assert(!parent_ref_frame || parent_ref_flag);
644 if (parent_ref_frame && !parent_ref_flag)
645 parent_ref_valid = 0;
649 mode_mv = mode_mv_sb[sign_bias];
650 best_ref_mv.as_int = 0;
651 vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
652 vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
654 /* Setup search priorities */
655 #if CONFIG_MULTI_RES_ENCODING
656 if (parent_ref_valid && parent_ref_frame && dissim < 8)
658 ref_frame_map[0] = -1;
659 ref_frame_map[1] = parent_ref_frame;
660 ref_frame_map[2] = -1;
661 ref_frame_map[3] = -1;
664 get_reference_search_order(cpi, ref_frame_map);
666 /* Check to see if there is at least 1 valid reference frame that we need
667 * to calculate near_mvs.
669 if (ref_frame_map[1] > 0)
671 sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
672 x->e_mbd.mode_info_context,
677 cpi->common.ref_frame_sign_bias);
679 mode_mv = mode_mv_sb[sign_bias];
680 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
683 get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
685 /* Count of the number of MBs tested so far this frame */
686 x->mbs_tested_so_far++;
688 *returnintra = INT_MAX;
691 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
693 /* If the frame has big static background and current MB is in low
694 * motion area, its mode decision is biased to ZEROMV mode.
696 calculate_zeromv_rd_adjustment(cpi, x, &rd_adjustment);
698 #if CONFIG_TEMPORAL_DENOISING
699 if (cpi->oxcf.noise_sensitivity) {
700 rd_adjustment = (int)(rd_adjustment *
701 cpi->denoiser.denoise_pars.pickmode_mv_bias / 100);
705 /* if we encode a new mv this is important
706 * find the best new motion vector
708 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
711 int this_rd = INT_MAX;
712 int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
714 if (best_rd <= x->rd_threshes[mode_index])
717 if (this_ref_frame < 0)
720 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
722 /* everything but intra */
723 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
725 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
726 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
727 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
729 if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
731 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
732 mode_mv = mode_mv_sb[sign_bias];
733 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
736 #if CONFIG_MULTI_RES_ENCODING
737 if (parent_ref_valid)
739 if (vp8_mode_order[mode_index] == NEARESTMV &&
740 mode_mv[NEARESTMV].as_int ==0)
742 if (vp8_mode_order[mode_index] == NEARMV &&
743 mode_mv[NEARMV].as_int ==0)
746 if (vp8_mode_order[mode_index] == NEWMV && parent_mode == ZEROMV
747 && best_ref_mv.as_int==0)
749 else if(vp8_mode_order[mode_index] == NEWMV && dissim==0
750 && best_ref_mv.as_int==parent_ref_mv.as_int)
756 /* Check to see if the testing frequency for this mode is at its max
757 * If so then prevent it from being tested and increase the threshold
759 if (x->mode_test_hit_counts[mode_index] &&
760 (cpi->mode_check_freq[mode_index] > 1))
762 if (x->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] *
763 x->mode_test_hit_counts[mode_index]))
765 /* Increase the threshold for coding this mode to make it less
766 * likely to be chosen */
767 x->rd_thresh_mult[mode_index] += 4;
769 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
770 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
772 x->rd_threshes[mode_index] =
773 (cpi->rd_baseline_thresh[mode_index] >> 7) *
774 x->rd_thresh_mult[mode_index];
779 /* We have now reached the point where we are going to test the current
780 * mode so increment the counter for the number of times it has been
782 x->mode_test_hit_counts[mode_index] ++;
787 this_mode = vp8_mode_order[mode_index];
789 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
790 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
792 /* Work out the cost assosciated with selecting the reference frame */
794 x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
797 /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
798 * unless ARNR filtering is enabled in which case we want
799 * an unfiltered alternative */
800 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
802 if (this_mode != ZEROMV ||
803 x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
810 /* Pass best so far to pick_intra4x4mby_modes to use as breakout */
811 distortion2 = best_rd_sse;
812 pick_intra4x4mby_modes(x, &rate, &distortion2);
814 if (distortion2 == INT_MAX)
821 distortion2 = vp8_variance16x16(
822 *(b->base_src), b->src_stride,
823 x->e_mbd.predictor, 16, &sse);
824 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
826 if (this_rd < best_intra_rd)
828 best_intra_rd = this_rd;
829 *returnintra = distortion2;
837 /* Split MV modes currently not supported when RD is not enabled. */
844 vp8_build_intra_predictors_mby_s(xd,
845 xd->dst.y_buffer - xd->dst.y_stride,
846 xd->dst.y_buffer - 1,
850 distortion2 = vp8_variance16x16
851 (*(b->base_src), b->src_stride,
852 x->e_mbd.predictor, 16, &sse);
853 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
854 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
856 if (this_rd < best_intra_rd)
858 best_intra_rd = this_rd;
859 *returnintra = distortion2;
869 int sadpb = x->sadperbit16;
872 int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
873 int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
874 int col_max = (best_ref_mv.as_mv.col>>3)
876 int row_max = (best_ref_mv.as_mv.row>>3)
879 int tmp_col_min = x->mv_col_min;
880 int tmp_col_max = x->mv_col_max;
881 int tmp_row_min = x->mv_row_min;
882 int tmp_row_max = x->mv_row_max;
884 int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
886 /* Further step/diamond searches as necessary */
887 step_param = cpi->sf.first_step + speed_adjust;
889 #if CONFIG_MULTI_RES_ENCODING
890 /* If lower-res drops this frame, then higher-res encoder does
891 motion search without any previous knowledge. Also, since
892 last frame motion info is not stored, then we can not
893 use improved_mv_pred. */
894 if (cpi->oxcf.mr_encoder_id && !parent_ref_valid)
895 sf_improved_mv_pred = 0;
897 if (parent_ref_valid && parent_ref_frame)
899 /* Use parent MV as predictor. Adjust search range
902 mvp.as_int = parent_ref_mv.as_int;
903 mvp_full.as_mv.col = parent_ref_mv.as_mv.col>>3;
904 mvp_full.as_mv.row = parent_ref_mv.as_mv.row>>3;
906 if(dissim <=32) step_param += 3;
907 else if(dissim <=128) step_param += 2;
908 else step_param += 1;
912 if(sf_improved_mv_pred)
916 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
920 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context,
921 &mvp,x->e_mbd.mode_info_context->mbmi.ref_frame,
922 cpi->common.ref_frame_sign_bias, &sr,
926 /* adjust search range according to sr from mv prediction */
930 mvp_full.as_mv.col = mvp.as_mv.col>>3;
931 mvp_full.as_mv.row = mvp.as_mv.row>>3;
934 mvp.as_int = best_ref_mv.as_int;
935 mvp_full.as_mv.col = best_ref_mv.as_mv.col>>3;
936 mvp_full.as_mv.row = best_ref_mv.as_mv.row>>3;
940 #if CONFIG_MULTI_RES_ENCODING
941 if (parent_ref_valid && parent_ref_frame && dissim <= 2 &&
942 MAX(abs(best_ref_mv.as_mv.row - parent_ref_mv.as_mv.row),
943 abs(best_ref_mv.as_mv.col - parent_ref_mv.as_mv.col)) <= 4)
945 d->bmi.mv.as_int = mvp_full.as_int;
946 mode_mv[NEWMV].as_int = mvp_full.as_int;
948 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
950 &cpi->fn_ptr[BLOCK_16X16],
956 /* Get intersection of UMV window and valid MV window to
957 * reduce # of checks in diamond search. */
958 if (x->mv_col_min < col_min )
959 x->mv_col_min = col_min;
960 if (x->mv_col_max > col_max )
961 x->mv_col_max = col_max;
962 if (x->mv_row_min < row_min )
963 x->mv_row_min = row_min;
964 if (x->mv_row_max > row_max )
965 x->mv_row_max = row_max;
967 further_steps = (cpi->Speed >= 8)?
968 0: (cpi->sf.max_step_search_steps - 1 - step_param);
970 if (cpi->sf.search_method == HEX)
972 #if CONFIG_MULTI_RES_ENCODING
973 /* TODO: In higher-res pick_inter_mode, step_param is used to
974 * modify hex search range. Here, set step_param to 0 not to
975 * change the behavior in lowest-resolution encoder.
976 * Will improve it later.
978 /* Set step_param to 0 to ensure large-range motion search
979 when encoder drops this frame at lower-resolution.
981 if (!parent_ref_valid)
984 bestsme = vp8_hex_search(x, b, d, &mvp_full, &d->bmi.mv,
986 &cpi->fn_ptr[BLOCK_16X16],
987 x->mvsadcost, x->mvcost, &best_ref_mv);
988 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
992 bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full,
993 &d->bmi.mv, step_param, sadpb, &num00,
994 &cpi->fn_ptr[BLOCK_16X16],
995 x->mvcost, &best_ref_mv);
996 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
998 /* Further step/diamond searches as necessary */
1002 while (n < further_steps)
1011 cpi->diamond_search_sad(x, b, d, &mvp_full,
1015 &cpi->fn_ptr[BLOCK_16X16],
1016 x->mvcost, &best_ref_mv);
1017 if (thissme < bestsme)
1020 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1024 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
1030 x->mv_col_min = tmp_col_min;
1031 x->mv_col_max = tmp_col_max;
1032 x->mv_row_min = tmp_row_min;
1033 x->mv_row_max = tmp_row_max;
1035 if (bestsme < INT_MAX)
1036 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv,
1037 &best_ref_mv, x->errorperbit,
1038 &cpi->fn_ptr[BLOCK_16X16],
1043 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
1046 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv,
1047 cpi->mb.mvcost, 128);
1053 if (mode_mv[this_mode].as_int == 0)
1058 /* Trap vectors that reach beyond the UMV borders
1059 * Note that ALL New MV, Nearest MV Near MV and Zero MV code drops
1060 * through to this point because of the lack of break statements
1061 * in the previous two cases.
1063 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) ||
1064 ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
1065 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) ||
1066 ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1069 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1070 x->e_mbd.mode_info_context->mbmi.mv.as_int =
1071 mode_mv[this_mode].as_int;
1072 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1080 #if CONFIG_TEMPORAL_DENOISING
1081 if (cpi->oxcf.noise_sensitivity)
1084 /* Store for later use by denoiser. */
1085 // Dont' denoise with GOLDEN OR ALTREF is they are old reference
1086 // frames (greater than MAX_GF_ARF_DENOISE_RANGE frames in past).
1087 int skip_old_reference = ((this_ref_frame != LAST_FRAME) &&
1088 (cpi->common.current_video_frame -
1089 cpi->current_ref_frames[this_ref_frame] >
1090 MAX_GF_ARF_DENOISE_RANGE)) ? 1 : 0;
1091 if (this_mode == ZEROMV && sse < zero_mv_sse &&
1092 !skip_old_reference)
1095 x->best_zeromv_reference_frame =
1096 x->e_mbd.mode_info_context->mbmi.ref_frame;
1099 /* Store the best NEWMV in x for later use in the denoiser. */
1100 if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
1101 sse < best_sse && !skip_old_reference)
1104 x->best_sse_inter_mode = NEWMV;
1105 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
1106 x->need_to_clamp_best_mvs =
1107 x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
1108 x->best_reference_frame =
1109 x->e_mbd.mode_info_context->mbmi.ref_frame;
1114 if (this_rd < best_rd || x->skip)
1116 /* Note index of best mode */
1117 best_mode_index = mode_index;
1119 *returnrate = rate2;
1120 *returndistortion = distortion2;
1123 vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1124 sizeof(MB_MODE_INFO));
1126 /* Testing this mode gave rise to an improvement in best error
1127 * score. Lower threshold a bit for next time
1129 x->rd_thresh_mult[mode_index] =
1130 (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
1131 x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
1132 x->rd_threshes[mode_index] =
1133 (cpi->rd_baseline_thresh[mode_index] >> 7) *
1134 x->rd_thresh_mult[mode_index];
1137 /* If the mode did not help improve the best error case then raise the
1138 * threshold for testing that mode next time around.
1142 x->rd_thresh_mult[mode_index] += 4;
1144 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1145 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1147 x->rd_threshes[mode_index] =
1148 (cpi->rd_baseline_thresh[mode_index] >> 7) *
1149 x->rd_thresh_mult[mode_index];
1156 /* Reduce the activation RD thresholds for the best choice mode */
1157 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
1159 int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 3);
1161 x->rd_thresh_mult[best_mode_index] =
1162 (x->rd_thresh_mult[best_mode_index]
1163 >= (MIN_THRESHMULT + best_adjustment)) ?
1164 x->rd_thresh_mult[best_mode_index] - best_adjustment :
1166 x->rd_threshes[best_mode_index] =
1167 (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
1168 x->rd_thresh_mult[best_mode_index];
1173 int this_rdbin = (*returndistortion >> 7);
1175 if (this_rdbin >= 1024)
1180 x->error_bins[this_rdbin] ++;
1183 #if CONFIG_TEMPORAL_DENOISING
1184 if (cpi->oxcf.noise_sensitivity)
1186 int block_index = mb_row * cpi->common.mb_cols + mb_col;
1187 if (x->best_sse_inter_mode == DC_PRED)
1189 /* No best MV found. */
1190 x->best_sse_inter_mode = best_mbmode.mode;
1191 x->best_sse_mv = best_mbmode.mv;
1192 x->need_to_clamp_best_mvs = best_mbmode.need_to_clamp_mvs;
1193 x->best_reference_frame = best_mbmode.ref_frame;
1194 best_sse = best_rd_sse;
1196 x->increase_denoising = 0;
1197 vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
1198 recon_yoffset, recon_uvoffset,
1199 &cpi->common.lf_info, mb_row, mb_col,
1202 /* Reevaluate ZEROMV after denoising. */
1203 if (best_mbmode.ref_frame == INTRA_FRAME &&
1204 x->best_zeromv_reference_frame != INTRA_FRAME)
1207 int this_ref_frame = x->best_zeromv_reference_frame;
1208 rate2 = x->ref_frame_cost[this_ref_frame] +
1209 vp8_cost_mv_ref(ZEROMV, mdcounts);
1212 /* set up the proper prediction buffers for the frame */
1213 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
1214 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
1215 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
1216 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
1218 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1219 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1220 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1221 this_rd = evaluate_inter_mode(&sse, rate2, &distortion2, cpi, x,
1224 if (this_rd < best_rd)
1226 vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi,
1227 sizeof(MB_MODE_INFO));
1234 if (cpi->is_src_frame_alt_ref &&
1235 (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
1237 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
1238 x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
1239 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
1240 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1241 x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
1242 (cpi->common.mb_no_coeff_skip);
1243 x->e_mbd.mode_info_context->mbmi.partitioning = 0;
1248 /* set to the best mb mode, this copy can be skip if x->skip since it
1249 * already has the right content */
1251 vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode,
1252 sizeof(MB_MODE_INFO));
1254 if (best_mbmode.mode <= B_PRED)
1256 /* set mode_info_context->mbmi.uv_mode */
1257 pick_intra_mbuv_mode(x);
1261 != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
1262 best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
1264 update_mvcount(x, &best_ref_mv);
1268 void vp8_pick_intra_mode(MACROBLOCK *x, int *rate_)
1270 int error4x4, error16x16 = INT_MAX;
1271 int rate, best_rate = 0, distortion, best_sse;
1272 MB_PREDICTION_MODE mode, best_mode = DC_PRED;
1275 BLOCK *b = &x->block[0];
1276 MACROBLOCKD *xd = &x->e_mbd;
1278 xd->mode_info_context->mbmi.ref_frame = INTRA_FRAME;
1280 pick_intra_mbuv_mode(x);
1282 for (mode = DC_PRED; mode <= TM_PRED; mode ++)
1284 xd->mode_info_context->mbmi.mode = mode;
1285 vp8_build_intra_predictors_mby_s(xd,
1286 xd->dst.y_buffer - xd->dst.y_stride,
1287 xd->dst.y_buffer - 1,
1291 distortion = vp8_variance16x16
1292 (*(b->base_src), b->src_stride, xd->predictor, 16, &sse);
1293 rate = x->mbmode_cost[xd->frame_type][mode];
1294 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1296 if (error16x16 > this_rd)
1298 error16x16 = this_rd;
1304 xd->mode_info_context->mbmi.mode = best_mode;
1306 error4x4 = pick_intra4x4mby_modes(x, &rate,
1308 if (error4x4 < error16x16)
1310 xd->mode_info_context->mbmi.mode = B_PRED;