Statistics
| Branch: | Revision:

ffmpeg / libavcodec / aacps.c @ 2912e87a

History | View | Annotate | Download (38 KB)

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 Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include <stdint.h>
23
#include "libavutil/common.h"
24
#include "libavutil/mathematics.h"
25
#include "avcodec.h"
26
#include "get_bits.h"
27
#include "aacps.h"
28
#include "aacps_tablegen.h"
29
#include "aacpsdata.c"
30

    
31
#define PS_BASELINE 0  //< Operate in Baseline PS mode
32
                       //< Baseline implies 10 or 20 stereo bands,
33
                       //< mixing mode A, and no ipd/opd
34

    
35
#define numQMFSlots 32 //numTimeSlots * RATE
36

    
37
static const int8_t num_env_tab[2][4] = {
38
    { 0, 1, 2, 4, },
39
    { 1, 2, 3, 4, },
40
};
41

    
42
static const int8_t nr_iidicc_par_tab[] = {
43
    10, 20, 34, 10, 20, 34,
44
};
45

    
46
static const int8_t nr_iidopd_par_tab[] = {
47
     5, 11, 17,  5, 11, 17,
48
};
49

    
50
enum {
51
    huff_iid_df1,
52
    huff_iid_dt1,
53
    huff_iid_df0,
54
    huff_iid_dt0,
55
    huff_icc_df,
56
    huff_icc_dt,
57
    huff_ipd_df,
58
    huff_ipd_dt,
59
    huff_opd_df,
60
    huff_opd_dt,
61
};
62

    
63
static const int huff_iid[] = {
64
    huff_iid_df0,
65
    huff_iid_df1,
66
    huff_iid_dt0,
67
    huff_iid_dt1,
68
};
69

    
70
static VLC vlc_ps[10];
71

    
72
/**
73
 * Read Inter-channel Intensity Difference/Inter-Channel Coherence/
74
 * Inter-channel Phase Difference/Overall Phase Difference parameters from the
75
 * bitstream.
76
 *
77
 * @param avctx contains the current codec context
78
 * @param gb    pointer to the input bitstream
79
 * @param ps    pointer to the Parametric Stereo context
80
 * @param par   pointer to the parameter to be read
81
 * @param e     envelope to decode
82
 * @param dt    1: time delta-coded, 0: frequency delta-coded
83
 */
84
#define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
85
static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
86
                        int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
87
{ \
88
    int b, num = ps->nr_ ## PAR ## _par; \
89
    VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
90
    if (dt) { \
91
        int e_prev = e ? e - 1 : ps->num_env_old - 1; \
92
        e_prev = FFMAX(e_prev, 0); \
93
        for (b = 0; b < num; b++) { \
94
            int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
95
            if (MASK) val &= MASK; \
96
            PAR[e][b] = val; \
97
            if (ERR_CONDITION) \
98
                goto err; \
99
        } \
100
    } else { \
101
        int val = 0; \
102
        for (b = 0; b < num; b++) { \
103
            val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
104
            if (MASK) val &= MASK; \
105
            PAR[e][b] = val; \
106
            if (ERR_CONDITION) \
107
                goto err; \
108
        } \
109
    } \
110
    return 0; \
111
err: \
112
    av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
113
    return -1; \
114
}
115

    
116
READ_PAR_DATA(iid,    huff_offset[table_idx],    0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
117
READ_PAR_DATA(icc,    huff_offset[table_idx],    0, ps->icc_par[e][b] > 7U)
118
READ_PAR_DATA(ipdopd,                      0, 0x07, 0)
119

    
120
static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
121
{
122
    int e;
123
    int count = get_bits_count(gb);
124

    
125
    if (ps_extension_id)
126
        return 0;
127

    
128
    ps->enable_ipdopd = get_bits1(gb);
129
    if (ps->enable_ipdopd) {
130
        for (e = 0; e < ps->num_env; e++) {
131
            int dt = get_bits1(gb);
132
            read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
133
            dt = get_bits1(gb);
134
            read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
135
        }
136
    }
137
    skip_bits1(gb);      //reserved_ps
138
    return get_bits_count(gb) - count;
139
}
140

    
141
static void ipdopd_reset(int8_t *opd_hist, int8_t *ipd_hist)
142
{
143
    int i;
144
    for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
145
        opd_hist[i] = 0;
146
        ipd_hist[i] = 0;
147
    }
148
}
149

    
150
int ff_ps_read_data(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
151
{
152
    int e;
153
    int bit_count_start = get_bits_count(gb_host);
154
    int header;
155
    int bits_consumed;
156
    GetBitContext gbc = *gb_host, *gb = &gbc;
157

    
158
    header = get_bits1(gb);
159
    if (header) {     //enable_ps_header
160
        ps->enable_iid = get_bits1(gb);
161
        if (ps->enable_iid) {
162
            int iid_mode = get_bits(gb, 3);
163
            if (iid_mode > 5) {
164
                av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
165
                       iid_mode);
166
                goto err;
167
            }
168
            ps->nr_iid_par    = nr_iidicc_par_tab[iid_mode];
169
            ps->iid_quant     = iid_mode > 2;
170
            ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
171
        }
172
        ps->enable_icc = get_bits1(gb);
173
        if (ps->enable_icc) {
174
            ps->icc_mode = get_bits(gb, 3);
175
            if (ps->icc_mode > 5) {
176
                av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
177
                       ps->icc_mode);
178
                goto err;
179
            }
180
            ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode];
181
        }
182
        ps->enable_ext = get_bits1(gb);
183
    }
184

    
185
    ps->frame_class = get_bits1(gb);
186
    ps->num_env_old = ps->num_env;
187
    ps->num_env     = num_env_tab[ps->frame_class][get_bits(gb, 2)];
188

    
189
    ps->border_position[0] = -1;
190
    if (ps->frame_class) {
191
        for (e = 1; e <= ps->num_env; e++)
192
            ps->border_position[e] = get_bits(gb, 5);
193
    } else
194
        for (e = 1; e <= ps->num_env; e++)
195
            ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
196

    
197
    if (ps->enable_iid) {
198
        for (e = 0; e < ps->num_env; e++) {
199
            int dt = get_bits1(gb);
200
            if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
201
                goto err;
202
        }
203
    } else
204
        memset(ps->iid_par, 0, sizeof(ps->iid_par));
205

    
206
    if (ps->enable_icc)
207
        for (e = 0; e < ps->num_env; e++) {
208
            int dt = get_bits1(gb);
209
            if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
210
                goto err;
211
        }
212
    else
213
        memset(ps->icc_par, 0, sizeof(ps->icc_par));
214

    
215
    if (ps->enable_ext) {
216
        int cnt = get_bits(gb, 4);
217
        if (cnt == 15) {
218
            cnt += get_bits(gb, 8);
219
        }
220
        cnt *= 8;
221
        while (cnt > 7) {
222
            int ps_extension_id = get_bits(gb, 2);
223
            cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
224
        }
225
        if (cnt < 0) {
226
            av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d", cnt);
227
            goto err;
228
        }
229
        skip_bits(gb, cnt);
230
    }
231

    
232
    ps->enable_ipdopd &= !PS_BASELINE;
233

    
234
    //Fix up envelopes
235
    if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
236
        //Create a fake envelope
237
        int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
238
        if (source >= 0 && source != ps->num_env) {
239
            if (ps->enable_iid) {
240
                memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
241
            }
242
            if (ps->enable_icc) {
243
                memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
244
            }
245
            if (ps->enable_ipdopd) {
246
                memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
247
                memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
248
            }
249
        }
250
        ps->num_env++;
251
        ps->border_position[ps->num_env] = numQMFSlots - 1;
252
    }
253

    
254

    
255
    ps->is34bands_old = ps->is34bands;
256
    if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
257
        ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
258
                        (ps->enable_icc && ps->nr_icc_par == 34);
259

    
260
    //Baseline
261
    if (!ps->enable_ipdopd) {
262
        memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
263
        memset(ps->opd_par, 0, sizeof(ps->opd_par));
264
    }
265

    
266
    if (header)
267
        ps->start = 1;
268

    
269
    bits_consumed = get_bits_count(gb) - bit_count_start;
270
    if (bits_consumed <= bits_left) {
271
        skip_bits_long(gb_host, bits_consumed);
272
        return bits_consumed;
273
    }
274
    av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
275
err:
276
    ps->start = 0;
277
    skip_bits_long(gb_host, bits_left);
278
    return bits_left;
279
}
280

    
281
/** Split one subband into 2 subsubbands with a symmetric real filter.
282
 * The filter must have its non-center even coefficients equal to zero. */
283
static void hybrid2_re(float (*in)[2], float (*out)[32][2], const float filter[7], int len, int reverse)
284
{
285
    int i, j;
286
    for (i = 0; i < len; i++, in++) {
287
        float re_in = filter[6] * in[6][0];          //real inphase
288
        float re_op = 0.0f;                          //real out of phase
289
        float im_in = filter[6] * in[6][1];          //imag inphase
290
        float im_op = 0.0f;                          //imag out of phase
291
        for (j = 0; j < 6; j += 2) {
292
            re_op += filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
293
            im_op += filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
294
        }
295
        out[ reverse][i][0] = re_in + re_op;
296
        out[ reverse][i][1] = im_in + im_op;
297
        out[!reverse][i][0] = re_in - re_op;
298
        out[!reverse][i][1] = im_in - im_op;
299
    }
300
}
301

    
302
/** Split one subband into 6 subsubbands with a complex filter */
303
static void hybrid6_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int len)
304
{
305
    int i, j, ssb;
306
    int N = 8;
307
    float temp[8][2];
308

    
309
    for (i = 0; i < len; i++, in++) {
310
        for (ssb = 0; ssb < N; ssb++) {
311
            float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1];
312
            for (j = 0; j < 6; j++) {
313
                float in0_re = in[j][0];
314
                float in0_im = in[j][1];
315
                float in1_re = in[12-j][0];
316
                float in1_im = in[12-j][1];
317
                sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im);
318
                sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re);
319
            }
320
            temp[ssb][0] = sum_re;
321
            temp[ssb][1] = sum_im;
322
        }
323
        out[0][i][0] = temp[6][0];
324
        out[0][i][1] = temp[6][1];
325
        out[1][i][0] = temp[7][0];
326
        out[1][i][1] = temp[7][1];
327
        out[2][i][0] = temp[0][0];
328
        out[2][i][1] = temp[0][1];
329
        out[3][i][0] = temp[1][0];
330
        out[3][i][1] = temp[1][1];
331
        out[4][i][0] = temp[2][0] + temp[5][0];
332
        out[4][i][1] = temp[2][1] + temp[5][1];
333
        out[5][i][0] = temp[3][0] + temp[4][0];
334
        out[5][i][1] = temp[3][1] + temp[4][1];
335
    }
336
}
337

    
338
static void hybrid4_8_12_cx(float (*in)[2], float (*out)[32][2], const float (*filter)[7][2], int N, int len)
339
{
340
    int i, j, ssb;
341

    
342
    for (i = 0; i < len; i++, in++) {
343
        for (ssb = 0; ssb < N; ssb++) {
344
            float sum_re = filter[ssb][6][0] * in[6][0], sum_im = filter[ssb][6][0] * in[6][1];
345
            for (j = 0; j < 6; j++) {
346
                float in0_re = in[j][0];
347
                float in0_im = in[j][1];
348
                float in1_re = in[12-j][0];
349
                float in1_im = in[12-j][1];
350
                sum_re += filter[ssb][j][0] * (in0_re + in1_re) - filter[ssb][j][1] * (in0_im - in1_im);
351
                sum_im += filter[ssb][j][0] * (in0_im + in1_im) + filter[ssb][j][1] * (in0_re - in1_re);
352
            }
353
            out[ssb][i][0] = sum_re;
354
            out[ssb][i][1] = sum_im;
355
        }
356
    }
357
}
358

    
359
static void hybrid_analysis(float out[91][32][2], float in[5][44][2], float L[2][38][64], int is34, int len)
360
{
361
    int i, j;
362
    for (i = 0; i < 5; i++) {
363
        for (j = 0; j < 38; j++) {
364
            in[i][j+6][0] = L[0][j][i];
365
            in[i][j+6][1] = L[1][j][i];
366
        }
367
    }
368
    if (is34) {
369
        hybrid4_8_12_cx(in[0], out,    f34_0_12, 12, len);
370
        hybrid4_8_12_cx(in[1], out+12, f34_1_8,   8, len);
371
        hybrid4_8_12_cx(in[2], out+20, f34_2_4,   4, len);
372
        hybrid4_8_12_cx(in[3], out+24, f34_2_4,   4, len);
373
        hybrid4_8_12_cx(in[4], out+28, f34_2_4,   4, len);
374
        for (i = 0; i < 59; i++) {
375
            for (j = 0; j < len; j++) {
376
                out[i+32][j][0] = L[0][j][i+5];
377
                out[i+32][j][1] = L[1][j][i+5];
378
            }
379
        }
380
    } else {
381
        hybrid6_cx(in[0], out, f20_0_8, len);
382
        hybrid2_re(in[1], out+6, g1_Q2, len, 1);
383
        hybrid2_re(in[2], out+8, g1_Q2, len, 0);
384
        for (i = 0; i < 61; i++) {
385
            for (j = 0; j < len; j++) {
386
                out[i+10][j][0] = L[0][j][i+3];
387
                out[i+10][j][1] = L[1][j][i+3];
388
            }
389
        }
390
    }
391
    //update in_buf
392
    for (i = 0; i < 5; i++) {
393
        memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
394
    }
395
}
396

    
397
static void hybrid_synthesis(float out[2][38][64], float in[91][32][2], int is34, int len)
398
{
399
    int i, n;
400
    if (is34) {
401
        for (n = 0; n < len; n++) {
402
            memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
403
            memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
404
            for (i = 0; i < 12; i++) {
405
                out[0][n][0] += in[   i][n][0];
406
                out[1][n][0] += in[   i][n][1];
407
            }
408
            for (i = 0; i < 8; i++) {
409
                out[0][n][1] += in[12+i][n][0];
410
                out[1][n][1] += in[12+i][n][1];
411
            }
412
            for (i = 0; i < 4; i++) {
413
                out[0][n][2] += in[20+i][n][0];
414
                out[1][n][2] += in[20+i][n][1];
415
                out[0][n][3] += in[24+i][n][0];
416
                out[1][n][3] += in[24+i][n][1];
417
                out[0][n][4] += in[28+i][n][0];
418
                out[1][n][4] += in[28+i][n][1];
419
            }
420
        }
421
        for (i = 0; i < 59; i++) {
422
            for (n = 0; n < len; n++) {
423
                out[0][n][i+5] = in[i+32][n][0];
424
                out[1][n][i+5] = in[i+32][n][1];
425
            }
426
        }
427
    } else {
428
        for (n = 0; n < len; n++) {
429
            out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +
430
                           in[3][n][0] + in[4][n][0] + in[5][n][0];
431
            out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +
432
                           in[3][n][1] + in[4][n][1] + in[5][n][1];
433
            out[0][n][1] = in[6][n][0] + in[7][n][0];
434
            out[1][n][1] = in[6][n][1] + in[7][n][1];
435
            out[0][n][2] = in[8][n][0] + in[9][n][0];
436
            out[1][n][2] = in[8][n][1] + in[9][n][1];
437
        }
438
        for (i = 0; i < 61; i++) {
439
            for (n = 0; n < len; n++) {
440
                out[0][n][i+3] = in[i+10][n][0];
441
                out[1][n][i+3] = in[i+10][n][1];
442
            }
443
        }
444
    }
445
}
446

    
447
/// All-pass filter decay slope
448
#define DECAY_SLOPE      0.05f
449
/// Number of frequency bands that can be addressed by the parameter index, b(k)
450
static const int   NR_PAR_BANDS[]      = { 20, 34 };
451
/// Number of frequency bands that can be addressed by the sub subband index, k
452
static const int   NR_BANDS[]          = { 71, 91 };
453
/// Start frequency band for the all-pass filter decay slope
454
static const int   DECAY_CUTOFF[]      = { 10, 32 };
455
/// Number of all-pass filer bands
456
static const int   NR_ALLPASS_BANDS[]  = { 30, 50 };
457
/// First stereo band using the short one sample delay
458
static const int   SHORT_DELAY_BAND[]  = { 42, 62 };
459

    
460
/** Table 8.46 */
461
static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
462
{
463
    int b;
464
    if (full)
465
        b = 9;
466
    else {
467
        b = 4;
468
        par_mapped[10] = 0;
469
    }
470
    for (; b >= 0; b--) {
471
        par_mapped[2*b+1] = par_mapped[2*b] = par[b];
472
    }
473
}
474

    
475
static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
476
{
477
    par_mapped[ 0] = (2*par[ 0] +   par[ 1]) / 3;
478
    par_mapped[ 1] = (  par[ 1] + 2*par[ 2]) / 3;
479
    par_mapped[ 2] = (2*par[ 3] +   par[ 4]) / 3;
480
    par_mapped[ 3] = (  par[ 4] + 2*par[ 5]) / 3;
481
    par_mapped[ 4] = (  par[ 6] +   par[ 7]) / 2;
482
    par_mapped[ 5] = (  par[ 8] +   par[ 9]) / 2;
483
    par_mapped[ 6] =    par[10];
484
    par_mapped[ 7] =    par[11];
485
    par_mapped[ 8] = (  par[12] +   par[13]) / 2;
486
    par_mapped[ 9] = (  par[14] +   par[15]) / 2;
487
    par_mapped[10] =    par[16];
488
    if (full) {
489
        par_mapped[11] =    par[17];
490
        par_mapped[12] =    par[18];
491
        par_mapped[13] =    par[19];
492
        par_mapped[14] = (  par[20] +   par[21]) / 2;
493
        par_mapped[15] = (  par[22] +   par[23]) / 2;
494
        par_mapped[16] = (  par[24] +   par[25]) / 2;
495
        par_mapped[17] = (  par[26] +   par[27]) / 2;
496
        par_mapped[18] = (  par[28] +   par[29] +   par[30] +   par[31]) / 4;
497
        par_mapped[19] = (  par[32] +   par[33]) / 2;
498
    }
499
}
500

    
501
static void map_val_34_to_20(float par[PS_MAX_NR_IIDICC])
502
{
503
    par[ 0] = (2*par[ 0] +   par[ 1]) * 0.33333333f;
504
    par[ 1] = (  par[ 1] + 2*par[ 2]) * 0.33333333f;
505
    par[ 2] = (2*par[ 3] +   par[ 4]) * 0.33333333f;
506
    par[ 3] = (  par[ 4] + 2*par[ 5]) * 0.33333333f;
507
    par[ 4] = (  par[ 6] +   par[ 7]) * 0.5f;
508
    par[ 5] = (  par[ 8] +   par[ 9]) * 0.5f;
509
    par[ 6] =    par[10];
510
    par[ 7] =    par[11];
511
    par[ 8] = (  par[12] +   par[13]) * 0.5f;
512
    par[ 9] = (  par[14] +   par[15]) * 0.5f;
513
    par[10] =    par[16];
514
    par[11] =    par[17];
515
    par[12] =    par[18];
516
    par[13] =    par[19];
517
    par[14] = (  par[20] +   par[21]) * 0.5f;
518
    par[15] = (  par[22] +   par[23]) * 0.5f;
519
    par[16] = (  par[24] +   par[25]) * 0.5f;
520
    par[17] = (  par[26] +   par[27]) * 0.5f;
521
    par[18] = (  par[28] +   par[29] +   par[30] +   par[31]) * 0.25f;
522
    par[19] = (  par[32] +   par[33]) * 0.5f;
523
}
524

    
525
static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
526
{
527
    if (full) {
528
        par_mapped[33] = par[9];
529
        par_mapped[32] = par[9];
530
        par_mapped[31] = par[9];
531
        par_mapped[30] = par[9];
532
        par_mapped[29] = par[9];
533
        par_mapped[28] = par[9];
534
        par_mapped[27] = par[8];
535
        par_mapped[26] = par[8];
536
        par_mapped[25] = par[8];
537
        par_mapped[24] = par[8];
538
        par_mapped[23] = par[7];
539
        par_mapped[22] = par[7];
540
        par_mapped[21] = par[7];
541
        par_mapped[20] = par[7];
542
        par_mapped[19] = par[6];
543
        par_mapped[18] = par[6];
544
        par_mapped[17] = par[5];
545
        par_mapped[16] = par[5];
546
    } else {
547
        par_mapped[16] =      0;
548
    }
549
    par_mapped[15] = par[4];
550
    par_mapped[14] = par[4];
551
    par_mapped[13] = par[4];
552
    par_mapped[12] = par[4];
553
    par_mapped[11] = par[3];
554
    par_mapped[10] = par[3];
555
    par_mapped[ 9] = par[2];
556
    par_mapped[ 8] = par[2];
557
    par_mapped[ 7] = par[2];
558
    par_mapped[ 6] = par[2];
559
    par_mapped[ 5] = par[1];
560
    par_mapped[ 4] = par[1];
561
    par_mapped[ 3] = par[1];
562
    par_mapped[ 2] = par[0];
563
    par_mapped[ 1] = par[0];
564
    par_mapped[ 0] = par[0];
565
}
566

    
567
static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
568
{
569
    if (full) {
570
        par_mapped[33] =  par[19];
571
        par_mapped[32] =  par[19];
572
        par_mapped[31] =  par[18];
573
        par_mapped[30] =  par[18];
574
        par_mapped[29] =  par[18];
575
        par_mapped[28] =  par[18];
576
        par_mapped[27] =  par[17];
577
        par_mapped[26] =  par[17];
578
        par_mapped[25] =  par[16];
579
        par_mapped[24] =  par[16];
580
        par_mapped[23] =  par[15];
581
        par_mapped[22] =  par[15];
582
        par_mapped[21] =  par[14];
583
        par_mapped[20] =  par[14];
584
        par_mapped[19] =  par[13];
585
        par_mapped[18] =  par[12];
586
        par_mapped[17] =  par[11];
587
    }
588
    par_mapped[16] =  par[10];
589
    par_mapped[15] =  par[ 9];
590
    par_mapped[14] =  par[ 9];
591
    par_mapped[13] =  par[ 8];
592
    par_mapped[12] =  par[ 8];
593
    par_mapped[11] =  par[ 7];
594
    par_mapped[10] =  par[ 6];
595
    par_mapped[ 9] =  par[ 5];
596
    par_mapped[ 8] =  par[ 5];
597
    par_mapped[ 7] =  par[ 4];
598
    par_mapped[ 6] =  par[ 4];
599
    par_mapped[ 5] =  par[ 3];
600
    par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
601
    par_mapped[ 3] =  par[ 2];
602
    par_mapped[ 2] =  par[ 1];
603
    par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
604
    par_mapped[ 0] =  par[ 0];
605
}
606

    
607
static void map_val_20_to_34(float par[PS_MAX_NR_IIDICC])
608
{
609
    par[33] =  par[19];
610
    par[32] =  par[19];
611
    par[31] =  par[18];
612
    par[30] =  par[18];
613
    par[29] =  par[18];
614
    par[28] =  par[18];
615
    par[27] =  par[17];
616
    par[26] =  par[17];
617
    par[25] =  par[16];
618
    par[24] =  par[16];
619
    par[23] =  par[15];
620
    par[22] =  par[15];
621
    par[21] =  par[14];
622
    par[20] =  par[14];
623
    par[19] =  par[13];
624
    par[18] =  par[12];
625
    par[17] =  par[11];
626
    par[16] =  par[10];
627
    par[15] =  par[ 9];
628
    par[14] =  par[ 9];
629
    par[13] =  par[ 8];
630
    par[12] =  par[ 8];
631
    par[11] =  par[ 7];
632
    par[10] =  par[ 6];
633
    par[ 9] =  par[ 5];
634
    par[ 8] =  par[ 5];
635
    par[ 7] =  par[ 4];
636
    par[ 6] =  par[ 4];
637
    par[ 5] =  par[ 3];
638
    par[ 4] = (par[ 2] + par[ 3]) * 0.5f;
639
    par[ 3] =  par[ 2];
640
    par[ 2] =  par[ 1];
641
    par[ 1] = (par[ 0] + par[ 1]) * 0.5f;
642
    par[ 0] =  par[ 0];
643
}
644

    
645
static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34)
646
{
647
    float power[34][PS_QMF_TIME_SLOTS] = {{0}};
648
    float transient_gain[34][PS_QMF_TIME_SLOTS];
649
    float *peak_decay_nrg = ps->peak_decay_nrg;
650
    float *power_smooth = ps->power_smooth;
651
    float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
652
    float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
653
    float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
654
    const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
655
    const float peak_decay_factor = 0.76592833836465f;
656
    const float transient_impact  = 1.5f;
657
    const float a_smooth          = 0.25f; //< Smoothing coefficient
658
    int i, k, m, n;
659
    int n0 = 0, nL = 32;
660
    static const int link_delay[] = { 3, 4, 5 };
661
    static const float a[] = { 0.65143905753106f,
662
                               0.56471812200776f,
663
                               0.48954165955695f };
664

    
665
    if (is34 != ps->is34bands_old) {
666
        memset(ps->peak_decay_nrg,         0, sizeof(ps->peak_decay_nrg));
667
        memset(ps->power_smooth,           0, sizeof(ps->power_smooth));
668
        memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
669
        memset(ps->delay,                  0, sizeof(ps->delay));
670
        memset(ps->ap_delay,               0, sizeof(ps->ap_delay));
671
    }
672

    
673
    for (n = n0; n < nL; n++) {
674
        for (k = 0; k < NR_BANDS[is34]; k++) {
675
            int i = k_to_i[k];
676
            power[i][n] += s[k][n][0] * s[k][n][0] + s[k][n][1] * s[k][n][1];
677
        }
678
    }
679

    
680
    //Transient detection
681
    for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
682
        for (n = n0; n < nL; n++) {
683
            float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
684
            float denom;
685
            peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
686
            power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
687
            peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
688
            denom = transient_impact * peak_decay_diff_smooth[i];
689
            transient_gain[i][n]   = (denom > power_smooth[i]) ?
690
                                         power_smooth[i] / denom : 1.0f;
691
        }
692
    }
693

    
694
    //Decorrelation and transient reduction
695
    //                         PS_AP_LINKS - 1
696
    //                               -----
697
    //                                | |  Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
698
    //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
699
    //                                | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
700
    //                               m = 0
701
    //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
702
    for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
703
        int b = k_to_i[k];
704
        float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
705
        float ag[PS_AP_LINKS];
706
        g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
707
        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
708
        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
709
        for (m = 0; m < PS_AP_LINKS; m++) {
710
            memcpy(ap_delay[k][m],   ap_delay[k][m]+numQMFSlots,           5*sizeof(ap_delay[k][m][0]));
711
            ag[m] = a[m] * g_decay_slope;
712
        }
713
        for (n = n0; n < nL; n++) {
714
            float in_re = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][0] -
715
                          delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][1];
716
            float in_im = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][1] +
717
                          delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][0];
718
            for (m = 0; m < PS_AP_LINKS; m++) {
719
                float a_re                = ag[m] * in_re;
720
                float a_im                = ag[m] * in_im;
721
                float link_delay_re       = ap_delay[k][m][n+5-link_delay[m]][0];
722
                float link_delay_im       = ap_delay[k][m][n+5-link_delay[m]][1];
723
                float fractional_delay_re = Q_fract_allpass[is34][k][m][0];
724
                float fractional_delay_im = Q_fract_allpass[is34][k][m][1];
725
                ap_delay[k][m][n+5][0] = in_re;
726
                ap_delay[k][m][n+5][1] = in_im;
727
                in_re = link_delay_re * fractional_delay_re - link_delay_im * fractional_delay_im - a_re;
728
                in_im = link_delay_re * fractional_delay_im + link_delay_im * fractional_delay_re - a_im;
729
                ap_delay[k][m][n+5][0] += ag[m] * in_re;
730
                ap_delay[k][m][n+5][1] += ag[m] * in_im;
731
            }
732
            out[k][n][0] = transient_gain[b][n] * in_re;
733
            out[k][n][1] = transient_gain[b][n] * in_im;
734
        }
735
    }
736
    for (; k < SHORT_DELAY_BAND[is34]; k++) {
737
        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
738
        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
739
        for (n = n0; n < nL; n++) {
740
            //H = delay 14
741
            out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][0];
742
            out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][1];
743
        }
744
    }
745
    for (; k < NR_BANDS[is34]; k++) {
746
        memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
747
        memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
748
        for (n = n0; n < nL; n++) {
749
            //H = delay 1
750
            out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][0];
751
            out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][1];
752
        }
753
    }
754
}
755

    
756
static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
757
                    int8_t           (*par)[PS_MAX_NR_IIDICC],
758
                    int num_par, int num_env, int full)
759
{
760
    int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
761
    int e;
762
    if (num_par == 20 || num_par == 11) {
763
        for (e = 0; e < num_env; e++) {
764
            map_idx_20_to_34(par_mapped[e], par[e], full);
765
        }
766
    } else if (num_par == 10 || num_par == 5) {
767
        for (e = 0; e < num_env; e++) {
768
            map_idx_10_to_34(par_mapped[e], par[e], full);
769
        }
770
    } else {
771
        *p_par_mapped = par;
772
    }
773
}
774

    
775
static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
776
                    int8_t           (*par)[PS_MAX_NR_IIDICC],
777
                    int num_par, int num_env, int full)
778
{
779
    int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
780
    int e;
781
    if (num_par == 34 || num_par == 17) {
782
        for (e = 0; e < num_env; e++) {
783
            map_idx_34_to_20(par_mapped[e], par[e], full);
784
        }
785
    } else if (num_par == 10 || num_par == 5) {
786
        for (e = 0; e < num_env; e++) {
787
            map_idx_10_to_20(par_mapped[e], par[e], full);
788
        }
789
    } else {
790
        *p_par_mapped = par;
791
    }
792
}
793

    
794
static void stereo_processing(PSContext *ps, float (*l)[32][2], float (*r)[32][2], int is34)
795
{
796
    int e, b, k, n;
797

    
798
    float (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
799
    float (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
800
    float (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
801
    float (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
802
    int8_t *opd_hist = ps->opd_hist;
803
    int8_t *ipd_hist = ps->ipd_hist;
804
    int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
805
    int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
806
    int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
807
    int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
808
    int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
809
    int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
810
    int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
811
    int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
812
    const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
813
    const float (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
814

    
815
    //Remapping
816
    memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
817
    memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
818
    memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
819
    memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
820
    memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
821
    memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
822
    memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
823
    memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
824
    if (is34) {
825
        remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
826
        remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
827
        if (ps->enable_ipdopd) {
828
            remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
829
            remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
830
        }
831
        if (!ps->is34bands_old) {
832
            map_val_20_to_34(H11[0][0]);
833
            map_val_20_to_34(H11[1][0]);
834
            map_val_20_to_34(H12[0][0]);
835
            map_val_20_to_34(H12[1][0]);
836
            map_val_20_to_34(H21[0][0]);
837
            map_val_20_to_34(H21[1][0]);
838
            map_val_20_to_34(H22[0][0]);
839
            map_val_20_to_34(H22[1][0]);
840
            ipdopd_reset(ipd_hist, opd_hist);
841
        }
842
    } else {
843
        remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
844
        remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
845
        if (ps->enable_ipdopd) {
846
            remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
847
            remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
848
        }
849
        if (ps->is34bands_old) {
850
            map_val_34_to_20(H11[0][0]);
851
            map_val_34_to_20(H11[1][0]);
852
            map_val_34_to_20(H12[0][0]);
853
            map_val_34_to_20(H12[1][0]);
854
            map_val_34_to_20(H21[0][0]);
855
            map_val_34_to_20(H21[1][0]);
856
            map_val_34_to_20(H22[0][0]);
857
            map_val_34_to_20(H22[1][0]);
858
            ipdopd_reset(ipd_hist, opd_hist);
859
        }
860
    }
861

    
862
    //Mixing
863
    for (e = 0; e < ps->num_env; e++) {
864
        for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
865
            float h11, h12, h21, h22;
866
            h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0];
867
            h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
868
            h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
869
            h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
870
            if (!PS_BASELINE && ps->enable_ipdopd && b < ps->nr_ipdopd_par) {
871
                //The spec say says to only run this smoother when enable_ipdopd
872
                //is set but the reference decoder appears to run it constantly
873
                float h11i, h12i, h21i, h22i;
874
                float ipd_adj_re, ipd_adj_im;
875
                int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
876
                int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
877
                float opd_re = pd_re_smooth[opd_idx];
878
                float opd_im = pd_im_smooth[opd_idx];
879
                float ipd_re = pd_re_smooth[ipd_idx];
880
                float ipd_im = pd_im_smooth[ipd_idx];
881
                opd_hist[b] = opd_idx & 0x3F;
882
                ipd_hist[b] = ipd_idx & 0x3F;
883

    
884
                ipd_adj_re = opd_re*ipd_re + opd_im*ipd_im;
885
                ipd_adj_im = opd_im*ipd_re - opd_re*ipd_im;
886
                h11i = h11 * opd_im;
887
                h11  = h11 * opd_re;
888
                h12i = h12 * ipd_adj_im;
889
                h12  = h12 * ipd_adj_re;
890
                h21i = h21 * opd_im;
891
                h21  = h21 * opd_re;
892
                h22i = h22 * ipd_adj_im;
893
                h22  = h22 * ipd_adj_re;
894
                H11[1][e+1][b] = h11i;
895
                H12[1][e+1][b] = h12i;
896
                H21[1][e+1][b] = h21i;
897
                H22[1][e+1][b] = h22i;
898
            }
899
            H11[0][e+1][b] = h11;
900
            H12[0][e+1][b] = h12;
901
            H21[0][e+1][b] = h21;
902
            H22[0][e+1][b] = h22;
903
        }
904
        for (k = 0; k < NR_BANDS[is34]; k++) {
905
            float h11r, h12r, h21r, h22r;
906
            float h11i, h12i, h21i, h22i;
907
            float h11r_step, h12r_step, h21r_step, h22r_step;
908
            float h11i_step, h12i_step, h21i_step, h22i_step;
909
            int start = ps->border_position[e];
910
            int stop  = ps->border_position[e+1];
911
            float width = 1.f / (stop - start);
912
            b = k_to_i[k];
913
            h11r = H11[0][e][b];
914
            h12r = H12[0][e][b];
915
            h21r = H21[0][e][b];
916
            h22r = H22[0][e][b];
917
            if (!PS_BASELINE && ps->enable_ipdopd) {
918
            //Is this necessary? ps_04_new seems unchanged
919
            if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
920
                h11i = -H11[1][e][b];
921
                h12i = -H12[1][e][b];
922
                h21i = -H21[1][e][b];
923
                h22i = -H22[1][e][b];
924
            } else {
925
                h11i = H11[1][e][b];
926
                h12i = H12[1][e][b];
927
                h21i = H21[1][e][b];
928
                h22i = H22[1][e][b];
929
            }
930
            }
931
            //Interpolation
932
            h11r_step = (H11[0][e+1][b] - h11r) * width;
933
            h12r_step = (H12[0][e+1][b] - h12r) * width;
934
            h21r_step = (H21[0][e+1][b] - h21r) * width;
935
            h22r_step = (H22[0][e+1][b] - h22r) * width;
936
            if (!PS_BASELINE && ps->enable_ipdopd) {
937
                h11i_step = (H11[1][e+1][b] - h11i) * width;
938
                h12i_step = (H12[1][e+1][b] - h12i) * width;
939
                h21i_step = (H21[1][e+1][b] - h21i) * width;
940
                h22i_step = (H22[1][e+1][b] - h22i) * width;
941
            }
942
            for (n = start + 1; n <= stop; n++) {
943
                //l is s, r is d
944
                float l_re = l[k][n][0];
945
                float l_im = l[k][n][1];
946
                float r_re = r[k][n][0];
947
                float r_im = r[k][n][1];
948
                h11r += h11r_step;
949
                h12r += h12r_step;
950
                h21r += h21r_step;
951
                h22r += h22r_step;
952
                if (!PS_BASELINE && ps->enable_ipdopd) {
953
                    h11i += h11i_step;
954
                    h12i += h12i_step;
955
                    h21i += h21i_step;
956
                    h22i += h22i_step;
957

    
958
                    l[k][n][0] = h11r*l_re + h21r*r_re - h11i*l_im - h21i*r_im;
959
                    l[k][n][1] = h11r*l_im + h21r*r_im + h11i*l_re + h21i*r_re;
960
                    r[k][n][0] = h12r*l_re + h22r*r_re - h12i*l_im - h22i*r_im;
961
                    r[k][n][1] = h12r*l_im + h22r*r_im + h12i*l_re + h22i*r_re;
962
                } else {
963
                    l[k][n][0] = h11r*l_re + h21r*r_re;
964
                    l[k][n][1] = h11r*l_im + h21r*r_im;
965
                    r[k][n][0] = h12r*l_re + h22r*r_re;
966
                    r[k][n][1] = h12r*l_im + h22r*r_im;
967
                }
968
            }
969
        }
970
    }
971
}
972

    
973
int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top)
974
{
975
    float Lbuf[91][32][2];
976
    float Rbuf[91][32][2];
977
    const int len = 32;
978
    int is34 = ps->is34bands;
979

    
980
    top += NR_BANDS[is34] - 64;
981
    memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
982
    if (top < NR_ALLPASS_BANDS[is34])
983
        memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
984

    
985
    hybrid_analysis(Lbuf, ps->in_buf, L, is34, len);
986
    decorrelation(ps, Rbuf, Lbuf, is34);
987
    stereo_processing(ps, Lbuf, Rbuf, is34);
988
    hybrid_synthesis(L, Lbuf, is34, len);
989
    hybrid_synthesis(R, Rbuf, is34, len);
990

    
991
    return 0;
992
}
993

    
994
#define PS_INIT_VLC_STATIC(num, size) \
995
    INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size,    \
996
                    ps_tmp[num].ps_bits, 1, 1,                                          \
997
                    ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
998
                    size);
999

    
1000
#define PS_VLC_ROW(name) \
1001
    { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
1002

    
1003
av_cold void ff_ps_init(void) {
1004
    // Syntax initialization
1005
    static const struct {
1006
        const void *ps_codes, *ps_bits;
1007
        const unsigned int table_size, elem_size;
1008
    } ps_tmp[] = {
1009
        PS_VLC_ROW(huff_iid_df1),
1010
        PS_VLC_ROW(huff_iid_dt1),
1011
        PS_VLC_ROW(huff_iid_df0),
1012
        PS_VLC_ROW(huff_iid_dt0),
1013
        PS_VLC_ROW(huff_icc_df),
1014
        PS_VLC_ROW(huff_icc_dt),
1015
        PS_VLC_ROW(huff_ipd_df),
1016
        PS_VLC_ROW(huff_ipd_dt),
1017
        PS_VLC_ROW(huff_opd_df),
1018
        PS_VLC_ROW(huff_opd_dt),
1019
    };
1020

    
1021
    PS_INIT_VLC_STATIC(0, 1544);
1022
    PS_INIT_VLC_STATIC(1,  832);
1023
    PS_INIT_VLC_STATIC(2, 1024);
1024
    PS_INIT_VLC_STATIC(3, 1036);
1025
    PS_INIT_VLC_STATIC(4,  544);
1026
    PS_INIT_VLC_STATIC(5,  544);
1027
    PS_INIT_VLC_STATIC(6,  512);
1028
    PS_INIT_VLC_STATIC(7,  512);
1029
    PS_INIT_VLC_STATIC(8,  512);
1030
    PS_INIT_VLC_STATIC(9,  512);
1031

    
1032
    ps_tableinit();
1033
}
1034

    
1035
av_cold void ff_ps_ctx_init(PSContext *ps)
1036
{
1037
}