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_META(b) (((GstBufferImpl *)(b))->item)
153 /* the memory blocks */
155 GstMemory *mem[GST_BUFFER_MEM_MAX];
157 /* FIXME, make metadata allocation more efficient by using part of the
163 _span_memory (GstBuffer * buffer, gsize offset, gsize size, gboolean writable)
165 GstMemory *span, **mem[1];
168 /* not enough room, span buffers */
169 mem[0] = GST_BUFFER_MEM_ARRAY (buffer);
170 len[0] = GST_BUFFER_MEM_LEN (buffer);
173 size = gst_buffer_get_size (buffer);
175 span = _gst_buffer_arr_span (mem, len, 1, offset, size, writable);
181 _replace_memory (GstBuffer * buffer, GstMemory * mem)
185 /* unref old buffers */
186 len = GST_BUFFER_MEM_LEN (buffer);
187 for (i = 0; i < len; i++)
188 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
190 /* replace with single spanned buffer */
191 GST_BUFFER_MEM_PTR (buffer, 0) = mem;
192 GST_BUFFER_MEM_LEN (buffer) = 1;
196 _memory_add (GstBuffer * buffer, GstMemory * mem)
198 guint len = GST_BUFFER_MEM_LEN (buffer);
200 if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) {
201 /* to many buffer, span them */
202 _replace_memory (buffer, _span_memory (buffer, 0, -1, FALSE));
203 /* we now have 1 single spanned buffer */
206 /* and append the new buffer */
207 GST_BUFFER_MEM_PTR (buffer, len) = mem;
208 GST_BUFFER_MEM_LEN (buffer) = len + 1;
212 /* buffer alignment in bytes
213 * an alignment of 8 would be the same as malloc() guarantees
215 #ifdef HAVE_POSIX_MEMALIGN
216 #if defined(BUFFER_ALIGNMENT_MALLOC)
217 static size_t _gst_buffer_data_alignment = 8;
218 #elif defined(BUFFER_ALIGNMENT_PAGESIZE)
219 static size_t _gst_buffer_data_alignment = 0;
220 #elif defined(BUFFER_ALIGNMENT)
221 static size_t _gst_buffer_data_alignment = BUFFER_ALIGNMENT;
223 #error "No buffer alignment configured"
225 #endif /* HAVE_POSIX_MEMALIGN */
229 _gst_buffer_initialize (void)
231 if (G_LIKELY (_gst_buffer_type == 0)) {
232 _gst_buffer_type = gst_mini_object_register ("GstBuffer");
233 #ifdef HAVE_GETPAGESIZE
234 #ifdef BUFFER_ALIGNMENT_PAGESIZE
235 _gst_buffer_data_alignment = getpagesize ();
242 * gst_buffer_copy_into:
243 * @dest: a destination #GstBuffer
244 * @src: a source #GstBuffer
245 * @flags: flags indicating what metadata fields should be copied.
246 * @offset: offset to copy from
247 * @size: total size to copy
249 * Copies the information from @src into @dest.
251 * @flags indicate which fields will be copied.
254 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
255 GstBufferCopyFlags flags, gsize offset, gsize size)
260 g_return_if_fail (dest != NULL);
261 g_return_if_fail (src != NULL);
263 /* nothing to copy if the buffers are the same */
264 if (G_UNLIKELY (dest == src))
267 g_return_if_fail (gst_buffer_is_writable (dest));
269 bufsize = gst_buffer_get_size (src);
270 g_return_if_fail (bufsize >= offset);
272 size = bufsize - offset;
273 g_return_if_fail (bufsize >= offset + size);
275 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p, offset %" G_GSIZE_FORMAT
276 "-%" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT, src, dest, offset, size,
279 if (flags & GST_BUFFER_COPY_FLAGS) {
282 /* copy relevant flags */
283 mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
284 GST_BUFFER_FLAG_DELTA_UNIT | GST_BUFFER_FLAG_DISCONT |
285 GST_BUFFER_FLAG_GAP | GST_BUFFER_FLAG_MEDIA1 |
286 GST_BUFFER_FLAG_MEDIA2 | GST_BUFFER_FLAG_MEDIA3;
287 GST_MINI_OBJECT_FLAGS (dest) |= GST_MINI_OBJECT_FLAGS (src) & mask;
290 if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
292 GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
293 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
294 if (size == bufsize) {
295 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
296 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
299 GST_BUFFER_TIMESTAMP (dest) = GST_CLOCK_TIME_NONE;
300 GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
301 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
302 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
306 if (flags & GST_BUFFER_COPY_CAPS) {
307 gst_caps_replace (&GST_BUFFER_CAPS (dest), GST_BUFFER_CAPS (src));
310 if (flags & GST_BUFFER_COPY_MEMORY) {
312 gsize left, len, i, bsize;
314 len = GST_BUFFER_MEM_LEN (src);
317 /* copy and make regions of the memory */
318 for (i = 0; i < len && left > 0; i++) {
319 mem = GST_BUFFER_MEM_PTR (src, i);
320 bsize = gst_memory_get_sizes (mem, NULL);
322 if (bsize <= offset) {
323 /* don't copy buffer */
328 tocopy = MIN (bsize - offset, left);
329 if (tocopy < bsize) {
330 /* we need to clip something */
331 mem = gst_memory_share (mem, offset, tocopy);
333 mem = gst_memory_ref (mem);
335 _memory_add (dest, mem);
339 if (flags & GST_BUFFER_COPY_MERGE) {
340 _replace_memory (dest, _span_memory (dest, 0, size, FALSE));
344 for (walk = GST_BUFFER_META (src); walk; walk = walk->next) {
345 GstMeta *meta = &walk->meta;
346 const GstMetaInfo *info = meta->info;
349 info->copy_func (dest, meta, src, offset, size);
354 _gst_buffer_copy (GstBuffer * buffer)
358 g_return_val_if_fail (buffer != NULL, NULL);
360 /* create a fresh new buffer */
361 copy = gst_buffer_new ();
363 /* we simply copy everything from our parent */
364 gst_buffer_copy_into (copy, buffer, GST_BUFFER_COPY_ALL, 0, -1);
369 /* the default dispose function revives the buffer and returns it to the
370 * pool when there is a pool */
372 _gst_buffer_dispose (GstBuffer * buffer)
376 if ((pool = buffer->pool) != NULL) {
377 /* keep the buffer alive */
378 gst_buffer_ref (buffer);
379 /* return the buffer to the pool */
380 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
381 gst_buffer_pool_release_buffer (pool, buffer);
386 _gst_buffer_free (GstBuffer * buffer)
388 GstMetaItem *walk, *next;
392 g_return_if_fail (buffer != NULL);
394 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
396 gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
399 for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
400 GstMeta *meta = &walk->meta;
401 const GstMetaInfo *info = meta->info;
403 /* call free_func if any */
405 info->free_func (meta, buffer);
408 /* and free the slice */
409 g_slice_free1 (ITEM_SIZE (info), walk);
412 /* get the size, when unreffing the memory, we could also unref the buffer
414 msize = GST_MINI_OBJECT_SIZE (buffer);
416 /* free our memory */
417 len = GST_BUFFER_MEM_LEN (buffer);
418 for (i = 0; i < len; i++)
419 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
422 g_slice_free1 (msize, buffer);
426 gst_buffer_init (GstBufferImpl * buffer, gsize size)
428 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), _gst_buffer_type, size);
430 buffer->buffer.mini_object.copy =
431 (GstMiniObjectCopyFunction) _gst_buffer_copy;
432 buffer->buffer.mini_object.dispose =
433 (GstMiniObjectDisposeFunction) _gst_buffer_dispose;
434 buffer->buffer.mini_object.free =
435 (GstMiniObjectFreeFunction) _gst_buffer_free;
437 GST_BUFFER (buffer)->pool = NULL;
438 GST_BUFFER_CAPS (buffer) = NULL;
439 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
440 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
441 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
442 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
444 GST_BUFFER_MEM_LEN (buffer) = 0;
445 GST_BUFFER_META (buffer) = NULL;
451 * Creates a newly allocated buffer without any data.
455 * Returns: (transfer full): the new #GstBuffer.
458 gst_buffer_new (void)
460 GstBufferImpl *newbuf;
462 newbuf = g_slice_new (GstBufferImpl);
463 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
465 gst_buffer_init (newbuf, sizeof (GstBufferImpl));
467 return GST_BUFFER_CAST (newbuf);
471 * gst_buffer_new_and_alloc:
472 * @size: the size in bytes of the new buffer's data.
474 * Tries to create a newly allocated buffer with data of the given size. If
475 * the requested amount of memory can't be allocated, NULL will be returned.
476 * The allocated buffer memory is not cleared.
478 * Note that when @size == 0, the buffer will not have memory associated with it.
482 * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
486 gst_buffer_new_and_alloc (guint size)
497 mem = gst_memory_new_alloc (size, _gst_buffer_data_alignment);
498 if (G_UNLIKELY (mem == NULL))
504 newbuf = gst_buffer_new ();
507 _memory_add (newbuf, mem);
509 GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
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, mem);
533 /* allocate memory and buffer */
534 asize = sizeof (GstBufferImpl) + size;
535 mem = gst_memory_new_alloc (asize, 0);
536 if (G_UNLIKELY (mem == NULL))
539 /* map the data part and init the buffer in it, set the buffer size to 0 so
540 * that a finalize won't free the buffer */
541 data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
542 gst_buffer_init ((GstBufferImpl *) data, 0);
543 gst_memory_unmap (mem, data, asize);
545 /* strip off the buffer */
546 gst_memory_resize (mem, sizeof (GstBufferImpl), size);
548 newbuf = GST_BUFFER_CAST (data);
551 _memory_add (newbuf, mem);
559 GST_CAT_WARNING (GST_CAT_BUFFER, "failed to allocate %d bytes", size);
565 * gst_buffer_n_memory:
566 * @buffer: a #GstBuffer.
568 * Get the amount of memory blocks that this buffer has.
570 * Returns: (transfer full): the amount of memory block in this buffer.
573 gst_buffer_n_memory (GstBuffer * buffer)
575 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
577 return GST_BUFFER_MEM_LEN (buffer);
581 * gst_buffer_take_memory:
582 * @buffer: a #GstBuffer.
583 * @mem: a #GstMemory.
585 * Add the memory block @mem to @buffer. This function takes ownership of @mem
586 * and thus doesn't increase its refcount.
589 gst_buffer_take_memory (GstBuffer * buffer, GstMemory * mem)
591 g_return_if_fail (GST_IS_BUFFER (buffer));
592 g_return_if_fail (gst_buffer_is_writable (buffer));
593 g_return_if_fail (mem != NULL);
595 _memory_add (buffer, mem);
599 * gst_buffer_peek_memory:
600 * @buffer: a #GstBuffer.
603 * Get the memory block in @buffer at @idx. This function does not return a
604 * refcount to the memory block. The memory block stays valid for as long as the
605 * caller has a valid reference to @buffer.
607 * Returns: a #GstMemory at @idx.
610 gst_buffer_peek_memory (GstBuffer * buffer, guint idx, GstMapFlags flags)
615 write = (flags & GST_MAP_WRITE) != 0;
617 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
618 g_return_val_if_fail (idx < GST_BUFFER_MEM_LEN (buffer), NULL);
620 /* check if we can write when asked for write access */
621 if (G_UNLIKELY (write && !gst_buffer_is_writable (buffer)))
624 mem = GST_BUFFER_MEM_PTR (buffer, idx);
631 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
637 * gst_buffer_remove_memory_range:
638 * @buffer: a #GstBuffer.
642 * Remove @len memory blocks in @buffer starting from @idx.
644 * @length can be -1, in which case all memory starting from @idx is removed.
647 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, guint length)
651 g_return_if_fail (GST_IS_BUFFER (buffer));
652 g_return_if_fail (gst_buffer_is_writable (buffer));
654 len = GST_BUFFER_MEM_LEN (buffer);
656 g_return_if_fail (idx < len);
661 for (i = idx; i < end; i++)
662 gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
665 g_memmove (&GST_BUFFER_MEM_PTR (buffer, idx),
666 &GST_BUFFER_MEM_PTR (buffer, end), (len - end) * sizeof (gpointer));
668 GST_BUFFER_MEM_LEN (buffer) = len - length;
672 * gst_buffer_get_size:
673 * @buffer: a #GstBuffer.
675 * Get the total size of all memory blocks in @buffer.
677 * Returns: the total size of the memory in @buffer.
680 gst_buffer_get_size (GstBuffer * buffer)
684 g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
686 len = GST_BUFFER_MEM_LEN (buffer);
689 for (i = 0; i < len; i++) {
690 size += gst_memory_get_sizes (GST_BUFFER_MEM_PTR (buffer, i), NULL);
697 * @buffer: a #GstBuffer.
698 * @offset: the new offset
699 * @size: the new size
701 * Set the total size of the buffer
704 gst_buffer_resize (GstBuffer * buffer, gsize offset, gsize size)
708 gsize bsize, bufsize;
711 GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSIZE_FORMAT "-%" G_GSIZE_FORMAT,
712 buffer, offset, size);
714 g_return_if_fail (gst_buffer_is_writable (buffer));
716 bufsize = gst_buffer_get_size (buffer);
717 g_return_if_fail (bufsize >= offset);
719 size = bufsize - offset;
720 g_return_if_fail (bufsize >= offset + size);
722 len = GST_BUFFER_MEM_LEN (buffer);
725 for (di = si = 0; si < len && size > 0; si++) {
726 mem = GST_BUFFER_MEM_PTR (buffer, si);
727 bsize = gst_memory_get_sizes (mem, NULL);
729 if (bsize <= offset) {
731 gst_memory_unref (mem);
736 tocopy = MIN (bsize - offset, size);
737 if (tocopy < bsize) {
738 /* we need to clip something */
739 if (GST_MEMORY_IS_WRITABLE (mem)) {
740 gst_memory_resize (mem, offset, tocopy);
743 tmp = gst_memory_share (mem, offset, tocopy);
744 gst_memory_unref (mem);
748 GST_BUFFER_MEM_PTR (buffer, di++) = mem;
752 GST_BUFFER_MEM_LEN (buffer) = di;
757 * @buffer: a #GstBuffer.
758 * @size: a location for the size
759 * @maxsize: a location for the max size
760 * @flags: flags for the mapping
762 * This function return a pointer to the memory in @buffer. @flags describe the
763 * desired access of the memory. When @flags is #GST_MAP_WRITE, @buffer should
764 * be writable (as returned from gst_buffer_is_writable()).
766 * @size and @maxsize will contain the current valid number of bytes in the
767 * returned memory area and the total maximum mount of bytes available in the
768 * returned memory area respectively.
770 * When @buffer is writable but the memory isn't, a writable copy will
771 * automatically be created and returned. The readonly copy of the buffer memory
772 * will then also be replaced with this writable copy.
774 * When the buffer contains multiple memory blocks, the returned pointer will be
775 * a concatenation of the memory blocks.
777 * Returns: a pointer to the memory for the buffer.
780 gst_buffer_map (GstBuffer * buffer, gsize * size, gsize * maxsize,
786 gboolean write, writable;
788 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
790 write = (flags & GST_MAP_WRITE) != 0;
791 writable = gst_buffer_is_writable (buffer);
793 /* check if we can write when asked for write access */
794 if (G_UNLIKELY (write && !writable))
797 len = GST_BUFFER_MEM_LEN (buffer);
799 if (G_UNLIKELY (len == 0)) {
800 /* no memory, return immediately */
808 if (G_LIKELY (len == 1)) {
809 /* we can take the first one */
810 mem = GST_BUFFER_MEM_PTR (buffer, 0);
812 /* we need to span memory */
814 /* if we can write, we can change the memory with the spanned
816 mem = _span_memory (buffer, 0, -1, write);
817 _replace_memory (buffer, mem);
821 /* extract all data in new memory, FIXME slow!! */
822 bsize = gst_buffer_get_size (buffer);
824 data = g_malloc (bsize);
825 gst_buffer_extract (buffer, 0, data, bsize);
834 if (G_UNLIKELY (write && !GST_MEMORY_IS_WRITABLE (mem))) {
836 /* replace with a writable copy */
837 copy = gst_memory_copy (mem, 0, -1);
838 GST_BUFFER_MEM_PTR (buffer, 0) = copy;
839 gst_memory_unref (mem);
843 data = gst_memory_map (mem, size, maxsize, flags);
850 g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
857 * @buffer: a #GstBuffer.
858 * @data: the previously mapped data
859 * @size: the size of @data
861 * Release the memory previously mapped with gst_buffer_map().
863 * Returns: #TRUE on success. #FALSE can be returned when the new size is larger
864 * than the maxsize of the memory.
867 gst_buffer_unmap (GstBuffer * buffer, gpointer data, gsize size)
872 g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
874 len = GST_BUFFER_MEM_LEN (buffer);
876 if (G_LIKELY (len == 1)) {
877 GstMemory *mem = GST_BUFFER_MEM_PTR (buffer, 0);
879 result = gst_memory_unmap (mem, data, size);
881 /* this must have been from read-only access. After _map, the buffer either
882 * only contains 1 memory block or it allocated memory to join memory
883 * blocks. It's not allowed to add buffers between _map and _unmap. */
892 * @buffer: a #GstBuffer.
893 * @offset: the offset to fill
894 * @src: the source address
895 * @size: the size to fill
897 * Copy @size bytes fro @src to @buffer at @offset.
900 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
904 const guint8 *ptr = src;
906 g_return_if_fail (GST_IS_BUFFER (buffer));
907 g_return_if_fail (gst_buffer_is_writable (buffer));
908 g_return_if_fail (src != NULL);
910 len = GST_BUFFER_MEM_LEN (buffer);
912 for (i = 0; i < len && size > 0; i++) {
917 mem = GST_BUFFER_MEM_PTR (buffer, i);
919 data = gst_memory_map (mem, &ssize, NULL, GST_MAP_WRITE);
920 if (ssize > offset) {
922 tocopy = MIN (ssize - offset, size);
923 memcpy (data + offset, ptr, tocopy);
928 /* offset past buffer, skip */
931 gst_memory_unmap (mem, data, ssize);
936 * gst_buffer_extract:
937 * @buffer: a #GstBuffer.
938 * @offset: the offset to extract
939 * @dest: the destination address
940 * @size: the size to extract
942 * Copy @size bytes starting from @offset in @buffer to @dest.
945 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
950 g_return_if_fail (GST_IS_BUFFER (buffer));
951 g_return_if_fail (dest != NULL);
953 len = GST_BUFFER_MEM_LEN (buffer);
955 for (i = 0; i < len && size > 0; i++) {
960 mem = GST_BUFFER_MEM_PTR (buffer, i);
962 data = gst_memory_map (mem, &ssize, NULL, GST_MAP_READ);
963 if (ssize > offset) {
965 tocopy = MIN (ssize - offset, size);
966 memcpy (ptr, data + offset, tocopy);
971 /* offset past buffer, skip */
974 gst_memory_unmap (mem, data, ssize);
979 * gst_buffer_get_caps:
980 * @buffer: a #GstBuffer.
982 * Gets the media type of the buffer. This can be NULL if there
983 * is no media type attached to this buffer.
985 * Returns: (transfer full): a reference to the #GstCaps. unref after usage.
986 * Returns NULL if there were no caps on this buffer.
988 /* this is not made atomic because if the buffer were reffed from multiple
989 * threads, it would have a refcount > 2 and thus be immutable.
992 gst_buffer_get_caps (GstBuffer * buffer)
996 g_return_val_if_fail (buffer != NULL, NULL);
998 ret = GST_BUFFER_CAPS (buffer);
1007 * gst_buffer_set_caps:
1008 * @buffer: a #GstBuffer.
1009 * @caps: (transfer none): a #GstCaps.
1011 * Sets the media type on the buffer. The refcount of the caps will
1012 * be increased and any previous caps on the buffer will be
1015 /* this is not made atomic because if the buffer were reffed from multiple
1016 * threads, it would have a refcount > 2 and thus be immutable.
1019 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
1021 g_return_if_fail (buffer != NULL);
1022 g_return_if_fail (caps == NULL || GST_CAPS_IS_SIMPLE (caps));
1024 #if GST_VERSION_NANO == 1
1025 /* we enable this extra debugging in git versions only for now */
1026 g_warn_if_fail (gst_buffer_is_writable (buffer));
1027 /* FIXME: would be nice to also check if caps are fixed here, but expensive */
1030 gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
1034 * gst_buffer_copy_region:
1035 * @parent: a #GstBuffer.
1036 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
1038 * @size: the size of the new #GstBuffer sub-buffer, in bytes.
1040 * Creates a sub-buffer from @parent at @offset and @size.
1041 * This sub-buffer uses the actual memory space of the parent buffer.
1042 * This function will copy the offset and timestamp fields when the
1043 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
1044 * #GST_BUFFER_OFFSET_NONE.
1045 * If @offset equals 0 and @size equals the total size of @buffer, the
1046 * duration and offset end fields are also copied. If not they will be set
1047 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
1051 * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
1055 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
1056 gsize offset, gsize size)
1060 g_return_val_if_fail (buffer != NULL, NULL);
1062 /* create the new buffer */
1063 copy = gst_buffer_new ();
1065 GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
1066 "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
1068 gst_buffer_copy_into (copy, buffer, flags, offset, size);
1074 _gst_buffer_arr_is_span_fast (GstMemory ** mem[], gsize len[], guint n,
1075 gsize * offset, GstMemory ** parent)
1077 GstMemory *mcur, *mprv;
1078 gboolean have_offset = FALSE;
1082 for (count = 0; count < n; count++) {
1089 for (i = 0; i < clen; i++) {
1095 /* check is memory is contiguous */
1096 if (!gst_memory_is_span (mprv, mcur, &offs))
1103 *parent = mprv->parent;
1114 _gst_buffer_arr_span (GstMemory ** mem[], gsize len[], guint n, gsize offset,
1115 gsize size, gboolean writable)
1117 GstMemory *span, *parent;
1121 && _gst_buffer_arr_is_span_fast (mem, len, n, &poffset, &parent)) {
1122 span = gst_memory_share (parent, offset + poffset, size);
1127 span = gst_memory_new_alloc (size, 0);
1128 dest = gst_memory_map (span, NULL, NULL, GST_MAP_WRITE);
1133 for (count = 0; count < n; count++) {
1134 gsize i, tocopy, clen, ssize;
1141 for (i = 0; i < clen && left > 0; i++) {
1142 src = gst_memory_map (cmem[i], &ssize, NULL, GST_MAP_READ);
1143 tocopy = MIN (ssize, left);
1144 if (tocopy > offset) {
1145 memcpy (ptr, src + offset, tocopy - offset);
1152 gst_memory_unmap (cmem[i], src, ssize);
1155 gst_memory_unmap (span, dest, size);
1161 * gst_buffer_is_span_fast:
1162 * @buf1: the first #GstBuffer.
1163 * @buf2: the second #GstBuffer.
1165 * Determines whether a gst_buffer_span() can be done without copying
1166 * the contents, that is, whether the data areas are contiguous sub-buffers of
1170 * Returns: TRUE if the buffers are contiguous,
1171 * FALSE if a copy would be required.
1174 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
1179 g_return_val_if_fail (GST_IS_BUFFER (buf1), FALSE);
1180 g_return_val_if_fail (GST_IS_BUFFER (buf2), FALSE);
1181 g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
1182 g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
1184 mem[0] = GST_BUFFER_MEM_ARRAY (buf1);
1185 len[0] = GST_BUFFER_MEM_LEN (buf1);
1186 mem[1] = GST_BUFFER_MEM_ARRAY (buf2);
1187 len[1] = GST_BUFFER_MEM_LEN (buf2);
1189 return _gst_buffer_arr_is_span_fast (mem, len, 2, NULL, NULL);
1194 * @buf1: the first source #GstBuffer to merge.
1195 * @offset: the offset in the first buffer from where the new
1196 * buffer should start.
1197 * @buf2: the second source #GstBuffer to merge.
1198 * @size: the total size of the new buffer.
1200 * Creates a new buffer that consists of part of buf1 and buf2.
1201 * Logically, buf1 and buf2 are concatenated into a single larger
1202 * buffer, and a new buffer is created at the given offset inside
1203 * this space, with a given length.
1205 * If the two source buffers are children of the same larger buffer,
1206 * and are contiguous, the new buffer will be a child of the shared
1207 * parent, and thus no copying is necessary. you can use
1208 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
1212 * Returns: (transfer full): the new #GstBuffer that spans the two source
1213 * buffers, or NULL if the arguments are invalid.
1216 gst_buffer_span (GstBuffer * buf1, gsize offset, GstBuffer * buf2, gsize size)
1221 gsize len[2], len1, len2;
1223 g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
1224 g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
1225 g_return_val_if_fail (buf1->mini_object.refcount > 0, NULL);
1226 g_return_val_if_fail (buf2->mini_object.refcount > 0, NULL);
1227 len1 = gst_buffer_get_size (buf1);
1228 len2 = gst_buffer_get_size (buf2);
1229 g_return_val_if_fail (len1 + len2 > offset, NULL);
1231 size = len1 + len2 - offset;
1233 g_return_val_if_fail (size <= len1 + len2 - offset, NULL);
1235 mem[0] = GST_BUFFER_MEM_ARRAY (buf1);
1236 len[0] = GST_BUFFER_MEM_LEN (buf1);
1237 mem[1] = GST_BUFFER_MEM_ARRAY (buf2);
1238 len[1] = GST_BUFFER_MEM_LEN (buf2);
1240 span = _gst_buffer_arr_span (mem, len, 2, offset, size, FALSE);
1242 newbuf = gst_buffer_new ();
1243 _memory_add (newbuf, span);
1246 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
1248 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
1249 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
1251 /* if we completely merged the two buffers (appended), we can
1252 * calculate the duration too. Also make sure we's not messing with
1253 * invalid DURATIONS */
1254 if (buf1->size + buf2->size == len) {
1255 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
1256 GST_BUFFER_DURATION_IS_VALID (buf2)) {
1258 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
1259 GST_BUFFER_DURATION (buf2);
1261 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
1262 /* add offset_end */
1263 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
1273 * gst_buffer_get_meta:
1274 * @buffer: a #GstBuffer
1275 * @info: a #GstMetaInfo
1277 * Get the metadata for the api in @info on buffer. When there is no such
1278 * metadata, NULL is returned.
1280 * Note that the result metadata might not be of the implementation @info.
1282 * Returns: the metadata for the api in @info on @buffer.
1285 gst_buffer_get_meta (GstBuffer * buffer, const GstMetaInfo * info)
1288 GstMeta *result = NULL;
1290 g_return_val_if_fail (buffer != NULL, NULL);
1291 g_return_val_if_fail (info != NULL, NULL);
1293 /* find GstMeta of the requested API */
1294 for (item = GST_BUFFER_META (buffer); item; item = item->next) {
1295 GstMeta *meta = &item->meta;
1296 if (meta->info->api == info->api) {
1305 * gst_buffer_add_meta:
1306 * @buffer: a #GstBuffer
1307 * @info: a #GstMetaInfo
1308 * @params: params for @info
1310 * Add metadata for @info to @buffer using the parameters in @params.
1312 * Returns: the metadata for the api in @info on @buffer.
1315 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
1319 GstMeta *result = NULL;
1322 g_return_val_if_fail (buffer != NULL, NULL);
1323 g_return_val_if_fail (info != NULL, NULL);
1325 /* create a new slice */
1326 GST_CAT_DEBUG (GST_CAT_BUFFER, "alloc metadata of size %" G_GSIZE_FORMAT,
1329 size = ITEM_SIZE (info);
1330 item = g_slice_alloc (size);
1331 result = &item->meta;
1332 result->info = info;
1334 /* call the init_func when needed */
1335 if (info->init_func)
1336 if (!info->init_func (result, params, buffer))
1339 /* and add to the list of metadata */
1340 item->next = GST_BUFFER_META (buffer);
1341 GST_BUFFER_META (buffer) = item;
1347 g_slice_free1 (size, item);
1353 * gst_buffer_remove_meta:
1354 * @buffer: a #GstBuffer
1357 * Remove the metadata for @meta on @buffer.
1359 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
1360 * metadata was on @buffer.
1363 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
1365 GstMetaItem *walk, *prev;
1367 g_return_val_if_fail (buffer != NULL, FALSE);
1368 g_return_val_if_fail (meta != NULL, FALSE);
1370 /* find the metadata and delete */
1371 prev = GST_BUFFER_META (buffer);
1372 for (walk = prev; walk; walk = walk->next) {
1373 GstMeta *m = &walk->meta;
1375 const GstMetaInfo *info = meta->info;
1377 /* remove from list */
1378 if (GST_BUFFER_META (buffer) == walk)
1379 GST_BUFFER_META (buffer) = walk->next;
1381 prev->next = walk->next;
1382 /* call free_func if any */
1383 if (info->free_func)
1384 info->free_func (m, buffer);
1386 /* and free the slice */
1387 g_slice_free1 (ITEM_SIZE (info), walk);
1392 return walk != NULL;
1396 * gst_buffer_iterate_meta:
1397 * @buffer: a #GstBuffer
1398 * @state: an opaque state pointer
1400 * Retrieve the next #GstMeta after @current. If @state points
1401 * to %NULL, the first metadata is returned.
1403 * @state will be updated with an opage state pointer
1405 * Returns: The next #GstMeta or %NULL when there are no more items.
1408 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
1412 g_return_val_if_fail (buffer != NULL, NULL);
1413 g_return_val_if_fail (state != NULL, NULL);
1415 meta = (GstMetaItem **) state;
1417 /* state NULL, move to first item */
1418 *meta = GST_BUFFER_META (buffer);
1420 /* state !NULL, move to next item in list */
1421 *meta = (*meta)->next;
1424 return &(*meta)->meta;