1 /*****************************************************************************
2 * audio_decoder.c: MPEG1 Layer I-II audio decoder
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *****************************************************************************/
27 * - optimiser les NeedBits() et les GetBits() du code là où c'est possible ;
31 #include "int_types.h"
32 //#include "audio_constants.h"
33 #include "audio_decoder.h"
34 #include "audio_math.h" /* DCT32(), PCM() */
35 #include "audio_bit_stream.h"
37 #define NULL ((void *)0)
41 static float adec_scalefactor_table[64] = { /* 2 ^ (1 - i/3) */
42 2.0000000000000000, 1.5874010519681994, 1.2599210498948732,
43 1.0000000000000000, 0.7937005259840998, 0.6299605249474366,
44 0.5000000000000000, 0.3968502629920499, 0.3149802624737183,
45 0.2500000000000000, 0.1984251314960249, 0.1574901312368591,
46 0.1250000000000000, 0.0992125657480125, 0.0787450656184296,
47 0.0625000000000000, 0.0496062828740062, 0.0393725328092148,
48 0.0312500000000000, 0.0248031414370031, 0.0196862664046074,
49 0.0156250000000000, 0.0124015707185016, 0.0098431332023037,
50 0.0078125000000000, 0.0062007853592508, 0.0049215666011518,
51 0.0039062500000000, 0.0031003926796254, 0.0024607833005759,
52 0.0019531250000000, 0.0015501963398127, 0.0012303916502880,
53 0.0009765625000000, 0.0007750981699063, 0.0006151958251440,
54 0.0004882812500000, 0.0003875490849532, 0.0003075979125720,
55 0.0002441406250000, 0.0001937745424766, 0.0001537989562860,
56 0.0001220703125000, 0.0000968872712383, 0.0000768994781430,
57 0.0000610351562500, 0.0000484436356191, 0.0000384497390715,
58 0.0000305175781250, 0.0000242218178096, 0.0000192248695357,
59 0.0000152587890625, 0.0000121109089048, 0.0000096124347679,
60 0.0000076293945312, 0.0000060554544524, 0.0000048062173839,
61 0.0000038146972656, 0.0000030277272262, 0.0000024031086920,
62 0.0000019073486328, 0.0000015138636131, 0.0000012015543460,
63 0.0000009536743164 /* last element is not in the standard... invalid ??? */
66 static float adec_slope_table[15] = {
67 0.6666666666666666, 0.2857142857142857, 0.1333333333333333,
68 0.0645161290322581, 0.0317460317460317, 0.0157480314960630,
69 0.0078431372549020, 0.0039138943248532, 0.0019550342130987,
70 0.0009770395701026, 0.0004884004884005, 0.0002441704309608,
71 0.0001220777635354, 0.0000610370189520, 0.0000305180437934
74 static float adec_offset_table[15] = {
75 -0.6666666666666666, -0.8571428571428571, -0.9333333333333333,
76 -0.9677419354838710, -0.9841269841269841, -0.9921259842519685,
77 -0.9960784313725490, -0.9980430528375733, -0.9990224828934506,
78 -0.9995114802149487, -0.9997557997557998, -0.9998779147845196,
79 -0.9999389611182323, -0.9999694814905240, -0.9999847409781033
82 static u8 adec_layer1_allocation_table[15] = {
83 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
86 static int adec_bound_table[4] = {4, 8, 12, 16};
88 static int adec_layer1_mono (audiodec_t * p_adec, s16 * buffer)
98 /* parse allocation */
100 for (sb = 0; sb < 32; sb += 2)
103 tmp = GetByte ( &p_adec->bit_stream );
104 if ( (tmp >> 4) > 14 )
106 allocation[sb] = adec_layer1_allocation_table [tmp >> 4];
109 allocation[sb+1] = adec_layer1_allocation_table [tmp & 15];
112 /* parse scalefactors */
114 for (sb = 0; sb < 32; sb++)
116 if ( allocation[sb] )
121 NeedBits ( &p_adec->bit_stream, 6 );
122 index = p_adec->bit_stream.buffer >> (32 - 6);
123 DumpBits ( &p_adec->bit_stream, 6 );
125 scalefactor = adec_scalefactor_table[index];
127 slope[sb] = adec_slope_table[allocation[sb]-2] * scalefactor;
128 offset[sb] = adec_offset_table[allocation[sb]-2] * scalefactor;
134 for (s = 0; s < 12; s++)
138 for (sb = 0; sb < 32; sb++)
148 NeedBits (&p_adec->bit_stream, allocation[sb]);
149 code = p_adec->bit_stream.buffer >> (32 - allocation[sb]);
150 DumpBits (&p_adec->bit_stream, allocation[sb]);
152 sample[sb] = slope[sb] * code + offset[sb];
156 DCT32 (sample, &p_adec->bank_0);
158 PCM (&p_adec->bank_0, &XXX_buf, 1);
165 static int adec_layer1_stereo (audiodec_t * p_adec, s16 * buffer)
167 u8 allocation_0[32], allocation_1[32];
168 float slope_0[32], slope_1[32];
169 float offset_0[32], offset_1[32];
170 float sample_0[32], sample_1[32];
176 /* calculate bound */
179 if ( (p_adec->header & 0xc0) == 0x40)
180 { /* intensity stereo */
182 index = (p_adec->header >> 4) & 3;
183 bound = adec_bound_table[index];
186 /* parse allocation */
188 for (sb = 0; sb < bound; sb++)
191 tmp = GetByte (&p_adec->bit_stream);
194 allocation_0[sb] = adec_layer1_allocation_table [tmp >> 4];
197 allocation_1[sb] = adec_layer1_allocation_table [tmp & 15];
199 for (; sb < 32; sb += 2)
202 tmp = GetByte (&p_adec->bit_stream);
205 allocation_0[sb] = allocation_1[sb] = adec_layer1_allocation_table [tmp >> 4];
208 allocation_0[sb+1] = allocation_1[sb+1] = adec_layer1_allocation_table [tmp & 15];
211 /* parse scalefactors */
213 for ( sb = 0; sb < 32; sb++ )
215 if ( allocation_0[sb] )
220 NeedBits (&p_adec->bit_stream, 6);
221 index = p_adec->bit_stream.buffer >> (32 - 6);
222 DumpBits (&p_adec->bit_stream, 6);
224 scalefactor = adec_scalefactor_table[index];
226 slope_0[sb] = adec_slope_table[allocation_0[sb]-2] * scalefactor;
227 offset_0[sb] = adec_offset_table[allocation_0[sb]-2] * scalefactor;
229 if (allocation_1[sb])
234 NeedBits (&p_adec->bit_stream, 6);
235 index = p_adec->bit_stream.buffer >> (32 - 6);
236 DumpBits (&p_adec->bit_stream, 6);
238 scalefactor = adec_scalefactor_table[index];
240 slope_1[sb] = adec_slope_table[allocation_1[sb]-2] * scalefactor;
241 offset_1[sb] = adec_offset_table[allocation_1[sb]-2] * scalefactor;
247 for (s = 0; s < 12; s++)
251 for (sb = 0; sb < bound; sb++)
253 if (!allocation_0[sb])
259 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
260 code = p_adec->bit_stream.buffer >> (32 - allocation_0[sb]);
261 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
263 sample_0[sb] = slope_0[sb] * code + offset_0[sb];
265 if ( !allocation_1[sb] )
271 NeedBits (&p_adec->bit_stream, allocation_1[sb]);
272 code = p_adec->bit_stream.buffer >> (32 - allocation_1[sb]);
273 DumpBits (&p_adec->bit_stream, allocation_1[sb]);
275 sample_1[sb] = slope_1[sb] * code + offset_1[sb];
278 for (; sb < 32; sb++)
280 if (!allocation_0[sb])
289 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
290 sample = p_adec->bit_stream.buffer >> (32 - allocation_0[sb]);
291 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
293 sample_0[sb] = slope_0[sb] * sample + offset_0[sb];
294 sample_1[sb] = slope_1[sb] * sample + offset_1[sb];
298 DCT32 (sample_0, &p_adec->bank_0);
300 PCM (&p_adec->bank_0, &XXX_buf, 2);
301 DCT32 (sample_1, &p_adec->bank_1);
303 PCM (&p_adec->bank_1, &XXX_buf, 2);
319 static void adec_layer2_get_table (u32 header, u8 freq_table[15],
320 alloc_table_t ** alloc, int * sblimit)
322 static s8 table_ab0[16] = {
323 0, L3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
325 static s8 table_ab3[16] = {
326 0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16
328 static s8 table_ab11[8] = {
329 0, L3, L5, 3, L9, 4, 5, 16
331 static s8 table_ab23[8] = {
334 static alloc_table_t mpeg1_ab = {
335 {4,4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,0,0},
336 {table_ab0, table_ab0, table_ab0, table_ab3,
337 table_ab3, table_ab3, table_ab3, table_ab3,
338 table_ab3, table_ab3, table_ab3, table_ab11,
339 table_ab11, table_ab11, table_ab11, table_ab11,
340 table_ab11, table_ab11, table_ab11, table_ab11,
341 table_ab11, table_ab11, table_ab11, table_ab23,
342 table_ab23, table_ab23, table_ab23, table_ab23,
343 table_ab23, table_ab23, NULL, NULL}
346 static s8 table_cd[16] = {
347 0, L3, L5, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
349 static alloc_table_t mpeg1_cd = {
350 {4,4,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
351 {table_cd, table_cd, table_cd, table_cd,
352 table_cd, table_cd, table_cd, table_cd,
353 table_cd, table_cd, table_cd, table_cd,
354 NULL, NULL, NULL, NULL,
355 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
356 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
359 static s8 table_0[16] = {
360 0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
362 static s8 table_4[8] = {
363 0, L3, L5, L9, 4, 5, 6, 7
365 static alloc_table_t mpeg2 = {
366 {4,4,4,4,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0},
367 {table_0, table_0, table_0, table_0,
368 table_4, table_4, table_4, table_4,
369 table_4, table_4, table_4, table_4,
370 table_4, table_4, table_4, table_4,
371 table_4, table_4, table_4, table_4,
372 table_4, table_4, table_4, table_4,
373 table_4, table_4, table_4, table_4,
374 table_4, table_4, NULL, NULL}
377 static alloc_table_t * alloc_table [4] = {
378 &mpeg2, &mpeg1_cd, &mpeg1_ab, &mpeg1_ab
380 static int sblimit_table[12] = {
381 30, 8, 27, 30, 30, 8, 27, 27, 30, 12, 27, 30
386 if (!(header & 0x80000))
387 index = 0; /* mpeg2 */
389 index = (header >> 12) & 15; /* mpeg1, bitrate */
390 index = freq_table [index];
393 *alloc = alloc_table[index];
394 index |= (header >> 8) & 12;
395 *sblimit = sblimit_table[index];
398 static int adec_layer2_mono (audiodec_t * p_adec, s16 * buffer)
400 static u8 freq_table[15] = {3, 0, 0, 0, 1, 0, 1, 2, 2, 2, 3, 3, 3, 3, 3};
401 static float L3_table[3] = {-2/3.0, 0, 2/3.0};
402 static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
403 static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
404 2/9.0, 4/9.0, 6/9.0, 8/9.0};
411 alloc_table_t * alloc_table;
420 /* get the right allocation table */
422 adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
424 /* bound -- not required for mono ! */
428 /* parse allocation table */
430 for ( sb=0 ; sb < sblimit ; sb++ )
434 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
435 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
436 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
438 allocation[sb] = alloc_table->alloc[sb][index];
443 for ( sb = 0 ; sb < sblimit ; sb++ )
447 NeedBits (&p_adec->bit_stream, 2);
448 scfsi[sb] = p_adec->bit_stream.buffer >> (32 - 2);
449 DumpBits (&p_adec->bit_stream, 2);
453 /* Parse scalefactors */
455 for ( sb = 0 ; sb < sblimit ; sb++ )
457 if ( allocation[sb] )
459 int index_0, index_1, index_2;
460 float r_slope,r_offset;
464 NeedBits (&p_adec->bit_stream, 18);
465 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
466 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
467 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
468 DumpBits (&p_adec->bit_stream, 18);
470 if (allocation[sb] < 0)
472 slope[0][sb] = adec_scalefactor_table[index_0];
473 slope[1][sb] = adec_scalefactor_table[index_1];
474 slope[2][sb] = adec_scalefactor_table[index_2];
480 r_slope = adec_slope_table[allocation[sb]-2];
481 r_offset = adec_offset_table[allocation[sb]-2];
483 scalefactor = adec_scalefactor_table[index_0];
484 slope[0][sb] = r_slope * scalefactor;
485 offset[0][sb] = r_offset * scalefactor;
487 scalefactor = adec_scalefactor_table[index_1];
488 slope[1][sb] = r_slope * scalefactor;
489 offset[1][sb] = r_offset * scalefactor;
491 scalefactor = adec_scalefactor_table[index_2];
492 slope[2][sb] = r_slope * scalefactor;
493 offset[2][sb] = r_offset * scalefactor;
498 NeedBits ( &p_adec->bit_stream, 12 );
499 index_0 = p_adec->bit_stream.buffer >> (32-6);
500 index_2 = ( p_adec->bit_stream.buffer >> (32-12) ) & 63;
502 if ( allocation[sb] < 0 )
504 slope[0][sb] = slope[1][sb] =
505 adec_scalefactor_table[index_0];
506 slope[2][sb] = adec_scalefactor_table[index_2];
512 r_slope = adec_slope_table[allocation[sb]-2];
513 r_offset = adec_offset_table[allocation[sb]-2];
515 scalefactor = adec_scalefactor_table[index_0];
516 slope[0][sb] = slope[1][sb] = r_slope * scalefactor;
517 offset[0][sb] = offset[1][sb] = r_offset * scalefactor;
519 scalefactor = adec_scalefactor_table[index_2];
520 slope[2][sb] = r_slope * scalefactor;
521 offset[2][sb] = r_offset * scalefactor;
526 NeedBits ( &p_adec->bit_stream, 6);
527 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
528 if ( allocation[sb] < 0 )
530 slope[0][sb] = slope[1][sb] = slope[2][sb] =
531 adec_scalefactor_table[index_0];
537 r_slope = adec_slope_table[allocation[sb]-2];
538 r_offset = adec_offset_table[allocation[sb]-2];
540 scalefactor = adec_scalefactor_table[index_0];
541 slope[0][sb] = slope[1][sb] = slope[2][sb] =
542 r_slope * scalefactor;
543 offset[0][sb] = offset[1][sb] = offset[2][sb] =
544 r_offset * scalefactor;
549 NeedBits ( &p_adec->bit_stream, 12 );
550 index_0 = p_adec->bit_stream.buffer >> (32-6);
551 index_2 = ( p_adec->bit_stream.buffer >> (32-12) ) & 63;
553 if ( allocation[sb] < 0 )
555 slope[0][sb] = adec_scalefactor_table[index_0];
556 slope[2][sb] = slope[1][sb] =
557 adec_scalefactor_table[index_2];
563 r_slope = adec_slope_table[allocation[sb]-2];
564 r_offset = adec_offset_table[allocation[sb]-2];
566 scalefactor = adec_scalefactor_table[index_0];
567 slope[0][sb] = r_slope * scalefactor;
568 offset[0][sb] = r_offset * scalefactor;
570 scalefactor = adec_scalefactor_table[index_2];
571 slope[2][sb] = slope[1][sb] = r_slope * scalefactor;
572 offset[2][sb] = offset[1][sb] = r_offset * scalefactor;
582 for (gr0 = 0; gr0 < 3; gr0++)
583 for (gr1 = 0; gr1 < 4; gr1++)
587 for (sb = 0; sb < bound; sb++)
591 switch (allocation[sb])
594 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
597 NeedBits (&p_adec->bit_stream, 5);
598 code = p_adec->bit_stream.buffer >> (32 - 5);
599 DumpBits (&p_adec->bit_stream, 5);
601 sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
603 sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
605 sample[2][sb] = slope[gr0][sb] * L3_table[code];
609 NeedBits (&p_adec->bit_stream, 7);
610 code = p_adec->bit_stream.buffer >> (32 - 7);
611 DumpBits (&p_adec->bit_stream, 7);
613 sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
615 sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
617 sample[2][sb] = slope[gr0][sb] * L5_table[code];
621 NeedBits (&p_adec->bit_stream, 10);
622 code = p_adec->bit_stream.buffer >> (32 - 10);
623 DumpBits (&p_adec->bit_stream, 10);
625 sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
627 sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
629 sample[2][sb] = slope[gr0][sb] * L9_table[code];
633 for (s = 0; s < 3; s++) {
634 NeedBits (&p_adec->bit_stream, allocation[sb]);
635 code = (p_adec->bit_stream.buffer >>
636 (32 - allocation[sb]));
637 DumpBits (&p_adec->bit_stream, allocation[sb]);
640 slope[gr0][sb] * code + offset[gr0][sb];
646 for (; sb < sblimit; sb++) {
649 switch (allocation[sb]) {
651 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
654 NeedBits (&p_adec->bit_stream, 5);
655 code = p_adec->bit_stream.buffer >> (32 - 5);
656 DumpBits (&p_adec->bit_stream, 5);
658 sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
660 sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
662 sample[2][sb] = slope[gr0][sb] * L3_table[code];
666 NeedBits (&p_adec->bit_stream, 7);
667 code = p_adec->bit_stream.buffer >> (32 - 7);
668 DumpBits (&p_adec->bit_stream, 7);
670 sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
672 sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
674 sample[2][sb] = slope[gr0][sb] * L5_table[code];
678 NeedBits (&p_adec->bit_stream, 10);
679 code = p_adec->bit_stream.buffer >> (32 - 10);
680 DumpBits (&p_adec->bit_stream, 10);
682 sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
684 sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
686 sample[2][sb] = slope[gr0][sb] * L9_table[code];
690 for (s = 0; s < 3; s++) {
691 NeedBits (&p_adec->bit_stream, allocation[sb]);
692 code = (p_adec->bit_stream.buffer >>
693 (32 - allocation[sb]));
694 DumpBits (&p_adec->bit_stream, allocation[sb]);
697 slope[gr0][sb] * code + offset[gr0][sb];
701 for (; sb < 32; sb++)
703 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
706 for (s = 0; s < 3; s++)
708 DCT32 (sample[s], &p_adec->bank_0);
710 PCM (&p_adec->bank_0, &XXX_buf, 1);
723 static int adec_layer2_stereo (audiodec_t * p_adec, s16 * buffer)
725 static u8 freq_table[15] = {3, 0, 0, 0, 1, 0, 1, 2, 2, 2, 3, 3, 3, 3, 3};
726 static float L3_table[3] = {-2/3.0, 0, 2/3.0};
727 static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
728 static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
729 2/9.0, 4/9.0, 6/9.0, 8/9.0};
731 s8 allocation_0[32], allocation_1[32];
732 u8 scfsi_0[32], scfsi_1[32];
733 float slope_0[3][32], slope_1[3][32];
734 float offset_0[3][32], offset_1[3][32];
735 float sample_0[3][32], sample_1[3][32];
736 alloc_table_t * alloc_table;
744 /* get the right allocation table */
746 adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
748 /* calculate bound */
751 if ((p_adec->header & 0xc0) == 0x40) { /* intensity stereo */
753 index = (p_adec->header >> 4) & 3;
754 if (adec_bound_table[index] < sblimit)
755 bound = adec_bound_table[index];
758 /* parse allocation */
760 for (sb = 0; sb < bound; sb++) {
763 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
764 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
765 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
767 allocation_0[sb] = alloc_table->alloc[sb][index];
769 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
770 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
771 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
773 allocation_1[sb] = alloc_table->alloc[sb][index];
775 for (; sb < sblimit; sb++) {
778 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
779 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
780 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
782 allocation_0[sb] = allocation_1[sb] = alloc_table->alloc[sb][index];
787 for (sb = 0; sb < sblimit; sb++) {
788 if (allocation_0[sb]) {
789 NeedBits (&p_adec->bit_stream, 2);
790 scfsi_0[sb] = p_adec->bit_stream.buffer >> (32 - 2);
791 DumpBits (&p_adec->bit_stream, 2);
793 if (allocation_1[sb]) {
794 NeedBits (&p_adec->bit_stream, 2);
795 scfsi_1[sb] = p_adec->bit_stream.buffer >> (32 - 2);
796 DumpBits (&p_adec->bit_stream, 2);
800 /* parse scalefactors */
802 for (sb = 0; sb < sblimit; sb++)
804 if (allocation_0[sb]) {
805 int index_0, index_1, index_2;
807 switch (scfsi_0[sb]) {
809 NeedBits (&p_adec->bit_stream, 18);
810 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
811 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
812 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
813 DumpBits (&p_adec->bit_stream, 18);
815 if (allocation_0[sb] < 0) {
816 slope_0[0][sb] = adec_scalefactor_table[index_0];
817 slope_0[1][sb] = adec_scalefactor_table[index_1];
818 slope_0[2][sb] = adec_scalefactor_table[index_2];
823 slope = adec_slope_table[allocation_0[sb]-2];
824 offset = adec_offset_table[allocation_0[sb]-2];
826 scalefactor = adec_scalefactor_table[index_0];
827 slope_0[0][sb] = slope * scalefactor;
828 offset_0[0][sb] = offset * scalefactor;
830 scalefactor = adec_scalefactor_table[index_1];
831 slope_0[1][sb] = slope * scalefactor;
832 offset_0[1][sb] = offset * scalefactor;
834 scalefactor = adec_scalefactor_table[index_2];
835 slope_0[2][sb] = slope * scalefactor;
836 offset_0[2][sb] = offset * scalefactor;
840 NeedBits (&p_adec->bit_stream, 12);
841 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
842 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
843 DumpBits (&p_adec->bit_stream, 12);
845 if (allocation_0[sb] < 0) {
846 slope_0[0][sb] = slope_0[1][sb] =
847 adec_scalefactor_table[index_0];
848 slope_0[2][sb] = adec_scalefactor_table[index_1];
853 slope = adec_slope_table[allocation_0[sb]-2];
854 offset = adec_offset_table[allocation_0[sb]-2];
856 scalefactor = adec_scalefactor_table[index_0];
857 slope_0[0][sb] = slope_0[1][sb] = slope * scalefactor;
858 offset_0[0][sb] = offset_0[1][sb] = offset * scalefactor;
860 scalefactor = adec_scalefactor_table[index_1];
861 slope_0[2][sb] = slope * scalefactor;
862 offset_0[2][sb] = offset * scalefactor;
866 NeedBits (&p_adec->bit_stream, 6);
867 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
868 DumpBits (&p_adec->bit_stream, 6);
870 if (allocation_0[sb] < 0) {
871 slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
872 adec_scalefactor_table[index_0];
877 slope = adec_slope_table[allocation_0[sb]-2];
878 offset = adec_offset_table[allocation_0[sb]-2];
880 scalefactor = adec_scalefactor_table[index_0];
881 slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
883 offset_0[0][sb] = offset_0[1][sb] = offset_0[2][sb] =
884 offset * scalefactor;
888 NeedBits (&p_adec->bit_stream, 12);
889 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
890 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
891 DumpBits (&p_adec->bit_stream, 12);
893 if (allocation_0[sb] < 0) {
894 slope_0[0][sb] = adec_scalefactor_table[index_0];
895 slope_0[1][sb] = slope_0[2][sb] =
896 adec_scalefactor_table[index_1];
901 slope = adec_slope_table[allocation_0[sb]-2];
902 offset = adec_offset_table[allocation_0[sb]-2];
904 scalefactor = adec_scalefactor_table[index_0];
905 slope_0[0][sb] = slope * scalefactor;
906 offset_0[0][sb] = offset * scalefactor;
908 scalefactor = adec_scalefactor_table[index_1];
909 slope_0[1][sb] = slope_0[2][sb] = slope * scalefactor;
910 offset_0[1][sb] = offset_0[2][sb] = offset * scalefactor;
915 if (allocation_1[sb]) {
916 int index_0, index_1, index_2;
918 switch (scfsi_1[sb]) {
920 NeedBits (&p_adec->bit_stream, 18);
921 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
922 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
923 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
924 DumpBits (&p_adec->bit_stream, 18);
926 if (allocation_1[sb] < 0) {
927 slope_1[0][sb] = adec_scalefactor_table[index_0];
928 slope_1[1][sb] = adec_scalefactor_table[index_1];
929 slope_1[2][sb] = adec_scalefactor_table[index_2];
934 slope = adec_slope_table[allocation_1[sb]-2];
935 offset = adec_offset_table[allocation_1[sb]-2];
937 scalefactor = adec_scalefactor_table[index_0];
938 slope_1[0][sb] = slope * scalefactor;
939 offset_1[0][sb] = offset * scalefactor;
941 scalefactor = adec_scalefactor_table[index_1];
942 slope_1[1][sb] = slope * scalefactor;
943 offset_1[1][sb] = offset * scalefactor;
945 scalefactor = adec_scalefactor_table[index_2];
946 slope_1[2][sb] = slope * scalefactor;
947 offset_1[2][sb] = offset * scalefactor;
951 NeedBits (&p_adec->bit_stream, 12);
952 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
953 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
954 DumpBits (&p_adec->bit_stream, 12);
956 if (allocation_1[sb] < 0) {
957 slope_1[0][sb] = slope_1[1][sb] =
958 adec_scalefactor_table[index_0];
959 slope_1[2][sb] = adec_scalefactor_table[index_1];
964 slope = adec_slope_table[allocation_1[sb]-2];
965 offset = adec_offset_table[allocation_1[sb]-2];
967 scalefactor = adec_scalefactor_table[index_0];
968 slope_1[0][sb] = slope_1[1][sb] = slope * scalefactor;
969 offset_1[0][sb] = offset_1[1][sb] = offset * scalefactor;
971 scalefactor = adec_scalefactor_table[index_1];
972 slope_1[2][sb] = slope * scalefactor;
973 offset_1[2][sb] = offset * scalefactor;
977 NeedBits (&p_adec->bit_stream, 6);
978 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
979 DumpBits (&p_adec->bit_stream, 6);
981 if (allocation_1[sb] < 0) {
982 slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
983 adec_scalefactor_table[index_0];
988 slope = adec_slope_table[allocation_1[sb]-2];
989 offset = adec_offset_table[allocation_1[sb]-2];
991 scalefactor = adec_scalefactor_table[index_0];
992 slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
994 offset_1[0][sb] = offset_1[1][sb] = offset_1[2][sb] =
995 offset * scalefactor;
999 NeedBits (&p_adec->bit_stream, 12);
1000 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
1001 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
1002 DumpBits (&p_adec->bit_stream, 12);
1004 if (allocation_1[sb] < 0) {
1005 slope_1[0][sb] = adec_scalefactor_table[index_0];
1006 slope_1[1][sb] = slope_1[2][sb] =
1007 adec_scalefactor_table[index_1];
1010 float slope, offset;
1012 slope = adec_slope_table[allocation_1[sb]-2];
1013 offset = adec_offset_table[allocation_1[sb]-2];
1015 scalefactor = adec_scalefactor_table[index_0];
1016 slope_1[0][sb] = slope * scalefactor;
1017 offset_1[0][sb] = offset * scalefactor;
1019 scalefactor = adec_scalefactor_table[index_1];
1020 slope_1[1][sb] = slope_1[2][sb] = slope * scalefactor;
1021 offset_1[1][sb] = offset_1[2][sb] = offset * scalefactor;
1030 for (gr0 = 0; gr0 < 3; gr0++)
1031 for (gr1 = 0; gr1 < 4; gr1++) {
1034 for (sb = 0; sb < bound; sb++) {
1037 switch (allocation_0[sb]) {
1039 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1042 NeedBits (&p_adec->bit_stream, 5);
1043 code = p_adec->bit_stream.buffer >> (32 - 5);
1044 DumpBits (&p_adec->bit_stream, 5);
1046 sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1048 sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1050 sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
1054 NeedBits (&p_adec->bit_stream, 7);
1055 code = p_adec->bit_stream.buffer >> (32 - 7);
1056 DumpBits (&p_adec->bit_stream, 7);
1058 sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1060 sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1062 sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
1066 NeedBits (&p_adec->bit_stream, 10);
1067 code = p_adec->bit_stream.buffer >> (32 - 10);
1068 DumpBits (&p_adec->bit_stream, 10);
1070 sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1072 sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1074 sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
1078 for (s = 0; s < 3; s++) {
1079 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
1080 code = (p_adec->bit_stream.buffer >>
1081 (32 - allocation_0[sb]));
1082 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
1085 slope_0[gr0][sb] * code + offset_0[gr0][sb];
1088 switch (allocation_1[sb]) {
1090 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1093 NeedBits (&p_adec->bit_stream, 5);
1094 code = p_adec->bit_stream.buffer >> (32 - 5);
1095 DumpBits (&p_adec->bit_stream, 5);
1097 sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1099 sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1101 sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
1105 NeedBits (&p_adec->bit_stream, 7);
1106 code = p_adec->bit_stream.buffer >> (32 - 7);
1107 DumpBits (&p_adec->bit_stream, 7);
1109 sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1111 sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1113 sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
1117 NeedBits (&p_adec->bit_stream, 10);
1118 code = p_adec->bit_stream.buffer >> (32 - 10);
1119 DumpBits (&p_adec->bit_stream, 10);
1121 sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1123 sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1125 sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
1129 for (s = 0; s < 3; s++) {
1130 NeedBits (&p_adec->bit_stream, allocation_1[sb]);
1131 code = (p_adec->bit_stream.buffer >>
1132 (32 - allocation_1[sb]));
1133 DumpBits (&p_adec->bit_stream, allocation_1[sb]);
1136 slope_1[gr0][sb] * code + offset_1[gr0][sb];
1140 for (; sb < sblimit; sb++) {
1143 switch (allocation_0[sb]) {
1145 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1146 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1149 NeedBits (&p_adec->bit_stream, 5);
1150 code = p_adec->bit_stream.buffer >> (32 - 5);
1151 DumpBits (&p_adec->bit_stream, 5);
1153 sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1154 sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1156 sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1157 sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1159 sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
1160 sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
1164 NeedBits (&p_adec->bit_stream, 7);
1165 code = p_adec->bit_stream.buffer >> (32 - 7);
1166 DumpBits (&p_adec->bit_stream, 7);
1168 sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1169 sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1171 sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1172 sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1174 sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
1175 sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
1179 NeedBits (&p_adec->bit_stream, 10);
1180 code = p_adec->bit_stream.buffer >> (32 - 10);
1181 DumpBits (&p_adec->bit_stream, 10);
1183 sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1184 sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1186 sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1187 sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1189 sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
1190 sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
1194 for (s = 0; s < 3; s++) {
1195 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
1196 code = (p_adec->bit_stream.buffer >>
1197 (32 - allocation_0[sb]));
1198 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
1201 slope_0[gr0][sb] * code + offset_0[gr0][sb];
1203 slope_1[gr0][sb] * code + offset_1[gr0][sb];
1207 for (; sb < 32; sb++) {
1208 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1209 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1211 for (s = 0; s < 3; s++) {
1212 DCT32 (sample_0[s], &p_adec->bank_0);
1214 PCM (&p_adec->bank_0, &XXX_buf, 2);
1215 DCT32 (sample_1[s], &p_adec->bank_1);
1217 PCM (&p_adec->bank_1, &XXX_buf, 2);
1225 int adec_init (audiodec_t * p_adec)
1227 p_adec->bank_0.actual = p_adec->bank_0.v1;
1228 p_adec->bank_0.pos = 0;
1229 p_adec->bank_1.actual = p_adec->bank_1.v1;
1230 p_adec->bank_1.pos = 0;
1234 int adec_sync_frame (audiodec_t * p_adec, adec_sync_info_t * p_sync_info)
1236 static int mpeg1_sample_rate[3] = {44100, 48000, 32000};
1237 static int mpeg1_layer1_bit_rate[15] = {
1238 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
1240 static int mpeg1_layer2_bit_rate[15] = {
1241 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384
1243 static int mpeg2_layer1_bit_rate[15] = {
1244 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256
1246 static int mpeg2_layer2_bit_rate[15] = {
1247 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160
1249 static int * bit_rate_table[8] = {
1250 NULL, NULL, mpeg2_layer2_bit_rate, mpeg2_layer1_bit_rate,
1251 NULL, NULL, mpeg1_layer2_bit_rate, mpeg1_layer1_bit_rate
1261 p_adec->bit_stream.total_bytes_read = 0;
1263 header = GetByte (&p_adec->bit_stream) << 24;
1264 header |= GetByte (&p_adec->bit_stream) << 16;
1265 header |= GetByte (&p_adec->bit_stream) << 8;
1266 header |= GetByte (&p_adec->bit_stream);
1267 p_adec->header = header;
1269 /* basic header check : sync word, no emphasis */
1270 if ((header & 0xfff00003) != 0xfff00000)
1273 /* calculate bit rate */
1275 index = (header >> 17) & 7; /* mpeg ID + layer */
1276 bit_rates = bit_rate_table[index];
1277 if (bit_rate_table == NULL)
1278 return 1; /* invalid layer */
1280 index = (header >> 12) & 15; /* bit rate index */
1283 bit_rate = bit_rates[index];
1285 /* mpeg 1 layer 2 : check that bitrate per channel is valid */
1287 if (bit_rates == mpeg1_layer2_bit_rate) {
1288 if ((header & 0xc0) == 0xc0) { /* mono */
1290 return 1; /* invalid bitrate per channel */
1291 } else { /* stereo */
1292 if ((1 << index) & 0x2e)
1293 return 1; /* invalid bitrate per channel */
1297 /* calculate sample rate */
1299 index = (header >> 10) & 3; /* sample rate index */
1302 sample_rate = mpeg1_sample_rate[index];
1303 if (!(header & 0x80000))
1304 sample_rate >>= 1; /* half sample rate for mpeg2 */
1306 /* calculate frame length */
1308 if ((header & 0x60000) == 0x60000) { /* layer 1 */
1309 frame_size = 48000 * bit_rate / sample_rate;
1310 if (header & 0x200) /* padding */
1312 } else { /* layer >1 */
1313 frame_size = 144000 * bit_rate / sample_rate;
1314 if (header & 0x200) /* padding */
1318 p_sync_info->sample_rate = sample_rate;
1319 p_sync_info->bit_rate = bit_rate;
1320 p_sync_info->frame_size = frame_size;
1321 p_adec->frame_size = frame_size;
1326 int adec_decode_frame (audiodec_t * p_adec, s16 * buffer)
1328 if (!(p_adec->header & 0x10000)) { /* error check, skip it */
1329 GetByte (&p_adec->bit_stream);
1330 GetByte (&p_adec->bit_stream);
1333 /* parse audio data */
1335 p_adec->bit_stream.i_available = 0;
1337 switch ((p_adec->header >> 17) & 3) {
1338 case 2: /* layer 2 */
1339 if ((p_adec->header & 0xc0) == 0xc0) {
1340 if (adec_layer2_mono (p_adec, buffer))
1343 if (adec_layer2_stereo (p_adec, buffer))
1347 case 3: /* layer 1 */
1348 if ((p_adec->header & 0xc0) == 0xc0) {
1349 if (adec_layer1_mono (p_adec, buffer))
1352 if (adec_layer1_stereo (p_adec, buffer))
1358 /* skip ancillary data */
1360 if ((p_adec->header & 0xf000) == 0) /* free bitrate format */
1363 /* XXX rewrite the byte counting system to reduce overhead */
1366 printf ("skip %d\n",
1367 p_adec->frame_size - p_adec->bit_stream.total_bytes_read);
1370 if (p_adec->bit_stream.total_bytes_read > p_adec->frame_size)
1371 return 1; /* overrun */
1373 while (p_adec->bit_stream.total_bytes_read < p_adec->frame_size)
1374 GetByte (&p_adec->bit_stream); /* skip ancillary data */