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_get_scale_factors.cpp
25
26     Date: 09/21/2007
27
28------------------------------------------------------------------------------
29 REVISION HISTORY
30
31
32 Description:
33
34------------------------------------------------------------------------------
35 INPUT AND OUTPUT DEFINITIONS
36
37 Input
38    mp3ScaleFactors *scalefac,
39    mp3SideInfo *si,               side info
40    int32 gr,                      granule
41    int32 ch,                      channel
42    tbits *pMainData               bit stream
43
44  Returns
45
46    mp3ScaleFactors *scalefac,   scale factors
47
48------------------------------------------------------------------------------
49 FUNCTION DESCRIPTION
50
51    get scale factors
52
53------------------------------------------------------------------------------
54 REQUIREMENTS
55
56
57------------------------------------------------------------------------------
58 REFERENCES
59
60 [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
61     ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
62
63------------------------------------------------------------------------------
64 PSEUDO-CODE
65
66------------------------------------------------------------------------------
67*/
68
69
70/*----------------------------------------------------------------------------
71; INCLUDES
72----------------------------------------------------------------------------*/
73
74#include "pvmp3_get_scale_factors.h"
75#include "pvmp3_getbits.h"
76#include "mp3_mem_funcs.h"
77
78/*----------------------------------------------------------------------------
79; MACROS
80; Define module specific macros here
81----------------------------------------------------------------------------*/
82
83
84/*----------------------------------------------------------------------------
85; DEFINES
86; Include all pre-processor statements here. Include conditional
87; compile variables also.
88----------------------------------------------------------------------------*/
89#define Qfmt_28(a)(int32(double(0x10000000)*a))
90
91/*----------------------------------------------------------------------------
92; LOCAL FUNCTION DEFINITIONS
93; Function Prototype declaration
94----------------------------------------------------------------------------*/
95
96/*----------------------------------------------------------------------------
97; LOCAL STORE/BUFFER/POINTER DEFINITIONS
98; Variable declaration - defined here and used outside this module
99----------------------------------------------------------------------------*/
100const int32 slen[2][16] =
101{
102    {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
103    {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
104};
105
106const struct
107{
108    int32 l[5];
109    int32 s[3];
110} sfbtable =
111{
112    {0, 6, 11, 16, 21},
113    {0, 6, 12}
114};
115
116const int32 long_sfbtable[4] = { 6, 5, 5, 5};
117
118/*----------------------------------------------------------------------------
119; EXTERNAL FUNCTION REFERENCES
120; Declare functions defined elsewhere and referenced in this module
121----------------------------------------------------------------------------*/
122
123/*----------------------------------------------------------------------------
124; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
125; Declare variables used in this module but defined elsewhere
126----------------------------------------------------------------------------*/
127
128/*----------------------------------------------------------------------------
129; FUNCTION CODE
130----------------------------------------------------------------------------*/
131
132void pvmp3_get_scale_factors(mp3ScaleFactors *scalefac,
133                             mp3SideInfo    *si,
134                             int32          gr,
135                             int32          ch,
136                             tmp3Bits       *pMainData)
137{
138    int32 sfb;
139    int32 i;
140    int32 window;
141    granuleInfo *gr_info = &(si->ch[ch].gran[gr]);
142
143    if (gr_info->window_switching_flag && (gr_info->block_type == 2))
144    {
145        if (gr_info->mixed_block_flag)
146        { /* MIXED */
147            for (sfb = 0; sfb < 8; sfb++)
148            {
149                scalefac->l[sfb] = getNbits(pMainData, slen[0][gr_info->scalefac_compress]);
150            }
151
152            for (sfb = 3; sfb < 6; sfb++)
153            {
154                for (window = 0; window < 3; window++)
155                {
156                    scalefac->s[window][sfb] = getNbits(pMainData, slen[0][gr_info->scalefac_compress]);
157                }
158            }
159            for (sfb = 6; sfb < 12; sfb++)
160            {
161                for (window = 0; window < 3; window++)
162                {
163                    scalefac->s[window][sfb] = getNbits(pMainData, slen[1][gr_info->scalefac_compress]);
164                }
165            }
166        }
167        else
168        {  /* SHORT*/
169            for (i = 0; i < 2; i++)
170            {
171                for (sfb = sfbtable.s[i]; sfb < sfbtable.s[i+1]; sfb++)
172                {
173                    for (window = 0; window < 3; window++)
174                    {
175                        scalefac->s[window][sfb] = getNbits(pMainData, slen[i][gr_info->scalefac_compress]);
176                    }
177                }
178            }
179        }
180
181        scalefac->s[0][12] = 0;    /* sfb = 12 win= 0 */
182        scalefac->s[1][12] = 0;    /* sfb = 12 win= 1 */
183        scalefac->s[2][12] = 0;    /* sfb = 12 win= 2 */
184    }
185    else
186    {   /* LONG types 0,1,3 */
187
188        int32 *ptr = &scalefac->l[0];
189
190        for (i = 0; i < 4; i++)
191        {
192            int32 tmp4 = long_sfbtable[i];
193
194            if ((si->ch[ch].scfsi[i] == 0) || (gr == 0))
195            {
196                int32 tmp1 = slen[(i>>1)][gr_info->scalefac_compress];
197
198                if (tmp1)
199                {
200                    int32 tmp2 = tmp1 * tmp4;
201                    uint32 tmp3 = getNbits(pMainData, tmp2);
202                    tmp4 = 32 - tmp1;
203                    for (; tmp2 > 0; tmp2 -= tmp1)
204                    {
205                        *(ptr++) = (tmp3 << (32 - tmp2)) >> tmp4;
206                    }
207                }
208                else
209                {
210                    for (sfb = tmp4; sfb != 0; sfb--)
211                    {
212                        *(ptr++) = 0;
213                    }
214
215                }
216            }
217            else
218            {
219                ptr += tmp4;
220            }
221        }
222        scalefac->l[21] = 0;
223        scalefac->l[22] = 0;
224    }
225}
226
227