remove old version of the lpcoeff quantizer, other debug detritus
[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, cmax = -1e99;
88
89         assert(bits_per_sample > 0);
90         assert(bits_per_sample <= sizeof(int32)*8);
91         assert(precision > 0);
92         assert(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
93         assert(precision + bits_per_sample < sizeof(int32)*8);
94 #ifdef NDEBUG
95         (void)bits_per_sample; /* silence compiler warning about unused parameter */
96 #endif
97
98         /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
99         precision--;
100
101         for(i = 0; i < order; i++) {
102                 if(lp_coeff[i] == 0.0)
103                         continue;
104                 d = fabs(lp_coeff[i]);
105                 if(d > cmax)
106                         cmax = d;
107         }
108         if(cmax < 0) {
109                 /* => coeffients are all 0, which means our constant-detect didn't work */
110                 return 2;
111         }
112         else {
113                 const int maxshift = (int)precision - floor(log(cmax) / M_LN2) - 1;
114                 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
115                 const int min_shiftlimit = -max_shiftlimit - 1;
116
117                 *shift = maxshift;
118
119                 if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
120                         return 1;
121                 }
122         }
123
124         if(*shift != 0) { /* just to avoid wasting time... */
125                 for(i = 0; i < order; i++)
126                         qlp_coeff[i] = (int32)floor(lp_coeff[i] * (real)(1 << *shift));
127         }
128         return 0;
129 }
130
131 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[])
132 {
133 #ifdef FLAC_OVERFLOW_DETECT
134         int64 sumo;
135 #endif
136         unsigned i, j;
137         int32 sum;
138         const int32 *history;
139
140 #ifdef FLAC_OVERFLOW_DETECT_VERBOSE
141         fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
142         for(i=0;i<order;i++)
143                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
144         fprintf(stderr,"\n");
145 #endif
146         assert(order > 0);
147
148         for(i = 0; i < data_len; i++) {
149 #ifdef FLAC_OVERFLOW_DETECT
150                 sumo = 0;
151 #endif
152                 sum = 0;
153                 history = data;
154                 for(j = 0; j < order; j++) {
155                         sum += qlp_coeff[j] * (*(--history));
156 #ifdef FLAC_OVERFLOW_DETECT
157                         sumo += (int64)qlp_coeff[j] * (int64)(*history);
158                         if(sumo > 2147483647ll || sumo < -2147483648ll) {
159                                 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);
160                         }
161 #endif
162                 }
163                 *(residual++) = *(data++) - (sum >> lp_quantization);
164         }
165
166         /* Here's a slower but clearer version:
167         for(i = 0; i < data_len; i++) {
168                 sum = 0;
169                 for(j = 0; j < order; j++)
170                         sum += qlp_coeff[j] * data[i-j-1];
171                 residual[i] = data[i] - (sum >> lp_quantization);
172         }
173         */
174 }
175
176 void FLAC__lpc_restore_signal(const int32 residual[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 data[])
177 {
178 #ifdef FLAC_OVERFLOW_DETECT
179         int64 sumo;
180 #endif
181         unsigned i, j;
182         int32 sum;
183         const int32 *history;
184
185 #ifdef FLAC_OVERFLOW_DETECT_VERBOSE
186         fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
187         for(i=0;i<order;i++)
188                 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
189         fprintf(stderr,"\n");
190 #endif
191         assert(order > 0);
192
193         for(i = 0; i < data_len; i++) {
194 #ifdef FLAC_OVERFLOW_DETECT
195                 sumo = 0;
196 #endif
197                 sum = 0;
198                 history = data;
199                 for(j = 0; j < order; j++) {
200                         sum += qlp_coeff[j] * (*(--history));
201 #ifdef FLAC_OVERFLOW_DETECT
202                         sumo += (int64)qlp_coeff[j] * (int64)(*history);
203                         if(sumo > 2147483647ll || sumo < -2147483648ll) {
204                                 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);
205                         }
206 #endif
207                 }
208                 *(data++) = *(residual++) + (sum >> lp_quantization);
209         }
210
211         /* Here's a slower but clearer version:
212         for(i = 0; i < data_len; i++) {
213                 sum = 0;
214                 for(j = 0; j < order; j++)
215                         sum += qlp_coeff[j] * data[i-j-1];
216                 data[i] = residual[i] + (sum >> lp_quantization);
217         }
218         */
219 }
220
221 real FLAC__lpc_compute_expected_bits_per_residual_sample(real lpc_error, unsigned total_samples)
222 {
223         real escale;
224
225         assert(lpc_error >= 0.0); /* the error can never be negative */
226         assert(total_samples > 0);
227
228         escale = 0.5 * M_LN2 * M_LN2 / (real)total_samples;
229
230         if(lpc_error > 0.0) {
231                 real bps = 0.5 * log(escale * lpc_error) / M_LN2;
232                 if(bps >= 0.0)
233                         return bps;
234                 else
235                         return 0.0;
236         }
237         else {
238                 return 0.0;
239         }
240 }
241
242 unsigned FLAC__lpc_compute_best_order(const real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
243 {
244         unsigned order, best_order;
245         real best_bits, tmp_bits;
246
247         assert(max_order > 0);
248
249         best_order = 0;
250         best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[0], total_samples) * (real)total_samples;
251
252         for(order = 1; order < max_order; order++) {
253                 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);
254                 if(tmp_bits < best_bits) {
255                         best_order = order;
256                         best_bits = tmp_bits;
257                 }
258         }
259
260         return best_order+1; /* +1 since index of lpc_error[] is order-1 */
261 }