| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2002, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bcmDccBits.c $ |
|---|
| 11 | * $brcm_Revision: \main\SanJose_MSTV_Devel\1 $ |
|---|
| 12 | * $brcm_Date: 3/12/02 4:46p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * This module contains functions useful for extracting bits |
|---|
| 17 | * from a byte stream. |
|---|
| 18 | * |
|---|
| 19 | * Revision History: |
|---|
| 20 | * |
|---|
| 21 | * $brcm_Log: $ |
|---|
| 22 | * |
|---|
| 23 | * \main\SanJose_MSTV_Devel\1 3/12/02 4:46p erikg |
|---|
| 24 | * Merge from SanJose_MSTV_Devel_erikg_mpegccxport2_2002_03_06_1118_38 |
|---|
| 25 | * |
|---|
| 26 | * \main\SanJose_MSTV_Devel_erikg_mpegccxport2_2002_03_06_1118_38\1 3/6/02 11:29a erikg |
|---|
| 27 | * new source files |
|---|
| 28 | * |
|---|
| 29 | ***************************************************************************/ |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | /************************************************************************* |
|---|
| 33 | * |
|---|
| 34 | * MODULE DETAILS: |
|---|
| 35 | * |
|---|
| 36 | * The functions in this module operate on bits in a byte buffer (array). |
|---|
| 37 | * Each function is given a pointer to the buffer and a byte offset and a |
|---|
| 38 | * bit offset indicating the current position in the buffer. The byte/bit |
|---|
| 39 | * offsets are either ints or int pointers, depending on whether or not |
|---|
| 40 | * the current position is to be updated. |
|---|
| 41 | * |
|---|
| 42 | * It is assumed that the first bit to be operated on is in the msb position, |
|---|
| 43 | * that is, 0x80. |
|---|
| 44 | * |
|---|
| 45 | *************************************************************************/ |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | #include "bcmDccBits.h" |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | /************************************************************************* |
|---|
| 52 | * |
|---|
| 53 | * FUNCTION: getnextbit |
|---|
| 54 | * |
|---|
| 55 | * Inputs: |
|---|
| 56 | * pBuf - stream buffer |
|---|
| 57 | * pbyteoff - current byte offset into pBuf |
|---|
| 58 | * pbitoff - bit offset into current byte |
|---|
| 59 | * |
|---|
| 60 | * Outputs: |
|---|
| 61 | * pbyteoff - current byte offset into pBuf |
|---|
| 62 | * pbitoff - bit offset into current byte |
|---|
| 63 | * |
|---|
| 64 | * Returns: the requested bit, 0 or 1 |
|---|
| 65 | * |
|---|
| 66 | * Description: |
|---|
| 67 | * |
|---|
| 68 | * This function returns the next bit and increments the position. |
|---|
| 69 | * |
|---|
| 70 | *************************************************************************/ |
|---|
| 71 | unsigned int |
|---|
| 72 | getnextbit(unsigned char * pBuf, int * pbyteoff, int * pbitoff) |
|---|
| 73 | { |
|---|
| 74 | unsigned char mask ; |
|---|
| 75 | unsigned char maskedbyte ; |
|---|
| 76 | |
|---|
| 77 | /* |
|---|
| 78 | * find the bit in question |
|---|
| 79 | */ |
|---|
| 80 | mask = (0x80 >> *pbitoff) ; |
|---|
| 81 | maskedbyte = pBuf[*pbyteoff] & mask ; |
|---|
| 82 | |
|---|
| 83 | /* |
|---|
| 84 | * update the position |
|---|
| 85 | */ |
|---|
| 86 | if ( ++(*pbitoff) == 8 ) |
|---|
| 87 | { |
|---|
| 88 | *pbitoff = 0 ; |
|---|
| 89 | (*pbyteoff)++ ; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /* |
|---|
| 93 | * return either a 1 or 0 |
|---|
| 94 | */ |
|---|
| 95 | return( maskedbyte ? 1 : 0 ) ; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /************************************************************************* |
|---|
| 99 | * |
|---|
| 100 | * FUNCTION: getnextbits |
|---|
| 101 | * |
|---|
| 102 | * Inputs: |
|---|
| 103 | * pBuf - stream buffer |
|---|
| 104 | * pbyteoff - current byte offset into pBuf |
|---|
| 105 | * pbitoff - bit offset into current byte |
|---|
| 106 | * numbits - number of bits to get (max 32) |
|---|
| 107 | * |
|---|
| 108 | * Outputs: |
|---|
| 109 | * pbyteoff - current byte offset into pBuf |
|---|
| 110 | * pbitoff - bit offset into current byte |
|---|
| 111 | * |
|---|
| 112 | * Returns: the requested bits, lsb justified |
|---|
| 113 | * |
|---|
| 114 | * Description: |
|---|
| 115 | * |
|---|
| 116 | * This function returns the requested number of bits and |
|---|
| 117 | * updates the position. |
|---|
| 118 | * |
|---|
| 119 | *************************************************************************/ |
|---|
| 120 | unsigned int |
|---|
| 121 | getnextbits(unsigned char * pBuf, int * pbyteoff, int * pbitoff, int numbits) |
|---|
| 122 | { |
|---|
| 123 | unsigned int retbits = 0 ; |
|---|
| 124 | unsigned int newbit ; |
|---|
| 125 | |
|---|
| 126 | while ( numbits-- ) |
|---|
| 127 | { |
|---|
| 128 | newbit = getnextbit(pBuf, pbyteoff, pbitoff) ; |
|---|
| 129 | retbits = (retbits << 1) | newbit ; |
|---|
| 130 | } |
|---|
| 131 | return(retbits) ; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /************************************************************************* |
|---|
| 135 | * |
|---|
| 136 | * FUNCTION: getnextbits_rev |
|---|
| 137 | * |
|---|
| 138 | * Inputs: |
|---|
| 139 | * pBuf - stream buffer |
|---|
| 140 | * pbyteoff - current byte offset into pBuf |
|---|
| 141 | * pbitoff - bit offset into current byte |
|---|
| 142 | * numbits - number of bits to get (max 32) |
|---|
| 143 | * |
|---|
| 144 | * Outputs: |
|---|
| 145 | * pbyteoff - current byte offset into pBuf |
|---|
| 146 | * pbitoff - bit offset into current byte |
|---|
| 147 | * |
|---|
| 148 | * Returns: the requested bits, bit reversed, lsb justified |
|---|
| 149 | * |
|---|
| 150 | * Description: |
|---|
| 151 | * |
|---|
| 152 | * This function returns the requested number of bits in reverse |
|---|
| 153 | * order and updates the position. |
|---|
| 154 | * |
|---|
| 155 | *************************************************************************/ |
|---|
| 156 | unsigned int |
|---|
| 157 | getnextbits_rev(unsigned char * pBuf, int * pbyteoff, int * pbitoff, int numbits) |
|---|
| 158 | { |
|---|
| 159 | unsigned int retbits = 0 ; |
|---|
| 160 | unsigned int newbit ; |
|---|
| 161 | unsigned int bitmask = 1 ; |
|---|
| 162 | |
|---|
| 163 | while ( numbits-- ) |
|---|
| 164 | { |
|---|
| 165 | newbit = getnextbit(pBuf, pbyteoff, pbitoff) ; |
|---|
| 166 | if ( newbit ) |
|---|
| 167 | retbits |= bitmask ; |
|---|
| 168 | bitmask <<= 1 ; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | return(retbits) ; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /************************************************************************* |
|---|
| 175 | * |
|---|
| 176 | * FUNCTION: nextbits |
|---|
| 177 | * |
|---|
| 178 | * Inputs: |
|---|
| 179 | * pBuf - stream buffer |
|---|
| 180 | * byteoff - current byte offset into pBuf |
|---|
| 181 | * bitoff - bit offset into current byte |
|---|
| 182 | * numbits - number of bits to get (max 32) |
|---|
| 183 | * |
|---|
| 184 | * Outputs: |
|---|
| 185 | * <none> |
|---|
| 186 | * |
|---|
| 187 | * Returns: the requested bits, lsb justified |
|---|
| 188 | * |
|---|
| 189 | * Description: |
|---|
| 190 | * |
|---|
| 191 | * This function returns the requested number of bits but does |
|---|
| 192 | * not update the position. |
|---|
| 193 | * |
|---|
| 194 | *************************************************************************/ |
|---|
| 195 | unsigned int |
|---|
| 196 | nextbits(unsigned char * pBuf, int byteoff, int bitoff, int numbits) |
|---|
| 197 | { |
|---|
| 198 | return( getnextbits(pBuf, &byteoff, &bitoff, numbits) ) ; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | /************************************************************************* |
|---|
| 202 | * |
|---|
| 203 | * FUNCTION: next_start_code |
|---|
| 204 | * |
|---|
| 205 | * Inputs: |
|---|
| 206 | * pBuf - stream buffer |
|---|
| 207 | * pbyteoff - current byte offset into pBuf |
|---|
| 208 | * pbitoff - bit offset into current byte |
|---|
| 209 | * numbytes - max bytes to scan forward |
|---|
| 210 | * |
|---|
| 211 | * Outputs: |
|---|
| 212 | * pbyteoff - current byte offset into pBuf |
|---|
| 213 | * pbitoff - bit offset into current byte |
|---|
| 214 | * |
|---|
| 215 | * Returns: 0 for success |
|---|
| 216 | * |
|---|
| 217 | * Description: |
|---|
| 218 | * |
|---|
| 219 | * This function updates the position to the next start code. |
|---|
| 220 | * |
|---|
| 221 | *************************************************************************/ |
|---|
| 222 | int next_start_code(unsigned char * pBuf, int * pbyteoff, int * pbitoff, int numbytes) |
|---|
| 223 | { |
|---|
| 224 | /* |
|---|
| 225 | * first, get byte aligned |
|---|
| 226 | */ |
|---|
| 227 | while ( *pbitoff ) |
|---|
| 228 | { |
|---|
| 229 | getnextbit(pBuf, pbyteoff, pbitoff) ; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | /* |
|---|
| 233 | * search for the StartCode pattern, 0x000001 |
|---|
| 234 | */ |
|---|
| 235 | while ( nextbits(pBuf, *pbyteoff, *pbitoff, 24) != 0x000001 ) |
|---|
| 236 | { |
|---|
| 237 | /* consume one byte */ |
|---|
| 238 | getnextbits(pBuf, pbyteoff, pbitoff, 8) ; |
|---|
| 239 | if ( *pbyteoff >= numbytes ) |
|---|
| 240 | { |
|---|
| 241 | /* gone too far */ |
|---|
| 242 | return(-1) ; |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | return(0) ; |
|---|
| 246 | } /* next_start_code */ |
|---|
| 247 | |
|---|