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 a region of memory as part
30 * of a stream. Sub-buffers are also supported, allowing a smaller region of a
31 * buffer to become its own buffer, with mechanisms in place to ensure that
32 * 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_create_sub().
90 * If a plug-in wants to modify the buffer data in-place, it should first obtain
91 * a buffer that is safe to modify by using gst_buffer_make_writable(). This
92 * function is optimized so that a copy will only be made when it is necessary.
94 * A plugin that only wishes to modify the metadata of a buffer, such as the
95 * offset, timestamp or caps, should use gst_buffer_make_metadata_writable(),
96 * which will create a subbuffer of the original buffer to ensure the caller
97 * has sole ownership, and not copy the buffer data.
99 * Several flags of the buffer can be set and unset with the
100 * GST_BUFFER_FLAG_SET() and GST_BUFFER_FLAG_UNSET() macros. Use
101 * GST_BUFFER_FLAG_IS_SET() to test if a certain #GstBufferFlag is set.
103 * Buffers can be efficiently merged into a larger buffer with
104 * gst_buffer_merge() and gst_buffer_span() if the gst_buffer_is_span_fast()
105 * function returns TRUE.
107 * An element should either unref the buffer or push it out on a src pad
108 * using gst_pad_push() (see #GstPad).
110 * Buffers are usually freed by unreffing them with gst_buffer_unref(). When
111 * the refcount drops to 0, any data pointed to by GST_BUFFER_MALLOCDATA() will
114 * Last reviewed on August 11th, 2006 (0.10.10)
116 #include "gst_private.h"
125 #include "gstbuffer.h"
126 #include "gstbufferpool.h"
128 #include "gstutils.h"
129 #include "gstminiobject.h"
130 #include "gstversion.h"
132 GType _gst_buffer_type = 0;
134 typedef struct _GstMetaItem GstMetaItem;
142 #define ITEM_SIZE(info) ((info)->size + sizeof (GstMetaItem))
144 /* buffer alignment in bytes
145 * an alignment of 8 would be the same as malloc() guarantees
147 #ifdef HAVE_POSIX_MEMALIGN
148 #if defined(BUFFER_ALIGNMENT_MALLOC)
149 static size_t _gst_buffer_data_alignment = 8;
150 #elif defined(BUFFER_ALIGNMENT_PAGESIZE)
151 static size_t _gst_buffer_data_alignment = 0;
152 #elif defined(BUFFER_ALIGNMENT)
153 static size_t _gst_buffer_data_alignment = BUFFER_ALIGNMENT;
155 #error "No buffer alignment configured"
157 #endif /* HAVE_POSIX_MEMALIGN */
160 _gst_buffer_initialize (void)
162 if (G_LIKELY (_gst_buffer_type == 0)) {
163 _gst_buffer_type = gst_mini_object_register ("GstBuffer");
164 #ifdef HAVE_GETPAGESIZE
165 #ifdef BUFFER_ALIGNMENT_PAGESIZE
166 _gst_buffer_data_alignment = getpagesize ();
173 * gst_buffer_copy_into:
174 * @dest: a destination #GstBuffer
175 * @src: a source #GstBuffer
176 * @flags: flags indicating what metadata fields should be copied.
177 * @offset: offset to copy from
178 * @trim: bytes to trim from end
180 * Copies the information from @src into @dest.
182 * @flags indicate which fields will be copied.
185 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
186 GstBufferCopyFlags flags, gsize offset, gsize trim)
190 g_return_if_fail (dest != NULL);
191 g_return_if_fail (src != NULL);
193 /* nothing to copy if the buffers are the same */
194 if (G_UNLIKELY (dest == src))
197 #if GST_VERSION_NANO == 1
198 /* we enable this extra debugging in git versions only for now */
199 g_warn_if_fail (gst_buffer_is_metadata_writable (dest));
202 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", src, dest);
204 if (flags & GST_BUFFER_COPY_FLAGS) {
207 /* copy relevant flags */
208 mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
209 GST_BUFFER_FLAG_DELTA_UNIT | GST_BUFFER_FLAG_DISCONT |
210 GST_BUFFER_FLAG_GAP | GST_BUFFER_FLAG_MEDIA1 |
211 GST_BUFFER_FLAG_MEDIA2 | GST_BUFFER_FLAG_MEDIA3;
212 GST_MINI_OBJECT_FLAGS (dest) |= GST_MINI_OBJECT_FLAGS (src) & mask;
215 if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
217 GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
218 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
220 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
221 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
224 GST_BUFFER_TIMESTAMP (dest) = GST_CLOCK_TIME_NONE;
225 GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
226 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
227 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
231 if (flags & GST_BUFFER_COPY_CAPS) {
232 gst_caps_replace (&GST_BUFFER_CAPS (dest), GST_BUFFER_CAPS (src));
235 if (flags & GST_BUFFER_COPY_MEMORY) {
236 GPtrArray *sarr = (GPtrArray *) src->memory;
237 GPtrArray *darr = (GPtrArray *) dest->memory;
242 if (flags & GST_BUFFER_COPY_MEMORY_SHARE) {
243 for (i = 0; i < len; i++) {
244 GstMemory *mem = g_ptr_array_index (sarr, i);
245 g_ptr_array_add (darr, gst_memory_ref (mem));
248 for (i = 0; i < len; i++) {
249 GstMemory *mem = g_ptr_array_index (sarr, i);
250 g_ptr_array_add (darr, gst_memory_copy (mem));
255 for (walk = src->priv; walk; walk = walk->next) {
256 GstMeta *meta = &walk->meta;
257 const GstMetaInfo *info = meta->info;
258 GstMetaTransformData data = { GST_META_TRANSFORM_COPY };
260 if (info->transform_func)
261 info->transform_func (dest, meta, (GstBuffer *) src, &data);
266 _gst_buffer_copy (GstBuffer * buffer)
270 g_return_val_if_fail (buffer != NULL, NULL);
272 /* create a fresh new buffer */
273 copy = gst_buffer_new ();
275 /* we simply copy everything from our parent */
276 gst_buffer_copy_into (copy, buffer, GST_BUFFER_COPY_ALL, 0, 0);
281 /* the default dispose function revives the buffer and returns it to the
282 * pool when there is a pool */
284 _gst_buffer_dispose (GstBuffer * buffer)
288 if ((pool = buffer->pool) != NULL) {
289 /* keep the buffer alive */
290 gst_buffer_ref (buffer);
291 /* return the buffer to the pool */
292 GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
293 gst_buffer_pool_release_buffer (pool, buffer);
298 _gst_buffer_free (GstBuffer * buffer)
300 GstMetaItem *walk, *next;
302 g_return_if_fail (buffer != NULL);
304 GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
306 gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
309 for (walk = buffer->priv; walk; walk = next) {
310 GstMeta *meta = &walk->meta;
311 const GstMetaInfo *info = meta->info;
313 /* call free_func if any */
315 info->free_func (meta, buffer);
316 /* and free the slice */
318 g_slice_free (GstMetaItem, walk);
321 /* free our data, unrefs the memory too */
322 g_ptr_array_free (buffer->memory, TRUE);
324 g_slice_free1 (GST_MINI_OBJECT_SIZE (buffer), buffer);
328 gst_buffer_init (GstBuffer * buffer, gsize size)
330 gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), _gst_buffer_type, size);
332 buffer->mini_object.copy = (GstMiniObjectCopyFunction) _gst_buffer_copy;
333 buffer->mini_object.dispose =
334 (GstMiniObjectDisposeFunction) _gst_buffer_dispose;
335 buffer->mini_object.free = (GstMiniObjectFreeFunction) _gst_buffer_free;
337 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
338 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
339 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
340 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
342 /* FIXME, do more efficient with array in the buffer memory itself */
344 g_ptr_array_new_with_free_func ((GDestroyNotify) gst_memory_unref);
350 * Creates a newly allocated buffer without any data.
354 * Returns: (transfer full): the new #GstBuffer.
357 gst_buffer_new (void)
361 newbuf = g_slice_new0 (GstBuffer);
362 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
364 gst_buffer_init (newbuf, sizeof (GstBuffer));
370 * gst_buffer_new_and_alloc:
371 * @size: the size in bytes of the new buffer's data.
373 * Creates a newly allocated buffer with data of the given size.
374 * The buffer memory is not cleared. If the requested amount of
375 * memory can't be allocated, the program will abort. Use
376 * gst_buffer_try_new_and_alloc() if you want to handle this case
377 * gracefully or have gotten the size to allocate from an untrusted
378 * source such as a media stream.
380 * Note that when @size == 0, the buffer data pointer will be NULL.
384 * Returns: (transfer full): the new #GstBuffer.
387 gst_buffer_new_and_alloc (guint size)
391 newbuf = gst_buffer_new ();
393 gst_buffer_take_memory (newbuf, gst_memory_new_alloc (size,
394 _gst_buffer_data_alignment));
396 GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
402 * gst_buffer_try_new_and_alloc:
403 * @size: the size in bytes of the new buffer's data.
405 * Tries to create a newly allocated buffer with data of the given size. If
406 * the requested amount of memory can't be allocated, NULL will be returned.
407 * The buffer memory is not cleared.
409 * Note that when @size == 0, the buffer data pointer will be NULL.
413 * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
419 gst_buffer_try_new_and_alloc (guint size)
424 mem = gst_memory_new_alloc (size, _gst_buffer_data_alignment);
425 if (G_UNLIKELY (mem == NULL)) {
426 GST_CAT_WARNING (GST_CAT_BUFFER, "failed to allocate %d bytes", size);
430 newbuf = gst_buffer_new ();
431 gst_buffer_take_memory (newbuf, mem);
433 GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
439 gst_buffer_n_memory (GstBuffer * buffer)
441 GPtrArray *arr = (GPtrArray *) buffer->memory;
447 gst_buffer_take_memory (GstBuffer * buffer, GstMemory * mem)
449 GPtrArray *arr = (GPtrArray *) buffer->memory;
451 g_ptr_array_add (arr, mem);
455 gst_buffer_peek_memory (GstBuffer * buffer, guint idx)
458 GPtrArray *arr = (GPtrArray *) buffer->memory;
460 mem = g_ptr_array_index (arr, idx);
466 gst_buffer_remove_memory (GstBuffer * buffer, guint idx)
468 GPtrArray *arr = (GPtrArray *) buffer->memory;
470 g_ptr_array_remove_index (arr, idx);
474 gst_buffer_get_size (GstBuffer * buffer)
476 GPtrArray *arr = (GPtrArray *) buffer->memory;
482 for (i = 0; i < len; i++) {
483 size += gst_memory_get_sizes (g_ptr_array_index (arr, i), NULL);
490 gst_buffer_map (GstBuffer * buffer, gsize * size, gsize * maxsize,
493 GPtrArray *arr = (GPtrArray *) buffer->memory;
499 if (G_LIKELY (len == 1)) {
500 GstMemory *mem = g_ptr_array_index (arr, 0);
502 data = gst_memory_map (mem, size, maxsize, flags);
510 gst_buffer_unmap (GstBuffer * buffer, gpointer data, gsize size)
512 GPtrArray *arr = (GPtrArray *) buffer->memory;
518 if (G_LIKELY (len == 1)) {
519 GstMemory *mem = g_ptr_array_index (arr, 0);
521 result = gst_memory_unmap (mem, data, size);
529 * gst_buffer_get_caps:
530 * @buffer: a #GstBuffer.
532 * Gets the media type of the buffer. This can be NULL if there
533 * is no media type attached to this buffer.
535 * Returns: (transfer full): a reference to the #GstCaps. unref after usage.
536 * Returns NULL if there were no caps on this buffer.
538 /* this is not made atomic because if the buffer were reffed from multiple
539 * threads, it would have a refcount > 2 and thus be immutable.
542 gst_buffer_get_caps (GstBuffer * buffer)
546 g_return_val_if_fail (buffer != NULL, NULL);
548 ret = GST_BUFFER_CAPS (buffer);
557 * gst_buffer_set_caps:
558 * @buffer: a #GstBuffer.
559 * @caps: (transfer none): a #GstCaps.
561 * Sets the media type on the buffer. The refcount of the caps will
562 * be increased and any previous caps on the buffer will be
565 /* this is not made atomic because if the buffer were reffed from multiple
566 * threads, it would have a refcount > 2 and thus be immutable.
569 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
571 g_return_if_fail (buffer != NULL);
572 g_return_if_fail (caps == NULL || GST_CAPS_IS_SIMPLE (caps));
574 #if GST_VERSION_NANO == 1
575 /* we enable this extra debugging in git versions only for now */
576 g_warn_if_fail (gst_buffer_is_metadata_writable (buffer));
577 /* FIXME: would be nice to also check if caps are fixed here, but expensive */
580 gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
584 * gst_buffer_is_metadata_writable:
587 * Similar to gst_buffer_is_writable, but this only ensures that the
588 * refcount of the buffer is 1, indicating that the caller is the sole
589 * owner and can change the buffer metadata, such as caps and timestamps.
591 * Returns: TRUE if the metadata is writable.
594 gst_buffer_is_metadata_writable (GstBuffer * buf)
596 return (GST_MINI_OBJECT_REFCOUNT_VALUE (GST_MINI_OBJECT_CAST (buf)) == 1);
600 * gst_buffer_make_metadata_writable:
601 * @buf: (transfer full): a #GstBuffer
603 * Similar to gst_buffer_make_writable, but does not ensure that the buffer
604 * data array is writable. Instead, this just ensures that the returned buffer
605 * is solely owned by the caller, by creating a subbuffer of the original
606 * buffer if necessary.
608 * After calling this function, @buf should not be referenced anymore. The
609 * result of this function has guaranteed writable metadata.
611 * Returns: (transfer full): a new #GstBuffer with writable metadata, which
612 * may or may not be the same as @buf.
615 gst_buffer_make_metadata_writable (GstBuffer * buf)
619 if (gst_buffer_is_metadata_writable (buf)) {
622 /* create a fresh new buffer */
623 ret = gst_buffer_new ();
625 /* we simply copy everything from our parent */
626 gst_buffer_copy_into (ret, buf, GST_BUFFER_SHARE_ALL, 0, 0);
627 gst_buffer_unref (buf);
633 * gst_buffer_create_sub:
634 * @parent: a #GstBuffer.
635 * @offset: the offset into parent #GstBuffer at which the new sub-buffer
637 * @size: the size of the new #GstBuffer sub-buffer, in bytes.
639 * Creates a sub-buffer from @parent at @offset and @size.
640 * This sub-buffer uses the actual memory space of the parent buffer.
641 * This function will copy the offset and timestamp fields when the
642 * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
643 * #GST_BUFFER_OFFSET_NONE.
644 * If @offset equals 0 and @size equals the total size of @buffer, the
645 * duration and offset end fields are also copied. If not they will be set
646 * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
650 * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
654 gst_buffer_create_sub (GstBuffer * buffer, gsize offset, gsize size)
656 GstBuffer *subbuffer;
659 g_return_val_if_fail (buffer != NULL, NULL);
660 g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL);
662 bufsize = gst_buffer_get_size (buffer);
663 g_return_val_if_fail (bufsize >= offset + size, NULL);
665 /* create the new buffer */
666 subbuffer = gst_buffer_new ();
668 GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p of %p", subbuffer, buffer);
670 gst_buffer_copy_into (subbuffer, buffer, GST_BUFFER_SHARE_ALL, offset,
671 bufsize - (size + offset));
677 * gst_buffer_is_span_fast:
678 * @buf1: the first #GstBuffer.
679 * @buf2: the second #GstBuffer.
681 * Determines whether a gst_buffer_span() can be done without copying
682 * the contents, that is, whether the data areas are contiguous sub-buffers of
686 * Returns: TRUE if the buffers are contiguous,
687 * FALSE if a copy would be required.
690 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
692 GPtrArray *arr1, *arr2;
694 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
695 g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
696 g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
698 arr1 = (GPtrArray *) buf1->memory;
699 arr2 = (GPtrArray *) buf2->memory;
701 return gst_memory_is_span ((GstMemory **) arr1->pdata, arr1->len,
702 (GstMemory **) arr2->pdata, arr2->len, NULL, NULL);
707 * @buf1: the first source #GstBuffer to merge.
708 * @offset: the offset in the first buffer from where the new
709 * buffer should start.
710 * @buf2: the second source #GstBuffer to merge.
711 * @len: the total length of the new buffer.
713 * Creates a new buffer that consists of part of buf1 and buf2.
714 * Logically, buf1 and buf2 are concatenated into a single larger
715 * buffer, and a new buffer is created at the given offset inside
716 * this space, with a given length.
718 * If the two source buffers are children of the same larger buffer,
719 * and are contiguous, the new buffer will be a child of the shared
720 * parent, and thus no copying is necessary. you can use
721 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
725 * Returns: (transfer full): the new #GstBuffer that spans the two source
726 * buffers, or NULL if the arguments are invalid.
729 gst_buffer_span (GstBuffer * buf1, gsize offset, GstBuffer * buf2, gsize len)
732 GPtrArray *arr1, *arr2;
735 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
736 g_return_val_if_fail (buf1->mini_object.refcount > 0, NULL);
737 g_return_val_if_fail (buf2->mini_object.refcount > 0, NULL);
738 g_return_val_if_fail (len > 0, NULL);
740 newbuf = gst_buffer_new ();
742 arr1 = (GPtrArray *) buf1->memory;
743 arr2 = (GPtrArray *) buf2->memory;
745 mem = gst_memory_span ((GstMemory **) arr1->pdata, arr1->len, offset,
746 (GstMemory **) arr2->pdata, arr2->len, len);
747 gst_buffer_take_memory (newbuf, mem);
750 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
752 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
753 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
755 /* if we completely merged the two buffers (appended), we can
756 * calculate the duration too. Also make sure we's not messing with
757 * invalid DURATIONS */
758 if (buf1->size + buf2->size == len) {
759 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
760 GST_BUFFER_DURATION_IS_VALID (buf2)) {
762 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
763 GST_BUFFER_DURATION (buf2);
765 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
767 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
777 * gst_buffer_get_meta:
778 * @buffer: a #GstBuffer
779 * @info: a #GstMetaInfo
781 * Get the metadata for the api in @info on buffer. When there is no such
782 * metadata, NULL is returned.
784 * Note that the result metadata might not be of the implementation @info.
786 * Returns: the metadata for the api in @info on @buffer.
789 gst_buffer_get_meta (GstBuffer * buffer, const GstMetaInfo * info)
792 GstMeta *result = NULL;
794 g_return_val_if_fail (buffer != NULL, NULL);
795 g_return_val_if_fail (info != NULL, NULL);
797 /* find GstMeta of the requested API */
798 for (item = buffer->priv; item; item = item->next) {
799 GstMeta *meta = &item->meta;
800 if (meta->info->api == info->api) {
809 * gst_buffer_add_meta:
810 * @buffer: a #GstBuffer
811 * @info: a #GstMetaInfo
812 * @params: params for @info
814 * Add metadata for @info to @buffer using the parameters in @params.
816 * Returns: the metadata for the api in @info on @buffer.
819 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
823 GstMeta *result = NULL;
826 g_return_val_if_fail (buffer != NULL, NULL);
827 g_return_val_if_fail (info != NULL, NULL);
829 /* create a new slice */
830 GST_CAT_DEBUG (GST_CAT_BUFFER, "alloc metadata of size %" G_GSIZE_FORMAT,
833 size = ITEM_SIZE (info);
834 item = g_slice_alloc (size);
835 result = &item->meta;
838 /* call the init_func when needed */
840 if (!info->init_func (result, params, buffer))
843 /* and add to the list of metadata */
844 item->next = buffer->priv;
851 g_slice_free1 (size, item);
857 * gst_buffer_remove_meta:
858 * @buffer: a #GstBuffer
859 * @info: a #GstMetaInfo
861 * Remove the metadata for @info on @buffer.
863 * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
864 * metadata was on @buffer.
867 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
869 GstMetaItem *walk, *prev;
871 g_return_val_if_fail (buffer != NULL, FALSE);
872 g_return_val_if_fail (meta != NULL, FALSE);
874 /* find the metadata and delete */
876 for (walk = prev; walk; walk = walk->next) {
877 GstMeta *m = &walk->meta;
879 const GstMetaInfo *info = meta->info;
881 /* remove from list */
882 if (buffer->priv == walk)
883 buffer->priv = walk->next;
885 prev->next = walk->next;
886 /* call free_func if any */
888 info->free_func (m, buffer);
890 /* and free the slice */
891 g_slice_free1 (ITEM_SIZE (info), walk);
900 * gst_buffer_iterate_meta:
901 * @buffer: a #GstBuffer
902 * @state: an opaque state pointer
904 * Retrieve the next #GstMeta after @current. If @state points
905 * to %NULL, the first metadata is returned.
907 * @state will be updated with an opage state pointer
909 * Returns: The next #GstMeta or %NULL when there are no more items.
912 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
916 g_return_val_if_fail (buffer != NULL, NULL);
917 g_return_val_if_fail (state != NULL, NULL);
919 meta = (GstMetaItem **) state;
921 /* state NULL, move to first item */
922 *meta = buffer->priv;
924 /* state !NULL, move to next item in list */
925 *meta = (*meta)->next;
928 return &(*meta)->meta;