]> git.sesse.net Git - vlc/blob - plugins/mpeg_adec/adec_layer2.c
Some heavy changes today:
[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.5 2001/12/30 07:09:55 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             s16 * XXX_buf;
376
377             for (sb = 0; sb < sblimit; sb++)
378             {
379                 int code;
380
381                 switch (allocation[sb])
382                 {
383                     case 0:
384                         sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
385                         break;
386
387                     case L3:
388                         code = GetBits( &p_adec->bit_stream, 5 );
389                         i_read_bits += 5;
390
391                         sample[0][sb] = slope[gr0][sb] * L3_table[code % 3];
392                         code /= 3;
393                         sample[1][sb] = slope[gr0][sb] * L3_table[code % 3];
394                         code /= 3;
395                         sample[2][sb] = slope[gr0][sb] * L3_table[code];
396                     break;
397
398                     case L5:
399                         code = GetBits( &p_adec->bit_stream, 7 );
400                         i_read_bits += 7;
401
402                         sample[0][sb] = slope[gr0][sb] * L5_table[code % 5];
403                         code /= 5;
404                         sample[1][sb] = slope[gr0][sb] * L5_table[code % 5];
405                         code /= 5;
406                         sample[2][sb] = slope[gr0][sb] * L5_table[code];
407                     break;
408
409                     case L9:
410                         code = GetBits( &p_adec->bit_stream, 10 );
411                         i_read_bits += 10;
412
413                         sample[0][sb] = slope[gr0][sb] * L9_table[code % 9];
414                         code /= 9;
415                         sample[1][sb] = slope[gr0][sb] * L9_table[code % 9];
416                         code /= 9;
417                         sample[2][sb] = slope[gr0][sb] * L9_table[code];
418                     break;
419
420                     default:
421                         for (s = 0; s < 3; s++)
422                         {
423                             code = GetBits( &p_adec->bit_stream,
424                                             allocation[sb] );
425                             i_read_bits += allocation[sb];
426
427                             sample[s][sb] =
428                                 slope[gr0][sb] * code + offset[gr0][sb];
429                         }
430                 }
431             }
432
433             for (; sb < 32; sb++)
434             {
435                 sample[0][sb] = sample[1][sb] = sample[2][sb] = 0;
436             }
437
438             for (s = 0; s < 3; s++)
439             {
440                 DCT32 (sample[s], &p_adec->bank_0);
441                 XXX_buf = buffer;
442                 PCM (&p_adec->bank_0, &XXX_buf, 2);
443
444                 /* FIXME: one shouldn't have to do it twice ! */
445                 DCT32 (sample[s], &p_adec->bank_1);
446                 XXX_buf = buffer+1;
447                 PCM (&p_adec->bank_1, &XXX_buf, 2);
448
449                 buffer += 64;
450             }
451         }
452     }
453
454     p_adec->i_read_bits += i_read_bits;
455
456     return 0;
457 }
458
459 int adec_layer2_stereo( adec_thread_t * p_adec, s16 * buffer )
460 {
461     static u8 freq_table[15] = {3, 0, 0, 0, 1, 0, 1, 2, 2, 2, 3, 3, 3, 3, 3};
462     static float L3_table[3] = {-2/3.0, 0, 2/3.0};
463     static float L5_table[5] = {-4/5.0, -2/5.0, 0, 2/5.0, 4/5.0};
464     static float L9_table[9] = {-8/9.0, -6/9.0, -4/9.0, -2/9.0, 0,
465                                 2/9.0, 4/9.0, 6/9.0, 8/9.0};
466
467     s8 allocation_0[32], allocation_1[32];
468     u8 scfsi_0[32], scfsi_1[32];
469     float slope_0[3][32], slope_1[3][32];
470     float offset_0[3][32], offset_1[3][32];
471     float sample_0[3][32], sample_1[3][32];
472     alloc_table_t * alloc_table;
473
474     int sblimit;
475     int bound;
476     int sb;
477     int gr0, gr1;
478     int s;
479     int i_read_bits = 0;
480
481     /* get the right allocation table */
482     adec_layer2_get_table (p_adec->header, freq_table, &alloc_table, &sblimit);
483
484     /* calculate bound */
485     bound = sblimit;
486     if ((p_adec->header & 0xc0) == 0x40) { /* intensity stereo */
487         int index;
488         index = (p_adec->header >> 4) & 3;
489         if (adec_bound_table[index] < sblimit)
490         {
491             bound = adec_bound_table[index];
492         }
493     }
494
495     /* parse allocation */
496
497     for (sb = 0; sb < bound; sb++)
498     {
499         int index;
500
501         index = GetBits( &p_adec->bit_stream, alloc_table->nbal[sb] );
502         allocation_0[sb] = alloc_table->alloc[sb][index];
503
504         index = GetBits( &p_adec->bit_stream, alloc_table->nbal[sb] );
505         allocation_1[sb] = alloc_table->alloc[sb][index];
506
507         i_read_bits += alloc_table->nbal[sb] * 2;
508     }
509
510     for (; sb < sblimit; sb++)
511     {
512         int index;
513
514         index = GetBits( &p_adec->bit_stream, alloc_table->nbal[sb] );
515         allocation_0[sb] = allocation_1[sb] = alloc_table->alloc[sb][index];
516         i_read_bits += alloc_table->nbal[sb];
517     }
518
519     /* parse scfsi */
520
521     for (sb = 0; sb < sblimit; sb++)
522     {
523         if (allocation_0[sb])
524         {
525             scfsi_0[sb] = GetBits (&p_adec->bit_stream, 2);
526             i_read_bits += 2;
527         }
528
529         if (allocation_1[sb])
530         {
531             scfsi_1[sb] = GetBits (&p_adec->bit_stream, 2);
532             i_read_bits += 2;
533         }
534     }
535
536     /* parse scalefactors */
537
538     for (sb = 0; sb < sblimit; sb++)
539     {
540         if (allocation_0[sb])
541         {
542             int index_0, index_1, index_2;
543
544             switch (scfsi_0[sb])
545             {
546             case 0:
547                 index_0 = GetBits(&p_adec->bit_stream,6);
548                 index_1 = GetBits(&p_adec->bit_stream,6);
549                 index_2 = GetBits(&p_adec->bit_stream,6);
550                 i_read_bits += 18;
551
552                 if (allocation_0[sb] < 0)
553                 {
554                     slope_0[0][sb] = adec_scalefactor_table[index_0];
555                     slope_0[1][sb] = adec_scalefactor_table[index_1];
556                     slope_0[2][sb] = adec_scalefactor_table[index_2];
557                 }
558                 else
559                 {
560                     float scalefactor;
561                     float slope, offset;
562
563                     slope = adec_slope_table[allocation_0[sb]-2];
564                     offset = adec_offset_table[allocation_0[sb]-2];
565
566                     scalefactor = adec_scalefactor_table[index_0];
567                     slope_0[0][sb] = slope * scalefactor;
568                     offset_0[0][sb] = offset * scalefactor;
569
570                     scalefactor = adec_scalefactor_table[index_1];
571                     slope_0[1][sb] = slope * scalefactor;
572                     offset_0[1][sb] = offset * scalefactor;
573
574                     scalefactor = adec_scalefactor_table[index_2];
575                     slope_0[2][sb] = slope * scalefactor;
576                     offset_0[2][sb] = offset * scalefactor;
577                 }
578                 break;
579
580             case 1:
581                 index_0 = GetBits(&p_adec->bit_stream,6);
582                 index_1 = GetBits(&p_adec->bit_stream,6);
583                 i_read_bits += 12;
584
585                 if (allocation_0[sb] < 0)
586                 {
587                     slope_0[0][sb] = slope_0[1][sb] =
588                         adec_scalefactor_table[index_0];
589                     slope_0[2][sb] = adec_scalefactor_table[index_1];
590                 }
591                 else
592                 {
593                     float scalefactor;
594                     float slope, offset;
595
596                     slope = adec_slope_table[allocation_0[sb]-2];
597                     offset = adec_offset_table[allocation_0[sb]-2];
598
599                     scalefactor = adec_scalefactor_table[index_0];
600                     slope_0[0][sb] = slope_0[1][sb] = slope * scalefactor;
601                     offset_0[0][sb] = offset_0[1][sb] =
602                             offset * scalefactor;
603
604                     scalefactor = adec_scalefactor_table[index_1];
605                     slope_0[2][sb] = slope * scalefactor;
606                     offset_0[2][sb] = offset * scalefactor;
607                 }
608                 break;
609
610             case 2:
611                 index_0 = GetBits( &p_adec->bit_stream, 6 );
612                 i_read_bits += 6;
613
614                 if (allocation_0[sb] < 0)
615                 {
616                     slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
617                         adec_scalefactor_table[index_0];
618                 }
619                 else
620                 {
621                     float scalefactor;
622                     float slope, offset;
623
624                     slope = adec_slope_table[allocation_0[sb]-2];
625                     offset = adec_offset_table[allocation_0[sb]-2];
626
627                     scalefactor = adec_scalefactor_table[index_0];
628                     slope_0[0][sb] = slope_0[1][sb] = slope_0[2][sb] =
629                         slope * scalefactor;
630                     offset_0[0][sb] = offset_0[1][sb] = offset_0[2][sb] =
631                         offset * scalefactor;
632                 }
633                 break;
634
635             case 3:
636                 index_0 = GetBits(&p_adec->bit_stream,6);
637                 index_1 = GetBits(&p_adec->bit_stream,6);
638                 i_read_bits += 12;
639
640                 if (allocation_0[sb] < 0)
641                 {
642                     slope_0[0][sb] = adec_scalefactor_table[index_0];
643                     slope_0[1][sb] = slope_0[2][sb] =
644                         adec_scalefactor_table[index_1];
645                 }
646                 else
647                 {
648                     float scalefactor;
649                     float slope, offset;
650
651                     slope = adec_slope_table[allocation_0[sb]-2];
652                     offset = adec_offset_table[allocation_0[sb]-2];
653
654                     scalefactor = adec_scalefactor_table[index_0];
655                     slope_0[0][sb] = slope * scalefactor;
656                     offset_0[0][sb] = offset * scalefactor;
657
658                     scalefactor = adec_scalefactor_table[index_1];
659                     slope_0[1][sb] = slope_0[2][sb] = slope * scalefactor;
660                     offset_0[1][sb] = offset_0[2][sb] =
661                         offset * scalefactor;
662                 }
663                 break;
664             }
665         }
666
667         if (allocation_1[sb])
668         {
669             int index_0, index_1, index_2;
670
671             switch (scfsi_1[sb])
672             {
673             case 0:
674                 index_0 = GetBits(&p_adec->bit_stream,6);
675                 index_1 = GetBits(&p_adec->bit_stream,6);
676                 index_2 = GetBits(&p_adec->bit_stream,6);
677                 i_read_bits += 18;
678
679                 if (allocation_1[sb] < 0)
680                 {
681                     slope_1[0][sb] = adec_scalefactor_table[index_0];
682                     slope_1[1][sb] = adec_scalefactor_table[index_1];
683                     slope_1[2][sb] = adec_scalefactor_table[index_2];
684                 }
685                 else
686                 {
687                     float scalefactor;
688                     float slope, offset;
689
690                     slope = adec_slope_table[allocation_1[sb]-2];
691                     offset = adec_offset_table[allocation_1[sb]-2];
692
693                     scalefactor = adec_scalefactor_table[index_0];
694                     slope_1[0][sb] = slope * scalefactor;
695                     offset_1[0][sb] = offset * scalefactor;
696
697                     scalefactor = adec_scalefactor_table[index_1];
698                     slope_1[1][sb] = slope * scalefactor;
699                     offset_1[1][sb] = offset * scalefactor;
700
701                     scalefactor = adec_scalefactor_table[index_2];
702                     slope_1[2][sb] = slope * scalefactor;
703                     offset_1[2][sb] = offset * scalefactor;
704                 }
705                 break;
706
707             case 1:
708                 index_0 = GetBits(&p_adec->bit_stream,6);
709                 index_1 = GetBits(&p_adec->bit_stream,6);
710                 i_read_bits += 12;
711
712                 if (allocation_1[sb] < 0)
713                 {
714                     slope_1[0][sb] = slope_1[1][sb] =
715                         adec_scalefactor_table[index_0];
716                     slope_1[2][sb] = adec_scalefactor_table[index_1];
717                 }
718                 else
719                 {
720                     float scalefactor;
721                     float slope, offset;
722
723                     slope = adec_slope_table[allocation_1[sb]-2];
724                     offset = adec_offset_table[allocation_1[sb]-2];
725
726                     scalefactor = adec_scalefactor_table[index_0];
727                     slope_1[0][sb] = slope_1[1][sb] = slope * scalefactor;
728                     offset_1[0][sb] = offset_1[1][sb] =
729                         offset * scalefactor;
730
731                     scalefactor = adec_scalefactor_table[index_1];
732                     slope_1[2][sb] = slope * scalefactor;
733                     offset_1[2][sb] = offset * scalefactor;
734                 }
735                 break;
736
737             case 2:
738                 index_0 = GetBits( &p_adec->bit_stream, 6 );
739                 i_read_bits += 6;
740
741                 if (allocation_1[sb] < 0)
742                 {
743                     slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
744                         adec_scalefactor_table[index_0];
745                 }
746                 else
747                 {
748                     float scalefactor;
749                     float slope, offset;
750
751                     slope = adec_slope_table[allocation_1[sb]-2];
752                     offset = adec_offset_table[allocation_1[sb]-2];
753
754                     scalefactor = adec_scalefactor_table[index_0];
755                     slope_1[0][sb] = slope_1[1][sb] = slope_1[2][sb] =
756                         slope * scalefactor;
757                     offset_1[0][sb] = offset_1[1][sb] = offset_1[2][sb] =
758                         offset * scalefactor;
759                 }
760                 break;
761
762             case 3:
763                 index_0 = GetBits(&p_adec->bit_stream,6);
764                 index_1 = GetBits(&p_adec->bit_stream,6);
765                 i_read_bits += 12;
766
767                 if (allocation_1[sb] < 0)
768                 {
769                     slope_1[0][sb] = adec_scalefactor_table[index_0];
770                     slope_1[1][sb] = slope_1[2][sb] =
771                         adec_scalefactor_table[index_1];
772                 }
773                 else
774                 {
775                     float scalefactor;
776                     float slope, offset;
777
778                     slope = adec_slope_table[allocation_1[sb]-2];
779                     offset = adec_offset_table[allocation_1[sb]-2];
780
781                     scalefactor = adec_scalefactor_table[index_0];
782                     slope_1[0][sb] = slope * scalefactor;
783                     offset_1[0][sb] = offset * scalefactor;
784
785                     scalefactor = adec_scalefactor_table[index_1];
786                     slope_1[1][sb] = slope_1[2][sb] = slope * scalefactor;
787                     offset_1[1][sb] = offset_1[2][sb] =
788                         offset * scalefactor;
789                 }
790                 break;
791             }
792         }
793     }
794
795     /* parse samples */
796
797     for (gr0 = 0; gr0 < 3; gr0++)
798     {
799         for (gr1 = 0; gr1 < 4; gr1++)
800         {
801             s16 * XXX_buf;
802
803             for (sb = 0; sb < bound; sb++)
804             {
805                 int code;
806
807                 switch (allocation_0[sb])
808                 {
809                     case 0:
810                         sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
811                         break;
812
813                     case L3:
814                         code = GetBits( &p_adec->bit_stream, 5 );
815                         i_read_bits += 5;
816
817                         sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
818                         code /= 3;
819                         sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
820                         code /= 3;
821                         sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
822                     break;
823
824                     case L5:
825                         code = GetBits( &p_adec->bit_stream, 7 );
826                         i_read_bits += 7;
827
828                         sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
829                         code /= 5;
830                         sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
831                         code /= 5;
832                         sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
833                     break;
834
835                     case L9:
836                         code = GetBits( &p_adec->bit_stream, 10 );
837                         i_read_bits += 10;
838
839                         sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
840                         code /= 9;
841                         sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
842                         code /= 9;
843                         sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
844                     break;
845
846                     default:
847                         for (s = 0; s < 3; s++)
848                         {
849                             code = GetBits( &p_adec->bit_stream,
850                                             allocation_0[sb] );
851                             i_read_bits += allocation_0[sb];
852
853                             sample_0[s][sb] =
854                                 slope_0[gr0][sb] * code + offset_0[gr0][sb];
855                         }
856                 }
857
858                 switch (allocation_1[sb])
859                 {
860                     case 0:
861                         sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
862                     break;
863
864                     case L3:
865                         code = GetBits( &p_adec->bit_stream, 5 );
866                         i_read_bits += 5;
867
868                         sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
869                         code /= 3;
870                         sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
871                         code /= 3;
872                         sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
873                     break;
874
875                     case L5:
876                         code = GetBits( &p_adec->bit_stream, 7 );
877                         i_read_bits += 7;
878
879                         sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
880                         code /= 5;
881                         sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
882                         code /= 5;
883                         sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
884                     break;
885
886                     case L9:
887                         code = GetBits( &p_adec->bit_stream, 10 );
888                         i_read_bits += 10;
889
890                         sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
891                         code /= 9;
892                         sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
893                         code /= 9;
894                         sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
895                     break;
896
897                     default:
898                         for (s = 0; s < 3; s++)
899                         {
900                             code = GetBits( &p_adec->bit_stream,
901                                             allocation_1[sb] );
902                             i_read_bits += allocation_1[sb];
903
904                             sample_1[s][sb] =
905                                 slope_1[gr0][sb] * code + offset_1[gr0][sb];
906                         }
907                 }
908             }
909
910             for (; sb < sblimit; sb++)
911             {
912                 int code;
913
914                 switch (allocation_0[sb])
915                 {
916                     case 0:
917                         sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
918                         sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
919                     break;
920
921                     case L3:
922                         code = GetBits( &p_adec->bit_stream, 5 );
923                         i_read_bits += 5;
924
925                         sample_0[0][sb] = slope_0[gr0][sb] * L3_table[code % 3];
926                         sample_1[0][sb] = slope_1[gr0][sb] * L3_table[code % 3];
927                         code /= 3;
928                         sample_0[1][sb] = slope_0[gr0][sb] * L3_table[code % 3];
929                         sample_1[1][sb] = slope_1[gr0][sb] * L3_table[code % 3];
930                         code /= 3;
931                         sample_0[2][sb] = slope_0[gr0][sb] * L3_table[code];
932                         sample_1[2][sb] = slope_1[gr0][sb] * L3_table[code];
933                     break;
934
935                     case L5:
936                         code = GetBits( &p_adec->bit_stream, 7 );
937                         i_read_bits += 7;
938
939                         sample_0[0][sb] = slope_0[gr0][sb] * L5_table[code % 5];
940                         sample_1[0][sb] = slope_1[gr0][sb] * L5_table[code % 5];
941                         code /= 5;
942                         sample_0[1][sb] = slope_0[gr0][sb] * L5_table[code % 5];
943                         sample_1[1][sb] = slope_1[gr0][sb] * L5_table[code % 5];
944                         code /= 5;
945                         sample_0[2][sb] = slope_0[gr0][sb] * L5_table[code];
946                         sample_1[2][sb] = slope_1[gr0][sb] * L5_table[code];
947                     break;
948
949                     case L9:
950                         code = GetBits( &p_adec->bit_stream, 10 );
951                         i_read_bits += 10;
952
953                         sample_0[0][sb] = slope_0[gr0][sb] * L9_table[code % 9];
954                         sample_1[0][sb] = slope_1[gr0][sb] * L9_table[code % 9];
955                         code /= 9;
956                         sample_0[1][sb] = slope_0[gr0][sb] * L9_table[code % 9];
957                         sample_1[1][sb] = slope_1[gr0][sb] * L9_table[code % 9];
958                         code /= 9;
959                         sample_0[2][sb] = slope_0[gr0][sb] * L9_table[code];
960                         sample_1[2][sb] = slope_1[gr0][sb] * L9_table[code];
961                     break;
962
963                     default:
964                         for (s = 0; s < 3; s++)
965                         {
966                             code = GetBits( &p_adec->bit_stream,
967                                             allocation_0[sb] );
968                             i_read_bits += allocation_0[sb];
969
970                             sample_0[s][sb] =
971                                 slope_0[gr0][sb] * code + offset_0[gr0][sb];
972                             sample_1[s][sb] =
973                                 slope_1[gr0][sb] * code + offset_1[gr0][sb];
974                         }
975                 }
976             }
977
978             for (; sb < 32; sb++)
979             {
980                 sample_0[0][sb] = sample_0[1][sb] = sample_0[2][sb] = 0;
981                 sample_1[0][sb] = sample_1[1][sb] = sample_1[2][sb] = 0;
982             }
983
984             for (s = 0; s < 3; s++)
985             {
986                 DCT32 (sample_0[s], &p_adec->bank_0);
987                 XXX_buf = buffer;
988                 PCM (&p_adec->bank_0, &XXX_buf, 2);
989
990                 DCT32 (sample_1[s], &p_adec->bank_1);
991                 XXX_buf = buffer+1;
992                 PCM (&p_adec->bank_1, &XXX_buf, 2);
993
994                 buffer += 64;
995             }
996         }
997     }
998
999     p_adec->i_read_bits += i_read_bits;
1000
1001     return 0;
1002 }
1003