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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
26 * - optimiser les NeedBits() et les GetBits() du code là où c'est possible ;
30 #include "int_types.h"
31 //#include "audio_constants.h"
32 #include "audio_decoder.h"
33 #include "audio_math.h" /* DCT32(), PCM() */
34 #include "audio_bit_stream.h"
38 #define NULL ((void *)0)
42 static float adec_scalefactor_table[64] =
44 2.0000000000000000, 1.5874010519681994, 1.2599210498948732,
45 1.0000000000000000, 0.7937005259840998, 0.6299605249474366,
46 0.5000000000000000, 0.3968502629920499, 0.3149802624737183,
47 0.2500000000000000, 0.1984251314960249, 0.1574901312368591,
48 0.1250000000000000, 0.0992125657480125, 0.0787450656184296,
49 0.0625000000000000, 0.0496062828740062, 0.0393725328092148,
50 0.0312500000000000, 0.0248031414370031, 0.0196862664046074,
51 0.0156250000000000, 0.0124015707185016, 0.0098431332023037,
52 0.0078125000000000, 0.0062007853592508, 0.0049215666011518,
53 0.0039062500000000, 0.0031003926796254, 0.0024607833005759,
54 0.0019531250000000, 0.0015501963398127, 0.0012303916502880,
55 0.0009765625000000, 0.0007750981699063, 0.0006151958251440,
56 0.0004882812500000, 0.0003875490849532, 0.0003075979125720,
57 0.0002441406250000, 0.0001937745424766, 0.0001537989562860,
58 0.0001220703125000, 0.0000968872712383, 0.0000768994781430,
59 0.0000610351562500, 0.0000484436356191, 0.0000384497390715,
60 0.0000305175781250, 0.0000242218178096, 0.0000192248695357,
61 0.0000152587890625, 0.0000121109089048, 0.0000096124347679,
62 0.0000076293945312, 0.0000060554544524, 0.0000048062173839,
63 0.0000038146972656, 0.0000030277272262, 0.0000024031086920,
64 0.0000019073486328, 0.0000015138636131, 0.0000012015543460,
65 0.0000009536743164 /* last element is not in the standard... invalid ??? */
68 static float adec_slope_table[15] =
70 0.6666666666666666, 0.2857142857142857, 0.1333333333333333,
71 0.0645161290322581, 0.0317460317460317, 0.0157480314960630,
72 0.0078431372549020, 0.0039138943248532, 0.0019550342130987,
73 0.0009770395701026, 0.0004884004884005, 0.0002441704309608,
74 0.0001220777635354, 0.0000610370189520, 0.0000305180437934
77 static float adec_offset_table[15] =
79 -0.6666666666666666, -0.8571428571428571, -0.9333333333333333,
80 -0.9677419354838710, -0.9841269841269841, -0.9921259842519685,
81 -0.9960784313725490, -0.9980430528375733, -0.9990224828934506,
82 -0.9995114802149487, -0.9997557997557998, -0.9998779147845196,
83 -0.9999389611182323, -0.9999694814905240, -0.9999847409781033
86 static u8 adec_layer1_allocation_table[15] =
88 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
91 static int adec_bound_table[4] = { 4, 8, 12, 16 };
93 static int adec_layer1_mono( audiodec_t * p_adec, s16 * buffer )
103 /* parse allocation */
105 for (sb = 0; sb < 32; sb += 2)
108 tmp = GetByte ( &p_adec->bit_stream );
110 if ( (tmp >> 4) > 14 )
115 allocation[sb] = adec_layer1_allocation_table [tmp >> 4];
122 allocation[sb+1] = adec_layer1_allocation_table [tmp & 15];
125 /* parse scalefactors */
127 for ( sb = 0; sb < 32; sb++ )
129 if ( allocation[sb] )
134 NeedBits ( &p_adec->bit_stream, 6 );
135 index = p_adec->bit_stream.buffer >> (32 - 6);
136 DumpBits ( &p_adec->bit_stream, 6 );
138 scalefactor = adec_scalefactor_table[index];
140 slope[sb] = adec_slope_table[allocation[sb]-2] * scalefactor;
141 offset[sb] = adec_offset_table[allocation[sb]-2] * scalefactor;
147 for (s = 0; s < 12; s++)
151 for (sb = 0; sb < 32; sb++)
161 NeedBits (&p_adec->bit_stream, allocation[sb]);
162 code = p_adec->bit_stream.buffer >> (32 - allocation[sb]);
163 DumpBits (&p_adec->bit_stream, allocation[sb]);
165 sample[sb] = slope[sb] * code + offset[sb];
169 DCT32 (sample, &p_adec->bank_0);
171 PCM (&p_adec->bank_0, &XXX_buf, 1);
178 static int adec_layer1_stereo (audiodec_t * p_adec, s16 * buffer)
180 u8 allocation_0[32], allocation_1[32];
181 float slope_0[32], slope_1[32];
182 float offset_0[32], offset_1[32];
183 float sample_0[32], sample_1[32];
189 /* calculate bound */
192 if ( (p_adec->header & 0xc0) == 0x40)
193 { /* intensity stereo */
195 index = (p_adec->header >> 4) & 3;
196 bound = adec_bound_table[index];
199 /* parse allocation */
201 for (sb = 0; sb < bound; sb++)
204 tmp = GetByte (&p_adec->bit_stream);
209 allocation_0[sb] = adec_layer1_allocation_table [tmp >> 4];
214 allocation_1[sb] = adec_layer1_allocation_table [tmp & 15];
217 for (; sb < 32; sb += 2)
220 tmp = GetByte (&p_adec->bit_stream);
225 allocation_0[sb] = allocation_1[sb] = adec_layer1_allocation_table [tmp >> 4];
230 allocation_0[sb+1] = allocation_1[sb+1] = adec_layer1_allocation_table [tmp & 15];
233 /* parse scalefactors */
235 for ( sb = 0; sb < 32; sb++ )
237 if ( allocation_0[sb] )
242 NeedBits (&p_adec->bit_stream, 6);
243 index = p_adec->bit_stream.buffer >> (32 - 6);
244 DumpBits (&p_adec->bit_stream, 6);
246 scalefactor = adec_scalefactor_table[index];
248 slope_0[sb] = adec_slope_table[allocation_0[sb]-2] * scalefactor;
249 offset_0[sb] = adec_offset_table[allocation_0[sb]-2] * scalefactor;
252 if (allocation_1[sb])
257 NeedBits (&p_adec->bit_stream, 6);
258 index = p_adec->bit_stream.buffer >> (32 - 6);
259 DumpBits (&p_adec->bit_stream, 6);
261 scalefactor = adec_scalefactor_table[index];
263 slope_1[sb] = adec_slope_table[allocation_1[sb]-2] * scalefactor;
264 offset_1[sb] = adec_offset_table[allocation_1[sb]-2] * scalefactor;
270 for (s = 0; s < 12; s++)
274 for (sb = 0; sb < bound; sb++)
276 if (!allocation_0[sb])
284 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
285 code = p_adec->bit_stream.buffer >> (32 - allocation_0[sb]);
286 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
288 sample_0[sb] = slope_0[sb] * code + offset_0[sb];
291 if ( !allocation_1[sb] )
299 NeedBits (&p_adec->bit_stream, allocation_1[sb]);
300 code = p_adec->bit_stream.buffer >> (32 - allocation_1[sb]);
301 DumpBits (&p_adec->bit_stream, allocation_1[sb]);
303 sample_1[sb] = slope_1[sb] * code + offset_1[sb];
307 for (; sb < 32; sb++)
309 if (!allocation_0[sb])
318 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
319 code = p_adec->bit_stream.buffer >> (32 - allocation_0[sb]);
320 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
322 sample_0[sb] = slope_0[sb] * code + offset_0[sb];
323 sample_1[sb] = slope_1[sb] * code + offset_1[sb];
327 DCT32 (sample_0, &p_adec->bank_0);
329 PCM (&p_adec->bank_0, &XXX_buf, 2);
330 DCT32 (sample_1, &p_adec->bank_1);
332 PCM (&p_adec->bank_1, &XXX_buf, 2);
349 static void adec_layer2_get_table( u32 header, u8 freq_table[15],
350 alloc_table_t ** alloc, int * sblimit )
352 static s8 table_ab0[16] =
354 0, L3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
356 static s8 table_ab3[16] =
358 0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16
360 static s8 table_ab11[8] =
362 0, L3, L5, 3, L9, 4, 5, 16
364 static s8 table_ab23[8] =
368 static alloc_table_t mpeg1_ab =
370 {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},
371 {table_ab0, table_ab0, table_ab0, table_ab3,
372 table_ab3, table_ab3, table_ab3, table_ab3,
373 table_ab3, table_ab3, table_ab3, table_ab11,
374 table_ab11, table_ab11, table_ab11, table_ab11,
375 table_ab11, table_ab11, table_ab11, table_ab11,
376 table_ab11, table_ab11, table_ab11, table_ab23,
377 table_ab23, table_ab23, table_ab23, table_ab23,
378 table_ab23, table_ab23, NULL, NULL}
381 static s8 table_cd[16] =
383 0, L3, L5, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
385 static alloc_table_t mpeg1_cd =
387 {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},
388 {table_cd, table_cd, table_cd, table_cd,
389 table_cd, table_cd, table_cd, table_cd,
390 table_cd, table_cd, table_cd, table_cd,
391 NULL, NULL, NULL, NULL,
392 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
393 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
396 static s8 table_0[16] =
398 0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
400 static s8 table_4[8] =
402 0, L3, L5, L9, 4, 5, 6, 7
404 static alloc_table_t mpeg2 =
406 {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},
407 {table_0, table_0, table_0, table_0,
408 table_4, table_4, table_4, table_4,
409 table_4, table_4, table_4, table_4,
410 table_4, table_4, table_4, table_4,
411 table_4, table_4, table_4, table_4,
412 table_4, table_4, table_4, table_4,
413 table_4, table_4, table_4, table_4,
414 table_4, table_4, NULL, NULL}
417 static alloc_table_t * alloc_table [4] =
419 &mpeg2, &mpeg1_cd, &mpeg1_ab, &mpeg1_ab
421 static int sblimit_table[12] =
423 30, 8, 27, 30, 30, 8, 27, 27, 30, 12, 27, 30
428 if (!(header & 0x80000))
430 index = 0; /* mpeg2 */
434 index = (header >> 12) & 15; /* mpeg1, bitrate */
435 index = freq_table [index];
438 *alloc = alloc_table[index];
439 index |= (header >> 8) & 12;
440 *sblimit = sblimit_table[index];
443 static int adec_layer2_mono (audiodec_t * p_adec, s16 * buffer)
445 static u8 freq_table[15] = {2, 1, 1, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2};
446 static float L3_table[3] = {-2/3.0, 0, 2/3.0};
447 static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
448 static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
449 2/9.0, 4/9.0, 6/9.0, 8/9.0};
456 alloc_table_t * alloc_table;
463 /* get the right allocation table */
464 adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
466 /* parse allocation */
469 for (sb = 0; sb < sblimit; sb++)
473 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
474 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
475 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
477 allocation[sb] = alloc_table->alloc[sb][index];
482 for (sb = 0; sb < sblimit; sb++)
486 NeedBits (&p_adec->bit_stream, 2);
487 scfsi[sb] = p_adec->bit_stream.buffer >> (32 - 2);
488 DumpBits (&p_adec->bit_stream, 2);
492 /* parse scalefactors */
494 for (sb = 0; sb < sblimit; sb++)
498 int index_0, index_1, index_2;
503 NeedBits (&p_adec->bit_stream, 18);
504 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
505 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
506 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
507 DumpBits (&p_adec->bit_stream, 18);
509 if (allocation[sb] < 0)
511 slope[0][sb] = adec_scalefactor_table[index_0];
512 slope[1][sb] = adec_scalefactor_table[index_1];
513 slope[2][sb] = adec_scalefactor_table[index_2];
518 float r_slope, r_offset;
520 r_slope = adec_slope_table[allocation[sb]-2];
521 r_offset = adec_offset_table[allocation[sb]-2];
523 r_scalefactor = adec_scalefactor_table[index_0];
524 slope[0][sb] = r_slope * r_scalefactor;
525 offset[0][sb] = r_offset * r_scalefactor;
527 r_scalefactor = adec_scalefactor_table[index_1];
528 slope[1][sb] = r_slope * r_scalefactor;
529 offset[1][sb] = r_offset * r_scalefactor;
531 r_scalefactor = adec_scalefactor_table[index_2];
532 slope[2][sb] = r_slope * r_scalefactor;
533 offset[2][sb] = r_offset * r_scalefactor;
538 NeedBits (&p_adec->bit_stream, 12);
539 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
540 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
541 DumpBits (&p_adec->bit_stream, 12);
543 if (allocation[sb] < 0)
545 slope[0][sb] = slope[1][sb] =
546 adec_scalefactor_table[index_0];
547 slope[2][sb] = adec_scalefactor_table[index_1];
552 float r_slope, r_offset;
554 r_slope = adec_slope_table[allocation[sb]-2];
555 r_offset = adec_offset_table[allocation[sb]-2];
557 r_scalefactor = adec_scalefactor_table[index_0];
558 slope[0][sb] = slope[1][sb] = r_slope * r_scalefactor;
559 offset[0][sb] = offset[1][sb] =
560 r_offset * r_scalefactor;
562 r_scalefactor = adec_scalefactor_table[index_1];
563 slope[2][sb] = r_slope * r_scalefactor;
564 offset[2][sb] = r_offset * r_scalefactor;
569 NeedBits (&p_adec->bit_stream, 6);
570 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
571 DumpBits (&p_adec->bit_stream, 6);
573 if (allocation[sb] < 0)
575 slope[0][sb] = slope[1][sb] = slope[2][sb] =
576 adec_scalefactor_table[index_0];
581 float r_slope, r_offset;
583 r_slope = adec_slope_table[allocation[sb]-2];
584 r_offset = adec_offset_table[allocation[sb]-2];
586 r_scalefactor = adec_scalefactor_table[index_0];
587 slope[0][sb] = slope[1][sb] = slope[2][sb] =
588 r_slope * r_scalefactor;
589 offset[0][sb] = offset[1][sb] = offset[2][sb] =
590 r_offset * r_scalefactor;
595 NeedBits (&p_adec->bit_stream, 12);
596 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
597 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
598 DumpBits (&p_adec->bit_stream, 12);
600 if (allocation[sb] < 0)
602 slope[0][sb] = adec_scalefactor_table[index_0];
603 slope[1][sb] = slope[2][sb] =
604 adec_scalefactor_table[index_1];
609 float r_slope, r_offset;
611 r_slope = adec_slope_table[allocation[sb]-2];
612 r_offset = adec_offset_table[allocation[sb]-2];
614 r_scalefactor = adec_scalefactor_table[index_0];
615 slope[0][sb] = r_slope * r_scalefactor;
616 offset[0][sb] = r_offset * r_scalefactor;
618 r_scalefactor = adec_scalefactor_table[index_1];
619 slope[1][sb] = slope[2][sb] = r_slope * r_scalefactor;
620 offset[1][sb] = offset[2][sb] =
621 r_offset * r_scalefactor;
630 for (gr0 = 0; gr0 < 3; gr0++)
632 for (gr1 = 0; gr1 < 4; gr1++)
636 for (sb = 0; sb < sblimit; sb++)
640 switch (allocation[sb])
643 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
647 NeedBits (&p_adec->bit_stream, 5);
648 code = p_adec->bit_stream.buffer >> (32 - 5);
649 DumpBits (&p_adec->bit_stream, 5);
651 sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
653 sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
655 sample[2][sb] = slope[gr0][sb] * L3_table[code];
659 NeedBits (&p_adec->bit_stream, 7);
660 code = p_adec->bit_stream.buffer >> (32 - 7);
661 DumpBits (&p_adec->bit_stream, 7);
663 sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
665 sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
667 sample[2][sb] = slope[gr0][sb] * L5_table[code];
671 NeedBits (&p_adec->bit_stream, 10);
672 code = p_adec->bit_stream.buffer >> (32 - 10);
673 DumpBits (&p_adec->bit_stream, 10);
675 sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
677 sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
679 sample[2][sb] = slope[gr0][sb] * L9_table[code];
683 for (s = 0; s < 3; s++)
685 NeedBits (&p_adec->bit_stream, allocation[sb]);
686 code = ( p_adec->bit_stream.buffer >>
687 (32 - allocation[sb]) );
688 DumpBits (&p_adec->bit_stream, allocation[sb]);
691 slope[gr0][sb] * code + offset[gr0][sb];
696 for (; sb < 32; sb++)
698 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
701 for (s = 0; s < 3; s++)
703 DCT32 (sample[s], &p_adec->bank_0);
705 PCM (&p_adec->bank_0, &XXX_buf, 2);
707 /* FIXME: one shouldn't have to do it twice ! */
708 DCT32 (sample[s], &p_adec->bank_1);
710 PCM (&p_adec->bank_1, &XXX_buf, 2);
720 static int adec_layer2_stereo (audiodec_t * p_adec, s16 * buffer)
722 static u8 freq_table[15] = {3, 0, 0, 0, 1, 0, 1, 2, 2, 2, 3, 3, 3, 3, 3};
723 static float L3_table[3] = {-2/3.0, 0, 2/3.0};
724 static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
725 static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
726 2/9.0, 4/9.0, 6/9.0, 8/9.0};
728 s8 allocation_0[32], allocation_1[32];
729 u8 scfsi_0[32], scfsi_1[32];
730 float slope_0[3][32], slope_1[3][32];
731 float offset_0[3][32], offset_1[3][32];
732 float sample_0[3][32], sample_1[3][32];
733 alloc_table_t * alloc_table;
741 /* get the right allocation table */
742 adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
744 /* calculate bound */
746 if ((p_adec->header & 0xc0) == 0x40) { /* intensity stereo */
748 index = (p_adec->header >> 4) & 3;
749 if (adec_bound_table[index] < sblimit)
751 bound = adec_bound_table[index];
755 /* parse allocation */
757 for (sb = 0; sb < bound; sb++)
761 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
762 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
763 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
765 allocation_0[sb] = alloc_table->alloc[sb][index];
767 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
768 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
769 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
771 allocation_1[sb] = alloc_table->alloc[sb][index];
774 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++)
789 if (allocation_0[sb])
791 NeedBits (&p_adec->bit_stream, 2);
792 scfsi_0[sb] = p_adec->bit_stream.buffer >> (32 - 2);
793 DumpBits (&p_adec->bit_stream, 2);
796 if (allocation_1[sb])
798 NeedBits (&p_adec->bit_stream, 2);
799 scfsi_1[sb] = p_adec->bit_stream.buffer >> (32 - 2);
800 DumpBits (&p_adec->bit_stream, 2);
804 /* parse scalefactors */
806 for (sb = 0; sb < sblimit; sb++)
808 if (allocation_0[sb])
810 int index_0, index_1, index_2;
815 NeedBits (&p_adec->bit_stream, 18);
816 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
817 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
818 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
819 DumpBits (&p_adec->bit_stream, 18);
821 if (allocation_0[sb] < 0)
823 slope_0[0][sb] = adec_scalefactor_table[index_0];
824 slope_0[1][sb] = adec_scalefactor_table[index_1];
825 slope_0[2][sb] = adec_scalefactor_table[index_2];
832 slope = adec_slope_table[allocation_0[sb]-2];
833 offset = adec_offset_table[allocation_0[sb]-2];
835 scalefactor = adec_scalefactor_table[index_0];
836 slope_0[0][sb] = slope * scalefactor;
837 offset_0[0][sb] = offset * scalefactor;
839 scalefactor = adec_scalefactor_table[index_1];
840 slope_0[1][sb] = slope * scalefactor;
841 offset_0[1][sb] = offset * scalefactor;
843 scalefactor = adec_scalefactor_table[index_2];
844 slope_0[2][sb] = slope * scalefactor;
845 offset_0[2][sb] = offset * scalefactor;
850 NeedBits (&p_adec->bit_stream, 12);
851 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
852 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
853 DumpBits (&p_adec->bit_stream, 12);
855 if (allocation_0[sb] < 0)
857 slope_0[0][sb] = slope_0[1][sb] =
858 adec_scalefactor_table[index_0];
859 slope_0[2][sb] = adec_scalefactor_table[index_1];
866 slope = adec_slope_table[allocation_0[sb]-2];
867 offset = adec_offset_table[allocation_0[sb]-2];
869 scalefactor = adec_scalefactor_table[index_0];
870 slope_0[0][sb] = slope_0[1][sb] = slope * scalefactor;
871 offset_0[0][sb] = offset_0[1][sb] =
872 offset * scalefactor;
874 scalefactor = adec_scalefactor_table[index_1];
875 slope_0[2][sb] = slope * scalefactor;
876 offset_0[2][sb] = offset * scalefactor;
881 NeedBits (&p_adec->bit_stream, 6);
882 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
883 DumpBits (&p_adec->bit_stream, 6);
885 if (allocation_0[sb] < 0)
887 slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
888 adec_scalefactor_table[index_0];
895 slope = adec_slope_table[allocation_0[sb]-2];
896 offset = adec_offset_table[allocation_0[sb]-2];
898 scalefactor = adec_scalefactor_table[index_0];
899 slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
901 offset_0[0][sb] = offset_0[1][sb] = offset_0[2][sb] =
902 offset * scalefactor;
907 NeedBits (&p_adec->bit_stream, 12);
908 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
909 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
910 DumpBits (&p_adec->bit_stream, 12);
912 if (allocation_0[sb] < 0)
914 slope_0[0][sb] = adec_scalefactor_table[index_0];
915 slope_0[1][sb] = slope_0[2][sb] =
916 adec_scalefactor_table[index_1];
923 slope = adec_slope_table[allocation_0[sb]-2];
924 offset = adec_offset_table[allocation_0[sb]-2];
926 scalefactor = adec_scalefactor_table[index_0];
927 slope_0[0][sb] = slope * scalefactor;
928 offset_0[0][sb] = offset * scalefactor;
930 scalefactor = adec_scalefactor_table[index_1];
931 slope_0[1][sb] = slope_0[2][sb] = slope * scalefactor;
932 offset_0[1][sb] = offset_0[2][sb] =
933 offset * scalefactor;
939 if (allocation_1[sb])
941 int index_0, index_1, index_2;
946 NeedBits (&p_adec->bit_stream, 18);
947 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
948 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
949 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
950 DumpBits (&p_adec->bit_stream, 18);
952 if (allocation_1[sb] < 0)
954 slope_1[0][sb] = adec_scalefactor_table[index_0];
955 slope_1[1][sb] = adec_scalefactor_table[index_1];
956 slope_1[2][sb] = adec_scalefactor_table[index_2];
963 slope = adec_slope_table[allocation_1[sb]-2];
964 offset = adec_offset_table[allocation_1[sb]-2];
966 scalefactor = adec_scalefactor_table[index_0];
967 slope_1[0][sb] = slope * scalefactor;
968 offset_1[0][sb] = offset * scalefactor;
970 scalefactor = adec_scalefactor_table[index_1];
971 slope_1[1][sb] = slope * scalefactor;
972 offset_1[1][sb] = offset * scalefactor;
974 scalefactor = adec_scalefactor_table[index_2];
975 slope_1[2][sb] = slope * scalefactor;
976 offset_1[2][sb] = offset * scalefactor;
981 NeedBits (&p_adec->bit_stream, 12);
982 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
983 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
984 DumpBits (&p_adec->bit_stream, 12);
986 if (allocation_1[sb] < 0)
988 slope_1[0][sb] = slope_1[1][sb] =
989 adec_scalefactor_table[index_0];
990 slope_1[2][sb] = adec_scalefactor_table[index_1];
997 slope = adec_slope_table[allocation_1[sb]-2];
998 offset = adec_offset_table[allocation_1[sb]-2];
1000 scalefactor = adec_scalefactor_table[index_0];
1001 slope_1[0][sb] = slope_1[1][sb] = slope * scalefactor;
1002 offset_1[0][sb] = offset_1[1][sb] =
1003 offset * scalefactor;
1005 scalefactor = adec_scalefactor_table[index_1];
1006 slope_1[2][sb] = slope * scalefactor;
1007 offset_1[2][sb] = offset * scalefactor;
1012 NeedBits (&p_adec->bit_stream, 6);
1013 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
1014 DumpBits (&p_adec->bit_stream, 6);
1016 if (allocation_1[sb] < 0)
1018 slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
1019 adec_scalefactor_table[index_0];
1024 float slope, offset;
1026 slope = adec_slope_table[allocation_1[sb]-2];
1027 offset = adec_offset_table[allocation_1[sb]-2];
1029 scalefactor = adec_scalefactor_table[index_0];
1030 slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
1031 slope * scalefactor;
1032 offset_1[0][sb] = offset_1[1][sb] = offset_1[2][sb] =
1033 offset * scalefactor;
1038 NeedBits (&p_adec->bit_stream, 12);
1039 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
1040 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
1041 DumpBits (&p_adec->bit_stream, 12);
1043 if (allocation_1[sb] < 0)
1045 slope_1[0][sb] = adec_scalefactor_table[index_0];
1046 slope_1[1][sb] = slope_1[2][sb] =
1047 adec_scalefactor_table[index_1];
1052 float slope, offset;
1054 slope = adec_slope_table[allocation_1[sb]-2];
1055 offset = adec_offset_table[allocation_1[sb]-2];
1057 scalefactor = adec_scalefactor_table[index_0];
1058 slope_1[0][sb] = slope * scalefactor;
1059 offset_1[0][sb] = offset * scalefactor;
1061 scalefactor = adec_scalefactor_table[index_1];
1062 slope_1[1][sb] = slope_1[2][sb] = slope * scalefactor;
1063 offset_1[1][sb] = offset_1[2][sb] =
1064 offset * scalefactor;
1073 for (gr0 = 0; gr0 < 3; gr0++)
1075 for (gr1 = 0; gr1 < 4; gr1++)
1079 for (sb = 0; sb < bound; sb++)
1083 switch (allocation_0[sb])
1086 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1090 NeedBits (&p_adec->bit_stream, 5);
1091 code = p_adec->bit_stream.buffer >> (32 - 5);
1092 DumpBits (&p_adec->bit_stream, 5);
1094 sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1096 sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1098 sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
1102 NeedBits (&p_adec->bit_stream, 7);
1103 code = p_adec->bit_stream.buffer >> (32 - 7);
1104 DumpBits (&p_adec->bit_stream, 7);
1106 sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1108 sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1110 sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
1114 NeedBits (&p_adec->bit_stream, 10);
1115 code = p_adec->bit_stream.buffer >> (32 - 10);
1116 DumpBits (&p_adec->bit_stream, 10);
1118 sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1120 sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1122 sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
1126 for (s = 0; s < 3; s++)
1128 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
1129 code = ( p_adec->bit_stream.buffer >>
1130 (32 - allocation_0[sb]) );
1131 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
1134 slope_0[gr0][sb] * code + offset_0[gr0][sb];
1138 switch (allocation_1[sb])
1141 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1145 NeedBits (&p_adec->bit_stream, 5);
1146 code = p_adec->bit_stream.buffer >> (32 - 5);
1147 DumpBits (&p_adec->bit_stream, 5);
1149 sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1151 sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1153 sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
1157 NeedBits (&p_adec->bit_stream, 7);
1158 code = p_adec->bit_stream.buffer >> (32 - 7);
1159 DumpBits (&p_adec->bit_stream, 7);
1161 sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1163 sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1165 sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
1169 NeedBits (&p_adec->bit_stream, 10);
1170 code = p_adec->bit_stream.buffer >> (32 - 10);
1171 DumpBits (&p_adec->bit_stream, 10);
1173 sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1175 sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1177 sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
1181 for (s = 0; s < 3; s++)
1183 NeedBits (&p_adec->bit_stream, allocation_1[sb]);
1184 code = ( p_adec->bit_stream.buffer >>
1185 (32 - allocation_1[sb]) );
1186 DumpBits (&p_adec->bit_stream, allocation_1[sb]);
1189 slope_1[gr0][sb] * code + offset_1[gr0][sb];
1194 for (; sb < sblimit; sb++)
1198 switch (allocation_0[sb])
1201 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1202 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1206 NeedBits (&p_adec->bit_stream, 5);
1207 code = p_adec->bit_stream.buffer >> (32 - 5);
1208 DumpBits (&p_adec->bit_stream, 5);
1210 sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1211 sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1213 sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1214 sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1216 sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
1217 sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
1221 NeedBits (&p_adec->bit_stream, 7);
1222 code = p_adec->bit_stream.buffer >> (32 - 7);
1223 DumpBits (&p_adec->bit_stream, 7);
1225 sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1226 sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1228 sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1229 sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1231 sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
1232 sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
1236 NeedBits (&p_adec->bit_stream, 10);
1237 code = p_adec->bit_stream.buffer >> (32 - 10);
1238 DumpBits (&p_adec->bit_stream, 10);
1240 sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1241 sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1243 sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1244 sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1246 sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
1247 sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
1251 for (s = 0; s < 3; s++)
1253 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
1254 code = ( p_adec->bit_stream.buffer >>
1255 (32 - allocation_0[sb]) );
1256 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
1259 slope_0[gr0][sb] * code + offset_0[gr0][sb];
1261 slope_1[gr0][sb] * code + offset_1[gr0][sb];
1266 for (; sb < 32; sb++)
1268 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1269 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1272 for (s = 0; s < 3; s++)
1274 DCT32 (sample_0[s], &p_adec->bank_0);
1276 PCM (&p_adec->bank_0, &XXX_buf, 2);
1278 DCT32 (sample_1[s], &p_adec->bank_1);
1280 PCM (&p_adec->bank_1, &XXX_buf, 2);
1290 int adec_init (audiodec_t * p_adec)
1292 p_adec->bank_0.actual = p_adec->bank_0.v1;
1293 p_adec->bank_0.pos = 0;
1294 p_adec->bank_1.actual = p_adec->bank_1.v1;
1295 p_adec->bank_1.pos = 0;
1299 int adec_sync_frame (audiodec_t * p_adec, adec_sync_info_t * p_sync_info)
1301 static int mpeg1_sample_rate[3] = {44100, 48000, 32000};
1302 static int mpeg1_layer1_bit_rate[15] =
1304 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
1306 static int mpeg1_layer2_bit_rate[15] =
1308 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384
1310 static int mpeg2_layer1_bit_rate[15] =
1312 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256
1314 static int mpeg2_layer2_bit_rate[15] =
1316 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160
1318 static int * bit_rate_table[8] =
1320 NULL, NULL, mpeg2_layer2_bit_rate, mpeg2_layer1_bit_rate,
1321 NULL, NULL, mpeg1_layer2_bit_rate, mpeg1_layer1_bit_rate
1331 p_adec->bit_stream.total_bytes_read = 0;
1333 header = GetByte (&p_adec->bit_stream) << 24;
1334 header |= GetByte (&p_adec->bit_stream) << 16;
1335 header |= GetByte (&p_adec->bit_stream) << 8;
1336 header |= GetByte (&p_adec->bit_stream);
1337 p_adec->header = header;
1339 /* basic header check : sync word, no emphasis */
1340 if ((header & 0xfff00003) != 0xfff00000)
1345 /* calculate bit rate */
1346 index = (header >> 17) & 7; /* mpeg ID + layer */
1347 bit_rates = bit_rate_table[index];
1348 if (bit_rate_table == NULL)
1350 return 1; /* invalid layer */
1353 index = (header >> 12) & 15; /* bit rate index */
1358 bit_rate = bit_rates[index];
1360 /* mpeg 1 layer 2 : check that bitrate per channel is valid */
1362 if (bit_rates == mpeg1_layer2_bit_rate)
1364 if ((header & 0xc0) == 0xc0)
1368 return 1; /* invalid bitrate per channel */
1373 if ((1 << index) & 0x2e)
1375 return 1; /* invalid bitrate per channel */
1380 /* calculate sample rate */
1382 index = (header >> 10) & 3; /* sample rate index */
1388 sample_rate = mpeg1_sample_rate[index];
1389 if (!(header & 0x80000))
1391 sample_rate >>= 1; /* half sample rate for mpeg2 */
1394 /* calculate frame length */
1396 if ((header & 0x60000) == 0x60000)
1398 frame_size = 48000 * bit_rate / sample_rate;
1399 if (header & 0x200) /* padding */
1406 frame_size = 144000 * bit_rate / sample_rate;
1407 if (header & 0x200) /* padding */
1413 p_sync_info->sample_rate = sample_rate;
1414 p_sync_info->bit_rate = bit_rate;
1415 p_sync_info->frame_size = frame_size;
1416 p_adec->frame_size = frame_size;
1421 int adec_decode_frame (audiodec_t * p_adec, s16 * buffer)
1423 if (!(p_adec->header & 0x10000))
1424 { /* error check, skip it */
1425 GetByte (&p_adec->bit_stream);
1426 GetByte (&p_adec->bit_stream);
1429 /* parse audio data */
1431 p_adec->bit_stream.i_available = 0;
1433 switch ((p_adec->header >> 17) & 3)
1435 case 2: /* layer 2 */
1436 if ((p_adec->header & 0xc0) == 0xc0)
1438 if (adec_layer2_mono (p_adec, buffer))
1445 if (adec_layer2_stereo (p_adec, buffer))
1452 case 3: /* layer 1 */
1453 if ((p_adec->header & 0xc0) == 0xc0)
1455 if (adec_layer1_mono (p_adec, buffer))
1462 if (adec_layer1_stereo (p_adec, buffer))
1470 /* skip ancillary data */
1472 if ((p_adec->header & 0xf000) == 0) /* free bitrate format */
1477 /* XXX rewrite the byte counting system to reduce overhead */
1480 intf_DbgMsg ( "skip %d\n",
1481 p_adec->frame_size - p_adec->bit_stream.total_bytes_read );
1484 if (p_adec->bit_stream.total_bytes_read > p_adec->frame_size)
1486 return 1; /* overrun */
1489 while (p_adec->bit_stream.total_bytes_read < p_adec->frame_size)
1491 GetByte (&p_adec->bit_stream); /* skip ancillary data */