Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudiodec.c @ 6bb6fb05

History | View | Annotate | Download (72.1 KB)

1
/*
2
 * MPEG Audio decoder
3
 * Copyright (c) 2001, 2002 Fabrice Bellard
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file
24
 * MPEG Audio decoder.
25
 */
26

    
27
#include "libavutil/audioconvert.h"
28
#include "avcodec.h"
29
#include "get_bits.h"
30
#include "dsputil.h"
31

    
32
/*
33
 * TODO:
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
#if CONFIG_FLOAT
71
#    include "fft.h"
72
#else
73
#    include "dct32.c"
74
#endif
75

    
76
static void compute_antialias(MPADecodeContext *s, GranuleDef *g);
77
static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
78
                               int *dither_state, OUT_INT *samples, int incr);
79

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

    
105
static int16_t division_tab3[1<<6 ];
106
static int16_t division_tab5[1<<8 ];
107
static int16_t division_tab9[1<<11];
108

    
109
static int16_t * const division_tabs[4] = {
110
    division_tab3, division_tab5, NULL, division_tab9
111
};
112

    
113
/* lower 2 bits: modulo 3, higher bits: shift */
114
static uint16_t scale_factor_modshift[64];
115
/* [i][j]:  2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
116
static int32_t scale_factor_mult[15][3];
117
/* mult table for layer 2 group quantization */
118

    
119
#define SCALE_GEN(v) \
120
{ FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
121

    
122
static const int32_t scale_factor_mult2[3][3] = {
123
    SCALE_GEN(4.0 / 3.0), /* 3 steps */
124
    SCALE_GEN(4.0 / 5.0), /* 5 steps */
125
    SCALE_GEN(4.0 / 9.0), /* 9 steps */
126
};
127

    
128
DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
129

    
130
/**
131
 * Convert region offsets to region sizes and truncate
132
 * size to big_values.
133
 */
134
static void ff_region_offset2size(GranuleDef *g){
135
    int i, k, j=0;
136
    g->region_size[2] = (576 / 2);
137
    for(i=0;i<3;i++) {
138
        k = FFMIN(g->region_size[i], g->big_values);
139
        g->region_size[i] = k - j;
140
        j = k;
141
    }
142
}
143

    
144
static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
145
    if (g->block_type == 2)
146
        g->region_size[0] = (36 / 2);
147
    else {
148
        if (s->sample_rate_index <= 2)
149
            g->region_size[0] = (36 / 2);
150
        else if (s->sample_rate_index != 8)
151
            g->region_size[0] = (54 / 2);
152
        else
153
            g->region_size[0] = (108 / 2);
154
    }
155
    g->region_size[1] = (576 / 2);
156
}
157

    
158
static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
159
    int l;
160
    g->region_size[0] =
161
        band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
162
    /* should not overflow */
163
    l = FFMIN(ra1 + ra2 + 2, 22);
164
    g->region_size[1] =
165
        band_index_long[s->sample_rate_index][l] >> 1;
166
}
167

    
168
static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
169
    if (g->block_type == 2) {
170
        if (g->switch_point) {
171
            /* if switched mode, we handle the 36 first samples as
172
                long blocks.  For 8000Hz, we handle the 48 first
173
                exponents as long blocks (XXX: check this!) */
174
            if (s->sample_rate_index <= 2)
175
                g->long_end = 8;
176
            else if (s->sample_rate_index != 8)
177
                g->long_end = 6;
178
            else
179
                g->long_end = 4; /* 8000 Hz */
180

    
181
            g->short_start = 2 + (s->sample_rate_index != 8);
182
        } else {
183
            g->long_end = 0;
184
            g->short_start = 0;
185
        }
186
    } else {
187
        g->short_start = 13;
188
        g->long_end = 22;
189
    }
190
}
191

    
192
/* layer 1 unscaling */
193
/* n = number of bits of the mantissa minus 1 */
194
static inline int l1_unscale(int n, int mant, int scale_factor)
195
{
196
    int shift, mod;
197
    int64_t val;
198

    
199
    shift = scale_factor_modshift[scale_factor];
200
    mod = shift & 3;
201
    shift >>= 2;
202
    val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
203
    shift += n;
204
    /* NOTE: at this point, 1 <= shift >= 21 + 15 */
205
    return (int)((val + (1LL << (shift - 1))) >> shift);
206
}
207

    
208
static inline int l2_unscale_group(int steps, int mant, int scale_factor)
209
{
210
    int shift, mod, val;
211

    
212
    shift = scale_factor_modshift[scale_factor];
213
    mod = shift & 3;
214
    shift >>= 2;
215

    
216
    val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
217
    /* NOTE: at this point, 0 <= shift <= 21 */
218
    if (shift > 0)
219
        val = (val + (1 << (shift - 1))) >> shift;
220
    return val;
221
}
222

    
223
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
224
static inline int l3_unscale(int value, int exponent)
225
{
226
    unsigned int m;
227
    int e;
228

    
229
    e = table_4_3_exp  [4*value + (exponent&3)];
230
    m = table_4_3_value[4*value + (exponent&3)];
231
    e -= (exponent >> 2);
232
    assert(e>=1);
233
    if (e > 31)
234
        return 0;
235
    m = (m + (1 << (e-1))) >> e;
236

    
237
    return m;
238
}
239

    
240
/* all integer n^(4/3) computation code */
241
#define DEV_ORDER 13
242

    
243
#define POW_FRAC_BITS 24
244
#define POW_FRAC_ONE    (1 << POW_FRAC_BITS)
245
#define POW_FIX(a)   ((int)((a) * POW_FRAC_ONE))
246
#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
247

    
248
static int dev_4_3_coefs[DEV_ORDER];
249

    
250
#if 0 /* unused */
251
static int pow_mult3[3] = {
252
    POW_FIX(1.0),
253
    POW_FIX(1.25992104989487316476),
254
    POW_FIX(1.58740105196819947474),
255
};
256
#endif
257

    
258
static av_cold void int_pow_init(void)
259
{
260
    int i, a;
261

    
262
    a = POW_FIX(1.0);
263
    for(i=0;i<DEV_ORDER;i++) {
264
        a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
265
        dev_4_3_coefs[i] = a;
266
    }
267
}
268

    
269
#if 0 /* unused, remove? */
270
/* return the mantissa and the binary exponent */
271
static int int_pow(int i, int *exp_ptr)
272
{
273
    int e, er, eq, j;
274
    int a, a1;
275

276
    /* renormalize */
277
    a = i;
278
    e = POW_FRAC_BITS;
279
    while (a < (1 << (POW_FRAC_BITS - 1))) {
280
        a = a << 1;
281
        e--;
282
    }
283
    a -= (1 << POW_FRAC_BITS);
284
    a1 = 0;
285
    for(j = DEV_ORDER - 1; j >= 0; j--)
286
        a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
287
    a = (1 << POW_FRAC_BITS) + a1;
288
    /* exponent compute (exact) */
289
    e = e * 4;
290
    er = e % 3;
291
    eq = e / 3;
292
    a = POW_MULL(a, pow_mult3[er]);
293
    while (a >= 2 * POW_FRAC_ONE) {
294
        a = a >> 1;
295
        eq++;
296
    }
297
    /* convert to float */
298
    while (a < POW_FRAC_ONE) {
299
        a = a << 1;
300
        eq--;
301
    }
302
    /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
303
#if POW_FRAC_BITS > FRAC_BITS
304
    a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
305
    /* correct overflow */
306
    if (a >= 2 * (1 << FRAC_BITS)) {
307
        a = a >> 1;
308
        eq++;
309
    }
310
#endif
311
    *exp_ptr = eq;
312
    return a;
313
}
314
#endif
315

    
316
static av_cold int decode_init(AVCodecContext * avctx)
317
{
318
    MPADecodeContext *s = avctx->priv_data;
319
    static int init=0;
320
    int i, j, k;
321

    
322
    s->avctx = avctx;
323
    s->apply_window_mp3 = apply_window_mp3_c;
324
#if HAVE_MMX && CONFIG_FLOAT
325
    ff_mpegaudiodec_init_mmx(s);
326
#endif
327
#if CONFIG_FLOAT
328
    ff_dct_init(&s->dct, 5, DCT_II);
329
#endif
330
    if (HAVE_ALTIVEC && CONFIG_FLOAT) ff_mpegaudiodec_init_altivec(s);
331

    
332
    avctx->sample_fmt= OUT_FMT;
333
    s->error_recognition= avctx->error_recognition;
334

    
335
    if (!init && !avctx->parse_only) {
336
        int offset;
337

    
338
        /* scale factors table for layer 1/2 */
339
        for(i=0;i<64;i++) {
340
            int shift, mod;
341
            /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
342
            shift = (i / 3);
343
            mod = i % 3;
344
            scale_factor_modshift[i] = mod | (shift << 2);
345
        }
346

    
347
        /* scale factor multiply for layer 1 */
348
        for(i=0;i<15;i++) {
349
            int n, norm;
350
            n = i + 2;
351
            norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
352
            scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0          * 2.0), FRAC_BITS);
353
            scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
354
            scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
355
            av_dlog(avctx, "%d: norm=%x s=%x %x %x\n",
356
                    i, norm,
357
                    scale_factor_mult[i][0],
358
                    scale_factor_mult[i][1],
359
                    scale_factor_mult[i][2]);
360
        }
361

    
362
        RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
363

    
364
        /* huffman decode tables */
365
        offset = 0;
366
        for(i=1;i<16;i++) {
367
            const HuffTable *h = &mpa_huff_tables[i];
368
            int xsize, x, y;
369
            uint8_t  tmp_bits [512];
370
            uint16_t tmp_codes[512];
371

    
372
            memset(tmp_bits , 0, sizeof(tmp_bits ));
373
            memset(tmp_codes, 0, sizeof(tmp_codes));
374

    
375
            xsize = h->xsize;
376

    
377
            j = 0;
378
            for(x=0;x<xsize;x++) {
379
                for(y=0;y<xsize;y++){
380
                    tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j  ];
381
                    tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
382
                }
383
            }
384

    
385
            /* XXX: fail test */
386
            huff_vlc[i].table = huff_vlc_tables+offset;
387
            huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
388
            init_vlc(&huff_vlc[i], 7, 512,
389
                     tmp_bits, 1, 1, tmp_codes, 2, 2,
390
                     INIT_VLC_USE_NEW_STATIC);
391
            offset += huff_vlc_tables_sizes[i];
392
        }
393
        assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
394

    
395
        offset = 0;
396
        for(i=0;i<2;i++) {
397
            huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
398
            huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
399
            init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
400
                     mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
401
                     INIT_VLC_USE_NEW_STATIC);
402
            offset += huff_quad_vlc_tables_sizes[i];
403
        }
404
        assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
405

    
406
        for(i=0;i<9;i++) {
407
            k = 0;
408
            for(j=0;j<22;j++) {
409
                band_index_long[i][j] = k;
410
                k += band_size_long[i][j];
411
            }
412
            band_index_long[i][22] = k;
413
        }
414

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

    
417
        int_pow_init();
418
        mpegaudio_tableinit();
419

    
420
        for (i = 0; i < 4; i++)
421
            if (ff_mpa_quant_bits[i] < 0)
422
                for (j = 0; j < (1<<(-ff_mpa_quant_bits[i]+1)); j++) {
423
                    int val1, val2, val3, steps;
424
                    int val = j;
425
                    steps  = ff_mpa_quant_steps[i];
426
                    val1 = val % steps;
427
                    val /= steps;
428
                    val2 = val % steps;
429
                    val3 = val / steps;
430
                    division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
431
                }
432

    
433

    
434
        for(i=0;i<7;i++) {
435
            float f;
436
            INTFLOAT v;
437
            if (i != 6) {
438
                f = tan((double)i * M_PI / 12.0);
439
                v = FIXR(f / (1.0 + f));
440
            } else {
441
                v = FIXR(1.0);
442
            }
443
            is_table[0][i] = v;
444
            is_table[1][6 - i] = v;
445
        }
446
        /* invalid values */
447
        for(i=7;i<16;i++)
448
            is_table[0][i] = is_table[1][i] = 0.0;
449

    
450
        for(i=0;i<16;i++) {
451
            double f;
452
            int e, k;
453

    
454
            for(j=0;j<2;j++) {
455
                e = -(j + 1) * ((i + 1) >> 1);
456
                f = pow(2.0, e / 4.0);
457
                k = i & 1;
458
                is_table_lsf[j][k ^ 1][i] = FIXR(f);
459
                is_table_lsf[j][k][i] = FIXR(1.0);
460
                av_dlog(avctx, "is_table_lsf %d %d: %x %x\n",
461
                        i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
462
            }
463
        }
464

    
465
        for(i=0;i<8;i++) {
466
            float ci, cs, ca;
467
            ci = ci_table[i];
468
            cs = 1.0 / sqrt(1.0 + ci * ci);
469
            ca = cs * ci;
470
            csa_table[i][0] = FIXHR(cs/4);
471
            csa_table[i][1] = FIXHR(ca/4);
472
            csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
473
            csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
474
            csa_table_float[i][0] = cs;
475
            csa_table_float[i][1] = ca;
476
            csa_table_float[i][2] = ca + cs;
477
            csa_table_float[i][3] = ca - cs;
478
        }
479

    
480
        /* compute mdct windows */
481
        for(i=0;i<36;i++) {
482
            for(j=0; j<4; j++){
483
                double d;
484

    
485
                if(j==2 && i%3 != 1)
486
                    continue;
487

    
488
                d= sin(M_PI * (i + 0.5) / 36.0);
489
                if(j==1){
490
                    if     (i>=30) d= 0;
491
                    else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
492
                    else if(i>=18) d= 1;
493
                }else if(j==3){
494
                    if     (i<  6) d= 0;
495
                    else if(i< 12) d= sin(M_PI * (i -  6 + 0.5) / 12.0);
496
                    else if(i< 18) d= 1;
497
                }
498
                //merge last stage of imdct into the window coefficients
499
                d*= 0.5 / cos(M_PI*(2*i + 19)/72);
500

    
501
                if(j==2)
502
                    mdct_win[j][i/3] = FIXHR((d / (1<<5)));
503
                else
504
                    mdct_win[j][i  ] = FIXHR((d / (1<<5)));
505
            }
506
        }
507

    
508
        /* NOTE: we do frequency inversion adter the MDCT by changing
509
           the sign of the right window coefs */
510
        for(j=0;j<4;j++) {
511
            for(i=0;i<36;i+=2) {
512
                mdct_win[j + 4][i] = mdct_win[j][i];
513
                mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
514
            }
515
        }
516

    
517
        init = 1;
518
    }
519

    
520
    if (avctx->codec_id == CODEC_ID_MP3ADU)
521
        s->adu_mode = 1;
522
    return 0;
523
}
524

    
525

    
526
#if CONFIG_FLOAT
527
static inline float round_sample(float *sum)
528
{
529
    float sum1=*sum;
530
    *sum = 0;
531
    return sum1;
532
}
533

    
534
/* signed 16x16 -> 32 multiply add accumulate */
535
#define MACS(rt, ra, rb) rt+=(ra)*(rb)
536

    
537
/* signed 16x16 -> 32 multiply */
538
#define MULS(ra, rb) ((ra)*(rb))
539

    
540
#define MLSS(rt, ra, rb) rt-=(ra)*(rb)
541

    
542
#else
543

    
544
static inline int round_sample(int64_t *sum)
545
{
546
    int sum1;
547
    sum1 = (int)((*sum) >> OUT_SHIFT);
548
    *sum &= (1<<OUT_SHIFT)-1;
549
    return av_clip(sum1, OUT_MIN, OUT_MAX);
550
}
551

    
552
#   define MULS(ra, rb) MUL64(ra, rb)
553
#   define MACS(rt, ra, rb) MAC64(rt, ra, rb)
554
#   define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
555
#endif
556

    
557
#define SUM8(op, sum, w, p)               \
558
{                                         \
559
    op(sum, (w)[0 * 64], (p)[0 * 64]);    \
560
    op(sum, (w)[1 * 64], (p)[1 * 64]);    \
561
    op(sum, (w)[2 * 64], (p)[2 * 64]);    \
562
    op(sum, (w)[3 * 64], (p)[3 * 64]);    \
563
    op(sum, (w)[4 * 64], (p)[4 * 64]);    \
564
    op(sum, (w)[5 * 64], (p)[5 * 64]);    \
565
    op(sum, (w)[6 * 64], (p)[6 * 64]);    \
566
    op(sum, (w)[7 * 64], (p)[7 * 64]);    \
567
}
568

    
569
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
570
{                                               \
571
    INTFLOAT tmp;\
572
    tmp = p[0 * 64];\
573
    op1(sum1, (w1)[0 * 64], tmp);\
574
    op2(sum2, (w2)[0 * 64], tmp);\
575
    tmp = p[1 * 64];\
576
    op1(sum1, (w1)[1 * 64], tmp);\
577
    op2(sum2, (w2)[1 * 64], tmp);\
578
    tmp = p[2 * 64];\
579
    op1(sum1, (w1)[2 * 64], tmp);\
580
    op2(sum2, (w2)[2 * 64], tmp);\
581
    tmp = p[3 * 64];\
582
    op1(sum1, (w1)[3 * 64], tmp);\
583
    op2(sum2, (w2)[3 * 64], tmp);\
584
    tmp = p[4 * 64];\
585
    op1(sum1, (w1)[4 * 64], tmp);\
586
    op2(sum2, (w2)[4 * 64], tmp);\
587
    tmp = p[5 * 64];\
588
    op1(sum1, (w1)[5 * 64], tmp);\
589
    op2(sum2, (w2)[5 * 64], tmp);\
590
    tmp = p[6 * 64];\
591
    op1(sum1, (w1)[6 * 64], tmp);\
592
    op2(sum2, (w2)[6 * 64], tmp);\
593
    tmp = p[7 * 64];\
594
    op1(sum1, (w1)[7 * 64], tmp);\
595
    op2(sum2, (w2)[7 * 64], tmp);\
596
}
597

    
598
void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
599
{
600
    int i, j;
601

    
602
    /* max = 18760, max sum over all 16 coefs : 44736 */
603
    for(i=0;i<257;i++) {
604
        INTFLOAT v;
605
        v = ff_mpa_enwindow[i];
606
#if CONFIG_FLOAT
607
        v *= 1.0 / (1LL<<(16 + FRAC_BITS));
608
#endif
609
        window[i] = v;
610
        if ((i & 63) != 0)
611
            v = -v;
612
        if (i != 0)
613
            window[512 - i] = v;
614
    }
615

    
616
    // Needed for avoiding shuffles in ASM implementations
617
    for(i=0; i < 8; i++)
618
        for(j=0; j < 16; j++)
619
            window[512+16*i+j] = window[64*i+32-j];
620

    
621
    for(i=0; i < 8; i++)
622
        for(j=0; j < 16; j++)
623
            window[512+128+16*i+j] = window[64*i+48-j];
624
}
625

    
626
static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
627
                               int *dither_state, OUT_INT *samples, int incr)
628
{
629
    register const MPA_INT *w, *w2, *p;
630
    int j;
631
    OUT_INT *samples2;
632
#if CONFIG_FLOAT
633
    float sum, sum2;
634
#else
635
    int64_t sum, sum2;
636
#endif
637

    
638
    /* copy to avoid wrap */
639
    memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
640

    
641
    samples2 = samples + 31 * incr;
642
    w = window;
643
    w2 = window + 31;
644

    
645
    sum = *dither_state;
646
    p = synth_buf + 16;
647
    SUM8(MACS, sum, w, p);
648
    p = synth_buf + 48;
649
    SUM8(MLSS, sum, w + 32, p);
650
    *samples = round_sample(&sum);
651
    samples += incr;
652
    w++;
653

    
654
    /* we calculate two samples at the same time to avoid one memory
655
       access per two sample */
656
    for(j=1;j<16;j++) {
657
        sum2 = 0;
658
        p = synth_buf + 16 + j;
659
        SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
660
        p = synth_buf + 48 - j;
661
        SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
662

    
663
        *samples = round_sample(&sum);
664
        samples += incr;
665
        sum += sum2;
666
        *samples2 = round_sample(&sum);
667
        samples2 -= incr;
668
        w++;
669
        w2--;
670
    }
671

    
672
    p = synth_buf + 32;
673
    SUM8(MLSS, sum, w + 32, p);
674
    *samples = round_sample(&sum);
675
    *dither_state= sum;
676
}
677

    
678

    
679
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
680
   32 samples. */
681
/* XXX: optimize by avoiding ring buffer usage */
682
#if !CONFIG_FLOAT
683
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
684
                         MPA_INT *window, int *dither_state,
685
                         OUT_INT *samples, int incr,
686
                         INTFLOAT sb_samples[SBLIMIT])
687
{
688
    register MPA_INT *synth_buf;
689
    int offset;
690

    
691
    offset = *synth_buf_offset;
692
    synth_buf = synth_buf_ptr + offset;
693

    
694
    dct32(synth_buf, sb_samples);
695
    apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);
696

    
697
    offset = (offset - 32) & 511;
698
    *synth_buf_offset = offset;
699
}
700
#endif
701

    
702
#define C3 FIXHR(0.86602540378443864676/2)
703

    
704
/* 0.5 / cos(pi*(2*i+1)/36) */
705
static const INTFLOAT icos36[9] = {
706
    FIXR(0.50190991877167369479),
707
    FIXR(0.51763809020504152469), //0
708
    FIXR(0.55168895948124587824),
709
    FIXR(0.61038729438072803416),
710
    FIXR(0.70710678118654752439), //1
711
    FIXR(0.87172339781054900991),
712
    FIXR(1.18310079157624925896),
713
    FIXR(1.93185165257813657349), //2
714
    FIXR(5.73685662283492756461),
715
};
716

    
717
/* 0.5 / cos(pi*(2*i+1)/36) */
718
static const INTFLOAT icos36h[9] = {
719
    FIXHR(0.50190991877167369479/2),
720
    FIXHR(0.51763809020504152469/2), //0
721
    FIXHR(0.55168895948124587824/2),
722
    FIXHR(0.61038729438072803416/2),
723
    FIXHR(0.70710678118654752439/2), //1
724
    FIXHR(0.87172339781054900991/2),
725
    FIXHR(1.18310079157624925896/4),
726
    FIXHR(1.93185165257813657349/4), //2
727
//    FIXHR(5.73685662283492756461),
728
};
729

    
730
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
731
   cases. */
732
static void imdct12(INTFLOAT *out, INTFLOAT *in)
733
{
734
    INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
735

    
736
    in0= in[0*3];
737
    in1= in[1*3] + in[0*3];
738
    in2= in[2*3] + in[1*3];
739
    in3= in[3*3] + in[2*3];
740
    in4= in[4*3] + in[3*3];
741
    in5= in[5*3] + in[4*3];
742
    in5 += in3;
743
    in3 += in1;
744

    
745
    in2= MULH3(in2, C3, 2);
746
    in3= MULH3(in3, C3, 4);
747

    
748
    t1 = in0 - in4;
749
    t2 = MULH3(in1 - in5, icos36h[4], 2);
750

    
751
    out[ 7]=
752
    out[10]= t1 + t2;
753
    out[ 1]=
754
    out[ 4]= t1 - t2;
755

    
756
    in0 += SHR(in4, 1);
757
    in4 = in0 + in2;
758
    in5 += 2*in1;
759
    in1 = MULH3(in5 + in3, icos36h[1], 1);
760
    out[ 8]=
761
    out[ 9]= in4 + in1;
762
    out[ 2]=
763
    out[ 3]= in4 - in1;
764

    
765
    in0 -= in2;
766
    in5 = MULH3(in5 - in3, icos36h[7], 2);
767
    out[ 0]=
768
    out[ 5]= in0 - in5;
769
    out[ 6]=
770
    out[11]= in0 + in5;
771
}
772

    
773
/* cos(pi*i/18) */
774
#define C1 FIXHR(0.98480775301220805936/2)
775
#define C2 FIXHR(0.93969262078590838405/2)
776
#define C3 FIXHR(0.86602540378443864676/2)
777
#define C4 FIXHR(0.76604444311897803520/2)
778
#define C5 FIXHR(0.64278760968653932632/2)
779
#define C6 FIXHR(0.5/2)
780
#define C7 FIXHR(0.34202014332566873304/2)
781
#define C8 FIXHR(0.17364817766693034885/2)
782

    
783

    
784
/* using Lee like decomposition followed by hand coded 9 points DCT */
785
static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
786
{
787
    int i, j;
788
    INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
789
    INTFLOAT tmp[18], *tmp1, *in1;
790

    
791
    for(i=17;i>=1;i--)
792
        in[i] += in[i-1];
793
    for(i=17;i>=3;i-=2)
794
        in[i] += in[i-2];
795

    
796
    for(j=0;j<2;j++) {
797
        tmp1 = tmp + j;
798
        in1 = in + j;
799

    
800
        t2 = in1[2*4] + in1[2*8] - in1[2*2];
801

    
802
        t3 = in1[2*0] + SHR(in1[2*6],1);
803
        t1 = in1[2*0] - in1[2*6];
804
        tmp1[ 6] = t1 - SHR(t2,1);
805
        tmp1[16] = t1 + t2;
806

    
807
        t0 = MULH3(in1[2*2] + in1[2*4] ,    C2, 2);
808
        t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
809
        t2 = MULH3(in1[2*2] + in1[2*8] ,   -C4, 2);
810

    
811
        tmp1[10] = t3 - t0 - t2;
812
        tmp1[ 2] = t3 + t0 + t1;
813
        tmp1[14] = t3 + t2 - t1;
814

    
815
        tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
816
        t2 = MULH3(in1[2*1] + in1[2*5],    C1, 2);
817
        t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
818
        t0 = MULH3(in1[2*3], C3, 2);
819

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

    
822
        tmp1[ 0] = t2 + t3 + t0;
823
        tmp1[12] = t2 + t1 - t0;
824
        tmp1[ 8] = t3 - t1 - t0;
825
    }
826

    
827
    i = 0;
828
    for(j=0;j<4;j++) {
829
        t0 = tmp[i];
830
        t1 = tmp[i + 2];
831
        s0 = t1 + t0;
832
        s2 = t1 - t0;
833

    
834
        t2 = tmp[i + 1];
835
        t3 = tmp[i + 3];
836
        s1 = MULH3(t3 + t2, icos36h[j], 2);
837
        s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
838

    
839
        t0 = s0 + s1;
840
        t1 = s0 - s1;
841
        out[(9 + j)*SBLIMIT] =  MULH3(t1, win[9 + j], 1) + buf[9 + j];
842
        out[(8 - j)*SBLIMIT] =  MULH3(t1, win[8 - j], 1) + buf[8 - j];
843
        buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
844
        buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
845

    
846
        t0 = s2 + s3;
847
        t1 = s2 - s3;
848
        out[(9 + 8 - j)*SBLIMIT] =  MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
849
        out[(        j)*SBLIMIT] =  MULH3(t1, win[        j], 1) + buf[        j];
850
        buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
851
        buf[      + j] = MULH3(t0, win[18         + j], 1);
852
        i += 4;
853
    }
854

    
855
    s0 = tmp[16];
856
    s1 = MULH3(tmp[17], icos36h[4], 2);
857
    t0 = s0 + s1;
858
    t1 = s0 - s1;
859
    out[(9 + 4)*SBLIMIT] =  MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
860
    out[(8 - 4)*SBLIMIT] =  MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
861
    buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
862
    buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
863
}
864

    
865
/* return the number of decoded frames */
866
static int mp_decode_layer1(MPADecodeContext *s)
867
{
868
    int bound, i, v, n, ch, j, mant;
869
    uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
870
    uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
871

    
872
    if (s->mode == MPA_JSTEREO)
873
        bound = (s->mode_ext + 1) * 4;
874
    else
875
        bound = SBLIMIT;
876

    
877
    /* allocation bits */
878
    for(i=0;i<bound;i++) {
879
        for(ch=0;ch<s->nb_channels;ch++) {
880
            allocation[ch][i] = get_bits(&s->gb, 4);
881
        }
882
    }
883
    for(i=bound;i<SBLIMIT;i++) {
884
        allocation[0][i] = get_bits(&s->gb, 4);
885
    }
886

    
887
    /* scale factors */
888
    for(i=0;i<bound;i++) {
889
        for(ch=0;ch<s->nb_channels;ch++) {
890
            if (allocation[ch][i])
891
                scale_factors[ch][i] = get_bits(&s->gb, 6);
892
        }
893
    }
894
    for(i=bound;i<SBLIMIT;i++) {
895
        if (allocation[0][i]) {
896
            scale_factors[0][i] = get_bits(&s->gb, 6);
897
            scale_factors[1][i] = get_bits(&s->gb, 6);
898
        }
899
    }
900

    
901
    /* compute samples */
902
    for(j=0;j<12;j++) {
903
        for(i=0;i<bound;i++) {
904
            for(ch=0;ch<s->nb_channels;ch++) {
905
                n = allocation[ch][i];
906
                if (n) {
907
                    mant = get_bits(&s->gb, n + 1);
908
                    v = l1_unscale(n, mant, scale_factors[ch][i]);
909
                } else {
910
                    v = 0;
911
                }
912
                s->sb_samples[ch][j][i] = v;
913
            }
914
        }
915
        for(i=bound;i<SBLIMIT;i++) {
916
            n = allocation[0][i];
917
            if (n) {
918
                mant = get_bits(&s->gb, n + 1);
919
                v = l1_unscale(n, mant, scale_factors[0][i]);
920
                s->sb_samples[0][j][i] = v;
921
                v = l1_unscale(n, mant, scale_factors[1][i]);
922
                s->sb_samples[1][j][i] = v;
923
            } else {
924
                s->sb_samples[0][j][i] = 0;
925
                s->sb_samples[1][j][i] = 0;
926
            }
927
        }
928
    }
929
    return 12;
930
}
931

    
932
static int mp_decode_layer2(MPADecodeContext *s)
933
{
934
    int sblimit; /* number of used subbands */
935
    const unsigned char *alloc_table;
936
    int table, bit_alloc_bits, i, j, ch, bound, v;
937
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
938
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
939
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
940
    int scale, qindex, bits, steps, k, l, m, b;
941

    
942
    /* select decoding table */
943
    table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
944
                            s->sample_rate, s->lsf);
945
    sblimit = ff_mpa_sblimit_table[table];
946
    alloc_table = ff_mpa_alloc_tables[table];
947

    
948
    if (s->mode == MPA_JSTEREO)
949
        bound = (s->mode_ext + 1) * 4;
950
    else
951
        bound = sblimit;
952

    
953
    av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
954

    
955
    /* sanity check */
956
    if( bound > sblimit ) bound = sblimit;
957

    
958
    /* parse bit allocation */
959
    j = 0;
960
    for(i=0;i<bound;i++) {
961
        bit_alloc_bits = alloc_table[j];
962
        for(ch=0;ch<s->nb_channels;ch++) {
963
            bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
964
        }
965
        j += 1 << bit_alloc_bits;
966
    }
967
    for(i=bound;i<sblimit;i++) {
968
        bit_alloc_bits = alloc_table[j];
969
        v = get_bits(&s->gb, bit_alloc_bits);
970
        bit_alloc[0][i] = v;
971
        bit_alloc[1][i] = v;
972
        j += 1 << bit_alloc_bits;
973
    }
974

    
975
    /* scale codes */
976
    for(i=0;i<sblimit;i++) {
977
        for(ch=0;ch<s->nb_channels;ch++) {
978
            if (bit_alloc[ch][i])
979
                scale_code[ch][i] = get_bits(&s->gb, 2);
980
        }
981
    }
982

    
983
    /* scale factors */
984
    for(i=0;i<sblimit;i++) {
985
        for(ch=0;ch<s->nb_channels;ch++) {
986
            if (bit_alloc[ch][i]) {
987
                sf = scale_factors[ch][i];
988
                switch(scale_code[ch][i]) {
989
                default:
990
                case 0:
991
                    sf[0] = get_bits(&s->gb, 6);
992
                    sf[1] = get_bits(&s->gb, 6);
993
                    sf[2] = get_bits(&s->gb, 6);
994
                    break;
995
                case 2:
996
                    sf[0] = get_bits(&s->gb, 6);
997
                    sf[1] = sf[0];
998
                    sf[2] = sf[0];
999
                    break;
1000
                case 1:
1001
                    sf[0] = get_bits(&s->gb, 6);
1002
                    sf[2] = get_bits(&s->gb, 6);
1003
                    sf[1] = sf[0];
1004
                    break;
1005
                case 3:
1006
                    sf[0] = get_bits(&s->gb, 6);
1007
                    sf[2] = get_bits(&s->gb, 6);
1008
                    sf[1] = sf[2];
1009
                    break;
1010
                }
1011
            }
1012
        }
1013
    }
1014

    
1015
    /* samples */
1016
    for(k=0;k<3;k++) {
1017
        for(l=0;l<12;l+=3) {
1018
            j = 0;
1019
            for(i=0;i<bound;i++) {
1020
                bit_alloc_bits = alloc_table[j];
1021
                for(ch=0;ch<s->nb_channels;ch++) {
1022
                    b = bit_alloc[ch][i];
1023
                    if (b) {
1024
                        scale = scale_factors[ch][i][k];
1025
                        qindex = alloc_table[j+b];
1026
                        bits = ff_mpa_quant_bits[qindex];
1027
                        if (bits < 0) {
1028
                            int v2;
1029
                            /* 3 values at the same time */
1030
                            v = get_bits(&s->gb, -bits);
1031
                            v2 = division_tabs[qindex][v];
1032
                            steps  = ff_mpa_quant_steps[qindex];
1033

    
1034
                            s->sb_samples[ch][k * 12 + l + 0][i] =
1035
                                l2_unscale_group(steps, v2        & 15, scale);
1036
                            s->sb_samples[ch][k * 12 + l + 1][i] =
1037
                                l2_unscale_group(steps, (v2 >> 4) & 15, scale);
1038
                            s->sb_samples[ch][k * 12 + l + 2][i] =
1039
                                l2_unscale_group(steps,  v2 >> 8      , scale);
1040
                        } else {
1041
                            for(m=0;m<3;m++) {
1042
                                v = get_bits(&s->gb, bits);
1043
                                v = l1_unscale(bits - 1, v, scale);
1044
                                s->sb_samples[ch][k * 12 + l + m][i] = v;
1045
                            }
1046
                        }
1047
                    } else {
1048
                        s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1049
                        s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1050
                        s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1051
                    }
1052
                }
1053
                /* next subband in alloc table */
1054
                j += 1 << bit_alloc_bits;
1055
            }
1056
            /* XXX: find a way to avoid this duplication of code */
1057
            for(i=bound;i<sblimit;i++) {
1058
                bit_alloc_bits = alloc_table[j];
1059
                b = bit_alloc[0][i];
1060
                if (b) {
1061
                    int mant, scale0, scale1;
1062
                    scale0 = scale_factors[0][i][k];
1063
                    scale1 = scale_factors[1][i][k];
1064
                    qindex = alloc_table[j+b];
1065
                    bits = ff_mpa_quant_bits[qindex];
1066
                    if (bits < 0) {
1067
                        /* 3 values at the same time */
1068
                        v = get_bits(&s->gb, -bits);
1069
                        steps = ff_mpa_quant_steps[qindex];
1070
                        mant = v % steps;
1071
                        v = v / steps;
1072
                        s->sb_samples[0][k * 12 + l + 0][i] =
1073
                            l2_unscale_group(steps, mant, scale0);
1074
                        s->sb_samples[1][k * 12 + l + 0][i] =
1075
                            l2_unscale_group(steps, mant, scale1);
1076
                        mant = v % steps;
1077
                        v = v / steps;
1078
                        s->sb_samples[0][k * 12 + l + 1][i] =
1079
                            l2_unscale_group(steps, mant, scale0);
1080
                        s->sb_samples[1][k * 12 + l + 1][i] =
1081
                            l2_unscale_group(steps, mant, scale1);
1082
                        s->sb_samples[0][k * 12 + l + 2][i] =
1083
                            l2_unscale_group(steps, v, scale0);
1084
                        s->sb_samples[1][k * 12 + l + 2][i] =
1085
                            l2_unscale_group(steps, v, scale1);
1086
                    } else {
1087
                        for(m=0;m<3;m++) {
1088
                            mant = get_bits(&s->gb, bits);
1089
                            s->sb_samples[0][k * 12 + l + m][i] =
1090
                                l1_unscale(bits - 1, mant, scale0);
1091
                            s->sb_samples[1][k * 12 + l + m][i] =
1092
                                l1_unscale(bits - 1, mant, scale1);
1093
                        }
1094
                    }
1095
                } else {
1096
                    s->sb_samples[0][k * 12 + l + 0][i] = 0;
1097
                    s->sb_samples[0][k * 12 + l + 1][i] = 0;
1098
                    s->sb_samples[0][k * 12 + l + 2][i] = 0;
1099
                    s->sb_samples[1][k * 12 + l + 0][i] = 0;
1100
                    s->sb_samples[1][k * 12 + l + 1][i] = 0;
1101
                    s->sb_samples[1][k * 12 + l + 2][i] = 0;
1102
                }
1103
                /* next subband in alloc table */
1104
                j += 1 << bit_alloc_bits;
1105
            }
1106
            /* fill remaining samples to zero */
1107
            for(i=sblimit;i<SBLIMIT;i++) {
1108
                for(ch=0;ch<s->nb_channels;ch++) {
1109
                    s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1110
                    s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1111
                    s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1112
                }
1113
            }
1114
        }
1115
    }
1116
    return 3 * 12;
1117
}
1118

    
1119
#define SPLIT(dst,sf,n)\
1120
    if(n==3){\
1121
        int m= (sf*171)>>9;\
1122
        dst= sf - 3*m;\
1123
        sf=m;\
1124
    }else if(n==4){\
1125
        dst= sf&3;\
1126
        sf>>=2;\
1127
    }else if(n==5){\
1128
        int m= (sf*205)>>10;\
1129
        dst= sf - 5*m;\
1130
        sf=m;\
1131
    }else if(n==6){\
1132
        int m= (sf*171)>>10;\
1133
        dst= sf - 6*m;\
1134
        sf=m;\
1135
    }else{\
1136
        dst=0;\
1137
    }
1138

    
1139
static av_always_inline void lsf_sf_expand(int *slen,
1140
                                 int sf, int n1, int n2, int n3)
1141
{
1142
    SPLIT(slen[3], sf, n3)
1143
    SPLIT(slen[2], sf, n2)
1144
    SPLIT(slen[1], sf, n1)
1145
    slen[0] = sf;
1146
}
1147

    
1148
static void exponents_from_scale_factors(MPADecodeContext *s,
1149
                                         GranuleDef *g,
1150
                                         int16_t *exponents)
1151
{
1152
    const uint8_t *bstab, *pretab;
1153
    int len, i, j, k, l, v0, shift, gain, gains[3];
1154
    int16_t *exp_ptr;
1155

    
1156
    exp_ptr = exponents;
1157
    gain = g->global_gain - 210;
1158
    shift = g->scalefac_scale + 1;
1159

    
1160
    bstab = band_size_long[s->sample_rate_index];
1161
    pretab = mpa_pretab[g->preflag];
1162
    for(i=0;i<g->long_end;i++) {
1163
        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1164
        len = bstab[i];
1165
        for(j=len;j>0;j--)
1166
            *exp_ptr++ = v0;
1167
    }
1168

    
1169
    if (g->short_start < 13) {
1170
        bstab = band_size_short[s->sample_rate_index];
1171
        gains[0] = gain - (g->subblock_gain[0] << 3);
1172
        gains[1] = gain - (g->subblock_gain[1] << 3);
1173
        gains[2] = gain - (g->subblock_gain[2] << 3);
1174
        k = g->long_end;
1175
        for(i=g->short_start;i<13;i++) {
1176
            len = bstab[i];
1177
            for(l=0;l<3;l++) {
1178
                v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1179
                for(j=len;j>0;j--)
1180
                *exp_ptr++ = v0;
1181
            }
1182
        }
1183
    }
1184
}
1185

    
1186
/* handle n = 0 too */
1187
static inline int get_bitsz(GetBitContext *s, int n)
1188
{
1189
    if (n == 0)
1190
        return 0;
1191
    else
1192
        return get_bits(s, n);
1193
}
1194

    
1195

    
1196
static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1197
    if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1198
        s->gb= s->in_gb;
1199
        s->in_gb.buffer=NULL;
1200
        assert((get_bits_count(&s->gb) & 7) == 0);
1201
        skip_bits_long(&s->gb, *pos - *end_pos);
1202
        *end_pos2=
1203
        *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1204
        *pos= get_bits_count(&s->gb);
1205
    }
1206
}
1207

    
1208
/* Following is a optimized code for
1209
            INTFLOAT v = *src
1210
            if(get_bits1(&s->gb))
1211
                v = -v;
1212
            *dst = v;
1213
*/
1214
#if CONFIG_FLOAT
1215
#define READ_FLIP_SIGN(dst,src)\
1216
            v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1217
            AV_WN32A(dst, v);
1218
#else
1219
#define READ_FLIP_SIGN(dst,src)\
1220
            v= -get_bits1(&s->gb);\
1221
            *(dst) = (*(src) ^ v) - v;
1222
#endif
1223

    
1224
static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1225
                          int16_t *exponents, int end_pos2)
1226
{
1227
    int s_index;
1228
    int i;
1229
    int last_pos, bits_left;
1230
    VLC *vlc;
1231
    int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1232

    
1233
    /* low frequencies (called big values) */
1234
    s_index = 0;
1235
    for(i=0;i<3;i++) {
1236
        int j, k, l, linbits;
1237
        j = g->region_size[i];
1238
        if (j == 0)
1239
            continue;
1240
        /* select vlc table */
1241
        k = g->table_select[i];
1242
        l = mpa_huff_data[k][0];
1243
        linbits = mpa_huff_data[k][1];
1244
        vlc = &huff_vlc[l];
1245

    
1246
        if(!l){
1247
            memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1248
            s_index += 2*j;
1249
            continue;
1250
        }
1251

    
1252
        /* read huffcode and compute each couple */
1253
        for(;j>0;j--) {
1254
            int exponent, x, y;
1255
            int v;
1256
            int pos= get_bits_count(&s->gb);
1257

    
1258
            if (pos >= end_pos){
1259
//                av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1260
                switch_buffer(s, &pos, &end_pos, &end_pos2);
1261
//                av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1262
                if(pos >= end_pos)
1263
                    break;
1264
            }
1265
            y = get_vlc2(&s->gb, vlc->table, 7, 3);
1266

    
1267
            if(!y){
1268
                g->sb_hybrid[s_index  ] =
1269
                g->sb_hybrid[s_index+1] = 0;
1270
                s_index += 2;
1271
                continue;
1272
            }
1273

    
1274
            exponent= exponents[s_index];
1275

    
1276
            av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1277
                    i, g->region_size[i] - j, x, y, exponent);
1278
            if(y&16){
1279
                x = y >> 5;
1280
                y = y & 0x0f;
1281
                if (x < 15){
1282
                    READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
1283
                }else{
1284
                    x += get_bitsz(&s->gb, linbits);
1285
                    v = l3_unscale(x, exponent);
1286
                    if (get_bits1(&s->gb))
1287
                        v = -v;
1288
                    g->sb_hybrid[s_index] = v;
1289
                }
1290
                if (y < 15){
1291
                    READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
1292
                }else{
1293
                    y += get_bitsz(&s->gb, linbits);
1294
                    v = l3_unscale(y, exponent);
1295
                    if (get_bits1(&s->gb))
1296
                        v = -v;
1297
                    g->sb_hybrid[s_index+1] = v;
1298
                }
1299
            }else{
1300
                x = y >> 5;
1301
                y = y & 0x0f;
1302
                x += y;
1303
                if (x < 15){
1304
                    READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
1305
                }else{
1306
                    x += get_bitsz(&s->gb, linbits);
1307
                    v = l3_unscale(x, exponent);
1308
                    if (get_bits1(&s->gb))
1309
                        v = -v;
1310
                    g->sb_hybrid[s_index+!!y] = v;
1311
                }
1312
                g->sb_hybrid[s_index+ !y] = 0;
1313
            }
1314
            s_index+=2;
1315
        }
1316
    }
1317

    
1318
    /* high frequencies */
1319
    vlc = &huff_quad_vlc[g->count1table_select];
1320
    last_pos=0;
1321
    while (s_index <= 572) {
1322
        int pos, code;
1323
        pos = get_bits_count(&s->gb);
1324
        if (pos >= end_pos) {
1325
            if (pos > end_pos2 && last_pos){
1326
                /* some encoders generate an incorrect size for this
1327
                   part. We must go back into the data */
1328
                s_index -= 4;
1329
                skip_bits_long(&s->gb, last_pos - pos);
1330
                av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1331
                if(s->error_recognition >= FF_ER_COMPLIANT)
1332
                    s_index=0;
1333
                break;
1334
            }
1335
//                av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1336
            switch_buffer(s, &pos, &end_pos, &end_pos2);
1337
//                av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1338
            if(pos >= end_pos)
1339
                break;
1340
        }
1341
        last_pos= pos;
1342

    
1343
        code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1344
        av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1345
        g->sb_hybrid[s_index+0]=
1346
        g->sb_hybrid[s_index+1]=
1347
        g->sb_hybrid[s_index+2]=
1348
        g->sb_hybrid[s_index+3]= 0;
1349
        while(code){
1350
            static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1351
            int v;
1352
            int pos= s_index+idxtab[code];
1353
            code ^= 8>>idxtab[code];
1354
            READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
1355
        }
1356
        s_index+=4;
1357
    }
1358
    /* skip extension bits */
1359
    bits_left = end_pos2 - get_bits_count(&s->gb);
1360
//av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1361
    if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1362
        av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1363
        s_index=0;
1364
    }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1365
        av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1366
        s_index=0;
1367
    }
1368
    memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1369
    skip_bits_long(&s->gb, bits_left);
1370

    
1371
    i= get_bits_count(&s->gb);
1372
    switch_buffer(s, &i, &end_pos, &end_pos2);
1373

    
1374
    return 0;
1375
}
1376

    
1377
/* Reorder short blocks from bitstream order to interleaved order. It
1378
   would be faster to do it in parsing, but the code would be far more
1379
   complicated */
1380
static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1381
{
1382
    int i, j, len;
1383
    INTFLOAT *ptr, *dst, *ptr1;
1384
    INTFLOAT tmp[576];
1385

    
1386
    if (g->block_type != 2)
1387
        return;
1388

    
1389
    if (g->switch_point) {
1390
        if (s->sample_rate_index != 8) {
1391
            ptr = g->sb_hybrid + 36;
1392
        } else {
1393
            ptr = g->sb_hybrid + 48;
1394
        }
1395
    } else {
1396
        ptr = g->sb_hybrid;
1397
    }
1398

    
1399
    for(i=g->short_start;i<13;i++) {
1400
        len = band_size_short[s->sample_rate_index][i];
1401
        ptr1 = ptr;
1402
        dst = tmp;
1403
        for(j=len;j>0;j--) {
1404
            *dst++ = ptr[0*len];
1405
            *dst++ = ptr[1*len];
1406
            *dst++ = ptr[2*len];
1407
            ptr++;
1408
        }
1409
        ptr+=2*len;
1410
        memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1411
    }
1412
}
1413

    
1414
#define ISQRT2 FIXR(0.70710678118654752440)
1415

    
1416
static void compute_stereo(MPADecodeContext *s,
1417
                           GranuleDef *g0, GranuleDef *g1)
1418
{
1419
    int i, j, k, l;
1420
    int sf_max, sf, len, non_zero_found;
1421
    INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1422
    int non_zero_found_short[3];
1423

    
1424
    /* intensity stereo */
1425
    if (s->mode_ext & MODE_EXT_I_STEREO) {
1426
        if (!s->lsf) {
1427
            is_tab = is_table;
1428
            sf_max = 7;
1429
        } else {
1430
            is_tab = is_table_lsf[g1->scalefac_compress & 1];
1431
            sf_max = 16;
1432
        }
1433

    
1434
        tab0 = g0->sb_hybrid + 576;
1435
        tab1 = g1->sb_hybrid + 576;
1436

    
1437
        non_zero_found_short[0] = 0;
1438
        non_zero_found_short[1] = 0;
1439
        non_zero_found_short[2] = 0;
1440
        k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1441
        for(i = 12;i >= g1->short_start;i--) {
1442
            /* for last band, use previous scale factor */
1443
            if (i != 11)
1444
                k -= 3;
1445
            len = band_size_short[s->sample_rate_index][i];
1446
            for(l=2;l>=0;l--) {
1447
                tab0 -= len;
1448
                tab1 -= len;
1449
                if (!non_zero_found_short[l]) {
1450
                    /* test if non zero band. if so, stop doing i-stereo */
1451
                    for(j=0;j<len;j++) {
1452
                        if (tab1[j] != 0) {
1453
                            non_zero_found_short[l] = 1;
1454
                            goto found1;
1455
                        }
1456
                    }
1457
                    sf = g1->scale_factors[k + l];
1458
                    if (sf >= sf_max)
1459
                        goto found1;
1460

    
1461
                    v1 = is_tab[0][sf];
1462
                    v2 = is_tab[1][sf];
1463
                    for(j=0;j<len;j++) {
1464
                        tmp0 = tab0[j];
1465
                        tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1466
                        tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1467
                    }
1468
                } else {
1469
                found1:
1470
                    if (s->mode_ext & MODE_EXT_MS_STEREO) {
1471
                        /* lower part of the spectrum : do ms stereo
1472
                           if enabled */
1473
                        for(j=0;j<len;j++) {
1474
                            tmp0 = tab0[j];
1475
                            tmp1 = tab1[j];
1476
                            tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1477
                            tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1478
                        }
1479
                    }
1480
                }
1481
            }
1482
        }
1483

    
1484
        non_zero_found = non_zero_found_short[0] |
1485
            non_zero_found_short[1] |
1486
            non_zero_found_short[2];
1487

    
1488
        for(i = g1->long_end - 1;i >= 0;i--) {
1489
            len = band_size_long[s->sample_rate_index][i];
1490
            tab0 -= len;
1491
            tab1 -= len;
1492
            /* test if non zero band. if so, stop doing i-stereo */
1493
            if (!non_zero_found) {
1494
                for(j=0;j<len;j++) {
1495
                    if (tab1[j] != 0) {
1496
                        non_zero_found = 1;
1497
                        goto found2;
1498
                    }
1499
                }
1500
                /* for last band, use previous scale factor */
1501
                k = (i == 21) ? 20 : i;
1502
                sf = g1->scale_factors[k];
1503
                if (sf >= sf_max)
1504
                    goto found2;
1505
                v1 = is_tab[0][sf];
1506
                v2 = is_tab[1][sf];
1507
                for(j=0;j<len;j++) {
1508
                    tmp0 = tab0[j];
1509
                    tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1510
                    tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1511
                }
1512
            } else {
1513
            found2:
1514
                if (s->mode_ext & MODE_EXT_MS_STEREO) {
1515
                    /* lower part of the spectrum : do ms stereo
1516
                       if enabled */
1517
                    for(j=0;j<len;j++) {
1518
                        tmp0 = tab0[j];
1519
                        tmp1 = tab1[j];
1520
                        tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1521
                        tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1522
                    }
1523
                }
1524
            }
1525
        }
1526
    } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1527
        /* ms stereo ONLY */
1528
        /* NOTE: the 1/sqrt(2) normalization factor is included in the
1529
           global gain */
1530
        tab0 = g0->sb_hybrid;
1531
        tab1 = g1->sb_hybrid;
1532
        for(i=0;i<576;i++) {
1533
            tmp0 = tab0[i];
1534
            tmp1 = tab1[i];
1535
            tab0[i] = tmp0 + tmp1;
1536
            tab1[i] = tmp0 - tmp1;
1537
        }
1538
    }
1539
}
1540

    
1541
#if !CONFIG_FLOAT
1542
static void compute_antialias_integer(MPADecodeContext *s,
1543
                              GranuleDef *g)
1544
{
1545
    int32_t *ptr, *csa;
1546
    int n, i;
1547

    
1548
    /* we antialias only "long" bands */
1549
    if (g->block_type == 2) {
1550
        if (!g->switch_point)
1551
            return;
1552
        /* XXX: check this for 8000Hz case */
1553
        n = 1;
1554
    } else {
1555
        n = SBLIMIT - 1;
1556
    }
1557

    
1558
    ptr = g->sb_hybrid + 18;
1559
    for(i = n;i > 0;i--) {
1560
        int tmp0, tmp1, tmp2;
1561
        csa = &csa_table[0][0];
1562
#define INT_AA(j) \
1563
            tmp0 = ptr[-1-j];\
1564
            tmp1 = ptr[   j];\
1565
            tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1566
            ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1567
            ptr[   j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1568

    
1569
        INT_AA(0)
1570
        INT_AA(1)
1571
        INT_AA(2)
1572
        INT_AA(3)
1573
        INT_AA(4)
1574
        INT_AA(5)
1575
        INT_AA(6)
1576
        INT_AA(7)
1577

    
1578
        ptr += 18;
1579
    }
1580
}
1581
#endif
1582

    
1583
static void compute_imdct(MPADecodeContext *s,
1584
                          GranuleDef *g,
1585
                          INTFLOAT *sb_samples,
1586
                          INTFLOAT *mdct_buf)
1587
{
1588
    INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1589
    INTFLOAT out2[12];
1590
    int i, j, mdct_long_end, sblimit;
1591

    
1592
    /* find last non zero block */
1593
    ptr = g->sb_hybrid + 576;
1594
    ptr1 = g->sb_hybrid + 2 * 18;
1595
    while (ptr >= ptr1) {
1596
        int32_t *p;
1597
        ptr -= 6;
1598
        p= (int32_t*)ptr;
1599
        if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1600
            break;
1601
    }
1602
    sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1603

    
1604
    if (g->block_type == 2) {
1605
        /* XXX: check for 8000 Hz */
1606
        if (g->switch_point)
1607
            mdct_long_end = 2;
1608
        else
1609
            mdct_long_end = 0;
1610
    } else {
1611
        mdct_long_end = sblimit;
1612
    }
1613

    
1614
    buf = mdct_buf;
1615
    ptr = g->sb_hybrid;
1616
    for(j=0;j<mdct_long_end;j++) {
1617
        /* apply window & overlap with previous buffer */
1618
        out_ptr = sb_samples + j;
1619
        /* select window */
1620
        if (g->switch_point && j < 2)
1621
            win1 = mdct_win[0];
1622
        else
1623
            win1 = mdct_win[g->block_type];
1624
        /* select frequency inversion */
1625
        win = win1 + ((4 * 36) & -(j & 1));
1626
        imdct36(out_ptr, buf, ptr, win);
1627
        out_ptr += 18*SBLIMIT;
1628
        ptr += 18;
1629
        buf += 18;
1630
    }
1631
    for(j=mdct_long_end;j<sblimit;j++) {
1632
        /* select frequency inversion */
1633
        win = mdct_win[2] + ((4 * 36) & -(j & 1));
1634
        out_ptr = sb_samples + j;
1635

    
1636
        for(i=0; i<6; i++){
1637
            *out_ptr = buf[i];
1638
            out_ptr += SBLIMIT;
1639
        }
1640
        imdct12(out2, ptr + 0);
1641
        for(i=0;i<6;i++) {
1642
            *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*1];
1643
            buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1644
            out_ptr += SBLIMIT;
1645
        }
1646
        imdct12(out2, ptr + 1);
1647
        for(i=0;i<6;i++) {
1648
            *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*2];
1649
            buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1650
            out_ptr += SBLIMIT;
1651
        }
1652
        imdct12(out2, ptr + 2);
1653
        for(i=0;i<6;i++) {
1654
            buf[i + 6*0] = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*0];
1655
            buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1656
            buf[i + 6*2] = 0;
1657
        }
1658
        ptr += 18;
1659
        buf += 18;
1660
    }
1661
    /* zero bands */
1662
    for(j=sblimit;j<SBLIMIT;j++) {
1663
        /* overlap */
1664
        out_ptr = sb_samples + j;
1665
        for(i=0;i<18;i++) {
1666
            *out_ptr = buf[i];
1667
            buf[i] = 0;
1668
            out_ptr += SBLIMIT;
1669
        }
1670
        buf += 18;
1671
    }
1672
}
1673

    
1674
/* main layer3 decoding function */
1675
static int mp_decode_layer3(MPADecodeContext *s)
1676
{
1677
    int nb_granules, main_data_begin, private_bits;
1678
    int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1679
    GranuleDef *g;
1680
    int16_t exponents[576]; //FIXME try INTFLOAT
1681

    
1682
    /* read side info */
1683
    if (s->lsf) {
1684
        main_data_begin = get_bits(&s->gb, 8);
1685
        private_bits = get_bits(&s->gb, s->nb_channels);
1686
        nb_granules = 1;
1687
    } else {
1688
        main_data_begin = get_bits(&s->gb, 9);
1689
        if (s->nb_channels == 2)
1690
            private_bits = get_bits(&s->gb, 3);
1691
        else
1692
            private_bits = get_bits(&s->gb, 5);
1693
        nb_granules = 2;
1694
        for(ch=0;ch<s->nb_channels;ch++) {
1695
            s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1696
            s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1697
        }
1698
    }
1699

    
1700
    for(gr=0;gr<nb_granules;gr++) {
1701
        for(ch=0;ch<s->nb_channels;ch++) {
1702
            av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1703
            g = &s->granules[ch][gr];
1704
            g->part2_3_length = get_bits(&s->gb, 12);
1705
            g->big_values = get_bits(&s->gb, 9);
1706
            if(g->big_values > 288){
1707
                av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1708
                return -1;
1709
            }
1710

    
1711
            g->global_gain = get_bits(&s->gb, 8);
1712
            /* if MS stereo only is selected, we precompute the
1713
               1/sqrt(2) renormalization factor */
1714
            if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1715
                MODE_EXT_MS_STEREO)
1716
                g->global_gain -= 2;
1717
            if (s->lsf)
1718
                g->scalefac_compress = get_bits(&s->gb, 9);
1719
            else
1720
                g->scalefac_compress = get_bits(&s->gb, 4);
1721
            blocksplit_flag = get_bits1(&s->gb);
1722
            if (blocksplit_flag) {
1723
                g->block_type = get_bits(&s->gb, 2);
1724
                if (g->block_type == 0){
1725
                    av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1726
                    return -1;
1727
                }
1728
                g->switch_point = get_bits1(&s->gb);
1729
                for(i=0;i<2;i++)
1730
                    g->table_select[i] = get_bits(&s->gb, 5);
1731
                for(i=0;i<3;i++)
1732
                    g->subblock_gain[i] = get_bits(&s->gb, 3);
1733
                ff_init_short_region(s, g);
1734
            } else {
1735
                int region_address1, region_address2;
1736
                g->block_type = 0;
1737
                g->switch_point = 0;
1738
                for(i=0;i<3;i++)
1739
                    g->table_select[i] = get_bits(&s->gb, 5);
1740
                /* compute huffman coded region sizes */
1741
                region_address1 = get_bits(&s->gb, 4);
1742
                region_address2 = get_bits(&s->gb, 3);
1743
                av_dlog(s->avctx, "region1=%d region2=%d\n",
1744
                        region_address1, region_address2);
1745
                ff_init_long_region(s, g, region_address1, region_address2);
1746
            }
1747
            ff_region_offset2size(g);
1748
            ff_compute_band_indexes(s, g);
1749

    
1750
            g->preflag = 0;
1751
            if (!s->lsf)
1752
                g->preflag = get_bits1(&s->gb);
1753
            g->scalefac_scale = get_bits1(&s->gb);
1754
            g->count1table_select = get_bits1(&s->gb);
1755
            av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1756
                    g->block_type, g->switch_point);
1757
        }
1758
    }
1759

    
1760
  if (!s->adu_mode) {
1761
    const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1762
    assert((get_bits_count(&s->gb) & 7) == 0);
1763
    /* now we get bits from the main_data_begin offset */
1764
    av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1765
//av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1766

    
1767
    memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1768
    s->in_gb= s->gb;
1769
        init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1770
        skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1771
  }
1772

    
1773
    for(gr=0;gr<nb_granules;gr++) {
1774
        for(ch=0;ch<s->nb_channels;ch++) {
1775
            g = &s->granules[ch][gr];
1776
            if(get_bits_count(&s->gb)<0){
1777
                av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1778
                                            main_data_begin, s->last_buf_size, gr);
1779
                skip_bits_long(&s->gb, g->part2_3_length);
1780
                memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1781
                if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
1782
                    skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1783
                    s->gb= s->in_gb;
1784
                    s->in_gb.buffer=NULL;
1785
                }
1786
                continue;
1787
            }
1788

    
1789
            bits_pos = get_bits_count(&s->gb);
1790

    
1791
            if (!s->lsf) {
1792
                uint8_t *sc;
1793
                int slen, slen1, slen2;
1794

    
1795
                /* MPEG1 scale factors */
1796
                slen1 = slen_table[0][g->scalefac_compress];
1797
                slen2 = slen_table[1][g->scalefac_compress];
1798
                av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1799
                if (g->block_type == 2) {
1800
                    n = g->switch_point ? 17 : 18;
1801
                    j = 0;
1802
                    if(slen1){
1803
                        for(i=0;i<n;i++)
1804
                            g->scale_factors[j++] = get_bits(&s->gb, slen1);
1805
                    }else{
1806
                        for(i=0;i<n;i++)
1807
                            g->scale_factors[j++] = 0;
1808
                    }
1809
                    if(slen2){
1810
                        for(i=0;i<18;i++)
1811
                            g->scale_factors[j++] = get_bits(&s->gb, slen2);
1812
                        for(i=0;i<3;i++)
1813
                            g->scale_factors[j++] = 0;
1814
                    }else{
1815
                        for(i=0;i<21;i++)
1816
                            g->scale_factors[j++] = 0;
1817
                    }
1818
                } else {
1819
                    sc = s->granules[ch][0].scale_factors;
1820
                    j = 0;
1821
                    for(k=0;k<4;k++) {
1822
                        n = (k == 0 ? 6 : 5);
1823
                        if ((g->scfsi & (0x8 >> k)) == 0) {
1824
                            slen = (k < 2) ? slen1 : slen2;
1825
                            if(slen){
1826
                                for(i=0;i<n;i++)
1827
                                    g->scale_factors[j++] = get_bits(&s->gb, slen);
1828
                            }else{
1829
                                for(i=0;i<n;i++)
1830
                                    g->scale_factors[j++] = 0;
1831
                            }
1832
                        } else {
1833
                            /* simply copy from last granule */
1834
                            for(i=0;i<n;i++) {
1835
                                g->scale_factors[j] = sc[j];
1836
                                j++;
1837
                            }
1838
                        }
1839
                    }
1840
                    g->scale_factors[j++] = 0;
1841
                }
1842
            } else {
1843
                int tindex, tindex2, slen[4], sl, sf;
1844

    
1845
                /* LSF scale factors */
1846
                if (g->block_type == 2) {
1847
                    tindex = g->switch_point ? 2 : 1;
1848
                } else {
1849
                    tindex = 0;
1850
                }
1851
                sf = g->scalefac_compress;
1852
                if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1853
                    /* intensity stereo case */
1854
                    sf >>= 1;
1855
                    if (sf < 180) {
1856
                        lsf_sf_expand(slen, sf, 6, 6, 0);
1857
                        tindex2 = 3;
1858
                    } else if (sf < 244) {
1859
                        lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1860
                        tindex2 = 4;
1861
                    } else {
1862
                        lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1863
                        tindex2 = 5;
1864
                    }
1865
                } else {
1866
                    /* normal case */
1867
                    if (sf < 400) {
1868
                        lsf_sf_expand(slen, sf, 5, 4, 4);
1869
                        tindex2 = 0;
1870
                    } else if (sf < 500) {
1871
                        lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1872
                        tindex2 = 1;
1873
                    } else {
1874
                        lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1875
                        tindex2 = 2;
1876
                        g->preflag = 1;
1877
                    }
1878
                }
1879

    
1880
                j = 0;
1881
                for(k=0;k<4;k++) {
1882
                    n = lsf_nsf_table[tindex2][tindex][k];
1883
                    sl = slen[k];
1884
                    if(sl){
1885
                        for(i=0;i<n;i++)
1886
                            g->scale_factors[j++] = get_bits(&s->gb, sl);
1887
                    }else{
1888
                        for(i=0;i<n;i++)
1889
                            g->scale_factors[j++] = 0;
1890
                    }
1891
                }
1892
                /* XXX: should compute exact size */
1893
                for(;j<40;j++)
1894
                    g->scale_factors[j] = 0;
1895
            }
1896

    
1897
            exponents_from_scale_factors(s, g, exponents);
1898

    
1899
            /* read Huffman coded residue */
1900
            huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1901
        } /* ch */
1902

    
1903
        if (s->nb_channels == 2)
1904
            compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1905

    
1906
        for(ch=0;ch<s->nb_channels;ch++) {
1907
            g = &s->granules[ch][gr];
1908

    
1909
            reorder_block(s, g);
1910
            compute_antialias(s, g);
1911
            compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1912
        }
1913
    } /* gr */
1914
    if(get_bits_count(&s->gb)<0)
1915
        skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1916
    return nb_granules * 18;
1917
}
1918

    
1919
static int mp_decode_frame(MPADecodeContext *s,
1920
                           OUT_INT *samples, const uint8_t *buf, int buf_size)
1921
{
1922
    int i, nb_frames, ch;
1923
    OUT_INT *samples_ptr;
1924

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

    
1927
    /* skip error protection field */
1928
    if (s->error_protection)
1929
        skip_bits(&s->gb, 16);
1930

    
1931
    av_dlog(s->avctx, "frame %d:\n", s->frame_count);
1932
    switch(s->layer) {
1933
    case 1:
1934
        s->avctx->frame_size = 384;
1935
        nb_frames = mp_decode_layer1(s);
1936
        break;
1937
    case 2:
1938
        s->avctx->frame_size = 1152;
1939
        nb_frames = mp_decode_layer2(s);
1940
        break;
1941
    case 3:
1942
        s->avctx->frame_size = s->lsf ? 576 : 1152;
1943
    default:
1944
        nb_frames = mp_decode_layer3(s);
1945

    
1946
        s->last_buf_size=0;
1947
        if(s->in_gb.buffer){
1948
            align_get_bits(&s->gb);
1949
            i= get_bits_left(&s->gb)>>3;
1950
            if(i >= 0 && i <= BACKSTEP_SIZE){
1951
                memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1952
                s->last_buf_size=i;
1953
            }else
1954
                av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1955
            s->gb= s->in_gb;
1956
            s->in_gb.buffer= NULL;
1957
        }
1958

    
1959
        align_get_bits(&s->gb);
1960
        assert((get_bits_count(&s->gb) & 7) == 0);
1961
        i= get_bits_left(&s->gb)>>3;
1962

    
1963
        if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
1964
            if(i<0)
1965
                av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1966
            i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1967
        }
1968
        assert(i <= buf_size - HEADER_SIZE && i>= 0);
1969
        memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1970
        s->last_buf_size += i;
1971

    
1972
        break;
1973
    }
1974

    
1975
    /* apply the synthesis filter */
1976
    for(ch=0;ch<s->nb_channels;ch++) {
1977
        samples_ptr = samples + ch;
1978
        for(i=0;i<nb_frames;i++) {
1979
            RENAME(ff_mpa_synth_filter)(
1980
#if CONFIG_FLOAT
1981
                         s,
1982
#endif
1983
                         s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1984
                         RENAME(ff_mpa_synth_window), &s->dither_state,
1985
                         samples_ptr, s->nb_channels,
1986
                         s->sb_samples[ch][i]);
1987
            samples_ptr += 32 * s->nb_channels;
1988
        }
1989
    }
1990

    
1991
    return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1992
}
1993

    
1994
static int decode_frame(AVCodecContext * avctx,
1995
                        void *data, int *data_size,
1996
                        AVPacket *avpkt)
1997
{
1998
    const uint8_t *buf = avpkt->data;
1999
    int buf_size = avpkt->size;
2000
    MPADecodeContext *s = avctx->priv_data;
2001
    uint32_t header;
2002
    int out_size;
2003
    OUT_INT *out_samples = data;
2004

    
2005
    if(buf_size < HEADER_SIZE)
2006
        return -1;
2007

    
2008
    header = AV_RB32(buf);
2009
    if(ff_mpa_check_header(header) < 0){
2010
        av_log(avctx, AV_LOG_ERROR, "Header missing\n");
2011
        return -1;
2012
    }
2013

    
2014
    if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
2015
        /* free format: prepare to compute frame size */
2016
        s->frame_size = -1;
2017
        return -1;
2018
    }
2019
    /* update codec info */
2020
    avctx->channels = s->nb_channels;
2021
    avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
2022
    if (!avctx->bit_rate)
2023
        avctx->bit_rate = s->bit_rate;
2024
    avctx->sub_id = s->layer;
2025

    
2026
    if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
2027
        return -1;
2028
    *data_size = 0;
2029

    
2030
    if(s->frame_size<=0 || s->frame_size > buf_size){
2031
        av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2032
        return -1;
2033
    }else if(s->frame_size < buf_size){
2034
        av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2035
        buf_size= s->frame_size;
2036
    }
2037

    
2038
    out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2039
    if(out_size>=0){
2040
        *data_size = out_size;
2041
        avctx->sample_rate = s->sample_rate;
2042
        //FIXME maybe move the other codec info stuff from above here too
2043
    }else
2044
        av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2045
    s->frame_size = 0;
2046
    return buf_size;
2047
}
2048

    
2049
static void flush(AVCodecContext *avctx){
2050
    MPADecodeContext *s = avctx->priv_data;
2051
    memset(s->synth_buf, 0, sizeof(s->synth_buf));
2052
    s->last_buf_size= 0;
2053
}
2054

    
2055
#if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
2056
static int decode_frame_adu(AVCodecContext * avctx,
2057
                        void *data, int *data_size,
2058
                        AVPacket *avpkt)
2059
{
2060
    const uint8_t *buf = avpkt->data;
2061
    int buf_size = avpkt->size;
2062
    MPADecodeContext *s = avctx->priv_data;
2063
    uint32_t header;
2064
    int len, out_size;
2065
    OUT_INT *out_samples = data;
2066

    
2067
    len = buf_size;
2068

    
2069
    // Discard too short frames
2070
    if (buf_size < HEADER_SIZE) {
2071
        *data_size = 0;
2072
        return buf_size;
2073
    }
2074

    
2075

    
2076
    if (len > MPA_MAX_CODED_FRAME_SIZE)
2077
        len = MPA_MAX_CODED_FRAME_SIZE;
2078

    
2079
    // Get header and restore sync word
2080
    header = AV_RB32(buf) | 0xffe00000;
2081

    
2082
    if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2083
        *data_size = 0;
2084
        return buf_size;
2085
    }
2086

    
2087
    ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2088
    /* update codec info */
2089
    avctx->sample_rate = s->sample_rate;
2090
    avctx->channels = s->nb_channels;
2091
    if (!avctx->bit_rate)
2092
        avctx->bit_rate = s->bit_rate;
2093
    avctx->sub_id = s->layer;
2094

    
2095
    s->frame_size = len;
2096

    
2097
    if (avctx->parse_only) {
2098
        out_size = buf_size;
2099
    } else {
2100
        out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2101
    }
2102

    
2103
    *data_size = out_size;
2104
    return buf_size;
2105
}
2106
#endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
2107

    
2108
#if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
2109

    
2110
/**
2111
 * Context for MP3On4 decoder
2112
 */
2113
typedef struct MP3On4DecodeContext {
2114
    int frames;   ///< number of mp3 frames per block (number of mp3 decoder instances)
2115
    int syncword; ///< syncword patch
2116
    const uint8_t *coff; ///< channels offsets in output buffer
2117
    MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2118
} MP3On4DecodeContext;
2119

    
2120
#include "mpeg4audio.h"
2121

    
2122
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2123
static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5};   /* number of mp3 decoder instances */
2124
/* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2125
static const uint8_t chan_offset[8][5] = {
2126
    {0},
2127
    {0},            // C
2128
    {0},            // FLR
2129
    {2,0},          // C FLR
2130
    {2,0,3},        // C FLR BS
2131
    {4,0,2},        // C FLR BLRS
2132
    {4,0,2,5},      // C FLR BLRS LFE
2133
    {4,0,2,6,5},    // C FLR BLRS BLR LFE
2134
};
2135

    
2136

    
2137
static int decode_init_mp3on4(AVCodecContext * avctx)
2138
{
2139
    MP3On4DecodeContext *s = avctx->priv_data;
2140
    MPEG4AudioConfig cfg;
2141
    int i;
2142

    
2143
    if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2144
        av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2145
        return -1;
2146
    }
2147

    
2148
    ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2149
    if (!cfg.chan_config || cfg.chan_config > 7) {
2150
        av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2151
        return -1;
2152
    }
2153
    s->frames = mp3Frames[cfg.chan_config];
2154
    s->coff = chan_offset[cfg.chan_config];
2155
    avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2156

    
2157
    if (cfg.sample_rate < 16000)
2158
        s->syncword = 0xffe00000;
2159
    else
2160
        s->syncword = 0xfff00000;
2161

    
2162
    /* Init the first mp3 decoder in standard way, so that all tables get builded
2163
     * We replace avctx->priv_data with the context of the first decoder so that
2164
     * decode_init() does not have to be changed.
2165
     * Other decoders will be initialized here copying data from the first context
2166
     */
2167
    // Allocate zeroed memory for the first decoder context
2168
    s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2169
    // Put decoder context in place to make init_decode() happy
2170
    avctx->priv_data = s->mp3decctx[0];
2171
    decode_init(avctx);
2172
    // Restore mp3on4 context pointer
2173
    avctx->priv_data = s;
2174
    s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2175

    
2176
    /* Create a separate codec/context for each frame (first is already ok).
2177
     * Each frame is 1 or 2 channels - up to 5 frames allowed
2178
     */
2179
    for (i = 1; i < s->frames; i++) {
2180
        s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2181
        s->mp3decctx[i]->adu_mode = 1;
2182
        s->mp3decctx[i]->avctx = avctx;
2183
    }
2184

    
2185
    return 0;
2186
}
2187

    
2188

    
2189
static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
2190
{
2191
    MP3On4DecodeContext *s = avctx->priv_data;
2192
    int i;
2193

    
2194
    for (i = 0; i < s->frames; i++)
2195
        av_free(s->mp3decctx[i]);
2196

    
2197
    return 0;
2198
}
2199

    
2200

    
2201
static int decode_frame_mp3on4(AVCodecContext * avctx,
2202
                        void *data, int *data_size,
2203
                        AVPacket *avpkt)
2204
{
2205
    const uint8_t *buf = avpkt->data;
2206
    int buf_size = avpkt->size;
2207
    MP3On4DecodeContext *s = avctx->priv_data;
2208
    MPADecodeContext *m;
2209
    int fsize, len = buf_size, out_size = 0;
2210
    uint32_t header;
2211
    OUT_INT *out_samples = data;
2212
    OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2213
    OUT_INT *outptr, *bp;
2214
    int fr, j, n;
2215

    
2216
    if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2217
        return -1;
2218

    
2219
    *data_size = 0;
2220
    // Discard too short frames
2221
    if (buf_size < HEADER_SIZE)
2222
        return -1;
2223

    
2224
    // If only one decoder interleave is not needed
2225
    outptr = s->frames == 1 ? out_samples : decoded_buf;
2226

    
2227
    avctx->bit_rate = 0;
2228

    
2229
    for (fr = 0; fr < s->frames; fr++) {
2230
        fsize = AV_RB16(buf) >> 4;
2231
        fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2232
        m = s->mp3decctx[fr];
2233
        assert (m != NULL);
2234

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

    
2237
        if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2238
            break;
2239

    
2240
        ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2241
        out_size += mp_decode_frame(m, outptr, buf, fsize);
2242
        buf += fsize;
2243
        len -= fsize;
2244

    
2245
        if(s->frames > 1) {
2246
            n = m->avctx->frame_size*m->nb_channels;
2247
            /* interleave output data */
2248
            bp = out_samples + s->coff[fr];
2249
            if(m->nb_channels == 1) {
2250
                for(j = 0; j < n; j++) {
2251
                    *bp = decoded_buf[j];
2252
                    bp += avctx->channels;
2253
                }
2254
            } else {
2255
                for(j = 0; j < n; j++) {
2256
                    bp[0] = decoded_buf[j++];
2257
                    bp[1] = decoded_buf[j];
2258
                    bp += avctx->channels;
2259
                }
2260
            }
2261
        }
2262
        avctx->bit_rate += m->bit_rate;
2263
    }
2264

    
2265
    /* update codec info */
2266
    avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2267

    
2268
    *data_size = out_size;
2269
    return buf_size;
2270
}
2271
#endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2272

    
2273
#if !CONFIG_FLOAT
2274
#if CONFIG_MP1_DECODER
2275
AVCodec ff_mp1_decoder =
2276
{
2277
    "mp1",
2278
    AVMEDIA_TYPE_AUDIO,
2279
    CODEC_ID_MP1,
2280
    sizeof(MPADecodeContext),
2281
    decode_init,
2282
    NULL,
2283
    NULL,
2284
    decode_frame,
2285
    CODEC_CAP_PARSE_ONLY,
2286
    .flush= flush,
2287
    .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2288
};
2289
#endif
2290
#if CONFIG_MP2_DECODER
2291
AVCodec ff_mp2_decoder =
2292
{
2293
    "mp2",
2294
    AVMEDIA_TYPE_AUDIO,
2295
    CODEC_ID_MP2,
2296
    sizeof(MPADecodeContext),
2297
    decode_init,
2298
    NULL,
2299
    NULL,
2300
    decode_frame,
2301
    CODEC_CAP_PARSE_ONLY,
2302
    .flush= flush,
2303
    .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2304
};
2305
#endif
2306
#if CONFIG_MP3_DECODER
2307
AVCodec ff_mp3_decoder =
2308
{
2309
    "mp3",
2310
    AVMEDIA_TYPE_AUDIO,
2311
    CODEC_ID_MP3,
2312
    sizeof(MPADecodeContext),
2313
    decode_init,
2314
    NULL,
2315
    NULL,
2316
    decode_frame,
2317
    CODEC_CAP_PARSE_ONLY,
2318
    .flush= flush,
2319
    .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2320
};
2321
#endif
2322
#if CONFIG_MP3ADU_DECODER
2323
AVCodec ff_mp3adu_decoder =
2324
{
2325
    "mp3adu",
2326
    AVMEDIA_TYPE_AUDIO,
2327
    CODEC_ID_MP3ADU,
2328
    sizeof(MPADecodeContext),
2329
    decode_init,
2330
    NULL,
2331
    NULL,
2332
    decode_frame_adu,
2333
    CODEC_CAP_PARSE_ONLY,
2334
    .flush= flush,
2335
    .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2336
};
2337
#endif
2338
#if CONFIG_MP3ON4_DECODER
2339
AVCodec ff_mp3on4_decoder =
2340
{
2341
    "mp3on4",
2342
    AVMEDIA_TYPE_AUDIO,
2343
    CODEC_ID_MP3ON4,
2344
    sizeof(MP3On4DecodeContext),
2345
    decode_init_mp3on4,
2346
    NULL,
2347
    decode_close_mp3on4,
2348
    decode_frame_mp3on4,
2349
    .flush= flush,
2350
    .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
2351
};
2352
#endif
2353
#endif