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_decode_header.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 tbits *inputStream, bit stream
39 mp3Header *info,
40 uint32 *crc
41 Returns
42
43 mp3Header *info, structure holding the parsed mp3 header info
44 uint32 *crc initialized crc computation
45
46
47 ------------------------------------------------------------------------------
48 FUNCTION DESCRIPTION
49
50 gets mp3 header information
51
52 ------------------------------------------------------------------------------
53 REQUIREMENTS
54
55
56 ------------------------------------------------------------------------------
57 REFERENCES
58
59 [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
60 ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
61
62 ------------------------------------------------------------------------------
63 PSEUDO-CODE
64
65 ------------------------------------------------------------------------------
66 */
67
68
69 /*----------------------------------------------------------------------------
70 ; INCLUDES
71 ----------------------------------------------------------------------------*/
72
73 #include "pvmp3_decode_header.h"
74 #include "pvmp3_crc.h"
75 #include "pvmp3_getbits.h"
76 #include "pvmp3_seek_synch.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_decode_header(tmp3Bits * inputStream,mp3Header * info,uint32 * crc)115 ERROR_CODE pvmp3_decode_header(tmp3Bits *inputStream,
116 mp3Header *info,
117 uint32 *crc)
118 {
119
120 ERROR_CODE err = NO_DECODING_ERROR;
121 uint32 temp;
122
123 /*
124 * Verify that at least the header is complete
125 */
126 if (inputStream->inputBufferCurrentLength < (SYNC_WORD_LNGTH + 21))
127 {
128 return NO_ENOUGH_MAIN_DATA_ERROR;
129 }
130
131 /*
132 * MPEG Audio Version ID
133 */
134 temp = getUpTo17bits(inputStream, SYNC_WORD_LNGTH);
135 if ((temp & SYNC_WORD) != SYNC_WORD)
136 {
137 err = pvmp3_header_sync(inputStream);
138
139 if (err != NO_DECODING_ERROR)
140 {
141 return err;
142 }
143 }
144
145 temp = getNbits(inputStream, 21); // to avoid multiple bitstream accesses
146
147
148 switch (temp >> 19) /* 2 */
149 {
150 case 0:
151 info->version_x = MPEG_2_5;
152 break;
153 case 2:
154 info->version_x = MPEG_2;
155 break;
156 case 3:
157 info->version_x = MPEG_1;
158 break;
159 default:
160 info->version_x = INVALID_VERSION;
161 err = UNSUPPORTED_LAYER;
162 break;
163 }
164
165 info->layer_description = 4 - ((temp << 13) >> 30); /* 2 */
166 info->error_protection = !((temp << 15) >> 31); /* 1 */
167
168 if (info->error_protection)
169 {
170 *crc = 0xffff; /* CRC start value */
171 calculate_crc((temp << 16) >> 16, 16, crc);
172 }
173
174 info->bitrate_index = (temp << 16) >> 28; /* 4 */
175 info->sampling_frequency = (temp << 20) >> 30; /* 2 */
176 info->padding = (temp << 22) >> 31; /* 1 */
177 info->extension = (temp << 23) >> 31; /* 1 */
178 info->mode = (temp << 24) >> 30; /* 2 */
179 info->mode_ext = (temp << 26) >> 30; /* 2 */
180 info->copyright = (temp << 27) >> 31; /* 1 */
181 info->original = (temp << 28) >> 31; /* 1 */
182 info->emphasis = (temp << 30) >> 30; /* 2 */
183
184
185 if (!info->bitrate_index || info->sampling_frequency == 3)
186 {
187 err = UNSUPPORTED_FREE_BITRATE;
188 }
189
190 return(err);
191 }
192
193