Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudiodec.c @ a7a85899

History | View | Annotate | Download (81.4 KB)

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

    
20
/**
21
 * @file mpegaudiodec.c
22
 * MPEG Audio decoder.
23
 */ 
24

    
25
//#define DEBUG
26
#include "avcodec.h"
27
#include "bitstream.h"
28
#include "mpegaudio.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
/* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
38
   audio decoder */
39
#ifdef CONFIG_MPEGAUDIO_HP
40
#define USE_HIGHPRECISION
41
#endif
42

    
43
#ifdef USE_HIGHPRECISION
44
#define FRAC_BITS   23   /* fractional bits for sb_samples and dct */
45
#define WFRAC_BITS  16   /* fractional bits for window */
46
#else
47
#define FRAC_BITS   15   /* fractional bits for sb_samples and dct */
48
#define WFRAC_BITS  14   /* fractional bits for window */
49
#endif
50

    
51
#define FRAC_ONE    (1 << FRAC_BITS)
52

    
53
#define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
54
#define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
55
#define FIX(a)   ((int)((a) * FRAC_ONE))
56
/* WARNING: only correct for posititive numbers */
57
#define FIXR(a)   ((int)((a) * FRAC_ONE + 0.5))
58
#define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
59

    
60
#if FRAC_BITS <= 15
61
typedef int16_t MPA_INT;
62
#else
63
typedef int32_t MPA_INT;
64
#endif
65

    
66
/****************/
67

    
68
#define HEADER_SIZE 4
69
#define BACKSTEP_SIZE 512
70

    
71
struct GranuleDef;
72

    
73
typedef struct MPADecodeContext {
74
    uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE];        /* input buffer */
75
    int inbuf_index;
76
    uint8_t *inbuf_ptr, *inbuf;
77
    int frame_size;
78
    int free_format_frame_size; /* frame size in case of free format
79
                                   (zero if currently unknown) */
80
    /* next header (used in free format parsing) */
81
    uint32_t free_format_next_header; 
82
    int error_protection;
83
    int layer;
84
    int sample_rate;
85
    int sample_rate_index; /* between 0 and 8 */
86
    int bit_rate;
87
    int old_frame_size;
88
    GetBitContext gb;
89
    int nb_channels;
90
    int mode;
91
    int mode_ext;
92
    int lsf;
93
    MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
94
    int synth_buf_offset[MPA_MAX_CHANNELS];
95
    int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
96
    int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
97
#ifdef DEBUG
98
    int frame_count;
99
#endif
100
    void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
101
    int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
102
    unsigned int dither_state;
103
} MPADecodeContext;
104

    
105
/* layer 3 "granule" */
106
typedef struct GranuleDef {
107
    uint8_t scfsi;
108
    int part2_3_length;
109
    int big_values;
110
    int global_gain;
111
    int scalefac_compress;
112
    uint8_t block_type;
113
    uint8_t switch_point;
114
    int table_select[3];
115
    int subblock_gain[3];
116
    uint8_t scalefac_scale;
117
    uint8_t count1table_select;
118
    int region_size[3]; /* number of huffman codes in each region */
119
    int preflag;
120
    int short_start, long_end; /* long/short band indexes */
121
    uint8_t scale_factors[40];
122
    int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
123
} GranuleDef;
124

    
125
#define MODE_EXT_MS_STEREO 2
126
#define MODE_EXT_I_STEREO  1
127

    
128
/* layer 3 huffman tables */
129
typedef struct HuffTable {
130
    int xsize;
131
    const uint8_t *bits;
132
    const uint16_t *codes;
133
} HuffTable;
134

    
135
#include "mpegaudiodectab.h"
136

    
137
static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
138
static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
139

    
140
/* vlc structure for decoding layer 3 huffman tables */
141
static VLC huff_vlc[16]; 
142
static uint8_t *huff_code_table[16];
143
static VLC huff_quad_vlc[2];
144
/* computed from band_size_long */
145
static uint16_t band_index_long[9][23];
146
/* XXX: free when all decoders are closed */
147
#define TABLE_4_3_SIZE (8191 + 16)
148
static int8_t  *table_4_3_exp;
149
#if FRAC_BITS <= 15
150
static uint16_t *table_4_3_value;
151
#else
152
static uint32_t *table_4_3_value;
153
#endif
154
/* intensity stereo coef table */
155
static int32_t is_table[2][16];
156
static int32_t is_table_lsf[2][2][16];
157
static int32_t csa_table[8][4];
158
static float csa_table_float[8][4];
159
static int32_t mdct_win[8][36];
160

    
161
/* lower 2 bits: modulo 3, higher bits: shift */
162
static uint16_t scale_factor_modshift[64];
163
/* [i][j]:  2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
164
static int32_t scale_factor_mult[15][3];
165
/* mult table for layer 2 group quantization */
166

    
167
#define SCALE_GEN(v) \
168
{ FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
169

    
170
static int32_t scale_factor_mult2[3][3] = {
171
    SCALE_GEN(4.0 / 3.0), /* 3 steps */
172
    SCALE_GEN(4.0 / 5.0), /* 5 steps */
173
    SCALE_GEN(4.0 / 9.0), /* 9 steps */
174
};
175

    
176
/* 2^(n/4) */
177
static uint32_t scale_factor_mult3[4] = {
178
    FIXR(1.0),
179
    FIXR(1.18920711500272106671),
180
    FIXR(1.41421356237309504880),
181
    FIXR(1.68179283050742908605),
182
};
183

    
184
void ff_mpa_synth_init(MPA_INT *window);
185
static MPA_INT window[512] __attribute__((aligned(16)));
186
    
187
/* layer 1 unscaling */
188
/* n = number of bits of the mantissa minus 1 */
189
static inline int l1_unscale(int n, int mant, int scale_factor)
190
{
191
    int shift, mod;
192
    int64_t val;
193

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

    
203
static inline int l2_unscale_group(int steps, int mant, int scale_factor)
204
{
205
    int shift, mod, val;
206

    
207
    shift = scale_factor_modshift[scale_factor];
208
    mod = shift & 3;
209
    shift >>= 2;
210

    
211
    val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
212
    /* NOTE: at this point, 0 <= shift <= 21 */
213
    if (shift > 0)
214
        val = (val + (1 << (shift - 1))) >> shift;
215
    return val;
216
}
217

    
218
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
219
static inline int l3_unscale(int value, int exponent)
220
{
221
#if FRAC_BITS <= 15    
222
    unsigned int m;
223
#else
224
    uint64_t m;
225
#endif
226
    int e;
227

    
228
    e = table_4_3_exp[value];
229
    e += (exponent >> 2);
230
    e = FRAC_BITS - e;
231
#if FRAC_BITS <= 15    
232
    if (e > 31)
233
        e = 31;
234
#endif
235
    m = table_4_3_value[value];
236
#if FRAC_BITS <= 15    
237
    m = (m * scale_factor_mult3[exponent & 3]);
238
    m = (m + (1 << (e-1))) >> e;
239
    return m;
240
#else
241
    m = MUL64(m, scale_factor_mult3[exponent & 3]);
242
    m = (m + (uint64_t_C(1) << (e-1))) >> e;
243
    return m;
244
#endif
245
}
246

    
247
/* all integer n^(4/3) computation code */
248
#define DEV_ORDER 13
249

    
250
#define POW_FRAC_BITS 24
251
#define POW_FRAC_ONE    (1 << POW_FRAC_BITS)
252
#define POW_FIX(a)   ((int)((a) * POW_FRAC_ONE))
253
#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
254

    
255
static int dev_4_3_coefs[DEV_ORDER];
256

    
257
static int pow_mult3[3] = {
258
    POW_FIX(1.0),
259
    POW_FIX(1.25992104989487316476),
260
    POW_FIX(1.58740105196819947474),
261
};
262

    
263
static void int_pow_init(void)
264
{
265
    int i, a;
266

    
267
    a = POW_FIX(1.0);
268
    for(i=0;i<DEV_ORDER;i++) {
269
        a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
270
        dev_4_3_coefs[i] = a;
271
    }
272
}
273

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

    
319
static int decode_init(AVCodecContext * avctx)
320
{
321
    MPADecodeContext *s = avctx->priv_data;
322
    static int init=0;
323
    int i, j, k;
324

    
325
    if(avctx->antialias_algo == FF_AA_INT)
326
        s->compute_antialias= compute_antialias_integer;
327
    else
328
        s->compute_antialias= compute_antialias_float;
329

    
330
    if (!init && !avctx->parse_only) {
331
        /* scale factors table for layer 1/2 */
332
        for(i=0;i<64;i++) {
333
            int shift, mod;
334
            /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
335
            shift = (i / 3);
336
            mod = i % 3;
337
            scale_factor_modshift[i] = mod | (shift << 2);
338
        }
339

    
340
        /* scale factor multiply for layer 1 */
341
        for(i=0;i<15;i++) {
342
            int n, norm;
343
            n = i + 2;
344
            norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
345
            scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
346
            scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
347
            scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
348
            dprintf("%d: norm=%x s=%x %x %x\n",
349
                    i, norm, 
350
                    scale_factor_mult[i][0],
351
                    scale_factor_mult[i][1],
352
                    scale_factor_mult[i][2]);
353
        }
354
        
355
        ff_mpa_synth_init(window);
356
        
357
        /* huffman decode tables */
358
        huff_code_table[0] = NULL;
359
        for(i=1;i<16;i++) {
360
            const HuffTable *h = &mpa_huff_tables[i];
361
            int xsize, x, y;
362
            unsigned int n;
363
            uint8_t *code_table;
364

    
365
            xsize = h->xsize;
366
            n = xsize * xsize;
367
            /* XXX: fail test */
368
            init_vlc(&huff_vlc[i], 8, n, 
369
                     h->bits, 1, 1, h->codes, 2, 2, 1);
370
            
371
            code_table = av_mallocz(n);
372
            j = 0;
373
            for(x=0;x<xsize;x++) {
374
                for(y=0;y<xsize;y++)
375
                    code_table[j++] = (x << 4) | y;
376
            }
377
            huff_code_table[i] = code_table;
378
        }
379
        for(i=0;i<2;i++) {
380
            init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16, 
381
                     mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
382
        }
383

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

    
393
        /* compute n ^ (4/3) and store it in mantissa/exp format */
394
        table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
395
        if(!table_4_3_exp)
396
            return -1;
397
        table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
398
        if(!table_4_3_value)
399
            return -1;
400
        
401
        int_pow_init();
402
        for(i=1;i<TABLE_4_3_SIZE;i++) {
403
            int e, m;
404
            m = int_pow(i, &e);
405
#if 0
406
            /* test code */
407
            {
408
                double f, fm;
409
                int e1, m1;
410
                f = pow((double)i, 4.0 / 3.0);
411
                fm = frexp(f, &e1);
412
                m1 = FIXR(2 * fm);
413
#if FRAC_BITS <= 15
414
                if ((unsigned short)m1 != m1) {
415
                    m1 = m1 >> 1;
416
                    e1++;
417
                }
418
#endif
419
                e1--;
420
                if (m != m1 || e != e1) {
421
                    printf("%4d: m=%x m1=%x e=%d e1=%d\n",
422
                           i, m, m1, e, e1);
423
                }
424
            }
425
#endif
426
            /* normalized to FRAC_BITS */
427
            table_4_3_value[i] = m;
428
            table_4_3_exp[i] = e;
429
        }
430
        
431
        for(i=0;i<7;i++) {
432
            float f;
433
            int v;
434
            if (i != 6) {
435
                f = tan((double)i * M_PI / 12.0);
436
                v = FIXR(f / (1.0 + f));
437
            } else {
438
                v = FIXR(1.0);
439
            }
440
            is_table[0][i] = v;
441
            is_table[1][6 - i] = v;
442
        }
443
        /* invalid values */
444
        for(i=7;i<16;i++)
445
            is_table[0][i] = is_table[1][i] = 0.0;
446

    
447
        for(i=0;i<16;i++) {
448
            double f;
449
            int e, k;
450

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

    
462
        for(i=0;i<8;i++) {
463
            float ci, cs, ca;
464
            ci = ci_table[i];
465
            cs = 1.0 / sqrt(1.0 + ci * ci);
466
            ca = cs * ci;
467
            csa_table[i][0] = FIX(cs);
468
            csa_table[i][1] = FIX(ca);
469
            csa_table[i][2] = FIX(ca) + FIX(cs);
470
            csa_table[i][3] = FIX(ca) - FIX(cs); 
471
            csa_table_float[i][0] = cs;
472
            csa_table_float[i][1] = ca;
473
            csa_table_float[i][2] = ca + cs;
474
            csa_table_float[i][3] = ca - cs; 
475
//            printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
476
        }
477

    
478
        /* compute mdct windows */
479
        for(i=0;i<36;i++) {
480
            int v;
481
            v = FIXR(sin(M_PI * (i + 0.5) / 36.0));
482
            mdct_win[0][i] = v;
483
            mdct_win[1][i] = v;
484
            mdct_win[3][i] = v;
485
        }
486
        for(i=0;i<6;i++) {
487
            mdct_win[1][18 + i] = FIXR(1.0);
488
            mdct_win[1][24 + i] = FIXR(sin(M_PI * ((i + 6) + 0.5) / 12.0));
489
            mdct_win[1][30 + i] = FIXR(0.0);
490

    
491
            mdct_win[3][i] = FIXR(0.0);
492
            mdct_win[3][6 + i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
493
            mdct_win[3][12 + i] = FIXR(1.0);
494
        }
495

    
496
        for(i=0;i<12;i++)
497
            mdct_win[2][i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
498
        
499
        /* NOTE: we do frequency inversion adter the MDCT by changing
500
           the sign of the right window coefs */
501
        for(j=0;j<4;j++) {
502
            for(i=0;i<36;i+=2) {
503
                mdct_win[j + 4][i] = mdct_win[j][i];
504
                mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
505
            }
506
        }
507

    
508
#if defined(DEBUG)
509
        for(j=0;j<8;j++) {
510
            printf("win%d=\n", j);
511
            for(i=0;i<36;i++)
512
                printf("%f, ", (double)mdct_win[j][i] / FRAC_ONE);
513
            printf("\n");
514
        }
515
#endif
516
        init = 1;
517
    }
518

    
519
    s->inbuf_index = 0;
520
    s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
521
    s->inbuf_ptr = s->inbuf;
522
#ifdef DEBUG
523
    s->frame_count = 0;
524
#endif
525
    if (avctx->codec_id == CODEC_ID_MP3ADU)
526
        s->adu_mode = 1;
527
    return 0;
528
}
529

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

    
532
/* cos(i*pi/64) */
533

    
534
#define COS0_0  FIXR(0.50060299823519630134)
535
#define COS0_1  FIXR(0.50547095989754365998)
536
#define COS0_2  FIXR(0.51544730992262454697)
537
#define COS0_3  FIXR(0.53104259108978417447)
538
#define COS0_4  FIXR(0.55310389603444452782)
539
#define COS0_5  FIXR(0.58293496820613387367)
540
#define COS0_6  FIXR(0.62250412303566481615)
541
#define COS0_7  FIXR(0.67480834145500574602)
542
#define COS0_8  FIXR(0.74453627100229844977)
543
#define COS0_9  FIXR(0.83934964541552703873)
544
#define COS0_10 FIXR(0.97256823786196069369)
545
#define COS0_11 FIXR(1.16943993343288495515)
546
#define COS0_12 FIXR(1.48416461631416627724)
547
#define COS0_13 FIXR(2.05778100995341155085)
548
#define COS0_14 FIXR(3.40760841846871878570)
549
#define COS0_15 FIXR(10.19000812354805681150)
550

    
551
#define COS1_0 FIXR(0.50241928618815570551)
552
#define COS1_1 FIXR(0.52249861493968888062)
553
#define COS1_2 FIXR(0.56694403481635770368)
554
#define COS1_3 FIXR(0.64682178335999012954)
555
#define COS1_4 FIXR(0.78815462345125022473)
556
#define COS1_5 FIXR(1.06067768599034747134)
557
#define COS1_6 FIXR(1.72244709823833392782)
558
#define COS1_7 FIXR(5.10114861868916385802)
559

    
560
#define COS2_0 FIXR(0.50979557910415916894)
561
#define COS2_1 FIXR(0.60134488693504528054)
562
#define COS2_2 FIXR(0.89997622313641570463)
563
#define COS2_3 FIXR(2.56291544774150617881)
564

    
565
#define COS3_0 FIXR(0.54119610014619698439)
566
#define COS3_1 FIXR(1.30656296487637652785)
567

    
568
#define COS4_0 FIXR(0.70710678118654752439)
569

    
570
/* butterfly operator */
571
#define BF(a, b, c)\
572
{\
573
    tmp0 = tab[a] + tab[b];\
574
    tmp1 = tab[a] - tab[b];\
575
    tab[a] = tmp0;\
576
    tab[b] = MULL(tmp1, c);\
577
}
578

    
579
#define BF1(a, b, c, d)\
580
{\
581
    BF(a, b, COS4_0);\
582
    BF(c, d, -COS4_0);\
583
    tab[c] += tab[d];\
584
}
585

    
586
#define BF2(a, b, c, d)\
587
{\
588
    BF(a, b, COS4_0);\
589
    BF(c, d, -COS4_0);\
590
    tab[c] += tab[d];\
591
    tab[a] += tab[c];\
592
    tab[c] += tab[b];\
593
    tab[b] += tab[d];\
594
}
595

    
596
#define ADD(a, b) tab[a] += tab[b]
597

    
598
/* DCT32 without 1/sqrt(2) coef zero scaling. */
599
static void dct32(int32_t *out, int32_t *tab)
600
{
601
    int tmp0, tmp1;
602

    
603
    /* pass 1 */
604
    BF(0, 31, COS0_0);
605
    BF(1, 30, COS0_1);
606
    BF(2, 29, COS0_2);
607
    BF(3, 28, COS0_3);
608
    BF(4, 27, COS0_4);
609
    BF(5, 26, COS0_5);
610
    BF(6, 25, COS0_6);
611
    BF(7, 24, COS0_7);
612
    BF(8, 23, COS0_8);
613
    BF(9, 22, COS0_9);
614
    BF(10, 21, COS0_10);
615
    BF(11, 20, COS0_11);
616
    BF(12, 19, COS0_12);
617
    BF(13, 18, COS0_13);
618
    BF(14, 17, COS0_14);
619
    BF(15, 16, COS0_15);
620

    
621
    /* pass 2 */
622
    BF(0, 15, COS1_0);
623
    BF(1, 14, COS1_1);
624
    BF(2, 13, COS1_2);
625
    BF(3, 12, COS1_3);
626
    BF(4, 11, COS1_4);
627
    BF(5, 10, COS1_5);
628
    BF(6,  9, COS1_6);
629
    BF(7,  8, COS1_7);
630
    
631
    BF(16, 31, -COS1_0);
632
    BF(17, 30, -COS1_1);
633
    BF(18, 29, -COS1_2);
634
    BF(19, 28, -COS1_3);
635
    BF(20, 27, -COS1_4);
636
    BF(21, 26, -COS1_5);
637
    BF(22, 25, -COS1_6);
638
    BF(23, 24, -COS1_7);
639
    
640
    /* pass 3 */
641
    BF(0, 7, COS2_0);
642
    BF(1, 6, COS2_1);
643
    BF(2, 5, COS2_2);
644
    BF(3, 4, COS2_3);
645
    
646
    BF(8, 15, -COS2_0);
647
    BF(9, 14, -COS2_1);
648
    BF(10, 13, -COS2_2);
649
    BF(11, 12, -COS2_3);
650
    
651
    BF(16, 23, COS2_0);
652
    BF(17, 22, COS2_1);
653
    BF(18, 21, COS2_2);
654
    BF(19, 20, COS2_3);
655
    
656
    BF(24, 31, -COS2_0);
657
    BF(25, 30, -COS2_1);
658
    BF(26, 29, -COS2_2);
659
    BF(27, 28, -COS2_3);
660

    
661
    /* pass 4 */
662
    BF(0, 3, COS3_0);
663
    BF(1, 2, COS3_1);
664
    
665
    BF(4, 7, -COS3_0);
666
    BF(5, 6, -COS3_1);
667
    
668
    BF(8, 11, COS3_0);
669
    BF(9, 10, COS3_1);
670
    
671
    BF(12, 15, -COS3_0);
672
    BF(13, 14, -COS3_1);
673
    
674
    BF(16, 19, COS3_0);
675
    BF(17, 18, COS3_1);
676
    
677
    BF(20, 23, -COS3_0);
678
    BF(21, 22, -COS3_1);
679
    
680
    BF(24, 27, COS3_0);
681
    BF(25, 26, COS3_1);
682
    
683
    BF(28, 31, -COS3_0);
684
    BF(29, 30, -COS3_1);
685
    
686
    /* pass 5 */
687
    BF1(0, 1, 2, 3);
688
    BF2(4, 5, 6, 7);
689
    BF1(8, 9, 10, 11);
690
    BF2(12, 13, 14, 15);
691
    BF1(16, 17, 18, 19);
692
    BF2(20, 21, 22, 23);
693
    BF1(24, 25, 26, 27);
694
    BF2(28, 29, 30, 31);
695
    
696
    /* pass 6 */
697
    
698
    ADD( 8, 12);
699
    ADD(12, 10);
700
    ADD(10, 14);
701
    ADD(14,  9);
702
    ADD( 9, 13);
703
    ADD(13, 11);
704
    ADD(11, 15);
705

    
706
    out[ 0] = tab[0];
707
    out[16] = tab[1];
708
    out[ 8] = tab[2];
709
    out[24] = tab[3];
710
    out[ 4] = tab[4];
711
    out[20] = tab[5];
712
    out[12] = tab[6];
713
    out[28] = tab[7];
714
    out[ 2] = tab[8];
715
    out[18] = tab[9];
716
    out[10] = tab[10];
717
    out[26] = tab[11];
718
    out[ 6] = tab[12];
719
    out[22] = tab[13];
720
    out[14] = tab[14];
721
    out[30] = tab[15];
722
    
723
    ADD(24, 28);
724
    ADD(28, 26);
725
    ADD(26, 30);
726
    ADD(30, 25);
727
    ADD(25, 29);
728
    ADD(29, 27);
729
    ADD(27, 31);
730

    
731
    out[ 1] = tab[16] + tab[24];
732
    out[17] = tab[17] + tab[25];
733
    out[ 9] = tab[18] + tab[26];
734
    out[25] = tab[19] + tab[27];
735
    out[ 5] = tab[20] + tab[28];
736
    out[21] = tab[21] + tab[29];
737
    out[13] = tab[22] + tab[30];
738
    out[29] = tab[23] + tab[31];
739
    out[ 3] = tab[24] + tab[20];
740
    out[19] = tab[25] + tab[21];
741
    out[11] = tab[26] + tab[22];
742
    out[27] = tab[27] + tab[23];
743
    out[ 7] = tab[28] + tab[18];
744
    out[23] = tab[29] + tab[19];
745
    out[15] = tab[30] + tab[17];
746
    out[31] = tab[31];
747
}
748

    
749
#define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
750

    
751
#if FRAC_BITS <= 15
752

    
753
static inline int round_sample(int *sum)
754
{
755
    int sum1;
756
    sum1 = (*sum) >> OUT_SHIFT;
757
    *sum &= (1<<OUT_SHIFT)-1;
758
    if (sum1 < -32768)
759
        sum1 = -32768;
760
    else if (sum1 > 32767)
761
        sum1 = 32767;
762
    return sum1;
763
}
764

    
765
#if defined(ARCH_POWERPC_405)
766

    
767
/* signed 16x16 -> 32 multiply add accumulate */
768
#define MACS(rt, ra, rb) \
769
    asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
770

    
771
/* signed 16x16 -> 32 multiply */
772
#define MULS(ra, rb) \
773
    ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
774

    
775
#else
776

    
777
/* signed 16x16 -> 32 multiply add accumulate */
778
#define MACS(rt, ra, rb) rt += (ra) * (rb)
779

    
780
/* signed 16x16 -> 32 multiply */
781
#define MULS(ra, rb) ((ra) * (rb))
782

    
783
#endif
784

    
785
#else
786

    
787
static inline int round_sample(int64_t *sum) 
788
{
789
    int sum1;
790
    sum1 = (int)((*sum) >> OUT_SHIFT);
791
    *sum &= (1<<OUT_SHIFT)-1;
792
    if (sum1 < -32768)
793
        sum1 = -32768;
794
    else if (sum1 > 32767)
795
        sum1 = 32767;
796
    return sum1;
797
}
798

    
799
#define MULS(ra, rb) MUL64(ra, rb)
800

    
801
#endif
802

    
803
#define SUM8(sum, op, w, p) \
804
{                                               \
805
    sum op MULS((w)[0 * 64], p[0 * 64]);\
806
    sum op MULS((w)[1 * 64], p[1 * 64]);\
807
    sum op MULS((w)[2 * 64], p[2 * 64]);\
808
    sum op MULS((w)[3 * 64], p[3 * 64]);\
809
    sum op MULS((w)[4 * 64], p[4 * 64]);\
810
    sum op MULS((w)[5 * 64], p[5 * 64]);\
811
    sum op MULS((w)[6 * 64], p[6 * 64]);\
812
    sum op MULS((w)[7 * 64], p[7 * 64]);\
813
}
814

    
815
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
816
{                                               \
817
    int tmp;\
818
    tmp = p[0 * 64];\
819
    sum1 op1 MULS((w1)[0 * 64], tmp);\
820
    sum2 op2 MULS((w2)[0 * 64], tmp);\
821
    tmp = p[1 * 64];\
822
    sum1 op1 MULS((w1)[1 * 64], tmp);\
823
    sum2 op2 MULS((w2)[1 * 64], tmp);\
824
    tmp = p[2 * 64];\
825
    sum1 op1 MULS((w1)[2 * 64], tmp);\
826
    sum2 op2 MULS((w2)[2 * 64], tmp);\
827
    tmp = p[3 * 64];\
828
    sum1 op1 MULS((w1)[3 * 64], tmp);\
829
    sum2 op2 MULS((w2)[3 * 64], tmp);\
830
    tmp = p[4 * 64];\
831
    sum1 op1 MULS((w1)[4 * 64], tmp);\
832
    sum2 op2 MULS((w2)[4 * 64], tmp);\
833
    tmp = p[5 * 64];\
834
    sum1 op1 MULS((w1)[5 * 64], tmp);\
835
    sum2 op2 MULS((w2)[5 * 64], tmp);\
836
    tmp = p[6 * 64];\
837
    sum1 op1 MULS((w1)[6 * 64], tmp);\
838
    sum2 op2 MULS((w2)[6 * 64], tmp);\
839
    tmp = p[7 * 64];\
840
    sum1 op1 MULS((w1)[7 * 64], tmp);\
841
    sum2 op2 MULS((w2)[7 * 64], tmp);\
842
}
843

    
844
void ff_mpa_synth_init(MPA_INT *window)
845
{
846
    int i;
847

    
848
    /* max = 18760, max sum over all 16 coefs : 44736 */
849
    for(i=0;i<257;i++) {
850
        int v;
851
        v = mpa_enwindow[i];
852
#if WFRAC_BITS < 16
853
        v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
854
#endif
855
        window[i] = v;
856
        if ((i & 63) != 0)
857
            v = -v;
858
        if (i != 0)
859
            window[512 - i] = v;
860
    }        
861
}
862

    
863
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
864
   32 samples. */
865
/* XXX: optimize by avoiding ring buffer usage */
866
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
867
                         MPA_INT *window,
868
                         int16_t *samples, int incr, 
869
                         int32_t sb_samples[SBLIMIT])
870
{
871
    int32_t tmp[32];
872
    register MPA_INT *synth_buf;
873
    register const MPA_INT *w, *w2, *p;
874
    int j, offset, v;
875
    int16_t *samples2;
876
#if FRAC_BITS <= 15
877
    int sum, sum2;
878
#else
879
    int64_t sum, sum2;
880
#endif
881

    
882
    dct32(tmp, sb_samples);
883
    
884
    offset = *synth_buf_offset;
885
    synth_buf = synth_buf_ptr + offset;
886

    
887
    for(j=0;j<32;j++) {
888
        v = tmp[j];
889
#if FRAC_BITS <= 15
890
        /* NOTE: can cause a loss in precision if very high amplitude
891
           sound */
892
        if (v > 32767)
893
            v = 32767;
894
        else if (v < -32768)
895
            v = -32768;
896
#endif
897
        synth_buf[j] = v;
898
    }
899
    /* copy to avoid wrap */
900
    memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
901

    
902
    samples2 = samples + 31 * incr;
903
    w = window;
904
    w2 = window + 31;
905

    
906
    sum = s1->dither_state;
907
    p = synth_buf + 16;
908
    SUM8(sum, +=, w, p);
909
    p = synth_buf + 48;
910
    SUM8(sum, -=, w + 32, p);
911
    *samples = round_sample(&sum);
912
    samples += incr;
913
    w++;
914

    
915
    /* we calculate two samples at the same time to avoid one memory
916
       access per two sample */
917
    for(j=1;j<16;j++) {
918
        sum2 = 0;
919
        p = synth_buf + 16 + j;
920
        SUM8P2(sum, +=, sum2, -=, w, w2, p);
921
        p = synth_buf + 48 - j;
922
        SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
923

    
924
        *samples = round_sample(&sum);
925
        samples += incr;
926
        sum += sum2;
927
        *samples2 = round_sample(&sum);
928
        samples2 -= incr;
929
        w++;
930
        w2--;
931
    }
932
    
933
    p = synth_buf + 32;
934
    SUM8(sum, -=, w + 32, p);
935
    *samples = round_sample(&sum);
936
    s1->dither_state= sum;
937

    
938
    offset = (offset - 32) & 511;
939
    *synth_buf_offset = offset;
940
}
941

    
942
/* cos(pi*i/24) */
943
#define C1  FIXR(0.99144486137381041114)
944
#define C3  FIXR(0.92387953251128675612)
945
#define C5  FIXR(0.79335334029123516458)
946
#define C7  FIXR(0.60876142900872063941)
947
#define C9  FIXR(0.38268343236508977173)
948
#define C11 FIXR(0.13052619222005159154)
949

    
950
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
951
   cases. */
952
static void imdct12(int *out, int *in)
953
{
954
    int tmp;
955
    int64_t in1_3, in1_9, in4_3, in4_9;
956

    
957
    in1_3 = MUL64(in[1], C3);
958
    in1_9 = MUL64(in[1], C9);
959
    in4_3 = MUL64(in[4], C3);
960
    in4_9 = MUL64(in[4], C9);
961
    
962
    tmp = FRAC_RND(MUL64(in[0], C7) - in1_3 - MUL64(in[2], C11) + 
963
                   MUL64(in[3], C1) - in4_9 - MUL64(in[5], C5));
964
    out[0] = tmp;
965
    out[5] = -tmp;
966
    tmp = FRAC_RND(MUL64(in[0] - in[3], C9) - in1_3 + 
967
                   MUL64(in[2] + in[5], C3) - in4_9);
968
    out[1] = tmp;
969
    out[4] = -tmp;
970
    tmp = FRAC_RND(MUL64(in[0], C11) - in1_9 + MUL64(in[2], C7) -
971
                   MUL64(in[3], C5) + in4_3 - MUL64(in[5], C1));
972
    out[2] = tmp;
973
    out[3] = -tmp;
974
    tmp = FRAC_RND(MUL64(-in[0], C5) + in1_9 + MUL64(in[2], C1) + 
975
                   MUL64(in[3], C11) - in4_3 - MUL64(in[5], C7));
976
    out[6] = tmp;
977
    out[11] = tmp;
978
    tmp = FRAC_RND(MUL64(-in[0] + in[3], C3) - in1_9 + 
979
                   MUL64(in[2] + in[5], C9) + in4_3);
980
    out[7] = tmp;
981
    out[10] = tmp;
982
    tmp = FRAC_RND(-MUL64(in[0], C1) - in1_3 - MUL64(in[2], C5) -
983
                   MUL64(in[3], C7) - in4_9 - MUL64(in[5], C11));
984
    out[8] = tmp;
985
    out[9] = tmp;
986
}
987

    
988
#undef C1
989
#undef C3
990
#undef C5
991
#undef C7
992
#undef C9
993
#undef C11
994

    
995
/* cos(pi*i/18) */
996
#define C1 FIXR(0.98480775301220805936)
997
#define C2 FIXR(0.93969262078590838405)
998
#define C3 FIXR(0.86602540378443864676)
999
#define C4 FIXR(0.76604444311897803520)
1000
#define C5 FIXR(0.64278760968653932632)
1001
#define C6 FIXR(0.5)
1002
#define C7 FIXR(0.34202014332566873304)
1003
#define C8 FIXR(0.17364817766693034885)
1004

    
1005
/* 0.5 / cos(pi*(2*i+1)/36) */
1006
static const int icos36[9] = {
1007
    FIXR(0.50190991877167369479),
1008
    FIXR(0.51763809020504152469),
1009
    FIXR(0.55168895948124587824),
1010
    FIXR(0.61038729438072803416),
1011
    FIXR(0.70710678118654752439),
1012
    FIXR(0.87172339781054900991),
1013
    FIXR(1.18310079157624925896),
1014
    FIXR(1.93185165257813657349),
1015
    FIXR(5.73685662283492756461),
1016
};
1017

    
1018
static const int icos72[18] = {
1019
    /* 0.5 / cos(pi*(2*i+19)/72) */
1020
    FIXR(0.74009361646113053152),
1021
    FIXR(0.82133981585229078570),
1022
    FIXR(0.93057949835178895673),
1023
    FIXR(1.08284028510010010928),
1024
    FIXR(1.30656296487637652785),
1025
    FIXR(1.66275476171152078719),
1026
    FIXR(2.31011315767264929558),
1027
    FIXR(3.83064878777019433457),
1028
    FIXR(11.46279281302667383546),
1029

    
1030
    /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
1031
    FIXR(-0.67817085245462840086),
1032
    FIXR(-0.63023620700513223342),
1033
    FIXR(-0.59284452371708034528),
1034
    FIXR(-0.56369097343317117734),
1035
    FIXR(-0.54119610014619698439),
1036
    FIXR(-0.52426456257040533932),
1037
    FIXR(-0.51213975715725461845),
1038
    FIXR(-0.50431448029007636036),
1039
    FIXR(-0.50047634258165998492),
1040
};
1041

    
1042
/* using Lee like decomposition followed by hand coded 9 points DCT */
1043
static void imdct36(int *out, int *in)
1044
{
1045
    int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1046
    int tmp[18], *tmp1, *in1;
1047
    int64_t in3_3, in6_6;
1048

    
1049
    for(i=17;i>=1;i--)
1050
        in[i] += in[i-1];
1051
    for(i=17;i>=3;i-=2)
1052
        in[i] += in[i-2];
1053

    
1054
    for(j=0;j<2;j++) {
1055
        tmp1 = tmp + j;
1056
        in1 = in + j;
1057

    
1058
        in3_3 = MUL64(in1[2*3], C3);
1059
        in6_6 = MUL64(in1[2*6], C6);
1060

    
1061
        tmp1[0] = FRAC_RND(MUL64(in1[2*1], C1) + in3_3 + 
1062
                           MUL64(in1[2*5], C5) + MUL64(in1[2*7], C7));
1063
        tmp1[2] = in1[2*0] + FRAC_RND(MUL64(in1[2*2], C2) + 
1064
                                      MUL64(in1[2*4], C4) + in6_6 + 
1065
                                      MUL64(in1[2*8], C8));
1066
        tmp1[4] = FRAC_RND(MUL64(in1[2*1] - in1[2*5] - in1[2*7], C3));
1067
        tmp1[6] = FRAC_RND(MUL64(in1[2*2] - in1[2*4] - in1[2*8], C6)) - 
1068
            in1[2*6] + in1[2*0];
1069
        tmp1[8] = FRAC_RND(MUL64(in1[2*1], C5) - in3_3 - 
1070
                           MUL64(in1[2*5], C7) + MUL64(in1[2*7], C1));
1071
        tmp1[10] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C8) - 
1072
                                       MUL64(in1[2*4], C2) + in6_6 + 
1073
                                       MUL64(in1[2*8], C4));
1074
        tmp1[12] = FRAC_RND(MUL64(in1[2*1], C7) - in3_3 + 
1075
                            MUL64(in1[2*5], C1) - 
1076
                            MUL64(in1[2*7], C5));
1077
        tmp1[14] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C4) + 
1078
                                       MUL64(in1[2*4], C8) + in6_6 - 
1079
                                       MUL64(in1[2*8], C2));
1080
        tmp1[16] = in1[2*0] - in1[2*2] + in1[2*4] - in1[2*6] + in1[2*8];
1081
    }
1082

    
1083
    i = 0;
1084
    for(j=0;j<4;j++) {
1085
        t0 = tmp[i];
1086
        t1 = tmp[i + 2];
1087
        s0 = t1 + t0;
1088
        s2 = t1 - t0;
1089

    
1090
        t2 = tmp[i + 1];
1091
        t3 = tmp[i + 3];
1092
        s1 = MULL(t3 + t2, icos36[j]);
1093
        s3 = MULL(t3 - t2, icos36[8 - j]);
1094
        
1095
        t0 = MULL(s0 + s1, icos72[9 + 8 - j]);
1096
        t1 = MULL(s0 - s1, icos72[8 - j]);
1097
        out[18 + 9 + j] = t0;
1098
        out[18 + 8 - j] = t0;
1099
        out[9 + j] = -t1;
1100
        out[8 - j] = t1;
1101
        
1102
        t0 = MULL(s2 + s3, icos72[9+j]);
1103
        t1 = MULL(s2 - s3, icos72[j]);
1104
        out[18 + 9 + (8 - j)] = t0;
1105
        out[18 + j] = t0;
1106
        out[9 + (8 - j)] = -t1;
1107
        out[j] = t1;
1108
        i += 4;
1109
    }
1110

    
1111
    s0 = tmp[16];
1112
    s1 = MULL(tmp[17], icos36[4]);
1113
    t0 = MULL(s0 + s1, icos72[9 + 4]);
1114
    t1 = MULL(s0 - s1, icos72[4]);
1115
    out[18 + 9 + 4] = t0;
1116
    out[18 + 8 - 4] = t0;
1117
    out[9 + 4] = -t1;
1118
    out[8 - 4] = t1;
1119
}
1120

    
1121
/* header decoding. MUST check the header before because no
1122
   consistency check is done there. Return 1 if free format found and
1123
   that the frame size must be computed externally */
1124
static int decode_header(MPADecodeContext *s, uint32_t header)
1125
{
1126
    int sample_rate, frame_size, mpeg25, padding;
1127
    int sample_rate_index, bitrate_index;
1128
    if (header & (1<<20)) {
1129
        s->lsf = (header & (1<<19)) ? 0 : 1;
1130
        mpeg25 = 0;
1131
    } else {
1132
        s->lsf = 1;
1133
        mpeg25 = 1;
1134
    }
1135
    
1136
    s->layer = 4 - ((header >> 17) & 3);
1137
    /* extract frequency */
1138
    sample_rate_index = (header >> 10) & 3;
1139
    sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1140
    sample_rate_index += 3 * (s->lsf + mpeg25);
1141
    s->sample_rate_index = sample_rate_index;
1142
    s->error_protection = ((header >> 16) & 1) ^ 1;
1143
    s->sample_rate = sample_rate;
1144

    
1145
    bitrate_index = (header >> 12) & 0xf;
1146
    padding = (header >> 9) & 1;
1147
    //extension = (header >> 8) & 1;
1148
    s->mode = (header >> 6) & 3;
1149
    s->mode_ext = (header >> 4) & 3;
1150
    //copyright = (header >> 3) & 1;
1151
    //original = (header >> 2) & 1;
1152
    //emphasis = header & 3;
1153

    
1154
    if (s->mode == MPA_MONO)
1155
        s->nb_channels = 1;
1156
    else
1157
        s->nb_channels = 2;
1158
    
1159
    if (bitrate_index != 0) {
1160
        frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1161
        s->bit_rate = frame_size * 1000;
1162
        switch(s->layer) {
1163
        case 1:
1164
            frame_size = (frame_size * 12000) / sample_rate;
1165
            frame_size = (frame_size + padding) * 4;
1166
            break;
1167
        case 2:
1168
            frame_size = (frame_size * 144000) / sample_rate;
1169
            frame_size += padding;
1170
            break;
1171
        default:
1172
        case 3:
1173
            frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1174
            frame_size += padding;
1175
            break;
1176
        }
1177
        s->frame_size = frame_size;
1178
    } else {
1179
        /* if no frame size computed, signal it */
1180
        if (!s->free_format_frame_size)
1181
            return 1;
1182
        /* free format: compute bitrate and real frame size from the
1183
           frame size we extracted by reading the bitstream */
1184
        s->frame_size = s->free_format_frame_size;
1185
        switch(s->layer) {
1186
        case 1:
1187
            s->frame_size += padding  * 4;
1188
            s->bit_rate = (s->frame_size * sample_rate) / 48000;
1189
            break;
1190
        case 2:
1191
            s->frame_size += padding;
1192
            s->bit_rate = (s->frame_size * sample_rate) / 144000;
1193
            break;
1194
        default:
1195
        case 3:
1196
            s->frame_size += padding;
1197
            s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1198
            break;
1199
        }
1200
    }
1201
    
1202
#if defined(DEBUG)
1203
    printf("layer%d, %d Hz, %d kbits/s, ",
1204
           s->layer, s->sample_rate, s->bit_rate);
1205
    if (s->nb_channels == 2) {
1206
        if (s->layer == 3) {
1207
            if (s->mode_ext & MODE_EXT_MS_STEREO)
1208
                printf("ms-");
1209
            if (s->mode_ext & MODE_EXT_I_STEREO)
1210
                printf("i-");
1211
        }
1212
        printf("stereo");
1213
    } else {
1214
        printf("mono");
1215
    }
1216
    printf("\n");
1217
#endif
1218
    return 0;
1219
}
1220

    
1221
/* useful helper to get mpeg audio stream infos. Return -1 if error in
1222
   header, otherwise the coded frame size in bytes */
1223
int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1224
{
1225
    MPADecodeContext s1, *s = &s1;
1226
    memset( s, 0, sizeof(MPADecodeContext) );
1227

    
1228
    if (ff_mpa_check_header(head) != 0)
1229
        return -1;
1230

    
1231
    if (decode_header(s, head) != 0) {
1232
        return -1;
1233
    }
1234

    
1235
    switch(s->layer) {
1236
    case 1:
1237
        avctx->frame_size = 384;
1238
        break;
1239
    case 2:
1240
        avctx->frame_size = 1152;
1241
        break;
1242
    default:
1243
    case 3:
1244
        if (s->lsf)
1245
            avctx->frame_size = 576;
1246
        else
1247
            avctx->frame_size = 1152;
1248
        break;
1249
    }
1250

    
1251
    avctx->sample_rate = s->sample_rate;
1252
    avctx->channels = s->nb_channels;
1253
    avctx->bit_rate = s->bit_rate;
1254
    avctx->sub_id = s->layer;
1255
    return s->frame_size;
1256
}
1257

    
1258
/* return the number of decoded frames */
1259
static int mp_decode_layer1(MPADecodeContext *s)
1260
{
1261
    int bound, i, v, n, ch, j, mant;
1262
    uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1263
    uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1264

    
1265
    if (s->mode == MPA_JSTEREO) 
1266
        bound = (s->mode_ext + 1) * 4;
1267
    else
1268
        bound = SBLIMIT;
1269

    
1270
    /* allocation bits */
1271
    for(i=0;i<bound;i++) {
1272
        for(ch=0;ch<s->nb_channels;ch++) {
1273
            allocation[ch][i] = get_bits(&s->gb, 4);
1274
        }
1275
    }
1276
    for(i=bound;i<SBLIMIT;i++) {
1277
        allocation[0][i] = get_bits(&s->gb, 4);
1278
    }
1279

    
1280
    /* scale factors */
1281
    for(i=0;i<bound;i++) {
1282
        for(ch=0;ch<s->nb_channels;ch++) {
1283
            if (allocation[ch][i])
1284
                scale_factors[ch][i] = get_bits(&s->gb, 6);
1285
        }
1286
    }
1287
    for(i=bound;i<SBLIMIT;i++) {
1288
        if (allocation[0][i]) {
1289
            scale_factors[0][i] = get_bits(&s->gb, 6);
1290
            scale_factors[1][i] = get_bits(&s->gb, 6);
1291
        }
1292
    }
1293
    
1294
    /* compute samples */
1295
    for(j=0;j<12;j++) {
1296
        for(i=0;i<bound;i++) {
1297
            for(ch=0;ch<s->nb_channels;ch++) {
1298
                n = allocation[ch][i];
1299
                if (n) {
1300
                    mant = get_bits(&s->gb, n + 1);
1301
                    v = l1_unscale(n, mant, scale_factors[ch][i]);
1302
                } else {
1303
                    v = 0;
1304
                }
1305
                s->sb_samples[ch][j][i] = v;
1306
            }
1307
        }
1308
        for(i=bound;i<SBLIMIT;i++) {
1309
            n = allocation[0][i];
1310
            if (n) {
1311
                mant = get_bits(&s->gb, n + 1);
1312
                v = l1_unscale(n, mant, scale_factors[0][i]);
1313
                s->sb_samples[0][j][i] = v;
1314
                v = l1_unscale(n, mant, scale_factors[1][i]);
1315
                s->sb_samples[1][j][i] = v;
1316
            } else {
1317
                s->sb_samples[0][j][i] = 0;
1318
                s->sb_samples[1][j][i] = 0;
1319
            }
1320
        }
1321
    }
1322
    return 12;
1323
}
1324

    
1325
/* bitrate is in kb/s */
1326
int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1327
{
1328
    int ch_bitrate, table;
1329
    
1330
    ch_bitrate = bitrate / nb_channels;
1331
    if (!lsf) {
1332
        if ((freq == 48000 && ch_bitrate >= 56) ||
1333
            (ch_bitrate >= 56 && ch_bitrate <= 80)) 
1334
            table = 0;
1335
        else if (freq != 48000 && ch_bitrate >= 96) 
1336
            table = 1;
1337
        else if (freq != 32000 && ch_bitrate <= 48) 
1338
            table = 2;
1339
        else 
1340
            table = 3;
1341
    } else {
1342
        table = 4;
1343
    }
1344
    return table;
1345
}
1346

    
1347
static int mp_decode_layer2(MPADecodeContext *s)
1348
{
1349
    int sblimit; /* number of used subbands */
1350
    const unsigned char *alloc_table;
1351
    int table, bit_alloc_bits, i, j, ch, bound, v;
1352
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1353
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1354
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1355
    int scale, qindex, bits, steps, k, l, m, b;
1356

    
1357
    /* select decoding table */
1358
    table = l2_select_table(s->bit_rate / 1000, s->nb_channels, 
1359
                            s->sample_rate, s->lsf);
1360
    sblimit = sblimit_table[table];
1361
    alloc_table = alloc_tables[table];
1362

    
1363
    if (s->mode == MPA_JSTEREO) 
1364
        bound = (s->mode_ext + 1) * 4;
1365
    else
1366
        bound = sblimit;
1367

    
1368
    dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1369

    
1370
    /* sanity check */
1371
    if( bound > sblimit ) bound = sblimit;
1372

    
1373
    /* parse bit allocation */
1374
    j = 0;
1375
    for(i=0;i<bound;i++) {
1376
        bit_alloc_bits = alloc_table[j];
1377
        for(ch=0;ch<s->nb_channels;ch++) {
1378
            bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1379
        }
1380
        j += 1 << bit_alloc_bits;
1381
    }
1382
    for(i=bound;i<sblimit;i++) {
1383
        bit_alloc_bits = alloc_table[j];
1384
        v = get_bits(&s->gb, bit_alloc_bits);
1385
        bit_alloc[0][i] = v;
1386
        bit_alloc[1][i] = v;
1387
        j += 1 << bit_alloc_bits;
1388
    }
1389

    
1390
#ifdef DEBUG
1391
    {
1392
        for(ch=0;ch<s->nb_channels;ch++) {
1393
            for(i=0;i<sblimit;i++)
1394
                printf(" %d", bit_alloc[ch][i]);
1395
            printf("\n");
1396
        }
1397
    }
1398
#endif
1399

    
1400
    /* scale codes */
1401
    for(i=0;i<sblimit;i++) {
1402
        for(ch=0;ch<s->nb_channels;ch++) {
1403
            if (bit_alloc[ch][i]) 
1404
                scale_code[ch][i] = get_bits(&s->gb, 2);
1405
        }
1406
    }
1407
    
1408
    /* scale factors */
1409
    for(i=0;i<sblimit;i++) {
1410
        for(ch=0;ch<s->nb_channels;ch++) {
1411
            if (bit_alloc[ch][i]) {
1412
                sf = scale_factors[ch][i];
1413
                switch(scale_code[ch][i]) {
1414
                default:
1415
                case 0:
1416
                    sf[0] = get_bits(&s->gb, 6);
1417
                    sf[1] = get_bits(&s->gb, 6);
1418
                    sf[2] = get_bits(&s->gb, 6);
1419
                    break;
1420
                case 2:
1421
                    sf[0] = get_bits(&s->gb, 6);
1422
                    sf[1] = sf[0];
1423
                    sf[2] = sf[0];
1424
                    break;
1425
                case 1:
1426
                    sf[0] = get_bits(&s->gb, 6);
1427
                    sf[2] = get_bits(&s->gb, 6);
1428
                    sf[1] = sf[0];
1429
                    break;
1430
                case 3:
1431
                    sf[0] = get_bits(&s->gb, 6);
1432
                    sf[2] = get_bits(&s->gb, 6);
1433
                    sf[1] = sf[2];
1434
                    break;
1435
                }
1436
            }
1437
        }
1438
    }
1439

    
1440
#ifdef DEBUG
1441
    for(ch=0;ch<s->nb_channels;ch++) {
1442
        for(i=0;i<sblimit;i++) {
1443
            if (bit_alloc[ch][i]) {
1444
                sf = scale_factors[ch][i];
1445
                printf(" %d %d %d", sf[0], sf[1], sf[2]);
1446
            } else {
1447
                printf(" -");
1448
            }
1449
        }
1450
        printf("\n");
1451
    }
1452
#endif
1453

    
1454
    /* samples */
1455
    for(k=0;k<3;k++) {
1456
        for(l=0;l<12;l+=3) {
1457
            j = 0;
1458
            for(i=0;i<bound;i++) {
1459
                bit_alloc_bits = alloc_table[j];
1460
                for(ch=0;ch<s->nb_channels;ch++) {
1461
                    b = bit_alloc[ch][i];
1462
                    if (b) {
1463
                        scale = scale_factors[ch][i][k];
1464
                        qindex = alloc_table[j+b];
1465
                        bits = quant_bits[qindex];
1466
                        if (bits < 0) {
1467
                            /* 3 values at the same time */
1468
                            v = get_bits(&s->gb, -bits);
1469
                            steps = quant_steps[qindex];
1470
                            s->sb_samples[ch][k * 12 + l + 0][i] = 
1471
                                l2_unscale_group(steps, v % steps, scale);
1472
                            v = v / steps;
1473
                            s->sb_samples[ch][k * 12 + l + 1][i] = 
1474
                                l2_unscale_group(steps, v % steps, scale);
1475
                            v = v / steps;
1476
                            s->sb_samples[ch][k * 12 + l + 2][i] = 
1477
                                l2_unscale_group(steps, v, scale);
1478
                        } else {
1479
                            for(m=0;m<3;m++) {
1480
                                v = get_bits(&s->gb, bits);
1481
                                v = l1_unscale(bits - 1, v, scale);
1482
                                s->sb_samples[ch][k * 12 + l + m][i] = v;
1483
                            }
1484
                        }
1485
                    } else {
1486
                        s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1487
                        s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1488
                        s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1489
                    }
1490
                }
1491
                /* next subband in alloc table */
1492
                j += 1 << bit_alloc_bits; 
1493
            }
1494
            /* XXX: find a way to avoid this duplication of code */
1495
            for(i=bound;i<sblimit;i++) {
1496
                bit_alloc_bits = alloc_table[j];
1497
                b = bit_alloc[0][i];
1498
                if (b) {
1499
                    int mant, scale0, scale1;
1500
                    scale0 = scale_factors[0][i][k];
1501
                    scale1 = scale_factors[1][i][k];
1502
                    qindex = alloc_table[j+b];
1503
                    bits = quant_bits[qindex];
1504
                    if (bits < 0) {
1505
                        /* 3 values at the same time */
1506
                        v = get_bits(&s->gb, -bits);
1507
                        steps = quant_steps[qindex];
1508
                        mant = v % steps;
1509
                        v = v / steps;
1510
                        s->sb_samples[0][k * 12 + l + 0][i] = 
1511
                            l2_unscale_group(steps, mant, scale0);
1512
                        s->sb_samples[1][k * 12 + l + 0][i] = 
1513
                            l2_unscale_group(steps, mant, scale1);
1514
                        mant = v % steps;
1515
                        v = v / steps;
1516
                        s->sb_samples[0][k * 12 + l + 1][i] = 
1517
                            l2_unscale_group(steps, mant, scale0);
1518
                        s->sb_samples[1][k * 12 + l + 1][i] = 
1519
                            l2_unscale_group(steps, mant, scale1);
1520
                        s->sb_samples[0][k * 12 + l + 2][i] = 
1521
                            l2_unscale_group(steps, v, scale0);
1522
                        s->sb_samples[1][k * 12 + l + 2][i] = 
1523
                            l2_unscale_group(steps, v, scale1);
1524
                    } else {
1525
                        for(m=0;m<3;m++) {
1526
                            mant = get_bits(&s->gb, bits);
1527
                            s->sb_samples[0][k * 12 + l + m][i] = 
1528
                                l1_unscale(bits - 1, mant, scale0);
1529
                            s->sb_samples[1][k * 12 + l + m][i] = 
1530
                                l1_unscale(bits - 1, mant, scale1);
1531
                        }
1532
                    }
1533
                } else {
1534
                    s->sb_samples[0][k * 12 + l + 0][i] = 0;
1535
                    s->sb_samples[0][k * 12 + l + 1][i] = 0;
1536
                    s->sb_samples[0][k * 12 + l + 2][i] = 0;
1537
                    s->sb_samples[1][k * 12 + l + 0][i] = 0;
1538
                    s->sb_samples[1][k * 12 + l + 1][i] = 0;
1539
                    s->sb_samples[1][k * 12 + l + 2][i] = 0;
1540
                }
1541
                /* next subband in alloc table */
1542
                j += 1 << bit_alloc_bits; 
1543
            }
1544
            /* fill remaining samples to zero */
1545
            for(i=sblimit;i<SBLIMIT;i++) {
1546
                for(ch=0;ch<s->nb_channels;ch++) {
1547
                    s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1548
                    s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1549
                    s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1550
                }
1551
            }
1552
        }
1553
    }
1554
    return 3 * 12;
1555
}
1556

    
1557
/*
1558
 * Seek back in the stream for backstep bytes (at most 511 bytes)
1559
 */
1560
static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1561
{
1562
    uint8_t *ptr;
1563

    
1564
    /* compute current position in stream */
1565
    ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1566

    
1567
    /* copy old data before current one */
1568
    ptr -= backstep;
1569
    memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] + 
1570
           BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1571
    /* init get bits again */
1572
    init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1573

    
1574
    /* prepare next buffer */
1575
    s->inbuf_index ^= 1;
1576
    s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1577
    s->old_frame_size = s->frame_size;
1578
}
1579

    
1580
static inline void lsf_sf_expand(int *slen,
1581
                                 int sf, int n1, int n2, int n3)
1582
{
1583
    if (n3) {
1584
        slen[3] = sf % n3;
1585
        sf /= n3;
1586
    } else {
1587
        slen[3] = 0;
1588
    }
1589
    if (n2) {
1590
        slen[2] = sf % n2;
1591
        sf /= n2;
1592
    } else {
1593
        slen[2] = 0;
1594
    }
1595
    slen[1] = sf % n1;
1596
    sf /= n1;
1597
    slen[0] = sf;
1598
}
1599

    
1600
static void exponents_from_scale_factors(MPADecodeContext *s, 
1601
                                         GranuleDef *g,
1602
                                         int16_t *exponents)
1603
{
1604
    const uint8_t *bstab, *pretab;
1605
    int len, i, j, k, l, v0, shift, gain, gains[3];
1606
    int16_t *exp_ptr;
1607

    
1608
    exp_ptr = exponents;
1609
    gain = g->global_gain - 210;
1610
    shift = g->scalefac_scale + 1;
1611

    
1612
    bstab = band_size_long[s->sample_rate_index];
1613
    pretab = mpa_pretab[g->preflag];
1614
    for(i=0;i<g->long_end;i++) {
1615
        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1616
        len = bstab[i];
1617
        for(j=len;j>0;j--)
1618
            *exp_ptr++ = v0;
1619
    }
1620

    
1621
    if (g->short_start < 13) {
1622
        bstab = band_size_short[s->sample_rate_index];
1623
        gains[0] = gain - (g->subblock_gain[0] << 3);
1624
        gains[1] = gain - (g->subblock_gain[1] << 3);
1625
        gains[2] = gain - (g->subblock_gain[2] << 3);
1626
        k = g->long_end;
1627
        for(i=g->short_start;i<13;i++) {
1628
            len = bstab[i];
1629
            for(l=0;l<3;l++) {
1630
                v0 = gains[l] - (g->scale_factors[k++] << shift);
1631
                for(j=len;j>0;j--)
1632
                *exp_ptr++ = v0;
1633
            }
1634
        }
1635
    }
1636
}
1637

    
1638
/* handle n = 0 too */
1639
static inline int get_bitsz(GetBitContext *s, int n)
1640
{
1641
    if (n == 0)
1642
        return 0;
1643
    else
1644
        return get_bits(s, n);
1645
}
1646

    
1647
static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1648
                          int16_t *exponents, int end_pos)
1649
{
1650
    int s_index;
1651
    int linbits, code, x, y, l, v, i, j, k, pos;
1652
    GetBitContext last_gb;
1653
    VLC *vlc;
1654
    uint8_t *code_table;
1655

    
1656
    /* low frequencies (called big values) */
1657
    s_index = 0;
1658
    for(i=0;i<3;i++) {
1659
        j = g->region_size[i];
1660
        if (j == 0)
1661
            continue;
1662
        /* select vlc table */
1663
        k = g->table_select[i];
1664
        l = mpa_huff_data[k][0];
1665
        linbits = mpa_huff_data[k][1];
1666
        vlc = &huff_vlc[l];
1667
        code_table = huff_code_table[l];
1668

    
1669
        /* read huffcode and compute each couple */
1670
        for(;j>0;j--) {
1671
            if (get_bits_count(&s->gb) >= end_pos)
1672
                break;
1673
            if (code_table) {
1674
                code = get_vlc(&s->gb, vlc);
1675
                if (code < 0)
1676
                    return -1;
1677
                y = code_table[code];
1678
                x = y >> 4;
1679
                y = y & 0x0f;
1680
            } else {
1681
                x = 0;
1682
                y = 0;
1683
            }
1684
            dprintf("region=%d n=%d x=%d y=%d exp=%d\n", 
1685
                    i, g->region_size[i] - j, x, y, exponents[s_index]);
1686
            if (x) {
1687
                if (x == 15)
1688
                    x += get_bitsz(&s->gb, linbits);
1689
                v = l3_unscale(x, exponents[s_index]);
1690
                if (get_bits1(&s->gb))
1691
                    v = -v;
1692
            } else {
1693
                v = 0;
1694
            }
1695
            g->sb_hybrid[s_index++] = v;
1696
            if (y) {
1697
                if (y == 15)
1698
                    y += get_bitsz(&s->gb, linbits);
1699
                v = l3_unscale(y, exponents[s_index]);
1700
                if (get_bits1(&s->gb))
1701
                    v = -v;
1702
            } else {
1703
                v = 0;
1704
            }
1705
            g->sb_hybrid[s_index++] = v;
1706
        }
1707
    }
1708
            
1709
    /* high frequencies */
1710
    vlc = &huff_quad_vlc[g->count1table_select];
1711
    last_gb.buffer = NULL;
1712
    while (s_index <= 572) {
1713
        pos = get_bits_count(&s->gb);
1714
        if (pos >= end_pos) {
1715
            if (pos > end_pos && last_gb.buffer != NULL) {
1716
                /* some encoders generate an incorrect size for this
1717
                   part. We must go back into the data */
1718
                s_index -= 4;
1719
                s->gb = last_gb;
1720
            }
1721
            break;
1722
        }
1723
        last_gb= s->gb;
1724

    
1725
        code = get_vlc(&s->gb, vlc);
1726
        dprintf("t=%d code=%d\n", g->count1table_select, code);
1727
        if (code < 0)
1728
            return -1;
1729
        for(i=0;i<4;i++) {
1730
            if (code & (8 >> i)) {
1731
                /* non zero value. Could use a hand coded function for
1732
                   'one' value */
1733
                v = l3_unscale(1, exponents[s_index]);
1734
                if(get_bits1(&s->gb))
1735
                    v = -v;
1736
            } else {
1737
                v = 0;
1738
            }
1739
            g->sb_hybrid[s_index++] = v;
1740
        }
1741
    }
1742
    while (s_index < 576)
1743
        g->sb_hybrid[s_index++] = 0;
1744
    return 0;
1745
}
1746

    
1747
/* Reorder short blocks from bitstream order to interleaved order. It
1748
   would be faster to do it in parsing, but the code would be far more
1749
   complicated */
1750
static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1751
{
1752
    int i, j, k, len;
1753
    int32_t *ptr, *dst, *ptr1;
1754
    int32_t tmp[576];
1755

    
1756
    if (g->block_type != 2)
1757
        return;
1758

    
1759
    if (g->switch_point) {
1760
        if (s->sample_rate_index != 8) {
1761
            ptr = g->sb_hybrid + 36;
1762
        } else {
1763
            ptr = g->sb_hybrid + 48;
1764
        }
1765
    } else {
1766
        ptr = g->sb_hybrid;
1767
    }
1768
    
1769
    for(i=g->short_start;i<13;i++) {
1770
        len = band_size_short[s->sample_rate_index][i];
1771
        ptr1 = ptr;
1772
        for(k=0;k<3;k++) {
1773
            dst = tmp + k;
1774
            for(j=len;j>0;j--) {
1775
                *dst = *ptr++;
1776
                dst += 3;
1777
            }
1778
        }
1779
        memcpy(ptr1, tmp, len * 3 * sizeof(int32_t));
1780
    }
1781
}
1782

    
1783
#define ISQRT2 FIXR(0.70710678118654752440)
1784

    
1785
static void compute_stereo(MPADecodeContext *s,
1786
                           GranuleDef *g0, GranuleDef *g1)
1787
{
1788
    int i, j, k, l;
1789
    int32_t v1, v2;
1790
    int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1791
    int32_t (*is_tab)[16];
1792
    int32_t *tab0, *tab1;
1793
    int non_zero_found_short[3];
1794

    
1795
    /* intensity stereo */
1796
    if (s->mode_ext & MODE_EXT_I_STEREO) {
1797
        if (!s->lsf) {
1798
            is_tab = is_table;
1799
            sf_max = 7;
1800
        } else {
1801
            is_tab = is_table_lsf[g1->scalefac_compress & 1];
1802
            sf_max = 16;
1803
        }
1804
            
1805
        tab0 = g0->sb_hybrid + 576;
1806
        tab1 = g1->sb_hybrid + 576;
1807

    
1808
        non_zero_found_short[0] = 0;
1809
        non_zero_found_short[1] = 0;
1810
        non_zero_found_short[2] = 0;
1811
        k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1812
        for(i = 12;i >= g1->short_start;i--) {
1813
            /* for last band, use previous scale factor */
1814
            if (i != 11)
1815
                k -= 3;
1816
            len = band_size_short[s->sample_rate_index][i];
1817
            for(l=2;l>=0;l--) {
1818
                tab0 -= len;
1819
                tab1 -= len;
1820
                if (!non_zero_found_short[l]) {
1821
                    /* test if non zero band. if so, stop doing i-stereo */
1822
                    for(j=0;j<len;j++) {
1823
                        if (tab1[j] != 0) {
1824
                            non_zero_found_short[l] = 1;
1825
                            goto found1;
1826
                        }
1827
                    }
1828
                    sf = g1->scale_factors[k + l];
1829
                    if (sf >= sf_max)
1830
                        goto found1;
1831

    
1832
                    v1 = is_tab[0][sf];
1833
                    v2 = is_tab[1][sf];
1834
                    for(j=0;j<len;j++) {
1835
                        tmp0 = tab0[j];
1836
                        tab0[j] = MULL(tmp0, v1);
1837
                        tab1[j] = MULL(tmp0, v2);
1838
                    }
1839
                } else {
1840
                found1:
1841
                    if (s->mode_ext & MODE_EXT_MS_STEREO) {
1842
                        /* lower part of the spectrum : do ms stereo
1843
                           if enabled */
1844
                        for(j=0;j<len;j++) {
1845
                            tmp0 = tab0[j];
1846
                            tmp1 = tab1[j];
1847
                            tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1848
                            tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1849
                        }
1850
                    }
1851
                }
1852
            }
1853
        }
1854

    
1855
        non_zero_found = non_zero_found_short[0] | 
1856
            non_zero_found_short[1] | 
1857
            non_zero_found_short[2];
1858

    
1859
        for(i = g1->long_end - 1;i >= 0;i--) {
1860
            len = band_size_long[s->sample_rate_index][i];
1861
            tab0 -= len;
1862
            tab1 -= len;
1863
            /* test if non zero band. if so, stop doing i-stereo */
1864
            if (!non_zero_found) {
1865
                for(j=0;j<len;j++) {
1866
                    if (tab1[j] != 0) {
1867
                        non_zero_found = 1;
1868
                        goto found2;
1869
                    }
1870
                }
1871
                /* for last band, use previous scale factor */
1872
                k = (i == 21) ? 20 : i;
1873
                sf = g1->scale_factors[k];
1874
                if (sf >= sf_max)
1875
                    goto found2;
1876
                v1 = is_tab[0][sf];
1877
                v2 = is_tab[1][sf];
1878
                for(j=0;j<len;j++) {
1879
                    tmp0 = tab0[j];
1880
                    tab0[j] = MULL(tmp0, v1);
1881
                    tab1[j] = MULL(tmp0, v2);
1882
                }
1883
            } else {
1884
            found2:
1885
                if (s->mode_ext & MODE_EXT_MS_STEREO) {
1886
                    /* lower part of the spectrum : do ms stereo
1887
                       if enabled */
1888
                    for(j=0;j<len;j++) {
1889
                        tmp0 = tab0[j];
1890
                        tmp1 = tab1[j];
1891
                        tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1892
                        tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1893
                    }
1894
                }
1895
            }
1896
        }
1897
    } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1898
        /* ms stereo ONLY */
1899
        /* NOTE: the 1/sqrt(2) normalization factor is included in the
1900
           global gain */
1901
        tab0 = g0->sb_hybrid;
1902
        tab1 = g1->sb_hybrid;
1903
        for(i=0;i<576;i++) {
1904
            tmp0 = tab0[i];
1905
            tmp1 = tab1[i];
1906
            tab0[i] = tmp0 + tmp1;
1907
            tab1[i] = tmp0 - tmp1;
1908
        }
1909
    }
1910
}
1911

    
1912
static void compute_antialias_integer(MPADecodeContext *s,
1913
                              GranuleDef *g)
1914
{
1915
    int32_t *ptr, *p0, *p1, *csa;
1916
    int n, i, j;
1917

    
1918
    /* we antialias only "long" bands */
1919
    if (g->block_type == 2) {
1920
        if (!g->switch_point)
1921
            return;
1922
        /* XXX: check this for 8000Hz case */
1923
        n = 1;
1924
    } else {
1925
        n = SBLIMIT - 1;
1926
    }
1927
    
1928
    ptr = g->sb_hybrid + 18;
1929
    for(i = n;i > 0;i--) {
1930
        p0 = ptr - 1;
1931
        p1 = ptr;
1932
        csa = &csa_table[0][0];       
1933
        for(j=0;j<4;j++) {
1934
            int tmp0 = *p0;
1935
            int tmp1 = *p1;
1936
#if 0
1937
            *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1938
            *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1939
#else
1940
            int64_t tmp2= MUL64(tmp0 + tmp1, csa[0]);
1941
            *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2]));
1942
            *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3]));
1943
#endif
1944
            p0--; p1++;
1945
            csa += 4;
1946
            tmp0 = *p0;
1947
            tmp1 = *p1;
1948
#if 0
1949
            *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1950
            *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1951
#else
1952
            tmp2= MUL64(tmp0 + tmp1, csa[0]);
1953
            *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2]));
1954
            *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3]));
1955
#endif
1956
            p0--; p1++;
1957
            csa += 4;
1958
        }
1959
        ptr += 18;       
1960
    }
1961
}
1962

    
1963
static void compute_antialias_float(MPADecodeContext *s,
1964
                              GranuleDef *g)
1965
{
1966
    int32_t *ptr, *p0, *p1;
1967
    int n, i, j;
1968

    
1969
    /* we antialias only "long" bands */
1970
    if (g->block_type == 2) {
1971
        if (!g->switch_point)
1972
            return;
1973
        /* XXX: check this for 8000Hz case */
1974
        n = 1;
1975
    } else {
1976
        n = SBLIMIT - 1;
1977
    }
1978
    
1979
    ptr = g->sb_hybrid + 18;
1980
    for(i = n;i > 0;i--) {
1981
        float *csa = &csa_table_float[0][0];       
1982
        p0 = ptr - 1;
1983
        p1 = ptr;
1984
        for(j=0;j<4;j++) {
1985
            float tmp0 = *p0;
1986
            float tmp1 = *p1;
1987
#if 1
1988
            *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]);
1989
            *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]);
1990
#else
1991
            float tmp2= (tmp0 + tmp1) * csa[0];
1992
            *p0 = lrintf(tmp2 - tmp1 * csa[2]);
1993
            *p1 = lrintf(tmp2 + tmp0 * csa[3]);
1994
#endif
1995
            p0--; p1++;
1996
            csa += 4;
1997
            tmp0 = *p0;
1998
            tmp1 = *p1;
1999
#if 1
2000
            *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]);
2001
            *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]);
2002
#else
2003
            tmp2= (tmp0 + tmp1) * csa[0];
2004
            *p0 = lrintf(tmp2 - tmp1 * csa[2]);
2005
            *p1 = lrintf(tmp2 + tmp0 * csa[3]);
2006
#endif
2007
            p0--; p1++;
2008
            csa += 4;
2009
        }
2010
        ptr += 18;       
2011
    }
2012
}
2013

    
2014
static void compute_imdct(MPADecodeContext *s,
2015
                          GranuleDef *g, 
2016
                          int32_t *sb_samples,
2017
                          int32_t *mdct_buf)
2018
{
2019
    int32_t *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1;
2020
    int32_t in[6];
2021
    int32_t out[36];
2022
    int32_t out2[12];
2023
    int i, j, k, mdct_long_end, v, sblimit;
2024

    
2025
    /* find last non zero block */
2026
    ptr = g->sb_hybrid + 576;
2027
    ptr1 = g->sb_hybrid + 2 * 18;
2028
    while (ptr >= ptr1) {
2029
        ptr -= 6;
2030
        v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2031
        if (v != 0)
2032
            break;
2033
    }
2034
    sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2035

    
2036
    if (g->block_type == 2) {
2037
        /* XXX: check for 8000 Hz */
2038
        if (g->switch_point)
2039
            mdct_long_end = 2;
2040
        else
2041
            mdct_long_end = 0;
2042
    } else {
2043
        mdct_long_end = sblimit;
2044
    }
2045

    
2046
    buf = mdct_buf;
2047
    ptr = g->sb_hybrid;
2048
    for(j=0;j<mdct_long_end;j++) {
2049
        imdct36(out, ptr);
2050
        /* apply window & overlap with previous buffer */
2051
        out_ptr = sb_samples + j;
2052
        /* select window */
2053
        if (g->switch_point && j < 2)
2054
            win1 = mdct_win[0];
2055
        else
2056
            win1 = mdct_win[g->block_type];
2057
        /* select frequency inversion */
2058
        win = win1 + ((4 * 36) & -(j & 1));
2059
        for(i=0;i<18;i++) {
2060
            *out_ptr = MULL(out[i], win[i]) + buf[i];
2061
            buf[i] = MULL(out[i + 18], win[i + 18]);
2062
            out_ptr += SBLIMIT;
2063
        }
2064
        ptr += 18;
2065
        buf += 18;
2066
    }
2067
    for(j=mdct_long_end;j<sblimit;j++) {
2068
        for(i=0;i<6;i++) {
2069
            out[i] = 0;
2070
            out[6 + i] = 0;
2071
            out[30+i] = 0;
2072
        }
2073
        /* select frequency inversion */
2074
        win = mdct_win[2] + ((4 * 36) & -(j & 1));
2075
        buf2 = out + 6;
2076
        for(k=0;k<3;k++) {
2077
            /* reorder input for short mdct */
2078
            ptr1 = ptr + k;
2079
            for(i=0;i<6;i++) {
2080
                in[i] = *ptr1;
2081
                ptr1 += 3;
2082
            }
2083
            imdct12(out2, in);
2084
            /* apply 12 point window and do small overlap */
2085
            for(i=0;i<6;i++) {
2086
                buf2[i] = MULL(out2[i], win[i]) + buf2[i];
2087
                buf2[i + 6] = MULL(out2[i + 6], win[i + 6]);
2088
            }
2089
            buf2 += 6;
2090
        }
2091
        /* overlap */
2092
        out_ptr = sb_samples + j;
2093
        for(i=0;i<18;i++) {
2094
            *out_ptr = out[i] + buf[i];
2095
            buf[i] = out[i + 18];
2096
            out_ptr += SBLIMIT;
2097
        }
2098
        ptr += 18;
2099
        buf += 18;
2100
    }
2101
    /* zero bands */
2102
    for(j=sblimit;j<SBLIMIT;j++) {
2103
        /* overlap */
2104
        out_ptr = sb_samples + j;
2105
        for(i=0;i<18;i++) {
2106
            *out_ptr = buf[i];
2107
            buf[i] = 0;
2108
            out_ptr += SBLIMIT;
2109
        }
2110
        buf += 18;
2111
    }
2112
}
2113

    
2114
#if defined(DEBUG)
2115
void sample_dump(int fnum, int32_t *tab, int n)
2116
{
2117
    static FILE *files[16], *f;
2118
    char buf[512];
2119
    int i;
2120
    int32_t v;
2121
    
2122
    f = files[fnum];
2123
    if (!f) {
2124
        snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm", 
2125
                fnum, 
2126
#ifdef USE_HIGHPRECISION
2127
                "hp"
2128
#else
2129
                "lp"
2130
#endif
2131
                );
2132
        f = fopen(buf, "w");
2133
        if (!f)
2134
            return;
2135
        files[fnum] = f;
2136
    }
2137
    
2138
    if (fnum == 0) {
2139
        static int pos = 0;
2140
        printf("pos=%d\n", pos);
2141
        for(i=0;i<n;i++) {
2142
            printf(" %0.4f", (double)tab[i] / FRAC_ONE);
2143
            if ((i % 18) == 17)
2144
                printf("\n");
2145
        }
2146
        pos += n;
2147
    }
2148
    for(i=0;i<n;i++) {
2149
        /* normalize to 23 frac bits */
2150
        v = tab[i] << (23 - FRAC_BITS);
2151
        fwrite(&v, 1, sizeof(int32_t), f);
2152
    }
2153
}
2154
#endif
2155

    
2156

    
2157
/* main layer3 decoding function */
2158
static int mp_decode_layer3(MPADecodeContext *s)
2159
{
2160
    int nb_granules, main_data_begin, private_bits;
2161
    int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2162
    GranuleDef granules[2][2], *g;
2163
    int16_t exponents[576];
2164

    
2165
    /* read side info */
2166
    if (s->lsf) {
2167
        main_data_begin = get_bits(&s->gb, 8);
2168
        if (s->nb_channels == 2)
2169
            private_bits = get_bits(&s->gb, 2);
2170
        else
2171
            private_bits = get_bits(&s->gb, 1);
2172
        nb_granules = 1;
2173
    } else {
2174
        main_data_begin = get_bits(&s->gb, 9);
2175
        if (s->nb_channels == 2)
2176
            private_bits = get_bits(&s->gb, 3);
2177
        else
2178
            private_bits = get_bits(&s->gb, 5);
2179
        nb_granules = 2;
2180
        for(ch=0;ch<s->nb_channels;ch++) {
2181
            granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2182
            granules[ch][1].scfsi = get_bits(&s->gb, 4);
2183
        }
2184
    }
2185
    
2186
    for(gr=0;gr<nb_granules;gr++) {
2187
        for(ch=0;ch<s->nb_channels;ch++) {
2188
            dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2189
            g = &granules[ch][gr];
2190
            g->part2_3_length = get_bits(&s->gb, 12);
2191
            g->big_values = get_bits(&s->gb, 9);
2192
            g->global_gain = get_bits(&s->gb, 8);
2193
            /* if MS stereo only is selected, we precompute the
2194
               1/sqrt(2) renormalization factor */
2195
            if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) == 
2196
                MODE_EXT_MS_STEREO)
2197
                g->global_gain -= 2;
2198
            if (s->lsf)
2199
                g->scalefac_compress = get_bits(&s->gb, 9);
2200
            else
2201
                g->scalefac_compress = get_bits(&s->gb, 4);
2202
            blocksplit_flag = get_bits(&s->gb, 1);
2203
            if (blocksplit_flag) {
2204
                g->block_type = get_bits(&s->gb, 2);
2205
                if (g->block_type == 0)
2206
                    return -1;
2207
                g->switch_point = get_bits(&s->gb, 1);
2208
                for(i=0;i<2;i++)
2209
                    g->table_select[i] = get_bits(&s->gb, 5);
2210
                for(i=0;i<3;i++) 
2211
                    g->subblock_gain[i] = get_bits(&s->gb, 3);
2212
                /* compute huffman coded region sizes */
2213
                if (g->block_type == 2)
2214
                    g->region_size[0] = (36 / 2);
2215
                else {
2216
                    if (s->sample_rate_index <= 2) 
2217
                        g->region_size[0] = (36 / 2);
2218
                    else if (s->sample_rate_index != 8) 
2219
                        g->region_size[0] = (54 / 2);
2220
                    else
2221
                        g->region_size[0] = (108 / 2);
2222
                }
2223
                g->region_size[1] = (576 / 2);
2224
            } else {
2225
                int region_address1, region_address2, l;
2226
                g->block_type = 0;
2227
                g->switch_point = 0;
2228
                for(i=0;i<3;i++)
2229
                    g->table_select[i] = get_bits(&s->gb, 5);
2230
                /* compute huffman coded region sizes */
2231
                region_address1 = get_bits(&s->gb, 4);
2232
                region_address2 = get_bits(&s->gb, 3);
2233
                dprintf("region1=%d region2=%d\n", 
2234
                        region_address1, region_address2);
2235
                g->region_size[0] = 
2236
                    band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2237
                l = region_address1 + region_address2 + 2;
2238
                /* should not overflow */
2239
                if (l > 22)
2240
                    l = 22;
2241
                g->region_size[1] = 
2242
                    band_index_long[s->sample_rate_index][l] >> 1;
2243
            }
2244
            /* convert region offsets to region sizes and truncate
2245
               size to big_values */
2246
            g->region_size[2] = (576 / 2);
2247
            j = 0;
2248
            for(i=0;i<3;i++) {
2249
                k = g->region_size[i];
2250
                if (k > g->big_values)
2251
                    k = g->big_values;
2252
                g->region_size[i] = k - j;
2253
                j = k;
2254
            }
2255

    
2256
            /* compute band indexes */
2257
            if (g->block_type == 2) {
2258
                if (g->switch_point) {
2259
                    /* if switched mode, we handle the 36 first samples as
2260
                       long blocks.  For 8000Hz, we handle the 48 first
2261
                       exponents as long blocks (XXX: check this!) */
2262
                    if (s->sample_rate_index <= 2)
2263
                        g->long_end = 8;
2264
                    else if (s->sample_rate_index != 8)
2265
                        g->long_end = 6;
2266
                    else
2267
                        g->long_end = 4; /* 8000 Hz */
2268
                    
2269
                    if (s->sample_rate_index != 8)
2270
                        g->short_start = 3;
2271
                    else
2272
                        g->short_start = 2; 
2273
                } else {
2274
                    g->long_end = 0;
2275
                    g->short_start = 0;
2276
                }
2277
            } else {
2278
                g->short_start = 13;
2279
                g->long_end = 22;
2280
            }
2281
            
2282
            g->preflag = 0;
2283
            if (!s->lsf)
2284
                g->preflag = get_bits(&s->gb, 1);
2285
            g->scalefac_scale = get_bits(&s->gb, 1);
2286
            g->count1table_select = get_bits(&s->gb, 1);
2287
            dprintf("block_type=%d switch_point=%d\n",
2288
                    g->block_type, g->switch_point);
2289
        }
2290
    }
2291

    
2292
  if (!s->adu_mode) {
2293
    /* now we get bits from the main_data_begin offset */
2294
    dprintf("seekback: %d\n", main_data_begin);
2295
    seek_to_maindata(s, main_data_begin);
2296
  }
2297

    
2298
    for(gr=0;gr<nb_granules;gr++) {
2299
        for(ch=0;ch<s->nb_channels;ch++) {
2300
            g = &granules[ch][gr];
2301
            
2302
            bits_pos = get_bits_count(&s->gb);
2303
            
2304
            if (!s->lsf) {
2305
                uint8_t *sc;
2306
                int slen, slen1, slen2;
2307

    
2308
                /* MPEG1 scale factors */
2309
                slen1 = slen_table[0][g->scalefac_compress];
2310
                slen2 = slen_table[1][g->scalefac_compress];
2311
                dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2312
                if (g->block_type == 2) {
2313
                    n = g->switch_point ? 17 : 18;
2314
                    j = 0;
2315
                    for(i=0;i<n;i++)
2316
                        g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2317
                    for(i=0;i<18;i++)
2318
                        g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2319
                    for(i=0;i<3;i++)
2320
                        g->scale_factors[j++] = 0;
2321
                } else {
2322
                    sc = granules[ch][0].scale_factors;
2323
                    j = 0;
2324
                    for(k=0;k<4;k++) {
2325
                        n = (k == 0 ? 6 : 5);
2326
                        if ((g->scfsi & (0x8 >> k)) == 0) {
2327
                            slen = (k < 2) ? slen1 : slen2;
2328
                            for(i=0;i<n;i++)
2329
                                g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2330
                        } else {
2331
                            /* simply copy from last granule */
2332
                            for(i=0;i<n;i++) {
2333
                                g->scale_factors[j] = sc[j];
2334
                                j++;
2335
                            }
2336
                        }
2337
                    }
2338
                    g->scale_factors[j++] = 0;
2339
                }
2340
#if defined(DEBUG)
2341
                {
2342
                    printf("scfsi=%x gr=%d ch=%d scale_factors:\n", 
2343
                           g->scfsi, gr, ch);
2344
                    for(i=0;i<j;i++)
2345
                        printf(" %d", g->scale_factors[i]);
2346
                    printf("\n");
2347
                }
2348
#endif
2349
            } else {
2350
                int tindex, tindex2, slen[4], sl, sf;
2351

    
2352
                /* LSF scale factors */
2353
                if (g->block_type == 2) {
2354
                    tindex = g->switch_point ? 2 : 1;
2355
                } else {
2356
                    tindex = 0;
2357
                }
2358
                sf = g->scalefac_compress;
2359
                if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2360
                    /* intensity stereo case */
2361
                    sf >>= 1;
2362
                    if (sf < 180) {
2363
                        lsf_sf_expand(slen, sf, 6, 6, 0);
2364
                        tindex2 = 3;
2365
                    } else if (sf < 244) {
2366
                        lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2367
                        tindex2 = 4;
2368
                    } else {
2369
                        lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2370
                        tindex2 = 5;
2371
                    }
2372
                } else {
2373
                    /* normal case */
2374
                    if (sf < 400) {
2375
                        lsf_sf_expand(slen, sf, 5, 4, 4);
2376
                        tindex2 = 0;
2377
                    } else if (sf < 500) {
2378
                        lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2379
                        tindex2 = 1;
2380
                    } else {
2381
                        lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2382
                        tindex2 = 2;
2383
                        g->preflag = 1;
2384
                    }
2385
                }
2386

    
2387
                j = 0;
2388
                for(k=0;k<4;k++) {
2389
                    n = lsf_nsf_table[tindex2][tindex][k];
2390
                    sl = slen[k];
2391
                    for(i=0;i<n;i++)
2392
                        g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2393
                }
2394
                /* XXX: should compute exact size */
2395
                for(;j<40;j++)
2396
                    g->scale_factors[j] = 0;
2397
#if defined(DEBUG)
2398
                {
2399
                    printf("gr=%d ch=%d scale_factors:\n", 
2400
                           gr, ch);
2401
                    for(i=0;i<40;i++)
2402
                        printf(" %d", g->scale_factors[i]);
2403
                    printf("\n");
2404
                }
2405
#endif
2406
            }
2407

    
2408
            exponents_from_scale_factors(s, g, exponents);
2409

    
2410
            /* read Huffman coded residue */
2411
            if (huffman_decode(s, g, exponents,
2412
                               bits_pos + g->part2_3_length) < 0)
2413
                return -1;
2414
#if defined(DEBUG)
2415
            sample_dump(0, g->sb_hybrid, 576);
2416
#endif
2417

    
2418
            /* skip extension bits */
2419
            bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2420
            if (bits_left < 0) {
2421
                dprintf("bits_left=%d\n", bits_left);
2422
                return -1;
2423
            }
2424
            while (bits_left >= 16) {
2425
                skip_bits(&s->gb, 16);
2426
                bits_left -= 16;
2427
            }
2428
            if (bits_left > 0)
2429
                skip_bits(&s->gb, bits_left);
2430
        } /* ch */
2431

    
2432
        if (s->nb_channels == 2)
2433
            compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2434

    
2435
        for(ch=0;ch<s->nb_channels;ch++) {
2436
            g = &granules[ch][gr];
2437

    
2438
            reorder_block(s, g);
2439
#if defined(DEBUG)
2440
            sample_dump(0, g->sb_hybrid, 576);
2441
#endif
2442
            s->compute_antialias(s, g);
2443
#if defined(DEBUG)
2444
            sample_dump(1, g->sb_hybrid, 576);
2445
#endif
2446
            compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); 
2447
#if defined(DEBUG)
2448
            sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2449
#endif
2450
        }
2451
    } /* gr */
2452
    return nb_granules * 18;
2453
}
2454

    
2455
static int mp_decode_frame(MPADecodeContext *s, 
2456
                           short *samples)
2457
{
2458
    int i, nb_frames, ch;
2459
    short *samples_ptr;
2460

    
2461
    init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, 
2462
                  (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2463
    
2464
    /* skip error protection field */
2465
    if (s->error_protection)
2466
        get_bits(&s->gb, 16);
2467

    
2468
    dprintf("frame %d:\n", s->frame_count);
2469
    switch(s->layer) {
2470
    case 1:
2471
        nb_frames = mp_decode_layer1(s);
2472
        break;
2473
    case 2:
2474
        nb_frames = mp_decode_layer2(s);
2475
        break;
2476
    case 3:
2477
    default:
2478
        nb_frames = mp_decode_layer3(s);
2479
        break;
2480
    }
2481
#if defined(DEBUG)
2482
    for(i=0;i<nb_frames;i++) {
2483
        for(ch=0;ch<s->nb_channels;ch++) {
2484
            int j;
2485
            printf("%d-%d:", i, ch);
2486
            for(j=0;j<SBLIMIT;j++)
2487
                printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2488
            printf("\n");
2489
        }
2490
    }
2491
#endif
2492
    /* apply the synthesis filter */
2493
    for(ch=0;ch<s->nb_channels;ch++) {
2494
        samples_ptr = samples + ch;
2495
        for(i=0;i<nb_frames;i++) {
2496
            ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2497
                         window,
2498
                         samples_ptr, s->nb_channels,
2499
                         s->sb_samples[ch][i]);
2500
            samples_ptr += 32 * s->nb_channels;
2501
        }
2502
    }
2503
#ifdef DEBUG
2504
    s->frame_count++;        
2505
#endif
2506
    return nb_frames * 32 * sizeof(short) * s->nb_channels;
2507
}
2508

    
2509
static int decode_frame(AVCodecContext * avctx,
2510
                        void *data, int *data_size,
2511
                        uint8_t * buf, int buf_size)
2512
{
2513
    MPADecodeContext *s = avctx->priv_data;
2514
    uint32_t header;
2515
    uint8_t *buf_ptr;
2516
    int len, out_size;
2517
    short *out_samples = data;
2518

    
2519
    buf_ptr = buf;
2520
    while (buf_size > 0) {
2521
        len = s->inbuf_ptr - s->inbuf;
2522
        if (s->frame_size == 0) {
2523
            /* special case for next header for first frame in free
2524
               format case (XXX: find a simpler method) */
2525
            if (s->free_format_next_header != 0) {
2526
                s->inbuf[0] = s->free_format_next_header >> 24;
2527
                s->inbuf[1] = s->free_format_next_header >> 16;
2528
                s->inbuf[2] = s->free_format_next_header >> 8;
2529
                s->inbuf[3] = s->free_format_next_header;
2530
                s->inbuf_ptr = s->inbuf + 4;
2531
                s->free_format_next_header = 0;
2532
                goto got_header;
2533
            }
2534
            /* no header seen : find one. We need at least HEADER_SIZE
2535
               bytes to parse it */
2536
            len = HEADER_SIZE - len;
2537
            if (len > buf_size)
2538
                len = buf_size;
2539
            if (len > 0) {
2540
                memcpy(s->inbuf_ptr, buf_ptr, len);
2541
                buf_ptr += len;
2542
                buf_size -= len;
2543
                s->inbuf_ptr += len;
2544
            }
2545
            if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2546
            got_header:
2547
                header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2548
                    (s->inbuf[2] << 8) | s->inbuf[3];
2549

    
2550
                if (ff_mpa_check_header(header) < 0) {
2551
                    /* no sync found : move by one byte (inefficient, but simple!) */
2552
                    memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2553
                    s->inbuf_ptr--;
2554
                    dprintf("skip %x\n", header);
2555
                    /* reset free format frame size to give a chance
2556
                       to get a new bitrate */
2557
                    s->free_format_frame_size = 0;
2558
                } else {
2559
                    if (decode_header(s, header) == 1) {
2560
                        /* free format: prepare to compute frame size */
2561
                        s->frame_size = -1;
2562
                    }
2563
                    /* update codec info */
2564
                    avctx->sample_rate = s->sample_rate;
2565
                    avctx->channels = s->nb_channels;
2566
                    avctx->bit_rate = s->bit_rate;
2567
                    avctx->sub_id = s->layer;
2568
                    switch(s->layer) {
2569
                    case 1:
2570
                        avctx->frame_size = 384;
2571
                        break;
2572
                    case 2:
2573
                        avctx->frame_size = 1152;
2574
                        break;
2575
                    case 3:
2576
                        if (s->lsf)
2577
                            avctx->frame_size = 576;
2578
                        else
2579
                            avctx->frame_size = 1152;
2580
                        break;
2581
                    }
2582
                }
2583
            }
2584
        } else if (s->frame_size == -1) {
2585
            /* free format : find next sync to compute frame size */
2586
            len = MPA_MAX_CODED_FRAME_SIZE - len;
2587
            if (len > buf_size)
2588
                len = buf_size;
2589
            if (len == 0) {
2590
                /* frame too long: resync */
2591
                s->frame_size = 0;
2592
                memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2593
                s->inbuf_ptr--;
2594
            } else {
2595
                uint8_t *p, *pend;
2596
                uint32_t header1;
2597
                int padding;
2598

    
2599
                memcpy(s->inbuf_ptr, buf_ptr, len);
2600
                /* check for header */
2601
                p = s->inbuf_ptr - 3;
2602
                pend = s->inbuf_ptr + len - 4;
2603
                while (p <= pend) {
2604
                    header = (p[0] << 24) | (p[1] << 16) |
2605
                        (p[2] << 8) | p[3];
2606
                    header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2607
                        (s->inbuf[2] << 8) | s->inbuf[3];
2608
                    /* check with high probability that we have a
2609
                       valid header */
2610
                    if ((header & SAME_HEADER_MASK) ==
2611
                        (header1 & SAME_HEADER_MASK)) {
2612
                        /* header found: update pointers */
2613
                        len = (p + 4) - s->inbuf_ptr;
2614
                        buf_ptr += len;
2615
                        buf_size -= len;
2616
                        s->inbuf_ptr = p;
2617
                        /* compute frame size */
2618
                        s->free_format_next_header = header;
2619
                        s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2620
                        padding = (header1 >> 9) & 1;
2621
                        if (s->layer == 1)
2622
                            s->free_format_frame_size -= padding * 4;
2623
                        else
2624
                            s->free_format_frame_size -= padding;
2625
                        dprintf("free frame size=%d padding=%d\n", 
2626
                                s->free_format_frame_size, padding);
2627
                        decode_header(s, header1);
2628
                        goto next_data;
2629
                    }
2630
                    p++;
2631
                }
2632
                /* not found: simply increase pointers */
2633
                buf_ptr += len;
2634
                s->inbuf_ptr += len;
2635
                buf_size -= len;
2636
            }
2637
        } else if (len < s->frame_size) {
2638
            if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2639
                s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2640
            len = s->frame_size - len;
2641
            if (len > buf_size)
2642
                len = buf_size;
2643
            memcpy(s->inbuf_ptr, buf_ptr, len);
2644
            buf_ptr += len;
2645
            s->inbuf_ptr += len;
2646
            buf_size -= len;
2647
        }
2648
    next_data:
2649
        if (s->frame_size > 0 && 
2650
            (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2651
            if (avctx->parse_only) {
2652
                /* simply return the frame data */
2653
                *(uint8_t **)data = s->inbuf;
2654
                out_size = s->inbuf_ptr - s->inbuf;
2655
            } else {
2656
                out_size = mp_decode_frame(s, out_samples);
2657
            }
2658
            s->inbuf_ptr = s->inbuf;
2659
            s->frame_size = 0;
2660
            *data_size = out_size;
2661
            break;
2662
        }
2663
    }
2664
    return buf_ptr - buf;
2665
}
2666

    
2667

    
2668
static int decode_frame_adu(AVCodecContext * avctx,
2669
                        void *data, int *data_size,
2670
                        uint8_t * buf, int buf_size)
2671
{
2672
    MPADecodeContext *s = avctx->priv_data;
2673
    uint32_t header;
2674
    int len, out_size;
2675
    short *out_samples = data;
2676

    
2677
    len = buf_size;
2678

    
2679
    // Discard too short frames
2680
    if (buf_size < HEADER_SIZE) {
2681
        *data_size = 0;
2682
        return buf_size;
2683
    }
2684

    
2685

    
2686
    if (len > MPA_MAX_CODED_FRAME_SIZE)
2687
        len = MPA_MAX_CODED_FRAME_SIZE;
2688

    
2689
    memcpy(s->inbuf, buf, len);
2690
    s->inbuf_ptr = s->inbuf + len;
2691

    
2692
    // Get header and restore sync word
2693
    header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2694
              (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
2695

    
2696
    if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2697
        *data_size = 0;
2698
        return buf_size;
2699
    }
2700

    
2701
    decode_header(s, header);
2702
    /* update codec info */
2703
    avctx->sample_rate = s->sample_rate;
2704
    avctx->channels = s->nb_channels;
2705
    avctx->bit_rate = s->bit_rate;
2706
    avctx->sub_id = s->layer;
2707

    
2708
    avctx->frame_size=s->frame_size = len;
2709

    
2710
    if (avctx->parse_only) {
2711
        /* simply return the frame data */
2712
        *(uint8_t **)data = s->inbuf;
2713
        out_size = s->inbuf_ptr - s->inbuf;
2714
    } else {
2715
        out_size = mp_decode_frame(s, out_samples);
2716
    }
2717

    
2718
    *data_size = out_size;
2719
    return buf_size;
2720
}
2721

    
2722

    
2723
AVCodec mp2_decoder =
2724
{
2725
    "mp2",
2726
    CODEC_TYPE_AUDIO,
2727
    CODEC_ID_MP2,
2728
    sizeof(MPADecodeContext),
2729
    decode_init,
2730
    NULL,
2731
    NULL,
2732
    decode_frame,
2733
    CODEC_CAP_PARSE_ONLY,
2734
};
2735

    
2736
AVCodec mp3_decoder =
2737
{
2738
    "mp3",
2739
    CODEC_TYPE_AUDIO,
2740
    CODEC_ID_MP3,
2741
    sizeof(MPADecodeContext),
2742
    decode_init,
2743
    NULL,
2744
    NULL,
2745
    decode_frame,
2746
    CODEC_CAP_PARSE_ONLY,
2747
};
2748

    
2749
AVCodec mp3adu_decoder =
2750
{
2751
    "mp3adu",
2752
    CODEC_TYPE_AUDIO,
2753
    CODEC_ID_MP3ADU,
2754
    sizeof(MPADecodeContext),
2755
    decode_init,
2756
    NULL,
2757
    NULL,
2758
    decode_frame_adu,
2759
    CODEC_CAP_PARSE_ONLY,
2760
};