source: svn/trunk/newcon3bcm2_21bu/dta/src/dcc/src/bdccbits.c @ 2

Last change on this file since 2 was 2, checked in by jglee, 11 years ago

first commit

  • Property svn:executable set to *
File size: 6.4 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2002-2005, 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: bdccbits.c $
11 * $brcm_Revision: 1 $
12 * $brcm_Date: 4/18/05 2:41p $
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: /BSEAV/lib/ccgfx/source/bdccbits.c $
22 *
23 * 1   4/18/05 2:41p shyam
24 * PR 8365 : Add other sub-modules of ccgfx
25 *
26 ***************************************************************************/
27
28
29/*************************************************************************
30 *
31 * MODULE DETAILS:
32 *
33 * The functions in this module operate on bits in a byte buffer (array).
34 * Each function is given a pointer to the buffer and a byte offset and a
35 * bit offset indicating the current position in the buffer.  The byte/bit
36 * offsets are either ints or int pointers, depending on whether or not
37 * the current position is to be updated.
38 *
39 * It is assumed that the first bit to be operated on is in the msb position,
40 * that is, 0x80.
41 *
42 *************************************************************************/
43
44
45#include "bdccbits.h"
46
47 
48/*************************************************************************
49 *
50 * FUNCTION:                    BDCC_BITS_P_GetNextBit
51 *
52 * Inputs:                             
53 *                                              pBuf                            - stream buffer
54 *                                              pbyteoff                        - current byte offset into pBuf
55 *                                              pbitoff                         - bit offset into current byte
56 *
57 * Outputs:                             
58 *                                              pbyteoff                        - current byte offset into pBuf
59 *                                              pbitoff                         - bit offset into current byte
60 *
61 * Returns:                             the requested bit, 0 or 1
62 *
63 * Description:
64 *
65 * This function returns the next bit and increments the position.
66 *
67 *************************************************************************/
68unsigned int 
69BDCC_BITS_P_GetNextBit(unsigned char * pBuf, int * pbyteoff, int * pbitoff)
70{
71        unsigned char mask ;
72        unsigned char maskedbyte ;
73
74        /*
75         * find the bit in question
76         */
77        mask = (0x80 >> *pbitoff) ;
78        maskedbyte = pBuf[*pbyteoff] & mask ;
79
80        /*
81         * update the position
82         */
83        if ( ++(*pbitoff) == 8 )
84        {
85                *pbitoff = 0 ;
86                (*pbyteoff)++ ;
87        }
88
89        /*
90         * return either a 1 or 0
91         */
92        return( maskedbyte ? 1 : 0 ) ;
93}
94
95/*************************************************************************
96 *
97 * FUNCTION:                    BDCC_BITS_P_GetNextBits
98 *
99 * Inputs:                             
100 *                                              pBuf                            - stream buffer
101 *                                              pbyteoff                        - current byte offset into pBuf
102 *                                              pbitoff                         - bit offset into current byte
103 *                                              numbits                         - number of bits to get (max 32)
104 *
105 * Outputs:                             
106 *                                              pbyteoff                        - current byte offset into pBuf
107 *                                              pbitoff                         - bit offset into current byte
108 *
109 * Returns:                             the requested bits, lsb justified
110 *
111 * Description:
112 *
113 * This function returns the requested number of bits and
114 * updates the position.
115 *
116 *************************************************************************/
117unsigned int 
118BDCC_BITS_P_GetNextBits(unsigned char * pBuf, int * pbyteoff, int * pbitoff, int numbits)
119{
120        unsigned int retbits = 0 ;
121        unsigned int newbit ;
122
123        while ( numbits-- )
124        {
125                newbit = BDCC_BITS_P_GetNextBit(pBuf, pbyteoff, pbitoff) ;
126                retbits = (retbits << 1) | newbit ;
127        }
128
129        return(retbits) ;
130}
131
132/*************************************************************************
133 *
134 * FUNCTION:                    BDCC_BITS_P_GetNextBits_rev
135 *
136 * Inputs:                             
137 *                                              pBuf                            - stream buffer
138 *                                              pbyteoff                        - current byte offset into pBuf
139 *                                              pbitoff                         - bit offset into current byte
140 *                                              numbits                         - number of bits to get (max 32)
141 *
142 * Outputs:                             
143 *                                              pbyteoff                        - current byte offset into pBuf
144 *                                              pbitoff                         - bit offset into current byte
145 *
146 * Returns:                             the requested bits, bit reversed, lsb justified
147 *
148 * Description:
149 *
150 * This function returns the requested number of bits in reverse
151 * order and updates the position.
152 *
153 *************************************************************************/
154unsigned int 
155BDCC_BITS_P_GetNextBitsRev(unsigned char * pBuf, int * pbyteoff, int * pbitoff, int numbits)
156{
157        unsigned int retbits = 0 ;
158        unsigned int newbit ;
159        unsigned int bitmask = 1 ;
160
161        while ( numbits-- )
162        {
163                newbit = BDCC_BITS_P_GetNextBit(pBuf, pbyteoff, pbitoff) ;
164                if ( newbit )
165                        retbits |= bitmask ;
166                bitmask <<= 1 ;
167        }
168
169        return(retbits) ;
170}
171
172/*************************************************************************
173 *
174 * FUNCTION:                    BDCC_BITS_P_Nextbits
175 *
176 * Inputs:                             
177 *                                              pBuf                            - stream buffer
178 *                                              byteoff                         - current byte offset into pBuf
179 *                                              bitoff                          - bit offset into current byte
180 *                                              numbits                         - number of bits to get (max 32)
181 *
182 * Outputs:                             
183 *                                              <none>
184 *
185 * Returns:                             the requested bits, lsb justified
186 *
187 * Description:
188 *
189 * This function returns the requested number of bits but does
190 * not update the position.
191 *
192 *************************************************************************/
193unsigned int 
194BDCC_BITS_P_Nextbits(unsigned char * pBuf, int byteoff, int bitoff, int numbits)
195{
196        return( BDCC_BITS_P_GetNextBits(pBuf, &byteoff, &bitoff, numbits) ) ;
197}
198
199/*************************************************************************
200 *
201 * FUNCTION:                    BDCC_BITS_P_NextStartCode
202 *
203 * Inputs:                             
204 *                                              pBuf                            - stream buffer
205 *                                              pbyteoff                        - current byte offset into pBuf
206 *                                              pbitoff                         - bit offset into current byte
207 *                                              numbytes                        - max bytes to scan forward
208 *
209 * Outputs:                             
210 *                                              pbyteoff                        - current byte offset into pBuf
211 *                                              pbitoff                         - bit offset into current byte
212 *
213 * Returns:                             0 for success
214 *
215 * Description:
216 *
217 * This function updates the position to the next start code.
218 *
219 *************************************************************************/
220int BDCC_BITS_P_NextStartCode(unsigned char * pBuf, int * pbyteoff, int * pbitoff, int numbytes)
221{
222        /*
223         * first, get byte aligned
224         */
225        while ( *pbitoff )
226        {
227                BDCC_BITS_P_GetNextBit(pBuf, pbyteoff, pbitoff) ;
228        }
229
230        /*
231         * search for the StartCode pattern, 0x000001
232         */
233        while ( BDCC_BITS_P_Nextbits(pBuf, *pbyteoff, *pbitoff, 24) != 0x000001 )
234        {
235                /* consume one byte */
236                BDCC_BITS_P_GetNextBits(pBuf, pbyteoff, pbitoff, 8) ;
237                if ( *pbyteoff >= numbytes )
238                {
239                        /* gone too far */
240                        return(-1) ;
241                }
242        }
243        return(0) ;
244} /* BDCC_BITS_P_NextStartCode */
245
Note: See TracBrowser for help on using the repository browser.