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