buffer: allow configurable memory alignment. Fixes #596832
[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 "gstinfo.h"
127 #include "gstutils.h"
128 #include "gstminiobject.h"
129 #include "gstversion.h"
130
131 static void gst_buffer_finalize (GstBuffer * buffer);
132 static GstBuffer *_gst_buffer_copy (GstBuffer * buffer);
133
134 static GType _gst_buffer_type = 0;
135
136 /* buffer alignment in bytes
137  * an alignment of 8 would be the same as malloc() guarantees
138  */
139 #ifdef HAVE_POSIX_MEMALIGN
140 #if defined(BUFFER_ALIGNMENT_MALLOC)
141 static size_t _gst_buffer_data_alignment = 8;
142 #elif defined(BUFFER_ALIGNMENT_PAGESIZE)
143 static size_t _gst_buffer_data_alignment = 0;
144 #elif defined(BUFFER_ALIGNMENT)
145 static size_t _gst_buffer_data_alignment = BUFFER_ALIGNMENT;
146 #else
147 #error "No buffer alignment configured"
148 #endif
149
150 static inline gboolean
151 aligned_malloc (gpointer * memptr, guint size)
152 {
153   gint res;
154
155   res = posix_memalign (memptr, _gst_buffer_data_alignment, size);
156   return (res == 0);
157 }
158
159 #endif /* HAVE_POSIX_MEMALIGN */
160
161 void
162 _gst_buffer_initialize (void)
163 {
164   /* the GstMiniObject types need to be class_ref'd once before it can be
165    * done from multiple threads;
166    * see http://bugzilla.gnome.org/show_bug.cgi?id=304551 */
167   g_type_class_ref (gst_buffer_get_type ());
168 #ifdef HAVE_GETPAGESIZE
169 #ifdef BUFFER_ALIGNMENT_PAGESIZE
170   _gst_buffer_data_alignment = getpagesize ();
171 #endif
172 #endif
173 }
174
175 #define _do_init \
176 { \
177   _gst_buffer_type = g_define_type_id; \
178 }
179
180 G_DEFINE_TYPE_WITH_CODE (GstBuffer, gst_buffer, GST_TYPE_MINI_OBJECT, _do_init);
181
182 static void
183 gst_buffer_class_init (GstBufferClass * klass)
184 {
185   klass->mini_object_class.copy = (GstMiniObjectCopyFunction) _gst_buffer_copy;
186   klass->mini_object_class.finalize =
187       (GstMiniObjectFinalizeFunction) gst_buffer_finalize;
188 }
189
190 static void
191 gst_buffer_finalize (GstBuffer * buffer)
192 {
193   g_return_if_fail (buffer != NULL);
194
195   GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
196
197   /* free our data */
198   if (G_LIKELY (buffer->malloc_data))
199     buffer->free_func (buffer->malloc_data);
200
201   gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
202
203   if (buffer->parent)
204     gst_buffer_unref (buffer->parent);
205
206 /*   ((GstMiniObjectClass *) */
207 /*       gst_buffer_parent_class)->finalize (GST_MINI_OBJECT_CAST (buffer)); */
208 }
209
210 /**
211  * gst_buffer_copy_metadata:
212  * @dest: a destination #GstBuffer
213  * @src: a source #GstBuffer
214  * @flags: flags indicating what metadata fields should be copied.
215  *
216  * Copies the metadata from @src into @dest. The data, size and mallocdata
217  * fields are not copied.
218  *
219  * @flags indicate which fields will be copied. Use #GST_BUFFER_COPY_ALL to copy
220  * all the metadata fields.
221  *
222  * This function is typically called from a custom buffer copy function after
223  * creating @dest and setting the data, size, mallocdata.
224  *
225  * Since: 0.10.13
226  */
227 void
228 gst_buffer_copy_metadata (GstBuffer * dest, const GstBuffer * src,
229     GstBufferCopyFlags flags)
230 {
231   g_return_if_fail (dest != NULL);
232   g_return_if_fail (src != NULL);
233
234   /* nothing to copy if the buffers are the same */
235   if (G_UNLIKELY (dest == src))
236     return;
237
238 #if GST_VERSION_NANO == 1
239   /* we enable this extra debugging in git versions only for now */
240   g_return_if_fail (gst_buffer_is_metadata_writable (dest));
241 #endif
242
243   GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", src, dest);
244
245   if (flags & GST_BUFFER_COPY_FLAGS) {
246     guint mask;
247
248     /* copy relevant flags */
249     mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
250         GST_BUFFER_FLAG_DELTA_UNIT | GST_BUFFER_FLAG_DISCONT |
251         GST_BUFFER_FLAG_GAP | GST_BUFFER_FLAG_MEDIA1 |
252         GST_BUFFER_FLAG_MEDIA2 | GST_BUFFER_FLAG_MEDIA3;
253     GST_MINI_OBJECT_FLAGS (dest) |= GST_MINI_OBJECT_FLAGS (src) & mask;
254   }
255
256   if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
257     GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
258     GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
259     GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
260     GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
261   }
262
263   if (flags & GST_BUFFER_COPY_CAPS) {
264     gst_caps_replace (&GST_BUFFER_CAPS (dest), GST_BUFFER_CAPS (src));
265   }
266 }
267
268 static GstBuffer *
269 _gst_buffer_copy (GstBuffer * buffer)
270 {
271   GstBuffer *copy;
272
273   g_return_val_if_fail (buffer != NULL, NULL);
274
275   /* create a fresh new buffer */
276   copy = gst_buffer_new ();
277
278   /* we simply copy everything from our parent */
279 #ifdef HAVE_POSIX_MEMALIGN
280   {
281     gpointer memptr = NULL;
282
283     if (G_LIKELY (buffer->size)) {
284       if (G_UNLIKELY (!aligned_malloc (&memptr, buffer->size))) {
285         /* terminate on error like g_memdup() would */
286         g_error ("%s: failed to allocate %" G_GSIZE_FORMAT " bytes",
287             G_STRLOC, buffer->size);
288       } else {
289         memcpy (memptr, buffer->data, buffer->size);
290       }
291     }
292     copy->data = (guint8 *) memptr;
293     GST_BUFFER_FREE_FUNC (copy) = free;
294   }
295 #else
296   copy->data = g_memdup (buffer->data, buffer->size);
297 #endif
298
299   /* make sure it gets freed (even if the parent is subclassed, we return a
300      normal buffer) */
301   copy->malloc_data = copy->data;
302   copy->size = buffer->size;
303
304   gst_buffer_copy_metadata (copy, buffer, GST_BUFFER_COPY_ALL);
305
306   return copy;
307 }
308
309 static void
310 gst_buffer_init (GstBuffer * buffer)
311 {
312   GST_CAT_LOG (GST_CAT_BUFFER, "init %p", buffer);
313
314   GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
315   GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
316   GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
317   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
318   GST_BUFFER_FREE_FUNC (buffer) = g_free;
319 }
320
321 /**
322  * gst_buffer_new:
323  *
324  * Creates a newly allocated buffer without any data.
325  *
326  * MT safe.
327  * Returns: the new #GstBuffer.
328  */
329 GstBuffer *
330 gst_buffer_new (void)
331 {
332   GstBuffer *newbuf;
333
334   newbuf = (GstBuffer *) gst_mini_object_new (_gst_buffer_type);
335
336   GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
337
338   return newbuf;
339 }
340
341 /**
342  * gst_buffer_new_and_alloc:
343  * @size: the size of the new buffer's data.
344  *
345  * Creates a newly allocated buffer with data of the given size.
346  * The buffer memory is not cleared. If the requested amount of
347  * memory can't be allocated, the program will abort. Use
348  * gst_buffer_try_new_and_alloc() if you want to handle this case
349  * gracefully or have gotten the size to allocate from an untrusted
350  * source such as a media stream.
351  * 
352  *
353  * Note that when @size == 0, the buffer data pointer will be NULL.
354  *
355  * MT safe.
356  * Returns: the new #GstBuffer.
357  */
358 GstBuffer *
359 gst_buffer_new_and_alloc (guint size)
360 {
361   GstBuffer *newbuf;
362
363   newbuf = gst_buffer_new ();
364
365 #ifdef HAVE_POSIX_MEMALIGN
366   {
367     gpointer memptr = NULL;
368
369     if (G_LIKELY (size)) {
370       if (G_UNLIKELY (!aligned_malloc (&memptr, size))) {
371         /* terminate on error like g_memdup() would */
372         g_error ("%s: failed to allocate %" G_GSIZE_FORMAT " bytes",
373             G_STRLOC, size);
374       }
375     }
376     newbuf->malloc_data = (guint8 *) memptr;
377     GST_BUFFER_FREE_FUNC (newbuf) = free;
378   }
379 #else
380   newbuf->malloc_data = g_malloc (size);
381 #endif
382   GST_BUFFER_DATA (newbuf) = newbuf->malloc_data;
383   GST_BUFFER_SIZE (newbuf) = size;
384
385   GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
386
387   return newbuf;
388 }
389
390 /**
391  * gst_buffer_try_new_and_alloc:
392  * @size: the size of the new buffer's data.
393  *
394  * Tries to create a newly allocated buffer with data of the given size. If
395  * the requested amount of memory can't be allocated, NULL will be returned.
396  * The buffer memory is not cleared.
397  *
398  * Note that when @size == 0, the buffer data pointer will be NULL.
399  *
400  * MT safe.
401  *
402  * Returns: a new #GstBuffer, or NULL if the memory couldn't be allocated.
403  *
404  * Since: 0.10.13
405  */
406 GstBuffer *
407 gst_buffer_try_new_and_alloc (guint size)
408 {
409   GstBuffer *newbuf;
410   guint8 *malloc_data;
411 #ifdef HAVE_POSIX_MEMALIGN
412   gpointer memptr = NULL;
413
414   if (G_LIKELY (size)) {
415     if (G_UNLIKELY (!aligned_malloc (&memptr, size))) {
416       GST_CAT_WARNING (GST_CAT_BUFFER, "failed to allocate %d bytes", size);
417       return NULL;
418     }
419   }
420   malloc_data = (guint8 *) memptr;
421 #else
422   malloc_data = g_try_malloc (size);
423
424   if (G_UNLIKELY (malloc_data == NULL && size != 0)) {
425     GST_CAT_WARNING (GST_CAT_BUFFER, "failed to allocate %d bytes", size);
426     return NULL;
427   }
428 #endif
429
430   /* FIXME: there's no g_type_try_create_instance() in GObject yet, so this
431    * will still abort if a new GstBuffer structure can't be allocated */
432   newbuf = gst_buffer_new ();
433
434   GST_BUFFER_MALLOCDATA (newbuf) = malloc_data;
435   GST_BUFFER_DATA (newbuf) = malloc_data;
436   GST_BUFFER_SIZE (newbuf) = size;
437 #ifdef HAVE_POSIX_MEMALIGN
438   GST_BUFFER_FREE_FUNC (newbuf) = free;
439 #endif
440
441   GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
442
443   return newbuf;
444 }
445
446 /**
447  * gst_buffer_get_caps:
448  * @buffer: a #GstBuffer.
449  *
450  * Gets the media type of the buffer. This can be NULL if there
451  * is no media type attached to this buffer.
452  *
453  * Returns: a reference to the #GstCaps. unref after usage.
454  * Returns NULL if there were no caps on this buffer.
455  */
456 /* this is not made atomic because if the buffer were reffed from multiple
457  * threads, it would have a refcount > 2 and thus be immutable.
458  */
459 GstCaps *
460 gst_buffer_get_caps (GstBuffer * buffer)
461 {
462   GstCaps *ret;
463
464   g_return_val_if_fail (buffer != NULL, NULL);
465
466   ret = GST_BUFFER_CAPS (buffer);
467
468   if (ret)
469     gst_caps_ref (ret);
470
471   return ret;
472 }
473
474 /**
475  * gst_buffer_set_caps:
476  * @buffer: a #GstBuffer.
477  * @caps: a #GstCaps.
478  *
479  * Sets the media type on the buffer. The refcount of the caps will
480  * be increased and any previous caps on the buffer will be
481  * unreffed.
482  */
483 /* this is not made atomic because if the buffer were reffed from multiple
484  * threads, it would have a refcount > 2 and thus be immutable.
485  */
486 void
487 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
488 {
489   g_return_if_fail (buffer != NULL);
490 #if GST_VERSION_NANO == 1
491   /* we enable this extra debugging in git versions only for now */
492   g_return_if_fail (gst_buffer_is_metadata_writable (buffer));
493 #endif
494
495   gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
496 }
497
498 /**
499  * gst_buffer_is_metadata_writable:
500  * @buf: a #GstBuffer
501  *
502  * Similar to gst_buffer_is_writable, but this only ensures that the
503  * refcount of the buffer is 1, indicating that the caller is the sole
504  * owner and can change the buffer metadata, such as caps and timestamps.
505  *
506  * Returns: TRUE if the metadata is writable.
507  */
508 gboolean
509 gst_buffer_is_metadata_writable (GstBuffer * buf)
510 {
511   return (GST_MINI_OBJECT_REFCOUNT_VALUE (GST_MINI_OBJECT_CAST (buf)) == 1);
512 }
513
514 /**
515  * gst_buffer_make_metadata_writable:
516  * @buf: a #GstBuffer
517  *
518  * Similar to gst_buffer_make_writable, but does not ensure that the buffer
519  * data array is writable. Instead, this just ensures that the returned buffer
520  * is solely owned by the caller, by creating a subbuffer of the original
521  * buffer if necessary.
522  * 
523  * After calling this function, @buf should not be referenced anymore. The
524  * result of this function has guaranteed writable metadata.
525  *
526  * Returns: A new #GstBuffer with writable metadata.
527  */
528 GstBuffer *
529 gst_buffer_make_metadata_writable (GstBuffer * buf)
530 {
531   GstBuffer *ret;
532
533   if (gst_buffer_is_metadata_writable (buf)) {
534     ret = buf;
535   } else {
536     ret = gst_buffer_create_sub (buf, 0, GST_BUFFER_SIZE (buf));
537
538     gst_buffer_unref (buf);
539   }
540
541   return ret;
542 }
543
544 #define GST_IS_SUBBUFFER(obj)   (GST_BUFFER_CAST(obj)->parent != NULL)
545
546 /**
547  * gst_buffer_create_sub:
548  * @parent: a #GstBuffer.
549  * @offset: the offset into parent #GstBuffer at which the new sub-buffer 
550  *          begins.
551  * @size: the size of the new #GstBuffer sub-buffer, in bytes.
552  *
553  * Creates a sub-buffer from @parent at @offset and @size.
554  * This sub-buffer uses the actual memory space of the parent buffer.
555  * This function will copy the offset and timestamp fields when the
556  * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and 
557  * #GST_BUFFER_OFFSET_NONE.
558  * If @offset equals 0 and @size equals the total size of @buffer, the
559  * duration and offset end fields are also copied. If not they will be set
560  * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
561  *
562  * MT safe.
563  * Returns: the new #GstBuffer.
564  * Returns NULL if the arguments were invalid.
565  */
566 GstBuffer *
567 gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size)
568 {
569   GstBuffer *subbuffer;
570   GstBuffer *parent;
571   gboolean complete;
572
573   g_return_val_if_fail (buffer != NULL, NULL);
574   g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL);
575   g_return_val_if_fail (buffer->size >= offset + size, NULL);
576
577   /* find real parent */
578   if (GST_IS_SUBBUFFER (buffer)) {
579     parent = buffer->parent;
580   } else {
581     parent = buffer;
582   }
583   gst_buffer_ref (parent);
584
585   /* create the new buffer */
586   subbuffer = gst_buffer_new ();
587   subbuffer->parent = parent;
588   GST_BUFFER_FLAG_SET (subbuffer, GST_BUFFER_FLAG_READONLY);
589
590   GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", subbuffer,
591       parent);
592
593   /* set the right values in the child */
594   GST_BUFFER_DATA (subbuffer) = buffer->data + offset;
595   GST_BUFFER_SIZE (subbuffer) = size;
596
597   if ((offset == 0) && (size == GST_BUFFER_SIZE (buffer))) {
598     /* copy all the flags except IN_CAPS */
599     GST_BUFFER_FLAG_SET (subbuffer, GST_BUFFER_FLAGS (buffer));
600     GST_BUFFER_FLAG_UNSET (subbuffer, GST_BUFFER_FLAG_IN_CAPS);
601   } else {
602     /* copy only PREROLL & GAP flags */
603     GST_BUFFER_FLAG_SET (subbuffer, (GST_BUFFER_FLAGS (buffer) &
604             (GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_GAP)));
605   }
606
607   /* we can copy the timestamp and offset if the new buffer starts at
608    * offset 0 */
609   if (offset == 0) {
610     GST_BUFFER_TIMESTAMP (subbuffer) = GST_BUFFER_TIMESTAMP (buffer);
611     GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET (buffer);
612     complete = (buffer->size == size);
613   } else {
614     GST_BUFFER_TIMESTAMP (subbuffer) = GST_CLOCK_TIME_NONE;
615     GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET_NONE;
616     complete = FALSE;
617   }
618
619   if (complete) {
620     GstCaps *caps;
621
622     /* if we copied the complete buffer we can copy the duration,
623      * offset_end and caps as well */
624     GST_BUFFER_DURATION (subbuffer) = GST_BUFFER_DURATION (buffer);
625     GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_END (buffer);
626     if ((caps = GST_BUFFER_CAPS (buffer)))
627       gst_caps_ref (caps);
628     GST_BUFFER_CAPS (subbuffer) = caps;
629   } else {
630     GST_BUFFER_DURATION (subbuffer) = GST_CLOCK_TIME_NONE;
631     GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_NONE;
632     GST_BUFFER_CAPS (subbuffer) = NULL;
633   }
634   return subbuffer;
635 }
636
637 /**
638  * gst_buffer_is_span_fast:
639  * @buf1: the first #GstBuffer.
640  * @buf2: the second #GstBuffer.
641  *
642  * Determines whether a gst_buffer_span() can be done without copying
643  * the contents, that is, whether the data areas are contiguous sub-buffers of 
644  * the same buffer.
645  *
646  * MT safe.
647  * Returns: TRUE if the buffers are contiguous,
648  * FALSE if a copy would be required.
649  */
650 gboolean
651 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
652 {
653   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
654   g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
655   g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
656
657   /* it's only fast if we have subbuffers of the same parent */
658   return (GST_IS_SUBBUFFER (buf1) &&
659       GST_IS_SUBBUFFER (buf2) && (buf1->parent == buf2->parent)
660       && ((buf1->data + buf1->size) == buf2->data));
661 }
662
663 /**
664  * gst_buffer_span:
665  * @buf1: the first source #GstBuffer to merge.
666  * @offset: the offset in the first buffer from where the new
667  * buffer should start.
668  * @buf2: the second source #GstBuffer to merge.
669  * @len: the total length of the new buffer.
670  *
671  * Creates a new buffer that consists of part of buf1 and buf2.
672  * Logically, buf1 and buf2 are concatenated into a single larger
673  * buffer, and a new buffer is created at the given offset inside
674  * this space, with a given length.
675  *
676  * If the two source buffers are children of the same larger buffer,
677  * and are contiguous, the new buffer will be a child of the shared
678  * parent, and thus no copying is necessary. you can use
679  * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
680  *
681  * MT safe.
682  * Returns: the new #GstBuffer that spans the two source buffers.
683  * Returns NULL if the arguments are invalid.
684  */
685 GstBuffer *
686 gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
687     guint32 len)
688 {
689   GstBuffer *newbuf;
690
691   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
692   g_return_val_if_fail (buf1->mini_object.refcount > 0, NULL);
693   g_return_val_if_fail (buf2->mini_object.refcount > 0, NULL);
694   g_return_val_if_fail (len > 0, NULL);
695   g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
696
697   /* if the two buffers have the same parent and are adjacent */
698   if (gst_buffer_is_span_fast (buf1, buf2)) {
699     GstBuffer *parent = buf1->parent;
700
701     /* we simply create a subbuffer of the common parent */
702     newbuf = gst_buffer_create_sub (parent,
703         buf1->data - parent->data + offset, len);
704   } else {
705     GST_CAT_DEBUG (GST_CAT_BUFFER,
706         "slow path taken while spanning buffers %p and %p", buf1, buf2);
707     /* otherwise we simply have to brute-force copy the buffers */
708     newbuf = gst_buffer_new_and_alloc (len);
709
710     /* copy the first buffer's data across */
711     memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
712     /* copy the second buffer's data across */
713     memcpy (newbuf->data + (buf1->size - offset), buf2->data,
714         len - (buf1->size - offset));
715   }
716   /* if the offset is 0, the new buffer has the same timestamp as buf1 */
717   if (offset == 0) {
718     GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
719     GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
720
721     /* if we completely merged the two buffers (appended), we can
722      * calculate the duration too. Also make sure we's not messing with
723      * invalid DURATIONS */
724     if (buf1->size + buf2->size == len) {
725       if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
726           GST_BUFFER_DURATION_IS_VALID (buf2)) {
727         /* add duration */
728         GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
729             GST_BUFFER_DURATION (buf2);
730       }
731       if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
732         /* add offset_end */
733         GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
734       }
735     }
736   }
737
738   return newbuf;
739 }