Coverage Report

Created: 2026-12-01 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/faac/libfaad/faad_internal.h
Line
Count
Source
1
/*
2
 * FAAD - Freeware Advanced Audio Decoder
3
 * Copyright (C) 2026 Nils Schimmelmann
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 */
15
16
#include "faad_symbols.h"
17
18
#ifndef FAAD_INTERNAL_H
19
#define FAAD_INTERNAL_H
20
21
#ifndef _USE_MATH_DEFINES
22
#define _USE_MATH_DEFINES
23
#endif
24
25
#include <stdint.h>
26
#include <stdbool.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include "build_defaults.h"
30
#include <math.h>
31
#include "bits.h"
32
#include <float.h>
33
#include "faac_endian.h"
34
35
#ifndef M_PI
36
#define M_PI 3.14159265358979323846
37
#endif
38
39
/* Memory management macros (overridable, e.g. for embedded PSRAM) */
40
#ifndef AllocMemory
41
33.1k
#define AllocMemory(size) malloc(size)
42
#endif
43
#ifndef FreeMemory
44
33.1k
#define FreeMemory(block) free(block)
45
#endif
46
47
#include "faad3.h"
48
#include "asc_codec.h"
49
_Static_assert(sizeof(bool) == 1, "public ABI requires one-byte flags");
50
_Static_assert(sizeof(float) == 4 && FLT_RADIX == 2 && FLT_MANT_DIG == 24,
51
               "PCM requires IEEE binary32 floats");
52
#include "faad_stats.h"
53
#include "sbr_tables.h"
54
typedef struct {
55
    uint32_t code : 24;
56
    uint32_t len : 8;
57
} SBRHuffEntry;
58
59
extern const float sbr_noise_table[512][2];
60
61
/* Parametric stereo codebooks (ISO/IEC 14496-3 §8.6). */
62
#define PS_HUFF_IID_DF_FINE_OFFSET 30
63
#define PS_HUFF_IID_DF_FINE_NSYMS  61
64
extern const SBRHuffEntry ps_huff_iid_df_fine[PS_HUFF_IID_DF_FINE_NSYMS];
65
#define PS_HUFF_IID_DT_FINE_OFFSET 30
66
#define PS_HUFF_IID_DT_FINE_NSYMS  61
67
extern const SBRHuffEntry ps_huff_iid_dt_fine[PS_HUFF_IID_DT_FINE_NSYMS];
68
#define PS_HUFF_IID_DF_OFFSET 14
69
#define PS_HUFF_IID_DF_NSYMS  29
70
extern const SBRHuffEntry ps_huff_iid_df[PS_HUFF_IID_DF_NSYMS];
71
#define PS_HUFF_IID_DT_OFFSET 14
72
#define PS_HUFF_IID_DT_NSYMS  29
73
extern const SBRHuffEntry ps_huff_iid_dt[PS_HUFF_IID_DT_NSYMS];
74
#define PS_HUFF_ICC_DF_OFFSET 7
75
#define PS_HUFF_ICC_DF_NSYMS  15
76
extern const SBRHuffEntry ps_huff_icc_df[PS_HUFF_ICC_DF_NSYMS];
77
#define PS_HUFF_ICC_DT_OFFSET 7
78
#define PS_HUFF_ICC_DT_NSYMS  15
79
extern const SBRHuffEntry ps_huff_icc_dt[PS_HUFF_ICC_DT_NSYMS];
80
#define PS_HUFF_IPD_DF_OFFSET 0
81
#define PS_HUFF_IPD_DF_NSYMS  8
82
extern const SBRHuffEntry ps_huff_ipd_df[PS_HUFF_IPD_DF_NSYMS];
83
#define PS_HUFF_IPD_DT_OFFSET 0
84
#define PS_HUFF_IPD_DT_NSYMS  8
85
extern const SBRHuffEntry ps_huff_ipd_dt[PS_HUFF_IPD_DT_NSYMS];
86
#define PS_HUFF_OPD_DF_OFFSET 0
87
#define PS_HUFF_OPD_DF_NSYMS  8
88
extern const SBRHuffEntry ps_huff_opd_df[PS_HUFF_OPD_DF_NSYMS];
89
#define PS_HUFF_OPD_DT_OFFSET 0
90
#define PS_HUFF_OPD_DT_NSYMS  8
91
extern const SBRHuffEntry ps_huff_opd_dt[PS_HUFF_OPD_DT_NSYMS];
92
93
/* Channel capacity: the build's -Dmax-channels (config.h) when present. Every
94
 * per-channel buffer, including the SBR and PS state, scales with it. */
95
#if (MAX_CHANNELS < 2 || defined(FAAD_DISABLE_SBR)) && !defined(FAAD_DISABLE_PS)
96
#define FAAD_DISABLE_PS /* parametric stereo needs SBR's QMF domain and two output channels */
97
#endif
98
312M
#define FRAME_LEN_LONG 1024
99
#ifdef FAAD_DISABLE_SBR
100
#define FRAME_SAMPLES_MAX 1024 /* no SBR doubling of the output rate */
101
#else
102
#define FRAME_SAMPLES_MAX 2048
103
#endif
104
#define FRAME_LEN_SHORT 128
105
#define NUM_WINDOWS 8
106
107
/* Index into the SFB tables for a sample rate (asc.c). */
108
int get_sr_index(uint32_t sample_rate);
109
110
1.25M
#define ID_SCE 0x0
111
273k
#define ID_CPE 0x1
112
81.5k
#define ID_CCE 0x2
113
151k
#define ID_LFE 0x3
114
62.3k
#define ID_DSE 0x4
115
46.5k
#define ID_PCE 0x5
116
36.4k
#define ID_FIL 0x6
117
539k
#define ID_END 0x7
118
119
918k
#define ONLY_LONG_SEQUENCE 0
120
5.02k
#define LONG_START_SEQUENCE 1
121
2.57M
#define EIGHT_SHORT_SEQUENCE 2
122
309k
#define LONG_STOP_SEQUENCE 3
123
124
#define SINE_WINDOW 0
125
1.29M
#define KBD_WINDOW  1
126
127
57.7k
#define SBR_EXTENSION_DATA 13
128
50.8k
#define SBR_EXTENSION_DATA_CRC 14
129
1.94k
#define PS_EXTENSION_DATA 2
130
131
#define SBR_PS_BANDS 20
132
#define SBR_PS_IID_LEVELS 15
133
#define SBR_PS_ICC_LEVELS 8
134
135
typedef struct {
136
    const uint8_t *buffer;
137
    uint32_t len;
138
    uint32_t byte_pos;
139
    uint32_t bit_pos;    /* 0..7 within the byte, MSB first */
140
    bool overread;
141
} BitReader;
142
143
void bits_init(BitReader *bs, const uint8_t *buffer, uint32_t len);
144
145
uint32_t bits_get(BitReader *bs, uint32_t nbits);
146
uint32_t bits_show(BitReader *bs, uint32_t nbits);
147
148
static inline uint32_t bits_get_1(BitReader *bs)
149
2.81M
{
150
2.81M
    if (bs->byte_pos < bs->len) {
151
695k
        uint32_t bit = (bs->buffer[bs->byte_pos] >> (7 - bs->bit_pos)) & 1U;
152
695k
        uint32_t next_bit = bs->bit_pos + 1;
153
695k
        bs->byte_pos += next_bit >> 3;
154
695k
        bs->bit_pos = next_bit & 7;
155
695k
        return bit;
156
695k
    }
157
2.11M
    bs->overread = true;
158
2.11M
    return 0;
159
2.81M
}
Unexecuted instantiation: decoder.c:bits_get_1
Unexecuted instantiation: bits.c:bits_get_1
Unexecuted instantiation: asc.c:bits_get_1
huffman.c:bits_get_1
Line
Count
Source
149
2.67M
{
150
2.67M
    if (bs->byte_pos < bs->len) {
151
616k
        uint32_t bit = (bs->buffer[bs->byte_pos] >> (7 - bs->bit_pos)) & 1U;
152
616k
        uint32_t next_bit = bs->bit_pos + 1;
153
616k
        bs->byte_pos += next_bit >> 3;
154
616k
        bs->bit_pos = next_bit & 7;
155
616k
        return bit;
156
616k
    }
157
2.05M
    bs->overread = true;
158
2.05M
    return 0;
159
2.67M
}
Unexecuted instantiation: syntax.c:bits_get_1
Unexecuted instantiation: dequant.c:bits_get_1
Unexecuted instantiation: stereo.c:bits_get_1
Unexecuted instantiation: tns.c:bits_get_1
Unexecuted instantiation: imdct.c:bits_get_1
sbr.c:bits_get_1
Line
Count
Source
149
142k
{
150
142k
    if (bs->byte_pos < bs->len) {
151
79.2k
        uint32_t bit = (bs->buffer[bs->byte_pos] >> (7 - bs->bit_pos)) & 1U;
152
79.2k
        uint32_t next_bit = bs->bit_pos + 1;
153
79.2k
        bs->byte_pos += next_bit >> 3;
154
79.2k
        bs->bit_pos = next_bit & 7;
155
79.2k
        return bit;
156
79.2k
    }
157
63.5k
    bs->overread = true;
158
63.5k
    return 0;
159
142k
}
Unexecuted instantiation: ps.c:bits_get_1
Unexecuted instantiation: coupling.c:bits_get_1
Unexecuted instantiation: sbr_dec_tables.c:bits_get_1
160
161
static inline uint32_t bits_get_fast(BitReader *bs, uint32_t nbits)
162
293k
{
163
293k
    if (nbits <= 24 && bs->byte_pos + 4 <= bs->len) {
164
82.9k
        const uint8_t *ptr = bs->buffer + bs->byte_pos;
165
82.9k
        uint32_t word = read_u32_be(ptr);
166
82.9k
        uint32_t val = (word >> (32 - bs->bit_pos - nbits)) & ((1U << nbits) - 1U);
167
82.9k
        uint32_t total_bits = bs->bit_pos + nbits;
168
82.9k
        bs->byte_pos += total_bits >> 3;
169
82.9k
        bs->bit_pos = total_bits & 7;
170
82.9k
        return val;
171
82.9k
    }
172
210k
    return bits_get(bs, nbits);
173
293k
}
Unexecuted instantiation: decoder.c:bits_get_fast
Unexecuted instantiation: bits.c:bits_get_fast
Unexecuted instantiation: asc.c:bits_get_fast
huffman.c:bits_get_fast
Line
Count
Source
162
293k
{
163
293k
    if (nbits <= 24 && bs->byte_pos + 4 <= bs->len) {
164
82.9k
        const uint8_t *ptr = bs->buffer + bs->byte_pos;
165
82.9k
        uint32_t word = read_u32_be(ptr);
166
82.9k
        uint32_t val = (word >> (32 - bs->bit_pos - nbits)) & ((1U << nbits) - 1U);
167
82.9k
        uint32_t total_bits = bs->bit_pos + nbits;
168
82.9k
        bs->byte_pos += total_bits >> 3;
169
82.9k
        bs->bit_pos = total_bits & 7;
170
82.9k
        return val;
171
82.9k
    }
172
210k
    return bits_get(bs, nbits);
173
293k
}
Unexecuted instantiation: syntax.c:bits_get_fast
Unexecuted instantiation: dequant.c:bits_get_fast
Unexecuted instantiation: stereo.c:bits_get_fast
Unexecuted instantiation: tns.c:bits_get_fast
Unexecuted instantiation: imdct.c:bits_get_fast
Unexecuted instantiation: sbr.c:bits_get_fast
Unexecuted instantiation: ps.c:bits_get_fast
Unexecuted instantiation: coupling.c:bits_get_fast
Unexecuted instantiation: sbr_dec_tables.c:bits_get_fast
174
175
static inline uint32_t bits_show_fast(BitReader *bs, uint32_t nbits)
176
6.94M
{
177
6.94M
    if (nbits > 0 && nbits <= 24 && bs->byte_pos + 4 <= bs->len) {
178
2.12M
        const uint8_t *ptr = bs->buffer + bs->byte_pos;
179
2.12M
        uint32_t word = read_u32_be(ptr);
180
2.12M
        return (word << bs->bit_pos) >> (32 - nbits);
181
2.12M
    }
182
4.81M
    return bits_show(bs, nbits);
183
6.94M
}
Unexecuted instantiation: decoder.c:bits_show_fast
Unexecuted instantiation: bits.c:bits_show_fast
Unexecuted instantiation: asc.c:bits_show_fast
huffman.c:bits_show_fast
Line
Count
Source
176
6.94M
{
177
6.94M
    if (nbits > 0 && nbits <= 24 && bs->byte_pos + 4 <= bs->len) {
178
2.12M
        const uint8_t *ptr = bs->buffer + bs->byte_pos;
179
2.12M
        uint32_t word = read_u32_be(ptr);
180
2.12M
        return (word << bs->bit_pos) >> (32 - nbits);
181
2.12M
    }
182
4.81M
    return bits_show(bs, nbits);
183
6.94M
}
Unexecuted instantiation: syntax.c:bits_show_fast
Unexecuted instantiation: dequant.c:bits_show_fast
Unexecuted instantiation: stereo.c:bits_show_fast
Unexecuted instantiation: tns.c:bits_show_fast
Unexecuted instantiation: imdct.c:bits_show_fast
Unexecuted instantiation: sbr.c:bits_show_fast
Unexecuted instantiation: ps.c:bits_show_fast
Unexecuted instantiation: coupling.c:bits_show_fast
Unexecuted instantiation: sbr_dec_tables.c:bits_show_fast
184
void bits_skip(BitReader *bs, uint32_t nbits);
185
void bits_byte_align(BitReader *bs);
186
uint32_t bits_get_consumed(BitReader *bs);
187
188
typedef struct {
189
    enum faad_object_type object_type;
190
    uint32_t sample_rate;
191
    uint32_t num_channels;
192
    AscPce pce;
193
    bool is_sbr;
194
    bool is_ps;
195
    uint32_t sbr_sample_rate;
196
    bool frame_length_flag; /* false = 1024, true = 960 */
197
} AudioSpecificConfig;
198
199
faad_status asc_decode(BitReader *bs, AudioSpecificConfig *asc);
200
faad_status adts_decode_header(BitReader *bs, AudioSpecificConfig *asc, uint32_t *frame_length);
201
202
4.72M
#define MAX_SFB       52 /* long blocks reach 51 bands, short ones 8 x 15 */
203
15.3k
#define TNS_MAX_ORDER 12 /* AAC-LC: 12 for long blocks, 7 for short */
204
205
typedef struct {
206
    uint16_t frame_len_long; /* 1024 or 960 */
207
    uint8_t window_sequence;
208
    uint8_t window_shape;
209
    uint8_t max_sfb;
210
    uint8_t num_window_groups;
211
    uint8_t window_group_length[NUM_WINDOWS];
212
    uint8_t num_windows;
213
    uint16_t sfb_offsets[68];
214
    uint8_t num_sfbs;
215
    int8_t sample_rate_index; /* core rate index, for TNS_MAX_BANDS */
216
    /* per window group and scalefactor band: the codebook (0 zero, 13 noise,
217
     * 14/15 intensity) and the scalefactor, noise energy or intensity position */
218
    uint8_t sfb_cb[8][MAX_SFB];
219
    int16_t scalefactors[8][MAX_SFB];
220
    uint8_t global_gain;
221
222
    /* Pulse data: up to four spectral lines of a long window whose
223
     * quantized magnitude is raised by pulse_amp */
224
    bool pulse_data_present;
225
    uint8_t pulse_count;
226
    uint8_t pulse_amp[4];
227
    uint16_t pulse_pos[4];
228
229
    bool tns_data_present;
230
    uint8_t tns_n_filt[8];
231
    uint8_t tns_length[8][4];
232
    uint8_t tns_order[8][4];
233
    uint8_t tns_direction[8][4];
234
    uint8_t tns_coef_res[8];
235
    int8_t  tns_coef[8][4][TNS_MAX_ORDER];
236
237
    bool gain_control_present;
238
} ICSInfo;
239
240
typedef enum {
241
    MS_MASK_NONE = 0,
242
    MS_MASK_EXPLICIT = 1,
243
    MS_MASK_ALL = 2,
244
    MS_MASK_RESERVED = 3
245
} MSMaskMode;
246
247
typedef struct {
248
    bool common_window;
249
    MSMaskMode ms_mask_present;
250
    uint8_t ms_used[8][MAX_SFB];
251
    ICSInfo ics[2];
252
} CPEInfo;
253
254
255
/* Parametric stereo (ISO/IEC 14496-3 §8.6) */
256
2.27k
#define PS_MAX_ENV      5   /* four coded envelopes plus the implicit trailing one */
257
#define PS_NR_PAR       34
258
#define PS_NR_BANDS     91  /* hybrid sub-bands in the 34-parameter layout */
259
/* Without DAB+ support the frame length is a constant, so the 1024-sample
260
 * paths fold at compile time instead of branching on the stream. */
261
#ifdef FAAD_DISABLE_DAB
262
#define FRAME_IS_960(dec) 0
263
#define ICS_IS_960(ics) 0
264
#else
265
1.46M
#define FRAME_IS_960(dec) ((dec)->asc.frame_length_flag)
266
1.71M
#define ICS_IS_960(ics) ((ics)->frame_len_long == 960)
267
#endif
268
#define PS_QMF_SLOTS    32
269
0
#define PS_IN_SLOTS     38  /* QMF slots handed to the hybrid bank: 32 plus 6 of look-ahead */
270
36.8M
#define PS_MAX_DELAY    14
271
#define PS_NR_ALLPASS   50  /* all-pass bands in the 34-parameter layout (30 in the 20) */
272
#define PS_AP_STATE     12  /* link delays 3 + 4 + 5 */
273
274
typedef struct {
275
    bool    start;          /* a header has been seen */
276
    bool    enable_iid, enable_icc, enable_ext, enable_ipdopd;
277
    bool    iid_quant;      /* fine (31-step) IID quantization */
278
    uint8_t icc_mode;
279
    uint8_t nr_iid_par, nr_icc_par, nr_ipdopd_par;
280
    uint8_t frame_class, num_env, num_env_old;
281
    int8_t  border[PS_MAX_ENV + 1];
282
    int8_t  iid_par[PS_MAX_ENV][PS_NR_PAR];
283
    int8_t  icc_par[PS_MAX_ENV][PS_NR_PAR];
284
    int8_t  ipd_par[PS_MAX_ENV][PS_NR_PAR];
285
    int8_t  opd_par[PS_MAX_ENV][PS_NR_PAR];
286
    bool    is34, is34_old;
287
288
    float   in_buf[5][PS_IN_SLOTS + 6][2];                  /* hybrid analysis history */
289
    /* Decorrelator history, only what a frame leaves for the next: the
290
     * two-slot input delay and the 3+4+5 link states of each all-pass
291
     * band, the 14-slot line of each delay band (indexed from the first
292
     * delay band). */
293
    float   dc_in[PS_NR_ALLPASS][2][2];
294
    float   dc_ap[PS_NR_ALLPASS][PS_AP_STATE][2];
295
    float   dc_delay[PS_NR_BANDS - 30][PS_MAX_DELAY][2];
296
    uint8_t ap_pos[3];        /* ring positions of the three link states */
297
    uint8_t dl_pos;           /* ring position of the delay lines */
298
    float   peak_decay_nrg[PS_NR_PAR], power_smooth[PS_NR_PAR], peak_decay_diff_smooth[PS_NR_PAR];
299
    float   H[4][2][PS_MAX_ENV + 1][PS_NR_PAR];             /* mixing matrix per envelope border */
300
    int8_t  ipd_hist[17], opd_hist[17];                     /* two previous indices, packed */
301
} PSState;
302
303
/* Canonical SBR books need only symbol order and length boundaries; PS
304
 * books retain explicit codewords because some are not canonical. */
305
enum {
306
    HB_T_ENV_15, HB_F_ENV_15, HB_T_ENV_BAL_15, HB_F_ENV_BAL_15,
307
    HB_T_ENV_30, HB_F_ENV_30, HB_T_ENV_BAL_30, HB_F_ENV_BAL_30,
308
    HB_T_NOISE_30, HB_T_NOISE_BAL_30,
309
#ifndef FAAD_DISABLE_PS
310
    HB_PS_IID_DF_FINE, HB_PS_IID_DT_FINE, HB_PS_IID_DF, HB_PS_IID_DT,
311
    HB_PS_ICC_DF, HB_PS_ICC_DT, HB_PS_IPD_DF, HB_PS_IPD_DT, HB_PS_OPD_DF, HB_PS_OPD_DT,
312
#endif
313
    HB_COUNT
314
};
315
142k
#define HB_MAX_LEN 20
316
typedef struct {
317
    const SBRHuffEntry *tab; /* NULL for canonical codebooks */
318
    const uint8_t *order;
319
    int8_t offset;
320
    uint8_t first[HB_MAX_LEN + 2];
321
} SBRHuffBook;
322
extern const SBRHuffBook sbr_books[HB_COUNT];
323
int  sbr_huff_decode(BitReader *bs, const SBRHuffBook *book);
324
325
/* SBR (ISO/IEC 14496-3 §4.6.18) */
326
3.94M
#define SBR_SLOTS        32  /* QMF time slots per frame: numTimeSlots (16) * RATE (2) */
327
259M
#define SBR_T_HFGEN      8   /* slots of the previous frame kept for the covariance and X_low */
328
12.6M
#define SBR_T_HFADJ      2   /* offset of the envelope-adjusted region within the buffer */
329
#define SBR_HF_TAIL_SLOTS (SBR_T_HFGEN - SBR_T_HFADJ)
330
3.94M
#define SBR_BUF_SLOTS    (SBR_SLOTS + SBR_T_HFGEN)
331
8.76M
#define SBR_MAX_BANDS    64
332
14.5k
#define SBR_MAX_ENV      5
333
12.8k
#define SBR_MAX_NQ       5
334
14.2k
#define SBR_MAX_PATCHES  6
335
#define SBR_MAX_LIM      (SBR_MAX_BANDS + SBR_MAX_PATCHES + 2)
336
337
typedef struct {
338
    uint8_t frame_class, L_E, L_Q, bs_pointer;
339
    int8_t  l_A;
340
    uint8_t t_E[SBR_MAX_ENV + 1], t_Q[3], freq_res[SBR_MAX_ENV];
341
    uint8_t df_env[SBR_MAX_ENV], df_noise[2];
342
    uint8_t invf_mode[SBR_MAX_NQ], invf_mode_prev[SBR_MAX_NQ];
343
    bool    add_harmonic_flag;
344
    uint8_t add_harmonic[SBR_MAX_BANDS];
345
    int16_t E[SBR_MAX_ENV][SBR_MAX_BANDS];
346
    int16_t Q[2][SBR_MAX_NQ];
347
    bool    amp_res; /* this frame's resolution (a single FIXFIX envelope forces 1.5 dB) */
348
349
    int16_t E_prev[SBR_MAX_BANDS];
350
    int16_t Q_prev[SBR_MAX_NQ];
351
    uint8_t freq_res_prev;
352
    /* Synthesis history is committed only after a complete fill. */
353
    float   bw_array[SBR_MAX_NQ];
354
    float   unit_energy; /* the spec's unit energy, scaled with the envelope when DRC is applied */
355
    float   g_hist[4][SBR_MAX_BANDS]; /* gains of the four previous envelopes, a ring */
356
    float   q_hist[4][SBR_MAX_BANDS];
357
    uint8_t hist_pos;                 /* oldest entry of the ring */
358
    uint8_t s_index_prev[SBR_MAX_BANDS];
359
    int8_t  l_A_prev;
360
    uint8_t L_E_prev;
361
    uint8_t t_E_end_prev; /* RATE * t_E(L_E) of the previous frame */
362
    uint8_t kx_prev, M_prev;
363
    uint16_t index_noise;
364
    uint8_t  index_sine;
365
    bool    have_frame;   /* a payload has been decoded since the last reset */
366
    uint8_t el_ch;        /* first channel of the element that filled it last */
367
    bool    primed;       /* smoothing history holds real gains */
368
    float   x_low_tail[32][SBR_T_HFGEN][2];
369
    float   y_tail[SBR_MAX_BANDS][SBR_HF_TAIL_SLOTS][2];
370
    float   qmf_x[320];  /* analysis delay line, newest sample first, ring */
371
    uint16_t qmf_x_pos;  /* start of the newest block in qmf_x */
372
    float   qmf_v[1280]; /* synthesis delay line, ring of 128-sample blocks */
373
    uint16_t qmf_v_pos;  /* start of the newest block in qmf_v */
374
} SBRChannel;
375
376
/* Header and frequency tables, shared by the channels of one element and
377
 * stored at the element's first channel. */
378
typedef struct {
379
    bool header_present;
380
    bool coupling;
381
    uint8_t nch;
382
    bool amp_res;
383
    uint8_t start_freq, stop_freq, xover_band, freq_scale, alter_scale, noise_bands;
384
    uint8_t limiter_bands, limiter_gains, interpol_freq, smoothing_mode;
385
    uint8_t k0, k2, kx, M;
386
    uint8_t n_master, n_high, n_low, n_q, n_lim, num_patches;
387
    uint8_t f_master[SBR_MAX_BANDS + 1], f_high[SBR_MAX_BANDS + 1], f_low[SBR_MAX_BANDS + 1];
388
    uint8_t f_noise[SBR_MAX_NQ + 1], f_lim[SBR_MAX_LIM + 1];
389
    uint8_t patch_start[SBR_MAX_PATCHES], patch_num[SBR_MAX_PATCHES];
390
} SBRElement;
391
392
/* Per-frame working buffers, one channel at a time. */
393
typedef struct {
394
    float (*x_low)[SBR_BUF_SLOTS][2]; /* low bands of the shared QMF workspace */
395
    float (*y)[SBR_BUF_SLOTS][2]; /* shared QMF workspace: generated HF replaces unused low bands */
396
} SBRScratch;
397
398
#if MAX_CHANNELS >= 3
399
/* CCE state is allocated by tag only when a stream uses coupling. Gains
400
 * follow the CCE's window groups, at most 8 * 15 short-window bands. */
401
typedef struct CouplingChannel {
402
    struct CouplingChannel *next;
403
    uint8_t tag, point, targets;
404
    bool present;
405
    struct { uint8_t tag, pair, select, gain, ch; } target[8];
406
    ICSInfo ics;
407
    float gain[16][128];
408
    float spec[FRAME_LEN_LONG];
409
    float *overlap;
410
    uint8_t prev_shape;
411
} CouplingChannel;
412
413
typedef struct {
414
    BitReader bits;
415
    uint32_t seed;
416
    uint8_t type, tag, ch;
417
} CouplingTarget;
418
419
#endif
420
421
/* Working memory of one frame's decode phases, which never overlap: the
422
 * element being parsed, the IMDCT
423
 * temporaries, then the SBR/PS buffers. */
424
typedef union {
425
    CPEInfo cpe;
426
    float work[2 * FRAME_LEN_LONG];
427
} FrameScratch;
428
#if !defined(FAAD_DISABLE_SBR) && MAX_CHANNELS >= 2
429
_Static_assert(sizeof(FrameScratch) <= sizeof(float[MAX_CHANNELS * FRAME_LEN_LONG]),
430
               "core scratch must fit in the second half of PCM storage");
431
#endif
432
433
/* Where the channels of a stream sit: built from the channel configuration,
434
 * from a PCE seen before the first PCM, or from the element sequence of a
435
 * stream that has neither. The output is ordered by WAVE speaker position. */
436
struct faad_layout {
437
    uint16_t mask;              /* speaker mask, 0 while the positions are unknown */
438
    uint8_t cfg, n, lfe;        /* configuration built for, channels, native LFE (0xff none) */
439
    uint8_t n_el;               /* PCE only: elements bound to slots by tag */
440
    uint8_t frozen;             /* PCM has been emitted in this layout; a PCE no longer changes it */
441
    uint8_t el_key[8], el_slot[8];
442
    uint8_t map[MAX_CHANNELS];  /* native index of each output channel */
443
};
444
445
struct faad_decoder {
446
#ifndef FAAD_DISABLE_DRC
447
    struct {
448
        bool present, ref_present, applied, boost;
449
        uint8_t ref, bands, top[16], ctl[16], sign[16];
450
        bool excluded[MAX_CHANNELS];
451
        float previous_norm, sbr_gain[MAX_CHANNELS];
452
    } drc;
453
#endif
454
    faad_config config;
455
    AudioSpecificConfig asc;
456
    bool asc_parsed;
457
    size_t allocated_bytes, largest_allocation;
458
    bool format_known;
459
    bool pcm_emitted;
460
#if MAX_CHANNELS >= 3
461
    struct faad_layout layout;
462
#endif
463
    /* Parsing and synthesis may advance on rejected packets; only emitted PCM
464
     * may replace the output format that callers use. */
465
    struct {
466
        uint32_t sample_rate, channels, frame_samples, channel_mask, decoder_delay;
467
    } emitted_format;
468
    bool ps_seen;
469
    bool spectral_downmix_active;
470
    uint32_t spectral_downmix_channels;
471
    uint32_t spectral_downmix_layout;
472
    uint32_t core_channels;
473
    uint32_t max_output_bytes;
474
475
    uint32_t frame_samples; /* 1024 or 2048 */
476
    uint32_t num_channels;
477
    uint32_t sample_rate;      /* nominal (post-SBR) rate, for reporting */
478
    uint32_t core_sample_rate; /* window/sfb layout rate: half of sample_rate with SBR */
479
480
    float (*spec)[FRAME_LEN_LONG];
481
    float (*overlap)[FRAME_LEN_LONG];
482
    uint8_t prev_window_shape[MAX_CHANNELS]; /* the left window half follows the previous block's shape */
483
    uint8_t prev_window_seq[MAX_CHANNELS];   /* what a concealed frame continues from */
484
485
#ifndef FAAD_DISABLE_SBR
486
    SBRChannel *sbr;
487
    SBRElement *sbr_el;
488
#endif
489
    bool sbr_present;
490
    bool sbr_seen; /* an SBR payload has appeared: frames without one still run at the SBR rate */
491
492
#ifndef FAAD_DISABLE_PS
493
    PSState *ps;
494
#endif
495
    bool ps_present;
496
497
    uint32_t pns_seed;
498
    uint32_t consecutive_errors;
499
    float (*prev_spec)[FRAME_LEN_LONG];
500
501
#if MAX_CHANNELS >= 3
502
    CouplingChannel *coupling;
503
#endif
504
    FrameScratch *scratch;
505
#ifndef FAAD_DISABLE_SBR
506
    SBRScratch sbr_scratch;
507
#endif
508
    uint8_t win_seq[MAX_CHANNELS];   /* this frame's window of each decoded channel, for the IMDCT */
509
    uint8_t win_shape[MAX_CHANNELS];
510
#ifndef FAAD_DISABLE_SBR
511
    /* The element layout of the last frame decoded without error: the
512
     * channel count of the element starting at each channel, 0 elsewhere. */
513
    uint8_t elem_nch[MAX_CHANNELS];
514
#endif
515
516
    float *pcm; /* core output, then SBR output in place */
517
};
518
519
#if MAX_CHANNELS >= 3
520
void faad_layout_update(struct faad_decoder *dec);
521
void faad_layout_infer(struct faad_decoder *dec, uint32_t seq);
522
186k
static inline void faad_layout_freeze(struct faad_decoder *dec) { dec->layout.frozen = 1; }
decoder.c:faad_layout_freeze
Line
Count
Source
522
186k
static inline void faad_layout_freeze(struct faad_decoder *dec) { dec->layout.frozen = 1; }
Unexecuted instantiation: bits.c:faad_layout_freeze
Unexecuted instantiation: asc.c:faad_layout_freeze
Unexecuted instantiation: huffman.c:faad_layout_freeze
Unexecuted instantiation: syntax.c:faad_layout_freeze
Unexecuted instantiation: dequant.c:faad_layout_freeze
Unexecuted instantiation: stereo.c:faad_layout_freeze
Unexecuted instantiation: tns.c:faad_layout_freeze
Unexecuted instantiation: imdct.c:faad_layout_freeze
Unexecuted instantiation: sbr.c:faad_layout_freeze
Unexecuted instantiation: ps.c:faad_layout_freeze
Unexecuted instantiation: coupling.c:faad_layout_freeze
Unexecuted instantiation: sbr_dec_tables.c:faad_layout_freeze
523
34.6k
static inline bool faad_layout_frozen(const struct faad_decoder *dec) { return dec->layout.frozen; }
decoder.c:faad_layout_frozen
Line
Count
Source
523
34.6k
static inline bool faad_layout_frozen(const struct faad_decoder *dec) { return dec->layout.frozen; }
Unexecuted instantiation: bits.c:faad_layout_frozen
Unexecuted instantiation: asc.c:faad_layout_frozen
Unexecuted instantiation: huffman.c:faad_layout_frozen
Unexecuted instantiation: syntax.c:faad_layout_frozen
Unexecuted instantiation: dequant.c:faad_layout_frozen
Unexecuted instantiation: stereo.c:faad_layout_frozen
Unexecuted instantiation: tns.c:faad_layout_frozen
Unexecuted instantiation: imdct.c:faad_layout_frozen
Unexecuted instantiation: sbr.c:faad_layout_frozen
Unexecuted instantiation: ps.c:faad_layout_frozen
Unexecuted instantiation: coupling.c:faad_layout_frozen
Unexecuted instantiation: sbr_dec_tables.c:faad_layout_frozen
524
/* NULL keeps the element order. */
525
static inline const uint8_t *faad_layout_map(const struct faad_decoder *dec, uint32_t num_chs)
526
187k
{
527
187k
    return dec->layout.mask && dec->layout.n == num_chs ? dec->layout.map : NULL;
528
187k
}
decoder.c:faad_layout_map
Line
Count
Source
526
187k
{
527
187k
    return dec->layout.mask && dec->layout.n == num_chs ? dec->layout.map : NULL;
528
187k
}
Unexecuted instantiation: bits.c:faad_layout_map
Unexecuted instantiation: asc.c:faad_layout_map
Unexecuted instantiation: huffman.c:faad_layout_map
Unexecuted instantiation: syntax.c:faad_layout_map
Unexecuted instantiation: dequant.c:faad_layout_map
Unexecuted instantiation: stereo.c:faad_layout_map
Unexecuted instantiation: tns.c:faad_layout_map
Unexecuted instantiation: imdct.c:faad_layout_map
Unexecuted instantiation: sbr.c:faad_layout_map
Unexecuted instantiation: ps.c:faad_layout_map
Unexecuted instantiation: coupling.c:faad_layout_map
Unexecuted instantiation: sbr_dec_tables.c:faad_layout_map
529
static inline uint32_t faad_layout_mask(const struct faad_decoder *dec, uint32_t num_chs)
530
196k
{
531
196k
    return dec->layout.n == num_chs ? dec->layout.mask : 0;
532
196k
}
decoder.c:faad_layout_mask
Line
Count
Source
530
196k
{
531
196k
    return dec->layout.n == num_chs ? dec->layout.mask : 0;
532
196k
}
Unexecuted instantiation: bits.c:faad_layout_mask
Unexecuted instantiation: asc.c:faad_layout_mask
Unexecuted instantiation: huffman.c:faad_layout_mask
Unexecuted instantiation: syntax.c:faad_layout_mask
Unexecuted instantiation: dequant.c:faad_layout_mask
Unexecuted instantiation: stereo.c:faad_layout_mask
Unexecuted instantiation: tns.c:faad_layout_mask
Unexecuted instantiation: imdct.c:faad_layout_mask
Unexecuted instantiation: sbr.c:faad_layout_mask
Unexecuted instantiation: ps.c:faad_layout_mask
Unexecuted instantiation: coupling.c:faad_layout_mask
Unexecuted instantiation: sbr_dec_tables.c:faad_layout_mask
533
#else
534
static inline void faad_layout_update(struct faad_decoder *dec) { (void)dec; }
535
static inline void faad_layout_infer(struct faad_decoder *dec, uint32_t seq) { (void)dec; (void)seq; }
536
static inline void faad_layout_freeze(struct faad_decoder *dec) { (void)dec; }
537
static inline bool faad_layout_frozen(const struct faad_decoder *dec) { (void)dec; return true; }
538
static inline const uint8_t *faad_layout_map(const struct faad_decoder *dec, uint32_t n) { (void)dec; (void)n; return NULL; }
539
static inline uint32_t faad_layout_mask(const struct faad_decoder *dec, uint32_t n) { (void)dec; (void)n; return 0; }
540
#endif
541
542
static inline uint32_t faad_downmix_lfe_channel(const struct faad_decoder *dec, uint32_t num_chs)
543
267k
{
544
267k
    if (dec->config.stream_format != FAAD_STREAM_RAW || !dec->asc_parsed
545
267k
        || dec->config.downmix_mode == FAAD_DOWNMIX_NONE
546
0
        || (dec->config.downmix_mode & 8u) /* the target keeps the LFE */
547
0
        || faac_popcount32((uint32_t)dec->config.downmix_mode) >= num_chs) /* nothing is mixed */
548
267k
        return UINT32_MAX;
549
0
#if MAX_CHANNELS >= 3
550
0
    if (dec->layout.n == num_chs && dec->layout.lfe != 0xff) return dec->layout.lfe;
551
#else
552
    (void)num_chs;
553
#endif
554
0
    return UINT32_MAX;
555
0
}
decoder.c:faad_downmix_lfe_channel
Line
Count
Source
543
187k
{
544
187k
    if (dec->config.stream_format != FAAD_STREAM_RAW || !dec->asc_parsed
545
187k
        || dec->config.downmix_mode == FAAD_DOWNMIX_NONE
546
0
        || (dec->config.downmix_mode & 8u) /* the target keeps the LFE */
547
0
        || faac_popcount32((uint32_t)dec->config.downmix_mode) >= num_chs) /* nothing is mixed */
548
187k
        return UINT32_MAX;
549
0
#if MAX_CHANNELS >= 3
550
0
    if (dec->layout.n == num_chs && dec->layout.lfe != 0xff) return dec->layout.lfe;
551
#else
552
    (void)num_chs;
553
#endif
554
0
    return UINT32_MAX;
555
0
}
Unexecuted instantiation: bits.c:faad_downmix_lfe_channel
Unexecuted instantiation: asc.c:faad_downmix_lfe_channel
Unexecuted instantiation: huffman.c:faad_downmix_lfe_channel
Unexecuted instantiation: syntax.c:faad_downmix_lfe_channel
Unexecuted instantiation: dequant.c:faad_downmix_lfe_channel
Unexecuted instantiation: stereo.c:faad_downmix_lfe_channel
Unexecuted instantiation: tns.c:faad_downmix_lfe_channel
Unexecuted instantiation: imdct.c:faad_downmix_lfe_channel
sbr.c:faad_downmix_lfe_channel
Line
Count
Source
543
80.1k
{
544
80.1k
    if (dec->config.stream_format != FAAD_STREAM_RAW || !dec->asc_parsed
545
80.1k
        || dec->config.downmix_mode == FAAD_DOWNMIX_NONE
546
0
        || (dec->config.downmix_mode & 8u) /* the target keeps the LFE */
547
0
        || faac_popcount32((uint32_t)dec->config.downmix_mode) >= num_chs) /* nothing is mixed */
548
80.1k
        return UINT32_MAX;
549
0
#if MAX_CHANNELS >= 3
550
0
    if (dec->layout.n == num_chs && dec->layout.lfe != 0xff) return dec->layout.lfe;
551
#else
552
    (void)num_chs;
553
#endif
554
0
    return UINT32_MAX;
555
0
}
Unexecuted instantiation: ps.c:faad_downmix_lfe_channel
Unexecuted instantiation: coupling.c:faad_downmix_lfe_channel
Unexecuted instantiation: sbr_dec_tables.c:faad_downmix_lfe_channel
556
557
/* Core-rate QMF delay; public metadata converts it to output samples. */
558
79.6k
#define FAAD_SBR_CORE_DELAY 481u
559
void setup_sfb_offsets(ICSInfo *ics, uint32_t sample_rate);
560
faad_status decode_scale_factor_data(BitReader *bs, ICSInfo *ics, uint32_t sample_rate);
561
faad_status decode_spectral_data(BitReader *bs, ICSInfo *ics, float *spec);
562
void apply_pns(ICSInfo *ics, float *spec, uint32_t *pns_seed);
563
void apply_ms_stereo(CPEInfo *cpe, float *spec_l, float *spec_r);
564
void apply_is_stereo(CPEInfo *cpe, float *spec_l, float *spec_r);
565
void apply_tns(ICSInfo *ics, float *spec);
566
void imdct_and_window(struct faad_decoder *dec, uint32_t ch, uint8_t window_sequence, uint8_t window_shape, float *spec, float *out_pcm);
567
568
faad_status decode_pce(BitReader *bs, struct faad_decoder *dec);
569
#if MAX_CHANNELS >= 3
570
faad_status decode_cce(BitReader *bs, struct faad_decoder *dec);
571
int decode_coupling_gain(BitReader *bs);
572
faad_status coupling_spectrum(struct faad_decoder *dec, CouplingTarget *targets, unsigned count);
573
void coupling_pcm(struct faad_decoder *dec);
574
#endif
575
void imdct_window(struct faad_decoder *dec, uint8_t sequence, uint8_t shape,
576
                  uint8_t previous_shape, float *overlap, float *spec, float *pcm);
577
faad_status decode_dse(BitReader *bs);
578
faad_status decode_ics(BitReader *bs, struct faad_decoder *dec, ICSInfo *ics, float *spec, bool common_window);
579
faad_status decode_cpe(BitReader *bs, struct faad_decoder *dec, CPEInfo *cpe, uint32_t ch);
580
faad_status decode_sce(BitReader *bs, struct faad_decoder *dec, ICSInfo *ics, uint32_t ch);
581
582
void faad_init_global_tables(void);
583
void sbr_init_tables(void);
584
faad_status sbr_decode_extension(struct faad_decoder *dec, BitReader *bs, uint32_t ch0, uint32_t syntax_id, bool crc);
585
void ps_read_data(struct faad_decoder *dec, BitReader *bs, uint32_t bits_left);
586
void ps_frame_input(struct faad_decoder *dec, int n, float X[64][2]);
587
void ps_frame_begin(struct faad_decoder *dec, int top);
588
void ps_slot(struct faad_decoder *dec, int n, float X[64][2], float L[64][2], float R[64][2]);
589
void init_ps_tables(void);
590
void sbr_apply(struct faad_decoder *dec, uint32_t num_ch, float *pcm);
591
#ifndef FAAD_DISABLE_SBR
592
faad_status faad_ensure_sbr(struct faad_decoder *dec);
593
#ifndef FAAD_DISABLE_PS
594
faad_status faad_ensure_ps(struct faad_decoder *dec);
595
#endif
596
#endif
597
#ifdef FAAD_STATS
598
FILE *faad_dump_file(void);
599
#endif
600
601
#endif /* FAAD_INTERNAL_H */