| 1 | /****************************************************************************** |
|---|
| 2 | *_Copyright (c) 2009 Digital Stream Technology Inc. All Rights Reserved. |
|---|
| 3 | * |
|---|
| 4 | * Module: dsthalBitBuffer.c |
|---|
| 5 | * |
|---|
| 6 | * Description |
|---|
| 7 | * Utility for extracting bits from a buffer |
|---|
| 8 | * |
|---|
| 9 | ******************************************************************************/ |
|---|
| 10 | |
|---|
| 11 | #include "dsthalcommon.h" |
|---|
| 12 | #include "dsthalPsiBitBuffer.h" |
|---|
| 13 | |
|---|
| 14 | #include <stdlib.h> |
|---|
| 15 | #include <string.h> |
|---|
| 16 | |
|---|
| 17 | #ifdef DMALLOC |
|---|
| 18 | #include <dmalloc.h> |
|---|
| 19 | #endif |
|---|
| 20 | |
|---|
| 21 | /****************************************************************************** |
|---|
| 22 | * Global variable declaration |
|---|
| 23 | ******************************************************************************/ |
|---|
| 24 | |
|---|
| 25 | /****************************************************************************** |
|---|
| 26 | * Imported variable declaration |
|---|
| 27 | ******************************************************************************/ |
|---|
| 28 | |
|---|
| 29 | /****************************************************************************** |
|---|
| 30 | * Imported function declaration |
|---|
| 31 | ******************************************************************************/ |
|---|
| 32 | |
|---|
| 33 | /****************************************************************************** |
|---|
| 34 | * Local definitions |
|---|
| 35 | ******************************************************************************/ |
|---|
| 36 | |
|---|
| 37 | /****************************************************************************** |
|---|
| 38 | * Local typedefs |
|---|
| 39 | ******************************************************************************/ |
|---|
| 40 | struct bitBuffer { |
|---|
| 41 | DS_U8* buffer; |
|---|
| 42 | DS_U32 bufSize; |
|---|
| 43 | DS_U32 bitOffset; |
|---|
| 44 | DS_BOOL overrunError; |
|---|
| 45 | }bitBuffer; |
|---|
| 46 | |
|---|
| 47 | /****************************************************************************** |
|---|
| 48 | * Local variables declaration |
|---|
| 49 | ******************************************************************************/ |
|---|
| 50 | static const DS_U32 bitMaskTable[] = { |
|---|
| 51 | 0x00000000,0x00000001,0x00000003,0x00000007,0x0000000F,0x0000001F,0x0000003F,0x0000007F, |
|---|
| 52 | 0x000000FF,0x000001FF,0x000003FF,0x000007FF,0x00000FFF,0x00001FFF,0x00003FFF,0x00007FFF, |
|---|
| 53 | 0x0000FFFF,0x0001FFFF,0x0003FFFF,0x0007FFFF,0x000FFFFF,0x001FFFFF,0x003FFFFF,0x007FFFFF, |
|---|
| 54 | 0x00FFFFFF,0x01FFFFFF,0x03FFFFFF,0x07FFFFFF,0x0FFFFFFF,0x1FFFFFFF,0x3FFFFFFF,0x7FFFFFFF, |
|---|
| 55 | 0xFFFFFFFF |
|---|
| 56 | }; |
|---|
| 57 | static const DS_U8 shiftTable1[] = {8,1,2,3,4,5,6,7}; |
|---|
| 58 | static const DS_U8 shiftTable2[] = {0,7,6,5,4,3,2,1}; |
|---|
| 59 | |
|---|
| 60 | /****************************************************************************** |
|---|
| 61 | * Local function prototypes |
|---|
| 62 | ******************************************************************************/ |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /*========================================================================= |
|---|
| 66 | ErrCode bitBufferCreate (bitBufferPtr_t *instance, DS_U8 *buffer, DS_U32 bufSize) |
|---|
| 67 | |
|---|
| 68 | *instance: bitBuffer instance passed back to caller. |
|---|
| 69 | *buffer: pointer to a byte buffer. |
|---|
| 70 | bufSize: size of the buffer (in bytes). |
|---|
| 71 | |
|---|
| 72 | Creates a bitBuffer. After creation, the bitBufferGetBits() function can |
|---|
| 73 | be called to extract bits. |
|---|
| 74 | =========================================================================*/ |
|---|
| 75 | DHL_RESULT bitBufferCreate (bitBufferPtr_t *instance, DS_U8 *buffer, DS_U32 bufSize) |
|---|
| 76 | { |
|---|
| 77 | bitBufferPtr_t bitBufferPtr; |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | /* create bitBuffer structure */ |
|---|
| 81 | bitBufferPtr = (bitBufferPtr_t)malloc(sizeof(bitBuffer_t)); |
|---|
| 82 | if (bitBufferPtr == NULL) { |
|---|
| 83 | return DHL_FAIL_OUT_OF_RESOURCE; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | memset( bitBufferPtr , 0 , sizeof(bitBuffer_t) ); |
|---|
| 87 | |
|---|
| 88 | /* Initialize */ |
|---|
| 89 | bitBufferPtr->buffer = buffer; |
|---|
| 90 | bitBufferPtr->bufSize = bufSize; |
|---|
| 91 | bitBufferPtr->bitOffset = 0; |
|---|
| 92 | |
|---|
| 93 | /* pass instance to user */ |
|---|
| 94 | *instance = bitBufferPtr; |
|---|
| 95 | |
|---|
| 96 | return (DHL_OK); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | /*========================================================================= |
|---|
| 101 | DHL_RESULT bitBufferDestroy (bitBufferPtr_t instance) |
|---|
| 102 | |
|---|
| 103 | instance: bitBuffer instance. |
|---|
| 104 | |
|---|
| 105 | Deletes the bitBuffer. DOES NOT free the buffer passed by the user in the |
|---|
| 106 | function bitBufferCreate()!! |
|---|
| 107 | =========================================================================*/ |
|---|
| 108 | DHL_RESULT bitBufferDestroy (bitBufferPtr_t instance) |
|---|
| 109 | { |
|---|
| 110 | //AtiCore_MemFree(instance); |
|---|
| 111 | free(instance); |
|---|
| 112 | return (DHL_OK); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | /*========================================================================= |
|---|
| 117 | DS_U32 bitBufferGetBits (bitBufferPtr_t bitBufferPtr, DS_U8 numberOfBits) |
|---|
| 118 | |
|---|
| 119 | bitBufferPtr: bitBuffer instance. |
|---|
| 120 | numberOfBits: number of bits to be read from bitBuffer. |
|---|
| 121 | |
|---|
| 122 | Extracts the specified number of bits from the bitBuffer. The number of |
|---|
| 123 | bits must be less than or equal to 32. If more bits are requested than |
|---|
| 124 | are present in the bitBuffer, the overrunError flag is set. |
|---|
| 125 | =========================================================================*/ |
|---|
| 126 | DS_U32 bitBufferGetBits (bitBufferPtr_t bitBufferPtr, DS_U8 numberOfBits) |
|---|
| 127 | { |
|---|
| 128 | DS_U32 returnBits = 0; |
|---|
| 129 | DS_U8 bitIndex; |
|---|
| 130 | DS_U8 newBitIndexDiv8,newBitIndexMod8; |
|---|
| 131 | DS_U8 readByteCount; |
|---|
| 132 | DS_U8 *p; |
|---|
| 133 | DS_U8 i; |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | if (numberOfBits > (8*bitBufferPtr->bufSize - bitBufferPtr->bitOffset)) { |
|---|
| 137 | |
|---|
| 138 | bitBufferPtr->overrunError = _TRUE_; |
|---|
| 139 | bitBufferPtr->bitOffset = 8*bitBufferPtr->bufSize; |
|---|
| 140 | returnBits = 0; |
|---|
| 141 | goto GetBitsReturn; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | bitIndex = bitBufferPtr->bitOffset & 0x07; |
|---|
| 145 | newBitIndexDiv8 = (bitIndex+numberOfBits) >> 3; |
|---|
| 146 | newBitIndexMod8 = (bitIndex+numberOfBits) & 0x07; |
|---|
| 147 | readByteCount = newBitIndexDiv8 + (newBitIndexMod8 ? 1:0); |
|---|
| 148 | |
|---|
| 149 | p = bitBufferPtr->buffer + (bitBufferPtr->bitOffset >> 3); |
|---|
| 150 | |
|---|
| 151 | /* copy first byte */ |
|---|
| 152 | returnBits = *p++; |
|---|
| 153 | if (readByteCount < 2) { |
|---|
| 154 | /* shift bits right */ |
|---|
| 155 | returnBits = returnBits >> shiftTable2[newBitIndexMod8]; |
|---|
| 156 | } |
|---|
| 157 | else { /* more bytes */ |
|---|
| 158 | for (i=0; i < readByteCount-2; i++) { |
|---|
| 159 | /* just copying 'middle' bytes */ |
|---|
| 160 | returnBits = (returnBits << 8) | *p++; |
|---|
| 161 | } |
|---|
| 162 | /* copy last byte */ |
|---|
| 163 | returnBits = (returnBits << shiftTable1[newBitIndexMod8]) | ((*p) >> shiftTable2[newBitIndexMod8]); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | returnBits &= bitMaskTable[numberOfBits]; |
|---|
| 167 | bitBufferPtr->bitOffset += numberOfBits; |
|---|
| 168 | |
|---|
| 169 | GetBitsReturn: |
|---|
| 170 | return (returnBits); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | /*========================================================================= |
|---|
| 175 | void bitBufferSkipBits (bitBufferPtr_t bitBufferPtr, DS_U16 numberOfBits) |
|---|
| 176 | |
|---|
| 177 | bitBufferPtr: bitBuffer instance. |
|---|
| 178 | numberOfBits: number of bits to be skipped. |
|---|
| 179 | |
|---|
| 180 | Skips the specified number of bits in the bitBuffer. This operation is |
|---|
| 181 | faster than extracting bits and can be used when a bit field is ignored. |
|---|
| 182 | =========================================================================*/ |
|---|
| 183 | void bitBufferSkipBits (bitBufferPtr_t bitBufferPtr, DS_U16 numberOfBits) |
|---|
| 184 | { |
|---|
| 185 | bitBufferPtr->bitOffset += numberOfBits; |
|---|
| 186 | if (8*bitBufferPtr->bufSize < bitBufferPtr->bitOffset) { |
|---|
| 187 | bitBufferPtr->overrunError = _TRUE_; |
|---|
| 188 | } |
|---|
| 189 | return; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | /*========================================================================= |
|---|
| 194 | DS_U8* bitBufferGetBytePointer (INT32 instance) |
|---|
| 195 | |
|---|
| 196 | bitBufferPtr: bitBuffer instance. |
|---|
| 197 | |
|---|
| 198 | Returns a pointer to the current byte offset. |
|---|
| 199 | =========================================================================*/ |
|---|
| 200 | DS_U8* bitBufferGetBytePointer (bitBufferPtr_t bitBufferPtr) |
|---|
| 201 | { |
|---|
| 202 | return (bitBufferPtr->buffer + (bitBufferPtr->bitOffset >> 3)); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | /*========================================================================= |
|---|
| 207 | DS_BOOL bitBufferCheckError (INT32 instance) |
|---|
| 208 | |
|---|
| 209 | bitBufferPtr_t: bitBuffer instance. |
|---|
| 210 | |
|---|
| 211 | Returns '_TRUE_' if the bitBuffer is in an overrun error state. |
|---|
| 212 | =========================================================================*/ |
|---|
| 213 | DS_BOOL bitBufferCheckError (bitBufferPtr_t bitBufferPtr) |
|---|
| 214 | { |
|---|
| 215 | return (bitBufferPtr->overrunError); |
|---|
| 216 | } |
|---|