1 /**************************************************************************
3 * Copyright 2009 Younes Manton.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vl_bitstream_parser.h"
31 #include <util/u_memory.h>
34 grab_bits(unsigned cursor
, unsigned how_many_bits
, unsigned bitstream_elt
)
36 unsigned excess_bits
= sizeof(unsigned) * CHAR_BIT
- how_many_bits
- cursor
;
38 assert(cursor
< sizeof(unsigned) * CHAR_BIT
);
39 assert(how_many_bits
> 0 && how_many_bits
<= sizeof(unsigned) * CHAR_BIT
);
40 assert(cursor
+ how_many_bits
<= sizeof(unsigned) * CHAR_BIT
);
42 return (bitstream_elt
<< excess_bits
) >> (excess_bits
+ cursor
);
46 show_bits(unsigned cursor
, unsigned how_many_bits
, const unsigned *bitstream
)
48 unsigned cur_int
= cursor
/ (sizeof(unsigned) * CHAR_BIT
);
49 unsigned cur_bit
= cursor
% (sizeof(unsigned) * CHAR_BIT
);
53 if (cur_bit
+ how_many_bits
> sizeof(unsigned) * CHAR_BIT
) {
54 unsigned lower
= grab_bits(cur_bit
, sizeof(unsigned) * CHAR_BIT
- cur_bit
,
56 unsigned upper
= grab_bits(0, cur_bit
+ how_many_bits
- sizeof(unsigned) * CHAR_BIT
,
57 bitstream
[cur_int
+ 1]);
58 return lower
| upper
<< (sizeof(unsigned) * CHAR_BIT
- cur_bit
);
61 return grab_bits(cur_bit
, how_many_bits
, bitstream
[cur_int
]);
64 bool vl_bitstream_parser_init(struct vl_bitstream_parser
*parser
,
65 unsigned num_bitstreams
,
66 const void **bitstreams
,
67 const unsigned *sizes
)
70 assert(num_bitstreams
);
74 parser
->num_bitstreams
= num_bitstreams
;
75 parser
->bitstreams
= (const unsigned**)bitstreams
;
76 parser
->sizes
= sizes
;
77 parser
->cur_bitstream
= 0;
83 void vl_bitstream_parser_cleanup(struct vl_bitstream_parser
*parser
)
89 vl_bitstream_parser_get_bits(struct vl_bitstream_parser
*parser
,
90 unsigned how_many_bits
)
96 bits
= vl_bitstream_parser_show_bits(parser
, how_many_bits
);
98 vl_bitstream_parser_forward(parser
, how_many_bits
);
104 vl_bitstream_parser_show_bits(struct vl_bitstream_parser
*parser
,
105 unsigned how_many_bits
)
110 unsigned cur_bitstream
;
114 cursor
= parser
->cursor
;
115 cur_bitstream
= parser
->cur_bitstream
;
118 unsigned bits_left
= parser
->sizes
[cur_bitstream
] * CHAR_BIT
- cursor
;
119 unsigned bits_to_show
= how_many_bits
> bits_left
? bits_left
: how_many_bits
;
121 bits
|= show_bits(cursor
, bits_to_show
,
122 parser
->bitstreams
[cur_bitstream
]) << shift
;
124 if (how_many_bits
> bits_to_show
) {
125 how_many_bits
-= bits_to_show
;
128 shift
+= bits_to_show
;
137 void vl_bitstream_parser_forward(struct vl_bitstream_parser
*parser
,
138 unsigned how_many_bits
)
141 assert(how_many_bits
);
143 parser
->cursor
+= how_many_bits
;
145 while (parser
->cursor
> parser
->sizes
[parser
->cur_bitstream
] * CHAR_BIT
) {
146 parser
->cursor
-= parser
->sizes
[parser
->cur_bitstream
++] * CHAR_BIT
;
147 assert(parser
->cur_bitstream
< parser
->num_bitstreams
);
151 void vl_bitstream_parser_rewind(struct vl_bitstream_parser
*parser
,
152 unsigned how_many_bits
)
157 assert(how_many_bits
);
159 c
= parser
->cursor
- how_many_bits
;
162 c
+= parser
->sizes
[parser
->cur_bitstream
--] * CHAR_BIT
;
163 assert(parser
->cur_bitstream
< parser
->num_bitstreams
);
166 parser
->cursor
= (unsigned)c
;