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 inline void endian_swap_ushort(unsigned short *x
)
40 inline void endian_swap_uint(unsigned int *x
)
43 ((x
[0]<<8) & 0x00FF0000) |
44 ((x
[0]>>8) & 0x0000FF00) |
48 inline void endian_swap_ulonglong(unsigned long long *x
)
51 ((x
[0]<<40) & 0x00FF000000000000) |
52 ((x
[0]<<24) & 0x0000FF0000000000) |
53 ((x
[0]<<8) & 0x000000FF00000000) |
54 ((x
[0]>>8) & 0x00000000FF000000) |
55 ((x
[0]>>24) & 0x0000000000FF0000) |
56 ((x
[0]>>40) & 0x000000000000FF00) |
61 grab_bits(unsigned cursor
, unsigned how_many_bits
, unsigned bitstream_elt
)
63 unsigned excess_bits
= sizeof(unsigned) * CHAR_BIT
- how_many_bits
;
65 assert(cursor
< sizeof(unsigned) * CHAR_BIT
);
66 assert(how_many_bits
> 0 && how_many_bits
<= sizeof(unsigned) * CHAR_BIT
);
67 assert(cursor
+ how_many_bits
<= sizeof(unsigned) * CHAR_BIT
);
69 #ifndef PIPE_ARCH_BIG_ENDIAN
70 switch (sizeof(unsigned)) {
72 endian_swap_ushort(&bitstream_elt
);
75 endian_swap_uint(&bitstream_elt
);
78 endian_swap_ulonglong(&bitstream_elt
);
81 #endif // !PIPE_ARCH_BIG_ENDIAN
83 return (bitstream_elt
<< cursor
) >> (excess_bits
);
87 show_bits(unsigned cursor
, unsigned how_many_bits
, const unsigned *bitstream
)
89 unsigned cur_int
= cursor
/ (sizeof(unsigned) * CHAR_BIT
);
90 unsigned cur_bit
= cursor
% (sizeof(unsigned) * CHAR_BIT
);
94 if (cur_bit
+ how_many_bits
> sizeof(unsigned) * CHAR_BIT
) {
95 unsigned lower
= grab_bits(cur_bit
, sizeof(unsigned) * CHAR_BIT
- cur_bit
,
97 unsigned upper
= grab_bits(0, cur_bit
+ how_many_bits
- sizeof(unsigned) * CHAR_BIT
,
98 bitstream
[cur_int
+ 1]);
99 return lower
| upper
<< (sizeof(unsigned) * CHAR_BIT
- cur_bit
);
102 return grab_bits(cur_bit
, how_many_bits
, bitstream
[cur_int
]);
105 bool vl_bitstream_parser_init(struct vl_bitstream_parser
*parser
,
106 unsigned num_bitstreams
,
107 const void **bitstreams
,
108 const unsigned *sizes
)
111 assert(num_bitstreams
);
115 parser
->num_bitstreams
= num_bitstreams
;
116 parser
->bitstreams
= (const unsigned**)bitstreams
;
117 parser
->sizes
= sizes
;
118 parser
->cur_bitstream
= 0;
124 void vl_bitstream_parser_cleanup(struct vl_bitstream_parser
*parser
)
130 vl_bitstream_parser_get_bits(struct vl_bitstream_parser
*parser
,
131 unsigned how_many_bits
)
137 bits
= vl_bitstream_parser_show_bits(parser
, how_many_bits
);
139 vl_bitstream_parser_forward(parser
, how_many_bits
);
145 vl_bitstream_parser_show_bits(struct vl_bitstream_parser
*parser
,
146 unsigned how_many_bits
)
151 unsigned cur_bitstream
;
155 cursor
= parser
->cursor
;
156 cur_bitstream
= parser
->cur_bitstream
;
159 unsigned bits_left
= parser
->sizes
[cur_bitstream
] * CHAR_BIT
- cursor
;
160 unsigned bits_to_show
= how_many_bits
> bits_left
? bits_left
: how_many_bits
;
162 bits
|= show_bits(cursor
, bits_to_show
,
163 parser
->bitstreams
[cur_bitstream
]) << shift
;
165 if (how_many_bits
> bits_to_show
) {
166 how_many_bits
-= bits_to_show
;
169 shift
+= bits_to_show
;
178 void vl_bitstream_parser_forward(struct vl_bitstream_parser
*parser
,
179 unsigned how_many_bits
)
182 assert(how_many_bits
);
184 parser
->cursor
+= how_many_bits
;
186 while (parser
->cursor
> parser
->sizes
[parser
->cur_bitstream
] * CHAR_BIT
) {
187 parser
->cursor
-= parser
->sizes
[parser
->cur_bitstream
++] * CHAR_BIT
;
188 assert(parser
->cur_bitstream
< parser
->num_bitstreams
);
192 void vl_bitstream_parser_rewind(struct vl_bitstream_parser
*parser
,
193 unsigned how_many_bits
)
198 assert(how_many_bits
);
200 c
= parser
->cursor
- how_many_bits
;
203 c
+= parser
->sizes
[parser
->cur_bitstream
--] * CHAR_BIT
;
204 assert(parser
->cur_bitstream
< parser
->num_bitstreams
);
207 parser
->cursor
= (unsigned)c
;