michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "vpx_config.h" michael@0: #include "vp8_rtcd.h" michael@0: #include "vp8/common/pragmas.h" michael@0: #include "tokenize.h" michael@0: #include "treewriter.h" michael@0: #include "onyx_int.h" michael@0: #include "modecosts.h" michael@0: #include "encodeintra.h" michael@0: #include "pickinter.h" michael@0: #include "vp8/common/entropymode.h" michael@0: #include "vp8/common/reconinter.h" michael@0: #include "vp8/common/reconintra4x4.h" michael@0: #include "vp8/common/findnearmv.h" michael@0: #include "vp8/common/quant_common.h" michael@0: #include "encodemb.h" michael@0: #include "quantize.h" michael@0: #include "vp8/common/variance.h" michael@0: #include "mcomp.h" michael@0: #include "rdopt.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vp8/common/systemdependent.h" michael@0: #if CONFIG_TEMPORAL_DENOISING michael@0: #include "denoising.h" michael@0: #endif michael@0: extern void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x); michael@0: michael@0: #define MAXF(a,b) (((a) > (b)) ? (a) : (b)) michael@0: michael@0: typedef struct rate_distortion_struct michael@0: { michael@0: int rate2; michael@0: int rate_y; michael@0: int rate_uv; michael@0: int distortion2; michael@0: int distortion_uv; michael@0: } RATE_DISTORTION; michael@0: michael@0: typedef struct best_mode_struct michael@0: { michael@0: int yrd; michael@0: int rd; michael@0: int intra_rd; michael@0: MB_MODE_INFO mbmode; michael@0: union b_mode_info bmodes[16]; michael@0: PARTITION_INFO partition; michael@0: } BEST_MODE; michael@0: michael@0: static const int auto_speed_thresh[17] = michael@0: { michael@0: 1000, michael@0: 200, michael@0: 150, michael@0: 130, michael@0: 150, michael@0: 125, michael@0: 120, michael@0: 115, michael@0: 115, michael@0: 115, michael@0: 115, michael@0: 115, michael@0: 115, michael@0: 115, michael@0: 115, michael@0: 115, michael@0: 105 michael@0: }; michael@0: michael@0: const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES] = michael@0: { michael@0: ZEROMV, michael@0: DC_PRED, michael@0: michael@0: NEARESTMV, michael@0: NEARMV, michael@0: michael@0: ZEROMV, michael@0: NEARESTMV, michael@0: michael@0: ZEROMV, michael@0: NEARESTMV, michael@0: michael@0: NEARMV, michael@0: NEARMV, michael@0: michael@0: V_PRED, michael@0: H_PRED, michael@0: TM_PRED, michael@0: michael@0: NEWMV, michael@0: NEWMV, michael@0: NEWMV, michael@0: michael@0: SPLITMV, michael@0: SPLITMV, michael@0: SPLITMV, michael@0: michael@0: B_PRED, michael@0: }; michael@0: michael@0: /* This table determines the search order in reference frame priority order, michael@0: * which may not necessarily match INTRA,LAST,GOLDEN,ARF michael@0: */ michael@0: const int vp8_ref_frame_order[MAX_MODES] = michael@0: { michael@0: 1, michael@0: 0, michael@0: michael@0: 1, michael@0: 1, michael@0: michael@0: 2, michael@0: 2, michael@0: michael@0: 3, michael@0: 3, michael@0: michael@0: 2, michael@0: 3, michael@0: michael@0: 0, michael@0: 0, michael@0: 0, michael@0: michael@0: 1, michael@0: 2, michael@0: 3, michael@0: michael@0: 1, michael@0: 2, michael@0: 3, michael@0: michael@0: 0, michael@0: }; michael@0: michael@0: static void fill_token_costs( michael@0: int c[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS], michael@0: const vp8_prob p[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES] michael@0: ) michael@0: { michael@0: int i, j, k; michael@0: michael@0: michael@0: for (i = 0; i < BLOCK_TYPES; i++) michael@0: for (j = 0; j < COEF_BANDS; j++) michael@0: for (k = 0; k < PREV_COEF_CONTEXTS; k++) michael@0: michael@0: /* check for pt=0 and band > 1 if block type 0 michael@0: * and 0 if blocktype 1 michael@0: */ michael@0: if (k == 0 && j > (i == 0)) michael@0: vp8_cost_tokens2(c[i][j][k], p [i][j][k], vp8_coef_tree, 2); michael@0: else michael@0: vp8_cost_tokens(c[i][j][k], p [i][j][k], vp8_coef_tree); michael@0: } michael@0: michael@0: static const int rd_iifactor[32] = michael@0: { michael@0: 4, 4, 3, 2, 1, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0 michael@0: }; michael@0: michael@0: /* values are now correlated to quantizer */ michael@0: static const int sad_per_bit16lut[QINDEX_RANGE] = michael@0: { michael@0: 2, 2, 2, 2, 2, 2, 2, 2, michael@0: 2, 2, 2, 2, 2, 2, 2, 2, michael@0: 3, 3, 3, 3, 3, 3, 3, 3, michael@0: 3, 3, 3, 3, 3, 3, 4, 4, michael@0: 4, 4, 4, 4, 4, 4, 4, 4, michael@0: 4, 4, 5, 5, 5, 5, 5, 5, michael@0: 5, 5, 5, 5, 5, 5, 6, 6, michael@0: 6, 6, 6, 6, 6, 6, 6, 6, michael@0: 6, 6, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 8, 8, michael@0: 8, 8, 8, 8, 8, 8, 8, 8, michael@0: 8, 8, 9, 9, 9, 9, 9, 9, michael@0: 9, 9, 9, 9, 9, 9, 10, 10, michael@0: 10, 10, 10, 10, 10, 10, 11, 11, michael@0: 11, 11, 11, 11, 12, 12, 12, 12, michael@0: 12, 12, 13, 13, 13, 13, 14, 14 michael@0: }; michael@0: static const int sad_per_bit4lut[QINDEX_RANGE] = michael@0: { michael@0: 2, 2, 2, 2, 2, 2, 3, 3, michael@0: 3, 3, 3, 3, 3, 3, 3, 3, michael@0: 3, 3, 3, 3, 4, 4, 4, 4, michael@0: 4, 4, 4, 4, 4, 4, 5, 5, michael@0: 5, 5, 5, 5, 6, 6, 6, 6, michael@0: 6, 6, 6, 6, 6, 6, 6, 6, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 8, 8, 8, michael@0: 8, 8, 9, 9, 9, 9, 9, 9, michael@0: 10, 10, 10, 10, 10, 10, 10, 10, michael@0: 11, 11, 11, 11, 11, 11, 11, 11, michael@0: 12, 12, 12, 12, 12, 12, 12, 12, michael@0: 13, 13, 13, 13, 13, 13, 13, 14, michael@0: 14, 14, 14, 14, 15, 15, 15, 15, michael@0: 16, 16, 16, 16, 17, 17, 17, 18, michael@0: 18, 18, 19, 19, 19, 20, 20, 20, michael@0: }; michael@0: michael@0: void vp8cx_initialize_me_consts(VP8_COMP *cpi, int QIndex) michael@0: { michael@0: cpi->mb.sadperbit16 = sad_per_bit16lut[QIndex]; michael@0: cpi->mb.sadperbit4 = sad_per_bit4lut[QIndex]; michael@0: } michael@0: michael@0: void vp8_initialize_rd_consts(VP8_COMP *cpi, MACROBLOCK *x, int Qvalue) michael@0: { michael@0: int q; michael@0: int i; michael@0: double capped_q = (Qvalue < 160) ? (double)Qvalue : 160.0; michael@0: double rdconst = 2.80; michael@0: michael@0: vp8_clear_system_state(); michael@0: michael@0: /* Further tests required to see if optimum is different michael@0: * for key frames, golden frames and arf frames. michael@0: */ michael@0: cpi->RDMULT = (int)(rdconst * (capped_q * capped_q)); michael@0: michael@0: /* Extend rate multiplier along side quantizer zbin increases */ michael@0: if (cpi->mb.zbin_over_quant > 0) michael@0: { michael@0: double oq_factor; michael@0: double modq; michael@0: michael@0: /* Experimental code using the same basic equation as used for Q above michael@0: * The units of cpi->mb.zbin_over_quant are 1/128 of Q bin size michael@0: */ michael@0: oq_factor = 1.0 + ((double)0.0015625 * cpi->mb.zbin_over_quant); michael@0: modq = (int)((double)capped_q * oq_factor); michael@0: cpi->RDMULT = (int)(rdconst * (modq * modq)); michael@0: } michael@0: michael@0: if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME)) michael@0: { michael@0: if (cpi->twopass.next_iiratio > 31) michael@0: cpi->RDMULT += (cpi->RDMULT * rd_iifactor[31]) >> 4; michael@0: else michael@0: cpi->RDMULT += michael@0: (cpi->RDMULT * rd_iifactor[cpi->twopass.next_iiratio]) >> 4; michael@0: } michael@0: michael@0: cpi->mb.errorperbit = (cpi->RDMULT / 110); michael@0: cpi->mb.errorperbit += (cpi->mb.errorperbit==0); michael@0: michael@0: vp8_set_speed_features(cpi); michael@0: michael@0: for (i = 0; i < MAX_MODES; i++) michael@0: { michael@0: x->mode_test_hit_counts[i] = 0; michael@0: } michael@0: michael@0: q = (int)pow(Qvalue, 1.25); michael@0: michael@0: if (q < 8) michael@0: q = 8; michael@0: michael@0: if (cpi->RDMULT > 1000) michael@0: { michael@0: cpi->RDDIV = 1; michael@0: cpi->RDMULT /= 100; michael@0: michael@0: for (i = 0; i < MAX_MODES; i++) michael@0: { michael@0: if (cpi->sf.thresh_mult[i] < INT_MAX) michael@0: { michael@0: x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q / 100; michael@0: } michael@0: else michael@0: { michael@0: x->rd_threshes[i] = INT_MAX; michael@0: } michael@0: michael@0: cpi->rd_baseline_thresh[i] = x->rd_threshes[i]; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: cpi->RDDIV = 100; michael@0: michael@0: for (i = 0; i < MAX_MODES; i++) michael@0: { michael@0: if (cpi->sf.thresh_mult[i] < (INT_MAX / q)) michael@0: { michael@0: x->rd_threshes[i] = cpi->sf.thresh_mult[i] * q; michael@0: } michael@0: else michael@0: { michael@0: x->rd_threshes[i] = INT_MAX; michael@0: } michael@0: michael@0: cpi->rd_baseline_thresh[i] = x->rd_threshes[i]; michael@0: } michael@0: } michael@0: michael@0: { michael@0: /* build token cost array for the type of frame we have now */ michael@0: FRAME_CONTEXT *l = &cpi->lfc_n; michael@0: michael@0: if(cpi->common.refresh_alt_ref_frame) michael@0: l = &cpi->lfc_a; michael@0: else if(cpi->common.refresh_golden_frame) michael@0: l = &cpi->lfc_g; michael@0: michael@0: fill_token_costs( michael@0: cpi->mb.token_costs, michael@0: (const vp8_prob( *)[8][3][11]) l->coef_probs michael@0: ); michael@0: /* michael@0: fill_token_costs( michael@0: cpi->mb.token_costs, michael@0: (const vp8_prob( *)[8][3][11]) cpi->common.fc.coef_probs); michael@0: */ michael@0: michael@0: michael@0: /* TODO make these mode costs depend on last,alt or gold too. (jbb) */ michael@0: vp8_init_mode_costs(cpi); michael@0: } michael@0: michael@0: } michael@0: michael@0: void vp8_auto_select_speed(VP8_COMP *cpi) michael@0: { michael@0: int milliseconds_for_compress = (int)(1000000 / cpi->framerate); michael@0: michael@0: milliseconds_for_compress = milliseconds_for_compress * (16 - cpi->oxcf.cpu_used) / 16; michael@0: michael@0: #if 0 michael@0: michael@0: if (0) michael@0: { michael@0: FILE *f; michael@0: michael@0: f = fopen("speed.stt", "a"); michael@0: fprintf(f, " %8ld %10ld %10ld %10ld\n", michael@0: cpi->common.current_video_frame, cpi->Speed, milliseconds_for_compress, cpi->avg_pick_mode_time); michael@0: fclose(f); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: if (cpi->avg_pick_mode_time < milliseconds_for_compress && (cpi->avg_encode_time - cpi->avg_pick_mode_time) < milliseconds_for_compress) michael@0: { michael@0: if (cpi->avg_pick_mode_time == 0) michael@0: { michael@0: cpi->Speed = 4; michael@0: } michael@0: else michael@0: { michael@0: if (milliseconds_for_compress * 100 < cpi->avg_encode_time * 95) michael@0: { michael@0: cpi->Speed += 2; michael@0: cpi->avg_pick_mode_time = 0; michael@0: cpi->avg_encode_time = 0; michael@0: michael@0: if (cpi->Speed > 16) michael@0: { michael@0: cpi->Speed = 16; michael@0: } michael@0: } michael@0: michael@0: if (milliseconds_for_compress * 100 > cpi->avg_encode_time * auto_speed_thresh[cpi->Speed]) michael@0: { michael@0: cpi->Speed -= 1; michael@0: cpi->avg_pick_mode_time = 0; michael@0: cpi->avg_encode_time = 0; michael@0: michael@0: /* In real-time mode, cpi->speed is in [4, 16]. */ michael@0: if (cpi->Speed < 4) michael@0: { michael@0: cpi->Speed = 4; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: cpi->Speed += 4; michael@0: michael@0: if (cpi->Speed > 16) michael@0: cpi->Speed = 16; michael@0: michael@0: michael@0: cpi->avg_pick_mode_time = 0; michael@0: cpi->avg_encode_time = 0; michael@0: } michael@0: } michael@0: michael@0: int vp8_block_error_c(short *coeff, short *dqcoeff) michael@0: { michael@0: int i; michael@0: int error = 0; michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: int this_diff = coeff[i] - dqcoeff[i]; michael@0: error += this_diff * this_diff; michael@0: } michael@0: michael@0: return error; michael@0: } michael@0: michael@0: int vp8_mbblock_error_c(MACROBLOCK *mb, int dc) michael@0: { michael@0: BLOCK *be; michael@0: BLOCKD *bd; michael@0: int i, j; michael@0: int berror, error = 0; michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: be = &mb->block[i]; michael@0: bd = &mb->e_mbd.block[i]; michael@0: michael@0: berror = 0; michael@0: michael@0: for (j = dc; j < 16; j++) michael@0: { michael@0: int this_diff = be->coeff[j] - bd->dqcoeff[j]; michael@0: berror += this_diff * this_diff; michael@0: } michael@0: michael@0: error += berror; michael@0: } michael@0: michael@0: return error; michael@0: } michael@0: michael@0: int vp8_mbuverror_c(MACROBLOCK *mb) michael@0: { michael@0: michael@0: BLOCK *be; michael@0: BLOCKD *bd; michael@0: michael@0: michael@0: int i; michael@0: int error = 0; michael@0: michael@0: for (i = 16; i < 24; i++) michael@0: { michael@0: be = &mb->block[i]; michael@0: bd = &mb->e_mbd.block[i]; michael@0: michael@0: error += vp8_block_error_c(be->coeff, bd->dqcoeff); michael@0: } michael@0: michael@0: return error; michael@0: } michael@0: michael@0: int VP8_UVSSE(MACROBLOCK *x) michael@0: { michael@0: unsigned char *uptr, *vptr; michael@0: unsigned char *upred_ptr = (*(x->block[16].base_src) + x->block[16].src); michael@0: unsigned char *vpred_ptr = (*(x->block[20].base_src) + x->block[20].src); michael@0: int uv_stride = x->block[16].src_stride; michael@0: michael@0: unsigned int sse1 = 0; michael@0: unsigned int sse2 = 0; michael@0: int mv_row = x->e_mbd.mode_info_context->mbmi.mv.as_mv.row; michael@0: int mv_col = x->e_mbd.mode_info_context->mbmi.mv.as_mv.col; michael@0: int offset; michael@0: int pre_stride = x->e_mbd.pre.uv_stride; michael@0: michael@0: if (mv_row < 0) michael@0: mv_row -= 1; michael@0: else michael@0: mv_row += 1; michael@0: michael@0: if (mv_col < 0) michael@0: mv_col -= 1; michael@0: else michael@0: mv_col += 1; michael@0: michael@0: mv_row /= 2; michael@0: mv_col /= 2; michael@0: michael@0: offset = (mv_row >> 3) * pre_stride + (mv_col >> 3); michael@0: uptr = x->e_mbd.pre.u_buffer + offset; michael@0: vptr = x->e_mbd.pre.v_buffer + offset; michael@0: michael@0: if ((mv_row | mv_col) & 7) michael@0: { michael@0: vp8_sub_pixel_variance8x8(uptr, pre_stride, michael@0: mv_col & 7, mv_row & 7, upred_ptr, uv_stride, &sse2); michael@0: vp8_sub_pixel_variance8x8(vptr, pre_stride, michael@0: mv_col & 7, mv_row & 7, vpred_ptr, uv_stride, &sse1); michael@0: sse2 += sse1; michael@0: } michael@0: else michael@0: { michael@0: vp8_variance8x8(uptr, pre_stride, michael@0: upred_ptr, uv_stride, &sse2); michael@0: vp8_variance8x8(vptr, pre_stride, michael@0: vpred_ptr, uv_stride, &sse1); michael@0: sse2 += sse1; michael@0: } michael@0: return sse2; michael@0: michael@0: } michael@0: michael@0: static int cost_coeffs(MACROBLOCK *mb, BLOCKD *b, int type, ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l) michael@0: { michael@0: int c = !type; /* start at coef 0, unless Y with Y2 */ michael@0: int eob = (int)(*b->eob); michael@0: int pt ; /* surrounding block/prev coef predictor */ michael@0: int cost = 0; michael@0: short *qcoeff_ptr = b->qcoeff; michael@0: michael@0: VP8_COMBINEENTROPYCONTEXTS(pt, *a, *l); michael@0: michael@0: # define QC( I) ( qcoeff_ptr [vp8_default_zig_zag1d[I]] ) michael@0: michael@0: for (; c < eob; c++) michael@0: { michael@0: int v = QC(c); michael@0: int t = vp8_dct_value_tokens_ptr[v].Token; michael@0: cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [t]; michael@0: cost += vp8_dct_value_cost_ptr[v]; michael@0: pt = vp8_prev_token_class[t]; michael@0: } michael@0: michael@0: # undef QC michael@0: michael@0: if (c < 16) michael@0: cost += mb->token_costs [type] [vp8_coef_bands[c]] [pt] [DCT_EOB_TOKEN]; michael@0: michael@0: pt = (c != !type); /* is eob first coefficient; */ michael@0: *a = *l = pt; michael@0: michael@0: return cost; michael@0: } michael@0: michael@0: static int vp8_rdcost_mby(MACROBLOCK *mb) michael@0: { michael@0: int cost = 0; michael@0: int b; michael@0: MACROBLOCKD *x = &mb->e_mbd; michael@0: ENTROPY_CONTEXT_PLANES t_above, t_left; michael@0: ENTROPY_CONTEXT *ta; michael@0: ENTROPY_CONTEXT *tl; michael@0: michael@0: vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: michael@0: ta = (ENTROPY_CONTEXT *)&t_above; michael@0: tl = (ENTROPY_CONTEXT *)&t_left; michael@0: michael@0: for (b = 0; b < 16; b++) michael@0: cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_NO_DC, michael@0: ta + vp8_block2above[b], tl + vp8_block2left[b]); michael@0: michael@0: cost += cost_coeffs(mb, x->block + 24, PLANE_TYPE_Y2, michael@0: ta + vp8_block2above[24], tl + vp8_block2left[24]); michael@0: michael@0: return cost; michael@0: } michael@0: michael@0: static void macro_block_yrd( MACROBLOCK *mb, michael@0: int *Rate, michael@0: int *Distortion) michael@0: { michael@0: int b; michael@0: MACROBLOCKD *const x = &mb->e_mbd; michael@0: BLOCK *const mb_y2 = mb->block + 24; michael@0: BLOCKD *const x_y2 = x->block + 24; michael@0: short *Y2DCPtr = mb_y2->src_diff; michael@0: BLOCK *beptr; michael@0: int d; michael@0: michael@0: vp8_subtract_mby( mb->src_diff, *(mb->block[0].base_src), michael@0: mb->block[0].src_stride, mb->e_mbd.predictor, 16); michael@0: michael@0: /* Fdct and building the 2nd order block */ michael@0: for (beptr = mb->block; beptr < mb->block + 16; beptr += 2) michael@0: { michael@0: mb->short_fdct8x4(beptr->src_diff, beptr->coeff, 32); michael@0: *Y2DCPtr++ = beptr->coeff[0]; michael@0: *Y2DCPtr++ = beptr->coeff[16]; michael@0: } michael@0: michael@0: /* 2nd order fdct */ michael@0: mb->short_walsh4x4(mb_y2->src_diff, mb_y2->coeff, 8); michael@0: michael@0: /* Quantization */ michael@0: for (b = 0; b < 16; b++) michael@0: { michael@0: mb->quantize_b(&mb->block[b], &mb->e_mbd.block[b]); michael@0: } michael@0: michael@0: /* DC predication and Quantization of 2nd Order block */ michael@0: mb->quantize_b(mb_y2, x_y2); michael@0: michael@0: /* Distortion */ michael@0: d = vp8_mbblock_error(mb, 1) << 2; michael@0: d += vp8_block_error(mb_y2->coeff, x_y2->dqcoeff); michael@0: michael@0: *Distortion = (d >> 4); michael@0: michael@0: /* rate */ michael@0: *Rate = vp8_rdcost_mby(mb); michael@0: } michael@0: michael@0: static void copy_predictor(unsigned char *dst, const unsigned char *predictor) michael@0: { michael@0: const unsigned int *p = (const unsigned int *)predictor; michael@0: unsigned int *d = (unsigned int *)dst; michael@0: d[0] = p[0]; michael@0: d[4] = p[4]; michael@0: d[8] = p[8]; michael@0: d[12] = p[12]; michael@0: } michael@0: static int rd_pick_intra4x4block( michael@0: MACROBLOCK *x, michael@0: BLOCK *be, michael@0: BLOCKD *b, michael@0: B_PREDICTION_MODE *best_mode, michael@0: const int *bmode_costs, michael@0: ENTROPY_CONTEXT *a, michael@0: ENTROPY_CONTEXT *l, michael@0: michael@0: int *bestrate, michael@0: int *bestratey, michael@0: int *bestdistortion) michael@0: { michael@0: B_PREDICTION_MODE mode; michael@0: int best_rd = INT_MAX; michael@0: int rate = 0; michael@0: int distortion; michael@0: michael@0: ENTROPY_CONTEXT ta = *a, tempa = *a; michael@0: ENTROPY_CONTEXT tl = *l, templ = *l; michael@0: /* michael@0: * The predictor buffer is a 2d buffer with a stride of 16. Create michael@0: * a temp buffer that meets the stride requirements, but we are only michael@0: * interested in the left 4x4 block michael@0: * */ michael@0: DECLARE_ALIGNED_ARRAY(16, unsigned char, best_predictor, 16*4); michael@0: DECLARE_ALIGNED_ARRAY(16, short, best_dqcoeff, 16); michael@0: int dst_stride = x->e_mbd.dst.y_stride; michael@0: unsigned char *dst = x->e_mbd.dst.y_buffer + b->offset; michael@0: michael@0: unsigned char *Above = dst - dst_stride; michael@0: unsigned char *yleft = dst - 1; michael@0: unsigned char top_left = Above[-1]; michael@0: michael@0: for (mode = B_DC_PRED; mode <= B_HU_PRED; mode++) michael@0: { michael@0: int this_rd; michael@0: int ratey; michael@0: michael@0: rate = bmode_costs[mode]; michael@0: michael@0: vp8_intra4x4_predict(Above, yleft, dst_stride, mode, michael@0: b->predictor, 16, top_left); michael@0: vp8_subtract_b(be, b, 16); michael@0: x->short_fdct4x4(be->src_diff, be->coeff, 32); michael@0: x->quantize_b(be, b); michael@0: michael@0: tempa = ta; michael@0: templ = tl; michael@0: michael@0: ratey = cost_coeffs(x, b, PLANE_TYPE_Y_WITH_DC, &tempa, &templ); michael@0: rate += ratey; michael@0: distortion = vp8_block_error(be->coeff, b->dqcoeff) >> 2; michael@0: michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion); michael@0: michael@0: if (this_rd < best_rd) michael@0: { michael@0: *bestrate = rate; michael@0: *bestratey = ratey; michael@0: *bestdistortion = distortion; michael@0: best_rd = this_rd; michael@0: *best_mode = mode; michael@0: *a = tempa; michael@0: *l = templ; michael@0: copy_predictor(best_predictor, b->predictor); michael@0: vpx_memcpy(best_dqcoeff, b->dqcoeff, 32); michael@0: } michael@0: } michael@0: b->bmi.as_mode = *best_mode; michael@0: michael@0: vp8_short_idct4x4llm(best_dqcoeff, best_predictor, 16, dst, dst_stride); michael@0: michael@0: return best_rd; michael@0: } michael@0: michael@0: static int rd_pick_intra4x4mby_modes(MACROBLOCK *mb, int *Rate, michael@0: int *rate_y, int *Distortion, int best_rd) michael@0: { michael@0: MACROBLOCKD *const xd = &mb->e_mbd; michael@0: int i; michael@0: int cost = mb->mbmode_cost [xd->frame_type] [B_PRED]; michael@0: int distortion = 0; michael@0: int tot_rate_y = 0; michael@0: int64_t total_rd = 0; michael@0: ENTROPY_CONTEXT_PLANES t_above, t_left; michael@0: ENTROPY_CONTEXT *ta; michael@0: ENTROPY_CONTEXT *tl; michael@0: const int *bmode_costs; michael@0: michael@0: vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: michael@0: ta = (ENTROPY_CONTEXT *)&t_above; michael@0: tl = (ENTROPY_CONTEXT *)&t_left; michael@0: michael@0: intra_prediction_down_copy(xd, xd->dst.y_buffer - xd->dst.y_stride + 16); michael@0: michael@0: bmode_costs = mb->inter_bmode_costs; michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: MODE_INFO *const mic = xd->mode_info_context; michael@0: const int mis = xd->mode_info_stride; michael@0: B_PREDICTION_MODE UNINITIALIZED_IS_SAFE(best_mode); michael@0: int UNINITIALIZED_IS_SAFE(r), UNINITIALIZED_IS_SAFE(ry), UNINITIALIZED_IS_SAFE(d); michael@0: michael@0: if (mb->e_mbd.frame_type == KEY_FRAME) michael@0: { michael@0: const B_PREDICTION_MODE A = above_block_mode(mic, i, mis); michael@0: const B_PREDICTION_MODE L = left_block_mode(mic, i); michael@0: michael@0: bmode_costs = mb->bmode_costs[A][L]; michael@0: } michael@0: michael@0: total_rd += rd_pick_intra4x4block( michael@0: mb, mb->block + i, xd->block + i, &best_mode, bmode_costs, michael@0: ta + vp8_block2above[i], michael@0: tl + vp8_block2left[i], &r, &ry, &d); michael@0: michael@0: cost += r; michael@0: distortion += d; michael@0: tot_rate_y += ry; michael@0: michael@0: mic->bmi[i].as_mode = best_mode; michael@0: michael@0: if(total_rd >= (int64_t)best_rd) michael@0: break; michael@0: } michael@0: michael@0: if(total_rd >= (int64_t)best_rd) michael@0: return INT_MAX; michael@0: michael@0: *Rate = cost; michael@0: *rate_y = tot_rate_y; michael@0: *Distortion = distortion; michael@0: michael@0: return RDCOST(mb->rdmult, mb->rddiv, cost, distortion); michael@0: } michael@0: michael@0: michael@0: static int rd_pick_intra16x16mby_mode(MACROBLOCK *x, michael@0: int *Rate, michael@0: int *rate_y, michael@0: int *Distortion) michael@0: { michael@0: MB_PREDICTION_MODE mode; michael@0: MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected); michael@0: int rate, ratey; michael@0: int distortion; michael@0: int best_rd = INT_MAX; michael@0: int this_rd; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: michael@0: /* Y Search for 16x16 intra prediction mode */ michael@0: for (mode = DC_PRED; mode <= TM_PRED; mode++) michael@0: { michael@0: xd->mode_info_context->mbmi.mode = mode; michael@0: michael@0: vp8_build_intra_predictors_mby_s(xd, michael@0: xd->dst.y_buffer - xd->dst.y_stride, michael@0: xd->dst.y_buffer - 1, michael@0: xd->dst.y_stride, michael@0: xd->predictor, michael@0: 16); michael@0: michael@0: macro_block_yrd(x, &ratey, &distortion); michael@0: rate = ratey + x->mbmode_cost[xd->frame_type] michael@0: [xd->mode_info_context->mbmi.mode]; michael@0: michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion); michael@0: michael@0: if (this_rd < best_rd) michael@0: { michael@0: mode_selected = mode; michael@0: best_rd = this_rd; michael@0: *Rate = rate; michael@0: *rate_y = ratey; michael@0: *Distortion = distortion; michael@0: } michael@0: } michael@0: michael@0: xd->mode_info_context->mbmi.mode = mode_selected; michael@0: return best_rd; michael@0: } michael@0: michael@0: static int rd_cost_mbuv(MACROBLOCK *mb) michael@0: { michael@0: int b; michael@0: int cost = 0; michael@0: MACROBLOCKD *x = &mb->e_mbd; michael@0: ENTROPY_CONTEXT_PLANES t_above, t_left; michael@0: ENTROPY_CONTEXT *ta; michael@0: ENTROPY_CONTEXT *tl; michael@0: michael@0: vpx_memcpy(&t_above, mb->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: vpx_memcpy(&t_left, mb->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: michael@0: ta = (ENTROPY_CONTEXT *)&t_above; michael@0: tl = (ENTROPY_CONTEXT *)&t_left; michael@0: michael@0: for (b = 16; b < 24; b++) michael@0: cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_UV, michael@0: ta + vp8_block2above[b], tl + vp8_block2left[b]); michael@0: michael@0: return cost; michael@0: } michael@0: michael@0: michael@0: static int rd_inter16x16_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, michael@0: int *distortion, int fullpixel) michael@0: { michael@0: vp8_build_inter16x16_predictors_mbuv(&x->e_mbd); michael@0: vp8_subtract_mbuv(x->src_diff, michael@0: x->src.u_buffer, x->src.v_buffer, x->src.uv_stride, michael@0: &x->e_mbd.predictor[256], &x->e_mbd.predictor[320], 8); michael@0: michael@0: vp8_transform_mbuv(x); michael@0: vp8_quantize_mbuv(x); michael@0: michael@0: *rate = rd_cost_mbuv(x); michael@0: *distortion = vp8_mbuverror(x) / 4; michael@0: michael@0: return RDCOST(x->rdmult, x->rddiv, *rate, *distortion); michael@0: } michael@0: michael@0: static int rd_inter4x4_uv(VP8_COMP *cpi, MACROBLOCK *x, int *rate, michael@0: int *distortion, int fullpixel) michael@0: { michael@0: vp8_build_inter4x4_predictors_mbuv(&x->e_mbd); michael@0: vp8_subtract_mbuv(x->src_diff, michael@0: x->src.u_buffer, x->src.v_buffer, x->src.uv_stride, michael@0: &x->e_mbd.predictor[256], &x->e_mbd.predictor[320], 8); michael@0: michael@0: vp8_transform_mbuv(x); michael@0: vp8_quantize_mbuv(x); michael@0: michael@0: *rate = rd_cost_mbuv(x); michael@0: *distortion = vp8_mbuverror(x) / 4; michael@0: michael@0: return RDCOST(x->rdmult, x->rddiv, *rate, *distortion); michael@0: } michael@0: michael@0: static void rd_pick_intra_mbuv_mode(MACROBLOCK *x, int *rate, michael@0: int *rate_tokenonly, int *distortion) michael@0: { michael@0: MB_PREDICTION_MODE mode; michael@0: MB_PREDICTION_MODE UNINITIALIZED_IS_SAFE(mode_selected); michael@0: int best_rd = INT_MAX; michael@0: int UNINITIALIZED_IS_SAFE(d), UNINITIALIZED_IS_SAFE(r); michael@0: int rate_to; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: michael@0: for (mode = DC_PRED; mode <= TM_PRED; mode++) michael@0: { michael@0: int this_rate; michael@0: int this_distortion; michael@0: int this_rd; michael@0: michael@0: xd->mode_info_context->mbmi.uv_mode = mode; michael@0: michael@0: vp8_build_intra_predictors_mbuv_s(xd, michael@0: xd->dst.u_buffer - xd->dst.uv_stride, michael@0: xd->dst.v_buffer - xd->dst.uv_stride, michael@0: xd->dst.u_buffer - 1, michael@0: xd->dst.v_buffer - 1, michael@0: xd->dst.uv_stride, michael@0: &xd->predictor[256], &xd->predictor[320], michael@0: 8); michael@0: michael@0: michael@0: vp8_subtract_mbuv(x->src_diff, michael@0: x->src.u_buffer, x->src.v_buffer, x->src.uv_stride, michael@0: &xd->predictor[256], &xd->predictor[320], 8); michael@0: vp8_transform_mbuv(x); michael@0: vp8_quantize_mbuv(x); michael@0: michael@0: rate_to = rd_cost_mbuv(x); michael@0: this_rate = rate_to + x->intra_uv_mode_cost[xd->frame_type][xd->mode_info_context->mbmi.uv_mode]; michael@0: michael@0: this_distortion = vp8_mbuverror(x) / 4; michael@0: michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); michael@0: michael@0: if (this_rd < best_rd) michael@0: { michael@0: best_rd = this_rd; michael@0: d = this_distortion; michael@0: r = this_rate; michael@0: *rate_tokenonly = rate_to; michael@0: mode_selected = mode; michael@0: } michael@0: } michael@0: michael@0: *rate = r; michael@0: *distortion = d; michael@0: michael@0: xd->mode_info_context->mbmi.uv_mode = mode_selected; michael@0: } michael@0: michael@0: int vp8_cost_mv_ref(MB_PREDICTION_MODE m, const int near_mv_ref_ct[4]) michael@0: { michael@0: vp8_prob p [VP8_MVREFS-1]; michael@0: assert(NEARESTMV <= m && m <= SPLITMV); michael@0: vp8_mv_ref_probs(p, near_mv_ref_ct); michael@0: return vp8_cost_token(vp8_mv_ref_tree, p, michael@0: vp8_mv_ref_encoding_array + (m - NEARESTMV)); michael@0: } michael@0: michael@0: void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv) michael@0: { michael@0: x->e_mbd.mode_info_context->mbmi.mode = mb; michael@0: x->e_mbd.mode_info_context->mbmi.mv.as_int = mv->as_int; michael@0: } michael@0: michael@0: static int labels2mode( michael@0: MACROBLOCK *x, michael@0: int const *labelings, int which_label, michael@0: B_PREDICTION_MODE this_mode, michael@0: int_mv *this_mv, int_mv *best_ref_mv, michael@0: int *mvcost[2] michael@0: ) michael@0: { michael@0: MACROBLOCKD *const xd = & x->e_mbd; michael@0: MODE_INFO *const mic = xd->mode_info_context; michael@0: const int mis = xd->mode_info_stride; michael@0: michael@0: int cost = 0; michael@0: int thismvcost = 0; michael@0: michael@0: /* We have to be careful retrieving previously-encoded motion vectors. michael@0: Ones from this macroblock have to be pulled from the BLOCKD array michael@0: as they have not yet made it to the bmi array in our MB_MODE_INFO. */ michael@0: michael@0: int i = 0; michael@0: michael@0: do michael@0: { michael@0: BLOCKD *const d = xd->block + i; michael@0: const int row = i >> 2, col = i & 3; michael@0: michael@0: B_PREDICTION_MODE m; michael@0: michael@0: if (labelings[i] != which_label) michael@0: continue; michael@0: michael@0: if (col && labelings[i] == labelings[i-1]) michael@0: m = LEFT4X4; michael@0: else if (row && labelings[i] == labelings[i-4]) michael@0: m = ABOVE4X4; michael@0: else michael@0: { michael@0: /* the only time we should do costing for new motion vector michael@0: * or mode is when we are on a new label (jbb May 08, 2007) michael@0: */ michael@0: switch (m = this_mode) michael@0: { michael@0: case NEW4X4 : michael@0: thismvcost = vp8_mv_bit_cost(this_mv, best_ref_mv, mvcost, 102); michael@0: break; michael@0: case LEFT4X4: michael@0: this_mv->as_int = col ? d[-1].bmi.mv.as_int : left_block_mv(mic, i); michael@0: break; michael@0: case ABOVE4X4: michael@0: this_mv->as_int = row ? d[-4].bmi.mv.as_int : above_block_mv(mic, i, mis); michael@0: break; michael@0: case ZERO4X4: michael@0: this_mv->as_int = 0; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: if (m == ABOVE4X4) /* replace above with left if same */ michael@0: { michael@0: int_mv left_mv; michael@0: michael@0: left_mv.as_int = col ? d[-1].bmi.mv.as_int : michael@0: left_block_mv(mic, i); michael@0: michael@0: if (left_mv.as_int == this_mv->as_int) michael@0: m = LEFT4X4; michael@0: } michael@0: michael@0: cost = x->inter_bmode_costs[ m]; michael@0: } michael@0: michael@0: d->bmi.mv.as_int = this_mv->as_int; michael@0: michael@0: x->partition_info->bmi[i].mode = m; michael@0: x->partition_info->bmi[i].mv.as_int = this_mv->as_int; michael@0: michael@0: } michael@0: while (++i < 16); michael@0: michael@0: cost += thismvcost ; michael@0: return cost; michael@0: } michael@0: michael@0: static int rdcost_mbsegment_y(MACROBLOCK *mb, const int *labels, michael@0: int which_label, ENTROPY_CONTEXT *ta, michael@0: ENTROPY_CONTEXT *tl) michael@0: { michael@0: int cost = 0; michael@0: int b; michael@0: MACROBLOCKD *x = &mb->e_mbd; michael@0: michael@0: for (b = 0; b < 16; b++) michael@0: if (labels[ b] == which_label) michael@0: cost += cost_coeffs(mb, x->block + b, PLANE_TYPE_Y_WITH_DC, michael@0: ta + vp8_block2above[b], michael@0: tl + vp8_block2left[b]); michael@0: michael@0: return cost; michael@0: michael@0: } michael@0: static unsigned int vp8_encode_inter_mb_segment(MACROBLOCK *x, int const *labels, int which_label) michael@0: { michael@0: int i; michael@0: unsigned int distortion = 0; michael@0: int pre_stride = x->e_mbd.pre.y_stride; michael@0: unsigned char *base_pre = x->e_mbd.pre.y_buffer; michael@0: michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: if (labels[i] == which_label) michael@0: { michael@0: BLOCKD *bd = &x->e_mbd.block[i]; michael@0: BLOCK *be = &x->block[i]; michael@0: michael@0: vp8_build_inter_predictors_b(bd, 16, base_pre, pre_stride, x->e_mbd.subpixel_predict); michael@0: vp8_subtract_b(be, bd, 16); michael@0: x->short_fdct4x4(be->src_diff, be->coeff, 32); michael@0: x->quantize_b(be, bd); michael@0: michael@0: distortion += vp8_block_error(be->coeff, bd->dqcoeff); michael@0: } michael@0: } michael@0: michael@0: return distortion; michael@0: } michael@0: michael@0: michael@0: static const unsigned int segmentation_to_sseshift[4] = {3, 3, 2, 0}; michael@0: michael@0: michael@0: typedef struct michael@0: { michael@0: int_mv *ref_mv; michael@0: int_mv mvp; michael@0: michael@0: int segment_rd; michael@0: int segment_num; michael@0: int r; michael@0: int d; michael@0: int segment_yrate; michael@0: B_PREDICTION_MODE modes[16]; michael@0: int_mv mvs[16]; michael@0: unsigned char eobs[16]; michael@0: michael@0: int mvthresh; michael@0: int *mdcounts; michael@0: michael@0: int_mv sv_mvp[4]; /* save 4 mvp from 8x8 */ michael@0: int sv_istep[2]; /* save 2 initial step_param for 16x8/8x16 */ michael@0: michael@0: } BEST_SEG_INFO; michael@0: michael@0: michael@0: static void rd_check_segment(VP8_COMP *cpi, MACROBLOCK *x, michael@0: BEST_SEG_INFO *bsi, unsigned int segmentation) michael@0: { michael@0: int i; michael@0: int const *labels; michael@0: int br = 0; michael@0: int bd = 0; michael@0: B_PREDICTION_MODE this_mode; michael@0: michael@0: michael@0: int label_count; michael@0: int this_segment_rd = 0; michael@0: int label_mv_thresh; michael@0: int rate = 0; michael@0: int sbr = 0; michael@0: int sbd = 0; michael@0: int segmentyrate = 0; michael@0: michael@0: vp8_variance_fn_ptr_t *v_fn_ptr; michael@0: michael@0: ENTROPY_CONTEXT_PLANES t_above, t_left; michael@0: ENTROPY_CONTEXT *ta; michael@0: ENTROPY_CONTEXT *tl; michael@0: ENTROPY_CONTEXT_PLANES t_above_b, t_left_b; michael@0: ENTROPY_CONTEXT *ta_b; michael@0: ENTROPY_CONTEXT *tl_b; michael@0: michael@0: vpx_memcpy(&t_above, x->e_mbd.above_context, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: vpx_memcpy(&t_left, x->e_mbd.left_context, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: michael@0: ta = (ENTROPY_CONTEXT *)&t_above; michael@0: tl = (ENTROPY_CONTEXT *)&t_left; michael@0: ta_b = (ENTROPY_CONTEXT *)&t_above_b; michael@0: tl_b = (ENTROPY_CONTEXT *)&t_left_b; michael@0: michael@0: br = 0; michael@0: bd = 0; michael@0: michael@0: v_fn_ptr = &cpi->fn_ptr[segmentation]; michael@0: labels = vp8_mbsplits[segmentation]; michael@0: label_count = vp8_mbsplit_count[segmentation]; michael@0: michael@0: /* 64 makes this threshold really big effectively making it so that we michael@0: * very rarely check mvs on segments. setting this to 1 would make mv michael@0: * thresh roughly equal to what it is for macroblocks michael@0: */ michael@0: label_mv_thresh = 1 * bsi->mvthresh / label_count ; michael@0: michael@0: /* Segmentation method overheads */ michael@0: rate = vp8_cost_token(vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + segmentation); michael@0: rate += vp8_cost_mv_ref(SPLITMV, bsi->mdcounts); michael@0: this_segment_rd += RDCOST(x->rdmult, x->rddiv, rate, 0); michael@0: br += rate; michael@0: michael@0: for (i = 0; i < label_count; i++) michael@0: { michael@0: int_mv mode_mv[B_MODE_COUNT]; michael@0: int best_label_rd = INT_MAX; michael@0: B_PREDICTION_MODE mode_selected = ZERO4X4; michael@0: int bestlabelyrate = 0; michael@0: michael@0: /* search for the best motion vector on this segment */ michael@0: for (this_mode = LEFT4X4; this_mode <= NEW4X4 ; this_mode ++) michael@0: { michael@0: int this_rd; michael@0: int distortion; michael@0: int labelyrate; michael@0: ENTROPY_CONTEXT_PLANES t_above_s, t_left_s; michael@0: ENTROPY_CONTEXT *ta_s; michael@0: ENTROPY_CONTEXT *tl_s; michael@0: michael@0: vpx_memcpy(&t_above_s, &t_above, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: vpx_memcpy(&t_left_s, &t_left, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: michael@0: ta_s = (ENTROPY_CONTEXT *)&t_above_s; michael@0: tl_s = (ENTROPY_CONTEXT *)&t_left_s; michael@0: michael@0: if (this_mode == NEW4X4) michael@0: { michael@0: int sseshift; michael@0: int num00; michael@0: int step_param = 0; michael@0: int further_steps; michael@0: int n; michael@0: int thissme; michael@0: int bestsme = INT_MAX; michael@0: int_mv temp_mv; michael@0: BLOCK *c; michael@0: BLOCKD *e; michael@0: michael@0: /* Is the best so far sufficiently good that we cant justify michael@0: * doing a new motion search. michael@0: */ michael@0: if (best_label_rd < label_mv_thresh) michael@0: break; michael@0: michael@0: if(cpi->compressor_speed) michael@0: { michael@0: if (segmentation == BLOCK_8X16 || segmentation == BLOCK_16X8) michael@0: { michael@0: bsi->mvp.as_int = bsi->sv_mvp[i].as_int; michael@0: if (i==1 && segmentation == BLOCK_16X8) michael@0: bsi->mvp.as_int = bsi->sv_mvp[2].as_int; michael@0: michael@0: step_param = bsi->sv_istep[i]; michael@0: } michael@0: michael@0: /* use previous block's result as next block's MV michael@0: * predictor. michael@0: */ michael@0: if (segmentation == BLOCK_4X4 && i>0) michael@0: { michael@0: bsi->mvp.as_int = x->e_mbd.block[i-1].bmi.mv.as_int; michael@0: if (i==4 || i==8 || i==12) michael@0: bsi->mvp.as_int = x->e_mbd.block[i-4].bmi.mv.as_int; michael@0: step_param = 2; michael@0: } michael@0: } michael@0: michael@0: further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; michael@0: michael@0: { michael@0: int sadpb = x->sadperbit4; michael@0: int_mv mvp_full; michael@0: michael@0: mvp_full.as_mv.row = bsi->mvp.as_mv.row >>3; michael@0: mvp_full.as_mv.col = bsi->mvp.as_mv.col >>3; michael@0: michael@0: /* find first label */ michael@0: n = vp8_mbsplit_offset[segmentation][i]; michael@0: michael@0: c = &x->block[n]; michael@0: e = &x->e_mbd.block[n]; michael@0: michael@0: { michael@0: bestsme = cpi->diamond_search_sad(x, c, e, &mvp_full, michael@0: &mode_mv[NEW4X4], step_param, michael@0: sadpb, &num00, v_fn_ptr, michael@0: x->mvcost, bsi->ref_mv); michael@0: michael@0: n = num00; michael@0: num00 = 0; michael@0: michael@0: while (n < further_steps) michael@0: { michael@0: n++; michael@0: michael@0: if (num00) michael@0: num00--; michael@0: else michael@0: { michael@0: thissme = cpi->diamond_search_sad(x, c, e, michael@0: &mvp_full, &temp_mv, michael@0: step_param + n, sadpb, michael@0: &num00, v_fn_ptr, michael@0: x->mvcost, bsi->ref_mv); michael@0: michael@0: if (thissme < bestsme) michael@0: { michael@0: bestsme = thissme; michael@0: mode_mv[NEW4X4].as_int = temp_mv.as_int; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: sseshift = segmentation_to_sseshift[segmentation]; michael@0: michael@0: /* Should we do a full search (best quality only) */ michael@0: if ((cpi->compressor_speed == 0) && (bestsme >> sseshift) > 4000) michael@0: { michael@0: /* Check if mvp_full is within the range. */ michael@0: vp8_clamp_mv(&mvp_full, x->mv_col_min, x->mv_col_max, x->mv_row_min, x->mv_row_max); michael@0: michael@0: thissme = cpi->full_search_sad(x, c, e, &mvp_full, michael@0: sadpb, 16, v_fn_ptr, michael@0: x->mvcost, bsi->ref_mv); michael@0: michael@0: if (thissme < bestsme) michael@0: { michael@0: bestsme = thissme; michael@0: mode_mv[NEW4X4].as_int = e->bmi.mv.as_int; michael@0: } michael@0: else michael@0: { michael@0: /* The full search result is actually worse so michael@0: * re-instate the previous best vector michael@0: */ michael@0: e->bmi.mv.as_int = mode_mv[NEW4X4].as_int; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (bestsme < INT_MAX) michael@0: { michael@0: int disto; michael@0: unsigned int sse; michael@0: cpi->find_fractional_mv_step(x, c, e, &mode_mv[NEW4X4], michael@0: bsi->ref_mv, x->errorperbit, v_fn_ptr, x->mvcost, michael@0: &disto, &sse); michael@0: } michael@0: } /* NEW4X4 */ michael@0: michael@0: rate = labels2mode(x, labels, i, this_mode, &mode_mv[this_mode], michael@0: bsi->ref_mv, x->mvcost); michael@0: michael@0: /* Trap vectors that reach beyond the UMV borders */ michael@0: 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) || michael@0: ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max)) michael@0: { michael@0: continue; michael@0: } michael@0: michael@0: distortion = vp8_encode_inter_mb_segment(x, labels, i) / 4; michael@0: michael@0: labelyrate = rdcost_mbsegment_y(x, labels, i, ta_s, tl_s); michael@0: rate += labelyrate; michael@0: michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion); michael@0: michael@0: if (this_rd < best_label_rd) michael@0: { michael@0: sbr = rate; michael@0: sbd = distortion; michael@0: bestlabelyrate = labelyrate; michael@0: mode_selected = this_mode; michael@0: best_label_rd = this_rd; michael@0: michael@0: vpx_memcpy(ta_b, ta_s, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: vpx_memcpy(tl_b, tl_s, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: michael@0: } michael@0: } /*for each 4x4 mode*/ michael@0: michael@0: vpx_memcpy(ta, ta_b, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: vpx_memcpy(tl, tl_b, sizeof(ENTROPY_CONTEXT_PLANES)); michael@0: michael@0: labels2mode(x, labels, i, mode_selected, &mode_mv[mode_selected], michael@0: bsi->ref_mv, x->mvcost); michael@0: michael@0: br += sbr; michael@0: bd += sbd; michael@0: segmentyrate += bestlabelyrate; michael@0: this_segment_rd += best_label_rd; michael@0: michael@0: if (this_segment_rd >= bsi->segment_rd) michael@0: break; michael@0: michael@0: } /* for each label */ michael@0: michael@0: if (this_segment_rd < bsi->segment_rd) michael@0: { michael@0: bsi->r = br; michael@0: bsi->d = bd; michael@0: bsi->segment_yrate = segmentyrate; michael@0: bsi->segment_rd = this_segment_rd; michael@0: bsi->segment_num = segmentation; michael@0: michael@0: /* store everything needed to come back to this!! */ michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: bsi->mvs[i].as_mv = x->partition_info->bmi[i].mv.as_mv; michael@0: bsi->modes[i] = x->partition_info->bmi[i].mode; michael@0: bsi->eobs[i] = x->e_mbd.eobs[i]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static michael@0: void vp8_cal_step_param(int sr, int *sp) michael@0: { michael@0: int step = 0; michael@0: michael@0: if (sr > MAX_FIRST_STEP) sr = MAX_FIRST_STEP; michael@0: else if (sr < 1) sr = 1; michael@0: michael@0: while (sr>>=1) michael@0: step++; michael@0: michael@0: *sp = MAX_MVSEARCH_STEPS - 1 - step; michael@0: } michael@0: michael@0: static int vp8_rd_pick_best_mbsegmentation(VP8_COMP *cpi, MACROBLOCK *x, michael@0: int_mv *best_ref_mv, int best_rd, michael@0: int *mdcounts, int *returntotrate, michael@0: int *returnyrate, int *returndistortion, michael@0: int mvthresh) michael@0: { michael@0: int i; michael@0: BEST_SEG_INFO bsi; michael@0: michael@0: vpx_memset(&bsi, 0, sizeof(bsi)); michael@0: michael@0: bsi.segment_rd = best_rd; michael@0: bsi.ref_mv = best_ref_mv; michael@0: bsi.mvp.as_int = best_ref_mv->as_int; michael@0: bsi.mvthresh = mvthresh; michael@0: bsi.mdcounts = mdcounts; michael@0: michael@0: for(i = 0; i < 16; i++) michael@0: { michael@0: bsi.modes[i] = ZERO4X4; michael@0: } michael@0: michael@0: if(cpi->compressor_speed == 0) michael@0: { michael@0: /* for now, we will keep the original segmentation order michael@0: when in best quality mode */ michael@0: rd_check_segment(cpi, x, &bsi, BLOCK_16X8); michael@0: rd_check_segment(cpi, x, &bsi, BLOCK_8X16); michael@0: rd_check_segment(cpi, x, &bsi, BLOCK_8X8); michael@0: rd_check_segment(cpi, x, &bsi, BLOCK_4X4); michael@0: } michael@0: else michael@0: { michael@0: int sr; michael@0: michael@0: rd_check_segment(cpi, x, &bsi, BLOCK_8X8); michael@0: michael@0: if (bsi.segment_rd < best_rd) michael@0: { michael@0: int col_min = ((best_ref_mv->as_mv.col+7)>>3) - MAX_FULL_PEL_VAL; michael@0: int row_min = ((best_ref_mv->as_mv.row+7)>>3) - MAX_FULL_PEL_VAL; michael@0: int col_max = (best_ref_mv->as_mv.col>>3) + MAX_FULL_PEL_VAL; michael@0: int row_max = (best_ref_mv->as_mv.row>>3) + MAX_FULL_PEL_VAL; michael@0: michael@0: int tmp_col_min = x->mv_col_min; michael@0: int tmp_col_max = x->mv_col_max; michael@0: int tmp_row_min = x->mv_row_min; michael@0: int tmp_row_max = x->mv_row_max; michael@0: michael@0: /* Get intersection of UMV window and valid MV window to reduce # of checks in diamond search. */ michael@0: if (x->mv_col_min < col_min ) michael@0: x->mv_col_min = col_min; michael@0: if (x->mv_col_max > col_max ) michael@0: x->mv_col_max = col_max; michael@0: if (x->mv_row_min < row_min ) michael@0: x->mv_row_min = row_min; michael@0: if (x->mv_row_max > row_max ) michael@0: x->mv_row_max = row_max; michael@0: michael@0: /* Get 8x8 result */ michael@0: bsi.sv_mvp[0].as_int = bsi.mvs[0].as_int; michael@0: bsi.sv_mvp[1].as_int = bsi.mvs[2].as_int; michael@0: bsi.sv_mvp[2].as_int = bsi.mvs[8].as_int; michael@0: bsi.sv_mvp[3].as_int = bsi.mvs[10].as_int; michael@0: michael@0: /* Use 8x8 result as 16x8/8x16's predictor MV. Adjust search range according to the closeness of 2 MV. */ michael@0: /* block 8X16 */ michael@0: { michael@0: 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); michael@0: vp8_cal_step_param(sr, &bsi.sv_istep[0]); michael@0: michael@0: 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); michael@0: vp8_cal_step_param(sr, &bsi.sv_istep[1]); michael@0: michael@0: rd_check_segment(cpi, x, &bsi, BLOCK_8X16); michael@0: } michael@0: michael@0: /* block 16X8 */ michael@0: { michael@0: 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); michael@0: vp8_cal_step_param(sr, &bsi.sv_istep[0]); michael@0: michael@0: 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); michael@0: vp8_cal_step_param(sr, &bsi.sv_istep[1]); michael@0: michael@0: rd_check_segment(cpi, x, &bsi, BLOCK_16X8); michael@0: } michael@0: michael@0: /* If 8x8 is better than 16x8/8x16, then do 4x4 search */ michael@0: /* Not skip 4x4 if speed=0 (good quality) */ michael@0: if (cpi->sf.no_skip_block4x4_search || bsi.segment_num == BLOCK_8X8) /* || (sv_segment_rd8x8-bsi.segment_rd) < sv_segment_rd8x8>>5) */ michael@0: { michael@0: bsi.mvp.as_int = bsi.sv_mvp[0].as_int; michael@0: rd_check_segment(cpi, x, &bsi, BLOCK_4X4); michael@0: } michael@0: michael@0: /* restore UMV window */ michael@0: x->mv_col_min = tmp_col_min; michael@0: x->mv_col_max = tmp_col_max; michael@0: x->mv_row_min = tmp_row_min; michael@0: x->mv_row_max = tmp_row_max; michael@0: } michael@0: } michael@0: michael@0: /* set it to the best */ michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: BLOCKD *bd = &x->e_mbd.block[i]; michael@0: michael@0: bd->bmi.mv.as_int = bsi.mvs[i].as_int; michael@0: *bd->eob = bsi.eobs[i]; michael@0: } michael@0: michael@0: *returntotrate = bsi.r; michael@0: *returndistortion = bsi.d; michael@0: *returnyrate = bsi.segment_yrate; michael@0: michael@0: /* save partitions */ michael@0: x->e_mbd.mode_info_context->mbmi.partitioning = bsi.segment_num; michael@0: x->partition_info->count = vp8_mbsplit_count[bsi.segment_num]; michael@0: michael@0: for (i = 0; i < x->partition_info->count; i++) michael@0: { michael@0: int j; michael@0: michael@0: j = vp8_mbsplit_offset[bsi.segment_num][i]; michael@0: michael@0: x->partition_info->bmi[i].mode = bsi.modes[j]; michael@0: x->partition_info->bmi[i].mv.as_mv = bsi.mvs[j].as_mv; michael@0: } michael@0: /* michael@0: * used to set x->e_mbd.mode_info_context->mbmi.mv.as_int michael@0: */ michael@0: x->partition_info->bmi[15].mv.as_int = bsi.mvs[15].as_int; michael@0: michael@0: return bsi.segment_rd; michael@0: } michael@0: michael@0: /* The improved MV prediction */ michael@0: void vp8_mv_pred michael@0: ( michael@0: VP8_COMP *cpi, michael@0: MACROBLOCKD *xd, michael@0: const MODE_INFO *here, michael@0: int_mv *mvp, michael@0: int refframe, michael@0: int *ref_frame_sign_bias, michael@0: int *sr, michael@0: int near_sadidx[] michael@0: ) michael@0: { michael@0: const MODE_INFO *above = here - xd->mode_info_stride; michael@0: const MODE_INFO *left = here - 1; michael@0: const MODE_INFO *aboveleft = above - 1; michael@0: int_mv near_mvs[8]; michael@0: int near_ref[8]; michael@0: int_mv mv; michael@0: int vcnt=0; michael@0: int find=0; michael@0: int mb_offset; michael@0: michael@0: int mvx[8]; michael@0: int mvy[8]; michael@0: int i; michael@0: michael@0: mv.as_int = 0; michael@0: michael@0: if(here->mbmi.ref_frame != INTRA_FRAME) michael@0: { michael@0: 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; michael@0: 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; michael@0: michael@0: /* read in 3 nearby block's MVs from current frame as prediction michael@0: * candidates. michael@0: */ michael@0: if (above->mbmi.ref_frame != INTRA_FRAME) michael@0: { michael@0: near_mvs[vcnt].as_int = above->mbmi.mv.as_int; michael@0: mv_bias(ref_frame_sign_bias[above->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias); michael@0: near_ref[vcnt] = above->mbmi.ref_frame; michael@0: } michael@0: vcnt++; michael@0: if (left->mbmi.ref_frame != INTRA_FRAME) michael@0: { michael@0: near_mvs[vcnt].as_int = left->mbmi.mv.as_int; michael@0: mv_bias(ref_frame_sign_bias[left->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias); michael@0: near_ref[vcnt] = left->mbmi.ref_frame; michael@0: } michael@0: vcnt++; michael@0: if (aboveleft->mbmi.ref_frame != INTRA_FRAME) michael@0: { michael@0: near_mvs[vcnt].as_int = aboveleft->mbmi.mv.as_int; michael@0: mv_bias(ref_frame_sign_bias[aboveleft->mbmi.ref_frame], refframe, &near_mvs[vcnt], ref_frame_sign_bias); michael@0: near_ref[vcnt] = aboveleft->mbmi.ref_frame; michael@0: } michael@0: vcnt++; michael@0: michael@0: /* read in 5 nearby block's MVs from last frame. */ michael@0: if(cpi->common.last_frame_type != KEY_FRAME) michael@0: { michael@0: mb_offset = (-xd->mb_to_top_edge/128 + 1) * (xd->mode_info_stride +1) + (-xd->mb_to_left_edge/128 +1) ; michael@0: michael@0: /* current in last frame */ michael@0: if (cpi->lf_ref_frame[mb_offset] != INTRA_FRAME) michael@0: { michael@0: near_mvs[vcnt].as_int = cpi->lfmv[mb_offset].as_int; michael@0: mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset], refframe, &near_mvs[vcnt], ref_frame_sign_bias); michael@0: near_ref[vcnt] = cpi->lf_ref_frame[mb_offset]; michael@0: } michael@0: vcnt++; michael@0: michael@0: /* above in last frame */ michael@0: if (cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1] != INTRA_FRAME) michael@0: { michael@0: near_mvs[vcnt].as_int = cpi->lfmv[mb_offset - xd->mode_info_stride-1].as_int; michael@0: mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset - xd->mode_info_stride-1], refframe, &near_mvs[vcnt], ref_frame_sign_bias); michael@0: near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - xd->mode_info_stride-1]; michael@0: } michael@0: vcnt++; michael@0: michael@0: /* left in last frame */ michael@0: if (cpi->lf_ref_frame[mb_offset-1] != INTRA_FRAME) michael@0: { michael@0: near_mvs[vcnt].as_int = cpi->lfmv[mb_offset -1].as_int; michael@0: mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset -1], refframe, &near_mvs[vcnt], ref_frame_sign_bias); michael@0: near_ref[vcnt] = cpi->lf_ref_frame[mb_offset - 1]; michael@0: } michael@0: vcnt++; michael@0: michael@0: /* right in last frame */ michael@0: if (cpi->lf_ref_frame[mb_offset +1] != INTRA_FRAME) michael@0: { michael@0: near_mvs[vcnt].as_int = cpi->lfmv[mb_offset +1].as_int; michael@0: mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias); michael@0: near_ref[vcnt] = cpi->lf_ref_frame[mb_offset +1]; michael@0: } michael@0: vcnt++; michael@0: michael@0: /* below in last frame */ michael@0: if (cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1] != INTRA_FRAME) michael@0: { michael@0: near_mvs[vcnt].as_int = cpi->lfmv[mb_offset + xd->mode_info_stride +1].as_int; michael@0: mv_bias(cpi->lf_ref_frame_sign_bias[mb_offset + xd->mode_info_stride +1], refframe, &near_mvs[vcnt], ref_frame_sign_bias); michael@0: near_ref[vcnt] = cpi->lf_ref_frame[mb_offset + xd->mode_info_stride +1]; michael@0: } michael@0: vcnt++; michael@0: } michael@0: michael@0: for(i=0; i< vcnt; i++) michael@0: { michael@0: if(near_ref[near_sadidx[i]] != INTRA_FRAME) michael@0: { michael@0: if(here->mbmi.ref_frame == near_ref[near_sadidx[i]]) michael@0: { michael@0: mv.as_int = near_mvs[near_sadidx[i]].as_int; michael@0: find = 1; michael@0: if (i < 3) michael@0: *sr = 3; michael@0: else michael@0: *sr = 2; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(!find) michael@0: { michael@0: for(i=0; ias_int = mv.as_int; michael@0: vp8_clamp_mv2(mvp, xd); michael@0: } michael@0: michael@0: void vp8_cal_sad(VP8_COMP *cpi, MACROBLOCKD *xd, MACROBLOCK *x, int recon_yoffset, int near_sadidx[]) michael@0: { michael@0: /* near_sad indexes: michael@0: * 0-cf above, 1-cf left, 2-cf aboveleft, michael@0: * 3-lf current, 4-lf above, 5-lf left, 6-lf right, 7-lf below michael@0: */ michael@0: int near_sad[8] = {0}; michael@0: BLOCK *b = &x->block[0]; michael@0: unsigned char *src_y_ptr = *(b->base_src); michael@0: michael@0: /* calculate sad for current frame 3 nearby MBs. */ michael@0: if( xd->mb_to_top_edge==0 && xd->mb_to_left_edge ==0) michael@0: { michael@0: near_sad[0] = near_sad[1] = near_sad[2] = INT_MAX; michael@0: }else if(xd->mb_to_top_edge==0) michael@0: { /* only has left MB for sad calculation. */ michael@0: near_sad[0] = near_sad[2] = INT_MAX; michael@0: 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); michael@0: }else if(xd->mb_to_left_edge ==0) michael@0: { /* only has left MB for sad calculation. */ michael@0: near_sad[1] = near_sad[2] = INT_MAX; michael@0: 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); michael@0: }else michael@0: { michael@0: 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); michael@0: 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); michael@0: 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); michael@0: } michael@0: michael@0: if(cpi->common.last_frame_type != KEY_FRAME) michael@0: { michael@0: /* calculate sad for last frame 5 nearby MBs. */ michael@0: unsigned char *pre_y_buffer = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_buffer + recon_yoffset; michael@0: int pre_y_stride = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_stride; michael@0: michael@0: if(xd->mb_to_top_edge==0) near_sad[4] = INT_MAX; michael@0: if(xd->mb_to_left_edge ==0) near_sad[5] = INT_MAX; michael@0: if(xd->mb_to_right_edge ==0) near_sad[6] = INT_MAX; michael@0: if(xd->mb_to_bottom_edge==0) near_sad[7] = INT_MAX; michael@0: michael@0: if(near_sad[4] != INT_MAX) michael@0: 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); michael@0: if(near_sad[5] != INT_MAX) michael@0: near_sad[5] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer - 16, pre_y_stride, UINT_MAX); michael@0: near_sad[3] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer, pre_y_stride, UINT_MAX); michael@0: if(near_sad[6] != INT_MAX) michael@0: near_sad[6] = cpi->fn_ptr[BLOCK_16X16].sdf(src_y_ptr, b->src_stride, pre_y_buffer + 16, pre_y_stride, UINT_MAX); michael@0: if(near_sad[7] != INT_MAX) michael@0: 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); michael@0: } michael@0: michael@0: if(cpi->common.last_frame_type != KEY_FRAME) michael@0: { michael@0: insertsortsad(near_sad, near_sadidx, 8); michael@0: }else michael@0: { michael@0: insertsortsad(near_sad, near_sadidx, 3); michael@0: } michael@0: } michael@0: michael@0: static void rd_update_mvcount(MACROBLOCK *x, int_mv *best_ref_mv) michael@0: { michael@0: if (x->e_mbd.mode_info_context->mbmi.mode == SPLITMV) michael@0: { michael@0: int i; michael@0: michael@0: for (i = 0; i < x->partition_info->count; i++) michael@0: { michael@0: if (x->partition_info->bmi[i].mode == NEW4X4) michael@0: { michael@0: x->MVcount[0][mv_max+((x->partition_info->bmi[i].mv.as_mv.row michael@0: - best_ref_mv->as_mv.row) >> 1)]++; michael@0: x->MVcount[1][mv_max+((x->partition_info->bmi[i].mv.as_mv.col michael@0: - best_ref_mv->as_mv.col) >> 1)]++; michael@0: } michael@0: } michael@0: } michael@0: else if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV) michael@0: { michael@0: x->MVcount[0][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.row michael@0: - best_ref_mv->as_mv.row) >> 1)]++; michael@0: x->MVcount[1][mv_max+((x->e_mbd.mode_info_context->mbmi.mv.as_mv.col michael@0: - best_ref_mv->as_mv.col) >> 1)]++; michael@0: } michael@0: } michael@0: michael@0: static int evaluate_inter_mode_rd(int mdcounts[4], michael@0: RATE_DISTORTION* rd, michael@0: int* disable_skip, michael@0: VP8_COMP *cpi, MACROBLOCK *x) michael@0: { michael@0: MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode; michael@0: BLOCK *b = &x->block[0]; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: int distortion; michael@0: vp8_build_inter16x16_predictors_mby(&x->e_mbd, x->e_mbd.predictor, 16); michael@0: michael@0: if (cpi->active_map_enabled && x->active_ptr[0] == 0) { michael@0: x->skip = 1; michael@0: } michael@0: else if (x->encode_breakout) michael@0: { michael@0: unsigned int sse; michael@0: unsigned int var; michael@0: unsigned int threshold = (xd->block[0].dequant[1] michael@0: * xd->block[0].dequant[1] >>4); michael@0: michael@0: if(threshold < x->encode_breakout) michael@0: threshold = x->encode_breakout; michael@0: michael@0: var = vp8_variance16x16 michael@0: (*(b->base_src), b->src_stride, michael@0: x->e_mbd.predictor, 16, &sse); michael@0: michael@0: if (sse < threshold) michael@0: { michael@0: unsigned int q2dc = xd->block[24].dequant[0]; michael@0: /* If theres is no codeable 2nd order dc michael@0: or a very small uniform pixel change change */ michael@0: if ((sse - var < q2dc * q2dc >>4) || michael@0: (sse /2 > var && sse-var < 64)) michael@0: { michael@0: /* Check u and v to make sure skip is ok */ michael@0: unsigned int sse2 = VP8_UVSSE(x); michael@0: if (sse2 * 2 < threshold) michael@0: { michael@0: x->skip = 1; michael@0: rd->distortion2 = sse + sse2; michael@0: rd->rate2 = 500; michael@0: michael@0: /* for best_yrd calculation */ michael@0: rd->rate_uv = 0; michael@0: rd->distortion_uv = sse2; michael@0: michael@0: *disable_skip = 1; michael@0: return RDCOST(x->rdmult, x->rddiv, rd->rate2, michael@0: rd->distortion2); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* Add in the Mv/mode cost */ michael@0: rd->rate2 += vp8_cost_mv_ref(this_mode, mdcounts); michael@0: michael@0: /* Y cost and distortion */ michael@0: macro_block_yrd(x, &rd->rate_y, &distortion); michael@0: rd->rate2 += rd->rate_y; michael@0: rd->distortion2 += distortion; michael@0: michael@0: /* UV cost and distortion */ michael@0: rd_inter16x16_uv(cpi, x, &rd->rate_uv, &rd->distortion_uv, michael@0: cpi->common.full_pixel); michael@0: rd->rate2 += rd->rate_uv; michael@0: rd->distortion2 += rd->distortion_uv; michael@0: return INT_MAX; michael@0: } michael@0: michael@0: static int calculate_final_rd_costs(int this_rd, michael@0: RATE_DISTORTION* rd, michael@0: int* other_cost, michael@0: int disable_skip, michael@0: int uv_intra_tteob, michael@0: int intra_rd_penalty, michael@0: VP8_COMP *cpi, MACROBLOCK *x) michael@0: { michael@0: MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode; michael@0: michael@0: /* Where skip is allowable add in the default per mb cost for the no michael@0: * skip case. where we then decide to skip we have to delete this and michael@0: * replace it with the cost of signalling a skip michael@0: */ michael@0: if (cpi->common.mb_no_coeff_skip) michael@0: { michael@0: *other_cost += vp8_cost_bit(cpi->prob_skip_false, 0); michael@0: rd->rate2 += *other_cost; michael@0: } michael@0: michael@0: /* Estimate the reference frame signaling cost and add it michael@0: * to the rolling cost variable. michael@0: */ michael@0: rd->rate2 += michael@0: x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame]; michael@0: michael@0: if (!disable_skip) michael@0: { michael@0: /* Test for the condition where skip block will be activated michael@0: * because there are no non zero coefficients and make any michael@0: * necessary adjustment for rate michael@0: */ michael@0: if (cpi->common.mb_no_coeff_skip) michael@0: { michael@0: int i; michael@0: int tteob; michael@0: int has_y2_block = (this_mode!=SPLITMV && this_mode!=B_PRED); michael@0: michael@0: tteob = 0; michael@0: if(has_y2_block) michael@0: tteob += x->e_mbd.eobs[24]; michael@0: michael@0: for (i = 0; i < 16; i++) michael@0: tteob += (x->e_mbd.eobs[i] > has_y2_block); michael@0: michael@0: if (x->e_mbd.mode_info_context->mbmi.ref_frame) michael@0: { michael@0: for (i = 16; i < 24; i++) michael@0: tteob += x->e_mbd.eobs[i]; michael@0: } michael@0: else michael@0: tteob += uv_intra_tteob; michael@0: michael@0: if (tteob == 0) michael@0: { michael@0: rd->rate2 -= (rd->rate_y + rd->rate_uv); michael@0: /* for best_yrd calculation */ michael@0: rd->rate_uv = 0; michael@0: michael@0: /* Back out no skip flag costing and add in skip flag costing */ michael@0: if (cpi->prob_skip_false) michael@0: { michael@0: int prob_skip_cost; michael@0: michael@0: prob_skip_cost = vp8_cost_bit(cpi->prob_skip_false, 1); michael@0: prob_skip_cost -= vp8_cost_bit(cpi->prob_skip_false, 0); michael@0: rd->rate2 += prob_skip_cost; michael@0: *other_cost += prob_skip_cost; michael@0: } michael@0: } michael@0: } michael@0: /* Calculate the final RD estimate for this mode */ michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, rd->rate2, rd->distortion2); michael@0: if (this_rd < INT_MAX && x->e_mbd.mode_info_context->mbmi.ref_frame michael@0: == INTRA_FRAME) michael@0: this_rd += intra_rd_penalty; michael@0: } michael@0: return this_rd; michael@0: } michael@0: michael@0: static void update_best_mode(BEST_MODE* best_mode, int this_rd, michael@0: RATE_DISTORTION* rd, int other_cost, MACROBLOCK *x) michael@0: { michael@0: MB_PREDICTION_MODE this_mode = x->e_mbd.mode_info_context->mbmi.mode; michael@0: michael@0: other_cost += michael@0: x->ref_frame_cost[x->e_mbd.mode_info_context->mbmi.ref_frame]; michael@0: michael@0: /* Calculate the final y RD estimate for this mode */ michael@0: best_mode->yrd = RDCOST(x->rdmult, x->rddiv, (rd->rate2-rd->rate_uv-other_cost), michael@0: (rd->distortion2-rd->distortion_uv)); michael@0: michael@0: best_mode->rd = this_rd; michael@0: vpx_memcpy(&best_mode->mbmode, &x->e_mbd.mode_info_context->mbmi, sizeof(MB_MODE_INFO)); michael@0: vpx_memcpy(&best_mode->partition, x->partition_info, sizeof(PARTITION_INFO)); michael@0: michael@0: if ((this_mode == B_PRED) || (this_mode == SPLITMV)) michael@0: { michael@0: int i; michael@0: for (i = 0; i < 16; i++) michael@0: { michael@0: best_mode->bmodes[i] = x->e_mbd.block[i].bmi; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void vp8_rd_pick_inter_mode(VP8_COMP *cpi, MACROBLOCK *x, int recon_yoffset, michael@0: int recon_uvoffset, int *returnrate, michael@0: int *returndistortion, int *returnintra) michael@0: { michael@0: BLOCK *b = &x->block[0]; michael@0: BLOCKD *d = &x->e_mbd.block[0]; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: int_mv best_ref_mv_sb[2]; michael@0: int_mv mode_mv_sb[2][MB_MODE_COUNT]; michael@0: int_mv best_ref_mv; michael@0: int_mv *mode_mv; michael@0: MB_PREDICTION_MODE this_mode; michael@0: int num00; michael@0: int best_mode_index = 0; michael@0: BEST_MODE best_mode; michael@0: michael@0: int i; michael@0: int mode_index; michael@0: int mdcounts[4]; michael@0: int rate; michael@0: RATE_DISTORTION rd; michael@0: int uv_intra_rate, uv_intra_distortion, uv_intra_rate_tokenonly; michael@0: int uv_intra_tteob = 0; michael@0: int uv_intra_done = 0; michael@0: michael@0: MB_PREDICTION_MODE uv_intra_mode = 0; michael@0: int_mv mvp; michael@0: int near_sadidx[8] = {0, 1, 2, 3, 4, 5, 6, 7}; michael@0: int saddone=0; michael@0: /* search range got from mv_pred(). It uses step_param levels. (0-7) */ michael@0: int sr=0; michael@0: michael@0: unsigned char *plane[4][3]; michael@0: int ref_frame_map[4]; michael@0: int sign_bias = 0; michael@0: michael@0: int intra_rd_penalty = 10* vp8_dc_quant(cpi->common.base_qindex, michael@0: cpi->common.y1dc_delta_q); michael@0: michael@0: #if CONFIG_TEMPORAL_DENOISING michael@0: unsigned int zero_mv_sse = INT_MAX, best_sse = INT_MAX, michael@0: best_rd_sse = INT_MAX; michael@0: #endif michael@0: michael@0: mode_mv = mode_mv_sb[sign_bias]; michael@0: best_ref_mv.as_int = 0; michael@0: best_mode.rd = INT_MAX; michael@0: best_mode.yrd = INT_MAX; michael@0: best_mode.intra_rd = INT_MAX; michael@0: vpx_memset(mode_mv_sb, 0, sizeof(mode_mv_sb)); michael@0: vpx_memset(&best_mode.mbmode, 0, sizeof(best_mode.mbmode)); michael@0: vpx_memset(&best_mode.bmodes, 0, sizeof(best_mode.bmodes)); michael@0: michael@0: /* Setup search priorities */ michael@0: get_reference_search_order(cpi, ref_frame_map); michael@0: michael@0: /* Check to see if there is at least 1 valid reference frame that we need michael@0: * to calculate near_mvs. michael@0: */ michael@0: if (ref_frame_map[1] > 0) michael@0: { michael@0: sign_bias = vp8_find_near_mvs_bias(&x->e_mbd, michael@0: x->e_mbd.mode_info_context, michael@0: mode_mv_sb, michael@0: best_ref_mv_sb, michael@0: mdcounts, michael@0: ref_frame_map[1], michael@0: cpi->common.ref_frame_sign_bias); michael@0: michael@0: mode_mv = mode_mv_sb[sign_bias]; michael@0: best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int; michael@0: } michael@0: michael@0: get_predictor_pointers(cpi, plane, recon_yoffset, recon_uvoffset); michael@0: michael@0: *returnintra = INT_MAX; michael@0: /* Count of the number of MBs tested so far this frame */ michael@0: x->mbs_tested_so_far++; michael@0: michael@0: x->skip = 0; michael@0: michael@0: for (mode_index = 0; mode_index < MAX_MODES; mode_index++) michael@0: { michael@0: int this_rd = INT_MAX; michael@0: int disable_skip = 0; michael@0: int other_cost = 0; michael@0: int this_ref_frame = ref_frame_map[vp8_ref_frame_order[mode_index]]; michael@0: michael@0: /* Test best rd so far against threshold for trying this mode. */ michael@0: if (best_mode.rd <= x->rd_threshes[mode_index]) michael@0: continue; michael@0: michael@0: if (this_ref_frame < 0) michael@0: continue; michael@0: michael@0: /* These variables hold are rolling total cost and distortion for michael@0: * this mode michael@0: */ michael@0: rd.rate2 = 0; michael@0: rd.distortion2 = 0; michael@0: michael@0: this_mode = vp8_mode_order[mode_index]; michael@0: michael@0: x->e_mbd.mode_info_context->mbmi.mode = this_mode; michael@0: x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame; michael@0: michael@0: /* Only consider ZEROMV/ALTREF_FRAME for alt ref frame, michael@0: * unless ARNR filtering is enabled in which case we want michael@0: * an unfiltered alternative michael@0: */ michael@0: if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) michael@0: { michael@0: if (this_mode != ZEROMV || x->e_mbd.mode_info_context->mbmi.ref_frame != ALTREF_FRAME) michael@0: continue; michael@0: } michael@0: michael@0: /* everything but intra */ michael@0: if (x->e_mbd.mode_info_context->mbmi.ref_frame) michael@0: { michael@0: x->e_mbd.pre.y_buffer = plane[this_ref_frame][0]; michael@0: x->e_mbd.pre.u_buffer = plane[this_ref_frame][1]; michael@0: x->e_mbd.pre.v_buffer = plane[this_ref_frame][2]; michael@0: michael@0: if (sign_bias != cpi->common.ref_frame_sign_bias[this_ref_frame]) michael@0: { michael@0: sign_bias = cpi->common.ref_frame_sign_bias[this_ref_frame]; michael@0: mode_mv = mode_mv_sb[sign_bias]; michael@0: best_ref_mv.as_int = best_ref_mv_sb[sign_bias].as_int; michael@0: } michael@0: } michael@0: michael@0: /* Check to see if the testing frequency for this mode is at its michael@0: * max If so then prevent it from being tested and increase the michael@0: * threshold for its testing michael@0: */ michael@0: if (x->mode_test_hit_counts[mode_index] && (cpi->mode_check_freq[mode_index] > 1)) michael@0: { michael@0: if (x->mbs_tested_so_far <= cpi->mode_check_freq[mode_index] * x->mode_test_hit_counts[mode_index]) michael@0: { michael@0: /* Increase the threshold for coding this mode to make it michael@0: * less likely to be chosen michael@0: */ michael@0: x->rd_thresh_mult[mode_index] += 4; michael@0: michael@0: if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT) michael@0: x->rd_thresh_mult[mode_index] = MAX_THRESHMULT; michael@0: michael@0: x->rd_threshes[mode_index] = michael@0: (cpi->rd_baseline_thresh[mode_index] >> 7) * michael@0: x->rd_thresh_mult[mode_index]; michael@0: michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: /* We have now reached the point where we are going to test the michael@0: * current mode so increment the counter for the number of times michael@0: * it has been tested michael@0: */ michael@0: x->mode_test_hit_counts[mode_index] ++; michael@0: michael@0: /* Experimental code. Special case for gf and arf zeromv modes. michael@0: * Increase zbin size to supress noise michael@0: */ michael@0: if (x->zbin_mode_boost_enabled) michael@0: { michael@0: if ( this_ref_frame == INTRA_FRAME ) michael@0: x->zbin_mode_boost = 0; michael@0: else michael@0: { michael@0: if (vp8_mode_order[mode_index] == ZEROMV) michael@0: { michael@0: if (this_ref_frame != LAST_FRAME) michael@0: x->zbin_mode_boost = GF_ZEROMV_ZBIN_BOOST; michael@0: else michael@0: x->zbin_mode_boost = LF_ZEROMV_ZBIN_BOOST; michael@0: } michael@0: else if (vp8_mode_order[mode_index] == SPLITMV) michael@0: x->zbin_mode_boost = 0; michael@0: else michael@0: x->zbin_mode_boost = MV_ZBIN_BOOST; michael@0: } michael@0: michael@0: vp8_update_zbin_extra(cpi, x); michael@0: } michael@0: michael@0: if(!uv_intra_done && this_ref_frame == INTRA_FRAME) michael@0: { michael@0: rd_pick_intra_mbuv_mode(x, &uv_intra_rate, michael@0: &uv_intra_rate_tokenonly, michael@0: &uv_intra_distortion); michael@0: uv_intra_mode = x->e_mbd.mode_info_context->mbmi.uv_mode; michael@0: michael@0: /* michael@0: * Total of the eobs is used later to further adjust rate2. Since uv michael@0: * block's intra eobs will be overwritten when we check inter modes, michael@0: * we need to save uv_intra_tteob here. michael@0: */ michael@0: for (i = 16; i < 24; i++) michael@0: uv_intra_tteob += x->e_mbd.eobs[i]; michael@0: michael@0: uv_intra_done = 1; michael@0: } michael@0: michael@0: switch (this_mode) michael@0: { michael@0: case B_PRED: michael@0: { michael@0: int tmp_rd; michael@0: michael@0: /* Note the rate value returned here includes the cost of michael@0: * coding the BPRED mode: x->mbmode_cost[x->e_mbd.frame_type][BPRED] michael@0: */ michael@0: int distortion; michael@0: tmp_rd = rd_pick_intra4x4mby_modes(x, &rate, &rd.rate_y, &distortion, best_mode.yrd); michael@0: rd.rate2 += rate; michael@0: rd.distortion2 += distortion; michael@0: michael@0: if(tmp_rd < best_mode.yrd) michael@0: { michael@0: rd.rate2 += uv_intra_rate; michael@0: rd.rate_uv = uv_intra_rate_tokenonly; michael@0: rd.distortion2 += uv_intra_distortion; michael@0: rd.distortion_uv = uv_intra_distortion; michael@0: } michael@0: else michael@0: { michael@0: this_rd = INT_MAX; michael@0: disable_skip = 1; michael@0: } michael@0: } michael@0: break; michael@0: michael@0: case SPLITMV: michael@0: { michael@0: int tmp_rd; michael@0: int this_rd_thresh; michael@0: int distortion; michael@0: michael@0: this_rd_thresh = (vp8_ref_frame_order[mode_index] == 1) ? michael@0: x->rd_threshes[THR_NEW1] : x->rd_threshes[THR_NEW3]; michael@0: this_rd_thresh = (vp8_ref_frame_order[mode_index] == 2) ? michael@0: x->rd_threshes[THR_NEW2] : this_rd_thresh; michael@0: michael@0: tmp_rd = vp8_rd_pick_best_mbsegmentation(cpi, x, &best_ref_mv, michael@0: best_mode.yrd, mdcounts, michael@0: &rate, &rd.rate_y, &distortion, this_rd_thresh) ; michael@0: michael@0: rd.rate2 += rate; michael@0: rd.distortion2 += distortion; michael@0: michael@0: /* If even the 'Y' rd value of split is higher than best so far michael@0: * then dont bother looking at UV michael@0: */ michael@0: if (tmp_rd < best_mode.yrd) michael@0: { michael@0: /* Now work out UV cost and add it in */ michael@0: rd_inter4x4_uv(cpi, x, &rd.rate_uv, &rd.distortion_uv, cpi->common.full_pixel); michael@0: rd.rate2 += rd.rate_uv; michael@0: rd.distortion2 += rd.distortion_uv; michael@0: } michael@0: else michael@0: { michael@0: this_rd = INT_MAX; michael@0: disable_skip = 1; michael@0: } michael@0: } michael@0: break; michael@0: case DC_PRED: michael@0: case V_PRED: michael@0: case H_PRED: michael@0: case TM_PRED: michael@0: { michael@0: int distortion; michael@0: x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME; michael@0: michael@0: vp8_build_intra_predictors_mby_s(xd, michael@0: xd->dst.y_buffer - xd->dst.y_stride, michael@0: xd->dst.y_buffer - 1, michael@0: xd->dst.y_stride, michael@0: xd->predictor, michael@0: 16); michael@0: macro_block_yrd(x, &rd.rate_y, &distortion) ; michael@0: rd.rate2 += rd.rate_y; michael@0: rd.distortion2 += distortion; michael@0: rd.rate2 += x->mbmode_cost[x->e_mbd.frame_type][x->e_mbd.mode_info_context->mbmi.mode]; michael@0: rd.rate2 += uv_intra_rate; michael@0: rd.rate_uv = uv_intra_rate_tokenonly; michael@0: rd.distortion2 += uv_intra_distortion; michael@0: rd.distortion_uv = uv_intra_distortion; michael@0: } michael@0: break; michael@0: michael@0: case NEWMV: michael@0: { michael@0: int thissme; michael@0: int bestsme = INT_MAX; michael@0: int step_param = cpi->sf.first_step; michael@0: int further_steps; michael@0: int n; michael@0: int do_refine=1; /* If last step (1-away) of n-step search doesn't pick the center point as the best match, michael@0: we will do a final 1-away diamond refining search */ michael@0: michael@0: int sadpb = x->sadperbit16; michael@0: int_mv mvp_full; michael@0: michael@0: int col_min = ((best_ref_mv.as_mv.col+7)>>3) - MAX_FULL_PEL_VAL; michael@0: int row_min = ((best_ref_mv.as_mv.row+7)>>3) - MAX_FULL_PEL_VAL; michael@0: int col_max = (best_ref_mv.as_mv.col>>3) + MAX_FULL_PEL_VAL; michael@0: int row_max = (best_ref_mv.as_mv.row>>3) + MAX_FULL_PEL_VAL; michael@0: michael@0: int tmp_col_min = x->mv_col_min; michael@0: int tmp_col_max = x->mv_col_max; michael@0: int tmp_row_min = x->mv_row_min; michael@0: int tmp_row_max = x->mv_row_max; michael@0: michael@0: if(!saddone) michael@0: { michael@0: vp8_cal_sad(cpi,xd,x, recon_yoffset ,&near_sadidx[0] ); michael@0: saddone = 1; michael@0: } michael@0: michael@0: vp8_mv_pred(cpi, &x->e_mbd, x->e_mbd.mode_info_context, &mvp, michael@0: x->e_mbd.mode_info_context->mbmi.ref_frame, cpi->common.ref_frame_sign_bias, &sr, &near_sadidx[0]); michael@0: michael@0: mvp_full.as_mv.col = mvp.as_mv.col>>3; michael@0: mvp_full.as_mv.row = mvp.as_mv.row>>3; michael@0: michael@0: /* Get intersection of UMV window and valid MV window to michael@0: * reduce # of checks in diamond search. michael@0: */ michael@0: if (x->mv_col_min < col_min ) michael@0: x->mv_col_min = col_min; michael@0: if (x->mv_col_max > col_max ) michael@0: x->mv_col_max = col_max; michael@0: if (x->mv_row_min < row_min ) michael@0: x->mv_row_min = row_min; michael@0: if (x->mv_row_max > row_max ) michael@0: x->mv_row_max = row_max; michael@0: michael@0: /* adjust search range according to sr from mv prediction */ michael@0: if(sr > step_param) michael@0: step_param = sr; michael@0: michael@0: /* Initial step/diamond search */ michael@0: { michael@0: bestsme = cpi->diamond_search_sad(x, b, d, &mvp_full, &d->bmi.mv, michael@0: step_param, sadpb, &num00, michael@0: &cpi->fn_ptr[BLOCK_16X16], michael@0: x->mvcost, &best_ref_mv); michael@0: mode_mv[NEWMV].as_int = d->bmi.mv.as_int; michael@0: michael@0: /* Further step/diamond searches as necessary */ michael@0: n = 0; michael@0: further_steps = (cpi->sf.max_step_search_steps - 1) - step_param; michael@0: michael@0: n = num00; michael@0: num00 = 0; michael@0: michael@0: /* If there won't be more n-step search, check to see if refining search is needed. */ michael@0: if (n > further_steps) michael@0: do_refine = 0; michael@0: michael@0: while (n < further_steps) michael@0: { michael@0: n++; michael@0: michael@0: if (num00) michael@0: num00--; michael@0: else michael@0: { michael@0: thissme = cpi->diamond_search_sad(x, b, d, &mvp_full, michael@0: &d->bmi.mv, step_param + n, sadpb, &num00, michael@0: &cpi->fn_ptr[BLOCK_16X16], x->mvcost, michael@0: &best_ref_mv); michael@0: michael@0: /* check to see if refining search is needed. */ michael@0: if (num00 > (further_steps-n)) michael@0: do_refine = 0; michael@0: michael@0: if (thissme < bestsme) michael@0: { michael@0: bestsme = thissme; michael@0: mode_mv[NEWMV].as_int = d->bmi.mv.as_int; michael@0: } michael@0: else michael@0: { michael@0: d->bmi.mv.as_int = mode_mv[NEWMV].as_int; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* final 1-away diamond refining search */ michael@0: if (do_refine == 1) michael@0: { michael@0: int search_range; michael@0: michael@0: search_range = 8; michael@0: michael@0: thissme = cpi->refining_search_sad(x, b, d, &d->bmi.mv, sadpb, michael@0: search_range, &cpi->fn_ptr[BLOCK_16X16], michael@0: x->mvcost, &best_ref_mv); michael@0: michael@0: if (thissme < bestsme) michael@0: { michael@0: bestsme = thissme; michael@0: mode_mv[NEWMV].as_int = d->bmi.mv.as_int; michael@0: } michael@0: else michael@0: { michael@0: d->bmi.mv.as_int = mode_mv[NEWMV].as_int; michael@0: } michael@0: } michael@0: michael@0: x->mv_col_min = tmp_col_min; michael@0: x->mv_col_max = tmp_col_max; michael@0: x->mv_row_min = tmp_row_min; michael@0: x->mv_row_max = tmp_row_max; michael@0: michael@0: if (bestsme < INT_MAX) michael@0: { michael@0: int dis; /* TODO: use dis in distortion calculation later. */ michael@0: unsigned int sse; michael@0: cpi->find_fractional_mv_step(x, b, d, &d->bmi.mv, &best_ref_mv, michael@0: x->errorperbit, michael@0: &cpi->fn_ptr[BLOCK_16X16], michael@0: x->mvcost, &dis, &sse); michael@0: } michael@0: michael@0: mode_mv[NEWMV].as_int = d->bmi.mv.as_int; michael@0: michael@0: /* Add the new motion vector cost to our rolling cost variable */ michael@0: rd.rate2 += vp8_mv_bit_cost(&mode_mv[NEWMV], &best_ref_mv, x->mvcost, 96); michael@0: } michael@0: michael@0: case NEARESTMV: michael@0: case NEARMV: michael@0: /* Clip "next_nearest" so that it does not extend to far out michael@0: * of image michael@0: */ michael@0: vp8_clamp_mv2(&mode_mv[this_mode], xd); michael@0: michael@0: /* Do not bother proceeding if the vector (from newmv, nearest michael@0: * or near) is 0,0 as this should then be coded using the zeromv michael@0: * mode. michael@0: */ michael@0: if (((this_mode == NEARMV) || (this_mode == NEARESTMV)) && (mode_mv[this_mode].as_int == 0)) michael@0: continue; michael@0: michael@0: case ZEROMV: michael@0: michael@0: /* Trap vectors that reach beyond the UMV borders michael@0: * Note that ALL New MV, Nearest MV Near MV and Zero MV code michael@0: * drops through to this point because of the lack of break michael@0: * statements in the previous two cases. michael@0: */ michael@0: 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) || michael@0: ((mode_mv[this_mode].as_mv.col >> 3) < x->mv_col_min) || ((mode_mv[this_mode].as_mv.col >> 3) > x->mv_col_max)) michael@0: continue; michael@0: michael@0: vp8_set_mbmode_and_mvs(x, this_mode, &mode_mv[this_mode]); michael@0: this_rd = evaluate_inter_mode_rd(mdcounts, &rd, michael@0: &disable_skip, cpi, x); michael@0: break; michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: this_rd = calculate_final_rd_costs(this_rd, &rd, &other_cost, michael@0: disable_skip, uv_intra_tteob, michael@0: intra_rd_penalty, cpi, x); michael@0: michael@0: /* Keep record of best intra distortion */ michael@0: if ((x->e_mbd.mode_info_context->mbmi.ref_frame == INTRA_FRAME) && michael@0: (this_rd < best_mode.intra_rd) ) michael@0: { michael@0: best_mode.intra_rd = this_rd; michael@0: *returnintra = rd.distortion2 ; michael@0: } michael@0: #if CONFIG_TEMPORAL_DENOISING michael@0: if (cpi->oxcf.noise_sensitivity) michael@0: { michael@0: unsigned int sse; michael@0: vp8_get_inter_mbpred_error(x,&cpi->fn_ptr[BLOCK_16X16],&sse, michael@0: mode_mv[this_mode]); michael@0: michael@0: if (sse < best_rd_sse) michael@0: best_rd_sse = sse; michael@0: michael@0: /* Store for later use by denoiser. */ michael@0: if (this_mode == ZEROMV && sse < zero_mv_sse ) michael@0: { michael@0: zero_mv_sse = sse; michael@0: x->best_zeromv_reference_frame = michael@0: x->e_mbd.mode_info_context->mbmi.ref_frame; michael@0: } michael@0: michael@0: /* Store the best NEWMV in x for later use in the denoiser. */ michael@0: if (x->e_mbd.mode_info_context->mbmi.mode == NEWMV && michael@0: sse < best_sse) michael@0: { michael@0: best_sse = sse; michael@0: vp8_get_inter_mbpred_error(x,&cpi->fn_ptr[BLOCK_16X16],&best_sse, michael@0: mode_mv[this_mode]); michael@0: x->best_sse_inter_mode = NEWMV; michael@0: x->best_sse_mv = x->e_mbd.mode_info_context->mbmi.mv; michael@0: x->need_to_clamp_best_mvs = michael@0: x->e_mbd.mode_info_context->mbmi.need_to_clamp_mvs; michael@0: x->best_reference_frame = michael@0: x->e_mbd.mode_info_context->mbmi.ref_frame; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* Did this mode help.. i.i is it the new best mode */ michael@0: if (this_rd < best_mode.rd || x->skip) michael@0: { michael@0: /* Note index of best mode so far */ michael@0: best_mode_index = mode_index; michael@0: *returnrate = rd.rate2; michael@0: *returndistortion = rd.distortion2; michael@0: if (this_mode <= B_PRED) michael@0: { michael@0: x->e_mbd.mode_info_context->mbmi.uv_mode = uv_intra_mode; michael@0: /* required for left and above block mv */ michael@0: x->e_mbd.mode_info_context->mbmi.mv.as_int = 0; michael@0: } michael@0: update_best_mode(&best_mode, this_rd, &rd, other_cost, x); michael@0: michael@0: michael@0: /* Testing this mode gave rise to an improvement in best error michael@0: * score. Lower threshold a bit for next time michael@0: */ michael@0: x->rd_thresh_mult[mode_index] = michael@0: (x->rd_thresh_mult[mode_index] >= (MIN_THRESHMULT + 2)) ? michael@0: x->rd_thresh_mult[mode_index] - 2 : MIN_THRESHMULT; michael@0: } michael@0: michael@0: /* If the mode did not help improve the best error case then raise michael@0: * the threshold for testing that mode next time around. michael@0: */ michael@0: else michael@0: { michael@0: x->rd_thresh_mult[mode_index] += 4; michael@0: michael@0: if (x->rd_thresh_mult[mode_index] > MAX_THRESHMULT) michael@0: x->rd_thresh_mult[mode_index] = MAX_THRESHMULT; michael@0: } michael@0: x->rd_threshes[mode_index] = michael@0: (cpi->rd_baseline_thresh[mode_index] >> 7) * michael@0: x->rd_thresh_mult[mode_index]; michael@0: michael@0: if (x->skip) michael@0: break; michael@0: michael@0: } michael@0: michael@0: /* Reduce the activation RD thresholds for the best choice mode */ michael@0: if ((cpi->rd_baseline_thresh[best_mode_index] > 0) && (cpi->rd_baseline_thresh[best_mode_index] < (INT_MAX >> 2))) michael@0: { michael@0: int best_adjustment = (x->rd_thresh_mult[best_mode_index] >> 2); michael@0: michael@0: x->rd_thresh_mult[best_mode_index] = michael@0: (x->rd_thresh_mult[best_mode_index] >= michael@0: (MIN_THRESHMULT + best_adjustment)) ? michael@0: x->rd_thresh_mult[best_mode_index] - best_adjustment : michael@0: MIN_THRESHMULT; michael@0: x->rd_threshes[best_mode_index] = michael@0: (cpi->rd_baseline_thresh[best_mode_index] >> 7) * michael@0: x->rd_thresh_mult[best_mode_index]; michael@0: } michael@0: michael@0: #if CONFIG_TEMPORAL_DENOISING michael@0: if (cpi->oxcf.noise_sensitivity) michael@0: { michael@0: if (x->best_sse_inter_mode == DC_PRED) michael@0: { michael@0: /* No best MV found. */ michael@0: x->best_sse_inter_mode = best_mode.mbmode.mode; michael@0: x->best_sse_mv = best_mode.mbmode.mv; michael@0: x->need_to_clamp_best_mvs = best_mode.mbmode.need_to_clamp_mvs; michael@0: x->best_reference_frame = best_mode.mbmode.ref_frame; michael@0: best_sse = best_rd_sse; michael@0: } michael@0: vp8_denoiser_denoise_mb(&cpi->denoiser, x, best_sse, zero_mv_sse, michael@0: recon_yoffset, recon_uvoffset); michael@0: michael@0: michael@0: /* Reevaluate ZEROMV after denoising. */ michael@0: if (best_mode.mbmode.ref_frame == INTRA_FRAME && michael@0: x->best_zeromv_reference_frame != INTRA_FRAME) michael@0: { michael@0: int this_rd = INT_MAX; michael@0: int disable_skip = 0; michael@0: int other_cost = 0; michael@0: int this_ref_frame = x->best_zeromv_reference_frame; michael@0: rd.rate2 = x->ref_frame_cost[this_ref_frame] + michael@0: vp8_cost_mv_ref(ZEROMV, mdcounts); michael@0: rd.distortion2 = 0; michael@0: michael@0: /* set up the proper prediction buffers for the frame */ michael@0: x->e_mbd.mode_info_context->mbmi.ref_frame = this_ref_frame; michael@0: x->e_mbd.pre.y_buffer = plane[this_ref_frame][0]; michael@0: x->e_mbd.pre.u_buffer = plane[this_ref_frame][1]; michael@0: x->e_mbd.pre.v_buffer = plane[this_ref_frame][2]; michael@0: michael@0: x->e_mbd.mode_info_context->mbmi.mode = ZEROMV; michael@0: x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED; michael@0: x->e_mbd.mode_info_context->mbmi.mv.as_int = 0; michael@0: michael@0: this_rd = evaluate_inter_mode_rd(mdcounts, &rd, &disable_skip, cpi, x); michael@0: this_rd = calculate_final_rd_costs(this_rd, &rd, &other_cost, michael@0: disable_skip, uv_intra_tteob, michael@0: intra_rd_penalty, cpi, x); michael@0: if (this_rd < best_mode.rd || x->skip) michael@0: { michael@0: /* Note index of best mode so far */ michael@0: best_mode_index = mode_index; michael@0: *returnrate = rd.rate2; michael@0: *returndistortion = rd.distortion2; michael@0: update_best_mode(&best_mode, this_rd, &rd, other_cost, x); michael@0: } michael@0: } michael@0: michael@0: } michael@0: #endif michael@0: michael@0: if (cpi->is_src_frame_alt_ref && michael@0: (best_mode.mbmode.mode != ZEROMV || best_mode.mbmode.ref_frame != ALTREF_FRAME)) michael@0: { michael@0: x->e_mbd.mode_info_context->mbmi.mode = ZEROMV; michael@0: x->e_mbd.mode_info_context->mbmi.ref_frame = ALTREF_FRAME; michael@0: x->e_mbd.mode_info_context->mbmi.mv.as_int = 0; michael@0: x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED; michael@0: x->e_mbd.mode_info_context->mbmi.mb_skip_coeff = michael@0: (cpi->common.mb_no_coeff_skip); michael@0: x->e_mbd.mode_info_context->mbmi.partitioning = 0; michael@0: return; michael@0: } michael@0: michael@0: michael@0: /* macroblock modes */ michael@0: vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mode.mbmode, sizeof(MB_MODE_INFO)); michael@0: michael@0: if (best_mode.mbmode.mode == B_PRED) michael@0: { michael@0: for (i = 0; i < 16; i++) michael@0: xd->mode_info_context->bmi[i].as_mode = best_mode.bmodes[i].as_mode; michael@0: } michael@0: michael@0: if (best_mode.mbmode.mode == SPLITMV) michael@0: { michael@0: for (i = 0; i < 16; i++) michael@0: xd->mode_info_context->bmi[i].mv.as_int = best_mode.bmodes[i].mv.as_int; michael@0: michael@0: vpx_memcpy(x->partition_info, &best_mode.partition, sizeof(PARTITION_INFO)); michael@0: michael@0: x->e_mbd.mode_info_context->mbmi.mv.as_int = michael@0: x->partition_info->bmi[15].mv.as_int; michael@0: } michael@0: michael@0: if (sign_bias michael@0: != cpi->common.ref_frame_sign_bias[xd->mode_info_context->mbmi.ref_frame]) michael@0: best_ref_mv.as_int = best_ref_mv_sb[!sign_bias].as_int; michael@0: michael@0: rd_update_mvcount(x, &best_ref_mv); michael@0: } michael@0: michael@0: void vp8_rd_pick_intra_mode(MACROBLOCK *x, int *rate_) michael@0: { michael@0: int error4x4, error16x16; michael@0: int rate4x4, rate16x16 = 0, rateuv; michael@0: int dist4x4, dist16x16, distuv; michael@0: int rate; michael@0: int rate4x4_tokenonly = 0; michael@0: int rate16x16_tokenonly = 0; michael@0: int rateuv_tokenonly = 0; michael@0: michael@0: x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME; michael@0: michael@0: rd_pick_intra_mbuv_mode(x, &rateuv, &rateuv_tokenonly, &distuv); michael@0: rate = rateuv; michael@0: michael@0: error16x16 = rd_pick_intra16x16mby_mode(x, &rate16x16, &rate16x16_tokenonly, michael@0: &dist16x16); michael@0: michael@0: error4x4 = rd_pick_intra4x4mby_modes(x, &rate4x4, &rate4x4_tokenonly, michael@0: &dist4x4, error16x16); michael@0: michael@0: if (error4x4 < error16x16) michael@0: { michael@0: x->e_mbd.mode_info_context->mbmi.mode = B_PRED; michael@0: rate += rate4x4; michael@0: } michael@0: else michael@0: { michael@0: rate += rate16x16; michael@0: } michael@0: michael@0: *rate_ = rate; michael@0: }