]> git.sesse.net Git - vlc/blob - src/audio_decoder/audio_decoder.c
Proprification du decodeur mpeg audio, comme j'avais fait pour l'ac3 :
[vlc] / src / audio_decoder / audio_decoder.c
1 /*****************************************************************************
2  * audio_decoder.c: MPEG1 Layer I-II audio decoder
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 /*
25  * TODO :
26  *
27  * - optimiser les NeedBits() et les GetBits() du code là où c'est possible ;
28  *
29  */
30
31 #include "int_types.h"
32 #include "audio_constants.h"
33 #include "audio_decoder.h"
34 #include "audio_math.h"                                    /* DCT32(), PCM() */
35 #include "audio_bit_stream.h"
36
37 #define NULL ((void *)0)
38
39 /*****************************************************************************
40  * adec_Layer`L'_`M': decodes an mpeg 1, layer `L', mode `M', audio frame
41  *****************************************************************************
42  * These functions decode the audio frame which has already its header loaded
43  * in the i_header member of the audio decoder thread structure and its first
44  * byte of data described by the bit stream structure of the audio decoder
45  * thread (there is no bit available in the bit buffer yet)
46  *****************************************************************************/
47
48 /*****************************************************************************
49  * adec_Layer1_Mono
50  *****************************************************************************/
51 static __inline__ int adec_Layer1_Mono (audiodec_t * p_adec)
52 {
53     p_adec->bit_stream.buffer = 0;
54     p_adec->bit_stream.i_available = 0;
55     return (0);
56 }
57
58 /*****************************************************************************
59  * adec_Layer1_Stereo
60  *****************************************************************************/
61 static __inline__ int adec_Layer1_Stereo (audiodec_t * p_adec)
62 {
63     p_adec->bit_stream.buffer = 0;
64     p_adec->bit_stream.i_available = 0;
65     return (0);
66 }
67
68 /*****************************************************************************
69  * adec_Layer2_Mono
70  *****************************************************************************/
71 static __inline__ int adec_Layer2_Mono (audiodec_t * p_adec)
72 {
73     p_adec->bit_stream.buffer = 0;
74     p_adec->bit_stream.i_available = 0;
75     return (0);
76 }
77
78 /*****************************************************************************
79  * adec_Layer2_Stereo
80  *****************************************************************************/
81 static __inline__ int adec_Layer2_Stereo (audiodec_t * p_adec, s16 * buffer)
82 {
83     typedef struct requantization_s
84     {
85         u8                              i_bits_per_codeword;
86         const float *                   pf_ungroup;
87         float                           f_slope;
88         float                           f_offset;
89     } requantization_t;
90
91     static const float                  pf_scalefactor[64] = ADEC_SCALE_FACTOR;
92
93     static u32                          i_header;
94     static int                          i_sampling_frequency, i_mode, i_bound;
95     static int                          pi_allocation_0[32], pi_allocation_1[32]; /* see ISO/IEC 11172-3 2.4.1.6 */
96     int                                 i_sb, i_nbal;
97     float                               f_scalefactor_0, f_scalefactor_1;
98
99     static const u8                     ppi_bitrate_per_channel_index[4][15] = ADEC_LAYER2_BITRATE_PER_CHANNEL_INDEX;
100     static const u8                     ppi_sblimit[3][11] = ADEC_LAYER2_SBLIMIT;
101     static const u8                     ppi_nbal[2][32] = ADEC_LAYER2_NBAL;
102
103     static const float                  pf_ungroup3[3*3*3 * 3] = ADEC_LAYER2_UNGROUP3;
104     static const float                  pf_ungroup5[5*5*5 * 3] = ADEC_LAYER2_UNGROUP5;
105     static const float                  pf_ungroup9[9*9*9 * 3] = ADEC_LAYER2_UNGROUP9;
106
107     static const requantization_t       p_requantization_cd[16] = ADEC_LAYER2_REQUANTIZATION_CD;
108     static const requantization_t       p_requantization_ab1[16] = ADEC_LAYER2_REQUANTIZATION_AB1;
109     static const requantization_t       p_requantization_ab2[16] = ADEC_LAYER2_REQUANTIZATION_AB2;
110     static const requantization_t       p_requantization_ab3[16] = ADEC_LAYER2_REQUANTIZATION_AB3;
111     static const requantization_t       p_requantization_ab4[16] = ADEC_LAYER2_REQUANTIZATION_AB4;
112     static const requantization_t *     pp_requantization_ab[30] = ADEC_LAYER2_REQUANTIZATION_AB;
113
114     static int                          i_sblimit, i_bitrate_per_channel_index;
115     static int                          pi_scfsi_0[30], pi_scfsi_1[30];
116     static const u8 *                   pi_nbal;
117     static float                        ppf_sample_0[3][32], ppf_sample_1[3][32];
118     static const requantization_t *     pp_requantization_0[30];
119     static const requantization_t *     pp_requantization_1[30];
120     static requantization_t             requantization;
121     static const float *                pf_ungroup;
122
123     static float                        pf_scalefactor_0_0[30], pf_scalefactor_0_1[30], pf_scalefactor_0_2[30];
124     static float                        pf_scalefactor_1_0[30], pf_scalefactor_1_1[30], pf_scalefactor_1_2[30];
125
126     int                                 i_2nbal, i_gr;
127     float                               f_dummy;
128
129     s16 *                               p_s16;
130
131     int                                 i_need = 0, i_dump = 0;
132 #if 0
133     static const int                    pi_framesize[512] = ADEC_FRAME_SIZE;
134 #endif
135
136     /* Read the audio frame header and flush the bit buffer */
137     i_header = p_adec->header;
138     /* Read the sampling frequency (see ISO/IEC 11172-3 2.4.2.3) */
139     i_sampling_frequency = (int)((i_header & ADEC_HEADER_SAMPLING_FREQUENCY_MASK)
140         >> ADEC_HEADER_SAMPLING_FREQUENCY_SHIFT);
141     /* Read the mode (see ISO/IEC 11172-3 2.4.2.3) */
142     i_mode = (int)((i_header & ADEC_HEADER_MODE_MASK) >> ADEC_HEADER_MODE_SHIFT);
143     /* If a CRC can be found in the frame, get rid of it */
144     if ((i_header & ADEC_HEADER_PROTECTION_BIT_MASK) == 0)
145     {
146         NeedBits (&p_adec->bit_stream, 16);
147         DumpBits (&p_adec->bit_stream, 16);
148     }
149
150     /* Find out the bitrate per channel index */
151     i_bitrate_per_channel_index = (int)ppi_bitrate_per_channel_index[i_mode]
152         [(i_header & ADEC_HEADER_BITRATE_INDEX_MASK) >> ADEC_HEADER_BITRATE_INDEX_SHIFT];
153     /* Find out the number of subbands */
154     i_sblimit = (int)ppi_sblimit[i_sampling_frequency][i_bitrate_per_channel_index];
155     /* Check if the frame is valid or not */
156     if (i_sblimit == 0)
157     {
158         return (0);                                 /* the frame is invalid */
159     }
160     /* Find out the number of bits allocated */
161     pi_nbal = ppi_nbal[ (i_bitrate_per_channel_index <= 2) ? 0 : 1 ];
162
163     /* Find out the `bound' subband (see ISO/IEC 11172-3 2.4.2.3) */
164     if (i_mode == 1)
165     {
166         i_bound = (int)(((i_header & ADEC_HEADER_MODE_EXTENSION_MASK) >> (ADEC_HEADER_MODE_EXTENSION_SHIFT - 2)) + 4);
167         if (i_bound > i_sblimit)
168         {
169             i_bound = i_sblimit;
170         }
171     }
172     else
173     {
174         i_bound = i_sblimit;
175     }
176
177     /* Read the allocation information (see ISO/IEC 11172-3 2.4.1.6) */
178     for (i_sb = 0; i_sb < i_bound; i_sb++)
179     {
180         i_2nbal = 2 * (i_nbal = (int)pi_nbal[ i_sb ]);
181         NeedBits (&p_adec->bit_stream, i_2nbal);
182         i_need += i_2nbal;
183         pi_allocation_0[ i_sb ] = (int)(p_adec->bit_stream.buffer >> (32 - i_nbal));
184         p_adec->bit_stream.buffer <<= i_nbal;
185         pi_allocation_1[ i_sb ] = (int)(p_adec->bit_stream.buffer >> (32 - i_nbal));
186         p_adec->bit_stream.buffer <<= i_nbal;
187         p_adec->bit_stream.i_available -= i_2nbal;
188         i_dump += i_2nbal;
189     }
190     for (; i_sb < i_sblimit; i_sb++)
191     {
192         i_nbal = (int)pi_nbal[ i_sb ];
193         NeedBits (&p_adec->bit_stream, i_nbal);
194         i_need += i_nbal;
195         pi_allocation_0[ i_sb ] = (int)(p_adec->bit_stream.buffer >> (32 - i_nbal));
196         DumpBits (&p_adec->bit_stream, i_nbal);
197         i_dump += i_nbal;
198     }
199
200 #define MACRO(p_requantization) \
201     for (i_sb = 0; i_sb < i_bound; i_sb++) \
202     { \
203         if (pi_allocation_0[i_sb]) \
204         { \
205             pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
206             NeedBits (&p_adec->bit_stream, 2); \
207             i_need += 2; \
208             pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.buffer >> (32 - 2)); \
209             DumpBits (&p_adec->bit_stream, 2); \
210             i_dump += 2; \
211         } \
212         else \
213         { \
214             ppf_sample_0[0][i_sb] = .0; \
215             ppf_sample_0[1][i_sb] = .0; \
216             ppf_sample_0[2][i_sb] = .0; \
217         } \
218 \
219         if (pi_allocation_1[i_sb]) \
220         { \
221             pp_requantization_1[i_sb] = &((p_requantization)[pi_allocation_1[i_sb]]); \
222             NeedBits (&p_adec->bit_stream, 2); \
223             i_need += 2; \
224             pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.buffer >> (32 - 2)); \
225             DumpBits (&p_adec->bit_stream, 2); \
226             i_dump += 2; \
227         } \
228         else \
229         { \
230             ppf_sample_1[0][i_sb] = .0; \
231             ppf_sample_1[1][i_sb] = .0; \
232             ppf_sample_1[2][i_sb] = .0; \
233         } \
234     } \
235 \
236     for (; i_sb < i_sblimit; i_sb++) \
237     { \
238         if (pi_allocation_0[i_sb]) \
239         { \
240             pp_requantization_0[i_sb] = &((p_requantization)[pi_allocation_0[i_sb]]); \
241             NeedBits (&p_adec->bit_stream, 4); \
242             i_need += 4; \
243             pi_scfsi_0[i_sb] = (int)(p_adec->bit_stream.buffer >> (32 - 2)); \
244             p_adec->bit_stream.buffer <<= 2; \
245             pi_scfsi_1[i_sb] = (int)(p_adec->bit_stream.buffer >> (32 - 2)); \
246             p_adec->bit_stream.buffer <<= 2; \
247             p_adec->bit_stream.i_available -= 4; \
248             i_dump += 4; \
249         } \
250         else \
251         { \
252             ppf_sample_0[0][i_sb] = .0; \
253             ppf_sample_0[1][i_sb] = .0; \
254             ppf_sample_0[2][i_sb] = .0; \
255             ppf_sample_1[0][i_sb] = .0; \
256             ppf_sample_1[1][i_sb] = .0; \
257             ppf_sample_1[2][i_sb] = .0; \
258         } \
259     }
260 /* #define MACRO */
261
262     if (i_bitrate_per_channel_index <= 2)
263     {
264         MACRO (p_requantization_cd)
265     }
266     else
267     {
268         MACRO (pp_requantization_ab[i_sb])
269     }
270
271 #define SWITCH(pi_scfsi,pf_scalefactor_0,pf_scalefactor_1,pf_scalefactor_2) \
272     switch ((pi_scfsi)[i_sb]) \
273     { \
274         case 0: \
275             NeedBits (&p_adec->bit_stream, (3*6)); \
276             i_need += 18; \
277            (pf_scalefactor_0)[i_sb] = pf_scalefactor[p_adec->bit_stream.buffer >> (32 - 6)]; \
278             p_adec->bit_stream.buffer <<= 6; \
279            (pf_scalefactor_1)[i_sb] = pf_scalefactor[p_adec->bit_stream.buffer >> (32 - 6)]; \
280             p_adec->bit_stream.buffer <<= 6; \
281            (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.buffer >> (32 - 6)]; \
282             p_adec->bit_stream.buffer <<= 6; \
283             p_adec->bit_stream.i_available -= (3*6); \
284             i_dump += 18; \
285             break; \
286 \
287         case 1: \
288             NeedBits (&p_adec->bit_stream, (2*6)); \
289             i_need += 12; \
290            (pf_scalefactor_0)[i_sb] = \
291                (pf_scalefactor_1)[i_sb] = pf_scalefactor[p_adec->bit_stream.buffer >> (32 - 6)]; \
292             p_adec->bit_stream.buffer <<= 6; \
293            (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.buffer >> (32 - 6)]; \
294             p_adec->bit_stream.buffer <<= 6; \
295             p_adec->bit_stream.i_available -= (2*6); \
296             i_dump += 12; \
297             break; \
298 \
299         case 2: \
300             NeedBits (&p_adec->bit_stream, (1*6)); \
301             i_need += 6; \
302            (pf_scalefactor_0)[i_sb] = \
303                (pf_scalefactor_1)[i_sb] = \
304                (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.buffer >> (32 - 6)]; \
305             DumpBits (&p_adec->bit_stream, (1*6)); \
306             i_dump += 6; \
307             break; \
308 \
309         case 3: \
310             NeedBits (&p_adec->bit_stream, (2*6)); \
311             i_need += 12; \
312            (pf_scalefactor_0)[i_sb] = pf_scalefactor[p_adec->bit_stream.buffer >> (32 - 6)]; \
313             p_adec->bit_stream.buffer <<= 6; \
314            (pf_scalefactor_1)[i_sb] = \
315                (pf_scalefactor_2)[i_sb] = pf_scalefactor[p_adec->bit_stream.buffer >> (32 - 6)]; \
316             p_adec->bit_stream.buffer <<= 6; \
317             p_adec->bit_stream.i_available -= (2*6); \
318             i_dump += 12; \
319             break; \
320     }
321 /* #define SWITCH */
322
323     for (i_sb = 0; i_sb < i_bound; i_sb++)
324     {
325         if (pi_allocation_0[i_sb])
326         {
327             SWITCH (pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2)
328         }
329         if (pi_allocation_1[i_sb])
330         {
331             SWITCH (pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2)
332         }
333     }
334     for (; i_sb < i_sblimit; i_sb++)
335     {
336         if (pi_allocation_0[i_sb])
337         {
338             SWITCH (pi_scfsi_0, pf_scalefactor_0_0, pf_scalefactor_0_1, pf_scalefactor_0_2)
339             SWITCH (pi_scfsi_1, pf_scalefactor_1_0, pf_scalefactor_1_1, pf_scalefactor_1_2)
340         }
341     }
342     for (; i_sb < 32; i_sb++)
343     {
344         ppf_sample_0[0][i_sb] = .0;
345         ppf_sample_0[1][i_sb] = .0;
346         ppf_sample_0[2][i_sb] = .0;
347         ppf_sample_1[0][i_sb] = .0;
348         ppf_sample_1[1][i_sb] = .0;
349         ppf_sample_1[2][i_sb] = .0;
350     }
351
352 #define GROUPTEST(pp_requantization,ppf_sample,pf_sf) \
353     requantization = *((pp_requantization)[i_sb]); \
354     if (requantization.pf_ungroup == NULL) \
355     { \
356         NeedBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
357         i_need += requantization.i_bits_per_codeword; \
358        (ppf_sample)[0][i_sb] = (f_scalefactor_0 = (pf_sf)[i_sb]) * (requantization.f_slope * \
359            (p_adec->bit_stream.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
360         DumpBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
361         i_dump += requantization.i_bits_per_codeword; \
362 \
363         NeedBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
364         i_need += requantization.i_bits_per_codeword; \
365        (ppf_sample)[1][i_sb] = f_scalefactor_0 * (requantization.f_slope * \
366            (p_adec->bit_stream.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
367         DumpBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
368         i_dump += requantization.i_bits_per_codeword; \
369 \
370         NeedBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
371         i_need += requantization.i_bits_per_codeword; \
372        (ppf_sample)[2][i_sb] = f_scalefactor_0 * (requantization.f_slope * \
373            (p_adec->bit_stream.buffer >> (32 - requantization.i_bits_per_codeword)) + requantization.f_offset); \
374         DumpBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
375         i_dump += requantization.i_bits_per_codeword; \
376     } \
377     else \
378     { \
379         NeedBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
380         i_need += requantization.i_bits_per_codeword; \
381         pf_ungroup = requantization.pf_ungroup + 3 * \
382            (p_adec->bit_stream.buffer >> (32 - requantization.i_bits_per_codeword)); \
383         DumpBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
384         i_dump += requantization.i_bits_per_codeword; \
385        (ppf_sample)[0][i_sb] = (f_scalefactor_0 = (pf_sf)[i_sb]) * pf_ungroup[0]; \
386        (ppf_sample)[1][i_sb] = f_scalefactor_0 * pf_ungroup[1]; \
387        (ppf_sample)[2][i_sb] = f_scalefactor_0 * pf_ungroup[2]; \
388     }
389 /* #define GROUPTEST */
390
391 #define READ_SAMPLE_L2S(pf_scalefactor_0,pf_scalefactor_1,i_grlimit) \
392     for (; i_gr < (i_grlimit); i_gr++) \
393     { \
394         for (i_sb = 0; i_sb < i_bound; i_sb++) \
395         { \
396             if (pi_allocation_0[i_sb]) \
397             { \
398                 GROUPTEST (pp_requantization_0, ppf_sample_0, (pf_scalefactor_0)) \
399             } \
400             if (pi_allocation_1[i_sb]) \
401             { \
402                 GROUPTEST (pp_requantization_1, ppf_sample_1, (pf_scalefactor_1)) \
403             } \
404         } \
405         for (; i_sb < i_sblimit; i_sb++) \
406         { \
407             if (pi_allocation_0[i_sb]) \
408             { \
409                 requantization = *(pp_requantization_0[i_sb]); \
410                 if (requantization.pf_ungroup == NULL) \
411                 { \
412                     NeedBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
413                     i_need += requantization.i_bits_per_codeword; \
414                     ppf_sample_0[0][i_sb] = (f_scalefactor_0 = (pf_scalefactor_0)[i_sb]) * \
415                        (requantization.f_slope * (f_dummy = \
416                        (float)(p_adec->bit_stream.buffer >> (32 - requantization.i_bits_per_codeword))) + \
417                         requantization.f_offset); \
418                     DumpBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
419                     i_dump += requantization.i_bits_per_codeword; \
420                     ppf_sample_1[0][i_sb] = (f_scalefactor_1 = (pf_scalefactor_1)[i_sb]) * \
421                        (requantization.f_slope * f_dummy + requantization.f_offset); \
422 \
423                     NeedBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
424                     i_need += requantization.i_bits_per_codeword; \
425                     ppf_sample_0[1][i_sb] = f_scalefactor_0 * \
426                        (requantization.f_slope * (f_dummy = \
427                        (float)(p_adec->bit_stream.buffer >> (32 - requantization.i_bits_per_codeword))) + \
428                         requantization.f_offset); \
429                     DumpBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
430                     i_dump += requantization.i_bits_per_codeword; \
431                     ppf_sample_1[1][i_sb] = f_scalefactor_1 * \
432                        (requantization.f_slope * f_dummy + requantization.f_offset); \
433 \
434                     NeedBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
435                     i_need += requantization.i_bits_per_codeword; \
436                     ppf_sample_0[2][i_sb] = f_scalefactor_0 * \
437                        (requantization.f_slope * (f_dummy = \
438                        (float)(p_adec->bit_stream.buffer >> (32 - requantization.i_bits_per_codeword))) + \
439                         requantization.f_offset); \
440                     DumpBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
441                     i_dump += requantization.i_bits_per_codeword; \
442                     ppf_sample_1[2][i_sb] = f_scalefactor_1 * \
443                        (requantization.f_slope * f_dummy + requantization.f_offset); \
444                 } \
445                 else \
446                 { \
447                     NeedBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
448                     i_need += requantization.i_bits_per_codeword; \
449                     pf_ungroup = requantization.pf_ungroup + 3 * \
450                        (p_adec->bit_stream.buffer >> (32 - requantization.i_bits_per_codeword)); \
451                     DumpBits (&p_adec->bit_stream, requantization.i_bits_per_codeword); \
452                     i_dump += requantization.i_bits_per_codeword; \
453 \
454                     ppf_sample_0[0][i_sb] = (f_scalefactor_0 = (pf_scalefactor_0)[i_sb]) * pf_ungroup[0]; \
455                     ppf_sample_0[1][i_sb] = f_scalefactor_0 * pf_ungroup[1]; \
456                     ppf_sample_0[2][i_sb] = f_scalefactor_0 * pf_ungroup[2]; \
457 \
458                     ppf_sample_1[0][i_sb] = (f_scalefactor_1 = (pf_scalefactor_1)[i_sb]) * pf_ungroup[0]; \
459                     ppf_sample_1[1][i_sb] = f_scalefactor_1 * pf_ungroup[1]; \
460                     ppf_sample_1[2][i_sb] = f_scalefactor_1 * pf_ungroup[2]; \
461                 } \
462             } \
463         } \
464 \
465         DCT32 (ppf_sample_0[0], &p_adec->bank_0); \
466         PCM (&p_adec->bank_0, &p_s16, 2); \
467         p_s16 -= 63; \
468 \
469         DCT32 (ppf_sample_1[0], &p_adec->bank_1); \
470         PCM (&p_adec->bank_1, &p_s16, 2); \
471         p_s16 -= 1; \
472 \
473         DCT32 (ppf_sample_0[1], &p_adec->bank_0); \
474         PCM (&p_adec->bank_0, &p_s16, 2); \
475         p_s16 -= 63; \
476 \
477         DCT32 (ppf_sample_1[1], &p_adec->bank_1); \
478         PCM (&p_adec->bank_1, &p_s16, 2); \
479         p_s16 -= 1; \
480 \
481         DCT32 (ppf_sample_0[2], &p_adec->bank_0); \
482         PCM (&p_adec->bank_0, &p_s16, 2); \
483         p_s16 -= 63; \
484 \
485         DCT32 (ppf_sample_1[2], &p_adec->bank_1); \
486         PCM (&p_adec->bank_1, &p_s16, 2); \
487         p_s16 -= 1; \
488     }
489 /* #define READ_SAMPLE_L2S */
490
491     i_gr = 0;
492     p_s16 = buffer;
493
494     READ_SAMPLE_L2S (pf_scalefactor_0_0, pf_scalefactor_1_0, 4)
495     READ_SAMPLE_L2S (pf_scalefactor_0_1, pf_scalefactor_1_1, 8)
496     READ_SAMPLE_L2S (pf_scalefactor_0_2, pf_scalefactor_1_2, 12)
497
498     p_adec->bit_stream.buffer = 0;
499     p_adec->bit_stream.i_available = 0;
500     return (6);
501 }
502
503 /**** wkn ****/
504
505 int adec_init (audiodec_t * p_adec)
506 {
507     p_adec->bank_0.actual = p_adec->bank_0.v1;
508     p_adec->bank_0.pos = 0;
509     p_adec->bank_1.actual = p_adec->bank_1.v1;
510     p_adec->bank_1.pos = 0;
511     return 0;
512 }
513
514 int adec_sync_frame (audiodec_t * p_adec, adec_sync_info_t * p_sync_info)
515 {
516     static int mpeg1_sample_rate[3] = {44100, 48000, 32000};
517     static int mpeg1_layer1_bit_rate[15] = {
518         0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448
519     };
520     static int mpeg1_layer2_bit_rate[15] = {
521         0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384
522     };
523     static int mpeg2_layer1_bit_rate[15] = {
524         0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256
525     };
526     static int mpeg2_layer2_bit_rate[15] = {
527         0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160
528     };
529     u32 header;
530     int index;
531     int * bit_rate_table;
532     int sample_rate;
533     int bit_rate;
534     int frame_size;
535
536     p_adec->bit_stream.total_bytes_read = 0;
537     header = GetByte (&p_adec->bit_stream) << 24;
538     header |= GetByte (&p_adec->bit_stream) << 16;
539     header |= GetByte (&p_adec->bit_stream) << 8;
540     header |= GetByte (&p_adec->bit_stream);
541
542     p_adec->header = header;
543
544     /* basic header check : sync word, no emphasis */
545     if ((header & 0xfff00003) != 0xfff00000)
546         return 1;
547
548     index = (header >> 10) & 3;         /* sample rate index */
549     if (index > 2)
550         return 1;
551     sample_rate = mpeg1_sample_rate[index];
552
553     switch ((header >> 17) & 7) {
554     case 2:     /* mpeg 2, layer 2 */
555         sample_rate >>= 1;              /* half sample rate for mpeg2 */
556         bit_rate_table = mpeg2_layer2_bit_rate;
557         break;
558     case 3:     /* mpeg 2, layer 1 */
559         sample_rate >>= 1;              /* half sample rate for mpeg2 */
560         bit_rate_table = mpeg2_layer1_bit_rate;
561         break;
562     case 6:     /* mpeg1, layer 2 */
563         bit_rate_table = mpeg1_layer2_bit_rate;
564         break;
565     case 7:     /* mpeg1, layer 1 */
566         bit_rate_table = mpeg1_layer1_bit_rate;
567         break;
568     default:    /* invalid layer */
569         return 1;
570     }
571
572     index = (header >> 12) & 15;        /* bit rate index */
573     if (index > 14)
574         return 1;
575     bit_rate = bit_rate_table[index];
576
577     p_sync_info->sample_rate = sample_rate;
578     p_sync_info->bit_rate = bit_rate;
579
580     if ((header & 0x60000) == 0x60000) {        /* layer 1 */
581         frame_size = 48000 * bit_rate / sample_rate;
582         if (header & 0x200)     /* padding */
583             frame_size += 4;
584     } else {    /* layer >1 */
585         frame_size = 144000 * bit_rate / sample_rate;
586         if (header & 0x200)     /* padding */
587             frame_size ++;
588     }
589
590     p_sync_info->frame_size = frame_size;
591     p_adec->frame_size = frame_size;
592
593     return 0;
594 }
595
596 int adec_decode_frame (audiodec_t * p_adec, s16 * buffer)
597 {
598     p_adec->bit_stream.i_available = 0;
599
600     adec_Layer2_Stereo (p_adec, buffer);
601
602     if (p_adec->bit_stream.total_bytes_read > p_adec->frame_size)
603         return 1;
604
605     while (p_adec->bit_stream.total_bytes_read < p_adec->frame_size)
606         GetByte (&p_adec->bit_stream);
607
608     return 0;
609 }