2002-04-12 09:24:37 +00:00
|
|
|
#include "getbits.h"
|
2001-12-23 17:12:25 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
unsigned long _gst_getbits_int_cb (gst_getbits_t * gb, unsigned long bits);
|
|
|
|
unsigned long _gst_get1bit_int (gst_getbits_t * gb, unsigned long bits);
|
|
|
|
unsigned long _gst_getbits_int (gst_getbits_t * gb, unsigned long bits);
|
|
|
|
unsigned long _gst_getbits_fast_int (gst_getbits_t * gb, unsigned long bits);
|
|
|
|
unsigned long _gst_showbits_int (gst_getbits_t * gb, unsigned long bits);
|
|
|
|
void _gst_flushbits_int (gst_getbits_t * gb, unsigned long bits);
|
|
|
|
void _gst_getbits_back_int (gst_getbits_t * gb, unsigned long bits);
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
_gst_getbits_int_cb (gst_getbits_t * gb, unsigned long bits)
|
|
|
|
{
|
2001-12-23 17:12:25 +00:00
|
|
|
int result;
|
|
|
|
int bitsleft;
|
|
|
|
|
2002-03-19 04:10:13 +00:00
|
|
|
/*printf("gst_getbits%lu %ld %p %08x\n", bits, gb->bits, gb->ptr, gb->dword); */
|
2001-12-23 17:12:25 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (!bits)
|
|
|
|
return 0;
|
2001-12-23 17:12:25 +00:00
|
|
|
|
|
|
|
gb->bits -= bits;
|
2004-03-13 15:27:01 +00:00
|
|
|
result = gb->dword >> (32 - bits);
|
2001-12-23 17:12:25 +00:00
|
|
|
|
|
|
|
if (gb->bits < 0) {
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2001-12-23 17:12:25 +00:00
|
|
|
gb->ptr += 4;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
bitsleft = (gb->endptr - gb->ptr) * 8;
|
2001-12-23 17:12:25 +00:00
|
|
|
bits = -gb->bits;
|
2004-03-13 15:27:01 +00:00
|
|
|
gb->bits += (bitsleft > 32 ? 32 : bitsleft);
|
|
|
|
|
2001-12-23 17:12:25 +00:00
|
|
|
if (gb->endptr <= gb->ptr) {
|
2004-03-13 15:27:01 +00:00
|
|
|
(gb->callback) (gb, gb->data);
|
2001-12-23 17:12:25 +00:00
|
|
|
gb->bits -= bits;
|
|
|
|
}
|
2004-03-13 15:27:01 +00:00
|
|
|
gb->dword = swab32 (*((unsigned long *) (gb->ptr)));
|
2001-12-23 17:12:25 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
result |= (gb->dword >> (32 - bits));
|
2001-12-23 17:12:25 +00:00
|
|
|
}
|
|
|
|
gb->dword <<= bits;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
unsigned long
|
|
|
|
_gst_get1bit_int (gst_getbits_t * gb, unsigned long bits)
|
|
|
|
{
|
2001-12-23 17:12:25 +00:00
|
|
|
unsigned char rval;
|
|
|
|
|
|
|
|
rval = *gb->ptr << gb->bits;
|
|
|
|
|
|
|
|
gb->bits++;
|
2004-03-13 15:27:01 +00:00
|
|
|
gb->ptr += (gb->bits >> 3);
|
2001-12-23 17:12:25 +00:00
|
|
|
gb->bits &= 0x7;
|
|
|
|
|
2003-06-29 14:05:49 +00:00
|
|
|
GST_DEBUG ("getbits%ld, %08x", bits, rval);
|
2004-03-13 15:27:01 +00:00
|
|
|
return rval >> 7;
|
2001-12-23 17:12:25 +00:00
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
unsigned long
|
|
|
|
_gst_getbits_int (gst_getbits_t * gb, unsigned long bits)
|
|
|
|
{
|
2001-12-23 17:12:25 +00:00
|
|
|
unsigned long rval;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (bits == 0)
|
|
|
|
return 0;
|
2001-12-23 17:12:25 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
rval = swab32 (*((unsigned long *) (gb->ptr)));
|
2001-12-23 17:12:25 +00:00
|
|
|
rval <<= gb->bits;
|
|
|
|
|
|
|
|
gb->bits += bits;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
rval >>= (32 - bits);
|
|
|
|
gb->ptr += (gb->bits >> 3);
|
2001-12-23 17:12:25 +00:00
|
|
|
gb->bits &= 0x7;
|
|
|
|
|
2003-06-29 14:05:49 +00:00
|
|
|
GST_DEBUG ("getbits%ld, %08lx", bits, rval);
|
2001-12-23 17:12:25 +00:00
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
unsigned long
|
|
|
|
_gst_getbits_fast_int (gst_getbits_t * gb, unsigned long bits)
|
|
|
|
{
|
2001-12-23 17:12:25 +00:00
|
|
|
unsigned long rval;
|
|
|
|
|
|
|
|
rval = (unsigned char) (gb->ptr[0] << gb->bits);
|
2004-03-13 15:27:01 +00:00
|
|
|
rval |= ((unsigned int) gb->ptr[1] << gb->bits) >> 8;
|
2001-12-23 17:12:25 +00:00
|
|
|
rval <<= bits;
|
|
|
|
rval >>= 8;
|
|
|
|
|
|
|
|
gb->bits += bits;
|
2004-03-13 15:27:01 +00:00
|
|
|
gb->ptr += (gb->bits >> 3);
|
2001-12-23 17:12:25 +00:00
|
|
|
gb->bits &= 0x7;
|
|
|
|
|
2003-06-29 14:05:49 +00:00
|
|
|
GST_DEBUG ("getbits%ld, %08lx", bits, rval);
|
2001-12-23 17:12:25 +00:00
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
unsigned long
|
|
|
|
_gst_showbits_int (gst_getbits_t * gb, unsigned long bits)
|
|
|
|
{
|
2001-12-23 17:12:25 +00:00
|
|
|
unsigned long rval;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
if (bits == 0)
|
|
|
|
return 0;
|
2001-12-23 17:12:25 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
rval = swab32 (*((unsigned long *) (gb->ptr)));
|
2001-12-23 17:12:25 +00:00
|
|
|
rval <<= gb->bits;
|
2004-03-13 15:27:01 +00:00
|
|
|
rval >>= (32 - bits);
|
2001-12-23 17:12:25 +00:00
|
|
|
|
2003-06-29 14:05:49 +00:00
|
|
|
GST_DEBUG ("showbits%ld, %08lx", bits, rval);
|
2001-12-23 17:12:25 +00:00
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
void
|
|
|
|
_gst_flushbits_int (gst_getbits_t * gb, unsigned long bits)
|
|
|
|
{
|
2001-12-23 17:12:25 +00:00
|
|
|
gb->bits += bits;
|
2004-03-13 15:27:01 +00:00
|
|
|
gb->ptr += (gb->bits >> 3);
|
2001-12-23 17:12:25 +00:00
|
|
|
gb->bits &= 0x7;
|
2003-06-29 14:05:49 +00:00
|
|
|
GST_DEBUG ("flushbits%ld", bits);
|
2001-12-23 17:12:25 +00:00
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
void
|
|
|
|
_gst_getbits_back_int (gst_getbits_t * gb, unsigned long bits)
|
|
|
|
{
|
2001-12-23 17:12:25 +00:00
|
|
|
gb->bits -= bits;
|
2004-03-13 15:27:01 +00:00
|
|
|
gb->ptr += (gb->bits >> 3);
|
2001-12-23 17:12:25 +00:00
|
|
|
gb->bits &= 0x7;
|
|
|
|
}
|