memory: more fixes
[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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:gstbuffer
25  * @short_description: Data-passing buffer type, supporting sub-buffers.
26  * @see_also: #GstPad, #GstMiniObject
27  *
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.
33  *
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.
38  * <example>
39  * <title>Creating a buffer for a video frame</title>
40  *   <programlisting>
41  *   GstBuffer *buffer;
42  *   gint size, width, height, bpp;
43  *   ...
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);
49  *   ...
50  *   </programlisting>
51  * </example>
52  *
53  * Alternatively, use gst_buffer_new_and_alloc()
54  * to create a buffer with preallocated data of a given size.
55  *
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.
59  *
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.
63  *
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.
68  *
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).
73  *
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.
82  *
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
85  * next element.
86  *
87  * To efficiently create a smaller buffer out of an existing one, you can
88  * use gst_buffer_create_sub().
89  *
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.
93  *
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.
98  *
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.
102  *
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.
106  *
107  * An element should either unref the buffer or push it out on a src pad
108  * using gst_pad_push() (see #GstPad).
109  *
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
112  * also be freed.
113  *
114  * Last reviewed on August 11th, 2006 (0.10.10)
115  */
116 #include "gst_private.h"
117
118 #ifdef HAVE_UNISTD_H
119 #include <unistd.h>
120 #endif
121 #ifdef HAVE_STDLIB_H
122 #include <stdlib.h>
123 #endif
124
125 #include "gstbuffer.h"
126 #include "gstbufferpool.h"
127 #include "gstinfo.h"
128 #include "gstutils.h"
129 #include "gstminiobject.h"
130 #include "gstversion.h"
131
132 GType _gst_buffer_type = 0;
133
134 typedef struct _GstMetaItem GstMetaItem;
135
136 struct _GstMetaItem
137 {
138   GstMetaItem *next;
139   GstMeta meta;
140 };
141
142 #define ITEM_SIZE(info) ((info)->size + sizeof (GstMetaItem))
143
144 /* buffer alignment in bytes
145  * an alignment of 8 would be the same as malloc() guarantees
146  */
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;
154 #else
155 #error "No buffer alignment configured"
156 #endif
157 #endif /* HAVE_POSIX_MEMALIGN */
158
159 void
160 _gst_buffer_initialize (void)
161 {
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 ();
167 #endif
168 #endif
169   }
170 }
171
172 /**
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
179  *
180  * Copies the information from @src into @dest.
181  *
182  * @flags indicate which fields will be copied.
183  */
184 void
185 gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
186     GstBufferCopyFlags flags, gsize offset, gsize trim)
187 {
188   GstMetaItem *walk;
189
190   g_return_if_fail (dest != NULL);
191   g_return_if_fail (src != NULL);
192
193   /* nothing to copy if the buffers are the same */
194   if (G_UNLIKELY (dest == src))
195     return;
196
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));
200 #endif
201
202   GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", src, dest);
203
204   if (flags & GST_BUFFER_COPY_FLAGS) {
205     guint mask;
206
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;
213   }
214
215   if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
216     if (offset == 0) {
217       GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
218       GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
219       if (trim == 0) {
220         GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
221         GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
222       }
223     } else {
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;
228     }
229   }
230
231   if (flags & GST_BUFFER_COPY_CAPS) {
232     gst_caps_replace (&GST_BUFFER_CAPS (dest), GST_BUFFER_CAPS (src));
233   }
234
235   if (flags & GST_BUFFER_COPY_MEMORY) {
236     GPtrArray *sarr = (GPtrArray *) src->memory;
237     GPtrArray *darr = (GPtrArray *) dest->memory;
238     guint i, len;
239
240     len = sarr->len;
241
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));
246       }
247     } else {
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));
251       }
252     }
253   }
254
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 };
259
260     if (info->transform_func)
261       info->transform_func (dest, meta, (GstBuffer *) src, &data);
262   }
263 }
264
265 static GstBuffer *
266 _gst_buffer_copy (GstBuffer * buffer)
267 {
268   GstBuffer *copy;
269
270   g_return_val_if_fail (buffer != NULL, NULL);
271
272   /* create a fresh new buffer */
273   copy = gst_buffer_new ();
274
275   /* we simply copy everything from our parent */
276   gst_buffer_copy_into (copy, buffer, GST_BUFFER_COPY_ALL, 0, 0);
277
278   return copy;
279 }
280
281 /* the default dispose function revives the buffer and returns it to the
282  * pool when there is a pool */
283 static void
284 _gst_buffer_dispose (GstBuffer * buffer)
285 {
286   GstBufferPool *pool;
287
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);
294   }
295 }
296
297 static void
298 _gst_buffer_free (GstBuffer * buffer)
299 {
300   GstMetaItem *walk, *next;
301
302   g_return_if_fail (buffer != NULL);
303
304   GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
305
306   gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
307
308   /* free metadata */
309   for (walk = buffer->priv; walk; walk = next) {
310     GstMeta *meta = &walk->meta;
311     const GstMetaInfo *info = meta->info;
312
313     /* call free_func if any */
314     if (info->free_func)
315       info->free_func (meta, buffer);
316     /* and free the slice */
317     next = walk->next;
318     g_slice_free (GstMetaItem, walk);
319   }
320
321   /* free our data, unrefs the memory too */
322   g_ptr_array_free (buffer->memory, TRUE);
323
324   g_slice_free1 (GST_MINI_OBJECT_SIZE (buffer), buffer);
325 }
326
327 static void
328 gst_buffer_init (GstBuffer * buffer, gsize size)
329 {
330   gst_mini_object_init (GST_MINI_OBJECT_CAST (buffer), _gst_buffer_type, size);
331
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;
336
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;
341
342   /* FIXME, do more efficient with array in the buffer memory itself */
343   buffer->memory =
344       g_ptr_array_new_with_free_func ((GDestroyNotify) gst_memory_unref);
345 }
346
347 /**
348  * gst_buffer_new:
349  *
350  * Creates a newly allocated buffer without any data.
351  *
352  * MT safe.
353  *
354  * Returns: (transfer full): the new #GstBuffer.
355  */
356 GstBuffer *
357 gst_buffer_new (void)
358 {
359   GstBuffer *newbuf;
360
361   newbuf = g_slice_new0 (GstBuffer);
362   GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
363
364   gst_buffer_init (newbuf, sizeof (GstBuffer));
365
366   return newbuf;
367 }
368
369 /**
370  * gst_buffer_new_and_alloc:
371  * @size: the size in bytes of the new buffer's data.
372  *
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.
379  *
380  * Note that when @size == 0, the buffer data pointer will be NULL.
381  *
382  * MT safe.
383  *
384  * Returns: (transfer full): the new #GstBuffer.
385  */
386 GstBuffer *
387 gst_buffer_new_and_alloc (guint size)
388 {
389   GstBuffer *newbuf;
390
391   newbuf = gst_buffer_new ();
392
393   gst_buffer_take_memory (newbuf, gst_memory_new_alloc (size,
394           _gst_buffer_data_alignment));
395
396   GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
397
398   return newbuf;
399 }
400
401 /**
402  * gst_buffer_try_new_and_alloc:
403  * @size: the size in bytes of the new buffer's data.
404  *
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.
408  *
409  * Note that when @size == 0, the buffer data pointer will be NULL.
410  *
411  * MT safe.
412  *
413  * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
414  *     be allocated.
415  *
416  * Since: 0.10.13
417  */
418 GstBuffer *
419 gst_buffer_try_new_and_alloc (guint size)
420 {
421   GstBuffer *newbuf;
422   GstMemory *mem;
423
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);
427     return NULL;
428   }
429
430   newbuf = gst_buffer_new ();
431   gst_buffer_take_memory (newbuf, mem);
432
433   GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
434
435   return newbuf;
436 }
437
438 guint
439 gst_buffer_n_memory (GstBuffer * buffer)
440 {
441   GPtrArray *arr = (GPtrArray *) buffer->memory;
442
443   return arr->len;
444 }
445
446 void
447 gst_buffer_take_memory (GstBuffer * buffer, GstMemory * mem)
448 {
449   GPtrArray *arr = (GPtrArray *) buffer->memory;
450
451   g_ptr_array_add (arr, mem);
452 }
453
454 GstMemory *
455 gst_buffer_peek_memory (GstBuffer * buffer, guint idx)
456 {
457   GstMemory *mem;
458   GPtrArray *arr = (GPtrArray *) buffer->memory;
459
460   mem = g_ptr_array_index (arr, idx);
461
462   return mem;
463 }
464
465 void
466 gst_buffer_remove_memory (GstBuffer * buffer, guint idx)
467 {
468   GPtrArray *arr = (GPtrArray *) buffer->memory;
469
470   g_ptr_array_remove_index (arr, idx);
471 }
472
473 gsize
474 gst_buffer_get_size (GstBuffer * buffer)
475 {
476   GPtrArray *arr = (GPtrArray *) buffer->memory;
477   guint i, size, len;
478
479   len = arr->len;
480
481   size = 0;
482   for (i = 0; i < len; i++) {
483     size += gst_memory_get_sizes (g_ptr_array_index (arr, i), NULL);
484   }
485   return size;
486 }
487
488 /* getting memory */
489 gpointer
490 gst_buffer_map (GstBuffer * buffer, gsize * size, gsize * maxsize,
491     GstMapFlags flags)
492 {
493   GPtrArray *arr = (GPtrArray *) buffer->memory;
494   guint len;
495   gpointer data;
496
497   len = arr->len;
498
499   if (G_LIKELY (len == 1)) {
500     GstMemory *mem = g_ptr_array_index (arr, 0);
501
502     data = gst_memory_map (mem, size, maxsize, flags);
503   } else {
504     data = NULL;
505   }
506   return data;
507 }
508
509 gboolean
510 gst_buffer_unmap (GstBuffer * buffer, gpointer data, gsize size)
511 {
512   GPtrArray *arr = (GPtrArray *) buffer->memory;
513   gboolean result;
514   guint len;
515
516   len = arr->len;
517
518   if (G_LIKELY (len == 1)) {
519     GstMemory *mem = g_ptr_array_index (arr, 0);
520
521     result = gst_memory_unmap (mem, data, size);
522   } else {
523     result = FALSE;
524   }
525   return result;
526 }
527
528 /**
529  * gst_buffer_get_caps:
530  * @buffer: a #GstBuffer.
531  *
532  * Gets the media type of the buffer. This can be NULL if there
533  * is no media type attached to this buffer.
534  *
535  * Returns: (transfer full): a reference to the #GstCaps. unref after usage.
536  * Returns NULL if there were no caps on this buffer.
537  */
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.
540  */
541 GstCaps *
542 gst_buffer_get_caps (GstBuffer * buffer)
543 {
544   GstCaps *ret;
545
546   g_return_val_if_fail (buffer != NULL, NULL);
547
548   ret = GST_BUFFER_CAPS (buffer);
549
550   if (ret)
551     gst_caps_ref (ret);
552
553   return ret;
554 }
555
556 /**
557  * gst_buffer_set_caps:
558  * @buffer: a #GstBuffer.
559  * @caps: (transfer none): a #GstCaps.
560  *
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
563  * unreffed.
564  */
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.
567  */
568 void
569 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
570 {
571   g_return_if_fail (buffer != NULL);
572   g_return_if_fail (caps == NULL || GST_CAPS_IS_SIMPLE (caps));
573
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 */
578 #endif
579
580   gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
581 }
582
583 /**
584  * gst_buffer_is_metadata_writable:
585  * @buf: a #GstBuffer
586  *
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.
590  *
591  * Returns: TRUE if the metadata is writable.
592  */
593 gboolean
594 gst_buffer_is_metadata_writable (GstBuffer * buf)
595 {
596   return (GST_MINI_OBJECT_REFCOUNT_VALUE (GST_MINI_OBJECT_CAST (buf)) == 1);
597 }
598
599 /**
600  * gst_buffer_make_metadata_writable:
601  * @buf: (transfer full): a #GstBuffer
602  *
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.
607  * 
608  * After calling this function, @buf should not be referenced anymore. The
609  * result of this function has guaranteed writable metadata.
610  *
611  * Returns: (transfer full): a new #GstBuffer with writable metadata, which
612  *     may or may not be the same as @buf.
613  */
614 GstBuffer *
615 gst_buffer_make_metadata_writable (GstBuffer * buf)
616 {
617   GstBuffer *ret;
618
619   if (gst_buffer_is_metadata_writable (buf)) {
620     ret = buf;
621   } else {
622     /* create a fresh new buffer */
623     ret = gst_buffer_new ();
624
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);
628   }
629   return ret;
630 }
631
632 /**
633  * gst_buffer_create_sub:
634  * @parent: a #GstBuffer.
635  * @offset: the offset into parent #GstBuffer at which the new sub-buffer 
636  *          begins.
637  * @size: the size of the new #GstBuffer sub-buffer, in bytes.
638  *
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.
647  *
648  * MT safe.
649  *
650  * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
651  *     invalid.
652  */
653 GstBuffer *
654 gst_buffer_create_sub (GstBuffer * buffer, gsize offset, gsize size)
655 {
656   GstBuffer *subbuffer;
657   gsize bufsize;
658
659   g_return_val_if_fail (buffer != NULL, NULL);
660   g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL);
661
662   bufsize = gst_buffer_get_size (buffer);
663   g_return_val_if_fail (bufsize >= offset + size, NULL);
664
665   /* create the new buffer */
666   subbuffer = gst_buffer_new ();
667
668   GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p of %p", subbuffer, buffer);
669
670   gst_buffer_copy_into (subbuffer, buffer, GST_BUFFER_SHARE_ALL, offset,
671       bufsize - (size + offset));
672
673   return subbuffer;
674 }
675
676 /**
677  * gst_buffer_is_span_fast:
678  * @buf1: the first #GstBuffer.
679  * @buf2: the second #GstBuffer.
680  *
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
683  * the same buffer.
684  *
685  * MT safe.
686  * Returns: TRUE if the buffers are contiguous,
687  * FALSE if a copy would be required.
688  */
689 gboolean
690 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
691 {
692   GPtrArray *arr1, *arr2;
693
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);
697
698   arr1 = (GPtrArray *) buf1->memory;
699   arr2 = (GPtrArray *) buf2->memory;
700
701   return gst_memory_is_span ((GstMemory **) arr1->pdata, arr1->len,
702       (GstMemory **) arr2->pdata, arr2->len, NULL, NULL);
703 }
704
705 /**
706  * gst_buffer_span:
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.
712  *
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.
717  *
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.
722  *
723  * MT safe.
724  *
725  * Returns: (transfer full): the new #GstBuffer that spans the two source
726  *     buffers, or NULL if the arguments are invalid.
727  */
728 GstBuffer *
729 gst_buffer_span (GstBuffer * buf1, gsize offset, GstBuffer * buf2, gsize len)
730 {
731   GstBuffer *newbuf;
732   GPtrArray *arr1, *arr2;
733   GstMemory *mem;
734
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);
739
740   newbuf = gst_buffer_new ();
741
742   arr1 = (GPtrArray *) buf1->memory;
743   arr2 = (GPtrArray *) buf2->memory;
744
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);
748
749 #if 0
750   /* if the offset is 0, the new buffer has the same timestamp as buf1 */
751   if (offset == 0) {
752     GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
753     GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
754
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)) {
761         /* add duration */
762         GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
763             GST_BUFFER_DURATION (buf2);
764       }
765       if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
766         /* add offset_end */
767         GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
768       }
769     }
770   }
771 #endif
772
773   return newbuf;
774 }
775
776 /**
777  * gst_buffer_get_meta:
778  * @buffer: a #GstBuffer
779  * @info: a #GstMetaInfo
780  *
781  * Get the metadata for the api in @info on buffer. When there is no such
782  * metadata, NULL is returned.
783  *
784  * Note that the result metadata might not be of the implementation @info.
785  *
786  * Returns: the metadata for the api in @info on @buffer.
787  */
788 GstMeta *
789 gst_buffer_get_meta (GstBuffer * buffer, const GstMetaInfo * info)
790 {
791   GstMetaItem *item;
792   GstMeta *result = NULL;
793
794   g_return_val_if_fail (buffer != NULL, NULL);
795   g_return_val_if_fail (info != NULL, NULL);
796
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) {
801       result = meta;
802       break;
803     }
804   }
805   return result;
806 }
807
808 /**
809  * gst_buffer_add_meta:
810  * @buffer: a #GstBuffer
811  * @info: a #GstMetaInfo
812  * @params: params for @info
813  *
814  * Add metadata for @info to @buffer using the parameters in @params.
815  *
816  * Returns: the metadata for the api in @info on @buffer.
817  */
818 GstMeta *
819 gst_buffer_add_meta (GstBuffer * buffer, const GstMetaInfo * info,
820     gpointer params)
821 {
822   GstMetaItem *item;
823   GstMeta *result = NULL;
824   gsize size;
825
826   g_return_val_if_fail (buffer != NULL, NULL);
827   g_return_val_if_fail (info != NULL, NULL);
828
829   /* create a new slice */
830   GST_CAT_DEBUG (GST_CAT_BUFFER, "alloc metadata of size %" G_GSIZE_FORMAT,
831       info->size);
832
833   size = ITEM_SIZE (info);
834   item = g_slice_alloc (size);
835   result = &item->meta;
836   result->info = info;
837
838   /* call the init_func when needed */
839   if (info->init_func)
840     if (!info->init_func (result, params, buffer))
841       goto init_failed;
842
843   /* and add to the list of metadata */
844   item->next = buffer->priv;
845   buffer->priv = item;
846
847   return result;
848
849 init_failed:
850   {
851     g_slice_free1 (size, item);
852     return NULL;
853   }
854 }
855
856 /**
857  * gst_buffer_remove_meta:
858  * @buffer: a #GstBuffer
859  * @info: a #GstMetaInfo
860  *
861  * Remove the metadata for @info on @buffer.
862  *
863  * Returns: %TRUE if the metadata existed and was removed, %FALSE if no such
864  * metadata was on @buffer.
865  */
866 gboolean
867 gst_buffer_remove_meta (GstBuffer * buffer, GstMeta * meta)
868 {
869   GstMetaItem *walk, *prev;
870
871   g_return_val_if_fail (buffer != NULL, FALSE);
872   g_return_val_if_fail (meta != NULL, FALSE);
873
874   /* find the metadata and delete */
875   prev = buffer->priv;
876   for (walk = prev; walk; walk = walk->next) {
877     GstMeta *m = &walk->meta;
878     if (m == meta) {
879       const GstMetaInfo *info = meta->info;
880
881       /* remove from list */
882       if (buffer->priv == walk)
883         buffer->priv = walk->next;
884       else
885         prev->next = walk->next;
886       /* call free_func if any */
887       if (info->free_func)
888         info->free_func (m, buffer);
889
890       /* and free the slice */
891       g_slice_free1 (ITEM_SIZE (info), walk);
892       break;
893     }
894     prev = walk;
895   }
896   return walk != NULL;
897 }
898
899 /**
900  * gst_buffer_iterate_meta:
901  * @buffer: a #GstBuffer
902  * @state: an opaque state pointer
903  *
904  * Retrieve the next #GstMeta after @current. If @state points
905  * to %NULL, the first metadata is returned.
906  *
907  * @state will be updated with an opage state pointer 
908  *
909  * Returns: The next #GstMeta or %NULL when there are no more items.
910  */
911 GstMeta *
912 gst_buffer_iterate_meta (GstBuffer * buffer, gpointer * state)
913 {
914   GstMetaItem **meta;
915
916   g_return_val_if_fail (buffer != NULL, NULL);
917   g_return_val_if_fail (state != NULL, NULL);
918
919   meta = (GstMetaItem **) state;
920   if (*meta == NULL)
921     /* state NULL, move to first item */
922     *meta = buffer->priv;
923   else
924     /* state !NULL, move to next item in list */
925     *meta = (*meta)->next;
926
927   if (*meta)
928     return &(*meta)->meta;
929   else
930     return NULL;
931 }