Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudiodec.c @ 5bb6fbb3

History | View | Annotate | Download (74.7 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * MPEG Audio decoder
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2001, 2002 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 ff4ec49e Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 *
10 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 ff4ec49e Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 */
19 239c2f4c Fabrice Bellard
//#define DEBUG
20 de6d9b64 Fabrice Bellard
#include "avcodec.h"
21 239c2f4c Fabrice Bellard
#include "mpegaudio.h"
22 de6d9b64 Fabrice Bellard
23
/*
24 239c2f4c Fabrice Bellard
 * TODO:
25
 *  - in low precision mode, use more 16 bit multiplies in synth filter
26
 *  - test lsf / mpeg25 extensively.
27 de6d9b64 Fabrice Bellard
 */
28
29 239c2f4c Fabrice Bellard
/* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
30
   audio decoder */
31 81552334 Fabrice Bellard
#ifdef CONFIG_MPEGAUDIO_HP
32
#define USE_HIGHPRECISION
33
#endif
34 239c2f4c Fabrice Bellard
35
#ifdef USE_HIGHPRECISION
36
#define FRAC_BITS   23   /* fractional bits for sb_samples and dct */
37
#define WFRAC_BITS  16   /* fractional bits for window */
38
#else
39
#define FRAC_BITS   15   /* fractional bits for sb_samples and dct */
40
#define WFRAC_BITS  14   /* fractional bits for window */
41
#endif
42
43
#define FRAC_ONE    (1 << FRAC_BITS)
44
45
#define MULL(a,b) (((INT64)(a) * (INT64)(b)) >> FRAC_BITS)
46
#define MUL64(a,b) ((INT64)(a) * (INT64)(b))
47
#define FIX(a)   ((int)((a) * FRAC_ONE))
48
/* WARNING: only correct for posititive numbers */
49
#define FIXR(a)   ((int)((a) * FRAC_ONE + 0.5))
50
#define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
51
52
#if FRAC_BITS <= 15
53
typedef INT16 MPA_INT;
54
#else
55
typedef INT32 MPA_INT;
56
#endif
57
58
/****************/
59
60 de6d9b64 Fabrice Bellard
#define HEADER_SIZE 4
61
#define BACKSTEP_SIZE 512
62
63
typedef struct MPADecodeContext {
64 239c2f4c Fabrice Bellard
    UINT8 inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE];        /* input buffer */
65 de6d9b64 Fabrice Bellard
    int inbuf_index;
66
    UINT8 *inbuf_ptr, *inbuf;
67
    int frame_size;
68 239c2f4c Fabrice Bellard
    int free_format_frame_size; /* frame size in case of free format
69
                                   (zero if currently unknown) */
70
    /* next header (used in free format parsing) */
71
    UINT32 free_format_next_header; 
72 de6d9b64 Fabrice Bellard
    int error_protection;
73
    int layer;
74
    int sample_rate;
75 239c2f4c Fabrice Bellard
    int sample_rate_index; /* between 0 and 8 */
76 de6d9b64 Fabrice Bellard
    int bit_rate;
77
    int old_frame_size;
78
    GetBitContext gb;
79 239c2f4c Fabrice Bellard
    int nb_channels;
80
    int mode;
81
    int mode_ext;
82
    int lsf;
83
    MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2];
84
    int synth_buf_offset[MPA_MAX_CHANNELS];
85
    INT32 sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT];
86
    INT32 mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
87
#ifdef DEBUG
88
    int frame_count;
89
#endif
90 de6d9b64 Fabrice Bellard
} MPADecodeContext;
91
92 239c2f4c Fabrice Bellard
/* layer 3 "granule" */
93
typedef struct GranuleDef {
94
    UINT8 scfsi;
95
    int part2_3_length;
96
    int big_values;
97
    int global_gain;
98
    int scalefac_compress;
99
    UINT8 block_type;
100
    UINT8 switch_point;
101
    int table_select[3];
102
    int subblock_gain[3];
103
    UINT8 scalefac_scale;
104
    UINT8 count1table_select;
105
    int region_size[3]; /* number of huffman codes in each region */
106
    int preflag;
107
    int short_start, long_end; /* long/short band indexes */
108
    UINT8 scale_factors[40];
109
    INT32 sb_hybrid[SBLIMIT * 18]; /* 576 samples */
110
} GranuleDef;
111 de6d9b64 Fabrice Bellard
112 239c2f4c Fabrice Bellard
#define MODE_EXT_MS_STEREO 2
113
#define MODE_EXT_I_STEREO  1
114
115
/* layer 3 huffman tables */
116
typedef struct HuffTable {
117
    int xsize;
118
    const UINT8 *bits;
119
    const UINT16 *codes;
120
} HuffTable;
121
122
#include "mpegaudiodectab.h"
123
124
/* vlc structure for decoding layer 3 huffman tables */
125
static VLC huff_vlc[16]; 
126
static UINT8 *huff_code_table[16];
127
static VLC huff_quad_vlc[2];
128
/* computed from band_size_long */
129
static UINT16 band_index_long[9][23];
130
/* XXX: free when all decoders are closed */
131
#define TABLE_4_3_SIZE (8191 + 16)
132 747a67fb Fabrice Bellard
static INT8  *table_4_3_exp;
133 239c2f4c Fabrice Bellard
#if FRAC_BITS <= 15
134
static UINT16 *table_4_3_value;
135
#else
136
static UINT32 *table_4_3_value;
137
#endif
138
/* intensity stereo coef table */
139
static INT32 is_table[2][16];
140
static INT32 is_table_lsf[2][2][16];
141
static INT32 csa_table[8][2];
142
static INT32 mdct_win[8][36];
143
144
/* lower 2 bits: modulo 3, higher bits: shift */
145
static UINT16 scale_factor_modshift[64];
146
/* [i][j]:  2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
147
static INT32 scale_factor_mult[15][3];
148
/* mult table for layer 2 group quantization */
149
150
#define SCALE_GEN(v) \
151
{ FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
152
153
static INT32 scale_factor_mult2[3][3] = {
154 81552334 Fabrice Bellard
    SCALE_GEN(4.0 / 3.0), /* 3 steps */
155
    SCALE_GEN(4.0 / 5.0), /* 5 steps */
156
    SCALE_GEN(4.0 / 9.0), /* 9 steps */
157 239c2f4c Fabrice Bellard
};
158
159
/* 2^(n/4) */
160
static UINT32 scale_factor_mult3[4] = {
161
    FIXR(1.0),
162
    FIXR(1.18920711500272106671),
163
    FIXR(1.41421356237309504880),
164
    FIXR(1.68179283050742908605),
165 de6d9b64 Fabrice Bellard
};
166
167 239c2f4c Fabrice Bellard
static MPA_INT window[512];
168
    
169
/* layer 1 unscaling */
170
/* n = number of bits of the mantissa minus 1 */
171
static inline int l1_unscale(int n, int mant, int scale_factor)
172
{
173
    int shift, mod;
174
    INT64 val;
175
176
    shift = scale_factor_modshift[scale_factor];
177
    mod = shift & 3;
178
    shift >>= 2;
179
    val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
180
    shift += n;
181 81552334 Fabrice Bellard
    /* NOTE: at this point, 1 <= shift >= 21 + 15 */
182
    return (int)((val + (1LL << (shift - 1))) >> shift);
183 239c2f4c Fabrice Bellard
}
184
185
static inline int l2_unscale_group(int steps, int mant, int scale_factor)
186
{
187
    int shift, mod, val;
188
189
    shift = scale_factor_modshift[scale_factor];
190
    mod = shift & 3;
191
    shift >>= 2;
192 81552334 Fabrice Bellard
193
    val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
194
    /* NOTE: at this point, 0 <= shift <= 21 */
195
    if (shift > 0)
196
        val = (val + (1 << (shift - 1))) >> shift;
197
    return val;
198 239c2f4c Fabrice Bellard
}
199
200
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
201
static inline int l3_unscale(int value, int exponent)
202
{
203
#if FRAC_BITS <= 15    
204
    unsigned int m;
205
#else
206
    UINT64 m;
207
#endif
208
    int e;
209
210
    e = table_4_3_exp[value];
211
    e += (exponent >> 2);
212
    e = FRAC_BITS - e;
213
#if FRAC_BITS <= 15    
214
    if (e > 31)
215
        e = 31;
216
#endif
217
    m = table_4_3_value[value];
218
#if FRAC_BITS <= 15    
219
    m = (m * scale_factor_mult3[exponent & 3]);
220
    m = (m + (1 << (e-1))) >> e;
221
    return m;
222
#else
223
    m = MUL64(m, scale_factor_mult3[exponent & 3]);
224
    m = (m + (UINT64_C(1) << (e-1))) >> e;
225
    return m;
226
#endif
227
}
228
229 f9ed4f88 Fabrice Bellard
/* all integer n^(4/3) computation code */
230
#define DEV_ORDER 13
231
232
#define POW_FRAC_BITS 24
233
#define POW_FRAC_ONE    (1 << POW_FRAC_BITS)
234
#define POW_FIX(a)   ((int)((a) * POW_FRAC_ONE))
235
#define POW_MULL(a,b) (((INT64)(a) * (INT64)(b)) >> POW_FRAC_BITS)
236
237
static int dev_4_3_coefs[DEV_ORDER];
238
239
static int pow_mult3[3] = {
240
    POW_FIX(1.0),
241
    POW_FIX(1.25992104989487316476),
242
    POW_FIX(1.58740105196819947474),
243
};
244
245
static void int_pow_init(void)
246
{
247
    int i, a;
248
249
    a = POW_FIX(1.0);
250
    for(i=0;i<DEV_ORDER;i++) {
251
        a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
252
        dev_4_3_coefs[i] = a;
253
    }
254
}
255
256
/* return the mantissa and the binary exponent */
257
static int int_pow(int i, int *exp_ptr)
258
{
259
    int e, er, eq, j;
260
    int a, a1;
261
    
262
    /* renormalize */
263
    a = i;
264
    e = POW_FRAC_BITS;
265
    while (a < (1 << (POW_FRAC_BITS - 1))) {
266
        a = a << 1;
267
        e--;
268
    }
269
    a -= (1 << POW_FRAC_BITS);
270
    a1 = 0;
271
    for(j = DEV_ORDER - 1; j >= 0; j--)
272
        a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
273
    a = (1 << POW_FRAC_BITS) + a1;
274
    /* exponent compute (exact) */
275
    e = e * 4;
276
    er = e % 3;
277
    eq = e / 3;
278
    a = POW_MULL(a, pow_mult3[er]);
279
    while (a >= 2 * POW_FRAC_ONE) {
280
        a = a >> 1;
281
        eq++;
282
    }
283
    /* convert to float */
284
    while (a < POW_FRAC_ONE) {
285
        a = a << 1;
286
        eq--;
287
    }
288 59d3e367 Fabrice Bellard
    /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
289 81552334 Fabrice Bellard
#if POW_FRAC_BITS > FRAC_BITS
290 59d3e367 Fabrice Bellard
    a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
291
    /* correct overflow */
292
    if (a >= 2 * (1 << FRAC_BITS)) {
293
        a = a >> 1;
294
        eq++;
295
    }
296
#endif
297 f9ed4f88 Fabrice Bellard
    *exp_ptr = eq;
298
    return a;
299
}
300 de6d9b64 Fabrice Bellard
301
static int decode_init(AVCodecContext * avctx)
302
{
303
    MPADecodeContext *s = avctx->priv_data;
304
    static int init;
305 239c2f4c Fabrice Bellard
    int i, j, k;
306 de6d9b64 Fabrice Bellard
307
    if(!init) {
308 239c2f4c Fabrice Bellard
        /* scale factors table for layer 1/2 */
309
        for(i=0;i<64;i++) {
310
            int shift, mod;
311
            /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
312 81552334 Fabrice Bellard
            shift = (i / 3);
313 239c2f4c Fabrice Bellard
            mod = i % 3;
314
            scale_factor_modshift[i] = mod | (shift << 2);
315
        }
316
317
        /* scale factor multiply for layer 1 */
318
        for(i=0;i<15;i++) {
319
            int n, norm;
320
            n = i + 2;
321
            norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
322 81552334 Fabrice Bellard
            scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
323
            scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
324
            scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
325 239c2f4c Fabrice Bellard
            dprintf("%d: norm=%x s=%x %x %x\n",
326
                    i, norm, 
327
                    scale_factor_mult[i][0],
328
                    scale_factor_mult[i][1],
329
                    scale_factor_mult[i][2]);
330
        }
331
        
332
        /* window */
333
        /* max = 18760, max sum over all 16 coefs : 44736 */
334
        for(i=0;i<257;i++) {
335
            int v;
336
            v = mpa_enwindow[i];
337
#if WFRAC_BITS < 16
338
            v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
339
#endif
340
            window[i] = v;
341
            if ((i & 63) != 0)
342
                v = -v;
343
            if (i != 0)
344
                window[512 - i] = v;
345
        }
346
        
347
        /* huffman decode tables */
348
        huff_code_table[0] = NULL;
349
        for(i=1;i<16;i++) {
350
            const HuffTable *h = &mpa_huff_tables[i];
351
            int xsize, n, x, y;
352
            UINT8 *code_table;
353
354
            xsize = h->xsize;
355
            n = xsize * xsize;
356
            /* XXX: fail test */
357
            init_vlc(&huff_vlc[i], 8, n, 
358
                     h->bits, 1, 1, h->codes, 2, 2);
359
            
360
            code_table = av_mallocz(n);
361
            j = 0;
362
            for(x=0;x<xsize;x++) {
363
                for(y=0;y<xsize;y++)
364
                    code_table[j++] = (x << 4) | y;
365
            }
366
            huff_code_table[i] = code_table;
367
        }
368
        for(i=0;i<2;i++) {
369
            init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16, 
370
                     mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1);
371
        }
372
373
        for(i=0;i<9;i++) {
374
            k = 0;
375
            for(j=0;j<22;j++) {
376
                band_index_long[i][j] = k;
377
                k += band_size_long[i][j];
378
            }
379
            band_index_long[i][22] = k;
380
        }
381
382
        /* compute n ^ (4/3) and store it in mantissa/exp format */
383
        table_4_3_exp = av_mallocz(TABLE_4_3_SIZE * 
384
                                   sizeof(table_4_3_exp[0]));
385
        if (!table_4_3_exp)
386
            return -1;
387
        table_4_3_value = av_mallocz(TABLE_4_3_SIZE * 
388
                                     sizeof(table_4_3_value[0]));
389
        if (!table_4_3_value) {
390 f9ed4f88 Fabrice Bellard
            av_free(table_4_3_exp);
391 239c2f4c Fabrice Bellard
            return -1;
392
        }
393
        
394 f9ed4f88 Fabrice Bellard
        int_pow_init();
395 239c2f4c Fabrice Bellard
        for(i=1;i<TABLE_4_3_SIZE;i++) {
396
            int e, m;
397 f9ed4f88 Fabrice Bellard
            m = int_pow(i, &e);
398
#if 0
399
            /* test code */
400
            {
401
                double f, fm;
402
                int e1, m1;
403
                f = pow((double)i, 4.0 / 3.0);
404
                fm = frexp(f, &e1);
405
                m1 = FIXR(2 * fm);
406
#if FRAC_BITS <= 15
407 59d3e367 Fabrice Bellard
                if ((unsigned short)m1 != m1) {
408
                    m1 = m1 >> 1;
409
                    e1++;
410
                }
411 f9ed4f88 Fabrice Bellard
#endif
412
                e1--;
413
                if (m != m1 || e != e1) {
414
                    printf("%4d: m=%x m1=%x e=%d e1=%d\n",
415
                           i, m, m1, e, e1);
416
                }
417
            }
418
#endif
419 239c2f4c Fabrice Bellard
            /* normalized to FRAC_BITS */
420
            table_4_3_value[i] = m;
421 59d3e367 Fabrice Bellard
            table_4_3_exp[i] = e;
422 239c2f4c Fabrice Bellard
        }
423
        
424
        for(i=0;i<7;i++) {
425
            float f;
426
            int v;
427
            if (i != 6) {
428
                f = tan((double)i * M_PI / 12.0);
429
                v = FIXR(f / (1.0 + f));
430
            } else {
431
                v = FIXR(1.0);
432
            }
433
            is_table[0][i] = v;
434
            is_table[1][6 - i] = v;
435
        }
436
        /* invalid values */
437
        for(i=7;i<16;i++)
438
            is_table[0][i] = is_table[1][i] = 0.0;
439
440
        for(i=0;i<16;i++) {
441
            double f;
442
            int e, k;
443
444
            for(j=0;j<2;j++) {
445
                e = -(j + 1) * ((i + 1) >> 1);
446
                f = pow(2.0, e / 4.0);
447
                k = i & 1;
448
                is_table_lsf[j][k ^ 1][i] = FIXR(f);
449
                is_table_lsf[j][k][i] = FIXR(1.0);
450
                dprintf("is_table_lsf %d %d: %x %x\n", 
451
                        i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
452
            }
453
        }
454
455
        for(i=0;i<8;i++) {
456
            float ci, cs, ca;
457
            ci = ci_table[i];
458
            cs = 1.0 / sqrt(1.0 + ci * ci);
459
            ca = cs * ci;
460
            csa_table[i][0] = FIX(cs);
461
            csa_table[i][1] = FIX(ca);
462
        }
463
464
        /* compute mdct windows */
465
        for(i=0;i<36;i++) {
466
            int v;
467
            v = FIXR(sin(M_PI * (i + 0.5) / 36.0));
468
            mdct_win[0][i] = v;
469
            mdct_win[1][i] = v;
470
            mdct_win[3][i] = v;
471
        }
472
        for(i=0;i<6;i++) {
473
            mdct_win[1][18 + i] = FIXR(1.0);
474
            mdct_win[1][24 + i] = FIXR(sin(M_PI * ((i + 6) + 0.5) / 12.0));
475
            mdct_win[1][30 + i] = FIXR(0.0);
476
477
            mdct_win[3][i] = FIXR(0.0);
478
            mdct_win[3][6 + i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
479
            mdct_win[3][12 + i] = FIXR(1.0);
480
        }
481
482
        for(i=0;i<12;i++)
483
            mdct_win[2][i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
484
        
485
        /* NOTE: we do frequency inversion adter the MDCT by changing
486
           the sign of the right window coefs */
487
        for(j=0;j<4;j++) {
488
            for(i=0;i<36;i+=2) {
489
                mdct_win[j + 4][i] = mdct_win[j][i];
490
                mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
491
            }
492
        }
493
494
#if defined(DEBUG)
495
        for(j=0;j<8;j++) {
496
            printf("win%d=\n", j);
497
            for(i=0;i<36;i++)
498
                printf("%f, ", (double)mdct_win[j][i] / FRAC_ONE);
499
            printf("\n");
500
        }
501
#endif
502 de6d9b64 Fabrice Bellard
        init = 1;
503
    }
504
505
    s->inbuf_index = 0;
506
    s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
507
    s->inbuf_ptr = s->inbuf;
508 239c2f4c Fabrice Bellard
#ifdef DEBUG
509
    s->frame_count = 0;
510
#endif
511 de6d9b64 Fabrice Bellard
    return 0;
512
}
513
514 239c2f4c Fabrice Bellard
/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */;
515
516
/* cos(i*pi/64) */
517
518
#define COS0_0  FIXR(0.50060299823519630134)
519
#define COS0_1  FIXR(0.50547095989754365998)
520
#define COS0_2  FIXR(0.51544730992262454697)
521
#define COS0_3  FIXR(0.53104259108978417447)
522
#define COS0_4  FIXR(0.55310389603444452782)
523
#define COS0_5  FIXR(0.58293496820613387367)
524
#define COS0_6  FIXR(0.62250412303566481615)
525
#define COS0_7  FIXR(0.67480834145500574602)
526
#define COS0_8  FIXR(0.74453627100229844977)
527
#define COS0_9  FIXR(0.83934964541552703873)
528
#define COS0_10 FIXR(0.97256823786196069369)
529
#define COS0_11 FIXR(1.16943993343288495515)
530
#define COS0_12 FIXR(1.48416461631416627724)
531
#define COS0_13 FIXR(2.05778100995341155085)
532
#define COS0_14 FIXR(3.40760841846871878570)
533
#define COS0_15 FIXR(10.19000812354805681150)
534
535
#define COS1_0 FIXR(0.50241928618815570551)
536
#define COS1_1 FIXR(0.52249861493968888062)
537
#define COS1_2 FIXR(0.56694403481635770368)
538
#define COS1_3 FIXR(0.64682178335999012954)
539
#define COS1_4 FIXR(0.78815462345125022473)
540
#define COS1_5 FIXR(1.06067768599034747134)
541
#define COS1_6 FIXR(1.72244709823833392782)
542
#define COS1_7 FIXR(5.10114861868916385802)
543
544
#define COS2_0 FIXR(0.50979557910415916894)
545
#define COS2_1 FIXR(0.60134488693504528054)
546
#define COS2_2 FIXR(0.89997622313641570463)
547
#define COS2_3 FIXR(2.56291544774150617881)
548
549
#define COS3_0 FIXR(0.54119610014619698439)
550
#define COS3_1 FIXR(1.30656296487637652785)
551
552
#define COS4_0 FIXR(0.70710678118654752439)
553
554
/* butterfly operator */
555
#define BF(a, b, c)\
556
{\
557
    tmp0 = tab[a] + tab[b];\
558
    tmp1 = tab[a] - tab[b];\
559
    tab[a] = tmp0;\
560
    tab[b] = MULL(tmp1, c);\
561
}
562
563
#define BF1(a, b, c, d)\
564
{\
565
    BF(a, b, COS4_0);\
566
    BF(c, d, -COS4_0);\
567
    tab[c] += tab[d];\
568
}
569
570
#define BF2(a, b, c, d)\
571
{\
572
    BF(a, b, COS4_0);\
573
    BF(c, d, -COS4_0);\
574
    tab[c] += tab[d];\
575
    tab[a] += tab[c];\
576
    tab[c] += tab[b];\
577
    tab[b] += tab[d];\
578
}
579
580
#define ADD(a, b) tab[a] += tab[b]
581
582
/* DCT32 without 1/sqrt(2) coef zero scaling. */
583
static void dct32(INT32 *out, INT32 *tab)
584
{
585
    int tmp0, tmp1;
586
587
    /* pass 1 */
588
    BF(0, 31, COS0_0);
589
    BF(1, 30, COS0_1);
590
    BF(2, 29, COS0_2);
591
    BF(3, 28, COS0_3);
592
    BF(4, 27, COS0_4);
593
    BF(5, 26, COS0_5);
594
    BF(6, 25, COS0_6);
595
    BF(7, 24, COS0_7);
596
    BF(8, 23, COS0_8);
597
    BF(9, 22, COS0_9);
598
    BF(10, 21, COS0_10);
599
    BF(11, 20, COS0_11);
600
    BF(12, 19, COS0_12);
601
    BF(13, 18, COS0_13);
602
    BF(14, 17, COS0_14);
603
    BF(15, 16, COS0_15);
604
605
    /* pass 2 */
606
    BF(0, 15, COS1_0);
607
    BF(1, 14, COS1_1);
608
    BF(2, 13, COS1_2);
609
    BF(3, 12, COS1_3);
610
    BF(4, 11, COS1_4);
611
    BF(5, 10, COS1_5);
612
    BF(6,  9, COS1_6);
613
    BF(7,  8, COS1_7);
614
    
615
    BF(16, 31, -COS1_0);
616
    BF(17, 30, -COS1_1);
617
    BF(18, 29, -COS1_2);
618
    BF(19, 28, -COS1_3);
619
    BF(20, 27, -COS1_4);
620
    BF(21, 26, -COS1_5);
621
    BF(22, 25, -COS1_6);
622
    BF(23, 24, -COS1_7);
623
    
624
    /* pass 3 */
625
    BF(0, 7, COS2_0);
626
    BF(1, 6, COS2_1);
627
    BF(2, 5, COS2_2);
628
    BF(3, 4, COS2_3);
629
    
630
    BF(8, 15, -COS2_0);
631
    BF(9, 14, -COS2_1);
632
    BF(10, 13, -COS2_2);
633
    BF(11, 12, -COS2_3);
634
    
635
    BF(16, 23, COS2_0);
636
    BF(17, 22, COS2_1);
637
    BF(18, 21, COS2_2);
638
    BF(19, 20, COS2_3);
639
    
640
    BF(24, 31, -COS2_0);
641
    BF(25, 30, -COS2_1);
642
    BF(26, 29, -COS2_2);
643
    BF(27, 28, -COS2_3);
644
645
    /* pass 4 */
646
    BF(0, 3, COS3_0);
647
    BF(1, 2, COS3_1);
648
    
649
    BF(4, 7, -COS3_0);
650
    BF(5, 6, -COS3_1);
651
    
652
    BF(8, 11, COS3_0);
653
    BF(9, 10, COS3_1);
654
    
655
    BF(12, 15, -COS3_0);
656
    BF(13, 14, -COS3_1);
657
    
658
    BF(16, 19, COS3_0);
659
    BF(17, 18, COS3_1);
660
    
661
    BF(20, 23, -COS3_0);
662
    BF(21, 22, -COS3_1);
663
    
664
    BF(24, 27, COS3_0);
665
    BF(25, 26, COS3_1);
666
    
667
    BF(28, 31, -COS3_0);
668
    BF(29, 30, -COS3_1);
669
    
670
    /* pass 5 */
671
    BF1(0, 1, 2, 3);
672
    BF2(4, 5, 6, 7);
673
    BF1(8, 9, 10, 11);
674
    BF2(12, 13, 14, 15);
675
    BF1(16, 17, 18, 19);
676
    BF2(20, 21, 22, 23);
677
    BF1(24, 25, 26, 27);
678
    BF2(28, 29, 30, 31);
679
    
680
    /* pass 6 */
681
    
682
    ADD( 8, 12);
683
    ADD(12, 10);
684
    ADD(10, 14);
685
    ADD(14,  9);
686
    ADD( 9, 13);
687
    ADD(13, 11);
688
    ADD(11, 15);
689
690
    out[ 0] = tab[0];
691
    out[16] = tab[1];
692
    out[ 8] = tab[2];
693
    out[24] = tab[3];
694
    out[ 4] = tab[4];
695
    out[20] = tab[5];
696
    out[12] = tab[6];
697
    out[28] = tab[7];
698
    out[ 2] = tab[8];
699
    out[18] = tab[9];
700
    out[10] = tab[10];
701
    out[26] = tab[11];
702
    out[ 6] = tab[12];
703
    out[22] = tab[13];
704
    out[14] = tab[14];
705
    out[30] = tab[15];
706
    
707
    ADD(24, 28);
708
    ADD(28, 26);
709
    ADD(26, 30);
710
    ADD(30, 25);
711
    ADD(25, 29);
712
    ADD(29, 27);
713
    ADD(27, 31);
714
715
    out[ 1] = tab[16] + tab[24];
716
    out[17] = tab[17] + tab[25];
717
    out[ 9] = tab[18] + tab[26];
718
    out[25] = tab[19] + tab[27];
719
    out[ 5] = tab[20] + tab[28];
720
    out[21] = tab[21] + tab[29];
721
    out[13] = tab[22] + tab[30];
722
    out[29] = tab[23] + tab[31];
723
    out[ 3] = tab[24] + tab[20];
724
    out[19] = tab[25] + tab[21];
725
    out[11] = tab[26] + tab[22];
726
    out[27] = tab[27] + tab[23];
727
    out[ 7] = tab[28] + tab[18];
728
    out[23] = tab[29] + tab[19];
729
    out[15] = tab[30] + tab[17];
730
    out[31] = tab[31];
731
}
732
733
#define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
734
735
#if FRAC_BITS <= 15
736
737
#define OUT_SAMPLE(sum)\
738
{\
739
    int sum1;\
740
    sum1 = (sum + (1 << (OUT_SHIFT - 1))) >> OUT_SHIFT;\
741
    if (sum1 < -32768)\
742
        sum1 = -32768;\
743
    else if (sum1 > 32767)\
744
        sum1 = 32767;\
745
    *samples = sum1;\
746
    samples += incr;\
747
}
748
749
#define SUM8(off, op)                           \
750
{                                               \
751
    sum op w[0 * 64 + off] * p[0 * 64];\
752
    sum op w[1 * 64 + off] * p[1 * 64];\
753
    sum op w[2 * 64 + off] * p[2 * 64];\
754
    sum op w[3 * 64 + off] * p[3 * 64];\
755
    sum op w[4 * 64 + off] * p[4 * 64];\
756
    sum op w[5 * 64 + off] * p[5 * 64];\
757
    sum op w[6 * 64 + off] * p[6 * 64];\
758
    sum op w[7 * 64 + off] * p[7 * 64];\
759
}
760
761
#else
762
763
#define OUT_SAMPLE(sum)\
764
{\
765
    int sum1;\
766
    sum1 = (int)((sum + (INT64_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT);\
767
    if (sum1 < -32768)\
768
        sum1 = -32768;\
769
    else if (sum1 > 32767)\
770
        sum1 = 32767;\
771
    *samples = sum1;\
772
    samples += incr;\
773
}
774
775
#define SUM8(off, op)                           \
776
{                                               \
777
    sum op MUL64(w[0 * 64 + off], p[0 * 64]);\
778
    sum op MUL64(w[1 * 64 + off], p[1 * 64]);\
779
    sum op MUL64(w[2 * 64 + off], p[2 * 64]);\
780
    sum op MUL64(w[3 * 64 + off], p[3 * 64]);\
781
    sum op MUL64(w[4 * 64 + off], p[4 * 64]);\
782
    sum op MUL64(w[5 * 64 + off], p[5 * 64]);\
783
    sum op MUL64(w[6 * 64 + off], p[6 * 64]);\
784
    sum op MUL64(w[7 * 64 + off], p[7 * 64]);\
785
}
786
787
#endif
788
789
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
790
   32 samples. */
791
/* XXX: optimize by avoiding ring buffer usage */
792
static void synth_filter(MPADecodeContext *s1,
793
                         int ch, INT16 *samples, int incr, 
794
                         INT32 sb_samples[SBLIMIT])
795
{
796
    INT32 tmp[32];
797
    register MPA_INT *synth_buf, *p;
798
    register MPA_INT *w;
799
    int j, offset, v;
800
#if FRAC_BITS <= 15
801
    int sum;
802
#else
803
    INT64 sum;
804
#endif
805
806
    dct32(tmp, sb_samples);
807
    
808
    offset = s1->synth_buf_offset[ch];
809
    synth_buf = s1->synth_buf[ch] + offset;
810
811
    for(j=0;j<32;j++) {
812
        v = tmp[j];
813
#if FRAC_BITS <= 15
814 81552334 Fabrice Bellard
        /* NOTE: can cause a loss in precision if very high amplitude
815
           sound */
816 239c2f4c Fabrice Bellard
        if (v > 32767)
817
            v = 32767;
818
        else if (v < -32768)
819
            v = -32768;
820
#endif
821
        synth_buf[j] = v;
822
    }
823
    /* copy to avoid wrap */
824
    memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
825
826
    w = window;
827
    for(j=0;j<16;j++) {
828
        sum = 0;
829
        p = synth_buf + 16 + j;    /* 0-15  */
830
        SUM8(0, +=);
831
        p = synth_buf + 48 - j;    /* 32-47 */
832
        SUM8(32, -=);
833
        OUT_SAMPLE(sum);
834
        w++;
835
    }
836
    
837
    p = synth_buf + 32; /* 48 */
838
    sum = 0;
839
    SUM8(32, -=);
840
    OUT_SAMPLE(sum);
841
    w++;
842
843
    for(j=17;j<32;j++) {
844
        sum = 0;
845
        p = synth_buf + 48 - j; /* 17-31 */
846
        SUM8(0, -=);
847
        p = synth_buf + 16 + j; /* 49-63 */
848
        SUM8(32, -=);
849
        OUT_SAMPLE(sum);
850
        w++;
851
    }
852
    offset = (offset - 32) & 511;
853
    s1->synth_buf_offset[ch] = offset;
854
}
855
856
/* cos(pi*i/24) */
857
#define C1  FIXR(0.99144486137381041114)
858
#define C3  FIXR(0.92387953251128675612)
859
#define C5  FIXR(0.79335334029123516458)
860
#define C7  FIXR(0.60876142900872063941)
861
#define C9  FIXR(0.38268343236508977173)
862
#define C11 FIXR(0.13052619222005159154)
863
864
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
865
   cases. */
866
static void imdct12(int *out, int *in)
867
{
868
    int tmp;
869
    INT64 in1_3, in1_9, in4_3, in4_9;
870
871
    in1_3 = MUL64(in[1], C3);
872
    in1_9 = MUL64(in[1], C9);
873
    in4_3 = MUL64(in[4], C3);
874
    in4_9 = MUL64(in[4], C9);
875
    
876
    tmp = FRAC_RND(MUL64(in[0], C7) - in1_3 - MUL64(in[2], C11) + 
877
                   MUL64(in[3], C1) - in4_9 - MUL64(in[5], C5));
878
    out[0] = tmp;
879
    out[5] = -tmp;
880
    tmp = FRAC_RND(MUL64(in[0] - in[3], C9) - in1_3 + 
881
                   MUL64(in[2] + in[5], C3) - in4_9);
882
    out[1] = tmp;
883
    out[4] = -tmp;
884
    tmp = FRAC_RND(MUL64(in[0], C11) - in1_9 + MUL64(in[2], C7) -
885
                   MUL64(in[3], C5) + in4_3 - MUL64(in[5], C1));
886
    out[2] = tmp;
887
    out[3] = -tmp;
888
    tmp = FRAC_RND(MUL64(-in[0], C5) + in1_9 + MUL64(in[2], C1) + 
889
                   MUL64(in[3], C11) - in4_3 - MUL64(in[5], C7));
890
    out[6] = tmp;
891
    out[11] = tmp;
892
    tmp = FRAC_RND(MUL64(-in[0] + in[3], C3) - in1_9 + 
893
                   MUL64(in[2] + in[5], C9) + in4_3);
894
    out[7] = tmp;
895
    out[10] = tmp;
896
    tmp = FRAC_RND(-MUL64(in[0], C1) - in1_3 - MUL64(in[2], C5) -
897
                   MUL64(in[3], C7) - in4_9 - MUL64(in[5], C11));
898
    out[8] = tmp;
899
    out[9] = tmp;
900
}
901
902
#undef C1
903
#undef C3
904
#undef C5
905
#undef C7
906
#undef C9
907
#undef C11
908
909
/* cos(pi*i/18) */
910
#define C1 FIXR(0.98480775301220805936)
911
#define C2 FIXR(0.93969262078590838405)
912
#define C3 FIXR(0.86602540378443864676)
913
#define C4 FIXR(0.76604444311897803520)
914
#define C5 FIXR(0.64278760968653932632)
915
#define C6 FIXR(0.5)
916
#define C7 FIXR(0.34202014332566873304)
917
#define C8 FIXR(0.17364817766693034885)
918
919
/* 0.5 / cos(pi*(2*i+1)/36) */
920
static const int icos36[9] = {
921
    FIXR(0.50190991877167369479),
922
    FIXR(0.51763809020504152469),
923
    FIXR(0.55168895948124587824),
924
    FIXR(0.61038729438072803416),
925
    FIXR(0.70710678118654752439),
926
    FIXR(0.87172339781054900991),
927
    FIXR(1.18310079157624925896),
928
    FIXR(1.93185165257813657349),
929
    FIXR(5.73685662283492756461),
930
};
931
932
static const int icos72[18] = {
933
    /* 0.5 / cos(pi*(2*i+19)/72) */
934
    FIXR(0.74009361646113053152),
935
    FIXR(0.82133981585229078570),
936
    FIXR(0.93057949835178895673),
937
    FIXR(1.08284028510010010928),
938
    FIXR(1.30656296487637652785),
939
    FIXR(1.66275476171152078719),
940
    FIXR(2.31011315767264929558),
941
    FIXR(3.83064878777019433457),
942
    FIXR(11.46279281302667383546),
943
944
    /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
945
    FIXR(-0.67817085245462840086),
946
    FIXR(-0.63023620700513223342),
947
    FIXR(-0.59284452371708034528),
948
    FIXR(-0.56369097343317117734),
949
    FIXR(-0.54119610014619698439),
950
    FIXR(-0.52426456257040533932),
951
    FIXR(-0.51213975715725461845),
952
    FIXR(-0.50431448029007636036),
953
    FIXR(-0.50047634258165998492),
954
};
955
956
/* using Lee like decomposition followed by hand coded 9 points DCT */
957
static void imdct36(int *out, int *in)
958
{
959
    int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
960
    int tmp[18], *tmp1, *in1;
961
    INT64 in3_3, in6_6;
962
963
    for(i=17;i>=1;i--)
964
        in[i] += in[i-1];
965
    for(i=17;i>=3;i-=2)
966
        in[i] += in[i-2];
967
968
    for(j=0;j<2;j++) {
969
        tmp1 = tmp + j;
970
        in1 = in + j;
971
972
        in3_3 = MUL64(in1[2*3], C3);
973
        in6_6 = MUL64(in1[2*6], C6);
974
975
        tmp1[0] = FRAC_RND(MUL64(in1[2*1], C1) + in3_3 + 
976
                           MUL64(in1[2*5], C5) + MUL64(in1[2*7], C7));
977
        tmp1[2] = in1[2*0] + FRAC_RND(MUL64(in1[2*2], C2) + 
978
                                      MUL64(in1[2*4], C4) + in6_6 + 
979
                                      MUL64(in1[2*8], C8));
980
        tmp1[4] = FRAC_RND(MUL64(in1[2*1] - in1[2*5] - in1[2*7], C3));
981
        tmp1[6] = FRAC_RND(MUL64(in1[2*2] - in1[2*4] - in1[2*8], C6)) - 
982
            in1[2*6] + in1[2*0];
983
        tmp1[8] = FRAC_RND(MUL64(in1[2*1], C5) - in3_3 - 
984
                           MUL64(in1[2*5], C7) + MUL64(in1[2*7], C1));
985
        tmp1[10] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C8) - 
986
                                       MUL64(in1[2*4], C2) + in6_6 + 
987
                                       MUL64(in1[2*8], C4));
988
        tmp1[12] = FRAC_RND(MUL64(in1[2*1], C7) - in3_3 + 
989
                            MUL64(in1[2*5], C1) - 
990
                            MUL64(in1[2*7], C5));
991
        tmp1[14] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C4) + 
992
                                       MUL64(in1[2*4], C8) + in6_6 - 
993
                                       MUL64(in1[2*8], C2));
994
        tmp1[16] = in1[2*0] - in1[2*2] + in1[2*4] - in1[2*6] + in1[2*8];
995
    }
996
997
    i = 0;
998
    for(j=0;j<4;j++) {
999
        t0 = tmp[i];
1000
        t1 = tmp[i + 2];
1001
        s0 = t1 + t0;
1002
        s2 = t1 - t0;
1003
1004
        t2 = tmp[i + 1];
1005
        t3 = tmp[i + 3];
1006
        s1 = MULL(t3 + t2, icos36[j]);
1007
        s3 = MULL(t3 - t2, icos36[8 - j]);
1008
        
1009
        t0 = MULL(s0 + s1, icos72[9 + 8 - j]);
1010
        t1 = MULL(s0 - s1, icos72[8 - j]);
1011
        out[18 + 9 + j] = t0;
1012
        out[18 + 8 - j] = t0;
1013
        out[9 + j] = -t1;
1014
        out[8 - j] = t1;
1015
        
1016
        t0 = MULL(s2 + s3, icos72[9+j]);
1017
        t1 = MULL(s2 - s3, icos72[j]);
1018
        out[18 + 9 + (8 - j)] = t0;
1019
        out[18 + j] = t0;
1020
        out[9 + (8 - j)] = -t1;
1021
        out[j] = t1;
1022
        i += 4;
1023
    }
1024
1025
    s0 = tmp[16];
1026
    s1 = MULL(tmp[17], icos36[4]);
1027
    t0 = MULL(s0 + s1, icos72[9 + 4]);
1028
    t1 = MULL(s0 - s1, icos72[4]);
1029
    out[18 + 9 + 4] = t0;
1030
    out[18 + 8 - 4] = t0;
1031
    out[9 + 4] = -t1;
1032
    out[8 - 4] = t1;
1033
}
1034
1035 de6d9b64 Fabrice Bellard
/* fast header check for resync */
1036
static int check_header(UINT32 header)
1037
{
1038
    /* header */
1039
    if ((header & 0xffe00000) != 0xffe00000)
1040
        return -1;
1041
    /* layer check */
1042
    if (((header >> 17) & 3) == 0)
1043
        return -1;
1044 239c2f4c Fabrice Bellard
    /* bit rate */
1045
    if (((header >> 12) & 0xf) == 0xf)
1046 de6d9b64 Fabrice Bellard
        return -1;
1047
    /* frequency */
1048
    if (((header >> 10) & 3) == 3)
1049
        return -1;
1050
    return 0;
1051
}
1052
1053 239c2f4c Fabrice Bellard
/* header + layer + bitrate + freq + lsf/mpeg25 */
1054
#define SAME_HEADER_MASK \
1055
   (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1056
1057 de6d9b64 Fabrice Bellard
/* header decoding. MUST check the header before because no
1058 239c2f4c Fabrice Bellard
   consistency check is done there. Return 1 if free format found and
1059
   that the frame size must be computed externally */
1060
static int decode_header(MPADecodeContext *s, UINT32 header)
1061 de6d9b64 Fabrice Bellard
{
1062 239c2f4c Fabrice Bellard
    int sample_rate, frame_size, mpeg25, padding;
1063
    int sample_rate_index, bitrate_index;
1064 de6d9b64 Fabrice Bellard
    if (header & (1<<20)) {
1065 239c2f4c Fabrice Bellard
        s->lsf = (header & (1<<19)) ? 0 : 1;
1066
        mpeg25 = 0;
1067 de6d9b64 Fabrice Bellard
    } else {
1068 239c2f4c Fabrice Bellard
        s->lsf = 1;
1069
        mpeg25 = 1;
1070 de6d9b64 Fabrice Bellard
    }
1071
    
1072
    s->layer = 4 - ((header >> 17) & 3);
1073
    /* extract frequency */
1074 239c2f4c Fabrice Bellard
    sample_rate_index = (header >> 10) & 3;
1075
    sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1076
    sample_rate_index += 3 * (s->lsf + mpeg25);
1077
    s->sample_rate_index = sample_rate_index;
1078
    s->error_protection = ((header >> 16) & 1) ^ 1;
1079 81552334 Fabrice Bellard
    s->sample_rate = sample_rate;
1080 de6d9b64 Fabrice Bellard
1081 239c2f4c Fabrice Bellard
    bitrate_index = (header >> 12) & 0xf;
1082
    padding = (header >> 9) & 1;
1083
    //extension = (header >> 8) & 1;
1084
    s->mode = (header >> 6) & 3;
1085
    s->mode_ext = (header >> 4) & 3;
1086
    //copyright = (header >> 3) & 1;
1087
    //original = (header >> 2) & 1;
1088
    //emphasis = header & 3;
1089 de6d9b64 Fabrice Bellard
1090 239c2f4c Fabrice Bellard
    if (s->mode == MPA_MONO)
1091
        s->nb_channels = 1;
1092
    else
1093
        s->nb_channels = 2;
1094 de6d9b64 Fabrice Bellard
    
1095 239c2f4c Fabrice Bellard
    if (bitrate_index != 0) {
1096
        frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1097
        s->bit_rate = frame_size * 1000;
1098
        switch(s->layer) {
1099
        case 1:
1100
            frame_size = (frame_size * 12000) / sample_rate;
1101
            frame_size = (frame_size + padding) * 4;
1102
            break;
1103
        case 2:
1104
            frame_size = (frame_size * 144000) / sample_rate;
1105
            frame_size += padding;
1106
            break;
1107
        default:
1108
        case 3:
1109
            frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1110
            frame_size += padding;
1111
            break;
1112
        }
1113
        s->frame_size = frame_size;
1114
    } else {
1115
        /* if no frame size computed, signal it */
1116
        if (!s->free_format_frame_size)
1117
            return 1;
1118
        /* free format: compute bitrate and real frame size from the
1119
           frame size we extracted by reading the bitstream */
1120
        s->frame_size = s->free_format_frame_size;
1121
        switch(s->layer) {
1122
        case 1:
1123
            s->frame_size += padding  * 4;
1124
            s->bit_rate = (s->frame_size * sample_rate) / 48000;
1125
            break;
1126
        case 2:
1127
            s->frame_size += padding;
1128
            s->bit_rate = (s->frame_size * sample_rate) / 144000;
1129
            break;
1130
        default:
1131
        case 3:
1132
            s->frame_size += padding;
1133
            s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1134
            break;
1135
        }
1136 de6d9b64 Fabrice Bellard
    }
1137 239c2f4c Fabrice Bellard
    
1138 fad9f495 Fabrice Bellard
#if defined(DEBUG)
1139 239c2f4c Fabrice Bellard
    printf("layer%d, %d Hz, %d kbits/s, ",
1140
           s->layer, s->sample_rate, s->bit_rate);
1141
    if (s->nb_channels == 2) {
1142
        if (s->layer == 3) {
1143
            if (s->mode_ext & MODE_EXT_MS_STEREO)
1144
                printf("ms-");
1145
            if (s->mode_ext & MODE_EXT_I_STEREO)
1146
                printf("i-");
1147
        }
1148
        printf("stereo");
1149
    } else {
1150
        printf("mono");
1151
    }
1152
    printf("\n");
1153 de6d9b64 Fabrice Bellard
#endif
1154 239c2f4c Fabrice Bellard
    return 0;
1155 de6d9b64 Fabrice Bellard
}
1156
1157 239c2f4c Fabrice Bellard
/* return the number of decoded frames */
1158
static int mp_decode_layer1(MPADecodeContext *s)
1159 de6d9b64 Fabrice Bellard
{
1160 239c2f4c Fabrice Bellard
    int bound, i, v, n, ch, j, mant;
1161
    UINT8 allocation[MPA_MAX_CHANNELS][SBLIMIT];
1162
    UINT8 scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1163
1164
    if (s->mode == MPA_JSTEREO) 
1165
        bound = (s->mode_ext + 1) * 4;
1166
    else
1167
        bound = SBLIMIT;
1168
1169
    /* allocation bits */
1170
    for(i=0;i<bound;i++) {
1171
        for(ch=0;ch<s->nb_channels;ch++) {
1172
            allocation[ch][i] = get_bits(&s->gb, 4);
1173
        }
1174
    }
1175
    for(i=bound;i<SBLIMIT;i++) {
1176
        allocation[0][i] = get_bits(&s->gb, 4);
1177
    }
1178
1179
    /* scale factors */
1180
    for(i=0;i<bound;i++) {
1181
        for(ch=0;ch<s->nb_channels;ch++) {
1182
            if (allocation[ch][i])
1183
                scale_factors[ch][i] = get_bits(&s->gb, 6);
1184
        }
1185
    }
1186
    for(i=bound;i<SBLIMIT;i++) {
1187
        if (allocation[0][i]) {
1188
            scale_factors[0][i] = get_bits(&s->gb, 6);
1189
            scale_factors[1][i] = get_bits(&s->gb, 6);
1190
        }
1191
    }
1192 de6d9b64 Fabrice Bellard
    
1193 239c2f4c Fabrice Bellard
    /* compute samples */
1194
    for(j=0;j<12;j++) {
1195
        for(i=0;i<bound;i++) {
1196
            for(ch=0;ch<s->nb_channels;ch++) {
1197
                n = allocation[ch][i];
1198
                if (n) {
1199
                    mant = get_bits(&s->gb, n + 1);
1200
                    v = l1_unscale(n, mant, scale_factors[ch][i]);
1201
                } else {
1202
                    v = 0;
1203
                }
1204
                s->sb_samples[ch][j][i] = v;
1205
            }
1206
        }
1207
        for(i=bound;i<SBLIMIT;i++) {
1208
            n = allocation[0][i];
1209
            if (n) {
1210
                mant = get_bits(&s->gb, n + 1);
1211
                v = l1_unscale(n, mant, scale_factors[0][i]);
1212
                s->sb_samples[0][j][i] = v;
1213
                v = l1_unscale(n, mant, scale_factors[1][i]);
1214
                s->sb_samples[1][j][i] = v;
1215
            } else {
1216
                s->sb_samples[0][j][i] = 0;
1217
                s->sb_samples[1][j][i] = 0;
1218
            }
1219
        }
1220
    }
1221
    return 12;
1222
}
1223
1224
/* bitrate is in kb/s */
1225
int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1226
{
1227
    int ch_bitrate, table;
1228 de6d9b64 Fabrice Bellard
    
1229 239c2f4c Fabrice Bellard
    ch_bitrate = bitrate / nb_channels;
1230
    if (!lsf) {
1231
        if ((freq == 48000 && ch_bitrate >= 56) ||
1232
            (ch_bitrate >= 56 && ch_bitrate <= 80)) 
1233
            table = 0;
1234
        else if (freq != 48000 && ch_bitrate >= 96) 
1235
            table = 1;
1236
        else if (freq != 32000 && ch_bitrate <= 48) 
1237
            table = 2;
1238
        else 
1239
            table = 3;
1240
    } else {
1241
        table = 4;
1242
    }
1243
    return table;
1244
}
1245 de6d9b64 Fabrice Bellard
1246 239c2f4c Fabrice Bellard
static int mp_decode_layer2(MPADecodeContext *s)
1247
{
1248
    int sblimit; /* number of used subbands */
1249
    const unsigned char *alloc_table;
1250
    int table, bit_alloc_bits, i, j, ch, bound, v;
1251
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1252
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1253
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1254
    int scale, qindex, bits, steps, k, l, m, b;
1255 de6d9b64 Fabrice Bellard
1256 239c2f4c Fabrice Bellard
    /* select decoding table */
1257
    table = l2_select_table(s->bit_rate / 1000, s->nb_channels, 
1258
                            s->sample_rate, s->lsf);
1259
    sblimit = sblimit_table[table];
1260
    alloc_table = alloc_tables[table];
1261
1262
    if (s->mode == MPA_JSTEREO) 
1263
        bound = (s->mode_ext + 1) * 4;
1264
    else
1265
        bound = sblimit;
1266
1267
    dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1268
    /* parse bit allocation */
1269
    j = 0;
1270
    for(i=0;i<bound;i++) {
1271
        bit_alloc_bits = alloc_table[j];
1272
        for(ch=0;ch<s->nb_channels;ch++) {
1273
            bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1274
        }
1275
        j += 1 << bit_alloc_bits;
1276
    }
1277
    for(i=bound;i<sblimit;i++) {
1278
        bit_alloc_bits = alloc_table[j];
1279
        v = get_bits(&s->gb, bit_alloc_bits);
1280
        bit_alloc[0][i] = v;
1281
        bit_alloc[1][i] = v;
1282
        j += 1 << bit_alloc_bits;
1283 de6d9b64 Fabrice Bellard
    }
1284 239c2f4c Fabrice Bellard
1285
#ifdef DEBUG
1286
    {
1287
        for(ch=0;ch<s->nb_channels;ch++) {
1288
            for(i=0;i<sblimit;i++)
1289
                printf(" %d", bit_alloc[ch][i]);
1290
            printf("\n");
1291
        }
1292
    }
1293
#endif
1294
1295
    /* scale codes */
1296
    for(i=0;i<sblimit;i++) {
1297
        for(ch=0;ch<s->nb_channels;ch++) {
1298
            if (bit_alloc[ch][i]) 
1299
                scale_code[ch][i] = get_bits(&s->gb, 2);
1300
        }
1301
    }
1302
    
1303
    /* scale factors */
1304
    for(i=0;i<sblimit;i++) {
1305
        for(ch=0;ch<s->nb_channels;ch++) {
1306
            if (bit_alloc[ch][i]) {
1307
                sf = scale_factors[ch][i];
1308
                switch(scale_code[ch][i]) {
1309
                default:
1310
                case 0:
1311
                    sf[0] = get_bits(&s->gb, 6);
1312
                    sf[1] = get_bits(&s->gb, 6);
1313
                    sf[2] = get_bits(&s->gb, 6);
1314
                    break;
1315
                case 2:
1316
                    sf[0] = get_bits(&s->gb, 6);
1317
                    sf[1] = sf[0];
1318
                    sf[2] = sf[0];
1319
                    break;
1320
                case 1:
1321
                    sf[0] = get_bits(&s->gb, 6);
1322
                    sf[2] = get_bits(&s->gb, 6);
1323
                    sf[1] = sf[0];
1324
                    break;
1325
                case 3:
1326
                    sf[0] = get_bits(&s->gb, 6);
1327
                    sf[2] = get_bits(&s->gb, 6);
1328
                    sf[1] = sf[2];
1329
                    break;
1330
                }
1331
            }
1332
        }
1333
    }
1334
1335
#ifdef DEBUG
1336
    for(ch=0;ch<s->nb_channels;ch++) {
1337
        for(i=0;i<sblimit;i++) {
1338
            if (bit_alloc[ch][i]) {
1339
                sf = scale_factors[ch][i];
1340
                printf(" %d %d %d", sf[0], sf[1], sf[2]);
1341
            } else {
1342
                printf(" -");
1343
            }
1344
        }
1345
        printf("\n");
1346
    }
1347
#endif
1348
1349
    /* samples */
1350
    for(k=0;k<3;k++) {
1351
        for(l=0;l<12;l+=3) {
1352
            j = 0;
1353
            for(i=0;i<bound;i++) {
1354
                bit_alloc_bits = alloc_table[j];
1355
                for(ch=0;ch<s->nb_channels;ch++) {
1356
                    b = bit_alloc[ch][i];
1357
                    if (b) {
1358
                        scale = scale_factors[ch][i][k];
1359
                        qindex = alloc_table[j+b];
1360
                        bits = quant_bits[qindex];
1361
                        if (bits < 0) {
1362
                            /* 3 values at the same time */
1363
                            v = get_bits(&s->gb, -bits);
1364
                            steps = quant_steps[qindex];
1365
                            s->sb_samples[ch][k * 12 + l + 0][i] = 
1366
                                l2_unscale_group(steps, v % steps, scale);
1367
                            v = v / steps;
1368
                            s->sb_samples[ch][k * 12 + l + 1][i] = 
1369
                                l2_unscale_group(steps, v % steps, scale);
1370
                            v = v / steps;
1371
                            s->sb_samples[ch][k * 12 + l + 2][i] = 
1372
                                l2_unscale_group(steps, v, scale);
1373
                        } else {
1374
                            for(m=0;m<3;m++) {
1375
                                v = get_bits(&s->gb, bits);
1376
                                v = l1_unscale(bits - 1, v, scale);
1377
                                s->sb_samples[ch][k * 12 + l + m][i] = v;
1378
                            }
1379
                        }
1380
                    } else {
1381
                        s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1382
                        s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1383
                        s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1384
                    }
1385
                }
1386
                /* next subband in alloc table */
1387
                j += 1 << bit_alloc_bits; 
1388
            }
1389
            /* XXX: find a way to avoid this duplication of code */
1390
            for(i=bound;i<sblimit;i++) {
1391
                bit_alloc_bits = alloc_table[j];
1392
                b = bit_alloc[0][i];
1393
                if (b) {
1394
                    int mant, scale0, scale1;
1395
                    scale0 = scale_factors[0][i][k];
1396
                    scale1 = scale_factors[1][i][k];
1397
                    qindex = alloc_table[j+b];
1398
                    bits = quant_bits[qindex];
1399
                    if (bits < 0) {
1400
                        /* 3 values at the same time */
1401
                        v = get_bits(&s->gb, -bits);
1402
                        steps = quant_steps[qindex];
1403
                        mant = v % steps;
1404
                        v = v / steps;
1405
                        s->sb_samples[0][k * 12 + l + 0][i] = 
1406
                            l2_unscale_group(steps, mant, scale0);
1407
                        s->sb_samples[1][k * 12 + l + 0][i] = 
1408
                            l2_unscale_group(steps, mant, scale1);
1409
                        mant = v % steps;
1410
                        v = v / steps;
1411
                        s->sb_samples[0][k * 12 + l + 1][i] = 
1412
                            l2_unscale_group(steps, mant, scale0);
1413
                        s->sb_samples[1][k * 12 + l + 1][i] = 
1414
                            l2_unscale_group(steps, mant, scale1);
1415
                        s->sb_samples[0][k * 12 + l + 2][i] = 
1416
                            l2_unscale_group(steps, v, scale0);
1417
                        s->sb_samples[1][k * 12 + l + 2][i] = 
1418
                            l2_unscale_group(steps, v, scale1);
1419
                    } else {
1420
                        for(m=0;m<3;m++) {
1421
                            mant = get_bits(&s->gb, bits);
1422
                            s->sb_samples[0][k * 12 + l + m][i] = 
1423
                                l1_unscale(bits - 1, mant, scale0);
1424
                            s->sb_samples[1][k * 12 + l + m][i] = 
1425
                                l1_unscale(bits - 1, mant, scale1);
1426
                        }
1427
                    }
1428
                } else {
1429
                    s->sb_samples[0][k * 12 + l + 0][i] = 0;
1430
                    s->sb_samples[0][k * 12 + l + 1][i] = 0;
1431
                    s->sb_samples[0][k * 12 + l + 2][i] = 0;
1432
                    s->sb_samples[1][k * 12 + l + 0][i] = 0;
1433
                    s->sb_samples[1][k * 12 + l + 1][i] = 0;
1434
                    s->sb_samples[1][k * 12 + l + 2][i] = 0;
1435
                }
1436
                /* next subband in alloc table */
1437
                j += 1 << bit_alloc_bits; 
1438
            }
1439
            /* fill remaining samples to zero */
1440
            for(i=sblimit;i<SBLIMIT;i++) {
1441
                for(ch=0;ch<s->nb_channels;ch++) {
1442
                    s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1443
                    s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1444
                    s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1445
                }
1446
            }
1447
        }
1448
    }
1449
    return 3 * 12;
1450 de6d9b64 Fabrice Bellard
}
1451
1452
/*
1453 239c2f4c Fabrice Bellard
 * Seek back in the stream for backstep bytes (at most 511 bytes)
1454 de6d9b64 Fabrice Bellard
 */
1455 239c2f4c Fabrice Bellard
static void seek_to_maindata(MPADecodeContext *s, long backstep)
1456 de6d9b64 Fabrice Bellard
{
1457
    UINT8 *ptr;
1458
1459
    /* compute current position in stream */
1460 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
1461
    ptr = s->gb.buffer + (s->gb.index>>3);
1462
#else
1463 239c2f4c Fabrice Bellard
    ptr = s->gb.buf_ptr - (s->gb.bit_cnt >> 3);
1464 27a3e2c5 Michael Niedermayer
#endif    
1465 de6d9b64 Fabrice Bellard
    /* copy old data before current one */
1466
    ptr -= backstep;
1467 239c2f4c Fabrice Bellard
    memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] + 
1468
           BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1469 de6d9b64 Fabrice Bellard
    /* init get bits again */
1470 239c2f4c Fabrice Bellard
    init_get_bits(&s->gb, ptr, s->frame_size + backstep);
1471 de6d9b64 Fabrice Bellard
1472 239c2f4c Fabrice Bellard
    /* prepare next buffer */
1473
    s->inbuf_index ^= 1;
1474
    s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1475
    s->old_frame_size = s->frame_size;
1476
}
1477
1478
static inline void lsf_sf_expand(int *slen,
1479
                                 int sf, int n1, int n2, int n3)
1480
{
1481
    if (n3) {
1482
        slen[3] = sf % n3;
1483
        sf /= n3;
1484
    } else {
1485
        slen[3] = 0;
1486
    }
1487
    if (n2) {
1488
        slen[2] = sf % n2;
1489
        sf /= n2;
1490
    } else {
1491
        slen[2] = 0;
1492
    }
1493
    slen[1] = sf % n1;
1494
    sf /= n1;
1495
    slen[0] = sf;
1496
}
1497
1498
static void exponents_from_scale_factors(MPADecodeContext *s, 
1499
                                         GranuleDef *g,
1500
                                         INT16 *exponents)
1501
{
1502
    const UINT8 *bstab, *pretab;
1503
    int len, i, j, k, l, v0, shift, gain, gains[3];
1504
    INT16 *exp_ptr;
1505
1506
    exp_ptr = exponents;
1507
    gain = g->global_gain - 210;
1508
    shift = g->scalefac_scale + 1;
1509
1510
    bstab = band_size_long[s->sample_rate_index];
1511
    pretab = mpa_pretab[g->preflag];
1512
    for(i=0;i<g->long_end;i++) {
1513
        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1514
        len = bstab[i];
1515
        for(j=len;j>0;j--)
1516
            *exp_ptr++ = v0;
1517
    }
1518
1519
    if (g->short_start < 13) {
1520
        bstab = band_size_short[s->sample_rate_index];
1521
        gains[0] = gain - (g->subblock_gain[0] << 3);
1522
        gains[1] = gain - (g->subblock_gain[1] << 3);
1523
        gains[2] = gain - (g->subblock_gain[2] << 3);
1524
        k = g->long_end;
1525
        for(i=g->short_start;i<13;i++) {
1526
            len = bstab[i];
1527
            for(l=0;l<3;l++) {
1528
                v0 = gains[l] - (g->scale_factors[k++] << shift);
1529
                for(j=len;j>0;j--)
1530
                *exp_ptr++ = v0;
1531
            }
1532
        }
1533
    }
1534
}
1535
1536
/* handle n = 0 too */
1537
static inline int get_bitsz(GetBitContext *s, int n)
1538
{
1539
    if (n == 0)
1540
        return 0;
1541
    else
1542
        return get_bits(s, n);
1543
}
1544
1545
static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1546
                          INT16 *exponents, int end_pos)
1547
{
1548
    int s_index;
1549
    int linbits, code, x, y, l, v, i, j, k, pos;
1550
    UINT8 *last_buf_ptr;
1551
    UINT32 last_bit_buf;
1552
    int last_bit_cnt;
1553
    VLC *vlc;
1554
    UINT8 *code_table;
1555
1556
    /* low frequencies (called big values) */
1557
    s_index = 0;
1558
    for(i=0;i<3;i++) {
1559
        j = g->region_size[i];
1560
        if (j == 0)
1561
            continue;
1562
        /* select vlc table */
1563
        k = g->table_select[i];
1564
        l = mpa_huff_data[k][0];
1565
        linbits = mpa_huff_data[k][1];
1566
        vlc = &huff_vlc[l];
1567
        code_table = huff_code_table[l];
1568
1569
        /* read huffcode and compute each couple */
1570
        for(;j>0;j--) {
1571
            if (get_bits_count(&s->gb) >= end_pos)
1572
                break;
1573
            if (code_table) {
1574
                code = get_vlc(&s->gb, vlc);
1575
                if (code < 0)
1576
                    return -1;
1577
                y = code_table[code];
1578
                x = y >> 4;
1579
                y = y & 0x0f;
1580
            } else {
1581
                x = 0;
1582
                y = 0;
1583
            }
1584
            dprintf("region=%d n=%d x=%d y=%d exp=%d\n", 
1585
                    i, g->region_size[i] - j, x, y, exponents[s_index]);
1586
            if (x) {
1587
                if (x == 15)
1588
                    x += get_bitsz(&s->gb, linbits);
1589
                v = l3_unscale(x, exponents[s_index]);
1590
                if (get_bits1(&s->gb))
1591
                    v = -v;
1592
            } else {
1593
                v = 0;
1594
            }
1595
            g->sb_hybrid[s_index++] = v;
1596
            if (y) {
1597
                if (y == 15)
1598
                    y += get_bitsz(&s->gb, linbits);
1599
                v = l3_unscale(y, exponents[s_index]);
1600
                if (get_bits1(&s->gb))
1601
                    v = -v;
1602
            } else {
1603
                v = 0;
1604
            }
1605
            g->sb_hybrid[s_index++] = v;
1606
        }
1607
    }
1608
            
1609
    /* high frequencies */
1610
    vlc = &huff_quad_vlc[g->count1table_select];
1611
    last_buf_ptr = NULL;
1612
    last_bit_buf = 0;
1613
    last_bit_cnt = 0;
1614
    while (s_index <= 572) {
1615
        pos = get_bits_count(&s->gb);
1616
        if (pos >= end_pos) {
1617
            if (pos > end_pos && last_buf_ptr != NULL) {
1618
                /* some encoders generate an incorrect size for this
1619
                   part. We must go back into the data */
1620
                s_index -= 4;
1621 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
1622
                s->gb.buffer = last_buf_ptr;
1623
                s->gb.index = last_bit_cnt;
1624
#else
1625 239c2f4c Fabrice Bellard
                s->gb.buf_ptr = last_buf_ptr;
1626
                s->gb.bit_buf = last_bit_buf;
1627
                s->gb.bit_cnt = last_bit_cnt;
1628 27a3e2c5 Michael Niedermayer
#endif            
1629 239c2f4c Fabrice Bellard
            }
1630
            break;
1631
        }
1632 27a3e2c5 Michael Niedermayer
#ifdef ALT_BITSTREAM_READER
1633
        last_buf_ptr = s->gb.buffer;
1634
        last_bit_cnt = s->gb.index;
1635
#else
1636 239c2f4c Fabrice Bellard
        last_buf_ptr = s->gb.buf_ptr;
1637
        last_bit_buf = s->gb.bit_buf;
1638
        last_bit_cnt = s->gb.bit_cnt;
1639 27a3e2c5 Michael Niedermayer
#endif
1640 239c2f4c Fabrice Bellard
        
1641
        code = get_vlc(&s->gb, vlc);
1642
        dprintf("t=%d code=%d\n", g->count1table_select, code);
1643
        if (code < 0)
1644
            return -1;
1645
        for(i=0;i<4;i++) {
1646
            if (code & (8 >> i)) {
1647
                /* non zero value. Could use a hand coded function for
1648
                   'one' value */
1649
                v = l3_unscale(1, exponents[s_index]);
1650
                if(get_bits1(&s->gb))
1651
                    v = -v;
1652
            } else {
1653
                v = 0;
1654
            }
1655
            g->sb_hybrid[s_index++] = v;
1656
        }
1657
    }
1658
    while (s_index < 576)
1659
        g->sb_hybrid[s_index++] = 0;
1660 de6d9b64 Fabrice Bellard
    return 0;
1661
}
1662
1663 239c2f4c Fabrice Bellard
/* Reorder short blocks from bitstream order to interleaved order. It
1664
   would be faster to do it in parsing, but the code would be far more
1665
   complicated */
1666
static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1667
{
1668
    int i, j, k, len;
1669
    INT32 *ptr, *dst, *ptr1;
1670
    INT32 tmp[576];
1671
1672
    if (g->block_type != 2)
1673
        return;
1674
1675
    if (g->switch_point) {
1676
        if (s->sample_rate_index != 8) {
1677
            ptr = g->sb_hybrid + 36;
1678
        } else {
1679
            ptr = g->sb_hybrid + 48;
1680
        }
1681
    } else {
1682
        ptr = g->sb_hybrid;
1683
    }
1684
    
1685
    for(i=g->short_start;i<13;i++) {
1686
        len = band_size_short[s->sample_rate_index][i];
1687
        ptr1 = ptr;
1688
        for(k=0;k<3;k++) {
1689
            dst = tmp + k;
1690
            for(j=len;j>0;j--) {
1691
                *dst = *ptr++;
1692
                dst += 3;
1693
            }
1694
        }
1695
        memcpy(ptr1, tmp, len * 3 * sizeof(INT32));
1696
    }
1697
}
1698
1699
#define ISQRT2 FIXR(0.70710678118654752440)
1700
1701
static void compute_stereo(MPADecodeContext *s,
1702
                           GranuleDef *g0, GranuleDef *g1)
1703
{
1704
    int i, j, k, l;
1705
    INT32 v1, v2;
1706
    int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1707
    INT32 (*is_tab)[16];
1708
    INT32 *tab0, *tab1;
1709
    int non_zero_found_short[3];
1710
1711
    /* intensity stereo */
1712
    if (s->mode_ext & MODE_EXT_I_STEREO) {
1713
        if (!s->lsf) {
1714
            is_tab = is_table;
1715
            sf_max = 7;
1716
        } else {
1717
            is_tab = is_table_lsf[g1->scalefac_compress & 1];
1718
            sf_max = 16;
1719
        }
1720
            
1721
        tab0 = g0->sb_hybrid + 576;
1722
        tab1 = g1->sb_hybrid + 576;
1723
1724
        non_zero_found_short[0] = 0;
1725
        non_zero_found_short[1] = 0;
1726
        non_zero_found_short[2] = 0;
1727
        k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1728
        for(i = 12;i >= g1->short_start;i--) {
1729
            /* for last band, use previous scale factor */
1730
            if (i != 11)
1731
                k -= 3;
1732
            len = band_size_short[s->sample_rate_index][i];
1733
            for(l=2;l>=0;l--) {
1734
                tab0 -= len;
1735
                tab1 -= len;
1736
                if (!non_zero_found_short[l]) {
1737
                    /* test if non zero band. if so, stop doing i-stereo */
1738
                    for(j=0;j<len;j++) {
1739
                        if (tab1[j] != 0) {
1740
                            non_zero_found_short[l] = 1;
1741
                            goto found1;
1742
                        }
1743
                    }
1744
                    sf = g1->scale_factors[k + l];
1745
                    if (sf >= sf_max)
1746
                        goto found1;
1747
1748
                    v1 = is_tab[0][sf];
1749
                    v2 = is_tab[1][sf];
1750
                    for(j=0;j<len;j++) {
1751
                        tmp0 = tab0[j];
1752
                        tab0[j] = MULL(tmp0, v1);
1753
                        tab1[j] = MULL(tmp0, v2);
1754
                    }
1755
                } else {
1756
                found1:
1757
                    if (s->mode_ext & MODE_EXT_MS_STEREO) {
1758
                        /* lower part of the spectrum : do ms stereo
1759
                           if enabled */
1760
                        for(j=0;j<len;j++) {
1761
                            tmp0 = tab0[j];
1762
                            tmp1 = tab1[j];
1763
                            tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1764
                            tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1765
                        }
1766
                    }
1767
                }
1768
            }
1769
        }
1770
1771
        non_zero_found = non_zero_found_short[0] | 
1772
            non_zero_found_short[1] | 
1773
            non_zero_found_short[2];
1774
1775
        for(i = g1->long_end - 1;i >= 0;i--) {
1776
            len = band_size_long[s->sample_rate_index][i];
1777
            tab0 -= len;
1778
            tab1 -= len;
1779
            /* test if non zero band. if so, stop doing i-stereo */
1780
            if (!non_zero_found) {
1781
                for(j=0;j<len;j++) {
1782
                    if (tab1[j] != 0) {
1783
                        non_zero_found = 1;
1784
                        goto found2;
1785
                    }
1786
                }
1787
                /* for last band, use previous scale factor */
1788
                k = (i == 21) ? 20 : i;
1789
                sf = g1->scale_factors[k];
1790
                if (sf >= sf_max)
1791
                    goto found2;
1792
                v1 = is_tab[0][sf];
1793
                v2 = is_tab[1][sf];
1794
                for(j=0;j<len;j++) {
1795
                    tmp0 = tab0[j];
1796
                    tab0[j] = MULL(tmp0, v1);
1797
                    tab1[j] = MULL(tmp0, v2);
1798
                }
1799
            } else {
1800
            found2:
1801
                if (s->mode_ext & MODE_EXT_MS_STEREO) {
1802
                    /* lower part of the spectrum : do ms stereo
1803
                       if enabled */
1804
                    for(j=0;j<len;j++) {
1805
                        tmp0 = tab0[j];
1806
                        tmp1 = tab1[j];
1807
                        tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1808
                        tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1809
                    }
1810
                }
1811
            }
1812
        }
1813
    } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1814
        /* ms stereo ONLY */
1815
        /* NOTE: the 1/sqrt(2) normalization factor is included in the
1816
           global gain */
1817
        tab0 = g0->sb_hybrid;
1818
        tab1 = g1->sb_hybrid;
1819
        for(i=0;i<576;i++) {
1820
            tmp0 = tab0[i];
1821
            tmp1 = tab1[i];
1822
            tab0[i] = tmp0 + tmp1;
1823
            tab1[i] = tmp0 - tmp1;
1824
        }
1825
    }
1826
}
1827
1828
static void compute_antialias(MPADecodeContext *s,
1829
                              GranuleDef *g)
1830
{
1831
    INT32 *ptr, *p0, *p1, *csa;
1832
    int n, tmp0, tmp1, i, j;
1833
1834
    /* we antialias only "long" bands */
1835
    if (g->block_type == 2) {
1836
        if (!g->switch_point)
1837
            return;
1838
        /* XXX: check this for 8000Hz case */
1839
        n = 1;
1840
    } else {
1841
        n = SBLIMIT - 1;
1842
    }
1843
    
1844
    ptr = g->sb_hybrid + 18;
1845
    for(i = n;i > 0;i--) {
1846
        p0 = ptr - 1;
1847
        p1 = ptr;
1848
        csa = &csa_table[0][0];
1849
        for(j=0;j<8;j++) {
1850
            tmp0 = *p0;
1851
            tmp1 = *p1;
1852
            *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1853
            *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1854
            p0--;
1855
            p1++;
1856
            csa += 2;
1857
        }
1858
        ptr += 18;
1859
    }
1860
}
1861
1862
static void compute_imdct(MPADecodeContext *s,
1863
                          GranuleDef *g, 
1864
                          INT32 *sb_samples,
1865
                          INT32 *mdct_buf)
1866
{
1867
    INT32 *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1;
1868
    INT32 in[6];
1869
    INT32 out[36];
1870
    INT32 out2[12];
1871
    int i, j, k, mdct_long_end, v, sblimit;
1872
1873
    /* find last non zero block */
1874
    ptr = g->sb_hybrid + 576;
1875
    ptr1 = g->sb_hybrid + 2 * 18;
1876
    while (ptr >= ptr1) {
1877
        ptr -= 6;
1878
        v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1879
        if (v != 0)
1880
            break;
1881
    }
1882
    sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1883
1884
    if (g->block_type == 2) {
1885
        /* XXX: check for 8000 Hz */
1886
        if (g->switch_point)
1887
            mdct_long_end = 2;
1888
        else
1889
            mdct_long_end = 0;
1890
    } else {
1891
        mdct_long_end = sblimit;
1892
    }
1893
1894
    buf = mdct_buf;
1895
    ptr = g->sb_hybrid;
1896
    for(j=0;j<mdct_long_end;j++) {
1897
        imdct36(out, ptr);
1898
        /* apply window & overlap with previous buffer */
1899
        out_ptr = sb_samples + j;
1900
        /* select window */
1901
        if (g->switch_point && j < 2)
1902
            win1 = mdct_win[0];
1903
        else
1904
            win1 = mdct_win[g->block_type];
1905
        /* select frequency inversion */
1906
        win = win1 + ((4 * 36) & -(j & 1));
1907
        for(i=0;i<18;i++) {
1908
            *out_ptr = MULL(out[i], win[i]) + buf[i];
1909
            buf[i] = MULL(out[i + 18], win[i + 18]);
1910
            out_ptr += SBLIMIT;
1911
        }
1912
        ptr += 18;
1913
        buf += 18;
1914
    }
1915
    for(j=mdct_long_end;j<sblimit;j++) {
1916
        for(i=0;i<6;i++) {
1917
            out[i] = 0;
1918
            out[6 + i] = 0;
1919
            out[30+i] = 0;
1920
        }
1921
        /* select frequency inversion */
1922
        win = mdct_win[2] + ((4 * 36) & -(j & 1));
1923
        buf2 = out + 6;
1924
        for(k=0;k<3;k++) {
1925
            /* reorder input for short mdct */
1926
            ptr1 = ptr + k;
1927
            for(i=0;i<6;i++) {
1928
                in[i] = *ptr1;
1929
                ptr1 += 3;
1930
            }
1931
            imdct12(out2, in);
1932
            /* apply 12 point window and do small overlap */
1933
            for(i=0;i<6;i++) {
1934
                buf2[i] = MULL(out2[i], win[i]) + buf2[i];
1935
                buf2[i + 6] = MULL(out2[i + 6], win[i + 6]);
1936
            }
1937
            buf2 += 6;
1938
        }
1939
        /* overlap */
1940
        out_ptr = sb_samples + j;
1941
        for(i=0;i<18;i++) {
1942
            *out_ptr = out[i] + buf[i];
1943
            buf[i] = out[i + 18];
1944
            out_ptr += SBLIMIT;
1945
        }
1946
        ptr += 18;
1947
        buf += 18;
1948
    }
1949
    /* zero bands */
1950
    for(j=sblimit;j<SBLIMIT;j++) {
1951
        /* overlap */
1952
        out_ptr = sb_samples + j;
1953
        for(i=0;i<18;i++) {
1954
            *out_ptr = buf[i];
1955
            buf[i] = 0;
1956
            out_ptr += SBLIMIT;
1957
        }
1958
        buf += 18;
1959
    }
1960
}
1961
1962 747a67fb Fabrice Bellard
#if defined(DEBUG)
1963 239c2f4c Fabrice Bellard
void sample_dump(int fnum, INT32 *tab, int n)
1964
{
1965
    static FILE *files[16], *f;
1966
    char buf[512];
1967 81552334 Fabrice Bellard
    int i;
1968
    INT32 v;
1969
    
1970 239c2f4c Fabrice Bellard
    f = files[fnum];
1971
    if (!f) {
1972 81552334 Fabrice Bellard
        sprintf(buf, "/tmp/out%d.%s.pcm", 
1973
                fnum, 
1974
#ifdef USE_HIGHPRECISION
1975
                "hp"
1976
#else
1977
                "lp"
1978
#endif
1979
                );
1980 239c2f4c Fabrice Bellard
        f = fopen(buf, "w");
1981
        if (!f)
1982
            return;
1983
        files[fnum] = f;
1984
    }
1985
    
1986
    if (fnum == 0) {
1987
        static int pos = 0;
1988
        printf("pos=%d\n", pos);
1989
        for(i=0;i<n;i++) {
1990 81552334 Fabrice Bellard
            printf(" %0.4f", (double)tab[i] / FRAC_ONE);
1991 239c2f4c Fabrice Bellard
            if ((i % 18) == 17)
1992
                printf("\n");
1993
        }
1994
        pos += n;
1995
    }
1996 81552334 Fabrice Bellard
    for(i=0;i<n;i++) {
1997
        /* normalize to 23 frac bits */
1998
        v = tab[i] << (23 - FRAC_BITS);
1999
        fwrite(&v, 1, sizeof(INT32), f);
2000
    }
2001 239c2f4c Fabrice Bellard
}
2002
#endif
2003
2004
2005
/* main layer3 decoding function */
2006
static int mp_decode_layer3(MPADecodeContext *s)
2007
{
2008
    int nb_granules, main_data_begin, private_bits;
2009
    int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2010
    GranuleDef granules[2][2], *g;
2011
    INT16 exponents[576];
2012
2013
    /* read side info */
2014
    if (s->lsf) {
2015
        main_data_begin = get_bits(&s->gb, 8);
2016
        if (s->nb_channels == 2)
2017
            private_bits = get_bits(&s->gb, 2);
2018
        else
2019
            private_bits = get_bits(&s->gb, 1);
2020
        nb_granules = 1;
2021
    } else {
2022
        main_data_begin = get_bits(&s->gb, 9);
2023
        if (s->nb_channels == 2)
2024
            private_bits = get_bits(&s->gb, 3);
2025
        else
2026
            private_bits = get_bits(&s->gb, 5);
2027
        nb_granules = 2;
2028
        for(ch=0;ch<s->nb_channels;ch++) {
2029
            granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2030
            granules[ch][1].scfsi = get_bits(&s->gb, 4);
2031
        }
2032
    }
2033
    
2034
    for(gr=0;gr<nb_granules;gr++) {
2035
        for(ch=0;ch<s->nb_channels;ch++) {
2036
            dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2037
            g = &granules[ch][gr];
2038
            g->part2_3_length = get_bits(&s->gb, 12);
2039
            g->big_values = get_bits(&s->gb, 9);
2040
            g->global_gain = get_bits(&s->gb, 8);
2041
            /* if MS stereo only is selected, we precompute the
2042
               1/sqrt(2) renormalization factor */
2043
            if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) == 
2044
                MODE_EXT_MS_STEREO)
2045
                g->global_gain -= 2;
2046
            if (s->lsf)
2047
                g->scalefac_compress = get_bits(&s->gb, 9);
2048
            else
2049
                g->scalefac_compress = get_bits(&s->gb, 4);
2050
            blocksplit_flag = get_bits(&s->gb, 1);
2051
            if (blocksplit_flag) {
2052
                g->block_type = get_bits(&s->gb, 2);
2053
                if (g->block_type == 0)
2054
                    return -1;
2055
                g->switch_point = get_bits(&s->gb, 1);
2056
                for(i=0;i<2;i++)
2057
                    g->table_select[i] = get_bits(&s->gb, 5);
2058
                for(i=0;i<3;i++) 
2059
                    g->subblock_gain[i] = get_bits(&s->gb, 3);
2060
                /* compute huffman coded region sizes */
2061
                if (g->block_type == 2)
2062
                    g->region_size[0] = (36 / 2);
2063
                else {
2064
                    if (s->sample_rate_index <= 2) 
2065
                        g->region_size[0] = (36 / 2);
2066
                    else if (s->sample_rate_index != 8) 
2067
                        g->region_size[0] = (54 / 2);
2068
                    else
2069
                        g->region_size[0] = (108 / 2);
2070
                }
2071
                g->region_size[1] = (576 / 2);
2072
            } else {
2073
                int region_address1, region_address2, l;
2074
                g->block_type = 0;
2075
                g->switch_point = 0;
2076
                for(i=0;i<3;i++)
2077
                    g->table_select[i] = get_bits(&s->gb, 5);
2078
                /* compute huffman coded region sizes */
2079
                region_address1 = get_bits(&s->gb, 4);
2080
                region_address2 = get_bits(&s->gb, 3);
2081
                dprintf("region1=%d region2=%d\n", 
2082
                        region_address1, region_address2);
2083
                g->region_size[0] = 
2084
                    band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2085
                l = region_address1 + region_address2 + 2;
2086
                /* should not overflow */
2087
                if (l > 22)
2088
                    l = 22;
2089
                g->region_size[1] = 
2090
                    band_index_long[s->sample_rate_index][l] >> 1;
2091
            }
2092
            /* convert region offsets to region sizes and truncate
2093
               size to big_values */
2094
            g->region_size[2] = (576 / 2);
2095
            j = 0;
2096
            for(i=0;i<3;i++) {
2097
                k = g->region_size[i];
2098
                if (k > g->big_values)
2099
                    k = g->big_values;
2100
                g->region_size[i] = k - j;
2101
                j = k;
2102
            }
2103
2104
            /* compute band indexes */
2105
            if (g->block_type == 2) {
2106
                if (g->switch_point) {
2107
                    /* if switched mode, we handle the 36 first samples as
2108
                       long blocks.  For 8000Hz, we handle the 48 first
2109
                       exponents as long blocks (XXX: check this!) */
2110
                    if (s->sample_rate_index <= 2)
2111
                        g->long_end = 8;
2112
                    else if (s->sample_rate_index != 8)
2113
                        g->long_end = 6;
2114
                    else
2115
                        g->long_end = 4; /* 8000 Hz */
2116
                    
2117
                    if (s->sample_rate_index != 8)
2118
                        g->short_start = 3;
2119
                    else
2120
                        g->short_start = 2; 
2121
                } else {
2122
                    g->long_end = 0;
2123
                    g->short_start = 0;
2124
                }
2125
            } else {
2126
                g->short_start = 13;
2127
                g->long_end = 22;
2128
            }
2129
            
2130
            g->preflag = 0;
2131
            if (!s->lsf)
2132
                g->preflag = get_bits(&s->gb, 1);
2133
            g->scalefac_scale = get_bits(&s->gb, 1);
2134
            g->count1table_select = get_bits(&s->gb, 1);
2135
            dprintf("block_type=%d switch_point=%d\n",
2136
                    g->block_type, g->switch_point);
2137
        }
2138
    }
2139
2140
    /* now we get bits from the main_data_begin offset */
2141
    dprintf("seekback: %d\n", main_data_begin);
2142
    seek_to_maindata(s, main_data_begin);
2143
2144
    for(gr=0;gr<nb_granules;gr++) {
2145
        for(ch=0;ch<s->nb_channels;ch++) {
2146
            g = &granules[ch][gr];
2147
            
2148
            bits_pos = get_bits_count(&s->gb);
2149
            
2150
            if (!s->lsf) {
2151
                UINT8 *sc;
2152
                int slen, slen1, slen2;
2153
2154
                /* MPEG1 scale factors */
2155
                slen1 = slen_table[0][g->scalefac_compress];
2156
                slen2 = slen_table[1][g->scalefac_compress];
2157
                dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2158
                if (g->block_type == 2) {
2159
                    n = g->switch_point ? 17 : 18;
2160
                    j = 0;
2161
                    for(i=0;i<n;i++)
2162
                        g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2163
                    for(i=0;i<18;i++)
2164
                        g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2165
                    for(i=0;i<3;i++)
2166
                        g->scale_factors[j++] = 0;
2167
                } else {
2168
                    sc = granules[ch][0].scale_factors;
2169
                    j = 0;
2170
                    for(k=0;k<4;k++) {
2171
                        n = (k == 0 ? 6 : 5);
2172
                        if ((g->scfsi & (0x8 >> k)) == 0) {
2173
                            slen = (k < 2) ? slen1 : slen2;
2174
                            for(i=0;i<n;i++)
2175
                                g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2176
                        } else {
2177
                            /* simply copy from last granule */
2178
                            for(i=0;i<n;i++) {
2179
                                g->scale_factors[j] = sc[j];
2180
                                j++;
2181
                            }
2182
                        }
2183
                    }
2184
                    g->scale_factors[j++] = 0;
2185
                }
2186 747a67fb Fabrice Bellard
#if defined(DEBUG)
2187 239c2f4c Fabrice Bellard
                {
2188
                    printf("scfsi=%x gr=%d ch=%d scale_factors:\n", 
2189
                           g->scfsi, gr, ch);
2190
                    for(i=0;i<j;i++)
2191
                        printf(" %d", g->scale_factors[i]);
2192
                    printf("\n");
2193
                }
2194
#endif
2195
            } else {
2196
                int tindex, tindex2, slen[4], sl, sf;
2197
2198
                /* LSF scale factors */
2199
                if (g->block_type == 2) {
2200
                    tindex = g->switch_point ? 2 : 1;
2201
                } else {
2202
                    tindex = 0;
2203
                }
2204
                sf = g->scalefac_compress;
2205
                if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2206
                    /* intensity stereo case */
2207
                    sf >>= 1;
2208
                    if (sf < 180) {
2209
                        lsf_sf_expand(slen, sf, 6, 6, 0);
2210
                        tindex2 = 3;
2211
                    } else if (sf < 244) {
2212
                        lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2213
                        tindex2 = 4;
2214
                    } else {
2215
                        lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2216
                        tindex2 = 5;
2217
                    }
2218
                } else {
2219
                    /* normal case */
2220
                    if (sf < 400) {
2221
                        lsf_sf_expand(slen, sf, 5, 4, 4);
2222
                        tindex2 = 0;
2223
                    } else if (sf < 500) {
2224
                        lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2225
                        tindex2 = 1;
2226
                    } else {
2227
                        lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2228
                        tindex2 = 2;
2229
                        g->preflag = 1;
2230
                    }
2231
                }
2232
2233
                j = 0;
2234
                for(k=0;k<4;k++) {
2235
                    n = lsf_nsf_table[tindex2][tindex][k];
2236
                    sl = slen[k];
2237
                    for(i=0;i<n;i++)
2238
                        g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2239
                }
2240
                /* XXX: should compute exact size */
2241
                for(;j<40;j++)
2242
                    g->scale_factors[j] = 0;
2243 747a67fb Fabrice Bellard
#if defined(DEBUG)
2244 239c2f4c Fabrice Bellard
                {
2245
                    printf("gr=%d ch=%d scale_factors:\n", 
2246
                           gr, ch);
2247
                    for(i=0;i<40;i++)
2248
                        printf(" %d", g->scale_factors[i]);
2249
                    printf("\n");
2250
                }
2251
#endif
2252
            }
2253
2254
            exponents_from_scale_factors(s, g, exponents);
2255
2256
            /* read Huffman coded residue */
2257
            if (huffman_decode(s, g, exponents,
2258
                               bits_pos + g->part2_3_length) < 0)
2259
                return -1;
2260 747a67fb Fabrice Bellard
#if defined(DEBUG)
2261
            sample_dump(0, g->sb_hybrid, 576);
2262 239c2f4c Fabrice Bellard
#endif
2263
2264
            /* skip extension bits */
2265
            bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2266
            if (bits_left < 0) {
2267
                dprintf("bits_left=%d\n", bits_left);
2268
                return -1;
2269
            }
2270
            while (bits_left >= 16) {
2271
                skip_bits(&s->gb, 16);
2272
                bits_left -= 16;
2273
            }
2274
            if (bits_left > 0)
2275
                skip_bits(&s->gb, bits_left);
2276
        } /* ch */
2277
2278
        if (s->nb_channels == 2)
2279
            compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2280
2281
        for(ch=0;ch<s->nb_channels;ch++) {
2282
            g = &granules[ch][gr];
2283
2284
            reorder_block(s, g);
2285 747a67fb Fabrice Bellard
#if defined(DEBUG)
2286 239c2f4c Fabrice Bellard
            sample_dump(0, g->sb_hybrid, 576);
2287
#endif
2288
            compute_antialias(s, g);
2289 81552334 Fabrice Bellard
#if defined(DEBUG)
2290 239c2f4c Fabrice Bellard
            sample_dump(1, g->sb_hybrid, 576);
2291
#endif
2292
            compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); 
2293 81552334 Fabrice Bellard
#if defined(DEBUG)
2294 239c2f4c Fabrice Bellard
            sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2295
#endif
2296
        }
2297
    } /* gr */
2298
    return nb_granules * 18;
2299
}
2300
2301
static int mp_decode_frame(MPADecodeContext *s, 
2302
                           short *samples)
2303
{
2304
    int i, nb_frames, ch;
2305
    short *samples_ptr;
2306
2307
    init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, 
2308
                  s->inbuf_ptr - s->inbuf - HEADER_SIZE);
2309
    
2310
    /* skip error protection field */
2311
    if (s->error_protection)
2312
        get_bits(&s->gb, 16);
2313
2314
    dprintf("frame %d:\n", s->frame_count);
2315
    switch(s->layer) {
2316
    case 1:
2317
        nb_frames = mp_decode_layer1(s);
2318
        break;
2319
    case 2:
2320
        nb_frames = mp_decode_layer2(s);
2321
        break;
2322
    case 3:
2323
    default:
2324
        nb_frames = mp_decode_layer3(s);
2325
        break;
2326
    }
2327
#if defined(DEBUG)
2328
    for(i=0;i<nb_frames;i++) {
2329
        for(ch=0;ch<s->nb_channels;ch++) {
2330
            int j;
2331
            printf("%d-%d:", i, ch);
2332
            for(j=0;j<SBLIMIT;j++)
2333
                printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2334
            printf("\n");
2335
        }
2336
    }
2337
#endif
2338
    /* apply the synthesis filter */
2339
    for(ch=0;ch<s->nb_channels;ch++) {
2340
        samples_ptr = samples + ch;
2341
        for(i=0;i<nb_frames;i++) {
2342
            synth_filter(s, ch, samples_ptr, s->nb_channels,
2343
                         s->sb_samples[ch][i]);
2344
            samples_ptr += 32 * s->nb_channels;
2345
        }
2346
    }
2347
#ifdef DEBUG
2348
    s->frame_count++;        
2349
#endif
2350
    return nb_frames * 32 * sizeof(short) * s->nb_channels;
2351
}
2352
2353 de6d9b64 Fabrice Bellard
static int decode_frame(AVCodecContext * avctx,
2354
                        void *data, int *data_size,
2355
                        UINT8 * buf, int buf_size)
2356
{
2357
    MPADecodeContext *s = avctx->priv_data;
2358
    UINT32 header;
2359
    UINT8 *buf_ptr;
2360
    int len, out_size;
2361
    short *out_samples = data;
2362
2363
    *data_size = 0;
2364
    buf_ptr = buf;
2365
    while (buf_size > 0) {
2366
        len = s->inbuf_ptr - s->inbuf;
2367
        if (s->frame_size == 0) {
2368 239c2f4c Fabrice Bellard
            /* special case for next header for first frame in free
2369
               format case (XXX: find a simpler method) */
2370
            if (s->free_format_next_header != 0) {
2371
                s->inbuf[0] = s->free_format_next_header >> 24;
2372
                s->inbuf[1] = s->free_format_next_header >> 16;
2373
                s->inbuf[2] = s->free_format_next_header >> 8;
2374
                s->inbuf[3] = s->free_format_next_header;
2375
                s->inbuf_ptr = s->inbuf + 4;
2376
                s->free_format_next_header = 0;
2377
                goto got_header;
2378
            }
2379
            /* no header seen : find one. We need at least HEADER_SIZE
2380
               bytes to parse it */
2381 de6d9b64 Fabrice Bellard
            len = HEADER_SIZE - len;
2382
            if (len > buf_size)
2383
                len = buf_size;
2384 92d24f49 Zdenek Kabelac
            if (len > 0) {
2385 2d83f323 Zdenek Kabelac
                memcpy(s->inbuf_ptr, buf_ptr, len);
2386
                buf_ptr += len;
2387
                buf_size -= len;
2388 c152c983 Zdenek Kabelac
                s->inbuf_ptr += len;
2389
            }
2390
            if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2391 239c2f4c Fabrice Bellard
            got_header:
2392 de6d9b64 Fabrice Bellard
                header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2393
                    (s->inbuf[2] << 8) | s->inbuf[3];
2394 92d24f49 Zdenek Kabelac
2395 de6d9b64 Fabrice Bellard
                if (check_header(header) < 0) {
2396
                    /* no sync found : move by one byte (inefficient, but simple!) */
2397 2d83f323 Zdenek Kabelac
                    memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2398 de6d9b64 Fabrice Bellard
                    s->inbuf_ptr--;
2399 239c2f4c Fabrice Bellard
                    dprintf("skip %x\n", header);
2400
                    /* reset free format frame size to give a chance
2401
                       to get a new bitrate */
2402
                    s->free_format_frame_size = 0;
2403 de6d9b64 Fabrice Bellard
                } else {
2404 239c2f4c Fabrice Bellard
                    if (decode_header(s, header) == 1) {
2405 81552334 Fabrice Bellard
                        /* free format: prepare to compute frame size */
2406 2d83f323 Zdenek Kabelac
                        s->frame_size = -1;
2407 239c2f4c Fabrice Bellard
                    }
2408 81552334 Fabrice Bellard
                    /* update codec info */
2409
                    avctx->sample_rate = s->sample_rate;
2410
                    avctx->channels = s->nb_channels;
2411
                    avctx->bit_rate = s->bit_rate;
2412
                    avctx->frame_size = s->frame_size;
2413 de6d9b64 Fabrice Bellard
                }
2414
            }
2415 239c2f4c Fabrice Bellard
        } else if (s->frame_size == -1) {
2416
            /* free format : find next sync to compute frame size */
2417
            len = MPA_MAX_CODED_FRAME_SIZE - len;
2418
            if (len > buf_size)
2419
                len = buf_size;
2420
            if (len == 0) {
2421
                /* frame too long: resync */
2422
                s->frame_size = 0;
2423
            } else {
2424
                UINT8 *p, *pend;
2425
                UINT32 header1;
2426
                int padding;
2427
2428
                memcpy(s->inbuf_ptr, buf_ptr, len);
2429
                /* check for header */
2430
                p = s->inbuf_ptr - 3;
2431
                pend = s->inbuf_ptr + len - 4;
2432
                while (p <= pend) {
2433
                    header = (p[0] << 24) | (p[1] << 16) |
2434
                        (p[2] << 8) | p[3];
2435
                    header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2436
                        (s->inbuf[2] << 8) | s->inbuf[3];
2437
                    /* check with high probability that we have a
2438
                       valid header */
2439
                    if ((header & SAME_HEADER_MASK) ==
2440
                        (header1 & SAME_HEADER_MASK)) {
2441
                        /* header found: update pointers */
2442
                        len = (p + 4) - s->inbuf_ptr;
2443
                        buf_ptr += len;
2444
                        buf_size -= len;
2445
                        s->inbuf_ptr = p;
2446
                        /* compute frame size */
2447
                        s->free_format_next_header = header;
2448
                        s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2449
                        padding = (header1 >> 9) & 1;
2450
                        if (s->layer == 1)
2451
                            s->free_format_frame_size -= padding * 4;
2452
                        else
2453
                            s->free_format_frame_size -= padding;
2454
                        dprintf("free frame size=%d padding=%d\n", 
2455
                                s->free_format_frame_size, padding);
2456
                        decode_header(s, header1);
2457
                        goto next_data;
2458
                    }
2459
                    p++;
2460
                }
2461
                /* not found: simply increase pointers */
2462
                buf_ptr += len;
2463
                s->inbuf_ptr += len;
2464
                buf_size -= len;
2465
            }
2466 de6d9b64 Fabrice Bellard
        } else if (len < s->frame_size) {
2467 de5123dc Zdenek Kabelac
            if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2468
                s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2469 de6d9b64 Fabrice Bellard
            len = s->frame_size - len;
2470
            if (len > buf_size)
2471
                len = buf_size;
2472
            memcpy(s->inbuf_ptr, buf_ptr, len);
2473
            buf_ptr += len;
2474
            s->inbuf_ptr += len;
2475
            buf_size -= len;
2476
        } else {
2477
            out_size = mp_decode_frame(s, out_samples);
2478
            s->inbuf_ptr = s->inbuf;
2479
            s->frame_size = 0;
2480
            *data_size = out_size;
2481
            break;
2482
        }
2483 239c2f4c Fabrice Bellard
    next_data:
2484 de6d9b64 Fabrice Bellard
    }
2485
    return buf_ptr - buf;
2486
}
2487
2488 4b1f4f23 Juanjo
AVCodec mp2_decoder =
2489 de6d9b64 Fabrice Bellard
{
2490 4b1f4f23 Juanjo
    "mp2",
2491 de6d9b64 Fabrice Bellard
    CODEC_TYPE_AUDIO,
2492
    CODEC_ID_MP2,
2493
    sizeof(MPADecodeContext),
2494
    decode_init,
2495
    NULL,
2496
    NULL,
2497
    decode_frame,
2498
};
2499 4b1f4f23 Juanjo
2500
AVCodec mp3_decoder =
2501
{
2502
    "mp3",
2503
    CODEC_TYPE_AUDIO,
2504
    CODEC_ID_MP3LAME,
2505
    sizeof(MPADecodeContext),
2506
    decode_init,
2507
    NULL,
2508
    NULL,
2509
    decode_frame,
2510
};
2511 cd4af68a Zdenek Kabelac
2512
#undef C1
2513
#undef C2
2514
#undef C3
2515
#undef C4
2516
#undef C5
2517
#undef C6
2518
#undef C7
2519
#undef C8
2520
#undef FRAC_BITS
2521
#undef HEADER_SIZE