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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #define GST_BIT_READER_DISABLE_INLINES
26 #include "gstbitreader.h"
28 #include <string.h>
30 /**
31 * SECTION:gstbitreader
32 * @short_description: Reads any number of bits from a memory buffer
33 *
34 * #GstBitReader provides a bit reader that can read any number of bits
35 * from a memory buffer. It provides functions for reading any number of bits
36 * into 8, 16, 32 and 64 bit variables.
37 */
39 /**
40 * gst_bit_reader_new:
41 * @data: Data from which the #GstBitReader should read
42 * @size: Size of @data in bytes
43 *
44 * Create a new #GstBitReader instance, which will read from @data.
45 *
46 * Free-function: gst_bit_reader_free
47 *
48 * Returns: (transfer full): a new #GstBitReader instance
49 *
50 * Since: 0.10.22
51 */
52 GstBitReader *
53 gst_bit_reader_new (const guint8 * data, guint size)
54 {
55 GstBitReader *ret = g_slice_new0 (GstBitReader);
57 ret->data = data;
58 ret->size = size;
60 return ret;
61 }
63 /**
64 * gst_bit_reader_new_from_buffer:
65 * @buffer: Buffer from which the #GstBitReader should read
66 *
67 * Create a new #GstBitReader instance, which will read from the
68 * #GstBuffer @buffer.
69 *
70 * Free-function: gst_bit_reader_free
71 *
72 * Returns: (transfer full): a new #GstBitReader instance
73 *
74 * Since: 0.10.22
75 */
76 GstBitReader *
77 gst_bit_reader_new_from_buffer (const GstBuffer * buffer)
78 {
79 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
81 return gst_bit_reader_new (GST_BUFFER_DATA (buffer),
82 GST_BUFFER_SIZE (buffer));
83 }
85 /**
86 * gst_bit_reader_free:
87 * @reader: (in) (transfer full): a #GstBitReader instance
88 *
89 * Frees a #GstBitReader instance, which was previously allocated by
90 * gst_bit_reader_new() or gst_bit_reader_new_from_buffer().
91 *
92 * Since: 0.10.22
93 */
94 void
95 gst_bit_reader_free (GstBitReader * reader)
96 {
97 g_return_if_fail (reader != NULL);
99 g_slice_free (GstBitReader, reader);
100 }
102 /**
103 * gst_bit_reader_init:
104 * @reader: a #GstBitReader instance
105 * @data: (in) (array length=size): data from which the bit reader should read
106 * @size: Size of @data in bytes
107 *
108 * Initializes a #GstBitReader instance to read from @data. This function
109 * can be called on already initialized instances.
110 *
111 * Since: 0.10.22
112 */
113 void
114 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
115 {
116 g_return_if_fail (reader != NULL);
118 reader->data = data;
119 reader->size = size;
120 reader->byte = reader->bit = 0;
121 }
123 /**
124 * gst_bit_reader_init_from_buffer:
125 * @reader: a #GstBitReader instance
126 * @buffer: (transfer none): Buffer from which the #GstBitReader should read
127 *
128 * Initializes a #GstBitReader instance to read from @buffer. This function
129 * can be called on already initialized instances.
130 *
131 * Since: 0.10.22
132 */
133 void
134 gst_bit_reader_init_from_buffer (GstBitReader * reader,
135 const GstBuffer * buffer)
136 {
137 g_return_if_fail (GST_IS_BUFFER (buffer));
139 gst_bit_reader_init (reader, GST_BUFFER_DATA (buffer),
140 GST_BUFFER_SIZE (buffer));
141 }
143 /**
144 * gst_bit_reader_set_pos:
145 * @reader: a #GstBitReader instance
146 * @pos: The new position in bits
147 *
148 * Sets the new position of a #GstBitReader instance to @pos in bits.
149 *
150 * Returns: %TRUE if the position could be set successfully, %FALSE
151 * otherwise.
152 *
153 * Since: 0.10.22
154 */
155 gboolean
156 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
157 {
158 g_return_val_if_fail (reader != NULL, FALSE);
160 if (pos > reader->size * 8)
161 return FALSE;
163 reader->byte = pos / 8;
164 reader->bit = pos % 8;
166 return TRUE;
167 }
169 /**
170 * gst_bit_reader_get_pos:
171 * @reader: a #GstBitReader instance
172 *
173 * Returns the current position of a #GstBitReader instance in bits.
174 *
175 * Returns: The current position of @reader in bits.
176 *
177 * Since: 0.10.22
178 */
179 guint
180 gst_bit_reader_get_pos (const GstBitReader * reader)
181 {
182 return _gst_bit_reader_get_pos_inline (reader);
183 }
185 /**
186 * gst_bit_reader_get_remaining:
187 * @reader: a #GstBitReader instance
188 *
189 * Returns the remaining number of bits of a #GstBitReader instance.
190 *
191 * Returns: The remaining number of bits of @reader instance.
192 *
193 * Since: 0.10.22
194 */
195 guint
196 gst_bit_reader_get_remaining (const GstBitReader * reader)
197 {
198 return _gst_bit_reader_get_remaining_inline (reader);
199 }
201 /**
202 * gst_bit_reader_get_size:
203 * @reader: a #GstBitReader instance
204 *
205 * Returns the total number of bits of a #GstBitReader instance.
206 *
207 * Returns: The total number of bits of @reader instance.
208 *
209 * Since: 0.10.26
210 */
211 guint
212 gst_bit_reader_get_size (const GstBitReader * reader)
213 {
214 return _gst_bit_reader_get_size_inline (reader);
215 }
217 /**
218 * gst_bit_reader_skip:
219 * @reader: a #GstBitReader instance
220 * @nbits: the number of bits to skip
221 *
222 * Skips @nbits bits of the #GstBitReader instance.
223 *
224 * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
225 *
226 * Since: 0.10.22
227 */
228 gboolean
229 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
230 {
231 return _gst_bit_reader_skip_inline (reader, nbits);
232 }
234 /**
235 * gst_bit_reader_skip_to_byte:
236 * @reader: a #GstBitReader instance
237 *
238 * Skips until the next byte.
239 *
240 * Returns: %TRUE if successful, %FALSE otherwise.
241 *
242 * Since: 0.10.22
243 */
244 gboolean
245 gst_bit_reader_skip_to_byte (GstBitReader * reader)
246 {
247 return _gst_bit_reader_skip_to_byte_inline (reader);
248 }
250 /**
251 * gst_bit_reader_get_bits_uint8:
252 * @reader: a #GstBitReader instance
253 * @val: (out): Pointer to a #guint8 to store the result
254 * @nbits: number of bits to read
255 *
256 * Read @nbits bits into @val and update the current position.
257 *
258 * Returns: %TRUE if successful, %FALSE otherwise.
259 *
260 * Since: 0.10.22
261 */
263 /**
264 * gst_bit_reader_get_bits_uint16:
265 * @reader: a #GstBitReader instance
266 * @val: (out): Pointer to a #guint16 to store the result
267 * @nbits: number of bits to read
268 *
269 * Read @nbits bits into @val and update the current position.
270 *
271 * Returns: %TRUE if successful, %FALSE otherwise.
272 *
273 * Since: 0.10.22
274 */
276 /**
277 * gst_bit_reader_get_bits_uint32:
278 * @reader: a #GstBitReader instance
279 * @val: (out): Pointer to a #guint32 to store the result
280 * @nbits: number of bits to read
281 *
282 * Read @nbits bits into @val and update the current position.
283 *
284 * Returns: %TRUE if successful, %FALSE otherwise.
285 *
286 * Since: 0.10.22
287 */
289 /**
290 * gst_bit_reader_get_bits_uint64:
291 * @reader: a #GstBitReader instance
292 * @val: (out): Pointer to a #guint64 to store the result
293 * @nbits: number of bits to read
294 *
295 * Read @nbits bits into @val and update the current position.
296 *
297 * Returns: %TRUE if successful, %FALSE otherwise.
298 *
299 * Since: 0.10.22
300 */
302 /**
303 * gst_bit_reader_peek_bits_uint8:
304 * @reader: a #GstBitReader instance
305 * @val: (out): Pointer to a #guint8 to store the result
306 * @nbits: number of bits to read
307 *
308 * Read @nbits bits into @val but keep the current position.
309 *
310 * Returns: %TRUE if successful, %FALSE otherwise.
311 *
312 * Since: 0.10.22
313 */
315 /**
316 * gst_bit_reader_peek_bits_uint16:
317 * @reader: a #GstBitReader instance
318 * @val: (out): Pointer to a #guint16 to store the result
319 * @nbits: number of bits to read
320 *
321 * Read @nbits bits into @val but keep the current position.
322 *
323 * Returns: %TRUE if successful, %FALSE otherwise.
324 *
325 * Since: 0.10.22
326 */
328 /**
329 * gst_bit_reader_peek_bits_uint32:
330 * @reader: a #GstBitReader instance
331 * @val: (out): Pointer to a #guint32 to store the result
332 * @nbits: number of bits to read
333 *
334 * Read @nbits bits into @val but keep the current position.
335 *
336 * Returns: %TRUE if successful, %FALSE otherwise.
337 *
338 * Since: 0.10.22
339 */
341 /**
342 * gst_bit_reader_peek_bits_uint64:
343 * @reader: a #GstBitReader instance
344 * @val: (out): Pointer to a #guint64 to store the result
345 * @nbits: number of bits to read
346 *
347 * Read @nbits bits into @val but keep the current position.
348 *
349 * Returns: %TRUE if successful, %FALSE otherwise.
350 *
351 * Since: 0.10.22
352 */
354 #define GST_BIT_READER_READ_BITS(bits) \
355 gboolean \
356 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
357 { \
358 return _gst_bit_reader_peek_bits_uint##bits##_inline (reader, val, nbits); \
359 } \
360 \
361 gboolean \
362 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
363 { \
364 return _gst_bit_reader_get_bits_uint##bits##_inline (reader, val, nbits); \
365 }
367 GST_BIT_READER_READ_BITS (8);
368 GST_BIT_READER_READ_BITS (16);
369 GST_BIT_READER_READ_BITS (32);
370 GST_BIT_READER_READ_BITS (64);