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.
113 * Last reviewed on 2009-05-13 (0.10.24).
116 #include <gst/gst_private.h>
117 #include "gstadapter.h"
120 /* default size for the assembled data buffer */
121 #define DEFAULT_SIZE 4096
123 static void gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush);
125 GST_DEBUG_CATEGORY_STATIC (gst_adapter_debug);
126 #define GST_CAT_DEFAULT gst_adapter_debug
138 /* we keep state of assembled pieces */
139 gpointer assembled_data;
140 gsize assembled_size;
144 guint64 pts_distance;
146 guint64 dts_distance;
154 struct _GstAdapterClass
156 GObjectClass parent_class;
160 GST_DEBUG_CATEGORY_INIT (gst_adapter_debug, "adapter", 0, "object to splice and merge buffers to desired size")
161 #define gst_adapter_parent_class parent_class
162 G_DEFINE_TYPE_WITH_CODE (GstAdapter, gst_adapter, G_TYPE_OBJECT, _do_init);
164 static void gst_adapter_dispose (GObject * object);
165 static void gst_adapter_finalize (GObject * object);
168 gst_adapter_class_init (GstAdapterClass * klass)
170 GObjectClass *object = G_OBJECT_CLASS (klass);
172 object->dispose = gst_adapter_dispose;
173 object->finalize = gst_adapter_finalize;
177 gst_adapter_init (GstAdapter * adapter)
179 adapter->assembled_data = g_malloc (DEFAULT_SIZE);
180 adapter->assembled_size = DEFAULT_SIZE;
181 adapter->pts = GST_CLOCK_TIME_NONE;
182 adapter->pts_distance = 0;
183 adapter->dts = GST_CLOCK_TIME_NONE;
184 adapter->dts_distance = 0;
188 gst_adapter_dispose (GObject * object)
190 GstAdapter *adapter = GST_ADAPTER (object);
192 gst_adapter_clear (adapter);
194 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
198 gst_adapter_finalize (GObject * object)
200 GstAdapter *adapter = GST_ADAPTER (object);
202 g_free (adapter->assembled_data);
204 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
210 * Creates a new #GstAdapter. Free with g_object_unref().
212 * Returns: (transfer full): a new #GstAdapter
215 gst_adapter_new (void)
217 return g_object_newv (GST_TYPE_ADAPTER, 0, NULL);
222 * @adapter: a #GstAdapter
224 * Removes all buffers from @adapter.
227 gst_adapter_clear (GstAdapter * adapter)
229 g_return_if_fail (GST_IS_ADAPTER (adapter));
231 if (adapter->info.memory)
232 gst_adapter_unmap (adapter);
234 g_slist_foreach (adapter->buflist, (GFunc) gst_mini_object_unref, NULL);
235 g_slist_free (adapter->buflist);
236 adapter->buflist = NULL;
237 adapter->buflist_end = NULL;
240 adapter->assembled_len = 0;
241 adapter->pts = GST_CLOCK_TIME_NONE;
242 adapter->pts_distance = 0;
243 adapter->dts = GST_CLOCK_TIME_NONE;
244 adapter->dts_distance = 0;
245 adapter->scan_offset = 0;
246 adapter->scan_entry = NULL;
250 update_timestamps (GstAdapter * adapter, GstBuffer * buf)
252 GstClockTime pts, dts;
254 pts = GST_BUFFER_PTS (buf);
255 if (GST_CLOCK_TIME_IS_VALID (pts)) {
256 GST_LOG_OBJECT (adapter, "new pts %" GST_TIME_FORMAT, GST_TIME_ARGS (pts));
258 adapter->pts_distance = 0;
260 dts = GST_BUFFER_DTS (buf);
261 if (GST_CLOCK_TIME_IS_VALID (dts)) {
262 GST_LOG_OBJECT (adapter, "new dts %" GST_TIME_FORMAT, GST_TIME_ARGS (dts));
264 adapter->dts_distance = 0;
268 /* copy data into @dest, skipping @skip bytes from the head buffers */
270 copy_into_unchecked (GstAdapter * adapter, guint8 * dest, gsize skip,
277 /* first step, do skipping */
278 /* we might well be copying where we were scanning */
279 if (adapter->scan_entry && (adapter->scan_offset <= skip)) {
280 g = adapter->scan_entry;
281 skip -= adapter->scan_offset;
283 g = adapter->buflist;
286 bsize = gst_buffer_get_size (buf);
287 while (G_UNLIKELY (skip >= bsize)) {
289 g = g_slist_next (g);
291 bsize = gst_buffer_get_size (buf);
293 /* copy partial buffer */
294 csize = MIN (bsize - skip, size);
295 GST_DEBUG ("bsize %" G_GSIZE_FORMAT ", skip %" G_GSIZE_FORMAT ", csize %"
296 G_GSIZE_FORMAT, bsize, skip, csize);
297 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter, "extract %" G_GSIZE_FORMAT
299 gst_buffer_extract (buf, skip, dest, csize);
303 /* second step, copy remainder */
305 g = g_slist_next (g);
307 bsize = gst_buffer_get_size (buf);
308 if (G_LIKELY (bsize > 0)) {
309 csize = MIN (bsize, size);
310 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
311 "extract %" G_GSIZE_FORMAT " bytes", csize);
312 gst_buffer_extract (buf, 0, dest, csize);
321 * @adapter: a #GstAdapter
322 * @buf: (transfer full): a #GstBuffer to add to queue in the adapter
324 * Adds the data from @buf to the data stored inside @adapter and takes
325 * ownership of the buffer.
328 gst_adapter_push (GstAdapter * adapter, GstBuffer * buf)
332 g_return_if_fail (GST_IS_ADAPTER (adapter));
333 g_return_if_fail (GST_IS_BUFFER (buf));
335 size = gst_buffer_get_size (buf);
336 adapter->size += size;
338 /* Note: merging buffers at this point is premature. */
339 if (G_UNLIKELY (adapter->buflist == NULL)) {
340 GST_LOG_OBJECT (adapter, "pushing %p first %" G_GSIZE_FORMAT " bytes",
342 adapter->buflist = adapter->buflist_end = g_slist_append (NULL, buf);
343 update_timestamps (adapter, buf);
345 /* Otherwise append to the end, and advance our end pointer */
346 GST_LOG_OBJECT (adapter, "pushing %p %" G_GSIZE_FORMAT " bytes at end, "
347 "size now %" G_GSIZE_FORMAT, buf, size, adapter->size);
348 adapter->buflist_end = g_slist_append (adapter->buflist_end, buf);
349 adapter->buflist_end = g_slist_next (adapter->buflist_end);
354 /* Internal method only. Tries to merge buffers at the head of the queue
355 * to form a single larger buffer of size 'size'.
357 * Returns TRUE if it managed to merge anything.
360 gst_adapter_try_to_merge_up (GstAdapter * adapter, gsize size)
362 GstBuffer *cur, *head;
364 gboolean ret = FALSE;
367 g = adapter->buflist;
373 hsize = gst_buffer_get_size (head);
375 /* Remove skipped part from the buffer (otherwise the buffer might grow indefinitely) */
376 head = gst_buffer_make_writable (head);
377 gst_buffer_resize (head, adapter->skip, hsize - adapter->skip);
378 hsize -= adapter->skip;
382 g = g_slist_next (g);
384 while (g != NULL && hsize < size) {
386 /* Merge the head buffer and the next in line */
387 GST_LOG_OBJECT (adapter, "Merging buffers of size %" G_GSIZE_FORMAT " & %"
388 G_GSIZE_FORMAT " in search of target %" G_GSIZE_FORMAT,
389 hsize, gst_buffer_get_size (cur), size);
391 head = gst_buffer_append (head, cur);
392 hsize = gst_buffer_get_size (head);
395 /* Delete the front list item, and store our new buffer in the 2nd list
397 adapter->buflist = g_slist_delete_link (adapter->buflist, adapter->buflist);
400 /* invalidate scan position */
401 adapter->scan_offset = 0;
402 adapter->scan_entry = NULL;
404 g = g_slist_next (g);
413 * @adapter: a #GstAdapter
414 * @size: the number of bytes to map/peek
416 * Gets the first @size bytes stored in the @adapter. The returned pointer is
417 * valid until the next function is called on the adapter.
419 * Note that setting the returned pointer as the data of a #GstBuffer is
420 * incorrect for general-purpose plugins. The reason is that if a downstream
421 * element stores the buffer so that it has access to it outside of the bounds
422 * of its chain function, the buffer will have an invalid data pointer after
423 * your element flushes the bytes. In that case you should use
424 * gst_adapter_take(), which returns a freshly-allocated buffer that you can set
425 * as #GstBuffer memory or the potentially more performant
426 * gst_adapter_take_buffer().
428 * Returns #NULL if @size bytes are not available.
430 * Returns: (transfer none) (array length=size) (element-type guint8):
431 * a pointer to the first @size bytes of data, or NULL
434 gst_adapter_map (GstAdapter * adapter, gsize size)
438 gsize toreuse, tocopy;
441 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
442 g_return_val_if_fail (size > 0, NULL);
444 if (adapter->info.memory)
445 gst_adapter_unmap (adapter);
447 /* we don't have enough data, return NULL. This is unlikely
448 * as one usually does an _available() first instead of peeking a
450 if (G_UNLIKELY (size > adapter->size))
453 /* we have enough assembled data, return it */
454 if (adapter->assembled_len >= size)
455 return adapter->assembled_data;
460 cur = adapter->buflist->data;
461 skip = adapter->skip;
463 csize = gst_buffer_get_size (cur);
464 if (csize >= size + skip) {
465 if (!gst_buffer_map (cur, &adapter->info, GST_MAP_READ))
468 return (guint8 *) adapter->info.data + skip;
470 /* We may be able to efficiently merge buffers in our pool to
471 * gather a big enough chunk to return it from the head buffer directly */
473 } while (gst_adapter_try_to_merge_up (adapter, size));
476 /* see how much data we can reuse from the assembled memory and how much
478 toreuse = adapter->assembled_len;
479 tocopy = size - toreuse;
481 /* Gonna need to copy stuff out */
482 if (G_UNLIKELY (adapter->assembled_size < size)) {
483 adapter->assembled_size = (size / DEFAULT_SIZE + 1) * DEFAULT_SIZE;
484 GST_DEBUG_OBJECT (adapter, "resizing internal buffer to %" G_GSIZE_FORMAT,
485 adapter->assembled_size);
487 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "alloc new buffer");
488 /* no g_realloc to avoid a memcpy that is not desired here since we are
489 * not going to reuse any data here */
490 g_free (adapter->assembled_data);
491 adapter->assembled_data = g_malloc (adapter->assembled_size);
493 /* we are going to reuse all data, realloc then */
494 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "reusing %" G_GSIZE_FORMAT " bytes",
496 adapter->assembled_data =
497 g_realloc (adapter->assembled_data, adapter->assembled_size);
500 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy remaining %" G_GSIZE_FORMAT
501 " bytes from adapter", tocopy);
502 data = adapter->assembled_data;
503 copy_into_unchecked (adapter, data + toreuse, skip + toreuse, tocopy);
504 adapter->assembled_len = size;
506 return adapter->assembled_data;
511 * @adapter: a #GstAdapter
513 * Releases the memory obtained with the last gst_adapter_map().
516 gst_adapter_unmap (GstAdapter * adapter)
518 g_return_if_fail (GST_IS_ADAPTER (adapter));
520 if (adapter->info.memory) {
521 GstBuffer *cur = adapter->buflist->data;
522 GST_LOG_OBJECT (adapter, "unmap memory buffer %p", cur);
523 gst_buffer_unmap (cur, &adapter->info);
524 adapter->info.memory = NULL;
529 * gst_adapter_copy: (skip)
530 * @adapter: a #GstAdapter
531 * @dest: (out caller-allocates) (array length=size) (element-type guint8):
532 * the memory to copy into
533 * @offset: the bytes offset in the adapter to start from
534 * @size: the number of bytes to copy
536 * Copies @size bytes of data starting at @offset out of the buffers
537 * contained in @GstAdapter into an array @dest provided by the caller.
539 * The array @dest should be large enough to contain @size bytes.
540 * The user should check that the adapter has (@offset + @size) bytes
541 * available before calling this function.
544 gst_adapter_copy (GstAdapter * adapter, gpointer dest, gsize offset, gsize size)
546 g_return_if_fail (GST_IS_ADAPTER (adapter));
547 g_return_if_fail (size > 0);
548 g_return_if_fail (offset + size <= adapter->size);
550 copy_into_unchecked (adapter, dest, offset + adapter->skip, size);
554 * gst_adapter_copy_bytes:
555 * @adapter: a #GstAdapter
556 * @offset: the bytes offset in the adapter to start from
557 * @size: the number of bytes to copy
559 * Similar to gst_adapter_copy, but more suitable for language bindings. @size
560 * bytes of data starting at @offset will be copied out of the buffers contained
561 * in @adapter and into a new #GBytes structure which is returned. Depending on
562 * the value of the @size argument an empty #GBytes structure may be returned.
564 * Returns: (transfer full): A new #GBytes structure containing the copied data.
566 * Rename to: gst_adapter_copy
571 gst_adapter_copy_bytes (GstAdapter * adapter, gsize offset, gsize size)
574 data = g_malloc (size);
575 gst_adapter_copy (adapter, data, offset, size);
576 return g_bytes_new_take (data, size);
579 /*Flushes the first @flush bytes in the @adapter*/
581 gst_adapter_flush_unchecked (GstAdapter * adapter, gsize flush)
587 GST_LOG_OBJECT (adapter, "flushing %" G_GSIZE_FORMAT " bytes", flush);
589 if (adapter->info.memory)
590 gst_adapter_unmap (adapter);
593 adapter->size -= flush;
594 adapter->assembled_len = 0;
596 /* take skip into account */
597 flush += adapter->skip;
598 /* distance is always at least the amount of skipped bytes */
599 adapter->pts_distance -= adapter->skip;
600 adapter->dts_distance -= adapter->skip;
602 g = adapter->buflist;
604 size = gst_buffer_get_size (cur);
605 while (flush >= size) {
606 /* can skip whole buffer */
607 GST_LOG_OBJECT (adapter, "flushing out head buffer");
608 adapter->pts_distance += size;
609 adapter->dts_distance += size;
612 gst_buffer_unref (cur);
613 g = g_slist_delete_link (g, g);
615 if (G_UNLIKELY (g == NULL)) {
616 GST_LOG_OBJECT (adapter, "adapter empty now");
617 adapter->buflist_end = NULL;
620 /* there is a new head buffer, update the timestamps */
622 update_timestamps (adapter, cur);
623 size = gst_buffer_get_size (cur);
625 adapter->buflist = g;
626 /* account for the remaining bytes */
627 adapter->skip = flush;
628 adapter->pts_distance += flush;
629 adapter->dts_distance += flush;
630 /* invalidate scan position */
631 adapter->scan_offset = 0;
632 adapter->scan_entry = NULL;
637 * @adapter: a #GstAdapter
638 * @flush: the number of bytes to flush
640 * Flushes the first @flush bytes in the @adapter. The caller must ensure that
641 * at least this many bytes are available.
643 * See also: gst_adapter_map(), gst_adapter_unmap()
646 gst_adapter_flush (GstAdapter * adapter, gsize flush)
648 g_return_if_fail (GST_IS_ADAPTER (adapter));
649 g_return_if_fail (flush <= adapter->size);
651 /* flushing out 0 bytes will do nothing */
652 if (G_UNLIKELY (flush == 0))
655 gst_adapter_flush_unchecked (adapter, flush);
658 /* internal function, nbytes should be flushed after calling this function */
660 gst_adapter_take_internal (GstAdapter * adapter, gsize nbytes)
663 gsize toreuse, tocopy;
665 /* see how much data we can reuse from the assembled memory and how much
667 toreuse = MIN (nbytes, adapter->assembled_len);
668 tocopy = nbytes - toreuse;
670 /* find memory to return */
671 if (adapter->assembled_size >= nbytes && toreuse > 0) {
672 /* we reuse already allocated memory but only when we're going to reuse
673 * something from it because else we are worse than the malloc and copy
675 GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes of assembled"
677 /* we have enough free space in the assembled array */
678 data = adapter->assembled_data;
679 /* flush after this function should set the assembled_size to 0 */
680 adapter->assembled_data = g_malloc (adapter->assembled_size);
682 GST_LOG_OBJECT (adapter, "allocating %" G_GSIZE_FORMAT " bytes", nbytes);
683 /* not enough bytes in the assembled array, just allocate new space */
684 data = g_malloc (nbytes);
685 /* reuse what we can from the already assembled data */
687 GST_LOG_OBJECT (adapter, "reusing %" G_GSIZE_FORMAT " bytes", toreuse);
688 GST_CAT_LOG_OBJECT (GST_CAT_PERFORMANCE, adapter,
689 "memcpy %" G_GSIZE_FORMAT " bytes", toreuse);
690 memcpy (data, adapter->assembled_data, toreuse);
694 /* copy the remaining data */
695 copy_into_unchecked (adapter, toreuse + data, toreuse + adapter->skip,
703 * @adapter: a #GstAdapter
704 * @nbytes: the number of bytes to take
706 * Returns a freshly allocated buffer containing the first @nbytes bytes of the
707 * @adapter. The returned bytes will be flushed from the adapter.
709 * Caller owns returned value. g_free after usage.
711 * Free-function: g_free
713 * Returns: (transfer full) (array length=nbytes) (element-type guint8):
714 * oven-fresh hot data, or #NULL if @nbytes bytes are not available
717 gst_adapter_take (GstAdapter * adapter, gsize nbytes)
721 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
722 g_return_val_if_fail (nbytes > 0, NULL);
724 /* we don't have enough data, return NULL. This is unlikely
725 * as one usually does an _available() first instead of peeking a
727 if (G_UNLIKELY (nbytes > adapter->size))
730 data = gst_adapter_take_internal (adapter, nbytes);
732 gst_adapter_flush_unchecked (adapter, nbytes);
738 * gst_adapter_take_buffer_fast:
739 * @adapter: a #GstAdapter
740 * @nbytes: the number of bytes to take
742 * Returns a #GstBuffer containing the first @nbytes of the @adapter.
743 * The returned bytes will be flushed from the adapter. This function
744 * is potentially more performant than gst_adapter_take_buffer() since
745 * it can reuse the memory in pushed buffers by subbuffering or
746 * merging. Unlike gst_adapter_take_buffer(), the returned buffer may
747 * be composed of multiple non-contiguous #GstMemory objects, no
750 * Note that no assumptions should be made as to whether certain buffer
751 * flags such as the DISCONT flag are set on the returned buffer, or not.
752 * The caller needs to explicitly set or unset flags that should be set or
755 * This function can return buffer up to the return value of
756 * gst_adapter_available() without making copies if possible.
758 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
761 * Free-function: gst_buffer_unref
763 * Returns: (transfer full): a #GstBuffer containing the first @nbytes of
764 * the adapter, or #NULL if @nbytes bytes are not available.
765 * gst_buffer_unref() when no longer needed.
771 gst_adapter_take_buffer_fast (GstAdapter * adapter, gsize nbytes)
773 GstBuffer *buffer = NULL;
779 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
780 g_return_val_if_fail (nbytes > 0, NULL);
782 GST_LOG_OBJECT (adapter, "taking buffer of %" G_GSIZE_FORMAT " bytes",
785 /* we don't have enough data, return NULL. This is unlikely
786 * as one usually does an _available() first instead of grabbing a
788 if (G_UNLIKELY (nbytes > adapter->size))
791 skip = adapter->skip;
792 cur = adapter->buflist->data;
794 if (skip == 0 && gst_buffer_get_size (cur) == nbytes) {
795 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
796 " as head buffer", nbytes);
797 buffer = gst_buffer_ref (cur);
801 for (item = adapter->buflist; item && left > 0; item = item->next) {
805 size = MIN (gst_buffer_get_size (cur) - skip, left);
807 GST_LOG_OBJECT (adapter, "appending %" G_GSIZE_FORMAT " bytes"
808 " via region copy", size);
810 gst_buffer_copy_into (buffer, cur, GST_BUFFER_COPY_MEMORY, skip, size);
812 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, size);
818 gst_adapter_flush_unchecked (adapter, nbytes);
824 * gst_adapter_take_buffer:
825 * @adapter: a #GstAdapter
826 * @nbytes: the number of bytes to take
828 * Returns a #GstBuffer containing the first @nbytes bytes of the
829 * @adapter. The returned bytes will be flushed from the adapter.
830 * This function is potentially more performant than
831 * gst_adapter_take() since it can reuse the memory in pushed buffers
832 * by subbuffering or merging. This function will always return a
833 * buffer with a single memory region.
835 * Note that no assumptions should be made as to whether certain buffer
836 * flags such as the DISCONT flag are set on the returned buffer, or not.
837 * The caller needs to explicitly set or unset flags that should be set or
840 * Caller owns a reference to the returned buffer. gst_buffer_unref() after
843 * Free-function: gst_buffer_unref
845 * Returns: (transfer full): a #GstBuffer containing the first @nbytes of
846 * the adapter, or #NULL if @nbytes bytes are not available.
847 * gst_buffer_unref() when no longer needed.
850 gst_adapter_take_buffer (GstAdapter * adapter, gsize nbytes)
857 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
858 g_return_val_if_fail (nbytes > 0, NULL);
860 GST_LOG_OBJECT (adapter, "taking buffer of %" G_GSIZE_FORMAT " bytes",
863 /* we don't have enough data, return NULL. This is unlikely
864 * as one usually does an _available() first instead of grabbing a
866 if (G_UNLIKELY (nbytes > adapter->size))
869 cur = adapter->buflist->data;
870 skip = adapter->skip;
871 hsize = gst_buffer_get_size (cur);
873 /* our head buffer has enough data left, return it */
874 if (skip == 0 && hsize == nbytes) {
875 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
876 " as head buffer", nbytes);
877 buffer = gst_buffer_ref (cur);
879 } else if (hsize >= nbytes + skip) {
880 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
881 " via region copy", nbytes);
882 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
886 if (gst_adapter_try_to_merge_up (adapter, nbytes)) {
887 /* Merged something, let's try again for sub-buffering */
888 cur = adapter->buflist->data;
889 skip = adapter->skip;
890 if (gst_buffer_get_size (cur) >= nbytes + skip) {
891 GST_LOG_OBJECT (adapter, "providing buffer of %" G_GSIZE_FORMAT " bytes"
892 " via sub-buffer", nbytes);
893 buffer = gst_buffer_copy_region (cur, GST_BUFFER_COPY_ALL, skip, nbytes);
899 data = gst_adapter_take_internal (adapter, nbytes);
901 buffer = gst_buffer_new_wrapped (data, nbytes);
904 gst_adapter_flush_unchecked (adapter, nbytes);
910 * gst_adapter_take_list:
911 * @adapter: a #GstAdapter
912 * @nbytes: the number of bytes to take
914 * Returns a #GList of buffers containing the first @nbytes bytes of the
915 * @adapter. The returned bytes will be flushed from the adapter.
916 * When the caller can deal with individual buffers, this function is more
917 * performant because no memory should be copied.
919 * Caller owns returned list and contained buffers. gst_buffer_unref() each
920 * buffer in the list before freeing the list after usage.
922 * Returns: (element-type Gst.Buffer) (transfer full): a #GList of buffers
923 * containing the first @nbytes of the adapter, or #NULL if @nbytes bytes
927 gst_adapter_take_list (GstAdapter * adapter, gsize nbytes)
929 GQueue queue = G_QUEUE_INIT;
933 g_return_val_if_fail (GST_IS_ADAPTER (adapter), NULL);
934 g_return_val_if_fail (nbytes <= adapter->size, NULL);
936 GST_LOG_OBJECT (adapter, "taking %" G_GSIZE_FORMAT " bytes", nbytes);
939 cur = adapter->buflist->data;
940 skip = adapter->skip;
941 hsize = MIN (nbytes, gst_buffer_get_size (cur) - skip);
943 cur = gst_adapter_take_buffer (adapter, hsize);
945 g_queue_push_tail (&queue, cur);
953 * gst_adapter_available:
954 * @adapter: a #GstAdapter
956 * Gets the maximum amount of bytes available, that is it returns the maximum
957 * value that can be supplied to gst_adapter_map() without that function
960 * Returns: number of bytes available in @adapter
963 gst_adapter_available (GstAdapter * adapter)
965 g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
967 return adapter->size;
971 * gst_adapter_available_fast:
972 * @adapter: a #GstAdapter
974 * Gets the maximum number of bytes that are immediately available without
975 * requiring any expensive operations (like copying the data into a
978 * Returns: number of bytes that are available in @adapter without expensive
982 gst_adapter_available_fast (GstAdapter * adapter)
988 g_return_val_if_fail (GST_IS_ADAPTER (adapter), 0);
991 if (adapter->size == 0)
994 /* some stuff we already assembled */
995 if (adapter->assembled_len)
996 return adapter->assembled_len;
998 /* take the first non-zero buffer */
999 g = adapter->buflist;
1002 size = gst_buffer_get_size (cur);
1005 g = g_slist_next (g);
1008 /* we can quickly get the (remaining) data of the first buffer */
1009 return size - adapter->skip;
1013 * gst_adapter_prev_pts:
1014 * @adapter: a #GstAdapter
1015 * @distance: (out) (allow-none): pointer to location for distance, or NULL
1017 * Get the pts that was before the current byte in the adapter. When
1018 * @distance is given, the amount of bytes between the pts and the current
1019 * position is returned.
1021 * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1022 * the adapter is first created or when it is cleared. This also means that before
1023 * the first byte with a pts is removed from the adapter, the pts
1024 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1026 * Returns: The previously seen pts.
1029 gst_adapter_prev_pts (GstAdapter * adapter, guint64 * distance)
1031 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1034 *distance = adapter->pts_distance;
1036 return adapter->pts;
1040 * gst_adapter_prev_dts:
1041 * @adapter: a #GstAdapter
1042 * @distance: (out) (allow-none): pointer to location for distance, or NULL
1044 * Get the dts that was before the current byte in the adapter. When
1045 * @distance is given, the amount of bytes between the dts and the current
1046 * position is returned.
1048 * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1049 * the adapter is first created or when it is cleared. This also means that before
1050 * the first byte with a dts is removed from the adapter, the dts
1051 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1053 * Returns: The previously seen dts.
1056 gst_adapter_prev_dts (GstAdapter * adapter, guint64 * distance)
1058 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1061 *distance = adapter->dts_distance;
1063 return adapter->dts;
1067 * gst_adapter_prev_pts_at_offset:
1068 * @adapter: a #GstAdapter
1069 * @offset: the offset in the adapter at which to get timestamp
1070 * @distance: (out) (allow-none): pointer to location for distance, or NULL
1072 * Get the pts that was before the byte at offset @offset in the adapter. When
1073 * @distance is given, the amount of bytes between the pts and the current
1074 * position is returned.
1076 * The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1077 * the adapter is first created or when it is cleared. This also means that before
1078 * the first byte with a pts is removed from the adapter, the pts
1079 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1082 * Returns: The previously seen pts at given offset.
1085 gst_adapter_prev_pts_at_offset (GstAdapter * adapter, gsize offset,
1090 gsize read_offset = 0;
1091 GstClockTime pts = adapter->pts;
1093 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1094 g_return_val_if_fail (offset >= 0, GST_CLOCK_TIME_NONE);
1096 g = adapter->buflist;
1098 while (g && read_offset < offset + adapter->skip) {
1101 read_offset += gst_buffer_get_size (cur);
1102 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (cur))) {
1103 pts = GST_BUFFER_PTS (cur);
1106 g = g_slist_next (g);
1110 *distance = adapter->dts_distance + offset;
1116 * gst_adapter_prev_dts_at_offset:
1117 * @adapter: a #GstAdapter
1118 * @offset: the offset in the adapter at which to get timestamp
1119 * @distance: (out) (allow-none): pointer to location for distance, or NULL
1121 * Get the dts that was before the byte at offset @offset in the adapter. When
1122 * @distance is given, the amount of bytes between the dts and the current
1123 * position is returned.
1125 * The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
1126 * the adapter is first created or when it is cleared. This also means that before
1127 * the first byte with a dts is removed from the adapter, the dts
1128 * and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
1131 * Returns: The previously seen dts at given offset.
1134 gst_adapter_prev_dts_at_offset (GstAdapter * adapter, gsize offset,
1139 gsize read_offset = 0;
1140 GstClockTime dts = adapter->dts;
1142 g_return_val_if_fail (GST_IS_ADAPTER (adapter), GST_CLOCK_TIME_NONE);
1143 g_return_val_if_fail (offset >= 0, GST_CLOCK_TIME_NONE);
1145 g = adapter->buflist;
1147 while (g && read_offset < offset + adapter->skip) {
1150 read_offset += gst_buffer_get_size (cur);
1151 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (cur))) {
1152 dts = GST_BUFFER_DTS (cur);
1155 g = g_slist_next (g);
1159 *distance = adapter->dts_distance + offset;
1165 * gst_adapter_masked_scan_uint32_peek:
1166 * @adapter: a #GstAdapter
1167 * @mask: mask to apply to data before matching against @pattern
1168 * @pattern: pattern to match (after mask is applied)
1169 * @offset: offset into the adapter data from which to start scanning, returns
1170 * the last scanned position.
1171 * @size: number of bytes to scan from offset
1172 * @value: pointer to uint32 to return matching data
1174 * Scan for pattern @pattern with applied mask @mask in the adapter data,
1175 * starting from offset @offset. If a match is found, the value that matched
1176 * is returned through @value, otherwise @value is left untouched.
1178 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1179 * of endianness. All four bytes of the pattern must be present in the
1180 * adapter for it to match, even if the first or last bytes are masked out.
1182 * It is an error to call this function without making sure that there is
1183 * enough data (offset+size bytes) in the adapter.
1185 * Returns: offset of the first match, or -1 if no match was found.
1188 gst_adapter_masked_scan_uint32_peek (GstAdapter * adapter, guint32 mask,
1189 guint32 pattern, gsize offset, gsize size, guint32 * value)
1192 gsize skip, bsize, i;
1198 g_return_val_if_fail (size > 0, -1);
1199 g_return_val_if_fail (offset + size <= adapter->size, -1);
1200 g_return_val_if_fail (((~mask) & pattern) == 0, -1);
1202 /* we can't find the pattern with less than 4 bytes */
1203 if (G_UNLIKELY (size < 4))
1206 skip = offset + adapter->skip;
1208 /* first step, do skipping and position on the first buffer */
1209 /* optimistically assume scanning continues sequentially */
1210 if (adapter->scan_entry && (adapter->scan_offset <= skip)) {
1211 g = adapter->scan_entry;
1212 skip -= adapter->scan_offset;
1214 g = adapter->buflist;
1215 adapter->scan_offset = 0;
1216 adapter->scan_entry = NULL;
1219 bsize = gst_buffer_get_size (buf);
1220 while (G_UNLIKELY (skip >= bsize)) {
1222 g = g_slist_next (g);
1223 adapter->scan_offset += bsize;
1224 adapter->scan_entry = g;
1226 bsize = gst_buffer_get_size (buf);
1228 /* get the data now */
1229 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1232 bdata = (guint8 *) info.data + skip;
1233 bsize = info.size - skip;
1236 /* set the state to something that does not match */
1241 bsize = MIN (bsize, size);
1242 for (i = 0; i < bsize; i++) {
1243 state = ((state << 8) | bdata[i]);
1244 if (G_UNLIKELY ((state & mask) == pattern)) {
1245 /* we have a match but we need to have skipped at
1246 * least 4 bytes to fill the state. */
1247 if (G_LIKELY (skip + i >= 3)) {
1248 if (G_LIKELY (value))
1250 gst_buffer_unmap (buf, &info);
1251 return offset + skip + i - 3;
1259 /* nothing found yet, go to next buffer */
1261 g = g_slist_next (g);
1262 adapter->scan_offset += info.size;
1263 adapter->scan_entry = g;
1264 gst_buffer_unmap (buf, &info);
1267 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
1274 gst_buffer_unmap (buf, &info);
1281 * gst_adapter_masked_scan_uint32:
1282 * @adapter: a #GstAdapter
1283 * @mask: mask to apply to data before matching against @pattern
1284 * @pattern: pattern to match (after mask is applied)
1285 * @offset: offset into the adapter data from which to start scanning, returns
1286 * the last scanned position.
1287 * @size: number of bytes to scan from offset
1289 * Scan for pattern @pattern with applied mask @mask in the adapter data,
1290 * starting from offset @offset.
1292 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
1293 * of endianness. All four bytes of the pattern must be present in the
1294 * adapter for it to match, even if the first or last bytes are masked out.
1296 * It is an error to call this function without making sure that there is
1297 * enough data (offset+size bytes) in the adapter.
1299 * This function calls gst_adapter_masked_scan_uint32_peek() passing NULL
1302 * Returns: offset of the first match, or -1 if no match was found.
1306 * // Assume the adapter contains 0x00 0x01 0x02 ... 0xfe 0xff
1308 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 0, 256);
1310 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x00010203, 1, 255);
1312 * gst_adapter_masked_scan_uint32 (adapter, 0xffffffff, 0x01020304, 1, 255);
1314 * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0001, 0, 256);
1316 * gst_adapter_masked_scan_uint32 (adapter, 0xffff, 0x0203, 0, 256);
1318 * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 256);
1320 * gst_adapter_masked_scan_uint32 (adapter, 0xffff0000, 0x02030000, 0, 4);
1325 gst_adapter_masked_scan_uint32 (GstAdapter * adapter, guint32 mask,
1326 guint32 pattern, gsize offset, gsize size)
1328 return gst_adapter_masked_scan_uint32_peek (adapter, mask, pattern, offset,