--- bitstream.h 2002/11/26 23:44:10 1.15 +++ bitstream.h 2003/04/04 22:12:07 1.16.2.1 @@ -50,13 +50,15 @@ * exception also makes it possible to release a modified version which * carries forward this exception. * - * $Id: bitstream.h,v 1.15 2002/11/26 23:44:10 edgomez Exp $ + * $Id: bitstream.h,v 1.16.2.1 2003/04/04 22:12:07 edgomez Exp $ * ****************************************************************************/ #ifndef _BITSTREAM_H_ #define _BITSTREAM_H_ +#include + #include "../portab.h" #include "../decoder.h" #include "../encoder.h" @@ -165,23 +167,31 @@ uint32_t length) { uint32_t tmp; + size_t bitpos; + ptr_t adjbitstream = (ptr_t)bitstream; - bs->start = bs->tail = (uint32_t *) bitstream; + /* + * Start the stream on a uint32_t boundary, by rounding down to the + * previous uint32_t and skipping the intervening bytes. + */ + bitpos = ((sizeof(uint32_t)-1) & (size_t)bitstream); + adjbitstream = adjbitstream - bitpos; + bs->start = bs->tail = (uint32_t *) adjbitstream; - tmp = *(uint32_t *) bitstream; + tmp = *bs->start; #ifndef ARCH_IS_BIG_ENDIAN BSWAP(tmp); #endif bs->bufa = tmp; - tmp = *((uint32_t *) bitstream + 1); + tmp = *(bs->start + 1); #ifndef ARCH_IS_BIG_ENDIAN BSWAP(tmp); #endif bs->bufb = tmp; bs->buf = 0; - bs->pos = 0; + bs->pos = bs->initpos = bitpos*8; bs->length = length; } @@ -208,7 +218,7 @@ bs->bufb = tmp; bs->buf = 0; - bs->pos = 0; + bs->pos = bs->initpos; } @@ -301,7 +311,7 @@ static uint32_t __inline BitstreamPos(const Bitstream * const bs) { - return 8 * ((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos; + return((uint32_t)(8*((ptr_t)bs->tail - (ptr_t)bs->start) + bs->pos - bs->initpos)); } @@ -312,7 +322,7 @@ static uint32_t __inline BitstreamLength(Bitstream * const bs) { - uint32_t len = (ptr_t) bs->tail - (ptr_t) bs->start; + uint32_t len = (uint32_t)((ptr_t)bs->tail - (ptr_t)bs->start); if (bs->pos) { uint32_t b = bs->buf; @@ -325,6 +335,10 @@ len += (bs->pos + 7) / 8; } + /* initpos is always on a byte boundary */ + if (bs->initpos) + len -= bs->initpos/8; + return len; }