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, NULL);
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_append(). Copying of memory will only be done when absolutely
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 typedef struct _GstMetaItem GstMetaItem;
128 #define ITEM_SIZE(info) ((info)->size + sizeof (GstMetaItem))
130 #define GST_BUFFER_MEM_MAX 16
132 #define GST_BUFFER_MEM_LEN(b) (((GstBufferImpl *)(b))->len)
133 #define GST_BUFFER_MEM_ARRAY(b) (((GstBufferImpl *)(b))->mem)
134 #define GST_BUFFER_MEM_PTR(b,i) (((GstBufferImpl *)(b))->mem[i])
135 #define GST_BUFFER_BUFMEM(b) (((GstBufferImpl *)(b))->bufmem)
136 #define GST_BUFFER_META(b) (((GstBufferImpl *)(b))->item)
142 /* the memory blocks */
144 GstMemory *mem[GST_BUFFER_MEM_MAX];
146 /* memory of the buffer when allocated from 1 chunk */
149 /* FIXME, make metadata allocation more efficient by using part of the
156 _is_span_fast (GstMemory ** mem[], gsize len[], guint n,
157 gsize * offset, GstMemory ** parent)
159 GstMemory *mcur, *mprv;
160 gboolean have_offset = FALSE;
164 for (count = 0; count < n; count++) {
171 for (i = 0; i < clen; i++) {
177 /* check if memory is contiguous */
178 if (!gst_memory_is_span (mprv, mcur, &offs))
185 *parent = mprv->parent;
196 _arr_span (GstMemory ** mem[], gsize len[], guint n, gsize size)
198 GstMemory *span, *parent = NULL;
201 if (_is_span_fast (mem, len, n, &poffset, &parent)) {
202 if (parent->flags & GST_MEMORY_FLAG_NO_SHARE) {
203 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy for span %p", parent);
204 span = gst_memory_copy (parent, poffset, size);
206 span = gst_memory_share (parent, poffset, size);
213 span = gst_allocator_alloc (NULL, size, NULL);
214 gst_memory_map (span, &dinfo, GST_MAP_WRITE);
219 for (count = 0; count < n; count++) {
221 gsize i, tocopy, clen;
227 for (i = 0; i < clen && left > 0; i++) {
228 gst_memory_map (cmem[i], &sinfo, GST_MAP_READ);
229 tocopy = MIN (sinfo.size, left);
230 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
231 "memcpy for span %p from memory %p", span, cmem[i]);
232 memcpy (ptr, (guint8 *) sinfo.data, tocopy);
235 gst_memory_unmap (cmem[i], &sinfo);
238 gst_memory_unmap (span, &dinfo);
244 _span_memory (GstBuffer * buffer)
246 GstMemory *span, **mem[1];
249 /* not enough room, span buffers */
250 mem[0] = GST_BUFFER_MEM_ARRAY (buffer);
251 len[0] = GST_BUFFER_MEM_LEN (buffer);
253 size = gst_buffer_get_size (buffer);
255 span = _arr_span (mem, len, 1, size);
261 _get_merged_memory (GstBuffer * buffer, gboolean * merged)
266 len = GST_BUFFER_MEM_LEN (buffer);
268 if (G_UNLIKELY (len == 0)) {
271 } else if (G_LIKELY (len == 1)) {
272 /* we can take the first one */
273 mem = GST_BUFFER_MEM_PTR (buffer, 0);
274 gst_memory_ref (mem);
277 /* we need to span memory */
278 mem = _span_memory (buffer);
285 _replace_all_memory (GstBuffer * buffer, GstMemory * mem)
289 len = GST_BUFFER_MEM_LEN (buffer);
291 if (G_LIKELY (len == 1 && GST_BUFFER_MEM_PTR (buffer, 0) == mem)) {
292 gst_memory_unref (mem);
296 GST_LOG ("buffer %p replace with memory %p", buffer, mem);
298 /* unref old memory */
299 for (i = 0; i < len; i++)
300 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
301 /* replace with single memory */
302 GST_BUFFER_MEM_PTR (buffer, 0) = mem;
303 GST_BUFFER_MEM_LEN (buffer) = 1;
307 _memory_add (GstBuffer * buffer, guint idx, GstMemory * mem)
309 guint i, len = GST_BUFFER_MEM_LEN (buffer);
311 if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) {
312 /* too many buffer, span them. */
313 /* FIXME, there is room for improvement here: We could only try to merge
314 * 2 buffers to make some room. If we can't efficiently merge 2 buffers we
315 * could try to only merge the two smallest buffers to avoid memcpy, etc. */
316 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "memory array overflow in buffer %p",
318 _replace_all_memory (buffer, _span_memory (buffer));
319 /* we now have 1 single spanned buffer */
326 for (i = len; i > idx; i--) {
327 /* move buffers to insert, FIXME, we need to insert first and then merge */
328 GST_BUFFER_MEM_PTR (buffer, i) = GST_BUFFER_MEM_PTR (buffer, i - 1);
330 /* and insert the new buffer */
331 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
332 GST_BUFFER_MEM_LEN (buffer) = len + 1;
335 GST_DEFINE_MINI_OBJECT_TYPE (GstBuffer, gst_buffer);
338 _priv_gst_buffer_initialize (void)
340 _gst_buffer_type = gst_buffer_get_type ();
344 * gst_buffer_copy_into:
345 * @dest: a destination #GstBuffer
346 * @src: a source #GstBuffer
347 * @flags: flags indicating what metadata fields should be copied.
348 * @offset: offset to copy from
349 * @size: total size to copy. If -1, all data is copied.
351 * Copies the information from @src into @dest.
353 * If @dest already contains memory and @flags contains GST_BUFFER_COPY_MEMORY,
354 * the memory from @src will be appended to @dest.
356 * @flags indicate which fields will be copied.
359 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
360 GstBufferCopyFlags flags, gsize offset, gsize size)
364 gboolean region = FALSE;
366 g_return_if_fail (dest != NULL);
367 g_return_if_fail (src != NULL);
369 /* nothing to copy if the buffers are the same */
370 if (G_UNLIKELY (dest == src))
373 g_return_if_fail (gst_buffer_is_writable (dest));
375 bufsize = gst_buffer_get_size (src);
376 g_return_if_fail (bufsize >= offset);
380 size = bufsize - offset;
383 g_return_if_fail (bufsize >= offset + size);
385 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p, offset %" G_GSIZE_FORMAT
386 "-%" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT, src, dest, offset, size,
389 if (flags & GST_BUFFER_COPY_FLAGS) {
391 GST_MINI_OBJECT_FLAGS (dest) = GST_MINI_OBJECT_FLAGS (src);
394 if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
396 GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src);
397 GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src);
398 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
399 if (size == bufsize) {
400 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
401 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
404 GST_BUFFER_PTS (dest) = GST_CLOCK_TIME_NONE;
405 GST_BUFFER_DTS (dest) = GST_CLOCK_TIME_NONE;
406 GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
407 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
408 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
412 if (flags & GST_BUFFER_COPY_MEMORY) {
414 gsize skip, left, len, i, bsize;
416 len = GST_BUFFER_MEM_LEN (src);
420 /* copy and make regions of the memory */
421 for (i = 0; i < len && left > 0; i++) {
422 mem = GST_BUFFER_MEM_PTR (src, i);
423 bsize = gst_memory_get_sizes (mem, NULL, NULL);
426 /* don't copy buffer */
431 tocopy = MIN (bsize - skip, left);
432 if (mem->flags & GST_MEMORY_FLAG_NO_SHARE) {
433 /* no share, always copy then */
434 mem = gst_memory_copy (mem, skip, tocopy);
436 } else if (tocopy < bsize) {
437 /* we need to clip something */
438 mem = gst_memory_share (mem, skip, tocopy);
441 mem = gst_memory_ref (mem);
443 _memory_add (dest, -1, mem);
447 if (flags & GST_BUFFER_COPY_MERGE) {
448 _replace_all_memory (dest, _span_memory (dest));
452 if (flags & GST_BUFFER_COPY_META) {
453 for (walk = GST_BUFFER_META (src); walk; walk = walk->next) {
454 GstMeta *meta = &walk->meta;
455 const GstMetaInfo *info = meta->info;
457 if (info->transform_func) {
458 GstMetaTransformCopy copy_data;
460 copy_data.region = region;
461 copy_data.offset = offset;
462 copy_data.size = size;
464 info->transform_func (dest, meta, src,
465 _gst_meta_transform_copy, ©_data);
472 _gst_buffer_copy (GstBuffer * buffer)
476 g_return_val_if_fail (buffer != NULL, NULL);
478 /* create a fresh new buffer */
479 copy = gst_buffer_new ();
481 /* we simply copy everything from our parent */
482 gst_buffer_copy_into (copy, buffer, GST_BUFFER_COPY_ALL, 0, -1);
487 /* the default dispose function revives the buffer and returns it to the
488 * pool when there is a pool */
490 _gst_buffer_dispose (GstBuffer * buffer)
494 /* no pool, do free */
495 if ((pool = buffer->pool) == NULL)
498 /* keep the buffer alive */
499 gst_buffer_ref (buffer);
500 /* return the buffer to the pool */
501 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
502 gst_buffer_pool_release_buffer (pool, buffer);
508 _gst_buffer_free (GstBuffer * buffer)
510 GstMetaItem *walk, *next;
514 g_return_if_fail (buffer != NULL);
516 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
519 for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
520 GstMeta *meta = &walk->meta;
521 const GstMetaInfo *info = meta->info;
523 /* call free_func if any */
525 info->free_func (meta, buffer);
528 /* and free the slice */
529 g_slice_free1 (ITEM_SIZE (info), walk);
532 /* get the size, when unreffing the memory, we could also unref the buffer
534 msize = GST_MINI_OBJECT_SIZE (buffer);
536 /* free our memory */
537 len = GST_BUFFER_MEM_LEN (buffer);
538 for (i = 0; i < len; i++)
539 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
541 /* we set msize to 0 when the buffer is part of the memory block */
543 g_slice_free1 (msize, buffer);
545 gst_memory_unref (GST_BUFFER_BUFMEM (buffer));
549 gst_buffer_init (GstBufferImpl * buffer, gsize size)
551 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), _gst_buffer_type, size);
553 buffer->buffer.mini_object.copy =
554 (GstMiniObjectCopyFunction) _gst_buffer_copy;
555 buffer->buffer.mini_object.dispose =
556 (GstMiniObjectDisposeFunction) _gst_buffer_dispose;
557 buffer->buffer.mini_object.free =
558 (GstMiniObjectFreeFunction) _gst_buffer_free;
560 GST_BUFFER (buffer)->pool = NULL;
561 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
562 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
563 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
564 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
565 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
567 GST_BUFFER_MEM_LEN (buffer) = 0;
568 GST_BUFFER_META (buffer) = NULL;
574 * Creates a newly allocated buffer without any data.
578 * Returns: (transfer full): the new #GstBuffer.
581 gst_buffer_new (void)
583 GstBufferImpl *newbuf;
585 newbuf = g_slice_new (GstBufferImpl);
586 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
588 gst_buffer_init (newbuf, sizeof (GstBufferImpl));
590 return GST_BUFFER_CAST (newbuf);
594 * gst_buffer_new_allocate:
595 * @allocator: (transfer none) (allow-none): the #GstAllocator to use, or NULL to use the
597 * @size: the size in bytes of the new buffer's data.
598 * @params: (transfer none) (allow-none): optional parameters
600 * Tries to create a newly allocated buffer with data of the given size and
601 * extra parameters from @allocator. If the requested amount of memory can't be
602 * allocated, NULL will be returned. The allocated buffer memory is not cleared.
604 * When @allocator is NULL, the default memory allocator will be used.
606 * Note that when @size == 0, the buffer will not have memory associated with it.
610 * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
614 gst_buffer_new_allocate (GstAllocator * allocator, gsize size,
615 GstAllocationParams * params)
626 mem = gst_allocator_alloc (allocator, size, params);
627 if (G_UNLIKELY (mem == NULL))
633 newbuf = gst_buffer_new ();
636 _memory_add (newbuf, -1, mem);
638 GST_CAT_LOG (GST_CAT_BUFFER,
639 "new buffer %p of size %" G_GSIZE_FORMAT " from allocator %p", newbuf,
644 asize = sizeof (GstBufferImpl) + size;
645 data = g_slice_alloc (asize);
646 if (G_UNLIKELY (data == NULL))
649 newbuf = GST_BUFFER_CAST (data);
651 gst_buffer_init ((GstBufferImpl *) data, asize);
653 mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL,
655 _memory_add (newbuf, -1, mem);
660 /* allocate memory and buffer, it might be interesting to do this but there
661 * are many complications. We need to keep the memory mapped to access the
662 * buffer fields and the memory for the buffer might be just very slow. We
663 * also need to do some more magic to get the alignment right. */
664 asize = sizeof (GstBufferImpl) + size;
665 mem = gst_allocator_alloc (allocator, asize, align);
666 if (G_UNLIKELY (mem == NULL))
669 /* map the data part and init the buffer in it, set the buffer size to 0 so
670 * that a finalize won't free the buffer */
671 data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
672 gst_buffer_init ((GstBufferImpl *) data, 0);
673 gst_memory_unmap (mem);
675 /* strip off the buffer */
676 gst_memory_resize (mem, sizeof (GstBufferImpl), size);
678 newbuf = GST_BUFFER_CAST (data);
679 GST_BUFFER_BUFMEM (newbuf) = mem;
682 _memory_add (newbuf, -1, gst_memory_ref (mem));
690 GST_CAT_WARNING (GST_CAT_BUFFER,
691 "failed to allocate %" G_GSIZE_FORMAT " bytes", size);
697 * gst_buffer_new_wrapped_full:
698 * @flags: #GstMemoryFlags
699 * @data: data to wrap
700 * @maxsize: allocated size of @data
701 * @offset: offset in @data
702 * @size: size of valid data
703 * @user_data: user_data
704 * @notify: called with @user_data when the memory is freed
706 * Allocate a new buffer that wraps the given memory. @data must point to
707 * @maxsize of memory, the wrapped buffer will have the region from @offset and
710 * When the buffer is destroyed, @notify will be called with @user_data.
712 * The prefix/padding must be filled with 0 if @flags contains
713 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
715 * Returns: (transfer full): a new #GstBuffer
718 gst_buffer_new_wrapped_full (GstMemoryFlags flags, gpointer data,
719 gsize maxsize, gsize offset, gsize size, gpointer user_data,
720 GDestroyNotify notify)
724 newbuf = gst_buffer_new ();
725 gst_buffer_append_memory (newbuf,
726 gst_memory_new_wrapped (flags, data, maxsize, offset, size,
733 * gst_buffer_new_wrapped:
734 * @data: data to wrap
735 * @size: allocated size of @data
737 * Creates a new buffer that wraps the given @data. The memory will be freed
738 * with g_free and will be marked writable.
742 * Returns: (transfer full): a new #GstBuffer
745 gst_buffer_new_wrapped (gpointer data, gsize size)
747 return gst_buffer_new_wrapped_full (0, data, size, 0, size, data, g_free);
751 * gst_buffer_n_memory:
752 * @buffer: a #GstBuffer.
754 * Get the amount of memory blocks that this buffer has.
756 * Returns: (transfer full): the amount of memory block in this buffer.
759 gst_buffer_n_memory (GstBuffer * buffer)
761 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
763 return GST_BUFFER_MEM_LEN (buffer);
767 * gst_buffer_take_memory:
768 * @buffer: a #GstBuffer.
769 * @idx: the index to add the memory at, or -1 to append it to the end
770 * @mem: (transfer full): a #GstMemory.
772 * Add the memory block @mem to @buffer at @idx. This function takes ownership
773 * of @mem and thus doesn't increase its refcount.
776 gst_buffer_take_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
778 g_return_if_fail (GST_IS_BUFFER (buffer));
779 g_return_if_fail (gst_buffer_is_writable (buffer));
780 g_return_if_fail (mem != NULL);
781 g_return_if_fail (idx == -1 ||
782 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)));
784 _memory_add (buffer, idx, mem);
788 _get_mapped (GstBuffer * buffer, guint idx, GstMapInfo * info,
791 GstMemory *mem, *mapped;
793 mem = GST_BUFFER_MEM_PTR (buffer, idx);
795 mapped = gst_memory_make_mapped (mem, info, flags);
800 GST_BUFFER_MEM_PTR (buffer, idx) = mapped;
801 gst_memory_unref (mem);
808 * gst_buffer_get_memory:
809 * @buffer: a #GstBuffer.
812 * Get the memory block in @buffer at @idx. If @idx is -1, all memory is merged
813 * into one large #GstMemory object that is then returned.
815 * Returns: (transfer full): a #GstMemory at @idx. Use gst_memory_unref () after usage.
818 gst_buffer_get_memory (GstBuffer * buffer, gint idx)
823 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
824 g_return_val_if_fail (idx == -1 ||
825 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)), NULL);
828 mem = _get_merged_memory (buffer, &merged);
829 } else if ((mem = GST_BUFFER_MEM_PTR (buffer, idx))) {
830 gst_memory_ref (mem);
836 * gst_buffer_replace_memory:
837 * @buffer: a #GstBuffer.
839 * @mem: (transfer full): a #GstMemory
841 * Replaces the memory block in @buffer at @idx with @mem. If @idx is -1, all
842 * memory will be removed and replaced with @mem.
844 * @buffer should be writable.
847 gst_buffer_replace_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
849 g_return_if_fail (GST_IS_BUFFER (buffer));
850 g_return_if_fail (gst_buffer_is_writable (buffer));
851 g_return_if_fail (idx == -1 ||
852 (idx >= 0 && idx < GST_BUFFER_MEM_LEN (buffer)));
855 _replace_all_memory (buffer, mem);
859 if ((old = GST_BUFFER_MEM_PTR (buffer, idx)))
860 gst_memory_unref (old);
861 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
866 * gst_buffer_remove_memory_range:
867 * @buffer: a #GstBuffer.
871 * Remove @len memory blocks in @buffer starting from @idx.
873 * @length can be -1, in which case all memory starting from @idx is removed.
876 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, gint length)
880 g_return_if_fail (GST_IS_BUFFER (buffer));
881 g_return_if_fail (gst_buffer_is_writable (buffer));
883 len = GST_BUFFER_MEM_LEN (buffer);
884 g_return_if_fail ((length == -1 && idx < len) || length + idx < len);
890 for (i = idx; i < end; i++)
891 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
894 g_memmove (&GST_BUFFER_MEM_PTR (buffer, idx),
895 &GST_BUFFER_MEM_PTR (buffer, end), (len - end) * sizeof (gpointer));
897 GST_BUFFER_MEM_LEN (buffer) = len - length;
901 * gst_buffer_get_sizes:
902 * @buffer: a #GstBuffer.
903 * @offset: a pointer to the offset
904 * @maxsize: a pointer to the maxsize
906 * Get the total size of all memory blocks in @buffer.
908 * When not %NULL, @offset will contain the offset of the data in the first
909 * memory block in @buffer and @maxsize will contain the sum of the size
910 * and @offset and the amount of extra padding on the last memory block.
911 * @offset and @maxsize can be used to resize the buffer with
912 * gst_buffer_resize().
914 * Returns: the total size of the memory in @buffer.
917 gst_buffer_get_sizes (GstBuffer * buffer, gsize * offset, gsize * maxsize)
923 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
925 len = GST_BUFFER_MEM_LEN (buffer);
927 if (G_LIKELY (len == 1)) {
929 mem = GST_BUFFER_MEM_PTR (buffer, 0);
930 size = gst_memory_get_sizes (mem, offset, maxsize);
935 size = offs = extra = 0;
936 for (i = 0; i < len; i++) {
939 mem = GST_BUFFER_MEM_PTR (buffer, i);
940 s = gst_memory_get_sizes (mem, &o, &ms);
944 /* first size, take accumulated data before as the offset */
948 /* save the amount of data after this block */
949 extra = ms - (o + s);
951 /* empty block, add as extra */
958 *maxsize = offs + size + extra;
965 * @buffer: a #GstBuffer.
966 * @offset: the offset adjustement
967 * @size: the new size or -1 to just adjust the offset
969 * Set the total size of the buffer
972 gst_buffer_resize (GstBuffer * buffer, gssize offset, gssize size)
976 gsize bsize, bufsize, bufoffs, bufmax;
979 g_return_if_fail (gst_buffer_is_writable (buffer));
980 g_return_if_fail (size >= -1);
982 bufsize = gst_buffer_get_sizes (buffer, &bufoffs, &bufmax);
984 GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSSIZE_FORMAT
985 " size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%"
986 G_GSIZE_FORMAT, buffer, offset, size, bufsize, bufoffs, bufmax);
988 /* we can't go back further than the current offset or past the end of the
990 g_return_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
991 && bufoffs + offset <= bufmax));
993 g_return_if_fail (bufsize >= offset);
994 size = bufsize - offset;
996 g_return_if_fail (bufmax >= bufoffs + offset + size);
999 if (offset == 0 && size == bufsize)
1002 len = GST_BUFFER_MEM_LEN (buffer);
1005 for (i = 0; i < len; i++) {
1008 mem = GST_BUFFER_MEM_PTR (buffer, i);
1009 bsize = gst_memory_get_sizes (mem, NULL, NULL);
1012 /* last buffer always gets resized to the remaining size */
1015 /* shrink buffers before the offset */
1016 else if ((gssize) bsize <= offset) {
1018 noffs = offset - bsize;
1021 /* clip other buffers */
1023 left = MIN (bsize - offset, size);
1025 if (offset != 0 || left != bsize) {
1026 if (gst_memory_is_exclusive (mem)) {
1027 gst_memory_resize (mem, offset, left);
1031 if (mem->flags & GST_MEMORY_FLAG_NO_SHARE)
1032 tmp = gst_memory_copy (mem, offset, left);
1034 tmp = gst_memory_share (mem, offset, left);
1036 gst_memory_unref (mem);
1043 GST_BUFFER_MEM_PTR (buffer, i) = mem;
1049 * @buffer: a #GstBuffer.
1050 * @info: (out): info about the mapping
1051 * @flags: flags for the mapping
1053 * This function fills @info with a pointer to the merged memory in @buffer.
1054 * @flags describe the desired access of the memory. When @flags is
1055 * #GST_MAP_WRITE, @buffer should be writable (as returned from
1056 * gst_buffer_is_writable()).
1058 * When @buffer is writable but the memory isn't, a writable copy will
1059 * automatically be created and returned. The readonly copy of the buffer memory
1060 * will then also be replaced with this writable copy.
1062 * When the buffer contains multiple memory blocks, the returned pointer will be
1063 * a concatenation of the memory blocks.
1065 * The memory in @info should be unmapped with gst_buffer_unmap() after usage.
1067 * Returns: (transfer full): %TRUE if the map succeeded and @info contains valid
1071 gst_buffer_map (GstBuffer * buffer, GstMapInfo * info, GstMapFlags flags)
1073 GstMemory *mem, *nmem;
1074 gboolean write, writable, merged;
1076 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1077 g_return_val_if_fail (info != NULL, FALSE);
1079 write = (flags & GST_MAP_WRITE) != 0;
1080 writable = gst_buffer_is_writable (buffer);
1082 /* check if we can write when asked for write access */
1083 if (G_UNLIKELY (write && !writable))
1086 mem = _get_merged_memory (buffer, &merged);
1087 if (G_UNLIKELY (mem == NULL))
1090 /* now try to map */
1091 nmem = gst_memory_make_mapped (mem, info, flags);
1092 if (G_UNLIKELY (nmem == NULL))
1095 /* if we merged or when the map returned a different memory, we try to replace
1096 * the memory in the buffer */
1097 if (G_UNLIKELY (merged || nmem != mem)) {
1098 /* if the buffer is writable, replace the memory */
1100 _replace_all_memory (buffer, gst_memory_ref (nmem));
1102 if (GST_BUFFER_MEM_LEN (buffer) > 1) {
1103 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
1104 "temporary mapping for memory %p in buffer %p", nmem, buffer);
1113 GST_WARNING_OBJECT (buffer, "write map requested on non-writable buffer");
1114 g_critical ("write map requested on non-writable buffer");
1119 /* empty buffer, we need to return NULL */
1120 GST_DEBUG_OBJECT (buffer, "can't get buffer memory");
1121 info->memory = NULL;
1129 GST_DEBUG_OBJECT (buffer, "cannot map memory");
1136 * @buffer: a #GstBuffer.
1137 * @info: a #GstMapInfo
1139 * Release the memory previously mapped with gst_buffer_map().
1142 gst_buffer_unmap (GstBuffer * buffer, GstMapInfo * info)
1144 g_return_if_fail (GST_IS_BUFFER (buffer));
1145 g_return_if_fail (info != NULL);
1147 /* we need to check for NULL, it is possible that we tried to map a buffer
1148 * without memory and we should be able to unmap that fine */
1149 if (G_LIKELY (info->memory)) {
1150 gst_memory_unmap (info->memory, info);
1151 gst_memory_unref (info->memory);
1157 * @buffer: a #GstBuffer.
1158 * @offset: the offset to fill
1159 * @src: the source address
1160 * @size: the size to fill
1162 * Copy @size bytes from @src to @buffer at @offset.
1164 * Returns: The amount of bytes copied. This value can be lower than @size
1165 * when @buffer did not contain enough data.
1168 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
1172 const guint8 *ptr = src;
1174 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1175 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1176 g_return_val_if_fail (src != NULL, 0);
1178 len = GST_BUFFER_MEM_LEN (buffer);
1181 for (i = 0; i < len && left > 0; i++) {
1186 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1187 if (info.size > offset) {
1188 /* we have enough */
1189 tocopy = MIN (info.size - offset, left);
1190 memcpy ((guint8 *) info.data + offset, ptr, tocopy);
1195 /* offset past buffer, skip */
1196 offset -= info.size;
1198 gst_memory_unmap (mem, &info);
1204 * gst_buffer_extract:
1205 * @buffer: a #GstBuffer.
1206 * @offset: the offset to extract
1207 * @dest: the destination address
1208 * @size: the size to extract
1210 * Copy @size bytes starting from @offset in @buffer to @dest.
1212 * Returns: The amount of bytes extracted. This value can be lower than @size
1213 * when @buffer did not contain enough data.
1216 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
1221 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1222 g_return_val_if_fail (dest != NULL, 0);
1224 len = GST_BUFFER_MEM_LEN (buffer);
1227 for (i = 0; i < len && left > 0; i++) {
1232 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1233 if (info.size > offset) {
1234 /* we have enough */
1235 tocopy = MIN (info.size - offset, left);
1236 memcpy (ptr, (guint8 *) info.data + offset, tocopy);
1241 /* offset past buffer, skip */
1242 offset -= info.size;
1244 gst_memory_unmap (mem, &info);
1250 * gst_buffer_memcmp:
1251 * @buffer: a #GstBuffer.
1252 * @offset: the offset in @buffer
1253 * @mem: the memory to compare
1254 * @size: the size to compare
1256 * Compare @size bytes starting from @offset in @buffer with the memory in @mem.
1258 * Returns: 0 if the memory is equal.
1261 gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem,
1265 const guint8 *ptr = mem;
1268 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1269 g_return_val_if_fail (mem != NULL, 0);
1271 len = GST_BUFFER_MEM_LEN (buffer);
1273 for (i = 0; i < len && size > 0 && res == 0; i++) {
1278 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1279 if (info.size > offset) {
1280 /* we have enough */
1281 tocmp = MIN (info.size - offset, size);
1282 res = memcmp (ptr, (guint8 *) info.data + offset, tocmp);
1287 /* offset past buffer, skip */
1288 offset -= info.size;
1290 gst_memory_unmap (mem, &info);
1296 * gst_buffer_memset:
1297 * @buffer: a #GstBuffer.
1298 * @offset: the offset in @buffer
1299 * @val: the value to set
1300 * @size: the size to set
1302 * Fill @buf with @size bytes with @val starting from @offset.
1304 * Returns: The amount of bytes filled. This value can be lower than @size
1305 * when @buffer did not contain enough data.
1308 gst_buffer_memset (GstBuffer * buffer, gsize offset, guint8 val, gsize size)
1312 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1313 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1315 len = GST_BUFFER_MEM_LEN (buffer);
1318 for (i = 0; i < len && left > 0; i++) {
1323 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1324 if (info.size > offset) {
1325 /* we have enough */
1326 toset = MIN (info.size - offset, left);
1327 memset ((guint8 *) info.data + offset, val, toset);
1331 /* offset past buffer, skip */
1332 offset -= info.size;
1334 gst_memory_unmap (mem, &info);
1340 * gst_buffer_copy_region:
1341 * @parent: a #GstBuffer.
1342 * @flags: the #GstBufferCopyFlags
1343 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
1345 * @size: the size of the new #GstBuffer sub-buffer, in bytes.
1347 * Creates a sub-buffer from @parent at @offset and @size.
1348 * This sub-buffer uses the actual memory space of the parent buffer.
1349 * This function will copy the offset and timestamp fields when the
1350 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
1351 * #GST_BUFFER_OFFSET_NONE.
1352 * If @offset equals 0 and @size equals the total size of @buffer, the
1353 * duration and offset end fields are also copied. If not they will be set
1354 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
1358 * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
1362 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
1363 gsize offset, gsize size)
1367 g_return_val_if_fail (buffer != NULL, NULL);
1369 /* create the new buffer */
1370 copy = gst_buffer_new ();
1372 GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
1373 "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
1375 gst_buffer_copy_into (copy, buffer, flags, offset, size);
1381 * gst_buffer_append:
1382 * @buf1: (transfer full): the first source #GstBuffer to append.
1383 * @buf2: (transfer full): the second source #GstBuffer to append.
1385 * Append all the memory from @buf2 to @buf1. The result buffer will contain a
1386 * concatenation of the memory of @buf1 and @buf2.
1388 * Returns: (transfer full): the new #GstBuffer that contains the memory
1389 * of the two source buffers.
1392 gst_buffer_append (GstBuffer * buf1, GstBuffer * buf2)
1396 g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
1397 g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
1399 buf1 = gst_buffer_make_writable (buf1);
1400 buf2 = gst_buffer_make_writable (buf2);
1402 len = GST_BUFFER_MEM_LEN (buf2);
1403 for (i = 0; i < len; i++) {
1406 mem = GST_BUFFER_MEM_PTR (buf2, i);
1407 GST_BUFFER_MEM_PTR (buf2, i) = NULL;
1408 _memory_add (buf1, -1, mem);
1411 /* we can calculate the duration too. Also make sure we're not messing
1412 * with invalid DURATIONS */
1413 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
1414 GST_BUFFER_DURATION_IS_VALID (buf2)) {
1416 GST_BUFFER_DURATION (buf1) += GST_BUFFER_DURATION (buf2);
1418 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
1419 /* set offset_end */
1420 GST_BUFFER_OFFSET_END (buf1) = GST_BUFFER_OFFSET_END (buf2);
1423 GST_BUFFER_MEM_LEN (buf2) = 0;
1424 gst_buffer_unref (buf2);
1430 * gst_buffer_get_meta:
1431 * @buffer: a #GstBuffer
1432 * @api: the #GType of an API
1434 * Get the metadata for @api on buffer. When there is no such
1435 * metadata, NULL is returned.
1437 * Returns: the metadata for @api on @buffer.
1440 gst_buffer_get_meta (GstBuffer * buffer, GType api)
1443 GstMeta *result = NULL;
1445 g_return_val_if_fail (buffer != NULL, NULL);
1446 g_return_val_if_fail (api != 0, NULL);
1448 /* find GstMeta of the requested API */
1449 for (item = GST_BUFFER_META (buffer); item; item = item->next) {
1450 GstMeta *meta = &item->meta;
1451 if (meta->info->api == api) {
1460 * gst_buffer_add_meta:
1461 * @buffer: a #GstBuffer
1462 * @info: a #GstMetaInfo
1463 * @params: params for @info
1465 * Add metadata for @info to @buffer using the parameters in @params.
1467 * Returns: (transfer none): the metadata for the api in @info on @buffer.
1470 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
1474 GstMeta *result = NULL;
1477 g_return_val_if_fail (buffer != NULL, NULL);
1478 g_return_val_if_fail (info != NULL, NULL);
1479 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
1481 /* create a new slice */
1482 size = ITEM_SIZE (info);
1483 item = g_slice_alloc (size);
1484 result = &item->meta;
1485 result->info = info;
1486 result->flags = GST_META_FLAG_NONE;
1488 GST_CAT_DEBUG (GST_CAT_BUFFER,
1489 "alloc metadata %p (%s) of size %" G_GSIZE_FORMAT, result,
1490 g_type_name (info->type), info->size);
1492 /* call the init_func when needed */
1493 if (info->init_func)
1494 if (!info->init_func (result, params, buffer))
1497 /* and add to the list of metadata */
1498 item->next = GST_BUFFER_META (buffer);
1499 GST_BUFFER_META (buffer) = item;
1505 g_slice_free1 (size, item);
1511 * gst_buffer_remove_meta:
1512 * @buffer: a #GstBuffer
1515 * Remove the metadata for @meta on @buffer.
1517 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
1518 * metadata was on @buffer.
1521 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
1523 GstMetaItem *walk, *prev;
1525 g_return_val_if_fail (buffer != NULL, FALSE);
1526 g_return_val_if_fail (meta != NULL, FALSE);
1527 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
1529 /* find the metadata and delete */
1530 prev = GST_BUFFER_META (buffer);
1531 for (walk = prev; walk; walk = walk->next) {
1532 GstMeta *m = &walk->meta;
1534 const GstMetaInfo *info = meta->info;
1536 /* remove from list */
1537 if (GST_BUFFER_META (buffer) == walk)
1538 GST_BUFFER_META (buffer) = walk->next;
1540 prev->next = walk->next;
1541 /* call free_func if any */
1542 if (info->free_func)
1543 info->free_func (m, buffer);
1545 /* and free the slice */
1546 g_slice_free1 (ITEM_SIZE (info), walk);
1551 return walk != NULL;
1555 * gst_buffer_iterate_meta:
1556 * @buffer: a #GstBuffer
1557 * @state: an opaque state pointer
1559 * Retrieve the next #GstMeta after @current. If @state points
1560 * to %NULL, the first metadata is returned.
1562 * @state will be updated with an opage state pointer
1564 * Returns: The next #GstMeta or %NULL when there are no more items.
1567 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
1571 g_return_val_if_fail (buffer != NULL, NULL);
1572 g_return_val_if_fail (state != NULL, NULL);
1574 meta = (GstMetaItem **) state;
1576 /* state NULL, move to first item */
1577 *meta = GST_BUFFER_META (buffer);
1579 /* state !NULL, move to next item in list */
1580 *meta = (*meta)->next;
1583 return &(*meta)->meta;
1589 * gst_buffer_foreach_meta:
1590 * @buffer: a #GstBuffer
1591 * @func: (scope call): a #GstBufferForeachMetaFunc to call
1592 * @user_data: (closure): user data passed to @func
1594 * Call @func with @user_data for each meta in @buffer.
1596 * @func can modify the passed meta pointer or its contents. The return value
1597 * of @func define if this function returns or if the remaining metadata items
1598 * in the buffer should be skipped.
1601 gst_buffer_foreach_meta (GstBuffer * buffer, GstBufferForeachMetaFunc func,
1604 GstMetaItem *walk, *prev, *next;
1606 g_return_if_fail (buffer != NULL);
1607 g_return_if_fail (func != NULL);
1609 /* find the metadata and delete */
1610 prev = GST_BUFFER_META (buffer);
1611 for (walk = prev; walk; walk = next) {
1615 m = new = &walk->meta;
1618 res = func (buffer, &new, user_data);
1621 const GstMetaInfo *info = m->info;
1623 GST_CAT_DEBUG (GST_CAT_BUFFER, "remove metadata %p (%s)", m,
1624 g_type_name (info->type));
1626 g_return_if_fail (gst_buffer_is_writable (buffer));
1628 /* remove from list */
1629 if (GST_BUFFER_META (buffer) == walk)
1630 GST_BUFFER_META (buffer) = next;
1634 /* call free_func if any */
1635 if (info->free_func)
1636 info->free_func (m, buffer);
1638 /* and free the slice */
1639 g_slice_free1 (ITEM_SIZE (info), walk);