]> git.sesse.net Git - vlc/blob - src/audio_decoder/audio_decoder.c
Bon. On ne rit pas, je m'�tais juste plant� dans l'en-t�te des
[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 #define NULL ((void *)0)
37
38 /**** wkn ****/
39
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 ??? */
63 };
64
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
71 };
72
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
79 };
80
81 static u8 adec_layer1_allocation_table[15] = {
82     0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
83 };
84
85 static int adec_bound_table[4] = {4, 8, 12, 16};
86
87 static int adec_layer1_mono (audiodec_t * p_adec, s16 * buffer)
88 {
89     u8 allocation[32];
90     float slope[32];
91     float offset[32];
92     float sample[32];
93
94     int sb;
95     int s;
96
97     /* parse allocation */
98
99     for (sb = 0; sb < 32; sb += 2) 
100     {
101         u8 tmp;
102         tmp = GetByte ( &p_adec->bit_stream );
103         if ( (tmp >> 4) > 14 )
104             return 1;
105         allocation[sb] = adec_layer1_allocation_table [tmp >> 4];
106         if ((tmp & 15) > 14)
107             return 1;
108         allocation[sb+1] = adec_layer1_allocation_table [tmp & 15];
109     }
110
111     /* parse scalefactors */
112
113     for (sb = 0; sb < 32; sb++) 
114     {
115             if ( allocation[sb] ) 
116         {
117                 int index;
118             float scalefactor;
119
120                 NeedBits ( &p_adec->bit_stream, 6 );
121                 index = p_adec->bit_stream.buffer >> (32 - 6);
122             DumpBits ( &p_adec->bit_stream, 6 );
123     
124                 scalefactor = adec_scalefactor_table[index];
125
126             slope[sb] = adec_slope_table[allocation[sb]-2] * scalefactor;
127                 offset[sb] = adec_offset_table[allocation[sb]-2] * scalefactor;
128         }
129     }
130
131     /* parse samples */
132
133     for (s = 0; s < 12; s++) 
134     {
135             s16 * XXX_buf;
136
137         for (sb = 0; sb < 32; sb++) 
138         {
139                 if (!allocation[sb]) 
140             {
141                         sample[sb] = 0;
142                 } 
143             else 
144             {
145                         int code;
146
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]);
150
151                         sample[sb] = slope[sb] * code + offset[sb];
152                 }
153         }  
154
155             DCT32 (sample, &p_adec->bank_0);
156         XXX_buf = buffer;
157             PCM (&p_adec->bank_0, &XXX_buf, 1);
158         buffer += 32;
159     }
160
161     return 0;
162 }
163
164 static int adec_layer1_stereo (audiodec_t * p_adec, s16 * buffer)
165 {
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];
170
171     int bound;
172     int sb;
173     int s;
174
175     /* calculate bound */
176
177     bound = 32;
178     if ( (p_adec->header & 0xc0) == 0x40) 
179     {   /* intensity stereo */
180             int index;
181         index = (p_adec->header >> 4) & 3;
182         bound = adec_bound_table[index];
183     }
184
185     /* parse allocation */
186
187     for (sb = 0; sb < bound; sb++) 
188     {
189         u8 tmp;
190         tmp = GetByte (&p_adec->bit_stream);
191         if ((tmp >> 4) > 14)
192                 return 1;
193         allocation_0[sb] = adec_layer1_allocation_table [tmp >> 4];
194         if ((tmp & 15) > 14)
195             return 1;
196         allocation_1[sb] = adec_layer1_allocation_table [tmp & 15];
197     }
198     for (; sb < 32; sb += 2) 
199     {
200         u8 tmp;
201         tmp = GetByte (&p_adec->bit_stream);
202         if ((tmp >> 4) > 14)
203             return 1;
204         allocation_0[sb] = allocation_1[sb] = adec_layer1_allocation_table [tmp >> 4];
205         if ((tmp & 15) > 14)
206             return 1;
207         allocation_0[sb+1] = allocation_1[sb+1] = adec_layer1_allocation_table [tmp & 15];
208     }
209
210     /* parse scalefactors */
211
212     for ( sb = 0; sb < 32; sb++ ) 
213     {
214         if ( allocation_0[sb] ) 
215         {
216             int index;
217             float scalefactor;
218
219             NeedBits (&p_adec->bit_stream, 6);
220             index = p_adec->bit_stream.buffer >> (32 - 6);
221             DumpBits (&p_adec->bit_stream, 6);
222
223             scalefactor = adec_scalefactor_table[index];
224
225             slope_0[sb] = adec_slope_table[allocation_0[sb]-2] * scalefactor;
226             offset_0[sb] = adec_offset_table[allocation_0[sb]-2] * scalefactor;
227         }
228         if (allocation_1[sb]) 
229         {
230             int index;
231             float scalefactor;
232
233             NeedBits (&p_adec->bit_stream, 6);
234             index = p_adec->bit_stream.buffer >> (32 - 6);
235             DumpBits (&p_adec->bit_stream, 6);
236
237             scalefactor = adec_scalefactor_table[index];
238
239             slope_1[sb] = adec_slope_table[allocation_1[sb]-2] * scalefactor;
240             offset_1[sb] = adec_offset_table[allocation_1[sb]-2] * scalefactor;
241         }
242     }
243
244     /* parse samples */
245
246     for (s = 0; s < 12; s++) 
247     {
248         s16 * XXX_buf;
249
250         for (sb = 0; sb < bound; sb++) 
251         {
252             if (!allocation_0[sb])
253                     sample_0[sb] = 0;
254             else 
255             {
256                         int code;
257
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]);
261
262                 sample_0[sb] = slope_0[sb] * code + offset_0[sb];
263             }
264             if ( !allocation_1[sb] )
265                 sample_1[sb] = 0;
266             else 
267             {
268                 int code;
269                 
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]);
273                 
274                 sample_1[sb] = slope_1[sb] * code + offset_1[sb];
275             }
276         }
277             for (; sb < 32; sb++) 
278         {
279             if (!allocation_0[sb]) 
280             {
281                 sample_0[sb] = 0;
282                 sample_1[sb] = 0;
283             } 
284             else 
285             {
286                 int sample;
287                 
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]);
291                 
292                 sample_0[sb] = slope_0[sb] * sample + offset_0[sb];
293                 sample_1[sb] = slope_1[sb] * sample + offset_1[sb];
294             }
295         }
296
297         DCT32 (sample_0, &p_adec->bank_0);
298         XXX_buf = buffer;
299         PCM (&p_adec->bank_0, &XXX_buf, 2);
300         DCT32 (sample_1, &p_adec->bank_1);
301         XXX_buf = buffer+1;
302         PCM (&p_adec->bank_1, &XXX_buf, 2);
303         buffer += 64;
304     }
305
306     return 0;
307 }
308
309 typedef struct {
310     s8 nbal[32];
311     u8 * alloc[32];
312 } alloc_table_t;
313
314 #define L3 -1
315 #define L5 -2
316 #define L9 -3
317
318 static void adec_layer2_get_table (u32 header, u8 freq_table[15],
319                                    alloc_table_t ** alloc, int * sblimit)
320 {
321     static s8 table_ab0[16] = {
322         0, L3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
323     };
324     static s8 table_ab3[16] = {
325         0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16
326     };
327     static s8 table_ab11[8] = {
328         0, L3, L5, 3, L9, 4, 5, 16
329     };
330     static s8 table_ab23[8] = {
331         0, L3, L5, 16
332     };
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}
343     };
344
345     static s8 table_cd[16] = {
346         0, L3, L5, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
347     };
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}
356     };
357
358     static s8 table_0[16] = {
359         0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
360     };
361     static s8 table_4[8] = {
362         0, L3, L5, L9, 4, 5, 6, 7
363     };
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}
374     };
375
376     static alloc_table_t * alloc_table [4] = {
377         &mpeg2, &mpeg1_cd, &mpeg1_ab, &mpeg1_ab
378     };
379     static int sblimit_table[12] = {
380         30, 8, 27, 30, 30, 8, 27, 27, 30, 12, 27, 30
381     };
382
383     int index;
384
385     if (!(header & 0x80000))
386         index = 0;                      /* mpeg2 */
387     else {
388         index = (header >> 12) & 15;    /* mpeg1, bitrate */
389         index = freq_table [index];
390     }
391
392     *alloc = alloc_table[index];
393     index |= (header >> 8) & 12;
394     *sblimit = sblimit_table[index];
395 }
396
397 static int adec_layer2_mono (audiodec_t * p_adec, s16 * buffer)
398 {
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};
404
405     s8 allocation[32];
406     u8 scfsi[32];
407     float slope[3][32];
408     float offset[3][32];
409     float sample[3][32];
410     alloc_table_t * alloc_table;
411
412     int sblimit;
413     int bound;
414     int sb;
415     int gr0, gr1;
416     int s;
417
418     
419     /* get the right allocation table */
420
421     adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
422
423     /* bound -- not required for mono ! */
424
425     bound = sblimit;
426
427     /* parse allocation table */
428
429     for ( sb=0 ; sb < sblimit ; sb++ )
430     {
431         int index;
432
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]);
436
437         allocation[sb] = alloc_table->alloc[sb][index];
438     }
439
440     /* parse scfsi */
441
442     for ( sb = 0 ; sb < sblimit ; sb++ )
443     {
444         if (allocation[sb]) 
445         {
446             NeedBits (&p_adec->bit_stream, 2);
447             scfsi[sb] = p_adec->bit_stream.buffer >> (32 - 2);
448             DumpBits (&p_adec->bit_stream, 2);
449         }
450         }
451
452     /* Parse scalefactors */
453
454     for ( sb = 0 ; sb < sblimit ; sb++ )
455     {
456         if ( allocation[sb] )
457         {
458             int index_0, index_1, index_2;
459             float r_slope,r_offset;
460             switch ( scfsi[sb] )
461             {
462                 case 0 : 
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);
468                     
469                     if (allocation[sb] < 0) 
470                     {
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];
474                     } 
475                     else 
476                     {
477                         float scalefactor;
478                         
479                         r_slope = adec_slope_table[allocation[sb]-2];
480                         r_offset = adec_offset_table[allocation[sb]-2];
481                         
482                         scalefactor = adec_scalefactor_table[index_0];
483                         slope[0][sb] = r_slope * scalefactor;
484                         offset[0][sb] = r_offset * scalefactor;
485                         
486                         scalefactor = adec_scalefactor_table[index_1];
487                         slope[1][sb] = r_slope * scalefactor;
488                         offset[1][sb] = r_offset * scalefactor;
489                         
490                         scalefactor = adec_scalefactor_table[index_2];
491                         slope[2][sb] = r_slope * scalefactor;
492                         offset[2][sb] = r_offset * scalefactor;
493                     }
494                 break;
495
496                 case 1 :
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;
500
501                     if ( allocation[sb] < 0 )
502                     {
503                         slope[0][sb] = slope[1][sb] = 
504                             adec_scalefactor_table[index_0];
505                         slope[2][sb] = adec_scalefactor_table[index_2];
506                     }
507                     else
508                     {
509                         float scalefactor;
510
511                         r_slope = adec_slope_table[allocation[sb]-2];
512                         r_offset = adec_offset_table[allocation[sb]-2];
513
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;
517
518                         scalefactor = adec_scalefactor_table[index_2];
519                         slope[2][sb] = r_slope * scalefactor;
520                         offset[2][sb] = r_offset * scalefactor;
521                     }
522                 break;
523
524                 case 2:
525                     NeedBits ( &p_adec->bit_stream, 6);
526                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
527                      if ( allocation[sb] < 0 )
528                      {
529                          slope[0][sb] = slope[1][sb] = slope[2][sb] = 
530                              adec_scalefactor_table[index_0];
531                      }
532                      else
533                      {
534                         float scalefactor;
535
536                         r_slope = adec_slope_table[allocation[sb]-2];
537                         r_offset = adec_offset_table[allocation[sb]-2];
538
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;
544                      }
545                  break;
546
547                 case 3 :
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;
551
552                     if ( allocation[sb] < 0 )
553                     {
554                         slope[0][sb] = adec_scalefactor_table[index_0];
555                         slope[2][sb] = slope[1][sb] = 
556                             adec_scalefactor_table[index_2];
557                     }
558                     else
559                     {
560                         float scalefactor;
561
562                         r_slope = adec_slope_table[allocation[sb]-2];
563                         r_offset = adec_offset_table[allocation[sb]-2];
564
565                         scalefactor = adec_scalefactor_table[index_0];
566                         slope[0][sb] = r_slope * scalefactor;
567                         offset[0][sb] = r_offset * scalefactor;
568
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;
572                     }
573                 break;
574             }    
575         }
576     }
577     
578
579     /* parse samples */
580
581     for (gr0 = 0; gr0 < 3; gr0++)
582         for (gr1 = 0; gr1 < 4; gr1++) 
583     {
584             s16 * XXX_buf;
585
586             for (sb = 0; sb < bound; sb++) 
587         {
588                 int code;
589
590                 switch (allocation[sb]) 
591         {
592                 case 0:
593                     sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
594                     break;
595                 case L3:
596                     NeedBits (&p_adec->bit_stream, 5);
597                     code = p_adec->bit_stream.buffer >> (32 - 5);
598                     DumpBits (&p_adec->bit_stream, 5);
599
600                     sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
601                     code /= 3;
602                     sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
603                     code /= 3;
604                     sample[2][sb] = slope[gr0][sb] * L3_table[code];
605
606                     break;
607                 case L5:
608                     NeedBits (&p_adec->bit_stream, 7);
609                     code = p_adec->bit_stream.buffer >> (32 - 7);
610                     DumpBits (&p_adec->bit_stream, 7);
611
612                     sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
613                     code /= 5;
614                     sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
615                     code /= 5;
616                     sample[2][sb] = slope[gr0][sb] * L5_table[code];
617
618                     break;
619                 case L9:
620                     NeedBits (&p_adec->bit_stream, 10);
621                     code = p_adec->bit_stream.buffer >> (32 - 10);
622                     DumpBits (&p_adec->bit_stream, 10);
623
624                     sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
625                     code /= 9;
626                     sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
627                     code /= 9;
628                     sample[2][sb] = slope[gr0][sb] * L9_table[code];
629
630                     break;
631                 default:
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]);
637
638                         sample[s][sb] =
639                             slope[gr0][sb] * code + offset[gr0][sb];
640                     }
641                 }
642         }
643
644         
645             for (; sb < sblimit; sb++) {
646                 int code;
647
648                 switch (allocation[sb]) {
649                 case 0:
650                     sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
651                     break;
652                 case L3:
653                     NeedBits (&p_adec->bit_stream, 5);
654                     code = p_adec->bit_stream.buffer >> (32 - 5);
655                     DumpBits (&p_adec->bit_stream, 5);
656
657                     sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
658                     code /= 3;
659                     sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
660                     code /= 3;
661                     sample[2][sb] = slope[gr0][sb] * L3_table[code];
662
663                     break;
664                 case L5:
665                     NeedBits (&p_adec->bit_stream, 7);
666                     code = p_adec->bit_stream.buffer >> (32 - 7);
667                     DumpBits (&p_adec->bit_stream, 7);
668
669                     sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
670                     code /= 5;
671                     sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
672                     code /= 5;
673                     sample[2][sb] = slope[gr0][sb] * L5_table[code];
674
675                     break;
676                 case L9:
677                     NeedBits (&p_adec->bit_stream, 10);
678                     code = p_adec->bit_stream.buffer >> (32 - 10);
679                     DumpBits (&p_adec->bit_stream, 10);
680
681                     sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
682                     code /= 9;
683                     sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
684                     code /= 9;
685                     sample[2][sb] = slope[gr0][sb] * L9_table[code];
686
687                     break;
688                 default:
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]);
694
695                         sample[s][sb] =
696                             slope[gr0][sb] * code + offset[gr0][sb];
697                     }
698                 }
699             }
700             for (; sb < 32; sb++) 
701         {
702                 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
703             }
704         
705             for (s = 0; s < 3; s++) 
706         {
707                 DCT32 (sample[s], &p_adec->bank_0);
708                 XXX_buf = buffer;
709                 PCM (&p_adec->bank_0, &XXX_buf, 1);
710                 buffer += 32;
711             }
712         
713         
714     
715     }
716
717     
718 return 0;
719     
720 }
721
722 static int adec_layer2_stereo (audiodec_t * p_adec, s16 * buffer)
723 {
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};
729
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;
736
737     int sblimit;
738     int bound;
739     int sb;
740     int gr0, gr1;
741     int s;
742
743     /* get the right allocation table */
744
745     adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
746
747     /* calculate bound */
748
749     bound = sblimit;
750     if ((p_adec->header & 0xc0) == 0x40) {      /* intensity stereo */
751         int index;
752         index = (p_adec->header >> 4) & 3;
753         if (adec_bound_table[index] < sblimit)
754             bound = adec_bound_table[index];
755     }
756
757     /* parse allocation */
758
759     for (sb = 0; sb < bound; sb++) {
760         int index;
761
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]);
765
766         allocation_0[sb] = alloc_table->alloc[sb][index];
767
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]);
771
772         allocation_1[sb] = alloc_table->alloc[sb][index];
773     }
774     for (; sb < sblimit; sb++) {
775         int index;
776
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]);
780
781         allocation_0[sb] = allocation_1[sb] = alloc_table->alloc[sb][index];
782     }
783
784     /* parse scfsi */
785
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);
791         }
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);
796         }
797     }
798
799     /* parse scalefactors */
800
801     for (sb = 0; sb < sblimit; sb++) 
802     {
803         if (allocation_0[sb]) {
804             int index_0, index_1, index_2;
805
806             switch (scfsi_0[sb]) {
807             case 0:
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);
813
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];
818                 } else {
819                     float scalefactor;
820                     float slope, offset;
821
822                     slope = adec_slope_table[allocation_0[sb]-2];
823                     offset = adec_offset_table[allocation_0[sb]-2];
824
825                     scalefactor = adec_scalefactor_table[index_0];
826                     slope_0[0][sb] = slope * scalefactor;
827                     offset_0[0][sb] = offset * scalefactor;
828
829                     scalefactor = adec_scalefactor_table[index_1];
830                     slope_0[1][sb] = slope * scalefactor;
831                     offset_0[1][sb] = offset * scalefactor;
832
833                     scalefactor = adec_scalefactor_table[index_2];
834                     slope_0[2][sb] = slope * scalefactor;
835                     offset_0[2][sb] = offset * scalefactor;
836                 }
837                 break;
838             case 1:
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);
843
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];
848                 } else {
849                     float scalefactor;
850                     float slope, offset;
851
852                     slope = adec_slope_table[allocation_0[sb]-2];
853                     offset = adec_offset_table[allocation_0[sb]-2];
854
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;
858
859                     scalefactor = adec_scalefactor_table[index_1];
860                     slope_0[2][sb] = slope * scalefactor;
861                     offset_0[2][sb] = offset * scalefactor;
862                 }
863                 break;
864             case 2:
865                 NeedBits (&p_adec->bit_stream, 6);
866                 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
867                 DumpBits (&p_adec->bit_stream, 6);
868
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];
872                 } else {
873                     float scalefactor;
874                     float slope, offset;
875
876                     slope = adec_slope_table[allocation_0[sb]-2];
877                     offset = adec_offset_table[allocation_0[sb]-2];
878
879                     scalefactor = adec_scalefactor_table[index_0];
880                     slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
881                         slope * scalefactor;
882                     offset_0[0][sb] = offset_0[1][sb] = offset_0[2][sb] =
883                         offset * scalefactor;
884                 }
885                 break;
886             case 3:
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);
891
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];
896                 } else {
897                     float scalefactor;
898                     float slope, offset;
899
900                     slope = adec_slope_table[allocation_0[sb]-2];
901                     offset = adec_offset_table[allocation_0[sb]-2];
902
903                     scalefactor = adec_scalefactor_table[index_0];
904                     slope_0[0][sb] = slope * scalefactor;
905                     offset_0[0][sb] = offset * scalefactor;
906
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;
910                 }
911                 break;
912             }
913         }
914         if (allocation_1[sb]) {
915             int index_0, index_1, index_2;
916
917             switch (scfsi_1[sb]) {
918             case 0:
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);
924
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];
929                 } else {
930                     float scalefactor;
931                     float slope, offset;
932
933                     slope = adec_slope_table[allocation_1[sb]-2];
934                     offset = adec_offset_table[allocation_1[sb]-2];
935
936                     scalefactor = adec_scalefactor_table[index_0];
937                     slope_1[0][sb] = slope * scalefactor;
938                     offset_1[0][sb] = offset * scalefactor;
939
940                     scalefactor = adec_scalefactor_table[index_1];
941                     slope_1[1][sb] = slope * scalefactor;
942                     offset_1[1][sb] = offset * scalefactor;
943
944                     scalefactor = adec_scalefactor_table[index_2];
945                     slope_1[2][sb] = slope * scalefactor;
946                     offset_1[2][sb] = offset * scalefactor;
947                 }
948                 break;
949             case 1:
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);
954
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];
959                 } else {
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_1[1][sb] = slope * scalefactor;
968                     offset_1[0][sb] = offset_1[1][sb] = offset * scalefactor;
969
970                     scalefactor = adec_scalefactor_table[index_1];
971                     slope_1[2][sb] = slope * scalefactor;
972                     offset_1[2][sb] = offset * scalefactor;
973                 }
974                 break;
975             case 2:
976                 NeedBits (&p_adec->bit_stream, 6);
977                 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
978                 DumpBits (&p_adec->bit_stream, 6);
979
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];
983                 } else {
984                     float scalefactor;
985                     float slope, offset;
986
987                     slope = adec_slope_table[allocation_1[sb]-2];
988                     offset = adec_offset_table[allocation_1[sb]-2];
989
990                     scalefactor = adec_scalefactor_table[index_0];
991                     slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
992                         slope * scalefactor;
993                     offset_1[0][sb] = offset_1[1][sb] = offset_1[2][sb] =
994                         offset * scalefactor;
995                 }
996                 break;
997             case 3:
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);
1002
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];
1007                 } else {
1008                     float scalefactor;
1009                     float slope, offset;
1010
1011                     slope = adec_slope_table[allocation_1[sb]-2];
1012                     offset = adec_offset_table[allocation_1[sb]-2];
1013
1014                     scalefactor = adec_scalefactor_table[index_0];
1015                     slope_1[0][sb] = slope * scalefactor;
1016                     offset_1[0][sb] = offset * scalefactor;
1017
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;
1021                 }
1022                 break;
1023             }
1024         }
1025     }
1026
1027     /* parse samples */
1028
1029     for (gr0 = 0; gr0 < 3; gr0++)
1030         for (gr1 = 0; gr1 < 4; gr1++) {
1031             s16 * XXX_buf;
1032
1033             for (sb = 0; sb < bound; sb++) {
1034                 int code;
1035
1036                 switch (allocation_0[sb]) {
1037                 case 0:
1038                     sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1039                     break;
1040                 case L3:
1041                     NeedBits (&p_adec->bit_stream, 5);
1042                     code = p_adec->bit_stream.buffer >> (32 - 5);
1043                     DumpBits (&p_adec->bit_stream, 5);
1044
1045                     sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1046                     code /= 3;
1047                     sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1048                     code /= 3;
1049                     sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
1050
1051                     break;
1052                 case L5:
1053                     NeedBits (&p_adec->bit_stream, 7);
1054                     code = p_adec->bit_stream.buffer >> (32 - 7);
1055                     DumpBits (&p_adec->bit_stream, 7);
1056
1057                     sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1058                     code /= 5;
1059                     sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1060                     code /= 5;
1061                     sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
1062
1063                     break;
1064                 case L9:
1065                     NeedBits (&p_adec->bit_stream, 10);
1066                     code = p_adec->bit_stream.buffer >> (32 - 10);
1067                     DumpBits (&p_adec->bit_stream, 10);
1068
1069                     sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1070                     code /= 9;
1071                     sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1072                     code /= 9;
1073                     sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
1074
1075                     break;
1076                 default:
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]);
1082
1083                         sample_0[s][sb] =
1084                             slope_0[gr0][sb] * code + offset_0[gr0][sb];
1085                     }
1086                 }
1087                 switch (allocation_1[sb]) {
1088                 case 0:
1089                     sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1090                     break;
1091                 case L3:
1092                     NeedBits (&p_adec->bit_stream, 5);
1093                     code = p_adec->bit_stream.buffer >> (32 - 5);
1094                     DumpBits (&p_adec->bit_stream, 5);
1095
1096                     sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1097                     code /= 3;
1098                     sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1099                     code /= 3;
1100                     sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
1101
1102                     break;
1103                 case L5:
1104                     NeedBits (&p_adec->bit_stream, 7);
1105                     code = p_adec->bit_stream.buffer >> (32 - 7);
1106                     DumpBits (&p_adec->bit_stream, 7);
1107
1108                     sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1109                     code /= 5;
1110                     sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1111                     code /= 5;
1112                     sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
1113
1114                     break;
1115                 case L9:
1116                     NeedBits (&p_adec->bit_stream, 10);
1117                     code = p_adec->bit_stream.buffer >> (32 - 10);
1118                     DumpBits (&p_adec->bit_stream, 10);
1119
1120                     sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1121                     code /= 9;
1122                     sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1123                     code /= 9;
1124                     sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
1125
1126                     break;
1127                 default:
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]);
1133
1134                         sample_1[s][sb] =
1135                             slope_1[gr0][sb] * code + offset_1[gr0][sb];
1136                     }
1137                 }
1138             }
1139             for (; sb < sblimit; sb++) {
1140                 int code;
1141
1142                 switch (allocation_0[sb]) {
1143                 case 0:
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;
1146                     break;
1147                 case L3:
1148                     NeedBits (&p_adec->bit_stream, 5);
1149                     code = p_adec->bit_stream.buffer >> (32 - 5);
1150                     DumpBits (&p_adec->bit_stream, 5);
1151
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];
1154                     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];
1157                     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];
1160
1161                     break;
1162                 case L5:
1163                     NeedBits (&p_adec->bit_stream, 7);
1164                     code = p_adec->bit_stream.buffer >> (32 - 7);
1165                     DumpBits (&p_adec->bit_stream, 7);
1166
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];
1169                     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];
1172                     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];
1175
1176                     break;
1177                 case L9:
1178                     NeedBits (&p_adec->bit_stream, 10);
1179                     code = p_adec->bit_stream.buffer >> (32 - 10);
1180                     DumpBits (&p_adec->bit_stream, 10);
1181
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];
1184                     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];
1187                     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];
1190
1191                     break;
1192                 default:
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]);
1198
1199                         sample_0[s][sb] =
1200                             slope_0[gr0][sb] * code + offset_0[gr0][sb];
1201                         sample_1[s][sb] =
1202                             slope_1[gr0][sb] * code + offset_1[gr0][sb];
1203                     }
1204                 }
1205             }
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;
1209             }
1210             for (s = 0; s < 3; s++) {
1211                 DCT32 (sample_0[s], &p_adec->bank_0);
1212                 XXX_buf = buffer;
1213                 PCM (&p_adec->bank_0, &XXX_buf, 2);
1214                 DCT32 (sample_1[s], &p_adec->bank_1);
1215                 XXX_buf = buffer+1;
1216                 PCM (&p_adec->bank_1, &XXX_buf, 2);
1217                 buffer += 64;
1218             }
1219         }
1220
1221     return 0;
1222 }
1223
1224 int adec_init (audiodec_t * p_adec)
1225 {
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;
1230     return 0;
1231 }
1232
1233 int adec_sync_frame (audiodec_t * p_adec, adec_sync_info_t * p_sync_info)
1234 {
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
1238     };
1239     static int mpeg1_layer2_bit_rate[15] = {
1240         0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384
1241     };
1242     static int mpeg2_layer1_bit_rate[15] = {
1243         0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256
1244     };
1245     static int mpeg2_layer2_bit_rate[15] = {
1246         0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160
1247     };
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
1251     };
1252
1253     u32 header;
1254     int index;
1255     int * bit_rates;
1256     int sample_rate;
1257     int bit_rate;
1258     int frame_size;
1259
1260     p_adec->bit_stream.total_bytes_read = 0;
1261
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;
1267
1268     /* basic header check : sync word, no emphasis */
1269     if ((header & 0xfff00003) != 0xfff00000)
1270         return 1;
1271
1272     /* calculate bit rate */
1273
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 */
1278
1279     index = (header >> 12) & 15;        /* bit rate index */
1280     if (index > 14)
1281         return 1;
1282     bit_rate = bit_rates[index];
1283
1284     /* mpeg 1 layer 2 : check that bitrate per channel is valid */
1285
1286     if (bit_rates == mpeg1_layer2_bit_rate) {
1287         if ((header & 0xc0) == 0xc0) {  /* mono */
1288             if (index > 10)
1289                 return 1;               /* invalid bitrate per channel */
1290         } else {                        /* stereo */
1291             if ((1 << index) & 0x2e)
1292                 return 1;               /* invalid bitrate per channel */
1293         }
1294     }
1295
1296     /* calculate sample rate */
1297
1298     index = (header >> 10) & 3;         /* sample rate index */
1299     if (index > 2)
1300         return 1;
1301     sample_rate = mpeg1_sample_rate[index];
1302     if (!(header & 0x80000))
1303         sample_rate >>= 1;              /* half sample rate for mpeg2 */
1304
1305     /* calculate frame length */
1306
1307     if ((header & 0x60000) == 0x60000) {        /* layer 1 */
1308         frame_size = 48000 * bit_rate / sample_rate;
1309         if (header & 0x200)     /* padding */
1310             frame_size += 4;
1311     } else {    /* layer >1 */
1312         frame_size = 144000 * bit_rate / sample_rate;
1313         if (header & 0x200)     /* padding */
1314             frame_size ++;
1315     }
1316
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;
1321
1322     return 0;
1323 }
1324
1325 int adec_decode_frame (audiodec_t * p_adec, s16 * buffer)
1326 {
1327     if (!(p_adec->header & 0x10000)) {          /* error check, skip it */
1328         GetByte (&p_adec->bit_stream);
1329         GetByte (&p_adec->bit_stream);
1330     }
1331
1332     /* parse audio data */
1333
1334     p_adec->bit_stream.i_available = 0;
1335
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))
1340                 return 1;
1341         } else {
1342             if (adec_layer2_stereo (p_adec, buffer))
1343                 return 1;
1344         }
1345         break;
1346     case 3:     /* layer 1 */
1347         if ((p_adec->header & 0xc0) == 0xc0) {
1348             if (adec_layer1_mono (p_adec, buffer))
1349                 return 1;
1350         } else {
1351             if (adec_layer1_stereo (p_adec, buffer))
1352                 return 1;
1353         }
1354         break;
1355     }
1356
1357     /* skip ancillary data */
1358
1359     if ((p_adec->header & 0xf000) == 0)         /* free bitrate format */
1360         return 0;
1361
1362     /* XXX rewrite the byte counting system to reduce overhead */
1363
1364 #if 0
1365     printf ("skip %d\n",
1366             p_adec->frame_size - p_adec->bit_stream.total_bytes_read);
1367 #endif
1368
1369     if (p_adec->bit_stream.total_bytes_read > p_adec->frame_size)
1370         return 1;                               /* overrun */
1371
1372     while (p_adec->bit_stream.total_bytes_read < p_adec->frame_size)
1373         GetByte (&p_adec->bit_stream);          /* skip ancillary data */
1374
1375     return 0;
1376 }