aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/mp3/src/l3dq.c
blob: 3ee0764aa54376901c295be4ac00a5e9314b7851 (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
/*____________________________________________________________________________
	
	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$
____________________________________________________________________________*/

/****  quant.c  ***************************************************
  Layer III  dequant

  does reordering of short blocks

  mod 8/19/98 decode 22 sf bands

******************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <math.h>
#include <string.h>
#include "L3.h"
#include "mhead.h"
#include "protos.h"


/*----------
static struct  {
int l[23];
int s[14];} sfBandTable[3] =   
{{{0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
 {0,4,8,12,16,22,30,40,52,66,84,106,136,192}},
{{0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
 {0,4,8,12,16,22,28,38,50,64,80,100,126,192}},
{{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
 {0,4,8,12,16,22,30,42,58,78,104,138,180,192}}};
----------*/

/*--------------------------------*/
static int pretab[2][22] =
{
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0},
};


/* 8 bit plus 2 lookup x = pow(2.0, 0.25*(global_gain-210)) */
/* two extra slots to do 1/sqrt(2) scaling for ms */
/* 4 extra slots to do 1/2 scaling for cvt to mono */

/*-------- scaling lookup
x = pow(2.0, -0.5*(1+scalefact_scale)*scalefac + preemp)
look_scale[scalefact_scale][preemp][scalefac]
-----------------------*/
#if 0
typedef float LS[4][32];
#endif

/*--- iSample**(4/3) lookup, -32<=i<=31 ---*/

/*-- pow(2.0, -0.25*8.0*subblock_gain) --*/

/*-- reorder buffer ---*/
typedef float ARRAY3[3];


/*=============================================================*/
float *quant_init_global_addr(MPEG *m)
{
   return m->cupl.look_global;
}
/*-------------------------------------------------------------*/
LS *quant_init_scale_addr(MPEG *m)
{
   return m->cupl.look_scale;
}
/*-------------------------------------------------------------*/
float *quant_init_pow_addr(MPEG *m)
{
   return m->cupl.look_pow;
}
/*-------------------------------------------------------------*/
float *quant_init_subblock_addr(MPEG *m)
{
   return m->cupl.look_subblock;
}
/*=============================================================*/

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

void dequant(MPEG *m, SAMPLE Sample[], int *nsamp,
	     SCALEFACT * sf,
	     GR * gr,
	     CB_INFO * cb_info, int ncbl_mixed)
{
   int i, j;
   int cb, n, w;
   float x0, xs;
   float xsb[3];
   double tmp;
   int ncbl;
   int cbs0;
   ARRAY3 *buf;			/* short block reorder */
   int nbands;
   int i0;
   int non_zero;
   int cbmax[3];

   nbands = *nsamp;


   ncbl = 22;			/* long block cb end */
   cbs0 = 12;			/* short block cb start */
/* ncbl_mixed = 8 or 6  mpeg1 or 2 */
   if (gr->block_type == 2)
   {
      ncbl = 0;
      cbs0 = 0;
      if (gr->mixed_block_flag)
      {
	 ncbl = ncbl_mixed;
	 cbs0 = 3;
      }
   }
/* fill in cb_info -- */
   /* This doesn't seem used anywhere...
   cb_info->lb_type = gr->block_type;
   if (gr->block_type == 2)
      cb_info->lb_type;
   */
   cb_info->cbs0 = cbs0;
   cb_info->ncbl = ncbl;

   cbmax[2] = cbmax[1] = cbmax[0] = 0;
/* global gain pre-adjusted by 2 if ms_mode, 0 otherwise */
   x0 = m->cupl.look_global[(2 + 4) + gr->global_gain];
   i = 0;
/*----- long blocks ---*/
   for (cb = 0; cb < ncbl; cb++)
   {
      non_zero = 0;
      xs = x0 * m->cupl.look_scale[gr->scalefac_scale][pretab[gr->preflag][cb]][sf->l[cb]];
      n = m->cupl.nBand[0][cb];
      for (j = 0; j < n; j++, i++)
      {
	 if (Sample[i].s == 0)
	    Sample[i].x = 0.0F;
	 else
	 {
	    non_zero = 1;
	    if ((Sample[i].s >= (-ISMAX)) && (Sample[i].s < ISMAX))
	       Sample[i].x = xs * m->cupl.look_pow[ISMAX + Sample[i].s];
	    else
	    {
		float tmpConst = (float)(1.0/3.0);
	       tmp = (double) Sample[i].s;
	       Sample[i].x = (float) (xs * tmp * pow(fabs(tmp), tmpConst));
	    }
	 }
      }
      if (non_zero)
	 cbmax[0] = cb;
      if (i >= nbands)
	 break;
   }

   cb_info->cbmax = cbmax[0];
   cb_info->cbtype = 0;		// type = long

   if (cbs0 >= 12)
      return;
/*---------------------------
block type = 2  short blocks
----------------------------*/
   cbmax[2] = cbmax[1] = cbmax[0] = cbs0;
   i0 = i;			/* save for reorder */
   buf = m->cupl.re_buf;
   for (w = 0; w < 3; w++)
      xsb[w] = x0 * m->cupl.look_subblock[gr->subblock_gain[w]];
   for (cb = cbs0; cb < 13; cb++)
   {
      n = m->cupl.nBand[1][cb];
      for (w = 0; w < 3; w++)
      {
	 non_zero = 0;
	 xs = xsb[w] * m->cupl.look_scale[gr->scalefac_scale][0][sf->s[w][cb]];
	 for (j = 0; j < n; j++, i++)
	 {
	    if (Sample[i].s == 0)
	       buf[j][w] = 0.0F;
	    else
	    {
	       non_zero = 1;
	       if ((Sample[i].s >= (-ISMAX)) && (Sample[i].s < ISMAX))
		  buf[j][w] = xs * m->cupl.look_pow[ISMAX + Sample[i].s];
	       else
	       {
		  float tmpConst = (float)(1.0/3.0);
		  tmp = (double) Sample[i].s;
		  buf[j][w] = (float) (xs * tmp * pow(fabs(tmp), tmpConst));
	       }
	    }
	 }
	 if (non_zero)
	    cbmax[w] = cb;
      }
      if (i >= nbands)
	 break;
      buf += n;
   }


   memmove(&Sample[i0].x, &m->cupl.re_buf[0][0], sizeof(float) * (i - i0));

   *nsamp = i;			/* update nsamp */
   cb_info->cbmax_s[0] = cbmax[0];
   cb_info->cbmax_s[1] = cbmax[1];
   cb_info->cbmax_s[2] = cbmax[2];
   if (cbmax[1] > cbmax[0])
      cbmax[0] = cbmax[1];
   if (cbmax[2] > cbmax[0])
      cbmax[0] = cbmax[2];

   cb_info->cbmax = cbmax[0];
   cb_info->cbtype = 1;		/* type = short */


   return;
}

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

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