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_ports/config.h"
15 #include "modecosts.h"
16 #include "encodeintra.h"
17 #include "vp8/common/entropymode.h"
18 #include "pickinter.h"
19 #include "vp8/common/findnearmv.h"
21 #include "vp8/common/reconinter.h"
22 #include "vp8/common/reconintra.h"
23 #include "vp8/common/reconintra4x4.h"
24 #include "vp8/common/g_common.h"
28 #include "vpx_mem/vpx_mem.h"
30 #if CONFIG_RUNTIME_CPU_DETECT
31 #define IF_RTCD(x) (x)
33 #define IF_RTCD(x) NULL
36 extern int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd);
39 extern unsigned int cnt_pm;
42 extern const MV_REFERENCE_FRAME vp8_ref_frame_order[MAX_MODES];
43 extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES];
46 extern unsigned int (*vp8_get16x16pred_error)(unsigned char *src_ptr, int src_stride, unsigned char *ref_ptr, int ref_stride);
47 extern unsigned int (*vp8_get4x4sse_cs)(unsigned char *src_ptr, int source_stride, unsigned char *ref_ptr, int recon_stride);
48 extern int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *best_ref_mv, int best_rd, int *, int *, int *, int, int *mvcost[2], int, int fullpixel);
49 extern int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]);
50 extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv);
53 int vp8_skip_fractional_mv_step(MACROBLOCK *mb, BLOCK *b, BLOCKD *d,
54 int_mv *bestmv, int_mv *ref_mv,
56 const vp8_variance_fn_ptr_t *vfp,
57 int *mvcost[2], int *distortion,
68 bestmv->as_mv.row <<= 3;
69 bestmv->as_mv.col <<= 3;
74 static int get_inter_mbpred_error(MACROBLOCK *mb, const vp8_variance_fn_ptr_t *vfp, unsigned int *sse)
77 BLOCK *b = &mb->block[0];
78 BLOCKD *d = &mb->e_mbd.block[0];
79 unsigned char *what = (*(b->base_src) + b->src);
80 int what_stride = b->src_stride;
81 unsigned char *in_what = *(d->base_pre) + d->pre ;
82 int in_what_stride = d->pre_stride;
83 int xoffset = d->bmi.mv.as_mv.col & 7;
84 int yoffset = d->bmi.mv.as_mv.row & 7;
86 in_what += (d->bmi.mv.as_mv.row >> 3) * d->pre_stride + (d->bmi.mv.as_mv.col >> 3);
88 if (xoffset | yoffset)
90 return vfp->svf(in_what, in_what_stride, xoffset, yoffset, what, what_stride, sse);
94 return vfp->vf(what, what_stride, in_what, in_what_stride, sse);
99 unsigned int vp8_get16x16pred_error_c
101 const unsigned char *src_ptr,
103 const unsigned char *ref_ptr,
108 unsigned pred_error = 0;
112 for (i = 0; i < 16; i++)
116 for (j = 0; j < 16; j++)
118 diff = src_ptr[j] - ref_ptr[j];
120 pred_error += diff * diff;
123 src_ptr += src_stride;
124 ref_ptr += ref_stride;
127 pred_error -= sum * sum / 256;
132 unsigned int vp8_get4x4sse_cs_c
134 const unsigned char *src_ptr,
136 const unsigned char *ref_ptr,
144 for (r = 0; r < 4; r++)
146 for (c = 0; c < 4; c++)
148 int diff = src_ptr[c] - ref_ptr[c];
149 distortion += diff * diff;
152 src_ptr += source_stride;
153 ref_ptr += recon_stride;
159 static int get_prediction_error(BLOCK *be, BLOCKD *b, const vp8_variance_rtcd_vtable_t *rtcd)
163 sptr = (*(be->base_src) + be->src);
166 return VARIANCE_INVOKE(rtcd, get4x4sse_cs)(sptr, be->src_stride, dptr, 16, 0x7fffffff);
170 static int pick_intra4x4block(
171 const VP8_ENCODER_RTCD *rtcd,
175 B_PREDICTION_MODE *best_mode,
176 B_PREDICTION_MODE above,
177 B_PREDICTION_MODE left,
182 B_PREDICTION_MODE mode;
183 int best_rd = INT_MAX; // 1<<30
186 unsigned int *mode_costs;
188 if (x->e_mbd.frame_type == KEY_FRAME)
190 mode_costs = x->bmode_costs[above][left];
194 mode_costs = x->inter_bmode_costs;
197 for (mode = B_DC_PRED; mode <= B_HE_PRED /*B_HU_PRED*/; mode++)
201 rate = mode_costs[mode];
202 RECON_INVOKE(&rtcd->common->recon, intra4x4_predict)
203 (b, mode, b->predictor);
204 distortion = get_prediction_error(be, b, &rtcd->variance);
205 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
207 if (this_rd < best_rd)
210 *bestdistortion = distortion;
216 b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
217 vp8_encode_intra4x4block(rtcd, x, be, b, b->bmi.mode);
223 int vp8_pick_intra4x4mby_modes
225 const VP8_ENCODER_RTCD *rtcd,
231 MACROBLOCKD *const xd = &mb->e_mbd;
233 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
237 vp8_intra_prediction_down_copy(xd);
239 for (i = 0; i < 16; i++)
241 MODE_INFO *const mic = xd->mode_info_context;
242 const int mis = xd->mode_info_stride;
243 const B_PREDICTION_MODE A = vp8_above_bmi(mic, i, mis)->mode;
244 const B_PREDICTION_MODE L = vp8_left_bmi(mic, i)->mode;
245 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
246 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(d);
248 pick_intra4x4block(rtcd, mb, mb->block + i, xd->block + i,
249 &best_mode, A, L, &r, &d);
254 mic->bmi[i].mode = xd->block[i].bmi.mode = best_mode;
256 // Break out case where we have already exceeded best so far value
257 // that was passed in
258 if (distortion > *best_dist)
266 *best_dist = distortion;
267 error = RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
271 *best_dist = INT_MAX;
275 for (i = 0; i < 16; i++)
276 xd->block[i].bmi.mv.as_int = 0;
281 void vp8_pick_intra_mbuv_mode(MACROBLOCK *mb)
284 MACROBLOCKD *x = &mb->e_mbd;
285 unsigned char *uabove_row = x->dst.u_buffer - x->dst.uv_stride;
286 unsigned char *vabove_row = x->dst.v_buffer - x->dst.uv_stride;
287 unsigned char *usrc_ptr = (mb->block[16].src + *mb->block[16].base_src);
288 unsigned char *vsrc_ptr = (mb->block[20].src + *mb->block[20].base_src);
289 int uvsrc_stride = mb->block[16].src_stride;
290 unsigned char uleft_col[8];
291 unsigned char vleft_col[8];
292 unsigned char utop_left = uabove_row[-1];
293 unsigned char vtop_left = vabove_row[-1];
301 int pred_error[4] = {0, 0, 0, 0}, best_error = INT_MAX;
302 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
305 for (i = 0; i < 8; i++)
307 uleft_col[i] = x->dst.u_buffer [i* x->dst.uv_stride -1];
308 vleft_col[i] = x->dst.v_buffer [i* x->dst.uv_stride -1];
311 if (!x->up_available && !x->left_available)
323 for (i = 0; i < 8; i++)
325 Uaverage += uabove_row[i];
326 Vaverage += vabove_row[i];
333 if (x->left_available)
335 for (i = 0; i < 8; i++)
337 Uaverage += uleft_col[i];
338 Vaverage += vleft_col[i];
345 expected_udc = (Uaverage + (1 << (shift - 1))) >> shift;
346 expected_vdc = (Vaverage + (1 << (shift - 1))) >> shift;
350 for (i = 0; i < 8; i++)
352 for (j = 0; j < 8; j++)
355 int predu = uleft_col[i] + uabove_row[j] - utop_left;
356 int predv = vleft_col[i] + vabove_row[j] - vtop_left;
375 diff = u_p - expected_udc;
376 pred_error[DC_PRED] += diff * diff;
377 diff = v_p - expected_vdc;
378 pred_error[DC_PRED] += diff * diff;
381 diff = u_p - uabove_row[j];
382 pred_error[V_PRED] += diff * diff;
383 diff = v_p - vabove_row[j];
384 pred_error[V_PRED] += diff * diff;
387 diff = u_p - uleft_col[i];
388 pred_error[H_PRED] += diff * diff;
389 diff = v_p - vleft_col[i];
390 pred_error[H_PRED] += diff * diff;
394 pred_error[TM_PRED] += diff * diff;
396 pred_error[TM_PRED] += diff * diff;
401 usrc_ptr += uvsrc_stride;
402 vsrc_ptr += uvsrc_stride;
406 usrc_ptr = (mb->block[18].src + *mb->block[18].base_src);
407 vsrc_ptr = (mb->block[22].src + *mb->block[22].base_src);
415 for (i = DC_PRED; i <= TM_PRED; i++)
417 if (best_error > pred_error[i])
419 best_error = pred_error[i];
420 best_mode = (MB_PREDICTION_MODE)i;
425 mb->e_mbd.mode_info_context->mbmi.uv_mode = best_mode;
429 void vp8_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
431 BLOCK *b = &x->block[0];
432 BLOCKD *d = &x->e_mbd.block[0];
433 MACROBLOCKD *xd = &x->e_mbd;
434 B_MODE_INFO best_bmodes[16];
435 MB_MODE_INFO best_mbmode;
436 PARTITION_INFO best_partition;
438 int_mv mode_mv[MB_MODE_COUNT];
439 MB_PREDICTION_MODE this_mode;
443 int best_rd = INT_MAX; // 1 << 30;
444 int best_intra_rd = INT_MAX;
446 int ref_frame_cost[MAX_REF_FRAMES];
451 //int all_rds[MAX_MODES]; // Experimental debug code.
452 int best_mode_index = 0;
453 unsigned int sse = INT_MAX;
456 int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
458 int sr=0; //search range got from mv_pred(). It uses step_param levels. (0-7)
460 int_mv nearest_mv[4];
462 int_mv frame_best_ref_mv[4];
464 unsigned char *y_buffer[4];
465 unsigned char *u_buffer[4];
466 unsigned char *v_buffer[4];
468 int skip_mode[4] = {0, 0, 0, 0};
470 int have_subp_search = cpi->sf.half_pixel_search; /* In real-time mode, when Speed >= 15, no sub-pixel search. */
472 vpx_memset(mode_mv, 0, sizeof(mode_mv));
473 vpx_memset(nearest_mv, 0, sizeof(nearest_mv));
474 vpx_memset(near_mv, 0, sizeof(near_mv));
475 vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
478 // set up all the refframe dependent pointers.
479 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
481 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
483 vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[LAST_FRAME], &near_mv[LAST_FRAME],
484 &frame_best_ref_mv[LAST_FRAME], MDCounts[LAST_FRAME], LAST_FRAME, cpi->common.ref_frame_sign_bias);
486 y_buffer[LAST_FRAME] = lst_yv12->y_buffer + recon_yoffset;
487 u_buffer[LAST_FRAME] = lst_yv12->u_buffer + recon_uvoffset;
488 v_buffer[LAST_FRAME] = lst_yv12->v_buffer + recon_uvoffset;
491 skip_mode[LAST_FRAME] = 1;
493 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
495 YV12_BUFFER_CONFIG *gld_yv12 = &cpi->common.yv12_fb[cpi->common.gld_fb_idx];
497 vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[GOLDEN_FRAME], &near_mv[GOLDEN_FRAME],
498 &frame_best_ref_mv[GOLDEN_FRAME], MDCounts[GOLDEN_FRAME], GOLDEN_FRAME, cpi->common.ref_frame_sign_bias);
500 y_buffer[GOLDEN_FRAME] = gld_yv12->y_buffer + recon_yoffset;
501 u_buffer[GOLDEN_FRAME] = gld_yv12->u_buffer + recon_uvoffset;
502 v_buffer[GOLDEN_FRAME] = gld_yv12->v_buffer + recon_uvoffset;
505 skip_mode[GOLDEN_FRAME] = 1;
507 if (cpi->ref_frame_flags & VP8_ALT_FLAG && cpi->source_alt_ref_active)
509 YV12_BUFFER_CONFIG *alt_yv12 = &cpi->common.yv12_fb[cpi->common.alt_fb_idx];
511 vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &nearest_mv[ALTREF_FRAME], &near_mv[ALTREF_FRAME],
512 &frame_best_ref_mv[ALTREF_FRAME], MDCounts[ALTREF_FRAME], ALTREF_FRAME, cpi->common.ref_frame_sign_bias);
514 y_buffer[ALTREF_FRAME] = alt_yv12->y_buffer + recon_yoffset;
515 u_buffer[ALTREF_FRAME] = alt_yv12->u_buffer + recon_uvoffset;
516 v_buffer[ALTREF_FRAME] = alt_yv12->v_buffer + recon_uvoffset;
519 skip_mode[ALTREF_FRAME] = 1;
521 cpi->mbs_tested_so_far++; // Count of the number of MBs tested so far this frame
523 *returnintra = INT_MAX;
526 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(cpi->prob_intra_coded);
528 // Special case treatment when GF and ARF are not sensible options for reference
529 if (cpi->ref_frame_flags == VP8_LAST_FLAG)
531 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
532 + vp8_cost_zero(255);
533 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
535 + vp8_cost_zero(128);
536 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
542 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
543 + vp8_cost_zero(cpi->prob_last_coded);
544 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
545 + vp8_cost_one(cpi->prob_last_coded)
546 + vp8_cost_zero(cpi->prob_gf_coded);
547 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
548 + vp8_cost_one(cpi->prob_last_coded)
549 + vp8_cost_one(cpi->prob_gf_coded);
552 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
554 // if we encode a new mv this is important
555 // find the best new motion vector
556 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
559 int this_rd = INT_MAX;
561 if (best_rd <= cpi->rd_threshes[mode_index])
564 x->e_mbd.mode_info_context->mbmi.ref_frame = vp8_ref_frame_order[mode_index];
566 if (skip_mode[x->e_mbd.mode_info_context->mbmi.ref_frame])
569 // Check to see if the testing frequency for this mode is at its max
570 // If so then prevent it from being tested and increase the threshold for its testing
571 if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
573 //if ( (cpi->mbs_tested_so_far / cpi->mode_test_hit_counts[mode_index]) <= cpi->mode_check_freq[mode_index] )
574 if (cpi->mbs_tested_so_far <= (cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index]))
576 // Increase the threshold for coding this mode to make it less likely to be chosen
577 cpi->rd_thresh_mult[mode_index] += 4;
579 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
580 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
582 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
588 // We have now reached the point where we are going to test the current mode so increment the counter for the number of times it has been tested
589 cpi->mode_test_hit_counts[mode_index] ++;
594 this_mode = vp8_mode_order[mode_index];
596 // Experimental debug code.
597 //all_rds[mode_index] = -1;
599 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
600 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
602 // Work out the cost assosciated with selecting the reference frame
603 frame_cost = ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
606 // everything but intra
607 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
609 x->e_mbd.pre.y_buffer = y_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
610 x->e_mbd.pre.u_buffer = u_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
611 x->e_mbd.pre.v_buffer = v_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
612 mode_mv[NEARESTMV] = nearest_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
613 mode_mv[NEARMV] = near_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
614 best_ref_mv = frame_best_ref_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
615 memcpy(mdcounts, MDCounts[x->e_mbd.mode_info_context->mbmi.ref_frame], sizeof(mdcounts));
618 // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
619 // unless ARNR filtering is enabled in which case we want
620 // an unfiltered alternative
621 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
623 if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
627 if(cpi->sf.improved_mv_pred && x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
631 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
635 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
636 x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
638 /* adjust mvp to make sure it is within MV range */
640 best_ref_mv.as_mv.row - MAX_FULL_PEL_VAL,
641 best_ref_mv.as_mv.row + MAX_FULL_PEL_VAL,
642 best_ref_mv.as_mv.col - MAX_FULL_PEL_VAL,
643 best_ref_mv.as_mv.col + MAX_FULL_PEL_VAL);
649 // Pass best so far to vp8_pick_intra4x4mby_modes to use as breakout
650 distortion2 = *returndistortion;
651 vp8_pick_intra4x4mby_modes(IF_RTCD(&cpi->rtcd), x,
652 &rate, &distortion2);
654 if (distortion2 == INT_MAX)
661 distortion2 = VARIANCE_INVOKE
662 (&cpi->rtcd.variance, get16x16prederror)(
663 x->src.y_buffer, x->src.y_stride,
664 x->e_mbd.predictor, 16, 0x7fffffff);
665 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
667 if (this_rd < best_intra_rd)
669 best_intra_rd = this_rd;
670 *returnintra = distortion2;
678 // Split MV modes currently not supported when RD is nopt enabled.
685 RECON_INVOKE(&cpi->common.rtcd.recon, build_intra_predictors_mby)
687 distortion2 = VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16prederror)(x->src.y_buffer, x->src.y_stride, x->e_mbd.predictor, 16, 0x7fffffff);
688 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
689 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
691 if (this_rd < best_intra_rd)
693 best_intra_rd = this_rd;
694 *returnintra = distortion2;
704 int sadpb = x->sadperbit16;
711 int tmp_col_min = x->mv_col_min;
712 int tmp_col_max = x->mv_col_max;
713 int tmp_row_min = x->mv_row_min;
714 int tmp_row_max = x->mv_row_max;
716 int speed_adjust = (cpi->Speed > 5) ? ((cpi->Speed >= 8)? 3 : 2) : 1;
718 // Further step/diamond searches as necessary
719 step_param = cpi->sf.first_step + speed_adjust;
721 if(cpi->sf.improved_mv_pred)
724 //adjust search range according to sr from mv prediction
728 col_min = (best_ref_mv.as_mv.col - MAX_FULL_PEL_VAL) >>3;
729 col_max = (best_ref_mv.as_mv.col + MAX_FULL_PEL_VAL) >>3;
730 row_min = (best_ref_mv.as_mv.row - MAX_FULL_PEL_VAL) >>3;
731 row_max = (best_ref_mv.as_mv.row + MAX_FULL_PEL_VAL) >>3;
733 // Get intersection of UMV window and valid MV window to reduce # of checks in diamond search.
734 if (x->mv_col_min < col_min )
735 x->mv_col_min = col_min;
736 if (x->mv_col_max > col_max )
737 x->mv_col_max = col_max;
738 if (x->mv_row_min < row_min )
739 x->mv_row_min = row_min;
740 if (x->mv_row_max > row_max )
741 x->mv_row_max = row_max;
744 mvp.as_int = best_ref_mv.as_int;
747 further_steps = (cpi->Speed >= 8)? 0: (cpi->sf.max_step_search_steps - 1 - step_param);
749 if (cpi->sf.search_method == HEX)
751 bestsme = vp8_hex_search(x, b, d, &mvp, &d->bmi.mv, step_param, sadpb/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv);
752 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
756 bestsme = cpi->diamond_search_sad(x, b, d, &mvp, &d->bmi.mv, step_param, sadpb / 2/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv); //sadpb < 9
757 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
759 // Further step/diamond searches as necessary
761 //further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
766 while (n < further_steps)
775 cpi->diamond_search_sad(x, b, d, &mvp,
779 &cpi->fn_ptr[BLOCK_16X16],
780 x->mvcost, &best_ref_mv);
781 if (thissme < bestsme)
784 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
788 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
794 if(cpi->sf.improved_mv_pred)
796 x->mv_col_min = tmp_col_min;
797 x->mv_col_max = tmp_col_max;
798 x->mv_row_min = tmp_row_min;
799 x->mv_row_max = tmp_row_max;
802 if (bestsme < INT_MAX)
803 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv, x->errorperbit, &cpi->fn_ptr[BLOCK_16X16], cpi->mb.mvcost, &distortion2, &sse);
805 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
808 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, cpi->mb.mvcost, 128);
814 if (mode_mv[this_mode].as_int == 0)
819 // Trap vectors that reach beyond the UMV borders
820 // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
821 // because of the lack of break statements in the previous two cases.
822 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
823 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
826 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
827 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
828 x->e_mbd.mode_info_context->mbmi.mv.as_mv = mode_mv[this_mode].as_mv;
829 x->e_mbd.block[0].bmi.mode = this_mode;
830 x->e_mbd.block[0].bmi.mv.as_int = x->e_mbd.mode_info_context->mbmi.mv.as_int;
832 if((this_mode != NEWMV) || !(have_subp_search))
833 distortion2 = get_inter_mbpred_error(x, &cpi->fn_ptr[BLOCK_16X16], &sse);
835 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
837 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
841 else if (sse < x->encode_breakout)
843 // Check u and v to make sure skip is ok
846 sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
848 if (sse2 * 2 < x->encode_breakout)
859 // Experimental debug code.
860 //all_rds[mode_index] = this_rd;
862 if (this_rd < best_rd || x->skip)
864 // Note index of best mode
865 best_mode_index = mode_index;
868 *returndistortion = distortion2;
870 vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
871 vpx_memcpy(&best_partition, x->partition_info, sizeof(PARTITION_INFO));
873 if (this_mode == B_PRED || this_mode == SPLITMV)
874 for (i = 0; i < 16; i++)
876 vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
880 best_bmodes[0].mv = x->e_mbd.block[0].bmi.mv;
883 // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
884 cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
885 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
888 // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
891 cpi->rd_thresh_mult[mode_index] += 4;
893 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
894 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
896 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
903 // Reduce the activation RD thresholds for the best choice mode
904 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
906 int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 3);
908 cpi->rd_thresh_mult[best_mode_index] = (cpi->rd_thresh_mult[best_mode_index] >= (MIN_THRESHMULT + best_adjustment)) ? cpi->rd_thresh_mult[best_mode_index] - best_adjustment : MIN_THRESHMULT;
909 cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
912 // Keep a record of best mode index for use in next loop
913 cpi->last_best_mode_index = best_mode_index;
915 if (best_mbmode.mode <= B_PRED)
917 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
918 vp8_pick_intra_mbuv_mode(x);
919 best_mbmode.uv_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
924 int this_rdbin = (*returndistortion >> 7);
926 if (this_rdbin >= 1024)
931 cpi->error_bins[this_rdbin] ++;
935 if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
937 best_mbmode.mode = ZEROMV;
938 best_mbmode.ref_frame = ALTREF_FRAME;
939 best_mbmode.mv.as_int = 0;
940 best_mbmode.uv_mode = 0;
941 best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
942 best_mbmode.partitioning = 0;
944 vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
945 vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
947 for (i = 0; i < 16; i++)
949 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
952 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
958 vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
959 vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
961 if (x->e_mbd.mode_info_context->mbmi.mode == B_PRED || x->e_mbd.mode_info_context->mbmi.mode == SPLITMV)
962 for (i = 0; i < 16; i++)
964 vpx_memcpy(&x->e_mbd.block[i].bmi, &best_bmodes[i], sizeof(B_MODE_INFO));
969 vp8_set_mbmode_and_mvs(x, x->e_mbd.mode_info_context->mbmi.mode, &best_bmodes[0].mv);
972 x->e_mbd.mode_info_context->mbmi.mv.as_mv = x->e_mbd.block[15].bmi.mv.as_mv;