Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudiodec.c @ cf92cec7

History | View | Annotate | Download (77.9 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * MPEG Audio decoder
3 406792e7 Diego Biurrun
 * Copyright (c) 2001, 2002 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 ff4ec49e Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 983e3246 Michael Niedermayer
22
/**
23
 * @file mpegaudiodec.c
24
 * MPEG Audio decoder.
25 115329f1 Diego Biurrun
 */
26 983e3246 Michael Niedermayer
27 de6d9b64 Fabrice Bellard
#include "avcodec.h"
28 caa336b4 Michael Niedermayer
#include "bitstream.h"
29 84f986c0 Steven M. Schultz
#include "dsputil.h"
30 de6d9b64 Fabrice Bellard
31
/*
32 239c2f4c Fabrice Bellard
 * TODO:
33
 *  - in low precision mode, use more 16 bit multiplies in synth filter
34
 *  - test lsf / mpeg25 extensively.
35 de6d9b64 Fabrice Bellard
 */
36
37 d9b1c197 Roberto Togni
#include "mpegaudio.h"
38 4bd8e17c Aurelien Jacobs
#include "mpegaudiodecheader.h"
39 a3a5f4d6 Michael Niedermayer
40 004390a6 Luca Barbato
#include "mathops.h"
41
42 239c2f4c Fabrice Bellard
/* WARNING: only correct for posititive numbers */
43
#define FIXR(a)   ((int)((a) * FRAC_ONE + 0.5))
44
#define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
45
46 711ae726 Michael Niedermayer
#define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
47
48 239c2f4c Fabrice Bellard
/****************/
49
50 de6d9b64 Fabrice Bellard
#define HEADER_SIZE 4
51
52 239c2f4c Fabrice Bellard
/* layer 3 "granule" */
53
typedef struct GranuleDef {
54 0c1a9eda Zdenek Kabelac
    uint8_t scfsi;
55 239c2f4c Fabrice Bellard
    int part2_3_length;
56
    int big_values;
57
    int global_gain;
58
    int scalefac_compress;
59 0c1a9eda Zdenek Kabelac
    uint8_t block_type;
60
    uint8_t switch_point;
61 239c2f4c Fabrice Bellard
    int table_select[3];
62
    int subblock_gain[3];
63 0c1a9eda Zdenek Kabelac
    uint8_t scalefac_scale;
64
    uint8_t count1table_select;
65 239c2f4c Fabrice Bellard
    int region_size[3]; /* number of huffman codes in each region */
66
    int preflag;
67
    int short_start, long_end; /* long/short band indexes */
68 0c1a9eda Zdenek Kabelac
    uint8_t scale_factors[40];
69
    int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
70 239c2f4c Fabrice Bellard
} GranuleDef;
71 de6d9b64 Fabrice Bellard
72 4991c051 Aurelien Jacobs
#include "mpegaudiodata.h"
73 239c2f4c Fabrice Bellard
#include "mpegaudiodectab.h"
74
75 a1e257b2 Michael Niedermayer
static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
76
static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
77
78 239c2f4c Fabrice Bellard
/* vlc structure for decoding layer 3 huffman tables */
79 115329f1 Diego Biurrun
static VLC huff_vlc[16];
80 bbdf8728 Art Clarke
static VLC_TYPE huff_vlc_tables[
81
  0+128+128+128+130+128+154+166+
82
  142+204+190+170+542+460+662+414
83
  ][2];
84
static const int huff_vlc_tables_sizes[16] = {
85
  0, 128, 128, 128, 130, 128, 154, 166,
86
  142, 204, 190, 170, 542, 460, 662, 414
87
};
88 239c2f4c Fabrice Bellard
static VLC huff_quad_vlc[2];
89 bbdf8728 Art Clarke
static VLC_TYPE huff_quad_vlc_tables[128+16][2];
90
static const int huff_quad_vlc_tables_sizes[2] = {
91
  128, 16
92
};
93 239c2f4c Fabrice Bellard
/* computed from band_size_long */
94 0c1a9eda Zdenek Kabelac
static uint16_t band_index_long[9][23];
95 239c2f4c Fabrice Bellard
/* XXX: free when all decoders are closed */
96 d04728bb Michael Niedermayer
#define TABLE_4_3_SIZE (8191 + 16)*4
97 9608652d Michael Niedermayer
static int8_t  table_4_3_exp[TABLE_4_3_SIZE];
98
static uint32_t table_4_3_value[TABLE_4_3_SIZE];
99 f8347c41 Michael Niedermayer
static uint32_t exp_table[512];
100 55017f35 Michael Niedermayer
static uint32_t expval_table[512][16];
101 239c2f4c Fabrice Bellard
/* intensity stereo coef table */
102 0c1a9eda Zdenek Kabelac
static int32_t is_table[2][16];
103
static int32_t is_table_lsf[2][2][16];
104 a1e257b2 Michael Niedermayer
static int32_t csa_table[8][4];
105
static float csa_table_float[8][4];
106 0c1a9eda Zdenek Kabelac
static int32_t mdct_win[8][36];
107 239c2f4c Fabrice Bellard
108
/* lower 2 bits: modulo 3, higher bits: shift */
109 0c1a9eda Zdenek Kabelac
static uint16_t scale_factor_modshift[64];
110 239c2f4c Fabrice Bellard
/* [i][j]:  2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
111 0c1a9eda Zdenek Kabelac
static int32_t scale_factor_mult[15][3];
112 239c2f4c Fabrice Bellard
/* mult table for layer 2 group quantization */
113
114
#define SCALE_GEN(v) \
115
{ FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
116
117 c26ae41d Michael Niedermayer
static const int32_t scale_factor_mult2[3][3] = {
118 81552334 Fabrice Bellard
    SCALE_GEN(4.0 / 3.0), /* 3 steps */
119
    SCALE_GEN(4.0 / 5.0), /* 5 steps */
120
    SCALE_GEN(4.0 / 9.0), /* 9 steps */
121 239c2f4c Fabrice Bellard
};
122
123 c6bcbb2c Reimar Döffinger
static DECLARE_ALIGNED_16(MPA_INT, window[512]);
124 115329f1 Diego Biurrun
125 9f7458b2 Michael Niedermayer
/**
126
 * Convert region offsets to region sizes and truncate
127
 * size to big_values.
128
 */
129
void ff_region_offset2size(GranuleDef *g){
130
    int i, k, j=0;
131
    g->region_size[2] = (576 / 2);
132
    for(i=0;i<3;i++) {
133
        k = FFMIN(g->region_size[i], g->big_values);
134
        g->region_size[i] = k - j;
135
        j = k;
136
    }
137
}
138
139
void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
140
    if (g->block_type == 2)
141
        g->region_size[0] = (36 / 2);
142
    else {
143
        if (s->sample_rate_index <= 2)
144
            g->region_size[0] = (36 / 2);
145
        else if (s->sample_rate_index != 8)
146
            g->region_size[0] = (54 / 2);
147
        else
148
            g->region_size[0] = (108 / 2);
149
    }
150
    g->region_size[1] = (576 / 2);
151
}
152
153
void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
154
    int l;
155
    g->region_size[0] =
156
        band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
157
    /* should not overflow */
158
    l = FFMIN(ra1 + ra2 + 2, 22);
159
    g->region_size[1] =
160
        band_index_long[s->sample_rate_index][l] >> 1;
161
}
162
163
void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
164
    if (g->block_type == 2) {
165
        if (g->switch_point) {
166
            /* if switched mode, we handle the 36 first samples as
167
                long blocks.  For 8000Hz, we handle the 48 first
168
                exponents as long blocks (XXX: check this!) */
169
            if (s->sample_rate_index <= 2)
170
                g->long_end = 8;
171
            else if (s->sample_rate_index != 8)
172
                g->long_end = 6;
173
            else
174
                g->long_end = 4; /* 8000 Hz */
175
176
            g->short_start = 2 + (s->sample_rate_index != 8);
177
        } else {
178
            g->long_end = 0;
179
            g->short_start = 0;
180
        }
181
    } else {
182
        g->short_start = 13;
183
        g->long_end = 22;
184
    }
185
}
186
187 239c2f4c Fabrice Bellard
/* layer 1 unscaling */
188
/* n = number of bits of the mantissa minus 1 */
189
static inline int l1_unscale(int n, int mant, int scale_factor)
190
{
191
    int shift, mod;
192 0c1a9eda Zdenek Kabelac
    int64_t val;
193 239c2f4c Fabrice Bellard
194
    shift = scale_factor_modshift[scale_factor];
195
    mod = shift & 3;
196
    shift >>= 2;
197
    val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
198
    shift += n;
199 81552334 Fabrice Bellard
    /* NOTE: at this point, 1 <= shift >= 21 + 15 */
200
    return (int)((val + (1LL << (shift - 1))) >> shift);
201 239c2f4c Fabrice Bellard
}
202
203
static inline int l2_unscale_group(int steps, int mant, int scale_factor)
204
{
205
    int shift, mod, val;
206
207
    shift = scale_factor_modshift[scale_factor];
208
    mod = shift & 3;
209
    shift >>= 2;
210 81552334 Fabrice Bellard
211
    val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
212
    /* NOTE: at this point, 0 <= shift <= 21 */
213
    if (shift > 0)
214
        val = (val + (1 << (shift - 1))) >> shift;
215
    return val;
216 239c2f4c Fabrice Bellard
}
217
218
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
219
static inline int l3_unscale(int value, int exponent)
220
{
221
    unsigned int m;
222
    int e;
223
224 d04728bb Michael Niedermayer
    e = table_4_3_exp  [4*value + (exponent&3)];
225
    m = table_4_3_value[4*value + (exponent&3)];
226
    e -= (exponent >> 2);
227
    assert(e>=1);
228 239c2f4c Fabrice Bellard
    if (e > 31)
229 b696d2a6 Michael Niedermayer
        return 0;
230 239c2f4c Fabrice Bellard
    m = (m + (1 << (e-1))) >> e;
231 d04728bb Michael Niedermayer
232 239c2f4c Fabrice Bellard
    return m;
233
}
234
235 f9ed4f88 Fabrice Bellard
/* all integer n^(4/3) computation code */
236
#define DEV_ORDER 13
237
238
#define POW_FRAC_BITS 24
239
#define POW_FRAC_ONE    (1 << POW_FRAC_BITS)
240
#define POW_FIX(a)   ((int)((a) * POW_FRAC_ONE))
241 0c1a9eda Zdenek Kabelac
#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
242 f9ed4f88 Fabrice Bellard
243
static int dev_4_3_coefs[DEV_ORDER];
244
245 88730be6 Måns Rullgård
#if 0 /* unused */
246 f9ed4f88 Fabrice Bellard
static int pow_mult3[3] = {
247
    POW_FIX(1.0),
248
    POW_FIX(1.25992104989487316476),
249
    POW_FIX(1.58740105196819947474),
250
};
251 88730be6 Måns Rullgård
#endif
252 f9ed4f88 Fabrice Bellard
253
static void int_pow_init(void)
254
{
255
    int i, a;
256
257
    a = POW_FIX(1.0);
258
    for(i=0;i<DEV_ORDER;i++) {
259
        a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
260
        dev_4_3_coefs[i] = a;
261
    }
262
}
263
264 88730be6 Måns Rullgård
#if 0 /* unused, remove? */
265 f9ed4f88 Fabrice Bellard
/* return the mantissa and the binary exponent */
266
static int int_pow(int i, int *exp_ptr)
267
{
268
    int e, er, eq, j;
269
    int a, a1;
270 115329f1 Diego Biurrun

271 f9ed4f88 Fabrice Bellard
    /* renormalize */
272
    a = i;
273
    e = POW_FRAC_BITS;
274
    while (a < (1 << (POW_FRAC_BITS - 1))) {
275
        a = a << 1;
276
        e--;
277
    }
278
    a -= (1 << POW_FRAC_BITS);
279
    a1 = 0;
280
    for(j = DEV_ORDER - 1; j >= 0; j--)
281
        a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
282
    a = (1 << POW_FRAC_BITS) + a1;
283
    /* exponent compute (exact) */
284
    e = e * 4;
285
    er = e % 3;
286
    eq = e / 3;
287
    a = POW_MULL(a, pow_mult3[er]);
288
    while (a >= 2 * POW_FRAC_ONE) {
289
        a = a >> 1;
290
        eq++;
291
    }
292
    /* convert to float */
293
    while (a < POW_FRAC_ONE) {
294
        a = a << 1;
295
        eq--;
296
    }
297 59d3e367 Fabrice Bellard
    /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
298 81552334 Fabrice Bellard
#if POW_FRAC_BITS > FRAC_BITS
299 59d3e367 Fabrice Bellard
    a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
300
    /* correct overflow */
301
    if (a >= 2 * (1 << FRAC_BITS)) {
302
        a = a >> 1;
303
        eq++;
304
    }
305
#endif
306 f9ed4f88 Fabrice Bellard
    *exp_ptr = eq;
307
    return a;
308
}
309 88730be6 Måns Rullgård
#endif
310 de6d9b64 Fabrice Bellard
311
static int decode_init(AVCodecContext * avctx)
312
{
313
    MPADecodeContext *s = avctx->priv_data;
314 b587a7cb Michael Niedermayer
    static int init=0;
315 239c2f4c Fabrice Bellard
    int i, j, k;
316 de6d9b64 Fabrice Bellard
317 318c5e05 Michel Bardiaux
    s->avctx = avctx;
318
319 49cdad8d Aurelien Jacobs
    avctx->sample_fmt= OUT_FMT;
320 047599a4 Michael Niedermayer
    s->error_recognition= avctx->error_recognition;
321 115329f1 Diego Biurrun
322 ce4a29c0 Michael Niedermayer
    if(avctx->antialias_algo != FF_AA_FLOAT)
323 ac806113 Michael Niedermayer
        s->compute_antialias= compute_antialias_integer;
324
    else
325
        s->compute_antialias= compute_antialias_float;
326
327 8c5b5683 Fabrice Bellard
    if (!init && !avctx->parse_only) {
328 bbdf8728 Art Clarke
        int offset;
329
330 239c2f4c Fabrice Bellard
        /* scale factors table for layer 1/2 */
331
        for(i=0;i<64;i++) {
332
            int shift, mod;
333
            /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
334 81552334 Fabrice Bellard
            shift = (i / 3);
335 239c2f4c Fabrice Bellard
            mod = i % 3;
336
            scale_factor_modshift[i] = mod | (shift << 2);
337
        }
338
339
        /* scale factor multiply for layer 1 */
340
        for(i=0;i<15;i++) {
341
            int n, norm;
342
            n = i + 2;
343 8da9266c Måns Rullgård
            norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
344 4deaa946 Måns Rullgård
            scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm, FRAC_BITS);
345
            scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm, FRAC_BITS);
346
            scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm, FRAC_BITS);
347 318c5e05 Michel Bardiaux
            dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
348 115329f1 Diego Biurrun
                    i, norm,
349 239c2f4c Fabrice Bellard
                    scale_factor_mult[i][0],
350
                    scale_factor_mult[i][1],
351
                    scale_factor_mult[i][2]);
352
        }
353 115329f1 Diego Biurrun
354 bb270c08 Diego Biurrun
        ff_mpa_synth_init(window);
355 115329f1 Diego Biurrun
356 239c2f4c Fabrice Bellard
        /* huffman decode tables */
357 bbdf8728 Art Clarke
        offset = 0;
358 239c2f4c Fabrice Bellard
        for(i=1;i<16;i++) {
359
            const HuffTable *h = &mpa_huff_tables[i];
360 bb270c08 Diego Biurrun
            int xsize, x, y;
361 5c91a675 Zdenek Kabelac
            unsigned int n;
362 9bffa9e7 Michael Niedermayer
            uint8_t  tmp_bits [512];
363
            uint16_t tmp_codes[512];
364 c7aa3696 Michael Niedermayer
365
            memset(tmp_bits , 0, sizeof(tmp_bits ));
366
            memset(tmp_codes, 0, sizeof(tmp_codes));
367 239c2f4c Fabrice Bellard
368
            xsize = h->xsize;
369
            n = xsize * xsize;
370 115329f1 Diego Biurrun
371 239c2f4c Fabrice Bellard
            j = 0;
372
            for(x=0;x<xsize;x++) {
373 c7aa3696 Michael Niedermayer
                for(y=0;y<xsize;y++){
374 9bffa9e7 Michael Niedermayer
                    tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j  ];
375
                    tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
376 c7aa3696 Michael Niedermayer
                }
377 239c2f4c Fabrice Bellard
            }
378 c7aa3696 Michael Niedermayer
379
            /* XXX: fail test */
380 bbdf8728 Art Clarke
            huff_vlc[i].table = huff_vlc_tables+offset;
381
            huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
382 9bffa9e7 Michael Niedermayer
            init_vlc(&huff_vlc[i], 7, 512,
383 bbdf8728 Art Clarke
                     tmp_bits, 1, 1, tmp_codes, 2, 2,
384
                     INIT_VLC_USE_NEW_STATIC);
385
            offset += huff_vlc_tables_sizes[i];
386 239c2f4c Fabrice Bellard
        }
387 37d3e066 Aurelien Jacobs
        assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
388 bbdf8728 Art Clarke
389
        offset = 0;
390 239c2f4c Fabrice Bellard
        for(i=0;i<2;i++) {
391 bbdf8728 Art Clarke
            huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
392
            huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
393 115329f1 Diego Biurrun
            init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
394 bbdf8728 Art Clarke
                     mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
395
                     INIT_VLC_USE_NEW_STATIC);
396
            offset += huff_quad_vlc_tables_sizes[i];
397 239c2f4c Fabrice Bellard
        }
398 37d3e066 Aurelien Jacobs
        assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
399 239c2f4c Fabrice Bellard
400
        for(i=0;i<9;i++) {
401
            k = 0;
402
            for(j=0;j<22;j++) {
403
                band_index_long[i][j] = k;
404
                k += band_size_long[i][j];
405
            }
406
            band_index_long[i][22] = k;
407
        }
408
409 bb270c08 Diego Biurrun
        /* compute n ^ (4/3) and store it in mantissa/exp format */
410 115329f1 Diego Biurrun
411 f9ed4f88 Fabrice Bellard
        int_pow_init();
412 239c2f4c Fabrice Bellard
        for(i=1;i<TABLE_4_3_SIZE;i++) {
413 d04728bb Michael Niedermayer
            double f, fm;
414 239c2f4c Fabrice Bellard
            int e, m;
415 d04728bb Michael Niedermayer
            f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
416
            fm = frexp(f, &e);
417 f86f4481 Michael Niedermayer
            m = (uint32_t)(fm*(1LL<<31) + 0.5);
418 bc2d2757 Michael Niedermayer
            e+= FRAC_BITS - 31 + 5 - 100;
419 d04728bb Michael Niedermayer
420 239c2f4c Fabrice Bellard
            /* normalized to FRAC_BITS */
421
            table_4_3_value[i] = m;
422 d04728bb Michael Niedermayer
            table_4_3_exp[i] = -e;
423 239c2f4c Fabrice Bellard
        }
424 55017f35 Michael Niedermayer
        for(i=0; i<512*16; i++){
425 bc2d2757 Michael Niedermayer
            int exponent= (i>>4);
426
            double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
427 39e1b5cf Michael Niedermayer
            expval_table[exponent][i&15]= llrint(f);
428 55017f35 Michael Niedermayer
            if((i&15)==1)
429 39e1b5cf Michael Niedermayer
                exp_table[exponent]= llrint(f);
430 f8347c41 Michael Niedermayer
        }
431 115329f1 Diego Biurrun
432 239c2f4c Fabrice Bellard
        for(i=0;i<7;i++) {
433
            float f;
434
            int v;
435
            if (i != 6) {
436
                f = tan((double)i * M_PI / 12.0);
437
                v = FIXR(f / (1.0 + f));
438
            } else {
439
                v = FIXR(1.0);
440
            }
441
            is_table[0][i] = v;
442
            is_table[1][6 - i] = v;
443
        }
444
        /* invalid values */
445
        for(i=7;i<16;i++)
446
            is_table[0][i] = is_table[1][i] = 0.0;
447
448
        for(i=0;i<16;i++) {
449
            double f;
450
            int e, k;
451
452
            for(j=0;j<2;j++) {
453
                e = -(j + 1) * ((i + 1) >> 1);
454
                f = pow(2.0, e / 4.0);
455
                k = i & 1;
456
                is_table_lsf[j][k ^ 1][i] = FIXR(f);
457
                is_table_lsf[j][k][i] = FIXR(1.0);
458 318c5e05 Michel Bardiaux
                dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
459 239c2f4c Fabrice Bellard
                        i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
460
            }
461
        }
462
463
        for(i=0;i<8;i++) {
464
            float ci, cs, ca;
465
            ci = ci_table[i];
466
            cs = 1.0 / sqrt(1.0 + ci * ci);
467
            ca = cs * ci;
468 ce4a29c0 Michael Niedermayer
            csa_table[i][0] = FIXHR(cs/4);
469
            csa_table[i][1] = FIXHR(ca/4);
470
            csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
471 115329f1 Diego Biurrun
            csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
472 a1e257b2 Michael Niedermayer
            csa_table_float[i][0] = cs;
473
            csa_table_float[i][1] = ca;
474
            csa_table_float[i][2] = ca + cs;
475 115329f1 Diego Biurrun
            csa_table_float[i][3] = ca - cs;
476 239c2f4c Fabrice Bellard
        }
477
478
        /* compute mdct windows */
479
        for(i=0;i<36;i++) {
480 711ae726 Michael Niedermayer
            for(j=0; j<4; j++){
481
                double d;
482 115329f1 Diego Biurrun
483 125d6246 Michael Niedermayer
                if(j==2 && i%3 != 1)
484
                    continue;
485 115329f1 Diego Biurrun
486 711ae726 Michael Niedermayer
                d= sin(M_PI * (i + 0.5) / 36.0);
487
                if(j==1){
488
                    if     (i>=30) d= 0;
489
                    else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
490
                    else if(i>=18) d= 1;
491
                }else if(j==3){
492
                    if     (i<  6) d= 0;
493
                    else if(i< 12) d= sin(M_PI * (i -  6 + 0.5) / 12.0);
494
                    else if(i< 18) d= 1;
495
                }
496
                //merge last stage of imdct into the window coefficients
497 125d6246 Michael Niedermayer
                d*= 0.5 / cos(M_PI*(2*i + 19)/72);
498
499
                if(j==2)
500
                    mdct_win[j][i/3] = FIXHR((d / (1<<5)));
501
                else
502
                    mdct_win[j][i  ] = FIXHR((d / (1<<5)));
503 711ae726 Michael Niedermayer
            }
504 239c2f4c Fabrice Bellard
        }
505
506
        /* NOTE: we do frequency inversion adter the MDCT by changing
507
           the sign of the right window coefs */
508
        for(j=0;j<4;j++) {
509
            for(i=0;i<36;i+=2) {
510
                mdct_win[j + 4][i] = mdct_win[j][i];
511
                mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
512
            }
513
        }
514
515 de6d9b64 Fabrice Bellard
        init = 1;
516
    }
517
518 1ede228a Roberto Togni
    if (avctx->codec_id == CODEC_ID_MP3ADU)
519
        s->adu_mode = 1;
520 de6d9b64 Fabrice Bellard
    return 0;
521
}
522
523 ef9f7306 Måns Rullgård
/* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
524 239c2f4c Fabrice Bellard
525
/* cos(i*pi/64) */
526
527 0bd2483a Michael Niedermayer
#define COS0_0  FIXHR(0.50060299823519630134/2)
528
#define COS0_1  FIXHR(0.50547095989754365998/2)
529
#define COS0_2  FIXHR(0.51544730992262454697/2)
530
#define COS0_3  FIXHR(0.53104259108978417447/2)
531
#define COS0_4  FIXHR(0.55310389603444452782/2)
532
#define COS0_5  FIXHR(0.58293496820613387367/2)
533
#define COS0_6  FIXHR(0.62250412303566481615/2)
534
#define COS0_7  FIXHR(0.67480834145500574602/2)
535
#define COS0_8  FIXHR(0.74453627100229844977/2)
536
#define COS0_9  FIXHR(0.83934964541552703873/2)
537
#define COS0_10 FIXHR(0.97256823786196069369/2)
538
#define COS0_11 FIXHR(1.16943993343288495515/4)
539
#define COS0_12 FIXHR(1.48416461631416627724/4)
540
#define COS0_13 FIXHR(2.05778100995341155085/8)
541
#define COS0_14 FIXHR(3.40760841846871878570/8)
542
#define COS0_15 FIXHR(10.19000812354805681150/32)
543
544
#define COS1_0 FIXHR(0.50241928618815570551/2)
545
#define COS1_1 FIXHR(0.52249861493968888062/2)
546
#define COS1_2 FIXHR(0.56694403481635770368/2)
547
#define COS1_3 FIXHR(0.64682178335999012954/2)
548
#define COS1_4 FIXHR(0.78815462345125022473/2)
549
#define COS1_5 FIXHR(1.06067768599034747134/4)
550
#define COS1_6 FIXHR(1.72244709823833392782/4)
551
#define COS1_7 FIXHR(5.10114861868916385802/16)
552
553
#define COS2_0 FIXHR(0.50979557910415916894/2)
554
#define COS2_1 FIXHR(0.60134488693504528054/2)
555
#define COS2_2 FIXHR(0.89997622313641570463/2)
556
#define COS2_3 FIXHR(2.56291544774150617881/8)
557
558
#define COS3_0 FIXHR(0.54119610014619698439/2)
559
#define COS3_1 FIXHR(1.30656296487637652785/4)
560
561
#define COS4_0 FIXHR(0.70710678118654752439/2)
562 239c2f4c Fabrice Bellard
563
/* butterfly operator */
564 0bd2483a Michael Niedermayer
#define BF(a, b, c, s)\
565 239c2f4c Fabrice Bellard
{\
566
    tmp0 = tab[a] + tab[b];\
567
    tmp1 = tab[a] - tab[b];\
568
    tab[a] = tmp0;\
569 0bd2483a Michael Niedermayer
    tab[b] = MULH(tmp1<<(s), c);\
570 239c2f4c Fabrice Bellard
}
571
572
#define BF1(a, b, c, d)\
573
{\
574 0bd2483a Michael Niedermayer
    BF(a, b, COS4_0, 1);\
575
    BF(c, d,-COS4_0, 1);\
576 239c2f4c Fabrice Bellard
    tab[c] += tab[d];\
577
}
578
579
#define BF2(a, b, c, d)\
580
{\
581 0bd2483a Michael Niedermayer
    BF(a, b, COS4_0, 1);\
582
    BF(c, d,-COS4_0, 1);\
583 239c2f4c Fabrice Bellard
    tab[c] += tab[d];\
584
    tab[a] += tab[c];\
585
    tab[c] += tab[b];\
586
    tab[b] += tab[d];\
587
}
588
589
#define ADD(a, b) tab[a] += tab[b]
590
591
/* DCT32 without 1/sqrt(2) coef zero scaling. */
592 0c1a9eda Zdenek Kabelac
static void dct32(int32_t *out, int32_t *tab)
593 239c2f4c Fabrice Bellard
{
594
    int tmp0, tmp1;
595
596
    /* pass 1 */
597 0bd2483a Michael Niedermayer
    BF( 0, 31, COS0_0 , 1);
598
    BF(15, 16, COS0_15, 5);
599 120aad7f Michael Niedermayer
    /* pass 2 */
600 0bd2483a Michael Niedermayer
    BF( 0, 15, COS1_0 , 1);
601
    BF(16, 31,-COS1_0 , 1);
602 120aad7f Michael Niedermayer
    /* pass 1 */
603 0bd2483a Michael Niedermayer
    BF( 7, 24, COS0_7 , 1);
604
    BF( 8, 23, COS0_8 , 1);
605 120aad7f Michael Niedermayer
    /* pass 2 */
606 0bd2483a Michael Niedermayer
    BF( 7,  8, COS1_7 , 4);
607
    BF(23, 24,-COS1_7 , 4);
608 120aad7f Michael Niedermayer
    /* pass 3 */
609 0bd2483a Michael Niedermayer
    BF( 0,  7, COS2_0 , 1);
610
    BF( 8, 15,-COS2_0 , 1);
611
    BF(16, 23, COS2_0 , 1);
612
    BF(24, 31,-COS2_0 , 1);
613 120aad7f Michael Niedermayer
    /* pass 1 */
614 0bd2483a Michael Niedermayer
    BF( 3, 28, COS0_3 , 1);
615
    BF(12, 19, COS0_12, 2);
616 239c2f4c Fabrice Bellard
    /* pass 2 */
617 0bd2483a Michael Niedermayer
    BF( 3, 12, COS1_3 , 1);
618
    BF(19, 28,-COS1_3 , 1);
619 120aad7f Michael Niedermayer
    /* pass 1 */
620 0bd2483a Michael Niedermayer
    BF( 4, 27, COS0_4 , 1);
621
    BF(11, 20, COS0_11, 2);
622 120aad7f Michael Niedermayer
    /* pass 2 */
623 0bd2483a Michael Niedermayer
    BF( 4, 11, COS1_4 , 1);
624
    BF(20, 27,-COS1_4 , 1);
625 120aad7f Michael Niedermayer
    /* pass 3 */
626 0bd2483a Michael Niedermayer
    BF( 3,  4, COS2_3 , 3);
627
    BF(11, 12,-COS2_3 , 3);
628
    BF(19, 20, COS2_3 , 3);
629
    BF(27, 28,-COS2_3 , 3);
630 120aad7f Michael Niedermayer
    /* pass 4 */
631 0bd2483a Michael Niedermayer
    BF( 0,  3, COS3_0 , 1);
632
    BF( 4,  7,-COS3_0 , 1);
633
    BF( 8, 11, COS3_0 , 1);
634
    BF(12, 15,-COS3_0 , 1);
635
    BF(16, 19, COS3_0 , 1);
636
    BF(20, 23,-COS3_0 , 1);
637
    BF(24, 27, COS3_0 , 1);
638
    BF(28, 31,-COS3_0 , 1);
639 115329f1 Diego Biurrun
640 120aad7f Michael Niedermayer
641
642
    /* pass 1 */
643 0bd2483a Michael Niedermayer
    BF( 1, 30, COS0_1 , 1);
644
    BF(14, 17, COS0_14, 3);
645 120aad7f Michael Niedermayer
    /* pass 2 */
646 0bd2483a Michael Niedermayer
    BF( 1, 14, COS1_1 , 1);
647
    BF(17, 30,-COS1_1 , 1);
648 120aad7f Michael Niedermayer
    /* pass 1 */
649 0bd2483a Michael Niedermayer
    BF( 6, 25, COS0_6 , 1);
650
    BF( 9, 22, COS0_9 , 1);
651 120aad7f Michael Niedermayer
    /* pass 2 */
652 0bd2483a Michael Niedermayer
    BF( 6,  9, COS1_6 , 2);
653
    BF(22, 25,-COS1_6 , 2);
654 239c2f4c Fabrice Bellard
    /* pass 3 */
655 0bd2483a Michael Niedermayer
    BF( 1,  6, COS2_1 , 1);
656
    BF( 9, 14,-COS2_1 , 1);
657
    BF(17, 22, COS2_1 , 1);
658
    BF(25, 30,-COS2_1 , 1);
659 239c2f4c Fabrice Bellard
660 120aad7f Michael Niedermayer
    /* pass 1 */
661 0bd2483a Michael Niedermayer
    BF( 2, 29, COS0_2 , 1);
662
    BF(13, 18, COS0_13, 3);
663 120aad7f Michael Niedermayer
    /* pass 2 */
664 0bd2483a Michael Niedermayer
    BF( 2, 13, COS1_2 , 1);
665
    BF(18, 29,-COS1_2 , 1);
666 120aad7f Michael Niedermayer
    /* pass 1 */
667 0bd2483a Michael Niedermayer
    BF( 5, 26, COS0_5 , 1);
668
    BF(10, 21, COS0_10, 1);
669 120aad7f Michael Niedermayer
    /* pass 2 */
670 0bd2483a Michael Niedermayer
    BF( 5, 10, COS1_5 , 2);
671
    BF(21, 26,-COS1_5 , 2);
672 120aad7f Michael Niedermayer
    /* pass 3 */
673 0bd2483a Michael Niedermayer
    BF( 2,  5, COS2_2 , 1);
674
    BF(10, 13,-COS2_2 , 1);
675
    BF(18, 21, COS2_2 , 1);
676
    BF(26, 29,-COS2_2 , 1);
677 239c2f4c Fabrice Bellard
    /* pass 4 */
678 0bd2483a Michael Niedermayer
    BF( 1,  2, COS3_1 , 2);
679
    BF( 5,  6,-COS3_1 , 2);
680
    BF( 9, 10, COS3_1 , 2);
681
    BF(13, 14,-COS3_1 , 2);
682
    BF(17, 18, COS3_1 , 2);
683
    BF(21, 22,-COS3_1 , 2);
684
    BF(25, 26, COS3_1 , 2);
685
    BF(29, 30,-COS3_1 , 2);
686 115329f1 Diego Biurrun
687 239c2f4c Fabrice Bellard
    /* pass 5 */
688 0bd2483a Michael Niedermayer
    BF1( 0,  1,  2,  3);
689
    BF2( 4,  5,  6,  7);
690
    BF1( 8,  9, 10, 11);
691 239c2f4c Fabrice Bellard
    BF2(12, 13, 14, 15);
692
    BF1(16, 17, 18, 19);
693
    BF2(20, 21, 22, 23);
694
    BF1(24, 25, 26, 27);
695
    BF2(28, 29, 30, 31);
696 115329f1 Diego Biurrun
697 239c2f4c Fabrice Bellard
    /* pass 6 */
698 115329f1 Diego Biurrun
699 239c2f4c Fabrice Bellard
    ADD( 8, 12);
700
    ADD(12, 10);
701
    ADD(10, 14);
702
    ADD(14,  9);
703
    ADD( 9, 13);
704
    ADD(13, 11);
705
    ADD(11, 15);
706
707
    out[ 0] = tab[0];
708
    out[16] = tab[1];
709
    out[ 8] = tab[2];
710
    out[24] = tab[3];
711
    out[ 4] = tab[4];
712
    out[20] = tab[5];
713
    out[12] = tab[6];
714
    out[28] = tab[7];
715
    out[ 2] = tab[8];
716
    out[18] = tab[9];
717
    out[10] = tab[10];
718
    out[26] = tab[11];
719
    out[ 6] = tab[12];
720
    out[22] = tab[13];
721
    out[14] = tab[14];
722
    out[30] = tab[15];
723 115329f1 Diego Biurrun
724 239c2f4c Fabrice Bellard
    ADD(24, 28);
725
    ADD(28, 26);
726
    ADD(26, 30);
727
    ADD(30, 25);
728
    ADD(25, 29);
729
    ADD(29, 27);
730
    ADD(27, 31);
731
732
    out[ 1] = tab[16] + tab[24];
733
    out[17] = tab[17] + tab[25];
734
    out[ 9] = tab[18] + tab[26];
735
    out[25] = tab[19] + tab[27];
736
    out[ 5] = tab[20] + tab[28];
737
    out[21] = tab[21] + tab[29];
738
    out[13] = tab[22] + tab[30];
739
    out[29] = tab[23] + tab[31];
740
    out[ 3] = tab[24] + tab[20];
741
    out[19] = tab[25] + tab[21];
742
    out[11] = tab[26] + tab[22];
743
    out[27] = tab[27] + tab[23];
744
    out[ 7] = tab[28] + tab[18];
745
    out[23] = tab[29] + tab[19];
746
    out[15] = tab[30] + tab[17];
747
    out[31] = tab[31];
748
}
749
750
#if FRAC_BITS <= 15
751
752 a7a85899 Michael Niedermayer
static inline int round_sample(int *sum)
753 8c5b5683 Fabrice Bellard
{
754
    int sum1;
755 a7a85899 Michael Niedermayer
    sum1 = (*sum) >> OUT_SHIFT;
756
    *sum &= (1<<OUT_SHIFT)-1;
757 a3a5f4d6 Michael Niedermayer
    if (sum1 < OUT_MIN)
758
        sum1 = OUT_MIN;
759
    else if (sum1 > OUT_MAX)
760
        sum1 = OUT_MAX;
761 8c5b5683 Fabrice Bellard
    return sum1;
762 239c2f4c Fabrice Bellard
}
763
764 004390a6 Luca Barbato
/* signed 16x16 -> 32 multiply add accumulate */
765
#define MACS(rt, ra, rb) MAC16(rt, ra, rb)
766 024da4b1 Siarhei Siamashka
767 004390a6 Luca Barbato
/* signed 16x16 -> 32 multiply */
768
#define MULS(ra, rb) MUL16(ra, rb)
769 5ab1972b Michael Niedermayer
770 9ea583e1 Måns Rullgård
#define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
771
772 8c5b5683 Fabrice Bellard
#else
773
774 115329f1 Diego Biurrun
static inline int round_sample(int64_t *sum)
775 8c5b5683 Fabrice Bellard
{
776
    int sum1;
777 a7a85899 Michael Niedermayer
    sum1 = (int)((*sum) >> OUT_SHIFT);
778
    *sum &= (1<<OUT_SHIFT)-1;
779 a3a5f4d6 Michael Niedermayer
    if (sum1 < OUT_MIN)
780
        sum1 = OUT_MIN;
781
    else if (sum1 > OUT_MAX)
782
        sum1 = OUT_MAX;
783 8c5b5683 Fabrice Bellard
    return sum1;
784 239c2f4c Fabrice Bellard
}
785
786 47a0cd74 Michael Niedermayer
#   define MULS(ra, rb) MUL64(ra, rb)
787 9ea583e1 Måns Rullgård
#   define MACS(rt, ra, rb) MAC64(rt, ra, rb)
788
#   define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
789 8c5b5683 Fabrice Bellard
#endif
790
791 9ea583e1 Måns Rullgård
#define SUM8(op, sum, w, p)               \
792
{                                         \
793
    op(sum, (w)[0 * 64], p[0 * 64]);      \
794
    op(sum, (w)[1 * 64], p[1 * 64]);      \
795
    op(sum, (w)[2 * 64], p[2 * 64]);      \
796
    op(sum, (w)[3 * 64], p[3 * 64]);      \
797
    op(sum, (w)[4 * 64], p[4 * 64]);      \
798
    op(sum, (w)[5 * 64], p[5 * 64]);      \
799
    op(sum, (w)[6 * 64], p[6 * 64]);      \
800
    op(sum, (w)[7 * 64], p[7 * 64]);      \
801 8c5b5683 Fabrice Bellard
}
802
803
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
804
{                                               \
805
    int tmp;\
806
    tmp = p[0 * 64];\
807 9ea583e1 Måns Rullgård
    op1(sum1, (w1)[0 * 64], tmp);\
808
    op2(sum2, (w2)[0 * 64], tmp);\
809 8c5b5683 Fabrice Bellard
    tmp = p[1 * 64];\
810 9ea583e1 Måns Rullgård
    op1(sum1, (w1)[1 * 64], tmp);\
811
    op2(sum2, (w2)[1 * 64], tmp);\
812 8c5b5683 Fabrice Bellard
    tmp = p[2 * 64];\
813 9ea583e1 Måns Rullgård
    op1(sum1, (w1)[2 * 64], tmp);\
814
    op2(sum2, (w2)[2 * 64], tmp);\
815 8c5b5683 Fabrice Bellard
    tmp = p[3 * 64];\
816 9ea583e1 Måns Rullgård
    op1(sum1, (w1)[3 * 64], tmp);\
817
    op2(sum2, (w2)[3 * 64], tmp);\
818 8c5b5683 Fabrice Bellard
    tmp = p[4 * 64];\
819 9ea583e1 Måns Rullgård
    op1(sum1, (w1)[4 * 64], tmp);\
820
    op2(sum2, (w2)[4 * 64], tmp);\
821 8c5b5683 Fabrice Bellard
    tmp = p[5 * 64];\
822 9ea583e1 Måns Rullgård
    op1(sum1, (w1)[5 * 64], tmp);\
823
    op2(sum2, (w2)[5 * 64], tmp);\
824 8c5b5683 Fabrice Bellard
    tmp = p[6 * 64];\
825 9ea583e1 Måns Rullgård
    op1(sum1, (w1)[6 * 64], tmp);\
826
    op2(sum2, (w2)[6 * 64], tmp);\
827 8c5b5683 Fabrice Bellard
    tmp = p[7 * 64];\
828 9ea583e1 Måns Rullgård
    op1(sum1, (w1)[7 * 64], tmp);\
829
    op2(sum2, (w2)[7 * 64], tmp);\
830 239c2f4c Fabrice Bellard
}
831
832 bf1f4da0 Alex Beregszaszi
void ff_mpa_synth_init(MPA_INT *window)
833
{
834
    int i;
835
836
    /* max = 18760, max sum over all 16 coefs : 44736 */
837
    for(i=0;i<257;i++) {
838
        int v;
839 677fe2e2 Aurelien Jacobs
        v = ff_mpa_enwindow[i];
840 bf1f4da0 Alex Beregszaszi
#if WFRAC_BITS < 16
841
        v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
842
#endif
843
        window[i] = v;
844
        if ((i & 63) != 0)
845
            v = -v;
846
        if (i != 0)
847
            window[512 - i] = v;
848 115329f1 Diego Biurrun
    }
849 bf1f4da0 Alex Beregszaszi
}
850 239c2f4c Fabrice Bellard
851
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
852
   32 samples. */
853
/* XXX: optimize by avoiding ring buffer usage */
854 bf1f4da0 Alex Beregszaszi
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
855 bb270c08 Diego Biurrun
                         MPA_INT *window, int *dither_state,
856 115329f1 Diego Biurrun
                         OUT_INT *samples, int incr,
857 0c1a9eda Zdenek Kabelac
                         int32_t sb_samples[SBLIMIT])
858 239c2f4c Fabrice Bellard
{
859 0c1a9eda Zdenek Kabelac
    int32_t tmp[32];
860 8c5b5683 Fabrice Bellard
    register MPA_INT *synth_buf;
861 491c4a10 Alex Beregszaszi
    register const MPA_INT *w, *w2, *p;
862 239c2f4c Fabrice Bellard
    int j, offset, v;
863 a3a5f4d6 Michael Niedermayer
    OUT_INT *samples2;
864 239c2f4c Fabrice Bellard
#if FRAC_BITS <= 15
865 8c5b5683 Fabrice Bellard
    int sum, sum2;
866 239c2f4c Fabrice Bellard
#else
867 8c5b5683 Fabrice Bellard
    int64_t sum, sum2;
868 239c2f4c Fabrice Bellard
#endif
869 bf1f4da0 Alex Beregszaszi
870 239c2f4c Fabrice Bellard
    dct32(tmp, sb_samples);
871 115329f1 Diego Biurrun
872 bf1f4da0 Alex Beregszaszi
    offset = *synth_buf_offset;
873
    synth_buf = synth_buf_ptr + offset;
874 239c2f4c Fabrice Bellard
875
    for(j=0;j<32;j++) {
876
        v = tmp[j];
877
#if FRAC_BITS <= 15
878 81552334 Fabrice Bellard
        /* NOTE: can cause a loss in precision if very high amplitude
879
           sound */
880 aee481ce Aurelien Jacobs
        v = av_clip_int16(v);
881 239c2f4c Fabrice Bellard
#endif
882
        synth_buf[j] = v;
883
    }
884
    /* copy to avoid wrap */
885
    memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
886
887 8c5b5683 Fabrice Bellard
    samples2 = samples + 31 * incr;
888 239c2f4c Fabrice Bellard
    w = window;
889 8c5b5683 Fabrice Bellard
    w2 = window + 31;
890
891 093c6e50 Michael Niedermayer
    sum = *dither_state;
892 8c5b5683 Fabrice Bellard
    p = synth_buf + 16;
893 9ea583e1 Måns Rullgård
    SUM8(MACS, sum, w, p);
894 8c5b5683 Fabrice Bellard
    p = synth_buf + 48;
895 9ea583e1 Måns Rullgård
    SUM8(MLSS, sum, w + 32, p);
896 a7a85899 Michael Niedermayer
    *samples = round_sample(&sum);
897 8c5b5683 Fabrice Bellard
    samples += incr;
898 239c2f4c Fabrice Bellard
    w++;
899
900 8c5b5683 Fabrice Bellard
    /* we calculate two samples at the same time to avoid one memory
901
       access per two sample */
902
    for(j=1;j<16;j++) {
903
        sum2 = 0;
904
        p = synth_buf + 16 + j;
905 9ea583e1 Måns Rullgård
        SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
906 8c5b5683 Fabrice Bellard
        p = synth_buf + 48 - j;
907 9ea583e1 Måns Rullgård
        SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
908 8c5b5683 Fabrice Bellard
909 a7a85899 Michael Niedermayer
        *samples = round_sample(&sum);
910 8c5b5683 Fabrice Bellard
        samples += incr;
911 a7a85899 Michael Niedermayer
        sum += sum2;
912
        *samples2 = round_sample(&sum);
913 8c5b5683 Fabrice Bellard
        samples2 -= incr;
914 239c2f4c Fabrice Bellard
        w++;
915 8c5b5683 Fabrice Bellard
        w2--;
916 239c2f4c Fabrice Bellard
    }
917 115329f1 Diego Biurrun
918 8c5b5683 Fabrice Bellard
    p = synth_buf + 32;
919 9ea583e1 Måns Rullgård
    SUM8(MLSS, sum, w + 32, p);
920 a7a85899 Michael Niedermayer
    *samples = round_sample(&sum);
921 093c6e50 Michael Niedermayer
    *dither_state= sum;
922 8c5b5683 Fabrice Bellard
923 239c2f4c Fabrice Bellard
    offset = (offset - 32) & 511;
924 bf1f4da0 Alex Beregszaszi
    *synth_buf_offset = offset;
925 239c2f4c Fabrice Bellard
}
926
927 125d6246 Michael Niedermayer
#define C3 FIXHR(0.86602540378443864676/2)
928
929
/* 0.5 / cos(pi*(2*i+1)/36) */
930
static const int icos36[9] = {
931
    FIXR(0.50190991877167369479),
932
    FIXR(0.51763809020504152469), //0
933
    FIXR(0.55168895948124587824),
934
    FIXR(0.61038729438072803416),
935
    FIXR(0.70710678118654752439), //1
936
    FIXR(0.87172339781054900991),
937
    FIXR(1.18310079157624925896),
938
    FIXR(1.93185165257813657349), //2
939
    FIXR(5.73685662283492756461),
940
};
941 239c2f4c Fabrice Bellard
942 eb644776 Michael Niedermayer
/* 0.5 / cos(pi*(2*i+1)/36) */
943
static const int icos36h[9] = {
944
    FIXHR(0.50190991877167369479/2),
945
    FIXHR(0.51763809020504152469/2), //0
946
    FIXHR(0.55168895948124587824/2),
947
    FIXHR(0.61038729438072803416/2),
948
    FIXHR(0.70710678118654752439/2), //1
949
    FIXHR(0.87172339781054900991/2),
950
    FIXHR(1.18310079157624925896/4),
951
    FIXHR(1.93185165257813657349/4), //2
952
//    FIXHR(5.73685662283492756461),
953
};
954
955 239c2f4c Fabrice Bellard
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
956
   cases. */
957
static void imdct12(int *out, int *in)
958
{
959 125d6246 Michael Niedermayer
    int in0, in1, in2, in3, in4, in5, t1, t2;
960 44f1698a Michael Niedermayer
961
    in0= in[0*3];
962
    in1= in[1*3] + in[0*3];
963
    in2= in[2*3] + in[1*3];
964
    in3= in[3*3] + in[2*3];
965
    in4= in[4*3] + in[3*3];
966
    in5= in[5*3] + in[4*3];
967 125d6246 Michael Niedermayer
    in5 += in3;
968
    in3 += in1;
969
970
    in2= MULH(2*in2, C3);
971 eb644776 Michael Niedermayer
    in3= MULH(4*in3, C3);
972 115329f1 Diego Biurrun
973 125d6246 Michael Niedermayer
    t1 = in0 - in4;
974 eb644776 Michael Niedermayer
    t2 = MULH(2*(in1 - in5), icos36h[4]);
975 125d6246 Michael Niedermayer
976 115329f1 Diego Biurrun
    out[ 7]=
977 125d6246 Michael Niedermayer
    out[10]= t1 + t2;
978
    out[ 1]=
979
    out[ 4]= t1 - t2;
980
981
    in0 += in4>>1;
982
    in4 = in0 + in2;
983 eb644776 Michael Niedermayer
    in5 += 2*in1;
984
    in1 = MULH(in5 + in3, icos36h[1]);
985 115329f1 Diego Biurrun
    out[ 8]=
986 eb644776 Michael Niedermayer
    out[ 9]= in4 + in1;
987 125d6246 Michael Niedermayer
    out[ 2]=
988 eb644776 Michael Niedermayer
    out[ 3]= in4 - in1;
989 115329f1 Diego Biurrun
990 125d6246 Michael Niedermayer
    in0 -= in2;
991 eb644776 Michael Niedermayer
    in5 = MULH(2*(in5 - in3), icos36h[7]);
992 125d6246 Michael Niedermayer
    out[ 0]=
993 eb644776 Michael Niedermayer
    out[ 5]= in0 - in5;
994 125d6246 Michael Niedermayer
    out[ 6]=
995 eb644776 Michael Niedermayer
    out[11]= in0 + in5;
996 239c2f4c Fabrice Bellard
}
997
998
/* cos(pi*i/18) */
999 711ae726 Michael Niedermayer
#define C1 FIXHR(0.98480775301220805936/2)
1000
#define C2 FIXHR(0.93969262078590838405/2)
1001
#define C3 FIXHR(0.86602540378443864676/2)
1002
#define C4 FIXHR(0.76604444311897803520/2)
1003
#define C5 FIXHR(0.64278760968653932632/2)
1004
#define C6 FIXHR(0.5/2)
1005
#define C7 FIXHR(0.34202014332566873304/2)
1006
#define C8 FIXHR(0.17364817766693034885/2)
1007
1008 239c2f4c Fabrice Bellard
1009
/* using Lee like decomposition followed by hand coded 9 points DCT */
1010 711ae726 Michael Niedermayer
static void imdct36(int *out, int *buf, int *in, int *win)
1011 239c2f4c Fabrice Bellard
{
1012
    int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1013
    int tmp[18], *tmp1, *in1;
1014
1015
    for(i=17;i>=1;i--)
1016
        in[i] += in[i-1];
1017
    for(i=17;i>=3;i-=2)
1018
        in[i] += in[i-2];
1019
1020
    for(j=0;j<2;j++) {
1021
        tmp1 = tmp + j;
1022
        in1 = in + j;
1023 711ae726 Michael Niedermayer
#if 0
1024
//more accurate but slower
1025
        int64_t t0, t1, t2, t3;
1026
        t2 = in1[2*4] + in1[2*8] - in1[2*2];
1027 115329f1 Diego Biurrun

1028 711ae726 Michael Niedermayer
        t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1029
        t1 = in1[2*0] - in1[2*6];
1030
        tmp1[ 6] = t1 - (t2>>1);
1031
        tmp1[16] = t1 + t2;
1032

1033
        t0 = MUL64(2*(in1[2*2] + in1[2*4]),    C2);
1034
        t1 = MUL64(   in1[2*4] - in1[2*8] , -2*C8);
1035
        t2 = MUL64(2*(in1[2*2] + in1[2*8]),   -C4);
1036 115329f1 Diego Biurrun

1037 711ae726 Michael Niedermayer
        tmp1[10] = (t3 - t0 - t2) >> 32;
1038
        tmp1[ 2] = (t3 + t0 + t1) >> 32;
1039
        tmp1[14] = (t3 + t2 - t1) >> 32;
1040 115329f1 Diego Biurrun

1041 711ae726 Michael Niedermayer
        tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1042
        t2 = MUL64(2*(in1[2*1] + in1[2*5]),    C1);
1043
        t3 = MUL64(   in1[2*5] - in1[2*7] , -2*C7);
1044
        t0 = MUL64(2*in1[2*3], C3);
1045

1046
        t1 = MUL64(2*(in1[2*1] + in1[2*7]),   -C5);
1047

1048
        tmp1[ 0] = (t2 + t3 + t0) >> 32;
1049
        tmp1[12] = (t2 + t1 - t0) >> 32;
1050
        tmp1[ 8] = (t3 - t1 - t0) >> 32;
1051
#else
1052
        t2 = in1[2*4] + in1[2*8] - in1[2*2];
1053 115329f1 Diego Biurrun
1054 711ae726 Michael Niedermayer
        t3 = in1[2*0] + (in1[2*6]>>1);
1055
        t1 = in1[2*0] - in1[2*6];
1056
        tmp1[ 6] = t1 - (t2>>1);
1057
        tmp1[16] = t1 + t2;
1058
1059
        t0 = MULH(2*(in1[2*2] + in1[2*4]),    C2);
1060
        t1 = MULH(   in1[2*4] - in1[2*8] , -2*C8);
1061
        t2 = MULH(2*(in1[2*2] + in1[2*8]),   -C4);
1062 115329f1 Diego Biurrun
1063 711ae726 Michael Niedermayer
        tmp1[10] = t3 - t0 - t2;
1064
        tmp1[ 2] = t3 + t0 + t1;
1065
        tmp1[14] = t3 + t2 - t1;
1066 115329f1 Diego Biurrun
1067 711ae726 Michael Niedermayer
        tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1068
        t2 = MULH(2*(in1[2*1] + in1[2*5]),    C1);
1069
        t3 = MULH(   in1[2*5] - in1[2*7] , -2*C7);
1070
        t0 = MULH(2*in1[2*3], C3);
1071 239c2f4c Fabrice Bellard
1072 711ae726 Michael Niedermayer
        t1 = MULH(2*(in1[2*1] + in1[2*7]),   -C5);
1073
1074
        tmp1[ 0] = t2 + t3 + t0;
1075
        tmp1[12] = t2 + t1 - t0;
1076
        tmp1[ 8] = t3 - t1 - t0;
1077
#endif
1078 239c2f4c Fabrice Bellard
    }
1079
1080
    i = 0;
1081
    for(j=0;j<4;j++) {
1082
        t0 = tmp[i];
1083
        t1 = tmp[i + 2];
1084
        s0 = t1 + t0;
1085
        s2 = t1 - t0;
1086
1087
        t2 = tmp[i + 1];
1088
        t3 = tmp[i + 3];
1089 eb644776 Michael Niedermayer
        s1 = MULH(2*(t3 + t2), icos36h[j]);
1090 4deaa946 Måns Rullgård
        s3 = MULL(t3 - t2, icos36[8 - j], FRAC_BITS);
1091 115329f1 Diego Biurrun
1092 44f1698a Michael Niedermayer
        t0 = s0 + s1;
1093
        t1 = s0 - s1;
1094 125d6246 Michael Niedermayer
        out[(9 + j)*SBLIMIT] =  MULH(t1, win[9 + j]) + buf[9 + j];
1095 711ae726 Michael Niedermayer
        out[(8 - j)*SBLIMIT] =  MULH(t1, win[8 - j]) + buf[8 - j];
1096
        buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1097
        buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1098 115329f1 Diego Biurrun
1099 44f1698a Michael Niedermayer
        t0 = s2 + s3;
1100
        t1 = s2 - s3;
1101 125d6246 Michael Niedermayer
        out[(9 + 8 - j)*SBLIMIT] =  MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1102 711ae726 Michael Niedermayer
        out[(        j)*SBLIMIT] =  MULH(t1, win[        j]) + buf[        j];
1103
        buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1104
        buf[      + j] = MULH(t0, win[18         + j]);
1105 239c2f4c Fabrice Bellard
        i += 4;
1106
    }
1107
1108
    s0 = tmp[16];
1109 eb644776 Michael Niedermayer
    s1 = MULH(2*tmp[17], icos36h[4]);
1110 44f1698a Michael Niedermayer
    t0 = s0 + s1;
1111
    t1 = s0 - s1;
1112 125d6246 Michael Niedermayer
    out[(9 + 4)*SBLIMIT] =  MULH(t1, win[9 + 4]) + buf[9 + 4];
1113 711ae726 Michael Niedermayer
    out[(8 - 4)*SBLIMIT] =  MULH(t1, win[8 - 4]) + buf[8 - 4];
1114
    buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1115
    buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1116 239c2f4c Fabrice Bellard
}
1117
1118
/* return the number of decoded frames */
1119
static int mp_decode_layer1(MPADecodeContext *s)
1120 de6d9b64 Fabrice Bellard
{
1121 239c2f4c Fabrice Bellard
    int bound, i, v, n, ch, j, mant;
1122 0c1a9eda Zdenek Kabelac
    uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1123
    uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1124 239c2f4c Fabrice Bellard
1125 115329f1 Diego Biurrun
    if (s->mode == MPA_JSTEREO)
1126 239c2f4c Fabrice Bellard
        bound = (s->mode_ext + 1) * 4;
1127
    else
1128
        bound = SBLIMIT;
1129
1130
    /* allocation bits */
1131
    for(i=0;i<bound;i++) {
1132
        for(ch=0;ch<s->nb_channels;ch++) {
1133
            allocation[ch][i] = get_bits(&s->gb, 4);
1134
        }
1135
    }
1136
    for(i=bound;i<SBLIMIT;i++) {
1137
        allocation[0][i] = get_bits(&s->gb, 4);
1138
    }
1139
1140
    /* scale factors */
1141
    for(i=0;i<bound;i++) {
1142
        for(ch=0;ch<s->nb_channels;ch++) {
1143
            if (allocation[ch][i])
1144
                scale_factors[ch][i] = get_bits(&s->gb, 6);
1145
        }
1146
    }
1147
    for(i=bound;i<SBLIMIT;i++) {
1148
        if (allocation[0][i]) {
1149
            scale_factors[0][i] = get_bits(&s->gb, 6);
1150
            scale_factors[1][i] = get_bits(&s->gb, 6);
1151
        }
1152
    }
1153 115329f1 Diego Biurrun
1154 239c2f4c Fabrice Bellard
    /* compute samples */
1155
    for(j=0;j<12;j++) {
1156
        for(i=0;i<bound;i++) {
1157
            for(ch=0;ch<s->nb_channels;ch++) {
1158
                n = allocation[ch][i];
1159
                if (n) {
1160
                    mant = get_bits(&s->gb, n + 1);
1161
                    v = l1_unscale(n, mant, scale_factors[ch][i]);
1162
                } else {
1163
                    v = 0;
1164
                }
1165
                s->sb_samples[ch][j][i] = v;
1166
            }
1167
        }
1168
        for(i=bound;i<SBLIMIT;i++) {
1169
            n = allocation[0][i];
1170
            if (n) {
1171
                mant = get_bits(&s->gb, n + 1);
1172
                v = l1_unscale(n, mant, scale_factors[0][i]);
1173
                s->sb_samples[0][j][i] = v;
1174
                v = l1_unscale(n, mant, scale_factors[1][i]);
1175
                s->sb_samples[1][j][i] = v;
1176
            } else {
1177
                s->sb_samples[0][j][i] = 0;
1178
                s->sb_samples[1][j][i] = 0;
1179
            }
1180
        }
1181
    }
1182
    return 12;
1183
}
1184
1185
static int mp_decode_layer2(MPADecodeContext *s)
1186
{
1187
    int sblimit; /* number of used subbands */
1188
    const unsigned char *alloc_table;
1189
    int table, bit_alloc_bits, i, j, ch, bound, v;
1190
    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1191
    unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1192
    unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1193
    int scale, qindex, bits, steps, k, l, m, b;
1194 de6d9b64 Fabrice Bellard
1195 239c2f4c Fabrice Bellard
    /* select decoding table */
1196 08aa2c9b Aurelien Jacobs
    table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
1197 239c2f4c Fabrice Bellard
                            s->sample_rate, s->lsf);
1198 677fe2e2 Aurelien Jacobs
    sblimit = ff_mpa_sblimit_table[table];
1199
    alloc_table = ff_mpa_alloc_tables[table];
1200 239c2f4c Fabrice Bellard
1201 115329f1 Diego Biurrun
    if (s->mode == MPA_JSTEREO)
1202 239c2f4c Fabrice Bellard
        bound = (s->mode_ext + 1) * 4;
1203
    else
1204
        bound = sblimit;
1205
1206 318c5e05 Michel Bardiaux
    dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
1207 2caa92d9 Michael Niedermayer
1208
    /* sanity check */
1209
    if( bound > sblimit ) bound = sblimit;
1210
1211 239c2f4c Fabrice Bellard
    /* parse bit allocation */
1212
    j = 0;
1213
    for(i=0;i<bound;i++) {
1214
        bit_alloc_bits = alloc_table[j];
1215
        for(ch=0;ch<s->nb_channels;ch++) {
1216
            bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1217
        }
1218
        j += 1 << bit_alloc_bits;
1219
    }
1220
    for(i=bound;i<sblimit;i++) {
1221
        bit_alloc_bits = alloc_table[j];
1222
        v = get_bits(&s->gb, bit_alloc_bits);
1223
        bit_alloc[0][i] = v;
1224
        bit_alloc[1][i] = v;
1225
        j += 1 << bit_alloc_bits;
1226 de6d9b64 Fabrice Bellard
    }
1227 239c2f4c Fabrice Bellard
1228
    /* scale codes */
1229
    for(i=0;i<sblimit;i++) {
1230
        for(ch=0;ch<s->nb_channels;ch++) {
1231 115329f1 Diego Biurrun
            if (bit_alloc[ch][i])
1232 239c2f4c Fabrice Bellard
                scale_code[ch][i] = get_bits(&s->gb, 2);
1233
        }
1234
    }
1235 115329f1 Diego Biurrun
1236 239c2f4c Fabrice Bellard
    /* scale factors */
1237
    for(i=0;i<sblimit;i++) {
1238
        for(ch=0;ch<s->nb_channels;ch++) {
1239
            if (bit_alloc[ch][i]) {
1240
                sf = scale_factors[ch][i];
1241
                switch(scale_code[ch][i]) {
1242
                default:
1243
                case 0:
1244
                    sf[0] = get_bits(&s->gb, 6);
1245
                    sf[1] = get_bits(&s->gb, 6);
1246
                    sf[2] = get_bits(&s->gb, 6);
1247
                    break;
1248
                case 2:
1249
                    sf[0] = get_bits(&s->gb, 6);
1250
                    sf[1] = sf[0];
1251
                    sf[2] = sf[0];
1252
                    break;
1253
                case 1:
1254
                    sf[0] = get_bits(&s->gb, 6);
1255
                    sf[2] = get_bits(&s->gb, 6);
1256
                    sf[1] = sf[0];
1257
                    break;
1258
                case 3:
1259
                    sf[0] = get_bits(&s->gb, 6);
1260
                    sf[2] = get_bits(&s->gb, 6);
1261
                    sf[1] = sf[2];
1262
                    break;
1263
                }
1264
            }
1265
        }
1266
    }
1267
1268
    /* samples */
1269
    for(k=0;k<3;k++) {
1270
        for(l=0;l<12;l+=3) {
1271
            j = 0;
1272
            for(i=0;i<bound;i++) {
1273
                bit_alloc_bits = alloc_table[j];
1274
                for(ch=0;ch<s->nb_channels;ch++) {
1275
                    b = bit_alloc[ch][i];
1276
                    if (b) {
1277
                        scale = scale_factors[ch][i][k];
1278
                        qindex = alloc_table[j+b];
1279 677fe2e2 Aurelien Jacobs
                        bits = ff_mpa_quant_bits[qindex];
1280 239c2f4c Fabrice Bellard
                        if (bits < 0) {
1281
                            /* 3 values at the same time */
1282
                            v = get_bits(&s->gb, -bits);
1283 677fe2e2 Aurelien Jacobs
                            steps = ff_mpa_quant_steps[qindex];
1284 115329f1 Diego Biurrun
                            s->sb_samples[ch][k * 12 + l + 0][i] =
1285 239c2f4c Fabrice Bellard
                                l2_unscale_group(steps, v % steps, scale);
1286
                            v = v / steps;
1287 115329f1 Diego Biurrun
                            s->sb_samples[ch][k * 12 + l + 1][i] =
1288 239c2f4c Fabrice Bellard
                                l2_unscale_group(steps, v % steps, scale);
1289
                            v = v / steps;
1290 115329f1 Diego Biurrun
                            s->sb_samples[ch][k * 12 + l + 2][i] =
1291 239c2f4c Fabrice Bellard
                                l2_unscale_group(steps, v, scale);
1292
                        } else {
1293
                            for(m=0;m<3;m++) {
1294
                                v = get_bits(&s->gb, bits);
1295
                                v = l1_unscale(bits - 1, v, scale);
1296
                                s->sb_samples[ch][k * 12 + l + m][i] = v;
1297
                            }
1298
                        }
1299
                    } else {
1300
                        s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1301
                        s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1302
                        s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1303
                    }
1304
                }
1305
                /* next subband in alloc table */
1306 115329f1 Diego Biurrun
                j += 1 << bit_alloc_bits;
1307 239c2f4c Fabrice Bellard
            }
1308
            /* XXX: find a way to avoid this duplication of code */
1309
            for(i=bound;i<sblimit;i++) {
1310
                bit_alloc_bits = alloc_table[j];
1311
                b = bit_alloc[0][i];
1312
                if (b) {
1313
                    int mant, scale0, scale1;
1314
                    scale0 = scale_factors[0][i][k];
1315
                    scale1 = scale_factors[1][i][k];
1316
                    qindex = alloc_table[j+b];
1317 677fe2e2 Aurelien Jacobs
                    bits = ff_mpa_quant_bits[qindex];
1318 239c2f4c Fabrice Bellard
                    if (bits < 0) {
1319
                        /* 3 values at the same time */
1320
                        v = get_bits(&s->gb, -bits);
1321 677fe2e2 Aurelien Jacobs
                        steps = ff_mpa_quant_steps[qindex];
1322 239c2f4c Fabrice Bellard
                        mant = v % steps;
1323
                        v = v / steps;
1324 115329f1 Diego Biurrun
                        s->sb_samples[0][k * 12 + l + 0][i] =
1325 239c2f4c Fabrice Bellard
                            l2_unscale_group(steps, mant, scale0);
1326 115329f1 Diego Biurrun
                        s->sb_samples[1][k * 12 + l + 0][i] =
1327 239c2f4c Fabrice Bellard
                            l2_unscale_group(steps, mant, scale1);
1328
                        mant = v % steps;
1329
                        v = v / steps;
1330 115329f1 Diego Biurrun
                        s->sb_samples[0][k * 12 + l + 1][i] =
1331 239c2f4c Fabrice Bellard
                            l2_unscale_group(steps, mant, scale0);
1332 115329f1 Diego Biurrun
                        s->sb_samples[1][k * 12 + l + 1][i] =
1333 239c2f4c Fabrice Bellard
                            l2_unscale_group(steps, mant, scale1);
1334 115329f1 Diego Biurrun
                        s->sb_samples[0][k * 12 + l + 2][i] =
1335 239c2f4c Fabrice Bellard
                            l2_unscale_group(steps, v, scale0);
1336 115329f1 Diego Biurrun
                        s->sb_samples[1][k * 12 + l + 2][i] =
1337 239c2f4c Fabrice Bellard
                            l2_unscale_group(steps, v, scale1);
1338
                    } else {
1339
                        for(m=0;m<3;m++) {
1340
                            mant = get_bits(&s->gb, bits);
1341 115329f1 Diego Biurrun
                            s->sb_samples[0][k * 12 + l + m][i] =
1342 239c2f4c Fabrice Bellard
                                l1_unscale(bits - 1, mant, scale0);
1343 115329f1 Diego Biurrun
                            s->sb_samples[1][k * 12 + l + m][i] =
1344 239c2f4c Fabrice Bellard
                                l1_unscale(bits - 1, mant, scale1);
1345
                        }
1346
                    }
1347
                } else {
1348
                    s->sb_samples[0][k * 12 + l + 0][i] = 0;
1349
                    s->sb_samples[0][k * 12 + l + 1][i] = 0;
1350
                    s->sb_samples[0][k * 12 + l + 2][i] = 0;
1351
                    s->sb_samples[1][k * 12 + l + 0][i] = 0;
1352
                    s->sb_samples[1][k * 12 + l + 1][i] = 0;
1353
                    s->sb_samples[1][k * 12 + l + 2][i] = 0;
1354
                }
1355
                /* next subband in alloc table */
1356 115329f1 Diego Biurrun
                j += 1 << bit_alloc_bits;
1357 239c2f4c Fabrice Bellard
            }
1358
            /* fill remaining samples to zero */
1359
            for(i=sblimit;i<SBLIMIT;i++) {
1360
                for(ch=0;ch<s->nb_channels;ch++) {
1361
                    s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1362
                    s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1363
                    s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1364
                }
1365
            }
1366
        }
1367
    }
1368
    return 3 * 12;
1369 de6d9b64 Fabrice Bellard
}
1370
1371 239c2f4c Fabrice Bellard
static inline void lsf_sf_expand(int *slen,
1372
                                 int sf, int n1, int n2, int n3)
1373
{
1374
    if (n3) {
1375
        slen[3] = sf % n3;
1376
        sf /= n3;
1377
    } else {
1378
        slen[3] = 0;
1379
    }
1380
    if (n2) {
1381
        slen[2] = sf % n2;
1382
        sf /= n2;
1383
    } else {
1384
        slen[2] = 0;
1385
    }
1386
    slen[1] = sf % n1;
1387
    sf /= n1;
1388
    slen[0] = sf;
1389
}
1390
1391 115329f1 Diego Biurrun
static void exponents_from_scale_factors(MPADecodeContext *s,
1392 239c2f4c Fabrice Bellard
                                         GranuleDef *g,
1393 0c1a9eda Zdenek Kabelac
                                         int16_t *exponents)
1394 239c2f4c Fabrice Bellard
{
1395 0c1a9eda Zdenek Kabelac
    const uint8_t *bstab, *pretab;
1396 239c2f4c Fabrice Bellard
    int len, i, j, k, l, v0, shift, gain, gains[3];
1397 0c1a9eda Zdenek Kabelac
    int16_t *exp_ptr;
1398 239c2f4c Fabrice Bellard
1399
    exp_ptr = exponents;
1400
    gain = g->global_gain - 210;
1401
    shift = g->scalefac_scale + 1;
1402
1403
    bstab = band_size_long[s->sample_rate_index];
1404
    pretab = mpa_pretab[g->preflag];
1405
    for(i=0;i<g->long_end;i++) {
1406 bc2d2757 Michael Niedermayer
        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1407 239c2f4c Fabrice Bellard
        len = bstab[i];
1408
        for(j=len;j>0;j--)
1409
            *exp_ptr++ = v0;
1410
    }
1411
1412
    if (g->short_start < 13) {
1413
        bstab = band_size_short[s->sample_rate_index];
1414
        gains[0] = gain - (g->subblock_gain[0] << 3);
1415
        gains[1] = gain - (g->subblock_gain[1] << 3);
1416
        gains[2] = gain - (g->subblock_gain[2] << 3);
1417
        k = g->long_end;
1418
        for(i=g->short_start;i<13;i++) {
1419
            len = bstab[i];
1420
            for(l=0;l<3;l++) {
1421 bc2d2757 Michael Niedermayer
                v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1422 239c2f4c Fabrice Bellard
                for(j=len;j>0;j--)
1423
                *exp_ptr++ = v0;
1424
            }
1425
        }
1426
    }
1427
}
1428
1429
/* handle n = 0 too */
1430
static inline int get_bitsz(GetBitContext *s, int n)
1431
{
1432
    if (n == 0)
1433
        return 0;
1434
    else
1435
        return get_bits(s, n);
1436
}
1437
1438 5773d460 Michael Niedermayer
1439
static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1440
    if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1441
        s->gb= s->in_gb;
1442
        s->in_gb.buffer=NULL;
1443
        assert((get_bits_count(&s->gb) & 7) == 0);
1444
        skip_bits_long(&s->gb, *pos - *end_pos);
1445
        *end_pos2=
1446
        *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1447
        *pos= get_bits_count(&s->gb);
1448
    }
1449
}
1450
1451 239c2f4c Fabrice Bellard
static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1452 06e7fb82 Michael Niedermayer
                          int16_t *exponents, int end_pos2)
1453 239c2f4c Fabrice Bellard
{
1454
    int s_index;
1455 14168ddb Michael Niedermayer
    int i;
1456 06e7fb82 Michael Niedermayer
    int last_pos, bits_left;
1457 239c2f4c Fabrice Bellard
    VLC *vlc;
1458 06e7fb82 Michael Niedermayer
    int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1459 239c2f4c Fabrice Bellard
1460
    /* low frequencies (called big values) */
1461
    s_index = 0;
1462
    for(i=0;i<3;i++) {
1463 14168ddb Michael Niedermayer
        int j, k, l, linbits;
1464 239c2f4c Fabrice Bellard
        j = g->region_size[i];
1465
        if (j == 0)
1466
            continue;
1467
        /* select vlc table */
1468
        k = g->table_select[i];
1469
        l = mpa_huff_data[k][0];
1470
        linbits = mpa_huff_data[k][1];
1471
        vlc = &huff_vlc[l];
1472
1473 daf4cd9a Michael Niedermayer
        if(!l){
1474 731d0b1a Michael Niedermayer
            memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1475 daf4cd9a Michael Niedermayer
            s_index += 2*j;
1476
            continue;
1477
        }
1478
1479 239c2f4c Fabrice Bellard
        /* read huffcode and compute each couple */
1480
        for(;j>0;j--) {
1481 14168ddb Michael Niedermayer
            int exponent, x, y, v;
1482 06e7fb82 Michael Niedermayer
            int pos= get_bits_count(&s->gb);
1483
1484
            if (pos >= end_pos){
1485
//                av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1486 5773d460 Michael Niedermayer
                switch_buffer(s, &pos, &end_pos, &end_pos2);
1487 06e7fb82 Michael Niedermayer
//                av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1488
                if(pos >= end_pos)
1489
                    break;
1490
            }
1491 1f1d675b Michael Niedermayer
            y = get_vlc2(&s->gb, vlc->table, 7, 3);
1492 3328ec63 Michael Niedermayer
1493
            if(!y){
1494
                g->sb_hybrid[s_index  ] =
1495
                g->sb_hybrid[s_index+1] = 0;
1496
                s_index += 2;
1497
                continue;
1498
            }
1499
1500 9b49f89c Michael Niedermayer
            exponent= exponents[s_index];
1501 daf4cd9a Michael Niedermayer
1502 318c5e05 Michel Bardiaux
            dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1503 9b49f89c Michael Niedermayer
                    i, g->region_size[i] - j, x, y, exponent);
1504 9bffa9e7 Michael Niedermayer
            if(y&16){
1505
                x = y >> 5;
1506
                y = y & 0x0f;
1507 55017f35 Michael Niedermayer
                if (x < 15){
1508 bc2d2757 Michael Niedermayer
                    v = expval_table[ exponent ][ x ];
1509
//                      v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1510 55017f35 Michael Niedermayer
                }else{
1511
                    x += get_bitsz(&s->gb, linbits);
1512 9b49f89c Michael Niedermayer
                    v = l3_unscale(x, exponent);
1513 55017f35 Michael Niedermayer
                }
1514 239c2f4c Fabrice Bellard
                if (get_bits1(&s->gb))
1515
                    v = -v;
1516 9bffa9e7 Michael Niedermayer
                g->sb_hybrid[s_index] = v;
1517 55017f35 Michael Niedermayer
                if (y < 15){
1518 bc2d2757 Michael Niedermayer
                    v = expval_table[ exponent ][ y ];
1519 55017f35 Michael Niedermayer
                }else{
1520
                    y += get_bitsz(&s->gb, linbits);
1521 9b49f89c Michael Niedermayer
                    v = l3_unscale(y, exponent);
1522 55017f35 Michael Niedermayer
                }
1523 239c2f4c Fabrice Bellard
                if (get_bits1(&s->gb))
1524
                    v = -v;
1525 9bffa9e7 Michael Niedermayer
                g->sb_hybrid[s_index+1] = v;
1526
            }else{
1527
                x = y >> 5;
1528
                y = y & 0x0f;
1529
                x += y;
1530
                if (x < 15){
1531
                    v = expval_table[ exponent ][ x ];
1532
                }else{
1533
                    x += get_bitsz(&s->gb, linbits);
1534
                    v = l3_unscale(x, exponent);
1535
                }
1536
                if (get_bits1(&s->gb))
1537
                    v = -v;
1538
                g->sb_hybrid[s_index+!!y] = v;
1539 d37bb9d8 Michael Niedermayer
                g->sb_hybrid[s_index+ !y] = 0;
1540 239c2f4c Fabrice Bellard
            }
1541 9bffa9e7 Michael Niedermayer
            s_index+=2;
1542 239c2f4c Fabrice Bellard
        }
1543
    }
1544 115329f1 Diego Biurrun
1545 239c2f4c Fabrice Bellard
    /* high frequencies */
1546
    vlc = &huff_quad_vlc[g->count1table_select];
1547 a08a502e Michael Niedermayer
    last_pos=0;
1548 239c2f4c Fabrice Bellard
    while (s_index <= 572) {
1549 14168ddb Michael Niedermayer
        int pos, code;
1550 239c2f4c Fabrice Bellard
        pos = get_bits_count(&s->gb);
1551
        if (pos >= end_pos) {
1552 1b4f17fa Michael Niedermayer
            if (pos > end_pos2 && last_pos){
1553
                /* some encoders generate an incorrect size for this
1554
                   part. We must go back into the data */
1555
                s_index -= 4;
1556
                skip_bits_long(&s->gb, last_pos - pos);
1557 11f63a22 Andreas Öman
                av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1558 047599a4 Michael Niedermayer
                if(s->error_recognition >= FF_ER_COMPLIANT)
1559 8e5606bf Michael Niedermayer
                    s_index=0;
1560 1b4f17fa Michael Niedermayer
                break;
1561
            }
1562 06e7fb82 Michael Niedermayer
//                av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1563 5773d460 Michael Niedermayer
            switch_buffer(s, &pos, &end_pos, &end_pos2);
1564 06e7fb82 Michael Niedermayer
//                av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1565
            if(pos >= end_pos)
1566
                break;
1567 239c2f4c Fabrice Bellard
        }
1568 a08a502e Michael Niedermayer
        last_pos= pos;
1569 8db1a1dd Michael Niedermayer
1570 3c693e77 Michael Niedermayer
        code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1571 318c5e05 Michel Bardiaux
        dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1572 3c693e77 Michael Niedermayer
        g->sb_hybrid[s_index+0]=
1573
        g->sb_hybrid[s_index+1]=
1574
        g->sb_hybrid[s_index+2]=
1575
        g->sb_hybrid[s_index+3]= 0;
1576
        while(code){
1577 5a6a6cc7 Diego Biurrun
            static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1578 14168ddb Michael Niedermayer
            int v;
1579 3c693e77 Michael Niedermayer
            int pos= s_index+idxtab[code];
1580
            code ^= 8>>idxtab[code];
1581 bc2d2757 Michael Niedermayer
            v = exp_table[ exponents[pos] ];
1582
//            v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1583 3c693e77 Michael Niedermayer
            if(get_bits1(&s->gb))
1584
                v = -v;
1585
            g->sb_hybrid[pos] = v;
1586 239c2f4c Fabrice Bellard
        }
1587 3c693e77 Michael Niedermayer
        s_index+=4;
1588 239c2f4c Fabrice Bellard
    }
1589 06e7fb82 Michael Niedermayer
    /* skip extension bits */
1590 e0cf9204 Michael Niedermayer
    bits_left = end_pos2 - get_bits_count(&s->gb);
1591 06e7fb82 Michael Niedermayer
//av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1592 bf3a971c Michael Niedermayer
    if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1593 11f63a22 Andreas Öman
        av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1594 8e5606bf Michael Niedermayer
        s_index=0;
1595 047599a4 Michael Niedermayer
    }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1596 11f63a22 Andreas Öman
        av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1597 8e5606bf Michael Niedermayer
        s_index=0;
1598 06e7fb82 Michael Niedermayer
    }
1599 8e5606bf Michael Niedermayer
    memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1600 06e7fb82 Michael Niedermayer
    skip_bits_long(&s->gb, bits_left);
1601
1602 e0cf9204 Michael Niedermayer
    i= get_bits_count(&s->gb);
1603 5773d460 Michael Niedermayer
    switch_buffer(s, &i, &end_pos, &end_pos2);
1604 e0cf9204 Michael Niedermayer
1605 de6d9b64 Fabrice Bellard
    return 0;
1606
}
1607
1608 239c2f4c Fabrice Bellard
/* Reorder short blocks from bitstream order to interleaved order. It
1609
   would be faster to do it in parsing, but the code would be far more
1610
   complicated */
1611
static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1612
{
1613 6430ce0f Michael Niedermayer
    int i, j, len;
1614 0c1a9eda Zdenek Kabelac
    int32_t *ptr, *dst, *ptr1;
1615
    int32_t tmp[576];
1616 239c2f4c Fabrice Bellard
1617
    if (g->block_type != 2)
1618
        return;
1619
1620
    if (g->switch_point) {
1621
        if (s->sample_rate_index != 8) {
1622
            ptr = g->sb_hybrid + 36;
1623
        } else {
1624
            ptr = g->sb_hybrid + 48;
1625
        }
1626
    } else {
1627
        ptr = g->sb_hybrid;
1628
    }
1629 115329f1 Diego Biurrun
1630 239c2f4c Fabrice Bellard
    for(i=g->short_start;i<13;i++) {
1631
        len = band_size_short[s->sample_rate_index][i];
1632
        ptr1 = ptr;
1633 6430ce0f Michael Niedermayer
        dst = tmp;
1634
        for(j=len;j>0;j--) {
1635
            *dst++ = ptr[0*len];
1636
            *dst++ = ptr[1*len];
1637
            *dst++ = ptr[2*len];
1638
            ptr++;
1639 239c2f4c Fabrice Bellard
        }
1640 6430ce0f Michael Niedermayer
        ptr+=2*len;
1641
        memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1642 239c2f4c Fabrice Bellard
    }
1643
}
1644
1645
#define ISQRT2 FIXR(0.70710678118654752440)
1646
1647
static void compute_stereo(MPADecodeContext *s,
1648
                           GranuleDef *g0, GranuleDef *g1)
1649
{
1650
    int i, j, k, l;
1651 0c1a9eda Zdenek Kabelac
    int32_t v1, v2;
1652 239c2f4c Fabrice Bellard
    int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1653 0c1a9eda Zdenek Kabelac
    int32_t (*is_tab)[16];
1654
    int32_t *tab0, *tab1;
1655 239c2f4c Fabrice Bellard
    int non_zero_found_short[3];
1656
1657
    /* intensity stereo */
1658
    if (s->mode_ext & MODE_EXT_I_STEREO) {
1659
        if (!s->lsf) {
1660
            is_tab = is_table;
1661
            sf_max = 7;
1662
        } else {
1663
            is_tab = is_table_lsf[g1->scalefac_compress & 1];
1664
            sf_max = 16;
1665
        }
1666 115329f1 Diego Biurrun
1667 239c2f4c Fabrice Bellard
        tab0 = g0->sb_hybrid + 576;
1668
        tab1 = g1->sb_hybrid + 576;
1669
1670
        non_zero_found_short[0] = 0;
1671
        non_zero_found_short[1] = 0;
1672
        non_zero_found_short[2] = 0;
1673
        k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1674
        for(i = 12;i >= g1->short_start;i--) {
1675
            /* for last band, use previous scale factor */
1676
            if (i != 11)
1677
                k -= 3;
1678
            len = band_size_short[s->sample_rate_index][i];
1679
            for(l=2;l>=0;l--) {
1680
                tab0 -= len;
1681
                tab1 -= len;
1682
                if (!non_zero_found_short[l]) {
1683
                    /* test if non zero band. if so, stop doing i-stereo */
1684
                    for(j=0;j<len;j++) {
1685
                        if (tab1[j] != 0) {
1686
                            non_zero_found_short[l] = 1;
1687
                            goto found1;
1688
                        }
1689
                    }
1690
                    sf = g1->scale_factors[k + l];
1691
                    if (sf >= sf_max)
1692
                        goto found1;
1693
1694
                    v1 = is_tab[0][sf];
1695
                    v2 = is_tab[1][sf];
1696
                    for(j=0;j<len;j++) {
1697
                        tmp0 = tab0[j];
1698 4deaa946 Måns Rullgård
                        tab0[j] = MULL(tmp0, v1, FRAC_BITS);
1699
                        tab1[j] = MULL(tmp0, v2, FRAC_BITS);
1700 239c2f4c Fabrice Bellard
                    }
1701
                } else {
1702
                found1:
1703
                    if (s->mode_ext & MODE_EXT_MS_STEREO) {
1704
                        /* lower part of the spectrum : do ms stereo
1705
                           if enabled */
1706
                        for(j=0;j<len;j++) {
1707
                            tmp0 = tab0[j];
1708
                            tmp1 = tab1[j];
1709 4deaa946 Måns Rullgård
                            tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1710
                            tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1711 239c2f4c Fabrice Bellard
                        }
1712
                    }
1713
                }
1714
            }
1715
        }
1716
1717 115329f1 Diego Biurrun
        non_zero_found = non_zero_found_short[0] |
1718
            non_zero_found_short[1] |
1719 239c2f4c Fabrice Bellard
            non_zero_found_short[2];
1720
1721
        for(i = g1->long_end - 1;i >= 0;i--) {
1722
            len = band_size_long[s->sample_rate_index][i];
1723
            tab0 -= len;
1724
            tab1 -= len;
1725
            /* test if non zero band. if so, stop doing i-stereo */
1726
            if (!non_zero_found) {
1727
                for(j=0;j<len;j++) {
1728
                    if (tab1[j] != 0) {
1729
                        non_zero_found = 1;
1730
                        goto found2;
1731
                    }
1732
                }
1733
                /* for last band, use previous scale factor */
1734
                k = (i == 21) ? 20 : i;
1735
                sf = g1->scale_factors[k];
1736
                if (sf >= sf_max)
1737
                    goto found2;
1738
                v1 = is_tab[0][sf];
1739
                v2 = is_tab[1][sf];
1740
                for(j=0;j<len;j++) {
1741
                    tmp0 = tab0[j];
1742 4deaa946 Måns Rullgård
                    tab0[j] = MULL(tmp0, v1, FRAC_BITS);
1743
                    tab1[j] = MULL(tmp0, v2, FRAC_BITS);
1744 239c2f4c Fabrice Bellard
                }
1745
            } else {
1746
            found2:
1747
                if (s->mode_ext & MODE_EXT_MS_STEREO) {
1748
                    /* lower part of the spectrum : do ms stereo
1749
                       if enabled */
1750
                    for(j=0;j<len;j++) {
1751
                        tmp0 = tab0[j];
1752
                        tmp1 = tab1[j];
1753 4deaa946 Måns Rullgård
                        tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1754
                        tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1755 239c2f4c Fabrice Bellard
                    }
1756
                }
1757
            }
1758
        }
1759
    } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1760
        /* ms stereo ONLY */
1761
        /* NOTE: the 1/sqrt(2) normalization factor is included in the
1762
           global gain */
1763
        tab0 = g0->sb_hybrid;
1764
        tab1 = g1->sb_hybrid;
1765
        for(i=0;i<576;i++) {
1766
            tmp0 = tab0[i];
1767
            tmp1 = tab1[i];
1768
            tab0[i] = tmp0 + tmp1;
1769
            tab1[i] = tmp0 - tmp1;
1770
        }
1771
    }
1772
}
1773
1774 a1e257b2 Michael Niedermayer
static void compute_antialias_integer(MPADecodeContext *s,
1775 239c2f4c Fabrice Bellard
                              GranuleDef *g)
1776
{
1777 ce4a29c0 Michael Niedermayer
    int32_t *ptr, *csa;
1778
    int n, i;
1779 239c2f4c Fabrice Bellard
1780
    /* we antialias only "long" bands */
1781
    if (g->block_type == 2) {
1782
        if (!g->switch_point)
1783
            return;
1784
        /* XXX: check this for 8000Hz case */
1785
        n = 1;
1786
    } else {
1787
        n = SBLIMIT - 1;
1788
    }
1789 115329f1 Diego Biurrun
1790 239c2f4c Fabrice Bellard
    ptr = g->sb_hybrid + 18;
1791
    for(i = n;i > 0;i--) {
1792 ce4a29c0 Michael Niedermayer
        int tmp0, tmp1, tmp2;
1793
        csa = &csa_table[0][0];
1794
#define INT_AA(j) \
1795 44f1698a Michael Niedermayer
            tmp0 = ptr[-1-j];\
1796
            tmp1 = ptr[   j];\
1797 ce4a29c0 Michael Niedermayer
            tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1798 44f1698a Michael Niedermayer
            ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1799
            ptr[   j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1800 ce4a29c0 Michael Niedermayer
1801
        INT_AA(0)
1802
        INT_AA(1)
1803
        INT_AA(2)
1804
        INT_AA(3)
1805
        INT_AA(4)
1806
        INT_AA(5)
1807
        INT_AA(6)
1808
        INT_AA(7)
1809 115329f1 Diego Biurrun
1810
        ptr += 18;
1811 a1e257b2 Michael Niedermayer
    }
1812
}
1813
1814
static void compute_antialias_float(MPADecodeContext *s,
1815
                              GranuleDef *g)
1816
{
1817 ce4a29c0 Michael Niedermayer
    int32_t *ptr;
1818
    int n, i;
1819 a1e257b2 Michael Niedermayer
1820
    /* we antialias only "long" bands */
1821
    if (g->block_type == 2) {
1822
        if (!g->switch_point)
1823
            return;
1824
        /* XXX: check this for 8000Hz case */
1825
        n = 1;
1826
    } else {
1827
        n = SBLIMIT - 1;
1828
    }
1829 115329f1 Diego Biurrun
1830 a1e257b2 Michael Niedermayer
    ptr = g->sb_hybrid + 18;
1831
    for(i = n;i > 0;i--) {
1832 ce4a29c0 Michael Niedermayer
        float tmp0, tmp1;
1833 115329f1 Diego Biurrun
        float *csa = &csa_table_float[0][0];
1834 ce4a29c0 Michael Niedermayer
#define FLOAT_AA(j)\
1835
        tmp0= ptr[-1-j];\
1836
        tmp1= ptr[   j];\
1837
        ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1838
        ptr[   j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
1839 115329f1 Diego Biurrun
1840 ce4a29c0 Michael Niedermayer
        FLOAT_AA(0)
1841
        FLOAT_AA(1)
1842
        FLOAT_AA(2)
1843
        FLOAT_AA(3)
1844
        FLOAT_AA(4)
1845
        FLOAT_AA(5)
1846
        FLOAT_AA(6)
1847
        FLOAT_AA(7)
1848
1849 115329f1 Diego Biurrun
        ptr += 18;
1850 239c2f4c Fabrice Bellard
    }
1851
}
1852
1853
static void compute_imdct(MPADecodeContext *s,
1854 115329f1 Diego Biurrun
                          GranuleDef *g,
1855 0c1a9eda Zdenek Kabelac
                          int32_t *sb_samples,
1856
                          int32_t *mdct_buf)
1857 239c2f4c Fabrice Bellard
{
1858 125d6246 Michael Niedermayer
    int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
1859 0c1a9eda Zdenek Kabelac
    int32_t out2[12];
1860 125d6246 Michael Niedermayer
    int i, j, mdct_long_end, v, sblimit;
1861 239c2f4c Fabrice Bellard
1862
    /* find last non zero block */
1863
    ptr = g->sb_hybrid + 576;
1864
    ptr1 = g->sb_hybrid + 2 * 18;
1865
    while (ptr >= ptr1) {
1866
        ptr -= 6;
1867
        v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1868
        if (v != 0)
1869
            break;
1870
    }
1871
    sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1872
1873
    if (g->block_type == 2) {
1874
        /* XXX: check for 8000 Hz */
1875
        if (g->switch_point)
1876
            mdct_long_end = 2;
1877
        else
1878
            mdct_long_end = 0;
1879
    } else {
1880
        mdct_long_end = sblimit;
1881
    }
1882
1883
    buf = mdct_buf;
1884
    ptr = g->sb_hybrid;
1885
    for(j=0;j<mdct_long_end;j++) {
1886
        /* apply window & overlap with previous buffer */
1887
        out_ptr = sb_samples + j;
1888
        /* select window */
1889
        if (g->switch_point && j < 2)
1890
            win1 = mdct_win[0];
1891
        else
1892
            win1 = mdct_win[g->block_type];
1893
        /* select frequency inversion */
1894
        win = win1 + ((4 * 36) & -(j & 1));
1895 711ae726 Michael Niedermayer
        imdct36(out_ptr, buf, ptr, win);
1896
        out_ptr += 18*SBLIMIT;
1897 239c2f4c Fabrice Bellard
        ptr += 18;
1898
        buf += 18;
1899
    }
1900
    for(j=mdct_long_end;j<sblimit;j++) {
1901
        /* select frequency inversion */
1902
        win = mdct_win[2] + ((4 * 36) & -(j & 1));
1903
        out_ptr = sb_samples + j;
1904 115329f1 Diego Biurrun
1905 125d6246 Michael Niedermayer
        for(i=0; i<6; i++){
1906
            *out_ptr = buf[i];
1907
            out_ptr += SBLIMIT;
1908
        }
1909
        imdct12(out2, ptr + 0);
1910
        for(i=0;i<6;i++) {
1911
            *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
1912
            buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
1913 239c2f4c Fabrice Bellard
            out_ptr += SBLIMIT;
1914
        }
1915 125d6246 Michael Niedermayer
        imdct12(out2, ptr + 1);
1916
        for(i=0;i<6;i++) {
1917
            *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
1918
            buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
1919
            out_ptr += SBLIMIT;
1920
        }
1921
        imdct12(out2, ptr + 2);
1922
        for(i=0;i<6;i++) {
1923
            buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
1924
            buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
1925
            buf[i + 6*2] = 0;
1926
        }
1927 239c2f4c Fabrice Bellard
        ptr += 18;
1928
        buf += 18;
1929
    }
1930
    /* zero bands */
1931
    for(j=sblimit;j<SBLIMIT;j++) {
1932
        /* overlap */
1933
        out_ptr = sb_samples + j;
1934
        for(i=0;i<18;i++) {
1935
            *out_ptr = buf[i];
1936
            buf[i] = 0;
1937
            out_ptr += SBLIMIT;
1938
        }
1939
        buf += 18;
1940
    }
1941
}
1942
1943
/* main layer3 decoding function */
1944
static int mp_decode_layer3(MPADecodeContext *s)
1945
{
1946
    int nb_granules, main_data_begin, private_bits;
1947 06e7fb82 Michael Niedermayer
    int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1948 239c2f4c Fabrice Bellard
    GranuleDef granules[2][2], *g;
1949 0c1a9eda Zdenek Kabelac
    int16_t exponents[576];
1950 239c2f4c Fabrice Bellard
1951
    /* read side info */
1952
    if (s->lsf) {
1953
        main_data_begin = get_bits(&s->gb, 8);
1954 a753e55b Michael Niedermayer
        private_bits = get_bits(&s->gb, s->nb_channels);
1955 239c2f4c Fabrice Bellard
        nb_granules = 1;
1956
    } else {
1957
        main_data_begin = get_bits(&s->gb, 9);
1958
        if (s->nb_channels == 2)
1959
            private_bits = get_bits(&s->gb, 3);
1960
        else
1961
            private_bits = get_bits(&s->gb, 5);
1962
        nb_granules = 2;
1963
        for(ch=0;ch<s->nb_channels;ch++) {
1964
            granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
1965
            granules[ch][1].scfsi = get_bits(&s->gb, 4);
1966
        }
1967
    }
1968 115329f1 Diego Biurrun
1969 239c2f4c Fabrice Bellard
    for(gr=0;gr<nb_granules;gr++) {
1970
        for(ch=0;ch<s->nb_channels;ch++) {
1971 318c5e05 Michel Bardiaux
            dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1972 239c2f4c Fabrice Bellard
            g = &granules[ch][gr];
1973
            g->part2_3_length = get_bits(&s->gb, 12);
1974
            g->big_values = get_bits(&s->gb, 9);
1975 bd60e11b Michael Niedermayer
            if(g->big_values > 288){
1976 318c5e05 Michel Bardiaux
                av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1977 bd60e11b Michael Niedermayer
                return -1;
1978
            }
1979
1980 239c2f4c Fabrice Bellard
            g->global_gain = get_bits(&s->gb, 8);
1981
            /* if MS stereo only is selected, we precompute the
1982
               1/sqrt(2) renormalization factor */
1983 115329f1 Diego Biurrun
            if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1984 239c2f4c Fabrice Bellard
                MODE_EXT_MS_STEREO)
1985
                g->global_gain -= 2;
1986
            if (s->lsf)
1987
                g->scalefac_compress = get_bits(&s->gb, 9);
1988
            else
1989
                g->scalefac_compress = get_bits(&s->gb, 4);
1990 5fc32c27 Alex Beregszaszi
            blocksplit_flag = get_bits1(&s->gb);
1991 239c2f4c Fabrice Bellard
            if (blocksplit_flag) {
1992
                g->block_type = get_bits(&s->gb, 2);
1993 bd60e11b Michael Niedermayer
                if (g->block_type == 0){
1994 11f63a22 Andreas Öman
                    av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1995 239c2f4c Fabrice Bellard
                    return -1;
1996 bd60e11b Michael Niedermayer
                }
1997 5fc32c27 Alex Beregszaszi
                g->switch_point = get_bits1(&s->gb);
1998 239c2f4c Fabrice Bellard
                for(i=0;i<2;i++)
1999
                    g->table_select[i] = get_bits(&s->gb, 5);
2000 115329f1 Diego Biurrun
                for(i=0;i<3;i++)
2001 239c2f4c Fabrice Bellard
                    g->subblock_gain[i] = get_bits(&s->gb, 3);
2002 9f7458b2 Michael Niedermayer
                ff_init_short_region(s, g);
2003 239c2f4c Fabrice Bellard
            } else {
2004 9f7458b2 Michael Niedermayer
                int region_address1, region_address2;
2005 239c2f4c Fabrice Bellard
                g->block_type = 0;
2006
                g->switch_point = 0;
2007
                for(i=0;i<3;i++)
2008
                    g->table_select[i] = get_bits(&s->gb, 5);
2009
                /* compute huffman coded region sizes */
2010
                region_address1 = get_bits(&s->gb, 4);
2011
                region_address2 = get_bits(&s->gb, 3);
2012 318c5e05 Michel Bardiaux
                dprintf(s->avctx, "region1=%d region2=%d\n",
2013 239c2f4c Fabrice Bellard
                        region_address1, region_address2);
2014 9f7458b2 Michael Niedermayer
                ff_init_long_region(s, g, region_address1, region_address2);
2015 239c2f4c Fabrice Bellard
            }
2016 9f7458b2 Michael Niedermayer
            ff_region_offset2size(g);
2017
            ff_compute_band_indexes(s, g);
2018 115329f1 Diego Biurrun
2019 239c2f4c Fabrice Bellard
            g->preflag = 0;
2020
            if (!s->lsf)
2021 5fc32c27 Alex Beregszaszi
                g->preflag = get_bits1(&s->gb);
2022
            g->scalefac_scale = get_bits1(&s->gb);
2023
            g->count1table_select = get_bits1(&s->gb);
2024 318c5e05 Michel Bardiaux
            dprintf(s->avctx, "block_type=%d switch_point=%d\n",
2025 239c2f4c Fabrice Bellard
                    g->block_type, g->switch_point);
2026
        }
2027
    }
2028
2029 1ede228a Roberto Togni
  if (!s->adu_mode) {
2030 06e7fb82 Michael Niedermayer
    const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2031 1b4f17fa Michael Niedermayer
    assert((get_bits_count(&s->gb) & 7) == 0);
2032 239c2f4c Fabrice Bellard
    /* now we get bits from the main_data_begin offset */
2033 318c5e05 Michel Bardiaux
    dprintf(s->avctx, "seekback: %d\n", main_data_begin);
2034 06e7fb82 Michael Niedermayer
//av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2035
2036
    memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2037
    s->in_gb= s->gb;
2038 ee50a7c1 Michael Niedermayer
        init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
2039
        skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
2040 1ede228a Roberto Togni
  }
2041 239c2f4c Fabrice Bellard
2042
    for(gr=0;gr<nb_granules;gr++) {
2043
        for(ch=0;ch<s->nb_channels;ch++) {
2044
            g = &granules[ch][gr];
2045 ee50a7c1 Michael Niedermayer
            if(get_bits_count(&s->gb)<0){
2046 11f63a22 Andreas Öman
                av_log(s->avctx, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skipping granule %d\n",
2047 ee50a7c1 Michael Niedermayer
                                            main_data_begin, s->last_buf_size, gr);
2048
                skip_bits_long(&s->gb, g->part2_3_length);
2049
                memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
2050
                if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
2051
                    skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
2052
                    s->gb= s->in_gb;
2053
                    s->in_gb.buffer=NULL;
2054
                }
2055
                continue;
2056
            }
2057 115329f1 Diego Biurrun
2058 239c2f4c Fabrice Bellard
            bits_pos = get_bits_count(&s->gb);
2059 115329f1 Diego Biurrun
2060 239c2f4c Fabrice Bellard
            if (!s->lsf) {
2061 0c1a9eda Zdenek Kabelac
                uint8_t *sc;
2062 239c2f4c Fabrice Bellard
                int slen, slen1, slen2;
2063
2064
                /* MPEG1 scale factors */
2065
                slen1 = slen_table[0][g->scalefac_compress];
2066
                slen2 = slen_table[1][g->scalefac_compress];
2067 318c5e05 Michel Bardiaux
                dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
2068 239c2f4c Fabrice Bellard
                if (g->block_type == 2) {
2069
                    n = g->switch_point ? 17 : 18;
2070
                    j = 0;
2071 e90a7270 Michael Niedermayer
                    if(slen1){
2072
                        for(i=0;i<n;i++)
2073
                            g->scale_factors[j++] = get_bits(&s->gb, slen1);
2074
                    }else{
2075
                        for(i=0;i<n;i++)
2076
                            g->scale_factors[j++] = 0;
2077
                    }
2078
                    if(slen2){
2079
                        for(i=0;i<18;i++)
2080
                            g->scale_factors[j++] = get_bits(&s->gb, slen2);
2081
                        for(i=0;i<3;i++)
2082
                            g->scale_factors[j++] = 0;
2083
                    }else{
2084
                        for(i=0;i<21;i++)
2085
                            g->scale_factors[j++] = 0;
2086
                    }
2087 239c2f4c Fabrice Bellard
                } else {
2088
                    sc = granules[ch][0].scale_factors;
2089
                    j = 0;
2090
                    for(k=0;k<4;k++) {
2091
                        n = (k == 0 ? 6 : 5);
2092
                        if ((g->scfsi & (0x8 >> k)) == 0) {
2093
                            slen = (k < 2) ? slen1 : slen2;
2094 e90a7270 Michael Niedermayer
                            if(slen){
2095
                                for(i=0;i<n;i++)
2096
                                    g->scale_factors[j++] = get_bits(&s->gb, slen);
2097
                            }else{
2098
                                for(i=0;i<n;i++)
2099
                                    g->scale_factors[j++] = 0;
2100
                            }
2101 239c2f4c Fabrice Bellard
                        } else {
2102
                            /* simply copy from last granule */
2103
                            for(i=0;i<n;i++) {
2104
                                g->scale_factors[j] = sc[j];
2105
                                j++;
2106
                            }
2107
                        }
2108
                    }
2109
                    g->scale_factors[j++] = 0;
2110
                }
2111
            } else {
2112
                int tindex, tindex2, slen[4], sl, sf;
2113
2114
                /* LSF scale factors */
2115
                if (g->block_type == 2) {
2116
                    tindex = g->switch_point ? 2 : 1;
2117
                } else {
2118
                    tindex = 0;
2119
                }
2120
                sf = g->scalefac_compress;
2121
                if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2122
                    /* intensity stereo case */
2123
                    sf >>= 1;
2124
                    if (sf < 180) {
2125
                        lsf_sf_expand(slen, sf, 6, 6, 0);
2126
                        tindex2 = 3;
2127
                    } else if (sf < 244) {
2128
                        lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2129
                        tindex2 = 4;
2130
                    } else {
2131
                        lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2132
                        tindex2 = 5;
2133
                    }
2134
                } else {
2135
                    /* normal case */
2136
                    if (sf < 400) {
2137
                        lsf_sf_expand(slen, sf, 5, 4, 4);
2138
                        tindex2 = 0;
2139
                    } else if (sf < 500) {
2140
                        lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2141
                        tindex2 = 1;
2142
                    } else {
2143
                        lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2144
                        tindex2 = 2;
2145
                        g->preflag = 1;
2146
                    }
2147
                }
2148
2149
                j = 0;
2150
                for(k=0;k<4;k++) {
2151
                    n = lsf_nsf_table[tindex2][tindex][k];
2152
                    sl = slen[k];
2153 92c5cb62 Michael Niedermayer
                    if(sl){
2154 e90a7270 Michael Niedermayer
                        for(i=0;i<n;i++)
2155
                            g->scale_factors[j++] = get_bits(&s->gb, sl);
2156
                    }else{
2157
                        for(i=0;i<n;i++)
2158
                            g->scale_factors[j++] = 0;
2159
                    }
2160 239c2f4c Fabrice Bellard
                }
2161
                /* XXX: should compute exact size */
2162
                for(;j<40;j++)
2163
                    g->scale_factors[j] = 0;
2164
            }
2165
2166
            exponents_from_scale_factors(s, g, exponents);
2167
2168
            /* read Huffman coded residue */
2169 ee50a7c1 Michael Niedermayer
            huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
2170 239c2f4c Fabrice Bellard
        } /* ch */
2171
2172
        if (s->nb_channels == 2)
2173
            compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2174
2175
        for(ch=0;ch<s->nb_channels;ch++) {
2176
            g = &granules[ch][gr];
2177
2178
            reorder_block(s, g);
2179 a1e257b2 Michael Niedermayer
            s->compute_antialias(s, g);
2180 115329f1 Diego Biurrun
            compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2181 239c2f4c Fabrice Bellard
        }
2182
    } /* gr */
2183 ee50a7c1 Michael Niedermayer
    if(get_bits_count(&s->gb)<0)
2184
        skip_bits_long(&s->gb, -get_bits_count(&s->gb));
2185 239c2f4c Fabrice Bellard
    return nb_granules * 18;
2186
}
2187
2188 115329f1 Diego Biurrun
static int mp_decode_frame(MPADecodeContext *s,
2189 06e7fb82 Michael Niedermayer
                           OUT_INT *samples, const uint8_t *buf, int buf_size)
2190 239c2f4c Fabrice Bellard
{
2191
    int i, nb_frames, ch;
2192 a3a5f4d6 Michael Niedermayer
    OUT_INT *samples_ptr;
2193 239c2f4c Fabrice Bellard
2194 06e7fb82 Michael Niedermayer
    init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2195 115329f1 Diego Biurrun
2196 239c2f4c Fabrice Bellard
    /* skip error protection field */
2197
    if (s->error_protection)
2198 7ae7300e Alex Beregszaszi
        skip_bits(&s->gb, 16);
2199 239c2f4c Fabrice Bellard
2200 318c5e05 Michel Bardiaux
    dprintf(s->avctx, "frame %d:\n", s->frame_count);
2201 239c2f4c Fabrice Bellard
    switch(s->layer) {
2202
    case 1:
2203 6122b733 Baptiste Coudurier
        s->avctx->frame_size = 384;
2204 239c2f4c Fabrice Bellard
        nb_frames = mp_decode_layer1(s);
2205
        break;
2206
    case 2:
2207 6122b733 Baptiste Coudurier
        s->avctx->frame_size = 1152;
2208 239c2f4c Fabrice Bellard
        nb_frames = mp_decode_layer2(s);
2209
        break;
2210
    case 3:
2211 6122b733 Baptiste Coudurier
        s->avctx->frame_size = s->lsf ? 576 : 1152;
2212 239c2f4c Fabrice Bellard
    default:
2213
        nb_frames = mp_decode_layer3(s);
2214 06e7fb82 Michael Niedermayer
2215 c0c66dd8 Michael Niedermayer
        s->last_buf_size=0;
2216
        if(s->in_gb.buffer){
2217
            align_get_bits(&s->gb);
2218
            i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2219 1b4f17fa Michael Niedermayer
            if(i >= 0 && i <= BACKSTEP_SIZE){
2220 c0c66dd8 Michael Niedermayer
                memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2221
                s->last_buf_size=i;
2222 1b4f17fa Michael Niedermayer
            }else
2223 11f63a22 Andreas Öman
                av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2224 06e7fb82 Michael Niedermayer
            s->gb= s->in_gb;
2225 ee50a7c1 Michael Niedermayer
            s->in_gb.buffer= NULL;
2226 c0c66dd8 Michael Niedermayer
        }
2227
2228 06e7fb82 Michael Niedermayer
        align_get_bits(&s->gb);
2229
        assert((get_bits_count(&s->gb) & 7) == 0);
2230 c0c66dd8 Michael Niedermayer
        i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2231
2232 1b4f17fa Michael Niedermayer
        if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2233 1d4113d0 Michael Niedermayer
            if(i<0)
2234
                av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2235 1b4f17fa Michael Niedermayer
            i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2236
        }
2237 c0c66dd8 Michael Niedermayer
        assert(i <= buf_size - HEADER_SIZE && i>= 0);
2238 1b4f17fa Michael Niedermayer
        memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2239 c0c66dd8 Michael Niedermayer
        s->last_buf_size += i;
2240 06e7fb82 Michael Niedermayer
2241 239c2f4c Fabrice Bellard
        break;
2242
    }
2243 969c163f Michael Niedermayer
2244 239c2f4c Fabrice Bellard
    /* apply the synthesis filter */
2245
    for(ch=0;ch<s->nb_channels;ch++) {
2246
        samples_ptr = samples + ch;
2247
        for(i=0;i<nb_frames;i++) {
2248 bf1f4da0 Alex Beregszaszi
            ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2249 bb270c08 Diego Biurrun
                         window, &s->dither_state,
2250
                         samples_ptr, s->nb_channels,
2251 239c2f4c Fabrice Bellard
                         s->sb_samples[ch][i]);
2252
            samples_ptr += 32 * s->nb_channels;
2253
        }
2254
    }
2255 969c163f Michael Niedermayer
2256 a3a5f4d6 Michael Niedermayer
    return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2257 239c2f4c Fabrice Bellard
}
2258
2259 de6d9b64 Fabrice Bellard
static int decode_frame(AVCodecContext * avctx,
2260 bb270c08 Diego Biurrun
                        void *data, int *data_size,
2261 7993df65 Michael Niedermayer
                        const uint8_t * buf, int buf_size)
2262 de6d9b64 Fabrice Bellard
{
2263
    MPADecodeContext *s = avctx->priv_data;
2264 0c1a9eda Zdenek Kabelac
    uint32_t header;
2265 06e7fb82 Michael Niedermayer
    int out_size;
2266 a3a5f4d6 Michael Niedermayer
    OUT_INT *out_samples = data;
2267 de6d9b64 Fabrice Bellard
2268 06e7fb82 Michael Niedermayer
retry:
2269
    if(buf_size < HEADER_SIZE)
2270
        return -1;
2271
2272 2c124cb6 Ramiro Polla
    header = AV_RB32(buf);
2273 06e7fb82 Michael Niedermayer
    if(ff_mpa_check_header(header) < 0){
2274
        buf++;
2275
//        buf_size--;
2276 e1931c59 Diego Biurrun
        av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");
2277 06e7fb82 Michael Niedermayer
        goto retry;
2278
    }
2279
2280 cf92cec7 Andreas Öman
    if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
2281 06e7fb82 Michael Niedermayer
        /* free format: prepare to compute frame size */
2282
        s->frame_size = -1;
2283
        return -1;
2284
    }
2285
    /* update codec info */
2286
    avctx->channels = s->nb_channels;
2287
    avctx->bit_rate = s->bit_rate;
2288
    avctx->sub_id = s->layer;
2289
2290 7b95bc58 Michael Niedermayer
    if(s->frame_size<=0 || s->frame_size > buf_size){
2291 06e7fb82 Michael Niedermayer
        av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2292
        return -1;
2293 7b95bc58 Michael Niedermayer
    }else if(s->frame_size < buf_size){
2294
        av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2295 d5aa01cd Michael Niedermayer
        buf_size= s->frame_size;
2296 de6d9b64 Fabrice Bellard
    }
2297 06e7fb82 Michael Niedermayer
2298
    out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2299 498c544a Michael Niedermayer
    if(out_size>=0){
2300 06e7fb82 Michael Niedermayer
        *data_size = out_size;
2301 498c544a Michael Niedermayer
        avctx->sample_rate = s->sample_rate;
2302
        //FIXME maybe move the other codec info stuff from above here too
2303
    }else
2304 e1931c59 Diego Biurrun
        av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2305 06e7fb82 Michael Niedermayer
    s->frame_size = 0;
2306
    return buf_size;
2307 de6d9b64 Fabrice Bellard
}
2308
2309 9bb328d3 Michael Niedermayer
static void flush(AVCodecContext *avctx){
2310
    MPADecodeContext *s = avctx->priv_data;
2311 812f29ff Brian Brice
    memset(s->synth_buf, 0, sizeof(s->synth_buf));
2312 9bb328d3 Michael Niedermayer
    s->last_buf_size= 0;
2313
}
2314
2315 b250f9c6 Aurelien Jacobs
#if CONFIG_MP3ADU_DECODER
2316 1ede228a Roberto Togni
static int decode_frame_adu(AVCodecContext * avctx,
2317 bb270c08 Diego Biurrun
                        void *data, int *data_size,
2318 7993df65 Michael Niedermayer
                        const uint8_t * buf, int buf_size)
2319 1ede228a Roberto Togni
{
2320
    MPADecodeContext *s = avctx->priv_data;
2321
    uint32_t header;
2322
    int len, out_size;
2323 a3a5f4d6 Michael Niedermayer
    OUT_INT *out_samples = data;
2324 1ede228a Roberto Togni
2325
    len = buf_size;
2326
2327
    // Discard too short frames
2328
    if (buf_size < HEADER_SIZE) {
2329
        *data_size = 0;
2330
        return buf_size;
2331
    }
2332
2333
2334
    if (len > MPA_MAX_CODED_FRAME_SIZE)
2335
        len = MPA_MAX_CODED_FRAME_SIZE;
2336
2337
    // Get header and restore sync word
2338 2c124cb6 Ramiro Polla
    header = AV_RB32(buf) | 0xffe00000;
2339 1ede228a Roberto Togni
2340 a7a85899 Michael Niedermayer
    if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2341 1ede228a Roberto Togni
        *data_size = 0;
2342
        return buf_size;
2343
    }
2344
2345 cf92cec7 Andreas Öman
    ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2346 1ede228a Roberto Togni
    /* update codec info */
2347
    avctx->sample_rate = s->sample_rate;
2348
    avctx->channels = s->nb_channels;
2349
    avctx->bit_rate = s->bit_rate;
2350
    avctx->sub_id = s->layer;
2351
2352 d0ed455f Baptiste Coudurier
    s->frame_size = len;
2353 1ede228a Roberto Togni
2354
    if (avctx->parse_only) {
2355 06e7fb82 Michael Niedermayer
        out_size = buf_size;
2356 1ede228a Roberto Togni
    } else {
2357 06e7fb82 Michael Niedermayer
        out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2358 1ede228a Roberto Togni
    }
2359
2360
    *data_size = out_size;
2361
    return buf_size;
2362
}
2363 62bb489b Måns Rullgård
#endif /* CONFIG_MP3ADU_DECODER */
2364 1ede228a Roberto Togni
2365 b250f9c6 Aurelien Jacobs
#if CONFIG_MP3ON4_DECODER
2366 b61d2782 Baptiste Coudurier
2367 676e26ab Baptiste Coudurier
/**
2368
 * Context for MP3On4 decoder
2369
 */
2370
typedef struct MP3On4DecodeContext {
2371
    int frames;   ///< number of mp3 frames per block (number of mp3 decoder instances)
2372
    int syncword; ///< syncword patch
2373 0d5b2eb4 Baptiste Coudurier
    const uint8_t *coff; ///< channels offsets in output buffer
2374 676e26ab Baptiste Coudurier
    MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2375
} MP3On4DecodeContext;
2376
2377 b61d2782 Baptiste Coudurier
#include "mpeg4audio.h"
2378
2379 d2a7718d Roberto Togni
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2380 60dfa0b8 Baptiste Coudurier
static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5};   /* number of mp3 decoder instances */
2381 d2a7718d Roberto Togni
/* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2382 60dfa0b8 Baptiste Coudurier
static const uint8_t chan_offset[8][5] = {
2383 d2a7718d Roberto Togni
    {0},
2384
    {0},            // C
2385
    {0},            // FLR
2386
    {2,0},          // C FLR
2387
    {2,0,3},        // C FLR BS
2388
    {4,0,2},        // C FLR BLRS
2389
    {4,0,2,5},      // C FLR BLRS LFE
2390
    {4,0,2,6,5},    // C FLR BLRS BLR LFE
2391
};
2392
2393
2394
static int decode_init_mp3on4(AVCodecContext * avctx)
2395
{
2396
    MP3On4DecodeContext *s = avctx->priv_data;
2397 b61d2782 Baptiste Coudurier
    MPEG4AudioConfig cfg;
2398 d2a7718d Roberto Togni
    int i;
2399
2400
    if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2401
        av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2402
        return -1;
2403
    }
2404
2405 b61d2782 Baptiste Coudurier
    ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2406
    if (!cfg.chan_config || cfg.chan_config > 7) {
2407 d2a7718d Roberto Togni
        av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2408
        return -1;
2409
    }
2410 0d5b2eb4 Baptiste Coudurier
    s->frames = mp3Frames[cfg.chan_config];
2411
    s->coff = chan_offset[cfg.chan_config];
2412
    avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2413 d2a7718d Roberto Togni
2414 9f95bfe2 Baptiste Coudurier
    if (cfg.sample_rate < 16000)
2415
        s->syncword = 0xffe00000;
2416
    else
2417
        s->syncword = 0xfff00000;
2418
2419 d2a7718d Roberto Togni
    /* Init the first mp3 decoder in standard way, so that all tables get builded
2420
     * We replace avctx->priv_data with the context of the first decoder so that
2421
     * decode_init() does not have to be changed.
2422 5e534865 Diego Biurrun
     * Other decoders will be initialized here copying data from the first context
2423 d2a7718d Roberto Togni
     */
2424
    // Allocate zeroed memory for the first decoder context
2425
    s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2426
    // Put decoder context in place to make init_decode() happy
2427
    avctx->priv_data = s->mp3decctx[0];
2428
    decode_init(avctx);
2429
    // Restore mp3on4 context pointer
2430
    avctx->priv_data = s;
2431
    s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2432
2433
    /* Create a separate codec/context for each frame (first is already ok).
2434
     * Each frame is 1 or 2 channels - up to 5 frames allowed
2435
     */
2436
    for (i = 1; i < s->frames; i++) {
2437
        s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2438
        s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2439
        s->mp3decctx[i]->adu_mode = 1;
2440 318c5e05 Michel Bardiaux
        s->mp3decctx[i]->avctx = avctx;
2441 d2a7718d Roberto Togni
    }
2442
2443
    return 0;
2444
}
2445
2446
2447
static int decode_close_mp3on4(AVCodecContext * avctx)
2448
{
2449
    MP3On4DecodeContext *s = avctx->priv_data;
2450
    int i;
2451
2452
    for (i = 0; i < s->frames; i++)
2453
        if (s->mp3decctx[i])
2454
            av_free(s->mp3decctx[i]);
2455
2456
    return 0;
2457
}
2458
2459
2460
static int decode_frame_mp3on4(AVCodecContext * avctx,
2461 bb270c08 Diego Biurrun
                        void *data, int *data_size,
2462 7993df65 Michael Niedermayer
                        const uint8_t * buf, int buf_size)
2463 d2a7718d Roberto Togni
{
2464
    MP3On4DecodeContext *s = avctx->priv_data;
2465
    MPADecodeContext *m;
2466 41b8800f Baptiste Coudurier
    int fsize, len = buf_size, out_size = 0;
2467 d2a7718d Roberto Togni
    uint32_t header;
2468
    OUT_INT *out_samples = data;
2469
    OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2470
    OUT_INT *outptr, *bp;
2471 5fd7a9fc Baptiste Coudurier
    int fr, j, n;
2472 d2a7718d Roberto Togni
2473 a82dcdff Baptiste Coudurier
    *data_size = 0;
2474 d2a7718d Roberto Togni
    // Discard too short frames
2475 a82dcdff Baptiste Coudurier
    if (buf_size < HEADER_SIZE)
2476
        return -1;
2477 d2a7718d Roberto Togni
2478
    // If only one decoder interleave is not needed
2479
    outptr = s->frames == 1 ? out_samples : decoded_buf;
2480
2481 5fd7a9fc Baptiste Coudurier
    avctx->bit_rate = 0;
2482
2483 d2a7718d Roberto Togni
    for (fr = 0; fr < s->frames; fr++) {
2484 ec39f7d8 Baptiste Coudurier
        fsize = AV_RB16(buf) >> 4;
2485 5be25fc1 Baptiste Coudurier
        fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2486 d2a7718d Roberto Togni
        m = s->mp3decctx[fr];
2487
        assert (m != NULL);
2488
2489 9f95bfe2 Baptiste Coudurier
        header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2490 d2a7718d Roberto Togni
2491 169203c1 Baptiste Coudurier
        if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2492
            break;
2493 d2a7718d Roberto Togni
2494 cf92cec7 Andreas Öman
        ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2495 f0f53c83 Baptiste Coudurier
        out_size += mp_decode_frame(m, outptr, buf, fsize);
2496 ec39f7d8 Baptiste Coudurier
        buf += fsize;
2497
        len -= fsize;
2498 d2a7718d Roberto Togni
2499
        if(s->frames > 1) {
2500 9708d52e Baptiste Coudurier
            n = m->avctx->frame_size*m->nb_channels;
2501 d2a7718d Roberto Togni
            /* interleave output data */
2502 0d5b2eb4 Baptiste Coudurier
            bp = out_samples + s->coff[fr];
2503 d2a7718d Roberto Togni
            if(m->nb_channels == 1) {
2504
                for(j = 0; j < n; j++) {
2505
                    *bp = decoded_buf[j];
2506 8da8970d Baptiste Coudurier
                    bp += avctx->channels;
2507 d2a7718d Roberto Togni
                }
2508
            } else {
2509
                for(j = 0; j < n; j++) {
2510
                    bp[0] = decoded_buf[j++];
2511
                    bp[1] = decoded_buf[j];
2512 8da8970d Baptiste Coudurier
                    bp += avctx->channels;
2513 d2a7718d Roberto Togni
                }
2514
            }
2515
        }
2516 5fd7a9fc Baptiste Coudurier
        avctx->bit_rate += m->bit_rate;
2517 d2a7718d Roberto Togni
    }
2518
2519
    /* update codec info */
2520
    avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2521
2522
    *data_size = out_size;
2523
    return buf_size;
2524
}
2525 62bb489b Måns Rullgård
#endif /* CONFIG_MP3ON4_DECODER */
2526 d2a7718d Roberto Togni
2527 b250f9c6 Aurelien Jacobs
#if CONFIG_MP1_DECODER
2528 cb595b7f Michael Niedermayer
AVCodec mp1_decoder =
2529
{
2530
    "mp1",
2531
    CODEC_TYPE_AUDIO,
2532
    CODEC_ID_MP1,
2533
    sizeof(MPADecodeContext),
2534
    decode_init,
2535
    NULL,
2536
    NULL,
2537
    decode_frame,
2538
    CODEC_CAP_PARSE_ONLY,
2539
    .flush= flush,
2540
    .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2541
};
2542
#endif
2543 b250f9c6 Aurelien Jacobs
#if CONFIG_MP2_DECODER
2544 4b1f4f23 Juanjo
AVCodec mp2_decoder =
2545 de6d9b64 Fabrice Bellard
{
2546 4b1f4f23 Juanjo
    "mp2",
2547 de6d9b64 Fabrice Bellard
    CODEC_TYPE_AUDIO,
2548
    CODEC_ID_MP2,
2549
    sizeof(MPADecodeContext),
2550
    decode_init,
2551
    NULL,
2552
    NULL,
2553
    decode_frame,
2554 8c5b5683 Fabrice Bellard
    CODEC_CAP_PARSE_ONLY,
2555 4a69055b Brian Brice
    .flush= flush,
2556 fe4bf374 Stefano Sabatini
    .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2557 de6d9b64 Fabrice Bellard
};
2558 62bb489b Måns Rullgård
#endif
2559 b250f9c6 Aurelien Jacobs
#if CONFIG_MP3_DECODER
2560 4b1f4f23 Juanjo
AVCodec mp3_decoder =
2561
{
2562
    "mp3",
2563
    CODEC_TYPE_AUDIO,
2564 80783dc2 Fabrice Bellard
    CODEC_ID_MP3,
2565 4b1f4f23 Juanjo
    sizeof(MPADecodeContext),
2566
    decode_init,
2567
    NULL,
2568
    NULL,
2569
    decode_frame,
2570 8c5b5683 Fabrice Bellard
    CODEC_CAP_PARSE_ONLY,
2571 9bb328d3 Michael Niedermayer
    .flush= flush,
2572 fe4bf374 Stefano Sabatini
    .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2573 4b1f4f23 Juanjo
};
2574 62bb489b Måns Rullgård
#endif
2575 b250f9c6 Aurelien Jacobs
#if CONFIG_MP3ADU_DECODER
2576 1ede228a Roberto Togni
AVCodec mp3adu_decoder =
2577
{
2578
    "mp3adu",
2579
    CODEC_TYPE_AUDIO,
2580
    CODEC_ID_MP3ADU,
2581
    sizeof(MPADecodeContext),
2582
    decode_init,
2583
    NULL,
2584
    NULL,
2585
    decode_frame_adu,
2586
    CODEC_CAP_PARSE_ONLY,
2587 9bb328d3 Michael Niedermayer
    .flush= flush,
2588 fe4bf374 Stefano Sabatini
    .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2589 1ede228a Roberto Togni
};
2590 62bb489b Måns Rullgård
#endif
2591 b250f9c6 Aurelien Jacobs
#if CONFIG_MP3ON4_DECODER
2592 d2a7718d Roberto Togni
AVCodec mp3on4_decoder =
2593
{
2594
    "mp3on4",
2595
    CODEC_TYPE_AUDIO,
2596
    CODEC_ID_MP3ON4,
2597
    sizeof(MP3On4DecodeContext),
2598
    decode_init_mp3on4,
2599
    NULL,
2600
    decode_close_mp3on4,
2601
    decode_frame_mp3on4,
2602 9bb328d3 Michael Niedermayer
    .flush= flush,
2603 fe4bf374 Stefano Sabatini
    .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
2604 d2a7718d Roberto Togni
};
2605 62bb489b Måns Rullgård
#endif