Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudiodec.c @ 525ac37f

History | View | Annotate | Download (77.7 KB)

1
/*
2
 * MPEG Audio decoder
3
 * Copyright (c) 2001, 2002 Fabrice Bellard
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
/**
23
 * @file
24
 * MPEG Audio decoder.
25
 */
26

    
27
#include "avcodec.h"
28
#include "get_bits.h"
29
#include "dsputil.h"
30

    
31
/*
32
 * TODO:
33
 *  - in low precision mode, use more 16 bit multiplies in synth filter
34
 *  - test lsf / mpeg25 extensively.
35
 */
36

    
37
#include "mpegaudio.h"
38
#include "mpegaudiodecheader.h"
39

    
40
#include "mathops.h"
41

    
42
#if CONFIG_FLOAT
43
#   define SHR(a,b)       ((a)*(1.0f/(1<<(b))))
44
#   define compute_antialias compute_antialias_float
45
#   define FIXR_OLD(a)    ((int)((a) * FRAC_ONE + 0.5))
46
#   define FIXR(x)        ((float)(x))
47
#   define FIXHR(x)       ((float)(x))
48
#   define MULH3(x, y, s) ((s)*(y)*(x))
49
#   define MULLx(x, y, s) ((y)*(x))
50
#   define RENAME(a) a ## _float
51
#else
52
#   define SHR(a,b)       ((a)>>(b))
53
#   define compute_antialias compute_antialias_integer
54
/* WARNING: only correct for posititive numbers */
55
#   define FIXR_OLD(a)    ((int)((a) * FRAC_ONE + 0.5))
56
#   define FIXR(a)        ((int)((a) * FRAC_ONE + 0.5))
57
#   define FIXHR(a)       ((int)((a) * (1LL<<32) + 0.5))
58
#   define MULH3(x, y, s) MULH((s)*(x), y)
59
#   define MULLx(x, y, s) MULL(x,y,s)
60
#   define RENAME(a)      a
61
#endif
62

    
63
/****************/
64

    
65
#define HEADER_SIZE 4
66

    
67
#include "mpegaudiodata.h"
68
#include "mpegaudiodectab.h"
69

    
70
static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
71
static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
72

    
73
/* vlc structure for decoding layer 3 huffman tables */
74
static VLC huff_vlc[16];
75
static VLC_TYPE huff_vlc_tables[
76
  0+128+128+128+130+128+154+166+
77
  142+204+190+170+542+460+662+414
78
  ][2];
79
static const int huff_vlc_tables_sizes[16] = {
80
  0, 128, 128, 128, 130, 128, 154, 166,
81
  142, 204, 190, 170, 542, 460, 662, 414
82
};
83
static VLC huff_quad_vlc[2];
84
static VLC_TYPE huff_quad_vlc_tables[128+16][2];
85
static const int huff_quad_vlc_tables_sizes[2] = {
86
  128, 16
87
};
88
/* computed from band_size_long */
89
static uint16_t band_index_long[9][23];
90
#include "mpegaudio_tablegen.h"
91
/* intensity stereo coef table */
92
static INTFLOAT is_table[2][16];
93
static INTFLOAT is_table_lsf[2][2][16];
94
static int32_t csa_table[8][4];
95
static float csa_table_float[8][4];
96
static INTFLOAT mdct_win[8][36];
97

    
98
/* lower 2 bits: modulo 3, higher bits: shift */
99
static uint16_t scale_factor_modshift[64];
100
/* [i][j]:  2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
101
static int32_t scale_factor_mult[15][3];
102
/* mult table for layer 2 group quantization */
103

    
104
#define SCALE_GEN(v) \
105
{ FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
106

    
107
static const int32_t scale_factor_mult2[3][3] = {
108
    SCALE_GEN(4.0 / 3.0), /* 3 steps */
109
    SCALE_GEN(4.0 / 5.0), /* 5 steps */
110
    SCALE_GEN(4.0 / 9.0), /* 9 steps */
111
};
112

    
113
DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512];
114

    
115
/**
116
 * Convert region offsets to region sizes and truncate
117
 * size to big_values.
118
 */
119
static void ff_region_offset2size(GranuleDef *g){
120
    int i, k, j=0;
121
    g->region_size[2] = (576 / 2);
122
    for(i=0;i<3;i++) {
123
        k = FFMIN(g->region_size[i], g->big_values);
124
        g->region_size[i] = k - j;
125
        j = k;
126
    }
127
}
128

    
129
static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
130
    if (g->block_type == 2)
131
        g->region_size[0] = (36 / 2);
132
    else {
133
        if (s->sample_rate_index <= 2)
134
            g->region_size[0] = (36 / 2);
135
        else if (s->sample_rate_index != 8)
136
            g->region_size[0] = (54 / 2);
137
        else
138
            g->region_size[0] = (108 / 2);
139
    }
140
    g->region_size[1] = (576 / 2);
141
}
142

    
143
static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
144
    int l;
145
    g->region_size[0] =
146
        band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
147
    /* should not overflow */
148
    l = FFMIN(ra1 + ra2 + 2, 22);
149
    g->region_size[1] =
150
        band_index_long[s->sample_rate_index][l] >> 1;
151
}
152

    
153
static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
154
    if (g->block_type == 2) {
155
        if (g->switch_point) {
156
            /* if switched mode, we handle the 36 first samples as
157
                long blocks.  For 8000Hz, we handle the 48 first
158
                exponents as long blocks (XXX: check this!) */
159
            if (s->sample_rate_index <= 2)
160
                g->long_end = 8;
161
            else if (s->sample_rate_index != 8)
162
                g->long_end = 6;
163
            else
164
                g->long_end = 4; /* 8000 Hz */
165

    
166
            g->short_start = 2 + (s->sample_rate_index != 8);
167
        } else {
168
            g->long_end = 0;
169
            g->short_start = 0;
170
        }
171
    } else {
172
        g->short_start = 13;
173
        g->long_end = 22;
174
    }
175
}
176

    
177
/* layer 1 unscaling */
178
/* n = number of bits of the mantissa minus 1 */
179
static inline int l1_unscale(int n, int mant, int scale_factor)
180
{
181
    int shift, mod;
182
    int64_t val;
183

    
184
    shift = scale_factor_modshift[scale_factor];
185
    mod = shift & 3;
186
    shift >>= 2;
187
    val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
188
    shift += n;
189
    /* NOTE: at this point, 1 <= shift >= 21 + 15 */
190
    return (int)((val + (1LL << (shift - 1))) >> shift);
191
}
192

    
193
static inline int l2_unscale_group(int steps, int mant, int scale_factor)
194
{
195
    int shift, mod, val;
196

    
197
    shift = scale_factor_modshift[scale_factor];
198
    mod = shift & 3;
199
    shift >>= 2;
200

    
201
    val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
202
    /* NOTE: at this point, 0 <= shift <= 21 */
203
    if (shift > 0)
204
        val = (val + (1 << (shift - 1))) >> shift;
205
    return val;
206
}
207

    
208
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
209
static inline int l3_unscale(int value, int exponent)
210
{
211
    unsigned int m;
212
    int e;
213

    
214
    e = table_4_3_exp  [4*value + (exponent&3)];
215
    m = table_4_3_value[4*value + (exponent&3)];
216
    e -= (exponent >> 2);
217
    assert(e>=1);
218
    if (e > 31)
219
        return 0;
220
    m = (m + (1 << (e-1))) >> e;
221

    
222
    return m;
223
}
224

    
225
/* all integer n^(4/3) computation code */
226
#define DEV_ORDER 13
227

    
228
#define POW_FRAC_BITS 24
229
#define POW_FRAC_ONE    (1 << POW_FRAC_BITS)
230
#define POW_FIX(a)   ((int)((a) * POW_FRAC_ONE))
231
#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
232

    
233
static int dev_4_3_coefs[DEV_ORDER];
234

    
235
#if 0 /* unused */
236
static int pow_mult3[3] = {
237
    POW_FIX(1.0),
238
    POW_FIX(1.25992104989487316476),
239
    POW_FIX(1.58740105196819947474),
240
};
241
#endif
242

    
243
static av_cold void int_pow_init(void)
244
{
245
    int i, a;
246

    
247
    a = POW_FIX(1.0);
248
    for(i=0;i<DEV_ORDER;i++) {
249
        a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
250
        dev_4_3_coefs[i] = a;
251
    }
252
}
253

    
254
#if 0 /* unused, remove? */
255
/* return the mantissa and the binary exponent */
256
static int int_pow(int i, int *exp_ptr)
257
{
258
    int e, er, eq, j;
259
    int a, a1;
260

261
    /* renormalize */
262
    a = i;
263
    e = POW_FRAC_BITS;
264
    while (a < (1 << (POW_FRAC_BITS - 1))) {
265
        a = a << 1;
266
        e--;
267
    }
268
    a -= (1 << POW_FRAC_BITS);
269
    a1 = 0;
270
    for(j = DEV_ORDER - 1; j >= 0; j--)
271
        a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
272
    a = (1 << POW_FRAC_BITS) + a1;
273
    /* exponent compute (exact) */
274
    e = e * 4;
275
    er = e % 3;
276
    eq = e / 3;
277
    a = POW_MULL(a, pow_mult3[er]);
278
    while (a >= 2 * POW_FRAC_ONE) {
279
        a = a >> 1;
280
        eq++;
281
    }
282
    /* convert to float */
283
    while (a < POW_FRAC_ONE) {
284
        a = a << 1;
285
        eq--;
286
    }
287
    /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
288
#if POW_FRAC_BITS > FRAC_BITS
289
    a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
290
    /* correct overflow */
291
    if (a >= 2 * (1 << FRAC_BITS)) {
292
        a = a >> 1;
293
        eq++;
294
    }
295
#endif
296
    *exp_ptr = eq;
297
    return a;
298
}
299
#endif
300

    
301
static av_cold int decode_init(AVCodecContext * avctx)
302
{
303
    MPADecodeContext *s = avctx->priv_data;
304
    static int init=0;
305
    int i, j, k;
306

    
307
    s->avctx = avctx;
308

    
309
    avctx->sample_fmt= OUT_FMT;
310
    s->error_recognition= avctx->error_recognition;
311

    
312
    if (!init && !avctx->parse_only) {
313
        int offset;
314

    
315
        /* scale factors table for layer 1/2 */
316
        for(i=0;i<64;i++) {
317
            int shift, mod;
318
            /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
319
            shift = (i / 3);
320
            mod = i % 3;
321
            scale_factor_modshift[i] = mod | (shift << 2);
322
        }
323

    
324
        /* scale factor multiply for layer 1 */
325
        for(i=0;i<15;i++) {
326
            int n, norm;
327
            n = i + 2;
328
            norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
329
            scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0          * 2.0), FRAC_BITS);
330
            scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
331
            scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
332
            dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
333
                    i, norm,
334
                    scale_factor_mult[i][0],
335
                    scale_factor_mult[i][1],
336
                    scale_factor_mult[i][2]);
337
        }
338

    
339
        RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
340

    
341
        /* huffman decode tables */
342
        offset = 0;
343
        for(i=1;i<16;i++) {
344
            const HuffTable *h = &mpa_huff_tables[i];
345
            int xsize, x, y;
346
            uint8_t  tmp_bits [512];
347
            uint16_t tmp_codes[512];
348

    
349
            memset(tmp_bits , 0, sizeof(tmp_bits ));
350
            memset(tmp_codes, 0, sizeof(tmp_codes));
351

    
352
            xsize = h->xsize;
353

    
354
            j = 0;
355
            for(x=0;x<xsize;x++) {
356
                for(y=0;y<xsize;y++){
357
                    tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j  ];
358
                    tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
359
                }
360
            }
361

    
362
            /* XXX: fail test */
363
            huff_vlc[i].table = huff_vlc_tables+offset;
364
            huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
365
            init_vlc(&huff_vlc[i], 7, 512,
366
                     tmp_bits, 1, 1, tmp_codes, 2, 2,
367
                     INIT_VLC_USE_NEW_STATIC);
368
            offset += huff_vlc_tables_sizes[i];
369
        }
370
        assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
371

    
372
        offset = 0;
373
        for(i=0;i<2;i++) {
374
            huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
375
            huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
376
            init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
377
                     mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
378
                     INIT_VLC_USE_NEW_STATIC);
379
            offset += huff_quad_vlc_tables_sizes[i];
380
        }
381
        assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
382

    
383
        for(i=0;i<9;i++) {
384
            k = 0;
385
            for(j=0;j<22;j++) {
386
                band_index_long[i][j] = k;
387
                k += band_size_long[i][j];
388
            }
389
            band_index_long[i][22] = k;
390
        }
391

    
392
        /* compute n ^ (4/3) and store it in mantissa/exp format */
393

    
394
        int_pow_init();
395
        mpegaudio_tableinit();
396

    
397
        for(i=0;i<7;i++) {
398
            float f;
399
            INTFLOAT v;
400
            if (i != 6) {
401
                f = tan((double)i * M_PI / 12.0);
402
                v = FIXR(f / (1.0 + f));
403
            } else {
404
                v = FIXR(1.0);
405
            }
406
            is_table[0][i] = v;
407
            is_table[1][6 - i] = v;
408
        }
409
        /* invalid values */
410
        for(i=7;i<16;i++)
411
            is_table[0][i] = is_table[1][i] = 0.0;
412

    
413
        for(i=0;i<16;i++) {
414
            double f;
415
            int e, k;
416

    
417
            for(j=0;j<2;j++) {
418
                e = -(j + 1) * ((i + 1) >> 1);
419
                f = pow(2.0, e / 4.0);
420
                k = i & 1;
421
                is_table_lsf[j][k ^ 1][i] = FIXR(f);
422
                is_table_lsf[j][k][i] = FIXR(1.0);
423
                dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
424
                        i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
425
            }
426
        }
427

    
428
        for(i=0;i<8;i++) {
429
            float ci, cs, ca;
430
            ci = ci_table[i];
431
            cs = 1.0 / sqrt(1.0 + ci * ci);
432
            ca = cs * ci;
433
            csa_table[i][0] = FIXHR(cs/4);
434
            csa_table[i][1] = FIXHR(ca/4);
435
            csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
436
            csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
437
            csa_table_float[i][0] = cs;
438
            csa_table_float[i][1] = ca;
439
            csa_table_float[i][2] = ca + cs;
440
            csa_table_float[i][3] = ca - cs;
441
        }
442

    
443
        /* compute mdct windows */
444
        for(i=0;i<36;i++) {
445
            for(j=0; j<4; j++){
446
                double d;
447

    
448
                if(j==2 && i%3 != 1)
449
                    continue;
450

    
451
                d= sin(M_PI * (i + 0.5) / 36.0);
452
                if(j==1){
453
                    if     (i>=30) d= 0;
454
                    else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
455
                    else if(i>=18) d= 1;
456
                }else if(j==3){
457
                    if     (i<  6) d= 0;
458
                    else if(i< 12) d= sin(M_PI * (i -  6 + 0.5) / 12.0);
459
                    else if(i< 18) d= 1;
460
                }
461
                //merge last stage of imdct into the window coefficients
462
                d*= 0.5 / cos(M_PI*(2*i + 19)/72);
463

    
464
                if(j==2)
465
                    mdct_win[j][i/3] = FIXHR((d / (1<<5)));
466
                else
467
                    mdct_win[j][i  ] = FIXHR((d / (1<<5)));
468
            }
469
        }
470

    
471
        /* NOTE: we do frequency inversion adter the MDCT by changing
472
           the sign of the right window coefs */
473
        for(j=0;j<4;j++) {
474
            for(i=0;i<36;i+=2) {
475
                mdct_win[j + 4][i] = mdct_win[j][i];
476
                mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
477
            }
478
        }
479

    
480
        init = 1;
481
    }
482

    
483
    if (avctx->codec_id == CODEC_ID_MP3ADU)
484
        s->adu_mode = 1;
485
    return 0;
486
}
487

    
488
/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
489

    
490
/* cos(i*pi/64) */
491

    
492
#define COS0_0  FIXHR(0.50060299823519630134/2)
493
#define COS0_1  FIXHR(0.50547095989754365998/2)
494
#define COS0_2  FIXHR(0.51544730992262454697/2)
495
#define COS0_3  FIXHR(0.53104259108978417447/2)
496
#define COS0_4  FIXHR(0.55310389603444452782/2)
497
#define COS0_5  FIXHR(0.58293496820613387367/2)
498
#define COS0_6  FIXHR(0.62250412303566481615/2)
499
#define COS0_7  FIXHR(0.67480834145500574602/2)
500
#define COS0_8  FIXHR(0.74453627100229844977/2)
501
#define COS0_9  FIXHR(0.83934964541552703873/2)
502
#define COS0_10 FIXHR(0.97256823786196069369/2)
503
#define COS0_11 FIXHR(1.16943993343288495515/4)
504
#define COS0_12 FIXHR(1.48416461631416627724/4)
505
#define COS0_13 FIXHR(2.05778100995341155085/8)
506
#define COS0_14 FIXHR(3.40760841846871878570/8)
507
#define COS0_15 FIXHR(10.19000812354805681150/32)
508

    
509
#define COS1_0 FIXHR(0.50241928618815570551/2)
510
#define COS1_1 FIXHR(0.52249861493968888062/2)
511
#define COS1_2 FIXHR(0.56694403481635770368/2)
512
#define COS1_3 FIXHR(0.64682178335999012954/2)
513
#define COS1_4 FIXHR(0.78815462345125022473/2)
514
#define COS1_5 FIXHR(1.06067768599034747134/4)
515
#define COS1_6 FIXHR(1.72244709823833392782/4)
516
#define COS1_7 FIXHR(5.10114861868916385802/16)
517

    
518
#define COS2_0 FIXHR(0.50979557910415916894/2)
519
#define COS2_1 FIXHR(0.60134488693504528054/2)
520
#define COS2_2 FIXHR(0.89997622313641570463/2)
521
#define COS2_3 FIXHR(2.56291544774150617881/8)
522

    
523
#define COS3_0 FIXHR(0.54119610014619698439/2)
524
#define COS3_1 FIXHR(1.30656296487637652785/4)
525

    
526
#define COS4_0 FIXHR(0.70710678118654752439/2)
527

    
528
/* butterfly operator */
529
#define BF(a, b, c, s)\
530
{\
531
    tmp0 = val##a + val##b;\
532
    tmp1 = val##a - val##b;\
533
    val##a = tmp0;\
534
    val##b = MULH3(tmp1, c, 1<<(s));\
535
}
536

    
537
#define BF0(a, b, c, s)\
538
{\
539
    tmp0 = tab[a] + tab[b];\
540
    tmp1 = tab[a] - tab[b];\
541
    val##a = tmp0;\
542
    val##b = MULH3(tmp1, c, 1<<(s));\
543
}
544

    
545
#define BF1(a, b, c, d)\
546
{\
547
    BF(a, b, COS4_0, 1);\
548
    BF(c, d,-COS4_0, 1);\
549
    val##c += val##d;\
550
}
551

    
552
#define BF2(a, b, c, d)\
553
{\
554
    BF(a, b, COS4_0, 1);\
555
    BF(c, d,-COS4_0, 1);\
556
    val##c += val##d;\
557
    val##a += val##c;\
558
    val##c += val##b;\
559
    val##b += val##d;\
560
}
561

    
562
#define ADD(a, b) val##a += val##b
563

    
564
/* DCT32 without 1/sqrt(2) coef zero scaling. */
565
static void dct32(INTFLOAT *out, const INTFLOAT *tab)
566
{
567
    INTFLOAT tmp0, tmp1;
568

    
569
    INTFLOAT val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 ,
570
             val8 , val9 , val10, val11, val12, val13, val14, val15,
571
             val16, val17, val18, val19, val20, val21, val22, val23,
572
             val24, val25, val26, val27, val28, val29, val30, val31;
573

    
574
    /* pass 1 */
575
    BF0( 0, 31, COS0_0 , 1);
576
    BF0(15, 16, COS0_15, 5);
577
    /* pass 2 */
578
    BF( 0, 15, COS1_0 , 1);
579
    BF(16, 31,-COS1_0 , 1);
580
    /* pass 1 */
581
    BF0( 7, 24, COS0_7 , 1);
582
    BF0( 8, 23, COS0_8 , 1);
583
    /* pass 2 */
584
    BF( 7,  8, COS1_7 , 4);
585
    BF(23, 24,-COS1_7 , 4);
586
    /* pass 3 */
587
    BF( 0,  7, COS2_0 , 1);
588
    BF( 8, 15,-COS2_0 , 1);
589
    BF(16, 23, COS2_0 , 1);
590
    BF(24, 31,-COS2_0 , 1);
591
    /* pass 1 */
592
    BF0( 3, 28, COS0_3 , 1);
593
    BF0(12, 19, COS0_12, 2);
594
    /* pass 2 */
595
    BF( 3, 12, COS1_3 , 1);
596
    BF(19, 28,-COS1_3 , 1);
597
    /* pass 1 */
598
    BF0( 4, 27, COS0_4 , 1);
599
    BF0(11, 20, COS0_11, 2);
600
    /* pass 2 */
601
    BF( 4, 11, COS1_4 , 1);
602
    BF(20, 27,-COS1_4 , 1);
603
    /* pass 3 */
604
    BF( 3,  4, COS2_3 , 3);
605
    BF(11, 12,-COS2_3 , 3);
606
    BF(19, 20, COS2_3 , 3);
607
    BF(27, 28,-COS2_3 , 3);
608
    /* pass 4 */
609
    BF( 0,  3, COS3_0 , 1);
610
    BF( 4,  7,-COS3_0 , 1);
611
    BF( 8, 11, COS3_0 , 1);
612
    BF(12, 15,-COS3_0 , 1);
613
    BF(16, 19, COS3_0 , 1);
614
    BF(20, 23,-COS3_0 , 1);
615
    BF(24, 27, COS3_0 , 1);
616
    BF(28, 31,-COS3_0 , 1);
617

    
618

    
619

    
620
    /* pass 1 */
621
    BF0( 1, 30, COS0_1 , 1);
622
    BF0(14, 17, COS0_14, 3);
623
    /* pass 2 */
624
    BF( 1, 14, COS1_1 , 1);
625
    BF(17, 30,-COS1_1 , 1);
626
    /* pass 1 */
627
    BF0( 6, 25, COS0_6 , 1);
628
    BF0( 9, 22, COS0_9 , 1);
629
    /* pass 2 */
630
    BF( 6,  9, COS1_6 , 2);
631
    BF(22, 25,-COS1_6 , 2);
632
    /* pass 3 */
633
    BF( 1,  6, COS2_1 , 1);
634
    BF( 9, 14,-COS2_1 , 1);
635
    BF(17, 22, COS2_1 , 1);
636
    BF(25, 30,-COS2_1 , 1);
637

    
638
    /* pass 1 */
639
    BF0( 2, 29, COS0_2 , 1);
640
    BF0(13, 18, COS0_13, 3);
641
    /* pass 2 */
642
    BF( 2, 13, COS1_2 , 1);
643
    BF(18, 29,-COS1_2 , 1);
644
    /* pass 1 */
645
    BF0( 5, 26, COS0_5 , 1);
646
    BF0(10, 21, COS0_10, 1);
647
    /* pass 2 */
648
    BF( 5, 10, COS1_5 , 2);
649
    BF(21, 26,-COS1_5 , 2);
650
    /* pass 3 */
651
    BF( 2,  5, COS2_2 , 1);
652
    BF(10, 13,-COS2_2 , 1);
653
    BF(18, 21, COS2_2 , 1);
654
    BF(26, 29,-COS2_2 , 1);
655
    /* pass 4 */
656
    BF( 1,  2, COS3_1 , 2);
657
    BF( 5,  6,-COS3_1 , 2);
658
    BF( 9, 10, COS3_1 , 2);
659
    BF(13, 14,-COS3_1 , 2);
660
    BF(17, 18, COS3_1 , 2);
661
    BF(21, 22,-COS3_1 , 2);
662
    BF(25, 26, COS3_1 , 2);
663
    BF(29, 30,-COS3_1 , 2);
664

    
665
    /* pass 5 */
666
    BF1( 0,  1,  2,  3);
667
    BF2( 4,  5,  6,  7);
668
    BF1( 8,  9, 10, 11);
669
    BF2(12, 13, 14, 15);
670
    BF1(16, 17, 18, 19);
671
    BF2(20, 21, 22, 23);
672
    BF1(24, 25, 26, 27);
673
    BF2(28, 29, 30, 31);
674

    
675
    /* pass 6 */
676

    
677
    ADD( 8, 12);
678
    ADD(12, 10);
679
    ADD(10, 14);
680
    ADD(14,  9);
681
    ADD( 9, 13);
682
    ADD(13, 11);
683
    ADD(11, 15);
684

    
685
    out[ 0] = val0;
686
    out[16] = val1;
687
    out[ 8] = val2;
688
    out[24] = val3;
689
    out[ 4] = val4;
690
    out[20] = val5;
691
    out[12] = val6;
692
    out[28] = val7;
693
    out[ 2] = val8;
694
    out[18] = val9;
695
    out[10] = val10;
696
    out[26] = val11;
697
    out[ 6] = val12;
698
    out[22] = val13;
699
    out[14] = val14;
700
    out[30] = val15;
701

    
702
    ADD(24, 28);
703
    ADD(28, 26);
704
    ADD(26, 30);
705
    ADD(30, 25);
706
    ADD(25, 29);
707
    ADD(29, 27);
708
    ADD(27, 31);
709

    
710
    out[ 1] = val16 + val24;
711
    out[17] = val17 + val25;
712
    out[ 9] = val18 + val26;
713
    out[25] = val19 + val27;
714
    out[ 5] = val20 + val28;
715
    out[21] = val21 + val29;
716
    out[13] = val22 + val30;
717
    out[29] = val23 + val31;
718
    out[ 3] = val24 + val20;
719
    out[19] = val25 + val21;
720
    out[11] = val26 + val22;
721
    out[27] = val27 + val23;
722
    out[ 7] = val28 + val18;
723
    out[23] = val29 + val19;
724
    out[15] = val30 + val17;
725
    out[31] = val31;
726
}
727

    
728
#if CONFIG_FLOAT
729
static inline float round_sample(float *sum)
730
{
731
    float sum1=*sum;
732
    *sum = 0;
733
    return sum1;
734
}
735

    
736
/* signed 16x16 -> 32 multiply add accumulate */
737
#define MACS(rt, ra, rb) rt+=(ra)*(rb)
738

    
739
/* signed 16x16 -> 32 multiply */
740
#define MULS(ra, rb) ((ra)*(rb))
741

    
742
#define MLSS(rt, ra, rb) rt-=(ra)*(rb)
743

    
744
#elif FRAC_BITS <= 15
745

    
746
static inline int round_sample(int *sum)
747
{
748
    int sum1;
749
    sum1 = (*sum) >> OUT_SHIFT;
750
    *sum &= (1<<OUT_SHIFT)-1;
751
    return av_clip(sum1, OUT_MIN, OUT_MAX);
752
}
753

    
754
/* signed 16x16 -> 32 multiply add accumulate */
755
#define MACS(rt, ra, rb) MAC16(rt, ra, rb)
756

    
757
/* signed 16x16 -> 32 multiply */
758
#define MULS(ra, rb) MUL16(ra, rb)
759

    
760
#define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
761

    
762
#else
763

    
764
static inline int round_sample(int64_t *sum)
765
{
766
    int sum1;
767
    sum1 = (int)((*sum) >> OUT_SHIFT);
768
    *sum &= (1<<OUT_SHIFT)-1;
769
    return av_clip(sum1, OUT_MIN, OUT_MAX);
770
}
771

    
772
#   define MULS(ra, rb) MUL64(ra, rb)
773
#   define MACS(rt, ra, rb) MAC64(rt, ra, rb)
774
#   define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
775
#endif
776

    
777
#define SUM8(op, sum, w, p)               \
778
{                                         \
779
    op(sum, (w)[0 * 64], (p)[0 * 64]);    \
780
    op(sum, (w)[1 * 64], (p)[1 * 64]);    \
781
    op(sum, (w)[2 * 64], (p)[2 * 64]);    \
782
    op(sum, (w)[3 * 64], (p)[3 * 64]);    \
783
    op(sum, (w)[4 * 64], (p)[4 * 64]);    \
784
    op(sum, (w)[5 * 64], (p)[5 * 64]);    \
785
    op(sum, (w)[6 * 64], (p)[6 * 64]);    \
786
    op(sum, (w)[7 * 64], (p)[7 * 64]);    \
787
}
788

    
789
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
790
{                                               \
791
    INTFLOAT tmp;\
792
    tmp = p[0 * 64];\
793
    op1(sum1, (w1)[0 * 64], tmp);\
794
    op2(sum2, (w2)[0 * 64], tmp);\
795
    tmp = p[1 * 64];\
796
    op1(sum1, (w1)[1 * 64], tmp);\
797
    op2(sum2, (w2)[1 * 64], tmp);\
798
    tmp = p[2 * 64];\
799
    op1(sum1, (w1)[2 * 64], tmp);\
800
    op2(sum2, (w2)[2 * 64], tmp);\
801
    tmp = p[3 * 64];\
802
    op1(sum1, (w1)[3 * 64], tmp);\
803
    op2(sum2, (w2)[3 * 64], tmp);\
804
    tmp = p[4 * 64];\
805
    op1(sum1, (w1)[4 * 64], tmp);\
806
    op2(sum2, (w2)[4 * 64], tmp);\
807
    tmp = p[5 * 64];\
808
    op1(sum1, (w1)[5 * 64], tmp);\
809
    op2(sum2, (w2)[5 * 64], tmp);\
810
    tmp = p[6 * 64];\
811
    op1(sum1, (w1)[6 * 64], tmp);\
812
    op2(sum2, (w2)[6 * 64], tmp);\
813
    tmp = p[7 * 64];\
814
    op1(sum1, (w1)[7 * 64], tmp);\
815
    op2(sum2, (w2)[7 * 64], tmp);\
816
}
817

    
818
void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
819
{
820
    int i;
821

    
822
    /* max = 18760, max sum over all 16 coefs : 44736 */
823
    for(i=0;i<257;i++) {
824
        INTFLOAT v;
825
        v = ff_mpa_enwindow[i];
826
#if CONFIG_FLOAT
827
        v *= 1.0 / (1LL<<(16 + FRAC_BITS));
828
#elif WFRAC_BITS < 16
829
        v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
830
#endif
831
        window[i] = v;
832
        if ((i & 63) != 0)
833
            v = -v;
834
        if (i != 0)
835
            window[512 - i] = v;
836
    }
837
}
838

    
839
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
840
   32 samples. */
841
/* XXX: optimize by avoiding ring buffer usage */
842
void RENAME(ff_mpa_synth_filter)(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
843
                         MPA_INT *window, int *dither_state,
844
                         OUT_INT *samples, int incr,
845
                         INTFLOAT sb_samples[SBLIMIT])
846
{
847
    register MPA_INT *synth_buf;
848
    register const MPA_INT *w, *w2, *p;
849
    int j, offset;
850
    OUT_INT *samples2;
851
#if CONFIG_FLOAT
852
    float sum, sum2;
853
#elif FRAC_BITS <= 15
854
    int32_t tmp[32];
855
    int sum, sum2;
856
#else
857
    int64_t sum, sum2;
858
#endif
859

    
860
    offset = *synth_buf_offset;
861
    synth_buf = synth_buf_ptr + offset;
862

    
863
#if FRAC_BITS <= 15 && !CONFIG_FLOAT
864
    dct32(tmp, sb_samples);
865
    for(j=0;j<32;j++) {
866
        /* NOTE: can cause a loss in precision if very high amplitude
867
           sound */
868
        synth_buf[j] = av_clip_int16(tmp[j]);
869
    }
870
#else
871
    dct32(synth_buf, sb_samples);
872
#endif
873

    
874
    /* copy to avoid wrap */
875
    memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
876

    
877
    samples2 = samples + 31 * incr;
878
    w = window;
879
    w2 = window + 31;
880

    
881
    sum = *dither_state;
882
    p = synth_buf + 16;
883
    SUM8(MACS, sum, w, p);
884
    p = synth_buf + 48;
885
    SUM8(MLSS, sum, w + 32, p);
886
    *samples = round_sample(&sum);
887
    samples += incr;
888
    w++;
889

    
890
    /* we calculate two samples at the same time to avoid one memory
891
       access per two sample */
892
    for(j=1;j<16;j++) {
893
        sum2 = 0;
894
        p = synth_buf + 16 + j;
895
        SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
896
        p = synth_buf + 48 - j;
897
        SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
898

    
899
        *samples = round_sample(&sum);
900
        samples += incr;
901
        sum += sum2;
902
        *samples2 = round_sample(&sum);
903
        samples2 -= incr;
904
        w++;
905
        w2--;
906
    }
907

    
908
    p = synth_buf + 32;
909
    SUM8(MLSS, sum, w + 32, p);
910
    *samples = round_sample(&sum);
911
    *dither_state= sum;
912

    
913
    offset = (offset - 32) & 511;
914
    *synth_buf_offset = offset;
915
}
916

    
917
#define C3 FIXHR(0.86602540378443864676/2)
918

    
919
/* 0.5 / cos(pi*(2*i+1)/36) */
920
static const INTFLOAT icos36[9] = {
921
    FIXR(0.50190991877167369479),
922
    FIXR(0.51763809020504152469), //0
923
    FIXR(0.55168895948124587824),
924
    FIXR(0.61038729438072803416),
925
    FIXR(0.70710678118654752439), //1
926
    FIXR(0.87172339781054900991),
927
    FIXR(1.18310079157624925896),
928
    FIXR(1.93185165257813657349), //2
929
    FIXR(5.73685662283492756461),
930
};
931

    
932
/* 0.5 / cos(pi*(2*i+1)/36) */
933
static const INTFLOAT icos36h[9] = {
934
    FIXHR(0.50190991877167369479/2),
935
    FIXHR(0.51763809020504152469/2), //0
936
    FIXHR(0.55168895948124587824/2),
937
    FIXHR(0.61038729438072803416/2),
938
    FIXHR(0.70710678118654752439/2), //1
939
    FIXHR(0.87172339781054900991/2),
940
    FIXHR(1.18310079157624925896/4),
941
    FIXHR(1.93185165257813657349/4), //2
942
//    FIXHR(5.73685662283492756461),
943
};
944

    
945
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
946
   cases. */
947
static void imdct12(INTFLOAT *out, INTFLOAT *in)
948
{
949
    INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
950

    
951
    in0= in[0*3];
952
    in1= in[1*3] + in[0*3];
953
    in2= in[2*3] + in[1*3];
954
    in3= in[3*3] + in[2*3];
955
    in4= in[4*3] + in[3*3];
956
    in5= in[5*3] + in[4*3];
957
    in5 += in3;
958
    in3 += in1;
959

    
960
    in2= MULH3(in2, C3, 2);
961
    in3= MULH3(in3, C3, 4);
962

    
963
    t1 = in0 - in4;
964
    t2 = MULH3(in1 - in5, icos36h[4], 2);
965

    
966
    out[ 7]=
967
    out[10]= t1 + t2;
968
    out[ 1]=
969
    out[ 4]= t1 - t2;
970

    
971
    in0 += SHR(in4, 1);
972
    in4 = in0 + in2;
973
    in5 += 2*in1;
974
    in1 = MULH3(in5 + in3, icos36h[1], 1);
975
    out[ 8]=
976
    out[ 9]= in4 + in1;
977
    out[ 2]=
978
    out[ 3]= in4 - in1;
979

    
980
    in0 -= in2;
981
    in5 = MULH3(in5 - in3, icos36h[7], 2);
982
    out[ 0]=
983
    out[ 5]= in0 - in5;
984
    out[ 6]=
985
    out[11]= in0 + in5;
986
}
987

    
988
/* cos(pi*i/18) */
989
#define C1 FIXHR(0.98480775301220805936/2)
990
#define C2 FIXHR(0.93969262078590838405/2)
991
#define C3 FIXHR(0.86602540378443864676/2)
992
#define C4 FIXHR(0.76604444311897803520/2)
993
#define C5 FIXHR(0.64278760968653932632/2)
994
#define C6 FIXHR(0.5/2)
995
#define C7 FIXHR(0.34202014332566873304/2)
996
#define C8 FIXHR(0.17364817766693034885/2)
997

    
998

    
999
/* using Lee like decomposition followed by hand coded 9 points DCT */
1000
static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
1001
{
1002
    int i, j;
1003
    INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
1004
    INTFLOAT tmp[18], *tmp1, *in1;
1005

    
1006
    for(i=17;i>=1;i--)
1007
        in[i] += in[i-1];
1008
    for(i=17;i>=3;i-=2)
1009
        in[i] += in[i-2];
1010

    
1011
    for(j=0;j<2;j++) {
1012
        tmp1 = tmp + j;
1013
        in1 = in + j;
1014

    
1015
        t2 = in1[2*4] + in1[2*8] - in1[2*2];
1016

    
1017
        t3 = in1[2*0] + SHR(in1[2*6],1);
1018
        t1 = in1[2*0] - in1[2*6];
1019
        tmp1[ 6] = t1 - SHR(t2,1);
1020
        tmp1[16] = t1 + t2;
1021

    
1022
        t0 = MULH3(in1[2*2] + in1[2*4] ,    C2, 2);
1023
        t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
1024
        t2 = MULH3(in1[2*2] + in1[2*8] ,   -C4, 2);
1025

    
1026
        tmp1[10] = t3 - t0 - t2;
1027
        tmp1[ 2] = t3 + t0 + t1;
1028
        tmp1[14] = t3 + t2 - t1;
1029

    
1030
        tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
1031
        t2 = MULH3(in1[2*1] + in1[2*5],    C1, 2);
1032
        t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
1033
        t0 = MULH3(in1[2*3], C3, 2);
1034

    
1035
        t1 = MULH3(in1[2*1] + in1[2*7],   -C5, 2);
1036

    
1037
        tmp1[ 0] = t2 + t3 + t0;
1038
        tmp1[12] = t2 + t1 - t0;
1039
        tmp1[ 8] = t3 - t1 - t0;
1040
    }
1041

    
1042
    i = 0;
1043
    for(j=0;j<4;j++) {
1044
        t0 = tmp[i];
1045
        t1 = tmp[i + 2];
1046
        s0 = t1 + t0;
1047
        s2 = t1 - t0;
1048

    
1049
        t2 = tmp[i + 1];
1050
        t3 = tmp[i + 3];
1051
        s1 = MULH3(t3 + t2, icos36h[j], 2);
1052
        s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
1053

    
1054
        t0 = s0 + s1;
1055
        t1 = s0 - s1;
1056
        out[(9 + j)*SBLIMIT] =  MULH3(t1, win[9 + j], 1) + buf[9 + j];
1057
        out[(8 - j)*SBLIMIT] =  MULH3(t1, win[8 - j], 1) + buf[8 - j];
1058
        buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
1059
        buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
1060

    
1061
        t0 = s2 + s3;
1062
        t1 = s2 - s3;
1063
        out[(9 + 8 - j)*SBLIMIT] =  MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
1064
        out[(        j)*SBLIMIT] =  MULH3(t1, win[        j], 1) + buf[        j];
1065
        buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
1066
        buf[      + j] = MULH3(t0, win[18         + j], 1);
1067
        i += 4;
1068
    }
1069

    
1070
    s0 = tmp[16];
1071
    s1 = MULH3(tmp[17], icos36h[4], 2);
1072
    t0 = s0 + s1;
1073
    t1 = s0 - s1;
1074
    out[(9 + 4)*SBLIMIT] =  MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
1075
    out[(8 - 4)*SBLIMIT] =  MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
1076
    buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
1077
    buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
1078
}
1079

    
1080
/* return the number of decoded frames */
1081
static int mp_decode_layer1(MPADecodeContext *s)
1082
{
1083
    int bound, i, v, n, ch, j, mant;
1084
    uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1085
    uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1086

    
1087
    if (s->mode == MPA_JSTEREO)
1088
        bound = (s->mode_ext + 1) * 4;
1089
    else
1090
        bound = SBLIMIT;
1091

    
1092
    /* allocation bits */
1093
    for(i=0;i<bound;i++) {
1094
        for(ch=0;ch<s->nb_channels;ch++) {
1095
            allocation[ch][i] = get_bits(&s->gb, 4);
1096
        }
1097
    }
1098
    for(i=bound;i<SBLIMIT;i++) {
1099
        allocation[0][i] = get_bits(&s->gb, 4);
1100
    }
1101

    
1102
    /* scale factors */
1103
    for(i=0;i<bound;i++) {
1104
        for(ch=0;ch<s->nb_channels;ch++) {
1105
            if (allocation[ch][i])
1106
                scale_factors[ch][i] = get_bits(&s->gb, 6);
1107
        }
1108
    }
1109
    for(i=bound;i<SBLIMIT;i++) {
1110
        if (allocation[0][i]) {
1111
            scale_factors[0][i] = get_bits(&s->gb, 6);
1112
            scale_factors[1][i] = get_bits(&s->gb, 6);
1113
        }
1114
    }
1115

    
1116
    /* compute samples */
1117
    for(j=0;j<12;j++) {
1118
        for(i=0;i<bound;i++) {
1119
            for(ch=0;ch<s->nb_channels;ch++) {
1120
                n = allocation[ch][i];
1121
                if (n) {
1122
                    mant = get_bits(&s->gb, n + 1);
1123
                    v = l1_unscale(n, mant, scale_factors[ch][i]);
1124
                } else {
1125
                    v = 0;
1126
                }
1127
                s->sb_samples[ch][j][i] = v;
1128
            }
1129
        }
1130
        for(i=bound;i<SBLIMIT;i++) {
1131
            n = allocation[0][i];
1132
            if (n) {
1133
                mant = get_bits(&s->gb, n + 1);
1134
                v = l1_unscale(n, mant, scale_factors[0][i]);
1135
                s->sb_samples[0][j][i] = v;
1136
                v = l1_unscale(n, mant, scale_factors[1][i]);
1137
                s->sb_samples[1][j][i] = v;
1138
            } else {
1139
                s->sb_samples[0][j][i] = 0;
1140
                s->sb_samples[1][j][i] = 0;
1141
            }
1142
        }
1143
    }
1144
    return 12;
1145
}
1146

    
1147
static int mp_decode_layer2(MPADecodeContext *s)
1148
{
1149
    int sblimit; /* number of used subbands */
1150
    const unsigned char *alloc_table;
1151
    int table, bit_alloc_bits, i, j, ch, bound, v;
1152
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1153
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1154
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1155
    int scale, qindex, bits, steps, k, l, m, b;
1156

    
1157
    /* select decoding table */
1158
    table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
1159
                            s->sample_rate, s->lsf);
1160
    sblimit = ff_mpa_sblimit_table[table];
1161
    alloc_table = ff_mpa_alloc_tables[table];
1162

    
1163
    if (s->mode == MPA_JSTEREO)
1164
        bound = (s->mode_ext + 1) * 4;
1165
    else
1166
        bound = sblimit;
1167

    
1168
    dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
1169

    
1170
    /* sanity check */
1171
    if( bound > sblimit ) bound = sblimit;
1172

    
1173
    /* parse bit allocation */
1174
    j = 0;
1175
    for(i=0;i<bound;i++) {
1176
        bit_alloc_bits = alloc_table[j];
1177
        for(ch=0;ch<s->nb_channels;ch++) {
1178
            bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1179
        }
1180
        j += 1 << bit_alloc_bits;
1181
    }
1182
    for(i=bound;i<sblimit;i++) {
1183
        bit_alloc_bits = alloc_table[j];
1184
        v = get_bits(&s->gb, bit_alloc_bits);
1185
        bit_alloc[0][i] = v;
1186
        bit_alloc[1][i] = v;
1187
        j += 1 << bit_alloc_bits;
1188
    }
1189

    
1190
    /* scale codes */
1191
    for(i=0;i<sblimit;i++) {
1192
        for(ch=0;ch<s->nb_channels;ch++) {
1193
            if (bit_alloc[ch][i])
1194
                scale_code[ch][i] = get_bits(&s->gb, 2);
1195
        }
1196
    }
1197

    
1198
    /* scale factors */
1199
    for(i=0;i<sblimit;i++) {
1200
        for(ch=0;ch<s->nb_channels;ch++) {
1201
            if (bit_alloc[ch][i]) {
1202
                sf = scale_factors[ch][i];
1203
                switch(scale_code[ch][i]) {
1204
                default:
1205
                case 0:
1206
                    sf[0] = get_bits(&s->gb, 6);
1207
                    sf[1] = get_bits(&s->gb, 6);
1208
                    sf[2] = get_bits(&s->gb, 6);
1209
                    break;
1210
                case 2:
1211
                    sf[0] = get_bits(&s->gb, 6);
1212
                    sf[1] = sf[0];
1213
                    sf[2] = sf[0];
1214
                    break;
1215
                case 1:
1216
                    sf[0] = get_bits(&s->gb, 6);
1217
                    sf[2] = get_bits(&s->gb, 6);
1218
                    sf[1] = sf[0];
1219
                    break;
1220
                case 3:
1221
                    sf[0] = get_bits(&s->gb, 6);
1222
                    sf[2] = get_bits(&s->gb, 6);
1223
                    sf[1] = sf[2];
1224
                    break;
1225
                }
1226
            }
1227
        }
1228
    }
1229

    
1230
    /* samples */
1231
    for(k=0;k<3;k++) {
1232
        for(l=0;l<12;l+=3) {
1233
            j = 0;
1234
            for(i=0;i<bound;i++) {
1235
                bit_alloc_bits = alloc_table[j];
1236
                for(ch=0;ch<s->nb_channels;ch++) {
1237
                    b = bit_alloc[ch][i];
1238
                    if (b) {
1239
                        scale = scale_factors[ch][i][k];
1240
                        qindex = alloc_table[j+b];
1241
                        bits = ff_mpa_quant_bits[qindex];
1242
                        if (bits < 0) {
1243
                            /* 3 values at the same time */
1244
                            v = get_bits(&s->gb, -bits);
1245
                            steps = ff_mpa_quant_steps[qindex];
1246
                            s->sb_samples[ch][k * 12 + l + 0][i] =
1247
                                l2_unscale_group(steps, v % steps, scale);
1248
                            v = v / steps;
1249
                            s->sb_samples[ch][k * 12 + l + 1][i] =
1250
                                l2_unscale_group(steps, v % steps, scale);
1251
                            v = v / steps;
1252
                            s->sb_samples[ch][k * 12 + l + 2][i] =
1253
                                l2_unscale_group(steps, v, scale);
1254
                        } else {
1255
                            for(m=0;m<3;m++) {
1256
                                v = get_bits(&s->gb, bits);
1257
                                v = l1_unscale(bits - 1, v, scale);
1258
                                s->sb_samples[ch][k * 12 + l + m][i] = v;
1259
                            }
1260
                        }
1261
                    } else {
1262
                        s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1263
                        s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1264
                        s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1265
                    }
1266
                }
1267
                /* next subband in alloc table */
1268
                j += 1 << bit_alloc_bits;
1269
            }
1270
            /* XXX: find a way to avoid this duplication of code */
1271
            for(i=bound;i<sblimit;i++) {
1272
                bit_alloc_bits = alloc_table[j];
1273
                b = bit_alloc[0][i];
1274
                if (b) {
1275
                    int mant, scale0, scale1;
1276
                    scale0 = scale_factors[0][i][k];
1277
                    scale1 = scale_factors[1][i][k];
1278
                    qindex = alloc_table[j+b];
1279
                    bits = ff_mpa_quant_bits[qindex];
1280
                    if (bits < 0) {
1281
                        /* 3 values at the same time */
1282
                        v = get_bits(&s->gb, -bits);
1283
                        steps = ff_mpa_quant_steps[qindex];
1284
                        mant = v % steps;
1285
                        v = v / steps;
1286
                        s->sb_samples[0][k * 12 + l + 0][i] =
1287
                            l2_unscale_group(steps, mant, scale0);
1288
                        s->sb_samples[1][k * 12 + l + 0][i] =
1289
                            l2_unscale_group(steps, mant, scale1);
1290
                        mant = v % steps;
1291
                        v = v / steps;
1292
                        s->sb_samples[0][k * 12 + l + 1][i] =
1293
                            l2_unscale_group(steps, mant, scale0);
1294
                        s->sb_samples[1][k * 12 + l + 1][i] =
1295
                            l2_unscale_group(steps, mant, scale1);
1296
                        s->sb_samples[0][k * 12 + l + 2][i] =
1297
                            l2_unscale_group(steps, v, scale0);
1298
                        s->sb_samples[1][k * 12 + l + 2][i] =
1299
                            l2_unscale_group(steps, v, scale1);
1300
                    } else {
1301
                        for(m=0;m<3;m++) {
1302
                            mant = get_bits(&s->gb, bits);
1303
                            s->sb_samples[0][k * 12 + l + m][i] =
1304
                                l1_unscale(bits - 1, mant, scale0);
1305
                            s->sb_samples[1][k * 12 + l + m][i] =
1306
                                l1_unscale(bits - 1, mant, scale1);
1307
                        }
1308
                    }
1309
                } else {
1310
                    s->sb_samples[0][k * 12 + l + 0][i] = 0;
1311
                    s->sb_samples[0][k * 12 + l + 1][i] = 0;
1312
                    s->sb_samples[0][k * 12 + l + 2][i] = 0;
1313
                    s->sb_samples[1][k * 12 + l + 0][i] = 0;
1314
                    s->sb_samples[1][k * 12 + l + 1][i] = 0;
1315
                    s->sb_samples[1][k * 12 + l + 2][i] = 0;
1316
                }
1317
                /* next subband in alloc table */
1318
                j += 1 << bit_alloc_bits;
1319
            }
1320
            /* fill remaining samples to zero */
1321
            for(i=sblimit;i<SBLIMIT;i++) {
1322
                for(ch=0;ch<s->nb_channels;ch++) {
1323
                    s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1324
                    s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1325
                    s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1326
                }
1327
            }
1328
        }
1329
    }
1330
    return 3 * 12;
1331
}
1332

    
1333
#define SPLIT(dst,sf,n)\
1334
    if(n==3){\
1335
        int m= (sf*171)>>9;\
1336
        dst= sf - 3*m;\
1337
        sf=m;\
1338
    }else if(n==4){\
1339
        dst= sf&3;\
1340
        sf>>=2;\
1341
    }else if(n==5){\
1342
        int m= (sf*205)>>10;\
1343
        dst= sf - 5*m;\
1344
        sf=m;\
1345
    }else if(n==6){\
1346
        int m= (sf*171)>>10;\
1347
        dst= sf - 6*m;\
1348
        sf=m;\
1349
    }else{\
1350
        dst=0;\
1351
    }
1352

    
1353
static av_always_inline void lsf_sf_expand(int *slen,
1354
                                 int sf, int n1, int n2, int n3)
1355
{
1356
    SPLIT(slen[3], sf, n3)
1357
    SPLIT(slen[2], sf, n2)
1358
    SPLIT(slen[1], sf, n1)
1359
    slen[0] = sf;
1360
}
1361

    
1362
static void exponents_from_scale_factors(MPADecodeContext *s,
1363
                                         GranuleDef *g,
1364
                                         int16_t *exponents)
1365
{
1366
    const uint8_t *bstab, *pretab;
1367
    int len, i, j, k, l, v0, shift, gain, gains[3];
1368
    int16_t *exp_ptr;
1369

    
1370
    exp_ptr = exponents;
1371
    gain = g->global_gain - 210;
1372
    shift = g->scalefac_scale + 1;
1373

    
1374
    bstab = band_size_long[s->sample_rate_index];
1375
    pretab = mpa_pretab[g->preflag];
1376
    for(i=0;i<g->long_end;i++) {
1377
        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1378
        len = bstab[i];
1379
        for(j=len;j>0;j--)
1380
            *exp_ptr++ = v0;
1381
    }
1382

    
1383
    if (g->short_start < 13) {
1384
        bstab = band_size_short[s->sample_rate_index];
1385
        gains[0] = gain - (g->subblock_gain[0] << 3);
1386
        gains[1] = gain - (g->subblock_gain[1] << 3);
1387
        gains[2] = gain - (g->subblock_gain[2] << 3);
1388
        k = g->long_end;
1389
        for(i=g->short_start;i<13;i++) {
1390
            len = bstab[i];
1391
            for(l=0;l<3;l++) {
1392
                v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1393
                for(j=len;j>0;j--)
1394
                *exp_ptr++ = v0;
1395
            }
1396
        }
1397
    }
1398
}
1399

    
1400
/* handle n = 0 too */
1401
static inline int get_bitsz(GetBitContext *s, int n)
1402
{
1403
    if (n == 0)
1404
        return 0;
1405
    else
1406
        return get_bits(s, n);
1407
}
1408

    
1409

    
1410
static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1411
    if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1412
        s->gb= s->in_gb;
1413
        s->in_gb.buffer=NULL;
1414
        assert((get_bits_count(&s->gb) & 7) == 0);
1415
        skip_bits_long(&s->gb, *pos - *end_pos);
1416
        *end_pos2=
1417
        *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1418
        *pos= get_bits_count(&s->gb);
1419
    }
1420
}
1421

    
1422
/* Following is a optimized code for
1423
            INTFLOAT v = *src
1424
            if(get_bits1(&s->gb))
1425
                v = -v;
1426
            *dst = v;
1427
*/
1428
#if CONFIG_FLOAT
1429
#define READ_FLIP_SIGN(dst,src)\
1430
            v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1431
            AV_WN32A(dst, v);
1432
#else
1433
#define READ_FLIP_SIGN(dst,src)\
1434
            v= -get_bits1(&s->gb);\
1435
            *(dst) = (*(src) ^ v) - v;
1436
#endif
1437

    
1438
static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1439
                          int16_t *exponents, int end_pos2)
1440
{
1441
    int s_index;
1442
    int i;
1443
    int last_pos, bits_left;
1444
    VLC *vlc;
1445
    int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1446

    
1447
    /* low frequencies (called big values) */
1448
    s_index = 0;
1449
    for(i=0;i<3;i++) {
1450
        int j, k, l, linbits;
1451
        j = g->region_size[i];
1452
        if (j == 0)
1453
            continue;
1454
        /* select vlc table */
1455
        k = g->table_select[i];
1456
        l = mpa_huff_data[k][0];
1457
        linbits = mpa_huff_data[k][1];
1458
        vlc = &huff_vlc[l];
1459

    
1460
        if(!l){
1461
            memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1462
            s_index += 2*j;
1463
            continue;
1464
        }
1465

    
1466
        /* read huffcode and compute each couple */
1467
        for(;j>0;j--) {
1468
            int exponent, x, y;
1469
            int v;
1470
            int pos= get_bits_count(&s->gb);
1471

    
1472
            if (pos >= end_pos){
1473
//                av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1474
                switch_buffer(s, &pos, &end_pos, &end_pos2);
1475
//                av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1476
                if(pos >= end_pos)
1477
                    break;
1478
            }
1479
            y = get_vlc2(&s->gb, vlc->table, 7, 3);
1480

    
1481
            if(!y){
1482
                g->sb_hybrid[s_index  ] =
1483
                g->sb_hybrid[s_index+1] = 0;
1484
                s_index += 2;
1485
                continue;
1486
            }
1487

    
1488
            exponent= exponents[s_index];
1489

    
1490
            dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1491
                    i, g->region_size[i] - j, x, y, exponent);
1492
            if(y&16){
1493
                x = y >> 5;
1494
                y = y & 0x0f;
1495
                if (x < 15){
1496
                    READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
1497
                }else{
1498
                    x += get_bitsz(&s->gb, linbits);
1499
                    v = l3_unscale(x, exponent);
1500
                    if (get_bits1(&s->gb))
1501
                        v = -v;
1502
                    g->sb_hybrid[s_index] = v;
1503
                }
1504
                if (y < 15){
1505
                    READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
1506
                }else{
1507
                    y += get_bitsz(&s->gb, linbits);
1508
                    v = l3_unscale(y, exponent);
1509
                    if (get_bits1(&s->gb))
1510
                        v = -v;
1511
                    g->sb_hybrid[s_index+1] = v;
1512
                }
1513
            }else{
1514
                x = y >> 5;
1515
                y = y & 0x0f;
1516
                x += y;
1517
                if (x < 15){
1518
                    READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
1519
                }else{
1520
                    x += get_bitsz(&s->gb, linbits);
1521
                    v = l3_unscale(x, exponent);
1522
                    if (get_bits1(&s->gb))
1523
                        v = -v;
1524
                    g->sb_hybrid[s_index+!!y] = v;
1525
                }
1526
                g->sb_hybrid[s_index+ !y] = 0;
1527
            }
1528
            s_index+=2;
1529
        }
1530
    }
1531

    
1532
    /* high frequencies */
1533
    vlc = &huff_quad_vlc[g->count1table_select];
1534
    last_pos=0;
1535
    while (s_index <= 572) {
1536
        int pos, code;
1537
        pos = get_bits_count(&s->gb);
1538
        if (pos >= end_pos) {
1539
            if (pos > end_pos2 && last_pos){
1540
                /* some encoders generate an incorrect size for this
1541
                   part. We must go back into the data */
1542
                s_index -= 4;
1543
                skip_bits_long(&s->gb, last_pos - pos);
1544
                av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1545
                if(s->error_recognition >= FF_ER_COMPLIANT)
1546
                    s_index=0;
1547
                break;
1548
            }
1549
//                av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1550
            switch_buffer(s, &pos, &end_pos, &end_pos2);
1551
//                av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1552
            if(pos >= end_pos)
1553
                break;
1554
        }
1555
        last_pos= pos;
1556

    
1557
        code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1558
        dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1559
        g->sb_hybrid[s_index+0]=
1560
        g->sb_hybrid[s_index+1]=
1561
        g->sb_hybrid[s_index+2]=
1562
        g->sb_hybrid[s_index+3]= 0;
1563
        while(code){
1564
            static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1565
            int v;
1566
            int pos= s_index+idxtab[code];
1567
            code ^= 8>>idxtab[code];
1568
            READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
1569
        }
1570
        s_index+=4;
1571
    }
1572
    /* skip extension bits */
1573
    bits_left = end_pos2 - get_bits_count(&s->gb);
1574
//av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1575
    if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1576
        av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1577
        s_index=0;
1578
    }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1579
        av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1580
        s_index=0;
1581
    }
1582
    memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1583
    skip_bits_long(&s->gb, bits_left);
1584

    
1585
    i= get_bits_count(&s->gb);
1586
    switch_buffer(s, &i, &end_pos, &end_pos2);
1587

    
1588
    return 0;
1589
}
1590

    
1591
/* Reorder short blocks from bitstream order to interleaved order. It
1592
   would be faster to do it in parsing, but the code would be far more
1593
   complicated */
1594
static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1595
{
1596
    int i, j, len;
1597
    INTFLOAT *ptr, *dst, *ptr1;
1598
    INTFLOAT tmp[576];
1599

    
1600
    if (g->block_type != 2)
1601
        return;
1602

    
1603
    if (g->switch_point) {
1604
        if (s->sample_rate_index != 8) {
1605
            ptr = g->sb_hybrid + 36;
1606
        } else {
1607
            ptr = g->sb_hybrid + 48;
1608
        }
1609
    } else {
1610
        ptr = g->sb_hybrid;
1611
    }
1612

    
1613
    for(i=g->short_start;i<13;i++) {
1614
        len = band_size_short[s->sample_rate_index][i];
1615
        ptr1 = ptr;
1616
        dst = tmp;
1617
        for(j=len;j>0;j--) {
1618
            *dst++ = ptr[0*len];
1619
            *dst++ = ptr[1*len];
1620
            *dst++ = ptr[2*len];
1621
            ptr++;
1622
        }
1623
        ptr+=2*len;
1624
        memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1625
    }
1626
}
1627

    
1628
#define ISQRT2 FIXR(0.70710678118654752440)
1629

    
1630
static void compute_stereo(MPADecodeContext *s,
1631
                           GranuleDef *g0, GranuleDef *g1)
1632
{
1633
    int i, j, k, l;
1634
    int sf_max, sf, len, non_zero_found;
1635
    INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1636
    int non_zero_found_short[3];
1637

    
1638
    /* intensity stereo */
1639
    if (s->mode_ext & MODE_EXT_I_STEREO) {
1640
        if (!s->lsf) {
1641
            is_tab = is_table;
1642
            sf_max = 7;
1643
        } else {
1644
            is_tab = is_table_lsf[g1->scalefac_compress & 1];
1645
            sf_max = 16;
1646
        }
1647

    
1648
        tab0 = g0->sb_hybrid + 576;
1649
        tab1 = g1->sb_hybrid + 576;
1650

    
1651
        non_zero_found_short[0] = 0;
1652
        non_zero_found_short[1] = 0;
1653
        non_zero_found_short[2] = 0;
1654
        k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1655
        for(i = 12;i >= g1->short_start;i--) {
1656
            /* for last band, use previous scale factor */
1657
            if (i != 11)
1658
                k -= 3;
1659
            len = band_size_short[s->sample_rate_index][i];
1660
            for(l=2;l>=0;l--) {
1661
                tab0 -= len;
1662
                tab1 -= len;
1663
                if (!non_zero_found_short[l]) {
1664
                    /* test if non zero band. if so, stop doing i-stereo */
1665
                    for(j=0;j<len;j++) {
1666
                        if (tab1[j] != 0) {
1667
                            non_zero_found_short[l] = 1;
1668
                            goto found1;
1669
                        }
1670
                    }
1671
                    sf = g1->scale_factors[k + l];
1672
                    if (sf >= sf_max)
1673
                        goto found1;
1674

    
1675
                    v1 = is_tab[0][sf];
1676
                    v2 = is_tab[1][sf];
1677
                    for(j=0;j<len;j++) {
1678
                        tmp0 = tab0[j];
1679
                        tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1680
                        tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1681
                    }
1682
                } else {
1683
                found1:
1684
                    if (s->mode_ext & MODE_EXT_MS_STEREO) {
1685
                        /* lower part of the spectrum : do ms stereo
1686
                           if enabled */
1687
                        for(j=0;j<len;j++) {
1688
                            tmp0 = tab0[j];
1689
                            tmp1 = tab1[j];
1690
                            tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1691
                            tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1692
                        }
1693
                    }
1694
                }
1695
            }
1696
        }
1697

    
1698
        non_zero_found = non_zero_found_short[0] |
1699
            non_zero_found_short[1] |
1700
            non_zero_found_short[2];
1701

    
1702
        for(i = g1->long_end - 1;i >= 0;i--) {
1703
            len = band_size_long[s->sample_rate_index][i];
1704
            tab0 -= len;
1705
            tab1 -= len;
1706
            /* test if non zero band. if so, stop doing i-stereo */
1707
            if (!non_zero_found) {
1708
                for(j=0;j<len;j++) {
1709
                    if (tab1[j] != 0) {
1710
                        non_zero_found = 1;
1711
                        goto found2;
1712
                    }
1713
                }
1714
                /* for last band, use previous scale factor */
1715
                k = (i == 21) ? 20 : i;
1716
                sf = g1->scale_factors[k];
1717
                if (sf >= sf_max)
1718
                    goto found2;
1719
                v1 = is_tab[0][sf];
1720
                v2 = is_tab[1][sf];
1721
                for(j=0;j<len;j++) {
1722
                    tmp0 = tab0[j];
1723
                    tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1724
                    tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1725
                }
1726
            } else {
1727
            found2:
1728
                if (s->mode_ext & MODE_EXT_MS_STEREO) {
1729
                    /* lower part of the spectrum : do ms stereo
1730
                       if enabled */
1731
                    for(j=0;j<len;j++) {
1732
                        tmp0 = tab0[j];
1733
                        tmp1 = tab1[j];
1734
                        tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1735
                        tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1736
                    }
1737
                }
1738
            }
1739
        }
1740
    } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1741
        /* ms stereo ONLY */
1742
        /* NOTE: the 1/sqrt(2) normalization factor is included in the
1743
           global gain */
1744
        tab0 = g0->sb_hybrid;
1745
        tab1 = g1->sb_hybrid;
1746
        for(i=0;i<576;i++) {
1747
            tmp0 = tab0[i];
1748
            tmp1 = tab1[i];
1749
            tab0[i] = tmp0 + tmp1;
1750
            tab1[i] = tmp0 - tmp1;
1751
        }
1752
    }
1753
}
1754

    
1755
static void compute_antialias_integer(MPADecodeContext *s,
1756
                              GranuleDef *g)
1757
{
1758
    int32_t *ptr, *csa;
1759
    int n, i;
1760

    
1761
    /* we antialias only "long" bands */
1762
    if (g->block_type == 2) {
1763
        if (!g->switch_point)
1764
            return;
1765
        /* XXX: check this for 8000Hz case */
1766
        n = 1;
1767
    } else {
1768
        n = SBLIMIT - 1;
1769
    }
1770

    
1771
    ptr = g->sb_hybrid + 18;
1772
    for(i = n;i > 0;i--) {
1773
        int tmp0, tmp1, tmp2;
1774
        csa = &csa_table[0][0];
1775
#define INT_AA(j) \
1776
            tmp0 = ptr[-1-j];\
1777
            tmp1 = ptr[   j];\
1778
            tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1779
            ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1780
            ptr[   j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1781

    
1782
        INT_AA(0)
1783
        INT_AA(1)
1784
        INT_AA(2)
1785
        INT_AA(3)
1786
        INT_AA(4)
1787
        INT_AA(5)
1788
        INT_AA(6)
1789
        INT_AA(7)
1790

    
1791
        ptr += 18;
1792
    }
1793
}
1794

    
1795
static void compute_antialias_float(MPADecodeContext *s,
1796
                              GranuleDef *g)
1797
{
1798
    float *ptr;
1799
    int n, i;
1800

    
1801
    /* we antialias only "long" bands */
1802
    if (g->block_type == 2) {
1803
        if (!g->switch_point)
1804
            return;
1805
        /* XXX: check this for 8000Hz case */
1806
        n = 1;
1807
    } else {
1808
        n = SBLIMIT - 1;
1809
    }
1810

    
1811
    ptr = g->sb_hybrid + 18;
1812
    for(i = n;i > 0;i--) {
1813
        float tmp0, tmp1;
1814
        float *csa = &csa_table_float[0][0];
1815
#define FLOAT_AA(j)\
1816
        tmp0= ptr[-1-j];\
1817
        tmp1= ptr[   j];\
1818
        ptr[-1-j] = tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j];\
1819
        ptr[   j] = tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j];
1820

    
1821
        FLOAT_AA(0)
1822
        FLOAT_AA(1)
1823
        FLOAT_AA(2)
1824
        FLOAT_AA(3)
1825
        FLOAT_AA(4)
1826
        FLOAT_AA(5)
1827
        FLOAT_AA(6)
1828
        FLOAT_AA(7)
1829

    
1830
        ptr += 18;
1831
    }
1832
}
1833

    
1834
static void compute_imdct(MPADecodeContext *s,
1835
                          GranuleDef *g,
1836
                          INTFLOAT *sb_samples,
1837
                          INTFLOAT *mdct_buf)
1838
{
1839
    INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1840
    INTFLOAT out2[12];
1841
    int i, j, mdct_long_end, sblimit;
1842

    
1843
    /* find last non zero block */
1844
    ptr = g->sb_hybrid + 576;
1845
    ptr1 = g->sb_hybrid + 2 * 18;
1846
    while (ptr >= ptr1) {
1847
        int32_t *p;
1848
        ptr -= 6;
1849
        p= (int32_t*)ptr;
1850
        if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1851
            break;
1852
    }
1853
    sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1854

    
1855
    if (g->block_type == 2) {
1856
        /* XXX: check for 8000 Hz */
1857
        if (g->switch_point)
1858
            mdct_long_end = 2;
1859
        else
1860
            mdct_long_end = 0;
1861
    } else {
1862
        mdct_long_end = sblimit;
1863
    }
1864

    
1865
    buf = mdct_buf;
1866
    ptr = g->sb_hybrid;
1867
    for(j=0;j<mdct_long_end;j++) {
1868
        /* apply window & overlap with previous buffer */
1869
        out_ptr = sb_samples + j;
1870
        /* select window */
1871
        if (g->switch_point && j < 2)
1872
            win1 = mdct_win[0];
1873
        else
1874
            win1 = mdct_win[g->block_type];
1875
        /* select frequency inversion */
1876
        win = win1 + ((4 * 36) & -(j & 1));
1877
        imdct36(out_ptr, buf, ptr, win);
1878
        out_ptr += 18*SBLIMIT;
1879
        ptr += 18;
1880
        buf += 18;
1881
    }
1882
    for(j=mdct_long_end;j<sblimit;j++) {
1883
        /* select frequency inversion */
1884
        win = mdct_win[2] + ((4 * 36) & -(j & 1));
1885
        out_ptr = sb_samples + j;
1886

    
1887
        for(i=0; i<6; i++){
1888
            *out_ptr = buf[i];
1889
            out_ptr += SBLIMIT;
1890
        }
1891
        imdct12(out2, ptr + 0);
1892
        for(i=0;i<6;i++) {
1893
            *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*1];
1894
            buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1895
            out_ptr += SBLIMIT;
1896
        }
1897
        imdct12(out2, ptr + 1);
1898
        for(i=0;i<6;i++) {
1899
            *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*2];
1900
            buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1901
            out_ptr += SBLIMIT;
1902
        }
1903
        imdct12(out2, ptr + 2);
1904
        for(i=0;i<6;i++) {
1905
            buf[i + 6*0] = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*0];
1906
            buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1907
            buf[i + 6*2] = 0;
1908
        }
1909
        ptr += 18;
1910
        buf += 18;
1911
    }
1912
    /* zero bands */
1913
    for(j=sblimit;j<SBLIMIT;j++) {
1914
        /* overlap */
1915
        out_ptr = sb_samples + j;
1916
        for(i=0;i<18;i++) {
1917
            *out_ptr = buf[i];
1918
            buf[i] = 0;
1919
            out_ptr += SBLIMIT;
1920
        }
1921
        buf += 18;
1922
    }
1923
}
1924

    
1925
/* main layer3 decoding function */
1926
static int mp_decode_layer3(MPADecodeContext *s)
1927
{
1928
    int nb_granules, main_data_begin, private_bits;
1929
    int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1930
    GranuleDef *g;
1931
    int16_t exponents[576]; //FIXME try INTFLOAT
1932

    
1933
    /* read side info */
1934
    if (s->lsf) {
1935
        main_data_begin = get_bits(&s->gb, 8);
1936
        private_bits = get_bits(&s->gb, s->nb_channels);
1937
        nb_granules = 1;
1938
    } else {
1939
        main_data_begin = get_bits(&s->gb, 9);
1940
        if (s->nb_channels == 2)
1941
            private_bits = get_bits(&s->gb, 3);
1942
        else
1943
            private_bits = get_bits(&s->gb, 5);
1944
        nb_granules = 2;
1945
        for(ch=0;ch<s->nb_channels;ch++) {
1946
            s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1947
            s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1948
        }
1949
    }
1950

    
1951
    for(gr=0;gr<nb_granules;gr++) {
1952
        for(ch=0;ch<s->nb_channels;ch++) {
1953
            dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1954
            g = &s->granules[ch][gr];
1955
            g->part2_3_length = get_bits(&s->gb, 12);
1956
            g->big_values = get_bits(&s->gb, 9);
1957
            if(g->big_values > 288){
1958
                av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1959
                return -1;
1960
            }
1961

    
1962
            g->global_gain = get_bits(&s->gb, 8);
1963
            /* if MS stereo only is selected, we precompute the
1964
               1/sqrt(2) renormalization factor */
1965
            if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1966
                MODE_EXT_MS_STEREO)
1967
                g->global_gain -= 2;
1968
            if (s->lsf)
1969
                g->scalefac_compress = get_bits(&s->gb, 9);
1970
            else
1971
                g->scalefac_compress = get_bits(&s->gb, 4);
1972
            blocksplit_flag = get_bits1(&s->gb);
1973
            if (blocksplit_flag) {
1974
                g->block_type = get_bits(&s->gb, 2);
1975
                if (g->block_type == 0){
1976
                    av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1977
                    return -1;
1978
                }
1979
                g->switch_point = get_bits1(&s->gb);
1980
                for(i=0;i<2;i++)
1981
                    g->table_select[i] = get_bits(&s->gb, 5);
1982
                for(i=0;i<3;i++)
1983
                    g->subblock_gain[i] = get_bits(&s->gb, 3);
1984
                ff_init_short_region(s, g);
1985
            } else {
1986
                int region_address1, region_address2;
1987
                g->block_type = 0;
1988
                g->switch_point = 0;
1989
                for(i=0;i<3;i++)
1990
                    g->table_select[i] = get_bits(&s->gb, 5);
1991
                /* compute huffman coded region sizes */
1992
                region_address1 = get_bits(&s->gb, 4);
1993
                region_address2 = get_bits(&s->gb, 3);
1994
                dprintf(s->avctx, "region1=%d region2=%d\n",
1995
                        region_address1, region_address2);
1996
                ff_init_long_region(s, g, region_address1, region_address2);
1997
            }
1998
            ff_region_offset2size(g);
1999
            ff_compute_band_indexes(s, g);
2000

    
2001
            g->preflag = 0;
2002
            if (!s->lsf)
2003
                g->preflag = get_bits1(&s->gb);
2004
            g->scalefac_scale = get_bits1(&s->gb);
2005
            g->count1table_select = get_bits1(&s->gb);
2006
            dprintf(s->avctx, "block_type=%d switch_point=%d\n",
2007
                    g->block_type, g->switch_point);
2008
        }
2009
    }
2010

    
2011
  if (!s->adu_mode) {
2012
    const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2013
    assert((get_bits_count(&s->gb) & 7) == 0);
2014
    /* now we get bits from the main_data_begin offset */
2015
    dprintf(s->avctx, "seekback: %d\n", main_data_begin);
2016
//av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2017

    
2018
    memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2019
    s->in_gb= s->gb;
2020
        init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
2021
        skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
2022
  }
2023

    
2024
    for(gr=0;gr<nb_granules;gr++) {
2025
        for(ch=0;ch<s->nb_channels;ch++) {
2026
            g = &s->granules[ch][gr];
2027
            if(get_bits_count(&s->gb)<0){
2028
                av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
2029
                                            main_data_begin, s->last_buf_size, gr);
2030
                skip_bits_long(&s->gb, g->part2_3_length);
2031
                memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
2032
                if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
2033
                    skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
2034
                    s->gb= s->in_gb;
2035
                    s->in_gb.buffer=NULL;
2036
                }
2037
                continue;
2038
            }
2039

    
2040
            bits_pos = get_bits_count(&s->gb);
2041

    
2042
            if (!s->lsf) {
2043
                uint8_t *sc;
2044
                int slen, slen1, slen2;
2045

    
2046
                /* MPEG1 scale factors */
2047
                slen1 = slen_table[0][g->scalefac_compress];
2048
                slen2 = slen_table[1][g->scalefac_compress];
2049
                dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
2050
                if (g->block_type == 2) {
2051
                    n = g->switch_point ? 17 : 18;
2052
                    j = 0;
2053
                    if(slen1){
2054
                        for(i=0;i<n;i++)
2055
                            g->scale_factors[j++] = get_bits(&s->gb, slen1);
2056
                    }else{
2057
                        for(i=0;i<n;i++)
2058
                            g->scale_factors[j++] = 0;
2059
                    }
2060
                    if(slen2){
2061
                        for(i=0;i<18;i++)
2062
                            g->scale_factors[j++] = get_bits(&s->gb, slen2);
2063
                        for(i=0;i<3;i++)
2064
                            g->scale_factors[j++] = 0;
2065
                    }else{
2066
                        for(i=0;i<21;i++)
2067
                            g->scale_factors[j++] = 0;
2068
                    }
2069
                } else {
2070
                    sc = s->granules[ch][0].scale_factors;
2071
                    j = 0;
2072
                    for(k=0;k<4;k++) {
2073
                        n = (k == 0 ? 6 : 5);
2074
                        if ((g->scfsi & (0x8 >> k)) == 0) {
2075
                            slen = (k < 2) ? slen1 : slen2;
2076
                            if(slen){
2077
                                for(i=0;i<n;i++)
2078
                                    g->scale_factors[j++] = get_bits(&s->gb, slen);
2079
                            }else{
2080
                                for(i=0;i<n;i++)
2081
                                    g->scale_factors[j++] = 0;
2082
                            }
2083
                        } else {
2084
                            /* simply copy from last granule */
2085
                            for(i=0;i<n;i++) {
2086
                                g->scale_factors[j] = sc[j];
2087
                                j++;
2088
                            }
2089
                        }
2090
                    }
2091
                    g->scale_factors[j++] = 0;
2092
                }
2093
            } else {
2094
                int tindex, tindex2, slen[4], sl, sf;
2095

    
2096
                /* LSF scale factors */
2097
                if (g->block_type == 2) {
2098
                    tindex = g->switch_point ? 2 : 1;
2099
                } else {
2100
                    tindex = 0;
2101
                }
2102
                sf = g->scalefac_compress;
2103
                if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2104
                    /* intensity stereo case */
2105
                    sf >>= 1;
2106
                    if (sf < 180) {
2107
                        lsf_sf_expand(slen, sf, 6, 6, 0);
2108
                        tindex2 = 3;
2109
                    } else if (sf < 244) {
2110
                        lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2111
                        tindex2 = 4;
2112
                    } else {
2113
                        lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2114
                        tindex2 = 5;
2115
                    }
2116
                } else {
2117
                    /* normal case */
2118
                    if (sf < 400) {
2119
                        lsf_sf_expand(slen, sf, 5, 4, 4);
2120
                        tindex2 = 0;
2121
                    } else if (sf < 500) {
2122
                        lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2123
                        tindex2 = 1;
2124
                    } else {
2125
                        lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2126
                        tindex2 = 2;
2127
                        g->preflag = 1;
2128
                    }
2129
                }
2130

    
2131
                j = 0;
2132
                for(k=0;k<4;k++) {
2133
                    n = lsf_nsf_table[tindex2][tindex][k];
2134
                    sl = slen[k];
2135
                    if(sl){
2136
                        for(i=0;i<n;i++)
2137
                            g->scale_factors[j++] = get_bits(&s->gb, sl);
2138
                    }else{
2139
                        for(i=0;i<n;i++)
2140
                            g->scale_factors[j++] = 0;
2141
                    }
2142
                }
2143
                /* XXX: should compute exact size */
2144
                for(;j<40;j++)
2145
                    g->scale_factors[j] = 0;
2146
            }
2147

    
2148
            exponents_from_scale_factors(s, g, exponents);
2149

    
2150
            /* read Huffman coded residue */
2151
            huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
2152
        } /* ch */
2153

    
2154
        if (s->nb_channels == 2)
2155
            compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
2156

    
2157
        for(ch=0;ch<s->nb_channels;ch++) {
2158
            g = &s->granules[ch][gr];
2159

    
2160
            reorder_block(s, g);
2161
            compute_antialias(s, g);
2162
            compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2163
        }
2164
    } /* gr */
2165
    if(get_bits_count(&s->gb)<0)
2166
        skip_bits_long(&s->gb, -get_bits_count(&s->gb));
2167
    return nb_granules * 18;
2168
}
2169

    
2170
static int mp_decode_frame(MPADecodeContext *s,
2171
                           OUT_INT *samples, const uint8_t *buf, int buf_size)
2172
{
2173
    int i, nb_frames, ch;
2174
    OUT_INT *samples_ptr;
2175

    
2176
    init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2177

    
2178
    /* skip error protection field */
2179
    if (s->error_protection)
2180
        skip_bits(&s->gb, 16);
2181

    
2182
    dprintf(s->avctx, "frame %d:\n", s->frame_count);
2183
    switch(s->layer) {
2184
    case 1:
2185
        s->avctx->frame_size = 384;
2186
        nb_frames = mp_decode_layer1(s);
2187
        break;
2188
    case 2:
2189
        s->avctx->frame_size = 1152;
2190
        nb_frames = mp_decode_layer2(s);
2191
        break;
2192
    case 3:
2193
        s->avctx->frame_size = s->lsf ? 576 : 1152;
2194
    default:
2195
        nb_frames = mp_decode_layer3(s);
2196

    
2197
        s->last_buf_size=0;
2198
        if(s->in_gb.buffer){
2199
            align_get_bits(&s->gb);
2200
            i= get_bits_left(&s->gb)>>3;
2201
            if(i >= 0 && i <= BACKSTEP_SIZE){
2202
                memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2203
                s->last_buf_size=i;
2204
            }else
2205
                av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2206
            s->gb= s->in_gb;
2207
            s->in_gb.buffer= NULL;
2208
        }
2209

    
2210
        align_get_bits(&s->gb);
2211
        assert((get_bits_count(&s->gb) & 7) == 0);
2212
        i= get_bits_left(&s->gb)>>3;
2213

    
2214
        if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2215
            if(i<0)
2216
                av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2217
            i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2218
        }
2219
        assert(i <= buf_size - HEADER_SIZE && i>= 0);
2220
        memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2221
        s->last_buf_size += i;
2222

    
2223
        break;
2224
    }
2225

    
2226
    /* apply the synthesis filter */
2227
    for(ch=0;ch<s->nb_channels;ch++) {
2228
        samples_ptr = samples + ch;
2229
        for(i=0;i<nb_frames;i++) {
2230
            RENAME(ff_mpa_synth_filter)(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2231
                         RENAME(ff_mpa_synth_window), &s->dither_state,
2232
                         samples_ptr, s->nb_channels,
2233
                         s->sb_samples[ch][i]);
2234
            samples_ptr += 32 * s->nb_channels;
2235
        }
2236
    }
2237

    
2238
    return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2239
}
2240

    
2241
static int decode_frame(AVCodecContext * avctx,
2242
                        void *data, int *data_size,
2243
                        AVPacket *avpkt)
2244
{
2245
    const uint8_t *buf = avpkt->data;
2246
    int buf_size = avpkt->size;
2247
    MPADecodeContext *s = avctx->priv_data;
2248
    uint32_t header;
2249
    int out_size;
2250
    OUT_INT *out_samples = data;
2251

    
2252
    if(buf_size < HEADER_SIZE)
2253
        return -1;
2254

    
2255
    header = AV_RB32(buf);
2256
    if(ff_mpa_check_header(header) < 0){
2257
        av_log(avctx, AV_LOG_ERROR, "Header missing\n");
2258
        return -1;
2259
    }
2260

    
2261
    if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
2262
        /* free format: prepare to compute frame size */
2263
        s->frame_size = -1;
2264
        return -1;
2265
    }
2266
    /* update codec info */
2267
    avctx->channels = s->nb_channels;
2268
    avctx->bit_rate = s->bit_rate;
2269
    avctx->sub_id = s->layer;
2270

    
2271
    if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
2272
        return -1;
2273
    *data_size = 0;
2274

    
2275
    if(s->frame_size<=0 || s->frame_size > buf_size){
2276
        av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2277
        return -1;
2278
    }else if(s->frame_size < buf_size){
2279
        av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2280
        buf_size= s->frame_size;
2281
    }
2282

    
2283
    out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2284
    if(out_size>=0){
2285
        *data_size = out_size;
2286
        avctx->sample_rate = s->sample_rate;
2287
        //FIXME maybe move the other codec info stuff from above here too
2288
    }else
2289
        av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2290
    s->frame_size = 0;
2291
    return buf_size;
2292
}
2293

    
2294
static void flush(AVCodecContext *avctx){
2295
    MPADecodeContext *s = avctx->priv_data;
2296
    memset(s->synth_buf, 0, sizeof(s->synth_buf));
2297
    s->last_buf_size= 0;
2298
}
2299

    
2300
#if CONFIG_MP3ADU_DECODER
2301
static int decode_frame_adu(AVCodecContext * avctx,
2302
                        void *data, int *data_size,
2303
                        AVPacket *avpkt)
2304
{
2305
    const uint8_t *buf = avpkt->data;
2306
    int buf_size = avpkt->size;
2307
    MPADecodeContext *s = avctx->priv_data;
2308
    uint32_t header;
2309
    int len, out_size;
2310
    OUT_INT *out_samples = data;
2311

    
2312
    len = buf_size;
2313

    
2314
    // Discard too short frames
2315
    if (buf_size < HEADER_SIZE) {
2316
        *data_size = 0;
2317
        return buf_size;
2318
    }
2319

    
2320

    
2321
    if (len > MPA_MAX_CODED_FRAME_SIZE)
2322
        len = MPA_MAX_CODED_FRAME_SIZE;
2323

    
2324
    // Get header and restore sync word
2325
    header = AV_RB32(buf) | 0xffe00000;
2326

    
2327
    if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2328
        *data_size = 0;
2329
        return buf_size;
2330
    }
2331

    
2332
    ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2333
    /* update codec info */
2334
    avctx->sample_rate = s->sample_rate;
2335
    avctx->channels = s->nb_channels;
2336
    avctx->bit_rate = s->bit_rate;
2337
    avctx->sub_id = s->layer;
2338

    
2339
    s->frame_size = len;
2340

    
2341
    if (avctx->parse_only) {
2342
        out_size = buf_size;
2343
    } else {
2344
        out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2345
    }
2346

    
2347
    *data_size = out_size;
2348
    return buf_size;
2349
}
2350
#endif /* CONFIG_MP3ADU_DECODER */
2351

    
2352
#if CONFIG_MP3ON4_DECODER
2353

    
2354
/**
2355
 * Context for MP3On4 decoder
2356
 */
2357
typedef struct MP3On4DecodeContext {
2358
    int frames;   ///< number of mp3 frames per block (number of mp3 decoder instances)
2359
    int syncword; ///< syncword patch
2360
    const uint8_t *coff; ///< channels offsets in output buffer
2361
    MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2362
} MP3On4DecodeContext;
2363

    
2364
#include "mpeg4audio.h"
2365

    
2366
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2367
static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5};   /* number of mp3 decoder instances */
2368
/* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2369
static const uint8_t chan_offset[8][5] = {
2370
    {0},
2371
    {0},            // C
2372
    {0},            // FLR
2373
    {2,0},          // C FLR
2374
    {2,0,3},        // C FLR BS
2375
    {4,0,2},        // C FLR BLRS
2376
    {4,0,2,5},      // C FLR BLRS LFE
2377
    {4,0,2,6,5},    // C FLR BLRS BLR LFE
2378
};
2379

    
2380

    
2381
static int decode_init_mp3on4(AVCodecContext * avctx)
2382
{
2383
    MP3On4DecodeContext *s = avctx->priv_data;
2384
    MPEG4AudioConfig cfg;
2385
    int i;
2386

    
2387
    if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2388
        av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2389
        return -1;
2390
    }
2391

    
2392
    ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2393
    if (!cfg.chan_config || cfg.chan_config > 7) {
2394
        av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2395
        return -1;
2396
    }
2397
    s->frames = mp3Frames[cfg.chan_config];
2398
    s->coff = chan_offset[cfg.chan_config];
2399
    avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2400

    
2401
    if (cfg.sample_rate < 16000)
2402
        s->syncword = 0xffe00000;
2403
    else
2404
        s->syncword = 0xfff00000;
2405

    
2406
    /* Init the first mp3 decoder in standard way, so that all tables get builded
2407
     * We replace avctx->priv_data with the context of the first decoder so that
2408
     * decode_init() does not have to be changed.
2409
     * Other decoders will be initialized here copying data from the first context
2410
     */
2411
    // Allocate zeroed memory for the first decoder context
2412
    s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2413
    // Put decoder context in place to make init_decode() happy
2414
    avctx->priv_data = s->mp3decctx[0];
2415
    decode_init(avctx);
2416
    // Restore mp3on4 context pointer
2417
    avctx->priv_data = s;
2418
    s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2419

    
2420
    /* Create a separate codec/context for each frame (first is already ok).
2421
     * Each frame is 1 or 2 channels - up to 5 frames allowed
2422
     */
2423
    for (i = 1; i < s->frames; i++) {
2424
        s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2425
        s->mp3decctx[i]->adu_mode = 1;
2426
        s->mp3decctx[i]->avctx = avctx;
2427
    }
2428

    
2429
    return 0;
2430
}
2431

    
2432

    
2433
static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
2434
{
2435
    MP3On4DecodeContext *s = avctx->priv_data;
2436
    int i;
2437

    
2438
    for (i = 0; i < s->frames; i++)
2439
        if (s->mp3decctx[i])
2440
            av_free(s->mp3decctx[i]);
2441

    
2442
    return 0;
2443
}
2444

    
2445

    
2446
static int decode_frame_mp3on4(AVCodecContext * avctx,
2447
                        void *data, int *data_size,
2448
                        AVPacket *avpkt)
2449
{
2450
    const uint8_t *buf = avpkt->data;
2451
    int buf_size = avpkt->size;
2452
    MP3On4DecodeContext *s = avctx->priv_data;
2453
    MPADecodeContext *m;
2454
    int fsize, len = buf_size, out_size = 0;
2455
    uint32_t header;
2456
    OUT_INT *out_samples = data;
2457
    OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2458
    OUT_INT *outptr, *bp;
2459
    int fr, j, n;
2460

    
2461
    if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2462
        return -1;
2463

    
2464
    *data_size = 0;
2465
    // Discard too short frames
2466
    if (buf_size < HEADER_SIZE)
2467
        return -1;
2468

    
2469
    // If only one decoder interleave is not needed
2470
    outptr = s->frames == 1 ? out_samples : decoded_buf;
2471

    
2472
    avctx->bit_rate = 0;
2473

    
2474
    for (fr = 0; fr < s->frames; fr++) {
2475
        fsize = AV_RB16(buf) >> 4;
2476
        fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2477
        m = s->mp3decctx[fr];
2478
        assert (m != NULL);
2479

    
2480
        header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2481

    
2482
        if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2483
            break;
2484

    
2485
        ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2486
        out_size += mp_decode_frame(m, outptr, buf, fsize);
2487
        buf += fsize;
2488
        len -= fsize;
2489

    
2490
        if(s->frames > 1) {
2491
            n = m->avctx->frame_size*m->nb_channels;
2492
            /* interleave output data */
2493
            bp = out_samples + s->coff[fr];
2494
            if(m->nb_channels == 1) {
2495
                for(j = 0; j < n; j++) {
2496
                    *bp = decoded_buf[j];
2497
                    bp += avctx->channels;
2498
                }
2499
            } else {
2500
                for(j = 0; j < n; j++) {
2501
                    bp[0] = decoded_buf[j++];
2502
                    bp[1] = decoded_buf[j];
2503
                    bp += avctx->channels;
2504
                }
2505
            }
2506
        }
2507
        avctx->bit_rate += m->bit_rate;
2508
    }
2509

    
2510
    /* update codec info */
2511
    avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2512

    
2513
    *data_size = out_size;
2514
    return buf_size;
2515
}
2516
#endif /* CONFIG_MP3ON4_DECODER */
2517

    
2518
#if !CONFIG_FLOAT
2519
#if CONFIG_MP1_DECODER
2520
AVCodec mp1_decoder =
2521
{
2522
    "mp1",
2523
    AVMEDIA_TYPE_AUDIO,
2524
    CODEC_ID_MP1,
2525
    sizeof(MPADecodeContext),
2526
    decode_init,
2527
    NULL,
2528
    NULL,
2529
    decode_frame,
2530
    CODEC_CAP_PARSE_ONLY,
2531
    .flush= flush,
2532
    .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2533
};
2534
#endif
2535
#if CONFIG_MP2_DECODER
2536
AVCodec mp2_decoder =
2537
{
2538
    "mp2",
2539
    AVMEDIA_TYPE_AUDIO,
2540
    CODEC_ID_MP2,
2541
    sizeof(MPADecodeContext),
2542
    decode_init,
2543
    NULL,
2544
    NULL,
2545
    decode_frame,
2546
    CODEC_CAP_PARSE_ONLY,
2547
    .flush= flush,
2548
    .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2549
};
2550
#endif
2551
#if CONFIG_MP3_DECODER
2552
AVCodec mp3_decoder =
2553
{
2554
    "mp3",
2555
    AVMEDIA_TYPE_AUDIO,
2556
    CODEC_ID_MP3,
2557
    sizeof(MPADecodeContext),
2558
    decode_init,
2559
    NULL,
2560
    NULL,
2561
    decode_frame,
2562
    CODEC_CAP_PARSE_ONLY,
2563
    .flush= flush,
2564
    .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2565
};
2566
#endif
2567
#if CONFIG_MP3ADU_DECODER
2568
AVCodec mp3adu_decoder =
2569
{
2570
    "mp3adu",
2571
    AVMEDIA_TYPE_AUDIO,
2572
    CODEC_ID_MP3ADU,
2573
    sizeof(MPADecodeContext),
2574
    decode_init,
2575
    NULL,
2576
    NULL,
2577
    decode_frame_adu,
2578
    CODEC_CAP_PARSE_ONLY,
2579
    .flush= flush,
2580
    .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2581
};
2582
#endif
2583
#if CONFIG_MP3ON4_DECODER
2584
AVCodec mp3on4_decoder =
2585
{
2586
    "mp3on4",
2587
    AVMEDIA_TYPE_AUDIO,
2588
    CODEC_ID_MP3ON4,
2589
    sizeof(MP3On4DecodeContext),
2590
    decode_init_mp3on4,
2591
    NULL,
2592
    decode_close_mp3on4,
2593
    decode_frame_mp3on4,
2594
    .flush= flush,
2595
    .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
2596
};
2597
#endif
2598
#endif