annotate src/flac/libflac/lpc.c @ 1218:cc4e03df48a3

Make things not crash.
author William Pitcock <nenolod@atheme-project.org>
date Sun, 08 Jul 2007 22:25:12 -0500
parents 454ad11020ec
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
715
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
1 /* libFLAC - Free Lossless Audio Codec library
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
3 *
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
4 * Redistribution and use in source and binary forms, with or without
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
5 * modification, are permitted provided that the following conditions
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
6 * are met:
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
7 *
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
8 * - Redistributions of source code must retain the above copyright
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
10 *
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
11 * - Redistributions in binary form must reproduce the above copyright
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer in the
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
13 * documentation and/or other materials provided with the distribution.
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
14 *
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
15 * - Neither the name of the Xiph.org Foundation nor the names of its
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
16 * contributors may be used to endorse or promote products derived from
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
17 * this software without specific prior written permission.
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
18 *
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
30 */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
31
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
32 #if HAVE_CONFIG_H
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
33 # include <config.h>
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
34 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
35
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
36 #include <math.h>
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
37 #include "FLAC/assert.h"
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
38 #include "FLAC/format.h"
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
39 #include "private/bitmath.h"
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
40 #include "private/lpc.h"
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
41 #if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
42 #include <stdio.h>
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
43 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
44
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
45 #ifndef FLAC__INTEGER_ONLY_LIBRARY
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
46
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
47 #ifndef M_LN2
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
48 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
49 #define M_LN2 0.69314718055994530942
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
50 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
51
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
52 void FLAC__lpc_window_data(const FLAC__real in[], const FLAC__real window[], FLAC__real out[], unsigned data_len)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
53 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
54 unsigned i;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
55 for(i = 0; i < data_len; i++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
56 out[i] = in[i] * window[i];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
57 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
58
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
59 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
60 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
61 /* a readable, but slower, version */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
62 #if 0
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
63 FLAC__real d;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
64 unsigned i;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
65
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
66 FLAC__ASSERT(lag > 0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
67 FLAC__ASSERT(lag <= data_len);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
68
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
69 /*
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
70 * Technically we should subtract the mean first like so:
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
71 * for(i = 0; i < data_len; i++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
72 * data[i] -= mean;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
73 * but it appears not to make enough of a difference to matter, and
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
74 * most signals are already closely centered around zero
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
75 */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
76 while(lag--) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
77 for(i = lag, d = 0.0; i < data_len; i++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
78 d += data[i] * data[i - lag];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
79 autoc[lag] = d;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
80 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
81 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
82
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
83 /*
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
84 * this version tends to run faster because of better data locality
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
85 * ('data_len' is usually much larger than 'lag')
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
86 */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
87 FLAC__real d;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
88 unsigned sample, coeff;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
89 const unsigned limit = data_len - lag;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
90
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
91 FLAC__ASSERT(lag > 0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
92 FLAC__ASSERT(lag <= data_len);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
93
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
94 for(coeff = 0; coeff < lag; coeff++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
95 autoc[coeff] = 0.0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
96 for(sample = 0; sample <= limit; sample++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
97 d = data[sample];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
98 for(coeff = 0; coeff < lag; coeff++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
99 autoc[coeff] += d * data[sample+coeff];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
100 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
101 for(; sample < data_len; sample++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
102 d = data[sample];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
103 for(coeff = 0; coeff < data_len - sample; coeff++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
104 autoc[coeff] += d * data[sample+coeff];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
105 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
106 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
107
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
108 void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[])
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
109 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
110 unsigned i, j;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
111 FLAC__double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
112
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
113 FLAC__ASSERT(0 != max_order);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
114 FLAC__ASSERT(0 < *max_order);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
115 FLAC__ASSERT(*max_order <= FLAC__MAX_LPC_ORDER);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
116 FLAC__ASSERT(autoc[0] != 0.0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
117
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
118 err = autoc[0];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
119
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
120 for(i = 0; i < *max_order; i++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
121 /* Sum up this iteration's reflection coefficient. */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
122 r = -autoc[i+1];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
123 for(j = 0; j < i; j++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
124 r -= lpc[j] * autoc[i-j];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
125 ref[i] = (r/=err);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
126
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
127 /* Update LPC coefficients and total error. */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
128 lpc[i]=r;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
129 for(j = 0; j < (i>>1); j++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
130 FLAC__double tmp = lpc[j];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
131 lpc[j] += r * lpc[i-1-j];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
132 lpc[i-1-j] += r * tmp;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
133 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
134 if(i & 1)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
135 lpc[j] += lpc[j] * r;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
136
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
137 err *= (1.0 - r * r);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
138
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
139 /* save this order */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
140 for(j = 0; j <= i; j++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
141 lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
142 error[i] = err;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
143
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
144 /* see SF bug #1601812 http://sourceforge.net/tracker/index.php?func=detail&aid=1601812&group_id=13478&atid=113478 */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
145 if(err == 0.0) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
146 *max_order = i+1;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
147 return;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
148 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
149 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
150 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
151
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
152 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
153 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
154 unsigned i;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
155 FLAC__double cmax;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
156 FLAC__int32 qmax, qmin;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
157
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
158 FLAC__ASSERT(precision > 0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
159 FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
160
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
161 /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
162 precision--;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
163 qmax = 1 << precision;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
164 qmin = -qmax;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
165 qmax--;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
166
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
167 /* calc cmax = max( |lp_coeff[i]| ) */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
168 cmax = 0.0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
169 for(i = 0; i < order; i++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
170 const FLAC__double d = fabs(lp_coeff[i]);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
171 if(d > cmax)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
172 cmax = d;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
173 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
174
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
175 if(cmax <= 0.0) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
176 /* => coefficients are all 0, which means our constant-detect didn't work */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
177 return 2;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
178 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
179 else {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
180 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
181 const int min_shiftlimit = -max_shiftlimit - 1;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
182 int log2cmax;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
183
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
184 (void)frexp(cmax, &log2cmax);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
185 log2cmax--;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
186 *shift = (int)precision - log2cmax - 1;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
187
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
188 if(*shift > max_shiftlimit)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
189 *shift = max_shiftlimit;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
190 else if(*shift < min_shiftlimit)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
191 return 1;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
192 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
193
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
194 if(*shift >= 0) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
195 FLAC__double error = 0.0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
196 FLAC__int32 q;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
197 for(i = 0; i < order; i++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
198 error += lp_coeff[i] * (1 << *shift);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
199 #if 1 /* unfortunately lround() is C99 */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
200 if(error >= 0.0)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
201 q = (FLAC__int32)(error + 0.5);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
202 else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
203 q = (FLAC__int32)(error - 0.5);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
204 #else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
205 q = lround(error);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
206 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
207 #ifdef FLAC__OVERFLOW_DETECT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
208 if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
209 fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
210 else if(q < qmin)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
211 fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
212 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
213 if(q > qmax)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
214 q = qmax;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
215 else if(q < qmin)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
216 q = qmin;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
217 error -= q;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
218 qlp_coeff[i] = q;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
219 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
220 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
221 /* negative shift is very rare but due to design flaw, negative shift is
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
222 * a NOP in the decoder, so it must be handled specially by scaling down
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
223 * coeffs
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
224 */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
225 else {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
226 const int nshift = -(*shift);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
227 FLAC__double error = 0.0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
228 FLAC__int32 q;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
229 #ifdef DEBUG
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
230 fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
231 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
232 for(i = 0; i < order; i++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
233 error += lp_coeff[i] / (1 << nshift);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
234 #if 1 /* unfortunately lround() is C99 */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
235 if(error >= 0.0)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
236 q = (FLAC__int32)(error + 0.5);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
237 else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
238 q = (FLAC__int32)(error - 0.5);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
239 #else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
240 q = lround(error);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
241 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
242 #ifdef FLAC__OVERFLOW_DETECT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
243 if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
244 fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
245 else if(q < qmin)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
246 fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
247 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
248 if(q > qmax)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
249 q = qmax;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
250 else if(q < qmin)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
251 q = qmin;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
252 error -= q;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
253 qlp_coeff[i] = q;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
254 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
255 *shift = 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
256 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
257
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
258 return 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
259 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
260
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
261 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[])
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
262 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
263 #ifdef FLAC__OVERFLOW_DETECT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
264 FLAC__int64 sumo;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
265 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
266 unsigned i, j;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
267 FLAC__int32 sum;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
268 const FLAC__int32 *history;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
269
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
270 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
271 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
272 for(i=0;i<order;i++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
273 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
274 fprintf(stderr,"\n");
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
275 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
276 FLAC__ASSERT(order > 0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
277
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
278 for(i = 0; i < data_len; i++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
279 #ifdef FLAC__OVERFLOW_DETECT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
280 sumo = 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
281 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
282 sum = 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
283 history = data;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
284 for(j = 0; j < order; j++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
285 sum += qlp_coeff[j] * (*(--history));
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
286 #ifdef FLAC__OVERFLOW_DETECT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
287 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
288 #if defined _MSC_VER
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
289 if(sumo > 2147483647I64 || sumo < -2147483648I64)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
290 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
291 #else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
292 if(sumo > 2147483647ll || sumo < -2147483648ll)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
293 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,(long long)sumo);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
294 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
295 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
296 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
297 *(residual++) = *(data++) - (sum >> lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
298 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
299
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
300 /* Here's a slower but clearer version:
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
301 for(i = 0; i < data_len; i++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
302 sum = 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
303 for(j = 0; j < order; j++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
304 sum += qlp_coeff[j] * data[i-j-1];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
305 residual[i] = data[i] - (sum >> lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
306 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
307 */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
308 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
309
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
310 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[])
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
311 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
312 unsigned i, j;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
313 FLAC__int64 sum;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
314 const FLAC__int32 *history;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
315
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
316 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
317 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
318 for(i=0;i<order;i++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
319 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
320 fprintf(stderr,"\n");
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
321 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
322 FLAC__ASSERT(order > 0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
323
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
324 for(i = 0; i < data_len; i++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
325 sum = 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
326 history = data;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
327 for(j = 0; j < order; j++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
328 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
329 #ifdef FLAC__OVERFLOW_DETECT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
330 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
331 #if defined _MSC_VER
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
332 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
333 #else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
334 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
335 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
336 break;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
337 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
338 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
339 #if defined _MSC_VER
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
340 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%I64d, residual=%I64d\n", i, *data, sum >> lp_quantization, (FLAC__int64)(*data) - (sum >> lp_quantization));
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
341 #else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
342 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%lld, residual=%lld\n", i, *data, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*data) - (sum >> lp_quantization)));
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
343 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
344 break;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
345 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
346 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
347 *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
348 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
349 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
350
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
351 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
352
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
353 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[])
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
354 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
355 #ifdef FLAC__OVERFLOW_DETECT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
356 FLAC__int64 sumo;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
357 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
358 unsigned i, j;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
359 FLAC__int32 sum;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
360 const FLAC__int32 *r = residual, *history;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
361
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
362 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
363 fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
364 for(i=0;i<order;i++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
365 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
366 fprintf(stderr,"\n");
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
367 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
368 FLAC__ASSERT(order > 0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
369
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
370 for(i = 0; i < data_len; i++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
371 #ifdef FLAC__OVERFLOW_DETECT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
372 sumo = 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
373 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
374 sum = 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
375 history = data;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
376 for(j = 0; j < order; j++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
377 sum += qlp_coeff[j] * (*(--history));
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
378 #ifdef FLAC__OVERFLOW_DETECT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
379 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
380 #if defined _MSC_VER
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
381 if(sumo > 2147483647I64 || sumo < -2147483648I64)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
382 fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
383 #else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
384 if(sumo > 2147483647ll || sumo < -2147483648ll)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
385 fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
386 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
387 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
388 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
389 *(data++) = *(r++) + (sum >> lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
390 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
391
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
392 /* Here's a slower but clearer version:
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
393 for(i = 0; i < data_len; i++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
394 sum = 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
395 for(j = 0; j < order; j++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
396 sum += qlp_coeff[j] * data[i-j-1];
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
397 data[i] = residual[i] + (sum >> lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
398 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
399 */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
400 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
401
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
402 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[])
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
403 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
404 unsigned i, j;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
405 FLAC__int64 sum;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
406 const FLAC__int32 *r = residual, *history;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
407
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
408 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
409 fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
410 for(i=0;i<order;i++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
411 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
412 fprintf(stderr,"\n");
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
413 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
414 FLAC__ASSERT(order > 0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
415
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
416 for(i = 0; i < data_len; i++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
417 sum = 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
418 history = data;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
419 for(j = 0; j < order; j++)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
420 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
421 #ifdef FLAC__OVERFLOW_DETECT
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
422 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
423 #ifdef _MSC_VER
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
424 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%I64d\n", i, sum >> lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
425 #else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
426 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
427 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
428 break;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
429 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
430 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
431 #ifdef _MSC_VER
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
432 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%I64d, data=%I64d\n", i, *r, sum >> lp_quantization, (FLAC__int64)(*r) + (sum >> lp_quantization));
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
433 #else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
434 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%lld, data=%lld\n", i, *r, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*r) + (sum >> lp_quantization)));
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
435 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
436 break;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
437 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
438 #endif
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
439 *(data++) = *(r++) + (FLAC__int32)(sum >> lp_quantization);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
440 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
441 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
442
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
443 #ifndef FLAC__INTEGER_ONLY_LIBRARY
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
444
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
445 FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
446 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
447 FLAC__double error_scale;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
448
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
449 FLAC__ASSERT(total_samples > 0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
450
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
451 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
452
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
453 return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
454 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
455
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
456 FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
457 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
458 if(lpc_error > 0.0) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
459 FLAC__double bps = (FLAC__double)0.5 * log(error_scale * lpc_error) / M_LN2;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
460 if(bps >= 0.0)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
461 return bps;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
462 else
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
463 return 0.0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
464 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
465 else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate floating-point resolution */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
466 return 1e32;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
467 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
468 else {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
469 return 0.0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
470 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
471 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
472
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
473 unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order)
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
474 {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
475 unsigned order, index, best_index; /* 'index' the index into lpc_error; index==order-1 since lpc_error[0] is for order==1, lpc_error[1] is for order==2, etc */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
476 FLAC__double bits, best_bits, error_scale;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
477
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
478 FLAC__ASSERT(max_order > 0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
479 FLAC__ASSERT(total_samples > 0);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
480
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
481 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
482
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
483 best_index = 0;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
484 best_bits = (unsigned)(-1);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
485
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
486 for(index = 0, order = 1; index < max_order; index++, order++) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
487 bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[index], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * overhead_bits_per_order);
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
488 if(bits < best_bits) {
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
489 best_index = index;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
490 best_bits = bits;
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
491 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
492 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
493
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
494 return best_index+1; /* +1 since index of lpc_error[] is order-1 */
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
495 }
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
496
a9b178bc4ae4 [svn] Import flac. Please test.
js
parents:
diff changeset
497 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */