1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "FLAC/assert.h"
22 #include "FLAC/format.h"
23 #include "private/bitmath.h"
24 #include "private/lpc.h"
25 #if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
30 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
31 #define M_LN2 0.69314718055994530942
34 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
36 /* a readable, but slower, version */
41 FLAC__ASSERT(lag > 0);
42 FLAC__ASSERT(lag <= data_len);
45 for(i = lag, d = 0.0; i < data_len; i++)
46 d += data[i] * data[i - lag];
52 * this version tends to run faster because of better data locality
53 * ('data_len' is usually much larger than 'lag')
56 unsigned sample, coeff;
57 const unsigned limit = data_len - lag;
59 FLAC__ASSERT(lag > 0);
60 FLAC__ASSERT(lag <= data_len);
62 for(coeff = 0; coeff < lag; coeff++)
64 for(sample = 0; sample <= limit; sample++) {
66 for(coeff = 0; coeff < lag; coeff++)
67 autoc[coeff] += d * data[sample+coeff];
69 for(; sample < data_len; sample++) {
71 for(coeff = 0; coeff < data_len - sample; coeff++)
72 autoc[coeff] += d * data[sample+coeff];
76 void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__real error[])
79 double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
81 FLAC__ASSERT(0 < max_order);
82 FLAC__ASSERT(max_order <= FLAC__MAX_LPC_ORDER);
83 FLAC__ASSERT(autoc[0] != 0.0);
87 for(i = 0; i < max_order; i++) {
88 /* Sum up this iteration's reflection coefficient. */
90 for(j = 0; j < i; j++)
91 r -= lpc[j] * autoc[i-j];
94 /* Update LPC coefficients and total error. */
96 for(j = 0; j < (i>>1); j++) {
98 lpc[j] += r * lpc[i-1-j];
99 lpc[i-1-j] += r * tmp;
102 lpc[j] += lpc[j] * r;
104 err *= (1.0 - r * r);
106 /* save this order */
107 for(j = 0; j <= i; j++)
108 lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
109 error[i] = (FLAC__real)err;
113 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift)
116 double d, cmax = -1e32;
117 FLAC__int32 qmax, qmin;
118 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
119 const int min_shiftlimit = -max_shiftlimit - 1;
121 FLAC__ASSERT(precision > 0);
122 FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
124 /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
126 qmax = 1 << precision;
130 for(i = 0; i < order; i++) {
131 if(lp_coeff[i] == 0.0)
133 d = fabs(lp_coeff[i]);
139 /* => coefficients are all 0, which means our constant-detect didn't work */
145 (void)frexp(cmax, &log2cmax);
147 *shift = (int)precision - log2cmax - 1;
149 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
151 /*@@@ this does not seem to help at all, but was not extensively tested either: */
152 if(*shift > max_shiftlimit)
153 *shift = max_shiftlimit;
161 for(i = 0; i < order; i++) {
162 qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] * (double)(1 << *shift));
164 /* double-check the result */
165 if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
166 #ifdef FLAC__OVERFLOW_DETECT
167 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, (double)lp_coeff[i] * (double)(1 << *shift), floor((double)lp_coeff[i] * (double)(1 << *shift)));
174 else { /* (*shift < 0) */
175 const int nshift = -(*shift);
177 fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift = %d\n", *shift);
179 for(i = 0; i < order; i++) {
180 qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] / (double)(1 << nshift));
182 /* double-check the result */
183 if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
184 #ifdef FLAC__OVERFLOW_DETECT
185 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, (double)lp_coeff[i] / (double)(1 << nshift), floor((double)lp_coeff[i] / (double)(1 << nshift)));
196 void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[])
198 #ifdef FLAC__OVERFLOW_DETECT
203 const FLAC__int32 *history;
205 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
206 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
208 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
209 fprintf(stderr,"\n");
211 FLAC__ASSERT(order > 0);
213 for(i = 0; i < data_len; i++) {
214 #ifdef FLAC__OVERFLOW_DETECT
219 for(j = 0; j < order; j++) {
220 sum += qlp_coeff[j] * (*(--history));
221 #ifdef FLAC__OVERFLOW_DETECT
222 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
224 if(sumo > 2147483647I64 || sumo < -2147483648I64)
226 if(sumo > 2147483647ll || sumo < -2147483648ll)
229 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);
233 *(residual++) = *(data++) - (sum >> lp_quantization);
236 /* Here's a slower but clearer version:
237 for(i = 0; i < data_len; i++) {
239 for(j = 0; j < order; j++)
240 sum += qlp_coeff[j] * data[i-j-1];
241 residual[i] = data[i] - (sum >> lp_quantization);
246 void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[])
250 const FLAC__int32 *history;
252 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
253 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
255 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
256 fprintf(stderr,"\n");
258 FLAC__ASSERT(order > 0);
260 for(i = 0; i < data_len; i++) {
263 for(j = 0; j < order; j++)
264 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
265 #ifdef FLAC__OVERFLOW_DETECT
266 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
267 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, sum >> lp_quantization);
270 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
271 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));
275 *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
279 void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[])
281 #ifdef FLAC__OVERFLOW_DETECT
286 const FLAC__int32 *history;
288 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
289 fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
291 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
292 fprintf(stderr,"\n");
294 FLAC__ASSERT(order > 0);
296 for(i = 0; i < data_len; i++) {
297 #ifdef FLAC__OVERFLOW_DETECT
302 for(j = 0; j < order; j++) {
303 sum += qlp_coeff[j] * (*(--history));
304 #ifdef FLAC__OVERFLOW_DETECT
305 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
307 if(sumo > 2147483647I64 || sumo < -2147483648I64)
309 if(sumo > 2147483647ll || sumo < -2147483648ll)
312 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);
316 *(data++) = *(residual++) + (sum >> lp_quantization);
319 /* Here's a slower but clearer version:
320 for(i = 0; i < data_len; i++) {
322 for(j = 0; j < order; j++)
323 sum += qlp_coeff[j] * data[i-j-1];
324 data[i] = residual[i] + (sum >> lp_quantization);
329 void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[])
333 const FLAC__int32 *history;
335 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
336 fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
338 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
339 fprintf(stderr,"\n");
341 FLAC__ASSERT(order > 0);
343 for(i = 0; i < data_len; i++) {
346 for(j = 0; j < order; j++)
347 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
348 #ifdef FLAC__OVERFLOW_DETECT
349 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
350 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, sum >> lp_quantization);
353 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*residual) + (sum >> lp_quantization)) > 32) {
354 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));
358 *(data++) = *(residual++) + (FLAC__int32)(sum >> lp_quantization);
362 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__real lpc_error, unsigned total_samples)
366 FLAC__ASSERT(total_samples > 0);
368 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
370 return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
373 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__real lpc_error, double error_scale)
375 if(lpc_error > 0.0) {
376 FLAC__real bps = (FLAC__real)((double)0.5 * log(error_scale * lpc_error) / M_LN2);
382 else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
383 return (FLAC__real)1e32;
390 unsigned FLAC__lpc_compute_best_order(const FLAC__real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
392 unsigned order, best_order;
393 FLAC__real best_bits, tmp_bits;
396 FLAC__ASSERT(max_order > 0);
397 FLAC__ASSERT(total_samples > 0);
399 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
402 best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (FLAC__real)total_samples;
404 for(order = 1; order < max_order; order++) {
405 tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[order], error_scale) * (FLAC__real)(total_samples - order) + (FLAC__real)(order * bits_per_signal_sample);
406 if(tmp_bits < best_bits) {
408 best_bits = tmp_bits;
412 return best_order+1; /* +1 since index of lpc_error[] is order-1 */