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