]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegaudiodec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / mpegaudiodec.c
1 /*
2  * MPEG Audio decoder
3  * Copyright (c) 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * MPEG Audio decoder
25  */
26
27 #define UNCHECKED_BITSTREAM_READER 1
28
29 #include "libavutil/audioconvert.h"
30 #include "avcodec.h"
31 #include "get_bits.h"
32 #include "mathops.h"
33 #include "mpegaudiodsp.h"
34
35 /*
36  * TODO:
37  *  - test lsf / mpeg25 extensively.
38  */
39
40 #include "mpegaudio.h"
41 #include "mpegaudiodecheader.h"
42
43 #define BACKSTEP_SIZE 512
44 #define EXTRABYTES 24
45
46 /* layer 3 "granule" */
47 typedef struct GranuleDef {
48     uint8_t scfsi;
49     int part2_3_length;
50     int big_values;
51     int global_gain;
52     int scalefac_compress;
53     uint8_t block_type;
54     uint8_t switch_point;
55     int table_select[3];
56     int subblock_gain[3];
57     uint8_t scalefac_scale;
58     uint8_t count1table_select;
59     int region_size[3]; /* number of huffman codes in each region */
60     int preflag;
61     int short_start, long_end; /* long/short band indexes */
62     uint8_t scale_factors[40];
63     DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
64 } GranuleDef;
65
66 typedef struct MPADecodeContext {
67     MPA_DECODE_HEADER
68     uint8_t last_buf[2 * BACKSTEP_SIZE + EXTRABYTES];
69     int last_buf_size;
70     /* next header (used in free format parsing) */
71     uint32_t free_format_next_header;
72     GetBitContext gb;
73     GetBitContext in_gb;
74     DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
75     int synth_buf_offset[MPA_MAX_CHANNELS];
76     DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
77     INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
78     GranuleDef granules[2][2]; /* Used in Layer 3 */
79     int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
80     int dither_state;
81     int err_recognition;
82     AVCodecContext* avctx;
83     MPADSPContext mpadsp;
84     AVFrame frame;
85 } MPADecodeContext;
86
87 #if CONFIG_FLOAT
88 #   define SHR(a,b)       ((a)*(1.0f/(1<<(b))))
89 #   define FIXR_OLD(a)    ((int)((a) * FRAC_ONE + 0.5))
90 #   define FIXR(x)        ((float)(x))
91 #   define FIXHR(x)       ((float)(x))
92 #   define MULH3(x, y, s) ((s)*(y)*(x))
93 #   define MULLx(x, y, s) ((y)*(x))
94 #   define RENAME(a) a ## _float
95 #   define OUT_FMT AV_SAMPLE_FMT_FLT
96 #else
97 #   define SHR(a,b)       ((a)>>(b))
98 /* WARNING: only correct for positive numbers */
99 #   define FIXR_OLD(a)    ((int)((a) * FRAC_ONE + 0.5))
100 #   define FIXR(a)        ((int)((a) * FRAC_ONE + 0.5))
101 #   define FIXHR(a)       ((int)((a) * (1LL<<32) + 0.5))
102 #   define MULH3(x, y, s) MULH((s)*(x), y)
103 #   define MULLx(x, y, s) MULL(x,y,s)
104 #   define RENAME(a)      a ## _fixed
105 #   define OUT_FMT AV_SAMPLE_FMT_S16
106 #endif
107
108 /****************/
109
110 #define HEADER_SIZE 4
111
112 #include "mpegaudiodata.h"
113 #include "mpegaudiodectab.h"
114
115 /* vlc structure for decoding layer 3 huffman tables */
116 static VLC huff_vlc[16];
117 static VLC_TYPE huff_vlc_tables[
118     0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
119   142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
120   ][2];
121 static const int huff_vlc_tables_sizes[16] = {
122     0,  128,  128,  128,  130,  128,  154,  166,
123   142,  204,  190,  170,  542,  460,  662,  414
124 };
125 static VLC huff_quad_vlc[2];
126 static VLC_TYPE  huff_quad_vlc_tables[128+16][2];
127 static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
128 /* computed from band_size_long */
129 static uint16_t band_index_long[9][23];
130 #include "mpegaudio_tablegen.h"
131 /* intensity stereo coef table */
132 static INTFLOAT is_table[2][16];
133 static INTFLOAT is_table_lsf[2][2][16];
134 static INTFLOAT csa_table[8][4];
135 /** Window for MDCT. Note that only the component [0,17] and [20,37] are used,
136     the components 18 and 19 are there only to assure 128-bit alignment for asm
137  */
138 DECLARE_ALIGNED(16, static INTFLOAT, mdct_win)[8][40];
139
140 static int16_t division_tab3[1<<6 ];
141 static int16_t division_tab5[1<<8 ];
142 static int16_t division_tab9[1<<11];
143
144 static int16_t * const division_tabs[4] = {
145     division_tab3, division_tab5, NULL, division_tab9
146 };
147
148 /* lower 2 bits: modulo 3, higher bits: shift */
149 static uint16_t scale_factor_modshift[64];
150 /* [i][j]:  2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
151 static int32_t scale_factor_mult[15][3];
152 /* mult table for layer 2 group quantization */
153
154 #define SCALE_GEN(v) \
155 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
156
157 static const int32_t scale_factor_mult2[3][3] = {
158     SCALE_GEN(4.0 / 3.0), /* 3 steps */
159     SCALE_GEN(4.0 / 5.0), /* 5 steps */
160     SCALE_GEN(4.0 / 9.0), /* 9 steps */
161 };
162
163 /**
164  * Convert region offsets to region sizes and truncate
165  * size to big_values.
166  */
167 static void ff_region_offset2size(GranuleDef *g)
168 {
169     int i, k, j = 0;
170     g->region_size[2] = 576 / 2;
171     for (i = 0; i < 3; i++) {
172         k = FFMIN(g->region_size[i], g->big_values);
173         g->region_size[i] = k - j;
174         j = k;
175     }
176 }
177
178 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
179 {
180     if (g->block_type == 2)
181         g->region_size[0] = (36 / 2);
182     else {
183         if (s->sample_rate_index <= 2)
184             g->region_size[0] = (36 / 2);
185         else if (s->sample_rate_index != 8)
186             g->region_size[0] = (54 / 2);
187         else
188             g->region_size[0] = (108 / 2);
189     }
190     g->region_size[1] = (576 / 2);
191 }
192
193 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
194 {
195     int l;
196     g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
197     /* should not overflow */
198     l = FFMIN(ra1 + ra2 + 2, 22);
199     g->region_size[1] = band_index_long[s->sample_rate_index][      l] >> 1;
200 }
201
202 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
203 {
204     if (g->block_type == 2) {
205         if (g->switch_point) {
206             /* if switched mode, we handle the 36 first samples as
207                 long blocks.  For 8000Hz, we handle the 48 first
208                 exponents as long blocks (XXX: check this!) */
209             if (s->sample_rate_index <= 2)
210                 g->long_end = 8;
211             else if (s->sample_rate_index != 8)
212                 g->long_end = 6;
213             else
214                 g->long_end = 4; /* 8000 Hz */
215
216             g->short_start = 2 + (s->sample_rate_index != 8);
217         } else {
218             g->long_end    = 0;
219             g->short_start = 0;
220         }
221     } else {
222         g->short_start = 13;
223         g->long_end    = 22;
224     }
225 }
226
227 /* layer 1 unscaling */
228 /* n = number of bits of the mantissa minus 1 */
229 static inline int l1_unscale(int n, int mant, int scale_factor)
230 {
231     int shift, mod;
232     int64_t val;
233
234     shift   = scale_factor_modshift[scale_factor];
235     mod     = shift & 3;
236     shift >>= 2;
237     val     = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
238     shift  += n;
239     /* NOTE: at this point, 1 <= shift >= 21 + 15 */
240     return (int)((val + (1LL << (shift - 1))) >> shift);
241 }
242
243 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
244 {
245     int shift, mod, val;
246
247     shift   = scale_factor_modshift[scale_factor];
248     mod     = shift & 3;
249     shift >>= 2;
250
251     val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
252     /* NOTE: at this point, 0 <= shift <= 21 */
253     if (shift > 0)
254         val = (val + (1 << (shift - 1))) >> shift;
255     return val;
256 }
257
258 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
259 static inline int l3_unscale(int value, int exponent)
260 {
261     unsigned int m;
262     int e;
263
264     e  = table_4_3_exp  [4 * value + (exponent & 3)];
265     m  = table_4_3_value[4 * value + (exponent & 3)];
266     e -= exponent >> 2;
267     assert(e >= 1);
268     if (e > 31)
269         return 0;
270     m = (m + (1 << (e - 1))) >> e;
271
272     return m;
273 }
274
275 static av_cold void decode_init_static(void)
276 {
277     int i, j, k;
278     int offset;
279
280     /* scale factors table for layer 1/2 */
281     for (i = 0; i < 64; i++) {
282         int shift, mod;
283         /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
284         shift = i / 3;
285         mod   = i % 3;
286         scale_factor_modshift[i] = mod | (shift << 2);
287     }
288
289     /* scale factor multiply for layer 1 */
290     for (i = 0; i < 15; i++) {
291         int n, norm;
292         n = i + 2;
293         norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
294         scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0          * 2.0), FRAC_BITS);
295         scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
296         scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
297         av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
298                 scale_factor_mult[i][0],
299                 scale_factor_mult[i][1],
300                 scale_factor_mult[i][2]);
301     }
302
303     RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
304
305     /* huffman decode tables */
306     offset = 0;
307     for (i = 1; i < 16; i++) {
308         const HuffTable *h = &mpa_huff_tables[i];
309         int xsize, x, y;
310         uint8_t  tmp_bits [512];
311         uint16_t tmp_codes[512];
312
313         memset(tmp_bits , 0, sizeof(tmp_bits ));
314         memset(tmp_codes, 0, sizeof(tmp_codes));
315
316         xsize = h->xsize;
317
318         j = 0;
319         for (x = 0; x < xsize; x++) {
320             for (y = 0; y < xsize; y++) {
321                 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j  ];
322                 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
323             }
324         }
325
326         /* XXX: fail test */
327         huff_vlc[i].table = huff_vlc_tables+offset;
328         huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
329         init_vlc(&huff_vlc[i], 7, 512,
330                  tmp_bits, 1, 1, tmp_codes, 2, 2,
331                  INIT_VLC_USE_NEW_STATIC);
332         offset += huff_vlc_tables_sizes[i];
333     }
334     assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
335
336     offset = 0;
337     for (i = 0; i < 2; i++) {
338         huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
339         huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
340         init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
341                  mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
342                  INIT_VLC_USE_NEW_STATIC);
343         offset += huff_quad_vlc_tables_sizes[i];
344     }
345     assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
346
347     for (i = 0; i < 9; i++) {
348         k = 0;
349         for (j = 0; j < 22; j++) {
350             band_index_long[i][j] = k;
351             k += band_size_long[i][j];
352         }
353         band_index_long[i][22] = k;
354     }
355
356     /* compute n ^ (4/3) and store it in mantissa/exp format */
357
358     mpegaudio_tableinit();
359
360     for (i = 0; i < 4; i++) {
361         if (ff_mpa_quant_bits[i] < 0) {
362             for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
363                 int val1, val2, val3, steps;
364                 int val = j;
365                 steps   = ff_mpa_quant_steps[i];
366                 val1    = val % steps;
367                 val    /= steps;
368                 val2    = val % steps;
369                 val3    = val / steps;
370                 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
371             }
372         }
373     }
374
375
376     for (i = 0; i < 7; i++) {
377         float f;
378         INTFLOAT v;
379         if (i != 6) {
380             f = tan((double)i * M_PI / 12.0);
381             v = FIXR(f / (1.0 + f));
382         } else {
383             v = FIXR(1.0);
384         }
385         is_table[0][    i] = v;
386         is_table[1][6 - i] = v;
387     }
388     /* invalid values */
389     for (i = 7; i < 16; i++)
390         is_table[0][i] = is_table[1][i] = 0.0;
391
392     for (i = 0; i < 16; i++) {
393         double f;
394         int e, k;
395
396         for (j = 0; j < 2; j++) {
397             e = -(j + 1) * ((i + 1) >> 1);
398             f = pow(2.0, e / 4.0);
399             k = i & 1;
400             is_table_lsf[j][k ^ 1][i] = FIXR(f);
401             is_table_lsf[j][k    ][i] = FIXR(1.0);
402             av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
403                     i, j, (float) is_table_lsf[j][0][i],
404                     (float) is_table_lsf[j][1][i]);
405         }
406     }
407
408     for (i = 0; i < 8; i++) {
409         float ci, cs, ca;
410         ci = ci_table[i];
411         cs = 1.0 / sqrt(1.0 + ci * ci);
412         ca = cs * ci;
413 #if !CONFIG_FLOAT
414         csa_table[i][0] = FIXHR(cs/4);
415         csa_table[i][1] = FIXHR(ca/4);
416         csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
417         csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
418 #else
419         csa_table[i][0] = cs;
420         csa_table[i][1] = ca;
421         csa_table[i][2] = ca + cs;
422         csa_table[i][3] = ca - cs;
423 #endif
424     }
425
426     /* compute mdct windows */
427     for (i = 0; i < 36; i++) {
428         for (j = 0; j < 4; j++) {
429             double d;
430
431             if (j == 2 && i % 3 != 1)
432                 continue;
433
434             d = sin(M_PI * (i + 0.5) / 36.0);
435             if (j == 1) {
436                 if      (i >= 30) d = 0;
437                 else if (i >= 24) d = sin(M_PI * (i - 18 + 0.5) / 12.0);
438                 else if (i >= 18) d = 1;
439             } else if (j == 3) {
440                 if      (i <   6) d = 0;
441                 else if (i <  12) d = sin(M_PI * (i -  6 + 0.5) / 12.0);
442                 else if (i <  18) d = 1;
443             }
444             //merge last stage of imdct into the window coefficients
445             d *= 0.5 / cos(M_PI * (2 * i + 19) / 72);
446
447             if (j == 2)
448                 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
449             else {
450                 int idx = i < 18 ? i : i + 2;
451                 mdct_win[j][idx] = FIXHR((d / (1<<5)));
452             }
453         }
454     }
455
456     /* NOTE: we do frequency inversion adter the MDCT by changing
457         the sign of the right window coefs */
458     for (j = 0; j < 4; j++) {
459         for (i = 0; i < 40; i += 2) {
460             mdct_win[j + 4][i    ] =  mdct_win[j][i    ];
461             mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
462         }
463     }
464 }
465
466 static av_cold int decode_init(AVCodecContext * avctx)
467 {
468     static int initialized_tables = 0;
469     MPADecodeContext *s = avctx->priv_data;
470
471     if (!initialized_tables) {
472         decode_init_static();
473         initialized_tables = 1;
474     }
475
476     s->avctx = avctx;
477
478     ff_mpadsp_init(&s->mpadsp);
479
480     avctx->sample_fmt= OUT_FMT;
481     s->err_recognition = avctx->err_recognition;
482
483     if (avctx->codec_id == CODEC_ID_MP3ADU)
484         s->adu_mode = 1;
485
486     avcodec_get_frame_defaults(&s->frame);
487     avctx->coded_frame = &s->frame;
488
489     return 0;
490 }
491
492 #define C3 FIXHR(0.86602540378443864676/2)
493 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
494 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
495 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
496
497 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
498    cases. */
499 static void imdct12(INTFLOAT *out, INTFLOAT *in)
500 {
501     INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
502
503     in0  = in[0*3];
504     in1  = in[1*3] + in[0*3];
505     in2  = in[2*3] + in[1*3];
506     in3  = in[3*3] + in[2*3];
507     in4  = in[4*3] + in[3*3];
508     in5  = in[5*3] + in[4*3];
509     in5 += in3;
510     in3 += in1;
511
512     in2  = MULH3(in2, C3, 2);
513     in3  = MULH3(in3, C3, 4);
514
515     t1   = in0 - in4;
516     t2   = MULH3(in1 - in5, C4, 2);
517
518     out[ 7] =
519     out[10] = t1 + t2;
520     out[ 1] =
521     out[ 4] = t1 - t2;
522
523     in0    += SHR(in4, 1);
524     in4     = in0 + in2;
525     in5    += 2*in1;
526     in1     = MULH3(in5 + in3, C5, 1);
527     out[ 8] =
528     out[ 9] = in4 + in1;
529     out[ 2] =
530     out[ 3] = in4 - in1;
531
532     in0    -= in2;
533     in5     = MULH3(in5 - in3, C6, 2);
534     out[ 0] =
535     out[ 5] = in0 - in5;
536     out[ 6] =
537     out[11] = in0 + in5;
538 }
539
540 /* return the number of decoded frames */
541 static int mp_decode_layer1(MPADecodeContext *s)
542 {
543     int bound, i, v, n, ch, j, mant;
544     uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
545     uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
546
547     if (s->mode == MPA_JSTEREO)
548         bound = (s->mode_ext + 1) * 4;
549     else
550         bound = SBLIMIT;
551
552     /* allocation bits */
553     for (i = 0; i < bound; i++) {
554         for (ch = 0; ch < s->nb_channels; ch++) {
555             allocation[ch][i] = get_bits(&s->gb, 4);
556         }
557     }
558     for (i = bound; i < SBLIMIT; i++)
559         allocation[0][i] = get_bits(&s->gb, 4);
560
561     /* scale factors */
562     for (i = 0; i < bound; i++) {
563         for (ch = 0; ch < s->nb_channels; ch++) {
564             if (allocation[ch][i])
565                 scale_factors[ch][i] = get_bits(&s->gb, 6);
566         }
567     }
568     for (i = bound; i < SBLIMIT; i++) {
569         if (allocation[0][i]) {
570             scale_factors[0][i] = get_bits(&s->gb, 6);
571             scale_factors[1][i] = get_bits(&s->gb, 6);
572         }
573     }
574
575     /* compute samples */
576     for (j = 0; j < 12; j++) {
577         for (i = 0; i < bound; i++) {
578             for (ch = 0; ch < s->nb_channels; ch++) {
579                 n = allocation[ch][i];
580                 if (n) {
581                     mant = get_bits(&s->gb, n + 1);
582                     v = l1_unscale(n, mant, scale_factors[ch][i]);
583                 } else {
584                     v = 0;
585                 }
586                 s->sb_samples[ch][j][i] = v;
587             }
588         }
589         for (i = bound; i < SBLIMIT; i++) {
590             n = allocation[0][i];
591             if (n) {
592                 mant = get_bits(&s->gb, n + 1);
593                 v = l1_unscale(n, mant, scale_factors[0][i]);
594                 s->sb_samples[0][j][i] = v;
595                 v = l1_unscale(n, mant, scale_factors[1][i]);
596                 s->sb_samples[1][j][i] = v;
597             } else {
598                 s->sb_samples[0][j][i] = 0;
599                 s->sb_samples[1][j][i] = 0;
600             }
601         }
602     }
603     return 12;
604 }
605
606 static int mp_decode_layer2(MPADecodeContext *s)
607 {
608     int sblimit; /* number of used subbands */
609     const unsigned char *alloc_table;
610     int table, bit_alloc_bits, i, j, ch, bound, v;
611     unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
612     unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
613     unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
614     int scale, qindex, bits, steps, k, l, m, b;
615
616     /* select decoding table */
617     table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
618                                    s->sample_rate, s->lsf);
619     sblimit     = ff_mpa_sblimit_table[table];
620     alloc_table = ff_mpa_alloc_tables[table];
621
622     if (s->mode == MPA_JSTEREO)
623         bound = (s->mode_ext + 1) * 4;
624     else
625         bound = sblimit;
626
627     av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
628
629     /* sanity check */
630     if (bound > sblimit)
631         bound = sblimit;
632
633     /* parse bit allocation */
634     j = 0;
635     for (i = 0; i < bound; i++) {
636         bit_alloc_bits = alloc_table[j];
637         for (ch = 0; ch < s->nb_channels; ch++)
638             bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
639         j += 1 << bit_alloc_bits;
640     }
641     for (i = bound; i < sblimit; i++) {
642         bit_alloc_bits = alloc_table[j];
643         v = get_bits(&s->gb, bit_alloc_bits);
644         bit_alloc[0][i] = v;
645         bit_alloc[1][i] = v;
646         j += 1 << bit_alloc_bits;
647     }
648
649     /* scale codes */
650     for (i = 0; i < sblimit; i++) {
651         for (ch = 0; ch < s->nb_channels; ch++) {
652             if (bit_alloc[ch][i])
653                 scale_code[ch][i] = get_bits(&s->gb, 2);
654         }
655     }
656
657     /* scale factors */
658     for (i = 0; i < sblimit; i++) {
659         for (ch = 0; ch < s->nb_channels; ch++) {
660             if (bit_alloc[ch][i]) {
661                 sf = scale_factors[ch][i];
662                 switch (scale_code[ch][i]) {
663                 default:
664                 case 0:
665                     sf[0] = get_bits(&s->gb, 6);
666                     sf[1] = get_bits(&s->gb, 6);
667                     sf[2] = get_bits(&s->gb, 6);
668                     break;
669                 case 2:
670                     sf[0] = get_bits(&s->gb, 6);
671                     sf[1] = sf[0];
672                     sf[2] = sf[0];
673                     break;
674                 case 1:
675                     sf[0] = get_bits(&s->gb, 6);
676                     sf[2] = get_bits(&s->gb, 6);
677                     sf[1] = sf[0];
678                     break;
679                 case 3:
680                     sf[0] = get_bits(&s->gb, 6);
681                     sf[2] = get_bits(&s->gb, 6);
682                     sf[1] = sf[2];
683                     break;
684                 }
685             }
686         }
687     }
688
689     /* samples */
690     for (k = 0; k < 3; k++) {
691         for (l = 0; l < 12; l += 3) {
692             j = 0;
693             for (i = 0; i < bound; i++) {
694                 bit_alloc_bits = alloc_table[j];
695                 for (ch = 0; ch < s->nb_channels; ch++) {
696                     b = bit_alloc[ch][i];
697                     if (b) {
698                         scale = scale_factors[ch][i][k];
699                         qindex = alloc_table[j+b];
700                         bits = ff_mpa_quant_bits[qindex];
701                         if (bits < 0) {
702                             int v2;
703                             /* 3 values at the same time */
704                             v = get_bits(&s->gb, -bits);
705                             v2 = division_tabs[qindex][v];
706                             steps  = ff_mpa_quant_steps[qindex];
707
708                             s->sb_samples[ch][k * 12 + l + 0][i] =
709                                 l2_unscale_group(steps,  v2       & 15, scale);
710                             s->sb_samples[ch][k * 12 + l + 1][i] =
711                                 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
712                             s->sb_samples[ch][k * 12 + l + 2][i] =
713                                 l2_unscale_group(steps,  v2 >> 8      , scale);
714                         } else {
715                             for (m = 0; m < 3; m++) {
716                                 v = get_bits(&s->gb, bits);
717                                 v = l1_unscale(bits - 1, v, scale);
718                                 s->sb_samples[ch][k * 12 + l + m][i] = v;
719                             }
720                         }
721                     } else {
722                         s->sb_samples[ch][k * 12 + l + 0][i] = 0;
723                         s->sb_samples[ch][k * 12 + l + 1][i] = 0;
724                         s->sb_samples[ch][k * 12 + l + 2][i] = 0;
725                     }
726                 }
727                 /* next subband in alloc table */
728                 j += 1 << bit_alloc_bits;
729             }
730             /* XXX: find a way to avoid this duplication of code */
731             for (i = bound; i < sblimit; i++) {
732                 bit_alloc_bits = alloc_table[j];
733                 b = bit_alloc[0][i];
734                 if (b) {
735                     int mant, scale0, scale1;
736                     scale0 = scale_factors[0][i][k];
737                     scale1 = scale_factors[1][i][k];
738                     qindex = alloc_table[j+b];
739                     bits = ff_mpa_quant_bits[qindex];
740                     if (bits < 0) {
741                         /* 3 values at the same time */
742                         v = get_bits(&s->gb, -bits);
743                         steps = ff_mpa_quant_steps[qindex];
744                         mant = v % steps;
745                         v = v / steps;
746                         s->sb_samples[0][k * 12 + l + 0][i] =
747                             l2_unscale_group(steps, mant, scale0);
748                         s->sb_samples[1][k * 12 + l + 0][i] =
749                             l2_unscale_group(steps, mant, scale1);
750                         mant = v % steps;
751                         v = v / steps;
752                         s->sb_samples[0][k * 12 + l + 1][i] =
753                             l2_unscale_group(steps, mant, scale0);
754                         s->sb_samples[1][k * 12 + l + 1][i] =
755                             l2_unscale_group(steps, mant, scale1);
756                         s->sb_samples[0][k * 12 + l + 2][i] =
757                             l2_unscale_group(steps, v, scale0);
758                         s->sb_samples[1][k * 12 + l + 2][i] =
759                             l2_unscale_group(steps, v, scale1);
760                     } else {
761                         for (m = 0; m < 3; m++) {
762                             mant = get_bits(&s->gb, bits);
763                             s->sb_samples[0][k * 12 + l + m][i] =
764                                 l1_unscale(bits - 1, mant, scale0);
765                             s->sb_samples[1][k * 12 + l + m][i] =
766                                 l1_unscale(bits - 1, mant, scale1);
767                         }
768                     }
769                 } else {
770                     s->sb_samples[0][k * 12 + l + 0][i] = 0;
771                     s->sb_samples[0][k * 12 + l + 1][i] = 0;
772                     s->sb_samples[0][k * 12 + l + 2][i] = 0;
773                     s->sb_samples[1][k * 12 + l + 0][i] = 0;
774                     s->sb_samples[1][k * 12 + l + 1][i] = 0;
775                     s->sb_samples[1][k * 12 + l + 2][i] = 0;
776                 }
777                 /* next subband in alloc table */
778                 j += 1 << bit_alloc_bits;
779             }
780             /* fill remaining samples to zero */
781             for (i = sblimit; i < SBLIMIT; i++) {
782                 for (ch = 0; ch < s->nb_channels; ch++) {
783                     s->sb_samples[ch][k * 12 + l + 0][i] = 0;
784                     s->sb_samples[ch][k * 12 + l + 1][i] = 0;
785                     s->sb_samples[ch][k * 12 + l + 2][i] = 0;
786                 }
787             }
788         }
789     }
790     return 3 * 12;
791 }
792
793 #define SPLIT(dst,sf,n)             \
794     if (n == 3) {                   \
795         int m = (sf * 171) >> 9;    \
796         dst   = sf - 3 * m;         \
797         sf    = m;                  \
798     } else if (n == 4) {            \
799         dst  = sf & 3;              \
800         sf >>= 2;                   \
801     } else if (n == 5) {            \
802         int m = (sf * 205) >> 10;   \
803         dst   = sf - 5 * m;         \
804         sf    = m;                  \
805     } else if (n == 6) {            \
806         int m = (sf * 171) >> 10;   \
807         dst   = sf - 6 * m;         \
808         sf    = m;                  \
809     } else {                        \
810         dst = 0;                    \
811     }
812
813 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
814                                            int n3)
815 {
816     SPLIT(slen[3], sf, n3)
817     SPLIT(slen[2], sf, n2)
818     SPLIT(slen[1], sf, n1)
819     slen[0] = sf;
820 }
821
822 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
823                                          int16_t *exponents)
824 {
825     const uint8_t *bstab, *pretab;
826     int len, i, j, k, l, v0, shift, gain, gains[3];
827     int16_t *exp_ptr;
828
829     exp_ptr = exponents;
830     gain    = g->global_gain - 210;
831     shift   = g->scalefac_scale + 1;
832
833     bstab  = band_size_long[s->sample_rate_index];
834     pretab = mpa_pretab[g->preflag];
835     for (i = 0; i < g->long_end; i++) {
836         v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
837         len = bstab[i];
838         for (j = len; j > 0; j--)
839             *exp_ptr++ = v0;
840     }
841
842     if (g->short_start < 13) {
843         bstab    = band_size_short[s->sample_rate_index];
844         gains[0] = gain - (g->subblock_gain[0] << 3);
845         gains[1] = gain - (g->subblock_gain[1] << 3);
846         gains[2] = gain - (g->subblock_gain[2] << 3);
847         k        = g->long_end;
848         for (i = g->short_start; i < 13; i++) {
849             len = bstab[i];
850             for (l = 0; l < 3; l++) {
851                 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
852                 for (j = len; j > 0; j--)
853                     *exp_ptr++ = v0;
854             }
855         }
856     }
857 }
858
859 /* handle n = 0 too */
860 static inline int get_bitsz(GetBitContext *s, int n)
861 {
862     return n ? get_bits(s, n) : 0;
863 }
864
865
866 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
867                           int *end_pos2)
868 {
869     if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
870         s->gb           = s->in_gb;
871         s->in_gb.buffer = NULL;
872         assert((get_bits_count(&s->gb) & 7) == 0);
873         skip_bits_long(&s->gb, *pos - *end_pos);
874         *end_pos2 =
875         *end_pos  = *end_pos2 + get_bits_count(&s->gb) - *pos;
876         *pos      = get_bits_count(&s->gb);
877     }
878 }
879
880 /* Following is a optimized code for
881             INTFLOAT v = *src
882             if(get_bits1(&s->gb))
883                 v = -v;
884             *dst = v;
885 */
886 #if CONFIG_FLOAT
887 #define READ_FLIP_SIGN(dst,src)                     \
888     v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31);  \
889     AV_WN32A(dst, v);
890 #else
891 #define READ_FLIP_SIGN(dst,src)     \
892     v      = -get_bits1(&s->gb);    \
893     *(dst) = (*(src) ^ v) - v;
894 #endif
895
896 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
897                           int16_t *exponents, int end_pos2)
898 {
899     int s_index;
900     int i;
901     int last_pos, bits_left;
902     VLC *vlc;
903     int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
904
905     /* low frequencies (called big values) */
906     s_index = 0;
907     for (i = 0; i < 3; i++) {
908         int j, k, l, linbits;
909         j = g->region_size[i];
910         if (j == 0)
911             continue;
912         /* select vlc table */
913         k       = g->table_select[i];
914         l       = mpa_huff_data[k][0];
915         linbits = mpa_huff_data[k][1];
916         vlc     = &huff_vlc[l];
917
918         if (!l) {
919             memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
920             s_index += 2 * j;
921             continue;
922         }
923
924         /* read huffcode and compute each couple */
925         for (; j > 0; j--) {
926             int exponent, x, y;
927             int v;
928             int pos = get_bits_count(&s->gb);
929
930             if (pos >= end_pos){
931 //                av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
932                 switch_buffer(s, &pos, &end_pos, &end_pos2);
933 //                av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
934                 if (pos >= end_pos)
935                     break;
936             }
937             y = get_vlc2(&s->gb, vlc->table, 7, 3);
938
939             if (!y) {
940                 g->sb_hybrid[s_index  ] =
941                 g->sb_hybrid[s_index+1] = 0;
942                 s_index += 2;
943                 continue;
944             }
945
946             exponent= exponents[s_index];
947
948             av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
949                     i, g->region_size[i] - j, x, y, exponent);
950             if (y & 16) {
951                 x = y >> 5;
952                 y = y & 0x0f;
953                 if (x < 15) {
954                     READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
955                 } else {
956                     x += get_bitsz(&s->gb, linbits);
957                     v  = l3_unscale(x, exponent);
958                     if (get_bits1(&s->gb))
959                         v = -v;
960                     g->sb_hybrid[s_index] = v;
961                 }
962                 if (y < 15) {
963                     READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
964                 } else {
965                     y += get_bitsz(&s->gb, linbits);
966                     v  = l3_unscale(y, exponent);
967                     if (get_bits1(&s->gb))
968                         v = -v;
969                     g->sb_hybrid[s_index+1] = v;
970                 }
971             } else {
972                 x = y >> 5;
973                 y = y & 0x0f;
974                 x += y;
975                 if (x < 15) {
976                     READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
977                 } else {
978                     x += get_bitsz(&s->gb, linbits);
979                     v  = l3_unscale(x, exponent);
980                     if (get_bits1(&s->gb))
981                         v = -v;
982                     g->sb_hybrid[s_index+!!y] = v;
983                 }
984                 g->sb_hybrid[s_index + !y] = 0;
985             }
986             s_index += 2;
987         }
988     }
989
990     /* high frequencies */
991     vlc = &huff_quad_vlc[g->count1table_select];
992     last_pos = 0;
993     while (s_index <= 572) {
994         int pos, code;
995         pos = get_bits_count(&s->gb);
996         if (pos >= end_pos) {
997             if (pos > end_pos2 && last_pos) {
998                 /* some encoders generate an incorrect size for this
999                    part. We must go back into the data */
1000                 s_index -= 4;
1001                 skip_bits_long(&s->gb, last_pos - pos);
1002                 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1003                 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
1004                     s_index=0;
1005                 break;
1006             }
1007 //                av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1008             switch_buffer(s, &pos, &end_pos, &end_pos2);
1009 //                av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1010             if (pos >= end_pos)
1011                 break;
1012         }
1013         last_pos = pos;
1014
1015         code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1016         av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1017         g->sb_hybrid[s_index+0] =
1018         g->sb_hybrid[s_index+1] =
1019         g->sb_hybrid[s_index+2] =
1020         g->sb_hybrid[s_index+3] = 0;
1021         while (code) {
1022             static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
1023             int v;
1024             int pos = s_index + idxtab[code];
1025             code   ^= 8 >> idxtab[code];
1026             READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
1027         }
1028         s_index += 4;
1029     }
1030     /* skip extension bits */
1031     bits_left = end_pos2 - get_bits_count(&s->gb);
1032 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1033     if (bits_left < 0 && (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) {
1034         av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1035         s_index=0;
1036     } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE))) {
1037         av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1038         s_index = 0;
1039     }
1040     memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
1041     skip_bits_long(&s->gb, bits_left);
1042
1043     i = get_bits_count(&s->gb);
1044     switch_buffer(s, &i, &end_pos, &end_pos2);
1045
1046     return 0;
1047 }
1048
1049 /* Reorder short blocks from bitstream order to interleaved order. It
1050    would be faster to do it in parsing, but the code would be far more
1051    complicated */
1052 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1053 {
1054     int i, j, len;
1055     INTFLOAT *ptr, *dst, *ptr1;
1056     INTFLOAT tmp[576];
1057
1058     if (g->block_type != 2)
1059         return;
1060
1061     if (g->switch_point) {
1062         if (s->sample_rate_index != 8)
1063             ptr = g->sb_hybrid + 36;
1064         else
1065             ptr = g->sb_hybrid + 48;
1066     } else {
1067         ptr = g->sb_hybrid;
1068     }
1069
1070     for (i = g->short_start; i < 13; i++) {
1071         len  = band_size_short[s->sample_rate_index][i];
1072         ptr1 = ptr;
1073         dst  = tmp;
1074         for (j = len; j > 0; j--) {
1075             *dst++ = ptr[0*len];
1076             *dst++ = ptr[1*len];
1077             *dst++ = ptr[2*len];
1078             ptr++;
1079         }
1080         ptr += 2 * len;
1081         memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1082     }
1083 }
1084
1085 #define ISQRT2 FIXR(0.70710678118654752440)
1086
1087 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1088 {
1089     int i, j, k, l;
1090     int sf_max, sf, len, non_zero_found;
1091     INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1092     int non_zero_found_short[3];
1093
1094     /* intensity stereo */
1095     if (s->mode_ext & MODE_EXT_I_STEREO) {
1096         if (!s->lsf) {
1097             is_tab = is_table;
1098             sf_max = 7;
1099         } else {
1100             is_tab = is_table_lsf[g1->scalefac_compress & 1];
1101             sf_max = 16;
1102         }
1103
1104         tab0 = g0->sb_hybrid + 576;
1105         tab1 = g1->sb_hybrid + 576;
1106
1107         non_zero_found_short[0] = 0;
1108         non_zero_found_short[1] = 0;
1109         non_zero_found_short[2] = 0;
1110         k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1111         for (i = 12; i >= g1->short_start; i--) {
1112             /* for last band, use previous scale factor */
1113             if (i != 11)
1114                 k -= 3;
1115             len = band_size_short[s->sample_rate_index][i];
1116             for (l = 2; l >= 0; l--) {
1117                 tab0 -= len;
1118                 tab1 -= len;
1119                 if (!non_zero_found_short[l]) {
1120                     /* test if non zero band. if so, stop doing i-stereo */
1121                     for (j = 0; j < len; j++) {
1122                         if (tab1[j] != 0) {
1123                             non_zero_found_short[l] = 1;
1124                             goto found1;
1125                         }
1126                     }
1127                     sf = g1->scale_factors[k + l];
1128                     if (sf >= sf_max)
1129                         goto found1;
1130
1131                     v1 = is_tab[0][sf];
1132                     v2 = is_tab[1][sf];
1133                     for (j = 0; j < len; j++) {
1134                         tmp0    = tab0[j];
1135                         tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1136                         tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1137                     }
1138                 } else {
1139 found1:
1140                     if (s->mode_ext & MODE_EXT_MS_STEREO) {
1141                         /* lower part of the spectrum : do ms stereo
1142                            if enabled */
1143                         for (j = 0; j < len; j++) {
1144                             tmp0    = tab0[j];
1145                             tmp1    = tab1[j];
1146                             tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1147                             tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1148                         }
1149                     }
1150                 }
1151             }
1152         }
1153
1154         non_zero_found = non_zero_found_short[0] |
1155                          non_zero_found_short[1] |
1156                          non_zero_found_short[2];
1157
1158         for (i = g1->long_end - 1;i >= 0;i--) {
1159             len   = band_size_long[s->sample_rate_index][i];
1160             tab0 -= len;
1161             tab1 -= len;
1162             /* test if non zero band. if so, stop doing i-stereo */
1163             if (!non_zero_found) {
1164                 for (j = 0; j < len; j++) {
1165                     if (tab1[j] != 0) {
1166                         non_zero_found = 1;
1167                         goto found2;
1168                     }
1169                 }
1170                 /* for last band, use previous scale factor */
1171                 k  = (i == 21) ? 20 : i;
1172                 sf = g1->scale_factors[k];
1173                 if (sf >= sf_max)
1174                     goto found2;
1175                 v1 = is_tab[0][sf];
1176                 v2 = is_tab[1][sf];
1177                 for (j = 0; j < len; j++) {
1178                     tmp0    = tab0[j];
1179                     tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1180                     tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1181                 }
1182             } else {
1183 found2:
1184                 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1185                     /* lower part of the spectrum : do ms stereo
1186                        if enabled */
1187                     for (j = 0; j < len; j++) {
1188                         tmp0    = tab0[j];
1189                         tmp1    = tab1[j];
1190                         tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1191                         tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1192                     }
1193                 }
1194             }
1195         }
1196     } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1197         /* ms stereo ONLY */
1198         /* NOTE: the 1/sqrt(2) normalization factor is included in the
1199            global gain */
1200         tab0 = g0->sb_hybrid;
1201         tab1 = g1->sb_hybrid;
1202         for (i = 0; i < 576; i++) {
1203             tmp0    = tab0[i];
1204             tmp1    = tab1[i];
1205             tab0[i] = tmp0 + tmp1;
1206             tab1[i] = tmp0 - tmp1;
1207         }
1208     }
1209 }
1210
1211 #if CONFIG_FLOAT
1212 #define AA(j) do {                                                      \
1213         float tmp0 = ptr[-1-j];                                         \
1214         float tmp1 = ptr[   j];                                         \
1215         ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1];    \
1216         ptr[   j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0];    \
1217     } while (0)
1218 #else
1219 #define AA(j) do {                                              \
1220         int tmp0 = ptr[-1-j];                                   \
1221         int tmp1 = ptr[   j];                                   \
1222         int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]);          \
1223         ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2]));   \
1224         ptr[   j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3]));   \
1225     } while (0)
1226 #endif
1227
1228 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1229 {
1230     INTFLOAT *ptr;
1231     int n, i;
1232
1233     /* we antialias only "long" bands */
1234     if (g->block_type == 2) {
1235         if (!g->switch_point)
1236             return;
1237         /* XXX: check this for 8000Hz case */
1238         n = 1;
1239     } else {
1240         n = SBLIMIT - 1;
1241     }
1242
1243     ptr = g->sb_hybrid + 18;
1244     for (i = n; i > 0; i--) {
1245         AA(0);
1246         AA(1);
1247         AA(2);
1248         AA(3);
1249         AA(4);
1250         AA(5);
1251         AA(6);
1252         AA(7);
1253
1254         ptr += 18;
1255     }
1256 }
1257
1258 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1259                           INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1260 {
1261     INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1262     INTFLOAT out2[12];
1263     int i, j, mdct_long_end, sblimit;
1264
1265     /* find last non zero block */
1266     ptr  = g->sb_hybrid + 576;
1267     ptr1 = g->sb_hybrid + 2 * 18;
1268     while (ptr >= ptr1) {
1269         int32_t *p;
1270         ptr -= 6;
1271         p    = (int32_t*)ptr;
1272         if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1273             break;
1274     }
1275     sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1276
1277     if (g->block_type == 2) {
1278         /* XXX: check for 8000 Hz */
1279         if (g->switch_point)
1280             mdct_long_end = 2;
1281         else
1282             mdct_long_end = 0;
1283     } else {
1284         mdct_long_end = sblimit;
1285     }
1286
1287     buf = mdct_buf;
1288     ptr = g->sb_hybrid;
1289     for (j = 0; j < mdct_long_end; j++) {
1290         int win_idx = (g->switch_point && j < 2) ? 0 : g->block_type;
1291         /* apply window & overlap with previous buffer */
1292         out_ptr = sb_samples + j;
1293         /* select window */
1294         win = mdct_win[win_idx + (4 & -(j & 1))];
1295         s->mpadsp.RENAME(imdct36)(out_ptr, buf, ptr, win);
1296         out_ptr += 18 * SBLIMIT;
1297         ptr     += 18;
1298         buf     += 18;
1299     }
1300     for (j = mdct_long_end; j < sblimit; j++) {
1301         /* select frequency inversion */
1302         win     = mdct_win[2 + (4  & -(j & 1))];
1303         out_ptr = sb_samples + j;
1304
1305         for (i = 0; i < 6; i++) {
1306             *out_ptr = buf[i];
1307             out_ptr += SBLIMIT;
1308         }
1309         imdct12(out2, ptr + 0);
1310         for (i = 0; i < 6; i++) {
1311             *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*1];
1312             buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1313             out_ptr += SBLIMIT;
1314         }
1315         imdct12(out2, ptr + 1);
1316         for (i = 0; i < 6; i++) {
1317             *out_ptr     = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*2];
1318             buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1319             out_ptr += SBLIMIT;
1320         }
1321         imdct12(out2, ptr + 2);
1322         for (i = 0; i < 6; i++) {
1323             buf[i + 6*0] = MULH3(out2[i    ], win[i    ], 1) + buf[i + 6*0];
1324             buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1325             buf[i + 6*2] = 0;
1326         }
1327         ptr += 18;
1328         buf += 18;
1329     }
1330     /* zero bands */
1331     for (j = sblimit; j < SBLIMIT; j++) {
1332         /* overlap */
1333         out_ptr = sb_samples + j;
1334         for (i = 0; i < 18; i++) {
1335             *out_ptr = buf[i];
1336             buf[i]   = 0;
1337             out_ptr += SBLIMIT;
1338         }
1339         buf += 18;
1340     }
1341 }
1342
1343 /* main layer3 decoding function */
1344 static int mp_decode_layer3(MPADecodeContext *s)
1345 {
1346     int nb_granules, main_data_begin;
1347     int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1348     GranuleDef *g;
1349     int16_t exponents[576]; //FIXME try INTFLOAT
1350
1351     /* read side info */
1352     if (s->lsf) {
1353         main_data_begin = get_bits(&s->gb, 8);
1354         skip_bits(&s->gb, s->nb_channels);
1355         nb_granules = 1;
1356     } else {
1357         main_data_begin = get_bits(&s->gb, 9);
1358         if (s->nb_channels == 2)
1359             skip_bits(&s->gb, 3);
1360         else
1361             skip_bits(&s->gb, 5);
1362         nb_granules = 2;
1363         for (ch = 0; ch < s->nb_channels; ch++) {
1364             s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1365             s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1366         }
1367     }
1368
1369     for (gr = 0; gr < nb_granules; gr++) {
1370         for (ch = 0; ch < s->nb_channels; ch++) {
1371             av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1372             g = &s->granules[ch][gr];
1373             g->part2_3_length = get_bits(&s->gb, 12);
1374             g->big_values     = get_bits(&s->gb,  9);
1375             if (g->big_values > 288) {
1376                 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1377                 return AVERROR_INVALIDDATA;
1378             }
1379
1380             g->global_gain = get_bits(&s->gb, 8);
1381             /* if MS stereo only is selected, we precompute the
1382                1/sqrt(2) renormalization factor */
1383             if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1384                 MODE_EXT_MS_STEREO)
1385                 g->global_gain -= 2;
1386             if (s->lsf)
1387                 g->scalefac_compress = get_bits(&s->gb, 9);
1388             else
1389                 g->scalefac_compress = get_bits(&s->gb, 4);
1390             blocksplit_flag = get_bits1(&s->gb);
1391             if (blocksplit_flag) {
1392                 g->block_type = get_bits(&s->gb, 2);
1393                 if (g->block_type == 0) {
1394                     av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1395                     return AVERROR_INVALIDDATA;
1396                 }
1397                 g->switch_point = get_bits1(&s->gb);
1398                 for (i = 0; i < 2; i++)
1399                     g->table_select[i] = get_bits(&s->gb, 5);
1400                 for (i = 0; i < 3; i++)
1401                     g->subblock_gain[i] = get_bits(&s->gb, 3);
1402                 ff_init_short_region(s, g);
1403             } else {
1404                 int region_address1, region_address2;
1405                 g->block_type = 0;
1406                 g->switch_point = 0;
1407                 for (i = 0; i < 3; i++)
1408                     g->table_select[i] = get_bits(&s->gb, 5);
1409                 /* compute huffman coded region sizes */
1410                 region_address1 = get_bits(&s->gb, 4);
1411                 region_address2 = get_bits(&s->gb, 3);
1412                 av_dlog(s->avctx, "region1=%d region2=%d\n",
1413                         region_address1, region_address2);
1414                 ff_init_long_region(s, g, region_address1, region_address2);
1415             }
1416             ff_region_offset2size(g);
1417             ff_compute_band_indexes(s, g);
1418
1419             g->preflag = 0;
1420             if (!s->lsf)
1421                 g->preflag = get_bits1(&s->gb);
1422             g->scalefac_scale     = get_bits1(&s->gb);
1423             g->count1table_select = get_bits1(&s->gb);
1424             av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1425                     g->block_type, g->switch_point);
1426         }
1427     }
1428
1429     if (!s->adu_mode) {
1430         int skip;
1431         const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1432         assert((get_bits_count(&s->gb) & 7) == 0);
1433         /* now we get bits from the main_data_begin offset */
1434         av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1435     //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1436
1437         memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1438         s->in_gb = s->gb;
1439         init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1440 #if !UNCHECKED_BITSTREAM_READER
1441         s->gb.size_in_bits_plus8 += EXTRABYTES * 8;
1442 #endif
1443         skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1444     }
1445
1446     for (gr = 0; gr < nb_granules; gr++) {
1447         for (ch = 0; ch < s->nb_channels; ch++) {
1448             g = &s->granules[ch][gr];
1449             if (get_bits_count(&s->gb) < 0) {
1450                 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1451                        main_data_begin, s->last_buf_size, gr);
1452                 skip_bits_long(&s->gb, g->part2_3_length);
1453                 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1454                 if (get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer) {
1455                     skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1456                     s->gb           = s->in_gb;
1457                     s->in_gb.buffer = NULL;
1458                 }
1459                 continue;
1460             }
1461
1462             bits_pos = get_bits_count(&s->gb);
1463
1464             if (!s->lsf) {
1465                 uint8_t *sc;
1466                 int slen, slen1, slen2;
1467
1468                 /* MPEG1 scale factors */
1469                 slen1 = slen_table[0][g->scalefac_compress];
1470                 slen2 = slen_table[1][g->scalefac_compress];
1471                 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1472                 if (g->block_type == 2) {
1473                     n = g->switch_point ? 17 : 18;
1474                     j = 0;
1475                     if (slen1) {
1476                         for (i = 0; i < n; i++)
1477                             g->scale_factors[j++] = get_bits(&s->gb, slen1);
1478                     } else {
1479                         for (i = 0; i < n; i++)
1480                             g->scale_factors[j++] = 0;
1481                     }
1482                     if (slen2) {
1483                         for (i = 0; i < 18; i++)
1484                             g->scale_factors[j++] = get_bits(&s->gb, slen2);
1485                         for (i = 0; i < 3; i++)
1486                             g->scale_factors[j++] = 0;
1487                     } else {
1488                         for (i = 0; i < 21; i++)
1489                             g->scale_factors[j++] = 0;
1490                     }
1491                 } else {
1492                     sc = s->granules[ch][0].scale_factors;
1493                     j = 0;
1494                     for (k = 0; k < 4; k++) {
1495                         n = k == 0 ? 6 : 5;
1496                         if ((g->scfsi & (0x8 >> k)) == 0) {
1497                             slen = (k < 2) ? slen1 : slen2;
1498                             if (slen) {
1499                                 for (i = 0; i < n; i++)
1500                                     g->scale_factors[j++] = get_bits(&s->gb, slen);
1501                             } else {
1502                                 for (i = 0; i < n; i++)
1503                                     g->scale_factors[j++] = 0;
1504                             }
1505                         } else {
1506                             /* simply copy from last granule */
1507                             for (i = 0; i < n; i++) {
1508                                 g->scale_factors[j] = sc[j];
1509                                 j++;
1510                             }
1511                         }
1512                     }
1513                     g->scale_factors[j++] = 0;
1514                 }
1515             } else {
1516                 int tindex, tindex2, slen[4], sl, sf;
1517
1518                 /* LSF scale factors */
1519                 if (g->block_type == 2)
1520                     tindex = g->switch_point ? 2 : 1;
1521                 else
1522                     tindex = 0;
1523
1524                 sf = g->scalefac_compress;
1525                 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1526                     /* intensity stereo case */
1527                     sf >>= 1;
1528                     if (sf < 180) {
1529                         lsf_sf_expand(slen, sf, 6, 6, 0);
1530                         tindex2 = 3;
1531                     } else if (sf < 244) {
1532                         lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1533                         tindex2 = 4;
1534                     } else {
1535                         lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1536                         tindex2 = 5;
1537                     }
1538                 } else {
1539                     /* normal case */
1540                     if (sf < 400) {
1541                         lsf_sf_expand(slen, sf, 5, 4, 4);
1542                         tindex2 = 0;
1543                     } else if (sf < 500) {
1544                         lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1545                         tindex2 = 1;
1546                     } else {
1547                         lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1548                         tindex2 = 2;
1549                         g->preflag = 1;
1550                     }
1551                 }
1552
1553                 j = 0;
1554                 for (k = 0; k < 4; k++) {
1555                     n  = lsf_nsf_table[tindex2][tindex][k];
1556                     sl = slen[k];
1557                     if (sl) {
1558                         for (i = 0; i < n; i++)
1559                             g->scale_factors[j++] = get_bits(&s->gb, sl);
1560                     } else {
1561                         for (i = 0; i < n; i++)
1562                             g->scale_factors[j++] = 0;
1563                     }
1564                 }
1565                 /* XXX: should compute exact size */
1566                 for (; j < 40; j++)
1567                     g->scale_factors[j] = 0;
1568             }
1569
1570             exponents_from_scale_factors(s, g, exponents);
1571
1572             /* read Huffman coded residue */
1573             huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1574         } /* ch */
1575
1576         if (s->nb_channels == 2)
1577             compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1578
1579         for (ch = 0; ch < s->nb_channels; ch++) {
1580             g = &s->granules[ch][gr];
1581
1582             reorder_block(s, g);
1583             compute_antialias(s, g);
1584             compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1585         }
1586     } /* gr */
1587     if (get_bits_count(&s->gb) < 0)
1588         skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1589     return nb_granules * 18;
1590 }
1591
1592 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1593                            const uint8_t *buf, int buf_size)
1594 {
1595     int i, nb_frames, ch, ret;
1596     OUT_INT *samples_ptr;
1597
1598     init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1599
1600     /* skip error protection field */
1601     if (s->error_protection)
1602         skip_bits(&s->gb, 16);
1603
1604     switch(s->layer) {
1605     case 1:
1606         s->avctx->frame_size = 384;
1607         nb_frames = mp_decode_layer1(s);
1608         break;
1609     case 2:
1610         s->avctx->frame_size = 1152;
1611         nb_frames = mp_decode_layer2(s);
1612         break;
1613     case 3:
1614         s->avctx->frame_size = s->lsf ? 576 : 1152;
1615     default:
1616         nb_frames = mp_decode_layer3(s);
1617
1618         s->last_buf_size=0;
1619         if (s->in_gb.buffer) {
1620             align_get_bits(&s->gb);
1621             i = get_bits_left(&s->gb)>>3;
1622             if (i >= 0 && i <= BACKSTEP_SIZE) {
1623                 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1624                 s->last_buf_size=i;
1625             } else
1626                 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1627             s->gb           = s->in_gb;
1628             s->in_gb.buffer = NULL;
1629         }
1630
1631         align_get_bits(&s->gb);
1632         assert((get_bits_count(&s->gb) & 7) == 0);
1633         i = get_bits_left(&s->gb) >> 3;
1634
1635         if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1636             if (i < 0)
1637                 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1638             i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1639         }
1640         assert(i <= buf_size - HEADER_SIZE && i >= 0);
1641         memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1642         s->last_buf_size += i;
1643     }
1644
1645     /* get output buffer */
1646     if (!samples) {
1647         s->frame.nb_samples = s->avctx->frame_size;
1648         if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1649             av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1650             return ret;
1651         }
1652         samples = (OUT_INT *)s->frame.data[0];
1653     }
1654
1655     /* apply the synthesis filter */
1656     for (ch = 0; ch < s->nb_channels; ch++) {
1657         samples_ptr = samples + ch;
1658         for (i = 0; i < nb_frames; i++) {
1659             RENAME(ff_mpa_synth_filter)(
1660                          &s->mpadsp,
1661                          s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1662                          RENAME(ff_mpa_synth_window), &s->dither_state,
1663                          samples_ptr, s->nb_channels,
1664                          s->sb_samples[ch][i]);
1665             samples_ptr += 32 * s->nb_channels;
1666         }
1667     }
1668
1669     return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1670 }
1671
1672 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1673                         AVPacket *avpkt)
1674 {
1675     const uint8_t *buf  = avpkt->data;
1676     int buf_size        = avpkt->size;
1677     MPADecodeContext *s = avctx->priv_data;
1678     uint32_t header;
1679     int out_size;
1680
1681     if (buf_size < HEADER_SIZE)
1682         return AVERROR_INVALIDDATA;
1683
1684     header = AV_RB32(buf);
1685     if (ff_mpa_check_header(header) < 0) {
1686         av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1687         return AVERROR_INVALIDDATA;
1688     }
1689
1690     if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1691         /* free format: prepare to compute frame size */
1692         s->frame_size = -1;
1693         return AVERROR_INVALIDDATA;
1694     }
1695     /* update codec info */
1696     avctx->channels       = s->nb_channels;
1697     avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1698     if (!avctx->bit_rate)
1699         avctx->bit_rate = s->bit_rate;
1700     avctx->sub_id = s->layer;
1701
1702     if (s->frame_size <= 0 || s->frame_size > buf_size) {
1703         av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1704         return AVERROR_INVALIDDATA;
1705     }else if(s->frame_size < buf_size){
1706         av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1707         buf_size= s->frame_size;
1708     }
1709
1710     out_size = mp_decode_frame(s, NULL, buf, buf_size);
1711     if (out_size >= 0) {
1712         *got_frame_ptr   = 1;
1713         *(AVFrame *)data = s->frame;
1714         avctx->sample_rate = s->sample_rate;
1715         //FIXME maybe move the other codec info stuff from above here too
1716     } else {
1717         av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1718         /* Only return an error if the bad frame makes up the whole packet.
1719            If there is more data in the packet, just consume the bad frame
1720            instead of returning an error, which would discard the whole
1721            packet. */
1722         *got_frame_ptr = 0;
1723         if (buf_size == avpkt->size)
1724             return out_size;
1725     }
1726     s->frame_size = 0;
1727     return buf_size;
1728 }
1729
1730 static void flush(AVCodecContext *avctx)
1731 {
1732     MPADecodeContext *s = avctx->priv_data;
1733     memset(s->synth_buf, 0, sizeof(s->synth_buf));
1734     s->last_buf_size = 0;
1735 }
1736
1737 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1738 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1739                             int *got_frame_ptr, AVPacket *avpkt)
1740 {
1741     const uint8_t *buf  = avpkt->data;
1742     int buf_size        = avpkt->size;
1743     MPADecodeContext *s = avctx->priv_data;
1744     uint32_t header;
1745     int len, out_size;
1746
1747     len = buf_size;
1748
1749     // Discard too short frames
1750     if (buf_size < HEADER_SIZE) {
1751         av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1752         return AVERROR_INVALIDDATA;
1753     }
1754
1755
1756     if (len > MPA_MAX_CODED_FRAME_SIZE)
1757         len = MPA_MAX_CODED_FRAME_SIZE;
1758
1759     // Get header and restore sync word
1760     header = AV_RB32(buf) | 0xffe00000;
1761
1762     if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1763         av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1764         return AVERROR_INVALIDDATA;
1765     }
1766
1767     avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1768     /* update codec info */
1769     avctx->sample_rate = s->sample_rate;
1770     avctx->channels    = s->nb_channels;
1771     if (!avctx->bit_rate)
1772         avctx->bit_rate = s->bit_rate;
1773     avctx->sub_id = s->layer;
1774
1775     s->frame_size = len;
1776
1777 #if FF_API_PARSE_FRAME
1778     if (avctx->parse_only)
1779         out_size = buf_size;
1780     else
1781 #endif
1782     out_size = mp_decode_frame(s, NULL, buf, buf_size);
1783
1784     *got_frame_ptr   = 1;
1785     *(AVFrame *)data = s->frame;
1786
1787     return buf_size;
1788 }
1789 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1790
1791 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1792
1793 /**
1794  * Context for MP3On4 decoder
1795  */
1796 typedef struct MP3On4DecodeContext {
1797     AVFrame *frame;
1798     int frames;                     ///< number of mp3 frames per block (number of mp3 decoder instances)
1799     int syncword;                   ///< syncword patch
1800     const uint8_t *coff;            ///< channel offsets in output buffer
1801     MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1802     OUT_INT *decoded_buf;           ///< output buffer for decoded samples
1803 } MP3On4DecodeContext;
1804
1805 #include "mpeg4audio.h"
1806
1807 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1808
1809 /* number of mp3 decoder instances */
1810 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1811
1812 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1813 static const uint8_t chan_offset[8][5] = {
1814     { 0             },
1815     { 0             },  // C
1816     { 0             },  // FLR
1817     { 2, 0          },  // C FLR
1818     { 2, 0, 3       },  // C FLR BS
1819     { 2, 0, 3       },  // C FLR BLRS
1820     { 2, 0, 4, 3    },  // C FLR BLRS LFE
1821     { 2, 0, 6, 4, 3 },  // C FLR BLRS BLR LFE
1822 };
1823
1824 /* mp3on4 channel layouts */
1825 static const int16_t chan_layout[8] = {
1826     0,
1827     AV_CH_LAYOUT_MONO,
1828     AV_CH_LAYOUT_STEREO,
1829     AV_CH_LAYOUT_SURROUND,
1830     AV_CH_LAYOUT_4POINT0,
1831     AV_CH_LAYOUT_5POINT0,
1832     AV_CH_LAYOUT_5POINT1,
1833     AV_CH_LAYOUT_7POINT1
1834 };
1835
1836 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1837 {
1838     MP3On4DecodeContext *s = avctx->priv_data;
1839     int i;
1840
1841     for (i = 0; i < s->frames; i++)
1842         av_free(s->mp3decctx[i]);
1843
1844     av_freep(&s->decoded_buf);
1845
1846     return 0;
1847 }
1848
1849
1850 static int decode_init_mp3on4(AVCodecContext * avctx)
1851 {
1852     MP3On4DecodeContext *s = avctx->priv_data;
1853     MPEG4AudioConfig cfg;
1854     int i;
1855
1856     if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1857         av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1858         return AVERROR_INVALIDDATA;
1859     }
1860
1861     avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1862                                  avctx->extradata_size * 8, 1);
1863     if (!cfg.chan_config || cfg.chan_config > 7) {
1864         av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1865         return AVERROR_INVALIDDATA;
1866     }
1867     s->frames             = mp3Frames[cfg.chan_config];
1868     s->coff               = chan_offset[cfg.chan_config];
1869     avctx->channels       = ff_mpeg4audio_channels[cfg.chan_config];
1870     avctx->channel_layout = chan_layout[cfg.chan_config];
1871
1872     if (cfg.sample_rate < 16000)
1873         s->syncword = 0xffe00000;
1874     else
1875         s->syncword = 0xfff00000;
1876
1877     /* Init the first mp3 decoder in standard way, so that all tables get builded
1878      * We replace avctx->priv_data with the context of the first decoder so that
1879      * decode_init() does not have to be changed.
1880      * Other decoders will be initialized here copying data from the first context
1881      */
1882     // Allocate zeroed memory for the first decoder context
1883     s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1884     if (!s->mp3decctx[0])
1885         goto alloc_fail;
1886     // Put decoder context in place to make init_decode() happy
1887     avctx->priv_data = s->mp3decctx[0];
1888     decode_init(avctx);
1889     s->frame = avctx->coded_frame;
1890     // Restore mp3on4 context pointer
1891     avctx->priv_data = s;
1892     s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1893
1894     /* Create a separate codec/context for each frame (first is already ok).
1895      * Each frame is 1 or 2 channels - up to 5 frames allowed
1896      */
1897     for (i = 1; i < s->frames; i++) {
1898         s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1899         if (!s->mp3decctx[i])
1900             goto alloc_fail;
1901         s->mp3decctx[i]->adu_mode = 1;
1902         s->mp3decctx[i]->avctx = avctx;
1903         s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1904     }
1905
1906     /* Allocate buffer for multi-channel output if needed */
1907     if (s->frames > 1) {
1908         s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1909                                    sizeof(*s->decoded_buf));
1910         if (!s->decoded_buf)
1911             goto alloc_fail;
1912     }
1913
1914     return 0;
1915 alloc_fail:
1916     decode_close_mp3on4(avctx);
1917     return AVERROR(ENOMEM);
1918 }
1919
1920
1921 static void flush_mp3on4(AVCodecContext *avctx)
1922 {
1923     int i;
1924     MP3On4DecodeContext *s = avctx->priv_data;
1925
1926     for (i = 0; i < s->frames; i++) {
1927         MPADecodeContext *m = s->mp3decctx[i];
1928         memset(m->synth_buf, 0, sizeof(m->synth_buf));
1929         m->last_buf_size = 0;
1930     }
1931 }
1932
1933
1934 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1935                                int *got_frame_ptr, AVPacket *avpkt)
1936 {
1937     const uint8_t *buf     = avpkt->data;
1938     int buf_size           = avpkt->size;
1939     MP3On4DecodeContext *s = avctx->priv_data;
1940     MPADecodeContext *m;
1941     int fsize, len = buf_size, out_size = 0;
1942     uint32_t header;
1943     OUT_INT *out_samples;
1944     OUT_INT *outptr, *bp;
1945     int fr, j, n, ch, ret;
1946
1947     /* get output buffer */
1948     s->frame->nb_samples = MPA_FRAME_SIZE;
1949     if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1950         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1951         return ret;
1952     }
1953     out_samples = (OUT_INT *)s->frame->data[0];
1954
1955     // Discard too short frames
1956     if (buf_size < HEADER_SIZE)
1957         return AVERROR_INVALIDDATA;
1958
1959     // If only one decoder interleave is not needed
1960     outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1961
1962     avctx->bit_rate = 0;
1963
1964     ch = 0;
1965     for (fr = 0; fr < s->frames; fr++) {
1966         fsize = AV_RB16(buf) >> 4;
1967         fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1968         m     = s->mp3decctx[fr];
1969         assert(m != NULL);
1970
1971         header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1972
1973         if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1974             break;
1975
1976         avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1977
1978         if (ch + m->nb_channels > avctx->channels) {
1979             av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1980                                         "channel count\n");
1981             return AVERROR_INVALIDDATA;
1982         }
1983         ch += m->nb_channels;
1984
1985         out_size += mp_decode_frame(m, outptr, buf, fsize);
1986         buf      += fsize;
1987         len      -= fsize;
1988
1989         if (s->frames > 1) {
1990             n = m->avctx->frame_size*m->nb_channels;
1991             /* interleave output data */
1992             bp = out_samples + s->coff[fr];
1993             if (m->nb_channels == 1) {
1994                 for (j = 0; j < n; j++) {
1995                     *bp = s->decoded_buf[j];
1996                     bp += avctx->channels;
1997                 }
1998             } else {
1999                 for (j = 0; j < n; j++) {
2000                     bp[0] = s->decoded_buf[j++];
2001                     bp[1] = s->decoded_buf[j];
2002                     bp   += avctx->channels;
2003                 }
2004             }
2005         }
2006         avctx->bit_rate += m->bit_rate;
2007     }
2008
2009     /* update codec info */
2010     avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2011
2012     s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
2013     *got_frame_ptr   = 1;
2014     *(AVFrame *)data = *s->frame;
2015
2016     return buf_size;
2017 }
2018 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2019
2020 #if !CONFIG_FLOAT
2021 #if CONFIG_MP1_DECODER
2022 AVCodec ff_mp1_decoder = {
2023     .name           = "mp1",
2024     .type           = AVMEDIA_TYPE_AUDIO,
2025     .id             = CODEC_ID_MP1,
2026     .priv_data_size = sizeof(MPADecodeContext),
2027     .init           = decode_init,
2028     .decode         = decode_frame,
2029 #if FF_API_PARSE_FRAME
2030     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2031 #else
2032     .capabilities   = CODEC_CAP_DR1,
2033 #endif
2034     .flush          = flush,
2035     .long_name      = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2036 };
2037 #endif
2038 #if CONFIG_MP2_DECODER
2039 AVCodec ff_mp2_decoder = {
2040     .name           = "mp2",
2041     .type           = AVMEDIA_TYPE_AUDIO,
2042     .id             = CODEC_ID_MP2,
2043     .priv_data_size = sizeof(MPADecodeContext),
2044     .init           = decode_init,
2045     .decode         = decode_frame,
2046 #if FF_API_PARSE_FRAME
2047     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2048 #else
2049     .capabilities   = CODEC_CAP_DR1,
2050 #endif
2051     .flush          = flush,
2052     .long_name      = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2053 };
2054 #endif
2055 #if CONFIG_MP3_DECODER
2056 AVCodec ff_mp3_decoder = {
2057     .name           = "mp3",
2058     .type           = AVMEDIA_TYPE_AUDIO,
2059     .id             = CODEC_ID_MP3,
2060     .priv_data_size = sizeof(MPADecodeContext),
2061     .init           = decode_init,
2062     .decode         = decode_frame,
2063 #if FF_API_PARSE_FRAME
2064     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2065 #else
2066     .capabilities   = CODEC_CAP_DR1,
2067 #endif
2068     .flush          = flush,
2069     .long_name      = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2070 };
2071 #endif
2072 #if CONFIG_MP3ADU_DECODER
2073 AVCodec ff_mp3adu_decoder = {
2074     .name           = "mp3adu",
2075     .type           = AVMEDIA_TYPE_AUDIO,
2076     .id             = CODEC_ID_MP3ADU,
2077     .priv_data_size = sizeof(MPADecodeContext),
2078     .init           = decode_init,
2079     .decode         = decode_frame_adu,
2080 #if FF_API_PARSE_FRAME
2081     .capabilities   = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2082 #else
2083     .capabilities   = CODEC_CAP_DR1,
2084 #endif
2085     .flush          = flush,
2086     .long_name      = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2087 };
2088 #endif
2089 #if CONFIG_MP3ON4_DECODER
2090 AVCodec ff_mp3on4_decoder = {
2091     .name           = "mp3on4",
2092     .type           = AVMEDIA_TYPE_AUDIO,
2093     .id             = CODEC_ID_MP3ON4,
2094     .priv_data_size = sizeof(MP3On4DecodeContext),
2095     .init           = decode_init_mp3on4,
2096     .close          = decode_close_mp3on4,
2097     .decode         = decode_frame_mp3on4,
2098     .capabilities   = CODEC_CAP_DR1,
2099     .flush          = flush_mp3on4,
2100     .long_name      = NULL_IF_CONFIG_SMALL("MP3onMP4"),
2101 };
2102 #endif
2103 #endif