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, supporting sub-buffers.
26 * @see_also: #GstPad, #GstMiniObject, #GstBufferPool
28 * Buffers are the basic unit of data transfer in GStreamer. The #GstBuffer
29 * type provides all the state necessary to define the regions of memory as
30 * part of a stream. Region copies are also supported, allowing a smaller
31 * region of a buffer to become its own buffer, with mechanisms in place to
32 * ensure that neither memory space goes away prematurely.
34 * Buffers are usually created with gst_buffer_new(). After a buffer has been
35 * created one will typically allocate memory for it and add it to the buffer.
36 * The following example creates a buffer that can hold a given video frame
37 * with a given width, height and bits per plane.
39 * <title>Creating a buffer for a video frame</title>
43 * gint size, width, height, bpp;
45 * size = width * height * bpp;
46 * buffer = gst_buffer_new ();
47 * memory = gst_allocator_alloc (NULL, size, 0);
48 * gst_buffer_take_memory (buffer, -1, memory);
53 * Alternatively, use gst_buffer_new_allocate()
54 * to create a buffer with preallocated data of a given size.
56 * Buffers can contain a list of #GstMemory objects. You can retrieve how many
57 * memory objects with gst_buffer_n_memory() and you can get a pointer
58 * to memory with gst_buffer_peek_memory()
60 * A buffer will usually have timestamps, and a duration, but neither of these
61 * are guaranteed (they may be set to #GST_CLOCK_TIME_NONE). Whenever a
62 * meaningful value can be given for these, they should be set. The timestamps
63 * and duration are measured in nanoseconds (they are #GstClockTime values).
65 * A buffer can also have one or both of a start and an end offset. These are
66 * media-type specific. For video buffers, the start offset will generally be
67 * the frame number. For audio buffers, it will be the number of samples
68 * produced so far. For compressed data, it could be the byte offset in a
69 * source or destination file. Likewise, the end offset will be the offset of
70 * the end of the buffer. These can only be meaningfully interpreted if you
71 * know the media type of the buffer (the #GstCaps set on it). Either or both
72 * can be set to #GST_BUFFER_OFFSET_NONE.
74 * gst_buffer_ref() is used to increase the refcount of a buffer. This must be
75 * done when you want to keep a handle to the buffer after pushing it to the
78 * To efficiently create a smaller buffer out of an existing one, you can
79 * use gst_buffer_copy_region().
81 * If a plug-in wants to modify the buffer data or metadata in-place, it should
82 * first obtain a buffer that is safe to modify by using
83 * gst_buffer_make_writable(). This function is optimized so that a copy will
84 * only be made when it is necessary.
86 * Several flags of the buffer can be set and unset with the
87 * GST_BUFFER_FLAG_SET() and GST_BUFFER_FLAG_UNSET() macros. Use
88 * GST_BUFFER_FLAG_IS_SET() to test if a certain #GstBufferFlag is set.
90 * Buffers can be efficiently merged into a larger buffer with
91 * gst_buffer_span(), which avoids memory copies when the gst_buffer_is_span_fast()
92 * function returns TRUE.
94 * An element should either unref the buffer or push it out on a src pad
95 * using gst_pad_push() (see #GstPad).
97 * Buffers are usually freed by unreffing them with gst_buffer_unref(). When
98 * the refcount drops to 0, any data pointed to by the buffer is unreffed as
101 * Last reviewed on November 8, 2011 (0.11.2)
103 #include "gst_private.h"
112 #include "gstbuffer.h"
113 #include "gstbufferpool.h"
115 #include "gstutils.h"
116 #include "gstminiobject.h"
117 #include "gstversion.h"
119 GType _gst_buffer_type = 0;
121 static GstMemory *_gst_buffer_arr_span (GstMemory ** mem[], gsize len[],
122 guint n, gsize offset, gsize size, gboolean writable);
124 typedef struct _GstMetaItem GstMetaItem;
131 #define ITEM_SIZE(info) ((info)->size + sizeof (GstMetaItem))
133 #define GST_BUFFER_MEM_MAX 16
135 #define GST_BUFFER_MEM_LEN(b) (((GstBufferImpl *)(b))->len)
136 #define GST_BUFFER_MEM_ARRAY(b) (((GstBufferImpl *)(b))->mem)
137 #define GST_BUFFER_MEM_PTR(b,i) (((GstBufferImpl *)(b))->mem[i])
138 #define GST_BUFFER_BUFMEM(b) (((GstBufferImpl *)(b))->bufmem)
139 #define GST_BUFFER_META(b) (((GstBufferImpl *)(b))->item)
145 /* the memory blocks */
147 GstMemory *mem[GST_BUFFER_MEM_MAX];
149 /* memory of the buffer when allocated from 1 chunk */
152 /* FIXME, make metadata allocation more efficient by using part of the
158 _span_memory (GstBuffer * buffer, gsize offset, gsize size, gboolean writable)
160 GstMemory *span, **mem[1];
163 /* not enough room, span buffers */
164 mem[0] = GST_BUFFER_MEM_ARRAY (buffer);
165 len[0] = GST_BUFFER_MEM_LEN (buffer);
168 size = gst_buffer_get_size (buffer);
170 span = _gst_buffer_arr_span (mem, len, 1, offset, size, writable);
176 _get_merged_memory (GstBuffer * buffer, gboolean * merged)
181 len = GST_BUFFER_MEM_LEN (buffer);
183 if (G_UNLIKELY (len == 0)) {
186 } else if (G_LIKELY (len == 1)) {
187 /* we can take the first one */
188 mem = GST_BUFFER_MEM_PTR (buffer, 0);
189 gst_memory_ref (mem);
192 /* we need to span memory */
193 mem = _span_memory (buffer, 0, -1, FALSE);
200 _replace_all_memory (GstBuffer * buffer, GstMemory * mem)
204 len = GST_BUFFER_MEM_LEN (buffer);
206 if (G_LIKELY (len == 1 && GST_BUFFER_MEM_PTR (buffer, 0) == mem)) {
207 gst_memory_unref (mem);
211 GST_LOG ("buffer %p replace with memory %p", buffer, mem);
213 /* unref old memory */
214 for (i = 0; i < len; i++)
215 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
216 /* replace with single memory */
217 GST_BUFFER_MEM_PTR (buffer, 0) = mem;
218 GST_BUFFER_MEM_LEN (buffer) = 1;
222 _memory_add (GstBuffer * buffer, guint idx, GstMemory * mem)
224 guint i, len = GST_BUFFER_MEM_LEN (buffer);
226 if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) {
227 /* too many buffer, span them. */
228 /* FIXME, there is room for improvement here: We could only try to merge
229 * 2 buffers to make some room. If we can't efficiently merge 2 buffers we
230 * could try to only merge the two smallest buffers to avoid memcpy, etc. */
231 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "memory array overflow in buffer %p",
233 _replace_all_memory (buffer, _span_memory (buffer, 0, -1, FALSE));
234 /* we now have 1 single spanned buffer */
241 for (i = len; i > idx; i--) {
242 /* move buffers to insert, FIXME, we need to insert first and then merge */
243 GST_BUFFER_MEM_PTR (buffer, i) = GST_BUFFER_MEM_PTR (buffer, i - 1);
245 /* and insert the new buffer */
246 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
247 GST_BUFFER_MEM_LEN (buffer) = len + 1;
250 GST_DEFINE_MINI_OBJECT_TYPE (GstBuffer, gst_buffer);
253 _priv_gst_buffer_initialize (void)
255 _gst_buffer_type = gst_buffer_get_type ();
259 * gst_buffer_copy_into:
260 * @dest: a destination #GstBuffer
261 * @src: a source #GstBuffer
262 * @flags: flags indicating what metadata fields should be copied.
263 * @offset: offset to copy from
264 * @size: total size to copy. If -1, all data is copied.
266 * Copies the information from @src into @dest.
268 * @flags indicate which fields will be copied.
271 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
272 GstBufferCopyFlags flags, gsize offset, gsize size)
276 gboolean region = FALSE;
278 g_return_if_fail (dest != NULL);
279 g_return_if_fail (src != NULL);
281 /* nothing to copy if the buffers are the same */
282 if (G_UNLIKELY (dest == src))
285 g_return_if_fail (gst_buffer_is_writable (dest));
287 bufsize = gst_buffer_get_size (src);
288 g_return_if_fail (bufsize >= offset);
292 size = bufsize - offset;
295 g_return_if_fail (bufsize >= offset + size);
297 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p, offset %" G_GSIZE_FORMAT
298 "-%" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT, src, dest, offset, size,
301 if (flags & GST_BUFFER_COPY_FLAGS) {
303 GST_MINI_OBJECT_FLAGS (dest) = GST_MINI_OBJECT_FLAGS (src);
306 if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
308 GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src);
309 GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src);
310 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
311 if (size == bufsize) {
312 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
313 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
316 GST_BUFFER_PTS (dest) = GST_CLOCK_TIME_NONE;
317 GST_BUFFER_DTS (dest) = GST_CLOCK_TIME_NONE;
318 GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
319 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
320 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
324 if (flags & GST_BUFFER_COPY_MEMORY) {
326 gsize skip, left, len, i, bsize;
328 len = GST_BUFFER_MEM_LEN (src);
332 /* copy and make regions of the memory */
333 for (i = 0; i < len && left > 0; i++) {
334 mem = GST_BUFFER_MEM_PTR (src, i);
335 bsize = gst_memory_get_sizes (mem, NULL, NULL);
338 /* don't copy buffer */
343 tocopy = MIN (bsize - skip, left);
344 if (mem->flags & GST_MEMORY_FLAG_NO_SHARE) {
345 /* no share, always copy then */
346 mem = gst_memory_copy (mem, skip, tocopy);
348 } else if (tocopy < bsize) {
349 /* we need to clip something */
350 mem = gst_memory_share (mem, skip, tocopy);
353 mem = gst_memory_ref (mem);
355 _memory_add (dest, -1, mem);
359 if (flags & GST_BUFFER_COPY_MERGE) {
360 _replace_all_memory (dest, _span_memory (dest, 0, size, FALSE));
364 if (flags & GST_BUFFER_COPY_META) {
365 for (walk = GST_BUFFER_META (src); walk; walk = walk->next) {
366 GstMeta *meta = &walk->meta;
367 const GstMetaInfo *info = meta->info;
369 if (info->transform_func) {
370 GstMetaTransformCopy copy_data;
372 copy_data.region = region;
373 copy_data.offset = offset;
374 copy_data.size = size;
376 info->transform_func (dest, meta, src,
377 _gst_meta_transform_copy, ©_data);
384 _gst_buffer_copy (GstBuffer * buffer)
388 g_return_val_if_fail (buffer != NULL, NULL);
390 /* create a fresh new buffer */
391 copy = gst_buffer_new ();
393 /* we simply copy everything from our parent */
394 gst_buffer_copy_into (copy, buffer, GST_BUFFER_COPY_ALL, 0, -1);
399 /* the default dispose function revives the buffer and returns it to the
400 * pool when there is a pool */
402 _gst_buffer_dispose (GstBuffer * buffer)
406 /* no pool, do free */
407 if ((pool = buffer->pool) == NULL)
410 /* keep the buffer alive */
411 gst_buffer_ref (buffer);
412 /* return the buffer to the pool */
413 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
414 gst_buffer_pool_release_buffer (pool, buffer);
420 _gst_buffer_free (GstBuffer * buffer)
422 GstMetaItem *walk, *next;
426 g_return_if_fail (buffer != NULL);
428 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
431 for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
432 GstMeta *meta = &walk->meta;
433 const GstMetaInfo *info = meta->info;
435 /* call free_func if any */
437 info->free_func (meta, buffer);
440 /* and free the slice */
441 g_slice_free1 (ITEM_SIZE (info), walk);
444 /* get the size, when unreffing the memory, we could also unref the buffer
446 msize = GST_MINI_OBJECT_SIZE (buffer);
448 /* free our memory */
449 len = GST_BUFFER_MEM_LEN (buffer);
450 for (i = 0; i < len; i++)
451 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
453 /* we set msize to 0 when the buffer is part of the memory block */
455 g_slice_free1 (msize, buffer);
457 gst_memory_unref (GST_BUFFER_BUFMEM (buffer));
461 gst_buffer_init (GstBufferImpl * buffer, gsize size)
463 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), _gst_buffer_type, size);
465 buffer->buffer.mini_object.copy =
466 (GstMiniObjectCopyFunction) _gst_buffer_copy;
467 buffer->buffer.mini_object.dispose =
468 (GstMiniObjectDisposeFunction) _gst_buffer_dispose;
469 buffer->buffer.mini_object.free =
470 (GstMiniObjectFreeFunction) _gst_buffer_free;
472 GST_BUFFER (buffer)->pool = NULL;
473 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
474 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
475 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
476 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
477 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
479 GST_BUFFER_MEM_LEN (buffer) = 0;
480 GST_BUFFER_META (buffer) = NULL;
486 * Creates a newly allocated buffer without any data.
490 * Returns: (transfer full): the new #GstBuffer.
493 gst_buffer_new (void)
495 GstBufferImpl *newbuf;
497 newbuf = g_slice_new (GstBufferImpl);
498 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
500 gst_buffer_init (newbuf, sizeof (GstBufferImpl));
502 return GST_BUFFER_CAST (newbuf);
506 * gst_buffer_new_allocate:
507 * @allocator: (allow-none): the #GstAllocator to use, or NULL to use the
509 * @size: the size in bytes of the new buffer's data.
510 * @align: the alignment of the buffer memory
512 * Tries to create a newly allocated buffer with data of the given size and
513 * alignment from @allocator. If the requested amount of memory can't be
514 * allocated, NULL will be returned. The allocated buffer memory is not cleared.
516 * When @allocator is NULL, the default memory allocator will be used.
518 * Allocator buffer memory will be aligned to multiples of (@align + 1) bytes.
520 * Note that when @size == 0, the buffer will not have memory associated with it.
524 * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
528 gst_buffer_new_allocate (GstAllocator * allocator, gsize size, gsize align)
539 mem = gst_allocator_alloc (allocator, 0, size, 0, size, align);
540 if (G_UNLIKELY (mem == NULL))
546 newbuf = gst_buffer_new ();
549 _memory_add (newbuf, -1, mem);
551 GST_CAT_LOG (GST_CAT_BUFFER,
552 "new buffer %p of size %" G_GSIZE_FORMAT " from allocator %p", newbuf,
557 asize = sizeof (GstBufferImpl) + size;
558 data = g_slice_alloc (asize);
559 if (G_UNLIKELY (data == NULL))
562 newbuf = GST_BUFFER_CAST (data);
564 gst_buffer_init ((GstBufferImpl *) data, asize);
566 mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL,
568 _memory_add (newbuf, -1, mem);
573 /* allocate memory and buffer, it might be interesting to do this but there
574 * are many complications. We need to keep the memory mapped to access the
575 * buffer fields and the memory for the buffer might be just very slow. We
576 * also need to do some more magic to get the alignment right. */
577 asize = sizeof (GstBufferImpl) + size;
578 mem = gst_allocator_alloc (allocator, asize, align);
579 if (G_UNLIKELY (mem == NULL))
582 /* map the data part and init the buffer in it, set the buffer size to 0 so
583 * that a finalize won't free the buffer */
584 data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
585 gst_buffer_init ((GstBufferImpl *) data, 0);
586 gst_memory_unmap (mem);
588 /* strip off the buffer */
589 gst_memory_resize (mem, sizeof (GstBufferImpl), size);
591 newbuf = GST_BUFFER_CAST (data);
592 GST_BUFFER_BUFMEM (newbuf) = mem;
595 _memory_add (newbuf, -1, gst_memory_ref (mem));
603 GST_CAT_WARNING (GST_CAT_BUFFER,
604 "failed to allocate %" G_GSIZE_FORMAT " bytes", size);
610 * gst_buffer_new_wrapped_full:
611 * @data: data to wrap
612 * @free_func: function to free @data
613 * @offset: offset in @data of valid data
614 * @size: size of valid data in @data starting at @offset
616 * Creates a new buffer that wraps the given @data. Valid data is set
617 * to start at @offset and up to @size. If no @free_func is provided,
618 * buffer memory is marked READONLY.
622 * Returns: (transfer full): a new #GstBuffer
625 gst_buffer_new_wrapped_full (gpointer data, GFreeFunc free_func, gsize offset,
630 newbuf = gst_buffer_new ();
631 gst_buffer_append_memory (newbuf,
632 gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
633 data, offset + size, offset, size, data, free_func));
639 * gst_buffer_new_wrapped:
640 * @data: data to wrap
641 * @size: allocated size of @data
643 * Creates a new buffer that wraps the given @data. The memory will be freed
644 * with g_free and will be marked writable.
648 * Returns: (transfer full): a new #GstBuffer
651 gst_buffer_new_wrapped (gpointer data, gsize size)
653 return gst_buffer_new_wrapped_full (data, g_free, 0, size);
657 * gst_buffer_n_memory:
658 * @buffer: a #GstBuffer.
660 * Get the amount of memory blocks that this buffer has.
662 * Returns: (transfer full): the amount of memory block in this buffer.
665 gst_buffer_n_memory (GstBuffer * buffer)
667 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
669 return GST_BUFFER_MEM_LEN (buffer);
673 * gst_buffer_take_memory:
674 * @buffer: a #GstBuffer.
675 * @idx: the index to add the memory at, or -1 to append it to the end
676 * @mem: (transfer full): a #GstMemory.
678 * Add the memory block @mem to @buffer at @idx. This function takes ownership
679 * of @mem and thus doesn't increase its refcount.
682 gst_buffer_take_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
684 g_return_if_fail (GST_IS_BUFFER (buffer));
685 g_return_if_fail (gst_buffer_is_writable (buffer));
686 g_return_if_fail (mem != NULL);
687 g_return_if_fail (idx == -1 ||
688 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)));
690 _memory_add (buffer, idx, mem);
694 _get_mapped (GstBuffer * buffer, guint idx, GstMapInfo * info,
697 GstMemory *mem, *mapped;
699 mem = GST_BUFFER_MEM_PTR (buffer, idx);
701 mapped = gst_memory_make_mapped (mem, info, flags);
706 GST_BUFFER_MEM_PTR (buffer, idx) = mapped;
707 gst_memory_unref (mem);
714 * gst_buffer_get_memory:
715 * @buffer: a #GstBuffer.
718 * Get the memory block in @buffer at @idx. If @idx is -1, all memory is merged
719 * into one large #GstMemory object that is then returned.
721 * Returns: (transfer full): a #GstMemory at @idx. Use gst_memory_unref () after usage.
724 gst_buffer_get_memory (GstBuffer * buffer, gint idx)
729 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
730 g_return_val_if_fail (idx == -1 ||
731 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)), NULL);
734 mem = _get_merged_memory (buffer, &merged);
735 } else if ((mem = GST_BUFFER_MEM_PTR (buffer, idx))) {
736 gst_memory_ref (mem);
742 * gst_buffer_replace_memory:
743 * @buffer: a #GstBuffer.
745 * @mem: (transfer full): a #GstMemory
747 * Replaces the memory block in @buffer at @idx with @mem. If @idx is -1, all
748 * memory will be removed and replaced with @mem.
750 * @buffer should be writable.
753 gst_buffer_replace_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
755 g_return_if_fail (GST_IS_BUFFER (buffer));
756 g_return_if_fail (gst_buffer_is_writable (buffer));
757 g_return_if_fail (idx == -1 ||
758 (idx >= 0 && idx < GST_BUFFER_MEM_LEN (buffer)));
761 _replace_all_memory (buffer, mem);
765 if ((old = GST_BUFFER_MEM_PTR (buffer, idx)))
766 gst_memory_unref (old);
767 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
772 * gst_buffer_remove_memory_range:
773 * @buffer: a #GstBuffer.
777 * Remove @len memory blocks in @buffer starting from @idx.
779 * @length can be -1, in which case all memory starting from @idx is removed.
782 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, gint length)
786 g_return_if_fail (GST_IS_BUFFER (buffer));
787 g_return_if_fail (gst_buffer_is_writable (buffer));
789 len = GST_BUFFER_MEM_LEN (buffer);
790 g_return_if_fail ((length == -1 && idx < len) || length + idx < len);
796 for (i = idx; i < end; i++)
797 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
800 g_memmove (&GST_BUFFER_MEM_PTR (buffer, idx),
801 &GST_BUFFER_MEM_PTR (buffer, end), (len - end) * sizeof (gpointer));
803 GST_BUFFER_MEM_LEN (buffer) = len - length;
807 * gst_buffer_get_sizes:
808 * @buffer: a #GstBuffer.
809 * @offset: a pointer to the offset
810 * @maxsize: a pointer to the maxsize
812 * Get the total size of all memory blocks in @buffer.
814 * When not %NULL, @offset will contain the offset of the data in the first
815 * memory block in @buffer and @maxsize will contain the sum of the size
816 * and @offset and the amount of extra padding on the last memory block.
817 * @offset and @maxsize can be used to resize the buffer with
818 * gst_buffer_resize().
820 * Returns: the total size of the memory in @buffer.
823 gst_buffer_get_sizes (GstBuffer * buffer, gsize * offset, gsize * maxsize)
829 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
831 len = GST_BUFFER_MEM_LEN (buffer);
833 if (G_LIKELY (len == 1)) {
835 mem = GST_BUFFER_MEM_PTR (buffer, 0);
836 size = gst_memory_get_sizes (mem, offset, maxsize);
841 size = offs = extra = 0;
842 for (i = 0; i < len; i++) {
845 mem = GST_BUFFER_MEM_PTR (buffer, i);
846 s = gst_memory_get_sizes (mem, &o, &ms);
850 /* first size, take accumulated data before as the offset */
854 /* save the amount of data after this block */
855 extra = ms - (o + s);
857 /* empty block, add as extra */
864 *maxsize = offs + size + extra;
871 * @buffer: a #GstBuffer.
872 * @offset: the offset adjustement
873 * @size: the new size or -1 to just adjust the offset
875 * Set the total size of the buffer
878 gst_buffer_resize (GstBuffer * buffer, gssize offset, gssize size)
882 gsize bsize, bufsize, bufoffs, bufmax;
885 g_return_if_fail (gst_buffer_is_writable (buffer));
886 g_return_if_fail (size >= -1);
888 bufsize = gst_buffer_get_sizes (buffer, &bufoffs, &bufmax);
890 GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSSIZE_FORMAT
891 " size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%"
892 G_GSIZE_FORMAT, buffer, offset, size, bufsize, bufoffs, bufmax);
894 /* we can't go back further than the current offset or past the end of the
896 g_return_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
897 && bufoffs + offset <= bufmax));
899 g_return_if_fail (bufsize >= offset);
900 size = bufsize - offset;
902 g_return_if_fail (bufmax >= bufoffs + offset + size);
905 if (offset == 0 && size == bufsize)
908 len = GST_BUFFER_MEM_LEN (buffer);
911 for (i = 0; i < len; i++) {
914 mem = GST_BUFFER_MEM_PTR (buffer, i);
915 bsize = gst_memory_get_sizes (mem, NULL, NULL);
918 /* last buffer always gets resized to the remaining size */
921 /* shrink buffers before the offset */
922 else if ((gssize) bsize <= offset) {
924 noffs = offset - bsize;
927 /* clip other buffers */
929 left = MIN (bsize - offset, size);
931 if (offset != 0 || left != bsize) {
932 if (gst_memory_is_exclusive (mem)) {
933 gst_memory_resize (mem, offset, left);
937 if (mem->flags & GST_MEMORY_FLAG_NO_SHARE)
938 tmp = gst_memory_copy (mem, offset, left);
940 tmp = gst_memory_share (mem, offset, left);
942 gst_memory_unref (mem);
949 GST_BUFFER_MEM_PTR (buffer, i) = mem;
955 * @buffer: a #GstBuffer.
956 * @info: (out): info about the mapping
957 * @flags: flags for the mapping
959 * This function fills @info with a pointer to the merged memory in @buffer.
960 * @flags describe the desired access of the memory. When @flags is
961 * #GST_MAP_WRITE, @buffer should be writable (as returned from
962 * gst_buffer_is_writable()).
964 * When @buffer is writable but the memory isn't, a writable copy will
965 * automatically be created and returned. The readonly copy of the buffer memory
966 * will then also be replaced with this writable copy.
968 * When the buffer contains multiple memory blocks, the returned pointer will be
969 * a concatenation of the memory blocks.
971 * The memory in @info should be unmapped with gst_buffer_unmap() after usage.
973 * Returns: (transfer full): %TRUE if the map succeeded and @info contains valid
977 gst_buffer_map (GstBuffer * buffer, GstMapInfo * info, GstMapFlags flags)
979 GstMemory *mem, *nmem;
980 gboolean write, writable, merged;
982 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
983 g_return_val_if_fail (info != NULL, FALSE);
985 write = (flags & GST_MAP_WRITE) != 0;
986 writable = gst_buffer_is_writable (buffer);
988 /* check if we can write when asked for write access */
989 if (G_UNLIKELY (write && !writable))
992 mem = _get_merged_memory (buffer, &merged);
993 if (G_UNLIKELY (mem == NULL))
997 nmem = gst_memory_make_mapped (mem, info, flags);
998 if (G_UNLIKELY (nmem == NULL))
1001 /* if we merged or when the map returned a different memory, we try to replace
1002 * the memory in the buffer */
1003 if (G_UNLIKELY (merged || nmem != mem)) {
1004 /* if the buffer is writable, replace the memory */
1006 _replace_all_memory (buffer, gst_memory_ref (nmem));
1008 if (GST_BUFFER_MEM_LEN (buffer) > 1) {
1009 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
1010 "temporary mapping for memory %p in buffer %p", nmem, buffer);
1019 GST_WARNING_OBJECT (buffer, "write map requested on non-writable buffer");
1020 g_critical ("write map requested on non-writable buffer");
1025 /* empty buffer, we need to return NULL */
1026 GST_DEBUG_OBJECT (buffer, "can't get buffer memory");
1027 info->memory = NULL;
1035 GST_DEBUG_OBJECT (buffer, "cannot map memory");
1042 * @buffer: a #GstBuffer.
1043 * @info: a #GstMapInfo
1045 * Release the memory previously mapped with gst_buffer_map().
1048 gst_buffer_unmap (GstBuffer * buffer, GstMapInfo * info)
1050 g_return_if_fail (GST_IS_BUFFER (buffer));
1051 g_return_if_fail (info != NULL);
1053 /* we need to check for NULL, it is possible that we tried to map a buffer
1054 * without memory and we should be able to unmap that fine */
1055 if (G_LIKELY (info->memory)) {
1056 gst_memory_unmap (info->memory, info);
1057 gst_memory_unref (info->memory);
1063 * @buffer: a #GstBuffer.
1064 * @offset: the offset to fill
1065 * @src: the source address
1066 * @size: the size to fill
1068 * Copy @size bytes from @src to @buffer at @offset.
1070 * Returns: The amount of bytes copied. This value can be lower than @size
1071 * when @buffer did not contain enough data.
1074 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
1078 const guint8 *ptr = src;
1080 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1081 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1082 g_return_val_if_fail (src != NULL, 0);
1084 len = GST_BUFFER_MEM_LEN (buffer);
1087 for (i = 0; i < len && left > 0; i++) {
1092 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1093 if (info.size > offset) {
1094 /* we have enough */
1095 tocopy = MIN (info.size - offset, left);
1096 memcpy ((guint8 *) info.data + offset, ptr, tocopy);
1101 /* offset past buffer, skip */
1102 offset -= info.size;
1104 gst_memory_unmap (mem, &info);
1110 * gst_buffer_extract:
1111 * @buffer: a #GstBuffer.
1112 * @offset: the offset to extract
1113 * @dest: the destination address
1114 * @size: the size to extract
1116 * Copy @size bytes starting from @offset in @buffer to @dest.
1118 * Returns: The amount of bytes extracted. This value can be lower than @size
1119 * when @buffer did not contain enough data.
1122 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
1127 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1128 g_return_val_if_fail (dest != NULL, 0);
1130 len = GST_BUFFER_MEM_LEN (buffer);
1133 for (i = 0; i < len && left > 0; i++) {
1138 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1139 if (info.size > offset) {
1140 /* we have enough */
1141 tocopy = MIN (info.size - offset, left);
1142 memcpy (ptr, (guint8 *) info.data + offset, tocopy);
1147 /* offset past buffer, skip */
1148 offset -= info.size;
1150 gst_memory_unmap (mem, &info);
1156 * gst_buffer_memcmp:
1157 * @buffer: a #GstBuffer.
1158 * @offset: the offset in @buffer
1159 * @mem: the memory to compare
1160 * @size: the size to compare
1162 * Compare @size bytes starting from @offset in @buffer with the memory in @mem.
1164 * Returns: 0 if the memory is equal.
1167 gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem,
1171 const guint8 *ptr = mem;
1174 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1175 g_return_val_if_fail (mem != NULL, 0);
1177 len = GST_BUFFER_MEM_LEN (buffer);
1179 for (i = 0; i < len && size > 0 && res == 0; i++) {
1184 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1185 if (info.size > offset) {
1186 /* we have enough */
1187 tocmp = MIN (info.size - offset, size);
1188 res = memcmp (ptr, (guint8 *) info.data + offset, tocmp);
1193 /* offset past buffer, skip */
1194 offset -= info.size;
1196 gst_memory_unmap (mem, &info);
1202 * gst_buffer_memset:
1203 * @buffer: a #GstBuffer.
1204 * @offset: the offset in @buffer
1205 * @val: the value to set
1206 * @size: the size to set
1208 * Fill @buf with @size bytes with @val starting from @offset.
1210 * Returns: The amount of bytes filled. This value can be lower than @size
1211 * when @buffer did not contain enough data.
1214 gst_buffer_memset (GstBuffer * buffer, gsize offset, guint8 val, gsize size)
1218 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1219 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1221 len = GST_BUFFER_MEM_LEN (buffer);
1224 for (i = 0; i < len && left > 0; i++) {
1229 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1230 if (info.size > offset) {
1231 /* we have enough */
1232 toset = MIN (info.size - offset, left);
1233 memset ((guint8 *) info.data + offset, val, toset);
1237 /* offset past buffer, skip */
1238 offset -= info.size;
1240 gst_memory_unmap (mem, &info);
1246 * gst_buffer_copy_region:
1247 * @parent: a #GstBuffer.
1248 * @flags: the #GstBufferCopyFlags
1249 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
1251 * @size: the size of the new #GstBuffer sub-buffer, in bytes.
1253 * Creates a sub-buffer from @parent at @offset and @size.
1254 * This sub-buffer uses the actual memory space of the parent buffer.
1255 * This function will copy the offset and timestamp fields when the
1256 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
1257 * #GST_BUFFER_OFFSET_NONE.
1258 * If @offset equals 0 and @size equals the total size of @buffer, the
1259 * duration and offset end fields are also copied. If not they will be set
1260 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
1264 * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
1268 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
1269 gsize offset, gsize size)
1273 g_return_val_if_fail (buffer != NULL, NULL);
1275 /* create the new buffer */
1276 copy = gst_buffer_new ();
1278 GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
1279 "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
1281 gst_buffer_copy_into (copy, buffer, flags, offset, size);
1287 _gst_buffer_arr_is_span_fast (GstMemory ** mem[], gsize len[], guint n,
1288 gsize * offset, GstMemory ** parent)
1290 GstMemory *mcur, *mprv;
1291 gboolean have_offset = FALSE;
1295 for (count = 0; count < n; count++) {
1302 for (i = 0; i < clen; i++) {
1308 /* check is memory is contiguous */
1309 if (!gst_memory_is_span (mprv, mcur, &offs))
1316 *parent = mprv->parent;
1327 _gst_buffer_arr_span (GstMemory ** mem[], gsize len[], guint n, gsize offset,
1328 gsize size, gboolean writable)
1330 GstMemory *span, *parent = NULL;
1334 && _gst_buffer_arr_is_span_fast (mem, len, n, &poffset, &parent)) {
1335 if (parent->flags & GST_MEMORY_FLAG_NO_SHARE) {
1336 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy for span %p", parent);
1337 span = gst_memory_copy (parent, offset + poffset, size);
1339 span = gst_memory_share (parent, offset + poffset, size);
1346 span = gst_allocator_alloc (NULL, 0, size, 0, size, 0);
1347 gst_memory_map (span, &dinfo, GST_MAP_WRITE);
1352 for (count = 0; count < n; count++) {
1354 gsize i, tocopy, clen;
1360 for (i = 0; i < clen && left > 0; i++) {
1361 gst_memory_map (cmem[i], &sinfo, GST_MAP_READ);
1362 tocopy = MIN (sinfo.size, left);
1363 if (tocopy > offset) {
1364 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
1365 "memcpy for span %p from memory %p", span, cmem[i]);
1366 memcpy (ptr, (guint8 *) sinfo.data + offset, tocopy - offset);
1373 gst_memory_unmap (cmem[i], &sinfo);
1376 gst_memory_unmap (span, &dinfo);
1382 * gst_buffer_is_span_fast:
1383 * @buf1: the first #GstBuffer.
1384 * @buf2: the second #GstBuffer.
1386 * Determines whether a gst_buffer_span() can be done without copying
1387 * the contents, that is, whether the data areas are contiguous sub-buffers of
1391 * Returns: TRUE if the buffers are contiguous,
1392 * FALSE if a copy would be required.
1395 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
1400 g_return_val_if_fail (GST_IS_BUFFER (buf1), FALSE);
1401 g_return_val_if_fail (GST_IS_BUFFER (buf2), FALSE);
1402 g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
1403 g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
1405 mem[0] = GST_BUFFER_MEM_ARRAY (buf1);
1406 len[0] = GST_BUFFER_MEM_LEN (buf1);
1407 mem[1] = GST_BUFFER_MEM_ARRAY (buf2);
1408 len[1] = GST_BUFFER_MEM_LEN (buf2);
1410 return _gst_buffer_arr_is_span_fast (mem, len, 2, NULL, NULL);
1415 * @buf1: the first source #GstBuffer to merge.
1416 * @offset: the offset in the first buffer from where the new
1417 * buffer should start.
1418 * @buf2: the second source #GstBuffer to merge.
1419 * @size: the total size of the new buffer.
1421 * Creates a new buffer that consists of part of buf1 and buf2.
1422 * Logically, buf1 and buf2 are concatenated into a single larger
1423 * buffer, and a new buffer is created at the given offset inside
1424 * this space, with a given length.
1426 * If the two source buffers are children of the same larger buffer,
1427 * and are contiguous, the new buffer will be a child of the shared
1428 * parent, and thus no copying is necessary. you can use
1429 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
1433 * Returns: (transfer full): the new #GstBuffer that spans the two source
1434 * buffers, or NULL if the arguments are invalid.
1437 gst_buffer_span (GstBuffer * buf1, gsize offset, GstBuffer * buf2, gsize size)
1442 gsize len[2], len1, len2;
1444 g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
1445 g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
1446 g_return_val_if_fail (buf1->mini_object.refcount > 0, NULL);
1447 g_return_val_if_fail (buf2->mini_object.refcount > 0, NULL);
1448 len1 = gst_buffer_get_size (buf1);
1449 len2 = gst_buffer_get_size (buf2);
1450 g_return_val_if_fail (len1 + len2 > offset, NULL);
1452 size = len1 + len2 - offset;
1454 g_return_val_if_fail (size <= len1 + len2 - offset, NULL);
1456 mem[0] = GST_BUFFER_MEM_ARRAY (buf1);
1457 len[0] = GST_BUFFER_MEM_LEN (buf1);
1458 mem[1] = GST_BUFFER_MEM_ARRAY (buf2);
1459 len[1] = GST_BUFFER_MEM_LEN (buf2);
1461 span = _gst_buffer_arr_span (mem, len, 2, offset, size, FALSE);
1463 newbuf = gst_buffer_new ();
1464 _memory_add (newbuf, -1, span);
1467 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
1469 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
1470 GST_BUFFER_PTS (newbuf) = GST_BUFFER_PTS (buf1);
1471 GST_BUFFER_DTS (newbuf) = GST_BUFFER_DTS (buf1);
1473 /* if we completely merged the two buffers (appended), we can
1474 * calculate the duration too. Also make sure we's not messing with
1475 * invalid DURATIONS */
1476 if (buf1->size + buf2->size == len) {
1477 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
1478 GST_BUFFER_DURATION_IS_VALID (buf2)) {
1480 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
1481 GST_BUFFER_DURATION (buf2);
1483 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
1484 /* add offset_end */
1485 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
1495 * gst_buffer_get_meta:
1496 * @buffer: a #GstBuffer
1497 * @api: the #GType of an API
1499 * Get the metadata for @api on buffer. When there is no such
1500 * metadata, NULL is returned.
1502 * Returns: the metadata for @api on @buffer.
1505 gst_buffer_get_meta (GstBuffer * buffer, GType api)
1508 GstMeta *result = NULL;
1510 g_return_val_if_fail (buffer != NULL, NULL);
1511 g_return_val_if_fail (api != 0, NULL);
1513 /* find GstMeta of the requested API */
1514 for (item = GST_BUFFER_META (buffer); item; item = item->next) {
1515 GstMeta *meta = &item->meta;
1516 if (meta->info->api == api) {
1525 * gst_buffer_add_meta:
1526 * @buffer: a #GstBuffer
1527 * @info: a #GstMetaInfo
1528 * @params: params for @info
1530 * Add metadata for @info to @buffer using the parameters in @params.
1532 * Returns: (transfer none): the metadata for the api in @info on @buffer.
1535 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
1539 GstMeta *result = NULL;
1542 g_return_val_if_fail (buffer != NULL, NULL);
1543 g_return_val_if_fail (info != NULL, NULL);
1545 /* create a new slice */
1546 size = ITEM_SIZE (info);
1547 item = g_slice_alloc (size);
1548 result = &item->meta;
1549 result->info = info;
1550 result->flags = GST_META_FLAG_NONE;
1552 GST_CAT_DEBUG (GST_CAT_BUFFER,
1553 "alloc metadata %p (%s) of size %" G_GSIZE_FORMAT, result,
1554 g_type_name (info->type), info->size);
1556 /* call the init_func when needed */
1557 if (info->init_func)
1558 if (!info->init_func (result, params, buffer))
1561 /* and add to the list of metadata */
1562 item->next = GST_BUFFER_META (buffer);
1563 GST_BUFFER_META (buffer) = item;
1569 g_slice_free1 (size, item);
1575 * gst_buffer_remove_meta:
1576 * @buffer: a #GstBuffer
1579 * Remove the metadata for @meta on @buffer.
1581 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
1582 * metadata was on @buffer.
1585 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
1587 GstMetaItem *walk, *prev;
1589 g_return_val_if_fail (buffer != NULL, FALSE);
1590 g_return_val_if_fail (meta != NULL, FALSE);
1592 /* find the metadata and delete */
1593 prev = GST_BUFFER_META (buffer);
1594 for (walk = prev; walk; walk = walk->next) {
1595 GstMeta *m = &walk->meta;
1597 const GstMetaInfo *info = meta->info;
1599 /* remove from list */
1600 if (GST_BUFFER_META (buffer) == walk)
1601 GST_BUFFER_META (buffer) = walk->next;
1603 prev->next = walk->next;
1604 /* call free_func if any */
1605 if (info->free_func)
1606 info->free_func (m, buffer);
1608 /* and free the slice */
1609 g_slice_free1 (ITEM_SIZE (info), walk);
1614 return walk != NULL;
1618 * gst_buffer_iterate_meta:
1619 * @buffer: a #GstBuffer
1620 * @state: an opaque state pointer
1622 * Retrieve the next #GstMeta after @current. If @state points
1623 * to %NULL, the first metadata is returned.
1625 * @state will be updated with an opage state pointer
1627 * Returns: The next #GstMeta or %NULL when there are no more items.
1630 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
1634 g_return_val_if_fail (buffer != NULL, NULL);
1635 g_return_val_if_fail (state != NULL, NULL);
1637 meta = (GstMetaItem **) state;
1639 /* state NULL, move to first item */
1640 *meta = GST_BUFFER_META (buffer);
1642 /* state !NULL, move to next item in list */
1643 *meta = (*meta)->next;
1646 return &(*meta)->meta;
1652 * gst_buffer_foreach_meta:
1653 * @buffer: a #GstBuffer
1654 * @func: (scope call): a #GstBufferForeachMetaFunc to call
1655 * @user_data: (closure): user data passed to @func
1657 * Call @func with @user_data for each meta in @buffer.
1659 * @func can modify the passed meta pointer or its contents. The return value
1660 * of @func define if this function returns or if the remaining metadata items
1661 * in the buffer should be skipped.
1664 gst_buffer_foreach_meta (GstBuffer * buffer, GstBufferForeachMetaFunc func,
1667 GstMetaItem *walk, *prev, *next;
1669 g_return_if_fail (buffer != NULL);
1670 g_return_if_fail (func != NULL);
1672 /* find the metadata and delete */
1673 prev = GST_BUFFER_META (buffer);
1674 for (walk = prev; walk; walk = next) {
1678 m = new = &walk->meta;
1681 res = func (buffer, &new, user_data);
1684 const GstMetaInfo *info = m->info;
1686 GST_CAT_DEBUG (GST_CAT_BUFFER, "remove metadata %p (%s)", m,
1687 g_type_name (info->type));
1689 /* remove from list */
1690 if (GST_BUFFER_META (buffer) == walk)
1691 GST_BUFFER_META (buffer) = next;
1695 /* call free_func if any */
1696 if (info->free_func)
1697 info->free_func (m, buffer);
1699 /* and free the slice */
1700 g_slice_free1 (ITEM_SIZE (info), walk);