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 * @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.
40 * gst_bit_reader_new: (skip)
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
52 gst_bit_reader_new (const guint8 * data, guint size)
54 GstBitReader *ret = g_slice_new0 (GstBitReader);
63 * gst_bit_reader_free:
64 * @reader: (in) (transfer full): a #GstBitReader instance
66 * Frees a #GstBitReader instance, which was previously allocated by
67 * gst_bit_reader_new().
70 gst_bit_reader_free (GstBitReader * reader)
72 g_return_if_fail (reader != NULL);
74 g_slice_free (GstBitReader, reader);
78 * gst_bit_reader_init:
79 * @reader: a #GstBitReader instance
80 * @data: (in) (array length=size): data from which the bit reader should read
81 * @size: Size of @data in bytes
83 * Initializes a #GstBitReader instance to read from @data. This function
84 * can be called on already initialized instances.
87 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
89 g_return_if_fail (reader != NULL);
93 reader->byte = reader->bit = 0;
97 * gst_bit_reader_set_pos:
98 * @reader: a #GstBitReader instance
99 * @pos: The new position in bits
101 * Sets the new position of a #GstBitReader instance to @pos in bits.
103 * Returns: %TRUE if the position could be set successfully, %FALSE
107 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
109 g_return_val_if_fail (reader != NULL, FALSE);
111 if (pos > reader->size * 8)
114 reader->byte = pos / 8;
115 reader->bit = pos % 8;
121 * gst_bit_reader_get_pos:
122 * @reader: a #GstBitReader instance
124 * Returns the current position of a #GstBitReader instance in bits.
126 * Returns: The current position of @reader in bits.
129 gst_bit_reader_get_pos (const GstBitReader * reader)
131 return _gst_bit_reader_get_pos_inline (reader);
135 * gst_bit_reader_get_remaining:
136 * @reader: a #GstBitReader instance
138 * Returns the remaining number of bits of a #GstBitReader instance.
140 * Returns: The remaining number of bits of @reader instance.
143 gst_bit_reader_get_remaining (const GstBitReader * reader)
145 return _gst_bit_reader_get_remaining_inline (reader);
149 * gst_bit_reader_get_size:
150 * @reader: a #GstBitReader instance
152 * Returns the total number of bits of a #GstBitReader instance.
154 * Returns: The total number of bits of @reader instance.
157 gst_bit_reader_get_size (const GstBitReader * reader)
159 return _gst_bit_reader_get_size_inline (reader);
163 * gst_bit_reader_skip:
164 * @reader: a #GstBitReader instance
165 * @nbits: the number of bits to skip
167 * Skips @nbits bits of the #GstBitReader instance.
169 * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
172 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
174 return _gst_bit_reader_skip_inline (reader, nbits);
178 * gst_bit_reader_skip_to_byte:
179 * @reader: a #GstBitReader instance
181 * Skips until the next byte.
183 * Returns: %TRUE if successful, %FALSE otherwise.
186 gst_bit_reader_skip_to_byte (GstBitReader * reader)
188 return _gst_bit_reader_skip_to_byte_inline (reader);
192 * gst_bit_reader_get_bits_uint8:
193 * @reader: a #GstBitReader instance
194 * @val: (out): Pointer to a #guint8 to store the result
195 * @nbits: number of bits to read
197 * Read @nbits bits into @val and update the current position.
199 * Returns: %TRUE if successful, %FALSE otherwise.
203 * gst_bit_reader_get_bits_uint16:
204 * @reader: a #GstBitReader instance
205 * @val: (out): Pointer to a #guint16 to store the result
206 * @nbits: number of bits to read
208 * Read @nbits bits into @val and update the current position.
210 * Returns: %TRUE if successful, %FALSE otherwise.
214 * gst_bit_reader_get_bits_uint32:
215 * @reader: a #GstBitReader instance
216 * @val: (out): Pointer to a #guint32 to store the result
217 * @nbits: number of bits to read
219 * Read @nbits bits into @val and update the current position.
221 * Returns: %TRUE if successful, %FALSE otherwise.
225 * gst_bit_reader_get_bits_uint64:
226 * @reader: a #GstBitReader instance
227 * @val: (out): Pointer to a #guint64 to store the result
228 * @nbits: number of bits to read
230 * Read @nbits bits into @val and update the current position.
232 * Returns: %TRUE if successful, %FALSE otherwise.
236 * gst_bit_reader_peek_bits_uint8:
237 * @reader: a #GstBitReader instance
238 * @val: (out): Pointer to a #guint8 to store the result
239 * @nbits: number of bits to read
241 * Read @nbits bits into @val but keep the current position.
243 * Returns: %TRUE if successful, %FALSE otherwise.
247 * gst_bit_reader_peek_bits_uint16:
248 * @reader: a #GstBitReader instance
249 * @val: (out): Pointer to a #guint16 to store the result
250 * @nbits: number of bits to read
252 * Read @nbits bits into @val but keep the current position.
254 * Returns: %TRUE if successful, %FALSE otherwise.
258 * gst_bit_reader_peek_bits_uint32:
259 * @reader: a #GstBitReader instance
260 * @val: (out): Pointer to a #guint32 to store the result
261 * @nbits: number of bits to read
263 * Read @nbits bits into @val but keep the current position.
265 * Returns: %TRUE if successful, %FALSE otherwise.
269 * gst_bit_reader_peek_bits_uint64:
270 * @reader: a #GstBitReader instance
271 * @val: (out): Pointer to a #guint64 to store the result
272 * @nbits: number of bits to read
274 * Read @nbits bits into @val but keep the current position.
276 * Returns: %TRUE if successful, %FALSE otherwise.
279 #define GST_BIT_READER_READ_BITS(bits) \
281 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
283 return _gst_bit_reader_peek_bits_uint##bits##_inline (reader, val, nbits); \
287 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
289 return _gst_bit_reader_get_bits_uint##bits##_inline (reader, val, nbits); \
292 GST_BIT_READER_READ_BITS (8);
293 GST_BIT_READER_READ_BITS (16);
294 GST_BIT_READER_READ_BITS (32);
295 GST_BIT_READER_READ_BITS (64);