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.
23 #include "gst_private.h"
25 #include "gstbuffer.h"
28 #include "gstminiobject.h"
30 #ifndef GST_DISABLE_TRACE
31 /* #define GST_WITH_ALLOC_TRACE */
34 static GstAllocTrace *_gst_buffer_trace;
37 static void gst_buffer_init (GTypeInstance * instance, gpointer g_class);
38 static void gst_buffer_class_init (gpointer g_class, gpointer class_data);
39 static void gst_buffer_finalize (GstBuffer * buffer);
40 static GstBuffer *_gst_buffer_copy (GstBuffer * buffer);
44 _gst_buffer_initialize (void)
46 gst_buffer_get_type ();
48 #ifndef GST_DISABLE_TRACE
49 _gst_buffer_trace = gst_alloc_trace_register (GST_BUFFER_TRACE_NAME);
54 gst_buffer_get_type (void)
56 static GType _gst_buffer_type;
58 if (G_UNLIKELY (_gst_buffer_type == 0)) {
59 static const GTypeInfo buffer_info = {
60 sizeof (GstBufferClass),
63 gst_buffer_class_init,
72 _gst_buffer_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
73 "GstBuffer", &buffer_info, 0);
75 return _gst_buffer_type;
79 gst_buffer_class_init (gpointer g_class, gpointer class_data)
81 GstBufferClass *buffer_class = GST_BUFFER_CLASS (g_class);
83 buffer_class->mini_object_class.copy =
84 (GstMiniObjectCopyFunction) _gst_buffer_copy;
85 buffer_class->mini_object_class.finalize =
86 (GstMiniObjectFinalizeFunction) gst_buffer_finalize;
91 gst_buffer_finalize (GstBuffer * buffer)
93 g_return_if_fail (buffer != NULL);
96 if (buffer->malloc_data) {
97 g_free (buffer->malloc_data);
100 gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
104 _gst_buffer_copy (GstBuffer * buffer)
109 g_return_val_if_fail (buffer != NULL, NULL);
111 /* create a fresh new buffer */
112 copy = gst_buffer_new ();
114 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", buffer, copy);
116 /* copy relevant flags */
117 mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
118 GST_BUFFER_FLAG_DELTA_UNIT;
119 GST_MINI_OBJECT (copy)->flags |= GST_MINI_OBJECT (buffer)->flags & mask;
121 /* we simply copy everything from our parent */
122 if (buffer->malloc_data) {
123 copy->malloc_data = g_memdup (buffer->data, buffer->size);
124 copy->data = copy->malloc_data;
127 copy->size = buffer->size;
129 GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
130 GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
131 GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
132 GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
134 if (GST_BUFFER_CAPS (buffer))
135 GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
137 GST_BUFFER_CAPS (copy) = NULL;
143 gst_buffer_init (GTypeInstance * instance, gpointer g_class)
147 buffer = (GstBuffer *) instance;
149 GST_CAT_LOG (GST_CAT_BUFFER, "init %p", buffer);
151 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
152 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
153 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
154 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
160 * Creates a newly allocated buffer without any data.
162 * Returns: the new #GstBuffer.
167 gst_buffer_new (void)
171 newbuf = (GstBuffer *) gst_mini_object_new (GST_TYPE_BUFFER);
173 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
179 * gst_buffer_new_and_alloc:
180 * @size: the size of the new buffer's data.
182 * Creates a newly allocated buffer with data of the given size.
184 * Returns: the new #GstBuffer.
189 gst_buffer_new_and_alloc (guint size)
193 newbuf = gst_buffer_new ();
195 newbuf->malloc_data = g_malloc (size);
196 GST_BUFFER_DATA (newbuf) = newbuf->malloc_data;
197 GST_BUFFER_SIZE (newbuf) = size;
204 * gst_buffer_get_caps:
205 * @buffer: a #GstBuffer to get the caps of.
207 * Gets the media type of the buffer. This can be NULL if there
208 * is not media type attached to this buffer or when the media
209 * type is the same as the previous received buffer.
211 * This function does not increment the refcount of the caps. The
212 * caps pointer will therefore remain valid until the buffer is
215 * Returns: the #GstCaps, or NULL if there was an error or there
216 * were no caps on this buffer.
218 /* FIXME can we make this threadsafe without a lock on the buffer?
219 * We can use compare and swap and atomic reads. */
221 gst_buffer_get_caps (GstBuffer * buffer)
223 g_return_val_if_fail (buffer != NULL, NULL);
225 return GST_BUFFER_CAPS (buffer);
229 * gst_buffer_set_caps:
230 * @buffer: a #GstBuffer to set the caps of.
231 * @caps: a #GstCaps to set.
233 * Sets the media type on the buffer. The caps' refcount will
234 * be increased and any previous caps on the buffer will be
237 /* FIXME can we make this threadsafe without a lock on the buffer?
238 * We can use compare and swap and atomic reads. Another idea is to
239 * not attach the caps to the buffer but use an event to signal a caps
242 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
244 g_return_if_fail (buffer != NULL);
246 gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
250 typedef struct _GstSubBuffer GstSubBuffer;
251 typedef struct _GstSubBufferClass GstSubBufferClass;
253 #define GST_TYPE_SUBBUFFER (gst_subbuffer_get_type())
255 #define GST_IS_SUBBUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SUBBUFFER))
256 #define GST_SUBBUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SUBBUFFER, GstSubBuffer))
265 struct _GstSubBufferClass
267 GstBufferClass buffer_class;
270 static GstBufferClass *sub_parent_class;
272 static void gst_subbuffer_init (GTypeInstance * instance, gpointer g_class);
273 static void gst_subbuffer_class_init (gpointer g_class, gpointer class_data);
274 static void gst_subbuffer_finalize (GstSubBuffer * buffer);
277 gst_subbuffer_get_type (void)
279 static GType _gst_subbuffer_type = 0;
281 if (G_UNLIKELY (_gst_subbuffer_type == 0)) {
282 static const GTypeInfo subbuffer_info = {
283 sizeof (GstSubBufferClass),
286 gst_subbuffer_class_init,
289 sizeof (GstSubBuffer),
295 _gst_subbuffer_type = g_type_register_static (GST_TYPE_BUFFER,
296 "GstSubBuffer", &subbuffer_info, 0);
298 return _gst_subbuffer_type;
302 gst_subbuffer_class_init (gpointer g_class, gpointer class_data)
304 GstBufferClass *buffer_class = GST_BUFFER_CLASS (g_class);
306 sub_parent_class = g_type_class_ref (GST_TYPE_BUFFER);
308 buffer_class->mini_object_class.finalize =
309 (GstMiniObjectFinalizeFunction) gst_subbuffer_finalize;
313 gst_subbuffer_finalize (GstSubBuffer * buffer)
315 gst_buffer_unref (buffer->parent);
317 GST_MINI_OBJECT_CLASS (sub_parent_class)->finalize (GST_MINI_OBJECT (buffer));
321 gst_subbuffer_init (GTypeInstance * instance, gpointer g_class)
326 * gst_buffer_create_subbuffer:
327 * @parent: a parent #GstBuffer to create a subbuffer from.
328 * @offset: the offset into parent #GstBuffer.
329 * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
331 * Creates a sub-buffer from the parent at a given offset.
332 * This sub-buffer uses the actual memory space of the parent buffer.
333 * This function will copy the offset and timestamp field when the
334 * offset is 0, else they are set to _NONE.
335 * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
337 * Returns: the new #GstBuffer, or NULL if there was an error.
342 gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size)
344 GstSubBuffer *subbuffer;
347 g_return_val_if_fail (buffer != NULL, NULL);
348 g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL);
349 g_return_val_if_fail (size > 0, NULL);
350 g_return_val_if_fail (buffer->size >= offset + size, NULL);
352 /* find real parent */
353 if (GST_IS_SUBBUFFER (buffer)) {
354 parent = GST_SUBBUFFER (buffer)->parent;
358 gst_buffer_ref (parent);
360 /* create the new buffer */
361 subbuffer = (GstSubBuffer *) gst_mini_object_new (GST_TYPE_SUBBUFFER);
362 subbuffer->parent = parent;
364 GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", subbuffer,
367 /* set the right values in the child */
368 GST_BUFFER_DATA (GST_BUFFER_CAST (subbuffer)) = buffer->data + offset;
369 GST_BUFFER_SIZE (GST_BUFFER_CAST (subbuffer)) = size;
371 /* we can copy the timestamp and offset if the new buffer starts at
374 GST_BUFFER_TIMESTAMP (subbuffer) = GST_BUFFER_TIMESTAMP (buffer);
375 GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET (buffer);
377 GST_BUFFER_TIMESTAMP (subbuffer) = GST_CLOCK_TIME_NONE;
378 GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET_NONE;
381 GST_BUFFER_DURATION (subbuffer) = GST_CLOCK_TIME_NONE;
382 GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_NONE;
384 GST_BUFFER_CAPS (subbuffer) = NULL;
386 return GST_BUFFER_CAST (subbuffer);
390 * gst_buffer_is_span_fast:
391 * @buf1: a first source #GstBuffer.
392 * @buf2: the second source #GstBuffer.
394 * Determines whether a gst_buffer_span() can be done without copying
395 * the contents, that is, whether the data areas are contiguous.
397 * Returns: TRUE if the buffers are contiguous,
398 * FALSE if a copy would be required.
403 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
405 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
406 g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
407 g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
409 /* it's only fast if we have subbuffers of the same parent */
410 return (GST_IS_SUBBUFFER (buf1) &&
411 GST_IS_SUBBUFFER (buf2) &&
412 (GST_SUBBUFFER (buf1)->parent == GST_SUBBUFFER (buf2)->parent) &&
413 ((buf1->data + buf1->size) == buf2->data));
418 * @buf1: a first source #GstBuffer to merge.
419 * @offset: the offset in the first buffer from where the new
420 * buffer should start.
421 * @buf2: the second source #GstBuffer to merge.
422 * @len: the total length of the new buffer.
424 * Creates a new buffer that consists of part of buf1 and buf2.
425 * Logically, buf1 and buf2 are concatenated into a single larger
426 * buffer, and a new buffer is created at the given offset inside
427 * this space, with a given length.
429 * If the two source buffers are children of the same larger buffer,
430 * and are contiguous, the new buffer will be a child of the shared
431 * parent, and thus no copying is necessary. you can use
432 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
434 * Returns: the new #GstBuffer that spans the two source buffers.
439 gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
444 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
445 g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
446 g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
447 g_return_val_if_fail (len > 0, NULL);
448 g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
450 /* if the two buffers have the same parent and are adjacent */
451 if (gst_buffer_is_span_fast (buf1, buf2)) {
452 GstBuffer *parent = GST_SUBBUFFER (buf1)->parent;
454 /* we simply create a subbuffer of the common parent */
455 newbuf = gst_buffer_create_sub (parent,
456 buf1->data - parent->data + offset, len);
458 GST_CAT_DEBUG (GST_CAT_BUFFER,
459 "slow path taken while spanning buffers %p and %p", buf1, buf2);
460 /* otherwise we simply have to brute-force copy the buffers */
461 newbuf = gst_buffer_new_and_alloc (len);
463 /* copy the first buffer's data across */
464 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
465 /* copy the second buffer's data across */
466 memcpy (newbuf->data + (buf1->size - offset), buf2->data,
467 len - (buf1->size - offset));
468 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
470 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
471 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
474 /* if we completely merged the two buffers (appended), we can
475 * calculate the duration too. Also make sure we's not messing with
476 * invalid DURATIONS */
477 if (offset == 0 && buf1->size + buf2->size == len) {
478 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
479 GST_BUFFER_DURATION_IS_VALID (buf2)) {
481 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
482 GST_BUFFER_DURATION (buf2);
484 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
486 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);