]> 4ch.mooo.com Git - 16.git/blob - src/lib/dl/ext/lame/layer1.c
cleaned up the repo from debugging watcom2 ^^
[16.git] / src / lib / dl / ext / lame / layer1.c
1 /* 
2  * layer1.c: Mpeg Layer-1 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
24 /* $Id: layer1.c,v 1.30 2010/03/22 14:30:19 robert Exp $ */
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #include <assert.h>
31 #include "common.h"
32 #include "decode_i386.h"
33
34 #ifdef WITH_DMALLOC
35 #include <dmalloc.h>
36 #endif
37
38 #include "layer1.h"
39
40 static int gd_are_hip_tables_layer1_initialized = 0;
41
42 void
43 hip_init_tables_layer1(void)
44 {
45     if (gd_are_hip_tables_layer1_initialized) {
46         return;
47     }
48     gd_are_hip_tables_layer1_initialized = 1;
49 }
50
51 typedef struct sideinfo_layer_I_struct
52 {
53     unsigned char allocation[SBLIMIT][2]; 
54     unsigned char scalefactor[SBLIMIT][2];
55 } sideinfo_layer_I;
56
57 static void
58 I_step_one(PMPSTR mp, sideinfo_layer_I* si)
59 {
60     struct frame *fr = &(mp->fr);
61     int     jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : 32;
62     int     i;
63     memset(si, 0, sizeof(*si));
64     assert(fr->stereo == 1 || fr->stereo == 2);
65
66     if (fr->stereo == 2) {
67         for (i = 0; i < jsbound; i++) {
68             unsigned char b0 = get_leq_8_bits(mp, 4);       /* values 0-15 */
69             unsigned char b1 = get_leq_8_bits(mp, 4);       /* values 0-15 */
70             si->allocation[i][0] = b0;
71             si->allocation[i][1] = b1;
72         }
73         for (i = jsbound; i < SBLIMIT; i++) {
74             unsigned char b = get_leq_8_bits(mp, 4);        /* values 0-15 */
75             si->allocation[i][0] = b;
76             si->allocation[i][1] = b;
77         }
78         for (i = 0; i < SBLIMIT; i++) {
79             unsigned char n0 = si->allocation[i][0];
80             unsigned char n1 = si->allocation[i][1];
81             unsigned char b0 = n0 ? get_leq_8_bits(mp, 6) : 0;  /* values 0-63 */
82             unsigned char b1 = n1 ? get_leq_8_bits(mp, 6) : 0;  /* values 0-63 */
83             si->scalefactor[i][0] = b0;
84             si->scalefactor[i][1] = b1;
85         }
86     }
87     else {
88         for (i = 0; i < SBLIMIT; i++) {
89             unsigned char b0 =  get_leq_8_bits(mp, 4);          /* values 0-15 */
90             si->allocation[i][0] = b0;
91         }
92         for (i = 0; i < SBLIMIT; i++) {
93             unsigned char n0 = si->allocation[i][0];
94             unsigned char b0 = n0 ? get_leq_8_bits(mp, 6) : 0;  /* values 0-63 */
95             si->scalefactor[i][0] = b0;
96         }
97     }
98 }
99
100 static void
101 I_step_two(PMPSTR mp, sideinfo_layer_I *si, real fraction[2][SBLIMIT])
102 {
103     double  r0, r1;
104     struct frame *fr = &(mp->fr);
105     int     ds_limit = fr->down_sample_sblimit;
106     int     i;
107
108     assert(fr->stereo == 1 || fr->stereo == 2);
109     if (fr->stereo == 2) {
110         int     jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : 32;
111         for (i = 0; i < jsbound; i++) {
112             unsigned char i0 = si->scalefactor[i][0];
113             unsigned char i1 = si->scalefactor[i][1];
114             unsigned char n0 = si->allocation[i][0];
115             unsigned char n1 = si->allocation[i][1];
116             assert( i0 < 64 );
117             assert( i1 < 64 );
118             assert( n0 < 16 );
119             assert( n1 < 16 );
120             if (n0 > 0) {
121                 unsigned short v = get_leq_16_bits(mp, n0 + 1); /* 0-65535 */
122                 r0 = (((-1) << n0) + v + 1) * muls[n0 + 1][i0];
123             }
124             else {
125                 r0 = 0;
126             }
127             if (n1 > 0) {
128                 unsigned short v = get_leq_16_bits(mp, n1 + 1); /* 0-65535 */
129                 r1 = (((-1) << n1) + v + 1) * muls[n1 + 1][i1];
130             }
131             else {
132                 r1 = 0;
133             }
134             fraction[0][i] = (real)r0;
135             fraction[1][i] = (real)r1;
136         }
137         for (i = jsbound; i < SBLIMIT; i++) {
138             unsigned char i0 = si->scalefactor[i][0];
139             unsigned char i1 = si->scalefactor[i][1];
140             unsigned char n = si->allocation[i][0];
141             assert( i0 < 64 );
142             assert( i1 < 64 );
143             assert( n < 16 );
144             if (n > 0) {
145                 unsigned short v = get_leq_16_bits(mp, n + 1); /* 0-65535 */
146                 unsigned int w = (((-1) << n) + v + 1);
147                 r0 = w * muls[n + 1][i0];
148                 r1 = w * muls[n + 1][i1];
149             }
150             else {
151                 r0 = r1 = 0;
152             }
153             fraction[0][i] = (real)r0;
154             fraction[1][i] = (real)r1;
155         }
156         for (i = ds_limit; i < SBLIMIT; i++) {
157             fraction[0][i] = 0.0;
158             fraction[1][i] = 0.0;
159         }
160     }
161     else {
162         for (i = 0; i < SBLIMIT; i++) {
163             unsigned char n = si->allocation[i][0];
164             unsigned char j = si->scalefactor[i][0];
165             assert( j < 64 );
166             assert( n < 16 );
167             if (n > 0) {
168                 unsigned short v = get_leq_16_bits(mp, n + 1);
169                 r0 = (((-1) << n) + v + 1) * muls[n + 1][j];
170             }
171             else {
172                 r0 = 0;
173             }
174             fraction[0][i] = (real)r0;
175         }
176         for (i = ds_limit; i < SBLIMIT; i++) {
177             fraction[0][i] = 0.0;
178         }
179     }
180 }
181
182 int
183 decode_layer1_sideinfo(PMPSTR mp)
184 {
185     (void) mp;
186     /* FIXME: extract side information and check values */
187     return 0;
188 }
189
190 int
191 decode_layer1_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point)
192 {
193     real    fraction[2][SBLIMIT]; /* FIXME: change real -> double ? */
194     sideinfo_layer_I si;
195     struct frame *fr = &(mp->fr);
196     int     single = fr->single;
197     int     i, clip = 0;
198
199     I_step_one(mp, &si);
200
201     if (fr->stereo == 1 || single == 3)
202         single = 0;
203
204     if (single >= 0) {
205         /* decoding one of possibly two channels */
206         for (i = 0; i < SCALE_BLOCK; i++) {
207             I_step_two(mp, &si, fraction);
208             clip += synth_1to1_mono(mp, (real *) fraction[single], pcm_sample, pcm_point);
209         }
210     }
211     else {
212         for (i = 0; i < SCALE_BLOCK; i++) {
213             int     p1 = *pcm_point;
214             I_step_two(mp, &si, fraction);
215             clip += synth_1to1(mp, (real *) fraction[0], 0, pcm_sample, &p1);
216             clip += synth_1to1(mp, (real *) fraction[1], 1, pcm_sample, pcm_point);
217         }
218     }
219
220     return clip;
221 }