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