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
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 set the size of the
36 * buffer data. The following example creates a buffer that can hold a given
37 * video frame with a given width, height and bits per plane.
39 * <title>Creating a buffer for a video frame</title>
42 * gint size, width, height, bpp;
44 * size = width * height * bpp;
45 * buffer = gst_buffer_new ();
46 * GST_BUFFER_SIZE (buffer) = size;
47 * GST_BUFFER_MALLOCDATA (buffer) = g_malloc (size);
48 * GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
53 * Alternatively, use gst_buffer_new_and_alloc()
54 * to create a buffer with preallocated data of a given size.
56 * The data pointed to by the buffer can be retrieved with the GST_BUFFER_DATA()
57 * macro. The size of the data can be found with GST_BUFFER_SIZE(). For buffers
58 * of size 0, the data pointer is undefined (usually NULL) and should never be used.
60 * If an element knows what pad you will push the buffer out on, it should use
61 * gst_pad_alloc_buffer() instead to create a buffer. This allows downstream
62 * elements to provide special buffers to write in, like hardware buffers.
64 * A buffer has a pointer to a #GstCaps describing the media type of the data
65 * in the buffer. Attach caps to the buffer with gst_buffer_set_caps(); this
66 * is typically done before pushing out a buffer using gst_pad_push() so that
67 * the downstream element knows the type of the buffer.
69 * A buffer will usually have a timestamp, and a duration, but neither of these
70 * are guaranteed (they may be set to #GST_CLOCK_TIME_NONE). Whenever a
71 * meaningful value can be given for these, they should be set. The timestamp
72 * and duration are measured in nanoseconds (they are #GstClockTime values).
74 * A buffer can also have one or both of a start and an end offset. These are
75 * media-type specific. For video buffers, the start offset will generally be
76 * the frame number. For audio buffers, it will be the number of samples
77 * produced so far. For compressed data, it could be the byte offset in a
78 * source or destination file. Likewise, the end offset will be the offset of
79 * the end of the buffer. These can only be meaningfully interpreted if you
80 * know the media type of the buffer (the #GstCaps set on it). Either or both
81 * can be set to #GST_BUFFER_OFFSET_NONE.
83 * gst_buffer_ref() is used to increase the refcount of a buffer. This must be
84 * done when you want to keep a handle to the buffer after pushing it to the
87 * To efficiently create a smaller buffer out of an existing one, you can
88 * use gst_buffer_copy_region().
90 * If a plug-in wants to modify the buffer data or metadata in-place, it should
91 * first obtain a buffer that is safe to modify by using
92 * gst_buffer_make_writable(). This function is optimized so that a copy will
93 * only be made when it is necessary.
95 * Several flags of the buffer can be set and unset with the
96 * GST_BUFFER_FLAG_SET() and GST_BUFFER_FLAG_UNSET() macros. Use
97 * GST_BUFFER_FLAG_IS_SET() to test if a certain #GstBufferFlag is set.
99 * Buffers can be efficiently merged into a larger buffer with
100 * gst_buffer_span(), which avoids memory copies when the gst_buffer_is_span_fast()
101 * function returns TRUE.
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 data pointed to by the buffer is unreffed as
110 * Last reviewed on March 30, 2011 (0.11.0)
112 #include "gst_private.h"
121 #include "gstbuffer.h"
122 #include "gstbufferpool.h"
124 #include "gstutils.h"
125 #include "gstminiobject.h"
126 #include "gstversion.h"
128 GType _gst_buffer_type = 0;
130 static GstMemory *_gst_buffer_arr_span (GstMemory ** mem[], gsize len[],
131 guint n, gsize offset, gsize size, gboolean writable);
133 typedef struct _GstMetaItem GstMetaItem;
140 #define ITEM_SIZE(info) ((info)->size + sizeof (GstMetaItem))
142 #define GST_BUFFER_MEM_MAX 16
144 #define GST_BUFFER_MEM_LEN(b) (((GstBufferImpl *)(b))->len)
145 #define GST_BUFFER_MEM_ARRAY(b) (((GstBufferImpl *)(b))->mem)
146 #define GST_BUFFER_MEM_PTR(b,i) (((GstBufferImpl *)(b))->mem[i])
147 #define GST_BUFFER_BUFMEM(b) (((GstBufferImpl *)(b))->bufmem)
148 #define GST_BUFFER_META(b) (((GstBufferImpl *)(b))->item)
154 /* the memory blocks */
156 GstMemory *mem[GST_BUFFER_MEM_MAX];
158 /* memory of the buffer when allocated from 1 chunk */
161 /* FIXME, make metadata allocation more efficient by using part of the
167 _span_memory (GstBuffer * buffer, gsize offset, gsize size, gboolean writable)
169 GstMemory *span, **mem[1];
172 /* not enough room, span buffers */
173 mem[0] = GST_BUFFER_MEM_ARRAY (buffer);
174 len[0] = GST_BUFFER_MEM_LEN (buffer);
177 size = gst_buffer_get_size (buffer);
179 span = _gst_buffer_arr_span (mem, len, 1, offset, size, writable);
185 _replace_memory (GstBuffer * buffer, GstMemory * mem)
189 /* unref old buffers */
190 len = GST_BUFFER_MEM_LEN (buffer);
191 for (i = 0; i < len; i++)
192 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
194 /* replace with single spanned buffer */
195 GST_BUFFER_MEM_PTR (buffer, 0) = mem;
196 GST_BUFFER_MEM_LEN (buffer) = 1;
200 _memory_add (GstBuffer * buffer, guint idx, GstMemory * mem)
202 guint i, len = GST_BUFFER_MEM_LEN (buffer);
204 if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) {
205 /* too many buffer, span them. */
206 /* FIXME, there is room for improvement here: We could only try to merge
207 * 2 buffers to make some room. If we can't efficiently merge 2 buffers we
208 * could try to only merge the two smallest buffers to avoid memcpy, etc. */
209 _replace_memory (buffer, _span_memory (buffer, 0, -1, FALSE));
210 /* we now have 1 single spanned buffer */
217 for (i = len; i > idx; i--) {
218 /* move buffers to insert, FIXME, we need to insert first and then merge */
219 GST_BUFFER_MEM_PTR (buffer, i) = GST_BUFFER_MEM_PTR (buffer, i - 1);
221 /* and insert the new buffer */
222 GST_BUFFER_MEM_PTR (buffer, idx) = mem;
223 GST_BUFFER_MEM_LEN (buffer) = len + 1;
226 GST_DEFINE_MINI_OBJECT_TYPE (GstBuffer, gst_buffer);
229 _priv_gst_buffer_initialize (void)
231 _gst_buffer_type = gst_buffer_get_type ();
235 * gst_buffer_copy_into:
236 * @dest: a destination #GstBuffer
237 * @src: a source #GstBuffer
238 * @flags: flags indicating what metadata fields should be copied.
239 * @offset: offset to copy from
240 * @size: total size to copy. If -1, all data is copied.
242 * Copies the information from @src into @dest.
244 * @flags indicate which fields will be copied.
247 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
248 GstBufferCopyFlags flags, gsize offset, gsize size)
253 g_return_if_fail (dest != NULL);
254 g_return_if_fail (src != NULL);
256 /* nothing to copy if the buffers are the same */
257 if (G_UNLIKELY (dest == src))
260 g_return_if_fail (gst_buffer_is_writable (dest));
262 bufsize = gst_buffer_get_size (src);
263 g_return_if_fail (bufsize >= offset);
265 size = bufsize - offset;
266 g_return_if_fail (bufsize >= offset + size);
268 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p, offset %" G_GSIZE_FORMAT
269 "-%" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT, src, dest, offset, size,
272 if (flags & GST_BUFFER_COPY_FLAGS) {
274 GST_MINI_OBJECT_FLAGS (dest) = GST_MINI_OBJECT_FLAGS (src);
277 if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
279 GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
280 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
281 if (size == bufsize) {
282 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
283 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
286 GST_BUFFER_TIMESTAMP (dest) = GST_CLOCK_TIME_NONE;
287 GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
288 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
289 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
293 if (flags & GST_BUFFER_COPY_MEMORY) {
295 gsize skip, left, len, i, bsize;
297 len = GST_BUFFER_MEM_LEN (src);
301 /* copy and make regions of the memory */
302 for (i = 0; i < len && left > 0; i++) {
303 mem = GST_BUFFER_MEM_PTR (src, i);
304 bsize = gst_memory_get_sizes (mem, NULL, NULL);
307 /* don't copy buffer */
312 tocopy = MIN (bsize - skip, left);
313 if (mem->flags & GST_MEMORY_FLAG_NO_SHARE) {
314 /* no share, always copy then */
315 mem = gst_memory_copy (mem, skip, tocopy);
317 } else if (tocopy < bsize) {
318 /* we need to clip something */
319 mem = gst_memory_share (mem, skip, tocopy);
322 mem = gst_memory_ref (mem);
324 _memory_add (dest, -1, mem);
328 if (flags & GST_BUFFER_COPY_MERGE) {
329 _replace_memory (dest, _span_memory (dest, 0, size, FALSE));
333 for (walk = GST_BUFFER_META (src); walk; walk = walk->next) {
334 GstMeta *meta = &walk->meta;
335 const GstMetaInfo *info = meta->info;
338 info->copy_func (dest, meta, src, offset, size);
343 _gst_buffer_copy (GstBuffer * buffer)
347 g_return_val_if_fail (buffer != NULL, NULL);
349 /* create a fresh new buffer */
350 copy = gst_buffer_new ();
352 /* we simply copy everything from our parent */
353 gst_buffer_copy_into (copy, buffer, GST_BUFFER_COPY_ALL, 0, -1);
358 /* the default dispose function revives the buffer and returns it to the
359 * pool when there is a pool */
361 _gst_buffer_dispose (GstBuffer * buffer)
365 /* no pool, do free */
366 if ((pool = buffer->pool) == NULL)
369 /* keep the buffer alive */
370 gst_buffer_ref (buffer);
371 /* return the buffer to the pool */
372 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
373 gst_buffer_pool_release_buffer (pool, buffer);
379 _gst_buffer_free (GstBuffer * buffer)
381 GstMetaItem *walk, *next;
385 g_return_if_fail (buffer != NULL);
387 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
390 for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
391 GstMeta *meta = &walk->meta;
392 const GstMetaInfo *info = meta->info;
394 /* call free_func if any */
396 info->free_func (meta, buffer);
399 /* and free the slice */
400 g_slice_free1 (ITEM_SIZE (info), walk);
403 /* get the size, when unreffing the memory, we could also unref the buffer
405 msize = GST_MINI_OBJECT_SIZE (buffer);
407 /* free our memory */
408 len = GST_BUFFER_MEM_LEN (buffer);
409 for (i = 0; i < len; i++)
410 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
412 /* we set msize to 0 when the buffer is part of the memory block */
414 g_slice_free1 (msize, buffer);
416 gst_memory_unref (GST_BUFFER_BUFMEM (buffer));
420 gst_buffer_init (GstBufferImpl * buffer, gsize size)
422 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), _gst_buffer_type, size);
424 buffer->buffer.mini_object.copy =
425 (GstMiniObjectCopyFunction) _gst_buffer_copy;
426 buffer->buffer.mini_object.dispose =
427 (GstMiniObjectDisposeFunction) _gst_buffer_dispose;
428 buffer->buffer.mini_object.free =
429 (GstMiniObjectFreeFunction) _gst_buffer_free;
431 GST_BUFFER (buffer)->pool = NULL;
432 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
433 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
434 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
435 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
437 GST_BUFFER_MEM_LEN (buffer) = 0;
438 GST_BUFFER_META (buffer) = NULL;
444 * Creates a newly allocated buffer without any data.
448 * Returns: (transfer full): the new #GstBuffer.
451 gst_buffer_new (void)
453 GstBufferImpl *newbuf;
455 newbuf = g_slice_new (GstBufferImpl);
456 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
458 gst_buffer_init (newbuf, sizeof (GstBufferImpl));
460 return GST_BUFFER_CAST (newbuf);
464 * gst_buffer_new_allocate:
465 * @allocator: the #GstAllocator to use
466 * @size: the size in bytes of the new buffer's data.
467 * @align: the alignment of the buffer memory
469 * Tries to create a newly allocated buffer with data of the given size and
470 * alignment from @allocator. If the requested amount of memory can't be
471 * allocated, NULL will be returned. The allocated buffer memory is not cleared.
473 * When @allocator is NULL, the default memory allocator will be used.
475 * Allocator buffer memory will be aligned to multiples of (@align + 1) bytes.
477 * Note that when @size == 0, the buffer will not have memory associated with it.
481 * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
485 gst_buffer_new_allocate (const GstAllocator * allocator, gsize size,
497 mem = gst_allocator_alloc (allocator, size, align);
498 if (G_UNLIKELY (mem == NULL))
504 newbuf = gst_buffer_new ();
507 _memory_add (newbuf, -1, mem);
509 GST_CAT_LOG (GST_CAT_BUFFER,
510 "new buffer %p of size %" G_GSIZE_FORMAT " from allocator %p", newbuf,
515 asize = sizeof (GstBufferImpl) + size;
516 data = g_slice_alloc (asize);
517 if (G_UNLIKELY (data == NULL))
520 newbuf = GST_BUFFER_CAST (data);
522 gst_buffer_init ((GstBufferImpl *) data, asize);
524 mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL,
526 _memory_add (newbuf, -1, mem);
531 /* allocate memory and buffer, it might be interesting to do this but there
532 * are many complications. We need to keep the memory mapped to access the
533 * buffer fields and the memory for the buffer might be just very slow. We
534 * also need to do some more magic to get the alignment right. */
535 asize = sizeof (GstBufferImpl) + size;
536 mem = gst_allocator_alloc (allocator, asize, align);
537 if (G_UNLIKELY (mem == NULL))
540 /* map the data part and init the buffer in it, set the buffer size to 0 so
541 * that a finalize won't free the buffer */
542 data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
543 gst_buffer_init ((GstBufferImpl *) data, 0);
544 gst_memory_unmap (mem, data, asize);
546 /* strip off the buffer */
547 gst_memory_resize (mem, sizeof (GstBufferImpl), size);
549 newbuf = GST_BUFFER_CAST (data);
550 GST_BUFFER_BUFMEM (newbuf) = mem;
553 _memory_add (newbuf, -1, gst_memory_ref (mem));
561 GST_CAT_WARNING (GST_CAT_BUFFER,
562 "failed to allocate %" G_GSIZE_FORMAT " bytes", size);
568 * gst_buffer_new_wrapped_full:
569 * @data: data to wrap
570 * @free_func: function to free @data
571 * @offset: offset in @data of valid data
572 * @size: size of valid data in @data starting at @offset
574 * Creates a new buffer that wraps the given @data. Valid data is set
575 * to start at @offset and up to @size. If no @free_func is provided,
576 * buffer memory is marked READONLY.
580 * Returns: (transfer full): a new #GstBuffer
583 gst_buffer_new_wrapped_full (gpointer data, GFreeFunc free_func, gsize offset,
588 g_return_val_if_fail (offset <= size, NULL);
590 newbuf = gst_buffer_new ();
591 gst_buffer_take_memory (newbuf, -1,
592 gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
593 data, free_func, offset + size, offset, size));
599 * gst_buffer_new_wrapped:
600 * @data: data to wrap
601 * @size: allocated size of @data
603 * Creates a new buffer that wraps the given @data. The memory will be freed
604 * with g_free and will be marked writable.
608 * Returns: (transfer full): a new #GstBuffer
611 gst_buffer_new_wrapped (gpointer data, gsize size)
613 return gst_buffer_new_wrapped_full (data, g_free, 0, size);
617 * gst_buffer_n_memory:
618 * @buffer: a #GstBuffer.
620 * Get the amount of memory blocks that this buffer has.
622 * Returns: (transfer full): the amount of memory block in this buffer.
625 gst_buffer_n_memory (GstBuffer * buffer)
627 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
629 return GST_BUFFER_MEM_LEN (buffer);
633 * gst_buffer_take_memory:
634 * @buffer: a #GstBuffer.
635 * @idx: the index to add the memory at, or -1 to append it to the end
636 * @mem: (transfer full): a #GstMemory.
638 * Add the memory block @mem to @buffer at @idx. This function takes ownership
639 * of @mem and thus doesn't increase its refcount.
642 gst_buffer_take_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
644 g_return_if_fail (GST_IS_BUFFER (buffer));
645 g_return_if_fail (gst_buffer_is_writable (buffer));
646 g_return_if_fail (mem != NULL);
647 g_return_if_fail (idx == -1 ||
648 (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)));
650 _memory_add (buffer, idx, mem);
654 _get_memory (GstBuffer * buffer, guint idx, gboolean write)
658 mem = GST_BUFFER_MEM_PTR (buffer, idx);
660 if (G_UNLIKELY (write && !GST_MEMORY_IS_WRITABLE (mem))) {
662 GST_CAT_LOG (GST_CAT_BUFFER,
663 "making writable copy of memory %p in buffer %p", mem, buffer);
664 /* replace with a writable copy */
665 copy = gst_memory_copy (mem, 0, -1);
666 GST_BUFFER_MEM_PTR (buffer, idx) = copy;
667 gst_memory_unref (mem);
674 * gst_buffer_peek_memory:
675 * @buffer: a #GstBuffer.
677 * @flags: #GstMapFlags
679 * Get the memory block in @buffer at @idx for memory access in @flags.
680 * This function does not return a refcount to the memory block. The memory
681 * block stays valid for as long as the caller has a valid reference to @buffer.
683 * @buffer should be writable when @flags contains #GST_MAP_WRITE. If the memory
684 * at @idx is not writable, a new writable copy will be installed in @buffer and
687 * Returns: a #GstMemory at @idx.
690 gst_buffer_peek_memory (GstBuffer * buffer, guint idx, GstMapFlags flags)
695 write = (flags & GST_MAP_WRITE) != 0;
697 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
698 g_return_val_if_fail (idx < GST_BUFFER_MEM_LEN (buffer), NULL);
700 /* check if we can write when asked for write access */
701 if (G_UNLIKELY (write && !gst_buffer_is_writable (buffer)))
704 mem = _get_memory (buffer, idx, write);
711 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
717 * gst_buffer_remove_memory_range:
718 * @buffer: a #GstBuffer.
722 * Remove @len memory blocks in @buffer starting from @idx.
724 * @length can be -1, in which case all memory starting from @idx is removed.
727 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, guint length)
731 g_return_if_fail (GST_IS_BUFFER (buffer));
732 g_return_if_fail (gst_buffer_is_writable (buffer));
734 len = GST_BUFFER_MEM_LEN (buffer);
736 g_return_if_fail (idx < len);
741 for (i = idx; i < end; i++)
742 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
745 g_memmove (&GST_BUFFER_MEM_PTR (buffer, idx),
746 &GST_BUFFER_MEM_PTR (buffer, end), (len - end) * sizeof (gpointer));
748 GST_BUFFER_MEM_LEN (buffer) = len - length;
752 * gst_buffer_get_sizes:
753 * @buffer: a #GstBuffer.
754 * @offset: a pointer to the offset
755 * @maxsize: a pointer to the maxsize
757 * Get the total size of all memory blocks in @buffer.
759 * When not %NULL, @offset will contain the offset of the data in the first
760 * memory block in @buffer and @maxsize will contain the sum of the size
761 * and @offset and the amount of extra padding on the last memory block.
762 * @offset and @maxsize can be used to resize the buffer with
763 * gst_buffer_resize().
765 * Returns: the total size of the memory in @buffer.
768 gst_buffer_get_sizes (GstBuffer * buffer, gsize * offset, gsize * maxsize)
774 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
776 len = GST_BUFFER_MEM_LEN (buffer);
778 if (G_LIKELY (len == 1)) {
780 mem = GST_BUFFER_MEM_PTR (buffer, 0);
781 size = gst_memory_get_sizes (mem, offset, maxsize);
786 size = offs = extra = 0;
787 for (i = 0; i < len; i++) {
790 mem = GST_BUFFER_MEM_PTR (buffer, i);
791 s = gst_memory_get_sizes (mem, &o, &ms);
795 /* first size, take accumulated data before as the offset */
799 /* save the amount of data after this block */
800 extra = ms - (o + s);
802 /* empty block, add as extra */
809 *maxsize = offs + size + extra;
816 * @buffer: a #GstBuffer.
817 * @offset: the offset adjustement
818 * @size: the new size
820 * Set the total size of the buffer
823 gst_buffer_resize (GstBuffer * buffer, gssize offset, gsize size)
827 gsize bsize, bufsize, bufoffs, bufmax;
830 g_return_if_fail (gst_buffer_is_writable (buffer));
832 bufsize = gst_buffer_get_sizes (buffer, &bufoffs, &bufmax);
834 GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSIZE_FORMAT
835 " size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%" G_GSIZE_FORMAT,
836 buffer, offset, size, bufsize, bufoffs, bufmax);
838 /* we can't go back further than the current offset or past the end of the
840 g_return_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
841 && bufoffs + offset <= bufmax));
843 g_return_if_fail (bufsize >= offset);
844 size = bufsize - offset;
846 g_return_if_fail (bufmax >= bufoffs + offset + size);
848 len = GST_BUFFER_MEM_LEN (buffer);
851 for (i = 0; i < len; i++) {
854 mem = GST_BUFFER_MEM_PTR (buffer, i);
855 bsize = gst_memory_get_sizes (mem, NULL, NULL);
858 /* last buffer always gets resized to the remaining size */
861 /* shrink buffers before the offset */
862 else if ((gssize) bsize <= offset) {
864 noffs = offset - bsize;
867 /* clip other buffers */
869 left = MIN (bsize - offset, size);
871 if (offset != 0 || left != bsize) {
872 /* we need to clip something */
873 if (GST_MEMORY_IS_WRITABLE (mem)) {
874 gst_memory_resize (mem, offset, left);
878 if (mem->flags & GST_MEMORY_FLAG_NO_SHARE)
879 tmp = gst_memory_copy (mem, offset, left);
881 tmp = gst_memory_share (mem, offset, left);
883 gst_memory_unref (mem);
890 GST_BUFFER_MEM_PTR (buffer, i) = mem;
896 * @buffer: a #GstBuffer.
897 * @size: a location for the size
898 * @maxsize: a location for the max size
899 * @flags: flags for the mapping
901 * This function return a pointer to the memory in @buffer. @flags describe the
902 * desired access of the memory. When @flags is #GST_MAP_WRITE, @buffer should
903 * be writable (as returned from gst_buffer_is_writable()).
905 * @size and @maxsize will contain the current valid number of bytes in the
906 * returned memory area and the total maximum mount of bytes available in the
907 * returned memory area respectively.
909 * When @buffer is writable but the memory isn't, a writable copy will
910 * automatically be created and returned. The readonly copy of the buffer memory
911 * will then also be replaced with this writable copy.
913 * When the buffer contains multiple memory blocks, the returned pointer will be
914 * a concatenation of the memory blocks.
916 * Returns: a pointer to the memory for the buffer.
919 gst_buffer_map (GstBuffer * buffer, gsize * size, gsize * maxsize,
925 gboolean write, writable;
927 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
929 write = (flags & GST_MAP_WRITE) != 0;
930 writable = gst_buffer_is_writable (buffer);
932 /* check if we can write when asked for write access */
933 if (G_UNLIKELY (write && !writable))
936 len = GST_BUFFER_MEM_LEN (buffer);
938 if (G_UNLIKELY (len == 0)) {
939 /* no memory, return immediately */
947 if (G_LIKELY (len == 1)) {
948 /* we can take the first one */
949 mem = GST_BUFFER_MEM_PTR (buffer, 0);
951 /* we need to span memory */
953 /* if we can write, we can change the memory with the spanned
955 mem = _span_memory (buffer, 0, -1, write);
956 _replace_memory (buffer, mem);
960 /* extract all data in new memory, FIXME slow!! */
961 bsize = gst_buffer_get_size (buffer);
963 data = g_malloc (bsize);
964 gst_buffer_extract (buffer, 0, data, bsize);
973 if (G_UNLIKELY (write && !GST_MEMORY_IS_WRITABLE (mem))) {
975 /* replace with a writable copy */
976 copy = gst_memory_copy (mem, 0, -1);
977 GST_BUFFER_MEM_PTR (buffer, 0) = copy;
978 gst_memory_unref (mem);
982 data = gst_memory_map (mem, size, maxsize, flags);
989 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
996 * @buffer: a #GstBuffer.
997 * @data: the previously mapped data
998 * @size: the size of @data
1000 * Release the memory previously mapped with gst_buffer_map().
1002 * Returns: #TRUE on success. #FALSE can be returned when the new size is larger
1003 * than the maxsize of the memory.
1006 gst_buffer_unmap (GstBuffer * buffer, gpointer data, gsize size)
1011 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1013 len = GST_BUFFER_MEM_LEN (buffer);
1015 if (G_LIKELY (len == 1)) {
1016 GstMemory *mem = GST_BUFFER_MEM_PTR (buffer, 0);
1018 result = gst_memory_unmap (mem, data, size);
1020 /* this must have been from read-only access. After _map, the buffer either
1021 * only contains 1 memory block or it allocated memory to join memory
1022 * blocks. It's not allowed to add buffers between _map and _unmap. */
1031 * @buffer: a #GstBuffer.
1032 * @offset: the offset to fill
1033 * @src: the source address
1034 * @size: the size to fill
1036 * Copy @size bytes from @src to @buffer at @offset.
1038 * Returns: The amount of bytes copied. This value can be lower than @size
1039 * when @buffer did not contain enough data.
1042 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
1046 const guint8 *ptr = src;
1048 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1049 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1050 g_return_val_if_fail (src != NULL, 0);
1052 len = GST_BUFFER_MEM_LEN (buffer);
1055 for (i = 0; i < len && left > 0; i++) {
1057 gsize ssize, tocopy;
1060 mem = _get_memory (buffer, i, TRUE);
1062 data = gst_memory_map (mem, &ssize, NULL, GST_MAP_WRITE);
1063 if (ssize > offset) {
1064 /* we have enough */
1065 tocopy = MIN (ssize - offset, left);
1066 memcpy (data + offset, ptr, tocopy);
1071 /* offset past buffer, skip */
1074 gst_memory_unmap (mem, data, ssize);
1080 * gst_buffer_extract:
1081 * @buffer: a #GstBuffer.
1082 * @offset: the offset to extract
1083 * @dest: the destination address
1084 * @size: the size to extract
1086 * Copy @size bytes starting from @offset in @buffer to @dest.
1088 * Returns: The amount of bytes extracted. This value can be lower than @size
1089 * when @buffer did not contain enough data.
1092 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
1097 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1098 g_return_val_if_fail (dest != NULL, 0);
1100 len = GST_BUFFER_MEM_LEN (buffer);
1103 for (i = 0; i < len && left > 0; i++) {
1105 gsize ssize, tocopy;
1108 mem = GST_BUFFER_MEM_PTR (buffer, i);
1110 data = gst_memory_map (mem, &ssize, NULL, GST_MAP_READ);
1111 if (ssize > offset) {
1112 /* we have enough */
1113 tocopy = MIN (ssize - offset, left);
1114 memcpy (ptr, data + offset, tocopy);
1119 /* offset past buffer, skip */
1122 gst_memory_unmap (mem, data, ssize);
1128 * gst_buffer_memcmp:
1129 * @buffer: a #GstBuffer.
1130 * @offset: the offset in @buffer
1131 * @mem: the memory to compare
1132 * @size: the size to compare
1134 * Compare @size bytes starting from @offset in @buffer with the memory in @mem.
1136 * Returns: 0 if the memory is equal.
1139 gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem,
1143 const guint8 *ptr = mem;
1146 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1147 g_return_val_if_fail (mem != NULL, 0);
1149 len = GST_BUFFER_MEM_LEN (buffer);
1151 for (i = 0; i < len && size > 0 && res == 0; i++) {
1156 mem = GST_BUFFER_MEM_PTR (buffer, i);
1158 data = gst_memory_map (mem, &ssize, NULL, GST_MAP_READ);
1159 if (ssize > offset) {
1160 /* we have enough */
1161 tocmp = MIN (ssize - offset, size);
1162 res = memcmp (ptr, data + offset, tocmp);
1167 /* offset past buffer, skip */
1170 gst_memory_unmap (mem, data, ssize);
1176 * gst_buffer_memset:
1177 * @buffer: a #GstBuffer.
1178 * @offset: the offset in @buffer
1179 * @val: the value to set
1180 * @size: the size to set
1182 * Fill @buf with @size bytes with @val starting from @offset.
1184 * Returns: The amount of bytes filled. This value can be lower than @size
1185 * when @buffer did not contain enough data.
1188 gst_buffer_memset (GstBuffer * buffer, gsize offset, guint8 val, gsize size)
1192 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1193 g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1195 len = GST_BUFFER_MEM_LEN (buffer);
1198 for (i = 0; i < len && left > 0; i++) {
1203 mem = GST_BUFFER_MEM_PTR (buffer, i);
1205 data = gst_memory_map (mem, &ssize, NULL, GST_MAP_WRITE);
1206 if (ssize > offset) {
1207 /* we have enough */
1208 toset = MIN (ssize - offset, left);
1209 memset (data + offset, val, toset);
1213 /* offset past buffer, skip */
1216 gst_memory_unmap (mem, data, ssize);
1222 * gst_buffer_copy_region:
1223 * @parent: a #GstBuffer.
1224 * @flags: the #GstBufferCopyFlags
1225 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
1227 * @size: the size of the new #GstBuffer sub-buffer, in bytes.
1229 * Creates a sub-buffer from @parent at @offset and @size.
1230 * This sub-buffer uses the actual memory space of the parent buffer.
1231 * This function will copy the offset and timestamp fields when the
1232 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
1233 * #GST_BUFFER_OFFSET_NONE.
1234 * If @offset equals 0 and @size equals the total size of @buffer, the
1235 * duration and offset end fields are also copied. If not they will be set
1236 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
1240 * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
1244 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
1245 gsize offset, gsize size)
1249 g_return_val_if_fail (buffer != NULL, NULL);
1251 /* create the new buffer */
1252 copy = gst_buffer_new ();
1254 GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
1255 "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
1257 gst_buffer_copy_into (copy, buffer, flags, offset, size);
1263 _gst_buffer_arr_is_span_fast (GstMemory ** mem[], gsize len[], guint n,
1264 gsize * offset, GstMemory ** parent)
1266 GstMemory *mcur, *mprv;
1267 gboolean have_offset = FALSE;
1271 for (count = 0; count < n; count++) {
1278 for (i = 0; i < clen; i++) {
1284 /* check is memory is contiguous */
1285 if (!gst_memory_is_span (mprv, mcur, &offs))
1292 *parent = mprv->parent;
1303 _gst_buffer_arr_span (GstMemory ** mem[], gsize len[], guint n, gsize offset,
1304 gsize size, gboolean writable)
1306 GstMemory *span, *parent = NULL;
1310 && _gst_buffer_arr_is_span_fast (mem, len, n, &poffset, &parent)) {
1311 if (parent->flags & GST_MEMORY_FLAG_NO_SHARE)
1312 span = gst_memory_copy (parent, offset + poffset, size);
1314 span = gst_memory_share (parent, offset + poffset, size);
1319 span = gst_allocator_alloc (NULL, size, 0);
1320 dest = gst_memory_map (span, NULL, NULL, GST_MAP_WRITE);
1325 for (count = 0; count < n; count++) {
1326 gsize i, tocopy, clen, ssize;
1333 for (i = 0; i < clen && left > 0; i++) {
1334 src = gst_memory_map (cmem[i], &ssize, NULL, GST_MAP_READ);
1335 tocopy = MIN (ssize, left);
1336 if (tocopy > offset) {
1337 memcpy (ptr, src + offset, tocopy - offset);
1344 gst_memory_unmap (cmem[i], src, ssize);
1347 gst_memory_unmap (span, dest, size);
1353 * gst_buffer_is_span_fast:
1354 * @buf1: the first #GstBuffer.
1355 * @buf2: the second #GstBuffer.
1357 * Determines whether a gst_buffer_span() can be done without copying
1358 * the contents, that is, whether the data areas are contiguous sub-buffers of
1362 * Returns: TRUE if the buffers are contiguous,
1363 * FALSE if a copy would be required.
1366 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
1371 g_return_val_if_fail (GST_IS_BUFFER (buf1), FALSE);
1372 g_return_val_if_fail (GST_IS_BUFFER (buf2), FALSE);
1373 g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
1374 g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
1376 mem[0] = GST_BUFFER_MEM_ARRAY (buf1);
1377 len[0] = GST_BUFFER_MEM_LEN (buf1);
1378 mem[1] = GST_BUFFER_MEM_ARRAY (buf2);
1379 len[1] = GST_BUFFER_MEM_LEN (buf2);
1381 return _gst_buffer_arr_is_span_fast (mem, len, 2, NULL, NULL);
1386 * @buf1: the first source #GstBuffer to merge.
1387 * @offset: the offset in the first buffer from where the new
1388 * buffer should start.
1389 * @buf2: the second source #GstBuffer to merge.
1390 * @size: the total size of the new buffer.
1392 * Creates a new buffer that consists of part of buf1 and buf2.
1393 * Logically, buf1 and buf2 are concatenated into a single larger
1394 * buffer, and a new buffer is created at the given offset inside
1395 * this space, with a given length.
1397 * If the two source buffers are children of the same larger buffer,
1398 * and are contiguous, the new buffer will be a child of the shared
1399 * parent, and thus no copying is necessary. you can use
1400 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
1404 * Returns: (transfer full): the new #GstBuffer that spans the two source
1405 * buffers, or NULL if the arguments are invalid.
1408 gst_buffer_span (GstBuffer * buf1, gsize offset, GstBuffer * buf2, gsize size)
1413 gsize len[2], len1, len2;
1415 g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
1416 g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
1417 g_return_val_if_fail (buf1->mini_object.refcount > 0, NULL);
1418 g_return_val_if_fail (buf2->mini_object.refcount > 0, NULL);
1419 len1 = gst_buffer_get_size (buf1);
1420 len2 = gst_buffer_get_size (buf2);
1421 g_return_val_if_fail (len1 + len2 > offset, NULL);
1423 size = len1 + len2 - offset;
1425 g_return_val_if_fail (size <= len1 + len2 - offset, NULL);
1427 mem[0] = GST_BUFFER_MEM_ARRAY (buf1);
1428 len[0] = GST_BUFFER_MEM_LEN (buf1);
1429 mem[1] = GST_BUFFER_MEM_ARRAY (buf2);
1430 len[1] = GST_BUFFER_MEM_LEN (buf2);
1432 span = _gst_buffer_arr_span (mem, len, 2, offset, size, FALSE);
1434 newbuf = gst_buffer_new ();
1435 _memory_add (newbuf, -1, span);
1438 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
1440 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
1441 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
1443 /* if we completely merged the two buffers (appended), we can
1444 * calculate the duration too. Also make sure we's not messing with
1445 * invalid DURATIONS */
1446 if (buf1->size + buf2->size == len) {
1447 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
1448 GST_BUFFER_DURATION_IS_VALID (buf2)) {
1450 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
1451 GST_BUFFER_DURATION (buf2);
1453 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
1454 /* add offset_end */
1455 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
1465 * gst_buffer_get_meta:
1466 * @buffer: a #GstBuffer
1467 * @info: a #GstMetaInfo
1469 * Get the metadata for the api in @info on buffer. When there is no such
1470 * metadata, NULL is returned.
1472 * Note that the result metadata might not be of the implementation @info.
1474 * Returns: the metadata for the api in @info on @buffer.
1477 gst_buffer_get_meta (GstBuffer * buffer, const GstMetaInfo * info)
1480 GstMeta *result = NULL;
1482 g_return_val_if_fail (buffer != NULL, NULL);
1483 g_return_val_if_fail (info != NULL, NULL);
1485 /* find GstMeta of the requested API */
1486 for (item = GST_BUFFER_META (buffer); item; item = item->next) {
1487 GstMeta *meta = &item->meta;
1488 if (meta->info->api == info->api) {
1497 * gst_buffer_add_meta:
1498 * @buffer: a #GstBuffer
1499 * @info: a #GstMetaInfo
1500 * @params: params for @info
1502 * Add metadata for @info to @buffer using the parameters in @params.
1504 * Returns: the metadata for the api in @info on @buffer.
1507 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
1511 GstMeta *result = NULL;
1514 g_return_val_if_fail (buffer != NULL, NULL);
1515 g_return_val_if_fail (info != NULL, NULL);
1517 /* create a new slice */
1518 GST_CAT_DEBUG (GST_CAT_BUFFER, "alloc metadata %s of size %" G_GSIZE_FORMAT,
1519 g_type_name (info->type), info->size);
1521 size = ITEM_SIZE (info);
1522 item = g_slice_alloc (size);
1523 result = &item->meta;
1524 result->info = info;
1526 /* call the init_func when needed */
1527 if (info->init_func)
1528 if (!info->init_func (result, params, buffer))
1531 /* and add to the list of metadata */
1532 item->next = GST_BUFFER_META (buffer);
1533 GST_BUFFER_META (buffer) = item;
1539 g_slice_free1 (size, item);
1545 * gst_buffer_remove_meta:
1546 * @buffer: a #GstBuffer
1549 * Remove the metadata for @meta on @buffer.
1551 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
1552 * metadata was on @buffer.
1555 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
1557 GstMetaItem *walk, *prev;
1559 g_return_val_if_fail (buffer != NULL, FALSE);
1560 g_return_val_if_fail (meta != NULL, FALSE);
1562 /* find the metadata and delete */
1563 prev = GST_BUFFER_META (buffer);
1564 for (walk = prev; walk; walk = walk->next) {
1565 GstMeta *m = &walk->meta;
1567 const GstMetaInfo *info = meta->info;
1569 /* remove from list */
1570 if (GST_BUFFER_META (buffer) == walk)
1571 GST_BUFFER_META (buffer) = walk->next;
1573 prev->next = walk->next;
1574 /* call free_func if any */
1575 if (info->free_func)
1576 info->free_func (m, buffer);
1578 /* and free the slice */
1579 g_slice_free1 (ITEM_SIZE (info), walk);
1584 return walk != NULL;
1588 * gst_buffer_iterate_meta:
1589 * @buffer: a #GstBuffer
1590 * @state: an opaque state pointer
1592 * Retrieve the next #GstMeta after @current. If @state points
1593 * to %NULL, the first metadata is returned.
1595 * @state will be updated with an opage state pointer
1597 * Returns: The next #GstMeta or %NULL when there are no more items.
1600 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
1604 g_return_val_if_fail (buffer != NULL, NULL);
1605 g_return_val_if_fail (state != NULL, NULL);
1607 meta = (GstMetaItem **) state;
1609 /* state NULL, move to first item */
1610 *meta = GST_BUFFER_META (buffer);
1612 /* state !NULL, move to next item in list */
1613 *meta = (*meta)->next;
1616 return &(*meta)->meta;