1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18/*
19------------------------------------------------------------------------------
20
21   PacketVideo Corp.
22   MP3 Decoder Library
23
24   Filename: pvmp3_dequantize_sample.cpp
25
26   Functions:
27      power_1_third
28      pvmp3_dequantize_sample
29
30     Date: 09/21/2007
31
32------------------------------------------------------------------------------
33 REVISION HISTORY
34
35
36 Description:
37
38------------------------------------------------------------------------------
39 INPUT AND OUTPUT DEFINITIONS
40
41power_1_third
42int32 power_1_third( int32 xx)
43
44Input
45    int32           xx,                     int32 in the [0, 8192] range
46
47 Returns
48
49    int32           xx^(1/3)                int32 Q26 number representing
50                                            the 1/3 power of the input
51
52------------------------------------------------------------------------------
53 INPUT AND OUTPUT DEFINITIONS
54
55pvmp3_dequantize_sample
56
57Input
58    int32 is[SUBBANDS_NUMBER*FILTERBANK_BANDS],
59    mp3ScaleFactors *scalefac,                 scale factor structure
60    struct gr_info_s *gr_info,                 granule structure informatiom
61    mp3Header *info                            mp3 header info
62
63 Returns
64
65    int32 is[SUBBANDS_NUMBER*FILTERBANK_BANDS], dequantize output as (.)^(4/3)
66
67------------------------------------------------------------------------------
68 FUNCTION DESCRIPTION
69
70    dequantize sample
71
72------------------------------------------------------------------------------
73 REQUIREMENTS
74
75
76------------------------------------------------------------------------------
77 REFERENCES
78
79 [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
80     ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
81
82------------------------------------------------------------------------------
83 PSEUDO-CODE
84
85------------------------------------------------------------------------------
86*/
87
88
89/*----------------------------------------------------------------------------
90; INCLUDES
91----------------------------------------------------------------------------*/
92
93#include "pv_mp3dec_fxd_op.h"
94#include "pvmp3_dec_defs.h"
95#include "pvmp3_dequantize_sample.h"
96#include "pvmp3_normalize.h"
97#include "mp3_mem_funcs.h"
98#include "pvmp3_tables.h"
99
100/*----------------------------------------------------------------------------
101; MACROS
102; Define module specific macros here
103----------------------------------------------------------------------------*/
104
105
106/*----------------------------------------------------------------------------
107; DEFINES
108; Include all pre-processor statements here. Include conditional
109; compile variables also.
110----------------------------------------------------------------------------*/
111#define Q30_fmt(a)(int32(double(0x40000000)*a))
112#define Q29_fmt(a)(int32(double(0x20000000)*a))
113
114/*----------------------------------------------------------------------------
115; LOCAL FUNCTION DEFINITIONS
116; Function Prototype declaration
117----------------------------------------------------------------------------*/
118
119/*----------------------------------------------------------------------------
120; LOCAL STORE/BUFFER/POINTER DEFINITIONS
121; Variable declaration - defined here and used outside this module
122----------------------------------------------------------------------------*/
123const int32 pretab[22] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0};
124
125const int32 pow_2_1_fourth[4] =
126{
127    Q30_fmt(1.0),                Q30_fmt(1.18920711500272),
128    Q30_fmt(1.41421356237310),   Q30_fmt(1.68179283050743)
129};
130
131const int32 two_cubic_roots[7] =
132{
133    Q29_fmt(0),                  Q29_fmt(1.25992104989487),
134    Q29_fmt(1.58740105196820),   Q29_fmt(2.00000000000000),
135    Q29_fmt(2.51984209978975),   Q29_fmt(3.17480210393640),
136    Q29_fmt(3.99999999999999)
137};
138
139/*----------------------------------------------------------------------------
140; EXTERNAL FUNCTION REFERENCES
141; Declare functions defined elsewhere and referenced in this module
142----------------------------------------------------------------------------*/
143
144/*----------------------------------------------------------------------------
145; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
146; Declare variables used in this module but defined elsewhere
147----------------------------------------------------------------------------*/
148
149/*----------------------------------------------------------------------------
150; FUNCTION CODE
151----------------------------------------------------------------------------*/
152
153
154int32 power_1_third(int32 xx)
155{
156
157    if (xx <= 512)
158    {
159        return (power_one_third[xx] >> 1);
160    }
161    else
162    {
163        if (xx >> 15)
164        {
165            return 0x7FFFFFFF;  /* saturate any value over 32767 */
166        }
167        else
168        {
169            int32 x = xx;
170            int32 m = 22 - pvmp3_normalize(xx);
171
172            xx >>= m;
173            xx = (power_one_third[xx]) + (((power_one_third[xx+1] - power_one_third[xx]) >> m) * (x & ((1 << m) - 1)));
174            return (fxp_mul32_Q30(xx, two_cubic_roots[m]));
175        }
176
177    }
178}
179
180
181/*----------------------------------------------------------------------------
182; FUNCTION CODE
183----------------------------------------------------------------------------*/
184
185
186
187void pvmp3_dequantize_sample(int32 is[SUBBANDS_NUMBER*FILTERBANK_BANDS],
188                             mp3ScaleFactors *scalefac,
189                             granuleInfo *gr_info,
190                             int32  used_freq_lines,
191                             mp3Header *info)
192{
193    int32 ss;
194    int32 cb = 0;
195    int32 global_gain;
196    int32 sfreq = info->sampling_frequency + info->version_x + (info->version_x << 1);
197
198    /* apply formula per block type */
199
200    if (gr_info->window_switching_flag && (gr_info->block_type == 2))
201    {
202        int32 next_cb_boundary;
203        int32 cb_begin = 0;
204        int32 cb_width = 0;
205        int32 mixstart = 8;                                       /* added 2003/08/21  efs */
206
207        if (info->version_x != MPEG_1)
208        {
209            mixstart = 6;                                   /* different value in MPEG2 LSF */
210        }
211
212        if (gr_info->mixed_block_flag)
213        {
214            next_cb_boundary = mp3_sfBandIndex[sfreq].l[1];  /* LONG blocks: 0,1,3 */
215        }
216        else
217        {
218            next_cb_boundary = mp3_sfBandIndex[sfreq].s[1] * 3; /* pure SHORT block */
219            cb_width = 0;
220        }
221
222        global_gain =  gr_info->global_gain;
223        int32 two_raise_one_fourth = pow_2_1_fourth[global_gain&0x3];
224        global_gain = 12 + (global_gain >> 2);
225
226        for (ss = 0 ; ss < used_freq_lines ; ss++)
227        {
228            if (ss == next_cb_boundary)
229            {
230                cb++;       /*  critical band counter */
231                if (gr_info->mixed_block_flag)
232                {
233                    if (next_cb_boundary == mp3_sfBandIndex[sfreq].l[mixstart])
234                    {
235                        next_cb_boundary = mp3_sfBandIndex[sfreq].s[4] * 3;
236
237                        cb_begin = mp3_sfBandIndex[sfreq].s[3] * 3;
238                        cb_width = 3;
239                        cb = 3;
240                    }
241                    else if (ss < mp3_sfBandIndex[sfreq].l[mixstart])
242                    {
243                        next_cb_boundary = mp3_sfBandIndex[sfreq].l[cb+1];
244                    }
245                    else
246                    {
247                        next_cb_boundary = mp3_sfBandIndex[sfreq].s[cb+1] * 3;
248
249                        cb_width = cb;
250                        cb_begin = mp3_sfBandIndex[sfreq].s[cb] * 3;
251                    }
252
253                    if (ss < 2*FILTERBANK_BANDS)
254                    {   /*  1st 2 subbands of switched blocks */
255                        global_gain  = (gr_info->global_gain);
256                        global_gain -= (1 + gr_info->scalefac_scale) *
257                                       (scalefac->l[cb] + gr_info->preflag * pretab[cb]) << 1;
258
259                        two_raise_one_fourth = pow_2_1_fourth[global_gain&0x3];
260                        global_gain = 12 + (global_gain >> 2);
261                    }
262                }
263                else
264                {
265                    next_cb_boundary = mp3_sfBandIndex[sfreq].s[cb+1] * 3;
266                    cb_width = cb;
267                    cb_begin = mp3_sfBandIndex[sfreq].s[cb] * 3;
268                }
269
270            }   /*  end-if ( ss == next_cb_boundary) */
271
272            /* Do long/short dependent scaling operations. */
273            if ((gr_info->mixed_block_flag == 0) || (gr_info->mixed_block_flag && (ss >= 2*FILTERBANK_BANDS)))
274            {
275                int32 temp2 = fxp_mul32_Q32((ss - cb_begin) << 16, mp3_shortwindBandWidths[sfreq][cb_width]);
276                temp2 = (temp2 + 1) >> 15;
277
278                global_gain  = (gr_info->global_gain);
279                global_gain -=  gr_info->subblock_gain[temp2] << 3;
280                global_gain -= (1 + gr_info->scalefac_scale) * (scalefac->s[temp2][cb] << 1);
281
282                two_raise_one_fourth = pow_2_1_fourth[global_gain&0x3];
283                global_gain = 12 + (global_gain >> 2);
284
285            }
286
287
288            /*
289             *       xr[sb][ss] = 2^(global_gain/4)
290             */
291
292            /* Scale quantized value. */
293
294            /* 0 < abs(is[ss]) < 8192 */
295
296            int32 tmp = fxp_mul32_Q30((is[ss] << 16), power_1_third(pv_abs(is[ ss])));
297
298            tmp = fxp_mul32_Q30(tmp, two_raise_one_fourth);
299
300            if (global_gain < 0)
301            {
302                int32 temp = - global_gain;
303                if (temp < 32)
304                {
305                    is[ss] = (tmp >> temp);
306                }
307                else
308                {
309                    is[ss] = 0;
310                }
311            }
312            else
313            {
314                is[ss] = (tmp << global_gain);
315            }
316
317        }  /*   for (ss=0 ; ss < used_freq_lines ; ss++)   */
318
319    }
320    else
321    {
322
323        for (cb = 0 ; cb < 22 ; cb++)
324        {
325
326            /* Compute overall (global) scaling. */
327
328            global_gain  = (gr_info->global_gain);
329
330            global_gain -= (1 + gr_info->scalefac_scale) *
331                           (scalefac->l[cb] + gr_info->preflag * pretab[cb]) << 1;
332
333
334            int32 two_raise_one_fourth = pow_2_1_fourth[global_gain&0x3];
335            global_gain = 12 + (global_gain >> 2);
336
337            /*
338             *       xr[sb][ss] = 2^(global_gain/4)
339             */
340
341            /* Scale quantized value. */
342
343            if (used_freq_lines >= mp3_sfBandIndex[sfreq].l[cb+1])
344            {
345                if (global_gain <= 0)
346                {
347                    global_gain = - global_gain;
348                    if (global_gain < 32)
349                    {
350                        for (ss = mp3_sfBandIndex[sfreq].l[cb]; ss < mp3_sfBandIndex[sfreq].l[cb+1]; ss += 2)
351                        {
352                            int32 tmp =  is[ss];
353                            if (tmp)
354                            {
355                                tmp = fxp_mul32_Q30((tmp << 16), power_1_third(pv_abs(tmp)));
356                                is[ss] = fxp_mul32_Q30(tmp, two_raise_one_fourth) >> global_gain;
357                            }
358                            tmp =  is[ss+1];
359                            if (tmp)
360                            {
361                                tmp = fxp_mul32_Q30((tmp << 16), power_1_third(pv_abs(tmp)));
362                                is[ss+1] = fxp_mul32_Q30(tmp, two_raise_one_fourth) >> global_gain;
363                            }
364                        }
365                    }
366                    else
367                    {
368                        pv_memset(&is[ mp3_sfBandIndex[sfreq].l[cb]],
369                                  0,
370                                  (mp3_sfBandIndex[sfreq].l[cb+1] - mp3_sfBandIndex[sfreq].l[cb])*sizeof(*is));
371                    }
372                }
373                else
374                {
375                    for (ss = mp3_sfBandIndex[sfreq].l[cb]; ss < mp3_sfBandIndex[sfreq].l[cb+1]; ss += 2)
376                    {
377                        int32 tmp =  is[ss];
378                        if (tmp)
379                        {
380                            tmp = fxp_mul32_Q30((tmp << 16), power_1_third(pv_abs(tmp)));
381                            is[ss] = fxp_mul32_Q30(tmp, two_raise_one_fourth) << global_gain;
382                        }
383
384                        tmp =  is[ss+1];
385                        if (tmp)
386                        {
387                            tmp = fxp_mul32_Q30((tmp << 16), power_1_third(pv_abs(tmp)));
388                            is[ss+1] = fxp_mul32_Q30(tmp, two_raise_one_fourth) << global_gain;
389                        }
390                    }
391                }
392            }
393            else
394            {
395                if (global_gain <= 0)
396                {
397                    global_gain = - global_gain;
398                    if (global_gain < 32)
399                    {
400                        for (ss = mp3_sfBandIndex[sfreq].l[cb]; ss < used_freq_lines; ss += 2)
401                        {
402                            int32 tmp =  is[ss];
403                            if (tmp)
404                            {
405                                tmp = fxp_mul32_Q30((tmp << 16), power_1_third(pv_abs(tmp)));
406                                is[ss] = fxp_mul32_Q30(tmp, two_raise_one_fourth) >> global_gain;
407                            }
408                            tmp =  is[ss+1];
409                            if (tmp)
410                            {
411                                tmp = fxp_mul32_Q30((tmp << 16), power_1_third(pv_abs(tmp)));
412                                is[ss+1] = fxp_mul32_Q30(tmp, two_raise_one_fourth) >> global_gain;
413                            }
414                        }
415
416                    }
417                    else
418                    {
419                        pv_memset(&is[ mp3_sfBandIndex[sfreq].l[cb]],
420                                  0,
421                                  (mp3_sfBandIndex[sfreq].l[cb+1] - mp3_sfBandIndex[sfreq].l[cb])*sizeof(*is));
422                    }
423                }
424                else
425                {
426                    for (ss = mp3_sfBandIndex[sfreq].l[cb]; ss < used_freq_lines; ss++)
427                    {
428                        int32 tmp =  is[ss];
429
430                        if (tmp)
431                        {
432                            tmp = fxp_mul32_Q30((tmp << 16), power_1_third(pv_abs(tmp)));
433                            is[ss] = fxp_mul32_Q30(tmp, two_raise_one_fourth) << global_gain;
434                        }
435                    }
436                }
437
438                cb = 22;  // force breaking out of the loop
439
440            } /*  if ( used_freq_lines >= mp3_sfBandIndex[sfreq].l[cb+1]) */
441
442        }   /* for (cb=0 ; cb < 22 ; cb++)  */
443
444    }   /*  if (gr_info->window_switching_flag && (gr_info->block_type == 2))  */
445
446
447    pv_memset(&is[used_freq_lines],
448              0,
449              (FILTERBANK_BANDS*SUBBANDS_NUMBER - used_freq_lines)*sizeof(*is));
450
451}
452
453