Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / auxiliary / vl / vl_bitstream_parser.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include "vl_bitstream_parser.h"
29 #include <assert.h>
30 #include <limits.h>
31 #include <util/u_memory.h>
32
33 static unsigned
34 grab_bits(unsigned cursor, unsigned how_many_bits, unsigned bitstream_elt)
35 {
36 unsigned excess_bits = sizeof(unsigned) * CHAR_BIT - how_many_bits - cursor;
37
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);
41
42 return (bitstream_elt << excess_bits) >> (excess_bits + cursor);
43 }
44
45 static unsigned
46 show_bits(unsigned cursor, unsigned how_many_bits, const unsigned *bitstream)
47 {
48 unsigned cur_int = cursor / (sizeof(unsigned) * CHAR_BIT);
49 unsigned cur_bit = cursor % (sizeof(unsigned) * CHAR_BIT);
50
51 assert(bitstream);
52
53 if (cur_bit + how_many_bits > sizeof(unsigned) * CHAR_BIT) {
54 unsigned lower = grab_bits(cur_bit, sizeof(unsigned) * CHAR_BIT - cur_bit,
55 bitstream[cur_int]);
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);
59 }
60 else
61 return grab_bits(cur_bit, how_many_bits, bitstream[cur_int]);
62 }
63
64 bool vl_bitstream_parser_init(struct vl_bitstream_parser *parser,
65 unsigned num_bitstreams,
66 const void **bitstreams,
67 const unsigned *sizes)
68 {
69 assert(parser);
70 assert(num_bitstreams);
71 assert(bitstreams);
72 assert(sizes);
73
74 parser->num_bitstreams = num_bitstreams;
75 parser->bitstreams = (const unsigned**)bitstreams;
76 parser->sizes = sizes;
77 parser->cur_bitstream = 0;
78 parser->cursor = 0;
79
80 return true;
81 }
82
83 void vl_bitstream_parser_cleanup(struct vl_bitstream_parser *parser)
84 {
85 assert(parser);
86 }
87
88 unsigned
89 vl_bitstream_parser_get_bits(struct vl_bitstream_parser *parser,
90 unsigned how_many_bits)
91 {
92 unsigned bits;
93
94 assert(parser);
95
96 bits = vl_bitstream_parser_show_bits(parser, how_many_bits);
97
98 vl_bitstream_parser_forward(parser, how_many_bits);
99
100 return bits;
101 }
102
103 unsigned
104 vl_bitstream_parser_show_bits(struct vl_bitstream_parser *parser,
105 unsigned how_many_bits)
106 {
107 unsigned bits = 0;
108 unsigned shift = 0;
109 unsigned cursor;
110 unsigned cur_bitstream;
111
112 assert(parser);
113
114 cursor = parser->cursor;
115 cur_bitstream = parser->cur_bitstream;
116
117 while (1) {
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;
120
121 bits |= show_bits(cursor, bits_to_show,
122 parser->bitstreams[cur_bitstream]) << shift;
123
124 if (how_many_bits > bits_to_show) {
125 how_many_bits -= bits_to_show;
126 cursor = 0;
127 ++cur_bitstream;
128 shift += bits_to_show;
129 }
130 else
131 break;
132 }
133
134 return bits;
135 }
136
137 void vl_bitstream_parser_forward(struct vl_bitstream_parser *parser,
138 unsigned how_many_bits)
139 {
140 assert(parser);
141 assert(how_many_bits);
142
143 parser->cursor += how_many_bits;
144
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);
148 }
149 }
150
151 void vl_bitstream_parser_rewind(struct vl_bitstream_parser *parser,
152 unsigned how_many_bits)
153 {
154 signed c;
155
156 assert(parser);
157 assert(how_many_bits);
158
159 c = parser->cursor - how_many_bits;
160
161 while (c < 0) {
162 c += parser->sizes[parser->cur_bitstream--] * CHAR_BIT;
163 assert(parser->cur_bitstream < parser->num_bitstreams);
164 }
165
166 parser->cursor = (unsigned)c;
167 }