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_side_info.cpp
25
26     Date: 09/21/2007
27
28------------------------------------------------------------------------------
29 REVISION HISTORY
30
31
32 Description:
33
34------------------------------------------------------------------------------
35 INPUT AND OUTPUT DEFINITIONS
36
37Input
38    mp3SideInfo     *si,
39    mp3Header       *info,             mp3 header information
40    uint32          *crc               initialized crc value (if enabled)
41
42
43 Returns
44
45    mp3SideInfo     *si,               side information
46
47
48------------------------------------------------------------------------------
49 FUNCTION DESCRIPTION
50
51    acquires side information
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_side_info.h"
75#include "pvmp3_crc.h"
76
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
90/*----------------------------------------------------------------------------
91; LOCAL FUNCTION DEFINITIONS
92; Function Prototype declaration
93----------------------------------------------------------------------------*/
94
95/*----------------------------------------------------------------------------
96; LOCAL STORE/BUFFER/POINTER DEFINITIONS
97; Variable declaration - defined here and used outside this module
98----------------------------------------------------------------------------*/
99
100/*----------------------------------------------------------------------------
101; EXTERNAL FUNCTION REFERENCES
102; Declare functions defined elsewhere and referenced in this module
103----------------------------------------------------------------------------*/
104
105/*----------------------------------------------------------------------------
106; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
107; Declare variables used in this module but defined elsewhere
108----------------------------------------------------------------------------*/
109
110/*----------------------------------------------------------------------------
111; FUNCTION CODE
112----------------------------------------------------------------------------*/
113
114ERROR_CODE pvmp3_get_side_info(tmp3Bits    *inputStream,
115                               mp3SideInfo *si,
116                               mp3Header   *info,
117                               uint32      *crc)
118{
119    int32 ch, gr;
120    uint32 tmp;
121
122    int stereo  = (info->mode == MPG_MD_MONO) ? 1 : 2;
123
124    if (info->version_x == MPEG_1)
125    {
126        if (stereo == 1)
127        {
128            tmp = getbits_crc(inputStream, 14, crc, info->error_protection);
129            si->main_data_begin = (tmp << 18) >> 23;    /* 9 */
130            si->private_bits    = (tmp << 23) >> 27;    /* 5 */
131        }
132        else
133        {
134            tmp = getbits_crc(inputStream, 12, crc, info->error_protection);
135            si->main_data_begin = (tmp << 20) >> 23;    /* 9 */
136            si->private_bits    = (tmp << 23) >> 29;    /* 3 */
137
138        }
139
140        for (ch = 0; ch < stereo; ch++)
141        {
142            tmp = getbits_crc(inputStream, 4, crc, info->error_protection);
143            si->ch[ch].scfsi[0] = (tmp << 28) >> 31;    /* 1 */
144            si->ch[ch].scfsi[1] = (tmp << 29) >> 31;    /* 1 */
145            si->ch[ch].scfsi[2] = (tmp << 30) >> 31;    /* 1 */
146            si->ch[ch].scfsi[3] =  tmp & 1;         /* 1 */
147        }
148
149        for (gr = 0; gr < 2 ; gr++)
150        {
151            for (ch = 0; ch < stereo; ch++)
152            {
153                si->ch[ch].gran[gr].part2_3_length    = getbits_crc(inputStream, 12, crc, info->error_protection);
154                tmp = getbits_crc(inputStream, 22, crc, info->error_protection);
155
156                si->ch[ch].gran[gr].big_values            = (tmp << 10) >> 23;   /* 9 */
157                si->ch[ch].gran[gr].global_gain           = ((tmp << 19) >> 24) - 210;   /* 8 */
158                si->ch[ch].gran[gr].scalefac_compress     = (tmp << 27) >> 28;   /* 4 */
159                si->ch[ch].gran[gr].window_switching_flag = tmp & 1;         /* 1 */
160
161                if (si->ch[ch].gran[gr].window_switching_flag)
162                {
163                    tmp = getbits_crc(inputStream, 22, crc, info->error_protection);
164
165                    si->ch[ch].gran[gr].block_type       = (tmp << 10) >> 30;   /* 2 */;
166                    si->ch[ch].gran[gr].mixed_block_flag = (tmp << 12) >> 31;   /* 1 */;
167
168                    si->ch[ch].gran[gr].table_select[0]  = (tmp << 13) >> 27;   /* 5 */;
169                    si->ch[ch].gran[gr].table_select[1]  = (tmp << 18) >> 27;   /* 5 */;
170
171                    si->ch[ch].gran[gr].subblock_gain[0] = (tmp << 23) >> 29;   /* 3 */;
172                    si->ch[ch].gran[gr].subblock_gain[1] = (tmp << 26) >> 29;   /* 3 */;
173                    si->ch[ch].gran[gr].subblock_gain[2] = (tmp << 29) >> 29;   /* 3 */;
174
175                    /* Set region_count parameters since they are implicit in this case. */
176
177                    if (si->ch[ch].gran[gr].block_type == 0)
178                    {
179                        return(SIDE_INFO_ERROR);
180                    }
181                    else if ((si->ch[ch].gran[gr].block_type       == 2)
182                             && (si->ch[ch].gran[gr].mixed_block_flag == 0))
183                    {
184                        si->ch[ch].gran[gr].region0_count = 8; /* MI 9; */
185                        si->ch[ch].gran[gr].region1_count = 12;
186                    }
187                    else
188                    {
189                        si->ch[ch].gran[gr].region0_count = 7; /* MI 8; */
190                        si->ch[ch].gran[gr].region1_count = 13;
191                    }
192                }
193                else
194                {
195                    tmp = getbits_crc(inputStream, 22, crc, info->error_protection);
196
197                    si->ch[ch].gran[gr].table_select[0] = (tmp << 10) >> 27;   /* 5 */;
198                    si->ch[ch].gran[gr].table_select[1] = (tmp << 15) >> 27;   /* 5 */;
199                    si->ch[ch].gran[gr].table_select[2] = (tmp << 20) >> 27;   /* 5 */;
200
201                    si->ch[ch].gran[gr].region0_count   = (tmp << 25) >> 28;   /* 4 */;
202                    si->ch[ch].gran[gr].region1_count   = (tmp << 29) >> 29;   /* 3 */;
203
204                    si->ch[ch].gran[gr].block_type      = 0;
205                }
206
207                tmp = getbits_crc(inputStream, 3, crc, info->error_protection);
208                si->ch[ch].gran[gr].preflag            = (tmp << 29) >> 31;    /* 1 */
209                si->ch[ch].gran[gr].scalefac_scale     = (tmp << 30) >> 31;    /* 1 */
210                si->ch[ch].gran[gr].count1table_select =  tmp & 1;         /* 1 */
211            }
212        }
213    }
214    else /* Layer 3 LSF */
215    {
216        si->main_data_begin = getbits_crc(inputStream,      8, crc, info->error_protection);
217        si->private_bits    = getbits_crc(inputStream, stereo, crc, info->error_protection);
218
219        for (ch = 0; ch < stereo; ch++)
220        {
221            tmp = getbits_crc(inputStream, 21, crc, info->error_protection);
222            si->ch[ch].gran[0].part2_3_length    = (tmp << 11) >> 20;  /* 12 */
223            si->ch[ch].gran[0].big_values        = (tmp << 23) >> 23;  /*  9 */
224
225            tmp = getbits_crc(inputStream, 18, crc, info->error_protection);
226            si->ch[ch].gran[0].global_gain           = ((tmp << 14) >> 24) - 210;   /* 8 */
227            si->ch[ch].gran[0].scalefac_compress     = (tmp << 22) >> 23;   /* 9 */
228            si->ch[ch].gran[0].window_switching_flag = tmp & 1;         /* 1 */
229
230            if (si->ch[ch].gran[0].window_switching_flag)
231            {
232
233                tmp = getbits_crc(inputStream, 22, crc, info->error_protection);
234
235                si->ch[ch].gran[0].block_type       = (tmp << 10) >> 30;   /* 2 */;
236                si->ch[ch].gran[0].mixed_block_flag = (tmp << 12) >> 31;   /* 1 */;
237
238                si->ch[ch].gran[0].table_select[0]  = (tmp << 13) >> 27;   /* 5 */;
239                si->ch[ch].gran[0].table_select[1]  = (tmp << 18) >> 27;   /* 5 */;
240
241                si->ch[ch].gran[0].subblock_gain[0] = (tmp << 23) >> 29;   /* 3 */;
242                si->ch[ch].gran[0].subblock_gain[1] = (tmp << 26) >> 29;   /* 3 */;
243                si->ch[ch].gran[0].subblock_gain[2] = (tmp << 29) >> 29;   /* 3 */;
244
245                /* Set region_count parameters since they are implicit in this case. */
246
247                if (si->ch[ch].gran[0].block_type == 0)
248                {
249                    return(SIDE_INFO_ERROR);
250                }
251                else if ((si->ch[ch].gran[0].block_type       == 2)
252                         && (si->ch[ch].gran[0].mixed_block_flag == 0))
253                {
254                    si->ch[ch].gran[0].region0_count = 8; /* MI 9; */
255                    si->ch[ch].gran[0].region1_count = 12;
256                }
257                else
258                {
259                    si->ch[ch].gran[0].region0_count = 7; /* MI 8; */
260                    si->ch[ch].gran[0].region1_count = 13;
261                }
262            }
263            else
264            {
265                tmp = getbits_crc(inputStream, 22, crc, info->error_protection);
266
267                si->ch[ch].gran[0].table_select[0] = (tmp << 10) >> 27;   /* 5 */;
268                si->ch[ch].gran[0].table_select[1] = (tmp << 15) >> 27;   /* 5 */;
269                si->ch[ch].gran[0].table_select[2] = (tmp << 20) >> 27;   /* 5 */;
270
271                si->ch[ch].gran[0].region0_count   = (tmp << 25) >> 28;   /* 4 */;
272                si->ch[ch].gran[0].region1_count   = (tmp << 29) >> 29;   /* 3 */;
273
274                si->ch[ch].gran[0].block_type      = 0;
275            }
276
277            tmp = getbits_crc(inputStream, 2, crc, info->error_protection);
278            si->ch[ch].gran[0].scalefac_scale     =  tmp >> 1;  /* 1 */
279            si->ch[ch].gran[0].count1table_select =  tmp & 1;  /* 1 */
280
281        }
282    }
283    return (NO_DECODING_ERROR);
284}
285
286