base: add (nullable) annotations to return values
[platform/upstream/gstreamer.git] / libs / gst / base / gstadapter.c
1 /* GStreamer
2  * Copyright (C) 2004 Benjamin Otte <otte@gnome.org>
3  *               2005 Wim Taymans <wim@fluendo.com>
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 /**
22  * SECTION:gstadapter
23  * @short_description: adapts incoming data on a sink pad into chunks of N bytes
24  *
25  * This class is for elements that receive buffers in an undesired size.
26  * While for example raw video contains one image per buffer, the same is not
27  * true for a lot of other formats, especially those that come directly from
28  * a file. So if you have undefined buffer sizes and require a specific size,
29  * this object is for you.
30  *
31  * An adapter is created with gst_adapter_new(). It can be freed again with
32  * g_object_unref().
33  *
34  * The theory of operation is like this: All buffers received are put
35  * into the adapter using gst_adapter_push() and the data is then read back
36  * in chunks of the desired size using gst_adapter_map()/gst_adapter_unmap()
37  * and/or gst_adapter_copy(). After the data has been processed, it is freed
38  * using gst_adapter_unmap().
39  *
40  * Other methods such as gst_adapter_take() and gst_adapter_take_buffer()
41  * combine gst_adapter_map() and gst_adapter_unmap() in one method and are
42  * potentially more convenient for some use cases.
43  *
44  * For example, a sink pad's chain function that needs to pass data to a library
45  * in 512-byte chunks could be implemented like this:
46  * |[
47  * static GstFlowReturn
48  * sink_pad_chain (GstPad *pad, GstObject *parent, GstBuffer *buffer)
49  * {
50  *   MyElement *this;
51  *   GstAdapter *adapter;
52  *   GstFlowReturn ret = GST_FLOW_OK;
53  *
54  *   this = MY_ELEMENT (parent);
55  *
56  *   adapter = this->adapter;
57  *
58  *   // put buffer into adapter
59  *   gst_adapter_push (adapter, buffer);
60  *
61  *   // while we can read out 512 bytes, process them
62  *   while (gst_adapter_available (adapter) >= 512 && ret == GST_FLOW_OK) {
63  *     const guint8 *data = gst_adapter_map (adapter, 512);
64  *     // use flowreturn as an error value
65  *     ret = my_library_foo (data);
66  *     gst_adapter_unmap (adapter);
67  *     gst_adapter_flush (adapter, 512);
68  *   }
69  *   return ret;
70  * }
71  * ]|
72  *
73  * For another example, a simple element inside GStreamer that uses #GstAdapter
74  * is the libvisual element.
75  *
76  * An element using #GstAdapter in its sink pad chain function should ensure that
77  * when the FLUSH_STOP event is received, that any queued data is cleared using
78  * gst_adapter_clear(). Data should also be cleared or processed on EOS and
79  * when changing state from %GST_STATE_PAUSED to %GST_STATE_READY.
80  *
81  * Also check the GST_BUFFER_FLAG_DISCONT flag on the buffer. Some elements might
82  * need to clear the adapter after a discontinuity.
83  *
84  * The adapter will keep track of the timestamps of the buffers
85  * that were pushed. The last seen timestamp before the current position
86  * can be queried with gst_adapter_prev_pts(). This function can
87  * optionally return the number of bytes between the start of the buffer that
88  * carried the timestamp and the current adapter position. The distance is
89  * useful when dealing with, for example, raw audio samples because it allows
90  * you to calculate the timestamp of the current adapter position by using the
91  * last seen timestamp and the amount of bytes since.  Additionally, the
92  * gst_adapter_prev_pts_at_offset() can be used to determine the last
93  * seen timestamp at a particular offset in the adapter.
94  *
95  * A last thing to note is that while #GstAdapter is pretty optimized,
96  * merging buffers still might be an operation that requires a malloc() and
97  * memcpy() operation, and these operations are not the fastest. Because of
98  * this, some functions like gst_adapter_available_fast() are provided to help
99  * speed up such cases should you want to. To avoid repeated memory allocations,
100  * gst_adapter_copy() can be used to copy data into a (statically allocated)
101  * user provided buffer.
102  *
103  * #GstAdapter is not MT safe. All operations on an adapter must be serialized by
104  * the caller. This is not normally a problem, however, as the normal use case
105  * of #GstAdapter is inside one pad's chain function, in which case access is
106  * serialized via the pad's STREAM_LOCK.
107  *
108  * Note that gst_adapter_push() takes ownership of the buffer passed. Use
109  * gst_buffer_ref() before pushing it into the adapter if you still want to
110  * access the buffer later. The adapter will never modify the data in the
111  * buffer pushed in it.
112  */
113
114 #include <gst/gst_private.h>
115 #include "gstadapter.h"
116 #include <string.h>
117
118 /* default size for the assembled data buffer */
119 #define DEFAULT_SIZE 4096
120
121 static void gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush);
122
123 GST_DEBUG_CATEGORY_STATIC (gst_adapter_debug);
124 #define GST_CAT_DEFAULT gst_adapter_debug
125
126 struct _GstAdapter
127 {
128   GObject object;
129
130   /*< private > */
131   GSList *buflist;
132   GSList *buflist_end;
133   gsize size;
134   gsize skip;
135
136   /* we keep state of assembled pieces */
137   gpointer assembled_data;
138   gsize assembled_size;
139   gsize assembled_len;
140
141   GstClockTime pts;
142   guint64 pts_distance;
143   GstClockTime dts;
144   guint64 dts_distance;
145
146   gsize scan_offset;
147   GSList *scan_entry;
148
149   GstMapInfo info;
150 };
151
152 struct _GstAdapterClass
153 {
154   GObjectClass parent_class;
155 };
156
157 #define _do_init \
158   GST_DEBUG_CATEGORY_INIT (gst_adapter_debug, "adapter", 0, "object to splice and merge buffers to desired size")
159 #define gst_adapter_parent_class parent_class
160 G_DEFINE_TYPE_WITH_CODE (GstAdapter, gst_adapter, G_TYPE_OBJECT, _do_init);
161
162 static void gst_adapter_dispose (GObject * object);
163 static void gst_adapter_finalize (GObject * object);
164
165 static void
166 gst_adapter_class_init (GstAdapterClass * klass)
167 {
168   GObjectClass *object = G_OBJECT_CLASS (klass);
169
170   object->dispose = gst_adapter_dispose;
171   object->finalize = gst_adapter_finalize;
172 }
173
174 static void
175 gst_adapter_init (GstAdapter * adapter)
176 {
177   adapter->assembled_data = g_malloc (DEFAULT_SIZE);
178   adapter->assembled_size = DEFAULT_SIZE;
179   adapter->pts = GST_CLOCK_TIME_NONE;
180   adapter->pts_distance = 0;
181   adapter->dts = GST_CLOCK_TIME_NONE;
182   adapter->dts_distance = 0;
183 }
184
185 static void
186 gst_adapter_dispose (GObject * object)
187 {
188   GstAdapter *adapter = GST_ADAPTER (object);
189
190   gst_adapter_clear (adapter);
191
192   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
193 }
194
195 static void
196 gst_adapter_finalize (GObject * object)
197 {
198   GstAdapter *adapter = GST_ADAPTER (object);
199
200   g_free (adapter->assembled_data);
201
202   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
203 }
204
205 /**
206  * gst_adapter_new:
207  *
208  * Creates a new #GstAdapter. Free with g_object_unref().
209  *
210  * Returns: (transfer full): a new #GstAdapter
211  */
212 GstAdapter *
213 gst_adapter_new (void)
214 {
215   return g_object_newv (GST_TYPE_ADAPTER, 0, NULL);
216 }
217
218 /**
219  * gst_adapter_clear:
220  * @adapter: a #GstAdapter
221  *
222  * Removes all buffers from @adapter.
223  */
224 void
225 gst_adapter_clear (GstAdapter * adapter)
226 {
227   g_return_if_fail (GST_IS_ADAPTER (adapter));
228
229   if (adapter->info.memory)
230     gst_adapter_unmap (adapter);
231
232   g_slist_foreach (adapter->buflist, (GFunc) gst_mini_object_unref, NULL);
233   g_slist_free (adapter->buflist);
234   adapter->buflist = NULL;
235   adapter->buflist_end = NULL;
236   adapter->size = 0;
237   adapter->skip = 0;
238   adapter->assembled_len = 0;
239   adapter->pts = GST_CLOCK_TIME_NONE;
240   adapter->pts_distance = 0;
241   adapter->dts = GST_CLOCK_TIME_NONE;
242   adapter->dts_distance = 0;
243   adapter->scan_offset = 0;
244   adapter->scan_entry = NULL;
245 }
246
247 static inline void
248 update_timestamps (GstAdapter * adapter, GstBuffer * buf)
249 {
250   GstClockTime pts, dts;
251
252   pts = GST_BUFFER_PTS (buf);
253   if (GST_CLOCK_TIME_IS_VALID (pts)) {
254     GST_LOG_OBJECT (adapter, "new pts %" GST_TIME_FORMAT, GST_TIME_ARGS (pts));
255     adapter->pts = pts;
256     adapter->pts_distance = 0;
257   }
258   dts = GST_BUFFER_DTS (buf);
259   if (GST_CLOCK_TIME_IS_VALID (dts)) {
260     GST_LOG_OBJECT (adapter, "new dts %" GST_TIME_FORMAT, GST_TIME_ARGS (dts));
261     adapter->dts = dts;
262     adapter->dts_distance = 0;
263   }
264 }
265
266 /* copy data into @dest, skipping @skip bytes from the head buffers */
267 static void
268 copy_into_unchecked (GstAdapter * adapter, guint8 * dest, gsize skip,
269     gsize size)
270 {
271   GSList *g;
272   GstBuffer *buf;
273   gsize bsize, csize;
274
275   /* first step, do skipping */
276   /* we might well be copying where we were scanning */
277   if (adapter->scan_entry && (adapter->scan_offset <= skip)) {
278     g = adapter->scan_entry;
279     skip -= adapter->scan_offset;
280   } else {
281     g = adapter->buflist;
282   }
283   buf = g->data;
284   bsize = gst_buffer_get_size (buf);
285   while (G_UNLIKELY (skip >= bsize)) {
286     skip -= bsize;
287     g = g_slist_next (g);
288     buf = g->data;
289     bsize = gst_buffer_get_size (buf);
290   }
291   /* copy partial buffer */
292   csize = MIN (bsize - skip, size);
293   GST_DEBUG ("bsize %" G_GSIZE_FORMAT ", skip %" G_GSIZE_FORMAT ", csize %"
294       G_GSIZE_FORMAT, bsize, skip, csize);
295   GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter, "extract %" G_GSIZE_FORMAT
296       " bytes", csize);
297   gst_buffer_extract (buf, skip, dest, csize);
298   size -= csize;
299   dest += csize;
300
301   /* second step, copy remainder */
302   while (size > 0) {
303     g = g_slist_next (g);
304     buf = g->data;
305     bsize = gst_buffer_get_size (buf);
306     if (G_LIKELY (bsize > 0)) {
307       csize = MIN (bsize, size);
308       GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
309           "extract %" G_GSIZE_FORMAT " bytes", csize);
310       gst_buffer_extract (buf, 0, dest, csize);
311       size -= csize;
312       dest += csize;
313     }
314   }
315 }
316
317 /**
318  * gst_adapter_push:
319  * @adapter: a #GstAdapter
320  * @buf: (transfer full): a #GstBuffer to add to queue in the adapter
321  *
322  * Adds the data from @buf to the data stored inside @adapter and takes
323  * ownership of the buffer.
324  */
325 void
326 gst_adapter_push (GstAdapter * adapter, GstBuffer * buf)
327 {
328   gsize size;
329
330   g_return_if_fail (GST_IS_ADAPTER (adapter));
331   g_return_if_fail (GST_IS_BUFFER (buf));
332
333   size = gst_buffer_get_size (buf);
334   adapter->size += size;
335
336   /* Note: merging buffers at this point is premature. */
337   if (G_UNLIKELY (adapter->buflist == NULL)) {
338     GST_LOG_OBJECT (adapter, "pushing %p first %" G_GSIZE_FORMAT " bytes",
339         buf, size);
340     adapter->buflist = adapter->buflist_end = g_slist_append (NULL, buf);
341     update_timestamps (adapter, buf);
342   } else {
343     /* Otherwise append to the end, and advance our end pointer */
344     GST_LOG_OBJECT (adapter, "pushing %p %" G_GSIZE_FORMAT " bytes at end, "
345         "size now %" G_GSIZE_FORMAT, buf, size, adapter->size);
346     adapter->buflist_end = g_slist_append (adapter->buflist_end, buf);
347     adapter->buflist_end = g_slist_next (adapter->buflist_end);
348   }
349 }
350
351 #if 0
352 /* Internal method only. Tries to merge buffers at the head of the queue
353  * to form a single larger buffer of size 'size'.
354  *
355  * Returns %TRUE if it managed to merge anything.
356  */
357 static gboolean
358 gst_adapter_try_to_merge_up (GstAdapter * adapter, gsize size)
359 {
360   GstBuffer *cur, *head;
361   GSList *g;
362   gboolean ret = FALSE;
363   gsize hsize;
364
365   g = adapter->buflist;
366   if (g == NULL)
367     return FALSE;
368
369   head = g->data;
370
371   hsize = gst_buffer_get_size (head);
372
373   /* Remove skipped part from the buffer (otherwise the buffer might grow indefinitely) */
374   head = gst_buffer_make_writable (head);
375   gst_buffer_resize (head, adapter->skip, hsize - adapter->skip);
376   hsize -= adapter->skip;
377   adapter->skip = 0;
378   g->data = head;
379
380   g = g_slist_next (g);
381
382   while (g != NULL && hsize < size) {
383     cur = g->data;
384     /* Merge the head buffer and the next in line */
385     GST_LOG_OBJECT (adapter, "Merging buffers of size %" G_GSIZE_FORMAT " & %"
386         G_GSIZE_FORMAT " in search of target %" G_GSIZE_FORMAT,
387         hsize, gst_buffer_get_size (cur), size);
388
389     head = gst_buffer_append (head, cur);
390     hsize = gst_buffer_get_size (head);
391     ret = TRUE;
392
393     /* Delete the front list item, and store our new buffer in the 2nd list
394      * item */
395     adapter->buflist = g_slist_delete_link (adapter->buflist, adapter->buflist);
396     g->data = head;
397
398     /* invalidate scan position */
399     adapter->scan_offset = 0;
400     adapter->scan_entry = NULL;
401
402     g = g_slist_next (g);
403   }
404
405   return ret;
406 }
407 #endif
408
409 /**
410  * gst_adapter_map:
411  * @adapter: a #GstAdapter
412  * @size: the number of bytes to map/peek
413  *
414  * Gets the first @size bytes stored in the @adapter. The returned pointer is
415  * valid until the next function is called on the adapter.
416  *
417  * Note that setting the returned pointer as the data of a #GstBuffer is
418  * incorrect for general-purpose plugins. The reason is that if a downstream
419  * element stores the buffer so that it has access to it outside of the bounds
420  * of its chain function, the buffer will have an invalid data pointer after
421  * your element flushes the bytes. In that case you should use
422  * gst_adapter_take(), which returns a freshly-allocated buffer that you can set
423  * as #GstBuffer memory or the potentially more performant
424  * gst_adapter_take_buffer().
425  *
426  * Returns %NULL if @size bytes are not available.
427  *
428  * Returns: (transfer none) (array length=size) (element-type guint8) (nullable):
429  *     a pointer to the first @size bytes of data, or %NULL
430  */
431 gconstpointer
432 gst_adapter_map (GstAdapter * adapter, gsize size)
433 {
434   GstBuffer *cur;
435   gsize skip, csize;
436   gsize toreuse, tocopy;
437   guint8 *data;
438
439   g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
440   g_return_val_if_fail (size > 0, NULL);
441
442   if (adapter->info.memory)
443     gst_adapter_unmap (adapter);
444
445   /* we don't have enough data, return NULL. This is unlikely
446    * as one usually does an _available() first instead of peeking a
447    * random size. */
448   if (G_UNLIKELY (size > adapter->size))
449     return NULL;
450
451   /* we have enough assembled data, return it */
452   if (adapter->assembled_len >= size)
453     return adapter->assembled_data;
454
455 #if 0
456   do {
457 #endif
458     cur = adapter->buflist->data;
459     skip = adapter->skip;
460
461     csize = gst_buffer_get_size (cur);
462     if (csize >= size + skip) {
463       if (!gst_buffer_map (cur, &adapter->info, GST_MAP_READ))
464         return FALSE;
465
466       return (guint8 *) adapter->info.data + skip;
467     }
468     /* We may be able to efficiently merge buffers in our pool to
469      * gather a big enough chunk to return it from the head buffer directly */
470 #if 0
471   } while (gst_adapter_try_to_merge_up (adapter, size));
472 #endif
473
474   /* see how much data we can reuse from the assembled memory and how much
475    * we need to copy */
476   toreuse = adapter->assembled_len;
477   tocopy = size - toreuse;
478
479   /* Gonna need to copy stuff out */
480   if (G_UNLIKELY (adapter->assembled_size < size)) {
481     adapter->assembled_size = (size / DEFAULT_SIZE + 1) * DEFAULT_SIZE;
482     GST_DEBUG_OBJECT (adapter, "resizing internal buffer to %" G_GSIZE_FORMAT,
483         adapter->assembled_size);
484     if (toreuse == 0) {
485       GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "alloc new buffer");
486       /* no g_realloc to avoid a memcpy that is not desired here since we are
487        * not going to reuse any data here */
488       g_free (adapter->assembled_data);
489       adapter->assembled_data = g_malloc (adapter->assembled_size);
490     } else {
491       /* we are going to reuse all data, realloc then */
492       GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "reusing %" G_GSIZE_FORMAT " bytes",
493           toreuse);
494       adapter->assembled_data =
495           g_realloc (adapter->assembled_data, adapter->assembled_size);
496     }
497   }
498   GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy remaining %" G_GSIZE_FORMAT
499       " bytes from adapter", tocopy);
500   data = adapter->assembled_data;
501   copy_into_unchecked (adapter, data + toreuse, skip + toreuse, tocopy);
502   adapter->assembled_len = size;
503
504   return adapter->assembled_data;
505 }
506
507 /**
508  * gst_adapter_unmap:
509  * @adapter: a #GstAdapter
510  *
511  * Releases the memory obtained with the last gst_adapter_map().
512  */
513 void
514 gst_adapter_unmap (GstAdapter * adapter)
515 {
516   g_return_if_fail (GST_IS_ADAPTER (adapter));
517
518   if (adapter->info.memory) {
519     GstBuffer *cur = adapter->buflist->data;
520     GST_LOG_OBJECT (adapter, "unmap memory buffer %p", cur);
521     gst_buffer_unmap (cur, &adapter->info);
522     adapter->info.memory = NULL;
523   }
524 }
525
526 /**
527  * gst_adapter_copy: (skip)
528  * @adapter: a #GstAdapter
529  * @dest: (out caller-allocates) (array length=size) (element-type guint8):
530  *     the memory to copy into
531  * @offset: the bytes offset in the adapter to start from
532  * @size: the number of bytes to copy
533  *
534  * Copies @size bytes of data starting at @offset out of the buffers
535  * contained in #GstAdapter into an array @dest provided by the caller.
536  *
537  * The array @dest should be large enough to contain @size bytes.
538  * The user should check that the adapter has (@offset + @size) bytes
539  * available before calling this function.
540  */
541 void
542 gst_adapter_copy (GstAdapter * adapter, gpointer dest, gsize offset, gsize size)
543 {
544   g_return_if_fail (GST_IS_ADAPTER (adapter));
545   g_return_if_fail (size > 0);
546   g_return_if_fail (offset + size <= adapter->size);
547
548   copy_into_unchecked (adapter, dest, offset + adapter->skip, size);
549 }
550
551 /**
552  * gst_adapter_copy_bytes:
553  * @adapter: a #GstAdapter
554  * @offset: the bytes offset in the adapter to start from
555  * @size: the number of bytes to copy
556  *
557  * Similar to gst_adapter_copy, but more suitable for language bindings. @size
558  * bytes of data starting at @offset will be copied out of the buffers contained
559  * in @adapter and into a new #GBytes structure which is returned. Depending on
560  * the value of the @size argument an empty #GBytes structure may be returned.
561  *
562  * Returns: (transfer full): A new #GBytes structure containing the copied data.
563  *
564  * Rename to: gst_adapter_copy
565  *
566  * Since: 1.4
567  */
568 GBytes *
569 gst_adapter_copy_bytes (GstAdapter * adapter, gsize offset, gsize size)
570 {
571   gpointer data;
572   data = g_malloc (size);
573   gst_adapter_copy (adapter, data, offset, size);
574   return g_bytes_new_take (data, size);
575 }
576
577 /*Flushes the first @flush bytes in the @adapter*/
578 static void
579 gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush)
580 {
581   GstBuffer *cur;
582   gsize size;
583   GSList *g;
584
585   GST_LOG_OBJECT (adapter, "flushing %" G_GSIZE_FORMAT " bytes", flush);
586
587   if (adapter->info.memory)
588     gst_adapter_unmap (adapter);
589
590   /* clear state */
591   adapter->size -= flush;
592   adapter->assembled_len = 0;
593
594   /* take skip into account */
595   flush += adapter->skip;
596   /* distance is always at least the amount of skipped bytes */
597   adapter->pts_distance -= adapter->skip;
598   adapter->dts_distance -= adapter->skip;
599
600   g = adapter->buflist;
601   cur = g->data;
602   size = gst_buffer_get_size (cur);
603   while (flush >= size) {
604     /* can skip whole buffer */
605     GST_LOG_OBJECT (adapter, "flushing out head buffer");
606     adapter->pts_distance += size;
607     adapter->dts_distance += size;
608     flush -= size;
609
610     gst_buffer_unref (cur);
611     g = g_slist_delete_link (g, g);
612
613     if (G_UNLIKELY (g == NULL)) {
614       GST_LOG_OBJECT (adapter, "adapter empty now");
615       adapter->buflist_end = NULL;
616       break;
617     }
618     /* there is a new head buffer, update the timestamps */
619     cur = g->data;
620     update_timestamps (adapter, cur);
621     size = gst_buffer_get_size (cur);
622   }
623   adapter->buflist = g;
624   /* account for the remaining bytes */
625   adapter->skip = flush;
626   adapter->pts_distance += flush;
627   adapter->dts_distance += flush;
628   /* invalidate scan position */
629   adapter->scan_offset = 0;
630   adapter->scan_entry = NULL;
631 }
632
633 /**
634  * gst_adapter_flush:
635  * @adapter: a #GstAdapter
636  * @flush: the number of bytes to flush
637  *
638  * Flushes the first @flush bytes in the @adapter. The caller must ensure that
639  * at least this many bytes are available.
640  *
641  * See also: gst_adapter_map(), gst_adapter_unmap()
642  */
643 void
644 gst_adapter_flush (GstAdapter * adapter, gsize flush)
645 {
646   g_return_if_fail (GST_IS_ADAPTER (adapter));
647   g_return_if_fail (flush <= adapter->size);
648
649   /* flushing out 0 bytes will do nothing */
650   if (G_UNLIKELY (flush == 0))
651     return;
652
653   gst_adapter_flush_unchecked (adapter, flush);
654 }
655
656 /* internal function, nbytes should be flushed after calling this function */
657 static guint8 *
658 gst_adapter_take_internal (GstAdapter * adapter, gsize nbytes)
659 {
660   guint8 *data;
661   gsize toreuse, tocopy;
662
663   /* see how much data we can reuse from the assembled memory and how much
664    * we need to copy */
665   toreuse = MIN (nbytes, adapter->assembled_len);
666   tocopy = nbytes - toreuse;
667
668   /* find memory to return */
669   if (adapter->assembled_size >= nbytes && toreuse > 0) {
670     /* we reuse already allocated memory but only when we're going to reuse
671      * something from it because else we are worse than the malloc and copy
672      * case below */
673     GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes of assembled"
674         " data", toreuse);
675     /* we have enough free space in the assembled array */
676     data = adapter->assembled_data;
677     /* flush after this function should set the assembled_size to 0 */
678     adapter->assembled_data = g_malloc (adapter->assembled_size);
679   } else {
680     GST_LOG_OBJECT (adapter, "allocating %" G_GSIZE_FORMAT " bytes", nbytes);
681     /* not enough bytes in the assembled array, just allocate new space */
682     data = g_malloc (nbytes);
683     /* reuse what we can from the already assembled data */
684     if (toreuse) {
685       GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes", toreuse);
686       GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
687           "memcpy %" G_GSIZE_FORMAT " bytes", toreuse);
688       memcpy (data, adapter->assembled_data, toreuse);
689     }
690   }
691   if (tocopy) {
692     /* copy the remaining data */
693     copy_into_unchecked (adapter, toreuse + data, toreuse + adapter->skip,
694         tocopy);
695   }
696   return data;
697 }
698
699 /**
700  * gst_adapter_take:
701  * @adapter: a #GstAdapter
702  * @nbytes: the number of bytes to take
703  *
704  * Returns a freshly allocated buffer containing the first @nbytes bytes of the
705  * @adapter. The returned bytes will be flushed from the adapter.
706  *
707  * Caller owns returned value. g_free after usage.
708  *
709  * Free-function: g_free
710  *
711  * Returns: (transfer full) (array length=nbytes) (element-type guint8) (nullable):
712  *     oven-fresh hot data, or %NULL if @nbytes bytes are not available
713  */
714 gpointer
715 gst_adapter_take (GstAdapter * adapter, gsize nbytes)
716 {
717   gpointer data;
718
719   g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
720   g_return_val_if_fail (nbytes > 0, NULL);
721
722   /* we don't have enough data, return NULL. This is unlikely
723    * as one usually does an _available() first instead of peeking a
724    * random size. */
725   if (G_UNLIKELY (nbytes > adapter->size))
726     return NULL;
727
728   data = gst_adapter_take_internal (adapter, nbytes);
729
730   gst_adapter_flush_unchecked (adapter, nbytes);
731
732   return data;
733 }
734
735 /**
736  * gst_adapter_take_buffer_fast:
737  * @adapter:  a #GstAdapter
738  * @nbytes: the number of bytes to take
739  *
740  * Returns a #GstBuffer containing the first @nbytes of the @adapter.
741  * The returned bytes will be flushed from the adapter.  This function
742  * is potentially more performant than gst_adapter_take_buffer() since
743  * it can reuse the memory in pushed buffers by subbuffering or
744  * merging. Unlike gst_adapter_take_buffer(), the returned buffer may
745  * be composed of multiple non-contiguous #GstMemory objects, no
746  * copies are made.
747  *
748  * Note that no assumptions should be made as to whether certain buffer
749  * flags such as the DISCONT flag are set on the returned buffer, or not.
750  * The caller needs to explicitly set or unset flags that should be set or
751  * unset.
752  *
753  * This function can return buffer up to the return value of
754  * gst_adapter_available() without making copies if possible.
755  *
756  * Caller owns a reference to the returned buffer. gst_buffer_unref() after
757  * usage.
758  *
759  * Free-function: gst_buffer_unref
760  *
761  * Returns: (transfer full) (nullable): a #GstBuffer containing the first
762  *     @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
763  *     gst_buffer_unref() when no longer needed.
764  *
765  * Since: 1.2
766  */
767
768 GstBuffer *
769 gst_adapter_take_buffer_fast (GstAdapter * adapter, gsize nbytes)
770 {
771   GstBuffer *buffer = NULL;
772   GstBuffer *cur;
773   GSList *item;
774   gsize skip;
775   gsize left = nbytes;
776
777   g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
778   g_return_val_if_fail (nbytes > 0, NULL);
779
780   GST_LOG_OBJECT (adapter, "taking buffer of %" G_GSIZE_FORMAT " bytes",
781       nbytes);
782
783   /* we don't have enough data, return NULL. This is unlikely
784    * as one usually does an _available() first instead of grabbing a
785    * random size. */
786   if (G_UNLIKELY (nbytes > adapter->size))
787     return NULL;
788
789   skip = adapter->skip;
790   cur = adapter->buflist->data;
791
792   if (skip == 0 && gst_buffer_get_size (cur) == nbytes) {
793     GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
794         " as head buffer", nbytes);
795     buffer = gst_buffer_ref (cur);
796     goto done;
797   }
798
799   for (item = adapter->buflist; item && left > 0; item = item->next) {
800     gsize size;
801
802     cur = item->data;
803     size = MIN (gst_buffer_get_size (cur) - skip, left);
804
805     GST_LOG_OBJECT (adapter, "appending %" G_GSIZE_FORMAT " bytes"
806         " via region copy", size);
807     if (buffer)
808       gst_buffer_copy_into (buffer, cur, GST_BUFFER_COPY_MEMORY, skip, size);
809     else
810       buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, size);
811     skip = 0;
812     left -= size;
813   }
814
815 done:
816   gst_adapter_flush_unchecked (adapter, nbytes);
817
818   return buffer;
819 }
820
821 /**
822  * gst_adapter_take_buffer:
823  * @adapter: a #GstAdapter
824  * @nbytes: the number of bytes to take
825  *
826  * Returns a #GstBuffer containing the first @nbytes bytes of the
827  * @adapter. The returned bytes will be flushed from the adapter.
828  * This function is potentially more performant than
829  * gst_adapter_take() since it can reuse the memory in pushed buffers
830  * by subbuffering or merging. This function will always return a
831  * buffer with a single memory region.
832  *
833  * Note that no assumptions should be made as to whether certain buffer
834  * flags such as the DISCONT flag are set on the returned buffer, or not.
835  * The caller needs to explicitly set or unset flags that should be set or
836  * unset.
837  *
838  * Caller owns a reference to the returned buffer. gst_buffer_unref() after
839  * usage.
840  *
841  * Free-function: gst_buffer_unref
842  *
843  * Returns: (transfer full) (nullable): a #GstBuffer containing the first
844  *     @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
845  *     gst_buffer_unref() when no longer needed.
846  */
847 GstBuffer *
848 gst_adapter_take_buffer (GstAdapter * adapter, gsize nbytes)
849 {
850   GstBuffer *buffer;
851   GstBuffer *cur;
852   gsize hsize, skip;
853   guint8 *data;
854
855   g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
856   g_return_val_if_fail (nbytes > 0, NULL);
857
858   GST_LOG_OBJECT (adapter, "taking buffer of %" G_GSIZE_FORMAT " bytes",
859       nbytes);
860
861   /* we don't have enough data, return NULL. This is unlikely
862    * as one usually does an _available() first instead of grabbing a
863    * random size. */
864   if (G_UNLIKELY (nbytes > adapter->size))
865     return NULL;
866
867   cur = adapter->buflist->data;
868   skip = adapter->skip;
869   hsize = gst_buffer_get_size (cur);
870
871   /* our head buffer has enough data left, return it */
872   if (skip == 0 && hsize == nbytes) {
873     GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
874         " as head buffer", nbytes);
875     buffer = gst_buffer_ref (cur);
876     goto done;
877   } else if (hsize >= nbytes + skip) {
878     GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
879         " via region copy", nbytes);
880     buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
881     goto done;
882   }
883 #if 0
884   if (gst_adapter_try_to_merge_up (adapter, nbytes)) {
885     /* Merged something, let's try again for sub-buffering */
886     cur = adapter->buflist->data;
887     skip = adapter->skip;
888     if (gst_buffer_get_size (cur) >= nbytes + skip) {
889       GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
890           " via sub-buffer", nbytes);
891       buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
892       goto done;
893     }
894   }
895 #endif
896
897   data = gst_adapter_take_internal (adapter, nbytes);
898
899   buffer = gst_buffer_new_wrapped (data, nbytes);
900
901 done:
902   gst_adapter_flush_unchecked (adapter, nbytes);
903
904   return buffer;
905 }
906
907 /**
908  * gst_adapter_take_list:
909  * @adapter: a #GstAdapter
910  * @nbytes: the number of bytes to take
911  *
912  * Returns a #GList of buffers containing the first @nbytes bytes of the
913  * @adapter. The returned bytes will be flushed from the adapter.
914  * When the caller can deal with individual buffers, this function is more
915  * performant because no memory should be copied.
916  *
917  * Caller owns returned list and contained buffers. gst_buffer_unref() each
918  * buffer in the list before freeing the list after usage.
919  *
920  * Returns: (element-type Gst.Buffer) (transfer full) (nullable): a #GList of
921  *     buffers containing the first @nbytes of the adapter, or %NULL if @nbytes
922  *     bytes are not available
923  */
924 GList *
925 gst_adapter_take_list (GstAdapter * adapter, gsize nbytes)
926 {
927   GQueue queue = G_QUEUE_INIT;
928   GstBuffer *cur;
929   gsize hsize, skip;
930
931   g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
932   g_return_val_if_fail (nbytes <= adapter->size, NULL);
933
934   GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes);
935
936   while (nbytes > 0) {
937     cur = adapter->buflist->data;
938     skip = adapter->skip;
939     hsize = MIN (nbytes, gst_buffer_get_size (cur) - skip);
940
941     cur = gst_adapter_take_buffer (adapter, hsize);
942
943     g_queue_push_tail (&queue, cur);
944
945     nbytes -= hsize;
946   }
947   return queue.head;
948 }
949
950 /**
951  * gst_adapter_available:
952  * @adapter: a #GstAdapter
953  *
954  * Gets the maximum amount of bytes available, that is it returns the maximum
955  * value that can be supplied to gst_adapter_map() without that function
956  * returning %NULL.
957  *
958  * Returns: number of bytes available in @adapter
959  */
960 gsize
961 gst_adapter_available (GstAdapter * adapter)
962 {
963   g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
964
965   return adapter->size;
966 }
967
968 /**
969  * gst_adapter_available_fast:
970  * @adapter: a #GstAdapter
971  *
972  * Gets the maximum number of bytes that are immediately available without
973  * requiring any expensive operations (like copying the data into a
974  * temporary buffer).
975  *
976  * Returns: number of bytes that are available in @adapter without expensive
977  * operations
978  */
979 gsize
980 gst_adapter_available_fast (GstAdapter * adapter)
981 {
982   GstBuffer *cur;
983   gsize size;
984   GSList *g;
985
986   g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
987
988   /* no data */
989   if (adapter->size == 0)
990     return 0;
991
992   /* some stuff we already assembled */
993   if (adapter->assembled_len)
994     return adapter->assembled_len;
995
996   /* take the first non-zero buffer */
997   g = adapter->buflist;
998   while (TRUE) {
999     cur = g->data;
1000     size = gst_buffer_get_size (cur);
1001     if (size != 0)
1002       break;
1003     g = g_slist_next (g);
1004   }
1005
1006   /* we can quickly get the (remaining) data of the first buffer */
1007   return size - adapter->skip;
1008 }
1009
1010 /**
1011  * gst_adapter_prev_pts:
1012  * @adapter: a #GstAdapter
1013  * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1014  *
1015  * Get the pts that was before the current byte in the adapter. When
1016  * @distance is given, the amount of bytes between the pts and the current
1017  * position is returned.
1018  *
1019  * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1020  * the adapter is first created or when it is cleared. This also means that before
1021  * the first byte with a pts is removed from the adapter, the pts
1022  * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1023  *
1024  * Returns: The previously seen pts.
1025  */
1026 GstClockTime
1027 gst_adapter_prev_pts (GstAdapter * adapter, guint64 * distance)
1028 {
1029   g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1030
1031   if (distance)
1032     *distance = adapter->pts_distance;
1033
1034   return adapter->pts;
1035 }
1036
1037 /**
1038  * gst_adapter_prev_dts:
1039  * @adapter: a #GstAdapter
1040  * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1041  *
1042  * Get the dts that was before the current byte in the adapter. When
1043  * @distance is given, the amount of bytes between the dts and the current
1044  * position is returned.
1045  *
1046  * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1047  * the adapter is first created or when it is cleared. This also means that before
1048  * the first byte with a dts is removed from the adapter, the dts
1049  * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1050  *
1051  * Returns: The previously seen dts.
1052  */
1053 GstClockTime
1054 gst_adapter_prev_dts (GstAdapter * adapter, guint64 * distance)
1055 {
1056   g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1057
1058   if (distance)
1059     *distance = adapter->dts_distance;
1060
1061   return adapter->dts;
1062 }
1063
1064 /**
1065  * gst_adapter_prev_pts_at_offset:
1066  * @adapter: a #GstAdapter
1067  * @offset: the offset in the adapter at which to get timestamp
1068  * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1069  *
1070  * Get the pts that was before the byte at offset @offset in the adapter. When
1071  * @distance is given, the amount of bytes between the pts and the current
1072  * position is returned.
1073  *
1074  * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1075  * the adapter is first created or when it is cleared. This also means that before
1076  * the first byte with a pts is removed from the adapter, the pts
1077  * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1078  *
1079  * Since: 1.2
1080  * Returns: The previously seen pts at given offset.
1081  */
1082 GstClockTime
1083 gst_adapter_prev_pts_at_offset (GstAdapter * adapter, gsize offset,
1084     guint64 * distance)
1085 {
1086   GstBuffer *cur;
1087   GSList *g;
1088   gsize read_offset = 0;
1089   GstClockTime pts = adapter->pts;
1090
1091   g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1092
1093   g = adapter->buflist;
1094
1095   while (g && read_offset < offset + adapter->skip) {
1096     cur = g->data;
1097
1098     read_offset += gst_buffer_get_size (cur);
1099     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (cur))) {
1100       pts = GST_BUFFER_PTS (cur);
1101     }
1102
1103     g = g_slist_next (g);
1104   }
1105
1106   if (distance)
1107     *distance = adapter->dts_distance + offset;
1108
1109   return pts;
1110 }
1111
1112 /**
1113  * gst_adapter_prev_dts_at_offset:
1114  * @adapter: a #GstAdapter
1115  * @offset: the offset in the adapter at which to get timestamp
1116  * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1117  *
1118  * Get the dts that was before the byte at offset @offset in the adapter. When
1119  * @distance is given, the amount of bytes between the dts and the current
1120  * position is returned.
1121  *
1122  * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1123  * the adapter is first created or when it is cleared. This also means that before
1124  * the first byte with a dts is removed from the adapter, the dts
1125  * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1126  *
1127  * Since: 1.2
1128  * Returns: The previously seen dts at given offset.
1129  */
1130 GstClockTime
1131 gst_adapter_prev_dts_at_offset (GstAdapter * adapter, gsize offset,
1132     guint64 * distance)
1133 {
1134   GstBuffer *cur;
1135   GSList *g;
1136   gsize read_offset = 0;
1137   GstClockTime dts = adapter->dts;
1138
1139   g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1140
1141   g = adapter->buflist;
1142
1143   while (g && read_offset < offset + adapter->skip) {
1144     cur = g->data;
1145
1146     read_offset += gst_buffer_get_size (cur);
1147     if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cur))) {
1148       dts = GST_BUFFER_DTS (cur);
1149     }
1150
1151     g = g_slist_next (g);
1152   }
1153
1154   if (distance)
1155     *distance = adapter->dts_distance + offset;
1156
1157   return dts;
1158 }
1159
1160 /**
1161  * gst_adapter_masked_scan_uint32_peek:
1162  * @adapter: a #GstAdapter
1163  * @mask: mask to apply to data before matching against @pattern
1164  * @pattern: pattern to match (after mask is applied)
1165  * @offset: offset into the adapter data from which to start scanning, returns
1166  *          the last scanned position.
1167  * @size: number of bytes to scan from offset
1168  * @value: pointer to uint32 to return matching data
1169  *
1170  * Scan for pattern @pattern with applied mask @mask in the adapter data,
1171  * starting from offset @offset.  If a match is found, the value that matched
1172  * is returned through @value, otherwise @value is left untouched.
1173  *
1174  * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1175  * of endianness.  All four bytes of the pattern must be present in the
1176  * adapter for it to match, even if the first or last bytes are masked out.
1177  *
1178  * It is an error to call this function without making sure that there is
1179  * enough data (offset+size bytes) in the adapter.
1180  *
1181  * Returns: offset of the first match, or -1 if no match was found.
1182  */
1183 gssize
1184 gst_adapter_masked_scan_uint32_peek (GstAdapter * adapter, guint32 mask,
1185     guint32 pattern, gsize offset, gsize size, guint32 * value)
1186 {
1187   GSList *g;
1188   gsize skip, bsize, i;
1189   guint32 state;
1190   GstMapInfo info;
1191   guint8 *bdata;
1192   GstBuffer *buf;
1193
1194   g_return_val_if_fail (size > 0, -1);
1195   g_return_val_if_fail (offset + size <= adapter->size, -1);
1196   g_return_val_if_fail (((~mask) & pattern) == 0, -1);
1197
1198   /* we can't find the pattern with less than 4 bytes */
1199   if (G_UNLIKELY (size < 4))
1200     return -1;
1201
1202   skip = offset + adapter->skip;
1203
1204   /* first step, do skipping and position on the first buffer */
1205   /* optimistically assume scanning continues sequentially */
1206   if (adapter->scan_entry && (adapter->scan_offset <= skip)) {
1207     g = adapter->scan_entry;
1208     skip -= adapter->scan_offset;
1209   } else {
1210     g = adapter->buflist;
1211     adapter->scan_offset = 0;
1212     adapter->scan_entry = NULL;
1213   }
1214   buf = g->data;
1215   bsize = gst_buffer_get_size (buf);
1216   while (G_UNLIKELY (skip >= bsize)) {
1217     skip -= bsize;
1218     g = g_slist_next (g);
1219     adapter->scan_offset += bsize;
1220     adapter->scan_entry = g;
1221     buf = g->data;
1222     bsize = gst_buffer_get_size (buf);
1223   }
1224   /* get the data now */
1225   if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1226     return -1;
1227
1228   bdata = (guint8 *) info.data + skip;
1229   bsize = info.size - skip;
1230   skip = 0;
1231
1232   /* set the state to something that does not match */
1233   state = ~pattern;
1234
1235   /* now find data */
1236   do {
1237     bsize = MIN (bsize, size);
1238     for (i = 0; i < bsize; i++) {
1239       state = ((state << 8) | bdata[i]);
1240       if (G_UNLIKELY ((state & mask) == pattern)) {
1241         /* we have a match but we need to have skipped at
1242          * least 4 bytes to fill the state. */
1243         if (G_LIKELY (skip + i >= 3)) {
1244           if (G_LIKELY (value))
1245             *value = state;
1246           gst_buffer_unmap (buf, &info);
1247           return offset + skip + i - 3;
1248         }
1249       }
1250     }
1251     size -= bsize;
1252     if (size == 0)
1253       break;
1254
1255     /* nothing found yet, go to next buffer */
1256     skip += bsize;
1257     g = g_slist_next (g);
1258     adapter->scan_offset += info.size;
1259     adapter->scan_entry = g;
1260     gst_buffer_unmap (buf, &info);
1261     buf = g->data;
1262
1263     if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1264       return -1;
1265
1266     bsize = info.size;
1267     bdata = info.data;
1268   } while (TRUE);
1269
1270   gst_buffer_unmap (buf, &info);
1271
1272   /* nothing found */
1273   return -1;
1274 }
1275
1276 /**
1277  * gst_adapter_masked_scan_uint32:
1278  * @adapter: a #GstAdapter
1279  * @mask: mask to apply to data before matching against @pattern
1280  * @pattern: pattern to match (after mask is applied)
1281  * @offset: offset into the adapter data from which to start scanning, returns
1282  *          the last scanned position.
1283  * @size: number of bytes to scan from offset
1284  *
1285  * Scan for pattern @pattern with applied mask @mask in the adapter data,
1286  * starting from offset @offset.
1287  *
1288  * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1289  * of endianness.  All four bytes of the pattern must be present in the
1290  * adapter for it to match, even if the first or last bytes are masked out.
1291  *
1292  * It is an error to call this function without making sure that there is
1293  * enough data (offset+size bytes) in the adapter.
1294  *
1295  * This function calls gst_adapter_masked_scan_uint32_peek() passing %NULL
1296  * for value.
1297  *
1298  * Returns: offset of the first match, or -1 if no match was found.
1299  *
1300  * Example:
1301  * <programlisting>
1302  * // Assume the adapter contains 0x00 0x01 0x02 ... 0xfe 0xff
1303  *
1304  * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 0, 256);
1305  * // -> returns 0
1306  * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 1, 255);
1307  * // -> returns -1
1308  * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x01020304, 1, 255);
1309  * // -> returns 1
1310  * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0001, 0, 256);
1311  * // -> returns -1
1312  * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0203, 0, 256);
1313  * // -> returns 0
1314  * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 256);
1315  * // -> returns 2
1316  * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 4);
1317  * // -> returns -1
1318  * </programlisting>
1319  */
1320 gssize
1321 gst_adapter_masked_scan_uint32 (GstAdapter * adapter, guint32 mask,
1322     guint32 pattern, gsize offset, gsize size)
1323 {
1324   return gst_adapter_masked_scan_uint32_peek (adapter, mask, pattern, offset,
1325       size, NULL);
1326 }