Replace AOMMIN+AOMMAX combos with clamp or fclamp AOMMIN and AOMMAX are macros, so their arguments are evaluated twice. It is especially bad if AOMMIN and AOMMAX are used together to clamp a value to a range. Replace that combination with the clamp() or fclamp() inline function. Change-Id: I5d34cd68636668c1427a5a692d82979f548ba144
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c index 9cb1aad..90ab40c 100644 --- a/av1/av1_cx_iface.c +++ b/av1/av1_cx_iface.c
@@ -3935,18 +3935,14 @@ // and if the ctrl_set_layer_id is not used after this call, the // previous (last_encoded) values of spatial/temporal_layer_id will be used, // which may be invalid. - cpi->svc.spatial_layer_id = AOMMAX( - 0, - AOMMIN(cpi->svc.spatial_layer_id, cpi->svc.number_spatial_layers - 1)); - cpi->svc.temporal_layer_id = - AOMMAX(0, AOMMIN(cpi->svc.temporal_layer_id, - cpi->svc.number_temporal_layers - 1)); - cpi->common.spatial_layer_id = - AOMMAX(0, AOMMIN(cpi->common.spatial_layer_id, - cpi->svc.number_spatial_layers - 1)); - cpi->common.temporal_layer_id = - AOMMAX(0, AOMMIN(cpi->common.temporal_layer_id, - cpi->svc.number_temporal_layers - 1)); + cpi->svc.spatial_layer_id = + clamp(cpi->svc.spatial_layer_id, 0, cpi->svc.number_spatial_layers - 1); + cpi->svc.temporal_layer_id = clamp(cpi->svc.temporal_layer_id, 0, + cpi->svc.number_temporal_layers - 1); + cpi->common.spatial_layer_id = clamp(cpi->common.spatial_layer_id, 0, + cpi->svc.number_spatial_layers - 1); + cpi->common.temporal_layer_id = clamp(cpi->common.temporal_layer_id, 0, + cpi->svc.number_temporal_layers - 1); } if (ppi->number_spatial_layers > 1 || ppi->number_temporal_layers > 1) {
diff --git a/av1/common/cdef.h b/av1/common/cdef.h index c7a8a33..859bb1a 100644 --- a/av1/common/cdef.h +++ b/av1/common/cdef.h
@@ -61,9 +61,9 @@ static inline int constrain(int diff, int threshold, int damping) { if (!threshold) return 0; - const int shift = AOMMAX(0, damping - get_msb(threshold)); - return sign(diff) * - AOMMIN(abs(diff), AOMMAX(0, threshold - (abs(diff) >> shift))); + int shift = damping - get_msb(threshold); + shift = AOMMAX(0, shift); + return sign(diff) * clamp(threshold - (abs(diff) >> shift), 0, abs(diff)); } #ifdef __cplusplus
diff --git a/av1/common/resize.c b/av1/common/resize.c index 8d7a352..57a89c5 100644 --- a/av1/common/resize.c +++ b/av1/common/resize.c
@@ -273,7 +273,7 @@ sum = 0; for (k = 0; k < interp_taps; ++k) { const int pk = int_pel - interp_taps / 2 + 1 + k; - sum += filter[k] * input[AOMMAX(AOMMIN(pk, in_length - 1), 0)]; + sum += filter[k] * input[clamp(pk, 0, in_length - 1)]; } *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)); } @@ -720,7 +720,7 @@ sum = 0; for (k = 0; k < interp_taps; ++k) { const int pk = int_pel - interp_taps / 2 + 1 + k; - sum += filter[k] * input[AOMMAX(AOMMIN(pk, in_length - 1), 0)]; + sum += filter[k] * input[clamp(pk, 0, in_length - 1)]; } *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd); }
diff --git a/av1/encoder/av1_quantize.c b/av1/encoder/av1_quantize.c index e5aa111..610b703 100644 --- a/av1/encoder/av1_quantize.c +++ b/av1/encoder/av1_quantize.c
@@ -803,19 +803,19 @@ const FRAME_TYPE frame_type = cm->current_frame.frame_type; int qindex_rd; - const int current_qindex = AOMMAX( - 0, - AOMMIN(QINDEX_RANGE - 1, cm->delta_q_info.delta_q_present_flag - ? quant_params->base_qindex + x->delta_qindex - : quant_params->base_qindex)); + const int current_qindex = + clamp(cm->delta_q_info.delta_q_present_flag + ? quant_params->base_qindex + x->delta_qindex + : quant_params->base_qindex, + 0, QINDEX_RANGE - 1); const int qindex = av1_get_qindex(&cm->seg, segment_id, current_qindex); if (cpi->oxcf.sb_qp_sweep) { const int current_rd_qindex = - AOMMAX(0, AOMMIN(QINDEX_RANGE - 1, cm->delta_q_info.delta_q_present_flag - ? quant_params->base_qindex + - x->rdmult_delta_qindex - : quant_params->base_qindex)); + clamp(cm->delta_q_info.delta_q_present_flag + ? quant_params->base_qindex + x->rdmult_delta_qindex + : quant_params->base_qindex, + 0, QINDEX_RANGE - 1); qindex_rd = av1_get_qindex(&cm->seg, segment_id, current_rd_qindex); } else { qindex_rd = qindex;
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c index 5891276..c7f85d3 100644 --- a/av1/encoder/ethread.c +++ b/av1/encoder/ethread.c
@@ -1241,7 +1241,7 @@ if (num_fp_contexts < MAX_PARALLEL_FRAMES) num_fp_contexts = 1; } - num_fp_contexts = AOMMAX(1, AOMMIN(num_fp_contexts, MAX_PARALLEL_FRAMES)); + num_fp_contexts = clamp(num_fp_contexts, 1, MAX_PARALLEL_FRAMES); // Limit recalculated num_fp_contexts to ppi->num_fp_contexts. num_fp_contexts = (ppi->num_fp_contexts == 1) ? num_fp_contexts
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c index a7d893c..b43532a 100644 --- a/av1/encoder/pass2_strategy.c +++ b/av1/encoder/pass2_strategy.c
@@ -198,9 +198,7 @@ if ((twopass->bpm_factor <= 1 && factor < twopass->bpm_factor) || (twopass->bpm_factor >= 1 && factor > twopass->bpm_factor)) { - twopass->bpm_factor = factor; - twopass->bpm_factor = - AOMMAX(min_fac, AOMMIN(max_fac, twopass->bpm_factor)); + twopass->bpm_factor = fclamp(factor, min_fac, max_fac); } } #endif // CONFIG_THREE_PASS @@ -248,7 +246,7 @@ rate_err_factor = 1.0 - error_fraction; } - rate_err_factor = AOMMAX(min_fac, AOMMIN(max_fac, rate_err_factor)); + rate_err_factor = fclamp(rate_err_factor, min_fac, max_fac); } // Is the rate control trending in the right direction. Only make @@ -256,7 +254,7 @@ if ((rate_err_factor < 1.0 && err_estimate >= 0) || (rate_err_factor > 1.0 && err_estimate <= 0)) { twopass->bpm_factor *= rate_err_factor; - twopass->bpm_factor = AOMMAX(min_fac, AOMMIN(max_fac, twopass->bpm_factor)); + twopass->bpm_factor = fclamp(twopass->bpm_factor, min_fac, max_fac); } } @@ -1113,7 +1111,7 @@ for (i = start_idx; i <= last_idx; i++) { double total_wt = 0; for (j = -HALF_FILT_LEN; j <= HALF_FILT_LEN; j++) { - int idx = AOMMIN(AOMMAX(i + j, start_idx), last_idx); + int idx = clamp(i + j, start_idx, last_idx); if (stats[idx].is_flash) continue; filt_intra_err[i] += @@ -1129,7 +1127,7 @@ for (i = start_idx; i <= last_idx; i++) { double total_wt = 0; for (j = -HALF_FILT_LEN; j <= HALF_FILT_LEN; j++) { - int idx = AOMMIN(AOMMAX(i + j, start_idx), last_idx); + int idx = clamp(i + j, start_idx, last_idx); // Coded error involves idx and idx - 1. if (stats[idx].is_flash || (idx > 0 && stats[idx - 1].is_flash)) continue; @@ -1356,7 +1354,7 @@ double mean_coded = 0.001, var_coded = 0.001; int count = 0; for (j = -HALF_WIN; j <= HALF_WIN; j++) { - int idx = AOMMIN(AOMMAX(i + j, this_start), this_last); + int idx = clamp(i + j, this_start, this_last); if (stats[idx].is_flash || (idx > 0 && stats[idx - 1].is_flash)) continue; mean_intra += stats[idx].intra_error; var_intra += stats[idx].intra_error * stats[idx].intra_error; @@ -3148,7 +3146,7 @@ double boost_score = 0.0; const double kf_max_boost = cpi->oxcf.rc_cfg.mode == AOM_Q - ? AOMMIN(AOMMAX(rc->frames_to_key * 2.0, KF_MIN_FRAME_BOOST), + ? fclamp(rc->frames_to_key * 2.0, KF_MIN_FRAME_BOOST, KF_MAX_FRAME_BOOST) : KF_MAX_FRAME_BOOST; @@ -3574,7 +3572,7 @@ double total_noise = 0; double total_wt = 0; for (int j = -HALF_FILT_LEN; j <= HALF_FILT_LEN; j++) { - int idx = AOMMIN(AOMMAX(i + j, 0), len - 1); + int idx = clamp(i + j, 0, len - 1); if (first_stats[idx].is_flash) continue; total_noise += first_stats[idx].noise_var; @@ -3710,7 +3708,7 @@ 0.001) / AOMMAX(this_stats->intra_error - this_stats->noise_var, 0.001)); // clip correlation coefficient. - this_stats->cor_coeff = AOMMIN(AOMMAX(this_stats->cor_coeff, 0), 1); + this_stats->cor_coeff = fclamp(this_stats->cor_coeff, 0.0, 1.0); } first_stats->cor_coeff = 1.0; }
diff --git a/av1/encoder/ratectrl.c b/av1/encoder/ratectrl.c index 5ed99e5..d8688c5 100644 --- a/av1/encoder/ratectrl.c +++ b/av1/encoder/ratectrl.c
@@ -246,7 +246,7 @@ const int ratio = (cpi->rc.bit_est_ratio == 0) ? get_init_ratio(sse_sqrt) : cpi->rc.bit_est_ratio; // Clamp the enumerator to lower the q fluctuations. - enumerator = AOMMIN(AOMMAX((int)(ratio * sse_sqrt), 20000), 170000); + enumerator = clamp((int)(ratio * sse_sqrt), 20000, 170000); } else if (cpi->oxcf.rc_cfg.mode == AOM_CBR && frame_type == KEY_FRAME && cpi->sf.rt_sf.rc_adjust_keyframe && bit_depth == 8 && cpi->oxcf.rc_cfg.max_intra_bitrate_pct > 0 && @@ -620,9 +620,9 @@ // next refresh cycle. if (cpi->is_screen_content_type && (cpi->cyclic_refresh->sb_index > cpi->cyclic_refresh->last_sb_index)) { - max_delta_down = AOMMIN(8, AOMMAX(1, rc->q_1_frame / 32)); + max_delta_down = clamp(rc->q_1_frame / 32, 1, 8); } else { - max_delta_down = AOMMIN(16, AOMMAX(1, rc->q_1_frame / 8)); + max_delta_down = clamp(rc->q_1_frame / 8, 1, 16); } if (!cpi->ppi->use_svc && cpi->is_screen_content_type) { // Link max_delta_up to max_delta_down and buffer status. @@ -633,9 +633,9 @@ } } } else { - max_delta_down = (cpi->is_screen_content_type) - ? AOMMIN(8, AOMMAX(1, rc->q_1_frame / 16)) - : AOMMIN(16, AOMMAX(1, rc->q_1_frame / 8)); + max_delta_down = cpi->is_screen_content_type + ? clamp(rc->q_1_frame / 16, 1, 8) + : clamp(rc->q_1_frame / 8, 1, 16); } // For screen static content with stable buffer level: relax the // limit on max_delta_down and apply bias qp, based on buffer fullness. @@ -756,7 +756,7 @@ const int min_dist = av1_svc_get_min_ref_dist(cpi); q = q - AOMMIN(min_dist, 20); } - return AOMMAX(AOMMIN(q, cpi->rc.worst_quality), cpi->rc.best_quality); + return clamp(q, cpi->rc.best_quality, cpi->rc.worst_quality); } static const RATE_FACTOR_LEVEL rate_factor_levels[FRAME_UPDATE_TYPES] = { @@ -2034,19 +2034,12 @@ if (cq_level > 0) active_best_quality = AOMMAX(1, active_best_quality); - *top_index = active_worst_quality; - *bottom_index = active_best_quality; + *top_index = clamp(active_worst_quality, rc->best_quality, rc->worst_quality); - *top_index = AOMMAX(*top_index, rc->best_quality); - *top_index = AOMMIN(*top_index, rc->worst_quality); + *bottom_index = + clamp(active_best_quality, rc->best_quality, rc->worst_quality); - *bottom_index = AOMMAX(*bottom_index, rc->best_quality); - *bottom_index = AOMMIN(*bottom_index, rc->worst_quality); - - q = active_best_quality; - - q = AOMMAX(q, rc->best_quality); - q = AOMMIN(q, rc->worst_quality); + q = *bottom_index; assert(*top_index <= rc->worst_quality && *top_index >= rc->best_quality); assert(*bottom_index <= rc->worst_quality &&
diff --git a/av1/encoder/tune_vmaf.c b/av1/encoder/tune_vmaf.c index 7e425ed..7784202 100644 --- a/av1/encoder/tune_vmaf.c +++ b/av1/encoder/tune_vmaf.c
@@ -327,7 +327,7 @@ } while (approx_vmaf > best_vmaf && loop_count < max_loop_count); unsharp_amount = approx_vmaf > best_vmaf ? unsharp_amount : unsharp_amount - step_size; - return AOMMIN(max_amount, AOMMAX(unsharp_amount, min_amount)); + return fclamp(unsharp_amount, min_amount, max_amount); } static double find_best_frame_unsharp_amount( @@ -944,10 +944,7 @@ const double beta = AOMMAX(approx_sse / (dsse + approx_sse), 0.5); const int offset = av1_get_deltaq_offset(cm->seq_params->bit_depth, current_qindex, beta); - int qindex = current_qindex + offset; - - qindex = AOMMIN(qindex, MAXQ); - qindex = AOMMAX(qindex, MINQ); + const int qindex = clamp(current_qindex + offset, MINQ, MAXQ); return qindex; } @@ -996,7 +993,7 @@ unsharp_amount = approx_score > best_score ? unsharp_amount : unsharp_amount - step_size; - return AOMMIN(max_amount, AOMMAX(unsharp_amount, min_amount)); + return fclamp(unsharp_amount, min_amount, max_amount); } static double find_best_frame_unsharp_amount_neg(