/*************************************************************************** * Copyright (c) 2002, Broadcom Corporation * All Rights Reserved * Confidential Property of Broadcom Corporation * * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. * * $brcm_Workfile: bcmDccBits.c $ * $brcm_Revision: \main\SanJose_MSTV_Devel\1 $ * $brcm_Date: 3/12/02 4:46p $ * * Module Description: * * This module contains functions useful for extracting bits * from a byte stream. * * Revision History: * * $brcm_Log: $ * * \main\SanJose_MSTV_Devel\1 3/12/02 4:46p erikg * Merge from SanJose_MSTV_Devel_erikg_mpegccxport2_2002_03_06_1118_38 * * \main\SanJose_MSTV_Devel_erikg_mpegccxport2_2002_03_06_1118_38\1 3/6/02 11:29a erikg * new source files * ***************************************************************************/ /************************************************************************* * * MODULE DETAILS: * * The functions in this module operate on bits in a byte buffer (array). * Each function is given a pointer to the buffer and a byte offset and a * bit offset indicating the current position in the buffer. The byte/bit * offsets are either ints or int pointers, depending on whether or not * the current position is to be updated. * * It is assumed that the first bit to be operated on is in the msb position, * that is, 0x80. * *************************************************************************/ #include "bcmDccBits.h" /************************************************************************* * * FUNCTION: getnextbit * * Inputs: * pBuf - stream buffer * pbyteoff - current byte offset into pBuf * pbitoff - bit offset into current byte * * Outputs: * pbyteoff - current byte offset into pBuf * pbitoff - bit offset into current byte * * Returns: the requested bit, 0 or 1 * * Description: * * This function returns the next bit and increments the position. * *************************************************************************/ unsigned int getnextbit(unsigned char * pBuf, int * pbyteoff, int * pbitoff) { unsigned char mask ; unsigned char maskedbyte ; /* * find the bit in question */ mask = (0x80 >> *pbitoff) ; maskedbyte = pBuf[*pbyteoff] & mask ; /* * update the position */ if ( ++(*pbitoff) == 8 ) { *pbitoff = 0 ; (*pbyteoff)++ ; } /* * return either a 1 or 0 */ return( maskedbyte ? 1 : 0 ) ; } /************************************************************************* * * FUNCTION: getnextbits * * Inputs: * pBuf - stream buffer * pbyteoff - current byte offset into pBuf * pbitoff - bit offset into current byte * numbits - number of bits to get (max 32) * * Outputs: * pbyteoff - current byte offset into pBuf * pbitoff - bit offset into current byte * * Returns: the requested bits, lsb justified * * Description: * * This function returns the requested number of bits and * updates the position. * *************************************************************************/ unsigned int getnextbits(unsigned char * pBuf, int * pbyteoff, int * pbitoff, int numbits) { unsigned int retbits = 0 ; unsigned int newbit ; while ( numbits-- ) { newbit = getnextbit(pBuf, pbyteoff, pbitoff) ; retbits = (retbits << 1) | newbit ; } return(retbits) ; } /************************************************************************* * * FUNCTION: getnextbits_rev * * Inputs: * pBuf - stream buffer * pbyteoff - current byte offset into pBuf * pbitoff - bit offset into current byte * numbits - number of bits to get (max 32) * * Outputs: * pbyteoff - current byte offset into pBuf * pbitoff - bit offset into current byte * * Returns: the requested bits, bit reversed, lsb justified * * Description: * * This function returns the requested number of bits in reverse * order and updates the position. * *************************************************************************/ unsigned int getnextbits_rev(unsigned char * pBuf, int * pbyteoff, int * pbitoff, int numbits) { unsigned int retbits = 0 ; unsigned int newbit ; unsigned int bitmask = 1 ; while ( numbits-- ) { newbit = getnextbit(pBuf, pbyteoff, pbitoff) ; if ( newbit ) retbits |= bitmask ; bitmask <<= 1 ; } return(retbits) ; } /************************************************************************* * * FUNCTION: nextbits * * Inputs: * pBuf - stream buffer * byteoff - current byte offset into pBuf * bitoff - bit offset into current byte * numbits - number of bits to get (max 32) * * Outputs: * * * Returns: the requested bits, lsb justified * * Description: * * This function returns the requested number of bits but does * not update the position. * *************************************************************************/ unsigned int nextbits(unsigned char * pBuf, int byteoff, int bitoff, int numbits) { return( getnextbits(pBuf, &byteoff, &bitoff, numbits) ) ; } /************************************************************************* * * FUNCTION: next_start_code * * Inputs: * pBuf - stream buffer * pbyteoff - current byte offset into pBuf * pbitoff - bit offset into current byte * numbytes - max bytes to scan forward * * Outputs: * pbyteoff - current byte offset into pBuf * pbitoff - bit offset into current byte * * Returns: 0 for success * * Description: * * This function updates the position to the next start code. * *************************************************************************/ int next_start_code(unsigned char * pBuf, int * pbyteoff, int * pbitoff, int numbytes) { /* * first, get byte aligned */ while ( *pbitoff ) { getnextbit(pBuf, pbyteoff, pbitoff) ; } /* * search for the StartCode pattern, 0x000001 */ while ( nextbits(pBuf, *pbyteoff, *pbitoff, 24) != 0x000001 ) { /* consume one byte */ getnextbits(pBuf, pbyteoff, pbitoff, 8) ; if ( *pbyteoff >= numbytes ) { /* gone too far */ return(-1) ; } } return(0) ; } /* next_start_code */