]> git.sesse.net Git - vlc/blob - src/audio_decoder/audio_decoder.c
. additional fix for Layer 1 mono
[vlc] / src / audio_decoder / audio_decoder.c
1 /*****************************************************************************
2  * audio_decoder.c: MPEG1 Layer I-II audio decoder
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*
24  * TODO :
25  *
26  * - optimiser les NeedBits() et les GetBits() du code là où c'est possible ;
27  *
28  */
29
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"
35
36 #include <stdio.h>
37
38 #define NULL ((void *)0)
39
40 /**** wkn ****/
41
42 static float adec_scalefactor_table[64] =
43 {   /* 2 ^ (1 - i/3) */
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 ??? */
66 };
67
68 static float adec_slope_table[15] =
69 {
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
75 };
76
77 static float adec_offset_table[15] =
78 {
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
84 };
85
86 static u8 adec_layer1_allocation_table[15] =
87 {
88     0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
89 };
90
91 static int adec_bound_table[4] = { 4, 8, 12, 16 };
92
93 static int adec_layer1_mono( audiodec_t * p_adec, s16 * buffer )
94 {
95     u8 allocation[32];
96     float slope[32];
97     float offset[32];
98     float sample[32];
99
100     int sb;
101     int s;
102
103     /* parse allocation */
104
105     for (sb = 0; sb < 32; sb += 2)
106     {
107         u8 tmp;
108         tmp = GetByte ( &p_adec->bit_stream );
109
110         if ( (tmp >> 4) > 14 )
111         {
112             return 1;
113         }
114
115         allocation[sb] = adec_layer1_allocation_table [tmp >> 4];
116
117         if ((tmp & 15) > 14)
118         {
119             return 1;
120         }
121
122         allocation[sb+1] = adec_layer1_allocation_table [tmp & 15];
123     }
124
125     /* parse scalefactors */
126
127     for ( sb = 0; sb < 32; sb++ )
128     {
129         if ( allocation[sb] )
130         {
131             int index;
132             float scalefactor;
133
134             NeedBits ( &p_adec->bit_stream, 6 );
135             index = p_adec->bit_stream.buffer >> (32 - 6);
136             DumpBits ( &p_adec->bit_stream, 6 );
137
138             scalefactor = adec_scalefactor_table[index];
139
140             slope[sb] = adec_slope_table[allocation[sb]-2] * scalefactor;
141             offset[sb] = adec_offset_table[allocation[sb]-2] * scalefactor;
142         }
143     }
144
145     /* parse samples */
146
147     for (s = 0; s < 12; s++)
148     {
149         s16 * XXX_buf;
150
151         for (sb = 0; sb < 32; sb++)
152         {
153             if (!allocation[sb])
154             {
155                 sample[sb] = 0;
156             }
157             else
158             {
159                 int code;
160
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]);
164
165                 sample[sb] = slope[sb] * code + offset[sb];
166             }
167         }
168
169         DCT32 (sample, &p_adec->bank_0);
170         XXX_buf = buffer;
171         PCM (&p_adec->bank_0, &XXX_buf, 1);
172         buffer += 32;
173     }
174
175     return 0;
176 }
177
178 static int adec_layer1_stereo (audiodec_t * p_adec, s16 * buffer)
179 {
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];
184
185     int bound;
186     int sb;
187     int s;
188
189     /* calculate bound */
190
191     bound = 32;
192     if ( (p_adec->header & 0xc0) == 0x40)
193     {   /* intensity stereo */
194         int index;
195         index = (p_adec->header >> 4) & 3;
196         bound = adec_bound_table[index];
197     }
198
199     /* parse allocation */
200
201     for (sb = 0; sb < bound; sb++)
202     {
203         u8 tmp;
204         tmp = GetByte (&p_adec->bit_stream);
205         if ((tmp >> 4) > 14)
206         {
207             return 1;
208         }
209         allocation_0[sb] = adec_layer1_allocation_table [tmp >> 4];
210         if ((tmp & 15) > 14)
211         {
212             return 1;
213         }
214         allocation_1[sb] = adec_layer1_allocation_table [tmp & 15];
215     }
216
217     for (; sb < 32; sb += 2)
218     {
219         u8 tmp;
220         tmp = GetByte (&p_adec->bit_stream);
221         if ((tmp >> 4) > 14)
222         {
223             return 1;
224         }
225         allocation_0[sb] = allocation_1[sb] = adec_layer1_allocation_table [tmp >> 4];
226         if ((tmp & 15) > 14)
227         {
228             return 1;
229         }
230         allocation_0[sb+1] = allocation_1[sb+1] = adec_layer1_allocation_table [tmp & 15];
231     }
232
233     /* parse scalefactors */
234
235     for ( sb = 0; sb < 32; sb++ )
236     {
237         if ( allocation_0[sb] )
238         {
239             int index;
240             float scalefactor;
241
242             NeedBits (&p_adec->bit_stream, 6);
243             index = p_adec->bit_stream.buffer >> (32 - 6);
244             DumpBits (&p_adec->bit_stream, 6);
245
246             scalefactor = adec_scalefactor_table[index];
247
248             slope_0[sb] = adec_slope_table[allocation_0[sb]-2] * scalefactor;
249             offset_0[sb] = adec_offset_table[allocation_0[sb]-2] * scalefactor;
250         }
251
252         if (allocation_1[sb])
253         {
254             int index;
255             float scalefactor;
256
257             NeedBits (&p_adec->bit_stream, 6);
258             index = p_adec->bit_stream.buffer >> (32 - 6);
259             DumpBits (&p_adec->bit_stream, 6);
260
261             scalefactor = adec_scalefactor_table[index];
262
263             slope_1[sb] = adec_slope_table[allocation_1[sb]-2] * scalefactor;
264             offset_1[sb] = adec_offset_table[allocation_1[sb]-2] * scalefactor;
265         }
266     }
267
268     /* parse samples */
269
270     for (s = 0; s < 12; s++)
271     {
272         s16 * XXX_buf;
273
274         for (sb = 0; sb < bound; sb++)
275         {
276             if (!allocation_0[sb])
277             {
278                 sample_0[sb] = 0;
279             }
280             else
281             {
282                 int code;
283
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]);
287
288                 sample_0[sb] = slope_0[sb] * code + offset_0[sb];
289             }
290
291             if ( !allocation_1[sb] )
292             {
293                 sample_1[sb] = 0;
294             }
295             else
296             {
297                 int code;
298
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]);
302
303                 sample_1[sb] = slope_1[sb] * code + offset_1[sb];
304             }
305         }
306
307         for (; sb < 32; sb++)
308         {
309             if (!allocation_0[sb])
310             {
311                 sample_0[sb] = 0;
312                 sample_1[sb] = 0;
313             }
314             else
315             {
316                 int code;
317
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]);
321
322                 sample_0[sb] = slope_0[sb] * code + offset_0[sb];
323                 sample_1[sb] = slope_1[sb] * code + offset_1[sb];
324             }
325         }
326
327         DCT32 (sample_0, &p_adec->bank_0);
328         XXX_buf = buffer;
329         PCM (&p_adec->bank_0, &XXX_buf, 2);
330         DCT32 (sample_1, &p_adec->bank_1);
331         XXX_buf = buffer+1;
332         PCM (&p_adec->bank_1, &XXX_buf, 2);
333         buffer += 64;
334     }
335
336     return 0;
337 }
338
339 typedef struct
340 {
341     s8 nbal[32];
342     u8 * alloc[32];
343 } alloc_table_t;
344
345 #define L3 -1
346 #define L5 -2
347 #define L9 -3
348
349 static void adec_layer2_get_table( u32 header, u8 freq_table[15],
350                                    alloc_table_t ** alloc, int * sblimit )
351 {
352     static s8 table_ab0[16] =
353     {
354         0, L3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
355     };
356     static s8 table_ab3[16] =
357     {
358         0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16
359     };
360     static s8 table_ab11[8] =
361     {
362         0, L3, L5, 3, L9, 4, 5, 16
363     };
364     static s8 table_ab23[8] =
365     {
366         0, L3, L5, 16
367     };
368     static alloc_table_t mpeg1_ab =
369     {
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}
379     };
380
381     static s8 table_cd[16] =
382     {
383         0, L3, L5, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
384     };
385     static alloc_table_t mpeg1_cd =
386     {
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}
394     };
395
396     static s8 table_0[16] =
397     {
398         0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
399     };
400     static s8 table_4[8] =
401     {
402         0, L3, L5, L9, 4, 5, 6, 7
403     };
404     static alloc_table_t mpeg2 =
405     {
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}
415     };
416
417     static alloc_table_t * alloc_table [4] =
418     {
419         &mpeg2, &mpeg1_cd, &mpeg1_ab, &mpeg1_ab
420     };
421     static int sblimit_table[12] =
422     {
423         30, 8, 27, 30, 30, 8, 27, 27, 30, 12, 27, 30
424     };
425
426     int index;
427
428     if (!(header & 0x80000))
429     {
430         index = 0; /* mpeg2 */
431     }
432     else
433     {
434         index = (header >> 12) & 15; /* mpeg1, bitrate */
435         index = freq_table [index];
436     }
437
438     *alloc = alloc_table[index];
439     index |= (header >> 8) & 12;
440     *sblimit = sblimit_table[index];
441 }
442
443 static int adec_layer2_mono (audiodec_t * p_adec, s16 * buffer)
444 {
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};
450
451     s8 allocation[32];
452     u8 scfsi[32];
453     float slope[3][32];
454     float offset[3][32];
455     float sample[3][32];
456     alloc_table_t * alloc_table;
457
458     int sblimit;
459     int sb;
460     int gr0, gr1;
461     int s;
462
463     /* get the right allocation table */
464     adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
465
466     /* parse allocation */
467     //sblimit=27;
468
469     for (sb = 0; sb < sblimit; sb++)
470     {
471         int index;
472
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]);
476
477         allocation[sb] = alloc_table->alloc[sb][index];
478     }
479
480     /* parse scfsi */
481
482     for (sb = 0; sb < sblimit; sb++)
483     {
484         if (allocation[sb])
485         {
486             NeedBits (&p_adec->bit_stream, 2);
487             scfsi[sb] = p_adec->bit_stream.buffer >> (32 - 2);
488             DumpBits (&p_adec->bit_stream, 2);
489         }
490     }
491
492     /* parse scalefactors */
493
494     for (sb = 0; sb < sblimit; sb++)
495     {
496         if (allocation[sb])
497         {
498             int index_0, index_1, index_2;
499
500             switch (scfsi[sb])
501             {
502                 case 0:
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);
508
509                     if (allocation[sb] < 0)
510                     {
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];
514                     }
515                     else
516                     {
517                         float r_scalefactor;
518                         float r_slope, r_offset;
519
520                         r_slope = adec_slope_table[allocation[sb]-2];
521                         r_offset = adec_offset_table[allocation[sb]-2];
522
523                         r_scalefactor = adec_scalefactor_table[index_0];
524                         slope[0][sb] = r_slope * r_scalefactor;
525                         offset[0][sb] = r_offset * r_scalefactor;
526
527                         r_scalefactor = adec_scalefactor_table[index_1];
528                         slope[1][sb] = r_slope * r_scalefactor;
529                         offset[1][sb] = r_offset * r_scalefactor;
530
531                         r_scalefactor = adec_scalefactor_table[index_2];
532                         slope[2][sb] = r_slope * r_scalefactor;
533                         offset[2][sb] = r_offset * r_scalefactor;
534                     }
535                 break;
536
537                 case 1:
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);
542
543                     if (allocation[sb] < 0)
544                     {
545                         slope[0][sb] = slope[1][sb] =
546                             adec_scalefactor_table[index_0];
547                         slope[2][sb] = adec_scalefactor_table[index_1];
548                     }
549                     else
550                     {
551                         float r_scalefactor;
552                         float r_slope, r_offset;
553
554                         r_slope = adec_slope_table[allocation[sb]-2];
555                         r_offset = adec_offset_table[allocation[sb]-2];
556
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;
561
562                         r_scalefactor = adec_scalefactor_table[index_1];
563                         slope[2][sb] = r_slope * r_scalefactor;
564                         offset[2][sb] = r_offset * r_scalefactor;
565                     }
566                 break;
567
568                 case 2:
569                     NeedBits (&p_adec->bit_stream, 6);
570                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
571                     DumpBits (&p_adec->bit_stream, 6);
572
573                     if (allocation[sb] < 0)
574                     {
575                         slope[0][sb] = slope[1][sb] = slope[2][sb] =
576                             adec_scalefactor_table[index_0];
577                     }
578                     else
579                     {
580                         float r_scalefactor;
581                         float r_slope, r_offset;
582
583                         r_slope = adec_slope_table[allocation[sb]-2];
584                         r_offset = adec_offset_table[allocation[sb]-2];
585
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;
591                     }
592                 break;
593
594                 case 3:
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);
599
600                     if (allocation[sb] < 0)
601                     {
602                         slope[0][sb] = adec_scalefactor_table[index_0];
603                         slope[1][sb] = slope[2][sb] =
604                             adec_scalefactor_table[index_1];
605                     }
606                     else
607                     {
608                         float r_scalefactor;
609                         float r_slope, r_offset;
610
611                         r_slope = adec_slope_table[allocation[sb]-2];
612                         r_offset = adec_offset_table[allocation[sb]-2];
613
614                         r_scalefactor = adec_scalefactor_table[index_0];
615                         slope[0][sb] = r_slope * r_scalefactor;
616                         offset[0][sb] = r_offset * r_scalefactor;
617
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;
622                     }
623                 break;
624             }
625         }
626     }
627
628     /* parse samples */
629
630     for (gr0 = 0; gr0 < 3; gr0++)
631     {
632         for (gr1 = 0; gr1 < 4; gr1++)
633         {
634             s16 * XXX_buf;
635
636             for (sb = 0; sb < sblimit; sb++)
637             {
638                 int code;
639
640                 switch (allocation[sb])
641                 {
642                     case 0:
643                         sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
644                         break;
645
646                     case L3:
647                         NeedBits (&p_adec->bit_stream, 5);
648                         code = p_adec->bit_stream.buffer >> (32 - 5);
649                         DumpBits (&p_adec->bit_stream, 5);
650
651                         sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
652                         code /= 3;
653                         sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
654                         code /= 3;
655                         sample[2][sb] = slope[gr0][sb] * L3_table[code];
656                     break;
657
658                     case L5:
659                         NeedBits (&p_adec->bit_stream, 7);
660                         code = p_adec->bit_stream.buffer >> (32 - 7);
661                         DumpBits (&p_adec->bit_stream, 7);
662
663                         sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
664                         code /= 5;
665                         sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
666                         code /= 5;
667                         sample[2][sb] = slope[gr0][sb] * L5_table[code];
668                     break;
669
670                     case L9:
671                         NeedBits (&p_adec->bit_stream, 10);
672                         code = p_adec->bit_stream.buffer >> (32 - 10);
673                         DumpBits (&p_adec->bit_stream, 10);
674
675                         sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
676                         code /= 9;
677                         sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
678                         code /= 9;
679                         sample[2][sb] = slope[gr0][sb] * L9_table[code];
680                     break;
681
682                     default:
683                         for (s = 0; s < 3; s++)
684                         {
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]);
689
690                             sample[s][sb] =
691                                 slope[gr0][sb] * code + offset[gr0][sb];
692                         }
693                 }
694             }
695
696             for (; sb < 32; sb++)
697             {
698                 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
699             }
700
701             for (s = 0; s < 3; s++)
702             {
703                 DCT32 (sample[s], &p_adec->bank_0);
704                 XXX_buf = buffer;
705                 PCM (&p_adec->bank_0, &XXX_buf, 2);
706
707                 /* FIXME: one shouldn't have to do it twice ! */
708                 DCT32 (sample[s], &p_adec->bank_1);
709                 XXX_buf = buffer+1;
710                 PCM (&p_adec->bank_1, &XXX_buf, 2);
711
712                 buffer += 64;
713             }
714         }
715     }
716
717     return 0;
718 }
719
720 static int adec_layer2_stereo (audiodec_t * p_adec, s16 * buffer)
721 {
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};
727
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;
734
735     int sblimit;
736     int bound;
737     int sb;
738     int gr0, gr1;
739     int s;
740
741     /* get the right allocation table */
742     adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
743
744     /* calculate bound */
745     bound = sblimit;
746     if ((p_adec->header & 0xc0) == 0x40) { /* intensity stereo */
747         int index;
748         index = (p_adec->header >> 4) & 3;
749         if (adec_bound_table[index] < sblimit)
750         {
751             bound = adec_bound_table[index];
752         }
753     }
754
755     /* parse allocation */
756
757     for (sb = 0; sb < bound; sb++)
758     {
759         int index;
760
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]);
764
765         allocation_0[sb] = alloc_table->alloc[sb][index];
766
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]);
770
771         allocation_1[sb] = alloc_table->alloc[sb][index];
772     }
773
774     for (; sb < sblimit; sb++)
775     {
776         int index;
777
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]);
781
782         allocation_0[sb] = allocation_1[sb] = alloc_table->alloc[sb][index];
783     }
784
785     /* parse scfsi */
786
787     for (sb = 0; sb < sblimit; sb++)
788     {
789         if (allocation_0[sb])
790         {
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);
794         }
795
796         if (allocation_1[sb])
797         {
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);
801         }
802     }
803
804     /* parse scalefactors */
805
806     for (sb = 0; sb < sblimit; sb++)
807     {
808         if (allocation_0[sb])
809         {
810             int index_0, index_1, index_2;
811
812             switch (scfsi_0[sb])
813             {
814                 case 0:
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);
820
821                     if (allocation_0[sb] < 0)
822                     {
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];
826                     }
827                     else
828                     {
829                         float scalefactor;
830                         float slope, offset;
831
832                         slope = adec_slope_table[allocation_0[sb]-2];
833                         offset = adec_offset_table[allocation_0[sb]-2];
834
835                         scalefactor = adec_scalefactor_table[index_0];
836                         slope_0[0][sb] = slope * scalefactor;
837                         offset_0[0][sb] = offset * scalefactor;
838
839                         scalefactor = adec_scalefactor_table[index_1];
840                         slope_0[1][sb] = slope * scalefactor;
841                         offset_0[1][sb] = offset * scalefactor;
842
843                         scalefactor = adec_scalefactor_table[index_2];
844                         slope_0[2][sb] = slope * scalefactor;
845                         offset_0[2][sb] = offset * scalefactor;
846                     }
847                 break;
848
849                 case 1:
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);
854
855                     if (allocation_0[sb] < 0)
856                     {
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];
860                     }
861                     else
862                     {
863                         float scalefactor;
864                         float slope, offset;
865
866                         slope = adec_slope_table[allocation_0[sb]-2];
867                         offset = adec_offset_table[allocation_0[sb]-2];
868
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;
873
874                         scalefactor = adec_scalefactor_table[index_1];
875                         slope_0[2][sb] = slope * scalefactor;
876                         offset_0[2][sb] = offset * scalefactor;
877                     }
878                 break;
879
880                 case 2:
881                     NeedBits (&p_adec->bit_stream, 6);
882                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
883                     DumpBits (&p_adec->bit_stream, 6);
884
885                     if (allocation_0[sb] < 0)
886                     {
887                         slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
888                             adec_scalefactor_table[index_0];
889                     }
890                     else
891                     {
892                         float scalefactor;
893                         float slope, offset;
894
895                         slope = adec_slope_table[allocation_0[sb]-2];
896                         offset = adec_offset_table[allocation_0[sb]-2];
897
898                         scalefactor = adec_scalefactor_table[index_0];
899                         slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
900                             slope * scalefactor;
901                         offset_0[0][sb] = offset_0[1][sb] = offset_0[2][sb] =
902                             offset * scalefactor;
903                     }
904                 break;
905
906                 case 3:
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);
911
912                     if (allocation_0[sb] < 0)
913                     {
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];
917                     }
918                     else
919                     {
920                         float scalefactor;
921                         float slope, offset;
922
923                         slope = adec_slope_table[allocation_0[sb]-2];
924                         offset = adec_offset_table[allocation_0[sb]-2];
925
926                         scalefactor = adec_scalefactor_table[index_0];
927                         slope_0[0][sb] = slope * scalefactor;
928                         offset_0[0][sb] = offset * scalefactor;
929
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;
934                     }
935                 break;
936             }
937         }
938
939         if (allocation_1[sb])
940         {
941             int index_0, index_1, index_2;
942
943             switch (scfsi_1[sb])
944             {
945                 case 0:
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);
951
952                     if (allocation_1[sb] < 0)
953                     {
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];
957                     }
958                     else
959                     {
960                         float scalefactor;
961                         float slope, offset;
962
963                         slope = adec_slope_table[allocation_1[sb]-2];
964                         offset = adec_offset_table[allocation_1[sb]-2];
965
966                         scalefactor = adec_scalefactor_table[index_0];
967                         slope_1[0][sb] = slope * scalefactor;
968                         offset_1[0][sb] = offset * scalefactor;
969
970                         scalefactor = adec_scalefactor_table[index_1];
971                         slope_1[1][sb] = slope * scalefactor;
972                         offset_1[1][sb] = offset * scalefactor;
973
974                         scalefactor = adec_scalefactor_table[index_2];
975                         slope_1[2][sb] = slope * scalefactor;
976                         offset_1[2][sb] = offset * scalefactor;
977                     }
978                 break;
979
980                 case 1:
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);
985
986                     if (allocation_1[sb] < 0)
987                     {
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];
991                     }
992                     else
993                     {
994                         float scalefactor;
995                         float slope, offset;
996
997                         slope = adec_slope_table[allocation_1[sb]-2];
998                         offset = adec_offset_table[allocation_1[sb]-2];
999
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;
1004
1005                         scalefactor = adec_scalefactor_table[index_1];
1006                         slope_1[2][sb] = slope * scalefactor;
1007                         offset_1[2][sb] = offset * scalefactor;
1008                     }
1009                 break;
1010
1011                 case 2:
1012                     NeedBits (&p_adec->bit_stream, 6);
1013                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
1014                     DumpBits (&p_adec->bit_stream, 6);
1015
1016                     if (allocation_1[sb] < 0)
1017                     {
1018                         slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
1019                             adec_scalefactor_table[index_0];
1020                     }
1021                     else
1022                     {
1023                         float scalefactor;
1024                         float slope, offset;
1025
1026                         slope = adec_slope_table[allocation_1[sb]-2];
1027                         offset = adec_offset_table[allocation_1[sb]-2];
1028
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;
1034                     }
1035                 break;
1036
1037                 case 3:
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);
1042
1043                     if (allocation_1[sb] < 0)
1044                     {
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];
1048                     }
1049                     else
1050                     {
1051                         float scalefactor;
1052                         float slope, offset;
1053
1054                         slope = adec_slope_table[allocation_1[sb]-2];
1055                         offset = adec_offset_table[allocation_1[sb]-2];
1056
1057                         scalefactor = adec_scalefactor_table[index_0];
1058                         slope_1[0][sb] = slope * scalefactor;
1059                         offset_1[0][sb] = offset * scalefactor;
1060
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;
1065                     }
1066                 break;
1067             }
1068         }
1069     }
1070
1071     /* parse samples */
1072
1073     for (gr0 = 0; gr0 < 3; gr0++)
1074     {
1075         for (gr1 = 0; gr1 < 4; gr1++)
1076         {
1077             s16 * XXX_buf;
1078
1079             for (sb = 0; sb < bound; sb++)
1080             {
1081                 int code;
1082
1083                 switch (allocation_0[sb])
1084                 {
1085                     case 0:
1086                         sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1087                         break;
1088
1089                     case L3:
1090                         NeedBits (&p_adec->bit_stream, 5);
1091                         code = p_adec->bit_stream.buffer >> (32 - 5);
1092                         DumpBits (&p_adec->bit_stream, 5);
1093
1094                         sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1095                         code /= 3;
1096                         sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1097                         code /= 3;
1098                         sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
1099                     break;
1100
1101                     case L5:
1102                         NeedBits (&p_adec->bit_stream, 7);
1103                         code = p_adec->bit_stream.buffer >> (32 - 7);
1104                         DumpBits (&p_adec->bit_stream, 7);
1105
1106                         sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1107                         code /= 5;
1108                         sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1109                         code /= 5;
1110                         sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
1111                     break;
1112
1113                     case L9:
1114                         NeedBits (&p_adec->bit_stream, 10);
1115                         code = p_adec->bit_stream.buffer >> (32 - 10);
1116                         DumpBits (&p_adec->bit_stream, 10);
1117
1118                         sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1119                         code /= 9;
1120                         sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1121                         code /= 9;
1122                         sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
1123                     break;
1124
1125                     default:
1126                         for (s = 0; s < 3; s++)
1127                         {
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]);
1132
1133                             sample_0[s][sb] =
1134                                 slope_0[gr0][sb] * code + offset_0[gr0][sb];
1135                         }
1136                 }
1137
1138                 switch (allocation_1[sb])
1139                 {
1140                     case 0:
1141                         sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1142                     break;
1143
1144                     case L3:
1145                         NeedBits (&p_adec->bit_stream, 5);
1146                         code = p_adec->bit_stream.buffer >> (32 - 5);
1147                         DumpBits (&p_adec->bit_stream, 5);
1148
1149                         sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1150                         code /= 3;
1151                         sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1152                         code /= 3;
1153                         sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
1154                     break;
1155
1156                     case L5:
1157                         NeedBits (&p_adec->bit_stream, 7);
1158                         code = p_adec->bit_stream.buffer >> (32 - 7);
1159                         DumpBits (&p_adec->bit_stream, 7);
1160
1161                         sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1162                         code /= 5;
1163                         sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1164                         code /= 5;
1165                         sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
1166                     break;
1167
1168                     case L9:
1169                         NeedBits (&p_adec->bit_stream, 10);
1170                         code = p_adec->bit_stream.buffer >> (32 - 10);
1171                         DumpBits (&p_adec->bit_stream, 10);
1172
1173                         sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1174                         code /= 9;
1175                         sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1176                         code /= 9;
1177                         sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
1178                     break;
1179
1180                     default:
1181                         for (s = 0; s < 3; s++)
1182                         {
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]);
1187
1188                             sample_1[s][sb] =
1189                                 slope_1[gr0][sb] * code + offset_1[gr0][sb];
1190                         }
1191                 }
1192             }
1193
1194             for (; sb < sblimit; sb++)
1195             {
1196                 int code;
1197
1198                 switch (allocation_0[sb])
1199                 {
1200                     case 0:
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;
1203                     break;
1204
1205                     case L3:
1206                         NeedBits (&p_adec->bit_stream, 5);
1207                         code = p_adec->bit_stream.buffer >> (32 - 5);
1208                         DumpBits (&p_adec->bit_stream, 5);
1209
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];
1212                         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];
1215                         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];
1218                     break;
1219
1220                     case L5:
1221                         NeedBits (&p_adec->bit_stream, 7);
1222                         code = p_adec->bit_stream.buffer >> (32 - 7);
1223                         DumpBits (&p_adec->bit_stream, 7);
1224
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];
1227                         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];
1230                         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];
1233                     break;
1234
1235                     case L9:
1236                         NeedBits (&p_adec->bit_stream, 10);
1237                         code = p_adec->bit_stream.buffer >> (32 - 10);
1238                         DumpBits (&p_adec->bit_stream, 10);
1239
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];
1242                         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];
1245                         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];
1248                     break;
1249
1250                     default:
1251                         for (s = 0; s < 3; s++)
1252                         {
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]);
1257
1258                             sample_0[s][sb] =
1259                                 slope_0[gr0][sb] * code + offset_0[gr0][sb];
1260                             sample_1[s][sb] =
1261                                 slope_1[gr0][sb] * code + offset_1[gr0][sb];
1262                         }
1263                 }
1264             }
1265
1266             for (; sb < 32; sb++)
1267             {
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;
1270             }
1271
1272             for (s = 0; s < 3; s++)
1273             {
1274                 DCT32 (sample_0[s], &p_adec->bank_0);
1275                 XXX_buf = buffer;
1276                 PCM (&p_adec->bank_0, &XXX_buf, 2);
1277
1278                 DCT32 (sample_1[s], &p_adec->bank_1);
1279                 XXX_buf = buffer+1;
1280                 PCM (&p_adec->bank_1, &XXX_buf, 2);
1281
1282                 buffer += 64;
1283             }
1284         }
1285     }
1286
1287     return 0;
1288 }
1289
1290 int adec_init (audiodec_t * p_adec)
1291 {
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;
1296     return 0;
1297 }
1298
1299 int adec_sync_frame (audiodec_t * p_adec, adec_sync_info_t * p_sync_info)
1300 {
1301     static int mpeg1_sample_rate[3] = {44100, 48000, 32000};
1302     static int mpeg1_layer1_bit_rate[15] =
1303     {
1304         0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
1305     };
1306     static int mpeg1_layer2_bit_rate[15] =
1307     {
1308         0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384
1309     };
1310     static int mpeg2_layer1_bit_rate[15] =
1311     {
1312         0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256
1313     };
1314     static int mpeg2_layer2_bit_rate[15] =
1315     {
1316         0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160
1317     };
1318     static int * bit_rate_table[8] =
1319     {
1320         NULL, NULL, mpeg2_layer2_bit_rate, mpeg2_layer1_bit_rate,
1321         NULL, NULL, mpeg1_layer2_bit_rate, mpeg1_layer1_bit_rate
1322     };
1323
1324     u32 header;
1325     int index;
1326     int * bit_rates;
1327     int sample_rate;
1328     int bit_rate;
1329     int frame_size;
1330
1331     p_adec->bit_stream.total_bytes_read = 0;
1332
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;
1338
1339     /* basic header check : sync word, no emphasis */
1340     if ((header & 0xfff00003) != 0xfff00000)
1341     {
1342         return 1;
1343     }
1344
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)
1349     {
1350         return 1; /* invalid layer */
1351     }
1352
1353     index = (header >> 12) & 15; /* bit rate index */
1354     if (index > 14)
1355     {
1356         return 1;
1357     }
1358     bit_rate = bit_rates[index];
1359
1360     /* mpeg 1 layer 2 : check that bitrate per channel is valid */
1361
1362     if (bit_rates == mpeg1_layer2_bit_rate)
1363     {
1364         if ((header & 0xc0) == 0xc0)
1365         {   /* mono */
1366             if (index > 10)
1367             {
1368                 return 1; /* invalid bitrate per channel */
1369             }
1370         }
1371         else
1372         {   /* stereo */
1373             if ((1 << index) & 0x2e)
1374             {
1375                 return 1; /* invalid bitrate per channel */
1376             }
1377         }
1378     }
1379
1380     /* calculate sample rate */
1381
1382     index = (header >> 10) & 3; /* sample rate index */
1383     if (index > 2)
1384     {
1385         return 1;
1386     }
1387
1388     sample_rate = mpeg1_sample_rate[index];
1389     if (!(header & 0x80000))
1390     {
1391         sample_rate >>= 1; /* half sample rate for mpeg2 */
1392     }
1393
1394     /* calculate frame length */
1395
1396     if ((header & 0x60000) == 0x60000)
1397     {   /* layer 1 */
1398         frame_size = 48000 * bit_rate / sample_rate;
1399         if (header & 0x200) /* padding */
1400         {
1401             frame_size += 4;
1402         }
1403     }
1404     else
1405     {   /* layer >1 */
1406         frame_size = 144000 * bit_rate / sample_rate;
1407         if (header & 0x200) /* padding */
1408         {
1409             frame_size ++;
1410         }
1411     }
1412
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;
1417
1418     return 0;
1419 }
1420
1421 int adec_decode_frame (audiodec_t * p_adec, s16 * buffer)
1422 {
1423     if (!(p_adec->header & 0x10000))
1424     {   /* error check, skip it */
1425         GetByte (&p_adec->bit_stream);
1426         GetByte (&p_adec->bit_stream);
1427     }
1428
1429     /* parse audio data */
1430
1431     p_adec->bit_stream.i_available = 0;
1432
1433     switch ((p_adec->header >> 17) & 3)
1434     {
1435         case 2: /* layer 2 */
1436             if ((p_adec->header & 0xc0) == 0xc0)
1437             {
1438                 if (adec_layer2_mono (p_adec, buffer))
1439                 {
1440                     return 1;
1441                 }
1442             }
1443             else
1444             {
1445                 if (adec_layer2_stereo (p_adec, buffer))
1446                 {
1447                     return 1;
1448                 }
1449             }
1450         break;
1451
1452         case 3: /* layer 1 */
1453             if ((p_adec->header & 0xc0) == 0xc0)
1454             {
1455                 if (adec_layer1_mono (p_adec, buffer))
1456                 {
1457                     return 1;
1458                 }
1459             }
1460             else
1461             {
1462                 if (adec_layer1_stereo (p_adec, buffer))
1463                 {
1464                     return 1;
1465                 }
1466             }
1467         break;
1468     }
1469
1470     /* skip ancillary data */
1471
1472     if ((p_adec->header & 0xf000) == 0) /* free bitrate format */
1473     {
1474         return 0;
1475     }
1476
1477     /* XXX rewrite the byte counting system to reduce overhead */
1478
1479 #if 0
1480     intf_DbgMsg ( "skip %d\n",
1481             p_adec->frame_size - p_adec->bit_stream.total_bytes_read );
1482 #endif
1483
1484     if (p_adec->bit_stream.total_bytes_read > p_adec->frame_size)
1485     {
1486         return 1; /* overrun */
1487     }
1488
1489     while (p_adec->bit_stream.total_bytes_read < p_adec->frame_size)
1490     {
1491         GetByte (&p_adec->bit_stream); /* skip ancillary data */
1492     }
1493
1494     return 0;
1495 }
1496