Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ps.c @ 78ba1d39

History | View | Annotate | Download (37.7 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  //< Operate in Baseline PS mode
31
                       //< Baseline implies 10 or 20 stereo bands,
32
                       //< mixing mode A, and no ipd/opd
33

    
34
#define numQMFSlots 32 //numTimeSlots * RATE
35

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

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

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

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

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

    
69
static VLC vlc_ps[10];
70

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

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

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

    
124
    if (ps_extension_id)
125
        return 0;
126

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

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

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

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

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

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

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

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

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

    
231
    ps->enable_ipdopd &= !PS_BASELINE;
232

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

    
253

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
992
    return 0;
993
}
994

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

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

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

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

    
1033
    ps_tableinit();
1034
}
1035

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