Merge branch 'master' into 0.11
[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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, 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:
41  * @data: Data from which the #GstBitReader should read
42  * @size: Size of @data in bytes
43  *
44  * Create a new #GstBitReader instance, which will read from @data.
45  *
46  * Free-function: gst_bit_reader_free
47  *
48  * Returns: (transfer full): a new #GstBitReader instance
49  *
50  * Since: 0.10.22
51  */
52 GstBitReader *
53 gst_bit_reader_new (const guint8 * data, guint size)
54 {
55   GstBitReader *ret = g_slice_new0 (GstBitReader);
56
57   ret->data = data;
58   ret->size = size;
59
60   return ret;
61 }
62
63 /**
64  * gst_bit_reader_free:
65  * @reader: (in) (transfer full): a #GstBitReader instance
66  *
67  * Frees a #GstBitReader instance, which was previously allocated by
68  * gst_bit_reader_new().
69  * 
70  * Since: 0.10.22
71  */
72 void
73 gst_bit_reader_free (GstBitReader * reader)
74 {
75   g_return_if_fail (reader != NULL);
76
77   g_slice_free (GstBitReader, reader);
78 }
79
80 /**
81  * gst_bit_reader_init:
82  * @reader: a #GstBitReader instance
83  * @data: (in) (array length=size): data from which the bit reader should read
84  * @size: Size of @data in bytes
85  *
86  * Initializes a #GstBitReader instance to read from @data. This function
87  * can be called on already initialized instances.
88  * 
89  * Since: 0.10.22
90  */
91 void
92 gst_bit_reader_init (GstBitReader * reader, const guint8 * data, guint size)
93 {
94   g_return_if_fail (reader != NULL);
95
96   reader->data = data;
97   reader->size = size;
98   reader->byte = reader->bit = 0;
99 }
100
101 /**
102  * gst_bit_reader_set_pos:
103  * @reader: a #GstBitReader instance
104  * @pos: The new position in bits
105  *
106  * Sets the new position of a #GstBitReader instance to @pos in bits.
107  *
108  * Returns: %TRUE if the position could be set successfully, %FALSE
109  * otherwise.
110  * 
111  * Since: 0.10.22
112  */
113 gboolean
114 gst_bit_reader_set_pos (GstBitReader * reader, guint pos)
115 {
116   g_return_val_if_fail (reader != NULL, FALSE);
117
118   if (pos > reader->size * 8)
119     return FALSE;
120
121   reader->byte = pos / 8;
122   reader->bit = pos % 8;
123
124   return TRUE;
125 }
126
127 /**
128  * gst_bit_reader_get_pos:
129  * @reader: a #GstBitReader instance
130  *
131  * Returns the current position of a #GstBitReader instance in bits.
132  *
133  * Returns: The current position of @reader in bits.
134  * 
135  * Since: 0.10.22
136  */
137 guint
138 gst_bit_reader_get_pos (const GstBitReader * reader)
139 {
140   return _gst_bit_reader_get_pos_inline (reader);
141 }
142
143 /**
144  * gst_bit_reader_get_remaining:
145  * @reader: a #GstBitReader instance
146  *
147  * Returns the remaining number of bits of a #GstBitReader instance.
148  *
149  * Returns: The remaining number of bits of @reader instance.
150  * 
151  * Since: 0.10.22
152  */
153 guint
154 gst_bit_reader_get_remaining (const GstBitReader * reader)
155 {
156   return _gst_bit_reader_get_remaining_inline (reader);
157 }
158
159 /**
160  * gst_bit_reader_get_size:
161  * @reader: a #GstBitReader instance
162  *
163  * Returns the total number of bits of a #GstBitReader instance.
164  *
165  * Returns: The total number of bits of @reader instance.
166  * 
167  * Since: 0.10.26
168  */
169 guint
170 gst_bit_reader_get_size (const GstBitReader * reader)
171 {
172   return _gst_bit_reader_get_size_inline (reader);
173 }
174
175 /**
176  * gst_bit_reader_skip:
177  * @reader: a #GstBitReader instance
178  * @nbits: the number of bits to skip
179  *
180  * Skips @nbits bits of the #GstBitReader instance.
181  *
182  * Returns: %TRUE if @nbits bits could be skipped, %FALSE otherwise.
183  * 
184  * Since: 0.10.22
185  */
186 gboolean
187 gst_bit_reader_skip (GstBitReader * reader, guint nbits)
188 {
189   return _gst_bit_reader_skip_inline (reader, nbits);
190 }
191
192 /**
193  * gst_bit_reader_skip_to_byte:
194  * @reader: a #GstBitReader instance
195  *
196  * Skips until the next byte.
197  *
198  * Returns: %TRUE if successful, %FALSE otherwise.
199  * 
200  * Since: 0.10.22
201  */
202 gboolean
203 gst_bit_reader_skip_to_byte (GstBitReader * reader)
204 {
205   return _gst_bit_reader_skip_to_byte_inline (reader);
206 }
207
208 /**
209  * gst_bit_reader_get_bits_uint8:
210  * @reader: a #GstBitReader instance
211  * @val: (out): Pointer to a #guint8 to store the result
212  * @nbits: number of bits to read
213  *
214  * Read @nbits bits into @val and update the current position.
215  *
216  * Returns: %TRUE if successful, %FALSE otherwise.
217  * 
218  * Since: 0.10.22
219  */
220
221 /**
222  * gst_bit_reader_get_bits_uint16:
223  * @reader: a #GstBitReader instance
224  * @val: (out): Pointer to a #guint16 to store the result
225  * @nbits: number of bits to read
226  *
227  * Read @nbits bits into @val and update the current position.
228  *
229  * Returns: %TRUE if successful, %FALSE otherwise.
230  * 
231  * Since: 0.10.22
232  */
233
234 /**
235  * gst_bit_reader_get_bits_uint32:
236  * @reader: a #GstBitReader instance
237  * @val: (out): Pointer to a #guint32 to store the result
238  * @nbits: number of bits to read
239  *
240  * Read @nbits bits into @val and update the current position.
241  *
242  * Returns: %TRUE if successful, %FALSE otherwise.
243  * 
244  * Since: 0.10.22
245  */
246
247 /**
248  * gst_bit_reader_get_bits_uint64:
249  * @reader: a #GstBitReader instance
250  * @val: (out): Pointer to a #guint64 to store the result
251  * @nbits: number of bits to read
252  *
253  * Read @nbits bits into @val and update the current position.
254  *
255  * Returns: %TRUE if successful, %FALSE otherwise.
256  * 
257  * Since: 0.10.22
258  */
259
260 /**
261  * gst_bit_reader_peek_bits_uint8:
262  * @reader: a #GstBitReader instance
263  * @val: (out): Pointer to a #guint8 to store the result
264  * @nbits: number of bits to read
265  *
266  * Read @nbits bits into @val but keep the current position.
267  *
268  * Returns: %TRUE if successful, %FALSE otherwise.
269  * 
270  * Since: 0.10.22
271  */
272
273 /**
274  * gst_bit_reader_peek_bits_uint16:
275  * @reader: a #GstBitReader instance
276  * @val: (out): Pointer to a #guint16 to store the result
277  * @nbits: number of bits to read
278  *
279  * Read @nbits bits into @val but keep the current position.
280  *
281  * Returns: %TRUE if successful, %FALSE otherwise.
282  * 
283  * Since: 0.10.22
284  */
285
286 /**
287  * gst_bit_reader_peek_bits_uint32:
288  * @reader: a #GstBitReader instance
289  * @val: (out): Pointer to a #guint32 to store the result
290  * @nbits: number of bits to read
291  *
292  * Read @nbits bits into @val but keep the current position.
293  *
294  * Returns: %TRUE if successful, %FALSE otherwise.
295  * 
296  * Since: 0.10.22
297  */
298
299 /**
300  * gst_bit_reader_peek_bits_uint64:
301  * @reader: a #GstBitReader instance
302  * @val: (out): Pointer to a #guint64 to store the result
303  * @nbits: number of bits to read
304  *
305  * Read @nbits bits into @val but keep the current position.
306  *
307  * Returns: %TRUE if successful, %FALSE otherwise.
308  * 
309  * Since: 0.10.22
310  */
311
312 #define GST_BIT_READER_READ_BITS(bits) \
313 gboolean \
314 gst_bit_reader_peek_bits_uint##bits (const GstBitReader *reader, guint##bits *val, guint nbits) \
315 { \
316   return _gst_bit_reader_peek_bits_uint##bits##_inline (reader, val, nbits); \
317 } \
318 \
319 gboolean \
320 gst_bit_reader_get_bits_uint##bits (GstBitReader *reader, guint##bits *val, guint nbits) \
321 { \
322   return _gst_bit_reader_get_bits_uint##bits##_inline (reader, val, nbits); \
323 }
324
325 GST_BIT_READER_READ_BITS (8);
326 GST_BIT_READER_READ_BITS (16);
327 GST_BIT_READER_READ_BITS (32);
328 GST_BIT_READER_READ_BITS (64);