Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ps.c @ 284b041b

History | View | Annotate | Download (37.6 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 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

    
22
#include <stdint.h>
23
#include "libavutil/mathematics.h"
24
#include "avcodec.h"
25
#include "get_bits.h"
26
#include "ps.h"
27
#include "ps_tablegen.h"
28
#include "psdata.c"
29

    
30
#define PS_BASELINE 0
31

    
32
#define numQMFSlots 32 //numTimeSlots * RATE
33

    
34
static const int8_t num_env_tab[2][4] = {
35
    { 0, 1, 2, 4, },
36
    { 1, 2, 3, 4, },
37
};
38

    
39
static const int8_t nr_iidicc_par_tab[] = {
40
    10, 20, 34, 10, 20, 34,
41
};
42

    
43
static const int8_t nr_iidopd_par_tab[] = {
44
     5, 11, 17,  5, 11, 17,
45
};
46

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

    
60
static const int huff_iid[] = {
61
    huff_iid_df0,
62
    huff_iid_df1,
63
    huff_iid_dt0,
64
    huff_iid_dt1,
65
};
66

    
67
static VLC vlc_ps[10];
68

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

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

    
117
static int ps_extension(GetBitContext *gb, PSContext *ps, int ps_extension_id)
118
{
119
    int e;
120
    int count = get_bits_count(gb);
121

    
122
    if (ps_extension_id)
123
        return 0;
124

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

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

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

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

    
182
    ps->frame_class = get_bits1(gb);
183
    ps->num_env_old = ps->num_env;
184
    ps->num_env     = num_env_tab[ps->frame_class][get_bits(gb, 2)];
185

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

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

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

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

    
229
    ps->enable_ipdopd &= !PS_BASELINE;
230

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

    
251

    
252
    ps->is34bands_old = ps->is34bands;
253
    if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
254
        ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
255
                        (ps->enable_icc && ps->nr_icc_par == 34);
256

    
257
    //Baseline
258
    if (!ps->enable_ipdopd) {
259
        memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
260
        memset(ps->opd_par, 0, sizeof(ps->opd_par));
261
    }
262

    
263
    if (header)
264
        ps->start = 1;
265

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
990
    return 0;
991
}
992

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

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

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

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

    
1031
    ps_tableinit();
1032
}
1033

    
1034
av_cold void ff_ps_ctx_init(PSContext *ps)
1035
{
1036
    ipdopd_reset(ps->ipd_hist, ps->opd_hist);
1037
}