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: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "vp9/common/vp9_pragmas.h" michael@0: #include "vp9/encoder/vp9_tokenize.h" michael@0: #include "vp9/encoder/vp9_treewriter.h" michael@0: #include "vp9/encoder/vp9_onyx_int.h" michael@0: #include "vp9/encoder/vp9_modecosts.h" michael@0: #include "vp9/encoder/vp9_encodeintra.h" michael@0: #include "vp9/common/vp9_entropymode.h" michael@0: #include "vp9/common/vp9_reconinter.h" michael@0: #include "vp9/common/vp9_reconintra.h" michael@0: #include "vp9/common/vp9_findnearmv.h" michael@0: #include "vp9/common/vp9_quant_common.h" michael@0: #include "vp9/encoder/vp9_encodemb.h" michael@0: #include "vp9/encoder/vp9_quantize.h" michael@0: #include "vp9/encoder/vp9_variance.h" michael@0: #include "vp9/encoder/vp9_mcomp.h" michael@0: #include "vp9/encoder/vp9_rdopt.h" michael@0: #include "vp9/encoder/vp9_ratectrl.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vp9/common/vp9_systemdependent.h" michael@0: #include "vp9/encoder/vp9_encodemv.h" michael@0: #include "vp9/common/vp9_seg_common.h" michael@0: #include "vp9/common/vp9_pred_common.h" michael@0: #include "vp9/common/vp9_entropy.h" michael@0: #include "./vp9_rtcd.h" michael@0: #include "vp9/common/vp9_mvref_common.h" michael@0: #include "vp9/common/vp9_common.h" michael@0: michael@0: #define INVALID_MV 0x80008000 michael@0: michael@0: /* Factor to weigh the rate for switchable interp filters */ michael@0: #define SWITCHABLE_INTERP_RATE_FACTOR 1 michael@0: michael@0: #define LAST_FRAME_MODE_MASK 0xFFEDCD60 michael@0: #define GOLDEN_FRAME_MODE_MASK 0xFFDA3BB0 michael@0: #define ALT_REF_MODE_MASK 0xFFC648D0 michael@0: michael@0: #define MIN_EARLY_TERM_INDEX 3 michael@0: michael@0: const MODE_DEFINITION vp9_mode_order[MAX_MODES] = { michael@0: {NEARESTMV, LAST_FRAME, NONE}, michael@0: {NEARESTMV, ALTREF_FRAME, NONE}, michael@0: {NEARESTMV, GOLDEN_FRAME, NONE}, michael@0: michael@0: {DC_PRED, INTRA_FRAME, NONE}, michael@0: michael@0: {NEWMV, LAST_FRAME, NONE}, michael@0: {NEWMV, ALTREF_FRAME, NONE}, michael@0: {NEWMV, GOLDEN_FRAME, NONE}, michael@0: michael@0: {NEARMV, LAST_FRAME, NONE}, michael@0: {NEARMV, ALTREF_FRAME, NONE}, michael@0: {NEARESTMV, LAST_FRAME, ALTREF_FRAME}, michael@0: {NEARESTMV, GOLDEN_FRAME, ALTREF_FRAME}, michael@0: michael@0: {TM_PRED, INTRA_FRAME, NONE}, michael@0: michael@0: {NEARMV, LAST_FRAME, ALTREF_FRAME}, michael@0: {NEWMV, LAST_FRAME, ALTREF_FRAME}, michael@0: {NEARMV, GOLDEN_FRAME, NONE}, michael@0: {NEARMV, GOLDEN_FRAME, ALTREF_FRAME}, michael@0: {NEWMV, GOLDEN_FRAME, ALTREF_FRAME}, michael@0: michael@0: {ZEROMV, LAST_FRAME, NONE}, michael@0: {ZEROMV, GOLDEN_FRAME, NONE}, michael@0: {ZEROMV, ALTREF_FRAME, NONE}, michael@0: {ZEROMV, LAST_FRAME, ALTREF_FRAME}, michael@0: {ZEROMV, GOLDEN_FRAME, ALTREF_FRAME}, michael@0: michael@0: {H_PRED, INTRA_FRAME, NONE}, michael@0: {V_PRED, INTRA_FRAME, NONE}, michael@0: {D135_PRED, INTRA_FRAME, NONE}, michael@0: {D207_PRED, INTRA_FRAME, NONE}, michael@0: {D153_PRED, INTRA_FRAME, NONE}, michael@0: {D63_PRED, INTRA_FRAME, NONE}, michael@0: {D117_PRED, INTRA_FRAME, NONE}, michael@0: {D45_PRED, INTRA_FRAME, NONE}, michael@0: }; michael@0: michael@0: const REF_DEFINITION vp9_ref_order[MAX_REFS] = { michael@0: {LAST_FRAME, NONE}, michael@0: {GOLDEN_FRAME, NONE}, michael@0: {ALTREF_FRAME, NONE}, michael@0: {LAST_FRAME, ALTREF_FRAME}, michael@0: {GOLDEN_FRAME, ALTREF_FRAME}, michael@0: {INTRA_FRAME, NONE}, michael@0: }; michael@0: michael@0: // The baseline rd thresholds for breaking out of the rd loop for michael@0: // certain modes are assumed to be based on 8x8 blocks. michael@0: // This table is used to correct for blocks size. michael@0: // The factors here are << 2 (2 = x0.5, 32 = x8 etc). michael@0: static int rd_thresh_block_size_factor[BLOCK_SIZES] = michael@0: {2, 3, 3, 4, 6, 6, 8, 12, 12, 16, 24, 24, 32}; michael@0: michael@0: #define RD_THRESH_MAX_FACT 64 michael@0: #define RD_THRESH_INC 1 michael@0: #define RD_THRESH_POW 1.25 michael@0: #define RD_MULT_EPB_RATIO 64 michael@0: michael@0: #define MV_COST_WEIGHT 108 michael@0: #define MV_COST_WEIGHT_SUB 120 michael@0: michael@0: static void fill_token_costs(vp9_coeff_cost *c, michael@0: vp9_coeff_probs_model (*p)[BLOCK_TYPES]) { michael@0: int i, j, k, l; michael@0: TX_SIZE t; michael@0: for (t = TX_4X4; t <= TX_32X32; t++) michael@0: for (i = 0; i < BLOCK_TYPES; i++) michael@0: for (j = 0; j < REF_TYPES; j++) michael@0: for (k = 0; k < COEF_BANDS; k++) michael@0: for (l = 0; l < PREV_COEF_CONTEXTS; l++) { michael@0: vp9_prob probs[ENTROPY_NODES]; michael@0: vp9_model_to_full_probs(p[t][i][j][k][l], probs); michael@0: vp9_cost_tokens((int *)c[t][i][j][k][0][l], probs, michael@0: vp9_coef_tree); michael@0: vp9_cost_tokens_skip((int *)c[t][i][j][k][1][l], probs, michael@0: vp9_coef_tree); michael@0: assert(c[t][i][j][k][0][l][DCT_EOB_TOKEN] == michael@0: c[t][i][j][k][1][l][DCT_EOB_TOKEN]); michael@0: } michael@0: } michael@0: michael@0: static const int rd_iifactor[32] = { 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: // 3* dc_qlookup[Q]*dc_qlookup[Q]; michael@0: michael@0: /* values are now correlated to quantizer */ michael@0: static int sad_per_bit16lut[QINDEX_RANGE]; michael@0: static int sad_per_bit4lut[QINDEX_RANGE]; michael@0: michael@0: void vp9_init_me_luts() { michael@0: int i; michael@0: michael@0: // Initialize the sad lut tables using a formulaic calculation for now michael@0: // This is to make it easier to resolve the impact of experimental changes michael@0: // to the quantizer tables. michael@0: for (i = 0; i < QINDEX_RANGE; i++) { michael@0: sad_per_bit16lut[i] = michael@0: (int)((0.0418 * vp9_convert_qindex_to_q(i)) + 2.4107); michael@0: sad_per_bit4lut[i] = (int)(0.063 * vp9_convert_qindex_to_q(i) + 2.742); michael@0: } michael@0: } michael@0: michael@0: int vp9_compute_rd_mult(VP9_COMP *cpi, int qindex) { michael@0: const int q = vp9_dc_quant(qindex, 0); michael@0: // TODO(debargha): Adjust the function below michael@0: int rdmult = 88 * q * q / 25; michael@0: if (cpi->pass == 2 && (cpi->common.frame_type != KEY_FRAME)) { michael@0: if (cpi->twopass.next_iiratio > 31) michael@0: rdmult += (rdmult * rd_iifactor[31]) >> 4; michael@0: else michael@0: rdmult += (rdmult * rd_iifactor[cpi->twopass.next_iiratio]) >> 4; michael@0: } michael@0: return rdmult; michael@0: } michael@0: michael@0: static int compute_rd_thresh_factor(int qindex) { michael@0: int q; michael@0: // TODO(debargha): Adjust the function below michael@0: q = (int)(pow(vp9_dc_quant(qindex, 0) / 4.0, RD_THRESH_POW) * 5.12); michael@0: if (q < 8) michael@0: q = 8; michael@0: return q; michael@0: } michael@0: michael@0: void vp9_initialize_me_consts(VP9_COMP *cpi, int qindex) { michael@0: cpi->mb.sadperbit16 = sad_per_bit16lut[qindex]; michael@0: cpi->mb.sadperbit4 = sad_per_bit4lut[qindex]; michael@0: } michael@0: michael@0: static void set_block_thresholds(VP9_COMP *cpi) { michael@0: int i, bsize, segment_id; michael@0: VP9_COMMON *cm = &cpi->common; michael@0: michael@0: for (segment_id = 0; segment_id < MAX_SEGMENTS; ++segment_id) { michael@0: int q; michael@0: int segment_qindex = vp9_get_qindex(&cm->seg, segment_id, cm->base_qindex); michael@0: segment_qindex = clamp(segment_qindex + cm->y_dc_delta_q, 0, MAXQ); michael@0: q = compute_rd_thresh_factor(segment_qindex); michael@0: michael@0: for (bsize = 0; bsize < BLOCK_SIZES; ++bsize) { michael@0: // Threshold here seem unecessarily harsh but fine given actual michael@0: // range of values used for cpi->sf.thresh_mult[] michael@0: int thresh_max = INT_MAX / (q * rd_thresh_block_size_factor[bsize]); michael@0: michael@0: for (i = 0; i < MAX_MODES; ++i) { michael@0: if (cpi->sf.thresh_mult[i] < thresh_max) { michael@0: cpi->rd_threshes[segment_id][bsize][i] = michael@0: cpi->sf.thresh_mult[i] * q * michael@0: rd_thresh_block_size_factor[bsize] / 4; michael@0: } else { michael@0: cpi->rd_threshes[segment_id][bsize][i] = INT_MAX; michael@0: } michael@0: } michael@0: michael@0: for (i = 0; i < MAX_REFS; ++i) { michael@0: if (cpi->sf.thresh_mult_sub8x8[i] < thresh_max) { michael@0: cpi->rd_thresh_sub8x8[segment_id][bsize][i] = michael@0: cpi->sf.thresh_mult_sub8x8[i] * q * michael@0: rd_thresh_block_size_factor[bsize] / 4; michael@0: } else { michael@0: cpi->rd_thresh_sub8x8[segment_id][bsize][i] = INT_MAX; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void vp9_initialize_rd_consts(VP9_COMP *cpi) { michael@0: VP9_COMMON *cm = &cpi->common; michael@0: int qindex, i; michael@0: michael@0: vp9_clear_system_state(); // __asm emms; 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: // if (cpi->common.refresh_golden_frame || michael@0: // cpi->common.refresh_alt_ref_frame) michael@0: qindex = clamp(cm->base_qindex + cm->y_dc_delta_q, 0, MAXQ); michael@0: michael@0: cpi->RDDIV = RDDIV_BITS; // in bits (to multiply D by 128) michael@0: cpi->RDMULT = vp9_compute_rd_mult(cpi, qindex); michael@0: michael@0: cpi->mb.errorperbit = cpi->RDMULT / RD_MULT_EPB_RATIO; michael@0: cpi->mb.errorperbit += (cpi->mb.errorperbit == 0); michael@0: michael@0: vp9_set_speed_features(cpi); michael@0: michael@0: cpi->mb.select_txfm_size = (cpi->sf.tx_size_search_method == USE_LARGESTALL && michael@0: cm->frame_type != KEY_FRAME) ? michael@0: 0 : 1; michael@0: michael@0: set_block_thresholds(cpi); michael@0: michael@0: fill_token_costs(cpi->mb.token_costs, cm->fc.coef_probs); michael@0: michael@0: for (i = 0; i < PARTITION_CONTEXTS; i++) michael@0: vp9_cost_tokens(cpi->mb.partition_cost[i], get_partition_probs(cm, i), michael@0: vp9_partition_tree); michael@0: michael@0: /*rough estimate for costing*/ michael@0: vp9_init_mode_costs(cpi); michael@0: michael@0: if (!frame_is_intra_only(cm)) { michael@0: vp9_build_nmv_cost_table( michael@0: cpi->mb.nmvjointcost, michael@0: cm->allow_high_precision_mv ? cpi->mb.nmvcost_hp : cpi->mb.nmvcost, michael@0: &cm->fc.nmvc, michael@0: cm->allow_high_precision_mv, 1, 1); michael@0: michael@0: for (i = 0; i < INTER_MODE_CONTEXTS; i++) { michael@0: MB_PREDICTION_MODE m; michael@0: michael@0: for (m = NEARESTMV; m < MB_MODE_COUNT; m++) michael@0: cpi->mb.inter_mode_cost[i][INTER_OFFSET(m)] = michael@0: cost_token(vp9_inter_mode_tree, michael@0: cm->fc.inter_mode_probs[i], michael@0: &vp9_inter_mode_encodings[INTER_OFFSET(m)]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static INLINE void linear_interpolate2(double x, int ntab, int inv_step, michael@0: const double *tab1, const double *tab2, michael@0: double *v1, double *v2) { michael@0: double y = x * inv_step; michael@0: int d = (int) y; michael@0: if (d >= ntab - 1) { michael@0: *v1 = tab1[ntab - 1]; michael@0: *v2 = tab2[ntab - 1]; michael@0: } else { michael@0: double a = y - d; michael@0: *v1 = tab1[d] * (1 - a) + tab1[d + 1] * a; michael@0: *v2 = tab2[d] * (1 - a) + tab2[d + 1] * a; michael@0: } michael@0: } michael@0: michael@0: static void model_rd_norm(double x, double *R, double *D) { michael@0: static const int inv_tab_step = 8; michael@0: static const int tab_size = 120; michael@0: // NOTE: The tables below must be of the same size michael@0: // michael@0: // Normalized rate michael@0: // This table models the rate for a Laplacian source michael@0: // source with given variance when quantized with a uniform quantizer michael@0: // with given stepsize. The closed form expression is: michael@0: // Rn(x) = H(sqrt(r)) + sqrt(r)*[1 + H(r)/(1 - r)], michael@0: // where r = exp(-sqrt(2) * x) and x = qpstep / sqrt(variance), michael@0: // and H(x) is the binary entropy function. michael@0: static const double rate_tab[] = { michael@0: 64.00, 4.944, 3.949, 3.372, 2.966, 2.655, 2.403, 2.194, michael@0: 2.014, 1.858, 1.720, 1.596, 1.485, 1.384, 1.291, 1.206, michael@0: 1.127, 1.054, 0.986, 0.923, 0.863, 0.808, 0.756, 0.708, michael@0: 0.662, 0.619, 0.579, 0.541, 0.506, 0.473, 0.442, 0.412, michael@0: 0.385, 0.359, 0.335, 0.313, 0.291, 0.272, 0.253, 0.236, michael@0: 0.220, 0.204, 0.190, 0.177, 0.165, 0.153, 0.142, 0.132, michael@0: 0.123, 0.114, 0.106, 0.099, 0.091, 0.085, 0.079, 0.073, michael@0: 0.068, 0.063, 0.058, 0.054, 0.050, 0.047, 0.043, 0.040, michael@0: 0.037, 0.034, 0.032, 0.029, 0.027, 0.025, 0.023, 0.022, michael@0: 0.020, 0.019, 0.017, 0.016, 0.015, 0.014, 0.013, 0.012, michael@0: 0.011, 0.010, 0.009, 0.008, 0.008, 0.007, 0.007, 0.006, michael@0: 0.006, 0.005, 0.005, 0.005, 0.004, 0.004, 0.004, 0.003, michael@0: 0.003, 0.003, 0.003, 0.002, 0.002, 0.002, 0.002, 0.002, michael@0: 0.002, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, michael@0: 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.000, michael@0: }; michael@0: // Normalized distortion michael@0: // This table models the normalized distortion for a Laplacian source michael@0: // source with given variance when quantized with a uniform quantizer michael@0: // with given stepsize. The closed form expression is: michael@0: // Dn(x) = 1 - 1/sqrt(2) * x / sinh(x/sqrt(2)) michael@0: // where x = qpstep / sqrt(variance) michael@0: // Note the actual distortion is Dn * variance. michael@0: static const double dist_tab[] = { michael@0: 0.000, 0.001, 0.005, 0.012, 0.021, 0.032, 0.045, 0.061, michael@0: 0.079, 0.098, 0.119, 0.142, 0.166, 0.190, 0.216, 0.242, michael@0: 0.269, 0.296, 0.324, 0.351, 0.378, 0.405, 0.432, 0.458, michael@0: 0.484, 0.509, 0.534, 0.557, 0.580, 0.603, 0.624, 0.645, michael@0: 0.664, 0.683, 0.702, 0.719, 0.735, 0.751, 0.766, 0.780, michael@0: 0.794, 0.807, 0.819, 0.830, 0.841, 0.851, 0.861, 0.870, michael@0: 0.878, 0.886, 0.894, 0.901, 0.907, 0.913, 0.919, 0.925, michael@0: 0.930, 0.935, 0.939, 0.943, 0.947, 0.951, 0.954, 0.957, michael@0: 0.960, 0.963, 0.966, 0.968, 0.971, 0.973, 0.975, 0.976, michael@0: 0.978, 0.980, 0.981, 0.982, 0.984, 0.985, 0.986, 0.987, michael@0: 0.988, 0.989, 0.990, 0.990, 0.991, 0.992, 0.992, 0.993, michael@0: 0.993, 0.994, 0.994, 0.995, 0.995, 0.996, 0.996, 0.996, michael@0: 0.996, 0.997, 0.997, 0.997, 0.997, 0.998, 0.998, 0.998, michael@0: 0.998, 0.998, 0.998, 0.999, 0.999, 0.999, 0.999, 0.999, michael@0: 0.999, 0.999, 0.999, 0.999, 0.999, 0.999, 0.999, 1.000, michael@0: }; michael@0: /* michael@0: assert(sizeof(rate_tab) == tab_size * sizeof(rate_tab[0]); michael@0: assert(sizeof(dist_tab) == tab_size * sizeof(dist_tab[0]); michael@0: assert(sizeof(rate_tab) == sizeof(dist_tab)); michael@0: */ michael@0: assert(x >= 0.0); michael@0: linear_interpolate2(x, tab_size, inv_tab_step, michael@0: rate_tab, dist_tab, R, D); michael@0: } michael@0: michael@0: static void model_rd_from_var_lapndz(int var, int n, int qstep, michael@0: int *rate, int64_t *dist) { michael@0: // This function models the rate and distortion for a Laplacian michael@0: // source with given variance when quantized with a uniform quantizer michael@0: // with given stepsize. The closed form expressions are in: michael@0: // Hang and Chen, "Source Model for transform video coder and its michael@0: // application - Part I: Fundamental Theory", IEEE Trans. Circ. michael@0: // Sys. for Video Tech., April 1997. michael@0: vp9_clear_system_state(); michael@0: if (var == 0 || n == 0) { michael@0: *rate = 0; michael@0: *dist = 0; michael@0: } else { michael@0: double D, R; michael@0: double s2 = (double) var / n; michael@0: double x = qstep / sqrt(s2); michael@0: model_rd_norm(x, &R, &D); michael@0: *rate = (int)((n << 8) * R + 0.5); michael@0: *dist = (int)(var * D + 0.5); michael@0: } michael@0: vp9_clear_system_state(); michael@0: } michael@0: michael@0: static void model_rd_for_sb(VP9_COMP *cpi, BLOCK_SIZE bsize, michael@0: MACROBLOCK *x, MACROBLOCKD *xd, michael@0: int *out_rate_sum, int64_t *out_dist_sum) { michael@0: // Note our transform coeffs are 8 times an orthogonal transform. michael@0: // Hence quantizer step is also 8 times. To get effective quantizer michael@0: // we need to divide by 8 before sending to modeling function. michael@0: int i, rate_sum = 0, dist_sum = 0; michael@0: michael@0: for (i = 0; i < MAX_MB_PLANE; ++i) { michael@0: struct macroblock_plane *const p = &x->plane[i]; michael@0: struct macroblockd_plane *const pd = &xd->plane[i]; michael@0: const BLOCK_SIZE bs = get_plane_block_size(bsize, pd); michael@0: unsigned int sse; michael@0: int rate; michael@0: int64_t dist; michael@0: (void) cpi->fn_ptr[bs].vf(p->src.buf, p->src.stride, michael@0: pd->dst.buf, pd->dst.stride, &sse); michael@0: // sse works better than var, since there is no dc prediction used michael@0: model_rd_from_var_lapndz(sse, 1 << num_pels_log2_lookup[bs], michael@0: pd->dequant[1] >> 3, &rate, &dist); michael@0: michael@0: rate_sum += rate; michael@0: dist_sum += (int)dist; michael@0: } michael@0: michael@0: *out_rate_sum = rate_sum; michael@0: *out_dist_sum = dist_sum << 4; michael@0: } michael@0: michael@0: static void model_rd_for_sb_y_tx(VP9_COMP *cpi, BLOCK_SIZE bsize, michael@0: TX_SIZE tx_size, michael@0: MACROBLOCK *x, MACROBLOCKD *xd, michael@0: int *out_rate_sum, int64_t *out_dist_sum, michael@0: int *out_skip) { michael@0: int j, k; michael@0: BLOCK_SIZE bs; michael@0: struct macroblock_plane *const p = &x->plane[0]; michael@0: struct macroblockd_plane *const pd = &xd->plane[0]; michael@0: const int width = 4 << num_4x4_blocks_wide_lookup[bsize]; michael@0: const int height = 4 << num_4x4_blocks_high_lookup[bsize]; michael@0: int rate_sum = 0; michael@0: int64_t dist_sum = 0; michael@0: const int t = 4 << tx_size; michael@0: michael@0: if (tx_size == TX_4X4) { michael@0: bs = BLOCK_4X4; michael@0: } else if (tx_size == TX_8X8) { michael@0: bs = BLOCK_8X8; michael@0: } else if (tx_size == TX_16X16) { michael@0: bs = BLOCK_16X16; michael@0: } else if (tx_size == TX_32X32) { michael@0: bs = BLOCK_32X32; michael@0: } else { michael@0: assert(0); michael@0: } michael@0: michael@0: *out_skip = 1; michael@0: for (j = 0; j < height; j += t) { michael@0: for (k = 0; k < width; k += t) { michael@0: int rate; michael@0: int64_t dist; michael@0: unsigned int sse; michael@0: cpi->fn_ptr[bs].vf(&p->src.buf[j * p->src.stride + k], p->src.stride, michael@0: &pd->dst.buf[j * pd->dst.stride + k], pd->dst.stride, michael@0: &sse); michael@0: // sse works better than var, since there is no dc prediction used michael@0: model_rd_from_var_lapndz(sse, t * t, pd->dequant[1] >> 3, &rate, &dist); michael@0: rate_sum += rate; michael@0: dist_sum += dist; michael@0: *out_skip &= (rate < 1024); michael@0: } michael@0: } michael@0: michael@0: *out_rate_sum = rate_sum; michael@0: *out_dist_sum = dist_sum << 4; michael@0: } michael@0: michael@0: int64_t vp9_block_error_c(int16_t *coeff, int16_t *dqcoeff, michael@0: intptr_t block_size, int64_t *ssz) { michael@0: int i; michael@0: int64_t error = 0, sqcoeff = 0; michael@0: michael@0: for (i = 0; i < block_size; i++) { michael@0: int this_diff = coeff[i] - dqcoeff[i]; michael@0: error += (unsigned)this_diff * this_diff; michael@0: sqcoeff += (unsigned) coeff[i] * coeff[i]; michael@0: } michael@0: michael@0: *ssz = sqcoeff; michael@0: return error; michael@0: } michael@0: michael@0: /* The trailing '0' is a terminator which is used inside cost_coeffs() to michael@0: * decide whether to include cost of a trailing EOB node or not (i.e. we michael@0: * can skip this if the last coefficient in this transform block, e.g. the michael@0: * 16th coefficient in a 4x4 block or the 64th coefficient in a 8x8 block, michael@0: * were non-zero). */ michael@0: static const int16_t band_counts[TX_SIZES][8] = { michael@0: { 1, 2, 3, 4, 3, 16 - 13, 0 }, michael@0: { 1, 2, 3, 4, 11, 64 - 21, 0 }, michael@0: { 1, 2, 3, 4, 11, 256 - 21, 0 }, michael@0: { 1, 2, 3, 4, 11, 1024 - 21, 0 }, michael@0: }; michael@0: michael@0: static INLINE int cost_coeffs(MACROBLOCK *x, michael@0: int plane, int block, michael@0: ENTROPY_CONTEXT *A, ENTROPY_CONTEXT *L, michael@0: TX_SIZE tx_size, michael@0: const int16_t *scan, const int16_t *nb) { michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; michael@0: struct macroblockd_plane *pd = &xd->plane[plane]; michael@0: const PLANE_TYPE type = pd->plane_type; michael@0: const int16_t *band_count = &band_counts[tx_size][1]; michael@0: const int eob = pd->eobs[block]; michael@0: const int16_t *const qcoeff_ptr = BLOCK_OFFSET(pd->qcoeff, block); michael@0: const int ref = mbmi->ref_frame[0] != INTRA_FRAME; michael@0: unsigned int (*token_costs)[2][PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS] = michael@0: x->token_costs[tx_size][type][ref]; michael@0: const ENTROPY_CONTEXT above_ec = !!*A, left_ec = !!*L; michael@0: uint8_t *p_tok = x->token_cache; michael@0: int pt = combine_entropy_contexts(above_ec, left_ec); michael@0: int c, cost; michael@0: michael@0: // Check for consistency of tx_size with mode info michael@0: assert(type == PLANE_TYPE_Y_WITH_DC ? mbmi->tx_size == tx_size michael@0: : get_uv_tx_size(mbmi) == tx_size); michael@0: michael@0: if (eob == 0) { michael@0: // single eob token michael@0: cost = token_costs[0][0][pt][DCT_EOB_TOKEN]; michael@0: c = 0; michael@0: } else { michael@0: int band_left = *band_count++; michael@0: michael@0: // dc token michael@0: int v = qcoeff_ptr[0]; michael@0: int prev_t = vp9_dct_value_tokens_ptr[v].token; michael@0: cost = (*token_costs)[0][pt][prev_t] + vp9_dct_value_cost_ptr[v]; michael@0: p_tok[0] = vp9_pt_energy_class[prev_t]; michael@0: ++token_costs; michael@0: michael@0: // ac tokens michael@0: for (c = 1; c < eob; c++) { michael@0: const int rc = scan[c]; michael@0: int t; michael@0: michael@0: v = qcoeff_ptr[rc]; michael@0: t = vp9_dct_value_tokens_ptr[v].token; michael@0: pt = get_coef_context(nb, p_tok, c); michael@0: cost += (*token_costs)[!prev_t][pt][t] + vp9_dct_value_cost_ptr[v]; michael@0: p_tok[rc] = vp9_pt_energy_class[t]; michael@0: prev_t = t; michael@0: if (!--band_left) { michael@0: band_left = *band_count++; michael@0: ++token_costs; michael@0: } michael@0: } michael@0: michael@0: // eob token michael@0: if (band_left) { michael@0: pt = get_coef_context(nb, p_tok, c); michael@0: cost += (*token_costs)[0][pt][DCT_EOB_TOKEN]; michael@0: } michael@0: } michael@0: michael@0: // is eob first coefficient; michael@0: *A = *L = (c > 0); michael@0: michael@0: return cost; michael@0: } michael@0: michael@0: static void dist_block(int plane, int block, TX_SIZE tx_size, void *arg) { michael@0: const int ss_txfrm_size = tx_size << 1; michael@0: struct rdcost_block_args* args = arg; michael@0: MACROBLOCK* const x = args->x; michael@0: MACROBLOCKD* const xd = &x->e_mbd; michael@0: struct macroblock_plane *const p = &x->plane[plane]; michael@0: struct macroblockd_plane *const pd = &xd->plane[plane]; michael@0: int64_t this_sse; michael@0: int shift = args->tx_size == TX_32X32 ? 0 : 2; michael@0: int16_t *const coeff = BLOCK_OFFSET(p->coeff, block); michael@0: int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block); michael@0: args->dist = vp9_block_error(coeff, dqcoeff, 16 << ss_txfrm_size, michael@0: &this_sse) >> shift; michael@0: args->sse = this_sse >> shift; michael@0: michael@0: if (x->skip_encode && michael@0: xd->mi_8x8[0]->mbmi.ref_frame[0] == INTRA_FRAME) { michael@0: // TODO(jingning): tune the model to better capture the distortion. michael@0: int64_t p = (pd->dequant[1] * pd->dequant[1] * michael@0: (1 << ss_txfrm_size)) >> (shift + 2); michael@0: args->dist += (p >> 4); michael@0: args->sse += p; michael@0: } michael@0: } michael@0: michael@0: static void rate_block(int plane, int block, BLOCK_SIZE plane_bsize, michael@0: TX_SIZE tx_size, void *arg) { michael@0: struct rdcost_block_args* args = arg; michael@0: michael@0: int x_idx, y_idx; michael@0: txfrm_block_to_raster_xy(plane_bsize, args->tx_size, block, &x_idx, &y_idx); michael@0: michael@0: args->rate = cost_coeffs(args->x, plane, block, args->t_above + x_idx, michael@0: args->t_left + y_idx, args->tx_size, michael@0: args->scan, args->nb); michael@0: } michael@0: michael@0: static void block_yrd_txfm(int plane, int block, BLOCK_SIZE plane_bsize, michael@0: TX_SIZE tx_size, void *arg) { michael@0: struct rdcost_block_args *args = arg; michael@0: MACROBLOCK *const x = args->x; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: struct encode_b_args encode_args = {x, NULL}; michael@0: int64_t rd1, rd2, rd; michael@0: michael@0: if (args->skip) michael@0: return; michael@0: michael@0: if (!is_inter_block(&xd->mi_8x8[0]->mbmi)) michael@0: vp9_encode_block_intra(plane, block, plane_bsize, tx_size, &encode_args); michael@0: else michael@0: vp9_xform_quant(plane, block, plane_bsize, tx_size, &encode_args); michael@0: michael@0: dist_block(plane, block, tx_size, args); michael@0: rate_block(plane, block, plane_bsize, tx_size, args); michael@0: rd1 = RDCOST(x->rdmult, x->rddiv, args->rate, args->dist); michael@0: rd2 = RDCOST(x->rdmult, x->rddiv, 0, args->sse); michael@0: michael@0: // TODO(jingning): temporarily enabled only for luma component michael@0: rd = MIN(rd1, rd2); michael@0: if (!xd->lossless && plane == 0) michael@0: x->zcoeff_blk[tx_size][block] = rd1 > rd2 || !xd->plane[plane].eobs[block]; michael@0: michael@0: args->this_rate += args->rate; michael@0: args->this_dist += args->dist; michael@0: args->this_sse += args->sse; michael@0: args->this_rd += rd; michael@0: michael@0: if (args->this_rd > args->best_rd) { michael@0: args->skip = 1; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: void vp9_get_entropy_contexts(TX_SIZE tx_size, michael@0: ENTROPY_CONTEXT t_above[16], ENTROPY_CONTEXT t_left[16], michael@0: const ENTROPY_CONTEXT *above, const ENTROPY_CONTEXT *left, michael@0: int num_4x4_w, int num_4x4_h) { michael@0: int i; michael@0: switch (tx_size) { michael@0: case TX_4X4: michael@0: vpx_memcpy(t_above, above, sizeof(ENTROPY_CONTEXT) * num_4x4_w); michael@0: vpx_memcpy(t_left, left, sizeof(ENTROPY_CONTEXT) * num_4x4_h); michael@0: break; michael@0: case TX_8X8: michael@0: for (i = 0; i < num_4x4_w; i += 2) michael@0: t_above[i] = !!*(const uint16_t *)&above[i]; michael@0: for (i = 0; i < num_4x4_h; i += 2) michael@0: t_left[i] = !!*(const uint16_t *)&left[i]; michael@0: break; michael@0: case TX_16X16: michael@0: for (i = 0; i < num_4x4_w; i += 4) michael@0: t_above[i] = !!*(const uint32_t *)&above[i]; michael@0: for (i = 0; i < num_4x4_h; i += 4) michael@0: t_left[i] = !!*(const uint32_t *)&left[i]; michael@0: break; michael@0: case TX_32X32: michael@0: for (i = 0; i < num_4x4_w; i += 8) michael@0: t_above[i] = !!*(const uint64_t *)&above[i]; michael@0: for (i = 0; i < num_4x4_h; i += 8) michael@0: t_left[i] = !!*(const uint64_t *)&left[i]; michael@0: break; michael@0: default: michael@0: assert(!"Invalid transform size."); michael@0: } michael@0: } michael@0: michael@0: static void init_rdcost_stack(MACROBLOCK *x, TX_SIZE tx_size, michael@0: const int num_4x4_w, const int num_4x4_h, michael@0: const int64_t ref_rdcost, michael@0: struct rdcost_block_args *arg) { michael@0: vpx_memset(arg, 0, sizeof(struct rdcost_block_args)); michael@0: arg->x = x; michael@0: arg->tx_size = tx_size; michael@0: arg->bw = num_4x4_w; michael@0: arg->bh = num_4x4_h; michael@0: arg->best_rd = ref_rdcost; michael@0: } michael@0: michael@0: static void txfm_rd_in_plane(MACROBLOCK *x, michael@0: struct rdcost_block_args *rd_stack, michael@0: int *rate, int64_t *distortion, michael@0: int *skippable, int64_t *sse, michael@0: int64_t ref_best_rd, int plane, michael@0: BLOCK_SIZE bsize, TX_SIZE tx_size) { michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: struct macroblockd_plane *const pd = &xd->plane[plane]; michael@0: const BLOCK_SIZE bs = get_plane_block_size(bsize, pd); michael@0: const int num_4x4_w = num_4x4_blocks_wide_lookup[bs]; michael@0: const int num_4x4_h = num_4x4_blocks_high_lookup[bs]; michael@0: michael@0: init_rdcost_stack(x, tx_size, num_4x4_w, num_4x4_h, michael@0: ref_best_rd, rd_stack); michael@0: if (plane == 0) michael@0: xd->mi_8x8[0]->mbmi.tx_size = tx_size; michael@0: michael@0: vp9_get_entropy_contexts(tx_size, rd_stack->t_above, rd_stack->t_left, michael@0: pd->above_context, pd->left_context, michael@0: num_4x4_w, num_4x4_h); michael@0: michael@0: get_scan(xd, tx_size, pd->plane_type, 0, &rd_stack->scan, &rd_stack->nb); michael@0: michael@0: foreach_transformed_block_in_plane(xd, bsize, plane, michael@0: block_yrd_txfm, rd_stack); michael@0: if (rd_stack->skip) { michael@0: *rate = INT_MAX; michael@0: *distortion = INT64_MAX; michael@0: *sse = INT64_MAX; michael@0: *skippable = 0; michael@0: } else { michael@0: *distortion = rd_stack->this_dist; michael@0: *rate = rd_stack->this_rate; michael@0: *sse = rd_stack->this_sse; michael@0: *skippable = vp9_is_skippable_in_plane(xd, bsize, plane); michael@0: } michael@0: } michael@0: michael@0: static void choose_largest_txfm_size(VP9_COMP *cpi, MACROBLOCK *x, michael@0: int *rate, int64_t *distortion, michael@0: int *skip, int64_t *sse, michael@0: int64_t ref_best_rd, michael@0: BLOCK_SIZE bs) { michael@0: const TX_SIZE max_tx_size = max_txsize_lookup[bs]; michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: const TX_SIZE largest_tx_size = tx_mode_to_biggest_tx_size[cm->tx_mode]; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; michael@0: michael@0: mbmi->tx_size = MIN(max_tx_size, largest_tx_size); michael@0: michael@0: txfm_rd_in_plane(x, &cpi->rdcost_stack, rate, distortion, skip, michael@0: &sse[mbmi->tx_size], ref_best_rd, 0, bs, michael@0: mbmi->tx_size); michael@0: cpi->tx_stepdown_count[0]++; michael@0: } michael@0: michael@0: static void choose_txfm_size_from_rd(VP9_COMP *cpi, MACROBLOCK *x, michael@0: int (*r)[2], int *rate, michael@0: int64_t *d, int64_t *distortion, michael@0: int *s, int *skip, michael@0: int64_t tx_cache[TX_MODES], michael@0: BLOCK_SIZE bs) { michael@0: const TX_SIZE max_tx_size = max_txsize_lookup[bs]; michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; michael@0: vp9_prob skip_prob = vp9_get_pred_prob_mbskip(cm, xd); michael@0: int64_t rd[TX_SIZES][2]; michael@0: int n, m; michael@0: int s0, s1; michael@0: michael@0: const vp9_prob *tx_probs = get_tx_probs2(max_tx_size, xd, &cm->fc.tx_probs); michael@0: michael@0: for (n = TX_4X4; n <= max_tx_size; n++) { michael@0: r[n][1] = r[n][0]; michael@0: if (r[n][0] == INT_MAX) michael@0: continue; michael@0: for (m = 0; m <= n - (n == max_tx_size); m++) { michael@0: if (m == n) michael@0: r[n][1] += vp9_cost_zero(tx_probs[m]); michael@0: else michael@0: r[n][1] += vp9_cost_one(tx_probs[m]); michael@0: } michael@0: } michael@0: michael@0: assert(skip_prob > 0); michael@0: s0 = vp9_cost_bit(skip_prob, 0); michael@0: s1 = vp9_cost_bit(skip_prob, 1); michael@0: michael@0: for (n = TX_4X4; n <= max_tx_size; n++) { michael@0: if (d[n] == INT64_MAX) { michael@0: rd[n][0] = rd[n][1] = INT64_MAX; michael@0: continue; michael@0: } michael@0: if (s[n]) { michael@0: rd[n][0] = rd[n][1] = RDCOST(x->rdmult, x->rddiv, s1, d[n]); michael@0: } else { michael@0: rd[n][0] = RDCOST(x->rdmult, x->rddiv, r[n][0] + s0, d[n]); michael@0: rd[n][1] = RDCOST(x->rdmult, x->rddiv, r[n][1] + s0, d[n]); michael@0: } michael@0: } michael@0: michael@0: if (max_tx_size == TX_32X32 && michael@0: (cm->tx_mode == ALLOW_32X32 || michael@0: (cm->tx_mode == TX_MODE_SELECT && michael@0: rd[TX_32X32][1] < rd[TX_16X16][1] && rd[TX_32X32][1] < rd[TX_8X8][1] && michael@0: rd[TX_32X32][1] < rd[TX_4X4][1]))) { michael@0: mbmi->tx_size = TX_32X32; michael@0: } else if (max_tx_size >= TX_16X16 && michael@0: (cm->tx_mode == ALLOW_16X16 || michael@0: cm->tx_mode == ALLOW_32X32 || michael@0: (cm->tx_mode == TX_MODE_SELECT && michael@0: rd[TX_16X16][1] < rd[TX_8X8][1] && michael@0: rd[TX_16X16][1] < rd[TX_4X4][1]))) { michael@0: mbmi->tx_size = TX_16X16; michael@0: } else if (cm->tx_mode == ALLOW_8X8 || michael@0: cm->tx_mode == ALLOW_16X16 || michael@0: cm->tx_mode == ALLOW_32X32 || michael@0: (cm->tx_mode == TX_MODE_SELECT && rd[TX_8X8][1] < rd[TX_4X4][1])) { michael@0: mbmi->tx_size = TX_8X8; michael@0: } else { michael@0: mbmi->tx_size = TX_4X4; michael@0: } michael@0: michael@0: *distortion = d[mbmi->tx_size]; michael@0: *rate = r[mbmi->tx_size][cm->tx_mode == TX_MODE_SELECT]; michael@0: *skip = s[mbmi->tx_size]; michael@0: michael@0: tx_cache[ONLY_4X4] = rd[TX_4X4][0]; michael@0: tx_cache[ALLOW_8X8] = rd[TX_8X8][0]; michael@0: tx_cache[ALLOW_16X16] = rd[MIN(max_tx_size, TX_16X16)][0]; michael@0: tx_cache[ALLOW_32X32] = rd[MIN(max_tx_size, TX_32X32)][0]; michael@0: if (max_tx_size == TX_32X32 && michael@0: rd[TX_32X32][1] < rd[TX_16X16][1] && rd[TX_32X32][1] < rd[TX_8X8][1] && michael@0: rd[TX_32X32][1] < rd[TX_4X4][1]) michael@0: tx_cache[TX_MODE_SELECT] = rd[TX_32X32][1]; michael@0: else if (max_tx_size >= TX_16X16 && michael@0: rd[TX_16X16][1] < rd[TX_8X8][1] && rd[TX_16X16][1] < rd[TX_4X4][1]) michael@0: tx_cache[TX_MODE_SELECT] = rd[TX_16X16][1]; michael@0: else michael@0: tx_cache[TX_MODE_SELECT] = rd[TX_4X4][1] < rd[TX_8X8][1] ? michael@0: rd[TX_4X4][1] : rd[TX_8X8][1]; michael@0: michael@0: if (max_tx_size == TX_32X32 && michael@0: rd[TX_32X32][1] < rd[TX_16X16][1] && michael@0: rd[TX_32X32][1] < rd[TX_8X8][1] && michael@0: rd[TX_32X32][1] < rd[TX_4X4][1]) { michael@0: cpi->tx_stepdown_count[0]++; michael@0: } else if (max_tx_size >= TX_16X16 && michael@0: rd[TX_16X16][1] < rd[TX_8X8][1] && michael@0: rd[TX_16X16][1] < rd[TX_4X4][1]) { michael@0: cpi->tx_stepdown_count[max_tx_size - TX_16X16]++; michael@0: } else if (rd[TX_8X8][1] < rd[TX_4X4][1]) { michael@0: cpi->tx_stepdown_count[max_tx_size - TX_8X8]++; michael@0: } else { michael@0: cpi->tx_stepdown_count[max_tx_size - TX_4X4]++; michael@0: } michael@0: } michael@0: michael@0: static void choose_txfm_size_from_modelrd(VP9_COMP *cpi, MACROBLOCK *x, michael@0: int (*r)[2], int *rate, michael@0: int64_t *d, int64_t *distortion, michael@0: int *s, int *skip, int64_t *sse, michael@0: int64_t ref_best_rd, michael@0: BLOCK_SIZE bs) { michael@0: const TX_SIZE max_tx_size = max_txsize_lookup[bs]; michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; michael@0: vp9_prob skip_prob = vp9_get_pred_prob_mbskip(cm, xd); michael@0: int64_t rd[TX_SIZES][2]; michael@0: int n, m; michael@0: int s0, s1; michael@0: double scale_rd[TX_SIZES] = {1.73, 1.44, 1.20, 1.00}; michael@0: // double scale_r[TX_SIZES] = {2.82, 2.00, 1.41, 1.00}; michael@0: michael@0: const vp9_prob *tx_probs = get_tx_probs2(max_tx_size, xd, &cm->fc.tx_probs); michael@0: michael@0: // for (n = TX_4X4; n <= max_txfm_size; n++) michael@0: // r[n][0] = (r[n][0] * scale_r[n]); michael@0: michael@0: for (n = TX_4X4; n <= max_tx_size; n++) { michael@0: r[n][1] = r[n][0]; michael@0: for (m = 0; m <= n - (n == max_tx_size); m++) { michael@0: if (m == n) michael@0: r[n][1] += vp9_cost_zero(tx_probs[m]); michael@0: else michael@0: r[n][1] += vp9_cost_one(tx_probs[m]); michael@0: } michael@0: } michael@0: michael@0: assert(skip_prob > 0); michael@0: s0 = vp9_cost_bit(skip_prob, 0); michael@0: s1 = vp9_cost_bit(skip_prob, 1); michael@0: michael@0: for (n = TX_4X4; n <= max_tx_size; n++) { michael@0: if (s[n]) { michael@0: rd[n][0] = rd[n][1] = RDCOST(x->rdmult, x->rddiv, s1, d[n]); michael@0: } else { michael@0: rd[n][0] = RDCOST(x->rdmult, x->rddiv, r[n][0] + s0, d[n]); michael@0: rd[n][1] = RDCOST(x->rdmult, x->rddiv, r[n][1] + s0, d[n]); michael@0: } michael@0: } michael@0: for (n = TX_4X4; n <= max_tx_size; n++) { michael@0: rd[n][0] = (int64_t)(scale_rd[n] * rd[n][0]); michael@0: rd[n][1] = (int64_t)(scale_rd[n] * rd[n][1]); michael@0: } michael@0: michael@0: if (max_tx_size == TX_32X32 && michael@0: (cm->tx_mode == ALLOW_32X32 || michael@0: (cm->tx_mode == TX_MODE_SELECT && michael@0: rd[TX_32X32][1] <= rd[TX_16X16][1] && michael@0: rd[TX_32X32][1] <= rd[TX_8X8][1] && michael@0: rd[TX_32X32][1] <= rd[TX_4X4][1]))) { michael@0: mbmi->tx_size = TX_32X32; michael@0: } else if (max_tx_size >= TX_16X16 && michael@0: (cm->tx_mode == ALLOW_16X16 || michael@0: cm->tx_mode == ALLOW_32X32 || michael@0: (cm->tx_mode == TX_MODE_SELECT && michael@0: rd[TX_16X16][1] <= rd[TX_8X8][1] && michael@0: rd[TX_16X16][1] <= rd[TX_4X4][1]))) { michael@0: mbmi->tx_size = TX_16X16; michael@0: } else if (cm->tx_mode == ALLOW_8X8 || michael@0: cm->tx_mode == ALLOW_16X16 || michael@0: cm->tx_mode == ALLOW_32X32 || michael@0: (cm->tx_mode == TX_MODE_SELECT && michael@0: rd[TX_8X8][1] <= rd[TX_4X4][1])) { michael@0: mbmi->tx_size = TX_8X8; michael@0: } else { michael@0: mbmi->tx_size = TX_4X4; michael@0: } michael@0: michael@0: // Actually encode using the chosen mode if a model was used, but do not michael@0: // update the r, d costs michael@0: txfm_rd_in_plane(x, &cpi->rdcost_stack, rate, distortion, skip, michael@0: &sse[mbmi->tx_size], ref_best_rd, 0, bs, mbmi->tx_size); michael@0: michael@0: if (max_tx_size == TX_32X32 && michael@0: rd[TX_32X32][1] <= rd[TX_16X16][1] && michael@0: rd[TX_32X32][1] <= rd[TX_8X8][1] && michael@0: rd[TX_32X32][1] <= rd[TX_4X4][1]) { michael@0: cpi->tx_stepdown_count[0]++; michael@0: } else if (max_tx_size >= TX_16X16 && michael@0: rd[TX_16X16][1] <= rd[TX_8X8][1] && michael@0: rd[TX_16X16][1] <= rd[TX_4X4][1]) { michael@0: cpi->tx_stepdown_count[max_tx_size - TX_16X16]++; michael@0: } else if (rd[TX_8X8][1] <= rd[TX_4X4][1]) { michael@0: cpi->tx_stepdown_count[max_tx_size - TX_8X8]++; michael@0: } else { michael@0: cpi->tx_stepdown_count[max_tx_size - TX_4X4]++; michael@0: } michael@0: } michael@0: michael@0: static void super_block_yrd(VP9_COMP *cpi, michael@0: MACROBLOCK *x, int *rate, int64_t *distortion, michael@0: int *skip, int64_t *psse, BLOCK_SIZE bs, michael@0: int64_t txfm_cache[TX_MODES], michael@0: int64_t ref_best_rd) { michael@0: int r[TX_SIZES][2], s[TX_SIZES]; michael@0: int64_t d[TX_SIZES], sse[TX_SIZES]; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; michael@0: struct rdcost_block_args *rdcost_stack = &cpi->rdcost_stack; michael@0: const int b_inter_mode = is_inter_block(mbmi); michael@0: michael@0: assert(bs == mbmi->sb_type); michael@0: if (b_inter_mode) michael@0: vp9_subtract_sby(x, bs); michael@0: michael@0: if (cpi->sf.tx_size_search_method == USE_LARGESTALL || michael@0: (cpi->sf.tx_size_search_method != USE_FULL_RD && michael@0: !b_inter_mode)) { michael@0: vpx_memset(txfm_cache, 0, TX_MODES * sizeof(int64_t)); michael@0: choose_largest_txfm_size(cpi, x, rate, distortion, skip, sse, michael@0: ref_best_rd, bs); michael@0: if (psse) michael@0: *psse = sse[mbmi->tx_size]; michael@0: return; michael@0: } michael@0: michael@0: if (cpi->sf.tx_size_search_method == USE_LARGESTINTRA_MODELINTER && michael@0: b_inter_mode) { michael@0: if (bs >= BLOCK_32X32) michael@0: model_rd_for_sb_y_tx(cpi, bs, TX_32X32, x, xd, michael@0: &r[TX_32X32][0], &d[TX_32X32], &s[TX_32X32]); michael@0: if (bs >= BLOCK_16X16) michael@0: model_rd_for_sb_y_tx(cpi, bs, TX_16X16, x, xd, michael@0: &r[TX_16X16][0], &d[TX_16X16], &s[TX_16X16]); michael@0: michael@0: model_rd_for_sb_y_tx(cpi, bs, TX_8X8, x, xd, michael@0: &r[TX_8X8][0], &d[TX_8X8], &s[TX_8X8]); michael@0: michael@0: model_rd_for_sb_y_tx(cpi, bs, TX_4X4, x, xd, michael@0: &r[TX_4X4][0], &d[TX_4X4], &s[TX_4X4]); michael@0: michael@0: choose_txfm_size_from_modelrd(cpi, x, r, rate, d, distortion, s, michael@0: skip, sse, ref_best_rd, bs); michael@0: } else { michael@0: if (bs >= BLOCK_32X32) michael@0: txfm_rd_in_plane(x, rdcost_stack, &r[TX_32X32][0], &d[TX_32X32], michael@0: &s[TX_32X32], &sse[TX_32X32], michael@0: ref_best_rd, 0, bs, TX_32X32); michael@0: if (bs >= BLOCK_16X16) michael@0: txfm_rd_in_plane(x, rdcost_stack, &r[TX_16X16][0], &d[TX_16X16], michael@0: &s[TX_16X16], &sse[TX_16X16], michael@0: ref_best_rd, 0, bs, TX_16X16); michael@0: txfm_rd_in_plane(x, rdcost_stack, &r[TX_8X8][0], &d[TX_8X8], &s[TX_8X8], michael@0: &sse[TX_8X8], ref_best_rd, 0, bs, TX_8X8); michael@0: txfm_rd_in_plane(x, rdcost_stack, &r[TX_4X4][0], &d[TX_4X4], &s[TX_4X4], michael@0: &sse[TX_4X4], ref_best_rd, 0, bs, TX_4X4); michael@0: choose_txfm_size_from_rd(cpi, x, r, rate, d, distortion, s, michael@0: skip, txfm_cache, bs); michael@0: } michael@0: if (psse) michael@0: *psse = sse[mbmi->tx_size]; michael@0: } michael@0: michael@0: static int conditional_skipintra(MB_PREDICTION_MODE mode, michael@0: MB_PREDICTION_MODE best_intra_mode) { michael@0: if (mode == D117_PRED && michael@0: best_intra_mode != V_PRED && michael@0: best_intra_mode != D135_PRED) michael@0: return 1; michael@0: if (mode == D63_PRED && michael@0: best_intra_mode != V_PRED && michael@0: best_intra_mode != D45_PRED) michael@0: return 1; michael@0: if (mode == D207_PRED && michael@0: best_intra_mode != H_PRED && michael@0: best_intra_mode != D45_PRED) michael@0: return 1; michael@0: if (mode == D153_PRED && michael@0: best_intra_mode != H_PRED && michael@0: best_intra_mode != D135_PRED) michael@0: return 1; michael@0: return 0; michael@0: } michael@0: michael@0: static int64_t rd_pick_intra4x4block(VP9_COMP *cpi, MACROBLOCK *x, int ib, michael@0: MB_PREDICTION_MODE *best_mode, michael@0: int *bmode_costs, michael@0: ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l, michael@0: int *bestrate, int *bestratey, michael@0: int64_t *bestdistortion, michael@0: BLOCK_SIZE bsize, int64_t rd_thresh) { michael@0: MB_PREDICTION_MODE mode; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: int64_t best_rd = rd_thresh; michael@0: int rate = 0; michael@0: int64_t distortion; michael@0: struct macroblock_plane *p = &x->plane[0]; michael@0: struct macroblockd_plane *pd = &xd->plane[0]; michael@0: const int src_stride = p->src.stride; michael@0: const int dst_stride = pd->dst.stride; michael@0: uint8_t *src_init = raster_block_offset_uint8(BLOCK_8X8, ib, michael@0: p->src.buf, src_stride); michael@0: uint8_t *dst_init = raster_block_offset_uint8(BLOCK_8X8, ib, michael@0: pd->dst.buf, dst_stride); michael@0: int16_t *src_diff, *coeff; michael@0: michael@0: ENTROPY_CONTEXT ta[2], tempa[2]; michael@0: ENTROPY_CONTEXT tl[2], templ[2]; michael@0: michael@0: const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize]; michael@0: const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize]; michael@0: int idx, idy; michael@0: uint8_t best_dst[8 * 8]; michael@0: michael@0: assert(ib < 4); michael@0: michael@0: vpx_memcpy(ta, a, sizeof(ta)); michael@0: vpx_memcpy(tl, l, sizeof(tl)); michael@0: xd->mi_8x8[0]->mbmi.tx_size = TX_4X4; michael@0: michael@0: for (mode = DC_PRED; mode <= TM_PRED; ++mode) { michael@0: int64_t this_rd; michael@0: int ratey = 0; michael@0: michael@0: if (!(cpi->sf.intra_y_mode_mask[TX_4X4] & (1 << mode))) michael@0: continue; michael@0: michael@0: // Only do the oblique modes if the best so far is michael@0: // one of the neighboring directional modes michael@0: if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { michael@0: if (conditional_skipintra(mode, *best_mode)) michael@0: continue; michael@0: } michael@0: michael@0: rate = bmode_costs[mode]; michael@0: distortion = 0; michael@0: michael@0: vpx_memcpy(tempa, ta, sizeof(ta)); michael@0: vpx_memcpy(templ, tl, sizeof(tl)); michael@0: michael@0: for (idy = 0; idy < num_4x4_blocks_high; ++idy) { michael@0: for (idx = 0; idx < num_4x4_blocks_wide; ++idx) { michael@0: int64_t ssz; michael@0: const int16_t *scan; michael@0: const int16_t *nb; michael@0: uint8_t *src = src_init + idx * 4 + idy * 4 * src_stride; michael@0: uint8_t *dst = dst_init + idx * 4 + idy * 4 * dst_stride; michael@0: const int block = ib + idy * 2 + idx; michael@0: TX_TYPE tx_type; michael@0: xd->mi_8x8[0]->bmi[block].as_mode = mode; michael@0: src_diff = raster_block_offset_int16(BLOCK_8X8, block, p->src_diff); michael@0: coeff = BLOCK_OFFSET(x->plane[0].coeff, block); michael@0: vp9_predict_intra_block(xd, block, 1, michael@0: TX_4X4, mode, michael@0: x->skip_encode ? src : dst, michael@0: x->skip_encode ? src_stride : dst_stride, michael@0: dst, dst_stride); michael@0: vp9_subtract_block(4, 4, src_diff, 8, michael@0: src, src_stride, michael@0: dst, dst_stride); michael@0: michael@0: tx_type = get_tx_type_4x4(PLANE_TYPE_Y_WITH_DC, xd, block); michael@0: get_scan_nb_4x4(tx_type, &scan, &nb); michael@0: michael@0: if (tx_type != DCT_DCT) michael@0: vp9_short_fht4x4(src_diff, coeff, 8, tx_type); michael@0: else michael@0: x->fwd_txm4x4(src_diff, coeff, 8); michael@0: michael@0: vp9_regular_quantize_b_4x4(x, 4, block, scan, get_iscan_4x4(tx_type)); michael@0: michael@0: ratey += cost_coeffs(x, 0, block, michael@0: tempa + idx, templ + idy, TX_4X4, scan, nb); michael@0: distortion += vp9_block_error(coeff, BLOCK_OFFSET(pd->dqcoeff, block), michael@0: 16, &ssz) >> 2; michael@0: if (RDCOST(x->rdmult, x->rddiv, ratey, distortion) >= best_rd) michael@0: goto next; michael@0: michael@0: if (tx_type != DCT_DCT) michael@0: vp9_iht4x4_16_add(BLOCK_OFFSET(pd->dqcoeff, block), michael@0: dst, pd->dst.stride, tx_type); michael@0: else michael@0: xd->itxm_add(BLOCK_OFFSET(pd->dqcoeff, block), dst, pd->dst.stride, michael@0: 16); michael@0: } michael@0: } michael@0: michael@0: rate += ratey; michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, rate, distortion); michael@0: michael@0: if (this_rd < best_rd) { 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: vpx_memcpy(a, tempa, sizeof(tempa)); michael@0: vpx_memcpy(l, templ, sizeof(templ)); michael@0: for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy) michael@0: vpx_memcpy(best_dst + idy * 8, dst_init + idy * dst_stride, michael@0: num_4x4_blocks_wide * 4); michael@0: } michael@0: next: michael@0: {} michael@0: } michael@0: michael@0: if (best_rd >= rd_thresh || x->skip_encode) michael@0: return best_rd; michael@0: michael@0: for (idy = 0; idy < num_4x4_blocks_high * 4; ++idy) michael@0: vpx_memcpy(dst_init + idy * dst_stride, best_dst + idy * 8, michael@0: num_4x4_blocks_wide * 4); michael@0: michael@0: return best_rd; michael@0: } michael@0: michael@0: static int64_t rd_pick_intra_sub_8x8_y_mode(VP9_COMP * const cpi, michael@0: MACROBLOCK * const mb, michael@0: int * const rate, michael@0: int * const rate_y, michael@0: int64_t * const distortion, michael@0: int64_t best_rd) { michael@0: int i, j; michael@0: MACROBLOCKD *const xd = &mb->e_mbd; michael@0: MODE_INFO *const mic = xd->mi_8x8[0]; michael@0: const MODE_INFO *above_mi = xd->mi_8x8[-xd->mode_info_stride]; michael@0: const MODE_INFO *left_mi = xd->left_available ? xd->mi_8x8[-1] : NULL; michael@0: const BLOCK_SIZE bsize = xd->mi_8x8[0]->mbmi.sb_type; michael@0: const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize]; michael@0: const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize]; michael@0: int idx, idy; michael@0: int cost = 0; michael@0: int64_t total_distortion = 0; michael@0: int tot_rate_y = 0; michael@0: int64_t total_rd = 0; michael@0: ENTROPY_CONTEXT t_above[4], t_left[4]; michael@0: int *bmode_costs; michael@0: michael@0: vpx_memcpy(t_above, xd->plane[0].above_context, sizeof(t_above)); michael@0: vpx_memcpy(t_left, xd->plane[0].left_context, sizeof(t_left)); michael@0: michael@0: bmode_costs = mb->mbmode_cost; michael@0: michael@0: // Pick modes for each sub-block (of size 4x4, 4x8, or 8x4) in an 8x8 block. michael@0: for (idy = 0; idy < 2; idy += num_4x4_blocks_high) { michael@0: for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) { michael@0: MB_PREDICTION_MODE best_mode = DC_PRED; michael@0: int r = INT_MAX, ry = INT_MAX; michael@0: int64_t d = INT64_MAX, this_rd = INT64_MAX; michael@0: i = idy * 2 + idx; michael@0: if (cpi->common.frame_type == KEY_FRAME) { michael@0: const MB_PREDICTION_MODE A = above_block_mode(mic, above_mi, i); michael@0: const MB_PREDICTION_MODE L = left_block_mode(mic, left_mi, i); michael@0: michael@0: bmode_costs = mb->y_mode_costs[A][L]; michael@0: } michael@0: michael@0: this_rd = rd_pick_intra4x4block(cpi, mb, i, &best_mode, bmode_costs, michael@0: t_above + idx, t_left + idy, &r, &ry, &d, michael@0: bsize, best_rd - total_rd); michael@0: if (this_rd >= best_rd - total_rd) michael@0: return INT64_MAX; michael@0: michael@0: total_rd += this_rd; michael@0: cost += r; michael@0: total_distortion += d; michael@0: tot_rate_y += ry; michael@0: michael@0: mic->bmi[i].as_mode = best_mode; michael@0: for (j = 1; j < num_4x4_blocks_high; ++j) michael@0: mic->bmi[i + j * 2].as_mode = best_mode; michael@0: for (j = 1; j < num_4x4_blocks_wide; ++j) michael@0: mic->bmi[i + j].as_mode = best_mode; michael@0: michael@0: if (total_rd >= best_rd) michael@0: return INT64_MAX; michael@0: } michael@0: } michael@0: michael@0: *rate = cost; michael@0: *rate_y = tot_rate_y; michael@0: *distortion = total_distortion; michael@0: mic->mbmi.mode = mic->bmi[3].as_mode; michael@0: michael@0: return RDCOST(mb->rdmult, mb->rddiv, cost, total_distortion); michael@0: } michael@0: michael@0: static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x, michael@0: int *rate, int *rate_tokenonly, michael@0: int64_t *distortion, int *skippable, michael@0: BLOCK_SIZE bsize, michael@0: int64_t tx_cache[TX_MODES], michael@0: int64_t best_rd) { michael@0: MB_PREDICTION_MODE mode; michael@0: MB_PREDICTION_MODE mode_selected = DC_PRED; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: MODE_INFO *const mic = xd->mi_8x8[0]; michael@0: int this_rate, this_rate_tokenonly, s; michael@0: int64_t this_distortion, this_rd; michael@0: TX_SIZE best_tx = TX_4X4; michael@0: int i; michael@0: int *bmode_costs = x->mbmode_cost; michael@0: michael@0: if (cpi->sf.tx_size_search_method == USE_FULL_RD) michael@0: for (i = 0; i < TX_MODES; i++) michael@0: tx_cache[i] = INT64_MAX; michael@0: michael@0: /* Y Search for intra prediction mode */ michael@0: for (mode = DC_PRED; mode <= TM_PRED; mode++) { michael@0: int64_t local_tx_cache[TX_MODES]; michael@0: MODE_INFO *above_mi = xd->mi_8x8[-xd->mode_info_stride]; michael@0: MODE_INFO *left_mi = xd->left_available ? xd->mi_8x8[-1] : NULL; michael@0: michael@0: if (!(cpi->sf.intra_y_mode_mask[max_txsize_lookup[bsize]] & (1 << mode))) michael@0: continue; michael@0: michael@0: if (cpi->common.frame_type == KEY_FRAME) { michael@0: const MB_PREDICTION_MODE A = above_block_mode(mic, above_mi, 0); michael@0: const MB_PREDICTION_MODE L = left_block_mode(mic, left_mi, 0); michael@0: michael@0: bmode_costs = x->y_mode_costs[A][L]; michael@0: } michael@0: mic->mbmi.mode = mode; michael@0: michael@0: super_block_yrd(cpi, x, &this_rate_tokenonly, &this_distortion, &s, NULL, michael@0: bsize, local_tx_cache, best_rd); michael@0: michael@0: if (this_rate_tokenonly == INT_MAX) michael@0: continue; michael@0: michael@0: this_rate = this_rate_tokenonly + bmode_costs[mode]; michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); michael@0: michael@0: if (this_rd < best_rd) { michael@0: mode_selected = mode; michael@0: best_rd = this_rd; michael@0: best_tx = mic->mbmi.tx_size; michael@0: *rate = this_rate; michael@0: *rate_tokenonly = this_rate_tokenonly; michael@0: *distortion = this_distortion; michael@0: *skippable = s; michael@0: } michael@0: michael@0: if (cpi->sf.tx_size_search_method == USE_FULL_RD && this_rd < INT64_MAX) { michael@0: for (i = 0; i < TX_MODES && local_tx_cache[i] < INT64_MAX; i++) { michael@0: const int64_t adj_rd = this_rd + local_tx_cache[i] - michael@0: local_tx_cache[cpi->common.tx_mode]; michael@0: if (adj_rd < tx_cache[i]) { michael@0: tx_cache[i] = adj_rd; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: mic->mbmi.mode = mode_selected; michael@0: mic->mbmi.tx_size = best_tx; michael@0: michael@0: return best_rd; michael@0: } michael@0: michael@0: static void super_block_uvrd(VP9_COMP *const cpi, MACROBLOCK *x, michael@0: int *rate, int64_t *distortion, int *skippable, michael@0: int64_t *sse, BLOCK_SIZE bsize, michael@0: int64_t ref_best_rd) { michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; michael@0: TX_SIZE uv_txfm_size = get_uv_tx_size(mbmi); michael@0: int plane; michael@0: int pnrate = 0, pnskip = 1; michael@0: int64_t pndist = 0, pnsse = 0; michael@0: michael@0: if (ref_best_rd < 0) michael@0: goto term; michael@0: michael@0: if (is_inter_block(mbmi)) michael@0: vp9_subtract_sbuv(x, bsize); michael@0: michael@0: *rate = 0; michael@0: *distortion = 0; michael@0: *sse = 0; michael@0: *skippable = 1; michael@0: michael@0: for (plane = 1; plane < MAX_MB_PLANE; ++plane) { michael@0: txfm_rd_in_plane(x, &cpi->rdcost_stack, &pnrate, &pndist, &pnskip, &pnsse, michael@0: ref_best_rd, plane, bsize, uv_txfm_size); michael@0: if (pnrate == INT_MAX) michael@0: goto term; michael@0: *rate += pnrate; michael@0: *distortion += pndist; michael@0: *sse += pnsse; michael@0: *skippable &= pnskip; michael@0: } michael@0: return; michael@0: michael@0: term: michael@0: *rate = INT_MAX; michael@0: *distortion = INT64_MAX; michael@0: *sse = INT64_MAX; michael@0: *skippable = 0; michael@0: return; michael@0: } michael@0: michael@0: static int64_t rd_pick_intra_sbuv_mode(VP9_COMP *cpi, MACROBLOCK *x, michael@0: PICK_MODE_CONTEXT *ctx, michael@0: int *rate, int *rate_tokenonly, michael@0: int64_t *distortion, int *skippable, michael@0: BLOCK_SIZE bsize) { michael@0: MB_PREDICTION_MODE mode; michael@0: MB_PREDICTION_MODE mode_selected = DC_PRED; michael@0: int64_t best_rd = INT64_MAX, this_rd; michael@0: int this_rate_tokenonly, this_rate, s; michael@0: int64_t this_distortion, this_sse; michael@0: michael@0: // int mode_mask = (bsize <= BLOCK_8X8) michael@0: // ? ALL_INTRA_MODES : cpi->sf.intra_uv_mode_mask; michael@0: michael@0: for (mode = DC_PRED; mode <= TM_PRED; mode ++) { michael@0: // if (!(mode_mask & (1 << mode))) michael@0: if (!(cpi->sf.intra_uv_mode_mask[max_uv_txsize_lookup[bsize]] michael@0: & (1 << mode))) michael@0: continue; michael@0: michael@0: x->e_mbd.mi_8x8[0]->mbmi.uv_mode = mode; michael@0: michael@0: super_block_uvrd(cpi, x, &this_rate_tokenonly, michael@0: &this_distortion, &s, &this_sse, bsize, best_rd); michael@0: if (this_rate_tokenonly == INT_MAX) michael@0: continue; michael@0: this_rate = this_rate_tokenonly + michael@0: x->intra_uv_mode_cost[cpi->common.frame_type][mode]; michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, this_rate, this_distortion); michael@0: michael@0: if (this_rd < best_rd) { michael@0: mode_selected = mode; michael@0: best_rd = this_rd; michael@0: *rate = this_rate; michael@0: *rate_tokenonly = this_rate_tokenonly; michael@0: *distortion = this_distortion; michael@0: *skippable = s; michael@0: if (!x->select_txfm_size) { michael@0: int i; michael@0: struct macroblock_plane *const p = x->plane; michael@0: struct macroblockd_plane *const pd = x->e_mbd.plane; michael@0: for (i = 1; i < MAX_MB_PLANE; ++i) { michael@0: p[i].coeff = ctx->coeff_pbuf[i][2]; michael@0: pd[i].qcoeff = ctx->qcoeff_pbuf[i][2]; michael@0: pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][2]; michael@0: pd[i].eobs = ctx->eobs_pbuf[i][2]; michael@0: michael@0: ctx->coeff_pbuf[i][2] = ctx->coeff_pbuf[i][0]; michael@0: ctx->qcoeff_pbuf[i][2] = ctx->qcoeff_pbuf[i][0]; michael@0: ctx->dqcoeff_pbuf[i][2] = ctx->dqcoeff_pbuf[i][0]; michael@0: ctx->eobs_pbuf[i][2] = ctx->eobs_pbuf[i][0]; michael@0: michael@0: ctx->coeff_pbuf[i][0] = p[i].coeff; michael@0: ctx->qcoeff_pbuf[i][0] = pd[i].qcoeff; michael@0: ctx->dqcoeff_pbuf[i][0] = pd[i].dqcoeff; michael@0: ctx->eobs_pbuf[i][0] = pd[i].eobs; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: x->e_mbd.mi_8x8[0]->mbmi.uv_mode = mode_selected; michael@0: michael@0: return best_rd; michael@0: } michael@0: michael@0: static int64_t rd_sbuv_dcpred(VP9_COMP *cpi, MACROBLOCK *x, michael@0: int *rate, int *rate_tokenonly, michael@0: int64_t *distortion, int *skippable, michael@0: BLOCK_SIZE bsize) { michael@0: int64_t this_rd; michael@0: int64_t this_sse; michael@0: michael@0: x->e_mbd.mi_8x8[0]->mbmi.uv_mode = DC_PRED; michael@0: super_block_uvrd(cpi, x, rate_tokenonly, distortion, michael@0: skippable, &this_sse, bsize, INT64_MAX); michael@0: *rate = *rate_tokenonly + michael@0: x->intra_uv_mode_cost[cpi->common.frame_type][DC_PRED]; michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, *rate, *distortion); michael@0: michael@0: return this_rd; michael@0: } michael@0: michael@0: static void choose_intra_uv_mode(VP9_COMP *cpi, PICK_MODE_CONTEXT *ctx, michael@0: BLOCK_SIZE bsize, int *rate_uv, michael@0: int *rate_uv_tokenonly, michael@0: int64_t *dist_uv, int *skip_uv, michael@0: MB_PREDICTION_MODE *mode_uv) { michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: michael@0: // Use an estimated rd for uv_intra based on DC_PRED if the michael@0: // appropriate speed flag is set. michael@0: if (cpi->sf.use_uv_intra_rd_estimate) { michael@0: rd_sbuv_dcpred(cpi, x, rate_uv, rate_uv_tokenonly, dist_uv, skip_uv, michael@0: bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize); michael@0: // Else do a proper rd search for each possible transform size that may michael@0: // be considered in the main rd loop. michael@0: } else { michael@0: rd_pick_intra_sbuv_mode(cpi, x, ctx, michael@0: rate_uv, rate_uv_tokenonly, dist_uv, skip_uv, michael@0: bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize); michael@0: } michael@0: *mode_uv = x->e_mbd.mi_8x8[0]->mbmi.uv_mode; michael@0: } michael@0: michael@0: static int cost_mv_ref(VP9_COMP *cpi, MB_PREDICTION_MODE mode, michael@0: int mode_context) { michael@0: MACROBLOCK *const x = &cpi->mb; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: const int segment_id = xd->mi_8x8[0]->mbmi.segment_id; michael@0: michael@0: // Don't account for mode here if segment skip is enabled. michael@0: if (!vp9_segfeature_active(&cpi->common.seg, segment_id, SEG_LVL_SKIP)) { michael@0: assert(is_inter_mode(mode)); michael@0: return x->inter_mode_cost[mode_context][INTER_OFFSET(mode)]; michael@0: } else { michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: void vp9_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv) { michael@0: x->e_mbd.mi_8x8[0]->mbmi.mode = mb; michael@0: x->e_mbd.mi_8x8[0]->mbmi.mv[0].as_int = mv->as_int; michael@0: } michael@0: michael@0: static void joint_motion_search(VP9_COMP *cpi, MACROBLOCK *x, michael@0: BLOCK_SIZE bsize, michael@0: int_mv *frame_mv, michael@0: int mi_row, int mi_col, michael@0: int_mv single_newmv[MAX_REF_FRAMES], michael@0: int *rate_mv); michael@0: michael@0: static int labels2mode(MACROBLOCK *x, int i, michael@0: MB_PREDICTION_MODE this_mode, michael@0: int_mv *this_mv, int_mv *this_second_mv, michael@0: int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES], michael@0: int_mv seg_mvs[MAX_REF_FRAMES], michael@0: int_mv *best_ref_mv, michael@0: int_mv *second_best_ref_mv, michael@0: int *mvjcost, int *mvcost[2], VP9_COMP *cpi) { michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: MODE_INFO *const mic = xd->mi_8x8[0]; michael@0: MB_MODE_INFO *mbmi = &mic->mbmi; michael@0: int cost = 0, thismvcost = 0; michael@0: int idx, idy; michael@0: const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[mbmi->sb_type]; michael@0: const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[mbmi->sb_type]; michael@0: const int has_second_rf = has_second_ref(mbmi); 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: MB_PREDICTION_MODE m; michael@0: michael@0: // the only time we should do costing for new motion vector or mode michael@0: // is when we are on a new label (jbb May 08, 2007) michael@0: switch (m = this_mode) { michael@0: case NEWMV: michael@0: this_mv->as_int = seg_mvs[mbmi->ref_frame[0]].as_int; michael@0: thismvcost = vp9_mv_bit_cost(&this_mv->as_mv, &best_ref_mv->as_mv, michael@0: mvjcost, mvcost, MV_COST_WEIGHT_SUB); michael@0: if (has_second_rf) { michael@0: this_second_mv->as_int = seg_mvs[mbmi->ref_frame[1]].as_int; michael@0: thismvcost += vp9_mv_bit_cost(&this_second_mv->as_mv, michael@0: &second_best_ref_mv->as_mv, michael@0: mvjcost, mvcost, MV_COST_WEIGHT_SUB); michael@0: } michael@0: break; michael@0: case NEARESTMV: michael@0: this_mv->as_int = frame_mv[NEARESTMV][mbmi->ref_frame[0]].as_int; michael@0: if (has_second_rf) michael@0: this_second_mv->as_int = michael@0: frame_mv[NEARESTMV][mbmi->ref_frame[1]].as_int; michael@0: break; michael@0: case NEARMV: michael@0: this_mv->as_int = frame_mv[NEARMV][mbmi->ref_frame[0]].as_int; michael@0: if (has_second_rf) michael@0: this_second_mv->as_int = michael@0: frame_mv[NEARMV][mbmi->ref_frame[1]].as_int; michael@0: break; michael@0: case ZEROMV: michael@0: this_mv->as_int = 0; michael@0: if (has_second_rf) michael@0: this_second_mv->as_int = 0; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: cost = cost_mv_ref(cpi, this_mode, michael@0: mbmi->mode_context[mbmi->ref_frame[0]]); michael@0: michael@0: mic->bmi[i].as_mv[0].as_int = this_mv->as_int; michael@0: if (has_second_rf) michael@0: mic->bmi[i].as_mv[1].as_int = this_second_mv->as_int; michael@0: michael@0: mic->bmi[i].as_mode = m; michael@0: michael@0: for (idy = 0; idy < num_4x4_blocks_high; ++idy) michael@0: for (idx = 0; idx < num_4x4_blocks_wide; ++idx) michael@0: vpx_memcpy(&mic->bmi[i + idy * 2 + idx], michael@0: &mic->bmi[i], sizeof(mic->bmi[i])); michael@0: michael@0: cost += thismvcost; michael@0: return cost; michael@0: } michael@0: michael@0: static int64_t encode_inter_mb_segment(VP9_COMP *cpi, michael@0: MACROBLOCK *x, michael@0: int64_t best_yrd, michael@0: int i, michael@0: int *labelyrate, michael@0: int64_t *distortion, int64_t *sse, michael@0: ENTROPY_CONTEXT *ta, michael@0: ENTROPY_CONTEXT *tl) { michael@0: int k; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: struct macroblockd_plane *const pd = &xd->plane[0]; michael@0: struct macroblock_plane *const p = &x->plane[0]; michael@0: MODE_INFO *const mi = xd->mi_8x8[0]; michael@0: const BLOCK_SIZE bsize = mi->mbmi.sb_type; michael@0: const int width = plane_block_width(bsize, pd); michael@0: const int height = plane_block_height(bsize, pd); michael@0: int idx, idy; michael@0: michael@0: uint8_t *const src = raster_block_offset_uint8(BLOCK_8X8, i, michael@0: p->src.buf, p->src.stride); michael@0: uint8_t *const dst = raster_block_offset_uint8(BLOCK_8X8, i, michael@0: pd->dst.buf, pd->dst.stride); michael@0: int64_t thisdistortion = 0, thissse = 0; michael@0: int thisrate = 0, ref; michael@0: const int is_compound = has_second_ref(&mi->mbmi); michael@0: for (ref = 0; ref < 1 + is_compound; ++ref) { michael@0: const uint8_t *pre = raster_block_offset_uint8(BLOCK_8X8, i, michael@0: pd->pre[ref].buf, pd->pre[ref].stride); michael@0: vp9_build_inter_predictor(pre, pd->pre[ref].stride, michael@0: dst, pd->dst.stride, michael@0: &mi->bmi[i].as_mv[ref].as_mv, michael@0: &xd->scale_factor[ref], michael@0: width, height, ref, &xd->subpix, MV_PRECISION_Q3); michael@0: } michael@0: michael@0: vp9_subtract_block(height, width, michael@0: raster_block_offset_int16(BLOCK_8X8, i, p->src_diff), 8, michael@0: src, p->src.stride, michael@0: dst, pd->dst.stride); michael@0: michael@0: k = i; michael@0: for (idy = 0; idy < height / 4; ++idy) { michael@0: for (idx = 0; idx < width / 4; ++idx) { michael@0: int64_t ssz, rd, rd1, rd2; michael@0: int16_t* coeff; michael@0: michael@0: k += (idy * 2 + idx); michael@0: coeff = BLOCK_OFFSET(p->coeff, k); michael@0: x->fwd_txm4x4(raster_block_offset_int16(BLOCK_8X8, k, p->src_diff), michael@0: coeff, 8); michael@0: vp9_regular_quantize_b_4x4(x, 4, k, get_scan_4x4(DCT_DCT), michael@0: get_iscan_4x4(DCT_DCT)); michael@0: thisdistortion += vp9_block_error(coeff, BLOCK_OFFSET(pd->dqcoeff, k), michael@0: 16, &ssz); michael@0: thissse += ssz; michael@0: thisrate += cost_coeffs(x, 0, k, michael@0: ta + (k & 1), michael@0: tl + (k >> 1), TX_4X4, michael@0: vp9_default_scan_4x4, michael@0: vp9_default_scan_4x4_neighbors); michael@0: rd1 = RDCOST(x->rdmult, x->rddiv, thisrate, thisdistortion >> 2); michael@0: rd2 = RDCOST(x->rdmult, x->rddiv, 0, thissse >> 2); michael@0: rd = MIN(rd1, rd2); michael@0: if (rd >= best_yrd) michael@0: return INT64_MAX; michael@0: } michael@0: } michael@0: michael@0: *distortion = thisdistortion >> 2; michael@0: *labelyrate = thisrate; michael@0: *sse = thissse >> 2; michael@0: michael@0: return RDCOST(x->rdmult, x->rddiv, *labelyrate, *distortion); michael@0: } michael@0: michael@0: typedef struct { michael@0: int eobs; michael@0: int brate; michael@0: int byrate; michael@0: int64_t bdist; michael@0: int64_t bsse; michael@0: int64_t brdcost; michael@0: int_mv mvs[2]; michael@0: ENTROPY_CONTEXT ta[2]; michael@0: ENTROPY_CONTEXT tl[2]; michael@0: } SEG_RDSTAT; michael@0: michael@0: typedef struct { michael@0: int_mv *ref_mv, *second_ref_mv; michael@0: int_mv mvp; michael@0: michael@0: int64_t segment_rd; michael@0: int r; michael@0: int64_t d; michael@0: int64_t sse; michael@0: int segment_yrate; michael@0: MB_PREDICTION_MODE modes[4]; michael@0: SEG_RDSTAT rdstat[4][INTER_MODES]; michael@0: int mvthresh; michael@0: } BEST_SEG_INFO; michael@0: michael@0: static INLINE int mv_check_bounds(MACROBLOCK *x, int_mv *mv) { michael@0: int r = 0; michael@0: r |= (mv->as_mv.row >> 3) < x->mv_row_min; michael@0: r |= (mv->as_mv.row >> 3) > x->mv_row_max; michael@0: r |= (mv->as_mv.col >> 3) < x->mv_col_min; michael@0: r |= (mv->as_mv.col >> 3) > x->mv_col_max; michael@0: return r; michael@0: } michael@0: michael@0: static INLINE void mi_buf_shift(MACROBLOCK *x, int i) { michael@0: MB_MODE_INFO *const mbmi = &x->e_mbd.mi_8x8[0]->mbmi; michael@0: struct macroblock_plane *const p = &x->plane[0]; michael@0: struct macroblockd_plane *const pd = &x->e_mbd.plane[0]; michael@0: michael@0: p->src.buf = raster_block_offset_uint8(BLOCK_8X8, i, p->src.buf, michael@0: p->src.stride); michael@0: assert(((intptr_t)pd->pre[0].buf & 0x7) == 0); michael@0: pd->pre[0].buf = raster_block_offset_uint8(BLOCK_8X8, i, pd->pre[0].buf, michael@0: pd->pre[0].stride); michael@0: if (has_second_ref(mbmi)) michael@0: pd->pre[1].buf = raster_block_offset_uint8(BLOCK_8X8, i, pd->pre[1].buf, michael@0: pd->pre[1].stride); michael@0: } michael@0: michael@0: static INLINE void mi_buf_restore(MACROBLOCK *x, struct buf_2d orig_src, michael@0: struct buf_2d orig_pre[2]) { michael@0: MB_MODE_INFO *mbmi = &x->e_mbd.mi_8x8[0]->mbmi; michael@0: x->plane[0].src = orig_src; michael@0: x->e_mbd.plane[0].pre[0] = orig_pre[0]; michael@0: if (has_second_ref(mbmi)) michael@0: x->e_mbd.plane[0].pre[1] = orig_pre[1]; michael@0: } michael@0: michael@0: static void rd_check_segment_txsize(VP9_COMP *cpi, MACROBLOCK *x, michael@0: const TileInfo *const tile, michael@0: BEST_SEG_INFO *bsi_buf, int filter_idx, michael@0: int_mv seg_mvs[4][MAX_REF_FRAMES], michael@0: int mi_row, int mi_col) { michael@0: int i, br = 0, idx, idy; michael@0: int64_t bd = 0, block_sse = 0; michael@0: MB_PREDICTION_MODE this_mode; michael@0: MODE_INFO *mi = x->e_mbd.mi_8x8[0]; michael@0: MB_MODE_INFO *const mbmi = &mi->mbmi; michael@0: struct macroblockd_plane *const pd = &x->e_mbd.plane[0]; michael@0: const int label_count = 4; michael@0: int64_t this_segment_rd = 0; michael@0: int label_mv_thresh; michael@0: int segmentyrate = 0; michael@0: const BLOCK_SIZE bsize = mbmi->sb_type; michael@0: const int num_4x4_blocks_wide = num_4x4_blocks_wide_lookup[bsize]; michael@0: const int num_4x4_blocks_high = num_4x4_blocks_high_lookup[bsize]; michael@0: vp9_variance_fn_ptr_t *v_fn_ptr; michael@0: ENTROPY_CONTEXT t_above[2], t_left[2]; michael@0: BEST_SEG_INFO *bsi = bsi_buf + filter_idx; michael@0: int mode_idx; michael@0: int subpelmv = 1, have_ref = 0; michael@0: const int has_second_rf = has_second_ref(mbmi); michael@0: michael@0: vpx_memcpy(t_above, pd->above_context, sizeof(t_above)); michael@0: vpx_memcpy(t_left, pd->left_context, sizeof(t_left)); michael@0: michael@0: v_fn_ptr = &cpi->fn_ptr[bsize]; michael@0: michael@0: // 64 makes this threshold really big effectively michael@0: // making it so that we very rarely check mvs on michael@0: // segments. setting this to 1 would make mv thresh michael@0: // roughly equal to what it is for macroblocks michael@0: label_mv_thresh = 1 * bsi->mvthresh / label_count; michael@0: michael@0: // Segmentation method overheads michael@0: for (idy = 0; idy < 2; idy += num_4x4_blocks_high) { michael@0: for (idx = 0; idx < 2; idx += num_4x4_blocks_wide) { michael@0: // TODO(jingning,rbultje): rewrite the rate-distortion optimization michael@0: // loop for 4x4/4x8/8x4 block coding. to be replaced with new rd loop michael@0: int_mv mode_mv[MB_MODE_COUNT], second_mode_mv[MB_MODE_COUNT]; michael@0: int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES]; michael@0: MB_PREDICTION_MODE mode_selected = ZEROMV; michael@0: int64_t best_rd = INT64_MAX; michael@0: i = idy * 2 + idx; michael@0: michael@0: frame_mv[ZEROMV][mbmi->ref_frame[0]].as_int = 0; michael@0: vp9_append_sub8x8_mvs_for_idx(&cpi->common, &x->e_mbd, tile, michael@0: &frame_mv[NEARESTMV][mbmi->ref_frame[0]], michael@0: &frame_mv[NEARMV][mbmi->ref_frame[0]], michael@0: i, 0, mi_row, mi_col); michael@0: if (has_second_rf) { michael@0: frame_mv[ZEROMV][mbmi->ref_frame[1]].as_int = 0; michael@0: vp9_append_sub8x8_mvs_for_idx(&cpi->common, &x->e_mbd, tile, michael@0: &frame_mv[NEARESTMV][mbmi->ref_frame[1]], michael@0: &frame_mv[NEARMV][mbmi->ref_frame[1]], michael@0: i, 1, mi_row, mi_col); michael@0: } michael@0: // search for the best motion vector on this segment michael@0: for (this_mode = NEARESTMV; this_mode <= NEWMV; ++this_mode) { michael@0: const struct buf_2d orig_src = x->plane[0].src; michael@0: struct buf_2d orig_pre[2]; michael@0: michael@0: mode_idx = INTER_OFFSET(this_mode); michael@0: bsi->rdstat[i][mode_idx].brdcost = INT64_MAX; michael@0: michael@0: // if we're near/nearest and mv == 0,0, compare to zeromv michael@0: if ((this_mode == NEARMV || this_mode == NEARESTMV || michael@0: this_mode == ZEROMV) && michael@0: frame_mv[this_mode][mbmi->ref_frame[0]].as_int == 0 && michael@0: (!has_second_rf || michael@0: frame_mv[this_mode][mbmi->ref_frame[1]].as_int == 0)) { michael@0: int rfc = mbmi->mode_context[mbmi->ref_frame[0]]; michael@0: int c1 = cost_mv_ref(cpi, NEARMV, rfc); michael@0: int c2 = cost_mv_ref(cpi, NEARESTMV, rfc); michael@0: int c3 = cost_mv_ref(cpi, ZEROMV, rfc); michael@0: michael@0: if (this_mode == NEARMV) { michael@0: if (c1 > c3) michael@0: continue; michael@0: } else if (this_mode == NEARESTMV) { michael@0: if (c2 > c3) michael@0: continue; michael@0: } else { michael@0: assert(this_mode == ZEROMV); michael@0: if (!has_second_rf) { michael@0: if ((c3 >= c2 && michael@0: frame_mv[NEARESTMV][mbmi->ref_frame[0]].as_int == 0) || michael@0: (c3 >= c1 && michael@0: frame_mv[NEARMV][mbmi->ref_frame[0]].as_int == 0)) michael@0: continue; michael@0: } else { michael@0: if ((c3 >= c2 && michael@0: frame_mv[NEARESTMV][mbmi->ref_frame[0]].as_int == 0 && michael@0: frame_mv[NEARESTMV][mbmi->ref_frame[1]].as_int == 0) || michael@0: (c3 >= c1 && michael@0: frame_mv[NEARMV][mbmi->ref_frame[0]].as_int == 0 && michael@0: frame_mv[NEARMV][mbmi->ref_frame[1]].as_int == 0)) michael@0: continue; michael@0: } michael@0: } michael@0: } michael@0: michael@0: vpx_memcpy(orig_pre, pd->pre, sizeof(orig_pre)); michael@0: vpx_memcpy(bsi->rdstat[i][mode_idx].ta, t_above, michael@0: sizeof(bsi->rdstat[i][mode_idx].ta)); michael@0: vpx_memcpy(bsi->rdstat[i][mode_idx].tl, t_left, michael@0: sizeof(bsi->rdstat[i][mode_idx].tl)); michael@0: michael@0: // motion search for newmv (single predictor case only) michael@0: if (!has_second_rf && this_mode == NEWMV && michael@0: seg_mvs[i][mbmi->ref_frame[0]].as_int == INVALID_MV) { michael@0: int step_param = 0; michael@0: int further_steps; michael@0: int thissme, bestsme = INT_MAX; michael@0: int sadpb = x->sadperbit4; michael@0: int_mv mvp_full; michael@0: int max_mv; michael@0: michael@0: /* Is the best so far sufficiently good that we cant justify doing michael@0: * and new motion search. */ michael@0: if (best_rd < label_mv_thresh) michael@0: break; michael@0: michael@0: if (cpi->compressor_speed) { michael@0: // use previous block's result as next block's MV predictor. michael@0: if (i > 0) { michael@0: bsi->mvp.as_int = mi->bmi[i - 1].as_mv[0].as_int; michael@0: if (i == 2) michael@0: bsi->mvp.as_int = mi->bmi[i - 2].as_mv[0].as_int; michael@0: } michael@0: } michael@0: if (i == 0) michael@0: max_mv = x->max_mv_context[mbmi->ref_frame[0]]; michael@0: else michael@0: max_mv = MAX(abs(bsi->mvp.as_mv.row), abs(bsi->mvp.as_mv.col)) >> 3; michael@0: michael@0: if (cpi->sf.auto_mv_step_size && cpi->common.show_frame) { michael@0: // Take wtd average of the step_params based on the last frame's michael@0: // max mv magnitude and the best ref mvs of the current block for michael@0: // the given reference. michael@0: step_param = (vp9_init_search_range(cpi, max_mv) + michael@0: cpi->mv_step_param) >> 1; michael@0: } else { michael@0: step_param = cpi->mv_step_param; michael@0: } 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: if (cpi->sf.adaptive_motion_search && cpi->common.show_frame) { michael@0: mvp_full.as_mv.row = x->pred_mv[mbmi->ref_frame[0]].as_mv.row >> 3; michael@0: mvp_full.as_mv.col = x->pred_mv[mbmi->ref_frame[0]].as_mv.col >> 3; michael@0: step_param = MAX(step_param, 8); michael@0: } michael@0: michael@0: further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; michael@0: // adjust src pointer for this block michael@0: mi_buf_shift(x, i); michael@0: if (cpi->sf.search_method == HEX) { michael@0: bestsme = vp9_hex_search(x, &mvp_full.as_mv, michael@0: step_param, michael@0: sadpb, 1, v_fn_ptr, 1, michael@0: &bsi->ref_mv->as_mv, michael@0: &mode_mv[NEWMV].as_mv); michael@0: } else if (cpi->sf.search_method == SQUARE) { michael@0: bestsme = vp9_square_search(x, &mvp_full.as_mv, michael@0: step_param, michael@0: sadpb, 1, v_fn_ptr, 1, michael@0: &bsi->ref_mv->as_mv, michael@0: &mode_mv[NEWMV].as_mv); michael@0: } else if (cpi->sf.search_method == BIGDIA) { michael@0: bestsme = vp9_bigdia_search(x, &mvp_full.as_mv, michael@0: step_param, michael@0: sadpb, 1, v_fn_ptr, 1, michael@0: &bsi->ref_mv->as_mv, michael@0: &mode_mv[NEWMV].as_mv); michael@0: } else { michael@0: bestsme = vp9_full_pixel_diamond(cpi, x, &mvp_full, step_param, michael@0: sadpb, further_steps, 0, v_fn_ptr, michael@0: bsi->ref_mv, &mode_mv[NEWMV]); michael@0: } michael@0: michael@0: // Should we do a full search (best quality only) michael@0: if (cpi->compressor_speed == 0) { michael@0: /* Check if mvp_full is within the range. */ michael@0: clamp_mv(&mvp_full.as_mv, x->mv_col_min, x->mv_col_max, michael@0: x->mv_row_min, x->mv_row_max); michael@0: michael@0: thissme = cpi->full_search_sad(x, &mvp_full, michael@0: sadpb, 16, v_fn_ptr, michael@0: x->nmvjointcost, x->mvcost, michael@0: bsi->ref_mv, i); michael@0: michael@0: if (thissme < bestsme) { michael@0: bestsme = thissme; michael@0: mode_mv[NEWMV].as_int = mi->bmi[i].as_mv[0].as_int; michael@0: } else { michael@0: /* The full search result is actually worse so re-instate the michael@0: * previous best vector */ michael@0: mi->bmi[i].as_mv[0].as_int = mode_mv[NEWMV].as_int; michael@0: } michael@0: } michael@0: michael@0: if (bestsme < INT_MAX) { michael@0: int distortion; michael@0: unsigned int sse; michael@0: cpi->find_fractional_mv_step(x, michael@0: &mode_mv[NEWMV].as_mv, michael@0: &bsi->ref_mv->as_mv, michael@0: cpi->common.allow_high_precision_mv, michael@0: x->errorperbit, v_fn_ptr, michael@0: 0, cpi->sf.subpel_iters_per_step, michael@0: x->nmvjointcost, x->mvcost, michael@0: &distortion, &sse); michael@0: michael@0: // save motion search result for use in compound prediction michael@0: seg_mvs[i][mbmi->ref_frame[0]].as_int = mode_mv[NEWMV].as_int; michael@0: } michael@0: michael@0: if (cpi->sf.adaptive_motion_search) michael@0: x->pred_mv[mbmi->ref_frame[0]].as_int = mode_mv[NEWMV].as_int; michael@0: michael@0: // restore src pointers michael@0: mi_buf_restore(x, orig_src, orig_pre); michael@0: } michael@0: michael@0: if (has_second_rf) { michael@0: if (seg_mvs[i][mbmi->ref_frame[1]].as_int == INVALID_MV || michael@0: seg_mvs[i][mbmi->ref_frame[0]].as_int == INVALID_MV) michael@0: continue; michael@0: } michael@0: michael@0: if (has_second_rf && this_mode == NEWMV && michael@0: mbmi->interp_filter == EIGHTTAP) { michael@0: // adjust src pointers michael@0: mi_buf_shift(x, i); michael@0: if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { michael@0: int rate_mv; michael@0: joint_motion_search(cpi, x, bsize, frame_mv[this_mode], michael@0: mi_row, mi_col, seg_mvs[i], michael@0: &rate_mv); michael@0: seg_mvs[i][mbmi->ref_frame[0]].as_int = michael@0: frame_mv[this_mode][mbmi->ref_frame[0]].as_int; michael@0: seg_mvs[i][mbmi->ref_frame[1]].as_int = michael@0: frame_mv[this_mode][mbmi->ref_frame[1]].as_int; michael@0: } michael@0: // restore src pointers michael@0: mi_buf_restore(x, orig_src, orig_pre); michael@0: } michael@0: michael@0: bsi->rdstat[i][mode_idx].brate = michael@0: labels2mode(x, i, this_mode, &mode_mv[this_mode], michael@0: &second_mode_mv[this_mode], frame_mv, seg_mvs[i], michael@0: bsi->ref_mv, bsi->second_ref_mv, x->nmvjointcost, michael@0: x->mvcost, cpi); michael@0: michael@0: michael@0: bsi->rdstat[i][mode_idx].mvs[0].as_int = mode_mv[this_mode].as_int; michael@0: if (num_4x4_blocks_wide > 1) michael@0: bsi->rdstat[i + 1][mode_idx].mvs[0].as_int = michael@0: mode_mv[this_mode].as_int; michael@0: if (num_4x4_blocks_high > 1) michael@0: bsi->rdstat[i + 2][mode_idx].mvs[0].as_int = michael@0: mode_mv[this_mode].as_int; michael@0: if (has_second_rf) { michael@0: bsi->rdstat[i][mode_idx].mvs[1].as_int = michael@0: second_mode_mv[this_mode].as_int; michael@0: if (num_4x4_blocks_wide > 1) michael@0: bsi->rdstat[i + 1][mode_idx].mvs[1].as_int = michael@0: second_mode_mv[this_mode].as_int; michael@0: if (num_4x4_blocks_high > 1) michael@0: bsi->rdstat[i + 2][mode_idx].mvs[1].as_int = michael@0: second_mode_mv[this_mode].as_int; michael@0: } michael@0: michael@0: // Trap vectors that reach beyond the UMV borders michael@0: if (mv_check_bounds(x, &mode_mv[this_mode])) michael@0: continue; michael@0: if (has_second_rf && michael@0: mv_check_bounds(x, &second_mode_mv[this_mode])) michael@0: continue; michael@0: michael@0: if (filter_idx > 0) { michael@0: BEST_SEG_INFO *ref_bsi = bsi_buf; michael@0: subpelmv = (mode_mv[this_mode].as_mv.row & 0x0f) || michael@0: (mode_mv[this_mode].as_mv.col & 0x0f); michael@0: have_ref = mode_mv[this_mode].as_int == michael@0: ref_bsi->rdstat[i][mode_idx].mvs[0].as_int; michael@0: if (has_second_rf) { michael@0: subpelmv |= (second_mode_mv[this_mode].as_mv.row & 0x0f) || michael@0: (second_mode_mv[this_mode].as_mv.col & 0x0f); michael@0: have_ref &= second_mode_mv[this_mode].as_int == michael@0: ref_bsi->rdstat[i][mode_idx].mvs[1].as_int; michael@0: } michael@0: michael@0: if (filter_idx > 1 && !subpelmv && !have_ref) { michael@0: ref_bsi = bsi_buf + 1; michael@0: have_ref = mode_mv[this_mode].as_int == michael@0: ref_bsi->rdstat[i][mode_idx].mvs[0].as_int; michael@0: if (has_second_rf) { michael@0: have_ref &= second_mode_mv[this_mode].as_int == michael@0: ref_bsi->rdstat[i][mode_idx].mvs[1].as_int; michael@0: } michael@0: } michael@0: michael@0: if (!subpelmv && have_ref && michael@0: ref_bsi->rdstat[i][mode_idx].brdcost < INT64_MAX) { michael@0: vpx_memcpy(&bsi->rdstat[i][mode_idx], &ref_bsi->rdstat[i][mode_idx], michael@0: sizeof(SEG_RDSTAT)); michael@0: if (num_4x4_blocks_wide > 1) michael@0: bsi->rdstat[i + 1][mode_idx].eobs = michael@0: ref_bsi->rdstat[i + 1][mode_idx].eobs; michael@0: if (num_4x4_blocks_high > 1) michael@0: bsi->rdstat[i + 2][mode_idx].eobs = michael@0: ref_bsi->rdstat[i + 2][mode_idx].eobs; michael@0: michael@0: if (bsi->rdstat[i][mode_idx].brdcost < best_rd) { michael@0: mode_selected = this_mode; michael@0: best_rd = bsi->rdstat[i][mode_idx].brdcost; michael@0: } michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: bsi->rdstat[i][mode_idx].brdcost = michael@0: encode_inter_mb_segment(cpi, x, michael@0: bsi->segment_rd - this_segment_rd, i, michael@0: &bsi->rdstat[i][mode_idx].byrate, michael@0: &bsi->rdstat[i][mode_idx].bdist, michael@0: &bsi->rdstat[i][mode_idx].bsse, michael@0: bsi->rdstat[i][mode_idx].ta, michael@0: bsi->rdstat[i][mode_idx].tl); michael@0: if (bsi->rdstat[i][mode_idx].brdcost < INT64_MAX) { michael@0: bsi->rdstat[i][mode_idx].brdcost += RDCOST(x->rdmult, x->rddiv, michael@0: bsi->rdstat[i][mode_idx].brate, 0); michael@0: bsi->rdstat[i][mode_idx].brate += bsi->rdstat[i][mode_idx].byrate; michael@0: bsi->rdstat[i][mode_idx].eobs = pd->eobs[i]; michael@0: if (num_4x4_blocks_wide > 1) michael@0: bsi->rdstat[i + 1][mode_idx].eobs = pd->eobs[i + 1]; michael@0: if (num_4x4_blocks_high > 1) michael@0: bsi->rdstat[i + 2][mode_idx].eobs = pd->eobs[i + 2]; michael@0: } michael@0: michael@0: if (bsi->rdstat[i][mode_idx].brdcost < best_rd) { michael@0: mode_selected = this_mode; michael@0: best_rd = bsi->rdstat[i][mode_idx].brdcost; michael@0: } michael@0: } /*for each 4x4 mode*/ michael@0: michael@0: if (best_rd == INT64_MAX) { michael@0: int iy, midx; michael@0: for (iy = i + 1; iy < 4; ++iy) michael@0: for (midx = 0; midx < INTER_MODES; ++midx) michael@0: bsi->rdstat[iy][midx].brdcost = INT64_MAX; michael@0: bsi->segment_rd = INT64_MAX; michael@0: return; michael@0: } michael@0: michael@0: mode_idx = INTER_OFFSET(mode_selected); michael@0: vpx_memcpy(t_above, bsi->rdstat[i][mode_idx].ta, sizeof(t_above)); michael@0: vpx_memcpy(t_left, bsi->rdstat[i][mode_idx].tl, sizeof(t_left)); michael@0: michael@0: labels2mode(x, i, mode_selected, &mode_mv[mode_selected], michael@0: &second_mode_mv[mode_selected], frame_mv, seg_mvs[i], michael@0: bsi->ref_mv, bsi->second_ref_mv, x->nmvjointcost, michael@0: x->mvcost, cpi); michael@0: michael@0: br += bsi->rdstat[i][mode_idx].brate; michael@0: bd += bsi->rdstat[i][mode_idx].bdist; michael@0: block_sse += bsi->rdstat[i][mode_idx].bsse; michael@0: segmentyrate += bsi->rdstat[i][mode_idx].byrate; michael@0: this_segment_rd += bsi->rdstat[i][mode_idx].brdcost; michael@0: michael@0: if (this_segment_rd > bsi->segment_rd) { michael@0: int iy, midx; michael@0: for (iy = i + 1; iy < 4; ++iy) michael@0: for (midx = 0; midx < INTER_MODES; ++midx) michael@0: bsi->rdstat[iy][midx].brdcost = INT64_MAX; michael@0: bsi->segment_rd = INT64_MAX; michael@0: return; michael@0: } michael@0: } michael@0: } /* for each label */ 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->sse = block_sse; michael@0: michael@0: // update the coding decisions michael@0: for (i = 0; i < 4; ++i) michael@0: bsi->modes[i] = mi->bmi[i].as_mode; michael@0: } michael@0: michael@0: static int64_t rd_pick_best_mbsegmentation(VP9_COMP *cpi, MACROBLOCK *x, michael@0: const TileInfo *const tile, michael@0: int_mv *best_ref_mv, michael@0: int_mv *second_best_ref_mv, michael@0: int64_t best_rd, michael@0: int *returntotrate, michael@0: int *returnyrate, michael@0: int64_t *returndistortion, michael@0: int *skippable, int64_t *psse, michael@0: int mvthresh, michael@0: int_mv seg_mvs[4][MAX_REF_FRAMES], michael@0: BEST_SEG_INFO *bsi_buf, michael@0: int filter_idx, michael@0: int mi_row, int mi_col) { michael@0: int i; michael@0: BEST_SEG_INFO *bsi = bsi_buf + filter_idx; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: MODE_INFO *mi = xd->mi_8x8[0]; michael@0: MB_MODE_INFO *mbmi = &mi->mbmi; michael@0: int mode_idx; michael@0: michael@0: vp9_zero(*bsi); michael@0: michael@0: bsi->segment_rd = best_rd; michael@0: bsi->ref_mv = best_ref_mv; michael@0: bsi->second_ref_mv = second_best_ref_mv; michael@0: bsi->mvp.as_int = best_ref_mv->as_int; michael@0: bsi->mvthresh = mvthresh; michael@0: michael@0: for (i = 0; i < 4; i++) michael@0: bsi->modes[i] = ZEROMV; michael@0: michael@0: rd_check_segment_txsize(cpi, x, tile, bsi_buf, filter_idx, seg_mvs, michael@0: mi_row, mi_col); michael@0: michael@0: if (bsi->segment_rd > best_rd) michael@0: return INT64_MAX; michael@0: /* set it to the best */ michael@0: for (i = 0; i < 4; i++) { michael@0: mode_idx = INTER_OFFSET(bsi->modes[i]); michael@0: mi->bmi[i].as_mv[0].as_int = bsi->rdstat[i][mode_idx].mvs[0].as_int; michael@0: if (has_second_ref(mbmi)) michael@0: mi->bmi[i].as_mv[1].as_int = bsi->rdstat[i][mode_idx].mvs[1].as_int; michael@0: xd->plane[0].eobs[i] = bsi->rdstat[i][mode_idx].eobs; michael@0: mi->bmi[i].as_mode = bsi->modes[i]; michael@0: } michael@0: michael@0: /* michael@0: * used to set mbmi->mv.as_int michael@0: */ michael@0: *returntotrate = bsi->r; michael@0: *returndistortion = bsi->d; michael@0: *returnyrate = bsi->segment_yrate; michael@0: *skippable = vp9_is_skippable_in_plane(&x->e_mbd, BLOCK_8X8, 0); michael@0: *psse = bsi->sse; michael@0: mbmi->mode = bsi->modes[3]; michael@0: michael@0: return bsi->segment_rd; michael@0: } michael@0: michael@0: static void mv_pred(VP9_COMP *cpi, MACROBLOCK *x, michael@0: uint8_t *ref_y_buffer, int ref_y_stride, michael@0: int ref_frame, BLOCK_SIZE block_size ) { michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; michael@0: int_mv this_mv; michael@0: int i; michael@0: int zero_seen = 0; michael@0: int best_index = 0; michael@0: int best_sad = INT_MAX; michael@0: int this_sad = INT_MAX; michael@0: unsigned int max_mv = 0; michael@0: michael@0: uint8_t *src_y_ptr = x->plane[0].src.buf; michael@0: uint8_t *ref_y_ptr; michael@0: int row_offset, col_offset; michael@0: int num_mv_refs = MAX_MV_REF_CANDIDATES + michael@0: (cpi->sf.adaptive_motion_search && michael@0: cpi->common.show_frame && michael@0: block_size < cpi->sf.max_partition_size); michael@0: michael@0: // Get the sad for each candidate reference mv michael@0: for (i = 0; i < num_mv_refs; i++) { michael@0: this_mv.as_int = (i < MAX_MV_REF_CANDIDATES) ? michael@0: mbmi->ref_mvs[ref_frame][i].as_int : x->pred_mv[ref_frame].as_int; michael@0: michael@0: max_mv = MAX(max_mv, michael@0: MAX(abs(this_mv.as_mv.row), abs(this_mv.as_mv.col)) >> 3); michael@0: // The list is at an end if we see 0 for a second time. michael@0: if (!this_mv.as_int && zero_seen) michael@0: break; michael@0: zero_seen = zero_seen || !this_mv.as_int; michael@0: michael@0: row_offset = this_mv.as_mv.row >> 3; michael@0: col_offset = this_mv.as_mv.col >> 3; michael@0: ref_y_ptr = ref_y_buffer + (ref_y_stride * row_offset) + col_offset; michael@0: michael@0: // Find sad for current vector. michael@0: this_sad = cpi->fn_ptr[block_size].sdf(src_y_ptr, x->plane[0].src.stride, michael@0: ref_y_ptr, ref_y_stride, michael@0: 0x7fffffff); michael@0: michael@0: // Note if it is the best so far. michael@0: if (this_sad < best_sad) { michael@0: best_sad = this_sad; michael@0: best_index = i; michael@0: } michael@0: } michael@0: michael@0: // Note the index of the mv that worked best in the reference list. michael@0: x->mv_best_ref_index[ref_frame] = best_index; michael@0: x->max_mv_context[ref_frame] = max_mv; michael@0: } michael@0: michael@0: static void estimate_ref_frame_costs(VP9_COMP *cpi, int segment_id, michael@0: unsigned int *ref_costs_single, michael@0: unsigned int *ref_costs_comp, michael@0: vp9_prob *comp_mode_p) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCKD *const xd = &cpi->mb.e_mbd; michael@0: int seg_ref_active = vp9_segfeature_active(&cm->seg, segment_id, michael@0: SEG_LVL_REF_FRAME); michael@0: if (seg_ref_active) { michael@0: vpx_memset(ref_costs_single, 0, MAX_REF_FRAMES * sizeof(*ref_costs_single)); michael@0: vpx_memset(ref_costs_comp, 0, MAX_REF_FRAMES * sizeof(*ref_costs_comp)); michael@0: *comp_mode_p = 128; michael@0: } else { michael@0: vp9_prob intra_inter_p = vp9_get_pred_prob_intra_inter(cm, xd); michael@0: vp9_prob comp_inter_p = 128; michael@0: michael@0: if (cm->comp_pred_mode == HYBRID_PREDICTION) { michael@0: comp_inter_p = vp9_get_pred_prob_comp_inter_inter(cm, xd); michael@0: *comp_mode_p = comp_inter_p; michael@0: } else { michael@0: *comp_mode_p = 128; michael@0: } michael@0: michael@0: ref_costs_single[INTRA_FRAME] = vp9_cost_bit(intra_inter_p, 0); michael@0: michael@0: if (cm->comp_pred_mode != COMP_PREDICTION_ONLY) { michael@0: vp9_prob ref_single_p1 = vp9_get_pred_prob_single_ref_p1(cm, xd); michael@0: vp9_prob ref_single_p2 = vp9_get_pred_prob_single_ref_p2(cm, xd); michael@0: unsigned int base_cost = vp9_cost_bit(intra_inter_p, 1); michael@0: michael@0: if (cm->comp_pred_mode == HYBRID_PREDICTION) michael@0: base_cost += vp9_cost_bit(comp_inter_p, 0); michael@0: michael@0: ref_costs_single[LAST_FRAME] = ref_costs_single[GOLDEN_FRAME] = michael@0: ref_costs_single[ALTREF_FRAME] = base_cost; michael@0: ref_costs_single[LAST_FRAME] += vp9_cost_bit(ref_single_p1, 0); michael@0: ref_costs_single[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p1, 1); michael@0: ref_costs_single[ALTREF_FRAME] += vp9_cost_bit(ref_single_p1, 1); michael@0: ref_costs_single[GOLDEN_FRAME] += vp9_cost_bit(ref_single_p2, 0); michael@0: ref_costs_single[ALTREF_FRAME] += vp9_cost_bit(ref_single_p2, 1); michael@0: } else { michael@0: ref_costs_single[LAST_FRAME] = 512; michael@0: ref_costs_single[GOLDEN_FRAME] = 512; michael@0: ref_costs_single[ALTREF_FRAME] = 512; michael@0: } michael@0: if (cm->comp_pred_mode != SINGLE_PREDICTION_ONLY) { michael@0: vp9_prob ref_comp_p = vp9_get_pred_prob_comp_ref_p(cm, xd); michael@0: unsigned int base_cost = vp9_cost_bit(intra_inter_p, 1); michael@0: michael@0: if (cm->comp_pred_mode == HYBRID_PREDICTION) michael@0: base_cost += vp9_cost_bit(comp_inter_p, 1); michael@0: michael@0: ref_costs_comp[LAST_FRAME] = base_cost + vp9_cost_bit(ref_comp_p, 0); michael@0: ref_costs_comp[GOLDEN_FRAME] = base_cost + vp9_cost_bit(ref_comp_p, 1); michael@0: } else { michael@0: ref_costs_comp[LAST_FRAME] = 512; michael@0: ref_costs_comp[GOLDEN_FRAME] = 512; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void store_coding_context(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, michael@0: int mode_index, michael@0: int_mv *ref_mv, michael@0: int_mv *second_ref_mv, michael@0: int64_t comp_pred_diff[NB_PREDICTION_TYPES], michael@0: int64_t tx_size_diff[TX_MODES], michael@0: int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS]) { michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: michael@0: // Take a snapshot of the coding context so it can be michael@0: // restored if we decide to encode this way michael@0: ctx->skip = x->skip; michael@0: ctx->best_mode_index = mode_index; michael@0: ctx->mic = *xd->mi_8x8[0]; michael@0: michael@0: ctx->best_ref_mv.as_int = ref_mv->as_int; michael@0: ctx->second_best_ref_mv.as_int = second_ref_mv->as_int; michael@0: michael@0: ctx->single_pred_diff = (int)comp_pred_diff[SINGLE_PREDICTION_ONLY]; michael@0: ctx->comp_pred_diff = (int)comp_pred_diff[COMP_PREDICTION_ONLY]; michael@0: ctx->hybrid_pred_diff = (int)comp_pred_diff[HYBRID_PREDICTION]; michael@0: michael@0: vpx_memcpy(ctx->tx_rd_diff, tx_size_diff, sizeof(ctx->tx_rd_diff)); michael@0: vpx_memcpy(ctx->best_filter_diff, best_filter_diff, michael@0: sizeof(*best_filter_diff) * SWITCHABLE_FILTER_CONTEXTS); michael@0: } michael@0: michael@0: static void setup_pred_block(const MACROBLOCKD *xd, michael@0: struct buf_2d dst[MAX_MB_PLANE], michael@0: const YV12_BUFFER_CONFIG *src, michael@0: int mi_row, int mi_col, michael@0: const struct scale_factors *scale, michael@0: const struct scale_factors *scale_uv) { michael@0: int i; michael@0: michael@0: dst[0].buf = src->y_buffer; michael@0: dst[0].stride = src->y_stride; michael@0: dst[1].buf = src->u_buffer; michael@0: dst[2].buf = src->v_buffer; michael@0: dst[1].stride = dst[2].stride = src->uv_stride; michael@0: #if CONFIG_ALPHA michael@0: dst[3].buf = src->alpha_buffer; michael@0: dst[3].stride = src->alpha_stride; michael@0: #endif michael@0: michael@0: // TODO(jkoleszar): Make scale factors per-plane data michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: setup_pred_plane(dst + i, dst[i].buf, dst[i].stride, mi_row, mi_col, michael@0: i ? scale_uv : scale, michael@0: xd->plane[i].subsampling_x, xd->plane[i].subsampling_y); michael@0: } michael@0: } michael@0: michael@0: static void setup_buffer_inter(VP9_COMP *cpi, MACROBLOCK *x, michael@0: const TileInfo *const tile, michael@0: int idx, MV_REFERENCE_FRAME frame_type, michael@0: BLOCK_SIZE block_size, michael@0: int mi_row, int mi_col, michael@0: int_mv frame_nearest_mv[MAX_REF_FRAMES], michael@0: int_mv frame_near_mv[MAX_REF_FRAMES], michael@0: struct buf_2d yv12_mb[4][MAX_MB_PLANE], michael@0: struct scale_factors scale[MAX_REF_FRAMES]) { michael@0: VP9_COMMON *cm = &cpi->common; michael@0: YV12_BUFFER_CONFIG *yv12 = &cm->yv12_fb[cpi->common.ref_frame_map[idx]]; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; michael@0: michael@0: // set up scaling factors michael@0: scale[frame_type] = cpi->common.active_ref_scale[frame_type - 1]; michael@0: michael@0: scale[frame_type].sfc->set_scaled_offsets(&scale[frame_type], michael@0: mi_row * MI_SIZE, mi_col * MI_SIZE); michael@0: michael@0: // TODO(jkoleszar): Is the UV buffer ever used here? If so, need to make this michael@0: // use the UV scaling factors. michael@0: setup_pred_block(xd, yv12_mb[frame_type], yv12, mi_row, mi_col, michael@0: &scale[frame_type], &scale[frame_type]); michael@0: michael@0: // Gets an initial list of candidate vectors from neighbours and orders them michael@0: vp9_find_mv_refs(cm, xd, tile, xd->mi_8x8[0], michael@0: xd->last_mi, michael@0: frame_type, michael@0: mbmi->ref_mvs[frame_type], mi_row, mi_col); michael@0: michael@0: // Candidate refinement carried out at encoder and decoder michael@0: vp9_find_best_ref_mvs(xd, cm->allow_high_precision_mv, michael@0: mbmi->ref_mvs[frame_type], michael@0: &frame_nearest_mv[frame_type], michael@0: &frame_near_mv[frame_type]); michael@0: michael@0: // Further refinement that is encode side only to test the top few candidates michael@0: // in full and choose the best as the centre point for subsequent searches. michael@0: // The current implementation doesn't support scaling. michael@0: if (!vp9_is_scaled(scale[frame_type].sfc) && block_size >= BLOCK_8X8) michael@0: mv_pred(cpi, x, yv12_mb[frame_type][0].buf, yv12->y_stride, michael@0: frame_type, block_size); michael@0: } michael@0: michael@0: static YV12_BUFFER_CONFIG *get_scaled_ref_frame(VP9_COMP *cpi, int ref_frame) { michael@0: YV12_BUFFER_CONFIG *scaled_ref_frame = NULL; michael@0: int fb = get_ref_frame_idx(cpi, ref_frame); michael@0: int fb_scale = get_scale_ref_frame_idx(cpi, ref_frame); michael@0: if (cpi->scaled_ref_idx[fb_scale] != cpi->common.ref_frame_map[fb]) michael@0: scaled_ref_frame = &cpi->common.yv12_fb[cpi->scaled_ref_idx[fb_scale]]; michael@0: return scaled_ref_frame; michael@0: } michael@0: michael@0: static INLINE int get_switchable_rate(const MACROBLOCK *x) { michael@0: const MACROBLOCKD *const xd = &x->e_mbd; michael@0: const MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; michael@0: const int ctx = vp9_get_pred_context_switchable_interp(xd); michael@0: return SWITCHABLE_INTERP_RATE_FACTOR * michael@0: x->switchable_interp_costs[ctx][mbmi->interp_filter]; michael@0: } michael@0: michael@0: static void single_motion_search(VP9_COMP *cpi, MACROBLOCK *x, michael@0: const TileInfo *const tile, michael@0: BLOCK_SIZE bsize, michael@0: int mi_row, int mi_col, michael@0: int_mv *tmp_mv, int *rate_mv) { michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: VP9_COMMON *cm = &cpi->common; michael@0: MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; michael@0: struct buf_2d backup_yv12[MAX_MB_PLANE] = {{0}}; michael@0: int bestsme = INT_MAX; michael@0: int further_steps, step_param; michael@0: int sadpb = x->sadperbit16; michael@0: int_mv mvp_full; michael@0: int ref = mbmi->ref_frame[0]; michael@0: int_mv ref_mv = mbmi->ref_mvs[ref][0]; michael@0: const BLOCK_SIZE block_size = get_plane_block_size(bsize, &xd->plane[0]); 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: YV12_BUFFER_CONFIG *scaled_ref_frame = get_scaled_ref_frame(cpi, ref); michael@0: michael@0: if (scaled_ref_frame) { michael@0: int i; michael@0: // Swap out the reference frame for a version that's been scaled to michael@0: // match the resolution of the current frame, allowing the existing michael@0: // motion search code to be used without additional modifications. michael@0: for (i = 0; i < MAX_MB_PLANE; i++) michael@0: backup_yv12[i] = xd->plane[i].pre[0]; michael@0: michael@0: setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL); michael@0: } michael@0: michael@0: vp9_clamp_mv_min_max(x, &ref_mv.as_mv); michael@0: michael@0: // Adjust search parameters based on small partitions' result. michael@0: if (x->fast_ms) { michael@0: // && abs(mvp_full.as_mv.row - x->pred_mv.as_mv.row) < 24 && michael@0: // abs(mvp_full.as_mv.col - x->pred_mv.as_mv.col) < 24) { michael@0: // adjust search range michael@0: step_param = 6; michael@0: if (x->fast_ms > 1) michael@0: step_param = 8; michael@0: michael@0: // Get prediction MV. michael@0: mvp_full.as_int = x->pred_mv[ref].as_int; michael@0: michael@0: // Adjust MV sign if needed. michael@0: if (cm->ref_frame_sign_bias[ref]) { michael@0: mvp_full.as_mv.col *= -1; michael@0: mvp_full.as_mv.row *= -1; michael@0: } michael@0: } else { michael@0: // Work out the size of the first step in the mv step search. michael@0: // 0 here is maximum length first step. 1 is MAX >> 1 etc. michael@0: if (cpi->sf.auto_mv_step_size && cpi->common.show_frame) { michael@0: // Take wtd average of the step_params based on the last frame's michael@0: // max mv magnitude and that based on the best ref mvs of the current michael@0: // block for the given reference. michael@0: step_param = (vp9_init_search_range(cpi, x->max_mv_context[ref]) + michael@0: cpi->mv_step_param) >> 1; michael@0: } else { michael@0: step_param = cpi->mv_step_param; michael@0: } michael@0: } michael@0: michael@0: if (cpi->sf.adaptive_motion_search && bsize < BLOCK_64X64 && michael@0: cpi->common.show_frame) { michael@0: int boffset = 2 * (b_width_log2(BLOCK_64X64) - MIN(b_height_log2(bsize), michael@0: b_width_log2(bsize))); michael@0: step_param = MAX(step_param, boffset); michael@0: } michael@0: michael@0: mvp_full.as_int = x->mv_best_ref_index[ref] < MAX_MV_REF_CANDIDATES ? michael@0: mbmi->ref_mvs[ref][x->mv_best_ref_index[ref]].as_int : michael@0: x->pred_mv[ref].as_int; michael@0: michael@0: mvp_full.as_mv.col >>= 3; michael@0: mvp_full.as_mv.row >>= 3; michael@0: michael@0: // Further step/diamond searches as necessary michael@0: further_steps = (cpi->sf.max_step_search_steps - 1) - step_param; michael@0: michael@0: if (cpi->sf.search_method == HEX) { michael@0: bestsme = vp9_hex_search(x, &mvp_full.as_mv, michael@0: step_param, michael@0: sadpb, 1, michael@0: &cpi->fn_ptr[block_size], 1, michael@0: &ref_mv.as_mv, &tmp_mv->as_mv); michael@0: } else if (cpi->sf.search_method == SQUARE) { michael@0: bestsme = vp9_square_search(x, &mvp_full.as_mv, michael@0: step_param, michael@0: sadpb, 1, michael@0: &cpi->fn_ptr[block_size], 1, michael@0: &ref_mv.as_mv, &tmp_mv->as_mv); michael@0: } else if (cpi->sf.search_method == BIGDIA) { michael@0: bestsme = vp9_bigdia_search(x, &mvp_full.as_mv, michael@0: step_param, michael@0: sadpb, 1, michael@0: &cpi->fn_ptr[block_size], 1, michael@0: &ref_mv.as_mv, &tmp_mv->as_mv); michael@0: } else { michael@0: bestsme = vp9_full_pixel_diamond(cpi, x, &mvp_full, step_param, michael@0: sadpb, further_steps, 1, michael@0: &cpi->fn_ptr[block_size], michael@0: &ref_mv, tmp_mv); 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: int dis; /* TODO: use dis in distortion calculation later. */ michael@0: unsigned int sse; michael@0: cpi->find_fractional_mv_step(x, &tmp_mv->as_mv, &ref_mv.as_mv, michael@0: cm->allow_high_precision_mv, michael@0: x->errorperbit, michael@0: &cpi->fn_ptr[block_size], michael@0: 0, cpi->sf.subpel_iters_per_step, michael@0: x->nmvjointcost, x->mvcost, michael@0: &dis, &sse); michael@0: } michael@0: *rate_mv = vp9_mv_bit_cost(&tmp_mv->as_mv, &ref_mv.as_mv, michael@0: x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); michael@0: michael@0: if (cpi->sf.adaptive_motion_search && cpi->common.show_frame) michael@0: x->pred_mv[ref].as_int = tmp_mv->as_int; michael@0: michael@0: if (scaled_ref_frame) { michael@0: int i; michael@0: for (i = 0; i < MAX_MB_PLANE; i++) michael@0: xd->plane[i].pre[0] = backup_yv12[i]; michael@0: } michael@0: } michael@0: michael@0: static void joint_motion_search(VP9_COMP *cpi, MACROBLOCK *x, michael@0: BLOCK_SIZE bsize, michael@0: int_mv *frame_mv, michael@0: int mi_row, int mi_col, michael@0: int_mv single_newmv[MAX_REF_FRAMES], michael@0: int *rate_mv) { michael@0: int pw = 4 << b_width_log2(bsize), ph = 4 << b_height_log2(bsize); michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; michael@0: const int refs[2] = { mbmi->ref_frame[0], michael@0: mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1] }; michael@0: int_mv ref_mv[2]; michael@0: const BLOCK_SIZE block_size = get_plane_block_size(bsize, &xd->plane[0]); michael@0: int ite, ref; michael@0: // Prediction buffer from second frame. michael@0: uint8_t *second_pred = vpx_memalign(16, pw * ph * sizeof(uint8_t)); michael@0: michael@0: // Do joint motion search in compound mode to get more accurate mv. michael@0: struct buf_2d backup_yv12[2][MAX_MB_PLANE]; michael@0: struct buf_2d scaled_first_yv12 = xd->plane[0].pre[0]; michael@0: int last_besterr[2] = {INT_MAX, INT_MAX}; michael@0: YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = { michael@0: get_scaled_ref_frame(cpi, mbmi->ref_frame[0]), michael@0: get_scaled_ref_frame(cpi, mbmi->ref_frame[1]) michael@0: }; michael@0: michael@0: for (ref = 0; ref < 2; ++ref) { michael@0: ref_mv[ref] = mbmi->ref_mvs[refs[ref]][0]; michael@0: michael@0: if (scaled_ref_frame[ref]) { michael@0: int i; michael@0: // Swap out the reference frame for a version that's been scaled to michael@0: // match the resolution of the current frame, allowing the existing michael@0: // motion search code to be used without additional modifications. michael@0: for (i = 0; i < MAX_MB_PLANE; i++) michael@0: backup_yv12[ref][i] = xd->plane[i].pre[ref]; michael@0: setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col, NULL); michael@0: } michael@0: michael@0: xd->scale_factor[ref].sfc->set_scaled_offsets(&xd->scale_factor[ref], michael@0: mi_row, mi_col); michael@0: frame_mv[refs[ref]].as_int = single_newmv[refs[ref]].as_int; michael@0: } michael@0: michael@0: // Allow joint search multiple times iteratively for each ref frame michael@0: // and break out the search loop if it couldn't find better mv. michael@0: for (ite = 0; ite < 4; ite++) { michael@0: struct buf_2d ref_yv12[2]; michael@0: int bestsme = INT_MAX; michael@0: int sadpb = x->sadperbit16; michael@0: int_mv tmp_mv; michael@0: int search_range = 3; 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: int id = ite % 2; michael@0: michael@0: // Initialized here because of compiler problem in Visual Studio. michael@0: ref_yv12[0] = xd->plane[0].pre[0]; michael@0: ref_yv12[1] = xd->plane[0].pre[1]; michael@0: michael@0: // Get pred block from second frame. michael@0: vp9_build_inter_predictor(ref_yv12[!id].buf, michael@0: ref_yv12[!id].stride, michael@0: second_pred, pw, michael@0: &frame_mv[refs[!id]].as_mv, michael@0: &xd->scale_factor[!id], michael@0: pw, ph, 0, michael@0: &xd->subpix, MV_PRECISION_Q3); michael@0: michael@0: // Compound motion search on first ref frame. michael@0: if (id) michael@0: xd->plane[0].pre[0] = ref_yv12[id]; michael@0: vp9_clamp_mv_min_max(x, &ref_mv[id].as_mv); michael@0: michael@0: // Use mv result from single mode as mvp. michael@0: tmp_mv.as_int = frame_mv[refs[id]].as_int; michael@0: michael@0: tmp_mv.as_mv.col >>= 3; michael@0: tmp_mv.as_mv.row >>= 3; michael@0: michael@0: // Small-range full-pixel motion search michael@0: bestsme = vp9_refining_search_8p_c(x, &tmp_mv, sadpb, michael@0: search_range, michael@0: &cpi->fn_ptr[block_size], michael@0: x->nmvjointcost, x->mvcost, michael@0: &ref_mv[id], second_pred, michael@0: pw, ph); 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: int dis; /* TODO: use dis in distortion calculation later. */ michael@0: unsigned int sse; michael@0: michael@0: bestsme = cpi->find_fractional_mv_step_comp( michael@0: x, &tmp_mv.as_mv, michael@0: &ref_mv[id].as_mv, michael@0: cpi->common.allow_high_precision_mv, michael@0: x->errorperbit, michael@0: &cpi->fn_ptr[block_size], michael@0: 0, cpi->sf.subpel_iters_per_step, michael@0: x->nmvjointcost, x->mvcost, michael@0: &dis, &sse, second_pred, michael@0: pw, ph); michael@0: } michael@0: michael@0: if (id) michael@0: xd->plane[0].pre[0] = scaled_first_yv12; michael@0: michael@0: if (bestsme < last_besterr[id]) { michael@0: frame_mv[refs[id]].as_int = tmp_mv.as_int; michael@0: last_besterr[id] = bestsme; michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: *rate_mv = 0; michael@0: michael@0: for (ref = 0; ref < 2; ++ref) { michael@0: if (scaled_ref_frame[ref]) { michael@0: // restore the predictor michael@0: int i; michael@0: for (i = 0; i < MAX_MB_PLANE; i++) michael@0: xd->plane[i].pre[ref] = backup_yv12[ref][i]; michael@0: } michael@0: michael@0: *rate_mv += vp9_mv_bit_cost(&frame_mv[refs[ref]].as_mv, michael@0: &mbmi->ref_mvs[refs[ref]][0].as_mv, michael@0: x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); michael@0: } michael@0: michael@0: vpx_free(second_pred); michael@0: } michael@0: michael@0: static int64_t handle_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, michael@0: const TileInfo *const tile, michael@0: BLOCK_SIZE bsize, michael@0: int64_t txfm_cache[], michael@0: int *rate2, int64_t *distortion, michael@0: int *skippable, michael@0: int *rate_y, int64_t *distortion_y, michael@0: int *rate_uv, int64_t *distortion_uv, michael@0: int *mode_excluded, int *disable_skip, michael@0: INTERPOLATION_TYPE *best_filter, michael@0: int_mv (*mode_mv)[MAX_REF_FRAMES], michael@0: int mi_row, int mi_col, michael@0: int_mv single_newmv[MAX_REF_FRAMES], michael@0: int64_t *psse, michael@0: const int64_t ref_best_rd) { michael@0: VP9_COMMON *cm = &cpi->common; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; michael@0: const int is_comp_pred = has_second_ref(mbmi); michael@0: const int num_refs = is_comp_pred ? 2 : 1; michael@0: const int this_mode = mbmi->mode; michael@0: int_mv *frame_mv = mode_mv[this_mode]; michael@0: int i; michael@0: int refs[2] = { mbmi->ref_frame[0], michael@0: (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) }; michael@0: int_mv cur_mv[2]; michael@0: int64_t this_rd = 0; michael@0: DECLARE_ALIGNED_ARRAY(16, uint8_t, tmp_buf, MAX_MB_PLANE * 64 * 64); michael@0: int pred_exists = 0; michael@0: int intpel_mv; michael@0: int64_t rd, best_rd = INT64_MAX; michael@0: int best_needs_copy = 0; michael@0: uint8_t *orig_dst[MAX_MB_PLANE]; michael@0: int orig_dst_stride[MAX_MB_PLANE]; michael@0: int rs = 0; michael@0: michael@0: if (is_comp_pred) { michael@0: if (frame_mv[refs[0]].as_int == INVALID_MV || michael@0: frame_mv[refs[1]].as_int == INVALID_MV) michael@0: return INT64_MAX; michael@0: } michael@0: michael@0: if (this_mode == NEWMV) { michael@0: int rate_mv; michael@0: if (is_comp_pred) { michael@0: // Initialize mv using single prediction mode result. michael@0: frame_mv[refs[0]].as_int = single_newmv[refs[0]].as_int; michael@0: frame_mv[refs[1]].as_int = single_newmv[refs[1]].as_int; michael@0: michael@0: if (cpi->sf.comp_inter_joint_search_thresh <= bsize) { michael@0: joint_motion_search(cpi, x, bsize, frame_mv, michael@0: mi_row, mi_col, single_newmv, &rate_mv); michael@0: } else { michael@0: rate_mv = vp9_mv_bit_cost(&frame_mv[refs[0]].as_mv, michael@0: &mbmi->ref_mvs[refs[0]][0].as_mv, michael@0: x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); michael@0: rate_mv += vp9_mv_bit_cost(&frame_mv[refs[1]].as_mv, michael@0: &mbmi->ref_mvs[refs[1]][0].as_mv, michael@0: x->nmvjointcost, x->mvcost, MV_COST_WEIGHT); michael@0: } michael@0: *rate2 += rate_mv; michael@0: } else { michael@0: int_mv tmp_mv; michael@0: single_motion_search(cpi, x, tile, bsize, mi_row, mi_col, michael@0: &tmp_mv, &rate_mv); michael@0: *rate2 += rate_mv; michael@0: frame_mv[refs[0]].as_int = michael@0: xd->mi_8x8[0]->bmi[0].as_mv[0].as_int = tmp_mv.as_int; michael@0: single_newmv[refs[0]].as_int = tmp_mv.as_int; michael@0: } michael@0: } michael@0: michael@0: // if we're near/nearest and mv == 0,0, compare to zeromv michael@0: if ((this_mode == NEARMV || this_mode == NEARESTMV || this_mode == ZEROMV) && michael@0: frame_mv[refs[0]].as_int == 0 && michael@0: !vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) && michael@0: (num_refs == 1 || frame_mv[refs[1]].as_int == 0)) { michael@0: int rfc = mbmi->mode_context[mbmi->ref_frame[0]]; michael@0: int c1 = cost_mv_ref(cpi, NEARMV, rfc); michael@0: int c2 = cost_mv_ref(cpi, NEARESTMV, rfc); michael@0: int c3 = cost_mv_ref(cpi, ZEROMV, rfc); michael@0: michael@0: if (this_mode == NEARMV) { michael@0: if (c1 > c3) michael@0: return INT64_MAX; michael@0: } else if (this_mode == NEARESTMV) { michael@0: if (c2 > c3) michael@0: return INT64_MAX; michael@0: } else { michael@0: assert(this_mode == ZEROMV); michael@0: if (num_refs == 1) { michael@0: if ((c3 >= c2 && michael@0: mode_mv[NEARESTMV][mbmi->ref_frame[0]].as_int == 0) || michael@0: (c3 >= c1 && michael@0: mode_mv[NEARMV][mbmi->ref_frame[0]].as_int == 0)) michael@0: return INT64_MAX; michael@0: } else { michael@0: if ((c3 >= c2 && michael@0: mode_mv[NEARESTMV][mbmi->ref_frame[0]].as_int == 0 && michael@0: mode_mv[NEARESTMV][mbmi->ref_frame[1]].as_int == 0) || michael@0: (c3 >= c1 && michael@0: mode_mv[NEARMV][mbmi->ref_frame[0]].as_int == 0 && michael@0: mode_mv[NEARMV][mbmi->ref_frame[1]].as_int == 0)) michael@0: return INT64_MAX; michael@0: } michael@0: } michael@0: } michael@0: michael@0: for (i = 0; i < num_refs; ++i) { michael@0: cur_mv[i] = frame_mv[refs[i]]; michael@0: // Clip "next_nearest" so that it does not extend to far out of image michael@0: if (this_mode != NEWMV) michael@0: clamp_mv2(&cur_mv[i].as_mv, xd); michael@0: michael@0: if (mv_check_bounds(x, &cur_mv[i])) michael@0: return INT64_MAX; michael@0: mbmi->mv[i].as_int = cur_mv[i].as_int; michael@0: } michael@0: michael@0: // do first prediction into the destination buffer. Do the next michael@0: // prediction into a temporary buffer. Then keep track of which one michael@0: // of these currently holds the best predictor, and use the other michael@0: // one for future predictions. In the end, copy from tmp_buf to michael@0: // dst if necessary. michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: orig_dst[i] = xd->plane[i].dst.buf; michael@0: orig_dst_stride[i] = xd->plane[i].dst.stride; michael@0: } michael@0: michael@0: /* We don't include the cost of the second reference here, because there michael@0: * are only three options: Last/Golden, ARF/Last or Golden/ARF, or in other michael@0: * words if you present them in that order, the second one is always known michael@0: * if the first is known */ michael@0: *rate2 += cost_mv_ref(cpi, this_mode, michael@0: mbmi->mode_context[mbmi->ref_frame[0]]); michael@0: michael@0: if (!(*mode_excluded)) { michael@0: if (is_comp_pred) { michael@0: *mode_excluded = (cpi->common.comp_pred_mode == SINGLE_PREDICTION_ONLY); michael@0: } else { michael@0: *mode_excluded = (cpi->common.comp_pred_mode == COMP_PREDICTION_ONLY); michael@0: } michael@0: } michael@0: michael@0: pred_exists = 0; michael@0: // Are all MVs integer pel for Y and UV michael@0: intpel_mv = (mbmi->mv[0].as_mv.row & 15) == 0 && michael@0: (mbmi->mv[0].as_mv.col & 15) == 0; michael@0: if (is_comp_pred) michael@0: intpel_mv &= (mbmi->mv[1].as_mv.row & 15) == 0 && michael@0: (mbmi->mv[1].as_mv.col & 15) == 0; michael@0: // Search for best switchable filter by checking the variance of michael@0: // pred error irrespective of whether the filter will be used michael@0: if (cm->mcomp_filter_type != BILINEAR) { michael@0: *best_filter = EIGHTTAP; michael@0: if (x->source_variance < michael@0: cpi->sf.disable_filter_search_var_thresh) { michael@0: *best_filter = EIGHTTAP; michael@0: vp9_zero(cpi->rd_filter_cache); michael@0: } else { michael@0: int i, newbest; michael@0: int tmp_rate_sum = 0; michael@0: int64_t tmp_dist_sum = 0; michael@0: michael@0: cpi->rd_filter_cache[SWITCHABLE_FILTERS] = INT64_MAX; michael@0: for (i = 0; i < SWITCHABLE_FILTERS; ++i) { michael@0: int j; michael@0: int64_t rs_rd; michael@0: mbmi->interp_filter = i; michael@0: vp9_setup_interp_filters(xd, mbmi->interp_filter, cm); michael@0: rs = get_switchable_rate(x); michael@0: rs_rd = RDCOST(x->rdmult, x->rddiv, rs, 0); michael@0: michael@0: if (i > 0 && intpel_mv) { michael@0: cpi->rd_filter_cache[i] = RDCOST(x->rdmult, x->rddiv, michael@0: tmp_rate_sum, tmp_dist_sum); michael@0: cpi->rd_filter_cache[SWITCHABLE_FILTERS] = michael@0: MIN(cpi->rd_filter_cache[SWITCHABLE_FILTERS], michael@0: cpi->rd_filter_cache[i] + rs_rd); michael@0: rd = cpi->rd_filter_cache[i]; michael@0: if (cm->mcomp_filter_type == SWITCHABLE) michael@0: rd += rs_rd; michael@0: } else { michael@0: int rate_sum = 0; michael@0: int64_t dist_sum = 0; michael@0: if ((cm->mcomp_filter_type == SWITCHABLE && michael@0: (!i || best_needs_copy)) || michael@0: (cm->mcomp_filter_type != SWITCHABLE && michael@0: (cm->mcomp_filter_type == mbmi->interp_filter || michael@0: (i == 0 && intpel_mv)))) { michael@0: for (j = 0; j < MAX_MB_PLANE; j++) { michael@0: xd->plane[j].dst.buf = orig_dst[j]; michael@0: xd->plane[j].dst.stride = orig_dst_stride[j]; michael@0: } michael@0: } else { michael@0: for (j = 0; j < MAX_MB_PLANE; j++) { michael@0: xd->plane[j].dst.buf = tmp_buf + j * 64 * 64; michael@0: xd->plane[j].dst.stride = 64; michael@0: } michael@0: } michael@0: vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize); michael@0: model_rd_for_sb(cpi, bsize, x, xd, &rate_sum, &dist_sum); michael@0: cpi->rd_filter_cache[i] = RDCOST(x->rdmult, x->rddiv, michael@0: rate_sum, dist_sum); michael@0: cpi->rd_filter_cache[SWITCHABLE_FILTERS] = michael@0: MIN(cpi->rd_filter_cache[SWITCHABLE_FILTERS], michael@0: cpi->rd_filter_cache[i] + rs_rd); michael@0: rd = cpi->rd_filter_cache[i]; michael@0: if (cm->mcomp_filter_type == SWITCHABLE) michael@0: rd += rs_rd; michael@0: if (i == 0 && intpel_mv) { michael@0: tmp_rate_sum = rate_sum; michael@0: tmp_dist_sum = dist_sum; michael@0: } michael@0: } michael@0: if (i == 0 && cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) { michael@0: if (rd / 2 > ref_best_rd) { michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: xd->plane[i].dst.buf = orig_dst[i]; michael@0: xd->plane[i].dst.stride = orig_dst_stride[i]; michael@0: } michael@0: return INT64_MAX; michael@0: } michael@0: } michael@0: newbest = i == 0 || rd < best_rd; michael@0: michael@0: if (newbest) { michael@0: best_rd = rd; michael@0: *best_filter = mbmi->interp_filter; michael@0: if (cm->mcomp_filter_type == SWITCHABLE && i && !intpel_mv) michael@0: best_needs_copy = !best_needs_copy; michael@0: } michael@0: michael@0: if ((cm->mcomp_filter_type == SWITCHABLE && newbest) || michael@0: (cm->mcomp_filter_type != SWITCHABLE && michael@0: cm->mcomp_filter_type == mbmi->interp_filter)) { michael@0: pred_exists = 1; michael@0: } michael@0: } michael@0: michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: xd->plane[i].dst.buf = orig_dst[i]; michael@0: xd->plane[i].dst.stride = orig_dst_stride[i]; michael@0: } michael@0: } michael@0: } michael@0: // Set the appropriate filter michael@0: mbmi->interp_filter = cm->mcomp_filter_type != SWITCHABLE ? michael@0: cm->mcomp_filter_type : *best_filter; michael@0: vp9_setup_interp_filters(xd, mbmi->interp_filter, cm); michael@0: rs = cm->mcomp_filter_type == SWITCHABLE ? get_switchable_rate(x) : 0; michael@0: michael@0: if (pred_exists) { michael@0: if (best_needs_copy) { michael@0: // again temporarily set the buffers to local memory to prevent a memcpy michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: xd->plane[i].dst.buf = tmp_buf + i * 64 * 64; michael@0: xd->plane[i].dst.stride = 64; michael@0: } michael@0: } michael@0: } else { michael@0: // Handles the special case when a filter that is not in the michael@0: // switchable list (ex. bilinear, 6-tap) is indicated at the frame level michael@0: vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize); michael@0: } michael@0: michael@0: michael@0: if (cpi->sf.use_rd_breakout && ref_best_rd < INT64_MAX) { michael@0: int tmp_rate; michael@0: int64_t tmp_dist; michael@0: model_rd_for_sb(cpi, bsize, x, xd, &tmp_rate, &tmp_dist); michael@0: rd = RDCOST(x->rdmult, x->rddiv, rs + tmp_rate, tmp_dist); michael@0: // if current pred_error modeled rd is substantially more than the best michael@0: // so far, do not bother doing full rd michael@0: if (rd / 2 > ref_best_rd) { michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: xd->plane[i].dst.buf = orig_dst[i]; michael@0: xd->plane[i].dst.stride = orig_dst_stride[i]; michael@0: } michael@0: return INT64_MAX; michael@0: } michael@0: } michael@0: michael@0: if (cpi->common.mcomp_filter_type == SWITCHABLE) michael@0: *rate2 += get_switchable_rate(x); michael@0: michael@0: if (!is_comp_pred && cpi->enable_encode_breakout) { michael@0: if (cpi->active_map_enabled && x->active_ptr[0] == 0) michael@0: x->skip = 1; michael@0: else if (x->encode_breakout) { michael@0: const BLOCK_SIZE y_size = get_plane_block_size(bsize, &xd->plane[0]); michael@0: const BLOCK_SIZE uv_size = get_plane_block_size(bsize, &xd->plane[1]); michael@0: unsigned int var, sse; michael@0: // Skipping threshold for ac. michael@0: unsigned int thresh_ac; michael@0: // The encode_breakout input michael@0: unsigned int encode_breakout = x->encode_breakout << 4; michael@0: unsigned int max_thresh = 36000; michael@0: michael@0: // Use extreme low threshold for static frames to limit skipping. michael@0: if (cpi->enable_encode_breakout == 2) michael@0: max_thresh = 128; michael@0: michael@0: // Calculate threshold according to dequant value. michael@0: thresh_ac = (xd->plane[0].dequant[1] * xd->plane[0].dequant[1]) / 9; michael@0: michael@0: // Use encode_breakout input if it is bigger than internal threshold. michael@0: if (thresh_ac < encode_breakout) michael@0: thresh_ac = encode_breakout; michael@0: michael@0: // Set a maximum for threshold to avoid big PSNR loss in low bitrate case. michael@0: if (thresh_ac > max_thresh) michael@0: thresh_ac = max_thresh; michael@0: michael@0: var = cpi->fn_ptr[y_size].vf(x->plane[0].src.buf, x->plane[0].src.stride, michael@0: xd->plane[0].dst.buf, michael@0: xd->plane[0].dst.stride, &sse); michael@0: michael@0: // Adjust threshold according to partition size. michael@0: thresh_ac >>= 8 - (b_width_log2_lookup[bsize] + michael@0: b_height_log2_lookup[bsize]); michael@0: michael@0: // Y skipping condition checking michael@0: if (sse < thresh_ac || sse == 0) { michael@0: // Skipping threshold for dc michael@0: unsigned int thresh_dc; michael@0: michael@0: thresh_dc = (xd->plane[0].dequant[0] * xd->plane[0].dequant[0] >> 6); michael@0: michael@0: // dc skipping checking michael@0: if ((sse - var) < thresh_dc || sse == var) { michael@0: unsigned int sse_u, sse_v; michael@0: unsigned int var_u, var_v; michael@0: michael@0: var_u = cpi->fn_ptr[uv_size].vf(x->plane[1].src.buf, michael@0: x->plane[1].src.stride, michael@0: xd->plane[1].dst.buf, michael@0: xd->plane[1].dst.stride, &sse_u); michael@0: michael@0: // U skipping condition checking michael@0: if ((sse_u * 4 < thresh_ac || sse_u == 0) && michael@0: (sse_u - var_u < thresh_dc || sse_u == var_u)) { michael@0: var_v = cpi->fn_ptr[uv_size].vf(x->plane[2].src.buf, michael@0: x->plane[2].src.stride, michael@0: xd->plane[2].dst.buf, michael@0: xd->plane[2].dst.stride, &sse_v); michael@0: michael@0: // V skipping condition checking michael@0: if ((sse_v * 4 < thresh_ac || sse_v == 0) && michael@0: (sse_v - var_v < thresh_dc || sse_v == var_v)) { michael@0: x->skip = 1; michael@0: michael@0: // The cost of skip bit needs to be added. michael@0: *rate2 += vp9_cost_bit(vp9_get_pred_prob_mbskip(cm, xd), 1); michael@0: michael@0: // Scaling factor for SSE from spatial domain to frequency domain michael@0: // is 16. Adjust distortion accordingly. michael@0: *distortion_uv = (sse_u + sse_v) << 4; michael@0: *distortion = (sse << 4) + *distortion_uv; michael@0: michael@0: *disable_skip = 1; michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, *rate2, *distortion); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (!x->skip) { michael@0: int skippable_y, skippable_uv; michael@0: int64_t sseuv = INT64_MAX; michael@0: int64_t rdcosty = INT64_MAX; michael@0: michael@0: // Y cost and distortion michael@0: super_block_yrd(cpi, x, rate_y, distortion_y, &skippable_y, psse, michael@0: bsize, txfm_cache, ref_best_rd); michael@0: michael@0: if (*rate_y == INT_MAX) { michael@0: *rate2 = INT_MAX; michael@0: *distortion = INT64_MAX; michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: xd->plane[i].dst.buf = orig_dst[i]; michael@0: xd->plane[i].dst.stride = orig_dst_stride[i]; michael@0: } michael@0: return INT64_MAX; michael@0: } michael@0: michael@0: *rate2 += *rate_y; michael@0: *distortion += *distortion_y; michael@0: michael@0: rdcosty = RDCOST(x->rdmult, x->rddiv, *rate2, *distortion); michael@0: rdcosty = MIN(rdcosty, RDCOST(x->rdmult, x->rddiv, 0, *psse)); michael@0: michael@0: super_block_uvrd(cpi, x, rate_uv, distortion_uv, &skippable_uv, &sseuv, michael@0: bsize, ref_best_rd - rdcosty); michael@0: if (*rate_uv == INT_MAX) { michael@0: *rate2 = INT_MAX; michael@0: *distortion = INT64_MAX; michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: xd->plane[i].dst.buf = orig_dst[i]; michael@0: xd->plane[i].dst.stride = orig_dst_stride[i]; michael@0: } michael@0: return INT64_MAX; michael@0: } michael@0: michael@0: *psse += sseuv; michael@0: *rate2 += *rate_uv; michael@0: *distortion += *distortion_uv; michael@0: *skippable = skippable_y && skippable_uv; michael@0: } michael@0: michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: xd->plane[i].dst.buf = orig_dst[i]; michael@0: xd->plane[i].dst.stride = orig_dst_stride[i]; michael@0: } michael@0: michael@0: return this_rd; // if 0, this will be re-calculated by caller michael@0: } michael@0: michael@0: static void swap_block_ptr(MACROBLOCK *x, PICK_MODE_CONTEXT *ctx, michael@0: int max_plane) { michael@0: struct macroblock_plane *const p = x->plane; michael@0: struct macroblockd_plane *const pd = x->e_mbd.plane; michael@0: int i; michael@0: michael@0: for (i = 0; i < max_plane; ++i) { michael@0: p[i].coeff = ctx->coeff_pbuf[i][1]; michael@0: pd[i].qcoeff = ctx->qcoeff_pbuf[i][1]; michael@0: pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; michael@0: pd[i].eobs = ctx->eobs_pbuf[i][1]; michael@0: michael@0: ctx->coeff_pbuf[i][1] = ctx->coeff_pbuf[i][0]; michael@0: ctx->qcoeff_pbuf[i][1] = ctx->qcoeff_pbuf[i][0]; michael@0: ctx->dqcoeff_pbuf[i][1] = ctx->dqcoeff_pbuf[i][0]; michael@0: ctx->eobs_pbuf[i][1] = ctx->eobs_pbuf[i][0]; michael@0: michael@0: ctx->coeff_pbuf[i][0] = p[i].coeff; michael@0: ctx->qcoeff_pbuf[i][0] = pd[i].qcoeff; michael@0: ctx->dqcoeff_pbuf[i][0] = pd[i].dqcoeff; michael@0: ctx->eobs_pbuf[i][0] = pd[i].eobs; michael@0: } michael@0: } michael@0: michael@0: void vp9_rd_pick_intra_mode_sb(VP9_COMP *cpi, MACROBLOCK *x, michael@0: int *returnrate, int64_t *returndist, michael@0: BLOCK_SIZE bsize, michael@0: PICK_MODE_CONTEXT *ctx, int64_t best_rd) { michael@0: VP9_COMMON *const cm = &cpi->common; michael@0: MACROBLOCKD *const xd = &x->e_mbd; michael@0: int rate_y = 0, rate_uv = 0, rate_y_tokenonly = 0, rate_uv_tokenonly = 0; michael@0: int y_skip = 0, uv_skip = 0; michael@0: int64_t dist_y = 0, dist_uv = 0, tx_cache[TX_MODES] = { 0 }; michael@0: x->skip_encode = 0; michael@0: ctx->skip = 0; michael@0: xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME; michael@0: if (bsize >= BLOCK_8X8) { michael@0: if (rd_pick_intra_sby_mode(cpi, x, &rate_y, &rate_y_tokenonly, michael@0: &dist_y, &y_skip, bsize, tx_cache, michael@0: best_rd) >= best_rd) { michael@0: *returnrate = INT_MAX; michael@0: return; michael@0: } michael@0: rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly, michael@0: &dist_uv, &uv_skip, bsize); michael@0: } else { michael@0: y_skip = 0; michael@0: if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate_y, &rate_y_tokenonly, michael@0: &dist_y, best_rd) >= best_rd) { michael@0: *returnrate = INT_MAX; michael@0: return; michael@0: } michael@0: rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly, michael@0: &dist_uv, &uv_skip, BLOCK_8X8); michael@0: } michael@0: michael@0: if (y_skip && uv_skip) { michael@0: *returnrate = rate_y + rate_uv - rate_y_tokenonly - rate_uv_tokenonly + michael@0: vp9_cost_bit(vp9_get_pred_prob_mbskip(cm, xd), 1); michael@0: *returndist = dist_y + dist_uv; michael@0: vp9_zero(ctx->tx_rd_diff); michael@0: } else { michael@0: int i; michael@0: *returnrate = rate_y + rate_uv + michael@0: vp9_cost_bit(vp9_get_pred_prob_mbskip(cm, xd), 0); michael@0: *returndist = dist_y + dist_uv; michael@0: if (cpi->sf.tx_size_search_method == USE_FULL_RD) michael@0: for (i = 0; i < TX_MODES; i++) { michael@0: if (tx_cache[i] < INT64_MAX && tx_cache[cm->tx_mode] < INT64_MAX) michael@0: ctx->tx_rd_diff[i] = tx_cache[i] - tx_cache[cm->tx_mode]; michael@0: else michael@0: ctx->tx_rd_diff[i] = 0; michael@0: } michael@0: } michael@0: michael@0: ctx->mic = *xd->mi_8x8[0]; michael@0: } michael@0: michael@0: int64_t vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, MACROBLOCK *x, michael@0: const TileInfo *const tile, michael@0: int mi_row, int mi_col, michael@0: int *returnrate, michael@0: int64_t *returndistortion, michael@0: BLOCK_SIZE bsize, michael@0: PICK_MODE_CONTEXT *ctx, michael@0: int64_t best_rd_so_far) { michael@0: VP9_COMMON *cm = &cpi->common; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; michael@0: const struct segmentation *seg = &cm->seg; michael@0: const BLOCK_SIZE block_size = get_plane_block_size(bsize, &xd->plane[0]); michael@0: MB_PREDICTION_MODE this_mode; michael@0: MV_REFERENCE_FRAME ref_frame, second_ref_frame; michael@0: unsigned char segment_id = mbmi->segment_id; michael@0: int comp_pred, i; michael@0: int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES]; michael@0: struct buf_2d yv12_mb[4][MAX_MB_PLANE]; michael@0: int_mv single_newmv[MAX_REF_FRAMES] = { { 0 } }; michael@0: static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG, michael@0: VP9_ALT_FLAG }; michael@0: int idx_list[4] = {0, michael@0: cpi->lst_fb_idx, michael@0: cpi->gld_fb_idx, michael@0: cpi->alt_fb_idx}; michael@0: int64_t best_rd = best_rd_so_far; michael@0: int64_t best_tx_rd[TX_MODES]; michael@0: int64_t best_tx_diff[TX_MODES]; michael@0: int64_t best_pred_diff[NB_PREDICTION_TYPES]; michael@0: int64_t best_pred_rd[NB_PREDICTION_TYPES]; michael@0: int64_t best_filter_rd[SWITCHABLE_FILTER_CONTEXTS]; michael@0: int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS]; michael@0: MB_MODE_INFO best_mbmode = { 0 }; michael@0: int j; michael@0: int mode_index, best_mode_index = 0; michael@0: unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES]; michael@0: vp9_prob comp_mode_p; michael@0: int64_t best_intra_rd = INT64_MAX; michael@0: int64_t best_inter_rd = INT64_MAX; michael@0: MB_PREDICTION_MODE best_intra_mode = DC_PRED; michael@0: MV_REFERENCE_FRAME best_inter_ref_frame = LAST_FRAME; michael@0: INTERPOLATION_TYPE tmp_best_filter = SWITCHABLE; michael@0: int rate_uv_intra[TX_SIZES], rate_uv_tokenonly[TX_SIZES]; michael@0: int64_t dist_uv[TX_SIZES]; michael@0: int skip_uv[TX_SIZES]; michael@0: MB_PREDICTION_MODE mode_uv[TX_SIZES]; michael@0: struct scale_factors scale_factor[4]; michael@0: unsigned int ref_frame_mask = 0; michael@0: unsigned int mode_mask = 0; michael@0: int64_t mode_distortions[MB_MODE_COUNT] = {-1}; michael@0: int64_t frame_distortions[MAX_REF_FRAMES] = {-1}; michael@0: int intra_cost_penalty = 20 * vp9_dc_quant(cm->base_qindex, cm->y_dc_delta_q); michael@0: const int bws = num_8x8_blocks_wide_lookup[bsize] / 2; michael@0: const int bhs = num_8x8_blocks_high_lookup[bsize] / 2; michael@0: int best_skip2 = 0; michael@0: michael@0: x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH; michael@0: michael@0: // Everywhere the flag is set the error is much higher than its neighbors. michael@0: ctx->frames_with_high_error = 0; michael@0: ctx->modes_with_high_error = 0; michael@0: michael@0: estimate_ref_frame_costs(cpi, segment_id, ref_costs_single, ref_costs_comp, michael@0: &comp_mode_p); michael@0: michael@0: for (i = 0; i < NB_PREDICTION_TYPES; ++i) michael@0: best_pred_rd[i] = INT64_MAX; michael@0: for (i = 0; i < TX_MODES; i++) michael@0: best_tx_rd[i] = INT64_MAX; michael@0: for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) michael@0: best_filter_rd[i] = INT64_MAX; michael@0: for (i = 0; i < TX_SIZES; i++) michael@0: rate_uv_intra[i] = INT_MAX; michael@0: michael@0: *returnrate = INT_MAX; michael@0: michael@0: // Create a mask set to 1 for each reference frame used by a smaller michael@0: // resolution. michael@0: if (cpi->sf.use_avoid_tested_higherror) { michael@0: switch (block_size) { michael@0: case BLOCK_64X64: michael@0: for (i = 0; i < 4; i++) { michael@0: for (j = 0; j < 4; j++) { michael@0: ref_frame_mask |= x->mb_context[i][j].frames_with_high_error; michael@0: mode_mask |= x->mb_context[i][j].modes_with_high_error; michael@0: } michael@0: } michael@0: for (i = 0; i < 4; i++) { michael@0: ref_frame_mask |= x->sb32_context[i].frames_with_high_error; michael@0: mode_mask |= x->sb32_context[i].modes_with_high_error; michael@0: } michael@0: break; michael@0: case BLOCK_32X32: michael@0: for (i = 0; i < 4; i++) { michael@0: ref_frame_mask |= michael@0: x->mb_context[x->sb_index][i].frames_with_high_error; michael@0: mode_mask |= x->mb_context[x->sb_index][i].modes_with_high_error; michael@0: } michael@0: break; michael@0: default: michael@0: // Until we handle all block sizes set it to present; michael@0: ref_frame_mask = 0; michael@0: mode_mask = 0; michael@0: break; michael@0: } michael@0: ref_frame_mask = ~ref_frame_mask; michael@0: mode_mask = ~mode_mask; michael@0: } michael@0: michael@0: for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) { michael@0: if (cpi->ref_frame_flags & flag_list[ref_frame]) { michael@0: setup_buffer_inter(cpi, x, tile, idx_list[ref_frame], ref_frame, michael@0: block_size, mi_row, mi_col, michael@0: frame_mv[NEARESTMV], frame_mv[NEARMV], michael@0: yv12_mb, scale_factor); michael@0: } michael@0: frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; michael@0: frame_mv[ZEROMV][ref_frame].as_int = 0; michael@0: } michael@0: michael@0: for (mode_index = 0; mode_index < MAX_MODES; ++mode_index) { michael@0: int mode_excluded = 0; michael@0: int64_t this_rd = INT64_MAX; michael@0: int disable_skip = 0; michael@0: int compmode_cost = 0; michael@0: int rate2 = 0, rate_y = 0, rate_uv = 0; michael@0: int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0; michael@0: int skippable = 0; michael@0: int64_t tx_cache[TX_MODES]; michael@0: int i; michael@0: int this_skip2 = 0; michael@0: int64_t total_sse = INT_MAX; michael@0: int early_term = 0; michael@0: michael@0: for (i = 0; i < TX_MODES; ++i) michael@0: tx_cache[i] = INT64_MAX; michael@0: michael@0: x->skip = 0; michael@0: this_mode = vp9_mode_order[mode_index].mode; michael@0: ref_frame = vp9_mode_order[mode_index].ref_frame; michael@0: second_ref_frame = vp9_mode_order[mode_index].second_ref_frame; michael@0: michael@0: // Look at the reference frame of the best mode so far and set the michael@0: // skip mask to look at a subset of the remaining modes. michael@0: if (mode_index > cpi->sf.mode_skip_start) { michael@0: if (mode_index == (cpi->sf.mode_skip_start + 1)) { michael@0: switch (vp9_mode_order[best_mode_index].ref_frame) { michael@0: case INTRA_FRAME: michael@0: cpi->mode_skip_mask = 0; michael@0: break; michael@0: case LAST_FRAME: michael@0: cpi->mode_skip_mask = LAST_FRAME_MODE_MASK; michael@0: break; michael@0: case GOLDEN_FRAME: michael@0: cpi->mode_skip_mask = GOLDEN_FRAME_MODE_MASK; michael@0: break; michael@0: case ALTREF_FRAME: michael@0: cpi->mode_skip_mask = ALT_REF_MODE_MASK; michael@0: break; michael@0: case NONE: michael@0: case MAX_REF_FRAMES: michael@0: assert(!"Invalid Reference frame"); michael@0: } michael@0: } michael@0: if (cpi->mode_skip_mask & ((int64_t)1 << mode_index)) michael@0: continue; michael@0: } michael@0: michael@0: // Skip if the current reference frame has been masked off michael@0: if (cpi->sf.reference_masking && !cpi->set_ref_frame_mask && michael@0: (cpi->ref_frame_mask & (1 << ref_frame))) michael@0: continue; michael@0: michael@0: // Test best rd so far against threshold for trying this mode. michael@0: if ((best_rd < ((int64_t)cpi->rd_threshes[segment_id][bsize][mode_index] * michael@0: cpi->rd_thresh_freq_fact[bsize][mode_index] >> 5)) || michael@0: cpi->rd_threshes[segment_id][bsize][mode_index] == INT_MAX) michael@0: continue; michael@0: michael@0: // Do not allow compound prediction if the segment level reference michael@0: // frame feature is in use as in this case there can only be one reference. michael@0: if ((second_ref_frame > INTRA_FRAME) && michael@0: vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) michael@0: continue; michael@0: michael@0: // Skip some checking based on small partitions' result. michael@0: if (x->fast_ms > 1 && !ref_frame) michael@0: continue; michael@0: if (x->fast_ms > 2 && ref_frame != x->subblock_ref) michael@0: continue; michael@0: michael@0: if (cpi->sf.use_avoid_tested_higherror && bsize >= BLOCK_8X8) { michael@0: if (!(ref_frame_mask & (1 << ref_frame))) { michael@0: continue; michael@0: } michael@0: if (!(mode_mask & (1 << this_mode))) { michael@0: continue; michael@0: } michael@0: if (second_ref_frame != NONE michael@0: && !(ref_frame_mask & (1 << second_ref_frame))) { michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: mbmi->ref_frame[0] = ref_frame; michael@0: mbmi->ref_frame[1] = second_ref_frame; michael@0: michael@0: if (!(ref_frame == INTRA_FRAME michael@0: || (cpi->ref_frame_flags & flag_list[ref_frame]))) { michael@0: continue; michael@0: } michael@0: if (!(second_ref_frame == NONE michael@0: || (cpi->ref_frame_flags & flag_list[second_ref_frame]))) { michael@0: continue; michael@0: } michael@0: michael@0: comp_pred = second_ref_frame > INTRA_FRAME; michael@0: if (comp_pred) { michael@0: if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) michael@0: if (vp9_mode_order[best_mode_index].ref_frame == INTRA_FRAME) michael@0: continue; michael@0: if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_COMP_REFMISMATCH) michael@0: if (ref_frame != best_inter_ref_frame && michael@0: second_ref_frame != best_inter_ref_frame) michael@0: continue; michael@0: } michael@0: michael@0: set_scale_factors(xd, ref_frame, second_ref_frame, scale_factor); michael@0: mbmi->uv_mode = DC_PRED; michael@0: michael@0: // Evaluate all sub-pel filters irrespective of whether we can use michael@0: // them for this frame. michael@0: mbmi->interp_filter = cm->mcomp_filter_type; michael@0: vp9_setup_interp_filters(xd, mbmi->interp_filter, cm); michael@0: michael@0: if (comp_pred) { michael@0: if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) michael@0: continue; michael@0: set_scale_factors(xd, ref_frame, second_ref_frame, scale_factor); michael@0: michael@0: mode_excluded = mode_excluded michael@0: ? mode_excluded michael@0: : cm->comp_pred_mode == SINGLE_PREDICTION_ONLY; michael@0: } else { michael@0: if (ref_frame != INTRA_FRAME && second_ref_frame != INTRA_FRAME) { michael@0: mode_excluded = michael@0: mode_excluded ? michael@0: mode_excluded : cm->comp_pred_mode == COMP_PREDICTION_ONLY; michael@0: } michael@0: } michael@0: michael@0: // Select prediction reference frames. michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: xd->plane[i].pre[0] = yv12_mb[ref_frame][i]; michael@0: if (comp_pred) michael@0: xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i]; michael@0: } michael@0: michael@0: // If the segment reference frame feature is enabled.... michael@0: // then do nothing if the current ref frame is not allowed.. michael@0: if (vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && michael@0: vp9_get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != michael@0: (int)ref_frame) { michael@0: continue; michael@0: // If the segment skip feature is enabled.... michael@0: // then do nothing if the current mode is not allowed.. michael@0: } else if (vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP) && michael@0: (this_mode != ZEROMV && ref_frame != INTRA_FRAME)) { michael@0: continue; michael@0: // Disable this drop out case if the ref frame michael@0: // segment level feature is enabled for this segment. This is to michael@0: // prevent the possibility that we end up unable to pick any mode. michael@0: } else if (!vp9_segfeature_active(seg, segment_id, michael@0: SEG_LVL_REF_FRAME)) { 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. We allow near/nearest as well michael@0: // because they may result in zero-zero MVs but be cheaper. michael@0: if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) { michael@0: if ((this_mode != ZEROMV && michael@0: !(this_mode == NEARMV && michael@0: frame_mv[NEARMV][ALTREF_FRAME].as_int == 0) && michael@0: !(this_mode == NEARESTMV && michael@0: frame_mv[NEARESTMV][ALTREF_FRAME].as_int == 0)) || michael@0: ref_frame != ALTREF_FRAME) { michael@0: continue; michael@0: } michael@0: } michael@0: } michael@0: // TODO(JBB): This is to make up for the fact that we don't have sad michael@0: // functions that work when the block size reads outside the umv. We michael@0: // should fix this either by making the motion search just work on michael@0: // a representative block in the boundary ( first ) and then implement a michael@0: // function that does sads when inside the border.. michael@0: if (((mi_row + bhs) > cm->mi_rows || (mi_col + bws) > cm->mi_cols) && michael@0: this_mode == NEWMV) { michael@0: continue; michael@0: } michael@0: michael@0: #ifdef MODE_TEST_HIT_STATS michael@0: // TEST/DEBUG CODE michael@0: // Keep a rcord of the number of test hits at each size michael@0: cpi->mode_test_hits[bsize]++; michael@0: #endif michael@0: michael@0: michael@0: if (ref_frame == INTRA_FRAME) { michael@0: TX_SIZE uv_tx; michael@0: // Disable intra modes other than DC_PRED for blocks with low variance michael@0: // Threshold for intra skipping based on source variance michael@0: // TODO(debargha): Specialize the threshold for super block sizes michael@0: static const unsigned int skip_intra_var_thresh[BLOCK_SIZES] = { michael@0: 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, michael@0: }; michael@0: if ((cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_LOWVAR) && michael@0: this_mode != DC_PRED && michael@0: x->source_variance < skip_intra_var_thresh[mbmi->sb_type]) michael@0: continue; michael@0: // Only search the oblique modes if the best so far is michael@0: // one of the neighboring directional modes michael@0: if ((cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_BESTINTER) && michael@0: (this_mode >= D45_PRED && this_mode <= TM_PRED)) { michael@0: if (vp9_mode_order[best_mode_index].ref_frame > INTRA_FRAME) michael@0: continue; michael@0: } michael@0: mbmi->mode = this_mode; michael@0: if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_INTRA_DIRMISMATCH) { michael@0: if (conditional_skipintra(mbmi->mode, best_intra_mode)) michael@0: continue; michael@0: } michael@0: michael@0: super_block_yrd(cpi, x, &rate_y, &distortion_y, &skippable, NULL, michael@0: bsize, tx_cache, best_rd); michael@0: michael@0: if (rate_y == INT_MAX) michael@0: continue; michael@0: michael@0: uv_tx = MIN(mbmi->tx_size, max_uv_txsize_lookup[bsize]); michael@0: if (rate_uv_intra[uv_tx] == INT_MAX) { michael@0: choose_intra_uv_mode(cpi, ctx, bsize, &rate_uv_intra[uv_tx], michael@0: &rate_uv_tokenonly[uv_tx], michael@0: &dist_uv[uv_tx], &skip_uv[uv_tx], michael@0: &mode_uv[uv_tx]); michael@0: } michael@0: michael@0: rate_uv = rate_uv_tokenonly[uv_tx]; michael@0: distortion_uv = dist_uv[uv_tx]; michael@0: skippable = skippable && skip_uv[uv_tx]; michael@0: mbmi->uv_mode = mode_uv[uv_tx]; michael@0: michael@0: rate2 = rate_y + x->mbmode_cost[mbmi->mode] + rate_uv_intra[uv_tx]; michael@0: if (this_mode != DC_PRED && this_mode != TM_PRED) michael@0: rate2 += intra_cost_penalty; michael@0: distortion2 = distortion_y + distortion_uv; michael@0: } else { michael@0: mbmi->mode = this_mode; michael@0: compmode_cost = vp9_cost_bit(comp_mode_p, second_ref_frame > INTRA_FRAME); michael@0: this_rd = handle_inter_mode(cpi, x, tile, bsize, michael@0: tx_cache, michael@0: &rate2, &distortion2, &skippable, michael@0: &rate_y, &distortion_y, michael@0: &rate_uv, &distortion_uv, michael@0: &mode_excluded, &disable_skip, michael@0: &tmp_best_filter, frame_mv, michael@0: mi_row, mi_col, michael@0: single_newmv, &total_sse, best_rd); michael@0: if (this_rd == INT64_MAX) michael@0: continue; michael@0: } michael@0: michael@0: if (cm->comp_pred_mode == HYBRID_PREDICTION) { michael@0: rate2 += compmode_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: if (second_ref_frame > INTRA_FRAME) { michael@0: rate2 += ref_costs_comp[ref_frame]; michael@0: } else { michael@0: rate2 += ref_costs_single[ref_frame]; michael@0: } michael@0: michael@0: if (!disable_skip) { 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. Ignore if skip is coded at michael@0: // segment level as the cost wont have been added in. michael@0: // Is Mb level skip allowed (i.e. not coded at segment level). michael@0: const int mb_skip_allowed = !vp9_segfeature_active(seg, segment_id, michael@0: SEG_LVL_SKIP); michael@0: michael@0: if (skippable) { michael@0: // Back out the coefficient coding costs michael@0: rate2 -= (rate_y + rate_uv); michael@0: // for best yrd calculation michael@0: rate_uv = 0; michael@0: michael@0: if (mb_skip_allowed) { michael@0: int prob_skip_cost; michael@0: michael@0: // Cost the skip mb case michael@0: vp9_prob skip_prob = michael@0: vp9_get_pred_prob_mbskip(cm, xd); michael@0: michael@0: if (skip_prob) { michael@0: prob_skip_cost = vp9_cost_bit(skip_prob, 1); michael@0: rate2 += prob_skip_cost; michael@0: } michael@0: } michael@0: } else if (mb_skip_allowed && ref_frame != INTRA_FRAME && !xd->lossless) { michael@0: if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) < michael@0: RDCOST(x->rdmult, x->rddiv, 0, total_sse)) { michael@0: // Add in the cost of the no skip flag. michael@0: int prob_skip_cost = vp9_cost_bit(vp9_get_pred_prob_mbskip(cm, xd), michael@0: 0); michael@0: rate2 += prob_skip_cost; michael@0: } else { michael@0: // FIXME(rbultje) make this work for splitmv also michael@0: int prob_skip_cost = vp9_cost_bit(vp9_get_pred_prob_mbskip(cm, xd), michael@0: 1); michael@0: rate2 += prob_skip_cost; michael@0: distortion2 = total_sse; michael@0: assert(total_sse >= 0); michael@0: rate2 -= (rate_y + rate_uv); michael@0: rate_y = 0; michael@0: rate_uv = 0; michael@0: this_skip2 = 1; michael@0: } michael@0: } else if (mb_skip_allowed) { michael@0: // Add in the cost of the no skip flag. michael@0: int prob_skip_cost = vp9_cost_bit(vp9_get_pred_prob_mbskip(cm, xd), michael@0: 0); michael@0: rate2 += prob_skip_cost; michael@0: } michael@0: michael@0: // Calculate the final RD estimate for this mode. michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); michael@0: } michael@0: michael@0: // Keep record of best intra rd michael@0: if (!is_inter_block(&xd->mi_8x8[0]->mbmi) && michael@0: this_rd < best_intra_rd) { michael@0: best_intra_rd = this_rd; michael@0: best_intra_mode = xd->mi_8x8[0]->mbmi.mode; michael@0: } michael@0: michael@0: // Keep record of best inter rd with single reference michael@0: if (is_inter_block(&xd->mi_8x8[0]->mbmi) && michael@0: !has_second_ref(&xd->mi_8x8[0]->mbmi) && michael@0: !mode_excluded && this_rd < best_inter_rd) { michael@0: best_inter_rd = this_rd; michael@0: best_inter_ref_frame = ref_frame; michael@0: } michael@0: michael@0: if (!disable_skip && ref_frame == INTRA_FRAME) { michael@0: for (i = 0; i < NB_PREDICTION_TYPES; ++i) michael@0: best_pred_rd[i] = MIN(best_pred_rd[i], this_rd); michael@0: for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) michael@0: best_filter_rd[i] = MIN(best_filter_rd[i], this_rd); michael@0: } michael@0: michael@0: // Store the respective mode distortions for later use. michael@0: if (mode_distortions[this_mode] == -1 michael@0: || distortion2 < mode_distortions[this_mode]) { michael@0: mode_distortions[this_mode] = distortion2; michael@0: } michael@0: if (frame_distortions[ref_frame] == -1 michael@0: || distortion2 < frame_distortions[ref_frame]) { michael@0: frame_distortions[ref_frame] = distortion2; michael@0: } michael@0: michael@0: // Did this mode help.. i.e. is it the new best mode michael@0: if (this_rd < best_rd || x->skip) { michael@0: int max_plane = MAX_MB_PLANE; michael@0: if (!mode_excluded) { michael@0: // Note index of best mode so far michael@0: best_mode_index = mode_index; michael@0: michael@0: if (ref_frame == INTRA_FRAME) { michael@0: /* required for left and above block mv */ michael@0: mbmi->mv[0].as_int = 0; michael@0: max_plane = 1; michael@0: } michael@0: michael@0: *returnrate = rate2; michael@0: *returndistortion = distortion2; michael@0: best_rd = this_rd; michael@0: best_mbmode = *mbmi; michael@0: best_skip2 = this_skip2; michael@0: if (!x->select_txfm_size) michael@0: swap_block_ptr(x, ctx, max_plane); michael@0: vpx_memcpy(ctx->zcoeff_blk, x->zcoeff_blk[mbmi->tx_size], michael@0: sizeof(uint8_t) * ctx->num_4x4_blk); michael@0: michael@0: // TODO(debargha): enhance this test with a better distortion prediction michael@0: // based on qp, activity mask and history michael@0: if ((cpi->sf.mode_search_skip_flags & FLAG_EARLY_TERMINATE) && michael@0: (mode_index > MIN_EARLY_TERM_INDEX)) { michael@0: const int qstep = xd->plane[0].dequant[1]; michael@0: // TODO(debargha): Enhance this by specializing for each mode_index michael@0: int scale = 4; michael@0: if (x->source_variance < UINT_MAX) { michael@0: const int var_adjust = (x->source_variance < 16); michael@0: scale -= var_adjust; michael@0: } michael@0: if (ref_frame > INTRA_FRAME && michael@0: distortion2 * scale < qstep * qstep) { michael@0: early_term = 1; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* keep record of best compound/single-only prediction */ michael@0: if (!disable_skip && ref_frame != INTRA_FRAME) { michael@0: int single_rd, hybrid_rd, single_rate, hybrid_rate; michael@0: michael@0: if (cm->comp_pred_mode == HYBRID_PREDICTION) { michael@0: single_rate = rate2 - compmode_cost; michael@0: hybrid_rate = rate2; michael@0: } else { michael@0: single_rate = rate2; michael@0: hybrid_rate = rate2 + compmode_cost; michael@0: } michael@0: michael@0: single_rd = RDCOST(x->rdmult, x->rddiv, single_rate, distortion2); michael@0: hybrid_rd = RDCOST(x->rdmult, x->rddiv, hybrid_rate, distortion2); michael@0: michael@0: if (second_ref_frame <= INTRA_FRAME && michael@0: single_rd < best_pred_rd[SINGLE_PREDICTION_ONLY]) { michael@0: best_pred_rd[SINGLE_PREDICTION_ONLY] = single_rd; michael@0: } else if (second_ref_frame > INTRA_FRAME && michael@0: single_rd < best_pred_rd[COMP_PREDICTION_ONLY]) { michael@0: best_pred_rd[COMP_PREDICTION_ONLY] = single_rd; michael@0: } michael@0: if (hybrid_rd < best_pred_rd[HYBRID_PREDICTION]) michael@0: best_pred_rd[HYBRID_PREDICTION] = hybrid_rd; michael@0: } michael@0: michael@0: /* keep record of best filter type */ michael@0: if (!mode_excluded && !disable_skip && ref_frame != INTRA_FRAME && michael@0: cm->mcomp_filter_type != BILINEAR) { michael@0: int64_t ref = cpi->rd_filter_cache[cm->mcomp_filter_type == SWITCHABLE ? michael@0: SWITCHABLE_FILTERS : cm->mcomp_filter_type]; michael@0: for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) { michael@0: int64_t adj_rd; michael@0: // In cases of poor prediction, filter_cache[] can contain really big michael@0: // values, which actually are bigger than this_rd itself. This can michael@0: // cause negative best_filter_rd[] values, which is obviously silly. michael@0: // Therefore, if filter_cache < ref, we do an adjusted calculation. michael@0: if (cpi->rd_filter_cache[i] >= ref) { michael@0: adj_rd = this_rd + cpi->rd_filter_cache[i] - ref; michael@0: } else { michael@0: // FIXME(rbultje) do this for comppsred also michael@0: // michael@0: // To prevent out-of-range computation in michael@0: // adj_rd = cpi->rd_filter_cache[i] * this_rd / ref michael@0: // cpi->rd_filter_cache[i] / ref is converted to a 256 based ratio. michael@0: int tmp = cpi->rd_filter_cache[i] * 256 / ref; michael@0: adj_rd = (this_rd * tmp) >> 8; michael@0: } michael@0: best_filter_rd[i] = MIN(best_filter_rd[i], adj_rd); michael@0: } michael@0: } michael@0: michael@0: /* keep record of best txfm size */ michael@0: if (bsize < BLOCK_32X32) { michael@0: if (bsize < BLOCK_16X16) michael@0: tx_cache[ALLOW_16X16] = tx_cache[ALLOW_8X8]; michael@0: michael@0: tx_cache[ALLOW_32X32] = tx_cache[ALLOW_16X16]; michael@0: } michael@0: if (!mode_excluded && this_rd != INT64_MAX) { michael@0: for (i = 0; i < TX_MODES && tx_cache[i] < INT64_MAX; i++) { michael@0: int64_t adj_rd = INT64_MAX; michael@0: adj_rd = this_rd + tx_cache[i] - tx_cache[cm->tx_mode]; michael@0: michael@0: if (adj_rd < best_tx_rd[i]) michael@0: best_tx_rd[i] = adj_rd; michael@0: } michael@0: } michael@0: michael@0: if (early_term) michael@0: break; michael@0: michael@0: if (x->skip && !comp_pred) michael@0: break; michael@0: } michael@0: michael@0: if (best_rd >= best_rd_so_far) michael@0: return INT64_MAX; michael@0: michael@0: // If we used an estimate for the uv intra rd in the loop above... michael@0: if (cpi->sf.use_uv_intra_rd_estimate) { michael@0: // Do Intra UV best rd mode selection if best mode choice above was intra. michael@0: if (vp9_mode_order[best_mode_index].ref_frame == INTRA_FRAME) { michael@0: TX_SIZE uv_tx_size = get_uv_tx_size(mbmi); michael@0: rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv_intra[uv_tx_size], michael@0: &rate_uv_tokenonly[uv_tx_size], michael@0: &dist_uv[uv_tx_size], michael@0: &skip_uv[uv_tx_size], michael@0: bsize < BLOCK_8X8 ? BLOCK_8X8 : bsize); michael@0: } michael@0: } michael@0: michael@0: // If we are using reference masking and the set mask flag is set then michael@0: // create the reference frame mask. michael@0: if (cpi->sf.reference_masking && cpi->set_ref_frame_mask) michael@0: cpi->ref_frame_mask = ~(1 << vp9_mode_order[best_mode_index].ref_frame); michael@0: michael@0: // Flag all modes that have a distortion thats > 2x the best we found at michael@0: // this level. michael@0: for (mode_index = 0; mode_index < MB_MODE_COUNT; ++mode_index) { michael@0: if (mode_index == NEARESTMV || mode_index == NEARMV || mode_index == NEWMV) michael@0: continue; michael@0: michael@0: if (mode_distortions[mode_index] > 2 * *returndistortion) { michael@0: ctx->modes_with_high_error |= (1 << mode_index); michael@0: } michael@0: } michael@0: michael@0: // Flag all ref frames that have a distortion thats > 2x the best we found at michael@0: // this level. michael@0: for (ref_frame = INTRA_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) { michael@0: if (frame_distortions[ref_frame] > 2 * *returndistortion) { michael@0: ctx->frames_with_high_error |= (1 << ref_frame); michael@0: } michael@0: } michael@0: michael@0: assert((cm->mcomp_filter_type == SWITCHABLE) || michael@0: (cm->mcomp_filter_type == best_mbmode.interp_filter) || michael@0: (best_mbmode.ref_frame[0] == INTRA_FRAME)); michael@0: michael@0: // Updating rd_thresh_freq_fact[] here means that the different michael@0: // partition/block sizes are handled independently based on the best michael@0: // choice for the current partition. It may well be better to keep a scaled michael@0: // best rd so far value and update rd_thresh_freq_fact based on the mode/size michael@0: // combination that wins out. michael@0: if (cpi->sf.adaptive_rd_thresh) { michael@0: for (mode_index = 0; mode_index < MAX_MODES; ++mode_index) { michael@0: if (mode_index == best_mode_index) { michael@0: cpi->rd_thresh_freq_fact[bsize][mode_index] -= michael@0: (cpi->rd_thresh_freq_fact[bsize][mode_index] >> 3); michael@0: } else { michael@0: cpi->rd_thresh_freq_fact[bsize][mode_index] += RD_THRESH_INC; michael@0: if (cpi->rd_thresh_freq_fact[bsize][mode_index] > michael@0: (cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT)) { michael@0: cpi->rd_thresh_freq_fact[bsize][mode_index] = michael@0: cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // macroblock modes michael@0: *mbmi = best_mbmode; michael@0: x->skip |= best_skip2; michael@0: michael@0: for (i = 0; i < NB_PREDICTION_TYPES; ++i) { michael@0: if (best_pred_rd[i] == INT64_MAX) michael@0: best_pred_diff[i] = INT_MIN; michael@0: else michael@0: best_pred_diff[i] = best_rd - best_pred_rd[i]; michael@0: } michael@0: michael@0: if (!x->skip) { michael@0: for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) { michael@0: if (best_filter_rd[i] == INT64_MAX) michael@0: best_filter_diff[i] = 0; michael@0: else michael@0: best_filter_diff[i] = best_rd - best_filter_rd[i]; michael@0: } michael@0: if (cm->mcomp_filter_type == SWITCHABLE) michael@0: assert(best_filter_diff[SWITCHABLE_FILTERS] == 0); michael@0: } else { michael@0: vp9_zero(best_filter_diff); michael@0: } michael@0: michael@0: if (!x->skip) { michael@0: for (i = 0; i < TX_MODES; i++) { michael@0: if (best_tx_rd[i] == INT64_MAX) michael@0: best_tx_diff[i] = 0; michael@0: else michael@0: best_tx_diff[i] = best_rd - best_tx_rd[i]; michael@0: } michael@0: } else { michael@0: vp9_zero(best_tx_diff); michael@0: } michael@0: michael@0: set_scale_factors(xd, mbmi->ref_frame[0], mbmi->ref_frame[1], michael@0: scale_factor); michael@0: store_coding_context(x, ctx, best_mode_index, michael@0: &mbmi->ref_mvs[mbmi->ref_frame[0]][0], michael@0: &mbmi->ref_mvs[mbmi->ref_frame[1] < 0 ? 0 : michael@0: mbmi->ref_frame[1]][0], michael@0: best_pred_diff, best_tx_diff, best_filter_diff); michael@0: michael@0: return best_rd; michael@0: } michael@0: michael@0: michael@0: int64_t vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, MACROBLOCK *x, michael@0: const TileInfo *const tile, michael@0: int mi_row, int mi_col, michael@0: int *returnrate, michael@0: int64_t *returndistortion, michael@0: BLOCK_SIZE bsize, michael@0: PICK_MODE_CONTEXT *ctx, michael@0: int64_t best_rd_so_far) { michael@0: VP9_COMMON *cm = &cpi->common; michael@0: MACROBLOCKD *xd = &x->e_mbd; michael@0: MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi; michael@0: const struct segmentation *seg = &cm->seg; michael@0: const BLOCK_SIZE block_size = get_plane_block_size(bsize, &xd->plane[0]); michael@0: MV_REFERENCE_FRAME ref_frame, second_ref_frame; michael@0: unsigned char segment_id = mbmi->segment_id; michael@0: int comp_pred, i; michael@0: int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES]; michael@0: struct buf_2d yv12_mb[4][MAX_MB_PLANE]; michael@0: static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG, michael@0: VP9_ALT_FLAG }; michael@0: int idx_list[4] = {0, michael@0: cpi->lst_fb_idx, michael@0: cpi->gld_fb_idx, michael@0: cpi->alt_fb_idx}; michael@0: int64_t best_rd = best_rd_so_far; michael@0: int64_t best_yrd = best_rd_so_far; // FIXME(rbultje) more precise michael@0: int64_t best_tx_rd[TX_MODES]; michael@0: int64_t best_tx_diff[TX_MODES]; michael@0: int64_t best_pred_diff[NB_PREDICTION_TYPES]; michael@0: int64_t best_pred_rd[NB_PREDICTION_TYPES]; michael@0: int64_t best_filter_rd[SWITCHABLE_FILTER_CONTEXTS]; michael@0: int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS]; michael@0: MB_MODE_INFO best_mbmode = { 0 }; michael@0: int mode_index, best_mode_index = 0; michael@0: unsigned int ref_costs_single[MAX_REF_FRAMES], ref_costs_comp[MAX_REF_FRAMES]; michael@0: vp9_prob comp_mode_p; michael@0: int64_t best_inter_rd = INT64_MAX; michael@0: MV_REFERENCE_FRAME best_inter_ref_frame = LAST_FRAME; michael@0: INTERPOLATION_TYPE tmp_best_filter = SWITCHABLE; michael@0: int rate_uv_intra[TX_SIZES], rate_uv_tokenonly[TX_SIZES]; michael@0: int64_t dist_uv[TX_SIZES]; michael@0: int skip_uv[TX_SIZES]; michael@0: MB_PREDICTION_MODE mode_uv[TX_SIZES] = { 0 }; michael@0: struct scale_factors scale_factor[4]; michael@0: unsigned int ref_frame_mask = 0; michael@0: unsigned int mode_mask = 0; michael@0: int intra_cost_penalty = 20 * vp9_dc_quant(cpi->common.base_qindex, michael@0: cpi->common.y_dc_delta_q); michael@0: int_mv seg_mvs[4][MAX_REF_FRAMES]; michael@0: b_mode_info best_bmodes[4]; michael@0: int best_skip2 = 0; michael@0: michael@0: x->skip_encode = cpi->sf.skip_encode_frame && x->q_index < QIDX_SKIP_THRESH; michael@0: vpx_memset(x->zcoeff_blk[TX_4X4], 0, 4); michael@0: michael@0: for (i = 0; i < 4; i++) { michael@0: int j; michael@0: for (j = 0; j < MAX_REF_FRAMES; j++) michael@0: seg_mvs[i][j].as_int = INVALID_MV; michael@0: } michael@0: michael@0: estimate_ref_frame_costs(cpi, segment_id, ref_costs_single, ref_costs_comp, michael@0: &comp_mode_p); michael@0: michael@0: for (i = 0; i < NB_PREDICTION_TYPES; ++i) michael@0: best_pred_rd[i] = INT64_MAX; michael@0: for (i = 0; i < TX_MODES; i++) michael@0: best_tx_rd[i] = INT64_MAX; michael@0: for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) michael@0: best_filter_rd[i] = INT64_MAX; michael@0: for (i = 0; i < TX_SIZES; i++) michael@0: rate_uv_intra[i] = INT_MAX; michael@0: michael@0: *returnrate = INT_MAX; michael@0: michael@0: // Create a mask set to 1 for each reference frame used by a smaller michael@0: // resolution. michael@0: if (cpi->sf.use_avoid_tested_higherror) { michael@0: ref_frame_mask = 0; michael@0: mode_mask = 0; michael@0: ref_frame_mask = ~ref_frame_mask; michael@0: mode_mask = ~mode_mask; michael@0: } michael@0: michael@0: for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) { michael@0: if (cpi->ref_frame_flags & flag_list[ref_frame]) { michael@0: setup_buffer_inter(cpi, x, tile, idx_list[ref_frame], ref_frame, michael@0: block_size, mi_row, mi_col, michael@0: frame_mv[NEARESTMV], frame_mv[NEARMV], michael@0: yv12_mb, scale_factor); michael@0: } michael@0: frame_mv[NEWMV][ref_frame].as_int = INVALID_MV; michael@0: frame_mv[ZEROMV][ref_frame].as_int = 0; michael@0: } michael@0: michael@0: for (mode_index = 0; mode_index < MAX_REFS; ++mode_index) { michael@0: int mode_excluded = 0; michael@0: int64_t this_rd = INT64_MAX; michael@0: int disable_skip = 0; michael@0: int compmode_cost = 0; michael@0: int rate2 = 0, rate_y = 0, rate_uv = 0; michael@0: int64_t distortion2 = 0, distortion_y = 0, distortion_uv = 0; michael@0: int skippable = 0; michael@0: int64_t tx_cache[TX_MODES]; michael@0: int i; michael@0: int this_skip2 = 0; michael@0: int64_t total_sse = INT_MAX; michael@0: int early_term = 0; michael@0: michael@0: for (i = 0; i < TX_MODES; ++i) michael@0: tx_cache[i] = INT64_MAX; michael@0: michael@0: x->skip = 0; michael@0: ref_frame = vp9_ref_order[mode_index].ref_frame; michael@0: second_ref_frame = vp9_ref_order[mode_index].second_ref_frame; michael@0: michael@0: // Look at the reference frame of the best mode so far and set the michael@0: // skip mask to look at a subset of the remaining modes. michael@0: if (mode_index > 2 && cpi->sf.mode_skip_start < MAX_MODES) { michael@0: if (mode_index == 3) { michael@0: switch (vp9_ref_order[best_mode_index].ref_frame) { michael@0: case INTRA_FRAME: michael@0: cpi->mode_skip_mask = 0; michael@0: break; michael@0: case LAST_FRAME: michael@0: cpi->mode_skip_mask = 0x0010; michael@0: break; michael@0: case GOLDEN_FRAME: michael@0: cpi->mode_skip_mask = 0x0008; michael@0: break; michael@0: case ALTREF_FRAME: michael@0: cpi->mode_skip_mask = 0x0000; michael@0: break; michael@0: case NONE: michael@0: case MAX_REF_FRAMES: michael@0: assert(!"Invalid Reference frame"); michael@0: } michael@0: } michael@0: if (cpi->mode_skip_mask & ((int64_t)1 << mode_index)) michael@0: continue; michael@0: } michael@0: michael@0: // Skip if the current reference frame has been masked off michael@0: if (cpi->sf.reference_masking && !cpi->set_ref_frame_mask && michael@0: (cpi->ref_frame_mask & (1 << ref_frame))) michael@0: continue; michael@0: michael@0: // Test best rd so far against threshold for trying this mode. michael@0: if ((best_rd < michael@0: ((int64_t)cpi->rd_thresh_sub8x8[segment_id][bsize][mode_index] * michael@0: cpi->rd_thresh_freq_sub8x8[bsize][mode_index] >> 5)) || michael@0: cpi->rd_thresh_sub8x8[segment_id][bsize][mode_index] == INT_MAX) michael@0: continue; michael@0: michael@0: // Do not allow compound prediction if the segment level reference michael@0: // frame feature is in use as in this case there can only be one reference. michael@0: if ((second_ref_frame > INTRA_FRAME) && michael@0: vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) michael@0: continue; michael@0: michael@0: mbmi->ref_frame[0] = ref_frame; michael@0: mbmi->ref_frame[1] = second_ref_frame; michael@0: michael@0: if (!(ref_frame == INTRA_FRAME michael@0: || (cpi->ref_frame_flags & flag_list[ref_frame]))) { michael@0: continue; michael@0: } michael@0: if (!(second_ref_frame == NONE michael@0: || (cpi->ref_frame_flags & flag_list[second_ref_frame]))) { michael@0: continue; michael@0: } michael@0: michael@0: comp_pred = second_ref_frame > INTRA_FRAME; michael@0: if (comp_pred) { michael@0: if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_COMP_BESTINTRA) michael@0: if (vp9_ref_order[best_mode_index].ref_frame == INTRA_FRAME) michael@0: continue; michael@0: if (cpi->sf.mode_search_skip_flags & FLAG_SKIP_COMP_REFMISMATCH) michael@0: if (ref_frame != best_inter_ref_frame && michael@0: second_ref_frame != best_inter_ref_frame) michael@0: continue; michael@0: } michael@0: michael@0: // TODO(jingning, jkoleszar): scaling reference frame not supported for michael@0: // sub8x8 blocks. michael@0: if (ref_frame > 0 && michael@0: vp9_is_scaled(scale_factor[ref_frame].sfc)) michael@0: continue; michael@0: michael@0: if (second_ref_frame > 0 && michael@0: vp9_is_scaled(scale_factor[second_ref_frame].sfc)) michael@0: continue; michael@0: michael@0: set_scale_factors(xd, ref_frame, second_ref_frame, scale_factor); michael@0: mbmi->uv_mode = DC_PRED; michael@0: michael@0: // Evaluate all sub-pel filters irrespective of whether we can use michael@0: // them for this frame. michael@0: mbmi->interp_filter = cm->mcomp_filter_type; michael@0: vp9_setup_interp_filters(xd, mbmi->interp_filter, &cpi->common); michael@0: michael@0: if (comp_pred) { michael@0: if (!(cpi->ref_frame_flags & flag_list[second_ref_frame])) michael@0: continue; michael@0: set_scale_factors(xd, ref_frame, second_ref_frame, scale_factor); michael@0: michael@0: mode_excluded = mode_excluded michael@0: ? mode_excluded michael@0: : cm->comp_pred_mode == SINGLE_PREDICTION_ONLY; michael@0: } else { michael@0: if (ref_frame != INTRA_FRAME && second_ref_frame != INTRA_FRAME) { michael@0: mode_excluded = michael@0: mode_excluded ? michael@0: mode_excluded : cm->comp_pred_mode == COMP_PREDICTION_ONLY; michael@0: } michael@0: } michael@0: michael@0: // Select prediction reference frames. michael@0: for (i = 0; i < MAX_MB_PLANE; i++) { michael@0: xd->plane[i].pre[0] = yv12_mb[ref_frame][i]; michael@0: if (comp_pred) michael@0: xd->plane[i].pre[1] = yv12_mb[second_ref_frame][i]; michael@0: } michael@0: michael@0: // If the segment reference frame feature is enabled.... michael@0: // then do nothing if the current ref frame is not allowed.. michael@0: if (vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME) && michael@0: vp9_get_segdata(seg, segment_id, SEG_LVL_REF_FRAME) != michael@0: (int)ref_frame) { michael@0: continue; michael@0: // If the segment skip feature is enabled.... michael@0: // then do nothing if the current mode is not allowed.. michael@0: } else if (vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP) && michael@0: ref_frame != INTRA_FRAME) { michael@0: continue; michael@0: // Disable this drop out case if the ref frame michael@0: // segment level feature is enabled for this segment. This is to michael@0: // prevent the possibility that we end up unable to pick any mode. michael@0: } else if (!vp9_segfeature_active(seg, segment_id, michael@0: SEG_LVL_REF_FRAME)) { 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. We allow near/nearest as well michael@0: // because they may result in zero-zero MVs but be cheaper. michael@0: if (cpi->is_src_frame_alt_ref && (cpi->oxcf.arnr_max_frames == 0)) michael@0: continue; michael@0: } michael@0: michael@0: #ifdef MODE_TEST_HIT_STATS michael@0: // TEST/DEBUG CODE michael@0: // Keep a rcord of the number of test hits at each size michael@0: cpi->mode_test_hits[bsize]++; michael@0: #endif michael@0: michael@0: if (ref_frame == INTRA_FRAME) { michael@0: int rate; michael@0: mbmi->tx_size = TX_4X4; michael@0: if (rd_pick_intra_sub_8x8_y_mode(cpi, x, &rate, &rate_y, michael@0: &distortion_y, best_rd) >= best_rd) michael@0: continue; michael@0: rate2 += rate; michael@0: rate2 += intra_cost_penalty; michael@0: distortion2 += distortion_y; michael@0: michael@0: if (rate_uv_intra[TX_4X4] == INT_MAX) { michael@0: choose_intra_uv_mode(cpi, ctx, bsize, &rate_uv_intra[TX_4X4], michael@0: &rate_uv_tokenonly[TX_4X4], michael@0: &dist_uv[TX_4X4], &skip_uv[TX_4X4], michael@0: &mode_uv[TX_4X4]); michael@0: } michael@0: rate2 += rate_uv_intra[TX_4X4]; michael@0: rate_uv = rate_uv_tokenonly[TX_4X4]; michael@0: distortion2 += dist_uv[TX_4X4]; michael@0: distortion_uv = dist_uv[TX_4X4]; michael@0: mbmi->uv_mode = mode_uv[TX_4X4]; michael@0: tx_cache[ONLY_4X4] = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); michael@0: for (i = 0; i < TX_MODES; ++i) michael@0: tx_cache[i] = tx_cache[ONLY_4X4]; michael@0: } else { michael@0: int rate; michael@0: int64_t distortion; michael@0: int64_t this_rd_thresh; michael@0: int64_t tmp_rd, tmp_best_rd = INT64_MAX, tmp_best_rdu = INT64_MAX; michael@0: int tmp_best_rate = INT_MAX, tmp_best_ratey = INT_MAX; michael@0: int64_t tmp_best_distortion = INT_MAX, tmp_best_sse, uv_sse; michael@0: int tmp_best_skippable = 0; michael@0: int switchable_filter_index; michael@0: int_mv *second_ref = comp_pred ? michael@0: &mbmi->ref_mvs[second_ref_frame][0] : NULL; michael@0: b_mode_info tmp_best_bmodes[16]; michael@0: MB_MODE_INFO tmp_best_mbmode; michael@0: BEST_SEG_INFO bsi[SWITCHABLE_FILTERS]; michael@0: int pred_exists = 0; michael@0: int uv_skippable; michael@0: michael@0: this_rd_thresh = (ref_frame == LAST_FRAME) ? michael@0: cpi->rd_thresh_sub8x8[segment_id][bsize][THR_LAST] : michael@0: cpi->rd_thresh_sub8x8[segment_id][bsize][THR_ALTR]; michael@0: this_rd_thresh = (ref_frame == GOLDEN_FRAME) ? michael@0: cpi->rd_thresh_sub8x8[segment_id][bsize][THR_GOLD] : this_rd_thresh; michael@0: xd->mi_8x8[0]->mbmi.tx_size = TX_4X4; michael@0: michael@0: cpi->rd_filter_cache[SWITCHABLE_FILTERS] = INT64_MAX; michael@0: if (cm->mcomp_filter_type != BILINEAR) { michael@0: tmp_best_filter = EIGHTTAP; michael@0: if (x->source_variance < michael@0: cpi->sf.disable_filter_search_var_thresh) { michael@0: tmp_best_filter = EIGHTTAP; michael@0: vp9_zero(cpi->rd_filter_cache); michael@0: } else { michael@0: for (switchable_filter_index = 0; michael@0: switchable_filter_index < SWITCHABLE_FILTERS; michael@0: ++switchable_filter_index) { michael@0: int newbest, rs; michael@0: int64_t rs_rd; michael@0: mbmi->interp_filter = switchable_filter_index; michael@0: vp9_setup_interp_filters(xd, mbmi->interp_filter, &cpi->common); michael@0: michael@0: tmp_rd = rd_pick_best_mbsegmentation(cpi, x, tile, michael@0: &mbmi->ref_mvs[ref_frame][0], michael@0: second_ref, michael@0: best_yrd, michael@0: &rate, &rate_y, &distortion, michael@0: &skippable, &total_sse, michael@0: (int)this_rd_thresh, seg_mvs, michael@0: bsi, switchable_filter_index, michael@0: mi_row, mi_col); michael@0: michael@0: if (tmp_rd == INT64_MAX) michael@0: continue; michael@0: cpi->rd_filter_cache[switchable_filter_index] = tmp_rd; michael@0: rs = get_switchable_rate(x); michael@0: rs_rd = RDCOST(x->rdmult, x->rddiv, rs, 0); michael@0: cpi->rd_filter_cache[SWITCHABLE_FILTERS] = michael@0: MIN(cpi->rd_filter_cache[SWITCHABLE_FILTERS], michael@0: tmp_rd + rs_rd); michael@0: if (cm->mcomp_filter_type == SWITCHABLE) michael@0: tmp_rd += rs_rd; michael@0: michael@0: newbest = (tmp_rd < tmp_best_rd); michael@0: if (newbest) { michael@0: tmp_best_filter = mbmi->interp_filter; michael@0: tmp_best_rd = tmp_rd; michael@0: } michael@0: if ((newbest && cm->mcomp_filter_type == SWITCHABLE) || michael@0: (mbmi->interp_filter == cm->mcomp_filter_type && michael@0: cm->mcomp_filter_type != SWITCHABLE)) { michael@0: tmp_best_rdu = tmp_rd; michael@0: tmp_best_rate = rate; michael@0: tmp_best_ratey = rate_y; michael@0: tmp_best_distortion = distortion; michael@0: tmp_best_sse = total_sse; michael@0: tmp_best_skippable = skippable; michael@0: tmp_best_mbmode = *mbmi; michael@0: for (i = 0; i < 4; i++) { michael@0: tmp_best_bmodes[i] = xd->mi_8x8[0]->bmi[i]; michael@0: x->zcoeff_blk[TX_4X4][i] = !xd->plane[0].eobs[i]; michael@0: } michael@0: pred_exists = 1; michael@0: if (switchable_filter_index == 0 && michael@0: cpi->sf.use_rd_breakout && michael@0: best_rd < INT64_MAX) { michael@0: if (tmp_best_rdu / 2 > best_rd) { michael@0: // skip searching the other filters if the first is michael@0: // already substantially larger than the best so far michael@0: tmp_best_filter = mbmi->interp_filter; michael@0: tmp_best_rdu = INT64_MAX; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } // switchable_filter_index loop michael@0: } michael@0: } michael@0: michael@0: if (tmp_best_rdu == INT64_MAX) michael@0: continue; michael@0: michael@0: mbmi->interp_filter = (cm->mcomp_filter_type == SWITCHABLE ? michael@0: tmp_best_filter : cm->mcomp_filter_type); michael@0: vp9_setup_interp_filters(xd, mbmi->interp_filter, &cpi->common); michael@0: if (!pred_exists) { michael@0: // Handles the special case when a filter that is not in the michael@0: // switchable list (bilinear, 6-tap) is indicated at the frame level michael@0: tmp_rd = rd_pick_best_mbsegmentation(cpi, x, tile, michael@0: &mbmi->ref_mvs[ref_frame][0], michael@0: second_ref, michael@0: best_yrd, michael@0: &rate, &rate_y, &distortion, michael@0: &skippable, &total_sse, michael@0: (int)this_rd_thresh, seg_mvs, michael@0: bsi, 0, michael@0: mi_row, mi_col); michael@0: if (tmp_rd == INT64_MAX) michael@0: continue; michael@0: } else { michael@0: if (cpi->common.mcomp_filter_type == SWITCHABLE) { michael@0: int rs = get_switchable_rate(x); michael@0: tmp_best_rdu -= RDCOST(x->rdmult, x->rddiv, rs, 0); michael@0: } michael@0: tmp_rd = tmp_best_rdu; michael@0: total_sse = tmp_best_sse; michael@0: rate = tmp_best_rate; michael@0: rate_y = tmp_best_ratey; michael@0: distortion = tmp_best_distortion; michael@0: skippable = tmp_best_skippable; michael@0: *mbmi = tmp_best_mbmode; michael@0: for (i = 0; i < 4; i++) michael@0: xd->mi_8x8[0]->bmi[i] = tmp_best_bmodes[i]; michael@0: } michael@0: michael@0: rate2 += rate; michael@0: distortion2 += distortion; michael@0: michael@0: if (cpi->common.mcomp_filter_type == SWITCHABLE) michael@0: rate2 += get_switchable_rate(x); michael@0: michael@0: if (!mode_excluded) { michael@0: if (comp_pred) michael@0: mode_excluded = cpi->common.comp_pred_mode == SINGLE_PREDICTION_ONLY; michael@0: else michael@0: mode_excluded = cpi->common.comp_pred_mode == COMP_PREDICTION_ONLY; michael@0: } michael@0: compmode_cost = vp9_cost_bit(comp_mode_p, comp_pred); michael@0: michael@0: tmp_best_rdu = best_rd - michael@0: MIN(RDCOST(x->rdmult, x->rddiv, rate2, distortion2), michael@0: RDCOST(x->rdmult, x->rddiv, 0, total_sse)); michael@0: michael@0: if (tmp_best_rdu > 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: vp9_build_inter_predictors_sbuv(&x->e_mbd, mi_row, mi_col, michael@0: BLOCK_8X8); michael@0: super_block_uvrd(cpi, x, &rate_uv, &distortion_uv, &uv_skippable, michael@0: &uv_sse, BLOCK_8X8, tmp_best_rdu); michael@0: if (rate_uv == INT_MAX) michael@0: continue; michael@0: rate2 += rate_uv; michael@0: distortion2 += distortion_uv; michael@0: skippable = skippable && uv_skippable; michael@0: total_sse += uv_sse; michael@0: michael@0: tx_cache[ONLY_4X4] = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); michael@0: for (i = 0; i < TX_MODES; ++i) michael@0: tx_cache[i] = tx_cache[ONLY_4X4]; michael@0: } michael@0: } michael@0: michael@0: if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) { michael@0: rate2 += compmode_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: if (second_ref_frame > INTRA_FRAME) { michael@0: rate2 += ref_costs_comp[ref_frame]; michael@0: } else { michael@0: rate2 += ref_costs_single[ref_frame]; michael@0: } michael@0: michael@0: if (!disable_skip) { 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. Ignore if skip is coded at michael@0: // segment level as the cost wont have been added in. michael@0: // Is Mb level skip allowed (i.e. not coded at segment level). michael@0: const int mb_skip_allowed = !vp9_segfeature_active(seg, segment_id, michael@0: SEG_LVL_SKIP); michael@0: michael@0: if (mb_skip_allowed && ref_frame != INTRA_FRAME && !xd->lossless) { michael@0: if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) < michael@0: RDCOST(x->rdmult, x->rddiv, 0, total_sse)) { michael@0: // Add in the cost of the no skip flag. michael@0: int prob_skip_cost = vp9_cost_bit(vp9_get_pred_prob_mbskip(cm, xd), michael@0: 0); michael@0: rate2 += prob_skip_cost; michael@0: } else { michael@0: // FIXME(rbultje) make this work for splitmv also michael@0: int prob_skip_cost = vp9_cost_bit(vp9_get_pred_prob_mbskip(cm, xd), michael@0: 1); michael@0: rate2 += prob_skip_cost; michael@0: distortion2 = total_sse; michael@0: assert(total_sse >= 0); michael@0: rate2 -= (rate_y + rate_uv); michael@0: rate_y = 0; michael@0: rate_uv = 0; michael@0: this_skip2 = 1; michael@0: } michael@0: } else if (mb_skip_allowed) { michael@0: // Add in the cost of the no skip flag. michael@0: int prob_skip_cost = vp9_cost_bit(vp9_get_pred_prob_mbskip(cm, xd), michael@0: 0); michael@0: rate2 += prob_skip_cost; michael@0: } michael@0: michael@0: // Calculate the final RD estimate for this mode. michael@0: this_rd = RDCOST(x->rdmult, x->rddiv, rate2, distortion2); michael@0: } michael@0: michael@0: // Keep record of best inter rd with single reference michael@0: if (xd->mi_8x8[0]->mbmi.ref_frame[0] > INTRA_FRAME && michael@0: xd->mi_8x8[0]->mbmi.ref_frame[1] == NONE && michael@0: !mode_excluded && michael@0: this_rd < best_inter_rd) { michael@0: best_inter_rd = this_rd; michael@0: best_inter_ref_frame = ref_frame; michael@0: } michael@0: michael@0: if (!disable_skip && ref_frame == INTRA_FRAME) { michael@0: for (i = 0; i < NB_PREDICTION_TYPES; ++i) michael@0: best_pred_rd[i] = MIN(best_pred_rd[i], this_rd); michael@0: for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) michael@0: best_filter_rd[i] = MIN(best_filter_rd[i], this_rd); michael@0: } michael@0: michael@0: // Did this mode help.. i.e. is it the new best mode michael@0: if (this_rd < best_rd || x->skip) { michael@0: if (!mode_excluded) { michael@0: int max_plane = MAX_MB_PLANE; michael@0: // Note index of best mode so far michael@0: best_mode_index = mode_index; michael@0: michael@0: if (ref_frame == INTRA_FRAME) { michael@0: /* required for left and above block mv */ michael@0: mbmi->mv[0].as_int = 0; michael@0: max_plane = 1; michael@0: } michael@0: michael@0: *returnrate = rate2; michael@0: *returndistortion = distortion2; michael@0: best_rd = this_rd; michael@0: best_yrd = best_rd - michael@0: RDCOST(x->rdmult, x->rddiv, rate_uv, distortion_uv); michael@0: best_mbmode = *mbmi; michael@0: best_skip2 = this_skip2; michael@0: if (!x->select_txfm_size) michael@0: swap_block_ptr(x, ctx, max_plane); michael@0: vpx_memcpy(ctx->zcoeff_blk, x->zcoeff_blk[mbmi->tx_size], michael@0: sizeof(uint8_t) * ctx->num_4x4_blk); michael@0: michael@0: for (i = 0; i < 4; i++) michael@0: best_bmodes[i] = xd->mi_8x8[0]->bmi[i]; michael@0: michael@0: // TODO(debargha): enhance this test with a better distortion prediction michael@0: // based on qp, activity mask and history michael@0: if ((cpi->sf.mode_search_skip_flags & FLAG_EARLY_TERMINATE) && michael@0: (mode_index > MIN_EARLY_TERM_INDEX)) { michael@0: const int qstep = xd->plane[0].dequant[1]; michael@0: // TODO(debargha): Enhance this by specializing for each mode_index michael@0: int scale = 4; michael@0: if (x->source_variance < UINT_MAX) { michael@0: const int var_adjust = (x->source_variance < 16); michael@0: scale -= var_adjust; michael@0: } michael@0: if (ref_frame > INTRA_FRAME && michael@0: distortion2 * scale < qstep * qstep) { michael@0: early_term = 1; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* keep record of best compound/single-only prediction */ michael@0: if (!disable_skip && ref_frame != INTRA_FRAME) { michael@0: int single_rd, hybrid_rd, single_rate, hybrid_rate; michael@0: michael@0: if (cpi->common.comp_pred_mode == HYBRID_PREDICTION) { michael@0: single_rate = rate2 - compmode_cost; michael@0: hybrid_rate = rate2; michael@0: } else { michael@0: single_rate = rate2; michael@0: hybrid_rate = rate2 + compmode_cost; michael@0: } michael@0: michael@0: single_rd = RDCOST(x->rdmult, x->rddiv, single_rate, distortion2); michael@0: hybrid_rd = RDCOST(x->rdmult, x->rddiv, hybrid_rate, distortion2); michael@0: michael@0: if (second_ref_frame <= INTRA_FRAME && michael@0: single_rd < best_pred_rd[SINGLE_PREDICTION_ONLY]) { michael@0: best_pred_rd[SINGLE_PREDICTION_ONLY] = single_rd; michael@0: } else if (second_ref_frame > INTRA_FRAME && michael@0: single_rd < best_pred_rd[COMP_PREDICTION_ONLY]) { michael@0: best_pred_rd[COMP_PREDICTION_ONLY] = single_rd; michael@0: } michael@0: if (hybrid_rd < best_pred_rd[HYBRID_PREDICTION]) michael@0: best_pred_rd[HYBRID_PREDICTION] = hybrid_rd; michael@0: } michael@0: michael@0: /* keep record of best filter type */ michael@0: if (!mode_excluded && !disable_skip && ref_frame != INTRA_FRAME && michael@0: cm->mcomp_filter_type != BILINEAR) { michael@0: int64_t ref = cpi->rd_filter_cache[cm->mcomp_filter_type == SWITCHABLE ? michael@0: SWITCHABLE_FILTERS : cm->mcomp_filter_type]; michael@0: for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) { michael@0: int64_t adj_rd; michael@0: // In cases of poor prediction, filter_cache[] can contain really big michael@0: // values, which actually are bigger than this_rd itself. This can michael@0: // cause negative best_filter_rd[] values, which is obviously silly. michael@0: // Therefore, if filter_cache < ref, we do an adjusted calculation. michael@0: if (cpi->rd_filter_cache[i] >= ref) michael@0: adj_rd = this_rd + cpi->rd_filter_cache[i] - ref; michael@0: else // FIXME(rbultje) do this for comppred also michael@0: adj_rd = this_rd - (ref - cpi->rd_filter_cache[i]) * this_rd / ref; michael@0: best_filter_rd[i] = MIN(best_filter_rd[i], adj_rd); michael@0: } michael@0: } michael@0: michael@0: /* keep record of best txfm size */ michael@0: if (bsize < BLOCK_32X32) { michael@0: if (bsize < BLOCK_16X16) { michael@0: tx_cache[ALLOW_8X8] = tx_cache[ONLY_4X4]; michael@0: tx_cache[ALLOW_16X16] = tx_cache[ALLOW_8X8]; michael@0: } michael@0: tx_cache[ALLOW_32X32] = tx_cache[ALLOW_16X16]; michael@0: } michael@0: if (!mode_excluded && this_rd != INT64_MAX) { michael@0: for (i = 0; i < TX_MODES && tx_cache[i] < INT64_MAX; i++) { michael@0: int64_t adj_rd = INT64_MAX; michael@0: if (ref_frame > INTRA_FRAME) michael@0: adj_rd = this_rd + tx_cache[i] - tx_cache[cm->tx_mode]; michael@0: else michael@0: adj_rd = this_rd; michael@0: michael@0: if (adj_rd < best_tx_rd[i]) michael@0: best_tx_rd[i] = adj_rd; michael@0: } michael@0: } michael@0: michael@0: if (early_term) michael@0: break; michael@0: michael@0: if (x->skip && !comp_pred) michael@0: break; michael@0: } michael@0: michael@0: if (best_rd >= best_rd_so_far) michael@0: return INT64_MAX; michael@0: michael@0: // If we used an estimate for the uv intra rd in the loop above... michael@0: if (cpi->sf.use_uv_intra_rd_estimate) { michael@0: // Do Intra UV best rd mode selection if best mode choice above was intra. michael@0: if (vp9_ref_order[best_mode_index].ref_frame == INTRA_FRAME) { michael@0: TX_SIZE uv_tx_size = get_uv_tx_size(mbmi); michael@0: rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv_intra[uv_tx_size], michael@0: &rate_uv_tokenonly[uv_tx_size], michael@0: &dist_uv[uv_tx_size], michael@0: &skip_uv[uv_tx_size], michael@0: BLOCK_8X8); michael@0: } michael@0: } michael@0: michael@0: // If we are using reference masking and the set mask flag is set then michael@0: // create the reference frame mask. michael@0: if (cpi->sf.reference_masking && cpi->set_ref_frame_mask) michael@0: cpi->ref_frame_mask = ~(1 << vp9_ref_order[best_mode_index].ref_frame); michael@0: michael@0: if (best_rd == INT64_MAX && bsize < BLOCK_8X8) { michael@0: *returnrate = INT_MAX; michael@0: *returndistortion = INT_MAX; michael@0: return best_rd; michael@0: } michael@0: michael@0: assert((cm->mcomp_filter_type == SWITCHABLE) || michael@0: (cm->mcomp_filter_type == best_mbmode.interp_filter) || michael@0: (best_mbmode.ref_frame[0] == INTRA_FRAME)); michael@0: michael@0: // Updating rd_thresh_freq_fact[] here means that the different michael@0: // partition/block sizes are handled independently based on the best michael@0: // choice for the current partition. It may well be better to keep a scaled michael@0: // best rd so far value and update rd_thresh_freq_fact based on the mode/size michael@0: // combination that wins out. michael@0: if (cpi->sf.adaptive_rd_thresh) { michael@0: for (mode_index = 0; mode_index < MAX_REFS; ++mode_index) { michael@0: if (mode_index == best_mode_index) { michael@0: cpi->rd_thresh_freq_sub8x8[bsize][mode_index] -= michael@0: (cpi->rd_thresh_freq_sub8x8[bsize][mode_index] >> 3); michael@0: } else { michael@0: cpi->rd_thresh_freq_sub8x8[bsize][mode_index] += RD_THRESH_INC; michael@0: if (cpi->rd_thresh_freq_sub8x8[bsize][mode_index] > michael@0: (cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT)) { michael@0: cpi->rd_thresh_freq_sub8x8[bsize][mode_index] = michael@0: cpi->sf.adaptive_rd_thresh * RD_THRESH_MAX_FACT; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // macroblock modes michael@0: *mbmi = best_mbmode; michael@0: x->skip |= best_skip2; michael@0: if (best_mbmode.ref_frame[0] == INTRA_FRAME) { michael@0: for (i = 0; i < 4; i++) michael@0: xd->mi_8x8[0]->bmi[i].as_mode = best_bmodes[i].as_mode; michael@0: } else { michael@0: for (i = 0; i < 4; ++i) michael@0: vpx_memcpy(&xd->mi_8x8[0]->bmi[i], &best_bmodes[i], sizeof(b_mode_info)); michael@0: michael@0: mbmi->mv[0].as_int = xd->mi_8x8[0]->bmi[3].as_mv[0].as_int; michael@0: mbmi->mv[1].as_int = xd->mi_8x8[0]->bmi[3].as_mv[1].as_int; michael@0: } michael@0: michael@0: for (i = 0; i < NB_PREDICTION_TYPES; ++i) { michael@0: if (best_pred_rd[i] == INT64_MAX) michael@0: best_pred_diff[i] = INT_MIN; michael@0: else michael@0: best_pred_diff[i] = best_rd - best_pred_rd[i]; michael@0: } michael@0: michael@0: if (!x->skip) { michael@0: for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) { michael@0: if (best_filter_rd[i] == INT64_MAX) michael@0: best_filter_diff[i] = 0; michael@0: else michael@0: best_filter_diff[i] = best_rd - best_filter_rd[i]; michael@0: } michael@0: if (cm->mcomp_filter_type == SWITCHABLE) michael@0: assert(best_filter_diff[SWITCHABLE_FILTERS] == 0); michael@0: } else { michael@0: vp9_zero(best_filter_diff); michael@0: } michael@0: michael@0: if (!x->skip) { michael@0: for (i = 0; i < TX_MODES; i++) { michael@0: if (best_tx_rd[i] == INT64_MAX) michael@0: best_tx_diff[i] = 0; michael@0: else michael@0: best_tx_diff[i] = best_rd - best_tx_rd[i]; michael@0: } michael@0: } else { michael@0: vp9_zero(best_tx_diff); michael@0: } michael@0: michael@0: set_scale_factors(xd, mbmi->ref_frame[0], mbmi->ref_frame[1], michael@0: scale_factor); michael@0: store_coding_context(x, ctx, best_mode_index, michael@0: &mbmi->ref_mvs[mbmi->ref_frame[0]][0], michael@0: &mbmi->ref_mvs[mbmi->ref_frame[1] < 0 ? 0 : michael@0: mbmi->ref_frame[1]][0], michael@0: best_pred_diff, best_tx_diff, best_filter_diff); michael@0: michael@0: return best_rd; michael@0: }