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_crc.cpp 25 26 Functions: 27 getbits_crc 28 calculate_crc 29 30 Date: 09/21/2007 31 32 ------------------------------------------------------------------------------ 33 REVISION HISTORY 34 35 36 Description: 37 38 ------------------------------------------------------------------------------ 39 INPUT AND OUTPUT DEFINITIONS 40 41 getbits_crc 42 43 Input 44 tbits *inputStream, bit stream structure 45 int32 neededBits, number of bits to read from the bit stream 46 uint32 *crc, memory location holding calculated crc value 47 uint32 crc_enabled flag to enable/disable crc checking 48 49 ------------------------------------------------------------------------------ 50 INPUT AND OUTPUT DEFINITIONS 51 52 calculate_crc 53 54 Input 55 uint32 data, data vector 56 uint32 length, number of element upon the crc will be calculated 57 uint32 *crc, memory location holding calculated crc value 58 59 ------------------------------------------------------------------------------ 60 FUNCTION DESCRIPTION 61 62 63 ------------------------------------------------------------------------------ 64 REQUIREMENTS 65 66 67 ------------------------------------------------------------------------------ 68 REFERENCES 69 70 [1] ISO MPEG Audio Subgroup Software Simulation Group (1996) 71 ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension 72 73 ------------------------------------------------------------------------------ 74 PSEUDO-CODE 75 76 ------------------------------------------------------------------------------ 77 */ 78 79 80 /*---------------------------------------------------------------------------- 81 ; INCLUDES 82 ----------------------------------------------------------------------------*/ 83 84 #include "pvmp3_getbits.h" 85 #include "pvmp3_crc.h" 86 87 /*---------------------------------------------------------------------------- 88 ; MACROS 89 ; Define module specific macros here 90 ----------------------------------------------------------------------------*/ 91 92 93 /*---------------------------------------------------------------------------- 94 ; DEFINES 95 ; Include all pre-processor statements here. Include conditional 96 ; compile variables also. 97 ----------------------------------------------------------------------------*/ 98 99 /*---------------------------------------------------------------------------- 100 ; LOCAL FUNCTION DEFINITIONS 101 ; Function Prototype declaration 102 ----------------------------------------------------------------------------*/ 103 104 /*---------------------------------------------------------------------------- 105 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS 106 ; Variable declaration - defined here and used outside this module 107 ----------------------------------------------------------------------------*/ 108 109 /*---------------------------------------------------------------------------- 110 ; EXTERNAL FUNCTION REFERENCES 111 ; Declare functions defined elsewhere and referenced in this module 112 ----------------------------------------------------------------------------*/ 113 114 /*---------------------------------------------------------------------------- 115 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES 116 ; Declare variables used in this module but defined elsewhere 117 ----------------------------------------------------------------------------*/ 118 119 /*---------------------------------------------------------------------------- 120 ; FUNCTION CODE 121 ----------------------------------------------------------------------------*/ 122 123 uint32 getbits_crc(tmp3Bits *inputStream, /* bit stream structure */ 124 int32 neededBits, /* number of bits to read from the bit stream */ 125 uint32 *crc, 126 uint32 crc_enabled) 127 { 128 uint32 bits = getNbits(inputStream, neededBits); 129 130 if (crc_enabled) 131 { 132 calculate_crc(bits, neededBits, crc); 133 } 134 return(bits); 135 } 136 137 /*---------------------------------------------------------------------------- 138 ; FUNCTION CODE 139 ----------------------------------------------------------------------------*/ 140 141 void calculate_crc(uint32 data, 142 uint32 length, 143 uint32 *crc) 144 { 145 uint32 carry; 146 uint32 masking = 1 << length; 147 148 while ((masking >>= 1)) 149 { 150 carry = *crc & 0x8000; 151 *crc <<= 1; 152 if (!carry ^ !(data & masking)) 153 { 154 *crc ^= CRC16_POLYNOMIAL; 155 } 156 } 157 *crc &= 0xffff; 158 } 159 160 161 162