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)
234 double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
235 double rdconst = 3.00;
237 vp8_clear_system_state(); //__asm emms;
239 // Further tests required to see if optimum is different
240 // for key frames, golden frames and arf frames.
241 // if (cpi->common.refresh_golden_frame ||
242 // cpi->common.refresh_alt_ref_frame)
243 cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
245 // Extend rate multiplier along side quantizer zbin increases
246 if (cpi->zbin_over_quant > 0)
251 // Experimental code using the same basic equation as used for Q above
252 // The units of cpi->zbin_over_quant are 1/128 of Q bin size
253 oq_factor = 1.0 + ((double)0.0015625 * cpi->zbin_over_quant);
254 modq = (int)((double)capped_q * oq_factor);
255 cpi->RDMULT = (int)(rdconst * (modq * modq));
258 if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
260 if (cpi->next_iiratio > 31)
261 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
263 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[cpi->next_iiratio]) >> 4;
266 if (cpi->RDMULT < 125)
269 cpi->mb.errorperbit = (cpi->RDMULT / 100);
271 if (cpi->mb.errorperbit < 1)
272 cpi->mb.errorperbit = 1;
274 vp8_set_speed_features(cpi);
276 if (cpi->common.simpler_lpf)
277 cpi->common.filter_type = SIMPLE_LOOPFILTER;
279 q = (int)pow(Qvalue, 1.25);
284 if (cpi->ref_frame_flags == VP8_ALT_FLAG)
286 thresh = &cpi->rd_threshes[THR_NEWA];
287 threshmult = cpi->sf.thresh_mult[THR_NEWA];
289 else if (cpi->ref_frame_flags == VP8_GOLD_FLAG)
291 thresh = &cpi->rd_threshes[THR_NEWG];
292 threshmult = cpi->sf.thresh_mult[THR_NEWG];
296 thresh = &cpi->rd_threshes[THR_NEWMV];
297 threshmult = cpi->sf.thresh_mult[THR_NEWMV];
300 if (cpi->RDMULT > 1000)
305 for (i = 0; i < MAX_MODES; i++)
307 if (cpi->sf.thresh_mult[i] < INT_MAX)
309 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
313 cpi->rd_threshes[i] = INT_MAX;
316 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
323 for (i = 0; i < MAX_MODES; i++)
325 if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
327 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
331 cpi->rd_threshes[i] = INT_MAX;
334 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
340 (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs
343 vp8_init_mode_costs(cpi);
347 void vp8_auto_select_speed(VP8_COMP *cpi)
349 int used = cpi->oxcf.cpu_used;
351 int milliseconds_for_compress = (int)(1000000 / cpi->oxcf.frame_rate);
353 milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
361 f = fopen("speed.stt", "a");
362 fprintf(f, " %8ld %10ld %10ld %10ld\n",
363 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
370 // this is done during parameter valid check
377 if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
379 if (cpi->avg_pick_mode_time == 0)
385 if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
388 cpi->avg_pick_mode_time = 0;
389 cpi->avg_encode_time = 0;
397 if (milliseconds_for_compress * 100 > cpi->avg_encode_time * vp8_auto_speed_thresh[cpi->Speed])
400 cpi->avg_pick_mode_time = 0;
401 cpi->avg_encode_time = 0;
403 // In real-time mode, cpi->speed is in [4, 16].
404 if (cpi->Speed < 4) //if ( cpi->Speed < 0 )
406 cpi->Speed = 4; //cpi->Speed = 0;
419 cpi->avg_pick_mode_time = 0;
420 cpi->avg_encode_time = 0;
424 int vp8_block_error_c(short *coeff, short *dqcoeff)
429 for (i = 0; i < 16; i++)
431 int this_diff = coeff[i] - dqcoeff[i];
432 error += this_diff * this_diff;
438 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
443 int berror, error = 0;
445 for (i = 0; i < 16; i++)
448 bd = &mb->e_mbd.block[i];
452 for (j = dc; j < 16; j++)
454 int this_diff = be->coeff[j] - bd->dqcoeff[j];
455 berror += this_diff * this_diff;
464 int vp8_mbuverror_c(MACROBLOCK *mb)
474 for (i = 16; i < 24; i++)
477 bd = &mb->e_mbd.block[i];
479 error += vp8_block_error_c(be->coeff, bd->dqcoeff);
485 #if !(CONFIG_REALTIME_ONLY)
486 static int macro_block_max_error(MACROBLOCK *mb)
494 dc = !(mb->e_mbd.mbmi.mode == B_PRED || mb->e_mbd.mbmi.mode == SPLITMV);
496 for (i = 0; i < 16; i++)
502 for (j = dc; j < 16; j++)
504 int this_diff = be->coeff[j];
505 berror += this_diff * this_diff;
511 for (i = 16; i < 24; i++)
516 for (j = 0; j < 16; j++)
518 int this_diff = be->coeff[j];
519 berror += this_diff * this_diff;
532 for (j = 0; j < 16; j++)
534 int this_diff = be->coeff[j];
535 berror += this_diff * this_diff;
546 int VP8_UVSSE(MACROBLOCK *x, const vp8_variance_rtcd_vtable_t *rtcd)
548 unsigned char *uptr, *vptr;
549 unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
550 unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
551 int uv_stride = x->block[16].src_stride;
553 unsigned int sse1 = 0;
554 unsigned int sse2 = 0;
558 int pre_stride = x->e_mbd.block[16].pre_stride;
560 vp8_build_uvmvs(&x->e_mbd, 0);
561 mv_row = x->e_mbd.block[16].bmi.mv.as_mv.row;
562 mv_col = x->e_mbd.block[16].bmi.mv.as_mv.col;
564 offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
565 uptr = x->e_mbd.pre.u_buffer + offset;
566 vptr = x->e_mbd.pre.v_buffer + offset;
568 if ((mv_row | mv_col) & 7)
570 VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
571 VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
576 VARIANCE_INVOKE(rtcd, subpixvar8x8)(uptr, pre_stride, mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
577 VARIANCE_INVOKE(rtcd, subpixvar8x8)(vptr, pre_stride, mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
585 #if !(CONFIG_REALTIME_ONLY)
586 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
588 int c = !type; /* start at coef 0, unless Y with Y2 */
590 int pt ; /* surrounding block/prev coef predictor */
592 short *qcoeff_ptr = b->qcoeff;
594 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
596 # define QC( I) ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )
601 int t = vp8_dct_value_tokens_ptr[v].Token;
602 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
603 cost += vp8_dct_value_cost_ptr[v];
604 pt = vp8_prev_token_class[t];
610 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
612 pt = (c != !type); // is eob first coefficient;
618 int vp8_rdcost_mby(MACROBLOCK *mb)
625 MACROBLOCKD *x = &mb->e_mbd;
627 vp8_setup_temp_context(&t, x->above_context[Y1CONTEXT], x->left_context[Y1CONTEXT], 4);
628 vp8_setup_temp_context(&t2, x->above_context[Y2CONTEXT], x->left_context[Y2CONTEXT], 1);
630 if (x->mbmi.mode == SPLITMV)
633 for (b = 0; b < 16; b++)
634 cost += cost_coeffs(mb, x->block + b, type,
635 t.a + vp8_block2above[b], t.l + vp8_block2left[b]);
637 if (x->mbmi.mode != SPLITMV)
638 cost += cost_coeffs(mb, x->block + 24, 1,
639 t2.a + vp8_block2above[24], t2.l + vp8_block2left[24]);
645 static void rd_pick_intra4x4block(
650 B_PREDICTION_MODE *best_mode,
651 B_PREDICTION_MODE above,
652 B_PREDICTION_MODE left,
660 B_PREDICTION_MODE mode;
661 int best_rd = INT_MAX; // 1<<30
664 unsigned int *mode_costs;
666 ENTROPY_CONTEXT ta = *a, tempa = *a;
667 ENTROPY_CONTEXT tl = *l, templ = *l;
670 if (x->e_mbd.frame_type == KEY_FRAME)
672 mode_costs = x->bmode_costs[above][left];
676 mode_costs = x->inter_bmode_costs;
679 for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
684 rate = mode_costs[mode];
685 vp8_encode_intra4x4block_rd(IF_RTCD(&cpi->rtcd), x, be, b, mode);
690 ratey = cost_coeffs(x, b, 3, &tempa, &templ);
692 distortion = ENCODEMB_INVOKE(IF_RTCD(&cpi->rtcd.encodemb), berr)(be->coeff, b->dqcoeff) >> 2;
694 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
696 if (this_rd < best_rd)
700 *bestdistortion = distortion;
708 b->bmi.mode = (B_PREDICTION_MODE)(*best_mode);
709 vp8_encode_intra4x4block_rd(IF_RTCD(&cpi->rtcd), x, be, b, b->bmi.mode);
714 int vp8_rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate, int *rate_y, int *Distortion)
716 MACROBLOCKD *const xd = &mb->e_mbd;
719 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
723 vp8_intra_prediction_down_copy(xd);
724 vp8_setup_temp_context(&t, xd->above_context[Y1CONTEXT], xd->left_context[Y1CONTEXT], 4);
726 for (i = 0; i < 16; i++)
728 MODE_INFO *const mic = xd->mode_info_context;
729 const int mis = xd->mode_info_stride;
730 const B_PREDICTION_MODE A = vp8_above_bmi(mic, i, mis)->mode;
731 const B_PREDICTION_MODE L = vp8_left_bmi(mic, i)->mode;
732 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
733 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
735 rd_pick_intra4x4block(
736 cpi, mb, mb->block + i, xd->block + i, &best_mode, A, L,
737 t.a + vp8_block2above[i],
738 t.l + vp8_block2left[i], &r, &ry, &d);
743 mic->bmi[i].mode = xd->block[i].bmi.mode = best_mode;
747 *rate_y += tot_rate_y;
748 *Distortion = distortion;
750 return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
753 int vp8_rd_pick_intra16x16mby_mode(VP8_COMP *cpi, MACROBLOCK *x, int *Rate, int *rate_y, int *Distortion)
756 MB_PREDICTION_MODE mode;
757 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
759 unsigned int distortion;
760 int best_rd = INT_MAX;
762 //Y Search for 16x16 intra prediction mode
763 for (mode = DC_PRED; mode <= TM_PRED; mode++)
769 x->e_mbd.mbmi.mode = mode;
771 rate += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.mode];
773 vp8_encode_intra16x16mbyrd(IF_RTCD(&cpi->rtcd), x);
775 ratey = vp8_rdcost_mby(x);
779 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);
781 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
783 if (this_rd < best_rd)
785 mode_selected = mode;
789 *Distortion = (int)distortion;
793 x->e_mbd.mbmi.mode = mode_selected;
798 static int rd_cost_mbuv(MACROBLOCK *mb)
803 MACROBLOCKD *x = &mb->e_mbd;
805 vp8_setup_temp_context(&t, x->above_context[UCONTEXT], x->left_context[UCONTEXT], 2);
806 vp8_setup_temp_context(&t2, x->above_context[VCONTEXT], x->left_context[VCONTEXT], 2);
808 for (b = 16; b < 20; b++)
809 cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
810 t.a + vp8_block2above[b], t.l + vp8_block2left[b]);
812 for (b = 20; b < 24; b++)
813 cost += cost_coeffs(mb, x->block + b, vp8_block2type[b],
814 t2.a + vp8_block2above[b], t2.l + vp8_block2left[b]);
820 unsigned int vp8_get_mbuvrecon_error(const vp8_variance_rtcd_vtable_t *rtcd, const MACROBLOCK *x) // sum of squares
822 unsigned int sse0, sse1;
824 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);
825 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);
826 return (sse0 + sse1);
829 static int vp8_rd_inter_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *distortion, int fullpixel)
831 vp8_build_uvmvs(&x->e_mbd, fullpixel);
832 vp8_encode_inter16x16uvrd(IF_RTCD(&cpi->rtcd), x);
835 *rate = rd_cost_mbuv(x);
836 *distortion = ENCODEMB_INVOKE(&cpi->rtcd.encodemb, mbuverr)(x) / 4;
838 return UVRDFUNC(x->rdmult, x->rddiv, *rate, *distortion, cpi->target_bits_per_mb);
841 int vp8_rd_pick_intra_mbuv_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, int *distortion)
843 MB_PREDICTION_MODE mode;
844 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
845 int best_rd = INT_MAX;
846 int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
849 for (mode = DC_PRED; mode <= TM_PRED; mode++)
855 x->e_mbd.mbmi.uv_mode = mode;
856 vp8_encode_intra16x16mbuvrd(IF_RTCD(&cpi->rtcd), x);
858 rate_to = rd_cost_mbuv(x);
859 rate = rate_to + x->intra_uv_mode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.uv_mode];
861 distortion = vp8_get_mbuvrecon_error(IF_RTCD(&cpi->rtcd.variance), x);
863 this_rd = UVRDFUNC(x->rdmult, x->rddiv, rate, distortion, cpi->target_bits_per_mb);
865 if (this_rd < best_rd)
870 *rate_tokenonly = rate_to;
871 mode_selected = mode;
878 x->e_mbd.mbmi.uv_mode = mode_selected;
883 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
885 vp8_prob p [VP8_MVREFS-1];
886 assert(NEARESTMV <= m && m <= SPLITMV);
887 vp8_mv_ref_probs(p, near_mv_ref_ct);
888 return vp8_cost_token(vp8_mv_ref_tree, p, VP8_MVREFENCODINGS + m);
891 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv)
895 x->e_mbd.mbmi.mode = mb;
896 x->e_mbd.mbmi.mv.as_mv.row = mv->row;
897 x->e_mbd.mbmi.mv.as_mv.col = mv->col;
899 for (i = 0; i < 16; i++)
901 B_MODE_INFO *bmi = &x->e_mbd.block[i].bmi;
902 bmi->mode = (B_PREDICTION_MODE) mb;
903 bmi->mv.as_mv.row = mv->row;
904 bmi->mv.as_mv.col = mv->col;
908 #if !(CONFIG_REALTIME_ONLY)
909 int vp8_count_labels(int const *labelings)
914 for (i = 0; i < 16; i++)
916 if (labelings[i] > count)
917 count = labelings[i];
924 static int labels2mode(
926 int const *labelings, int which_label,
927 B_PREDICTION_MODE this_mode,
928 MV *this_mv, MV *best_ref_mv,
932 MACROBLOCKD *const xd = & x->e_mbd;
933 MODE_INFO *const mic = xd->mode_info_context;
934 const int mis = xd->mode_info_stride;
939 /* We have to be careful retrieving previously-encoded motion vectors.
940 Ones from this macroblock have to be pulled from the BLOCKD array
941 as they have not yet made it to the bmi array in our MB_MODE_INFO. */
947 BLOCKD *const d = xd->block + i;
948 const int row = i >> 2, col = i & 3;
952 if (labelings[i] != which_label)
955 if (col && labelings[i] == labelings[i-1])
957 else if (row && labelings[i] == labelings[i-4])
961 // the only time we should do costing for new motion vector or mode
962 // is when we are on a new label (jbb May 08, 2007)
963 switch (m = this_mode)
966 thismvcost = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
969 *this_mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
972 *this_mv = row ? d[-4].bmi.mv.as_mv : vp8_above_bmi(mic, i, mis)->mv.as_mv;
975 this_mv->row = this_mv->col = 0;
981 if (m == ABOVE4X4) // replace above with left if same
983 const MV mv = col ? d[-1].bmi.mv.as_mv : vp8_left_bmi(mic, i)->mv.as_mv;
985 if (mv.row == this_mv->row && mv.col == this_mv->col)
989 cost = x->inter_bmode_costs[ m];
993 d->bmi.mv.as_mv = *this_mv;
1002 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels, int which_label, TEMP_CONTEXT *t)
1006 MACROBLOCKD *x = &mb->e_mbd;
1009 for (b = 0; b < 16; b++)
1010 if (labels[ b] == which_label)
1011 cost += cost_coeffs(mb, x->block + b, 3,
1012 t->a + vp8_block2above[b],
1013 t->l + vp8_block2left[b]);
1018 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label, const vp8_encodemb_rtcd_vtable_t *rtcd)
1021 unsigned int distortion = 0;
1023 for (i = 0; i < 16; i++)
1025 if (labels[i] == which_label)
1027 BLOCKD *bd = &x->e_mbd.block[i];
1028 BLOCK *be = &x->block[i];
1031 vp8_build_inter_predictors_b(bd, 16, x->e_mbd.subpixel_predict);
1032 ENCODEMB_INVOKE(rtcd, subb)(be, bd, 16);
1033 x->vp8_short_fdct4x4(be->src_diff, be->coeff, 32);
1035 // set to 0 no way to account for 2nd order DC so discount
1037 x->quantize_b(be, bd);
1039 distortion += ENCODEMB_INVOKE(rtcd, berr)(be->coeff, bd->dqcoeff);
1046 static void macro_block_yrd(MACROBLOCK *mb, int *Rate, int *Distortion, const vp8_encodemb_rtcd_vtable_t *rtcd)
1049 MACROBLOCKD *const x = &mb->e_mbd;
1050 BLOCK *const mb_y2 = mb->block + 24;
1051 BLOCKD *const x_y2 = x->block + 24;
1052 short *Y2DCPtr = mb_y2->src_diff;
1056 ENCODEMB_INVOKE(rtcd, submby)(mb->src_diff, mb->src.y_buffer, mb->e_mbd.predictor, mb->src.y_stride);
1058 // Fdct and building the 2nd order block
1059 for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
1061 mb->vp8_short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
1062 *Y2DCPtr++ = beptr->coeff[0];
1063 *Y2DCPtr++ = beptr->coeff[16];
1067 if (x->mbmi.mode != SPLITMV)
1069 mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
1073 for (b = 0; b < 16; b++)
1075 mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
1078 // DC predication and Quantization of 2nd Order block
1079 if (x->mbmi.mode != SPLITMV)
1083 mb->quantize_b(mb_y2, x_y2);
1088 if (x->mbmi.mode == SPLITMV)
1089 d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 0) << 2;
1092 d = ENCODEMB_INVOKE(rtcd, mberr)(mb, 1) << 2;
1093 d += ENCODEMB_INVOKE(rtcd, berr)(mb_y2->coeff, x_y2->dqcoeff);
1096 *Distortion = (d >> 4);
1099 *Rate = vp8_rdcost_mby(mb);
1102 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)
1104 int i, segmentation;
1105 B_PREDICTION_MODE this_mode;
1106 MACROBLOCKD *xc = &x->e_mbd;
1107 BLOCK *b = &x->block[0];
1108 BLOCKD *d = &x->e_mbd.block[0];
1109 BLOCK *c = &x->block[0];
1110 BLOCKD *e = &x->e_mbd.block[0];
1112 int best_segment_rd = INT_MAX;
1118 int bestsegmentyrate = 0;
1120 // FIX TO Rd error outrange bug PGW 9 june 2004
1121 B_PREDICTION_MODE bmodes[16] = {ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1122 ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1123 ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4,
1124 ZERO4X4, ZERO4X4, ZERO4X4, ZERO4X4
1130 vpx_memset(beobs, 0, sizeof(beobs));
1133 for (segmentation = 0; segmentation < VP8_NUMMBSPLITS; segmentation++)
1136 int this_segment_rd = 0;
1137 int label_mv_thresh;
1141 int UNINITIALIZED_IS_SAFE(sseshift);
1142 int segmentyrate = 0;
1144 vp8_variance_fn_ptr_t v_fn_ptr;
1148 vp8_setup_temp_context(&t, xc->above_context[Y1CONTEXT], xc->left_context[Y1CONTEXT], 4);
1153 switch (segmentation)
1156 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var16x8);
1157 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar16x8);
1158 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8);
1159 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x3);
1160 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad16x8x4d);
1164 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x16);
1165 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x16);
1166 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16);
1167 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x3);
1168 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x16x4d);
1172 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var8x8);
1173 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar8x8);
1174 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8);
1175 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x3);
1176 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad8x8x4d);
1180 v_fn_ptr.vf = VARIANCE_INVOKE(&cpi->rtcd.variance, var4x4);
1181 v_fn_ptr.svf = VARIANCE_INVOKE(&cpi->rtcd.variance, subpixvar4x4);
1182 v_fn_ptr.sdf = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4);
1183 v_fn_ptr.sdx3f = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x3);
1184 v_fn_ptr.sdx4df = VARIANCE_INVOKE(&cpi->rtcd.variance, sad4x4x4d);
1189 labels = vp8_mbsplits[segmentation];
1190 label_count = vp8_count_labels(labels);
1192 // 64 makes this threshold really big effectively
1193 // making it so that we very rarely check mvs on
1194 // segments. setting this to 1 would make mv thresh
1195 // roughly equal to what it is for macroblocks
1196 label_mv_thresh = 1 * mvthresh / label_count ;
1198 // Segmentation method overheads
1199 rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
1201 rate += vp8_cost_mv_ref(SPLITMV, mdcounts);
1203 this_segment_rd += RDFUNC(x->rdmult, x->rddiv, rate, 0, cpi->target_bits_per_mb);
1206 for (i = 0; i < label_count; i++)
1208 MV mode_mv[B_MODE_COUNT];
1209 int best_label_rd = INT_MAX;
1210 B_PREDICTION_MODE mode_selected = ZERO4X4;
1212 int bestlabelyrate = 0;
1215 d = &x->e_mbd.block[0];
1219 for (j = 0; j < 16; j++)
1224 e = &x->e_mbd.block[j];
1226 // search for the best motion vector on this segment
1227 for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
1235 vp8_setup_temp_context(&ts, &t.a[0], &t.l[0], 4);
1237 if (this_mode == NEW4X4)
1240 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
1243 int bestsme = INT_MAX;
1246 // Is the best so far sufficiently good that we cant justify doing and new motion search.
1247 if (best_label_rd < label_mv_thresh)
1251 int sadpb = x->sadperbit4;
1253 if (cpi->sf.search_method == HEX)
1254 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);
1257 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);
1262 while (n < further_steps)
1270 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);
1272 if (thissme < bestsme)
1275 mode_mv[NEW4X4].row = temp_mv.row;
1276 mode_mv[NEW4X4].col = temp_mv.col;
1282 // Should we do a full search (best quality only)
1283 if ((compressor_speed == 0) && (bestsme >> sseshift) > 4000)
1285 thissme = cpi->full_search_sad(x, c, e, best_ref_mv, sadpb / 4, 16, &v_fn_ptr, x->mvcost, x->mvsadcost);
1287 if (thissme < bestsme)
1290 mode_mv[NEW4X4] = e->bmi.mv.as_mv;
1294 // The full search result is actually worse so re-instate the previous best vector
1295 e->bmi.mv.as_mv = mode_mv[NEW4X4];
1300 if (bestsme < INT_MAX)
1303 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);
1305 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);
1309 rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode], best_ref_mv, mvcost);
1311 // Trap vectors that reach beyond the UMV borders
1312 if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
1313 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
1318 distortion = vp8_encode_inter_mb_segment(x, labels, i, IF_RTCD(&cpi->rtcd.encodemb)) / 4;
1320 labelyrate = rdcost_mbsegment_y(x, labels, i, &ts);
1323 this_rd = RDFUNC(x->rdmult, x->rddiv, rate, distortion, cpi->target_bits_per_mb);
1325 if (this_rd < best_label_rd)
1329 bestlabelyrate = labelyrate;
1330 mode_selected = this_mode;
1331 best_label_rd = this_rd;
1332 vp8_setup_temp_context(&tb, &ts.a[0], &ts.l[0], 4);
1337 vp8_setup_temp_context(&t, &tb.a[0], &tb.l[0], 4);
1339 labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected], best_ref_mv, mvcost);
1343 segmentyrate += bestlabelyrate;
1344 this_segment_rd += best_label_rd;
1346 if ((this_segment_rd > best_rd) || (this_segment_rd > best_segment_rd))
1350 if ((this_segment_rd <= best_rd) && (this_segment_rd < best_segment_rd))
1354 bestsegmentyrate = segmentyrate;
1355 best_segment_rd = this_segment_rd;
1356 best_seg = segmentation;
1358 // store everything needed to come back to this!!
1359 for (i = 0; i < 16; i++)
1361 BLOCKD *bd = &x->e_mbd.block[i];
1363 bmvs[i] = bd->bmi.mv.as_mv;
1364 bmodes[i] = bd->bmi.mode;
1370 // set it to the best
1371 for (i = 0; i < 16; i++)
1373 BLOCKD *bd = &x->e_mbd.block[i];
1375 bd->bmi.mv.as_mv = bmvs[i];
1376 bd->bmi.mode = bmodes[i];
1380 // Trap cases where the best split mode has all vectors coded 0,0 (or all the same)
1385 for (i = 1; i < 16; i++)
1387 if ((bmvs[i].col != bmvs[i-1].col) || (bmvs[i].row != bmvs[i-1].row))
1396 best_segment_rd = INT_MAX;
1400 *returntotrate = bsr;
1401 *returndistortion = bsd;
1402 *returnyrate = bestsegmentyrate;
1407 labels = vp8_mbsplits[best_seg];
1408 x->e_mbd.mbmi.partitioning = best_seg;
1409 x->e_mbd.mbmi.partition_count = vp8_count_labels(labels);
1411 for (i = 0; i < x->e_mbd.mbmi.partition_count; i++)
1415 for (j = 0; j < 16; j++)
1421 x->e_mbd.mbmi.partition_bmi[i].mode = x->e_mbd.block[j].bmi.mode;
1422 x->e_mbd.mbmi.partition_bmi[i].mv.as_mv = x->e_mbd.block[j].bmi.mv.as_mv;
1425 return best_segment_rd;
1429 int vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, int recon_uvoffset, int *returnrate, int *returndistortion, int *returnintra)
1431 BLOCK *b = &x->block[0];
1432 BLOCKD *d = &x->e_mbd.block[0];
1433 MACROBLOCKD *xd = &x->e_mbd;
1434 B_MODE_INFO best_bmodes[16];
1435 MB_MODE_INFO best_mbmode;
1437 MV mode_mv[MB_MODE_COUNT];
1438 MB_PREDICTION_MODE this_mode;
1440 int best_mode_index = 0;
1447 int best_rd = INT_MAX; // 1 << 30;
1448 int ref_frame_cost[MAX_REF_FRAMES];
1449 int rate2, distortion2;
1450 int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
1451 int rate_y, UNINITIALIZED_IS_SAFE(rate_uv);
1453 //int all_rds[MAX_MODES]; // Experimental debug code.
1454 //int all_rates[MAX_MODES];
1455 //int all_dist[MAX_MODES];
1456 //int intermodecost[MAX_MODES];
1458 MB_PREDICTION_MODE uv_intra_mode;
1461 int uvintra_eob = 0;
1463 int force_no_skip = 0;
1465 *returnintra = INT_MAX;
1467 vpx_memset(&best_mbmode, 0, sizeof(best_mbmode)); // clean
1469 cpi->mbs_tested_so_far++; // Count of the number of MBs tested so far this frame
1473 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(cpi->prob_intra_coded);
1475 // Experimental code
1476 // Adjust the RD multiplier based on the best case distortion we saw in the most recently coded mb
1477 //if ( (cpi->last_mb_distortion) > 0 && (cpi->target_bits_per_mb > 0) )
1481 //tmprdmult = (cpi->last_mb_distortion * 256) / ((cpi->av_per_frame_bandwidth*256)/cpi->common.MBs);
1482 tmprdmult = (cpi->last_mb_distortion * 256) / cpi->target_bits_per_mb;
1483 //tmprdmult = tmprdmult;
1485 //if ( tmprdmult > cpi->RDMULT * 2 )
1486 // tmprdmult = cpi->RDMULT * 2;
1487 //else if ( tmprdmult < cpi->RDMULT / 2 )
1488 // tmprdmult = cpi->RDMULT / 2;
1490 //tmprdmult = (tmprdmult < 25) ? 25 : tmprdmult;
1492 //x->rdmult = tmprdmult;
1496 // Special case treatment when GF and ARF are not sensible options for reference
1497 if (cpi->ref_frame_flags == VP8_LAST_FLAG)
1499 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1500 + vp8_cost_zero(255);
1501 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1503 + vp8_cost_zero(128);
1504 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1506 + vp8_cost_one(128);
1510 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1511 + vp8_cost_zero(cpi->prob_last_coded);
1512 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1513 + vp8_cost_one(cpi->prob_last_coded)
1514 + vp8_cost_zero(cpi->prob_gf_coded);
1515 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1516 + vp8_cost_one(cpi->prob_last_coded)
1517 + vp8_cost_one(cpi->prob_gf_coded);
1520 vpx_memset(mode_mv, 0, sizeof(mode_mv));
1522 x->e_mbd.mbmi.ref_frame = INTRA_FRAME;
1523 vp8_rd_pick_intra_mbuv_mode(cpi, x, &uv_intra_rate, &uv_intra_rate_tokenonly, &uv_intra_distortion);
1524 uv_intra_mode = x->e_mbd.mbmi.uv_mode;
1528 for (i = 16; i < 24; i++)
1529 uvintra_eob += x->e_mbd.block[i].eob;
1532 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
1535 int this_rd = INT_MAX;
1536 int lf_or_gf = 0; // Lat Frame (01) or gf/arf (1)
1537 int disable_skip = 0;
1541 // Experimental debug code.
1542 // Record of rd values recorded for this MB. -1 indicates not measured
1543 //all_rds[mode_index] = -1;
1544 //all_rates[mode_index] = -1;
1545 //all_dist[mode_index] = -1;
1546 //intermodecost[mode_index] = -1;
1548 // Test best rd so far against threshold for trying this mode.
1549 if (best_rd <= cpi->rd_threshes[mode_index])
1554 // These variables hold are rolling total cost and distortion for this mode
1558 // Where skip is allowable add in the default per mb cost for the no skip case.
1559 // where we then decide to skip we have to delete this and replace it with the
1560 // cost of signallying a skip
1561 if (cpi->common.mb_no_coeff_skip)
1563 rate2 += vp8_cost_bit(cpi->prob_skip_false, 0);
1566 this_mode = vp8_mode_order[mode_index];
1568 x->e_mbd.mbmi.mode = this_mode;
1569 x->e_mbd.mbmi.uv_mode = DC_PRED;
1570 x->e_mbd.mbmi.ref_frame = vp8_ref_frame_order[mode_index];
1572 //Only consider ZEROMV/ALTREF_FRAME for alt ref frame.
1573 if (cpi->is_src_frame_alt_ref)
1575 if (this_mode != ZEROMV || x->e_mbd.mbmi.ref_frame != ALTREF_FRAME)
1579 if (x->e_mbd.mbmi.ref_frame == LAST_FRAME)
1581 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
1583 if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
1586 lf_or_gf = 0; // Local last frame vs Golden frame flag
1588 // Set up pointers for this macro block into the previous frame recon buffer
1589 x->e_mbd.pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
1590 x->e_mbd.pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
1591 x->e_mbd.pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
1593 else if (x->e_mbd.mbmi.ref_frame == GOLDEN_FRAME)
1595 YV12_BUFFER_CONFIG *gld_yv12 = &cpi->common.yv12_fb[cpi->common.gld_fb_idx];
1597 // not supposed to reference gold frame
1598 if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
1601 lf_or_gf = 1; // Local last frame vs Golden frame flag
1603 // Set up pointers for this macro block into the previous frame recon buffer
1604 x->e_mbd.pre.y_buffer = gld_yv12->y_buffer + recon_yoffset;
1605 x->e_mbd.pre.u_buffer = gld_yv12->u_buffer + recon_uvoffset;
1606 x->e_mbd.pre.v_buffer = gld_yv12->v_buffer + recon_uvoffset;
1608 else if (x->e_mbd.mbmi.ref_frame == ALTREF_FRAME)
1610 YV12_BUFFER_CONFIG *alt_yv12 = &cpi->common.yv12_fb[cpi->common.alt_fb_idx];
1612 // not supposed to reference alt ref frame
1613 if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
1616 //if ( !cpi->source_alt_ref_active )
1619 lf_or_gf = 1; // Local last frame vs Golden frame flag
1621 // Set up pointers for this macro block into the previous frame recon buffer
1622 x->e_mbd.pre.y_buffer = alt_yv12->y_buffer + recon_yoffset;
1623 x->e_mbd.pre.u_buffer = alt_yv12->u_buffer + recon_uvoffset;
1624 x->e_mbd.pre.v_buffer = alt_yv12->v_buffer + recon_uvoffset;
1627 vp8_find_near_mvs(&x->e_mbd,
1628 x->e_mbd.mode_info_context,
1629 &mode_mv[NEARESTMV], &mode_mv[NEARMV], &best_ref_mv,
1630 mdcounts, x->e_mbd.mbmi.ref_frame, cpi->common.ref_frame_sign_bias);
1633 // Estimate the reference frame signaling cost and add it to the rolling cost variable.
1634 frame_cost = ref_frame_cost[x->e_mbd.mbmi.ref_frame];
1635 rate2 += frame_cost;
1637 if (this_mode <= B_PRED)
1639 for (i = 0; i < 16; i++)
1641 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
1645 // Check to see if the testing frequency for this mode is at its max
1646 // If so then prevent it from being tested and increase the threshold for its testing
1647 if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
1649 if (cpi->mbs_tested_so_far <= cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index])
1651 // Increase the threshold for coding this mode to make it less likely to be chosen
1652 cpi->rd_thresh_mult[mode_index] += 4;
1654 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
1655 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
1657 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
1663 // 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
1664 cpi->mode_test_hit_counts[mode_index] ++;
1666 // Experimental code. Special case for gf and arf zeromv modes. Increase zbin size to supress noise
1667 if (cpi->zbin_mode_boost_enabled)
1669 if ((vp8_mode_order[mode_index] == ZEROMV) && (vp8_ref_frame_order[mode_index] != LAST_FRAME))
1670 cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
1672 cpi->zbin_mode_boost = 0;
1674 vp8cx_mb_init_quantizer(cpi, x);
1681 // Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
1682 vp8_rd_pick_intra4x4mby_modes(cpi, x, &rate, &rate_y, &distortion);
1685 distortion2 += distortion;
1686 rate2 += uv_intra_rate;
1687 rate_uv = uv_intra_rate_tokenonly;
1688 distortion2 += uv_intra_distortion;
1693 int frame_cost_rd = RDFUNC(x->rdmult, x->rddiv, frame_cost, 0, cpi->target_bits_per_mb);
1694 int saved_rate = rate2;
1696 // vp8_rd_pick_best_mbsegmentation looks only at Y and does not account for frame_cost.
1697 // (best_rd - frame_cost_rd) is thus a conservative breakout number.
1698 int breakout_rd = best_rd - frame_cost_rd;
1701 if (x->e_mbd.mbmi.ref_frame == LAST_FRAME)
1702 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) ;
1703 else if (x->e_mbd.mbmi.ref_frame == GOLDEN_FRAME)
1704 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) ;
1706 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) ;
1709 distortion2 += distortion;
1711 // If even the 'Y' rd value of split is higher than best so far then dont bother looking at UV
1712 if (tmp_rd < breakout_rd)
1714 // Now work out UV cost and add it in
1715 vp8_rd_inter_uv(cpi, x, &rate, &distortion, cpi->common.full_pixel);
1718 distortion2 += distortion;
1727 // Trap cases where the best split mode has all vectors coded 0,0 (or all the same)
1732 for (i = 1; i < 16; i++)
1734 BLOCKD *bd = &x->e_mbd.block[i];
1736 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 ) )
1745 // reset mode and mv and jump to newmv
1749 mode_mv[NEWMV].row = x->e_mbd.block[0].bmi.mv.as_mv.row;
1750 mode_mv[NEWMV].col = x->e_mbd.block[0].bmi.mv.as_mv.col;
1751 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
1756 // trap cases where the 8x8s can be promoted to 8x16s or 16x8s
1757 if (0)//x->e_mbd.mbmi.partition_count == 4)
1760 if (x->e_mbd.mbmi.partition_bmi[0].mv.as_int == x->e_mbd.mbmi.partition_bmi[1].mv.as_int
1761 && x->e_mbd.mbmi.partition_bmi[2].mv.as_int == x->e_mbd.mbmi.partition_bmi[3].mv.as_int)
1763 const int *labels = vp8_mbsplits[2];
1764 x->e_mbd.mbmi.partitioning = 0;
1765 rate -= vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + 2);
1766 rate += vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings);
1767 //rate -= x->inter_bmode_costs[ x->e_mbd.mbmi.partition_bmi[1]];
1768 //rate -= x->inter_bmode_costs[ x->e_mbd.mbmi.partition_bmi[3]];
1769 x->e_mbd.mbmi.partition_bmi[1] = x->e_mbd.mbmi.partition_bmi[2];
1779 x->e_mbd.mbmi.ref_frame = INTRA_FRAME;
1780 vp8_build_intra_predictors_mby_ptr(&x->e_mbd);
1782 macro_block_yrd(x, &rate, &distortion, IF_RTCD(&cpi->rtcd.encodemb)) ;
1785 distortion2 += distortion;
1786 rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mbmi.mode];
1787 rate2 += uv_intra_rate;
1788 rate_uv = uv_intra_rate_tokenonly;
1789 distortion2 += uv_intra_distortion;
1795 // Decrement full search counter
1796 if (cpi->check_freq[lf_or_gf] > 0)
1797 cpi->check_freq[lf_or_gf] --;
1801 int bestsme = INT_MAX;
1802 int step_param = cpi->sf.first_step;
1807 // Work out how long a search we should do
1808 search_range = MAXF(abs(best_ref_mv.col), abs(best_ref_mv.row)) >> 3;
1810 if (search_range >= x->vector_range)
1811 x->vector_range = search_range;
1812 else if (x->vector_range > cpi->sf.min_fs_radius)
1815 // Initial step/diamond search
1817 int sadpb = x->sadperbit16;
1819 if (cpi->sf.search_method == HEX)
1821 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);
1822 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1823 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1827 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
1828 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1829 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1831 // Further step/diamond searches as necessary
1833 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
1838 while (n < further_steps)
1846 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
1848 if (thissme < bestsme)
1851 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1852 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1856 d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
1857 d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
1865 // Should we do a full search
1866 if (!cpi->check_freq[lf_or_gf] || cpi->do_full[lf_or_gf])
1869 int full_flag_thresh = 0;
1871 // Update x->vector_range based on best vector found in step search
1872 search_range = MAXF(abs(d->bmi.mv.as_mv.row), abs(d->bmi.mv.as_mv.col));
1874 if (search_range > x->vector_range)
1875 x->vector_range = search_range;
1877 search_range = x->vector_range;
1880 search_range = (search_range > cpi->sf.max_fs_radius) ? cpi->sf.max_fs_radius : search_range;
1882 int sadpb = x->sadperbit16 >> 2;
1883 thissme = cpi->full_search_sad(x, b, d, &best_ref_mv, sadpb, search_range, &cpi->fn_ptr, x->mvcost, x->mvsadcost);
1886 // Barrier threshold to initiating full search
1887 // full_flag_thresh = 10 + (thissme >> 7);
1888 if ((thissme + full_flag_thresh) < bestsme)
1890 cpi->do_full[lf_or_gf] ++;
1893 else if (thissme < bestsme)
1897 cpi->do_full[lf_or_gf] = cpi->do_full[lf_or_gf] >> 1;
1898 cpi->check_freq[lf_or_gf] = cpi->sf.full_freq[lf_or_gf];
1900 // The full search result is actually worse so re-instate the previous best vector
1901 d->bmi.mv.as_mv.row = mode_mv[NEWMV].row;
1902 d->bmi.mv.as_mv.col = mode_mv[NEWMV].col;
1906 if (bestsme < INT_MAX)
1907 // 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
1908 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);
1910 mode_mv[NEWMV].row = d->bmi.mv.as_mv.row;
1911 mode_mv[NEWMV].col = d->bmi.mv.as_mv.col;
1913 // Add the new motion vector cost to our rolling cost variable
1914 rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
1921 // Clip "next_nearest" so that it does not extend to far out of image
1922 if (mode_mv[this_mode].col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN))
1923 mode_mv[this_mode].col = xd->mb_to_left_edge - LEFT_TOP_MARGIN;
1924 else if (mode_mv[this_mode].col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN)
1925 mode_mv[this_mode].col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN;
1927 if (mode_mv[this_mode].row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN))
1928 mode_mv[this_mode].row = xd->mb_to_top_edge - LEFT_TOP_MARGIN;
1929 else if (mode_mv[this_mode].row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN)
1930 mode_mv[this_mode].row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN;
1932 // 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.
1933 if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) &&
1934 ((mode_mv[this_mode].row == 0) && (mode_mv[this_mode].col == 0)))
1941 // Trap vectors that reach beyond the UMV borders
1942 // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
1943 // because of the lack of break statements in the previous two cases.
1944 if (((mode_mv[this_mode].row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].row >> 3) > x->mv_row_max) ||
1945 ((mode_mv[this_mode].col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].col >> 3) > x->mv_col_max))
1948 vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
1949 vp8_build_inter_predictors_mby(&x->e_mbd);
1950 VARIANCE_INVOKE(&cpi->rtcd.variance, get16x16var)(x->src.y_buffer, x->src.y_stride, x->e_mbd.predictor, 16, (unsigned int *)(&sse), &sum);
1952 if (cpi->active_map_enabled && x->active_ptr[0] == 0)
1956 else if (sse < x->encode_breakout)
1958 // Check u and v to make sure skip is ok
1961 sse2 = VP8_UVSSE(x, IF_RTCD(&cpi->rtcd.variance));
1963 if (sse2 * 2 < x->encode_breakout)
1969 disable_skip = 1; // We have no real rate data so trying to adjust for rate_y and rate_uv below will cause problems.
1970 this_rd = RDFUNC(x->rdmult, x->rddiv, rate2, distortion2, cpi->target_bits_per_mb);
1972 break; // (PGW) Move break here from below - for now at least
1978 //intermodecost[mode_index] = vp8_cost_mv_ref(this_mode, mdcounts); // Experimental debug code
1980 // Add in the Mv/mode cost
1981 rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1983 // Y cost and distortion
1984 macro_block_yrd(x, &rate, &distortion, IF_RTCD(&cpi->rtcd.encodemb));
1987 distortion2 += distortion;
1989 // UV cost and distortion
1990 vp8_rd_inter_uv(cpi, x, &rate, &distortion, cpi->common.full_pixel);
1993 distortion2 += distortion;
2002 // Test for the condition where skip block will be activated because there are no non zero coefficients and make any necessary adjustment for rate
2003 if (cpi->common.mb_no_coeff_skip)
2007 for (i = 0; i <= 24; i++)
2009 tteob += x->e_mbd.block[i].eob;
2015 rate2 -= (rate_y + rate_uv);
2017 // Back out no skip flag costing and add in skip flag costing
2018 if (cpi->prob_skip_false)
2020 rate2 += vp8_cost_bit(cpi->prob_skip_false, 1);
2021 rate2 -= vp8_cost_bit(cpi->prob_skip_false, 0);
2030 ratenotuseskip = rate_y + rate_uv + vp8_cost_bit(cpi->prob_skip_false, 0);
2031 rateuseskip = vp8_cost_bit(cpi->prob_skip_false, 1);
2033 if (1) // rateuseskip<ratenotuseskip)
2035 rate2 -= ratenotuseskip;
2036 rate2 += rateuseskip;
2056 // distortion when no coeff is encoded
2057 maxdistortion = macro_block_max_error(x);
2059 ratenotuseskip = rate_y + rate_uv + vp8_cost_bit(cpi->prob_skip_false, 0);
2060 rateuseskip = vp8_cost_bit(cpi->prob_skip_false, 1);
2062 minrate = rateuseskip - ratenotuseskip;
2064 skip_rd = RDFUNC(x->rdmult, x->rddiv, minrate, maxdistortion - distortion2, cpi->target_bits_per_mb);
2066 if (skip_rd + 50 < 0 && x->e_mbd.mbmi.ref_frame != INTRA_FRAME && rate_y + rate_uv < 4000)
2069 rate2 = rate2 + rateuseskip - ratenotuseskip;
2070 distortion2 = maxdistortion;
2083 // Calculate the final RD estimate for this mode
2084 this_rd = RDFUNC(x->rdmult, x->rddiv, rate2, distortion2, cpi->target_bits_per_mb);
2087 // Experimental debug code.
2088 //all_rds[mode_index] = this_rd;
2089 //all_rates[mode_index] = rate2;
2090 //all_dist[mode_index] = distortion2;
2092 if ((x->e_mbd.mbmi.ref_frame == INTRA_FRAME) && (this_rd < *returnintra))
2094 *returnintra = this_rd ;
2097 // Did this mode help.. i.i is it the new best mode
2098 if (this_rd < best_rd || x->skip)
2100 // Note index of best mode so far
2101 best_mode_index = mode_index;
2102 x->e_mbd.mbmi.force_no_skip = force_no_skip;
2104 if (this_mode <= B_PRED)
2106 x->e_mbd.mbmi.uv_mode = uv_intra_mode;
2109 *returnrate = rate2;
2110 *returndistortion = distortion2;
2112 vpx_memcpy(&best_mbmode, &x->e_mbd.mbmi, sizeof(MB_MODE_INFO));
2114 for (i = 0; i < 16; i++)
2116 vpx_memcpy(&best_bmodes[i], &x->e_mbd.block[i].bmi, sizeof(B_MODE_INFO));
2119 // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
2120 cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
2121 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2124 // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
2127 cpi->rd_thresh_mult[mode_index] += 4;
2129 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2130 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2132 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2139 // Reduce the activation RD thresholds for the best choice mode
2140 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
2142 int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 2);
2144 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;
2145 cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
2147 // If we chose a split mode then reset the new MV thresholds as well
2148 /*if ( vp8_mode_order[best_mode_index] == SPLITMV )
2150 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWMV] >> 4);
2151 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;
2152 cpi->rd_threshes[THR_NEWMV] = (cpi->rd_baseline_thresh[THR_NEWMV] >> 7) * cpi->rd_thresh_mult[THR_NEWMV];
2154 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWG] >> 4);
2155 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;
2156 cpi->rd_threshes[THR_NEWG] = (cpi->rd_baseline_thresh[THR_NEWG] >> 7) * cpi->rd_thresh_mult[THR_NEWG];
2158 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWA] >> 4);
2159 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;
2160 cpi->rd_threshes[THR_NEWA] = (cpi->rd_baseline_thresh[THR_NEWA] >> 7) * cpi->rd_thresh_mult[THR_NEWA];
2165 // If we have chosen new mv or split then decay the full search check count more quickly.
2166 if ((vp8_mode_order[best_mode_index] == NEWMV) || (vp8_mode_order[best_mode_index] == SPLITMV))
2168 int lf_or_gf = (vp8_ref_frame_order[best_mode_index] == LAST_FRAME) ? 0 : 1;
2170 if (cpi->check_freq[lf_or_gf] && !cpi->do_full[lf_or_gf])
2172 cpi->check_freq[lf_or_gf] --;
2176 // Keep a record of best mode index that we chose
2177 cpi->last_best_mode_index = best_mode_index;
2179 // Note how often each mode chosen as best
2180 cpi->mode_chosen_counts[best_mode_index] ++;
2183 if (cpi->is_src_frame_alt_ref && (best_mbmode.mode != ZEROMV || best_mbmode.ref_frame != ALTREF_FRAME))
2185 best_mbmode.mode = ZEROMV;
2186 best_mbmode.ref_frame = ALTREF_FRAME;
2187 best_mbmode.mv.as_int = 0;
2188 best_mbmode.uv_mode = 0;
2189 best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
2190 best_mbmode.partitioning = 0;
2191 best_mbmode.dc_diff = 0;
2193 vpx_memcpy(&x->e_mbd.mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2195 for (i = 0; i < 16; i++)
2197 vpx_memset(&x->e_mbd.block[i].bmi, 0, sizeof(B_MODE_INFO));
2200 x->e_mbd.mbmi.mv.as_int = 0;
2207 vpx_memcpy(&x->e_mbd.mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
2209 for (i = 0; i < 16; i++)
2211 vpx_memcpy(&x->e_mbd.block[i].bmi, &best_bmodes[i], sizeof(B_MODE_INFO));
2214 x->e_mbd.mbmi.mv.as_mv = x->e_mbd.block[15].bmi.mv.as_mv;