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/coupling.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
/* AAC coupling channel syntax and reconstruction (ISO/IEC 14496-3,
17
 * Tables 4.8, 4.153 and 4.154). */
18
#include "faad_internal.h"
19
20
faad_status decode_cce(BitReader *bs, struct faad_decoder *dec)
21
1.35k
{
22
1.35k
    unsigned tag = bits_get(bs, 4);
23
1.35k
    CouplingChannel *cc = dec->coupling;
24
2.35k
    while (cc && cc->tag != tag) cc = cc->next;
25
1.35k
    if (!cc) {
26
1.16k
        cc = AllocMemory(sizeof(*cc));
27
1.16k
        if (!cc) return FAAD_ERR_NO_MEMORY;
28
1.16k
        memset(cc, 0, sizeof(*cc));
29
1.16k
        cc->tag = (uint8_t)tag;
30
1.16k
        cc->next = dec->coupling;
31
1.16k
        dec->coupling = cc;
32
1.16k
        dec->allocated_bytes += sizeof(*cc);
33
1.16k
        if (sizeof(*cc) > dec->largest_allocation) dec->largest_allocation = sizeof(*cc);
34
1.16k
    }
35
1.35k
    if (cc->present) return FAAD_ERR_DECODE_FAILED; /* duplicate tag */
36
1.34k
    cc->present = true;
37
1.34k
    bool independent = bits_get(bs, 1);
38
1.34k
    cc->targets = (uint8_t)(bits_get(bs, 3) + 1);
39
1.34k
    unsigned lists = 0;
40
5.49k
    for (unsigned i = 0; i < cc->targets; i++) {
41
4.15k
        cc->target[i].pair = (uint8_t)bits_get(bs, 1);
42
4.15k
        cc->target[i].tag = (uint8_t)bits_get(bs, 4);
43
4.15k
        cc->target[i].select = cc->target[i].pair ? (uint8_t)bits_get(bs, 2) : 0;
44
4.15k
        cc->target[i].gain = (uint8_t)lists++;
45
4.15k
        if (cc->target[i].select == 3) lists++;
46
4.15k
    }
47
1.34k
    unsigned domain = bits_get(bs, 1);
48
1.34k
    cc->point = independent ? 2 : (uint8_t)domain;
49
1.34k
    if (independent && !cc->overlap) {
50
466
        cc->overlap = AllocMemory(sizeof(float[FRAME_LEN_LONG]));
51
466
        if (!cc->overlap) return FAAD_ERR_NO_MEMORY;
52
466
        memset(cc->overlap, 0, sizeof(float[FRAME_LEN_LONG]));
53
466
        dec->allocated_bytes += sizeof(float[FRAME_LEN_LONG]);
54
466
        if (sizeof(float[FRAME_LEN_LONG]) > dec->largest_allocation)
55
0
            dec->largest_allocation = sizeof(float[FRAME_LEN_LONG]);
56
466
    }
57
1.34k
    bool sign = bits_get(bs, 1);
58
1.34k
    float step = (float)(1u << bits_get(bs, 2)) / 8.0f;
59
1.34k
    memset(&cc->ics, 0, sizeof(cc->ics));
60
1.34k
    faad_status st = decode_ics(bs, dec, &cc->ics, cc->spec, false);
61
1.34k
    if (st != FAAD_OK) return st;
62
1.29k
    unsigned bands = cc->ics.num_window_groups * cc->ics.max_sfb;
63
1.29k
    if (bands > 128) return FAAD_ERR_DECODE_FAILED;
64
5.61k
    for (unsigned l = 0; l < lists; l++) {
65
4.33k
        bool common = l == 0 || independent || bits_get(bs, 1);
66
4.33k
        int gain = l && common ? decode_coupling_gain(bs) : 0;
67
30.0k
        for (unsigned b = 0; b < (independent ? 1 : bands); b++) {
68
25.7k
            unsigned g = cc->ics.max_sfb ? b / cc->ics.max_sfb : 0;
69
25.7k
            unsigned sfb = cc->ics.max_sfb ? b % cc->ics.max_sfb : 0;
70
25.7k
            if (!common && cc->ics.sfb_cb[g][sfb]) gain += decode_coupling_gain(bs);
71
            /* The sign bit belongs to the accumulated gain, not its delta.
72
             * Division rounds down for signed odd exponents. */
73
25.7k
            int exponent = sign && !common ? (gain - (gain & 1)) / 2 : gain;
74
25.7k
            float value = exp2f(-exponent * step);
75
25.7k
            if (sign && !common && (gain & 1)) value = -value;
76
25.7k
            if (!isfinite(value)) return FAAD_ERR_DECODE_FAILED;
77
25.7k
            cc->gain[l][b] = value;
78
25.7k
        }
79
4.33k
    }
80
1.28k
    if (bs->overread) return FAAD_ERR_DECODE_FAILED;
81
1.01k
    apply_pns(&cc->ics, cc->spec, &dec->pns_seed);
82
1.01k
    apply_tns(&cc->ics, cc->spec);
83
1.01k
    return FAAD_OK;
84
1.28k
}
85
86
static void add_spectrum(const CouplingChannel *cc, unsigned list, float *dst)
87
504
{
88
504
    const ICSInfo *ics = &cc->ics;
89
504
    unsigned window = 0, band = 0;
90
504
    unsigned stride = ics->window_sequence == EIGHT_SHORT_SEQUENCE ? ics->frame_len_long / 8 : 0;
91
1.54k
    for (unsigned g = 0; g < ics->num_window_groups; g++) {
92
3.96k
        for (unsigned sfb = 0; sfb < ics->max_sfb; sfb++, band++) {
93
2.91k
            if (!ics->sfb_cb[g][sfb]) continue;
94
1.41k
            float gain = cc->gain[list][band];
95
3.24k
            for (unsigned w = 0; w < ics->window_group_length[g]; w++) {
96
1.82k
                unsigned base = (window + w) * stride;
97
14.4k
                for (unsigned i = ics->sfb_offsets[sfb]; i < ics->sfb_offsets[sfb + 1]; i++)
98
12.6k
                    dst[base + i] += gain * cc->spec[base + i];
99
1.82k
            }
100
1.41k
        }
101
1.04k
        window += ics->window_group_length[g];
102
1.04k
    }
103
504
}
104
105
static void couple_channel(const CouplingChannel *cc, unsigned ch, float *dst, const float *pcm)
106
1.36k
{
107
4.34k
    for (unsigned t = 0; t < cc->targets; t++) {
108
2.97k
        unsigned base = cc->target[t].ch, select = cc->target[t].select;
109
2.97k
        unsigned list = cc->target[t].gain;
110
2.97k
        bool left = ch == base && select != 1;
111
2.97k
        bool right = cc->target[t].pair && ch == base + 1 && select != 2;
112
2.97k
        if (!left && !right) continue;
113
807
        if (right && select == 3) list++;
114
807
        if (cc->point < 2) add_spectrum(cc, list, dst);
115
303
        else {
116
303
            float gain = cc->gain[list][0];
117
297k
            for (unsigned i = 0; i < cc->ics.frame_len_long; i++) dst[i] += gain * pcm[i];
118
303
        }
119
807
    }
120
1.36k
}
121
122
static void couple_spectrum(struct faad_decoder *dec, unsigned ch, unsigned point)
123
2.41k
{
124
5.62k
    for (CouplingChannel *cc = dec->coupling; cc; cc = cc->next)
125
3.21k
        if (cc->present && cc->point == point)
126
805
            couple_channel(cc, ch, dec->spec[ch], NULL);
127
2.41k
}
128
129
faad_status coupling_spectrum(struct faad_decoder *dec, CouplingTarget *targets, unsigned count)
130
355
{
131
797
    for (CouplingChannel *cc = dec->coupling; cc; cc = cc->next) {
132
489
        if (!cc->present) continue;
133
1.37k
        for (unsigned t = 0; t < cc->targets; t++) {
134
985
            unsigned i;
135
1.62k
            for (i = 0; i < count; i++)
136
1.57k
                if (targets[i].tag == cc->target[t].tag
137
1.05k
                    && targets[i].type == (cc->target[t].pair ? ID_CPE : ID_SCE)) break;
138
985
            if (i == count) return FAAD_ERR_DECODE_FAILED;
139
938
            cc->target[t].ch = targets[i].ch;
140
938
        }
141
437
    }
142
308
    uint32_t seed = dec->pns_seed;
143
#ifdef FAAD_STATS
144
    faadDecStats stats = g_faadStats;
145
#endif
146
308
    faad_status result = FAAD_OK;
147
    /* Only coupled frames take this pass. Reconstruct the pre-TNS spectra
148
     * from saved readers; this also handles CCEs preceding their targets.
149
     * Restore the original PNS seeds and leave frame statistics unchanged. */
150
1.31k
    for (unsigned t = 0; t < count; t++) {
151
1.04k
        CouplingTarget *target = &targets[t];
152
1.04k
        CPEInfo *cpe = &dec->scratch->cpe;
153
1.04k
        memset(cpe, 0, sizeof(*cpe));
154
1.04k
        BitReader bs = target->bits;
155
1.04k
        unsigned ch = target->ch, nch = target->type == ID_CPE ? 2 : 1;
156
1.04k
        dec->pns_seed = target->seed;
157
1.04k
        result = nch == 2 ? decode_cpe(&bs, dec, cpe, ch)
158
1.04k
                          : decode_sce(&bs, dec, &cpe->ics[0], ch);
159
1.04k
        if (result != FAAD_OK) break;
160
2.30k
        for (unsigned c = 0; c < nch; c++) apply_pns(&cpe->ics[c], dec->spec[ch + c], &dec->pns_seed);
161
1.04k
        if (nch == 2) {
162
202
            apply_ms_stereo(cpe, dec->spec[ch], dec->spec[ch + 1]);
163
202
            apply_is_stereo(cpe, dec->spec[ch], dec->spec[ch + 1]);
164
202
        }
165
2.25k
        for (unsigned c = 0; c < nch; c++) {
166
1.24k
            ICSInfo *ics = &cpe->ics[c];
167
2.86k
            for (CouplingChannel *cc = dec->coupling; cc; cc = cc->next) {
168
1.65k
                if (!cc->present || cc->point == 2) continue;
169
2.77k
                for (unsigned i = 0; i < cc->targets; i++) {
170
1.96k
                    unsigned base = cc->target[i].ch, sel = cc->target[i].select;
171
1.96k
                    if (!((ch + c == base && sel != 1)
172
1.44k
                        || (cc->target[i].pair && ch + c == base + 1 && sel != 2))) continue;
173
577
                    if (ics->window_sequence != cc->ics.window_sequence
174
546
                        || ics->window_shape != cc->ics.window_shape) {
175
43
                        result = FAAD_ERR_DECODE_FAILED;
176
43
                        goto done;
177
43
                    }
178
577
                }
179
852
            }
180
1.20k
            couple_spectrum(dec, ch + c, 0);
181
1.20k
            apply_tns(ics, dec->spec[ch + c]);
182
1.20k
            couple_spectrum(dec, ch + c, 1);
183
1.20k
        }
184
1.04k
    }
185
308
done:
186
308
    dec->pns_seed = seed;
187
#ifdef FAAD_STATS
188
    g_faadStats = stats;
189
#endif
190
308
    return result;
191
308
}
192
193
void coupling_pcm(struct faad_decoder *dec)
194
818
{
195
2.07k
    for (CouplingChannel *cc = dec->coupling; cc; cc = cc->next) {
196
1.25k
        if (!cc->present || cc->point != 2) {
197
1.12k
            if (cc->overlap) memset(cc->overlap, 0, sizeof(float[FRAME_LEN_LONG]));
198
1.12k
            cc->prev_shape = 0;
199
1.12k
            continue;
200
1.12k
        }
201
        /* Core synthesis and concealment history have consumed spec already;
202
         * SBR has not started, so one channel holds temporary coupling PCM. */
203
123
        float *pcm = dec->spec[0];
204
123
        imdct_window(dec, cc->ics.window_sequence, cc->ics.window_shape,
205
123
                     cc->prev_shape, cc->overlap, cc->spec, pcm);
206
123
        cc->prev_shape = cc->ics.window_shape;
207
687
        for (unsigned ch = 0; ch < dec->num_channels; ch++)
208
564
            couple_channel(cc, ch, dec->pcm + ch * FRAME_LEN_LONG, pcm);
209
123
    }
210
818
}