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