90
void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[])
108
void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[])
93
111
FLAC__double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
95
FLAC__ASSERT(0 < max_order);
96
FLAC__ASSERT(max_order <= FLAC__MAX_LPC_ORDER);
113
FLAC__ASSERT(0 != max_order);
114
FLAC__ASSERT(0 < *max_order);
115
FLAC__ASSERT(*max_order <= FLAC__MAX_LPC_ORDER);
97
116
FLAC__ASSERT(autoc[0] != 0.0);
101
for(i = 0; i < max_order; i++) {
120
for(i = 0; i < *max_order; i++) {
102
121
/* Sum up this iteration's reflection coefficient. */
104
123
for(j = 0; j < i; j++)
167
/* calc cmax = max( |lp_coeff[i]| ) */
144
169
for(i = 0; i < order; i++) {
145
if(lp_coeff[i] == 0.0)
147
d = fabs(lp_coeff[i]);
170
const FLAC__double d = fabs(lp_coeff[i]);
152
175
if(cmax <= 0.0) {
153
176
/* => coefficients are all 0, which means our constant-detect didn't work */
180
const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
181
const int min_shiftlimit = -max_shiftlimit - 1;
159
184
(void)frexp(cmax, &log2cmax);
161
186
*shift = (int)precision - log2cmax - 1;
163
if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
165
/*@@@ this does not seem to help at all, but was not extensively tested either: */
166
if(*shift > max_shiftlimit)
167
*shift = max_shiftlimit;
188
if(*shift > max_shiftlimit)
189
*shift = max_shiftlimit;
190
else if(*shift < min_shiftlimit)
174
194
if(*shift >= 0) {
195
FLAC__double error = 0.0;
175
197
for(i = 0; i < order; i++) {
176
qlp_coeff[i] = (FLAC__int32)floor((FLAC__double)lp_coeff[i] * (FLAC__double)(1 << *shift));
178
/* double-check the result */
179
if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
198
error += lp_coeff[i] * (1 << *shift);
199
#if 1 /* unfortunately lround() is C99 */
201
q = (FLAC__int32)(error + 0.5);
203
q = (FLAC__int32)(error - 0.5);
180
207
#ifdef FLAC__OVERFLOW_DETECT
181
fprintf(stderr,"FLAC__lpc_quantize_coefficients: compensating for overflow, qlp_coeff[%u]=%d, lp_coeff[%u]=%f, cmax=%f, precision=%u, shift=%d, q=%f, f(q)=%f\n", i, qlp_coeff[i], i, lp_coeff[i], cmax, precision, *shift, (FLAC__double)lp_coeff[i] * (FLAC__double)(1 << *shift), floor((FLAC__double)lp_coeff[i] * (FLAC__double)(1 << *shift)));
208
if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
209
fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
211
fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
188
else { /* (*shift < 0) */
221
/* negative shift is very rare but due to design flaw, negative shift is
222
* a NOP in the decoder, so it must be handled specially by scaling down
189
226
const int nshift = -(*shift);
227
FLAC__double error = 0.0;
191
fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift = %d\n", *shift);
230
fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax);
193
232
for(i = 0; i < order; i++) {
194
qlp_coeff[i] = (FLAC__int32)floor((FLAC__double)lp_coeff[i] / (FLAC__double)(1 << nshift));
196
/* double-check the result */
197
if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
233
error += lp_coeff[i] / (1 << nshift);
234
#if 1 /* unfortunately lround() is C99 */
236
q = (FLAC__int32)(error + 0.5);
238
q = (FLAC__int32)(error - 0.5);
198
242
#ifdef FLAC__OVERFLOW_DETECT
199
fprintf(stderr,"FLAC__lpc_quantize_coefficients: compensating for overflow, qlp_coeff[%u]=%d, lp_coeff[%u]=%f, cmax=%f, precision=%u, shift=%d, q=%f, f(q)=%f\n", i, qlp_coeff[i], i, lp_coeff[i], cmax, precision, *shift, (FLAC__double)lp_coeff[i] / (FLAC__double)(1 << nshift), floor((FLAC__double)lp_coeff[i] / (FLAC__double)(1 << nshift)));
243
if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
244
fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
246
fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
239
290
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
241
292
if(sumo > 2147483647ll || sumo < -2147483648ll)
242
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,sumo);
293
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
277
328
sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
278
329
#ifdef FLAC__OVERFLOW_DETECT
279
330
if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
280
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, sum >> lp_quantization);
332
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization);
334
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
283
338
if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
284
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%lld, residual=%lld\n", i, *data, sum >> lp_quantization, (FLAC__int64)(*data) - (sum >> lp_quantization));
340
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%I64d, residual=%I64d\n", i, *data, sum >> lp_quantization, (FLAC__int64)(*data) - (sum >> lp_quantization));
342
fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%lld, residual=%lld\n", i, *data, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*data) - (sum >> lp_quantization)));
323
382
fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
325
384
if(sumo > 2147483647ll || sumo < -2147483648ll)
326
fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,sumo);
385
fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
330
*(data++) = *(residual++) + (sum >> lp_quantization);
389
*(data++) = *(r++) + (sum >> lp_quantization);
333
392
/* Here's a slower but clearer version:
361
420
sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
362
421
#ifdef FLAC__OVERFLOW_DETECT
363
422
if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
364
fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, sum >> lp_quantization);
367
if(FLAC__bitmath_silog2_wide((FLAC__int64)(*residual) + (sum >> lp_quantization)) > 32) {
368
fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%lld, data=%lld\n", i, *residual, sum >> lp_quantization, (FLAC__int64)(*residual) + (sum >> lp_quantization));
372
*(data++) = *(residual++) + (FLAC__int32)(sum >> lp_quantization);
424
fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization);
426
fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
430
if(FLAC__bitmath_silog2_wide((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) {
432
fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%I64d, data=%I64d\n", i, *r, sum >> lp_quantization, (FLAC__int64)(*r) + (sum >> lp_quantization));
434
fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%lld, data=%lld\n", i, *r, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*r) + (sum >> lp_quantization)));
439
*(data++) = *(r++) + (FLAC__int32)(sum >> lp_quantization);
406
unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
473
unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order)
408
unsigned order, best_order;
409
FLAC__double best_bits, tmp_bits, error_scale;
475
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 */
476
FLAC__double bits, best_bits, error_scale;
411
478
FLAC__ASSERT(max_order > 0);
412
479
FLAC__ASSERT(total_samples > 0);
414
481
error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
417
best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (FLAC__double)total_samples;
484
best_bits = (unsigned)(-1);
419
for(order = 1; order < max_order; order++) {
420
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);
421
if(tmp_bits < best_bits) {
423
best_bits = tmp_bits;
486
for(index = 0, order = 1; index < max_order; index++, order++) {
487
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);
488
if(bits < best_bits) {
427
return best_order+1; /* +1 since index of lpc_error[] is order-1 */
494
return best_index+1; /* +1 since index of lpc_error[] is order-1 */
430
497
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */