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.
16 #include "vp8/common/pragmas.h"
19 #include "treewriter.h"
21 #include "modecosts.h"
22 #include "encodeintra.h"
23 #include "vp8/common/entropymode.h"
24 #include "vp8/common/reconinter.h"
25 #include "vp8/common/reconintra.h"
26 #include "vp8/common/reconintra4x4.h"
27 #include "vp8/common/findnearmv.h"
30 #include "vp8/common/idct.h"
31 #include "vp8/common/g_common.h"
35 #include "vpx_mem/vpx_mem.h"
37 #include "vp8/common/systemdependent.h"
39 #if CONFIG_RUNTIME_CPU_DETECT
40 #define IF_RTCD(x) (x)
42 #define IF_RTCD(x) NULL
46 extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
47 extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);
49 #define MAXF(a,b) (((a) > (b)) ? (a) : (b))
51 static const int auto_speed_thresh[17] =
72 const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
104 const MV_REFERENCE_FRAME vp8_ref_frame_order[MAX_MODES] =
136 static void fill_token_costs(
137 unsigned int c [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens],
138 const vp8_prob p [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1]
144 for (i = 0; i < BLOCK_TYPES; i++)
145 for (j = 0; j < COEF_BANDS; j++)
146 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
148 vp8_cost_tokens((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
152 static int rd_iifactor [ 32 ] = { 4, 4, 3, 2, 1, 0, 0, 0,
153 0, 0, 0, 0, 0, 0, 0, 0,
154 0, 0, 0, 0, 0, 0, 0, 0,
155 0, 0, 0, 0, 0, 0, 0, 0,
159 /* values are now correlated to quantizer */
160 static int sad_per_bit16lut[QINDEX_RANGE] =
162 5, 5, 5, 5, 5, 5, 6, 6,
163 6, 6, 6, 6, 6, 7, 7, 7,
164 7, 7, 7, 7, 8, 8, 8, 8,
165 8, 8, 8, 8, 8, 8, 9, 9,
166 9, 9, 9, 9, 10, 10, 10, 10,
167 10, 10, 11, 11, 11, 11, 11, 11,
168 12, 12, 12, 12, 12, 12, 12, 13,
169 13, 13, 13, 13, 13, 14, 14, 14,
170 14, 14, 15, 15, 15, 15, 15, 15,
171 16, 16, 16, 16, 16, 16, 17, 17,
172 17, 17, 17, 17, 17, 18, 18, 18,
173 18, 18, 19, 19, 19, 19, 19, 19,
174 20, 20, 20, 21, 21, 21, 21, 22,
175 22, 22, 23, 23, 23, 24, 24, 24,
176 25, 25, 26, 26, 27, 27, 27, 28,
177 28, 28, 29, 29, 30, 30, 31, 31
179 static int sad_per_bit4lut[QINDEX_RANGE] =
181 5, 5, 5, 5, 5, 5, 7, 7,
182 7, 7, 7, 7, 7, 8, 8, 8,
183 8, 8, 8, 8, 10, 10, 10, 10,
184 10, 10, 10, 10, 10, 10, 11, 11,
185 11, 11, 11, 11, 13, 13, 13, 13,
186 13, 13, 14, 14, 14, 14, 14, 14,
187 16, 16, 16, 16, 16, 16, 16, 17,
188 17, 17, 17, 17, 17, 19, 19, 19,
189 19, 19, 20, 20, 20, 20, 20, 20,
190 22, 22, 22, 22, 22, 22, 23, 23,
191 23, 23, 23, 23, 23, 25, 25, 25,
192 25, 25, 26, 26, 26, 26, 26, 26,
193 28, 28, 28, 29, 29, 29, 29, 31,
194 31, 31, 32, 32, 32, 34, 34, 34,
195 35, 35, 37, 37, 38, 38, 38, 40,
196 40, 40, 41, 41, 43, 43, 44, 44,
199 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
201 cpi->mb.sadperbit16 = sad_per_bit16lut[QIndex];
202 cpi->mb.sadperbit4 = sad_per_bit4lut[QIndex];
205 void vp8_initialize_rd_consts(VP8_COMP *cpi, int Qvalue)
209 double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
210 double rdconst = 2.70;
212 vp8_clear_system_state(); //__asm emms;
214 // Further tests required to see if optimum is different
215 // for key frames, golden frames and arf frames.
216 // if (cpi->common.refresh_golden_frame ||
217 // cpi->common.refresh_alt_ref_frame)
218 cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
220 // Extend rate multiplier along side quantizer zbin increases
221 if (cpi->zbin_over_quant > 0)
226 // Experimental code using the same basic equation as used for Q above
227 // The units of cpi->zbin_over_quant are 1/128 of Q bin size
228 oq_factor = 1.0 + ((double)0.0015625 * cpi->zbin_over_quant);
229 modq = (int)((double)capped_q * oq_factor);
230 cpi->RDMULT = (int)(rdconst * (modq * modq));
233 if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
235 if (cpi->next_iiratio > 31)
236 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
238 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[cpi->next_iiratio]) >> 4;
241 cpi->mb.errorperbit = (cpi->RDMULT / 100);
242 cpi->mb.errorperbit += (cpi->mb.errorperbit==0);
244 vp8_set_speed_features(cpi);
246 q = (int)pow(Qvalue, 1.25);
251 if (cpi->RDMULT > 1000)
256 for (i = 0; i < MAX_MODES; i++)
258 if (cpi->sf.thresh_mult[i] < INT_MAX)
260 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
264 cpi->rd_threshes[i] = INT_MAX;
267 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
274 for (i = 0; i < MAX_MODES; i++)
276 if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
278 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
282 cpi->rd_threshes[i] = INT_MAX;
285 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
291 (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs
294 vp8_init_mode_costs(cpi);
298 void vp8_auto_select_speed(VP8_COMP *cpi)
300 int milliseconds_for_compress = (int)(1000000 / cpi->oxcf.frame_rate);
302 milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
310 f = fopen("speed.stt", "a");
311 fprintf(f, " %8ld %10ld %10ld %10ld\n",
312 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
319 // this is done during parameter valid check
320 if( cpi->oxcf.cpu_used > 16)
321 cpi->oxcf.cpu_used = 16;
322 if( cpi->oxcf.cpu_used < -16)
323 cpi->oxcf.cpu_used = -16;
326 if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
328 if (cpi->avg_pick_mode_time == 0)
334 if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
337 cpi->avg_pick_mode_time = 0;
338 cpi->avg_encode_time = 0;
346 if (milliseconds_for_compress * 100 > cpi->avg_encode_time * auto_speed_thresh[cpi->Speed])
349 cpi->avg_pick_mode_time = 0;
350 cpi->avg_encode_time = 0;
352 // In real-time mode, cpi->speed is in [4, 16].
353 if (cpi->Speed < 4) //if ( cpi->Speed < 0 )
355 cpi->Speed = 4; //cpi->Speed = 0;
368 cpi->avg_pick_mode_time = 0;
369 cpi->avg_encode_time = 0;
373 int vp8_block_error_c(short *coeff, short *dqcoeff)
378 for (i = 0; i < 16; i++)
380 int this_diff = coeff[i] - dqcoeff[i];
381 error += this_diff * this_diff;
387 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
392 int berror, error = 0;
394 for (i = 0; i < 16; i++)
397 bd = &mb->e_mbd.block[i];
401 for (j = dc; j < 16; j++)
403 int this_diff = be->coeff[j] - bd->dqcoeff[j];
404 berror += this_diff * this_diff;
413 int vp8_mbuverror_c(MACROBLOCK *mb)
423 for (i = 16; i < 24; i++)
426 bd = &mb->e_mbd.block[i];
428 error += vp8_block_error_c(be->coeff, bd->dqcoeff);
434 int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd)
436 unsigned char *uptr, *vptr;
437 unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
438 unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
439 int uv_stride = x->block[16].src_stride;
441 unsigned int sse1 = 0;
442 unsigned int sse2 = 0;
446 int pre_stride = x->e_mbd.block[16].pre_stride;
448 vp8_build_uvmvs(&x->e_mbd, 0);
449 mv_row = x->e_mbd.block[16].bmi.mv.as_mv.row;
450 mv_col = x->e_mbd.block[16].bmi.mv.as_mv.col;
452 offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
453 uptr = x->e_mbd.pre.u_buffer + offset;
454 vptr = x->e_mbd.pre.v_buffer + offset;
456 if ((mv_row | mv_col) & 7)
458 VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
459 VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
464 VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
465 VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
473 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
475 int c = !type; /* start at coef 0, unless Y with Y2 */
477 int pt ; /* surrounding block/prev coef predictor */
479 short *qcoeff_ptr = b->qcoeff;
481 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
483 # define QC( I) ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )
488 int t = vp8_dct_value_tokens_ptr[v].Token;
489 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
490 cost += vp8_dct_value_cost_ptr[v];
491 pt = vp8_prev_token_class[t];
497 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
499 pt = (c != !type); // is eob first coefficient;
505 static int vp8_rdcost_mby(MACROBLOCK *mb)
509 MACROBLOCKD *x = &mb->e_mbd;
510 ENTROPY_CONTEXT_PLANES t_above, t_left;
514 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
515 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
517 ta = (ENTROPY_CONTEXT *)&t_above;
518 tl = (ENTROPY_CONTEXT *)&t_left;
520 for (b = 0; b < 16; b++)
521 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
522 ta + vp8_block2above[b], tl + vp8_block2left[b]);
524 cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2,
525 ta + vp8_block2above[24], tl + vp8_block2left[24]);
530 static void macro_block_yrd( MACROBLOCK *mb,
533 const vp8_encodemb_rtcd_vtable_t *rtcd)
536 MACROBLOCKD *const x = &mb->e_mbd;
537 BLOCK *const mb_y2 = mb->block + 24;
538 BLOCKD *const x_y2 = x->block + 24;
539 short *Y2DCPtr = mb_y2->src_diff;
543 ENCODEMB_INVOKE(rtcd, submby)( mb->src_diff, mb->src.y_buffer,
544 mb->e_mbd.predictor, mb->src.y_stride );
546 // Fdct and building the 2nd order block
547 for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
549 mb->vp8_short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
550 *Y2DCPtr++ = beptr->coeff[0];
551 *Y2DCPtr++ = beptr->coeff[16];
555 mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
558 for (b = 0; b < 16; b++)
560 mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
563 // DC predication and Quantization of 2nd Order block
564 mb->quantize_b(mb_y2, x_y2);
567 d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 1) << 2;
568 d += ENCODEMB_INVOKE(rtcd, berr)(mb_y2->coeff, x_y2->dqcoeff);
570 *Distortion = (d >> 4);
573 *Rate = vp8_rdcost_mby(mb);
576 static void copy_predictor(unsigned char *dst, const unsigned char *predictor)
578 const unsigned int *p = (const unsigned int *)predictor;
579 unsigned int *d = (unsigned int *)dst;
585 static int rd_pick_intra4x4block(
590 B_PREDICTION_MODE *best_mode,
591 unsigned int *bmode_costs,
599 B_PREDICTION_MODE mode;
600 int best_rd = INT_MAX;
604 ENTROPY_CONTEXT ta = *a, tempa = *a;
605 ENTROPY_CONTEXT tl = *l, templ = *l;
607 * The predictor buffer is a 2d buffer with a stride of 16. Create
608 * a temp buffer that meets the stride requirements, but we are only
609 * interested in the left 4x4 block
611 DECLARE_ALIGNED_ARRAY(16, unsigned char, best_predictor, 16*4);
612 DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16);
614 for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
619 rate = bmode_costs[mode];
621 RECON_INVOKE(&cpi->rtcd.common->recon, intra4x4_predict)
622 (b, mode, b->predictor);
623 ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), subb)(be, b, 16);
624 x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
625 x->quantize_b(be, b);
630 ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ);
632 distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(be->coeff, b->dqcoeff) >> 2;
634 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
636 if (this_rd < best_rd)
640 *bestdistortion = distortion;
645 copy_predictor(best_predictor, b->predictor);
646 vpx_memcpy(best_dqcoeff, b->dqcoeff, 32);
649 b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
651 IDCT_INVOKE(IF_RTCD(&cpi->rtcd.common->idct), idct16)(best_dqcoeff, b->diff, 32);
652 RECON_INVOKE(IF_RTCD(&cpi->rtcd.common->recon), recon)(best_predictor, b->diff, *(b->base_dst) + b->dst, b->dst_stride);
657 int vp8_rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate,
658 int *rate_y, int *Distortion, int best_rd)
660 MACROBLOCKD *const xd = &mb->e_mbd;
662 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
665 long long total_rd = 0;
666 ENTROPY_CONTEXT_PLANES t_above, t_left;
669 unsigned int *bmode_costs;
671 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
672 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
674 ta = (ENTROPY_CONTEXT *)&t_above;
675 tl = (ENTROPY_CONTEXT *)&t_left;
677 vp8_intra_prediction_down_copy(xd);
679 bmode_costs = mb->inter_bmode_costs;
681 for (i = 0; i < 16; i++)
683 MODE_INFO *const mic = xd->mode_info_context;
684 const int mis = xd->mode_info_stride;
685 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
686 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
688 if (mb->e_mbd.frame_type == KEY_FRAME)
690 const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
691 const B_PREDICTION_MODE L = left_block_mode(mic, i);
693 bmode_costs = mb->bmode_costs[A][L];
696 total_rd += rd_pick_intra4x4block(
697 cpi, mb, mb->block + i, xd->block + i, &best_mode, bmode_costs,
698 ta + vp8_block2above[i],
699 tl + vp8_block2left[i], &r, &ry, &d);
705 mic->bmi[i].as_mode = best_mode;
707 if(total_rd >= (long long)best_rd)
711 if(total_rd >= (long long)best_rd)
715 *rate_y += tot_rate_y;
716 *Distortion = distortion;
718 return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
720 int vp8_rd_pick_intra16x16mby_mode(VP8_COMP *cpi,
726 MB_PREDICTION_MODE mode;
727 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
730 int best_rd = INT_MAX;
733 //Y Search for 16x16 intra prediction mode
734 for (mode = DC_PRED; mode <= TM_PRED; mode++)
736 x->e_mbd.mode_info_context->mbmi.mode = mode;
738 RECON_INVOKE(&cpi->common.rtcd.recon, build_intra_predictors_mby)
741 macro_block_yrd(x, &ratey, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
742 rate = ratey + x->mbmode_cost[x->e_mbd.frame_type]
743 [x->e_mbd.mode_info_context->mbmi.mode];
745 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
747 if (this_rd < best_rd)
749 mode_selected = mode;
753 *Distortion = distortion;
757 x->e_mbd.mode_info_context->mbmi.mode = mode_selected;
761 static int rd_cost_mbuv(MACROBLOCK *mb)
765 MACROBLOCKD *x = &mb->e_mbd;
766 ENTROPY_CONTEXT_PLANES t_above, t_left;
770 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
771 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
773 ta = (ENTROPY_CONTEXT *)&t_above;
774 tl = (ENTROPY_CONTEXT *)&t_left;
776 for (b = 16; b < 24; b++)
777 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_UV,
778 ta + vp8_block2above[b], tl + vp8_block2left[b]);
784 static int vp8_rd_inter_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *distortion, int fullpixel)
786 vp8_build_uvmvs(&x->e_mbd, fullpixel);
787 vp8_encode_inter16x16uvrd(IF_RTCD(&cpi->rtcd), x);
790 *rate = rd_cost_mbuv(x);
791 *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
793 return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
796 void vp8_rd_pick_intra_mbuv_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, int *distortion)
798 MB_PREDICTION_MODE mode;
799 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
800 int best_rd = INT_MAX;
801 int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
804 for (mode = DC_PRED; mode <= TM_PRED; mode++)
810 x->e_mbd.mode_info_context->mbmi.uv_mode = mode;
811 RECON_INVOKE(&cpi->rtcd.common->recon, build_intra_predictors_mbuv)
813 ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), submbuv)(x->src_diff,
814 x->src.u_buffer, x->src.v_buffer, x->e_mbd.predictor,
816 vp8_transform_mbuv(x);
817 vp8_quantize_mbuv(x);
819 rate_to = rd_cost_mbuv(x);
820 rate = rate_to + x->intra_uv_mode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.uv_mode];
822 distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
824 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
826 if (this_rd < best_rd)
831 *rate_tokenonly = rate_to;
832 mode_selected = mode;
839 x->e_mbd.mode_info_context->mbmi.uv_mode = mode_selected;
842 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
844 vp8_prob p [VP8_MVREFS-1];
845 assert(NEARESTMV <= m && m <= SPLITMV);
846 vp8_mv_ref_probs(p, near_mv_ref_ct);
847 return vp8_cost_token(vp8_mv_ref_tree, p,
848 vp8_mv_ref_encoding_array - NEARESTMV + m);
851 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv)
853 x->e_mbd.mode_info_context->mbmi.mode = mb;
854 x->e_mbd.mode_info_context->mbmi.mv.as_int = mv->as_int;
857 static int labels2mode(
859 int const *labelings, int which_label,
860 B_PREDICTION_MODE this_mode,
861 int_mv *this_mv, int_mv *best_ref_mv,
865 MACROBLOCKD *const xd = & x->e_mbd;
866 MODE_INFO *const mic = xd->mode_info_context;
867 const int mis = xd->mode_info_stride;
872 /* We have to be careful retrieving previously-encoded motion vectors.
873 Ones from this macroblock have to be pulled from the BLOCKD array
874 as they have not yet made it to the bmi array in our MB_MODE_INFO. */
880 BLOCKD *const d = xd->block + i;
881 const int row = i >> 2, col = i & 3;
885 if (labelings[i] != which_label)
888 if (col && labelings[i] == labelings[i-1])
890 else if (row && labelings[i] == labelings[i-4])
894 // the only time we should do costing for new motion vector or mode
895 // is when we are on a new label (jbb May 08, 2007)
896 switch (m = this_mode)
899 thismvcost = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
902 this_mv->as_int = col ? d[-1].bmi.mv.as_int : left_block_mv(mic, i);
905 this_mv->as_int = row ? d[-4].bmi.mv.as_int : above_block_mv(mic, i, mis);
914 if (m == ABOVE4X4) // replace above with left if same
918 left_mv.as_int = col ? d[-1].bmi.mv.as_int :
919 left_block_mv(mic, i);
921 if (left_mv.as_int == this_mv->as_int)
925 cost = x->inter_bmode_costs[ m];
928 d->bmi.mv.as_int = this_mv->as_int;
930 x->partition_info->bmi[i].mode = m;
931 x->partition_info->bmi[i].mv.as_int = this_mv->as_int;
940 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels,
941 int which_label, ENTROPY_CONTEXT *ta,
946 MACROBLOCKD *x = &mb->e_mbd;
948 for (b = 0; b < 16; b++)
949 if (labels[ b] == which_label)
950 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_WITH_DC,
951 ta + vp8_block2above[b],
952 tl + vp8_block2left[b]);
957 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label, const vp8_encodemb_rtcd_vtable_t *rtcd)
960 unsigned int distortion = 0;
962 for (i = 0; i < 16; i++)
964 if (labels[i] == which_label)
966 BLOCKD *bd = &x->e_mbd.block[i];
967 BLOCK *be = &x->block[i];
970 vp8_build_inter_predictors_b(bd, 16, x->e_mbd.subpixel_predict);
971 ENCODEMB_INVOKE(rtcd, subb)(be, bd, 16);
972 x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
974 // set to 0 no way to account for 2nd order DC so discount
976 x->quantize_b(be, bd);
978 distortion += ENCODEMB_INVOKE(rtcd, berr)(be->coeff, bd->dqcoeff);
986 static const unsigned int segmentation_to_sseshift[4] = {3, 3, 2, 0};
999 B_PREDICTION_MODE modes[16];
1001 unsigned char eobs[16];
1006 int_mv sv_mvp[4]; // save 4 mvp from 8x8
1007 int sv_istep[2]; // save 2 initial step_param for 16x8/8x16
1012 static void rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x,
1013 BEST_SEG_INFO *bsi, unsigned int segmentation)
1019 B_PREDICTION_MODE this_mode;
1023 int this_segment_rd = 0;
1024 int label_mv_thresh;
1028 int segmentyrate = 0;
1030 vp8_variance_fn_ptr_t *v_fn_ptr;
1032 ENTROPY_CONTEXT_PLANES t_above, t_left;
1033 ENTROPY_CONTEXT *ta;
1034 ENTROPY_CONTEXT *tl;
1035 ENTROPY_CONTEXT_PLANES t_above_b, t_left_b;
1036 ENTROPY_CONTEXT *ta_b;
1037 ENTROPY_CONTEXT *tl_b;
1039 vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
1040 vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
1042 ta = (ENTROPY_CONTEXT *)&t_above;
1043 tl = (ENTROPY_CONTEXT *)&t_left;
1044 ta_b = (ENTROPY_CONTEXT *)&t_above_b;
1045 tl_b = (ENTROPY_CONTEXT *)&t_left_b;
1050 v_fn_ptr = &cpi->fn_ptr[segmentation];
1051 labels = vp8_mbsplits[segmentation];
1052 label_count = vp8_mbsplit_count[segmentation];
1054 // 64 makes this threshold really big effectively
1055 // making it so that we very rarely check mvs on
1056 // segments. setting this to 1 would make mv thresh
1057 // roughly equal to what it is for macroblocks
1058 label_mv_thresh = 1 * bsi->mvthresh / label_count ;
1060 // Segmentation method overheads
1061 rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
1062 rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts);
1063 this_segment_rd += RDCOST(x->rdmult, x->rddiv, rate, 0);
1066 for (i = 0; i < label_count; i++)
1068 int_mv mode_mv[B_MODE_COUNT];
1069 int best_label_rd = INT_MAX;
1070 B_PREDICTION_MODE mode_selected = ZERO4X4;
1071 int bestlabelyrate = 0;
1073 // search for the best motion vector on this segment
1074 for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
1079 ENTROPY_CONTEXT_PLANES t_above_s, t_left_s;
1080 ENTROPY_CONTEXT *ta_s;
1081 ENTROPY_CONTEXT *tl_s;
1083 vpx_memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES));
1084 vpx_memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES));
1086 ta_s = (ENTROPY_CONTEXT *)&t_above_s;
1087 tl_s = (ENTROPY_CONTEXT *)&t_left_s;
1089 if (this_mode == NEW4X4)
1097 int bestsme = INT_MAX;
1102 // Is the best so far sufficiently good that we cant justify doing and new motion search.
1103 if (best_label_rd < label_mv_thresh)
1106 if(cpi->compressor_speed)
1108 if (segmentation == BLOCK_8X16 || segmentation == BLOCK_16X8)
1110 bsi->mvp.as_int = bsi->sv_mvp[i].as_int;
1111 if (i==1 && segmentation == BLOCK_16X8)
1112 bsi->mvp.as_int = bsi->sv_mvp[2].as_int;
1114 step_param = bsi->sv_istep[i];
1117 // use previous block's result as next block's MV predictor.
1118 if (segmentation == BLOCK_4X4 && i>0)
1120 bsi->mvp.as_int = x->e_mbd.block[i-1].bmi.mv.as_int;
1121 if (i==4 || i==8 || i==12)
1122 bsi->mvp.as_int = x->e_mbd.block[i-4].bmi.mv.as_int;
1127 further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
1130 int sadpb = x->sadperbit4;
1133 n = vp8_mbsplit_offset[segmentation][i];
1136 e = &x->e_mbd.block[n];
1138 if (cpi->sf.search_method == HEX)
1139 bestsme = vp8_hex_search(x, c, e, bsi->ref_mv,
1140 &mode_mv[NEW4X4], step_param, sadpb, &num00, v_fn_ptr, x->mvsadcost, x->mvcost, bsi->ref_mv);
1144 bestsme = cpi->diamond_search_sad(x, c, e, &bsi->mvp,
1145 &mode_mv[NEW4X4], step_param,
1146 sadpb / 2, &num00, v_fn_ptr, x->mvcost, bsi->ref_mv);
1151 while (n < further_steps)
1159 thissme = cpi->diamond_search_sad(x, c, e, &bsi->mvp,
1160 &temp_mv, step_param + n,
1161 sadpb / 2, &num00, v_fn_ptr, x->mvcost, bsi->ref_mv);
1163 if (thissme < bestsme)
1166 mode_mv[NEW4X4].as_int = temp_mv.as_int;
1172 sseshift = segmentation_to_sseshift[segmentation];
1174 // Should we do a full search (best quality only)
1175 if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000)
1179 full_mvp.as_mv.row = bsi->mvp.as_mv.row >>3;
1180 full_mvp.as_mv.col = bsi->mvp.as_mv.col >>3;
1182 thissme = cpi->full_search_sad(x, c, e, &full_mvp,
1183 sadpb / 4, 16, v_fn_ptr, x->mvcost, bsi->ref_mv);
1185 if (thissme < bestsme)
1188 mode_mv[NEW4X4].as_int = e->bmi.mv.as_int;
1192 // The full search result is actually worse so re-instate the previous best vector
1193 e->bmi.mv.as_int = mode_mv[NEW4X4].as_int;
1198 if (bestsme < INT_MAX)
1203 if (!cpi->common.full_pixel)
1204 cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
1205 bsi->ref_mv, x->errorperbit / 2, v_fn_ptr, x->mvcost, &distortion, &sse);
1207 vp8_skip_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
1208 bsi->ref_mv, x->errorperbit, v_fn_ptr, x->mvcost, &distortion, &sse);
1212 rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode],
1213 bsi->ref_mv, x->mvcost);
1215 // Trap vectors that reach beyond the UMV borders
1216 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) ||
1217 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1222 distortion = vp8_encode_inter_mb_segment(x, labels, i, IF_RTCD(&cpi->rtcd.encodemb)) / 4;
1224 labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s);
1227 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1229 if (this_rd < best_label_rd)
1233 bestlabelyrate = labelyrate;
1234 mode_selected = this_mode;
1235 best_label_rd = this_rd;
1237 vpx_memcpy(ta_b, ta_s, sizeof(ENTROPY_CONTEXT_PLANES));
1238 vpx_memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES));
1241 } /*for each 4x4 mode*/
1243 vpx_memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES));
1244 vpx_memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES));
1246 labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected],
1247 bsi->ref_mv, x->mvcost);
1251 segmentyrate += bestlabelyrate;
1252 this_segment_rd += best_label_rd;
1254 if (this_segment_rd >= bsi->segment_rd)
1257 } /* for each label */
1259 if (this_segment_rd < bsi->segment_rd)
1263 bsi->segment_yrate = segmentyrate;
1264 bsi->segment_rd = this_segment_rd;
1265 bsi->segment_num = segmentation;
1267 // store everything needed to come back to this!!
1268 for (i = 0; i < 16; i++)
1270 BLOCKD *bd = &x->e_mbd.block[i];
1272 bsi->mvs[i].as_mv = x->partition_info->bmi[i].mv.as_mv;
1273 bsi->modes[i] = x->partition_info->bmi[i].mode;
1274 bsi->eobs[i] = bd->eob;
1280 void vp8_cal_step_param(int sr, int *sp)
1284 if (sr > MAX_FIRST_STEP) sr = MAX_FIRST_STEP;
1285 else if (sr < 1) sr = 1;
1290 *sp = MAX_MVSEARCH_STEPS - 1 - step;
1293 static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x,
1294 int_mv *best_ref_mv, int best_rd,
1295 int *mdcounts, int *returntotrate,
1296 int *returnyrate, int *returndistortion,
1302 vpx_memset(&bsi, 0, sizeof(bsi));
1304 bsi.segment_rd = best_rd;
1305 bsi.ref_mv = best_ref_mv;
1306 bsi.mvp.as_int = best_ref_mv->as_int;
1307 bsi.mvthresh = mvthresh;
1308 bsi.mdcounts = mdcounts;
1310 for(i = 0; i < 16; i++)
1312 bsi.modes[i] = ZERO4X4;
1315 if(cpi->compressor_speed == 0)
1317 /* for now, we will keep the original segmentation order
1318 when in best quality mode */
1319 rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1320 rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1321 rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1322 rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1328 rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1330 if (bsi.segment_rd < best_rd)
1332 int col_min = (best_ref_mv->as_mv.col - MAX_FULL_PEL_VAL) >>3;
1333 int col_max = (best_ref_mv->as_mv.col + MAX_FULL_PEL_VAL) >>3;
1334 int row_min = (best_ref_mv->as_mv.row - MAX_FULL_PEL_VAL) >>3;
1335 int row_max = (best_ref_mv->as_mv.row + MAX_FULL_PEL_VAL) >>3;
1337 int tmp_col_min = x->mv_col_min;
1338 int tmp_col_max = x->mv_col_max;
1339 int tmp_row_min = x->mv_row_min;
1340 int tmp_row_max = x->mv_row_max;
1342 /* Get intersection of UMV window and valid MV window to reduce # of checks in diamond search. */
1343 if (x->mv_col_min < col_min )
1344 x->mv_col_min = col_min;
1345 if (x->mv_col_max > col_max )
1346 x->mv_col_max = col_max;
1347 if (x->mv_row_min < row_min )
1348 x->mv_row_min = row_min;
1349 if (x->mv_row_max > row_max )
1350 x->mv_row_max = row_max;
1352 /* Get 8x8 result */
1353 bsi.sv_mvp[0].as_int = bsi.mvs[0].as_int;
1354 bsi.sv_mvp[1].as_int = bsi.mvs[2].as_int;
1355 bsi.sv_mvp[2].as_int = bsi.mvs[8].as_int;
1356 bsi.sv_mvp[3].as_int = bsi.mvs[10].as_int;
1358 /* Use 8x8 result as 16x8/8x16's predictor MV. Adjust search range according to the closeness of 2 MV. */
1361 sr = MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[2].as_mv.row))>>3, (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[2].as_mv.col))>>3);
1362 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1364 sr = MAXF((abs(bsi.sv_mvp[1].as_mv.row - bsi.sv_mvp[3].as_mv.row))>>3, (abs(bsi.sv_mvp[1].as_mv.col - bsi.sv_mvp[3].as_mv.col))>>3);
1365 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1367 rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1372 sr = MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[1].as_mv.row))>>3, (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[1].as_mv.col))>>3);
1373 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1375 sr = MAXF((abs(bsi.sv_mvp[2].as_mv.row - bsi.sv_mvp[3].as_mv.row))>>3, (abs(bsi.sv_mvp[2].as_mv.col - bsi.sv_mvp[3].as_mv.col))>>3);
1376 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1378 rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1381 /* If 8x8 is better than 16x8/8x16, then do 4x4 search */
1382 /* Not skip 4x4 if speed=0 (good quality) */
1383 if (cpi->sf.no_skip_block4x4_search || bsi.segment_num == BLOCK_8X8) /* || (sv_segment_rd8x8-bsi.segment_rd) < sv_segment_rd8x8>>5) */
1385 bsi.mvp.as_int = bsi.sv_mvp[0].as_int;
1386 rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1389 /* restore UMV window */
1390 x->mv_col_min = tmp_col_min;
1391 x->mv_col_max = tmp_col_max;
1392 x->mv_row_min = tmp_row_min;
1393 x->mv_row_max = tmp_row_max;
1397 /* set it to the best */
1398 for (i = 0; i < 16; i++)
1400 BLOCKD *bd = &x->e_mbd.block[i];
1402 bd->bmi.mv.as_mv = bsi.mvs[i].as_mv;
1403 bd->bmi.mode = bsi.modes[i];
1404 bd->eob = bsi.eobs[i];
1407 *returntotrate = bsi.r;
1408 *returndistortion = bsi.d;
1409 *returnyrate = bsi.segment_yrate;
1411 /* save partitions */
1412 x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num;
1413 x->partition_info->count = vp8_mbsplit_count[bsi.segment_num];
1415 for (i = 0; i < x->partition_info->count; i++)
1419 j = vp8_mbsplit_offset[bsi.segment_num][i];
1421 x->partition_info->bmi[i].mode = bsi.modes[j];
1422 x->partition_info->bmi[i].mv.as_mv = bsi.mvs[j].as_mv;
1425 * used to set x->e_mbd.mode_info_context->mbmi.mv.as_int
1427 x->partition_info->bmi[15].mv.as_int = bsi.mvs[15].as_int;
1429 return bsi.segment_rd;
1432 static void insertsortmv(int arr[], int len)
1436 for ( i = 1 ; i <= len-1 ; i++ )
1438 for ( j = 0 ; j < i ; j++ )
1440 if ( arr[j] > arr[i] )
1446 for ( k = i; k >j; k--)
1447 arr[k] = arr[k - 1] ;
1455 static void insertsortsad(int arr[],int idx[], int len)
1459 for ( i = 1 ; i <= len-1 ; i++ )
1461 for ( j = 0 ; j < i ; j++ )
1463 if ( arr[j] > arr[i] )
1470 for ( k = i; k >j; k--)
1472 arr[k] = arr[k - 1] ;
1473 idx[k] = idx[k - 1];
1483 //The improved MV prediction
1488 const MODE_INFO *here,
1491 int *ref_frame_sign_bias,
1496 const MODE_INFO *above = here - xd->mode_info_stride;
1497 const MODE_INFO *left = here - 1;
1498 const MODE_INFO *aboveleft = above - 1;
1512 if(here->mbmi.ref_frame != INTRA_FRAME)
1514 near_mvs[0].as_int = near_mvs[1].as_int = near_mvs[2].as_int = near_mvs[3].as_int = near_mvs[4].as_int = near_mvs[5].as_int = near_mvs[6].as_int = near_mvs[7].as_int = 0;
1515 near_ref[0] = near_ref[1] = near_ref[2] = near_ref[3] = near_ref[4] = near_ref[5] = near_ref[6] = near_ref[7] = 0;
1517 // read in 3 nearby block's MVs from current frame as prediction candidates.
1518 if (above->mbmi.ref_frame != INTRA_FRAME)
1520 near_mvs[vcnt].as_int = above->mbmi.mv.as_int;
1521 mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1522 near_ref[vcnt] = above->mbmi.ref_frame;
1525 if (left->mbmi.ref_frame != INTRA_FRAME)
1527 near_mvs[vcnt].as_int = left->mbmi.mv.as_int;
1528 mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1529 near_ref[vcnt] = left->mbmi.ref_frame;
1532 if (aboveleft->mbmi.ref_frame != INTRA_FRAME)
1534 near_mvs[vcnt].as_int = aboveleft->mbmi.mv.as_int;
1535 mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1536 near_ref[vcnt] = aboveleft->mbmi.ref_frame;
1540 // read in 5 nearby block's MVs from last frame.
1541 if(cpi->common.last_frame_type != KEY_FRAME)
1543 mb_offset = (-xd->mb_to_top_edge/128 + 1) * (xd->mode_info_stride +1) + (-xd->mb_to_left_edge/128 +1) ;
1545 // current in last frame
1546 if (cpi->lf_ref_frame[mb_offset] != INTRA_FRAME)
1548 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset].as_int;
1549 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1550 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset];
1554 // above in last frame
1555 if (cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1] != INTRA_FRAME)
1557 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset - xd->mode_info_stride-1].as_int;
1558 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset - xd->mode_info_stride-1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1559 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1];
1563 // left in last frame
1564 if (cpi->lf_ref_frame[mb_offset-1] != INTRA_FRAME)
1566 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset -1].as_int;
1567 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset -1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1568 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - 1];
1572 // right in last frame
1573 if (cpi->lf_ref_frame[mb_offset +1] != INTRA_FRAME)
1575 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset +1].as_int;
1576 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1577 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset +1];
1581 // below in last frame
1582 if (cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1] != INTRA_FRAME)
1584 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset + xd->mode_info_stride +1].as_int;
1585 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset + xd->mode_info_stride +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1586 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1];
1591 for(i=0; i< vcnt; i++)
1593 if(near_ref[near_sadidx[i]] != INTRA_FRAME)
1595 if(here->mbmi.ref_frame == near_ref[near_sadidx[i]])
1597 mv.as_int = near_mvs[near_sadidx[i]].as_int;
1610 for(i=0; i<vcnt; i++)
1612 mvx[i] = near_mvs[i].as_mv.row;
1613 mvy[i] = near_mvs[i].as_mv.col;
1616 insertsortmv(mvx, vcnt);
1617 insertsortmv(mvy, vcnt);
1618 mv.as_mv.row = mvx[vcnt/2];
1619 mv.as_mv.col = mvy[vcnt/2];
1622 //sr is set to 0 to allow calling function to decide the search range.
1627 /* Set up return values */
1628 mvp->as_int = mv.as_int;
1629 vp8_clamp_mv2(mvp, xd);
1632 void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, int recon_yoffset, int near_sadidx[])
1635 int near_sad[8] = {0}; // 0-cf above, 1-cf left, 2-cf aboveleft, 3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below
1637 //calculate sad for current frame 3 nearby MBs.
1638 if( xd->mb_to_top_edge==0 && xd->mb_to_left_edge ==0)
1640 near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX;
1641 }else if(xd->mb_to_top_edge==0)
1642 { //only has left MB for sad calculation.
1643 near_sad[0] = near_sad[2] = INT_MAX;
1644 near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
1645 }else if(xd->mb_to_left_edge ==0)
1646 { //only has left MB for sad calculation.
1647 near_sad[1] = near_sad[2] = INT_MAX;
1648 near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
1651 near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
1652 near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
1653 near_sad[2] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, xd->dst.y_buffer - xd->dst.y_stride *16 -16,xd->dst.y_stride, 0x7fffffff);
1656 if(cpi->common.last_frame_type != KEY_FRAME)
1658 //calculate sad for last frame 5 nearby MBs.
1659 unsigned char *pre_y_buffer = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset;
1660 int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride;
1662 if(xd->mb_to_top_edge==0) near_sad[4] = INT_MAX;
1663 if(xd->mb_to_left_edge ==0) near_sad[5] = INT_MAX;
1664 if(xd->mb_to_right_edge ==0) near_sad[6] = INT_MAX;
1665 if(xd->mb_to_bottom_edge==0) near_sad[7] = INT_MAX;
1667 if(near_sad[4] != INT_MAX)
1668 near_sad[4] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer - pre_y_stride *16, pre_y_stride, 0x7fffffff);
1669 if(near_sad[5] != INT_MAX)
1670 near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer - 16, pre_y_stride, 0x7fffffff);
1671 near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer, pre_y_stride, 0x7fffffff);
1672 if(near_sad[6] != INT_MAX)
1673 near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer + 16, pre_y_stride, 0x7fffffff);
1674 if(near_sad[7] != INT_MAX)
1675 near_sad[7] = cpi->fn_ptr[BLOCK_16X16].sdf(x->src.y_buffer, x->src.y_stride, pre_y_buffer + pre_y_stride *16, pre_y_stride, 0x7fffffff);
1678 if(cpi->common.last_frame_type != KEY_FRAME)
1680 insertsortsad(near_sad, near_sadidx, 8);
1683 insertsortsad(near_sad, near_sadidx, 3);
1687 static void rd_update_mvcount(VP8_COMP *cpi, MACROBLOCK *x, int_mv *best_ref_mv)
1689 if (x->e_mbd.mode_info_context->mbmi.mode == SPLITMV)
1693 for (i = 0; i < x->partition_info->count; i++)
1695 if (x->partition_info->bmi[i].mode == NEW4X4)
1697 cpi->MVcount[0][mv_max+((x->partition_info->bmi[i].mv.as_mv.row
1698 - best_ref_mv->as_mv.row) >> 1)]++;
1699 cpi->MVcount[1][mv_max+((x->partition_info->bmi[i].mv.as_mv.col
1700 - best_ref_mv->as_mv.col) >> 1)]++;
1704 else if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
1706 cpi->MVcount[0][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.row
1707 - best_ref_mv->as_mv.row) >> 1)]++;
1708 cpi->MVcount[1][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.col
1709 - best_ref_mv->as_mv.col) >> 1)]++;
1713 void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
1715 BLOCK *b = &x->block[0];
1716 BLOCKD *d = &x->e_mbd.block[0];
1717 MACROBLOCKD *xd = &x->e_mbd;
1718 B_MODE_INFO best_bmodes[16];
1719 MB_MODE_INFO best_mbmode;
1720 PARTITION_INFO best_partition;
1722 int_mv mode_mv[MB_MODE_COUNT];
1723 MB_PREDICTION_MODE this_mode;
1725 int best_mode_index = 0;
1732 int best_rd = INT_MAX;
1733 int best_intra_rd = INT_MAX;
1734 int ref_frame_cost[MAX_REF_FRAMES];
1735 int rate2, distortion2;
1736 int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
1737 int rate_y, UNINITIALIZED_IS_SAFE(rate_uv);
1739 int best_yrd = INT_MAX;
1741 //int all_rds[MAX_MODES]; // Experimental debug code.
1742 //int all_rates[MAX_MODES];
1743 //int all_dist[MAX_MODES];
1744 //int intermodecost[MAX_MODES];
1746 MB_PREDICTION_MODE uv_intra_mode;
1748 int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
1750 int sr=0; //search range got from mv_pred(). It uses step_param levels. (0-7)
1752 int_mv frame_nearest_mv[4];
1753 int_mv frame_near_mv[4];
1754 int_mv frame_best_ref_mv[4];
1755 int frame_mdcounts[4][4];
1756 int frame_lf_or_gf[4];
1757 unsigned char *y_buffer[4];
1758 unsigned char *u_buffer[4];
1759 unsigned char *v_buffer[4];
1761 vpx_memset(&best_mbmode, 0, sizeof(best_mbmode));
1763 if (cpi->ref_frame_flags & VP8_LAST_FLAG)
1765 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
1767 vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &frame_nearest_mv[LAST_FRAME], &frame_near_mv[LAST_FRAME],
1768 &frame_best_ref_mv[LAST_FRAME], frame_mdcounts[LAST_FRAME], LAST_FRAME, cpi->common.ref_frame_sign_bias);
1770 y_buffer[LAST_FRAME] = lst_yv12->y_buffer + recon_yoffset;
1771 u_buffer[LAST_FRAME] = lst_yv12->u_buffer + recon_uvoffset;
1772 v_buffer[LAST_FRAME] = lst_yv12->v_buffer + recon_uvoffset;
1774 frame_lf_or_gf[LAST_FRAME] = 0;
1777 if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
1779 YV12_BUFFER_CONFIG *gld_yv12 = &cpi->common.yv12_fb[cpi->common.gld_fb_idx];
1781 vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &frame_nearest_mv[GOLDEN_FRAME], &frame_near_mv[GOLDEN_FRAME],
1782 &frame_best_ref_mv[GOLDEN_FRAME], frame_mdcounts[GOLDEN_FRAME], GOLDEN_FRAME, cpi->common.ref_frame_sign_bias);
1784 y_buffer[GOLDEN_FRAME] = gld_yv12->y_buffer + recon_yoffset;
1785 u_buffer[GOLDEN_FRAME] = gld_yv12->u_buffer + recon_uvoffset;
1786 v_buffer[GOLDEN_FRAME] = gld_yv12->v_buffer + recon_uvoffset;
1788 frame_lf_or_gf[GOLDEN_FRAME] = 1;
1791 if (cpi->ref_frame_flags & VP8_ALT_FLAG)
1793 YV12_BUFFER_CONFIG *alt_yv12 = &cpi->common.yv12_fb[cpi->common.alt_fb_idx];
1795 vp8_find_near_mvs(&x->e_mbd, x->e_mbd.mode_info_context, &frame_nearest_mv[ALTREF_FRAME], &frame_near_mv[ALTREF_FRAME],
1796 &frame_best_ref_mv[ALTREF_FRAME], frame_mdcounts[ALTREF_FRAME], ALTREF_FRAME, cpi->common.ref_frame_sign_bias);
1798 y_buffer[ALTREF_FRAME] = alt_yv12->y_buffer + recon_yoffset;
1799 u_buffer[ALTREF_FRAME] = alt_yv12->u_buffer + recon_uvoffset;
1800 v_buffer[ALTREF_FRAME] = alt_yv12->v_buffer + recon_uvoffset;
1802 frame_lf_or_gf[ALTREF_FRAME] = 1;
1805 *returnintra = INT_MAX;
1806 cpi->mbs_tested_so_far++; // Count of the number of MBs tested so far this frame
1810 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(cpi->prob_intra_coded);
1812 // Special case treatment when GF and ARF are not sensible options for reference
1813 if (cpi->ref_frame_flags == VP8_LAST_FLAG)
1815 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1816 + vp8_cost_zero(255);
1817 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1819 + vp8_cost_zero(128);
1820 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1822 + vp8_cost_one(128);
1826 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1827 + vp8_cost_zero(cpi->prob_last_coded);
1828 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1829 + vp8_cost_one(cpi->prob_last_coded)
1830 + vp8_cost_zero(cpi->prob_gf_coded);
1831 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1832 + vp8_cost_one(cpi->prob_last_coded)
1833 + vp8_cost_one(cpi->prob_gf_coded);
1836 vpx_memset(mode_mv, 0, sizeof(mode_mv));
1838 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
1839 vp8_rd_pick_intra_mbuv_mode(cpi, x, &uv_intra_rate, &uv_intra_rate_tokenonly, &uv_intra_distortion);
1840 uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
1842 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
1844 int this_rd = INT_MAX;
1845 int lf_or_gf = 0; // Lat Frame (01) or gf/arf (1)
1846 int disable_skip = 0;
1849 // Experimental debug code.
1850 // Record of rd values recorded for this MB. -1 indicates not measured
1851 //all_rds[mode_index] = -1;
1852 //all_rates[mode_index] = -1;
1853 //all_dist[mode_index] = -1;
1854 //intermodecost[mode_index] = -1;
1856 // Test best rd so far against threshold for trying this mode.
1857 if (best_rd <= cpi->rd_threshes[mode_index])
1860 // These variables hold are rolling total cost and distortion for this mode
1864 this_mode = vp8_mode_order[mode_index];
1866 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
1867 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
1868 x->e_mbd.mode_info_context->mbmi.ref_frame = vp8_ref_frame_order[mode_index];
1870 // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
1871 // unless ARNR filtering is enabled in which case we want
1872 // an unfiltered alternative
1873 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
1875 if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
1879 /* everything but intra */
1880 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
1882 x->e_mbd.pre.y_buffer = y_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
1883 x->e_mbd.pre.u_buffer = u_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
1884 x->e_mbd.pre.v_buffer = v_buffer[x->e_mbd.mode_info_context->mbmi.ref_frame];
1885 mode_mv[NEARESTMV] = frame_nearest_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
1886 mode_mv[NEARMV] = frame_near_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
1887 best_ref_mv = frame_best_ref_mv[x->e_mbd.mode_info_context->mbmi.ref_frame];
1888 vpx_memcpy(mdcounts, frame_mdcounts[x->e_mbd.mode_info_context->mbmi.ref_frame], sizeof(mdcounts));
1889 lf_or_gf = frame_lf_or_gf[x->e_mbd.mode_info_context->mbmi.ref_frame];
1892 if(x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
1896 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
1900 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
1901 x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
1903 /* adjust mvp to make sure it is within MV range */
1905 best_ref_mv.as_mv.col - MAX_FULL_PEL_VAL,
1906 best_ref_mv.as_mv.col + MAX_FULL_PEL_VAL,
1907 best_ref_mv.as_mv.row - MAX_FULL_PEL_VAL,
1908 best_ref_mv.as_mv.row + MAX_FULL_PEL_VAL);
1911 // Check to see if the testing frequency for this mode is at its max
1912 // If so then prevent it from being tested and increase the threshold for its testing
1913 if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
1915 if (cpi->mbs_tested_so_far <= cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index])
1917 // Increase the threshold for coding this mode to make it less likely to be chosen
1918 cpi->rd_thresh_mult[mode_index] += 4;
1920 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1921 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1923 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
1929 // 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
1930 cpi->mode_test_hit_counts[mode_index] ++;
1932 // Experimental code. Special case for gf and arf zeromv modes. Increase zbin size to supress noise
1933 if (cpi->zbin_mode_boost_enabled)
1935 if ( vp8_ref_frame_order[mode_index] == INTRA_FRAME )
1936 cpi->zbin_mode_boost = 0;
1939 if (vp8_mode_order[mode_index] == ZEROMV)
1941 if (vp8_ref_frame_order[mode_index] != LAST_FRAME)
1942 cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
1944 cpi->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
1946 else if (vp8_mode_order[mode_index] == SPLITMV)
1947 cpi->zbin_mode_boost = 0;
1949 cpi->zbin_mode_boost = MV_ZBIN_BOOST;
1952 vp8_update_zbin_extra(cpi, x);
1961 // Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
1962 tmp_rd = vp8_rd_pick_intra4x4mby_modes(cpi, x, &rate, &rate_y, &distortion, best_yrd);
1964 distortion2 += distortion;
1966 if(tmp_rd < best_yrd)
1968 rate2 += uv_intra_rate;
1969 rate_uv = uv_intra_rate_tokenonly;
1970 distortion2 += uv_intra_distortion;
1971 distortion_uv = uv_intra_distortion;
1986 this_rd_thresh = (x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME) ? cpi->rd_threshes[THR_NEWMV] : cpi->rd_threshes[THR_NEWA];
1987 this_rd_thresh = (x->e_mbd.mode_info_context->mbmi.ref_frame == GOLDEN_FRAME) ? cpi->rd_threshes[THR_NEWG]: this_rd_thresh;
1989 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv,
1991 &rate, &rate_y, &distortion, this_rd_thresh) ;
1994 distortion2 += distortion;
1996 // If even the 'Y' rd value of split is higher than best so far then dont bother looking at UV
1997 if (tmp_rd < best_yrd)
1999 // Now work out UV cost and add it in
2000 vp8_rd_inter_uv(cpi, x, &rate_uv, &distortion_uv, cpi->common.full_pixel);
2002 distortion2 += distortion_uv;
2015 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
2016 RECON_INVOKE(&cpi->common.rtcd.recon, build_intra_predictors_mby)
2018 macro_block_yrd(x, &rate_y, &distortion, IF_RTCD(&cpi->rtcd.encodemb)) ;
2020 distortion2 += distortion;
2021 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
2022 rate2 += uv_intra_rate;
2023 rate_uv = uv_intra_rate_tokenonly;
2024 distortion2 += uv_intra_distortion;
2025 distortion_uv = uv_intra_distortion;
2031 int bestsme = INT_MAX;
2032 int step_param = cpi->sf.first_step;
2035 int do_refine=1; /* If last step (1-away) of n-step search doesn't pick the center point as the best match,
2036 we will do a final 1-away diamond refining search */
2038 int sadpb = x->sadperbit16;
2040 int col_min = (best_ref_mv.as_mv.col - MAX_FULL_PEL_VAL) >>3;
2041 int col_max = (best_ref_mv.as_mv.col + MAX_FULL_PEL_VAL) >>3;
2042 int row_min = (best_ref_mv.as_mv.row - MAX_FULL_PEL_VAL) >>3;
2043 int row_max = (best_ref_mv.as_mv.row + MAX_FULL_PEL_VAL) >>3;
2045 int tmp_col_min = x->mv_col_min;
2046 int tmp_col_max = x->mv_col_max;
2047 int tmp_row_min = x->mv_row_min;
2048 int tmp_row_max = x->mv_row_max;
2050 // Get intersection of UMV window and valid MV window to reduce # of checks in diamond search.
2051 if (x->mv_col_min < col_min )
2052 x->mv_col_min = col_min;
2053 if (x->mv_col_max > col_max )
2054 x->mv_col_max = col_max;
2055 if (x->mv_row_min < row_min )
2056 x->mv_row_min = row_min;
2057 if (x->mv_row_max > row_max )
2058 x->mv_row_max = row_max;
2060 //adjust search range according to sr from mv prediction
2064 // Initial step/diamond search
2065 if (cpi->sf.search_method == HEX)
2067 bestsme = vp8_hex_search(x, b, d, &best_ref_mv, &d->bmi.mv, step_param, sadpb/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvsadcost, x->mvcost, &best_ref_mv);
2068 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2072 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
2073 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2075 // Further step/diamond searches as necessary
2077 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
2082 /* If there won't be more n-step search, check to see if refining search is needed. */
2083 if (n > further_steps)
2086 while (n < further_steps)
2094 thissme = cpi->diamond_search_sad(x, b, d, &mvp, &d->bmi.mv, step_param + n, sadpb / 4/*x->errorperbit*/, &num00, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv); //sadpb = 9
2096 /* check to see if refining search is needed. */
2097 if (num00 > (further_steps-n))
2100 if (thissme < bestsme)
2103 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2107 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
2113 /* final 1-away diamond refining search */
2118 //It seems not a good way to set search_range. Need further investigation.
2119 //search_range = MAXF(abs((mvp.row>>3) - d->bmi.mv.as_mv.row), abs((mvp.col>>3) - d->bmi.mv.as_mv.col));
2122 //thissme = cpi->full_search_sad(x, b, d, &d->bmi.mv.as_mv, sadpb, search_range, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv);
2123 thissme = cpi->refining_search_sad(x, b, d, &d->bmi.mv, sadpb/4, search_range, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv);
2125 if (thissme < bestsme)
2128 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2132 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
2136 x->mv_col_min = tmp_col_min;
2137 x->mv_col_max = tmp_col_max;
2138 x->mv_row_min = tmp_row_min;
2139 x->mv_row_max = tmp_row_max;
2141 if (bestsme < INT_MAX)
2143 int dis; /* TODO: use dis in distortion calculation later. */
2145 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv, x->errorperbit / 4, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &dis, &sse);
2148 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2150 // Add the new motion vector cost to our rolling cost variable
2151 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
2156 // Clip "next_nearest" so that it does not extend to far out of image
2157 vp8_clamp_mv2(&mode_mv[this_mode], xd);
2159 // Do not bother proceeding if the vector (from newmv,nearest or near) is 0,0 as this should then be coded using the zeromv mode.
2160 if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) && (mode_mv[this_mode].as_int == 0))
2165 // Trap vectors that reach beyond the UMV borders
2166 // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
2167 // because of the lack of break statements in the previous two cases.
2168 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) ||
2169 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
2172 vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
2173 vp8_build_inter16x16_predictors_mby(&x->e_mbd);
2175 if (cpi->active_map_enabled && x->active_ptr[0] == 0) {
2178 else if (x->encode_breakout)
2182 int threshold = (xd->block[0].dequant[1]
2183 * xd->block[0].dequant[1] >>4);
2185 if(threshold < x->encode_breakout)
2186 threshold = x->encode_breakout;
2188 VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)
2189 (x->src.y_buffer, x->src.y_stride,
2190 x->e_mbd.predictor, 16, &sse, &sum);
2192 if (sse < threshold)
2194 // Check u and v to make sure skip is ok
2196 /* If theres is no codeable 2nd order dc
2197 or a very small uniform pixel change change */
2198 if (abs(sum) < (xd->block[24].dequant[0]<<2)||
2199 ((sum * sum>>8) > sse && abs(sum) <128))
2201 sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
2203 if (sse2 * 2 < threshold)
2206 distortion2 = sse + sse2;
2209 /* for best_yrd calculation */
2211 distortion_uv = sse2;
2214 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
2223 //intermodecost[mode_index] = vp8_cost_mv_ref(this_mode, mdcounts); // Experimental debug code
2225 // Add in the Mv/mode cost
2226 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
2228 // Y cost and distortion
2229 macro_block_yrd(x, &rate_y, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
2231 distortion2 += distortion;
2233 // UV cost and distortion
2234 vp8_rd_inter_uv(cpi, x, &rate_uv, &distortion_uv, cpi->common.full_pixel);
2236 distortion2 += distortion_uv;
2243 // Where skip is allowable add in the default per mb cost for the no skip case.
2244 // where we then decide to skip we have to delete this and replace it with the
2245 // cost of signallying a skip
2246 if (cpi->common.mb_no_coeff_skip)
2248 other_cost += vp8_cost_bit(cpi->prob_skip_false, 0);
2249 rate2 += other_cost;
2252 // Estimate the reference frame signaling cost and add it to the rolling cost variable.
2253 rate2 += ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
2257 // Test for the condition where skip block will be activated because there are no non zero coefficients and make any necessary adjustment for rate
2258 if (cpi->common.mb_no_coeff_skip)
2264 for (i = 0; i <= 24; i++)
2266 tteob += x->e_mbd.block[i].eob;
2271 rate2 -= (rate_y + rate_uv);
2272 //for best_yrd calculation
2275 // Back out no skip flag costing and add in skip flag costing
2276 if (cpi->prob_skip_false)
2280 prob_skip_cost = vp8_cost_bit(cpi->prob_skip_false, 1);
2281 prob_skip_cost -= vp8_cost_bit(cpi->prob_skip_false, 0);
2282 rate2 += prob_skip_cost;
2283 other_cost += prob_skip_cost;
2287 // Calculate the final RD estimate for this mode
2288 this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2);
2291 // Experimental debug code.
2292 //all_rds[mode_index] = this_rd;
2293 //all_rates[mode_index] = rate2;
2294 //all_dist[mode_index] = distortion2;
2296 // Keep record of best intra distortion
2297 if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) &&
2298 (this_rd < best_intra_rd) )
2300 best_intra_rd = this_rd;
2301 *returnintra = distortion2 ;
2304 // Did this mode help.. i.i is it the new best mode
2305 if (this_rd < best_rd || x->skip)
2307 // Note index of best mode so far
2308 best_mode_index = mode_index;
2310 if (this_mode <= B_PRED)
2312 x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode;
2313 /* required for left and above block mv */
2314 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2317 other_cost += ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
2319 /* Calculate the final y RD estimate for this mode */
2320 best_yrd = RDCOST(x->rdmult, x->rddiv, (rate2-rate_uv-other_cost),
2321 (distortion2-distortion_uv));
2323 *returnrate = rate2;
2324 *returndistortion = distortion2;
2326 vpx_memcpy(&best_mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
2327 vpx_memcpy(&best_partition, x->partition_info, sizeof(PARTITION_INFO));
2329 for (i = 0; i < 16; i++)
2331 vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
2334 // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
2335 cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
2336 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2339 // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
2342 cpi->rd_thresh_mult[mode_index] += 4;
2344 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2345 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2347 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2355 // Reduce the activation RD thresholds for the best choice mode
2356 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
2358 int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 2);
2360 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;
2361 cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
2363 // If we chose a split mode then reset the new MV thresholds as well
2364 /*if ( vp8_mode_order[best_mode_index] == SPLITMV )
2366 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWMV] >> 4);
2367 cpi->rd_thresh_mult[THR_NEWMV] = (cpi->rd_thresh_mult[THR_NEWMV] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWMV]-best_adjustment: MIN_THRESHMULT;
2368 cpi->rd_threshes[THR_NEWMV] = (cpi->rd_baseline_thresh[THR_NEWMV] >> 7) * cpi->rd_thresh_mult[THR_NEWMV];
2370 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWG] >> 4);
2371 cpi->rd_thresh_mult[THR_NEWG] = (cpi->rd_thresh_mult[THR_NEWG] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWG]-best_adjustment: MIN_THRESHMULT;
2372 cpi->rd_threshes[THR_NEWG] = (cpi->rd_baseline_thresh[THR_NEWG] >> 7) * cpi->rd_thresh_mult[THR_NEWG];
2374 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWA] >> 4);
2375 cpi->rd_thresh_mult[THR_NEWA] = (cpi->rd_thresh_mult[THR_NEWA] >= (MIN_THRESHMULT+best_adjustment)) ? cpi->rd_thresh_mult[THR_NEWA]-best_adjustment: MIN_THRESHMULT;
2376 cpi->rd_threshes[THR_NEWA] = (cpi->rd_baseline_thresh[THR_NEWA] >> 7) * cpi->rd_thresh_mult[THR_NEWA];
2381 // Note how often each mode chosen as best
2382 cpi->mode_chosen_counts[best_mode_index] ++;
2385 if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
2387 best_mbmode.mode = ZEROMV;
2388 best_mbmode.ref_frame = ALTREF_FRAME;
2389 best_mbmode.mv.as_int = 0;
2390 best_mbmode.uv_mode = 0;
2391 best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
2392 best_mbmode.partitioning = 0;
2394 vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2395 vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
2397 for (i = 0; i < 16; i++)
2399 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
2402 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2407 vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2409 if (best_mbmode.mode == B_PRED)
2411 for (i = 0; i < 16; i++)
2412 x->e_mbd.block[i].bmi.mode = best_bmodes[i].mode;
2415 if (best_mbmode.mode == SPLITMV)
2417 for (i = 0; i < 16; i++)
2418 x->e_mbd.block[i].bmi.mv.as_int = best_bmodes[i].mv.as_int;
2420 vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
2422 x->e_mbd.mode_info_context->mbmi.mv.as_int =
2423 x->partition_info->bmi[15].mv.as_int;
2426 rd_update_mvcount(cpi, x, &frame_best_ref_mv[xd->mode_info_context->mbmi.ref_frame]);