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.
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, GstObject *parent, GstBuffer *buffer)
51 * GstAdapter *adapter;
52 * GstFlowReturn ret = GST_FLOW_OK;
54 * this = MY_ELEMENT (parent);
56 * adapter = this->adapter;
58 * // put buffer into adapter
59 * gst_adapter_push (adapter, buffer);
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);
73 * For another example, a simple element inside GStreamer that uses #GstAdapter
74 * is the libvisual element.
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.
81 * Also check the GST_BUFFER_FLAG_DISCONT flag on the buffer. Some elements might
82 * need to clear the adapter after a discontinuity.
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.
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.
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.
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.
114 #include <gst/gst_private.h>
115 #include "gstadapter.h"
118 /* default size for the assembled data buffer */
119 #define DEFAULT_SIZE 4096
121 static void gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush);
123 GST_DEBUG_CATEGORY_STATIC (gst_adapter_debug);
124 #define GST_CAT_DEFAULT gst_adapter_debug
136 /* we keep state of assembled pieces */
137 gpointer assembled_data;
138 gsize assembled_size;
142 guint64 pts_distance;
144 guint64 dts_distance;
152 struct _GstAdapterClass
154 GObjectClass parent_class;
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);
162 static void gst_adapter_dispose (GObject * object);
163 static void gst_adapter_finalize (GObject * object);
166 gst_adapter_class_init (GstAdapterClass * klass)
168 GObjectClass *object = G_OBJECT_CLASS (klass);
170 object->dispose = gst_adapter_dispose;
171 object->finalize = gst_adapter_finalize;
175 gst_adapter_init (GstAdapter * adapter)
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;
186 gst_adapter_dispose (GObject * object)
188 GstAdapter *adapter = GST_ADAPTER (object);
190 gst_adapter_clear (adapter);
192 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
196 gst_adapter_finalize (GObject * object)
198 GstAdapter *adapter = GST_ADAPTER (object);
200 g_free (adapter->assembled_data);
202 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
208 * Creates a new #GstAdapter. Free with g_object_unref().
210 * Returns: (transfer full): a new #GstAdapter
213 gst_adapter_new (void)
215 return g_object_newv (GST_TYPE_ADAPTER, 0, NULL);
220 * @adapter: a #GstAdapter
222 * Removes all buffers from @adapter.
225 gst_adapter_clear (GstAdapter * adapter)
227 g_return_if_fail (GST_IS_ADAPTER (adapter));
229 if (adapter->info.memory)
230 gst_adapter_unmap (adapter);
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;
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;
248 update_timestamps (GstAdapter * adapter, GstBuffer * buf)
250 GstClockTime pts, dts;
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));
256 adapter->pts_distance = 0;
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));
262 adapter->dts_distance = 0;
266 /* copy data into @dest, skipping @skip bytes from the head buffers */
268 copy_into_unchecked (GstAdapter * adapter, guint8 * dest, gsize skip,
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;
281 g = adapter->buflist;
284 bsize = gst_buffer_get_size (buf);
285 while (G_UNLIKELY (skip >= bsize)) {
287 g = g_slist_next (g);
289 bsize = gst_buffer_get_size (buf);
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
297 gst_buffer_extract (buf, skip, dest, csize);
301 /* second step, copy remainder */
303 g = g_slist_next (g);
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);
319 * @adapter: a #GstAdapter
320 * @buf: (transfer full): a #GstBuffer to add to queue in the adapter
322 * Adds the data from @buf to the data stored inside @adapter and takes
323 * ownership of the buffer.
326 gst_adapter_push (GstAdapter * adapter, GstBuffer * buf)
330 g_return_if_fail (GST_IS_ADAPTER (adapter));
331 g_return_if_fail (GST_IS_BUFFER (buf));
333 size = gst_buffer_get_size (buf);
334 adapter->size += size;
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",
340 adapter->buflist = adapter->buflist_end = g_slist_append (NULL, buf);
341 update_timestamps (adapter, buf);
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);
352 /* Internal method only. Tries to merge buffers at the head of the queue
353 * to form a single larger buffer of size 'size'.
355 * Returns %TRUE if it managed to merge anything.
358 gst_adapter_try_to_merge_up (GstAdapter * adapter, gsize size)
360 GstBuffer *cur, *head;
362 gboolean ret = FALSE;
365 g = adapter->buflist;
371 hsize = gst_buffer_get_size (head);
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;
380 g = g_slist_next (g);
382 while (g != NULL && hsize < size) {
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);
389 head = gst_buffer_append (head, cur);
390 hsize = gst_buffer_get_size (head);
393 /* Delete the front list item, and store our new buffer in the 2nd list
395 adapter->buflist = g_slist_delete_link (adapter->buflist, adapter->buflist);
398 /* invalidate scan position */
399 adapter->scan_offset = 0;
400 adapter->scan_entry = NULL;
402 g = g_slist_next (g);
411 * @adapter: a #GstAdapter
412 * @size: the number of bytes to map/peek
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.
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().
426 * Returns %NULL if @size bytes are not available.
428 * Returns: (transfer none) (array length=size) (element-type guint8) (nullable):
429 * a pointer to the first @size bytes of data, or %NULL
432 gst_adapter_map (GstAdapter * adapter, gsize size)
436 gsize toreuse, tocopy;
439 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
440 g_return_val_if_fail (size > 0, NULL);
442 if (adapter->info.memory)
443 gst_adapter_unmap (adapter);
445 /* we don't have enough data, return NULL. This is unlikely
446 * as one usually does an _available() first instead of peeking a
448 if (G_UNLIKELY (size > adapter->size))
451 /* we have enough assembled data, return it */
452 if (adapter->assembled_len >= size)
453 return adapter->assembled_data;
458 cur = adapter->buflist->data;
459 skip = adapter->skip;
461 csize = gst_buffer_get_size (cur);
462 if (csize >= size + skip) {
463 if (!gst_buffer_map (cur, &adapter->info, GST_MAP_READ))
466 return (guint8 *) adapter->info.data + skip;
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 */
471 } while (gst_adapter_try_to_merge_up (adapter, size));
474 /* see how much data we can reuse from the assembled memory and how much
476 toreuse = adapter->assembled_len;
477 tocopy = size - toreuse;
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);
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);
491 /* we are going to reuse all data, realloc then */
492 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "reusing %" G_GSIZE_FORMAT " bytes",
494 adapter->assembled_data =
495 g_realloc (adapter->assembled_data, adapter->assembled_size);
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;
504 return adapter->assembled_data;
509 * @adapter: a #GstAdapter
511 * Releases the memory obtained with the last gst_adapter_map().
514 gst_adapter_unmap (GstAdapter * adapter)
516 g_return_if_fail (GST_IS_ADAPTER (adapter));
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;
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
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.
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.
542 gst_adapter_copy (GstAdapter * adapter, gpointer dest, gsize offset, gsize size)
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);
548 copy_into_unchecked (adapter, dest, offset + adapter->skip, size);
552 * gst_adapter_copy_bytes: (rename-to gst_adapter_copy)
553 * @adapter: a #GstAdapter
554 * @offset: the bytes offset in the adapter to start from
555 * @size: the number of bytes to copy
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.
562 * Returns: (transfer full): A new #GBytes structure containing the copied data.
567 gst_adapter_copy_bytes (GstAdapter * adapter, gsize offset, gsize size)
570 data = g_malloc (size);
571 gst_adapter_copy (adapter, data, offset, size);
572 return g_bytes_new_take (data, size);
575 /*Flushes the first @flush bytes in the @adapter*/
577 gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush)
583 GST_LOG_OBJECT (adapter, "flushing %" G_GSIZE_FORMAT " bytes", flush);
585 if (adapter->info.memory)
586 gst_adapter_unmap (adapter);
589 adapter->size -= flush;
590 adapter->assembled_len = 0;
592 /* take skip into account */
593 flush += adapter->skip;
594 /* distance is always at least the amount of skipped bytes */
595 adapter->pts_distance -= adapter->skip;
596 adapter->dts_distance -= adapter->skip;
598 g = adapter->buflist;
600 size = gst_buffer_get_size (cur);
601 while (flush >= size) {
602 /* can skip whole buffer */
603 GST_LOG_OBJECT (adapter, "flushing out head buffer");
604 adapter->pts_distance += size;
605 adapter->dts_distance += size;
608 gst_buffer_unref (cur);
609 g = g_slist_delete_link (g, g);
611 if (G_UNLIKELY (g == NULL)) {
612 GST_LOG_OBJECT (adapter, "adapter empty now");
613 adapter->buflist_end = NULL;
616 /* there is a new head buffer, update the timestamps */
618 update_timestamps (adapter, cur);
619 size = gst_buffer_get_size (cur);
621 adapter->buflist = g;
622 /* account for the remaining bytes */
623 adapter->skip = flush;
624 adapter->pts_distance += flush;
625 adapter->dts_distance += flush;
626 /* invalidate scan position */
627 adapter->scan_offset = 0;
628 adapter->scan_entry = NULL;
633 * @adapter: a #GstAdapter
634 * @flush: the number of bytes to flush
636 * Flushes the first @flush bytes in the @adapter. The caller must ensure that
637 * at least this many bytes are available.
639 * See also: gst_adapter_map(), gst_adapter_unmap()
642 gst_adapter_flush (GstAdapter * adapter, gsize flush)
644 g_return_if_fail (GST_IS_ADAPTER (adapter));
645 g_return_if_fail (flush <= adapter->size);
647 /* flushing out 0 bytes will do nothing */
648 if (G_UNLIKELY (flush == 0))
651 gst_adapter_flush_unchecked (adapter, flush);
654 /* internal function, nbytes should be flushed after calling this function */
656 gst_adapter_take_internal (GstAdapter * adapter, gsize nbytes)
659 gsize toreuse, tocopy;
661 /* see how much data we can reuse from the assembled memory and how much
663 toreuse = MIN (nbytes, adapter->assembled_len);
664 tocopy = nbytes - toreuse;
666 /* find memory to return */
667 if (adapter->assembled_size >= nbytes && toreuse > 0) {
668 /* we reuse already allocated memory but only when we're going to reuse
669 * something from it because else we are worse than the malloc and copy
671 GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes of assembled"
673 /* we have enough free space in the assembled array */
674 data = adapter->assembled_data;
675 /* flush after this function should set the assembled_size to 0 */
676 adapter->assembled_data = g_malloc (adapter->assembled_size);
678 GST_LOG_OBJECT (adapter, "allocating %" G_GSIZE_FORMAT " bytes", nbytes);
679 /* not enough bytes in the assembled array, just allocate new space */
680 data = g_malloc (nbytes);
681 /* reuse what we can from the already assembled data */
683 GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes", toreuse);
684 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
685 "memcpy %" G_GSIZE_FORMAT " bytes", toreuse);
686 memcpy (data, adapter->assembled_data, toreuse);
690 /* copy the remaining data */
691 copy_into_unchecked (adapter, toreuse + data, toreuse + adapter->skip,
699 * @adapter: a #GstAdapter
700 * @nbytes: the number of bytes to take
702 * Returns a freshly allocated buffer containing the first @nbytes bytes of the
703 * @adapter. The returned bytes will be flushed from the adapter.
705 * Caller owns returned value. g_free after usage.
707 * Free-function: g_free
709 * Returns: (transfer full) (array length=nbytes) (element-type guint8) (nullable):
710 * oven-fresh hot data, or %NULL if @nbytes bytes are not available
713 gst_adapter_take (GstAdapter * adapter, gsize nbytes)
717 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
718 g_return_val_if_fail (nbytes > 0, NULL);
720 /* we don't have enough data, return NULL. This is unlikely
721 * as one usually does an _available() first instead of peeking a
723 if (G_UNLIKELY (nbytes > adapter->size))
726 data = gst_adapter_take_internal (adapter, nbytes);
728 gst_adapter_flush_unchecked (adapter, nbytes);
734 * gst_adapter_take_buffer_fast:
735 * @adapter: a #GstAdapter
736 * @nbytes: the number of bytes to take
738 * Returns a #GstBuffer containing the first @nbytes of the @adapter.
739 * The returned bytes will be flushed from the adapter. This function
740 * is potentially more performant than gst_adapter_take_buffer() since
741 * it can reuse the memory in pushed buffers by subbuffering or
742 * merging. Unlike gst_adapter_take_buffer(), the returned buffer may
743 * be composed of multiple non-contiguous #GstMemory objects, no
746 * Note that no assumptions should be made as to whether certain buffer
747 * flags such as the DISCONT flag are set on the returned buffer, or not.
748 * The caller needs to explicitly set or unset flags that should be set or
751 * This function can return buffer up to the return value of
752 * gst_adapter_available() without making copies if possible.
754 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
757 * Free-function: gst_buffer_unref
759 * Returns: (transfer full) (nullable): a #GstBuffer containing the first
760 * @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
761 * gst_buffer_unref() when no longer needed.
767 gst_adapter_take_buffer_fast (GstAdapter * adapter, gsize nbytes)
769 GstBuffer *buffer = NULL;
775 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
776 g_return_val_if_fail (nbytes > 0, NULL);
778 GST_LOG_OBJECT (adapter, "taking buffer of %" G_GSIZE_FORMAT " bytes",
781 /* we don't have enough data, return NULL. This is unlikely
782 * as one usually does an _available() first instead of grabbing a
784 if (G_UNLIKELY (nbytes > adapter->size))
787 skip = adapter->skip;
788 cur = adapter->buflist->data;
790 if (skip == 0 && gst_buffer_get_size (cur) == nbytes) {
791 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
792 " as head buffer", nbytes);
793 buffer = gst_buffer_ref (cur);
797 for (item = adapter->buflist; item && left > 0; item = item->next) {
798 gsize size, cur_size;
801 cur_size = gst_buffer_get_size (cur);
802 size = MIN (cur_size - skip, left);
804 GST_LOG_OBJECT (adapter, "appending %" G_GSIZE_FORMAT " bytes"
805 " via region copy", size);
807 gst_buffer_copy_into (buffer, cur, GST_BUFFER_COPY_MEMORY, skip, size);
809 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, size);
815 gst_adapter_flush_unchecked (adapter, nbytes);
821 * gst_adapter_take_buffer:
822 * @adapter: a #GstAdapter
823 * @nbytes: the number of bytes to take
825 * Returns a #GstBuffer containing the first @nbytes bytes of the
826 * @adapter. The returned bytes will be flushed from the adapter.
827 * This function is potentially more performant than
828 * gst_adapter_take() since it can reuse the memory in pushed buffers
829 * by subbuffering or merging. This function will always return a
830 * buffer with a single memory region.
832 * Note that no assumptions should be made as to whether certain buffer
833 * flags such as the DISCONT flag are set on the returned buffer, or not.
834 * The caller needs to explicitly set or unset flags that should be set or
837 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
840 * Free-function: gst_buffer_unref
842 * Returns: (transfer full) (nullable): a #GstBuffer containing the first
843 * @nbytes of the adapter, or %NULL if @nbytes bytes are not available.
844 * gst_buffer_unref() when no longer needed.
847 gst_adapter_take_buffer (GstAdapter * adapter, gsize nbytes)
854 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
855 g_return_val_if_fail (nbytes > 0, NULL);
857 GST_LOG_OBJECT (adapter, "taking buffer of %" G_GSIZE_FORMAT " bytes",
860 /* we don't have enough data, return NULL. This is unlikely
861 * as one usually does an _available() first instead of grabbing a
863 if (G_UNLIKELY (nbytes > adapter->size))
866 cur = adapter->buflist->data;
867 skip = adapter->skip;
868 hsize = gst_buffer_get_size (cur);
870 /* our head buffer has enough data left, return it */
871 if (skip == 0 && hsize == nbytes) {
872 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
873 " as head buffer", nbytes);
874 buffer = gst_buffer_ref (cur);
876 } else if (hsize >= nbytes + skip) {
877 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
878 " via region copy", nbytes);
879 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
883 if (gst_adapter_try_to_merge_up (adapter, nbytes)) {
884 /* Merged something, let's try again for sub-buffering */
885 cur = adapter->buflist->data;
886 skip = adapter->skip;
887 if (gst_buffer_get_size (cur) >= nbytes + skip) {
888 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
889 " via sub-buffer", nbytes);
890 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
896 data = gst_adapter_take_internal (adapter, nbytes);
898 buffer = gst_buffer_new_wrapped (data, nbytes);
901 gst_adapter_flush_unchecked (adapter, nbytes);
907 * gst_adapter_take_list:
908 * @adapter: a #GstAdapter
909 * @nbytes: the number of bytes to take
911 * Returns a #GList of buffers containing the first @nbytes bytes of the
912 * @adapter. The returned bytes will be flushed from the adapter.
913 * When the caller can deal with individual buffers, this function is more
914 * performant because no memory should be copied.
916 * Caller owns returned list and contained buffers. gst_buffer_unref() each
917 * buffer in the list before freeing the list after usage.
919 * Returns: (element-type Gst.Buffer) (transfer full) (nullable): a #GList of
920 * buffers containing the first @nbytes of the adapter, or %NULL if @nbytes
921 * bytes are not available
924 gst_adapter_take_list (GstAdapter * adapter, gsize nbytes)
926 GQueue queue = G_QUEUE_INIT;
928 gsize hsize, skip, cur_size;
930 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
931 g_return_val_if_fail (nbytes <= adapter->size, NULL);
933 GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes);
936 cur = adapter->buflist->data;
937 skip = adapter->skip;
938 cur_size = gst_buffer_get_size (cur);
939 hsize = MIN (nbytes, cur_size - skip);
941 cur = gst_adapter_take_buffer (adapter, hsize);
943 g_queue_push_tail (&queue, cur);
951 * gst_adapter_take_buffer_list:
952 * @adapter: a #GstAdapter
953 * @nbytes: the number of bytes to take
955 * Returns a #GstBufferList of buffers containing the first @nbytes bytes of
956 * the @adapter. The returned bytes will be flushed from the adapter.
957 * When the caller can deal with individual buffers, this function is more
958 * performant because no memory should be copied.
960 * Caller owns the returned list. Call gst_buffer_list_unref() to free
961 * the list after usage.
963 * Returns: (transfer full) (nullable): a #GstBufferList of buffers containing
964 * the first @nbytes of the adapter, or %NULL if @nbytes bytes are not
970 gst_adapter_take_buffer_list (GstAdapter * adapter, gsize nbytes)
972 GstBufferList *buffer_list;
974 gsize hsize, skip, cur_size;
976 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
978 if (nbytes > adapter->size)
981 GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes);
983 buffer_list = gst_buffer_list_new ();
985 cur = adapter->buflist->data;
986 skip = adapter->skip;
987 cur_size = gst_buffer_get_size (cur);
988 hsize = MIN (nbytes, cur_size - skip);
990 gst_buffer_list_add (buffer_list, gst_adapter_take_buffer (adapter, hsize));
997 * gst_adapter_available:
998 * @adapter: a #GstAdapter
1000 * Gets the maximum amount of bytes available, that is it returns the maximum
1001 * value that can be supplied to gst_adapter_map() without that function
1004 * Returns: number of bytes available in @adapter
1007 gst_adapter_available (GstAdapter * adapter)
1009 g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
1011 return adapter->size;
1015 * gst_adapter_available_fast:
1016 * @adapter: a #GstAdapter
1018 * Gets the maximum number of bytes that are immediately available without
1019 * requiring any expensive operations (like copying the data into a
1020 * temporary buffer).
1022 * Returns: number of bytes that are available in @adapter without expensive
1026 gst_adapter_available_fast (GstAdapter * adapter)
1032 g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
1035 if (adapter->size == 0)
1038 /* some stuff we already assembled */
1039 if (adapter->assembled_len)
1040 return adapter->assembled_len;
1042 /* take the first non-zero buffer */
1043 g = adapter->buflist;
1046 size = gst_buffer_get_size (cur);
1049 g = g_slist_next (g);
1052 /* we can quickly get the (remaining) data of the first buffer */
1053 return size - adapter->skip;
1057 * gst_adapter_prev_pts:
1058 * @adapter: a #GstAdapter
1059 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1061 * Get the pts that was before the current byte in the adapter. When
1062 * @distance is given, the amount of bytes between the pts and the current
1063 * position is returned.
1065 * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1066 * the adapter is first created or when it is cleared. This also means that before
1067 * the first byte with a pts is removed from the adapter, the pts
1068 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1070 * Returns: The previously seen pts.
1073 gst_adapter_prev_pts (GstAdapter * adapter, guint64 * distance)
1075 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1078 *distance = adapter->pts_distance;
1080 return adapter->pts;
1084 * gst_adapter_prev_dts:
1085 * @adapter: a #GstAdapter
1086 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1088 * Get the dts that was before the current byte in the adapter. When
1089 * @distance is given, the amount of bytes between the dts and the current
1090 * position is returned.
1092 * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1093 * the adapter is first created or when it is cleared. This also means that before
1094 * the first byte with a dts is removed from the adapter, the dts
1095 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1097 * Returns: The previously seen dts.
1100 gst_adapter_prev_dts (GstAdapter * adapter, guint64 * distance)
1102 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1105 *distance = adapter->dts_distance;
1107 return adapter->dts;
1111 * gst_adapter_prev_pts_at_offset:
1112 * @adapter: a #GstAdapter
1113 * @offset: the offset in the adapter at which to get timestamp
1114 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1116 * Get the pts that was before the byte at offset @offset in the adapter. When
1117 * @distance is given, the amount of bytes between the pts and the current
1118 * position is returned.
1120 * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1121 * the adapter is first created or when it is cleared. This also means that before
1122 * the first byte with a pts is removed from the adapter, the pts
1123 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1126 * Returns: The previously seen pts at given offset.
1129 gst_adapter_prev_pts_at_offset (GstAdapter * adapter, gsize offset,
1134 gsize read_offset = 0;
1135 GstClockTime pts = adapter->pts;
1137 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1139 g = adapter->buflist;
1141 while (g && read_offset < offset + adapter->skip) {
1144 read_offset += gst_buffer_get_size (cur);
1145 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (cur))) {
1146 pts = GST_BUFFER_PTS (cur);
1149 g = g_slist_next (g);
1153 *distance = adapter->dts_distance + offset;
1159 * gst_adapter_prev_dts_at_offset:
1160 * @adapter: a #GstAdapter
1161 * @offset: the offset in the adapter at which to get timestamp
1162 * @distance: (out) (allow-none): pointer to location for distance, or %NULL
1164 * Get the dts that was before the byte at offset @offset in the adapter. When
1165 * @distance is given, the amount of bytes between the dts and the current
1166 * position is returned.
1168 * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1169 * the adapter is first created or when it is cleared. This also means that before
1170 * the first byte with a dts is removed from the adapter, the dts
1171 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1174 * Returns: The previously seen dts at given offset.
1177 gst_adapter_prev_dts_at_offset (GstAdapter * adapter, gsize offset,
1182 gsize read_offset = 0;
1183 GstClockTime dts = adapter->dts;
1185 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1187 g = adapter->buflist;
1189 while (g && read_offset < offset + adapter->skip) {
1192 read_offset += gst_buffer_get_size (cur);
1193 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cur))) {
1194 dts = GST_BUFFER_DTS (cur);
1197 g = g_slist_next (g);
1201 *distance = adapter->dts_distance + offset;
1207 * gst_adapter_masked_scan_uint32_peek:
1208 * @adapter: a #GstAdapter
1209 * @mask: mask to apply to data before matching against @pattern
1210 * @pattern: pattern to match (after mask is applied)
1211 * @offset: offset into the adapter data from which to start scanning, returns
1212 * the last scanned position.
1213 * @size: number of bytes to scan from offset
1214 * @value: (out) (allow-none): pointer to uint32 to return matching data
1216 * Scan for pattern @pattern with applied mask @mask in the adapter data,
1217 * starting from offset @offset. If a match is found, the value that matched
1218 * is returned through @value, otherwise @value is left untouched.
1220 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1221 * of endianness. All four bytes of the pattern must be present in the
1222 * adapter for it to match, even if the first or last bytes are masked out.
1224 * It is an error to call this function without making sure that there is
1225 * enough data (offset+size bytes) in the adapter.
1227 * Returns: offset of the first match, or -1 if no match was found.
1230 gst_adapter_masked_scan_uint32_peek (GstAdapter * adapter, guint32 mask,
1231 guint32 pattern, gsize offset, gsize size, guint32 * value)
1234 gsize skip, bsize, i;
1240 g_return_val_if_fail (size > 0, -1);
1241 g_return_val_if_fail (offset + size <= adapter->size, -1);
1242 g_return_val_if_fail (((~mask) & pattern) == 0, -1);
1244 /* we can't find the pattern with less than 4 bytes */
1245 if (G_UNLIKELY (size < 4))
1248 skip = offset + adapter->skip;
1250 /* first step, do skipping and position on the first buffer */
1251 /* optimistically assume scanning continues sequentially */
1252 if (adapter->scan_entry && (adapter->scan_offset <= skip)) {
1253 g = adapter->scan_entry;
1254 skip -= adapter->scan_offset;
1256 g = adapter->buflist;
1257 adapter->scan_offset = 0;
1258 adapter->scan_entry = NULL;
1261 bsize = gst_buffer_get_size (buf);
1262 while (G_UNLIKELY (skip >= bsize)) {
1264 g = g_slist_next (g);
1265 adapter->scan_offset += bsize;
1266 adapter->scan_entry = g;
1268 bsize = gst_buffer_get_size (buf);
1270 /* get the data now */
1271 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1274 bdata = (guint8 *) info.data + skip;
1275 bsize = info.size - skip;
1278 /* set the state to something that does not match */
1283 bsize = MIN (bsize, size);
1284 for (i = 0; i < bsize; i++) {
1285 state = ((state << 8) | bdata[i]);
1286 if (G_UNLIKELY ((state & mask) == pattern)) {
1287 /* we have a match but we need to have skipped at
1288 * least 4 bytes to fill the state. */
1289 if (G_LIKELY (skip + i >= 3)) {
1290 if (G_LIKELY (value))
1292 gst_buffer_unmap (buf, &info);
1293 return offset + skip + i - 3;
1301 /* nothing found yet, go to next buffer */
1303 g = g_slist_next (g);
1304 adapter->scan_offset += info.size;
1305 adapter->scan_entry = g;
1306 gst_buffer_unmap (buf, &info);
1309 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1316 gst_buffer_unmap (buf, &info);
1323 * gst_adapter_masked_scan_uint32:
1324 * @adapter: a #GstAdapter
1325 * @mask: mask to apply to data before matching against @pattern
1326 * @pattern: pattern to match (after mask is applied)
1327 * @offset: offset into the adapter data from which to start scanning, returns
1328 * the last scanned position.
1329 * @size: number of bytes to scan from offset
1331 * Scan for pattern @pattern with applied mask @mask in the adapter data,
1332 * starting from offset @offset.
1334 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1335 * of endianness. All four bytes of the pattern must be present in the
1336 * adapter for it to match, even if the first or last bytes are masked out.
1338 * It is an error to call this function without making sure that there is
1339 * enough data (offset+size bytes) in the adapter.
1341 * This function calls gst_adapter_masked_scan_uint32_peek() passing %NULL
1344 * Returns: offset of the first match, or -1 if no match was found.
1348 * // Assume the adapter contains 0x00 0x01 0x02 ... 0xfe 0xff
1350 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 0, 256);
1352 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 1, 255);
1354 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x01020304, 1, 255);
1356 * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0001, 0, 256);
1358 * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0203, 0, 256);
1360 * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 256);
1362 * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 4);
1367 gst_adapter_masked_scan_uint32 (GstAdapter * adapter, guint32 mask,
1368 guint32 pattern, gsize offset, gsize size)
1370 return gst_adapter_masked_scan_uint32_peek (adapter, mask, pattern, offset,