3 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
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.
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.
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.
25 #include "gstbitreader.h"
30 * SECTION:gstbitreader
31 * @short_description: Reads any number of bits from a memory buffer
33 * #GstBitReader provides a bit reader that can read any number of bits
34 * from a memory buffer. It provides functions for reading any number of bits
35 * into 8, 16, 32 and 64 bit variables.
40 * @data: Data from which the #GstBitReader should read
41 * @size: Size of @data in bytes
43 * Create a new #GstBitReader instance, which will read from @data.
45 * Returns: a new #GstBitReader instance
50 gst_bit_reader_new (const guint8 * data, guint size)
52 GstBitReader *ret = g_slice_new0 (GstBitReader);
61 * gst_bit_reader_new_from_buffer:
62 * @buffer: Buffer from which the #GstBitReader should read
64 * Create a new #GstBitReader instance, which will read from the
67 * Returns: a new #GstBitReader instance
72 gst_bit_reader_new_from_buffer (const GstBuffer * buffer)
74 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
76 return gst_bit_reader_new (GST_BUFFER_DATA (buffer),
77 GST_BUFFER_SIZE (buffer));
81 * gst_bit_reader_free:
82 * @reader: a #GstBitReader instance
84 * Frees a #GstBitReader instance, which was previously allocated by
85 * gst_bit_reader_new() or gst_bit_reader_new_from_buffer().
90 gst_bit_reader_free (GstBitReader * reader)
92 g_return_if_fail (reader != NULL);
94 g_slice_free (GstBitReader, reader);
98 * gst_bit_reader_init:
99 * @reader: a #GstBitReader instance
100 * @data: Data from which the #GstBitReader should read
101 * @size: Size of @data in bytes
103 * Initializes a #GstBitReader instance to read from @data. This function
104 * can be called on already initialized instances.
109 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
111 g_return_if_fail (reader != NULL);
115 reader->byte = reader->bit = 0;
119 * gst_bit_reader_init:
120 * @reader: a #GstBitReader instance
121 * @buffer: Buffer from which the #GstBitReader should read
123 * Initializes a #GstBitReader instance to read from @buffer. This function
124 * can be called on already initialized instances.
129 gst_bit_reader_init_from_buffer (GstBitReader * reader,
130 const GstBuffer * buffer)
132 g_return_if_fail (GST_IS_BUFFER (buffer));
134 gst_bit_reader_init (reader, GST_BUFFER_DATA (buffer),
135 GST_BUFFER_SIZE (buffer));
139 * gst_bit_reader_set_pos:
140 * @reader: a #GstBitReader instance
141 * @pos: The new position in bits
143 * Sets the new position of a #GstBitReader instance to @pos in bits.
145 * Returns: %TRUE if the position could be set successfully, %FALSE
151 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
153 g_return_val_if_fail (reader != NULL, FALSE);
155 if (pos > reader->size * 8)
158 reader->byte = pos / 8;
159 reader->bit = pos % 8;
165 * gst_bit_reader_get_pos:
166 * @reader: a #GstBitReader instance
168 * Returns the current position of a #GstBitReader instance in bits.
170 * Returns: The current position of @reader in bits.
175 gst_bit_reader_get_pos (const GstBitReader * reader)
177 g_return_val_if_fail (reader != NULL, 0);
179 return reader->byte * 8 + reader->bit;
183 * gst_bit_reader_get_remaining:
184 * @reader: a #GstBitReader instance
186 * Returns the remaining number of bits of a #GstBitReader instance.
188 * Returns: The remaining number of bits of @reader instance.
193 gst_bit_reader_get_remaining (const GstBitReader * reader)
195 g_return_val_if_fail (reader != NULL, 0);
197 return reader->size * 8 - (reader->byte * 8 + reader->bit);
201 * gst_bit_reader_skip:
202 * @reader: a #GstBitReader instance
203 * @nbits: the number of bits to skip
205 * Skips @nbits bits of the #GstBitReader instance.
207 * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
212 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
214 g_return_val_if_fail (reader != NULL, FALSE);
216 if (gst_bit_reader_get_remaining (reader) < nbits)
219 reader->bit += nbits;
220 reader->byte += reader->bit / 8;
221 reader->bit = reader->bit % 8;
227 * gst_bit_reader_skip_to_byte:
228 * @reader: a #GstBitReader instance
230 * Skips until the next byte.
232 * Returns: %TRUE if successful, %FALSE otherwise.
237 gst_bit_reader_skip_to_byte (GstBitReader * reader)
239 g_return_val_if_fail (reader != NULL, FALSE);
241 if (reader->byte > reader->size)
253 * gst_bit_reader_get_bits_uint8:
254 * @reader: a #GstBitReader instance
255 * @val: Pointer to a #guint8 to store the result
256 * @nbits: number of bits to read
258 * Read @nbits bits into @val and update the current position.
260 * Returns: %TRUE if successful, %FALSE otherwise.
266 * gst_bit_reader_get_bits_uint16:
267 * @reader: a #GstBitReader instance
268 * @val: Pointer to a #guint16 to store the result
269 * @nbits: number of bits to read
271 * Read @nbits bits into @val and update the current position.
273 * Returns: %TRUE if successful, %FALSE otherwise.
279 * gst_bit_reader_get_bits_uint32:
280 * @reader: a #GstBitReader instance
281 * @val: Pointer to a #guint32 to store the result
282 * @nbits: number of bits to read
284 * Read @nbits bits into @val and update the current position.
286 * Returns: %TRUE if successful, %FALSE otherwise.
292 * gst_bit_reader_get_bits_uint64:
293 * @reader: a #GstBitReader instance
294 * @val: Pointer to a #guint64 to store the result
295 * @nbits: number of bits to read
297 * Read @nbits bits into @val and update the current position.
299 * Returns: %TRUE if successful, %FALSE otherwise.
305 * gst_bit_reader_peek_bits_uint8:
306 * @reader: a #GstBitReader instance
307 * @val: Pointer to a #guint8 to store the result
308 * @nbits: number of bits to read
310 * Read @nbits bits into @val but keep the current position.
312 * Returns: %TRUE if successful, %FALSE otherwise.
318 * gst_bit_reader_peek_bits_uint16:
319 * @reader: a #GstBitReader instance
320 * @val: Pointer to a #guint16 to store the result
321 * @nbits: number of bits to read
323 * Read @nbits bits into @val but keep the current position.
325 * Returns: %TRUE if successful, %FALSE otherwise.
331 * gst_bit_reader_peek_bits_uint32:
332 * @reader: a #GstBitReader instance
333 * @val: Pointer to a #guint32 to store the result
334 * @nbits: number of bits to read
336 * Read @nbits bits into @val but keep the current position.
338 * Returns: %TRUE if successful, %FALSE otherwise.
344 * gst_bit_reader_peek_bits_uint64:
345 * @reader: a #GstBitReader instance
346 * @val: Pointer to a #guint64 to store the result
347 * @nbits: number of bits to read
349 * Read @nbits bits into @val but keep the current position.
351 * Returns: %TRUE if successful, %FALSE otherwise.
356 #define GST_BIT_READER_READ_BITS(bits) \
358 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
360 guint##bits ret = 0; \
362 g_return_val_if_fail (reader != NULL, FALSE); \
363 g_return_val_if_fail (val != NULL, FALSE); \
364 g_return_val_if_fail (nbits <= bits, FALSE); \
366 if (reader->byte * 8 + reader->bit + nbits > reader->size * 8) \
369 while (nbits > 0) { \
370 guint toread = MIN (nbits, 8 - reader->bit); \
373 ret |= (reader->data[reader->byte] & (0xff >> reader->bit)) >> (8 - toread - reader->bit); \
375 reader->bit += toread; \
376 if (reader->bit >= 8) { \
388 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
392 g_return_val_if_fail (reader != NULL, FALSE); \
394 return gst_bit_reader_get_bits_uint##bits (&tmp, val, nbits); \
397 GST_BIT_READER_READ_BITS (8);
398 GST_BIT_READER_READ_BITS (16);
399 GST_BIT_READER_READ_BITS (32);
400 GST_BIT_READER_READ_BITS (64);