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