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_take_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 "gstminiobject.h"
127 #include "gstversion.h"
129 GType _gst_buffer_type = 0;
131 typedef struct _GstMetaItem GstMetaItem;
138 #define ITEM_SIZE(info) ((info)->size + sizeof (GstMetaItem))
140 #define GST_BUFFER_MEM_MAX 16
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)
152 /* the memory blocks */
154 GstMemory *mem[GST_BUFFER_MEM_MAX];
156 /* memory of the buffer when allocated from 1 chunk */
159 /* FIXME, make metadata allocation more efficient by using part of the
166 _is_span (GstMemory ** mem, gsize len, gsize * poffset, GstMemory ** parent)
168 GstMemory *mcur, *mprv;
169 gboolean have_offset = FALSE;
174 for (i = 0; i < len; i++) {
182 /* check if memory is contiguous */
183 if (!gst_memory_is_span (mprv, mcur, &poffs))
190 *parent = mprv->parent;
200 _get_merged_memory (GstBuffer * buffer, gboolean * merged)
202 GstMemory **mem, *result;
205 mem = GST_BUFFER_MEM_ARRAY (buffer);
206 len = GST_BUFFER_MEM_LEN (buffer);
208 if (G_UNLIKELY (len == 0)) {
211 } else if (G_LIKELY (len == 1)) {
212 result = gst_memory_ref (mem[0]);
215 GstMemory *parent = NULL;
218 size = gst_buffer_get_size (buffer);
220 if (G_UNLIKELY (_is_span (mem, len, &poffset, &parent))) {
222 if (parent->flags & GST_MEMORY_FLAG_NO_SHARE) {
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 = 0; i < len && 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 for merge %p from memory %p", result, mem[i]);
244 memcpy (ptr, (guint8 *) sinfo.data, tocopy);
247 gst_memory_unmap (mem[i], &sinfo);
249 gst_memory_unmap (result, &dinfo);
257 _replace_all_memory (GstBuffer * buffer, GstMemory * mem)
261 GST_LOG ("buffer %p replace with memory %p", buffer, mem);
263 len = GST_BUFFER_MEM_LEN (buffer);
265 /* unref old memory */
266 for (i = 0; i < len; i++)
267 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
268 /* replace with single memory */
269 GST_BUFFER_MEM_PTR (buffer, 0) = mem;
270 GST_BUFFER_MEM_LEN (buffer) = 1;
274 _memory_add (GstBuffer * buffer, guint idx, GstMemory * mem)
276 guint i, len = GST_BUFFER_MEM_LEN (buffer);
278 if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) {
280 /* too many buffer, span them. */
281 /* FIXME, there is room for improvement here: We could only try to merge
282 * 2 buffers to make some room. If we can't efficiently merge 2 buffers we
283 * could try to only merge the two smallest buffers to avoid memcpy, etc. */
284 GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "memory array overflow in buffer %p",
286 _replace_all_memory (buffer, _get_merged_memory (buffer, &merged));
287 /* we now have 1 single spanned buffer */
294 for (i = len; i > idx; i--) {
295 /* move buffers to insert, FIXME, we need to insert first and then merge */
296 GST_BUFFER_MEM_PTR (buffer, i) = GST_BUFFER_MEM_PTR (buffer, i - 1);
298 /* and insert the new buffer */
299 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
300 GST_BUFFER_MEM_LEN (buffer) = len + 1;
303 GST_DEFINE_MINI_OBJECT_TYPE (GstBuffer, gst_buffer);
306 _priv_gst_buffer_initialize (void)
308 _gst_buffer_type = gst_buffer_get_type ();
312 * gst_buffer_copy_into:
313 * @dest: a destination #GstBuffer
314 * @src: a source #GstBuffer
315 * @flags: flags indicating what metadata fields should be copied.
316 * @offset: offset to copy from
317 * @size: total size to copy. If -1, all data is copied.
319 * Copies the information from @src into @dest.
321 * If @dest already contains memory and @flags contains GST_BUFFER_COPY_MEMORY,
322 * the memory from @src will be appended to @dest.
324 * @flags indicate which fields will be copied.
327 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
328 GstBufferCopyFlags flags, gsize offset, gsize size)
332 gboolean region = FALSE;
334 g_return_if_fail (dest != NULL);
335 g_return_if_fail (src != NULL);
337 /* nothing to copy if the buffers are the same */
338 if (G_UNLIKELY (dest == src))
341 g_return_if_fail (gst_buffer_is_writable (dest));
343 bufsize = gst_buffer_get_size (src);
344 g_return_if_fail (bufsize >= offset);
348 size = bufsize - offset;
351 g_return_if_fail (bufsize >= offset + size);
353 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p, offset %" G_GSIZE_FORMAT
354 "-%" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT, src, dest, offset, size,
357 if (flags & GST_BUFFER_COPY_FLAGS) {
359 GST_MINI_OBJECT_FLAGS (dest) = GST_MINI_OBJECT_FLAGS (src);
362 if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
364 GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src);
365 GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src);
366 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
367 if (size == bufsize) {
368 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
369 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
372 GST_BUFFER_PTS (dest) = GST_CLOCK_TIME_NONE;
373 GST_BUFFER_DTS (dest) = GST_CLOCK_TIME_NONE;
374 GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
375 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
376 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
380 if (flags & GST_BUFFER_COPY_MEMORY) {
382 gsize skip, left, len, i, bsize;
384 len = GST_BUFFER_MEM_LEN (src);
388 /* copy and make regions of the memory */
389 for (i = 0; i < len && left > 0; i++) {
390 mem = GST_BUFFER_MEM_PTR (src, i);
391 bsize = gst_memory_get_sizes (mem, NULL, NULL);
394 /* don't copy buffer */
399 tocopy = MIN (bsize - skip, left);
400 if (mem->flags & GST_MEMORY_FLAG_NO_SHARE) {
401 /* no share, always copy then */
402 mem = gst_memory_copy (mem, skip, tocopy);
404 } else if (tocopy < bsize) {
405 /* we need to clip something */
406 mem = gst_memory_share (mem, skip, tocopy);
409 mem = gst_memory_ref (mem);
411 _memory_add (dest, -1, mem);
415 if (flags & GST_BUFFER_COPY_MERGE) {
417 _replace_all_memory (dest, _get_merged_memory (dest, &merged));
421 if (flags & GST_BUFFER_COPY_META) {
422 for (walk = GST_BUFFER_META (src); walk; walk = walk->next) {
423 GstMeta *meta = &walk->meta;
424 const GstMetaInfo *info = meta->info;
426 if (info->transform_func) {
427 GstMetaTransformCopy copy_data;
429 copy_data.region = region;
430 copy_data.offset = offset;
431 copy_data.size = size;
433 info->transform_func (dest, meta, src,
434 _gst_meta_transform_copy, ©_data);
441 _gst_buffer_copy (GstBuffer * buffer)
445 g_return_val_if_fail (buffer != NULL, NULL);
447 /* create a fresh new buffer */
448 copy = gst_buffer_new ();
450 /* we simply copy everything from our parent */
451 gst_buffer_copy_into (copy, buffer, GST_BUFFER_COPY_ALL, 0, -1);
456 /* the default dispose function revives the buffer and returns it to the
457 * pool when there is a pool */
459 _gst_buffer_dispose (GstBuffer * buffer)
463 /* no pool, do free */
464 if ((pool = buffer->pool) == NULL)
467 /* keep the buffer alive */
468 gst_buffer_ref (buffer);
469 /* return the buffer to the pool */
470 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
471 gst_buffer_pool_release_buffer (pool, buffer);
477 _gst_buffer_free (GstBuffer * buffer)
479 GstMetaItem *walk, *next;
483 g_return_if_fail (buffer != NULL);
485 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
488 for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
489 GstMeta *meta = &walk->meta;
490 const GstMetaInfo *info = meta->info;
492 /* call free_func if any */
494 info->free_func (meta, buffer);
497 /* and free the slice */
498 g_slice_free1 (ITEM_SIZE (info), walk);
501 /* get the size, when unreffing the memory, we could also unref the buffer
503 msize = GST_MINI_OBJECT_SIZE (buffer);
505 /* free our memory */
506 len = GST_BUFFER_MEM_LEN (buffer);
507 for (i = 0; i < len; i++)
508 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
510 /* we set msize to 0 when the buffer is part of the memory block */
512 g_slice_free1 (msize, buffer);
514 gst_memory_unref (GST_BUFFER_BUFMEM (buffer));
518 gst_buffer_init (GstBufferImpl * buffer, gsize size)
520 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), _gst_buffer_type, size);
522 buffer->buffer.mini_object.copy =
523 (GstMiniObjectCopyFunction) _gst_buffer_copy;
524 buffer->buffer.mini_object.dispose =
525 (GstMiniObjectDisposeFunction) _gst_buffer_dispose;
526 buffer->buffer.mini_object.free =
527 (GstMiniObjectFreeFunction) _gst_buffer_free;
529 GST_BUFFER (buffer)->pool = NULL;
530 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
531 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
532 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
533 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
534 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
536 GST_BUFFER_MEM_LEN (buffer) = 0;
537 GST_BUFFER_META (buffer) = NULL;
543 * Creates a newly allocated buffer without any data.
547 * Returns: (transfer full): the new #GstBuffer.
550 gst_buffer_new (void)
552 GstBufferImpl *newbuf;
554 newbuf = g_slice_new (GstBufferImpl);
555 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
557 gst_buffer_init (newbuf, sizeof (GstBufferImpl));
559 return GST_BUFFER_CAST (newbuf);
563 * gst_buffer_new_allocate:
564 * @allocator: (transfer none) (allow-none): the #GstAllocator to use, or NULL to use the
566 * @size: the size in bytes of the new buffer's data.
567 * @params: (transfer none) (allow-none): optional parameters
569 * Tries to create a newly allocated buffer with data of the given size and
570 * extra parameters from @allocator. If the requested amount of memory can't be
571 * allocated, NULL will be returned. The allocated buffer memory is not cleared.
573 * When @allocator is NULL, the default memory allocator will be used.
575 * Note that when @size == 0, the buffer will not have memory associated with it.
579 * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
583 gst_buffer_new_allocate (GstAllocator * allocator, gsize size,
584 GstAllocationParams * params)
595 mem = gst_allocator_alloc (allocator, size, params);
596 if (G_UNLIKELY (mem == NULL))
602 newbuf = gst_buffer_new ();
605 _memory_add (newbuf, -1, mem);
607 GST_CAT_LOG (GST_CAT_BUFFER,
608 "new buffer %p of size %" G_GSIZE_FORMAT " from allocator %p", newbuf,
613 asize = sizeof (GstBufferImpl) + size;
614 data = g_slice_alloc (asize);
615 if (G_UNLIKELY (data == NULL))
618 newbuf = GST_BUFFER_CAST (data);
620 gst_buffer_init ((GstBufferImpl *) data, asize);
622 mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL,
624 _memory_add (newbuf, -1, mem);
629 /* allocate memory and buffer, it might be interesting to do this but there
630 * are many complications. We need to keep the memory mapped to access the
631 * buffer fields and the memory for the buffer might be just very slow. We
632 * also need to do some more magic to get the alignment right. */
633 asize = sizeof (GstBufferImpl) + size;
634 mem = gst_allocator_alloc (allocator, asize, align);
635 if (G_UNLIKELY (mem == NULL))
638 /* map the data part and init the buffer in it, set the buffer size to 0 so
639 * that a finalize won't free the buffer */
640 data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
641 gst_buffer_init ((GstBufferImpl *) data, 0);
642 gst_memory_unmap (mem);
644 /* strip off the buffer */
645 gst_memory_resize (mem, sizeof (GstBufferImpl), size);
647 newbuf = GST_BUFFER_CAST (data);
648 GST_BUFFER_BUFMEM (newbuf) = mem;
651 _memory_add (newbuf, -1, gst_memory_ref (mem));
659 GST_CAT_WARNING (GST_CAT_BUFFER,
660 "failed to allocate %" G_GSIZE_FORMAT " bytes", size);
666 * gst_buffer_new_wrapped_full:
667 * @flags: #GstMemoryFlags
668 * @data: data to wrap
669 * @maxsize: allocated size of @data
670 * @offset: offset in @data
671 * @size: size of valid data
672 * @user_data: user_data
673 * @notify: called with @user_data when the memory is freed
675 * Allocate a new buffer that wraps the given memory. @data must point to
676 * @maxsize of memory, the wrapped buffer will have the region from @offset and
679 * When the buffer is destroyed, @notify will be called with @user_data.
681 * The prefix/padding must be filled with 0 if @flags contains
682 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
684 * Returns: (transfer full): a new #GstBuffer
687 gst_buffer_new_wrapped_full (GstMemoryFlags flags, gpointer data,
688 gsize maxsize, gsize offset, gsize size, gpointer user_data,
689 GDestroyNotify notify)
693 newbuf = gst_buffer_new ();
694 gst_buffer_append_memory (newbuf,
695 gst_memory_new_wrapped (flags, data, maxsize, offset, size,
702 * gst_buffer_new_wrapped:
703 * @data: data to wrap
704 * @size: allocated size of @data
706 * Creates a new buffer that wraps the given @data. The memory will be freed
707 * with g_free and will be marked writable.
711 * Returns: (transfer full): a new #GstBuffer
714 gst_buffer_new_wrapped (gpointer data, gsize size)
716 return gst_buffer_new_wrapped_full (0, data, size, 0, size, data, g_free);
720 * gst_buffer_n_memory:
721 * @buffer: a #GstBuffer.
723 * Get the amount of memory blocks that this buffer has.
725 * Returns: (transfer full): the amount of memory block in this buffer.
728 gst_buffer_n_memory (GstBuffer * buffer)
730 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
732 return GST_BUFFER_MEM_LEN (buffer);
736 * gst_buffer_take_memory:
737 * @buffer: a #GstBuffer.
738 * @idx: the index to add the memory at, or -1 to append it to the end
739 * @mem: (transfer full): a #GstMemory.
741 * Add the memory block @mem to @buffer at @idx. This function takes ownership
742 * of @mem and thus doesn't increase its refcount.
745 gst_buffer_take_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
747 g_return_if_fail (GST_IS_BUFFER (buffer));
748 g_return_if_fail (gst_buffer_is_writable (buffer));
749 g_return_if_fail (mem != NULL);
750 g_return_if_fail (idx == -1 ||
751 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)));
753 _memory_add (buffer, idx, mem);
757 _get_mapped (GstBuffer * buffer, guint idx, GstMapInfo * info,
760 GstMemory *mem, *mapped;
762 mem = GST_BUFFER_MEM_PTR (buffer, idx);
764 mapped = gst_memory_make_mapped (mem, info, flags);
769 GST_BUFFER_MEM_PTR (buffer, idx) = mapped;
770 gst_memory_unref (mem);
777 * gst_buffer_get_memory:
778 * @buffer: a #GstBuffer.
781 * Get the memory block in @buffer at @idx. If @idx is -1, all memory is merged
782 * into one large #GstMemory object that is then returned.
784 * Returns: (transfer full): a #GstMemory at @idx. Use gst_memory_unref () after usage.
787 gst_buffer_get_memory (GstBuffer * buffer, gint idx)
791 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
792 g_return_val_if_fail (idx == -1 ||
793 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)), NULL);
797 mem = _get_merged_memory (buffer, &merged);
798 } else if ((mem = GST_BUFFER_MEM_PTR (buffer, idx))) {
799 gst_memory_ref (mem);
805 * gst_buffer_replace_memory:
806 * @buffer: a #GstBuffer.
808 * @mem: (transfer full): a #GstMemory
810 * Replaces the memory block in @buffer at @idx with @mem. If @idx is -1, all
811 * memory will be removed and replaced with @mem.
813 * @buffer should be writable.
816 gst_buffer_replace_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
818 g_return_if_fail (GST_IS_BUFFER (buffer));
819 g_return_if_fail (gst_buffer_is_writable (buffer));
820 g_return_if_fail (idx == -1 ||
821 (idx >= 0 && idx < GST_BUFFER_MEM_LEN (buffer)));
824 _replace_all_memory (buffer, mem);
828 if ((old = GST_BUFFER_MEM_PTR (buffer, idx)))
829 gst_memory_unref (old);
830 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
835 * gst_buffer_remove_memory_range:
836 * @buffer: a #GstBuffer.
840 * Remove @len memory blocks in @buffer starting from @idx.
842 * @length can be -1, in which case all memory starting from @idx is removed.
845 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, gint length)
849 g_return_if_fail (GST_IS_BUFFER (buffer));
850 g_return_if_fail (gst_buffer_is_writable (buffer));
852 len = GST_BUFFER_MEM_LEN (buffer);
853 g_return_if_fail ((length == -1 && idx < len) || length + idx < len);
859 for (i = idx; i < end; i++)
860 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
863 g_memmove (&GST_BUFFER_MEM_PTR (buffer, idx),
864 &GST_BUFFER_MEM_PTR (buffer, end), (len - end) * sizeof (gpointer));
866 GST_BUFFER_MEM_LEN (buffer) = len - length;
870 * gst_buffer_get_sizes:
871 * @buffer: a #GstBuffer.
872 * @offset: a pointer to the offset
873 * @maxsize: a pointer to the maxsize
875 * Get the total size of all memory blocks in @buffer.
877 * When not %NULL, @offset will contain the offset of the data in the first
878 * memory block in @buffer and @maxsize will contain the sum of the size
879 * and @offset and the amount of extra padding on the last memory block.
880 * @offset and @maxsize can be used to resize the buffer with
881 * gst_buffer_resize().
883 * Returns: the total size of the memory in @buffer.
886 gst_buffer_get_sizes (GstBuffer * buffer, gsize * offset, gsize * maxsize)
892 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
894 len = GST_BUFFER_MEM_LEN (buffer);
896 if (G_LIKELY (len == 1)) {
898 mem = GST_BUFFER_MEM_PTR (buffer, 0);
899 size = gst_memory_get_sizes (mem, offset, maxsize);
904 size = offs = extra = 0;
905 for (i = 0; i < len; i++) {
908 mem = GST_BUFFER_MEM_PTR (buffer, i);
909 s = gst_memory_get_sizes (mem, &o, &ms);
913 /* first size, take accumulated data before as the offset */
917 /* save the amount of data after this block */
918 extra = ms - (o + s);
920 /* empty block, add as extra */
927 *maxsize = offs + size + extra;
934 * @buffer: a #GstBuffer.
935 * @offset: the offset adjustement
936 * @size: the new size or -1 to just adjust the offset
938 * Set the total size of the buffer
941 gst_buffer_resize (GstBuffer * buffer, gssize offset, gssize size)
945 gsize bsize, bufsize, bufoffs, bufmax;
948 g_return_if_fail (gst_buffer_is_writable (buffer));
949 g_return_if_fail (size >= -1);
951 bufsize = gst_buffer_get_sizes (buffer, &bufoffs, &bufmax);
953 GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSSIZE_FORMAT
954 " size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%"
955 G_GSIZE_FORMAT, buffer, offset, size, bufsize, bufoffs, bufmax);
957 /* we can't go back further than the current offset or past the end of the
959 g_return_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
960 && bufoffs + offset <= bufmax));
962 g_return_if_fail (bufsize >= offset);
963 size = bufsize - offset;
965 g_return_if_fail (bufmax >= bufoffs + offset + size);
968 if (offset == 0 && size == bufsize)
971 len = GST_BUFFER_MEM_LEN (buffer);
974 for (i = 0; i < len; i++) {
977 mem = GST_BUFFER_MEM_PTR (buffer, i);
978 bsize = gst_memory_get_sizes (mem, NULL, NULL);
981 /* last buffer always gets resized to the remaining size */
984 /* shrink buffers before the offset */
985 else if ((gssize) bsize <= offset) {
987 noffs = offset - bsize;
990 /* clip other buffers */
992 left = MIN (bsize - offset, size);
994 if (offset != 0 || left != bsize) {
995 if (gst_memory_is_exclusive (mem)) {
996 gst_memory_resize (mem, offset, left);
1000 if (mem->flags & GST_MEMORY_FLAG_NO_SHARE)
1001 tmp = gst_memory_copy (mem, offset, left);
1003 tmp = gst_memory_share (mem, offset, left);
1005 gst_memory_unref (mem);
1012 GST_BUFFER_MEM_PTR (buffer, i) = mem;
1018 * @buffer: a #GstBuffer.
1019 * @info: (out): info about the mapping
1020 * @flags: flags for the mapping
1022 * This function fills @info with a pointer to the merged memory in @buffer.
1023 * @flags describe the desired access of the memory. When @flags is
1024 * #GST_MAP_WRITE, @buffer should be writable (as returned from
1025 * gst_buffer_is_writable()).
1027 * When @buffer is writable but the memory isn't, a writable copy will
1028 * automatically be created and returned. The readonly copy of the buffer memory
1029 * will then also be replaced with this writable copy.
1031 * When the buffer contains multiple memory blocks, the returned pointer will be
1032 * a concatenation of the memory blocks.
1034 * The memory in @info should be unmapped with gst_buffer_unmap() after usage.
1036 * Returns: (transfer full): %TRUE if the map succeeded and @info contains valid
1040 gst_buffer_map (GstBuffer * buffer, GstMapInfo * info, GstMapFlags flags)
1042 GstMemory *mem, *nmem;
1043 gboolean write, writable, merged;
1045 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1046 g_return_val_if_fail (info != NULL, FALSE);
1048 write = (flags & GST_MAP_WRITE) != 0;
1049 writable = gst_buffer_is_writable (buffer);
1051 /* check if we can write when asked for write access */
1052 if (G_UNLIKELY (write && !writable))
1055 mem = _get_merged_memory (buffer, &merged);
1056 if (G_UNLIKELY (mem == NULL))
1059 /* now try to map */
1060 nmem = gst_memory_make_mapped (mem, info, flags);
1061 if (G_UNLIKELY (nmem == NULL))
1064 /* if we merged or when the map returned a different memory, we try to replace
1065 * the memory in the buffer */
1066 if (G_UNLIKELY (merged || nmem != mem)) {
1067 /* if the buffer is writable, replace the memory */
1069 _replace_all_memory (buffer, gst_memory_ref (nmem));
1071 if (GST_BUFFER_MEM_LEN (buffer) > 1) {
1072 GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
1073 "temporary mapping for memory %p in buffer %p", nmem, buffer);
1082 GST_WARNING_OBJECT (buffer, "write map requested on non-writable buffer");
1083 g_critical ("write map requested on non-writable buffer");
1088 /* empty buffer, we need to return NULL */
1089 GST_DEBUG_OBJECT (buffer, "can't get buffer memory");
1090 info->memory = NULL;
1098 GST_DEBUG_OBJECT (buffer, "cannot map memory");
1105 * @buffer: a #GstBuffer.
1106 * @info: a #GstMapInfo
1108 * Release the memory previously mapped with gst_buffer_map().
1111 gst_buffer_unmap (GstBuffer * buffer, GstMapInfo * info)
1113 g_return_if_fail (GST_IS_BUFFER (buffer));
1114 g_return_if_fail (info != NULL);
1116 /* we need to check for NULL, it is possible that we tried to map a buffer
1117 * without memory and we should be able to unmap that fine */
1118 if (G_LIKELY (info->memory)) {
1119 gst_memory_unmap (info->memory, info);
1120 gst_memory_unref (info->memory);
1126 * @buffer: a #GstBuffer.
1127 * @offset: the offset to fill
1128 * @src: the source address
1129 * @size: the size to fill
1131 * Copy @size bytes from @src to @buffer at @offset.
1133 * Returns: The amount of bytes copied. This value can be lower than @size
1134 * when @buffer did not contain enough data.
1137 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
1141 const guint8 *ptr = src;
1143 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1144 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1145 g_return_val_if_fail (src != NULL, 0);
1147 len = GST_BUFFER_MEM_LEN (buffer);
1150 for (i = 0; i < len && left > 0; i++) {
1155 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1156 if (info.size > offset) {
1157 /* we have enough */
1158 tocopy = MIN (info.size - offset, left);
1159 memcpy ((guint8 *) info.data + offset, ptr, tocopy);
1164 /* offset past buffer, skip */
1165 offset -= info.size;
1167 gst_memory_unmap (mem, &info);
1173 * gst_buffer_extract:
1174 * @buffer: a #GstBuffer.
1175 * @offset: the offset to extract
1176 * @dest: the destination address
1177 * @size: the size to extract
1179 * Copy @size bytes starting from @offset in @buffer to @dest.
1181 * Returns: The amount of bytes extracted. This value can be lower than @size
1182 * when @buffer did not contain enough data.
1185 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
1190 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1191 g_return_val_if_fail (dest != NULL, 0);
1193 len = GST_BUFFER_MEM_LEN (buffer);
1196 for (i = 0; i < len && left > 0; i++) {
1201 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1202 if (info.size > offset) {
1203 /* we have enough */
1204 tocopy = MIN (info.size - offset, left);
1205 memcpy (ptr, (guint8 *) info.data + offset, tocopy);
1210 /* offset past buffer, skip */
1211 offset -= info.size;
1213 gst_memory_unmap (mem, &info);
1219 * gst_buffer_memcmp:
1220 * @buffer: a #GstBuffer.
1221 * @offset: the offset in @buffer
1222 * @mem: the memory to compare
1223 * @size: the size to compare
1225 * Compare @size bytes starting from @offset in @buffer with the memory in @mem.
1227 * Returns: 0 if the memory is equal.
1230 gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem,
1234 const guint8 *ptr = mem;
1237 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1238 g_return_val_if_fail (mem != NULL, 0);
1240 len = GST_BUFFER_MEM_LEN (buffer);
1242 for (i = 0; i < len && size > 0 && res == 0; i++) {
1247 mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1248 if (info.size > offset) {
1249 /* we have enough */
1250 tocmp = MIN (info.size - offset, size);
1251 res = memcmp (ptr, (guint8 *) info.data + offset, tocmp);
1256 /* offset past buffer, skip */
1257 offset -= info.size;
1259 gst_memory_unmap (mem, &info);
1265 * gst_buffer_memset:
1266 * @buffer: a #GstBuffer.
1267 * @offset: the offset in @buffer
1268 * @val: the value to set
1269 * @size: the size to set
1271 * Fill @buf with @size bytes with @val starting from @offset.
1273 * Returns: The amount of bytes filled. This value can be lower than @size
1274 * when @buffer did not contain enough data.
1277 gst_buffer_memset (GstBuffer * buffer, gsize offset, guint8 val, gsize size)
1281 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1282 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1284 len = GST_BUFFER_MEM_LEN (buffer);
1287 for (i = 0; i < len && left > 0; i++) {
1292 mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1293 if (info.size > offset) {
1294 /* we have enough */
1295 toset = MIN (info.size - offset, left);
1296 memset ((guint8 *) info.data + offset, val, toset);
1300 /* offset past buffer, skip */
1301 offset -= info.size;
1303 gst_memory_unmap (mem, &info);
1309 * gst_buffer_copy_region:
1310 * @parent: a #GstBuffer.
1311 * @flags: the #GstBufferCopyFlags
1312 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
1314 * @size: the size of the new #GstBuffer sub-buffer, in bytes.
1316 * Creates a sub-buffer from @parent at @offset and @size.
1317 * This sub-buffer uses the actual memory space of the parent buffer.
1318 * This function will copy the offset and timestamp fields when the
1319 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
1320 * #GST_BUFFER_OFFSET_NONE.
1321 * If @offset equals 0 and @size equals the total size of @buffer, the
1322 * duration and offset end fields are also copied. If not they will be set
1323 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
1327 * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
1331 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
1332 gsize offset, gsize size)
1336 g_return_val_if_fail (buffer != NULL, NULL);
1338 /* create the new buffer */
1339 copy = gst_buffer_new ();
1341 GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
1342 "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
1344 gst_buffer_copy_into (copy, buffer, flags, offset, size);
1350 * gst_buffer_append:
1351 * @buf1: (transfer full): the first source #GstBuffer to append.
1352 * @buf2: (transfer full): the second source #GstBuffer to append.
1354 * Append all the memory from @buf2 to @buf1. The result buffer will contain a
1355 * concatenation of the memory of @buf1 and @buf2.
1357 * Returns: (transfer full): the new #GstBuffer that contains the memory
1358 * of the two source buffers.
1361 gst_buffer_append (GstBuffer * buf1, GstBuffer * buf2)
1365 g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
1366 g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
1368 buf1 = gst_buffer_make_writable (buf1);
1369 buf2 = gst_buffer_make_writable (buf2);
1371 len = GST_BUFFER_MEM_LEN (buf2);
1372 for (i = 0; i < len; i++) {
1375 mem = GST_BUFFER_MEM_PTR (buf2, i);
1376 GST_BUFFER_MEM_PTR (buf2, i) = NULL;
1377 _memory_add (buf1, -1, mem);
1380 /* we can calculate the duration too. Also make sure we're not messing
1381 * with invalid DURATIONS */
1382 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
1383 GST_BUFFER_DURATION_IS_VALID (buf2)) {
1385 GST_BUFFER_DURATION (buf1) += GST_BUFFER_DURATION (buf2);
1387 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
1388 /* set offset_end */
1389 GST_BUFFER_OFFSET_END (buf1) = GST_BUFFER_OFFSET_END (buf2);
1392 GST_BUFFER_MEM_LEN (buf2) = 0;
1393 gst_buffer_unref (buf2);
1399 * gst_buffer_get_meta:
1400 * @buffer: a #GstBuffer
1401 * @api: the #GType of an API
1403 * Get the metadata for @api on buffer. When there is no such
1404 * metadata, NULL is returned.
1406 * Returns: the metadata for @api on @buffer.
1409 gst_buffer_get_meta (GstBuffer * buffer, GType api)
1412 GstMeta *result = NULL;
1414 g_return_val_if_fail (buffer != NULL, NULL);
1415 g_return_val_if_fail (api != 0, NULL);
1417 /* find GstMeta of the requested API */
1418 for (item = GST_BUFFER_META (buffer); item; item = item->next) {
1419 GstMeta *meta = &item->meta;
1420 if (meta->info->api == api) {
1429 * gst_buffer_add_meta:
1430 * @buffer: a #GstBuffer
1431 * @info: a #GstMetaInfo
1432 * @params: params for @info
1434 * Add metadata for @info to @buffer using the parameters in @params.
1436 * Returns: (transfer none): the metadata for the api in @info on @buffer.
1439 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
1443 GstMeta *result = NULL;
1446 g_return_val_if_fail (buffer != NULL, NULL);
1447 g_return_val_if_fail (info != NULL, NULL);
1448 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
1450 /* create a new slice */
1451 size = ITEM_SIZE (info);
1452 item = g_slice_alloc (size);
1453 result = &item->meta;
1454 result->info = info;
1455 result->flags = GST_META_FLAG_NONE;
1457 GST_CAT_DEBUG (GST_CAT_BUFFER,
1458 "alloc metadata %p (%s) of size %" G_GSIZE_FORMAT, result,
1459 g_type_name (info->type), info->size);
1461 /* call the init_func when needed */
1462 if (info->init_func)
1463 if (!info->init_func (result, params, buffer))
1466 /* and add to the list of metadata */
1467 item->next = GST_BUFFER_META (buffer);
1468 GST_BUFFER_META (buffer) = item;
1474 g_slice_free1 (size, item);
1480 * gst_buffer_remove_meta:
1481 * @buffer: a #GstBuffer
1484 * Remove the metadata for @meta on @buffer.
1486 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
1487 * metadata was on @buffer.
1490 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
1492 GstMetaItem *walk, *prev;
1494 g_return_val_if_fail (buffer != NULL, FALSE);
1495 g_return_val_if_fail (meta != NULL, FALSE);
1496 g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
1498 /* find the metadata and delete */
1499 prev = GST_BUFFER_META (buffer);
1500 for (walk = prev; walk; walk = walk->next) {
1501 GstMeta *m = &walk->meta;
1503 const GstMetaInfo *info = meta->info;
1505 /* remove from list */
1506 if (GST_BUFFER_META (buffer) == walk)
1507 GST_BUFFER_META (buffer) = walk->next;
1509 prev->next = walk->next;
1510 /* call free_func if any */
1511 if (info->free_func)
1512 info->free_func (m, buffer);
1514 /* and free the slice */
1515 g_slice_free1 (ITEM_SIZE (info), walk);
1520 return walk != NULL;
1524 * gst_buffer_iterate_meta:
1525 * @buffer: a #GstBuffer
1526 * @state: an opaque state pointer
1528 * Retrieve the next #GstMeta after @current. If @state points
1529 * to %NULL, the first metadata is returned.
1531 * @state will be updated with an opage state pointer
1533 * Returns: The next #GstMeta or %NULL when there are no more items.
1536 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
1540 g_return_val_if_fail (buffer != NULL, NULL);
1541 g_return_val_if_fail (state != NULL, NULL);
1543 meta = (GstMetaItem **) state;
1545 /* state NULL, move to first item */
1546 *meta = GST_BUFFER_META (buffer);
1548 /* state !NULL, move to next item in list */
1549 *meta = (*meta)->next;
1552 return &(*meta)->meta;
1558 * gst_buffer_foreach_meta:
1559 * @buffer: a #GstBuffer
1560 * @func: (scope call): a #GstBufferForeachMetaFunc to call
1561 * @user_data: (closure): user data passed to @func
1563 * Call @func with @user_data for each meta in @buffer.
1565 * @func can modify the passed meta pointer or its contents. The return value
1566 * of @func define if this function returns or if the remaining metadata items
1567 * in the buffer should be skipped.
1570 gst_buffer_foreach_meta (GstBuffer * buffer, GstBufferForeachMetaFunc func,
1573 GstMetaItem *walk, *prev, *next;
1575 g_return_if_fail (buffer != NULL);
1576 g_return_if_fail (func != NULL);
1578 /* find the metadata and delete */
1579 prev = GST_BUFFER_META (buffer);
1580 for (walk = prev; walk; walk = next) {
1584 m = new = &walk->meta;
1587 res = func (buffer, &new, user_data);
1590 const GstMetaInfo *info = m->info;
1592 GST_CAT_DEBUG (GST_CAT_BUFFER, "remove metadata %p (%s)", m,
1593 g_type_name (info->type));
1595 g_return_if_fail (gst_buffer_is_writable (buffer));
1597 /* remove from list */
1598 if (GST_BUFFER_META (buffer) == walk)
1599 GST_BUFFER_META (buffer) = next;
1603 /* call free_func if any */
1604 if (info->free_func)
1605 info->free_func (m, buffer);
1607 /* and free the slice */
1608 g_slice_free1 (ITEM_SIZE (info), walk);