1 #ifdef COMPILE_ME
2 /*____________________________________________________________________________
3
4 FreeAmp - The Free MP3 Player
5
6 MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
7 Corp. http://www.xingtech.com
8
9 Portions Copyright (C) 1998-1999 EMusic.com
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 $Id: cupini.c,v 1.3 1999/10/19 07:13:08 elrod Exp $
26 ____________________________________________________________________________*/
27
28 /*=========================================================
29 initialization for cup.c - include to cup.c
30 mpeg audio decoder portable "c"
31
32 mod 8/6/96 add 8 bit output
33
34 mod 5/10/95 add quick (low precision) window
35
36 mod 5/16/95 sb limit for reduced samprate output
37 changed from 94% to 100% of Nyquist sb
38
39 mod 11/15/95 for Layer I
40
41
42 =========================================================*/
43 /*-- compiler bug, floating constant overflow w/ansi --*/
44
45 static const long steps[18] =
46 {
47 0, 3, 5, 7, 9, 15, 31, 63, 127,
48 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535};
49
50
51 /* ABCD_INDEX = lookqt[mode][sr_index][br_index] */
52 /* -1 = invalid */
53 static const signed char lookqt[4][3][16] =
54 {
55 {{1, -1, -1, -1, 2, -1, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1}, /* 44ks stereo */
56 {0, -1, -1, -1, 2, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1}, /* 48ks */
57 {1, -1, -1, -1, 3, -1, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1}}, /* 32ks */
58 {{1, -1, -1, -1, 2, -1, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1}, /* 44ks joint stereo */
59 {0, -1, -1, -1, 2, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1}, /* 48ks */
60 {1, -1, -1, -1, 3, -1, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1}}, /* 32ks */
61 {{1, -1, -1, -1, 2, -1, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1}, /* 44ks dual chan */
62 {0, -1, -1, -1, 2, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1}, /* 48ks */
63 {1, -1, -1, -1, 3, -1, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1}}, /* 32ks */
64 // mono extended beyond legal br index
65 // 1,2,2,0,0,0,1,1,1,1,1,1,1,1,1,-1, /* 44ks single chan */
66 // 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,-1, /* 48ks */
67 // 1,3,3,0,0,0,1,1,1,1,1,1,1,1,1,-1, /* 32ks */
68 // legal mono
69 {{1, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1}, /* 44ks single chan */
70 {0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1}, /* 48ks */
71 {1, 3, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1}}, /* 32ks */
72 };
73
74 static const long sr_table[8] =
75 {22050L, 24000L, 16000L, 1L,
76 44100L, 48000L, 32000L, 1L};
77
78 /* bit allocation table look up */
79 /* table per mpeg spec tables 3b2a/b/c/d /e is mpeg2 */
80 /* look_bat[abcd_index][4][16] */
81 static const unsigned char look_bat[5][4][16] =
82 {
83 /* LOOK_BATA */
84 {{0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17},
85 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17},
86 {0, 1, 2, 3, 4, 5, 6, 17, 0, 0, 0, 0, 0, 0, 0, 0},
87 {0, 1, 2, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
88 /* LOOK_BATB */
89 {{0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17},
90 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17},
91 {0, 1, 2, 3, 4, 5, 6, 17, 0, 0, 0, 0, 0, 0, 0, 0},
92 {0, 1, 2, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
93 /* LOOK_BATC */
94 {{0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
95 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
96 {0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0},
97 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
98 /* LOOK_BATD */
99 {{0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
100 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
101 {0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0},
102 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
103 /* LOOK_BATE */
104 {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
105 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
106 {0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0},
107 {0, 1, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
108 };
109
110 /* look_nbat[abcd_index]][4] */
111 static const unsigned char look_nbat[5][4] =
112 {
113 {3, 8, 12, 4},
114 {3, 8, 12, 7},
115 {2, 0, 6, 0},
116 {2, 0, 10, 0},
117 {4, 0, 7, 19},
118 };
119
120
121 void sbt_mono(float *sample, short *pcm, int n);
122 void sbt_dual(float *sample, short *pcm, int n);
123 void sbt_dual_mono(float *sample, short *pcm, int n);
124 void sbt_dual_left(float *sample, short *pcm, int n);
125 void sbt_dual_right(float *sample, short *pcm, int n);
126 void sbt16_mono(float *sample, short *pcm, int n);
127 void sbt16_dual(float *sample, short *pcm, int n);
128 void sbt16_dual_mono(float *sample, short *pcm, int n);
129 void sbt16_dual_left(float *sample, short *pcm, int n);
130 void sbt16_dual_right(float *sample, short *pcm, int n);
131 void sbt8_mono(float *sample, short *pcm, int n);
132 void sbt8_dual(float *sample, short *pcm, int n);
133 void sbt8_dual_mono(float *sample, short *pcm, int n);
134 void sbt8_dual_left(float *sample, short *pcm, int n);
135 void sbt8_dual_right(float *sample, short *pcm, int n);
136
137 /*--- 8 bit output ---*/
138 void sbtB_mono(float *sample, unsigned char *pcm, int n);
139 void sbtB_dual(float *sample, unsigned char *pcm, int n);
140 void sbtB_dual_mono(float *sample, unsigned char *pcm, int n);
141 void sbtB_dual_left(float *sample, unsigned char *pcm, int n);
142 void sbtB_dual_right(float *sample, unsigned char *pcm, int n);
143 void sbtB16_mono(float *sample, unsigned char *pcm, int n);
144 void sbtB16_dual(float *sample, unsigned char *pcm, int n);
145 void sbtB16_dual_mono(float *sample, unsigned char *pcm, int n);
146 void sbtB16_dual_left(float *sample, unsigned char *pcm, int n);
147 void sbtB16_dual_right(float *sample, unsigned char *pcm, int n);
148 void sbtB8_mono(float *sample, unsigned char *pcm, int n);
149 void sbtB8_dual(float *sample, unsigned char *pcm, int n);
150 void sbtB8_dual_mono(float *sample, unsigned char *pcm, int n);
151 void sbtB8_dual_left(float *sample, unsigned char *pcm, int n);
152 void sbtB8_dual_right(float *sample, unsigned char *pcm, int n);
153
154
155 static const SBT_FUNCTION sbt_table[2][3][5] =
156 {
157 {{sbt_mono, sbt_dual, sbt_dual_mono, sbt_dual_left, sbt_dual_right},
158 {sbt16_mono, sbt16_dual, sbt16_dual_mono, sbt16_dual_left, sbt16_dual_right},
159 {sbt8_mono, sbt8_dual, sbt8_dual_mono, sbt8_dual_left, sbt8_dual_right}},
160 {{(SBT_FUNCTION) sbtB_mono,
161 (SBT_FUNCTION) sbtB_dual,
162 (SBT_FUNCTION) sbtB_dual_mono,
163 (SBT_FUNCTION) sbtB_dual_left,
164 (SBT_FUNCTION) sbtB_dual_right},
165 {(SBT_FUNCTION) sbtB16_mono,
166 (SBT_FUNCTION) sbtB16_dual,
167 (SBT_FUNCTION) sbtB16_dual_mono,
168 (SBT_FUNCTION) sbtB16_dual_left,
169 (SBT_FUNCTION) sbtB16_dual_right},
170 {(SBT_FUNCTION) sbtB8_mono,
171 (SBT_FUNCTION) sbtB8_dual,
172 (SBT_FUNCTION) sbtB8_dual_mono,
173 (SBT_FUNCTION) sbtB8_dual_left,
174 (SBT_FUNCTION) sbtB8_dual_right}},
175 };
176
177 static const int out_chans[5] =
178 {1, 2, 1, 1, 1};
179
180
181 int audio_decode_initL1(MPEG_HEAD * h, int framebytes_arg,
182 int reduction_code, int transform_code, int convert_code,
183 int freq_limit);
184 void sbt_init();
185
186
187 IN_OUT L1audio_decode(unsigned char *bs, signed short *pcm);
188 IN_OUT L2audio_decode(unsigned char *bs, signed short *pcm);
189 IN_OUT L3audio_decode(unsigned char *bs, unsigned char *pcm);
190 static const AUDIO_DECODE_ROUTINE decode_routine_table[4] =
191 {
192 L2audio_decode,
193 (AUDIO_DECODE_ROUTINE)L3audio_decode,
194 L2audio_decode,
195 L1audio_decode,};
196
197 /*---------------------------------------------------------*/
table_init()198 static void table_init()
199 {
200 int i, j;
201 int code;
202 static int iOnceOnly=0;
203
204 if (!iOnceOnly++)
205 {
206 /*-- c_values (dequant) --*/
207 for (i = 1; i < 18; i++)
208 look_c_value[i] = 2.0F / steps[i];
209
210 /*-- scale factor table, scale by 32768 for 16 pcm output --*/
211 for (i = 0; i < 64; i++)
212 sf_table[i] = (float) (32768.0 * 2.0 * pow(2.0, -i / 3.0));
213
214 /*-- grouped 3 level lookup table 5 bit token --*/
215 for (i = 0; i < 32; i++)
216 {
217 code = i;
218 for (j = 0; j < 3; j++)
219 {
220 group3_table[i][j] = (char) ((code % 3) - 1);
221 code /= 3;
222 }
223 }
224
225 /*-- grouped 5 level lookup table 7 bit token --*/
226 for (i = 0; i < 128; i++)
227 {
228 code = i;
229 for (j = 0; j < 3; j++)
230 {
231 group5_table[i][j] = (char) ((code % 5) - 2);
232 code /= 5;
233 }
234 }
235
236 /*-- grouped 9 level lookup table 10 bit token --*/
237 for (i = 0; i < 1024; i++)
238 {
239 code = i;
240 for (j = 0; j < 3; j++)
241 {
242 group9_table[i][j] = (short) ((code % 9) - 4);
243 code /= 9;
244 }
245 }
246 }
247 }
248 /*---------------------------------------------------------*/
249 int L1audio_decode_init(MPEG_HEAD * h, int framebytes_arg,
250 int reduction_code, int transform_code, int convert_code,
251 int freq_limit);
252 int L3audio_decode_init(MPEG_HEAD * h, int framebytes_arg,
253 int reduction_code, int transform_code, int convert_code,
254 int freq_limit);
255
256 /*---------------------------------------------------------*/
257 /* mpeg_head defined in mhead.h frame bytes is without pad */
audio_decode_init(MPEG_HEAD * h,int framebytes_arg,int reduction_code,int transform_code,int convert_code,int freq_limit)258 int audio_decode_init(MPEG_HEAD * h, int framebytes_arg,
259 int reduction_code, int transform_code, int convert_code,
260 int freq_limit)
261 {
262 int i, j, k;
263 static int first_pass = 1;
264 int abcd_index;
265 long samprate;
266 int limit;
267 int bit_code;
268
269 if (first_pass)
270 {
271 table_init();
272 first_pass = 0;
273 }
274
275 /* select decoder routine Layer I,II,III */
276 audio_decode_routine = decode_routine_table[h->option & 3];
277
278
279 if (h->option == 3) /* layer I */
280 return L1audio_decode_init(h, framebytes_arg,
281 reduction_code, transform_code, convert_code, freq_limit);
282
283 if (h->option == 1) /* layer III */
284 return L3audio_decode_init(h, framebytes_arg,
285 reduction_code, transform_code, convert_code, freq_limit);
286
287
288
289 bit_code = 0;
290 if (convert_code & 8)
291 bit_code = 1;
292 convert_code = convert_code & 3; /* higher bits used by dec8 freq cvt */
293 if (reduction_code < 0)
294 reduction_code = 0;
295 if (reduction_code > 2)
296 reduction_code = 2;
297 if (freq_limit < 1000)
298 freq_limit = 1000;
299
300
301 pMP3Stream->framebytes = framebytes_arg;
302 /* check if code handles */
303 if (h->option != 2)
304 return 0; /* layer II only */
305 if (h->sr_index == 3)
306 return 0; /* reserved */
307
308 /* compute abcd index for bit allo table selection */
309 if (h->id) /* mpeg 1 */
310 abcd_index = lookqt[h->mode][h->sr_index][h->br_index];
311 else
312 abcd_index = 4; /* mpeg 2 */
313
314 if (abcd_index < 0)
315 return 0; // fail invalid Layer II bit rate index
316
317 for (i = 0; i < 4; i++)
318 for (j = 0; j < 16; j++)
319 pMP3Stream->bat[i][j] = look_bat[abcd_index][i][j];
320 for (i = 0; i < 4; i++)
321 pMP3Stream->nbat[i] = look_nbat[abcd_index][i];
322 pMP3Stream->max_sb = pMP3Stream->nbat[0] + pMP3Stream->nbat[1] + pMP3Stream->nbat[2] + pMP3Stream->nbat[3];
323 /*----- compute pMP3Stream->nsb_limit --------*/
324 samprate = sr_table[4 * h->id + h->sr_index];
325 pMP3Stream->nsb_limit = (freq_limit * 64L + samprate / 2) / samprate;
326 /*- caller limit -*/
327 /*---- limit = 0.94*(32>>reduction_code); ----*/
328 limit = (32 >> reduction_code);
329 if (limit > 8)
330 limit--;
331 if (pMP3Stream->nsb_limit > limit)
332 pMP3Stream->nsb_limit = limit;
333 if (pMP3Stream->nsb_limit > pMP3Stream->max_sb)
334 pMP3Stream->nsb_limit = pMP3Stream->max_sb;
335
336 pMP3Stream->outvalues = 1152 >> reduction_code;
337 if (h->mode != 3)
338 { /* adjust for 2 channel modes */
339 for (i = 0; i < 4; i++)
340 pMP3Stream->nbat[i] *= 2;
341 pMP3Stream->max_sb *= 2;
342 pMP3Stream->nsb_limit *= 2;
343 }
344
345 /* set sbt function */
346 k = 1 + convert_code;
347 if (h->mode == 3)
348 {
349 k = 0;
350 }
351 pMP3Stream->sbt = sbt_table[bit_code][reduction_code][k];
352 pMP3Stream->outvalues *= out_chans[k];
353 if (bit_code)
354 pMP3Stream->outbytes = pMP3Stream->outvalues;
355 else
356 pMP3Stream->outbytes = sizeof(short) * pMP3Stream->outvalues;
357
358 decinfo.channels = out_chans[k];
359 decinfo.outvalues = pMP3Stream->outvalues;
360 decinfo.samprate = samprate >> reduction_code;
361 if (bit_code)
362 decinfo.bits = 8;
363 else
364 decinfo.bits = sizeof(short) * 8;
365
366 decinfo.framebytes = pMP3Stream->framebytes;
367 decinfo.type = 0;
368
369
370
371 /* clear sample buffer, unused sub bands must be 0 */
372 for (i = 0; i < 2304*2; i++) // the *2 here was inserted by me just in case, since the array is now *2, because of stereo files unpacking at 4608 bytes per frame (which may or may not be relevant, but in any case I don't think we use the L1 versions of MP3 now anyway
373 sample[i] = 0.0F;
374
375
376 /* init sub-band transform */
377 sbt_init();
378
379 return 1;
380 }
381 /*---------------------------------------------------------*/
audio_decode_info(DEC_INFO * info)382 void audio_decode_info(DEC_INFO * info)
383 {
384 *info = decinfo; /* info return, call after init */
385 }
386 /*---------------------------------------------------------*/
decode_table_init()387 void decode_table_init()
388 {
389 /* dummy for asm version compatability */
390 }
391 /*---------------------------------------------------------*/
392 #endif // #ifdef COMPILE_ME
393
394