1 /* GStreamer
2 *
3 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #define GST_BIT_READER_DISABLE_INLINES
26 #include "gstbitreader.h"
27
28 #include <string.h>
29
30 /**
31 * SECTION:gstbitreader
32 * @title: GstBitReader
33 * @short_description: Reads any number of bits from a memory buffer
34 *
35 * #GstBitReader provides a bit reader that can read any number of bits
36 * from a memory buffer. It provides functions for reading any number of bits
37 * into 8, 16, 32 and 64 bit variables.
38 */
39
40 /**
41 * gst_bit_reader_new: (skip)
42 * @data: (array length=size): Data from which the #GstBitReader
43 * should read
44 * @size: Size of @data in bytes
45 *
46 * Create a new #GstBitReader instance, which will read from @data.
47 *
48 * Free-function: gst_bit_reader_free
49 *
50 * Returns: (transfer full): a new #GstBitReader instance
51 */
52 GstBitReader *
gst_bit_reader_new(const guint8 * data,guint size)53 gst_bit_reader_new (const guint8 * data, guint size)
54 {
55 GstBitReader *ret = g_slice_new0 (GstBitReader);
56
57 ret->data = data;
58 ret->size = size;
59
60 return ret;
61 }
62
63 /**
64 * gst_bit_reader_free:
65 * @reader: (in) (transfer full): a #GstBitReader instance
66 *
67 * Frees a #GstBitReader instance, which was previously allocated by
68 * gst_bit_reader_new().
69 */
70 void
gst_bit_reader_free(GstBitReader * reader)71 gst_bit_reader_free (GstBitReader * reader)
72 {
73 g_return_if_fail (reader != NULL);
74
75 g_slice_free (GstBitReader, reader);
76 }
77
78 /**
79 * gst_bit_reader_init:
80 * @reader: a #GstBitReader instance
81 * @data: (in) (array length=size): data from which the bit reader should read
82 * @size: Size of @data in bytes
83 *
84 * Initializes a #GstBitReader instance to read from @data. This function
85 * can be called on already initialized instances.
86 */
87 void
gst_bit_reader_init(GstBitReader * reader,const guint8 * data,guint size)88 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
89 {
90 g_return_if_fail (reader != NULL);
91
92 reader->data = data;
93 reader->size = size;
94 reader->byte = reader->bit = 0;
95 }
96
97 /**
98 * gst_bit_reader_set_pos:
99 * @reader: a #GstBitReader instance
100 * @pos: The new position in bits
101 *
102 * Sets the new position of a #GstBitReader instance to @pos in bits.
103 *
104 * Returns: %TRUE if the position could be set successfully, %FALSE
105 * otherwise.
106 */
107 gboolean
gst_bit_reader_set_pos(GstBitReader * reader,guint pos)108 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
109 {
110 g_return_val_if_fail (reader != NULL, FALSE);
111
112 if (pos > reader->size * 8)
113 return FALSE;
114
115 reader->byte = pos / 8;
116 reader->bit = pos % 8;
117
118 return TRUE;
119 }
120
121 /**
122 * gst_bit_reader_get_pos:
123 * @reader: a #GstBitReader instance
124 *
125 * Returns the current position of a #GstBitReader instance in bits.
126 *
127 * Returns: The current position of @reader in bits.
128 */
129 guint
gst_bit_reader_get_pos(const GstBitReader * reader)130 gst_bit_reader_get_pos (const GstBitReader * reader)
131 {
132 return _gst_bit_reader_get_pos_inline (reader);
133 }
134
135 /**
136 * gst_bit_reader_get_remaining:
137 * @reader: a #GstBitReader instance
138 *
139 * Returns the remaining number of bits of a #GstBitReader instance.
140 *
141 * Returns: The remaining number of bits of @reader instance.
142 */
143 guint
gst_bit_reader_get_remaining(const GstBitReader * reader)144 gst_bit_reader_get_remaining (const GstBitReader * reader)
145 {
146 return _gst_bit_reader_get_remaining_inline (reader);
147 }
148
149 /**
150 * gst_bit_reader_get_size:
151 * @reader: a #GstBitReader instance
152 *
153 * Returns the total number of bits of a #GstBitReader instance.
154 *
155 * Returns: The total number of bits of @reader instance.
156 */
157 guint
gst_bit_reader_get_size(const GstBitReader * reader)158 gst_bit_reader_get_size (const GstBitReader * reader)
159 {
160 return _gst_bit_reader_get_size_inline (reader);
161 }
162
163 /**
164 * gst_bit_reader_skip:
165 * @reader: a #GstBitReader instance
166 * @nbits: the number of bits to skip
167 *
168 * Skips @nbits bits of the #GstBitReader instance.
169 *
170 * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
171 */
172 gboolean
gst_bit_reader_skip(GstBitReader * reader,guint nbits)173 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
174 {
175 return _gst_bit_reader_skip_inline (reader, nbits);
176 }
177
178 /**
179 * gst_bit_reader_skip_to_byte:
180 * @reader: a #GstBitReader instance
181 *
182 * Skips until the next byte.
183 *
184 * Returns: %TRUE if successful, %FALSE otherwise.
185 */
186 gboolean
gst_bit_reader_skip_to_byte(GstBitReader * reader)187 gst_bit_reader_skip_to_byte (GstBitReader * reader)
188 {
189 return _gst_bit_reader_skip_to_byte_inline (reader);
190 }
191
192 /**
193 * gst_bit_reader_get_bits_uint8:
194 * @reader: a #GstBitReader instance
195 * @val: (out): Pointer to a #guint8 to store the result
196 * @nbits: number of bits to read
197 *
198 * Read @nbits bits into @val and update the current position.
199 *
200 * Returns: %TRUE if successful, %FALSE otherwise.
201 */
202
203 /**
204 * gst_bit_reader_get_bits_uint16:
205 * @reader: a #GstBitReader instance
206 * @val: (out): Pointer to a #guint16 to store the result
207 * @nbits: number of bits to read
208 *
209 * Read @nbits bits into @val and update the current position.
210 *
211 * Returns: %TRUE if successful, %FALSE otherwise.
212 */
213
214 /**
215 * gst_bit_reader_get_bits_uint32:
216 * @reader: a #GstBitReader instance
217 * @val: (out): Pointer to a #guint32 to store the result
218 * @nbits: number of bits to read
219 *
220 * Read @nbits bits into @val and update the current position.
221 *
222 * Returns: %TRUE if successful, %FALSE otherwise.
223 */
224
225 /**
226 * gst_bit_reader_get_bits_uint64:
227 * @reader: a #GstBitReader instance
228 * @val: (out): Pointer to a #guint64 to store the result
229 * @nbits: number of bits to read
230 *
231 * Read @nbits bits into @val and update the current position.
232 *
233 * Returns: %TRUE if successful, %FALSE otherwise.
234 */
235
236 /**
237 * gst_bit_reader_peek_bits_uint8:
238 * @reader: a #GstBitReader instance
239 * @val: (out): Pointer to a #guint8 to store the result
240 * @nbits: number of bits to read
241 *
242 * Read @nbits bits into @val but keep the current position.
243 *
244 * Returns: %TRUE if successful, %FALSE otherwise.
245 */
246
247 /**
248 * gst_bit_reader_peek_bits_uint16:
249 * @reader: a #GstBitReader instance
250 * @val: (out): Pointer to a #guint16 to store the result
251 * @nbits: number of bits to read
252 *
253 * Read @nbits bits into @val but keep the current position.
254 *
255 * Returns: %TRUE if successful, %FALSE otherwise.
256 */
257
258 /**
259 * gst_bit_reader_peek_bits_uint32:
260 * @reader: a #GstBitReader instance
261 * @val: (out): Pointer to a #guint32 to store the result
262 * @nbits: number of bits to read
263 *
264 * Read @nbits bits into @val but keep the current position.
265 *
266 * Returns: %TRUE if successful, %FALSE otherwise.
267 */
268
269 /**
270 * gst_bit_reader_peek_bits_uint64:
271 * @reader: a #GstBitReader instance
272 * @val: (out): Pointer to a #guint64 to store the result
273 * @nbits: number of bits to read
274 *
275 * Read @nbits bits into @val but keep the current position.
276 *
277 * Returns: %TRUE if successful, %FALSE otherwise.
278 */
279
280 #define GST_BIT_READER_READ_BITS(bits) \
281 gboolean \
282 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
283 { \
284 return _gst_bit_reader_peek_bits_uint##bits##_inline (reader, val, nbits); \
285 } \
286 \
287 gboolean \
288 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
289 { \
290 return _gst_bit_reader_get_bits_uint##bits##_inline (reader, val, nbits); \
291 }
292
293 GST_BIT_READER_READ_BITS (8);
294 GST_BIT_READER_READ_BITS (16);
295 GST_BIT_READER_READ_BITS (32);
296 GST_BIT_READER_READ_BITS (64);
297