]> git.sesse.net Git - vlc/blob - src/audio_decoder/adec_layer2.c
. this is a coding style patch which removes all "foo(bar){" constructions
[vlc] / src / audio_decoder / adec_layer2.c
1 /*****************************************************************************
2  * adec_layer2.c: MPEG Layer 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
32 #include "adec_generic.h"
33 #include "adec_math.h"                                     /* DCT32(), PCM() */
34 #include "adec_bit_stream.h"
35
36 #define NULL ((void *)0)
37
38 /**** wkn ****/
39
40 static float adec_scalefactor_table[64] =
41 {   /* 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 {
68     0.6666666666666666, 0.2857142857142857, 0.1333333333333333,
69     0.0645161290322581, 0.0317460317460317, 0.0157480314960630,
70     0.0078431372549020, 0.0039138943248532, 0.0019550342130987,
71     0.0009770395701026, 0.0004884004884005, 0.0002441704309608,
72     0.0001220777635354, 0.0000610370189520, 0.0000305180437934
73 };
74
75 static float adec_offset_table[15] =
76 {
77     -0.6666666666666666, -0.8571428571428571, -0.9333333333333333,
78     -0.9677419354838710, -0.9841269841269841, -0.9921259842519685,
79     -0.9960784313725490, -0.9980430528375733, -0.9990224828934506,
80     -0.9995114802149487, -0.9997557997557998, -0.9998779147845196,
81     -0.9999389611182323, -0.9999694814905240, -0.9999847409781033
82 };
83
84 static int adec_bound_table[4] = { 4, 8, 12, 16 };
85
86 typedef struct
87 {
88     s8 nbal[32];
89     u8 * alloc[32];
90 } alloc_table_t;
91
92 #define L3 -1
93 #define L5 -2
94 #define L9 -3
95
96 static void adec_layer2_get_table( u32 header, u8 freq_table[15],
97                                    alloc_table_t ** alloc, int * sblimit )
98 {
99     static s8 table_ab0[16] =
100     {
101         0, L3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
102     };
103     static s8 table_ab3[16] =
104     {
105         0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16
106     };
107     static s8 table_ab11[8] =
108     {
109         0, L3, L5, 3, L9, 4, 5, 16
110     };
111     static s8 table_ab23[8] =
112     {
113         0, L3, L5, 16
114     };
115     static alloc_table_t mpeg1_ab =
116     {
117         {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},
118         {table_ab0,  table_ab0,  table_ab0,  table_ab3,
119          table_ab3,  table_ab3,  table_ab3,  table_ab3,
120          table_ab3,  table_ab3,  table_ab3,  table_ab11,
121          table_ab11, table_ab11, table_ab11, table_ab11,
122          table_ab11, table_ab11, table_ab11, table_ab11,
123          table_ab11, table_ab11, table_ab11, table_ab23,
124          table_ab23, table_ab23, table_ab23, table_ab23,
125          table_ab23, table_ab23, NULL, NULL}
126     };
127
128     static s8 table_cd[16] =
129     {
130         0, L3, L5, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
131     };
132     static alloc_table_t mpeg1_cd =
133     {
134         {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},
135         {table_cd, table_cd, table_cd, table_cd,
136          table_cd, table_cd, table_cd, table_cd,
137          table_cd, table_cd, table_cd, table_cd,
138          NULL, NULL, NULL, NULL,
139          NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
140          NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
141     };
142
143     static s8 table_0[16] =
144     {
145         0, L3, L5, 3, L9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
146     };
147     static s8 table_4[8] =
148     {
149         0, L3, L5, L9, 4, 5, 6, 7
150     };
151     static alloc_table_t mpeg2 =
152     {
153         {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},
154         {table_0, table_0, table_0, table_0,
155          table_4, table_4, table_4, table_4,
156          table_4, table_4, table_4, table_4,
157          table_4, table_4, table_4, table_4,
158          table_4, table_4, table_4, table_4,
159          table_4, table_4, table_4, table_4,
160          table_4, table_4, table_4, table_4,
161          table_4, table_4, NULL, NULL}
162     };
163
164     static alloc_table_t * alloc_table [4] =
165     {
166         &mpeg2, &mpeg1_cd, &mpeg1_ab, &mpeg1_ab
167     };
168     static int sblimit_table[12] =
169     {
170         30, 8, 27, 30, 30, 8, 27, 27, 30, 12, 27, 30
171     };
172
173     int index;
174
175     if (!(header & 0x80000))
176     {
177         index = 0; /* mpeg2 */
178     }
179     else
180     {
181         index = (header >> 12) & 15; /* mpeg1, bitrate */
182         index = freq_table [index];
183     }
184
185     *alloc = alloc_table[index];
186     index |= (header >> 8) & 12;
187     *sblimit = sblimit_table[index];
188 }
189
190 int adec_layer2_mono (audiodec_t * p_adec, s16 * buffer)
191 {
192     static u8 freq_table[15] = {2, 1, 1, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2};
193     static float L3_table[3] = {-2/3.0, 0, 2/3.0};
194     static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
195     static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
196                                 2/9.0, 4/9.0, 6/9.0, 8/9.0};
197
198     s8 allocation[32];
199     u8 scfsi[32];
200     float slope[3][32];
201     float offset[3][32];
202     float sample[3][32];
203     alloc_table_t * alloc_table;
204
205     int sblimit;
206     int sb;
207     int gr0, gr1;
208     int s;
209
210     /* get the right allocation table */
211     adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
212
213     /* parse allocation */
214     //sblimit=27;
215
216     for (sb = 0; sb < sblimit; sb++)
217     {
218         int index;
219
220         NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
221         index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
222         DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
223
224         allocation[sb] = alloc_table->alloc[sb][index];
225     }
226
227     /* parse scfsi */
228
229     for (sb = 0; sb < sblimit; sb++)
230     {
231         if (allocation[sb])
232         {
233             NeedBits (&p_adec->bit_stream, 2);
234             scfsi[sb] = p_adec->bit_stream.buffer >> (32 - 2);
235             DumpBits (&p_adec->bit_stream, 2);
236         }
237     }
238
239     /* parse scalefactors */
240
241     for (sb = 0; sb < sblimit; sb++)
242     {
243         if (allocation[sb])
244         {
245             int index_0, index_1, index_2;
246
247             switch (scfsi[sb])
248             {
249                 case 0:
250                     NeedBits (&p_adec->bit_stream, 18);
251                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
252                     index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
253                     index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
254                     DumpBits (&p_adec->bit_stream, 18);
255
256                     if (allocation[sb] < 0)
257                     {
258                         slope[0][sb] = adec_scalefactor_table[index_0];
259                         slope[1][sb] = adec_scalefactor_table[index_1];
260                         slope[2][sb] = adec_scalefactor_table[index_2];
261                     }
262                     else
263                     {
264                         float r_scalefactor;
265                         float r_slope, r_offset;
266
267                         r_slope = adec_slope_table[allocation[sb]-2];
268                         r_offset = adec_offset_table[allocation[sb]-2];
269
270                         r_scalefactor = adec_scalefactor_table[index_0];
271                         slope[0][sb] = r_slope * r_scalefactor;
272                         offset[0][sb] = r_offset * r_scalefactor;
273
274                         r_scalefactor = adec_scalefactor_table[index_1];
275                         slope[1][sb] = r_slope * r_scalefactor;
276                         offset[1][sb] = r_offset * r_scalefactor;
277
278                         r_scalefactor = adec_scalefactor_table[index_2];
279                         slope[2][sb] = r_slope * r_scalefactor;
280                         offset[2][sb] = r_offset * r_scalefactor;
281                     }
282                 break;
283
284                 case 1:
285                     NeedBits (&p_adec->bit_stream, 12);
286                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
287                     index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
288                     DumpBits (&p_adec->bit_stream, 12);
289
290                     if (allocation[sb] < 0)
291                     {
292                         slope[0][sb] = slope[1][sb] =
293                             adec_scalefactor_table[index_0];
294                         slope[2][sb] = adec_scalefactor_table[index_1];
295                     }
296                     else
297                     {
298                         float r_scalefactor;
299                         float r_slope, r_offset;
300
301                         r_slope = adec_slope_table[allocation[sb]-2];
302                         r_offset = adec_offset_table[allocation[sb]-2];
303
304                         r_scalefactor = adec_scalefactor_table[index_0];
305                         slope[0][sb] = slope[1][sb] = r_slope * r_scalefactor;
306                         offset[0][sb] = offset[1][sb] =
307                             r_offset * r_scalefactor;
308
309                         r_scalefactor = adec_scalefactor_table[index_1];
310                         slope[2][sb] = r_slope * r_scalefactor;
311                         offset[2][sb] = r_offset * r_scalefactor;
312                     }
313                 break;
314
315                 case 2:
316                     NeedBits (&p_adec->bit_stream, 6);
317                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
318                     DumpBits (&p_adec->bit_stream, 6);
319
320                     if (allocation[sb] < 0)
321                     {
322                         slope[0][sb] = slope[1][sb] = slope[2][sb] =
323                             adec_scalefactor_table[index_0];
324                     }
325                     else
326                     {
327                         float r_scalefactor;
328                         float r_slope, r_offset;
329
330                         r_slope = adec_slope_table[allocation[sb]-2];
331                         r_offset = adec_offset_table[allocation[sb]-2];
332
333                         r_scalefactor = adec_scalefactor_table[index_0];
334                         slope[0][sb] = slope[1][sb] = slope[2][sb] =
335                             r_slope * r_scalefactor;
336                         offset[0][sb] = offset[1][sb] = offset[2][sb] =
337                             r_offset * r_scalefactor;
338                     }
339                 break;
340
341                 case 3:
342                     NeedBits (&p_adec->bit_stream, 12);
343                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
344                     index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
345                     DumpBits (&p_adec->bit_stream, 12);
346
347                     if (allocation[sb] < 0)
348                     {
349                         slope[0][sb] = adec_scalefactor_table[index_0];
350                         slope[1][sb] = slope[2][sb] =
351                             adec_scalefactor_table[index_1];
352                     }
353                     else
354                     {
355                         float r_scalefactor;
356                         float r_slope, r_offset;
357
358                         r_slope = adec_slope_table[allocation[sb]-2];
359                         r_offset = adec_offset_table[allocation[sb]-2];
360
361                         r_scalefactor = adec_scalefactor_table[index_0];
362                         slope[0][sb] = r_slope * r_scalefactor;
363                         offset[0][sb] = r_offset * r_scalefactor;
364
365                         r_scalefactor = adec_scalefactor_table[index_1];
366                         slope[1][sb] = slope[2][sb] = r_slope * r_scalefactor;
367                         offset[1][sb] = offset[2][sb] =
368                             r_offset * r_scalefactor;
369                     }
370                 break;
371             }
372         }
373     }
374
375     /* parse samples */
376
377     for (gr0 = 0; gr0 < 3; gr0++)
378     {
379         for (gr1 = 0; gr1 < 4; gr1++)
380         {
381             s16 * XXX_buf;
382
383             for (sb = 0; sb < sblimit; sb++)
384             {
385                 int code;
386
387                 switch (allocation[sb])
388                 {
389                     case 0:
390                         sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
391                         break;
392
393                     case L3:
394                         NeedBits (&p_adec->bit_stream, 5);
395                         code = p_adec->bit_stream.buffer >> (32 - 5);
396                         DumpBits (&p_adec->bit_stream, 5);
397
398                         sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
399                         code /= 3;
400                         sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
401                         code /= 3;
402                         sample[2][sb] = slope[gr0][sb] * L3_table[code];
403                     break;
404
405                     case L5:
406                         NeedBits (&p_adec->bit_stream, 7);
407                         code = p_adec->bit_stream.buffer >> (32 - 7);
408                         DumpBits (&p_adec->bit_stream, 7);
409
410                         sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
411                         code /= 5;
412                         sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
413                         code /= 5;
414                         sample[2][sb] = slope[gr0][sb] * L5_table[code];
415                     break;
416
417                     case L9:
418                         NeedBits (&p_adec->bit_stream, 10);
419                         code = p_adec->bit_stream.buffer >> (32 - 10);
420                         DumpBits (&p_adec->bit_stream, 10);
421
422                         sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
423                         code /= 9;
424                         sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
425                         code /= 9;
426                         sample[2][sb] = slope[gr0][sb] * L9_table[code];
427                     break;
428
429                     default:
430                         for (s = 0; s < 3; s++)
431                         {
432                             NeedBits (&p_adec->bit_stream, allocation[sb]);
433                             code = ( p_adec->bit_stream.buffer >>
434                                      (32 - allocation[sb]) );
435                             DumpBits (&p_adec->bit_stream, allocation[sb]);
436
437                             sample[s][sb] =
438                                 slope[gr0][sb] * code + offset[gr0][sb];
439                         }
440                 }
441             }
442
443             for (; sb < 32; sb++)
444             {
445                 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
446             }
447
448             for (s = 0; s < 3; s++)
449             {
450                 DCT32 (sample[s], &p_adec->bank_0);
451                 XXX_buf = buffer;
452                 PCM (&p_adec->bank_0, &XXX_buf, 2);
453
454                 /* FIXME: one shouldn't have to do it twice ! */
455                 DCT32 (sample[s], &p_adec->bank_1);
456                 XXX_buf = buffer+1;
457                 PCM (&p_adec->bank_1, &XXX_buf, 2);
458
459                 buffer += 64;
460             }
461         }
462     }
463
464     return 0;
465 }
466
467 int adec_layer2_stereo (audiodec_t * p_adec, s16 * buffer)
468 {
469     static u8 freq_table[15] = {3, 0, 0, 0, 1, 0, 1, 2, 2, 2, 3, 3, 3, 3, 3};
470     static float L3_table[3] = {-2/3.0, 0, 2/3.0};
471     static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
472     static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
473                                 2/9.0, 4/9.0, 6/9.0, 8/9.0};
474
475     s8 allocation_0[32], allocation_1[32];
476     u8 scfsi_0[32], scfsi_1[32];
477     float slope_0[3][32], slope_1[3][32];
478     float offset_0[3][32], offset_1[3][32];
479     float sample_0[3][32], sample_1[3][32];
480     alloc_table_t * alloc_table;
481
482     int sblimit;
483     int bound;
484     int sb;
485     int gr0, gr1;
486     int s;
487
488     /* get the right allocation table */
489     adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
490
491     /* calculate bound */
492     bound = sblimit;
493     if ((p_adec->header & 0xc0) == 0x40) { /* intensity stereo */
494         int index;
495         index = (p_adec->header >> 4) & 3;
496         if (adec_bound_table[index] < sblimit)
497         {
498             bound = adec_bound_table[index];
499         }
500     }
501
502     /* parse allocation */
503
504     for (sb = 0; sb < bound; sb++)
505     {
506         int index;
507
508         NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
509         index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
510         DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
511
512         allocation_0[sb] = alloc_table->alloc[sb][index];
513
514         NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
515         index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
516         DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
517
518         allocation_1[sb] = alloc_table->alloc[sb][index];
519     }
520
521     for (; sb < sblimit; sb++)
522     {
523         int index;
524
525         NeedBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
526         index = p_adec->bit_stream.buffer >> (32 - alloc_table->nbal[sb]);
527         DumpBits (&p_adec->bit_stream, alloc_table->nbal[sb]);
528
529         allocation_0[sb] = allocation_1[sb] = alloc_table->alloc[sb][index];
530     }
531
532     /* parse scfsi */
533
534     for (sb = 0; sb < sblimit; sb++)
535     {
536         if (allocation_0[sb])
537         {
538             NeedBits (&p_adec->bit_stream, 2);
539             scfsi_0[sb] = p_adec->bit_stream.buffer >> (32 - 2);
540             DumpBits (&p_adec->bit_stream, 2);
541         }
542
543         if (allocation_1[sb])
544         {
545             NeedBits (&p_adec->bit_stream, 2);
546             scfsi_1[sb] = p_adec->bit_stream.buffer >> (32 - 2);
547             DumpBits (&p_adec->bit_stream, 2);
548         }
549     }
550
551     /* parse scalefactors */
552
553     for (sb = 0; sb < sblimit; sb++)
554     {
555         if (allocation_0[sb])
556         {
557             int index_0, index_1, index_2;
558
559             switch (scfsi_0[sb])
560             {
561                 case 0:
562                     NeedBits (&p_adec->bit_stream, 18);
563                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
564                     index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
565                     index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
566                     DumpBits (&p_adec->bit_stream, 18);
567
568                     if (allocation_0[sb] < 0)
569                     {
570                         slope_0[0][sb] = adec_scalefactor_table[index_0];
571                         slope_0[1][sb] = adec_scalefactor_table[index_1];
572                         slope_0[2][sb] = adec_scalefactor_table[index_2];
573                     }
574                     else
575                     {
576                         float scalefactor;
577                         float slope, offset;
578
579                         slope = adec_slope_table[allocation_0[sb]-2];
580                         offset = adec_offset_table[allocation_0[sb]-2];
581
582                         scalefactor = adec_scalefactor_table[index_0];
583                         slope_0[0][sb] = slope * scalefactor;
584                         offset_0[0][sb] = offset * scalefactor;
585
586                         scalefactor = adec_scalefactor_table[index_1];
587                         slope_0[1][sb] = slope * scalefactor;
588                         offset_0[1][sb] = offset * scalefactor;
589
590                         scalefactor = adec_scalefactor_table[index_2];
591                         slope_0[2][sb] = slope * scalefactor;
592                         offset_0[2][sb] = offset * scalefactor;
593                     }
594                 break;
595
596                 case 1:
597                     NeedBits (&p_adec->bit_stream, 12);
598                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
599                     index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
600                     DumpBits (&p_adec->bit_stream, 12);
601
602                     if (allocation_0[sb] < 0)
603                     {
604                         slope_0[0][sb] = slope_0[1][sb] =
605                             adec_scalefactor_table[index_0];
606                         slope_0[2][sb] = adec_scalefactor_table[index_1];
607                     }
608                     else
609                     {
610                         float scalefactor;
611                         float slope, offset;
612
613                         slope = adec_slope_table[allocation_0[sb]-2];
614                         offset = adec_offset_table[allocation_0[sb]-2];
615
616                         scalefactor = adec_scalefactor_table[index_0];
617                         slope_0[0][sb] = slope_0[1][sb] = slope * scalefactor;
618                         offset_0[0][sb] = offset_0[1][sb] =
619                             offset * scalefactor;
620
621                         scalefactor = adec_scalefactor_table[index_1];
622                         slope_0[2][sb] = slope * scalefactor;
623                         offset_0[2][sb] = offset * scalefactor;
624                     }
625                 break;
626
627                 case 2:
628                     NeedBits (&p_adec->bit_stream, 6);
629                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
630                     DumpBits (&p_adec->bit_stream, 6);
631
632                     if (allocation_0[sb] < 0)
633                     {
634                         slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
635                             adec_scalefactor_table[index_0];
636                     }
637                     else
638                     {
639                         float scalefactor;
640                         float slope, offset;
641
642                         slope = adec_slope_table[allocation_0[sb]-2];
643                         offset = adec_offset_table[allocation_0[sb]-2];
644
645                         scalefactor = adec_scalefactor_table[index_0];
646                         slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
647                             slope * scalefactor;
648                         offset_0[0][sb] = offset_0[1][sb] = offset_0[2][sb] =
649                             offset * scalefactor;
650                     }
651                 break;
652
653                 case 3:
654                     NeedBits (&p_adec->bit_stream, 12);
655                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
656                     index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
657                     DumpBits (&p_adec->bit_stream, 12);
658
659                     if (allocation_0[sb] < 0)
660                     {
661                         slope_0[0][sb] = adec_scalefactor_table[index_0];
662                         slope_0[1][sb] = slope_0[2][sb] =
663                             adec_scalefactor_table[index_1];
664                     }
665                     else
666                     {
667                         float scalefactor;
668                         float slope, offset;
669
670                         slope = adec_slope_table[allocation_0[sb]-2];
671                         offset = adec_offset_table[allocation_0[sb]-2];
672
673                         scalefactor = adec_scalefactor_table[index_0];
674                         slope_0[0][sb] = slope * scalefactor;
675                         offset_0[0][sb] = offset * scalefactor;
676
677                         scalefactor = adec_scalefactor_table[index_1];
678                         slope_0[1][sb] = slope_0[2][sb] = slope * scalefactor;
679                         offset_0[1][sb] = offset_0[2][sb] =
680                             offset * scalefactor;
681                     }
682                 break;
683             }
684         }
685
686         if (allocation_1[sb])
687         {
688             int index_0, index_1, index_2;
689
690             switch (scfsi_1[sb])
691             {
692                 case 0:
693                     NeedBits (&p_adec->bit_stream, 18);
694                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
695                     index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
696                     index_2 = (p_adec->bit_stream.buffer >> (32 - 18)) & 63;
697                     DumpBits (&p_adec->bit_stream, 18);
698
699                     if (allocation_1[sb] < 0)
700                     {
701                         slope_1[0][sb] = adec_scalefactor_table[index_0];
702                         slope_1[1][sb] = adec_scalefactor_table[index_1];
703                         slope_1[2][sb] = adec_scalefactor_table[index_2];
704                     }
705                     else
706                     {
707                         float scalefactor;
708                         float slope, offset;
709
710                         slope = adec_slope_table[allocation_1[sb]-2];
711                         offset = adec_offset_table[allocation_1[sb]-2];
712
713                         scalefactor = adec_scalefactor_table[index_0];
714                         slope_1[0][sb] = slope * scalefactor;
715                         offset_1[0][sb] = offset * scalefactor;
716
717                         scalefactor = adec_scalefactor_table[index_1];
718                         slope_1[1][sb] = slope * scalefactor;
719                         offset_1[1][sb] = offset * scalefactor;
720
721                         scalefactor = adec_scalefactor_table[index_2];
722                         slope_1[2][sb] = slope * scalefactor;
723                         offset_1[2][sb] = offset * scalefactor;
724                     }
725                 break;
726
727                 case 1:
728                     NeedBits (&p_adec->bit_stream, 12);
729                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
730                     index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
731                     DumpBits (&p_adec->bit_stream, 12);
732
733                     if (allocation_1[sb] < 0)
734                     {
735                         slope_1[0][sb] = slope_1[1][sb] =
736                             adec_scalefactor_table[index_0];
737                         slope_1[2][sb] = adec_scalefactor_table[index_1];
738                     }
739                     else
740                     {
741                         float scalefactor;
742                         float slope, offset;
743
744                         slope = adec_slope_table[allocation_1[sb]-2];
745                         offset = adec_offset_table[allocation_1[sb]-2];
746
747                         scalefactor = adec_scalefactor_table[index_0];
748                         slope_1[0][sb] = slope_1[1][sb] = slope * scalefactor;
749                         offset_1[0][sb] = offset_1[1][sb] =
750                             offset * scalefactor;
751
752                         scalefactor = adec_scalefactor_table[index_1];
753                         slope_1[2][sb] = slope * scalefactor;
754                         offset_1[2][sb] = offset * scalefactor;
755                     }
756                 break;
757
758                 case 2:
759                     NeedBits (&p_adec->bit_stream, 6);
760                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
761                     DumpBits (&p_adec->bit_stream, 6);
762
763                     if (allocation_1[sb] < 0)
764                     {
765                         slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
766                             adec_scalefactor_table[index_0];
767                     }
768                     else
769                     {
770                         float scalefactor;
771                         float slope, offset;
772
773                         slope = adec_slope_table[allocation_1[sb]-2];
774                         offset = adec_offset_table[allocation_1[sb]-2];
775
776                         scalefactor = adec_scalefactor_table[index_0];
777                         slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
778                             slope * scalefactor;
779                         offset_1[0][sb] = offset_1[1][sb] = offset_1[2][sb] =
780                             offset * scalefactor;
781                     }
782                 break;
783
784                 case 3:
785                     NeedBits (&p_adec->bit_stream, 12);
786                     index_0 = p_adec->bit_stream.buffer >> (32 - 6);
787                     index_1 = (p_adec->bit_stream.buffer >> (32 - 12)) & 63;
788                     DumpBits (&p_adec->bit_stream, 12);
789
790                     if (allocation_1[sb] < 0)
791                     {
792                         slope_1[0][sb] = adec_scalefactor_table[index_0];
793                         slope_1[1][sb] = slope_1[2][sb] =
794                             adec_scalefactor_table[index_1];
795                     }
796                     else
797                     {
798                         float scalefactor;
799                         float slope, offset;
800
801                         slope = adec_slope_table[allocation_1[sb]-2];
802                         offset = adec_offset_table[allocation_1[sb]-2];
803
804                         scalefactor = adec_scalefactor_table[index_0];
805                         slope_1[0][sb] = slope * scalefactor;
806                         offset_1[0][sb] = offset * scalefactor;
807
808                         scalefactor = adec_scalefactor_table[index_1];
809                         slope_1[1][sb] = slope_1[2][sb] = slope * scalefactor;
810                         offset_1[1][sb] = offset_1[2][sb] =
811                             offset * scalefactor;
812                     }
813                 break;
814             }
815         }
816     }
817
818     /* parse samples */
819
820     for (gr0 = 0; gr0 < 3; gr0++)
821     {
822         for (gr1 = 0; gr1 < 4; gr1++)
823         {
824             s16 * XXX_buf;
825
826             for (sb = 0; sb < bound; sb++)
827             {
828                 int code;
829
830                 switch (allocation_0[sb])
831                 {
832                     case 0:
833                         sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
834                         break;
835
836                     case L3:
837                         NeedBits (&p_adec->bit_stream, 5);
838                         code = p_adec->bit_stream.buffer >> (32 - 5);
839                         DumpBits (&p_adec->bit_stream, 5);
840
841                         sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
842                         code /= 3;
843                         sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
844                         code /= 3;
845                         sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
846                     break;
847
848                     case L5:
849                         NeedBits (&p_adec->bit_stream, 7);
850                         code = p_adec->bit_stream.buffer >> (32 - 7);
851                         DumpBits (&p_adec->bit_stream, 7);
852
853                         sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
854                         code /= 5;
855                         sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
856                         code /= 5;
857                         sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
858                     break;
859
860                     case L9:
861                         NeedBits (&p_adec->bit_stream, 10);
862                         code = p_adec->bit_stream.buffer >> (32 - 10);
863                         DumpBits (&p_adec->bit_stream, 10);
864
865                         sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
866                         code /= 9;
867                         sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
868                         code /= 9;
869                         sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
870                     break;
871
872                     default:
873                         for (s = 0; s < 3; s++)
874                         {
875                             NeedBits (&p_adec->bit_stream, allocation_0[sb]);
876                             code = ( p_adec->bit_stream.buffer >>
877                                      (32 - allocation_0[sb]) );
878                             DumpBits (&p_adec->bit_stream, allocation_0[sb]);
879
880                             sample_0[s][sb] =
881                                 slope_0[gr0][sb] * code + offset_0[gr0][sb];
882                         }
883                 }
884
885                 switch (allocation_1[sb])
886                 {
887                     case 0:
888                         sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
889                     break;
890
891                     case L3:
892                         NeedBits (&p_adec->bit_stream, 5);
893                         code = p_adec->bit_stream.buffer >> (32 - 5);
894                         DumpBits (&p_adec->bit_stream, 5);
895
896                         sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
897                         code /= 3;
898                         sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
899                         code /= 3;
900                         sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
901                     break;
902
903                     case L5:
904                         NeedBits (&p_adec->bit_stream, 7);
905                         code = p_adec->bit_stream.buffer >> (32 - 7);
906                         DumpBits (&p_adec->bit_stream, 7);
907
908                         sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
909                         code /= 5;
910                         sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
911                         code /= 5;
912                         sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
913                     break;
914
915                     case L9:
916                         NeedBits (&p_adec->bit_stream, 10);
917                         code = p_adec->bit_stream.buffer >> (32 - 10);
918                         DumpBits (&p_adec->bit_stream, 10);
919
920                         sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
921                         code /= 9;
922                         sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
923                         code /= 9;
924                         sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
925                     break;
926
927                     default:
928                         for (s = 0; s < 3; s++)
929                         {
930                             NeedBits (&p_adec->bit_stream, allocation_1[sb]);
931                             code = ( p_adec->bit_stream.buffer >>
932                                      (32 - allocation_1[sb]) );
933                             DumpBits (&p_adec->bit_stream, allocation_1[sb]);
934
935                             sample_1[s][sb] =
936                                 slope_1[gr0][sb] * code + offset_1[gr0][sb];
937                         }
938                 }
939             }
940
941             for (; sb < sblimit; sb++)
942             {
943                 int code;
944
945                 switch (allocation_0[sb])
946                 {
947                     case 0:
948                         sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
949                         sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
950                     break;
951
952                     case L3:
953                         NeedBits (&p_adec->bit_stream, 5);
954                         code = p_adec->bit_stream.buffer >> (32 - 5);
955                         DumpBits (&p_adec->bit_stream, 5);
956
957                         sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
958                         sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
959                         code /= 3;
960                         sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
961                         sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
962                         code /= 3;
963                         sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
964                         sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
965                     break;
966
967                     case L5:
968                         NeedBits (&p_adec->bit_stream, 7);
969                         code = p_adec->bit_stream.buffer >> (32 - 7);
970                         DumpBits (&p_adec->bit_stream, 7);
971
972                         sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
973                         sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
974                         code /= 5;
975                         sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
976                         sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
977                         code /= 5;
978                         sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
979                         sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
980                     break;
981
982                     case L9:
983                         NeedBits (&p_adec->bit_stream, 10);
984                         code = p_adec->bit_stream.buffer >> (32 - 10);
985                         DumpBits (&p_adec->bit_stream, 10);
986
987                         sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
988                         sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
989                         code /= 9;
990                         sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
991                         sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
992                         code /= 9;
993                         sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
994                         sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
995                     break;
996
997                     default:
998                         for (s = 0; s < 3; s++)
999                         {
1000                             NeedBits (&p_adec->bit_stream, allocation_0[sb]);
1001                             code = ( p_adec->bit_stream.buffer >>
1002                                      (32 - allocation_0[sb]) );
1003                             DumpBits (&p_adec->bit_stream, allocation_0[sb]);
1004
1005                             sample_0[s][sb] =
1006                                 slope_0[gr0][sb] * code + offset_0[gr0][sb];
1007                             sample_1[s][sb] =
1008                                 slope_1[gr0][sb] * code + offset_1[gr0][sb];
1009                         }
1010                 }
1011             }
1012
1013             for (; sb < 32; sb++)
1014             {
1015                 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
1016                 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
1017             }
1018
1019             for (s = 0; s < 3; s++)
1020             {
1021                 DCT32 (sample_0[s], &p_adec->bank_0);
1022                 XXX_buf = buffer;
1023                 PCM (&p_adec->bank_0, &XXX_buf, 2);
1024
1025                 DCT32 (sample_1[s], &p_adec->bank_1);
1026                 XXX_buf = buffer+1;
1027                 PCM (&p_adec->bank_1, &XXX_buf, 2);
1028
1029                 buffer += 64;
1030             }
1031         }
1032     }
1033
1034     return 0;
1035 }
1036