]> git.sesse.net Git - vlc/blob - src/audio_decoder/adec_layer1.c
0cae8a4899ea10dbaf88ca270cfea1dc4ceb6307
[vlc] / src / audio_decoder / adec_layer1.c
1 /*****************************************************************************
2  * adec_layer1.c: MPEG Layer I audio decoder
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: adec_layer1.c,v 1.5 2001/04/12 01:52:45 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 /*
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 "stream_control.h"
42 #include "input_ext-dec.h"
43
44 #include "adec_generic.h"
45 #include "audio_decoder.h"
46 #include "adec_math.h"                                     /* DCT32(), PCM() */
47
48 /**** wkn ****/
49
50 static float adec_scalefactor_table[64] =
51 {   /* 2 ^ (1 - i/3) */
52     2.0000000000000000, 1.5874010519681994, 1.2599210498948732,
53     1.0000000000000000, 0.7937005259840998, 0.6299605249474366,
54     0.5000000000000000, 0.3968502629920499, 0.3149802624737183,
55     0.2500000000000000, 0.1984251314960249, 0.1574901312368591,
56     0.1250000000000000, 0.0992125657480125, 0.0787450656184296,
57     0.0625000000000000, 0.0496062828740062, 0.0393725328092148,
58     0.0312500000000000, 0.0248031414370031, 0.0196862664046074,
59     0.0156250000000000, 0.0124015707185016, 0.0098431332023037,
60     0.0078125000000000, 0.0062007853592508, 0.0049215666011518,
61     0.0039062500000000, 0.0031003926796254, 0.0024607833005759,
62     0.0019531250000000, 0.0015501963398127, 0.0012303916502880,
63     0.0009765625000000, 0.0007750981699063, 0.0006151958251440,
64     0.0004882812500000, 0.0003875490849532, 0.0003075979125720,
65     0.0002441406250000, 0.0001937745424766, 0.0001537989562860,
66     0.0001220703125000, 0.0000968872712383, 0.0000768994781430,
67     0.0000610351562500, 0.0000484436356191, 0.0000384497390715,
68     0.0000305175781250, 0.0000242218178096, 0.0000192248695357,
69     0.0000152587890625, 0.0000121109089048, 0.0000096124347679,
70     0.0000076293945312, 0.0000060554544524, 0.0000048062173839,
71     0.0000038146972656, 0.0000030277272262, 0.0000024031086920,
72     0.0000019073486328, 0.0000015138636131, 0.0000012015543460,
73     0.0000009536743164 /* last element is not in the standard... invalid ??? */
74 };
75
76 static float adec_slope_table[15] =
77 {
78     0.6666666666666666, 0.2857142857142857, 0.1333333333333333,
79     0.0645161290322581, 0.0317460317460317, 0.0157480314960630,
80     0.0078431372549020, 0.0039138943248532, 0.0019550342130987,
81     0.0009770395701026, 0.0004884004884005, 0.0002441704309608,
82     0.0001220777635354, 0.0000610370189520, 0.0000305180437934
83 };
84
85 static float adec_offset_table[15] =
86 {
87     -0.6666666666666666, -0.8571428571428571, -0.9333333333333333,
88     -0.9677419354838710, -0.9841269841269841, -0.9921259842519685,
89     -0.9960784313725490, -0.9980430528375733, -0.9990224828934506,
90     -0.9995114802149487, -0.9997557997557998, -0.9998779147845196,
91     -0.9999389611182323, -0.9999694814905240, -0.9999847409781033
92 };
93
94 static u8 adec_layer1_allocation_table[15] =
95 {
96     0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
97 };
98
99 static int adec_bound_table[4] = { 4, 8, 12, 16 };
100
101 int adec_layer1_mono( adec_thread_t * p_adec, s16 * buffer )
102 {
103     u8 allocation[32];
104     float slope[32];
105     float offset[32];
106     float sample[32];
107
108     int i_sb;
109     int s;
110     int i_read_bits = 0;
111
112     /*
113      * Parse the allocation tables
114      */
115
116     for (i_sb = 0; i_sb < 32; i_sb += 2)
117     {
118         u8 tmp;
119
120         /* i_read_bits will be updated at the end of the loop */
121         tmp = GetBits ( &p_adec->bit_stream, 8 );
122
123         if ( (tmp >> 4) > 14 )
124         {
125             return 1;
126         }
127
128         allocation[i_sb] = adec_layer1_allocation_table [tmp >> 4];
129
130         if ((tmp & 15) > 14)
131         {
132             return 1;
133         }
134
135         allocation[i_sb+1] = adec_layer1_allocation_table [tmp & 15];
136     }
137
138     i_read_bits += 8 * 16; /* we did 16 iterations */
139
140     /*
141      * Parse scalefactors
142      */
143
144     for ( i_sb = 0; i_sb < 32; i_sb++ )
145     {
146         if ( allocation[i_sb] )
147         {
148             int index;
149             float scalefactor;
150
151             index = GetBits( &p_adec->bit_stream, 6);
152
153             /* We also add the bits we'll take later in the sample parsing */
154             i_read_bits += 6 + 12 * allocation[i_sb];
155
156             scalefactor = adec_scalefactor_table[index];
157
158             slope[i_sb] = adec_slope_table[allocation[i_sb]-2] * scalefactor;
159             offset[i_sb] = adec_offset_table[allocation[i_sb]-2] * scalefactor;
160         }
161     }
162
163     /* 
164      * Parse samples
165      */
166
167     for ( s = 0 ; s < 12; s++)
168     {
169         s16 * XXX_buf;
170
171         for (i_sb = 0; i_sb < 32; i_sb++)
172         {
173             if (!allocation[i_sb])
174             {
175                 sample[i_sb] = 0;
176             }
177             else
178             {
179                 int code;
180
181                 /* The bits were already counted in the scalefactors parsing */
182                 code = GetBits( &p_adec->bit_stream, allocation[i_sb] );
183
184                 sample[i_sb] = slope[i_sb] * code + offset[i_sb];
185             }
186         }
187
188         DCT32 (sample, &p_adec->bank_0);
189         XXX_buf = buffer;
190         PCM (&p_adec->bank_0, &XXX_buf, 1);
191         buffer += 32;
192     }
193
194     p_adec->i_read_bits += i_read_bits;
195
196     return 0;
197 }
198
199 int adec_layer1_stereo( adec_thread_t * p_adec, s16 * buffer )
200 {
201     u8 allocation_0[32], allocation_1[32];
202     float slope_0[32], slope_1[32];
203     float offset_0[32], offset_1[32];
204     float sample_0[32], sample_1[32];
205
206     int bound;
207     int i_sb;
208     int s;
209     int i_read_bits = 0;
210
211     /*
212      * Calculate bound
213      */
214
215     bound = 32;
216     if ( (p_adec->header & 0xc0) == 0x40)
217     {
218         /* intensity stereo */
219         int index;
220         index = (p_adec->header >> 4) & 3;
221         bound = adec_bound_table[index];
222     }
223
224     /*
225      * Parse allocation
226      */
227
228     for (i_sb = 0; i_sb < bound; i_sb++)
229     {
230         u8 tmp;
231         tmp = GetBits( &p_adec->bit_stream, 8 );
232         if ((tmp >> 4) > 14)
233         {
234             return 1;
235         }
236         allocation_0[i_sb] = adec_layer1_allocation_table [tmp >> 4];
237         if ((tmp & 15) > 14)
238         {
239             return 1;
240         }
241         allocation_1[i_sb] = adec_layer1_allocation_table [tmp & 15];
242     }
243
244     for (; i_sb < 32; i_sb += 2)
245     {
246         u8 tmp;
247         tmp = GetBits( &p_adec->bit_stream, 8 );
248
249         if ((tmp >> 4) > 14)
250         {
251             return 1;
252         }
253         allocation_0[i_sb] = allocation_1[i_sb]
254             = adec_layer1_allocation_table [tmp >> 4];
255
256         if ((tmp & 15) > 14)
257         {
258             return 1;
259         }
260         allocation_0[i_sb+1] = allocation_1[i_sb+1]
261             = adec_layer1_allocation_table [tmp & 15];
262     }
263
264     i_read_bits += 4 * ( 32 + bound ); /* we read 8*bound and 4*(32-bound) */
265
266     /*
267      * Parse scalefactors
268      */
269
270     for ( i_sb = 0; i_sb < 32; i_sb++ )
271     {
272         if ( allocation_0[i_sb] )
273         {
274             int index;
275             float scalefactor;
276
277             index = GetBits( &p_adec->bit_stream, 6 );
278             i_read_bits += 6;
279
280             scalefactor = adec_scalefactor_table[index];
281
282             slope_0[i_sb] = adec_slope_table[allocation_0[i_sb]-2] * scalefactor;
283             offset_0[i_sb] = adec_offset_table[allocation_0[i_sb]-2] * scalefactor;
284         }
285
286         if (allocation_1[i_sb])
287         {
288             int index;
289             float scalefactor;
290
291             index = GetBits( &p_adec->bit_stream, 6 );
292             i_read_bits += 6;
293
294             scalefactor = adec_scalefactor_table[index];
295
296             slope_1[i_sb] = adec_slope_table[allocation_1[i_sb]-2] * scalefactor;
297             offset_1[i_sb] = adec_offset_table[allocation_1[i_sb]-2] * scalefactor;
298         }
299     }
300
301     /* parse samples */
302
303     for (s = 0; s < 12; s++)
304     {
305         s16 * XXX_buf;
306
307         for (i_sb = 0; i_sb < bound; i_sb++)
308         {
309             if (!allocation_0[i_sb])
310             {
311                 sample_0[i_sb] = 0;
312             }
313             else
314             {
315                 int code;
316
317                 code = GetBits( &p_adec->bit_stream, allocation_0[i_sb] );
318                 i_read_bits += allocation_0[i_sb];
319
320                 sample_0[i_sb] = slope_0[i_sb] * code + offset_0[i_sb];
321             }
322
323             if ( !allocation_1[i_sb] )
324             {
325                 sample_1[i_sb] = 0;
326             }
327             else
328             {
329                 int code;
330
331                 code = GetBits( &p_adec->bit_stream, allocation_1[i_sb] );
332                 i_read_bits += allocation_1[i_sb];
333
334                 sample_1[i_sb] = slope_1[i_sb] * code + offset_1[i_sb];
335             }
336         }
337
338         for (; i_sb < 32; i_sb++)
339         {
340             if (!allocation_0[i_sb])
341             {
342                 sample_0[i_sb] = 0;
343                 sample_1[i_sb] = 0;
344             }
345             else
346             {
347                 int code;
348
349                 code = GetBits( &p_adec->bit_stream, allocation_0[i_sb] );
350                 i_read_bits += allocation_0[i_sb];
351
352                 sample_0[i_sb] = slope_0[i_sb] * code + offset_0[i_sb];
353                 sample_1[i_sb] = slope_1[i_sb] * code + offset_1[i_sb];
354             }
355         }
356
357         DCT32 (sample_0, &p_adec->bank_0);
358         XXX_buf = buffer;
359         PCM (&p_adec->bank_0, &XXX_buf, 2);
360         DCT32 (sample_1, &p_adec->bank_1);
361         XXX_buf = buffer+1;
362         PCM (&p_adec->bank_1, &XXX_buf, 2);
363         buffer += 64;
364     }
365
366     p_adec->i_read_bits += i_read_bits;
367
368     return 0;
369 }
370