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: Data from which the #GstBitReader should read
42 * @size: Size of @data in bytes
44 * Create a new #GstBitReader instance, which will read from @data.
46 * Free-function: gst_bit_reader_free
48 * Returns: (transfer full): a new #GstBitReader instance
53 gst_bit_reader_new (const guint8 * data, guint size)
55 GstBitReader *ret = g_slice_new0 (GstBitReader);
64 * gst_bit_reader_free:
65 * @reader: (in) (transfer full): a #GstBitReader instance
67 * Frees a #GstBitReader instance, which was previously allocated by
68 * gst_bit_reader_new().
73 gst_bit_reader_free (GstBitReader * reader)
75 g_return_if_fail (reader != NULL);
77 g_slice_free (GstBitReader, reader);
81 * gst_bit_reader_init:
82 * @reader: a #GstBitReader instance
83 * @data: (in) (array length=size): data from which the bit reader should read
84 * @size: Size of @data in bytes
86 * Initializes a #GstBitReader instance to read from @data. This function
87 * can be called on already initialized instances.
92 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
94 g_return_if_fail (reader != NULL);
98 reader->byte = reader->bit = 0;
102 * gst_bit_reader_set_pos:
103 * @reader: a #GstBitReader instance
104 * @pos: The new position in bits
106 * Sets the new position of a #GstBitReader instance to @pos in bits.
108 * Returns: %TRUE if the position could be set successfully, %FALSE
114 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
116 g_return_val_if_fail (reader != NULL, FALSE);
118 if (pos > reader->size * 8)
121 reader->byte = pos / 8;
122 reader->bit = pos % 8;
128 * gst_bit_reader_get_pos:
129 * @reader: a #GstBitReader instance
131 * Returns the current position of a #GstBitReader instance in bits.
133 * Returns: The current position of @reader in bits.
138 gst_bit_reader_get_pos (const GstBitReader * reader)
140 return _gst_bit_reader_get_pos_inline (reader);
144 * gst_bit_reader_get_remaining:
145 * @reader: a #GstBitReader instance
147 * Returns the remaining number of bits of a #GstBitReader instance.
149 * Returns: The remaining number of bits of @reader instance.
154 gst_bit_reader_get_remaining (const GstBitReader * reader)
156 return _gst_bit_reader_get_remaining_inline (reader);
160 * gst_bit_reader_get_size:
161 * @reader: a #GstBitReader instance
163 * Returns the total number of bits of a #GstBitReader instance.
165 * Returns: The total number of bits of @reader instance.
170 gst_bit_reader_get_size (const GstBitReader * reader)
172 return _gst_bit_reader_get_size_inline (reader);
176 * gst_bit_reader_skip:
177 * @reader: a #GstBitReader instance
178 * @nbits: the number of bits to skip
180 * Skips @nbits bits of the #GstBitReader instance.
182 * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
187 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
189 return _gst_bit_reader_skip_inline (reader, nbits);
193 * gst_bit_reader_skip_to_byte:
194 * @reader: a #GstBitReader instance
196 * Skips until the next byte.
198 * Returns: %TRUE if successful, %FALSE otherwise.
203 gst_bit_reader_skip_to_byte (GstBitReader * reader)
205 return _gst_bit_reader_skip_to_byte_inline (reader);
209 * gst_bit_reader_get_bits_uint8:
210 * @reader: a #GstBitReader instance
211 * @val: Pointer to a #guint8 to store the result
212 * @nbits: number of bits to read
214 * Read @nbits bits into @val and update the current position.
216 * Returns: %TRUE if successful, %FALSE otherwise.
222 * gst_bit_reader_get_bits_uint16:
223 * @reader: a #GstBitReader instance
224 * @val: Pointer to a #guint16 to store the result
225 * @nbits: number of bits to read
227 * Read @nbits bits into @val and update the current position.
229 * Returns: %TRUE if successful, %FALSE otherwise.
235 * gst_bit_reader_get_bits_uint32:
236 * @reader: a #GstBitReader instance
237 * @val: Pointer to a #guint32 to store the result
238 * @nbits: number of bits to read
240 * Read @nbits bits into @val and update the current position.
242 * Returns: %TRUE if successful, %FALSE otherwise.
248 * gst_bit_reader_get_bits_uint64:
249 * @reader: a #GstBitReader instance
250 * @val: Pointer to a #guint64 to store the result
251 * @nbits: number of bits to read
253 * Read @nbits bits into @val and update the current position.
255 * Returns: %TRUE if successful, %FALSE otherwise.
261 * gst_bit_reader_peek_bits_uint8:
262 * @reader: a #GstBitReader instance
263 * @val: Pointer to a #guint8 to store the result
264 * @nbits: number of bits to read
266 * Read @nbits bits into @val but keep the current position.
268 * Returns: %TRUE if successful, %FALSE otherwise.
274 * gst_bit_reader_peek_bits_uint16:
275 * @reader: a #GstBitReader instance
276 * @val: Pointer to a #guint16 to store the result
277 * @nbits: number of bits to read
279 * Read @nbits bits into @val but keep the current position.
281 * Returns: %TRUE if successful, %FALSE otherwise.
287 * gst_bit_reader_peek_bits_uint32:
288 * @reader: a #GstBitReader instance
289 * @val: Pointer to a #guint32 to store the result
290 * @nbits: number of bits to read
292 * Read @nbits bits into @val but keep the current position.
294 * Returns: %TRUE if successful, %FALSE otherwise.
300 * gst_bit_reader_peek_bits_uint64:
301 * @reader: a #GstBitReader instance
302 * @val: Pointer to a #guint64 to store the result
303 * @nbits: number of bits to read
305 * Read @nbits bits into @val but keep the current position.
307 * Returns: %TRUE if successful, %FALSE otherwise.
312 #define GST_BIT_READER_READ_BITS(bits) \
314 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
316 return _gst_bit_reader_peek_bits_uint##bits##_inline (reader, val, nbits); \
320 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
322 return _gst_bit_reader_get_bits_uint##bits##_inline (reader, val, nbits); \
325 GST_BIT_READER_READ_BITS (8);
326 GST_BIT_READER_READ_BITS (16);
327 GST_BIT_READER_READ_BITS (32);
328 GST_BIT_READER_READ_BITS (64);