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"
36 #define NULL ((void *)0)
40 static float adec_scalefactor_table[64] = { /* 2 ^ (1 - i/3) */
41 2.0000000000000000, 1.5874010519681994, 1.2599210498948732,
42 1.0000000000000000, 0.7937005259840998, 0.6299605249474366,
43 0.5000000000000000, 0.3968502629920499, 0.3149802624737183,
44 0.2500000000000000, 0.1984251314960249, 0.1574901312368591,
45 0.1250000000000000, 0.0992125657480125, 0.0787450656184296,
46 0.0625000000000000, 0.0496062828740062, 0.0393725328092148,
47 0.0312500000000000, 0.0248031414370031, 0.0196862664046074,
48 0.0156250000000000, 0.0124015707185016, 0.0098431332023037,
49 0.0078125000000000, 0.0062007853592508, 0.0049215666011518,
50 0.0039062500000000, 0.0031003926796254, 0.0024607833005759,
51 0.0019531250000000, 0.0015501963398127, 0.0012303916502880,
52 0.0009765625000000, 0.0007750981699063, 0.0006151958251440,
53 0.0004882812500000, 0.0003875490849532, 0.0003075979125720,
54 0.0002441406250000, 0.0001937745424766, 0.0001537989562860,
55 0.0001220703125000, 0.0000968872712383, 0.0000768994781430,
56 0.0000610351562500, 0.0000484436356191, 0.0000384497390715,
57 0.0000305175781250, 0.0000242218178096, 0.0000192248695357,
58 0.0000152587890625, 0.0000121109089048, 0.0000096124347679,
59 0.0000076293945312, 0.0000060554544524, 0.0000048062173839,
60 0.0000038146972656, 0.0000030277272262, 0.0000024031086920,
61 0.0000019073486328, 0.0000015138636131, 0.0000012015543460,
62 0.0000009536743164 /* last element is not in the standard... invalid ??? */
65 static float adec_slope_table[15] = {
66 0.6666666666666666, 0.2857142857142857, 0.1333333333333333,
67 0.0645161290322581, 0.0317460317460317, 0.0157480314960630,
68 0.0078431372549020, 0.0039138943248532, 0.0019550342130987,
69 0.0009770395701026, 0.0004884004884005, 0.0002441704309608,
70 0.0001220777635354, 0.0000610370189520, 0.0000305180437934
73 static float adec_offset_table[15] = {
74 -0.6666666666666666, -0.8571428571428571, -0.9333333333333333,
75 -0.9677419354838710, -0.9841269841269841, -0.9921259842519685,
76 -0.9960784313725490, -0.9980430528375733, -0.9990224828934506,
77 -0.9995114802149487, -0.9997557997557998, -0.9998779147845196,
78 -0.9999389611182323, -0.9999694814905240, -0.9999847409781033
81 static u8 adec_layer1_allocation_table[15] = {
82 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
85 static int adec_bound_table[4] = {4, 8, 12, 16};
87 static int adec_layer1_mono (audiodec_t * p_adec, s16 * buffer)
97 /* parse allocation */
99 for (sb = 0; sb < 32; sb += 2)
102 tmp = GetByte ( &p_adec->bit_stream );
103 if ( (tmp >> 4) > 14 )
105 allocation[sb] = adec_layer1_allocation_table [tmp >> 4];
108 allocation[sb+1] = adec_layer1_allocation_table [tmp & 15];
111 /* parse scalefactors */
113 for (sb = 0; sb < 32; sb++)
115 if ( allocation[sb] )
120 NeedBits ( &p_adec->bit_stream, 6 );
121 index = p_adec->bit_stream.buffer >> (32 - 6);
122 DumpBits ( &p_adec->bit_stream, 6 );
124 scalefactor = adec_scalefactor_table[index];
126 slope[sb] = adec_slope_table[allocation[sb]-2] * scalefactor;
127 offset[sb] = adec_offset_table[allocation[sb]-2] * scalefactor;
133 for (s = 0; s < 12; s++)
137 for (sb = 0; sb < 32; sb++)
147 NeedBits (&p_adec->bit_stream, allocation[sb]);
148 code = p_adec->bit_stream.buffer >> (32 - allocation[sb]);
149 DumpBits (&p_adec->bit_stream, allocation[sb]);
151 sample[sb] = slope[sb] * code + offset[sb];
155 DCT32 (sample, &p_adec->bank_0);
157 PCM (&p_adec->bank_0, &XXX_buf, 1);
164 static int adec_layer1_stereo (audiodec_t * p_adec, s16 * buffer)
166 u8 allocation_0[32], allocation_1[32];
167 float slope_0[32], slope_1[32];
168 float offset_0[32], offset_1[32];
169 float sample_0[32], sample_1[32];
175 /* calculate bound */
178 if ( (p_adec->header & 0xc0) == 0x40)
179 { /* intensity stereo */
181 index = (p_adec->header >> 4) & 3;
182 bound = adec_bound_table[index];
185 /* parse allocation */
187 for (sb = 0; sb < bound; sb++)
190 tmp = GetByte (&p_adec->bit_stream);
193 allocation_0[sb] = adec_layer1_allocation_table [tmp >> 4];
196 allocation_1[sb] = adec_layer1_allocation_table [tmp & 15];
198 for (; sb < 32; sb += 2)
201 tmp = GetByte (&p_adec->bit_stream);
204 allocation_0[sb] = allocation_1[sb] = adec_layer1_allocation_table [tmp >> 4];
207 allocation_0[sb+1] = allocation_1[sb+1] = adec_layer1_allocation_table [tmp & 15];
210 /* parse scalefactors */
212 for ( sb = 0; sb < 32; sb++ )
214 if ( allocation_0[sb] )
219 NeedBits (&p_adec->bit_stream, 6);
220 index = p_adec->bit_stream.buffer >> (32 - 6);
221 DumpBits (&p_adec->bit_stream, 6);
223 scalefactor = adec_scalefactor_table[index];
225 slope_0[sb] = adec_slope_table[allocation_0[sb]-2] * scalefactor;
226 offset_0[sb] = adec_offset_table[allocation_0[sb]-2] * scalefactor;
228 if (allocation_1[sb])
233 NeedBits (&p_adec->bit_stream, 6);
234 index = p_adec->bit_stream.buffer >> (32 - 6);
235 DumpBits (&p_adec->bit_stream, 6);
237 scalefactor = adec_scalefactor_table[index];
239 slope_1[sb] = adec_slope_table[allocation_1[sb]-2] * scalefactor;
240 offset_1[sb] = adec_offset_table[allocation_1[sb]-2] * scalefactor;
246 for (s = 0; s < 12; s++)
250 for (sb = 0; sb < bound; sb++)
252 if (!allocation_0[sb])
258 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
259 code = p_adec->bit_stream.buffer >> (32 - allocation_0[sb]);
260 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
262 sample_0[sb] = slope_0[sb] * code + offset_0[sb];
264 if ( !allocation_1[sb] )
270 NeedBits (&p_adec->bit_stream, allocation_1[sb]);
271 code = p_adec->bit_stream.buffer >> (32 - allocation_1[sb]);
272 DumpBits (&p_adec->bit_stream, allocation_1[sb]);
274 sample_1[sb] = slope_1[sb] * code + offset_1[sb];
277 for (; sb < 32; sb++)
279 if (!allocation_0[sb])
288 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
289 sample = p_adec->bit_stream.buffer >> (32 - allocation_0[sb]);
290 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
292 sample_0[sb] = slope_0[sb] * sample + offset_0[sb];
293 sample_1[sb] = slope_1[sb] * sample + offset_1[sb];
297 DCT32 (sample_0, &p_adec->bank_0);
299 PCM (&p_adec->bank_0, &XXX_buf, 2);
300 DCT32 (sample_1, &p_adec->bank_1);
302 PCM (&p_adec->bank_1, &XXX_buf, 2);
318 static void adec_layer2_get_table (u32 header, u8 freq_table[15],
319 alloc_table_t ** alloc, int * sblimit)
321 static s8 table_ab0[16] = {
322 0, L3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
324 static s8 table_ab3[16] = {
325 0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16
327 static s8 table_ab11[8] = {
328 0, L3, L5, 3, L9, 4, 5, 16
330 static s8 table_ab23[8] = {
333 static alloc_table_t mpeg1_ab = {
334 {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},
335 {table_ab0, table_ab0, table_ab0, table_ab3,
336 table_ab3, table_ab3, table_ab3, table_ab3,
337 table_ab3, table_ab3, table_ab3, table_ab11,
338 table_ab11, table_ab11, table_ab11, table_ab11,
339 table_ab11, table_ab11, table_ab11, table_ab11,
340 table_ab11, table_ab11, table_ab11, table_ab23,
341 table_ab23, table_ab23, table_ab23, table_ab23,
342 table_ab23, table_ab23, NULL, NULL}
345 static s8 table_cd[16] = {
346 0, L3, L5, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
348 static alloc_table_t mpeg1_cd = {
349 {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},
350 {table_cd, table_cd, table_cd, table_cd,
351 table_cd, table_cd, table_cd, table_cd,
352 table_cd, table_cd, table_cd, table_cd,
353 NULL, NULL, NULL, NULL,
354 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
355 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
358 static s8 table_0[16] = {
359 0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
361 static s8 table_4[8] = {
362 0, L3, L5, L9, 4, 5, 6, 7
364 static alloc_table_t mpeg2 = {
365 {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},
366 {table_0, table_0, table_0, table_0,
367 table_4, table_4, table_4, table_4,
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, NULL, NULL}
376 static alloc_table_t * alloc_table [4] = {
377 &mpeg2, &mpeg1_cd, &mpeg1_ab, &mpeg1_ab
379 static int sblimit_table[12] = {
380 30, 8, 27, 30, 30, 8, 27, 27, 30, 12, 27, 30
385 if (!(header & 0x80000))
386 index = 0; /* mpeg2 */
388 index = (header >> 12) & 15; /* mpeg1, bitrate */
389 index = freq_table [index];
392 *alloc = alloc_table[index];
393 index |= (header >> 8) & 12;
394 *sblimit = sblimit_table[index];
397 static int adec_layer2_mono (audiodec_t * p_adec, s16 * buffer)
399 static u8 freq_table[15] = {3, 0, 0, 0, 1, 0, 1, 2, 2, 2, 3, 3, 3, 3, 3};
400 static float L3_table[3] = {-2/3.0, 0, 2/3.0};
401 static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
402 static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
403 2/9.0, 4/9.0, 6/9.0, 8/9.0};
410 alloc_table_t * alloc_table;
419 /* get the right allocation table */
421 adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
423 /* bound -- not required for mono ! */
427 /* parse allocation table */
429 for ( sb=0 ; sb < sblimit ; sb++ )
433 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
434 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
435 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
437 allocation[sb] = alloc_table->alloc[sb][index];
442 for ( sb = 0 ; sb < sblimit ; sb++ )
446 NeedBits (&p_adec->bit_stream, 2);
447 scfsi[sb] = p_adec->bit_stream.buffer >> (32 - 2);
448 DumpBits (&p_adec->bit_stream, 2);
452 /* Parse scalefactors */
454 for ( sb = 0 ; sb < sblimit ; sb++ )
456 if ( allocation[sb] )
458 int index_0, index_1, index_2;
459 float r_slope,r_offset;
463 NeedBits (&p_adec->bit_stream, 18);
464 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
465 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
466 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
467 DumpBits (&p_adec->bit_stream, 18);
469 if (allocation[sb] < 0)
471 slope[0][sb] = adec_scalefactor_table[index_0];
472 slope[1][sb] = adec_scalefactor_table[index_1];
473 slope[2][sb] = adec_scalefactor_table[index_2];
479 r_slope = adec_slope_table[allocation[sb]-2];
480 r_offset = adec_offset_table[allocation[sb]-2];
482 scalefactor = adec_scalefactor_table[index_0];
483 slope[0][sb] = r_slope * scalefactor;
484 offset[0][sb] = r_offset * scalefactor;
486 scalefactor = adec_scalefactor_table[index_1];
487 slope[1][sb] = r_slope * scalefactor;
488 offset[1][sb] = r_offset * scalefactor;
490 scalefactor = adec_scalefactor_table[index_2];
491 slope[2][sb] = r_slope * scalefactor;
492 offset[2][sb] = r_offset * scalefactor;
497 NeedBits ( &p_adec->bit_stream, 12 );
498 index_0 = p_adec->bit_stream.buffer >> (32-6);
499 index_2 = ( p_adec->bit_stream.buffer >> (32-12) ) & 63;
501 if ( allocation[sb] < 0 )
503 slope[0][sb] = slope[1][sb] =
504 adec_scalefactor_table[index_0];
505 slope[2][sb] = adec_scalefactor_table[index_2];
511 r_slope = adec_slope_table[allocation[sb]-2];
512 r_offset = adec_offset_table[allocation[sb]-2];
514 scalefactor = adec_scalefactor_table[index_0];
515 slope[0][sb] = slope[1][sb] = r_slope * scalefactor;
516 offset[0][sb] = offset[1][sb] = r_offset * scalefactor;
518 scalefactor = adec_scalefactor_table[index_2];
519 slope[2][sb] = r_slope * scalefactor;
520 offset[2][sb] = r_offset * scalefactor;
525 NeedBits ( &p_adec->bit_stream, 6);
526 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
527 if ( allocation[sb] < 0 )
529 slope[0][sb] = slope[1][sb] = slope[2][sb] =
530 adec_scalefactor_table[index_0];
536 r_slope = adec_slope_table[allocation[sb]-2];
537 r_offset = adec_offset_table[allocation[sb]-2];
539 scalefactor = adec_scalefactor_table[index_0];
540 slope[0][sb] = slope[1][sb] = slope[2][sb] =
541 r_slope * scalefactor;
542 offset[0][sb] = offset[1][sb] = offset[2][sb] =
543 r_offset * scalefactor;
548 NeedBits ( &p_adec->bit_stream, 12 );
549 index_0 = p_adec->bit_stream.buffer >> (32-6);
550 index_2 = ( p_adec->bit_stream.buffer >> (32-12) ) & 63;
552 if ( allocation[sb] < 0 )
554 slope[0][sb] = adec_scalefactor_table[index_0];
555 slope[2][sb] = slope[1][sb] =
556 adec_scalefactor_table[index_2];
562 r_slope = adec_slope_table[allocation[sb]-2];
563 r_offset = adec_offset_table[allocation[sb]-2];
565 scalefactor = adec_scalefactor_table[index_0];
566 slope[0][sb] = r_slope * scalefactor;
567 offset[0][sb] = r_offset * scalefactor;
569 scalefactor = adec_scalefactor_table[index_2];
570 slope[2][sb] = slope[1][sb] = r_slope * scalefactor;
571 offset[2][sb] = offset[1][sb] = r_offset * scalefactor;
581 for (gr0 = 0; gr0 < 3; gr0++)
582 for (gr1 = 0; gr1 < 4; gr1++)
586 for (sb = 0; sb < bound; sb++)
590 switch (allocation[sb])
593 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
596 NeedBits (&p_adec->bit_stream, 5);
597 code = p_adec->bit_stream.buffer >> (32 - 5);
598 DumpBits (&p_adec->bit_stream, 5);
600 sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
602 sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
604 sample[2][sb] = slope[gr0][sb] * L3_table[code];
608 NeedBits (&p_adec->bit_stream, 7);
609 code = p_adec->bit_stream.buffer >> (32 - 7);
610 DumpBits (&p_adec->bit_stream, 7);
612 sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
614 sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
616 sample[2][sb] = slope[gr0][sb] * L5_table[code];
620 NeedBits (&p_adec->bit_stream, 10);
621 code = p_adec->bit_stream.buffer >> (32 - 10);
622 DumpBits (&p_adec->bit_stream, 10);
624 sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
626 sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
628 sample[2][sb] = slope[gr0][sb] * L9_table[code];
632 for (s = 0; s < 3; s++) {
633 NeedBits (&p_adec->bit_stream, allocation[sb]);
634 code = (p_adec->bit_stream.buffer >>
635 (32 - allocation[sb]));
636 DumpBits (&p_adec->bit_stream, allocation[sb]);
639 slope[gr0][sb] * code + offset[gr0][sb];
645 for (; sb < sblimit; sb++) {
648 switch (allocation[sb]) {
650 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
653 NeedBits (&p_adec->bit_stream, 5);
654 code = p_adec->bit_stream.buffer >> (32 - 5);
655 DumpBits (&p_adec->bit_stream, 5);
657 sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
659 sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
661 sample[2][sb] = slope[gr0][sb] * L3_table[code];
665 NeedBits (&p_adec->bit_stream, 7);
666 code = p_adec->bit_stream.buffer >> (32 - 7);
667 DumpBits (&p_adec->bit_stream, 7);
669 sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
671 sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
673 sample[2][sb] = slope[gr0][sb] * L5_table[code];
677 NeedBits (&p_adec->bit_stream, 10);
678 code = p_adec->bit_stream.buffer >> (32 - 10);
679 DumpBits (&p_adec->bit_stream, 10);
681 sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
683 sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
685 sample[2][sb] = slope[gr0][sb] * L9_table[code];
689 for (s = 0; s < 3; s++) {
690 NeedBits (&p_adec->bit_stream, allocation[sb]);
691 code = (p_adec->bit_stream.buffer >>
692 (32 - allocation[sb]));
693 DumpBits (&p_adec->bit_stream, allocation[sb]);
696 slope[gr0][sb] * code + offset[gr0][sb];
700 for (; sb < 32; sb++)
702 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
705 for (s = 0; s < 3; s++)
707 DCT32 (sample[s], &p_adec->bank_0);
709 PCM (&p_adec->bank_0, &XXX_buf, 1);
722 static int adec_layer2_stereo (audiodec_t * p_adec, s16 * buffer)
724 static u8 freq_table[15] = {3, 0, 0, 0, 1, 0, 1, 2, 2, 2, 3, 3, 3, 3, 3};
725 static float L3_table[3] = {-2/3.0, 0, 2/3.0};
726 static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
727 static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
728 2/9.0, 4/9.0, 6/9.0, 8/9.0};
730 s8 allocation_0[32], allocation_1[32];
731 u8 scfsi_0[32], scfsi_1[32];
732 float slope_0[3][32], slope_1[3][32];
733 float offset_0[3][32], offset_1[3][32];
734 float sample_0[3][32], sample_1[3][32];
735 alloc_table_t * alloc_table;
743 /* get the right allocation table */
745 adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
747 /* calculate bound */
750 if ((p_adec->header & 0xc0) == 0x40) { /* intensity stereo */
752 index = (p_adec->header >> 4) & 3;
753 if (adec_bound_table[index] < sblimit)
754 bound = adec_bound_table[index];
757 /* parse allocation */
759 for (sb = 0; sb < bound; sb++) {
762 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
763 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
764 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
766 allocation_0[sb] = alloc_table->alloc[sb][index];
768 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
769 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
770 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
772 allocation_1[sb] = alloc_table->alloc[sb][index];
774 for (; sb < sblimit; sb++) {
777 NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
778 index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
779 DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
781 allocation_0[sb] = allocation_1[sb] = alloc_table->alloc[sb][index];
786 for (sb = 0; sb < sblimit; sb++) {
787 if (allocation_0[sb]) {
788 NeedBits (&p_adec->bit_stream, 2);
789 scfsi_0[sb] = p_adec->bit_stream.buffer >> (32 - 2);
790 DumpBits (&p_adec->bit_stream, 2);
792 if (allocation_1[sb]) {
793 NeedBits (&p_adec->bit_stream, 2);
794 scfsi_1[sb] = p_adec->bit_stream.buffer >> (32 - 2);
795 DumpBits (&p_adec->bit_stream, 2);
799 /* parse scalefactors */
801 for (sb = 0; sb < sblimit; sb++)
803 if (allocation_0[sb]) {
804 int index_0, index_1, index_2;
806 switch (scfsi_0[sb]) {
808 NeedBits (&p_adec->bit_stream, 18);
809 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
810 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
811 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
812 DumpBits (&p_adec->bit_stream, 18);
814 if (allocation_0[sb] < 0) {
815 slope_0[0][sb] = adec_scalefactor_table[index_0];
816 slope_0[1][sb] = adec_scalefactor_table[index_1];
817 slope_0[2][sb] = adec_scalefactor_table[index_2];
822 slope = adec_slope_table[allocation_0[sb]-2];
823 offset = adec_offset_table[allocation_0[sb]-2];
825 scalefactor = adec_scalefactor_table[index_0];
826 slope_0[0][sb] = slope * scalefactor;
827 offset_0[0][sb] = offset * scalefactor;
829 scalefactor = adec_scalefactor_table[index_1];
830 slope_0[1][sb] = slope * scalefactor;
831 offset_0[1][sb] = offset * scalefactor;
833 scalefactor = adec_scalefactor_table[index_2];
834 slope_0[2][sb] = slope * scalefactor;
835 offset_0[2][sb] = offset * scalefactor;
839 NeedBits (&p_adec->bit_stream, 12);
840 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
841 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
842 DumpBits (&p_adec->bit_stream, 12);
844 if (allocation_0[sb] < 0) {
845 slope_0[0][sb] = slope_0[1][sb] =
846 adec_scalefactor_table[index_0];
847 slope_0[2][sb] = adec_scalefactor_table[index_1];
852 slope = adec_slope_table[allocation_0[sb]-2];
853 offset = adec_offset_table[allocation_0[sb]-2];
855 scalefactor = adec_scalefactor_table[index_0];
856 slope_0[0][sb] = slope_0[1][sb] = slope * scalefactor;
857 offset_0[0][sb] = offset_0[1][sb] = offset * scalefactor;
859 scalefactor = adec_scalefactor_table[index_1];
860 slope_0[2][sb] = slope * scalefactor;
861 offset_0[2][sb] = offset * scalefactor;
865 NeedBits (&p_adec->bit_stream, 6);
866 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
867 DumpBits (&p_adec->bit_stream, 6);
869 if (allocation_0[sb] < 0) {
870 slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
871 adec_scalefactor_table[index_0];
876 slope = adec_slope_table[allocation_0[sb]-2];
877 offset = adec_offset_table[allocation_0[sb]-2];
879 scalefactor = adec_scalefactor_table[index_0];
880 slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
882 offset_0[0][sb] = offset_0[1][sb] = offset_0[2][sb] =
883 offset * scalefactor;
887 NeedBits (&p_adec->bit_stream, 12);
888 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
889 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
890 DumpBits (&p_adec->bit_stream, 12);
892 if (allocation_0[sb] < 0) {
893 slope_0[0][sb] = adec_scalefactor_table[index_0];
894 slope_0[1][sb] = slope_0[2][sb] =
895 adec_scalefactor_table[index_1];
900 slope = adec_slope_table[allocation_0[sb]-2];
901 offset = adec_offset_table[allocation_0[sb]-2];
903 scalefactor = adec_scalefactor_table[index_0];
904 slope_0[0][sb] = slope * scalefactor;
905 offset_0[0][sb] = offset * scalefactor;
907 scalefactor = adec_scalefactor_table[index_1];
908 slope_0[1][sb] = slope_0[2][sb] = slope * scalefactor;
909 offset_0[1][sb] = offset_0[2][sb] = offset * scalefactor;
914 if (allocation_1[sb]) {
915 int index_0, index_1, index_2;
917 switch (scfsi_1[sb]) {
919 NeedBits (&p_adec->bit_stream, 18);
920 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
921 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
922 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
923 DumpBits (&p_adec->bit_stream, 18);
925 if (allocation_1[sb] < 0) {
926 slope_1[0][sb] = adec_scalefactor_table[index_0];
927 slope_1[1][sb] = adec_scalefactor_table[index_1];
928 slope_1[2][sb] = adec_scalefactor_table[index_2];
933 slope = adec_slope_table[allocation_1[sb]-2];
934 offset = adec_offset_table[allocation_1[sb]-2];
936 scalefactor = adec_scalefactor_table[index_0];
937 slope_1[0][sb] = slope * scalefactor;
938 offset_1[0][sb] = offset * scalefactor;
940 scalefactor = adec_scalefactor_table[index_1];
941 slope_1[1][sb] = slope * scalefactor;
942 offset_1[1][sb] = offset * scalefactor;
944 scalefactor = adec_scalefactor_table[index_2];
945 slope_1[2][sb] = slope * scalefactor;
946 offset_1[2][sb] = offset * scalefactor;
950 NeedBits (&p_adec->bit_stream, 12);
951 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
952 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
953 DumpBits (&p_adec->bit_stream, 12);
955 if (allocation_1[sb] < 0) {
956 slope_1[0][sb] = slope_1[1][sb] =
957 adec_scalefactor_table[index_0];
958 slope_1[2][sb] = adec_scalefactor_table[index_1];
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_1[1][sb] = slope * scalefactor;
968 offset_1[0][sb] = offset_1[1][sb] = offset * scalefactor;
970 scalefactor = adec_scalefactor_table[index_1];
971 slope_1[2][sb] = slope * scalefactor;
972 offset_1[2][sb] = offset * scalefactor;
976 NeedBits (&p_adec->bit_stream, 6);
977 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
978 DumpBits (&p_adec->bit_stream, 6);
980 if (allocation_1[sb] < 0) {
981 slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
982 adec_scalefactor_table[index_0];
987 slope = adec_slope_table[allocation_1[sb]-2];
988 offset = adec_offset_table[allocation_1[sb]-2];
990 scalefactor = adec_scalefactor_table[index_0];
991 slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
993 offset_1[0][sb] = offset_1[1][sb] = offset_1[2][sb] =
994 offset * scalefactor;
998 NeedBits (&p_adec->bit_stream, 12);
999 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
1000 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
1001 DumpBits (&p_adec->bit_stream, 12);
1003 if (allocation_1[sb] < 0) {
1004 slope_1[0][sb] = adec_scalefactor_table[index_0];
1005 slope_1[1][sb] = slope_1[2][sb] =
1006 adec_scalefactor_table[index_1];
1009 float slope, offset;
1011 slope = adec_slope_table[allocation_1[sb]-2];
1012 offset = adec_offset_table[allocation_1[sb]-2];
1014 scalefactor = adec_scalefactor_table[index_0];
1015 slope_1[0][sb] = slope * scalefactor;
1016 offset_1[0][sb] = offset * scalefactor;
1018 scalefactor = adec_scalefactor_table[index_1];
1019 slope_1[1][sb] = slope_1[2][sb] = slope * scalefactor;
1020 offset_1[1][sb] = offset_1[2][sb] = offset * scalefactor;
1029 for (gr0 = 0; gr0 < 3; gr0++)
1030 for (gr1 = 0; gr1 < 4; gr1++) {
1033 for (sb = 0; sb < bound; sb++) {
1036 switch (allocation_0[sb]) {
1038 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1041 NeedBits (&p_adec->bit_stream, 5);
1042 code = p_adec->bit_stream.buffer >> (32 - 5);
1043 DumpBits (&p_adec->bit_stream, 5);
1045 sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1047 sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1049 sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
1053 NeedBits (&p_adec->bit_stream, 7);
1054 code = p_adec->bit_stream.buffer >> (32 - 7);
1055 DumpBits (&p_adec->bit_stream, 7);
1057 sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1059 sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1061 sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
1065 NeedBits (&p_adec->bit_stream, 10);
1066 code = p_adec->bit_stream.buffer >> (32 - 10);
1067 DumpBits (&p_adec->bit_stream, 10);
1069 sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1071 sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1073 sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
1077 for (s = 0; s < 3; s++) {
1078 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
1079 code = (p_adec->bit_stream.buffer >>
1080 (32 - allocation_0[sb]));
1081 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
1084 slope_0[gr0][sb] * code + offset_0[gr0][sb];
1087 switch (allocation_1[sb]) {
1089 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1092 NeedBits (&p_adec->bit_stream, 5);
1093 code = p_adec->bit_stream.buffer >> (32 - 5);
1094 DumpBits (&p_adec->bit_stream, 5);
1096 sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1098 sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1100 sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
1104 NeedBits (&p_adec->bit_stream, 7);
1105 code = p_adec->bit_stream.buffer >> (32 - 7);
1106 DumpBits (&p_adec->bit_stream, 7);
1108 sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1110 sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1112 sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
1116 NeedBits (&p_adec->bit_stream, 10);
1117 code = p_adec->bit_stream.buffer >> (32 - 10);
1118 DumpBits (&p_adec->bit_stream, 10);
1120 sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1122 sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1124 sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
1128 for (s = 0; s < 3; s++) {
1129 NeedBits (&p_adec->bit_stream, allocation_1[sb]);
1130 code = (p_adec->bit_stream.buffer >>
1131 (32 - allocation_1[sb]));
1132 DumpBits (&p_adec->bit_stream, allocation_1[sb]);
1135 slope_1[gr0][sb] * code + offset_1[gr0][sb];
1139 for (; sb < sblimit; sb++) {
1142 switch (allocation_0[sb]) {
1144 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1145 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1148 NeedBits (&p_adec->bit_stream, 5);
1149 code = p_adec->bit_stream.buffer >> (32 - 5);
1150 DumpBits (&p_adec->bit_stream, 5);
1152 sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1153 sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1155 sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1156 sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1158 sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
1159 sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
1163 NeedBits (&p_adec->bit_stream, 7);
1164 code = p_adec->bit_stream.buffer >> (32 - 7);
1165 DumpBits (&p_adec->bit_stream, 7);
1167 sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1168 sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1170 sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1171 sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1173 sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
1174 sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
1178 NeedBits (&p_adec->bit_stream, 10);
1179 code = p_adec->bit_stream.buffer >> (32 - 10);
1180 DumpBits (&p_adec->bit_stream, 10);
1182 sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1183 sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1185 sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1186 sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1188 sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
1189 sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
1193 for (s = 0; s < 3; s++) {
1194 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
1195 code = (p_adec->bit_stream.buffer >>
1196 (32 - allocation_0[sb]));
1197 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
1200 slope_0[gr0][sb] * code + offset_0[gr0][sb];
1202 slope_1[gr0][sb] * code + offset_1[gr0][sb];
1206 for (; sb < 32; sb++) {
1207 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1208 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1210 for (s = 0; s < 3; s++) {
1211 DCT32 (sample_0[s], &p_adec->bank_0);
1213 PCM (&p_adec->bank_0, &XXX_buf, 2);
1214 DCT32 (sample_1[s], &p_adec->bank_1);
1216 PCM (&p_adec->bank_1, &XXX_buf, 2);
1224 int adec_init (audiodec_t * p_adec)
1226 p_adec->bank_0.actual = p_adec->bank_0.v1;
1227 p_adec->bank_0.pos = 0;
1228 p_adec->bank_1.actual = p_adec->bank_1.v1;
1229 p_adec->bank_1.pos = 0;
1233 int adec_sync_frame (audiodec_t * p_adec, adec_sync_info_t * p_sync_info)
1235 static int mpeg1_sample_rate[3] = {44100, 48000, 32000};
1236 static int mpeg1_layer1_bit_rate[15] = {
1237 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
1239 static int mpeg1_layer2_bit_rate[15] = {
1240 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384
1242 static int mpeg2_layer1_bit_rate[15] = {
1243 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256
1245 static int mpeg2_layer2_bit_rate[15] = {
1246 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160
1248 static int * bit_rate_table[8] = {
1249 NULL, NULL, mpeg2_layer2_bit_rate, mpeg2_layer1_bit_rate,
1250 NULL, NULL, mpeg1_layer2_bit_rate, mpeg1_layer1_bit_rate
1260 p_adec->bit_stream.total_bytes_read = 0;
1262 header = GetByte (&p_adec->bit_stream) << 24;
1263 header |= GetByte (&p_adec->bit_stream) << 16;
1264 header |= GetByte (&p_adec->bit_stream) << 8;
1265 header |= GetByte (&p_adec->bit_stream);
1266 p_adec->header = header;
1268 /* basic header check : sync word, no emphasis */
1269 if ((header & 0xfff00003) != 0xfff00000)
1272 /* calculate bit rate */
1274 index = (header >> 17) & 7; /* mpeg ID + layer */
1275 bit_rates = bit_rate_table[index];
1276 if (bit_rate_table == NULL)
1277 return 1; /* invalid layer */
1279 index = (header >> 12) & 15; /* bit rate index */
1282 bit_rate = bit_rates[index];
1284 /* mpeg 1 layer 2 : check that bitrate per channel is valid */
1286 if (bit_rates == mpeg1_layer2_bit_rate) {
1287 if ((header & 0xc0) == 0xc0) { /* mono */
1289 return 1; /* invalid bitrate per channel */
1290 } else { /* stereo */
1291 if ((1 << index) & 0x2e)
1292 return 1; /* invalid bitrate per channel */
1296 /* calculate sample rate */
1298 index = (header >> 10) & 3; /* sample rate index */
1301 sample_rate = mpeg1_sample_rate[index];
1302 if (!(header & 0x80000))
1303 sample_rate >>= 1; /* half sample rate for mpeg2 */
1305 /* calculate frame length */
1307 if ((header & 0x60000) == 0x60000) { /* layer 1 */
1308 frame_size = 48000 * bit_rate / sample_rate;
1309 if (header & 0x200) /* padding */
1311 } else { /* layer >1 */
1312 frame_size = 144000 * bit_rate / sample_rate;
1313 if (header & 0x200) /* padding */
1317 p_sync_info->sample_rate = sample_rate;
1318 p_sync_info->bit_rate = bit_rate;
1319 p_sync_info->frame_size = frame_size;
1320 p_adec->frame_size = frame_size;
1325 int adec_decode_frame (audiodec_t * p_adec, s16 * buffer)
1327 if (!(p_adec->header & 0x10000)) { /* error check, skip it */
1328 GetByte (&p_adec->bit_stream);
1329 GetByte (&p_adec->bit_stream);
1332 /* parse audio data */
1334 p_adec->bit_stream.i_available = 0;
1336 switch ((p_adec->header >> 17) & 3) {
1337 case 2: /* layer 2 */
1338 if ((p_adec->header & 0xc0) == 0xc0) {
1339 if (adec_layer2_mono (p_adec, buffer))
1342 if (adec_layer2_stereo (p_adec, buffer))
1346 case 3: /* layer 1 */
1347 if ((p_adec->header & 0xc0) == 0xc0) {
1348 if (adec_layer1_mono (p_adec, buffer))
1351 if (adec_layer1_stereo (p_adec, buffer))
1357 /* skip ancillary data */
1359 if ((p_adec->header & 0xf000) == 0) /* free bitrate format */
1362 /* XXX rewrite the byte counting system to reduce overhead */
1365 intf_DbgMsg ( "skip %d\n",
1366 p_adec->frame_size - p_adec->bit_stream.total_bytes_read );
1369 if (p_adec->bit_stream.total_bytes_read > p_adec->frame_size)
1370 return 1; /* overrun */
1372 while (p_adec->bit_stream.total_bytes_read < p_adec->frame_size)
1373 GetByte (&p_adec->bit_stream); /* skip ancillary data */