9 unsigned int required_bits (unsigned int x
)
13 while ((x
& (1 << bits
)) == 0 && bits
)
21 void write_number_binary (BitCoderState
*b
, unsigned int x
, int bits
, int u
)
23 //printf ("wrote %i with %i bits (%i+%i)\n", x, u+bits, u, bits);
26 bitcoder_write_bit (b
, (x
>> bits
) & 1);
32 unsigned int read_number_binary (BitCoderState
*b
, int bits
)
38 x
|= bitcoder_read_bit (b
) << bits
;
46 void golomb_write_number (BitCoderState
*b
, unsigned int x
, int bits
)
53 while ((q
= (x
- 1) >> bits
) > 0) {
54 bitcoder_write_bit (b
, 1); /* fast temporary adaption, write */
55 bits
++; /* unary representation of q */
59 bitcoder_write_bit (b
, 0);
61 r
= x
- 1 - (q
<< bits
);
63 write_number_binary (b
, r
, bits
, i
+1);
68 unsigned int golomb_read_number (BitCoderState
*b
, int bits
)
70 unsigned int q
= 0, r
, x
;
72 while (bitcoder_read_bit (b
) != 0) {
76 r
= read_number_binary (b
, bits
);
77 x
= (q
<< bits
) + r
+ 1;
85 uint8_t bits
; /* a 5.3 fixed point integer */
86 } GolombAdaptiveCoderState
;
88 #define GOLOMB_ADAPTIVE_CODER_STATE_INITIALIZER { 8<<3, 0 }
91 static const int golomb_w_tab
[] = { 256, 128, 64 };
97 void golombcoder_encode_number (GolombAdaptiveCoderState
*g
,
101 golomb_write_number (b
, x
, g
->bits
>> 3);
103 g
->bits
= ((256 - golomb_w_tab
[g
->count
]) * (int) g
->bits
+
104 golomb_w_tab
[g
->count
] * (required_bits(x
)<<3)) / 256;
113 unsigned int golombcoder_decode_number (GolombAdaptiveCoderState
*g
,
118 x
= golomb_read_number (b
, g
->bits
>> 3);
120 g
->bits
= ((256 - golomb_w_tab
[g
->count
]) * g
->bits
+
121 golomb_w_tab
[g
->count
] * (required_bits(x
)<<3)) / 256;