GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/aacps.c Lines: 428 500 85.6 %
Date: 2021-01-21 21:11:50 Branches: 113 148 76.4 %

Line Branch Exec Source
1
/*
2
 * MPEG-4 Parametric Stereo decoding functions
3
 * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 *
21
 * Note: Rounding-to-nearest used unless otherwise stated
22
 *
23
 */
24
25
#include <stdint.h>
26
#include "libavutil/common.h"
27
#include "libavutil/mathematics.h"
28
#include "libavutil/mem_internal.h"
29
#include "avcodec.h"
30
#include "aacps.h"
31
#if USE_FIXED
32
#include "aacps_fixed_tablegen.h"
33
#else
34
#include "libavutil/internal.h"
35
#include "aacps_tablegen.h"
36
#endif /* USE_FIXED */
37
38
static const INTFLOAT g1_Q2[] = {
39
    Q31(0.0f),  Q31(0.01899487526049f), Q31(0.0f), Q31(-0.07293139167538f),
40
    Q31(0.0f),  Q31(0.30596630545168f), Q31(0.5f)
41
};
42
43
3
static void ipdopd_reset(int8_t *ipd_hist, int8_t *opd_hist)
44
{
45
    int i;
46
54
    for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
47
51
        opd_hist[i] = 0;
48
51
        ipd_hist[i] = 0;
49
    }
50
3
}
51
52
/** Split one subband into 2 subsubbands with a symmetric real filter.
53
 * The filter must have its non-center even coefficients equal to zero. */
54
3028
static void hybrid2_re(INTFLOAT (*in)[2], INTFLOAT (*out)[32][2], const INTFLOAT filter[8], int len, int reverse)
55
{
56
    int i, j;
57
99924
    for (i = 0; i < len; i++, in++) {
58
96896
        INT64FLOAT re_in = AAC_MUL31(filter[6], in[6][0]); //real inphase
59
96896
        INT64FLOAT re_op = 0.0f;                          //real out of phase
60
96896
        INT64FLOAT im_in = AAC_MUL31(filter[6], in[6][1]); //imag inphase
61
96896
        INT64FLOAT im_op = 0.0f;                          //imag out of phase
62
387584
        for (j = 0; j < 6; j += 2) {
63
290688
            re_op += (INT64FLOAT)filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
64
290688
            im_op += (INT64FLOAT)filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
65
        }
66
67
#if USE_FIXED
68
        re_op = (re_op + 0x40000000) >> 31;
69
        im_op = (im_op + 0x40000000) >> 31;
70
#endif /* USE_FIXED */
71
72
96896
        out[ reverse][i][0] = (INTFLOAT)(re_in + re_op);
73
96896
        out[ reverse][i][1] = (INTFLOAT)(im_in + im_op);
74
96896
        out[!reverse][i][0] = (INTFLOAT)(re_in - re_op);
75
96896
        out[!reverse][i][1] = (INTFLOAT)(im_in - im_op);
76
    }
77
3028
}
78
79
/** Split one subband into 6 subsubbands with a complex filter */
80
1514
static void hybrid6_cx(PSDSPContext *dsp, INTFLOAT (*in)[2], INTFLOAT (*out)[32][2],
81
                       TABLE_CONST INTFLOAT (*filter)[8][2], int len)
82
{
83
    int i;
84
1514
    int N = 8;
85
1514
    LOCAL_ALIGNED_16(INTFLOAT, temp, [8], [2]);
86
87
49962
    for (i = 0; i < len; i++, in++) {
88
48448
        dsp->hybrid_analysis(temp, in, (const INTFLOAT (*)[8][2]) filter, 1, N);
89
48448
        out[0][i][0] = temp[6][0];
90
48448
        out[0][i][1] = temp[6][1];
91
48448
        out[1][i][0] = temp[7][0];
92
48448
        out[1][i][1] = temp[7][1];
93
48448
        out[2][i][0] = temp[0][0];
94
48448
        out[2][i][1] = temp[0][1];
95
48448
        out[3][i][0] = temp[1][0];
96
48448
        out[3][i][1] = temp[1][1];
97
48448
        out[4][i][0] = temp[2][0] + temp[5][0];
98
48448
        out[4][i][1] = temp[2][1] + temp[5][1];
99
48448
        out[5][i][0] = temp[3][0] + temp[4][0];
100
48448
        out[5][i][1] = temp[3][1] + temp[4][1];
101
    }
102
1514
}
103
104
510
static void hybrid4_8_12_cx(PSDSPContext *dsp,
105
                            INTFLOAT (*in)[2], INTFLOAT (*out)[32][2],
106
                            TABLE_CONST INTFLOAT (*filter)[8][2], int N, int len)
107
{
108
    int i;
109
110
16830
    for (i = 0; i < len; i++, in++) {
111
16320
        dsp->hybrid_analysis(out[0] + i, in, (const INTFLOAT (*)[8][2]) filter, 32, N);
112
    }
113
510
}
114
115
1616
static void hybrid_analysis(PSDSPContext *dsp, INTFLOAT out[91][32][2],
116
                            INTFLOAT in[5][44][2], INTFLOAT L[2][38][64],
117
                            int is34, int len)
118
{
119
    int i, j;
120
9696
    for (i = 0; i < 5; i++) {
121
315120
        for (j = 0; j < 38; j++) {
122
307040
            in[i][j+6][0] = L[0][j][i];
123
307040
            in[i][j+6][1] = L[1][j][i];
124
        }
125
    }
126
1616
    if (is34) {
127
102
        hybrid4_8_12_cx(dsp, in[0], out,    f34_0_12, 12, len);
128
102
        hybrid4_8_12_cx(dsp, in[1], out+12, f34_1_8,   8, len);
129
102
        hybrid4_8_12_cx(dsp, in[2], out+20, f34_2_4,   4, len);
130
102
        hybrid4_8_12_cx(dsp, in[3], out+24, f34_2_4,   4, len);
131
102
        hybrid4_8_12_cx(dsp, in[4], out+28, f34_2_4,   4, len);
132
102
        dsp->hybrid_analysis_ileave(out + 27, L, 5, len);
133
    } else {
134
1514
        hybrid6_cx(dsp, in[0], out, f20_0_8, len);
135
1514
        hybrid2_re(in[1], out+6, g1_Q2, len, 1);
136
1514
        hybrid2_re(in[2], out+8, g1_Q2, len, 0);
137
1514
        dsp->hybrid_analysis_ileave(out + 7, L, 3, len);
138
    }
139
    //update in_buf
140
9696
    for (i = 0; i < 5; i++) {
141
8080
        memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
142
    }
143
1616
}
144
145
3232
static void hybrid_synthesis(PSDSPContext *dsp, INTFLOAT out[2][38][64],
146
                             INTFLOAT in[91][32][2], int is34, int len)
147
{
148
    int i, n;
149
3232
    if (is34) {
150
6732
        for (n = 0; n < len; n++) {
151
6528
            memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
152
6528
            memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
153
84864
            for (i = 0; i < 12; i++) {
154
78336
                out[0][n][0] += (UINTFLOAT)in[   i][n][0];
155
78336
                out[1][n][0] += (UINTFLOAT)in[   i][n][1];
156
            }
157
58752
            for (i = 0; i < 8; i++) {
158
52224
                out[0][n][1] += (UINTFLOAT)in[12+i][n][0];
159
52224
                out[1][n][1] += (UINTFLOAT)in[12+i][n][1];
160
            }
161
32640
            for (i = 0; i < 4; i++) {
162
26112
                out[0][n][2] += (UINTFLOAT)in[20+i][n][0];
163
26112
                out[1][n][2] += (UINTFLOAT)in[20+i][n][1];
164
26112
                out[0][n][3] += (UINTFLOAT)in[24+i][n][0];
165
26112
                out[1][n][3] += (UINTFLOAT)in[24+i][n][1];
166
26112
                out[0][n][4] += (UINTFLOAT)in[28+i][n][0];
167
26112
                out[1][n][4] += (UINTFLOAT)in[28+i][n][1];
168
            }
169
        }
170
204
        dsp->hybrid_synthesis_deint(out, in + 27, 5, len);
171
    } else {
172
99924
        for (n = 0; n < len; n++) {
173
96896
            out[0][n][0] = (UINTFLOAT)in[0][n][0] + in[1][n][0] + in[2][n][0] +
174
96896
                           (UINTFLOAT)in[3][n][0] + in[4][n][0] + in[5][n][0];
175
96896
            out[1][n][0] = (UINTFLOAT)in[0][n][1] + in[1][n][1] + in[2][n][1] +
176
96896
                           (UINTFLOAT)in[3][n][1] + in[4][n][1] + in[5][n][1];
177
96896
            out[0][n][1] = (UINTFLOAT)in[6][n][0] + in[7][n][0];
178
96896
            out[1][n][1] = (UINTFLOAT)in[6][n][1] + in[7][n][1];
179
96896
            out[0][n][2] = (UINTFLOAT)in[8][n][0] + in[9][n][0];
180
96896
            out[1][n][2] = (UINTFLOAT)in[8][n][1] + in[9][n][1];
181
        }
182
3028
        dsp->hybrid_synthesis_deint(out, in + 7, 3, len);
183
    }
184
3232
}
185
186
/// All-pass filter decay slope
187
#define DECAY_SLOPE      Q30(0.05f)
188
/// Number of frequency bands that can be addressed by the parameter index, b(k)
189
static const int   NR_PAR_BANDS[]      = { 20, 34 };
190
static const int   NR_IPDOPD_BANDS[]   = { 11, 17 };
191
/// Number of frequency bands that can be addressed by the sub subband index, k
192
static const int   NR_BANDS[]          = { 71, 91 };
193
/// Start frequency band for the all-pass filter decay slope
194
static const int   DECAY_CUTOFF[]      = { 10, 32 };
195
/// Number of all-pass filer bands
196
static const int   NR_ALLPASS_BANDS[]  = { 30, 50 };
197
/// First stereo band using the short one sample delay
198
static const int   SHORT_DELAY_BAND[]  = { 42, 62 };
199
200
/** Table 8.46 */
201
1
static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
202
{
203
    int b;
204
1
    if (full)
205
1
        b = 9;
206
    else {
207
        b = 4;
208
        par_mapped[10] = 0;
209
    }
210
11
    for (; b >= 0; b--) {
211
10
        par_mapped[2*b+1] = par_mapped[2*b] = par[b];
212
    }
213
1
}
214
215
1
static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
216
{
217
1
    par_mapped[ 0] = (2*par[ 0] +   par[ 1]) / 3;
218
1
    par_mapped[ 1] = (  par[ 1] + 2*par[ 2]) / 3;
219
1
    par_mapped[ 2] = (2*par[ 3] +   par[ 4]) / 3;
220
1
    par_mapped[ 3] = (  par[ 4] + 2*par[ 5]) / 3;
221
1
    par_mapped[ 4] = (  par[ 6] +   par[ 7]) / 2;
222
1
    par_mapped[ 5] = (  par[ 8] +   par[ 9]) / 2;
223
1
    par_mapped[ 6] =    par[10];
224
1
    par_mapped[ 7] =    par[11];
225
1
    par_mapped[ 8] = (  par[12] +   par[13]) / 2;
226
1
    par_mapped[ 9] = (  par[14] +   par[15]) / 2;
227
1
    par_mapped[10] =    par[16];
228
1
    if (full) {
229
1
        par_mapped[11] =    par[17];
230
1
        par_mapped[12] =    par[18];
231
1
        par_mapped[13] =    par[19];
232
1
        par_mapped[14] = (  par[20] +   par[21]) / 2;
233
1
        par_mapped[15] = (  par[22] +   par[23]) / 2;
234
1
        par_mapped[16] = (  par[24] +   par[25]) / 2;
235
1
        par_mapped[17] = (  par[26] +   par[27]) / 2;
236
1
        par_mapped[18] = (  par[28] +   par[29] +   par[30] +   par[31]) / 4;
237
1
        par_mapped[19] = (  par[32] +   par[33]) / 2;
238
    }
239
1
}
240
241
8
static void map_val_34_to_20(INTFLOAT par[PS_MAX_NR_IIDICC])
242
{
243
#if USE_FIXED
244
    par[ 0] = (int)(((int64_t)(par[ 0] + (unsigned)(par[ 1]>>1)) * 1431655765 + \
245
                      0x40000000) >> 31);
246
    par[ 1] = (int)(((int64_t)((par[ 1]>>1) + (unsigned)par[ 2]) * 1431655765 + \
247
                      0x40000000) >> 31);
248
    par[ 2] = (int)(((int64_t)(par[ 3] + (unsigned)(par[ 4]>>1)) * 1431655765 + \
249
                      0x40000000) >> 31);
250
    par[ 3] = (int)(((int64_t)((par[ 4]>>1) + (unsigned)par[ 5]) * 1431655765 + \
251
                      0x40000000) >> 31);
252
#else
253
8
    par[ 0] = (2*par[ 0] +   par[ 1]) * 0.33333333f;
254
8
    par[ 1] = (  par[ 1] + 2*par[ 2]) * 0.33333333f;
255
8
    par[ 2] = (2*par[ 3] +   par[ 4]) * 0.33333333f;
256
8
    par[ 3] = (  par[ 4] + 2*par[ 5]) * 0.33333333f;
257
#endif /* USE_FIXED */
258
8
    par[ 4] = AAC_HALF_SUM(par[ 6], par[ 7]);
259
8
    par[ 5] = AAC_HALF_SUM(par[ 8], par[ 9]);
260
8
    par[ 6] =    par[10];
261
8
    par[ 7] =    par[11];
262
8
    par[ 8] = AAC_HALF_SUM(par[12], par[13]);
263
8
    par[ 9] = AAC_HALF_SUM(par[14], par[15]);
264
8
    par[10] =    par[16];
265
8
    par[11] =    par[17];
266
8
    par[12] =    par[18];
267
8
    par[13] =    par[19];
268
8
    par[14] = AAC_HALF_SUM(par[20], par[21]);
269
8
    par[15] = AAC_HALF_SUM(par[22], par[23]);
270
8
    par[16] = AAC_HALF_SUM(par[24], par[25]);
271
8
    par[17] = AAC_HALF_SUM(par[26], par[27]);
272
#if USE_FIXED
273
    par[18] = (((par[28]+2)>>2) + ((par[29]+2)>>2) + ((par[30]+2)>>2) + ((par[31]+2)>>2));
274
#else
275
8
    par[18] = (  par[28] +   par[29] +   par[30] +   par[31]) * 0.25f;
276
#endif /* USE_FIXED */
277
8
    par[19] = AAC_HALF_SUM(par[32], par[33]);
278
8
}
279
280
204
static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
281
{
282
204
    if (full) {
283
204
        par_mapped[33] = par[9];
284
204
        par_mapped[32] = par[9];
285
204
        par_mapped[31] = par[9];
286
204
        par_mapped[30] = par[9];
287
204
        par_mapped[29] = par[9];
288
204
        par_mapped[28] = par[9];
289
204
        par_mapped[27] = par[8];
290
204
        par_mapped[26] = par[8];
291
204
        par_mapped[25] = par[8];
292
204
        par_mapped[24] = par[8];
293
204
        par_mapped[23] = par[7];
294
204
        par_mapped[22] = par[7];
295
204
        par_mapped[21] = par[7];
296
204
        par_mapped[20] = par[7];
297
204
        par_mapped[19] = par[6];
298
204
        par_mapped[18] = par[6];
299
204
        par_mapped[17] = par[5];
300
204
        par_mapped[16] = par[5];
301
    } else {
302
        par_mapped[16] =      0;
303
    }
304
204
    par_mapped[15] = par[4];
305
204
    par_mapped[14] = par[4];
306
204
    par_mapped[13] = par[4];
307
204
    par_mapped[12] = par[4];
308
204
    par_mapped[11] = par[3];
309
204
    par_mapped[10] = par[3];
310
204
    par_mapped[ 9] = par[2];
311
204
    par_mapped[ 8] = par[2];
312
204
    par_mapped[ 7] = par[2];
313
204
    par_mapped[ 6] = par[2];
314
204
    par_mapped[ 5] = par[1];
315
204
    par_mapped[ 4] = par[1];
316
204
    par_mapped[ 3] = par[1];
317
204
    par_mapped[ 2] = par[0];
318
204
    par_mapped[ 1] = par[0];
319
204
    par_mapped[ 0] = par[0];
320
204
}
321
322
static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
323
{
324
    if (full) {
325
        par_mapped[33] =  par[19];
326
        par_mapped[32] =  par[19];
327
        par_mapped[31] =  par[18];
328
        par_mapped[30] =  par[18];
329
        par_mapped[29] =  par[18];
330
        par_mapped[28] =  par[18];
331
        par_mapped[27] =  par[17];
332
        par_mapped[26] =  par[17];
333
        par_mapped[25] =  par[16];
334
        par_mapped[24] =  par[16];
335
        par_mapped[23] =  par[15];
336
        par_mapped[22] =  par[15];
337
        par_mapped[21] =  par[14];
338
        par_mapped[20] =  par[14];
339
        par_mapped[19] =  par[13];
340
        par_mapped[18] =  par[12];
341
        par_mapped[17] =  par[11];
342
    }
343
    par_mapped[16] =  par[10];
344
    par_mapped[15] =  par[ 9];
345
    par_mapped[14] =  par[ 9];
346
    par_mapped[13] =  par[ 8];
347
    par_mapped[12] =  par[ 8];
348
    par_mapped[11] =  par[ 7];
349
    par_mapped[10] =  par[ 6];
350
    par_mapped[ 9] =  par[ 5];
351
    par_mapped[ 8] =  par[ 5];
352
    par_mapped[ 7] =  par[ 4];
353
    par_mapped[ 6] =  par[ 4];
354
    par_mapped[ 5] =  par[ 3];
355
    par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
356
    par_mapped[ 3] =  par[ 2];
357
    par_mapped[ 2] =  par[ 1];
358
    par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
359
    par_mapped[ 0] =  par[ 0];
360
}
361
362
16
static void map_val_20_to_34(INTFLOAT par[PS_MAX_NR_IIDICC])
363
{
364
16
    par[33] =  par[19];
365
16
    par[32] =  par[19];
366
16
    par[31] =  par[18];
367
16
    par[30] =  par[18];
368
16
    par[29] =  par[18];
369
16
    par[28] =  par[18];
370
16
    par[27] =  par[17];
371
16
    par[26] =  par[17];
372
16
    par[25] =  par[16];
373
16
    par[24] =  par[16];
374
16
    par[23] =  par[15];
375
16
    par[22] =  par[15];
376
16
    par[21] =  par[14];
377
16
    par[20] =  par[14];
378
16
    par[19] =  par[13];
379
16
    par[18] =  par[12];
380
16
    par[17] =  par[11];
381
16
    par[16] =  par[10];
382
16
    par[15] =  par[ 9];
383
16
    par[14] =  par[ 9];
384
16
    par[13] =  par[ 8];
385
16
    par[12] =  par[ 8];
386
16
    par[11] =  par[ 7];
387
16
    par[10] =  par[ 6];
388
16
    par[ 9] =  par[ 5];
389
16
    par[ 8] =  par[ 5];
390
16
    par[ 7] =  par[ 4];
391
16
    par[ 6] =  par[ 4];
392
16
    par[ 5] =  par[ 3];
393
16
    par[ 4] = AAC_HALF_SUM(par[ 2], par[ 3]);
394
16
    par[ 3] =  par[ 2];
395
16
    par[ 2] =  par[ 1];
396
16
    par[ 1] = AAC_HALF_SUM(par[ 0], par[ 1]);
397
16
}
398
399
1616
static void decorrelation(PSContext *ps, INTFLOAT (*out)[32][2], const INTFLOAT (*s)[32][2], int is34)
400
{
401
1616
    LOCAL_ALIGNED_16(INTFLOAT, power, [34], [PS_QMF_TIME_SLOTS]);
402
1616
    LOCAL_ALIGNED_16(INTFLOAT, transient_gain, [34], [PS_QMF_TIME_SLOTS]);
403
1616
    INTFLOAT *peak_decay_nrg = ps->peak_decay_nrg;
404
1616
    INTFLOAT *power_smooth = ps->power_smooth;
405
1616
    INTFLOAT *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
406
1616
    INTFLOAT (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
407
1616
    INTFLOAT (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
408
#if !USE_FIXED
409
1616
    const float transient_impact  = 1.5f;
410
1616
    const float a_smooth          = 0.25f; ///< Smoothing coefficient
411
#endif /* USE_FIXED */
412
1616
    const int8_t *const k_to_i = is34 ? ff_k_to_i_34 : ff_k_to_i_20;
413
    int i, k, m, n;
414
1616
    int n0 = 0, nL = 32;
415
1616
    const INTFLOAT peak_decay_factor = Q31(0.76592833836465f);
416
417
1616
    memset(power, 0, 34 * sizeof(*power));
418
419
1616
    if (is34 != ps->common.is34bands_old) {
420
3
        memset(ps->peak_decay_nrg,         0, sizeof(ps->peak_decay_nrg));
421
3
        memset(ps->power_smooth,           0, sizeof(ps->power_smooth));
422
3
        memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
423
3
        memset(ps->delay,                  0, sizeof(ps->delay));
424
3
        memset(ps->ap_delay,               0, sizeof(ps->ap_delay));
425
    }
426
427
118392
    for (k = 0; k < NR_BANDS[is34]; k++) {
428
116776
        int i = k_to_i[k];
429
116776
        ps->dsp.add_squares(power[i], s[k], nL - n0);
430
    }
431
432
    //Transient detection
433
#if USE_FIXED
434
    for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
435
        for (n = n0; n < nL; n++) {
436
            int decayed_peak;
437
            decayed_peak = (int)(((int64_t)peak_decay_factor * \
438
                                           peak_decay_nrg[i] + 0x40000000) >> 31);
439
            peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
440
            power_smooth[i] += (power[i][n] + 2LL - power_smooth[i]) >> 2;
441
            peak_decay_diff_smooth[i] += (peak_decay_nrg[i] + 2LL - power[i][n] - \
442
                                          peak_decay_diff_smooth[i]) >> 2;
443
444
            if (peak_decay_diff_smooth[i]) {
445
                transient_gain[i][n] = FFMIN(power_smooth[i]*43691LL / peak_decay_diff_smooth[i], 1<<16);
446
            } else
447
                transient_gain[i][n] = 1 << 16;
448
        }
449
    }
450
#else
451
35364
    for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
452
1113684
        for (n = n0; n < nL; n++) {
453
1079936
            float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
454
            float denom;
455
1079936
            peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
456
1079936
            power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
457
1079936
            peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
458
1079936
            denom = transient_impact * peak_decay_diff_smooth[i];
459
1079936
            transient_gain[i][n]   = (denom > power_smooth[i]) ?
460
1079936
                                         power_smooth[i] / denom : 1.0f;
461
        }
462
    }
463
464
#endif /* USE_FIXED */
465
    //Decorrelation and transient reduction
466
    //                         PS_AP_LINKS - 1
467
    //                               -----
468
    //                                | |  Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
469
    //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
470
    //                                | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
471
    //                               m = 0
472
    //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
473
52136
    for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
474
50520
        int b = k_to_i[k];
475
#if USE_FIXED
476
        int g_decay_slope;
477
478
        if (k - DECAY_CUTOFF[is34] <= 0) {
479
          g_decay_slope = 1 << 30;
480
        }
481
        else if (k - DECAY_CUTOFF[is34] >= 20) {
482
          g_decay_slope = 0;
483
        }
484
        else {
485
          g_decay_slope = (1 << 30) - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
486
        }
487
#else
488
50520
        float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
489
50520
        g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
490
#endif /* USE_FIXED */
491
50520
        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
492
50520
        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
493
202080
        for (m = 0; m < PS_AP_LINKS; m++) {
494
151560
            memcpy(ap_delay[k][m],   ap_delay[k][m]+numQMFSlots,           5*sizeof(ap_delay[k][m][0]));
495
        }
496
50520
        ps->dsp.decorrelate(out[k], delay[k] + PS_MAX_DELAY - 2, ap_delay[k],
497
50520
                            phi_fract[is34][k],
498
50520
                            (const INTFLOAT (*)[2]) Q_fract_allpass[is34][k],
499
50520
                            transient_gain[b], g_decay_slope, nL - n0);
500
    }
501
21008
    for (; k < SHORT_DELAY_BAND[is34]; k++) {
502
19392
        int i = k_to_i[k];
503
19392
        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
504
19392
        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
505
        //H = delay 14
506
19392
        ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 14,
507
19392
                                transient_gain[i], nL - n0);
508
    }
509
48480
    for (; k < NR_BANDS[is34]; k++) {
510
46864
        int i = k_to_i[k];
511
46864
        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
512
46864
        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
513
        //H = delay 1
514
46864
        ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 1,
515
46864
                                transient_gain[i], nL - n0);
516
    }
517
1616
}
518
519
204
static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
520
                    int8_t           (*par)[PS_MAX_NR_IIDICC],
521
                    int num_par, int num_env, int full)
522
{
523
204
    int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
524
    int e;
525

204
    if (num_par == 20 || num_par == 11) {
526
        for (e = 0; e < num_env; e++) {
527
            map_idx_20_to_34(par_mapped[e], par[e], full);
528
        }
529

204
    } else if (num_par == 10 || num_par == 5) {
530
306
        for (e = 0; e < num_env; e++) {
531
204
            map_idx_10_to_34(par_mapped[e], par[e], full);
532
        }
533
    } else {
534
102
        *p_par_mapped = par;
535
    }
536
204
}
537
538
3134
static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
539
                    int8_t           (*par)[PS_MAX_NR_IIDICC],
540
                    int num_par, int num_env, int full)
541
{
542
3134
    int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
543
    int e;
544

3134
    if (num_par == 34 || num_par == 17) {
545
2
        for (e = 0; e < num_env; e++) {
546
1
            map_idx_34_to_20(par_mapped[e], par[e], full);
547
        }
548

3133
    } else if (num_par == 10 || num_par == 5) {
549
2
        for (e = 0; e < num_env; e++) {
550
1
            map_idx_10_to_20(par_mapped[e], par[e], full);
551
        }
552
    } else {
553
3132
        *p_par_mapped = par;
554
    }
555
3134
}
556
557
1616
static void stereo_processing(PSContext *ps, INTFLOAT (*l)[32][2], INTFLOAT (*r)[32][2], int is34)
558
{
559
    int e, b, k;
560
561
1616
    PSCommonContext *const ps2 = &ps->common;
562
1616
    INTFLOAT (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
563
1616
    INTFLOAT (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
564
1616
    INTFLOAT (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
565
1616
    INTFLOAT (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
566
1616
    int8_t *opd_hist = ps->opd_hist;
567
1616
    int8_t *ipd_hist = ps->ipd_hist;
568
    int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
569
    int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
570
    int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
571
    int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
572
1616
    int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
573
1616
    int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
574
1616
    int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
575
1616
    int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
576
1616
    const int8_t *const k_to_i = is34 ? ff_k_to_i_34 : ff_k_to_i_20;
577
1616
    TABLE_CONST INTFLOAT (*H_LUT)[8][4] = (PS_BASELINE || ps2->icc_mode < 3) ? HA : HB;
578
579
    //Remapping
580
1616
    if (ps2->num_env_old) {
581
1590
        memcpy(H11[0][0], H11[0][ps2->num_env_old], sizeof(H11[0][0]));
582
1590
        memcpy(H11[1][0], H11[1][ps2->num_env_old], sizeof(H11[1][0]));
583
1590
        memcpy(H12[0][0], H12[0][ps2->num_env_old], sizeof(H12[0][0]));
584
1590
        memcpy(H12[1][0], H12[1][ps2->num_env_old], sizeof(H12[1][0]));
585
1590
        memcpy(H21[0][0], H21[0][ps2->num_env_old], sizeof(H21[0][0]));
586
1590
        memcpy(H21[1][0], H21[1][ps2->num_env_old], sizeof(H21[1][0]));
587
1590
        memcpy(H22[0][0], H22[0][ps2->num_env_old], sizeof(H22[0][0]));
588
1590
        memcpy(H22[1][0], H22[1][ps2->num_env_old], sizeof(H22[1][0]));
589
    }
590
591
1616
    if (is34) {
592
102
        remap34(&iid_mapped, ps2->iid_par, ps2->nr_iid_par, ps2->num_env, 1);
593
102
        remap34(&icc_mapped, ps2->icc_par, ps2->nr_icc_par, ps2->num_env, 1);
594
102
        if (ps2->enable_ipdopd) {
595
            remap34(&ipd_mapped, ps2->ipd_par, ps2->nr_ipdopd_par, ps2->num_env, 0);
596
            remap34(&opd_mapped, ps2->opd_par, ps2->nr_ipdopd_par, ps2->num_env, 0);
597
        }
598
102
        if (!ps2->is34bands_old) {
599
2
            map_val_20_to_34(H11[0][0]);
600
2
            map_val_20_to_34(H11[1][0]);
601
2
            map_val_20_to_34(H12[0][0]);
602
2
            map_val_20_to_34(H12[1][0]);
603
2
            map_val_20_to_34(H21[0][0]);
604
2
            map_val_20_to_34(H21[1][0]);
605
2
            map_val_20_to_34(H22[0][0]);
606
2
            map_val_20_to_34(H22[1][0]);
607
2
            ipdopd_reset(ipd_hist, opd_hist);
608
        }
609
    } else {
610
1514
        remap20(&iid_mapped, ps2->iid_par, ps2->nr_iid_par, ps2->num_env, 1);
611
1514
        remap20(&icc_mapped, ps2->icc_par, ps2->nr_icc_par, ps2->num_env, 1);
612
1514
        if (ps2->enable_ipdopd) {
613
53
            remap20(&ipd_mapped, ps2->ipd_par, ps2->nr_ipdopd_par, ps2->num_env, 0);
614
53
            remap20(&opd_mapped, ps2->opd_par, ps2->nr_ipdopd_par, ps2->num_env, 0);
615
        }
616
1514
        if (ps2->is34bands_old) {
617
1
            map_val_34_to_20(H11[0][0]);
618
1
            map_val_34_to_20(H11[1][0]);
619
1
            map_val_34_to_20(H12[0][0]);
620
1
            map_val_34_to_20(H12[1][0]);
621
1
            map_val_34_to_20(H21[0][0]);
622
1
            map_val_34_to_20(H21[1][0]);
623
1
            map_val_34_to_20(H22[0][0]);
624
1
            map_val_34_to_20(H22[1][0]);
625
1
            ipdopd_reset(ipd_hist, opd_hist);
626
        }
627
    }
628
629
    //Mixing
630
3649
    for (e = 0; e < ps2->num_env; e++) {
631
45549
        for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
632
            INTFLOAT h11, h12, h21, h22;
633
43516
            h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps2->iid_quant][icc_mapped[e][b]][0];
634
43516
            h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps2->iid_quant][icc_mapped[e][b]][1];
635
43516
            h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps2->iid_quant][icc_mapped[e][b]][2];
636
43516
            h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps2->iid_quant][icc_mapped[e][b]][3];
637
638

43516
            if (!PS_BASELINE && ps2->enable_ipdopd && b < NR_IPDOPD_BANDS[is34]) {
639
                //The spec say says to only run this smoother when enable_ipdopd
640
                //is set but the reference decoder appears to run it constantly
641
                INTFLOAT h11i, h12i, h21i, h22i;
642
                INTFLOAT ipd_adj_re, ipd_adj_im;
643
1144
                int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
644
1144
                int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
645
1144
                INTFLOAT opd_re = pd_re_smooth[opd_idx];
646
1144
                INTFLOAT opd_im = pd_im_smooth[opd_idx];
647
1144
                INTFLOAT ipd_re = pd_re_smooth[ipd_idx];
648
1144
                INTFLOAT ipd_im = pd_im_smooth[ipd_idx];
649
1144
                opd_hist[b] = opd_idx & 0x3F;
650
1144
                ipd_hist[b] = ipd_idx & 0x3F;
651
652
1144
                ipd_adj_re = AAC_MADD30(opd_re, ipd_re, opd_im, ipd_im);
653
1144
                ipd_adj_im = AAC_MSUB30(opd_im, ipd_re, opd_re, ipd_im);
654
1144
                h11i = AAC_MUL30(h11,  opd_im);
655
1144
                h11  = AAC_MUL30(h11,  opd_re);
656
1144
                h12i = AAC_MUL30(h12,  ipd_adj_im);
657
1144
                h12  = AAC_MUL30(h12,  ipd_adj_re);
658
1144
                h21i = AAC_MUL30(h21,  opd_im);
659
1144
                h21  = AAC_MUL30(h21,  opd_re);
660
1144
                h22i = AAC_MUL30(h22,  ipd_adj_im);
661
1144
                h22  = AAC_MUL30(h22,  ipd_adj_re);
662
1144
                H11[1][e+1][b] = h11i;
663
1144
                H12[1][e+1][b] = h12i;
664
1144
                H21[1][e+1][b] = h21i;
665
1144
                H22[1][e+1][b] = h22i;
666
            }
667
43516
            H11[0][e+1][b] = h11;
668
43516
            H12[0][e+1][b] = h12;
669
43516
            H21[0][e+1][b] = h21;
670
43516
            H22[0][e+1][b] = h22;
671
        }
672
150456
        for (k = 0; k < NR_BANDS[is34]; k++) {
673
148423
            LOCAL_ALIGNED_16(INTFLOAT, h, [2], [4]);
674
148423
            LOCAL_ALIGNED_16(INTFLOAT, h_step, [2], [4]);
675
148423
            int start = ps2->border_position[e];
676
148423
            int stop  = ps2->border_position[e+1];
677
148423
            INTFLOAT width = Q30(1.f) / ((stop - start) ? (stop - start) : 1);
678
#if USE_FIXED
679
            width = FFMIN(2U*width, INT_MAX);
680
#endif
681
148423
            b = k_to_i[k];
682
148423
            h[0][0] = H11[0][e][b];
683
148423
            h[0][1] = H12[0][e][b];
684
148423
            h[0][2] = H21[0][e][b];
685
148423
            h[0][3] = H22[0][e][b];
686
148423
            if (!PS_BASELINE && ps2->enable_ipdopd) {
687
            //Is this necessary? ps_04_new seems unchanged
688


7384
            if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
689
208
                h[1][0] = -H11[1][e][b];
690
208
                h[1][1] = -H12[1][e][b];
691
208
                h[1][2] = -H21[1][e][b];
692
208
                h[1][3] = -H22[1][e][b];
693
            } else {
694
7176
                h[1][0] = H11[1][e][b];
695
7176
                h[1][1] = H12[1][e][b];
696
7176
                h[1][2] = H21[1][e][b];
697
7176
                h[1][3] = H22[1][e][b];
698
            }
699
            }
700
            //Interpolation
701
148423
            h_step[0][0] = AAC_MSUB31_V3(H11[0][e+1][b], h[0][0], width);
702
148423
            h_step[0][1] = AAC_MSUB31_V3(H12[0][e+1][b], h[0][1], width);
703
148423
            h_step[0][2] = AAC_MSUB31_V3(H21[0][e+1][b], h[0][2], width);
704
148423
            h_step[0][3] = AAC_MSUB31_V3(H22[0][e+1][b], h[0][3], width);
705
148423
            if (!PS_BASELINE && ps2->enable_ipdopd) {
706
7384
                h_step[1][0] = AAC_MSUB31_V3(H11[1][e+1][b], h[1][0], width);
707
7384
                h_step[1][1] = AAC_MSUB31_V3(H12[1][e+1][b], h[1][1], width);
708
7384
                h_step[1][2] = AAC_MSUB31_V3(H21[1][e+1][b], h[1][2], width);
709
7384
                h_step[1][3] = AAC_MSUB31_V3(H22[1][e+1][b], h[1][3], width);
710
            }
711
148423
            if (stop - start)
712
148423
                ps->dsp.stereo_interpolate[!PS_BASELINE && ps2->enable_ipdopd](
713
148423
                    l[k] + 1 + start, r[k] + 1 + start,
714
                    h, h_step, stop - start);
715
        }
716
    }
717
1616
}
718
719
1616
int AAC_RENAME(ff_ps_apply)(AVCodecContext *avctx, PSContext *ps, INTFLOAT L[2][38][64], INTFLOAT R[2][38][64], int top)
720
{
721
1616
    INTFLOAT (*Lbuf)[32][2] = ps->Lbuf;
722
1616
    INTFLOAT (*Rbuf)[32][2] = ps->Rbuf;
723
1616
    const int len = 32;
724
1616
    int is34 = ps->common.is34bands;
725
726
1616
    top += NR_BANDS[is34] - 64;
727
1616
    memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
728
1616
    if (top < NR_ALLPASS_BANDS[is34])
729
        memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
730
731
1616
    hybrid_analysis(&ps->dsp, Lbuf, ps->in_buf, L, is34, len);
732
1616
    decorrelation(ps, Rbuf, (const INTFLOAT (*)[32][2]) Lbuf, is34);
733
1616
    stereo_processing(ps, Lbuf, Rbuf, is34);
734
1616
    hybrid_synthesis(&ps->dsp, L, Lbuf, is34, len);
735
1616
    hybrid_synthesis(&ps->dsp, R, Rbuf, is34, len);
736
737
1616
    return 0;
738
}
739
740
164
av_cold void AAC_RENAME(ff_ps_init)(void) {
741
164
    ps_tableinit();
742
164
    ff_ps_init_common();
743
164
}
744
745
347
av_cold void AAC_RENAME(ff_ps_ctx_init)(PSContext *ps)
746
{
747
347
    AAC_RENAME(ff_psdsp_init)(&ps->dsp);
748
347
}