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