1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 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);
223 #if defined _MSC_VER || defined __MINGW32__ /* don't know how to do 64-bit literals in VC++ */
224 if(sumo < 0) sumo = -sumo;
225 if(sumo > 2147483647)
227 if(sumo > 2147483647ll || sumo < -2147483648ll)
230 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);
234 *(residual++) = *(data++) - (sum >> lp_quantization);
237 /* Here's a slower but clearer version:
238 for(i = 0; i < data_len; i++) {
240 for(j = 0; j < order; j++)
241 sum += qlp_coeff[j] * data[i-j-1];
242 residual[i] = data[i] - (sum >> lp_quantization);
247 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[])
251 const FLAC__int32 *history;
253 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
254 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
256 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
257 fprintf(stderr,"\n");
259 FLAC__ASSERT(order > 0);
261 for(i = 0; i < data_len; i++) {
264 for(j = 0; j < order; j++)
265 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
266 #ifdef FLAC__OVERFLOW_DETECT
267 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
268 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, sum >> lp_quantization);
271 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
272 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));
276 *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
280 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[])
282 #ifdef FLAC__OVERFLOW_DETECT
287 const FLAC__int32 *history;
289 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
290 fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
292 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
293 fprintf(stderr,"\n");
295 FLAC__ASSERT(order > 0);
297 for(i = 0; i < data_len; i++) {
298 #ifdef FLAC__OVERFLOW_DETECT
303 for(j = 0; j < order; j++) {
304 sum += qlp_coeff[j] * (*(--history));
305 #ifdef FLAC__OVERFLOW_DETECT
306 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
307 #if defined _MSC_VER || defined __MINGW32__ /* don't know how to do 64-bit literals in VC++ */
308 if(sumo < 0) sumo = -sumo;
309 if(sumo > 2147483647)
311 if(sumo > 2147483647ll || sumo < -2147483648ll)
314 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);
318 *(data++) = *(residual++) + (sum >> lp_quantization);
321 /* Here's a slower but clearer version:
322 for(i = 0; i < data_len; i++) {
324 for(j = 0; j < order; j++)
325 sum += qlp_coeff[j] * data[i-j-1];
326 data[i] = residual[i] + (sum >> lp_quantization);
331 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[])
335 const FLAC__int32 *history;
337 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
338 fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
340 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
341 fprintf(stderr,"\n");
343 FLAC__ASSERT(order > 0);
345 for(i = 0; i < data_len; i++) {
348 for(j = 0; j < order; j++)
349 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
350 #ifdef FLAC__OVERFLOW_DETECT
351 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
352 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, sum >> lp_quantization);
355 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*residual) + (sum >> lp_quantization)) > 32) {
356 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));
360 *(data++) = *(residual++) + (FLAC__int32)(sum >> lp_quantization);
364 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__real lpc_error, unsigned total_samples)
368 FLAC__ASSERT(total_samples > 0);
370 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
372 return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
375 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__real lpc_error, double error_scale)
377 if(lpc_error > 0.0) {
378 FLAC__real bps = (FLAC__real)((double)0.5 * log(error_scale * lpc_error) / M_LN2);
384 else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
385 return (FLAC__real)1e32;
392 unsigned FLAC__lpc_compute_best_order(const FLAC__real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
394 unsigned order, best_order;
395 FLAC__real best_bits, tmp_bits;
398 FLAC__ASSERT(max_order > 0);
399 FLAC__ASSERT(total_samples > 0);
401 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
404 best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (FLAC__real)total_samples;
406 for(order = 1; order < max_order; order++) {
407 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);
408 if(tmp_bits < best_bits) {
410 best_bits = tmp_bits;
414 return best_order+1; /* +1 since index of lpc_error[] is order-1 */