1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "FLAC/assert.h"
39 #include "FLAC/format.h"
40 #include "share/compat.h"
41 #include "private/bitmath.h"
42 #include "private/lpc.h"
43 #include "private/macros.h"
44 #if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
48 /* OPT: #undef'ing this may improve the speed on some architectures */
49 #define FLAC__LPC_UNROLLED_FILTER_LOOPS
51 #ifndef FLAC__INTEGER_ONLY_LIBRARY
54 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
55 #define M_LN2 0.69314718055994530942
58 #if !defined(HAVE_LROUND)
61 #define copysign _copysign
62 #elif defined(__GNUC__)
63 #define copysign __builtin_copysign
65 static inline long int lround(double x) {
66 return (long)(x + copysign (0.5, x));
68 //If this fails, we are in the precence of a mid 90's compiler..move along...
71 void FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], unsigned data_len)
74 for(i = 0; i < data_len; i++)
75 out[i] = in[i] * window[i];
78 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
80 /* a readable, but slower, version */
85 FLAC__ASSERT(lag > 0);
86 FLAC__ASSERT(lag <= data_len);
89 * Technically we should subtract the mean first like so:
90 * for(i = 0; i < data_len; i++)
92 * but it appears not to make enough of a difference to matter, and
93 * most signals are already closely centered around zero
96 for(i = lag, d = 0.0; i < data_len; i++)
97 d += data[i] * data[i - lag];
103 * this version tends to run faster because of better data locality
104 * ('data_len' is usually much larger than 'lag')
107 unsigned sample, coeff;
108 const unsigned limit = data_len - lag;
110 FLAC__ASSERT(lag > 0);
111 FLAC__ASSERT(lag <= data_len);
113 for(coeff = 0; coeff < lag; coeff++)
115 for(sample = 0; sample <= limit; sample++) {
117 for(coeff = 0; coeff < lag; coeff++)
118 autoc[coeff] += d * data[sample+coeff];
120 for(; sample < data_len; sample++) {
122 for(coeff = 0; coeff < data_len - sample; coeff++)
123 autoc[coeff] += d * data[sample+coeff];
127 void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[])
130 FLAC__double r, err, lpc[FLAC__MAX_LPC_ORDER];
132 FLAC__ASSERT(0 != max_order);
133 FLAC__ASSERT(0 < *max_order);
134 FLAC__ASSERT(*max_order <= FLAC__MAX_LPC_ORDER);
135 FLAC__ASSERT(autoc[0] != 0.0);
139 for(i = 0; i < *max_order; i++) {
140 /* Sum up this iteration's reflection coefficient. */
142 for(j = 0; j < i; j++)
143 r -= lpc[j] * autoc[i-j];
146 /* Update LPC coefficients and total error. */
148 for(j = 0; j < (i>>1); j++) {
149 FLAC__double tmp = lpc[j];
150 lpc[j] += r * lpc[i-1-j];
151 lpc[i-1-j] += r * tmp;
154 lpc[j] += lpc[j] * r;
156 err *= (1.0 - r * r);
158 /* save this order */
159 for(j = 0; j <= i; j++)
160 lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
163 /* see SF bug #1601812 http://sourceforge.net/tracker/index.php?func=detail&aid=1601812&group_id=13478&atid=113478 */
171 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift)
175 FLAC__int32 qmax, qmin;
177 FLAC__ASSERT(precision > 0);
178 FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
180 /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
182 qmax = 1 << precision;
186 /* calc cmax = max( |lp_coeff[i]| ) */
188 for(i = 0; i < order; i++) {
189 const FLAC__double d = fabs(lp_coeff[i]);
195 /* => coefficients are all 0, which means our constant-detect didn't work */
199 const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
200 const int min_shiftlimit = -max_shiftlimit - 1;
203 (void)frexp(cmax, &log2cmax);
205 *shift = (int)precision - log2cmax - 1;
207 if(*shift > max_shiftlimit)
208 *shift = max_shiftlimit;
209 else if(*shift < min_shiftlimit)
214 FLAC__double error = 0.0;
216 for(i = 0; i < order; i++) {
217 error += lp_coeff[i] * (1 << *shift);
220 #ifdef FLAC__OVERFLOW_DETECT
221 if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
222 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]);
224 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]);
234 /* negative shift is very rare but due to design flaw, negative shift is
235 * a NOP in the decoder, so it must be handled specially by scaling down
239 const int nshift = -(*shift);
240 FLAC__double error = 0.0;
243 fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax);
245 for(i = 0; i < order; i++) {
246 error += lp_coeff[i] / (1 << nshift);
248 #ifdef FLAC__OVERFLOW_DETECT
249 if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
250 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]);
252 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]);
267 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[])
268 #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
273 const FLAC__int32 *history;
275 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
276 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
278 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
279 fprintf(stderr,"\n");
281 FLAC__ASSERT(order > 0);
283 for(i = 0; i < data_len; i++) {
287 for(j = 0; j < order; j++) {
288 sum += qlp_coeff[j] * (*(--history));
289 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
290 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%" PRId64 "\n",i,j,qlp_coeff[j],*history,sumo);
292 *(residual++) = *(data++) - (sum >> lp_quantization);
295 /* Here's a slower but clearer version:
296 for(i = 0; i < data_len; i++) {
298 for(j = 0; j < order; j++)
299 sum += qlp_coeff[j] * data[i-j-1];
300 residual[i] = data[i] - (sum >> lp_quantization);
304 #else /* fully unrolled version for normal use */
309 FLAC__ASSERT(order > 0);
310 FLAC__ASSERT(order <= 32);
313 * We do unique versions up to 12th order since that's the subset limit.
314 * Also they are roughly ordered to match frequency of occurrence to
315 * minimize branching.
321 for(i = 0; i < (int)data_len; i++) {
323 sum += qlp_coeff[11] * data[i-12];
324 sum += qlp_coeff[10] * data[i-11];
325 sum += qlp_coeff[9] * data[i-10];
326 sum += qlp_coeff[8] * data[i-9];
327 sum += qlp_coeff[7] * data[i-8];
328 sum += qlp_coeff[6] * data[i-7];
329 sum += qlp_coeff[5] * data[i-6];
330 sum += qlp_coeff[4] * data[i-5];
331 sum += qlp_coeff[3] * data[i-4];
332 sum += qlp_coeff[2] * data[i-3];
333 sum += qlp_coeff[1] * data[i-2];
334 sum += qlp_coeff[0] * data[i-1];
335 residual[i] = data[i] - (sum >> lp_quantization);
338 else { /* order == 11 */
339 for(i = 0; i < (int)data_len; i++) {
341 sum += qlp_coeff[10] * data[i-11];
342 sum += qlp_coeff[9] * data[i-10];
343 sum += qlp_coeff[8] * data[i-9];
344 sum += qlp_coeff[7] * data[i-8];
345 sum += qlp_coeff[6] * data[i-7];
346 sum += qlp_coeff[5] * data[i-6];
347 sum += qlp_coeff[4] * data[i-5];
348 sum += qlp_coeff[3] * data[i-4];
349 sum += qlp_coeff[2] * data[i-3];
350 sum += qlp_coeff[1] * data[i-2];
351 sum += qlp_coeff[0] * data[i-1];
352 residual[i] = data[i] - (sum >> lp_quantization);
358 for(i = 0; i < (int)data_len; i++) {
360 sum += qlp_coeff[9] * data[i-10];
361 sum += qlp_coeff[8] * data[i-9];
362 sum += qlp_coeff[7] * data[i-8];
363 sum += qlp_coeff[6] * data[i-7];
364 sum += qlp_coeff[5] * data[i-6];
365 sum += qlp_coeff[4] * data[i-5];
366 sum += qlp_coeff[3] * data[i-4];
367 sum += qlp_coeff[2] * data[i-3];
368 sum += qlp_coeff[1] * data[i-2];
369 sum += qlp_coeff[0] * data[i-1];
370 residual[i] = data[i] - (sum >> lp_quantization);
373 else { /* order == 9 */
374 for(i = 0; i < (int)data_len; i++) {
376 sum += qlp_coeff[8] * data[i-9];
377 sum += qlp_coeff[7] * data[i-8];
378 sum += qlp_coeff[6] * data[i-7];
379 sum += qlp_coeff[5] * data[i-6];
380 sum += qlp_coeff[4] * data[i-5];
381 sum += qlp_coeff[3] * data[i-4];
382 sum += qlp_coeff[2] * data[i-3];
383 sum += qlp_coeff[1] * data[i-2];
384 sum += qlp_coeff[0] * data[i-1];
385 residual[i] = data[i] - (sum >> lp_quantization);
393 for(i = 0; i < (int)data_len; i++) {
395 sum += qlp_coeff[7] * data[i-8];
396 sum += qlp_coeff[6] * data[i-7];
397 sum += qlp_coeff[5] * data[i-6];
398 sum += qlp_coeff[4] * data[i-5];
399 sum += qlp_coeff[3] * data[i-4];
400 sum += qlp_coeff[2] * data[i-3];
401 sum += qlp_coeff[1] * data[i-2];
402 sum += qlp_coeff[0] * data[i-1];
403 residual[i] = data[i] - (sum >> lp_quantization);
406 else { /* order == 7 */
407 for(i = 0; i < (int)data_len; i++) {
409 sum += qlp_coeff[6] * data[i-7];
410 sum += qlp_coeff[5] * data[i-6];
411 sum += qlp_coeff[4] * data[i-5];
412 sum += qlp_coeff[3] * data[i-4];
413 sum += qlp_coeff[2] * data[i-3];
414 sum += qlp_coeff[1] * data[i-2];
415 sum += qlp_coeff[0] * data[i-1];
416 residual[i] = data[i] - (sum >> lp_quantization);
422 for(i = 0; i < (int)data_len; i++) {
424 sum += qlp_coeff[5] * data[i-6];
425 sum += qlp_coeff[4] * data[i-5];
426 sum += qlp_coeff[3] * data[i-4];
427 sum += qlp_coeff[2] * data[i-3];
428 sum += qlp_coeff[1] * data[i-2];
429 sum += qlp_coeff[0] * data[i-1];
430 residual[i] = data[i] - (sum >> lp_quantization);
433 else { /* order == 5 */
434 for(i = 0; i < (int)data_len; i++) {
436 sum += qlp_coeff[4] * data[i-5];
437 sum += qlp_coeff[3] * data[i-4];
438 sum += qlp_coeff[2] * data[i-3];
439 sum += qlp_coeff[1] * data[i-2];
440 sum += qlp_coeff[0] * data[i-1];
441 residual[i] = data[i] - (sum >> lp_quantization);
449 for(i = 0; i < (int)data_len; i++) {
451 sum += qlp_coeff[3] * data[i-4];
452 sum += qlp_coeff[2] * data[i-3];
453 sum += qlp_coeff[1] * data[i-2];
454 sum += qlp_coeff[0] * data[i-1];
455 residual[i] = data[i] - (sum >> lp_quantization);
458 else { /* order == 3 */
459 for(i = 0; i < (int)data_len; i++) {
461 sum += qlp_coeff[2] * data[i-3];
462 sum += qlp_coeff[1] * data[i-2];
463 sum += qlp_coeff[0] * data[i-1];
464 residual[i] = data[i] - (sum >> lp_quantization);
470 for(i = 0; i < (int)data_len; i++) {
472 sum += qlp_coeff[1] * data[i-2];
473 sum += qlp_coeff[0] * data[i-1];
474 residual[i] = data[i] - (sum >> lp_quantization);
477 else { /* order == 1 */
478 for(i = 0; i < (int)data_len; i++)
479 residual[i] = data[i] - ((qlp_coeff[0] * data[i-1]) >> lp_quantization);
484 else { /* order > 12 */
485 for(i = 0; i < (int)data_len; i++) {
488 case 32: sum += qlp_coeff[31] * data[i-32];
489 case 31: sum += qlp_coeff[30] * data[i-31];
490 case 30: sum += qlp_coeff[29] * data[i-30];
491 case 29: sum += qlp_coeff[28] * data[i-29];
492 case 28: sum += qlp_coeff[27] * data[i-28];
493 case 27: sum += qlp_coeff[26] * data[i-27];
494 case 26: sum += qlp_coeff[25] * data[i-26];
495 case 25: sum += qlp_coeff[24] * data[i-25];
496 case 24: sum += qlp_coeff[23] * data[i-24];
497 case 23: sum += qlp_coeff[22] * data[i-23];
498 case 22: sum += qlp_coeff[21] * data[i-22];
499 case 21: sum += qlp_coeff[20] * data[i-21];
500 case 20: sum += qlp_coeff[19] * data[i-20];
501 case 19: sum += qlp_coeff[18] * data[i-19];
502 case 18: sum += qlp_coeff[17] * data[i-18];
503 case 17: sum += qlp_coeff[16] * data[i-17];
504 case 16: sum += qlp_coeff[15] * data[i-16];
505 case 15: sum += qlp_coeff[14] * data[i-15];
506 case 14: sum += qlp_coeff[13] * data[i-14];
507 case 13: sum += qlp_coeff[12] * data[i-13];
508 sum += qlp_coeff[11] * data[i-12];
509 sum += qlp_coeff[10] * data[i-11];
510 sum += qlp_coeff[ 9] * data[i-10];
511 sum += qlp_coeff[ 8] * data[i- 9];
512 sum += qlp_coeff[ 7] * data[i- 8];
513 sum += qlp_coeff[ 6] * data[i- 7];
514 sum += qlp_coeff[ 5] * data[i- 6];
515 sum += qlp_coeff[ 4] * data[i- 5];
516 sum += qlp_coeff[ 3] * data[i- 4];
517 sum += qlp_coeff[ 2] * data[i- 3];
518 sum += qlp_coeff[ 1] * data[i- 2];
519 sum += qlp_coeff[ 0] * data[i- 1];
521 residual[i] = data[i] - (sum >> lp_quantization);
527 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[])
528 #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
532 const FLAC__int32 *history;
534 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
535 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
537 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
538 fprintf(stderr,"\n");
540 FLAC__ASSERT(order > 0);
542 for(i = 0; i < data_len; i++) {
545 for(j = 0; j < order; j++)
546 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
547 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
548 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%" PRId64 "\n", i, (sum >> lp_quantization));
551 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
552 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%" PRId64 ", residual=%" PRId64 "\n", i, *data, (long long)(sum >> lp_quantization), ((FLAC__int64)(*data) - (sum >> lp_quantization)));
555 *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
558 #else /* fully unrolled version for normal use */
563 FLAC__ASSERT(order > 0);
564 FLAC__ASSERT(order <= 32);
567 * We do unique versions up to 12th order since that's the subset limit.
568 * Also they are roughly ordered to match frequency of occurrence to
569 * minimize branching.
575 for(i = 0; i < (int)data_len; i++) {
577 sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
578 sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
579 sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
580 sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
581 sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
582 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
583 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
584 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
585 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
586 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
587 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
588 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
589 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
592 else { /* order == 11 */
593 for(i = 0; i < (int)data_len; i++) {
595 sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
596 sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
597 sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
598 sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
599 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
600 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
601 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
602 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
603 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
604 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
605 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
606 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
612 for(i = 0; i < (int)data_len; i++) {
614 sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
615 sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
616 sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
617 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
618 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
619 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
620 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
621 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
622 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
623 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
624 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
627 else { /* order == 9 */
628 for(i = 0; i < (int)data_len; i++) {
630 sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
631 sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
632 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
633 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
634 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
635 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
636 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
637 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
638 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
639 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
647 for(i = 0; i < (int)data_len; i++) {
649 sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
650 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
651 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
652 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
653 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
654 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
655 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
656 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
657 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
660 else { /* order == 7 */
661 for(i = 0; i < (int)data_len; i++) {
663 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
664 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
665 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
666 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
667 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
668 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
669 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
670 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
676 for(i = 0; i < (int)data_len; i++) {
678 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
679 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
680 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
681 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
682 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
683 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
684 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
687 else { /* order == 5 */
688 for(i = 0; i < (int)data_len; i++) {
690 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
691 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
692 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
693 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
694 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
695 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
703 for(i = 0; i < (int)data_len; i++) {
705 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
706 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
707 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
708 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
709 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
712 else { /* order == 3 */
713 for(i = 0; i < (int)data_len; i++) {
715 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
716 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
717 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
718 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
724 for(i = 0; i < (int)data_len; i++) {
726 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
727 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
728 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
731 else { /* order == 1 */
732 for(i = 0; i < (int)data_len; i++)
733 residual[i] = data[i] - (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization);
738 else { /* order > 12 */
739 for(i = 0; i < (int)data_len; i++) {
742 case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32];
743 case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31];
744 case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30];
745 case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29];
746 case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28];
747 case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27];
748 case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26];
749 case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25];
750 case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24];
751 case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23];
752 case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22];
753 case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21];
754 case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20];
755 case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19];
756 case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18];
757 case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17];
758 case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16];
759 case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15];
760 case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14];
761 case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13];
762 sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
763 sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
764 sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10];
765 sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9];
766 sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8];
767 sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7];
768 sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6];
769 sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5];
770 sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4];
771 sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3];
772 sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2];
773 sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1];
775 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization);
781 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
783 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[])
784 #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
789 const FLAC__int32 *r = residual, *history;
791 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
792 fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
794 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
795 fprintf(stderr,"\n");
797 FLAC__ASSERT(order > 0);
799 for(i = 0; i < data_len; i++) {
803 for(j = 0; j < order; j++) {
804 sum += qlp_coeff[j] * (*(--history));
805 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
806 if(sumo > 2147483647ll || sumo < -2147483648ll)
807 fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%" PRId64 "\n",i,j,qlp_coeff[j],*history,sumo);
809 *(data++) = *(r++) + (sum >> lp_quantization);
812 /* Here's a slower but clearer version:
813 for(i = 0; i < data_len; i++) {
815 for(j = 0; j < order; j++)
816 sum += qlp_coeff[j] * data[i-j-1];
817 data[i] = residual[i] + (sum >> lp_quantization);
821 #else /* fully unrolled version for normal use */
826 FLAC__ASSERT(order > 0);
827 FLAC__ASSERT(order <= 32);
830 * We do unique versions up to 12th order since that's the subset limit.
831 * Also they are roughly ordered to match frequency of occurrence to
832 * minimize branching.
838 for(i = 0; i < (int)data_len; i++) {
840 sum += qlp_coeff[11] * data[i-12];
841 sum += qlp_coeff[10] * data[i-11];
842 sum += qlp_coeff[9] * data[i-10];
843 sum += qlp_coeff[8] * data[i-9];
844 sum += qlp_coeff[7] * data[i-8];
845 sum += qlp_coeff[6] * data[i-7];
846 sum += qlp_coeff[5] * data[i-6];
847 sum += qlp_coeff[4] * data[i-5];
848 sum += qlp_coeff[3] * data[i-4];
849 sum += qlp_coeff[2] * data[i-3];
850 sum += qlp_coeff[1] * data[i-2];
851 sum += qlp_coeff[0] * data[i-1];
852 data[i] = residual[i] + (sum >> lp_quantization);
855 else { /* order == 11 */
856 for(i = 0; i < (int)data_len; i++) {
858 sum += qlp_coeff[10] * data[i-11];
859 sum += qlp_coeff[9] * data[i-10];
860 sum += qlp_coeff[8] * data[i-9];
861 sum += qlp_coeff[7] * data[i-8];
862 sum += qlp_coeff[6] * data[i-7];
863 sum += qlp_coeff[5] * data[i-6];
864 sum += qlp_coeff[4] * data[i-5];
865 sum += qlp_coeff[3] * data[i-4];
866 sum += qlp_coeff[2] * data[i-3];
867 sum += qlp_coeff[1] * data[i-2];
868 sum += qlp_coeff[0] * data[i-1];
869 data[i] = residual[i] + (sum >> lp_quantization);
875 for(i = 0; i < (int)data_len; i++) {
877 sum += qlp_coeff[9] * data[i-10];
878 sum += qlp_coeff[8] * data[i-9];
879 sum += qlp_coeff[7] * data[i-8];
880 sum += qlp_coeff[6] * data[i-7];
881 sum += qlp_coeff[5] * data[i-6];
882 sum += qlp_coeff[4] * data[i-5];
883 sum += qlp_coeff[3] * data[i-4];
884 sum += qlp_coeff[2] * data[i-3];
885 sum += qlp_coeff[1] * data[i-2];
886 sum += qlp_coeff[0] * data[i-1];
887 data[i] = residual[i] + (sum >> lp_quantization);
890 else { /* order == 9 */
891 for(i = 0; i < (int)data_len; i++) {
893 sum += qlp_coeff[8] * data[i-9];
894 sum += qlp_coeff[7] * data[i-8];
895 sum += qlp_coeff[6] * data[i-7];
896 sum += qlp_coeff[5] * data[i-6];
897 sum += qlp_coeff[4] * data[i-5];
898 sum += qlp_coeff[3] * data[i-4];
899 sum += qlp_coeff[2] * data[i-3];
900 sum += qlp_coeff[1] * data[i-2];
901 sum += qlp_coeff[0] * data[i-1];
902 data[i] = residual[i] + (sum >> lp_quantization);
910 for(i = 0; i < (int)data_len; i++) {
912 sum += qlp_coeff[7] * data[i-8];
913 sum += qlp_coeff[6] * data[i-7];
914 sum += qlp_coeff[5] * data[i-6];
915 sum += qlp_coeff[4] * data[i-5];
916 sum += qlp_coeff[3] * data[i-4];
917 sum += qlp_coeff[2] * data[i-3];
918 sum += qlp_coeff[1] * data[i-2];
919 sum += qlp_coeff[0] * data[i-1];
920 data[i] = residual[i] + (sum >> lp_quantization);
923 else { /* order == 7 */
924 for(i = 0; i < (int)data_len; i++) {
926 sum += qlp_coeff[6] * data[i-7];
927 sum += qlp_coeff[5] * data[i-6];
928 sum += qlp_coeff[4] * data[i-5];
929 sum += qlp_coeff[3] * data[i-4];
930 sum += qlp_coeff[2] * data[i-3];
931 sum += qlp_coeff[1] * data[i-2];
932 sum += qlp_coeff[0] * data[i-1];
933 data[i] = residual[i] + (sum >> lp_quantization);
939 for(i = 0; i < (int)data_len; i++) {
941 sum += qlp_coeff[5] * data[i-6];
942 sum += qlp_coeff[4] * data[i-5];
943 sum += qlp_coeff[3] * data[i-4];
944 sum += qlp_coeff[2] * data[i-3];
945 sum += qlp_coeff[1] * data[i-2];
946 sum += qlp_coeff[0] * data[i-1];
947 data[i] = residual[i] + (sum >> lp_quantization);
950 else { /* order == 5 */
951 for(i = 0; i < (int)data_len; i++) {
953 sum += qlp_coeff[4] * data[i-5];
954 sum += qlp_coeff[3] * data[i-4];
955 sum += qlp_coeff[2] * data[i-3];
956 sum += qlp_coeff[1] * data[i-2];
957 sum += qlp_coeff[0] * data[i-1];
958 data[i] = residual[i] + (sum >> lp_quantization);
966 for(i = 0; i < (int)data_len; i++) {
968 sum += qlp_coeff[3] * data[i-4];
969 sum += qlp_coeff[2] * data[i-3];
970 sum += qlp_coeff[1] * data[i-2];
971 sum += qlp_coeff[0] * data[i-1];
972 data[i] = residual[i] + (sum >> lp_quantization);
975 else { /* order == 3 */
976 for(i = 0; i < (int)data_len; i++) {
978 sum += qlp_coeff[2] * data[i-3];
979 sum += qlp_coeff[1] * data[i-2];
980 sum += qlp_coeff[0] * data[i-1];
981 data[i] = residual[i] + (sum >> lp_quantization);
987 for(i = 0; i < (int)data_len; i++) {
989 sum += qlp_coeff[1] * data[i-2];
990 sum += qlp_coeff[0] * data[i-1];
991 data[i] = residual[i] + (sum >> lp_quantization);
994 else { /* order == 1 */
995 for(i = 0; i < (int)data_len; i++)
996 data[i] = residual[i] + ((qlp_coeff[0] * data[i-1]) >> lp_quantization);
1001 else { /* order > 12 */
1002 for(i = 0; i < (int)data_len; i++) {
1005 case 32: sum += qlp_coeff[31] * data[i-32];
1006 case 31: sum += qlp_coeff[30] * data[i-31];
1007 case 30: sum += qlp_coeff[29] * data[i-30];
1008 case 29: sum += qlp_coeff[28] * data[i-29];
1009 case 28: sum += qlp_coeff[27] * data[i-28];
1010 case 27: sum += qlp_coeff[26] * data[i-27];
1011 case 26: sum += qlp_coeff[25] * data[i-26];
1012 case 25: sum += qlp_coeff[24] * data[i-25];
1013 case 24: sum += qlp_coeff[23] * data[i-24];
1014 case 23: sum += qlp_coeff[22] * data[i-23];
1015 case 22: sum += qlp_coeff[21] * data[i-22];
1016 case 21: sum += qlp_coeff[20] * data[i-21];
1017 case 20: sum += qlp_coeff[19] * data[i-20];
1018 case 19: sum += qlp_coeff[18] * data[i-19];
1019 case 18: sum += qlp_coeff[17] * data[i-18];
1020 case 17: sum += qlp_coeff[16] * data[i-17];
1021 case 16: sum += qlp_coeff[15] * data[i-16];
1022 case 15: sum += qlp_coeff[14] * data[i-15];
1023 case 14: sum += qlp_coeff[13] * data[i-14];
1024 case 13: sum += qlp_coeff[12] * data[i-13];
1025 sum += qlp_coeff[11] * data[i-12];
1026 sum += qlp_coeff[10] * data[i-11];
1027 sum += qlp_coeff[ 9] * data[i-10];
1028 sum += qlp_coeff[ 8] * data[i- 9];
1029 sum += qlp_coeff[ 7] * data[i- 8];
1030 sum += qlp_coeff[ 6] * data[i- 7];
1031 sum += qlp_coeff[ 5] * data[i- 6];
1032 sum += qlp_coeff[ 4] * data[i- 5];
1033 sum += qlp_coeff[ 3] * data[i- 4];
1034 sum += qlp_coeff[ 2] * data[i- 3];
1035 sum += qlp_coeff[ 1] * data[i- 2];
1036 sum += qlp_coeff[ 0] * data[i- 1];
1038 data[i] = residual[i] + (sum >> lp_quantization);
1044 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[])
1045 #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
1049 const FLAC__int32 *r = residual, *history;
1051 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
1052 fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
1053 for(i=0;i<order;i++)
1054 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
1055 fprintf(stderr,"\n");
1057 FLAC__ASSERT(order > 0);
1059 for(i = 0; i < data_len; i++) {
1062 for(j = 0; j < order; j++)
1063 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
1064 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
1065 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%" PRId64 "\n", i, (sum >> lp_quantization));
1068 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) {
1069 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%" PRId64 ", data=%" PRId64 "\n", i, *r, (sum >> lp_quantization), ((FLAC__int64)(*r) + (sum >> lp_quantization)));
1072 *(data++) = *(r++) + (FLAC__int32)(sum >> lp_quantization);
1075 #else /* fully unrolled version for normal use */
1080 FLAC__ASSERT(order > 0);
1081 FLAC__ASSERT(order <= 32);
1084 * We do unique versions up to 12th order since that's the subset limit.
1085 * Also they are roughly ordered to match frequency of occurrence to
1086 * minimize branching.
1092 for(i = 0; i < (int)data_len; i++) {
1094 sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
1095 sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
1096 sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
1097 sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
1098 sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
1099 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1100 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1101 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1102 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1103 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1104 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1105 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1106 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1109 else { /* order == 11 */
1110 for(i = 0; i < (int)data_len; i++) {
1112 sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
1113 sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
1114 sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
1115 sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
1116 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1117 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1118 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1119 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1120 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1121 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1122 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1123 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1129 for(i = 0; i < (int)data_len; i++) {
1131 sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
1132 sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
1133 sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
1134 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1135 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1136 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1137 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1138 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1139 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1140 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1141 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1144 else { /* order == 9 */
1145 for(i = 0; i < (int)data_len; i++) {
1147 sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
1148 sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
1149 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1150 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1151 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1152 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1153 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1154 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1155 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1156 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1161 else if(order > 4) {
1164 for(i = 0; i < (int)data_len; i++) {
1166 sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
1167 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1168 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1169 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1170 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1171 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1172 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1173 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1174 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1177 else { /* order == 7 */
1178 for(i = 0; i < (int)data_len; i++) {
1180 sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1181 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1182 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1183 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1184 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1185 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1186 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1187 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1193 for(i = 0; i < (int)data_len; i++) {
1195 sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1196 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1197 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1198 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1199 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1200 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1201 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1204 else { /* order == 5 */
1205 for(i = 0; i < (int)data_len; i++) {
1207 sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1208 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1209 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1210 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1211 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1212 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1220 for(i = 0; i < (int)data_len; i++) {
1222 sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1223 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1224 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1225 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1226 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1229 else { /* order == 3 */
1230 for(i = 0; i < (int)data_len; i++) {
1232 sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1233 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1234 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1235 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1241 for(i = 0; i < (int)data_len; i++) {
1243 sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1244 sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1245 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1248 else { /* order == 1 */
1249 for(i = 0; i < (int)data_len; i++)
1250 data[i] = residual[i] + (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization);
1255 else { /* order > 12 */
1256 for(i = 0; i < (int)data_len; i++) {
1259 case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32];
1260 case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31];
1261 case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30];
1262 case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29];
1263 case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28];
1264 case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27];
1265 case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26];
1266 case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25];
1267 case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24];
1268 case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23];
1269 case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22];
1270 case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21];
1271 case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20];
1272 case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19];
1273 case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18];
1274 case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17];
1275 case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16];
1276 case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15];
1277 case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14];
1278 case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13];
1279 sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
1280 sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
1281 sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10];
1282 sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9];
1283 sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8];
1284 sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7];
1285 sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6];
1286 sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5];
1287 sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4];
1288 sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3];
1289 sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2];
1290 sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1];
1292 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization);
1298 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1300 FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples)
1302 FLAC__double error_scale;
1304 FLAC__ASSERT(total_samples > 0);
1306 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
1308 return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
1311 FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale)
1313 if(lpc_error > 0.0) {
1314 FLAC__double bps = (FLAC__double)0.5 * log(error_scale * lpc_error) / M_LN2;
1320 else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate floating-point resolution */
1328 unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order)
1330 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 */
1331 FLAC__double bits, best_bits, error_scale;
1333 FLAC__ASSERT(max_order > 0);
1334 FLAC__ASSERT(total_samples > 0);
1336 error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
1339 best_bits = (unsigned)(-1);
1341 for(index = 0, order = 1; index < max_order; index++, order++) {
1342 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);
1343 if(bits < best_bits) {
1349 return best_index+1; /* +1 since index of lpc_error[] is order-1 */
1352 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */