Coverage Report

Created: 2026-12-01 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/external/faac/libfaad/syntax.c
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_internal.h"
17
18
static void decode_ics_info(BitReader *bs, ICSInfo *ics)
19
15.9k
{
20
15.9k
    bits_skip(bs, 1);
21
15.9k
    ics->window_sequence = bits_get(bs, 2);
22
15.9k
    ics->window_shape = bits_get(bs, 1);
23
24
15.9k
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
25
5.98k
        ics->max_sfb = bits_get(bs, 4);
26
5.98k
        if (ics->max_sfb > 64) ics->max_sfb = 64;
27
5.98k
        uint32_t scale_factor_grouping = bits_get(bs, 7);
28
29
5.98k
        ics->num_window_groups = 1;
30
5.98k
        ics->window_group_length[0] = 1;
31
47.9k
        for (int i = 0; i < 7; i++) {
32
41.9k
            if ((scale_factor_grouping >> (6 - i)) & 1) {
33
8.55k
                ics->window_group_length[ics->num_window_groups - 1]++;
34
33.3k
            } else {
35
33.3k
                if (ics->num_window_groups < 8) {
36
33.3k
                    ics->num_window_groups++;
37
33.3k
                    ics->window_group_length[ics->num_window_groups - 1] = 1;
38
33.3k
                }
39
33.3k
            }
40
41.9k
        }
41
5.98k
        ics->num_windows = 8;
42
9.94k
    } else {
43
9.94k
        ics->max_sfb = bits_get(bs, 6);
44
9.94k
        if (ics->max_sfb > 64) ics->max_sfb = 64;
45
9.94k
        ics->num_window_groups = 1;
46
9.94k
        ics->window_group_length[0] = 1;
47
9.94k
        ics->num_windows = 1;
48
49
9.94k
        if (bits_get(bs, 1)) {
50
2.35k
            bits_skip(bs, 1);
51
2.35k
        }
52
9.94k
    }
53
15.9k
}
54
55
static faad_status decode_section_data(BitReader *bs, ICSInfo *ics)
56
16.7k
{
57
16.7k
    uint32_t sect_bits = (ics->window_sequence == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
58
67.6k
    for (int g = 0; g < ics->num_window_groups && g < 8; g++) {
59
51.0k
        int k = 0;
60
51.0k
        int i = 0;
61
81.4k
        while (k < ics->max_sfb && i < MAX_SFB) {
62
30.5k
            uint32_t cb = bits_get(bs, 4);
63
30.5k
            if (cb == 12) {
64
92
                return FAAD_ERR_DECODE_FAILED; /* Codebook 12 is reserved in ISO/IEC 14496-3 */
65
92
            }
66
30.4k
            uint32_t max_run = (1U << sect_bits) - 1;
67
30.4k
            uint32_t run_field = bits_get(bs, sect_bits);
68
30.4k
            uint32_t len = run_field;
69
            /* A run_field equal to max_run escapes: another one follows and adds on. */
70
14.2M
            while (run_field == max_run) {
71
14.2M
                run_field = bits_get(bs, sect_bits);
72
14.2M
                len += run_field;
73
14.2M
            }
74
30.4k
            if (len == 0) len = 1;
75
30.4k
            int end_sfb = k + len;
76
30.4k
            if (end_sfb > ics->max_sfb) end_sfb = ics->max_sfb;
77
30.4k
            if (end_sfb > MAX_SFB) end_sfb = MAX_SFB;
78
138k
            for (; k < end_sfb; k++) ics->sfb_cb[g][k] = (uint8_t)cb;
79
30.4k
            i++;
80
30.4k
        }
81
51.0k
    }
82
16.6k
    return FAAD_OK;
83
16.7k
}
84
85
faad_status decode_pce(BitReader *bs, struct faad_decoder *dec)
86
13.0k
{
87
13.0k
    uint32_t start = bits_get_consumed(bs);
88
13.0k
    asc_bitreader br = { bs->buffer, bs->len * 8, start };
89
13.0k
    AscPce pce;
90
13.0k
    if (!asc_parse_pce(&br, &pce))
91
499
        return FAAD_ERR_DECODE_FAILED;
92
12.5k
    bits_skip(bs, br.pos - start);
93
12.5k
    if (pce.channels > MAX_CHANNELS)
94
210
        return FAAD_ERR_UNSUPPORTED;
95
12.3k
    if (!dec->asc.num_channels) {
96
3.39k
        dec->asc.pce = pce;
97
3.39k
        faad_layout_update(dec); /* a layout with PCM in it is kept */
98
3.39k
    }
99
12.3k
    return FAAD_OK;
100
12.5k
}
101
102
faad_status decode_dse(BitReader *bs)
103
44.0k
{
104
44.0k
    bits_skip(bs, 4); /* element_instance_tag */
105
44.0k
    bool byte_align = bits_get(bs, 1);
106
44.0k
    uint32_t count = bits_get(bs, 8);
107
44.0k
    if (count == 255) count += bits_get(bs, 8);
108
44.0k
    if (byte_align) bits_byte_align(bs);
109
497k
    for (uint32_t i = 0; i < count; i++) bits_skip(bs, 8);
110
44.0k
    return FAAD_OK;
111
44.0k
}
112
113
faad_status decode_ics(BitReader *bs, struct faad_decoder *dec, ICSInfo *ics, float *spec, bool common_window)
114
16.9k
{
115
#ifdef FAAD_STATS
116
    g_faadStats.icsCount++;
117
#endif
118
119
16.9k
    ics->frame_len_long = FRAME_IS_960(dec) ? 960 : 1024;
120
16.9k
    ics->global_gain = bits_get(bs, 8);
121
122
16.9k
    if (!common_window) {
123
14.8k
        decode_ics_info(bs, ics);
124
14.8k
    }
125
126
16.9k
    extern void setup_sfb_offsets(ICSInfo *ics, uint32_t sample_rate);
127
    /* Window/sfb layout is defined relative to the AAC core codec's own rate
128
     * (Fs/2 of the nominal rate when SBR is present), not the nominal
129
     * post-SBR rate reported to callers. */
130
16.9k
    setup_sfb_offsets(ics, dec->core_sample_rate);
131
16.9k
    if (ics->max_sfb > ics->num_sfbs) return FAAD_ERR_DECODE_FAILED; /* a corrupt or misparsed ics_info */
132
133
#ifdef FAAD_STATS
134
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
135
        g_faadStats.shortBlockIcsCount++;
136
    }
137
#endif
138
139
#ifdef FAAD_STATS
140
    unsigned bits_mark = bits_get_consumed(bs);
141
#endif
142
16.7k
    faad_status st = decode_section_data(bs, ics);
143
16.7k
    if (st != FAAD_OK) return st;
144
#ifdef FAAD_STATS
145
    g_faadStats.frameSectBits += bits_get_consumed(bs) - bits_mark;
146
    bits_mark = bits_get_consumed(bs);
147
#endif
148
149
16.6k
    decode_scale_factor_data(bs, ics, dec->core_sample_rate);
150
#ifdef FAAD_STATS
151
    g_faadStats.frameSfBits += bits_get_consumed(bs) - bits_mark;
152
    bits_mark = bits_get_consumed(bs);
153
#endif
154
155
16.6k
    ics->pulse_data_present = bits_get(bs, 1);
156
16.6k
    ics->pulse_count = 0;
157
16.6k
    if (ics->pulse_data_present) {
158
1.19k
        uint32_t number_pulse = bits_get(bs, 2);
159
1.19k
        uint32_t pulse_start_sfb = bits_get(bs, 6);
160
        /* ยง4.6.4: pulses are for long windows only, and start inside the table */
161
1.19k
        if (ics->window_sequence == EIGHT_SHORT_SEQUENCE || pulse_start_sfb >= ics->num_sfbs)
162
123
            return FAAD_ERR_DECODE_FAILED;
163
1.07k
        uint32_t pos = ics->sfb_offsets[pulse_start_sfb];
164
3.62k
        for (uint32_t i = 0; i <= number_pulse; i++) {
165
2.55k
            pos += bits_get(bs, 5); /* pulse_offset */
166
2.55k
            if (pos >= ics->frame_len_long) return FAAD_ERR_DECODE_FAILED;
167
2.55k
            ics->pulse_pos[i] = (uint16_t)pos;
168
2.55k
            ics->pulse_amp[i] = (uint8_t)bits_get(bs, 4);
169
2.55k
        }
170
1.06k
        ics->pulse_count = (uint8_t)(number_pulse + 1);
171
1.06k
    }
172
173
16.4k
    ics->tns_data_present = bits_get(bs, 1);
174
#ifdef FAAD_STATS
175
    if (ics->tns_data_present) {
176
        g_faadStats.tnsActiveFrames++;
177
    }
178
#endif
179
16.4k
    if (ics->tns_data_present) {
180
1.67k
        uint32_t n_filt_bits = (ics->window_sequence == EIGHT_SHORT_SEQUENCE) ? 1 : 2;
181
1.67k
        int num_windows = (ics->window_sequence == EIGHT_SHORT_SEQUENCE) ? ics->num_windows : 1;
182
7.15k
        for (int w = 0; w < num_windows && w < 8; w++) {
183
5.55k
            ics->tns_n_filt[w] = bits_get(bs, n_filt_bits);
184
5.55k
            if (ics->tns_n_filt[w]) {
185
2.14k
                uint32_t coef_res = bits_get(bs, 1);
186
2.14k
                ics->tns_coef_res[w] = coef_res;
187
4.58k
                for (int f = 0; f < ics->tns_n_filt[w] && f < 4; f++) {
188
2.51k
                    ics->tns_length[w][f] = bits_get(bs, (ics->window_sequence == EIGHT_SHORT_SEQUENCE) ? 4 : 6);
189
2.51k
                    ics->tns_order[w][f] = bits_get(bs, (ics->window_sequence == EIGHT_SHORT_SEQUENCE) ? 3 : 5);
190
2.51k
                    if (ics->tns_order[w][f]) {
191
1.88k
                        ics->tns_direction[w][f] = bits_get(bs, 1);
192
1.88k
                        uint32_t coef_compress = bits_get(bs, 1);
193
1.88k
                        int bits_per_coef = (coef_res ? 4 : 3) - (int)coef_compress;
194
                        /* Only LC decodes, and its profile stops at 12; a longer filter
195
                         * cannot be applied as signaled, so the stream is not valid. */
196
1.88k
                        if (ics->tns_order[w][f] > TNS_MAX_ORDER) return FAAD_ERR_DECODE_FAILED;
197
11.2k
                        for (int c = 0; c < ics->tns_order[w][f]; c++) {
198
9.40k
                            uint32_t val = bits_get(bs, bits_per_coef);
199
9.40k
                            int32_t sval = (int32_t)val;
200
9.40k
                            if (sval & (1 << (bits_per_coef - 1))) {
201
5.36k
                                sval |= ~((1 << bits_per_coef) - 1);
202
5.36k
                            }
203
9.40k
                            ics->tns_coef[w][f][c] = (int8_t)sval;
204
9.40k
                        }
205
1.81k
                    }
206
2.51k
                }
207
2.14k
            }
208
5.55k
        }
209
1.67k
    }
210
211
16.4k
    ics->gain_control_present = bits_get(bs, 1);
212
16.4k
    if (ics->gain_control_present) {
213
1.98k
    }
214
215
#ifdef FAAD_STATS
216
    g_faadStats.frameAuxBits += bits_get_consumed(bs) - bits_mark;
217
    bits_mark = bits_get_consumed(bs);
218
#endif
219
16.4k
    st = decode_spectral_data(bs, ics, spec);
220
#ifdef FAAD_STATS
221
    g_faadStats.frameSpecBits += bits_get_consumed(bs) - bits_mark;
222
#endif
223
16.4k
    return st;
224
16.4k
}
225
226
faad_status decode_cpe(BitReader *bs, struct faad_decoder *dec, CPEInfo *cpe, uint32_t ch)
227
2.70k
{
228
#if MAX_CHANNELS < 2
229
    (void)bs; (void)dec; (void)cpe; (void)ch;
230
    return FAAD_ERR_DECODE_FAILED;
231
#else
232
2.70k
    if (ch + 1 >= MAX_CHANNELS) return FAAD_ERR_DECODE_FAILED;
233
2.70k
    bits_skip(bs, 4);
234
2.70k
    cpe->common_window = bits_get(bs, 1);
235
236
2.70k
    if (cpe->common_window) {
237
1.05k
        decode_ics_info(bs, &cpe->ics[0]);
238
1.05k
        cpe->ics[1] = cpe->ics[0];
239
240
1.05k
        cpe->ms_mask_present = (MSMaskMode)bits_get(bs, 2);
241
1.05k
        if (cpe->ms_mask_present == MS_MASK_EXPLICIT) {
242
752
            for (int g = 0; g < cpe->ics[0].num_window_groups && g < 8; g++) {
243
3.91k
                for (int sfb = 0; sfb < cpe->ics[0].max_sfb && sfb < MAX_SFB; sfb++) {
244
3.37k
                    cpe->ms_used[g][sfb] = bits_get(bs, 1);
245
3.37k
                }
246
543
            }
247
209
        }
248
1.05k
    }
249
250
2.70k
    faad_status st = decode_ics(bs, dec, &cpe->ics[0], dec->spec[ch], cpe->common_window);
251
2.70k
    if (st != FAAD_OK) return st;
252
2.60k
    return decode_ics(bs, dec, &cpe->ics[1], dec->spec[ch + 1], cpe->common_window);
253
2.70k
#endif
254
2.70k
}
255
256
faad_status decode_sce(BitReader *bs, struct faad_decoder *dec, ICSInfo *ics, uint32_t ch)
257
10.2k
{
258
10.2k
    if (ch >= MAX_CHANNELS) return FAAD_ERR_DECODE_FAILED;
259
10.2k
    memset(ics, 0, sizeof(*ics));
260
10.2k
    bits_skip(bs, 4);
261
10.2k
    return decode_ics(bs, dec, ics, dec->spec[ch], false);
262
10.2k
}