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