2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstbuffer.c: Buffer operations
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * @short_description: Data-passing buffer type, supporting sub-buffers.
25 * @see_also: #GstPad, #GstMiniObject
27 * Buffers are the basic unit of data transfer in GStreamer. The GstBuffer type
28 * provides all the state necessary to define a region of memory as part of a
29 * stream. Sub-buffers are also supported, allowing a smaller region of a
30 * buffer to become its own buffer, with mechanisms in place to ensure that
31 * neither memory space goes away.
33 * Buffers are usually created with gst_buffer_new(). After a buffer has been
34 * created one will typically allocate memory for it and set the size of the
35 * buffer data. The following example creates a buffer that can hold a given
36 * video frame with a given width, height and bits per plane.
38 * <title>Creating a buffer for a video frame</title>
41 * gint size, width, height, bpp;
45 * size = width * height * bpp;
47 * buffer = gst_buffer_new ();
48 * GST_BUFFER_SIZE (buffer) = size;
49 * GST_BUFFER_MALLOCDATA (buffer) = g_alloc (size);
50 * GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
55 * Alternatively, use gst_buffer_new_and_alloc()
56 * to create a buffer with preallocated data of a given size.
58 * If an element knows what pad you will push the buffer out on, it should use
59 * gst_pad_alloc_buffer() instead to create a buffer. This allows downstream
60 * elements to provide special buffers to write in, like hardware buffers.
62 * gst_buffer_ref() is used to increase the refcount of a buffer. This must be
63 * done when you want to keep a handle to the buffer after pushing it to the
66 * To efficiently create a smaller buffer out of an existing one, you can
67 * use gst_buffer_create_sub().
69 * If the plug-in wants to modify the buffer in-place, it should first obtain
70 * a buffer that is safe to modify by using gst_buffer_make_writable(). This
71 * function is optimized so that a copy will only be made when it is necessary.
73 * Several flags of the buffer can be set and unset with the GST_BUFFER_FLAG_SET()
74 * and GST_BUFFER_FLAG_UNSET() macros. Use GST_BUFFER_FLAG_IS_SET() to test it
75 * a certain #GstBufferFlag is set.
77 * Buffers can be efficiently merged into a larger buffer with gst_buffer_merge()
78 * and gst_buffer_span() if the gst_buffer_is_span_fast() function returns TRUE.
80 * An element should either unref the buffer or push it out on a src pad
81 * using gst_pad_push() (see #GstPad).
83 * Buffers usually are freed by unreffing them with gst_buffer_unref().
84 * Do not use gst_buffer_free() : this function effectively frees the buffer
85 * regardless of the refcount, which is dangerous.
87 * Last reviewed on August 12th, 2004 (0.8.5)
90 #include "gst_private.h"
92 #include "gstbuffer.h"
95 #include "gstminiobject.h"
98 static void gst_buffer_init (GTypeInstance * instance, gpointer g_class);
99 static void gst_buffer_class_init (gpointer g_class, gpointer class_data);
100 static void gst_buffer_finalize (GstBuffer * buffer);
101 static GstBuffer *_gst_buffer_copy (GstBuffer * buffer);
105 _gst_buffer_initialize (void)
109 gst_buffer_get_type ();
111 /* the GstMiniObject types need to be class_ref'd once before it can be
112 * done from multiple threads;
113 * see http://bugzilla.gnome.org/show_bug.cgi?id=304551 */
114 ptr = g_type_class_ref (GST_TYPE_BUFFER);
115 g_type_class_unref (ptr);
119 gst_buffer_get_type (void)
121 static GType _gst_buffer_type;
123 if (G_UNLIKELY (_gst_buffer_type == 0)) {
124 static const GTypeInfo buffer_info = {
125 sizeof (GstBufferClass),
128 gst_buffer_class_init,
137 _gst_buffer_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
138 "GstBuffer", &buffer_info, 0);
140 return _gst_buffer_type;
144 gst_buffer_class_init (gpointer g_class, gpointer class_data)
146 GstBufferClass *buffer_class = GST_BUFFER_CLASS (g_class);
148 buffer_class->mini_object_class.copy =
149 (GstMiniObjectCopyFunction) _gst_buffer_copy;
150 buffer_class->mini_object_class.finalize =
151 (GstMiniObjectFinalizeFunction) gst_buffer_finalize;
156 gst_buffer_finalize (GstBuffer * buffer)
158 g_return_if_fail (buffer != NULL);
161 if (buffer->malloc_data) {
162 g_free (buffer->malloc_data);
165 gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
169 _gst_buffer_copy (GstBuffer * buffer)
174 g_return_val_if_fail (buffer != NULL, NULL);
176 /* create a fresh new buffer */
177 copy = gst_buffer_new ();
179 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", buffer, copy);
181 /* copy relevant flags */
182 mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
183 GST_BUFFER_FLAG_DELTA_UNIT;
184 GST_MINI_OBJECT (copy)->flags |= GST_MINI_OBJECT (buffer)->flags & mask;
186 /* we simply copy everything from our parent */
187 copy->data = g_memdup (buffer->data, buffer->size);
188 /* make sure it gets freed (even if the parent is subclassed, we return a
190 copy->malloc_data = copy->data;
192 copy->size = buffer->size;
194 GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
195 GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
196 GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
197 GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
199 if (GST_BUFFER_CAPS (buffer))
200 GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
202 GST_BUFFER_CAPS (copy) = NULL;
208 gst_buffer_init (GTypeInstance * instance, gpointer g_class)
212 buffer = (GstBuffer *) instance;
214 GST_CAT_LOG (GST_CAT_BUFFER, "init %p", buffer);
216 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
217 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
218 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
219 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
225 * Creates a newly allocated buffer without any data.
227 * Returns: the new #GstBuffer.
232 gst_buffer_new (void)
236 newbuf = (GstBuffer *) gst_mini_object_new (GST_TYPE_BUFFER);
238 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
244 * gst_buffer_new_and_alloc:
245 * @size: the size of the new buffer's data.
247 * Creates a newly allocated buffer with data of the given size.
249 * Returns: the new #GstBuffer.
254 gst_buffer_new_and_alloc (guint size)
258 newbuf = gst_buffer_new ();
260 newbuf->malloc_data = g_malloc (size);
261 GST_BUFFER_DATA (newbuf) = newbuf->malloc_data;
262 GST_BUFFER_SIZE (newbuf) = size;
268 * gst_buffer_get_caps:
269 * @buffer: a #GstBuffer to get the caps of.
271 * Gets the media type of the buffer. This can be NULL if there
272 * is not media type attached to this buffer or when the media
273 * type is the same as the previous received buffer.
275 * This function does not increment the refcount of the caps. The
276 * caps pointer will therefore remain valid until the buffer is
279 * Returns: the #GstCaps, or NULL if there was an error or there
280 * were no caps on this buffer.
282 /* FIXME can we make this threadsafe without a lock on the buffer?
283 * We can use compare and swap and atomic reads. */
285 gst_buffer_get_caps (GstBuffer * buffer)
287 g_return_val_if_fail (buffer != NULL, NULL);
289 return GST_BUFFER_CAPS (buffer);
293 * gst_buffer_set_caps:
294 * @buffer: a #GstBuffer to set the caps of.
295 * @caps: a #GstCaps to set.
297 * Sets the media type on the buffer. The caps' refcount will
298 * be increased and any previous caps on the buffer will be
301 /* FIXME can we make this threadsafe without a lock on the buffer?
302 * We can use compare and swap and atomic reads. Another idea is to
303 * not attach the caps to the buffer but use an event to signal a caps
306 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
308 g_return_if_fail (buffer != NULL);
310 gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
314 typedef struct _GstSubBuffer GstSubBuffer;
315 typedef struct _GstSubBufferClass GstSubBufferClass;
317 #define GST_TYPE_SUBBUFFER (gst_subbuffer_get_type())
319 #define GST_IS_SUBBUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SUBBUFFER))
320 #define GST_SUBBUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SUBBUFFER, GstSubBuffer))
329 struct _GstSubBufferClass
331 GstBufferClass buffer_class;
334 static GstBufferClass *sub_parent_class;
336 static void gst_subbuffer_init (GTypeInstance * instance, gpointer g_class);
337 static void gst_subbuffer_class_init (gpointer g_class, gpointer class_data);
338 static void gst_subbuffer_finalize (GstSubBuffer * buffer);
341 gst_subbuffer_get_type (void)
343 static GType _gst_subbuffer_type = 0;
345 if (G_UNLIKELY (_gst_subbuffer_type == 0)) {
346 static const GTypeInfo subbuffer_info = {
347 sizeof (GstSubBufferClass),
350 gst_subbuffer_class_init,
353 sizeof (GstSubBuffer),
359 _gst_subbuffer_type = g_type_register_static (GST_TYPE_BUFFER,
360 "GstSubBuffer", &subbuffer_info, 0);
362 return _gst_subbuffer_type;
366 gst_subbuffer_class_init (gpointer g_class, gpointer class_data)
368 GstBufferClass *buffer_class = GST_BUFFER_CLASS (g_class);
370 sub_parent_class = g_type_class_ref (GST_TYPE_BUFFER);
372 buffer_class->mini_object_class.finalize =
373 (GstMiniObjectFinalizeFunction) gst_subbuffer_finalize;
377 gst_subbuffer_finalize (GstSubBuffer * buffer)
379 gst_buffer_unref (buffer->parent);
381 GST_MINI_OBJECT_CLASS (sub_parent_class)->finalize (GST_MINI_OBJECT (buffer));
385 gst_subbuffer_init (GTypeInstance * instance, gpointer g_class)
390 * gst_buffer_create_sub:
391 * @parent: a parent #GstBuffer to create a subbuffer from.
392 * @offset: the offset into parent #GstBuffer.
393 * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
395 * Creates a sub-buffer from the parent at a given offset.
396 * This sub-buffer uses the actual memory space of the parent buffer.
397 * This function will copy the offset and timestamp field when the
398 * offset is 0, else they are set to _NONE.
399 * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
401 * Returns: the new #GstBuffer, or NULL if there was an error.
406 gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size)
408 GstSubBuffer *subbuffer;
411 g_return_val_if_fail (buffer != NULL, NULL);
412 g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL);
413 g_return_val_if_fail (size > 0, NULL);
414 g_return_val_if_fail (buffer->size >= offset + size, NULL);
416 /* find real parent */
417 if (GST_IS_SUBBUFFER (buffer)) {
418 parent = GST_SUBBUFFER (buffer)->parent;
422 gst_buffer_ref (parent);
424 /* create the new buffer */
425 subbuffer = (GstSubBuffer *) gst_mini_object_new (GST_TYPE_SUBBUFFER);
426 subbuffer->parent = parent;
428 GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", subbuffer,
431 /* set the right values in the child */
432 GST_BUFFER_DATA (GST_BUFFER_CAST (subbuffer)) = buffer->data + offset;
433 GST_BUFFER_SIZE (GST_BUFFER_CAST (subbuffer)) = size;
435 /* we can copy the timestamp and offset if the new buffer starts at
438 GST_BUFFER_TIMESTAMP (subbuffer) = GST_BUFFER_TIMESTAMP (buffer);
439 GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET (buffer);
441 GST_BUFFER_TIMESTAMP (subbuffer) = GST_CLOCK_TIME_NONE;
442 GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET_NONE;
445 GST_BUFFER_DURATION (subbuffer) = GST_CLOCK_TIME_NONE;
446 GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_NONE;
448 GST_BUFFER_CAPS (subbuffer) = NULL;
450 return GST_BUFFER_CAST (subbuffer);
454 * gst_buffer_is_span_fast:
455 * @buf1: a first source #GstBuffer.
456 * @buf2: the second source #GstBuffer.
458 * Determines whether a gst_buffer_span() can be done without copying
459 * the contents, that is, whether the data areas are contiguous.
461 * Returns: TRUE if the buffers are contiguous,
462 * FALSE if a copy would be required.
467 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
469 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
470 g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
471 g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
473 /* it's only fast if we have subbuffers of the same parent */
474 return (GST_IS_SUBBUFFER (buf1) &&
475 GST_IS_SUBBUFFER (buf2) &&
476 (GST_SUBBUFFER (buf1)->parent == GST_SUBBUFFER (buf2)->parent) &&
477 ((buf1->data + buf1->size) == buf2->data));
482 * @buf1: a first source #GstBuffer to merge.
483 * @offset: the offset in the first buffer from where the new
484 * buffer should start.
485 * @buf2: the second source #GstBuffer to merge.
486 * @len: the total length of the new buffer.
488 * Creates a new buffer that consists of part of buf1 and buf2.
489 * Logically, buf1 and buf2 are concatenated into a single larger
490 * buffer, and a new buffer is created at the given offset inside
491 * this space, with a given length.
493 * If the two source buffers are children of the same larger buffer,
494 * and are contiguous, the new buffer will be a child of the shared
495 * parent, and thus no copying is necessary. you can use
496 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
498 * Returns: the new #GstBuffer that spans the two source buffers.
503 gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
508 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
509 g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
510 g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
511 g_return_val_if_fail (len > 0, NULL);
512 g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
514 /* if the two buffers have the same parent and are adjacent */
515 if (gst_buffer_is_span_fast (buf1, buf2)) {
516 GstBuffer *parent = GST_SUBBUFFER (buf1)->parent;
518 /* we simply create a subbuffer of the common parent */
519 newbuf = gst_buffer_create_sub (parent,
520 buf1->data - parent->data + offset, len);
522 GST_CAT_DEBUG (GST_CAT_BUFFER,
523 "slow path taken while spanning buffers %p and %p", buf1, buf2);
524 /* otherwise we simply have to brute-force copy the buffers */
525 newbuf = gst_buffer_new_and_alloc (len);
527 /* copy the first buffer's data across */
528 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
529 /* copy the second buffer's data across */
530 memcpy (newbuf->data + (buf1->size - offset), buf2->data,
531 len - (buf1->size - offset));
532 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
534 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
535 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
538 /* if we completely merged the two buffers (appended), we can
539 * calculate the duration too. Also make sure we's not messing with
540 * invalid DURATIONS */
541 if (offset == 0 && buf1->size + buf2->size == len) {
542 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
543 GST_BUFFER_DURATION_IS_VALID (buf2)) {
545 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
546 GST_BUFFER_DURATION (buf2);
548 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
550 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);