Coverage Report

Created: 2026-10-17 07:05

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
133M
#define local_abs(x) ((uint32_t)((x)<0? -(x) : (x)))
49
50
#ifdef local_abs64
51
#undef local_abs64
52
#endif
53
127M
#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
551k
{
227
551k
  FLAC__uint32 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
228
551k
  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
551k
  int i;
253
11.8M
  for(i = 0; i < (int)data_len; i++) {
254
11.3M
    total_error_0 += local_abs(data[i]);
255
11.3M
    total_error_1 += local_abs(data[i] - data[i-1]);
256
11.3M
    total_error_2 += local_abs(data[i] - 2 * data[i-1] + data[i-2]);
257
11.3M
    total_error_3 += local_abs(data[i] - 3 * data[i-1] + 3 * data[i-2] - data[i-3]);
258
11.3M
    total_error_4 += local_abs(data[i] - 4 * data[i-1] + 6 * data[i-2] - 4 * data[i-3] + data[i-4]);
259
11.3M
  }
260
551k
#endif
261
262
263
  /* prefer lower order */
264
551k
  if(total_error_0 <= flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
265
10.1k
    order = 0;
266
541k
  else if(total_error_1 <= flac_min(flac_min(total_error_2, total_error_3), total_error_4))
267
537k
    order = 1;
268
4.12k
  else if(total_error_2 <= flac_min(total_error_3, total_error_4))
269
5.18k
    order = 2;
270
18.4E
  else if(total_error_3 <= total_error_4)
271
772
    order = 3;
272
18.4E
  else
273
18.4E
    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
551k
  FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
279
551k
  FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
280
551k
  FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
281
551k
  FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
282
551k
  FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
283
551k
#ifndef FLAC__INTEGER_ONLY_LIBRARY
284
18.4E
  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
551k
  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
551k
  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
551k
  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
551k
  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
551k
  return order;
298
551k
}
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
29.8k
{
306
29.8k
  FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
307
29.8k
  uint32_t order;
308
29.8k
  int i;
309
310
15.3M
  for(i = 0; i < (int)data_len; i++) {
311
15.3M
    total_error_0 += local_abs(data[i]);
312
15.3M
    total_error_1 += local_abs(data[i] - data[i-1]);
313
15.3M
    total_error_2 += local_abs(data[i] - 2 * data[i-1] + data[i-2]);
314
15.3M
    total_error_3 += local_abs(data[i] - 3 * data[i-1] + 3 * data[i-2] - data[i-3]);
315
15.3M
    total_error_4 += local_abs(data[i] - 4 * data[i-1] + 6 * data[i-2] - 4 * data[i-3] + data[i-4]);
316
15.3M
  }
317
318
  /* prefer lower order */
319
29.8k
  if(total_error_0 <= flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
320
2.59k
    order = 0;
321
27.2k
  else if(total_error_1 <= flac_min(flac_min(total_error_2, total_error_3), total_error_4))
322
25.6k
    order = 1;
323
1.64k
  else if(total_error_2 <= flac_min(total_error_3, total_error_4))
324
456
    order = 2;
325
1.19k
  else if(total_error_3 <= total_error_4)
326
910
    order = 3;
327
280
  else
328
280
    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
29.8k
  FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
334
29.8k
  FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
335
29.8k
  FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
336
29.8k
  FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
337
29.8k
  FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
338
29.8k
#ifndef FLAC__INTEGER_ONLY_LIBRARY
339
29.8k
  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
29.8k
  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
29.8k
  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
29.8k
  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
29.8k
  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
29.8k
  return order;
353
29.8k
}
354
355
#ifndef FLAC__INTEGER_ONLY_LIBRARY
356
2.04M
#define CHECK_ORDER_IS_VALID(macro_order)   \
357
4.09M
if(order_##macro_order##_is_valid && total_error_##macro_order < smallest_error) { \
358
508k
  order = macro_order;        \
359
508k
  smallest_error = total_error_##macro_order ; \
360
508k
  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
508k
}              \
362
2.04M
else              \
363
2.04M
  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
382k
{
382
382k
  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
382k
  FLAC__uint64 error_0, error_1, error_2, error_3, error_4;
384
382k
  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
382k
  uint32_t order = 0;
386
382k
  int i;
387
388
18.1M
  for(i = -4; i < (int)data_len; i++) {
389
17.7M
    error_0 = local_abs64((FLAC__int64)data[i]);
390
17.7M
    error_1 = (i > -4) ? local_abs64((FLAC__int64)data[i] - data[i-1]) : 0 ;
391
17.7M
    error_2 = (i > -3) ? local_abs64((FLAC__int64)data[i] - 2 * (FLAC__int64)data[i-1] + data[i-2]) : 0;
392
17.7M
    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
17.7M
    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
17.7M
    total_error_0 += error_0;
396
17.7M
    total_error_1 += error_1;
397
17.7M
    total_error_2 += error_2;
398
17.7M
    total_error_3 += error_3;
399
17.7M
    total_error_4 += error_4;
400
401
    /* residual must not be INT32_MIN because abs(INT32_MIN) is undefined */
402
17.7M
    if(error_0 > INT32_MAX)
403
2.57M
      order_0_is_valid = false;
404
17.7M
    if(error_1 > INT32_MAX)
405
76.1k
      order_1_is_valid = false;
406
17.7M
    if(error_2 > INT32_MAX)
407
305k
      order_2_is_valid = false;
408
17.7M
    if(error_3 > INT32_MAX)
409
731k
      order_3_is_valid = false;
410
17.7M
    if(error_4 > INT32_MAX)
411
1.07M
      order_4_is_valid = false;
412
17.7M
  }
413
414
382k
  CHECK_ORDER_IS_VALID(0);
415
382k
  CHECK_ORDER_IS_VALID(1);
416
382k
  CHECK_ORDER_IS_VALID(2);
417
382k
  CHECK_ORDER_IS_VALID(3);
418
382k
  CHECK_ORDER_IS_VALID(4);
419
420
382k
  return order;
421
382k
}
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
26.8k
{
429
26.8k
  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
26.8k
  FLAC__uint64 error_0, error_1, error_2, error_3, error_4;
431
26.8k
  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
26.8k
  uint32_t order = 0;
433
26.8k
  int i;
434
435
8.54M
  for(i = -4; i < (int)data_len; i++) {
436
8.52M
    error_0 = local_abs64(data[i]);
437
8.52M
    error_1 = (i > -4) ? local_abs64(data[i] - data[i-1]) : 0 ;
438
8.52M
    error_2 = (i > -3) ? local_abs64(data[i] - 2 * data[i-1] + data[i-2]) : 0;
439
8.52M
    error_3 = (i > -2) ? local_abs64(data[i] - 3 * data[i-1] + 3 * data[i-2] - data[i-3]) : 0;
440
8.52M
    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
8.52M
    total_error_0 += error_0;
443
8.52M
    total_error_1 += error_1;
444
8.52M
    total_error_2 += error_2;
445
8.52M
    total_error_3 += error_3;
446
8.52M
    total_error_4 += error_4;
447
448
    /* residual must not be INT32_MIN because abs(INT32_MIN) is undefined */
449
8.52M
    if(error_0 > INT32_MAX)
450
355k
      order_0_is_valid = false;
451
8.52M
    if(error_1 > INT32_MAX)
452
303k
      order_1_is_valid = false;
453
8.52M
    if(error_2 > INT32_MAX)
454
641k
      order_2_is_valid = false;
455
8.52M
    if(error_3 > INT32_MAX)
456
1.15M
      order_3_is_valid = false;
457
8.52M
    if(error_4 > INT32_MAX)
458
1.41M
      order_4_is_valid = false;
459
8.52M
  }
460
461
26.8k
  CHECK_ORDER_IS_VALID(0);
462
26.8k
  CHECK_ORDER_IS_VALID(1);
463
26.8k
  CHECK_ORDER_IS_VALID(2);
464
26.8k
  CHECK_ORDER_IS_VALID(3);
465
26.8k
  CHECK_ORDER_IS_VALID(4);
466
467
26.8k
  return order;
468
26.8k
}
469
470
void FLAC__fixed_compute_residual(const FLAC__int32 data[], uint32_t data_len, uint32_t order, FLAC__int32 residual[])
471
546k
{
472
546k
  const int idata_len = (int)data_len;
473
546k
  int i;
474
475
546k
  switch(order) {
476
341k
    case 0:
477
341k
      FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
478
341k
      memcpy(residual, data, sizeof(residual[0])*data_len);
479
341k
      break;
480
89.6k
    case 1:
481
16.3M
      for(i = 0; i < idata_len; i++)
482
16.2M
        residual[i] = data[i] - data[i-1];
483
89.6k
      break;
484
44.9k
    case 2:
485
7.51M
      for(i = 0; i < idata_len; i++)
486
7.47M
        residual[i] = data[i] - 2*data[i-1] + data[i-2];
487
44.9k
      break;
488
35.8k
    case 3:
489
6.83M
      for(i = 0; i < idata_len; i++)
490
6.79M
        residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3];
491
35.8k
      break;
492
34.0k
    case 4:
493
6.70M
      for(i = 0; i < idata_len; i++)
494
6.67M
        residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4];
495
34.0k
      break;
496
0
    default:
497
0
      FLAC__ASSERT(0);
498
546k
  }
499
546k
}
500
501
void FLAC__fixed_compute_residual_wide(const FLAC__int32 data[], uint32_t data_len, uint32_t order, FLAC__int32 residual[])
502
118k
{
503
118k
  const int idata_len = (int)data_len;
504
118k
  int i;
505
506
118k
  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
54.6k
    case 1:
512
12.7M
      for(i = 0; i < idata_len; i++)
513
12.7M
        residual[i] = (FLAC__int64)data[i] - data[i-1];
514
54.6k
      break;
515
19.5k
    case 2:
516
5.12M
      for(i = 0; i < idata_len; i++)
517
5.10M
        residual[i] = (FLAC__int64)data[i] - 2*(FLAC__int64)data[i-1] + data[i-2];
518
19.5k
      break;
519
23.3k
    case 3:
520
3.92M
      for(i = 0; i < idata_len; i++)
521
3.89M
        residual[i] = (FLAC__int64)data[i] - 3*(FLAC__int64)data[i-1] + 3*(FLAC__int64)data[i-2] - data[i-3];
522
23.3k
      break;
523
20.6k
    case 4:
524
3.11M
      for(i = 0; i < idata_len; i++)
525
3.09M
        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
20.6k
      break;
527
0
    default:
528
0
      FLAC__ASSERT(0);
529
118k
  }
530
118k
}
531
532
void FLAC__fixed_compute_residual_wide_33bit(const FLAC__int64 data[], uint32_t data_len, uint32_t order, FLAC__int32 residual[])
533
10.7k
{
534
10.7k
  const int idata_len = (int)data_len;
535
10.7k
  int i;
536
537
10.7k
  switch(order) {
538
3.56k
    case 0:
539
2.16M
      for(i = 0; i < idata_len; i++)
540
2.16M
        residual[i] = data[i];
541
3.56k
      break;
542
3.15k
    case 1:
543
814k
      for(i = 0; i < idata_len; i++)
544
811k
        residual[i] = data[i] - data[i-1];
545
3.15k
      break;
546
1.39k
    case 2:
547
442k
      for(i = 0; i < idata_len; i++)
548
441k
        residual[i] = data[i] - 2*data[i-1] + data[i-2];
549
1.39k
      break;
550
1.56k
    case 3:
551
102k
      for(i = 0; i < idata_len; i++)
552
101k
        residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3];
553
1.56k
      break;
554
1.05k
    case 4:
555
14.8k
      for(i = 0; i < idata_len; i++)
556
13.7k
        residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4];
557
1.05k
      break;
558
0
    default:
559
0
      FLAC__ASSERT(0);
560
10.7k
  }
561
10.7k
}
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
69.6k
{
573
69.6k
  int i, idata_len = (int)data_len;
574
575
69.6k
  switch(order) {
576
16.0k
    case 0:
577
16.0k
      FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
578
16.0k
      memcpy(data, residual, sizeof(residual[0])*data_len);
579
16.0k
      break;
580
42.0k
    case 1:
581
10.0M
      for(i = 0; i < idata_len; i++)
582
9.99M
        data[i] = residual[i] + data[i-1];
583
42.0k
      break;
584
7.89k
    case 2:
585
216k
      for(i = 0; i < idata_len; i++)
586
208k
        data[i] = residual[i] + 2*data[i-1] - data[i-2];
587
7.89k
      break;
588
605
    case 3:
589
109k
      for(i = 0; i < idata_len; i++)
590
108k
        data[i] = residual[i] + 3*data[i-1] - 3*data[i-2] + data[i-3];
591
605
      break;
592
3.08k
    case 4:
593
301k
      for(i = 0; i < idata_len; i++)
594
298k
        data[i] = residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4];
595
3.08k
      break;
596
0
    default:
597
0
      FLAC__ASSERT(0);
598
69.6k
  }
599
69.6k
}
600
601
void FLAC__fixed_restore_signal_wide(const FLAC__int32 residual[], uint32_t data_len, uint32_t order, FLAC__int32 data[])
602
14.3k
{
603
14.3k
  int i, idata_len = (int)data_len;
604
605
14.3k
  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
10.7k
    case 1:
611
2.13M
      for(i = 0; i < idata_len; i++)
612
2.12M
        data[i] = (FLAC__int64)residual[i] + (FLAC__int64)data[i-1];
613
10.7k
      break;
614
1.52k
    case 2:
615
24.5k
      for(i = 0; i < idata_len; i++)
616
23.0k
        data[i] = (FLAC__int64)residual[i] + 2*(FLAC__int64)data[i-1] - (FLAC__int64)data[i-2];
617
1.52k
      break;
618
1.47k
    case 3:
619
100k
      for(i = 0; i < idata_len; i++)
620
99.3k
        data[i] = (FLAC__int64)residual[i] + 3*(FLAC__int64)data[i-1] - 3*(FLAC__int64)data[i-2] + (FLAC__int64)data[i-3];
621
1.47k
      break;
622
603
    case 4:
623
7.05k
      for(i = 0; i < idata_len; i++)
624
6.45k
        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
603
      break;
626
0
    default:
627
0
      FLAC__ASSERT(0);
628
14.3k
  }
629
14.3k
}
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
4.93k
{
641
4.93k
  int i, idata_len = (int)data_len;
642
643
4.93k
  switch(order) {
644
1.90k
    case 0:
645
876k
      for(i = 0; i < idata_len; i++)
646
874k
        data[i] = residual[i];
647
1.90k
      break;
648
1.14k
    case 1:
649
196k
      for(i = 0; i < idata_len; i++)
650
194k
        data[i] = (FLAC__int64)residual[i] + data[i-1];
651
1.14k
      break;
652
762
    case 2:
653
12.1k
      for(i = 0; i < idata_len; i++)
654
11.3k
        data[i] = (FLAC__int64)residual[i] + 2*data[i-1] - data[i-2];
655
762
      break;
656
627
    case 3:
657
44.0k
      for(i = 0; i < idata_len; i++)
658
43.3k
        data[i] = (FLAC__int64)residual[i] + 3*data[i-1] - 3*data[i-2] + data[i-3];
659
627
      break;
660
492
    case 4:
661
8.73k
      for(i = 0; i < idata_len; i++)
662
8.24k
        data[i] = (FLAC__int64)residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4];
663
492
      break;
664
0
    default:
665
0
      FLAC__ASSERT(0);
666
4.93k
  }
667
4.93k
}