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"
19 #include "treewriter.h"
21 #include "modecosts.h"
22 #include "encodeintra.h"
23 #include "pickinter.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"
39 extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x);
41 #define MAXF(a,b) (((a) > (b)) ? (a) : (b))
43 typedef struct rate_distortion_struct
52 typedef struct best_mode_struct
58 union b_mode_info bmodes[16];
59 PARTITION_INFO partition;
62 static const int auto_speed_thresh[17] =
83 const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] =
115 /* This table determines the search order in reference frame priority order,
116 * which may not necessarily match INTRA,LAST,GOLDEN,ARF
118 const int vp8_ref_frame_order[MAX_MODES] =
150 static void fill_token_costs(
151 int c[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
152 const vp8_prob p[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES]
158 for (i = 0; i < BLOCK_TYPES; i++)
159 for (j = 0; j < COEF_BANDS; j++)
160 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
162 /* check for pt=0 and band > 1 if block type 0
163 * and 0 if blocktype 1
165 if (k == 0 && j > (i == 0))
166 vp8_cost_tokens2(c[i][j][k], p [i][j][k], vp8_coef_tree, 2);
168 vp8_cost_tokens(c[i][j][k], p [i][j][k], vp8_coef_tree);
171 static const int rd_iifactor[32] =
173 4, 4, 3, 2, 1, 0, 0, 0,
174 0, 0, 0, 0, 0, 0, 0, 0,
175 0, 0, 0, 0, 0, 0, 0, 0,
176 0, 0, 0, 0, 0, 0, 0, 0
179 /* values are now correlated to quantizer */
180 static const int sad_per_bit16lut[QINDEX_RANGE] =
182 2, 2, 2, 2, 2, 2, 2, 2,
183 2, 2, 2, 2, 2, 2, 2, 2,
184 3, 3, 3, 3, 3, 3, 3, 3,
185 3, 3, 3, 3, 3, 3, 4, 4,
186 4, 4, 4, 4, 4, 4, 4, 4,
187 4, 4, 5, 5, 5, 5, 5, 5,
188 5, 5, 5, 5, 5, 5, 6, 6,
189 6, 6, 6, 6, 6, 6, 6, 6,
190 6, 6, 7, 7, 7, 7, 7, 7,
191 7, 7, 7, 7, 7, 7, 8, 8,
192 8, 8, 8, 8, 8, 8, 8, 8,
193 8, 8, 9, 9, 9, 9, 9, 9,
194 9, 9, 9, 9, 9, 9, 10, 10,
195 10, 10, 10, 10, 10, 10, 11, 11,
196 11, 11, 11, 11, 12, 12, 12, 12,
197 12, 12, 13, 13, 13, 13, 14, 14
199 static const int sad_per_bit4lut[QINDEX_RANGE] =
201 2, 2, 2, 2, 2, 2, 3, 3,
202 3, 3, 3, 3, 3, 3, 3, 3,
203 3, 3, 3, 3, 4, 4, 4, 4,
204 4, 4, 4, 4, 4, 4, 5, 5,
205 5, 5, 5, 5, 6, 6, 6, 6,
206 6, 6, 6, 6, 6, 6, 6, 6,
207 7, 7, 7, 7, 7, 7, 7, 7,
208 7, 7, 7, 7, 7, 8, 8, 8,
209 8, 8, 9, 9, 9, 9, 9, 9,
210 10, 10, 10, 10, 10, 10, 10, 10,
211 11, 11, 11, 11, 11, 11, 11, 11,
212 12, 12, 12, 12, 12, 12, 12, 12,
213 13, 13, 13, 13, 13, 13, 13, 14,
214 14, 14, 14, 14, 15, 15, 15, 15,
215 16, 16, 16, 16, 17, 17, 17, 18,
216 18, 18, 19, 19, 19, 20, 20, 20,
219 void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex)
221 cpi->mb.sadperbit16 = sad_per_bit16lut[QIndex];
222 cpi->mb.sadperbit4 = sad_per_bit4lut[QIndex];
225 void vp8_initialize_rd_consts(VP8_COMP *cpi, MACROBLOCK *x, int Qvalue)
229 double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0;
230 double rdconst = 2.80;
232 vp8_clear_system_state();
234 /* Further tests required to see if optimum is different
235 * for key frames, golden frames and arf frames.
237 cpi->RDMULT = (int)(rdconst * (capped_q * capped_q));
239 /* Extend rate multiplier along side quantizer zbin increases */
240 if (cpi->mb.zbin_over_quant > 0)
245 /* Experimental code using the same basic equation as used for Q above
246 * The units of cpi->mb.zbin_over_quant are 1/128 of Q bin size
248 oq_factor = 1.0 + ((double)0.0015625 * cpi->mb.zbin_over_quant);
249 modq = (int)((double)capped_q * oq_factor);
250 cpi->RDMULT = (int)(rdconst * (modq * modq));
253 if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME))
255 if (cpi->twopass.next_iiratio > 31)
256 cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4;
259 (cpi->RDMULT * rd_iifactor[cpi->twopass.next_iiratio]) >> 4;
262 cpi->mb.errorperbit = (cpi->RDMULT / 110);
263 cpi->mb.errorperbit += (cpi->mb.errorperbit==0);
265 vp8_set_speed_features(cpi);
267 for (i = 0; i < MAX_MODES; i++)
269 x->mode_test_hit_counts[i] = 0;
272 q = (int)pow(Qvalue, 1.25);
277 if (cpi->RDMULT > 1000)
282 for (i = 0; i < MAX_MODES; i++)
284 if (cpi->sf.thresh_mult[i] < INT_MAX)
286 x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100;
290 x->rd_threshes[i] = INT_MAX;
293 cpi->rd_baseline_thresh[i] = x->rd_threshes[i];
300 for (i = 0; i < MAX_MODES; i++)
302 if (cpi->sf.thresh_mult[i] < (INT_MAX / q))
304 x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q;
308 x->rd_threshes[i] = INT_MAX;
311 cpi->rd_baseline_thresh[i] = x->rd_threshes[i];
316 /* build token cost array for the type of frame we have now */
317 FRAME_CONTEXT *l = &cpi->lfc_n;
319 if(cpi->common.refresh_alt_ref_frame)
321 else if(cpi->common.refresh_golden_frame)
326 (const vp8_prob( *)[8][3][11]) l->coef_probs
331 (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs);
335 /* TODO make these mode costs depend on last,alt or gold too. (jbb) */
336 vp8_init_mode_costs(cpi);
341 void vp8_auto_select_speed(VP8_COMP *cpi)
343 int milliseconds_for_compress = (int)(1000000 / cpi->framerate);
345 milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16;
353 f = fopen("speed.stt", "a");
354 fprintf(f, " %8ld %10ld %10ld %10ld\n",
355 cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time);
361 if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress)
363 if (cpi->avg_pick_mode_time == 0)
369 if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95)
372 cpi->avg_pick_mode_time = 0;
373 cpi->avg_encode_time = 0;
381 if (milliseconds_for_compress * 100 > cpi->avg_encode_time * auto_speed_thresh[cpi->Speed])
384 cpi->avg_pick_mode_time = 0;
385 cpi->avg_encode_time = 0;
387 /* In real-time mode, cpi->speed is in [4, 16]. */
403 cpi->avg_pick_mode_time = 0;
404 cpi->avg_encode_time = 0;
408 int vp8_block_error_c(short *coeff, short *dqcoeff)
413 for (i = 0; i < 16; i++)
415 int this_diff = coeff[i] - dqcoeff[i];
416 error += this_diff * this_diff;
422 int vp8_mbblock_error_c(MACROBLOCK *mb, int dc)
427 int berror, error = 0;
429 for (i = 0; i < 16; i++)
432 bd = &mb->e_mbd.block[i];
436 for (j = dc; j < 16; j++)
438 int this_diff = be->coeff[j] - bd->dqcoeff[j];
439 berror += this_diff * this_diff;
448 int vp8_mbuverror_c(MACROBLOCK *mb)
458 for (i = 16; i < 24; i++)
461 bd = &mb->e_mbd.block[i];
463 error += vp8_block_error_c(be->coeff, bd->dqcoeff);
469 int VP8_UVSSE(MACROBLOCK *x)
471 unsigned char *uptr, *vptr;
472 unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src);
473 unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src);
474 int uv_stride = x->block[16].src_stride;
476 unsigned int sse1 = 0;
477 unsigned int sse2 = 0;
478 int mv_row = x->e_mbd.mode_info_context->mbmi.mv.as_mv.row;
479 int mv_col = x->e_mbd.mode_info_context->mbmi.mv.as_mv.col;
481 int pre_stride = x->e_mbd.pre.uv_stride;
496 offset = (mv_row >> 3) * pre_stride + (mv_col >> 3);
497 uptr = x->e_mbd.pre.u_buffer + offset;
498 vptr = x->e_mbd.pre.v_buffer + offset;
500 if ((mv_row | mv_col) & 7)
502 vp8_sub_pixel_variance8x8(uptr, pre_stride,
503 mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2);
504 vp8_sub_pixel_variance8x8(vptr, pre_stride,
505 mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1);
510 vp8_variance8x8(uptr, pre_stride,
511 upred_ptr, uv_stride, &sse2);
512 vp8_variance8x8(vptr, pre_stride,
513 vpred_ptr, uv_stride, &sse1);
520 static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l)
522 int c = !type; /* start at coef 0, unless Y with Y2 */
523 int eob = (int)(*b->eob);
524 int pt ; /* surrounding block/prev coef predictor */
526 short *qcoeff_ptr = b->qcoeff;
528 VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l);
533 const int v = qcoeff_ptr[vp8_default_zig_zag1d[c]];
534 const int t = vp8_dct_value_tokens_ptr[v].Token;
535 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t];
536 cost += vp8_dct_value_cost_ptr[v];
537 pt = vp8_prev_token_class[t];
541 cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN];
543 pt = (c != !type); /* is eob first coefficient; */
549 static int vp8_rdcost_mby(MACROBLOCK *mb)
553 MACROBLOCKD *x = &mb->e_mbd;
554 ENTROPY_CONTEXT_PLANES t_above, t_left;
558 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
559 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
561 ta = (ENTROPY_CONTEXT *)&t_above;
562 tl = (ENTROPY_CONTEXT *)&t_left;
564 for (b = 0; b < 16; b++)
565 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC,
566 ta + vp8_block2above[b], tl + vp8_block2left[b]);
568 cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2,
569 ta + vp8_block2above[24], tl + vp8_block2left[24]);
574 static void macro_block_yrd( MACROBLOCK *mb,
579 MACROBLOCKD *const x = &mb->e_mbd;
580 BLOCK *const mb_y2 = mb->block + 24;
581 BLOCKD *const x_y2 = x->block + 24;
582 short *Y2DCPtr = mb_y2->src_diff;
586 vp8_subtract_mby( mb->src_diff, *(mb->block[0].base_src),
587 mb->block[0].src_stride, mb->e_mbd.predictor, 16);
589 /* Fdct and building the 2nd order block */
590 for (beptr = mb->block; beptr < mb->block + 16; beptr += 2)
592 mb->short_fdct8x4(beptr->src_diff, beptr->coeff, 32);
593 *Y2DCPtr++ = beptr->coeff[0];
594 *Y2DCPtr++ = beptr->coeff[16];
598 mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8);
601 for (b = 0; b < 16; b++)
603 mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]);
606 /* DC predication and Quantization of 2nd Order block */
607 mb->quantize_b(mb_y2, x_y2);
610 d = vp8_mbblock_error(mb, 1) << 2;
611 d += vp8_block_error(mb_y2->coeff, x_y2->dqcoeff);
613 *Distortion = (d >> 4);
616 *Rate = vp8_rdcost_mby(mb);
619 static void copy_predictor(unsigned char *dst, const unsigned char *predictor)
621 const unsigned int *p = (const unsigned int *)predictor;
622 unsigned int *d = (unsigned int *)dst;
628 static int rd_pick_intra4x4block(
632 B_PREDICTION_MODE *best_mode,
633 const int *bmode_costs,
641 B_PREDICTION_MODE mode;
642 int best_rd = INT_MAX;
646 ENTROPY_CONTEXT ta = *a, tempa = *a;
647 ENTROPY_CONTEXT tl = *l, templ = *l;
649 * The predictor buffer is a 2d buffer with a stride of 16. Create
650 * a temp buffer that meets the stride requirements, but we are only
651 * interested in the left 4x4 block
653 DECLARE_ALIGNED_ARRAY(16, unsigned char, best_predictor, 16*4);
654 DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16);
655 int dst_stride = x->e_mbd.dst.y_stride;
656 unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset;
658 unsigned char *Above = dst - dst_stride;
659 unsigned char *yleft = dst - 1;
660 unsigned char top_left = Above[-1];
662 for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++)
667 rate = bmode_costs[mode];
669 vp8_intra4x4_predict(Above, yleft, dst_stride, mode,
670 b->predictor, 16, top_left);
671 vp8_subtract_b(be, b, 16);
672 x->short_fdct4x4(be->src_diff, be->coeff, 32);
673 x->quantize_b(be, b);
678 ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ);
680 distortion = vp8_block_error(be->coeff, b->dqcoeff) >> 2;
682 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
684 if (this_rd < best_rd)
688 *bestdistortion = distortion;
693 copy_predictor(best_predictor, b->predictor);
694 vpx_memcpy(best_dqcoeff, b->dqcoeff, 32);
697 b->bmi.as_mode = *best_mode;
699 vp8_short_idct4x4llm(best_dqcoeff, best_predictor, 16, dst, dst_stride);
704 static int rd_pick_intra4x4mby_modes(MACROBLOCK *mb, int *Rate,
705 int *rate_y, int *Distortion, int best_rd)
707 MACROBLOCKD *const xd = &mb->e_mbd;
709 int cost = mb->mbmode_cost [xd->frame_type] [B_PRED];
712 int64_t total_rd = 0;
713 ENTROPY_CONTEXT_PLANES t_above, t_left;
716 const int *bmode_costs;
718 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
719 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
721 ta = (ENTROPY_CONTEXT *)&t_above;
722 tl = (ENTROPY_CONTEXT *)&t_left;
724 intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16);
726 bmode_costs = mb->inter_bmode_costs;
728 for (i = 0; i < 16; i++)
730 MODE_INFO *const mic = xd->mode_info_context;
731 const int mis = xd->mode_info_stride;
732 B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode);
733 int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d);
735 if (mb->e_mbd.frame_type == KEY_FRAME)
737 const B_PREDICTION_MODE A = above_block_mode(mic, i, mis);
738 const B_PREDICTION_MODE L = left_block_mode(mic, i);
740 bmode_costs = mb->bmode_costs[A][L];
743 total_rd += rd_pick_intra4x4block(
744 mb, mb->block + i, xd->block + i, &best_mode, bmode_costs,
745 ta + vp8_block2above[i],
746 tl + vp8_block2left[i], &r, &ry, &d);
752 mic->bmi[i].as_mode = best_mode;
754 if(total_rd >= (int64_t)best_rd)
758 if(total_rd >= (int64_t)best_rd)
762 *rate_y = tot_rate_y;
763 *Distortion = distortion;
765 return RDCOST(mb->rdmult, mb->rddiv, cost, distortion);
769 static int rd_pick_intra16x16mby_mode(MACROBLOCK *x,
774 MB_PREDICTION_MODE mode;
775 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
778 int best_rd = INT_MAX;
780 MACROBLOCKD *xd = &x->e_mbd;
782 /* Y Search for 16x16 intra prediction mode */
783 for (mode = DC_PRED; mode <= TM_PRED; mode++)
785 xd->mode_info_context->mbmi.mode = mode;
787 vp8_build_intra_predictors_mby_s(xd,
788 xd->dst.y_buffer - xd->dst.y_stride,
789 xd->dst.y_buffer - 1,
794 macro_block_yrd(x, &ratey, &distortion);
795 rate = ratey + x->mbmode_cost[xd->frame_type]
796 [xd->mode_info_context->mbmi.mode];
798 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
800 if (this_rd < best_rd)
802 mode_selected = mode;
806 *Distortion = distortion;
810 xd->mode_info_context->mbmi.mode = mode_selected;
814 static int rd_cost_mbuv(MACROBLOCK *mb)
818 MACROBLOCKD *x = &mb->e_mbd;
819 ENTROPY_CONTEXT_PLANES t_above, t_left;
823 vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
824 vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
826 ta = (ENTROPY_CONTEXT *)&t_above;
827 tl = (ENTROPY_CONTEXT *)&t_left;
829 for (b = 16; b < 24; b++)
830 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_UV,
831 ta + vp8_block2above[b], tl + vp8_block2left[b]);
837 static int rd_inter16x16_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
838 int *distortion, int fullpixel)
840 vp8_build_inter16x16_predictors_mbuv(&x->e_mbd);
841 vp8_subtract_mbuv(x->src_diff,
842 x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
843 &x->e_mbd.predictor[256], &x->e_mbd.predictor[320], 8);
845 vp8_transform_mbuv(x);
846 vp8_quantize_mbuv(x);
848 *rate = rd_cost_mbuv(x);
849 *distortion = vp8_mbuverror(x) / 4;
851 return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
854 static int rd_inter4x4_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate,
855 int *distortion, int fullpixel)
857 vp8_build_inter4x4_predictors_mbuv(&x->e_mbd);
858 vp8_subtract_mbuv(x->src_diff,
859 x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
860 &x->e_mbd.predictor[256], &x->e_mbd.predictor[320], 8);
862 vp8_transform_mbuv(x);
863 vp8_quantize_mbuv(x);
865 *rate = rd_cost_mbuv(x);
866 *distortion = vp8_mbuverror(x) / 4;
868 return RDCOST(x->rdmult, x->rddiv, *rate, *distortion);
871 static void rd_pick_intra_mbuv_mode(MACROBLOCK *x, int *rate,
872 int *rate_tokenonly, int *distortion)
874 MB_PREDICTION_MODE mode;
875 MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected);
876 int best_rd = INT_MAX;
877 int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r);
879 MACROBLOCKD *xd = &x->e_mbd;
881 for (mode = DC_PRED; mode <= TM_PRED; mode++)
887 xd->mode_info_context->mbmi.uv_mode = mode;
889 vp8_build_intra_predictors_mbuv_s(xd,
890 xd->dst.u_buffer - xd->dst.uv_stride,
891 xd->dst.v_buffer - xd->dst.uv_stride,
892 xd->dst.u_buffer - 1,
893 xd->dst.v_buffer - 1,
895 &xd->predictor[256], &xd->predictor[320],
899 vp8_subtract_mbuv(x->src_diff,
900 x->src.u_buffer, x->src.v_buffer, x->src.uv_stride,
901 &xd->predictor[256], &xd->predictor[320], 8);
902 vp8_transform_mbuv(x);
903 vp8_quantize_mbuv(x);
905 rate_to = rd_cost_mbuv(x);
906 this_rate = rate_to + x->intra_uv_mode_cost[xd->frame_type][xd->mode_info_context->mbmi.uv_mode];
908 this_distortion = vp8_mbuverror(x) / 4;
910 this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion);
912 if (this_rd < best_rd)
917 *rate_tokenonly = rate_to;
918 mode_selected = mode;
925 xd->mode_info_context->mbmi.uv_mode = mode_selected;
928 int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4])
930 vp8_prob p [VP8_MVREFS-1];
931 assert(NEARESTMV <= m && m <= SPLITMV);
932 vp8_mv_ref_probs(p, near_mv_ref_ct);
933 return vp8_cost_token(vp8_mv_ref_tree, p,
934 vp8_mv_ref_encoding_array + (m - NEARESTMV));
937 void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv)
939 x->e_mbd.mode_info_context->mbmi.mode = mb;
940 x->e_mbd.mode_info_context->mbmi.mv.as_int = mv->as_int;
943 static int labels2mode(
945 int const *labelings, int which_label,
946 B_PREDICTION_MODE this_mode,
947 int_mv *this_mv, int_mv *best_ref_mv,
951 MACROBLOCKD *const xd = & x->e_mbd;
952 MODE_INFO *const mic = xd->mode_info_context;
953 const int mis = xd->mode_info_stride;
958 /* We have to be careful retrieving previously-encoded motion vectors.
959 Ones from this macroblock have to be pulled from the BLOCKD array
960 as they have not yet made it to the bmi array in our MB_MODE_INFO. */
966 BLOCKD *const d = xd->block + i;
967 const int row = i >> 2, col = i & 3;
971 if (labelings[i] != which_label)
974 if (col && labelings[i] == labelings[i-1])
976 else if (row && labelings[i] == labelings[i-4])
980 /* the only time we should do costing for new motion vector
981 * or mode is when we are on a new label (jbb May 08, 2007)
983 switch (m = this_mode)
986 thismvcost = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102);
989 this_mv->as_int = col ? d[-1].bmi.mv.as_int : left_block_mv(mic, i);
992 this_mv->as_int = row ? d[-4].bmi.mv.as_int : above_block_mv(mic, i, mis);
1001 if (m == ABOVE4X4) /* replace above with left if same */
1005 left_mv.as_int = col ? d[-1].bmi.mv.as_int :
1006 left_block_mv(mic, i);
1008 if (left_mv.as_int == this_mv->as_int)
1012 cost = x->inter_bmode_costs[ m];
1015 d->bmi.mv.as_int = this_mv->as_int;
1017 x->partition_info->bmi[i].mode = m;
1018 x->partition_info->bmi[i].mv.as_int = this_mv->as_int;
1023 cost += thismvcost ;
1027 static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels,
1028 int which_label, ENTROPY_CONTEXT *ta,
1029 ENTROPY_CONTEXT *tl)
1033 MACROBLOCKD *x = &mb->e_mbd;
1035 for (b = 0; b < 16; b++)
1036 if (labels[ b] == which_label)
1037 cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_WITH_DC,
1038 ta + vp8_block2above[b],
1039 tl + vp8_block2left[b]);
1044 static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label)
1047 unsigned int distortion = 0;
1048 int pre_stride = x->e_mbd.pre.y_stride;
1049 unsigned char *base_pre = x->e_mbd.pre.y_buffer;
1052 for (i = 0; i < 16; i++)
1054 if (labels[i] == which_label)
1056 BLOCKD *bd = &x->e_mbd.block[i];
1057 BLOCK *be = &x->block[i];
1059 vp8_build_inter_predictors_b(bd, 16, base_pre, pre_stride, x->e_mbd.subpixel_predict);
1060 vp8_subtract_b(be, bd, 16);
1061 x->short_fdct4x4(be->src_diff, be->coeff, 32);
1062 x->quantize_b(be, bd);
1064 distortion += vp8_block_error(be->coeff, bd->dqcoeff);
1072 static const unsigned int segmentation_to_sseshift[4] = {3, 3, 2, 0};
1085 B_PREDICTION_MODE modes[16];
1087 unsigned char eobs[16];
1092 int_mv sv_mvp[4]; /* save 4 mvp from 8x8 */
1093 int sv_istep[2]; /* save 2 initial step_param for 16x8/8x16 */
1098 static void rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x,
1099 BEST_SEG_INFO *bsi, unsigned int segmentation)
1105 B_PREDICTION_MODE this_mode;
1109 int this_segment_rd = 0;
1110 int label_mv_thresh;
1114 int segmentyrate = 0;
1116 vp8_variance_fn_ptr_t *v_fn_ptr;
1118 ENTROPY_CONTEXT_PLANES t_above, t_left;
1119 ENTROPY_CONTEXT *ta;
1120 ENTROPY_CONTEXT *tl;
1121 ENTROPY_CONTEXT_PLANES t_above_b, t_left_b;
1122 ENTROPY_CONTEXT *ta_b;
1123 ENTROPY_CONTEXT *tl_b;
1125 vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES));
1126 vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES));
1128 ta = (ENTROPY_CONTEXT *)&t_above;
1129 tl = (ENTROPY_CONTEXT *)&t_left;
1130 ta_b = (ENTROPY_CONTEXT *)&t_above_b;
1131 tl_b = (ENTROPY_CONTEXT *)&t_left_b;
1136 v_fn_ptr = &cpi->fn_ptr[segmentation];
1137 labels = vp8_mbsplits[segmentation];
1138 label_count = vp8_mbsplit_count[segmentation];
1140 /* 64 makes this threshold really big effectively making it so that we
1141 * very rarely check mvs on segments. setting this to 1 would make mv
1142 * thresh roughly equal to what it is for macroblocks
1144 label_mv_thresh = 1 * bsi->mvthresh / label_count ;
1146 /* Segmentation method overheads */
1147 rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation);
1148 rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts);
1149 this_segment_rd += RDCOST(x->rdmult, x->rddiv, rate, 0);
1152 for (i = 0; i < label_count; i++)
1154 int_mv mode_mv[B_MODE_COUNT];
1155 int best_label_rd = INT_MAX;
1156 B_PREDICTION_MODE mode_selected = ZERO4X4;
1157 int bestlabelyrate = 0;
1159 /* search for the best motion vector on this segment */
1160 for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++)
1165 ENTROPY_CONTEXT_PLANES t_above_s, t_left_s;
1166 ENTROPY_CONTEXT *ta_s;
1167 ENTROPY_CONTEXT *tl_s;
1169 vpx_memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES));
1170 vpx_memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES));
1172 ta_s = (ENTROPY_CONTEXT *)&t_above_s;
1173 tl_s = (ENTROPY_CONTEXT *)&t_left_s;
1175 if (this_mode == NEW4X4)
1183 int bestsme = INT_MAX;
1188 /* Is the best so far sufficiently good that we cant justify
1189 * doing a new motion search.
1191 if (best_label_rd < label_mv_thresh)
1194 if(cpi->compressor_speed)
1196 if (segmentation == BLOCK_8X16 || segmentation == BLOCK_16X8)
1198 bsi->mvp.as_int = bsi->sv_mvp[i].as_int;
1199 if (i==1 && segmentation == BLOCK_16X8)
1200 bsi->mvp.as_int = bsi->sv_mvp[2].as_int;
1202 step_param = bsi->sv_istep[i];
1205 /* use previous block's result as next block's MV
1208 if (segmentation == BLOCK_4X4 && i>0)
1210 bsi->mvp.as_int = x->e_mbd.block[i-1].bmi.mv.as_int;
1211 if (i==4 || i==8 || i==12)
1212 bsi->mvp.as_int = x->e_mbd.block[i-4].bmi.mv.as_int;
1217 further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
1220 int sadpb = x->sadperbit4;
1223 mvp_full.as_mv.row = bsi->mvp.as_mv.row >>3;
1224 mvp_full.as_mv.col = bsi->mvp.as_mv.col >>3;
1226 /* find first label */
1227 n = vp8_mbsplit_offset[segmentation][i];
1230 e = &x->e_mbd.block[n];
1233 bestsme = cpi->diamond_search_sad(x, c, e, &mvp_full,
1234 &mode_mv[NEW4X4], step_param,
1235 sadpb, &num00, v_fn_ptr,
1236 x->mvcost, bsi->ref_mv);
1241 while (n < further_steps)
1249 thissme = cpi->diamond_search_sad(x, c, e,
1250 &mvp_full, &temp_mv,
1251 step_param + n, sadpb,
1253 x->mvcost, bsi->ref_mv);
1255 if (thissme < bestsme)
1258 mode_mv[NEW4X4].as_int = temp_mv.as_int;
1264 sseshift = segmentation_to_sseshift[segmentation];
1266 /* Should we do a full search (best quality only) */
1267 if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000)
1269 /* Check if mvp_full is within the range. */
1270 vp8_clamp_mv(&mvp_full, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max);
1272 thissme = cpi->full_search_sad(x, c, e, &mvp_full,
1273 sadpb, 16, v_fn_ptr,
1274 x->mvcost, bsi->ref_mv);
1276 if (thissme < bestsme)
1279 mode_mv[NEW4X4].as_int = e->bmi.mv.as_int;
1283 /* The full search result is actually worse so
1284 * 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,
1301 rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode],
1302 bsi->ref_mv, x->mvcost);
1304 /* Trap vectors that reach beyond the UMV borders */
1305 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) ||
1306 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
1311 distortion = vp8_encode_inter_mb_segment(x, labels, i) / 4;
1313 labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s);
1316 this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion);
1318 if (this_rd < best_label_rd)
1322 bestlabelyrate = labelyrate;
1323 mode_selected = this_mode;
1324 best_label_rd = this_rd;
1326 vpx_memcpy(ta_b, ta_s, sizeof(ENTROPY_CONTEXT_PLANES));
1327 vpx_memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES));
1330 } /*for each 4x4 mode*/
1332 vpx_memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES));
1333 vpx_memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES));
1335 labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected],
1336 bsi->ref_mv, x->mvcost);
1340 segmentyrate += bestlabelyrate;
1341 this_segment_rd += best_label_rd;
1343 if (this_segment_rd >= bsi->segment_rd)
1346 } /* for each label */
1348 if (this_segment_rd < bsi->segment_rd)
1352 bsi->segment_yrate = segmentyrate;
1353 bsi->segment_rd = this_segment_rd;
1354 bsi->segment_num = segmentation;
1356 /* store everything needed to come back to this!! */
1357 for (i = 0; i < 16; i++)
1359 bsi->mvs[i].as_mv = x->partition_info->bmi[i].mv.as_mv;
1360 bsi->modes[i] = x->partition_info->bmi[i].mode;
1361 bsi->eobs[i] = x->e_mbd.eobs[i];
1367 void vp8_cal_step_param(int sr, int *sp)
1371 if (sr > MAX_FIRST_STEP) sr = MAX_FIRST_STEP;
1372 else if (sr < 1) sr = 1;
1377 *sp = MAX_MVSEARCH_STEPS - 1 - step;
1380 static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x,
1381 int_mv *best_ref_mv, int best_rd,
1382 int *mdcounts, int *returntotrate,
1383 int *returnyrate, int *returndistortion,
1389 vpx_memset(&bsi, 0, sizeof(bsi));
1391 bsi.segment_rd = best_rd;
1392 bsi.ref_mv = best_ref_mv;
1393 bsi.mvp.as_int = best_ref_mv->as_int;
1394 bsi.mvthresh = mvthresh;
1395 bsi.mdcounts = mdcounts;
1397 for(i = 0; i < 16; i++)
1399 bsi.modes[i] = ZERO4X4;
1402 if(cpi->compressor_speed == 0)
1404 /* for now, we will keep the original segmentation order
1405 when in best quality mode */
1406 rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1407 rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1408 rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1409 rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1415 rd_check_segment(cpi, x, &bsi, BLOCK_8X8);
1417 if (bsi.segment_rd < best_rd)
1419 int col_min = ((best_ref_mv->as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
1420 int row_min = ((best_ref_mv->as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
1421 int col_max = (best_ref_mv->as_mv.col>>3) + MAX_FULL_PEL_VAL;
1422 int row_max = (best_ref_mv->as_mv.row>>3) + MAX_FULL_PEL_VAL;
1424 int tmp_col_min = x->mv_col_min;
1425 int tmp_col_max = x->mv_col_max;
1426 int tmp_row_min = x->mv_row_min;
1427 int tmp_row_max = x->mv_row_max;
1429 /* Get intersection of UMV window and valid MV window to reduce # of checks in diamond search. */
1430 if (x->mv_col_min < col_min )
1431 x->mv_col_min = col_min;
1432 if (x->mv_col_max > col_max )
1433 x->mv_col_max = col_max;
1434 if (x->mv_row_min < row_min )
1435 x->mv_row_min = row_min;
1436 if (x->mv_row_max > row_max )
1437 x->mv_row_max = row_max;
1439 /* Get 8x8 result */
1440 bsi.sv_mvp[0].as_int = bsi.mvs[0].as_int;
1441 bsi.sv_mvp[1].as_int = bsi.mvs[2].as_int;
1442 bsi.sv_mvp[2].as_int = bsi.mvs[8].as_int;
1443 bsi.sv_mvp[3].as_int = bsi.mvs[10].as_int;
1445 /* Use 8x8 result as 16x8/8x16's predictor MV. Adjust search range according to the closeness of 2 MV. */
1448 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);
1449 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1451 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);
1452 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1454 rd_check_segment(cpi, x, &bsi, BLOCK_8X16);
1459 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);
1460 vp8_cal_step_param(sr, &bsi.sv_istep[0]);
1462 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);
1463 vp8_cal_step_param(sr, &bsi.sv_istep[1]);
1465 rd_check_segment(cpi, x, &bsi, BLOCK_16X8);
1468 /* If 8x8 is better than 16x8/8x16, then do 4x4 search */
1469 /* Not skip 4x4 if speed=0 (good quality) */
1470 if (cpi->sf.no_skip_block4x4_search || bsi.segment_num == BLOCK_8X8) /* || (sv_segment_rd8x8-bsi.segment_rd) < sv_segment_rd8x8>>5) */
1472 bsi.mvp.as_int = bsi.sv_mvp[0].as_int;
1473 rd_check_segment(cpi, x, &bsi, BLOCK_4X4);
1476 /* restore UMV window */
1477 x->mv_col_min = tmp_col_min;
1478 x->mv_col_max = tmp_col_max;
1479 x->mv_row_min = tmp_row_min;
1480 x->mv_row_max = tmp_row_max;
1484 /* set it to the best */
1485 for (i = 0; i < 16; i++)
1487 BLOCKD *bd = &x->e_mbd.block[i];
1489 bd->bmi.mv.as_int = bsi.mvs[i].as_int;
1490 *bd->eob = bsi.eobs[i];
1493 *returntotrate = bsi.r;
1494 *returndistortion = bsi.d;
1495 *returnyrate = bsi.segment_yrate;
1497 /* save partitions */
1498 x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num;
1499 x->partition_info->count = vp8_mbsplit_count[bsi.segment_num];
1501 for (i = 0; i < x->partition_info->count; i++)
1505 j = vp8_mbsplit_offset[bsi.segment_num][i];
1507 x->partition_info->bmi[i].mode = bsi.modes[j];
1508 x->partition_info->bmi[i].mv.as_mv = bsi.mvs[j].as_mv;
1511 * used to set x->e_mbd.mode_info_context->mbmi.mv.as_int
1513 x->partition_info->bmi[15].mv.as_int = bsi.mvs[15].as_int;
1515 return bsi.segment_rd;
1518 /* The improved MV prediction */
1523 const MODE_INFO *here,
1526 int *ref_frame_sign_bias,
1531 const MODE_INFO *above = here - xd->mode_info_stride;
1532 const MODE_INFO *left = here - 1;
1533 const MODE_INFO *aboveleft = above - 1;
1547 if(here->mbmi.ref_frame != INTRA_FRAME)
1549 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;
1550 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;
1552 /* read in 3 nearby block's MVs from current frame as prediction
1555 if (above->mbmi.ref_frame != INTRA_FRAME)
1557 near_mvs[vcnt].as_int = above->mbmi.mv.as_int;
1558 mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1559 near_ref[vcnt] = above->mbmi.ref_frame;
1562 if (left->mbmi.ref_frame != INTRA_FRAME)
1564 near_mvs[vcnt].as_int = left->mbmi.mv.as_int;
1565 mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1566 near_ref[vcnt] = left->mbmi.ref_frame;
1569 if (aboveleft->mbmi.ref_frame != INTRA_FRAME)
1571 near_mvs[vcnt].as_int = aboveleft->mbmi.mv.as_int;
1572 mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1573 near_ref[vcnt] = aboveleft->mbmi.ref_frame;
1577 /* read in 5 nearby block's MVs from last frame. */
1578 if(cpi->common.last_frame_type != KEY_FRAME)
1580 mb_offset = (-xd->mb_to_top_edge/128 + 1) * (xd->mode_info_stride +1) + (-xd->mb_to_left_edge/128 +1) ;
1582 /* current in last frame */
1583 if (cpi->lf_ref_frame[mb_offset] != INTRA_FRAME)
1585 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset].as_int;
1586 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1587 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset];
1591 /* above in last frame */
1592 if (cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1] != INTRA_FRAME)
1594 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset - xd->mode_info_stride-1].as_int;
1595 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset - xd->mode_info_stride-1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1596 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1];
1600 /* left in last frame */
1601 if (cpi->lf_ref_frame[mb_offset-1] != INTRA_FRAME)
1603 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset -1].as_int;
1604 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset -1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1605 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - 1];
1609 /* right in last frame */
1610 if (cpi->lf_ref_frame[mb_offset +1] != INTRA_FRAME)
1612 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset +1].as_int;
1613 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1614 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset +1];
1618 /* below in last frame */
1619 if (cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1] != INTRA_FRAME)
1621 near_mvs[vcnt].as_int = cpi->lfmv[mb_offset + xd->mode_info_stride +1].as_int;
1622 mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset + xd->mode_info_stride +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias);
1623 near_ref[vcnt] = cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1];
1628 for(i=0; i< vcnt; i++)
1630 if(near_ref[near_sadidx[i]] != INTRA_FRAME)
1632 if(here->mbmi.ref_frame == near_ref[near_sadidx[i]])
1634 mv.as_int = near_mvs[near_sadidx[i]].as_int;
1647 for(i=0; i<vcnt; i++)
1649 mvx[i] = near_mvs[i].as_mv.row;
1650 mvy[i] = near_mvs[i].as_mv.col;
1653 insertsortmv(mvx, vcnt);
1654 insertsortmv(mvy, vcnt);
1655 mv.as_mv.row = mvx[vcnt/2];
1656 mv.as_mv.col = mvy[vcnt/2];
1659 /* sr is set to 0 to allow calling function to decide the search
1666 /* Set up return values */
1667 mvp->as_int = mv.as_int;
1668 vp8_clamp_mv2(mvp, xd);
1671 void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, int recon_yoffset, int near_sadidx[])
1673 /* near_sad indexes:
1674 * 0-cf above, 1-cf left, 2-cf aboveleft,
1675 * 3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below
1677 int near_sad[8] = {0};
1678 BLOCK *b = &x->block[0];
1679 unsigned char *src_y_ptr = *(b->base_src);
1681 /* calculate sad for current frame 3 nearby MBs. */
1682 if( xd->mb_to_top_edge==0 && xd->mb_to_left_edge ==0)
1684 near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX;
1685 }else if(xd->mb_to_top_edge==0)
1686 { /* only has left MB for sad calculation. */
1687 near_sad[0] = near_sad[2] = INT_MAX;
1688 near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, UINT_MAX);
1689 }else if(xd->mb_to_left_edge ==0)
1690 { /* only has left MB for sad calculation. */
1691 near_sad[1] = near_sad[2] = INT_MAX;
1692 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, UINT_MAX);
1695 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, UINT_MAX);
1696 near_sad[1] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, xd->dst.y_buffer - 16,xd->dst.y_stride, UINT_MAX);
1697 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, UINT_MAX);
1700 if(cpi->common.last_frame_type != KEY_FRAME)
1702 /* calculate sad for last frame 5 nearby MBs. */
1703 unsigned char *pre_y_buffer = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset;
1704 int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride;
1706 if(xd->mb_to_top_edge==0) near_sad[4] = INT_MAX;
1707 if(xd->mb_to_left_edge ==0) near_sad[5] = INT_MAX;
1708 if(xd->mb_to_right_edge ==0) near_sad[6] = INT_MAX;
1709 if(xd->mb_to_bottom_edge==0) near_sad[7] = INT_MAX;
1711 if(near_sad[4] != INT_MAX)
1712 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, UINT_MAX);
1713 if(near_sad[5] != INT_MAX)
1714 near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer - 16, pre_y_stride, UINT_MAX);
1715 near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer, pre_y_stride, UINT_MAX);
1716 if(near_sad[6] != INT_MAX)
1717 near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer + 16, pre_y_stride, UINT_MAX);
1718 if(near_sad[7] != INT_MAX)
1719 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, UINT_MAX);
1722 if(cpi->common.last_frame_type != KEY_FRAME)
1724 insertsortsad(near_sad, near_sadidx, 8);
1727 insertsortsad(near_sad, near_sadidx, 3);
1731 static void rd_update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv)
1733 if (x->e_mbd.mode_info_context->mbmi.mode == SPLITMV)
1737 for (i = 0; i < x->partition_info->count; i++)
1739 if (x->partition_info->bmi[i].mode == NEW4X4)
1741 x->MVcount[0][mv_max+((x->partition_info->bmi[i].mv.as_mv.row
1742 - best_ref_mv->as_mv.row) >> 1)]++;
1743 x->MVcount[1][mv_max+((x->partition_info->bmi[i].mv.as_mv.col
1744 - best_ref_mv->as_mv.col) >> 1)]++;
1748 else if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV)
1750 x->MVcount[0][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.row
1751 - best_ref_mv->as_mv.row) >> 1)]++;
1752 x->MVcount[1][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.col
1753 - best_ref_mv->as_mv.col) >> 1)]++;
1757 static int evaluate_inter_mode_rd(int mdcounts[4],
1758 RATE_DISTORTION* rd,
1760 VP8_COMP *cpi, MACROBLOCK *x)
1762 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
1763 BLOCK *b = &x->block[0];
1764 MACROBLOCKD *xd = &x->e_mbd;
1766 vp8_build_inter16x16_predictors_mby(&x->e_mbd, x->e_mbd.predictor, 16);
1768 if (cpi->active_map_enabled && x->active_ptr[0] == 0) {
1771 else if (x->encode_breakout)
1775 unsigned int threshold = (xd->block[0].dequant[1]
1776 * xd->block[0].dequant[1] >>4);
1778 if(threshold < x->encode_breakout)
1779 threshold = x->encode_breakout;
1781 var = vp8_variance16x16
1782 (*(b->base_src), b->src_stride,
1783 x->e_mbd.predictor, 16, &sse);
1785 if (sse < threshold)
1787 unsigned int q2dc = xd->block[24].dequant[0];
1788 /* If theres is no codeable 2nd order dc
1789 or a very small uniform pixel change change */
1790 if ((sse - var < q2dc * q2dc >>4) ||
1791 (sse /2 > var && sse-var < 64))
1793 /* Check u and v to make sure skip is ok */
1794 unsigned int sse2 = VP8_UVSSE(x);
1795 if (sse2 * 2 < threshold)
1798 rd->distortion2 = sse + sse2;
1801 /* for best_yrd calculation */
1803 rd->distortion_uv = sse2;
1806 return RDCOST(x->rdmult, x->rddiv, rd->rate2,
1814 /* Add in the Mv/mode cost */
1815 rd->rate2 += vp8_cost_mv_ref(this_mode, mdcounts);
1817 /* Y cost and distortion */
1818 macro_block_yrd(x, &rd->rate_y, &distortion);
1819 rd->rate2 += rd->rate_y;
1820 rd->distortion2 += distortion;
1822 /* UV cost and distortion */
1823 rd_inter16x16_uv(cpi, x, &rd->rate_uv, &rd->distortion_uv,
1824 cpi->common.full_pixel);
1825 rd->rate2 += rd->rate_uv;
1826 rd->distortion2 += rd->distortion_uv;
1830 static int calculate_final_rd_costs(int this_rd,
1831 RATE_DISTORTION* rd,
1835 int intra_rd_penalty,
1836 VP8_COMP *cpi, MACROBLOCK *x)
1838 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
1840 /* Where skip is allowable add in the default per mb cost for the no
1841 * skip case. where we then decide to skip we have to delete this and
1842 * replace it with the cost of signalling a skip
1844 if (cpi->common.mb_no_coeff_skip)
1846 *other_cost += vp8_cost_bit(cpi->prob_skip_false, 0);
1847 rd->rate2 += *other_cost;
1850 /* Estimate the reference frame signaling cost and add it
1851 * to the rolling cost variable.
1854 x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
1858 /* Test for the condition where skip block will be activated
1859 * because there are no non zero coefficients and make any
1860 * necessary adjustment for rate
1862 if (cpi->common.mb_no_coeff_skip)
1866 int has_y2_block = (this_mode!=SPLITMV && this_mode!=B_PRED);
1870 tteob += x->e_mbd.eobs[24];
1872 for (i = 0; i < 16; i++)
1873 tteob += (x->e_mbd.eobs[i] > has_y2_block);
1875 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
1877 for (i = 16; i < 24; i++)
1878 tteob += x->e_mbd.eobs[i];
1881 tteob += uv_intra_tteob;
1885 rd->rate2 -= (rd->rate_y + rd->rate_uv);
1886 /* for best_yrd calculation */
1889 /* Back out no skip flag costing and add in skip flag costing */
1890 if (cpi->prob_skip_false)
1894 prob_skip_cost = vp8_cost_bit(cpi->prob_skip_false, 1);
1895 prob_skip_cost -= vp8_cost_bit(cpi->prob_skip_false, 0);
1896 rd->rate2 += prob_skip_cost;
1897 *other_cost += prob_skip_cost;
1901 /* Calculate the final RD estimate for this mode */
1902 this_rd = RDCOST(x->rdmult, x->rddiv, rd->rate2, rd->distortion2);
1903 if (this_rd < INT_MAX && x->e_mbd.mode_info_context->mbmi.ref_frame
1905 this_rd += intra_rd_penalty;
1910 static void update_best_mode(BEST_MODE* best_mode, int this_rd,
1911 RATE_DISTORTION* rd, int other_cost, MACROBLOCK *x)
1913 MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode;
1916 x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame];
1918 /* Calculate the final y RD estimate for this mode */
1919 best_mode->yrd = RDCOST(x->rdmult, x->rddiv, (rd->rate2-rd->rate_uv-other_cost),
1920 (rd->distortion2-rd->distortion_uv));
1922 best_mode->rd = this_rd;
1923 vpx_memcpy(&best_mode->mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO));
1924 vpx_memcpy(&best_mode->partition, x->partition_info, sizeof(PARTITION_INFO));
1926 if ((this_mode == B_PRED) || (this_mode == SPLITMV))
1929 for (i = 0; i < 16; i++)
1931 best_mode->bmodes[i] = x->e_mbd.block[i].bmi;
1936 void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset,
1937 int recon_uvoffset, int *returnrate,
1938 int *returndistortion, int *returnintra,
1939 int mb_row, int mb_col)
1941 BLOCK *b = &x->block[0];
1942 BLOCKD *d = &x->e_mbd.block[0];
1943 MACROBLOCKD *xd = &x->e_mbd;
1944 int_mv best_ref_mv_sb[2];
1945 int_mv mode_mv_sb[2][MB_MODE_COUNT];
1948 MB_PREDICTION_MODE this_mode;
1950 int best_mode_index = 0;
1951 BEST_MODE best_mode;
1958 int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly;
1959 int uv_intra_tteob = 0;
1960 int uv_intra_done = 0;
1962 MB_PREDICTION_MODE uv_intra_mode = 0;
1964 int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7};
1966 /* search range got from mv_pred(). It uses step_param levels. (0-7) */
1969 unsigned char *plane[4][3];
1970 int ref_frame_map[4];
1973 int intra_rd_penalty = 10* vp8_dc_quant(cpi->common.base_qindex,
1974 cpi->common.y1dc_delta_q);
1976 #if CONFIG_TEMPORAL_DENOISING
1977 unsigned int zero_mv_sse = UINT_MAX, best_sse = UINT_MAX,
1978 best_rd_sse = UINT_MAX;
1981 mode_mv = mode_mv_sb[sign_bias];
1982 best_ref_mv.as_int = 0;
1983 best_mode.rd = INT_MAX;
1984 best_mode.yrd = INT_MAX;
1985 best_mode.intra_rd = INT_MAX;
1986 vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb));
1987 vpx_memset(&best_mode.mbmode, 0, sizeof(best_mode.mbmode));
1988 vpx_memset(&best_mode.bmodes, 0, sizeof(best_mode.bmodes));
1990 /* Setup search priorities */
1991 get_reference_search_order(cpi, ref_frame_map);
1993 /* Check to see if there is at least 1 valid reference frame that we need
1994 * to calculate near_mvs.
1996 if (ref_frame_map[1] > 0)
1998 sign_bias = vp8_find_near_mvs_bias(&x->e_mbd,
1999 x->e_mbd.mode_info_context,
2004 cpi->common.ref_frame_sign_bias);
2006 mode_mv = mode_mv_sb[sign_bias];
2007 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
2010 get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset);
2012 *returnintra = INT_MAX;
2013 /* Count of the number of MBs tested so far this frame */
2014 x->mbs_tested_so_far++;
2018 for (mode_index = 0; mode_index < MAX_MODES; mode_index++)
2020 int this_rd = INT_MAX;
2021 int disable_skip = 0;
2023 int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]];
2025 /* Test best rd so far against threshold for trying this mode. */
2026 if (best_mode.rd <= x->rd_threshes[mode_index])
2029 if (this_ref_frame < 0)
2032 /* These variables hold are rolling total cost and distortion for
2038 this_mode = vp8_mode_order[mode_index];
2040 x->e_mbd.mode_info_context->mbmi.mode = this_mode;
2041 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
2043 /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame,
2044 * unless ARNR filtering is enabled in which case we want
2045 * an unfiltered alternative
2047 if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0))
2049 if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME)
2053 /* everything but intra */
2054 if (x->e_mbd.mode_info_context->mbmi.ref_frame)
2056 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
2057 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
2058 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
2060 if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame])
2062 sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame];
2063 mode_mv = mode_mv_sb[sign_bias];
2064 best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int;
2068 /* Check to see if the testing frequency for this mode is at its
2069 * max If so then prevent it from being tested and increase the
2070 * threshold for its testing
2072 if (x->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1))
2074 if (x->mbs_tested_so_far <= cpi->mode_check_freq[mode_index] * x->mode_test_hit_counts[mode_index])
2076 /* Increase the threshold for coding this mode to make it
2077 * less likely to be chosen
2079 x->rd_thresh_mult[mode_index] += 4;
2081 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2082 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2084 x->rd_threshes[mode_index] =
2085 (cpi->rd_baseline_thresh[mode_index] >> 7) *
2086 x->rd_thresh_mult[mode_index];
2092 /* We have now reached the point where we are going to test the
2093 * current mode so increment the counter for the number of times
2094 * it has been tested
2096 x->mode_test_hit_counts[mode_index] ++;
2098 /* Experimental code. Special case for gf and arf zeromv modes.
2099 * Increase zbin size to supress noise
2101 if (x->zbin_mode_boost_enabled)
2103 if ( this_ref_frame == INTRA_FRAME )
2104 x->zbin_mode_boost = 0;
2107 if (vp8_mode_order[mode_index] == ZEROMV)
2109 if (this_ref_frame != LAST_FRAME)
2110 x->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST;
2112 x->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST;
2114 else if (vp8_mode_order[mode_index] == SPLITMV)
2115 x->zbin_mode_boost = 0;
2117 x->zbin_mode_boost = MV_ZBIN_BOOST;
2120 vp8_update_zbin_extra(cpi, x);
2123 if(!uv_intra_done && this_ref_frame == INTRA_FRAME)
2125 rd_pick_intra_mbuv_mode(x, &uv_intra_rate,
2126 &uv_intra_rate_tokenonly,
2127 &uv_intra_distortion);
2128 uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode;
2131 * Total of the eobs is used later to further adjust rate2. Since uv
2132 * block's intra eobs will be overwritten when we check inter modes,
2133 * we need to save uv_intra_tteob here.
2135 for (i = 16; i < 24; i++)
2136 uv_intra_tteob += x->e_mbd.eobs[i];
2147 /* Note the rate value returned here includes the cost of
2148 * coding the BPRED mode: x->mbmode_cost[x->e_mbd.frame_type][BPRED]
2151 tmp_rd = rd_pick_intra4x4mby_modes(x, &rate, &rd.rate_y, &distortion, best_mode.yrd);
2153 rd.distortion2 += distortion;
2155 if(tmp_rd < best_mode.yrd)
2157 rd.rate2 += uv_intra_rate;
2158 rd.rate_uv = uv_intra_rate_tokenonly;
2159 rd.distortion2 += uv_intra_distortion;
2160 rd.distortion_uv = uv_intra_distortion;
2176 this_rd_thresh = (vp8_ref_frame_order[mode_index] == 1) ?
2177 x->rd_threshes[THR_NEW1] : x->rd_threshes[THR_NEW3];
2178 this_rd_thresh = (vp8_ref_frame_order[mode_index] == 2) ?
2179 x->rd_threshes[THR_NEW2] : this_rd_thresh;
2181 tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv,
2182 best_mode.yrd, mdcounts,
2183 &rate, &rd.rate_y, &distortion, this_rd_thresh) ;
2186 rd.distortion2 += distortion;
2188 /* If even the 'Y' rd value of split is higher than best so far
2189 * then dont bother looking at UV
2191 if (tmp_rd < best_mode.yrd)
2193 /* Now work out UV cost and add it in */
2194 rd_inter4x4_uv(cpi, x, &rd.rate_uv, &rd.distortion_uv, cpi->common.full_pixel);
2195 rd.rate2 += rd.rate_uv;
2196 rd.distortion2 += rd.distortion_uv;
2211 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
2213 vp8_build_intra_predictors_mby_s(xd,
2214 xd->dst.y_buffer - xd->dst.y_stride,
2215 xd->dst.y_buffer - 1,
2219 macro_block_yrd(x, &rd.rate_y, &distortion) ;
2220 rd.rate2 += rd.rate_y;
2221 rd.distortion2 += distortion;
2222 rd.rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode];
2223 rd.rate2 += uv_intra_rate;
2224 rd.rate_uv = uv_intra_rate_tokenonly;
2225 rd.distortion2 += uv_intra_distortion;
2226 rd.distortion_uv = uv_intra_distortion;
2233 int bestsme = INT_MAX;
2234 int step_param = cpi->sf.first_step;
2237 int do_refine=1; /* If last step (1-away) of n-step search doesn't pick the center point as the best match,
2238 we will do a final 1-away diamond refining search */
2240 int sadpb = x->sadperbit16;
2243 int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL;
2244 int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL;
2245 int col_max = (best_ref_mv.as_mv.col>>3) + MAX_FULL_PEL_VAL;
2246 int row_max = (best_ref_mv.as_mv.row>>3) + MAX_FULL_PEL_VAL;
2248 int tmp_col_min = x->mv_col_min;
2249 int tmp_col_max = x->mv_col_max;
2250 int tmp_row_min = x->mv_row_min;
2251 int tmp_row_max = x->mv_row_max;
2255 vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] );
2259 vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp,
2260 x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]);
2262 mvp_full.as_mv.col = mvp.as_mv.col>>3;
2263 mvp_full.as_mv.row = mvp.as_mv.row>>3;
2265 /* Get intersection of UMV window and valid MV window to
2266 * reduce # of checks in diamond search.
2268 if (x->mv_col_min < col_min )
2269 x->mv_col_min = col_min;
2270 if (x->mv_col_max > col_max )
2271 x->mv_col_max = col_max;
2272 if (x->mv_row_min < row_min )
2273 x->mv_row_min = row_min;
2274 if (x->mv_row_max > row_max )
2275 x->mv_row_max = row_max;
2277 /* adjust search range according to sr from mv prediction */
2281 /* Initial step/diamond search */
2283 bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full, &d->bmi.mv,
2284 step_param, sadpb, &num00,
2285 &cpi->fn_ptr[BLOCK_16X16],
2286 x->mvcost, &best_ref_mv);
2287 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2289 /* Further step/diamond searches as necessary */
2291 further_steps = (cpi->sf.max_step_search_steps - 1) - step_param;
2296 /* If there won't be more n-step search, check to see if refining search is needed. */
2297 if (n > further_steps)
2300 while (n < further_steps)
2308 thissme = cpi->diamond_search_sad(x, b, d, &mvp_full,
2309 &d->bmi.mv, step_param + n, sadpb, &num00,
2310 &cpi->fn_ptr[BLOCK_16X16], x->mvcost,
2313 /* check to see if refining search is needed. */
2314 if (num00 > (further_steps-n))
2317 if (thissme < bestsme)
2320 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2324 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
2330 /* final 1-away diamond refining search */
2337 thissme = cpi->refining_search_sad(x, b, d, &d->bmi.mv, sadpb,
2338 search_range, &cpi->fn_ptr[BLOCK_16X16],
2339 x->mvcost, &best_ref_mv);
2341 if (thissme < bestsme)
2344 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2348 d->bmi.mv.as_int = mode_mv[NEWMV].as_int;
2352 x->mv_col_min = tmp_col_min;
2353 x->mv_col_max = tmp_col_max;
2354 x->mv_row_min = tmp_row_min;
2355 x->mv_row_max = tmp_row_max;
2357 if (bestsme < INT_MAX)
2359 int dis; /* TODO: use dis in distortion calculation later. */
2361 cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv,
2363 &cpi->fn_ptr[BLOCK_16X16],
2364 x->mvcost, &dis, &sse);
2367 mode_mv[NEWMV].as_int = d->bmi.mv.as_int;
2369 /* Add the new motion vector cost to our rolling cost variable */
2370 rd.rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96);
2375 /* Clip "next_nearest" so that it does not extend to far out
2378 vp8_clamp_mv2(&mode_mv[this_mode], xd);
2380 /* Do not bother proceeding if the vector (from newmv, nearest
2381 * or near) is 0,0 as this should then be coded using the zeromv
2384 if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) && (mode_mv[this_mode].as_int == 0))
2389 /* Trap vectors that reach beyond the UMV borders
2390 * Note that ALL New MV, Nearest MV Near MV and Zero MV code
2391 * drops through to this point because of the lack of break
2392 * statements in the previous two cases.
2394 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) ||
2395 ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max))
2398 vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]);
2399 this_rd = evaluate_inter_mode_rd(mdcounts, &rd,
2400 &disable_skip, cpi, x);
2407 this_rd = calculate_final_rd_costs(this_rd, &rd, &other_cost,
2408 disable_skip, uv_intra_tteob,
2409 intra_rd_penalty, cpi, x);
2411 /* Keep record of best intra distortion */
2412 if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) &&
2413 (this_rd < best_mode.intra_rd) )
2415 best_mode.intra_rd = this_rd;
2416 *returnintra = rd.distortion2 ;
2418 #if CONFIG_TEMPORAL_DENOISING
2419 if (cpi->oxcf.noise_sensitivity)
2422 vp8_get_inter_mbpred_error(x,&cpi->fn_ptr[BLOCK_16X16],&sse,
2423 mode_mv[this_mode]);
2425 if (sse < best_rd_sse)
2428 /* Store for later use by denoiser. */
2429 if (this_mode == ZEROMV && sse < zero_mv_sse )
2432 x->best_zeromv_reference_frame =
2433 x->e_mbd.mode_info_context->mbmi.ref_frame;
2436 /* Store the best NEWMV in x for later use in the denoiser. */
2437 if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV &&
2441 vp8_get_inter_mbpred_error(x,&cpi->fn_ptr[BLOCK_16X16],&best_sse,
2442 mode_mv[this_mode]);
2443 x->best_sse_inter_mode = NEWMV;
2444 x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv;
2445 x->need_to_clamp_best_mvs =
2446 x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs;
2447 x->best_reference_frame =
2448 x->e_mbd.mode_info_context->mbmi.ref_frame;
2453 /* Did this mode help.. i.i is it the new best mode */
2454 if (this_rd < best_mode.rd || x->skip)
2456 /* Note index of best mode so far */
2457 best_mode_index = mode_index;
2458 *returnrate = rd.rate2;
2459 *returndistortion = rd.distortion2;
2460 if (this_mode <= B_PRED)
2462 x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode;
2463 /* required for left and above block mv */
2464 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2466 update_best_mode(&best_mode, this_rd, &rd, other_cost, x);
2469 /* Testing this mode gave rise to an improvement in best error
2470 * score. Lower threshold a bit for next time
2472 x->rd_thresh_mult[mode_index] =
2473 (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ?
2474 x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT;
2477 /* If the mode did not help improve the best error case then raise
2478 * the threshold for testing that mode next time around.
2482 x->rd_thresh_mult[mode_index] += 4;
2484 if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT)
2485 x->rd_thresh_mult[mode_index] = MAX_THRESHMULT;
2487 x->rd_threshes[mode_index] =
2488 (cpi->rd_baseline_thresh[mode_index] >> 7) *
2489 x->rd_thresh_mult[mode_index];
2496 /* Reduce the activation RD thresholds for the best choice mode */
2497 if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2)))
2499 int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 2);
2501 x->rd_thresh_mult[best_mode_index] =
2502 (x->rd_thresh_mult[best_mode_index] >=
2503 (MIN_THRESHMULT + best_adjustment)) ?
2504 x->rd_thresh_mult[best_mode_index] - best_adjustment :
2506 x->rd_threshes[best_mode_index] =
2507 (cpi->rd_baseline_thresh[best_mode_index] >> 7) *
2508 x->rd_thresh_mult[best_mode_index];
2511 #if CONFIG_TEMPORAL_DENOISING
2512 if (cpi->oxcf.noise_sensitivity)
2514 int uv_denoise = (cpi->oxcf.noise_sensitivity == 2) ? 1 : 0;
2515 int block_index = mb_row * cpi->common.mb_cols + mb_col;
2516 if (x->best_sse_inter_mode == DC_PRED)
2518 /* No best MV found. */
2519 x->best_sse_inter_mode = best_mode.mbmode.mode;
2520 x->best_sse_mv = best_mode.mbmode.mv;
2521 x->need_to_clamp_best_mvs = best_mode.mbmode.need_to_clamp_mvs;
2522 x->best_reference_frame = best_mode.mbmode.ref_frame;
2523 best_sse = best_rd_sse;
2525 vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse,
2526 recon_yoffset, recon_uvoffset,
2527 &cpi->common.lf_info, mb_row, mb_col,
2528 block_index, uv_denoise);
2531 /* Reevaluate ZEROMV after denoising. */
2532 if (best_mode.mbmode.ref_frame == INTRA_FRAME &&
2533 x->best_zeromv_reference_frame != INTRA_FRAME)
2535 int this_rd = INT_MAX;
2536 int disable_skip = 0;
2538 int this_ref_frame = x->best_zeromv_reference_frame;
2539 rd.rate2 = x->ref_frame_cost[this_ref_frame] +
2540 vp8_cost_mv_ref(ZEROMV, mdcounts);
2543 /* set up the proper prediction buffers for the frame */
2544 x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame;
2545 x->e_mbd.pre.y_buffer = plane[this_ref_frame][0];
2546 x->e_mbd.pre.u_buffer = plane[this_ref_frame][1];
2547 x->e_mbd.pre.v_buffer = plane[this_ref_frame][2];
2549 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
2550 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
2551 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2553 this_rd = evaluate_inter_mode_rd(mdcounts, &rd, &disable_skip, cpi, x);
2554 this_rd = calculate_final_rd_costs(this_rd, &rd, &other_cost,
2555 disable_skip, uv_intra_tteob,
2556 intra_rd_penalty, cpi, x);
2557 if (this_rd < best_mode.rd || x->skip)
2559 /* Note index of best mode so far */
2560 best_mode_index = mode_index;
2561 *returnrate = rd.rate2;
2562 *returndistortion = rd.distortion2;
2563 update_best_mode(&best_mode, this_rd, &rd, other_cost, x);
2570 if (cpi->is_src_frame_alt_ref &&
2571 (best_mode.mbmode.mode != ZEROMV || best_mode.mbmode.ref_frame != ALTREF_FRAME))
2573 x->e_mbd.mode_info_context->mbmi.mode = ZEROMV;
2574 x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME;
2575 x->e_mbd.mode_info_context->mbmi.mv.as_int = 0;
2576 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
2577 x->e_mbd.mode_info_context->mbmi.mb_skip_coeff =
2578 (cpi->common.mb_no_coeff_skip);
2579 x->e_mbd.mode_info_context->mbmi.partitioning = 0;
2584 /* macroblock modes */
2585 vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mode.mbmode, sizeof(MB_MODE_INFO));
2587 if (best_mode.mbmode.mode == B_PRED)
2589 for (i = 0; i < 16; i++)
2590 xd->mode_info_context->bmi[i].as_mode = best_mode.bmodes[i].as_mode;
2593 if (best_mode.mbmode.mode == SPLITMV)
2595 for (i = 0; i < 16; i++)
2596 xd->mode_info_context->bmi[i].mv.as_int = best_mode.bmodes[i].mv.as_int;
2598 vpx_memcpy(x->partition_info, &best_mode.partition, sizeof(PARTITION_INFO));
2600 x->e_mbd.mode_info_context->mbmi.mv.as_int =
2601 x->partition_info->bmi[15].mv.as_int;
2605 != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame])
2606 best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int;
2608 rd_update_mvcount(x, &best_ref_mv);
2611 void vp8_rd_pick_intra_mode(MACROBLOCK *x, int *rate_)
2613 int error4x4, error16x16;
2614 int rate4x4, rate16x16 = 0, rateuv;
2615 int dist4x4, dist16x16, distuv;
2617 int rate4x4_tokenonly = 0;
2618 int rate16x16_tokenonly = 0;
2619 int rateuv_tokenonly = 0;
2621 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
2623 rd_pick_intra_mbuv_mode(x, &rateuv, &rateuv_tokenonly, &distuv);
2626 error16x16 = rd_pick_intra16x16mby_mode(x, &rate16x16, &rate16x16_tokenonly,
2629 error4x4 = rd_pick_intra4x4mby_modes(x, &rate4x4, &rate4x4_tokenonly,
2630 &dist4x4, error16x16);
2632 if (error4x4 < error16x16)
2634 x->e_mbd.mode_info_context->mbmi.mode = B_PRED;