add wide (64-bit) versions of the FIR filter and inverse filter, remove unused arg...
[flac.git] / src / libFLAC / lpc.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <math.h>
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
26 #include <stdio.h>
27 #endif
28
29 #ifndef M_LN2
30 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
31 #define M_LN2 0.69314718055994530942
32 #endif
33
34 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
35 {
36         /* a readable, but slower, version */
37 #if 0
38         FLAC__real d;
39         unsigned i;
40
41         FLAC__ASSERT(lag > 0);
42         FLAC__ASSERT(lag <= data_len);
43
44         while(lag--) {
45                 for(i = lag, d = 0.0; i < data_len; i++)
46                         d += data[i] * data[i - lag];
47                 autoc[lag] = d;
48         }
49 #endif
50
51         /*
52          * this version tends to run faster because of better data locality
53          * ('data_len' is usually much larger than 'lag')
54          */
55         FLAC__real d;
56         unsigned sample, coeff;
57         const unsigned limit = data_len - lag;
58
59         FLAC__ASSERT(lag > 0);
60         FLAC__ASSERT(lag <= data_len);
61
62         for(coeff = 0; coeff < lag; coeff++)
63                 autoc[coeff] = 0.0;
64         for(sample = 0; sample <= limit; sample++) {
65                 d = data[sample];
66                 for(coeff = 0; coeff < lag; coeff++)
67                         autoc[coeff] += d * data[sample+coeff];
68         }
69         for(; sample < data_len; sample++) {
70                 d = data[sample];
71                 for(coeff = 0; coeff < data_len - sample; coeff++)
72                         autoc[coeff] += d * data[sample+coeff];
73         }
74 }
75
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[])
77 {
78         unsigned i, j;
79         double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
80
81         FLAC__ASSERT(0 < max_order);
82         FLAC__ASSERT(max_order <= FLAC__MAX_LPC_ORDER);
83         FLAC__ASSERT(autoc[0] != 0.0);
84
85         err = autoc[0];
86
87         for(i = 0; i < max_order; i++) {
88                 /* Sum up this iteration's reflection coefficient. */
89                 r = -autoc[i+1];
90                 for(j = 0; j < i; j++)
91                         r -= lpc[j] * autoc[i-j];
92                 ref[i] = (r/=err);
93
94                 /* Update LPC coefficients and total error. */
95                 lpc[i]=r;
96                 for(j = 0; j < (i>>1); j++) {
97                         double tmp = lpc[j];
98                         lpc[j] += r * lpc[i-1-j];
99                         lpc[i-1-j] += r * tmp;
100                 }
101                 if(i & 1)
102                         lpc[j] += lpc[j] * r;
103
104                 err *= (1.0 - r * r);
105
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;
110         }
111 }
112
113 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift)
114 {
115         unsigned i;
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;
120
121         FLAC__ASSERT(precision > 0);
122         FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
123
124         /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
125         precision--;
126         qmax = 1 << precision;
127         qmin = -qmax;
128         qmax--;
129
130         for(i = 0; i < order; i++) {
131                 if(lp_coeff[i] == 0.0)
132                         continue;
133                 d = fabs(lp_coeff[i]);
134                 if(d > cmax)
135                         cmax = d;
136         }
137 redo_it:
138         if(cmax <= 0.0) {
139                 /* => coefficients are all 0, which means our constant-detect didn't work */
140                 return 2;
141         }
142         else {
143                 int log2cmax;
144
145                 (void)frexp(cmax, &log2cmax);
146                 log2cmax--;
147                 *shift = (int)precision - log2cmax - 1;
148
149                 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
150 #if 0
151                         /*@@@ this does not seem to help at all, but was not extensively tested either: */
152                         if(*shift > max_shiftlimit)
153                                 *shift = max_shiftlimit;
154                         else
155 #endif
156                                 return 1;
157                 }
158         }
159
160         if(*shift >= 0) {
161                 for(i = 0; i < order; i++) {
162                         qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] * (double)(1 << *shift));
163
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)));
168 #endif
169                                 cmax *= 2.0;
170                                 goto redo_it;
171                         }
172                 }
173         }
174         else { /* (*shift < 0) */
175                 const int nshift = -(*shift);
176 #ifdef DEBUG
177                 fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift = %d\n", *shift);
178 #endif
179                 for(i = 0; i < order; i++) {
180                         qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] / (double)(1 << nshift));
181
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)));
186 #endif
187                                 cmax *= 2.0;
188                                 goto redo_it;
189                         }
190                 }
191         }
192
193         return 0;
194 }
195
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[])
197 {
198 #ifdef FLAC__OVERFLOW_DETECT
199         FLAC__int64 sumo;
200 #endif
201         unsigned i, j;
202         FLAC__int32 sum;
203         const FLAC__int32 *history;
204
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);
207         for(i=0;i<order;i++)
208                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
209         fprintf(stderr,"\n");
210 #endif
211         FLAC__ASSERT(order > 0);
212
213         for(i = 0; i < data_len; i++) {
214 #ifdef FLAC__OVERFLOW_DETECT
215                 sumo = 0;
216 #endif
217                 sum = 0;
218                 history = data;
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)
226 #else
227                         if(sumo > 2147483647ll || sumo < -2147483648ll)
228 #endif
229                         {
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);
231                         }
232 #endif
233                 }
234                 *(residual++) = *(data++) - (sum >> lp_quantization);
235         }
236
237         /* Here's a slower but clearer version:
238         for(i = 0; i < data_len; i++) {
239                 sum = 0;
240                 for(j = 0; j < order; j++)
241                         sum += qlp_coeff[j] * data[i-j-1];
242                 residual[i] = data[i] - (sum >> lp_quantization);
243         }
244         */
245 }
246
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[])
248 {
249         unsigned i, j;
250         FLAC__int64 sum;
251         const FLAC__int32 *history;
252
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);
255         for(i=0;i<order;i++)
256                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
257         fprintf(stderr,"\n");
258 #endif
259         FLAC__ASSERT(order > 0);
260
261         for(i = 0; i < data_len; i++) {
262                 sum = 0;
263                 history = data;
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);
269                         break;
270                 }
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));
273                         break;
274                 }
275 #endif
276                 *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
277         }
278 }
279
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[])
281 {
282 #ifdef FLAC__OVERFLOW_DETECT
283         FLAC__int64 sumo;
284 #endif
285         unsigned i, j;
286         FLAC__int32 sum;
287         const FLAC__int32 *history;
288
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);
291         for(i=0;i<order;i++)
292                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
293         fprintf(stderr,"\n");
294 #endif
295         FLAC__ASSERT(order > 0);
296
297         for(i = 0; i < data_len; i++) {
298 #ifdef FLAC__OVERFLOW_DETECT
299                 sumo = 0;
300 #endif
301                 sum = 0;
302                 history = data;
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)
310 #else
311                         if(sumo > 2147483647ll || sumo < -2147483648ll)
312 #endif
313                         {
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);
315                         }
316 #endif
317                 }
318                 *(data++) = *(residual++) + (sum >> lp_quantization);
319         }
320
321         /* Here's a slower but clearer version:
322         for(i = 0; i < data_len; i++) {
323                 sum = 0;
324                 for(j = 0; j < order; j++)
325                         sum += qlp_coeff[j] * data[i-j-1];
326                 data[i] = residual[i] + (sum >> lp_quantization);
327         }
328         */
329 }
330
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[])
332 {
333         unsigned i, j;
334         FLAC__int64 sum;
335         const FLAC__int32 *history;
336
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);
339         for(i=0;i<order;i++)
340                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
341         fprintf(stderr,"\n");
342 #endif
343         FLAC__ASSERT(order > 0);
344
345         for(i = 0; i < data_len; i++) {
346                 sum = 0;
347                 history = data;
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);
353                         break;
354                 }
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));
357                         break;
358                 }
359 #endif
360                 *(data++) = *(residual++) + (FLAC__int32)(sum >> lp_quantization);
361         }
362 }
363
364 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__real lpc_error, unsigned total_samples)
365 {
366         double error_scale;
367
368         FLAC__ASSERT(total_samples > 0);
369
370         error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
371
372         return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
373 }
374
375 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__real lpc_error, double error_scale)
376 {
377         if(lpc_error > 0.0) {
378                 FLAC__real bps = (FLAC__real)((double)0.5 * log(error_scale * lpc_error) / M_LN2);
379                 if(bps >= 0.0)
380                         return bps;
381                 else
382                         return 0.0;
383         }
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;
386         }
387         else {
388                 return 0.0;
389         }
390 }
391
392 unsigned FLAC__lpc_compute_best_order(const FLAC__real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
393 {
394         unsigned order, best_order;
395         FLAC__real best_bits, tmp_bits;
396         double error_scale;
397
398         FLAC__ASSERT(max_order > 0);
399         FLAC__ASSERT(total_samples > 0);
400
401         error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
402
403         best_order = 0;
404         best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (FLAC__real)total_samples;
405
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) {
409                         best_order = order;
410                         best_bits = tmp_bits;
411                 }
412         }
413
414         return best_order+1; /* +1 since index of lpc_error[] is order-1 */
415 }