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 _replace_memory (GstBuffer * buffer, GstMemory * mem)
180 len = GST_BUFFER_MEM_LEN (buffer);
181 /* unref old buffers */
182 for (i = 0; i < len; i++)
183 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
185 /* replace with single spanned buffer */
186 GST_BUFFER_MEM_PTR (buffer, 0) = mem;
187 GST_BUFFER_MEM_LEN (buffer) = 1;
191 _memory_add (GstBuffer * buffer, guint idx, GstMemory * mem)
193 guint i, len = GST_BUFFER_MEM_LEN (buffer);
195 if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) {
196 /* too many buffer, span them. */
197 /* FIXME, there is room for improvement here: We could only try to merge
198 * 2 buffers to make some room. If we can't efficiently merge 2 buffers we
199 * could try to only merge the two smallest buffers to avoid memcpy, etc. */
200 _replace_memory (buffer, _span_memory (buffer, 0, -1, FALSE));
201 /* we now have 1 single spanned buffer */
208 for (i = len; i > idx; i--) {
209 /* move buffers to insert, FIXME, we need to insert first and then merge */
210 GST_BUFFER_MEM_PTR (buffer, i) = GST_BUFFER_MEM_PTR (buffer, i - 1);
212 /* and insert the new buffer */
213 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
214 GST_BUFFER_MEM_LEN (buffer) = len + 1;
217 GST_DEFINE_MINI_OBJECT_TYPE (GstBuffer, gst_buffer);
220 _priv_gst_buffer_initialize (void)
222 _gst_buffer_type = gst_buffer_get_type ();
226 * gst_buffer_copy_into:
227 * @dest: a destination #GstBuffer
228 * @src: a source #GstBuffer
229 * @flags: flags indicating what metadata fields should be copied.
230 * @offset: offset to copy from
231 * @size: total size to copy. If -1, all data is copied.
233 * Copies the information from @src into @dest.
235 * @flags indicate which fields will be copied.
238 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
239 GstBufferCopyFlags flags, gsize offset, gsize size)
244 g_return_if_fail (dest != NULL);
245 g_return_if_fail (src != NULL);
247 /* nothing to copy if the buffers are the same */
248 if (G_UNLIKELY (dest == src))
251 g_return_if_fail (gst_buffer_is_writable (dest));
253 bufsize = gst_buffer_get_size (src);
254 g_return_if_fail (bufsize >= offset);
256 size = bufsize - offset;
257 g_return_if_fail (bufsize >= offset + size);
259 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p, offset %" G_GSIZE_FORMAT
260 "-%" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT, src, dest, offset, size,
263 if (flags & GST_BUFFER_COPY_FLAGS) {
265 GST_MINI_OBJECT_FLAGS (dest) = GST_MINI_OBJECT_FLAGS (src);
268 if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
270 GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src);
271 GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src);
272 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
273 if (size == bufsize) {
274 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
275 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
278 GST_BUFFER_PTS (dest) = GST_CLOCK_TIME_NONE;
279 GST_BUFFER_DTS (dest) = GST_CLOCK_TIME_NONE;
280 GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
281 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
282 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
286 if (flags & GST_BUFFER_COPY_MEMORY) {
288 gsize skip, left, len, i, bsize;
290 len = GST_BUFFER_MEM_LEN (src);
294 /* copy and make regions of the memory */
295 for (i = 0; i < len && left > 0; i++) {
296 mem = GST_BUFFER_MEM_PTR (src, i);
297 bsize = gst_memory_get_sizes (mem, NULL, NULL);
300 /* don't copy buffer */
305 tocopy = MIN (bsize - skip, left);
306 if (mem->flags & GST_MEMORY_FLAG_NO_SHARE) {
307 /* no share, always copy then */
308 mem = gst_memory_copy (mem, skip, tocopy);
310 } else if (tocopy < bsize) {
311 /* we need to clip something */
312 mem = gst_memory_share (mem, skip, tocopy);
315 mem = gst_memory_ref (mem);
317 _memory_add (dest, -1, mem);
321 if (flags & GST_BUFFER_COPY_MERGE) {
322 _replace_memory (dest, _span_memory (dest, 0, size, FALSE));
326 if (flags & GST_BUFFER_COPY_META) {
327 for (walk = GST_BUFFER_META (src); walk; walk = walk->next) {
328 GstMeta *meta = &walk->meta;
329 const GstMetaInfo *info = meta->info;
332 info->copy_func (dest, meta, src, offset, size);
338 _gst_buffer_copy (GstBuffer * buffer)
342 g_return_val_if_fail (buffer != NULL, NULL);
344 /* create a fresh new buffer */
345 copy = gst_buffer_new ();
347 /* we simply copy everything from our parent */
348 gst_buffer_copy_into (copy, buffer, GST_BUFFER_COPY_ALL, 0, -1);
353 /* the default dispose function revives the buffer and returns it to the
354 * pool when there is a pool */
356 _gst_buffer_dispose (GstBuffer * buffer)
360 /* no pool, do free */
361 if ((pool = buffer->pool) == NULL)
364 /* keep the buffer alive */
365 gst_buffer_ref (buffer);
366 /* return the buffer to the pool */
367 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
368 gst_buffer_pool_release_buffer (pool, buffer);
374 _gst_buffer_free (GstBuffer * buffer)
376 GstMetaItem *walk, *next;
380 g_return_if_fail (buffer != NULL);
382 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
385 for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
386 GstMeta *meta = &walk->meta;
387 const GstMetaInfo *info = meta->info;
389 /* call free_func if any */
391 info->free_func (meta, buffer);
394 /* and free the slice */
395 g_slice_free1 (ITEM_SIZE (info), walk);
398 /* get the size, when unreffing the memory, we could also unref the buffer
400 msize = GST_MINI_OBJECT_SIZE (buffer);
402 /* free our memory */
403 len = GST_BUFFER_MEM_LEN (buffer);
404 for (i = 0; i < len; i++)
405 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
407 /* we set msize to 0 when the buffer is part of the memory block */
409 g_slice_free1 (msize, buffer);
411 gst_memory_unref (GST_BUFFER_BUFMEM (buffer));
415 gst_buffer_init (GstBufferImpl * buffer, gsize size)
417 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), _gst_buffer_type, size);
419 buffer->buffer.mini_object.copy =
420 (GstMiniObjectCopyFunction) _gst_buffer_copy;
421 buffer->buffer.mini_object.dispose =
422 (GstMiniObjectDisposeFunction) _gst_buffer_dispose;
423 buffer->buffer.mini_object.free =
424 (GstMiniObjectFreeFunction) _gst_buffer_free;
426 GST_BUFFER (buffer)->pool = NULL;
427 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
428 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
429 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
430 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
431 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
433 GST_BUFFER_MEM_LEN (buffer) = 0;
434 GST_BUFFER_META (buffer) = NULL;
440 * Creates a newly allocated buffer without any data.
444 * Returns: (transfer full): the new #GstBuffer.
447 gst_buffer_new (void)
449 GstBufferImpl *newbuf;
451 newbuf = g_slice_new (GstBufferImpl);
452 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
454 gst_buffer_init (newbuf, sizeof (GstBufferImpl));
456 return GST_BUFFER_CAST (newbuf);
460 * gst_buffer_new_allocate:
461 * @allocator: (allow-none): the #GstAllocator to use, or NULL to use the
463 * @size: the size in bytes of the new buffer's data.
464 * @align: the alignment of the buffer memory
466 * Tries to create a newly allocated buffer with data of the given size and
467 * alignment from @allocator. If the requested amount of memory can't be
468 * allocated, NULL will be returned. The allocated buffer memory is not cleared.
470 * When @allocator is NULL, the default memory allocator will be used.
472 * Allocator buffer memory will be aligned to multiples of (@align + 1) bytes.
474 * Note that when @size == 0, the buffer will not have memory associated with it.
478 * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
482 gst_buffer_new_allocate (const GstAllocator * allocator, gsize size,
494 mem = gst_allocator_alloc (allocator, size, align);
495 if (G_UNLIKELY (mem == NULL))
501 newbuf = gst_buffer_new ();
504 _memory_add (newbuf, -1, mem);
506 GST_CAT_LOG (GST_CAT_BUFFER,
507 "new buffer %p of size %" G_GSIZE_FORMAT " from allocator %p", newbuf,
512 asize = sizeof (GstBufferImpl) + size;
513 data = g_slice_alloc (asize);
514 if (G_UNLIKELY (data == NULL))
517 newbuf = GST_BUFFER_CAST (data);
519 gst_buffer_init ((GstBufferImpl *) data, asize);
521 mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL,
523 _memory_add (newbuf, -1, mem);
528 /* allocate memory and buffer, it might be interesting to do this but there
529 * are many complications. We need to keep the memory mapped to access the
530 * buffer fields and the memory for the buffer might be just very slow. We
531 * also need to do some more magic to get the alignment right. */
532 asize = sizeof (GstBufferImpl) + size;
533 mem = gst_allocator_alloc (allocator, asize, align);
534 if (G_UNLIKELY (mem == NULL))
537 /* map the data part and init the buffer in it, set the buffer size to 0 so
538 * that a finalize won't free the buffer */
539 data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
540 gst_buffer_init ((GstBufferImpl *) data, 0);
541 gst_memory_unmap (mem);
543 /* strip off the buffer */
544 gst_memory_resize (mem, sizeof (GstBufferImpl), size);
546 newbuf = GST_BUFFER_CAST (data);
547 GST_BUFFER_BUFMEM (newbuf) = mem;
550 _memory_add (newbuf, -1, gst_memory_ref (mem));
558 GST_CAT_WARNING (GST_CAT_BUFFER,
559 "failed to allocate %" G_GSIZE_FORMAT " bytes", size);
565 * gst_buffer_new_wrapped_full:
566 * @data: data to wrap
567 * @free_func: function to free @data
568 * @offset: offset in @data of valid data
569 * @size: size of valid data in @data starting at @offset
571 * Creates a new buffer that wraps the given @data. Valid data is set
572 * to start at @offset and up to @size. If no @free_func is provided,
573 * buffer memory is marked READONLY.
577 * Returns: (transfer full): a new #GstBuffer
580 gst_buffer_new_wrapped_full (gpointer data, GFreeFunc free_func, gsize offset,
585 newbuf = gst_buffer_new ();
586 gst_buffer_take_memory (newbuf, -1,
587 gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
588 data, free_func, offset + size, offset, size));
594 * gst_buffer_new_wrapped:
595 * @data: data to wrap
596 * @size: allocated size of @data
598 * Creates a new buffer that wraps the given @data. The memory will be freed
599 * with g_free and will be marked writable.
603 * Returns: (transfer full): a new #GstBuffer
606 gst_buffer_new_wrapped (gpointer data, gsize size)
608 return gst_buffer_new_wrapped_full (data, g_free, 0, size);
612 * gst_buffer_n_memory:
613 * @buffer: a #GstBuffer.
615 * Get the amount of memory blocks that this buffer has.
617 * Returns: (transfer full): the amount of memory block in this buffer.
620 gst_buffer_n_memory (GstBuffer * buffer)
622 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
624 return GST_BUFFER_MEM_LEN (buffer);
628 * gst_buffer_take_memory:
629 * @buffer: a #GstBuffer.
630 * @idx: the index to add the memory at, or -1 to append it to the end
631 * @mem: (transfer full): a #GstMemory.
633 * Add the memory block @mem to @buffer at @idx. This function takes ownership
634 * of @mem and thus doesn't increase its refcount.
637 gst_buffer_take_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
639 g_return_if_fail (GST_IS_BUFFER (buffer));
640 g_return_if_fail (gst_buffer_is_writable (buffer));
641 g_return_if_fail (mem != NULL);
642 g_return_if_fail (idx == -1 ||
643 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)));
645 _memory_add (buffer, idx, mem);
649 _get_memory (GstBuffer * buffer, guint idx, gboolean write)
653 mem = GST_BUFFER_MEM_PTR (buffer, idx);
655 if (G_UNLIKELY (write && !gst_memory_is_writable (mem))) {
657 GST_CAT_LOG (GST_CAT_BUFFER,
658 "making writable copy of memory %p in buffer %p", mem, buffer);
659 /* replace with a writable copy */
660 copy = gst_memory_copy (mem, 0, -1);
661 GST_BUFFER_MEM_PTR (buffer, idx) = copy;
662 gst_memory_unref (mem);
669 * gst_buffer_peek_memory:
670 * @buffer: a #GstBuffer.
672 * @flags: #GstMapFlags
674 * Get the memory block in @buffer at @idx for memory access in @flags.
675 * This function does not return a refcount to the memory block. The memory
676 * block stays valid for as long as the caller has a valid reference to @buffer
677 * and as long as no operations that modify the memory blocks are called, such
678 * as gst_buffer_remove_memory_range(), gst_buffer_take_memory() and gst_buffer_resize().
680 * @buffer should be writable when @flags contains #GST_MAP_WRITE. If the memory
681 * at @idx is not writable, a new writable copy will be installed in @buffer and
684 * Returns: a #GstMemory at @idx.
687 gst_buffer_peek_memory (GstBuffer * buffer, guint idx, GstMapFlags flags)
692 write = (flags & GST_MAP_WRITE) != 0;
694 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
695 g_return_val_if_fail (idx < GST_BUFFER_MEM_LEN (buffer), NULL);
697 /* check if we can write when asked for write access */
698 if (G_UNLIKELY (write && !gst_buffer_is_writable (buffer)))
701 mem = _get_memory (buffer, idx, write);
708 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
714 * gst_buffer_remove_memory_range:
715 * @buffer: a #GstBuffer.
719 * Remove @len memory blocks in @buffer starting from @idx.
721 * @length can be -1, in which case all memory starting from @idx is removed.
724 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, guint length)
728 g_return_if_fail (GST_IS_BUFFER (buffer));
729 g_return_if_fail (gst_buffer_is_writable (buffer));
731 len = GST_BUFFER_MEM_LEN (buffer);
733 g_return_if_fail (idx < len);
738 for (i = idx; i < end; i++)
739 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
742 g_memmove (&GST_BUFFER_MEM_PTR (buffer, idx),
743 &GST_BUFFER_MEM_PTR (buffer, end), (len - end) * sizeof (gpointer));
745 GST_BUFFER_MEM_LEN (buffer) = len - length;
749 * gst_buffer_get_merged_memory:
750 * @buffer: a #GstBuffer.
752 * Return a #GstMemory object that contains all the memory in @buffer. If there
753 * was only one memory in @buffer, it will be returned directly, otherwise all
754 * memory objects will be merged into one object that will be returned.
756 * Returns: a #GstMemory with the merged memory in @buffer. This function can
757 * return %NULL if there is no memory in @buffer. Use gst_memory_unref() after
761 gst_buffer_get_merged_memory (GstBuffer * buffer)
766 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
768 len = GST_BUFFER_MEM_LEN (buffer);
770 if (G_UNLIKELY (len == 0)) {
773 } else if (G_LIKELY (len == 1)) {
774 /* we can take the first one */
775 mem = GST_BUFFER_MEM_PTR (buffer, 0);
776 gst_memory_ref (mem);
778 /* we need to span memory */
779 mem = _span_memory (buffer, 0, -1, FALSE);
785 * gst_buffer_get_sizes:
786 * @buffer: a #GstBuffer.
787 * @offset: a pointer to the offset
788 * @maxsize: a pointer to the maxsize
790 * Get the total size of all memory blocks in @buffer.
792 * When not %NULL, @offset will contain the offset of the data in the first
793 * memory block in @buffer and @maxsize will contain the sum of the size
794 * and @offset and the amount of extra padding on the last memory block.
795 * @offset and @maxsize can be used to resize the buffer with
796 * gst_buffer_resize().
798 * Returns: the total size of the memory in @buffer.
801 gst_buffer_get_sizes (GstBuffer * buffer, gsize * offset, gsize * maxsize)
807 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
809 len = GST_BUFFER_MEM_LEN (buffer);
811 if (G_LIKELY (len == 1)) {
813 mem = GST_BUFFER_MEM_PTR (buffer, 0);
814 size = gst_memory_get_sizes (mem, offset, maxsize);
819 size = offs = extra = 0;
820 for (i = 0; i < len; i++) {
823 mem = GST_BUFFER_MEM_PTR (buffer, i);
824 s = gst_memory_get_sizes (mem, &o, &ms);
828 /* first size, take accumulated data before as the offset */
832 /* save the amount of data after this block */
833 extra = ms - (o + s);
835 /* empty block, add as extra */
842 *maxsize = offs + size + extra;
849 * @buffer: a #GstBuffer.
850 * @offset: the offset adjustement
851 * @size: the new size or -1 to just adjust the offset
853 * Set the total size of the buffer
856 gst_buffer_resize (GstBuffer * buffer, gssize offset, gssize size)
860 gsize bsize, bufsize, bufoffs, bufmax;
863 g_return_if_fail (gst_buffer_is_writable (buffer));
864 g_return_if_fail (size >= -1);
866 bufsize = gst_buffer_get_sizes (buffer, &bufoffs, &bufmax);
868 GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSSIZE_FORMAT
869 " size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%"
870 G_GSIZE_FORMAT, buffer, offset, size, bufsize, bufoffs, bufmax);
872 /* we can't go back further than the current offset or past the end of the
874 g_return_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
875 && bufoffs + offset <= bufmax));
877 g_return_if_fail (bufsize >= offset);
878 size = bufsize - offset;
880 g_return_if_fail (bufmax >= bufoffs + offset + size);
882 len = GST_BUFFER_MEM_LEN (buffer);
885 for (i = 0; i < len; i++) {
888 mem = GST_BUFFER_MEM_PTR (buffer, i);
889 bsize = gst_memory_get_sizes (mem, NULL, NULL);
892 /* last buffer always gets resized to the remaining size */
895 /* shrink buffers before the offset */
896 else if ((gssize) bsize <= offset) {
898 noffs = offset - bsize;
901 /* clip other buffers */
903 left = MIN (bsize - offset, size);
905 if (offset != 0 || left != bsize) {
906 /* we need to clip something */
907 if (gst_memory_is_writable (mem)) {
908 gst_memory_resize (mem, offset, left);
912 if (mem->flags & GST_MEMORY_FLAG_NO_SHARE)
913 tmp = gst_memory_copy (mem, offset, left);
915 tmp = gst_memory_share (mem, offset, left);
917 gst_memory_unref (mem);
924 GST_BUFFER_MEM_PTR (buffer, i) = mem;
930 * @buffer: a #GstBuffer.
931 * @info: (out): info about the mapping
932 * @flags: flags for the mapping
934 * This function fills @info with a pointer to the merged memory in @buffer.
935 * @flags describe the desired access of the memory. When @flags is
936 * #GST_MAP_WRITE, @buffer should be writable (as returned from
937 * gst_buffer_is_writable()).
939 * When @buffer is writable but the memory isn't, a writable copy will
940 * automatically be created and returned. The readonly copy of the buffer memory
941 * will then also be replaced with this writable copy.
943 * When the buffer contains multiple memory blocks, the returned pointer will be
944 * a concatenation of the memory blocks.
946 * Returns: (transfer full): %TRUE if the map succeeded and @info contains valid
950 gst_buffer_map (GstBuffer * buffer, GstMapInfo * info, GstMapFlags flags)
953 gboolean write, writable;
955 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
956 g_return_val_if_fail (info != NULL, FALSE);
958 write = (flags & GST_MAP_WRITE) != 0;
959 writable = gst_buffer_is_writable (buffer);
961 /* check if we can write when asked for write access */
962 if (G_UNLIKELY (write && !writable))
965 mem = gst_buffer_get_merged_memory (buffer);
966 if (G_UNLIKELY (mem == NULL))
970 mem = gst_memory_make_mapped (mem, info, flags);
971 if (G_UNLIKELY (mem == NULL))
974 /* if the buffer is writable, replace the memory */
976 _replace_memory (buffer, gst_memory_ref (mem));
983 g_critical ("write map requested on non-writable buffer");
988 /* empty buffer, we need to return NULL */
989 GST_DEBUG_OBJECT (buffer, "can't get buffer memory");
998 GST_DEBUG_OBJECT (buffer, "cannot map memory");
999 gst_memory_unref (mem);
1006 * @buffer: a #GstBuffer.
1007 * @info: a #GstMapInfo
1009 * Release the memory previously mapped with gst_buffer_map().
1012 gst_buffer_unmap (GstBuffer * buffer, GstMapInfo * info)
1014 g_return_if_fail (GST_IS_BUFFER (buffer));
1015 g_return_if_fail (info != NULL);
1018 gst_memory_unmap (info->memory, info);
1019 gst_memory_unref (info->memory);
1025 * @buffer: a #GstBuffer.
1026 * @offset: the offset to fill
1027 * @src: the source address
1028 * @size: the size to fill
1030 * Copy @size bytes from @src to @buffer at @offset.
1032 * Returns: The amount of bytes copied. This value can be lower than @size
1033 * when @buffer did not contain enough data.
1036 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
1040 const guint8 *ptr = src;
1042 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1043 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1044 g_return_val_if_fail (src != NULL, 0);
1046 len = GST_BUFFER_MEM_LEN (buffer);
1049 for (i = 0; i < len && left > 0; i++) {
1054 mem = _get_memory (buffer, i, TRUE);
1056 gst_memory_map (mem, &info, GST_MAP_WRITE);
1057 if (info.size > offset) {
1058 /* we have enough */
1059 tocopy = MIN (info.size - offset, left);
1060 memcpy ((guint8 *) info.data + offset, ptr, tocopy);
1065 /* offset past buffer, skip */
1066 offset -= info.size;
1068 gst_memory_unmap (mem, &info);
1074 * gst_buffer_extract:
1075 * @buffer: a #GstBuffer.
1076 * @offset: the offset to extract
1077 * @dest: the destination address
1078 * @size: the size to extract
1080 * Copy @size bytes starting from @offset in @buffer to @dest.
1082 * Returns: The amount of bytes extracted. This value can be lower than @size
1083 * when @buffer did not contain enough data.
1086 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
1091 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1092 g_return_val_if_fail (dest != NULL, 0);
1094 len = GST_BUFFER_MEM_LEN (buffer);
1097 for (i = 0; i < len && left > 0; i++) {
1102 mem = GST_BUFFER_MEM_PTR (buffer, i);
1104 gst_memory_map (mem, &info, GST_MAP_READ);
1105 if (info.size > offset) {
1106 /* we have enough */
1107 tocopy = MIN (info.size - offset, left);
1108 memcpy (ptr, (guint8 *) info.data + offset, tocopy);
1113 /* offset past buffer, skip */
1114 offset -= info.size;
1116 gst_memory_unmap (mem, &info);
1122 * gst_buffer_memcmp:
1123 * @buffer: a #GstBuffer.
1124 * @offset: the offset in @buffer
1125 * @mem: the memory to compare
1126 * @size: the size to compare
1128 * Compare @size bytes starting from @offset in @buffer with the memory in @mem.
1130 * Returns: 0 if the memory is equal.
1133 gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem,
1137 const guint8 *ptr = mem;
1140 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1141 g_return_val_if_fail (mem != NULL, 0);
1143 len = GST_BUFFER_MEM_LEN (buffer);
1145 for (i = 0; i < len && size > 0 && res == 0; i++) {
1150 mem = GST_BUFFER_MEM_PTR (buffer, i);
1152 gst_memory_map (mem, &info, GST_MAP_READ);
1153 if (info.size > offset) {
1154 /* we have enough */
1155 tocmp = MIN (info.size - offset, size);
1156 res = memcmp (ptr, (guint8 *) info.data + offset, tocmp);
1161 /* offset past buffer, skip */
1162 offset -= info.size;
1164 gst_memory_unmap (mem, &info);
1170 * gst_buffer_memset:
1171 * @buffer: a #GstBuffer.
1172 * @offset: the offset in @buffer
1173 * @val: the value to set
1174 * @size: the size to set
1176 * Fill @buf with @size bytes with @val starting from @offset.
1178 * Returns: The amount of bytes filled. This value can be lower than @size
1179 * when @buffer did not contain enough data.
1182 gst_buffer_memset (GstBuffer * buffer, gsize offset, guint8 val, gsize size)
1186 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1187 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1189 len = GST_BUFFER_MEM_LEN (buffer);
1192 for (i = 0; i < len && left > 0; i++) {
1197 mem = _get_memory (buffer, i, TRUE);
1199 gst_memory_map (mem, &info, GST_MAP_WRITE);
1200 if (info.size > offset) {
1201 /* we have enough */
1202 toset = MIN (info.size - offset, left);
1203 memset ((guint8 *) info.data + offset, val, toset);
1207 /* offset past buffer, skip */
1208 offset -= info.size;
1210 gst_memory_unmap (mem, &info);
1216 * gst_buffer_copy_region:
1217 * @parent: a #GstBuffer.
1218 * @flags: the #GstBufferCopyFlags
1219 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
1221 * @size: the size of the new #GstBuffer sub-buffer, in bytes.
1223 * Creates a sub-buffer from @parent at @offset and @size.
1224 * This sub-buffer uses the actual memory space of the parent buffer.
1225 * This function will copy the offset and timestamp fields when the
1226 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
1227 * #GST_BUFFER_OFFSET_NONE.
1228 * If @offset equals 0 and @size equals the total size of @buffer, the
1229 * duration and offset end fields are also copied. If not they will be set
1230 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
1234 * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
1238 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
1239 gsize offset, gsize size)
1243 g_return_val_if_fail (buffer != NULL, NULL);
1245 /* create the new buffer */
1246 copy = gst_buffer_new ();
1248 GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
1249 "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
1251 gst_buffer_copy_into (copy, buffer, flags, offset, size);
1257 _gst_buffer_arr_is_span_fast (GstMemory ** mem[], gsize len[], guint n,
1258 gsize * offset, GstMemory ** parent)
1260 GstMemory *mcur, *mprv;
1261 gboolean have_offset = FALSE;
1265 for (count = 0; count < n; count++) {
1272 for (i = 0; i < clen; i++) {
1278 /* check is memory is contiguous */
1279 if (!gst_memory_is_span (mprv, mcur, &offs))
1286 *parent = mprv->parent;
1297 _gst_buffer_arr_span (GstMemory ** mem[], gsize len[], guint n, gsize offset,
1298 gsize size, gboolean writable)
1300 GstMemory *span, *parent = NULL;
1304 && _gst_buffer_arr_is_span_fast (mem, len, n, &poffset, &parent)) {
1305 if (parent->flags & GST_MEMORY_FLAG_NO_SHARE)
1306 span = gst_memory_copy (parent, offset + poffset, size);
1308 span = gst_memory_share (parent, offset + poffset, size);
1314 span = gst_allocator_alloc (NULL, size, 0);
1315 gst_memory_map (span, &dinfo, GST_MAP_WRITE);
1320 for (count = 0; count < n; count++) {
1322 gsize i, tocopy, clen;
1328 for (i = 0; i < clen && left > 0; i++) {
1329 gst_memory_map (cmem[i], &sinfo, GST_MAP_READ);
1330 tocopy = MIN (sinfo.size, left);
1331 if (tocopy > offset) {
1332 memcpy (ptr, (guint8 *) sinfo.data + offset, tocopy - offset);
1339 gst_memory_unmap (cmem[i], &sinfo);
1342 gst_memory_unmap (span, &dinfo);
1348 * gst_buffer_is_span_fast:
1349 * @buf1: the first #GstBuffer.
1350 * @buf2: the second #GstBuffer.
1352 * Determines whether a gst_buffer_span() can be done without copying
1353 * the contents, that is, whether the data areas are contiguous sub-buffers of
1357 * Returns: TRUE if the buffers are contiguous,
1358 * FALSE if a copy would be required.
1361 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
1366 g_return_val_if_fail (GST_IS_BUFFER (buf1), FALSE);
1367 g_return_val_if_fail (GST_IS_BUFFER (buf2), FALSE);
1368 g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
1369 g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
1371 mem[0] = GST_BUFFER_MEM_ARRAY (buf1);
1372 len[0] = GST_BUFFER_MEM_LEN (buf1);
1373 mem[1] = GST_BUFFER_MEM_ARRAY (buf2);
1374 len[1] = GST_BUFFER_MEM_LEN (buf2);
1376 return _gst_buffer_arr_is_span_fast (mem, len, 2, NULL, NULL);
1381 * @buf1: the first source #GstBuffer to merge.
1382 * @offset: the offset in the first buffer from where the new
1383 * buffer should start.
1384 * @buf2: the second source #GstBuffer to merge.
1385 * @size: the total size of the new buffer.
1387 * Creates a new buffer that consists of part of buf1 and buf2.
1388 * Logically, buf1 and buf2 are concatenated into a single larger
1389 * buffer, and a new buffer is created at the given offset inside
1390 * this space, with a given length.
1392 * If the two source buffers are children of the same larger buffer,
1393 * and are contiguous, the new buffer will be a child of the shared
1394 * parent, and thus no copying is necessary. you can use
1395 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
1399 * Returns: (transfer full): the new #GstBuffer that spans the two source
1400 * buffers, or NULL if the arguments are invalid.
1403 gst_buffer_span (GstBuffer * buf1, gsize offset, GstBuffer * buf2, gsize size)
1408 gsize len[2], len1, len2;
1410 g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
1411 g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
1412 g_return_val_if_fail (buf1->mini_object.refcount > 0, NULL);
1413 g_return_val_if_fail (buf2->mini_object.refcount > 0, NULL);
1414 len1 = gst_buffer_get_size (buf1);
1415 len2 = gst_buffer_get_size (buf2);
1416 g_return_val_if_fail (len1 + len2 > offset, NULL);
1418 size = len1 + len2 - offset;
1420 g_return_val_if_fail (size <= len1 + len2 - offset, NULL);
1422 mem[0] = GST_BUFFER_MEM_ARRAY (buf1);
1423 len[0] = GST_BUFFER_MEM_LEN (buf1);
1424 mem[1] = GST_BUFFER_MEM_ARRAY (buf2);
1425 len[1] = GST_BUFFER_MEM_LEN (buf2);
1427 span = _gst_buffer_arr_span (mem, len, 2, offset, size, FALSE);
1429 newbuf = gst_buffer_new ();
1430 _memory_add (newbuf, -1, span);
1433 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
1435 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
1436 GST_BUFFER_PTS (newbuf) = GST_BUFFER_PTS (buf1);
1437 GST_BUFFER_DTS (newbuf) = GST_BUFFER_DTS (buf1);
1439 /* if we completely merged the two buffers (appended), we can
1440 * calculate the duration too. Also make sure we's not messing with
1441 * invalid DURATIONS */
1442 if (buf1->size + buf2->size == len) {
1443 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
1444 GST_BUFFER_DURATION_IS_VALID (buf2)) {
1446 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
1447 GST_BUFFER_DURATION (buf2);
1449 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
1450 /* add offset_end */
1451 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
1461 * gst_buffer_get_meta:
1462 * @buffer: a #GstBuffer
1463 * @info: a #GstMetaInfo
1465 * Get the metadata for the api in @info on buffer. When there is no such
1466 * metadata, NULL is returned.
1468 * Note that the result metadata might not be of the implementation @info.
1470 * Returns: the metadata for the api in @info on @buffer.
1473 gst_buffer_get_meta (GstBuffer * buffer, const GstMetaInfo * info)
1476 GstMeta *result = NULL;
1478 g_return_val_if_fail (buffer != NULL, NULL);
1479 g_return_val_if_fail (info != NULL, NULL);
1481 /* find GstMeta of the requested API */
1482 for (item = GST_BUFFER_META (buffer); item; item = item->next) {
1483 GstMeta *meta = &item->meta;
1484 if (meta->info->api == info->api) {
1493 * gst_buffer_add_meta:
1494 * @buffer: a #GstBuffer
1495 * @info: a #GstMetaInfo
1496 * @params: params for @info
1498 * Add metadata for @info to @buffer using the parameters in @params.
1500 * Returns: (transfer none): the metadata for the api in @info on @buffer.
1503 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
1507 GstMeta *result = NULL;
1510 g_return_val_if_fail (buffer != NULL, NULL);
1511 g_return_val_if_fail (info != NULL, NULL);
1513 /* create a new slice */
1514 size = ITEM_SIZE (info);
1515 item = g_slice_alloc (size);
1516 result = &item->meta;
1517 result->info = info;
1518 result->flags = GST_META_FLAG_NONE;
1520 GST_CAT_DEBUG (GST_CAT_BUFFER,
1521 "alloc metadata %p (%s) of size %" G_GSIZE_FORMAT, result,
1522 g_type_name (info->type), info->size);
1524 /* call the init_func when needed */
1525 if (info->init_func)
1526 if (!info->init_func (result, params, buffer))
1529 /* and add to the list of metadata */
1530 item->next = GST_BUFFER_META (buffer);
1531 GST_BUFFER_META (buffer) = item;
1537 g_slice_free1 (size, item);
1543 * gst_buffer_remove_meta:
1544 * @buffer: a #GstBuffer
1547 * Remove the metadata for @meta on @buffer.
1549 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
1550 * metadata was on @buffer.
1553 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
1555 GstMetaItem *walk, *prev;
1557 g_return_val_if_fail (buffer != NULL, FALSE);
1558 g_return_val_if_fail (meta != NULL, FALSE);
1560 /* find the metadata and delete */
1561 prev = GST_BUFFER_META (buffer);
1562 for (walk = prev; walk; walk = walk->next) {
1563 GstMeta *m = &walk->meta;
1565 const GstMetaInfo *info = meta->info;
1567 /* remove from list */
1568 if (GST_BUFFER_META (buffer) == walk)
1569 GST_BUFFER_META (buffer) = walk->next;
1571 prev->next = walk->next;
1572 /* call free_func if any */
1573 if (info->free_func)
1574 info->free_func (m, buffer);
1576 /* and free the slice */
1577 g_slice_free1 (ITEM_SIZE (info), walk);
1582 return walk != NULL;
1586 * gst_buffer_iterate_meta:
1587 * @buffer: a #GstBuffer
1588 * @state: an opaque state pointer
1590 * Retrieve the next #GstMeta after @current. If @state points
1591 * to %NULL, the first metadata is returned.
1593 * @state will be updated with an opage state pointer
1595 * Returns: The next #GstMeta or %NULL when there are no more items.
1598 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
1602 g_return_val_if_fail (buffer != NULL, NULL);
1603 g_return_val_if_fail (state != NULL, NULL);
1605 meta = (GstMetaItem **) state;
1607 /* state NULL, move to first item */
1608 *meta = GST_BUFFER_META (buffer);
1610 /* state !NULL, move to next item in list */
1611 *meta = (*meta)->next;
1614 return &(*meta)->meta;
1620 * gst_buffer_foreach_meta:
1621 * @buffer: a #GstBuffer
1622 * @func: (scope call): a #GstBufferForeachMetaFunc to call
1623 * @user_data: (closure): user data passed to @func
1625 * Call @func with @user_data for each meta in @buffer.
1627 * @func can modify the passed meta pointer or its contents. The return value
1628 * of @func define if this function returns or if the remaining metadata items
1629 * in the buffer should be skipped.
1632 gst_buffer_foreach_meta (GstBuffer * buffer, GstBufferForeachMetaFunc func,
1635 GstMetaItem *walk, *prev, *next;
1637 g_return_if_fail (buffer != NULL);
1638 g_return_if_fail (func != NULL);
1640 /* find the metadata and delete */
1641 prev = GST_BUFFER_META (buffer);
1642 for (walk = prev; walk; walk = next) {
1646 m = new = &walk->meta;
1649 res = func (buffer, &new, user_data);
1652 const GstMetaInfo *info = m->info;
1654 GST_CAT_DEBUG (GST_CAT_BUFFER, "remove metadata %p (%s)", m,
1655 g_type_name (info->type));
1657 /* remove from list */
1658 if (GST_BUFFER_META (buffer) == walk)
1659 GST_BUFFER_META (buffer) = next;
1663 /* call free_func if any */
1664 if (info->free_func)
1665 info->free_func (m, buffer);
1667 /* and free the slice */
1668 g_slice_free1 (ITEM_SIZE (info), walk);