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