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