aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/mp3/src/uph.c
blob: c3cab0a200a3d2cf1db351bf3bf3cba69d7b1b9b (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*____________________________________________________________________________
	
	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$
____________________________________________________________________________*/

/****  uph.c  ***************************************************

Layer 3 audio
 huffman decode


******************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <math.h>

#include "L3.h"


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

/*===============================================================*/

/* max bits required for any lookup - change if htable changes */
/* quad required 10 bit w/signs  must have (MAXBITS+2) >= 10   */
#define MAXBITS 9

static HUFF_ELEMENT huff_table_0[4] =
{{0}, {0}, {0}, {64}};			/* dummy must not use */

#include "htable.h"

/*-- 6 bit lookup (purgebits, value) --*/
static unsigned char quad_table_a[][2] =
{
  {6, 11}, {6, 15}, {6, 13}, {6, 14}, {6, 7}, {6, 5}, {5, 9},
  {5, 9}, {5, 6}, {5, 6}, {5, 3}, {5, 3}, {5, 10}, {5, 10},
  {5, 12}, {5, 12}, {4, 2}, {4, 2}, {4, 2}, {4, 2}, {4, 1},
  {4, 1}, {4, 1}, {4, 1}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
  {4, 8}, {4, 8}, {4, 8}, {4, 8}, {1, 0}, {1, 0}, {1, 0},
  {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
  {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
  {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
  {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
  {1, 0},
};


typedef struct
{
   HUFF_ELEMENT *table;
   int linbits;
   int ncase;
}
HUFF_SETUP;

#define no_bits       0
#define one_shot      1
#define no_linbits    2
#define have_linbits  3
#define quad_a        4
#define quad_b        5


static HUFF_SETUP table_look[] =
{
  {huff_table_0, 0, no_bits},
  {huff_table_1, 0, one_shot},
  {huff_table_2, 0, one_shot},
  {huff_table_3, 0, one_shot},
  {huff_table_0, 0, no_bits},
  {huff_table_5, 0, one_shot},
  {huff_table_6, 0, one_shot},
  {huff_table_7, 0, no_linbits},
  {huff_table_8, 0, no_linbits},
  {huff_table_9, 0, no_linbits},
  {huff_table_10, 0, no_linbits},
  {huff_table_11, 0, no_linbits},
  {huff_table_12, 0, no_linbits},
  {huff_table_13, 0, no_linbits},
  {huff_table_0, 0, no_bits},
  {huff_table_15, 0, no_linbits},
  {huff_table_16, 1, have_linbits},
  {huff_table_16, 2, have_linbits},
  {huff_table_16, 3, have_linbits},
  {huff_table_16, 4, have_linbits},
  {huff_table_16, 6, have_linbits},
  {huff_table_16, 8, have_linbits},
  {huff_table_16, 10, have_linbits},
  {huff_table_16, 13, have_linbits},
  {huff_table_24, 4, have_linbits},
  {huff_table_24, 5, have_linbits},
  {huff_table_24, 6, have_linbits},
  {huff_table_24, 7, have_linbits},
  {huff_table_24, 8, have_linbits},
  {huff_table_24, 9, have_linbits},
  {huff_table_24, 11, have_linbits},
  {huff_table_24, 13, have_linbits},
  {huff_table_0, 0, quad_a},
  {huff_table_0, 0, quad_b},
};

/*========================================================*/
extern BITDAT bitdat;

/*------------- get n bits from bitstream -------------*/
/* unused
static unsigned int bitget(int n)
{
   unsigned int x;

   if (bitdat.bits < n)
   {			*/	/* refill bit buf if necessary */
/*      while (bitdat.bits <= 24)
      {
	 bitdat.bitbuf = (bitdat.bitbuf << 8) | *bitdat.bs_ptr++;
	 bitdat.bits += 8;
      }
   }
   bitdat.bits -= n;
   x = bitdat.bitbuf >> bitdat.bits;
   bitdat.bitbuf -= x << bitdat.bits;
   return x;
}
*/
/*----- get n bits  - checks for n+2 avail bits (linbits+sign) -----*/
static unsigned int bitget_lb(int n)
{
   unsigned int x;

   if (bitdat.bits < (n + 2))
   {				/* refill bit buf if necessary */
      while (bitdat.bits <= 24)
      {
	 bitdat.bitbuf = (bitdat.bitbuf << 8) | *bitdat.bs_ptr++;
	 bitdat.bits += 8;
      }
   }
   bitdat.bits -= n;
   x = bitdat.bitbuf >> bitdat.bits;
   bitdat.bitbuf -= x << bitdat.bits;
   return x;
}




/*------------- get n bits but DO NOT remove from bitstream --*/
static unsigned int bitget2(int n)
{
   unsigned int x;

   if (bitdat.bits < (MAXBITS + 2))
   {				/* refill bit buf if necessary */
      while (bitdat.bits <= 24)
      {
	 bitdat.bitbuf = (bitdat.bitbuf << 8) | *bitdat.bs_ptr++;
	 bitdat.bits += 8;
      }
   }
   x = bitdat.bitbuf >> (bitdat.bits - n);
   return x;
}
/*------------- remove n bits from bitstream ---------*/
/* unused
static void bitget_purge(int n)
{
   bitdat.bits -= n;
   bitdat.bitbuf -= (bitdat.bitbuf >> bitdat.bits) << bitdat.bits;
}
*/
/*------------- get 1 bit from bitstream NO CHECK -------------*/
/* unused
static unsigned int bitget_1bit()
{
   unsigned int x;

   bitdat.bits--;
   x = bitdat.bitbuf >> bitdat.bits;
   bitdat.bitbuf -= x << bitdat.bits;
   return x;
}
*/
/*========================================================*/
/*========================================================*/
#define mac_bitget_check(n) if( bitdat.bits < (n) ) {                   \
    while( bitdat.bits <= 24 ) {            \
        bitdat.bitbuf = (bitdat.bitbuf << 8) | *bitdat.bs_ptr++; \
        bitdat.bits += 8;                   \
    }                                       \
}
/*---------------------------------------------------------*/
#define mac_bitget2(n)  (bitdat.bitbuf >> (bitdat.bits-n));
/*---------------------------------------------------------*/
#define mac_bitget(n) ( bitdat.bits -= n,           \
         code  = bitdat.bitbuf >> bitdat.bits,     \
         bitdat.bitbuf -= code << bitdat.bits,     \
         code )
/*---------------------------------------------------------*/
#define mac_bitget_purge(n) bitdat.bits -= n,                    \
    bitdat.bitbuf -= (bitdat.bitbuf >> bitdat.bits) << bitdat.bits;
/*---------------------------------------------------------*/
#define mac_bitget_1bit() ( bitdat.bits--,                           \
         code  = bitdat.bitbuf >> bitdat.bits,    \
         bitdat.bitbuf -= code << bitdat.bits,  \
         code )
/*========================================================*/
/*========================================================*/
void unpack_huff(int xy[][2], int n, int ntable)
{
   int i;
   HUFF_ELEMENT *t;
   HUFF_ELEMENT *t0;
   int linbits;
   int bits;
   int code;
   int x, y;

   if (n <= 0)
      return;
   n = n >> 1;			/* huff in pairs */
/*-------------*/
   t0 = table_look[ntable].table;
   linbits = table_look[ntable].linbits;
   switch (table_look[ntable].ncase)
   {
      default:
/*------------------------------------------*/
      case no_bits:
/*- table 0, no data, x=y=0--*/
	 for (i = 0; i < n; i++)
	 {
	    xy[i][0] = 0;
	    xy[i][1] = 0;
	 }
	 return;
/*------------------------------------------*/
      case one_shot:
/*- single lookup, no escapes -*/
	 for (i = 0; i < n; i++)
	 {
	    mac_bitget_check((MAXBITS + 2));
	    bits = t0[0].b.signbits;
	    code = mac_bitget2(bits);
	    mac_bitget_purge(t0[1 + code].b.purgebits);
	    x = t0[1 + code].b.x;
	    y = t0[1 + code].b.y;
	    if (x)
	       if (mac_bitget_1bit())
		  x = -x;
	    if (y)
	       if (mac_bitget_1bit())
		  y = -y;
	    xy[i][0] = x;
	    xy[i][1] = y;
	    if (bitdat.bs_ptr > bitdat.bs_ptr_end)
	       break;		// bad data protect

	 }
	 return;
/*------------------------------------------*/
      case no_linbits:
	 for (i = 0; i < n; i++)
	 {
	    t = t0;
	    for (;;)
	    {
	       mac_bitget_check((MAXBITS + 2));
	       bits = t[0].b.signbits;
	       code = mac_bitget2(bits);
	       if (t[1 + code].b.purgebits)
		  break;
	       t += t[1 + code].ptr;	/* ptr include 1+code */
	       mac_bitget_purge(bits);
	    }
	    mac_bitget_purge(t[1 + code].b.purgebits);
	    x = t[1 + code].b.x;
	    y = t[1 + code].b.y;
	    if (x)
	       if (mac_bitget_1bit())
		  x = -x;
	    if (y)
	       if (mac_bitget_1bit())
		  y = -y;
	    xy[i][0] = x;
	    xy[i][1] = y;
	    if (bitdat.bs_ptr > bitdat.bs_ptr_end)
	       break;		// bad data protect

	 }
	 return;
/*------------------------------------------*/
      case have_linbits:
	 for (i = 0; i < n; i++)
	 {
	    t = t0;
	    for (;;)
	    {
	       bits = t[0].b.signbits;
	       code = bitget2(bits);
	       if (t[1 + code].b.purgebits)
		  break;
	       t += t[1 + code].ptr;	/* ptr includes 1+code */
	       mac_bitget_purge(bits);
	    }
	    mac_bitget_purge(t[1 + code].b.purgebits);
	    x = t[1 + code].b.x;
	    y = t[1 + code].b.y;
	    if (x == 15)
	       x += bitget_lb(linbits);
	    if (x)
	       if (mac_bitget_1bit())
		  x = -x;
	    if (y == 15)
	       y += bitget_lb(linbits);
	    if (y)
	       if (mac_bitget_1bit())
		  y = -y;
	    xy[i][0] = x;
	    xy[i][1] = y;
	    if (bitdat.bs_ptr > bitdat.bs_ptr_end)
	       break;		// bad data protect

	 }
	 return;
   }
/*--- end switch ---*/

}
/*==========================================================*/
int unpack_huff_quad(int vwxy[][4], int n, int nbits, int ntable)
{
   int i;
   int code;
   int x, y, v, w;
   int tmp;
   int i_non_zero, tmp_nz;

   tmp_nz = 15;
   i_non_zero = -1;

   n = n >> 2;			/* huff in quads */

   if (ntable)
      goto case_quad_b;

/* case_quad_a: */
   for (i = 0; i < n; i++)
   {
      if (nbits <= 0)
	 break;
      mac_bitget_check(10);
      code = mac_bitget2(6);
      nbits -= quad_table_a[code][0];
      mac_bitget_purge(quad_table_a[code][0]);
      tmp = quad_table_a[code][1];
      if (tmp)
      {
	 i_non_zero = i;
	 tmp_nz = tmp;
      }
      v = (tmp >> 3) & 1;
      w = (tmp >> 2) & 1;
      x = (tmp >> 1) & 1;
      y = tmp & 1;
      if (v)
      {
	 if (mac_bitget_1bit())
	    v = -v;
	 nbits--;
      }
      if (w)
      {
	 if (mac_bitget_1bit())
	    w = -w;
	 nbits--;
      }
      if (x)
      {
	 if (mac_bitget_1bit())
	    x = -x;
	 nbits--;
      }
      if (y)
      {
	 if (mac_bitget_1bit())
	    y = -y;
	 nbits--;
      }
      vwxy[i][0] = v;
      vwxy[i][1] = w;
      vwxy[i][2] = x;
      vwxy[i][3] = y;
      if (bitdat.bs_ptr > bitdat.bs_ptr_end)
	 break;			// bad data protect

   }
   if (nbits < 0)
   {
      i--;
      vwxy[i][0] = 0;
      vwxy[i][1] = 0;
      vwxy[i][2] = 0;
      vwxy[i][3] = 0;
   }

   i_non_zero = (i_non_zero + 1) << 2;

   if ((tmp_nz & 3) == 0)
      i_non_zero -= 2;

   return i_non_zero;

/*--------------------*/
 case_quad_b:
   for (i = 0; i < n; i++)
   {
      if (nbits < 4)
	 break;
      nbits -= 4;
      mac_bitget_check(8);
      tmp = mac_bitget(4) ^ 15;	/* one's complement of bitstream */
      if (tmp)
      {
	 i_non_zero = i;
	 tmp_nz = tmp;
      }
      v = (tmp >> 3) & 1;
      w = (tmp >> 2) & 1;
      x = (tmp >> 1) & 1;
      y = tmp & 1;
      if (v)
      {
	 if (mac_bitget_1bit())
	    v = -v;
	 nbits--;
      }
      if (w)
      {
	 if (mac_bitget_1bit())
	    w = -w;
	 nbits--;
      }
      if (x)
      {
	 if (mac_bitget_1bit())
	    x = -x;
	 nbits--;
      }
      if (y)
      {
	 if (mac_bitget_1bit())
	    y = -y;
	 nbits--;
      }
      vwxy[i][0] = v;
      vwxy[i][1] = w;
      vwxy[i][2] = x;
      vwxy[i][3] = y;
      if (bitdat.bs_ptr > bitdat.bs_ptr_end)
	 break;			// bad data protect

   }
   if (nbits < 0)
   {
      i--;
      vwxy[i][0] = 0;
      vwxy[i][1] = 0;
      vwxy[i][2] = 0;
      vwxy[i][3] = 0;
   }

   i_non_zero = (i_non_zero + 1) << 2;

   if ((tmp_nz & 3) == 0)
      i_non_zero -= 2;

   return i_non_zero;		/* return non-zero sample (to nearest pair) */

}
/*-----------------------------------------------------*/