gi: Skip allocator of non-boxed structure
[platform/upstream/gstreamer.git] / libs / gst / base / gstbitreader.c
1 /* GStreamer
2  *
3  * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #define GST_BIT_READER_DISABLE_INLINES
26 #include "gstbitreader.h"
27
28 #include <string.h>
29
30 /**
31  * SECTION:gstbitreader
32  * @short_description: Reads any number of bits from a memory buffer
33  *
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.
37  */
38
39 /**
40  * gst_bit_reader_new: (skip)
41  * @data: (array length=size): Data from which the #GstBitReader
42  *   should read
43  * @size: Size of @data in bytes
44  *
45  * Create a new #GstBitReader instance, which will read from @data.
46  *
47  * Free-function: gst_bit_reader_free
48  *
49  * Returns: (transfer full): a new #GstBitReader instance
50  */
51 GstBitReader *
52 gst_bit_reader_new (const guint8 * data, guint size)
53 {
54   GstBitReader *ret = g_slice_new0 (GstBitReader);
55
56   ret->data = data;
57   ret->size = size;
58
59   return ret;
60 }
61
62 /**
63  * gst_bit_reader_free:
64  * @reader: (in) (transfer full): a #GstBitReader instance
65  *
66  * Frees a #GstBitReader instance, which was previously allocated by
67  * gst_bit_reader_new().
68  */
69 void
70 gst_bit_reader_free (GstBitReader * reader)
71 {
72   g_return_if_fail (reader != NULL);
73
74   g_slice_free (GstBitReader, reader);
75 }
76
77 /**
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
82  *
83  * Initializes a #GstBitReader instance to read from @data. This function
84  * can be called on already initialized instances.
85  */
86 void
87 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
88 {
89   g_return_if_fail (reader != NULL);
90
91   reader->data = data;
92   reader->size = size;
93   reader->byte = reader->bit = 0;
94 }
95
96 /**
97  * gst_bit_reader_set_pos:
98  * @reader: a #GstBitReader instance
99  * @pos: The new position in bits
100  *
101  * Sets the new position of a #GstBitReader instance to @pos in bits.
102  *
103  * Returns: %TRUE if the position could be set successfully, %FALSE
104  * otherwise.
105  */
106 gboolean
107 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
108 {
109   g_return_val_if_fail (reader != NULL, FALSE);
110
111   if (pos > reader->size * 8)
112     return FALSE;
113
114   reader->byte = pos / 8;
115   reader->bit = pos % 8;
116
117   return TRUE;
118 }
119
120 /**
121  * gst_bit_reader_get_pos:
122  * @reader: a #GstBitReader instance
123  *
124  * Returns the current position of a #GstBitReader instance in bits.
125  *
126  * Returns: The current position of @reader in bits.
127  */
128 guint
129 gst_bit_reader_get_pos (const GstBitReader * reader)
130 {
131   return _gst_bit_reader_get_pos_inline (reader);
132 }
133
134 /**
135  * gst_bit_reader_get_remaining:
136  * @reader: a #GstBitReader instance
137  *
138  * Returns the remaining number of bits of a #GstBitReader instance.
139  *
140  * Returns: The remaining number of bits of @reader instance.
141  */
142 guint
143 gst_bit_reader_get_remaining (const GstBitReader * reader)
144 {
145   return _gst_bit_reader_get_remaining_inline (reader);
146 }
147
148 /**
149  * gst_bit_reader_get_size:
150  * @reader: a #GstBitReader instance
151  *
152  * Returns the total number of bits of a #GstBitReader instance.
153  *
154  * Returns: The total number of bits of @reader instance.
155  */
156 guint
157 gst_bit_reader_get_size (const GstBitReader * reader)
158 {
159   return _gst_bit_reader_get_size_inline (reader);
160 }
161
162 /**
163  * gst_bit_reader_skip:
164  * @reader: a #GstBitReader instance
165  * @nbits: the number of bits to skip
166  *
167  * Skips @nbits bits of the #GstBitReader instance.
168  *
169  * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
170  */
171 gboolean
172 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
173 {
174   return _gst_bit_reader_skip_inline (reader, nbits);
175 }
176
177 /**
178  * gst_bit_reader_skip_to_byte:
179  * @reader: a #GstBitReader instance
180  *
181  * Skips until the next byte.
182  *
183  * Returns: %TRUE if successful, %FALSE otherwise.
184  */
185 gboolean
186 gst_bit_reader_skip_to_byte (GstBitReader * reader)
187 {
188   return _gst_bit_reader_skip_to_byte_inline (reader);
189 }
190
191 /**
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
196  *
197  * Read @nbits bits into @val and update the current position.
198  *
199  * Returns: %TRUE if successful, %FALSE otherwise.
200  */
201
202 /**
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
207  *
208  * Read @nbits bits into @val and update the current position.
209  *
210  * Returns: %TRUE if successful, %FALSE otherwise.
211  */
212
213 /**
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
218  *
219  * Read @nbits bits into @val and update the current position.
220  *
221  * Returns: %TRUE if successful, %FALSE otherwise.
222  */
223
224 /**
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
229  *
230  * Read @nbits bits into @val and update the current position.
231  *
232  * Returns: %TRUE if successful, %FALSE otherwise.
233  */
234
235 /**
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
240  *
241  * Read @nbits bits into @val but keep the current position.
242  *
243  * Returns: %TRUE if successful, %FALSE otherwise.
244  */
245
246 /**
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
251  *
252  * Read @nbits bits into @val but keep the current position.
253  *
254  * Returns: %TRUE if successful, %FALSE otherwise.
255  */
256
257 /**
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
262  *
263  * Read @nbits bits into @val but keep the current position.
264  *
265  * Returns: %TRUE if successful, %FALSE otherwise.
266  */
267
268 /**
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
273  *
274  * Read @nbits bits into @val but keep the current position.
275  *
276  * Returns: %TRUE if successful, %FALSE otherwise.
277  */
278
279 #define GST_BIT_READER_READ_BITS(bits) \
280 gboolean \
281 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
282 { \
283   return _gst_bit_reader_peek_bits_uint##bits##_inline (reader, val, nbits); \
284 } \
285 \
286 gboolean \
287 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
288 { \
289   return _gst_bit_reader_get_bits_uint##bits##_inline (reader, val, nbits); \
290 }
291
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);