Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudiodec.c @ 40914d97

History | View | Annotate | Download (77.4 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 = tab[a] + tab[b];\
532
    tmp1 = tab[a] - tab[b];\
533
    tab[a] = tmp0;\
534
    tab[b] = MULH3(tmp1, c, 1<<(s));\
535
}
536

    
537
#define BF1(a, b, c, d)\
538
{\
539
    BF(a, b, COS4_0, 1);\
540
    BF(c, d,-COS4_0, 1);\
541
    tab[c] += tab[d];\
542
}
543

    
544
#define BF2(a, b, c, d)\
545
{\
546
    BF(a, b, COS4_0, 1);\
547
    BF(c, d,-COS4_0, 1);\
548
    tab[c] += tab[d];\
549
    tab[a] += tab[c];\
550
    tab[c] += tab[b];\
551
    tab[b] += tab[d];\
552
}
553

    
554
#define ADD(a, b) tab[a] += tab[b]
555

    
556
/* DCT32 without 1/sqrt(2) coef zero scaling. */
557
static void dct32(INTFLOAT *out, INTFLOAT *tab)
558
{
559
    INTFLOAT tmp0, tmp1;
560

    
561
    /* pass 1 */
562
    BF( 0, 31, COS0_0 , 1);
563
    BF(15, 16, COS0_15, 5);
564
    /* pass 2 */
565
    BF( 0, 15, COS1_0 , 1);
566
    BF(16, 31,-COS1_0 , 1);
567
    /* pass 1 */
568
    BF( 7, 24, COS0_7 , 1);
569
    BF( 8, 23, COS0_8 , 1);
570
    /* pass 2 */
571
    BF( 7,  8, COS1_7 , 4);
572
    BF(23, 24,-COS1_7 , 4);
573
    /* pass 3 */
574
    BF( 0,  7, COS2_0 , 1);
575
    BF( 8, 15,-COS2_0 , 1);
576
    BF(16, 23, COS2_0 , 1);
577
    BF(24, 31,-COS2_0 , 1);
578
    /* pass 1 */
579
    BF( 3, 28, COS0_3 , 1);
580
    BF(12, 19, COS0_12, 2);
581
    /* pass 2 */
582
    BF( 3, 12, COS1_3 , 1);
583
    BF(19, 28,-COS1_3 , 1);
584
    /* pass 1 */
585
    BF( 4, 27, COS0_4 , 1);
586
    BF(11, 20, COS0_11, 2);
587
    /* pass 2 */
588
    BF( 4, 11, COS1_4 , 1);
589
    BF(20, 27,-COS1_4 , 1);
590
    /* pass 3 */
591
    BF( 3,  4, COS2_3 , 3);
592
    BF(11, 12,-COS2_3 , 3);
593
    BF(19, 20, COS2_3 , 3);
594
    BF(27, 28,-COS2_3 , 3);
595
    /* pass 4 */
596
    BF( 0,  3, COS3_0 , 1);
597
    BF( 4,  7,-COS3_0 , 1);
598
    BF( 8, 11, COS3_0 , 1);
599
    BF(12, 15,-COS3_0 , 1);
600
    BF(16, 19, COS3_0 , 1);
601
    BF(20, 23,-COS3_0 , 1);
602
    BF(24, 27, COS3_0 , 1);
603
    BF(28, 31,-COS3_0 , 1);
604

    
605

    
606

    
607
    /* pass 1 */
608
    BF( 1, 30, COS0_1 , 1);
609
    BF(14, 17, COS0_14, 3);
610
    /* pass 2 */
611
    BF( 1, 14, COS1_1 , 1);
612
    BF(17, 30,-COS1_1 , 1);
613
    /* pass 1 */
614
    BF( 6, 25, COS0_6 , 1);
615
    BF( 9, 22, COS0_9 , 1);
616
    /* pass 2 */
617
    BF( 6,  9, COS1_6 , 2);
618
    BF(22, 25,-COS1_6 , 2);
619
    /* pass 3 */
620
    BF( 1,  6, COS2_1 , 1);
621
    BF( 9, 14,-COS2_1 , 1);
622
    BF(17, 22, COS2_1 , 1);
623
    BF(25, 30,-COS2_1 , 1);
624

    
625
    /* pass 1 */
626
    BF( 2, 29, COS0_2 , 1);
627
    BF(13, 18, COS0_13, 3);
628
    /* pass 2 */
629
    BF( 2, 13, COS1_2 , 1);
630
    BF(18, 29,-COS1_2 , 1);
631
    /* pass 1 */
632
    BF( 5, 26, COS0_5 , 1);
633
    BF(10, 21, COS0_10, 1);
634
    /* pass 2 */
635
    BF( 5, 10, COS1_5 , 2);
636
    BF(21, 26,-COS1_5 , 2);
637
    /* pass 3 */
638
    BF( 2,  5, COS2_2 , 1);
639
    BF(10, 13,-COS2_2 , 1);
640
    BF(18, 21, COS2_2 , 1);
641
    BF(26, 29,-COS2_2 , 1);
642
    /* pass 4 */
643
    BF( 1,  2, COS3_1 , 2);
644
    BF( 5,  6,-COS3_1 , 2);
645
    BF( 9, 10, COS3_1 , 2);
646
    BF(13, 14,-COS3_1 , 2);
647
    BF(17, 18, COS3_1 , 2);
648
    BF(21, 22,-COS3_1 , 2);
649
    BF(25, 26, COS3_1 , 2);
650
    BF(29, 30,-COS3_1 , 2);
651

    
652
    /* pass 5 */
653
    BF1( 0,  1,  2,  3);
654
    BF2( 4,  5,  6,  7);
655
    BF1( 8,  9, 10, 11);
656
    BF2(12, 13, 14, 15);
657
    BF1(16, 17, 18, 19);
658
    BF2(20, 21, 22, 23);
659
    BF1(24, 25, 26, 27);
660
    BF2(28, 29, 30, 31);
661

    
662
    /* pass 6 */
663

    
664
    ADD( 8, 12);
665
    ADD(12, 10);
666
    ADD(10, 14);
667
    ADD(14,  9);
668
    ADD( 9, 13);
669
    ADD(13, 11);
670
    ADD(11, 15);
671

    
672
    out[ 0] = tab[0];
673
    out[16] = tab[1];
674
    out[ 8] = tab[2];
675
    out[24] = tab[3];
676
    out[ 4] = tab[4];
677
    out[20] = tab[5];
678
    out[12] = tab[6];
679
    out[28] = tab[7];
680
    out[ 2] = tab[8];
681
    out[18] = tab[9];
682
    out[10] = tab[10];
683
    out[26] = tab[11];
684
    out[ 6] = tab[12];
685
    out[22] = tab[13];
686
    out[14] = tab[14];
687
    out[30] = tab[15];
688

    
689
    ADD(24, 28);
690
    ADD(28, 26);
691
    ADD(26, 30);
692
    ADD(30, 25);
693
    ADD(25, 29);
694
    ADD(29, 27);
695
    ADD(27, 31);
696

    
697
    out[ 1] = tab[16] + tab[24];
698
    out[17] = tab[17] + tab[25];
699
    out[ 9] = tab[18] + tab[26];
700
    out[25] = tab[19] + tab[27];
701
    out[ 5] = tab[20] + tab[28];
702
    out[21] = tab[21] + tab[29];
703
    out[13] = tab[22] + tab[30];
704
    out[29] = tab[23] + tab[31];
705
    out[ 3] = tab[24] + tab[20];
706
    out[19] = tab[25] + tab[21];
707
    out[11] = tab[26] + tab[22];
708
    out[27] = tab[27] + tab[23];
709
    out[ 7] = tab[28] + tab[18];
710
    out[23] = tab[29] + tab[19];
711
    out[15] = tab[30] + tab[17];
712
    out[31] = tab[31];
713
}
714

    
715
#if CONFIG_FLOAT
716
static inline float round_sample(float *sum)
717
{
718
    float sum1=*sum;
719
    *sum = 0;
720
    return sum1;
721
}
722

    
723
/* signed 16x16 -> 32 multiply add accumulate */
724
#define MACS(rt, ra, rb) rt+=(ra)*(rb)
725

    
726
/* signed 16x16 -> 32 multiply */
727
#define MULS(ra, rb) ((ra)*(rb))
728

    
729
#define MLSS(rt, ra, rb) rt-=(ra)*(rb)
730

    
731
#elif FRAC_BITS <= 15
732

    
733
static inline int round_sample(int *sum)
734
{
735
    int sum1;
736
    sum1 = (*sum) >> OUT_SHIFT;
737
    *sum &= (1<<OUT_SHIFT)-1;
738
    return av_clip(sum1, OUT_MIN, OUT_MAX);
739
}
740

    
741
/* signed 16x16 -> 32 multiply add accumulate */
742
#define MACS(rt, ra, rb) MAC16(rt, ra, rb)
743

    
744
/* signed 16x16 -> 32 multiply */
745
#define MULS(ra, rb) MUL16(ra, rb)
746

    
747
#define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
748

    
749
#else
750

    
751
static inline int round_sample(int64_t *sum)
752
{
753
    int sum1;
754
    sum1 = (int)((*sum) >> OUT_SHIFT);
755
    *sum &= (1<<OUT_SHIFT)-1;
756
    return av_clip(sum1, OUT_MIN, OUT_MAX);
757
}
758

    
759
#   define MULS(ra, rb) MUL64(ra, rb)
760
#   define MACS(rt, ra, rb) MAC64(rt, ra, rb)
761
#   define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
762
#endif
763

    
764
#define SUM8(op, sum, w, p)               \
765
{                                         \
766
    op(sum, (w)[0 * 64], (p)[0 * 64]);    \
767
    op(sum, (w)[1 * 64], (p)[1 * 64]);    \
768
    op(sum, (w)[2 * 64], (p)[2 * 64]);    \
769
    op(sum, (w)[3 * 64], (p)[3 * 64]);    \
770
    op(sum, (w)[4 * 64], (p)[4 * 64]);    \
771
    op(sum, (w)[5 * 64], (p)[5 * 64]);    \
772
    op(sum, (w)[6 * 64], (p)[6 * 64]);    \
773
    op(sum, (w)[7 * 64], (p)[7 * 64]);    \
774
}
775

    
776
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
777
{                                               \
778
    INTFLOAT tmp;\
779
    tmp = p[0 * 64];\
780
    op1(sum1, (w1)[0 * 64], tmp);\
781
    op2(sum2, (w2)[0 * 64], tmp);\
782
    tmp = p[1 * 64];\
783
    op1(sum1, (w1)[1 * 64], tmp);\
784
    op2(sum2, (w2)[1 * 64], tmp);\
785
    tmp = p[2 * 64];\
786
    op1(sum1, (w1)[2 * 64], tmp);\
787
    op2(sum2, (w2)[2 * 64], tmp);\
788
    tmp = p[3 * 64];\
789
    op1(sum1, (w1)[3 * 64], tmp);\
790
    op2(sum2, (w2)[3 * 64], tmp);\
791
    tmp = p[4 * 64];\
792
    op1(sum1, (w1)[4 * 64], tmp);\
793
    op2(sum2, (w2)[4 * 64], tmp);\
794
    tmp = p[5 * 64];\
795
    op1(sum1, (w1)[5 * 64], tmp);\
796
    op2(sum2, (w2)[5 * 64], tmp);\
797
    tmp = p[6 * 64];\
798
    op1(sum1, (w1)[6 * 64], tmp);\
799
    op2(sum2, (w2)[6 * 64], tmp);\
800
    tmp = p[7 * 64];\
801
    op1(sum1, (w1)[7 * 64], tmp);\
802
    op2(sum2, (w2)[7 * 64], tmp);\
803
}
804

    
805
void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
806
{
807
    int i;
808

    
809
    /* max = 18760, max sum over all 16 coefs : 44736 */
810
    for(i=0;i<257;i++) {
811
        INTFLOAT v;
812
        v = ff_mpa_enwindow[i];
813
#if CONFIG_FLOAT
814
        v *= 1.0 / (1LL<<(16 + FRAC_BITS));
815
#elif WFRAC_BITS < 16
816
        v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
817
#endif
818
        window[i] = v;
819
        if ((i & 63) != 0)
820
            v = -v;
821
        if (i != 0)
822
            window[512 - i] = v;
823
    }
824
}
825

    
826
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
827
   32 samples. */
828
/* XXX: optimize by avoiding ring buffer usage */
829
void RENAME(ff_mpa_synth_filter)(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
830
                         MPA_INT *window, int *dither_state,
831
                         OUT_INT *samples, int incr,
832
                         INTFLOAT sb_samples[SBLIMIT])
833
{
834
    register MPA_INT *synth_buf;
835
    register const MPA_INT *w, *w2, *p;
836
    int j, offset;
837
    OUT_INT *samples2;
838
#if CONFIG_FLOAT
839
    float sum, sum2;
840
#elif FRAC_BITS <= 15
841
    int32_t tmp[32];
842
    int sum, sum2;
843
#else
844
    int64_t sum, sum2;
845
#endif
846

    
847
    offset = *synth_buf_offset;
848
    synth_buf = synth_buf_ptr + offset;
849

    
850
#if FRAC_BITS <= 15 && !CONFIG_FLOAT
851
    dct32(tmp, sb_samples);
852
    for(j=0;j<32;j++) {
853
        /* NOTE: can cause a loss in precision if very high amplitude
854
           sound */
855
        synth_buf[j] = av_clip_int16(tmp[j]);
856
    }
857
#else
858
    dct32(synth_buf, sb_samples);
859
#endif
860

    
861
    /* copy to avoid wrap */
862
    memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
863

    
864
    samples2 = samples + 31 * incr;
865
    w = window;
866
    w2 = window + 31;
867

    
868
    sum = *dither_state;
869
    p = synth_buf + 16;
870
    SUM8(MACS, sum, w, p);
871
    p = synth_buf + 48;
872
    SUM8(MLSS, sum, w + 32, p);
873
    *samples = round_sample(&sum);
874
    samples += incr;
875
    w++;
876

    
877
    /* we calculate two samples at the same time to avoid one memory
878
       access per two sample */
879
    for(j=1;j<16;j++) {
880
        sum2 = 0;
881
        p = synth_buf + 16 + j;
882
        SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
883
        p = synth_buf + 48 - j;
884
        SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
885

    
886
        *samples = round_sample(&sum);
887
        samples += incr;
888
        sum += sum2;
889
        *samples2 = round_sample(&sum);
890
        samples2 -= incr;
891
        w++;
892
        w2--;
893
    }
894

    
895
    p = synth_buf + 32;
896
    SUM8(MLSS, sum, w + 32, p);
897
    *samples = round_sample(&sum);
898
    *dither_state= sum;
899

    
900
    offset = (offset - 32) & 511;
901
    *synth_buf_offset = offset;
902
}
903

    
904
#define C3 FIXHR(0.86602540378443864676/2)
905

    
906
/* 0.5 / cos(pi*(2*i+1)/36) */
907
static const INTFLOAT icos36[9] = {
908
    FIXR(0.50190991877167369479),
909
    FIXR(0.51763809020504152469), //0
910
    FIXR(0.55168895948124587824),
911
    FIXR(0.61038729438072803416),
912
    FIXR(0.70710678118654752439), //1
913
    FIXR(0.87172339781054900991),
914
    FIXR(1.18310079157624925896),
915
    FIXR(1.93185165257813657349), //2
916
    FIXR(5.73685662283492756461),
917
};
918

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

    
932
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
933
   cases. */
934
static void imdct12(INTFLOAT *out, INTFLOAT *in)
935
{
936
    INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
937

    
938
    in0= in[0*3];
939
    in1= in[1*3] + in[0*3];
940
    in2= in[2*3] + in[1*3];
941
    in3= in[3*3] + in[2*3];
942
    in4= in[4*3] + in[3*3];
943
    in5= in[5*3] + in[4*3];
944
    in5 += in3;
945
    in3 += in1;
946

    
947
    in2= MULH3(in2, C3, 2);
948
    in3= MULH3(in3, C3, 4);
949

    
950
    t1 = in0 - in4;
951
    t2 = MULH3(in1 - in5, icos36h[4], 2);
952

    
953
    out[ 7]=
954
    out[10]= t1 + t2;
955
    out[ 1]=
956
    out[ 4]= t1 - t2;
957

    
958
    in0 += SHR(in4, 1);
959
    in4 = in0 + in2;
960
    in5 += 2*in1;
961
    in1 = MULH3(in5 + in3, icos36h[1], 1);
962
    out[ 8]=
963
    out[ 9]= in4 + in1;
964
    out[ 2]=
965
    out[ 3]= in4 - in1;
966

    
967
    in0 -= in2;
968
    in5 = MULH3(in5 - in3, icos36h[7], 2);
969
    out[ 0]=
970
    out[ 5]= in0 - in5;
971
    out[ 6]=
972
    out[11]= in0 + in5;
973
}
974

    
975
/* cos(pi*i/18) */
976
#define C1 FIXHR(0.98480775301220805936/2)
977
#define C2 FIXHR(0.93969262078590838405/2)
978
#define C3 FIXHR(0.86602540378443864676/2)
979
#define C4 FIXHR(0.76604444311897803520/2)
980
#define C5 FIXHR(0.64278760968653932632/2)
981
#define C6 FIXHR(0.5/2)
982
#define C7 FIXHR(0.34202014332566873304/2)
983
#define C8 FIXHR(0.17364817766693034885/2)
984

    
985

    
986
/* using Lee like decomposition followed by hand coded 9 points DCT */
987
static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
988
{
989
    int i, j;
990
    INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
991
    INTFLOAT tmp[18], *tmp1, *in1;
992

    
993
    for(i=17;i>=1;i--)
994
        in[i] += in[i-1];
995
    for(i=17;i>=3;i-=2)
996
        in[i] += in[i-2];
997

    
998
    for(j=0;j<2;j++) {
999
        tmp1 = tmp + j;
1000
        in1 = in + j;
1001

    
1002
        t2 = in1[2*4] + in1[2*8] - in1[2*2];
1003

    
1004
        t3 = in1[2*0] + SHR(in1[2*6],1);
1005
        t1 = in1[2*0] - in1[2*6];
1006
        tmp1[ 6] = t1 - SHR(t2,1);
1007
        tmp1[16] = t1 + t2;
1008

    
1009
        t0 = MULH3(in1[2*2] + in1[2*4] ,    C2, 2);
1010
        t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
1011
        t2 = MULH3(in1[2*2] + in1[2*8] ,   -C4, 2);
1012

    
1013
        tmp1[10] = t3 - t0 - t2;
1014
        tmp1[ 2] = t3 + t0 + t1;
1015
        tmp1[14] = t3 + t2 - t1;
1016

    
1017
        tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
1018
        t2 = MULH3(in1[2*1] + in1[2*5],    C1, 2);
1019
        t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
1020
        t0 = MULH3(in1[2*3], C3, 2);
1021

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

    
1024
        tmp1[ 0] = t2 + t3 + t0;
1025
        tmp1[12] = t2 + t1 - t0;
1026
        tmp1[ 8] = t3 - t1 - t0;
1027
    }
1028

    
1029
    i = 0;
1030
    for(j=0;j<4;j++) {
1031
        t0 = tmp[i];
1032
        t1 = tmp[i + 2];
1033
        s0 = t1 + t0;
1034
        s2 = t1 - t0;
1035

    
1036
        t2 = tmp[i + 1];
1037
        t3 = tmp[i + 3];
1038
        s1 = MULH3(t3 + t2, icos36h[j], 2);
1039
        s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
1040

    
1041
        t0 = s0 + s1;
1042
        t1 = s0 - s1;
1043
        out[(9 + j)*SBLIMIT] =  MULH3(t1, win[9 + j], 1) + buf[9 + j];
1044
        out[(8 - j)*SBLIMIT] =  MULH3(t1, win[8 - j], 1) + buf[8 - j];
1045
        buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
1046
        buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
1047

    
1048
        t0 = s2 + s3;
1049
        t1 = s2 - s3;
1050
        out[(9 + 8 - j)*SBLIMIT] =  MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
1051
        out[(        j)*SBLIMIT] =  MULH3(t1, win[        j], 1) + buf[        j];
1052
        buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
1053
        buf[      + j] = MULH3(t0, win[18         + j], 1);
1054
        i += 4;
1055
    }
1056

    
1057
    s0 = tmp[16];
1058
    s1 = MULH3(tmp[17], icos36h[4], 2);
1059
    t0 = s0 + s1;
1060
    t1 = s0 - s1;
1061
    out[(9 + 4)*SBLIMIT] =  MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
1062
    out[(8 - 4)*SBLIMIT] =  MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
1063
    buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
1064
    buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
1065
}
1066

    
1067
/* return the number of decoded frames */
1068
static int mp_decode_layer1(MPADecodeContext *s)
1069
{
1070
    int bound, i, v, n, ch, j, mant;
1071
    uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1072
    uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1073

    
1074
    if (s->mode == MPA_JSTEREO)
1075
        bound = (s->mode_ext + 1) * 4;
1076
    else
1077
        bound = SBLIMIT;
1078

    
1079
    /* allocation bits */
1080
    for(i=0;i<bound;i++) {
1081
        for(ch=0;ch<s->nb_channels;ch++) {
1082
            allocation[ch][i] = get_bits(&s->gb, 4);
1083
        }
1084
    }
1085
    for(i=bound;i<SBLIMIT;i++) {
1086
        allocation[0][i] = get_bits(&s->gb, 4);
1087
    }
1088

    
1089
    /* scale factors */
1090
    for(i=0;i<bound;i++) {
1091
        for(ch=0;ch<s->nb_channels;ch++) {
1092
            if (allocation[ch][i])
1093
                scale_factors[ch][i] = get_bits(&s->gb, 6);
1094
        }
1095
    }
1096
    for(i=bound;i<SBLIMIT;i++) {
1097
        if (allocation[0][i]) {
1098
            scale_factors[0][i] = get_bits(&s->gb, 6);
1099
            scale_factors[1][i] = get_bits(&s->gb, 6);
1100
        }
1101
    }
1102

    
1103
    /* compute samples */
1104
    for(j=0;j<12;j++) {
1105
        for(i=0;i<bound;i++) {
1106
            for(ch=0;ch<s->nb_channels;ch++) {
1107
                n = allocation[ch][i];
1108
                if (n) {
1109
                    mant = get_bits(&s->gb, n + 1);
1110
                    v = l1_unscale(n, mant, scale_factors[ch][i]);
1111
                } else {
1112
                    v = 0;
1113
                }
1114
                s->sb_samples[ch][j][i] = v;
1115
            }
1116
        }
1117
        for(i=bound;i<SBLIMIT;i++) {
1118
            n = allocation[0][i];
1119
            if (n) {
1120
                mant = get_bits(&s->gb, n + 1);
1121
                v = l1_unscale(n, mant, scale_factors[0][i]);
1122
                s->sb_samples[0][j][i] = v;
1123
                v = l1_unscale(n, mant, scale_factors[1][i]);
1124
                s->sb_samples[1][j][i] = v;
1125
            } else {
1126
                s->sb_samples[0][j][i] = 0;
1127
                s->sb_samples[1][j][i] = 0;
1128
            }
1129
        }
1130
    }
1131
    return 12;
1132
}
1133

    
1134
static int mp_decode_layer2(MPADecodeContext *s)
1135
{
1136
    int sblimit; /* number of used subbands */
1137
    const unsigned char *alloc_table;
1138
    int table, bit_alloc_bits, i, j, ch, bound, v;
1139
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1140
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1141
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1142
    int scale, qindex, bits, steps, k, l, m, b;
1143

    
1144
    /* select decoding table */
1145
    table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
1146
                            s->sample_rate, s->lsf);
1147
    sblimit = ff_mpa_sblimit_table[table];
1148
    alloc_table = ff_mpa_alloc_tables[table];
1149

    
1150
    if (s->mode == MPA_JSTEREO)
1151
        bound = (s->mode_ext + 1) * 4;
1152
    else
1153
        bound = sblimit;
1154

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

    
1157
    /* sanity check */
1158
    if( bound > sblimit ) bound = sblimit;
1159

    
1160
    /* parse bit allocation */
1161
    j = 0;
1162
    for(i=0;i<bound;i++) {
1163
        bit_alloc_bits = alloc_table[j];
1164
        for(ch=0;ch<s->nb_channels;ch++) {
1165
            bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1166
        }
1167
        j += 1 << bit_alloc_bits;
1168
    }
1169
    for(i=bound;i<sblimit;i++) {
1170
        bit_alloc_bits = alloc_table[j];
1171
        v = get_bits(&s->gb, bit_alloc_bits);
1172
        bit_alloc[0][i] = v;
1173
        bit_alloc[1][i] = v;
1174
        j += 1 << bit_alloc_bits;
1175
    }
1176

    
1177
    /* scale codes */
1178
    for(i=0;i<sblimit;i++) {
1179
        for(ch=0;ch<s->nb_channels;ch++) {
1180
            if (bit_alloc[ch][i])
1181
                scale_code[ch][i] = get_bits(&s->gb, 2);
1182
        }
1183
    }
1184

    
1185
    /* scale factors */
1186
    for(i=0;i<sblimit;i++) {
1187
        for(ch=0;ch<s->nb_channels;ch++) {
1188
            if (bit_alloc[ch][i]) {
1189
                sf = scale_factors[ch][i];
1190
                switch(scale_code[ch][i]) {
1191
                default:
1192
                case 0:
1193
                    sf[0] = get_bits(&s->gb, 6);
1194
                    sf[1] = get_bits(&s->gb, 6);
1195
                    sf[2] = get_bits(&s->gb, 6);
1196
                    break;
1197
                case 2:
1198
                    sf[0] = get_bits(&s->gb, 6);
1199
                    sf[1] = sf[0];
1200
                    sf[2] = sf[0];
1201
                    break;
1202
                case 1:
1203
                    sf[0] = get_bits(&s->gb, 6);
1204
                    sf[2] = get_bits(&s->gb, 6);
1205
                    sf[1] = sf[0];
1206
                    break;
1207
                case 3:
1208
                    sf[0] = get_bits(&s->gb, 6);
1209
                    sf[2] = get_bits(&s->gb, 6);
1210
                    sf[1] = sf[2];
1211
                    break;
1212
                }
1213
            }
1214
        }
1215
    }
1216

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

    
1320
#define SPLIT(dst,sf,n)\
1321
    if(n==3){\
1322
        int m= (sf*171)>>9;\
1323
        dst= sf - 3*m;\
1324
        sf=m;\
1325
    }else if(n==4){\
1326
        dst= sf&3;\
1327
        sf>>=2;\
1328
    }else if(n==5){\
1329
        int m= (sf*205)>>10;\
1330
        dst= sf - 5*m;\
1331
        sf=m;\
1332
    }else if(n==6){\
1333
        int m= (sf*171)>>10;\
1334
        dst= sf - 6*m;\
1335
        sf=m;\
1336
    }else{\
1337
        dst=0;\
1338
    }
1339

    
1340
static av_always_inline void lsf_sf_expand(int *slen,
1341
                                 int sf, int n1, int n2, int n3)
1342
{
1343
    SPLIT(slen[3], sf, n3)
1344
    SPLIT(slen[2], sf, n2)
1345
    SPLIT(slen[1], sf, n1)
1346
    slen[0] = sf;
1347
}
1348

    
1349
static void exponents_from_scale_factors(MPADecodeContext *s,
1350
                                         GranuleDef *g,
1351
                                         int16_t *exponents)
1352
{
1353
    const uint8_t *bstab, *pretab;
1354
    int len, i, j, k, l, v0, shift, gain, gains[3];
1355
    int16_t *exp_ptr;
1356

    
1357
    exp_ptr = exponents;
1358
    gain = g->global_gain - 210;
1359
    shift = g->scalefac_scale + 1;
1360

    
1361
    bstab = band_size_long[s->sample_rate_index];
1362
    pretab = mpa_pretab[g->preflag];
1363
    for(i=0;i<g->long_end;i++) {
1364
        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1365
        len = bstab[i];
1366
        for(j=len;j>0;j--)
1367
            *exp_ptr++ = v0;
1368
    }
1369

    
1370
    if (g->short_start < 13) {
1371
        bstab = band_size_short[s->sample_rate_index];
1372
        gains[0] = gain - (g->subblock_gain[0] << 3);
1373
        gains[1] = gain - (g->subblock_gain[1] << 3);
1374
        gains[2] = gain - (g->subblock_gain[2] << 3);
1375
        k = g->long_end;
1376
        for(i=g->short_start;i<13;i++) {
1377
            len = bstab[i];
1378
            for(l=0;l<3;l++) {
1379
                v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1380
                for(j=len;j>0;j--)
1381
                *exp_ptr++ = v0;
1382
            }
1383
        }
1384
    }
1385
}
1386

    
1387
/* handle n = 0 too */
1388
static inline int get_bitsz(GetBitContext *s, int n)
1389
{
1390
    if (n == 0)
1391
        return 0;
1392
    else
1393
        return get_bits(s, n);
1394
}
1395

    
1396

    
1397
static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1398
    if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1399
        s->gb= s->in_gb;
1400
        s->in_gb.buffer=NULL;
1401
        assert((get_bits_count(&s->gb) & 7) == 0);
1402
        skip_bits_long(&s->gb, *pos - *end_pos);
1403
        *end_pos2=
1404
        *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1405
        *pos= get_bits_count(&s->gb);
1406
    }
1407
}
1408

    
1409
/* Following is a optimized code for
1410
            INTFLOAT v = *src
1411
            if(get_bits1(&s->gb))
1412
                v = -v;
1413
            *dst = v;
1414
*/
1415
#if CONFIG_FLOAT
1416
#define READ_FLIP_SIGN(dst,src)\
1417
            v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1418
            AV_WN32A(dst, v);
1419
#else
1420
#define READ_FLIP_SIGN(dst,src)\
1421
            v= -get_bits1(&s->gb);\
1422
            *(dst) = (*(src) ^ v) - v;
1423
#endif
1424

    
1425
static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1426
                          int16_t *exponents, int end_pos2)
1427
{
1428
    int s_index;
1429
    int i;
1430
    int last_pos, bits_left;
1431
    VLC *vlc;
1432
    int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1433

    
1434
    /* low frequencies (called big values) */
1435
    s_index = 0;
1436
    for(i=0;i<3;i++) {
1437
        int j, k, l, linbits;
1438
        j = g->region_size[i];
1439
        if (j == 0)
1440
            continue;
1441
        /* select vlc table */
1442
        k = g->table_select[i];
1443
        l = mpa_huff_data[k][0];
1444
        linbits = mpa_huff_data[k][1];
1445
        vlc = &huff_vlc[l];
1446

    
1447
        if(!l){
1448
            memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1449
            s_index += 2*j;
1450
            continue;
1451
        }
1452

    
1453
        /* read huffcode and compute each couple */
1454
        for(;j>0;j--) {
1455
            int exponent, x, y;
1456
            int v;
1457
            int pos= get_bits_count(&s->gb);
1458

    
1459
            if (pos >= end_pos){
1460
//                av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1461
                switch_buffer(s, &pos, &end_pos, &end_pos2);
1462
//                av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1463
                if(pos >= end_pos)
1464
                    break;
1465
            }
1466
            y = get_vlc2(&s->gb, vlc->table, 7, 3);
1467

    
1468
            if(!y){
1469
                g->sb_hybrid[s_index  ] =
1470
                g->sb_hybrid[s_index+1] = 0;
1471
                s_index += 2;
1472
                continue;
1473
            }
1474

    
1475
            exponent= exponents[s_index];
1476

    
1477
            dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1478
                    i, g->region_size[i] - j, x, y, exponent);
1479
            if(y&16){
1480
                x = y >> 5;
1481
                y = y & 0x0f;
1482
                if (x < 15){
1483
                    READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
1484
                }else{
1485
                    x += get_bitsz(&s->gb, linbits);
1486
                    v = l3_unscale(x, exponent);
1487
                    if (get_bits1(&s->gb))
1488
                        v = -v;
1489
                    g->sb_hybrid[s_index] = v;
1490
                }
1491
                if (y < 15){
1492
                    READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
1493
                }else{
1494
                    y += get_bitsz(&s->gb, linbits);
1495
                    v = l3_unscale(y, exponent);
1496
                    if (get_bits1(&s->gb))
1497
                        v = -v;
1498
                    g->sb_hybrid[s_index+1] = v;
1499
                }
1500
            }else{
1501
                x = y >> 5;
1502
                y = y & 0x0f;
1503
                x += y;
1504
                if (x < 15){
1505
                    READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
1506
                }else{
1507
                    x += get_bitsz(&s->gb, linbits);
1508
                    v = l3_unscale(x, exponent);
1509
                    if (get_bits1(&s->gb))
1510
                        v = -v;
1511
                    g->sb_hybrid[s_index+!!y] = v;
1512
                }
1513
                g->sb_hybrid[s_index+ !y] = 0;
1514
            }
1515
            s_index+=2;
1516
        }
1517
    }
1518

    
1519
    /* high frequencies */
1520
    vlc = &huff_quad_vlc[g->count1table_select];
1521
    last_pos=0;
1522
    while (s_index <= 572) {
1523
        int pos, code;
1524
        pos = get_bits_count(&s->gb);
1525
        if (pos >= end_pos) {
1526
            if (pos > end_pos2 && last_pos){
1527
                /* some encoders generate an incorrect size for this
1528
                   part. We must go back into the data */
1529
                s_index -= 4;
1530
                skip_bits_long(&s->gb, last_pos - pos);
1531
                av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1532
                if(s->error_recognition >= FF_ER_COMPLIANT)
1533
                    s_index=0;
1534
                break;
1535
            }
1536
//                av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1537
            switch_buffer(s, &pos, &end_pos, &end_pos2);
1538
//                av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1539
            if(pos >= end_pos)
1540
                break;
1541
        }
1542
        last_pos= pos;
1543

    
1544
        code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1545
        dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1546
        g->sb_hybrid[s_index+0]=
1547
        g->sb_hybrid[s_index+1]=
1548
        g->sb_hybrid[s_index+2]=
1549
        g->sb_hybrid[s_index+3]= 0;
1550
        while(code){
1551
            static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1552
            int v;
1553
            int pos= s_index+idxtab[code];
1554
            code ^= 8>>idxtab[code];
1555
            READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
1556
        }
1557
        s_index+=4;
1558
    }
1559
    /* skip extension bits */
1560
    bits_left = end_pos2 - get_bits_count(&s->gb);
1561
//av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1562
    if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1563
        av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1564
        s_index=0;
1565
    }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1566
        av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1567
        s_index=0;
1568
    }
1569
    memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1570
    skip_bits_long(&s->gb, bits_left);
1571

    
1572
    i= get_bits_count(&s->gb);
1573
    switch_buffer(s, &i, &end_pos, &end_pos2);
1574

    
1575
    return 0;
1576
}
1577

    
1578
/* Reorder short blocks from bitstream order to interleaved order. It
1579
   would be faster to do it in parsing, but the code would be far more
1580
   complicated */
1581
static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1582
{
1583
    int i, j, len;
1584
    INTFLOAT *ptr, *dst, *ptr1;
1585
    INTFLOAT tmp[576];
1586

    
1587
    if (g->block_type != 2)
1588
        return;
1589

    
1590
    if (g->switch_point) {
1591
        if (s->sample_rate_index != 8) {
1592
            ptr = g->sb_hybrid + 36;
1593
        } else {
1594
            ptr = g->sb_hybrid + 48;
1595
        }
1596
    } else {
1597
        ptr = g->sb_hybrid;
1598
    }
1599

    
1600
    for(i=g->short_start;i<13;i++) {
1601
        len = band_size_short[s->sample_rate_index][i];
1602
        ptr1 = ptr;
1603
        dst = tmp;
1604
        for(j=len;j>0;j--) {
1605
            *dst++ = ptr[0*len];
1606
            *dst++ = ptr[1*len];
1607
            *dst++ = ptr[2*len];
1608
            ptr++;
1609
        }
1610
        ptr+=2*len;
1611
        memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1612
    }
1613
}
1614

    
1615
#define ISQRT2 FIXR(0.70710678118654752440)
1616

    
1617
static void compute_stereo(MPADecodeContext *s,
1618
                           GranuleDef *g0, GranuleDef *g1)
1619
{
1620
    int i, j, k, l;
1621
    int sf_max, sf, len, non_zero_found;
1622
    INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1623
    int non_zero_found_short[3];
1624

    
1625
    /* intensity stereo */
1626
    if (s->mode_ext & MODE_EXT_I_STEREO) {
1627
        if (!s->lsf) {
1628
            is_tab = is_table;
1629
            sf_max = 7;
1630
        } else {
1631
            is_tab = is_table_lsf[g1->scalefac_compress & 1];
1632
            sf_max = 16;
1633
        }
1634

    
1635
        tab0 = g0->sb_hybrid + 576;
1636
        tab1 = g1->sb_hybrid + 576;
1637

    
1638
        non_zero_found_short[0] = 0;
1639
        non_zero_found_short[1] = 0;
1640
        non_zero_found_short[2] = 0;
1641
        k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1642
        for(i = 12;i >= g1->short_start;i--) {
1643
            /* for last band, use previous scale factor */
1644
            if (i != 11)
1645
                k -= 3;
1646
            len = band_size_short[s->sample_rate_index][i];
1647
            for(l=2;l>=0;l--) {
1648
                tab0 -= len;
1649
                tab1 -= len;
1650
                if (!non_zero_found_short[l]) {
1651
                    /* test if non zero band. if so, stop doing i-stereo */
1652
                    for(j=0;j<len;j++) {
1653
                        if (tab1[j] != 0) {
1654
                            non_zero_found_short[l] = 1;
1655
                            goto found1;
1656
                        }
1657
                    }
1658
                    sf = g1->scale_factors[k + l];
1659
                    if (sf >= sf_max)
1660
                        goto found1;
1661

    
1662
                    v1 = is_tab[0][sf];
1663
                    v2 = is_tab[1][sf];
1664
                    for(j=0;j<len;j++) {
1665
                        tmp0 = tab0[j];
1666
                        tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1667
                        tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1668
                    }
1669
                } else {
1670
                found1:
1671
                    if (s->mode_ext & MODE_EXT_MS_STEREO) {
1672
                        /* lower part of the spectrum : do ms stereo
1673
                           if enabled */
1674
                        for(j=0;j<len;j++) {
1675
                            tmp0 = tab0[j];
1676
                            tmp1 = tab1[j];
1677
                            tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1678
                            tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1679
                        }
1680
                    }
1681
                }
1682
            }
1683
        }
1684

    
1685
        non_zero_found = non_zero_found_short[0] |
1686
            non_zero_found_short[1] |
1687
            non_zero_found_short[2];
1688

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

    
1742
static void compute_antialias_integer(MPADecodeContext *s,
1743
                              GranuleDef *g)
1744
{
1745
    int32_t *ptr, *csa;
1746
    int n, i;
1747

    
1748
    /* we antialias only "long" bands */
1749
    if (g->block_type == 2) {
1750
        if (!g->switch_point)
1751
            return;
1752
        /* XXX: check this for 8000Hz case */
1753
        n = 1;
1754
    } else {
1755
        n = SBLIMIT - 1;
1756
    }
1757

    
1758
    ptr = g->sb_hybrid + 18;
1759
    for(i = n;i > 0;i--) {
1760
        int tmp0, tmp1, tmp2;
1761
        csa = &csa_table[0][0];
1762
#define INT_AA(j) \
1763
            tmp0 = ptr[-1-j];\
1764
            tmp1 = ptr[   j];\
1765
            tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1766
            ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1767
            ptr[   j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1768

    
1769
        INT_AA(0)
1770
        INT_AA(1)
1771
        INT_AA(2)
1772
        INT_AA(3)
1773
        INT_AA(4)
1774
        INT_AA(5)
1775
        INT_AA(6)
1776
        INT_AA(7)
1777

    
1778
        ptr += 18;
1779
    }
1780
}
1781

    
1782
static void compute_antialias_float(MPADecodeContext *s,
1783
                              GranuleDef *g)
1784
{
1785
    float *ptr;
1786
    int n, i;
1787

    
1788
    /* we antialias only "long" bands */
1789
    if (g->block_type == 2) {
1790
        if (!g->switch_point)
1791
            return;
1792
        /* XXX: check this for 8000Hz case */
1793
        n = 1;
1794
    } else {
1795
        n = SBLIMIT - 1;
1796
    }
1797

    
1798
    ptr = g->sb_hybrid + 18;
1799
    for(i = n;i > 0;i--) {
1800
        float tmp0, tmp1;
1801
        float *csa = &csa_table_float[0][0];
1802
#define FLOAT_AA(j)\
1803
        tmp0= ptr[-1-j];\
1804
        tmp1= ptr[   j];\
1805
        ptr[-1-j] = tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j];\
1806
        ptr[   j] = tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j];
1807

    
1808
        FLOAT_AA(0)
1809
        FLOAT_AA(1)
1810
        FLOAT_AA(2)
1811
        FLOAT_AA(3)
1812
        FLOAT_AA(4)
1813
        FLOAT_AA(5)
1814
        FLOAT_AA(6)
1815
        FLOAT_AA(7)
1816

    
1817
        ptr += 18;
1818
    }
1819
}
1820

    
1821
static void compute_imdct(MPADecodeContext *s,
1822
                          GranuleDef *g,
1823
                          INTFLOAT *sb_samples,
1824
                          INTFLOAT *mdct_buf)
1825
{
1826
    INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1827
    INTFLOAT out2[12];
1828
    int i, j, mdct_long_end, sblimit;
1829

    
1830
    /* find last non zero block */
1831
    ptr = g->sb_hybrid + 576;
1832
    ptr1 = g->sb_hybrid + 2 * 18;
1833
    while (ptr >= ptr1) {
1834
        int32_t *p;
1835
        ptr -= 6;
1836
        p= (int32_t*)ptr;
1837
        if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1838
            break;
1839
    }
1840
    sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1841

    
1842
    if (g->block_type == 2) {
1843
        /* XXX: check for 8000 Hz */
1844
        if (g->switch_point)
1845
            mdct_long_end = 2;
1846
        else
1847
            mdct_long_end = 0;
1848
    } else {
1849
        mdct_long_end = sblimit;
1850
    }
1851

    
1852
    buf = mdct_buf;
1853
    ptr = g->sb_hybrid;
1854
    for(j=0;j<mdct_long_end;j++) {
1855
        /* apply window & overlap with previous buffer */
1856
        out_ptr = sb_samples + j;
1857
        /* select window */
1858
        if (g->switch_point && j < 2)
1859
            win1 = mdct_win[0];
1860
        else
1861
            win1 = mdct_win[g->block_type];
1862
        /* select frequency inversion */
1863
        win = win1 + ((4 * 36) & -(j & 1));
1864
        imdct36(out_ptr, buf, ptr, win);
1865
        out_ptr += 18*SBLIMIT;
1866
        ptr += 18;
1867
        buf += 18;
1868
    }
1869
    for(j=mdct_long_end;j<sblimit;j++) {
1870
        /* select frequency inversion */
1871
        win = mdct_win[2] + ((4 * 36) & -(j & 1));
1872
        out_ptr = sb_samples + j;
1873

    
1874
        for(i=0; i<6; i++){
1875
            *out_ptr = buf[i];
1876
            out_ptr += SBLIMIT;
1877
        }
1878
        imdct12(out2, ptr + 0);
1879
        for(i=0;i<6;i++) {
1880
            *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*1];
1881
            buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1882
            out_ptr += SBLIMIT;
1883
        }
1884
        imdct12(out2, ptr + 1);
1885
        for(i=0;i<6;i++) {
1886
            *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*2];
1887
            buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1888
            out_ptr += SBLIMIT;
1889
        }
1890
        imdct12(out2, ptr + 2);
1891
        for(i=0;i<6;i++) {
1892
            buf[i + 6*0] = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*0];
1893
            buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1894
            buf[i + 6*2] = 0;
1895
        }
1896
        ptr += 18;
1897
        buf += 18;
1898
    }
1899
    /* zero bands */
1900
    for(j=sblimit;j<SBLIMIT;j++) {
1901
        /* overlap */
1902
        out_ptr = sb_samples + j;
1903
        for(i=0;i<18;i++) {
1904
            *out_ptr = buf[i];
1905
            buf[i] = 0;
1906
            out_ptr += SBLIMIT;
1907
        }
1908
        buf += 18;
1909
    }
1910
}
1911

    
1912
/* main layer3 decoding function */
1913
static int mp_decode_layer3(MPADecodeContext *s)
1914
{
1915
    int nb_granules, main_data_begin, private_bits;
1916
    int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1917
    GranuleDef *g;
1918
    int16_t exponents[576]; //FIXME try INTFLOAT
1919

    
1920
    /* read side info */
1921
    if (s->lsf) {
1922
        main_data_begin = get_bits(&s->gb, 8);
1923
        private_bits = get_bits(&s->gb, s->nb_channels);
1924
        nb_granules = 1;
1925
    } else {
1926
        main_data_begin = get_bits(&s->gb, 9);
1927
        if (s->nb_channels == 2)
1928
            private_bits = get_bits(&s->gb, 3);
1929
        else
1930
            private_bits = get_bits(&s->gb, 5);
1931
        nb_granules = 2;
1932
        for(ch=0;ch<s->nb_channels;ch++) {
1933
            s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1934
            s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1935
        }
1936
    }
1937

    
1938
    for(gr=0;gr<nb_granules;gr++) {
1939
        for(ch=0;ch<s->nb_channels;ch++) {
1940
            dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1941
            g = &s->granules[ch][gr];
1942
            g->part2_3_length = get_bits(&s->gb, 12);
1943
            g->big_values = get_bits(&s->gb, 9);
1944
            if(g->big_values > 288){
1945
                av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1946
                return -1;
1947
            }
1948

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

    
1988
            g->preflag = 0;
1989
            if (!s->lsf)
1990
                g->preflag = get_bits1(&s->gb);
1991
            g->scalefac_scale = get_bits1(&s->gb);
1992
            g->count1table_select = get_bits1(&s->gb);
1993
            dprintf(s->avctx, "block_type=%d switch_point=%d\n",
1994
                    g->block_type, g->switch_point);
1995
        }
1996
    }
1997

    
1998
  if (!s->adu_mode) {
1999
    const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2000
    assert((get_bits_count(&s->gb) & 7) == 0);
2001
    /* now we get bits from the main_data_begin offset */
2002
    dprintf(s->avctx, "seekback: %d\n", main_data_begin);
2003
//av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2004

    
2005
    memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2006
    s->in_gb= s->gb;
2007
        init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
2008
        skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
2009
  }
2010

    
2011
    for(gr=0;gr<nb_granules;gr++) {
2012
        for(ch=0;ch<s->nb_channels;ch++) {
2013
            g = &s->granules[ch][gr];
2014
            if(get_bits_count(&s->gb)<0){
2015
                av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
2016
                                            main_data_begin, s->last_buf_size, gr);
2017
                skip_bits_long(&s->gb, g->part2_3_length);
2018
                memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
2019
                if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
2020
                    skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
2021
                    s->gb= s->in_gb;
2022
                    s->in_gb.buffer=NULL;
2023
                }
2024
                continue;
2025
            }
2026

    
2027
            bits_pos = get_bits_count(&s->gb);
2028

    
2029
            if (!s->lsf) {
2030
                uint8_t *sc;
2031
                int slen, slen1, slen2;
2032

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

    
2083
                /* LSF scale factors */
2084
                if (g->block_type == 2) {
2085
                    tindex = g->switch_point ? 2 : 1;
2086
                } else {
2087
                    tindex = 0;
2088
                }
2089
                sf = g->scalefac_compress;
2090
                if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2091
                    /* intensity stereo case */
2092
                    sf >>= 1;
2093
                    if (sf < 180) {
2094
                        lsf_sf_expand(slen, sf, 6, 6, 0);
2095
                        tindex2 = 3;
2096
                    } else if (sf < 244) {
2097
                        lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2098
                        tindex2 = 4;
2099
                    } else {
2100
                        lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2101
                        tindex2 = 5;
2102
                    }
2103
                } else {
2104
                    /* normal case */
2105
                    if (sf < 400) {
2106
                        lsf_sf_expand(slen, sf, 5, 4, 4);
2107
                        tindex2 = 0;
2108
                    } else if (sf < 500) {
2109
                        lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2110
                        tindex2 = 1;
2111
                    } else {
2112
                        lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2113
                        tindex2 = 2;
2114
                        g->preflag = 1;
2115
                    }
2116
                }
2117

    
2118
                j = 0;
2119
                for(k=0;k<4;k++) {
2120
                    n = lsf_nsf_table[tindex2][tindex][k];
2121
                    sl = slen[k];
2122
                    if(sl){
2123
                        for(i=0;i<n;i++)
2124
                            g->scale_factors[j++] = get_bits(&s->gb, sl);
2125
                    }else{
2126
                        for(i=0;i<n;i++)
2127
                            g->scale_factors[j++] = 0;
2128
                    }
2129
                }
2130
                /* XXX: should compute exact size */
2131
                for(;j<40;j++)
2132
                    g->scale_factors[j] = 0;
2133
            }
2134

    
2135
            exponents_from_scale_factors(s, g, exponents);
2136

    
2137
            /* read Huffman coded residue */
2138
            huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
2139
        } /* ch */
2140

    
2141
        if (s->nb_channels == 2)
2142
            compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
2143

    
2144
        for(ch=0;ch<s->nb_channels;ch++) {
2145
            g = &s->granules[ch][gr];
2146

    
2147
            reorder_block(s, g);
2148
            compute_antialias(s, g);
2149
            compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2150
        }
2151
    } /* gr */
2152
    if(get_bits_count(&s->gb)<0)
2153
        skip_bits_long(&s->gb, -get_bits_count(&s->gb));
2154
    return nb_granules * 18;
2155
}
2156

    
2157
static int mp_decode_frame(MPADecodeContext *s,
2158
                           OUT_INT *samples, const uint8_t *buf, int buf_size)
2159
{
2160
    int i, nb_frames, ch;
2161
    OUT_INT *samples_ptr;
2162

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

    
2165
    /* skip error protection field */
2166
    if (s->error_protection)
2167
        skip_bits(&s->gb, 16);
2168

    
2169
    dprintf(s->avctx, "frame %d:\n", s->frame_count);
2170
    switch(s->layer) {
2171
    case 1:
2172
        s->avctx->frame_size = 384;
2173
        nb_frames = mp_decode_layer1(s);
2174
        break;
2175
    case 2:
2176
        s->avctx->frame_size = 1152;
2177
        nb_frames = mp_decode_layer2(s);
2178
        break;
2179
    case 3:
2180
        s->avctx->frame_size = s->lsf ? 576 : 1152;
2181
    default:
2182
        nb_frames = mp_decode_layer3(s);
2183

    
2184
        s->last_buf_size=0;
2185
        if(s->in_gb.buffer){
2186
            align_get_bits(&s->gb);
2187
            i= get_bits_left(&s->gb)>>3;
2188
            if(i >= 0 && i <= BACKSTEP_SIZE){
2189
                memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2190
                s->last_buf_size=i;
2191
            }else
2192
                av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2193
            s->gb= s->in_gb;
2194
            s->in_gb.buffer= NULL;
2195
        }
2196

    
2197
        align_get_bits(&s->gb);
2198
        assert((get_bits_count(&s->gb) & 7) == 0);
2199
        i= get_bits_left(&s->gb)>>3;
2200

    
2201
        if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2202
            if(i<0)
2203
                av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2204
            i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2205
        }
2206
        assert(i <= buf_size - HEADER_SIZE && i>= 0);
2207
        memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2208
        s->last_buf_size += i;
2209

    
2210
        break;
2211
    }
2212

    
2213
    /* apply the synthesis filter */
2214
    for(ch=0;ch<s->nb_channels;ch++) {
2215
        samples_ptr = samples + ch;
2216
        for(i=0;i<nb_frames;i++) {
2217
            RENAME(ff_mpa_synth_filter)(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2218
                         RENAME(ff_mpa_synth_window), &s->dither_state,
2219
                         samples_ptr, s->nb_channels,
2220
                         s->sb_samples[ch][i]);
2221
            samples_ptr += 32 * s->nb_channels;
2222
        }
2223
    }
2224

    
2225
    return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2226
}
2227

    
2228
static int decode_frame(AVCodecContext * avctx,
2229
                        void *data, int *data_size,
2230
                        AVPacket *avpkt)
2231
{
2232
    const uint8_t *buf = avpkt->data;
2233
    int buf_size = avpkt->size;
2234
    MPADecodeContext *s = avctx->priv_data;
2235
    uint32_t header;
2236
    int out_size;
2237
    OUT_INT *out_samples = data;
2238

    
2239
    if(buf_size < HEADER_SIZE)
2240
        return -1;
2241

    
2242
    header = AV_RB32(buf);
2243
    if(ff_mpa_check_header(header) < 0){
2244
        av_log(avctx, AV_LOG_ERROR, "Header missing\n");
2245
        return -1;
2246
    }
2247

    
2248
    if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
2249
        /* free format: prepare to compute frame size */
2250
        s->frame_size = -1;
2251
        return -1;
2252
    }
2253
    /* update codec info */
2254
    avctx->channels = s->nb_channels;
2255
    avctx->bit_rate = s->bit_rate;
2256
    avctx->sub_id = s->layer;
2257

    
2258
    if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
2259
        return -1;
2260
    *data_size = 0;
2261

    
2262
    if(s->frame_size<=0 || s->frame_size > buf_size){
2263
        av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2264
        return -1;
2265
    }else if(s->frame_size < buf_size){
2266
        av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2267
        buf_size= s->frame_size;
2268
    }
2269

    
2270
    out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2271
    if(out_size>=0){
2272
        *data_size = out_size;
2273
        avctx->sample_rate = s->sample_rate;
2274
        //FIXME maybe move the other codec info stuff from above here too
2275
    }else
2276
        av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2277
    s->frame_size = 0;
2278
    return buf_size;
2279
}
2280

    
2281
static void flush(AVCodecContext *avctx){
2282
    MPADecodeContext *s = avctx->priv_data;
2283
    memset(s->synth_buf, 0, sizeof(s->synth_buf));
2284
    s->last_buf_size= 0;
2285
}
2286

    
2287
#if CONFIG_MP3ADU_DECODER
2288
static int decode_frame_adu(AVCodecContext * avctx,
2289
                        void *data, int *data_size,
2290
                        AVPacket *avpkt)
2291
{
2292
    const uint8_t *buf = avpkt->data;
2293
    int buf_size = avpkt->size;
2294
    MPADecodeContext *s = avctx->priv_data;
2295
    uint32_t header;
2296
    int len, out_size;
2297
    OUT_INT *out_samples = data;
2298

    
2299
    len = buf_size;
2300

    
2301
    // Discard too short frames
2302
    if (buf_size < HEADER_SIZE) {
2303
        *data_size = 0;
2304
        return buf_size;
2305
    }
2306

    
2307

    
2308
    if (len > MPA_MAX_CODED_FRAME_SIZE)
2309
        len = MPA_MAX_CODED_FRAME_SIZE;
2310

    
2311
    // Get header and restore sync word
2312
    header = AV_RB32(buf) | 0xffe00000;
2313

    
2314
    if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2315
        *data_size = 0;
2316
        return buf_size;
2317
    }
2318

    
2319
    ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2320
    /* update codec info */
2321
    avctx->sample_rate = s->sample_rate;
2322
    avctx->channels = s->nb_channels;
2323
    avctx->bit_rate = s->bit_rate;
2324
    avctx->sub_id = s->layer;
2325

    
2326
    s->frame_size = len;
2327

    
2328
    if (avctx->parse_only) {
2329
        out_size = buf_size;
2330
    } else {
2331
        out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2332
    }
2333

    
2334
    *data_size = out_size;
2335
    return buf_size;
2336
}
2337
#endif /* CONFIG_MP3ADU_DECODER */
2338

    
2339
#if CONFIG_MP3ON4_DECODER
2340

    
2341
/**
2342
 * Context for MP3On4 decoder
2343
 */
2344
typedef struct MP3On4DecodeContext {
2345
    int frames;   ///< number of mp3 frames per block (number of mp3 decoder instances)
2346
    int syncword; ///< syncword patch
2347
    const uint8_t *coff; ///< channels offsets in output buffer
2348
    MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2349
} MP3On4DecodeContext;
2350

    
2351
#include "mpeg4audio.h"
2352

    
2353
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2354
static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5};   /* number of mp3 decoder instances */
2355
/* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2356
static const uint8_t chan_offset[8][5] = {
2357
    {0},
2358
    {0},            // C
2359
    {0},            // FLR
2360
    {2,0},          // C FLR
2361
    {2,0,3},        // C FLR BS
2362
    {4,0,2},        // C FLR BLRS
2363
    {4,0,2,5},      // C FLR BLRS LFE
2364
    {4,0,2,6,5},    // C FLR BLRS BLR LFE
2365
};
2366

    
2367

    
2368
static int decode_init_mp3on4(AVCodecContext * avctx)
2369
{
2370
    MP3On4DecodeContext *s = avctx->priv_data;
2371
    MPEG4AudioConfig cfg;
2372
    int i;
2373

    
2374
    if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2375
        av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2376
        return -1;
2377
    }
2378

    
2379
    ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2380
    if (!cfg.chan_config || cfg.chan_config > 7) {
2381
        av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2382
        return -1;
2383
    }
2384
    s->frames = mp3Frames[cfg.chan_config];
2385
    s->coff = chan_offset[cfg.chan_config];
2386
    avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2387

    
2388
    if (cfg.sample_rate < 16000)
2389
        s->syncword = 0xffe00000;
2390
    else
2391
        s->syncword = 0xfff00000;
2392

    
2393
    /* Init the first mp3 decoder in standard way, so that all tables get builded
2394
     * We replace avctx->priv_data with the context of the first decoder so that
2395
     * decode_init() does not have to be changed.
2396
     * Other decoders will be initialized here copying data from the first context
2397
     */
2398
    // Allocate zeroed memory for the first decoder context
2399
    s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2400
    // Put decoder context in place to make init_decode() happy
2401
    avctx->priv_data = s->mp3decctx[0];
2402
    decode_init(avctx);
2403
    // Restore mp3on4 context pointer
2404
    avctx->priv_data = s;
2405
    s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2406

    
2407
    /* Create a separate codec/context for each frame (first is already ok).
2408
     * Each frame is 1 or 2 channels - up to 5 frames allowed
2409
     */
2410
    for (i = 1; i < s->frames; i++) {
2411
        s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2412
        s->mp3decctx[i]->adu_mode = 1;
2413
        s->mp3decctx[i]->avctx = avctx;
2414
    }
2415

    
2416
    return 0;
2417
}
2418

    
2419

    
2420
static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
2421
{
2422
    MP3On4DecodeContext *s = avctx->priv_data;
2423
    int i;
2424

    
2425
    for (i = 0; i < s->frames; i++)
2426
        if (s->mp3decctx[i])
2427
            av_free(s->mp3decctx[i]);
2428

    
2429
    return 0;
2430
}
2431

    
2432

    
2433
static int decode_frame_mp3on4(AVCodecContext * avctx,
2434
                        void *data, int *data_size,
2435
                        AVPacket *avpkt)
2436
{
2437
    const uint8_t *buf = avpkt->data;
2438
    int buf_size = avpkt->size;
2439
    MP3On4DecodeContext *s = avctx->priv_data;
2440
    MPADecodeContext *m;
2441
    int fsize, len = buf_size, out_size = 0;
2442
    uint32_t header;
2443
    OUT_INT *out_samples = data;
2444
    OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2445
    OUT_INT *outptr, *bp;
2446
    int fr, j, n;
2447

    
2448
    if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2449
        return -1;
2450

    
2451
    *data_size = 0;
2452
    // Discard too short frames
2453
    if (buf_size < HEADER_SIZE)
2454
        return -1;
2455

    
2456
    // If only one decoder interleave is not needed
2457
    outptr = s->frames == 1 ? out_samples : decoded_buf;
2458

    
2459
    avctx->bit_rate = 0;
2460

    
2461
    for (fr = 0; fr < s->frames; fr++) {
2462
        fsize = AV_RB16(buf) >> 4;
2463
        fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2464
        m = s->mp3decctx[fr];
2465
        assert (m != NULL);
2466

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

    
2469
        if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2470
            break;
2471

    
2472
        ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2473
        out_size += mp_decode_frame(m, outptr, buf, fsize);
2474
        buf += fsize;
2475
        len -= fsize;
2476

    
2477
        if(s->frames > 1) {
2478
            n = m->avctx->frame_size*m->nb_channels;
2479
            /* interleave output data */
2480
            bp = out_samples + s->coff[fr];
2481
            if(m->nb_channels == 1) {
2482
                for(j = 0; j < n; j++) {
2483
                    *bp = decoded_buf[j];
2484
                    bp += avctx->channels;
2485
                }
2486
            } else {
2487
                for(j = 0; j < n; j++) {
2488
                    bp[0] = decoded_buf[j++];
2489
                    bp[1] = decoded_buf[j];
2490
                    bp += avctx->channels;
2491
                }
2492
            }
2493
        }
2494
        avctx->bit_rate += m->bit_rate;
2495
    }
2496

    
2497
    /* update codec info */
2498
    avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2499

    
2500
    *data_size = out_size;
2501
    return buf_size;
2502
}
2503
#endif /* CONFIG_MP3ON4_DECODER */
2504

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