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_new_from_buffer:
65 * @buffer: Buffer from which the #GstBitReader should read
67 * Create a new #GstBitReader instance, which will read from the
70 * Free-function: gst_bit_reader_free
72 * Returns: (transfer full): a new #GstBitReader instance
77 gst_bit_reader_new_from_buffer (const GstBuffer * buffer)
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));
86 * gst_bit_reader_free:
87 * @reader: (in) (transfer full): a #GstBitReader instance
89 * Frees a #GstBitReader instance, which was previously allocated by
90 * gst_bit_reader_new() or gst_bit_reader_new_from_buffer().
95 gst_bit_reader_free (GstBitReader * reader)
97 g_return_if_fail (reader != NULL);
99 g_slice_free (GstBitReader, reader);
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
108 * Initializes a #GstBitReader instance to read from @data. This function
109 * can be called on already initialized instances.
114 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
116 g_return_if_fail (reader != NULL);
120 reader->byte = reader->bit = 0;
124 * gst_bit_reader_init_from_buffer:
125 * @reader: a #GstBitReader instance
126 * @buffer: (transfer none): Buffer from which the #GstBitReader should read
128 * Initializes a #GstBitReader instance to read from @buffer. This function
129 * can be called on already initialized instances.
134 gst_bit_reader_init_from_buffer (GstBitReader * reader,
135 const GstBuffer * buffer)
137 g_return_if_fail (GST_IS_BUFFER (buffer));
139 gst_bit_reader_init (reader, GST_BUFFER_DATA (buffer),
140 GST_BUFFER_SIZE (buffer));
144 * gst_bit_reader_set_pos:
145 * @reader: a #GstBitReader instance
146 * @pos: The new position in bits
148 * Sets the new position of a #GstBitReader instance to @pos in bits.
150 * Returns: %TRUE if the position could be set successfully, %FALSE
156 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
158 g_return_val_if_fail (reader != NULL, FALSE);
160 if (pos > reader->size * 8)
163 reader->byte = pos / 8;
164 reader->bit = pos % 8;
170 * gst_bit_reader_get_pos:
171 * @reader: a #GstBitReader instance
173 * Returns the current position of a #GstBitReader instance in bits.
175 * Returns: The current position of @reader in bits.
180 gst_bit_reader_get_pos (const GstBitReader * reader)
182 return _gst_bit_reader_get_pos_inline (reader);
186 * gst_bit_reader_get_remaining:
187 * @reader: a #GstBitReader instance
189 * Returns the remaining number of bits of a #GstBitReader instance.
191 * Returns: The remaining number of bits of @reader instance.
196 gst_bit_reader_get_remaining (const GstBitReader * reader)
198 return _gst_bit_reader_get_remaining_inline (reader);
202 * gst_bit_reader_get_size:
203 * @reader: a #GstBitReader instance
205 * Returns the total number of bits of a #GstBitReader instance.
207 * Returns: The total number of bits of @reader instance.
212 gst_bit_reader_get_size (const GstBitReader * reader)
214 return _gst_bit_reader_get_size_inline (reader);
218 * gst_bit_reader_skip:
219 * @reader: a #GstBitReader instance
220 * @nbits: the number of bits to skip
222 * Skips @nbits bits of the #GstBitReader instance.
224 * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
229 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
231 return _gst_bit_reader_skip_inline (reader, nbits);
235 * gst_bit_reader_skip_to_byte:
236 * @reader: a #GstBitReader instance
238 * Skips until the next byte.
240 * Returns: %TRUE if successful, %FALSE otherwise.
245 gst_bit_reader_skip_to_byte (GstBitReader * reader)
247 return _gst_bit_reader_skip_to_byte_inline (reader);
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
256 * Read @nbits bits into @val and update the current position.
258 * Returns: %TRUE if successful, %FALSE otherwise.
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
269 * Read @nbits bits into @val and update the current position.
271 * Returns: %TRUE if successful, %FALSE otherwise.
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
282 * Read @nbits bits into @val and update the current position.
284 * Returns: %TRUE if successful, %FALSE otherwise.
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
295 * Read @nbits bits into @val and update the current position.
297 * Returns: %TRUE if successful, %FALSE otherwise.
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
308 * Read @nbits bits into @val but keep the current position.
310 * Returns: %TRUE if successful, %FALSE otherwise.
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
321 * Read @nbits bits into @val but keep the current position.
323 * Returns: %TRUE if successful, %FALSE otherwise.
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
334 * Read @nbits bits into @val but keep the current position.
336 * Returns: %TRUE if successful, %FALSE otherwise.
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
347 * Read @nbits bits into @val but keep the current position.
349 * Returns: %TRUE if successful, %FALSE otherwise.
354 #define GST_BIT_READER_READ_BITS(bits) \
356 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
358 return _gst_bit_reader_peek_bits_uint##bits##_inline (reader, val, nbits); \
362 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
364 return _gst_bit_reader_get_bits_uint##bits##_inline (reader, val, nbits); \
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);