2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
16 #include "vpx_config.h"
18 #include "vp8/common/pragmas.h"
20 #include "treewriter.h"
22 #include "modecosts.h"
23 #include "encodeintra.h"
24 #include "vp8/common/entropymode.h"
25 #include "vp8/common/reconinter.h"
26 #include "vp8/common/reconintra4x4.h"
27 #include "vp8/common/findnearmv.h"
28 #include "vp8/common/quant_common.h"
31 #include "vp8/common/variance.h"
34 #include "vpx_mem/vpx_mem.h"
35 #include "vp8/common/systemdependent.h"
36 #if CONFIG_TEMPORAL_DENOISING
37 #include "denoising.h"
40 extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);
42 #define MAXF(a,b) (((a) > (b)) ? (a) : (b))
44 typedef struct rate_distortion_struct
53 typedef struct best_mode_struct
59 union b_mode_info bmodes[16];
60 PARTITION_INFO partition;
63 static const int auto_speed_thresh[17] =
84 const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
116 /* This table determines the search order in reference frame priority order,
117 * which may not necessarily match INTRA,LAST,GOLDEN,ARF
119 const int vp8_ref_frame_order[MAX_MODES] =
151 static void fill_token_costs(
152 unsigned int c [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS],
153 const vp8_prob p [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES]
159 for (i = 0; i < BLOCK_TYPES; i++)
160 for (j = 0; j < COEF_BANDS; j++)
161 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
162 // check for pt=0 and band > 1 if block type 0 and 0 if blocktype 1
163 if(k==0 && j>(i==0) )
164 vp8_cost_tokens2((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree,2);
166 vp8_cost_tokens((int *)(c [i][j][k]), p [i][j][k], vp8_coef_tree);
169 static int rd_iifactor [ 32 ] = { 4, 4, 3, 2, 1, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0,
171 0, 0, 0, 0, 0, 0, 0, 0,
172 0, 0, 0, 0, 0, 0, 0, 0,
175 /* values are now correlated to quantizer */
176 static int sad_per_bit16lut[QINDEX_RANGE] =
178 2, 2, 2, 2, 2, 2, 2, 2,
179 2, 2, 2, 2, 2, 2, 2, 2,
180 3, 3, 3, 3, 3, 3, 3, 3,
181 3, 3, 3, 3, 3, 3, 4, 4,
182 4, 4, 4, 4, 4, 4, 4, 4,
183 4, 4, 5, 5, 5, 5, 5, 5,
184 5, 5, 5, 5, 5, 5, 6, 6,
185 6, 6, 6, 6, 6, 6, 6, 6,
186 6, 6, 7, 7, 7, 7, 7, 7,
187 7, 7, 7, 7, 7, 7, 8, 8,
188 8, 8, 8, 8, 8, 8, 8, 8,
189 8, 8, 9, 9, 9, 9, 9, 9,
190 9, 9, 9, 9, 9, 9, 10, 10,
191 10, 10, 10, 10, 10, 10, 11, 11,
192 11, 11, 11, 11, 12, 12, 12, 12,
193 12, 12, 13, 13, 13, 13, 14, 14
195 static int sad_per_bit4lut[QINDEX_RANGE] =
197 2, 2, 2, 2, 2, 2, 3, 3,
198 3, 3, 3, 3, 3, 3, 3, 3,
199 3, 3, 3, 3, 4, 4, 4, 4,
200 4, 4, 4, 4, 4, 4, 5, 5,
201 5, 5, 5, 5, 6, 6, 6, 6,
202 6, 6, 6, 6, 6, 6, 6, 6,
203 7, 7, 7, 7, 7, 7, 7, 7,
204 7, 7, 7, 7, 7, 8, 8, 8,
205 8, 8, 9, 9, 9, 9, 9, 9,
206 10, 10, 10, 10, 10, 10, 10, 10,
207 11, 11, 11, 11, 11, 11, 11, 11,
208 12, 12, 12, 12, 12, 12, 12, 12,
209 13, 13, 13, 13, 13, 13, 13, 14,
210 14, 14, 14, 14, 15, 15, 15, 15,
211 16, 16, 16, 16, 17, 17, 17, 18,
212 18, 18, 19, 19, 19, 20, 20, 20,
215 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
217 cpi->mb.sadperbit16 = sad_per_bit16lut[QIndex];
218 cpi->mb.sadperbit4 = sad_per_bit4lut[QIndex];
221 void vp8_initialize_rd_consts(VP8_COMP *cpi, int Qvalue)
225 double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
226 double rdconst = 2.80;
228 vp8_clear_system_state(); //__asm emms;
230 // Further tests required to see if optimum is different
231 // for key frames, golden frames and arf frames.
232 // if (cpi->common.refresh_golden_frame ||
233 // cpi->common.refresh_alt_ref_frame)
234 cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
236 // Extend rate multiplier along side quantizer zbin increases
237 if (cpi->zbin_over_quant > 0)
242 // Experimental code using the same basic equation as used for Q above
243 // The units of cpi->zbin_over_quant are 1/128 of Q bin size
244 oq_factor = 1.0 + ((double)0.0015625 * cpi->zbin_over_quant);
245 modq = (int)((double)capped_q * oq_factor);
246 cpi->RDMULT = (int)(rdconst * (modq * modq));
249 if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
251 if (cpi->twopass.next_iiratio > 31)
252 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
255 (cpi->RDMULT * rd_iifactor[cpi->twopass.next_iiratio]) >> 4;
258 cpi->mb.errorperbit = (cpi->RDMULT / 110);
259 cpi->mb.errorperbit += (cpi->mb.errorperbit==0);
261 vp8_set_speed_features(cpi);
263 q = (int)pow(Qvalue, 1.25);
268 if (cpi->RDMULT > 1000)
273 for (i = 0; i < MAX_MODES; i++)
275 if (cpi->sf.thresh_mult[i] < INT_MAX)
277 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
281 cpi->rd_threshes[i] = INT_MAX;
284 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
291 for (i = 0; i < MAX_MODES; i++)
293 if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
295 cpi->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
299 cpi->rd_threshes[i] = INT_MAX;
302 cpi->rd_baseline_thresh[i] = cpi->rd_threshes[i];
307 // build token cost array for the type of frame we have now
308 FRAME_CONTEXT *l = &cpi->lfc_n;
310 if(cpi->common.refresh_alt_ref_frame)
312 else if(cpi->common.refresh_golden_frame)
317 (const vp8_prob( *)[8][3][11]) l->coef_probs
322 (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs);
326 // TODO make these mode costs depend on last,alt or gold too. (jbb)
327 vp8_init_mode_costs(cpi);
329 // TODO figure onnnnuut why making mv cost frame type dependent didn't help (jbb)
330 //vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) l->mvc, flags);
336 void vp8_auto_select_speed(VP8_COMP *cpi)
338 int milliseconds_for_compress = (int)(1000000 / cpi->frame_rate);
340 milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
348 f = fopen("speed.stt", "a");
349 fprintf(f, " %8ld %10ld %10ld %10ld\n",
350 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
357 // this is done during parameter valid check
358 if( cpi->oxcf.cpu_used > 16)
359 cpi->oxcf.cpu_used = 16;
360 if( cpi->oxcf.cpu_used < -16)
361 cpi->oxcf.cpu_used = -16;
364 if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
366 if (cpi->avg_pick_mode_time == 0)
372 if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
375 cpi->avg_pick_mode_time = 0;
376 cpi->avg_encode_time = 0;
384 if (milliseconds_for_compress * 100 > cpi->avg_encode_time * auto_speed_thresh[cpi->Speed])
387 cpi->avg_pick_mode_time = 0;
388 cpi->avg_encode_time = 0;
390 // In real-time mode, cpi->speed is in [4, 16].
391 if (cpi->Speed < 4) //if ( cpi->Speed < 0 )
393 cpi->Speed = 4; //cpi->Speed = 0;
406 cpi->avg_pick_mode_time = 0;
407 cpi->avg_encode_time = 0;
411 int vp8_block_error_c(short *coeff, short *dqcoeff)
416 for (i = 0; i < 16; i++)
418 int this_diff = coeff[i] - dqcoeff[i];
419 error += this_diff * this_diff;
425 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
430 int berror, error = 0;
432 for (i = 0; i < 16; i++)
435 bd = &mb->e_mbd.block[i];
439 for (j = dc; j < 16; j++)
441 int this_diff = be->coeff[j] - bd->dqcoeff[j];
442 berror += this_diff * this_diff;
451 int vp8_mbuverror_c(MACROBLOCK *mb)
461 for (i = 16; i < 24; i++)
464 bd = &mb->e_mbd.block[i];
466 error += vp8_block_error_c(be->coeff, bd->dqcoeff);
472 int VP8_UVSSE(MACROBLOCK *x)
474 unsigned char *uptr, *vptr;
475 unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
476 unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
477 int uv_stride = x->block[16].src_stride;
479 unsigned int sse1 = 0;
480 unsigned int sse2 = 0;
481 int mv_row = x->e_mbd.mode_info_context->mbmi.mv.as_mv.row;
482 int mv_col = x->e_mbd.mode_info_context->mbmi.mv.as_mv.col;
484 int pre_stride = x->e_mbd.pre.uv_stride;
499 offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
500 uptr = x->e_mbd.pre.u_buffer + offset;
501 vptr = x->e_mbd.pre.v_buffer + offset;
503 if ((mv_row | mv_col) & 7)
505 vp8_sub_pixel_variance8x8(uptr, pre_stride,
506 mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
507 vp8_sub_pixel_variance8x8(vptr, pre_stride,
508 mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
513 vp8_variance8x8(uptr, pre_stride,
514 upred_ptr, uv_stride, &sse2);
515 vp8_variance8x8(vptr, pre_stride,
516 vpred_ptr, uv_stride, &sse1);
523 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
525 int c = !type; /* start at coef 0, unless Y with Y2 */
526 int eob = (int)(*b->eob);
527 int pt ; /* surrounding block/prev coef predictor */
529 short *qcoeff_ptr = b->qcoeff;
531 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
533 # define QC( I) ( qcoeff_ptr [vp8_default_zig_zag1d[I]] )
538 int t = vp8_dct_value_tokens_ptr[v].Token;
539 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
540 cost += vp8_dct_value_cost_ptr[v];
541 pt = vp8_prev_token_class[t];
547 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
549 pt = (c != !type); // is eob first coefficient;
555 static int vp8_rdcost_mby(MACROBLOCK *mb)
559 MACROBLOCKD *x = &mb->e_mbd;
560 ENTROPY_CONTEXT_PLANES t_above, t_left;
564 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
565 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
567 ta = (ENTROPY_CONTEXT *)&t_above;
568 tl = (ENTROPY_CONTEXT *)&t_left;
570 for (b = 0; b < 16; b++)
571 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
572 ta + vp8_block2above[b], tl + vp8_block2left[b]);
574 cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2,
575 ta + vp8_block2above[24], tl + vp8_block2left[24]);
580 static void macro_block_yrd( MACROBLOCK *mb,
585 MACROBLOCKD *const x = &mb->e_mbd;
586 BLOCK *const mb_y2 = mb->block + 24;
587 BLOCKD *const x_y2 = x->block + 24;
588 short *Y2DCPtr = mb_y2->src_diff;
592 vp8_subtract_mby( mb->src_diff, *(mb->block[0].base_src),
593 mb->block[0].src_stride, mb->e_mbd.predictor, 16);
595 // Fdct and building the 2nd order block
596 for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
598 mb->short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
599 *Y2DCPtr++ = beptr->coeff[0];
600 *Y2DCPtr++ = beptr->coeff[16];
604 mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
607 for (b = 0; b < 16; b++)
609 mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
612 // DC predication and Quantization of 2nd Order block
613 mb->quantize_b(mb_y2, x_y2);
616 d = vp8_mbblock_error(mb, 1) << 2;
617 d += vp8_block_error(mb_y2->coeff, x_y2->dqcoeff);
619 *Distortion = (d >> 4);
622 *Rate = vp8_rdcost_mby(mb);
625 static void copy_predictor(unsigned char *dst, const unsigned char *predictor)
627 const unsigned int *p = (const unsigned int *)predictor;
628 unsigned int *d = (unsigned int *)dst;
634 static int rd_pick_intra4x4block(
639 B_PREDICTION_MODE *best_mode,
640 unsigned int *bmode_costs,
648 B_PREDICTION_MODE mode;
649 int best_rd = INT_MAX;
653 ENTROPY_CONTEXT ta = *a, tempa = *a;
654 ENTROPY_CONTEXT tl = *l, templ = *l;
656 * The predictor buffer is a 2d buffer with a stride of 16. Create
657 * a temp buffer that meets the stride requirements, but we are only
658 * interested in the left 4x4 block
660 DECLARE_ALIGNED_ARRAY(16, unsigned char, best_predictor, 16*4);
661 DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16);
662 int dst_stride = x->e_mbd.dst.y_stride;
663 unsigned char *base_dst = x->e_mbd.dst.y_buffer;
665 for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
670 rate = bmode_costs[mode];
672 vp8_intra4x4_predict(base_dst + b->offset, dst_stride, mode,
674 vp8_subtract_b(be, b, 16);
675 x->short_fdct4x4(be->src_diff, be->coeff, 32);
676 x->quantize_b(be, b);
681 ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ);
683 distortion = vp8_block_error(be->coeff, b->dqcoeff) >> 2;
685 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
687 if (this_rd < best_rd)
691 *bestdistortion = distortion;
696 copy_predictor(best_predictor, b->predictor);
697 vpx_memcpy(best_dqcoeff, b->dqcoeff, 32);
700 b->bmi.as_mode = (B_PREDICTION_MODE)(*best_mode);
702 vp8_short_idct4x4llm(best_dqcoeff, best_predictor, 16, base_dst + b->offset,
708 static int rd_pick_intra4x4mby_modes(VP8_COMP *cpi, MACROBLOCK *mb, int *Rate,
709 int *rate_y, int *Distortion, int best_rd)
711 MACROBLOCKD *const xd = &mb->e_mbd;
713 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
716 int64_t total_rd = 0;
717 ENTROPY_CONTEXT_PLANES t_above, t_left;
720 unsigned int *bmode_costs;
722 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
723 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
725 ta = (ENTROPY_CONTEXT *)&t_above;
726 tl = (ENTROPY_CONTEXT *)&t_left;
728 intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
730 bmode_costs = mb->inter_bmode_costs;
732 for (i = 0; i < 16; i++)
734 MODE_INFO *const mic = xd->mode_info_context;
735 const int mis = xd->mode_info_stride;
736 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
737 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
739 if (mb->e_mbd.frame_type == KEY_FRAME)
741 const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
742 const B_PREDICTION_MODE L = left_block_mode(mic, i);
744 bmode_costs = mb->bmode_costs[A][L];
747 total_rd += rd_pick_intra4x4block(
748 cpi, mb, mb->block + i, xd->block + i, &best_mode, bmode_costs,
749 ta + vp8_block2above[i],
750 tl + vp8_block2left[i], &r, &ry, &d);
756 mic->bmi[i].as_mode = best_mode;
758 if(total_rd >= (int64_t)best_rd)
762 if(total_rd >= (int64_t)best_rd)
766 *rate_y = tot_rate_y;
767 *Distortion = distortion;
769 return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
773 static int rd_pick_intra16x16mby_mode(VP8_COMP *cpi,
779 MB_PREDICTION_MODE mode;
780 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
783 int best_rd = INT_MAX;
785 MACROBLOCKD *xd = &x->e_mbd;
787 //Y Search for 16x16 intra prediction mode
788 for (mode = DC_PRED; mode <= TM_PRED; mode++)
790 xd->mode_info_context->mbmi.mode = mode;
792 vp8_build_intra_predictors_mby_s(xd,
793 xd->dst.y_buffer - xd->dst.y_stride,
794 xd->dst.y_buffer - 1,
799 macro_block_yrd(x, &ratey, &distortion);
800 rate = ratey + x->mbmode_cost[xd->frame_type]
801 [xd->mode_info_context->mbmi.mode];
803 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
805 if (this_rd < best_rd)
807 mode_selected = mode;
811 *Distortion = distortion;
815 xd->mode_info_context->mbmi.mode = mode_selected;
819 static int rd_cost_mbuv(MACROBLOCK *mb)
823 MACROBLOCKD *x = &mb->e_mbd;
824 ENTROPY_CONTEXT_PLANES t_above, t_left;
828 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
829 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
831 ta = (ENTROPY_CONTEXT *)&t_above;
832 tl = (ENTROPY_CONTEXT *)&t_left;
834 for (b = 16; b < 24; b++)
835 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_UV,
836 ta + vp8_block2above[b], tl + vp8_block2left[b]);
842 static int rd_inter16x16_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
843 int *distortion, int fullpixel)
845 vp8_build_inter16x16_predictors_mbuv(&x->e_mbd);
846 vp8_subtract_mbuv(x->src_diff,
847 x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
848 &x->e_mbd.predictor[256], &x->e_mbd.predictor[320], 8);
850 vp8_transform_mbuv(x);
851 vp8_quantize_mbuv(x);
853 *rate = rd_cost_mbuv(x);
854 *distortion = vp8_mbuverror(x) / 4;
856 return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
859 static int rd_inter4x4_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
860 int *distortion, int fullpixel)
862 vp8_build_inter4x4_predictors_mbuv(&x->e_mbd);
863 vp8_subtract_mbuv(x->src_diff,
864 x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
865 &x->e_mbd.predictor[256], &x->e_mbd.predictor[320], 8);
867 vp8_transform_mbuv(x);
868 vp8_quantize_mbuv(x);
870 *rate = rd_cost_mbuv(x);
871 *distortion = vp8_mbuverror(x) / 4;
873 return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
876 static void rd_pick_intra_mbuv_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate, int *rate_tokenonly, int *distortion)
878 MB_PREDICTION_MODE mode;
879 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
880 int best_rd = INT_MAX;
881 int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
883 MACROBLOCKD *xd = &x->e_mbd;
885 for (mode = DC_PRED; mode <= TM_PRED; mode++)
891 xd->mode_info_context->mbmi.uv_mode = mode;
893 vp8_build_intra_predictors_mbuv_s(xd,
894 xd->dst.u_buffer - xd->dst.uv_stride,
895 xd->dst.v_buffer - xd->dst.uv_stride,
896 xd->dst.u_buffer - 1,
897 xd->dst.v_buffer - 1,
899 &xd->predictor[256], &xd->predictor[320],
903 vp8_subtract_mbuv(x->src_diff,
904 x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
905 &xd->predictor[256], &xd->predictor[320], 8);
906 vp8_transform_mbuv(x);
907 vp8_quantize_mbuv(x);
909 rate_to = rd_cost_mbuv(x);
910 rate = rate_to + x->intra_uv_mode_cost[xd->frame_type][xd->mode_info_context->mbmi.uv_mode];
912 distortion = vp8_mbuverror(x) / 4;
914 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
916 if (this_rd < best_rd)
921 *rate_tokenonly = rate_to;
922 mode_selected = mode;
929 xd->mode_info_context->mbmi.uv_mode = mode_selected;
932 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
934 vp8_prob p [VP8_MVREFS-1];
935 assert(NEARESTMV <= m && m <= SPLITMV);
936 vp8_mv_ref_probs(p, near_mv_ref_ct);
937 return vp8_cost_token(vp8_mv_ref_tree, p,
938 vp8_mv_ref_encoding_array - NEARESTMV + m);
941 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv)
943 x->e_mbd.mode_info_context->mbmi.mode = mb;
944 x->e_mbd.mode_info_context->mbmi.mv.as_int = mv->as_int;
947 static int labels2mode(
949 int const *labelings, int which_label,
950 B_PREDICTION_MODE this_mode,
951 int_mv *this_mv, int_mv *best_ref_mv,
955 MACROBLOCKD *const xd = & x->e_mbd;
956 MODE_INFO *const mic = xd->mode_info_context;
957 const int mis = xd->mode_info_stride;
962 /* We have to be careful retrieving previously-encoded motion vectors.
963 Ones from this macroblock have to be pulled from the BLOCKD array
964 as they have not yet made it to the bmi array in our MB_MODE_INFO. */
970 BLOCKD *const d = xd->block + i;
971 const int row = i >> 2, col = i & 3;
975 if (labelings[i] != which_label)
978 if (col && labelings[i] == labelings[i-1])
980 else if (row && labelings[i] == labelings[i-4])
984 // the only time we should do costing for new motion vector or mode
985 // is when we are on a new label (jbb May 08, 2007)
986 switch (m = this_mode)
989 thismvcost = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
992 this_mv->as_int = col ? d[-1].bmi.mv.as_int : left_block_mv(mic, i);
995 this_mv->as_int = row ? d[-4].bmi.mv.as_int : above_block_mv(mic, i, mis);
1004 if (m == ABOVE4X4) // replace above with left if same
1008 left_mv.as_int = col ? d[-1].bmi.mv.as_int :
1009 left_block_mv(mic, i);
1011 if (left_mv.as_int == this_mv->as_int)
1015 cost = x->inter_bmode_costs[ m];
1018 d->bmi.mv.as_int = this_mv->as_int;
1020 x->partition_info->bmi[i].mode = m;
1021 x->partition_info->bmi[i].mv.as_int = this_mv->as_int;
1026 cost += thismvcost ;
1030 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels,
1031 int which_label, ENTROPY_CONTEXT *ta,
1032 ENTROPY_CONTEXT *tl)
1036 MACROBLOCKD *x = &mb->e_mbd;
1038 for (b = 0; b < 16; b++)
1039 if (labels[ b] == which_label)
1040 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_WITH_DC,
1041 ta + vp8_block2above[b],
1042 tl + vp8_block2left[b]);
1047 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label)
1050 unsigned int distortion = 0;
1051 int pre_stride = x->e_mbd.pre.y_stride;
1052 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1055 for (i = 0; i < 16; i++)
1057 if (labels[i] == which_label)
1059 BLOCKD *bd = &x->e_mbd.block[i];
1060 BLOCK *be = &x->block[i];
1062 vp8_build_inter_predictors_b(bd, 16, base_pre, pre_stride, x->e_mbd.subpixel_predict);
1063 vp8_subtract_b(be, bd, 16);
1064 x->short_fdct4x4(be->src_diff, be->coeff, 32);
1066 // set to 0 no way to account for 2nd order DC so discount
1068 x->quantize_b(be, bd);
1070 distortion += vp8_block_error(be->coeff, bd->dqcoeff);
1078 static const unsigned int segmentation_to_sseshift[4] = {3, 3, 2, 0};
1091 B_PREDICTION_MODE modes[16];
1093 unsigned char eobs[16];
1098 int_mv sv_mvp[4]; // save 4 mvp from 8x8
1099 int sv_istep[2]; // save 2 initial step_param for 16x8/8x16
1104 static void rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x,
1105 BEST_SEG_INFO *bsi, unsigned int segmentation)
1111 B_PREDICTION_MODE this_mode;
1115 int this_segment_rd = 0;
1116 int label_mv_thresh;
1120 int segmentyrate = 0;
1122 vp8_variance_fn_ptr_t *v_fn_ptr;
1124 ENTROPY_CONTEXT_PLANES t_above, t_left;
1125 ENTROPY_CONTEXT *ta;
1126 ENTROPY_CONTEXT *tl;
1127 ENTROPY_CONTEXT_PLANES t_above_b, t_left_b;
1128 ENTROPY_CONTEXT *ta_b;
1129 ENTROPY_CONTEXT *tl_b;
1131 vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
1132 vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
1134 ta = (ENTROPY_CONTEXT *)&t_above;
1135 tl = (ENTROPY_CONTEXT *)&t_left;
1136 ta_b = (ENTROPY_CONTEXT *)&t_above_b;
1137 tl_b = (ENTROPY_CONTEXT *)&t_left_b;
1142 v_fn_ptr = &cpi->fn_ptr[segmentation];
1143 labels = vp8_mbsplits[segmentation];
1144 label_count = vp8_mbsplit_count[segmentation];
1146 // 64 makes this threshold really big effectively
1147 // making it so that we very rarely check mvs on
1148 // segments. setting this to 1 would make mv thresh
1149 // roughly equal to what it is for macroblocks
1150 label_mv_thresh = 1 * bsi->mvthresh / label_count ;
1152 // Segmentation method overheads
1153 rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
1154 rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts);
1155 this_segment_rd += RDCOST(x->rdmult, x->rddiv, rate, 0);
1158 for (i = 0; i < label_count; i++)
1160 int_mv mode_mv[B_MODE_COUNT];
1161 int best_label_rd = INT_MAX;
1162 B_PREDICTION_MODE mode_selected = ZERO4X4;
1163 int bestlabelyrate = 0;
1165 // search for the best motion vector on this segment
1166 for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
1171 ENTROPY_CONTEXT_PLANES t_above_s, t_left_s;
1172 ENTROPY_CONTEXT *ta_s;
1173 ENTROPY_CONTEXT *tl_s;
1175 vpx_memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES));
1176 vpx_memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES));
1178 ta_s = (ENTROPY_CONTEXT *)&t_above_s;
1179 tl_s = (ENTROPY_CONTEXT *)&t_left_s;
1181 if (this_mode == NEW4X4)
1189 int bestsme = INT_MAX;
1194 // Is the best so far sufficiently good that we cant justify doing and new motion search.
1195 if (best_label_rd < label_mv_thresh)
1198 if(cpi->compressor_speed)
1200 if (segmentation == BLOCK_8X16 || segmentation == BLOCK_16X8)
1202 bsi->mvp.as_int = bsi->sv_mvp[i].as_int;
1203 if (i==1 && segmentation == BLOCK_16X8)
1204 bsi->mvp.as_int = bsi->sv_mvp[2].as_int;
1206 step_param = bsi->sv_istep[i];
1209 // use previous block's result as next block's MV predictor.
1210 if (segmentation == BLOCK_4X4 && i>0)
1212 bsi->mvp.as_int = x->e_mbd.block[i-1].bmi.mv.as_int;
1213 if (i==4 || i==8 || i==12)
1214 bsi->mvp.as_int = x->e_mbd.block[i-4].bmi.mv.as_int;
1219 further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
1222 int sadpb = x->sadperbit4;
1225 mvp_full.as_mv.row = bsi->mvp.as_mv.row >>3;
1226 mvp_full.as_mv.col = bsi->mvp.as_mv.col >>3;
1229 n = vp8_mbsplit_offset[segmentation][i];
1232 e = &x->e_mbd.block[n];
1235 bestsme = cpi->diamond_search_sad(x, c, e, &mvp_full,
1236 &mode_mv[NEW4X4], step_param,
1237 sadpb, &num00, v_fn_ptr,
1238 x->mvcost, bsi->ref_mv);
1243 while (n < further_steps)
1251 thissme = cpi->diamond_search_sad(x, c, e,
1252 &mvp_full, &temp_mv,
1253 step_param + n, sadpb,
1255 x->mvcost, bsi->ref_mv);
1257 if (thissme < bestsme)
1260 mode_mv[NEW4X4].as_int = temp_mv.as_int;
1266 sseshift = segmentation_to_sseshift[segmentation];
1268 // Should we do a full search (best quality only)
1269 if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000)
1271 /* Check if mvp_full is within the range. */
1272 vp8_clamp_mv(&mvp_full, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1274 thissme = cpi->full_search_sad(x, c, e, &mvp_full,
1275 sadpb, 16, v_fn_ptr,
1276 x->mvcost, bsi->ref_mv);
1278 if (thissme < bestsme)
1281 mode_mv[NEW4X4].as_int = e->bmi.mv.as_int;
1285 // The full search result is actually worse so re-instate the previous best vector
1286 e->bmi.mv.as_int = mode_mv[NEW4X4].as_int;
1291 if (bestsme < INT_MAX)
1295 cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4],
1296 bsi->ref_mv, x->errorperbit, v_fn_ptr, x->mvcost,
1302 rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode],
1303 bsi->ref_mv, x->mvcost);
1305 // Trap vectors that reach beyond the UMV borders
1306 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
1307 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1312 distortion = vp8_encode_inter_mb_segment(x, labels, i) / 4;
1314 labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s);
1317 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1319 if (this_rd < best_label_rd)
1323 bestlabelyrate = labelyrate;
1324 mode_selected = this_mode;
1325 best_label_rd = this_rd;
1327 vpx_memcpy(ta_b, ta_s, sizeof(ENTROPY_CONTEXT_PLANES));
1328 vpx_memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES));
1331 } /*for each 4x4 mode*/
1333 vpx_memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES));
1334 vpx_memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES));
1336 labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected],
1337 bsi->ref_mv, x->mvcost);
1341 segmentyrate += bestlabelyrate;
1342 this_segment_rd += best_label_rd;
1344 if (this_segment_rd >= bsi->segment_rd)
1347 } /* for each label */
1349 if (this_segment_rd < bsi->segment_rd)
1353 bsi->segment_yrate = segmentyrate;
1354 bsi->segment_rd = this_segment_rd;
1355 bsi->segment_num = segmentation;
1357 // store everything needed to come back to this!!
1358 for (i = 0; i < 16; i++)
1360 bsi->mvs[i].as_mv = x->partition_info->bmi[i].mv.as_mv;
1361 bsi->modes[i] = x->partition_info->bmi[i].mode;
1362 bsi->eobs[i] = x->e_mbd.eobs[i];
1368 void vp8_cal_step_param(int sr, int *sp)
1372 if (sr > MAX_FIRST_STEP) sr = MAX_FIRST_STEP;
1373 else if (sr < 1) sr = 1;
1378 *sp = MAX_MVSEARCH_STEPS - 1 - step;
1381 static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x,
1382 int_mv *best_ref_mv, int best_rd,
1383 int *mdcounts, int *returntotrate,
1384 int *returnyrate, int *returndistortion,
1390 vpx_memset(&bsi, 0, sizeof(bsi));
1392 bsi.segment_rd = best_rd;
1393 bsi.ref_mv = best_ref_mv;
1394 bsi.mvp.as_int = best_ref_mv->as_int;
1395 bsi.mvthresh = mvthresh;
1396 bsi.mdcounts = mdcounts;
1398 for(i = 0; i < 16; i++)
1400 bsi.modes[i] = ZERO4X4;
1403 if(cpi->compressor_speed == 0)
1405 /* for now, we will keep the original segmentation order
1406 when in best quality mode */
1407 rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1408 rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1409 rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1410 rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1416 rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1418 if (bsi.segment_rd < best_rd)
1420 int col_min = ((best_ref_mv->as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
1421 int row_min = ((best_ref_mv->as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
1422 int col_max = (best_ref_mv->as_mv.col>>3) + MAX_FULL_PEL_VAL;
1423 int row_max = (best_ref_mv->as_mv.row>>3) + MAX_FULL_PEL_VAL;
1425 int tmp_col_min = x->mv_col_min;
1426 int tmp_col_max = x->mv_col_max;
1427 int tmp_row_min = x->mv_row_min;
1428 int tmp_row_max = x->mv_row_max;
1430 /* Get intersection of UMV window and valid MV window to reduce # of checks in diamond search. */
1431 if (x->mv_col_min < col_min )
1432 x->mv_col_min = col_min;
1433 if (x->mv_col_max > col_max )
1434 x->mv_col_max = col_max;
1435 if (x->mv_row_min < row_min )
1436 x->mv_row_min = row_min;
1437 if (x->mv_row_max > row_max )
1438 x->mv_row_max = row_max;
1440 /* Get 8x8 result */
1441 bsi.sv_mvp[0].as_int = bsi.mvs[0].as_int;
1442 bsi.sv_mvp[1].as_int = bsi.mvs[2].as_int;
1443 bsi.sv_mvp[2].as_int = bsi.mvs[8].as_int;
1444 bsi.sv_mvp[3].as_int = bsi.mvs[10].as_int;
1446 /* Use 8x8 result as 16x8/8x16's predictor MV. Adjust search range according to the closeness of 2 MV. */
1449 sr = MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[2].as_mv.row))>>3, (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[2].as_mv.col))>>3);
1450 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1452 sr = MAXF((abs(bsi.sv_mvp[1].as_mv.row - bsi.sv_mvp[3].as_mv.row))>>3, (abs(bsi.sv_mvp[1].as_mv.col - bsi.sv_mvp[3].as_mv.col))>>3);
1453 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1455 rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1460 sr = MAXF((abs(bsi.sv_mvp[0].as_mv.row - bsi.sv_mvp[1].as_mv.row))>>3, (abs(bsi.sv_mvp[0].as_mv.col - bsi.sv_mvp[1].as_mv.col))>>3);
1461 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1463 sr = MAXF((abs(bsi.sv_mvp[2].as_mv.row - bsi.sv_mvp[3].as_mv.row))>>3, (abs(bsi.sv_mvp[2].as_mv.col - bsi.sv_mvp[3].as_mv.col))>>3);
1464 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1466 rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1469 /* If 8x8 is better than 16x8/8x16, then do 4x4 search */
1470 /* Not skip 4x4 if speed=0 (good quality) */
1471 if (cpi->sf.no_skip_block4x4_search || bsi.segment_num == BLOCK_8X8) /* || (sv_segment_rd8x8-bsi.segment_rd) < sv_segment_rd8x8>>5) */
1473 bsi.mvp.as_int = bsi.sv_mvp[0].as_int;
1474 rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1477 /* restore UMV window */
1478 x->mv_col_min = tmp_col_min;
1479 x->mv_col_max = tmp_col_max;
1480 x->mv_row_min = tmp_row_min;
1481 x->mv_row_max = tmp_row_max;
1485 /* set it to the best */
1486 for (i = 0; i < 16; i++)
1488 BLOCKD *bd = &x->e_mbd.block[i];
1490 bd->bmi.mv.as_int = bsi.mvs[i].as_int;
1491 *bd->eob = bsi.eobs[i];
1494 *returntotrate = bsi.r;
1495 *returndistortion = bsi.d;
1496 *returnyrate = bsi.segment_yrate;
1498 /* save partitions */
1499 x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num;
1500 x->partition_info->count = vp8_mbsplit_count[bsi.segment_num];
1502 for (i = 0; i < x->partition_info->count; i++)
1506 j = vp8_mbsplit_offset[bsi.segment_num][i];
1508 x->partition_info->bmi[i].mode = bsi.modes[j];
1509 x->partition_info->bmi[i].mv.as_mv = bsi.mvs[j].as_mv;
1512 * used to set x->e_mbd.mode_info_context->mbmi.mv.as_int
1514 x->partition_info->bmi[15].mv.as_int = bsi.mvs[15].as_int;
1516 return bsi.segment_rd;
1519 //The improved MV prediction
1524 const MODE_INFO *here,
1527 int *ref_frame_sign_bias,
1532 const MODE_INFO *above = here - xd->mode_info_stride;
1533 const MODE_INFO *left = here - 1;
1534 const MODE_INFO *aboveleft = above - 1;
1548 if(here->mbmi.ref_frame != INTRA_FRAME)
1550 near_mvs[0].as_int = near_mvs[1].as_int = near_mvs[2].as_int = near_mvs[3].as_int = near_mvs[4].as_int = near_mvs[5].as_int = near_mvs[6].as_int = near_mvs[7].as_int = 0;
1551 near_ref[0] = near_ref[1] = near_ref[2] = near_ref[3] = near_ref[4] = near_ref[5] = near_ref[6] = near_ref[7] = 0;
1553 // read in 3 nearby block's MVs from current frame as prediction candidates.
1554 if (above->mbmi.ref_frame != INTRA_FRAME)
1556 near_mvs[vcnt].as_int = above->mbmi.mv.as_int;
1557 mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1558 near_ref[vcnt] = above->mbmi.ref_frame;
1561 if (left->mbmi.ref_frame != INTRA_FRAME)
1563 near_mvs[vcnt].as_int = left->mbmi.mv.as_int;
1564 mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1565 near_ref[vcnt] = left->mbmi.ref_frame;
1568 if (aboveleft->mbmi.ref_frame != INTRA_FRAME)
1570 near_mvs[vcnt].as_int = aboveleft->mbmi.mv.as_int;
1571 mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1572 near_ref[vcnt] = aboveleft->mbmi.ref_frame;
1576 // read in 5 nearby block's MVs from last frame.
1577 if(cpi->common.last_frame_type != KEY_FRAME)
1579 mb_offset = (-xd->mb_to_top_edge/128 + 1) * (xd->mode_info_stride +1) + (-xd->mb_to_left_edge/128 +1) ;
1581 // current in last frame
1582 if (cpi->lf_ref_frame[mb_offset] != INTRA_FRAME)
1584 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset].as_int;
1585 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1586 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset];
1590 // above in last frame
1591 if (cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1] != INTRA_FRAME)
1593 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset - xd->mode_info_stride-1].as_int;
1594 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset - xd->mode_info_stride-1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1595 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1];
1599 // left in last frame
1600 if (cpi->lf_ref_frame[mb_offset-1] != INTRA_FRAME)
1602 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset -1].as_int;
1603 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset -1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1604 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - 1];
1608 // right in last frame
1609 if (cpi->lf_ref_frame[mb_offset +1] != INTRA_FRAME)
1611 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset +1].as_int;
1612 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1613 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset +1];
1617 // below in last frame
1618 if (cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1] != INTRA_FRAME)
1620 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset + xd->mode_info_stride +1].as_int;
1621 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset + xd->mode_info_stride +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1622 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1];
1627 for(i=0; i< vcnt; i++)
1629 if(near_ref[near_sadidx[i]] != INTRA_FRAME)
1631 if(here->mbmi.ref_frame == near_ref[near_sadidx[i]])
1633 mv.as_int = near_mvs[near_sadidx[i]].as_int;
1646 for(i=0; i<vcnt; i++)
1648 mvx[i] = near_mvs[i].as_mv.row;
1649 mvy[i] = near_mvs[i].as_mv.col;
1652 insertsortmv(mvx, vcnt);
1653 insertsortmv(mvy, vcnt);
1654 mv.as_mv.row = mvx[vcnt/2];
1655 mv.as_mv.col = mvy[vcnt/2];
1658 //sr is set to 0 to allow calling function to decide the search range.
1663 /* Set up return values */
1664 mvp->as_int = mv.as_int;
1665 vp8_clamp_mv2(mvp, xd);
1668 void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, int recon_yoffset, int near_sadidx[])
1671 int near_sad[8] = {0}; // 0-cf above, 1-cf left, 2-cf aboveleft, 3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below
1672 BLOCK *b = &x->block[0];
1673 unsigned char *src_y_ptr = *(b->base_src);
1675 //calculate sad for current frame 3 nearby MBs.
1676 if( xd->mb_to_top_edge==0 && xd->mb_to_left_edge ==0)
1678 near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX;
1679 }else if(xd->mb_to_top_edge==0)
1680 { //only has left MB for sad calculation.
1681 near_sad[0] = near_sad[2] = INT_MAX;
1682 near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
1683 }else if(xd->mb_to_left_edge ==0)
1684 { //only has left MB for sad calculation.
1685 near_sad[1] = near_sad[2] = INT_MAX;
1686 near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
1689 near_sad[0] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride *16,xd->dst.y_stride, 0x7fffffff);
1690 near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, 0x7fffffff);
1691 near_sad[2] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - xd->dst.y_stride *16 -16,xd->dst.y_stride, 0x7fffffff);
1694 if(cpi->common.last_frame_type != KEY_FRAME)
1696 //calculate sad for last frame 5 nearby MBs.
1697 unsigned char *pre_y_buffer = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset;
1698 int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride;
1700 if(xd->mb_to_top_edge==0) near_sad[4] = INT_MAX;
1701 if(xd->mb_to_left_edge ==0) near_sad[5] = INT_MAX;
1702 if(xd->mb_to_right_edge ==0) near_sad[6] = INT_MAX;
1703 if(xd->mb_to_bottom_edge==0) near_sad[7] = INT_MAX;
1705 if(near_sad[4] != INT_MAX)
1706 near_sad[4] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer - pre_y_stride *16, pre_y_stride, 0x7fffffff);
1707 if(near_sad[5] != INT_MAX)
1708 near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer - 16, pre_y_stride, 0x7fffffff);
1709 near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer, pre_y_stride, 0x7fffffff);
1710 if(near_sad[6] != INT_MAX)
1711 near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer + 16, pre_y_stride, 0x7fffffff);
1712 if(near_sad[7] != INT_MAX)
1713 near_sad[7] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer + pre_y_stride *16, pre_y_stride, 0x7fffffff);
1716 if(cpi->common.last_frame_type != KEY_FRAME)
1718 insertsortsad(near_sad, near_sadidx, 8);
1721 insertsortsad(near_sad, near_sadidx, 3);
1725 static void rd_update_mvcount(VP8_COMP *cpi, MACROBLOCK *x, int_mv *best_ref_mv)
1727 if (x->e_mbd.mode_info_context->mbmi.mode == SPLITMV)
1731 for (i = 0; i < x->partition_info->count; i++)
1733 if (x->partition_info->bmi[i].mode == NEW4X4)
1735 cpi->MVcount[0][mv_max+((x->partition_info->bmi[i].mv.as_mv.row
1736 - best_ref_mv->as_mv.row) >> 1)]++;
1737 cpi->MVcount[1][mv_max+((x->partition_info->bmi[i].mv.as_mv.col
1738 - best_ref_mv->as_mv.col) >> 1)]++;
1742 else if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
1744 cpi->MVcount[0][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.row
1745 - best_ref_mv->as_mv.row) >> 1)]++;
1746 cpi->MVcount[1][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.col
1747 - best_ref_mv->as_mv.col) >> 1)]++;
1751 static int evaluate_inter_mode_rd(int mdcounts[4],
1752 RATE_DISTORTION* rd,
1754 VP8_COMP *cpi, MACROBLOCK *x)
1756 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
1757 BLOCK *b = &x->block[0];
1758 MACROBLOCKD *xd = &x->e_mbd;
1760 vp8_build_inter16x16_predictors_mby(&x->e_mbd, x->e_mbd.predictor, 16);
1762 if (cpi->active_map_enabled && x->active_ptr[0] == 0) {
1765 else if (x->encode_breakout)
1769 int threshold = (xd->block[0].dequant[1]
1770 * xd->block[0].dequant[1] >>4);
1772 if(threshold < x->encode_breakout)
1773 threshold = x->encode_breakout;
1775 var = vp8_variance16x16
1776 (*(b->base_src), b->src_stride,
1777 x->e_mbd.predictor, 16, &sse);
1779 if (sse < threshold)
1781 unsigned int q2dc = xd->block[24].dequant[0];
1782 /* If theres is no codeable 2nd order dc
1783 or a very small uniform pixel change change */
1784 if ((sse - var < q2dc * q2dc >>4) ||
1785 (sse /2 > var && sse-var < 64))
1787 // Check u and v to make sure skip is ok
1788 int sse2= VP8_UVSSE(x);
1789 if (sse2 * 2 < threshold)
1792 rd->distortion2 = sse + sse2;
1795 /* for best_yrd calculation */
1797 rd->distortion_uv = sse2;
1800 return RDCOST(x->rdmult, x->rddiv, rd->rate2,
1808 //intermodecost[mode_index] = vp8_cost_mv_ref(this_mode, mdcounts); // Experimental debug code
1810 // Add in the Mv/mode cost
1811 rd->rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1813 // Y cost and distortion
1814 macro_block_yrd(x, &rd->rate_y, &distortion);
1815 rd->rate2 += rd->rate_y;
1816 rd->distortion2 += distortion;
1818 // UV cost and distortion
1819 rd_inter16x16_uv(cpi, x, &rd->rate_uv, &rd->distortion_uv,
1820 cpi->common.full_pixel);
1821 rd->rate2 += rd->rate_uv;
1822 rd->distortion2 += rd->distortion_uv;
1826 static int calculate_final_rd_costs(int this_rd,
1827 RATE_DISTORTION* rd,
1831 int intra_rd_penalty,
1832 VP8_COMP *cpi, MACROBLOCK *x)
1834 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
1835 // Where skip is allowable add in the default per mb cost for the no skip case.
1836 // where we then decide to skip we have to delete this and replace it with the
1837 // cost of signallying a skip
1838 if (cpi->common.mb_no_coeff_skip)
1840 *other_cost += vp8_cost_bit(cpi->prob_skip_false, 0);
1841 rd->rate2 += *other_cost;
1844 /* Estimate the reference frame signaling cost and add it
1845 * to the rolling cost variable.
1848 x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
1852 // Test for the condition where skip block will be activated because there are no non zero coefficients and make any necessary adjustment for rate
1853 if (cpi->common.mb_no_coeff_skip)
1857 int has_y2_block = (this_mode!=SPLITMV && this_mode!=B_PRED);
1861 tteob += x->e_mbd.eobs[24];
1863 for (i = 0; i < 16; i++)
1864 tteob += (x->e_mbd.eobs[i] > has_y2_block);
1866 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
1868 for (i = 16; i < 24; i++)
1869 tteob += x->e_mbd.eobs[i];
1872 tteob += uv_intra_tteob;
1876 rd->rate2 -= (rd->rate_y + rd->rate_uv);
1877 //for best_yrd calculation
1880 // Back out no skip flag costing and add in skip flag costing
1881 if (cpi->prob_skip_false)
1885 prob_skip_cost = vp8_cost_bit(cpi->prob_skip_false, 1);
1886 prob_skip_cost -= vp8_cost_bit(cpi->prob_skip_false, 0);
1887 rd->rate2 += prob_skip_cost;
1888 *other_cost += prob_skip_cost;
1892 // Calculate the final RD estimate for this mode
1893 this_rd = RDCOST(x->rdmult, x->rddiv, rd->rate2, rd->distortion2);
1894 if (this_rd < INT_MAX && x->e_mbd.mode_info_context->mbmi.ref_frame
1896 this_rd += intra_rd_penalty;
1901 static void update_best_mode(BEST_MODE* best_mode, int this_rd,
1902 RATE_DISTORTION* rd, int other_cost, MACROBLOCK *x)
1904 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
1907 x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
1909 /* Calculate the final y RD estimate for this mode */
1910 best_mode->yrd = RDCOST(x->rdmult, x->rddiv, (rd->rate2-rd->rate_uv-other_cost),
1911 (rd->distortion2-rd->distortion_uv));
1913 best_mode->rd = this_rd;
1914 vpx_memcpy(&best_mode->mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
1915 vpx_memcpy(&best_mode->partition, x->partition_info, sizeof(PARTITION_INFO));
1917 if ((this_mode == B_PRED) || (this_mode == SPLITMV))
1920 for (i = 0; i < 16; i++)
1922 best_mode->bmodes[i] = x->e_mbd.block[i].bmi;
1927 void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
1928 int recon_uvoffset, int *returnrate,
1929 int *returndistortion, int *returnintra)
1931 BLOCK *b = &x->block[0];
1932 BLOCKD *d = &x->e_mbd.block[0];
1933 MACROBLOCKD *xd = &x->e_mbd;
1934 int_mv best_ref_mv_sb[2];
1935 int_mv mode_mv_sb[2][MB_MODE_COUNT];
1938 MB_PREDICTION_MODE this_mode;
1940 int best_mode_index = 0;
1941 BEST_MODE best_mode;
1948 int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
1949 int uv_intra_tteob = 0;
1950 int uv_intra_done = 0;
1952 MB_PREDICTION_MODE uv_intra_mode = 0;
1954 int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
1956 int sr=0; //search range got from mv_pred(). It uses step_param levels. (0-7)
1958 unsigned char *plane[4][3];
1959 int ref_frame_map[4];
1962 int intra_rd_penalty = 10* vp8_dc_quant(cpi->common.base_qindex,
1963 cpi->common.y1dc_delta_q);
1965 mode_mv = mode_mv_sb[sign_bias];
1966 best_ref_mv.as_int = 0;
1967 best_mode.rd = INT_MAX;
1968 best_mode.yrd = INT_MAX;
1969 best_mode.intra_rd = INT_MAX;
1970 vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
1971 vpx_memset(&best_mode.mbmode, 0, sizeof(best_mode.mbmode));
1972 vpx_memset(&best_mode.bmodes, 0, sizeof(best_mode.bmodes));
1974 /* Setup search priorities */
1975 get_reference_search_order(cpi, ref_frame_map);
1977 /* Check to see if there is at least 1 valid reference frame that we need
1978 * to calculate near_mvs.
1980 if (ref_frame_map[1] > 0)
1982 sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
1983 x->e_mbd.mode_info_context,
1988 cpi->common.ref_frame_sign_bias);
1990 mode_mv = mode_mv_sb[sign_bias];
1991 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
1994 get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
1996 *returnintra = INT_MAX;
1997 cpi->mbs_tested_so_far++; // Count of the number of MBs tested so far this frame
2001 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
2003 int this_rd = INT_MAX;
2004 int disable_skip = 0;
2006 int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
2008 // Test best rd so far against threshold for trying this mode.
2009 if (best_mode.rd <= cpi->rd_threshes[mode_index])
2012 if (this_ref_frame < 0)
2015 // These variables hold are rolling total cost and distortion for this mode
2019 this_mode = vp8_mode_order[mode_index];
2021 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
2022 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
2024 // Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
2025 // unless ARNR filtering is enabled in which case we want
2026 // an unfiltered alternative
2027 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
2029 if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
2033 /* everything but intra */
2034 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
2036 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
2037 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
2038 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
2040 if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
2042 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
2043 mode_mv = mode_mv_sb[sign_bias];
2044 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
2048 // Check to see if the testing frequency for this mode is at its max
2049 // If so then prevent it from being tested and increase the threshold for its testing
2050 if (cpi->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
2052 if (cpi->mbs_tested_so_far <= cpi->mode_check_freq[mode_index] * cpi->mode_test_hit_counts[mode_index])
2054 // Increase the threshold for coding this mode to make it less likely to be chosen
2055 cpi->rd_thresh_mult[mode_index] += 4;
2057 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2058 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2060 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2066 // 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
2067 cpi->mode_test_hit_counts[mode_index] ++;
2069 // Experimental code. Special case for gf and arf zeromv modes. Increase zbin size to supress noise
2070 if (cpi->zbin_mode_boost_enabled)
2072 if ( this_ref_frame == INTRA_FRAME )
2073 cpi->zbin_mode_boost = 0;
2076 if (vp8_mode_order[mode_index] == ZEROMV)
2078 if (this_ref_frame != LAST_FRAME)
2079 cpi->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
2081 cpi->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
2083 else if (vp8_mode_order[mode_index] == SPLITMV)
2084 cpi->zbin_mode_boost = 0;
2086 cpi->zbin_mode_boost = MV_ZBIN_BOOST;
2089 vp8_update_zbin_extra(cpi, x);
2092 if(!uv_intra_done && this_ref_frame == INTRA_FRAME)
2094 rd_pick_intra_mbuv_mode(cpi, x, &uv_intra_rate,
2095 &uv_intra_rate_tokenonly,
2096 &uv_intra_distortion);
2097 uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
2100 * Total of the eobs is used later to further adjust rate2. Since uv
2101 * block's intra eobs will be overwritten when we check inter modes,
2102 * we need to save uv_intra_tteob here.
2104 for (i = 16; i < 24; i++)
2105 uv_intra_tteob += x->e_mbd.eobs[i];
2116 // Note the rate value returned here includes the cost of coding the BPRED mode : x->mbmode_cost[x->e_mbd.frame_type][BPRED];
2118 tmp_rd = rd_pick_intra4x4mby_modes(cpi, x, &rate, &rd.rate_y, &distortion, best_mode.yrd);
2120 rd.distortion2 += distortion;
2122 if(tmp_rd < best_mode.yrd)
2124 rd.rate2 += uv_intra_rate;
2125 rd.rate_uv = uv_intra_rate_tokenonly;
2126 rd.distortion2 += uv_intra_distortion;
2127 rd.distortion_uv = uv_intra_distortion;
2143 this_rd_thresh = (vp8_ref_frame_order[mode_index] == 1) ? cpi->rd_threshes[THR_NEW1] : cpi->rd_threshes[THR_NEW3];
2144 this_rd_thresh = (vp8_ref_frame_order[mode_index] == 2) ? cpi->rd_threshes[THR_NEW2] : this_rd_thresh;
2146 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv,
2147 best_mode.yrd, mdcounts,
2148 &rate, &rd.rate_y, &distortion, this_rd_thresh) ;
2151 rd.distortion2 += distortion;
2153 // If even the 'Y' rd value of split is higher than best so far then dont bother looking at UV
2154 if (tmp_rd < best_mode.yrd)
2156 // Now work out UV cost and add it in
2157 rd_inter4x4_uv(cpi, x, &rd.rate_uv, &rd.distortion_uv, cpi->common.full_pixel);
2158 rd.rate2 += rd.rate_uv;
2159 rd.distortion2 += rd.distortion_uv;
2174 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
2176 vp8_build_intra_predictors_mby_s(xd,
2177 xd->dst.y_buffer - xd->dst.y_stride,
2178 xd->dst.y_buffer - 1,
2182 macro_block_yrd(x, &rd.rate_y, &distortion) ;
2183 rd.rate2 += rd.rate_y;
2184 rd.distortion2 += distortion;
2185 rd.rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
2186 rd.rate2 += uv_intra_rate;
2187 rd.rate_uv = uv_intra_rate_tokenonly;
2188 rd.distortion2 += uv_intra_distortion;
2189 rd.distortion_uv = uv_intra_distortion;
2196 int bestsme = INT_MAX;
2197 int step_param = cpi->sf.first_step;
2200 int do_refine=1; /* If last step (1-away) of n-step search doesn't pick the center point as the best match,
2201 we will do a final 1-away diamond refining search */
2203 int sadpb = x->sadperbit16;
2206 int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
2207 int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
2208 int col_max = (best_ref_mv.as_mv.col>>3) + MAX_FULL_PEL_VAL;
2209 int row_max = (best_ref_mv.as_mv.row>>3) + MAX_FULL_PEL_VAL;
2211 int tmp_col_min = x->mv_col_min;
2212 int tmp_col_max = x->mv_col_max;
2213 int tmp_row_min = x->mv_row_min;
2214 int tmp_row_max = x->mv_row_max;
2218 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
2222 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
2223 x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
2225 mvp_full.as_mv.col = mvp.as_mv.col>>3;
2226 mvp_full.as_mv.row = mvp.as_mv.row>>3;
2228 // Get intersection of UMV window and valid MV window to reduce # of checks in diamond search.
2229 if (x->mv_col_min < col_min )
2230 x->mv_col_min = col_min;
2231 if (x->mv_col_max > col_max )
2232 x->mv_col_max = col_max;
2233 if (x->mv_row_min < row_min )
2234 x->mv_row_min = row_min;
2235 if (x->mv_row_max > row_max )
2236 x->mv_row_max = row_max;
2238 //adjust search range according to sr from mv prediction
2242 // Initial step/diamond search
2244 bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full, &d->bmi.mv,
2245 step_param, sadpb, &num00,
2246 &cpi->fn_ptr[BLOCK_16X16],
2247 x->mvcost, &best_ref_mv);
2248 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2250 // Further step/diamond searches as necessary
2252 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
2257 /* If there won't be more n-step search, check to see if refining search is needed. */
2258 if (n > further_steps)
2261 while (n < further_steps)
2269 thissme = cpi->diamond_search_sad(x, b, d, &mvp_full,
2270 &d->bmi.mv, step_param + n, sadpb, &num00,
2271 &cpi->fn_ptr[BLOCK_16X16], x->mvcost,
2274 /* check to see if refining search is needed. */
2275 if (num00 > (further_steps-n))
2278 if (thissme < bestsme)
2281 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2285 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
2291 /* final 1-away diamond refining search */
2296 //It seems not a good way to set search_range. Need further investigation.
2297 //search_range = MAXF(abs((mvp.row>>3) - d->bmi.mv.as_mv.row), abs((mvp.col>>3) - d->bmi.mv.as_mv.col));
2300 //thissme = cpi->full_search_sad(x, b, d, &d->bmi.mv.as_mv, sadpb, search_range, &cpi->fn_ptr[BLOCK_16X16], x->mvcost, &best_ref_mv);
2301 thissme = cpi->refining_search_sad(x, b, d, &d->bmi.mv, sadpb,
2302 search_range, &cpi->fn_ptr[BLOCK_16X16],
2303 x->mvcost, &best_ref_mv);
2305 if (thissme < bestsme)
2308 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2312 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
2316 x->mv_col_min = tmp_col_min;
2317 x->mv_col_max = tmp_col_max;
2318 x->mv_row_min = tmp_row_min;
2319 x->mv_row_max = tmp_row_max;
2321 if (bestsme < INT_MAX)
2323 int dis; /* TODO: use dis in distortion calculation later. */
2325 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
2327 &cpi->fn_ptr[BLOCK_16X16],
2328 x->mvcost, &dis, &sse);
2331 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2333 // Add the new motion vector cost to our rolling cost variable
2334 rd.rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
2339 // Clip "next_nearest" so that it does not extend to far out of image
2340 vp8_clamp_mv2(&mode_mv[this_mode], xd);
2342 // 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.
2343 if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) && (mode_mv[this_mode].as_int == 0))
2348 // Trap vectors that reach beyond the UMV borders
2349 // Note that ALL New MV, Nearest MV Near MV and Zero MV code drops through to this point
2350 // because of the lack of break statements in the previous two cases.
2351 if (((mode_mv[this_mode].as_mv.row >> 3) < x->mv_row_min) || ((mode_mv[this_mode].as_mv.row >> 3) > x->mv_row_max) ||
2352 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
2355 vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
2356 this_rd = evaluate_inter_mode_rd(mdcounts, &rd,
2357 &disable_skip, cpi, x);
2364 this_rd = calculate_final_rd_costs(this_rd, &rd, &other_cost,
2365 disable_skip, uv_intra_tteob,
2366 intra_rd_penalty, cpi, x);
2368 // Keep record of best intra distortion
2369 if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) &&
2370 (this_rd < best_mode.intra_rd) )
2372 best_mode.intra_rd = this_rd;
2373 *returnintra = rd.distortion2 ;
2376 #if CONFIG_TEMPORAL_DENOISING
2377 if (cpi->oxcf.noise_sensitivity)
2379 // Store the best NEWMV in x for later use in the denoiser.
2380 // We are restricted to the LAST_FRAME since the denoiser only keeps
2381 // one filter state.
2382 if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
2383 x->e_mbd.mode_info_context->mbmi.ref_frame == LAST_FRAME)
2385 x->e_mbd.best_sse_inter_mode = NEWMV;
2386 x->e_mbd.best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
2387 x->e_mbd.need_to_clamp_best_mvs =
2388 x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
2393 // Did this mode help.. i.i is it the new best mode
2394 if (this_rd < best_mode.rd || x->skip)
2396 // Note index of best mode so far
2397 best_mode_index = mode_index;
2398 *returnrate = rd.rate2;
2399 *returndistortion = rd.distortion2;
2400 if (this_mode <= B_PRED)
2402 x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode;
2403 /* required for left and above block mv */
2404 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2406 update_best_mode(&best_mode, this_rd, &rd, other_cost, x);
2409 // Testing this mode gave rise to an improvement in best error score. Lower threshold a bit for next time
2410 cpi->rd_thresh_mult[mode_index] = (cpi->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? cpi->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
2411 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2414 // If the mode did not help improve the best error case then raise the threshold for testing that mode next time around.
2417 cpi->rd_thresh_mult[mode_index] += 4;
2419 if (cpi->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2420 cpi->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2422 cpi->rd_threshes[mode_index] = (cpi->rd_baseline_thresh[mode_index] >> 7) * cpi->rd_thresh_mult[mode_index];
2430 // Reduce the activation RD thresholds for the best choice mode
2431 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
2433 int best_adjustment = (cpi->rd_thresh_mult[best_mode_index] >> 2);
2435 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;
2436 cpi->rd_threshes[best_mode_index] = (cpi->rd_baseline_thresh[best_mode_index] >> 7) * cpi->rd_thresh_mult[best_mode_index];
2438 // If we chose a split mode then reset the new MV thresholds as well
2439 /*if ( vp8_mode_order[best_mode_index] == SPLITMV )
2441 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWMV] >> 4);
2442 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;
2443 cpi->rd_threshes[THR_NEWMV] = (cpi->rd_baseline_thresh[THR_NEWMV] >> 7) * cpi->rd_thresh_mult[THR_NEWMV];
2445 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWG] >> 4);
2446 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;
2447 cpi->rd_threshes[THR_NEWG] = (cpi->rd_baseline_thresh[THR_NEWG] >> 7) * cpi->rd_thresh_mult[THR_NEWG];
2449 best_adjustment = 4; //(cpi->rd_thresh_mult[THR_NEWA] >> 4);
2450 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;
2451 cpi->rd_threshes[THR_NEWA] = (cpi->rd_baseline_thresh[THR_NEWA] >> 7) * cpi->rd_thresh_mult[THR_NEWA];
2456 // Note how often each mode chosen as best
2457 cpi->mode_chosen_counts[best_mode_index] ++;
2459 #if CONFIG_TEMPORAL_DENOISING
2460 if (cpi->oxcf.noise_sensitivity)
2462 if (x->e_mbd.best_sse_inter_mode == DC_PRED) {
2463 // No best MV found.
2464 x->e_mbd.best_sse_inter_mode = best_mode.mbmode.mode;
2465 x->e_mbd.best_sse_mv = best_mode.mbmode.mv;
2466 x->e_mbd.need_to_clamp_best_mvs = best_mode.mbmode.need_to_clamp_mvs;
2469 // TODO(holmer): No SSEs are calculated in rdopt.c. What else can be used?
2470 vp8_denoiser_denoise_mb(&cpi->denoiser, x, 0, 0,
2471 recon_yoffset, recon_uvoffset);
2472 // Reevalute ZEROMV if the current mode is INTRA.
2473 if (best_mode.mbmode.ref_frame == INTRA_FRAME)
2475 int this_rd = INT_MAX;
2476 int disable_skip = 0;
2478 vpx_memset(&rd, 0, sizeof(rd));
2479 x->e_mbd.mode_info_context->mbmi.ref_frame = LAST_FRAME;
2480 rd.rate2 += x->ref_frame_cost[LAST_FRAME];
2481 rd.rate2 += vp8_cost_mv_ref(ZEROMV, mdcounts);
2482 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
2483 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
2484 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2485 this_rd = evaluate_inter_mode_rd(mdcounts, &rd, &disable_skip, cpi, x);
2486 this_rd = calculate_final_rd_costs(this_rd, &rd, &other_cost,
2487 disable_skip, uv_intra_tteob,
2488 intra_rd_penalty, cpi, x);
2489 if (this_rd < best_mode.rd || x->skip)
2491 // Note index of best mode so far
2492 best_mode_index = mode_index;
2493 *returnrate = rd.rate2;
2494 *returndistortion = rd.distortion2;
2495 update_best_mode(&best_mode, this_rd, &rd, other_cost, x);
2501 if (cpi->is_src_frame_alt_ref &&
2502 (best_mode.mbmode.mode != ZEROMV || best_mode.mbmode.ref_frame != ALTREF_FRAME))
2504 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
2505 x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
2506 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2507 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
2508 x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
2509 (cpi->common.mb_no_coeff_skip);
2510 x->e_mbd.mode_info_context->mbmi.partitioning = 0;
2516 vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mode.mbmode, sizeof(MB_MODE_INFO));
2518 if (best_mode.mbmode.mode == B_PRED)
2520 for (i = 0; i < 16; i++)
2521 xd->mode_info_context->bmi[i].as_mode = best_mode.bmodes[i].as_mode;
2524 if (best_mode.mbmode.mode == SPLITMV)
2526 for (i = 0; i < 16; i++)
2527 xd->mode_info_context->bmi[i].mv.as_int = best_mode.bmodes[i].mv.as_int;
2529 vpx_memcpy(x->partition_info, &best_mode.partition, sizeof(PARTITION_INFO));
2531 x->e_mbd.mode_info_context->mbmi.mv.as_int =
2532 x->partition_info->bmi[15].mv.as_int;
2536 != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
2537 best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
2539 rd_update_mvcount(cpi, x, &best_ref_mv);
2542 void vp8_rd_pick_intra_mode(VP8_COMP *cpi, MACROBLOCK *x, int *rate_)
2544 int error4x4, error16x16;
2545 int rate4x4, rate16x16 = 0, rateuv;
2546 int dist4x4, dist16x16, distuv;
2548 int rate4x4_tokenonly = 0;
2549 int rate16x16_tokenonly = 0;
2550 int rateuv_tokenonly = 0;
2552 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
2554 rd_pick_intra_mbuv_mode(cpi, x, &rateuv, &rateuv_tokenonly, &distuv);
2557 error16x16 = rd_pick_intra16x16mby_mode(cpi, x,
2558 &rate16x16, &rate16x16_tokenonly,
2561 error4x4 = rd_pick_intra4x4mby_modes(cpi, x,
2562 &rate4x4, &rate4x4_tokenonly,
2563 &dist4x4, error16x16);
2565 if (error4x4 < error16x16)
2567 x->e_mbd.mode_info_context->mbmi.mode = B_PRED;