Coverage Report

Created: 2025-10-10 06:56

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