buffer: Add annotations and pygi friendly extraction function
[platform/upstream/gstreamer.git] / gst / gstbuffer.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstbuffer.c: Buffer operations
6  *
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.
11  *
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.
16  *
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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /**
24  * SECTION:gstbuffer
25  * @short_description: Data-passing buffer type
26  * @see_also: #GstPad, #GstMiniObject, #GstMemory, #GstMeta, #GstBufferPool
27  *
28  * Buffers are the basic unit of data transfer in GStreamer. They contain the
29  * timing and offset along with other arbitrary metadata that is associated
30  * with the #GstMemory blocks that the buffer contains.
31  *
32  * Buffers are usually created with gst_buffer_new(). After a buffer has been
33  * created one will typically allocate memory for it and add it to the buffer.
34  * The following example creates a buffer that can hold a given video frame
35  * with a given width, height and bits per plane.
36  * <example>
37  * <title>Creating a buffer for a video frame</title>
38  *   <programlisting>
39  *   GstBuffer *buffer;
40  *   GstMemory *memory;
41  *   gint size, width, height, bpp;
42  *   ...
43  *   size = width * height * bpp;
44  *   buffer = gst_buffer_new ();
45  *   memory = gst_allocator_alloc (NULL, size, NULL);
46  *   gst_buffer_insert_memory (buffer, -1, memory);
47  *   ...
48  *   </programlisting>
49  * </example>
50  *
51  * Alternatively, use gst_buffer_new_allocate()
52  * to create a buffer with preallocated data of a given size.
53  *
54  * Buffers can contain a list of #GstMemory objects. You can retrieve how many
55  * memory objects with gst_buffer_n_memory() and you can get a pointer
56  * to memory with gst_buffer_peek_memory()
57  *
58  * A buffer will usually have timestamps, and a duration, but neither of these
59  * are guaranteed (they may be set to #GST_CLOCK_TIME_NONE). Whenever a
60  * meaningful value can be given for these, they should be set. The timestamps
61  * and duration are measured in nanoseconds (they are #GstClockTime values).
62  *
63  * The buffer DTS refers to the timestamp when the buffer should be decoded and
64  * is usually monotonically increasing. The buffer PTS refers to the timestamp when
65  * the buffer content should be presented to the user and is not always
66  * monotonically increasing.
67  *
68  * A buffer can also have one or both of a start and an end offset. These are
69  * media-type specific. For video buffers, the start offset will generally be
70  * the frame number. For audio buffers, it will be the number of samples
71  * produced so far. For compressed data, it could be the byte offset in a
72  * source or destination file. Likewise, the end offset will be the offset of
73  * the end of the buffer. These can only be meaningfully interpreted if you
74  * know the media type of the buffer (the preceeding CAPS event). Either or both
75  * can be set to #GST_BUFFER_OFFSET_NONE.
76  *
77  * gst_buffer_ref() is used to increase the refcount of a buffer. This must be
78  * done when you want to keep a handle to the buffer after pushing it to the
79  * next element. The buffer refcount determines the writability of the buffer, a
80  * buffer is only writable when the refcount is exactly 1, i.e. when the caller
81  * has the only reference to the buffer.
82  *
83  * To efficiently create a smaller buffer out of an existing one, you can
84  * use gst_buffer_copy_region(). This method tries to share the memory objects
85  * between the two buffers.
86  *
87  * If a plug-in wants to modify the buffer data or metadata in-place, it should
88  * first obtain a buffer that is safe to modify by using
89  * gst_buffer_make_writable().  This function is optimized so that a copy will
90  * only be made when it is necessary.
91  *
92  * Several flags of the buffer can be set and unset with the
93  * GST_BUFFER_FLAG_SET() and GST_BUFFER_FLAG_UNSET() macros. Use
94  * GST_BUFFER_FLAG_IS_SET() to test if a certain #GstBufferFlag is set.
95  *
96  * Buffers can be efficiently merged into a larger buffer with
97  * gst_buffer_append(). Copying of memory will only be done when absolutely
98  * needed.
99  *
100  * Arbitrary extra metadata can be set on a buffer with gst_buffer_add_meta().
101  * Metadata can be retrieved with gst_buffer_get_meta(). See also #GstMeta
102  *
103  * An element should either unref the buffer or push it out on a src pad
104  * using gst_pad_push() (see #GstPad).
105  *
106  * Buffers are usually freed by unreffing them with gst_buffer_unref(). When
107  * the refcount drops to 0, any memory and metadata pointed to by the buffer is
108  * unreffed as well. Buffers allocated from a #GstBufferPool will be returned to
109  * the pool when the refcount drops to 0.
110  *
111  * Last reviewed on 2012-03-28 (0.11.3)
112  */
113 #include "gst_private.h"
114
115 #ifdef HAVE_UNISTD_H
116 #include <unistd.h>
117 #endif
118 #ifdef HAVE_STDLIB_H
119 #include <stdlib.h>
120 #endif
121
122 #include "gstbuffer.h"
123 #include "gstbufferpool.h"
124 #include "gstinfo.h"
125 #include "gstutils.h"
126 #include "gstversion.h"
127
128 GType _gst_buffer_type = 0;
129
130 typedef struct _GstMetaItem GstMetaItem;
131
132 struct _GstMetaItem
133 {
134   GstMetaItem *next;
135   GstMeta meta;
136 };
137 #define ITEM_SIZE(info) ((info)->size + sizeof (GstMetaItem))
138
139 #define GST_BUFFER_MEM_MAX         16
140
141 #define GST_BUFFER_SLICE_SIZE(b)   (((GstBufferImpl *)(b))->slice_size)
142 #define GST_BUFFER_MEM_LEN(b)      (((GstBufferImpl *)(b))->len)
143 #define GST_BUFFER_MEM_ARRAY(b)    (((GstBufferImpl *)(b))->mem)
144 #define GST_BUFFER_MEM_PTR(b,i)    (((GstBufferImpl *)(b))->mem[i])
145 #define GST_BUFFER_BUFMEM(b)       (((GstBufferImpl *)(b))->bufmem)
146 #define GST_BUFFER_META(b)         (((GstBufferImpl *)(b))->item)
147
148 typedef struct
149 {
150   GstBuffer buffer;
151
152   gsize slice_size;
153
154   /* the memory blocks */
155   guint len;
156   GstMemory *mem[GST_BUFFER_MEM_MAX];
157
158   /* memory of the buffer when allocated from 1 chunk */
159   GstMemory *bufmem;
160
161   /* FIXME, make metadata allocation more efficient by using part of the
162    * GstBufferImpl */
163   GstMetaItem *item;
164 } GstBufferImpl;
165
166
167 static gboolean
168 _is_span (GstMemory ** mem, gsize len, gsize * poffset, GstMemory ** parent)
169 {
170   GstMemory *mcur, *mprv;
171   gboolean have_offset = FALSE;
172   gsize i;
173
174   mcur = mprv = NULL;
175
176   for (i = 0; i < len; i++) {
177     if (mcur)
178       mprv = mcur;
179     mcur = mem[i];
180
181     if (mprv && mcur) {
182       gsize poffs;
183
184       /* check if memory is contiguous */
185       if (!gst_memory_is_span (mprv, mcur, &poffs))
186         return FALSE;
187
188       if (!have_offset) {
189         if (poffset)
190           *poffset = poffs;
191         if (parent)
192           *parent = mprv->parent;
193
194         have_offset = TRUE;
195       }
196     }
197   }
198   return have_offset;
199 }
200
201 static GstMemory *
202 _get_merged_memory (GstBuffer * buffer, guint idx, guint length)
203 {
204   GstMemory **mem, *result = NULL;
205
206   GST_CAT_LOG (GST_CAT_BUFFER, "buffer %p, idx %u, length %u", buffer, idx,
207       length);
208
209   mem = GST_BUFFER_MEM_ARRAY (buffer);
210
211   if (G_UNLIKELY (length == 0)) {
212     result = NULL;
213   } else if (G_LIKELY (length == 1)) {
214     result = gst_memory_ref (mem[idx]);
215   } else {
216     GstMemory *parent = NULL;
217     gsize size, poffset = 0;
218
219     size = gst_buffer_get_size (buffer);
220
221     if (G_UNLIKELY (_is_span (mem + idx, length, &poffset, &parent))) {
222       if (!GST_MEMORY_IS_NO_SHARE (parent))
223         result = gst_memory_share (parent, poffset, size);
224       if (!result) {
225         GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy for merge %p", parent);
226         result = gst_memory_copy (parent, poffset, size);
227       }
228     } else {
229       gsize i, tocopy, left;
230       GstMapInfo sinfo, dinfo;
231       guint8 *ptr;
232
233       result = gst_allocator_alloc (NULL, size, NULL);
234       gst_memory_map (result, &dinfo, GST_MAP_WRITE);
235
236       ptr = dinfo.data;
237       left = size;
238
239       for (i = idx; i < length && left > 0; i++) {
240         gst_memory_map (mem[i], &sinfo, GST_MAP_READ);
241         tocopy = MIN (sinfo.size, left);
242         GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
243             "memcpy %" G_GSIZE_FORMAT " bytes for merge %p from memory %p",
244             tocopy, result, mem[i]);
245         memcpy (ptr, (guint8 *) sinfo.data, tocopy);
246         left -= tocopy;
247         ptr += tocopy;
248         gst_memory_unmap (mem[i], &sinfo);
249       }
250       gst_memory_unmap (result, &dinfo);
251     }
252   }
253   return result;
254 }
255
256 static void
257 _replace_memory (GstBuffer * buffer, guint len, guint idx, guint length,
258     GstMemory * mem)
259 {
260   gsize end, i;
261
262   end = idx + length;
263
264   GST_CAT_LOG (GST_CAT_BUFFER,
265       "buffer %p replace %u-%" G_GSIZE_FORMAT " with memory %p", buffer, idx,
266       end, mem);
267
268   /* unref old memory */
269   for (i = idx; i < end; i++) {
270     GstMemory *old = GST_BUFFER_MEM_PTR (buffer, i);
271
272     gst_memory_unlock (old, GST_LOCK_FLAG_EXCLUSIVE);
273     gst_memory_unref (old);
274   }
275
276   if (mem != NULL) {
277     /* replace with single memory */
278     gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
279     GST_BUFFER_MEM_PTR (buffer, idx) = mem;
280     idx++;
281     length--;
282   }
283
284   if (end < len) {
285     g_memmove (&GST_BUFFER_MEM_PTR (buffer, idx),
286         &GST_BUFFER_MEM_PTR (buffer, end), (len - end) * sizeof (gpointer));
287   }
288   GST_BUFFER_MEM_LEN (buffer) = len - length;
289 }
290
291 static inline void
292 _memory_add (GstBuffer * buffer, gint idx, GstMemory * mem, gboolean lock)
293 {
294   guint i, len = GST_BUFFER_MEM_LEN (buffer);
295
296   GST_CAT_LOG (GST_CAT_BUFFER, "buffer %p, idx %d, mem %p, lock %d", buffer,
297       idx, mem, lock);
298
299   if (G_UNLIKELY (len >= GST_BUFFER_MEM_MAX)) {
300     /* too many buffer, span them. */
301     /* FIXME, there is room for improvement here: We could only try to merge
302      * 2 buffers to make some room. If we can't efficiently merge 2 buffers we
303      * could try to only merge the two smallest buffers to avoid memcpy, etc. */
304     GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "memory array overflow in buffer %p",
305         buffer);
306     _replace_memory (buffer, len, 0, len, _get_merged_memory (buffer, 0, len));
307     /* we now have 1 single spanned buffer */
308     len = 1;
309   }
310
311   if (idx == -1)
312     idx = len;
313
314   for (i = len; i > idx; i--) {
315     /* move buffers to insert, FIXME, we need to insert first and then merge */
316     GST_BUFFER_MEM_PTR (buffer, i) = GST_BUFFER_MEM_PTR (buffer, i - 1);
317   }
318   /* and insert the new buffer */
319   if (lock)
320     gst_memory_lock (mem, GST_LOCK_FLAG_EXCLUSIVE);
321   GST_BUFFER_MEM_PTR (buffer, idx) = mem;
322   GST_BUFFER_MEM_LEN (buffer) = len + 1;
323 }
324
325 GST_DEFINE_MINI_OBJECT_TYPE (GstBuffer, gst_buffer);
326
327 void
328 _priv_gst_buffer_initialize (void)
329 {
330   _gst_buffer_type = gst_buffer_get_type ();
331 }
332
333 /**
334  * gst_buffer_copy_into:
335  * @dest: a destination #GstBuffer
336  * @src: a source #GstBuffer
337  * @flags: flags indicating what metadata fields should be copied.
338  * @offset: offset to copy from
339  * @size: total size to copy. If -1, all data is copied.
340  *
341  * Copies the information from @src into @dest.
342  *
343  * If @dest already contains memory and @flags contains GST_BUFFER_COPY_MEMORY,
344  * the memory from @src will be appended to @dest.
345  *
346  * @flags indicate which fields will be copied.
347  *
348  * Returns: %TRUE if the copying succeeded, %FALSE otherwise.
349  */
350 gboolean
351 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
352     GstBufferCopyFlags flags, gsize offset, gsize size)
353 {
354   GstMetaItem *walk;
355   gsize bufsize;
356   gboolean region = FALSE;
357
358   g_return_val_if_fail (dest != NULL, FALSE);
359   g_return_val_if_fail (src != NULL, FALSE);
360
361   /* nothing to copy if the buffers are the same */
362   if (G_UNLIKELY (dest == src))
363     return TRUE;
364
365   g_return_val_if_fail (gst_buffer_is_writable (dest), FALSE);
366
367   bufsize = gst_buffer_get_size (src);
368   g_return_val_if_fail (bufsize >= offset, FALSE);
369   if (offset > 0)
370     region = TRUE;
371   if (size == -1)
372     size = bufsize - offset;
373   if (size < bufsize)
374     region = TRUE;
375   g_return_val_if_fail (bufsize >= offset + size, FALSE);
376
377   GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p, offset %" G_GSIZE_FORMAT
378       "-%" G_GSIZE_FORMAT "/%" G_GSIZE_FORMAT, src, dest, offset, size,
379       bufsize);
380
381   if (flags & GST_BUFFER_COPY_FLAGS) {
382     /* copy flags */
383     GST_MINI_OBJECT_FLAGS (dest) = GST_MINI_OBJECT_FLAGS (src);
384   }
385
386   if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
387     if (offset == 0) {
388       GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src);
389       GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src);
390       GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
391       if (size == bufsize) {
392         GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
393         GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
394       }
395     } else {
396       GST_BUFFER_PTS (dest) = GST_CLOCK_TIME_NONE;
397       GST_BUFFER_DTS (dest) = GST_CLOCK_TIME_NONE;
398       GST_BUFFER_DURATION (dest) = GST_CLOCK_TIME_NONE;
399       GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET_NONE;
400       GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_NONE;
401     }
402   }
403
404   if (flags & GST_BUFFER_COPY_MEMORY) {
405     gsize skip, left, len, dest_len, i, bsize;
406     gboolean deep;
407
408     deep = flags & GST_BUFFER_COPY_DEEP;
409
410     len = GST_BUFFER_MEM_LEN (src);
411     dest_len = GST_BUFFER_MEM_LEN (dest);
412     left = size;
413     skip = offset;
414
415     /* copy and make regions of the memory */
416     for (i = 0; i < len && left > 0; i++) {
417       GstMemory *mem = GST_BUFFER_MEM_PTR (src, i);
418
419       bsize = gst_memory_get_sizes (mem, NULL, NULL);
420
421       if (bsize <= skip) {
422         /* don't copy buffer */
423         skip -= bsize;
424       } else {
425         GstMemory *newmem = NULL;
426         gsize tocopy;
427
428         tocopy = MIN (bsize - skip, left);
429
430         if (tocopy < bsize && !deep && !GST_MEMORY_IS_NO_SHARE (mem)) {
431           /* we need to clip something */
432           newmem = gst_memory_share (mem, skip, tocopy);
433           if (newmem)
434             skip = 0;
435         }
436
437         if (deep || GST_MEMORY_IS_NO_SHARE (mem) || (!newmem && tocopy < bsize)) {
438           /* deep copy or we're not allowed to share this memory
439            * between buffers, always copy then */
440           newmem = gst_memory_copy (mem, skip, tocopy);
441           skip = 0;
442         } else if (!newmem) {
443           newmem = gst_memory_ref (mem);
444         }
445
446         if (!newmem) {
447           gst_buffer_remove_memory_range (dest, dest_len, -1);
448           return FALSE;
449         }
450
451         _memory_add (dest, -1, newmem, TRUE);
452         left -= tocopy;
453       }
454     }
455     if (flags & GST_BUFFER_COPY_MERGE) {
456       GstMemory *mem;
457
458       len = GST_BUFFER_MEM_LEN (dest);
459       mem = _get_merged_memory (dest, 0, len);
460       if (!mem) {
461         gst_buffer_remove_memory_range (dest, dest_len, -1);
462         return FALSE;
463       }
464       _replace_memory (dest, len, 0, len, mem);
465     }
466   }
467
468   if (flags & GST_BUFFER_COPY_META) {
469     for (walk = GST_BUFFER_META (src); walk; walk = walk->next) {
470       GstMeta *meta = &walk->meta;
471       const GstMetaInfo *info = meta->info;
472
473       if (info->transform_func) {
474         GstMetaTransformCopy copy_data;
475
476         copy_data.region = region;
477         copy_data.offset = offset;
478         copy_data.size = size;
479
480         info->transform_func (dest, meta, src,
481             _gst_meta_transform_copy, &copy_data);
482       }
483     }
484   }
485
486   return TRUE;
487 }
488
489 static GstBuffer *
490 _gst_buffer_copy (GstBuffer * buffer)
491 {
492   GstBuffer *copy;
493
494   g_return_val_if_fail (buffer != NULL, NULL);
495
496   /* create a fresh new buffer */
497   copy = gst_buffer_new ();
498
499   /* we simply copy everything from our parent */
500   if (!gst_buffer_copy_into (copy, buffer, GST_BUFFER_COPY_ALL, 0, -1))
501     gst_buffer_replace (&copy, NULL);
502
503   return copy;
504 }
505
506 /* the default dispose function revives the buffer and returns it to the
507  * pool when there is a pool */
508 static gboolean
509 _gst_buffer_dispose (GstBuffer * buffer)
510 {
511   GstBufferPool *pool;
512
513   /* no pool, do free */
514   if ((pool = buffer->pool) == NULL)
515     return TRUE;
516
517   /* keep the buffer alive */
518   gst_buffer_ref (buffer);
519   /* return the buffer to the pool */
520   GST_CAT_LOG (GST_CAT_BUFFER, "release %p to pool %p", buffer, pool);
521   gst_buffer_pool_release_buffer (pool, buffer);
522
523   return FALSE;
524 }
525
526 static void
527 _gst_buffer_free (GstBuffer * buffer)
528 {
529   GstMetaItem *walk, *next;
530   guint i, len;
531   gsize msize;
532
533   g_return_if_fail (buffer != NULL);
534
535   GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
536
537   /* free metadata */
538   for (walk = GST_BUFFER_META (buffer); walk; walk = next) {
539     GstMeta *meta = &walk->meta;
540     const GstMetaInfo *info = meta->info;
541
542     /* call free_func if any */
543     if (info->free_func)
544       info->free_func (meta, buffer);
545
546     next = walk->next;
547     /* and free the slice */
548     g_slice_free1 (ITEM_SIZE (info), walk);
549   }
550
551   /* get the size, when unreffing the memory, we could also unref the buffer
552    * itself */
553   msize = GST_BUFFER_SLICE_SIZE (buffer);
554
555   /* free our memory */
556   len = GST_BUFFER_MEM_LEN (buffer);
557   for (i = 0; i < len; i++) {
558     gst_memory_unlock (GST_BUFFER_MEM_PTR (buffer, i), GST_LOCK_FLAG_EXCLUSIVE);
559     gst_memory_unref (GST_BUFFER_MEM_PTR (buffer, i));
560   }
561
562   /* we set msize to 0 when the buffer is part of the memory block */
563   if (msize) {
564 #ifdef USE_POISONING
565     memset (buffer, 0xff, msize);
566 #endif
567     g_slice_free1 (msize, buffer);
568   } else {
569     gst_memory_unref (GST_BUFFER_BUFMEM (buffer));
570   }
571 }
572
573 static void
574 gst_buffer_init (GstBufferImpl * buffer, gsize size)
575 {
576   gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), 0, _gst_buffer_type,
577       (GstMiniObjectCopyFunction) _gst_buffer_copy,
578       (GstMiniObjectDisposeFunction) _gst_buffer_dispose,
579       (GstMiniObjectFreeFunction) _gst_buffer_free);
580
581   GST_BUFFER_SLICE_SIZE (buffer) = size;
582
583   GST_BUFFER (buffer)->pool = NULL;
584   GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
585   GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
586   GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
587   GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
588   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
589
590   GST_BUFFER_MEM_LEN (buffer) = 0;
591   GST_BUFFER_META (buffer) = NULL;
592 }
593
594 /**
595  * gst_buffer_new:
596  *
597  * Creates a newly allocated buffer without any data.
598  *
599  * MT safe.
600  *
601  * Returns: (transfer full): the new #GstBuffer.
602  */
603 GstBuffer *
604 gst_buffer_new (void)
605 {
606   GstBufferImpl *newbuf;
607
608   newbuf = g_slice_new (GstBufferImpl);
609   GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
610
611   gst_buffer_init (newbuf, sizeof (GstBufferImpl));
612
613   return GST_BUFFER_CAST (newbuf);
614 }
615
616 /**
617  * gst_buffer_new_allocate:
618  * @allocator: (transfer none) (allow-none): the #GstAllocator to use, or NULL to use the
619  *     default allocator
620  * @size: the size in bytes of the new buffer's data.
621  * @params: (transfer none) (allow-none): optional parameters
622  *
623  * Tries to create a newly allocated buffer with data of the given size and
624  * extra parameters from @allocator. If the requested amount of memory can't be
625  * allocated, NULL will be returned. The allocated buffer memory is not cleared.
626  *
627  * When @allocator is NULL, the default memory allocator will be used.
628  *
629  * Note that when @size == 0, the buffer will not have memory associated with it.
630  *
631  * MT safe.
632  *
633  * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
634  *     be allocated.
635  */
636 GstBuffer *
637 gst_buffer_new_allocate (GstAllocator * allocator, gsize size,
638     GstAllocationParams * params)
639 {
640   GstBuffer *newbuf;
641   GstMemory *mem;
642 #if 0
643   guint8 *data;
644   gsize asize;
645 #endif
646
647 #if 1
648   if (size > 0) {
649     mem = gst_allocator_alloc (allocator, size, params);
650     if (G_UNLIKELY (mem == NULL))
651       goto no_memory;
652   } else {
653     mem = NULL;
654   }
655
656   newbuf = gst_buffer_new ();
657
658   if (mem != NULL)
659     _memory_add (newbuf, -1, mem, TRUE);
660
661   GST_CAT_LOG (GST_CAT_BUFFER,
662       "new buffer %p of size %" G_GSIZE_FORMAT " from allocator %p", newbuf,
663       size, allocator);
664 #endif
665
666 #if 0
667   asize = sizeof (GstBufferImpl) + size;
668   data = g_slice_alloc (asize);
669   if (G_UNLIKELY (data == NULL))
670     goto no_memory;
671
672   newbuf = GST_BUFFER_CAST (data);
673
674   gst_buffer_init ((GstBufferImpl *) data, asize);
675   if (size > 0) {
676     mem = gst_memory_new_wrapped (0, data + sizeof (GstBufferImpl), NULL,
677         size, 0, size);
678     _memory_add (newbuf, -1, mem, TRUE);
679   }
680 #endif
681
682 #if 0
683   /* allocate memory and buffer, it might be interesting to do this but there
684    * are many complications. We need to keep the memory mapped to access the
685    * buffer fields and the memory for the buffer might be just very slow. We
686    * also need to do some more magic to get the alignment right. */
687   asize = sizeof (GstBufferImpl) + size;
688   mem = gst_allocator_alloc (allocator, asize, align);
689   if (G_UNLIKELY (mem == NULL))
690     goto no_memory;
691
692   /* map the data part and init the buffer in it, set the buffer size to 0 so
693    * that a finalize won't free the buffer */
694   data = gst_memory_map (mem, &asize, NULL, GST_MAP_WRITE);
695   gst_buffer_init ((GstBufferImpl *) data, 0);
696   gst_memory_unmap (mem);
697
698   /* strip off the buffer */
699   gst_memory_resize (mem, sizeof (GstBufferImpl), size);
700
701   newbuf = GST_BUFFER_CAST (data);
702   GST_BUFFER_BUFMEM (newbuf) = mem;
703
704   if (size > 0)
705     _memory_add (newbuf, -1, gst_memory_ref (mem), TRUE);
706 #endif
707
708   return newbuf;
709
710   /* ERRORS */
711 no_memory:
712   {
713     GST_CAT_WARNING (GST_CAT_BUFFER,
714         "failed to allocate %" G_GSIZE_FORMAT " bytes", size);
715     return NULL;
716   }
717 }
718
719 /**
720  * gst_buffer_new_wrapped_full:
721  * @flags: #GstMemoryFlags
722  * @data: (array length=size) (element-type guint8) (transfer none): data to wrap
723  * @maxsize: allocated size of @data
724  * @offset: offset in @data
725  * @size: size of valid data
726  * @user_data: (allow-none): user_data
727  * @notify: (allow-none) (scope async) (closure user_data): called with @user_data when the memory is freed
728  *
729  * Allocate a new buffer that wraps the given memory. @data must point to
730  * @maxsize of memory, the wrapped buffer will have the region from @offset and
731  * @size visible.
732  *
733  * When the buffer is destroyed, @notify will be called with @user_data.
734  *
735  * The prefix/padding must be filled with 0 if @flags contains
736  * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
737  *
738  * Returns: (transfer full): a new #GstBuffer
739  */
740 GstBuffer *
741 gst_buffer_new_wrapped_full (GstMemoryFlags flags, gpointer data,
742     gsize maxsize, gsize offset, gsize size, gpointer user_data,
743     GDestroyNotify notify)
744 {
745   GstBuffer *newbuf;
746
747   newbuf = gst_buffer_new ();
748   gst_buffer_append_memory (newbuf,
749       gst_memory_new_wrapped (flags, data, maxsize, offset, size,
750           user_data, notify));
751
752   return newbuf;
753 }
754
755 /**
756  * gst_buffer_new_wrapped:
757  * @data: (array length=size) (element-type guint8) (transfer full): data to wrap
758  * @size: allocated size of @data
759  *
760  * Creates a new buffer that wraps the given @data. The memory will be freed
761  * with g_free and will be marked writable.
762  *
763  * MT safe.
764  *
765  * Returns: (transfer full): a new #GstBuffer
766  */
767 GstBuffer *
768 gst_buffer_new_wrapped (gpointer data, gsize size)
769 {
770   return gst_buffer_new_wrapped_full (0, data, size, 0, size, data, g_free);
771 }
772
773 /**
774  * gst_buffer_n_memory:
775  * @buffer: a #GstBuffer.
776  *
777  * Get the amount of memory blocks that this buffer has.
778  *
779  * Returns: (transfer full): the amount of memory block in this buffer.
780  */
781 guint
782 gst_buffer_n_memory (GstBuffer * buffer)
783 {
784   g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
785
786   return GST_BUFFER_MEM_LEN (buffer);
787 }
788
789 /**
790  * gst_buffer_prepend_memory:
791  * @buffer: a #GstBuffer.
792  * @mem: (transfer full): a #GstMemory.
793  *
794  * Prepend the memory block @mem to @buffer. This function takes
795  * ownership of @mem and thus doesn't increase its refcount.
796  */
797 void
798 gst_buffer_prepend_memory (GstBuffer * buffer, GstMemory * mem)
799 {
800   gst_buffer_insert_memory (buffer, 0, mem);
801 }
802
803 /**
804  * gst_buffer_append_memory:
805  * @buffer: a #GstBuffer.
806  * @mem: (transfer full): a #GstMemory.
807  *
808  * Append the memory block @mem to @buffer. This function takes
809  * ownership of @mem and thus doesn't increase its refcount.
810  */
811 void
812 gst_buffer_append_memory (GstBuffer * buffer, GstMemory * mem)
813 {
814   gst_buffer_insert_memory (buffer, -1, mem);
815 }
816
817 /**
818  * gst_buffer_insert_memory:
819  * @buffer: a #GstBuffer.
820  * @idx: the index to add the memory at, or -1 to append it to the end
821  * @mem: (transfer full): a #GstMemory.
822  *
823  * Insert the memory block @mem to @buffer at @idx. This function takes ownership
824  * of @mem and thus doesn't increase its refcount.
825  */
826 void
827 gst_buffer_insert_memory (GstBuffer * buffer, gint idx, GstMemory * mem)
828 {
829   g_return_if_fail (GST_IS_BUFFER (buffer));
830   g_return_if_fail (gst_buffer_is_writable (buffer));
831   g_return_if_fail (mem != NULL);
832   g_return_if_fail (idx == -1 ||
833       (idx >= 0 && idx <= GST_BUFFER_MEM_LEN (buffer)));
834
835   _memory_add (buffer, idx, mem, TRUE);
836 }
837
838 static GstMemory *
839 _get_mapped (GstBuffer * buffer, guint idx, GstMapInfo * info,
840     GstMapFlags flags)
841 {
842   GstMemory *mem, *mapped;
843
844   mem = gst_memory_ref (GST_BUFFER_MEM_PTR (buffer, idx));
845
846   mapped = gst_memory_make_mapped (mem, info, flags);
847
848   if (mapped != mem) {
849     /* memory changed, lock new memory */
850     gst_memory_lock (mapped, GST_LOCK_FLAG_EXCLUSIVE);
851     GST_BUFFER_MEM_PTR (buffer, idx) = mapped;
852     /* unlock old memory */
853     gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
854   }
855   gst_memory_unref (mem);
856
857   return mapped;
858 }
859
860 /**
861  * gst_buffer_peek_memory:
862  * @buffer: a #GstBuffer.
863  * @idx: an index
864  *
865  * Get the memory block at @idx in @buffer. The memory block stays valid until
866  * the memory block in @buffer is removed, replaced or merged, typically with
867  * any call that modifies the memory in @buffer.
868  *
869  * Since this call does not influence the refcount of the memory,
870  * gst_memory_is_writable() can be used to check if @buffer is the sole owner
871  * of the returned memory.
872  *
873  * Returns: (transfer none): the #GstMemory at @idx.
874  */
875 GstMemory *
876 gst_buffer_peek_memory (GstBuffer * buffer, guint idx)
877 {
878   guint len;
879
880   g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
881   len = GST_BUFFER_MEM_LEN (buffer);
882   g_return_val_if_fail (idx < len, NULL);
883
884   return GST_BUFFER_MEM_PTR (buffer, idx);
885 }
886
887 /**
888  * gst_buffer_get_memory:
889  * @buffer: a #GstBuffer.
890  * @idx: an index
891  *
892  * Get the memory block at index @idx in @buffer.
893  *
894  * Returns: (transfer full): a #GstMemory that contains the data of the
895  * memory block at @idx. Use gst_memory_unref () after usage.
896  */
897 GstMemory *
898 gst_buffer_get_memory (GstBuffer * buffer, guint idx)
899 {
900   return gst_buffer_get_memory_range (buffer, idx, 1);
901 }
902
903 /**
904  * gst_buffer_get_all_memory:
905  * @buffer: a #GstBuffer.
906  *
907  * Get all the memory block in @buffer. The memory blocks will be merged
908  * into one large #GstMemory.
909  *
910  * Returns: (transfer full): a #GstMemory that contains the merged memory.
911  * Use gst_memory_unref () after usage.
912  */
913 GstMemory *
914 gst_buffer_get_all_memory (GstBuffer * buffer)
915 {
916   return gst_buffer_get_memory_range (buffer, 0, -1);
917 }
918
919 /**
920  * gst_buffer_get_memory_range:
921  * @buffer: a #GstBuffer.
922  * @idx: an index
923  * @length: a length
924  *
925  * Get @length memory blocks in @buffer starting at @idx. The memory blocks will
926  * be merged into one large #GstMemory.
927  *
928  * If @length is -1, all memory starting from @idx is merged.
929  *
930  * Returns: (transfer full): a #GstMemory that contains the merged data of @length
931  *    blocks starting at @idx. Use gst_memory_unref () after usage.
932  */
933 GstMemory *
934 gst_buffer_get_memory_range (GstBuffer * buffer, guint idx, gint length)
935 {
936   guint len;
937
938   GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
939
940   g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
941   len = GST_BUFFER_MEM_LEN (buffer);
942   g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
943       (length == -1 && idx < len) || (length > 0 && length + idx <= len), NULL);
944
945   if (length == -1)
946     length = len - idx;
947
948   return _get_merged_memory (buffer, idx, length);
949 }
950
951 /**
952  * gst_buffer_replace_memory:
953  * @buffer: a #GstBuffer.
954  * @idx: an index
955  * @mem: (transfer full): a #GstMemory
956  *
957  * Replaces the memory block at index @idx in @buffer with @mem.
958  */
959 void
960 gst_buffer_replace_memory (GstBuffer * buffer, guint idx, GstMemory * mem)
961 {
962   gst_buffer_replace_memory_range (buffer, idx, 1, mem);
963 }
964
965 /**
966  * gst_buffer_replace_all_memory:
967  * @buffer: a #GstBuffer.
968  * @mem: (transfer full): a #GstMemory
969  *
970  * Replaces all memory in @buffer with @mem.
971  */
972 void
973 gst_buffer_replace_all_memory (GstBuffer * buffer, GstMemory * mem)
974 {
975   gst_buffer_replace_memory_range (buffer, 0, -1, mem);
976 }
977
978 /**
979  * gst_buffer_replace_memory_range:
980  * @buffer: a #GstBuffer.
981  * @idx: an index
982  * @length: a length should not be 0
983  * @mem: (transfer full): a #GstMemory
984  *
985  * Replaces @length memory blocks in @buffer starting at @idx with @mem.
986  *
987  * If @length is -1, all memory starting from @idx will be removed and
988  * replaced with @mem.
989  *
990  * @buffer should be writable.
991  */
992 void
993 gst_buffer_replace_memory_range (GstBuffer * buffer, guint idx, gint length,
994     GstMemory * mem)
995 {
996   guint len;
997
998   g_return_if_fail (GST_IS_BUFFER (buffer));
999   g_return_if_fail (gst_buffer_is_writable (buffer));
1000
1001   GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d, %p", idx, length, mem);
1002
1003   len = GST_BUFFER_MEM_LEN (buffer);
1004   g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
1005       (length == -1 && idx < len) || (length > 0 && length + idx <= len));
1006
1007   if (length == -1)
1008     length = len - idx;
1009
1010   _replace_memory (buffer, len, idx, length, mem);
1011 }
1012
1013 /**
1014  * gst_buffer_remove_memory:
1015  * @buffer: a #GstBuffer.
1016  * @idx: an index
1017  *
1018  * Remove the memory block in @b at index @i.
1019  */
1020 void
1021 gst_buffer_remove_memory (GstBuffer * buffer, guint idx)
1022 {
1023   gst_buffer_remove_memory_range (buffer, idx, 1);
1024 }
1025
1026 /**
1027  * gst_buffer_remove_all_memory:
1028  * @buffer: a #GstBuffer.
1029  *
1030  * Remove all the memory blocks in @buffer.
1031  */
1032 void
1033 gst_buffer_remove_all_memory (GstBuffer * buffer)
1034 {
1035   gst_buffer_remove_memory_range (buffer, 0, -1);
1036 }
1037
1038 /**
1039  * gst_buffer_remove_memory_range:
1040  * @buffer: a #GstBuffer.
1041  * @idx: an index
1042  * @length: a length
1043  *
1044  * Remove @length memory blocks in @buffer starting from @idx.
1045  *
1046  * @length can be -1, in which case all memory starting from @idx is removed.
1047  */
1048 void
1049 gst_buffer_remove_memory_range (GstBuffer * buffer, guint idx, gint length)
1050 {
1051   guint len;
1052
1053   g_return_if_fail (GST_IS_BUFFER (buffer));
1054   g_return_if_fail (gst_buffer_is_writable (buffer));
1055
1056   GST_CAT_DEBUG (GST_CAT_BUFFER, "idx %u, length %d", idx, length);
1057
1058   len = GST_BUFFER_MEM_LEN (buffer);
1059   g_return_if_fail ((len == 0 && idx == 0 && length == -1) ||
1060       (length == -1 && idx < len) || length + idx <= len);
1061
1062   if (length == -1)
1063     length = len - idx;
1064
1065   _replace_memory (buffer, len, idx, length, NULL);
1066 }
1067
1068 /**
1069  * gst_buffer_find_memory:
1070  * @buffer: a #GstBuffer.
1071  * @offset: an offset
1072  * @size: a size
1073  * @idx: (out): pointer to index
1074  * @length: (out): pointer to length
1075  * @skip: (out): pointer to skip
1076  *
1077  * Find the memory blocks that span @size bytes starting from @offset
1078  * in @buffer.
1079  *
1080  * When this function returns %TRUE, @idx will contain the index of the first
1081  * memory bock where the byte for @offset can be found and @length contains the
1082  * number of memory blocks containing the @size remaining bytes. @skip contains
1083  * the number of bytes to skip in the memory bock at @idx to get to the byte
1084  * for @offset.
1085  *
1086  * @size can be -1 to get all the memory blocks after @idx.
1087  *
1088  * Returns: %TRUE when @size bytes starting from @offset could be found in
1089  * @buffer and @idx, @length and @skip will be filled.
1090  */
1091 gboolean
1092 gst_buffer_find_memory (GstBuffer * buffer, gsize offset, gsize size,
1093     guint * idx, guint * length, gsize * skip)
1094 {
1095   guint i, len, found;
1096
1097   g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1098   g_return_val_if_fail (idx != NULL, FALSE);
1099   g_return_val_if_fail (length != NULL, FALSE);
1100   g_return_val_if_fail (skip != NULL, FALSE);
1101
1102   len = GST_BUFFER_MEM_LEN (buffer);
1103
1104   found = 0;
1105   for (i = 0; i < len; i++) {
1106     GstMemory *mem;
1107     gsize s;
1108
1109     mem = GST_BUFFER_MEM_PTR (buffer, i);
1110     s = gst_memory_get_sizes (mem, NULL, NULL);
1111
1112     if (s <= offset) {
1113       /* block before offset, or empty block, skip */
1114       offset -= s;
1115     } else {
1116       /* block after offset */
1117       if (found == 0) {
1118         /* first block, remember index and offset */
1119         *idx = i;
1120         *skip = offset;
1121         if (size == -1) {
1122           /* return remaining blocks */
1123           *length = len - i;
1124           return TRUE;
1125         }
1126         s -= offset;
1127         offset = 0;
1128       }
1129       /* count the amount of found bytes */
1130       found += s;
1131       if (found >= size) {
1132         /* we have enough bytes */
1133         *length = i - *idx + 1;
1134         return TRUE;
1135       }
1136     }
1137   }
1138   return FALSE;
1139 }
1140
1141 /**
1142  * gst_buffer_get_sizes:
1143  * @buffer: a #GstBuffer.
1144  * @offset: (out): a pointer to the offset
1145  * @maxsize: (out): a pointer to the maxsize
1146  *
1147  * Get the total size of the memory blocks in @b.
1148  *
1149  * When not %NULL, @offset will contain the offset of the data in the
1150  * first memory block in @buffer and @maxsize will contain the sum of
1151  * the size and @offset and the amount of extra padding on the last
1152  * memory block.  @offset and @maxsize can be used to resize the
1153  * buffer memory blocks with gst_buffer_resize().
1154  *
1155  * Returns: total size of the memory blocks in @buffer.
1156  */
1157 gsize
1158 gst_buffer_get_sizes (GstBuffer * buffer, gsize * offset, gsize * maxsize)
1159 {
1160   return gst_buffer_get_sizes_range (buffer, 0, -1, offset, maxsize);
1161 }
1162
1163 /**
1164  * gst_buffer_get_size:
1165  * @buffer: a #GstBuffer.
1166  *
1167  * Get the total size of the memory blocks in @buffer.
1168  *
1169  * Returns: total size of the memory blocks in @buffer.
1170  */
1171 gsize
1172 gst_buffer_get_size (GstBuffer * buffer)
1173 {
1174   return gst_buffer_get_sizes_range (buffer, 0, -1, NULL, NULL);
1175 }
1176
1177 /**
1178  * gst_buffer_get_sizes_range:
1179  * @buffer: a #GstBuffer.
1180  * @idx: an index
1181  * @length: a length
1182  * @offset: (out): a pointer to the offset
1183  * @maxsize: (out): a pointer to the maxsize
1184  *
1185  * Get the total size of @length memory blocks stating from @idx in @buffer.
1186  *
1187  * When not %NULL, @offset will contain the offset of the data in the
1188  * memory block in @buffer at @idx and @maxsize will contain the sum of the size
1189  * and @offset and the amount of extra padding on the memory block at @idx +
1190  * @length -1.
1191  * @offset and @maxsize can be used to resize the buffer memory blocks with
1192  * gst_buffer_resize_range().
1193  *
1194  * Returns: total size of @length memory blocks starting at @idx in @buffer.
1195  */
1196 gsize
1197 gst_buffer_get_sizes_range (GstBuffer * buffer, guint idx, gint length,
1198     gsize * offset, gsize * maxsize)
1199 {
1200   guint len;
1201   gsize size;
1202   GstMemory *mem;
1203
1204   g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1205   len = GST_BUFFER_MEM_LEN (buffer);
1206   g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1207       (length == -1 && idx < len) || (length + idx <= len), 0);
1208
1209   if (length == -1)
1210     length = len - idx;
1211
1212   if (G_LIKELY (length == 1)) {
1213     /* common case */
1214     mem = GST_BUFFER_MEM_PTR (buffer, idx);
1215     size = gst_memory_get_sizes (mem, offset, maxsize);
1216   } else {
1217     guint i, end;
1218     gsize extra, offs;
1219
1220     end = idx + length;
1221     size = offs = extra = 0;
1222     for (i = idx; i < end; i++) {
1223       gsize s, o, ms;
1224
1225       mem = GST_BUFFER_MEM_PTR (buffer, i);
1226       s = gst_memory_get_sizes (mem, &o, &ms);
1227
1228       if (s) {
1229         if (size == 0)
1230           /* first size, take accumulated data before as the offset */
1231           offs = extra + o;
1232         /* add sizes */
1233         size += s;
1234         /* save the amount of data after this block */
1235         extra = ms - (o + s);
1236       } else {
1237         /* empty block, add as extra */
1238         extra += ms;
1239       }
1240     }
1241     if (offset)
1242       *offset = offs;
1243     if (maxsize)
1244       *maxsize = offs + size + extra;
1245   }
1246   return size;
1247 }
1248
1249 /**
1250  * gst_buffer_resize:
1251  * @buffer: a #GstBuffer.
1252  * @offset: the offset adjustement
1253  * @size: the new size or -1 to just adjust the offset
1254  *
1255  * Set the offset and total size of the memory blocks in @buffer.
1256  */
1257 void
1258 gst_buffer_resize (GstBuffer * buffer, gssize offset, gssize size)
1259 {
1260   gst_buffer_resize_range (buffer, 0, -1, offset, size);
1261 }
1262
1263 /**
1264  * gst_buffer_set_size:
1265  * @buffer: a #GstBuffer.
1266  * @size: the new size
1267  *
1268  * Set the total size of the memory blocks in @buffer.
1269  */
1270 void
1271 gst_buffer_set_size (GstBuffer * buffer, gssize size)
1272 {
1273   gst_buffer_resize_range (buffer, 0, -1, 0, size);
1274 }
1275
1276 /**
1277  * gst_buffer_resize_range:
1278  * @buffer: a #GstBuffer.
1279  * @idx: an index
1280  * @length: a length
1281  * @offset: the offset adjustement
1282  * @size: the new size or -1 to just adjust the offset
1283  *
1284  * Set the total size of the @length memory blocks starting at @idx in
1285  * @buffer
1286  *
1287  * Returns: %TRUE if resizing succeeded, %FALSE otherwise.
1288  */
1289 gboolean
1290 gst_buffer_resize_range (GstBuffer * buffer, guint idx, gint length,
1291     gssize offset, gssize size)
1292 {
1293   guint i, len, end;
1294   gsize bsize, bufsize, bufoffs, bufmax;
1295
1296   g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
1297   g_return_val_if_fail (size >= -1, FALSE);
1298
1299   len = GST_BUFFER_MEM_LEN (buffer);
1300   g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1301       (length == -1 && idx < len) || (length + idx <= len), FALSE);
1302
1303   if (length == -1)
1304     length = len - idx;
1305
1306   bufsize = gst_buffer_get_sizes_range (buffer, idx, length, &bufoffs, &bufmax);
1307
1308   GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSSIZE_FORMAT
1309       " size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%"
1310       G_GSIZE_FORMAT, buffer, offset, size, bufsize, bufoffs, bufmax);
1311
1312   /* we can't go back further than the current offset or past the end of the
1313    * buffer */
1314   g_return_val_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
1315           && bufoffs + offset <= bufmax), FALSE);
1316   if (size == -1) {
1317     g_return_val_if_fail (bufsize >= offset, FALSE);
1318     size = bufsize - offset;
1319   }
1320   g_return_val_if_fail (bufmax >= bufoffs + offset + size, FALSE);
1321
1322   /* no change */
1323   if (offset == 0 && size == bufsize)
1324     return TRUE;
1325
1326   end = idx + length;
1327   /* copy and trim */
1328   for (i = idx; i < end; i++) {
1329     GstMemory *mem;
1330     gsize left, noffs;
1331
1332     mem = GST_BUFFER_MEM_PTR (buffer, i);
1333     bsize = gst_memory_get_sizes (mem, NULL, NULL);
1334
1335     noffs = 0;
1336     /* last buffer always gets resized to the remaining size */
1337     if (i + 1 == end)
1338       left = size;
1339     /* shrink buffers before the offset */
1340     else if ((gssize) bsize <= offset) {
1341       left = 0;
1342       noffs = offset - bsize;
1343       offset = 0;
1344     }
1345     /* clip other buffers */
1346     else
1347       left = MIN (bsize - offset, size);
1348
1349     if (offset != 0 || left != bsize) {
1350       if (gst_memory_is_writable (mem)) {
1351         gst_memory_resize (mem, offset, left);
1352       } else {
1353         GstMemory *newmem = NULL;
1354
1355         if (!GST_MEMORY_IS_NO_SHARE (mem))
1356           newmem = gst_memory_share (mem, offset, left);
1357
1358         if (!newmem)
1359           newmem = gst_memory_copy (mem, offset, left);
1360
1361         if (newmem == NULL)
1362           return FALSE;
1363
1364         gst_memory_lock (newmem, GST_LOCK_FLAG_EXCLUSIVE);
1365         GST_BUFFER_MEM_PTR (buffer, i) = newmem;
1366         gst_memory_unlock (mem, GST_LOCK_FLAG_EXCLUSIVE);
1367         gst_memory_unref (mem);
1368       }
1369     }
1370
1371     offset = noffs;
1372     size -= left;
1373   }
1374
1375   return TRUE;
1376 }
1377
1378 /**
1379  * gst_buffer_map:
1380  * @buffer: a #GstBuffer.
1381  * @info: (out): info about the mapping
1382  * @flags: flags for the mapping
1383  *
1384  * This function fills @info with the #GstMapInfo of all merged memory
1385  * blocks in @buffer.
1386  *
1387  * @flags describe the desired access of the memory. When @flags is
1388  * #GST_MAP_WRITE, @buffer should be writable (as returned from
1389  * gst_buffer_is_writable()).
1390  *
1391  * When @buffer is writable but the memory isn't, a writable copy will
1392  * automatically be created and returned. The readonly copy of the
1393  * buffer memory will then also be replaced with this writable copy.
1394  *
1395  * The memory in @info should be unmapped with gst_buffer_unmap() after
1396  * usage.
1397  *
1398  * Returns: %TRUE if the map succeeded and @info contains valid data.
1399  */
1400 gboolean
1401 gst_buffer_map (GstBuffer * buffer, GstMapInfo * info, GstMapFlags flags)
1402 {
1403   return gst_buffer_map_range (buffer, 0, -1, info, flags);
1404 }
1405
1406 /**
1407  * gst_buffer_map_range:
1408  * @buffer: a #GstBuffer.
1409  * @idx: an index
1410  * @length: a length
1411  * @info: (out): info about the mapping
1412  * @flags: flags for the mapping
1413  *
1414  * This function fills @info with the #GstMapInfo of @length merged memory blocks
1415  * starting at @idx in @buffer. When @length is -1, all memory blocks starting
1416  * from @idx are merged and mapped.
1417  *
1418  * @flags describe the desired access of the memory. When @flags is
1419  * #GST_MAP_WRITE, @buffer should be writable (as returned from
1420  * gst_buffer_is_writable()).
1421  *
1422  * When @buffer is writable but the memory isn't, a writable copy will
1423  * automatically be created and returned. The readonly copy of the buffer memory
1424  * will then also be replaced with this writable copy.
1425  *
1426  * The memory in @info should be unmapped with gst_buffer_unmap() after usage.
1427  *
1428  * Returns: %TRUE if the map succeeded and @info contains valid
1429  * data.
1430  */
1431 gboolean
1432 gst_buffer_map_range (GstBuffer * buffer, guint idx, gint length,
1433     GstMapInfo * info, GstMapFlags flags)
1434 {
1435   GstMemory *mem, *nmem;
1436   gboolean write, writable;
1437   gsize len;
1438
1439   g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE);
1440   g_return_val_if_fail (info != NULL, FALSE);
1441   len = GST_BUFFER_MEM_LEN (buffer);
1442   g_return_val_if_fail ((len == 0 && idx == 0 && length == -1) ||
1443       (length == -1 && idx < len) || (length > 0
1444           && length + idx <= len), FALSE);
1445
1446   GST_CAT_LOG (GST_CAT_BUFFER, "buffer %p, idx %u, length %d, flags %04x",
1447       buffer, idx, length, flags);
1448
1449   write = (flags & GST_MAP_WRITE) != 0;
1450   writable = gst_buffer_is_writable (buffer);
1451
1452   /* check if we can write when asked for write access */
1453   if (G_UNLIKELY (write && !writable))
1454     goto not_writable;
1455
1456   if (length == -1)
1457     length = len - idx;
1458
1459   mem = _get_merged_memory (buffer, idx, length);
1460   if (G_UNLIKELY (mem == NULL))
1461     goto no_memory;
1462
1463   /* now try to map */
1464   nmem = gst_memory_make_mapped (mem, info, flags);
1465   if (G_UNLIKELY (nmem == NULL))
1466     goto cannot_map;
1467
1468   /* if we merged or when the map returned a different memory, we try to replace
1469    * the memory in the buffer */
1470   if (G_UNLIKELY (length > 1 || nmem != mem)) {
1471     /* if the buffer is writable, replace the memory */
1472     if (writable) {
1473       _replace_memory (buffer, len, idx, length, gst_memory_ref (nmem));
1474     } else {
1475       if (len > 1) {
1476         GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
1477             "temporary mapping for memory %p in buffer %p", nmem, buffer);
1478       }
1479     }
1480   }
1481   return TRUE;
1482
1483   /* ERROR */
1484 not_writable:
1485   {
1486     GST_WARNING_OBJECT (buffer, "write map requested on non-writable buffer");
1487     g_critical ("write map requested on non-writable buffer");
1488     return FALSE;
1489   }
1490 no_memory:
1491   {
1492     /* empty buffer, we need to return NULL */
1493     GST_DEBUG_OBJECT (buffer, "can't get buffer memory");
1494     info->memory = NULL;
1495     info->data = NULL;
1496     info->size = 0;
1497     info->maxsize = 0;
1498     return TRUE;
1499   }
1500 cannot_map:
1501   {
1502     GST_DEBUG_OBJECT (buffer, "cannot map memory");
1503     return FALSE;
1504   }
1505 }
1506
1507 /**
1508  * gst_buffer_unmap:
1509  * @buffer: a #GstBuffer.
1510  * @info: a #GstMapInfo
1511  *
1512  * Release the memory previously mapped with gst_buffer_map().
1513  */
1514 void
1515 gst_buffer_unmap (GstBuffer * buffer, GstMapInfo * info)
1516 {
1517   g_return_if_fail (GST_IS_BUFFER (buffer));
1518   g_return_if_fail (info != NULL);
1519
1520   /* we need to check for NULL, it is possible that we tried to map a buffer
1521    * without memory and we should be able to unmap that fine */
1522   if (G_LIKELY (info->memory)) {
1523     gst_memory_unmap (info->memory, info);
1524     gst_memory_unref (info->memory);
1525   }
1526 }
1527
1528 /**
1529  * gst_buffer_fill:
1530  * @buffer: a #GstBuffer.
1531  * @offset: the offset to fill
1532  * @src: (array length=size) (element-type guint8): the source address
1533  * @size: the size to fill
1534  *
1535  * Copy @size bytes from @src to @buffer at @offset.
1536  *
1537  * Returns: The amount of bytes copied. This value can be lower than @size
1538  *    when @buffer did not contain enough data.
1539  */
1540 gsize
1541 gst_buffer_fill (GstBuffer * buffer, gsize offset, gconstpointer src,
1542     gsize size)
1543 {
1544   gsize i, len, left;
1545   const guint8 *ptr = src;
1546
1547   g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1548   g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1549   g_return_val_if_fail (src != NULL, 0);
1550
1551   GST_CAT_LOG (GST_CAT_BUFFER,
1552       "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
1553       offset, size);
1554
1555   len = GST_BUFFER_MEM_LEN (buffer);
1556   left = size;
1557
1558   for (i = 0; i < len && left > 0; i++) {
1559     GstMapInfo info;
1560     gsize tocopy;
1561     GstMemory *mem;
1562
1563     mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1564     if (info.size > offset) {
1565       /* we have enough */
1566       tocopy = MIN (info.size - offset, left);
1567       memcpy ((guint8 *) info.data + offset, ptr, tocopy);
1568       left -= tocopy;
1569       ptr += tocopy;
1570       offset = 0;
1571     } else {
1572       /* offset past buffer, skip */
1573       offset -= info.size;
1574     }
1575     gst_memory_unmap (mem, &info);
1576   }
1577   return size - left;
1578 }
1579
1580 /**
1581  * gst_buffer_extract:
1582  * @buffer: a #GstBuffer.
1583  * @offset: the offset to extract
1584  * @dest: the destination address
1585  * @size: the size to extract
1586  *
1587  * Copy @size bytes starting from @offset in @buffer to @dest.
1588  *
1589  * Returns: The amount of bytes extracted. This value can be lower than @size
1590  *    when @buffer did not contain enough data.
1591  */
1592 gsize
1593 gst_buffer_extract (GstBuffer * buffer, gsize offset, gpointer dest, gsize size)
1594 {
1595   gsize i, len, left;
1596   guint8 *ptr = dest;
1597
1598   g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1599   g_return_val_if_fail (dest != NULL, 0);
1600
1601   GST_CAT_LOG (GST_CAT_BUFFER,
1602       "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
1603       offset, size);
1604
1605   len = GST_BUFFER_MEM_LEN (buffer);
1606   left = size;
1607
1608   for (i = 0; i < len && left > 0; i++) {
1609     GstMapInfo info;
1610     gsize tocopy;
1611     GstMemory *mem;
1612
1613     mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1614     if (info.size > offset) {
1615       /* we have enough */
1616       tocopy = MIN (info.size - offset, left);
1617       memcpy (ptr, (guint8 *) info.data + offset, tocopy);
1618       left -= tocopy;
1619       ptr += tocopy;
1620       offset = 0;
1621     } else {
1622       /* offset past buffer, skip */
1623       offset -= info.size;
1624     }
1625     gst_memory_unmap (mem, &info);
1626   }
1627   return size - left;
1628 }
1629
1630 /**
1631  * gst_buffer_memcmp:
1632  * @buffer: a #GstBuffer.
1633  * @offset: the offset in @buffer
1634  * @mem: (array length=size) (element-type guint8): the memory to compare
1635  * @size: the size to compare
1636  *
1637  * Compare @size bytes starting from @offset in @buffer with the memory in @mem.
1638  *
1639  * Returns: 0 if the memory is equal.
1640  */
1641 gint
1642 gst_buffer_memcmp (GstBuffer * buffer, gsize offset, gconstpointer mem,
1643     gsize size)
1644 {
1645   gsize i, len;
1646   const guint8 *ptr = mem;
1647   gint res = 0;
1648
1649   g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1650   g_return_val_if_fail (mem != NULL, 0);
1651
1652   GST_CAT_LOG (GST_CAT_BUFFER,
1653       "buffer %p, offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
1654       offset, size);
1655
1656   len = GST_BUFFER_MEM_LEN (buffer);
1657
1658   for (i = 0; i < len && size > 0 && res == 0; i++) {
1659     GstMapInfo info;
1660     gsize tocmp;
1661     GstMemory *mem;
1662
1663     mem = _get_mapped (buffer, i, &info, GST_MAP_READ);
1664     if (info.size > offset) {
1665       /* we have enough */
1666       tocmp = MIN (info.size - offset, size);
1667       res = memcmp (ptr, (guint8 *) info.data + offset, tocmp);
1668       size -= tocmp;
1669       ptr += tocmp;
1670       offset = 0;
1671     } else {
1672       /* offset past buffer, skip */
1673       offset -= info.size;
1674     }
1675     gst_memory_unmap (mem, &info);
1676   }
1677   return res;
1678 }
1679
1680 /**
1681  * gst_buffer_memset:
1682  * @buffer: a #GstBuffer.
1683  * @offset: the offset in @buffer
1684  * @val: the value to set
1685  * @size: the size to set
1686  *
1687  * Fill @buf with @size bytes with @val starting from @offset.
1688  *
1689  * Returns: The amount of bytes filled. This value can be lower than @size
1690  *    when @buffer did not contain enough data.
1691  */
1692 gsize
1693 gst_buffer_memset (GstBuffer * buffer, gsize offset, guint8 val, gsize size)
1694 {
1695   gsize i, len, left;
1696
1697   g_return_val_if_fail (GST_IS_BUFFER (buffer), 0);
1698   g_return_val_if_fail (gst_buffer_is_writable (buffer), 0);
1699
1700   GST_CAT_LOG (GST_CAT_BUFFER,
1701       "buffer %p, offset %" G_GSIZE_FORMAT ", val %02x, size %" G_GSIZE_FORMAT,
1702       buffer, offset, val, size);
1703
1704   len = GST_BUFFER_MEM_LEN (buffer);
1705   left = size;
1706
1707   for (i = 0; i < len && left > 0; i++) {
1708     GstMapInfo info;
1709     gsize toset;
1710     GstMemory *mem;
1711
1712     mem = _get_mapped (buffer, i, &info, GST_MAP_WRITE);
1713     if (info.size > offset) {
1714       /* we have enough */
1715       toset = MIN (info.size - offset, left);
1716       memset ((guint8 *) info.data + offset, val, toset);
1717       left -= toset;
1718       offset = 0;
1719     } else {
1720       /* offset past buffer, skip */
1721       offset -= info.size;
1722     }
1723     gst_memory_unmap (mem, &info);
1724   }
1725   return size - left;
1726 }
1727
1728 /**
1729  * gst_buffer_copy_region:
1730  * @parent: a #GstBuffer.
1731  * @flags: the #GstBufferCopyFlags
1732  * @offset: the offset into parent #GstBuffer at which the new sub-buffer 
1733  *          begins.
1734  * @size: the size of the new #GstBuffer sub-buffer, in bytes.
1735  *
1736  * Creates a sub-buffer from @parent at @offset and @size.
1737  * This sub-buffer uses the actual memory space of the parent buffer.
1738  * This function will copy the offset and timestamp fields when the
1739  * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and 
1740  * #GST_BUFFER_OFFSET_NONE.
1741  * If @offset equals 0 and @size equals the total size of @buffer, the
1742  * duration and offset end fields are also copied. If not they will be set
1743  * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
1744  *
1745  * MT safe.
1746  *
1747  * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
1748  *     invalid.
1749  */
1750 GstBuffer *
1751 gst_buffer_copy_region (GstBuffer * buffer, GstBufferCopyFlags flags,
1752     gsize offset, gsize size)
1753 {
1754   GstBuffer *copy;
1755
1756   g_return_val_if_fail (buffer != NULL, NULL);
1757
1758   /* create the new buffer */
1759   copy = gst_buffer_new ();
1760
1761   GST_CAT_LOG (GST_CAT_BUFFER, "new region copy %p of %p %" G_GSIZE_FORMAT
1762       "-%" G_GSIZE_FORMAT, copy, buffer, offset, size);
1763
1764   if (!gst_buffer_copy_into (copy, buffer, flags, offset, size))
1765     gst_buffer_replace (&copy, NULL);
1766
1767   return copy;
1768 }
1769
1770 /**
1771  * gst_buffer_append:
1772  * @buf1: (transfer full): the first source #GstBuffer to append.
1773  * @buf2: (transfer full): the second source #GstBuffer to append.
1774  *
1775  * Append all the memory from @buf2 to @buf1. The result buffer will contain a
1776  * concatenation of the memory of @buf1 and @buf2.
1777  *
1778  * Returns: (transfer full): the new #GstBuffer that contains the memory
1779  *     of the two source buffers.
1780  */
1781 GstBuffer *
1782 gst_buffer_append (GstBuffer * buf1, GstBuffer * buf2)
1783 {
1784   return gst_buffer_append_region (buf1, buf2, 0, -1);
1785 }
1786
1787 /**
1788  * gst_buffer_append_region:
1789  * @buf1: (transfer full): the first source #GstBuffer to append.
1790  * @buf2: (transfer full): the second source #GstBuffer to append.
1791  * @offset: the offset in @buf2
1792  * @size: the size or -1 of @buf2
1793  *
1794  * Append @size bytes at @offset from @buf2 to @buf1. The result buffer will
1795  * contain a concatenation of the memory of @buf1 and the requested region of
1796  * @buf2.
1797  *
1798  * Returns: (transfer full): the new #GstBuffer that contains the memory
1799  *     of the two source buffers.
1800  */
1801 GstBuffer *
1802 gst_buffer_append_region (GstBuffer * buf1, GstBuffer * buf2, gssize offset,
1803     gssize size)
1804 {
1805   gsize i, len;
1806
1807   g_return_val_if_fail (GST_IS_BUFFER (buf1), NULL);
1808   g_return_val_if_fail (GST_IS_BUFFER (buf2), NULL);
1809
1810   buf1 = gst_buffer_make_writable (buf1);
1811   buf2 = gst_buffer_make_writable (buf2);
1812
1813   gst_buffer_resize (buf2, offset, size);
1814
1815   len = GST_BUFFER_MEM_LEN (buf2);
1816   for (i = 0; i < len; i++) {
1817     GstMemory *mem;
1818
1819     mem = GST_BUFFER_MEM_PTR (buf2, i);
1820     GST_BUFFER_MEM_PTR (buf2, i) = NULL;
1821     _memory_add (buf1, -1, mem, FALSE);
1822   }
1823
1824   GST_BUFFER_MEM_LEN (buf2) = 0;
1825   gst_buffer_unref (buf2);
1826
1827   return buf1;
1828 }
1829
1830 /**
1831  * gst_buffer_get_meta:
1832  * @buffer: a #GstBuffer
1833  * @api: the #GType of an API
1834  *
1835  * Get the metadata for @api on buffer. When there is no such
1836  * metadata, NULL is returned.
1837  *
1838  * Returns: (transfer none): the metadata for @api on @buffer.
1839  */
1840 GstMeta *
1841 gst_buffer_get_meta (GstBuffer * buffer, GType api)
1842 {
1843   GstMetaItem *item;
1844   GstMeta *result = NULL;
1845
1846   g_return_val_if_fail (buffer != NULL, NULL);
1847   g_return_val_if_fail (api != 0, NULL);
1848
1849   /* find GstMeta of the requested API */
1850   for (item = GST_BUFFER_META (buffer); item; item = item->next) {
1851     GstMeta *meta = &item->meta;
1852     if (meta->info->api == api) {
1853       result = meta;
1854       break;
1855     }
1856   }
1857   return result;
1858 }
1859
1860 /**
1861  * gst_buffer_add_meta:
1862  * @buffer: a #GstBuffer
1863  * @info: a #GstMetaInfo
1864  * @params: params for @info
1865  *
1866  * Add metadata for @info to @buffer using the parameters in @params.
1867  *
1868  * Returns: (transfer none): the metadata for the api in @info on @buffer.
1869  */
1870 GstMeta *
1871 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
1872     gpointer params)
1873 {
1874   GstMetaItem *item;
1875   GstMeta *result = NULL;
1876   gsize size;
1877
1878   g_return_val_if_fail (buffer != NULL, NULL);
1879   g_return_val_if_fail (info != NULL, NULL);
1880   g_return_val_if_fail (gst_buffer_is_writable (buffer), NULL);
1881
1882   /* create a new slice */
1883   size = ITEM_SIZE (info);
1884   item = g_slice_alloc (size);
1885   result = &item->meta;
1886   result->info = info;
1887   result->flags = GST_META_FLAG_NONE;
1888
1889   GST_CAT_DEBUG (GST_CAT_BUFFER,
1890       "alloc metadata %p (%s) of size %" G_GSIZE_FORMAT, result,
1891       g_type_name (info->type), info->size);
1892
1893   /* call the init_func when needed */
1894   if (info->init_func)
1895     if (!info->init_func (result, params, buffer))
1896       goto init_failed;
1897
1898   /* and add to the list of metadata */
1899   item->next = GST_BUFFER_META (buffer);
1900   GST_BUFFER_META (buffer) = item;
1901
1902   return result;
1903
1904 init_failed:
1905   {
1906     g_slice_free1 (size, item);
1907     return NULL;
1908   }
1909 }
1910
1911 /**
1912  * gst_buffer_remove_meta:
1913  * @buffer: a #GstBuffer
1914  * @meta: a #GstMeta
1915  *
1916  * Remove the metadata for @meta on @buffer.
1917  *
1918  * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
1919  * metadata was on @buffer.
1920  */
1921 gboolean
1922 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
1923 {
1924   GstMetaItem *walk, *prev;
1925
1926   g_return_val_if_fail (buffer != NULL, FALSE);
1927   g_return_val_if_fail (meta != NULL, FALSE);
1928   g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
1929   g_return_val_if_fail (!GST_META_FLAG_IS_SET (meta, GST_META_FLAG_LOCKED),
1930       FALSE);
1931
1932   /* find the metadata and delete */
1933   prev = GST_BUFFER_META (buffer);
1934   for (walk = prev; walk; walk = walk->next) {
1935     GstMeta *m = &walk->meta;
1936     if (m == meta) {
1937       const GstMetaInfo *info = meta->info;
1938
1939       /* remove from list */
1940       if (GST_BUFFER_META (buffer) == walk)
1941         GST_BUFFER_META (buffer) = walk->next;
1942       else
1943         prev->next = walk->next;
1944       /* call free_func if any */
1945       if (info->free_func)
1946         info->free_func (m, buffer);
1947
1948       /* and free the slice */
1949       g_slice_free1 (ITEM_SIZE (info), walk);
1950       break;
1951     }
1952     prev = walk;
1953   }
1954   return walk != NULL;
1955 }
1956
1957 /**
1958  * gst_buffer_iterate_meta:
1959  * @buffer: a #GstBuffer
1960  * @state: an opaque state pointer
1961  *
1962  * Retrieve the next #GstMeta after @current. If @state points
1963  * to %NULL, the first metadata is returned.
1964  *
1965  * @state will be updated with an opage state pointer 
1966  *
1967  * Returns: (transfer none): The next #GstMeta or %NULL when there are
1968  * no more items.
1969  */
1970 GstMeta *
1971 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
1972 {
1973   GstMetaItem **meta;
1974
1975   g_return_val_if_fail (buffer != NULL, NULL);
1976   g_return_val_if_fail (state != NULL, NULL);
1977
1978   meta = (GstMetaItem **) state;
1979   if (*meta == NULL)
1980     /* state NULL, move to first item */
1981     *meta = GST_BUFFER_META (buffer);
1982   else
1983     /* state !NULL, move to next item in list */
1984     *meta = (*meta)->next;
1985
1986   if (*meta)
1987     return &(*meta)->meta;
1988   else
1989     return NULL;
1990 }
1991
1992 /**
1993  * gst_buffer_foreach_meta:
1994  * @buffer: a #GstBuffer
1995  * @func: (scope call): a #GstBufferForeachMetaFunc to call
1996  * @user_data: (closure): user data passed to @func
1997  *
1998  * Call @func with @user_data for each meta in @buffer.
1999  *
2000  * @func can modify the passed meta pointer or its contents. The return value
2001  * of @func define if this function returns or if the remaining metadata items
2002  * in the buffer should be skipped.
2003  *
2004  * Returns: %FALSE when @func returned %FALSE for one of the metadata.
2005  */
2006 gboolean
2007 gst_buffer_foreach_meta (GstBuffer * buffer, GstBufferForeachMetaFunc func,
2008     gpointer user_data)
2009 {
2010   GstMetaItem *walk, *prev, *next;
2011   gboolean res = TRUE;
2012
2013   g_return_val_if_fail (buffer != NULL, FALSE);
2014   g_return_val_if_fail (func != NULL, FALSE);
2015
2016   /* find the metadata and delete */
2017   prev = GST_BUFFER_META (buffer);
2018   for (walk = prev; walk; walk = next) {
2019     GstMeta *m, *new;
2020
2021     m = new = &walk->meta;
2022     next = walk->next;
2023
2024     res = func (buffer, &new, user_data);
2025
2026     if (new == NULL) {
2027       const GstMetaInfo *info = m->info;
2028
2029       GST_CAT_DEBUG (GST_CAT_BUFFER, "remove metadata %p (%s)", m,
2030           g_type_name (info->type));
2031
2032       g_return_val_if_fail (gst_buffer_is_writable (buffer), FALSE);
2033       g_return_val_if_fail (!GST_META_FLAG_IS_SET (m, GST_META_FLAG_LOCKED),
2034           FALSE);
2035
2036       /* remove from list */
2037       if (GST_BUFFER_META (buffer) == walk)
2038         GST_BUFFER_META (buffer) = next;
2039       else
2040         prev->next = next;
2041
2042       /* call free_func if any */
2043       if (info->free_func)
2044         info->free_func (m, buffer);
2045
2046       /* and free the slice */
2047       g_slice_free1 (ITEM_SIZE (info), walk);
2048     }
2049     if (!res)
2050       break;
2051   }
2052   return res;
2053 }
2054
2055 /**
2056  * gst_buffer_extract_dup:
2057  * @buffer: a #GstBuffer
2058  * @offset: the offset to extract
2059  * @size: the size to extract
2060  * @dest: (array length=dest_size) (element-type guint8) (out): A pointer where
2061  *  the destination array will be written.
2062  * @dest_size: (out): A location where the size of @dest can be written
2063  *
2064  * Extracts a copy of at most @size bytes the data at @offset into a #GBytes.
2065  * @dest must be freed using g_free() when done.
2066  *
2067  * Since: 3.2
2068  */
2069
2070 void
2071 gst_buffer_extract_dup (GstBuffer * buffer, gsize offset, gsize size,
2072     gpointer * dest, gsize * dest_size)
2073 {
2074   gsize real_size;
2075
2076   real_size = gst_buffer_get_size (buffer);
2077
2078   *dest = g_malloc (MIN (real_size - offset, size));
2079
2080   *dest_size = gst_buffer_extract (buffer, offset, *dest, size);
2081 }