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 #define GST_BIT_READER_DISABLE_INLINES
26 #include "gstbitreader.h"
31 * SECTION:gstbitreader
32 * @short_description: Reads any number of bits from a memory buffer
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.
41 * @data: (array length=size): Data from which the #GstBitReader
43 * @size: Size of @data in bytes
45 * Create a new #GstBitReader instance, which will read from @data.
47 * Free-function: gst_bit_reader_free
49 * Returns: (transfer full): a new #GstBitReader instance
54 gst_bit_reader_new (const guint8 * data, guint size)
56 GstBitReader *ret = g_slice_new0 (GstBitReader);
65 * gst_bit_reader_free:
66 * @reader: (in) (transfer full): a #GstBitReader instance
68 * Frees a #GstBitReader instance, which was previously allocated by
69 * gst_bit_reader_new().
74 gst_bit_reader_free (GstBitReader * reader)
76 g_return_if_fail (reader != NULL);
78 g_slice_free (GstBitReader, reader);
82 * gst_bit_reader_init:
83 * @reader: a #GstBitReader instance
84 * @data: (in) (array length=size): data from which the bit reader should read
85 * @size: Size of @data in bytes
87 * Initializes a #GstBitReader instance to read from @data. This function
88 * can be called on already initialized instances.
93 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
95 g_return_if_fail (reader != NULL);
99 reader->byte = reader->bit = 0;
103 * gst_bit_reader_set_pos:
104 * @reader: a #GstBitReader instance
105 * @pos: The new position in bits
107 * Sets the new position of a #GstBitReader instance to @pos in bits.
109 * Returns: %TRUE if the position could be set successfully, %FALSE
115 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
117 g_return_val_if_fail (reader != NULL, FALSE);
119 if (pos > reader->size * 8)
122 reader->byte = pos / 8;
123 reader->bit = pos % 8;
129 * gst_bit_reader_get_pos:
130 * @reader: a #GstBitReader instance
132 * Returns the current position of a #GstBitReader instance in bits.
134 * Returns: The current position of @reader in bits.
139 gst_bit_reader_get_pos (const GstBitReader * reader)
141 return _gst_bit_reader_get_pos_inline (reader);
145 * gst_bit_reader_get_remaining:
146 * @reader: a #GstBitReader instance
148 * Returns the remaining number of bits of a #GstBitReader instance.
150 * Returns: The remaining number of bits of @reader instance.
155 gst_bit_reader_get_remaining (const GstBitReader * reader)
157 return _gst_bit_reader_get_remaining_inline (reader);
161 * gst_bit_reader_get_size:
162 * @reader: a #GstBitReader instance
164 * Returns the total number of bits of a #GstBitReader instance.
166 * Returns: The total number of bits of @reader instance.
171 gst_bit_reader_get_size (const GstBitReader * reader)
173 return _gst_bit_reader_get_size_inline (reader);
177 * gst_bit_reader_skip:
178 * @reader: a #GstBitReader instance
179 * @nbits: the number of bits to skip
181 * Skips @nbits bits of the #GstBitReader instance.
183 * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
188 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
190 return _gst_bit_reader_skip_inline (reader, nbits);
194 * gst_bit_reader_skip_to_byte:
195 * @reader: a #GstBitReader instance
197 * Skips until the next byte.
199 * Returns: %TRUE if successful, %FALSE otherwise.
204 gst_bit_reader_skip_to_byte (GstBitReader * reader)
206 return _gst_bit_reader_skip_to_byte_inline (reader);
210 * gst_bit_reader_get_bits_uint8:
211 * @reader: a #GstBitReader instance
212 * @val: (out): Pointer to a #guint8 to store the result
213 * @nbits: number of bits to read
215 * Read @nbits bits into @val and update the current position.
217 * Returns: %TRUE if successful, %FALSE otherwise.
223 * gst_bit_reader_get_bits_uint16:
224 * @reader: a #GstBitReader instance
225 * @val: (out): Pointer to a #guint16 to store the result
226 * @nbits: number of bits to read
228 * Read @nbits bits into @val and update the current position.
230 * Returns: %TRUE if successful, %FALSE otherwise.
236 * gst_bit_reader_get_bits_uint32:
237 * @reader: a #GstBitReader instance
238 * @val: (out): Pointer to a #guint32 to store the result
239 * @nbits: number of bits to read
241 * Read @nbits bits into @val and update the current position.
243 * Returns: %TRUE if successful, %FALSE otherwise.
249 * gst_bit_reader_get_bits_uint64:
250 * @reader: a #GstBitReader instance
251 * @val: (out): Pointer to a #guint64 to store the result
252 * @nbits: number of bits to read
254 * Read @nbits bits into @val and update the current position.
256 * Returns: %TRUE if successful, %FALSE otherwise.
262 * gst_bit_reader_peek_bits_uint8:
263 * @reader: a #GstBitReader instance
264 * @val: (out): Pointer to a #guint8 to store the result
265 * @nbits: number of bits to read
267 * Read @nbits bits into @val but keep the current position.
269 * Returns: %TRUE if successful, %FALSE otherwise.
275 * gst_bit_reader_peek_bits_uint16:
276 * @reader: a #GstBitReader instance
277 * @val: (out): Pointer to a #guint16 to store the result
278 * @nbits: number of bits to read
280 * Read @nbits bits into @val but keep the current position.
282 * Returns: %TRUE if successful, %FALSE otherwise.
288 * gst_bit_reader_peek_bits_uint32:
289 * @reader: a #GstBitReader instance
290 * @val: (out): Pointer to a #guint32 to store the result
291 * @nbits: number of bits to read
293 * Read @nbits bits into @val but keep the current position.
295 * Returns: %TRUE if successful, %FALSE otherwise.
301 * gst_bit_reader_peek_bits_uint64:
302 * @reader: a #GstBitReader instance
303 * @val: (out): Pointer to a #guint64 to store the result
304 * @nbits: number of bits to read
306 * Read @nbits bits into @val but keep the current position.
308 * Returns: %TRUE if successful, %FALSE otherwise.
313 #define GST_BIT_READER_READ_BITS(bits) \
315 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
317 return _gst_bit_reader_peek_bits_uint##bits##_inline (reader, val, nbits); \
321 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
323 return _gst_bit_reader_get_bits_uint##bits##_inline (reader, val, nbits); \
326 GST_BIT_READER_READ_BITS (8);
327 GST_BIT_READER_READ_BITS (16);
328 GST_BIT_READER_READ_BITS (32);
329 GST_BIT_READER_READ_BITS (64);