• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000-2009  Josh Coalson
3  * Copyright (C) 2011-2022  Xiph.Org Foundation
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Xiph.org Foundation nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36 
37 #include <math.h>
38 #include <stdlib.h>
39 
40 #include "FLAC/assert.h"
41 #include "FLAC/format.h"
42 #include "share/compat.h"
43 #include "private/bitmath.h"
44 #include "private/lpc.h"
45 #include "private/macros.h"
46 
47 #if !defined(NDEBUG) || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
48 #include <stdio.h>
49 #endif
50 
51 /* OPT: #undef'ing this may improve the speed on some architectures */
52 #define FLAC__LPC_UNROLLED_FILTER_LOOPS
53 
54 #ifndef FLAC__INTEGER_ONLY_LIBRARY
55 
56 #if defined(_MSC_VER) && (_MSC_VER < 1800)
57 #include <float.h>
lround(double x)58 static inline long int lround(double x) {
59 	return (long)(x + _copysign(0.5, x));
60 }
61 #elif !defined(HAVE_LROUND) && defined(__GNUC__)
lround(double x)62 static inline long int lround(double x) {
63 	return (long)(x + __builtin_copysign(0.5, x));
64 }
65 /* If this fails, we are in the presence of a mid 90's compiler, move along... */
66 #endif
67 
FLAC__lpc_window_data(const FLAC__int32 in[],const FLAC__real window[],FLAC__real out[],uint32_t data_len)68 void FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], uint32_t data_len)
69 {
70 	uint32_t i;
71 	for(i = 0; i < data_len; i++)
72 		out[i] = in[i] * window[i];
73 }
74 
FLAC__lpc_window_data_wide(const FLAC__int64 in[],const FLAC__real window[],FLAC__real out[],uint32_t data_len)75 void FLAC__lpc_window_data_wide(const FLAC__int64 in[], const FLAC__real window[], FLAC__real out[], uint32_t data_len)
76 {
77 	uint32_t i;
78 	for(i = 0; i < data_len; i++)
79 		out[i] = in[i] * window[i];
80 }
81 
FLAC__lpc_window_data_partial(const FLAC__int32 in[],const FLAC__real window[],FLAC__real out[],uint32_t data_len,uint32_t part_size,uint32_t data_shift)82 void FLAC__lpc_window_data_partial(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], uint32_t data_len, uint32_t part_size, uint32_t data_shift)
83 {
84 	uint32_t i, j;
85 	if((part_size + data_shift) < data_len){
86 		for(i = 0; i < part_size; i++)
87 			out[i] = in[data_shift+i] * window[i];
88 		i = flac_min(i,data_len - part_size - data_shift);
89 		for(j = data_len - part_size; j < data_len; i++, j++)
90 			out[i] = in[data_shift+i] * window[j];
91 		if(i < data_len)
92 			out[i] = 0.0f;
93 	}
94 }
95 
FLAC__lpc_window_data_partial_wide(const FLAC__int64 in[],const FLAC__real window[],FLAC__real out[],uint32_t data_len,uint32_t part_size,uint32_t data_shift)96 void FLAC__lpc_window_data_partial_wide(const FLAC__int64 in[], const FLAC__real window[], FLAC__real out[], uint32_t data_len, uint32_t part_size, uint32_t data_shift)
97 {
98 	uint32_t i, j;
99 	if((part_size + data_shift) < data_len){
100 		for(i = 0; i < part_size; i++)
101 			out[i] = in[data_shift+i] * window[i];
102 		i = flac_min(i,data_len - part_size - data_shift);
103 		for(j = data_len - part_size; j < data_len; i++, j++)
104 			out[i] = in[data_shift+i] * window[j];
105 		if(i < data_len)
106 			out[i] = 0.0f;
107 	}
108 }
109 
FLAC__lpc_compute_autocorrelation(const FLAC__real data[],uint32_t data_len,uint32_t lag,double autoc[])110 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[])
111 {
112 	/* a readable, but slower, version */
113 #if 0
114 	double d;
115 	uint32_t i;
116 
117 	FLAC__ASSERT(lag > 0);
118 	FLAC__ASSERT(lag <= data_len);
119 
120 	/*
121 	 * Technically we should subtract the mean first like so:
122 	 *   for(i = 0; i < data_len; i++)
123 	 *     data[i] -= mean;
124 	 * but it appears not to make enough of a difference to matter, and
125 	 * most signals are already closely centered around zero
126 	 */
127 	while(lag--) {
128 		for(i = lag, d = 0.0; i < data_len; i++)
129 			d += data[i] * (double)data[i - lag];
130 		autoc[lag] = d;
131 	}
132 #endif
133 	if (data_len < FLAC__MAX_LPC_ORDER || lag > 16) {
134 		/*
135 		 * this version tends to run faster because of better data locality
136 		 * ('data_len' is usually much larger than 'lag')
137 		 */
138 		double d;
139 		uint32_t sample, coeff;
140 		const uint32_t limit = data_len - lag;
141 
142 		FLAC__ASSERT(lag > 0);
143 		FLAC__ASSERT(lag <= data_len);
144 
145 		for(coeff = 0; coeff < lag; coeff++)
146 			autoc[coeff] = 0.0;
147 		for(sample = 0; sample <= limit; sample++) {
148 			d = data[sample];
149 			for(coeff = 0; coeff < lag; coeff++)
150 				autoc[coeff] += d * data[sample+coeff];
151 		}
152 		for(; sample < data_len; sample++) {
153 			d = data[sample];
154 			for(coeff = 0; coeff < data_len - sample; coeff++)
155 				autoc[coeff] += d * data[sample+coeff];
156 		}
157 	}
158 	else if(lag <= 8) {
159 		#undef MAX_LAG
160 		#define MAX_LAG 8
161 		#include "deduplication/lpc_compute_autocorrelation_intrin.c"
162 	}
163 	else if(lag <= 12) {
164 		#undef MAX_LAG
165 		#define MAX_LAG 12
166 		#include "deduplication/lpc_compute_autocorrelation_intrin.c"
167 	}
168 	else if(lag <= 16) {
169 		#undef MAX_LAG
170 		#define MAX_LAG 16
171 		#include "deduplication/lpc_compute_autocorrelation_intrin.c"
172 	}
173 
174 }
175 
FLAC__lpc_compute_lp_coefficients(const double autoc[],uint32_t * max_order,FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER],double error[])176 void FLAC__lpc_compute_lp_coefficients(const double autoc[], uint32_t *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], double error[])
177 {
178 	uint32_t i, j;
179 	double r, err, lpc[FLAC__MAX_LPC_ORDER];
180 
181 	FLAC__ASSERT(0 != max_order);
182 	FLAC__ASSERT(0 < *max_order);
183 	FLAC__ASSERT(*max_order <= FLAC__MAX_LPC_ORDER);
184 	FLAC__ASSERT(autoc[0] != 0.0);
185 
186 	err = autoc[0];
187 
188 	for(i = 0; i < *max_order; i++) {
189 		/* Sum up this iteration's reflection coefficient. */
190 		r = -autoc[i+1];
191 		for(j = 0; j < i; j++)
192 			r -= lpc[j] * autoc[i-j];
193 		r /= err;
194 
195 		/* Update LPC coefficients and total error. */
196 		lpc[i]=r;
197 		for(j = 0; j < (i>>1); j++) {
198 			double tmp = lpc[j];
199 			lpc[j] += r * lpc[i-1-j];
200 			lpc[i-1-j] += r * tmp;
201 		}
202 		if(i & 1)
203 			lpc[j] += lpc[j] * r;
204 
205 		err *= (1.0 - r * r);
206 
207 		/* save this order */
208 		for(j = 0; j <= i; j++)
209 			lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
210 		error[i] = err;
211 
212 		/* see SF bug https://sourceforge.net/p/flac/bugs/234/ */
213 		if(err == 0.0) {
214 			*max_order = i+1;
215 			return;
216 		}
217 	}
218 }
219 
FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[],uint32_t order,uint32_t precision,FLAC__int32 qlp_coeff[],int * shift)220 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], uint32_t order, uint32_t precision, FLAC__int32 qlp_coeff[], int *shift)
221 {
222 	uint32_t i;
223 	double cmax;
224 	FLAC__int32 qmax, qmin;
225 
226 	FLAC__ASSERT(precision > 0);
227 	FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
228 
229 	/* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
230 	precision--;
231 	qmax = 1 << precision;
232 	qmin = -qmax;
233 	qmax--;
234 
235 	/* calc cmax = max( |lp_coeff[i]| ) */
236 	cmax = 0.0;
237 	for(i = 0; i < order; i++) {
238 		const double d = fabs(lp_coeff[i]);
239 		if(d > cmax)
240 			cmax = d;
241 	}
242 
243 	if(cmax <= 0.0) {
244 		/* => coefficients are all 0, which means our constant-detect didn't work */
245 		return 2;
246 	}
247 	else {
248 		const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
249 		const int min_shiftlimit = -max_shiftlimit - 1;
250 		int log2cmax;
251 
252 		(void)frexp(cmax, &log2cmax);
253 		log2cmax--;
254 		*shift = (int)precision - log2cmax - 1;
255 
256 		if(*shift > max_shiftlimit)
257 			*shift = max_shiftlimit;
258 		else if(*shift < min_shiftlimit)
259 			return 1;
260 	}
261 
262 	if(*shift >= 0) {
263 		double error = 0.0;
264 		FLAC__int32 q;
265 		for(i = 0; i < order; i++) {
266 			error += lp_coeff[i] * (1 << *shift);
267 			q = lround(error);
268 
269 #ifdef FLAC__OVERFLOW_DETECT
270 			if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
271 				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]);
272 			else if(q < qmin)
273 				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]);
274 #endif
275 			if(q > qmax)
276 				q = qmax;
277 			else if(q < qmin)
278 				q = qmin;
279 			error -= q;
280 			qlp_coeff[i] = q;
281 		}
282 	}
283 	/* negative shift is very rare but due to design flaw, negative shift is
284 	 * not allowed in the decoder, so it must be handled specially by scaling
285 	 * down coeffs
286 	 */
287 	else {
288 		const int nshift = -(*shift);
289 		double error = 0.0;
290 		FLAC__int32 q;
291 #ifndef NDEBUG
292 		fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax);
293 #endif
294 		for(i = 0; i < order; i++) {
295 			error += lp_coeff[i] / (1 << nshift);
296 			q = lround(error);
297 #ifdef FLAC__OVERFLOW_DETECT
298 			if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */
299 				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]);
300 			else if(q < qmin)
301 				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]);
302 #endif
303 			if(q > qmax)
304 				q = qmax;
305 			else if(q < qmin)
306 				q = qmin;
307 			error -= q;
308 			qlp_coeff[i] = q;
309 		}
310 		*shift = 0;
311 	}
312 
313 	return 0;
314 }
315 
316 #if defined(_MSC_VER)
317 // silence MSVC warnings about __restrict modifier
318 #pragma warning ( disable : 4028 )
319 #endif
320 
FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 * flac_restrict data,uint32_t data_len,const FLAC__int32 * flac_restrict qlp_coeff,uint32_t order,int lp_quantization,FLAC__int32 * flac_restrict residual)321 void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 * flac_restrict data, uint32_t data_len, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization, FLAC__int32 * flac_restrict residual)
322 #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
323 {
324 	FLAC__int64 sumo;
325 	uint32_t i, j;
326 	FLAC__int32 sum;
327 	const FLAC__int32 *history;
328 
329 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
330 	fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
331 	for(i=0;i<order;i++)
332 		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
333 	fprintf(stderr,"\n");
334 #endif
335 	FLAC__ASSERT(order > 0);
336 
337 	for(i = 0; i < data_len; i++) {
338 		sumo = 0;
339 		sum = 0;
340 		history = data;
341 		for(j = 0; j < order; j++) {
342 			sum += qlp_coeff[j] * (*(--history));
343 			sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
344 			if(sumo > 2147483647ll || sumo < -2147483648ll)
345 				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);
346 		}
347 		*(residual++) = *(data++) - (sum >> lp_quantization);
348 	}
349 
350 	/* Here's a slower but clearer version:
351 	for(i = 0; i < data_len; i++) {
352 		sum = 0;
353 		for(j = 0; j < order; j++)
354 			sum += qlp_coeff[j] * data[i-j-1];
355 		residual[i] = data[i] - (sum >> lp_quantization);
356 	}
357 	*/
358 }
359 #else /* fully unrolled version for normal use */
360 {
361 	int i;
362 	FLAC__int32 sum;
363 
364 	FLAC__ASSERT(order > 0);
365 	FLAC__ASSERT(order <= 32);
366 
367 	/*
368 	 * We do unique versions up to 12th order since that's the subset limit.
369 	 * Also they are roughly ordered to match frequency of occurrence to
370 	 * minimize branching.
371 	 */
372 	if(order <= 12) {
373 		if(order > 8) {
374 			if(order > 10) {
375 				if(order == 12) {
376 					for(i = 0; i < (int)data_len; i++) {
377 						sum = 0;
378 						sum += qlp_coeff[11] * data[i-12];
379 						sum += qlp_coeff[10] * data[i-11];
380 						sum += qlp_coeff[9] * data[i-10];
381 						sum += qlp_coeff[8] * data[i-9];
382 						sum += qlp_coeff[7] * data[i-8];
383 						sum += qlp_coeff[6] * data[i-7];
384 						sum += qlp_coeff[5] * data[i-6];
385 						sum += qlp_coeff[4] * data[i-5];
386 						sum += qlp_coeff[3] * data[i-4];
387 						sum += qlp_coeff[2] * data[i-3];
388 						sum += qlp_coeff[1] * data[i-2];
389 						sum += qlp_coeff[0] * data[i-1];
390 						residual[i] = data[i] - (sum >> lp_quantization);
391 					}
392 				}
393 				else { /* order == 11 */
394 					for(i = 0; i < (int)data_len; i++) {
395 						sum = 0;
396 						sum += qlp_coeff[10] * data[i-11];
397 						sum += qlp_coeff[9] * data[i-10];
398 						sum += qlp_coeff[8] * data[i-9];
399 						sum += qlp_coeff[7] * data[i-8];
400 						sum += qlp_coeff[6] * data[i-7];
401 						sum += qlp_coeff[5] * data[i-6];
402 						sum += qlp_coeff[4] * data[i-5];
403 						sum += qlp_coeff[3] * data[i-4];
404 						sum += qlp_coeff[2] * data[i-3];
405 						sum += qlp_coeff[1] * data[i-2];
406 						sum += qlp_coeff[0] * data[i-1];
407 						residual[i] = data[i] - (sum >> lp_quantization);
408 					}
409 				}
410 			}
411 			else {
412 				if(order == 10) {
413 					for(i = 0; i < (int)data_len; i++) {
414 						sum = 0;
415 						sum += qlp_coeff[9] * data[i-10];
416 						sum += qlp_coeff[8] * data[i-9];
417 						sum += qlp_coeff[7] * data[i-8];
418 						sum += qlp_coeff[6] * data[i-7];
419 						sum += qlp_coeff[5] * data[i-6];
420 						sum += qlp_coeff[4] * data[i-5];
421 						sum += qlp_coeff[3] * data[i-4];
422 						sum += qlp_coeff[2] * data[i-3];
423 						sum += qlp_coeff[1] * data[i-2];
424 						sum += qlp_coeff[0] * data[i-1];
425 						residual[i] = data[i] - (sum >> lp_quantization);
426 					}
427 				}
428 				else { /* order == 9 */
429 					for(i = 0; i < (int)data_len; i++) {
430 						sum = 0;
431 						sum += qlp_coeff[8] * data[i-9];
432 						sum += qlp_coeff[7] * data[i-8];
433 						sum += qlp_coeff[6] * data[i-7];
434 						sum += qlp_coeff[5] * data[i-6];
435 						sum += qlp_coeff[4] * data[i-5];
436 						sum += qlp_coeff[3] * data[i-4];
437 						sum += qlp_coeff[2] * data[i-3];
438 						sum += qlp_coeff[1] * data[i-2];
439 						sum += qlp_coeff[0] * data[i-1];
440 						residual[i] = data[i] - (sum >> lp_quantization);
441 					}
442 				}
443 			}
444 		}
445 		else if(order > 4) {
446 			if(order > 6) {
447 				if(order == 8) {
448 					for(i = 0; i < (int)data_len; i++) {
449 						sum = 0;
450 						sum += qlp_coeff[7] * data[i-8];
451 						sum += qlp_coeff[6] * data[i-7];
452 						sum += qlp_coeff[5] * data[i-6];
453 						sum += qlp_coeff[4] * data[i-5];
454 						sum += qlp_coeff[3] * data[i-4];
455 						sum += qlp_coeff[2] * data[i-3];
456 						sum += qlp_coeff[1] * data[i-2];
457 						sum += qlp_coeff[0] * data[i-1];
458 						residual[i] = data[i] - (sum >> lp_quantization);
459 					}
460 				}
461 				else { /* order == 7 */
462 					for(i = 0; i < (int)data_len; i++) {
463 						sum = 0;
464 						sum += qlp_coeff[6] * data[i-7];
465 						sum += qlp_coeff[5] * data[i-6];
466 						sum += qlp_coeff[4] * data[i-5];
467 						sum += qlp_coeff[3] * data[i-4];
468 						sum += qlp_coeff[2] * data[i-3];
469 						sum += qlp_coeff[1] * data[i-2];
470 						sum += qlp_coeff[0] * data[i-1];
471 						residual[i] = data[i] - (sum >> lp_quantization);
472 					}
473 				}
474 			}
475 			else {
476 				if(order == 6) {
477 					for(i = 0; i < (int)data_len; i++) {
478 						sum = 0;
479 						sum += qlp_coeff[5] * data[i-6];
480 						sum += qlp_coeff[4] * data[i-5];
481 						sum += qlp_coeff[3] * data[i-4];
482 						sum += qlp_coeff[2] * data[i-3];
483 						sum += qlp_coeff[1] * data[i-2];
484 						sum += qlp_coeff[0] * data[i-1];
485 						residual[i] = data[i] - (sum >> lp_quantization);
486 					}
487 				}
488 				else { /* order == 5 */
489 					for(i = 0; i < (int)data_len; i++) {
490 						sum = 0;
491 						sum += qlp_coeff[4] * data[i-5];
492 						sum += qlp_coeff[3] * data[i-4];
493 						sum += qlp_coeff[2] * data[i-3];
494 						sum += qlp_coeff[1] * data[i-2];
495 						sum += qlp_coeff[0] * data[i-1];
496 						residual[i] = data[i] - (sum >> lp_quantization);
497 					}
498 				}
499 			}
500 		}
501 		else {
502 			if(order > 2) {
503 				if(order == 4) {
504 					for(i = 0; i < (int)data_len; i++) {
505 						sum = 0;
506 						sum += qlp_coeff[3] * data[i-4];
507 						sum += qlp_coeff[2] * data[i-3];
508 						sum += qlp_coeff[1] * data[i-2];
509 						sum += qlp_coeff[0] * data[i-1];
510 						residual[i] = data[i] - (sum >> lp_quantization);
511 					}
512 				}
513 				else { /* order == 3 */
514 					for(i = 0; i < (int)data_len; i++) {
515 						sum = 0;
516 						sum += qlp_coeff[2] * data[i-3];
517 						sum += qlp_coeff[1] * data[i-2];
518 						sum += qlp_coeff[0] * data[i-1];
519 						residual[i] = data[i] - (sum >> lp_quantization);
520 					}
521 				}
522 			}
523 			else {
524 				if(order == 2) {
525 					for(i = 0; i < (int)data_len; i++) {
526 						sum = 0;
527 						sum += qlp_coeff[1] * data[i-2];
528 						sum += qlp_coeff[0] * data[i-1];
529 						residual[i] = data[i] - (sum >> lp_quantization);
530 					}
531 				}
532 				else { /* order == 1 */
533 					for(i = 0; i < (int)data_len; i++)
534 						residual[i] = data[i] - ((qlp_coeff[0] * data[i-1]) >> lp_quantization);
535 				}
536 			}
537 		}
538 	}
539 	else { /* order > 12 */
540 		for(i = 0; i < (int)data_len; i++) {
541 			sum = 0;
542 			switch(order) {
543 				case 32: sum += qlp_coeff[31] * data[i-32]; /* Falls through. */
544 				case 31: sum += qlp_coeff[30] * data[i-31]; /* Falls through. */
545 				case 30: sum += qlp_coeff[29] * data[i-30]; /* Falls through. */
546 				case 29: sum += qlp_coeff[28] * data[i-29]; /* Falls through. */
547 				case 28: sum += qlp_coeff[27] * data[i-28]; /* Falls through. */
548 				case 27: sum += qlp_coeff[26] * data[i-27]; /* Falls through. */
549 				case 26: sum += qlp_coeff[25] * data[i-26]; /* Falls through. */
550 				case 25: sum += qlp_coeff[24] * data[i-25]; /* Falls through. */
551 				case 24: sum += qlp_coeff[23] * data[i-24]; /* Falls through. */
552 				case 23: sum += qlp_coeff[22] * data[i-23]; /* Falls through. */
553 				case 22: sum += qlp_coeff[21] * data[i-22]; /* Falls through. */
554 				case 21: sum += qlp_coeff[20] * data[i-21]; /* Falls through. */
555 				case 20: sum += qlp_coeff[19] * data[i-20]; /* Falls through. */
556 				case 19: sum += qlp_coeff[18] * data[i-19]; /* Falls through. */
557 				case 18: sum += qlp_coeff[17] * data[i-18]; /* Falls through. */
558 				case 17: sum += qlp_coeff[16] * data[i-17]; /* Falls through. */
559 				case 16: sum += qlp_coeff[15] * data[i-16]; /* Falls through. */
560 				case 15: sum += qlp_coeff[14] * data[i-15]; /* Falls through. */
561 				case 14: sum += qlp_coeff[13] * data[i-14]; /* Falls through. */
562 				case 13: sum += qlp_coeff[12] * data[i-13];
563 				         sum += qlp_coeff[11] * data[i-12];
564 				         sum += qlp_coeff[10] * data[i-11];
565 				         sum += qlp_coeff[ 9] * data[i-10];
566 				         sum += qlp_coeff[ 8] * data[i- 9];
567 				         sum += qlp_coeff[ 7] * data[i- 8];
568 				         sum += qlp_coeff[ 6] * data[i- 7];
569 				         sum += qlp_coeff[ 5] * data[i- 6];
570 				         sum += qlp_coeff[ 4] * data[i- 5];
571 				         sum += qlp_coeff[ 3] * data[i- 4];
572 				         sum += qlp_coeff[ 2] * data[i- 3];
573 				         sum += qlp_coeff[ 1] * data[i- 2];
574 				         sum += qlp_coeff[ 0] * data[i- 1];
575 			}
576 			residual[i] = data[i] - (sum >> lp_quantization);
577 		}
578 	}
579 }
580 #endif
581 
FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 * flac_restrict data,uint32_t data_len,const FLAC__int32 * flac_restrict qlp_coeff,uint32_t order,int lp_quantization,FLAC__int32 * flac_restrict residual)582 void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 * flac_restrict data, uint32_t data_len, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization, FLAC__int32 * flac_restrict residual)
583 #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
584 {
585 	uint32_t i, j;
586 	FLAC__int64 sum;
587 	const FLAC__int32 *history;
588 
589 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
590 	fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
591 	for(i=0;i<order;i++)
592 		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
593 	fprintf(stderr,"\n");
594 #endif
595 	FLAC__ASSERT(order > 0);
596 
597 	for(i = 0; i < data_len; i++) {
598 		sum = 0;
599 		history = data;
600 		for(j = 0; j < order; j++)
601 			sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
602 		if(FLAC__bitmath_silog2((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
603 			fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%" PRId64 ", residual=%" PRId64 "\n", i, *data, (int64_t)(sum >> lp_quantization), ((FLAC__int64)(*data) - (sum >> lp_quantization)));
604 			break;
605 		}
606 		*(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
607 	}
608 }
609 #else /* fully unrolled version for normal use */
610 {
611 	int i;
612 	FLAC__int64 sum;
613 
614 	FLAC__ASSERT(order > 0);
615 	FLAC__ASSERT(order <= 32);
616 
617 	/*
618 	 * We do unique versions up to 12th order since that's the subset limit.
619 	 * Also they are roughly ordered to match frequency of occurrence to
620 	 * minimize branching.
621 	 */
622 	if(order <= 12) {
623 		if(order > 8) {
624 			if(order > 10) {
625 				if(order == 12) {
626 					for(i = 0; i < (int)data_len; i++) {
627 						sum = 0;
628 						sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
629 						sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
630 						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
631 						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
632 						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
633 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
634 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
635 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
636 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
637 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
638 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
639 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
640 						residual[i] = data[i] - (sum >> lp_quantization);
641 					}
642 				}
643 				else { /* order == 11 */
644 					for(i = 0; i < (int)data_len; i++) {
645 						sum = 0;
646 						sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
647 						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
648 						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
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] - (sum >> lp_quantization);
658 					}
659 				}
660 			}
661 			else {
662 				if(order == 10) {
663 					for(i = 0; i < (int)data_len; i++) {
664 						sum = 0;
665 						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
666 						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
667 						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
668 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
669 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
670 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
671 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
672 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
673 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
674 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
675 						residual[i] = data[i] - (sum >> lp_quantization);
676 					}
677 				}
678 				else { /* order == 9 */
679 					for(i = 0; i < (int)data_len; i++) {
680 						sum = 0;
681 						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
682 						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
683 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
684 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
685 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
686 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
687 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
688 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
689 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
690 						residual[i] = data[i] - (sum >> lp_quantization);
691 					}
692 				}
693 			}
694 		}
695 		else if(order > 4) {
696 			if(order > 6) {
697 				if(order == 8) {
698 					for(i = 0; i < (int)data_len; i++) {
699 						sum = 0;
700 						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
701 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
702 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
703 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
704 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
705 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
706 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
707 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
708 						residual[i] = data[i] - (sum >> lp_quantization);
709 					}
710 				}
711 				else { /* order == 7 */
712 					for(i = 0; i < (int)data_len; i++) {
713 						sum = 0;
714 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
715 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
716 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
717 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
718 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
719 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
720 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
721 						residual[i] = data[i] - (sum >> lp_quantization);
722 					}
723 				}
724 			}
725 			else {
726 				if(order == 6) {
727 					for(i = 0; i < (int)data_len; i++) {
728 						sum = 0;
729 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
730 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
731 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
732 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
733 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
734 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
735 						residual[i] = data[i] - (sum >> lp_quantization);
736 					}
737 				}
738 				else { /* order == 5 */
739 					for(i = 0; i < (int)data_len; i++) {
740 						sum = 0;
741 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
742 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
743 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
744 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
745 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
746 						residual[i] = data[i] - (sum >> lp_quantization);
747 					}
748 				}
749 			}
750 		}
751 		else {
752 			if(order > 2) {
753 				if(order == 4) {
754 					for(i = 0; i < (int)data_len; i++) {
755 						sum = 0;
756 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
757 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
758 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
759 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
760 						residual[i] = data[i] - (sum >> lp_quantization);
761 					}
762 				}
763 				else { /* order == 3 */
764 					for(i = 0; i < (int)data_len; i++) {
765 						sum = 0;
766 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
767 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
768 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
769 						residual[i] = data[i] - (sum >> lp_quantization);
770 					}
771 				}
772 			}
773 			else {
774 				if(order == 2) {
775 					for(i = 0; i < (int)data_len; i++) {
776 						sum = 0;
777 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
778 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
779 						residual[i] = data[i] - (sum >> lp_quantization);
780 					}
781 				}
782 				else { /* order == 1 */
783 					for(i = 0; i < (int)data_len; i++)
784 						residual[i] = data[i] - ((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization);
785 				}
786 			}
787 		}
788 	}
789 	else { /* order > 12 */
790 		for(i = 0; i < (int)data_len; i++) {
791 			sum = 0;
792 			switch(order) {
793 				case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32]; /* Falls through. */
794 				case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31]; /* Falls through. */
795 				case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30]; /* Falls through. */
796 				case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29]; /* Falls through. */
797 				case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28]; /* Falls through. */
798 				case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27]; /* Falls through. */
799 				case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26]; /* Falls through. */
800 				case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25]; /* Falls through. */
801 				case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24]; /* Falls through. */
802 				case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23]; /* Falls through. */
803 				case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22]; /* Falls through. */
804 				case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21]; /* Falls through. */
805 				case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20]; /* Falls through. */
806 				case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19]; /* Falls through. */
807 				case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18]; /* Falls through. */
808 				case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17]; /* Falls through. */
809 				case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16]; /* Falls through. */
810 				case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15]; /* Falls through. */
811 				case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14]; /* Falls through. */
812 				case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13];
813 				         sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
814 				         sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
815 				         sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10];
816 				         sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9];
817 				         sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8];
818 				         sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7];
819 				         sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6];
820 				         sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5];
821 				         sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4];
822 				         sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3];
823 				         sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2];
824 				         sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1];
825 			}
826 			residual[i] = data[i] - (sum >> lp_quantization);
827 		}
828 	}
829 }
830 #endif
831 
FLAC__lpc_compute_residual_from_qlp_coefficients_limit_residual(const FLAC__int32 * flac_restrict data,uint32_t data_len,const FLAC__int32 * flac_restrict qlp_coeff,uint32_t order,int lp_quantization,FLAC__int32 * flac_restrict residual)832 FLAC__bool FLAC__lpc_compute_residual_from_qlp_coefficients_limit_residual(const FLAC__int32 * flac_restrict data, uint32_t data_len, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization, FLAC__int32 * flac_restrict residual)
833 {
834 	int i;
835 	FLAC__int64 sum, residual_to_check;
836 
837 	FLAC__ASSERT(order > 0);
838 	FLAC__ASSERT(order <= 32);
839 
840 	for(i = 0; i < (int)data_len; i++) {
841 		sum = 0;
842 		switch(order) {
843 			case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32]; /* Falls through. */
844 			case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31]; /* Falls through. */
845 			case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30]; /* Falls through. */
846 			case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29]; /* Falls through. */
847 			case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28]; /* Falls through. */
848 			case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27]; /* Falls through. */
849 			case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26]; /* Falls through. */
850 			case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25]; /* Falls through. */
851 			case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24]; /* Falls through. */
852 			case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23]; /* Falls through. */
853 			case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22]; /* Falls through. */
854 			case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21]; /* Falls through. */
855 			case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20]; /* Falls through. */
856 			case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19]; /* Falls through. */
857 			case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18]; /* Falls through. */
858 			case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17]; /* Falls through. */
859 			case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16]; /* Falls through. */
860 			case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15]; /* Falls through. */
861 			case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14]; /* Falls through. */
862 			case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13]; /* Falls through. */
863 			case 12: sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; /* Falls through. */
864 			case 11: sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; /* Falls through. */
865 			case 10: sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10]; /* Falls through. */
866 			case  9: sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9]; /* Falls through. */
867 			case  8: sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8]; /* Falls through. */
868 			case  7: sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7]; /* Falls through. */
869 			case  6: sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6]; /* Falls through. */
870 			case  5: sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5]; /* Falls through. */
871 			case  4: sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4]; /* Falls through. */
872 			case  3: sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3]; /* Falls through. */
873 			case  2: sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2]; /* Falls through. */
874 			case  1: sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1];
875 		}
876 		residual_to_check = data[i] - (sum >> lp_quantization);
877 		 /* residual must not be INT32_MIN because abs(INT32_MIN) is undefined */
878 		if(residual_to_check <= INT32_MIN || residual_to_check > INT32_MAX)
879 			return false;
880 		else
881 			residual[i] = residual_to_check;
882 	}
883 	return true;
884 }
885 
FLAC__lpc_compute_residual_from_qlp_coefficients_limit_residual_33bit(const FLAC__int64 * flac_restrict data,uint32_t data_len,const FLAC__int32 * flac_restrict qlp_coeff,uint32_t order,int lp_quantization,FLAC__int32 * flac_restrict residual)886 FLAC__bool FLAC__lpc_compute_residual_from_qlp_coefficients_limit_residual_33bit(const FLAC__int64 * flac_restrict data, uint32_t data_len, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization, FLAC__int32 * flac_restrict residual)
887 {
888 	int i;
889 	FLAC__int64 sum, residual_to_check;
890 
891 	FLAC__ASSERT(order > 0);
892 	FLAC__ASSERT(order <= 32);
893 
894 	for(i = 0; i < (int)data_len; i++) {
895 		sum = 0;
896 		switch(order) {
897 			case 32: sum += qlp_coeff[31] * data[i-32]; /* Falls through. */
898 			case 31: sum += qlp_coeff[30] * data[i-31]; /* Falls through. */
899 			case 30: sum += qlp_coeff[29] * data[i-30]; /* Falls through. */
900 			case 29: sum += qlp_coeff[28] * data[i-29]; /* Falls through. */
901 			case 28: sum += qlp_coeff[27] * data[i-28]; /* Falls through. */
902 			case 27: sum += qlp_coeff[26] * data[i-27]; /* Falls through. */
903 			case 26: sum += qlp_coeff[25] * data[i-26]; /* Falls through. */
904 			case 25: sum += qlp_coeff[24] * data[i-25]; /* Falls through. */
905 			case 24: sum += qlp_coeff[23] * data[i-24]; /* Falls through. */
906 			case 23: sum += qlp_coeff[22] * data[i-23]; /* Falls through. */
907 			case 22: sum += qlp_coeff[21] * data[i-22]; /* Falls through. */
908 			case 21: sum += qlp_coeff[20] * data[i-21]; /* Falls through. */
909 			case 20: sum += qlp_coeff[19] * data[i-20]; /* Falls through. */
910 			case 19: sum += qlp_coeff[18] * data[i-19]; /* Falls through. */
911 			case 18: sum += qlp_coeff[17] * data[i-18]; /* Falls through. */
912 			case 17: sum += qlp_coeff[16] * data[i-17]; /* Falls through. */
913 			case 16: sum += qlp_coeff[15] * data[i-16]; /* Falls through. */
914 			case 15: sum += qlp_coeff[14] * data[i-15]; /* Falls through. */
915 			case 14: sum += qlp_coeff[13] * data[i-14]; /* Falls through. */
916 			case 13: sum += qlp_coeff[12] * data[i-13]; /* Falls through. */
917 			case 12: sum += qlp_coeff[11] * data[i-12]; /* Falls through. */
918 			case 11: sum += qlp_coeff[10] * data[i-11]; /* Falls through. */
919 			case 10: sum += qlp_coeff[ 9] * data[i-10]; /* Falls through. */
920 			case  9: sum += qlp_coeff[ 8] * data[i- 9]; /* Falls through. */
921 			case  8: sum += qlp_coeff[ 7] * data[i- 8]; /* Falls through. */
922 			case  7: sum += qlp_coeff[ 6] * data[i- 7]; /* Falls through. */
923 			case  6: sum += qlp_coeff[ 5] * data[i- 6]; /* Falls through. */
924 			case  5: sum += qlp_coeff[ 4] * data[i- 5]; /* Falls through. */
925 			case  4: sum += qlp_coeff[ 3] * data[i- 4]; /* Falls through. */
926 			case  3: sum += qlp_coeff[ 2] * data[i- 3]; /* Falls through. */
927 			case  2: sum += qlp_coeff[ 1] * data[i- 2]; /* Falls through. */
928 			case  1: sum += qlp_coeff[ 0] * data[i- 1];
929 		}
930 		residual_to_check = data[i] - (sum >> lp_quantization);
931 		/* residual must not be INT32_MIN because abs(INT32_MIN) is undefined */
932 		if(residual_to_check <= INT32_MIN || residual_to_check > INT32_MAX)
933 			return false;
934 		else
935 			residual[i] = residual_to_check;
936 	}
937 	return true;
938 }
939 
940 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
941 
FLAC__lpc_max_prediction_before_shift_bps(uint32_t subframe_bps,const FLAC__int32 * flac_restrict qlp_coeff,uint32_t order)942 uint32_t FLAC__lpc_max_prediction_before_shift_bps(uint32_t subframe_bps, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order)
943 {
944 	/* This used to be subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order)
945 	 * but that treats both the samples as well as the predictor as unknown. The
946 	 * predictor is known however, so taking the log2 of the sum of the absolute values
947 	 * of all coefficients is a more accurate representation of the predictor */
948 	FLAC__int32 abs_sum_of_qlp_coeff = 0;
949 	uint32_t i;
950 	for(i = 0; i < order; i++)
951 		abs_sum_of_qlp_coeff += abs(qlp_coeff[i]);
952 	if(abs_sum_of_qlp_coeff == 0)
953 		abs_sum_of_qlp_coeff = 1;
954 	return subframe_bps + FLAC__bitmath_silog2(abs_sum_of_qlp_coeff);
955 }
956 
957 
FLAC__lpc_max_residual_bps(uint32_t subframe_bps,const FLAC__int32 * flac_restrict qlp_coeff,uint32_t order,int lp_quantization)958 uint32_t FLAC__lpc_max_residual_bps(uint32_t subframe_bps, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization)
959 {
960 	FLAC__int32 predictor_sum_bps = FLAC__lpc_max_prediction_before_shift_bps(subframe_bps, qlp_coeff, order) - lp_quantization;
961 	if((int)subframe_bps > predictor_sum_bps)
962 		return subframe_bps + 1;
963 	else
964 		return predictor_sum_bps + 1;
965 }
966 
967 #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && !defined(FUZZING_BUILD_MODE_FLAC_SANITIZE_SIGNED_INTEGER_OVERFLOW)
968 /* The attribute below is to silence the undefined sanitizer of oss-fuzz.
969  * Because fuzzing feeds bogus predictors and residual samples to the
970  * decoder, having overflows in this section is unavoidable. Also,
971  * because the calculated values are audio path only, there is no
972  * potential for security problems */
973 __attribute__((no_sanitize("signed-integer-overflow")))
974 #endif
FLAC__lpc_restore_signal(const FLAC__int32 * flac_restrict residual,uint32_t data_len,const FLAC__int32 * flac_restrict qlp_coeff,uint32_t order,int lp_quantization,FLAC__int32 * flac_restrict data)975 void FLAC__lpc_restore_signal(const FLAC__int32 * flac_restrict residual, uint32_t data_len, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization, FLAC__int32 * flac_restrict data)
976 #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
977 {
978 	FLAC__int64 sumo;
979 	uint32_t i, j;
980 	FLAC__int32 sum;
981 	const FLAC__int32 *r = residual, *history;
982 
983 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
984 	fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
985 	for(i=0;i<order;i++)
986 		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
987 	fprintf(stderr,"\n");
988 #endif
989 	FLAC__ASSERT(order > 0);
990 
991 	for(i = 0; i < data_len; i++) {
992 		sumo = 0;
993 		sum = 0;
994 		history = data;
995 		for(j = 0; j < order; j++) {
996 			sum += qlp_coeff[j] * (*(--history));
997 			sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
998 #ifdef FLAC__OVERFLOW_DETECT
999 			if(sumo > 2147483647ll || sumo < -2147483648ll)
1000 				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);
1001 #endif
1002 		}
1003 		*(data++) = *(r++) + (sum >> lp_quantization);
1004 	}
1005 
1006 	/* Here's a slower but clearer version:
1007 	for(i = 0; i < data_len; i++) {
1008 		sum = 0;
1009 		for(j = 0; j < order; j++)
1010 			sum += qlp_coeff[j] * data[i-j-1];
1011 		data[i] = residual[i] + (sum >> lp_quantization);
1012 	}
1013 	*/
1014 }
1015 #else /* fully unrolled version for normal use */
1016 {
1017 	int i;
1018 	FLAC__int32 sum;
1019 
1020 	FLAC__ASSERT(order > 0);
1021 	FLAC__ASSERT(order <= 32);
1022 
1023 	/*
1024 	 * We do unique versions up to 12th order since that's the subset limit.
1025 	 * Also they are roughly ordered to match frequency of occurrence to
1026 	 * minimize branching.
1027 	 */
1028 	if(order <= 12) {
1029 		if(order > 8) {
1030 			if(order > 10) {
1031 				if(order == 12) {
1032 					for(i = 0; i < (int)data_len; i++) {
1033 						sum = 0;
1034 						sum += qlp_coeff[11] * data[i-12];
1035 						sum += qlp_coeff[10] * data[i-11];
1036 						sum += qlp_coeff[9] * data[i-10];
1037 						sum += qlp_coeff[8] * data[i-9];
1038 						sum += qlp_coeff[7] * data[i-8];
1039 						sum += qlp_coeff[6] * data[i-7];
1040 						sum += qlp_coeff[5] * data[i-6];
1041 						sum += qlp_coeff[4] * data[i-5];
1042 						sum += qlp_coeff[3] * data[i-4];
1043 						sum += qlp_coeff[2] * data[i-3];
1044 						sum += qlp_coeff[1] * data[i-2];
1045 						sum += qlp_coeff[0] * data[i-1];
1046 						data[i] = residual[i] + (sum >> lp_quantization);
1047 					}
1048 				}
1049 				else { /* order == 11 */
1050 					for(i = 0; i < (int)data_len; i++) {
1051 						sum = 0;
1052 						sum += qlp_coeff[10] * data[i-11];
1053 						sum += qlp_coeff[9] * data[i-10];
1054 						sum += qlp_coeff[8] * data[i-9];
1055 						sum += qlp_coeff[7] * data[i-8];
1056 						sum += qlp_coeff[6] * data[i-7];
1057 						sum += qlp_coeff[5] * data[i-6];
1058 						sum += qlp_coeff[4] * data[i-5];
1059 						sum += qlp_coeff[3] * data[i-4];
1060 						sum += qlp_coeff[2] * data[i-3];
1061 						sum += qlp_coeff[1] * data[i-2];
1062 						sum += qlp_coeff[0] * data[i-1];
1063 						data[i] = residual[i] + (sum >> lp_quantization);
1064 					}
1065 				}
1066 			}
1067 			else {
1068 				if(order == 10) {
1069 					for(i = 0; i < (int)data_len; i++) {
1070 						sum = 0;
1071 						sum += qlp_coeff[9] * data[i-10];
1072 						sum += qlp_coeff[8] * data[i-9];
1073 						sum += qlp_coeff[7] * data[i-8];
1074 						sum += qlp_coeff[6] * data[i-7];
1075 						sum += qlp_coeff[5] * data[i-6];
1076 						sum += qlp_coeff[4] * data[i-5];
1077 						sum += qlp_coeff[3] * data[i-4];
1078 						sum += qlp_coeff[2] * data[i-3];
1079 						sum += qlp_coeff[1] * data[i-2];
1080 						sum += qlp_coeff[0] * data[i-1];
1081 						data[i] = residual[i] + (sum >> lp_quantization);
1082 					}
1083 				}
1084 				else { /* order == 9 */
1085 					for(i = 0; i < (int)data_len; i++) {
1086 						sum = 0;
1087 						sum += qlp_coeff[8] * data[i-9];
1088 						sum += qlp_coeff[7] * data[i-8];
1089 						sum += qlp_coeff[6] * data[i-7];
1090 						sum += qlp_coeff[5] * data[i-6];
1091 						sum += qlp_coeff[4] * data[i-5];
1092 						sum += qlp_coeff[3] * data[i-4];
1093 						sum += qlp_coeff[2] * data[i-3];
1094 						sum += qlp_coeff[1] * data[i-2];
1095 						sum += qlp_coeff[0] * data[i-1];
1096 						data[i] = residual[i] + (sum >> lp_quantization);
1097 					}
1098 				}
1099 			}
1100 		}
1101 		else if(order > 4) {
1102 			if(order > 6) {
1103 				if(order == 8) {
1104 					for(i = 0; i < (int)data_len; i++) {
1105 						sum = 0;
1106 						sum += qlp_coeff[7] * data[i-8];
1107 						sum += qlp_coeff[6] * data[i-7];
1108 						sum += qlp_coeff[5] * data[i-6];
1109 						sum += qlp_coeff[4] * data[i-5];
1110 						sum += qlp_coeff[3] * data[i-4];
1111 						sum += qlp_coeff[2] * data[i-3];
1112 						sum += qlp_coeff[1] * data[i-2];
1113 						sum += qlp_coeff[0] * data[i-1];
1114 						data[i] = residual[i] + (sum >> lp_quantization);
1115 					}
1116 				}
1117 				else { /* order == 7 */
1118 					for(i = 0; i < (int)data_len; i++) {
1119 						sum = 0;
1120 						sum += qlp_coeff[6] * data[i-7];
1121 						sum += qlp_coeff[5] * data[i-6];
1122 						sum += qlp_coeff[4] * data[i-5];
1123 						sum += qlp_coeff[3] * data[i-4];
1124 						sum += qlp_coeff[2] * data[i-3];
1125 						sum += qlp_coeff[1] * data[i-2];
1126 						sum += qlp_coeff[0] * data[i-1];
1127 						data[i] = residual[i] + (sum >> lp_quantization);
1128 					}
1129 				}
1130 			}
1131 			else {
1132 				if(order == 6) {
1133 					for(i = 0; i < (int)data_len; i++) {
1134 						sum = 0;
1135 						sum += qlp_coeff[5] * data[i-6];
1136 						sum += qlp_coeff[4] * data[i-5];
1137 						sum += qlp_coeff[3] * data[i-4];
1138 						sum += qlp_coeff[2] * data[i-3];
1139 						sum += qlp_coeff[1] * data[i-2];
1140 						sum += qlp_coeff[0] * data[i-1];
1141 						data[i] = residual[i] + (sum >> lp_quantization);
1142 					}
1143 				}
1144 				else { /* order == 5 */
1145 					for(i = 0; i < (int)data_len; i++) {
1146 						sum = 0;
1147 						sum += qlp_coeff[4] * data[i-5];
1148 						sum += qlp_coeff[3] * data[i-4];
1149 						sum += qlp_coeff[2] * data[i-3];
1150 						sum += qlp_coeff[1] * data[i-2];
1151 						sum += qlp_coeff[0] * data[i-1];
1152 						data[i] = residual[i] + (sum >> lp_quantization);
1153 					}
1154 				}
1155 			}
1156 		}
1157 		else {
1158 			if(order > 2) {
1159 				if(order == 4) {
1160 					for(i = 0; i < (int)data_len; i++) {
1161 						sum = 0;
1162 						sum += qlp_coeff[3] * data[i-4];
1163 						sum += qlp_coeff[2] * data[i-3];
1164 						sum += qlp_coeff[1] * data[i-2];
1165 						sum += qlp_coeff[0] * data[i-1];
1166 						data[i] = residual[i] + (sum >> lp_quantization);
1167 					}
1168 				}
1169 				else { /* order == 3 */
1170 					for(i = 0; i < (int)data_len; i++) {
1171 						sum = 0;
1172 						sum += qlp_coeff[2] * data[i-3];
1173 						sum += qlp_coeff[1] * data[i-2];
1174 						sum += qlp_coeff[0] * data[i-1];
1175 						data[i] = residual[i] + (sum >> lp_quantization);
1176 					}
1177 				}
1178 			}
1179 			else {
1180 				if(order == 2) {
1181 					for(i = 0; i < (int)data_len; i++) {
1182 						sum = 0;
1183 						sum += qlp_coeff[1] * data[i-2];
1184 						sum += qlp_coeff[0] * data[i-1];
1185 						data[i] = residual[i] + (sum >> lp_quantization);
1186 					}
1187 				}
1188 				else { /* order == 1 */
1189 					for(i = 0; i < (int)data_len; i++)
1190 						data[i] = residual[i] + ((qlp_coeff[0] * data[i-1]) >> lp_quantization);
1191 				}
1192 			}
1193 		}
1194 	}
1195 	else { /* order > 12 */
1196 		for(i = 0; i < (int)data_len; i++) {
1197 			sum = 0;
1198 			switch(order) {
1199 				case 32: sum += qlp_coeff[31] * data[i-32]; /* Falls through. */
1200 				case 31: sum += qlp_coeff[30] * data[i-31]; /* Falls through. */
1201 				case 30: sum += qlp_coeff[29] * data[i-30]; /* Falls through. */
1202 				case 29: sum += qlp_coeff[28] * data[i-29]; /* Falls through. */
1203 				case 28: sum += qlp_coeff[27] * data[i-28]; /* Falls through. */
1204 				case 27: sum += qlp_coeff[26] * data[i-27]; /* Falls through. */
1205 				case 26: sum += qlp_coeff[25] * data[i-26]; /* Falls through. */
1206 				case 25: sum += qlp_coeff[24] * data[i-25]; /* Falls through. */
1207 				case 24: sum += qlp_coeff[23] * data[i-24]; /* Falls through. */
1208 				case 23: sum += qlp_coeff[22] * data[i-23]; /* Falls through. */
1209 				case 22: sum += qlp_coeff[21] * data[i-22]; /* Falls through. */
1210 				case 21: sum += qlp_coeff[20] * data[i-21]; /* Falls through. */
1211 				case 20: sum += qlp_coeff[19] * data[i-20]; /* Falls through. */
1212 				case 19: sum += qlp_coeff[18] * data[i-19]; /* Falls through. */
1213 				case 18: sum += qlp_coeff[17] * data[i-18]; /* Falls through. */
1214 				case 17: sum += qlp_coeff[16] * data[i-17]; /* Falls through. */
1215 				case 16: sum += qlp_coeff[15] * data[i-16]; /* Falls through. */
1216 				case 15: sum += qlp_coeff[14] * data[i-15]; /* Falls through. */
1217 				case 14: sum += qlp_coeff[13] * data[i-14]; /* Falls through. */
1218 				case 13: sum += qlp_coeff[12] * data[i-13];
1219 				         sum += qlp_coeff[11] * data[i-12];
1220 				         sum += qlp_coeff[10] * data[i-11];
1221 				         sum += qlp_coeff[ 9] * data[i-10];
1222 				         sum += qlp_coeff[ 8] * data[i- 9];
1223 				         sum += qlp_coeff[ 7] * data[i- 8];
1224 				         sum += qlp_coeff[ 6] * data[i- 7];
1225 				         sum += qlp_coeff[ 5] * data[i- 6];
1226 				         sum += qlp_coeff[ 4] * data[i- 5];
1227 				         sum += qlp_coeff[ 3] * data[i- 4];
1228 				         sum += qlp_coeff[ 2] * data[i- 3];
1229 				         sum += qlp_coeff[ 1] * data[i- 2];
1230 				         sum += qlp_coeff[ 0] * data[i- 1];
1231 			}
1232 			data[i] = residual[i] + (sum >> lp_quantization);
1233 		}
1234 	}
1235 }
1236 #endif
1237 
FLAC__lpc_restore_signal_wide(const FLAC__int32 * flac_restrict residual,uint32_t data_len,const FLAC__int32 * flac_restrict qlp_coeff,uint32_t order,int lp_quantization,FLAC__int32 * flac_restrict data)1238 void FLAC__lpc_restore_signal_wide(const FLAC__int32 * flac_restrict residual, uint32_t data_len, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization, FLAC__int32 * flac_restrict data)
1239 #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
1240 {
1241 	uint32_t i, j;
1242 	FLAC__int64 sum;
1243 	const FLAC__int32 *r = residual, *history;
1244 
1245 #ifdef FLAC__OVERFLOW_DETECT_VERBOSE
1246 	fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
1247 	for(i=0;i<order;i++)
1248 		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
1249 	fprintf(stderr,"\n");
1250 #endif
1251 	FLAC__ASSERT(order > 0);
1252 
1253 	for(i = 0; i < data_len; i++) {
1254 		sum = 0;
1255 		history = data;
1256 		for(j = 0; j < order; j++)
1257 			sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
1258 #ifdef FLAC__OVERFLOW_DETECT
1259 		if(FLAC__bitmath_silog2((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) {
1260 			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)));
1261 			break;
1262 		}
1263 #endif
1264 		*(data++) = (FLAC__int32)(*(r++) + (sum >> lp_quantization));
1265 	}
1266 }
1267 #else /* fully unrolled version for normal use */
1268 {
1269 	int i;
1270 	FLAC__int64 sum;
1271 
1272 	FLAC__ASSERT(order > 0);
1273 	FLAC__ASSERT(order <= 32);
1274 
1275 	/*
1276 	 * We do unique versions up to 12th order since that's the subset limit.
1277 	 * Also they are roughly ordered to match frequency of occurrence to
1278 	 * minimize branching.
1279 	 */
1280 	if(order <= 12) {
1281 		if(order > 8) {
1282 			if(order > 10) {
1283 				if(order == 12) {
1284 					for(i = 0; i < (int)data_len; i++) {
1285 						sum = 0;
1286 						sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
1287 						sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
1288 						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
1289 						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
1290 						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
1291 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1292 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1293 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1294 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1295 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1296 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1297 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1298 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1299 					}
1300 				}
1301 				else { /* order == 11 */
1302 					for(i = 0; i < (int)data_len; i++) {
1303 						sum = 0;
1304 						sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
1305 						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
1306 						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
1307 						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
1308 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1309 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1310 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1311 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1312 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1313 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1314 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1315 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1316 					}
1317 				}
1318 			}
1319 			else {
1320 				if(order == 10) {
1321 					for(i = 0; i < (int)data_len; i++) {
1322 						sum = 0;
1323 						sum += qlp_coeff[9] * (FLAC__int64)data[i-10];
1324 						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
1325 						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
1326 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1327 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1328 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1329 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1330 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1331 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1332 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1333 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1334 					}
1335 				}
1336 				else { /* order == 9 */
1337 					for(i = 0; i < (int)data_len; i++) {
1338 						sum = 0;
1339 						sum += qlp_coeff[8] * (FLAC__int64)data[i-9];
1340 						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
1341 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1342 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1343 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1344 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1345 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1346 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1347 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1348 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1349 					}
1350 				}
1351 			}
1352 		}
1353 		else if(order > 4) {
1354 			if(order > 6) {
1355 				if(order == 8) {
1356 					for(i = 0; i < (int)data_len; i++) {
1357 						sum = 0;
1358 						sum += qlp_coeff[7] * (FLAC__int64)data[i-8];
1359 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1360 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1361 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1362 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1363 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1364 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1365 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1366 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1367 					}
1368 				}
1369 				else { /* order == 7 */
1370 					for(i = 0; i < (int)data_len; i++) {
1371 						sum = 0;
1372 						sum += qlp_coeff[6] * (FLAC__int64)data[i-7];
1373 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1374 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1375 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1376 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1377 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1378 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1379 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1380 					}
1381 				}
1382 			}
1383 			else {
1384 				if(order == 6) {
1385 					for(i = 0; i < (int)data_len; i++) {
1386 						sum = 0;
1387 						sum += qlp_coeff[5] * (FLAC__int64)data[i-6];
1388 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1389 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1390 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1391 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1392 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1393 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1394 					}
1395 				}
1396 				else { /* order == 5 */
1397 					for(i = 0; i < (int)data_len; i++) {
1398 						sum = 0;
1399 						sum += qlp_coeff[4] * (FLAC__int64)data[i-5];
1400 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1401 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1402 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1403 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1404 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1405 					}
1406 				}
1407 			}
1408 		}
1409 		else {
1410 			if(order > 2) {
1411 				if(order == 4) {
1412 					for(i = 0; i < (int)data_len; i++) {
1413 						sum = 0;
1414 						sum += qlp_coeff[3] * (FLAC__int64)data[i-4];
1415 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1416 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1417 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1418 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1419 					}
1420 				}
1421 				else { /* order == 3 */
1422 					for(i = 0; i < (int)data_len; i++) {
1423 						sum = 0;
1424 						sum += qlp_coeff[2] * (FLAC__int64)data[i-3];
1425 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1426 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1427 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1428 					}
1429 				}
1430 			}
1431 			else {
1432 				if(order == 2) {
1433 					for(i = 0; i < (int)data_len; i++) {
1434 						sum = 0;
1435 						sum += qlp_coeff[1] * (FLAC__int64)data[i-2];
1436 						sum += qlp_coeff[0] * (FLAC__int64)data[i-1];
1437 						data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1438 					}
1439 				}
1440 				else { /* order == 1 */
1441 					for(i = 0; i < (int)data_len; i++)
1442 						data[i] = (FLAC__int32)(residual[i] + ((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization));
1443 				}
1444 			}
1445 		}
1446 	}
1447 	else { /* order > 12 */
1448 		for(i = 0; i < (int)data_len; i++) {
1449 			sum = 0;
1450 			switch(order) {
1451 				case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32]; /* Falls through. */
1452 				case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31]; /* Falls through. */
1453 				case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30]; /* Falls through. */
1454 				case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29]; /* Falls through. */
1455 				case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28]; /* Falls through. */
1456 				case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27]; /* Falls through. */
1457 				case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26]; /* Falls through. */
1458 				case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25]; /* Falls through. */
1459 				case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24]; /* Falls through. */
1460 				case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23]; /* Falls through. */
1461 				case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22]; /* Falls through. */
1462 				case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21]; /* Falls through. */
1463 				case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20]; /* Falls through. */
1464 				case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19]; /* Falls through. */
1465 				case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18]; /* Falls through. */
1466 				case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17]; /* Falls through. */
1467 				case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16]; /* Falls through. */
1468 				case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15]; /* Falls through. */
1469 				case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14]; /* Falls through. */
1470 				case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13];
1471 				         sum += qlp_coeff[11] * (FLAC__int64)data[i-12];
1472 				         sum += qlp_coeff[10] * (FLAC__int64)data[i-11];
1473 				         sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10];
1474 				         sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9];
1475 				         sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8];
1476 				         sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7];
1477 				         sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6];
1478 				         sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5];
1479 				         sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4];
1480 				         sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3];
1481 				         sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2];
1482 				         sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1];
1483 			}
1484 			data[i] = (FLAC__int32) (residual[i] + (sum >> lp_quantization));
1485 		}
1486 	}
1487 }
1488 #endif
1489 
1490 #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && !defined(FUZZING_BUILD_MODE_FLAC_SANITIZE_SIGNED_INTEGER_OVERFLOW)
1491 /* The attribute below is to silence the undefined sanitizer of oss-fuzz.
1492  * Because fuzzing feeds bogus predictors and residual samples to the
1493  * decoder, having overflows in this section is unavoidable. Also,
1494  * because the calculated values are audio path only, there is no
1495  * potential for security problems */
1496 __attribute__((no_sanitize("signed-integer-overflow")))
1497 #endif
FLAC__lpc_restore_signal_wide_33bit(const FLAC__int32 * flac_restrict residual,uint32_t data_len,const FLAC__int32 * flac_restrict qlp_coeff,uint32_t order,int lp_quantization,FLAC__int64 * flac_restrict data)1498 void FLAC__lpc_restore_signal_wide_33bit(const FLAC__int32 * flac_restrict residual, uint32_t data_len, const FLAC__int32 * flac_restrict qlp_coeff, uint32_t order, int lp_quantization, FLAC__int64 * flac_restrict data)
1499 #if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS)
1500 {
1501 	uint32_t i, j;
1502 	FLAC__int64 sum;
1503 	const FLAC__int32 *r = residual;
1504 	const FLAC__int64 *history;
1505 
1506 	FLAC__ASSERT(order > 0);
1507 
1508 	for(i = 0; i < data_len; i++) {
1509 		sum = 0;
1510 		history = data;
1511 		for(j = 0; j < order; j++)
1512 			sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
1513 #ifdef FLAC__OVERFLOW_DETECT
1514 		if(FLAC__bitmath_silog2((FLAC__int64)(*r) + (sum >> lp_quantization)) > 33) {
1515 			fprintf(stderr,"FLAC__lpc_restore_signal_33bit: OVERFLOW, i=%u, residual=%d, sum=%" PRId64 ", data=%" PRId64 "\n", i, *r, (sum >> lp_quantization), ((FLAC__int64)(*r) + (sum >> lp_quantization)));
1516 			break;
1517 		}
1518 #endif
1519 		*(data++) = (FLAC__int64)(*(r++)) + (sum >> lp_quantization);
1520 	}
1521 }
1522 #else /* unrolled version for normal use */
1523 {
1524 	int i;
1525 	FLAC__int64 sum;
1526 
1527 	FLAC__ASSERT(order > 0);
1528 	FLAC__ASSERT(order <= 32);
1529 
1530 	for(i = 0; i < (int)data_len; i++) {
1531 		sum = 0;
1532 		switch(order) {
1533 			case 32: sum += qlp_coeff[31] * data[i-32]; /* Falls through. */
1534 			case 31: sum += qlp_coeff[30] * data[i-31]; /* Falls through. */
1535 			case 30: sum += qlp_coeff[29] * data[i-30]; /* Falls through. */
1536 			case 29: sum += qlp_coeff[28] * data[i-29]; /* Falls through. */
1537 			case 28: sum += qlp_coeff[27] * data[i-28]; /* Falls through. */
1538 			case 27: sum += qlp_coeff[26] * data[i-27]; /* Falls through. */
1539 			case 26: sum += qlp_coeff[25] * data[i-26]; /* Falls through. */
1540 			case 25: sum += qlp_coeff[24] * data[i-25]; /* Falls through. */
1541 			case 24: sum += qlp_coeff[23] * data[i-24]; /* Falls through. */
1542 			case 23: sum += qlp_coeff[22] * data[i-23]; /* Falls through. */
1543 			case 22: sum += qlp_coeff[21] * data[i-22]; /* Falls through. */
1544 			case 21: sum += qlp_coeff[20] * data[i-21]; /* Falls through. */
1545 			case 20: sum += qlp_coeff[19] * data[i-20]; /* Falls through. */
1546 			case 19: sum += qlp_coeff[18] * data[i-19]; /* Falls through. */
1547 			case 18: sum += qlp_coeff[17] * data[i-18]; /* Falls through. */
1548 			case 17: sum += qlp_coeff[16] * data[i-17]; /* Falls through. */
1549 			case 16: sum += qlp_coeff[15] * data[i-16]; /* Falls through. */
1550 			case 15: sum += qlp_coeff[14] * data[i-15]; /* Falls through. */
1551 			case 14: sum += qlp_coeff[13] * data[i-14]; /* Falls through. */
1552 			case 13: sum += qlp_coeff[12] * data[i-13]; /* Falls through. */
1553 			case 12: sum += qlp_coeff[11] * data[i-12]; /* Falls through. */
1554 			case 11: sum += qlp_coeff[10] * data[i-11]; /* Falls through. */
1555 			case 10: sum += qlp_coeff[ 9] * data[i-10]; /* Falls through. */
1556 			case  9: sum += qlp_coeff[ 8] * data[i- 9]; /* Falls through. */
1557 			case  8: sum += qlp_coeff[ 7] * data[i- 8]; /* Falls through. */
1558 			case  7: sum += qlp_coeff[ 6] * data[i- 7]; /* Falls through. */
1559 			case  6: sum += qlp_coeff[ 5] * data[i- 6]; /* Falls through. */
1560 			case  5: sum += qlp_coeff[ 4] * data[i- 5]; /* Falls through. */
1561 			case  4: sum += qlp_coeff[ 3] * data[i- 4]; /* Falls through. */
1562 			case  3: sum += qlp_coeff[ 2] * data[i- 3]; /* Falls through. */
1563 			case  2: sum += qlp_coeff[ 1] * data[i- 2]; /* Falls through. */
1564 			case  1: sum += qlp_coeff[ 0] * data[i- 1];
1565 		}
1566 		data[i] = residual[i] + (sum >> lp_quantization);
1567 	}
1568 }
1569 #endif
1570 
1571 #if defined(_MSC_VER)
1572 #pragma warning ( default : 4028 )
1573 #endif
1574 
1575 #ifndef FLAC__INTEGER_ONLY_LIBRARY
1576 
FLAC__lpc_compute_expected_bits_per_residual_sample(double lpc_error,uint32_t total_samples)1577 double FLAC__lpc_compute_expected_bits_per_residual_sample(double lpc_error, uint32_t total_samples)
1578 {
1579 	double error_scale;
1580 
1581 	FLAC__ASSERT(total_samples > 0);
1582 
1583 	error_scale = 0.5 / (double)total_samples;
1584 
1585 	return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
1586 }
1587 
FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(double lpc_error,double error_scale)1588 double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(double lpc_error, double error_scale)
1589 {
1590 	if(lpc_error > 0.0) {
1591 		double bps = (double)0.5 * log(error_scale * lpc_error) / M_LN2;
1592 		if(bps >= 0.0)
1593 			return bps;
1594 		else
1595 			return 0.0;
1596 	}
1597 	else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate floating-point resolution */
1598 		return 1e32;
1599 	}
1600 	else {
1601 		return 0.0;
1602 	}
1603 }
1604 
FLAC__lpc_compute_best_order(const double lpc_error[],uint32_t max_order,uint32_t total_samples,uint32_t overhead_bits_per_order)1605 uint32_t FLAC__lpc_compute_best_order(const double lpc_error[], uint32_t max_order, uint32_t total_samples, uint32_t overhead_bits_per_order)
1606 {
1607 	uint32_t order, indx, 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 */
1608 	double bits, best_bits, error_scale;
1609 
1610 	FLAC__ASSERT(max_order > 0);
1611 	FLAC__ASSERT(total_samples > 0);
1612 
1613 	error_scale = 0.5 / (double)total_samples;
1614 
1615 	best_index = 0;
1616 	best_bits = (uint32_t)(-1);
1617 
1618 	for(indx = 0, order = 1; indx < max_order; indx++, order++) {
1619 		bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[indx], error_scale) * (double)(total_samples - order) + (double)(order * overhead_bits_per_order);
1620 		if(bits < best_bits) {
1621 			best_index = indx;
1622 			best_bits = bits;
1623 		}
1624 	}
1625 
1626 	return best_index+1; /* +1 since indx of lpc_error[] is order-1 */
1627 }
1628 
1629 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
1630