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 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 * Note that SYNC_WORD_LNGTH is in unit of bits, but inputBufferCurrentLength 126 * is in unit of bytes. 127 */ 128 if (inputStream->inputBufferCurrentLength < ((SYNC_WORD_LNGTH + 21) >> 3)) 129 { 130 return NO_ENOUGH_MAIN_DATA_ERROR; 131 } 132 133 /* 134 * MPEG Audio Version ID 135 */ 136 temp = getUpTo17bits(inputStream, SYNC_WORD_LNGTH); 137 if ((temp & SYNC_WORD) != SYNC_WORD) 138 { 139 err = pvmp3_header_sync(inputStream); 140 141 if (err != NO_DECODING_ERROR) 142 { 143 return err; 144 } 145 } 146 147 temp = getNbits(inputStream, 21); // to avoid multiple bitstream accesses 148 149 150 switch (temp >> 19) /* 2 */ 151 { 152 case 0: 153 info->version_x = MPEG_2_5; 154 break; 155 case 2: 156 info->version_x = MPEG_2; 157 break; 158 case 3: 159 info->version_x = MPEG_1; 160 break; 161 default: 162 info->version_x = INVALID_VERSION; 163 err = UNSUPPORTED_LAYER; 164 break; 165 } 166 167 info->layer_description = 4 - ((temp << 13) >> 30); /* 2 */ 168 info->error_protection = !((temp << 15) >> 31); /* 1 */ 169 170 if (info->error_protection) 171 { 172 *crc = 0xffff; /* CRC start value */ 173 calculate_crc((temp << 16) >> 16, 16, crc); 174 } 175 176 info->bitrate_index = (temp << 16) >> 28; /* 4 */ 177 info->sampling_frequency = (temp << 20) >> 30; /* 2 */ 178 info->padding = (temp << 22) >> 31; /* 1 */ 179 info->extension = (temp << 23) >> 31; /* 1 */ 180 info->mode = (temp << 24) >> 30; /* 2 */ 181 info->mode_ext = (temp << 26) >> 30; /* 2 */ 182 info->copyright = (temp << 27) >> 31; /* 1 */ 183 info->original = (temp << 28) >> 31; /* 1 */ 184 info->emphasis = (temp << 30) >> 30; /* 2 */ 185 186 187 if (!info->bitrate_index || info->sampling_frequency == 3) 188 { 189 err = UNSUPPORTED_FREE_BITRATE; 190 } 191 192 return(err); 193 } 194 195