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