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"
29 /* FIXME 0.11: inline everything and get rid of non-inlined functions */
32 * SECTION:gstbitreader
33 * @short_description: Reads any number of bits from a memory buffer
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.
42 * @data: Data from which the #GstBitReader should read
43 * @size: Size of @data in bytes
45 * Create a new #GstBitReader instance, which will read from @data.
47 * Returns: a new #GstBitReader instance
52 gst_bit_reader_new (const guint8 * data, guint size)
54 GstBitReader *ret = g_slice_new0 (GstBitReader);
63 * gst_bit_reader_new_from_buffer:
64 * @buffer: Buffer from which the #GstBitReader should read
66 * Create a new #GstBitReader instance, which will read from the
69 * Returns: a new #GstBitReader instance
74 gst_bit_reader_new_from_buffer (const GstBuffer * buffer)
76 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
78 return gst_bit_reader_new (GST_BUFFER_DATA (buffer),
79 GST_BUFFER_SIZE (buffer));
83 * gst_bit_reader_free:
84 * @reader: a #GstBitReader instance
86 * Frees a #GstBitReader instance, which was previously allocated by
87 * gst_bit_reader_new() or gst_bit_reader_new_from_buffer().
92 gst_bit_reader_free (GstBitReader * reader)
94 g_return_if_fail (reader != NULL);
96 g_slice_free (GstBitReader, reader);
100 * gst_bit_reader_init:
101 * @reader: a #GstBitReader instance
102 * @data: Data from which the #GstBitReader should read
103 * @size: Size of @data in bytes
105 * Initializes a #GstBitReader instance to read from @data. This function
106 * can be called on already initialized instances.
111 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
113 g_return_if_fail (reader != NULL);
117 reader->byte = reader->bit = 0;
121 * gst_bit_reader_init_from_buffer:
122 * @reader: a #GstBitReader instance
123 * @buffer: Buffer from which the #GstBitReader should read
125 * Initializes a #GstBitReader instance to read from @buffer. This function
126 * can be called on already initialized instances.
131 gst_bit_reader_init_from_buffer (GstBitReader * reader,
132 const GstBuffer * buffer)
134 g_return_if_fail (GST_IS_BUFFER (buffer));
136 gst_bit_reader_init (reader, GST_BUFFER_DATA (buffer),
137 GST_BUFFER_SIZE (buffer));
141 * gst_bit_reader_set_pos:
142 * @reader: a #GstBitReader instance
143 * @pos: The new position in bits
145 * Sets the new position of a #GstBitReader instance to @pos in bits.
147 * Returns: %TRUE if the position could be set successfully, %FALSE
153 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
155 g_return_val_if_fail (reader != NULL, FALSE);
157 if (pos > reader->size * 8)
160 reader->byte = pos / 8;
161 reader->bit = pos % 8;
167 * gst_bit_reader_get_pos:
168 * @reader: a #GstBitReader instance
170 * Returns the current position of a #GstBitReader instance in bits.
172 * Returns: The current position of @reader in bits.
177 gst_bit_reader_get_pos (const GstBitReader * reader)
179 g_return_val_if_fail (reader != NULL, 0);
181 return reader->byte * 8 + reader->bit;
185 * gst_bit_reader_get_remaining:
186 * @reader: a #GstBitReader instance
188 * Returns the remaining number of bits of a #GstBitReader instance.
190 * Returns: The remaining number of bits of @reader instance.
195 gst_bit_reader_get_remaining (const GstBitReader * reader)
197 g_return_val_if_fail (reader != NULL, 0);
199 return reader->size * 8 - (reader->byte * 8 + reader->bit);
203 * gst_bit_reader_skip:
204 * @reader: a #GstBitReader instance
205 * @nbits: the number of bits to skip
207 * Skips @nbits bits of the #GstBitReader instance.
209 * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
214 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
216 g_return_val_if_fail (reader != NULL, FALSE);
218 if (gst_bit_reader_get_remaining (reader) < nbits)
221 reader->bit += nbits;
222 reader->byte += reader->bit / 8;
223 reader->bit = reader->bit % 8;
229 * gst_bit_reader_skip_to_byte:
230 * @reader: a #GstBitReader instance
232 * Skips until the next byte.
234 * Returns: %TRUE if successful, %FALSE otherwise.
239 gst_bit_reader_skip_to_byte (GstBitReader * reader)
241 g_return_val_if_fail (reader != NULL, FALSE);
243 if (reader->byte > reader->size)
255 * gst_bit_reader_get_bits_uint8:
256 * @reader: a #GstBitReader instance
257 * @val: Pointer to a #guint8 to store the result
258 * @nbits: number of bits to read
260 * Read @nbits bits into @val and update the current position.
262 * Returns: %TRUE if successful, %FALSE otherwise.
268 * gst_bit_reader_get_bits_uint16:
269 * @reader: a #GstBitReader instance
270 * @val: Pointer to a #guint16 to store the result
271 * @nbits: number of bits to read
273 * Read @nbits bits into @val and update the current position.
275 * Returns: %TRUE if successful, %FALSE otherwise.
281 * gst_bit_reader_get_bits_uint32:
282 * @reader: a #GstBitReader instance
283 * @val: Pointer to a #guint32 to store the result
284 * @nbits: number of bits to read
286 * Read @nbits bits into @val and update the current position.
288 * Returns: %TRUE if successful, %FALSE otherwise.
294 * gst_bit_reader_get_bits_uint64:
295 * @reader: a #GstBitReader instance
296 * @val: Pointer to a #guint64 to store the result
297 * @nbits: number of bits to read
299 * Read @nbits bits into @val and update the current position.
301 * Returns: %TRUE if successful, %FALSE otherwise.
307 * gst_bit_reader_peek_bits_uint8:
308 * @reader: a #GstBitReader instance
309 * @val: Pointer to a #guint8 to store the result
310 * @nbits: number of bits to read
312 * Read @nbits bits into @val but keep the current position.
314 * Returns: %TRUE if successful, %FALSE otherwise.
320 * gst_bit_reader_peek_bits_uint16:
321 * @reader: a #GstBitReader instance
322 * @val: Pointer to a #guint16 to store the result
323 * @nbits: number of bits to read
325 * Read @nbits bits into @val but keep the current position.
327 * Returns: %TRUE if successful, %FALSE otherwise.
333 * gst_bit_reader_peek_bits_uint32:
334 * @reader: a #GstBitReader instance
335 * @val: Pointer to a #guint32 to store the result
336 * @nbits: number of bits to read
338 * Read @nbits bits into @val but keep the current position.
340 * Returns: %TRUE if successful, %FALSE otherwise.
346 * gst_bit_reader_peek_bits_uint64:
347 * @reader: a #GstBitReader instance
348 * @val: Pointer to a #guint64 to store the result
349 * @nbits: number of bits to read
351 * Read @nbits bits into @val but keep the current position.
353 * Returns: %TRUE if successful, %FALSE otherwise.
358 #define GST_BIT_READER_READ_BITS(bits) \
360 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
362 guint##bits ret = 0; \
364 g_return_val_if_fail (reader != NULL, FALSE); \
365 g_return_val_if_fail (val != NULL, FALSE); \
366 g_return_val_if_fail (nbits <= bits, FALSE); \
368 if (reader->byte * 8 + reader->bit + nbits > reader->size * 8) \
371 while (nbits > 0) { \
372 guint toread = MIN (nbits, 8 - reader->bit); \
375 ret |= (reader->data[reader->byte] & (0xff >> reader->bit)) >> (8 - toread - reader->bit); \
377 reader->bit += toread; \
378 if (reader->bit >= 8) { \
390 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
394 g_return_val_if_fail (reader != NULL, FALSE); \
396 return gst_bit_reader_get_bits_uint##bits (&tmp, val, nbits); \
399 GST_BIT_READER_READ_BITS (8);
400 GST_BIT_READER_READ_BITS (16);
401 GST_BIT_READER_READ_BITS (32);
402 GST_BIT_READER_READ_BITS (64);