#ifdef DEBUG some printouts
[flac.git] / src / libFLAC / lpc.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001  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 <stdio.h>
22 #include "FLAC/assert.h"
23 #include "FLAC/format.h"
24 #include "private/lpc.h"
25
26 #ifndef M_LN2
27 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
28 #define M_LN2 0.69314718055994530942
29 #endif
30
31 #define LOCAL_FABS(x) ((x)<0.0? -(x):(x))
32
33 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
34 {
35         /* a readable, but slower, version */
36 #if 0
37         FLAC__real d;
38         unsigned i;
39
40         FLAC__ASSERT(lag > 0);
41         FLAC__ASSERT(lag <= data_len);
42
43         while(lag--) {
44                 for(i = lag, d = 0.0; i < data_len; i++)
45                         d += data[i] * data[i - lag];
46                 autoc[lag] = d;
47         }
48 #endif
49
50         /*
51          * this version tends to run faster because of better data locality
52          * ('data_len' is usually much larger than 'lag')
53          */
54         FLAC__real d;
55         unsigned sample, coeff;
56         const unsigned limit = data_len - lag;
57
58         FLAC__ASSERT(lag > 0);
59         FLAC__ASSERT(lag <= data_len);
60
61         for(coeff = 0; coeff < lag; coeff++)
62                 autoc[coeff] = 0.0;
63         for(sample = 0; sample <= limit; sample++) {
64                 d = data[sample];
65                 for(coeff = 0; coeff < lag; coeff++)
66                         autoc[coeff] += d * data[sample+coeff];
67         }
68         for(; sample < data_len; sample++) {
69                 d = data[sample];
70                 for(coeff = 0; coeff < data_len - sample; coeff++)
71                         autoc[coeff] += d * data[sample+coeff];
72         }
73 }
74
75 void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__real error[])
76 {
77         unsigned i, j;
78         double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
79
80         FLAC__ASSERT(0 < max_order);
81         FLAC__ASSERT(max_order <= FLAC__MAX_LPC_ORDER);
82         FLAC__ASSERT(autoc[0] != 0.0);
83
84         err = autoc[0];
85
86         for(i = 0; i < max_order; i++) {
87                 /* Sum up this iteration's reflection coefficient. */
88                 r = -autoc[i+1];
89                 for(j = 0; j < i; j++)
90                         r -= lpc[j] * autoc[i-j];
91                 ref[i] = (r/=err);
92
93                 /* Update LPC coefficients and total error. */
94                 lpc[i]=r;
95                 for(j = 0; j < (i>>1); j++) {
96                         double tmp = lpc[j];
97                         lpc[j] += r * lpc[i-1-j];
98                         lpc[i-1-j] += r * tmp;
99                 }
100                 if(i & 1)
101                         lpc[j] += lpc[j] * r;
102
103                 err *= (1.0 - r * r);
104
105                 /* save this order */
106                 for(j = 0; j <= i; j++)
107                         lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
108                 error[i] = (FLAC__real)err;
109         }
110 }
111
112 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, unsigned bits_per_sample, FLAC__int32 qlp_coeff[], int *shift)
113 {
114         unsigned i;
115         double d, cmax = -1e32;
116         FLAC__int32 qmax, qmin;
117         const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
118         const int min_shiftlimit = -max_shiftlimit - 1;
119
120         FLAC__ASSERT(bits_per_sample > 0);
121         FLAC__ASSERT(bits_per_sample <= sizeof(FLAC__int32)*8);
122         FLAC__ASSERT(precision > 0);
123         FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
124         FLAC__ASSERT(precision + bits_per_sample < sizeof(FLAC__int32)*8);
125 #ifdef NDEBUG
126         (void)bits_per_sample; /* silence compiler warning about unused parameter */
127 #endif
128
129         /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
130         precision--;
131         qmax = 1 << precision;
132         qmin = -qmax;
133         qmax--;
134
135         for(i = 0; i < order; i++) {
136                 if(lp_coeff[i] == 0.0)
137                         continue;
138                 d = LOCAL_FABS(lp_coeff[i]);
139                 if(d > cmax)
140                         cmax = d;
141         }
142 redo_it:
143         if(cmax < 0.0) {
144                 /* => coefficients are all 0, which means our constant-detect didn't work */
145                 return 2;
146         }
147         else {
148                 const int log2cmax = (int)floor(log(cmax) / M_LN2); /* this is a good estimate but may not be precise enough, so we have to check for corner cases later when shifting */
149                 const int maxshift = (int)precision - log2cmax - 1;
150
151                 *shift = maxshift;
152
153                 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
154                         return 1;
155                 }
156         }
157
158         if(*shift >= 0) {
159                 for(i = 0; i < order; i++) {
160                         qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] * (double)(1 << *shift));
161
162                         /* check for corner cases mentioned in the comment for log2cmax above */
163                         if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
164 #ifdef FLAC__OVERFLOW_DETECT
165                                 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)));
166 #endif
167                                 cmax *= 2.0;
168                                 goto redo_it;
169                         }
170                 }
171         }
172         else { /* (*shift < 0) */
173                 const int nshift = -(*shift);
174 #ifdef DEBUG
175                 fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift = %d\n", *shift);
176 #endif
177                 for(i = 0; i < order; i++) {
178                         qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] / (double)(1 << nshift));
179
180                         /* check for corner cases mentioned in the comment for log2cmax above */
181                         if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
182 #ifdef FLAC__OVERFLOW_DETECT
183                                 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)));
184 #endif
185                                 cmax *= 2.0;
186                                 goto redo_it;
187                         }
188                 }
189         }
190
191         return 0;
192 }
193
194 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[])
195 {
196 #ifdef FLAC__OVERFLOW_DETECT
197         FLAC__int64 sumo;
198 #endif
199         unsigned i, j;
200         FLAC__int32 sum;
201         const FLAC__int32 *history;
202
203 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
204         fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
205         for(i=0;i<order;i++)
206                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
207         fprintf(stderr,"\n");
208 #endif
209         FLAC__ASSERT(order > 0);
210
211         for(i = 0; i < data_len; i++) {
212 #ifdef FLAC__OVERFLOW_DETECT
213                 sumo = 0;
214 #endif
215                 sum = 0;
216                 history = data;
217                 for(j = 0; j < order; j++) {
218                         sum += qlp_coeff[j] * (*(--history));
219 #ifdef FLAC__OVERFLOW_DETECT
220                         sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
221 #ifdef _MSC_VER /* don't know how to do 64-bit literals in VC++ */
222                         if(sumo < 0) sumo = -sumo;
223                         if(sumo > 2147483647)
224 #else
225                         if(sumo > 2147483647ll || sumo < -2147483648ll)
226 #endif
227                         {
228                                 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);
229                         }
230 #endif
231                 }
232                 *(residual++) = *(data++) - (sum >> lp_quantization);
233         }
234
235         /* Here's a slower but clearer version:
236         for(i = 0; i < data_len; i++) {
237                 sum = 0;
238                 for(j = 0; j < order; j++)
239                         sum += qlp_coeff[j] * data[i-j-1];
240                 residual[i] = data[i] - (sum >> lp_quantization);
241         }
242         */
243 }
244
245 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[])
246 {
247 #ifdef FLAC__OVERFLOW_DETECT
248         FLAC__int64 sumo;
249 #endif
250         unsigned i, j;
251         FLAC__int32 sum;
252         const FLAC__int32 *history;
253
254 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
255         fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
256         for(i=0;i<order;i++)
257                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
258         fprintf(stderr,"\n");
259 #endif
260         FLAC__ASSERT(order > 0);
261
262         for(i = 0; i < data_len; i++) {
263 #ifdef FLAC__OVERFLOW_DETECT
264                 sumo = 0;
265 #endif
266                 sum = 0;
267                 history = data;
268                 for(j = 0; j < order; j++) {
269                         sum += qlp_coeff[j] * (*(--history));
270 #ifdef FLAC__OVERFLOW_DETECT
271                         sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
272 #ifdef _MSC_VER /* don't know how to do 64-bit literals in VC++ */
273                         if(sumo < 0) sumo = -sumo;
274                         if(sumo > 2147483647)
275 #else
276                         if(sumo > 2147483647ll || sumo < -2147483648ll)
277 #endif
278                         {
279                                 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);
280                         }
281 #endif
282                 }
283                 *(data++) = *(residual++) + (sum >> lp_quantization);
284         }
285
286         /* Here's a slower but clearer version:
287         for(i = 0; i < data_len; i++) {
288                 sum = 0;
289                 for(j = 0; j < order; j++)
290                         sum += qlp_coeff[j] * data[i-j-1];
291                 data[i] = residual[i] + (sum >> lp_quantization);
292         }
293         */
294 }
295
296 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__real lpc_error, unsigned total_samples)
297 {
298         double error_scale;
299
300         FLAC__ASSERT(total_samples > 0);
301
302         error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
303
304         if(lpc_error > 0.0) {
305                 FLAC__real bps = (FLAC__real)((double)0.5 * log(error_scale * lpc_error) / M_LN2);
306                 if(bps >= 0.0)
307                         return bps;
308                 else
309                         return 0.0;
310         }
311         else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
312                 return (FLAC__real)1e32;
313         }
314         else {
315                 return 0.0;
316         }
317 }
318
319 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__real lpc_error, double error_scale)
320 {
321         if(lpc_error > 0.0) {
322                 FLAC__real bps = (FLAC__real)((double)0.5 * log(error_scale * lpc_error) / M_LN2);
323                 if(bps >= 0.0)
324                         return bps;
325                 else
326                         return 0.0;
327         }
328         else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
329                 return (FLAC__real)1e32;
330         }
331         else {
332                 return 0.0;
333         }
334 }
335
336 unsigned FLAC__lpc_compute_best_order(const FLAC__real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
337 {
338         unsigned order, best_order;
339         FLAC__real best_bits, tmp_bits;
340         double error_scale;
341
342         FLAC__ASSERT(max_order > 0);
343         FLAC__ASSERT(total_samples > 0);
344
345         error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
346
347         best_order = 0;
348         best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (FLAC__real)total_samples;
349
350         for(order = 1; order < max_order; order++) {
351                 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);
352                 if(tmp_bits < best_bits) {
353                         best_order = order;
354                         best_bits = tmp_bits;
355                 }
356         }
357
358         return best_order+1; /* +1 since index of lpc_error[] is order-1 */
359 }