1 /*
2 * LPC utility code
3 * Copyright (c) 2006 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
26
27 #define LPC_USE_DOUBLE
31
32 /**
33 * Schur recursion.
34 * Produces reflection coefficients from autocorrelation data.
35 */
38 {
41
42 for (
int i = 0;
i < max_order;
i++)
43 gen0[
i] = gen1[
i] = autoc[
i + 1];
44
45 err = autoc[0];
47 err += gen1[0] *
ref[0];
50 for (
int i = 1;
i < max_order;
i++) {
51 for (
int j = 0; j < max_order -
i; j++) {
52 gen1[j] = gen1[j + 1] +
ref[
i - 1] * gen0[j];
53 gen0[j] = gen1[j + 1] *
ref[
i - 1] + gen0[j];
54 }
56 err += gen1[0] *
ref[
i];
59 }
60 }
61
62
63 /**
64 * Apply Welch window function to audio block
65 */
67 double *w_data)
68 {
72
74 w_data[0] = 0.0;
75 return;
76 }
77
79 c = 2.0 / (
len - 1.0);
80
87 }
88 w_data[n2] = 0.0;
89 return;
90 }
91
92 w_data+=n2;
99 }
100 }
101
102 /**
103 * Calculate autocorrelation data from audio samples
104 * A Welch window function is applied before calculation.
105 */
107 double *autoc)
108 {
110
111 for(j=0; j<lag; j+=2){
112 double sum0 = 1.0, sum1 = 1.0;
116 }
117 autoc[j ] = sum0;
118 autoc[j+1] = sum1;
119 }
120
121 if(j==lag){
122 double sum = 1.0;
125 }
126 autoc[j] = sum;
127 }
128 }
129
130 /**
131 * Quantize LPC coefficients
132 */
135 int max_shift, int zero_shift)
136 {
140 int sh;
141
142 /* define maximum levels */
143 qmax = (1 << (precision - 1)) - 1;
144
145 /* find maximum coefficient value */
146 cmax = 0.0;
147 for(
i=0;
i<order;
i++) {
149 }
150
151 /* if maximum value quantizes to zero, return all zeros */
152 if(cmax * (1 << max_shift) < 1.0) {
154 memset(lpc_out, 0,
sizeof(
int32_t) * order);
155 return;
156 }
157
158 /* calculate level shift which scales max coeff to available bits */
159 sh = max_shift;
160 while((cmax * (1 << sh) > qmax) && (sh > min_shift)) {
161 sh--;
162 }
163
164 /* since negative shift values are unsupported in decoder, scale down
165 coefficients instead */
166 if(sh == 0 && cmax > qmax) {
168 for(
i=0;
i<order;
i++) {
170 }
171 }
172
173 /* output quantized coefficients and level shift */
175 for(
i=0;
i<order;
i++) {
176 error -= lpc_in[
i] * (1 << sh);
179 }
181 }
182
184 {
186
187 est = min_order;
188 for(
i=max_order-1;
i>=min_order-1;
i--) {
191 break;
192 }
193 }
194 return est;
195 }
196
199 {
201
202 s->lpc_apply_welch_window(
samples,
s->blocksize,
s->windowed_samples);
203 s->lpc_compute_autocorr(
s->windowed_samples,
s->blocksize, order, autoc);
205
206 return order;
207 }
208
210 int order,
double *
ref)
211 {
213 double signal = 0.0f, avg_err = 0.0f;
215 const double a = 0.5f,
b = 1.0f -
a;
216
217 /* Apply windowing */
218 for (
i = 0;
i <=
len / 2;
i++) {
222 }
223
224 s->lpc_compute_autocorr(
s->windowed_samples,
len, order, autoc);
225 signal = autoc[0];
227 for (
i = 0;
i < order;
i++)
228 avg_err = (avg_err +
error[
i])/2.0f;
229 return avg_err ? signal/avg_err :
NAN;
230 }
231
232 /**
233 * Calculate LPC coefficients for multiple orders
234 *
235 * @param lpc_type LPC method for determining coefficients,
236 * see #FFLPCType for details
237 */
240 int max_order, int precision,
243 int omethod, int min_shift, int max_shift, int zero_shift)
244 {
249 int opt_order;
250
254
255 /* reinit LPC context if parameters have changed */
256 if (blocksize !=
s->blocksize || max_order !=
s->max_order ||
257 lpc_type !=
s->lpc_type) {
260 }
261
262 if(lpc_passes <= 0)
263 lpc_passes = 2;
264
266 s->lpc_apply_welch_window(
samples, blocksize,
s->windowed_samples);
267
268 s->lpc_compute_autocorr(
s->windowed_samples, blocksize, max_order, autoc);
269
271
272 for(
i=0;
i<max_order;
i++)
274
275 pass++;
276 }
277
283
284 /* Avoids initializing with an unused value when lpc_passes == 1 */
285 if (lpc_passes > 1)
286 for(j=0; j<max_order; j++)
287 m[0].
coeff[max_order-1][j] = -lpc[max_order-1][j];
288
289 for(; pass<lpc_passes; pass++){
291
293 for(
i=max_order;
i<blocksize;
i++){
294 for(j=0; j<=max_order; j++)
296
297 if(pass){
298 double eval, inv, rinv;
299 eval= m[pass&1].
evaluate_lls(&m[(pass-1)&1], var+1, max_order-1);
300 eval= (512>>pass) +
fabs(eval - var[0]);
301 inv = 1/eval;
302 rinv = sqrt(inv);
303 for(j=0; j<=max_order; j++)
304 var[j] *= rinv;
306 }else
308
310 }
312 }
313
314 for(
i=0;
i<max_order;
i++){
315 for(j=0; j<max_order; j++)
316 lpc[
i][j]=-m[(pass-1)&1].
coeff[
i][j];
317 ref[
i]= sqrt(m[(pass-1)&1].variance[
i] /
weight) * (blocksize - max_order) / 4000;
318 }
319 for(
i=max_order-1;
i>0;
i--)
321 }
322
323 opt_order = max_order;
324
329 min_shift, max_shift, zero_shift);
330 } else {
331 for(
i=min_order-1;
i<max_order;
i++) {
333 min_shift, max_shift, zero_shift);
334 }
335 }
336
337 return opt_order;
338 }
339
342 {
343 s->blocksize = blocksize;
344 s->max_order = max_order;
345 s->lpc_type = lpc_type;
346
348 sizeof(*
s->windowed_samples));
349 if (!
s->windowed_buffer)
351 s->windowed_samples =
s->windowed_buffer +
FFALIGN(max_order, 4);
352
355
356 #if ARCH_RISCV
358 #elif ARCH_X86
360 #endif
361
362 return 0;
363 }
364
366 {
368 }