Index: source/libvpx/vp9/encoder/vp9_rdopt.c |
diff --git a/source/libvpx/vp9/encoder/vp9_rdopt.c b/source/libvpx/vp9/encoder/vp9_rdopt.c |
index b6cb8b57f01e0c3135b872ea745912e893a4daab..038d1e11e301bc91a0e4f993b6c202ee73b4ff17 100644 |
--- a/source/libvpx/vp9/encoder/vp9_rdopt.c |
+++ b/source/libvpx/vp9/encoder/vp9_rdopt.c |
@@ -16,6 +16,7 @@ |
#include "vpx_mem/vpx_mem.h" |
#include "vpx_ports/mem.h" |
+#include "vpx_ports/system_state.h" |
#include "vp9/common/vp9_common.h" |
#include "vp9/common/vp9_entropy.h" |
@@ -28,7 +29,6 @@ |
#include "vp9/common/vp9_reconintra.h" |
#include "vp9/common/vp9_scan.h" |
#include "vp9/common/vp9_seg_common.h" |
-#include "vp9/common/vp9_systemdependent.h" |
#include "vp9/encoder/vp9_cost.h" |
#include "vp9/encoder/vp9_encodemb.h" |
@@ -192,8 +192,8 @@ static void model_rd_for_sb(VP9_COMP *cpi, BLOCK_SIZE bsize, |
const int64_t ac_thr = p->quant_thred[1] >> shift; |
// The low thresholds are used to measure if the prediction errors are |
// low enough so that we can skip the mode search. |
- const int64_t low_dc_thr = MIN(50, dc_thr >> 2); |
- const int64_t low_ac_thr = MIN(80, ac_thr >> 2); |
+ const int64_t low_dc_thr = VPXMIN(50, dc_thr >> 2); |
+ const int64_t low_ac_thr = VPXMIN(80, ac_thr >> 2); |
int bw = 1 << (b_width_log2_lookup[bs] - b_width_log2_lookup[unit_size]); |
int bh = 1 << (b_height_log2_lookup[bs] - b_width_log2_lookup[unit_size]); |
int idx, idy; |
@@ -505,7 +505,7 @@ static void block_rd_txfm(int plane, int block, BLOCK_SIZE plane_bsize, |
if (tx_size != TX_32X32) |
dc_correct >>= 2; |
- dist = MAX(0, sse - dc_correct); |
+ dist = VPXMAX(0, sse - dc_correct); |
} |
} else { |
// SKIP_TXFM_AC_DC |
@@ -531,7 +531,7 @@ static void block_rd_txfm(int plane, int block, BLOCK_SIZE plane_bsize, |
rd2 = RDCOST(x->rdmult, x->rddiv, 0, sse); |
// TODO(jingning): temporarily enabled only for luma component |
- rd = MIN(rd1, rd2); |
+ rd = VPXMIN(rd1, rd2); |
if (plane == 0) |
x->zcoeff_blk[tx_size][block] = !x->plane[plane].eobs[block] || |
(rd1 > rd2 && !xd->lossless); |
@@ -597,7 +597,7 @@ static void choose_largest_tx_size(VP9_COMP *cpi, MACROBLOCK *x, |
MACROBLOCKD *const xd = &x->e_mbd; |
MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
- mbmi->tx_size = MIN(max_tx_size, largest_tx_size); |
+ mbmi->tx_size = VPXMIN(max_tx_size, largest_tx_size); |
txfm_rd_in_plane(x, rate, distortion, skip, |
sse, ref_best_rd, 0, bs, |
@@ -637,29 +637,36 @@ static void choose_tx_size_from_rd(VP9_COMP *cpi, MACROBLOCK *x, |
start_tx = max_tx_size; |
end_tx = 0; |
} else { |
- TX_SIZE chosen_tx_size = MIN(max_tx_size, |
- tx_mode_to_biggest_tx_size[cm->tx_mode]); |
+ TX_SIZE chosen_tx_size = VPXMIN(max_tx_size, |
+ tx_mode_to_biggest_tx_size[cm->tx_mode]); |
start_tx = chosen_tx_size; |
end_tx = chosen_tx_size; |
} |
for (n = start_tx; n >= end_tx; n--) { |
+ int r_tx_size = 0; |
+ for (m = 0; m <= n - (n == (int) max_tx_size); m++) { |
+ if (m == n) |
+ r_tx_size += vp9_cost_zero(tx_probs[m]); |
+ else |
+ r_tx_size += vp9_cost_one(tx_probs[m]); |
+ } |
txfm_rd_in_plane(x, &r[n][0], &d[n], &s[n], |
&sse[n], ref_best_rd, 0, bs, n, |
cpi->sf.use_fast_coef_costing); |
r[n][1] = r[n][0]; |
if (r[n][0] < INT_MAX) { |
- for (m = 0; m <= n - (n == (int) max_tx_size); m++) { |
- if (m == n) |
- r[n][1] += vp9_cost_zero(tx_probs[m]); |
- else |
- r[n][1] += vp9_cost_one(tx_probs[m]); |
- } |
+ r[n][1] += r_tx_size; |
} |
- if (d[n] == INT64_MAX) { |
+ if (d[n] == INT64_MAX || r[n][0] == INT_MAX) { |
rd[n][0] = rd[n][1] = INT64_MAX; |
} else if (s[n]) { |
- rd[n][0] = rd[n][1] = RDCOST(x->rdmult, x->rddiv, s1, d[n]); |
+ if (is_inter_block(mbmi)) { |
+ rd[n][0] = rd[n][1] = RDCOST(x->rdmult, x->rddiv, s1, sse[n]); |
+ } else { |
+ rd[n][0] = RDCOST(x->rdmult, x->rddiv, s1, sse[n]); |
+ rd[n][1] = RDCOST(x->rdmult, x->rddiv, s1 + r_tx_size, sse[n]); |
+ } |
} else { |
rd[n][0] = RDCOST(x->rdmult, x->rddiv, r[n][0] + s0, d[n]); |
rd[n][1] = RDCOST(x->rdmult, x->rddiv, r[n][1] + s0, d[n]); |
@@ -1045,7 +1052,6 @@ static int64_t rd_pick_intra_sby_mode(VP9_COMP *cpi, MACROBLOCK *x, |
memset(x->skip_txfm, SKIP_TXFM_NONE, sizeof(x->skip_txfm)); |
/* Y Search for intra prediction mode */ |
for (mode = DC_PRED; mode <= TM_PRED; mode++) { |
- |
if (cpi->sf.use_nonrd_pick_mode) { |
// These speed features are turned on in hybrid non-RD and RD mode |
// for key frame coding in the context of real-time setting. |
@@ -1383,7 +1389,7 @@ static int64_t encode_inter_mb_segment(VP9_COMP *cpi, |
cpi->sf.use_fast_coef_costing); |
rd1 = RDCOST(x->rdmult, x->rddiv, thisrate, thisdistortion >> 2); |
rd2 = RDCOST(x->rdmult, x->rddiv, 0, thissse >> 2); |
- rd = MIN(rd1, rd2); |
+ rd = VPXMIN(rd1, rd2); |
if (rd >= best_yrd) |
return INT64_MAX; |
} |
@@ -1802,7 +1808,8 @@ static int64_t rd_pick_best_sub8x8_mode(VP9_COMP *cpi, MACROBLOCK *x, |
if (i == 0) |
max_mv = x->max_mv_context[mbmi->ref_frame[0]]; |
else |
- max_mv = MAX(abs(bsi->mvp.as_mv.row), abs(bsi->mvp.as_mv.col)) >> 3; |
+ max_mv = |
+ VPXMAX(abs(bsi->mvp.as_mv.row), abs(bsi->mvp.as_mv.col)) >> 3; |
if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) { |
// Take wtd average of the step_params based on the last frame's |
@@ -1820,7 +1827,7 @@ static int64_t rd_pick_best_sub8x8_mode(VP9_COMP *cpi, MACROBLOCK *x, |
if (cpi->sf.adaptive_motion_search) { |
mvp_full.row = x->pred_mv[mbmi->ref_frame[0]].row >> 3; |
mvp_full.col = x->pred_mv[mbmi->ref_frame[0]].col >> 3; |
- step_param = MAX(step_param, 8); |
+ step_param = VPXMAX(step_param, 8); |
} |
// adjust src pointer for this block |
@@ -2225,7 +2232,7 @@ static void single_motion_search(VP9_COMP *cpi, MACROBLOCK *x, |
vp9_set_mv_search_range(x, &ref_mv); |
// Work out the size of the first step in the mv step search. |
- // 0 here is maximum length first step. 1 is MAX >> 1 etc. |
+ // 0 here is maximum length first step. 1 is VPXMAX >> 1 etc. |
if (cpi->sf.mv.auto_mv_step_size && cm->show_frame) { |
// Take wtd average of the step_params based on the last frame's |
// max mv magnitude and that based on the best ref mvs of the current |
@@ -2237,9 +2244,10 @@ static void single_motion_search(VP9_COMP *cpi, MACROBLOCK *x, |
} |
if (cpi->sf.adaptive_motion_search && bsize < BLOCK_64X64) { |
- int boffset = 2 * (b_width_log2_lookup[BLOCK_64X64] - |
- MIN(b_height_log2_lookup[bsize], b_width_log2_lookup[bsize])); |
- step_param = MAX(step_param, boffset); |
+ int boffset = |
+ 2 * (b_width_log2_lookup[BLOCK_64X64] - |
+ VPXMIN(b_height_log2_lookup[bsize], b_width_log2_lookup[bsize])); |
+ step_param = VPXMAX(step_param, boffset); |
} |
if (cpi->sf.adaptive_motion_search) { |
@@ -2460,7 +2468,7 @@ static int64_t handle_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, |
// motion field, where the distortion gain for a single block may not |
// be enough to overcome the cost of a new mv. |
if (discount_newmv_test(cpi, this_mode, tmp_mv, mode_mv, refs[0])) { |
- *rate2 += MAX((rate_mv / NEW_MV_DISCOUNT_FACTOR), 1); |
+ *rate2 += VPXMAX((rate_mv / NEW_MV_DISCOUNT_FACTOR), 1); |
} else { |
*rate2 += rate_mv; |
} |
@@ -2489,18 +2497,17 @@ static int64_t handle_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, |
} |
// We don't include the cost of the second reference here, because there |
- // are only three options: Last/Golden, ARF/Last or Golden/ARF, or in other |
- // words if you present them in that order, the second one is always known |
- // if the first is known. |
+ // are only two options: Last/ARF or Golden/ARF; The second one is always |
+ // known, which is ARF. |
// |
// Under some circumstances we discount the cost of new mv mode to encourage |
// initiation of a motion field. |
if (discount_newmv_test(cpi, this_mode, frame_mv[refs[0]], |
mode_mv, refs[0])) { |
- *rate2 += MIN(cost_mv_ref(cpi, this_mode, |
- mbmi_ext->mode_context[refs[0]]), |
- cost_mv_ref(cpi, NEARESTMV, |
- mbmi_ext->mode_context[refs[0]])); |
+ *rate2 += VPXMIN(cost_mv_ref(cpi, this_mode, |
+ mbmi_ext->mode_context[refs[0]]), |
+ cost_mv_ref(cpi, NEARESTMV, |
+ mbmi_ext->mode_context[refs[0]])); |
} else { |
*rate2 += cost_mv_ref(cpi, this_mode, mbmi_ext->mode_context[refs[0]]); |
} |
@@ -2542,10 +2549,10 @@ static int64_t handle_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, |
rd = RDCOST(x->rdmult, x->rddiv, tmp_rate_sum, tmp_dist_sum); |
filter_cache[i] = rd; |
filter_cache[SWITCHABLE_FILTERS] = |
- MIN(filter_cache[SWITCHABLE_FILTERS], rd + rs_rd); |
+ VPXMIN(filter_cache[SWITCHABLE_FILTERS], rd + rs_rd); |
if (cm->interp_filter == SWITCHABLE) |
rd += rs_rd; |
- *mask_filter = MAX(*mask_filter, rd); |
+ *mask_filter = VPXMAX(*mask_filter, rd); |
} else { |
int rate_sum = 0; |
int64_t dist_sum = 0; |
@@ -2575,10 +2582,10 @@ static int64_t handle_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, |
rd = RDCOST(x->rdmult, x->rddiv, rate_sum, dist_sum); |
filter_cache[i] = rd; |
filter_cache[SWITCHABLE_FILTERS] = |
- MIN(filter_cache[SWITCHABLE_FILTERS], rd + rs_rd); |
+ VPXMIN(filter_cache[SWITCHABLE_FILTERS], rd + rs_rd); |
if (cm->interp_filter == SWITCHABLE) |
rd += rs_rd; |
- *mask_filter = MAX(*mask_filter, rd); |
+ *mask_filter = VPXMAX(*mask_filter, rd); |
if (i == 0 && intpel_mv) { |
tmp_rate_sum = rate_sum; |
@@ -2689,7 +2696,7 @@ static int64_t handle_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, |
*distortion += distortion_y; |
rdcosty = RDCOST(x->rdmult, x->rddiv, *rate2, *distortion); |
- rdcosty = MIN(rdcosty, RDCOST(x->rdmult, x->rddiv, 0, *psse)); |
+ rdcosty = VPXMIN(rdcosty, RDCOST(x->rdmult, x->rddiv, 0, *psse)); |
if (!super_block_uvrd(cpi, x, rate_uv, &distortion_uv, &skippable_uv, |
&sseuv, bsize, ref_best_rd - rdcosty)) { |
@@ -2754,7 +2761,7 @@ void vp9_rd_pick_intra_mode_sb(VP9_COMP *cpi, MACROBLOCK *x, |
pd[1].subsampling_x, |
pd[1].subsampling_y); |
rd_pick_intra_sbuv_mode(cpi, x, ctx, &rate_uv, &rate_uv_tokenonly, |
- &dist_uv, &uv_skip, MAX(BLOCK_8X8, bsize), |
+ &dist_uv, &uv_skip, VPXMAX(BLOCK_8X8, bsize), |
max_uv_tx_size); |
if (y_skip && uv_skip) { |
@@ -2821,12 +2828,12 @@ static void rd_variance_adjustment(VP9_COMP *cpi, |
// to a predictor with a low spatial complexity compared to the source. |
if ((source_variance > LOW_VAR_THRESH) && (ref_frame == INTRA_FRAME) && |
(source_variance > recon_variance)) { |
- var_factor = MIN(absvar_diff, MIN(VLOW_ADJ_MAX, var_error)); |
+ var_factor = VPXMIN(absvar_diff, VPXMIN(VLOW_ADJ_MAX, var_error)); |
// A second possible case of interest is where the source variance |
// is very low and we wish to discourage false texture or motion trails. |
} else if ((source_variance < (LOW_VAR_THRESH >> 1)) && |
(recon_variance > source_variance)) { |
- var_factor = MIN(absvar_diff, MIN(VHIGH_ADJ_MAX, var_error)); |
+ var_factor = VPXMIN(absvar_diff, VPXMIN(VHIGH_ADJ_MAX, var_error)); |
} |
*this_rd += (*this_rd * var_factor) / 100; |
} |
@@ -2856,7 +2863,7 @@ int vp9_active_h_edge(VP9_COMP *cpi, int mi_row, int mi_step) { |
top_edge += (int)(twopass->this_frame_stats.inactive_zone_rows * 2); |
bottom_edge -= (int)(twopass->this_frame_stats.inactive_zone_rows * 2); |
- bottom_edge = MAX(top_edge, bottom_edge); |
+ bottom_edge = VPXMAX(top_edge, bottom_edge); |
} |
if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) || |
@@ -2883,7 +2890,7 @@ int vp9_active_v_edge(VP9_COMP *cpi, int mi_col, int mi_step) { |
left_edge += (int)(twopass->this_frame_stats.inactive_zone_cols * 2); |
right_edge -= (int)(twopass->this_frame_stats.inactive_zone_cols * 2); |
- right_edge = MAX(left_edge, right_edge); |
+ right_edge = VPXMAX(left_edge, right_edge); |
} |
if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) || |
@@ -3130,7 +3137,7 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, |
} |
if ((ref_frame_skip_mask[0] & (1 << ref_frame)) && |
- (ref_frame_skip_mask[1] & (1 << MAX(0, second_ref_frame)))) |
+ (ref_frame_skip_mask[1] & (1 << VPXMAX(0, second_ref_frame)))) |
continue; |
if (mode_skip_mask[ref_frame] & (1 << this_mode)) |
@@ -3144,10 +3151,10 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, |
continue; |
if (sf->motion_field_mode_search) { |
- const int mi_width = MIN(num_8x8_blocks_wide_lookup[bsize], |
- tile_info->mi_col_end - mi_col); |
- const int mi_height = MIN(num_8x8_blocks_high_lookup[bsize], |
- tile_info->mi_row_end - mi_row); |
+ const int mi_width = VPXMIN(num_8x8_blocks_wide_lookup[bsize], |
+ tile_info->mi_col_end - mi_col); |
+ const int mi_height = VPXMIN(num_8x8_blocks_high_lookup[bsize], |
+ tile_info->mi_row_end - mi_row); |
const int bsl = mi_width_log2_lookup[bsize]; |
int cb_partition_search_ctrl = (((mi_row + mi_col) >> bsl) |
+ get_chessboard_index(cm->current_video_frame)) & 0x1; |
@@ -3365,9 +3372,9 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, |
if (!disable_skip && ref_frame == INTRA_FRAME) { |
for (i = 0; i < REFERENCE_MODES; ++i) |
- best_pred_rd[i] = MIN(best_pred_rd[i], this_rd); |
+ best_pred_rd[i] = VPXMIN(best_pred_rd[i], this_rd); |
for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) |
- best_filter_rd[i] = MIN(best_filter_rd[i], this_rd); |
+ best_filter_rd[i] = VPXMIN(best_filter_rd[i], this_rd); |
} |
// Did this mode help.. i.e. is it the new best mode |
@@ -3396,7 +3403,7 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, |
if (!x->select_tx_size) |
swap_block_ptr(x, ctx, 1, 0, 0, max_plane); |
memcpy(ctx->zcoeff_blk, x->zcoeff_blk[mbmi->tx_size], |
- sizeof(uint8_t) * ctx->num_4x4_blk); |
+ sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk); |
// TODO(debargha): enhance this test with a better distortion prediction |
// based on qp, activity mask and history |
@@ -3466,7 +3473,7 @@ void vp9_rd_pick_inter_mode_sb(VP9_COMP *cpi, |
adj_rd = filter_cache[i] - ref; |
adj_rd += this_rd; |
- best_filter_rd[i] = MIN(best_filter_rd[i], adj_rd); |
+ best_filter_rd[i] = VPXMIN(best_filter_rd[i], adj_rd); |
} |
} |
} |
@@ -3809,7 +3816,7 @@ void vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, |
} |
if ((ref_frame_skip_mask[0] & (1 << ref_frame)) && |
- (ref_frame_skip_mask[1] & (1 << MAX(0, second_ref_frame)))) |
+ (ref_frame_skip_mask[1] & (1 << VPXMAX(0, second_ref_frame)))) |
continue; |
// Test best rd so far against threshold for trying this mode. |
@@ -3964,12 +3971,11 @@ void vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, |
rs_rd = RDCOST(x->rdmult, x->rddiv, rs, 0); |
filter_cache[switchable_filter_index] = tmp_rd; |
filter_cache[SWITCHABLE_FILTERS] = |
- MIN(filter_cache[SWITCHABLE_FILTERS], |
- tmp_rd + rs_rd); |
+ VPXMIN(filter_cache[SWITCHABLE_FILTERS], tmp_rd + rs_rd); |
if (cm->interp_filter == SWITCHABLE) |
tmp_rd += rs_rd; |
- mask_filter = MAX(mask_filter, tmp_rd); |
+ mask_filter = VPXMAX(mask_filter, tmp_rd); |
newbest = (tmp_rd < tmp_best_rd); |
if (newbest) { |
@@ -4046,9 +4052,9 @@ void vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, |
compmode_cost = vp9_cost_bit(comp_mode_p, comp_pred); |
- tmp_best_rdu = best_rd - |
- MIN(RDCOST(x->rdmult, x->rddiv, rate2, distortion2), |
- RDCOST(x->rdmult, x->rddiv, 0, total_sse)); |
+ tmp_best_rdu = |
+ best_rd - VPXMIN(RDCOST(x->rdmult, x->rddiv, rate2, distortion2), |
+ RDCOST(x->rdmult, x->rddiv, 0, total_sse)); |
if (tmp_best_rdu > 0) { |
// If even the 'Y' rd value of split is higher than best so far |
@@ -4108,9 +4114,9 @@ void vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, |
if (!disable_skip && ref_frame == INTRA_FRAME) { |
for (i = 0; i < REFERENCE_MODES; ++i) |
- best_pred_rd[i] = MIN(best_pred_rd[i], this_rd); |
+ best_pred_rd[i] = VPXMIN(best_pred_rd[i], this_rd); |
for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++) |
- best_filter_rd[i] = MIN(best_filter_rd[i], this_rd); |
+ best_filter_rd[i] = VPXMIN(best_filter_rd[i], this_rd); |
} |
// Did this mode help.. i.e. is it the new best mode |
@@ -4137,7 +4143,7 @@ void vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, |
if (!x->select_tx_size) |
swap_block_ptr(x, ctx, 1, 0, 0, max_plane); |
memcpy(ctx->zcoeff_blk, x->zcoeff_blk[TX_4X4], |
- sizeof(uint8_t) * ctx->num_4x4_blk); |
+ sizeof(ctx->zcoeff_blk[0]) * ctx->num_4x4_blk); |
for (i = 0; i < 4; i++) |
best_bmodes[i] = xd->mi[0]->bmi[i]; |
@@ -4209,7 +4215,7 @@ void vp9_rd_pick_inter_mode_sub8x8(VP9_COMP *cpi, |
adj_rd = filter_cache[i] - ref; |
adj_rd += this_rd; |
- best_filter_rd[i] = MIN(best_filter_rd[i], adj_rd); |
+ best_filter_rd[i] = VPXMIN(best_filter_rd[i], adj_rd); |
} |
} |