OSDN Git Service

wwww
[proj16/16.git] / src / lib / doslib / ext / lame / layer2.c
1 /* 
2  * layer2.c: Mpeg Layer-2 audio decoder 
3  *
4  * Copyright (C) 1999-2010 The L.A.M.E. project
5  *
6  * Initially written by Michael Hipp, see also AUTHORS and README.
7  *  
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library 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  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 /* $Id: layer2.c,v 1.32 2011/05/24 20:40:56 robert Exp $ */
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include "common.h"
30 #include "layer2.h"
31 #include "l2tables.h"
32 #include "decode_i386.h"
33
34 #ifdef WITH_DMALLOC
35 #include <dmalloc.h>
36 #endif
37 #include <assert.h>
38
39 static int gd_are_hip_tables_layer2_initialized = 0;
40
41 static unsigned char grp_3tab[32 * 3] = { 0, }; /* used: 27 */
42 static unsigned char grp_5tab[128 * 3] = { 0, }; /* used: 125 */
43 static unsigned char grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
44
45
46 void
47 hip_init_tables_layer2(void)
48 {
49     static const double mulmul[27] = {
50         0.0, -2.0 / 3.0, 2.0 / 3.0,
51         2.0 / 7.0, 2.0 / 15.0, 2.0 / 31.0, 2.0 / 63.0, 2.0 / 127.0, 2.0 / 255.0,
52         2.0 / 511.0, 2.0 / 1023.0, 2.0 / 2047.0, 2.0 / 4095.0, 2.0 / 8191.0,
53         2.0 / 16383.0, 2.0 / 32767.0, 2.0 / 65535.0,
54         -4.0 / 5.0, -2.0 / 5.0, 2.0 / 5.0, 4.0 / 5.0,
55         -8.0 / 9.0, -4.0 / 9.0, -2.0 / 9.0, 2.0 / 9.0, 4.0 / 9.0, 8.0 / 9.0
56     };
57     static const unsigned char base[3][9] = {
58         {1, 0, 2,},
59         {17, 18, 0, 19, 20,},
60         {21, 1, 22, 23, 0, 24, 25, 2, 26}
61     };
62     int     i, j, k, l, len;
63     real   *table;
64     static const int tablen[3] = { 3, 5, 9 };
65     static unsigned char *itable, *tables[3] = { grp_3tab, grp_5tab, grp_9tab };
66
67     if (gd_are_hip_tables_layer2_initialized) {
68         return;
69     }
70     gd_are_hip_tables_layer2_initialized = 1;
71
72     for (i = 0; i < 3; i++) {
73         itable = tables[i];
74         len = tablen[i];
75         for (j = 0; j < len; j++)
76             for (k = 0; k < len; k++)
77                 for (l = 0; l < len; l++) {
78                     *itable++ = base[i][l];
79                     *itable++ = base[i][k];
80                     *itable++ = base[i][j];
81                 }
82     }
83
84     for (k = 0; k < 27; k++) {
85         double  m = mulmul[k];
86         table = muls[k];
87         for (j = 3, i = 0; i < 63; i++, j--)
88             *table++ = (real) (m * pow(2.0, (double) j / 3.0));
89         *table++ = 0.0;
90     }
91 }
92
93
94 static unsigned char*
95 grp_table_select(short d1, unsigned int idx)
96 {
97     /* RH: it seems to be common, that idx is larger than the table's sizes.
98            is it OK to return a zero vector in this case? FIXME
99     /*/
100     static unsigned char dummy_table[] = { 0,0,0 };
101     unsigned int x;
102     switch (d1) {
103         case 3:
104             x = 3*3*3;
105             idx = idx < x ? idx : x;
106             return &grp_3tab[3 * idx]; 
107         case 5: 
108             x = 5*5*5;
109             idx = idx < x ? idx : x;
110             return &grp_5tab[3 * idx]; 
111         case 9:
112             x = 9*9*9;
113             idx = idx < x ? idx : x;
114             return &grp_9tab[3 * idx];
115         default:
116             /* fatal error */
117             assert(0);
118     }
119     return &dummy_table[0];
120 }
121
122 typedef struct sideinfo_layer_II_struct
123 {
124     unsigned char allocation[SBLIMIT][2]; 
125     unsigned char scalefactor[SBLIMIT][2][3]; /* subband / channel / block */
126 } sideinfo_layer_II;
127
128
129
130 static void
131 II_step_one(PMPSTR mp, sideinfo_layer_II *si, struct frame *fr)
132 {
133     int     nch = fr->stereo;
134     int     sblimit = fr->II_sblimit;
135     int     jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : fr->II_sblimit;
136     struct al_table2 const *alloc1 = fr->alloc;
137     unsigned char scfsi[SBLIMIT][2];
138     int     i, ch;
139
140     memset(si, 0, sizeof(*si));
141
142     if (nch == 2) {
143         for (i = 0; i < jsbound; ++i) {
144             short   step = alloc1->bits;
145             unsigned char b0 = get_leq_8_bits(mp, step);
146             unsigned char b1 = get_leq_8_bits(mp, step);
147             alloc1 += (1 << step);
148             si->allocation[i][0] = b0;
149             si->allocation[i][1] = b1;
150         }
151         for (i = jsbound; i < sblimit; ++i) {
152             short   step = alloc1->bits;
153             unsigned char b0 = get_leq_8_bits(mp, step);
154             alloc1 += (1 << step);
155             si->allocation[i][0] = b0;
156             si->allocation[i][1] = b0;
157         }
158         for (i = 0; i < sblimit; ++i) {
159             unsigned char n0 = si->allocation[i][0];
160             unsigned char n1 = si->allocation[i][1];
161             unsigned char b0 = n0 ? get_leq_8_bits(mp, 2) : 0;
162             unsigned char b1 = n1 ? get_leq_8_bits(mp, 2) : 0;
163             scfsi[i][0] = b0;
164             scfsi[i][1] = b1;
165         }
166     }
167     else {              /* mono */
168         for (i = 0; i < sblimit; ++i) {
169             short   step = alloc1->bits;
170             unsigned char b0 = get_leq_8_bits(mp, step);
171             alloc1 += (1 << step);
172             si->allocation[i][0] = b0;
173         }
174         for (i = 0; i < sblimit; ++i) {
175             unsigned char n0 = si->allocation[i][0];
176             unsigned char b0 = n0 ? get_leq_8_bits(mp, 2) : 0;
177             scfsi[i][0] = b0;
178         }
179     }
180     for (i = 0; i < sblimit; ++i) {
181         for (ch = 0; ch < nch; ++ch) {
182             unsigned char s0 = 0, s1 = 0, s2 = 0;
183             if (si->allocation[i][ch]) {
184                 switch (scfsi[i][ch]) {
185                     case 0:
186                         s0 = get_leq_8_bits(mp, 6);
187                         s1 = get_leq_8_bits(mp, 6);
188                         s2 = get_leq_8_bits(mp, 6);
189                         break;
190                     case 1:
191                         s0 = get_leq_8_bits(mp, 6);
192                         s1 = s0;
193                         s2 = get_leq_8_bits(mp, 6);
194                         break;
195                     case 2:
196                         s0 = get_leq_8_bits(mp, 6);
197                         s1 = s0;
198                         s2 = s0;
199                         break;
200                     case 3:
201                         s0 = get_leq_8_bits(mp, 6); 
202                         s1 = get_leq_8_bits(mp, 6);
203                         s2 = s1;
204                         break;
205                     default:
206                         assert(0);
207                 }
208             }
209             si->scalefactor[i][ch][0] = s0;
210             si->scalefactor[i][ch][1] = s1;
211             si->scalefactor[i][ch][2] = s2;
212         }
213     }
214 }
215
216 static void
217 II_step_two(PMPSTR mp, sideinfo_layer_II* si, struct frame *fr, int gr, real fraction[2][4][SBLIMIT])
218 {
219     struct al_table2 const *alloc1 = fr->alloc;
220     int     sblimit = fr->II_sblimit;
221     int     jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : fr->II_sblimit;
222     int     i, ch, nch = fr->stereo;
223     double  cm, r0, r1, r2;
224
225     for (i = 0; i < jsbound; ++i) {
226         short   step = alloc1->bits;
227         for (ch = 0; ch < nch; ++ch) {
228             unsigned char ba = si->allocation[i][ch];
229             if (ba) {
230                 unsigned char x1 = si->scalefactor[i][ch][gr];
231                 struct al_table2 const *alloc2 = alloc1 + ba;
232                 short   k = alloc2->bits;
233                 short   d1 = alloc2->d;
234                 assert( k <= 16 );
235                 k = (k <= 16) ? k : 16;
236                 assert( x1 < 64 );
237                 x1 = (x1 < 64) ? x1 : 63;
238                 if (d1 < 0) {
239                     int v0 = getbits(mp, k);
240                     int v1 = getbits(mp, k);
241                     int v2 = getbits(mp, k);
242                     cm = muls[k][x1];
243                     r0 = (v0 + d1) * cm;
244                     r1 = (v1 + d1) * cm;
245                     r2 = (v2 + d1) * cm;
246                 }
247                 else {
248                     unsigned int idx = getbits(mp, k);
249                     unsigned char *tab = grp_table_select(d1, idx);
250                     unsigned char k0 = tab[0];
251                     unsigned char k1 = tab[1];
252                     unsigned char k2 = tab[2];
253                     r0 = muls[k0][x1];
254                     r1 = muls[k1][x1];
255                     r2 = muls[k2][x1];
256                 }
257                 fraction[ch][0][i] = (real) r0;
258                 fraction[ch][1][i] = (real) r1;
259                 fraction[ch][2][i] = (real) r2;
260             }
261             else {
262                 fraction[ch][0][i] = fraction[ch][1][i] = fraction[ch][2][i] = 0.0;
263             }
264         }
265         alloc1 += (1 << step);
266     }
267
268     for (i = jsbound; i < sblimit; i++) {
269         short   step = alloc1->bits;
270         unsigned char ba = si->allocation[i][0];
271         if (ba) {
272             struct al_table2 const *alloc2 = alloc1 + ba;
273             short   k = alloc2->bits;
274             short   d1 = alloc2->d;
275             assert( k <= 16 );
276             k = (k <= 16) ? k : 16;
277             if (d1 < 0) {
278                 int v0 = getbits(mp, k);
279                 int v1 = getbits(mp, k);
280                 int v2 = getbits(mp, k);
281                 for (ch = 0; ch < nch; ++ch) {
282                     unsigned char x1 = si->scalefactor[i][ch][gr];
283                     assert( x1 < 64 );
284                     x1 = (x1 < 64) ? x1 : 63;
285                     cm = muls[k][x1];
286                     r0 = (v0 + d1) * cm;
287                     r1 = (v1 + d1) * cm;
288                     r2 = (v2 + d1) * cm;
289                     fraction[ch][0][i] = (real) r0;
290                     fraction[ch][1][i] = (real) r1;
291                     fraction[ch][2][i] = (real) r2;
292                 }
293             }
294             else {
295                 unsigned int idx = getbits(mp, k);
296                 unsigned char *tab = grp_table_select(d1, idx);
297                 unsigned char k0 = tab[0];
298                 unsigned char k1 = tab[1];
299                 unsigned char k2 = tab[2];
300                 for (ch = 0; ch < nch; ++ch) {
301                     unsigned char x1 = si->scalefactor[i][ch][gr];
302                     assert( x1 < 64 );
303                     x1 = (x1 < 64) ? x1 : 63;
304                     r0 = muls[k0][x1];
305                     r1 = muls[k1][x1];
306                     r2 = muls[k2][x1];
307                     fraction[ch][0][i] = (real) r0;
308                     fraction[ch][1][i] = (real) r1;
309                     fraction[ch][2][i] = (real) r2;
310                 }
311             }
312         }
313         else {
314             fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = 0.0;
315             fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
316         }
317         alloc1 += (1 << step);
318     }
319     if (sblimit > fr->down_sample_sblimit) {
320         sblimit = fr->down_sample_sblimit; 
321     }
322     for (ch = 0; ch < nch; ++ch) {
323         for (i = sblimit; i < SBLIMIT; ++i) {
324             fraction[ch][0][i] = fraction[ch][1][i] = fraction[ch][2][i] = 0.0;
325         }
326     }
327 }
328
329 static void
330 II_select_table(struct frame *fr)
331 {
332     /* *INDENT-OFF* */
333   static const int translate[3][2][16] =
334    { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
335        { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
336      { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
337        { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
338      { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
339        { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
340     /* *INDENT-ON* */
341
342     int     table, sblim;
343     static const struct al_table2 *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 };
344     static const int sblims[5] = { 27, 30, 8, 12, 30 };
345
346     if (fr->lsf)
347         table = 4;
348     else
349         table = translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index];
350     sblim = sblims[table];
351
352     fr->alloc = (struct al_table2 const *) tables[table];
353     fr->II_sblimit = sblim;
354 }
355
356
357 int
358 decode_layer2_sideinfo(PMPSTR mp)
359 {
360     (void) mp;
361     /* FIXME: extract side information and check values */
362     return 0;
363 }
364
365 int
366 decode_layer2_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point)
367 {
368     real    fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
369     sideinfo_layer_II si;
370     struct frame *fr = &(mp->fr);
371     int     single = fr->single;
372     int     i, j, clip = 0;
373
374     II_select_table(fr);
375     II_step_one(mp, &si, fr);
376
377     if (fr->stereo == 1 || single == 3)
378         single = 0;
379
380     if (single >= 0) {
381         for (i = 0; i < SCALE_BLOCK; i++) {
382             II_step_two(mp, &si, fr, i >> 2, fraction);
383             for (j = 0; j < 3; j++) {
384                 clip += synth_1to1_mono(mp, fraction[single][j], pcm_sample, pcm_point);
385             }
386         }
387     }
388     else {
389         for (i = 0; i < SCALE_BLOCK; i++) {
390             II_step_two(mp, &si, fr, i >> 2, fraction);
391             for (j = 0; j < 3; j++) {
392                 int     p1 = *pcm_point;
393                 clip += synth_1to1(mp, fraction[0][j], 0, pcm_sample, &p1);
394                 clip += synth_1to1(mp, fraction[1][j], 1, pcm_sample, pcm_point);
395             }
396         }
397     }
398
399     return clip;
400 }