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)
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 */
33 uint32_t limit
; /* don't write more bytes to bitstream ... */
34 int eos
; /* end of stream reached */
40 void bitcoder_encoder_init (BitCoderState
*s
, uint32_t limit
)
45 s
->bitstream
= (uint8_t*) MALLOC (limit
);
52 void bitcoder_encoder_done (BitCoderState
*s
)
59 void bitcoder_decoder_init (BitCoderState
*s
, uint8_t *bitstream
, uint32_t limit
)
64 s
->bitstream
= bitstream
;
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);
84 void bitcoder_write_bit (BitCoderState
*s
, int bit
)
91 if (s
->bit_count
== 8) {
92 if (s
->byte_count
< s
->limit
) {
93 s
->bitstream
[s
->byte_count
++] = s
->byte
;
103 int bitcoder_read_bit (BitCoderState
*s
)
107 if (s
->bit_count
<= 0) {
113 if (s
->byte_count
< s
->limit
) {
114 s
->byte
= s
->bitstream
[s
->byte_count
++];
136 void bit_print (TYPE byte
)
138 int bit
= 8*sizeof(TYPE
);
142 printf ((byte
& (1 << bit
)) ? "1" : "0");