2 * Copyright (c) 2010 The VP8 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.
19 #include "treewriter.h"
21 #include "modecosts.h"
22 #include "encodeintra.h"
23 #include "entropymode.h"
24 #include "reconinter.h"
25 #include "reconintra.h"
26 #include "reconintra4x4.h"
27 #include "findnearmv.h"
35 #include "vpx_mem/vpx_mem.h"
37 #include "systemdependent.h"
39 #define DIAMONDSEARCH 1
40 #if CONFIG_RUNTIME_CPU_DETECT
41 #define IF_RTCD(x) (x)
43 #define IF_RTCD(x) NULL
47 void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
50 #define RDFUNC(RM,DM,R,D,target_rd) ( ((128+(R)*(RM)) >> 8) + (DM)*(D) )
51 /*int RDFUNC( int RM,int DM, int R, int D, int target_r )
55 rd_value = ( ((128+(R)*(RM)) >> 8) + (DM)*(D) );
60 #define UVRDFUNC(RM,DM,R,D,target_r) RDFUNC(RM,DM,R,D,target_r)
62 #define RDCOST(RM,DM,R,D) ( ((128+(R)*(RM)) >> 8) + (DM)*(D) )
64 #define MAXF(a,b) (((a) > (b)) ? (a) : (b))
67 extern const TOKENEXTRA vp8_dct_value_tokens[DCT_MAX_VALUE*2];
68 extern const TOKENEXTRA *vp8_dct_value_tokens_ptr;
69 extern int vp8_dct_value_cost[DCT_MAX_VALUE*2];
70 extern int *vp8_dct_value_cost_ptr;
73 const int vp8_auto_speed_thresh[17] =
94 const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
126 const MV_REFERENCE_FRAME vp8_ref_frame_order[MAX_MODES] =
158 static void fill_token_costs(
159 unsigned int c [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens],
160 const vp8_prob p [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1]
166 for (i = 0; i < BLOCK_TYPES; i++)
167 for (j = 0; j < COEF_BANDS; j++)
168 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
170 vp8_cost_tokens((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
174 static int rd_iifactor [ 32 ] = { 16, 16, 16, 12, 8, 4, 2, 0,
175 0, 0, 0, 0, 0, 0, 0, 0,
176 0, 0, 0, 0, 0, 0, 0, 0,
177 0, 0, 0, 0, 0, 0, 0, 0,
183 // The values in this table should be reviewed
184 static int sad_per_bit16lut[128] =
186 4, 4, 4, 4, 4, 4, 4, 4, // 4
187 4, 4, 4, 4, 4, 4, 4, 4, // 1
188 4, 4, 4, 4, 4, 4, 4, 4, // 2
189 4, 4, 4, 4, 4, 4, 4, 4, // 3
190 4, 4, 4, 4, 4, 4, 4, 4, // 4
191 4, 4, 12, 12, 13, 13, 14, 14, // 5
192 14, 14, 14, 15, 15, 15, 15, 15, // 6
193 15, 15, 15, 15, 15, 15, 15, 15, // 7
194 15, 15, 15, 15, 15, 16, 16, 16, // 8
195 16, 16, 18, 18, 18, 18, 19, 19, // 9
196 19, 19, 19, 19, 19, 19, 19, 19, // 10
197 20, 20, 22, 22, 22, 22, 21, 21, // 11
198 22, 22, 22, 22, 22, 22, 22, 22, // 12
199 22, 22, 22, 22, 22, 22, 22, 22, // 13
200 22, 22, 22, 22, 22, 22, 22, 22, // 14
201 22, 22, 22, 22, 22, 22, 22, 22, // 15
204 static int sad_per_bit4lut[128] =
206 4, 4, 4, 4, 4, 4, 4, 4, // 4
207 4, 4, 4, 4, 4, 4, 4, 4, // 1
208 4, 4, 4, 4, 4, 4, 4, 4, // 2
209 4, 4, 4, 4, 4, 4, 4, 4, // 3
210 4, 4, 4, 4, 4, 4, 4, 4, // 4
211 4, 4, 15, 15, 15, 15, 16, 16, // 5
212 16, 17, 17, 17, 17, 17, 17, 17, // 6
213 17, 17, 19, 19, 22, 22, 21, 21, // 7
214 23, 23, 23, 23, 23, 24, 24, 24, // 8
215 25, 25, 27, 27, 27, 27, 28, 28, // 9
216 28, 28, 29, 29, 29, 29, 29, 29, // 10
217 30, 30, 31, 31, 31, 31, 32, 32, // 11
218 34, 34, 34, 34, 34, 34, 34, 34, // 12
219 34, 34, 34, 34, 34, 34, 34, 34, // 13
220 34, 34, 34, 34, 34, 34, 34, 34, // 14
221 34, 34, 34, 34, 34, 34, 34, 34, // 15
224 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
226 cpi->mb.sadperbit16 = sad_per_bit16lut[QIndex];
227 cpi->mb.sadperbit4 = sad_per_bit4lut[QIndex];
230 void vp8_initialize_rd_consts(VP8_COMP *cpi, int Qvalue)
237 int capped_q = (Qvalue < 160) ? Qvalue : 160;
239 vp8_clear_system_state(); //__asm emms;
241 cpi->RDMULT = (int)((0.00007 * (capped_q * capped_q * capped_q * capped_q)) - (0.0125 * (capped_q * capped_q * capped_q)) +
242 (2.25 * (capped_q * capped_q)) - (12.5 * capped_q) + 25.0);
244 if (cpi->RDMULT < 25)
249 if (cpi->common.frame_type == KEY_FRAME)
250 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[0]) / 16;
251 else if (cpi->next_iiratio > 31)
252 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) / 16;
254 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[cpi->next_iiratio]) / 16;
258 // Extend rate multiplier along side quantizer zbin increases
259 if (cpi->zbin_over_quant > 0)
261 // Extend rate multiplier along side quantizer zbin increases
262 if (cpi->zbin_over_quant > 0)
264 double oq_factor = pow(1.006, cpi->zbin_over_quant);
266 if (oq_factor > (1.0 + ((double)cpi->zbin_over_quant / 64.0)))
267 oq_factor = (1.0 + (double)cpi->zbin_over_quant / 64.0);
269 cpi->RDMULT = (int)(oq_factor * cpi->RDMULT);
273 cpi->mb.errorperbit = (cpi->RDMULT / 100);
275 if (cpi->mb.errorperbit < 1)
276 cpi->mb.errorperbit = 1;
278 vp8_set_speed_features(cpi);
280 if (cpi->common.simpler_lpf)
281 cpi->common.filter_type = SIMPLE_LOOPFILTER;
283 q = (int)pow(Qvalue, 1.25);
288 if (cpi->ref_frame_flags == VP8_ALT_FLAG)
290 thresh = &cpi->rd_threshes[THR_NEWA];
291 threshmult = cpi->sf.thresh_mult[THR_NEWA];
293 else if (cpi->ref_frame_flags == VP8_GOLD_FLAG)
295 thresh = &cpi->rd_threshes[THR_NEWG];
296 threshmult = cpi->sf.thresh_mult[THR_NEWG];
300 thresh = &cpi->rd_threshes[THR_NEWMV];
301 threshmult = cpi->sf.thresh_mult[THR_NEWMV];
304 if (cpi->RDMULT > 1000)
309 for (i = 0; i < MAX_MODES; i++)
311 if (cpi->sf.thresh_mult[i] < INT_MAX)
313 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
317 cpi->rd_threshes[i] = INT_MAX;
320 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
327 for (i = 0; i < MAX_MODES; i++)
329 if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
331 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
335 cpi->rd_threshes[i] = INT_MAX;
338 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
344 (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs
347 vp8_init_mode_costs(cpi);
351 void vp8_auto_select_speed(VP8_COMP *cpi)
353 int used = cpi->oxcf.cpu_used;
355 int milliseconds_for_compress = (int)(1000000 / cpi->oxcf.frame_rate);
357 milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
365 f = fopen("speed.stt", "a");
366 fprintf(f, " %8ld %10ld %10ld %10ld\n",
367 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
374 // this is done during parameter valid check
381 if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
383 if (cpi->avg_pick_mode_time == 0)
389 if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
392 cpi->avg_pick_mode_time = 0;
393 cpi->avg_encode_time = 0;
401 if (milliseconds_for_compress * 100 > cpi->avg_encode_time * vp8_auto_speed_thresh[cpi->Speed])
404 cpi->avg_pick_mode_time = 0;
405 cpi->avg_encode_time = 0;
407 // In real-time mode, cpi->speed is in [4, 16].
408 if (cpi->Speed < 4) //if ( cpi->Speed < 0 )
410 cpi->Speed = 4; //cpi->Speed = 0;
423 cpi->avg_pick_mode_time = 0;
424 cpi->avg_encode_time = 0;
428 int vp8_block_error_c(short *coeff, short *dqcoeff)
433 for (i = 0; i < 16; i++)
435 int this_diff = coeff[i] - dqcoeff[i];
436 error += this_diff * this_diff;
442 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
447 int berror, error = 0;
449 for (i = 0; i < 16; i++)
452 bd = &mb->e_mbd.block[i];
456 for (j = dc; j < 16; j++)
458 int this_diff = be->coeff[j] - bd->dqcoeff[j];
459 berror += this_diff * this_diff;
468 int vp8_mbuverror_c(MACROBLOCK *mb)
478 for (i = 16; i < 24; i++)
481 bd = &mb->e_mbd.block[i];
483 error += vp8_block_error_c(be->coeff, bd->dqcoeff);
489 #if !(CONFIG_REALTIME_ONLY)
490 static int macro_block_max_error(MACROBLOCK *mb)
498 dc = !(mb->e_mbd.mbmi.mode == B_PRED || mb->e_mbd.mbmi.mode == SPLITMV);
500 for (i = 0; i < 16; i++)
506 for (j = dc; j < 16; j++)
508 int this_diff = be->coeff[j];
509 berror += this_diff * this_diff;
515 for (i = 16; i < 24; i++)
520 for (j = 0; j < 16; j++)
522 int this_diff = be->coeff[j];
523 berror += this_diff * this_diff;
536 for (j = 0; j < 16; j++)
538 int this_diff = be->coeff[j];
539 berror += this_diff * this_diff;
550 int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd)
552 unsigned char *uptr, *vptr;
553 unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
554 unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
555 int uv_stride = x->block[16].src_stride;
557 unsigned int sse1 = 0;
558 unsigned int sse2 = 0;
562 int pre_stride = x->e_mbd.block[16].pre_stride;
564 vp8_build_uvmvs(&x->e_mbd, 0);
565 mv_row = x->e_mbd.block[16].bmi.mv.as_mv.row;
566 mv_col = x->e_mbd.block[16].bmi.mv.as_mv.col;
568 offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
569 uptr = x->e_mbd.pre.u_buffer + offset;
570 vptr = x->e_mbd.pre.v_buffer + offset;
572 if ((mv_row | mv_col) & 7)
574 VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
575 VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
580 VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
581 VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
589 #if !(CONFIG_REALTIME_ONLY)
590 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
592 int c = !type; /* start at coef 0, unless Y with Y2 */
594 int pt ; /* surrounding block/prev coef predictor */
596 short *qcoeff_ptr = b->qcoeff;
598 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
600 # define QC( I) ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )
605 int t = vp8_dct_value_tokens_ptr[v].Token;
606 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
607 cost += vp8_dct_value_cost_ptr[v];
608 pt = vp8_prev_token_class[t];
614 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
616 pt = (c != !type); // is eob first coefficient;
622 int vp8_rdcost_mby(MACROBLOCK *mb)
629 MACROBLOCKD *x = &mb->e_mbd;
631 vp8_setup_temp_context(&t, x->above_context[Y1CONTEXT], x->left_context[Y1CONTEXT], 4);
632 vp8_setup_temp_context(&t2, x->above_context[Y2CONTEXT], x->left_context[Y2CONTEXT], 1);
634 if (x->mbmi.mode == SPLITMV)
637 for (b = 0; b < 16; b++)
638 cost += cost_coeffs(mb, x->block + b, type,
639 t.a + vp8_block2above[b], t.l + vp8_block2left[b]);
641 if (x->mbmi.mode != SPLITMV)
642 cost += cost_coeffs(mb, x->block + 24, 1,
643 t2.a + vp8_block2above[24], t2.l + vp8_block2left[24]);
649 static void rd_pick_intra4x4block(
654 B_PREDICTION_MODE *best_mode,
655 B_PREDICTION_MODE above,
656 B_PREDICTION_MODE left,
664 B_PREDICTION_MODE mode;
665 int best_rd = INT_MAX; // 1<<30
668 unsigned int *mode_costs;
670 ENTROPY_CONTEXT ta = *a, tempa = *a;
671 ENTROPY_CONTEXT tl = *l, templ = *l;
674 if (x->e_mbd.frame_type == KEY_FRAME)
676 mode_costs = x->bmode_costs[above][left];
680 mode_costs = x->inter_bmode_costs;
683 for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
688 rate = mode_costs[mode];
689 vp8_encode_intra4x4block_rd(IF_RTCD(&cpi->rtcd), x, be, b, mode);
694 ratey = cost_coeffs(x, b, 3, &tempa, &templ);
696 distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(be->coeff, b->dqcoeff) >> 2;
698 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
700 if (this_rd < best_rd)
704 *bestdistortion = distortion;
712 b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
713 vp8_encode_intra4x4block_rd(IF_RTCD(&cpi->rtcd), x, be, b, b->bmi.mode);
718 int vp8_rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate, int *rate_y, int *Distortion)
720 MACROBLOCKD *const xd = &mb->e_mbd;
723 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
727 vp8_intra_prediction_down_copy(xd);
728 vp8_setup_temp_context(&t, xd->above_context[Y1CONTEXT], xd->left_context[Y1CONTEXT], 4);
730 for (i = 0; i < 16; i++)
732 MODE_INFO *const mic = xd->mode_info_context;
733 const int mis = xd->mode_info_stride;
734 const B_PREDICTION_MODE A = vp8_above_bmi(mic, i, mis)->mode;
735 const B_PREDICTION_MODE L = vp8_left_bmi(mic, i)->mode;
736 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
737 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
739 rd_pick_intra4x4block(
740 cpi, mb, mb->block + i, xd->block + i, &best_mode, A, L,
741 t.a + vp8_block2above[i],
742 t.l + vp8_block2left[i], &r, &ry, &d);
747 mic->bmi[i].mode = xd->block[i].bmi.mode = best_mode;
751 *rate_y += tot_rate_y;
752 *Distortion = distortion;
754 return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
757 int vp8_rd_pick_intra16x16mby_mode(VP8_COMP *cpi, MACROBLOCK *x, int *Rate, int *rate_y, int *Distortion)
760 MB_PREDICTION_MODE mode;
761 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
763 unsigned int distortion;
764 int best_rd = INT_MAX;
766 //Y Search for 16x16 intra prediction mode
767 for (mode = DC_PRED; mode <= TM_PRED; mode++)
773 x->e_mbd.mbmi.mode = mode;
775 rate += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.mode];
777 vp8_encode_intra16x16mbyrd(IF_RTCD(&cpi->rtcd), x);
779 ratey = vp8_rdcost_mby(x);
783 VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)(x->src.y_buffer, x->src.y_stride, x->e_mbd.dst.y_buffer, x->e_mbd.dst.y_stride, &distortion, &dummy);
785 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
787 if (this_rd < best_rd)
789 mode_selected = mode;
793 *Distortion = (int)distortion;
797 x->e_mbd.mbmi.mode = mode_selected;
802 static int rd_cost_mbuv(MACROBLOCK *mb)
807 MACROBLOCKD *x = &mb->e_mbd;
809 vp8_setup_temp_context(&t, x->above_context[UCONTEXT], x->left_context[UCONTEXT], 2);
810 vp8_setup_temp_context(&t2, x->above_context[VCONTEXT], x->left_context[VCONTEXT], 2);
812 for (b = 16; b < 20; b++)
813 cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
814 t.a + vp8_block2above[b], t.l + vp8_block2left[b]);
816 for (b = 20; b < 24; b++)
817 cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
818 t2.a + vp8_block2above[b], t2.l + vp8_block2left[b]);
824 unsigned int vp8_get_mbuvrecon_error(const vp8_variance_rtcd_vtable_t *rtcd, const MACROBLOCK *x) // sum of squares
826 unsigned int sse0, sse1;
828 VARIANCE_INVOKE(rtcd, get8x8var)(x->src.u_buffer, x->src.uv_stride, x->e_mbd.dst.u_buffer, x->e_mbd.dst.uv_stride, &sse0, &sum0);
829 VARIANCE_INVOKE(rtcd, get8x8var)(x->src.v_buffer, x->src.uv_stride, x->e_mbd.dst.v_buffer, x->e_mbd.dst.uv_stride, &sse1, &sum1);
830 return (sse0 + sse1);
833 static int vp8_rd_inter_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *distortion, int fullpixel)
835 vp8_build_uvmvs(&x->e_mbd, fullpixel);
836 vp8_encode_inter16x16uvrd(IF_RTCD(&cpi->rtcd), x);
839 *rate = rd_cost_mbuv(x);
840 *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
842 return UVRDFUNC(x->rdmult, x->rddiv, *rate, *distortion, cpi->target_bits_per_mb);
845 int vp8_rd_pick_intra_mbuv_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, int *distortion)
847 MB_PREDICTION_MODE mode;
848 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
849 int best_rd = INT_MAX;
850 int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
853 for (mode = DC_PRED; mode <= TM_PRED; mode++)
859 x->e_mbd.mbmi.uv_mode = mode;
860 vp8_encode_intra16x16mbuvrd(IF_RTCD(&cpi->rtcd), x);
862 rate_to = rd_cost_mbuv(x);
863 rate = rate_to + x->intra_uv_mode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.uv_mode];
865 distortion = vp8_get_mbuvrecon_error(IF_RTCD(&cpi->rtcd.variance), x);
867 this_rd = UVRDFUNC(x->rdmult, x->rddiv, rate, distortion, cpi->target_bits_per_mb);
869 if (this_rd < best_rd)
874 *rate_tokenonly = rate_to;
875 mode_selected = mode;
882 x->e_mbd.mbmi.uv_mode = mode_selected;
887 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
889 vp8_prob p [VP8_MVREFS-1];
890 assert(NEARESTMV <= m && m <= SPLITMV);
891 vp8_mv_ref_probs(p, near_mv_ref_ct);
892 return vp8_cost_token(vp8_mv_ref_tree, p, VP8_MVREFENCODINGS + m);
895 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv)
899 x->e_mbd.mbmi.mode = mb;
900 x->e_mbd.mbmi.mv.as_mv.row = mv->row;
901 x->e_mbd.mbmi.mv.as_mv.col = mv->col;
903 for (i = 0; i < 16; i++)
905 B_MODE_INFO *bmi = &x->e_mbd.block[i].bmi;
906 bmi->mode = (B_PREDICTION_MODE) mb;
907 bmi->mv.as_mv.row = mv->row;
908 bmi->mv.as_mv.col = mv->col;
912 #if !(CONFIG_REALTIME_ONLY)
913 int vp8_count_labels(int const *labelings)
918 for (i = 0; i < 16; i++)
920 if (labelings[i] > count)
921 count = labelings[i];
928 static int labels2mode(
930 int const *labelings, int which_label,
931 B_PREDICTION_MODE this_mode,
932 MV *this_mv, MV *best_ref_mv,
936 MACROBLOCKD *const xd = & x->e_mbd;
937 MODE_INFO *const mic = xd->mode_info_context;
938 const int mis = xd->mode_info_stride;
943 /* We have to be careful retrieving previously-encoded motion vectors.
944 Ones from this macroblock have to be pulled from the BLOCKD array
945 as they have not yet made it to the bmi array in our MB_MODE_INFO. */
951 BLOCKD *const d = xd->block + i;
952 const int row = i >> 2, col = i & 3;
956 if (labelings[i] != which_label)
959 if (col && labelings[i] == labelings[i-1])
961 else if (row && labelings[i] == labelings[i-4])
965 // the only time we should do costing for new motion vector or mode
966 // is when we are on a new label (jbb May 08, 2007)
967 switch (m = this_mode)
970 thismvcost = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
973 *this_mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
976 *this_mv = row ? d[-4].bmi.mv.as_mv : vp8_above_bmi(mic, i, mis)->mv.as_mv;
979 this_mv->row = this_mv->col = 0;
985 if (m == ABOVE4X4) // replace above with left if same
987 const MV mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
989 if (mv.row == this_mv->row && mv.col == this_mv->col)
993 cost = x->inter_bmode_costs[ m];
997 d->bmi.mv.as_mv = *this_mv;
1002 cost += thismvcost ;
1006 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels, int which_label, TEMP_CONTEXT *t)
1010 MACROBLOCKD *x = &mb->e_mbd;
1013 for (b = 0; b < 16; b++)
1014 if (labels[ b] == which_label)
1015 cost += cost_coeffs(mb, x->block + b, 3,
1016 t->a + vp8_block2above[b],
1017 t->l + vp8_block2left[b]);
1022 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label, const vp8_encodemb_rtcd_vtable_t *rtcd)
1025 unsigned int distortion = 0;
1027 for (i = 0; i < 16; i++)
1029 if (labels[i] == which_label)
1031 BLOCKD *bd = &x->e_mbd.block[i];
1032 BLOCK *be = &x->block[i];
1035 vp8_build_inter_predictors_b(bd, 16, x->e_mbd.subpixel_predict);
1036 ENCODEMB_INVOKE(rtcd, subb)(be, bd, 16);
1037 x->short_fdct4x4rd(be->src_diff, be->coeff, 32);
1039 // set to 0 no way to account for 2nd order DC so discount
1041 x->quantize_b(be, bd);
1043 distortion += ENCODEMB_INVOKE(rtcd, berr)(be->coeff, bd->dqcoeff);
1050 static void macro_block_yrd(MACROBLOCK *mb, int *Rate, int *Distortion, const vp8_encodemb_rtcd_vtable_t *rtcd)
1053 MACROBLOCKD *const x = &mb->e_mbd;
1054 BLOCK *const mb_y2 = mb->block + 24;
1055 BLOCKD *const x_y2 = x->block + 24;
1056 short *Y2DCPtr = mb_y2->src_diff;
1060 ENCODEMB_INVOKE(rtcd, submby)(mb->src_diff, mb->src.y_buffer, mb->e_mbd.predictor, mb->src.y_stride);
1062 // Fdct and building the 2nd order block
1063 for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
1065 mb->short_fdct8x4rd(beptr->src_diff, beptr->coeff, 32);
1066 *Y2DCPtr++ = beptr->coeff[0];
1067 *Y2DCPtr++ = beptr->coeff[16];
1071 if (x->mbmi.mode != SPLITMV)
1073 mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
1077 for (b = 0; b < 16; b++)
1079 mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
1082 // DC predication and Quantization of 2nd Order block
1083 if (x->mbmi.mode != SPLITMV)
1087 mb->quantize_b(mb_y2, x_y2);
1092 if (x->mbmi.mode == SPLITMV)
1093 d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 0) << 2;
1096 d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 1) << 2;
1097 d += ENCODEMB_INVOKE(rtcd, berr)(mb_y2->coeff, x_y2->dqcoeff);
1100 *Distortion = (d >> 4);
1103 *Rate = vp8_rdcost_mby(mb);
1106 static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, MV *best_ref_mv, int best_rd, int *mdcounts, int *returntotrate, int *returnyrate, int *returndistortion, int compressor_speed, int *mvcost[2], int mvthresh, int fullpixel)
1108 int i, segmentation;
1109 B_PREDICTION_MODE this_mode;
1110 MACROBLOCKD *xc = &x->e_mbd;
1111 BLOCK *b = &x->block[0];
1112 BLOCKD *d = &x->e_mbd.block[0];
1113 BLOCK *c = &x->block[0];
1114 BLOCKD *e = &x->e_mbd.block[0];
1116 int best_segment_rd = INT_MAX;
1122 int bestsegmentyrate = 0;
1124 // FIX TO Rd error outrange bug PGW 9 june 2004
1125 B_PREDICTION_MODE bmodes[16] = {ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1126 ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1127 ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1128 ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4
1134 for (segmentation = 0; segmentation < VP8_NUMMBSPLITS; segmentation++)
1137 int this_segment_rd = 0;
1138 int label_mv_thresh;
1142 int UNINITIALIZED_IS_SAFE(sseshift);
1143 int segmentyrate = 0;
1145 vp8_variance_fn_ptr_t v_fn_ptr;
1149 vp8_setup_temp_context(&t, xc->above_context[Y1CONTEXT], xc->left_context[Y1CONTEXT], 4);
1154 switch (segmentation)
1157 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
1158 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
1159 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
1160 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
1161 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
1165 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
1166 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
1167 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
1168 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
1169 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
1173 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
1174 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
1175 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
1176 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
1177 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
1181 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
1182 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
1183 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
1184 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
1185 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
1190 labels = vp8_mbsplits[segmentation];
1191 label_count = vp8_count_labels(labels);
1193 // 64 makes this threshold really big effectively
1194 // making it so that we very rarely check mvs on
1195 // segments. setting this to 1 would make mv thresh
1196 // roughly equal to what it is for macroblocks
1197 label_mv_thresh = 1 * mvthresh / label_count ;
1199 // Segmentation method overheads
1200 rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
1202 rate += vp8_cost_mv_ref(SPLITMV, mdcounts);
1204 this_segment_rd += RDFUNC(x->rdmult, x->rddiv, rate, 0, cpi->target_bits_per_mb);
1207 for (i = 0; i < label_count; i++)
1209 MV mode_mv[B_MODE_COUNT];
1210 int best_label_rd = INT_MAX;
1211 B_PREDICTION_MODE mode_selected = ZERO4X4;
1213 int bestlabelyrate = 0;
1216 d = &x->e_mbd.block[0];
1220 for (j = 0; j < 16; j++)
1225 e = &x->e_mbd.block[j];
1227 // search for the best motion vector on this segment
1228 for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
1236 vp8_setup_temp_context(&ts, &t.a[0], &t.l[0], 4);
1238 if (this_mode == NEW4X4)
1241 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
1244 int bestsme = INT_MAX;
1247 // Is the best so far sufficiently good that we cant justify doing and new motion search.
1248 if (best_label_rd < label_mv_thresh)
1252 int sadpb = x->sadperbit4;
1254 if (cpi->sf.search_method == HEX)
1255 bestsme = vp8_hex_search(x, c, e, best_ref_mv, &mode_mv[NEW4X4], step_param, sadpb/*x->errorperbit*/, &num00, v_fn_ptr.vf, v_fn_ptr.sdf, x->mvsadcost, mvcost);
1258 bestsme = cpi->diamond_search_sad(x, c, e, best_ref_mv, &mode_mv[NEW4X4], step_param, sadpb / 2/*x->errorperbit*/, &num00, &v_fn_ptr, x->mvsadcost, mvcost);
1263 while (n < further_steps)
1271 thissme = cpi->diamond_search_sad(x, c, e, best_ref_mv, &temp_mv, step_param + n, sadpb / 2/*x->errorperbit*/, &num00, &v_fn_ptr, x->mvsadcost, mvcost);
1273 if (thissme < bestsme)
1276 mode_mv[NEW4X4].row = temp_mv.row;
1277 mode_mv[NEW4X4].col = temp_mv.col;
1283 // Should we do a full search (best quality only)
1284 if ((compressor_speed == 0) && (bestsme >> sseshift) > 4000)
1286 thissme = cpi->full_search_sad(x, c, e, best_ref_mv, sadpb / 4, 16, &v_fn_ptr, x->mvcost, x->mvsadcost);
1288 if (thissme < bestsme)
1291 mode_mv[NEW4X4] = e->bmi.mv.as_mv;
1295 // The full search result is actually worse so re-instate the previous best vector
1296 e->bmi.mv.as_mv = mode_mv[NEW4X4];
1301 if (bestsme < INT_MAX)
1304 cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], best_ref_mv, x->errorperbit / 2, v_fn_ptr.svf, v_fn_ptr.vf, mvcost);
1306 vp8_skip_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], best_ref_mv, x->errorperbit, v_fn_ptr.svf, v_fn_ptr.vf, mvcost);
1310 rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode], best_ref_mv, mvcost);
1312 // Trap vectors that reach beyond the UMV borders
1313 if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
1314 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
1319 distortion = vp8_encode_inter_mb_segment(x, labels, i, IF_RTCD(&cpi->rtcd.encodemb)) / 4;
1321 labelyrate = rdcost_mbsegment_y(x, labels, i, &ts);
1324 this_rd = RDFUNC(x->rdmult, x->rddiv, rate, distortion, cpi->target_bits_per_mb);
1326 if (this_rd < best_label_rd)
1330 bestlabelyrate = labelyrate;
1331 mode_selected = this_mode;
1332 best_label_rd = this_rd;
1333 vp8_setup_temp_context(&tb, &ts.a[0], &ts.l[0], 4);
1338 vp8_setup_temp_context(&t, &tb.a[0], &tb.l[0], 4);
1340 labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected], best_ref_mv, mvcost);
1344 segmentyrate += bestlabelyrate;
1345 this_segment_rd += best_label_rd;
1347 if ((this_segment_rd > best_rd) || (this_segment_rd > best_segment_rd))
1351 if ((this_segment_rd <= best_rd) && (this_segment_rd < best_segment_rd))
1355 bestsegmentyrate = segmentyrate;
1356 best_segment_rd = this_segment_rd;
1357 best_seg = segmentation;
1359 // store everything needed to come back to this!!
1360 for (i = 0; i < 16; i++)
1362 BLOCKD *bd = &x->e_mbd.block[i];
1364 bmvs[i] = bd->bmi.mv.as_mv;
1365 bmodes[i] = bd->bmi.mode;
1371 // set it to the best
1372 for (i = 0; i < 16; i++)
1374 BLOCKD *bd = &x->e_mbd.block[i];
1376 bd->bmi.mv.as_mv = bmvs[i];
1377 bd->bmi.mode = bmodes[i];
1381 // Trap cases where the best split mode has all vectors coded 0,0 (or all the same)
1386 for (i = 1; i < 16; i++)
1388 if ((bmvs[i].col != bmvs[i-1].col) || (bmvs[i].row != bmvs[i-1].row))
1397 best_segment_rd = INT_MAX;
1401 *returntotrate = bsr;
1402 *returndistortion = bsd;
1403 *returnyrate = bestsegmentyrate;
1408 labels = vp8_mbsplits[best_seg];
1409 x->e_mbd.mbmi.partitioning = best_seg;
1410 x->e_mbd.mbmi.partition_count = vp8_count_labels(labels);
1412 for (i = 0; i < x->e_mbd.mbmi.partition_count; i++)
1416 for (j = 0; j < 16; j++)
1422 x->e_mbd.mbmi.partition_bmi[i].mode = x->e_mbd.block[j].bmi.mode;
1423 x->e_mbd.mbmi.partition_bmi[i].mv.as_mv = x->e_mbd.block[j].bmi.mv.as_mv;
1426 return best_segment_rd;
1430 int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
1432 BLOCK *b = &x->block[0];
1433 BLOCKD *d = &x->e_mbd.block[0];
1434 MACROBLOCKD *xd = &x->e_mbd;
1435 B_MODE_INFO best_bmodes[16];
1436 MB_MODE_INFO best_mbmode;
1438 MV mode_mv[MB_MODE_COUNT];
1439 MB_PREDICTION_MODE this_mode;
1441 int best_mode_index = 0;
1448 int best_rd = INT_MAX; // 1 << 30;
1449 int ref_frame_cost[MAX_REF_FRAMES];
1450 int rate2, distortion2;
1451 int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
1452 int rate_y, UNINITIALIZED_IS_SAFE(rate_uv);
1454 //int all_rds[MAX_MODES]; // Experimental debug code.
1455 //int all_rates[MAX_MODES];
1456 //int all_dist[MAX_MODES];
1457 //int intermodecost[MAX_MODES];
1459 MB_PREDICTION_MODE uv_intra_mode;
1462 int uvintra_eob = 0;
1464 int force_no_skip = 0;
1466 *returnintra = INT_MAX;
1468 cpi->mbs_tested_so_far++; // Count of the number of MBs tested so far this frame
1472 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(cpi->prob_intra_coded);
1474 // Experimental code
1475 // Adjust the RD multiplier based on the best case distortion we saw in the most recently coded mb
1476 //if ( (cpi->last_mb_distortion) > 0 && (cpi->target_bits_per_mb > 0) )
1480 //tmprdmult = (cpi->last_mb_distortion * 256) / ((cpi->av_per_frame_bandwidth*256)/cpi->common.MBs);
1481 tmprdmult = (cpi->last_mb_distortion * 256) / cpi->target_bits_per_mb;
1482 //tmprdmult = tmprdmult;
1484 //if ( tmprdmult > cpi->RDMULT * 2 )
1485 // tmprdmult = cpi->RDMULT * 2;
1486 //else if ( tmprdmult < cpi->RDMULT / 2 )
1487 // tmprdmult = cpi->RDMULT / 2;
1489 //tmprdmult = (tmprdmult < 25) ? 25 : tmprdmult;
1491 //x->rdmult = tmprdmult;
1495 // Special case treatment when GF and ARF are not sensible options for reference
1496 if (cpi->ref_frame_flags == VP8_LAST_FLAG)
1498 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1499 + vp8_cost_zero(255);
1500 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1502 + vp8_cost_zero(128);
1503 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1505 + vp8_cost_one(128);
1509 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1510 + vp8_cost_zero(cpi->prob_last_coded);
1511 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1512 + vp8_cost_one(cpi->prob_last_coded)
1513 + vp8_cost_zero(cpi->prob_gf_coded);
1514 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1515 + vp8_cost_one(cpi->prob_last_coded)
1516 + vp8_cost_one(cpi->prob_gf_coded);
1519 vpx_memset(mode_mv, 0, sizeof(mode_mv));
1521 x->e_mbd.mbmi.ref_frame = INTRA_FRAME;
1522 vp8_rd_pick_intra_mbuv_mode(cpi, x, &uv_intra_rate, &uv_intra_rate_tokenonly, &uv_intra_distortion);
1523 uv_intra_mode = x->e_mbd.mbmi.uv_mode;
1527 for (i = 16; i < 24; i++)
1528 uvintra_eob += x->e_mbd.block[i].eob;
1531 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
1534 int this_rd = INT_MAX;
1535 int lf_or_gf = 0; // Lat Frame (01) or gf/arf (1)
1536 int disable_skip = 0;
1540 // Experimental debug code.
1541 // Record of rd values recorded for this MB. -1 indicates not measured
1542 //all_rds[mode_index] = -1;
1543 //all_rates[mode_index] = -1;
1544 //all_dist[mode_index] = -1;
1545 //intermodecost[mode_index] = -1;
1547 // Test best rd so far against threshold for trying this mode.
1548 if (best_rd <= cpi->rd_threshes[mode_index])
1553 // These variables hold are rolling total cost and distortion for this mode
1557 // Where skip is allowable add in the default per mb cost for the no skip case.
1558 // where we then decide to skip we have to delete this and replace it with the
1559 // cost of signallying a skip
1560 if (cpi->common.mb_no_coeff_skip)
1562 rate2 += vp8_cost_bit(cpi->prob_skip_false, 0);
1565 this_mode = vp8_mode_order[mode_index];
1567 x->e_mbd.mbmi.mode = this_mode;
1568 x->e_mbd.mbmi.uv_mode = DC_PRED;
1569 x->e_mbd.mbmi.ref_frame = vp8_ref_frame_order[mode_index];
1571 //Only consider ZEROMV/ALTREF_FRAME for alt ref frame.
1572 if (cpi->is_src_frame_alt_ref)
1574 if (this_mode != ZEROMV || x->e_mbd.mbmi.ref_frame != ALTREF_FRAME)
1578 if (x->e_mbd.mbmi.ref_frame == LAST_FRAME)
1580 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
1583 lf_or_gf = 0; // Local last frame vs Golden frame flag
1585 // Set up pointers for this macro block into the previous frame recon buffer
1586 x->e_mbd.pre.y_buffer = cpi->common.last_frame.y_buffer + recon_yoffset;
1587 x->e_mbd.pre.u_buffer = cpi->common.last_frame.u_buffer + recon_uvoffset;
1588 x->e_mbd.pre.v_buffer = cpi->common.last_frame.v_buffer + recon_uvoffset;
1590 else if (x->e_mbd.mbmi.ref_frame == GOLDEN_FRAME)
1593 // not supposed to reference gold frame
1594 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
1597 lf_or_gf = 1; // Local last frame vs Golden frame flag
1599 // Set up pointers for this macro block into the previous frame recon buffer
1600 x->e_mbd.pre.y_buffer = cpi->common.golden_frame.y_buffer + recon_yoffset;
1601 x->e_mbd.pre.u_buffer = cpi->common.golden_frame.u_buffer + recon_uvoffset;
1602 x->e_mbd.pre.v_buffer = cpi->common.golden_frame.v_buffer + recon_uvoffset;
1604 else if (x->e_mbd.mbmi.ref_frame == ALTREF_FRAME)
1606 // not supposed to reference alt ref frame
1607 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
1610 //if ( !cpi->source_alt_ref_active )
1613 lf_or_gf = 1; // Local last frame vs Golden frame flag
1615 // Set up pointers for this macro block into the previous frame recon buffer
1616 x->e_mbd.pre.y_buffer = cpi->common.alt_ref_frame.y_buffer + recon_yoffset;
1617 x->e_mbd.pre.u_buffer = cpi->common.alt_ref_frame.u_buffer + recon_uvoffset;
1618 x->e_mbd.pre.v_buffer = cpi->common.alt_ref_frame.v_buffer + recon_uvoffset;
1621 vp8_find_near_mvs(&x->e_mbd,
1622 x->e_mbd.mode_info_context,
1623 &mode_mv[NEARESTMV], &mode_mv[NEARMV], &best_ref_mv,
1624 mdcounts, x->e_mbd.mbmi.ref_frame, cpi->common.ref_frame_sign_bias);
1627 // Estimate the reference frame signaling cost and add it to the rolling cost variable.
1628 frame_cost = ref_frame_cost[x->e_mbd.mbmi.ref_frame];
1629 rate2 += frame_cost;
1631 if (this_mode <= B_PRED)
1633 for (i = 0; i < 16; i++)
1635 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
1639 // Check to see if the testing frequency for this mode is at its max
1640 // If so then prevent it from being tested and increase the threshold for its testing
1641 if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
1643 if (cpi->mbs_tested_so_far <= cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index])
1645 // Increase the threshold for coding this mode to make it less likely to be chosen
1646 cpi->rd_thresh_mult[mode_index] += 4;
1648 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1649 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1651 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
1657 // 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
1658 cpi->mode_test_hit_counts[mode_index] ++;
1660 // Experimental code. Special case for gf and arf zeromv modes. Increase zbin size to supress noise
1661 if (cpi->zbin_mode_boost_enabled)
1663 if ((vp8_mode_order[mode_index] == ZEROMV) && (vp8_ref_frame_order[mode_index] != LAST_FRAME))
1664 cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
1666 cpi->zbin_mode_boost = 0;
1668 vp8cx_mb_init_quantizer(cpi, x);
1675 // Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
1676 vp8_rd_pick_intra4x4mby_modes(cpi, x, &rate, &rate_y, &distortion);
1679 distortion2 += distortion;
1680 rate2 += uv_intra_rate;
1681 rate_uv = uv_intra_rate_tokenonly;
1682 distortion2 += uv_intra_distortion;
1687 int frame_cost_rd = RDFUNC(x->rdmult, x->rddiv, frame_cost, 0, cpi->target_bits_per_mb);
1688 int saved_rate = rate2;
1690 // vp8_rd_pick_best_mbsegmentation looks only at Y and does not account for frame_cost.
1691 // (best_rd - frame_cost_rd) is thus a conservative breakout number.
1692 int breakout_rd = best_rd - frame_cost_rd;
1695 if (x->e_mbd.mbmi.ref_frame == LAST_FRAME)
1696 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWMV], cpi->common.full_pixel) ;
1697 else if (x->e_mbd.mbmi.ref_frame == GOLDEN_FRAME)
1698 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWG], cpi->common.full_pixel) ;
1700 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, breakout_rd, mdcounts, &rate, &rate_y, &distortion, cpi->compressor_speed, x->mvcost, cpi->rd_threshes[THR_NEWA], cpi->common.full_pixel) ;
1703 distortion2 += distortion;
1705 // If even the 'Y' rd value of split is higher than best so far then dont bother looking at UV
1706 if (tmp_rd < breakout_rd)
1708 // Now work out UV cost and add it in
1709 vp8_rd_inter_uv(cpi, x, &rate, &distortion, cpi->common.full_pixel);
1712 distortion2 += distortion;
1721 // Trap cases where the best split mode has all vectors coded 0,0 (or all the same)
1726 for (i = 1; i < 16; i++)
1728 BLOCKD *bd = &x->e_mbd.block[i];
1730 if (bd->bmi.mv.as_int != x->e_mbd.block[0].bmi.mv.as_int) //(bmvs[i].col != bmvs[i-1].col) || (bmvs[i].row != bmvs[i-1].row ) )
1739 // reset mode and mv and jump to newmv
1743 mode_mv[NEWMV].row = x->e_mbd.block[0].bmi.mv.as_mv.row;
1744 mode_mv[NEWMV].col = x->e_mbd.block[0].bmi.mv.as_mv.col;
1745 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
1750 // trap cases where the 8x8s can be promoted to 8x16s or 16x8s
1751 if (0)//x->e_mbd.mbmi.partition_count == 4)
1754 if (x->e_mbd.mbmi.partition_bmi[0].mv.as_int == x->e_mbd.mbmi.partition_bmi[1].mv.as_int
1755 && x->e_mbd.mbmi.partition_bmi[2].mv.as_int == x->e_mbd.mbmi.partition_bmi[3].mv.as_int)
1757 const int *labels = vp8_mbsplits[2];
1758 x->e_mbd.mbmi.partitioning = 0;
1759 rate -= vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + 2);
1760 rate += vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings);
1761 //rate -= x->inter_bmode_costs[ x->e_mbd.mbmi.partition_bmi[1]];
1762 //rate -= x->inter_bmode_costs[ x->e_mbd.mbmi.partition_bmi[3]];
1763 x->e_mbd.mbmi.partition_bmi[1] = x->e_mbd.mbmi.partition_bmi[2];
1773 x->e_mbd.mbmi.ref_frame = INTRA_FRAME;
1774 vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
1776 macro_block_yrd(x, &rate, &distortion, IF_RTCD(&cpi->rtcd.encodemb)) ;
1779 distortion2 += distortion;
1780 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.mode];
1781 rate2 += uv_intra_rate;
1782 rate_uv = uv_intra_rate_tokenonly;
1783 distortion2 += uv_intra_distortion;
1789 // Decrement full search counter
1790 if (cpi->check_freq[lf_or_gf] > 0)
1791 cpi->check_freq[lf_or_gf] --;
1795 int bestsme = INT_MAX;
1796 int step_param = cpi->sf.first_step;
1801 // Work out how long a search we should do
1802 search_range = MAXF(abs(best_ref_mv.col), abs(best_ref_mv.row)) >> 3;
1804 if (search_range >= x->vector_range)
1805 x->vector_range = search_range;
1806 else if (x->vector_range > cpi->sf.min_fs_radius)
1809 // Initial step/diamond search
1811 int sadpb = x->sadperbit16;
1813 if (cpi->sf.search_method == HEX)
1815 bestsme = vp8_hex_search(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param, sadpb/*x->errorperbit*/, &num00, cpi->fn_ptr.vf, cpi->fn_ptr.sdf, x->mvsadcost, x->mvcost);
1816 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1817 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1821 bestsme = cpi->diamond_search_sad(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param, sadpb / 2/*x->errorperbit*/, &num00, &cpi->fn_ptr, x->mvsadcost, x->mvcost); //sadpb < 9
1822 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1823 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1825 // Further step/diamond searches as necessary
1827 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
1832 while (n < further_steps)
1840 thissme = cpi->diamond_search_sad(x, b, d, &best_ref_mv, &d->bmi.mv.as_mv, step_param + n, sadpb / 4/*x->errorperbit*/, &num00, &cpi->fn_ptr, x->mvsadcost, x->mvcost); //sadpb = 9
1842 if (thissme < bestsme)
1845 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1846 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1850 d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
1851 d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
1859 // Should we do a full search
1860 if (!cpi->check_freq[lf_or_gf] || cpi->do_full[lf_or_gf])
1863 int full_flag_thresh = 0;
1865 // Update x->vector_range based on best vector found in step search
1866 search_range = MAXF(abs(d->bmi.mv.as_mv.row), abs(d->bmi.mv.as_mv.col));
1868 if (search_range > x->vector_range)
1869 x->vector_range = search_range;
1871 search_range = x->vector_range;
1874 search_range = (search_range > cpi->sf.max_fs_radius) ? cpi->sf.max_fs_radius : search_range;
1876 int sadpb = x->sadperbit16 >> 2;
1877 thissme = cpi->full_search_sad(x, b, d, &best_ref_mv, sadpb, search_range, &cpi->fn_ptr, x->mvcost, x->mvsadcost);
1880 // Barrier threshold to initiating full search
1881 // full_flag_thresh = 10 + (thissme >> 7);
1882 if ((thissme + full_flag_thresh) < bestsme)
1884 cpi->do_full[lf_or_gf] ++;
1887 else if (thissme < bestsme)
1891 cpi->do_full[lf_or_gf] = cpi->do_full[lf_or_gf] >> 1;
1892 cpi->check_freq[lf_or_gf] = cpi->sf.full_freq[lf_or_gf];
1894 // The full search result is actually worse so re-instate the previous best vector
1895 d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
1896 d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
1900 if (bestsme < INT_MAX)
1901 // cpi->find_fractional_mv_step(x,b,d,&d->bmi.mv.as_mv,&best_ref_mv,x->errorperbit/2,cpi->fn_ptr.svf,cpi->fn_ptr.vf,x->mvcost); // normal mvc=11
1902 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv.as_mv, &best_ref_mv, x->errorperbit / 4, cpi->fn_ptr.svf, cpi->fn_ptr.vf, x->mvcost);
1904 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1905 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1907 // Add the new motion vector cost to our rolling cost variable
1908 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
1915 // Clip "next_nearest" so that it does not extend to far out of image
1916 if (mode_mv[this_mode].col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN))
1917 mode_mv[this_mode].col = xd->mb_to_left_edge - LEFT_TOP_MARGIN;
1918 else if (mode_mv[this_mode].col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN)
1919 mode_mv[this_mode].col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN;
1921 if (mode_mv[this_mode].row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN))
1922 mode_mv[this_mode].row = xd->mb_to_top_edge - LEFT_TOP_MARGIN;
1923 else if (mode_mv[this_mode].row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN)
1924 mode_mv[this_mode].row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN;
1926 // 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.
1927 if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) &&
1928 ((mode_mv[this_mode].row == 0) && (mode_mv[this_mode].col == 0)))
1935 // Trap vectors that reach beyond the UMV borders
1936 // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
1937 // because of the lack of break statements in the previous two cases.
1938 if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
1939 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
1942 vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
1943 vp8_build_inter_predictors_mby(&x->e_mbd);
1944 VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)(x->src.y_buffer, x->src.y_stride, x->e_mbd.predictor, 16, (unsigned int *)(&sse), &sum);
1946 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
1950 else if (sse < x->encode_breakout)
1952 // Check u and v to make sure skip is ok
1955 sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
1957 if (sse2 * 2 < x->encode_breakout)
1963 disable_skip = 1; // We have no real rate data so trying to adjust for rate_y and rate_uv below will cause problems.
1964 this_rd = RDFUNC(x->rdmult, x->rddiv, rate2, distortion2, cpi->target_bits_per_mb);
1966 break; // (PGW) Move break here from below - for now at least
1972 //intermodecost[mode_index] = vp8_cost_mv_ref(this_mode, mdcounts); // Experimental debug code
1974 // Add in the Mv/mode cost
1975 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1977 // Y cost and distortion
1978 macro_block_yrd(x, &rate, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
1981 distortion2 += distortion;
1983 // UV cost and distortion
1984 vp8_rd_inter_uv(cpi, x, &rate, &distortion, cpi->common.full_pixel);
1987 distortion2 += distortion;
1996 // Test for the condition where skip block will be activated because there are no non zero coefficients and make any necessary adjustment for rate
1997 if (cpi->common.mb_no_coeff_skip)
2001 for (i = 0; i <= 24; i++)
2003 tteob += x->e_mbd.block[i].eob;
2009 rate2 -= (rate_y + rate_uv);
2011 // Back out no skip flag costing and add in skip flag costing
2012 if (cpi->prob_skip_false)
2014 rate2 += vp8_cost_bit(cpi->prob_skip_false, 1);
2015 rate2 -= vp8_cost_bit(cpi->prob_skip_false, 0);
2024 ratenotuseskip = rate_y + rate_uv + vp8_cost_bit(cpi->prob_skip_false, 0);
2025 rateuseskip = vp8_cost_bit(cpi->prob_skip_false, 1);
2027 if (1) // rateuseskip<ratenotuseskip)
2029 rate2 -= ratenotuseskip;
2030 rate2 += rateuseskip;
2050 // distortion when no coeff is encoded
2051 maxdistortion = macro_block_max_error(x);
2053 ratenotuseskip = rate_y + rate_uv + vp8_cost_bit(cpi->prob_skip_false, 0);
2054 rateuseskip = vp8_cost_bit(cpi->prob_skip_false, 1);
2056 minrate = rateuseskip - ratenotuseskip;
2058 skip_rd = RDFUNC(x->rdmult, x->rddiv, minrate, maxdistortion - distortion2, cpi->target_bits_per_mb);
2060 if (skip_rd + 50 < 0 && x->e_mbd.mbmi.ref_frame != INTRA_FRAME && rate_y + rate_uv < 4000)
2063 rate2 = rate2 + rateuseskip - ratenotuseskip;
2064 distortion2 = maxdistortion;
2077 // Calculate the final RD estimate for this mode
2078 this_rd = RDFUNC(x->rdmult, x->rddiv, rate2, distortion2, cpi->target_bits_per_mb);
2081 // Experimental debug code.
2082 //all_rds[mode_index] = this_rd;
2083 //all_rates[mode_index] = rate2;
2084 //all_dist[mode_index] = distortion2;
2086 if ((x->e_mbd.mbmi.ref_frame == INTRA_FRAME) && (this_rd < *returnintra))
2088 *returnintra = this_rd ;
2091 // Did this mode help.. i.i is it the new best mode
2092 if (this_rd < best_rd || x->skip)
2094 // Note index of best mode so far
2095 best_mode_index = mode_index;
2096 x->e_mbd.mbmi.force_no_skip = force_no_skip;
2098 if (this_mode <= B_PRED)
2100 x->e_mbd.mbmi.uv_mode = uv_intra_mode;
2103 *returnrate = rate2;
2104 *returndistortion = distortion2;
2106 vpx_memcpy(&best_mbmode, &x->e_mbd.mbmi, sizeof(MB_MODE_INFO));
2108 for (i = 0; i < 16; i++)
2110 vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
2113 // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
2114 cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
2115 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2118 // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
2121 cpi->rd_thresh_mult[mode_index] += 4;
2123 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2124 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2126 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2133 // Reduce the activation RD thresholds for the best choice mode
2134 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
2136 int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 2);
2138 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;
2139 cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
2141 // If we chose a split mode then reset the new MV thresholds as well
2142 /*if ( vp8_mode_order[best_mode_index] == SPLITMV )
2144 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWMV] >> 4);
2145 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;
2146 cpi->rd_threshes[THR_NEWMV] = (cpi->rd_baseline_thresh[THR_NEWMV] >> 7) * cpi->rd_thresh_mult[THR_NEWMV];
2148 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWG] >> 4);
2149 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;
2150 cpi->rd_threshes[THR_NEWG] = (cpi->rd_baseline_thresh[THR_NEWG] >> 7) * cpi->rd_thresh_mult[THR_NEWG];
2152 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWA] >> 4);
2153 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;
2154 cpi->rd_threshes[THR_NEWA] = (cpi->rd_baseline_thresh[THR_NEWA] >> 7) * cpi->rd_thresh_mult[THR_NEWA];
2159 // If we have chosen new mv or split then decay the full search check count more quickly.
2160 if ((vp8_mode_order[best_mode_index] == NEWMV) || (vp8_mode_order[best_mode_index] == SPLITMV))
2162 int lf_or_gf = (vp8_ref_frame_order[best_mode_index] == LAST_FRAME) ? 0 : 1;
2164 if (cpi->check_freq[lf_or_gf] && !cpi->do_full[lf_or_gf])
2166 cpi->check_freq[lf_or_gf] --;
2170 // Keep a record of best mode index that we chose
2171 cpi->last_best_mode_index = best_mode_index;
2173 // Note how often each mode chosen as best
2174 cpi->mode_chosen_counts[best_mode_index] ++;
2177 if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
2179 best_mbmode.mode = ZEROMV;
2180 best_mbmode.ref_frame = ALTREF_FRAME;
2181 best_mbmode.mv.as_int = 0;
2182 best_mbmode.uv_mode = 0;
2183 best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
2184 best_mbmode.partitioning = 0;
2185 best_mbmode.dc_diff = 0;
2187 vpx_memcpy(&x->e_mbd.mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2189 for (i = 0; i < 16; i++)
2191 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
2194 x->e_mbd.mbmi.mv.as_int = 0;
2201 vpx_memcpy(&x->e_mbd.mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2203 for (i = 0; i < 16; i++)
2205 vpx_memcpy(&x->e_mbd.block[i].bmi, &best_bmodes[i], sizeof(B_MODE_INFO));
2208 x->e_mbd.mbmi.mv.as_mv = x->e_mbd.block[15].bmi.mv.as_mv;