svnno****@sourc*****
svnno****@sourc*****
2009年 8月 28日 (金) 01:25:51 JST
Revision: 176 http://sourceforge.jp/projects/swfed/svn/view?view=rev&revision=176 Author: yoya Date: 2009-08-28 01:25:51 +0900 (Fri, 28 Aug 2009) Log Message: ----------- - bitstream_getbits_signed を追加 - デバッグ用に bitstream_hexdump を追加 - エラー処理を実装途中 Modified Paths: -------------- trunk/src/bitstream.c trunk/src/bitstream.h -------------- next part -------------- Modified: trunk/src/bitstream.c =================================================================== --- trunk/src/bitstream.c 2009-08-27 16:20:13 UTC (rev 175) +++ trunk/src/bitstream.c 2009-08-27 16:25:51 UTC (rev 176) @@ -304,6 +304,21 @@ return bits; } +signed long +bitstream_getbits_signed(bitstream_t *bs, int bit_width) { + int i; + int bit; + unsigned long bits = 0; + for (i=0 ; i < bit_width ; i++) { + bit = bitstream_getbit(bs); + if (bit == -1) { + return -1; + } + bits |= bit << (bit_width - 1 - i); + } + return bits; +} + void bitstream_align(bitstream_t *bs) { if (bs->bit_offset > 0) { @@ -362,16 +377,76 @@ bitstream_length(bitstream_t *bs) { return bs->data_len; } +/* + * utility + */ -unsigned long + + +extern signed long +bitstream_unsigned2signed(unsigned long num, int size) { + unsigned long sig_bit = 1 << (size - 1); + if ((sig_bit & num) == 0) { + return (signed long) num; + } else { + unsigned long mask = sig_bit - 1; + return - ((num^mask) & mask) - 1; + } +} + +extern unsigned long +bitstream_signed2unsigned(signed long num, int size) { // XXX check me! + if (0 <= num){ + return (unsigned long) num; + } else { + unsigned long sig_bit = 1 << (size - 1); + unsigned long mask = sig_bit - 1; + return - ((num^mask) & mask) - 1; + } +} +/* + * error handling + */ + +int +bitstream_iserror(bitstream_t *bs) { + (void) bs; + return 0; +} + +void +bitstream_printerror(bitstream_t *bs) { + (void) bs; +} +/* + * for debug + */ + +void bitstream_hexdump(bitstream_t *bs, int length) { - unsigned long i; - printf("%08lu: ", bs->byte_offset); + unsigned long i, j; for ( i = bs->byte_offset ; i < bs->byte_offset + length ; i++) { + if ((i == bs->byte_offset) || (i%16) == 0) { + printf("%08lu: ", i); + if ((i%16) != 0) { + for( j = 0 ; j < (i%16) ; j++) { + printf(" "); + } + } + } printf("%02x ", bs->data[i] & 0xff); + if ((i%16) == 7) { + printf(" "); + } + if ((i%16) == 15) { + printf("\n"); + } } - printf("\n"); + if ((i%16) != 0) { + printf("\n"); + } } + void bitstream_print(bitstream_t *bs) { printf("data=%p data_len=%lu data_alloc_len=%lu\n", Modified: trunk/src/bitstream.h =================================================================== --- trunk/src/bitstream.h 2009-08-27 16:20:13 UTC (rev 175) +++ trunk/src/bitstream.h 2009-08-27 16:25:51 UTC (rev 176) @@ -16,10 +16,16 @@ /* seek position */ unsigned long byte_offset; unsigned long bit_offset; + /* error */ + int error; + } bitstream_t; #define BITSTREAM_DATA_LEN_MIN 0x100 +#define BITSTREAM_ERROR_NONE 0 +#define BITSTREAM_ERROR_NOMORE_DATA 1 + extern bitstream_t *bitstream_open(void); extern void bitstream_close(bitstream_t * bs); @@ -46,6 +52,7 @@ extern int bitstream_getbit(bitstream_t *bs); extern int bitstream_putbits(bitstream_t *bs, unsigned long bits, int bit_width); extern unsigned long bitstream_getbits(bitstream_t *bs, int bit_width); +extern signed long bitstream_getbits_signed(bitstream_t *bs, int bit_width); extern void bitstream_align(bitstream_t *bs); /* seeking */ @@ -61,8 +68,17 @@ extern unsigned char *bitstream_buffer(bitstream_t *bs, unsigned long byte_offset); extern unsigned long bitstream_length(bitstream_t *bs); -/* debug function */ -extern unsigned long bitstream_hexdump(bitstream_t *bs, int length); +/* utility */ + +extern signed long bitstream_unsigned2signed(unsigned long num, int size); +extern unsigned long bitstream_signed2unsigned(signed long num, int size); + +/* error handling */ +extern int bitstream_iserror(bitstream_t *bs); +extern void bitstream_printerror(bitstream_t *bs); + +/* for debug */ +extern void bitstream_hexdump(bitstream_t *bs, int length); extern void bitstream_print(bitstream_t *bs); #endif /* __BITSTREAM_H__ */