It appears Solaris's cc is ignoring the signedness of bitfield types.
[xiph/unicode.git] / w3d / bitcoder.h
blobf0b8cd69b0bdb6b84a4de84b26c1e07da8169813
1 #ifndef __BITCODER_H
2 #define __BITCODER_H
4 #include "mem.h"
6 #if defined(BITCODER)
8 #define OUTPUT_BIT(coder,bit) bitcoder_write_bit(coder,bit)
9 #define INPUT_BIT(coder) bitcoder_read_bit(coder)
10 #define OUTPUT_BIT_DIRECT(coder,bit) bitcoder_write_bit(coder,bit)
11 #define INPUT_BIT_DIRECT(coder) bitcoder_read_bit(coder)
12 #define ENTROPY_CODER BitCoderState
13 #define ENTROPY_ENCODER_init(coder,limit) bitcoder_coder_init(coder,limit)
14 #define ENTROPY_ENCODER_DONE(coder) bitcoder_encoder_done(coder)
15 #define ENTROPY_ENCODER_FLUSH(coder) bitcoder_flush(coder)
16 #define ENTROPY_DECODER_INIT(coder,bitstream,limit) \
17 bitcoder_decoder_init(coder,bitstream,limit)
18 #define ENTROPY_DECODER_DONE(coder) /* nothing to do ... */
19 #define ENTROPY_CODER_BITSTREAM(coder) (coder)->bitstream
21 #define ENTROPY_CODER_SYMBOL(coder) 1
22 #define ENTROPY_CODER_RUNLENGTH(coder) 0
23 #define ENTROPY_CODER_SKIP(coder,skip)
25 #endif
28 typedef struct {
29 int32_t bit_count; /* number of valid bits in byte */
30 uint8_t byte; /* buffer to save bits */
31 uint32_t byte_count; /* number of bytes written */
32 uint8_t *bitstream;
33 uint32_t limit; /* don't write more bytes to bitstream ... */
34 int eos; /* end of stream reached */
35 } BitCoderState;
39 static inline
40 void bitcoder_encoder_init (BitCoderState *s, uint32_t limit)
42 s->bit_count = 0;
43 s->byte = 0;
44 s->byte_count = 0;
45 s->bitstream = (uint8_t*) MALLOC (limit);
46 s->limit = limit;
47 s->eos = 0;
51 static inline
52 void bitcoder_encoder_done (BitCoderState *s)
54 FREE (s->bitstream);
58 static inline
59 void bitcoder_decoder_init (BitCoderState *s, uint8_t *bitstream, uint32_t limit)
61 s->bit_count = -1;
62 s->byte = 0;
63 s->byte_count = 0;
64 s->bitstream = bitstream;
65 s->limit = limit;
66 s->eos = 0;
70 static inline
71 uint32_t bitcoder_flush (BitCoderState *s)
73 if (s->bit_count > 0 && s->byte_count < s->limit)
74 s->bitstream [s->byte_count++] = s->byte << (8 - s->bit_count);
76 //printf ("%s: %i bytes written.\n", __FUNCTION__, s->byte_count);
77 //printf ("%s: last bit %i\n", __FUNCTION__, s->bit_count);
78 return s->byte_count;
83 static inline
84 void bitcoder_write_bit (BitCoderState *s, int bit)
86 s->byte <<= 1;
87 s->byte |= bit & 1;
89 s->bit_count++;
91 if (s->bit_count == 8) {
92 if (s->byte_count < s->limit) {
93 s->bitstream [s->byte_count++] = s->byte;
94 s->bit_count = 0;
95 } else {
96 s->eos = 1;
102 static inline
103 int bitcoder_read_bit (BitCoderState *s)
105 int ret;
107 if (s->bit_count <= 0) {
108 if (!s->bitstream) {
109 s->eos = 1;
110 return 0;
113 if (s->byte_count < s->limit) {
114 s->byte = s->bitstream [s->byte_count++];
115 } else {
116 s->eos = 1;
117 s->byte = 0;
120 s->bit_count = 8;
123 ret = s->byte >> 7;
124 s->byte <<= 1;
125 s->bit_count--;
127 return ret & 1;
135 static inline
136 void bit_print (TYPE byte)
138 int bit = 8*sizeof(TYPE);
140 do {
141 bit--;
142 printf ((byte & (1 << bit)) ? "1" : "0");
143 } while (bit);
144 printf ("\n");
147 #endif