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 meta with memory references %" GST_PTR_FORMAT, meta);
695 } else if (info->transform_func) {
696 GstMetaTransformCopy copy_data;
698 copy_data.region = region;
699 copy_data.offset = offset;
700 copy_data.size = size;
702 if (!info->transform_func (dest, meta, src,
703 _gst_meta_transform_copy, ©_data)) {
704 GST_CAT_ERROR (GST_CAT_BUFFER,
705 "failed to copy meta %p of API type %s", meta,
706 g_type_name (info->api));
716 gst_buffer_copy_with_flags (const GstBuffer * buffer, GstBufferCopyFlags flags)
720 g_return_val_if_fail (buffer != NULL, NULL);
722 /* create a fresh new buffer */
723 copy = gst_buffer_new ();
725 /* copy what the 'flags' want from our parent */
726 /* FIXME why we can't pass const to gst_buffer_copy_into() ? */
727 if (!gst_buffer_copy_into (copy, (GstBuffer *) buffer, flags, 0, -1))
728 gst_buffer_replace (©, NULL);
731 GST_BUFFER_FLAG_UNSET (copy, GST_BUFFER_FLAG_TAG_MEMORY);
737 _gst_buffer_copy (const GstBuffer * buffer)
739 return gst_buffer_copy_with_flags (buffer, GST_BUFFER_COPY_ALL);
743 * gst_buffer_copy_deep:
744 * @buf: a #GstBuffer.
746 * Creates a copy of the given buffer. This will make a newly allocated
747 * copy of the data the source buffer contains.
749 * Returns: (transfer full) (nullable): a new copy of @buf if the copy succeeded, %NULL otherwise.
754 gst_buffer_copy_deep (const GstBuffer * buffer)
756 return gst_buffer_copy_with_flags (buffer,
757 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP);
760 /* the default dispose function revives the buffer and returns it to the
761 * pool when there is a pool */
763 _gst_buffer_dispose (GstBuffer * buffer)
767 /* no pool, do free */
768 if ((pool = buffer->pool) == NULL)
771 /* keep the buffer alive */
772 gst_buffer_ref (buffer);
773 /* return the buffer to the pool */
774 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
775 gst_buffer_pool_release_buffer (pool, buffer);
781 _gst_buffer_free (GstBuffer * buffer)
783 GstMetaItem *walk, *next;
787 g_return_if_fail (buffer != NULL);
789 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
792 for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
793 GstMeta *meta = &walk->meta;
794 const GstMetaInfo *info = meta->info;
796 /* call free_func if any */
798 info->free_func (meta, buffer);
801 /* and free the slice */
802 g_slice_free1 (ITEM_SIZE (info), walk);
805 /* get the size, when unreffing the memory, we could also unref the buffer
807 msize = GST_BUFFER_SLICE_SIZE (buffer);
809 /* free our memory */
810 len = GST_BUFFER_MEM_LEN (buffer);
811 for (i = 0; i < len; i++) {
812 gst_memory_unlock (GST_BUFFER_MEM_PTR (buffer, i), GST_LOCK_FLAG_EXCLUSIVE);
813 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (GST_BUFFER_MEM_PTR
814 (buffer, i)), GST_MINI_OBJECT_CAST (buffer));
815 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
818 /* we set msize to 0 when the buffer is part of the memory block */
821 memset (buffer, 0xff, msize);
823 g_slice_free1 (msize, buffer);
825 gst_memory_unref (GST_BUFFER_BUFMEM (buffer));
830 gst_buffer_init (GstBufferImpl * buffer, gsize size)
832 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), 0, _gst_buffer_type,
833 (GstMiniObjectCopyFunction) _gst_buffer_copy,
834 (GstMiniObjectDisposeFunction) _gst_buffer_dispose,
835 (GstMiniObjectFreeFunction) _gst_buffer_free);
837 GST_BUFFER_SLICE_SIZE (buffer) = size;
839 GST_BUFFER (buffer)->pool = NULL;
840 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
841 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
842 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
843 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
844 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
846 GST_BUFFER_MEM_LEN (buffer) = 0;
847 GST_BUFFER_META (buffer) = NULL;
853 * Creates a newly allocated buffer without any data.
855 * Returns: (transfer full): the new #GstBuffer.
858 gst_buffer_new (void)
860 GstBufferImpl *newbuf;
862 newbuf = g_slice_new (GstBufferImpl);
863 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
865 gst_buffer_init (newbuf, sizeof (GstBufferImpl));
867 return GST_BUFFER_CAST (newbuf);
871 * gst_buffer_new_allocate:
872 * @allocator: (transfer none) (allow-none): the #GstAllocator to use, or %NULL to use the
874 * @size: the size in bytes of the new buffer's data.
875 * @params: (transfer none) (allow-none): optional parameters
877 * Tries to create a newly allocated buffer with data of the given size and
878 * extra parameters from @allocator. If the requested amount of memory can't be
879 * allocated, %NULL will be returned. The allocated buffer memory is not cleared.
881 * When @allocator is %NULL, the default memory allocator will be used.
883 * Note that when @size == 0, the buffer will not have memory associated with it.
885 * Returns: (transfer full) (nullable): a new #GstBuffer
888 gst_buffer_new_allocate (GstAllocator * allocator, gsize size,
889 GstAllocationParams * params)
900 mem = gst_allocator_alloc (allocator, size, params);
901 if (G_UNLIKELY (mem == NULL))
907 newbuf = gst_buffer_new ();
910 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
911 _memory_add (newbuf, -1, mem);
914 GST_CAT_LOG (GST_CAT_BUFFER,
915 "new buffer %p of size %" G_GSIZE_FORMAT " from allocator %p", newbuf,
920 asize = sizeof (GstBufferImpl) + size;
921 data = g_slice_alloc (asize);
922 if (G_UNLIKELY (data == NULL))
925 newbuf = GST_BUFFER_CAST (data);
927 gst_buffer_init ((GstBufferImpl *) data, asize);
929 mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL,
931 _memory_add (newbuf, -1, mem, TRUE);
936 /* allocate memory and buffer, it might be interesting to do this but there
937 * are many complications. We need to keep the memory mapped to access the
938 * buffer fields and the memory for the buffer might be just very slow. We
939 * also need to do some more magic to get the alignment right. */
940 asize = sizeof (GstBufferImpl) + size;
941 mem = gst_allocator_alloc (allocator, asize, align);
942 if (G_UNLIKELY (mem == NULL))
945 /* map the data part and init the buffer in it, set the buffer size to 0 so
946 * that a finalize won't free the buffer */
947 data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
948 gst_buffer_init ((GstBufferImpl *) data, 0);
949 gst_memory_unmap (mem);
951 /* strip off the buffer */
952 gst_memory_resize (mem, sizeof (GstBufferImpl), size);
954 newbuf = GST_BUFFER_CAST (data);
955 GST_BUFFER_BUFMEM (newbuf) = mem;
958 _memory_add (newbuf, -1, gst_memory_ref (mem), TRUE);
960 GST_BUFFER_FLAG_UNSET (newbuf, GST_BUFFER_FLAG_TAG_MEMORY);
967 GST_CAT_WARNING (GST_CAT_BUFFER,
968 "failed to allocate %" G_GSIZE_FORMAT " bytes", size);
974 * gst_buffer_new_wrapped_full:
975 * @flags: #GstMemoryFlags
976 * @data: (array length=size) (element-type guint8) (transfer none): data to wrap
977 * @maxsize: allocated size of @data
978 * @offset: offset in @data
979 * @size: size of valid data
980 * @user_data: (allow-none): user_data
981 * @notify: (allow-none) (scope async) (closure user_data): called with @user_data when the memory is freed
983 * Allocates a new buffer that wraps the given memory. @data must point to
984 * @maxsize of memory, the wrapped buffer will have the region from @offset and
987 * When the buffer is destroyed, @notify will be called with @user_data.
989 * The prefix/padding must be filled with 0 if @flags contains
990 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
992 * Returns: (transfer full): a new #GstBuffer
995 gst_buffer_new_wrapped_full (GstMemoryFlags flags, gpointer data,
996 gsize maxsize, gsize offset, gsize size, gpointer user_data,
997 GDestroyNotify notify)
1002 newbuf = gst_buffer_new ();
1004 gst_memory_new_wrapped (flags, data, maxsize, offset, size, user_data,
1006 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
1007 _memory_add (newbuf, -1, mem);
1008 GST_BUFFER_FLAG_UNSET (newbuf, GST_BUFFER_FLAG_TAG_MEMORY);
1014 * gst_buffer_new_wrapped:
1015 * @data: (array length=size) (element-type guint8) (transfer full): data to wrap
1016 * @size: allocated size of @data
1018 * Creates a new buffer that wraps the given @data. The memory will be freed
1019 * with g_free() and will be marked writable.
1021 * Returns: (transfer full): a new #GstBuffer
1024 gst_buffer_new_wrapped (gpointer data, gsize size)
1026 return gst_buffer_new_wrapped_full (0, data, size, 0, size, data, g_free);
1030 * gst_buffer_new_wrapped_bytes:
1031 * @bytes: (transfer none): a #GBytes to wrap
1033 * Creates a new #GstBuffer that wraps the given @bytes. The data inside
1034 * @bytes cannot be %NULL and the resulting buffer will be marked as read only.
1036 * Returns: (transfer full): a new #GstBuffer wrapping @bytes
1041 gst_buffer_new_wrapped_bytes (GBytes * bytes)
1046 g_return_val_if_fail (bytes != NULL, NULL);
1047 bytes_data = (guint8 *) g_bytes_get_data (bytes, &size);
1048 g_return_val_if_fail (bytes_data != NULL, NULL);
1050 return gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, bytes_data,
1051 size, 0, size, g_bytes_ref (bytes), (GDestroyNotify) g_bytes_unref);
1055 * gst_buffer_new_memdup:
1056 * @data: (array length=size) (element-type guint8) (transfer none): data to copy into new buffer
1057 * @size: size of @data in bytes
1059 * Creates a new buffer of size @size and fills it with a copy of @data.
1061 * Returns: (transfer full): a new #GstBuffer
1066 gst_buffer_new_memdup (gconstpointer data, gsize size)
1068 gpointer data2 = g_memdup2 (data, size);
1070 return gst_buffer_new_wrapped_full (0, data2, size, 0, size, data2, g_free);
1074 * gst_buffer_n_memory:
1075 * @buffer: a #GstBuffer.
1077 * Gets the amount of memory blocks that this buffer has. This amount is never
1078 * larger than what gst_buffer_get_max_memory() returns.
1080 * Returns: the number of memory blocks this buffer is made of.
1083 gst_buffer_n_memory (GstBuffer * buffer)
1085 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1087 return GST_BUFFER_MEM_LEN (buffer);
1091 * gst_buffer_prepend_memory:
1092 * @buffer: a #GstBuffer.
1093 * @mem: (transfer full): a #GstMemory.
1095 * Prepends the memory block @mem to @buffer. This function takes
1096 * ownership of @mem and thus doesn't increase its refcount.
1098 * This function is identical to gst_buffer_insert_memory() with an index of 0.
1099 * See gst_buffer_insert_memory() for more details.
1102 gst_buffer_prepend_memory (GstBuffer * buffer, GstMemory * mem)
1104 gst_buffer_insert_memory (buffer, 0, mem);
1108 * gst_buffer_append_memory:
1109 * @buffer: a #GstBuffer.
1110 * @mem: (transfer full): a #GstMemory.
1112 * Appends the memory block @mem to @buffer. This function takes
1113 * ownership of @mem and thus doesn't increase its refcount.
1115 * This function is identical to gst_buffer_insert_memory() with an index of -1.
1116 * See gst_buffer_insert_memory() for more details.
1119 gst_buffer_append_memory (GstBuffer * buffer, GstMemory * mem)
1121 gst_buffer_insert_memory (buffer, -1, mem);
1125 * gst_buffer_insert_memory:
1126 * @buffer: a #GstBuffer.
1127 * @idx: the index to add the memory at, or -1 to append it to the end
1128 * @mem: (transfer full): a #GstMemory.
1130 * Inserts the memory block @mem into @buffer at @idx. This function takes ownership
1131 * of @mem and thus doesn't increase its refcount.
1133 * Only gst_buffer_get_max_memory() can be added to a buffer. If more memory is
1134 * added, existing memory blocks will automatically be merged to make room for
1138 gst_buffer_insert_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
1142 g_return_if_fail (GST_IS_BUFFER (buffer));
1143 g_return_if_fail (gst_buffer_is_writable (buffer));
1144 g_return_if_fail (mem != NULL);
1145 g_return_if_fail (idx == -1 ||
1146 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)));
1148 tmp = _memory_get_exclusive_reference (mem);
1149 g_return_if_fail (tmp != NULL);
1150 gst_memory_unref (mem);
1151 _memory_add (buffer, idx, tmp);
1155 _get_mapped (GstBuffer * buffer, guint idx, GstMapInfo * info,
1158 GstMemory *mem, *mapped;
1160 mem = gst_memory_ref (GST_BUFFER_MEM_PTR (buffer, idx));
1162 mapped = gst_memory_make_mapped (mem, info, flags);
1164 if (mapped != mem) {
1165 /* memory changed, lock new memory */
1166 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (mapped),
1167 GST_MINI_OBJECT_CAST (buffer));
1168 gst_memory_lock (mapped, GST_LOCK_FLAG_EXCLUSIVE);
1169 GST_BUFFER_MEM_PTR (buffer, idx) = mapped;
1170 /* unlock old memory */
1171 gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
1172 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (mem),
1173 GST_MINI_OBJECT_CAST (buffer));
1174 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1176 gst_memory_unref (mem);
1182 * gst_buffer_peek_memory:
1183 * @buffer: a #GstBuffer.
1186 * Gets the memory block at @idx in @buffer. The memory block stays valid until
1187 * the memory block in @buffer is removed, replaced or merged, typically with
1188 * any call that modifies the memory in @buffer.
1190 * Returns: (transfer none) (nullable): the #GstMemory at @idx.
1193 gst_buffer_peek_memory (GstBuffer * buffer, guint idx)
1195 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1196 g_return_val_if_fail (idx < GST_BUFFER_MEM_LEN (buffer), NULL);
1198 return GST_BUFFER_MEM_PTR (buffer, idx);
1202 * gst_buffer_get_memory:
1203 * @buffer: a #GstBuffer.
1206 * Gets the memory block at index @idx in @buffer.
1208 * Returns: (transfer full) (nullable): a #GstMemory that contains the data of the
1209 * memory block at @idx.
1212 gst_buffer_get_memory (GstBuffer * buffer, guint idx)
1214 return gst_buffer_get_memory_range (buffer, idx, 1);
1218 * gst_buffer_get_all_memory:
1219 * @buffer: a #GstBuffer.
1221 * Gets all the memory blocks in @buffer. The memory blocks will be merged
1222 * into one large #GstMemory.
1224 * Returns: (transfer full) (nullable): a #GstMemory that contains the merged memory.
1227 gst_buffer_get_all_memory (GstBuffer * buffer)
1229 return gst_buffer_get_memory_range (buffer, 0, -1);
1233 * gst_buffer_get_memory_range:
1234 * @buffer: a #GstBuffer.
1238 * Gets @length memory blocks in @buffer starting at @idx. The memory blocks will
1239 * be merged into one large #GstMemory.
1241 * If @length is -1, all memory starting from @idx is merged.
1243 * Returns: (transfer full) (nullable): a #GstMemory that contains the merged data of @length
1244 * blocks starting at @idx.
1247 gst_buffer_get_memory_range (GstBuffer * buffer, guint idx, gint length)
1251 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
1253 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1254 len = GST_BUFFER_MEM_LEN (buffer);
1255 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1256 (length == -1 && idx < len) || (length > 0 && length + idx <= len), NULL);
1261 return _get_merged_memory (buffer, idx, length);
1265 * gst_buffer_replace_memory:
1266 * @buffer: a #GstBuffer.
1268 * @mem: (transfer full): a #GstMemory
1270 * Replaces the memory block at index @idx in @buffer with @mem.
1273 gst_buffer_replace_memory (GstBuffer * buffer, guint idx, GstMemory * mem)
1275 gst_buffer_replace_memory_range (buffer, idx, 1, mem);
1279 * gst_buffer_replace_all_memory:
1280 * @buffer: a #GstBuffer.
1281 * @mem: (transfer full): a #GstMemory
1283 * Replaces all memory in @buffer with @mem.
1286 gst_buffer_replace_all_memory (GstBuffer * buffer, GstMemory * mem)
1288 gst_buffer_replace_memory_range (buffer, 0, -1, mem);
1292 * gst_buffer_replace_memory_range:
1293 * @buffer: a #GstBuffer.
1295 * @length: a length, should not be 0
1296 * @mem: (transfer full): a #GstMemory
1298 * Replaces @length memory blocks in @buffer starting at @idx with @mem.
1300 * If @length is -1, all memory starting from @idx will be removed and
1301 * replaced with @mem.
1303 * @buffer should be writable.
1306 gst_buffer_replace_memory_range (GstBuffer * buffer, guint idx, gint length,
1311 g_return_if_fail (GST_IS_BUFFER (buffer));
1312 g_return_if_fail (gst_buffer_is_writable (buffer));
1314 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d, %p", idx, length, mem);
1316 len = GST_BUFFER_MEM_LEN (buffer);
1317 g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
1318 (length == -1 && idx < len) || (length > 0 && length + idx <= len));
1323 _replace_memory (buffer, len, idx, length, mem);
1327 * gst_buffer_remove_memory:
1328 * @buffer: a #GstBuffer.
1331 * Removes the memory block in @b at index @i.
1334 gst_buffer_remove_memory (GstBuffer * buffer, guint idx)
1336 gst_buffer_remove_memory_range (buffer, idx, 1);
1340 * gst_buffer_remove_all_memory:
1341 * @buffer: a #GstBuffer.
1343 * Removes all the memory blocks in @buffer.
1346 gst_buffer_remove_all_memory (GstBuffer * buffer)
1348 if (GST_BUFFER_MEM_LEN (buffer))
1349 gst_buffer_remove_memory_range (buffer, 0, -1);
1353 * gst_buffer_remove_memory_range:
1354 * @buffer: a #GstBuffer.
1358 * Removes @length memory blocks in @buffer starting from @idx.
1360 * @length can be -1, in which case all memory starting from @idx is removed.
1363 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, gint length)
1367 g_return_if_fail (GST_IS_BUFFER (buffer));
1368 g_return_if_fail (gst_buffer_is_writable (buffer));
1370 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
1372 len = GST_BUFFER_MEM_LEN (buffer);
1373 g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
1374 (length == -1 && idx < len) || length + idx <= len);
1379 _replace_memory (buffer, len, idx, length, NULL);
1383 * gst_buffer_find_memory:
1384 * @buffer: a #GstBuffer.
1385 * @offset: an offset
1387 * @idx: (out): pointer to index
1388 * @length: (out): pointer to length
1389 * @skip: (out): pointer to skip
1391 * Finds the memory blocks that span @size bytes starting from @offset
1394 * When this function returns %TRUE, @idx will contain the index of the first
1395 * memory block where the byte for @offset can be found and @length contains the
1396 * number of memory blocks containing the @size remaining bytes. @skip contains
1397 * the number of bytes to skip in the memory block at @idx to get to the byte
1400 * @size can be -1 to get all the memory blocks after @idx.
1402 * Returns: %TRUE when @size bytes starting from @offset could be found in
1403 * @buffer and @idx, @length and @skip will be filled.
1406 gst_buffer_find_memory (GstBuffer * buffer, gsize offset, gsize size,
1407 guint * idx, guint * length, gsize * skip)
1409 guint i, len, found;
1411 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1412 g_return_val_if_fail (idx != NULL, FALSE);
1413 g_return_val_if_fail (length != NULL, FALSE);
1414 g_return_val_if_fail (skip != NULL, FALSE);
1416 len = GST_BUFFER_MEM_LEN (buffer);
1419 for (i = 0; i < len; i++) {
1423 mem = GST_BUFFER_MEM_PTR (buffer, i);
1427 /* block before offset, or empty block, skip */
1430 /* block after offset */
1432 /* first block, remember index and offset */
1436 /* return remaining blocks */
1443 /* count the amount of found bytes */
1445 if (found >= size) {
1446 /* we have enough bytes */
1447 *length = i - *idx + 1;
1456 * gst_buffer_is_memory_range_writable:
1457 * @buffer: a #GstBuffer.
1459 * @length: a length, should not be 0
1461 * Checks if @length memory blocks in @buffer starting from @idx are writable.
1463 * @length can be -1 to check all the memory blocks after @idx.
1465 * Note that this function does not check if @buffer is writable, use
1466 * gst_buffer_is_writable() to check that if needed.
1468 * Returns: %TRUE if the memory range is writable
1473 gst_buffer_is_memory_range_writable (GstBuffer * buffer, guint idx, gint length)
1477 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1479 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
1481 len = GST_BUFFER_MEM_LEN (buffer);
1482 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1483 (length == -1 && idx < len) || (length > 0 && length + idx <= len),
1491 for (i = 0; i < len; i++) {
1492 if (!gst_memory_is_writable (GST_BUFFER_MEM_PTR (buffer, i + idx)))
1499 * gst_buffer_is_all_memory_writable:
1500 * @buffer: a #GstBuffer.
1502 * Checks if all memory blocks in @buffer are writable.
1504 * Note that this function does not check if @buffer is writable, use
1505 * gst_buffer_is_writable() to check that if needed.
1507 * Returns: %TRUE if all memory blocks in @buffer are writable
1512 gst_buffer_is_all_memory_writable (GstBuffer * buffer)
1514 return gst_buffer_is_memory_range_writable (buffer, 0, -1);
1518 * gst_buffer_get_sizes:
1519 * @buffer: a #GstBuffer.
1520 * @offset: (out) (allow-none): a pointer to the offset
1521 * @maxsize: (out) (allow-none): a pointer to the maxsize
1523 * Gets the total size of the memory blocks in @buffer.
1525 * When not %NULL, @offset will contain the offset of the data in the
1526 * first memory block in @buffer and @maxsize will contain the sum of
1527 * the size and @offset and the amount of extra padding on the last
1528 * memory block. @offset and @maxsize can be used to resize the
1529 * buffer memory blocks with gst_buffer_resize().
1531 * Returns: total size of the memory blocks in @buffer.
1534 gst_buffer_get_sizes (GstBuffer * buffer, gsize * offset, gsize * maxsize)
1536 return gst_buffer_get_sizes_range (buffer, 0, -1, offset, maxsize);
1540 * gst_buffer_get_size:
1541 * @buffer: a #GstBuffer.
1543 * Gets the total size of the memory blocks in @buffer.
1545 * Returns: total size of the memory blocks in @buffer.
1548 gst_buffer_get_size (GstBuffer * buffer)
1553 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1556 len = GST_BUFFER_MEM_LEN (buffer);
1557 for (i = 0, size = 0; i < len; i++)
1558 size += GST_BUFFER_MEM_PTR (buffer, i)->size;
1563 * gst_buffer_get_sizes_range:
1564 * @buffer: a #GstBuffer.
1567 * @offset: (out) (allow-none): a pointer to the offset
1568 * @maxsize: (out) (allow-none): a pointer to the maxsize
1570 * Gets the total size of @length memory blocks stating from @idx in @buffer.
1572 * When not %NULL, @offset will contain the offset of the data in the
1573 * memory block in @buffer at @idx and @maxsize will contain the sum of the size
1574 * and @offset and the amount of extra padding on the memory block at @idx +
1576 * @offset and @maxsize can be used to resize the buffer memory blocks with
1577 * gst_buffer_resize_range().
1579 * Returns: total size of @length memory blocks starting at @idx in @buffer.
1582 gst_buffer_get_sizes_range (GstBuffer * buffer, guint idx, gint length,
1583 gsize * offset, gsize * maxsize)
1589 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1590 len = GST_BUFFER_MEM_LEN (buffer);
1591 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1592 (length == -1 && idx < len) || (length + idx <= len), 0);
1597 if (G_LIKELY (length == 1)) {
1599 mem = GST_BUFFER_MEM_PTR (buffer, idx);
1600 size = gst_memory_get_sizes (mem, offset, maxsize);
1601 } else if (offset == NULL && maxsize == NULL) {
1607 for (i = idx; i < end; i++) {
1608 mem = GST_BUFFER_MEM_PTR (buffer, i);
1616 size = offs = extra = 0;
1617 for (i = idx; i < end; i++) {
1620 mem = GST_BUFFER_MEM_PTR (buffer, i);
1621 s = gst_memory_get_sizes (mem, &o, &ms);
1625 /* first size, take accumulated data before as the offset */
1629 /* save the amount of data after this block */
1630 extra = ms - (o + s);
1632 /* empty block, add as extra */
1639 *maxsize = offs + size + extra;
1645 * gst_buffer_resize:
1646 * @buffer: a #GstBuffer.
1647 * @offset: the offset adjustment
1648 * @size: the new size or -1 to just adjust the offset
1650 * Sets the offset and total size of the memory blocks in @buffer.
1653 gst_buffer_resize (GstBuffer * buffer, gssize offset, gssize size)
1655 gst_buffer_resize_range (buffer, 0, -1, offset, size);
1659 * gst_buffer_set_size:
1660 * @buffer: a #GstBuffer.
1661 * @size: the new size
1663 * Sets the total size of the memory blocks in @buffer.
1666 gst_buffer_set_size (GstBuffer * buffer, gssize size)
1668 gst_buffer_resize_range (buffer, 0, -1, 0, size);
1672 * gst_buffer_resize_range:
1673 * @buffer: a #GstBuffer.
1676 * @offset: the offset adjustment
1677 * @size: the new size or -1 to just adjust the offset
1679 * Sets the total size of the @length memory blocks starting at @idx in
1682 * Returns: %TRUE if resizing succeeded, %FALSE otherwise.
1685 gst_buffer_resize_range (GstBuffer * buffer, guint idx, gint length,
1686 gssize offset, gssize size)
1689 gsize bsize, bufsize, bufoffs, bufmax;
1691 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
1692 g_return_val_if_fail (size >= -1, FALSE);
1694 len = GST_BUFFER_MEM_LEN (buffer);
1695 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1696 (length == -1 && idx < len) || (length + idx <= len), FALSE);
1701 bufsize = gst_buffer_get_sizes_range (buffer, idx, length, &bufoffs, &bufmax);
1703 GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSSIZE_FORMAT
1704 " size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%"
1705 G_GSIZE_FORMAT, buffer, offset, size, bufsize, bufoffs, bufmax);
1707 /* we can't go back further than the current offset or past the end of the
1709 g_return_val_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
1710 && bufoffs + offset <= bufmax), FALSE);
1712 g_return_val_if_fail (bufsize >= offset, FALSE);
1713 size = bufsize - offset;
1715 g_return_val_if_fail (bufmax >= bufoffs + offset + size, FALSE);
1718 if (offset == 0 && size == bufsize)
1723 for (i = idx; i < end; i++) {
1727 mem = GST_BUFFER_MEM_PTR (buffer, i);
1731 /* last buffer always gets resized to the remaining size */
1734 /* shrink buffers before the offset */
1735 else if ((gssize) bsize <= offset) {
1737 noffs = offset - bsize;
1740 /* clip other buffers */
1742 left = MIN (bsize - offset, size);
1744 if (offset != 0 || left != bsize) {
1745 if (gst_memory_is_writable (mem)) {
1746 gst_memory_resize (mem, offset, left);
1748 GstMemory *newmem = NULL;
1750 if (!GST_MEMORY_IS_NO_SHARE (mem))
1751 newmem = gst_memory_share (mem, offset, left);
1754 newmem = gst_memory_copy (mem, offset, left);
1759 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (newmem),
1760 GST_MINI_OBJECT_CAST (buffer));
1761 gst_memory_lock (newmem, GST_LOCK_FLAG_EXCLUSIVE);
1762 GST_BUFFER_MEM_PTR (buffer, i) = newmem;
1763 gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
1764 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (mem),
1765 GST_MINI_OBJECT_CAST (buffer));
1766 gst_memory_unref (mem);
1768 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1781 * @buffer: a #GstBuffer.
1782 * @info: (out caller-allocates): info about the mapping
1783 * @flags: flags for the mapping
1785 * Fills @info with the #GstMapInfo of all merged memory blocks in @buffer.
1787 * @flags describe the desired access of the memory. When @flags is
1788 * #GST_MAP_WRITE, @buffer should be writable (as returned from
1789 * gst_buffer_is_writable()).
1791 * When @buffer is writable but the memory isn't, a writable copy will
1792 * automatically be created and returned. The readonly copy of the
1793 * buffer memory will then also be replaced with this writable copy.
1795 * The memory in @info should be unmapped with gst_buffer_unmap() after
1798 * Returns: %TRUE if the map succeeded and @info contains valid data.
1801 gst_buffer_map (GstBuffer * buffer, GstMapInfo * info, GstMapFlags flags)
1803 return gst_buffer_map_range (buffer, 0, -1, info, flags);
1807 * gst_buffer_map_range:
1808 * @buffer: a #GstBuffer.
1811 * @info: (out caller-allocates): info about the mapping
1812 * @flags: flags for the mapping
1814 * Fills @info with the #GstMapInfo of @length merged memory blocks
1815 * starting at @idx in @buffer. When @length is -1, all memory blocks starting
1816 * from @idx are merged and mapped.
1818 * @flags describe the desired access of the memory. When @flags is
1819 * #GST_MAP_WRITE, @buffer should be writable (as returned from
1820 * gst_buffer_is_writable()).
1822 * When @buffer is writable but the memory isn't, a writable copy will
1823 * automatically be created and returned. The readonly copy of the buffer memory
1824 * will then also be replaced with this writable copy.
1826 * The memory in @info should be unmapped with gst_buffer_unmap() after usage.
1828 * Returns: %TRUE if the map succeeded and @info contains valid
1832 gst_buffer_map_range (GstBuffer * buffer, guint idx, gint length,
1833 GstMapInfo * info, GstMapFlags flags)
1835 GstMemory *mem, *nmem;
1836 gboolean write, writable;
1839 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1840 g_return_val_if_fail (info != NULL, FALSE);
1841 len = GST_BUFFER_MEM_LEN (buffer);
1842 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1843 (length == -1 && idx < len) || (length > 0
1844 && length + idx <= len), FALSE);
1846 GST_CAT_LOG (GST_CAT_BUFFER, "buffer %p, idx %u, length %d, flags %04x",
1847 buffer, idx, length, flags);
1849 write = (flags & GST_MAP_WRITE) != 0;
1850 writable = gst_buffer_is_writable (buffer);
1852 /* check if we can write when asked for write access */
1853 if (G_UNLIKELY (write && !writable))
1859 mem = _get_merged_memory (buffer, idx, length);
1860 if (G_UNLIKELY (mem == NULL))
1863 /* now try to map */
1864 nmem = gst_memory_make_mapped (mem, info, flags);
1865 if (G_UNLIKELY (nmem == NULL))
1868 /* if we merged or when the map returned a different memory, we try to replace
1869 * the memory in the buffer */
1870 if (G_UNLIKELY (length > 1 || nmem != mem)) {
1871 /* if the buffer is writable, replace the memory */
1873 _replace_memory (buffer, len, idx, length, gst_memory_ref (nmem));
1876 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
1877 "temporary mapping for memory %p in buffer %p", nmem, buffer);
1886 GST_WARNING ("write map requested on non-writable buffer");
1887 g_critical ("write map requested on non-writable buffer");
1888 memset (info, 0, sizeof (GstMapInfo));
1893 /* empty buffer, we need to return NULL */
1894 GST_DEBUG ("can't get buffer memory");
1895 memset (info, 0, sizeof (GstMapInfo));
1900 GST_DEBUG ("cannot map memory");
1901 memset (info, 0, sizeof (GstMapInfo));
1908 * @buffer: a #GstBuffer.
1909 * @info: a #GstMapInfo
1911 * Releases the memory previously mapped with gst_buffer_map().
1914 gst_buffer_unmap (GstBuffer * buffer, GstMapInfo * info)
1916 g_return_if_fail (GST_IS_BUFFER (buffer));
1917 g_return_if_fail (info != NULL);
1919 _gst_buffer_map_info_clear ((GstBufferMapInfo *) info);
1924 * @buffer: a #GstBuffer.
1925 * @offset: the offset to fill
1926 * @src: (array length=size) (element-type guint8): the source address
1927 * @size: the size to fill
1929 * Copies @size bytes from @src to @buffer at @offset.
1931 * Returns: The amount of bytes copied. This value can be lower than @size
1932 * when @buffer did not contain enough data.
1935 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
1939 const guint8 *ptr = src;
1941 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1942 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1943 g_return_val_if_fail (src != NULL || size == 0, 0);
1945 GST_CAT_LOG (GST_CAT_BUFFER,
1946 "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
1949 len = GST_BUFFER_MEM_LEN (buffer);
1952 for (i = 0; i < len && left > 0; i++) {
1957 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1958 if (info.size > offset) {
1959 /* we have enough */
1960 tocopy = MIN (info.size - offset, left);
1961 memcpy ((guint8 *) info.data + offset, ptr, tocopy);
1966 /* offset past buffer, skip */
1967 offset -= info.size;
1969 gst_memory_unmap (mem, &info);
1975 * gst_buffer_extract:
1976 * @buffer: a #GstBuffer.
1977 * @offset: the offset to extract
1978 * @dest: (out caller-allocates) (array length=size) (element-type guint8):
1979 * the destination address
1980 * @size: the size to extract
1982 * Copies @size bytes starting from @offset in @buffer to @dest.
1984 * Returns: The amount of bytes extracted. This value can be lower than @size
1985 * when @buffer did not contain enough data.
1988 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
1993 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1994 g_return_val_if_fail (dest != NULL, 0);
1996 GST_CAT_LOG (GST_CAT_BUFFER,
1997 "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
2000 len = GST_BUFFER_MEM_LEN (buffer);
2003 for (i = 0; i < len && left > 0; i++) {
2008 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
2009 if (info.size > offset) {
2010 /* we have enough */
2011 tocopy = MIN (info.size - offset, left);
2012 memcpy (ptr, (guint8 *) info.data + offset, tocopy);
2017 /* offset past buffer, skip */
2018 offset -= info.size;
2020 gst_memory_unmap (mem, &info);
2026 * gst_buffer_memcmp:
2027 * @buffer: a #GstBuffer.
2028 * @offset: the offset in @buffer
2029 * @mem: (array length=size) (element-type guint8): the memory to compare
2030 * @size: the size to compare
2032 * Compares @size bytes starting from @offset in @buffer with the memory in @mem.
2034 * Returns: 0 if the memory is equal.
2037 gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem,
2041 const guint8 *ptr = mem;
2044 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
2045 g_return_val_if_fail (mem != NULL, 0);
2047 GST_CAT_LOG (GST_CAT_BUFFER,
2048 "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
2051 if (G_UNLIKELY (gst_buffer_get_size (buffer) < offset + size))
2054 len = GST_BUFFER_MEM_LEN (buffer);
2056 for (i = 0; i < len && size > 0 && res == 0; i++) {
2061 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
2062 if (info.size > offset) {
2063 /* we have enough */
2064 tocmp = MIN (info.size - offset, size);
2065 res = memcmp (ptr, (guint8 *) info.data + offset, tocmp);
2070 /* offset past buffer, skip */
2071 offset -= info.size;
2073 gst_memory_unmap (mem, &info);
2079 * gst_buffer_memset:
2080 * @buffer: a #GstBuffer.
2081 * @offset: the offset in @buffer
2082 * @val: the value to set
2083 * @size: the size to set
2085 * Fills @buf with @size bytes with @val starting from @offset.
2087 * Returns: The amount of bytes filled. This value can be lower than @size
2088 * when @buffer did not contain enough data.
2091 gst_buffer_memset (GstBuffer * buffer, gsize offset, guint8 val, gsize size)
2095 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
2096 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
2098 GST_CAT_LOG (GST_CAT_BUFFER,
2099 "buffer %p, offset %" G_GSIZE_FORMAT ", val %02x, size %" G_GSIZE_FORMAT,
2100 buffer, offset, val, size);
2102 len = GST_BUFFER_MEM_LEN (buffer);
2105 for (i = 0; i < len && left > 0; i++) {
2110 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
2111 if (info.size > offset) {
2112 /* we have enough */
2113 toset = MIN (info.size - offset, left);
2114 memset ((guint8 *) info.data + offset, val, toset);
2118 /* offset past buffer, skip */
2119 offset -= info.size;
2121 gst_memory_unmap (mem, &info);
2127 * gst_buffer_copy_region:
2128 * @parent: a #GstBuffer.
2129 * @flags: the #GstBufferCopyFlags
2130 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
2132 * @size: the size of the new #GstBuffer sub-buffer, in bytes. If -1, all
2135 * Creates a sub-buffer from @parent at @offset and @size.
2136 * This sub-buffer uses the actual memory space of the parent buffer.
2137 * This function will copy the offset and timestamp fields when the
2138 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
2139 * #GST_BUFFER_OFFSET_NONE.
2140 * If @offset equals 0 and @size equals the total size of @buffer, the
2141 * duration and offset end fields are also copied. If not they will be set
2142 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
2144 * Returns: (transfer full) (nullable): the new #GstBuffer or %NULL if copying
2148 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
2149 gsize offset, gsize size)
2153 g_return_val_if_fail (buffer != NULL, NULL);
2155 /* create the new buffer */
2156 copy = gst_buffer_new ();
2158 GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
2159 "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
2161 if (!gst_buffer_copy_into (copy, buffer, flags, offset, size))
2162 gst_buffer_replace (©, NULL);
2168 * gst_buffer_append:
2169 * @buf1: (transfer full): the first source #GstBuffer to append.
2170 * @buf2: (transfer full): the second source #GstBuffer to append.
2172 * Appends all the memory from @buf2 to @buf1. The result buffer will contain a
2173 * concatenation of the memory of @buf1 and @buf2.
2175 * Returns: (transfer full): the new #GstBuffer that contains the memory
2176 * of the two source buffers.
2179 gst_buffer_append (GstBuffer * buf1, GstBuffer * buf2)
2181 return gst_buffer_append_region (buf1, buf2, 0, -1);
2185 * gst_buffer_append_region:
2186 * @buf1: (transfer full): the first source #GstBuffer to append.
2187 * @buf2: (transfer full): the second source #GstBuffer to append.
2188 * @offset: the offset in @buf2
2189 * @size: the size or -1 of @buf2
2191 * Appends @size bytes at @offset from @buf2 to @buf1. The result buffer will
2192 * contain a concatenation of the memory of @buf1 and the requested region of
2195 * Returns: (transfer full): the new #GstBuffer that contains the memory
2196 * of the two source buffers.
2199 gst_buffer_append_region (GstBuffer * buf1, GstBuffer * buf2, gssize offset,
2204 g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
2205 g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
2207 buf1 = gst_buffer_make_writable (buf1);
2208 buf2 = gst_buffer_make_writable (buf2);
2210 gst_buffer_resize (buf2, offset, size);
2212 len = GST_BUFFER_MEM_LEN (buf2);
2213 for (i = 0; i < len; i++) {
2216 mem = GST_BUFFER_MEM_PTR (buf2, i);
2217 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (mem),
2218 GST_MINI_OBJECT_CAST (buf2));
2219 GST_BUFFER_MEM_PTR (buf2, i) = NULL;
2220 _memory_add (buf1, -1, mem);
2223 GST_BUFFER_MEM_LEN (buf2) = 0;
2224 GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_TAG_MEMORY);
2225 gst_buffer_unref (buf2);
2231 * gst_buffer_get_meta:
2232 * @buffer: a #GstBuffer
2233 * @api: the #GType of an API
2235 * Gets the metadata for @api on buffer. When there is no such metadata, %NULL is
2236 * returned. If multiple metadata with the given @api are attached to this
2237 * buffer only the first one is returned. To handle multiple metadata with a
2238 * given API use gst_buffer_iterate_meta() or gst_buffer_foreach_meta() instead
2239 * and check the `meta->info.api` member for the API type.
2241 * Returns: (transfer none) (nullable): the metadata for @api on @buffer.
2244 gst_buffer_get_meta (GstBuffer * buffer, GType api)
2247 GstMeta *result = NULL;
2249 g_return_val_if_fail (buffer != NULL, NULL);
2250 g_return_val_if_fail (api != 0, NULL);
2252 /* find GstMeta of the requested API */
2253 for (item = GST_BUFFER_META (buffer); item; item = item->next) {
2254 GstMeta *meta = &item->meta;
2255 if (meta->info->api == api) {
2264 * gst_buffer_get_n_meta:
2265 * @buffer: a #GstBuffer
2266 * @api_type: the #GType of an API
2268 * Returns: number of metas of type @api_type on @buffer.
2273 gst_buffer_get_n_meta (GstBuffer * buffer, GType api_type)
2275 gpointer state = NULL;
2279 while ((meta = gst_buffer_iterate_meta_filtered (buffer, &state, api_type)))
2286 * gst_buffer_add_meta:
2287 * @buffer: a #GstBuffer
2288 * @info: a #GstMetaInfo
2289 * @params: params for @info
2291 * Adds metadata for @info to @buffer using the parameters in @params.
2293 * Returns: (transfer none) (nullable): the metadata for the api in @info on @buffer.
2296 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
2300 GstMeta *result = NULL;
2303 g_return_val_if_fail (buffer != NULL, NULL);
2304 g_return_val_if_fail (info != NULL, NULL);
2305 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
2307 /* create a new slice */
2308 size = ITEM_SIZE (info);
2309 /* We warn in gst_meta_register() about metas without
2310 * init function but let's play safe here and prevent
2311 * uninitialized memory
2313 if (!info->init_func)
2314 item = g_slice_alloc0 (size);
2316 item = g_slice_alloc (size);
2317 result = &item->meta;
2318 result->info = info;
2319 result->flags = GST_META_FLAG_NONE;
2320 GST_CAT_DEBUG (GST_CAT_BUFFER,
2321 "alloc metadata %p (%s) of size %" G_GSIZE_FORMAT, result,
2322 g_type_name (info->type), info->size);
2324 /* call the init_func when needed */
2325 if (info->init_func)
2326 if (!info->init_func (result, params, buffer))
2329 item->seq_num = gst_atomic_int64_inc (&meta_seq);
2332 if (!GST_BUFFER_META (buffer)) {
2333 GST_BUFFER_META (buffer) = item;
2334 GST_BUFFER_TAIL_META (buffer) = item;
2336 GST_BUFFER_TAIL_META (buffer)->next = item;
2337 GST_BUFFER_TAIL_META (buffer) = item;
2344 g_slice_free1 (size, item);
2350 * gst_buffer_remove_meta:
2351 * @buffer: a #GstBuffer
2354 * Removes the metadata for @meta on @buffer.
2356 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
2357 * metadata was on @buffer.
2360 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
2362 GstMetaItem *walk, *prev;
2364 g_return_val_if_fail (buffer != NULL, FALSE);
2365 g_return_val_if_fail (meta != NULL, FALSE);
2366 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
2367 g_return_val_if_fail (!GST_META_FLAG_IS_SET (meta, GST_META_FLAG_LOCKED),
2370 /* find the metadata and delete */
2371 prev = GST_BUFFER_META (buffer);
2372 for (walk = prev; walk; walk = walk->next) {
2373 GstMeta *m = &walk->meta;
2375 const GstMetaInfo *info = meta->info;
2377 /* remove from list */
2378 if (GST_BUFFER_TAIL_META (buffer) == walk) {
2380 GST_BUFFER_TAIL_META (buffer) = prev;
2382 GST_BUFFER_TAIL_META (buffer) = NULL;
2385 if (GST_BUFFER_META (buffer) == walk)
2386 GST_BUFFER_META (buffer) = walk->next;
2388 prev->next = walk->next;
2390 /* call free_func if any */
2391 if (info->free_func)
2392 info->free_func (m, buffer);
2394 /* and free the slice */
2395 g_slice_free1 (ITEM_SIZE (info), walk);
2400 return walk != NULL;
2404 * gst_buffer_iterate_meta: (skip)
2405 * @buffer: a #GstBuffer
2406 * @state: (out caller-allocates): an opaque state pointer
2408 * Retrieves the next #GstMeta after @current. If @state points
2409 * to %NULL, the first metadata is returned.
2411 * @state will be updated with an opaque state pointer
2413 * Returns: (transfer none) (nullable): The next #GstMeta or %NULL
2414 * when there are no more items.
2417 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
2421 g_return_val_if_fail (buffer != NULL, NULL);
2422 g_return_val_if_fail (state != NULL, NULL);
2424 meta = (GstMetaItem **) state;
2426 /* state NULL, move to first item */
2427 *meta = GST_BUFFER_META (buffer);
2429 /* state !NULL, move to next item in list */
2430 *meta = (*meta)->next;
2433 return &(*meta)->meta;
2439 * gst_buffer_iterate_meta_filtered: (skip)
2440 * @buffer: a #GstBuffer
2441 * @state: (out caller-allocates): an opaque state pointer
2442 * @meta_api_type: only return #GstMeta of this type
2444 * Retrieves the next #GstMeta of type @meta_api_type after the current one
2445 * according to @state. If @state points to %NULL, the first metadata of
2446 * type @meta_api_type is returned.
2448 * @state will be updated with an opaque state pointer
2450 * Returns: (transfer none) (nullable): The next #GstMeta of type
2451 * @meta_api_type or %NULL when there are no more items.
2456 gst_buffer_iterate_meta_filtered (GstBuffer * buffer, gpointer * state,
2457 GType meta_api_type)
2461 g_return_val_if_fail (buffer != NULL, NULL);
2462 g_return_val_if_fail (state != NULL, NULL);
2464 meta = (GstMetaItem **) state;
2466 /* state NULL, move to first item */
2467 *meta = GST_BUFFER_META (buffer);
2469 /* state !NULL, move to next item in list */
2470 *meta = (*meta)->next;
2472 while (*meta != NULL && (*meta)->meta.info->api != meta_api_type)
2473 *meta = (*meta)->next;
2476 return &(*meta)->meta;
2482 * gst_buffer_foreach_meta:
2483 * @buffer: a #GstBuffer
2484 * @func: (scope call): a #GstBufferForeachMetaFunc to call
2485 * @user_data: (closure): user data passed to @func
2487 * Calls @func with @user_data for each meta in @buffer.
2489 * @func can modify the passed meta pointer or its contents. The return value
2490 * of @func defines if this function returns or if the remaining metadata items
2491 * in the buffer should be skipped.
2493 * Returns: %FALSE when @func returned %FALSE for one of the metadata.
2496 gst_buffer_foreach_meta (GstBuffer * buffer, GstBufferForeachMetaFunc func,
2499 GstMetaItem *walk, *prev, *next;
2500 gboolean res = TRUE;
2502 g_return_val_if_fail (buffer != NULL, FALSE);
2503 g_return_val_if_fail (func != NULL, FALSE);
2505 /* find the metadata and delete */
2506 prev = GST_BUFFER_META (buffer);
2507 for (walk = prev; walk; walk = next) {
2510 m = new = &walk->meta;
2513 res = func (buffer, &new, user_data);
2516 const GstMetaInfo *info = m->info;
2518 GST_CAT_DEBUG (GST_CAT_BUFFER, "remove metadata %p (%s)", m,
2519 g_type_name (info->type));
2521 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
2522 g_return_val_if_fail (!GST_META_FLAG_IS_SET (m, GST_META_FLAG_LOCKED),
2525 if (GST_BUFFER_TAIL_META (buffer) == walk) {
2527 GST_BUFFER_TAIL_META (buffer) = prev;
2529 GST_BUFFER_TAIL_META (buffer) = NULL;
2532 /* remove from list */
2533 if (GST_BUFFER_META (buffer) == walk)
2534 prev = GST_BUFFER_META (buffer) = next;
2538 /* call free_func if any */
2539 if (info->free_func)
2540 info->free_func (m, buffer);
2542 /* and free the slice */
2543 g_slice_free1 (ITEM_SIZE (info), walk);
2554 * gst_buffer_extract_dup:
2555 * @buffer: a #GstBuffer
2556 * @offset: the offset to extract
2557 * @size: the size to extract
2558 * @dest: (array length=dest_size) (element-type guint8) (out): A pointer where
2559 * the destination array will be written. Might be %NULL if the size is 0.
2560 * @dest_size: (out): A location where the size of @dest can be written
2562 * Extracts a copy of at most @size bytes the data at @offset into
2563 * newly-allocated memory. @dest must be freed using g_free() when done.
2569 gst_buffer_extract_dup (GstBuffer * buffer, gsize offset, gsize size,
2570 gpointer * dest, gsize * dest_size)
2572 gsize real_size, alloc_size;
2574 real_size = gst_buffer_get_size (buffer);
2576 alloc_size = MIN (real_size - offset, size);
2577 if (alloc_size == 0) {
2581 *dest = g_malloc (alloc_size);
2582 *dest_size = gst_buffer_extract (buffer, offset, *dest, size);
2586 GST_DEBUG_CATEGORY_STATIC (gst_parent_buffer_meta_debug);
2589 * gst_buffer_add_parent_buffer_meta:
2590 * @buffer: (transfer none): a #GstBuffer
2591 * @ref: (transfer none): a #GstBuffer to ref
2593 * Adds a #GstParentBufferMeta to @buffer that holds a reference on
2594 * @ref until the buffer is freed.
2596 * Returns: (transfer none) (nullable): The #GstParentBufferMeta that was added to the buffer
2600 GstParentBufferMeta *
2601 gst_buffer_add_parent_buffer_meta (GstBuffer * buffer, GstBuffer * ref)
2603 GstParentBufferMeta *meta;
2605 g_return_val_if_fail (GST_IS_BUFFER (ref), NULL);
2608 (GstParentBufferMeta *) gst_buffer_add_meta (buffer,
2609 GST_PARENT_BUFFER_META_INFO, NULL);
2614 meta->buffer = gst_buffer_ref (ref);
2620 _gst_parent_buffer_meta_transform (GstBuffer * dest, GstMeta * meta,
2621 GstBuffer * buffer, GQuark type, gpointer data)
2623 GstParentBufferMeta *dmeta, *smeta;
2625 smeta = (GstParentBufferMeta *) meta;
2627 if (GST_META_TRANSFORM_IS_COPY (type)) {
2628 /* copy over the reference to the parent buffer.
2629 * Usually, this meta means we need to keep the parent buffer
2630 * alive because one of the child memories is in use, which
2631 * might not be the case if memory is deep copied or sub-regioned,
2632 * but we can't tell, so keep the meta */
2633 dmeta = gst_buffer_add_parent_buffer_meta (dest, smeta->buffer);
2637 GST_CAT_DEBUG (gst_parent_buffer_meta_debug,
2638 "copy buffer reference metadata");
2640 /* return FALSE, if transform type is not supported */
2647 _gst_parent_buffer_meta_free (GstParentBufferMeta * parent_meta,
2650 GST_CAT_DEBUG (gst_parent_buffer_meta_debug,
2651 "Dropping reference on buffer %p", parent_meta->buffer);
2652 gst_buffer_unref (parent_meta->buffer);
2656 _gst_parent_buffer_meta_init (GstParentBufferMeta * parent_meta,
2657 gpointer params, GstBuffer * buffer)
2661 if (g_once_init_enter (&_init)) {
2662 GST_DEBUG_CATEGORY_INIT (gst_parent_buffer_meta_debug, "parentbuffermeta",
2663 0, "parentbuffermeta");
2664 g_once_init_leave (&_init, 1);
2667 parent_meta->buffer = NULL;
2673 * gst_parent_buffer_meta_api_get_type: (attributes doc.skip=true)
2676 gst_parent_buffer_meta_api_get_type (void)
2678 static GType type = 0;
2679 static const gchar *tags[] = { GST_META_TAG_MEMORY_REFERENCE_STR, NULL };
2681 if (g_once_init_enter (&type)) {
2682 GType _type = gst_meta_api_type_register ("GstParentBufferMetaAPI", tags);
2683 g_once_init_leave (&type, _type);
2690 * gst_parent_buffer_meta_get_info:
2692 * Gets the global #GstMetaInfo describing the #GstParentBufferMeta meta.
2694 * Returns: (transfer none): The #GstMetaInfo
2699 gst_parent_buffer_meta_get_info (void)
2701 static const GstMetaInfo *meta_info = NULL;
2703 if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
2704 const GstMetaInfo *meta =
2705 gst_meta_register (gst_parent_buffer_meta_api_get_type (),
2706 "GstParentBufferMeta",
2707 sizeof (GstParentBufferMeta),
2708 (GstMetaInitFunction) _gst_parent_buffer_meta_init,
2709 (GstMetaFreeFunction) _gst_parent_buffer_meta_free,
2710 _gst_parent_buffer_meta_transform);
2711 g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) meta);
2717 GST_DEBUG_CATEGORY_STATIC (gst_reference_timestamp_meta_debug);
2720 * gst_buffer_add_reference_timestamp_meta:
2721 * @buffer: (transfer none): a #GstBuffer
2722 * @reference: (transfer none): identifier for the timestamp reference.
2723 * @timestamp: timestamp
2724 * @duration: duration, or %GST_CLOCK_TIME_NONE
2726 * Adds a #GstReferenceTimestampMeta to @buffer that holds a @timestamp and
2727 * optionally @duration based on a specific timestamp @reference. See the
2728 * documentation of #GstReferenceTimestampMeta for details.
2730 * Returns: (transfer none) (nullable): The #GstReferenceTimestampMeta that was added to the buffer
2734 GstReferenceTimestampMeta *
2735 gst_buffer_add_reference_timestamp_meta (GstBuffer * buffer,
2736 GstCaps * reference, GstClockTime timestamp, GstClockTime duration)
2738 GstReferenceTimestampMeta *meta;
2740 g_return_val_if_fail (GST_IS_CAPS (reference), NULL);
2741 g_return_val_if_fail (timestamp != GST_CLOCK_TIME_NONE, NULL);
2744 (GstReferenceTimestampMeta *) gst_buffer_add_meta (buffer,
2745 GST_REFERENCE_TIMESTAMP_META_INFO, NULL);
2750 meta->reference = gst_caps_ref (reference);
2751 meta->timestamp = timestamp;
2752 meta->duration = duration;
2758 * gst_buffer_get_reference_timestamp_meta:
2759 * @buffer: a #GstBuffer
2760 * @reference: (allow-none): a reference #GstCaps
2762 * Finds the first #GstReferenceTimestampMeta on @buffer that conforms to
2763 * @reference. Conformance is tested by checking if the meta's reference is a
2764 * subset of @reference.
2766 * Buffers can contain multiple #GstReferenceTimestampMeta metadata items.
2768 * Returns: (transfer none) (nullable): the #GstReferenceTimestampMeta or %NULL when there
2769 * is no such metadata on @buffer.
2773 GstReferenceTimestampMeta *
2774 gst_buffer_get_reference_timestamp_meta (GstBuffer * buffer,
2775 GstCaps * reference)
2777 gpointer state = NULL;
2779 const GstMetaInfo *info = GST_REFERENCE_TIMESTAMP_META_INFO;
2781 while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
2782 if (meta->info->api == info->api) {
2783 GstReferenceTimestampMeta *rmeta = (GstReferenceTimestampMeta *) meta;
2787 if (gst_caps_is_subset (rmeta->reference, reference))
2795 _gst_reference_timestamp_meta_transform (GstBuffer * dest, GstMeta * meta,
2796 GstBuffer * buffer, GQuark type, gpointer data)
2798 GstReferenceTimestampMeta *dmeta, *smeta;
2800 /* we copy over the reference timestamp meta, independent of transformation
2801 * that happens. If it applied to the original buffer, it still applies to
2802 * the new buffer as it refers to the time when the media was captured */
2803 smeta = (GstReferenceTimestampMeta *) meta;
2805 gst_buffer_add_reference_timestamp_meta (dest, smeta->reference,
2806 smeta->timestamp, smeta->duration);
2810 GST_CAT_DEBUG (gst_reference_timestamp_meta_debug,
2811 "copy reference timestamp metadata from buffer %p to %p", buffer, dest);
2817 _gst_reference_timestamp_meta_free (GstReferenceTimestampMeta * meta,
2820 if (meta->reference)
2821 gst_caps_unref (meta->reference);
2825 _gst_reference_timestamp_meta_init (GstReferenceTimestampMeta * meta,
2826 gpointer params, GstBuffer * buffer)
2830 if (g_once_init_enter (&_init)) {
2831 GST_DEBUG_CATEGORY_INIT (gst_reference_timestamp_meta_debug,
2832 "referencetimestampmeta", 0, "referencetimestampmeta");
2833 g_once_init_leave (&_init, 1);
2836 meta->reference = NULL;
2837 meta->timestamp = GST_CLOCK_TIME_NONE;
2838 meta->duration = GST_CLOCK_TIME_NONE;
2844 * gst_reference_timestamp_meta_api_get_type: (attributes doc.skip=true)
2847 gst_reference_timestamp_meta_api_get_type (void)
2849 static GType type = 0;
2850 static const gchar *tags[] = { NULL };
2852 if (g_once_init_enter (&type)) {
2854 gst_meta_api_type_register ("GstReferenceTimestampMetaAPI", tags);
2855 g_once_init_leave (&type, _type);
2862 * gst_reference_timestamp_meta_get_info:
2864 * Gets the global #GstMetaInfo describing the #GstReferenceTimestampMeta meta.
2866 * Returns: (transfer none): The #GstMetaInfo
2871 gst_reference_timestamp_meta_get_info (void)
2873 static const GstMetaInfo *meta_info = NULL;
2875 if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
2876 const GstMetaInfo *meta =
2877 gst_meta_register (gst_reference_timestamp_meta_api_get_type (),
2878 "GstReferenceTimestampMeta",
2879 sizeof (GstReferenceTimestampMeta),
2880 (GstMetaInitFunction) _gst_reference_timestamp_meta_init,
2881 (GstMetaFreeFunction) _gst_reference_timestamp_meta_free,
2882 _gst_reference_timestamp_meta_transform);
2883 g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) meta);
2890 * gst_buffer_add_custom_meta:
2891 * @buffer: (transfer none): a #GstBuffer
2892 * @name: the registered name of the desired custom meta
2894 * Creates and adds a #GstCustomMeta for the desired @name. @name must have
2895 * been successfully registered with gst_meta_register_custom().
2897 * Returns: (transfer none) (nullable): The #GstCustomMeta that was added to the buffer
2902 gst_buffer_add_custom_meta (GstBuffer * buffer, const gchar * name)
2904 GstCustomMeta *meta;
2905 const GstMetaInfo *info;
2907 g_return_val_if_fail (name != NULL, NULL);
2908 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
2910 info = gst_meta_get_info (name);
2912 if (info == NULL || !gst_meta_info_is_custom (info))
2915 meta = (GstCustomMeta *) gst_buffer_add_meta (buffer, info, NULL);
2921 * gst_buffer_get_custom_meta:
2922 * @buffer: a #GstBuffer
2923 * @name: the registered name of the custom meta to retrieve.
2925 * Finds the first #GstCustomMeta on @buffer for the desired @name.
2927 * Returns: (transfer none) (nullable): the #GstCustomMeta
2932 gst_buffer_get_custom_meta (GstBuffer * buffer, const gchar * name)
2934 const GstMetaInfo *info;
2936 g_return_val_if_fail (buffer != NULL, NULL);
2937 g_return_val_if_fail (name != NULL, NULL);
2939 info = gst_meta_get_info (name);
2944 if (!gst_meta_info_is_custom (info))
2947 return (GstCustomMeta *) gst_buffer_get_meta (buffer, info->api);
2951 * gst_buffer_ref: (skip)
2952 * @buf: a #GstBuffer.
2954 * Increases the refcount of the given buffer by one.
2956 * Note that the refcount affects the writability
2957 * of @buf and its metadata, see gst_buffer_is_writable().
2958 * It is important to note that keeping additional references to
2959 * GstBuffer instances can potentially increase the number
2960 * of `memcpy` operations in a pipeline.
2962 * Returns: (transfer full): @buf
2965 gst_buffer_ref (GstBuffer * buf)
2967 return (GstBuffer *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (buf));
2971 * gst_buffer_unref: (skip)
2972 * @buf: (transfer full): a #GstBuffer.
2974 * Decreases the refcount of the buffer. If the refcount reaches 0, the buffer
2975 * with the associated metadata and memory will be freed.
2978 gst_buffer_unref (GstBuffer * buf)
2980 gst_mini_object_unref (GST_MINI_OBJECT_CAST (buf));
2984 * gst_clear_buffer: (skip)
2985 * @buf_ptr: a pointer to a #GstBuffer reference
2987 * Clears a reference to a #GstBuffer.
2989 * @buf_ptr must not be %NULL.
2991 * If the reference is %NULL then this function does nothing. Otherwise, the
2992 * reference count of the buffer is decreased and the pointer is set to %NULL.
2997 gst_clear_buffer (GstBuffer ** buf_ptr)
2999 gst_clear_mini_object ((GstMiniObject **) buf_ptr);
3003 * gst_buffer_copy: (skip)
3004 * @buf: a #GstBuffer.
3006 * Creates a copy of the given buffer. This will only copy the buffer's
3007 * data to a newly allocated memory if needed (if the type of memory
3008 * requires it), otherwise the underlying data is just referenced.
3009 * Check gst_buffer_copy_deep() if you want to force the data
3010 * to be copied to newly allocated memory.
3012 * Returns: (transfer full) (nullable): a new copy of @buf if the copy succeeded, %NULL otherwise.
3015 gst_buffer_copy (const GstBuffer * buf)
3017 return GST_BUFFER (gst_mini_object_copy (GST_MINI_OBJECT_CONST_CAST (buf)));
3021 * gst_buffer_replace: (skip)
3022 * @obuf: (inout) (transfer full) (nullable): pointer to a pointer to
3023 * a #GstBuffer to be replaced.
3024 * @nbuf: (transfer none) (allow-none): pointer to a #GstBuffer that will
3025 * replace the buffer pointed to by @obuf.
3027 * Modifies a pointer to a #GstBuffer to point to a different #GstBuffer. The
3028 * modification is done atomically (so this is useful for ensuring thread safety
3029 * in some cases), and the reference counts are updated appropriately (the old
3030 * buffer is unreffed, the new is reffed).
3032 * Either @nbuf or the #GstBuffer pointed to by @obuf may be %NULL.
3034 * Returns: %TRUE when @obuf was different from @nbuf.
3037 gst_buffer_replace (GstBuffer ** obuf, GstBuffer * nbuf)
3039 return gst_mini_object_replace ((GstMiniObject **) obuf,
3040 (GstMiniObject *) nbuf);