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.
21 #ifndef __GST_BIT_READER_H__
22 #define __GST_BIT_READER_H__
25 #include <gst/base/base-prelude.h>
27 /* FIXME: inline functions */
31 #define GST_BIT_READER(reader) ((GstBitReader *) (reader))
35 * @data: (array length=size): Data from which the bit reader will
37 * @size: Size of @data in bytes
38 * @byte: Current byte position
39 * @bit: Bit position in the current byte
41 * A bit reader instance.
47 guint byte; /* Byte position */
48 guint bit; /* Bit position in the current byte */
51 gpointer _gst_reserved[GST_PADDING];
55 GstBitReader * gst_bit_reader_new (const guint8 *data, guint size) G_GNUC_MALLOC;
58 void gst_bit_reader_free (GstBitReader *reader);
61 void gst_bit_reader_init (GstBitReader *reader, const guint8 *data, guint size);
64 gboolean gst_bit_reader_set_pos (GstBitReader *reader, guint pos);
67 guint gst_bit_reader_get_pos (const GstBitReader *reader);
70 guint gst_bit_reader_get_remaining (const GstBitReader *reader);
73 guint gst_bit_reader_get_size (const GstBitReader *reader);
76 gboolean gst_bit_reader_skip (GstBitReader *reader, guint nbits);
79 gboolean gst_bit_reader_skip_to_byte (GstBitReader *reader);
82 gboolean gst_bit_reader_get_bits_uint8 (GstBitReader *reader, guint8 *val, guint nbits);
85 gboolean gst_bit_reader_get_bits_uint16 (GstBitReader *reader, guint16 *val, guint nbits);
88 gboolean gst_bit_reader_get_bits_uint32 (GstBitReader *reader, guint32 *val, guint nbits);
91 gboolean gst_bit_reader_get_bits_uint64 (GstBitReader *reader, guint64 *val, guint nbits);
94 gboolean gst_bit_reader_peek_bits_uint8 (const GstBitReader *reader, guint8 *val, guint nbits);
97 gboolean gst_bit_reader_peek_bits_uint16 (const GstBitReader *reader, guint16 *val, guint nbits);
100 gboolean gst_bit_reader_peek_bits_uint32 (const GstBitReader *reader, guint32 *val, guint nbits);
103 gboolean gst_bit_reader_peek_bits_uint64 (const GstBitReader *reader, guint64 *val, guint nbits);
106 * GST_BIT_READER_INIT:
107 * @data: Data from which the #GstBitReader should read
108 * @size: Size of @data in bytes
110 * A #GstBitReader must be initialized with this macro, before it can be
111 * used. This macro can used be to initialize a variable, but it cannot
112 * be assigned to a variable. In that case you have to use
113 * gst_bit_reader_init().
115 #define GST_BIT_READER_INIT(data, size) {data, size, 0, 0}
117 /* Unchecked variants */
120 gst_bit_reader_skip_unchecked (GstBitReader * reader, guint nbits)
122 reader->bit += nbits;
123 reader->byte += reader->bit / 8;
124 reader->bit = reader->bit % 8;
128 gst_bit_reader_skip_to_byte_unchecked (GstBitReader * reader)
136 #define __GST_BIT_READER_READ_BITS_UNCHECKED(bits) \
137 static inline guint##bits \
138 gst_bit_reader_peek_bits_uint##bits##_unchecked (const GstBitReader *reader, guint nbits) \
140 guint##bits ret = 0; \
141 const guint8 *data; \
144 data = reader->data; \
145 byte = reader->byte; \
148 while (nbits > 0) { \
149 guint toread = MIN (nbits, 8 - bit); \
152 ret |= (data[byte] & (0xff >> bit)) >> (8 - toread - bit); \
165 static inline guint##bits \
166 gst_bit_reader_get_bits_uint##bits##_unchecked (GstBitReader *reader, guint nbits) \
170 ret = gst_bit_reader_peek_bits_uint##bits##_unchecked (reader, nbits); \
172 gst_bit_reader_skip_unchecked (reader, nbits); \
177 __GST_BIT_READER_READ_BITS_UNCHECKED (8)
178 __GST_BIT_READER_READ_BITS_UNCHECKED (16)
179 __GST_BIT_READER_READ_BITS_UNCHECKED (32)
180 __GST_BIT_READER_READ_BITS_UNCHECKED (64)
182 #undef __GST_BIT_READER_READ_BITS_UNCHECKED
184 /* unchecked variants -- do not use */
187 _gst_bit_reader_get_size_unchecked (const GstBitReader * reader)
189 return reader->size * 8;
193 _gst_bit_reader_get_pos_unchecked (const GstBitReader * reader)
195 return reader->byte * 8 + reader->bit;
199 _gst_bit_reader_get_remaining_unchecked (const GstBitReader * reader)
201 return reader->size * 8 - (reader->byte * 8 + reader->bit);
204 /* inlined variants -- do not use directly */
206 _gst_bit_reader_get_size_inline (const GstBitReader * reader)
208 g_return_val_if_fail (reader != NULL, 0);
210 return _gst_bit_reader_get_size_unchecked (reader);
214 _gst_bit_reader_get_pos_inline (const GstBitReader * reader)
216 g_return_val_if_fail (reader != NULL, 0);
218 return _gst_bit_reader_get_pos_unchecked (reader);
222 _gst_bit_reader_get_remaining_inline (const GstBitReader * reader)
224 g_return_val_if_fail (reader != NULL, 0);
226 return _gst_bit_reader_get_remaining_unchecked (reader);
229 static inline gboolean
230 _gst_bit_reader_skip_inline (GstBitReader * reader, guint nbits)
232 g_return_val_if_fail (reader != NULL, FALSE);
234 if (_gst_bit_reader_get_remaining_unchecked (reader) < nbits)
237 gst_bit_reader_skip_unchecked (reader, nbits);
242 static inline gboolean
243 _gst_bit_reader_skip_to_byte_inline (GstBitReader * reader)
245 g_return_val_if_fail (reader != NULL, FALSE);
247 if (reader->byte > reader->size)
250 gst_bit_reader_skip_to_byte_unchecked (reader);
255 #define __GST_BIT_READER_READ_BITS_INLINE(bits) \
256 static inline gboolean \
257 _gst_bit_reader_get_bits_uint##bits##_inline (GstBitReader *reader, guint##bits *val, guint nbits) \
259 g_return_val_if_fail (reader != NULL, FALSE); \
260 g_return_val_if_fail (val != NULL, FALSE); \
261 g_return_val_if_fail (nbits <= bits, FALSE); \
263 if (_gst_bit_reader_get_remaining_unchecked (reader) < nbits) \
266 *val = gst_bit_reader_get_bits_uint##bits##_unchecked (reader, nbits); \
270 static inline gboolean \
271 _gst_bit_reader_peek_bits_uint##bits##_inline (const GstBitReader *reader, guint##bits *val, guint nbits) \
273 g_return_val_if_fail (reader != NULL, FALSE); \
274 g_return_val_if_fail (val != NULL, FALSE); \
275 g_return_val_if_fail (nbits <= bits, FALSE); \
277 if (_gst_bit_reader_get_remaining_unchecked (reader) < nbits) \
280 *val = gst_bit_reader_peek_bits_uint##bits##_unchecked (reader, nbits); \
284 __GST_BIT_READER_READ_BITS_INLINE (8)
285 __GST_BIT_READER_READ_BITS_INLINE (16)
286 __GST_BIT_READER_READ_BITS_INLINE (32)
287 __GST_BIT_READER_READ_BITS_INLINE (64)
289 #undef __GST_BIT_READER_READ_BITS_INLINE
291 #ifndef GST_BIT_READER_DISABLE_INLINES
293 #define gst_bit_reader_get_size(reader) \
294 _gst_bit_reader_get_size_inline (reader)
295 #define gst_bit_reader_get_pos(reader) \
296 _gst_bit_reader_get_pos_inline (reader)
297 #define gst_bit_reader_get_remaining(reader) \
298 _gst_bit_reader_get_remaining_inline (reader)
300 /* we use defines here so we can add the G_LIKELY() */
302 #define gst_bit_reader_skip(reader, nbits)\
303 G_LIKELY (_gst_bit_reader_skip_inline(reader, nbits))
304 #define gst_bit_reader_skip_to_byte(reader)\
305 G_LIKELY (_gst_bit_reader_skip_to_byte_inline(reader))
307 #define gst_bit_reader_get_bits_uint8(reader, val, nbits) \
308 G_LIKELY (_gst_bit_reader_get_bits_uint8_inline (reader, val, nbits))
309 #define gst_bit_reader_get_bits_uint16(reader, val, nbits) \
310 G_LIKELY (_gst_bit_reader_get_bits_uint16_inline (reader, val, nbits))
311 #define gst_bit_reader_get_bits_uint32(reader, val, nbits) \
312 G_LIKELY (_gst_bit_reader_get_bits_uint32_inline (reader, val, nbits))
313 #define gst_bit_reader_get_bits_uint64(reader, val, nbits) \
314 G_LIKELY (_gst_bit_reader_get_bits_uint64_inline (reader, val, nbits))
316 #define gst_bit_reader_peek_bits_uint8(reader, val, nbits) \
317 G_LIKELY (_gst_bit_reader_peek_bits_uint8_inline (reader, val, nbits))
318 #define gst_bit_reader_peek_bits_uint16(reader, val, nbits) \
319 G_LIKELY (_gst_bit_reader_peek_bits_uint16_inline (reader, val, nbits))
320 #define gst_bit_reader_peek_bits_uint32(reader, val, nbits) \
321 G_LIKELY (_gst_bit_reader_peek_bits_uint32_inline (reader, val, nbits))
322 #define gst_bit_reader_peek_bits_uint64(reader, val, nbits) \
323 G_LIKELY (_gst_bit_reader_peek_bits_uint64_inline (reader, val, nbits))
328 #endif /* __GST_BIT_READER_H__ */