]> git.sesse.net Git - vlc/blob - plugins/mpeg_adec/adec_layer1.c
* ALL: the first libvlc commit.
[vlc] / plugins / mpeg_adec / adec_layer1.c
1 /*****************************************************************************
2  * adec_layer1.c: MPEG Layer I audio decoder
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: adec_layer1.c,v 1.7 2002/06/01 12:32:00 sam Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Michel Lespinasse <walken@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include <stdlib.h>                                                  /* NULL */
26 #include <string.h>                                    /* memcpy(), memset() */
27
28 #include <vlc/vlc.h>
29 #include <vlc/decoder.h>
30
31 #include "mpeg_adec_generic.h"
32 #include "mpeg_adec.h"
33 #include "adec_math.h"                                     /* DCT32(), PCM() */
34
35 /**** wkn ****/
36
37 static float adec_scalefactor_table[64] =
38 {   /* 2 ^ (1 - i/3) */
39     2.0000000000000000, 1.5874010519681994, 1.2599210498948732,
40     1.0000000000000000, 0.7937005259840998, 0.6299605249474366,
41     0.5000000000000000, 0.3968502629920499, 0.3149802624737183,
42     0.2500000000000000, 0.1984251314960249, 0.1574901312368591,
43     0.1250000000000000, 0.0992125657480125, 0.0787450656184296,
44     0.0625000000000000, 0.0496062828740062, 0.0393725328092148,
45     0.0312500000000000, 0.0248031414370031, 0.0196862664046074,
46     0.0156250000000000, 0.0124015707185016, 0.0098431332023037,
47     0.0078125000000000, 0.0062007853592508, 0.0049215666011518,
48     0.0039062500000000, 0.0031003926796254, 0.0024607833005759,
49     0.0019531250000000, 0.0015501963398127, 0.0012303916502880,
50     0.0009765625000000, 0.0007750981699063, 0.0006151958251440,
51     0.0004882812500000, 0.0003875490849532, 0.0003075979125720,
52     0.0002441406250000, 0.0001937745424766, 0.0001537989562860,
53     0.0001220703125000, 0.0000968872712383, 0.0000768994781430,
54     0.0000610351562500, 0.0000484436356191, 0.0000384497390715,
55     0.0000305175781250, 0.0000242218178096, 0.0000192248695357,
56     0.0000152587890625, 0.0000121109089048, 0.0000096124347679,
57     0.0000076293945312, 0.0000060554544524, 0.0000048062173839,
58     0.0000038146972656, 0.0000030277272262, 0.0000024031086920,
59     0.0000019073486328, 0.0000015138636131, 0.0000012015543460,
60     0.0000009536743164 /* last element is not in the standard... invalid ??? */
61 };
62
63 static float adec_slope_table[15] =
64 {
65     0.6666666666666666, 0.2857142857142857, 0.1333333333333333,
66     0.0645161290322581, 0.0317460317460317, 0.0157480314960630,
67     0.0078431372549020, 0.0039138943248532, 0.0019550342130987,
68     0.0009770395701026, 0.0004884004884005, 0.0002441704309608,
69     0.0001220777635354, 0.0000610370189520, 0.0000305180437934
70 };
71
72 static float adec_offset_table[15] =
73 {
74     -0.6666666666666666, -0.8571428571428571, -0.9333333333333333,
75     -0.9677419354838710, -0.9841269841269841, -0.9921259842519685,
76     -0.9960784313725490, -0.9980430528375733, -0.9990224828934506,
77     -0.9995114802149487, -0.9997557997557998, -0.9998779147845196,
78     -0.9999389611182323, -0.9999694814905240, -0.9999847409781033
79 };
80
81 static u8 adec_layer1_allocation_table[15] =
82 {
83     0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
84 };
85
86 static int adec_bound_table[4] = { 4, 8, 12, 16 };
87
88 int adec_layer1_mono( adec_thread_t * p_adec, s16 * buffer )
89 {
90     u8 allocation[32];
91     float slope[32];
92     float offset[32];
93     float sample[32];
94
95     int i_sb;
96     int s;
97     int i_read_bits = 0;
98
99     /*
100      * Parse the allocation tables
101      */
102
103     for (i_sb = 0; i_sb < 32; i_sb += 2)
104     {
105         u8 tmp;
106
107         /* i_read_bits will be updated at the end of the loop */
108         tmp = GetBits ( &p_adec->bit_stream, 8 );
109
110         if ( (tmp >> 4) > 14 )
111         {
112             return 1;
113         }
114
115         allocation[i_sb] = adec_layer1_allocation_table [tmp >> 4];
116
117         if ((tmp & 15) > 14)
118         {
119             return 1;
120         }
121
122         allocation[i_sb+1] = adec_layer1_allocation_table [tmp & 15];
123     }
124
125     i_read_bits += 8 * 16; /* we did 16 iterations */
126
127     /*
128      * Parse scalefactors
129      */
130
131     for ( i_sb = 0; i_sb < 32; i_sb++ )
132     {
133         if ( allocation[i_sb] )
134         {
135             int index;
136             float scalefactor;
137
138             index = GetBits( &p_adec->bit_stream, 6);
139
140             /* We also add the bits we'll take later in the sample parsing */
141             i_read_bits += 6 + 12 * allocation[i_sb];
142
143             scalefactor = adec_scalefactor_table[index];
144
145             slope[i_sb] = adec_slope_table[allocation[i_sb]-2] * scalefactor;
146             offset[i_sb] = adec_offset_table[allocation[i_sb]-2] * scalefactor;
147         }
148     }
149
150     /* 
151      * Parse samples
152      */
153
154     for ( s = 0 ; s < 12; s++)
155     {
156         for (i_sb = 0; i_sb < 32; i_sb++)
157         {
158             if (!allocation[i_sb])
159             {
160                 sample[i_sb] = 0;
161             }
162             else
163             {
164                 int code;
165
166                 /* The bits were already counted in the scalefactors parsing */
167                 code = GetBits( &p_adec->bit_stream, allocation[i_sb] );
168
169                 sample[i_sb] = slope[i_sb] * code + offset[i_sb];
170             }
171         }
172
173         DCT32( &p_adec->bank_0, sample );
174         PCM (&p_adec->bank_0, buffer, 1);
175         buffer += 32;
176     }
177
178     p_adec->i_read_bits += i_read_bits;
179
180     return 0;
181 }
182
183 int adec_layer1_stereo( adec_thread_t * p_adec, s16 * buffer )
184 {
185     u8 allocation_0[32], allocation_1[32];
186     float slope_0[32], slope_1[32];
187     float offset_0[32], offset_1[32];
188     float sample_0[32], sample_1[32];
189
190     int bound;
191     int i_sb;
192     int s;
193     int i_read_bits = 0;
194
195     /*
196      * Calculate bound
197      */
198
199     bound = 32;
200     if ( (p_adec->header & 0xc0) == 0x40)
201     {
202         /* intensity stereo */
203         int index;
204         index = (p_adec->header >> 4) & 3;
205         bound = adec_bound_table[index];
206     }
207
208     /*
209      * Parse allocation
210      */
211
212     for (i_sb = 0; i_sb < bound; i_sb++)
213     {
214         u8 tmp;
215         tmp = GetBits( &p_adec->bit_stream, 8 );
216         if ((tmp >> 4) > 14)
217         {
218             return 1;
219         }
220         allocation_0[i_sb] = adec_layer1_allocation_table [tmp >> 4];
221         if ((tmp & 15) > 14)
222         {
223             return 1;
224         }
225         allocation_1[i_sb] = adec_layer1_allocation_table [tmp & 15];
226     }
227
228     for (; i_sb < 32; i_sb += 2)
229     {
230         u8 tmp;
231         tmp = GetBits( &p_adec->bit_stream, 8 );
232
233         if ((tmp >> 4) > 14)
234         {
235             return 1;
236         }
237         allocation_0[i_sb] = allocation_1[i_sb]
238             = adec_layer1_allocation_table [tmp >> 4];
239
240         if ((tmp & 15) > 14)
241         {
242             return 1;
243         }
244         allocation_0[i_sb+1] = allocation_1[i_sb+1]
245             = adec_layer1_allocation_table [tmp & 15];
246     }
247
248     i_read_bits += 4 * ( 32 + bound ); /* we read 8*bound and 4*(32-bound) */
249
250     /*
251      * Parse scalefactors
252      */
253
254     for ( i_sb = 0; i_sb < 32; i_sb++ )
255     {
256         if ( allocation_0[i_sb] )
257         {
258             int index;
259             float scalefactor;
260
261             index = GetBits( &p_adec->bit_stream, 6 );
262             i_read_bits += 6;
263
264             scalefactor = adec_scalefactor_table[index];
265
266             slope_0[i_sb] = adec_slope_table[allocation_0[i_sb]-2] * scalefactor;
267             offset_0[i_sb] = adec_offset_table[allocation_0[i_sb]-2] * scalefactor;
268         }
269
270         if (allocation_1[i_sb])
271         {
272             int index;
273             float scalefactor;
274
275             index = GetBits( &p_adec->bit_stream, 6 );
276             i_read_bits += 6;
277
278             scalefactor = adec_scalefactor_table[index];
279
280             slope_1[i_sb] = adec_slope_table[allocation_1[i_sb]-2] * scalefactor;
281             offset_1[i_sb] = adec_offset_table[allocation_1[i_sb]-2] * scalefactor;
282         }
283     }
284
285     /* parse samples */
286
287     for (s = 0; s < 12; s++)
288     {
289         for (i_sb = 0; i_sb < bound; i_sb++)
290         {
291             if (!allocation_0[i_sb])
292             {
293                 sample_0[i_sb] = 0;
294             }
295             else
296             {
297                 int code;
298
299                 code = GetBits( &p_adec->bit_stream, allocation_0[i_sb] );
300                 i_read_bits += allocation_0[i_sb];
301
302                 sample_0[i_sb] = slope_0[i_sb] * code + offset_0[i_sb];
303             }
304
305             if ( !allocation_1[i_sb] )
306             {
307                 sample_1[i_sb] = 0;
308             }
309             else
310             {
311                 int code;
312
313                 code = GetBits( &p_adec->bit_stream, allocation_1[i_sb] );
314                 i_read_bits += allocation_1[i_sb];
315
316                 sample_1[i_sb] = slope_1[i_sb] * code + offset_1[i_sb];
317             }
318         }
319
320         for (; i_sb < 32; i_sb++)
321         {
322             if (!allocation_0[i_sb])
323             {
324                 sample_0[i_sb] = 0;
325                 sample_1[i_sb] = 0;
326             }
327             else
328             {
329                 int code;
330
331                 code = GetBits( &p_adec->bit_stream, allocation_0[i_sb] );
332                 i_read_bits += allocation_0[i_sb];
333
334                 sample_0[i_sb] = slope_0[i_sb] * code + offset_0[i_sb];
335                 sample_1[i_sb] = slope_1[i_sb] * code + offset_1[i_sb];
336             }
337         }
338
339         DCT32( &p_adec->bank_0, sample_0 );
340         PCM (&p_adec->bank_0, buffer, 2);
341         DCT32( &p_adec->bank_1, sample_1 );
342         PCM (&p_adec->bank_1, buffer + 1, 2);
343         buffer += 64;
344     }
345
346     p_adec->i_read_bits += i_read_bits;
347
348     return 0;
349 }
350