Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudiodec.c @ bf1f4da0

History | View | Annotate | Download (81.8 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
} MPADecodeContext;
103

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

    
124
#define MODE_EXT_MS_STEREO 2
125
#define MODE_EXT_I_STEREO  1
126

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

    
134
#include "mpegaudiodectab.h"
135

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
254
static int dev_4_3_coefs[DEV_ORDER];
255

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
567
#define COS4_0 FIXR(0.70710678118654752439)
568

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

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

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

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

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

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

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

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

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

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

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

    
750
#if FRAC_BITS <= 15
751

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

    
763
#if defined(ARCH_POWERPC_405)
764

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

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

    
773
#else
774

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

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

    
781
#endif
782

    
783
#else
784

    
785
static inline int round_sample(int64_t sum) 
786
{
787
    int sum1;
788
    sum1 = (int)((sum + (int64_t_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT);
789
    if (sum1 < -32768)
790
        sum1 = -32768;
791
    else if (sum1 > 32767)
792
        sum1 = 32767;
793
    return sum1;
794
}
795

    
796
#define MULS(ra, rb) MUL64(ra, rb)
797

    
798
#endif
799

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

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

    
841
void ff_mpa_synth_init(MPA_INT *window)
842
{
843
    int i;
844

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

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

    
879
    dct32(tmp, sb_samples);
880
    
881
    offset = *synth_buf_offset;
882
    synth_buf = synth_buf_ptr + offset;
883

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

    
899
    samples2 = samples + 31 * incr;
900
    w = window;
901
    w2 = window + 31;
902

    
903
    sum = 0;
904
    p = synth_buf + 16;
905
    SUM8(sum, +=, w, p);
906
    p = synth_buf + 48;
907
    SUM8(sum, -=, w + 32, p);
908
    *samples = round_sample(sum);
909
    samples += incr;
910
    w++;
911

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

    
922
        *samples = round_sample(sum);
923
        samples += incr;
924
        *samples2 = round_sample(sum2);
925
        samples2 -= incr;
926
        w++;
927
        w2--;
928
    }
929
    
930
    p = synth_buf + 32;
931
    sum = 0;
932
    SUM8(sum, -=, w + 32, p);
933
    *samples = round_sample(sum);
934

    
935
    offset = (offset - 32) & 511;
936
    *synth_buf_offset = offset;
937
}
938

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

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

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

    
985
#undef C1
986
#undef C3
987
#undef C5
988
#undef C7
989
#undef C9
990
#undef C11
991

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

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

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

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

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

    
1046
    for(i=17;i>=1;i--)
1047
        in[i] += in[i-1];
1048
    for(i=17;i>=3;i-=2)
1049
        in[i] += in[i-2];
1050

    
1051
    for(j=0;j<2;j++) {
1052
        tmp1 = tmp + j;
1053
        in1 = in + j;
1054

    
1055
        in3_3 = MUL64(in1[2*3], C3);
1056
        in6_6 = MUL64(in1[2*6], C6);
1057

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

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

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

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

    
1118
/* fast header check for resync */
1119
static int check_header(uint32_t header)
1120
{
1121
    /* header */
1122
    if ((header & 0xffe00000) != 0xffe00000)
1123
        return -1;
1124
    /* layer check */
1125
    if (((header >> 17) & 3) == 0)
1126
        return -1;
1127
    /* bit rate */
1128
    if (((header >> 12) & 0xf) == 0xf)
1129
        return -1;
1130
    /* frequency */
1131
    if (((header >> 10) & 3) == 3)
1132
        return -1;
1133
    return 0;
1134
}
1135

    
1136
/* header + layer + bitrate + freq + lsf/mpeg25 */
1137
#define SAME_HEADER_MASK \
1138
   (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1139

    
1140
/* header decoding. MUST check the header before because no
1141
   consistency check is done there. Return 1 if free format found and
1142
   that the frame size must be computed externally */
1143
static int decode_header(MPADecodeContext *s, uint32_t header)
1144
{
1145
    int sample_rate, frame_size, mpeg25, padding;
1146
    int sample_rate_index, bitrate_index;
1147
    if (header & (1<<20)) {
1148
        s->lsf = (header & (1<<19)) ? 0 : 1;
1149
        mpeg25 = 0;
1150
    } else {
1151
        s->lsf = 1;
1152
        mpeg25 = 1;
1153
    }
1154
    
1155
    s->layer = 4 - ((header >> 17) & 3);
1156
    /* extract frequency */
1157
    sample_rate_index = (header >> 10) & 3;
1158
    sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1159
    sample_rate_index += 3 * (s->lsf + mpeg25);
1160
    s->sample_rate_index = sample_rate_index;
1161
    s->error_protection = ((header >> 16) & 1) ^ 1;
1162
    s->sample_rate = sample_rate;
1163

    
1164
    bitrate_index = (header >> 12) & 0xf;
1165
    padding = (header >> 9) & 1;
1166
    //extension = (header >> 8) & 1;
1167
    s->mode = (header >> 6) & 3;
1168
    s->mode_ext = (header >> 4) & 3;
1169
    //copyright = (header >> 3) & 1;
1170
    //original = (header >> 2) & 1;
1171
    //emphasis = header & 3;
1172

    
1173
    if (s->mode == MPA_MONO)
1174
        s->nb_channels = 1;
1175
    else
1176
        s->nb_channels = 2;
1177
    
1178
    if (bitrate_index != 0) {
1179
        frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1180
        s->bit_rate = frame_size * 1000;
1181
        switch(s->layer) {
1182
        case 1:
1183
            frame_size = (frame_size * 12000) / sample_rate;
1184
            frame_size = (frame_size + padding) * 4;
1185
            break;
1186
        case 2:
1187
            frame_size = (frame_size * 144000) / sample_rate;
1188
            frame_size += padding;
1189
            break;
1190
        default:
1191
        case 3:
1192
            frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1193
            frame_size += padding;
1194
            break;
1195
        }
1196
        s->frame_size = frame_size;
1197
    } else {
1198
        /* if no frame size computed, signal it */
1199
        if (!s->free_format_frame_size)
1200
            return 1;
1201
        /* free format: compute bitrate and real frame size from the
1202
           frame size we extracted by reading the bitstream */
1203
        s->frame_size = s->free_format_frame_size;
1204
        switch(s->layer) {
1205
        case 1:
1206
            s->frame_size += padding  * 4;
1207
            s->bit_rate = (s->frame_size * sample_rate) / 48000;
1208
            break;
1209
        case 2:
1210
            s->frame_size += padding;
1211
            s->bit_rate = (s->frame_size * sample_rate) / 144000;
1212
            break;
1213
        default:
1214
        case 3:
1215
            s->frame_size += padding;
1216
            s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1217
            break;
1218
        }
1219
    }
1220
    
1221
#if defined(DEBUG)
1222
    printf("layer%d, %d Hz, %d kbits/s, ",
1223
           s->layer, s->sample_rate, s->bit_rate);
1224
    if (s->nb_channels == 2) {
1225
        if (s->layer == 3) {
1226
            if (s->mode_ext & MODE_EXT_MS_STEREO)
1227
                printf("ms-");
1228
            if (s->mode_ext & MODE_EXT_I_STEREO)
1229
                printf("i-");
1230
        }
1231
        printf("stereo");
1232
    } else {
1233
        printf("mono");
1234
    }
1235
    printf("\n");
1236
#endif
1237
    return 0;
1238
}
1239

    
1240
/* useful helper to get mpeg audio stream infos. Return -1 if error in
1241
   header, otherwise the coded frame size in bytes */
1242
int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1243
{
1244
    MPADecodeContext s1, *s = &s1;
1245
    memset( s, 0, sizeof(MPADecodeContext) );
1246

    
1247
    if (check_header(head) != 0)
1248
        return -1;
1249

    
1250
    if (decode_header(s, head) != 0) {
1251
        return -1;
1252
    }
1253

    
1254
    switch(s->layer) {
1255
    case 1:
1256
        avctx->frame_size = 384;
1257
        break;
1258
    case 2:
1259
        avctx->frame_size = 1152;
1260
        break;
1261
    default:
1262
    case 3:
1263
        if (s->lsf)
1264
            avctx->frame_size = 576;
1265
        else
1266
            avctx->frame_size = 1152;
1267
        break;
1268
    }
1269

    
1270
    avctx->sample_rate = s->sample_rate;
1271
    avctx->channels = s->nb_channels;
1272
    avctx->bit_rate = s->bit_rate;
1273
    avctx->sub_id = s->layer;
1274
    return s->frame_size;
1275
}
1276

    
1277
/* return the number of decoded frames */
1278
static int mp_decode_layer1(MPADecodeContext *s)
1279
{
1280
    int bound, i, v, n, ch, j, mant;
1281
    uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1282
    uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1283

    
1284
    if (s->mode == MPA_JSTEREO) 
1285
        bound = (s->mode_ext + 1) * 4;
1286
    else
1287
        bound = SBLIMIT;
1288

    
1289
    /* allocation bits */
1290
    for(i=0;i<bound;i++) {
1291
        for(ch=0;ch<s->nb_channels;ch++) {
1292
            allocation[ch][i] = get_bits(&s->gb, 4);
1293
        }
1294
    }
1295
    for(i=bound;i<SBLIMIT;i++) {
1296
        allocation[0][i] = get_bits(&s->gb, 4);
1297
    }
1298

    
1299
    /* scale factors */
1300
    for(i=0;i<bound;i++) {
1301
        for(ch=0;ch<s->nb_channels;ch++) {
1302
            if (allocation[ch][i])
1303
                scale_factors[ch][i] = get_bits(&s->gb, 6);
1304
        }
1305
    }
1306
    for(i=bound;i<SBLIMIT;i++) {
1307
        if (allocation[0][i]) {
1308
            scale_factors[0][i] = get_bits(&s->gb, 6);
1309
            scale_factors[1][i] = get_bits(&s->gb, 6);
1310
        }
1311
    }
1312
    
1313
    /* compute samples */
1314
    for(j=0;j<12;j++) {
1315
        for(i=0;i<bound;i++) {
1316
            for(ch=0;ch<s->nb_channels;ch++) {
1317
                n = allocation[ch][i];
1318
                if (n) {
1319
                    mant = get_bits(&s->gb, n + 1);
1320
                    v = l1_unscale(n, mant, scale_factors[ch][i]);
1321
                } else {
1322
                    v = 0;
1323
                }
1324
                s->sb_samples[ch][j][i] = v;
1325
            }
1326
        }
1327
        for(i=bound;i<SBLIMIT;i++) {
1328
            n = allocation[0][i];
1329
            if (n) {
1330
                mant = get_bits(&s->gb, n + 1);
1331
                v = l1_unscale(n, mant, scale_factors[0][i]);
1332
                s->sb_samples[0][j][i] = v;
1333
                v = l1_unscale(n, mant, scale_factors[1][i]);
1334
                s->sb_samples[1][j][i] = v;
1335
            } else {
1336
                s->sb_samples[0][j][i] = 0;
1337
                s->sb_samples[1][j][i] = 0;
1338
            }
1339
        }
1340
    }
1341
    return 12;
1342
}
1343

    
1344
/* bitrate is in kb/s */
1345
int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1346
{
1347
    int ch_bitrate, table;
1348
    
1349
    ch_bitrate = bitrate / nb_channels;
1350
    if (!lsf) {
1351
        if ((freq == 48000 && ch_bitrate >= 56) ||
1352
            (ch_bitrate >= 56 && ch_bitrate <= 80)) 
1353
            table = 0;
1354
        else if (freq != 48000 && ch_bitrate >= 96) 
1355
            table = 1;
1356
        else if (freq != 32000 && ch_bitrate <= 48) 
1357
            table = 2;
1358
        else 
1359
            table = 3;
1360
    } else {
1361
        table = 4;
1362
    }
1363
    return table;
1364
}
1365

    
1366
static int mp_decode_layer2(MPADecodeContext *s)
1367
{
1368
    int sblimit; /* number of used subbands */
1369
    const unsigned char *alloc_table;
1370
    int table, bit_alloc_bits, i, j, ch, bound, v;
1371
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1372
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1373
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1374
    int scale, qindex, bits, steps, k, l, m, b;
1375

    
1376
    /* select decoding table */
1377
    table = l2_select_table(s->bit_rate / 1000, s->nb_channels, 
1378
                            s->sample_rate, s->lsf);
1379
    sblimit = sblimit_table[table];
1380
    alloc_table = alloc_tables[table];
1381

    
1382
    if (s->mode == MPA_JSTEREO) 
1383
        bound = (s->mode_ext + 1) * 4;
1384
    else
1385
        bound = sblimit;
1386

    
1387
    dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1388

    
1389
    /* sanity check */
1390
    if( bound > sblimit ) bound = sblimit;
1391

    
1392
    /* parse bit allocation */
1393
    j = 0;
1394
    for(i=0;i<bound;i++) {
1395
        bit_alloc_bits = alloc_table[j];
1396
        for(ch=0;ch<s->nb_channels;ch++) {
1397
            bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1398
        }
1399
        j += 1 << bit_alloc_bits;
1400
    }
1401
    for(i=bound;i<sblimit;i++) {
1402
        bit_alloc_bits = alloc_table[j];
1403
        v = get_bits(&s->gb, bit_alloc_bits);
1404
        bit_alloc[0][i] = v;
1405
        bit_alloc[1][i] = v;
1406
        j += 1 << bit_alloc_bits;
1407
    }
1408

    
1409
#ifdef DEBUG
1410
    {
1411
        for(ch=0;ch<s->nb_channels;ch++) {
1412
            for(i=0;i<sblimit;i++)
1413
                printf(" %d", bit_alloc[ch][i]);
1414
            printf("\n");
1415
        }
1416
    }
1417
#endif
1418

    
1419
    /* scale codes */
1420
    for(i=0;i<sblimit;i++) {
1421
        for(ch=0;ch<s->nb_channels;ch++) {
1422
            if (bit_alloc[ch][i]) 
1423
                scale_code[ch][i] = get_bits(&s->gb, 2);
1424
        }
1425
    }
1426
    
1427
    /* scale factors */
1428
    for(i=0;i<sblimit;i++) {
1429
        for(ch=0;ch<s->nb_channels;ch++) {
1430
            if (bit_alloc[ch][i]) {
1431
                sf = scale_factors[ch][i];
1432
                switch(scale_code[ch][i]) {
1433
                default:
1434
                case 0:
1435
                    sf[0] = get_bits(&s->gb, 6);
1436
                    sf[1] = get_bits(&s->gb, 6);
1437
                    sf[2] = get_bits(&s->gb, 6);
1438
                    break;
1439
                case 2:
1440
                    sf[0] = get_bits(&s->gb, 6);
1441
                    sf[1] = sf[0];
1442
                    sf[2] = sf[0];
1443
                    break;
1444
                case 1:
1445
                    sf[0] = get_bits(&s->gb, 6);
1446
                    sf[2] = get_bits(&s->gb, 6);
1447
                    sf[1] = sf[0];
1448
                    break;
1449
                case 3:
1450
                    sf[0] = get_bits(&s->gb, 6);
1451
                    sf[2] = get_bits(&s->gb, 6);
1452
                    sf[1] = sf[2];
1453
                    break;
1454
                }
1455
            }
1456
        }
1457
    }
1458

    
1459
#ifdef DEBUG
1460
    for(ch=0;ch<s->nb_channels;ch++) {
1461
        for(i=0;i<sblimit;i++) {
1462
            if (bit_alloc[ch][i]) {
1463
                sf = scale_factors[ch][i];
1464
                printf(" %d %d %d", sf[0], sf[1], sf[2]);
1465
            } else {
1466
                printf(" -");
1467
            }
1468
        }
1469
        printf("\n");
1470
    }
1471
#endif
1472

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

    
1576
/*
1577
 * Seek back in the stream for backstep bytes (at most 511 bytes)
1578
 */
1579
static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1580
{
1581
    uint8_t *ptr;
1582

    
1583
    /* compute current position in stream */
1584
    ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1585

    
1586
    /* copy old data before current one */
1587
    ptr -= backstep;
1588
    memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] + 
1589
           BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1590
    /* init get bits again */
1591
    init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1592

    
1593
    /* prepare next buffer */
1594
    s->inbuf_index ^= 1;
1595
    s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1596
    s->old_frame_size = s->frame_size;
1597
}
1598

    
1599
static inline void lsf_sf_expand(int *slen,
1600
                                 int sf, int n1, int n2, int n3)
1601
{
1602
    if (n3) {
1603
        slen[3] = sf % n3;
1604
        sf /= n3;
1605
    } else {
1606
        slen[3] = 0;
1607
    }
1608
    if (n2) {
1609
        slen[2] = sf % n2;
1610
        sf /= n2;
1611
    } else {
1612
        slen[2] = 0;
1613
    }
1614
    slen[1] = sf % n1;
1615
    sf /= n1;
1616
    slen[0] = sf;
1617
}
1618

    
1619
static void exponents_from_scale_factors(MPADecodeContext *s, 
1620
                                         GranuleDef *g,
1621
                                         int16_t *exponents)
1622
{
1623
    const uint8_t *bstab, *pretab;
1624
    int len, i, j, k, l, v0, shift, gain, gains[3];
1625
    int16_t *exp_ptr;
1626

    
1627
    exp_ptr = exponents;
1628
    gain = g->global_gain - 210;
1629
    shift = g->scalefac_scale + 1;
1630

    
1631
    bstab = band_size_long[s->sample_rate_index];
1632
    pretab = mpa_pretab[g->preflag];
1633
    for(i=0;i<g->long_end;i++) {
1634
        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1635
        len = bstab[i];
1636
        for(j=len;j>0;j--)
1637
            *exp_ptr++ = v0;
1638
    }
1639

    
1640
    if (g->short_start < 13) {
1641
        bstab = band_size_short[s->sample_rate_index];
1642
        gains[0] = gain - (g->subblock_gain[0] << 3);
1643
        gains[1] = gain - (g->subblock_gain[1] << 3);
1644
        gains[2] = gain - (g->subblock_gain[2] << 3);
1645
        k = g->long_end;
1646
        for(i=g->short_start;i<13;i++) {
1647
            len = bstab[i];
1648
            for(l=0;l<3;l++) {
1649
                v0 = gains[l] - (g->scale_factors[k++] << shift);
1650
                for(j=len;j>0;j--)
1651
                *exp_ptr++ = v0;
1652
            }
1653
        }
1654
    }
1655
}
1656

    
1657
/* handle n = 0 too */
1658
static inline int get_bitsz(GetBitContext *s, int n)
1659
{
1660
    if (n == 0)
1661
        return 0;
1662
    else
1663
        return get_bits(s, n);
1664
}
1665

    
1666
static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1667
                          int16_t *exponents, int end_pos)
1668
{
1669
    int s_index;
1670
    int linbits, code, x, y, l, v, i, j, k, pos;
1671
    GetBitContext last_gb;
1672
    VLC *vlc;
1673
    uint8_t *code_table;
1674

    
1675
    /* low frequencies (called big values) */
1676
    s_index = 0;
1677
    for(i=0;i<3;i++) {
1678
        j = g->region_size[i];
1679
        if (j == 0)
1680
            continue;
1681
        /* select vlc table */
1682
        k = g->table_select[i];
1683
        l = mpa_huff_data[k][0];
1684
        linbits = mpa_huff_data[k][1];
1685
        vlc = &huff_vlc[l];
1686
        code_table = huff_code_table[l];
1687

    
1688
        /* read huffcode and compute each couple */
1689
        for(;j>0;j--) {
1690
            if (get_bits_count(&s->gb) >= end_pos)
1691
                break;
1692
            if (code_table) {
1693
                code = get_vlc(&s->gb, vlc);
1694
                if (code < 0)
1695
                    return -1;
1696
                y = code_table[code];
1697
                x = y >> 4;
1698
                y = y & 0x0f;
1699
            } else {
1700
                x = 0;
1701
                y = 0;
1702
            }
1703
            dprintf("region=%d n=%d x=%d y=%d exp=%d\n", 
1704
                    i, g->region_size[i] - j, x, y, exponents[s_index]);
1705
            if (x) {
1706
                if (x == 15)
1707
                    x += get_bitsz(&s->gb, linbits);
1708
                v = l3_unscale(x, exponents[s_index]);
1709
                if (get_bits1(&s->gb))
1710
                    v = -v;
1711
            } else {
1712
                v = 0;
1713
            }
1714
            g->sb_hybrid[s_index++] = v;
1715
            if (y) {
1716
                if (y == 15)
1717
                    y += get_bitsz(&s->gb, linbits);
1718
                v = l3_unscale(y, exponents[s_index]);
1719
                if (get_bits1(&s->gb))
1720
                    v = -v;
1721
            } else {
1722
                v = 0;
1723
            }
1724
            g->sb_hybrid[s_index++] = v;
1725
        }
1726
    }
1727
            
1728
    /* high frequencies */
1729
    vlc = &huff_quad_vlc[g->count1table_select];
1730
    last_gb.buffer = NULL;
1731
    while (s_index <= 572) {
1732
        pos = get_bits_count(&s->gb);
1733
        if (pos >= end_pos) {
1734
            if (pos > end_pos && last_gb.buffer != NULL) {
1735
                /* some encoders generate an incorrect size for this
1736
                   part. We must go back into the data */
1737
                s_index -= 4;
1738
                s->gb = last_gb;
1739
            }
1740
            break;
1741
        }
1742
        last_gb= s->gb;
1743

    
1744
        code = get_vlc(&s->gb, vlc);
1745
        dprintf("t=%d code=%d\n", g->count1table_select, code);
1746
        if (code < 0)
1747
            return -1;
1748
        for(i=0;i<4;i++) {
1749
            if (code & (8 >> i)) {
1750
                /* non zero value. Could use a hand coded function for
1751
                   'one' value */
1752
                v = l3_unscale(1, exponents[s_index]);
1753
                if(get_bits1(&s->gb))
1754
                    v = -v;
1755
            } else {
1756
                v = 0;
1757
            }
1758
            g->sb_hybrid[s_index++] = v;
1759
        }
1760
    }
1761
    while (s_index < 576)
1762
        g->sb_hybrid[s_index++] = 0;
1763
    return 0;
1764
}
1765

    
1766
/* Reorder short blocks from bitstream order to interleaved order. It
1767
   would be faster to do it in parsing, but the code would be far more
1768
   complicated */
1769
static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1770
{
1771
    int i, j, k, len;
1772
    int32_t *ptr, *dst, *ptr1;
1773
    int32_t tmp[576];
1774

    
1775
    if (g->block_type != 2)
1776
        return;
1777

    
1778
    if (g->switch_point) {
1779
        if (s->sample_rate_index != 8) {
1780
            ptr = g->sb_hybrid + 36;
1781
        } else {
1782
            ptr = g->sb_hybrid + 48;
1783
        }
1784
    } else {
1785
        ptr = g->sb_hybrid;
1786
    }
1787
    
1788
    for(i=g->short_start;i<13;i++) {
1789
        len = band_size_short[s->sample_rate_index][i];
1790
        ptr1 = ptr;
1791
        for(k=0;k<3;k++) {
1792
            dst = tmp + k;
1793
            for(j=len;j>0;j--) {
1794
                *dst = *ptr++;
1795
                dst += 3;
1796
            }
1797
        }
1798
        memcpy(ptr1, tmp, len * 3 * sizeof(int32_t));
1799
    }
1800
}
1801

    
1802
#define ISQRT2 FIXR(0.70710678118654752440)
1803

    
1804
static void compute_stereo(MPADecodeContext *s,
1805
                           GranuleDef *g0, GranuleDef *g1)
1806
{
1807
    int i, j, k, l;
1808
    int32_t v1, v2;
1809
    int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1810
    int32_t (*is_tab)[16];
1811
    int32_t *tab0, *tab1;
1812
    int non_zero_found_short[3];
1813

    
1814
    /* intensity stereo */
1815
    if (s->mode_ext & MODE_EXT_I_STEREO) {
1816
        if (!s->lsf) {
1817
            is_tab = is_table;
1818
            sf_max = 7;
1819
        } else {
1820
            is_tab = is_table_lsf[g1->scalefac_compress & 1];
1821
            sf_max = 16;
1822
        }
1823
            
1824
        tab0 = g0->sb_hybrid + 576;
1825
        tab1 = g1->sb_hybrid + 576;
1826

    
1827
        non_zero_found_short[0] = 0;
1828
        non_zero_found_short[1] = 0;
1829
        non_zero_found_short[2] = 0;
1830
        k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1831
        for(i = 12;i >= g1->short_start;i--) {
1832
            /* for last band, use previous scale factor */
1833
            if (i != 11)
1834
                k -= 3;
1835
            len = band_size_short[s->sample_rate_index][i];
1836
            for(l=2;l>=0;l--) {
1837
                tab0 -= len;
1838
                tab1 -= len;
1839
                if (!non_zero_found_short[l]) {
1840
                    /* test if non zero band. if so, stop doing i-stereo */
1841
                    for(j=0;j<len;j++) {
1842
                        if (tab1[j] != 0) {
1843
                            non_zero_found_short[l] = 1;
1844
                            goto found1;
1845
                        }
1846
                    }
1847
                    sf = g1->scale_factors[k + l];
1848
                    if (sf >= sf_max)
1849
                        goto found1;
1850

    
1851
                    v1 = is_tab[0][sf];
1852
                    v2 = is_tab[1][sf];
1853
                    for(j=0;j<len;j++) {
1854
                        tmp0 = tab0[j];
1855
                        tab0[j] = MULL(tmp0, v1);
1856
                        tab1[j] = MULL(tmp0, v2);
1857
                    }
1858
                } else {
1859
                found1:
1860
                    if (s->mode_ext & MODE_EXT_MS_STEREO) {
1861
                        /* lower part of the spectrum : do ms stereo
1862
                           if enabled */
1863
                        for(j=0;j<len;j++) {
1864
                            tmp0 = tab0[j];
1865
                            tmp1 = tab1[j];
1866
                            tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1867
                            tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1868
                        }
1869
                    }
1870
                }
1871
            }
1872
        }
1873

    
1874
        non_zero_found = non_zero_found_short[0] | 
1875
            non_zero_found_short[1] | 
1876
            non_zero_found_short[2];
1877

    
1878
        for(i = g1->long_end - 1;i >= 0;i--) {
1879
            len = band_size_long[s->sample_rate_index][i];
1880
            tab0 -= len;
1881
            tab1 -= len;
1882
            /* test if non zero band. if so, stop doing i-stereo */
1883
            if (!non_zero_found) {
1884
                for(j=0;j<len;j++) {
1885
                    if (tab1[j] != 0) {
1886
                        non_zero_found = 1;
1887
                        goto found2;
1888
                    }
1889
                }
1890
                /* for last band, use previous scale factor */
1891
                k = (i == 21) ? 20 : i;
1892
                sf = g1->scale_factors[k];
1893
                if (sf >= sf_max)
1894
                    goto found2;
1895
                v1 = is_tab[0][sf];
1896
                v2 = is_tab[1][sf];
1897
                for(j=0;j<len;j++) {
1898
                    tmp0 = tab0[j];
1899
                    tab0[j] = MULL(tmp0, v1);
1900
                    tab1[j] = MULL(tmp0, v2);
1901
                }
1902
            } else {
1903
            found2:
1904
                if (s->mode_ext & MODE_EXT_MS_STEREO) {
1905
                    /* lower part of the spectrum : do ms stereo
1906
                       if enabled */
1907
                    for(j=0;j<len;j++) {
1908
                        tmp0 = tab0[j];
1909
                        tmp1 = tab1[j];
1910
                        tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1911
                        tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1912
                    }
1913
                }
1914
            }
1915
        }
1916
    } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1917
        /* ms stereo ONLY */
1918
        /* NOTE: the 1/sqrt(2) normalization factor is included in the
1919
           global gain */
1920
        tab0 = g0->sb_hybrid;
1921
        tab1 = g1->sb_hybrid;
1922
        for(i=0;i<576;i++) {
1923
            tmp0 = tab0[i];
1924
            tmp1 = tab1[i];
1925
            tab0[i] = tmp0 + tmp1;
1926
            tab1[i] = tmp0 - tmp1;
1927
        }
1928
    }
1929
}
1930

    
1931
static void compute_antialias_integer(MPADecodeContext *s,
1932
                              GranuleDef *g)
1933
{
1934
    int32_t *ptr, *p0, *p1, *csa;
1935
    int n, i, j;
1936

    
1937
    /* we antialias only "long" bands */
1938
    if (g->block_type == 2) {
1939
        if (!g->switch_point)
1940
            return;
1941
        /* XXX: check this for 8000Hz case */
1942
        n = 1;
1943
    } else {
1944
        n = SBLIMIT - 1;
1945
    }
1946
    
1947
    ptr = g->sb_hybrid + 18;
1948
    for(i = n;i > 0;i--) {
1949
        p0 = ptr - 1;
1950
        p1 = ptr;
1951
        csa = &csa_table[0][0];       
1952
        for(j=0;j<4;j++) {
1953
            int tmp0 = *p0;
1954
            int tmp1 = *p1;
1955
#if 0
1956
            *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1957
            *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1958
#else
1959
            int64_t tmp2= MUL64(tmp0 + tmp1, csa[0]);
1960
            *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2]));
1961
            *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3]));
1962
#endif
1963
            p0--; p1++;
1964
            csa += 4;
1965
            tmp0 = *p0;
1966
            tmp1 = *p1;
1967
#if 0
1968
            *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1969
            *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1970
#else
1971
            tmp2= MUL64(tmp0 + tmp1, csa[0]);
1972
            *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2]));
1973
            *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3]));
1974
#endif
1975
            p0--; p1++;
1976
            csa += 4;
1977
        }
1978
        ptr += 18;       
1979
    }
1980
}
1981

    
1982
static void compute_antialias_float(MPADecodeContext *s,
1983
                              GranuleDef *g)
1984
{
1985
    int32_t *ptr, *p0, *p1;
1986
    int n, i, j;
1987

    
1988
    /* we antialias only "long" bands */
1989
    if (g->block_type == 2) {
1990
        if (!g->switch_point)
1991
            return;
1992
        /* XXX: check this for 8000Hz case */
1993
        n = 1;
1994
    } else {
1995
        n = SBLIMIT - 1;
1996
    }
1997
    
1998
    ptr = g->sb_hybrid + 18;
1999
    for(i = n;i > 0;i--) {
2000
        float *csa = &csa_table_float[0][0];       
2001
        p0 = ptr - 1;
2002
        p1 = ptr;
2003
        for(j=0;j<4;j++) {
2004
            float tmp0 = *p0;
2005
            float tmp1 = *p1;
2006
#if 1
2007
            *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]);
2008
            *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]);
2009
#else
2010
            float tmp2= (tmp0 + tmp1) * csa[0];
2011
            *p0 = lrintf(tmp2 - tmp1 * csa[2]);
2012
            *p1 = lrintf(tmp2 + tmp0 * csa[3]);
2013
#endif
2014
            p0--; p1++;
2015
            csa += 4;
2016
            tmp0 = *p0;
2017
            tmp1 = *p1;
2018
#if 1
2019
            *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]);
2020
            *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]);
2021
#else
2022
            tmp2= (tmp0 + tmp1) * csa[0];
2023
            *p0 = lrintf(tmp2 - tmp1 * csa[2]);
2024
            *p1 = lrintf(tmp2 + tmp0 * csa[3]);
2025
#endif
2026
            p0--; p1++;
2027
            csa += 4;
2028
        }
2029
        ptr += 18;       
2030
    }
2031
}
2032

    
2033
static void compute_imdct(MPADecodeContext *s,
2034
                          GranuleDef *g, 
2035
                          int32_t *sb_samples,
2036
                          int32_t *mdct_buf)
2037
{
2038
    int32_t *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1;
2039
    int32_t in[6];
2040
    int32_t out[36];
2041
    int32_t out2[12];
2042
    int i, j, k, mdct_long_end, v, sblimit;
2043

    
2044
    /* find last non zero block */
2045
    ptr = g->sb_hybrid + 576;
2046
    ptr1 = g->sb_hybrid + 2 * 18;
2047
    while (ptr >= ptr1) {
2048
        ptr -= 6;
2049
        v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2050
        if (v != 0)
2051
            break;
2052
    }
2053
    sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2054

    
2055
    if (g->block_type == 2) {
2056
        /* XXX: check for 8000 Hz */
2057
        if (g->switch_point)
2058
            mdct_long_end = 2;
2059
        else
2060
            mdct_long_end = 0;
2061
    } else {
2062
        mdct_long_end = sblimit;
2063
    }
2064

    
2065
    buf = mdct_buf;
2066
    ptr = g->sb_hybrid;
2067
    for(j=0;j<mdct_long_end;j++) {
2068
        imdct36(out, ptr);
2069
        /* apply window & overlap with previous buffer */
2070
        out_ptr = sb_samples + j;
2071
        /* select window */
2072
        if (g->switch_point && j < 2)
2073
            win1 = mdct_win[0];
2074
        else
2075
            win1 = mdct_win[g->block_type];
2076
        /* select frequency inversion */
2077
        win = win1 + ((4 * 36) & -(j & 1));
2078
        for(i=0;i<18;i++) {
2079
            *out_ptr = MULL(out[i], win[i]) + buf[i];
2080
            buf[i] = MULL(out[i + 18], win[i + 18]);
2081
            out_ptr += SBLIMIT;
2082
        }
2083
        ptr += 18;
2084
        buf += 18;
2085
    }
2086
    for(j=mdct_long_end;j<sblimit;j++) {
2087
        for(i=0;i<6;i++) {
2088
            out[i] = 0;
2089
            out[6 + i] = 0;
2090
            out[30+i] = 0;
2091
        }
2092
        /* select frequency inversion */
2093
        win = mdct_win[2] + ((4 * 36) & -(j & 1));
2094
        buf2 = out + 6;
2095
        for(k=0;k<3;k++) {
2096
            /* reorder input for short mdct */
2097
            ptr1 = ptr + k;
2098
            for(i=0;i<6;i++) {
2099
                in[i] = *ptr1;
2100
                ptr1 += 3;
2101
            }
2102
            imdct12(out2, in);
2103
            /* apply 12 point window and do small overlap */
2104
            for(i=0;i<6;i++) {
2105
                buf2[i] = MULL(out2[i], win[i]) + buf2[i];
2106
                buf2[i + 6] = MULL(out2[i + 6], win[i + 6]);
2107
            }
2108
            buf2 += 6;
2109
        }
2110
        /* overlap */
2111
        out_ptr = sb_samples + j;
2112
        for(i=0;i<18;i++) {
2113
            *out_ptr = out[i] + buf[i];
2114
            buf[i] = out[i + 18];
2115
            out_ptr += SBLIMIT;
2116
        }
2117
        ptr += 18;
2118
        buf += 18;
2119
    }
2120
    /* zero bands */
2121
    for(j=sblimit;j<SBLIMIT;j++) {
2122
        /* overlap */
2123
        out_ptr = sb_samples + j;
2124
        for(i=0;i<18;i++) {
2125
            *out_ptr = buf[i];
2126
            buf[i] = 0;
2127
            out_ptr += SBLIMIT;
2128
        }
2129
        buf += 18;
2130
    }
2131
}
2132

    
2133
#if defined(DEBUG)
2134
void sample_dump(int fnum, int32_t *tab, int n)
2135
{
2136
    static FILE *files[16], *f;
2137
    char buf[512];
2138
    int i;
2139
    int32_t v;
2140
    
2141
    f = files[fnum];
2142
    if (!f) {
2143
        snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm", 
2144
                fnum, 
2145
#ifdef USE_HIGHPRECISION
2146
                "hp"
2147
#else
2148
                "lp"
2149
#endif
2150
                );
2151
        f = fopen(buf, "w");
2152
        if (!f)
2153
            return;
2154
        files[fnum] = f;
2155
    }
2156
    
2157
    if (fnum == 0) {
2158
        static int pos = 0;
2159
        printf("pos=%d\n", pos);
2160
        for(i=0;i<n;i++) {
2161
            printf(" %0.4f", (double)tab[i] / FRAC_ONE);
2162
            if ((i % 18) == 17)
2163
                printf("\n");
2164
        }
2165
        pos += n;
2166
    }
2167
    for(i=0;i<n;i++) {
2168
        /* normalize to 23 frac bits */
2169
        v = tab[i] << (23 - FRAC_BITS);
2170
        fwrite(&v, 1, sizeof(int32_t), f);
2171
    }
2172
}
2173
#endif
2174

    
2175

    
2176
/* main layer3 decoding function */
2177
static int mp_decode_layer3(MPADecodeContext *s)
2178
{
2179
    int nb_granules, main_data_begin, private_bits;
2180
    int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2181
    GranuleDef granules[2][2], *g;
2182
    int16_t exponents[576];
2183

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

    
2275
            /* compute band indexes */
2276
            if (g->block_type == 2) {
2277
                if (g->switch_point) {
2278
                    /* if switched mode, we handle the 36 first samples as
2279
                       long blocks.  For 8000Hz, we handle the 48 first
2280
                       exponents as long blocks (XXX: check this!) */
2281
                    if (s->sample_rate_index <= 2)
2282
                        g->long_end = 8;
2283
                    else if (s->sample_rate_index != 8)
2284
                        g->long_end = 6;
2285
                    else
2286
                        g->long_end = 4; /* 8000 Hz */
2287
                    
2288
                    if (s->sample_rate_index != 8)
2289
                        g->short_start = 3;
2290
                    else
2291
                        g->short_start = 2; 
2292
                } else {
2293
                    g->long_end = 0;
2294
                    g->short_start = 0;
2295
                }
2296
            } else {
2297
                g->short_start = 13;
2298
                g->long_end = 22;
2299
            }
2300
            
2301
            g->preflag = 0;
2302
            if (!s->lsf)
2303
                g->preflag = get_bits(&s->gb, 1);
2304
            g->scalefac_scale = get_bits(&s->gb, 1);
2305
            g->count1table_select = get_bits(&s->gb, 1);
2306
            dprintf("block_type=%d switch_point=%d\n",
2307
                    g->block_type, g->switch_point);
2308
        }
2309
    }
2310

    
2311
  if (!s->adu_mode) {
2312
    /* now we get bits from the main_data_begin offset */
2313
    dprintf("seekback: %d\n", main_data_begin);
2314
    seek_to_maindata(s, main_data_begin);
2315
  }
2316

    
2317
    for(gr=0;gr<nb_granules;gr++) {
2318
        for(ch=0;ch<s->nb_channels;ch++) {
2319
            g = &granules[ch][gr];
2320
            
2321
            bits_pos = get_bits_count(&s->gb);
2322
            
2323
            if (!s->lsf) {
2324
                uint8_t *sc;
2325
                int slen, slen1, slen2;
2326

    
2327
                /* MPEG1 scale factors */
2328
                slen1 = slen_table[0][g->scalefac_compress];
2329
                slen2 = slen_table[1][g->scalefac_compress];
2330
                dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2331
                if (g->block_type == 2) {
2332
                    n = g->switch_point ? 17 : 18;
2333
                    j = 0;
2334
                    for(i=0;i<n;i++)
2335
                        g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2336
                    for(i=0;i<18;i++)
2337
                        g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2338
                    for(i=0;i<3;i++)
2339
                        g->scale_factors[j++] = 0;
2340
                } else {
2341
                    sc = granules[ch][0].scale_factors;
2342
                    j = 0;
2343
                    for(k=0;k<4;k++) {
2344
                        n = (k == 0 ? 6 : 5);
2345
                        if ((g->scfsi & (0x8 >> k)) == 0) {
2346
                            slen = (k < 2) ? slen1 : slen2;
2347
                            for(i=0;i<n;i++)
2348
                                g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2349
                        } else {
2350
                            /* simply copy from last granule */
2351
                            for(i=0;i<n;i++) {
2352
                                g->scale_factors[j] = sc[j];
2353
                                j++;
2354
                            }
2355
                        }
2356
                    }
2357
                    g->scale_factors[j++] = 0;
2358
                }
2359
#if defined(DEBUG)
2360
                {
2361
                    printf("scfsi=%x gr=%d ch=%d scale_factors:\n", 
2362
                           g->scfsi, gr, ch);
2363
                    for(i=0;i<j;i++)
2364
                        printf(" %d", g->scale_factors[i]);
2365
                    printf("\n");
2366
                }
2367
#endif
2368
            } else {
2369
                int tindex, tindex2, slen[4], sl, sf;
2370

    
2371
                /* LSF scale factors */
2372
                if (g->block_type == 2) {
2373
                    tindex = g->switch_point ? 2 : 1;
2374
                } else {
2375
                    tindex = 0;
2376
                }
2377
                sf = g->scalefac_compress;
2378
                if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2379
                    /* intensity stereo case */
2380
                    sf >>= 1;
2381
                    if (sf < 180) {
2382
                        lsf_sf_expand(slen, sf, 6, 6, 0);
2383
                        tindex2 = 3;
2384
                    } else if (sf < 244) {
2385
                        lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2386
                        tindex2 = 4;
2387
                    } else {
2388
                        lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2389
                        tindex2 = 5;
2390
                    }
2391
                } else {
2392
                    /* normal case */
2393
                    if (sf < 400) {
2394
                        lsf_sf_expand(slen, sf, 5, 4, 4);
2395
                        tindex2 = 0;
2396
                    } else if (sf < 500) {
2397
                        lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2398
                        tindex2 = 1;
2399
                    } else {
2400
                        lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2401
                        tindex2 = 2;
2402
                        g->preflag = 1;
2403
                    }
2404
                }
2405

    
2406
                j = 0;
2407
                for(k=0;k<4;k++) {
2408
                    n = lsf_nsf_table[tindex2][tindex][k];
2409
                    sl = slen[k];
2410
                    for(i=0;i<n;i++)
2411
                        g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2412
                }
2413
                /* XXX: should compute exact size */
2414
                for(;j<40;j++)
2415
                    g->scale_factors[j] = 0;
2416
#if defined(DEBUG)
2417
                {
2418
                    printf("gr=%d ch=%d scale_factors:\n", 
2419
                           gr, ch);
2420
                    for(i=0;i<40;i++)
2421
                        printf(" %d", g->scale_factors[i]);
2422
                    printf("\n");
2423
                }
2424
#endif
2425
            }
2426

    
2427
            exponents_from_scale_factors(s, g, exponents);
2428

    
2429
            /* read Huffman coded residue */
2430
            if (huffman_decode(s, g, exponents,
2431
                               bits_pos + g->part2_3_length) < 0)
2432
                return -1;
2433
#if defined(DEBUG)
2434
            sample_dump(0, g->sb_hybrid, 576);
2435
#endif
2436

    
2437
            /* skip extension bits */
2438
            bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2439
            if (bits_left < 0) {
2440
                dprintf("bits_left=%d\n", bits_left);
2441
                return -1;
2442
            }
2443
            while (bits_left >= 16) {
2444
                skip_bits(&s->gb, 16);
2445
                bits_left -= 16;
2446
            }
2447
            if (bits_left > 0)
2448
                skip_bits(&s->gb, bits_left);
2449
        } /* ch */
2450

    
2451
        if (s->nb_channels == 2)
2452
            compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2453

    
2454
        for(ch=0;ch<s->nb_channels;ch++) {
2455
            g = &granules[ch][gr];
2456

    
2457
            reorder_block(s, g);
2458
#if defined(DEBUG)
2459
            sample_dump(0, g->sb_hybrid, 576);
2460
#endif
2461
            s->compute_antialias(s, g);
2462
#if defined(DEBUG)
2463
            sample_dump(1, g->sb_hybrid, 576);
2464
#endif
2465
            compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); 
2466
#if defined(DEBUG)
2467
            sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2468
#endif
2469
        }
2470
    } /* gr */
2471
    return nb_granules * 18;
2472
}
2473

    
2474
static int mp_decode_frame(MPADecodeContext *s, 
2475
                           short *samples)
2476
{
2477
    int i, nb_frames, ch;
2478
    short *samples_ptr;
2479

    
2480
    init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, 
2481
                  (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2482
    
2483
    /* skip error protection field */
2484
    if (s->error_protection)
2485
        get_bits(&s->gb, 16);
2486

    
2487
    dprintf("frame %d:\n", s->frame_count);
2488
    switch(s->layer) {
2489
    case 1:
2490
        nb_frames = mp_decode_layer1(s);
2491
        break;
2492
    case 2:
2493
        nb_frames = mp_decode_layer2(s);
2494
        break;
2495
    case 3:
2496
    default:
2497
        nb_frames = mp_decode_layer3(s);
2498
        break;
2499
    }
2500
#if defined(DEBUG)
2501
    for(i=0;i<nb_frames;i++) {
2502
        for(ch=0;ch<s->nb_channels;ch++) {
2503
            int j;
2504
            printf("%d-%d:", i, ch);
2505
            for(j=0;j<SBLIMIT;j++)
2506
                printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2507
            printf("\n");
2508
        }
2509
    }
2510
#endif
2511
    /* apply the synthesis filter */
2512
    for(ch=0;ch<s->nb_channels;ch++) {
2513
        samples_ptr = samples + ch;
2514
        for(i=0;i<nb_frames;i++) {
2515
            ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2516
                         window,
2517
                         samples_ptr, s->nb_channels,
2518
                         s->sb_samples[ch][i]);
2519
            samples_ptr += 32 * s->nb_channels;
2520
        }
2521
    }
2522
#ifdef DEBUG
2523
    s->frame_count++;        
2524
#endif
2525
    return nb_frames * 32 * sizeof(short) * s->nb_channels;
2526
}
2527

    
2528
static int decode_frame(AVCodecContext * avctx,
2529
                        void *data, int *data_size,
2530
                        uint8_t * buf, int buf_size)
2531
{
2532
    MPADecodeContext *s = avctx->priv_data;
2533
    uint32_t header;
2534
    uint8_t *buf_ptr;
2535
    int len, out_size;
2536
    short *out_samples = data;
2537

    
2538
    buf_ptr = buf;
2539
    while (buf_size > 0) {
2540
        len = s->inbuf_ptr - s->inbuf;
2541
        if (s->frame_size == 0) {
2542
            /* special case for next header for first frame in free
2543
               format case (XXX: find a simpler method) */
2544
            if (s->free_format_next_header != 0) {
2545
                s->inbuf[0] = s->free_format_next_header >> 24;
2546
                s->inbuf[1] = s->free_format_next_header >> 16;
2547
                s->inbuf[2] = s->free_format_next_header >> 8;
2548
                s->inbuf[3] = s->free_format_next_header;
2549
                s->inbuf_ptr = s->inbuf + 4;
2550
                s->free_format_next_header = 0;
2551
                goto got_header;
2552
            }
2553
            /* no header seen : find one. We need at least HEADER_SIZE
2554
               bytes to parse it */
2555
            len = HEADER_SIZE - len;
2556
            if (len > buf_size)
2557
                len = buf_size;
2558
            if (len > 0) {
2559
                memcpy(s->inbuf_ptr, buf_ptr, len);
2560
                buf_ptr += len;
2561
                buf_size -= len;
2562
                s->inbuf_ptr += len;
2563
            }
2564
            if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2565
            got_header:
2566
                header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2567
                    (s->inbuf[2] << 8) | s->inbuf[3];
2568

    
2569
                if (check_header(header) < 0) {
2570
                    /* no sync found : move by one byte (inefficient, but simple!) */
2571
                    memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2572
                    s->inbuf_ptr--;
2573
                    dprintf("skip %x\n", header);
2574
                    /* reset free format frame size to give a chance
2575
                       to get a new bitrate */
2576
                    s->free_format_frame_size = 0;
2577
                } else {
2578
                    if (decode_header(s, header) == 1) {
2579
                        /* free format: prepare to compute frame size */
2580
                        s->frame_size = -1;
2581
                    }
2582
                    /* update codec info */
2583
                    avctx->sample_rate = s->sample_rate;
2584
                    avctx->channels = s->nb_channels;
2585
                    avctx->bit_rate = s->bit_rate;
2586
                    avctx->sub_id = s->layer;
2587
                    switch(s->layer) {
2588
                    case 1:
2589
                        avctx->frame_size = 384;
2590
                        break;
2591
                    case 2:
2592
                        avctx->frame_size = 1152;
2593
                        break;
2594
                    case 3:
2595
                        if (s->lsf)
2596
                            avctx->frame_size = 576;
2597
                        else
2598
                            avctx->frame_size = 1152;
2599
                        break;
2600
                    }
2601
                }
2602
            }
2603
        } else if (s->frame_size == -1) {
2604
            /* free format : find next sync to compute frame size */
2605
            len = MPA_MAX_CODED_FRAME_SIZE - len;
2606
            if (len > buf_size)
2607
                len = buf_size;
2608
            if (len == 0) {
2609
                /* frame too long: resync */
2610
                s->frame_size = 0;
2611
                memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2612
                s->inbuf_ptr--;
2613
            } else {
2614
                uint8_t *p, *pend;
2615
                uint32_t header1;
2616
                int padding;
2617

    
2618
                memcpy(s->inbuf_ptr, buf_ptr, len);
2619
                /* check for header */
2620
                p = s->inbuf_ptr - 3;
2621
                pend = s->inbuf_ptr + len - 4;
2622
                while (p <= pend) {
2623
                    header = (p[0] << 24) | (p[1] << 16) |
2624
                        (p[2] << 8) | p[3];
2625
                    header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2626
                        (s->inbuf[2] << 8) | s->inbuf[3];
2627
                    /* check with high probability that we have a
2628
                       valid header */
2629
                    if ((header & SAME_HEADER_MASK) ==
2630
                        (header1 & SAME_HEADER_MASK)) {
2631
                        /* header found: update pointers */
2632
                        len = (p + 4) - s->inbuf_ptr;
2633
                        buf_ptr += len;
2634
                        buf_size -= len;
2635
                        s->inbuf_ptr = p;
2636
                        /* compute frame size */
2637
                        s->free_format_next_header = header;
2638
                        s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2639
                        padding = (header1 >> 9) & 1;
2640
                        if (s->layer == 1)
2641
                            s->free_format_frame_size -= padding * 4;
2642
                        else
2643
                            s->free_format_frame_size -= padding;
2644
                        dprintf("free frame size=%d padding=%d\n", 
2645
                                s->free_format_frame_size, padding);
2646
                        decode_header(s, header1);
2647
                        goto next_data;
2648
                    }
2649
                    p++;
2650
                }
2651
                /* not found: simply increase pointers */
2652
                buf_ptr += len;
2653
                s->inbuf_ptr += len;
2654
                buf_size -= len;
2655
            }
2656
        } else if (len < s->frame_size) {
2657
            if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2658
                s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2659
            len = s->frame_size - len;
2660
            if (len > buf_size)
2661
                len = buf_size;
2662
            memcpy(s->inbuf_ptr, buf_ptr, len);
2663
            buf_ptr += len;
2664
            s->inbuf_ptr += len;
2665
            buf_size -= len;
2666
        }
2667
    next_data:
2668
        if (s->frame_size > 0 && 
2669
            (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2670
            if (avctx->parse_only) {
2671
                /* simply return the frame data */
2672
                *(uint8_t **)data = s->inbuf;
2673
                out_size = s->inbuf_ptr - s->inbuf;
2674
            } else {
2675
                out_size = mp_decode_frame(s, out_samples);
2676
            }
2677
            s->inbuf_ptr = s->inbuf;
2678
            s->frame_size = 0;
2679
            *data_size = out_size;
2680
            break;
2681
        }
2682
    }
2683
    return buf_ptr - buf;
2684
}
2685

    
2686

    
2687
static int decode_frame_adu(AVCodecContext * avctx,
2688
                        void *data, int *data_size,
2689
                        uint8_t * buf, int buf_size)
2690
{
2691
    MPADecodeContext *s = avctx->priv_data;
2692
    uint32_t header;
2693
    int len, out_size;
2694
    short *out_samples = data;
2695

    
2696
    len = buf_size;
2697

    
2698
    // Discard too short frames
2699
    if (buf_size < HEADER_SIZE) {
2700
        *data_size = 0;
2701
        return buf_size;
2702
    }
2703

    
2704

    
2705
    if (len > MPA_MAX_CODED_FRAME_SIZE)
2706
        len = MPA_MAX_CODED_FRAME_SIZE;
2707

    
2708
    memcpy(s->inbuf, buf, len);
2709
    s->inbuf_ptr = s->inbuf + len;
2710

    
2711
    // Get header and restore sync word
2712
    header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2713
              (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
2714

    
2715
    if (check_header(header) < 0) { // Bad header, discard frame
2716
        *data_size = 0;
2717
        return buf_size;
2718
    }
2719

    
2720
    decode_header(s, header);
2721
    /* update codec info */
2722
    avctx->sample_rate = s->sample_rate;
2723
    avctx->channels = s->nb_channels;
2724
    avctx->bit_rate = s->bit_rate;
2725
    avctx->sub_id = s->layer;
2726

    
2727
    avctx->frame_size=s->frame_size = len;
2728

    
2729
    if (avctx->parse_only) {
2730
        /* simply return the frame data */
2731
        *(uint8_t **)data = s->inbuf;
2732
        out_size = s->inbuf_ptr - s->inbuf;
2733
    } else {
2734
        out_size = mp_decode_frame(s, out_samples);
2735
    }
2736

    
2737
    *data_size = out_size;
2738
    return buf_size;
2739
}
2740

    
2741

    
2742
AVCodec mp2_decoder =
2743
{
2744
    "mp2",
2745
    CODEC_TYPE_AUDIO,
2746
    CODEC_ID_MP2,
2747
    sizeof(MPADecodeContext),
2748
    decode_init,
2749
    NULL,
2750
    NULL,
2751
    decode_frame,
2752
    CODEC_CAP_PARSE_ONLY,
2753
};
2754

    
2755
AVCodec mp3_decoder =
2756
{
2757
    "mp3",
2758
    CODEC_TYPE_AUDIO,
2759
    CODEC_ID_MP3,
2760
    sizeof(MPADecodeContext),
2761
    decode_init,
2762
    NULL,
2763
    NULL,
2764
    decode_frame,
2765
    CODEC_CAP_PARSE_ONLY,
2766
};
2767

    
2768
AVCodec mp3adu_decoder =
2769
{
2770
    "mp3adu",
2771
    CODEC_TYPE_AUDIO,
2772
    CODEC_ID_MP3ADU,
2773
    sizeof(MPADecodeContext),
2774
    decode_init,
2775
    NULL,
2776
    NULL,
2777
    decode_frame_adu,
2778
    CODEC_CAP_PARSE_ONLY,
2779
};