• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
37 Input
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 #include "pvmp3_getbits.h"
77 
78 
79 /*----------------------------------------------------------------------------
80 ; MACROS
81 ; Define module specific macros here
82 ----------------------------------------------------------------------------*/
83 
84 
85 /*----------------------------------------------------------------------------
86 ; DEFINES
87 ; Include all pre-processor statements here. Include conditional
88 ; compile variables also.
89 ----------------------------------------------------------------------------*/
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 ----------------------------------------------------------------------------*/
100 
101 /*----------------------------------------------------------------------------
102 ; EXTERNAL FUNCTION REFERENCES
103 ; Declare functions defined elsewhere and referenced in this module
104 ----------------------------------------------------------------------------*/
105 
106 /*----------------------------------------------------------------------------
107 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
108 ; Declare variables used in this module but defined elsewhere
109 ----------------------------------------------------------------------------*/
110 
111 /*----------------------------------------------------------------------------
112 ; FUNCTION CODE
113 ----------------------------------------------------------------------------*/
114 
pvmp3_get_side_info(tmp3Bits * inputStream,mp3SideInfo * si,mp3Header * info,uint32 * crc)115 ERROR_CODE pvmp3_get_side_info(tmp3Bits    *inputStream,
116                                mp3SideInfo *si,
117                                mp3Header   *info,
118                                uint32      *crc)
119 {
120     int32 ch, gr;
121     uint32 tmp;
122 
123     int stereo  = (info->mode == MPG_MD_MONO) ? 1 : 2;
124 
125     if (info->version_x == MPEG_1)
126     {
127         if (stereo == 1)
128         {
129             if (!bitsAvailable(inputStream, 14))
130             {
131                 return SIDE_INFO_ERROR;
132             }
133 
134             tmp = getbits_crc(inputStream, 14, crc, info->error_protection);
135             si->main_data_begin = (tmp << 18) >> 23;    /* 9 */
136             si->private_bits    = (tmp << 27) >> 27;    /* 5 */
137         }
138         else
139         {
140             if (!bitsAvailable(inputStream, 12))
141             {
142                 return SIDE_INFO_ERROR;
143             }
144 
145             tmp = getbits_crc(inputStream, 12, crc, info->error_protection);
146             si->main_data_begin = (tmp << 20) >> 23;    /* 9 */
147             si->private_bits    = (tmp << 29) >> 29;    /* 3 */
148 
149         }
150 
151         for (ch = 0; ch < stereo; ch++)
152         {
153             if (!bitsAvailable(inputStream, 4))
154             {
155                 return SIDE_INFO_ERROR;
156             }
157 
158             tmp = getbits_crc(inputStream, 4, crc, info->error_protection);
159             si->ch[ch].scfsi[0] = (tmp << 28) >> 31;    /* 1 */
160             si->ch[ch].scfsi[1] = (tmp << 29) >> 31;    /* 1 */
161             si->ch[ch].scfsi[2] = (tmp << 30) >> 31;    /* 1 */
162             si->ch[ch].scfsi[3] =  tmp & 1;         /* 1 */
163         }
164 
165         for (gr = 0; gr < 2 ; gr++)
166         {
167             for (ch = 0; ch < stereo; ch++)
168             {
169                 if (!bitsAvailable(inputStream, 34))
170                 {
171                     return SIDE_INFO_ERROR;
172                 }
173 
174                 si->ch[ch].gran[gr].part2_3_length    = getbits_crc(inputStream, 12, crc, info->error_protection);
175                 tmp = getbits_crc(inputStream, 22, crc, info->error_protection);
176 
177                 si->ch[ch].gran[gr].big_values            = (tmp << 10) >> 23;   /* 9 */
178                 si->ch[ch].gran[gr].global_gain        = (int32)((tmp << 19) >> 24) - 210; /* 8 */
179                 si->ch[ch].gran[gr].scalefac_compress     = (tmp << 27) >> 28;   /* 4 */
180                 si->ch[ch].gran[gr].window_switching_flag = tmp & 1;         /* 1 */
181 
182                 if (si->ch[ch].gran[gr].window_switching_flag)
183                 {
184                     if (!bitsAvailable(inputStream, 22))
185                     {
186                         return SIDE_INFO_ERROR;
187                     }
188 
189                     tmp = getbits_crc(inputStream, 22, crc, info->error_protection);
190 
191                     si->ch[ch].gran[gr].block_type       = (tmp << 10) >> 30;   /* 2 */;
192                     si->ch[ch].gran[gr].mixed_block_flag = (tmp << 12) >> 31;   /* 1 */;
193 
194                     si->ch[ch].gran[gr].table_select[0]  = (tmp << 13) >> 27;   /* 5 */;
195                     si->ch[ch].gran[gr].table_select[1]  = (tmp << 18) >> 27;   /* 5 */;
196 
197                     si->ch[ch].gran[gr].subblock_gain[0] = (tmp << 23) >> 29;   /* 3 */;
198                     si->ch[ch].gran[gr].subblock_gain[1] = (tmp << 26) >> 29;   /* 3 */;
199                     si->ch[ch].gran[gr].subblock_gain[2] = (tmp << 29) >> 29;   /* 3 */;
200 
201                     /* Set region_count parameters since they are implicit in this case. */
202 
203                     if (si->ch[ch].gran[gr].block_type == 0)
204                     {
205                         return(SIDE_INFO_ERROR);
206                     }
207                     else if ((si->ch[ch].gran[gr].block_type       == 2)
208                              && (si->ch[ch].gran[gr].mixed_block_flag == 0))
209                     {
210                         si->ch[ch].gran[gr].region0_count = 8; /* MI 9; */
211                         si->ch[ch].gran[gr].region1_count = 12;
212                     }
213                     else
214                     {
215                         si->ch[ch].gran[gr].region0_count = 7; /* MI 8; */
216                         si->ch[ch].gran[gr].region1_count = 13;
217                     }
218                 }
219                 else
220                 {
221                     if (!bitsAvailable(inputStream, 22))
222                     {
223                         return SIDE_INFO_ERROR;
224                     }
225 
226                     tmp = getbits_crc(inputStream, 22, crc, info->error_protection);
227 
228                     si->ch[ch].gran[gr].table_select[0] = (tmp << 10) >> 27;   /* 5 */;
229                     si->ch[ch].gran[gr].table_select[1] = (tmp << 15) >> 27;   /* 5 */;
230                     si->ch[ch].gran[gr].table_select[2] = (tmp << 20) >> 27;   /* 5 */;
231 
232                     si->ch[ch].gran[gr].region0_count   = (tmp << 25) >> 28;   /* 4 */;
233                     si->ch[ch].gran[gr].region1_count   = (tmp << 29) >> 29;   /* 3 */;
234 
235                     si->ch[ch].gran[gr].block_type      = 0;
236                 }
237 
238                 if (!bitsAvailable(inputStream, 3))
239                 {
240                     return SIDE_INFO_ERROR;
241                 }
242 
243                 tmp = getbits_crc(inputStream, 3, crc, info->error_protection);
244                 si->ch[ch].gran[gr].preflag            = (tmp << 29) >> 31;    /* 1 */
245                 si->ch[ch].gran[gr].scalefac_scale     = (tmp << 30) >> 31;    /* 1 */
246                 si->ch[ch].gran[gr].count1table_select =  tmp & 1;         /* 1 */
247             }
248         }
249     }
250     else /* Layer 3 LSF */
251     {
252         if (!bitsAvailable(inputStream, 8 + stereo))
253         {
254             return SIDE_INFO_ERROR;
255         }
256 
257         si->main_data_begin = getbits_crc(inputStream,      8, crc, info->error_protection);
258         si->private_bits    = getbits_crc(inputStream, stereo, crc, info->error_protection);
259 
260         for (ch = 0; ch < stereo; ch++)
261         {
262             if (!bitsAvailable(inputStream, 39))
263             {
264                 return SIDE_INFO_ERROR;
265             }
266 
267             tmp = getbits_crc(inputStream, 21, crc, info->error_protection);
268             si->ch[ch].gran[0].part2_3_length    = (tmp << 11) >> 20;  /* 12 */
269             si->ch[ch].gran[0].big_values        = (tmp << 23) >> 23;  /*  9 */
270 
271             tmp = getbits_crc(inputStream, 18, crc, info->error_protection);
272             si->ch[ch].gran[0].global_gain           = ((tmp << 14) >> 24) - 210;   /* 8 */
273             si->ch[ch].gran[0].scalefac_compress     = (tmp << 22) >> 23;   /* 9 */
274             si->ch[ch].gran[0].window_switching_flag = tmp & 1;         /* 1 */
275 
276             if (si->ch[ch].gran[0].window_switching_flag)
277             {
278 
279                 if (!bitsAvailable(inputStream, 22))
280                 {
281                     return SIDE_INFO_ERROR;
282                 }
283 
284                 tmp = getbits_crc(inputStream, 22, crc, info->error_protection);
285 
286                 si->ch[ch].gran[0].block_type       = (tmp << 10) >> 30;   /* 2 */;
287                 si->ch[ch].gran[0].mixed_block_flag = (tmp << 12) >> 31;   /* 1 */;
288 
289                 si->ch[ch].gran[0].table_select[0]  = (tmp << 13) >> 27;   /* 5 */;
290                 si->ch[ch].gran[0].table_select[1]  = (tmp << 18) >> 27;   /* 5 */;
291 
292                 si->ch[ch].gran[0].subblock_gain[0] = (tmp << 23) >> 29;   /* 3 */;
293                 si->ch[ch].gran[0].subblock_gain[1] = (tmp << 26) >> 29;   /* 3 */;
294                 si->ch[ch].gran[0].subblock_gain[2] = (tmp << 29) >> 29;   /* 3 */;
295 
296                 /* Set region_count parameters since they are implicit in this case. */
297 
298                 if (si->ch[ch].gran[0].block_type == 0)
299                 {
300                     return(SIDE_INFO_ERROR);
301                 }
302                 else if ((si->ch[ch].gran[0].block_type       == 2)
303                          && (si->ch[ch].gran[0].mixed_block_flag == 0))
304                 {
305                     si->ch[ch].gran[0].region0_count = 8; /* MI 9; */
306                     si->ch[ch].gran[0].region1_count = 12;
307                 }
308                 else
309                 {
310                     si->ch[ch].gran[0].region0_count = 7; /* MI 8; */
311                     si->ch[ch].gran[0].region1_count = 13;
312                 }
313             }
314             else
315             {
316                 if (!bitsAvailable(inputStream, 22))
317                 {
318                     return SIDE_INFO_ERROR;
319                 }
320 
321                 tmp = getbits_crc(inputStream, 22, crc, info->error_protection);
322 
323                 si->ch[ch].gran[0].table_select[0] = (tmp << 10) >> 27;   /* 5 */;
324                 si->ch[ch].gran[0].table_select[1] = (tmp << 15) >> 27;   /* 5 */;
325                 si->ch[ch].gran[0].table_select[2] = (tmp << 20) >> 27;   /* 5 */;
326 
327                 si->ch[ch].gran[0].region0_count   = (tmp << 25) >> 28;   /* 4 */;
328                 si->ch[ch].gran[0].region1_count   = (tmp << 29) >> 29;   /* 3 */;
329 
330                 si->ch[ch].gran[0].block_type      = 0;
331             }
332 
333             if (!bitsAvailable(inputStream, 2))
334             {
335                 return SIDE_INFO_ERROR;
336             }
337 
338             tmp = getbits_crc(inputStream, 2, crc, info->error_protection);
339             si->ch[ch].gran[0].scalefac_scale     =  tmp >> 1;  /* 1 */
340             si->ch[ch].gran[0].count1table_select =  tmp & 1;  /* 1 */
341 
342         }
343     }
344     return (NO_DECODING_ERROR);
345 }
346 
347