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.
37 * |[<!-- language="C" -->
40 * gint size, width, height, bpp;
42 * size = width * height * bpp;
43 * buffer = gst_buffer_new ();
44 * memory = gst_allocator_alloc (NULL, size, NULL);
45 * gst_buffer_insert_memory (buffer, -1, memory);
49 * Alternatively, use gst_buffer_new_allocate() to create a buffer with
50 * preallocated data of a given size.
52 * Buffers can contain a list of #GstMemory objects. You can retrieve how many
53 * memory objects with gst_buffer_n_memory() and you can get a pointer
54 * to memory with gst_buffer_peek_memory()
56 * A buffer will usually have timestamps, and a duration, but neither of these
57 * are guaranteed (they may be set to #GST_CLOCK_TIME_NONE). Whenever a
58 * meaningful value can be given for these, they should be set. The timestamps
59 * and duration are measured in nanoseconds (they are #GstClockTime values).
61 * The buffer DTS refers to the timestamp when the buffer should be decoded and
62 * is usually monotonically increasing. The buffer PTS refers to the timestamp when
63 * the buffer content should be presented to the user and is not always
64 * monotonically increasing.
66 * A buffer can also have one or both of a start and an end offset. These are
67 * media-type specific. For video buffers, the start offset will generally be
68 * the frame number. For audio buffers, it will be the number of samples
69 * produced so far. For compressed data, it could be the byte offset in a
70 * source or destination file. Likewise, the end offset will be the offset of
71 * the end of the buffer. These can only be meaningfully interpreted if you
72 * know the media type of the buffer (the preceding CAPS event). Either or both
73 * can be set to #GST_BUFFER_OFFSET_NONE.
75 * gst_buffer_ref() is used to increase the refcount of a buffer. This must be
76 * done when you want to keep a handle to the buffer after pushing it to the
77 * next element. The buffer refcount determines the writability of the buffer, a
78 * buffer is only writable when the refcount is exactly 1, i.e. when the caller
79 * has the only reference to the buffer.
81 * To efficiently create a smaller buffer out of an existing one, you can
82 * use gst_buffer_copy_region(). This method tries to share the memory objects
83 * between the two buffers.
85 * If a plug-in wants to modify the buffer data or metadata in-place, it should
86 * first obtain a buffer that is safe to modify by using
87 * gst_buffer_make_writable(). This function is optimized so that a copy will
88 * only be made when it is necessary.
90 * Several flags of the buffer can be set and unset with the
91 * GST_BUFFER_FLAG_SET() and GST_BUFFER_FLAG_UNSET() macros. Use
92 * GST_BUFFER_FLAG_IS_SET() to test if a certain #GstBufferFlags flag is set.
94 * Buffers can be efficiently merged into a larger buffer with
95 * gst_buffer_append(). Copying of memory will only be done when absolutely
98 * Arbitrary extra metadata can be set on a buffer with gst_buffer_add_meta().
99 * Metadata can be retrieved with gst_buffer_get_meta(). See also #GstMeta
101 * An element should either unref the buffer or push it out on a src pad
102 * using gst_pad_push() (see #GstPad).
104 * Buffers are usually freed by unreffing them with gst_buffer_unref(). When
105 * the refcount drops to 0, any memory and metadata pointed to by the buffer is
106 * unreffed as well. Buffers allocated from a #GstBufferPool will be returned to
107 * the pool when the refcount drops to 0.
109 * The #GstParentBufferMeta is a meta which can be attached to a #GstBuffer
110 * to hold a reference to another buffer that is only released when the child
111 * #GstBuffer is released.
113 * Typically, #GstParentBufferMeta is used when the child buffer is directly
114 * using the #GstMemory of the parent buffer, and wants to prevent the parent
115 * buffer from being returned to a buffer pool until the #GstMemory is available
116 * for re-use. (Since 1.6)
119 #include "gst_private.h"
128 #include "gstbuffer.h"
129 #include "gstbufferpool.h"
131 #include "gstutils.h"
132 #include "gstversion.h"
134 GType _gst_buffer_type = 0;
136 /* info->size will be sizeof(FooMeta) which contains a GstMeta at the beginning
137 * too, and then there is again a GstMeta in GstMetaItem, so subtract one. */
138 #define ITEM_SIZE(info) ((info)->size + sizeof (GstMetaItem) - sizeof (GstMeta))
140 #define GST_BUFFER_MEM_MAX 16
142 #define GST_BUFFER_SLICE_SIZE(b) (((GstBufferImpl *)(b))->slice_size)
143 #define GST_BUFFER_MEM_LEN(b) (((GstBufferImpl *)(b))->len)
144 #define GST_BUFFER_MEM_ARRAY(b) (((GstBufferImpl *)(b))->mem)
145 #define GST_BUFFER_MEM_PTR(b,i) (((GstBufferImpl *)(b))->mem[i])
146 #define GST_BUFFER_BUFMEM(b) (((GstBufferImpl *)(b))->bufmem)
147 #define GST_BUFFER_META(b) (((GstBufferImpl *)(b))->item)
155 /* the memory blocks */
157 GstMemory *mem[GST_BUFFER_MEM_MAX];
159 /* memory of the buffer when allocated from 1 chunk */
162 /* FIXME, make metadata allocation more efficient by using part of the
167 static gint64 meta_seq; /* 0 *//* ATOMIC */
169 /* TODO: use GLib's once https://gitlab.gnome.org/GNOME/glib/issues/1076 lands */
170 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
172 gst_atomic_int64_inc (volatile gint64 * atomic)
174 return __sync_fetch_and_add (atomic, 1);
176 #elif defined (G_PLATFORM_WIN32)
179 gst_atomic_int64_inc (volatile gint64 * atomic)
181 return InterlockedExchangeAdd64 (atomic, 1);
184 #warning No 64-bit atomic int defined for this platform/toolchain!
185 #define NO_64BIT_ATOMIC_INT_FOR_PLATFORM
186 G_LOCK_DEFINE_STATIC (meta_seq);
188 gst_atomic_int64_inc (volatile gint64 * atomic)
201 _is_span (GstMemory ** mem, gsize len, gsize * poffset, GstMemory ** parent)
203 GstMemory *mcur, *mprv;
204 gboolean have_offset = FALSE;
209 for (i = 0; i < len; i++) {
217 /* check if memory is contiguous */
218 if (!gst_memory_is_span (mprv, mcur, &poffs))
225 *parent = mprv->parent;
235 _get_merged_memory (GstBuffer * buffer, guint idx, guint length)
237 GstMemory **mem, *result = NULL;
239 GST_CAT_LOG (GST_CAT_BUFFER, "buffer %p, idx %u, length %u", buffer, idx,
242 mem = GST_BUFFER_MEM_ARRAY (buffer);
244 if (G_UNLIKELY (length == 0)) {
246 } else if (G_LIKELY (length == 1)) {
247 result = gst_memory_ref (mem[idx]);
249 GstMemory *parent = NULL;
250 gsize size, poffset = 0;
252 size = gst_buffer_get_sizes_range (buffer, idx, length, NULL, NULL);
254 if (G_UNLIKELY (_is_span (mem + idx, length, &poffset, &parent))) {
255 if (!GST_MEMORY_IS_NO_SHARE (parent))
256 result = gst_memory_share (parent, poffset, size);
258 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy for merge %p", parent);
259 result = gst_memory_copy (parent, poffset, size);
262 gsize i, tocopy, left;
263 GstMapInfo sinfo, dinfo;
266 result = gst_allocator_alloc (NULL, size, NULL);
267 if (result == NULL || !gst_memory_map (result, &dinfo, GST_MAP_WRITE)) {
268 GST_CAT_ERROR (GST_CAT_BUFFER, "Failed to map memory writable");
270 gst_memory_unref (result);
277 for (i = idx; i < (idx + length) && left > 0; i++) {
278 if (!gst_memory_map (mem[i], &sinfo, GST_MAP_READ)) {
279 GST_CAT_ERROR (GST_CAT_BUFFER,
280 "buffer %p, idx %u, length %u failed to map readable", buffer,
282 gst_memory_unmap (result, &dinfo);
283 gst_memory_unref (result);
286 tocopy = MIN (sinfo.size, left);
287 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
288 "memcpy %" G_GSIZE_FORMAT " bytes for merge %p from memory %p",
289 tocopy, result, mem[i]);
290 memcpy (ptr, (guint8 *) sinfo.data, tocopy);
293 gst_memory_unmap (mem[i], &sinfo);
295 gst_memory_unmap (result, &dinfo);
302 _replace_memory (GstBuffer * buffer, guint len, guint idx, guint length,
309 GST_CAT_LOG (GST_CAT_BUFFER,
310 "buffer %p replace %u-%" G_GSIZE_FORMAT " with memory %p", buffer, idx,
313 /* unref old memory */
314 for (i = idx; i < end; i++) {
315 GstMemory *old = GST_BUFFER_MEM_PTR (buffer, i);
317 gst_memory_unlock (old, GST_LOCK_FLAG_EXCLUSIVE);
318 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (old),
319 GST_MINI_OBJECT_CAST (buffer));
320 gst_memory_unref (old);
324 /* replace with single memory */
325 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (mem),
326 GST_MINI_OBJECT_CAST (buffer));
327 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
328 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
334 memmove (&GST_BUFFER_MEM_PTR (buffer, idx),
335 &GST_BUFFER_MEM_PTR (buffer, end), (len - end) * sizeof (gpointer));
337 GST_BUFFER_MEM_LEN (buffer) = len - length;
338 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
342 * gst_buffer_get_flags:
343 * @buffer: a #GstBuffer
345 * Get the #GstBufferFlags flags set on this buffer.
347 * Returns: the flags set on this buffer.
352 gst_buffer_get_flags (GstBuffer * buffer)
354 return (GstBufferFlags) GST_BUFFER_FLAGS (buffer);
358 * gst_buffer_flag_is_set:
359 * @buffer: a #GstBuffer
360 * @flags: the #GstBufferFlags flag to check.
362 * Gives the status of a specific flag on a buffer.
364 * Returns: %TRUE if all flags in @flags are found on @buffer.
369 gst_buffer_has_flags (GstBuffer * buffer, GstBufferFlags flags)
371 return GST_BUFFER_FLAG_IS_SET (buffer, flags);
375 * gst_buffer_set_flags:
376 * @buffer: a #GstBuffer
377 * @flags: the #GstBufferFlags to set.
379 * Sets one or more buffer flags on a buffer.
381 * Returns: %TRUE if @flags were successfully set on buffer.
386 gst_buffer_set_flags (GstBuffer * buffer, GstBufferFlags flags)
388 GST_BUFFER_FLAG_SET (buffer, flags);
393 * gst_buffer_unset_flags:
394 * @buffer: a #GstBuffer
395 * @flags: the #GstBufferFlags to clear
397 * Clears one or more buffer flags.
399 * Returns: true if @flags is successfully cleared from buffer.
404 gst_buffer_unset_flags (GstBuffer * buffer, GstBufferFlags flags)
406 GST_BUFFER_FLAG_UNSET (buffer, flags);
412 /* transfer full for return and transfer none for @mem */
413 static inline GstMemory *
414 _memory_get_exclusive_reference (GstMemory * mem)
416 GstMemory *ret = NULL;
418 if (gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE)) {
419 ret = gst_memory_ref (mem);
421 /* we cannot take another exclusive lock as the memory is already
422 * locked WRITE + EXCLUSIVE according to part-miniobject.txt */
423 ret = gst_memory_copy (mem, 0, -1);
426 if (!gst_memory_lock (ret, GST_LOCK_FLAG_EXCLUSIVE)) {
427 gst_memory_unref (ret);
434 GST_CAT_WARNING (GST_CAT_BUFFER, "Failed to acquire an exclusive lock for "
441 _memory_add (GstBuffer * buffer, gint idx, GstMemory * mem)
443 guint i, len = GST_BUFFER_MEM_LEN (buffer);
445 GST_CAT_LOG (GST_CAT_BUFFER, "buffer %p, idx %d, mem %p", buffer, idx, mem);
447 if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) {
448 /* too many buffer, span them. */
449 /* FIXME, there is room for improvement here: We could only try to merge
450 * 2 buffers to make some room. If we can't efficiently merge 2 buffers we
451 * could try to only merge the two smallest buffers to avoid memcpy, etc. */
452 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "memory array overflow in buffer %p",
454 _replace_memory (buffer, len, 0, len, _get_merged_memory (buffer, 0, len));
455 /* we now have 1 single spanned buffer */
462 for (i = len; i > idx; i--) {
463 /* move buffers to insert, FIXME, we need to insert first and then merge */
464 GST_BUFFER_MEM_PTR (buffer, i) = GST_BUFFER_MEM_PTR (buffer, i - 1);
466 /* and insert the new buffer */
467 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
468 GST_BUFFER_MEM_LEN (buffer) = len + 1;
469 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (mem),
470 GST_MINI_OBJECT_CAST (buffer));
472 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
475 GST_DEFINE_MINI_OBJECT_TYPE (GstBuffer, gst_buffer);
478 _priv_gst_buffer_initialize (void)
480 _gst_buffer_type = gst_buffer_get_type ();
482 #ifdef NO_64BIT_ATOMIC_INT_FOR_PLATFORM
483 GST_CAT_WARNING (GST_CAT_PERFORMANCE,
484 "No 64-bit atomic int defined for this platform/toolchain!");
489 * gst_buffer_get_max_memory:
491 * Get the maximum amount of memory blocks that a buffer can hold. This is a
492 * compile time constant that can be queried with the function.
494 * When more memory blocks are added, existing memory blocks will be merged
495 * together to make room for the new block.
497 * Returns: the maximum amount of memory blocks that a buffer can hold.
502 gst_buffer_get_max_memory (void)
504 return GST_BUFFER_MEM_MAX;
508 * gst_buffer_copy_into:
509 * @dest: a destination #GstBuffer
510 * @src: a source #GstBuffer
511 * @flags: flags indicating what metadata fields should be copied.
512 * @offset: offset to copy from
513 * @size: total size to copy. If -1, all data is copied.
515 * Copies the information from @src into @dest.
517 * If @dest already contains memory and @flags contains GST_BUFFER_COPY_MEMORY,
518 * the memory from @src will be appended to @dest.
520 * @flags indicate which fields will be copied.
522 * Returns: %TRUE if the copying succeeded, %FALSE otherwise.
525 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
526 GstBufferCopyFlags flags, gsize offset, gsize size)
530 gboolean region = FALSE;
532 g_return_val_if_fail (dest != NULL, FALSE);
533 g_return_val_if_fail (src != NULL, FALSE);
535 /* nothing to copy if the buffers are the same */
536 if (G_UNLIKELY (dest == src))
539 g_return_val_if_fail (gst_buffer_is_writable (dest), FALSE);
541 bufsize = gst_buffer_get_size (src);
542 g_return_val_if_fail (bufsize >= offset, FALSE);
546 size = bufsize - offset;
549 g_return_val_if_fail (bufsize >= offset + size, FALSE);
551 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p, offset %" G_GSIZE_FORMAT
552 "-%" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT, src, dest, offset, size,
555 if (flags & GST_BUFFER_COPY_FLAGS) {
557 guint flags_mask = ~GST_BUFFER_FLAG_TAG_MEMORY;
559 GST_MINI_OBJECT_FLAGS (dest) =
560 (GST_MINI_OBJECT_FLAGS (src) & flags_mask) |
561 (GST_MINI_OBJECT_FLAGS (dest) & ~flags_mask);
564 if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
566 GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src);
567 GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src);
568 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
569 if (size == bufsize) {
570 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
571 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
574 GST_BUFFER_PTS (dest) = GST_CLOCK_TIME_NONE;
575 GST_BUFFER_DTS (dest) = GST_CLOCK_TIME_NONE;
576 GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
577 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
578 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
582 if (flags & GST_BUFFER_COPY_MEMORY) {
583 gsize skip, left, len, dest_len, i, bsize;
586 deep = flags & GST_BUFFER_COPY_DEEP;
588 len = GST_BUFFER_MEM_LEN (src);
589 dest_len = GST_BUFFER_MEM_LEN (dest);
593 /* copy and make regions of the memory */
594 for (i = 0; i < len && left > 0; i++) {
595 GstMemory *mem = GST_BUFFER_MEM_PTR (src, i);
597 bsize = gst_memory_get_sizes (mem, NULL, NULL);
600 /* don't copy buffer */
603 GstMemory *newmem = NULL;
606 tocopy = MIN (bsize - skip, left);
608 if (tocopy < bsize && !deep && !GST_MEMORY_IS_NO_SHARE (mem)) {
609 /* we need to clip something */
610 newmem = gst_memory_share (mem, skip, tocopy);
612 gst_memory_lock (newmem, GST_LOCK_FLAG_EXCLUSIVE);
617 if (deep || GST_MEMORY_IS_NO_SHARE (mem) || (!newmem && tocopy < bsize)) {
618 /* deep copy or we're not allowed to share this memory
619 * between buffers, always copy then */
620 newmem = gst_memory_copy (mem, skip, tocopy);
622 gst_memory_lock (newmem, GST_LOCK_FLAG_EXCLUSIVE);
625 } else if (!newmem) {
626 newmem = _memory_get_exclusive_reference (mem);
630 gst_buffer_remove_memory_range (dest, dest_len, -1);
634 _memory_add (dest, -1, newmem);
638 if (flags & GST_BUFFER_COPY_MERGE) {
641 len = GST_BUFFER_MEM_LEN (dest);
642 mem = _get_merged_memory (dest, 0, len);
644 gst_buffer_remove_memory_range (dest, dest_len, -1);
647 _replace_memory (dest, len, 0, len, mem);
651 if (flags & GST_BUFFER_COPY_META) {
652 /* NOTE: GstGLSyncMeta copying relies on the meta
653 * being copied now, after the buffer data,
654 * so this has to happen last */
655 for (walk = GST_BUFFER_META (src); walk; walk = walk->next) {
656 GstMeta *meta = &walk->meta;
657 const GstMetaInfo *info = meta->info;
659 /* Don't copy memory metas if we only copied part of the buffer, didn't
660 * copy memories or merged memories. In all these cases the memory
661 * structure has changed and the memory meta becomes meaningless.
663 if ((region || !(flags & GST_BUFFER_COPY_MEMORY)
664 || (flags & GST_BUFFER_COPY_MERGE))
665 && gst_meta_api_type_has_tag (info->api, _gst_meta_tag_memory)) {
666 GST_CAT_DEBUG (GST_CAT_BUFFER,
667 "don't copy memory meta %p of API type %s", meta,
668 g_type_name (info->api));
669 } else if (info->transform_func) {
670 GstMetaTransformCopy copy_data;
672 copy_data.region = region;
673 copy_data.offset = offset;
674 copy_data.size = size;
676 if (!info->transform_func (dest, meta, src,
677 _gst_meta_transform_copy, ©_data)) {
678 GST_CAT_ERROR (GST_CAT_BUFFER,
679 "failed to copy meta %p of API type %s", meta,
680 g_type_name (info->api));
690 gst_buffer_copy_with_flags (const GstBuffer * buffer, GstBufferCopyFlags flags)
694 g_return_val_if_fail (buffer != NULL, NULL);
696 /* create a fresh new buffer */
697 copy = gst_buffer_new ();
699 /* copy what the 'flags' want from our parent */
700 /* FIXME why we can't pass const to gst_buffer_copy_into() ? */
701 if (!gst_buffer_copy_into (copy, (GstBuffer *) buffer, flags, 0, -1))
702 gst_buffer_replace (©, NULL);
705 GST_BUFFER_FLAG_UNSET (copy, GST_BUFFER_FLAG_TAG_MEMORY);
711 _gst_buffer_copy (const GstBuffer * buffer)
713 return gst_buffer_copy_with_flags (buffer, GST_BUFFER_COPY_ALL);
717 * gst_buffer_copy_deep:
718 * @buf: a #GstBuffer.
720 * Create a copy of the given buffer. This will make a newly allocated
721 * copy of the data the source buffer contains.
723 * Returns: (transfer full): a new copy of @buf.
728 gst_buffer_copy_deep (const GstBuffer * buffer)
730 return gst_buffer_copy_with_flags (buffer,
731 GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP);
734 /* the default dispose function revives the buffer and returns it to the
735 * pool when there is a pool */
737 _gst_buffer_dispose (GstBuffer * buffer)
741 /* no pool, do free */
742 if ((pool = buffer->pool) == NULL)
745 /* keep the buffer alive */
746 gst_buffer_ref (buffer);
747 /* return the buffer to the pool */
748 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
749 gst_buffer_pool_release_buffer (pool, buffer);
755 _gst_buffer_free (GstBuffer * buffer)
757 GstMetaItem *walk, *next;
761 g_return_if_fail (buffer != NULL);
763 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
766 for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
767 GstMeta *meta = &walk->meta;
768 const GstMetaInfo *info = meta->info;
770 /* call free_func if any */
772 info->free_func (meta, buffer);
775 /* and free the slice */
776 g_slice_free1 (ITEM_SIZE (info), walk);
779 /* get the size, when unreffing the memory, we could also unref the buffer
781 msize = GST_BUFFER_SLICE_SIZE (buffer);
783 /* free our memory */
784 len = GST_BUFFER_MEM_LEN (buffer);
785 for (i = 0; i < len; i++) {
786 gst_memory_unlock (GST_BUFFER_MEM_PTR (buffer, i), GST_LOCK_FLAG_EXCLUSIVE);
787 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (GST_BUFFER_MEM_PTR
788 (buffer, i)), GST_MINI_OBJECT_CAST (buffer));
789 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
792 /* we set msize to 0 when the buffer is part of the memory block */
795 memset (buffer, 0xff, msize);
797 g_slice_free1 (msize, buffer);
799 gst_memory_unref (GST_BUFFER_BUFMEM (buffer));
804 gst_buffer_init (GstBufferImpl * buffer, gsize size)
806 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), 0, _gst_buffer_type,
807 (GstMiniObjectCopyFunction) _gst_buffer_copy,
808 (GstMiniObjectDisposeFunction) _gst_buffer_dispose,
809 (GstMiniObjectFreeFunction) _gst_buffer_free);
811 GST_BUFFER_SLICE_SIZE (buffer) = size;
813 GST_BUFFER (buffer)->pool = NULL;
814 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
815 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
816 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
817 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
818 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
820 GST_BUFFER_MEM_LEN (buffer) = 0;
821 GST_BUFFER_META (buffer) = NULL;
827 * Creates a newly allocated buffer without any data.
831 * Returns: (transfer full): the new #GstBuffer.
834 gst_buffer_new (void)
836 GstBufferImpl *newbuf;
838 newbuf = g_slice_new (GstBufferImpl);
839 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
841 gst_buffer_init (newbuf, sizeof (GstBufferImpl));
843 return GST_BUFFER_CAST (newbuf);
847 * gst_buffer_new_allocate:
848 * @allocator: (transfer none) (allow-none): the #GstAllocator to use, or %NULL to use the
850 * @size: the size in bytes of the new buffer's data.
851 * @params: (transfer none) (allow-none): optional parameters
853 * Tries to create a newly allocated buffer with data of the given size and
854 * extra parameters from @allocator. If the requested amount of memory can't be
855 * allocated, %NULL will be returned. The allocated buffer memory is not cleared.
857 * When @allocator is %NULL, the default memory allocator will be used.
859 * Note that when @size == 0, the buffer will not have memory associated with it.
863 * Returns: (transfer full) (nullable): a new #GstBuffer, or %NULL if
864 * the memory couldn't be allocated.
867 gst_buffer_new_allocate (GstAllocator * allocator, gsize size,
868 GstAllocationParams * params)
879 mem = gst_allocator_alloc (allocator, size, params);
880 if (G_UNLIKELY (mem == NULL))
886 newbuf = gst_buffer_new ();
889 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
890 _memory_add (newbuf, -1, mem);
893 GST_CAT_LOG (GST_CAT_BUFFER,
894 "new buffer %p of size %" G_GSIZE_FORMAT " from allocator %p", newbuf,
899 asize = sizeof (GstBufferImpl) + size;
900 data = g_slice_alloc (asize);
901 if (G_UNLIKELY (data == NULL))
904 newbuf = GST_BUFFER_CAST (data);
906 gst_buffer_init ((GstBufferImpl *) data, asize);
908 mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL,
910 _memory_add (newbuf, -1, mem, TRUE);
915 /* allocate memory and buffer, it might be interesting to do this but there
916 * are many complications. We need to keep the memory mapped to access the
917 * buffer fields and the memory for the buffer might be just very slow. We
918 * also need to do some more magic to get the alignment right. */
919 asize = sizeof (GstBufferImpl) + size;
920 mem = gst_allocator_alloc (allocator, asize, align);
921 if (G_UNLIKELY (mem == NULL))
924 /* map the data part and init the buffer in it, set the buffer size to 0 so
925 * that a finalize won't free the buffer */
926 data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
927 gst_buffer_init ((GstBufferImpl *) data, 0);
928 gst_memory_unmap (mem);
930 /* strip off the buffer */
931 gst_memory_resize (mem, sizeof (GstBufferImpl), size);
933 newbuf = GST_BUFFER_CAST (data);
934 GST_BUFFER_BUFMEM (newbuf) = mem;
937 _memory_add (newbuf, -1, gst_memory_ref (mem), TRUE);
939 GST_BUFFER_FLAG_UNSET (newbuf, GST_BUFFER_FLAG_TAG_MEMORY);
946 GST_CAT_WARNING (GST_CAT_BUFFER,
947 "failed to allocate %" G_GSIZE_FORMAT " bytes", size);
953 * gst_buffer_new_wrapped_full:
954 * @flags: #GstMemoryFlags
955 * @data: (array length=size) (element-type guint8) (transfer none): data to wrap
956 * @maxsize: allocated size of @data
957 * @offset: offset in @data
958 * @size: size of valid data
959 * @user_data: (allow-none): user_data
960 * @notify: (allow-none) (scope async) (closure user_data): called with @user_data when the memory is freed
962 * Allocate a new buffer that wraps the given memory. @data must point to
963 * @maxsize of memory, the wrapped buffer will have the region from @offset and
966 * When the buffer is destroyed, @notify will be called with @user_data.
968 * The prefix/padding must be filled with 0 if @flags contains
969 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
971 * Returns: (transfer full): a new #GstBuffer
974 gst_buffer_new_wrapped_full (GstMemoryFlags flags, gpointer data,
975 gsize maxsize, gsize offset, gsize size, gpointer user_data,
976 GDestroyNotify notify)
981 newbuf = gst_buffer_new ();
983 gst_memory_new_wrapped (flags, data, maxsize, offset, size, user_data,
985 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
986 _memory_add (newbuf, -1, mem);
987 GST_BUFFER_FLAG_UNSET (newbuf, GST_BUFFER_FLAG_TAG_MEMORY);
993 * gst_buffer_new_wrapped:
994 * @data: (array length=size) (element-type guint8) (transfer full): data to wrap
995 * @size: allocated size of @data
997 * Creates a new buffer that wraps the given @data. The memory will be freed
998 * with g_free and will be marked writable.
1002 * Returns: (transfer full): a new #GstBuffer
1005 gst_buffer_new_wrapped (gpointer data, gsize size)
1007 return gst_buffer_new_wrapped_full (0, data, size, 0, size, data, g_free);
1011 * gst_buffer_new_wrapped_bytes:
1012 * @bytes: (transfer none): a #GBytes to wrap
1014 * Creates a new #GstBuffer that wraps the given @bytes. The data inside
1015 * @bytes cannot be %NULL and the resulting buffer will be marked as read only.
1019 * Returns: (transfer full): a new #GstBuffer wrapping @bytes
1024 gst_buffer_new_wrapped_bytes (GBytes * bytes)
1029 g_return_val_if_fail (bytes != NULL, NULL);
1030 bytes_data = (guint8 *) g_bytes_get_data (bytes, &size);
1031 g_return_val_if_fail (bytes_data != NULL, NULL);
1033 return gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, bytes_data,
1034 size, 0, size, g_bytes_ref (bytes), (GDestroyNotify) g_bytes_unref);
1038 * gst_buffer_n_memory:
1039 * @buffer: a #GstBuffer.
1041 * Get the amount of memory blocks that this buffer has. This amount is never
1042 * larger than what gst_buffer_get_max_memory() returns.
1044 * Returns: the number of memory blocks this buffer is made of.
1047 gst_buffer_n_memory (GstBuffer * buffer)
1049 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1051 return GST_BUFFER_MEM_LEN (buffer);
1055 * gst_buffer_prepend_memory:
1056 * @buffer: a #GstBuffer.
1057 * @mem: (transfer full): a #GstMemory.
1059 * Prepend the memory block @mem to @buffer. This function takes
1060 * ownership of @mem and thus doesn't increase its refcount.
1062 * This function is identical to gst_buffer_insert_memory() with an index of 0.
1063 * See gst_buffer_insert_memory() for more details.
1066 gst_buffer_prepend_memory (GstBuffer * buffer, GstMemory * mem)
1068 gst_buffer_insert_memory (buffer, 0, mem);
1072 * gst_buffer_append_memory:
1073 * @buffer: a #GstBuffer.
1074 * @mem: (transfer full): a #GstMemory.
1076 * Append the memory block @mem to @buffer. This function takes
1077 * ownership of @mem and thus doesn't increase its refcount.
1079 * This function is identical to gst_buffer_insert_memory() with an index of -1.
1080 * See gst_buffer_insert_memory() for more details.
1083 gst_buffer_append_memory (GstBuffer * buffer, GstMemory * mem)
1085 gst_buffer_insert_memory (buffer, -1, mem);
1089 * gst_buffer_insert_memory:
1090 * @buffer: a #GstBuffer.
1091 * @idx: the index to add the memory at, or -1 to append it to the end
1092 * @mem: (transfer full): a #GstMemory.
1094 * Insert the memory block @mem to @buffer at @idx. This function takes ownership
1095 * of @mem and thus doesn't increase its refcount.
1097 * Only gst_buffer_get_max_memory() can be added to a buffer. If more memory is
1098 * added, existing memory blocks will automatically be merged to make room for
1102 gst_buffer_insert_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
1106 g_return_if_fail (GST_IS_BUFFER (buffer));
1107 g_return_if_fail (gst_buffer_is_writable (buffer));
1108 g_return_if_fail (mem != NULL);
1109 g_return_if_fail (idx == -1 ||
1110 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)));
1112 tmp = _memory_get_exclusive_reference (mem);
1113 g_return_if_fail (tmp != NULL);
1114 gst_memory_unref (mem);
1115 _memory_add (buffer, idx, tmp);
1119 _get_mapped (GstBuffer * buffer, guint idx, GstMapInfo * info,
1122 GstMemory *mem, *mapped;
1124 mem = gst_memory_ref (GST_BUFFER_MEM_PTR (buffer, idx));
1126 mapped = gst_memory_make_mapped (mem, info, flags);
1128 if (mapped != mem) {
1129 /* memory changed, lock new memory */
1130 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (mapped),
1131 GST_MINI_OBJECT_CAST (buffer));
1132 gst_memory_lock (mapped, GST_LOCK_FLAG_EXCLUSIVE);
1133 GST_BUFFER_MEM_PTR (buffer, idx) = mapped;
1134 /* unlock old memory */
1135 gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
1136 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (mem),
1137 GST_MINI_OBJECT_CAST (buffer));
1138 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1140 gst_memory_unref (mem);
1146 * gst_buffer_peek_memory:
1147 * @buffer: a #GstBuffer.
1150 * Get the memory block at @idx in @buffer. The memory block stays valid until
1151 * the memory block in @buffer is removed, replaced or merged, typically with
1152 * any call that modifies the memory in @buffer.
1154 * Returns: (transfer none) (nullable): the #GstMemory at @idx.
1157 gst_buffer_peek_memory (GstBuffer * buffer, guint idx)
1161 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1162 len = GST_BUFFER_MEM_LEN (buffer);
1163 g_return_val_if_fail (idx < len, NULL);
1165 return GST_BUFFER_MEM_PTR (buffer, idx);
1169 * gst_buffer_get_memory:
1170 * @buffer: a #GstBuffer.
1173 * Get the memory block at index @idx in @buffer.
1175 * Returns: (transfer full) (nullable): a #GstMemory that contains the data of the
1176 * memory block at @idx. Use gst_memory_unref () after usage.
1179 gst_buffer_get_memory (GstBuffer * buffer, guint idx)
1181 return gst_buffer_get_memory_range (buffer, idx, 1);
1185 * gst_buffer_get_all_memory:
1186 * @buffer: a #GstBuffer.
1188 * Get all the memory block in @buffer. The memory blocks will be merged
1189 * into one large #GstMemory.
1191 * Returns: (transfer full) (nullable): a #GstMemory that contains the merged memory.
1192 * Use gst_memory_unref () after usage.
1195 gst_buffer_get_all_memory (GstBuffer * buffer)
1197 return gst_buffer_get_memory_range (buffer, 0, -1);
1201 * gst_buffer_get_memory_range:
1202 * @buffer: a #GstBuffer.
1206 * Get @length memory blocks in @buffer starting at @idx. The memory blocks will
1207 * be merged into one large #GstMemory.
1209 * If @length is -1, all memory starting from @idx is merged.
1211 * Returns: (transfer full) (nullable): a #GstMemory that contains the merged data of @length
1212 * blocks starting at @idx. Use gst_memory_unref () after usage.
1215 gst_buffer_get_memory_range (GstBuffer * buffer, guint idx, gint length)
1219 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
1221 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1222 len = GST_BUFFER_MEM_LEN (buffer);
1223 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1224 (length == -1 && idx < len) || (length > 0 && length + idx <= len), NULL);
1229 return _get_merged_memory (buffer, idx, length);
1233 * gst_buffer_replace_memory:
1234 * @buffer: a #GstBuffer.
1236 * @mem: (transfer full): a #GstMemory
1238 * Replaces the memory block at index @idx in @buffer with @mem.
1241 gst_buffer_replace_memory (GstBuffer * buffer, guint idx, GstMemory * mem)
1243 gst_buffer_replace_memory_range (buffer, idx, 1, mem);
1247 * gst_buffer_replace_all_memory:
1248 * @buffer: a #GstBuffer.
1249 * @mem: (transfer full): a #GstMemory
1251 * Replaces all memory in @buffer with @mem.
1254 gst_buffer_replace_all_memory (GstBuffer * buffer, GstMemory * mem)
1256 gst_buffer_replace_memory_range (buffer, 0, -1, mem);
1260 * gst_buffer_replace_memory_range:
1261 * @buffer: a #GstBuffer.
1263 * @length: a length should not be 0
1264 * @mem: (transfer full): a #GstMemory
1266 * Replaces @length memory blocks in @buffer starting at @idx with @mem.
1268 * If @length is -1, all memory starting from @idx will be removed and
1269 * replaced with @mem.
1271 * @buffer should be writable.
1274 gst_buffer_replace_memory_range (GstBuffer * buffer, guint idx, gint length,
1279 g_return_if_fail (GST_IS_BUFFER (buffer));
1280 g_return_if_fail (gst_buffer_is_writable (buffer));
1282 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d, %p", idx, length, mem);
1284 len = GST_BUFFER_MEM_LEN (buffer);
1285 g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
1286 (length == -1 && idx < len) || (length > 0 && length + idx <= len));
1291 _replace_memory (buffer, len, idx, length, mem);
1295 * gst_buffer_remove_memory:
1296 * @buffer: a #GstBuffer.
1299 * Remove the memory block in @b at index @i.
1302 gst_buffer_remove_memory (GstBuffer * buffer, guint idx)
1304 gst_buffer_remove_memory_range (buffer, idx, 1);
1308 * gst_buffer_remove_all_memory:
1309 * @buffer: a #GstBuffer.
1311 * Remove all the memory blocks in @buffer.
1314 gst_buffer_remove_all_memory (GstBuffer * buffer)
1316 gst_buffer_remove_memory_range (buffer, 0, -1);
1320 * gst_buffer_remove_memory_range:
1321 * @buffer: a #GstBuffer.
1325 * Remove @length memory blocks in @buffer starting from @idx.
1327 * @length can be -1, in which case all memory starting from @idx is removed.
1330 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, gint length)
1334 g_return_if_fail (GST_IS_BUFFER (buffer));
1335 g_return_if_fail (gst_buffer_is_writable (buffer));
1337 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
1339 len = GST_BUFFER_MEM_LEN (buffer);
1340 g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
1341 (length == -1 && idx < len) || length + idx <= len);
1346 _replace_memory (buffer, len, idx, length, NULL);
1350 * gst_buffer_find_memory:
1351 * @buffer: a #GstBuffer.
1352 * @offset: an offset
1354 * @idx: (out): pointer to index
1355 * @length: (out): pointer to length
1356 * @skip: (out): pointer to skip
1358 * Find the memory blocks that span @size bytes starting from @offset
1361 * When this function returns %TRUE, @idx will contain the index of the first
1362 * memory block where the byte for @offset can be found and @length contains the
1363 * number of memory blocks containing the @size remaining bytes. @skip contains
1364 * the number of bytes to skip in the memory block at @idx to get to the byte
1367 * @size can be -1 to get all the memory blocks after @idx.
1369 * Returns: %TRUE when @size bytes starting from @offset could be found in
1370 * @buffer and @idx, @length and @skip will be filled.
1373 gst_buffer_find_memory (GstBuffer * buffer, gsize offset, gsize size,
1374 guint * idx, guint * length, gsize * skip)
1376 guint i, len, found;
1378 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1379 g_return_val_if_fail (idx != NULL, FALSE);
1380 g_return_val_if_fail (length != NULL, FALSE);
1381 g_return_val_if_fail (skip != NULL, FALSE);
1383 len = GST_BUFFER_MEM_LEN (buffer);
1386 for (i = 0; i < len; i++) {
1390 mem = GST_BUFFER_MEM_PTR (buffer, i);
1391 s = gst_memory_get_sizes (mem, NULL, NULL);
1394 /* block before offset, or empty block, skip */
1397 /* block after offset */
1399 /* first block, remember index and offset */
1403 /* return remaining blocks */
1410 /* count the amount of found bytes */
1412 if (found >= size) {
1413 /* we have enough bytes */
1414 *length = i - *idx + 1;
1423 * gst_buffer_is_memory_range_writable:
1424 * @buffer: a #GstBuffer.
1426 * @length: a length should not be 0
1428 * Check if @length memory blocks in @buffer starting from @idx are writable.
1430 * @length can be -1 to check all the memory blocks after @idx.
1432 * Note that this function does not check if @buffer is writable, use
1433 * gst_buffer_is_writable() to check that if needed.
1435 * Returns: %TRUE if the memory range is writable
1440 gst_buffer_is_memory_range_writable (GstBuffer * buffer, guint idx, gint length)
1444 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1446 GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
1448 len = GST_BUFFER_MEM_LEN (buffer);
1449 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1450 (length == -1 && idx < len) || (length > 0 && length + idx <= len),
1458 for (i = 0; i < len; i++) {
1459 if (!gst_memory_is_writable (GST_BUFFER_MEM_PTR (buffer, i + idx)))
1466 * gst_buffer_is_all_memory_writable:
1467 * @buffer: a #GstBuffer.
1469 * Check if all memory blocks in @buffer are writable.
1471 * Note that this function does not check if @buffer is writable, use
1472 * gst_buffer_is_writable() to check that if needed.
1474 * Returns: %TRUE if all memory blocks in @buffer are writable
1479 gst_buffer_is_all_memory_writable (GstBuffer * buffer)
1481 return gst_buffer_is_memory_range_writable (buffer, 0, -1);
1485 * gst_buffer_get_sizes:
1486 * @buffer: a #GstBuffer.
1487 * @offset: (out) (allow-none): a pointer to the offset
1488 * @maxsize: (out) (allow-none): a pointer to the maxsize
1490 * Get the total size of the memory blocks in @b.
1492 * When not %NULL, @offset will contain the offset of the data in the
1493 * first memory block in @buffer and @maxsize will contain the sum of
1494 * the size and @offset and the amount of extra padding on the last
1495 * memory block. @offset and @maxsize can be used to resize the
1496 * buffer memory blocks with gst_buffer_resize().
1498 * Returns: total size of the memory blocks in @buffer.
1501 gst_buffer_get_sizes (GstBuffer * buffer, gsize * offset, gsize * maxsize)
1503 return gst_buffer_get_sizes_range (buffer, 0, -1, offset, maxsize);
1507 * gst_buffer_get_size:
1508 * @buffer: a #GstBuffer.
1510 * Get the total size of the memory blocks in @buffer.
1512 * Returns: total size of the memory blocks in @buffer.
1515 gst_buffer_get_size (GstBuffer * buffer)
1517 return gst_buffer_get_sizes_range (buffer, 0, -1, NULL, NULL);
1521 * gst_buffer_get_sizes_range:
1522 * @buffer: a #GstBuffer.
1525 * @offset: (out) (allow-none): a pointer to the offset
1526 * @maxsize: (out) (allow-none): a pointer to the maxsize
1528 * Get the total size of @length memory blocks stating from @idx in @buffer.
1530 * When not %NULL, @offset will contain the offset of the data in the
1531 * memory block in @buffer at @idx and @maxsize will contain the sum of the size
1532 * and @offset and the amount of extra padding on the memory block at @idx +
1534 * @offset and @maxsize can be used to resize the buffer memory blocks with
1535 * gst_buffer_resize_range().
1537 * Returns: total size of @length memory blocks starting at @idx in @buffer.
1540 gst_buffer_get_sizes_range (GstBuffer * buffer, guint idx, gint length,
1541 gsize * offset, gsize * maxsize)
1547 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1548 len = GST_BUFFER_MEM_LEN (buffer);
1549 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1550 (length == -1 && idx < len) || (length + idx <= len), 0);
1555 if (G_LIKELY (length == 1)) {
1557 mem = GST_BUFFER_MEM_PTR (buffer, idx);
1558 size = gst_memory_get_sizes (mem, offset, maxsize);
1564 size = offs = extra = 0;
1565 for (i = idx; i < end; i++) {
1568 mem = GST_BUFFER_MEM_PTR (buffer, i);
1569 s = gst_memory_get_sizes (mem, &o, &ms);
1573 /* first size, take accumulated data before as the offset */
1577 /* save the amount of data after this block */
1578 extra = ms - (o + s);
1580 /* empty block, add as extra */
1587 *maxsize = offs + size + extra;
1593 * gst_buffer_resize:
1594 * @buffer: a #GstBuffer.
1595 * @offset: the offset adjustment
1596 * @size: the new size or -1 to just adjust the offset
1598 * Set the offset and total size of the memory blocks in @buffer.
1601 gst_buffer_resize (GstBuffer * buffer, gssize offset, gssize size)
1603 gst_buffer_resize_range (buffer, 0, -1, offset, size);
1607 * gst_buffer_set_size:
1608 * @buffer: a #GstBuffer.
1609 * @size: the new size
1611 * Set the total size of the memory blocks in @buffer.
1614 gst_buffer_set_size (GstBuffer * buffer, gssize size)
1616 gst_buffer_resize_range (buffer, 0, -1, 0, size);
1620 * gst_buffer_resize_range:
1621 * @buffer: a #GstBuffer.
1624 * @offset: the offset adjustment
1625 * @size: the new size or -1 to just adjust the offset
1627 * Set the total size of the @length memory blocks starting at @idx in
1630 * Returns: %TRUE if resizing succeeded, %FALSE otherwise.
1633 gst_buffer_resize_range (GstBuffer * buffer, guint idx, gint length,
1634 gssize offset, gssize size)
1637 gsize bsize, bufsize, bufoffs, bufmax;
1639 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
1640 g_return_val_if_fail (size >= -1, FALSE);
1642 len = GST_BUFFER_MEM_LEN (buffer);
1643 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1644 (length == -1 && idx < len) || (length + idx <= len), FALSE);
1649 bufsize = gst_buffer_get_sizes_range (buffer, idx, length, &bufoffs, &bufmax);
1651 GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSSIZE_FORMAT
1652 " size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%"
1653 G_GSIZE_FORMAT, buffer, offset, size, bufsize, bufoffs, bufmax);
1655 /* we can't go back further than the current offset or past the end of the
1657 g_return_val_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
1658 && bufoffs + offset <= bufmax), FALSE);
1660 g_return_val_if_fail (bufsize >= offset, FALSE);
1661 size = bufsize - offset;
1663 g_return_val_if_fail (bufmax >= bufoffs + offset + size, FALSE);
1666 if (offset == 0 && size == bufsize)
1671 for (i = idx; i < end; i++) {
1675 mem = GST_BUFFER_MEM_PTR (buffer, i);
1676 bsize = gst_memory_get_sizes (mem, NULL, NULL);
1679 /* last buffer always gets resized to the remaining size */
1682 /* shrink buffers before the offset */
1683 else if ((gssize) bsize <= offset) {
1685 noffs = offset - bsize;
1688 /* clip other buffers */
1690 left = MIN (bsize - offset, size);
1692 if (offset != 0 || left != bsize) {
1693 if (gst_memory_is_writable (mem)) {
1694 gst_memory_resize (mem, offset, left);
1696 GstMemory *newmem = NULL;
1698 if (!GST_MEMORY_IS_NO_SHARE (mem))
1699 newmem = gst_memory_share (mem, offset, left);
1702 newmem = gst_memory_copy (mem, offset, left);
1707 gst_mini_object_add_parent (GST_MINI_OBJECT_CAST (newmem),
1708 GST_MINI_OBJECT_CAST (buffer));
1709 gst_memory_lock (newmem, GST_LOCK_FLAG_EXCLUSIVE);
1710 GST_BUFFER_MEM_PTR (buffer, i) = newmem;
1711 gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
1712 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (mem),
1713 GST_MINI_OBJECT_CAST (buffer));
1714 gst_memory_unref (mem);
1716 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY);
1729 * @buffer: a #GstBuffer.
1730 * @info: (out): info about the mapping
1731 * @flags: flags for the mapping
1733 * This function fills @info with the #GstMapInfo of all merged memory
1734 * blocks in @buffer.
1736 * @flags describe the desired access of the memory. When @flags is
1737 * #GST_MAP_WRITE, @buffer should be writable (as returned from
1738 * gst_buffer_is_writable()).
1740 * When @buffer is writable but the memory isn't, a writable copy will
1741 * automatically be created and returned. The readonly copy of the
1742 * buffer memory will then also be replaced with this writable copy.
1744 * The memory in @info should be unmapped with gst_buffer_unmap() after
1747 * Returns: %TRUE if the map succeeded and @info contains valid data.
1750 gst_buffer_map (GstBuffer * buffer, GstMapInfo * info, GstMapFlags flags)
1752 return gst_buffer_map_range (buffer, 0, -1, info, flags);
1756 * gst_buffer_map_range:
1757 * @buffer: a #GstBuffer.
1760 * @info: (out): info about the mapping
1761 * @flags: flags for the mapping
1763 * This function fills @info with the #GstMapInfo of @length merged memory blocks
1764 * starting at @idx in @buffer. When @length is -1, all memory blocks starting
1765 * from @idx are merged and mapped.
1767 * @flags describe the desired access of the memory. When @flags is
1768 * #GST_MAP_WRITE, @buffer should be writable (as returned from
1769 * gst_buffer_is_writable()).
1771 * When @buffer is writable but the memory isn't, a writable copy will
1772 * automatically be created and returned. The readonly copy of the buffer memory
1773 * will then also be replaced with this writable copy.
1775 * The memory in @info should be unmapped with gst_buffer_unmap() after usage.
1777 * Returns: %TRUE if the map succeeded and @info contains valid
1781 gst_buffer_map_range (GstBuffer * buffer, guint idx, gint length,
1782 GstMapInfo * info, GstMapFlags flags)
1784 GstMemory *mem, *nmem;
1785 gboolean write, writable;
1788 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1789 g_return_val_if_fail (info != NULL, FALSE);
1790 len = GST_BUFFER_MEM_LEN (buffer);
1791 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1792 (length == -1 && idx < len) || (length > 0
1793 && length + idx <= len), FALSE);
1795 GST_CAT_LOG (GST_CAT_BUFFER, "buffer %p, idx %u, length %d, flags %04x",
1796 buffer, idx, length, flags);
1798 write = (flags & GST_MAP_WRITE) != 0;
1799 writable = gst_buffer_is_writable (buffer);
1801 /* check if we can write when asked for write access */
1802 if (G_UNLIKELY (write && !writable))
1808 mem = _get_merged_memory (buffer, idx, length);
1809 if (G_UNLIKELY (mem == NULL))
1812 /* now try to map */
1813 nmem = gst_memory_make_mapped (mem, info, flags);
1814 if (G_UNLIKELY (nmem == NULL))
1817 /* if we merged or when the map returned a different memory, we try to replace
1818 * the memory in the buffer */
1819 if (G_UNLIKELY (length > 1 || nmem != mem)) {
1820 /* if the buffer is writable, replace the memory */
1822 _replace_memory (buffer, len, idx, length, gst_memory_ref (nmem));
1825 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
1826 "temporary mapping for memory %p in buffer %p", nmem, buffer);
1835 GST_WARNING_OBJECT (buffer, "write map requested on non-writable buffer");
1836 g_critical ("write map requested on non-writable buffer");
1837 memset (info, 0, sizeof (GstMapInfo));
1842 /* empty buffer, we need to return NULL */
1843 GST_DEBUG_OBJECT (buffer, "can't get buffer memory");
1844 memset (info, 0, sizeof (GstMapInfo));
1849 GST_DEBUG_OBJECT (buffer, "cannot map memory");
1850 memset (info, 0, sizeof (GstMapInfo));
1857 * @buffer: a #GstBuffer.
1858 * @info: a #GstMapInfo
1860 * Release the memory previously mapped with gst_buffer_map().
1863 gst_buffer_unmap (GstBuffer * buffer, GstMapInfo * info)
1865 g_return_if_fail (GST_IS_BUFFER (buffer));
1866 g_return_if_fail (info != NULL);
1868 /* we need to check for NULL, it is possible that we tried to map a buffer
1869 * without memory and we should be able to unmap that fine */
1870 if (G_LIKELY (info->memory)) {
1871 gst_memory_unmap (info->memory, info);
1872 gst_memory_unref (info->memory);
1878 * @buffer: a #GstBuffer.
1879 * @offset: the offset to fill
1880 * @src: (array length=size) (element-type guint8): the source address
1881 * @size: the size to fill
1883 * Copy @size bytes from @src to @buffer at @offset.
1885 * Returns: The amount of bytes copied. This value can be lower than @size
1886 * when @buffer did not contain enough data.
1889 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
1893 const guint8 *ptr = src;
1895 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1896 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1897 g_return_val_if_fail (src != NULL || size == 0, 0);
1899 GST_CAT_LOG (GST_CAT_BUFFER,
1900 "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
1903 len = GST_BUFFER_MEM_LEN (buffer);
1906 for (i = 0; i < len && left > 0; i++) {
1911 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1912 if (info.size > offset) {
1913 /* we have enough */
1914 tocopy = MIN (info.size - offset, left);
1915 memcpy ((guint8 *) info.data + offset, ptr, tocopy);
1920 /* offset past buffer, skip */
1921 offset -= info.size;
1923 gst_memory_unmap (mem, &info);
1929 * gst_buffer_extract:
1930 * @buffer: a #GstBuffer.
1931 * @offset: the offset to extract
1932 * @dest: (out caller-allocates) (array length=size) (element-type guint8):
1933 * the destination address
1934 * @size: the size to extract
1936 * Copy @size bytes starting from @offset in @buffer to @dest.
1938 * Returns: The amount of bytes extracted. This value can be lower than @size
1939 * when @buffer did not contain enough data.
1942 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
1947 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1948 g_return_val_if_fail (dest != NULL, 0);
1950 GST_CAT_LOG (GST_CAT_BUFFER,
1951 "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
1954 len = GST_BUFFER_MEM_LEN (buffer);
1957 for (i = 0; i < len && left > 0; i++) {
1962 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1963 if (info.size > offset) {
1964 /* we have enough */
1965 tocopy = MIN (info.size - offset, left);
1966 memcpy (ptr, (guint8 *) info.data + offset, tocopy);
1971 /* offset past buffer, skip */
1972 offset -= info.size;
1974 gst_memory_unmap (mem, &info);
1980 * gst_buffer_memcmp:
1981 * @buffer: a #GstBuffer.
1982 * @offset: the offset in @buffer
1983 * @mem: (array length=size) (element-type guint8): the memory to compare
1984 * @size: the size to compare
1986 * Compare @size bytes starting from @offset in @buffer with the memory in @mem.
1988 * Returns: 0 if the memory is equal.
1991 gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem,
1995 const guint8 *ptr = mem;
1998 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1999 g_return_val_if_fail (mem != NULL, 0);
2001 GST_CAT_LOG (GST_CAT_BUFFER,
2002 "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
2005 if (G_UNLIKELY (gst_buffer_get_size (buffer) < offset + size))
2008 len = GST_BUFFER_MEM_LEN (buffer);
2010 for (i = 0; i < len && size > 0 && res == 0; i++) {
2015 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
2016 if (info.size > offset) {
2017 /* we have enough */
2018 tocmp = MIN (info.size - offset, size);
2019 res = memcmp (ptr, (guint8 *) info.data + offset, tocmp);
2024 /* offset past buffer, skip */
2025 offset -= info.size;
2027 gst_memory_unmap (mem, &info);
2033 * gst_buffer_memset:
2034 * @buffer: a #GstBuffer.
2035 * @offset: the offset in @buffer
2036 * @val: the value to set
2037 * @size: the size to set
2039 * Fill @buf with @size bytes with @val starting from @offset.
2041 * Returns: The amount of bytes filled. This value can be lower than @size
2042 * when @buffer did not contain enough data.
2045 gst_buffer_memset (GstBuffer * buffer, gsize offset, guint8 val, gsize size)
2049 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
2050 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
2052 GST_CAT_LOG (GST_CAT_BUFFER,
2053 "buffer %p, offset %" G_GSIZE_FORMAT ", val %02x, size %" G_GSIZE_FORMAT,
2054 buffer, offset, val, size);
2056 len = GST_BUFFER_MEM_LEN (buffer);
2059 for (i = 0; i < len && left > 0; i++) {
2064 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
2065 if (info.size > offset) {
2066 /* we have enough */
2067 toset = MIN (info.size - offset, left);
2068 memset ((guint8 *) info.data + offset, val, toset);
2072 /* offset past buffer, skip */
2073 offset -= info.size;
2075 gst_memory_unmap (mem, &info);
2081 * gst_buffer_copy_region:
2082 * @parent: a #GstBuffer.
2083 * @flags: the #GstBufferCopyFlags
2084 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
2086 * @size: the size of the new #GstBuffer sub-buffer, in bytes. If -1, all
2089 * Creates a sub-buffer from @parent at @offset and @size.
2090 * This sub-buffer uses the actual memory space of the parent buffer.
2091 * This function will copy the offset and timestamp fields when the
2092 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
2093 * #GST_BUFFER_OFFSET_NONE.
2094 * If @offset equals 0 and @size equals the total size of @buffer, the
2095 * duration and offset end fields are also copied. If not they will be set
2096 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
2100 * Returns: (transfer full): the new #GstBuffer or %NULL if the arguments were
2104 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
2105 gsize offset, gsize size)
2109 g_return_val_if_fail (buffer != NULL, NULL);
2111 /* create the new buffer */
2112 copy = gst_buffer_new ();
2114 GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
2115 "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
2117 if (!gst_buffer_copy_into (copy, buffer, flags, offset, size))
2118 gst_buffer_replace (©, NULL);
2124 * gst_buffer_append:
2125 * @buf1: (transfer full): the first source #GstBuffer to append.
2126 * @buf2: (transfer full): the second source #GstBuffer to append.
2128 * Append all the memory from @buf2 to @buf1. The result buffer will contain a
2129 * concatenation of the memory of @buf1 and @buf2.
2131 * Returns: (transfer full): the new #GstBuffer that contains the memory
2132 * of the two source buffers.
2135 gst_buffer_append (GstBuffer * buf1, GstBuffer * buf2)
2137 return gst_buffer_append_region (buf1, buf2, 0, -1);
2141 * gst_buffer_append_region:
2142 * @buf1: (transfer full): the first source #GstBuffer to append.
2143 * @buf2: (transfer full): the second source #GstBuffer to append.
2144 * @offset: the offset in @buf2
2145 * @size: the size or -1 of @buf2
2147 * Append @size bytes at @offset from @buf2 to @buf1. The result buffer will
2148 * contain a concatenation of the memory of @buf1 and the requested region of
2151 * Returns: (transfer full): the new #GstBuffer that contains the memory
2152 * of the two source buffers.
2155 gst_buffer_append_region (GstBuffer * buf1, GstBuffer * buf2, gssize offset,
2160 g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
2161 g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
2163 buf1 = gst_buffer_make_writable (buf1);
2164 buf2 = gst_buffer_make_writable (buf2);
2166 gst_buffer_resize (buf2, offset, size);
2168 len = GST_BUFFER_MEM_LEN (buf2);
2169 for (i = 0; i < len; i++) {
2172 mem = GST_BUFFER_MEM_PTR (buf2, i);
2173 gst_mini_object_remove_parent (GST_MINI_OBJECT_CAST (mem),
2174 GST_MINI_OBJECT_CAST (buf2));
2175 GST_BUFFER_MEM_PTR (buf2, i) = NULL;
2176 _memory_add (buf1, -1, mem);
2179 GST_BUFFER_MEM_LEN (buf2) = 0;
2180 GST_BUFFER_FLAG_SET (buf2, GST_BUFFER_FLAG_TAG_MEMORY);
2181 gst_buffer_unref (buf2);
2187 * gst_buffer_get_meta:
2188 * @buffer: a #GstBuffer
2189 * @api: the #GType of an API
2191 * Get the metadata for @api on buffer. When there is no such metadata, %NULL is
2192 * returned. If multiple metadata with the given @api are attached to this
2193 * buffer only the first one is returned. To handle multiple metadata with a
2194 * given API use gst_buffer_iterate_meta() or gst_buffer_foreach_meta() instead
2195 * and check the meta->info.api member for the API type.
2197 * Returns: (transfer none) (nullable): the metadata for @api on
2201 gst_buffer_get_meta (GstBuffer * buffer, GType api)
2204 GstMeta *result = NULL;
2206 g_return_val_if_fail (buffer != NULL, NULL);
2207 g_return_val_if_fail (api != 0, NULL);
2209 /* find GstMeta of the requested API */
2210 for (item = GST_BUFFER_META (buffer); item; item = item->next) {
2211 GstMeta *meta = &item->meta;
2212 if (meta->info->api == api) {
2221 * gst_buffer_get_n_meta:
2222 * @buffer: a #GstBuffer
2223 * @api_type: the #GType of an API
2225 * Returns: number of metas of type @api_type on @buffer.
2230 gst_buffer_get_n_meta (GstBuffer * buffer, GType api_type)
2232 gpointer state = NULL;
2236 while ((meta = gst_buffer_iterate_meta_filtered (buffer, &state, api_type)))
2243 * gst_buffer_add_meta:
2244 * @buffer: a #GstBuffer
2245 * @info: a #GstMetaInfo
2246 * @params: params for @info
2248 * Add metadata for @info to @buffer using the parameters in @params.
2250 * Returns: (transfer none) (nullable): the metadata for the api in @info on @buffer.
2253 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
2257 GstMeta *result = NULL;
2260 g_return_val_if_fail (buffer != NULL, NULL);
2261 g_return_val_if_fail (info != NULL, NULL);
2262 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
2264 /* create a new slice */
2265 size = ITEM_SIZE (info);
2266 /* We warn in gst_meta_register() about metas without
2267 * init function but let's play safe here and prevent
2268 * uninitialized memory
2270 if (!info->init_func)
2271 item = g_slice_alloc0 (size);
2273 item = g_slice_alloc (size);
2274 result = &item->meta;
2275 result->info = info;
2276 result->flags = GST_META_FLAG_NONE;
2277 GST_CAT_DEBUG (GST_CAT_BUFFER,
2278 "alloc metadata %p (%s) of size %" G_GSIZE_FORMAT, result,
2279 g_type_name (info->type), info->size);
2281 /* call the init_func when needed */
2282 if (info->init_func)
2283 if (!info->init_func (result, params, buffer))
2286 item->seq_num = gst_atomic_int64_inc (&meta_seq);
2288 /* and add to the list of metadata */
2289 item->next = GST_BUFFER_META (buffer);
2290 GST_BUFFER_META (buffer) = item;
2296 g_slice_free1 (size, item);
2302 * gst_buffer_remove_meta:
2303 * @buffer: a #GstBuffer
2306 * Remove the metadata for @meta on @buffer.
2308 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
2309 * metadata was on @buffer.
2312 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
2314 GstMetaItem *walk, *prev;
2316 g_return_val_if_fail (buffer != NULL, FALSE);
2317 g_return_val_if_fail (meta != NULL, FALSE);
2318 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
2319 g_return_val_if_fail (!GST_META_FLAG_IS_SET (meta, GST_META_FLAG_LOCKED),
2322 /* find the metadata and delete */
2323 prev = GST_BUFFER_META (buffer);
2324 for (walk = prev; walk; walk = walk->next) {
2325 GstMeta *m = &walk->meta;
2327 const GstMetaInfo *info = meta->info;
2329 /* remove from list */
2330 if (GST_BUFFER_META (buffer) == walk)
2331 GST_BUFFER_META (buffer) = walk->next;
2333 prev->next = walk->next;
2334 /* call free_func if any */
2335 if (info->free_func)
2336 info->free_func (m, buffer);
2338 /* and free the slice */
2339 g_slice_free1 (ITEM_SIZE (info), walk);
2344 return walk != NULL;
2348 * gst_buffer_iterate_meta: (skip)
2349 * @buffer: a #GstBuffer
2350 * @state: (out caller-allocates): an opaque state pointer
2352 * Retrieve the next #GstMeta after @current. If @state points
2353 * to %NULL, the first metadata is returned.
2355 * @state will be updated with an opaque state pointer
2357 * Returns: (transfer none) (nullable): The next #GstMeta or %NULL
2358 * when there are no more items.
2361 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
2365 g_return_val_if_fail (buffer != NULL, NULL);
2366 g_return_val_if_fail (state != NULL, NULL);
2368 meta = (GstMetaItem **) state;
2370 /* state NULL, move to first item */
2371 *meta = GST_BUFFER_META (buffer);
2373 /* state !NULL, move to next item in list */
2374 *meta = (*meta)->next;
2377 return &(*meta)->meta;
2383 * gst_buffer_iterate_meta_filtered: (skip)
2384 * @buffer: a #GstBuffer
2385 * @state: (out caller-allocates): an opaque state pointer
2386 * @meta_api_type: only return #GstMeta of this type
2388 * Retrieve the next #GstMeta of type @meta_api_type after the current one
2389 * according to @state. If @state points to %NULL, the first metadata of
2390 * type @meta_api_type is returned.
2392 * @state will be updated with an opaque state pointer
2394 * Returns: (transfer none) (nullable): The next #GstMeta of type
2395 * @meta_api_type or %NULL when there are no more items.
2400 gst_buffer_iterate_meta_filtered (GstBuffer * buffer, gpointer * state,
2401 GType meta_api_type)
2405 g_return_val_if_fail (buffer != NULL, NULL);
2406 g_return_val_if_fail (state != NULL, NULL);
2408 meta = (GstMetaItem **) state;
2410 /* state NULL, move to first item */
2411 *meta = GST_BUFFER_META (buffer);
2413 /* state !NULL, move to next item in list */
2414 *meta = (*meta)->next;
2416 while (*meta != NULL && (*meta)->meta.info->api != meta_api_type)
2417 *meta = (*meta)->next;
2420 return &(*meta)->meta;
2426 * gst_buffer_foreach_meta:
2427 * @buffer: a #GstBuffer
2428 * @func: (scope call): a #GstBufferForeachMetaFunc to call
2429 * @user_data: (closure): user data passed to @func
2431 * Call @func with @user_data for each meta in @buffer.
2433 * @func can modify the passed meta pointer or its contents. The return value
2434 * of @func define if this function returns or if the remaining metadata items
2435 * in the buffer should be skipped.
2437 * Returns: %FALSE when @func returned %FALSE for one of the metadata.
2440 gst_buffer_foreach_meta (GstBuffer * buffer, GstBufferForeachMetaFunc func,
2443 GstMetaItem *walk, *prev, *next;
2444 gboolean res = TRUE;
2446 g_return_val_if_fail (buffer != NULL, FALSE);
2447 g_return_val_if_fail (func != NULL, FALSE);
2449 /* find the metadata and delete */
2450 prev = GST_BUFFER_META (buffer);
2451 for (walk = prev; walk; walk = next) {
2454 m = new = &walk->meta;
2457 res = func (buffer, &new, user_data);
2460 const GstMetaInfo *info = m->info;
2462 GST_CAT_DEBUG (GST_CAT_BUFFER, "remove metadata %p (%s)", m,
2463 g_type_name (info->type));
2465 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
2466 g_return_val_if_fail (!GST_META_FLAG_IS_SET (m, GST_META_FLAG_LOCKED),
2469 /* remove from list */
2470 if (GST_BUFFER_META (buffer) == walk)
2471 prev = GST_BUFFER_META (buffer) = next;
2475 /* call free_func if any */
2476 if (info->free_func)
2477 info->free_func (m, buffer);
2479 /* and free the slice */
2480 g_slice_free1 (ITEM_SIZE (info), walk);
2491 * gst_buffer_extract_dup:
2492 * @buffer: a #GstBuffer
2493 * @offset: the offset to extract
2494 * @size: the size to extract
2495 * @dest: (array length=dest_size) (element-type guint8) (out): A pointer where
2496 * the destination array will be written. Might be %NULL if the size is 0.
2497 * @dest_size: (out): A location where the size of @dest can be written
2499 * Extracts a copy of at most @size bytes the data at @offset into
2500 * newly-allocated memory. @dest must be freed using g_free() when done.
2506 gst_buffer_extract_dup (GstBuffer * buffer, gsize offset, gsize size,
2507 gpointer * dest, gsize * dest_size)
2509 gsize real_size, alloc_size;
2511 real_size = gst_buffer_get_size (buffer);
2513 alloc_size = MIN (real_size - offset, size);
2514 if (alloc_size == 0) {
2518 *dest = g_malloc (alloc_size);
2519 *dest_size = gst_buffer_extract (buffer, offset, *dest, size);
2523 GST_DEBUG_CATEGORY_STATIC (gst_parent_buffer_meta_debug);
2526 * gst_buffer_add_parent_buffer_meta:
2527 * @buffer: (transfer none): a #GstBuffer
2528 * @ref: (transfer none): a #GstBuffer to ref
2530 * Add a #GstParentBufferMeta to @buffer that holds a reference on
2531 * @ref until the buffer is freed.
2533 * Returns: (transfer none) (nullable): The #GstParentBufferMeta that was added to the buffer
2537 GstParentBufferMeta *
2538 gst_buffer_add_parent_buffer_meta (GstBuffer * buffer, GstBuffer * ref)
2540 GstParentBufferMeta *meta;
2542 g_return_val_if_fail (GST_IS_BUFFER (ref), NULL);
2545 (GstParentBufferMeta *) gst_buffer_add_meta (buffer,
2546 GST_PARENT_BUFFER_META_INFO, NULL);
2551 meta->buffer = gst_buffer_ref (ref);
2557 _gst_parent_buffer_meta_transform (GstBuffer * dest, GstMeta * meta,
2558 GstBuffer * buffer, GQuark type, gpointer data)
2560 GstParentBufferMeta *dmeta, *smeta;
2562 smeta = (GstParentBufferMeta *) meta;
2564 if (GST_META_TRANSFORM_IS_COPY (type)) {
2565 /* copy over the reference to the parent buffer.
2566 * Usually, this meta means we need to keep the parent buffer
2567 * alive because one of the child memories is in use, which
2568 * might not be the case if memory is deep copied or sub-regioned,
2569 * but we can't tell, so keep the meta */
2570 dmeta = gst_buffer_add_parent_buffer_meta (dest, smeta->buffer);
2574 GST_CAT_DEBUG (gst_parent_buffer_meta_debug,
2575 "copy buffer reference metadata");
2577 /* return FALSE, if transform type is not supported */
2584 _gst_parent_buffer_meta_free (GstParentBufferMeta * parent_meta,
2587 GST_CAT_DEBUG (gst_parent_buffer_meta_debug,
2588 "Dropping reference on buffer %p", parent_meta->buffer);
2589 gst_buffer_unref (parent_meta->buffer);
2593 _gst_parent_buffer_meta_init (GstParentBufferMeta * parent_meta,
2594 gpointer params, GstBuffer * buffer)
2596 static volatile gsize _init;
2598 if (g_once_init_enter (&_init)) {
2599 GST_DEBUG_CATEGORY_INIT (gst_parent_buffer_meta_debug, "parentbuffermeta",
2600 0, "parentbuffermeta");
2601 g_once_init_leave (&_init, 1);
2604 parent_meta->buffer = NULL;
2610 gst_parent_buffer_meta_api_get_type (void)
2612 static volatile GType type = 0;
2613 static const gchar *tags[] = { NULL };
2615 if (g_once_init_enter (&type)) {
2616 GType _type = gst_meta_api_type_register ("GstParentBufferMetaAPI", tags);
2617 g_once_init_leave (&type, _type);
2624 * gst_parent_buffer_meta_get_info:
2626 * Get the global #GstMetaInfo describing the #GstParentBufferMeta meta.
2628 * Returns: (transfer none): The #GstMetaInfo
2633 gst_parent_buffer_meta_get_info (void)
2635 static const GstMetaInfo *meta_info = NULL;
2637 if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
2638 const GstMetaInfo *meta =
2639 gst_meta_register (gst_parent_buffer_meta_api_get_type (),
2640 "GstParentBufferMeta",
2641 sizeof (GstParentBufferMeta),
2642 (GstMetaInitFunction) _gst_parent_buffer_meta_init,
2643 (GstMetaFreeFunction) _gst_parent_buffer_meta_free,
2644 _gst_parent_buffer_meta_transform);
2645 g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) meta);
2651 GST_DEBUG_CATEGORY_STATIC (gst_reference_timestamp_meta_debug);
2654 * gst_buffer_add_reference_timestamp_meta:
2655 * @buffer: (transfer none): a #GstBuffer
2656 * @reference: (transfer none): identifier for the timestamp reference.
2657 * @timestamp: timestamp
2658 * @duration: duration, or %GST_CLOCK_TIME_NONE
2660 * Add a #GstReferenceTimestampMeta to @buffer that holds a @timestamp and
2661 * optionally @duration based on a specific timestamp @reference. See the
2662 * documentation of #GstReferenceTimestampMeta for details.
2664 * Returns: (transfer none) (nullable): The #GstReferenceTimestampMeta that was added to the buffer
2668 GstReferenceTimestampMeta *
2669 gst_buffer_add_reference_timestamp_meta (GstBuffer * buffer,
2670 GstCaps * reference, GstClockTime timestamp, GstClockTime duration)
2672 GstReferenceTimestampMeta *meta;
2674 g_return_val_if_fail (GST_IS_CAPS (reference), NULL);
2675 g_return_val_if_fail (timestamp != GST_CLOCK_TIME_NONE, NULL);
2678 (GstReferenceTimestampMeta *) gst_buffer_add_meta (buffer,
2679 GST_REFERENCE_TIMESTAMP_META_INFO, NULL);
2684 meta->reference = gst_caps_ref (reference);
2685 meta->timestamp = timestamp;
2686 meta->duration = duration;
2692 * gst_buffer_get_reference_timestamp_meta:
2693 * @buffer: a #GstBuffer
2694 * @reference: (allow-none): a reference #GstCaps
2696 * Find the first #GstReferenceTimestampMeta on @buffer that conforms to
2697 * @reference. Conformance is tested by checking if the meta's reference is a
2698 * subset of @reference.
2700 * Buffers can contain multiple #GstReferenceTimestampMeta metadata items.
2702 * Returns: (transfer none) (nullable): the #GstReferenceTimestampMeta or %NULL when there
2703 * is no such metadata on @buffer.
2707 GstReferenceTimestampMeta *
2708 gst_buffer_get_reference_timestamp_meta (GstBuffer * buffer,
2709 GstCaps * reference)
2711 gpointer state = NULL;
2713 const GstMetaInfo *info = GST_REFERENCE_TIMESTAMP_META_INFO;
2715 while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
2716 if (meta->info->api == info->api) {
2717 GstReferenceTimestampMeta *rmeta = (GstReferenceTimestampMeta *) meta;
2721 if (gst_caps_is_subset (rmeta->reference, reference))
2729 _gst_reference_timestamp_meta_transform (GstBuffer * dest, GstMeta * meta,
2730 GstBuffer * buffer, GQuark type, gpointer data)
2732 GstReferenceTimestampMeta *dmeta, *smeta;
2734 /* we copy over the reference timestamp meta, independent of transformation
2735 * that happens. If it applied to the original buffer, it still applies to
2736 * the new buffer as it refers to the time when the media was captured */
2737 smeta = (GstReferenceTimestampMeta *) meta;
2739 gst_buffer_add_reference_timestamp_meta (dest, smeta->reference,
2740 smeta->timestamp, smeta->duration);
2744 GST_CAT_DEBUG (gst_reference_timestamp_meta_debug,
2745 "copy reference timestamp metadata from buffer %p to %p", buffer, dest);
2751 _gst_reference_timestamp_meta_free (GstReferenceTimestampMeta * meta,
2754 if (meta->reference)
2755 gst_caps_unref (meta->reference);
2759 _gst_reference_timestamp_meta_init (GstReferenceTimestampMeta * meta,
2760 gpointer params, GstBuffer * buffer)
2762 static volatile gsize _init;
2764 if (g_once_init_enter (&_init)) {
2765 GST_DEBUG_CATEGORY_INIT (gst_reference_timestamp_meta_debug,
2766 "referencetimestampmeta", 0, "referencetimestampmeta");
2767 g_once_init_leave (&_init, 1);
2770 meta->reference = NULL;
2771 meta->timestamp = GST_CLOCK_TIME_NONE;
2772 meta->duration = GST_CLOCK_TIME_NONE;
2778 gst_reference_timestamp_meta_api_get_type (void)
2780 static volatile GType type = 0;
2781 static const gchar *tags[] = { NULL };
2783 if (g_once_init_enter (&type)) {
2785 gst_meta_api_type_register ("GstReferenceTimestampMetaAPI", tags);
2786 g_once_init_leave (&type, _type);
2793 * gst_reference_timestamp_meta_get_info:
2795 * Get the global #GstMetaInfo describing the #GstReferenceTimestampMeta meta.
2797 * Returns: (transfer none): The #GstMetaInfo
2802 gst_reference_timestamp_meta_get_info (void)
2804 static const GstMetaInfo *meta_info = NULL;
2806 if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
2807 const GstMetaInfo *meta =
2808 gst_meta_register (gst_reference_timestamp_meta_api_get_type (),
2809 "GstReferenceTimestampMeta",
2810 sizeof (GstReferenceTimestampMeta),
2811 (GstMetaInitFunction) _gst_reference_timestamp_meta_init,
2812 (GstMetaFreeFunction) _gst_reference_timestamp_meta_free,
2813 _gst_reference_timestamp_meta_transform);
2814 g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) meta);