2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstbuffer.c: Buffer operations
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
26 * @short_description: Data-passing buffer type
27 * @see_also: #GstPad, #GstMiniObject, #GstMemory, #GstMeta, #GstBufferPool
29 * Buffers are the basic unit of data transfer in GStreamer. They contain the
30 * timing and offset along with other arbitrary metadata that is associated
31 * with the #GstMemory blocks that the buffer contains.
33 * Buffers are usually created with gst_buffer_new(). After a buffer has been
34 * created one will typically allocate memory for it and add it to the buffer.
35 * The following example creates a buffer that can hold a given video frame
36 * with a given width, height and bits per plane.
41 * gint size, width, height, bpp;
43 * size = width * height * bpp;
44 * buffer = gst_buffer_new ();
45 * memory = gst_allocator_alloc (NULL, size, NULL);
46 * gst_buffer_insert_memory (buffer, -1, memory);
50 * Alternatively, use gst_buffer_new_allocate() to create a buffer with
51 * preallocated data of a given size.
53 * Buffers can contain a list of #GstMemory objects. You can retrieve how many
54 * memory objects with gst_buffer_n_memory() and you can get a pointer
55 * to memory with gst_buffer_peek_memory()
57 * A buffer will usually have timestamps, and a duration, but neither of these
58 * are guaranteed (they may be set to #GST_CLOCK_TIME_NONE). Whenever a
59 * meaningful value can be given for these, they should be set. The timestamps
60 * and duration are measured in nanoseconds (they are #GstClockTime values).
62 * The buffer DTS refers to the timestamp when the buffer should be decoded and
63 * is usually monotonically increasing. The buffer PTS refers to the timestamp when
64 * the buffer content should be presented to the user and is not always
65 * monotonically increasing.
67 * A buffer can also have one or both of a start and an end offset. These are
68 * media-type specific. For video buffers, the start offset will generally be
69 * the frame number. For audio buffers, it will be the number of samples
70 * produced so far. For compressed data, it could be the byte offset in a
71 * source or destination file. Likewise, the end offset will be the offset of
72 * the end of the buffer. These can only be meaningfully interpreted if you
73 * know the media type of the buffer (the preceding CAPS event). Either or both
74 * can be set to #GST_BUFFER_OFFSET_NONE.
76 * gst_buffer_ref() is used to increase the refcount of a buffer. This must be
77 * done when you want to keep a handle to the buffer after pushing it to the
78 * next element. The buffer refcount determines the writability of the buffer, a
79 * buffer is only writable when the refcount is exactly 1, i.e. when the caller
80 * has the only reference to the buffer.
82 * To efficiently create a smaller buffer out of an existing one, you can
83 * use gst_buffer_copy_region(). This method tries to share the memory objects
84 * between the two buffers.
86 * If a plug-in wants to modify the buffer data or metadata in-place, it should
87 * first obtain a buffer that is safe to modify by using
88 * gst_buffer_make_writable(). This function is optimized so that a copy will
89 * only be made when it is necessary.
91 * Several flags of the buffer can be set and unset with the
92 * GST_BUFFER_FLAG_SET() and GST_BUFFER_FLAG_UNSET() macros. Use
93 * GST_BUFFER_FLAG_IS_SET() to test if a certain #GstBufferFlags flag is set.
95 * Buffers can be efficiently merged into a larger buffer with
96 * gst_buffer_append(). Copying of memory will only be done when absolutely
99 * Arbitrary extra metadata can be set on a buffer with gst_buffer_add_meta().
100 * Metadata can be retrieved with gst_buffer_get_meta(). See also #GstMeta.
102 * An element should either unref the buffer or push it out on a src pad
103 * using gst_pad_push() (see #GstPad).
105 * Buffers are usually freed by unreffing them with gst_buffer_unref(). When
106 * the refcount drops to 0, any memory and metadata pointed to by the buffer is
107 * unreffed as well. Buffers allocated from a #GstBufferPool will be returned to
108 * the pool when the refcount drops to 0.
110 * The #GstParentBufferMeta is a meta which can be attached to a #GstBuffer
111 * to hold a reference to another buffer that is only released when the child
112 * #GstBuffer is released.
114 * Typically, #GstParentBufferMeta is used when the child buffer is directly
115 * using the #GstMemory of the parent buffer, and wants to prevent the parent
116 * buffer from being returned to a buffer pool until the #GstMemory is available
117 * for re-use. (Since: 1.6)
120 #define GST_DISABLE_MINIOBJECT_INLINE_FUNCTIONS
121 #include "gst_private.h"
130 #include "gstbuffer.h"
131 #include "gstbufferpool.h"
134 #include "gstutils.h"
135 #include "gstversion.h"
138 #include "glib-compat-private.h"
140 GType _gst_buffer_type = 0;
142 /* info->size will be sizeof(FooMeta) which contains a GstMeta at the beginning
143 * too, and then there is again a GstMeta in GstMetaItem, so subtract one. */
144 #define ITEM_SIZE(info) ((info)->size + sizeof (GstMetaItem) - sizeof (GstMeta))
146 #define GST_BUFFER_MEM_MAX 16
148 #define GST_BUFFER_SLICE_SIZE(b) (((GstBufferImpl *)(b))->slice_size)
149 #define GST_BUFFER_MEM_LEN(b) (((GstBufferImpl *)(b))->len)
150 #define GST_BUFFER_MEM_ARRAY(b) (((GstBufferImpl *)(b))->mem)
151 #define GST_BUFFER_MEM_PTR(b,i) (((GstBufferImpl *)(b))->mem[i])
152 #define GST_BUFFER_BUFMEM(b) (((GstBufferImpl *)(b))->bufmem)
153 #define GST_BUFFER_META(b) (((GstBufferImpl *)(b))->item)
154 #define GST_BUFFER_TAIL_META(b) (((GstBufferImpl *)(b))->tail_item)
162 /* the memory blocks */
164 GstMemory *mem[GST_BUFFER_MEM_MAX];
166 /* memory of the buffer when allocated from 1 chunk */
169 /* FIXME, make metadata allocation more efficient by using part of the
172 GstMetaItem *tail_item;
175 static gint64 meta_seq; /* 0 *//* ATOMIC */
177 /* TODO: use GLib's once https://gitlab.gnome.org/GNOME/glib/issues/1076 lands */
178 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
180 gst_atomic_int64_inc (gint64 * atomic)
182 return __sync_fetch_and_add (atomic, 1);
184 #elif defined (G_PLATFORM_WIN32)
187 gst_atomic_int64_inc (gint64 * atomic)
189 return InterlockedExchangeAdd64 (atomic, 1);
192 #define STR_TOKEN(s) #s
193 #define STR(s) STR_TOKEN(s)
194 #pragma message "No 64-bit atomic int defined for this " STR(TARGET_CPU) " platform/toolchain!"
196 #define NO_64BIT_ATOMIC_INT_FOR_PLATFORM
197 G_LOCK_DEFINE_STATIC (meta_seq);
199 gst_atomic_int64_inc (gint64 * atomic)
212 _is_span (GstMemory ** mem, gsize len, gsize * poffset, GstMemory ** parent)
214 GstMemory *mcur, *mprv;
215 gboolean have_offset = FALSE;
220 for (i = 0; i < len; i++) {
228 /* check if memory is contiguous */
229 if (!gst_memory_is_span (mprv, mcur, &poffs))
236 *parent = mprv->parent;
246 _actual_merged_memory (GstBuffer * buffer, guint idx, guint length)
248 GstMemory **mem, *result = NULL;
249 GstMemory *parent = NULL;
250 gsize size, poffset = 0;
252 mem = GST_BUFFER_MEM_ARRAY (buffer);
254 size = gst_buffer_get_sizes_range (buffer, idx, length, NULL, NULL);
256 if (G_UNLIKELY (_is_span (mem + idx, length, &poffset, &parent))) {
257 if (!GST_MEMORY_IS_NO_SHARE (parent))
258 result = gst_memory_share (parent, poffset, size);
260 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy for merge %p", parent);
261 result = gst_memory_copy (parent, poffset, size);
264 gsize i, tocopy, left;
265 GstMapInfo sinfo, dinfo;
268 result = gst_allocator_alloc (NULL, size, NULL);
269 if (result == NULL || !gst_memory_map (result, &dinfo, GST_MAP_WRITE)) {
270 GST_CAT_ERROR (GST_CAT_BUFFER, "Failed to map memory writable");
272 gst_memory_unref (result);
279 for (i = idx; i < (idx + length) && left > 0; i++) {
280 if (!gst_memory_map (mem[i], &sinfo, GST_MAP_READ)) {
281 GST_CAT_ERROR (GST_CAT_BUFFER,
282 "buffer %p, idx %u, length %u failed to map readable", buffer,
284 gst_memory_unmap (result, &dinfo);
285 gst_memory_unref (result);
288 tocopy = MIN (sinfo.size, left);
289 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
290 "memcpy %" G_GSIZE_FORMAT " bytes for merge %p from memory %p",
291 tocopy, result, mem[i]);
292 memcpy (ptr, (guint8 *) sinfo.data, tocopy);
295 gst_memory_unmap (mem[i], &sinfo);
297 gst_memory_unmap (result, &dinfo);
303 static inline GstMemory *
304 _get_merged_memory (GstBuffer * buffer, guint idx, guint length)
306 GST_CAT_LOG (GST_CAT_BUFFER, "buffer %p, idx %u, length %u", buffer, idx,
309 if (G_UNLIKELY (length == 0))
312 if (G_LIKELY (length == 1))
313 return gst_memory_ref (GST_BUFFER_MEM_PTR (buffer, idx));
315 return _actual_merged_memory (buffer, idx, length);
320 _replace_memory (GstBuffer * buffer, guint len, guint idx, guint length,
327 GST_CAT_LOG (GST_CAT_BUFFER,
328 "buffer %p replace %u-%" G_GSIZE_FORMAT " with memory %p", buffer, idx,
331 /* unref old memory */
332 for (i = idx; i < end; i++) {
333 GstMemory *old = GST_BUFFER_MEM_PTR (buffer, i);
335 gst_memory_unlock (old, GST_LOCK_FLAG_EXCLUSIVE);
336 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
337 GST_MINI_OBJECT_CAST (buffer));
338 gst_memory_unref (old);
342 /* replace with single memory */
343 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (mem),
344 GST_MINI_OBJECT_CAST (buffer));
345 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
346 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
352 memmove (&GST_BUFFER_MEM_PTR (buffer, idx),
353 &GST_BUFFER_MEM_PTR (buffer, end), (len - end) * sizeof (gpointer));
355 GST_BUFFER_MEM_LEN (buffer) = len - length;
356 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
360 * gst_buffer_get_flags:
361 * @buffer: a #GstBuffer
363 * Gets the #GstBufferFlags flags set on this buffer.
365 * Returns: the flags set on this buffer.
370 gst_buffer_get_flags (GstBuffer * buffer)
372 return (GstBufferFlags) GST_BUFFER_FLAGS (buffer);
376 * gst_buffer_has_flags:
377 * @buffer: a #GstBuffer
378 * @flags: the #GstBufferFlags flag to check.
380 * Gives the status of a specific flag on a buffer.
382 * Returns: %TRUE if all flags in @flags are found on @buffer.
387 gst_buffer_has_flags (GstBuffer * buffer, GstBufferFlags flags)
389 return GST_BUFFER_FLAG_IS_SET (buffer, flags);
393 * gst_buffer_set_flags:
394 * @buffer: a #GstBuffer
395 * @flags: the #GstBufferFlags to set.
397 * Sets one or more buffer flags on a buffer.
399 * Returns: %TRUE if @flags were successfully set on buffer.
404 gst_buffer_set_flags (GstBuffer * buffer, GstBufferFlags flags)
406 GST_BUFFER_FLAG_SET (buffer, flags);
411 * gst_buffer_unset_flags:
412 * @buffer: a #GstBuffer
413 * @flags: the #GstBufferFlags to clear
415 * Clears one or more buffer flags.
417 * Returns: true if @flags is successfully cleared from buffer.
422 gst_buffer_unset_flags (GstBuffer * buffer, GstBufferFlags flags)
424 GST_BUFFER_FLAG_UNSET (buffer, flags);
430 /* transfer full for return and transfer none for @mem */
431 static inline GstMemory *
432 _memory_get_exclusive_reference (GstMemory * mem)
434 GstMemory *ret = NULL;
436 if (gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE)) {
437 ret = gst_memory_ref (mem);
439 /* we cannot take another exclusive lock as the memory is already
440 * locked WRITE + EXCLUSIVE according to part-miniobject.txt */
441 ret = gst_memory_copy (mem, 0, -1);
444 if (!gst_memory_lock (ret, GST_LOCK_FLAG_EXCLUSIVE)) {
445 gst_memory_unref (ret);
452 GST_CAT_WARNING (GST_CAT_BUFFER, "Failed to acquire an exclusive lock for "
459 _memory_add (GstBuffer * buffer, gint idx, GstMemory * mem)
461 guint i, len = GST_BUFFER_MEM_LEN (buffer);
463 GST_CAT_LOG (GST_CAT_BUFFER, "buffer %p, idx %d, mem %p", buffer, idx, mem);
465 if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) {
466 /* too many buffer, span them. */
467 /* FIXME, there is room for improvement here: We could only try to merge
468 * 2 buffers to make some room. If we can't efficiently merge 2 buffers we
469 * could try to only merge the two smallest buffers to avoid memcpy, etc. */
470 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "memory array overflow in buffer %p",
472 _replace_memory (buffer, len, 0, len, _get_merged_memory (buffer, 0, len));
473 /* we now have 1 single spanned buffer */
480 for (i = len; i > idx; i--) {
481 /* move buffers to insert, FIXME, we need to insert first and then merge */
482 GST_BUFFER_MEM_PTR (buffer, i) = GST_BUFFER_MEM_PTR (buffer, i - 1);
484 /* and insert the new buffer */
485 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
486 GST_BUFFER_MEM_LEN (buffer) = len + 1;
487 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (mem),
488 GST_MINI_OBJECT_CAST (buffer));
490 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
493 GST_DEFINE_MINI_OBJECT_TYPE (GstBuffer, gst_buffer);
496 _priv_gst_buffer_initialize (void)
498 _gst_buffer_type = gst_buffer_get_type ();
500 #ifdef NO_64BIT_ATOMIC_INT_FOR_PLATFORM
501 GST_CAT_WARNING (GST_CAT_PERFORMANCE,
502 "No 64-bit atomic int defined for this platform/toolchain!");
507 * gst_buffer_get_max_memory:
509 * Gets the maximum amount of memory blocks that a buffer can hold. This is a
510 * compile time constant that can be queried with the function.
512 * When more memory blocks are added, existing memory blocks will be merged
513 * together to make room for the new block.
515 * Returns: the maximum amount of memory blocks that a buffer can hold.
520 gst_buffer_get_max_memory (void)
522 return GST_BUFFER_MEM_MAX;
526 * gst_buffer_copy_into:
527 * @dest: a destination #GstBuffer
528 * @src: a source #GstBuffer
529 * @flags: flags indicating what metadata fields should be copied.
530 * @offset: offset to copy from
531 * @size: total size to copy. If -1, all data is copied.
533 * Copies the information from @src into @dest.
535 * If @dest already contains memory and @flags contains GST_BUFFER_COPY_MEMORY,
536 * the memory from @src will be appended to @dest.
538 * @flags indicate which fields will be copied.
540 * Returns: %TRUE if the copying succeeded, %FALSE otherwise.
543 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
544 GstBufferCopyFlags flags, gsize offset, gsize size)
548 gboolean region = FALSE;
550 g_return_val_if_fail (dest != NULL, FALSE);
551 g_return_val_if_fail (src != NULL, FALSE);
553 /* nothing to copy if the buffers are the same */
554 if (G_UNLIKELY (dest == src))
557 g_return_val_if_fail (gst_buffer_is_writable (dest), FALSE);
559 bufsize = gst_buffer_get_size (src);
560 g_return_val_if_fail (bufsize >= offset, FALSE);
564 size = bufsize - offset;
567 g_return_val_if_fail (bufsize >= offset + size, FALSE);
569 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p, offset %" G_GSIZE_FORMAT
570 "-%" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT, src, dest, offset, size,
573 if (flags & GST_BUFFER_COPY_FLAGS) {
575 guint flags_mask = ~GST_BUFFER_FLAG_TAG_MEMORY;
577 GST_MINI_OBJECT_FLAGS (dest) =
578 (GST_MINI_OBJECT_FLAGS (src) & flags_mask) |
579 (GST_MINI_OBJECT_FLAGS (dest) & ~flags_mask);
582 if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
584 GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src);
585 GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src);
586 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
587 if (size == bufsize) {
588 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
589 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
592 GST_BUFFER_PTS (dest) = GST_CLOCK_TIME_NONE;
593 GST_BUFFER_DTS (dest) = GST_CLOCK_TIME_NONE;
594 GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
595 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
596 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
600 if (flags & GST_BUFFER_COPY_MEMORY) {
601 gsize skip, left, len, dest_len, i, bsize;
604 deep = flags & GST_BUFFER_COPY_DEEP;
606 len = GST_BUFFER_MEM_LEN (src);
607 dest_len = GST_BUFFER_MEM_LEN (dest);
611 /* copy and make regions of the memory */
612 for (i = 0; i < len && left > 0; i++) {
613 GstMemory *mem = GST_BUFFER_MEM_PTR (src, i);
618 /* don't copy buffer */
621 GstMemory *newmem = NULL;
624 tocopy = MIN (bsize - skip, left);
626 if (tocopy < bsize && !deep && !GST_MEMORY_IS_NO_SHARE (mem)) {
627 /* we need to clip something */
628 newmem = gst_memory_share (mem, skip, tocopy);
630 gst_memory_lock (newmem, GST_LOCK_FLAG_EXCLUSIVE);
635 if (deep || GST_MEMORY_IS_NO_SHARE (mem) || (!newmem && tocopy < bsize)) {
636 /* deep copy or we're not allowed to share this memory
637 * between buffers, always copy then */
638 newmem = gst_memory_copy (mem, skip, tocopy);
640 gst_memory_lock (newmem, GST_LOCK_FLAG_EXCLUSIVE);
643 } else if (!newmem) {
644 newmem = _memory_get_exclusive_reference (mem);
648 gst_buffer_remove_memory_range (dest, dest_len, -1);
652 _memory_add (dest, -1, newmem);
656 if (flags & GST_BUFFER_COPY_MERGE) {
659 len = GST_BUFFER_MEM_LEN (dest);
660 mem = _get_merged_memory (dest, 0, len);
662 gst_buffer_remove_memory_range (dest, dest_len, -1);
665 _replace_memory (dest, len, 0, len, mem);
669 if (flags & GST_BUFFER_COPY_META) {
672 deep = (flags & GST_BUFFER_COPY_DEEP) != 0;
674 /* NOTE: GstGLSyncMeta copying relies on the meta
675 * being copied now, after the buffer data,
676 * so this has to happen last */
677 for (walk = GST_BUFFER_META (src); walk; walk = walk->next) {
678 GstMeta *meta = &walk->meta;
679 const GstMetaInfo *info = meta->info;
681 /* Don't copy memory metas if we only copied part of the buffer, didn't
682 * copy memories or merged memories. In all these cases the memory
683 * structure has changed and the memory meta becomes meaningless.
685 if ((region || !(flags & GST_BUFFER_COPY_MEMORY)
686 || (flags & GST_BUFFER_COPY_MERGE))
687 && gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)) {
688 GST_CAT_DEBUG (GST_CAT_BUFFER,
689 "don't copy memory meta %p of API type %s", meta,
690 g_type_name (info->api));
691 } else if (deep && gst_meta_api_type_has_tag (info->api,
692 _gst_meta_tag_memory_reference)) {
693 GST_CAT_DEBUG (GST_CAT_BUFFER,
694 "don't copy memory reference meta %p of API type %s", meta,
695 g_type_name (info->api));
696 } else if (info->transform_func) {
697 GstMetaTransformCopy copy_data;
699 copy_data.region = region;
700 copy_data.offset = offset;
701 copy_data.size = size;
703 if (!info->transform_func (dest, meta, src,
704 _gst_meta_transform_copy, ©_data)) {
705 GST_CAT_ERROR (GST_CAT_BUFFER,
706 "failed to copy meta %p of API type %s", meta,
707 g_type_name (info->api));
717 gst_buffer_copy_with_flags (const GstBuffer * buffer, GstBufferCopyFlags flags)
721 g_return_val_if_fail (buffer != NULL, NULL);
723 /* create a fresh new buffer */
724 copy = gst_buffer_new ();
726 /* copy what the 'flags' want from our parent */
727 /* FIXME why we can't pass const to gst_buffer_copy_into() ? */
728 if (!gst_buffer_copy_into (copy, (GstBuffer *) buffer, flags, 0, -1))
729 gst_buffer_replace (©, NULL);
732 GST_BUFFER_FLAG_UNSET (copy, GST_BUFFER_FLAG_TAG_MEMORY);
738 _gst_buffer_copy (const GstBuffer * buffer)
740 return gst_buffer_copy_with_flags (buffer, GST_BUFFER_COPY_ALL);
744 * gst_buffer_copy_deep:
745 * @buf: a #GstBuffer.
747 * Creates a copy of the given buffer. This will make a newly allocated
748 * copy of the data the source buffer contains.
750 * Returns: (transfer full) (nullable): a new copy of @buf if the copy succeeded, %NULL otherwise.
755 gst_buffer_copy_deep (const GstBuffer * buffer)
757 return gst_buffer_copy_with_flags (buffer,
758 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP);
761 /* the default dispose function revives the buffer and returns it to the
762 * pool when there is a pool */
764 _gst_buffer_dispose (GstBuffer * buffer)
768 /* no pool, do free */
769 if ((pool = buffer->pool) == NULL)
772 /* keep the buffer alive */
773 gst_buffer_ref (buffer);
774 /* return the buffer to the pool */
775 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
776 gst_buffer_pool_release_buffer (pool, buffer);
782 _gst_buffer_free (GstBuffer * buffer)
784 GstMetaItem *walk, *next;
788 g_return_if_fail (buffer != NULL);
790 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
793 for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
794 GstMeta *meta = &walk->meta;
795 const GstMetaInfo *info = meta->info;
797 /* call free_func if any */
799 info->free_func (meta, buffer);
802 /* and free the slice */
803 g_slice_free1 (ITEM_SIZE (info), walk);
806 /* get the size, when unreffing the memory, we could also unref the buffer
808 msize = GST_BUFFER_SLICE_SIZE (buffer);
810 /* free our memory */
811 len = GST_BUFFER_MEM_LEN (buffer);
812 for (i = 0; i < len; i++) {
813 gst_memory_unlock (GST_BUFFER_MEM_PTR (buffer, i), GST_LOCK_FLAG_EXCLUSIVE);
814 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (GST_BUFFER_MEM_PTR
815 (buffer, i)), GST_MINI_OBJECT_CAST (buffer));
816 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
819 /* we set msize to 0 when the buffer is part of the memory block */
822 memset (buffer, 0xff, msize);
824 g_slice_free1 (msize, buffer);
826 gst_memory_unref (GST_BUFFER_BUFMEM (buffer));
831 gst_buffer_init (GstBufferImpl * buffer, gsize size)
833 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), 0, _gst_buffer_type,
834 (GstMiniObjectCopyFunction) _gst_buffer_copy,
835 (GstMiniObjectDisposeFunction) _gst_buffer_dispose,
836 (GstMiniObjectFreeFunction) _gst_buffer_free);
838 GST_BUFFER_SLICE_SIZE (buffer) = size;
840 GST_BUFFER (buffer)->pool = NULL;
841 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
842 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
843 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
844 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
845 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
847 GST_BUFFER_MEM_LEN (buffer) = 0;
848 GST_BUFFER_META (buffer) = NULL;
854 * Creates a newly allocated buffer without any data.
856 * Returns: (transfer full): the new #GstBuffer.
859 gst_buffer_new (void)
861 GstBufferImpl *newbuf;
863 newbuf = g_slice_new (GstBufferImpl);
864 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
866 gst_buffer_init (newbuf, sizeof (GstBufferImpl));
868 return GST_BUFFER_CAST (newbuf);
872 * gst_buffer_new_allocate:
873 * @allocator: (transfer none) (allow-none): the #GstAllocator to use, or %NULL to use the
875 * @size: the size in bytes of the new buffer's data.
876 * @params: (transfer none) (allow-none): optional parameters
878 * Tries to create a newly allocated buffer with data of the given size and
879 * extra parameters from @allocator. If the requested amount of memory can't be
880 * allocated, %NULL will be returned. The allocated buffer memory is not cleared.
882 * When @allocator is %NULL, the default memory allocator will be used.
884 * Note that when @size == 0, the buffer will not have memory associated with it.
886 * Returns: (transfer full) (nullable): a new #GstBuffer
889 gst_buffer_new_allocate (GstAllocator * allocator, gsize size,
890 GstAllocationParams * params)
901 mem = gst_allocator_alloc (allocator, size, params);
902 if (G_UNLIKELY (mem == NULL))
908 newbuf = gst_buffer_new ();
911 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
912 _memory_add (newbuf, -1, mem);
915 GST_CAT_LOG (GST_CAT_BUFFER,
916 "new buffer %p of size %" G_GSIZE_FORMAT " from allocator %p", newbuf,
921 asize = sizeof (GstBufferImpl) + size;
922 data = g_slice_alloc (asize);
923 if (G_UNLIKELY (data == NULL))
926 newbuf = GST_BUFFER_CAST (data);
928 gst_buffer_init ((GstBufferImpl *) data, asize);
930 mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL,
932 _memory_add (newbuf, -1, mem, TRUE);
937 /* allocate memory and buffer, it might be interesting to do this but there
938 * are many complications. We need to keep the memory mapped to access the
939 * buffer fields and the memory for the buffer might be just very slow. We
940 * also need to do some more magic to get the alignment right. */
941 asize = sizeof (GstBufferImpl) + size;
942 mem = gst_allocator_alloc (allocator, asize, align);
943 if (G_UNLIKELY (mem == NULL))
946 /* map the data part and init the buffer in it, set the buffer size to 0 so
947 * that a finalize won't free the buffer */
948 data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
949 gst_buffer_init ((GstBufferImpl *) data, 0);
950 gst_memory_unmap (mem);
952 /* strip off the buffer */
953 gst_memory_resize (mem, sizeof (GstBufferImpl), size);
955 newbuf = GST_BUFFER_CAST (data);
956 GST_BUFFER_BUFMEM (newbuf) = mem;
959 _memory_add (newbuf, -1, gst_memory_ref (mem), TRUE);
961 GST_BUFFER_FLAG_UNSET (newbuf, GST_BUFFER_FLAG_TAG_MEMORY);
968 GST_CAT_WARNING (GST_CAT_BUFFER,
969 "failed to allocate %" G_GSIZE_FORMAT " bytes", size);
975 * gst_buffer_new_wrapped_full:
976 * @flags: #GstMemoryFlags
977 * @data: (array length=size) (element-type guint8) (transfer none): data to wrap
978 * @maxsize: allocated size of @data
979 * @offset: offset in @data
980 * @size: size of valid data
981 * @user_data: (allow-none): user_data
982 * @notify: (allow-none) (scope async) (closure user_data): called with @user_data when the memory is freed
984 * Allocates a new buffer that wraps the given memory. @data must point to
985 * @maxsize of memory, the wrapped buffer will have the region from @offset and
988 * When the buffer is destroyed, @notify will be called with @user_data.
990 * The prefix/padding must be filled with 0 if @flags contains
991 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
993 * Returns: (transfer full): a new #GstBuffer
996 gst_buffer_new_wrapped_full (GstMemoryFlags flags, gpointer data,
997 gsize maxsize, gsize offset, gsize size, gpointer user_data,
998 GDestroyNotify notify)
1003 newbuf = gst_buffer_new ();
1005 gst_memory_new_wrapped (flags, data, maxsize, offset, size, user_data,
1007 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
1008 _memory_add (newbuf, -1, mem);
1009 GST_BUFFER_FLAG_UNSET (newbuf, GST_BUFFER_FLAG_TAG_MEMORY);
1015 * gst_buffer_new_wrapped:
1016 * @data: (array length=size) (element-type guint8) (transfer full): data to wrap
1017 * @size: allocated size of @data
1019 * Creates a new buffer that wraps the given @data. The memory will be freed
1020 * with g_free() and will be marked writable.
1022 * Returns: (transfer full): a new #GstBuffer
1025 gst_buffer_new_wrapped (gpointer data, gsize size)
1027 return gst_buffer_new_wrapped_full (0, data, size, 0, size, data, g_free);
1031 * gst_buffer_new_wrapped_bytes:
1032 * @bytes: (transfer none): a #GBytes to wrap
1034 * Creates a new #GstBuffer that wraps the given @bytes. The data inside
1035 * @bytes cannot be %NULL and the resulting buffer will be marked as read only.
1037 * Returns: (transfer full): a new #GstBuffer wrapping @bytes
1042 gst_buffer_new_wrapped_bytes (GBytes * bytes)
1047 g_return_val_if_fail (bytes != NULL, NULL);
1048 bytes_data = (guint8 *) g_bytes_get_data (bytes, &size);
1049 g_return_val_if_fail (bytes_data != NULL, NULL);
1051 return gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, bytes_data,
1052 size, 0, size, g_bytes_ref (bytes), (GDestroyNotify) g_bytes_unref);
1056 * gst_buffer_new_memdup:
1057 * @data: (array length=size) (element-type guint8) (transfer none): data to copy into new buffer
1058 * @size: size of @data in bytes
1060 * Creates a new buffer of size @size and fills it with a copy of @data.
1062 * Returns: (transfer full): a new #GstBuffer
1067 gst_buffer_new_memdup (gconstpointer data, gsize size)
1069 gpointer data2 = g_memdup2 (data, size);
1071 return gst_buffer_new_wrapped_full (0, data2, size, 0, size, data2, g_free);
1075 * gst_buffer_n_memory:
1076 * @buffer: a #GstBuffer.
1078 * Gets the amount of memory blocks that this buffer has. This amount is never
1079 * larger than what gst_buffer_get_max_memory() returns.
1081 * Returns: the number of memory blocks this buffer is made of.
1084 gst_buffer_n_memory (GstBuffer * buffer)
1086 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1088 return GST_BUFFER_MEM_LEN (buffer);
1092 * gst_buffer_prepend_memory:
1093 * @buffer: a #GstBuffer.
1094 * @mem: (transfer full): a #GstMemory.
1096 * Prepends the memory block @mem to @buffer. This function takes
1097 * ownership of @mem and thus doesn't increase its refcount.
1099 * This function is identical to gst_buffer_insert_memory() with an index of 0.
1100 * See gst_buffer_insert_memory() for more details.
1103 gst_buffer_prepend_memory (GstBuffer * buffer, GstMemory * mem)
1105 gst_buffer_insert_memory (buffer, 0, mem);
1109 * gst_buffer_append_memory:
1110 * @buffer: a #GstBuffer.
1111 * @mem: (transfer full): a #GstMemory.
1113 * Appends the memory block @mem to @buffer. This function takes
1114 * ownership of @mem and thus doesn't increase its refcount.
1116 * This function is identical to gst_buffer_insert_memory() with an index of -1.
1117 * See gst_buffer_insert_memory() for more details.
1120 gst_buffer_append_memory (GstBuffer * buffer, GstMemory * mem)
1122 gst_buffer_insert_memory (buffer, -1, mem);
1126 * gst_buffer_insert_memory:
1127 * @buffer: a #GstBuffer.
1128 * @idx: the index to add the memory at, or -1 to append it to the end
1129 * @mem: (transfer full): a #GstMemory.
1131 * Inserts the memory block @mem into @buffer at @idx. This function takes ownership
1132 * of @mem and thus doesn't increase its refcount.
1134 * Only gst_buffer_get_max_memory() can be added to a buffer. If more memory is
1135 * added, existing memory blocks will automatically be merged to make room for
1139 gst_buffer_insert_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
1143 g_return_if_fail (GST_IS_BUFFER (buffer));
1144 g_return_if_fail (gst_buffer_is_writable (buffer));
1145 g_return_if_fail (mem != NULL);
1146 g_return_if_fail (idx == -1 ||
1147 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)));
1149 tmp = _memory_get_exclusive_reference (mem);
1150 g_return_if_fail (tmp != NULL);
1151 gst_memory_unref (mem);
1152 _memory_add (buffer, idx, tmp);
1156 _get_mapped (GstBuffer * buffer, guint idx, GstMapInfo * info,
1159 GstMemory *mem, *mapped;
1161 mem = gst_memory_ref (GST_BUFFER_MEM_PTR (buffer, idx));
1163 mapped = gst_memory_make_mapped (mem, info, flags);
1165 if (mapped != mem) {
1166 /* memory changed, lock new memory */
1167 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (mapped),
1168 GST_MINI_OBJECT_CAST (buffer));
1169 gst_memory_lock (mapped, GST_LOCK_FLAG_EXCLUSIVE);
1170 GST_BUFFER_MEM_PTR (buffer, idx) = mapped;
1171 /* unlock old memory */
1172 gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
1173 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (mem),
1174 GST_MINI_OBJECT_CAST (buffer));
1175 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1177 gst_memory_unref (mem);
1183 * gst_buffer_peek_memory:
1184 * @buffer: a #GstBuffer.
1187 * Gets the memory block at @idx in @buffer. The memory block stays valid until
1188 * the memory block in @buffer is removed, replaced or merged, typically with
1189 * any call that modifies the memory in @buffer.
1191 * Returns: (transfer none) (nullable): the #GstMemory at @idx.
1194 gst_buffer_peek_memory (GstBuffer * buffer, guint idx)
1196 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1197 g_return_val_if_fail (idx < GST_BUFFER_MEM_LEN (buffer), NULL);
1199 return GST_BUFFER_MEM_PTR (buffer, idx);
1203 * gst_buffer_get_memory:
1204 * @buffer: a #GstBuffer.
1207 * Gets the memory block at index @idx in @buffer.
1209 * Returns: (transfer full) (nullable): a #GstMemory that contains the data of the
1210 * memory block at @idx.
1213 gst_buffer_get_memory (GstBuffer * buffer, guint idx)
1215 return gst_buffer_get_memory_range (buffer, idx, 1);
1219 * gst_buffer_get_all_memory:
1220 * @buffer: a #GstBuffer.
1222 * Gets all the memory blocks in @buffer. The memory blocks will be merged
1223 * into one large #GstMemory.
1225 * Returns: (transfer full) (nullable): a #GstMemory that contains the merged memory.
1228 gst_buffer_get_all_memory (GstBuffer * buffer)
1230 return gst_buffer_get_memory_range (buffer, 0, -1);
1234 * gst_buffer_get_memory_range:
1235 * @buffer: a #GstBuffer.
1239 * Gets @length memory blocks in @buffer starting at @idx. The memory blocks will
1240 * be merged into one large #GstMemory.
1242 * If @length is -1, all memory starting from @idx is merged.
1244 * Returns: (transfer full) (nullable): a #GstMemory that contains the merged data of @length
1245 * blocks starting at @idx.
1248 gst_buffer_get_memory_range (GstBuffer * buffer, guint idx, gint length)
1252 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
1254 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1255 len = GST_BUFFER_MEM_LEN (buffer);
1256 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1257 (length == -1 && idx < len) || (length > 0 && length + idx <= len), NULL);
1262 return _get_merged_memory (buffer, idx, length);
1266 * gst_buffer_replace_memory:
1267 * @buffer: a #GstBuffer.
1269 * @mem: (transfer full): a #GstMemory
1271 * Replaces the memory block at index @idx in @buffer with @mem.
1274 gst_buffer_replace_memory (GstBuffer * buffer, guint idx, GstMemory * mem)
1276 gst_buffer_replace_memory_range (buffer, idx, 1, mem);
1280 * gst_buffer_replace_all_memory:
1281 * @buffer: a #GstBuffer.
1282 * @mem: (transfer full): a #GstMemory
1284 * Replaces all memory in @buffer with @mem.
1287 gst_buffer_replace_all_memory (GstBuffer * buffer, GstMemory * mem)
1289 gst_buffer_replace_memory_range (buffer, 0, -1, mem);
1293 * gst_buffer_replace_memory_range:
1294 * @buffer: a #GstBuffer.
1296 * @length: a length, should not be 0
1297 * @mem: (transfer full): a #GstMemory
1299 * Replaces @length memory blocks in @buffer starting at @idx with @mem.
1301 * If @length is -1, all memory starting from @idx will be removed and
1302 * replaced with @mem.
1304 * @buffer should be writable.
1307 gst_buffer_replace_memory_range (GstBuffer * buffer, guint idx, gint length,
1312 g_return_if_fail (GST_IS_BUFFER (buffer));
1313 g_return_if_fail (gst_buffer_is_writable (buffer));
1315 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d, %p", idx, length, mem);
1317 len = GST_BUFFER_MEM_LEN (buffer);
1318 g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
1319 (length == -1 && idx < len) || (length > 0 && length + idx <= len));
1324 _replace_memory (buffer, len, idx, length, mem);
1328 * gst_buffer_remove_memory:
1329 * @buffer: a #GstBuffer.
1332 * Removes the memory block in @b at index @i.
1335 gst_buffer_remove_memory (GstBuffer * buffer, guint idx)
1337 gst_buffer_remove_memory_range (buffer, idx, 1);
1341 * gst_buffer_remove_all_memory:
1342 * @buffer: a #GstBuffer.
1344 * Removes all the memory blocks in @buffer.
1347 gst_buffer_remove_all_memory (GstBuffer * buffer)
1349 if (GST_BUFFER_MEM_LEN (buffer))
1350 gst_buffer_remove_memory_range (buffer, 0, -1);
1354 * gst_buffer_remove_memory_range:
1355 * @buffer: a #GstBuffer.
1359 * Removes @length memory blocks in @buffer starting from @idx.
1361 * @length can be -1, in which case all memory starting from @idx is removed.
1364 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, gint length)
1368 g_return_if_fail (GST_IS_BUFFER (buffer));
1369 g_return_if_fail (gst_buffer_is_writable (buffer));
1371 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
1373 len = GST_BUFFER_MEM_LEN (buffer);
1374 g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
1375 (length == -1 && idx < len) || length + idx <= len);
1380 _replace_memory (buffer, len, idx, length, NULL);
1384 * gst_buffer_find_memory:
1385 * @buffer: a #GstBuffer.
1386 * @offset: an offset
1388 * @idx: (out): pointer to index
1389 * @length: (out): pointer to length
1390 * @skip: (out): pointer to skip
1392 * Finds the memory blocks that span @size bytes starting from @offset
1395 * When this function returns %TRUE, @idx will contain the index of the first
1396 * memory block where the byte for @offset can be found and @length contains the
1397 * number of memory blocks containing the @size remaining bytes. @skip contains
1398 * the number of bytes to skip in the memory block at @idx to get to the byte
1401 * @size can be -1 to get all the memory blocks after @idx.
1403 * Returns: %TRUE when @size bytes starting from @offset could be found in
1404 * @buffer and @idx, @length and @skip will be filled.
1407 gst_buffer_find_memory (GstBuffer * buffer, gsize offset, gsize size,
1408 guint * idx, guint * length, gsize * skip)
1410 guint i, len, found;
1412 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1413 g_return_val_if_fail (idx != NULL, FALSE);
1414 g_return_val_if_fail (length != NULL, FALSE);
1415 g_return_val_if_fail (skip != NULL, FALSE);
1417 len = GST_BUFFER_MEM_LEN (buffer);
1420 for (i = 0; i < len; i++) {
1424 mem = GST_BUFFER_MEM_PTR (buffer, i);
1428 /* block before offset, or empty block, skip */
1431 /* block after offset */
1433 /* first block, remember index and offset */
1437 /* return remaining blocks */
1444 /* count the amount of found bytes */
1446 if (found >= size) {
1447 /* we have enough bytes */
1448 *length = i - *idx + 1;
1457 * gst_buffer_is_memory_range_writable:
1458 * @buffer: a #GstBuffer.
1460 * @length: a length, should not be 0
1462 * Checks if @length memory blocks in @buffer starting from @idx are writable.
1464 * @length can be -1 to check all the memory blocks after @idx.
1466 * Note that this function does not check if @buffer is writable, use
1467 * gst_buffer_is_writable() to check that if needed.
1469 * Returns: %TRUE if the memory range is writable
1474 gst_buffer_is_memory_range_writable (GstBuffer * buffer, guint idx, gint length)
1478 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1480 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
1482 len = GST_BUFFER_MEM_LEN (buffer);
1483 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1484 (length == -1 && idx < len) || (length > 0 && length + idx <= len),
1492 for (i = 0; i < len; i++) {
1493 if (!gst_memory_is_writable (GST_BUFFER_MEM_PTR (buffer, i + idx)))
1500 * gst_buffer_is_all_memory_writable:
1501 * @buffer: a #GstBuffer.
1503 * Checks if all memory blocks in @buffer are writable.
1505 * Note that this function does not check if @buffer is writable, use
1506 * gst_buffer_is_writable() to check that if needed.
1508 * Returns: %TRUE if all memory blocks in @buffer are writable
1513 gst_buffer_is_all_memory_writable (GstBuffer * buffer)
1515 return gst_buffer_is_memory_range_writable (buffer, 0, -1);
1519 * gst_buffer_get_sizes:
1520 * @buffer: a #GstBuffer.
1521 * @offset: (out) (allow-none): a pointer to the offset
1522 * @maxsize: (out) (allow-none): a pointer to the maxsize
1524 * Gets the total size of the memory blocks in @buffer.
1526 * When not %NULL, @offset will contain the offset of the data in the
1527 * first memory block in @buffer and @maxsize will contain the sum of
1528 * the size and @offset and the amount of extra padding on the last
1529 * memory block. @offset and @maxsize can be used to resize the
1530 * buffer memory blocks with gst_buffer_resize().
1532 * Returns: total size of the memory blocks in @buffer.
1535 gst_buffer_get_sizes (GstBuffer * buffer, gsize * offset, gsize * maxsize)
1537 return gst_buffer_get_sizes_range (buffer, 0, -1, offset, maxsize);
1541 * gst_buffer_get_size:
1542 * @buffer: a #GstBuffer.
1544 * Gets the total size of the memory blocks in @buffer.
1546 * Returns: total size of the memory blocks in @buffer.
1549 gst_buffer_get_size (GstBuffer * buffer)
1554 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1557 len = GST_BUFFER_MEM_LEN (buffer);
1558 for (i = 0, size = 0; i < len; i++)
1559 size += GST_BUFFER_MEM_PTR (buffer, i)->size;
1564 * gst_buffer_get_sizes_range:
1565 * @buffer: a #GstBuffer.
1568 * @offset: (out) (allow-none): a pointer to the offset
1569 * @maxsize: (out) (allow-none): a pointer to the maxsize
1571 * Gets the total size of @length memory blocks stating from @idx in @buffer.
1573 * When not %NULL, @offset will contain the offset of the data in the
1574 * memory block in @buffer at @idx and @maxsize will contain the sum of the size
1575 * and @offset and the amount of extra padding on the memory block at @idx +
1577 * @offset and @maxsize can be used to resize the buffer memory blocks with
1578 * gst_buffer_resize_range().
1580 * Returns: total size of @length memory blocks starting at @idx in @buffer.
1583 gst_buffer_get_sizes_range (GstBuffer * buffer, guint idx, gint length,
1584 gsize * offset, gsize * maxsize)
1590 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1591 len = GST_BUFFER_MEM_LEN (buffer);
1592 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1593 (length == -1 && idx < len) || (length + idx <= len), 0);
1598 if (G_LIKELY (length == 1)) {
1600 mem = GST_BUFFER_MEM_PTR (buffer, idx);
1601 size = gst_memory_get_sizes (mem, offset, maxsize);
1602 } else if (offset == NULL && maxsize == NULL) {
1608 for (i = idx; i < end; i++) {
1609 mem = GST_BUFFER_MEM_PTR (buffer, i);
1617 size = offs = extra = 0;
1618 for (i = idx; i < end; i++) {
1621 mem = GST_BUFFER_MEM_PTR (buffer, i);
1622 s = gst_memory_get_sizes (mem, &o, &ms);
1626 /* first size, take accumulated data before as the offset */
1630 /* save the amount of data after this block */
1631 extra = ms - (o + s);
1633 /* empty block, add as extra */
1640 *maxsize = offs + size + extra;
1646 * gst_buffer_resize:
1647 * @buffer: a #GstBuffer.
1648 * @offset: the offset adjustment
1649 * @size: the new size or -1 to just adjust the offset
1651 * Sets the offset and total size of the memory blocks in @buffer.
1654 gst_buffer_resize (GstBuffer * buffer, gssize offset, gssize size)
1656 gst_buffer_resize_range (buffer, 0, -1, offset, size);
1660 * gst_buffer_set_size:
1661 * @buffer: a #GstBuffer.
1662 * @size: the new size
1664 * Sets the total size of the memory blocks in @buffer.
1667 gst_buffer_set_size (GstBuffer * buffer, gssize size)
1669 gst_buffer_resize_range (buffer, 0, -1, 0, size);
1673 * gst_buffer_resize_range:
1674 * @buffer: a #GstBuffer.
1677 * @offset: the offset adjustment
1678 * @size: the new size or -1 to just adjust the offset
1680 * Sets the total size of the @length memory blocks starting at @idx in
1683 * Returns: %TRUE if resizing succeeded, %FALSE otherwise.
1686 gst_buffer_resize_range (GstBuffer * buffer, guint idx, gint length,
1687 gssize offset, gssize size)
1690 gsize bsize, bufsize, bufoffs, bufmax;
1692 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
1693 g_return_val_if_fail (size >= -1, FALSE);
1695 len = GST_BUFFER_MEM_LEN (buffer);
1696 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1697 (length == -1 && idx < len) || (length + idx <= len), FALSE);
1702 bufsize = gst_buffer_get_sizes_range (buffer, idx, length, &bufoffs, &bufmax);
1704 GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSSIZE_FORMAT
1705 " size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%"
1706 G_GSIZE_FORMAT, buffer, offset, size, bufsize, bufoffs, bufmax);
1708 /* we can't go back further than the current offset or past the end of the
1710 g_return_val_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
1711 && bufoffs + offset <= bufmax), FALSE);
1713 g_return_val_if_fail (bufsize >= offset, FALSE);
1714 size = bufsize - offset;
1716 g_return_val_if_fail (bufmax >= bufoffs + offset + size, FALSE);
1719 if (offset == 0 && size == bufsize)
1724 for (i = idx; i < end; i++) {
1728 mem = GST_BUFFER_MEM_PTR (buffer, i);
1732 /* last buffer always gets resized to the remaining size */
1735 /* shrink buffers before the offset */
1736 else if ((gssize) bsize <= offset) {
1738 noffs = offset - bsize;
1741 /* clip other buffers */
1743 left = MIN (bsize - offset, size);
1745 if (offset != 0 || left != bsize) {
1746 if (gst_memory_is_writable (mem)) {
1747 gst_memory_resize (mem, offset, left);
1749 GstMemory *newmem = NULL;
1751 if (!GST_MEMORY_IS_NO_SHARE (mem))
1752 newmem = gst_memory_share (mem, offset, left);
1755 newmem = gst_memory_copy (mem, offset, left);
1760 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (newmem),
1761 GST_MINI_OBJECT_CAST (buffer));
1762 gst_memory_lock (newmem, GST_LOCK_FLAG_EXCLUSIVE);
1763 GST_BUFFER_MEM_PTR (buffer, i) = newmem;
1764 gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
1765 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (mem),
1766 GST_MINI_OBJECT_CAST (buffer));
1767 gst_memory_unref (mem);
1769 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1782 * @buffer: a #GstBuffer.
1783 * @info: (out caller-allocates): info about the mapping
1784 * @flags: flags for the mapping
1786 * Fills @info with the #GstMapInfo of all merged memory blocks in @buffer.
1788 * @flags describe the desired access of the memory. When @flags is
1789 * #GST_MAP_WRITE, @buffer should be writable (as returned from
1790 * gst_buffer_is_writable()).
1792 * When @buffer is writable but the memory isn't, a writable copy will
1793 * automatically be created and returned. The readonly copy of the
1794 * buffer memory will then also be replaced with this writable copy.
1796 * The memory in @info should be unmapped with gst_buffer_unmap() after
1799 * Returns: %TRUE if the map succeeded and @info contains valid data.
1802 gst_buffer_map (GstBuffer * buffer, GstMapInfo * info, GstMapFlags flags)
1804 return gst_buffer_map_range (buffer, 0, -1, info, flags);
1808 * gst_buffer_map_range:
1809 * @buffer: a #GstBuffer.
1812 * @info: (out caller-allocates): info about the mapping
1813 * @flags: flags for the mapping
1815 * Fills @info with the #GstMapInfo of @length merged memory blocks
1816 * starting at @idx in @buffer. When @length is -1, all memory blocks starting
1817 * from @idx are merged and mapped.
1819 * @flags describe the desired access of the memory. When @flags is
1820 * #GST_MAP_WRITE, @buffer should be writable (as returned from
1821 * gst_buffer_is_writable()).
1823 * When @buffer is writable but the memory isn't, a writable copy will
1824 * automatically be created and returned. The readonly copy of the buffer memory
1825 * will then also be replaced with this writable copy.
1827 * The memory in @info should be unmapped with gst_buffer_unmap() after usage.
1829 * Returns: %TRUE if the map succeeded and @info contains valid
1833 gst_buffer_map_range (GstBuffer * buffer, guint idx, gint length,
1834 GstMapInfo * info, GstMapFlags flags)
1836 GstMemory *mem, *nmem;
1837 gboolean write, writable;
1840 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1841 g_return_val_if_fail (info != NULL, FALSE);
1842 len = GST_BUFFER_MEM_LEN (buffer);
1843 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1844 (length == -1 && idx < len) || (length > 0
1845 && length + idx <= len), FALSE);
1847 GST_CAT_LOG (GST_CAT_BUFFER, "buffer %p, idx %u, length %d, flags %04x",
1848 buffer, idx, length, flags);
1850 write = (flags & GST_MAP_WRITE) != 0;
1851 writable = gst_buffer_is_writable (buffer);
1853 /* check if we can write when asked for write access */
1854 if (G_UNLIKELY (write && !writable))
1860 mem = _get_merged_memory (buffer, idx, length);
1861 if (G_UNLIKELY (mem == NULL))
1864 /* now try to map */
1865 nmem = gst_memory_make_mapped (mem, info, flags);
1866 if (G_UNLIKELY (nmem == NULL))
1869 /* if we merged or when the map returned a different memory, we try to replace
1870 * the memory in the buffer */
1871 if (G_UNLIKELY (length > 1 || nmem != mem)) {
1872 /* if the buffer is writable, replace the memory */
1874 _replace_memory (buffer, len, idx, length, gst_memory_ref (nmem));
1877 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
1878 "temporary mapping for memory %p in buffer %p", nmem, buffer);
1887 GST_WARNING ("write map requested on non-writable buffer");
1888 g_critical ("write map requested on non-writable buffer");
1889 memset (info, 0, sizeof (GstMapInfo));
1894 /* empty buffer, we need to return NULL */
1895 GST_DEBUG ("can't get buffer memory");
1896 memset (info, 0, sizeof (GstMapInfo));
1901 GST_DEBUG ("cannot map memory");
1902 memset (info, 0, sizeof (GstMapInfo));
1909 * @buffer: a #GstBuffer.
1910 * @info: a #GstMapInfo
1912 * Releases the memory previously mapped with gst_buffer_map().
1915 gst_buffer_unmap (GstBuffer * buffer, GstMapInfo * info)
1917 g_return_if_fail (GST_IS_BUFFER (buffer));
1918 g_return_if_fail (info != NULL);
1920 _gst_buffer_map_info_clear ((GstBufferMapInfo *) info);
1925 * @buffer: a #GstBuffer.
1926 * @offset: the offset to fill
1927 * @src: (array length=size) (element-type guint8): the source address
1928 * @size: the size to fill
1930 * Copies @size bytes from @src to @buffer at @offset.
1932 * Returns: The amount of bytes copied. This value can be lower than @size
1933 * when @buffer did not contain enough data.
1936 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
1940 const guint8 *ptr = src;
1942 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1943 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1944 g_return_val_if_fail (src != NULL || size == 0, 0);
1946 GST_CAT_LOG (GST_CAT_BUFFER,
1947 "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
1950 len = GST_BUFFER_MEM_LEN (buffer);
1953 for (i = 0; i < len && left > 0; i++) {
1958 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1959 if (info.size > offset) {
1960 /* we have enough */
1961 tocopy = MIN (info.size - offset, left);
1962 memcpy ((guint8 *) info.data + offset, ptr, tocopy);
1967 /* offset past buffer, skip */
1968 offset -= info.size;
1970 gst_memory_unmap (mem, &info);
1976 * gst_buffer_extract:
1977 * @buffer: a #GstBuffer.
1978 * @offset: the offset to extract
1979 * @dest: (out caller-allocates) (array length=size) (element-type guint8):
1980 * the destination address
1981 * @size: the size to extract
1983 * Copies @size bytes starting from @offset in @buffer to @dest.
1985 * Returns: The amount of bytes extracted. This value can be lower than @size
1986 * when @buffer did not contain enough data.
1989 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
1994 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1995 g_return_val_if_fail (dest != NULL, 0);
1997 GST_CAT_LOG (GST_CAT_BUFFER,
1998 "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
2001 len = GST_BUFFER_MEM_LEN (buffer);
2004 for (i = 0; i < len && left > 0; i++) {
2009 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
2010 if (info.size > offset) {
2011 /* we have enough */
2012 tocopy = MIN (info.size - offset, left);
2013 memcpy (ptr, (guint8 *) info.data + offset, tocopy);
2018 /* offset past buffer, skip */
2019 offset -= info.size;
2021 gst_memory_unmap (mem, &info);
2027 * gst_buffer_memcmp:
2028 * @buffer: a #GstBuffer.
2029 * @offset: the offset in @buffer
2030 * @mem: (array length=size) (element-type guint8): the memory to compare
2031 * @size: the size to compare
2033 * Compares @size bytes starting from @offset in @buffer with the memory in @mem.
2035 * Returns: 0 if the memory is equal.
2038 gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem,
2042 const guint8 *ptr = mem;
2045 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
2046 g_return_val_if_fail (mem != NULL, 0);
2048 GST_CAT_LOG (GST_CAT_BUFFER,
2049 "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
2052 if (G_UNLIKELY (gst_buffer_get_size (buffer) < offset + size))
2055 len = GST_BUFFER_MEM_LEN (buffer);
2057 for (i = 0; i < len && size > 0 && res == 0; i++) {
2062 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
2063 if (info.size > offset) {
2064 /* we have enough */
2065 tocmp = MIN (info.size - offset, size);
2066 res = memcmp (ptr, (guint8 *) info.data + offset, tocmp);
2071 /* offset past buffer, skip */
2072 offset -= info.size;
2074 gst_memory_unmap (mem, &info);
2080 * gst_buffer_memset:
2081 * @buffer: a #GstBuffer.
2082 * @offset: the offset in @buffer
2083 * @val: the value to set
2084 * @size: the size to set
2086 * Fills @buf with @size bytes with @val starting from @offset.
2088 * Returns: The amount of bytes filled. This value can be lower than @size
2089 * when @buffer did not contain enough data.
2092 gst_buffer_memset (GstBuffer * buffer, gsize offset, guint8 val, gsize size)
2096 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
2097 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
2099 GST_CAT_LOG (GST_CAT_BUFFER,
2100 "buffer %p, offset %" G_GSIZE_FORMAT ", val %02x, size %" G_GSIZE_FORMAT,
2101 buffer, offset, val, size);
2103 len = GST_BUFFER_MEM_LEN (buffer);
2106 for (i = 0; i < len && left > 0; i++) {
2111 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
2112 if (info.size > offset) {
2113 /* we have enough */
2114 toset = MIN (info.size - offset, left);
2115 memset ((guint8 *) info.data + offset, val, toset);
2119 /* offset past buffer, skip */
2120 offset -= info.size;
2122 gst_memory_unmap (mem, &info);
2128 * gst_buffer_copy_region:
2129 * @parent: a #GstBuffer.
2130 * @flags: the #GstBufferCopyFlags
2131 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
2133 * @size: the size of the new #GstBuffer sub-buffer, in bytes. If -1, all
2136 * Creates a sub-buffer from @parent at @offset and @size.
2137 * This sub-buffer uses the actual memory space of the parent buffer.
2138 * This function will copy the offset and timestamp fields when the
2139 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
2140 * #GST_BUFFER_OFFSET_NONE.
2141 * If @offset equals 0 and @size equals the total size of @buffer, the
2142 * duration and offset end fields are also copied. If not they will be set
2143 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
2145 * Returns: (transfer full) (nullable): the new #GstBuffer or %NULL if copying
2149 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
2150 gsize offset, gsize size)
2154 g_return_val_if_fail (buffer != NULL, NULL);
2156 /* create the new buffer */
2157 copy = gst_buffer_new ();
2159 GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
2160 "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
2162 if (!gst_buffer_copy_into (copy, buffer, flags, offset, size))
2163 gst_buffer_replace (©, NULL);
2169 * gst_buffer_append:
2170 * @buf1: (transfer full): the first source #GstBuffer to append.
2171 * @buf2: (transfer full): the second source #GstBuffer to append.
2173 * Appends all the memory from @buf2 to @buf1. The result buffer will contain a
2174 * concatenation of the memory of @buf1 and @buf2.
2176 * Returns: (transfer full): the new #GstBuffer that contains the memory
2177 * of the two source buffers.
2180 gst_buffer_append (GstBuffer * buf1, GstBuffer * buf2)
2182 return gst_buffer_append_region (buf1, buf2, 0, -1);
2186 * gst_buffer_append_region:
2187 * @buf1: (transfer full): the first source #GstBuffer to append.
2188 * @buf2: (transfer full): the second source #GstBuffer to append.
2189 * @offset: the offset in @buf2
2190 * @size: the size or -1 of @buf2
2192 * Appends @size bytes at @offset from @buf2 to @buf1. The result buffer will
2193 * contain a concatenation of the memory of @buf1 and the requested region of
2196 * Returns: (transfer full): the new #GstBuffer that contains the memory
2197 * of the two source buffers.
2200 gst_buffer_append_region (GstBuffer * buf1, GstBuffer * buf2, gssize offset,
2205 g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
2206 g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
2208 buf1 = gst_buffer_make_writable (buf1);
2209 buf2 = gst_buffer_make_writable (buf2);
2211 gst_buffer_resize (buf2, offset, size);
2213 len = GST_BUFFER_MEM_LEN (buf2);
2214 for (i = 0; i < len; i++) {
2217 mem = GST_BUFFER_MEM_PTR (buf2, i);
2218 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (mem),
2219 GST_MINI_OBJECT_CAST (buf2));
2220 GST_BUFFER_MEM_PTR (buf2, i) = NULL;
2221 _memory_add (buf1, -1, mem);
2224 GST_BUFFER_MEM_LEN (buf2) = 0;
2225 GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_TAG_MEMORY);
2226 gst_buffer_unref (buf2);
2232 * gst_buffer_get_meta:
2233 * @buffer: a #GstBuffer
2234 * @api: the #GType of an API
2236 * Gets the metadata for @api on buffer. When there is no such metadata, %NULL is
2237 * returned. If multiple metadata with the given @api are attached to this
2238 * buffer only the first one is returned. To handle multiple metadata with a
2239 * given API use gst_buffer_iterate_meta() or gst_buffer_foreach_meta() instead
2240 * and check the `meta->info.api` member for the API type.
2242 * Returns: (transfer none) (nullable): the metadata for @api on @buffer.
2245 gst_buffer_get_meta (GstBuffer * buffer, GType api)
2248 GstMeta *result = NULL;
2250 g_return_val_if_fail (buffer != NULL, NULL);
2251 g_return_val_if_fail (api != 0, NULL);
2253 /* find GstMeta of the requested API */
2254 for (item = GST_BUFFER_META (buffer); item; item = item->next) {
2255 GstMeta *meta = &item->meta;
2256 if (meta->info->api == api) {
2265 * gst_buffer_get_n_meta:
2266 * @buffer: a #GstBuffer
2267 * @api_type: the #GType of an API
2269 * Returns: number of metas of type @api_type on @buffer.
2274 gst_buffer_get_n_meta (GstBuffer * buffer, GType api_type)
2276 gpointer state = NULL;
2280 while ((meta = gst_buffer_iterate_meta_filtered (buffer, &state, api_type)))
2287 * gst_buffer_add_meta:
2288 * @buffer: a #GstBuffer
2289 * @info: a #GstMetaInfo
2290 * @params: params for @info
2292 * Adds metadata for @info to @buffer using the parameters in @params.
2294 * Returns: (transfer none) (nullable): the metadata for the api in @info on @buffer.
2297 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
2301 GstMeta *result = NULL;
2304 g_return_val_if_fail (buffer != NULL, NULL);
2305 g_return_val_if_fail (info != NULL, NULL);
2306 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
2308 /* create a new slice */
2309 size = ITEM_SIZE (info);
2310 /* We warn in gst_meta_register() about metas without
2311 * init function but let's play safe here and prevent
2312 * uninitialized memory
2314 if (!info->init_func)
2315 item = g_slice_alloc0 (size);
2317 item = g_slice_alloc (size);
2318 result = &item->meta;
2319 result->info = info;
2320 result->flags = GST_META_FLAG_NONE;
2321 GST_CAT_DEBUG (GST_CAT_BUFFER,
2322 "alloc metadata %p (%s) of size %" G_GSIZE_FORMAT, result,
2323 g_type_name (info->type), info->size);
2325 /* call the init_func when needed */
2326 if (info->init_func)
2327 if (!info->init_func (result, params, buffer))
2330 item->seq_num = gst_atomic_int64_inc (&meta_seq);
2333 if (!GST_BUFFER_META (buffer)) {
2334 GST_BUFFER_META (buffer) = item;
2335 GST_BUFFER_TAIL_META (buffer) = item;
2337 GST_BUFFER_TAIL_META (buffer)->next = item;
2338 GST_BUFFER_TAIL_META (buffer) = item;
2345 g_slice_free1 (size, item);
2351 * gst_buffer_remove_meta:
2352 * @buffer: a #GstBuffer
2355 * Removes the metadata for @meta on @buffer.
2357 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
2358 * metadata was on @buffer.
2361 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
2363 GstMetaItem *walk, *prev;
2365 g_return_val_if_fail (buffer != NULL, FALSE);
2366 g_return_val_if_fail (meta != NULL, FALSE);
2367 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
2368 g_return_val_if_fail (!GST_META_FLAG_IS_SET (meta, GST_META_FLAG_LOCKED),
2371 /* find the metadata and delete */
2372 prev = GST_BUFFER_META (buffer);
2373 for (walk = prev; walk; walk = walk->next) {
2374 GstMeta *m = &walk->meta;
2376 const GstMetaInfo *info = meta->info;
2378 /* remove from list */
2379 if (GST_BUFFER_TAIL_META (buffer) == walk) {
2381 GST_BUFFER_TAIL_META (buffer) = prev;
2383 GST_BUFFER_TAIL_META (buffer) = NULL;
2386 if (GST_BUFFER_META (buffer) == walk)
2387 GST_BUFFER_META (buffer) = walk->next;
2389 prev->next = walk->next;
2391 /* call free_func if any */
2392 if (info->free_func)
2393 info->free_func (m, buffer);
2395 /* and free the slice */
2396 g_slice_free1 (ITEM_SIZE (info), walk);
2401 return walk != NULL;
2405 * gst_buffer_iterate_meta: (skip)
2406 * @buffer: a #GstBuffer
2407 * @state: (out caller-allocates): an opaque state pointer
2409 * Retrieves the next #GstMeta after @current. If @state points
2410 * to %NULL, the first metadata is returned.
2412 * @state will be updated with an opaque state pointer
2414 * Returns: (transfer none) (nullable): The next #GstMeta or %NULL
2415 * when there are no more items.
2418 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
2422 g_return_val_if_fail (buffer != NULL, NULL);
2423 g_return_val_if_fail (state != NULL, NULL);
2425 meta = (GstMetaItem **) state;
2427 /* state NULL, move to first item */
2428 *meta = GST_BUFFER_META (buffer);
2430 /* state !NULL, move to next item in list */
2431 *meta = (*meta)->next;
2434 return &(*meta)->meta;
2440 * gst_buffer_iterate_meta_filtered: (skip)
2441 * @buffer: a #GstBuffer
2442 * @state: (out caller-allocates): an opaque state pointer
2443 * @meta_api_type: only return #GstMeta of this type
2445 * Retrieves the next #GstMeta of type @meta_api_type after the current one
2446 * according to @state. If @state points to %NULL, the first metadata of
2447 * type @meta_api_type is returned.
2449 * @state will be updated with an opaque state pointer
2451 * Returns: (transfer none) (nullable): The next #GstMeta of type
2452 * @meta_api_type or %NULL when there are no more items.
2457 gst_buffer_iterate_meta_filtered (GstBuffer * buffer, gpointer * state,
2458 GType meta_api_type)
2462 g_return_val_if_fail (buffer != NULL, NULL);
2463 g_return_val_if_fail (state != NULL, NULL);
2465 meta = (GstMetaItem **) state;
2467 /* state NULL, move to first item */
2468 *meta = GST_BUFFER_META (buffer);
2470 /* state !NULL, move to next item in list */
2471 *meta = (*meta)->next;
2473 while (*meta != NULL && (*meta)->meta.info->api != meta_api_type)
2474 *meta = (*meta)->next;
2477 return &(*meta)->meta;
2483 * gst_buffer_foreach_meta:
2484 * @buffer: a #GstBuffer
2485 * @func: (scope call): a #GstBufferForeachMetaFunc to call
2486 * @user_data: (closure): user data passed to @func
2488 * Calls @func with @user_data for each meta in @buffer.
2490 * @func can modify the passed meta pointer or its contents. The return value
2491 * of @func defines if this function returns or if the remaining metadata items
2492 * in the buffer should be skipped.
2494 * Returns: %FALSE when @func returned %FALSE for one of the metadata.
2497 gst_buffer_foreach_meta (GstBuffer * buffer, GstBufferForeachMetaFunc func,
2500 GstMetaItem *walk, *prev, *next;
2501 gboolean res = TRUE;
2503 g_return_val_if_fail (buffer != NULL, FALSE);
2504 g_return_val_if_fail (func != NULL, FALSE);
2506 /* find the metadata and delete */
2507 prev = GST_BUFFER_META (buffer);
2508 for (walk = prev; walk; walk = next) {
2511 m = new = &walk->meta;
2514 res = func (buffer, &new, user_data);
2517 const GstMetaInfo *info = m->info;
2519 GST_CAT_DEBUG (GST_CAT_BUFFER, "remove metadata %p (%s)", m,
2520 g_type_name (info->type));
2522 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
2523 g_return_val_if_fail (!GST_META_FLAG_IS_SET (m, GST_META_FLAG_LOCKED),
2526 if (GST_BUFFER_TAIL_META (buffer) == walk) {
2528 GST_BUFFER_TAIL_META (buffer) = prev;
2530 GST_BUFFER_TAIL_META (buffer) = NULL;
2533 /* remove from list */
2534 if (GST_BUFFER_META (buffer) == walk)
2535 prev = GST_BUFFER_META (buffer) = next;
2539 /* call free_func if any */
2540 if (info->free_func)
2541 info->free_func (m, buffer);
2543 /* and free the slice */
2544 g_slice_free1 (ITEM_SIZE (info), walk);
2555 * gst_buffer_extract_dup:
2556 * @buffer: a #GstBuffer
2557 * @offset: the offset to extract
2558 * @size: the size to extract
2559 * @dest: (array length=dest_size) (element-type guint8) (out): A pointer where
2560 * the destination array will be written. Might be %NULL if the size is 0.
2561 * @dest_size: (out): A location where the size of @dest can be written
2563 * Extracts a copy of at most @size bytes the data at @offset into
2564 * newly-allocated memory. @dest must be freed using g_free() when done.
2570 gst_buffer_extract_dup (GstBuffer * buffer, gsize offset, gsize size,
2571 gpointer * dest, gsize * dest_size)
2573 gsize real_size, alloc_size;
2575 real_size = gst_buffer_get_size (buffer);
2577 alloc_size = MIN (real_size - offset, size);
2578 if (alloc_size == 0) {
2582 *dest = g_malloc (alloc_size);
2583 *dest_size = gst_buffer_extract (buffer, offset, *dest, size);
2587 GST_DEBUG_CATEGORY_STATIC (gst_parent_buffer_meta_debug);
2590 * gst_buffer_add_parent_buffer_meta:
2591 * @buffer: (transfer none): a #GstBuffer
2592 * @ref: (transfer none): a #GstBuffer to ref
2594 * Adds a #GstParentBufferMeta to @buffer that holds a reference on
2595 * @ref until the buffer is freed.
2597 * Returns: (transfer none) (nullable): The #GstParentBufferMeta that was added to the buffer
2601 GstParentBufferMeta *
2602 gst_buffer_add_parent_buffer_meta (GstBuffer * buffer, GstBuffer * ref)
2604 GstParentBufferMeta *meta;
2606 g_return_val_if_fail (GST_IS_BUFFER (ref), NULL);
2609 (GstParentBufferMeta *) gst_buffer_add_meta (buffer,
2610 GST_PARENT_BUFFER_META_INFO, NULL);
2615 meta->buffer = gst_buffer_ref (ref);
2621 _gst_parent_buffer_meta_transform (GstBuffer * dest, GstMeta * meta,
2622 GstBuffer * buffer, GQuark type, gpointer data)
2624 GstParentBufferMeta *dmeta, *smeta;
2626 smeta = (GstParentBufferMeta *) meta;
2628 if (GST_META_TRANSFORM_IS_COPY (type)) {
2629 /* copy over the reference to the parent buffer.
2630 * Usually, this meta means we need to keep the parent buffer
2631 * alive because one of the child memories is in use, which
2632 * might not be the case if memory is deep copied or sub-regioned,
2633 * but we can't tell, so keep the meta */
2634 dmeta = gst_buffer_add_parent_buffer_meta (dest, smeta->buffer);
2638 GST_CAT_DEBUG (gst_parent_buffer_meta_debug,
2639 "copy buffer reference metadata");
2641 /* return FALSE, if transform type is not supported */
2648 _gst_parent_buffer_meta_free (GstParentBufferMeta * parent_meta,
2651 GST_CAT_DEBUG (gst_parent_buffer_meta_debug,
2652 "Dropping reference on buffer %p", parent_meta->buffer);
2653 gst_buffer_unref (parent_meta->buffer);
2657 _gst_parent_buffer_meta_init (GstParentBufferMeta * parent_meta,
2658 gpointer params, GstBuffer * buffer)
2662 if (g_once_init_enter (&_init)) {
2663 GST_DEBUG_CATEGORY_INIT (gst_parent_buffer_meta_debug, "parentbuffermeta",
2664 0, "parentbuffermeta");
2665 g_once_init_leave (&_init, 1);
2668 parent_meta->buffer = NULL;
2674 * gst_parent_buffer_meta_api_get_type: (attributes doc.skip=true)
2677 gst_parent_buffer_meta_api_get_type (void)
2679 static GType type = 0;
2680 static const gchar *tags[] = { GST_META_TAG_MEMORY_REFERENCE_STR, NULL };
2682 if (g_once_init_enter (&type)) {
2683 GType _type = gst_meta_api_type_register ("GstParentBufferMetaAPI", tags);
2684 g_once_init_leave (&type, _type);
2691 * gst_parent_buffer_meta_get_info:
2693 * Gets the global #GstMetaInfo describing the #GstParentBufferMeta meta.
2695 * Returns: (transfer none): The #GstMetaInfo
2700 gst_parent_buffer_meta_get_info (void)
2702 static const GstMetaInfo *meta_info = NULL;
2704 if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
2705 const GstMetaInfo *meta =
2706 gst_meta_register (gst_parent_buffer_meta_api_get_type (),
2707 "GstParentBufferMeta",
2708 sizeof (GstParentBufferMeta),
2709 (GstMetaInitFunction) _gst_parent_buffer_meta_init,
2710 (GstMetaFreeFunction) _gst_parent_buffer_meta_free,
2711 _gst_parent_buffer_meta_transform);
2712 g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) meta);
2718 GST_DEBUG_CATEGORY_STATIC (gst_reference_timestamp_meta_debug);
2721 * gst_buffer_add_reference_timestamp_meta:
2722 * @buffer: (transfer none): a #GstBuffer
2723 * @reference: (transfer none): identifier for the timestamp reference.
2724 * @timestamp: timestamp
2725 * @duration: duration, or %GST_CLOCK_TIME_NONE
2727 * Adds a #GstReferenceTimestampMeta to @buffer that holds a @timestamp and
2728 * optionally @duration based on a specific timestamp @reference. See the
2729 * documentation of #GstReferenceTimestampMeta for details.
2731 * Returns: (transfer none) (nullable): The #GstReferenceTimestampMeta that was added to the buffer
2735 GstReferenceTimestampMeta *
2736 gst_buffer_add_reference_timestamp_meta (GstBuffer * buffer,
2737 GstCaps * reference, GstClockTime timestamp, GstClockTime duration)
2739 GstReferenceTimestampMeta *meta;
2741 g_return_val_if_fail (GST_IS_CAPS (reference), NULL);
2742 g_return_val_if_fail (timestamp != GST_CLOCK_TIME_NONE, NULL);
2745 (GstReferenceTimestampMeta *) gst_buffer_add_meta (buffer,
2746 GST_REFERENCE_TIMESTAMP_META_INFO, NULL);
2751 meta->reference = gst_caps_ref (reference);
2752 meta->timestamp = timestamp;
2753 meta->duration = duration;
2759 * gst_buffer_get_reference_timestamp_meta:
2760 * @buffer: a #GstBuffer
2761 * @reference: (allow-none): a reference #GstCaps
2763 * Finds the first #GstReferenceTimestampMeta on @buffer that conforms to
2764 * @reference. Conformance is tested by checking if the meta's reference is a
2765 * subset of @reference.
2767 * Buffers can contain multiple #GstReferenceTimestampMeta metadata items.
2769 * Returns: (transfer none) (nullable): the #GstReferenceTimestampMeta or %NULL when there
2770 * is no such metadata on @buffer.
2774 GstReferenceTimestampMeta *
2775 gst_buffer_get_reference_timestamp_meta (GstBuffer * buffer,
2776 GstCaps * reference)
2778 gpointer state = NULL;
2780 const GstMetaInfo *info = GST_REFERENCE_TIMESTAMP_META_INFO;
2782 while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
2783 if (meta->info->api == info->api) {
2784 GstReferenceTimestampMeta *rmeta = (GstReferenceTimestampMeta *) meta;
2788 if (gst_caps_is_subset (rmeta->reference, reference))
2796 _gst_reference_timestamp_meta_transform (GstBuffer * dest, GstMeta * meta,
2797 GstBuffer * buffer, GQuark type, gpointer data)
2799 GstReferenceTimestampMeta *dmeta, *smeta;
2801 /* we copy over the reference timestamp meta, independent of transformation
2802 * that happens. If it applied to the original buffer, it still applies to
2803 * the new buffer as it refers to the time when the media was captured */
2804 smeta = (GstReferenceTimestampMeta *) meta;
2806 gst_buffer_add_reference_timestamp_meta (dest, smeta->reference,
2807 smeta->timestamp, smeta->duration);
2811 GST_CAT_DEBUG (gst_reference_timestamp_meta_debug,
2812 "copy reference timestamp metadata from buffer %p to %p", buffer, dest);
2818 _gst_reference_timestamp_meta_free (GstReferenceTimestampMeta * meta,
2821 if (meta->reference)
2822 gst_caps_unref (meta->reference);
2826 _gst_reference_timestamp_meta_init (GstReferenceTimestampMeta * meta,
2827 gpointer params, GstBuffer * buffer)
2831 if (g_once_init_enter (&_init)) {
2832 GST_DEBUG_CATEGORY_INIT (gst_reference_timestamp_meta_debug,
2833 "referencetimestampmeta", 0, "referencetimestampmeta");
2834 g_once_init_leave (&_init, 1);
2837 meta->reference = NULL;
2838 meta->timestamp = GST_CLOCK_TIME_NONE;
2839 meta->duration = GST_CLOCK_TIME_NONE;
2845 * gst_reference_timestamp_meta_api_get_type: (attributes doc.skip=true)
2848 gst_reference_timestamp_meta_api_get_type (void)
2850 static GType type = 0;
2851 static const gchar *tags[] = { NULL };
2853 if (g_once_init_enter (&type)) {
2855 gst_meta_api_type_register ("GstReferenceTimestampMetaAPI", tags);
2856 g_once_init_leave (&type, _type);
2863 * gst_reference_timestamp_meta_get_info:
2865 * Gets the global #GstMetaInfo describing the #GstReferenceTimestampMeta meta.
2867 * Returns: (transfer none): The #GstMetaInfo
2872 gst_reference_timestamp_meta_get_info (void)
2874 static const GstMetaInfo *meta_info = NULL;
2876 if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
2877 const GstMetaInfo *meta =
2878 gst_meta_register (gst_reference_timestamp_meta_api_get_type (),
2879 "GstReferenceTimestampMeta",
2880 sizeof (GstReferenceTimestampMeta),
2881 (GstMetaInitFunction) _gst_reference_timestamp_meta_init,
2882 (GstMetaFreeFunction) _gst_reference_timestamp_meta_free,
2883 _gst_reference_timestamp_meta_transform);
2884 g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) meta);
2891 * gst_buffer_add_custom_meta:
2892 * @buffer: (transfer none): a #GstBuffer
2893 * @name: the registered name of the desired custom meta
2895 * Creates and adds a #GstCustomMeta for the desired @name. @name must have
2896 * been successfully registered with gst_meta_register_custom().
2898 * Returns: (transfer none) (nullable): The #GstCustomMeta that was added to the buffer
2903 gst_buffer_add_custom_meta (GstBuffer * buffer, const gchar * name)
2905 GstCustomMeta *meta;
2906 const GstMetaInfo *info;
2908 g_return_val_if_fail (name != NULL, NULL);
2909 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
2911 info = gst_meta_get_info (name);
2913 if (info == NULL || !gst_meta_info_is_custom (info))
2916 meta = (GstCustomMeta *) gst_buffer_add_meta (buffer, info, NULL);
2922 * gst_buffer_get_custom_meta:
2923 * @buffer: a #GstBuffer
2924 * @name: the registered name of the custom meta to retrieve.
2926 * Finds the first #GstCustomMeta on @buffer for the desired @name.
2928 * Returns: (transfer none) (nullable): the #GstCustomMeta
2933 gst_buffer_get_custom_meta (GstBuffer * buffer, const gchar * name)
2935 const GstMetaInfo *info;
2937 g_return_val_if_fail (buffer != NULL, NULL);
2938 g_return_val_if_fail (name != NULL, NULL);
2940 info = gst_meta_get_info (name);
2945 if (!gst_meta_info_is_custom (info))
2948 return (GstCustomMeta *) gst_buffer_get_meta (buffer, info->api);
2952 * gst_buffer_ref: (skip)
2953 * @buf: a #GstBuffer.
2955 * Increases the refcount of the given buffer by one.
2957 * Note that the refcount affects the writability
2958 * of @buf and its metadata, see gst_buffer_is_writable().
2959 * It is important to note that keeping additional references to
2960 * GstBuffer instances can potentially increase the number
2961 * of `memcpy` operations in a pipeline.
2963 * Returns: (transfer full): @buf
2966 gst_buffer_ref (GstBuffer * buf)
2968 return (GstBuffer *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (buf));
2972 * gst_buffer_unref: (skip)
2973 * @buf: (transfer full): a #GstBuffer.
2975 * Decreases the refcount of the buffer. If the refcount reaches 0, the buffer
2976 * with the associated metadata and memory will be freed.
2979 gst_buffer_unref (GstBuffer * buf)
2981 gst_mini_object_unref (GST_MINI_OBJECT_CAST (buf));
2985 * gst_clear_buffer: (skip)
2986 * @buf_ptr: a pointer to a #GstBuffer reference
2988 * Clears a reference to a #GstBuffer.
2990 * @buf_ptr must not be %NULL.
2992 * If the reference is %NULL then this function does nothing. Otherwise, the
2993 * reference count of the buffer is decreased and the pointer is set to %NULL.
2998 gst_clear_buffer (GstBuffer ** buf_ptr)
3000 gst_clear_mini_object ((GstMiniObject **) buf_ptr);
3004 * gst_buffer_copy: (skip)
3005 * @buf: a #GstBuffer.
3007 * Creates a copy of the given buffer. This will only copy the buffer's
3008 * data to a newly allocated memory if needed (if the type of memory
3009 * requires it), otherwise the underlying data is just referenced.
3010 * Check gst_buffer_copy_deep() if you want to force the data
3011 * to be copied to newly allocated memory.
3013 * Returns: (transfer full) (nullable): a new copy of @buf if the copy succeeded, %NULL otherwise.
3016 gst_buffer_copy (const GstBuffer * buf)
3018 return GST_BUFFER (gst_mini_object_copy (GST_MINI_OBJECT_CONST_CAST (buf)));
3022 * gst_buffer_replace: (skip)
3023 * @obuf: (inout) (transfer full) (nullable): pointer to a pointer to
3024 * a #GstBuffer to be replaced.
3025 * @nbuf: (transfer none) (allow-none): pointer to a #GstBuffer that will
3026 * replace the buffer pointed to by @obuf.
3028 * Modifies a pointer to a #GstBuffer to point to a different #GstBuffer. The
3029 * modification is done atomically (so this is useful for ensuring thread safety
3030 * in some cases), and the reference counts are updated appropriately (the old
3031 * buffer is unreffed, the new is reffed).
3033 * Either @nbuf or the #GstBuffer pointed to by @obuf may be %NULL.
3035 * Returns: %TRUE when @obuf was different from @nbuf.
3038 gst_buffer_replace (GstBuffer ** obuf, GstBuffer * nbuf)
3040 return gst_mini_object_replace ((GstMiniObject **) obuf,
3041 (GstMiniObject *) nbuf);