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