1 #include "vl_bitstream_parser.h"
4 #include <util/u_memory.h>
7 grab_bits(unsigned cursor
, unsigned how_many_bits
, unsigned bitstream_elt
)
9 unsigned excess_bits
= sizeof(unsigned) * CHAR_BIT
- how_many_bits
- cursor
;
11 assert(cursor
< sizeof(unsigned) * CHAR_BIT
);
12 assert(how_many_bits
> 0 && how_many_bits
<= sizeof(unsigned) * CHAR_BIT
);
13 assert(cursor
+ how_many_bits
<= sizeof(unsigned) * CHAR_BIT
);
15 return (bitstream_elt
<< excess_bits
) >> (excess_bits
+ cursor
);
19 show_bits(unsigned cursor
, unsigned how_many_bits
, const unsigned *bitstream
)
21 unsigned cur_int
= cursor
/ (sizeof(unsigned) * CHAR_BIT
);
22 unsigned cur_bit
= cursor
% (sizeof(unsigned) * CHAR_BIT
);
26 if (cur_bit
+ how_many_bits
> sizeof(unsigned) * CHAR_BIT
) {
27 unsigned lower
= grab_bits(cur_bit
, sizeof(unsigned) * CHAR_BIT
- cur_bit
,
29 unsigned upper
= grab_bits(0, cur_bit
+ how_many_bits
- sizeof(unsigned) * CHAR_BIT
,
30 bitstream
[cur_int
+ 1]);
31 return lower
| upper
<< (sizeof(unsigned) * CHAR_BIT
- cur_bit
);
34 return grab_bits(cur_bit
, how_many_bits
, bitstream
[cur_int
]);
37 bool vl_bitstream_parser_init(struct vl_bitstream_parser
*parser
,
38 unsigned num_bitstreams
,
39 const void **bitstreams
,
40 const unsigned *sizes
)
43 assert(num_bitstreams
);
47 parser
->num_bitstreams
= num_bitstreams
;
48 parser
->bitstreams
= (const unsigned**)bitstreams
;
49 parser
->sizes
= sizes
;
50 parser
->cur_bitstream
= 0;
56 void vl_bitstream_parser_cleanup(struct vl_bitstream_parser
*parser
)
62 vl_bitstream_parser_get_bits(struct vl_bitstream_parser
*parser
,
63 unsigned how_many_bits
)
69 bits
= vl_bitstream_parser_show_bits(parser
, how_many_bits
);
71 vl_bitstream_parser_forward(parser
, how_many_bits
);
77 vl_bitstream_parser_show_bits(struct vl_bitstream_parser
*parser
,
78 unsigned how_many_bits
)
83 unsigned cur_bitstream
;
87 cursor
= parser
->cursor
;
88 cur_bitstream
= parser
->cur_bitstream
;
91 unsigned bits_left
= parser
->sizes
[cur_bitstream
] * CHAR_BIT
- cursor
;
92 unsigned bits_to_show
= how_many_bits
> bits_left
? bits_left
: how_many_bits
;
94 bits
|= show_bits(cursor
, bits_to_show
,
95 parser
->bitstreams
[cur_bitstream
]) << shift
;
97 if (how_many_bits
> bits_to_show
) {
98 how_many_bits
-= bits_to_show
;
101 shift
+= bits_to_show
;
110 void vl_bitstream_parser_forward(struct vl_bitstream_parser
*parser
,
111 unsigned how_many_bits
)
114 assert(how_many_bits
);
116 parser
->cursor
+= how_many_bits
;
118 while (parser
->cursor
> parser
->sizes
[parser
->cur_bitstream
] * CHAR_BIT
) {
119 parser
->cursor
-= parser
->sizes
[parser
->cur_bitstream
++] * CHAR_BIT
;
120 assert(parser
->cur_bitstream
< parser
->num_bitstreams
);
124 void vl_bitstream_parser_rewind(struct vl_bitstream_parser
*parser
,
125 unsigned how_many_bits
)
130 assert(how_many_bits
);
132 c
= parser
->cursor
- how_many_bits
;
135 c
+= parser
->sizes
[parser
->cur_bitstream
--] * CHAR_BIT
;
136 assert(parser
->cur_bitstream
< parser
->num_bitstreams
);
139 parser
->cursor
= (unsigned)c
;