aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/mp3/src/iupini.c
blob: 800103b5674b137e28b010392259c8d333e6c8b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*____________________________________________________________________________
	
	FreeAmp - The Free MP3 Player

        MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
        Corp.  http://www.xingtech.com

	Portions Copyright (C) 1998-1999 EMusic.com

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
	
	$Id$
____________________________________________________________________________*/

/*=========================================================
 initialization for iup.c - include to iup.c
 mpeg audio decoder portable "c"      integer


mods 11/15/95 for Layer I

mods 1/8/97 warnings

=========================================================*/
#include <limits.h>
#include <string.h>

/* Read only */
static long steps[18] =
{
   0, 3, 5, 7, 9, 15, 31, 63, 127,
   255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535};

/* Read only */
static int stepbits[18] =
{
   0, 2, 3, 3, 4, 4, 5, 6, 7,
   8, 9, 10, 11, 12, 13, 14, 15, 16};

/* ABCD_INDEX = lookqt[mode][sr_index][br_index]  */
/* -1 = invalid  */
/* Read only */
static signed char lookqt[4][3][16] =
{
 {{1, -1, -1, -1, 2, -1, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1},	/*  44ks stereo */
  {0, -1, -1, -1, 2, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1},	/*  48ks */
  {1, -1, -1, -1, 3, -1, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1}},	/*  32ks */
 {{1, -1, -1, -1, 2, -1, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1},	/*  44ks joint stereo */
  {0, -1, -1, -1, 2, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1},	/*  48ks */
  {1, -1, -1, -1, 3, -1, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1}},	/*  32ks */
 {{1, -1, -1, -1, 2, -1, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1},	/*  44ks dual chan */
  {0, -1, -1, -1, 2, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1},	/*  48ks */
  {1, -1, -1, -1, 3, -1, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1}},	/*  32ks */
 {{1, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1},	/*  44ks single chan */
  {0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1},	/*  48ks */
  {1, 3, 3, 0, 0, 0, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1}},	/*  32ks */
};

/* Read only */
static long sr_table[8] =
{22050L, 24000L, 16000L, 1L,
 44100L, 48000L, 32000L, 1L};

/* bit allocation table look up */
/* table per mpeg spec tables 3b2a/b/c/d  /e is mpeg2 */
/* look_bat[abcd_index][4][16]  */
/* Read only */
static unsigned char look_bat[5][4][16] =
{
/* LOOK_BATA */
 {{0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17},
  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17},
  {0, 1, 2, 3, 4, 5, 6, 17, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 1, 2, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
/* LOOK_BATB */
 {{0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17},
  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17},
  {0, 1, 2, 3, 4, 5, 6, 17, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 1, 2, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
/* LOOK_BATC */
 {{0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
/* LOOK_BATD */
 {{0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
/* LOOK_BATE */
 {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 1, 2, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 1, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
};

/* look_nbat[abcd_index]][4] */
/* Read only */
static unsigned char look_nbat[5][4] =
{
  {3, 8, 12, 4},
  {3, 8, 12, 7},
  {2, 0, 6, 0},
  {2, 0, 10, 0},
  {4, 0, 7, 19},
};


void i_sbt_mono(SAMPLEINT * sample, short *pcm, int n);
void i_sbt_dual(SAMPLEINT * sample, short *pcm, int n);
void i_sbt_dual_mono(SAMPLEINT * sample, short *pcm, int n);
void i_sbt_dual_left(SAMPLEINT * sample, short *pcm, int n);
void i_sbt_dual_right(SAMPLEINT * sample, short *pcm, int n);
void i_sbt16_mono(SAMPLEINT * sample, short *pcm, int n);
void i_sbt16_dual(SAMPLEINT * sample, short *pcm, int n);
void i_sbt16_dual_mono(SAMPLEINT * sample, short *pcm, int n);
void i_sbt16_dual_left(SAMPLEINT * sample, short *pcm, int n);
void i_sbt16_dual_right(SAMPLEINT * sample, short *pcm, int n);
void i_sbt8_mono(SAMPLEINT * sample, short *pcm, int n);
void i_sbt8_dual(SAMPLEINT * sample, short *pcm, int n);
void i_sbt8_dual_mono(SAMPLEINT * sample, short *pcm, int n);
void i_sbt8_dual_left(SAMPLEINT * sample, short *pcm, int n);
void i_sbt8_dual_right(SAMPLEINT * sample, short *pcm, int n);

/*--- 8 bit output ---*/
void i_sbtB_mono(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB_dual(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB_dual_mono(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB_dual_left(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB_dual_right(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB16_mono(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB16_dual(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB16_dual_mono(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB16_dual_left(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB16_dual_right(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB8_mono(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB8_dual(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB8_dual_mono(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB8_dual_left(SAMPLEINT * sample, unsigned char *pcm, int n);
void i_sbtB8_dual_right(SAMPLEINT * sample, unsigned char *pcm, int n);


/* Read Only */
static SBT_FUNCTION sbt_table[2][3][5] =
{
 {{i_sbt_mono, i_sbt_dual, i_sbt_dual_mono, i_sbt_dual_left, i_sbt_dual_right},
  {i_sbt16_mono, i_sbt16_dual, i_sbt16_dual_mono, i_sbt16_dual_left, i_sbt16_dual_right},
  {i_sbt8_mono, i_sbt8_dual, i_sbt8_dual_mono, i_sbt8_dual_left, i_sbt8_dual_right}},

 {{(SBT_FUNCTION) i_sbtB_mono,
   (SBT_FUNCTION) i_sbtB_dual,
   (SBT_FUNCTION) i_sbtB_dual_mono,
   (SBT_FUNCTION) i_sbtB_dual_left,
   (SBT_FUNCTION) i_sbtB_dual_right},
  {(SBT_FUNCTION) i_sbtB16_mono,
   (SBT_FUNCTION) i_sbtB16_dual,
   (SBT_FUNCTION) i_sbtB16_dual_mono,
   (SBT_FUNCTION) i_sbtB16_dual_left,
   (SBT_FUNCTION) i_sbtB16_dual_right},
  {(SBT_FUNCTION) i_sbtB8_mono,
   (SBT_FUNCTION) i_sbtB8_dual,
   (SBT_FUNCTION) i_sbtB8_dual_mono,
   (SBT_FUNCTION) i_sbtB8_dual_left,
   (SBT_FUNCTION) i_sbtB8_dual_right}},
};
/* Read Only */
static int out_chans[5] =
{1, 2, 1, 1, 1};

/*---------------------------------------------------------*/

#ifdef _MSC_VER
#pragma warning(disable: 4056)
#endif

void i_mpeg_init(MPEGI *m)
{
	memset(&m->iup, 0, sizeof(m->iup));
	m->iup.nsb_limit = 6;
	m->iup.nbat[0] = 3;
	m->iup.nbat[1] = 8;
	m->iup.nbat[3] = 12;
	m->iup.nbat[4] = 7;
	m->iup.nsbt = 36;
	m->iup.sbt = i_sbt_mono;
	m->iup.unpack_routine = unpack;
	m->iup.first_pass = 1;
	m->iup.first_pass_L1 = 1;
	m->iup.nbatL1 = 32;
	m->iup.cs_factorL1 = m->iup.cs_factor[0];
}

static void table_init(MPEGI *m)
{
   int i, j;
   int code;
   int bits;
   long tmp, sfmax;

/*--  c_values (dequant) --*/
   for (i = 1; i < 18; i++)
      m->iup.look_c_value[i] = (int) (32768.0 * 2.0 / steps[i]);
   for (i = 1; i < 18; i++)
      m->iup.look_c_shift[i] = 16 - stepbits[i];

/*--  scale factor table, scale by 32768 for 16 pcm output  --*/
   bits = min(8 * sizeof(SAMPLEINT), 8 * sizeof(m->iup.sf_table[0]));
   tmp = 1L << (bits - 2);
   sfmax = tmp + (tmp - 1);
   for (i = 0; i < 64; i++)
   {
      tmp = (long) (32768.0 * 2.0 * pow(2.0, -i / 3.0));
      if (tmp > sfmax)
	 tmp = sfmax;
      m->iup.sf_table[i] = tmp;
   }
/*--  grouped 3 level lookup table 5 bit token --*/
   for (i = 0; i < 32; i++)
   {
      code = i;
      for (j = 0; j < 3; j++)
      {
	 m->iup.group3_table[i][j] = (char) ((code % 3) - 1);
	 code /= 3;
      }
   }
/*--  grouped 5 level lookup table 7 bit token --*/
   for (i = 0; i < 128; i++)
   {
      code = i;
      for (j = 0; j < 3; j++)
      {
	 m->iup.group5_table[i][j] = (char) ((code % 5) - 2);
	 code /= 5;
      }
   }
/*--  grouped 9 level lookup table 10 bit token --*/
   for (i = 0; i < 1024; i++)
   {
      code = i;
      for (j = 0; j < 3; j++)
      {
	 m->iup.group9_table[i][j] = (short) ((code % 9) - 4);
	 code /= 9;
      }
   }


}

#ifdef _MSC_VER
#pragma warning(default: 4056)
#endif

/*---------------------------------------------------------*/
int i_audio_decode_initL1(MPEGI *m, MPEG_HEAD * h, int framebytes_arg,
		   int reduction_code, int transform_code, int convert_code,
			  int freq_limit);
void i_sbt_init();

/*---------------------------------------------------------*/
/* mpeg_head defined in mhead.h  frame bytes is without pad */
int i_audio_decode_init(MPEGI *m, MPEG_HEAD * h, int framebytes_arg,
		   int reduction_code, int transform_code, int convert_code,
			int freq_limit)
{
   int i, j, k;
   int abcd_index;
   long samprate;
   int limit;
   int bit_code;

   if (m->iup.first_pass)
   {
      table_init(m);
      m->iup.first_pass = 0;
   }

/* check if code handles */
   if (h->option == 3)		/* layer I */
      return i_audio_decode_initL1(m, h, framebytes_arg,
		  reduction_code, transform_code, convert_code, freq_limit);
   if (h->option != 2)
      return 0;			/* layer II only */


   m->iup.unpack_routine = unpack;

   transform_code = transform_code;	/* not used, asm compatability */
   bit_code = 0;
   if (convert_code & 8)
      bit_code = 1;
   convert_code = convert_code & 3;	/* higher bits used by dec8 freq cvt */
   if (reduction_code < 0)
      reduction_code = 0;
   if (reduction_code > 2)
      reduction_code = 2;
   if (freq_limit < 1000)
      freq_limit = 1000;


   m->iup.framebytes = framebytes_arg;

/* compute abcd index for bit allo table selection */
   if (h->id)			/* mpeg 1 */
      abcd_index = lookqt[h->mode][h->sr_index][h->br_index];
   else
      abcd_index = 4;		/* mpeg 2 */
   for (i = 0; i < 4; i++)
      for (j = 0; j < 16; j++)
	 m->iup.bat[i][j] = look_bat[abcd_index][i][j];
   for (i = 0; i < 4; i++)
      m->iup.nbat[i] = look_nbat[abcd_index][i];
   m->iup.max_sb = m->iup.nbat[0] + m->iup.nbat[1] + m->iup.nbat[2] + m->iup.nbat[3];
/*----- compute nsb_limit --------*/
   samprate = sr_table[4 * h->id + h->sr_index];
   m->iup.nsb_limit = (freq_limit * 64L + samprate / 2) / samprate;
/*- caller limit -*/
/*---- limit = 0.94*(32>>reduction_code);  ----*/
   limit = (32 >> reduction_code);
   if (limit > 8)
      limit--;
   if (m->iup.nsb_limit > limit)
      m->iup.nsb_limit = limit;
   if (m->iup.nsb_limit > m->iup.max_sb)
      m->iup.nsb_limit = m->iup.max_sb;

   m->iup.outvalues = 1152 >> reduction_code;
   if (h->mode != 3)
   {				/* adjust for 2 channel modes */
      for (i = 0; i < 4; i++)
	  m->iup.nbat[i] *= 2;
      m->iup.max_sb *= 2;
      m->iup.nsb_limit *= 2;
   }

/* set sbt function */
   m->iup.nsbt = 36;
   k = 1 + convert_code;
   if (h->mode == 3)
   {
      k = 0;
   }
   m->iup.sbt = sbt_table[bit_code][reduction_code][k];
   m->iup.outvalues *= out_chans[k];
   if (bit_code != 0)
      m->iup.outbytes = m->iup.outvalues;
   else
      m->iup.outbytes = sizeof(short) * m->iup.outvalues;

   m->iup.decinfo.channels = out_chans[k];
   m->iup.decinfo.outvalues = m->iup.outvalues;
   m->iup.decinfo.samprate = samprate >> reduction_code;
   if (bit_code != 0)
      m->iup.decinfo.bits = 8;
   else
      m->iup.decinfo.bits = sizeof(short) * 8;

   m->iup.decinfo.framebytes = m->iup.framebytes;
   m->iup.decinfo.type = 0;


/* clear sample buffer, unused sub bands must be 0 */
   for (i = 0; i < 2304; i++)
      m->iup.sample[i] = 0;


/* init sub-band transform */
   i_sbt_init();

   return 1;
}
/*---------------------------------------------------------*/
void i_audio_decode_info(MPEGI *m, DEC_INFO * info)
{
   *info = m->iup.decinfo;		/* info return, call after init */
}
/*---------------------------------------------------------*/