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: l3dq.c,v 1.6 1999/10/19 07:13:09 elrod Exp $
25 ____________________________________________________________________________*/
26
27 /**** quant.c ***************************************************
28 Layer III dequant
29
30 does reordering of short blocks
31
32 mod 8/19/98 decode 22 sf bands
33
34 ******************************************************************/
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <float.h>
39 #include <math.h>
40 #include <string.h>
41 #include "l3.h"
42
43 #include "mp3struct.h"
44
45 /*----------
46 static struct {
47 int l[23];
48 int s[14];} sfBandTable[3] =
49 {{{0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
50 {0,4,8,12,16,22,30,40,52,66,84,106,136,192}},
51 {{0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
52 {0,4,8,12,16,22,28,38,50,64,80,100,126,192}},
53 {{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
54 {0,4,8,12,16,22,30,42,58,78,104,138,180,192}}};
55 ----------*/
56
57 /*--------------------------------*/
58 static const int pretab[2][22] =
59 {
60 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
61 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0},
62 };
63
64
65 ////@@@@extern int nBand[2][22]; /* long = nBand[0][i], short = nBand[1][i] */
66
67 /* 8 bit plus 2 lookup x = pow(2.0, 0.25*(global_gain-210)) */
68 /* two extra slots to do 1/sqrt(2) scaling for ms */
69 /* 4 extra slots to do 1/2 scaling for cvt to mono */
70 static float look_global[256 + 2 + 4]; // effectively constant
71
72 /*-------- scaling lookup
73 x = pow(2.0, -0.5*(1+scalefact_scale)*scalefac + preemp)
74 look_scale[scalefact_scale][preemp][scalefac]
75 -----------------------*/
76 static float look_scale[2][4][32]; // effectively constant
77 typedef float LS[4][32];
78
79
80 /*--- iSample**(4/3) lookup, -32<=i<=31 ---*/
81 #define ISMAX 32
82 static float look_pow[2 * ISMAX]; // effectively constant
83
84 /*-- pow(2.0, -0.25*8.0*subblock_gain) --*/
85 static float look_subblock[8]; // effectively constant
86
87 /*-- reorder buffer ---*/
88 static float re_buf[192][3]; // used by dequant() below, but only during func (as workspace)
89 typedef float ARRAY3[3];
90
91
92 /*=============================================================*/
quant_init_global_addr()93 float *quant_init_global_addr()
94 {
95 return look_global;
96 }
97 /*-------------------------------------------------------------*/
quant_init_scale_addr()98 LS *quant_init_scale_addr()
99 {
100 return look_scale;
101 }
102 /*-------------------------------------------------------------*/
quant_init_pow_addr()103 float *quant_init_pow_addr()
104 {
105 return look_pow;
106 }
107 /*-------------------------------------------------------------*/
quant_init_subblock_addr()108 float *quant_init_subblock_addr()
109 {
110 return look_subblock;
111 }
112 /*=============================================================*/
113
dequant(SAMPLE Sample[],int * nsamp,SCALEFACT * sf,GR * gr,CB_INFO * cb_info,int ncbl_mixed)114 void dequant(SAMPLE Sample[], int *nsamp,
115 SCALEFACT * sf,
116 GR * gr,
117 CB_INFO * cb_info, int ncbl_mixed)
118 {
119 int i, j;
120 int cb, n, w;
121 float x0, xs;
122 float xsb[3];
123 double tmp;
124 int ncbl;
125 int cbs0;
126 ARRAY3 *buf; /* short block reorder */
127 int nbands;
128 int i0;
129 int non_zero;
130 int cbmax[3];
131
132 nbands = *nsamp;
133
134
135 ncbl = 22; /* long block cb end */
136 cbs0 = 12; /* short block cb start */
137 /* ncbl_mixed = 8 or 6 mpeg1 or 2 */
138 if (gr->block_type == 2)
139 {
140 ncbl = 0;
141 cbs0 = 0;
142 if (gr->mixed_block_flag)
143 {
144 ncbl = ncbl_mixed;
145 cbs0 = 3;
146 }
147 }
148 /* fill in cb_info -- */
149 /* This doesn't seem used anywhere...
150 cb_info->lb_type = gr->block_type;
151 if (gr->block_type == 2)
152 cb_info->lb_type;
153 */
154 cb_info->cbs0 = cbs0;
155 cb_info->ncbl = ncbl;
156
157 cbmax[2] = cbmax[1] = cbmax[0] = 0;
158 /* global gain pre-adjusted by 2 if ms_mode, 0 otherwise */
159 x0 = look_global[(2 + 4) + gr->global_gain];
160 i = 0;
161 /*----- long blocks ---*/
162 for (cb = 0; cb < ncbl; cb++)
163 {
164 non_zero = 0;
165 xs = x0 * look_scale[gr->scalefac_scale][pretab[gr->preflag][cb]][sf->l[cb]];
166 n = pMP3Stream->nBand[0][cb];
167 for (j = 0; j < n; j++, i++)
168 {
169 if (Sample[i].s == 0)
170 Sample[i].x = 0.0F;
171 else
172 {
173 non_zero = 1;
174 if ((Sample[i].s >= (-ISMAX)) && (Sample[i].s < ISMAX))
175 Sample[i].x = xs * look_pow[ISMAX + Sample[i].s];
176 else
177 {
178 float tmpConst = (float)(1.0/3.0);
179 tmp = (double) Sample[i].s;
180 Sample[i].x = (float) (xs * tmp * pow(fabs(tmp), tmpConst));
181 }
182 }
183 }
184 if (non_zero)
185 cbmax[0] = cb;
186 if (i >= nbands)
187 break;
188 }
189
190 cb_info->cbmax = cbmax[0];
191 cb_info->cbtype = 0; // type = long
192
193 if (cbs0 >= 12)
194 return;
195 /*---------------------------
196 block type = 2 short blocks
197 ----------------------------*/
198 cbmax[2] = cbmax[1] = cbmax[0] = cbs0;
199 i0 = i; /* save for reorder */
200 buf = re_buf;
201 for (w = 0; w < 3; w++)
202 xsb[w] = x0 * look_subblock[gr->subblock_gain[w]];
203 for (cb = cbs0; cb < 13; cb++)
204 {
205 n = pMP3Stream->nBand[1][cb];
206 for (w = 0; w < 3; w++)
207 {
208 non_zero = 0;
209 xs = xsb[w] * look_scale[gr->scalefac_scale][0][sf->s[w][cb]];
210 for (j = 0; j < n; j++, i++)
211 {
212 if (Sample[i].s == 0)
213 buf[j][w] = 0.0F;
214 else
215 {
216 non_zero = 1;
217 if ((Sample[i].s >= (-ISMAX)) && (Sample[i].s < ISMAX))
218 buf[j][w] = xs * look_pow[ISMAX + Sample[i].s];
219 else
220 {
221 float tmpConst = (float)(1.0/3.0);
222 tmp = (double) Sample[i].s;
223 buf[j][w] = (float) (xs * tmp * pow(fabs(tmp), tmpConst));
224 }
225 }
226 }
227 if (non_zero)
228 cbmax[w] = cb;
229 }
230 if (i >= nbands)
231 break;
232 buf += n;
233 }
234
235
236 memmove(&Sample[i0].x, &re_buf[0][0], sizeof(float) * (i - i0));
237
238 *nsamp = i; /* update nsamp */
239 cb_info->cbmax_s[0] = cbmax[0];
240 cb_info->cbmax_s[1] = cbmax[1];
241 cb_info->cbmax_s[2] = cbmax[2];
242 if (cbmax[1] > cbmax[0])
243 cbmax[0] = cbmax[1];
244 if (cbmax[2] > cbmax[0])
245 cbmax[0] = cbmax[2];
246
247 cb_info->cbmax = cbmax[0];
248 cb_info->cbtype = 1; /* type = short */
249
250
251 return;
252 }
253
254 /*-------------------------------------------------------------*/
255