]> git.sesse.net Git - vlc/blob - src/audio_decoder/audio_decoder.c
Ajout du layer II mono (non test�) et d�but de mise en conformit� avec
[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 GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 /*
25  * TODO :
26  *
27  * - optimiser les NeedBits() et les GetBits() du code là où c'est possible ;
28  *
29  */
30
31 #include "int_types.h"
32 //#include "audio_constants.h"
33 #include "audio_decoder.h"
34 #include "audio_math.h"                                    /* DCT32(), PCM() */
35 #include "audio_bit_stream.h"
36
37 #define NULL ((void *)0)
38
39 /**** wkn ****/
40
41 static float adec_scalefactor_table[64] = {     /* 2 ^ (1 - i/3) */
42     2.0000000000000000, 1.5874010519681994, 1.2599210498948732,
43     1.0000000000000000, 0.7937005259840998, 0.6299605249474366,
44     0.5000000000000000, 0.3968502629920499, 0.3149802624737183,
45     0.2500000000000000, 0.1984251314960249, 0.1574901312368591,
46     0.1250000000000000, 0.0992125657480125, 0.0787450656184296,
47     0.0625000000000000, 0.0496062828740062, 0.0393725328092148,
48     0.0312500000000000, 0.0248031414370031, 0.0196862664046074,
49     0.0156250000000000, 0.0124015707185016, 0.0098431332023037,
50     0.0078125000000000, 0.0062007853592508, 0.0049215666011518,
51     0.0039062500000000, 0.0031003926796254, 0.0024607833005759,
52     0.0019531250000000, 0.0015501963398127, 0.0012303916502880,
53     0.0009765625000000, 0.0007750981699063, 0.0006151958251440,
54     0.0004882812500000, 0.0003875490849532, 0.0003075979125720,
55     0.0002441406250000, 0.0001937745424766, 0.0001537989562860,
56     0.0001220703125000, 0.0000968872712383, 0.0000768994781430,
57     0.0000610351562500, 0.0000484436356191, 0.0000384497390715,
58     0.0000305175781250, 0.0000242218178096, 0.0000192248695357,
59     0.0000152587890625, 0.0000121109089048, 0.0000096124347679,
60     0.0000076293945312, 0.0000060554544524, 0.0000048062173839,
61     0.0000038146972656, 0.0000030277272262, 0.0000024031086920,
62     0.0000019073486328, 0.0000015138636131, 0.0000012015543460,
63     0.0000009536743164 /* last element is not in the standard... invalid ??? */
64 };
65
66 static float adec_slope_table[15] = {
67     0.6666666666666666, 0.2857142857142857, 0.1333333333333333,
68     0.0645161290322581, 0.0317460317460317, 0.0157480314960630,
69     0.0078431372549020, 0.0039138943248532, 0.0019550342130987,
70     0.0009770395701026, 0.0004884004884005, 0.0002441704309608,
71     0.0001220777635354, 0.0000610370189520, 0.0000305180437934
72 };
73
74 static float adec_offset_table[15] = {
75     -0.6666666666666666, -0.8571428571428571, -0.9333333333333333,
76     -0.9677419354838710, -0.9841269841269841, -0.9921259842519685,
77     -0.9960784313725490, -0.9980430528375733, -0.9990224828934506,
78     -0.9995114802149487, -0.9997557997557998, -0.9998779147845196,
79     -0.9999389611182323, -0.9999694814905240, -0.9999847409781033
80 };
81
82 static u8 adec_layer1_allocation_table[15] = {
83     0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
84 };
85
86 static int adec_bound_table[4] = {4, 8, 12, 16};
87
88 static int adec_layer1_mono (audiodec_t * p_adec, s16 * buffer)
89 {
90     u8 allocation[32];
91     float slope[32];
92     float offset[32];
93     float sample[32];
94
95     int sb;
96     int s;
97
98     /* parse allocation */
99
100     for (sb = 0; sb < 32; sb += 2) 
101     {
102         u8 tmp;
103         tmp = GetByte ( &p_adec->bit_stream );
104         if ( (tmp >> 4) > 14 )
105             return 1;
106         allocation[sb] = adec_layer1_allocation_table [tmp >> 4];
107         if ((tmp & 15) > 14)
108             return 1;
109         allocation[sb+1] = adec_layer1_allocation_table [tmp & 15];
110     }
111
112     /* parse scalefactors */
113
114     for (sb = 0; sb < 32; sb++) 
115     {
116             if ( allocation[sb] ) 
117         {
118                 int index;
119             float scalefactor;
120
121                 NeedBits ( &p_adec->bit_stream, 6 );
122                 index = p_adec->bit_stream.buffer >> (32 - 6);
123             DumpBits ( &p_adec->bit_stream, 6 );
124     
125                 scalefactor = adec_scalefactor_table[index];
126
127             slope[sb] = adec_slope_table[allocation[sb]-2] * scalefactor;
128                 offset[sb] = adec_offset_table[allocation[sb]-2] * scalefactor;
129         }
130     }
131
132     /* parse samples */
133
134     for (s = 0; s < 12; s++) 
135     {
136             s16 * XXX_buf;
137
138         for (sb = 0; sb < 32; sb++) 
139         {
140                 if (!allocation[sb]) 
141             {
142                         sample[sb] = 0;
143                 } 
144             else 
145             {
146                         int code;
147
148                         NeedBits (&p_adec->bit_stream, allocation[sb]);
149                         code = p_adec->bit_stream.buffer >> (32 - allocation[sb]);
150                         DumpBits (&p_adec->bit_stream, allocation[sb]);
151
152                         sample[sb] = slope[sb] * code + offset[sb];
153                 }
154         }  
155
156             DCT32 (sample, &p_adec->bank_0);
157         XXX_buf = buffer;
158             PCM (&p_adec->bank_0, &XXX_buf, 1);
159         buffer += 32;
160     }
161
162     return 0;
163 }
164
165 static int adec_layer1_stereo (audiodec_t * p_adec, s16 * buffer)
166 {
167     u8 allocation_0[32], allocation_1[32];
168     float slope_0[32], slope_1[32];
169     float offset_0[32], offset_1[32];
170     float sample_0[32], sample_1[32];
171
172     int bound;
173     int sb;
174     int s;
175
176     /* calculate bound */
177
178     bound = 32;
179     if ( (p_adec->header & 0xc0) == 0x40) 
180     {   /* intensity stereo */
181             int index;
182         index = (p_adec->header >> 4) & 3;
183         bound = adec_bound_table[index];
184     }
185
186     /* parse allocation */
187
188     for (sb = 0; sb < bound; sb++) 
189     {
190         u8 tmp;
191         tmp = GetByte (&p_adec->bit_stream);
192         if ((tmp >> 4) > 14)
193                 return 1;
194         allocation_0[sb] = adec_layer1_allocation_table [tmp >> 4];
195         if ((tmp & 15) > 14)
196             return 1;
197         allocation_1[sb] = adec_layer1_allocation_table [tmp & 15];
198     }
199     for (; sb < 32; sb += 2) 
200     {
201         u8 tmp;
202         tmp = GetByte (&p_adec->bit_stream);
203         if ((tmp >> 4) > 14)
204             return 1;
205         allocation_0[sb] = allocation_1[sb] = adec_layer1_allocation_table [tmp >> 4];
206         if ((tmp & 15) > 14)
207             return 1;
208         allocation_0[sb+1] = allocation_1[sb+1] = adec_layer1_allocation_table [tmp & 15];
209     }
210
211     /* parse scalefactors */
212
213     for ( sb = 0; sb < 32; sb++ ) 
214     {
215         if ( allocation_0[sb] ) 
216         {
217             int index;
218             float scalefactor;
219
220             NeedBits (&p_adec->bit_stream, 6);
221             index = p_adec->bit_stream.buffer >> (32 - 6);
222             DumpBits (&p_adec->bit_stream, 6);
223
224             scalefactor = adec_scalefactor_table[index];
225
226             slope_0[sb] = adec_slope_table[allocation_0[sb]-2] * scalefactor;
227             offset_0[sb] = adec_offset_table[allocation_0[sb]-2] * scalefactor;
228         }
229         if (allocation_1[sb]) 
230         {
231             int index;
232             float scalefactor;
233
234             NeedBits (&p_adec->bit_stream, 6);
235             index = p_adec->bit_stream.buffer >> (32 - 6);
236             DumpBits (&p_adec->bit_stream, 6);
237
238             scalefactor = adec_scalefactor_table[index];
239
240             slope_1[sb] = adec_slope_table[allocation_1[sb]-2] * scalefactor;
241             offset_1[sb] = adec_offset_table[allocation_1[sb]-2] * scalefactor;
242         }
243     }
244
245     /* parse samples */
246
247     for (s = 0; s < 12; s++) 
248     {
249         s16 * XXX_buf;
250
251         for (sb = 0; sb < bound; sb++) 
252         {
253             if (!allocation_0[sb])
254                     sample_0[sb] = 0;
255             else 
256             {
257                         int code;
258
259                 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
260                 code = p_adec->bit_stream.buffer >> (32 - allocation_0[sb]);
261                 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
262
263                 sample_0[sb] = slope_0[sb] * code + offset_0[sb];
264             }
265             if ( !allocation_1[sb] )
266                 sample_1[sb] = 0;
267             else 
268             {
269                 int code;
270                 
271                 NeedBits (&p_adec->bit_stream, allocation_1[sb]);
272                 code = p_adec->bit_stream.buffer >> (32 - allocation_1[sb]);
273                 DumpBits (&p_adec->bit_stream, allocation_1[sb]);
274                 
275                 sample_1[sb] = slope_1[sb] * code + offset_1[sb];
276             }
277         }
278             for (; sb < 32; sb++) 
279         {
280             if (!allocation_0[sb]) 
281             {
282                 sample_0[sb] = 0;
283                 sample_1[sb] = 0;
284             } 
285             else 
286             {
287                 int sample;
288                 
289                 NeedBits (&p_adec->bit_stream, allocation_0[sb]);
290                 sample = p_adec->bit_stream.buffer >> (32 - allocation_0[sb]);
291                 DumpBits (&p_adec->bit_stream, allocation_0[sb]);
292                 
293                 sample_0[sb] = slope_0[sb] * sample + offset_0[sb];
294                 sample_1[sb] = slope_1[sb] * sample + offset_1[sb];
295             }
296         }
297
298         DCT32 (sample_0, &p_adec->bank_0);
299         XXX_buf = buffer;
300         PCM (&p_adec->bank_0, &XXX_buf, 2);
301         DCT32 (sample_1, &p_adec->bank_1);
302         XXX_buf = buffer+1;
303         PCM (&p_adec->bank_1, &XXX_buf, 2);
304         buffer += 64;
305     }
306
307     return 0;
308 }
309
310 typedef struct {
311     s8 nbal[32];
312     u8 * alloc[32];
313 } alloc_table_t;
314
315 #define L3 -1
316 #define L5 -2
317 #define L9 -3
318
319 static void adec_layer2_get_table (u32 header, u8 freq_table[15],
320                                    alloc_table_t ** alloc, int * sblimit)
321 {
322     static s8 table_ab0[16] = {
323         0, L3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
324     };
325     static s8 table_ab3[16] = {
326         0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16
327     };
328     static s8 table_ab11[8] = {
329         0, L3, L5, 3, L9, 4, 5, 16
330     };
331     static s8 table_ab23[8] = {
332         0, L3, L5, 16
333     };
334     static alloc_table_t mpeg1_ab = {
335         {4,4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,0,0},
336         {table_ab0,  table_ab0,  table_ab0,  table_ab3,
337          table_ab3,  table_ab3,  table_ab3,  table_ab3,
338          table_ab3,  table_ab3,  table_ab3,  table_ab11,
339          table_ab11, table_ab11, table_ab11, table_ab11,
340          table_ab11, table_ab11, table_ab11, table_ab11,
341          table_ab11, table_ab11, table_ab11, table_ab23,
342          table_ab23, table_ab23, table_ab23, table_ab23,
343          table_ab23, table_ab23, NULL, NULL}
344     };
345
346     static s8 table_cd[16] = {
347         0, L3, L5, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
348     };
349     static alloc_table_t mpeg1_cd = {
350         {4,4,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
351         {table_cd, table_cd, table_cd, table_cd,
352          table_cd, table_cd, table_cd, table_cd,
353          table_cd, table_cd, table_cd, table_cd,
354          NULL, NULL, NULL, NULL,
355          NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
356          NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
357     };
358
359     static s8 table_0[16] = {
360         0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
361     };
362     static s8 table_4[8] = {
363         0, L3, L5, L9, 4, 5, 6, 7
364     };
365     static alloc_table_t mpeg2 = {
366         {4,4,4,4,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0},
367         {table_0, table_0, table_0, table_0,
368          table_4, table_4, table_4, table_4,
369          table_4, table_4, table_4, table_4,
370          table_4, table_4, table_4, table_4,
371          table_4, table_4, table_4, table_4,
372          table_4, table_4, table_4, table_4,
373          table_4, table_4, table_4, table_4,
374          table_4, table_4, NULL, NULL}
375     };
376
377     static alloc_table_t * alloc_table [4] = {
378         &mpeg2, &mpeg1_cd, &mpeg1_ab, &mpeg1_ab
379     };
380     static int sblimit_table[12] = {
381         30, 8, 27, 30, 30, 8, 27, 27, 30, 12, 27, 30
382     };
383
384     int index;
385
386     if (!(header & 0x80000))
387         index = 0;                      /* mpeg2 */
388     else {
389         index = (header >> 12) & 15;    /* mpeg1, bitrate */
390         index = freq_table [index];
391     }
392
393     *alloc = alloc_table[index];
394     index |= (header >> 8) & 12;
395     *sblimit = sblimit_table[index];
396 }
397
398 static int adec_layer2_mono (audiodec_t * p_adec, s16 * buffer)
399 {
400     static u8 freq_table[15] = {3, 0, 0, 0, 1, 0, 1, 2, 2, 2, 3, 3, 3, 3, 3};
401     static float L3_table[3] = {-2/3.0, 0, 2/3.0};
402     static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
403     static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
404                                 2/9.0, 4/9.0, 6/9.0, 8/9.0};
405
406     s8 allocation[32];
407     u8 scfsi[32];
408     float slope[3][32];
409     float offset[3][32];
410     float sample[3][32];
411     alloc_table_t * alloc_table;
412
413     int sblimit;
414     int bound;
415     int sb;
416     int gr0, gr1;
417     int s;
418
419     
420     /* get the right allocation table */
421
422     adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
423
424     /* bound -- not required for mono ! */
425
426     bound = sblimit;
427
428     /* parse allocation table */
429
430     for ( sb=0 ; sb < sblimit ; sb++ )
431     {
432         int index;
433
434         NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
435         index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
436         DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
437
438         allocation[sb] = alloc_table->alloc[sb][index];
439     }
440
441     /* parse scfsi */
442
443     for ( sb = 0 ; sb < sblimit ; sb++ )
444     {
445         if (allocation[sb]) 
446         {
447             NeedBits (&p_adec->bit_stream, 2);
448             scfsi[sb] = p_adec->bit_stream.buffer >> (32 - 2);
449             DumpBits (&p_adec->bit_stream, 2);
450         }
451         }
452
453     /* Parse scalefactors */
454
455     for ( sb = 0 ; sb < sblimit ; sb++ )
456     {
457         if ( allocation[sb] )
458         {
459             int index_0, index_1, index_2;
460             float r_slope,r_offset;
461             switch ( scfsi[sb] )
462             {
463                 case 0 : 
464                     NeedBits (&p_adec->bit_stream, 18);
465                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
466                     index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
467                     index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
468                     DumpBits (&p_adec->bit_stream, 18);
469                     
470                     if (allocation[sb] < 0) 
471                     {
472                         slope[0][sb] = adec_scalefactor_table[index_0];
473                         slope[1][sb] = adec_scalefactor_table[index_1];
474                         slope[2][sb] = adec_scalefactor_table[index_2];
475                     } 
476                     else 
477                     {
478                         float scalefactor;
479                         
480                         r_slope = adec_slope_table[allocation[sb]-2];
481                         r_offset = adec_offset_table[allocation[sb]-2];
482                         
483                         scalefactor = adec_scalefactor_table[index_0];
484                         slope[0][sb] = r_slope * scalefactor;
485                         offset[0][sb] = r_offset * scalefactor;
486                         
487                         scalefactor = adec_scalefactor_table[index_1];
488                         slope[1][sb] = r_slope * scalefactor;
489                         offset[1][sb] = r_offset * scalefactor;
490                         
491                         scalefactor = adec_scalefactor_table[index_2];
492                         slope[2][sb] = r_slope * scalefactor;
493                         offset[2][sb] = r_offset * scalefactor;
494                     }
495                 break;
496
497                 case 1 :
498                     NeedBits ( &p_adec->bit_stream, 12 );
499                     index_0 = p_adec->bit_stream.buffer >> (32-6);
500                     index_2 = ( p_adec->bit_stream.buffer >> (32-12) ) & 63;
501
502                     if ( allocation[sb] < 0 )
503                     {
504                         slope[0][sb] = slope[1][sb] = 
505                             adec_scalefactor_table[index_0];
506                         slope[2][sb] = adec_scalefactor_table[index_2];
507                     }
508                     else
509                     {
510                         float scalefactor;
511
512                         r_slope = adec_slope_table[allocation[sb]-2];
513                         r_offset = adec_offset_table[allocation[sb]-2];
514
515                         scalefactor = adec_scalefactor_table[index_0];
516                         slope[0][sb] = slope[1][sb] = r_slope * scalefactor;
517                         offset[0][sb] = offset[1][sb] = r_offset * scalefactor;
518
519                         scalefactor = adec_scalefactor_table[index_2];
520                         slope[2][sb] = r_slope * scalefactor;
521                         offset[2][sb] = r_offset * scalefactor;
522                     }
523                 break;
524
525                 case 2:
526                     NeedBits ( &p_adec->bit_stream, 6);
527                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
528                      if ( allocation[sb] < 0 )
529                      {
530                          slope[0][sb] = slope[1][sb] = slope[2][sb] = 
531                              adec_scalefactor_table[index_0];
532                      }
533                      else
534                      {
535                         float scalefactor;
536
537                         r_slope = adec_slope_table[allocation[sb]-2];
538                         r_offset = adec_offset_table[allocation[sb]-2];
539
540                         scalefactor = adec_scalefactor_table[index_0];
541                         slope[0][sb] = slope[1][sb] = slope[2][sb] = 
542                             r_slope * scalefactor;
543                         offset[0][sb] = offset[1][sb] = offset[2][sb] = 
544                             r_offset * scalefactor;
545                      }
546                  break;
547
548                 case 3 :
549                     NeedBits ( &p_adec->bit_stream, 12 );
550                     index_0 = p_adec->bit_stream.buffer >> (32-6);
551                     index_2 = ( p_adec->bit_stream.buffer >> (32-12) ) & 63;
552
553                     if ( allocation[sb] < 0 )
554                     {
555                         slope[0][sb] = adec_scalefactor_table[index_0];
556                         slope[2][sb] = slope[1][sb] = 
557                             adec_scalefactor_table[index_2];
558                     }
559                     else
560                     {
561                         float scalefactor;
562
563                         r_slope = adec_slope_table[allocation[sb]-2];
564                         r_offset = adec_offset_table[allocation[sb]-2];
565
566                         scalefactor = adec_scalefactor_table[index_0];
567                         slope[0][sb] = r_slope * scalefactor;
568                         offset[0][sb] = r_offset * scalefactor;
569
570                         scalefactor = adec_scalefactor_table[index_2];
571                         slope[2][sb] = slope[1][sb] = r_slope * scalefactor;
572                         offset[2][sb] = offset[1][sb] = r_offset * scalefactor;
573                     }
574                 break;
575             }    
576         }
577     }
578     
579
580     /* parse samples */
581
582     for (gr0 = 0; gr0 < 3; gr0++)
583         for (gr1 = 0; gr1 < 4; gr1++) 
584     {
585             s16 * XXX_buf;
586
587             for (sb = 0; sb < bound; sb++) 
588         {
589                 int code;
590
591                 switch (allocation[sb]) 
592         {
593                 case 0:
594                     sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
595                     break;
596                 case L3:
597                     NeedBits (&p_adec->bit_stream, 5);
598                     code = p_adec->bit_stream.buffer >> (32 - 5);
599                     DumpBits (&p_adec->bit_stream, 5);
600
601                     sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
602                     code /= 3;
603                     sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
604                     code /= 3;
605                     sample[2][sb] = slope[gr0][sb] * L3_table[code];
606
607                     break;
608                 case L5:
609                     NeedBits (&p_adec->bit_stream, 7);
610                     code = p_adec->bit_stream.buffer >> (32 - 7);
611                     DumpBits (&p_adec->bit_stream, 7);
612
613                     sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
614                     code /= 5;
615                     sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
616                     code /= 5;
617                     sample[2][sb] = slope[gr0][sb] * L5_table[code];
618
619                     break;
620                 case L9:
621                     NeedBits (&p_adec->bit_stream, 10);
622                     code = p_adec->bit_stream.buffer >> (32 - 10);
623                     DumpBits (&p_adec->bit_stream, 10);
624
625                     sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
626                     code /= 9;
627                     sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
628                     code /= 9;
629                     sample[2][sb] = slope[gr0][sb] * L9_table[code];
630
631                     break;
632                 default:
633                     for (s = 0; s < 3; s++) {
634                         NeedBits (&p_adec->bit_stream, allocation[sb]);
635                         code = (p_adec->bit_stream.buffer >>
636                                 (32 - allocation[sb]));
637                         DumpBits (&p_adec->bit_stream, allocation[sb]);
638
639                         sample[s][sb] =
640                             slope[gr0][sb] * code + offset[gr0][sb];
641                     }
642                 }
643         }
644
645         
646             for (; sb < sblimit; sb++) {
647                 int code;
648
649                 switch (allocation[sb]) {
650                 case 0:
651                     sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
652                     break;
653                 case L3:
654                     NeedBits (&p_adec->bit_stream, 5);
655                     code = p_adec->bit_stream.buffer >> (32 - 5);
656                     DumpBits (&p_adec->bit_stream, 5);
657
658                     sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
659                     code /= 3;
660                     sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
661                     code /= 3;
662                     sample[2][sb] = slope[gr0][sb] * L3_table[code];
663
664                     break;
665                 case L5:
666                     NeedBits (&p_adec->bit_stream, 7);
667                     code = p_adec->bit_stream.buffer >> (32 - 7);
668                     DumpBits (&p_adec->bit_stream, 7);
669
670                     sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
671                     code /= 5;
672                     sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
673                     code /= 5;
674                     sample[2][sb] = slope[gr0][sb] * L5_table[code];
675
676                     break;
677                 case L9:
678                     NeedBits (&p_adec->bit_stream, 10);
679                     code = p_adec->bit_stream.buffer >> (32 - 10);
680                     DumpBits (&p_adec->bit_stream, 10);
681
682                     sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
683                     code /= 9;
684                     sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
685                     code /= 9;
686                     sample[2][sb] = slope[gr0][sb] * L9_table[code];
687
688                     break;
689                 default:
690                     for (s = 0; s < 3; s++) {
691                         NeedBits (&p_adec->bit_stream, allocation[sb]);
692                         code = (p_adec->bit_stream.buffer >>
693                                 (32 - allocation[sb]));
694                         DumpBits (&p_adec->bit_stream, allocation[sb]);
695
696                         sample[s][sb] =
697                             slope[gr0][sb] * code + offset[gr0][sb];
698                     }
699                 }
700             }
701             for (; sb < 32; sb++) 
702         {
703                 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
704             }
705         
706             for (s = 0; s < 3; s++) 
707         {
708                 DCT32 (sample[s], &p_adec->bank_0);
709                 XXX_buf = buffer;
710                 PCM (&p_adec->bank_0, &XXX_buf, 1);
711                 buffer += 32;
712             }
713         
714         
715     
716     }
717
718     
719 return 0;
720     
721 }
722
723 static int adec_layer2_stereo (audiodec_t * p_adec, s16 * buffer)
724 {
725     static u8 freq_table[15] = {3, 0, 0, 0, 1, 0, 1, 2, 2, 2, 3, 3, 3, 3, 3};
726     static float L3_table[3] = {-2/3.0, 0, 2/3.0};
727     static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
728     static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
729                                 2/9.0, 4/9.0, 6/9.0, 8/9.0};
730
731     s8 allocation_0[32], allocation_1[32];
732     u8 scfsi_0[32], scfsi_1[32];
733     float slope_0[3][32], slope_1[3][32];
734     float offset_0[3][32], offset_1[3][32];
735     float sample_0[3][32], sample_1[3][32];
736     alloc_table_t * alloc_table;
737
738     int sblimit;
739     int bound;
740     int sb;
741     int gr0, gr1;
742     int s;
743
744     /* get the right allocation table */
745
746     adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
747
748     /* calculate bound */
749
750     bound = sblimit;
751     if ((p_adec->header & 0xc0) == 0x40) {      /* intensity stereo */
752         int index;
753         index = (p_adec->header >> 4) & 3;
754         if (adec_bound_table[index] < sblimit)
755             bound = adec_bound_table[index];
756     }
757
758     /* parse allocation */
759
760     for (sb = 0; sb < bound; sb++) {
761         int index;
762
763         NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
764         index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
765         DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
766
767         allocation_0[sb] = alloc_table->alloc[sb][index];
768
769         NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
770         index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
771         DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
772
773         allocation_1[sb] = alloc_table->alloc[sb][index];
774     }
775     for (; sb < sblimit; sb++) {
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         if (allocation_0[sb]) {
789             NeedBits (&p_adec->bit_stream, 2);
790             scfsi_0[sb] = p_adec->bit_stream.buffer >> (32 - 2);
791             DumpBits (&p_adec->bit_stream, 2);
792         }
793         if (allocation_1[sb]) {
794             NeedBits (&p_adec->bit_stream, 2);
795             scfsi_1[sb] = p_adec->bit_stream.buffer >> (32 - 2);
796             DumpBits (&p_adec->bit_stream, 2);
797         }
798     }
799
800     /* parse scalefactors */
801
802     for (sb = 0; sb < sblimit; sb++) 
803     {
804         if (allocation_0[sb]) {
805             int index_0, index_1, index_2;
806
807             switch (scfsi_0[sb]) {
808             case 0:
809                 NeedBits (&p_adec->bit_stream, 18);
810                 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
811                 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
812                 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
813                 DumpBits (&p_adec->bit_stream, 18);
814
815                 if (allocation_0[sb] < 0) {
816                     slope_0[0][sb] = adec_scalefactor_table[index_0];
817                     slope_0[1][sb] = adec_scalefactor_table[index_1];
818                     slope_0[2][sb] = adec_scalefactor_table[index_2];
819                 } else {
820                     float scalefactor;
821                     float slope, offset;
822
823                     slope = adec_slope_table[allocation_0[sb]-2];
824                     offset = adec_offset_table[allocation_0[sb]-2];
825
826                     scalefactor = adec_scalefactor_table[index_0];
827                     slope_0[0][sb] = slope * scalefactor;
828                     offset_0[0][sb] = offset * scalefactor;
829
830                     scalefactor = adec_scalefactor_table[index_1];
831                     slope_0[1][sb] = slope * scalefactor;
832                     offset_0[1][sb] = offset * scalefactor;
833
834                     scalefactor = adec_scalefactor_table[index_2];
835                     slope_0[2][sb] = slope * scalefactor;
836                     offset_0[2][sb] = offset * scalefactor;
837                 }
838                 break;
839             case 1:
840                 NeedBits (&p_adec->bit_stream, 12);
841                 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
842                 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
843                 DumpBits (&p_adec->bit_stream, 12);
844
845                 if (allocation_0[sb] < 0) {
846                     slope_0[0][sb] = slope_0[1][sb] =
847                         adec_scalefactor_table[index_0];
848                     slope_0[2][sb] = adec_scalefactor_table[index_1];
849                 } else {
850                     float scalefactor;
851                     float slope, offset;
852
853                     slope = adec_slope_table[allocation_0[sb]-2];
854                     offset = adec_offset_table[allocation_0[sb]-2];
855
856                     scalefactor = adec_scalefactor_table[index_0];
857                     slope_0[0][sb] = slope_0[1][sb] = slope * scalefactor;
858                     offset_0[0][sb] = offset_0[1][sb] = offset * scalefactor;
859
860                     scalefactor = adec_scalefactor_table[index_1];
861                     slope_0[2][sb] = slope * scalefactor;
862                     offset_0[2][sb] = offset * scalefactor;
863                 }
864                 break;
865             case 2:
866                 NeedBits (&p_adec->bit_stream, 6);
867                 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
868                 DumpBits (&p_adec->bit_stream, 6);
869
870                 if (allocation_0[sb] < 0) {
871                     slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
872                         adec_scalefactor_table[index_0];
873                 } else {
874                     float scalefactor;
875                     float slope, offset;
876
877                     slope = adec_slope_table[allocation_0[sb]-2];
878                     offset = adec_offset_table[allocation_0[sb]-2];
879
880                     scalefactor = adec_scalefactor_table[index_0];
881                     slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
882                         slope * scalefactor;
883                     offset_0[0][sb] = offset_0[1][sb] = offset_0[2][sb] =
884                         offset * scalefactor;
885                 }
886                 break;
887             case 3:
888                 NeedBits (&p_adec->bit_stream, 12);
889                 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
890                 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
891                 DumpBits (&p_adec->bit_stream, 12);
892
893                 if (allocation_0[sb] < 0) {
894                     slope_0[0][sb] = adec_scalefactor_table[index_0];
895                     slope_0[1][sb] = slope_0[2][sb] =
896                         adec_scalefactor_table[index_1];
897                 } else {
898                     float scalefactor;
899                     float slope, offset;
900
901                     slope = adec_slope_table[allocation_0[sb]-2];
902                     offset = adec_offset_table[allocation_0[sb]-2];
903
904                     scalefactor = adec_scalefactor_table[index_0];
905                     slope_0[0][sb] = slope * scalefactor;
906                     offset_0[0][sb] = offset * scalefactor;
907
908                     scalefactor = adec_scalefactor_table[index_1];
909                     slope_0[1][sb] = slope_0[2][sb] = slope * scalefactor;
910                     offset_0[1][sb] = offset_0[2][sb] = offset * scalefactor;
911                 }
912                 break;
913             }
914         }
915         if (allocation_1[sb]) {
916             int index_0, index_1, index_2;
917
918             switch (scfsi_1[sb]) {
919             case 0:
920                 NeedBits (&p_adec->bit_stream, 18);
921                 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
922                 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
923                 index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
924                 DumpBits (&p_adec->bit_stream, 18);
925
926                 if (allocation_1[sb] < 0) {
927                     slope_1[0][sb] = adec_scalefactor_table[index_0];
928                     slope_1[1][sb] = adec_scalefactor_table[index_1];
929                     slope_1[2][sb] = adec_scalefactor_table[index_2];
930                 } else {
931                     float scalefactor;
932                     float slope, offset;
933
934                     slope = adec_slope_table[allocation_1[sb]-2];
935                     offset = adec_offset_table[allocation_1[sb]-2];
936
937                     scalefactor = adec_scalefactor_table[index_0];
938                     slope_1[0][sb] = slope * scalefactor;
939                     offset_1[0][sb] = offset * scalefactor;
940
941                     scalefactor = adec_scalefactor_table[index_1];
942                     slope_1[1][sb] = slope * scalefactor;
943                     offset_1[1][sb] = offset * scalefactor;
944
945                     scalefactor = adec_scalefactor_table[index_2];
946                     slope_1[2][sb] = slope * scalefactor;
947                     offset_1[2][sb] = offset * scalefactor;
948                 }
949                 break;
950             case 1:
951                 NeedBits (&p_adec->bit_stream, 12);
952                 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
953                 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
954                 DumpBits (&p_adec->bit_stream, 12);
955
956                 if (allocation_1[sb] < 0) {
957                     slope_1[0][sb] = slope_1[1][sb] =
958                         adec_scalefactor_table[index_0];
959                     slope_1[2][sb] = adec_scalefactor_table[index_1];
960                 } else {
961                     float scalefactor;
962                     float slope, offset;
963
964                     slope = adec_slope_table[allocation_1[sb]-2];
965                     offset = adec_offset_table[allocation_1[sb]-2];
966
967                     scalefactor = adec_scalefactor_table[index_0];
968                     slope_1[0][sb] = slope_1[1][sb] = slope * scalefactor;
969                     offset_1[0][sb] = offset_1[1][sb] = offset * scalefactor;
970
971                     scalefactor = adec_scalefactor_table[index_1];
972                     slope_1[2][sb] = slope * scalefactor;
973                     offset_1[2][sb] = offset * scalefactor;
974                 }
975                 break;
976             case 2:
977                 NeedBits (&p_adec->bit_stream, 6);
978                 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
979                 DumpBits (&p_adec->bit_stream, 6);
980
981                 if (allocation_1[sb] < 0) {
982                     slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
983                         adec_scalefactor_table[index_0];
984                 } else {
985                     float scalefactor;
986                     float slope, offset;
987
988                     slope = adec_slope_table[allocation_1[sb]-2];
989                     offset = adec_offset_table[allocation_1[sb]-2];
990
991                     scalefactor = adec_scalefactor_table[index_0];
992                     slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
993                         slope * scalefactor;
994                     offset_1[0][sb] = offset_1[1][sb] = offset_1[2][sb] =
995                         offset * scalefactor;
996                 }
997                 break;
998             case 3:
999                 NeedBits (&p_adec->bit_stream, 12);
1000                 index_0 = p_adec->bit_stream.buffer >> (32 - 6);
1001                 index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
1002                 DumpBits (&p_adec->bit_stream, 12);
1003
1004                 if (allocation_1[sb] < 0) {
1005                     slope_1[0][sb] = adec_scalefactor_table[index_0];
1006                     slope_1[1][sb] = slope_1[2][sb] =
1007                         adec_scalefactor_table[index_1];
1008                 } else {
1009                     float scalefactor;
1010                     float slope, offset;
1011
1012                     slope = adec_slope_table[allocation_1[sb]-2];
1013                     offset = adec_offset_table[allocation_1[sb]-2];
1014
1015                     scalefactor = adec_scalefactor_table[index_0];
1016                     slope_1[0][sb] = slope * scalefactor;
1017                     offset_1[0][sb] = offset * scalefactor;
1018
1019                     scalefactor = adec_scalefactor_table[index_1];
1020                     slope_1[1][sb] = slope_1[2][sb] = slope * scalefactor;
1021                     offset_1[1][sb] = offset_1[2][sb] = offset * scalefactor;
1022                 }
1023                 break;
1024             }
1025         }
1026     }
1027
1028     /* parse samples */
1029
1030     for (gr0 = 0; gr0 < 3; gr0++)
1031         for (gr1 = 0; gr1 < 4; gr1++) {
1032             s16 * XXX_buf;
1033
1034             for (sb = 0; sb < bound; sb++) {
1035                 int code;
1036
1037                 switch (allocation_0[sb]) {
1038                 case 0:
1039                     sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1040                     break;
1041                 case L3:
1042                     NeedBits (&p_adec->bit_stream, 5);
1043                     code = p_adec->bit_stream.buffer >> (32 - 5);
1044                     DumpBits (&p_adec->bit_stream, 5);
1045
1046                     sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1047                     code /= 3;
1048                     sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1049                     code /= 3;
1050                     sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
1051
1052                     break;
1053                 case L5:
1054                     NeedBits (&p_adec->bit_stream, 7);
1055                     code = p_adec->bit_stream.buffer >> (32 - 7);
1056                     DumpBits (&p_adec->bit_stream, 7);
1057
1058                     sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1059                     code /= 5;
1060                     sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1061                     code /= 5;
1062                     sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
1063
1064                     break;
1065                 case L9:
1066                     NeedBits (&p_adec->bit_stream, 10);
1067                     code = p_adec->bit_stream.buffer >> (32 - 10);
1068                     DumpBits (&p_adec->bit_stream, 10);
1069
1070                     sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1071                     code /= 9;
1072                     sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1073                     code /= 9;
1074                     sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
1075
1076                     break;
1077                 default:
1078                     for (s = 0; s < 3; s++) {
1079                         NeedBits (&p_adec->bit_stream, allocation_0[sb]);
1080                         code = (p_adec->bit_stream.buffer >>
1081                                 (32 - allocation_0[sb]));
1082                         DumpBits (&p_adec->bit_stream, allocation_0[sb]);
1083
1084                         sample_0[s][sb] =
1085                             slope_0[gr0][sb] * code + offset_0[gr0][sb];
1086                     }
1087                 }
1088                 switch (allocation_1[sb]) {
1089                 case 0:
1090                     sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1091                     break;
1092                 case L3:
1093                     NeedBits (&p_adec->bit_stream, 5);
1094                     code = p_adec->bit_stream.buffer >> (32 - 5);
1095                     DumpBits (&p_adec->bit_stream, 5);
1096
1097                     sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1098                     code /= 3;
1099                     sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1100                     code /= 3;
1101                     sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
1102
1103                     break;
1104                 case L5:
1105                     NeedBits (&p_adec->bit_stream, 7);
1106                     code = p_adec->bit_stream.buffer >> (32 - 7);
1107                     DumpBits (&p_adec->bit_stream, 7);
1108
1109                     sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1110                     code /= 5;
1111                     sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1112                     code /= 5;
1113                     sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
1114
1115                     break;
1116                 case L9:
1117                     NeedBits (&p_adec->bit_stream, 10);
1118                     code = p_adec->bit_stream.buffer >> (32 - 10);
1119                     DumpBits (&p_adec->bit_stream, 10);
1120
1121                     sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1122                     code /= 9;
1123                     sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1124                     code /= 9;
1125                     sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
1126
1127                     break;
1128                 default:
1129                     for (s = 0; s < 3; s++) {
1130                         NeedBits (&p_adec->bit_stream, allocation_1[sb]);
1131                         code = (p_adec->bit_stream.buffer >>
1132                                 (32 - allocation_1[sb]));
1133                         DumpBits (&p_adec->bit_stream, allocation_1[sb]);
1134
1135                         sample_1[s][sb] =
1136                             slope_1[gr0][sb] * code + offset_1[gr0][sb];
1137                     }
1138                 }
1139             }
1140             for (; sb < sblimit; sb++) {
1141                 int code;
1142
1143                 switch (allocation_0[sb]) {
1144                 case 0:
1145                     sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1146                     sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1147                     break;
1148                 case L3:
1149                     NeedBits (&p_adec->bit_stream, 5);
1150                     code = p_adec->bit_stream.buffer >> (32 - 5);
1151                     DumpBits (&p_adec->bit_stream, 5);
1152
1153                     sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1154                     sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1155                     code /= 3;
1156                     sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
1157                     sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
1158                     code /= 3;
1159                     sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
1160                     sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
1161
1162                     break;
1163                 case L5:
1164                     NeedBits (&p_adec->bit_stream, 7);
1165                     code = p_adec->bit_stream.buffer >> (32 - 7);
1166                     DumpBits (&p_adec->bit_stream, 7);
1167
1168                     sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1169                     sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1170                     code /= 5;
1171                     sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
1172                     sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
1173                     code /= 5;
1174                     sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
1175                     sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
1176
1177                     break;
1178                 case L9:
1179                     NeedBits (&p_adec->bit_stream, 10);
1180                     code = p_adec->bit_stream.buffer >> (32 - 10);
1181                     DumpBits (&p_adec->bit_stream, 10);
1182
1183                     sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1184                     sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1185                     code /= 9;
1186                     sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
1187                     sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
1188                     code /= 9;
1189                     sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
1190                     sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
1191
1192                     break;
1193                 default:
1194                     for (s = 0; s < 3; s++) {
1195                         NeedBits (&p_adec->bit_stream, allocation_0[sb]);
1196                         code = (p_adec->bit_stream.buffer >>
1197                                 (32 - allocation_0[sb]));
1198                         DumpBits (&p_adec->bit_stream, allocation_0[sb]);
1199
1200                         sample_0[s][sb] =
1201                             slope_0[gr0][sb] * code + offset_0[gr0][sb];
1202                         sample_1[s][sb] =
1203                             slope_1[gr0][sb] * code + offset_1[gr0][sb];
1204                     }
1205                 }
1206             }
1207             for (; sb < 32; sb++) {
1208                 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1209                 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1210             }
1211             for (s = 0; s < 3; s++) {
1212                 DCT32 (sample_0[s], &p_adec->bank_0);
1213                 XXX_buf = buffer;
1214                 PCM (&p_adec->bank_0, &XXX_buf, 2);
1215                 DCT32 (sample_1[s], &p_adec->bank_1);
1216                 XXX_buf = buffer+1;
1217                 PCM (&p_adec->bank_1, &XXX_buf, 2);
1218                 buffer += 64;
1219             }
1220         }
1221
1222     return 0;
1223 }
1224
1225 int adec_init (audiodec_t * p_adec)
1226 {
1227     p_adec->bank_0.actual = p_adec->bank_0.v1;
1228     p_adec->bank_0.pos = 0;
1229     p_adec->bank_1.actual = p_adec->bank_1.v1;
1230     p_adec->bank_1.pos = 0;
1231     return 0;
1232 }
1233
1234 int adec_sync_frame (audiodec_t * p_adec, adec_sync_info_t * p_sync_info)
1235 {
1236     static int mpeg1_sample_rate[3] = {44100, 48000, 32000};
1237     static int mpeg1_layer1_bit_rate[15] = {
1238         0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
1239     };
1240     static int mpeg1_layer2_bit_rate[15] = {
1241         0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384
1242     };
1243     static int mpeg2_layer1_bit_rate[15] = {
1244         0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256
1245     };
1246     static int mpeg2_layer2_bit_rate[15] = {
1247         0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160
1248     };
1249     static int * bit_rate_table[8] = {
1250         NULL, NULL, mpeg2_layer2_bit_rate, mpeg2_layer1_bit_rate,
1251         NULL, NULL, mpeg1_layer2_bit_rate, mpeg1_layer1_bit_rate
1252     };
1253
1254     u32 header;
1255     int index;
1256     int * bit_rates;
1257     int sample_rate;
1258     int bit_rate;
1259     int frame_size;
1260
1261     p_adec->bit_stream.total_bytes_read = 0;
1262
1263     header = GetByte (&p_adec->bit_stream) << 24;
1264     header |= GetByte (&p_adec->bit_stream) << 16;
1265     header |= GetByte (&p_adec->bit_stream) << 8;
1266     header |= GetByte (&p_adec->bit_stream);
1267     p_adec->header = header;
1268
1269     /* basic header check : sync word, no emphasis */
1270     if ((header & 0xfff00003) != 0xfff00000)
1271         return 1;
1272
1273     /* calculate bit rate */
1274
1275     index = (header >> 17) & 7;         /* mpeg ID + layer */
1276     bit_rates = bit_rate_table[index];
1277     if (bit_rate_table == NULL)
1278         return 1;                       /* invalid layer */
1279
1280     index = (header >> 12) & 15;        /* bit rate index */
1281     if (index > 14)
1282         return 1;
1283     bit_rate = bit_rates[index];
1284
1285     /* mpeg 1 layer 2 : check that bitrate per channel is valid */
1286
1287     if (bit_rates == mpeg1_layer2_bit_rate) {
1288         if ((header & 0xc0) == 0xc0) {  /* mono */
1289             if (index > 10)
1290                 return 1;               /* invalid bitrate per channel */
1291         } else {                        /* stereo */
1292             if ((1 << index) & 0x2e)
1293                 return 1;               /* invalid bitrate per channel */
1294         }
1295     }
1296
1297     /* calculate sample rate */
1298
1299     index = (header >> 10) & 3;         /* sample rate index */
1300     if (index > 2)
1301         return 1;
1302     sample_rate = mpeg1_sample_rate[index];
1303     if (!(header & 0x80000))
1304         sample_rate >>= 1;              /* half sample rate for mpeg2 */
1305
1306     /* calculate frame length */
1307
1308     if ((header & 0x60000) == 0x60000) {        /* layer 1 */
1309         frame_size = 48000 * bit_rate / sample_rate;
1310         if (header & 0x200)     /* padding */
1311             frame_size += 4;
1312     } else {    /* layer >1 */
1313         frame_size = 144000 * bit_rate / sample_rate;
1314         if (header & 0x200)     /* padding */
1315             frame_size ++;
1316     }
1317
1318     p_sync_info->sample_rate = sample_rate;
1319     p_sync_info->bit_rate = bit_rate;
1320     p_sync_info->frame_size = frame_size;
1321     p_adec->frame_size = frame_size;
1322
1323     return 0;
1324 }
1325
1326 int adec_decode_frame (audiodec_t * p_adec, s16 * buffer)
1327 {
1328     if (!(p_adec->header & 0x10000)) {          /* error check, skip it */
1329         GetByte (&p_adec->bit_stream);
1330         GetByte (&p_adec->bit_stream);
1331     }
1332
1333     /* parse audio data */
1334
1335     p_adec->bit_stream.i_available = 0;
1336
1337     switch ((p_adec->header >> 17) & 3) {
1338     case 2:     /* layer 2 */
1339         if ((p_adec->header & 0xc0) == 0xc0) {
1340             if (adec_layer2_mono (p_adec, buffer))
1341                 return 1;
1342         } else {
1343             if (adec_layer2_stereo (p_adec, buffer))
1344                 return 1;
1345         }
1346         break;
1347     case 3:     /* layer 1 */
1348         if ((p_adec->header & 0xc0) == 0xc0) {
1349             if (adec_layer1_mono (p_adec, buffer))
1350                 return 1;
1351         } else {
1352             if (adec_layer1_stereo (p_adec, buffer))
1353                 return 1;
1354         }
1355         break;
1356     }
1357
1358     /* skip ancillary data */
1359
1360     if ((p_adec->header & 0xf000) == 0)         /* free bitrate format */
1361         return 0;
1362
1363     /* XXX rewrite the byte counting system to reduce overhead */
1364
1365 #if 0
1366     printf ("skip %d\n",
1367             p_adec->frame_size - p_adec->bit_stream.total_bytes_read);
1368 #endif
1369
1370     if (p_adec->bit_stream.total_bytes_read > p_adec->frame_size)
1371         return 1;                               /* overrun */
1372
1373     while (p_adec->bit_stream.total_bytes_read < p_adec->frame_size)
1374         GetByte (&p_adec->bit_stream);          /* skip ancillary data */
1375
1376     return 0;
1377 }