be more lenient with negative lpc_errors due to accuracy problems with float
[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 <assert.h>
21 #include <math.h>
22 #include <stdio.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 real data[], unsigned data_len, unsigned lag, real autoc[])
34 {
35         /* a readable, but slower, version */
36 #if 0
37         real d;
38         unsigned i;
39
40         assert(lag > 0);
41         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         real d;
55         unsigned sample, coeff;
56         const unsigned limit = data_len - lag;
57
58         assert(lag > 0);
59         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 real autoc[], unsigned max_order, real lp_coeff[][FLAC__MAX_LPC_ORDER], real error[])
76 {
77         unsigned i, j;
78         real r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
79
80         assert(0 < max_order);
81         assert(max_order <= FLAC__MAX_LPC_ORDER);
82         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                         real 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 if(err<0.0)fprintf(stderr,"@@@ err went negative, order=%u (%f,%f)\n",i,err,r);
105
106                 /* save this order */
107                 for(j = 0; j <= i; j++)
108                         lp_coeff[i][j] = -lpc[j]; /* negate FIR filter coeff to get predictor coeff */
109                 error[i] = err;
110         }
111 }
112
113 int FLAC__lpc_quantize_coefficients(const real lp_coeff[], unsigned order, unsigned precision, unsigned bits_per_sample, int32 qlp_coeff[], int *shift)
114 {
115         unsigned i;
116         real d, cmax = -1e10;
117
118         assert(bits_per_sample > 0);
119         assert(bits_per_sample <= sizeof(int32)*8);
120         assert(precision > 0);
121         assert(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
122         assert(precision + bits_per_sample < sizeof(int32)*8);
123 #ifdef NDEBUG
124         (void)bits_per_sample; /* silence compiler warning about unused parameter */
125 #endif
126
127         /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
128         precision--;
129
130         for(i = 0; i < order; i++) {
131                 if(lp_coeff[i] == 0.0)
132                         continue;
133                 d = LOCAL_FABS(lp_coeff[i]);
134                 if(d > cmax)
135                         cmax = d;
136         }
137         if(cmax < 0.0) {
138                 /* => coefficients are all 0, which means our constant-detect didn't work */
139                 return 2;
140         }
141         else {
142                 const int maxshift = (int)precision - (int)floor(log(cmax) / M_LN2) - 1;
143                 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
144                 const int min_shiftlimit = -max_shiftlimit - 1;
145
146                 *shift = maxshift;
147
148                 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
149                         return 1;
150                 }
151         }
152
153         if(*shift != 0) { /* just to avoid wasting time... */
154                 for(i = 0; i < order; i++)
155                         qlp_coeff[i] = (int32)floor(lp_coeff[i] * (real)(1 << *shift));
156         }
157         return 0;
158 }
159
160 void FLAC__lpc_compute_residual_from_qlp_coefficients(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[])
161 {
162 #ifdef FLAC__OVERFLOW_DETECT
163         int64 sumo;
164 #endif
165         unsigned i, j;
166         int32 sum;
167         const int32 *history;
168
169 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
170         fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
171         for(i=0;i<order;i++)
172                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
173         fprintf(stderr,"\n");
174 #endif
175         assert(order > 0);
176
177         for(i = 0; i < data_len; i++) {
178 #ifdef FLAC__OVERFLOW_DETECT
179                 sumo = 0;
180 #endif
181                 sum = 0;
182                 history = data;
183                 for(j = 0; j < order; j++) {
184                         sum += qlp_coeff[j] * (*(--history));
185 #ifdef FLAC__OVERFLOW_DETECT
186                         sumo += (int64)qlp_coeff[j] * (int64)(*history);
187                         if(sumo > 2147483647ll || sumo < -2147483648ll) {
188                                 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);
189                         }
190 #endif
191                 }
192                 *(residual++) = *(data++) - (sum >> lp_quantization);
193         }
194
195         /* Here's a slower but clearer version:
196         for(i = 0; i < data_len; i++) {
197                 sum = 0;
198                 for(j = 0; j < order; j++)
199                         sum += qlp_coeff[j] * data[i-j-1];
200                 residual[i] = data[i] - (sum >> lp_quantization);
201         }
202         */
203 }
204
205 void FLAC__lpc_restore_signal(const int32 residual[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 data[])
206 {
207 #ifdef FLAC__OVERFLOW_DETECT
208         int64 sumo;
209 #endif
210         unsigned i, j;
211         int32 sum;
212         const int32 *history;
213
214 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
215         fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
216         for(i=0;i<order;i++)
217                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
218         fprintf(stderr,"\n");
219 #endif
220         assert(order > 0);
221
222         for(i = 0; i < data_len; i++) {
223 #ifdef FLAC__OVERFLOW_DETECT
224                 sumo = 0;
225 #endif
226                 sum = 0;
227                 history = data;
228                 for(j = 0; j < order; j++) {
229                         sum += qlp_coeff[j] * (*(--history));
230 #ifdef FLAC__OVERFLOW_DETECT
231                         sumo += (int64)qlp_coeff[j] * (int64)(*history);
232                         if(sumo > 2147483647ll || sumo < -2147483648ll) {
233                                 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);
234                         }
235 #endif
236                 }
237                 *(data++) = *(residual++) + (sum >> lp_quantization);
238         }
239
240         /* Here's a slower but clearer version:
241         for(i = 0; i < data_len; i++) {
242                 sum = 0;
243                 for(j = 0; j < order; j++)
244                         sum += qlp_coeff[j] * data[i-j-1];
245                 data[i] = residual[i] + (sum >> lp_quantization);
246         }
247         */
248 }
249
250 real FLAC__lpc_compute_expected_bits_per_residual_sample(real lpc_error, unsigned total_samples)
251 {
252         real error_scale;
253
254         assert(total_samples > 0);
255
256         error_scale = 0.5 * M_LN2 * M_LN2 / (real)total_samples;
257
258         if(lpc_error > 0.0) {
259                 real bps = 0.5 * log(error_scale * lpc_error) / M_LN2;
260                 if(bps >= 0.0)
261                         return bps;
262                 else
263                         return 0.0;
264         }
265         else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
266                 return 1e10;
267         }
268         else {
269                 return 0.0;
270         }
271 }
272
273 real FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(real lpc_error, real error_scale)
274 {
275         if(lpc_error > 0.0) {
276                 real bps = 0.5 * log(error_scale * lpc_error) / M_LN2;
277                 if(bps >= 0.0)
278                         return bps;
279                 else
280                         return 0.0;
281         }
282         else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
283                 return 1e10;
284         }
285         else {
286                 return 0.0;
287         }
288 }
289
290 unsigned FLAC__lpc_compute_best_order(const real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
291 {
292         unsigned order, best_order;
293         real best_bits, tmp_bits;
294         const error_scale;
295
296         assert(max_order > 0);
297         assert(total_samples > 0);
298
299         error_scale = 0.5 * M_LN2 * M_LN2 / (real)total_samples;
300
301         best_order = 0;
302         best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (real)total_samples;
303
304         for(order = 1; order < max_order; order++) {
305                 tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[order], error_scale) * (real)(total_samples - order) + (real)(order * bits_per_signal_sample);
306                 if(tmp_bits < best_bits) {
307                         best_order = order;
308                         best_bits = tmp_bits;
309                 }
310         }
311
312         return best_order+1; /* +1 since index of lpc_error[] is order-1 */
313 }