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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
24 * @short_description: adapts incoming data on a sink pad into chunks of N bytes
26 * This class is for elements that receive buffers in an undesired size.
27 * While for example raw video contains one image per buffer, the same is not
28 * true for a lot of other formats, especially those that come directly from
29 * a file. So if you have undefined buffer sizes and require a specific size,
30 * this object is for you.
32 * An adapter is created with gst_adapter_new(). It can be freed again with
35 * The theory of operation is like this: All buffers received are put
36 * into the adapter using gst_adapter_push() and the data is then read back
37 * in chunks of the desired size using gst_adapter_map()/gst_adapter_unmap()
38 * and/or gst_adapter_copy(). After the data has been processed, it is freed
39 * using gst_adapter_unmap().
41 * Other methods such as gst_adapter_take() and gst_adapter_take_buffer()
42 * combine gst_adapter_map() and gst_adapter_unmap() in one method and are
43 * potentially more convenient for some use cases.
45 * For example, a sink pad's chain function that needs to pass data to a library
46 * in 512-byte chunks could be implemented like this:
47 * |[<!-- language="C" -->
48 * static GstFlowReturn
49 * sink_pad_chain (GstPad *pad, GstObject *parent, GstBuffer *buffer)
52 * GstAdapter *adapter;
53 * GstFlowReturn ret = GST_FLOW_OK;
55 * this = MY_ELEMENT (parent);
57 * adapter = this->adapter;
59 * // put buffer into adapter
60 * gst_adapter_push (adapter, buffer);
62 * // while we can read out 512 bytes, process them
63 * while (gst_adapter_available (adapter) >= 512 && ret == GST_FLOW_OK) {
64 * const guint8 *data = gst_adapter_map (adapter, 512);
65 * // use flowreturn as an error value
66 * ret = my_library_foo (data);
67 * gst_adapter_unmap (adapter);
68 * gst_adapter_flush (adapter, 512);
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 * 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_pts(). This function can
88 * optionally return the number 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. Additionally, the
93 * gst_adapter_prev_pts_at_offset() can be used to determine the last
94 * seen timestamp at a particular offset in the adapter.
96 * The adapter will also keep track of the offset of the buffers
97 * (#GST_BUFFER_OFFSET) that were pushed. The last seen offset before the
98 * current position can be queried with gst_adapter_prev_offset(). This function
99 * can optionally return the number of bytes between the start of the buffer
100 * that carried the offset and the current adapter position.
102 * Additionally the adapter also keeps track of the PTS, DTS and buffer offset
103 * at the last discontinuity, which can be retrieved with
104 * gst_adapter_pts_at_discont(), gst_adapter_dts_at_discont() and
105 * gst_adapter_offset_at_discont(). The number of bytes that were consumed
106 * since then can be queried with gst_adapter_distance_from_discont().
108 * A last thing to note is that while #GstAdapter is pretty optimized,
109 * merging buffers still might be an operation that requires a malloc() and
110 * memcpy() operation, and these operations are not the fastest. Because of
111 * this, some functions like gst_adapter_available_fast() are provided to help
112 * speed up such cases should you want to. To avoid repeated memory allocations,
113 * gst_adapter_copy() can be used to copy data into a (statically allocated)
114 * user provided buffer.
116 * #GstAdapter is not MT safe. All operations on an adapter must be serialized by
117 * the caller. This is not normally a problem, however, as the normal use case
118 * of #GstAdapter is inside one pad's chain function, in which case access is
119 * serialized via the pad's STREAM_LOCK.
121 * Note that gst_adapter_push() takes ownership of the buffer passed. Use
122 * gst_buffer_ref() before pushing it into the adapter if you still want to
123 * access the buffer later. The adapter will never modify the data in the
124 * buffer pushed in it.
127 #include <gst/gst_private.h>
128 #include "gstadapter.h"
130 #include <gst/base/gstqueuearray.h>
132 /* default size for the assembled data buffer */
133 #define DEFAULT_SIZE 4096
135 static void gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush);
137 GST_DEBUG_CATEGORY_STATIC (gst_adapter_debug);
138 #define GST_CAT_DEFAULT gst_adapter_debug
145 GstQueueArray *bufqueue;
150 /* we keep state of assembled pieces */
151 gpointer assembled_data;
152 gsize assembled_size;
156 guint64 pts_distance;
158 guint64 dts_distance;
160 guint64 offset_distance;
163 /* G_MAXUINT when unset */
164 guint scan_entry_idx;
166 GstClockTime pts_at_discont;
167 GstClockTime dts_at_discont;
168 guint64 offset_at_discont;
170 guint64 distance_from_discont;
175 struct _GstAdapterClass
177 GObjectClass parent_class;
181 GST_DEBUG_CATEGORY_INIT (gst_adapter_debug, "adapter", 0, "object to splice and merge buffers to desired size")
182 #define gst_adapter_parent_class parent_class
183 G_DEFINE_TYPE_WITH_CODE (GstAdapter, gst_adapter, G_TYPE_OBJECT, _do_init);
185 static void gst_adapter_dispose (GObject * object);
186 static void gst_adapter_finalize (GObject * object);
189 gst_adapter_class_init (GstAdapterClass * klass)
191 GObjectClass *object = G_OBJECT_CLASS (klass);
193 object->dispose = gst_adapter_dispose;
194 object->finalize = gst_adapter_finalize;
198 gst_adapter_init (GstAdapter * adapter)
200 adapter->assembled_data = g_malloc (DEFAULT_SIZE);
201 adapter->assembled_size = DEFAULT_SIZE;
202 adapter->pts = GST_CLOCK_TIME_NONE;
203 adapter->pts_distance = 0;
204 adapter->dts = GST_CLOCK_TIME_NONE;
205 adapter->dts_distance = 0;
206 adapter->offset = GST_BUFFER_OFFSET_NONE;
207 adapter->offset_distance = 0;
208 adapter->pts_at_discont = GST_CLOCK_TIME_NONE;
209 adapter->dts_at_discont = GST_CLOCK_TIME_NONE;
210 adapter->offset_at_discont = GST_BUFFER_OFFSET_NONE;
211 adapter->distance_from_discont = 0;
212 adapter->bufqueue = gst_queue_array_new (10);
216 gst_adapter_dispose (GObject * object)
218 GstAdapter *adapter = GST_ADAPTER (object);
220 gst_adapter_clear (adapter);
222 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
226 gst_adapter_finalize (GObject * object)
228 GstAdapter *adapter = GST_ADAPTER (object);
230 g_free (adapter->assembled_data);
232 gst_queue_array_free (adapter->bufqueue);
234 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
240 * Creates a new #GstAdapter. Free with g_object_unref().
242 * Returns: (transfer full): a new #GstAdapter
245 gst_adapter_new (void)
247 return g_object_new (GST_TYPE_ADAPTER, NULL);
252 * @adapter: a #GstAdapter
254 * Removes all buffers from @adapter.
257 gst_adapter_clear (GstAdapter * adapter)
260 g_return_if_fail (GST_IS_ADAPTER (adapter));
262 if (adapter->info.memory)
263 gst_adapter_unmap (adapter);
265 while ((obj = gst_queue_array_pop_head (adapter->bufqueue)))
266 gst_mini_object_unref (obj);
271 adapter->assembled_len = 0;
272 adapter->pts = GST_CLOCK_TIME_NONE;
273 adapter->pts_distance = 0;
274 adapter->dts = GST_CLOCK_TIME_NONE;
275 adapter->dts_distance = 0;
276 adapter->offset = GST_BUFFER_OFFSET_NONE;
277 adapter->offset_distance = 0;
278 adapter->pts_at_discont = GST_CLOCK_TIME_NONE;
279 adapter->dts_at_discont = GST_CLOCK_TIME_NONE;
280 adapter->offset_at_discont = GST_BUFFER_OFFSET_NONE;
281 adapter->distance_from_discont = 0;
282 adapter->scan_offset = 0;
283 adapter->scan_entry_idx = G_MAXUINT;
287 update_timestamps_and_offset (GstAdapter * adapter, GstBuffer * buf)
289 GstClockTime pts, dts;
292 pts = GST_BUFFER_PTS (buf);
293 if (GST_CLOCK_TIME_IS_VALID (pts)) {
294 GST_LOG_OBJECT (adapter, "new pts %" GST_TIME_FORMAT, GST_TIME_ARGS (pts));
296 adapter->pts_distance = 0;
298 dts = GST_BUFFER_DTS (buf);
299 if (GST_CLOCK_TIME_IS_VALID (dts)) {
300 GST_LOG_OBJECT (adapter, "new dts %" GST_TIME_FORMAT, GST_TIME_ARGS (dts));
302 adapter->dts_distance = 0;
304 offset = GST_BUFFER_OFFSET (buf);
305 if (offset != GST_BUFFER_OFFSET_NONE) {
306 GST_LOG_OBJECT (adapter, "new offset %" G_GUINT64_FORMAT, offset);
307 adapter->offset = offset;
308 adapter->offset_distance = 0;
311 if (GST_BUFFER_IS_DISCONT (buf)) {
312 /* Take values as-is (might be NONE) */
313 adapter->pts_at_discont = pts;
314 adapter->dts_at_discont = dts;
315 adapter->offset_at_discont = offset;
316 adapter->distance_from_discont = 0;
320 /* copy data into @dest, skipping @skip bytes from the head buffers */
322 copy_into_unchecked (GstAdapter * adapter, guint8 * dest, gsize skip,
329 /* first step, do skipping */
330 /* we might well be copying where we were scanning */
331 if (adapter->scan_entry_idx != G_MAXUINT && (adapter->scan_offset <= skip)) {
332 idx = adapter->scan_entry_idx;
333 skip -= adapter->scan_offset;
337 buf = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
338 bsize = gst_buffer_get_size (buf);
339 while (G_UNLIKELY (skip >= bsize)) {
341 buf = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
342 bsize = gst_buffer_get_size (buf);
344 /* copy partial buffer */
345 csize = MIN (bsize - skip, size);
346 GST_DEBUG ("bsize %" G_GSIZE_FORMAT ", skip %" G_GSIZE_FORMAT ", csize %"
347 G_GSIZE_FORMAT, bsize, skip, csize);
348 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter, "extract %" G_GSIZE_FORMAT
350 gst_buffer_extract (buf, skip, dest, csize);
354 /* second step, copy remainder */
356 buf = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
357 bsize = gst_buffer_get_size (buf);
358 if (G_LIKELY (bsize > 0)) {
359 csize = MIN (bsize, size);
360 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
361 "extract %" G_GSIZE_FORMAT " bytes", csize);
362 gst_buffer_extract (buf, 0, dest, csize);
371 * @adapter: a #GstAdapter
372 * @buf: (transfer full): a #GstBuffer to add to queue in the adapter
374 * Adds the data from @buf to the data stored inside @adapter and takes
375 * ownership of the buffer.
378 gst_adapter_push (GstAdapter * adapter, GstBuffer * buf)
382 g_return_if_fail (GST_IS_ADAPTER (adapter));
383 g_return_if_fail (GST_IS_BUFFER (buf));
385 size = gst_buffer_get_size (buf);
386 adapter->size += size;
388 /* Note: merging buffers at this point is premature. */
389 if (gst_queue_array_is_empty (adapter->bufqueue)) {
390 GST_LOG_OBJECT (adapter, "pushing %p first %" G_GSIZE_FORMAT " bytes",
392 gst_queue_array_push_tail (adapter->bufqueue, buf);
393 update_timestamps_and_offset (adapter, buf);
395 /* Otherwise append to the end, and advance our end pointer */
396 GST_LOG_OBJECT (adapter, "pushing %p %" G_GSIZE_FORMAT " bytes at end, "
397 "size now %" G_GSIZE_FORMAT, buf, size, adapter->size);
398 gst_queue_array_push_tail (adapter->bufqueue, buf);
404 /* Internal method only. Tries to merge buffers at the head of the queue
405 * to form a single larger buffer of size 'size'.
407 * Returns %TRUE if it managed to merge anything.
410 gst_adapter_try_to_merge_up (GstAdapter * adapter, gsize size)
412 GstBuffer *cur, *head;
414 gboolean ret = FALSE;
417 g = adapter->buflist;
423 hsize = gst_buffer_get_size (head);
425 /* Remove skipped part from the buffer (otherwise the buffer might grow indefinitely) */
426 head = gst_buffer_make_writable (head);
427 gst_buffer_resize (head, adapter->skip, hsize - adapter->skip);
428 hsize -= adapter->skip;
432 g = g_slist_next (g);
434 while (g != NULL && hsize < size) {
436 /* Merge the head buffer and the next in line */
437 GST_LOG_OBJECT (adapter, "Merging buffers of size %" G_GSIZE_FORMAT " & %"
438 G_GSIZE_FORMAT " in search of target %" G_GSIZE_FORMAT,
439 hsize, gst_buffer_get_size (cur), size);
441 head = gst_buffer_append (head, cur);
442 hsize = gst_buffer_get_size (head);
445 /* Delete the front list item, and store our new buffer in the 2nd list
447 adapter->buflist = g_slist_delete_link (adapter->buflist, adapter->buflist);
450 /* invalidate scan position */
451 adapter->scan_offset = 0;
452 adapter->scan_entry = NULL;
454 g = g_slist_next (g);
463 * @adapter: a #GstAdapter
464 * @size: the number of bytes to map/peek
466 * Gets the first @size bytes stored in the @adapter. The returned pointer is
467 * valid until the next function is called on the adapter.
469 * Note that setting the returned pointer as the data of a #GstBuffer is
470 * incorrect for general-purpose plugins. The reason is that if a downstream
471 * element stores the buffer so that it has access to it outside of the bounds
472 * of its chain function, the buffer will have an invalid data pointer after
473 * your element flushes the bytes. In that case you should use
474 * gst_adapter_take(), which returns a freshly-allocated buffer that you can set
475 * as #GstBuffer memory or the potentially more performant
476 * gst_adapter_take_buffer().
478 * Returns %NULL if @size bytes are not available.
480 * Returns: (transfer none) (array length=size) (element-type guint8) (nullable):
481 * a pointer to the first @size bytes of data, or %NULL
484 gst_adapter_map (GstAdapter * adapter, gsize size)
488 gsize toreuse, tocopy;
491 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
492 g_return_val_if_fail (size > 0, NULL);
494 if (adapter->info.memory)
495 gst_adapter_unmap (adapter);
497 /* we don't have enough data, return NULL. This is unlikely
498 * as one usually does an _available() first instead of peeking a
500 if (G_UNLIKELY (size > adapter->size))
503 /* we have enough assembled data, return it */
504 if (adapter->assembled_len >= size)
505 return adapter->assembled_data;
510 cur = gst_queue_array_peek_head (adapter->bufqueue);
511 skip = adapter->skip;
513 csize = gst_buffer_get_size (cur);
514 if (csize >= size + skip) {
515 if (!gst_buffer_map (cur, &adapter->info, GST_MAP_READ))
518 return (guint8 *) adapter->info.data + skip;
520 /* We may be able to efficiently merge buffers in our pool to
521 * gather a big enough chunk to return it from the head buffer directly */
523 } while (gst_adapter_try_to_merge_up (adapter, size));
526 /* see how much data we can reuse from the assembled memory and how much
528 toreuse = adapter->assembled_len;
529 tocopy = size - toreuse;
531 /* Gonna need to copy stuff out */
532 if (G_UNLIKELY (adapter->assembled_size < size)) {
533 adapter->assembled_size = (size / DEFAULT_SIZE + 1) * DEFAULT_SIZE;
534 GST_DEBUG_OBJECT (adapter, "resizing internal buffer to %" G_GSIZE_FORMAT,
535 adapter->assembled_size);
537 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "alloc new buffer");
538 /* no g_realloc to avoid a memcpy that is not desired here since we are
539 * not going to reuse any data here */
540 g_free (adapter->assembled_data);
541 adapter->assembled_data = g_malloc (adapter->assembled_size);
543 /* we are going to reuse all data, realloc then */
544 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "reusing %" G_GSIZE_FORMAT " bytes",
546 adapter->assembled_data =
547 g_realloc (adapter->assembled_data, adapter->assembled_size);
550 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy remaining %" G_GSIZE_FORMAT
551 " bytes from adapter", tocopy);
552 data = adapter->assembled_data;
553 copy_into_unchecked (adapter, data + toreuse, skip + toreuse, tocopy);
554 adapter->assembled_len = size;
556 return adapter->assembled_data;
561 * @adapter: a #GstAdapter
563 * Releases the memory obtained with the last gst_adapter_map().
566 gst_adapter_unmap (GstAdapter * adapter)
568 g_return_if_fail (GST_IS_ADAPTER (adapter));
570 if (adapter->info.memory) {
571 GstBuffer *cur = gst_queue_array_peek_head (adapter->bufqueue);
572 GST_LOG_OBJECT (adapter, "unmap memory buffer %p", cur);
573 gst_buffer_unmap (cur, &adapter->info);
574 adapter->info.memory = NULL;
579 * gst_adapter_copy: (skip)
580 * @adapter: a #GstAdapter
581 * @dest: (out caller-allocates) (array length=size) (element-type guint8):
582 * the memory to copy into
583 * @offset: the bytes offset in the adapter to start from
584 * @size: the number of bytes to copy
586 * Copies @size bytes of data starting at @offset out of the buffers
587 * contained in #GstAdapter into an array @dest provided by the caller.
589 * The array @dest should be large enough to contain @size bytes.
590 * The user should check that the adapter has (@offset + @size) bytes
591 * available before calling this function.
594 gst_adapter_copy (GstAdapter * adapter, gpointer dest, gsize offset, gsize size)
596 g_return_if_fail (GST_IS_ADAPTER (adapter));
597 g_return_if_fail (size > 0);
598 g_return_if_fail (offset + size <= adapter->size);
600 copy_into_unchecked (adapter, dest, offset + adapter->skip, size);
604 * gst_adapter_copy_bytes: (rename-to gst_adapter_copy)
605 * @adapter: a #GstAdapter
606 * @offset: the bytes offset in the adapter to start from
607 * @size: the number of bytes to copy
609 * Similar to gst_adapter_copy, but more suitable for language bindings. @size
610 * bytes of data starting at @offset will be copied out of the buffers contained
611 * in @adapter and into a new #GBytes structure which is returned. Depending on
612 * the value of the @size argument an empty #GBytes structure may be returned.
614 * Returns: (transfer full): A new #GBytes structure containing the copied data.
619 gst_adapter_copy_bytes (GstAdapter * adapter, gsize offset, gsize size)
622 data = g_malloc (size);
623 gst_adapter_copy (adapter, data, offset, size);
624 return g_bytes_new_take (data, size);
627 /*Flushes the first @flush bytes in the @adapter*/
629 gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush)
634 GST_LOG_OBJECT (adapter, "flushing %" G_GSIZE_FORMAT " bytes", flush);
636 if (adapter->info.memory)
637 gst_adapter_unmap (adapter);
640 adapter->size -= flush;
641 adapter->assembled_len = 0;
643 /* take skip into account */
644 flush += adapter->skip;
645 /* distance is always at least the amount of skipped bytes */
646 adapter->pts_distance -= adapter->skip;
647 adapter->dts_distance -= adapter->skip;
648 adapter->offset_distance -= adapter->skip;
649 adapter->distance_from_discont -= adapter->skip;
651 cur = gst_queue_array_peek_head (adapter->bufqueue);
652 size = gst_buffer_get_size (cur);
653 while (flush >= size) {
654 /* can skip whole buffer */
655 GST_LOG_OBJECT (adapter, "flushing out head buffer");
656 adapter->pts_distance += size;
657 adapter->dts_distance += size;
658 adapter->offset_distance += size;
659 adapter->distance_from_discont += size;
665 gst_buffer_unref (gst_queue_array_pop_head (adapter->bufqueue));
667 if (gst_queue_array_is_empty (adapter->bufqueue)) {
668 GST_LOG_OBJECT (adapter, "adapter empty now");
671 /* there is a new head buffer, update the timestamps */
672 cur = gst_queue_array_peek_head (adapter->bufqueue);
673 update_timestamps_and_offset (adapter, cur);
674 size = gst_buffer_get_size (cur);
676 /* account for the remaining bytes */
677 adapter->skip = flush;
678 adapter->pts_distance += flush;
679 adapter->dts_distance += flush;
680 adapter->offset_distance += flush;
681 adapter->distance_from_discont += flush;
682 /* invalidate scan position */
683 adapter->scan_offset = 0;
684 adapter->scan_entry_idx = G_MAXUINT;
689 * @adapter: a #GstAdapter
690 * @flush: the number of bytes to flush
692 * Flushes the first @flush bytes in the @adapter. The caller must ensure that
693 * at least this many bytes are available.
695 * See also: gst_adapter_map(), gst_adapter_unmap()
698 gst_adapter_flush (GstAdapter * adapter, gsize flush)
700 g_return_if_fail (GST_IS_ADAPTER (adapter));
701 g_return_if_fail (flush <= adapter->size);
703 /* flushing out 0 bytes will do nothing */
704 if (G_UNLIKELY (flush == 0))
707 gst_adapter_flush_unchecked (adapter, flush);
710 /* internal function, nbytes should be flushed if needed after calling this function */
712 gst_adapter_get_internal (GstAdapter * adapter, gsize nbytes)
715 gsize toreuse, tocopy;
717 /* see how much data we can reuse from the assembled memory and how much
719 toreuse = MIN (nbytes, adapter->assembled_len);
720 tocopy = nbytes - toreuse;
722 /* find memory to return */
723 if (adapter->assembled_size >= nbytes && toreuse > 0) {
724 /* we reuse already allocated memory but only when we're going to reuse
725 * something from it because else we are worse than the malloc and copy
727 GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes of assembled"
729 /* we have enough free space in the assembled array */
730 data = adapter->assembled_data;
731 /* flush after this function should set the assembled_size to 0 */
732 adapter->assembled_data = g_malloc (adapter->assembled_size);
734 GST_LOG_OBJECT (adapter, "allocating %" G_GSIZE_FORMAT " bytes", nbytes);
735 /* not enough bytes in the assembled array, just allocate new space */
736 data = g_malloc (nbytes);
737 /* reuse what we can from the already assembled data */
739 GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes", toreuse);
740 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
741 "memcpy %" G_GSIZE_FORMAT " bytes", toreuse);
742 memcpy (data, adapter->assembled_data, toreuse);
746 /* copy the remaining data */
747 copy_into_unchecked (adapter, toreuse + data, toreuse + adapter->skip,
755 * @adapter: a #GstAdapter
756 * @nbytes: the number of bytes to take
758 * Returns a freshly allocated buffer containing the first @nbytes bytes of the
759 * @adapter. The returned bytes will be flushed from the adapter.
761 * Caller owns returned value. g_free after usage.
763 * Free-function: g_free
765 * Returns: (transfer full) (array length=nbytes) (element-type guint8) (nullable):
766 * oven-fresh hot data, or %NULL if @nbytes bytes are not available
769 gst_adapter_take (GstAdapter * adapter, gsize nbytes)
773 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
774 g_return_val_if_fail (nbytes > 0, NULL);
776 /* we don't have enough data, return NULL. This is unlikely
777 * as one usually does an _available() first instead of peeking a
779 if (G_UNLIKELY (nbytes > adapter->size))
782 data = gst_adapter_get_internal (adapter, nbytes);
784 gst_adapter_flush_unchecked (adapter, nbytes);
790 * gst_adapter_get_buffer_fast:
791 * @adapter: a #GstAdapter
792 * @nbytes: the number of bytes to get
794 * Returns a #GstBuffer containing the first @nbytes of the @adapter, but
795 * does not flush them from the adapter. See gst_adapter_take_buffer_fast()
798 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
801 * Free-function: gst_buffer_unref
803 * Returns: (transfer full) (nullable): a #GstBuffer containing the first
804 * @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
805 * gst_buffer_unref() when no longer needed.
810 gst_adapter_get_buffer_fast (GstAdapter * adapter, gsize nbytes)
812 GstBuffer *buffer = NULL;
818 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
819 g_return_val_if_fail (nbytes > 0, NULL);
821 GST_LOG_OBJECT (adapter, "getting buffer of %" G_GSIZE_FORMAT " bytes",
824 /* we don't have enough data, return NULL. This is unlikely
825 * as one usually does an _available() first instead of grabbing a
827 if (G_UNLIKELY (nbytes > adapter->size))
830 skip = adapter->skip;
831 cur = gst_queue_array_peek_head (adapter->bufqueue);
833 if (skip == 0 && gst_buffer_get_size (cur) == nbytes) {
834 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
835 " as head buffer", nbytes);
836 buffer = gst_buffer_ref (cur);
840 len = gst_queue_array_get_length (adapter->bufqueue);
842 for (idx = 0; idx < len && left > 0; idx++) {
843 gsize size, cur_size;
845 cur = gst_queue_array_peek_nth (adapter->bufqueue, idx);
846 cur_size = gst_buffer_get_size (cur);
847 size = MIN (cur_size - skip, left);
849 GST_LOG_OBJECT (adapter, "appending %" G_GSIZE_FORMAT " bytes"
850 " via region copy", size);
852 gst_buffer_copy_into (buffer, cur,
853 GST_BUFFER_COPY_MEMORY | GST_BUFFER_COPY_META, skip, size);
855 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, size);
866 * gst_adapter_take_buffer_fast:
867 * @adapter: a #GstAdapter
868 * @nbytes: the number of bytes to take
870 * Returns a #GstBuffer containing the first @nbytes of the @adapter.
871 * The returned bytes will be flushed from the adapter. This function
872 * is potentially more performant than gst_adapter_take_buffer() since
873 * it can reuse the memory in pushed buffers by subbuffering or
874 * merging. Unlike gst_adapter_take_buffer(), the returned buffer may
875 * be composed of multiple non-contiguous #GstMemory objects, no
878 * Note that no assumptions should be made as to whether certain buffer
879 * flags such as the DISCONT flag are set on the returned buffer, or not.
880 * The caller needs to explicitly set or unset flags that should be set or
883 * This will also copy over all GstMeta of the input buffers except
884 * for meta with the %GST_META_FLAG_POOLED flag or with the "memory" tag.
886 * This function can return buffer up to the return value of
887 * gst_adapter_available() without making copies if possible.
889 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
892 * Free-function: gst_buffer_unref
894 * Returns: (transfer full) (nullable): a #GstBuffer containing the first
895 * @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
896 * gst_buffer_unref() when no longer needed.
901 gst_adapter_take_buffer_fast (GstAdapter * adapter, gsize nbytes)
905 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
906 g_return_val_if_fail (nbytes > 0, NULL);
908 buffer = gst_adapter_get_buffer_fast (adapter, nbytes);
910 gst_adapter_flush_unchecked (adapter, nbytes);
916 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
918 GstBuffer *outbuf = user_data;
919 const GstMetaInfo *info = (*meta)->info;
920 gboolean do_copy = FALSE;
922 if (gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)) {
923 /* never call the transform_meta with memory specific metadata */
924 GST_DEBUG ("not copying memory specific metadata %s",
925 g_type_name (info->api));
929 GST_DEBUG ("copying metadata %s", g_type_name (info->api));
932 if (do_copy && info->transform_func) {
933 GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
934 GST_DEBUG ("copy metadata %s", g_type_name (info->api));
935 /* simply copy then */
936 info->transform_func (outbuf, *meta, inbuf,
937 _gst_meta_transform_copy, ©_data);
943 * gst_adapter_get_buffer:
944 * @adapter: a #GstAdapter
945 * @nbytes: the number of bytes to get
947 * Returns a #GstBuffer containing the first @nbytes of the @adapter, but
948 * does not flush them from the adapter. See gst_adapter_take_buffer()
951 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
954 * Free-function: gst_buffer_unref
956 * Returns: (transfer full) (nullable): a #GstBuffer containing the first
957 * @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
958 * gst_buffer_unref() when no longer needed.
963 gst_adapter_get_buffer (GstAdapter * adapter, gsize nbytes)
970 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
971 g_return_val_if_fail (nbytes > 0, NULL);
973 GST_LOG_OBJECT (adapter, "getting buffer of %" G_GSIZE_FORMAT " bytes",
976 /* we don't have enough data, return NULL. This is unlikely
977 * as one usually does an _available() first instead of grabbing a
979 if (G_UNLIKELY (nbytes > adapter->size))
982 cur = gst_queue_array_peek_head (adapter->bufqueue);
983 skip = adapter->skip;
984 hsize = gst_buffer_get_size (cur);
986 /* our head buffer has enough data left, return it */
987 if (skip == 0 && hsize == nbytes) {
988 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
989 " as head buffer", nbytes);
990 buffer = gst_buffer_ref (cur);
992 } else if (hsize >= nbytes + skip) {
993 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
994 " via region copy", nbytes);
995 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
999 if (gst_adapter_try_to_merge_up (adapter, nbytes)) {
1000 /* Merged something, let's try again for sub-buffering */
1001 cur = adapter->buflist->data;
1002 skip = adapter->skip;
1003 if (gst_buffer_get_size (cur) >= nbytes + skip) {
1004 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
1005 " via sub-buffer", nbytes);
1006 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
1012 data = gst_adapter_get_internal (adapter, nbytes);
1014 buffer = gst_buffer_new_wrapped (data, nbytes);
1019 gsize read_offset = 0;
1022 len = gst_queue_array_get_length (adapter->bufqueue);
1024 while (idx < len && read_offset < nbytes + adapter->skip) {
1025 cur = gst_queue_array_peek_nth (adapter->bufqueue, idx);
1027 gst_buffer_foreach_meta (cur, foreach_metadata, buffer);
1028 read_offset += gst_buffer_get_size (cur);
1040 * gst_adapter_take_buffer:
1041 * @adapter: a #GstAdapter
1042 * @nbytes: the number of bytes to take
1044 * Returns a #GstBuffer containing the first @nbytes bytes of the
1045 * @adapter. The returned bytes will be flushed from the adapter.
1046 * This function is potentially more performant than
1047 * gst_adapter_take() since it can reuse the memory in pushed buffers
1048 * by subbuffering or merging. This function will always return a
1049 * buffer with a single memory region.
1051 * Note that no assumptions should be made as to whether certain buffer
1052 * flags such as the DISCONT flag are set on the returned buffer, or not.
1053 * The caller needs to explicitly set or unset flags that should be set or
1056 * Since 1.6 this will also copy over all GstMeta of the input buffers except
1057 * for meta with the %GST_META_FLAG_POOLED flag or with the "memory" tag.
1059 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
1062 * Free-function: gst_buffer_unref
1064 * Returns: (transfer full) (nullable): a #GstBuffer containing the first
1065 * @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
1066 * gst_buffer_unref() when no longer needed.
1069 gst_adapter_take_buffer (GstAdapter * adapter, gsize nbytes)
1073 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1074 g_return_val_if_fail (nbytes > 0, NULL);
1076 buffer = gst_adapter_get_buffer (adapter, nbytes);
1078 gst_adapter_flush_unchecked (adapter, nbytes);
1084 * gst_adapter_take_list:
1085 * @adapter: a #GstAdapter
1086 * @nbytes: the number of bytes to take
1088 * Returns a #GList of buffers containing the first @nbytes bytes of the
1089 * @adapter. The returned bytes will be flushed from the adapter.
1090 * When the caller can deal with individual buffers, this function is more
1091 * performant because no memory should be copied.
1093 * Caller owns returned list and contained buffers. gst_buffer_unref() each
1094 * buffer in the list before freeing the list after usage.
1096 * Returns: (element-type Gst.Buffer) (transfer full) (nullable): a #GList of
1097 * buffers containing the first @nbytes of the adapter, or %NULL if @nbytes
1098 * bytes are not available
1101 gst_adapter_take_list (GstAdapter * adapter, gsize nbytes)
1103 GQueue queue = G_QUEUE_INIT;
1105 gsize hsize, skip, cur_size;
1107 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1108 g_return_val_if_fail (nbytes <= adapter->size, NULL);
1110 GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes);
1112 while (nbytes > 0) {
1113 cur = gst_queue_array_peek_head (adapter->bufqueue);
1114 skip = adapter->skip;
1115 cur_size = gst_buffer_get_size (cur);
1116 hsize = MIN (nbytes, cur_size - skip);
1118 cur = gst_adapter_take_buffer (adapter, hsize);
1120 g_queue_push_tail (&queue, cur);
1128 * gst_adapter_get_list:
1129 * @adapter: a #GstAdapter
1130 * @nbytes: the number of bytes to get
1132 * Returns a #GList of buffers containing the first @nbytes bytes of the
1133 * @adapter, but does not flush them from the adapter. See
1134 * gst_adapter_take_list() for details.
1136 * Caller owns returned list and contained buffers. gst_buffer_unref() each
1137 * buffer in the list before freeing the list after usage.
1139 * Returns: (element-type Gst.Buffer) (transfer full) (nullable): a #GList of
1140 * buffers containing the first @nbytes of the adapter, or %NULL if @nbytes
1141 * bytes are not available
1146 gst_adapter_get_list (GstAdapter * adapter, gsize nbytes)
1148 GQueue queue = G_QUEUE_INIT;
1149 GstBuffer *cur, *buffer;
1150 gsize hsize, skip, cur_size;
1153 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1154 g_return_val_if_fail (nbytes <= adapter->size, NULL);
1156 GST_LOG_OBJECT (adapter, "getting %" G_GSIZE_FORMAT " bytes", nbytes);
1159 skip = adapter->skip;
1161 while (nbytes > 0) {
1162 cur = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
1163 cur_size = gst_buffer_get_size (cur);
1164 hsize = MIN (nbytes, cur_size - skip);
1166 if (skip == 0 && cur_size == hsize) {
1167 GST_LOG_OBJECT (adapter,
1168 "inserting a buffer of %" G_GSIZE_FORMAT " bytes", hsize);
1169 buffer = gst_buffer_ref (cur);
1171 GST_LOG_OBJECT (adapter, "inserting a buffer of %" G_GSIZE_FORMAT " bytes"
1172 " via region copy", hsize);
1173 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, hsize);
1176 g_queue_push_tail (&queue, buffer);
1186 * gst_adapter_take_buffer_list:
1187 * @adapter: a #GstAdapter
1188 * @nbytes: the number of bytes to take
1190 * Returns a #GstBufferList of buffers containing the first @nbytes bytes of
1191 * the @adapter. The returned bytes will be flushed from the adapter.
1192 * When the caller can deal with individual buffers, this function is more
1193 * performant because no memory should be copied.
1195 * Caller owns the returned list. Call gst_buffer_list_unref() to free
1196 * the list after usage.
1198 * Returns: (transfer full) (nullable): a #GstBufferList of buffers containing
1199 * the first @nbytes of the adapter, or %NULL if @nbytes bytes are not
1205 gst_adapter_take_buffer_list (GstAdapter * adapter, gsize nbytes)
1207 GstBufferList *buffer_list;
1209 gsize hsize, skip, cur_size;
1212 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1214 if (nbytes > adapter->size)
1217 GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes);
1219 /* try to create buffer list with sufficient size, so no resize is done later */
1220 if (adapter->count < 64)
1221 n_bufs = adapter->count;
1223 n_bufs = (adapter->count * nbytes * 1.2 / adapter->size) + 1;
1225 buffer_list = gst_buffer_list_new_sized (n_bufs);
1227 while (nbytes > 0) {
1228 cur = gst_queue_array_peek_head (adapter->bufqueue);
1229 skip = adapter->skip;
1230 cur_size = gst_buffer_get_size (cur);
1231 hsize = MIN (nbytes, cur_size - skip);
1233 gst_buffer_list_add (buffer_list, gst_adapter_take_buffer (adapter, hsize));
1240 * gst_adapter_get_buffer_list:
1241 * @adapter: a #GstAdapter
1242 * @nbytes: the number of bytes to get
1244 * Returns a #GstBufferList of buffers containing the first @nbytes bytes of
1245 * the @adapter but does not flush them from the adapter. See
1246 * gst_adapter_take_buffer_list() for details.
1248 * Caller owns the returned list. Call gst_buffer_list_unref() to free
1249 * the list after usage.
1251 * Returns: (transfer full) (nullable): a #GstBufferList of buffers containing
1252 * the first @nbytes of the adapter, or %NULL if @nbytes bytes are not
1258 gst_adapter_get_buffer_list (GstAdapter * adapter, gsize nbytes)
1260 GstBufferList *buffer_list;
1261 GstBuffer *cur, *buffer;
1262 gsize hsize, skip, cur_size;
1266 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
1268 if (nbytes > adapter->size)
1271 GST_LOG_OBJECT (adapter, "getting %" G_GSIZE_FORMAT " bytes", nbytes);
1273 /* try to create buffer list with sufficient size, so no resize is done later */
1274 if (adapter->count < 64)
1275 n_bufs = adapter->count;
1277 n_bufs = (adapter->count * nbytes * 1.2 / adapter->size) + 1;
1279 buffer_list = gst_buffer_list_new_sized (n_bufs);
1282 skip = adapter->skip;
1284 while (nbytes > 0) {
1285 cur = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
1286 cur_size = gst_buffer_get_size (cur);
1287 hsize = MIN (nbytes, cur_size - skip);
1289 if (skip == 0 && cur_size == hsize) {
1290 GST_LOG_OBJECT (adapter,
1291 "inserting a buffer of %" G_GSIZE_FORMAT " bytes", hsize);
1292 buffer = gst_buffer_ref (cur);
1294 GST_LOG_OBJECT (adapter, "inserting a buffer of %" G_GSIZE_FORMAT " bytes"
1295 " via region copy", hsize);
1296 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, hsize);
1299 gst_buffer_list_add (buffer_list, buffer);
1309 * gst_adapter_available:
1310 * @adapter: a #GstAdapter
1312 * Gets the maximum amount of bytes available, that is it returns the maximum
1313 * value that can be supplied to gst_adapter_map() without that function
1316 * Returns: number of bytes available in @adapter
1319 gst_adapter_available (GstAdapter * adapter)
1321 g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
1323 return adapter->size;
1327 * gst_adapter_available_fast:
1328 * @adapter: a #GstAdapter
1330 * Gets the maximum number of bytes that are immediately available without
1331 * requiring any expensive operations (like copying the data into a
1332 * temporary buffer).
1334 * Returns: number of bytes that are available in @adapter without expensive
1338 gst_adapter_available_fast (GstAdapter * adapter)
1344 g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
1347 if (adapter->size == 0)
1350 /* some stuff we already assembled */
1351 if (adapter->assembled_len)
1352 return adapter->assembled_len;
1354 /* take the first non-zero buffer */
1357 cur = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
1358 size = gst_buffer_get_size (cur);
1363 /* we can quickly get the (remaining) data of the first buffer */
1364 return size - adapter->skip;
1368 * gst_adapter_get_distance_from_discont:
1369 * @adapter: a #GstAdapter
1371 * Get the distance in bytes since the last buffer with the
1372 * %GST_BUFFER_FLAG_DISCONT flag.
1374 * The distance will be reset to 0 for all buffers with
1375 * %GST_BUFFER_FLAG_DISCONT on them, and then calculated for all other
1376 * following buffers based on their size.
1380 * Returns: The offset. Can be %GST_BUFFER_OFFSET_NONE.
1383 gst_adapter_distance_from_discont (GstAdapter * adapter)
1385 return adapter->distance_from_discont;
1389 * gst_adapter_offset_at_discont:
1390 * @adapter: a #GstAdapter
1392 * Get the offset that was on the last buffer with the GST_BUFFER_FLAG_DISCONT
1393 * flag, or GST_BUFFER_OFFSET_NONE.
1397 * Returns: The offset at the last discont or GST_BUFFER_OFFSET_NONE.
1400 gst_adapter_offset_at_discont (GstAdapter * adapter)
1402 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_BUFFER_OFFSET_NONE);
1404 return adapter->offset_at_discont;
1408 * gst_adapter_pts_at_discont:
1409 * @adapter: a #GstAdapter
1411 * Get the PTS that was on the last buffer with the GST_BUFFER_FLAG_DISCONT
1412 * flag, or GST_CLOCK_TIME_NONE.
1416 * Returns: The PTS at the last discont or GST_CLOCK_TIME_NONE.
1419 gst_adapter_pts_at_discont (GstAdapter * adapter)
1421 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1423 return adapter->pts_at_discont;
1427 * gst_adapter_dts_at_discont:
1428 * @adapter: a #GstAdapter
1430 * Get the DTS that was on the last buffer with the GST_BUFFER_FLAG_DISCONT
1431 * flag, or GST_CLOCK_TIME_NONE.
1435 * Returns: The DTS at the last discont or GST_CLOCK_TIME_NONE.
1438 gst_adapter_dts_at_discont (GstAdapter * adapter)
1440 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1442 return adapter->dts_at_discont;
1446 * gst_adapter_prev_offset:
1447 * @adapter: a #GstAdapter
1448 * @distance: (out) (allow-none): pointer to a location for distance, or %NULL
1450 * Get the offset that was before the current byte in the adapter. When
1451 * @distance is given, the amount of bytes between the offset and the current
1452 * position is returned.
1454 * The offset is reset to GST_BUFFER_OFFSET_NONE and the distance is set to 0
1455 * when the adapter is first created or when it is cleared. This also means that
1456 * before the first byte with an offset is removed from the adapter, the offset
1457 * and distance returned are GST_BUFFER_OFFSET_NONE and 0 respectively.
1461 * Returns: The previous seen offset.
1464 gst_adapter_prev_offset (GstAdapter * adapter, guint64 * distance)
1466 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_BUFFER_OFFSET_NONE);
1469 *distance = adapter->offset_distance;
1471 return adapter->offset;
1475 * gst_adapter_prev_pts:
1476 * @adapter: a #GstAdapter
1477 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1479 * Get the pts that was before the current byte in the adapter. When
1480 * @distance is given, the amount of bytes between the pts and the current
1481 * position is returned.
1483 * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1484 * the adapter is first created or when it is cleared. This also means that before
1485 * the first byte with a pts is removed from the adapter, the pts
1486 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1488 * Returns: The previously seen pts.
1491 gst_adapter_prev_pts (GstAdapter * adapter, guint64 * distance)
1493 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1496 *distance = adapter->pts_distance;
1498 return adapter->pts;
1502 * gst_adapter_prev_dts:
1503 * @adapter: a #GstAdapter
1504 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1506 * Get the dts that was before the current byte in the adapter. When
1507 * @distance is given, the amount of bytes between the dts and the current
1508 * position is returned.
1510 * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1511 * the adapter is first created or when it is cleared. This also means that before
1512 * the first byte with a dts is removed from the adapter, the dts
1513 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1515 * Returns: The previously seen dts.
1518 gst_adapter_prev_dts (GstAdapter * adapter, guint64 * distance)
1520 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1523 *distance = adapter->dts_distance;
1525 return adapter->dts;
1529 * gst_adapter_prev_pts_at_offset:
1530 * @adapter: a #GstAdapter
1531 * @offset: the offset in the adapter at which to get timestamp
1532 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1534 * Get the pts that was before the byte at offset @offset in the adapter. When
1535 * @distance is given, the amount of bytes between the pts and the current
1536 * position is returned.
1538 * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1539 * the adapter is first created or when it is cleared. This also means that before
1540 * the first byte with a pts is removed from the adapter, the pts
1541 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1544 * Returns: The previously seen pts at given offset.
1547 gst_adapter_prev_pts_at_offset (GstAdapter * adapter, gsize offset,
1551 gsize read_offset = 0;
1552 gsize pts_offset = 0;
1553 GstClockTime pts = adapter->pts;
1556 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1559 len = gst_queue_array_get_length (adapter->bufqueue);
1561 while (idx < len && read_offset < offset + adapter->skip) {
1562 cur = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
1564 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (cur))) {
1565 pts = GST_BUFFER_PTS (cur);
1566 pts_offset = read_offset;
1569 read_offset += gst_buffer_get_size (cur);
1573 *distance = adapter->pts_distance + offset - pts_offset;
1579 * gst_adapter_prev_dts_at_offset:
1580 * @adapter: a #GstAdapter
1581 * @offset: the offset in the adapter at which to get timestamp
1582 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1584 * Get the dts that was before the byte at offset @offset in the adapter. When
1585 * @distance is given, the amount of bytes between the dts and the current
1586 * position is returned.
1588 * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1589 * the adapter is first created or when it is cleared. This also means that before
1590 * the first byte with a dts is removed from the adapter, the dts
1591 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1594 * Returns: The previously seen dts at given offset.
1597 gst_adapter_prev_dts_at_offset (GstAdapter * adapter, gsize offset,
1601 gsize read_offset = 0;
1602 gsize dts_offset = 0;
1603 GstClockTime dts = adapter->dts;
1606 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1609 len = gst_queue_array_get_length (adapter->bufqueue);
1611 while (idx < len && read_offset < offset + adapter->skip) {
1612 cur = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
1614 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cur))) {
1615 dts = GST_BUFFER_DTS (cur);
1616 dts_offset = read_offset;
1619 read_offset += gst_buffer_get_size (cur);
1623 *distance = adapter->dts_distance + offset - dts_offset;
1629 * gst_adapter_masked_scan_uint32_peek:
1630 * @adapter: a #GstAdapter
1631 * @mask: mask to apply to data before matching against @pattern
1632 * @pattern: pattern to match (after mask is applied)
1633 * @offset: offset into the adapter data from which to start scanning, returns
1634 * the last scanned position.
1635 * @size: number of bytes to scan from offset
1636 * @value: (out) (allow-none): pointer to uint32 to return matching data
1638 * Scan for pattern @pattern with applied mask @mask in the adapter data,
1639 * starting from offset @offset. If a match is found, the value that matched
1640 * is returned through @value, otherwise @value is left untouched.
1642 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1643 * of endianness. All four bytes of the pattern must be present in the
1644 * adapter for it to match, even if the first or last bytes are masked out.
1646 * It is an error to call this function without making sure that there is
1647 * enough data (offset+size bytes) in the adapter.
1649 * Returns: offset of the first match, or -1 if no match was found.
1652 gst_adapter_masked_scan_uint32_peek (GstAdapter * adapter, guint32 mask,
1653 guint32 pattern, gsize offset, gsize size, guint32 * value)
1655 gsize skip, bsize, i;
1662 g_return_val_if_fail (size > 0, -1);
1663 g_return_val_if_fail (offset + size <= adapter->size, -1);
1664 g_return_val_if_fail (((~mask) & pattern) == 0, -1);
1666 /* we can't find the pattern with less than 4 bytes */
1667 if (G_UNLIKELY (size < 4))
1670 skip = offset + adapter->skip;
1672 /* first step, do skipping and position on the first buffer */
1673 /* optimistically assume scanning continues sequentially */
1674 if (adapter->scan_entry_idx != G_MAXUINT && (adapter->scan_offset <= skip)) {
1675 idx = adapter->scan_entry_idx;
1676 skip -= adapter->scan_offset;
1679 adapter->scan_offset = 0;
1680 adapter->scan_entry_idx = G_MAXUINT;
1682 buf = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
1683 bsize = gst_buffer_get_size (buf);
1684 while (G_UNLIKELY (skip >= bsize)) {
1686 adapter->scan_offset += bsize;
1687 adapter->scan_entry_idx = idx;
1688 buf = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
1689 bsize = gst_buffer_get_size (buf);
1691 /* get the data now */
1692 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1695 bdata = (guint8 *) info.data + skip;
1696 bsize = info.size - skip;
1699 /* set the state to something that does not match */
1704 bsize = MIN (bsize, size);
1705 for (i = 0; i < bsize; i++) {
1706 state = ((state << 8) | bdata[i]);
1707 if (G_UNLIKELY ((state & mask) == pattern)) {
1708 /* we have a match but we need to have skipped at
1709 * least 4 bytes to fill the state. */
1710 if (G_LIKELY (skip + i >= 3)) {
1711 if (G_LIKELY (value))
1713 gst_buffer_unmap (buf, &info);
1714 return offset + skip + i - 3;
1722 /* nothing found yet, go to next buffer */
1724 adapter->scan_offset += info.size;
1725 adapter->scan_entry_idx = idx;
1726 gst_buffer_unmap (buf, &info);
1727 buf = gst_queue_array_peek_nth (adapter->bufqueue, idx++);
1729 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1736 gst_buffer_unmap (buf, &info);
1743 * gst_adapter_masked_scan_uint32:
1744 * @adapter: a #GstAdapter
1745 * @mask: mask to apply to data before matching against @pattern
1746 * @pattern: pattern to match (after mask is applied)
1747 * @offset: offset into the adapter data from which to start scanning, returns
1748 * the last scanned position.
1749 * @size: number of bytes to scan from offset
1751 * Scan for pattern @pattern with applied mask @mask in the adapter data,
1752 * starting from offset @offset.
1754 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1755 * of endianness. All four bytes of the pattern must be present in the
1756 * adapter for it to match, even if the first or last bytes are masked out.
1758 * It is an error to call this function without making sure that there is
1759 * enough data (offset+size bytes) in the adapter.
1761 * This function calls gst_adapter_masked_scan_uint32_peek() passing %NULL
1764 * Returns: offset of the first match, or -1 if no match was found.
1768 * // Assume the adapter contains 0x00 0x01 0x02 ... 0xfe 0xff
1770 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 0, 256);
1772 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 1, 255);
1774 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x01020304, 1, 255);
1776 * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0001, 0, 256);
1778 * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0203, 0, 256);
1780 * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 256);
1782 * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 4);
1787 gst_adapter_masked_scan_uint32 (GstAdapter * adapter, guint32 mask,
1788 guint32 pattern, gsize offset, gsize size)
1790 return gst_adapter_masked_scan_uint32_peek (adapter, mask, pattern, offset,