2 * Copyright (C) 2004 Benjamin Otte <otte@gnome.org>
3 * 2005 Wim Taymans <wim@fluendo.com>
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 * @short_description: adapts incoming data on a sink pad into chunks of N bytes
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.
31 * An adapter is created with gst_adapter_new(). It can be freed again with
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().
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.
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:
47 * static GstFlowReturn
48 * sink_pad_chain (GstPad *pad, GstBuffer *buffer)
51 * GstAdapter *adapter;
52 * GstFlowReturn ret = GST_FLOW_OK;
54 * // will give the element an extra ref; remember to drop it
55 * this = MY_ELEMENT (gst_pad_get_parent (pad));
56 * adapter = this->adapter;
58 * // put buffer into adapter
59 * gst_adapter_push (adapter, buffer);
60 * // while we can read out 512 bytes, process them
61 * while (gst_adapter_available (adapter) >= 512 && ret == GST_FLOW_OK) {
62 * const guint8 *data = gst_adapter_map (adapter, 512);
63 * // use flowreturn as an error value
64 * ret = my_library_foo (data);
65 * gst_adapter_unmap (adapter);
66 * gst_adapter_flush (adapter, 512);
69 * gst_object_unref (this);
74 * For another example, a simple element inside GStreamer that uses GstAdapter
75 * is the libvisual element.
77 * An element using GstAdapter in its sink pad chain function should ensure that
78 * when the FLUSH_STOP event is received, that any queued data is cleared using
79 * gst_adapter_clear(). Data should also be cleared or processed on EOS and
80 * when changing state from #GST_STATE_PAUSED to #GST_STATE_READY.
82 * Also check the GST_BUFFER_FLAG_DISCONT flag on the buffer. Some elements might
83 * need to clear the adapter after a discontinuity.
85 * Since 0.10.24, the adapter will keep track of the timestamps of the buffers
86 * that were pushed. The last seen timestamp before the current position
87 * can be queried with gst_adapter_prev_timestamp(). This function can
88 * optionally return the amount of bytes between the start of the buffer that
89 * carried the timestamp and the current adapter position. The distance is
90 * useful when dealing with, for example, raw audio samples because it allows
91 * you to calculate the timestamp of the current adapter position by using the
92 * last seen timestamp and the amount of bytes since.
94 * A last thing to note is that while GstAdapter is pretty optimized,
95 * merging buffers still might be an operation that requires a malloc() and
96 * memcpy() operation, and these operations are not the fastest. Because of
97 * this, some functions like gst_adapter_available_fast() are provided to help
98 * speed up such cases should you want to. To avoid repeated memory allocations,
99 * gst_adapter_copy() can be used to copy data into a (statically allocated)
100 * user provided buffer.
102 * GstAdapter is not MT safe. All operations on an adapter must be serialized by
103 * the caller. This is not normally a problem, however, as the normal use case
104 * of GstAdapter is inside one pad's chain function, in which case access is
105 * serialized via the pad's STREAM_LOCK.
107 * Note that gst_adapter_push() takes ownership of the buffer passed. Use
108 * gst_buffer_ref() before pushing it into the adapter if you still want to
109 * access the buffer later. The adapter will never modify the data in the
110 * buffer pushed in it.
112 * Last reviewed on 2009-05-13 (0.10.24).
115 #include <gst/gst_private.h>
116 #include "gstadapter.h"
119 /* default size for the assembled data buffer */
120 #define DEFAULT_SIZE 4096
122 static void gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush);
124 GST_DEBUG_CATEGORY_STATIC (gst_adapter_debug);
125 #define GST_CAT_DEFAULT gst_adapter_debug
127 #define GST_ADAPTER_GET_PRIVATE(obj) \
128 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_ADAPTER, GstAdapterPrivate))
130 struct _GstAdapterPrivate
133 guint64 pts_distance;
135 guint64 dts_distance;
144 GST_DEBUG_CATEGORY_INIT (gst_adapter_debug, "adapter", 0, "object to splice and merge buffers to desired size")
145 #define gst_adapter_parent_class parent_class
146 G_DEFINE_TYPE_WITH_CODE (GstAdapter, gst_adapter, G_TYPE_OBJECT, _do_init);
148 static void gst_adapter_dispose (GObject * object);
149 static void gst_adapter_finalize (GObject * object);
152 gst_adapter_class_init (GstAdapterClass * klass)
154 GObjectClass *object = G_OBJECT_CLASS (klass);
156 g_type_class_add_private (klass, sizeof (GstAdapterPrivate));
158 object->dispose = gst_adapter_dispose;
159 object->finalize = gst_adapter_finalize;
163 gst_adapter_init (GstAdapter * adapter)
165 adapter->priv = GST_ADAPTER_GET_PRIVATE (adapter);
166 adapter->assembled_data = g_malloc (DEFAULT_SIZE);
167 adapter->assembled_size = DEFAULT_SIZE;
168 adapter->priv->pts = GST_CLOCK_TIME_NONE;
169 adapter->priv->pts_distance = 0;
170 adapter->priv->dts = GST_CLOCK_TIME_NONE;
171 adapter->priv->dts_distance = 0;
175 gst_adapter_dispose (GObject * object)
177 GstAdapter *adapter = GST_ADAPTER (object);
179 gst_adapter_clear (adapter);
181 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
185 gst_adapter_finalize (GObject * object)
187 GstAdapter *adapter = GST_ADAPTER (object);
189 g_free (adapter->assembled_data);
191 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
197 * Creates a new #GstAdapter. Free with g_object_unref().
199 * Returns: (transfer full): a new #GstAdapter
202 gst_adapter_new (void)
204 return g_object_newv (GST_TYPE_ADAPTER, 0, NULL);
209 * @adapter: a #GstAdapter
211 * Removes all buffers from @adapter.
214 gst_adapter_clear (GstAdapter * adapter)
216 GstAdapterPrivate *priv;
218 g_return_if_fail (GST_IS_ADAPTER (adapter));
220 priv = adapter->priv;
222 if (priv->info.memory)
223 gst_adapter_unmap (adapter);
225 g_slist_foreach (adapter->buflist, (GFunc) gst_mini_object_unref, NULL);
226 g_slist_free (adapter->buflist);
227 adapter->buflist = NULL;
228 adapter->buflist_end = NULL;
231 adapter->assembled_len = 0;
232 priv->pts = GST_CLOCK_TIME_NONE;
233 priv->pts_distance = 0;
234 priv->dts = GST_CLOCK_TIME_NONE;
235 priv->dts_distance = 0;
236 priv->scan_offset = 0;
237 priv->scan_entry = NULL;
241 update_timestamps (GstAdapter * adapter, GstBuffer * buf)
243 GstClockTime pts, dts;
245 pts = GST_BUFFER_PTS (buf);
246 if (GST_CLOCK_TIME_IS_VALID (pts)) {
247 GST_LOG_OBJECT (adapter, "new pts %" GST_TIME_FORMAT, GST_TIME_ARGS (pts));
248 adapter->priv->pts = pts;
249 adapter->priv->pts_distance = 0;
251 dts = GST_BUFFER_DTS (buf);
252 if (GST_CLOCK_TIME_IS_VALID (dts)) {
253 GST_LOG_OBJECT (adapter, "new dts %" GST_TIME_FORMAT, GST_TIME_ARGS (dts));
254 adapter->priv->dts = dts;
255 adapter->priv->dts_distance = 0;
259 /* copy data into @dest, skipping @skip bytes from the head buffers */
261 copy_into_unchecked (GstAdapter * adapter, guint8 * dest, gsize skip,
268 /* first step, do skipping */
269 /* we might well be copying where we were scanning */
270 if (adapter->priv->scan_entry && (adapter->priv->scan_offset <= skip)) {
271 g = adapter->priv->scan_entry;
272 skip -= adapter->priv->scan_offset;
274 g = adapter->buflist;
277 bsize = gst_buffer_get_size (buf);
278 while (G_UNLIKELY (skip >= bsize)) {
280 g = g_slist_next (g);
282 bsize = gst_buffer_get_size (buf);
284 /* copy partial buffer */
285 csize = MIN (bsize - skip, size);
286 GST_DEBUG ("bsize %" G_GSIZE_FORMAT ", skip %" G_GSIZE_FORMAT ", csize %"
287 G_GSIZE_FORMAT, bsize, skip, csize);
288 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter, "extract %" G_GSIZE_FORMAT
290 gst_buffer_extract (buf, skip, dest, csize);
294 /* second step, copy remainder */
296 g = g_slist_next (g);
298 bsize = gst_buffer_get_size (buf);
299 if (G_LIKELY (bsize > 0)) {
300 csize = MIN (bsize, size);
301 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
302 "extract %" G_GSIZE_FORMAT " bytes", csize);
303 gst_buffer_extract (buf, 0, dest, csize);
312 * @adapter: a #GstAdapter
313 * @buf: (transfer full): a #GstBuffer to add to queue in the adapter
315 * Adds the data from @buf to the data stored inside @adapter and takes
316 * ownership of the buffer.
319 gst_adapter_push (GstAdapter * adapter, GstBuffer * buf)
323 g_return_if_fail (GST_IS_ADAPTER (adapter));
324 g_return_if_fail (GST_IS_BUFFER (buf));
326 size = gst_buffer_get_size (buf);
327 adapter->size += size;
329 /* Note: merging buffers at this point is premature. */
330 if (G_UNLIKELY (adapter->buflist == NULL)) {
331 GST_LOG_OBJECT (adapter, "pushing %p first %" G_GSIZE_FORMAT " bytes",
333 adapter->buflist = adapter->buflist_end = g_slist_append (NULL, buf);
334 update_timestamps (adapter, buf);
336 /* Otherwise append to the end, and advance our end pointer */
337 GST_LOG_OBJECT (adapter, "pushing %p %" G_GSIZE_FORMAT " bytes at end, "
338 "size now %" G_GSIZE_FORMAT, buf, size, adapter->size);
339 adapter->buflist_end = g_slist_append (adapter->buflist_end, buf);
340 adapter->buflist_end = g_slist_next (adapter->buflist_end);
344 /* Internal method only. Tries to merge buffers at the head of the queue
345 * to form a single larger buffer of size 'size'.
347 * Returns TRUE if it managed to merge anything.
350 gst_adapter_try_to_merge_up (GstAdapter * adapter, gsize size)
352 GstBuffer *cur, *head;
354 gboolean ret = FALSE;
357 g = adapter->buflist;
362 g = g_slist_next (g);
364 hsize = gst_buffer_get_size (head);
366 /* Remove skipped part from the buffer (otherwise the buffer might grow indefinitely) */
367 gst_buffer_resize (head, adapter->skip, hsize - adapter->skip);
368 hsize -= adapter->skip;
371 while (g != NULL && hsize < size) {
373 /* Merge the head buffer and the next in line */
374 GST_LOG_OBJECT (adapter, "Merging buffers of size %" G_GSIZE_FORMAT " & %"
375 G_GSIZE_FORMAT " in search of target %" G_GSIZE_FORMAT,
376 hsize, gst_buffer_get_size (cur), size);
378 head = gst_buffer_append (head, cur);
379 hsize = gst_buffer_get_size (head);
382 /* Delete the front list item, and store our new buffer in the 2nd list
384 adapter->buflist = g_slist_delete_link (adapter->buflist, adapter->buflist);
387 /* invalidate scan position */
388 adapter->priv->scan_offset = 0;
389 adapter->priv->scan_entry = NULL;
391 g = g_slist_next (g);
399 * @adapter: a #GstAdapter
400 * @size: the number of bytes to map/peek
402 * Gets the first @size bytes stored in the @adapter. The returned pointer is
403 * valid until the next function is called on the adapter.
405 * Note that setting the returned pointer as the data of a #GstBuffer is
406 * incorrect for general-purpose plugins. The reason is that if a downstream
407 * element stores the buffer so that it has access to it outside of the bounds
408 * of its chain function, the buffer will have an invalid data pointer after
409 * your element flushes the bytes. In that case you should use
410 * gst_adapter_take(), which returns a freshly-allocated buffer that you can set
411 * as #GstBuffer malloc_data or the potentially more performant
412 * gst_adapter_take_buffer().
414 * Returns #NULL if @size bytes are not available.
416 * Returns: (transfer none) (array length=size): a pointer to the first
417 * @size bytes of data, or NULL
420 gst_adapter_map (GstAdapter * adapter, gsize size)
422 GstAdapterPrivate *priv;
425 gsize toreuse, tocopy;
428 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
429 g_return_val_if_fail (size > 0, NULL);
431 priv = adapter->priv;
433 if (priv->info.memory)
434 gst_adapter_unmap (adapter);
436 /* we don't have enough data, return NULL. This is unlikely
437 * as one usually does an _available() first instead of peeking a
439 if (G_UNLIKELY (size > adapter->size))
442 /* we have enough assembled data, return it */
443 if (adapter->assembled_len >= size)
444 return adapter->assembled_data;
447 cur = adapter->buflist->data;
448 skip = adapter->skip;
450 csize = gst_buffer_get_size (cur);
451 if (csize >= size + skip) {
452 if (!gst_buffer_map (cur, &priv->info, GST_MAP_READ))
455 return (guint8 *) priv->info.data + skip;
457 /* We may be able to efficiently merge buffers in our pool to
458 * gather a big enough chunk to return it from the head buffer directly */
459 } while (gst_adapter_try_to_merge_up (adapter, size));
461 /* see how much data we can reuse from the assembled memory and how much
463 toreuse = adapter->assembled_len;
464 tocopy = size - toreuse;
466 /* Gonna need to copy stuff out */
467 if (G_UNLIKELY (adapter->assembled_size < size)) {
468 adapter->assembled_size = (size / DEFAULT_SIZE + 1) * DEFAULT_SIZE;
469 GST_DEBUG_OBJECT (adapter, "resizing internal buffer to %" G_GSIZE_FORMAT,
470 adapter->assembled_size);
472 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "alloc new buffer");
473 /* no g_realloc to avoid a memcpy that is not desired here since we are
474 * not going to reuse any data here */
475 g_free (adapter->assembled_data);
476 adapter->assembled_data = g_malloc (adapter->assembled_size);
478 /* we are going to reuse all data, realloc then */
479 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "reusing %" G_GSIZE_FORMAT " bytes",
481 adapter->assembled_data =
482 g_realloc (adapter->assembled_data, adapter->assembled_size);
485 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy remaining %" G_GSIZE_FORMAT
486 " bytes from adapter", tocopy);
487 data = adapter->assembled_data;
488 copy_into_unchecked (adapter, data + toreuse, skip + toreuse, tocopy);
489 adapter->assembled_len = size;
491 return adapter->assembled_data;
496 * @adapter: a #GstAdapter
498 * Releases the memory obtained with the last gst_adapter_map().
501 gst_adapter_unmap (GstAdapter * adapter)
503 GstAdapterPrivate *priv;
505 g_return_if_fail (GST_IS_ADAPTER (adapter));
507 priv = adapter->priv;
509 if (priv->info.memory) {
510 GstBuffer *cur = adapter->buflist->data;
511 GST_LOG_OBJECT (adapter, "unmap memory buffer %p", cur);
512 gst_buffer_unmap (cur, &priv->info);
513 priv->info.memory = NULL;
519 * @adapter: a #GstAdapter
520 * @dest: (out caller-allocates) (array length=size): the memory to copy into
521 * @offset: the bytes offset in the adapter to start from
522 * @size: the number of bytes to copy
524 * Copies @size bytes of data starting at @offset out of the buffers
525 * contained in @GstAdapter into an array @dest provided by the caller.
527 * The array @dest should be large enough to contain @size bytes.
528 * The user should check that the adapter has (@offset + @size) bytes
529 * available before calling this function.
534 gst_adapter_copy (GstAdapter * adapter, gpointer dest, gsize offset, gsize size)
536 g_return_if_fail (GST_IS_ADAPTER (adapter));
537 g_return_if_fail (size > 0);
538 g_return_if_fail (offset + size <= adapter->size);
540 copy_into_unchecked (adapter, dest, offset + adapter->skip, size);
545 * @adapter: a #GstAdapter
546 * @flush: the number of bytes to flush
548 * Flushes the first @flush bytes in the @adapter. The caller must ensure that
549 * at least this many bytes are available.
551 * See also: gst_adapter_map(), gst_adapter_unmap()
554 gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush)
558 GstAdapterPrivate *priv;
561 GST_LOG_OBJECT (adapter, "flushing %" G_GSIZE_FORMAT " bytes", flush);
563 priv = adapter->priv;
565 if (priv->info.memory)
566 gst_adapter_unmap (adapter);
569 adapter->size -= flush;
570 adapter->assembled_len = 0;
572 /* take skip into account */
573 flush += adapter->skip;
574 /* distance is always at least the amount of skipped bytes */
575 priv->pts_distance -= adapter->skip;
576 priv->dts_distance -= adapter->skip;
578 g = adapter->buflist;
580 size = gst_buffer_get_size (cur);
581 while (flush >= size) {
582 /* can skip whole buffer */
583 GST_LOG_OBJECT (adapter, "flushing out head buffer");
584 priv->pts_distance += size;
585 priv->dts_distance += size;
588 gst_buffer_unref (cur);
589 g = g_slist_delete_link (g, g);
591 if (G_UNLIKELY (g == NULL)) {
592 GST_LOG_OBJECT (adapter, "adapter empty now");
593 adapter->buflist_end = NULL;
596 /* there is a new head buffer, update the timestamps */
598 update_timestamps (adapter, cur);
599 size = gst_buffer_get_size (cur);
601 adapter->buflist = g;
602 /* account for the remaining bytes */
603 adapter->skip = flush;
604 adapter->priv->pts_distance += flush;
605 adapter->priv->dts_distance += flush;
606 /* invalidate scan position */
607 priv->scan_offset = 0;
608 priv->scan_entry = NULL;
612 gst_adapter_flush (GstAdapter * adapter, gsize flush)
614 g_return_if_fail (GST_IS_ADAPTER (adapter));
615 g_return_if_fail (flush <= adapter->size);
617 /* flushing out 0 bytes will do nothing */
618 if (G_UNLIKELY (flush == 0))
621 gst_adapter_flush_unchecked (adapter, flush);
624 /* internal function, nbytes should be flushed after calling this function */
626 gst_adapter_take_internal (GstAdapter * adapter, gsize nbytes)
629 gsize toreuse, tocopy;
631 /* see how much data we can reuse from the assembled memory and how much
633 toreuse = MIN (nbytes, adapter->assembled_len);
634 tocopy = nbytes - toreuse;
636 /* find memory to return */
637 if (adapter->assembled_size >= nbytes && toreuse > 0) {
638 /* we reuse already allocated memory but only when we're going to reuse
639 * something from it because else we are worse than the malloc and copy
641 GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes of assembled"
643 /* we have enough free space in the assembled array */
644 data = adapter->assembled_data;
645 /* flush after this function should set the assembled_size to 0 */
646 adapter->assembled_data = g_malloc (adapter->assembled_size);
648 GST_LOG_OBJECT (adapter, "allocating %" G_GSIZE_FORMAT " bytes", nbytes);
649 /* not enough bytes in the assembled array, just allocate new space */
650 data = g_malloc (nbytes);
651 /* reuse what we can from the already assembled data */
653 GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes", toreuse);
654 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
655 "memcpy %" G_GSIZE_FORMAT " bytes", toreuse);
656 memcpy (data, adapter->assembled_data, toreuse);
660 /* copy the remaining data */
661 copy_into_unchecked (adapter, toreuse + data, toreuse + adapter->skip,
669 * @adapter: a #GstAdapter
670 * @nbytes: the number of bytes to take
672 * Returns a freshly allocated buffer containing the first @nbytes bytes of the
673 * @adapter. The returned bytes will be flushed from the adapter.
675 * Caller owns returned value. g_free after usage.
677 * Free-function: g_free
679 * Returns: (transfer full) (array length=nbytes): oven-fresh hot data, or
680 * #NULL if @nbytes bytes are not available
683 gst_adapter_take (GstAdapter * adapter, gsize nbytes)
687 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
688 g_return_val_if_fail (nbytes > 0, NULL);
690 /* we don't have enough data, return NULL. This is unlikely
691 * as one usually does an _available() first instead of peeking a
693 if (G_UNLIKELY (nbytes > adapter->size))
696 data = gst_adapter_take_internal (adapter, nbytes);
698 gst_adapter_flush_unchecked (adapter, nbytes);
704 * gst_adapter_take_buffer:
705 * @adapter: a #GstAdapter
706 * @nbytes: the number of bytes to take
708 * Returns a #GstBuffer containing the first @nbytes bytes of the
709 * @adapter. The returned bytes will be flushed from the adapter.
710 * This function is potentially more performant than gst_adapter_take()
711 * since it can reuse the memory in pushed buffers by subbuffering
714 * Caller owns returned value. gst_buffer_unref() after usage.
716 * Free-function: gst_buffer_unref
718 * Returns: (transfer full): a #GstBuffer containing the first @nbytes of
719 * the adapter, or #NULL if @nbytes bytes are not available
724 gst_adapter_take_buffer (GstAdapter * adapter, gsize nbytes)
731 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
732 g_return_val_if_fail (nbytes > 0, NULL);
734 GST_LOG_OBJECT (adapter, "taking buffer of %" G_GSIZE_FORMAT " bytes",
737 /* we don't have enough data, return NULL. This is unlikely
738 * as one usually does an _available() first instead of grabbing a
740 if (G_UNLIKELY (nbytes > adapter->size))
743 cur = adapter->buflist->data;
744 skip = adapter->skip;
745 hsize = gst_buffer_get_size (cur);
747 /* our head buffer has enough data left, return it */
748 if (skip == 0 && hsize == nbytes) {
749 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
750 " as head buffer", nbytes);
751 buffer = gst_buffer_ref (cur);
753 } else if (hsize >= nbytes + skip) {
754 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
755 " via region copy", nbytes);
756 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
760 if (gst_adapter_try_to_merge_up (adapter, nbytes)) {
761 /* Merged something, let's try again for sub-buffering */
762 cur = adapter->buflist->data;
763 if (gst_buffer_get_size (cur) >= nbytes + skip) {
764 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
765 " via sub-buffer", nbytes);
766 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
771 data = gst_adapter_take_internal (adapter, nbytes);
773 buffer = gst_buffer_new_wrapped (data, nbytes);
776 gst_adapter_flush_unchecked (adapter, nbytes);
782 * gst_adapter_take_list:
783 * @adapter: a #GstAdapter
784 * @nbytes: the number of bytes to take
786 * Returns a #GList of buffers containing the first @nbytes bytes of the
787 * @adapter. The returned bytes will be flushed from the adapter.
788 * When the caller can deal with individual buffers, this function is more
789 * performant because no memory should be copied.
791 * Caller owns returned list and contained buffers. gst_buffer_unref() each
792 * buffer in the list before freeing the list after usage.
794 * Returns: (element-type Gst.Buffer) (transfer full): a #GList of buffers
795 * containing the first @nbytes of the adapter, or #NULL if @nbytes bytes
801 gst_adapter_take_list (GstAdapter * adapter, gsize nbytes)
803 GQueue queue = G_QUEUE_INIT;
807 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
808 g_return_val_if_fail (nbytes <= adapter->size, NULL);
810 GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes);
813 cur = adapter->buflist->data;
814 skip = adapter->skip;
815 hsize = MIN (nbytes, gst_buffer_get_size (cur) - skip);
817 cur = gst_adapter_take_buffer (adapter, hsize);
819 g_queue_push_tail (&queue, cur);
827 * gst_adapter_available:
828 * @adapter: a #GstAdapter
830 * Gets the maximum amount of bytes available, that is it returns the maximum
831 * value that can be supplied to gst_adapter_map() without that function
834 * Returns: number of bytes available in @adapter
837 gst_adapter_available (GstAdapter * adapter)
839 g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
841 return adapter->size;
845 * gst_adapter_available_fast:
846 * @adapter: a #GstAdapter
848 * Gets the maximum number of bytes that are immediately available without
849 * requiring any expensive operations (like copying the data into a
852 * Returns: number of bytes that are available in @adapter without expensive
856 gst_adapter_available_fast (GstAdapter * adapter)
862 g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
865 if (adapter->size == 0)
868 /* some stuff we already assembled */
869 if (adapter->assembled_len)
870 return adapter->assembled_len;
872 /* take the first non-zero buffer */
873 g = adapter->buflist;
876 size = gst_buffer_get_size (cur);
879 g = g_slist_next (g);
882 /* we can quickly get the (remaining) data of the first buffer */
883 return size - adapter->skip;
887 * gst_adapter_prev_pts:
888 * @adapter: a #GstAdapter
889 * @distance: (out) (allow-none): pointer to location for distance, or NULL
891 * Get the pts that was before the current byte in the adapter. When
892 * @distance is given, the amount of bytes between the pts and the current
893 * position is returned.
895 * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
896 * the adapter is first created or when it is cleared. This also means that before
897 * the first byte with a pts is removed from the adapter, the pts
898 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
900 * Returns: The previously seen pts.
903 gst_adapter_prev_pts (GstAdapter * adapter, guint64 * distance)
905 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
908 *distance = adapter->priv->pts_distance;
910 return adapter->priv->pts;
914 * gst_adapter_prev_dts:
915 * @adapter: a #GstAdapter
916 * @distance: (out) (allow-none): pointer to location for distance, or NULL
918 * Get the dts that was before the current byte in the adapter. When
919 * @distance is given, the amount of bytes between the dts and the current
920 * position is returned.
922 * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
923 * the adapter is first created or when it is cleared. This also means that before
924 * the first byte with a dts is removed from the adapter, the dts
925 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
927 * Returns: The previously seen dts.
930 gst_adapter_prev_dts (GstAdapter * adapter, guint64 * distance)
932 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
935 *distance = adapter->priv->dts_distance;
937 return adapter->priv->dts;
941 * gst_adapter_masked_scan_uint32_peek:
942 * @adapter: a #GstAdapter
943 * @mask: mask to apply to data before matching against @pattern
944 * @pattern: pattern to match (after mask is applied)
945 * @offset: offset into the adapter data from which to start scanning, returns
946 * the last scanned position.
947 * @size: number of bytes to scan from offset
948 * @value: pointer to uint32 to return matching data
950 * Scan for pattern @pattern with applied mask @mask in the adapter data,
951 * starting from offset @offset. If a match is found, the value that matched
952 * is returned through @value, otherwise @value is left untouched.
954 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
955 * of endianness. All four bytes of the pattern must be present in the
956 * adapter for it to match, even if the first or last bytes are masked out.
958 * It is an error to call this function without making sure that there is
959 * enough data (offset+size bytes) in the adapter.
961 * Returns: offset of the first match, or -1 if no match was found.
966 gst_adapter_masked_scan_uint32_peek (GstAdapter * adapter, guint32 mask,
967 guint32 pattern, gsize offset, gsize size, guint32 * value)
970 gsize skip, bsize, i;
976 g_return_val_if_fail (size > 0, -1);
977 g_return_val_if_fail (offset + size <= adapter->size, -1);
978 g_return_val_if_fail (((~mask) & pattern) == 0, -1);
980 /* we can't find the pattern with less than 4 bytes */
981 if (G_UNLIKELY (size < 4))
984 skip = offset + adapter->skip;
986 /* first step, do skipping and position on the first buffer */
987 /* optimistically assume scanning continues sequentially */
988 if (adapter->priv->scan_entry && (adapter->priv->scan_offset <= skip)) {
989 g = adapter->priv->scan_entry;
990 skip -= adapter->priv->scan_offset;
992 g = adapter->buflist;
993 adapter->priv->scan_offset = 0;
994 adapter->priv->scan_entry = NULL;
997 bsize = gst_buffer_get_size (buf);
998 while (G_UNLIKELY (skip >= bsize)) {
1000 g = g_slist_next (g);
1001 adapter->priv->scan_offset += bsize;
1002 adapter->priv->scan_entry = g;
1004 bsize = gst_buffer_get_size (buf);
1006 /* get the data now */
1007 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1010 bdata = (guint8 *) info.data + skip;
1011 bsize = info.size - skip;
1014 /* set the state to something that does not match */
1019 bsize = MIN (bsize, size);
1020 for (i = 0; i < bsize; i++) {
1021 state = ((state << 8) | bdata[i]);
1022 if (G_UNLIKELY ((state & mask) == pattern)) {
1023 /* we have a match but we need to have skipped at
1024 * least 4 bytes to fill the state. */
1025 if (G_LIKELY (skip + i >= 3)) {
1026 if (G_LIKELY (value))
1028 gst_buffer_unmap (buf, &info);
1029 return offset + skip + i - 3;
1037 /* nothing found yet, go to next buffer */
1039 g = g_slist_next (g);
1040 adapter->priv->scan_offset += info.size;
1041 adapter->priv->scan_entry = g;
1042 gst_buffer_unmap (buf, &info);
1045 gst_buffer_map (buf, &info, GST_MAP_READ);
1050 gst_buffer_unmap (buf, &info);
1057 * gst_adapter_masked_scan_uint32:
1058 * @adapter: a #GstAdapter
1059 * @mask: mask to apply to data before matching against @pattern
1060 * @pattern: pattern to match (after mask is applied)
1061 * @offset: offset into the adapter data from which to start scanning, returns
1062 * the last scanned position.
1063 * @size: number of bytes to scan from offset
1065 * Scan for pattern @pattern with applied mask @mask in the adapter data,
1066 * starting from offset @offset.
1068 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1069 * of endianness. All four bytes of the pattern must be present in the
1070 * adapter for it to match, even if the first or last bytes are masked out.
1072 * It is an error to call this function without making sure that there is
1073 * enough data (offset+size bytes) in the adapter.
1075 * This function calls gst_adapter_masked_scan_uint32_peek() passing NULL
1078 * Returns: offset of the first match, or -1 if no match was found.
1082 * // Assume the adapter contains 0x00 0x01 0x02 ... 0xfe 0xff
1084 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 0, 256);
1086 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 1, 255);
1088 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x01020304, 1, 255);
1090 * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0001, 0, 256);
1092 * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0203, 0, 256);
1094 * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 256);
1096 * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 4);
1103 gst_adapter_masked_scan_uint32 (GstAdapter * adapter, guint32 mask,
1104 guint32 pattern, gsize offset, gsize size)
1106 return gst_adapter_masked_scan_uint32_peek (adapter, mask, pattern, offset,