GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/aacps.c Lines: 528 627 84.2 %
Date: 2019-11-18 18:00:01 Branches: 224 276 81.2 %

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 "avcodec.h"
29
#include "get_bits.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
#include "aacpsdata.c"
38
39
#define PS_BASELINE 0  ///< Operate in Baseline PS mode
40
                       ///< Baseline implies 10 or 20 stereo bands,
41
                       ///< mixing mode A, and no ipd/opd
42
43
#define numQMFSlots 32 //numTimeSlots * RATE
44
45
static const int8_t num_env_tab[2][4] = {
46
    { 0, 1, 2, 4, },
47
    { 1, 2, 3, 4, },
48
};
49
50
static const int8_t nr_iidicc_par_tab[] = {
51
    10, 20, 34, 10, 20, 34,
52
};
53
54
static const int8_t nr_iidopd_par_tab[] = {
55
     5, 11, 17,  5, 11, 17,
56
};
57
58
enum {
59
    huff_iid_df1,
60
    huff_iid_dt1,
61
    huff_iid_df0,
62
    huff_iid_dt0,
63
    huff_icc_df,
64
    huff_icc_dt,
65
    huff_ipd_df,
66
    huff_ipd_dt,
67
    huff_opd_df,
68
    huff_opd_dt,
69
};
70
71
static const int huff_iid[] = {
72
    huff_iid_df0,
73
    huff_iid_df1,
74
    huff_iid_dt0,
75
    huff_iid_dt1,
76
};
77
78
static VLC vlc_ps[10];
79
80
#define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
81
/** \
82
 * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \
83
 * Inter-channel Phase Difference/Overall Phase Difference parameters from the \
84
 * bitstream. \
85
 * \
86
 * @param avctx contains the current codec context \
87
 * @param gb    pointer to the input bitstream \
88
 * @param ps    pointer to the Parametric Stereo context \
89
 * @param PAR   pointer to the parameter to be read \
90
 * @param e     envelope to decode \
91
 * @param dt    1: time delta-coded, 0: frequency delta-coded \
92
 */ \
93
static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
94
                        int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
95
{ \
96
    int b, num = ps->nr_ ## PAR ## _par; \
97
    VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
98
    if (dt) { \
99
        int e_prev = e ? e - 1 : ps->num_env_old - 1; \
100
        e_prev = FFMAX(e_prev, 0); \
101
        for (b = 0; b < num; b++) { \
102
            int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
103
            if (MASK) val &= MASK; \
104
            PAR[e][b] = val; \
105
            if (ERR_CONDITION) \
106
                goto err; \
107
        } \
108
    } else { \
109
        int val = 0; \
110
        for (b = 0; b < num; b++) { \
111
            val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
112
            if (MASK) val &= MASK; \
113
            PAR[e][b] = val; \
114
            if (ERR_CONDITION) \
115
                goto err; \
116
        } \
117
    } \
118
    return 0; \
119
err: \
120
    av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
121
    return AVERROR_INVALIDDATA; \
122
}
123
124



38305
READ_PAR_DATA(iid,    huff_offset[table_idx],    0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
125



38330
READ_PAR_DATA(icc,    huff_offset[table_idx],    0, ps->icc_par[e][b] > 7U)
126


2472
READ_PAR_DATA(ipdopd,                      0, 0x07, 0)
127
128
53
static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
129
{
130
    int e;
131
53
    int count = get_bits_count(gb);
132
133
53
    if (ps_extension_id)
134
        return 0;
135
136
53
    ps->enable_ipdopd = get_bits1(gb);
137
53
    if (ps->enable_ipdopd) {
138
156
        for (e = 0; e < ps->num_env; e++) {
139
103
            int dt = get_bits1(gb);
140
103
            read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
141
103
            dt = get_bits1(gb);
142
103
            read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
143
        }
144
    }
145
53
    skip_bits1(gb);      //reserved_ps
146
53
    return get_bits_count(gb) - count;
147
}
148
149
3
static void ipdopd_reset(int8_t *ipd_hist, int8_t *opd_hist)
150
{
151
    int i;
152
54
    for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
153
51
        opd_hist[i] = 0;
154
51
        ipd_hist[i] = 0;
155
    }
156
3
}
157
158
1616
int AAC_RENAME(ff_ps_read_data)(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
159
{
160
    int e;
161
1616
    int bit_count_start = get_bits_count(gb_host);
162
    int header;
163
    int bits_consumed;
164
1616
    GetBitContext gbc = *gb_host, *gb = &gbc;
165
166
1616
    header = get_bits1(gb);
167
1616
    if (header) {     //enable_ps_header
168
307
        ps->enable_iid = get_bits1(gb);
169
307
        if (ps->enable_iid) {
170
175
            int iid_mode = get_bits(gb, 3);
171
175
            if (iid_mode > 5) {
172
                av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
173
                       iid_mode);
174
                goto err;
175
            }
176
175
            ps->nr_iid_par    = nr_iidicc_par_tab[iid_mode];
177
175
            ps->iid_quant     = iid_mode > 2;
178
175
            ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
179
        }
180
307
        ps->enable_icc = get_bits1(gb);
181
307
        if (ps->enable_icc) {
182
262
            ps->icc_mode = get_bits(gb, 3);
183
262
            if (ps->icc_mode > 5) {
184
                av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
185
                       ps->icc_mode);
186
                goto err;
187
            }
188
262
            ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode];
189
        }
190
307
        ps->enable_ext = get_bits1(gb);
191
    }
192
193
1616
    ps->frame_class = get_bits1(gb);
194
1616
    ps->num_env_old = ps->num_env;
195
1616
    ps->num_env     = num_env_tab[ps->frame_class][get_bits(gb, 2)];
196
197
1616
    ps->border_position[0] = -1;
198
1616
    if (ps->frame_class) {
199
940
        for (e = 1; e <= ps->num_env; e++) {
200
671
            ps->border_position[e] = get_bits(gb, 5);
201
671
            if (ps->border_position[e] < ps->border_position[e-1]) {
202
                av_log(avctx, AV_LOG_ERROR, "border_position non monotone.\n");
203
                goto err;
204
            }
205
        }
206
    } else
207
2628
        for (e = 1; e <= ps->num_env; e++)
208
1281
            ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
209
210
1616
    if (ps->enable_iid) {
211
3241
        for (e = 0; e < ps->num_env; e++) {
212
1805
            int dt = get_bits1(gb);
213
1805
            if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
214
                goto err;
215
        }
216
    } else
217
180
        memset(ps->iid_par, 0, sizeof(ps->iid_par));
218
219
1616
    if (ps->enable_icc)
220
3300
        for (e = 0; e < ps->num_env; e++) {
221
1806
            int dt = get_bits1(gb);
222

1806
            if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
223
                goto err;
224
        }
225
    else
226
122
        memset(ps->icc_par, 0, sizeof(ps->icc_par));
227
228
1616
    if (ps->enable_ext) {
229
53
        int cnt = get_bits(gb, 4);
230
53
        if (cnt == 15) {
231
            cnt += get_bits(gb, 8);
232
        }
233
53
        cnt *= 8;
234
106
        while (cnt > 7) {
235
53
            int ps_extension_id = get_bits(gb, 2);
236
53
            cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
237
        }
238
53
        if (cnt < 0) {
239
            av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d\n", cnt);
240
            goto err;
241
        }
242
53
        skip_bits(gb, cnt);
243
    }
244
245
1616
    ps->enable_ipdopd &= !PS_BASELINE;
246
247
    //Fix up envelopes
248

1616
    if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
249
        //Create a fake envelope
250
81
        int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
251
        int b;
252

81
        if (source >= 0 && source != ps->num_env) {
253
29
            if (ps->enable_iid) {
254
21
                memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
255
            }
256
29
            if (ps->enable_icc) {
257
21
                memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
258
            }
259
29
            if (ps->enable_ipdopd) {
260
                memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
261
                memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
262
            }
263
        }
264
81
        if (ps->enable_iid){
265
756
            for (b = 0; b < ps->nr_iid_par; b++) {
266
720
                if (FFABS(ps->iid_par[ps->num_env][b]) > 7 + 8 * ps->iid_quant) {
267
                    av_log(avctx, AV_LOG_ERROR, "iid_par invalid\n");
268
                    goto err;
269
                }
270
            }
271
        }
272
81
        if (ps->enable_icc){
273
882
            for (b = 0; b < ps->nr_iid_par; b++) {
274
840
                if (ps->icc_par[ps->num_env][b] > 7U) {
275
                    av_log(avctx, AV_LOG_ERROR, "icc_par invalid\n");
276
                    goto err;
277
                }
278
            }
279
        }
280
81
        ps->num_env++;
281
81
        ps->border_position[ps->num_env] = numQMFSlots - 1;
282
    }
283
284
285
1616
    ps->is34bands_old = ps->is34bands;
286

1616
    if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
287

3045
        ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
288

1498
                        (ps->enable_icc && ps->nr_icc_par == 34);
289
290
    //Baseline
291
1616
    if (!ps->enable_ipdopd) {
292
1563
        memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
293
1563
        memset(ps->opd_par, 0, sizeof(ps->opd_par));
294
    }
295
296
1616
    if (header)
297
307
        ps->start = 1;
298
299
1616
    bits_consumed = get_bits_count(gb) - bit_count_start;
300
1616
    if (bits_consumed <= bits_left) {
301
1616
        skip_bits_long(gb_host, bits_consumed);
302
1616
        return bits_consumed;
303
    }
304
    av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
305
err:
306
    ps->start = 0;
307
    skip_bits_long(gb_host, bits_left);
308
    memset(ps->iid_par, 0, sizeof(ps->iid_par));
309
    memset(ps->icc_par, 0, sizeof(ps->icc_par));
310
    memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
311
    memset(ps->opd_par, 0, sizeof(ps->opd_par));
312
    return bits_left;
313
}
314
315
/** Split one subband into 2 subsubbands with a symmetric real filter.
316
 * The filter must have its non-center even coefficients equal to zero. */
317
3028
static void hybrid2_re(INTFLOAT (*in)[2], INTFLOAT (*out)[32][2], const INTFLOAT filter[8], int len, int reverse)
318
{
319
    int i, j;
320
99924
    for (i = 0; i < len; i++, in++) {
321
96896
        INT64FLOAT re_in = AAC_MUL31(filter[6], in[6][0]); //real inphase
322
96896
        INT64FLOAT re_op = 0.0f;                          //real out of phase
323
96896
        INT64FLOAT im_in = AAC_MUL31(filter[6], in[6][1]); //imag inphase
324
96896
        INT64FLOAT im_op = 0.0f;                          //imag out of phase
325
387584
        for (j = 0; j < 6; j += 2) {
326
290688
            re_op += (INT64FLOAT)filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
327
290688
            im_op += (INT64FLOAT)filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
328
        }
329
330
#if USE_FIXED
331
        re_op = (re_op + 0x40000000) >> 31;
332
        im_op = (im_op + 0x40000000) >> 31;
333
#endif /* USE_FIXED */
334
335
96896
        out[ reverse][i][0] = (INTFLOAT)(re_in + re_op);
336
96896
        out[ reverse][i][1] = (INTFLOAT)(im_in + im_op);
337
96896
        out[!reverse][i][0] = (INTFLOAT)(re_in - re_op);
338
96896
        out[!reverse][i][1] = (INTFLOAT)(im_in - im_op);
339
    }
340
3028
}
341
342
/** Split one subband into 6 subsubbands with a complex filter */
343
1514
static void hybrid6_cx(PSDSPContext *dsp, INTFLOAT (*in)[2], INTFLOAT (*out)[32][2],
344
                       TABLE_CONST INTFLOAT (*filter)[8][2], int len)
345
{
346
    int i;
347
1514
    int N = 8;
348
1514
    LOCAL_ALIGNED_16(INTFLOAT, temp, [8], [2]);
349
350
49962
    for (i = 0; i < len; i++, in++) {
351
48448
        dsp->hybrid_analysis(temp, in, (const INTFLOAT (*)[8][2]) filter, 1, N);
352
48448
        out[0][i][0] = temp[6][0];
353
48448
        out[0][i][1] = temp[6][1];
354
48448
        out[1][i][0] = temp[7][0];
355
48448
        out[1][i][1] = temp[7][1];
356
48448
        out[2][i][0] = temp[0][0];
357
48448
        out[2][i][1] = temp[0][1];
358
48448
        out[3][i][0] = temp[1][0];
359
48448
        out[3][i][1] = temp[1][1];
360
48448
        out[4][i][0] = temp[2][0] + temp[5][0];
361
48448
        out[4][i][1] = temp[2][1] + temp[5][1];
362
48448
        out[5][i][0] = temp[3][0] + temp[4][0];
363
48448
        out[5][i][1] = temp[3][1] + temp[4][1];
364
    }
365
1514
}
366
367
510
static void hybrid4_8_12_cx(PSDSPContext *dsp,
368
                            INTFLOAT (*in)[2], INTFLOAT (*out)[32][2],
369
                            TABLE_CONST INTFLOAT (*filter)[8][2], int N, int len)
370
{
371
    int i;
372
373
16830
    for (i = 0; i < len; i++, in++) {
374
16320
        dsp->hybrid_analysis(out[0] + i, in, (const INTFLOAT (*)[8][2]) filter, 32, N);
375
    }
376
510
}
377
378
1616
static void hybrid_analysis(PSDSPContext *dsp, INTFLOAT out[91][32][2],
379
                            INTFLOAT in[5][44][2], INTFLOAT L[2][38][64],
380
                            int is34, int len)
381
{
382
    int i, j;
383
9696
    for (i = 0; i < 5; i++) {
384
315120
        for (j = 0; j < 38; j++) {
385
307040
            in[i][j+6][0] = L[0][j][i];
386
307040
            in[i][j+6][1] = L[1][j][i];
387
        }
388
    }
389
1616
    if (is34) {
390
102
        hybrid4_8_12_cx(dsp, in[0], out,    f34_0_12, 12, len);
391
102
        hybrid4_8_12_cx(dsp, in[1], out+12, f34_1_8,   8, len);
392
102
        hybrid4_8_12_cx(dsp, in[2], out+20, f34_2_4,   4, len);
393
102
        hybrid4_8_12_cx(dsp, in[3], out+24, f34_2_4,   4, len);
394
102
        hybrid4_8_12_cx(dsp, in[4], out+28, f34_2_4,   4, len);
395
102
        dsp->hybrid_analysis_ileave(out + 27, L, 5, len);
396
    } else {
397
1514
        hybrid6_cx(dsp, in[0], out, f20_0_8, len);
398
1514
        hybrid2_re(in[1], out+6, g1_Q2, len, 1);
399
1514
        hybrid2_re(in[2], out+8, g1_Q2, len, 0);
400
1514
        dsp->hybrid_analysis_ileave(out + 7, L, 3, len);
401
    }
402
    //update in_buf
403
9696
    for (i = 0; i < 5; i++) {
404
8080
        memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
405
    }
406
1616
}
407
408
3232
static void hybrid_synthesis(PSDSPContext *dsp, INTFLOAT out[2][38][64],
409
                             INTFLOAT in[91][32][2], int is34, int len)
410
{
411
    int i, n;
412
3232
    if (is34) {
413
6732
        for (n = 0; n < len; n++) {
414
6528
            memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
415
6528
            memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
416
84864
            for (i = 0; i < 12; i++) {
417
78336
                out[0][n][0] += (UINTFLOAT)in[   i][n][0];
418
78336
                out[1][n][0] += (UINTFLOAT)in[   i][n][1];
419
            }
420
58752
            for (i = 0; i < 8; i++) {
421
52224
                out[0][n][1] += (UINTFLOAT)in[12+i][n][0];
422
52224
                out[1][n][1] += (UINTFLOAT)in[12+i][n][1];
423
            }
424
32640
            for (i = 0; i < 4; i++) {
425
26112
                out[0][n][2] += (UINTFLOAT)in[20+i][n][0];
426
26112
                out[1][n][2] += (UINTFLOAT)in[20+i][n][1];
427
26112
                out[0][n][3] += (UINTFLOAT)in[24+i][n][0];
428
26112
                out[1][n][3] += (UINTFLOAT)in[24+i][n][1];
429
26112
                out[0][n][4] += (UINTFLOAT)in[28+i][n][0];
430
26112
                out[1][n][4] += (UINTFLOAT)in[28+i][n][1];
431
            }
432
        }
433
204
        dsp->hybrid_synthesis_deint(out, in + 27, 5, len);
434
    } else {
435
99924
        for (n = 0; n < len; n++) {
436
96896
            out[0][n][0] = (UINTFLOAT)in[0][n][0] + in[1][n][0] + in[2][n][0] +
437
96896
                           (UINTFLOAT)in[3][n][0] + in[4][n][0] + in[5][n][0];
438
96896
            out[1][n][0] = (UINTFLOAT)in[0][n][1] + in[1][n][1] + in[2][n][1] +
439
96896
                           (UINTFLOAT)in[3][n][1] + in[4][n][1] + in[5][n][1];
440
96896
            out[0][n][1] = (UINTFLOAT)in[6][n][0] + in[7][n][0];
441
96896
            out[1][n][1] = (UINTFLOAT)in[6][n][1] + in[7][n][1];
442
96896
            out[0][n][2] = (UINTFLOAT)in[8][n][0] + in[9][n][0];
443
96896
            out[1][n][2] = (UINTFLOAT)in[8][n][1] + in[9][n][1];
444
        }
445
3028
        dsp->hybrid_synthesis_deint(out, in + 7, 3, len);
446
    }
447
3232
}
448
449
/// All-pass filter decay slope
450
#define DECAY_SLOPE      Q30(0.05f)
451
/// Number of frequency bands that can be addressed by the parameter index, b(k)
452
static const int   NR_PAR_BANDS[]      = { 20, 34 };
453
static const int   NR_IPDOPD_BANDS[]   = { 11, 17 };
454
/// Number of frequency bands that can be addressed by the sub subband index, k
455
static const int   NR_BANDS[]          = { 71, 91 };
456
/// Start frequency band for the all-pass filter decay slope
457
static const int   DECAY_CUTOFF[]      = { 10, 32 };
458
/// Number of all-pass filer bands
459
static const int   NR_ALLPASS_BANDS[]  = { 30, 50 };
460
/// First stereo band using the short one sample delay
461
static const int   SHORT_DELAY_BAND[]  = { 42, 62 };
462
463
/** Table 8.46 */
464
1
static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
465
{
466
    int b;
467
1
    if (full)
468
1
        b = 9;
469
    else {
470
        b = 4;
471
        par_mapped[10] = 0;
472
    }
473
11
    for (; b >= 0; b--) {
474
10
        par_mapped[2*b+1] = par_mapped[2*b] = par[b];
475
    }
476
1
}
477
478
1
static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
479
{
480
1
    par_mapped[ 0] = (2*par[ 0] +   par[ 1]) / 3;
481
1
    par_mapped[ 1] = (  par[ 1] + 2*par[ 2]) / 3;
482
1
    par_mapped[ 2] = (2*par[ 3] +   par[ 4]) / 3;
483
1
    par_mapped[ 3] = (  par[ 4] + 2*par[ 5]) / 3;
484
1
    par_mapped[ 4] = (  par[ 6] +   par[ 7]) / 2;
485
1
    par_mapped[ 5] = (  par[ 8] +   par[ 9]) / 2;
486
1
    par_mapped[ 6] =    par[10];
487
1
    par_mapped[ 7] =    par[11];
488
1
    par_mapped[ 8] = (  par[12] +   par[13]) / 2;
489
1
    par_mapped[ 9] = (  par[14] +   par[15]) / 2;
490
1
    par_mapped[10] =    par[16];
491
1
    if (full) {
492
1
        par_mapped[11] =    par[17];
493
1
        par_mapped[12] =    par[18];
494
1
        par_mapped[13] =    par[19];
495
1
        par_mapped[14] = (  par[20] +   par[21]) / 2;
496
1
        par_mapped[15] = (  par[22] +   par[23]) / 2;
497
1
        par_mapped[16] = (  par[24] +   par[25]) / 2;
498
1
        par_mapped[17] = (  par[26] +   par[27]) / 2;
499
1
        par_mapped[18] = (  par[28] +   par[29] +   par[30] +   par[31]) / 4;
500
1
        par_mapped[19] = (  par[32] +   par[33]) / 2;
501
    }
502
1
}
503
504
8
static void map_val_34_to_20(INTFLOAT par[PS_MAX_NR_IIDICC])
505
{
506
#if USE_FIXED
507
    par[ 0] = (int)(((int64_t)(par[ 0] + (unsigned)(par[ 1]>>1)) * 1431655765 + \
508
                      0x40000000) >> 31);
509
    par[ 1] = (int)(((int64_t)((par[ 1]>>1) + (unsigned)par[ 2]) * 1431655765 + \
510
                      0x40000000) >> 31);
511
    par[ 2] = (int)(((int64_t)(par[ 3] + (unsigned)(par[ 4]>>1)) * 1431655765 + \
512
                      0x40000000) >> 31);
513
    par[ 3] = (int)(((int64_t)((par[ 4]>>1) + (unsigned)par[ 5]) * 1431655765 + \
514
                      0x40000000) >> 31);
515
#else
516
8
    par[ 0] = (2*par[ 0] +   par[ 1]) * 0.33333333f;
517
8
    par[ 1] = (  par[ 1] + 2*par[ 2]) * 0.33333333f;
518
8
    par[ 2] = (2*par[ 3] +   par[ 4]) * 0.33333333f;
519
8
    par[ 3] = (  par[ 4] + 2*par[ 5]) * 0.33333333f;
520
#endif /* USE_FIXED */
521
8
    par[ 4] = AAC_HALF_SUM(par[ 6], par[ 7]);
522
8
    par[ 5] = AAC_HALF_SUM(par[ 8], par[ 9]);
523
8
    par[ 6] =    par[10];
524
8
    par[ 7] =    par[11];
525
8
    par[ 8] = AAC_HALF_SUM(par[12], par[13]);
526
8
    par[ 9] = AAC_HALF_SUM(par[14], par[15]);
527
8
    par[10] =    par[16];
528
8
    par[11] =    par[17];
529
8
    par[12] =    par[18];
530
8
    par[13] =    par[19];
531
8
    par[14] = AAC_HALF_SUM(par[20], par[21]);
532
8
    par[15] = AAC_HALF_SUM(par[22], par[23]);
533
8
    par[16] = AAC_HALF_SUM(par[24], par[25]);
534
8
    par[17] = AAC_HALF_SUM(par[26], par[27]);
535
#if USE_FIXED
536
    par[18] = (((par[28]+2)>>2) + ((par[29]+2)>>2) + ((par[30]+2)>>2) + ((par[31]+2)>>2));
537
#else
538
8
    par[18] = (  par[28] +   par[29] +   par[30] +   par[31]) * 0.25f;
539
#endif /* USE_FIXED */
540
8
    par[19] = AAC_HALF_SUM(par[32], par[33]);
541
8
}
542
543
204
static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
544
{
545
204
    if (full) {
546
204
        par_mapped[33] = par[9];
547
204
        par_mapped[32] = par[9];
548
204
        par_mapped[31] = par[9];
549
204
        par_mapped[30] = par[9];
550
204
        par_mapped[29] = par[9];
551
204
        par_mapped[28] = par[9];
552
204
        par_mapped[27] = par[8];
553
204
        par_mapped[26] = par[8];
554
204
        par_mapped[25] = par[8];
555
204
        par_mapped[24] = par[8];
556
204
        par_mapped[23] = par[7];
557
204
        par_mapped[22] = par[7];
558
204
        par_mapped[21] = par[7];
559
204
        par_mapped[20] = par[7];
560
204
        par_mapped[19] = par[6];
561
204
        par_mapped[18] = par[6];
562
204
        par_mapped[17] = par[5];
563
204
        par_mapped[16] = par[5];
564
    } else {
565
        par_mapped[16] =      0;
566
    }
567
204
    par_mapped[15] = par[4];
568
204
    par_mapped[14] = par[4];
569
204
    par_mapped[13] = par[4];
570
204
    par_mapped[12] = par[4];
571
204
    par_mapped[11] = par[3];
572
204
    par_mapped[10] = par[3];
573
204
    par_mapped[ 9] = par[2];
574
204
    par_mapped[ 8] = par[2];
575
204
    par_mapped[ 7] = par[2];
576
204
    par_mapped[ 6] = par[2];
577
204
    par_mapped[ 5] = par[1];
578
204
    par_mapped[ 4] = par[1];
579
204
    par_mapped[ 3] = par[1];
580
204
    par_mapped[ 2] = par[0];
581
204
    par_mapped[ 1] = par[0];
582
204
    par_mapped[ 0] = par[0];
583
204
}
584
585
static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
586
{
587
    if (full) {
588
        par_mapped[33] =  par[19];
589
        par_mapped[32] =  par[19];
590
        par_mapped[31] =  par[18];
591
        par_mapped[30] =  par[18];
592
        par_mapped[29] =  par[18];
593
        par_mapped[28] =  par[18];
594
        par_mapped[27] =  par[17];
595
        par_mapped[26] =  par[17];
596
        par_mapped[25] =  par[16];
597
        par_mapped[24] =  par[16];
598
        par_mapped[23] =  par[15];
599
        par_mapped[22] =  par[15];
600
        par_mapped[21] =  par[14];
601
        par_mapped[20] =  par[14];
602
        par_mapped[19] =  par[13];
603
        par_mapped[18] =  par[12];
604
        par_mapped[17] =  par[11];
605
    }
606
    par_mapped[16] =  par[10];
607
    par_mapped[15] =  par[ 9];
608
    par_mapped[14] =  par[ 9];
609
    par_mapped[13] =  par[ 8];
610
    par_mapped[12] =  par[ 8];
611
    par_mapped[11] =  par[ 7];
612
    par_mapped[10] =  par[ 6];
613
    par_mapped[ 9] =  par[ 5];
614
    par_mapped[ 8] =  par[ 5];
615
    par_mapped[ 7] =  par[ 4];
616
    par_mapped[ 6] =  par[ 4];
617
    par_mapped[ 5] =  par[ 3];
618
    par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
619
    par_mapped[ 3] =  par[ 2];
620
    par_mapped[ 2] =  par[ 1];
621
    par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
622
    par_mapped[ 0] =  par[ 0];
623
}
624
625
16
static void map_val_20_to_34(INTFLOAT par[PS_MAX_NR_IIDICC])
626
{
627
16
    par[33] =  par[19];
628
16
    par[32] =  par[19];
629
16
    par[31] =  par[18];
630
16
    par[30] =  par[18];
631
16
    par[29] =  par[18];
632
16
    par[28] =  par[18];
633
16
    par[27] =  par[17];
634
16
    par[26] =  par[17];
635
16
    par[25] =  par[16];
636
16
    par[24] =  par[16];
637
16
    par[23] =  par[15];
638
16
    par[22] =  par[15];
639
16
    par[21] =  par[14];
640
16
    par[20] =  par[14];
641
16
    par[19] =  par[13];
642
16
    par[18] =  par[12];
643
16
    par[17] =  par[11];
644
16
    par[16] =  par[10];
645
16
    par[15] =  par[ 9];
646
16
    par[14] =  par[ 9];
647
16
    par[13] =  par[ 8];
648
16
    par[12] =  par[ 8];
649
16
    par[11] =  par[ 7];
650
16
    par[10] =  par[ 6];
651
16
    par[ 9] =  par[ 5];
652
16
    par[ 8] =  par[ 5];
653
16
    par[ 7] =  par[ 4];
654
16
    par[ 6] =  par[ 4];
655
16
    par[ 5] =  par[ 3];
656
16
    par[ 4] = AAC_HALF_SUM(par[ 2], par[ 3]);
657
16
    par[ 3] =  par[ 2];
658
16
    par[ 2] =  par[ 1];
659
16
    par[ 1] = AAC_HALF_SUM(par[ 0], par[ 1]);
660
16
}
661
662
1616
static void decorrelation(PSContext *ps, INTFLOAT (*out)[32][2], const INTFLOAT (*s)[32][2], int is34)
663
{
664
1616
    LOCAL_ALIGNED_16(INTFLOAT, power, [34], [PS_QMF_TIME_SLOTS]);
665
1616
    LOCAL_ALIGNED_16(INTFLOAT, transient_gain, [34], [PS_QMF_TIME_SLOTS]);
666
1616
    INTFLOAT *peak_decay_nrg = ps->peak_decay_nrg;
667
1616
    INTFLOAT *power_smooth = ps->power_smooth;
668
1616
    INTFLOAT *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
669
1616
    INTFLOAT (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
670
1616
    INTFLOAT (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
671
#if !USE_FIXED
672
1616
    const float transient_impact  = 1.5f;
673
1616
    const float a_smooth          = 0.25f; ///< Smoothing coefficient
674
#endif /* USE_FIXED */
675
1616
    const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
676
    int i, k, m, n;
677
1616
    int n0 = 0, nL = 32;
678
1616
    const INTFLOAT peak_decay_factor = Q31(0.76592833836465f);
679
680
1616
    memset(power, 0, 34 * sizeof(*power));
681
682
1616
    if (is34 != ps->is34bands_old) {
683
3
        memset(ps->peak_decay_nrg,         0, sizeof(ps->peak_decay_nrg));
684
3
        memset(ps->power_smooth,           0, sizeof(ps->power_smooth));
685
3
        memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
686
3
        memset(ps->delay,                  0, sizeof(ps->delay));
687
3
        memset(ps->ap_delay,               0, sizeof(ps->ap_delay));
688
    }
689
690
118392
    for (k = 0; k < NR_BANDS[is34]; k++) {
691
116776
        int i = k_to_i[k];
692
116776
        ps->dsp.add_squares(power[i], s[k], nL - n0);
693
    }
694
695
    //Transient detection
696
#if USE_FIXED
697
    for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
698
        for (n = n0; n < nL; n++) {
699
            int decayed_peak;
700
            decayed_peak = (int)(((int64_t)peak_decay_factor * \
701
                                           peak_decay_nrg[i] + 0x40000000) >> 31);
702
            peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
703
            power_smooth[i] += (power[i][n] + 2LL - power_smooth[i]) >> 2;
704
            peak_decay_diff_smooth[i] += (peak_decay_nrg[i] + 2LL - power[i][n] - \
705
                                          peak_decay_diff_smooth[i]) >> 2;
706
707
            if (peak_decay_diff_smooth[i]) {
708
                transient_gain[i][n] = FFMIN(power_smooth[i]*43691LL / peak_decay_diff_smooth[i], 1<<16);
709
            } else
710
                transient_gain[i][n] = 1 << 16;
711
        }
712
    }
713
#else
714
35364
    for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
715
1113684
        for (n = n0; n < nL; n++) {
716
1079936
            float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
717
            float denom;
718
1079936
            peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
719
1079936
            power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
720
1079936
            peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
721
1079936
            denom = transient_impact * peak_decay_diff_smooth[i];
722
1079936
            transient_gain[i][n]   = (denom > power_smooth[i]) ?
723
1079936
                                         power_smooth[i] / denom : 1.0f;
724
        }
725
    }
726
727
#endif /* USE_FIXED */
728
    //Decorrelation and transient reduction
729
    //                         PS_AP_LINKS - 1
730
    //                               -----
731
    //                                | |  Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
732
    //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
733
    //                                | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
734
    //                               m = 0
735
    //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
736
52136
    for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
737
50520
        int b = k_to_i[k];
738
#if USE_FIXED
739
        int g_decay_slope;
740
741
        if (k - DECAY_CUTOFF[is34] <= 0) {
742
          g_decay_slope = 1 << 30;
743
        }
744
        else if (k - DECAY_CUTOFF[is34] >= 20) {
745
          g_decay_slope = 0;
746
        }
747
        else {
748
          g_decay_slope = (1 << 30) - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
749
        }
750
#else
751
50520
        float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
752
50520
        g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
753
#endif /* USE_FIXED */
754
50520
        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
755
50520
        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
756
202080
        for (m = 0; m < PS_AP_LINKS; m++) {
757
151560
            memcpy(ap_delay[k][m],   ap_delay[k][m]+numQMFSlots,           5*sizeof(ap_delay[k][m][0]));
758
        }
759
50520
        ps->dsp.decorrelate(out[k], delay[k] + PS_MAX_DELAY - 2, ap_delay[k],
760
50520
                            phi_fract[is34][k],
761
50520
                            (const INTFLOAT (*)[2]) Q_fract_allpass[is34][k],
762
50520
                            transient_gain[b], g_decay_slope, nL - n0);
763
    }
764
21008
    for (; k < SHORT_DELAY_BAND[is34]; k++) {
765
19392
        int i = k_to_i[k];
766
19392
        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
767
19392
        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
768
        //H = delay 14
769
19392
        ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 14,
770
19392
                                transient_gain[i], nL - n0);
771
    }
772
48480
    for (; k < NR_BANDS[is34]; k++) {
773
46864
        int i = k_to_i[k];
774
46864
        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
775
46864
        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
776
        //H = delay 1
777
46864
        ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 1,
778
46864
                                transient_gain[i], nL - n0);
779
    }
780
1616
}
781
782
204
static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
783
                    int8_t           (*par)[PS_MAX_NR_IIDICC],
784
                    int num_par, int num_env, int full)
785
{
786
204
    int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
787
    int e;
788

204
    if (num_par == 20 || num_par == 11) {
789
        for (e = 0; e < num_env; e++) {
790
            map_idx_20_to_34(par_mapped[e], par[e], full);
791
        }
792

204
    } else if (num_par == 10 || num_par == 5) {
793
306
        for (e = 0; e < num_env; e++) {
794
204
            map_idx_10_to_34(par_mapped[e], par[e], full);
795
        }
796
    } else {
797
102
        *p_par_mapped = par;
798
    }
799
204
}
800
801
3134
static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
802
                    int8_t           (*par)[PS_MAX_NR_IIDICC],
803
                    int num_par, int num_env, int full)
804
{
805
3134
    int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
806
    int e;
807

3134
    if (num_par == 34 || num_par == 17) {
808
2
        for (e = 0; e < num_env; e++) {
809
1
            map_idx_34_to_20(par_mapped[e], par[e], full);
810
        }
811

3133
    } else if (num_par == 10 || num_par == 5) {
812
2
        for (e = 0; e < num_env; e++) {
813
1
            map_idx_10_to_20(par_mapped[e], par[e], full);
814
        }
815
    } else {
816
3132
        *p_par_mapped = par;
817
    }
818
3134
}
819
820
1616
static void stereo_processing(PSContext *ps, INTFLOAT (*l)[32][2], INTFLOAT (*r)[32][2], int is34)
821
{
822
    int e, b, k;
823
824
1616
    INTFLOAT (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
825
1616
    INTFLOAT (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
826
1616
    INTFLOAT (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
827
1616
    INTFLOAT (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
828
1616
    int8_t *opd_hist = ps->opd_hist;
829
1616
    int8_t *ipd_hist = ps->ipd_hist;
830
    int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
831
    int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
832
    int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
833
    int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
834
1616
    int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
835
1616
    int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
836
1616
    int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
837
1616
    int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
838
1616
    const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
839
1616
    TABLE_CONST INTFLOAT (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
840
841
    //Remapping
842
1616
    if (ps->num_env_old) {
843
1590
        memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
844
1590
        memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
845
1590
        memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
846
1590
        memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
847
1590
        memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
848
1590
        memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
849
1590
        memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
850
1590
        memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
851
    }
852
853
1616
    if (is34) {
854
102
        remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
855
102
        remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
856
102
        if (ps->enable_ipdopd) {
857
            remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
858
            remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
859
        }
860
102
        if (!ps->is34bands_old) {
861
2
            map_val_20_to_34(H11[0][0]);
862
2
            map_val_20_to_34(H11[1][0]);
863
2
            map_val_20_to_34(H12[0][0]);
864
2
            map_val_20_to_34(H12[1][0]);
865
2
            map_val_20_to_34(H21[0][0]);
866
2
            map_val_20_to_34(H21[1][0]);
867
2
            map_val_20_to_34(H22[0][0]);
868
2
            map_val_20_to_34(H22[1][0]);
869
2
            ipdopd_reset(ipd_hist, opd_hist);
870
        }
871
    } else {
872
1514
        remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
873
1514
        remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
874
1514
        if (ps->enable_ipdopd) {
875
53
            remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
876
53
            remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
877
        }
878
1514
        if (ps->is34bands_old) {
879
1
            map_val_34_to_20(H11[0][0]);
880
1
            map_val_34_to_20(H11[1][0]);
881
1
            map_val_34_to_20(H12[0][0]);
882
1
            map_val_34_to_20(H12[1][0]);
883
1
            map_val_34_to_20(H21[0][0]);
884
1
            map_val_34_to_20(H21[1][0]);
885
1
            map_val_34_to_20(H22[0][0]);
886
1
            map_val_34_to_20(H22[1][0]);
887
1
            ipdopd_reset(ipd_hist, opd_hist);
888
        }
889
    }
890
891
    //Mixing
892
3649
    for (e = 0; e < ps->num_env; e++) {
893
45549
        for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
894
            INTFLOAT h11, h12, h21, h22;
895
43516
            h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0];
896
43516
            h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
897
43516
            h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
898
43516
            h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
899
900

43516
            if (!PS_BASELINE && ps->enable_ipdopd && b < NR_IPDOPD_BANDS[is34]) {
901
                //The spec say says to only run this smoother when enable_ipdopd
902
                //is set but the reference decoder appears to run it constantly
903
                INTFLOAT h11i, h12i, h21i, h22i;
904
                INTFLOAT ipd_adj_re, ipd_adj_im;
905
1144
                int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
906
1144
                int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
907
1144
                INTFLOAT opd_re = pd_re_smooth[opd_idx];
908
1144
                INTFLOAT opd_im = pd_im_smooth[opd_idx];
909
1144
                INTFLOAT ipd_re = pd_re_smooth[ipd_idx];
910
1144
                INTFLOAT ipd_im = pd_im_smooth[ipd_idx];
911
1144
                opd_hist[b] = opd_idx & 0x3F;
912
1144
                ipd_hist[b] = ipd_idx & 0x3F;
913
914
1144
                ipd_adj_re = AAC_MADD30(opd_re, ipd_re, opd_im, ipd_im);
915
1144
                ipd_adj_im = AAC_MSUB30(opd_im, ipd_re, opd_re, ipd_im);
916
1144
                h11i = AAC_MUL30(h11,  opd_im);
917
1144
                h11  = AAC_MUL30(h11,  opd_re);
918
1144
                h12i = AAC_MUL30(h12,  ipd_adj_im);
919
1144
                h12  = AAC_MUL30(h12,  ipd_adj_re);
920
1144
                h21i = AAC_MUL30(h21,  opd_im);
921
1144
                h21  = AAC_MUL30(h21,  opd_re);
922
1144
                h22i = AAC_MUL30(h22,  ipd_adj_im);
923
1144
                h22  = AAC_MUL30(h22,  ipd_adj_re);
924
1144
                H11[1][e+1][b] = h11i;
925
1144
                H12[1][e+1][b] = h12i;
926
1144
                H21[1][e+1][b] = h21i;
927
1144
                H22[1][e+1][b] = h22i;
928
            }
929
43516
            H11[0][e+1][b] = h11;
930
43516
            H12[0][e+1][b] = h12;
931
43516
            H21[0][e+1][b] = h21;
932
43516
            H22[0][e+1][b] = h22;
933
        }
934
150456
        for (k = 0; k < NR_BANDS[is34]; k++) {
935
148423
            LOCAL_ALIGNED_16(INTFLOAT, h, [2], [4]);
936
148423
            LOCAL_ALIGNED_16(INTFLOAT, h_step, [2], [4]);
937
148423
            int start = ps->border_position[e];
938
148423
            int stop  = ps->border_position[e+1];
939
148423
            INTFLOAT width = Q30(1.f) / ((stop - start) ? (stop - start) : 1);
940
#if USE_FIXED
941
            width = FFMIN(2U*width, INT_MAX);
942
#endif
943
148423
            b = k_to_i[k];
944
148423
            h[0][0] = H11[0][e][b];
945
148423
            h[0][1] = H12[0][e][b];
946
148423
            h[0][2] = H21[0][e][b];
947
148423
            h[0][3] = H22[0][e][b];
948
148423
            if (!PS_BASELINE && ps->enable_ipdopd) {
949
            //Is this necessary? ps_04_new seems unchanged
950


7384
            if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
951
208
                h[1][0] = -H11[1][e][b];
952
208
                h[1][1] = -H12[1][e][b];
953
208
                h[1][2] = -H21[1][e][b];
954
208
                h[1][3] = -H22[1][e][b];
955
            } else {
956
7176
                h[1][0] = H11[1][e][b];
957
7176
                h[1][1] = H12[1][e][b];
958
7176
                h[1][2] = H21[1][e][b];
959
7176
                h[1][3] = H22[1][e][b];
960
            }
961
            }
962
            //Interpolation
963
148423
            h_step[0][0] = AAC_MSUB31_V3(H11[0][e+1][b], h[0][0], width);
964
148423
            h_step[0][1] = AAC_MSUB31_V3(H12[0][e+1][b], h[0][1], width);
965
148423
            h_step[0][2] = AAC_MSUB31_V3(H21[0][e+1][b], h[0][2], width);
966
148423
            h_step[0][3] = AAC_MSUB31_V3(H22[0][e+1][b], h[0][3], width);
967
148423
            if (!PS_BASELINE && ps->enable_ipdopd) {
968
7384
                h_step[1][0] = AAC_MSUB31_V3(H11[1][e+1][b], h[1][0], width);
969
7384
                h_step[1][1] = AAC_MSUB31_V3(H12[1][e+1][b], h[1][1], width);
970
7384
                h_step[1][2] = AAC_MSUB31_V3(H21[1][e+1][b], h[1][2], width);
971
7384
                h_step[1][3] = AAC_MSUB31_V3(H22[1][e+1][b], h[1][3], width);
972
            }
973
148423
            if (stop - start)
974
148423
                ps->dsp.stereo_interpolate[!PS_BASELINE && ps->enable_ipdopd](
975
148423
                    l[k] + 1 + start, r[k] + 1 + start,
976
                    h, h_step, stop - start);
977
        }
978
    }
979
1616
}
980
981
1616
int AAC_RENAME(ff_ps_apply)(AVCodecContext *avctx, PSContext *ps, INTFLOAT L[2][38][64], INTFLOAT R[2][38][64], int top)
982
{
983
1616
    INTFLOAT (*Lbuf)[32][2] = ps->Lbuf;
984
1616
    INTFLOAT (*Rbuf)[32][2] = ps->Rbuf;
985
1616
    const int len = 32;
986
1616
    int is34 = ps->is34bands;
987
988
1616
    top += NR_BANDS[is34] - 64;
989
1616
    memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
990
1616
    if (top < NR_ALLPASS_BANDS[is34])
991
        memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
992
993
1616
    hybrid_analysis(&ps->dsp, Lbuf, ps->in_buf, L, is34, len);
994
1616
    decorrelation(ps, Rbuf, (const INTFLOAT (*)[32][2]) Lbuf, is34);
995
1616
    stereo_processing(ps, Lbuf, Rbuf, is34);
996
1616
    hybrid_synthesis(&ps->dsp, L, Lbuf, is34, len);
997
1616
    hybrid_synthesis(&ps->dsp, R, Rbuf, is34, len);
998
999
1616
    return 0;
1000
}
1001
1002
#define PS_INIT_VLC_STATIC(num, size) \
1003
    INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size,    \
1004
                    ps_tmp[num].ps_bits, 1, 1,                                          \
1005
                    ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
1006
                    size);
1007
1008
#define PS_VLC_ROW(name) \
1009
    { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
1010
1011
164
av_cold void AAC_RENAME(ff_ps_init)(void) {
1012
    // Syntax initialization
1013
    static const struct {
1014
        const void *ps_codes, *ps_bits;
1015
        const unsigned int table_size, elem_size;
1016
    } ps_tmp[] = {
1017
        PS_VLC_ROW(huff_iid_df1),
1018
        PS_VLC_ROW(huff_iid_dt1),
1019
        PS_VLC_ROW(huff_iid_df0),
1020
        PS_VLC_ROW(huff_iid_dt0),
1021
        PS_VLC_ROW(huff_icc_df),
1022
        PS_VLC_ROW(huff_icc_dt),
1023
        PS_VLC_ROW(huff_ipd_df),
1024
        PS_VLC_ROW(huff_ipd_dt),
1025
        PS_VLC_ROW(huff_opd_df),
1026
        PS_VLC_ROW(huff_opd_dt),
1027
    };
1028
1029
164
    PS_INIT_VLC_STATIC(0, 1544);
1030
164
    PS_INIT_VLC_STATIC(1,  832);
1031
164
    PS_INIT_VLC_STATIC(2, 1024);
1032
164
    PS_INIT_VLC_STATIC(3, 1036);
1033
164
    PS_INIT_VLC_STATIC(4,  544);
1034
164
    PS_INIT_VLC_STATIC(5,  544);
1035
164
    PS_INIT_VLC_STATIC(6,  512);
1036
164
    PS_INIT_VLC_STATIC(7,  512);
1037
164
    PS_INIT_VLC_STATIC(8,  512);
1038
164
    PS_INIT_VLC_STATIC(9,  512);
1039
1040
164
    ps_tableinit();
1041
164
}
1042
1043
347
av_cold void AAC_RENAME(ff_ps_ctx_init)(PSContext *ps)
1044
{
1045
347
    AAC_RENAME(ff_psdsp_init)(&ps->dsp);
1046
347
}