27cfc4b8c203b390497dfbffcf8d42d6db3d24c5
[platform/upstream/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 void FLAC__lpc_compute_autocorrelation(const real data[], unsigned data_len, unsigned lag, real autoc[])
32 {
33         real d;
34         unsigned i;
35
36         assert(lag > 0);
37         assert(lag <= data_len);
38
39         while(lag--) {
40                 for(i = lag, d = 0.0; i < data_len; i++)
41                         d += data[i] * data[i - lag];
42                 autoc[lag] = d;
43         }
44 }
45
46 void FLAC__lpc_compute_lp_coefficients(const real autoc[], unsigned max_order, real lp_coeff[][FLAC__MAX_LPC_ORDER], real error[])
47 {
48         unsigned i, j;
49         real r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
50
51         assert(0 < max_order);
52         assert(max_order <= FLAC__MAX_LPC_ORDER);
53         assert(autoc[0] != 0.0);
54
55         err = autoc[0];
56
57         for(i = 0; i < max_order; i++) {
58                 /* Sum up this iteration's reflection coefficient. */
59                 r =- autoc[i+1];
60                 for(j = 0; j < i; j++)
61                         r -= lpc[j] * autoc[i-j];
62                 ref[i] = (r/=err);
63
64                 /* Update LPC coefficients and total error. */
65                 lpc[i]=r;
66                 for(j = 0; j < (i>>1); j++) {
67                         real tmp = lpc[j];
68                         lpc[j] += r * lpc[i-1-j];
69                         lpc[i-1-j] += r * tmp;
70                 }
71                 if(i & 1)
72                         lpc[j] += lpc[j] * r;
73
74                 err *= (1.0 - r * r);
75
76                 /* save this order */
77                 for(j = 0; j <= i; j++)
78                         lp_coeff[i][j] = -lpc[j]; /* N.B. why do we have to negate here? */
79                 error[i] = err;
80         }
81 }
82
83 #if 0
84 int FLAC__lpc_quantize_coefficients(const real lp_coeff[], unsigned order, unsigned precision, unsigned bits_per_sample, int32 qlp_coeff[], int *shift)
85 {
86         unsigned i;
87         real d, rprecision = (real)precision, maxlog = -1e99, minlog = 1e99;
88
89         assert(bits_per_sample > 0);
90         assert(bits_per_sample <= sizeof(int32)*8);
91         assert(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
92         assert(precision + bits_per_sample < sizeof(int32)*8);
93 #ifdef NDEBUG
94         (void)bits_per_sample; /* silence compiler warning about unused parameter */
95 #endif
96
97         for(i = 0; i < order; i++) {
98                 if(lp_coeff[i] == 0.0)
99                         continue;
100                 d = log(fabs(lp_coeff[i])) / M_LN2;
101                 if(d > maxlog)
102                         maxlog = d;
103                 if(d < minlog)
104                         minlog = d;
105         }
106         if(maxlog < minlog)
107                 return 2;
108         else if(maxlog - minlog >= (real)(precision+1))
109                 return 1;
110         else if((rprecision-1.0) - maxlog >= (real)(precision+1))
111                 rprecision = (real)precision + maxlog + 1.0;
112
113         *shift = (int)floor((rprecision-1.0) - maxlog); /* '-1' because *shift can be negative and the sign bit costs 1 bit */
114         if(*shift > (int)precision || *shift <= -(int)precision) {
115                 fprintf(stderr, "@@@ FLAC__lpc_quantize_coefficients(): ERROR: *shift=%d, maxlog=%f, minlog=%f, precision=%u, rprecision=%f\n", *shift, maxlog, minlog, precision, rprecision);
116                 return 1;
117         }
118
119         if(*shift != 0) { /* just to avoid wasting time... */
120                 for(i = 0; i < order; i++)
121                         qlp_coeff[i] = (int32)floor(lp_coeff[i] * (real)(1 << *shift));
122         }
123         return 0;
124 }
125 #endif
126
127 int FLAC__lpc_quantize_coefficients(const real lp_coeff[], unsigned order, unsigned precision, unsigned bits_per_sample, int32 qlp_coeff[], int *shift)
128 {
129         unsigned i;
130         real d, cmax = -1e99;//@@@, cmin = 1e99;
131
132         assert(bits_per_sample > 0);
133         assert(bits_per_sample <= sizeof(int32)*8);
134         assert(precision > 0);
135         assert(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
136         assert(precision + bits_per_sample < sizeof(int32)*8);
137 #ifdef NDEBUG
138         (void)bits_per_sample; /* silence compiler warning about unused parameter */
139 #endif
140
141         /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
142         precision--;
143
144         for(i = 0; i < order; i++) {
145                 if(lp_coeff[i] == 0.0)
146                         continue;
147                 d = fabs(lp_coeff[i]);
148                 if(d > cmax)
149                         cmax = d;
150 //@@@           if(d < cmin)
151 //@@@                   cmin = d;
152         }
153 //@@@   if(cmax < cmin)
154         if(cmax < 0) {
155                 /* => coeffients are all 0, which means our constant-detect didn't work */
156 fprintf(stderr,"@@@ LPCQ ERROR, all lpc_coeffs are 0\n");
157                 return 2;
158         }
159         else {
160 //@@@           const int minshift = (int)precision - floor(log(cmin) / M_LN2) - 1;
161                 const int maxshift = (int)precision - floor(log(cmax) / M_LN2) - 1;
162 //@@@           assert(maxshift >= minshift);
163                 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
164                 const int min_shiftlimit = -max_shiftlimit - 1;
165
166                 *shift = maxshift;
167
168                 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
169 fprintf(stderr,"@@@ LPCQ ERROR, shift is outside shiftlimit\n");
170                         return 1;
171                 }
172         }
173
174         if(*shift != 0) { /* just to avoid wasting time... */
175                 for(i = 0; i < order; i++)
176                         qlp_coeff[i] = (int32)floor(lp_coeff[i] * (real)(1 << *shift));
177         }
178         return 0;
179 }
180
181 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[])
182 {
183 #ifdef FLAC_OVERFLOW_DETECT
184         int64 sumo;
185 #endif
186         unsigned i, j;
187         int32 sum;
188         const int32 *history;
189
190 #ifdef FLAC_OVERFLOW_DETECT_VERBOSE
191         fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
192         for(i=0;i<order;i++)
193                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
194         fprintf(stderr,"\n");
195 #endif
196         assert(order > 0);
197
198         for(i = 0; i < data_len; i++) {
199 #ifdef FLAC_OVERFLOW_DETECT
200                 sumo = 0;
201 #endif
202                 sum = 0;
203                 history = data;
204                 for(j = 0; j < order; j++) {
205                         sum += qlp_coeff[j] * (*(--history));
206 #ifdef FLAC_OVERFLOW_DETECT
207                         sumo += (int64)qlp_coeff[j] * (int64)(*history);
208                         if(sumo > 2147483647ll || sumo < -2147483648ll) {
209                                 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);
210                         }
211 #endif
212                 }
213                 *(residual++) = *(data++) - (sum >> lp_quantization);
214         }
215
216         /* Here's a slower but clearer version:
217         for(i = 0; i < data_len; i++) {
218                 sum = 0;
219                 for(j = 0; j < order; j++)
220                         sum += qlp_coeff[j] * data[i-j-1];
221                 residual[i] = data[i] - (sum >> lp_quantization);
222         }
223         */
224 }
225
226 void FLAC__lpc_restore_signal(const int32 residual[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 data[])
227 {
228 #ifdef FLAC_OVERFLOW_DETECT
229         int64 sumo;
230 #endif
231         unsigned i, j;
232         int32 sum;
233         const int32 *history;
234
235 #ifdef FLAC_OVERFLOW_DETECT_VERBOSE
236         fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
237         for(i=0;i<order;i++)
238                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
239         fprintf(stderr,"\n");
240 #endif
241         assert(order > 0);
242
243         for(i = 0; i < data_len; i++) {
244 #ifdef FLAC_OVERFLOW_DETECT
245                 sumo = 0;
246 #endif
247                 sum = 0;
248                 history = data;
249                 for(j = 0; j < order; j++) {
250                         sum += qlp_coeff[j] * (*(--history));
251 #ifdef FLAC_OVERFLOW_DETECT
252                         sumo += (int64)qlp_coeff[j] * (int64)(*history);
253                         if(sumo > 2147483647ll || sumo < -2147483648ll) {
254                                 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);
255                         }
256 #endif
257                 }
258                 *(data++) = *(residual++) + (sum >> lp_quantization);
259         }
260
261         /* Here's a slower but clearer version:
262         for(i = 0; i < data_len; i++) {
263                 sum = 0;
264                 for(j = 0; j < order; j++)
265                         sum += qlp_coeff[j] * data[i-j-1];
266                 data[i] = residual[i] + (sum >> lp_quantization);
267         }
268         */
269 }
270
271 real FLAC__lpc_compute_expected_bits_per_residual_sample(real lpc_error, unsigned total_samples)
272 {
273         real escale;
274
275         assert(lpc_error >= 0.0); /* the error can never be negative */
276         assert(total_samples > 0);
277
278         escale = 0.5 * M_LN2 * M_LN2 / (real)total_samples;
279
280         if(lpc_error > 0.0) {
281                 real bps = 0.5 * log(escale * lpc_error) / M_LN2;
282                 if(bps >= 0.0)
283                         return bps;
284                 else
285                         return 0.0;
286         }
287         else {
288                 return 0.0;
289         }
290 }
291
292 unsigned FLAC__lpc_compute_best_order(const real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
293 {
294         unsigned order, best_order;
295         real best_bits, tmp_bits;
296
297         assert(max_order > 0);
298
299         best_order = 0;
300         best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[0], total_samples) * (real)total_samples;
301
302         for(order = 1; order < max_order; order++) {
303                 tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[order], total_samples) * (real)(total_samples - order) + (real)(order * bits_per_signal_sample);
304                 if(tmp_bits < best_bits) {
305                         best_order = order;
306                         best_bits = tmp_bits;
307                 }
308         }
309
310         return best_order+1; /* +1 since index of lpc_error[] is order-1 */
311 }