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 ] = { 4, 4, 3, 2, 1, 0, 0, 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,
181 // The values in this table should be reviewed
182 static int sad_per_bit16lut[128] =
184 4, 4, 4, 4, 4, 4, 4, 4, // 4
185 4, 4, 4, 4, 4, 4, 4, 4, // 1
186 4, 4, 4, 4, 4, 4, 4, 4, // 2
187 4, 4, 4, 4, 4, 4, 4, 4, // 3
188 4, 4, 4, 4, 4, 4, 4, 4, // 4
189 4, 4, 12, 12, 13, 13, 14, 14, // 5
190 14, 14, 14, 15, 15, 15, 15, 15, // 6
191 15, 15, 15, 15, 15, 15, 15, 15, // 7
192 15, 15, 15, 15, 15, 16, 16, 16, // 8
193 16, 16, 18, 18, 18, 18, 19, 19, // 9
194 19, 19, 19, 19, 19, 19, 19, 19, // 10
195 20, 20, 22, 22, 22, 22, 21, 21, // 11
196 22, 22, 22, 22, 22, 22, 22, 22, // 12
197 22, 22, 22, 22, 22, 22, 22, 22, // 13
198 22, 22, 22, 22, 22, 22, 22, 22, // 14
199 22, 22, 22, 22, 22, 22, 22, 22, // 15
202 static int sad_per_bit4lut[128] =
204 4, 4, 4, 4, 4, 4, 4, 4, // 4
205 4, 4, 4, 4, 4, 4, 4, 4, // 1
206 4, 4, 4, 4, 4, 4, 4, 4, // 2
207 4, 4, 4, 4, 4, 4, 4, 4, // 3
208 4, 4, 4, 4, 4, 4, 4, 4, // 4
209 4, 4, 15, 15, 15, 15, 16, 16, // 5
210 16, 17, 17, 17, 17, 17, 17, 17, // 6
211 17, 17, 19, 19, 22, 22, 21, 21, // 7
212 23, 23, 23, 23, 23, 24, 24, 24, // 8
213 25, 25, 27, 27, 27, 27, 28, 28, // 9
214 28, 28, 29, 29, 29, 29, 29, 29, // 10
215 30, 30, 31, 31, 31, 31, 32, 32, // 11
216 34, 34, 34, 34, 34, 34, 34, 34, // 12
217 34, 34, 34, 34, 34, 34, 34, 34, // 13
218 34, 34, 34, 34, 34, 34, 34, 34, // 14
219 34, 34, 34, 34, 34, 34, 34, 34, // 15
222 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
224 cpi->mb.sadperbit16 = sad_per_bit16lut[QIndex];
225 cpi->mb.sadperbit4 = sad_per_bit4lut[QIndex];
228 void vp8_initialize_rd_consts(VP8_COMP *cpi, int Qvalue)
235 int capped_q = (Qvalue < 160) ? Qvalue : 160;
237 vp8_clear_system_state(); //__asm emms;
239 cpi->RDMULT = (int)( (0.0001 * (capped_q * capped_q * capped_q * capped_q))
240 -(0.0125 * (capped_q * capped_q * capped_q))
241 +(3.25 * (capped_q * capped_q))
242 -(12.5 * capped_q) + 50.0);
244 if (cpi->RDMULT < 50)
247 if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
249 if (cpi->next_iiratio > 31)
250 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
252 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[cpi->next_iiratio]) >> 4;
256 // Extend rate multiplier along side quantizer zbin increases
257 if (cpi->zbin_over_quant > 0)
259 double oq_factor = pow(1.006, cpi->zbin_over_quant);
261 if (oq_factor > (1.0 + ((double)cpi->zbin_over_quant / 64.0)))
262 oq_factor = (1.0 + (double)cpi->zbin_over_quant / 64.0);
264 cpi->RDMULT = (int)(oq_factor * cpi->RDMULT);
267 cpi->mb.errorperbit = (cpi->RDMULT / 100);
269 if (cpi->mb.errorperbit < 1)
270 cpi->mb.errorperbit = 1;
272 vp8_set_speed_features(cpi);
274 if (cpi->common.simpler_lpf)
275 cpi->common.filter_type = SIMPLE_LOOPFILTER;
277 q = (int)pow(Qvalue, 1.25);
282 if (cpi->ref_frame_flags == VP8_ALT_FLAG)
284 thresh = &cpi->rd_threshes[THR_NEWA];
285 threshmult = cpi->sf.thresh_mult[THR_NEWA];
287 else if (cpi->ref_frame_flags == VP8_GOLD_FLAG)
289 thresh = &cpi->rd_threshes[THR_NEWG];
290 threshmult = cpi->sf.thresh_mult[THR_NEWG];
294 thresh = &cpi->rd_threshes[THR_NEWMV];
295 threshmult = cpi->sf.thresh_mult[THR_NEWMV];
298 if (cpi->RDMULT > 1000)
303 for (i = 0; i < MAX_MODES; i++)
305 if (cpi->sf.thresh_mult[i] < INT_MAX)
307 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
311 cpi->rd_threshes[i] = INT_MAX;
314 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
321 for (i = 0; i < MAX_MODES; i++)
323 if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
325 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
329 cpi->rd_threshes[i] = INT_MAX;
332 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
338 (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs
341 vp8_init_mode_costs(cpi);
345 void vp8_auto_select_speed(VP8_COMP *cpi)
347 int used = cpi->oxcf.cpu_used;
349 int milliseconds_for_compress = (int)(1000000 / cpi->oxcf.frame_rate);
351 milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
359 f = fopen("speed.stt", "a");
360 fprintf(f, " %8ld %10ld %10ld %10ld\n",
361 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
368 // this is done during parameter valid check
375 if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
377 if (cpi->avg_pick_mode_time == 0)
383 if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
386 cpi->avg_pick_mode_time = 0;
387 cpi->avg_encode_time = 0;
395 if (milliseconds_for_compress * 100 > cpi->avg_encode_time * vp8_auto_speed_thresh[cpi->Speed])
398 cpi->avg_pick_mode_time = 0;
399 cpi->avg_encode_time = 0;
401 // In real-time mode, cpi->speed is in [4, 16].
402 if (cpi->Speed < 4) //if ( cpi->Speed < 0 )
404 cpi->Speed = 4; //cpi->Speed = 0;
417 cpi->avg_pick_mode_time = 0;
418 cpi->avg_encode_time = 0;
422 int vp8_block_error_c(short *coeff, short *dqcoeff)
427 for (i = 0; i < 16; i++)
429 int this_diff = coeff[i] - dqcoeff[i];
430 error += this_diff * this_diff;
436 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
441 int berror, error = 0;
443 for (i = 0; i < 16; i++)
446 bd = &mb->e_mbd.block[i];
450 for (j = dc; j < 16; j++)
452 int this_diff = be->coeff[j] - bd->dqcoeff[j];
453 berror += this_diff * this_diff;
462 int vp8_mbuverror_c(MACROBLOCK *mb)
472 for (i = 16; i < 24; i++)
475 bd = &mb->e_mbd.block[i];
477 error += vp8_block_error_c(be->coeff, bd->dqcoeff);
483 #if !(CONFIG_REALTIME_ONLY)
484 static int macro_block_max_error(MACROBLOCK *mb)
492 dc = !(mb->e_mbd.mbmi.mode == B_PRED || mb->e_mbd.mbmi.mode == SPLITMV);
494 for (i = 0; i < 16; i++)
500 for (j = dc; j < 16; j++)
502 int this_diff = be->coeff[j];
503 berror += this_diff * this_diff;
509 for (i = 16; i < 24; i++)
514 for (j = 0; j < 16; j++)
516 int this_diff = be->coeff[j];
517 berror += this_diff * this_diff;
530 for (j = 0; j < 16; j++)
532 int this_diff = be->coeff[j];
533 berror += this_diff * this_diff;
544 int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd)
546 unsigned char *uptr, *vptr;
547 unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
548 unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
549 int uv_stride = x->block[16].src_stride;
551 unsigned int sse1 = 0;
552 unsigned int sse2 = 0;
556 int pre_stride = x->e_mbd.block[16].pre_stride;
558 vp8_build_uvmvs(&x->e_mbd, 0);
559 mv_row = x->e_mbd.block[16].bmi.mv.as_mv.row;
560 mv_col = x->e_mbd.block[16].bmi.mv.as_mv.col;
562 offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
563 uptr = x->e_mbd.pre.u_buffer + offset;
564 vptr = x->e_mbd.pre.v_buffer + offset;
566 if ((mv_row | mv_col) & 7)
568 VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
569 VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
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);
583 #if !(CONFIG_REALTIME_ONLY)
584 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
586 int c = !type; /* start at coef 0, unless Y with Y2 */
588 int pt ; /* surrounding block/prev coef predictor */
590 short *qcoeff_ptr = b->qcoeff;
592 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
594 # define QC( I) ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )
599 int t = vp8_dct_value_tokens_ptr[v].Token;
600 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
601 cost += vp8_dct_value_cost_ptr[v];
602 pt = vp8_prev_token_class[t];
608 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
610 pt = (c != !type); // is eob first coefficient;
616 int vp8_rdcost_mby(MACROBLOCK *mb)
623 MACROBLOCKD *x = &mb->e_mbd;
625 vp8_setup_temp_context(&t, x->above_context[Y1CONTEXT], x->left_context[Y1CONTEXT], 4);
626 vp8_setup_temp_context(&t2, x->above_context[Y2CONTEXT], x->left_context[Y2CONTEXT], 1);
628 if (x->mbmi.mode == SPLITMV)
631 for (b = 0; b < 16; b++)
632 cost += cost_coeffs(mb, x->block + b, type,
633 t.a + vp8_block2above[b], t.l + vp8_block2left[b]);
635 if (x->mbmi.mode != SPLITMV)
636 cost += cost_coeffs(mb, x->block + 24, 1,
637 t2.a + vp8_block2above[24], t2.l + vp8_block2left[24]);
643 static void rd_pick_intra4x4block(
648 B_PREDICTION_MODE *best_mode,
649 B_PREDICTION_MODE above,
650 B_PREDICTION_MODE left,
658 B_PREDICTION_MODE mode;
659 int best_rd = INT_MAX; // 1<<30
662 unsigned int *mode_costs;
664 ENTROPY_CONTEXT ta = *a, tempa = *a;
665 ENTROPY_CONTEXT tl = *l, templ = *l;
668 if (x->e_mbd.frame_type == KEY_FRAME)
670 mode_costs = x->bmode_costs[above][left];
674 mode_costs = x->inter_bmode_costs;
677 for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
682 rate = mode_costs[mode];
683 vp8_encode_intra4x4block_rd(IF_RTCD(&cpi->rtcd), x, be, b, mode);
688 ratey = cost_coeffs(x, b, 3, &tempa, &templ);
690 distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(be->coeff, b->dqcoeff) >> 2;
692 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
694 if (this_rd < best_rd)
698 *bestdistortion = distortion;
706 b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
707 vp8_encode_intra4x4block_rd(IF_RTCD(&cpi->rtcd), x, be, b, b->bmi.mode);
712 int vp8_rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate, int *rate_y, int *Distortion)
714 MACROBLOCKD *const xd = &mb->e_mbd;
717 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
721 vp8_intra_prediction_down_copy(xd);
722 vp8_setup_temp_context(&t, xd->above_context[Y1CONTEXT], xd->left_context[Y1CONTEXT], 4);
724 for (i = 0; i < 16; i++)
726 MODE_INFO *const mic = xd->mode_info_context;
727 const int mis = xd->mode_info_stride;
728 const B_PREDICTION_MODE A = vp8_above_bmi(mic, i, mis)->mode;
729 const B_PREDICTION_MODE L = vp8_left_bmi(mic, i)->mode;
730 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
731 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
733 rd_pick_intra4x4block(
734 cpi, mb, mb->block + i, xd->block + i, &best_mode, A, L,
735 t.a + vp8_block2above[i],
736 t.l + vp8_block2left[i], &r, &ry, &d);
741 mic->bmi[i].mode = xd->block[i].bmi.mode = best_mode;
745 *rate_y += tot_rate_y;
746 *Distortion = distortion;
748 return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
751 int vp8_rd_pick_intra16x16mby_mode(VP8_COMP *cpi, MACROBLOCK *x, int *Rate, int *rate_y, int *Distortion)
754 MB_PREDICTION_MODE mode;
755 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
757 unsigned int distortion;
758 int best_rd = INT_MAX;
760 //Y Search for 16x16 intra prediction mode
761 for (mode = DC_PRED; mode <= TM_PRED; mode++)
767 x->e_mbd.mbmi.mode = mode;
769 rate += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.mode];
771 vp8_encode_intra16x16mbyrd(IF_RTCD(&cpi->rtcd), x);
773 ratey = vp8_rdcost_mby(x);
777 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);
779 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
781 if (this_rd < best_rd)
783 mode_selected = mode;
787 *Distortion = (int)distortion;
791 x->e_mbd.mbmi.mode = mode_selected;
796 static int rd_cost_mbuv(MACROBLOCK *mb)
801 MACROBLOCKD *x = &mb->e_mbd;
803 vp8_setup_temp_context(&t, x->above_context[UCONTEXT], x->left_context[UCONTEXT], 2);
804 vp8_setup_temp_context(&t2, x->above_context[VCONTEXT], x->left_context[VCONTEXT], 2);
806 for (b = 16; b < 20; b++)
807 cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
808 t.a + vp8_block2above[b], t.l + vp8_block2left[b]);
810 for (b = 20; b < 24; b++)
811 cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
812 t2.a + vp8_block2above[b], t2.l + vp8_block2left[b]);
818 unsigned int vp8_get_mbuvrecon_error(const vp8_variance_rtcd_vtable_t *rtcd, const MACROBLOCK *x) // sum of squares
820 unsigned int sse0, sse1;
822 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);
823 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);
824 return (sse0 + sse1);
827 static int vp8_rd_inter_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *distortion, int fullpixel)
829 vp8_build_uvmvs(&x->e_mbd, fullpixel);
830 vp8_encode_inter16x16uvrd(IF_RTCD(&cpi->rtcd), x);
833 *rate = rd_cost_mbuv(x);
834 *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
836 return UVRDFUNC(x->rdmult, x->rddiv, *rate, *distortion, cpi->target_bits_per_mb);
839 int vp8_rd_pick_intra_mbuv_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, int *distortion)
841 MB_PREDICTION_MODE mode;
842 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
843 int best_rd = INT_MAX;
844 int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
847 for (mode = DC_PRED; mode <= TM_PRED; mode++)
853 x->e_mbd.mbmi.uv_mode = mode;
854 vp8_encode_intra16x16mbuvrd(IF_RTCD(&cpi->rtcd), x);
856 rate_to = rd_cost_mbuv(x);
857 rate = rate_to + x->intra_uv_mode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.uv_mode];
859 distortion = vp8_get_mbuvrecon_error(IF_RTCD(&cpi->rtcd.variance), x);
861 this_rd = UVRDFUNC(x->rdmult, x->rddiv, rate, distortion, cpi->target_bits_per_mb);
863 if (this_rd < best_rd)
868 *rate_tokenonly = rate_to;
869 mode_selected = mode;
876 x->e_mbd.mbmi.uv_mode = mode_selected;
881 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
883 vp8_prob p [VP8_MVREFS-1];
884 assert(NEARESTMV <= m && m <= SPLITMV);
885 vp8_mv_ref_probs(p, near_mv_ref_ct);
886 return vp8_cost_token(vp8_mv_ref_tree, p, VP8_MVREFENCODINGS + m);
889 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv)
893 x->e_mbd.mbmi.mode = mb;
894 x->e_mbd.mbmi.mv.as_mv.row = mv->row;
895 x->e_mbd.mbmi.mv.as_mv.col = mv->col;
897 for (i = 0; i < 16; i++)
899 B_MODE_INFO *bmi = &x->e_mbd.block[i].bmi;
900 bmi->mode = (B_PREDICTION_MODE) mb;
901 bmi->mv.as_mv.row = mv->row;
902 bmi->mv.as_mv.col = mv->col;
906 #if !(CONFIG_REALTIME_ONLY)
907 int vp8_count_labels(int const *labelings)
912 for (i = 0; i < 16; i++)
914 if (labelings[i] > count)
915 count = labelings[i];
922 static int labels2mode(
924 int const *labelings, int which_label,
925 B_PREDICTION_MODE this_mode,
926 MV *this_mv, MV *best_ref_mv,
930 MACROBLOCKD *const xd = & x->e_mbd;
931 MODE_INFO *const mic = xd->mode_info_context;
932 const int mis = xd->mode_info_stride;
937 /* We have to be careful retrieving previously-encoded motion vectors.
938 Ones from this macroblock have to be pulled from the BLOCKD array
939 as they have not yet made it to the bmi array in our MB_MODE_INFO. */
945 BLOCKD *const d = xd->block + i;
946 const int row = i >> 2, col = i & 3;
950 if (labelings[i] != which_label)
953 if (col && labelings[i] == labelings[i-1])
955 else if (row && labelings[i] == labelings[i-4])
959 // the only time we should do costing for new motion vector or mode
960 // is when we are on a new label (jbb May 08, 2007)
961 switch (m = this_mode)
964 thismvcost = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
967 *this_mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
970 *this_mv = row ? d[-4].bmi.mv.as_mv : vp8_above_bmi(mic, i, mis)->mv.as_mv;
973 this_mv->row = this_mv->col = 0;
979 if (m == ABOVE4X4) // replace above with left if same
981 const MV mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
983 if (mv.row == this_mv->row && mv.col == this_mv->col)
987 cost = x->inter_bmode_costs[ m];
991 d->bmi.mv.as_mv = *this_mv;
1000 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels, int which_label, TEMP_CONTEXT *t)
1004 MACROBLOCKD *x = &mb->e_mbd;
1007 for (b = 0; b < 16; b++)
1008 if (labels[ b] == which_label)
1009 cost += cost_coeffs(mb, x->block + b, 3,
1010 t->a + vp8_block2above[b],
1011 t->l + vp8_block2left[b]);
1016 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label, const vp8_encodemb_rtcd_vtable_t *rtcd)
1019 unsigned int distortion = 0;
1021 for (i = 0; i < 16; i++)
1023 if (labels[i] == which_label)
1025 BLOCKD *bd = &x->e_mbd.block[i];
1026 BLOCK *be = &x->block[i];
1029 vp8_build_inter_predictors_b(bd, 16, x->e_mbd.subpixel_predict);
1030 ENCODEMB_INVOKE(rtcd, subb)(be, bd, 16);
1031 x->short_fdct4x4rd(be->src_diff, be->coeff, 32);
1033 // set to 0 no way to account for 2nd order DC so discount
1035 x->quantize_b(be, bd);
1037 distortion += ENCODEMB_INVOKE(rtcd, berr)(be->coeff, bd->dqcoeff);
1044 static void macro_block_yrd(MACROBLOCK *mb, int *Rate, int *Distortion, const vp8_encodemb_rtcd_vtable_t *rtcd)
1047 MACROBLOCKD *const x = &mb->e_mbd;
1048 BLOCK *const mb_y2 = mb->block + 24;
1049 BLOCKD *const x_y2 = x->block + 24;
1050 short *Y2DCPtr = mb_y2->src_diff;
1054 ENCODEMB_INVOKE(rtcd, submby)(mb->src_diff, mb->src.y_buffer, mb->e_mbd.predictor, mb->src.y_stride);
1056 // Fdct and building the 2nd order block
1057 for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
1059 mb->short_fdct8x4rd(beptr->src_diff, beptr->coeff, 32);
1060 *Y2DCPtr++ = beptr->coeff[0];
1061 *Y2DCPtr++ = beptr->coeff[16];
1065 if (x->mbmi.mode != SPLITMV)
1067 mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
1071 for (b = 0; b < 16; b++)
1073 mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
1076 // DC predication and Quantization of 2nd Order block
1077 if (x->mbmi.mode != SPLITMV)
1081 mb->quantize_b(mb_y2, x_y2);
1086 if (x->mbmi.mode == SPLITMV)
1087 d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 0) << 2;
1090 d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 1) << 2;
1091 d += ENCODEMB_INVOKE(rtcd, berr)(mb_y2->coeff, x_y2->dqcoeff);
1094 *Distortion = (d >> 4);
1097 *Rate = vp8_rdcost_mby(mb);
1100 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)
1102 int i, segmentation;
1103 B_PREDICTION_MODE this_mode;
1104 MACROBLOCKD *xc = &x->e_mbd;
1105 BLOCK *b = &x->block[0];
1106 BLOCKD *d = &x->e_mbd.block[0];
1107 BLOCK *c = &x->block[0];
1108 BLOCKD *e = &x->e_mbd.block[0];
1110 int best_segment_rd = INT_MAX;
1116 int bestsegmentyrate = 0;
1118 // FIX TO Rd error outrange bug PGW 9 june 2004
1119 B_PREDICTION_MODE bmodes[16] = {ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1120 ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1121 ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1122 ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4
1128 for (segmentation = 0; segmentation < VP8_NUMMBSPLITS; segmentation++)
1131 int this_segment_rd = 0;
1132 int label_mv_thresh;
1136 int UNINITIALIZED_IS_SAFE(sseshift);
1137 int segmentyrate = 0;
1139 vp8_variance_fn_ptr_t v_fn_ptr;
1143 vp8_setup_temp_context(&t, xc->above_context[Y1CONTEXT], xc->left_context[Y1CONTEXT], 4);
1148 switch (segmentation)
1151 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
1152 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
1153 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
1154 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
1155 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
1159 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
1160 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
1161 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
1162 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
1163 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
1167 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
1168 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
1169 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
1170 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
1171 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
1175 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
1176 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
1177 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
1178 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
1179 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
1184 labels = vp8_mbsplits[segmentation];
1185 label_count = vp8_count_labels(labels);
1187 // 64 makes this threshold really big effectively
1188 // making it so that we very rarely check mvs on
1189 // segments. setting this to 1 would make mv thresh
1190 // roughly equal to what it is for macroblocks
1191 label_mv_thresh = 1 * mvthresh / label_count ;
1193 // Segmentation method overheads
1194 rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
1196 rate += vp8_cost_mv_ref(SPLITMV, mdcounts);
1198 this_segment_rd += RDFUNC(x->rdmult, x->rddiv, rate, 0, cpi->target_bits_per_mb);
1201 for (i = 0; i < label_count; i++)
1203 MV mode_mv[B_MODE_COUNT];
1204 int best_label_rd = INT_MAX;
1205 B_PREDICTION_MODE mode_selected = ZERO4X4;
1207 int bestlabelyrate = 0;
1210 d = &x->e_mbd.block[0];
1214 for (j = 0; j < 16; j++)
1219 e = &x->e_mbd.block[j];
1221 // search for the best motion vector on this segment
1222 for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
1230 vp8_setup_temp_context(&ts, &t.a[0], &t.l[0], 4);
1232 if (this_mode == NEW4X4)
1235 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
1238 int bestsme = INT_MAX;
1241 // Is the best so far sufficiently good that we cant justify doing and new motion search.
1242 if (best_label_rd < label_mv_thresh)
1246 int sadpb = x->sadperbit4;
1248 if (cpi->sf.search_method == HEX)
1249 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);
1252 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);
1257 while (n < further_steps)
1265 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);
1267 if (thissme < bestsme)
1270 mode_mv[NEW4X4].row = temp_mv.row;
1271 mode_mv[NEW4X4].col = temp_mv.col;
1277 // Should we do a full search (best quality only)
1278 if ((compressor_speed == 0) && (bestsme >> sseshift) > 4000)
1280 thissme = cpi->full_search_sad(x, c, e, best_ref_mv, sadpb / 4, 16, &v_fn_ptr, x->mvcost, x->mvsadcost);
1282 if (thissme < bestsme)
1285 mode_mv[NEW4X4] = e->bmi.mv.as_mv;
1289 // The full search result is actually worse so re-instate the previous best vector
1290 e->bmi.mv.as_mv = mode_mv[NEW4X4];
1295 if (bestsme < INT_MAX)
1298 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);
1300 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);
1304 rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode], best_ref_mv, mvcost);
1306 // Trap vectors that reach beyond the UMV borders
1307 if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
1308 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
1313 distortion = vp8_encode_inter_mb_segment(x, labels, i, IF_RTCD(&cpi->rtcd.encodemb)) / 4;
1315 labelyrate = rdcost_mbsegment_y(x, labels, i, &ts);
1318 this_rd = RDFUNC(x->rdmult, x->rddiv, rate, distortion, cpi->target_bits_per_mb);
1320 if (this_rd < best_label_rd)
1324 bestlabelyrate = labelyrate;
1325 mode_selected = this_mode;
1326 best_label_rd = this_rd;
1327 vp8_setup_temp_context(&tb, &ts.a[0], &ts.l[0], 4);
1332 vp8_setup_temp_context(&t, &tb.a[0], &tb.l[0], 4);
1334 labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected], best_ref_mv, mvcost);
1338 segmentyrate += bestlabelyrate;
1339 this_segment_rd += best_label_rd;
1341 if ((this_segment_rd > best_rd) || (this_segment_rd > best_segment_rd))
1345 if ((this_segment_rd <= best_rd) && (this_segment_rd < best_segment_rd))
1349 bestsegmentyrate = segmentyrate;
1350 best_segment_rd = this_segment_rd;
1351 best_seg = segmentation;
1353 // store everything needed to come back to this!!
1354 for (i = 0; i < 16; i++)
1356 BLOCKD *bd = &x->e_mbd.block[i];
1358 bmvs[i] = bd->bmi.mv.as_mv;
1359 bmodes[i] = bd->bmi.mode;
1365 // set it to the best
1366 for (i = 0; i < 16; i++)
1368 BLOCKD *bd = &x->e_mbd.block[i];
1370 bd->bmi.mv.as_mv = bmvs[i];
1371 bd->bmi.mode = bmodes[i];
1375 // Trap cases where the best split mode has all vectors coded 0,0 (or all the same)
1380 for (i = 1; i < 16; i++)
1382 if ((bmvs[i].col != bmvs[i-1].col) || (bmvs[i].row != bmvs[i-1].row))
1391 best_segment_rd = INT_MAX;
1395 *returntotrate = bsr;
1396 *returndistortion = bsd;
1397 *returnyrate = bestsegmentyrate;
1402 labels = vp8_mbsplits[best_seg];
1403 x->e_mbd.mbmi.partitioning = best_seg;
1404 x->e_mbd.mbmi.partition_count = vp8_count_labels(labels);
1406 for (i = 0; i < x->e_mbd.mbmi.partition_count; i++)
1410 for (j = 0; j < 16; j++)
1416 x->e_mbd.mbmi.partition_bmi[i].mode = x->e_mbd.block[j].bmi.mode;
1417 x->e_mbd.mbmi.partition_bmi[i].mv.as_mv = x->e_mbd.block[j].bmi.mv.as_mv;
1420 return best_segment_rd;
1424 int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
1426 BLOCK *b = &x->block[0];
1427 BLOCKD *d = &x->e_mbd.block[0];
1428 MACROBLOCKD *xd = &x->e_mbd;
1429 B_MODE_INFO best_bmodes[16];
1430 MB_MODE_INFO best_mbmode;
1432 MV mode_mv[MB_MODE_COUNT];
1433 MB_PREDICTION_MODE this_mode;
1435 int best_mode_index = 0;
1442 int best_rd = INT_MAX; // 1 << 30;
1443 int ref_frame_cost[MAX_REF_FRAMES];
1444 int rate2, distortion2;
1445 int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
1446 int rate_y, UNINITIALIZED_IS_SAFE(rate_uv);
1448 //int all_rds[MAX_MODES]; // Experimental debug code.
1449 //int all_rates[MAX_MODES];
1450 //int all_dist[MAX_MODES];
1451 //int intermodecost[MAX_MODES];
1453 MB_PREDICTION_MODE uv_intra_mode;
1456 int uvintra_eob = 0;
1458 int force_no_skip = 0;
1460 *returnintra = INT_MAX;
1462 cpi->mbs_tested_so_far++; // Count of the number of MBs tested so far this frame
1466 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(cpi->prob_intra_coded);
1468 // Experimental code
1469 // Adjust the RD multiplier based on the best case distortion we saw in the most recently coded mb
1470 //if ( (cpi->last_mb_distortion) > 0 && (cpi->target_bits_per_mb > 0) )
1474 //tmprdmult = (cpi->last_mb_distortion * 256) / ((cpi->av_per_frame_bandwidth*256)/cpi->common.MBs);
1475 tmprdmult = (cpi->last_mb_distortion * 256) / cpi->target_bits_per_mb;
1476 //tmprdmult = tmprdmult;
1478 //if ( tmprdmult > cpi->RDMULT * 2 )
1479 // tmprdmult = cpi->RDMULT * 2;
1480 //else if ( tmprdmult < cpi->RDMULT / 2 )
1481 // tmprdmult = cpi->RDMULT / 2;
1483 //tmprdmult = (tmprdmult < 25) ? 25 : tmprdmult;
1485 //x->rdmult = tmprdmult;
1489 // Special case treatment when GF and ARF are not sensible options for reference
1490 if (cpi->ref_frame_flags == VP8_LAST_FLAG)
1492 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1493 + vp8_cost_zero(255);
1494 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1496 + vp8_cost_zero(128);
1497 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1499 + vp8_cost_one(128);
1503 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1504 + vp8_cost_zero(cpi->prob_last_coded);
1505 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1506 + vp8_cost_one(cpi->prob_last_coded)
1507 + vp8_cost_zero(cpi->prob_gf_coded);
1508 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1509 + vp8_cost_one(cpi->prob_last_coded)
1510 + vp8_cost_one(cpi->prob_gf_coded);
1513 vpx_memset(mode_mv, 0, sizeof(mode_mv));
1515 x->e_mbd.mbmi.ref_frame = INTRA_FRAME;
1516 vp8_rd_pick_intra_mbuv_mode(cpi, x, &uv_intra_rate, &uv_intra_rate_tokenonly, &uv_intra_distortion);
1517 uv_intra_mode = x->e_mbd.mbmi.uv_mode;
1521 for (i = 16; i < 24; i++)
1522 uvintra_eob += x->e_mbd.block[i].eob;
1525 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
1528 int this_rd = INT_MAX;
1529 int lf_or_gf = 0; // Lat Frame (01) or gf/arf (1)
1530 int disable_skip = 0;
1534 // Experimental debug code.
1535 // Record of rd values recorded for this MB. -1 indicates not measured
1536 //all_rds[mode_index] = -1;
1537 //all_rates[mode_index] = -1;
1538 //all_dist[mode_index] = -1;
1539 //intermodecost[mode_index] = -1;
1541 // Test best rd so far against threshold for trying this mode.
1542 if (best_rd <= cpi->rd_threshes[mode_index])
1547 // These variables hold are rolling total cost and distortion for this mode
1551 // Where skip is allowable add in the default per mb cost for the no skip case.
1552 // where we then decide to skip we have to delete this and replace it with the
1553 // cost of signallying a skip
1554 if (cpi->common.mb_no_coeff_skip)
1556 rate2 += vp8_cost_bit(cpi->prob_skip_false, 0);
1559 this_mode = vp8_mode_order[mode_index];
1561 x->e_mbd.mbmi.mode = this_mode;
1562 x->e_mbd.mbmi.uv_mode = DC_PRED;
1563 x->e_mbd.mbmi.ref_frame = vp8_ref_frame_order[mode_index];
1565 //Only consider ZEROMV/ALTREF_FRAME for alt ref frame.
1566 if (cpi->is_src_frame_alt_ref)
1568 if (this_mode != ZEROMV || x->e_mbd.mbmi.ref_frame != ALTREF_FRAME)
1572 if (x->e_mbd.mbmi.ref_frame == LAST_FRAME)
1574 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
1577 lf_or_gf = 0; // Local last frame vs Golden frame flag
1579 // Set up pointers for this macro block into the previous frame recon buffer
1580 x->e_mbd.pre.y_buffer = cpi->common.last_frame.y_buffer + recon_yoffset;
1581 x->e_mbd.pre.u_buffer = cpi->common.last_frame.u_buffer + recon_uvoffset;
1582 x->e_mbd.pre.v_buffer = cpi->common.last_frame.v_buffer + recon_uvoffset;
1584 else if (x->e_mbd.mbmi.ref_frame == GOLDEN_FRAME)
1587 // not supposed to reference gold frame
1588 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
1591 lf_or_gf = 1; // Local last frame vs Golden frame flag
1593 // Set up pointers for this macro block into the previous frame recon buffer
1594 x->e_mbd.pre.y_buffer = cpi->common.golden_frame.y_buffer + recon_yoffset;
1595 x->e_mbd.pre.u_buffer = cpi->common.golden_frame.u_buffer + recon_uvoffset;
1596 x->e_mbd.pre.v_buffer = cpi->common.golden_frame.v_buffer + recon_uvoffset;
1598 else if (x->e_mbd.mbmi.ref_frame == ALTREF_FRAME)
1600 // not supposed to reference alt ref frame
1601 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
1604 //if ( !cpi->source_alt_ref_active )
1607 lf_or_gf = 1; // Local last frame vs Golden frame flag
1609 // Set up pointers for this macro block into the previous frame recon buffer
1610 x->e_mbd.pre.y_buffer = cpi->common.alt_ref_frame.y_buffer + recon_yoffset;
1611 x->e_mbd.pre.u_buffer = cpi->common.alt_ref_frame.u_buffer + recon_uvoffset;
1612 x->e_mbd.pre.v_buffer = cpi->common.alt_ref_frame.v_buffer + recon_uvoffset;
1615 vp8_find_near_mvs(&x->e_mbd,
1616 x->e_mbd.mode_info_context,
1617 &mode_mv[NEARESTMV], &mode_mv[NEARMV], &best_ref_mv,
1618 mdcounts, x->e_mbd.mbmi.ref_frame, cpi->common.ref_frame_sign_bias);
1621 // Estimate the reference frame signaling cost and add it to the rolling cost variable.
1622 frame_cost = ref_frame_cost[x->e_mbd.mbmi.ref_frame];
1623 rate2 += frame_cost;
1625 if (this_mode <= B_PRED)
1627 for (i = 0; i < 16; i++)
1629 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
1633 // Check to see if the testing frequency for this mode is at its max
1634 // If so then prevent it from being tested and increase the threshold for its testing
1635 if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
1637 if (cpi->mbs_tested_so_far <= cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index])
1639 // Increase the threshold for coding this mode to make it less likely to be chosen
1640 cpi->rd_thresh_mult[mode_index] += 4;
1642 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1643 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1645 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
1651 // 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
1652 cpi->mode_test_hit_counts[mode_index] ++;
1654 // Experimental code. Special case for gf and arf zeromv modes. Increase zbin size to supress noise
1655 if (cpi->zbin_mode_boost_enabled)
1657 if ((vp8_mode_order[mode_index] == ZEROMV) && (vp8_ref_frame_order[mode_index] != LAST_FRAME))
1658 cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
1660 cpi->zbin_mode_boost = 0;
1662 vp8cx_mb_init_quantizer(cpi, x);
1669 // Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
1670 vp8_rd_pick_intra4x4mby_modes(cpi, x, &rate, &rate_y, &distortion);
1673 distortion2 += distortion;
1674 rate2 += uv_intra_rate;
1675 rate_uv = uv_intra_rate_tokenonly;
1676 distortion2 += uv_intra_distortion;
1681 int frame_cost_rd = RDFUNC(x->rdmult, x->rddiv, frame_cost, 0, cpi->target_bits_per_mb);
1682 int saved_rate = rate2;
1684 // vp8_rd_pick_best_mbsegmentation looks only at Y and does not account for frame_cost.
1685 // (best_rd - frame_cost_rd) is thus a conservative breakout number.
1686 int breakout_rd = best_rd - frame_cost_rd;
1689 if (x->e_mbd.mbmi.ref_frame == LAST_FRAME)
1690 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) ;
1691 else if (x->e_mbd.mbmi.ref_frame == GOLDEN_FRAME)
1692 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) ;
1694 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) ;
1697 distortion2 += distortion;
1699 // If even the 'Y' rd value of split is higher than best so far then dont bother looking at UV
1700 if (tmp_rd < breakout_rd)
1702 // Now work out UV cost and add it in
1703 vp8_rd_inter_uv(cpi, x, &rate, &distortion, cpi->common.full_pixel);
1706 distortion2 += distortion;
1715 // Trap cases where the best split mode has all vectors coded 0,0 (or all the same)
1720 for (i = 1; i < 16; i++)
1722 BLOCKD *bd = &x->e_mbd.block[i];
1724 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 ) )
1733 // reset mode and mv and jump to newmv
1737 mode_mv[NEWMV].row = x->e_mbd.block[0].bmi.mv.as_mv.row;
1738 mode_mv[NEWMV].col = x->e_mbd.block[0].bmi.mv.as_mv.col;
1739 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
1744 // trap cases where the 8x8s can be promoted to 8x16s or 16x8s
1745 if (0)//x->e_mbd.mbmi.partition_count == 4)
1748 if (x->e_mbd.mbmi.partition_bmi[0].mv.as_int == x->e_mbd.mbmi.partition_bmi[1].mv.as_int
1749 && x->e_mbd.mbmi.partition_bmi[2].mv.as_int == x->e_mbd.mbmi.partition_bmi[3].mv.as_int)
1751 const int *labels = vp8_mbsplits[2];
1752 x->e_mbd.mbmi.partitioning = 0;
1753 rate -= vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + 2);
1754 rate += vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings);
1755 //rate -= x->inter_bmode_costs[ x->e_mbd.mbmi.partition_bmi[1]];
1756 //rate -= x->inter_bmode_costs[ x->e_mbd.mbmi.partition_bmi[3]];
1757 x->e_mbd.mbmi.partition_bmi[1] = x->e_mbd.mbmi.partition_bmi[2];
1767 x->e_mbd.mbmi.ref_frame = INTRA_FRAME;
1768 vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
1770 macro_block_yrd(x, &rate, &distortion, IF_RTCD(&cpi->rtcd.encodemb)) ;
1773 distortion2 += distortion;
1774 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.mode];
1775 rate2 += uv_intra_rate;
1776 rate_uv = uv_intra_rate_tokenonly;
1777 distortion2 += uv_intra_distortion;
1783 // Decrement full search counter
1784 if (cpi->check_freq[lf_or_gf] > 0)
1785 cpi->check_freq[lf_or_gf] --;
1789 int bestsme = INT_MAX;
1790 int step_param = cpi->sf.first_step;
1795 // Work out how long a search we should do
1796 search_range = MAXF(abs(best_ref_mv.col), abs(best_ref_mv.row)) >> 3;
1798 if (search_range >= x->vector_range)
1799 x->vector_range = search_range;
1800 else if (x->vector_range > cpi->sf.min_fs_radius)
1803 // Initial step/diamond search
1805 int sadpb = x->sadperbit16;
1807 if (cpi->sf.search_method == HEX)
1809 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);
1810 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1811 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1815 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
1816 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1817 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1819 // Further step/diamond searches as necessary
1821 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
1826 while (n < further_steps)
1834 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
1836 if (thissme < bestsme)
1839 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1840 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1844 d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
1845 d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
1853 // Should we do a full search
1854 if (!cpi->check_freq[lf_or_gf] || cpi->do_full[lf_or_gf])
1857 int full_flag_thresh = 0;
1859 // Update x->vector_range based on best vector found in step search
1860 search_range = MAXF(abs(d->bmi.mv.as_mv.row), abs(d->bmi.mv.as_mv.col));
1862 if (search_range > x->vector_range)
1863 x->vector_range = search_range;
1865 search_range = x->vector_range;
1868 search_range = (search_range > cpi->sf.max_fs_radius) ? cpi->sf.max_fs_radius : search_range;
1870 int sadpb = x->sadperbit16 >> 2;
1871 thissme = cpi->full_search_sad(x, b, d, &best_ref_mv, sadpb, search_range, &cpi->fn_ptr, x->mvcost, x->mvsadcost);
1874 // Barrier threshold to initiating full search
1875 // full_flag_thresh = 10 + (thissme >> 7);
1876 if ((thissme + full_flag_thresh) < bestsme)
1878 cpi->do_full[lf_or_gf] ++;
1881 else if (thissme < bestsme)
1885 cpi->do_full[lf_or_gf] = cpi->do_full[lf_or_gf] >> 1;
1886 cpi->check_freq[lf_or_gf] = cpi->sf.full_freq[lf_or_gf];
1888 // The full search result is actually worse so re-instate the previous best vector
1889 d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
1890 d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
1894 if (bestsme < INT_MAX)
1895 // 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
1896 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);
1898 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1899 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1901 // Add the new motion vector cost to our rolling cost variable
1902 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
1909 // Clip "next_nearest" so that it does not extend to far out of image
1910 if (mode_mv[this_mode].col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN))
1911 mode_mv[this_mode].col = xd->mb_to_left_edge - LEFT_TOP_MARGIN;
1912 else if (mode_mv[this_mode].col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN)
1913 mode_mv[this_mode].col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN;
1915 if (mode_mv[this_mode].row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN))
1916 mode_mv[this_mode].row = xd->mb_to_top_edge - LEFT_TOP_MARGIN;
1917 else if (mode_mv[this_mode].row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN)
1918 mode_mv[this_mode].row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN;
1920 // 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.
1921 if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) &&
1922 ((mode_mv[this_mode].row == 0) && (mode_mv[this_mode].col == 0)))
1929 // Trap vectors that reach beyond the UMV borders
1930 // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
1931 // because of the lack of break statements in the previous two cases.
1932 if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
1933 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
1936 vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
1937 vp8_build_inter_predictors_mby(&x->e_mbd);
1938 VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)(x->src.y_buffer, x->src.y_stride, x->e_mbd.predictor, 16, (unsigned int *)(&sse), &sum);
1940 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
1944 else if (sse < x->encode_breakout)
1946 // Check u and v to make sure skip is ok
1949 sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
1951 if (sse2 * 2 < x->encode_breakout)
1957 disable_skip = 1; // We have no real rate data so trying to adjust for rate_y and rate_uv below will cause problems.
1958 this_rd = RDFUNC(x->rdmult, x->rddiv, rate2, distortion2, cpi->target_bits_per_mb);
1960 break; // (PGW) Move break here from below - for now at least
1966 //intermodecost[mode_index] = vp8_cost_mv_ref(this_mode, mdcounts); // Experimental debug code
1968 // Add in the Mv/mode cost
1969 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1971 // Y cost and distortion
1972 macro_block_yrd(x, &rate, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
1975 distortion2 += distortion;
1977 // UV cost and distortion
1978 vp8_rd_inter_uv(cpi, x, &rate, &distortion, cpi->common.full_pixel);
1981 distortion2 += distortion;
1990 // Test for the condition where skip block will be activated because there are no non zero coefficients and make any necessary adjustment for rate
1991 if (cpi->common.mb_no_coeff_skip)
1995 for (i = 0; i <= 24; i++)
1997 tteob += x->e_mbd.block[i].eob;
2003 rate2 -= (rate_y + rate_uv);
2005 // Back out no skip flag costing and add in skip flag costing
2006 if (cpi->prob_skip_false)
2008 rate2 += vp8_cost_bit(cpi->prob_skip_false, 1);
2009 rate2 -= vp8_cost_bit(cpi->prob_skip_false, 0);
2018 ratenotuseskip = rate_y + rate_uv + vp8_cost_bit(cpi->prob_skip_false, 0);
2019 rateuseskip = vp8_cost_bit(cpi->prob_skip_false, 1);
2021 if (1) // rateuseskip<ratenotuseskip)
2023 rate2 -= ratenotuseskip;
2024 rate2 += rateuseskip;
2044 // distortion when no coeff is encoded
2045 maxdistortion = macro_block_max_error(x);
2047 ratenotuseskip = rate_y + rate_uv + vp8_cost_bit(cpi->prob_skip_false, 0);
2048 rateuseskip = vp8_cost_bit(cpi->prob_skip_false, 1);
2050 minrate = rateuseskip - ratenotuseskip;
2052 skip_rd = RDFUNC(x->rdmult, x->rddiv, minrate, maxdistortion - distortion2, cpi->target_bits_per_mb);
2054 if (skip_rd + 50 < 0 && x->e_mbd.mbmi.ref_frame != INTRA_FRAME && rate_y + rate_uv < 4000)
2057 rate2 = rate2 + rateuseskip - ratenotuseskip;
2058 distortion2 = maxdistortion;
2071 // Calculate the final RD estimate for this mode
2072 this_rd = RDFUNC(x->rdmult, x->rddiv, rate2, distortion2, cpi->target_bits_per_mb);
2075 // Experimental debug code.
2076 //all_rds[mode_index] = this_rd;
2077 //all_rates[mode_index] = rate2;
2078 //all_dist[mode_index] = distortion2;
2080 if ((x->e_mbd.mbmi.ref_frame == INTRA_FRAME) && (this_rd < *returnintra))
2082 *returnintra = this_rd ;
2085 // Did this mode help.. i.i is it the new best mode
2086 if (this_rd < best_rd || x->skip)
2088 // Note index of best mode so far
2089 best_mode_index = mode_index;
2090 x->e_mbd.mbmi.force_no_skip = force_no_skip;
2092 if (this_mode <= B_PRED)
2094 x->e_mbd.mbmi.uv_mode = uv_intra_mode;
2097 *returnrate = rate2;
2098 *returndistortion = distortion2;
2100 vpx_memcpy(&best_mbmode, &x->e_mbd.mbmi, sizeof(MB_MODE_INFO));
2102 for (i = 0; i < 16; i++)
2104 vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
2107 // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
2108 cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
2109 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2112 // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
2115 cpi->rd_thresh_mult[mode_index] += 4;
2117 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2118 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2120 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2127 // Reduce the activation RD thresholds for the best choice mode
2128 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
2130 int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 2);
2132 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;
2133 cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
2135 // If we chose a split mode then reset the new MV thresholds as well
2136 /*if ( vp8_mode_order[best_mode_index] == SPLITMV )
2138 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWMV] >> 4);
2139 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;
2140 cpi->rd_threshes[THR_NEWMV] = (cpi->rd_baseline_thresh[THR_NEWMV] >> 7) * cpi->rd_thresh_mult[THR_NEWMV];
2142 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWG] >> 4);
2143 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;
2144 cpi->rd_threshes[THR_NEWG] = (cpi->rd_baseline_thresh[THR_NEWG] >> 7) * cpi->rd_thresh_mult[THR_NEWG];
2146 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWA] >> 4);
2147 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;
2148 cpi->rd_threshes[THR_NEWA] = (cpi->rd_baseline_thresh[THR_NEWA] >> 7) * cpi->rd_thresh_mult[THR_NEWA];
2153 // If we have chosen new mv or split then decay the full search check count more quickly.
2154 if ((vp8_mode_order[best_mode_index] == NEWMV) || (vp8_mode_order[best_mode_index] == SPLITMV))
2156 int lf_or_gf = (vp8_ref_frame_order[best_mode_index] == LAST_FRAME) ? 0 : 1;
2158 if (cpi->check_freq[lf_or_gf] && !cpi->do_full[lf_or_gf])
2160 cpi->check_freq[lf_or_gf] --;
2164 // Keep a record of best mode index that we chose
2165 cpi->last_best_mode_index = best_mode_index;
2167 // Note how often each mode chosen as best
2168 cpi->mode_chosen_counts[best_mode_index] ++;
2171 if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
2173 best_mbmode.mode = ZEROMV;
2174 best_mbmode.ref_frame = ALTREF_FRAME;
2175 best_mbmode.mv.as_int = 0;
2176 best_mbmode.uv_mode = 0;
2177 best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
2178 best_mbmode.partitioning = 0;
2179 best_mbmode.dc_diff = 0;
2181 vpx_memcpy(&x->e_mbd.mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2183 for (i = 0; i < 16; i++)
2185 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
2188 x->e_mbd.mbmi.mv.as_int = 0;
2195 vpx_memcpy(&x->e_mbd.mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2197 for (i = 0; i < 16; i++)
2199 vpx_memcpy(&x->e_mbd.block[i].bmi, &best_bmodes[i], sizeof(B_MODE_INFO));
2202 x->e_mbd.mbmi.mv.as_mv = x->e_mbd.block[15].bmi.mv.as_mv;