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