mirror of
https://github.com/claunia/flac.git
synced 2025-12-16 18:54:26 +00:00
fix a calcuation bug in FLAC__lpc_compute_best_order()
This commit is contained in:
@@ -200,10 +200,11 @@ FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scal
|
|||||||
* IN lpc_error[0,max_order-1] >= 0.0 error returned from calculating LP coefficients
|
* IN lpc_error[0,max_order-1] >= 0.0 error returned from calculating LP coefficients
|
||||||
* IN max_order > 0 max LP order
|
* IN max_order > 0 max LP order
|
||||||
* IN total_samples > 0 # of samples in residual signal
|
* IN total_samples > 0 # of samples in residual signal
|
||||||
* IN bits_per_signal_sample # of bits per sample in the original signal
|
* IN overhead_bits_per_order # of bits overhead for each increased LP order
|
||||||
|
* (includes warmup sample size and quantized LP coefficient)
|
||||||
* RETURN [1,max_order] best order
|
* RETURN [1,max_order] best order
|
||||||
*/
|
*/
|
||||||
unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample);
|
unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order);
|
||||||
|
|
||||||
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
|
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
|
||||||
|
|
||||||
|
|||||||
@@ -62,6 +62,13 @@ void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_le
|
|||||||
FLAC__ASSERT(lag > 0);
|
FLAC__ASSERT(lag > 0);
|
||||||
FLAC__ASSERT(lag <= data_len);
|
FLAC__ASSERT(lag <= data_len);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Technically we should subtract the mean first like so:
|
||||||
|
* for(i = 0; i < data_len; i++)
|
||||||
|
* data[i] -= mean;
|
||||||
|
* but it appears not to make enough of a difference to matter, and
|
||||||
|
* most signals are already closely centered around zero
|
||||||
|
*/
|
||||||
while(lag--) {
|
while(lag--) {
|
||||||
for(i = lag, d = 0.0; i < data_len; i++)
|
for(i = lag, d = 0.0; i < data_len; i++)
|
||||||
d += data[i] * data[i - lag];
|
d += data[i] * data[i - lag];
|
||||||
@@ -410,28 +417,28 @@ FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scal
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
|
unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order)
|
||||||
{
|
{
|
||||||
unsigned order, best_order;
|
unsigned order, index, best_index; /* 'index' the index into lpc_error; index==order-1 since lpc_error[0] is for order==1, lpc_error[1] is for order==2, etc */
|
||||||
FLAC__double best_bits, tmp_bits, error_scale;
|
FLAC__double bits, best_bits, error_scale;
|
||||||
|
|
||||||
FLAC__ASSERT(max_order > 0);
|
FLAC__ASSERT(max_order > 0);
|
||||||
FLAC__ASSERT(total_samples > 0);
|
FLAC__ASSERT(total_samples > 0);
|
||||||
|
|
||||||
error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
|
error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
|
||||||
|
|
||||||
best_order = 0;
|
best_index = 0;
|
||||||
best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (FLAC__double)total_samples;
|
best_bits = (unsigned)(-1);
|
||||||
|
|
||||||
for(order = 1; order < max_order; order++) {
|
for(index = 0, order = 1; index < max_order; index++, order++) {
|
||||||
tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[order], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * bits_per_signal_sample);
|
bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[index], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * overhead_bits_per_order);
|
||||||
if(tmp_bits < best_bits) {
|
if(bits < best_bits) {
|
||||||
best_order = order;
|
best_index = index;
|
||||||
best_bits = tmp_bits;
|
best_bits = bits;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return best_order+1; /* +1 since index of lpc_error[] is order-1 */
|
return best_index+1; /* +1 since index of lpc_error[] is order-1 */
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
|
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
|
||||||
|
|||||||
Reference in New Issue
Block a user