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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Data-passing buffer type
26 * @see_also: #GstPad, #GstMiniObject, #GstMemory, #GstMeta, #GstBufferPool
28 * Buffers are the basic unit of data transfer in GStreamer. They contain the
29 * timing and offset along with other arbitrary metadata that is associated
30 * with the #GstMemory blocks that the buffer contains.
32 * Buffers are usually created with gst_buffer_new(). After a buffer has been
33 * created one will typically allocate memory for it and add it to the buffer.
34 * The following example creates a buffer that can hold a given video frame
35 * with a given width, height and bits per plane.
37 * <title>Creating a buffer for a video frame</title>
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);
51 * Alternatively, use gst_buffer_new_allocate()
52 * to create a buffer with preallocated data of a given size.
54 * Buffers can contain a list of #GstMemory objects. You can retrieve how many
55 * memory objects with gst_buffer_n_memory() and you can get a pointer
56 * to memory with gst_buffer_peek_memory()
58 * A buffer will usually have timestamps, and a duration, but neither of these
59 * are guaranteed (they may be set to #GST_CLOCK_TIME_NONE). Whenever a
60 * meaningful value can be given for these, they should be set. The timestamps
61 * and duration are measured in nanoseconds (they are #GstClockTime values).
63 * The buffer DTS refers to the timestamp when the buffer should be decoded and
64 * is usually monotonically increasing. The buffer PTS refers to the timestamp when
65 * the buffer content should be presented to the user and is not always
66 * monotonically increasing.
68 * A buffer can also have one or both of a start and an end offset. These are
69 * media-type specific. For video buffers, the start offset will generally be
70 * the frame number. For audio buffers, it will be the number of samples
71 * produced so far. For compressed data, it could be the byte offset in a
72 * source or destination file. Likewise, the end offset will be the offset of
73 * the end of the buffer. These can only be meaningfully interpreted if you
74 * know the media type of the buffer (the preceeding CAPS event). Either or both
75 * can be set to #GST_BUFFER_OFFSET_NONE.
77 * gst_buffer_ref() is used to increase the refcount of a buffer. This must be
78 * done when you want to keep a handle to the buffer after pushing it to the
79 * next element. The buffer refcount determines the writability of the buffer, a
80 * buffer is only writable when the refcount is exactly 1, i.e. when the caller
81 * has the only reference to the buffer.
83 * To efficiently create a smaller buffer out of an existing one, you can
84 * use gst_buffer_copy_region(). This method tries to share the memory objects
85 * between the two buffers.
87 * If a plug-in wants to modify the buffer data or metadata in-place, it should
88 * first obtain a buffer that is safe to modify by using
89 * gst_buffer_make_writable(). This function is optimized so that a copy will
90 * only be made when it is necessary.
92 * Several flags of the buffer can be set and unset with the
93 * GST_BUFFER_FLAG_SET() and GST_BUFFER_FLAG_UNSET() macros. Use
94 * GST_BUFFER_FLAG_IS_SET() to test if a certain #GstBufferFlag is set.
96 * Buffers can be efficiently merged into a larger buffer with
97 * gst_buffer_append(). Copying of memory will only be done when absolutely
100 * Arbitrary extra metadata can be set on a buffer with gst_buffer_add_meta().
101 * Metadata can be retrieved with gst_buffer_get_meta(). See also #GstMeta
103 * An element should either unref the buffer or push it out on a src pad
104 * using gst_pad_push() (see #GstPad).
106 * Buffers are usually freed by unreffing them with gst_buffer_unref(). When
107 * the refcount drops to 0, any memory and metadata pointed to by the buffer is
108 * unreffed as well. Buffers allocated from a #GstBufferPool will be returned to
109 * the pool when the refcount drops to 0.
111 * Last reviewed on 2012-03-28 (0.11.3)
113 #include "gst_private.h"
122 #include "gstbuffer.h"
123 #include "gstbufferpool.h"
125 #include "gstutils.h"
126 #include "gstversion.h"
128 GType _gst_buffer_type = 0;
130 typedef struct _GstMetaItem GstMetaItem;
137 #define ITEM_SIZE(info) ((info)->size + sizeof (GstMetaItem))
139 #define GST_BUFFER_MEM_MAX 16
141 #define GST_BUFFER_SLICE_SIZE(b) (((GstBufferImpl *)(b))->slice_size)
142 #define GST_BUFFER_MEM_LEN(b) (((GstBufferImpl *)(b))->len)
143 #define GST_BUFFER_MEM_ARRAY(b) (((GstBufferImpl *)(b))->mem)
144 #define GST_BUFFER_MEM_PTR(b,i) (((GstBufferImpl *)(b))->mem[i])
145 #define GST_BUFFER_BUFMEM(b) (((GstBufferImpl *)(b))->bufmem)
146 #define GST_BUFFER_META(b) (((GstBufferImpl *)(b))->item)
154 /* the memory blocks */
156 GstMemory *mem[GST_BUFFER_MEM_MAX];
158 /* memory of the buffer when allocated from 1 chunk */
161 /* FIXME, make metadata allocation more efficient by using part of the
168 _is_span (GstMemory ** mem, gsize len, gsize * poffset, GstMemory ** parent)
170 GstMemory *mcur, *mprv;
171 gboolean have_offset = FALSE;
176 for (i = 0; i < len; i++) {
184 /* check if memory is contiguous */
185 if (!gst_memory_is_span (mprv, mcur, &poffs))
192 *parent = mprv->parent;
202 _get_merged_memory (GstBuffer * buffer, guint idx, guint length)
204 GstMemory **mem, *result;
206 GST_LOG ("buffer %p, idx %u, length %u", buffer, idx, length);
208 mem = GST_BUFFER_MEM_ARRAY (buffer);
210 if (G_UNLIKELY (length == 0)) {
212 } else if (G_LIKELY (length == 1)) {
213 result = gst_memory_ref (mem[idx]);
215 GstMemory *parent = NULL;
216 gsize size, poffset = 0;
218 size = gst_buffer_get_size (buffer);
220 if (G_UNLIKELY (_is_span (mem + idx, length, &poffset, &parent))) {
222 if (GST_MEMORY_IS_NO_SHARE (parent)) {
223 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy for merge %p", parent);
224 result = gst_memory_copy (parent, poffset, size);
226 result = gst_memory_share (parent, poffset, size);
229 gsize i, tocopy, left;
230 GstMapInfo sinfo, dinfo;
233 result = gst_allocator_alloc (NULL, size, NULL);
234 gst_memory_map (result, &dinfo, GST_MAP_WRITE);
239 for (i = idx; i < length && left > 0; i++) {
240 gst_memory_map (mem[i], &sinfo, GST_MAP_READ);
241 tocopy = MIN (sinfo.size, left);
242 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
243 "memcpy %" G_GSIZE_FORMAT " bytes for merge %p from memory %p",
244 tocopy, result, mem[i]);
245 memcpy (ptr, (guint8 *) sinfo.data, tocopy);
248 gst_memory_unmap (mem[i], &sinfo);
250 gst_memory_unmap (result, &dinfo);
257 _replace_memory (GstBuffer * buffer, guint len, guint idx, guint length,
264 GST_LOG ("buffer %p replace %u-%" G_GSIZE_FORMAT " with memory %p", buffer,
267 /* unref old memory */
268 for (i = idx; i < end; i++) {
269 gst_memory_unlock (GST_BUFFER_MEM_PTR (buffer, i), GST_LOCK_FLAG_EXCLUSIVE);
270 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
274 /* replace with single memory */
275 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
276 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
282 g_memmove (&GST_BUFFER_MEM_PTR (buffer, idx),
283 &GST_BUFFER_MEM_PTR (buffer, end), (len - end) * sizeof (gpointer));
285 GST_BUFFER_MEM_LEN (buffer) = len - length;
289 _memory_add (GstBuffer * buffer, gint idx, GstMemory * mem, gboolean lock)
291 guint i, len = GST_BUFFER_MEM_LEN (buffer);
293 GST_LOG ("buffer %p, idx %d, mem %p, lock %d", buffer, idx, mem, lock);
295 if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) {
296 /* too many buffer, span them. */
297 /* FIXME, there is room for improvement here: We could only try to merge
298 * 2 buffers to make some room. If we can't efficiently merge 2 buffers we
299 * could try to only merge the two smallest buffers to avoid memcpy, etc. */
300 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "memory array overflow in buffer %p",
302 _replace_memory (buffer, len, 0, len, _get_merged_memory (buffer, 0, len));
303 /* we now have 1 single spanned buffer */
310 for (i = len; i > idx; i--) {
311 /* move buffers to insert, FIXME, we need to insert first and then merge */
312 GST_BUFFER_MEM_PTR (buffer, i) = GST_BUFFER_MEM_PTR (buffer, i - 1);
314 /* and insert the new buffer */
316 gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
317 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
318 GST_BUFFER_MEM_LEN (buffer) = len + 1;
321 GST_DEFINE_MINI_OBJECT_TYPE (GstBuffer, gst_buffer);
324 _priv_gst_buffer_initialize (void)
326 _gst_buffer_type = gst_buffer_get_type ();
330 * gst_buffer_copy_into:
331 * @dest: a destination #GstBuffer
332 * @src: a source #GstBuffer
333 * @flags: flags indicating what metadata fields should be copied.
334 * @offset: offset to copy from
335 * @size: total size to copy. If -1, all data is copied.
337 * Copies the information from @src into @dest.
339 * If @dest already contains memory and @flags contains GST_BUFFER_COPY_MEMORY,
340 * the memory from @src will be appended to @dest.
342 * @flags indicate which fields will be copied.
345 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
346 GstBufferCopyFlags flags, gsize offset, gsize size)
350 gboolean region = FALSE;
352 g_return_if_fail (dest != NULL);
353 g_return_if_fail (src != NULL);
355 /* nothing to copy if the buffers are the same */
356 if (G_UNLIKELY (dest == src))
359 g_return_if_fail (gst_buffer_is_writable (dest));
361 bufsize = gst_buffer_get_size (src);
362 g_return_if_fail (bufsize >= offset);
366 size = bufsize - offset;
369 g_return_if_fail (bufsize >= offset + size);
371 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p, offset %" G_GSIZE_FORMAT
372 "-%" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT, src, dest, offset, size,
375 if (flags & GST_BUFFER_COPY_FLAGS) {
377 GST_MINI_OBJECT_FLAGS (dest) = GST_MINI_OBJECT_FLAGS (src);
380 if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
382 GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src);
383 GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src);
384 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
385 if (size == bufsize) {
386 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
387 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
390 GST_BUFFER_PTS (dest) = GST_CLOCK_TIME_NONE;
391 GST_BUFFER_DTS (dest) = GST_CLOCK_TIME_NONE;
392 GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
393 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
394 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
398 if (flags & GST_BUFFER_COPY_MEMORY) {
400 gsize skip, left, len, i, bsize;
402 len = GST_BUFFER_MEM_LEN (src);
406 /* copy and make regions of the memory */
407 for (i = 0; i < len && left > 0; i++) {
408 mem = GST_BUFFER_MEM_PTR (src, i);
409 bsize = gst_memory_get_sizes (mem, NULL, NULL);
412 /* don't copy buffer */
417 tocopy = MIN (bsize - skip, left);
418 if (GST_MEMORY_IS_NO_SHARE (mem)) {
419 /* no share, always copy then */
420 mem = gst_memory_copy (mem, skip, tocopy);
422 } else if (tocopy < bsize) {
423 /* we need to clip something */
424 mem = gst_memory_share (mem, skip, tocopy);
427 mem = gst_memory_ref (mem);
429 _memory_add (dest, -1, mem, TRUE);
433 if (flags & GST_BUFFER_COPY_MERGE) {
434 len = GST_BUFFER_MEM_LEN (dest);
435 _replace_memory (dest, len, 0, len, _get_merged_memory (dest, 0, len));
439 if (flags & GST_BUFFER_COPY_META) {
440 for (walk = GST_BUFFER_META (src); walk; walk = walk->next) {
441 GstMeta *meta = &walk->meta;
442 const GstMetaInfo *info = meta->info;
444 if (info->transform_func) {
445 GstMetaTransformCopy copy_data;
447 copy_data.region = region;
448 copy_data.offset = offset;
449 copy_data.size = size;
451 info->transform_func (dest, meta, src,
452 _gst_meta_transform_copy, ©_data);
459 _gst_buffer_copy (GstBuffer * buffer)
463 g_return_val_if_fail (buffer != NULL, NULL);
465 /* create a fresh new buffer */
466 copy = gst_buffer_new ();
468 /* we simply copy everything from our parent */
469 gst_buffer_copy_into (copy, buffer, GST_BUFFER_COPY_ALL, 0, -1);
474 /* the default dispose function revives the buffer and returns it to the
475 * pool when there is a pool */
477 _gst_buffer_dispose (GstBuffer * buffer)
481 /* no pool, do free */
482 if ((pool = buffer->pool) == NULL)
485 /* keep the buffer alive */
486 gst_buffer_ref (buffer);
487 /* return the buffer to the pool */
488 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
489 gst_buffer_pool_release_buffer (pool, buffer);
495 _gst_buffer_free (GstBuffer * buffer)
497 GstMetaItem *walk, *next;
501 g_return_if_fail (buffer != NULL);
503 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
506 for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
507 GstMeta *meta = &walk->meta;
508 const GstMetaInfo *info = meta->info;
510 /* call free_func if any */
512 info->free_func (meta, buffer);
515 /* and free the slice */
516 g_slice_free1 (ITEM_SIZE (info), walk);
519 /* get the size, when unreffing the memory, we could also unref the buffer
521 msize = GST_BUFFER_SLICE_SIZE (buffer);
523 /* free our memory */
524 len = GST_BUFFER_MEM_LEN (buffer);
525 for (i = 0; i < len; i++) {
526 gst_memory_unlock (GST_BUFFER_MEM_PTR (buffer, i), GST_LOCK_FLAG_EXCLUSIVE);
527 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
530 /* we set msize to 0 when the buffer is part of the memory block */
532 g_slice_free1 (msize, buffer);
534 gst_memory_unref (GST_BUFFER_BUFMEM (buffer));
538 gst_buffer_init (GstBufferImpl * buffer, gsize size)
540 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), _gst_buffer_type,
541 (GstMiniObjectCopyFunction) _gst_buffer_copy,
542 (GstMiniObjectDisposeFunction) _gst_buffer_dispose,
543 (GstMiniObjectFreeFunction) _gst_buffer_free);
545 GST_BUFFER_SLICE_SIZE (buffer) = size;
547 GST_BUFFER (buffer)->pool = NULL;
548 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
549 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
550 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
551 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
552 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
554 GST_BUFFER_MEM_LEN (buffer) = 0;
555 GST_BUFFER_META (buffer) = NULL;
561 * Creates a newly allocated buffer without any data.
565 * Returns: (transfer full): the new #GstBuffer.
568 gst_buffer_new (void)
570 GstBufferImpl *newbuf;
572 newbuf = g_slice_new (GstBufferImpl);
573 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
575 gst_buffer_init (newbuf, sizeof (GstBufferImpl));
577 return GST_BUFFER_CAST (newbuf);
581 * gst_buffer_new_allocate:
582 * @allocator: (transfer none) (allow-none): the #GstAllocator to use, or NULL to use the
584 * @size: the size in bytes of the new buffer's data.
585 * @params: (transfer none) (allow-none): optional parameters
587 * Tries to create a newly allocated buffer with data of the given size and
588 * extra parameters from @allocator. If the requested amount of memory can't be
589 * allocated, NULL will be returned. The allocated buffer memory is not cleared.
591 * When @allocator is NULL, the default memory allocator will be used.
593 * Note that when @size == 0, the buffer will not have memory associated with it.
597 * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
601 gst_buffer_new_allocate (GstAllocator * allocator, gsize size,
602 GstAllocationParams * params)
613 mem = gst_allocator_alloc (allocator, size, params);
614 if (G_UNLIKELY (mem == NULL))
620 newbuf = gst_buffer_new ();
623 _memory_add (newbuf, -1, mem, TRUE);
625 GST_CAT_LOG (GST_CAT_BUFFER,
626 "new buffer %p of size %" G_GSIZE_FORMAT " from allocator %p", newbuf,
631 asize = sizeof (GstBufferImpl) + size;
632 data = g_slice_alloc (asize);
633 if (G_UNLIKELY (data == NULL))
636 newbuf = GST_BUFFER_CAST (data);
638 gst_buffer_init ((GstBufferImpl *) data, asize);
640 mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL,
642 _memory_add (newbuf, -1, mem, TRUE);
647 /* allocate memory and buffer, it might be interesting to do this but there
648 * are many complications. We need to keep the memory mapped to access the
649 * buffer fields and the memory for the buffer might be just very slow. We
650 * also need to do some more magic to get the alignment right. */
651 asize = sizeof (GstBufferImpl) + size;
652 mem = gst_allocator_alloc (allocator, asize, align);
653 if (G_UNLIKELY (mem == NULL))
656 /* map the data part and init the buffer in it, set the buffer size to 0 so
657 * that a finalize won't free the buffer */
658 data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
659 gst_buffer_init ((GstBufferImpl *) data, 0);
660 gst_memory_unmap (mem);
662 /* strip off the buffer */
663 gst_memory_resize (mem, sizeof (GstBufferImpl), size);
665 newbuf = GST_BUFFER_CAST (data);
666 GST_BUFFER_BUFMEM (newbuf) = mem;
669 _memory_add (newbuf, -1, gst_memory_ref (mem), TRUE);
677 GST_CAT_WARNING (GST_CAT_BUFFER,
678 "failed to allocate %" G_GSIZE_FORMAT " bytes", size);
684 * gst_buffer_new_wrapped_full:
685 * @flags: #GstMemoryFlags
686 * @data: data to wrap
687 * @maxsize: allocated size of @data
688 * @offset: offset in @data
689 * @size: size of valid data
690 * @user_data: user_data
691 * @notify: called with @user_data when the memory is freed
693 * Allocate a new buffer that wraps the given memory. @data must point to
694 * @maxsize of memory, the wrapped buffer will have the region from @offset and
697 * When the buffer is destroyed, @notify will be called with @user_data.
699 * The prefix/padding must be filled with 0 if @flags contains
700 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
702 * Returns: (transfer full): a new #GstBuffer
705 gst_buffer_new_wrapped_full (GstMemoryFlags flags, gpointer data,
706 gsize maxsize, gsize offset, gsize size, gpointer user_data,
707 GDestroyNotify notify)
711 newbuf = gst_buffer_new ();
712 gst_buffer_append_memory (newbuf,
713 gst_memory_new_wrapped (flags, data, maxsize, offset, size,
720 * gst_buffer_new_wrapped:
721 * @data: data to wrap
722 * @size: allocated size of @data
724 * Creates a new buffer that wraps the given @data. The memory will be freed
725 * with g_free and will be marked writable.
729 * Returns: (transfer full): a new #GstBuffer
732 gst_buffer_new_wrapped (gpointer data, gsize size)
734 return gst_buffer_new_wrapped_full (0, data, size, 0, size, data, g_free);
738 * gst_buffer_n_memory:
739 * @buffer: a #GstBuffer.
741 * Get the amount of memory blocks that this buffer has.
743 * Returns: (transfer full): the amount of memory block in this buffer.
746 gst_buffer_n_memory (GstBuffer * buffer)
748 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
750 return GST_BUFFER_MEM_LEN (buffer);
754 * gst_buffer_prepend_memory:
756 * @m: (transfer full): a #GstMemory.
758 * Prepend the memory block @m to @b. This function takes ownership
759 * of @m and thus doesn't increase its refcount.
762 * gst_buffer_append_memory:
764 * @m: (transfer full): a #GstMemory.
766 * Append the memory block @m to @b. This function takes ownership
767 * of @m and thus doesn't increase its refcount.
770 * gst_buffer_insert_memory:
771 * @buffer: a #GstBuffer.
772 * @idx: the index to add the memory at, or -1 to append it to the end
773 * @mem: (transfer full): a #GstMemory.
775 * Insert the memory block @mem to @buffer at @idx. This function takes ownership
776 * of @mem and thus doesn't increase its refcount.
779 gst_buffer_insert_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
781 g_return_if_fail (GST_IS_BUFFER (buffer));
782 g_return_if_fail (gst_buffer_is_writable (buffer));
783 g_return_if_fail (mem != NULL);
784 g_return_if_fail (idx == -1 ||
785 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)));
787 _memory_add (buffer, idx, mem, TRUE);
791 _get_mapped (GstBuffer * buffer, guint idx, GstMapInfo * info,
794 GstMemory *mem, *mapped;
796 mem = gst_memory_ref (GST_BUFFER_MEM_PTR (buffer, idx));
798 mapped = gst_memory_make_mapped (mem, info, flags);
801 /* memory changed, lock new memory */
802 gst_memory_lock (mapped, GST_LOCK_FLAG_EXCLUSIVE);
803 GST_BUFFER_MEM_PTR (buffer, idx) = mapped;
804 /* unlock old memory */
805 gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
807 gst_memory_unref (mem);
813 * gst_buffer_peek_memory:
814 * @buffer: a #GstBuffer.
817 * Get the memory block at @idx in @buffer. The memory block stays valid until
818 * the memory block in @buffer is removed, replaced or merged, typically with
819 * any call that modifies the memory in @buffer.
821 * Since this call does not influence the refcount of the memory,
822 * gst_memory_is_exclusive() can be used to check if @buffer is the sole owner
823 * of the returned memory.
825 * Returns: (transfer none): the #GstMemory at @idx.
828 gst_buffer_peek_memory (GstBuffer * buffer, guint idx)
832 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
833 len = GST_BUFFER_MEM_LEN (buffer);
834 g_return_val_if_fail (idx < len, NULL);
836 return GST_BUFFER_MEM_PTR (buffer, idx);
840 * gst_buffer_get_memory:
844 * Get the memory block at index @i in @buffer.
846 * Returns: (transfer full): a #GstMemory that contains the data of the
847 * memory block at @idx. Use gst_memory_unref () after usage.
850 * gst_buffer_get_all_memory:
853 * Get all the memory block in @buffer. The memory blocks will be merged
854 * into one large #GstMemory.
856 * Returns: (transfer full): a #GstMemory that contains the merged memory.
857 * Use gst_memory_unref () after usage.
860 * gst_buffer_get_memory_range:
861 * @buffer: a #GstBuffer.
865 * Get @length memory blocks in @buffer starting at @idx. The memory blocks will
866 * be merged into one large #GstMemory.
868 * If @length is -1, all memory starting from @idx is merged.
870 * Returns: (transfer full): a #GstMemory that contains the merged data of @length
871 * blocks starting at @idx. Use gst_memory_unref () after usage.
874 gst_buffer_get_memory_range (GstBuffer * buffer, guint idx, gint length)
878 GST_DEBUG ("idx %u, length %d", idx, length);
880 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
881 len = GST_BUFFER_MEM_LEN (buffer);
882 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
883 (length == -1 && idx < len) || (length > 0 && length + idx <= len), NULL);
888 return _get_merged_memory (buffer, idx, length);
892 * gst_buffer_replace_memory:
895 * @m: (transfer full): a #GstMemory
897 * Replaces the memory block at index @i in @b with @m.
900 * gst_buffer_replace_all_memory:
902 * @m: (transfer full): a #GstMemory
904 * Replaces all memory in @b with @m.
907 * gst_buffer_replace_memory_range:
908 * @buffer: a #GstBuffer.
910 * @length: a length should not be 0
911 * @mem: (transfer full): a #GstMemory
913 * Replaces @length memory blocks in @buffer starting at @idx with @mem.
915 * If @length is -1, all memory starting from @idx will be removed and
916 * replaced with @mem.
918 * @buffer should be writable.
921 gst_buffer_replace_memory_range (GstBuffer * buffer, guint idx, gint length,
926 g_return_if_fail (GST_IS_BUFFER (buffer));
927 g_return_if_fail (gst_buffer_is_writable (buffer));
928 len = GST_BUFFER_MEM_LEN (buffer);
929 g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
930 (length == -1 && idx < len) || (length > 0 && length + idx <= len));
935 _replace_memory (buffer, len, idx, length, mem);
939 * gst_buffer_remove_memory:
943 * Remove the memory block in @b at index @i.
946 * gst_buffer_remove_all_memory:
949 * Remove all the memory blocks in @b.
952 * gst_buffer_remove_memory_range:
953 * @buffer: a #GstBuffer.
957 * Remove @length memory blocks in @buffer starting from @idx.
959 * @length can be -1, in which case all memory starting from @idx is removed.
962 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, gint length)
966 g_return_if_fail (GST_IS_BUFFER (buffer));
967 g_return_if_fail (gst_buffer_is_writable (buffer));
969 len = GST_BUFFER_MEM_LEN (buffer);
970 g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
971 (length == -1 && idx < len) || length + idx <= len);
976 _replace_memory (buffer, len, idx, length, NULL);
980 * gst_buffer_find_memory:
981 * @buffer: a #GstBuffer.
984 * @idx: (out): pointer to index
985 * @length: (out): pointer to length
986 * @skip: (out): pointer to skip
988 * Find the memory blocks that span @size bytes starting from @offset
991 * When this function returns %TRUE, @idx will contain the index of the first
992 * memory bock where the byte for @offset can be found and @length contains the
993 * number of memory blocks containing the @size remaining bytes. @skip contains
994 * the number of bytes to skip in the memory bock at @idx to get to the byte
997 * @size can be -1 to get all the memory blocks after @idx.
999 * Returns: %TRUE when @size bytes starting from @offset could be found in
1000 * @buffer and @idx, @length and @skip will be filled.
1003 gst_buffer_find_memory (GstBuffer * buffer, gsize offset, gsize size,
1004 guint * idx, guint * length, gsize * skip)
1006 guint i, len, found;
1008 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1009 g_return_val_if_fail (idx != NULL, FALSE);
1010 g_return_val_if_fail (length != NULL, FALSE);
1011 g_return_val_if_fail (skip != NULL, FALSE);
1013 len = GST_BUFFER_MEM_LEN (buffer);
1016 for (i = 0; i < len; i++) {
1020 mem = GST_BUFFER_MEM_PTR (buffer, i);
1021 s = gst_memory_get_sizes (mem, NULL, NULL);
1024 /* block before offset, or empty block, skip */
1027 /* block after offset */
1029 /* first block, remember index and offset */
1033 /* return remaining blocks */
1040 /* count the amount of found bytes */
1042 if (found >= size) {
1043 /* we have enough bytes */
1044 *length = i - *idx + 1;
1053 * gst_buffer_get_sizes:
1055 * @of: a pointer to the offset
1056 * @ms: a pointer to the maxsize
1058 * Get the total size of the memory blocks in @b.
1060 * When not %NULL, @of will contain the offset of the data in the first
1061 * memory block in @buffer and @maxsize will contain the sum of the size
1062 * and @of and the amount of extra padding on the last memory block.
1063 * @of and @ms can be used to resize the buffer memory blocks with
1064 * gst_buffer_resize().
1066 * Returns: total size of the memory blocks in @b.
1069 * gst_buffer_get_size:
1072 * Get the total size of the memory blocks in @b.
1074 * Returns: total size of the memory blocks in @b.
1077 * gst_buffer_get_sizes_range:
1078 * @buffer: a #GstBuffer.
1081 * @offset: a pointer to the offset
1082 * @maxsize: a pointer to the maxsize
1084 * Get the total size of @length memory blocks stating from @idx in @buffer.
1086 * When not %NULL, @offset will contain the offset of the data in the
1087 * memory block in @buffer at @idx and @maxsize will contain the sum of the size
1088 * and @offset and the amount of extra padding on the memory block at @idx +
1090 * @offset and @maxsize can be used to resize the buffer memory blocks with
1091 * gst_buffer_resize_range().
1093 * Returns: total size of @length memory blocks starting at @idx in @buffer.
1096 gst_buffer_get_sizes_range (GstBuffer * buffer, guint idx, gint length,
1097 gsize * offset, gsize * maxsize)
1103 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1104 len = GST_BUFFER_MEM_LEN (buffer);
1105 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1106 (length == -1 && idx < len) || (length + idx <= len), 0);
1111 if (G_LIKELY (length == 1)) {
1113 mem = GST_BUFFER_MEM_PTR (buffer, idx);
1114 size = gst_memory_get_sizes (mem, offset, maxsize);
1120 size = offs = extra = 0;
1121 for (i = idx; i < end; i++) {
1124 mem = GST_BUFFER_MEM_PTR (buffer, i);
1125 s = gst_memory_get_sizes (mem, &o, &ms);
1129 /* first size, take accumulated data before as the offset */
1133 /* save the amount of data after this block */
1134 extra = ms - (o + s);
1136 /* empty block, add as extra */
1143 *maxsize = offs + size + extra;
1149 * gst_buffer_resize:
1151 * @of: the offset adjustement
1152 * @s: the new size or -1 to just adjust the offset
1154 * Set the offset and total size of the memory blocks in @b.
1157 * gst_buffer_set_size:
1161 * Set the total size of the memory blocks in @b.
1164 * gst_buffer_resize_range:
1165 * @buffer: a #GstBuffer.
1168 * @offset: the offset adjustement
1169 * @size: the new size or -1 to just adjust the offset
1171 * Set the total size of the @length memory blocks starting at @idx in
1175 gst_buffer_resize_range (GstBuffer * buffer, guint idx, gint length,
1176 gssize offset, gssize size)
1179 gsize bsize, bufsize, bufoffs, bufmax;
1182 g_return_if_fail (gst_buffer_is_writable (buffer));
1183 g_return_if_fail (size >= -1);
1184 len = GST_BUFFER_MEM_LEN (buffer);
1185 g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
1186 (length == -1 && idx < len) || (length + idx <= len));
1191 bufsize = gst_buffer_get_sizes_range (buffer, idx, length, &bufoffs, &bufmax);
1193 GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSSIZE_FORMAT
1194 " size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%"
1195 G_GSIZE_FORMAT, buffer, offset, size, bufsize, bufoffs, bufmax);
1197 /* we can't go back further than the current offset or past the end of the
1199 g_return_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
1200 && bufoffs + offset <= bufmax));
1202 g_return_if_fail (bufsize >= offset);
1203 size = bufsize - offset;
1205 g_return_if_fail (bufmax >= bufoffs + offset + size);
1208 if (offset == 0 && size == bufsize)
1213 for (i = idx; i < end; i++) {
1216 mem = GST_BUFFER_MEM_PTR (buffer, i);
1217 bsize = gst_memory_get_sizes (mem, NULL, NULL);
1220 /* last buffer always gets resized to the remaining size */
1223 /* shrink buffers before the offset */
1224 else if ((gssize) bsize <= offset) {
1226 noffs = offset - bsize;
1229 /* clip other buffers */
1231 left = MIN (bsize - offset, size);
1233 if (offset != 0 || left != bsize) {
1234 if (gst_memory_is_exclusive (mem)) {
1235 gst_memory_resize (mem, offset, left);
1239 if (GST_MEMORY_IS_NO_SHARE (mem))
1240 tmp = gst_memory_copy (mem, offset, left);
1242 tmp = gst_memory_share (mem, offset, left);
1244 gst_memory_unref (mem);
1251 /* FIXME, update exclusive counters */
1252 GST_BUFFER_MEM_PTR (buffer, i) = mem;
1259 * @i: (out): info about the mapping
1260 * @f: flags for the mapping
1262 * This function fills @i with the #GstMapInfo of all merged memory blocks
1265 * @flags describe the desired access of the memory. When @flags is
1266 * #GST_MAP_WRITE, @buffer should be writable (as returned from
1267 * gst_buffer_is_writable()).
1269 * When @buffer is writable but the memory isn't, a writable copy will
1270 * automatically be created and returned. The readonly copy of the buffer memory
1271 * will then also be replaced with this writable copy.
1273 * The memory in @i should be unmapped with gst_buffer_unmap() after usage.
1275 * Returns: %TRUE if the map succeeded and @i contains valid data.
1278 * gst_buffer_map_range:
1279 * @buffer: a #GstBuffer.
1282 * @info: (out): info about the mapping
1283 * @flags: flags for the mapping
1285 * This function fills @info with the #GstMapInfo of @length merged memory blocks
1286 * starting at @idx in @buffer. When @length is -1, all memory blocks starting
1287 * from @idx are merged and mapped.
1289 * @flags describe the desired access of the memory. When @flags is
1290 * #GST_MAP_WRITE, @buffer should be writable (as returned from
1291 * gst_buffer_is_writable()).
1293 * When @buffer is writable but the memory isn't, a writable copy will
1294 * automatically be created and returned. The readonly copy of the buffer memory
1295 * will then also be replaced with this writable copy.
1297 * The memory in @info should be unmapped with gst_buffer_unmap() after usage.
1299 * Returns: %TRUE if the map succeeded and @info contains valid
1303 gst_buffer_map_range (GstBuffer * buffer, guint idx, gint length,
1304 GstMapInfo * info, GstMapFlags flags)
1306 GstMemory *mem, *nmem;
1307 gboolean write, writable;
1310 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1311 g_return_val_if_fail (info != NULL, FALSE);
1312 len = GST_BUFFER_MEM_LEN (buffer);
1313 g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1314 (length == -1 && idx < len) || (length > 0
1315 && length + idx <= len), FALSE);
1317 GST_LOG ("buffer %p, idx %u, length %d, flags %04x", buffer, idx, length,
1320 write = (flags & GST_MAP_WRITE) != 0;
1321 writable = gst_buffer_is_writable (buffer);
1323 /* check if we can write when asked for write access */
1324 if (G_UNLIKELY (write && !writable))
1330 mem = _get_merged_memory (buffer, idx, length);
1331 if (G_UNLIKELY (mem == NULL))
1334 /* now try to map */
1335 nmem = gst_memory_make_mapped (mem, info, flags);
1336 if (G_UNLIKELY (nmem == NULL))
1339 /* if we merged or when the map returned a different memory, we try to replace
1340 * the memory in the buffer */
1341 if (G_UNLIKELY (length > 1 || nmem != mem)) {
1342 /* if the buffer is writable, replace the memory */
1344 _replace_memory (buffer, len, idx, length, gst_memory_ref (nmem));
1347 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
1348 "temporary mapping for memory %p in buffer %p", nmem, buffer);
1357 GST_WARNING_OBJECT (buffer, "write map requested on non-writable buffer");
1358 g_critical ("write map requested on non-writable buffer");
1363 /* empty buffer, we need to return NULL */
1364 GST_DEBUG_OBJECT (buffer, "can't get buffer memory");
1365 info->memory = NULL;
1373 GST_DEBUG_OBJECT (buffer, "cannot map memory");
1380 * @buffer: a #GstBuffer.
1381 * @info: a #GstMapInfo
1383 * Release the memory previously mapped with gst_buffer_map().
1386 gst_buffer_unmap (GstBuffer * buffer, GstMapInfo * info)
1388 g_return_if_fail (GST_IS_BUFFER (buffer));
1389 g_return_if_fail (info != NULL);
1391 /* we need to check for NULL, it is possible that we tried to map a buffer
1392 * without memory and we should be able to unmap that fine */
1393 if (G_LIKELY (info->memory)) {
1394 gst_memory_unmap (info->memory, info);
1395 gst_memory_unref (info->memory);
1401 * @buffer: a #GstBuffer.
1402 * @offset: the offset to fill
1403 * @src: the source address
1404 * @size: the size to fill
1406 * Copy @size bytes from @src to @buffer at @offset.
1408 * Returns: The amount of bytes copied. This value can be lower than @size
1409 * when @buffer did not contain enough data.
1412 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
1416 const guint8 *ptr = src;
1418 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1419 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1420 g_return_val_if_fail (src != NULL, 0);
1422 GST_LOG ("buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT,
1423 buffer, offset, size);
1425 len = GST_BUFFER_MEM_LEN (buffer);
1428 for (i = 0; i < len && left > 0; i++) {
1433 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1434 if (info.size > offset) {
1435 /* we have enough */
1436 tocopy = MIN (info.size - offset, left);
1437 memcpy ((guint8 *) info.data + offset, ptr, tocopy);
1442 /* offset past buffer, skip */
1443 offset -= info.size;
1445 gst_memory_unmap (mem, &info);
1451 * gst_buffer_extract:
1452 * @buffer: a #GstBuffer.
1453 * @offset: the offset to extract
1454 * @dest: the destination address
1455 * @size: the size to extract
1457 * Copy @size bytes starting from @offset in @buffer to @dest.
1459 * Returns: The amount of bytes extracted. This value can be lower than @size
1460 * when @buffer did not contain enough data.
1463 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
1468 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1469 g_return_val_if_fail (dest != NULL, 0);
1471 GST_LOG ("buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT,
1472 buffer, offset, size);
1474 len = GST_BUFFER_MEM_LEN (buffer);
1477 for (i = 0; i < len && left > 0; i++) {
1482 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1483 if (info.size > offset) {
1484 /* we have enough */
1485 tocopy = MIN (info.size - offset, left);
1486 memcpy (ptr, (guint8 *) info.data + offset, tocopy);
1491 /* offset past buffer, skip */
1492 offset -= info.size;
1494 gst_memory_unmap (mem, &info);
1500 * gst_buffer_memcmp:
1501 * @buffer: a #GstBuffer.
1502 * @offset: the offset in @buffer
1503 * @mem: the memory to compare
1504 * @size: the size to compare
1506 * Compare @size bytes starting from @offset in @buffer with the memory in @mem.
1508 * Returns: 0 if the memory is equal.
1511 gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem,
1515 const guint8 *ptr = mem;
1518 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1519 g_return_val_if_fail (mem != NULL, 0);
1521 GST_LOG ("buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT,
1522 buffer, offset, size);
1524 len = GST_BUFFER_MEM_LEN (buffer);
1526 for (i = 0; i < len && size > 0 && res == 0; i++) {
1531 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1532 if (info.size > offset) {
1533 /* we have enough */
1534 tocmp = MIN (info.size - offset, size);
1535 res = memcmp (ptr, (guint8 *) info.data + offset, tocmp);
1540 /* offset past buffer, skip */
1541 offset -= info.size;
1543 gst_memory_unmap (mem, &info);
1549 * gst_buffer_memset:
1550 * @buffer: a #GstBuffer.
1551 * @offset: the offset in @buffer
1552 * @val: the value to set
1553 * @size: the size to set
1555 * Fill @buf with @size bytes with @val starting from @offset.
1557 * Returns: The amount of bytes filled. This value can be lower than @size
1558 * when @buffer did not contain enough data.
1561 gst_buffer_memset (GstBuffer * buffer, gsize offset, guint8 val, gsize size)
1565 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1566 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1568 GST_LOG ("buffer %p, offset %" G_GSIZE_FORMAT ", val %02x, size %"
1569 G_GSIZE_FORMAT, buffer, offset, val, size);
1571 len = GST_BUFFER_MEM_LEN (buffer);
1574 for (i = 0; i < len && left > 0; i++) {
1579 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1580 if (info.size > offset) {
1581 /* we have enough */
1582 toset = MIN (info.size - offset, left);
1583 memset ((guint8 *) info.data + offset, val, toset);
1587 /* offset past buffer, skip */
1588 offset -= info.size;
1590 gst_memory_unmap (mem, &info);
1596 * gst_buffer_copy_region:
1597 * @parent: a #GstBuffer.
1598 * @flags: the #GstBufferCopyFlags
1599 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
1601 * @size: the size of the new #GstBuffer sub-buffer, in bytes.
1603 * Creates a sub-buffer from @parent at @offset and @size.
1604 * This sub-buffer uses the actual memory space of the parent buffer.
1605 * This function will copy the offset and timestamp fields when the
1606 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
1607 * #GST_BUFFER_OFFSET_NONE.
1608 * If @offset equals 0 and @size equals the total size of @buffer, the
1609 * duration and offset end fields are also copied. If not they will be set
1610 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
1614 * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
1618 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
1619 gsize offset, gsize size)
1623 g_return_val_if_fail (buffer != NULL, NULL);
1625 /* create the new buffer */
1626 copy = gst_buffer_new ();
1628 GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
1629 "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
1631 gst_buffer_copy_into (copy, buffer, flags, offset, size);
1637 * gst_buffer_append:
1638 * @buf1: (transfer full): the first source #GstBuffer to append.
1639 * @buf2: (transfer full): the second source #GstBuffer to append.
1641 * Append all the memory from @buf2 to @buf1. The result buffer will contain a
1642 * concatenation of the memory of @buf1 and @buf2.
1644 * Returns: (transfer full): the new #GstBuffer that contains the memory
1645 * of the two source buffers.
1648 * gst_buffer_append_region:
1649 * @buf1: (transfer full): the first source #GstBuffer to append.
1650 * @buf2: (transfer full): the second source #GstBuffer to append.
1651 * @offset: the offset in @buf2
1652 * @size: the size or -1 of @buf2
1654 * Append @size bytes at @offset from @buf2 to @buf1. The result buffer will
1655 * contain a concatenation of the memory of @buf1 and the requested region of
1658 * Returns: (transfer full): the new #GstBuffer that contains the memory
1659 * of the two source buffers.
1662 gst_buffer_append_region (GstBuffer * buf1, GstBuffer * buf2, gssize offset,
1667 g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
1668 g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
1670 buf1 = gst_buffer_make_writable (buf1);
1671 buf2 = gst_buffer_make_writable (buf2);
1673 gst_buffer_resize (buf2, offset, size);
1675 len = GST_BUFFER_MEM_LEN (buf2);
1676 for (i = 0; i < len; i++) {
1679 mem = GST_BUFFER_MEM_PTR (buf2, i);
1680 GST_BUFFER_MEM_PTR (buf2, i) = NULL;
1681 _memory_add (buf1, -1, mem, FALSE);
1684 GST_BUFFER_MEM_LEN (buf2) = 0;
1685 gst_buffer_unref (buf2);
1691 * gst_buffer_get_meta:
1692 * @buffer: a #GstBuffer
1693 * @api: the #GType of an API
1695 * Get the metadata for @api on buffer. When there is no such
1696 * metadata, NULL is returned.
1698 * Returns: (transfer none): the metadata for @api on @buffer.
1701 gst_buffer_get_meta (GstBuffer * buffer, GType api)
1704 GstMeta *result = NULL;
1706 g_return_val_if_fail (buffer != NULL, NULL);
1707 g_return_val_if_fail (api != 0, NULL);
1709 /* find GstMeta of the requested API */
1710 for (item = GST_BUFFER_META (buffer); item; item = item->next) {
1711 GstMeta *meta = &item->meta;
1712 if (meta->info->api == api) {
1721 * gst_buffer_add_meta:
1722 * @buffer: a #GstBuffer
1723 * @info: a #GstMetaInfo
1724 * @params: params for @info
1726 * Add metadata for @info to @buffer using the parameters in @params.
1728 * Returns: (transfer none): the metadata for the api in @info on @buffer.
1731 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
1735 GstMeta *result = NULL;
1738 g_return_val_if_fail (buffer != NULL, NULL);
1739 g_return_val_if_fail (info != NULL, NULL);
1740 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
1742 /* create a new slice */
1743 size = ITEM_SIZE (info);
1744 item = g_slice_alloc (size);
1745 result = &item->meta;
1746 result->info = info;
1747 result->flags = GST_META_FLAG_NONE;
1749 GST_CAT_DEBUG (GST_CAT_BUFFER,
1750 "alloc metadata %p (%s) of size %" G_GSIZE_FORMAT, result,
1751 g_type_name (info->type), info->size);
1753 /* call the init_func when needed */
1754 if (info->init_func)
1755 if (!info->init_func (result, params, buffer))
1758 /* and add to the list of metadata */
1759 item->next = GST_BUFFER_META (buffer);
1760 GST_BUFFER_META (buffer) = item;
1766 g_slice_free1 (size, item);
1772 * gst_buffer_remove_meta:
1773 * @buffer: a #GstBuffer
1776 * Remove the metadata for @meta on @buffer.
1778 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
1779 * metadata was on @buffer.
1782 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
1784 GstMetaItem *walk, *prev;
1786 g_return_val_if_fail (buffer != NULL, FALSE);
1787 g_return_val_if_fail (meta != NULL, FALSE);
1788 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
1789 g_return_val_if_fail (!GST_META_FLAG_IS_SET (meta, GST_META_FLAG_LOCKED),
1792 /* find the metadata and delete */
1793 prev = GST_BUFFER_META (buffer);
1794 for (walk = prev; walk; walk = walk->next) {
1795 GstMeta *m = &walk->meta;
1797 const GstMetaInfo *info = meta->info;
1799 /* remove from list */
1800 if (GST_BUFFER_META (buffer) == walk)
1801 GST_BUFFER_META (buffer) = walk->next;
1803 prev->next = walk->next;
1804 /* call free_func if any */
1805 if (info->free_func)
1806 info->free_func (m, buffer);
1808 /* and free the slice */
1809 g_slice_free1 (ITEM_SIZE (info), walk);
1814 return walk != NULL;
1818 * gst_buffer_iterate_meta:
1819 * @buffer: a #GstBuffer
1820 * @state: an opaque state pointer
1822 * Retrieve the next #GstMeta after @current. If @state points
1823 * to %NULL, the first metadata is returned.
1825 * @state will be updated with an opage state pointer
1827 * Returns: (transfer none): The next #GstMeta or %NULL when there are
1831 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
1835 g_return_val_if_fail (buffer != NULL, NULL);
1836 g_return_val_if_fail (state != NULL, NULL);
1838 meta = (GstMetaItem **) state;
1840 /* state NULL, move to first item */
1841 *meta = GST_BUFFER_META (buffer);
1843 /* state !NULL, move to next item in list */
1844 *meta = (*meta)->next;
1847 return &(*meta)->meta;
1853 * gst_buffer_foreach_meta:
1854 * @buffer: a #GstBuffer
1855 * @func: (scope call): a #GstBufferForeachMetaFunc to call
1856 * @user_data: (closure): user data passed to @func
1858 * Call @func with @user_data for each meta in @buffer.
1860 * @func can modify the passed meta pointer or its contents. The return value
1861 * of @func define if this function returns or if the remaining metadata items
1862 * in the buffer should be skipped.
1865 gst_buffer_foreach_meta (GstBuffer * buffer, GstBufferForeachMetaFunc func,
1868 GstMetaItem *walk, *prev, *next;
1870 g_return_if_fail (buffer != NULL);
1871 g_return_if_fail (func != NULL);
1873 /* find the metadata and delete */
1874 prev = GST_BUFFER_META (buffer);
1875 for (walk = prev; walk; walk = next) {
1879 m = new = &walk->meta;
1882 res = func (buffer, &new, user_data);
1885 const GstMetaInfo *info = m->info;
1887 GST_CAT_DEBUG (GST_CAT_BUFFER, "remove metadata %p (%s)", m,
1888 g_type_name (info->type));
1890 g_return_if_fail (gst_buffer_is_writable (buffer));
1891 g_return_if_fail (!GST_META_FLAG_IS_SET (m, GST_META_FLAG_LOCKED));
1893 /* remove from list */
1894 if (GST_BUFFER_META (buffer) == walk)
1895 GST_BUFFER_META (buffer) = next;
1899 /* call free_func if any */
1900 if (info->free_func)
1901 info->free_func (m, buffer);
1903 /* and free the slice */
1904 g_slice_free1 (ITEM_SIZE (info), walk);