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/ps.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
#include "sbr_tables.h"
18
19
#ifndef FAAD_DISABLE_PS
20
21
/* Band layout for the 20- and 34-parameter configurations. */
22
static const int ps_nr_par_bands[2]    = { 20, 34 };
23
static const int ps_nr_ipdopd_bands[2] = { 11, 17 };
24
static const int ps_nr_bands[2]        = { 71, 91 };
25
static const int ps_decay_cutoff[2]    = { 10, 32 };
26
static const int ps_nr_allpass[2]      = { 30, 50 };
27
static const int ps_short_delay_band[2] = { 42, 62 };
28
318k
#define PS_DECAY_SLOPE 0.05f
29
30
static const uint8_t ps_nr_iidicc_par[6] = { 10, 20, 34, 10, 20, 34 };
31
static const uint8_t ps_nr_ipdopd_par[6] = { 5, 11, 17, 5, 11, 17 };
32
static const uint8_t ps_num_env_tab[2][4] = { { 0, 1, 2, 4 }, { 1, 2, 3, 4 } };
33
34
/* Table 8.48 / 8.49: parameter band b(k) of each hybrid sub-band k. */
35
static const int8_t ps_k_to_b_20[71] = {
36
     1,  0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 14, 15,
37
    15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18,
38
    18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
39
    19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19
40
};
41
static const int8_t ps_k_to_b_34[91] = {
42
     0,  1,  2,  3,  4,  5,  6,  6,  7,  2,  1,  0, 10, 10,  4,  5,  6,  7,  8,
43
     9, 10, 11, 12,  9, 14, 11, 12, 13, 14, 15, 16, 13, 16, 17, 18, 19, 20, 21,
44
    22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29,
45
    30, 30, 30, 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33,
46
    33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33
47
};
48
49
/* Hybrid filter prototypes (§8.6.4.3), first 7 of 13 symmetric taps. */
50
static const float ps_g0_q8[7]  = { 0.00746082949812f, 0.02270420949825f, 0.04546865930473f, 0.07266113929591f, 0.09885108575264f, 0.11793710567217f, 0.125f };
51
static const float ps_g0_q12[7] = { 0.04081179924692f, 0.03812810994926f, 0.05144908135699f, 0.06399831151592f, 0.07428313801106f, 0.08100347892914f, 0.08333333333333f };
52
static const float ps_g1_q8[7]  = { 0.01565675600122f, 0.03752716391991f, 0.05417891378782f, 0.08417044116767f, 0.10307344158036f, 0.12222452249753f, 0.125f };
53
static const float ps_g2_q4[7]  = { -0.05908211155639f, -0.04871498374946f, 0.0f, 0.07778723915851f, 0.16486303567403f, 0.23279856662996f, 0.25f };
54
static const float ps_g1_q2[7]  = { 0.0f, 0.01899487526049f, 0.0f, -0.07293139167538f, 0.0f, 0.30596630545168f, 0.5f };
55
56
/* IID quantization levels in dB (Tables 8.24 / 8.25) and ICC values (Table 8.26). */
57
static const int8_t ps_iid_db_default[15] = { -25, -18, -14, -10, -7, -4, -2, 0, 2, 4, 7, 10, 14, 18, 25 };
58
static const int8_t ps_iid_db_fine[31] = { -50, -45, -40, -35, -30, -25, -22, -19, -16, -13, -10, -8, -6, -4, -2, 0,
59
                                           2, 4, 6, 8, 10, 13, 16, 19, 22, 25, 30, 35, 40, 45, 50 };
60
static const float ps_icc_invq[8] = { 1.0f, 0.937f, 0.84118f, 0.60092f, 0.36764f, 0.0f, -0.589f, -1.0f };
61
62
/* Sub-band center frequencies in QMF-band units, hybrid bands only. */
63
static const int8_t ps_f_center_20[10] = { -3, -1, 1, 3, 5, 7, 10, 14, 18, 22 };            /* /8  */
64
static const int8_t ps_f_center_34[32] = { 2, 6, 10, 14, 18, 22, 26, 30, 34, -10, -6, -2, 51, 57, 15, 21,
65
                                           27, 33, 39, 45, 54, 66, 78, 42, 102, 66, 78, 90, 102, 114, 126, 90 }; /* /24 */
66
67
/* The complex hybrid filters G_q(n) = g(n) exp(-j 2pi/Q (q + 1/2)(n - 6))
68
 * for each prototype and split factor Q, taps 0..6 (the rest by symmetry).
69
 * The 20-band layout splits QMF band 0 eight ways (prototype g0); the
70
 * 34-band layout splits band 0 twelve ways (g0), band 1 eight ways (g1) and
71
 * bands 2..4 four ways (g2). */
72
static float ps_split8_g0[8][7][2], ps_split12_g0[12][7][2], ps_split8_g1[8][7][2], ps_split4_g2[4][7][2];
73
static float ps_phi_fract[2][50][2];
74
static float ps_q_fract[2][50][3][2];
75
static float ps_HA[46][8][4], ps_HB[46][8][4];
76
static void ps_init_band_maps(void);
77
78
static void ps_init_split_filters(void)
79
2
{
80
2
    static const struct { float (*coef)[7][2]; const float *proto; uint8_t Q; } split[4] = {
81
2
        { ps_split8_g0, ps_g0_q8, 8 }, { ps_split12_g0, ps_g0_q12, 12 },
82
2
        { ps_split8_g1, ps_g1_q8, 8 }, { ps_split4_g2, ps_g2_q4, 4 },
83
2
    };
84
10
    for (int i = 0; i < 4; i++) {
85
72
        for (int q = 0; q < split[i].Q; q++) {
86
512
            for (int n = 0; n < 7; n++) {
87
448
                double phase = -2.0 * M_PI * (q + 0.5) * (n - 6) / split[i].Q;
88
448
                split[i].coef[q][n][0] = (float)(split[i].proto[n] * cos(phase));
89
448
                split[i].coef[q][n][1] = (float)(split[i].proto[n] * sin(phase));
90
448
            }
91
64
        }
92
8
    }
93
2
}
94
95
void init_ps_tables(void)
96
2
{
97
2
    static const double links[3] = { 0.43, 0.75, 0.347 };
98
2
    const double gain = 0.39;
99
100
2
    ps_init_split_filters();
101
102
6
    for (int is34 = 0; is34 < 2; is34++) {
103
164
        for (int k = 0; k < ps_nr_allpass[is34]; k++) {
104
160
            double fc;
105
160
            if (!is34) fc = (k < 10) ? ps_f_center_20[k] / 8.0 : k - 6.5;
106
100
            else       fc = (k < 32) ? ps_f_center_34[k] / 24.0 : k - 26.5;
107
640
            for (int m = 0; m < 3; m++) {
108
480
                double th = -M_PI * links[m] * fc;
109
480
                ps_q_fract[is34][k][m][0] = (float)cos(th);
110
480
                ps_q_fract[is34][k][m][1] = (float)sin(th);
111
480
            }
112
160
            double th = -M_PI * gain * fc;
113
160
            ps_phi_fract[is34][k][0] = (float)cos(th);
114
160
            ps_phi_fract[is34][k][1] = (float)sin(th);
115
160
        }
116
4
    }
117
118
    /* Mixing matrices (§8.6.4.6.2 type A, §8.6.4.6.3 type B) for every IID/ICC index. */
119
94
    for (int i = 0; i < 46; i++) {
120
92
        double db = (i < 15) ? ps_iid_db_default[i] : ps_iid_db_fine[i - 15];
121
92
        double c = pow(10.0, db / 20.0);
122
92
        double c1 = sqrt(2.0) / sqrt(1.0 + c * c);
123
92
        double c2 = c * c1;
124
828
        for (int icc = 0; icc < 8; icc++) {
125
736
            double alpha = 0.5 * acos(ps_icc_invq[icc]);
126
736
            double beta = alpha * (c1 - c2) / sqrt(2.0);
127
736
            ps_HA[i][icc][0] = (float)(c2 * cos(beta + alpha));
128
736
            ps_HA[i][icc][1] = (float)(c1 * cos(beta - alpha));
129
736
            ps_HA[i][icc][2] = (float)(c2 * sin(beta + alpha));
130
736
            ps_HA[i][icc][3] = (float)(c1 * sin(beta - alpha));
131
132
736
            double rho = ps_icc_invq[icc] > 0.05 ? ps_icc_invq[icc] : 0.05;
133
736
            double a = 0.5 * atan2(2.0 * c * rho, c * c - 1.0);
134
736
            double mu = c + 1.0 / c;
135
736
            mu = sqrt(1.0 + (4.0 * rho * rho - 4.0) / (mu * mu));
136
736
            double gamma = atan(sqrt((1.0 - mu) / (1.0 + mu)));
137
736
            if (a < 0) a += M_PI / 2;
138
736
            ps_HB[i][icc][0] = (float)( sqrt(2.0) * cos(a) * cos(gamma));
139
736
            ps_HB[i][icc][1] = (float)( sqrt(2.0) * sin(a) * cos(gamma));
140
736
            ps_HB[i][icc][2] = (float)(-sqrt(2.0) * sin(a) * sin(gamma));
141
736
            ps_HB[i][icc][3] = (float)( sqrt(2.0) * cos(a) * sin(gamma));
142
736
        }
143
92
    }
144
2
    ps_init_band_maps();
145
2
}
146
147
/* Bitstream (§8.6.2) */
148
149
/* Deltas along frequency (df) or against the previous envelope (dt). */
150
static bool ps_read_par(BitReader *bs, PSState *ps, int8_t par[PS_MAX_ENV][PS_NR_PAR], int num, int e, bool dt,
151
                        const SBRHuffBook *book, int mask, int limit)
152
17.1k
{
153
17.1k
    if (dt) {
154
7.58k
        int e_prev = e ? e - 1 : (int)ps->num_env_old - 1;
155
7.58k
        if (e_prev < 0) e_prev = 0;
156
83.3k
        for (int b = 0; b < num; b++) {
157
75.9k
            int v = par[e_prev][b] + sbr_huff_decode(bs, book);
158
75.9k
            if (mask) v &= mask;
159
75.9k
            par[e][b] = (int8_t)v;
160
75.9k
            if (limit && (v > limit || v < -limit)) return false;
161
75.9k
        }
162
9.52k
    } else {
163
9.52k
        int v = 0;
164
111k
        for (int b = 0; b < num; b++) {
165
102k
            v += sbr_huff_decode(bs, book);
166
102k
            if (mask) v &= mask;
167
102k
            par[e][b] = (int8_t)v;
168
102k
            if (limit && (v > limit || v < -limit)) return false;
169
102k
        }
170
9.52k
    }
171
16.5k
    return true;
172
17.1k
}
173
174
static void ps_copy_envelope(PSState *ps, int to, int from)
175
2.50k
{
176
2.50k
    memcpy(ps->iid_par[to], ps->iid_par[from], sizeof(ps->iid_par[0]));
177
2.50k
    memcpy(ps->icc_par[to], ps->icc_par[from], sizeof(ps->icc_par[0]));
178
2.50k
    memcpy(ps->ipd_par[to], ps->ipd_par[from], sizeof(ps->ipd_par[0]));
179
2.50k
    memcpy(ps->opd_par[to], ps->opd_par[from], sizeof(ps->opd_par[0]));
180
2.50k
}
181
182
static void ps_clear_params(PSState *ps)
183
2.16k
{
184
2.16k
    memset(ps->iid_par, 0, sizeof(ps->iid_par));
185
2.16k
    memset(ps->icc_par, 0, sizeof(ps->icc_par));
186
2.16k
    memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
187
2.16k
    memset(ps->opd_par, 0, sizeof(ps->opd_par));
188
2.16k
}
189
190
/* ps_data(): bits_left is the extension payload still available. */
191
void ps_read_data(struct faad_decoder *dec, BitReader *bs, uint32_t bits_left)
192
5.76k
{
193
5.76k
    PSState *ps = dec->ps;
194
5.76k
    uint32_t start_pos = bits_get_consumed(bs);
195
5.76k
    bool ok = true;
196
197
5.76k
    if (bits_get(bs, 1)) { /* enable_ps_header */
198
1.78k
        ps->enable_iid = bits_get(bs, 1);
199
1.78k
        if (ps->enable_iid) {
200
831
            int mode = (int)bits_get(bs, 3);
201
831
            if (mode > 5) { ok = false; mode = 0; }
202
831
            ps->nr_iid_par = ps_nr_iidicc_par[mode];
203
831
            ps->iid_quant = mode > 2;
204
831
            ps->nr_ipdopd_par = ps_nr_ipdopd_par[mode];
205
831
        }
206
1.78k
        ps->enable_icc = bits_get(bs, 1);
207
1.78k
        if (ps->enable_icc) {
208
805
            int mode = (int)bits_get(bs, 3);
209
805
            if (mode > 5) { ok = false; mode = 0; }
210
805
            ps->icc_mode = (uint8_t)mode;
211
805
            ps->nr_icc_par = ps_nr_iidicc_par[mode];
212
805
        }
213
1.78k
        ps->enable_ext = bits_get(bs, 1);
214
1.78k
        ps->start = true;
215
1.78k
    }
216
217
5.76k
    ps->frame_class = (uint8_t)bits_get(bs, 1);
218
5.76k
    ps->num_env_old = ps->num_env;
219
5.76k
    int num_env = ps_num_env_tab[ps->frame_class & 1][bits_get(bs, 2) & 3];
220
5.76k
    ps->num_env = (uint8_t)num_env;
221
5.76k
    ps->border[0] = -1;
222
5.76k
    int num_qmf_slots = FRAME_IS_960(dec) ? 30 : 32;
223
5.76k
    if (ps->frame_class) {
224
11.8k
        for (int e = 1; e <= num_env && e < PS_MAX_ENV; e++) {
225
7.67k
            ps->border[e] = (int8_t)bits_get(bs, 5);
226
7.67k
            if (ps->border[e] < ps->border[e - 1]) ok = false;
227
7.67k
        }
228
4.14k
    } else {
229
3.99k
        for (int e = 1; e <= num_env && e < PS_MAX_ENV; e++) {
230
2.36k
            int shift = (ps->num_env == 4) ? 2 : (ps->num_env == 2) ? 1 : 0;
231
2.36k
            ps->border[e] = (int8_t)(((e * num_qmf_slots) >> shift) - 1);
232
2.36k
        }
233
1.62k
    }
234
235
5.76k
    if (ps->enable_iid) {
236
3.07k
        int limit = ps->iid_quant ? 15 : 7;
237
7.63k
        for (int e = 0; e < ps->num_env && ok; e++) {
238
4.55k
            bool dt = bits_get(bs, 1);
239
4.55k
            int book = ps->iid_quant ? (dt ? HB_PS_IID_DT_FINE : HB_PS_IID_DF_FINE)
240
4.55k
                                     : (dt ? HB_PS_IID_DT : HB_PS_IID_DF);
241
4.55k
            ok = ps_read_par(bs, ps, ps->iid_par, ps->nr_iid_par, e, dt, &sbr_books[book], 0, limit);
242
4.55k
        }
243
3.07k
    } else {
244
2.69k
        memset(ps->iid_par, 0, sizeof(ps->iid_par));
245
2.69k
    }
246
5.76k
    if (ps->enable_icc) {
247
5.51k
        for (int e = 0; e < ps->num_env && ok; e++) {
248
2.98k
            bool dt = bits_get(bs, 1);
249
2.98k
            ok = ps_read_par(bs, ps, ps->icc_par, ps->nr_icc_par, e, dt,
250
2.98k
                             &sbr_books[dt ? HB_PS_ICC_DT : HB_PS_ICC_DF], 0, 0);
251
64.4k
            for (int b = 0; b < ps->nr_icc_par; b++) if ((unsigned)ps->icc_par[e][b] > 7) ok = false;
252
2.98k
        }
253
3.24k
    } else {
254
3.24k
        memset(ps->icc_par, 0, sizeof(ps->icc_par));
255
3.24k
    }
256
257
5.76k
    if (ps->enable_ext && ok) {
258
2.30k
        int cnt = (int)bits_get(bs, 4);
259
2.30k
        if (cnt == 15) cnt += (int)bits_get(bs, 8);
260
2.30k
        cnt *= 8;
261
13.6k
        while (cnt > 7) {
262
11.9k
            uint32_t before = bits_get_consumed(bs);
263
11.9k
            int id = (int)bits_get(bs, 2);
264
11.9k
            if (id == 0) {
265
5.73k
                ps->enable_ipdopd = bits_get(bs, 1);
266
5.73k
                if (ps->enable_ipdopd) {
267
7.01k
                    for (int e = 0; e < ps->num_env; e++) {
268
4.78k
                        bool dt = bits_get(bs, 1);
269
4.78k
                        ps_read_par(bs, ps, ps->ipd_par, ps->nr_ipdopd_par, e, dt,
270
4.78k
                                    &sbr_books[dt ? HB_PS_IPD_DT : HB_PS_IPD_DF], 7, 0);
271
4.78k
                        dt = bits_get(bs, 1);
272
4.78k
                        ps_read_par(bs, ps, ps->opd_par, ps->nr_ipdopd_par, e, dt,
273
4.78k
                                    &sbr_books[dt ? HB_PS_OPD_DT : HB_PS_OPD_DF], 7, 0);
274
4.78k
                    }
275
2.22k
                }
276
5.73k
                bits_skip(bs, 1);
277
5.73k
            }
278
11.9k
            uint32_t used = bits_get_consumed(bs) - before;
279
11.9k
            if (used == 0) { ok = false; break; }
280
11.3k
            cnt -= (int)used;
281
11.3k
        }
282
2.30k
        if (cnt < 0) ok = false;
283
2.23k
        else bits_skip(bs, (uint32_t)cnt);
284
2.30k
    }
285
286
5.76k
    if (ok) {
287
        /* The parameters must reach the end of the frame (§8.6.4.4): a
288
         * frame without envelopes holds the previous frame's last
289
         * parameters for its whole length, one whose last border stops
290
         * short repeats its last envelope up to slot 31. */
291
3.60k
        if (ps->num_env == 0) {
292
673
            if (ps->num_env_old > 1) ps_copy_envelope(ps, 0, ps->num_env_old - 1);
293
673
            ps->num_env = 1;
294
673
            ps->border[1] = (int8_t)(num_qmf_slots - 1);
295
2.93k
        } else if (ps->border[ps->num_env] < num_qmf_slots - 1) {
296
2.40k
            ps_copy_envelope(ps, ps->num_env, ps->num_env - 1);
297
2.40k
            ps->num_env++;
298
2.40k
            ps->border[ps->num_env] = (int8_t)(num_qmf_slots - 1);
299
2.40k
        }
300
3.60k
        ps->is34_old = ps->is34;
301
3.60k
        if (ps->enable_iid || ps->enable_icc)
302
2.31k
            ps->is34 = (ps->enable_iid && ps->nr_iid_par == 34) || (ps->enable_icc && ps->nr_icc_par == 34);
303
3.60k
        if (!ps->enable_ipdopd) {
304
3.14k
            memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
305
3.14k
            memset(ps->opd_par, 0, sizeof(ps->opd_par));
306
3.14k
        }
307
3.60k
        dec->ps_present = true;
308
#ifdef FAAD_STATS
309
        g_faadStats.psActiveFrames++;
310
        {
311
            FILE *df = faad_dump_file();
312
            if (df) fprintf(df, "P %u %d %d %d\n", g_faadStats.totalFrames,
313
                            (int)ps->enable_iid, (int)ps->enable_icc, (int)ps->num_env);
314
            if (df) {
315
                int iid_mode = ps->enable_iid
316
                    ? (ps->iid_quant ? 3 : 0) + (ps->nr_iid_par == 20 ? 1 : ps->nr_iid_par == 34 ? 2 : 0)
317
                    : -1;
318
                int iid_n = ps->enable_iid ? ps->nr_iid_par : 0;
319
                int icc_n = ps->enable_icc ? ps->nr_icc_par : 0;
320
                int ipd_n = ps->enable_ipdopd ? ps->nr_ipdopd_par : 0;
321
                if (iid_n > PS_NR_PAR) iid_n = PS_NR_PAR;
322
                if (icc_n > PS_NR_PAR) icc_n = PS_NR_PAR;
323
                if (ipd_n > PS_NR_PAR) ipd_n = PS_NR_PAR;
324
                fprintf(df, "PM %u %d %d %d %d %d %d %d\n", g_faadStats.totalFrames,
325
                        iid_mode, ps->enable_icc ? (int)ps->icc_mode : -1,
326
                        (int)ps->enable_ipdopd, iid_n, icc_n, ipd_n,
327
                        (int)ps->frame_class);
328
                for (int e = 0; e < ps->num_env && e < PS_MAX_ENV; e++) {
329
                    fprintf(df, "PE %u %d %d", g_faadStats.totalFrames, e,
330
                            (int)ps->border[e + 1]);
331
                    for (int b = 0; b < iid_n; b++) fprintf(df, " %d", (int)ps->iid_par[e][b]);
332
                    fprintf(df, " |");
333
                    for (int b = 0; b < icc_n; b++) fprintf(df, " %d", (int)ps->icc_par[e][b]);
334
                    fprintf(df, " |");
335
                    for (int b = 0; b < ipd_n; b++) fprintf(df, " %d", (int)ps->ipd_par[e][b]);
336
                    fprintf(df, " |");
337
                    for (int b = 0; b < ipd_n; b++) fprintf(df, " %d", (int)ps->opd_par[e][b]);
338
                    fputc('\n', df);
339
                }
340
            }
341
        }
342
#endif
343
3.60k
    } else {
344
2.16k
        ps->start = false;
345
2.16k
        ps_clear_params(ps);
346
2.16k
    }
347
348
5.76k
    uint32_t used = bits_get_consumed(bs) - start_pos;
349
5.76k
    if (used < bits_left) bits_skip(bs, bits_left - used);
350
5.76k
}
351
352
/* Hybrid filterbank (§8.6.4.3) */
353
354
/* One slot of a Q-way split of one QMF band. The 13-tap filters f_q share
355
 * the symmetric real prototype, so f_q[12-j] = conj(f_q[j]) and each tap
356
 * pair costs one complex multiply: f x_j + conj(f) x_{12-j}. */
357
static void ps_split_slot(float out[][2], float (*in)[2], float (*f)[7][2], int bands)
358
21.2k
{
359
165k
    for (int q = 0; q < bands; q++) {
360
144k
        float sr = f[q][6][0] * in[6][0], si = f[q][6][0] * in[6][1];
361
1.01M
        for (int j = 0; j < 6; j++) {
362
867k
            float fr = f[q][j][0], fi = f[q][j][1];
363
867k
            float ar = in[j][0], ai = in[j][1], br = in[12 - j][0], bi = in[12 - j][1];
364
867k
            sr += fr * (ar + br) - fi * (ai - bi);
365
867k
            si += fr * (ai + bi) + fi * (ar - br);
366
867k
        }
367
144k
        out[q][0] = sr;
368
144k
        out[q][1] = si;
369
144k
    }
370
21.2k
}
371
372
/* Two-way split with the real prototype g1_Q2, whose even taps are zero:
373
 * the two sub-bands are the center tap plus and minus the odd-tap sum. */
374
static void ps_split2_slot(float out[2][2], float (*in)[2], int reverse)
375
10.5k
{
376
10.5k
    float cr = ps_g1_q2[6] * in[6][0], ci = ps_g1_q2[6] * in[6][1];
377
10.5k
    float sr = 0.0f, si = 0.0f;
378
42.1k
    for (int j = 1; j < 6; j += 2) {
379
31.6k
        sr += ps_g1_q2[j] * (in[j][0] + in[12 - j][0]);
380
31.6k
        si += ps_g1_q2[j] * (in[j][1] + in[12 - j][1]);
381
31.6k
    }
382
10.5k
    out[reverse][0] = cr + sr;   out[reverse][1] = ci + si;
383
10.5k
    out[!reverse][0] = cr - sr;  out[!reverse][1] = ci - si;
384
10.5k
}
385
386
/* Slot n of the hybrid domain: the low QMF bands split, the rest passed
387
 * through. in_buf holds the QMF slots with six of look-back, so the filter
388
 * centered on slot n reads in_buf[n .. n+12]. */
389
static void ps_hybrid_analysis_slot(PSState *ps, int n, float out[PS_NR_BANDS][2], float X[64][2])
390
8.47k
{
391
8.47k
    if (ps->is34) {
392
3.19k
        ps_split_slot(out,      ps->in_buf[0] + n, ps_split12_g0, 12);
393
3.19k
        ps_split_slot(out + 12, ps->in_buf[1] + n, ps_split8_g1, 8);
394
3.19k
        ps_split_slot(out + 20, ps->in_buf[2] + n, ps_split4_g2, 4);
395
3.19k
        ps_split_slot(out + 24, ps->in_buf[3] + n, ps_split4_g2, 4);
396
3.19k
        ps_split_slot(out + 28, ps->in_buf[4] + n, ps_split4_g2, 4);
397
191k
        for (int k = 5; k < 64; k++) { out[k + 27][0] = X[k][0]; out[k + 27][1] = X[k][1]; }
398
5.27k
    } else {
399
        /* QMF band 0 splits eight ways and the sub-bands merge to six in
400
         * frequency order; bands 1 and 2 split two ways. */
401
5.27k
        float t[8][2];
402
5.27k
        ps_split_slot(t, ps->in_buf[0] + n, ps_split8_g0, 8);
403
5.27k
        out[0][0] = t[6][0];           out[0][1] = t[6][1];
404
5.27k
        out[1][0] = t[7][0];           out[1][1] = t[7][1];
405
5.27k
        out[2][0] = t[0][0];           out[2][1] = t[0][1];
406
5.27k
        out[3][0] = t[1][0];           out[3][1] = t[1][1];
407
5.27k
        out[4][0] = t[2][0] + t[5][0]; out[4][1] = t[2][1] + t[5][1];
408
5.27k
        out[5][0] = t[3][0] + t[4][0]; out[5][1] = t[3][1] + t[4][1];
409
5.27k
        ps_split2_slot(out + 6, ps->in_buf[1] + n, 1);
410
5.27k
        ps_split2_slot(out + 8, ps->in_buf[2] + n, 0);
411
326k
        for (int k = 3; k < 64; k++) { out[k + 7][0] = X[k][0]; out[k + 7][1] = X[k][1]; }
412
5.27k
    }
413
8.47k
}
414
415
/* Hybrid synthesis of one slot: the sub-bands of a split QMF band sum
416
 * back, the others pass through. */
417
static void ps_hybrid_synthesis_slot(float in[PS_NR_BANDS][2], float out[64][2], bool is34)
418
16.9k
{
419
16.9k
    if (is34) {
420
6.39k
        static const uint8_t counts[5] = { 12, 8, 4, 4, 4 };
421
6.39k
        int k = 0;
422
38.3k
        for (int b = 0; b < 5; b++) {
423
31.9k
            float sr = 0.0f, si = 0.0f;
424
236k
            for (int q = 0; q < counts[b]; q++, k++) { sr += in[k][0]; si += in[k][1]; }
425
31.9k
            out[b][0] = sr; out[b][1] = si;
426
31.9k
        }
427
383k
        for (int b = 5; b < 64; b++) { out[b][0] = in[b + 27][0]; out[b][1] = in[b + 27][1]; }
428
10.5k
    } else {
429
10.5k
        float sr = 0.0f, si = 0.0f;
430
73.8k
        for (int q = 0; q < 6; q++) { sr += in[q][0]; si += in[q][1]; }
431
10.5k
        out[0][0] = sr; out[0][1] = si;
432
10.5k
        out[1][0] = in[6][0] + in[7][0]; out[1][1] = in[6][1] + in[7][1];
433
10.5k
        out[2][0] = in[8][0] + in[9][0]; out[2][1] = in[8][1] + in[9][1];
434
653k
        for (int b = 3; b < 64; b++) { out[b][0] = in[b + 7][0]; out[b][1] = in[b + 7][1]; }
435
10.5k
    }
436
16.9k
}
437
438
/* Decorrelation (§8.6.4.6) */
439
440
/* Transient detection for one slot: per parameter band, a peak-decay
441
 * envelope against smoothed power gives the gain G_tr(b). */
442
static void ps_transient_gain_slot(PSState *ps, float s[PS_NR_BANDS][2], float gain[PS_NR_PAR])
443
8.47k
{
444
8.47k
    const float peak_decay = 0.76592833836465f, impact = 1.5f, alpha = 0.25f;
445
8.47k
    bool is34 = ps->is34;
446
8.47k
    const int8_t *k_to_b = is34 ? ps_k_to_b_34 : ps_k_to_b_20;
447
448
8.47k
    float power[PS_NR_PAR] = { 0 };
449
673k
    for (int k = 0; k < ps_nr_bands[is34]; k++) power[k_to_b[k]] += s[k][0] * s[k][0] + s[k][1] * s[k][1];
450
222k
    for (int b = 0; b < ps_nr_par_bands[is34]; b++) {
451
214k
        float pw = power[b];
452
214k
        float peak = ps->peak_decay_nrg[b] * peak_decay;
453
214k
        if (peak < pw) peak = pw;
454
214k
        ps->peak_decay_nrg[b] = peak;
455
214k
        ps->power_smooth[b] += alpha * (pw - ps->power_smooth[b]);
456
214k
        ps->peak_decay_diff_smooth[b] += alpha * (peak - pw - ps->peak_decay_diff_smooth[b]);
457
214k
        float thr = impact * ps->peak_decay_diff_smooth[b];
458
214k
        gain[b] = (thr > ps->power_smooth[b]) ? ps->power_smooth[b] / thr : 1.0f;
459
214k
    }
460
8.47k
}
461
462
/* One slot through
463
 *   H_k(z) = z^-2 phi_k prod_m (Q_m z^-d_m - a_m g_k) / (1 - a_m g_k Q_m z^-d_m)
464
 * for the all-pass bands (link delays 3, 4, 5, each in direct form II so its
465
 * state is d_m samples in a ring), and through a 14- or 1-slot delay for the
466
 * rest; then scaled by the transient gain of the band's parameter band.
467
 * The rings are indexed by slot counters shared by all bands. */
468
static void ps_decorrelate_slot(PSState *ps, float s[PS_NR_BANDS][2], float d[PS_NR_BANDS][2])
469
8.47k
{
470
8.47k
    static const float a[3] = { 0.65143905753106f, 0.56471812200776f, 0.48954165955695f };
471
8.47k
    static const int link_delay[3] = { 3, 4, 5 };
472
8.47k
    bool is34 = ps->is34;
473
8.47k
    const int8_t *k_to_b = is34 ? ps_k_to_b_34 : ps_k_to_b_20;
474
8.47k
    const int nr_allpass = ps_nr_allpass[is34];
475
8.47k
    float gain[PS_NR_PAR];
476
477
8.47k
    ps_transient_gain_slot(ps, s, gain);
478
479
8.47k
    int k = 0;
480
326k
    for (; k < nr_allpass; k++) {
481
318k
        float slope = 1.0f - PS_DECAY_SLOPE * (float)(k - ps_decay_cutoff[is34]);
482
318k
        if (slope < 0.0f) slope = 0.0f;
483
318k
        if (slope > 1.0f) slope = 1.0f;
484
485
        /* two-slot input delay, then the fractional-delay phase */
486
318k
        float (*in)[2] = ps->dc_in[k];
487
318k
        float xr = in[0][0], xi = in[0][1];
488
318k
        in[0][0] = in[1][0]; in[0][1] = in[1][1];
489
318k
        in[1][0] = s[k][0];  in[1][1] = s[k][1];
490
318k
        const float *phi = ps_phi_fract[is34][k];
491
318k
        float ur = xr * phi[0] - xi * phi[1];
492
318k
        float ui = xr * phi[1] + xi * phi[0];
493
494
318k
        float (*st)[2] = ps->dc_ap[k];
495
1.27M
        for (int m = 0; m < 3; m++) {
496
954k
            const float ag = a[m] * slope;
497
954k
            const float *q = ps_q_fract[is34][k][m];
498
954k
            float (*v)[2] = st + ps->ap_pos[m]; /* v(n - d_m) */
499
954k
            float tr = v[0][0] * q[0] - v[0][1] * q[1]; /* Q v(n-d) */
500
954k
            float ti = v[0][0] * q[1] + v[0][1] * q[0];
501
954k
            float vr = ur + ag * tr, vi = ui + ag * ti;   /* v(n) */
502
954k
            ur = tr - ag * vr;                            /* link output */
503
954k
            ui = ti - ag * vi;
504
954k
            v[0][0] = vr; v[0][1] = vi;
505
954k
            st += link_delay[m];
506
954k
        }
507
318k
        float g = gain[k_to_b[k]];
508
318k
        d[k][0] = g * ur;
509
318k
        d[k][1] = g * ui;
510
318k
    }
511
355k
    for (; k < ps_nr_bands[is34]; k++) {
512
347k
        int dl = (k < ps_short_delay_band[is34]) ? PS_MAX_DELAY : 1;
513
347k
        float (*line)[2] = ps->dc_delay[k - nr_allpass];
514
347k
        int rd = (ps->dl_pos + PS_MAX_DELAY - dl) % PS_MAX_DELAY;
515
347k
        float g = gain[k_to_b[k]];
516
347k
        d[k][0] = g * line[rd][0];
517
347k
        d[k][1] = g * line[rd][1];
518
347k
        line[ps->dl_pos][0] = s[k][0];
519
347k
        line[ps->dl_pos][1] = s[k][1];
520
347k
    }
521
33.8k
    for (int m = 0; m < 3; m++) ps->ap_pos[m] = (uint8_t)((ps->ap_pos[m] + 1) % link_delay[m]);
522
8.47k
    ps->dl_pos = (uint8_t)((ps->dl_pos + 1) % PS_MAX_DELAY);
523
8.47k
}
524
525
/* Parameter band layouts */
526
527
/* The 10, 20 and 34-band layouts meet when a frame switches layout or
528
 * codes IPD/OPD on fewer bands: parameters and the previous frame's mixing
529
 * matrix have to be read on another grid. The 10-band layout pairs the
530
 * 20-band one. Between 20 and 34 bands, a target band takes the mean of the
531
 * source bands whose center frequency falls inside it, or the nearest one;
532
 * those maps are built from the band centers at start. */
533
typedef struct { uint8_t first, count; } PSBandMap;
534
static PSBandMap ps_map_34_20[20], ps_map_20_34[34];
535
536
static void ps_layout_centers(bool is34, float *center)
537
4
{
538
4
    const int8_t *k_to_b = is34 ? ps_k_to_b_34 : ps_k_to_b_20;
539
4
    int count[PS_NR_PAR] = { 0 };
540
112
    for (int b = 0; b < ps_nr_par_bands[is34]; b++) center[b] = 0.0f;
541
328
    for (int k = 0; k < ps_nr_bands[is34]; k++) {
542
324
        float f = !is34 ? ((k < 10) ? ps_f_center_20[k] / 8.0f : k - 6.5f)
543
324
                        : ((k < 32) ? ps_f_center_34[k] / 24.0f : k - 26.5f);
544
324
        center[k_to_b[k]] += fabsf(f);
545
324
        count[k_to_b[k]]++;
546
324
    }
547
112
    for (int b = 0; b < ps_nr_par_bands[is34]; b++) center[b] /= (float)count[b];
548
4
}
549
550
static void ps_build_band_map(PSBandMap *map, const float *src, int n_src, const float *dst, int n_dst)
551
4
{
552
112
    for (int j = 0; j < n_dst; j++) {
553
108
        float lo = j ? 0.5f * (dst[j - 1] + dst[j]) : 0.0f;
554
108
        float hi = (j + 1 < n_dst) ? 0.5f * (dst[j] + dst[j + 1]) : 1e9f;
555
108
        int first = -1, count = 0;
556
2.82k
        for (int i = 0; i < n_src; i++) {
557
2.72k
            if (src[i] < lo || src[i] >= hi) continue;
558
108
            if (first < 0) first = i;
559
108
            count++;
560
108
        }
561
108
        if (count == 0) {
562
28
            first = 0;
563
560
            for (int i = 1; i < n_src; i++)
564
532
                if (fabsf(src[i] - dst[j]) < fabsf(src[first] - dst[j])) first = i;
565
28
            count = 1;
566
28
        }
567
108
        map[j].first = (uint8_t)first;
568
108
        map[j].count = (uint8_t)count;
569
108
    }
570
4
}
571
572
static void ps_init_band_maps(void)
573
2
{
574
2
    float c20[PS_NR_PAR], c34[PS_NR_PAR];
575
2
    ps_layout_centers(false, c20);
576
2
    ps_layout_centers(true, c34);
577
2
    ps_build_band_map(ps_map_34_20, c34, 34, c20, 20);
578
2
    ps_build_band_map(ps_map_20_34, c20, 20, c34, 34);
579
2
}
580
581
/* Reads band j of the target layout from src, which has n_src bands of the
582
 * source layout (10, 20 or 34 bands, or the low 5, 11 or 17 of them). */
583
static float ps_band_read(const float *src, int n_src, int src_layout, int dst_layout, int j)
584
52.6k
{
585
52.6k
    if (src_layout == dst_layout) return j < n_src ? src[j] : 0.0f;
586
44.9k
    if (src_layout == 10) {
587
19.6k
        if (dst_layout == 20) return (j >> 1) < n_src ? src[j >> 1] : 0.0f;
588
        /* 10 -> 34 through the 20-band grid */
589
3.91k
        const PSBandMap *m = &ps_map_20_34[j];
590
3.91k
        float sum = 0.0f; int n = 0;
591
7.82k
        for (int i = m->first; i < m->first + m->count; i++)
592
3.91k
            if ((i >> 1) < n_src) { sum += src[i >> 1]; n++; }
593
3.91k
        return n ? sum / (float)n : 0.0f;
594
19.6k
    }
595
25.2k
    const PSBandMap *m = (src_layout == 34) ? &ps_map_34_20[j] : &ps_map_20_34[j];
596
25.2k
    float sum = 0.0f; int n = 0;
597
53.9k
    for (int i = m->first; i < m->first + m->count && i < n_src; i++) { sum += src[i]; n++; }
598
25.2k
    return n ? sum / (float)n : 0.0f;
599
44.9k
}
600
601
690
static int ps_layout_of(int n_par) { return n_par >= 34 || n_par == 17 ? 34 : n_par >= 20 || n_par == 11 ? 20 : 10; }
602
603
/* Parameters of every envelope onto this frame's grid (full: all its bands,
604
 * else the low IPD/OPD subset). */
605
static void ps_remap(int8_t dst[PS_MAX_ENV][PS_NR_PAR], int8_t src[PS_MAX_ENV][PS_NR_PAR], int n_src, int num_env, bool full, bool is34)
606
690
{
607
690
    int dst_layout = is34 ? 34 : 20;
608
690
    int n_dst = full ? dst_layout : ps_nr_ipdopd_bands[is34];
609
690
    int src_layout = ps_layout_of(n_src);
610
2.10k
    for (int e = 0; e < num_env; e++) {
611
1.41k
        float v[PS_NR_PAR];
612
14.4k
        for (int i = 0; i < n_src; i++) v[i] = (float)src[e][i];
613
31.4k
        for (int j = 0; j < n_dst; j++) {
614
30.0k
            float x = ps_band_read(v, n_src, src_layout, dst_layout, j);
615
30.0k
            dst[e][j] = (int8_t)(x >= 0.0f ? (int)(x + 0.5f) : -(int)(0.5f - x));
616
30.0k
        }
617
1.41k
    }
618
690
}
619
620
/* The previous frame's mixing matrix row onto the other grid. */
621
static void ps_remap_values(float *p, bool to34)
622
736
{
623
736
    float src[PS_NR_PAR];
624
736
    memcpy(src, p, sizeof(src));
625
736
    int n = to34 ? 34 : 20;
626
23.2k
    for (int j = 0; j < n; j++) p[j] = ps_band_read(src, to34 ? 20 : 34, to34 ? 20 : 34, to34 ? 34 : 20, j);
627
736
}
628
629
/* Stereo processing (§8.6.4.7) */
630
631
static void ps_pd_smooth(int pd0, int pd1, int pd2, float *re, float *im)
632
3.79k
{
633
3.79k
    static const float c[8] = { 1.0f, 0.70710678f, 0.0f, -0.70710678f, -1.0f, -0.70710678f, 0.0f, 0.70710678f };
634
3.79k
    static const float s[8] = { 0.0f, 0.70710678f, 1.0f, 0.70710678f, 0.0f, -0.70710678f, -1.0f, -0.70710678f };
635
3.79k
    float r = 0.25f * c[pd0] + 0.5f * c[pd1] + c[pd2];
636
3.79k
    float i = 0.25f * s[pd0] + 0.5f * s[pd1] + s[pd2];
637
3.79k
    float mag = 1.0f / hypotf(r, i);
638
3.79k
    *re = r * mag;
639
3.79k
    *im = i * mag;
640
3.79k
}
641
642
/* The mixing matrix at every envelope border, from the IID/ICC (and IPD/OPD)
643
 * parameters; slots interpolate between borders. Entry [e][b] is the matrix
644
 * at the end of envelope e - 1, [0] carrying over from the previous frame. */
645
static void ps_mixing_matrices(PSState *ps)
646
274
{
647
274
    bool is34 = ps->is34;
648
274
    float (*H_LUT)[8][4] = (ps->icc_mode < 3) ? ps_HA : ps_HB;
649
274
    int8_t iid_m[PS_MAX_ENV][PS_NR_PAR], icc_m[PS_MAX_ENV][PS_NR_PAR], ipd_m[PS_MAX_ENV][PS_NR_PAR], opd_m[PS_MAX_ENV][PS_NR_PAR];
650
274
    float (*H)[2][PS_MAX_ENV + 1][PS_NR_PAR] = ps->H; /* [matrix entry][re/im][border][band] */
651
652
274
    if (ps->num_env_old) {
653
350
        for (int i = 0; i < 4; i++)
654
840
            for (int c = 0; c < 2; c++)
655
560
                memcpy(H[i][c][0], H[i][c][ps->num_env_old], sizeof(H[i][c][0]));
656
70
    }
657
274
    ps_remap(iid_m, ps->iid_par, ps->nr_iid_par, ps->num_env, true, is34);
658
274
    ps_remap(icc_m, ps->icc_par, ps->nr_icc_par, ps->num_env, true, is34);
659
274
    if (ps->enable_ipdopd) {
660
71
        ps_remap(ipd_m, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, false, is34);
661
71
        ps_remap(opd_m, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, false, is34);
662
71
    }
663
274
    if (is34 != ps->is34_old) {
664
460
        for (int i = 0; i < 4; i++)
665
1.10k
            for (int c = 0; c < 2; c++) {
666
736
                ps_remap_values(H[i][c][0], is34);
667
736
            }
668
92
        memset(ps->ipd_hist, 0, sizeof(ps->ipd_hist));
669
92
        memset(ps->opd_hist, 0, sizeof(ps->opd_hist));
670
92
    }
671
672
829
    for (int e = 0; e < ps->num_env; e++) {
673
13.6k
        for (int b = 0; b < ps_nr_par_bands[is34]; b++) {
674
13.1k
            int iid = iid_m[e][b] + 7 + 23 * (ps->iid_quant ? 1 : 0);
675
13.1k
            int icc = icc_m[e][b] & 7;
676
13.1k
            if (iid < 0) iid = 0;
677
13.1k
            if (iid > 45) iid = 45;
678
13.1k
            float h[4] = { H_LUT[iid][icc][0], H_LUT[iid][icc][1], H_LUT[iid][icc][2], H_LUT[iid][icc][3] };
679
13.1k
            float hi[4] = { 0, 0, 0, 0 };
680
13.1k
            if (ps->enable_ipdopd && b < ps_nr_ipdopd_bands[is34]) {
681
1.89k
                int opd = opd_m[e][b] & 7, ipd = ipd_m[e][b] & 7;
682
1.89k
                float opd_re, opd_im, ipd_re, ipd_im;
683
1.89k
                ps_pd_smooth(ps->opd_hist[b] >> 3, ps->opd_hist[b] & 7, opd, &opd_re, &opd_im);
684
1.89k
                ps_pd_smooth(ps->ipd_hist[b] >> 3, ps->ipd_hist[b] & 7, ipd, &ipd_re, &ipd_im);
685
1.89k
                ps->opd_hist[b] = (int8_t)(((ps->opd_hist[b] & 7) << 3) | opd);
686
1.89k
                ps->ipd_hist[b] = (int8_t)(((ps->ipd_hist[b] & 7) << 3) | ipd);
687
1.89k
                float adj_re = opd_re * ipd_re + opd_im * ipd_im;
688
1.89k
                float adj_im = opd_im * ipd_re - opd_re * ipd_im;
689
1.89k
                hi[0] = h[0] * opd_im; h[0] *= opd_re;
690
1.89k
                hi[1] = h[1] * adj_im; h[1] *= adj_re;
691
1.89k
                hi[2] = h[2] * opd_im; h[2] *= opd_re;
692
1.89k
                hi[3] = h[3] * adj_im; h[3] *= adj_re;
693
1.89k
            }
694
65.7k
            for (int i = 0; i < 4; i++) { H[i][0][e + 1][b] = h[i]; H[i][1][e + 1][b] = hi[i]; }
695
13.1k
        }
696
555
    }
697
274
}
698
699
/* Mix one slot: slot n of envelope e (border[e] < n <= border[e+1]) uses
700
 * the matrix interpolated (n - border[e]) / (border[e+1] - border[e]) of
701
 * the way from border e to border e + 1. */
702
static void ps_mix_slot(PSState *ps, int n, float l[PS_NR_BANDS][2], float r[PS_NR_BANDS][2])
703
8.47k
{
704
8.47k
    bool is34 = ps->is34;
705
8.47k
    const int8_t *k_to_b = is34 ? ps_k_to_b_34 : ps_k_to_b_20;
706
8.47k
    float (*H)[2][PS_MAX_ENV + 1][PS_NR_PAR] = ps->H;
707
8.47k
    int e = 0;
708
12.9k
    while (e + 1 < ps->num_env && n > ps->border[e + 1]) e++;
709
8.47k
    int start = ps->border[e], stop = ps->border[e + 1];
710
8.47k
    if (stop <= start) return;
711
8.47k
    float t = (float)(n - start) / (float)(stop - start);
712
713
    /* the matrix varies per parameter band; interpolate it once per band */
714
8.47k
    float h[PS_NR_PAR][4], hI[PS_NR_PAR][4], hF[PS_NR_PAR][4];
715
222k
    for (int b = 0; b < ps_nr_par_bands[is34]; b++)
716
1.07M
        for (int i = 0; i < 4; i++) h[b][i] = H[i][0][e][b] + (H[i][0][e + 1][b] - H[i][0][e][b]) * t;
717
8.47k
    if (ps->enable_ipdopd) {
718
57.2k
        for (int b = 0; b < ps_nr_par_bands[is34]; b++)
719
275k
            for (int i = 0; i < 4; i++) {
720
220k
                hI[b][i] = H[i][1][e][b] + (H[i][1][e + 1][b] - H[i][1][e][b]) * t;
721
220k
                hF[b][i] = -H[i][1][e][b] + (H[i][1][e + 1][b] + H[i][1][e][b]) * t;
722
220k
            }
723
2.17k
    }
724
725
673k
    for (int k = 0; k < ps_nr_bands[is34]; k++) {
726
665k
        const float *hb = h[k_to_b[k]];
727
665k
        float lr = l[k][0], li = l[k][1], rr = r[k][0], ri = r[k][1];
728
665k
        if (!ps->enable_ipdopd) {
729
494k
            l[k][0] = hb[0] * lr + hb[2] * rr; l[k][1] = hb[0] * li + hb[2] * ri;
730
494k
            r[k][0] = hb[1] * lr + hb[3] * rr; r[k][1] = hb[1] * li + hb[3] * ri;
731
494k
        } else {
732
            /* the phase runs the other way in the bands whose sub-band
733
             * order the hybrid split reversed */
734
170k
            bool flip = (is34 && k >= 9 && k <= 13) || (!is34 && k <= 1);
735
170k
            const float *hi = flip ? hF[k_to_b[k]] : hI[k_to_b[k]];
736
170k
            l[k][0] = hb[0] * lr + hb[2] * rr - hi[0] * li - hi[2] * ri;
737
170k
            l[k][1] = hb[0] * li + hb[2] * ri + hi[0] * lr + hi[2] * rr;
738
170k
            r[k][0] = hb[1] * lr + hb[3] * rr - hi[1] * li - hi[3] * ri;
739
170k
            r[k][1] = hb[1] * li + hb[3] * ri + hi[1] * lr + hi[3] * rr;
740
170k
        }
741
665k
    }
742
8.47k
}
743
744
/* Stage the five hybrid bands without retaining the high-band frame. */
745
void ps_frame_input(struct faad_decoder *dec, int n, float X[64][2])
746
912
{
747
912
    PSState *ps = dec->ps;
748
5.47k
    for (int i = 0; i < 5; i++) {
749
4.56k
        ps->in_buf[i][n + 6][0] = X[i][0];
750
4.56k
        ps->in_buf[i][n + 6][1] = X[i][1];
751
4.56k
    }
752
912
}
753
754
/* Prepare mixing matrices and reset histories when the band layout changes. */
755
void ps_frame_begin(struct faad_decoder *dec, int top)
756
274
{
757
274
    PSState *ps = dec->ps;
758
274
    bool is34 = ps->is34;
759
274
    if (is34 != ps->is34_old) {
760
92
        memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));
761
92
        memset(ps->power_smooth, 0, sizeof(ps->power_smooth));
762
92
        memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
763
92
        memset(ps->dc_in, 0, sizeof(ps->dc_in));
764
92
        memset(ps->dc_ap, 0, sizeof(ps->dc_ap));
765
92
        memset(ps->dc_delay, 0, sizeof(ps->dc_delay));
766
92
    }
767
    /* bands above the SBR range carry nothing: keep their lines silent */
768
274
    {
769
274
        int na = ps_nr_allpass[is34], nb = ps_nr_bands[is34];
770
274
        int top_k = top + nb - 64;
771
274
        if (top_k < 0) top_k = 0;
772
274
        if (top_k < na) {
773
53
            memset(ps->dc_in + top_k, 0, sizeof(ps->dc_in[0]) * (size_t)(na - top_k));
774
53
            memset(ps->dc_ap + top_k, 0, sizeof(ps->dc_ap[0]) * (size_t)(na - top_k));
775
53
        }
776
274
        int td = top_k > na ? top_k - na : 0;
777
274
        if (td < nb - na) memset(ps->dc_delay + td, 0, sizeof(ps->dc_delay[0]) * (size_t)(nb - na - td));
778
274
    }
779
274
    ps_mixing_matrices(ps);
780
274
}
781
782
/* One slot: hybrid domain, decorrelated copy, mix, back to the QMF domain. */
783
void ps_slot(struct faad_decoder *dec, int n, float X[64][2], float L[64][2], float R[64][2])
784
8.47k
{
785
8.47k
    PSState *ps = dec->ps;
786
8.47k
    float l[PS_NR_BANDS][2], r[PS_NR_BANDS][2];
787
788
8.47k
    ps_hybrid_analysis_slot(ps, n, l, X);
789
8.47k
    ps_decorrelate_slot(ps, l, r);
790
8.47k
    ps_mix_slot(ps, n, l, r);
791
8.47k
    ps_hybrid_synthesis_slot(l, L, ps->is34);
792
8.47k
    ps_hybrid_synthesis_slot(r, R, ps->is34);
793
8.47k
    int num_qmf_slots = FRAME_IS_960(dec) ? 30 : 32;
794
8.47k
    if (n == num_qmf_slots - 1) {
795
1.64k
        for (int i = 0; i < 5; i++)
796
1.37k
            memmove(ps->in_buf[i], ps->in_buf[i] + num_qmf_slots, 6 * sizeof(ps->in_buf[i][0]));
797
274
        ps->is34_old = ps->is34;
798
274
    }
799
8.47k
}
800
801
#endif /* FAAD_DISABLE_PS */