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