1 /* 2 * Copyright (C) 2009 The Android Open Source Project 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 express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /*------------------------------------------------------------------------------ 18 19 Table of contents 20 21 1. Include headers 22 2. External compiler flags 23 3. Module defines 24 4. Local function prototypes 25 5. Functions 26 h264bsdGetBits 27 h264bsdShowBits32 28 h264bsdFlushBits 29 h264bsdIsByteAligned 30 31 ------------------------------------------------------------------------------*/ 32 33 /*------------------------------------------------------------------------------ 34 1. Include headers 35 ------------------------------------------------------------------------------*/ 36 37 #include "h264bsd_util.h" 38 #include "h264bsd_stream.h" 39 40 /*------------------------------------------------------------------------------ 41 2. External compiler flags 42 -------------------------------------------------------------------------------- 43 44 -------------------------------------------------------------------------------- 45 3. Module defines 46 ------------------------------------------------------------------------------*/ 47 48 /*------------------------------------------------------------------------------ 49 4. Local function prototypes 50 ------------------------------------------------------------------------------*/ 51 52 /*------------------------------------------------------------------------------ 53 54 Function: h264bsdGetBits 55 56 Functional description: 57 Read and remove bits from the stream buffer. 58 59 Input: 60 pStrmData pointer to stream data structure 61 numBits number of bits to read 62 63 Output: 64 none 65 66 Returns: 67 bits read from stream 68 END_OF_STREAM if not enough bits left 69 70 ------------------------------------------------------------------------------*/ 71 72 u32 h264bsdGetBits(strmData_t *pStrmData, u32 numBits) 73 { 74 75 u32 out; 76 77 ASSERT(pStrmData); 78 ASSERT(numBits < 32); 79 80 out = h264bsdShowBits32(pStrmData) >> (32 - numBits); 81 82 if (h264bsdFlushBits(pStrmData, numBits) == HANTRO_OK) 83 { 84 return(out); 85 } 86 else 87 { 88 return(END_OF_STREAM); 89 } 90 91 } 92 93 /*------------------------------------------------------------------------------ 94 95 Function: h264bsdShowBits32 96 97 Functional description: 98 Read 32 bits from the stream buffer. Buffer is left as it is, i.e. 99 no bits are removed. First bit read from the stream is the MSB of 100 the return value. If there is not enough bits in the buffer -> 101 bits beyong the end of the stream are set to '0' in the return 102 value. 103 104 Input: 105 pStrmData pointer to stream data structure 106 107 Output: 108 none 109 110 Returns: 111 bits read from stream 112 113 ------------------------------------------------------------------------------*/ 114 115 u32 h264bsdShowBits32(strmData_t *pStrmData) 116 { 117 118 i32 bits, shift; 119 u32 out; 120 u8 *pStrm; 121 122 ASSERT(pStrmData); 123 ASSERT(pStrmData->pStrmCurrPos); 124 ASSERT(pStrmData->bitPosInWord < 8); 125 ASSERT(pStrmData->bitPosInWord == 126 (pStrmData->strmBuffReadBits & 0x7)); 127 128 pStrm = pStrmData->pStrmCurrPos; 129 130 /* number of bits left in the buffer */ 131 bits = (i32)pStrmData->strmBuffSize*8 - (i32)pStrmData->strmBuffReadBits; 132 133 /* at least 32-bits in the buffer */ 134 if (bits >= 32) 135 { 136 u32 bitPosInWord = pStrmData->bitPosInWord; 137 out = ((u32)pStrm[0] << 24) | ((u32)pStrm[1] << 16) | 138 ((u32)pStrm[2] << 8) | ((u32)pStrm[3]); 139 140 if (bitPosInWord) 141 { 142 u32 byte = (u32)pStrm[4]; 143 u32 tmp = (8-bitPosInWord); 144 out <<= bitPosInWord; 145 out |= byte>>tmp; 146 } 147 return (out); 148 } 149 /* at least one bit in the buffer */ 150 else if (bits > 0) 151 { 152 shift = (i32)(24 + pStrmData->bitPosInWord); 153 out = (u32)(*pStrm++) << shift; 154 bits -= (i32)(8 - pStrmData->bitPosInWord); 155 while (bits > 0) 156 { 157 shift -= 8; 158 out |= (u32)(*pStrm++) << shift; 159 bits -= 8; 160 } 161 return (out); 162 } 163 else 164 return (0); 165 166 } 167 168 /*------------------------------------------------------------------------------ 169 170 Function: h264bsdFlushBits 171 172 Functional description: 173 Remove bits from the stream buffer 174 175 Input: 176 pStrmData pointer to stream data structure 177 numBits number of bits to remove 178 179 Output: 180 none 181 182 Returns: 183 HANTRO_OK success 184 END_OF_STREAM not enough bits left 185 186 ------------------------------------------------------------------------------*/ 187 #ifndef H264DEC_NEON 188 u32 h264bsdFlushBits(strmData_t *pStrmData, u32 numBits) 189 { 190 191 ASSERT(pStrmData); 192 ASSERT(pStrmData->pStrmBuffStart); 193 ASSERT(pStrmData->pStrmCurrPos); 194 ASSERT(pStrmData->bitPosInWord < 8); 195 ASSERT(pStrmData->bitPosInWord == (pStrmData->strmBuffReadBits & 0x7)); 196 197 pStrmData->strmBuffReadBits += numBits; 198 pStrmData->bitPosInWord = pStrmData->strmBuffReadBits & 0x7; 199 if ( (pStrmData->strmBuffReadBits ) <= (8*pStrmData->strmBuffSize) ) 200 { 201 pStrmData->pStrmCurrPos = pStrmData->pStrmBuffStart + 202 (pStrmData->strmBuffReadBits >> 3); 203 return(HANTRO_OK); 204 } 205 else 206 return(END_OF_STREAM); 207 208 } 209 #endif 210 /*------------------------------------------------------------------------------ 211 212 Function: h264bsdIsByteAligned 213 214 Functional description: 215 Check if current stream position is byte aligned. 216 217 Inputs: 218 pStrmData pointer to stream data structure 219 220 Outputs: 221 none 222 223 Returns: 224 TRUE stream is byte aligned 225 FALSE stream is not byte aligned 226 227 ------------------------------------------------------------------------------*/ 228 229 u32 h264bsdIsByteAligned(strmData_t *pStrmData) 230 { 231 232 /* Variables */ 233 234 /* Code */ 235 236 if (!pStrmData->bitPosInWord) 237 return(HANTRO_TRUE); 238 else 239 return(HANTRO_FALSE); 240 241 } 242 243