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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #define GST_BIT_READER_DISABLE_INLINES
26 #include "gstbitreader.h"
31 * SECTION:gstbitreader
32 * @title: 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.
41 * gst_bit_reader_new: (skip)
42 * @data: (array length=size): Data from which the #GstBitReader
44 * @size: Size of @data in bytes
46 * Create a new #GstBitReader instance, which will read from @data.
48 * Free-function: gst_bit_reader_free
50 * 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().
71 gst_bit_reader_free (GstBitReader * reader)
73 g_return_if_fail (reader != NULL);
75 g_slice_free (GstBitReader, reader);
79 * gst_bit_reader_init:
80 * @reader: a #GstBitReader instance
81 * @data: (in) (array length=size): data from which the bit reader should read
82 * @size: Size of @data in bytes
84 * Initializes a #GstBitReader instance to read from @data. This function
85 * can be called on already initialized instances.
88 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
90 g_return_if_fail (reader != NULL);
94 reader->byte = reader->bit = 0;
98 * gst_bit_reader_set_pos:
99 * @reader: a #GstBitReader instance
100 * @pos: The new position in bits
102 * Sets the new position of a #GstBitReader instance to @pos in bits.
104 * Returns: %TRUE if the position could be set successfully, %FALSE
108 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
110 g_return_val_if_fail (reader != NULL, FALSE);
112 if (pos > reader->size * 8)
115 reader->byte = pos / 8;
116 reader->bit = pos % 8;
122 * gst_bit_reader_get_pos:
123 * @reader: a #GstBitReader instance
125 * Returns the current position of a #GstBitReader instance in bits.
127 * Returns: The current position of @reader in bits.
130 gst_bit_reader_get_pos (const GstBitReader * reader)
132 return _gst_bit_reader_get_pos_inline (reader);
136 * gst_bit_reader_get_remaining:
137 * @reader: a #GstBitReader instance
139 * Returns the remaining number of bits of a #GstBitReader instance.
141 * Returns: The remaining number of bits of @reader instance.
144 gst_bit_reader_get_remaining (const GstBitReader * reader)
146 return _gst_bit_reader_get_remaining_inline (reader);
150 * gst_bit_reader_get_size:
151 * @reader: a #GstBitReader instance
153 * Returns the total number of bits of a #GstBitReader instance.
155 * Returns: The total number of bits of @reader instance.
158 gst_bit_reader_get_size (const GstBitReader * reader)
160 return _gst_bit_reader_get_size_inline (reader);
164 * gst_bit_reader_skip:
165 * @reader: a #GstBitReader instance
166 * @nbits: the number of bits to skip
168 * Skips @nbits bits of the #GstBitReader instance.
170 * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
173 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
175 return _gst_bit_reader_skip_inline (reader, nbits);
179 * gst_bit_reader_skip_to_byte:
180 * @reader: a #GstBitReader instance
182 * Skips until the next byte.
184 * Returns: %TRUE if successful, %FALSE otherwise.
187 gst_bit_reader_skip_to_byte (GstBitReader * reader)
189 return _gst_bit_reader_skip_to_byte_inline (reader);
193 * gst_bit_reader_get_bits_uint8:
194 * @reader: a #GstBitReader instance
195 * @val: (out): Pointer to a #guint8 to store the result
196 * @nbits: number of bits to read
198 * Read @nbits bits into @val and update the current position.
200 * Returns: %TRUE if successful, %FALSE otherwise.
204 * gst_bit_reader_get_bits_uint16:
205 * @reader: a #GstBitReader instance
206 * @val: (out): Pointer to a #guint16 to store the result
207 * @nbits: number of bits to read
209 * Read @nbits bits into @val and update the current position.
211 * Returns: %TRUE if successful, %FALSE otherwise.
215 * gst_bit_reader_get_bits_uint32:
216 * @reader: a #GstBitReader instance
217 * @val: (out): Pointer to a #guint32 to store the result
218 * @nbits: number of bits to read
220 * Read @nbits bits into @val and update the current position.
222 * Returns: %TRUE if successful, %FALSE otherwise.
226 * gst_bit_reader_get_bits_uint64:
227 * @reader: a #GstBitReader instance
228 * @val: (out): Pointer to a #guint64 to store the result
229 * @nbits: number of bits to read
231 * Read @nbits bits into @val and update the current position.
233 * Returns: %TRUE if successful, %FALSE otherwise.
237 * gst_bit_reader_peek_bits_uint8:
238 * @reader: a #GstBitReader instance
239 * @val: (out): Pointer to a #guint8 to store the result
240 * @nbits: number of bits to read
242 * Read @nbits bits into @val but keep the current position.
244 * Returns: %TRUE if successful, %FALSE otherwise.
248 * gst_bit_reader_peek_bits_uint16:
249 * @reader: a #GstBitReader instance
250 * @val: (out): Pointer to a #guint16 to store the result
251 * @nbits: number of bits to read
253 * Read @nbits bits into @val but keep the current position.
255 * Returns: %TRUE if successful, %FALSE otherwise.
259 * gst_bit_reader_peek_bits_uint32:
260 * @reader: a #GstBitReader instance
261 * @val: (out): Pointer to a #guint32 to store the result
262 * @nbits: number of bits to read
264 * Read @nbits bits into @val but keep the current position.
266 * Returns: %TRUE if successful, %FALSE otherwise.
270 * gst_bit_reader_peek_bits_uint64:
271 * @reader: a #GstBitReader instance
272 * @val: (out): Pointer to a #guint64 to store the result
273 * @nbits: number of bits to read
275 * Read @nbits bits into @val but keep the current position.
277 * Returns: %TRUE if successful, %FALSE otherwise.
280 #define GST_BIT_READER_READ_BITS(bits) \
282 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
284 return _gst_bit_reader_peek_bits_uint##bits##_inline (reader, val, nbits); \
288 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
290 return _gst_bit_reader_get_bits_uint##bits##_inline (reader, val, nbits); \
293 GST_BIT_READER_READ_BITS (8);
294 GST_BIT_READER_READ_BITS (16);
295 GST_BIT_READER_READ_BITS (32);
296 GST_BIT_READER_READ_BITS (64);