Merge branch 'master' into pipe-video
[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 #include <stdio.h>
33
34 inline void endian_swap_ushort(unsigned short *x)
35 {
36 x[0] = (x[0]>>8) |
37 (x[0]<<8);
38 }
39
40 inline void endian_swap_uint(unsigned int *x)
41 {
42 x[0] = (x[0]>>24) |
43 ((x[0]<<8) & 0x00FF0000) |
44 ((x[0]>>8) & 0x0000FF00) |
45 (x[0]<<24);
46 }
47
48 inline void endian_swap_ulonglong(unsigned long long *x)
49 {
50 x[0] = (x[0]>>56) |
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) |
57 (x[0]<<56);
58 }
59
60 static unsigned
61 grab_bits(unsigned cursor, unsigned how_many_bits, unsigned bitstream_elt)
62 {
63 unsigned excess_bits = sizeof(unsigned) * CHAR_BIT - how_many_bits;
64
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);
68
69 #ifndef PIPE_ARCH_BIG_ENDIAN
70 switch (sizeof(unsigned)) {
71 case 2:
72 endian_swap_ushort(&bitstream_elt);
73 break;
74 case 4:
75 endian_swap_uint(&bitstream_elt);
76 break;
77 case 8:
78 endian_swap_ulonglong(&bitstream_elt);
79 break;
80 }
81 #endif // !PIPE_ARCH_BIG_ENDIAN
82
83 return (bitstream_elt << cursor) >> (excess_bits);
84 }
85
86 static unsigned
87 show_bits(unsigned cursor, unsigned how_many_bits, const unsigned *bitstream)
88 {
89 unsigned cur_int = cursor / (sizeof(unsigned) * CHAR_BIT);
90 unsigned cur_bit = cursor % (sizeof(unsigned) * CHAR_BIT);
91
92 assert(bitstream);
93
94 if (cur_bit + how_many_bits > sizeof(unsigned) * CHAR_BIT) {
95 unsigned lower = grab_bits(cur_bit, sizeof(unsigned) * CHAR_BIT - cur_bit,
96 bitstream[cur_int]);
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);
100 }
101 else
102 return grab_bits(cur_bit, how_many_bits, bitstream[cur_int]);
103 }
104
105 bool vl_bitstream_parser_init(struct vl_bitstream_parser *parser,
106 unsigned num_bitstreams,
107 const void **bitstreams,
108 const unsigned *sizes)
109 {
110 assert(parser);
111 assert(num_bitstreams);
112 assert(bitstreams);
113 assert(sizes);
114
115 parser->num_bitstreams = num_bitstreams;
116 parser->bitstreams = (const unsigned**)bitstreams;
117 parser->sizes = sizes;
118 parser->cur_bitstream = 0;
119 parser->cursor = 0;
120
121 return true;
122 }
123
124 void vl_bitstream_parser_cleanup(struct vl_bitstream_parser *parser)
125 {
126 assert(parser);
127 }
128
129 unsigned
130 vl_bitstream_parser_get_bits(struct vl_bitstream_parser *parser,
131 unsigned how_many_bits)
132 {
133 unsigned bits;
134
135 assert(parser);
136
137 bits = vl_bitstream_parser_show_bits(parser, how_many_bits);
138
139 vl_bitstream_parser_forward(parser, how_many_bits);
140
141 return bits;
142 }
143
144 unsigned
145 vl_bitstream_parser_show_bits(struct vl_bitstream_parser *parser,
146 unsigned how_many_bits)
147 {
148 unsigned bits = 0;
149 unsigned shift = 0;
150 unsigned cursor;
151 unsigned cur_bitstream;
152
153 assert(parser);
154
155 cursor = parser->cursor;
156 cur_bitstream = parser->cur_bitstream;
157
158 while (1) {
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;
161
162 bits |= show_bits(cursor, bits_to_show,
163 parser->bitstreams[cur_bitstream]) << shift;
164
165 if (how_many_bits > bits_to_show) {
166 how_many_bits -= bits_to_show;
167 cursor = 0;
168 ++cur_bitstream;
169 shift += bits_to_show;
170 }
171 else
172 break;
173 }
174
175 return bits;
176 }
177
178 void vl_bitstream_parser_forward(struct vl_bitstream_parser *parser,
179 unsigned how_many_bits)
180 {
181 assert(parser);
182 assert(how_many_bits);
183
184 parser->cursor += how_many_bits;
185
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);
189 }
190 }
191
192 void vl_bitstream_parser_rewind(struct vl_bitstream_parser *parser,
193 unsigned how_many_bits)
194 {
195 signed c;
196
197 assert(parser);
198 assert(how_many_bits);
199
200 c = parser->cursor - how_many_bits;
201
202 while (c < 0) {
203 c += parser->sizes[parser->cur_bitstream--] * CHAR_BIT;
204 assert(parser->cur_bitstream < parser->num_bitstreams);
205 }
206
207 parser->cursor = (unsigned)c;
208 }