1 /*____________________________________________________________________________
2 
3 	FreeAmp - The Free MP3 Player
4 
5         MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
6         Corp.  http://www.xingtech.com
7 
8 	Portions Copyright (C) 1998-1999 EMusic.com
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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 
24 	$Id: iupL1.c,v 1.6 2000/10/13 14:29:02 ijr Exp $
25 ____________________________________________________________________________*/
26 
27 /****  iupL1.c  ***************************************************
28 
29 MPEG audio decoder Layer I mpeg1 and mpeg2
30 should be portable ANSI C, should be endian independent
31 
32 icupL1 integer version of cupL1.c
33 
34 ******************************************************************/
35 /*======================================================================*/
36 /* Read Only */
37 static int bat_bit_masterL1[] =
38 {
39    0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
40 
41 /*======================================================================*/
unpack_baL1(MPEGI * m)42 static void unpack_baL1(MPEGI *m)
43 {
44    int k;
45    int nstereo;
46    int n;
47 
48    m->iup.bit_skip = 0;
49    nstereo = m->iup.stereo_sb;
50    for (k = 0; k < m->iup.nbatL1; k++)
51    {
52       mac_load_check(4);
53       n = m->iup.ballo[k] = m->iup.samp_dispatch[k] = mac_load(4);
54       if (k >= m->iup.nsb_limit)
55 	 m->iup.bit_skip += bat_bit_masterL1[m->iup.samp_dispatch[k]];
56       m->iup.c_value[k] = m->iup.look_c_valueL1[n];
57       m->iup.c_shift[k] = m->iup.look_c_shiftL1[n];
58       if (--nstereo < 0)
59       {
60 	 m->iup.ballo[k + 1] = m->iup.ballo[k];
61 	 m->iup.samp_dispatch[k] += 15;	/* flag as joint */
62 	 m->iup.samp_dispatch[k + 1] = m->iup.samp_dispatch[k];	/* flag for sf */
63 	 m->iup.c_value[k + 1] = m->iup.c_value[k];
64 	 m->iup.c_shift[k + 1] = m->iup.c_shift[k];
65 	 k++;
66       }
67    }
68    m->iup.samp_dispatch[m->iup.nsb_limit] = 31;	/* terminate the dispatcher with skip */
69    m->iup.samp_dispatch[k] = 30;	/* terminate the dispatcher */
70 
71 }
72 /*-------------------------------------------------------------------------*/
unpack_sfL1(MPEGI * m)73 static void unpack_sfL1(MPEGI *m)	/* unpack scale factor */
74 {				/* combine dequant and scale factors */
75    int i, n;
76    INT32 tmp;			/* only reason tmp is 32 bit is to get 32 bit mult result */
77 
78    for (i = 0; i < m->iup.nbatL1; i++)
79    {
80       if (m->iup.ballo[i])
81       {
82 	 mac_load_check(6);
83 	 tmp = m->iup.c_value[i];
84 	 n = m->iup.c_shift[i];
85 	 m->iup.cs_factorL1[i] = (tmp * m->iup.sf_table[mac_load(6)]) >> n;
86       }
87    }
88 
89 /*-- done --*/
90 }
91 /*-------------------------------------------------------------------------*/
92 #define UNPACKL1_N(n)  s[k]     =  (m->iup.cs_factorL1[k]*(load(m,n)-((1 << (n-1)) -1)))>>(n-1);   \
93     goto dispatch;
94 #define UNPACKL1J_N(n) tmp        =  (load(m,n)-((1 << (n-1)) -1));                 \
95     s[k]       =  (m->iup.cs_factorL1[k]*tmp)>>(n-1);               \
96     s[k+1]     =  (m->iup.cs_factorL1[k+1]*tmp)>>(n-1);             \
97     k++;       /* skip right chan dispatch */                \
98     goto dispatch;
99 /*-------------------------------------------------------------------------*/
unpack_sampL1(MPEGI * m)100 static void unpack_sampL1(MPEGI *m)	/* unpack samples */
101 {
102    int j, k;
103    SAMPLEINT *s;
104    INT32 tmp;
105 
106    s = m->iup.sample;
107    for (j = 0; j < 12; j++)
108    {
109       k = -1;
110     dispatch:switch (m->iup.samp_dispatch[++k])
111       {
112 	 case 0:
113 	    s[k] = 0;
114 	    goto dispatch;
115 	 case 1:
116 	    UNPACKL1_N(2)	/*  3 levels */
117 	 case 2:
118 	    UNPACKL1_N(3)	/*  7 levels */
119 	 case 3:
120 	    UNPACKL1_N(4)	/* 15 levels */
121 	 case 4:
122 	    UNPACKL1_N(5)	/* 31 levels */
123 	 case 5:
124 	    UNPACKL1_N(6)	/* 63 levels */
125 	 case 6:
126 	    UNPACKL1_N(7)	/* 127 levels */
127 	 case 7:
128 	    UNPACKL1_N(8)	/* 255 levels */
129 	 case 8:
130 	    UNPACKL1_N(9)	/* 511 levels */
131 	 case 9:
132 	    UNPACKL1_N(10)	/* 1023 levels */
133 	 case 10:
134 	    UNPACKL1_N(11)	/* 2047 levels */
135 	 case 11:
136 	    UNPACKL1_N(12)	/* 4095 levels */
137 	 case 12:
138 	    UNPACKL1_N(13)	/* 8191 levels */
139 	 case 13:
140 	    UNPACKL1_N(14)	/* 16383 levels */
141 	 case 14:
142 	    UNPACKL1_N(15)	/* 32767 levels */
143 /* -- joint ---- */
144 	 case 15 + 0:
145 	    s[k + 1] = s[k] = 0;
146 	    k++;		/* skip right chan dispatch */
147 	    goto dispatch;
148 /* -- joint ---- */
149 	 case 15 + 1:
150 	    UNPACKL1J_N(2)	/*  3 levels */
151 	 case 15 + 2:
152 	    UNPACKL1J_N(3)	/*  7 levels */
153 	 case 15 + 3:
154 	    UNPACKL1J_N(4)	/* 15 levels */
155 	 case 15 + 4:
156 	    UNPACKL1J_N(5)	/* 31 levels */
157 	 case 15 + 5:
158 	    UNPACKL1J_N(6)	/* 63 levels */
159 	 case 15 + 6:
160 	    UNPACKL1J_N(7)	/* 127 levels */
161 	 case 15 + 7:
162 	    UNPACKL1J_N(8)	/* 255 levels */
163 	 case 15 + 8:
164 	    UNPACKL1J_N(9)	/* 511 levels */
165 	 case 15 + 9:
166 	    UNPACKL1J_N(10)	/* 1023 levels */
167 	 case 15 + 10:
168 	    UNPACKL1J_N(11)	/* 2047 levels */
169 	 case 15 + 11:
170 	    UNPACKL1J_N(12)	/* 4095 levels */
171 	 case 15 + 12:
172 	    UNPACKL1J_N(13)	/* 8191 levels */
173 	 case 15 + 13:
174 	    UNPACKL1J_N(14)	/* 16383 levels */
175 	 case 15 + 14:
176 	    UNPACKL1J_N(15)	/* 32767 levels */
177 
178 /* -- end of dispatch -- */
179 	 case 31:
180 	    skip(m, m->iup.bit_skip);
181 	 case 30:
182 	    s += 64;
183       }				/* end switch */
184    }				/* end j loop */
185 
186 /*-- done --*/
187 }
188 /*-------------------------------------------------------------------------*/
unpackL1(MPEGI * m)189 static void unpackL1(MPEGI *m)
190 {
191    int prot;
192 
193 /* at entry bit getter points at id, sync skipped by caller */
194 
195    load(m,3);			/* skip id and option (checked by init) */
196    prot = load(m,1);		/* load prot bit */
197    load(m,6);			/* skip to pad */
198    m->iup.pad = load(m,1) << 2;
199    load(m,1);			/* skip to mode */
200    m->iup.stereo_sb = look_joint[load(m,4)];
201    if (prot)
202       load(m,4);			/* skip to data */
203    else
204       load(m,20);			/* skip crc */
205 
206    unpack_baL1(m);		/* unpack bit allocation */
207    unpack_sfL1(m);		/* unpack scale factor */
208    unpack_sampL1(m);		/* unpack samples */
209 
210 
211 }
212 /*-------------------------------------------------------------------------*/
213 
214 #ifdef _MSC_VER
215 #pragma warning(disable: 4056)
216 #endif
217 
i_audio_decode_initL1(MPEGI * m,MPEG_HEAD * h,int framebytes_arg,int reduction_code,int transform_code,int convert_code,int freq_limit)218 int i_audio_decode_initL1(MPEGI *m, MPEG_HEAD * h, int framebytes_arg,
219 		   int reduction_code, int transform_code, int convert_code,
220 			  int freq_limit)
221 {
222    int i, k;
223    long samprate;
224    int limit;
225    int stepbit;
226    long step;
227    int bit_code;
228 
229 /*--- sf table built by Layer II init ---*/
230 
231    if (m->iup.first_pass_L1)
232    {
233       stepbit = 2;
234       step = 4;
235       for (i = 1; i < 16; i++)
236       {
237 	 m->iup.look_c_valueL1[i] = (int) (32768.0 * 2.0 / (step - 1));
238 	 m->iup.look_c_shiftL1[i] = 16 - stepbit;
239 	 stepbit++;
240 	 step <<= 1;
241       }
242       m->iup.first_pass_L1 = 0;
243    }
244 
245    m->iup.unpack_routine = unpackL1;
246 
247 
248    transform_code = transform_code;	/* not used, asm compatability */
249    bit_code = 0;
250    if (convert_code & 8)
251       bit_code = 1;
252    convert_code = convert_code & 3;	/* higher bits used by dec8 freq cvt */
253    if (reduction_code < 0)
254       reduction_code = 0;
255    if (reduction_code > 2)
256       reduction_code = 2;
257    if (freq_limit < 1000)
258       freq_limit = 1000;
259 
260 
261    m->iup.framebytes = framebytes_arg;
262 /* check if code handles */
263    if (h->option != 3)
264       return 0;			/* layer I only */
265 
266    m->iup.nbatL1 = 32;
267    m->iup.max_sb = m->iup.nbatL1;
268 /*----- compute nsb_limit --------*/
269    samprate = sr_table[4 * h->id + h->sr_index];
270    m->iup.nsb_limit = (freq_limit * 64L + samprate / 2) / samprate;
271 /*- caller limit -*/
272 /*---- limit = 0.94*(32>>reduction_code);  ----*/
273    limit = (32 >> reduction_code);
274    if (limit > 8)
275       limit--;
276    if (m->iup.nsb_limit > limit)
277       m->iup.nsb_limit = limit;
278    if (m->iup.nsb_limit > m->iup.max_sb)
279       m->iup.nsb_limit = m->iup.max_sb;
280 
281    m->iup.outvalues = 384 >> reduction_code;
282    if (h->mode != 3)
283    {				/* adjust for 2 channel modes */
284       m->iup.nbatL1 *= 2;
285       m->iup.max_sb *= 2;
286       m->iup.nsb_limit *= 2;
287    }
288 
289 /* set sbt function */
290    m->iup.nsbt = 12;
291    k = 1 + convert_code;
292    if (h->mode == 3)
293    {
294       k = 0;
295    }
296    m->iup.sbt = sbt_table[bit_code][reduction_code][k];
297    m->iup.outvalues *= out_chans[k];
298    if (bit_code != 0)
299       m->iup.outbytes = m->iup.outvalues;
300    else
301       m->iup.outbytes = sizeof(short) * m->iup.outvalues;
302 
303    m->iup.decinfo.channels = out_chans[k];
304    m->iup.decinfo.outvalues = m->iup.outvalues;
305    m->iup.decinfo.samprate = samprate >> reduction_code;
306    if (bit_code != 0)
307       m->iup.decinfo.bits = 8;
308    else
309       m->iup.decinfo.bits = sizeof(short) * 8;
310 
311    m->iup.decinfo.framebytes = m->iup.framebytes;
312    m->iup.decinfo.type = 0;
313 
314 
315 /* clear sample buffer, unused sub bands must be 0 */
316    for (i = 0; i < 768; i++)
317       m->iup.sample[i] = 0;
318 
319 
320 /* init sub-band transform */
321    i_sbt_init();
322 
323    return 1;
324 }
325 
326 #ifdef _MSC_VER
327 #pragma warning(default: 4056)
328 #endif
329 
330 /*---------------------------------------------------------*/
331