Skip to content

Commit cfe3afc

Browse files
authored
Further improve calculation of when to use wide residual computation (#702)
1 parent 1ab3c8e commit cfe3afc

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/libFLAC/include/private/lpc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ void FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_avx2(const FLA
181181

182182
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
183183

184+
FLAC__uint64 FLAC__lpc_max_prediction_value_before_shift(uint32_t subframe_bps, const FLAC__int32 qlp_coeff[], uint32_t order);
184185
uint32_t FLAC__lpc_max_prediction_before_shift_bps(uint32_t subframe_bps, const FLAC__int32 qlp_coeff[], uint32_t order);
185186
uint32_t FLAC__lpc_max_residual_bps(uint32_t subframe_bps, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization);
186187

src/libFLAC/lpc.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -939,27 +939,32 @@ FLAC__bool FLAC__lpc_compute_residual_from_qlp_coefficients_limit_residual_33bit
939939

940940
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
941941

942+
FLAC__uint64 FLAC__lpc_max_prediction_value_before_shift(uint32_t subframe_bps, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order)
943+
{
944+
FLAC__uint64 max_abs_sample_value = (FLAC__uint64)(1) << (subframe_bps - 1);
945+
FLAC__uint32 abs_sum_of_qlp_coeff = 0;
946+
uint32_t i;
947+
for(i = 0; i < order; i++)
948+
abs_sum_of_qlp_coeff += abs(qlp_coeff[i]);
949+
return max_abs_sample_value * abs_sum_of_qlp_coeff;
950+
}
951+
942952
uint32_t FLAC__lpc_max_prediction_before_shift_bps(uint32_t subframe_bps, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order)
943953
{
944954
/* This used to be subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order)
945955
* but that treats both the samples as well as the predictor as unknown. The
946956
* predictor is known however, so taking the log2 of the sum of the absolute values
947957
* of all coefficients is a more accurate representation of the predictor */
948-
FLAC__uint32 abs_sum_of_qlp_coeff = 0;
949-
uint32_t i;
950-
for(i = 0; i < order; i++)
951-
abs_sum_of_qlp_coeff += abs(qlp_coeff[i]);
952-
return subframe_bps + FLAC__bitmath_extra_mulbits_unsigned(abs_sum_of_qlp_coeff);
958+
return FLAC__bitmath_silog2(FLAC__lpc_max_prediction_value_before_shift(subframe_bps, qlp_coeff, order));
953959
}
954960

955961

956962
uint32_t FLAC__lpc_max_residual_bps(uint32_t subframe_bps, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization)
957963
{
958-
FLAC__int32 predictor_sum_bps = FLAC__lpc_max_prediction_before_shift_bps(subframe_bps, qlp_coeff, order) - lp_quantization;
959-
if((int)subframe_bps > predictor_sum_bps)
960-
return subframe_bps + 1;
961-
else
962-
return predictor_sum_bps + 1;
964+
FLAC__uint64 max_abs_sample_value = (FLAC__uint64)(1) << (subframe_bps - 1);
965+
FLAC__uint64 max_prediction_value_after_shift = -1 * ((-1 * (FLAC__int64)FLAC__lpc_max_prediction_value_before_shift(subframe_bps, qlp_coeff, order)) >> lp_quantization);
966+
FLAC__uint64 max_residual_value = max_abs_sample_value + max_prediction_value_after_shift;
967+
return FLAC__bitmath_silog2(max_residual_value);
963968
}
964969

965970
#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && !defined(FUZZING_BUILD_MODE_FLAC_SANITIZE_SIGNED_INTEGER_OVERFLOW)

0 commit comments

Comments
 (0)