xref: /aosp_15_r20/external/flac/src/libFLAC/fixed.c (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1*600f14f4SXin Li /* libFLAC - Free Lossless Audio Codec library
2*600f14f4SXin Li  * Copyright (C) 2000-2009  Josh Coalson
3*600f14f4SXin Li  * Copyright (C) 2011-2023  Xiph.Org Foundation
4*600f14f4SXin Li  *
5*600f14f4SXin Li  * Redistribution and use in source and binary forms, with or without
6*600f14f4SXin Li  * modification, are permitted provided that the following conditions
7*600f14f4SXin Li  * are met:
8*600f14f4SXin Li  *
9*600f14f4SXin Li  * - Redistributions of source code must retain the above copyright
10*600f14f4SXin Li  * notice, this list of conditions and the following disclaimer.
11*600f14f4SXin Li  *
12*600f14f4SXin Li  * - Redistributions in binary form must reproduce the above copyright
13*600f14f4SXin Li  * notice, this list of conditions and the following disclaimer in the
14*600f14f4SXin Li  * documentation and/or other materials provided with the distribution.
15*600f14f4SXin Li  *
16*600f14f4SXin Li  * - Neither the name of the Xiph.org Foundation nor the names of its
17*600f14f4SXin Li  * contributors may be used to endorse or promote products derived from
18*600f14f4SXin Li  * this software without specific prior written permission.
19*600f14f4SXin Li  *
20*600f14f4SXin Li  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*600f14f4SXin Li  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*600f14f4SXin Li  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23*600f14f4SXin Li  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24*600f14f4SXin Li  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25*600f14f4SXin Li  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26*600f14f4SXin Li  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27*600f14f4SXin Li  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28*600f14f4SXin Li  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29*600f14f4SXin Li  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30*600f14f4SXin Li  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*600f14f4SXin Li  */
32*600f14f4SXin Li 
33*600f14f4SXin Li #ifdef HAVE_CONFIG_H
34*600f14f4SXin Li #  include <config.h>
35*600f14f4SXin Li #endif
36*600f14f4SXin Li 
37*600f14f4SXin Li #include <math.h>
38*600f14f4SXin Li #include <string.h>
39*600f14f4SXin Li #include "share/compat.h"
40*600f14f4SXin Li #include "private/bitmath.h"
41*600f14f4SXin Li #include "private/fixed.h"
42*600f14f4SXin Li #include "private/macros.h"
43*600f14f4SXin Li #include "FLAC/assert.h"
44*600f14f4SXin Li 
45*600f14f4SXin Li #ifdef local_abs
46*600f14f4SXin Li #undef local_abs
47*600f14f4SXin Li #endif
48*600f14f4SXin Li #define local_abs(x) ((uint32_t)((x)<0? -(x) : (x)))
49*600f14f4SXin Li 
50*600f14f4SXin Li #ifdef local_abs64
51*600f14f4SXin Li #undef local_abs64
52*600f14f4SXin Li #endif
53*600f14f4SXin Li #define local_abs64(x) ((uint64_t)((x)<0? -(x) : (x)))
54*600f14f4SXin Li 
55*600f14f4SXin Li #ifdef FLAC__INTEGER_ONLY_LIBRARY
56*600f14f4SXin Li /* rbps stands for residual bits per sample
57*600f14f4SXin Li  *
58*600f14f4SXin Li  *             (ln(2) * err)
59*600f14f4SXin Li  * rbps = log  (-----------)
60*600f14f4SXin Li  *           2 (     n     )
61*600f14f4SXin Li  */
local__compute_rbps_integerized(FLAC__uint32 err,FLAC__uint32 n)62*600f14f4SXin Li static FLAC__fixedpoint local__compute_rbps_integerized(FLAC__uint32 err, FLAC__uint32 n)
63*600f14f4SXin Li {
64*600f14f4SXin Li 	FLAC__uint32 rbps;
65*600f14f4SXin Li 	uint32_t bits; /* the number of bits required to represent a number */
66*600f14f4SXin Li 	int fracbits; /* the number of bits of rbps that comprise the fractional part */
67*600f14f4SXin Li 
68*600f14f4SXin Li 	FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint));
69*600f14f4SXin Li 	FLAC__ASSERT(err > 0);
70*600f14f4SXin Li 	FLAC__ASSERT(n > 0);
71*600f14f4SXin Li 
72*600f14f4SXin Li 	FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE);
73*600f14f4SXin Li 	if(err <= n)
74*600f14f4SXin Li 		return 0;
75*600f14f4SXin Li 	/*
76*600f14f4SXin Li 	 * The above two things tell us 1) n fits in 16 bits; 2) err/n > 1.
77*600f14f4SXin Li 	 * These allow us later to know we won't lose too much precision in the
78*600f14f4SXin Li 	 * fixed-point division (err<<fracbits)/n.
79*600f14f4SXin Li 	 */
80*600f14f4SXin Li 
81*600f14f4SXin Li 	fracbits = (8*sizeof(err)) - (FLAC__bitmath_ilog2(err)+1);
82*600f14f4SXin Li 
83*600f14f4SXin Li 	err <<= fracbits;
84*600f14f4SXin Li 	err /= n;
85*600f14f4SXin Li 	/* err now holds err/n with fracbits fractional bits */
86*600f14f4SXin Li 
87*600f14f4SXin Li 	/*
88*600f14f4SXin Li 	 * Whittle err down to 16 bits max.  16 significant bits is enough for
89*600f14f4SXin Li 	 * our purposes.
90*600f14f4SXin Li 	 */
91*600f14f4SXin Li 	FLAC__ASSERT(err > 0);
92*600f14f4SXin Li 	bits = FLAC__bitmath_ilog2(err)+1;
93*600f14f4SXin Li 	if(bits > 16) {
94*600f14f4SXin Li 		err >>= (bits-16);
95*600f14f4SXin Li 		fracbits -= (int)(bits-16);
96*600f14f4SXin Li 	}
97*600f14f4SXin Li 	rbps = (FLAC__uint32)err;
98*600f14f4SXin Li 
99*600f14f4SXin Li 	/* Multiply by fixed-point version of ln(2), with 16 fractional bits */
100*600f14f4SXin Li 	rbps *= FLAC__FP_LN2;
101*600f14f4SXin Li 	fracbits += 16;
102*600f14f4SXin Li 	FLAC__ASSERT(fracbits >= 0);
103*600f14f4SXin Li 
104*600f14f4SXin Li 	/* FLAC__fixedpoint_log2 requires fracbits%4 to be 0 */
105*600f14f4SXin Li 	{
106*600f14f4SXin Li 		const int f = fracbits & 3;
107*600f14f4SXin Li 		if(f) {
108*600f14f4SXin Li 			rbps >>= f;
109*600f14f4SXin Li 			fracbits -= f;
110*600f14f4SXin Li 		}
111*600f14f4SXin Li 	}
112*600f14f4SXin Li 
113*600f14f4SXin Li 	rbps = FLAC__fixedpoint_log2(rbps, fracbits, (uint32_t)(-1));
114*600f14f4SXin Li 
115*600f14f4SXin Li 	if(rbps == 0)
116*600f14f4SXin Li 		return 0;
117*600f14f4SXin Li 
118*600f14f4SXin Li 	/*
119*600f14f4SXin Li 	 * The return value must have 16 fractional bits.  Since the whole part
120*600f14f4SXin Li 	 * of the base-2 log of a 32 bit number must fit in 5 bits, and fracbits
121*600f14f4SXin Li 	 * must be >= -3, these assertion allows us to be able to shift rbps
122*600f14f4SXin Li 	 * left if necessary to get 16 fracbits without losing any bits of the
123*600f14f4SXin Li 	 * whole part of rbps.
124*600f14f4SXin Li 	 *
125*600f14f4SXin Li 	 * There is a slight chance due to accumulated error that the whole part
126*600f14f4SXin Li 	 * will require 6 bits, so we use 6 in the assertion.  Really though as
127*600f14f4SXin Li 	 * long as it fits in 13 bits (32 - (16 - (-3))) we are fine.
128*600f14f4SXin Li 	 */
129*600f14f4SXin Li 	FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6);
130*600f14f4SXin Li 	FLAC__ASSERT(fracbits >= -3);
131*600f14f4SXin Li 
132*600f14f4SXin Li 	/* now shift the decimal point into place */
133*600f14f4SXin Li 	if(fracbits < 16)
134*600f14f4SXin Li 		return rbps << (16-fracbits);
135*600f14f4SXin Li 	else if(fracbits > 16)
136*600f14f4SXin Li 		return rbps >> (fracbits-16);
137*600f14f4SXin Li 	else
138*600f14f4SXin Li 		return rbps;
139*600f14f4SXin Li }
140*600f14f4SXin Li 
local__compute_rbps_wide_integerized(FLAC__uint64 err,FLAC__uint32 n)141*600f14f4SXin Li static FLAC__fixedpoint local__compute_rbps_wide_integerized(FLAC__uint64 err, FLAC__uint32 n)
142*600f14f4SXin Li {
143*600f14f4SXin Li 	FLAC__uint32 rbps;
144*600f14f4SXin Li 	uint32_t bits; /* the number of bits required to represent a number */
145*600f14f4SXin Li 	int fracbits; /* the number of bits of rbps that comprise the fractional part */
146*600f14f4SXin Li 
147*600f14f4SXin Li 	FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint));
148*600f14f4SXin Li 	FLAC__ASSERT(err > 0);
149*600f14f4SXin Li 	FLAC__ASSERT(n > 0);
150*600f14f4SXin Li 
151*600f14f4SXin Li 	FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE);
152*600f14f4SXin Li 	if(err <= n)
153*600f14f4SXin Li 		return 0;
154*600f14f4SXin Li 	/*
155*600f14f4SXin Li 	 * The above two things tell us 1) n fits in 16 bits; 2) err/n > 1.
156*600f14f4SXin Li 	 * These allow us later to know we won't lose too much precision in the
157*600f14f4SXin Li 	 * fixed-point division (err<<fracbits)/n.
158*600f14f4SXin Li 	 */
159*600f14f4SXin Li 
160*600f14f4SXin Li 	fracbits = (8*sizeof(err)) - (FLAC__bitmath_ilog2_wide(err)+1);
161*600f14f4SXin Li 
162*600f14f4SXin Li 	err <<= fracbits;
163*600f14f4SXin Li 	err /= n;
164*600f14f4SXin Li 	/* err now holds err/n with fracbits fractional bits */
165*600f14f4SXin Li 
166*600f14f4SXin Li 	/*
167*600f14f4SXin Li 	 * Whittle err down to 16 bits max.  16 significant bits is enough for
168*600f14f4SXin Li 	 * our purposes.
169*600f14f4SXin Li 	 */
170*600f14f4SXin Li 	FLAC__ASSERT(err > 0);
171*600f14f4SXin Li 	bits = FLAC__bitmath_ilog2_wide(err)+1;
172*600f14f4SXin Li 	if(bits > 16) {
173*600f14f4SXin Li 		err >>= (bits-16);
174*600f14f4SXin Li 		fracbits -= (int)(bits-16); // defined, but cast to int to avoid ubsan assert.
175*600f14f4SXin Li 	}
176*600f14f4SXin Li 	rbps = (FLAC__uint32)err;
177*600f14f4SXin Li 
178*600f14f4SXin Li 	/* Multiply by fixed-point version of ln(2), with 16 fractional bits */
179*600f14f4SXin Li 	rbps *= FLAC__FP_LN2;
180*600f14f4SXin Li 	fracbits += 16;
181*600f14f4SXin Li 	FLAC__ASSERT(fracbits >= 0);
182*600f14f4SXin Li 
183*600f14f4SXin Li 	/* FLAC__fixedpoint_log2 requires fracbits%4 to be 0 */
184*600f14f4SXin Li 	{
185*600f14f4SXin Li 		const int f = fracbits & 3;
186*600f14f4SXin Li 		if(f) {
187*600f14f4SXin Li 			rbps >>= f;
188*600f14f4SXin Li 			fracbits -= f;
189*600f14f4SXin Li 		}
190*600f14f4SXin Li 	}
191*600f14f4SXin Li 
192*600f14f4SXin Li 	rbps = FLAC__fixedpoint_log2(rbps, fracbits, (uint32_t)(-1));
193*600f14f4SXin Li 
194*600f14f4SXin Li 	if(rbps == 0)
195*600f14f4SXin Li 		return 0;
196*600f14f4SXin Li 
197*600f14f4SXin Li 	/*
198*600f14f4SXin Li 	 * The return value must have 16 fractional bits.  Since the whole part
199*600f14f4SXin Li 	 * of the base-2 log of a 32 bit number must fit in 5 bits, and fracbits
200*600f14f4SXin Li 	 * must be >= -3, these assertion allows us to be able to shift rbps
201*600f14f4SXin Li 	 * left if necessary to get 16 fracbits without losing any bits of the
202*600f14f4SXin Li 	 * whole part of rbps.
203*600f14f4SXin Li 	 *
204*600f14f4SXin Li 	 * There is a slight chance due to accumulated error that the whole part
205*600f14f4SXin Li 	 * will require 6 bits, so we use 6 in the assertion.  Really though as
206*600f14f4SXin Li 	 * long as it fits in 13 bits (32 - (16 - (-3))) we are fine.
207*600f14f4SXin Li 	 */
208*600f14f4SXin Li 	FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6);
209*600f14f4SXin Li 	FLAC__ASSERT(fracbits >= -3);
210*600f14f4SXin Li 
211*600f14f4SXin Li 	/* now shift the decimal point into place */
212*600f14f4SXin Li 	if(fracbits < 16)
213*600f14f4SXin Li 		return rbps << (16-fracbits);
214*600f14f4SXin Li 	else if(fracbits > 16)
215*600f14f4SXin Li 		return rbps >> (fracbits-16);
216*600f14f4SXin Li 	else
217*600f14f4SXin Li 		return rbps;
218*600f14f4SXin Li }
219*600f14f4SXin Li #endif
220*600f14f4SXin Li 
221*600f14f4SXin Li #ifndef FLAC__INTEGER_ONLY_LIBRARY
FLAC__fixed_compute_best_predictor(const FLAC__int32 data[],uint32_t data_len,float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])222*600f14f4SXin Li uint32_t FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
223*600f14f4SXin Li #else
224*600f14f4SXin Li uint32_t FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
225*600f14f4SXin Li #endif
226*600f14f4SXin Li {
227*600f14f4SXin Li 	FLAC__uint32 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
228*600f14f4SXin Li 	uint32_t order;
229*600f14f4SXin Li #if 0
230*600f14f4SXin Li 	/* This code has been around a long time, and was written when compilers weren't able
231*600f14f4SXin Li 	 * to vectorize code. These days, compilers are better in optimizing the next block
232*600f14f4SXin Li 	 * which is also much more readable
233*600f14f4SXin Li 	 */
234*600f14f4SXin Li 	FLAC__int32 last_error_0 = data[-1];
235*600f14f4SXin Li 	FLAC__int32 last_error_1 = data[-1] - data[-2];
236*600f14f4SXin Li 	FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
237*600f14f4SXin Li 	FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
238*600f14f4SXin Li 	FLAC__int32 error, save;
239*600f14f4SXin Li 	uint32_t i;
240*600f14f4SXin Li 	/* total_error_* are 64-bits to avoid overflow when encoding
241*600f14f4SXin Li 	 * erratic signals when the bits-per-sample and blocksize are
242*600f14f4SXin Li 	 * large.
243*600f14f4SXin Li 	 */
244*600f14f4SXin Li 	for(i = 0; i < data_len; i++) {
245*600f14f4SXin Li 		error  = data[i]     ; total_error_0 += local_abs(error);                      save = error;
246*600f14f4SXin Li 		error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
247*600f14f4SXin Li 		error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
248*600f14f4SXin Li 		error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
249*600f14f4SXin Li 		error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
250*600f14f4SXin Li 	}
251*600f14f4SXin Li #else
252*600f14f4SXin Li 	int i;
253*600f14f4SXin Li 	for(i = 0; i < (int)data_len; i++) {
254*600f14f4SXin Li 		total_error_0 += local_abs(data[i]);
255*600f14f4SXin Li 		total_error_1 += local_abs(data[i] - data[i-1]);
256*600f14f4SXin Li 		total_error_2 += local_abs(data[i] - 2 * data[i-1] + data[i-2]);
257*600f14f4SXin Li 		total_error_3 += local_abs(data[i] - 3 * data[i-1] + 3 * data[i-2] - data[i-3]);
258*600f14f4SXin Li 		total_error_4 += local_abs(data[i] - 4 * data[i-1] + 6 * data[i-2] - 4 * data[i-3] + data[i-4]);
259*600f14f4SXin Li 	}
260*600f14f4SXin Li #endif
261*600f14f4SXin Li 
262*600f14f4SXin Li 
263*600f14f4SXin Li 	/* prefer lower order */
264*600f14f4SXin Li 	if(total_error_0 <= flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
265*600f14f4SXin Li 		order = 0;
266*600f14f4SXin Li 	else if(total_error_1 <= flac_min(flac_min(total_error_2, total_error_3), total_error_4))
267*600f14f4SXin Li 		order = 1;
268*600f14f4SXin Li 	else if(total_error_2 <= flac_min(total_error_3, total_error_4))
269*600f14f4SXin Li 		order = 2;
270*600f14f4SXin Li 	else if(total_error_3 <= total_error_4)
271*600f14f4SXin Li 		order = 3;
272*600f14f4SXin Li 	else
273*600f14f4SXin Li 		order = 4;
274*600f14f4SXin Li 
275*600f14f4SXin Li 	/* Estimate the expected number of bits per residual signal sample. */
276*600f14f4SXin Li 	/* 'total_error*' is linearly related to the variance of the residual */
277*600f14f4SXin Li 	/* signal, so we use it directly to compute E(|x|) */
278*600f14f4SXin Li 	FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
279*600f14f4SXin Li 	FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
280*600f14f4SXin Li 	FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
281*600f14f4SXin Li 	FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
282*600f14f4SXin Li 	FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
283*600f14f4SXin Li #ifndef FLAC__INTEGER_ONLY_LIBRARY
284*600f14f4SXin Li 	residual_bits_per_sample[0] = (float)((total_error_0 > 0) ? log(M_LN2 * (double)total_error_0 / (double)data_len) / M_LN2 : 0.0);
285*600f14f4SXin Li 	residual_bits_per_sample[1] = (float)((total_error_1 > 0) ? log(M_LN2 * (double)total_error_1 / (double)data_len) / M_LN2 : 0.0);
286*600f14f4SXin Li 	residual_bits_per_sample[2] = (float)((total_error_2 > 0) ? log(M_LN2 * (double)total_error_2 / (double)data_len) / M_LN2 : 0.0);
287*600f14f4SXin Li 	residual_bits_per_sample[3] = (float)((total_error_3 > 0) ? log(M_LN2 * (double)total_error_3 / (double)data_len) / M_LN2 : 0.0);
288*600f14f4SXin Li 	residual_bits_per_sample[4] = (float)((total_error_4 > 0) ? log(M_LN2 * (double)total_error_4 / (double)data_len) / M_LN2 : 0.0);
289*600f14f4SXin Li #else
290*600f14f4SXin Li 	residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_integerized(total_error_0, data_len) : 0;
291*600f14f4SXin Li 	residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_integerized(total_error_1, data_len) : 0;
292*600f14f4SXin Li 	residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_integerized(total_error_2, data_len) : 0;
293*600f14f4SXin Li 	residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_integerized(total_error_3, data_len) : 0;
294*600f14f4SXin Li 	residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_integerized(total_error_4, data_len) : 0;
295*600f14f4SXin Li #endif
296*600f14f4SXin Li 
297*600f14f4SXin Li 	return order;
298*600f14f4SXin Li }
299*600f14f4SXin Li 
300*600f14f4SXin Li #ifndef FLAC__INTEGER_ONLY_LIBRARY
FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[],uint32_t data_len,float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])301*600f14f4SXin Li uint32_t FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
302*600f14f4SXin Li #else
303*600f14f4SXin Li uint32_t FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
304*600f14f4SXin Li #endif
305*600f14f4SXin Li {
306*600f14f4SXin Li 	FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
307*600f14f4SXin Li 	uint32_t order;
308*600f14f4SXin Li 	int i;
309*600f14f4SXin Li 
310*600f14f4SXin Li 	for(i = 0; i < (int)data_len; i++) {
311*600f14f4SXin Li 		total_error_0 += local_abs(data[i]);
312*600f14f4SXin Li 		total_error_1 += local_abs(data[i] - data[i-1]);
313*600f14f4SXin Li 		total_error_2 += local_abs(data[i] - 2 * data[i-1] + data[i-2]);
314*600f14f4SXin Li 		total_error_3 += local_abs(data[i] - 3 * data[i-1] + 3 * data[i-2] - data[i-3]);
315*600f14f4SXin Li 		total_error_4 += local_abs(data[i] - 4 * data[i-1] + 6 * data[i-2] - 4 * data[i-3] + data[i-4]);
316*600f14f4SXin Li 	}
317*600f14f4SXin Li 
318*600f14f4SXin Li 	/* prefer lower order */
319*600f14f4SXin Li 	if(total_error_0 <= flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
320*600f14f4SXin Li 		order = 0;
321*600f14f4SXin Li 	else if(total_error_1 <= flac_min(flac_min(total_error_2, total_error_3), total_error_4))
322*600f14f4SXin Li 		order = 1;
323*600f14f4SXin Li 	else if(total_error_2 <= flac_min(total_error_3, total_error_4))
324*600f14f4SXin Li 		order = 2;
325*600f14f4SXin Li 	else if(total_error_3 <= total_error_4)
326*600f14f4SXin Li 		order = 3;
327*600f14f4SXin Li 	else
328*600f14f4SXin Li 		order = 4;
329*600f14f4SXin Li 
330*600f14f4SXin Li 	/* Estimate the expected number of bits per residual signal sample. */
331*600f14f4SXin Li 	/* 'total_error*' is linearly related to the variance of the residual */
332*600f14f4SXin Li 	/* signal, so we use it directly to compute E(|x|) */
333*600f14f4SXin Li 	FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
334*600f14f4SXin Li 	FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
335*600f14f4SXin Li 	FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
336*600f14f4SXin Li 	FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
337*600f14f4SXin Li 	FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
338*600f14f4SXin Li #ifndef FLAC__INTEGER_ONLY_LIBRARY
339*600f14f4SXin Li 	residual_bits_per_sample[0] = (float)((total_error_0 > 0) ? log(M_LN2 * (double)total_error_0 / (double)data_len) / M_LN2 : 0.0);
340*600f14f4SXin Li 	residual_bits_per_sample[1] = (float)((total_error_1 > 0) ? log(M_LN2 * (double)total_error_1 / (double)data_len) / M_LN2 : 0.0);
341*600f14f4SXin Li 	residual_bits_per_sample[2] = (float)((total_error_2 > 0) ? log(M_LN2 * (double)total_error_2 / (double)data_len) / M_LN2 : 0.0);
342*600f14f4SXin Li 	residual_bits_per_sample[3] = (float)((total_error_3 > 0) ? log(M_LN2 * (double)total_error_3 / (double)data_len) / M_LN2 : 0.0);
343*600f14f4SXin Li 	residual_bits_per_sample[4] = (float)((total_error_4 > 0) ? log(M_LN2 * (double)total_error_4 / (double)data_len) / M_LN2 : 0.0);
344*600f14f4SXin Li #else
345*600f14f4SXin Li 	residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_wide_integerized(total_error_0, data_len) : 0;
346*600f14f4SXin Li 	residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_wide_integerized(total_error_1, data_len) : 0;
347*600f14f4SXin Li 	residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_wide_integerized(total_error_2, data_len) : 0;
348*600f14f4SXin Li 	residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_wide_integerized(total_error_3, data_len) : 0;
349*600f14f4SXin Li 	residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_wide_integerized(total_error_4, data_len) : 0;
350*600f14f4SXin Li #endif
351*600f14f4SXin Li 
352*600f14f4SXin Li 	return order;
353*600f14f4SXin Li }
354*600f14f4SXin Li 
355*600f14f4SXin Li #ifndef FLAC__INTEGER_ONLY_LIBRARY
356*600f14f4SXin Li #define CHECK_ORDER_IS_VALID(macro_order)		\
357*600f14f4SXin Li if(order_##macro_order##_is_valid && total_error_##macro_order < smallest_error) { \
358*600f14f4SXin Li 	order = macro_order;				\
359*600f14f4SXin Li 	smallest_error = total_error_##macro_order ;	\
360*600f14f4SXin Li 	residual_bits_per_sample[ macro_order ] = (float)((total_error_0 > 0) ? log(M_LN2 * (double)total_error_0 / (double)data_len) / M_LN2 : 0.0); \
361*600f14f4SXin Li }							\
362*600f14f4SXin Li else							\
363*600f14f4SXin Li 	residual_bits_per_sample[ macro_order ] = 34.0f;
364*600f14f4SXin Li #else
365*600f14f4SXin Li #define CHECK_ORDER_IS_VALID(macro_order)		\
366*600f14f4SXin Li if(order_##macro_order##_is_valid && total_error_##macro_order < smallest_error) { \
367*600f14f4SXin Li 	order = macro_order;				\
368*600f14f4SXin Li 	smallest_error = total_error_##macro_order ;	\
369*600f14f4SXin Li 	residual_bits_per_sample[ macro_order ] = (total_error_##macro_order > 0) ? local__compute_rbps_wide_integerized(total_error_##macro_order, data_len) : 0; \
370*600f14f4SXin Li }							\
371*600f14f4SXin Li else							\
372*600f14f4SXin Li 	residual_bits_per_sample[ macro_order ] = 34 * FLAC__FP_ONE;
373*600f14f4SXin Li #endif
374*600f14f4SXin Li 
375*600f14f4SXin Li 
376*600f14f4SXin Li #ifndef FLAC__INTEGER_ONLY_LIBRARY
FLAC__fixed_compute_best_predictor_limit_residual(const FLAC__int32 data[],uint32_t data_len,float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])377*600f14f4SXin Li uint32_t FLAC__fixed_compute_best_predictor_limit_residual(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
378*600f14f4SXin Li #else
379*600f14f4SXin Li uint32_t FLAC__fixed_compute_best_predictor_limit_residual(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
380*600f14f4SXin Li #endif
381*600f14f4SXin Li {
382*600f14f4SXin Li 	FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0, smallest_error = UINT64_MAX;
383*600f14f4SXin Li 	FLAC__uint64 error_0, error_1, error_2, error_3, error_4;
384*600f14f4SXin Li 	FLAC__bool order_0_is_valid = true, order_1_is_valid = true, order_2_is_valid = true, order_3_is_valid = true, order_4_is_valid = true;
385*600f14f4SXin Li 	uint32_t order = 0;
386*600f14f4SXin Li 	int i;
387*600f14f4SXin Li 
388*600f14f4SXin Li 	for(i = -4; i < (int)data_len; i++) {
389*600f14f4SXin Li 		error_0 = local_abs64((FLAC__int64)data[i]);
390*600f14f4SXin Li 		error_1 = (i > -4) ? local_abs64((FLAC__int64)data[i] - data[i-1]) : 0 ;
391*600f14f4SXin Li 		error_2 = (i > -3) ? local_abs64((FLAC__int64)data[i] - 2 * (FLAC__int64)data[i-1] + data[i-2]) : 0;
392*600f14f4SXin Li 		error_3 = (i > -2) ? local_abs64((FLAC__int64)data[i] - 3 * (FLAC__int64)data[i-1] + 3 * (FLAC__int64)data[i-2] - data[i-3]) : 0;
393*600f14f4SXin Li 		error_4 = (i > -1) ? local_abs64((FLAC__int64)data[i] - 4 * (FLAC__int64)data[i-1] + 6 * (FLAC__int64)data[i-2] - 4 * (FLAC__int64)data[i-3] + data[i-4]) : 0;
394*600f14f4SXin Li 
395*600f14f4SXin Li 		total_error_0 += error_0;
396*600f14f4SXin Li 		total_error_1 += error_1;
397*600f14f4SXin Li 		total_error_2 += error_2;
398*600f14f4SXin Li 		total_error_3 += error_3;
399*600f14f4SXin Li 		total_error_4 += error_4;
400*600f14f4SXin Li 
401*600f14f4SXin Li 		/* residual must not be INT32_MIN because abs(INT32_MIN) is undefined */
402*600f14f4SXin Li 		if(error_0 > INT32_MAX)
403*600f14f4SXin Li 			order_0_is_valid = false;
404*600f14f4SXin Li 		if(error_1 > INT32_MAX)
405*600f14f4SXin Li 			order_1_is_valid = false;
406*600f14f4SXin Li 		if(error_2 > INT32_MAX)
407*600f14f4SXin Li 			order_2_is_valid = false;
408*600f14f4SXin Li 		if(error_3 > INT32_MAX)
409*600f14f4SXin Li 			order_3_is_valid = false;
410*600f14f4SXin Li 		if(error_4 > INT32_MAX)
411*600f14f4SXin Li 			order_4_is_valid = false;
412*600f14f4SXin Li 	}
413*600f14f4SXin Li 
414*600f14f4SXin Li 	CHECK_ORDER_IS_VALID(0);
415*600f14f4SXin Li 	CHECK_ORDER_IS_VALID(1);
416*600f14f4SXin Li 	CHECK_ORDER_IS_VALID(2);
417*600f14f4SXin Li 	CHECK_ORDER_IS_VALID(3);
418*600f14f4SXin Li 	CHECK_ORDER_IS_VALID(4);
419*600f14f4SXin Li 
420*600f14f4SXin Li 	return order;
421*600f14f4SXin Li }
422*600f14f4SXin Li 
423*600f14f4SXin Li #ifndef FLAC__INTEGER_ONLY_LIBRARY
FLAC__fixed_compute_best_predictor_limit_residual_33bit(const FLAC__int64 data[],uint32_t data_len,float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])424*600f14f4SXin Li uint32_t FLAC__fixed_compute_best_predictor_limit_residual_33bit(const FLAC__int64 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
425*600f14f4SXin Li #else
426*600f14f4SXin Li uint32_t FLAC__fixed_compute_best_predictor_limit_residual_33bit(const FLAC__int64 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
427*600f14f4SXin Li #endif
428*600f14f4SXin Li {
429*600f14f4SXin Li 	FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0, smallest_error = UINT64_MAX;
430*600f14f4SXin Li 	FLAC__uint64 error_0, error_1, error_2, error_3, error_4;
431*600f14f4SXin Li 	FLAC__bool order_0_is_valid = true, order_1_is_valid = true, order_2_is_valid = true, order_3_is_valid = true, order_4_is_valid = true;
432*600f14f4SXin Li 	uint32_t order = 0;
433*600f14f4SXin Li 	int i;
434*600f14f4SXin Li 
435*600f14f4SXin Li 	for(i = -4; i < (int)data_len; i++) {
436*600f14f4SXin Li 		error_0 = local_abs64(data[i]);
437*600f14f4SXin Li 		error_1 = (i > -4) ? local_abs64(data[i] - data[i-1]) : 0 ;
438*600f14f4SXin Li 		error_2 = (i > -3) ? local_abs64(data[i] - 2 * data[i-1] + data[i-2]) : 0;
439*600f14f4SXin Li 		error_3 = (i > -2) ? local_abs64(data[i] - 3 * data[i-1] + 3 * data[i-2] - data[i-3]) : 0;
440*600f14f4SXin Li 		error_4 = (i > -1) ? local_abs64(data[i] - 4 * data[i-1] + 6 * data[i-2] - 4 * data[i-3] + data[i-4]) : 0;
441*600f14f4SXin Li 
442*600f14f4SXin Li 		total_error_0 += error_0;
443*600f14f4SXin Li 		total_error_1 += error_1;
444*600f14f4SXin Li 		total_error_2 += error_2;
445*600f14f4SXin Li 		total_error_3 += error_3;
446*600f14f4SXin Li 		total_error_4 += error_4;
447*600f14f4SXin Li 
448*600f14f4SXin Li 		/* residual must not be INT32_MIN because abs(INT32_MIN) is undefined */
449*600f14f4SXin Li 		if(error_0 > INT32_MAX)
450*600f14f4SXin Li 			order_0_is_valid = false;
451*600f14f4SXin Li 		if(error_1 > INT32_MAX)
452*600f14f4SXin Li 			order_1_is_valid = false;
453*600f14f4SXin Li 		if(error_2 > INT32_MAX)
454*600f14f4SXin Li 			order_2_is_valid = false;
455*600f14f4SXin Li 		if(error_3 > INT32_MAX)
456*600f14f4SXin Li 			order_3_is_valid = false;
457*600f14f4SXin Li 		if(error_4 > INT32_MAX)
458*600f14f4SXin Li 			order_4_is_valid = false;
459*600f14f4SXin Li 	}
460*600f14f4SXin Li 
461*600f14f4SXin Li 	CHECK_ORDER_IS_VALID(0);
462*600f14f4SXin Li 	CHECK_ORDER_IS_VALID(1);
463*600f14f4SXin Li 	CHECK_ORDER_IS_VALID(2);
464*600f14f4SXin Li 	CHECK_ORDER_IS_VALID(3);
465*600f14f4SXin Li 	CHECK_ORDER_IS_VALID(4);
466*600f14f4SXin Li 
467*600f14f4SXin Li 	return order;
468*600f14f4SXin Li }
469*600f14f4SXin Li 
FLAC__fixed_compute_residual(const FLAC__int32 data[],uint32_t data_len,uint32_t order,FLAC__int32 residual[])470*600f14f4SXin Li void FLAC__fixed_compute_residual(const FLAC__int32 data[], uint32_t data_len, uint32_t order, FLAC__int32 residual[])
471*600f14f4SXin Li {
472*600f14f4SXin Li 	const int idata_len = (int)data_len;
473*600f14f4SXin Li 	int i;
474*600f14f4SXin Li 
475*600f14f4SXin Li 	switch(order) {
476*600f14f4SXin Li 		case 0:
477*600f14f4SXin Li 			FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
478*600f14f4SXin Li 			memcpy(residual, data, sizeof(residual[0])*data_len);
479*600f14f4SXin Li 			break;
480*600f14f4SXin Li 		case 1:
481*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
482*600f14f4SXin Li 				residual[i] = data[i] - data[i-1];
483*600f14f4SXin Li 			break;
484*600f14f4SXin Li 		case 2:
485*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
486*600f14f4SXin Li 				residual[i] = data[i] - 2*data[i-1] + data[i-2];
487*600f14f4SXin Li 			break;
488*600f14f4SXin Li 		case 3:
489*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
490*600f14f4SXin Li 				residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3];
491*600f14f4SXin Li 			break;
492*600f14f4SXin Li 		case 4:
493*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
494*600f14f4SXin Li 				residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4];
495*600f14f4SXin Li 			break;
496*600f14f4SXin Li 		default:
497*600f14f4SXin Li 			FLAC__ASSERT(0);
498*600f14f4SXin Li 	}
499*600f14f4SXin Li }
500*600f14f4SXin Li 
FLAC__fixed_compute_residual_wide(const FLAC__int32 data[],uint32_t data_len,uint32_t order,FLAC__int32 residual[])501*600f14f4SXin Li void FLAC__fixed_compute_residual_wide(const FLAC__int32 data[], uint32_t data_len, uint32_t order, FLAC__int32 residual[])
502*600f14f4SXin Li {
503*600f14f4SXin Li 	const int idata_len = (int)data_len;
504*600f14f4SXin Li 	int i;
505*600f14f4SXin Li 
506*600f14f4SXin Li 	switch(order) {
507*600f14f4SXin Li 		case 0:
508*600f14f4SXin Li 			FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
509*600f14f4SXin Li 			memcpy(residual, data, sizeof(residual[0])*data_len);
510*600f14f4SXin Li 			break;
511*600f14f4SXin Li 		case 1:
512*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
513*600f14f4SXin Li 				residual[i] = (FLAC__int64)data[i] - data[i-1];
514*600f14f4SXin Li 			break;
515*600f14f4SXin Li 		case 2:
516*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
517*600f14f4SXin Li 				residual[i] = (FLAC__int64)data[i] - 2*(FLAC__int64)data[i-1] + data[i-2];
518*600f14f4SXin Li 			break;
519*600f14f4SXin Li 		case 3:
520*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
521*600f14f4SXin Li 				residual[i] = (FLAC__int64)data[i] - 3*(FLAC__int64)data[i-1] + 3*(FLAC__int64)data[i-2] - data[i-3];
522*600f14f4SXin Li 			break;
523*600f14f4SXin Li 		case 4:
524*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
525*600f14f4SXin Li 				residual[i] = (FLAC__int64)data[i] - 4*(FLAC__int64)data[i-1] + 6*(FLAC__int64)data[i-2] - 4*(FLAC__int64)data[i-3] + data[i-4];
526*600f14f4SXin Li 			break;
527*600f14f4SXin Li 		default:
528*600f14f4SXin Li 			FLAC__ASSERT(0);
529*600f14f4SXin Li 	}
530*600f14f4SXin Li }
531*600f14f4SXin Li 
FLAC__fixed_compute_residual_wide_33bit(const FLAC__int64 data[],uint32_t data_len,uint32_t order,FLAC__int32 residual[])532*600f14f4SXin Li void FLAC__fixed_compute_residual_wide_33bit(const FLAC__int64 data[], uint32_t data_len, uint32_t order, FLAC__int32 residual[])
533*600f14f4SXin Li {
534*600f14f4SXin Li 	const int idata_len = (int)data_len;
535*600f14f4SXin Li 	int i;
536*600f14f4SXin Li 
537*600f14f4SXin Li 	switch(order) {
538*600f14f4SXin Li 		case 0:
539*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
540*600f14f4SXin Li 				residual[i] = data[i];
541*600f14f4SXin Li 			break;
542*600f14f4SXin Li 		case 1:
543*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
544*600f14f4SXin Li 				residual[i] = data[i] - data[i-1];
545*600f14f4SXin Li 			break;
546*600f14f4SXin Li 		case 2:
547*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
548*600f14f4SXin Li 				residual[i] = data[i] - 2*data[i-1] + data[i-2];
549*600f14f4SXin Li 			break;
550*600f14f4SXin Li 		case 3:
551*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
552*600f14f4SXin Li 				residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3];
553*600f14f4SXin Li 			break;
554*600f14f4SXin Li 		case 4:
555*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
556*600f14f4SXin Li 				residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4];
557*600f14f4SXin Li 			break;
558*600f14f4SXin Li 		default:
559*600f14f4SXin Li 			FLAC__ASSERT(0);
560*600f14f4SXin Li 	}
561*600f14f4SXin Li }
562*600f14f4SXin Li 
563*600f14f4SXin Li #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && !defined(FUZZING_BUILD_MODE_FLAC_SANITIZE_SIGNED_INTEGER_OVERFLOW)
564*600f14f4SXin Li /* The attribute below is to silence the undefined sanitizer of oss-fuzz.
565*600f14f4SXin Li  * Because fuzzing feeds bogus predictors and residual samples to the
566*600f14f4SXin Li  * decoder, having overflows in this section is unavoidable. Also,
567*600f14f4SXin Li  * because the calculated values are audio path only, there is no
568*600f14f4SXin Li  * potential for security problems */
569*600f14f4SXin Li __attribute__((no_sanitize("signed-integer-overflow")))
570*600f14f4SXin Li #endif
FLAC__fixed_restore_signal(const FLAC__int32 residual[],uint32_t data_len,uint32_t order,FLAC__int32 data[])571*600f14f4SXin Li void FLAC__fixed_restore_signal(const FLAC__int32 residual[], uint32_t data_len, uint32_t order, FLAC__int32 data[])
572*600f14f4SXin Li {
573*600f14f4SXin Li 	int i, idata_len = (int)data_len;
574*600f14f4SXin Li 
575*600f14f4SXin Li 	switch(order) {
576*600f14f4SXin Li 		case 0:
577*600f14f4SXin Li 			FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
578*600f14f4SXin Li 			memcpy(data, residual, sizeof(residual[0])*data_len);
579*600f14f4SXin Li 			break;
580*600f14f4SXin Li 		case 1:
581*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
582*600f14f4SXin Li 				data[i] = residual[i] + data[i-1];
583*600f14f4SXin Li 			break;
584*600f14f4SXin Li 		case 2:
585*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
586*600f14f4SXin Li 				data[i] = residual[i] + 2*data[i-1] - data[i-2];
587*600f14f4SXin Li 			break;
588*600f14f4SXin Li 		case 3:
589*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
590*600f14f4SXin Li 				data[i] = residual[i] + 3*data[i-1] - 3*data[i-2] + data[i-3];
591*600f14f4SXin Li 			break;
592*600f14f4SXin Li 		case 4:
593*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
594*600f14f4SXin Li 				data[i] = residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4];
595*600f14f4SXin Li 			break;
596*600f14f4SXin Li 		default:
597*600f14f4SXin Li 			FLAC__ASSERT(0);
598*600f14f4SXin Li 	}
599*600f14f4SXin Li }
600*600f14f4SXin Li 
FLAC__fixed_restore_signal_wide(const FLAC__int32 residual[],uint32_t data_len,uint32_t order,FLAC__int32 data[])601*600f14f4SXin Li void FLAC__fixed_restore_signal_wide(const FLAC__int32 residual[], uint32_t data_len, uint32_t order, FLAC__int32 data[])
602*600f14f4SXin Li {
603*600f14f4SXin Li 	int i, idata_len = (int)data_len;
604*600f14f4SXin Li 
605*600f14f4SXin Li 	switch(order) {
606*600f14f4SXin Li 		case 0:
607*600f14f4SXin Li 			FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
608*600f14f4SXin Li 			memcpy(data, residual, sizeof(residual[0])*data_len);
609*600f14f4SXin Li 			break;
610*600f14f4SXin Li 		case 1:
611*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
612*600f14f4SXin Li 				data[i] = (FLAC__int64)residual[i] + (FLAC__int64)data[i-1];
613*600f14f4SXin Li 			break;
614*600f14f4SXin Li 		case 2:
615*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
616*600f14f4SXin Li 				data[i] = (FLAC__int64)residual[i] + 2*(FLAC__int64)data[i-1] - (FLAC__int64)data[i-2];
617*600f14f4SXin Li 			break;
618*600f14f4SXin Li 		case 3:
619*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
620*600f14f4SXin Li 				data[i] = (FLAC__int64)residual[i] + 3*(FLAC__int64)data[i-1] - 3*(FLAC__int64)data[i-2] + (FLAC__int64)data[i-3];
621*600f14f4SXin Li 			break;
622*600f14f4SXin Li 		case 4:
623*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
624*600f14f4SXin Li 				data[i] = (FLAC__int64)residual[i] + 4*(FLAC__int64)data[i-1] - 6*(FLAC__int64)data[i-2] + 4*(FLAC__int64)data[i-3] - (FLAC__int64)data[i-4];
625*600f14f4SXin Li 			break;
626*600f14f4SXin Li 		default:
627*600f14f4SXin Li 			FLAC__ASSERT(0);
628*600f14f4SXin Li 	}
629*600f14f4SXin Li }
630*600f14f4SXin Li 
631*600f14f4SXin Li #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && !defined(FUZZING_BUILD_MODE_FLAC_SANITIZE_SIGNED_INTEGER_OVERFLOW)
632*600f14f4SXin Li /* The attribute below is to silence the undefined sanitizer of oss-fuzz.
633*600f14f4SXin Li  * Because fuzzing feeds bogus predictors and residual samples to the
634*600f14f4SXin Li  * decoder, having overflows in this section is unavoidable. Also,
635*600f14f4SXin Li  * because the calculated values are audio path only, there is no
636*600f14f4SXin Li  * potential for security problems */
637*600f14f4SXin Li __attribute__((no_sanitize("signed-integer-overflow")))
638*600f14f4SXin Li #endif
FLAC__fixed_restore_signal_wide_33bit(const FLAC__int32 residual[],uint32_t data_len,uint32_t order,FLAC__int64 data[])639*600f14f4SXin Li void FLAC__fixed_restore_signal_wide_33bit(const FLAC__int32 residual[], uint32_t data_len, uint32_t order, FLAC__int64 data[])
640*600f14f4SXin Li {
641*600f14f4SXin Li 	int i, idata_len = (int)data_len;
642*600f14f4SXin Li 
643*600f14f4SXin Li 	switch(order) {
644*600f14f4SXin Li 		case 0:
645*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
646*600f14f4SXin Li 				data[i] = residual[i];
647*600f14f4SXin Li 			break;
648*600f14f4SXin Li 		case 1:
649*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
650*600f14f4SXin Li 				data[i] = (FLAC__int64)residual[i] + data[i-1];
651*600f14f4SXin Li 			break;
652*600f14f4SXin Li 		case 2:
653*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
654*600f14f4SXin Li 				data[i] = (FLAC__int64)residual[i] + 2*data[i-1] - data[i-2];
655*600f14f4SXin Li 			break;
656*600f14f4SXin Li 		case 3:
657*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
658*600f14f4SXin Li 				data[i] = (FLAC__int64)residual[i] + 3*data[i-1] - 3*data[i-2] + data[i-3];
659*600f14f4SXin Li 			break;
660*600f14f4SXin Li 		case 4:
661*600f14f4SXin Li 			for(i = 0; i < idata_len; i++)
662*600f14f4SXin Li 				data[i] = (FLAC__int64)residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4];
663*600f14f4SXin Li 			break;
664*600f14f4SXin Li 		default:
665*600f14f4SXin Li 			FLAC__ASSERT(0);
666*600f14f4SXin Li 	}
667*600f14f4SXin Li }
668