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_DATA (buffer) = NULL;
152 //GST_BUFFER_SIZE (buffer) = 0;
153 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
154 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
155 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
156 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
157 //GST_BUFFER_CAPS (buffer) = NULL;
163 * Creates a newly allocated buffer without any data.
165 * Returns: the new #GstBuffer.
170 gst_buffer_new (void)
174 newbuf = (GstBuffer *) gst_mini_object_new (GST_TYPE_BUFFER);
176 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
182 * gst_buffer_new_and_alloc:
183 * @size: the size of the new buffer's data.
185 * Creates a newly allocated buffer with data of the given size.
187 * Returns: the new #GstBuffer.
192 gst_buffer_new_and_alloc (guint size)
196 newbuf = gst_buffer_new ();
198 newbuf->malloc_data = g_malloc (size);
199 GST_BUFFER_DATA (newbuf) = newbuf->malloc_data;
200 GST_BUFFER_SIZE (newbuf) = size;
207 * gst_buffer_get_caps:
208 * @buffer: a #GstBuffer to get the caps of.
210 * Gets the media type of the buffer. This can be NULL if there
211 * is not media type attached to this buffer or when the media
212 * type is the same as the previous received buffer.
214 * This function does not increment the refcount of the caps. The
215 * caps pointer will therefore remain valid until the buffer is
218 * Returns: the #GstCaps, or NULL if there was an error or there
219 * were no caps on this buffer.
221 /* FIXME can we make this threadsafe without a lock on the buffer?
222 * We can use compare and swap and atomic reads. */
224 gst_buffer_get_caps (GstBuffer * buffer)
226 g_return_val_if_fail (buffer != NULL, NULL);
228 return GST_BUFFER_CAPS (buffer);
232 * gst_buffer_set_caps:
233 * @buffer: a #GstBuffer to set the caps of.
234 * @caps: a #GstCaps to set.
236 * Sets the media type on the buffer. The caps' refcount will
237 * be increased and any previous caps on the buffer will be
240 /* FIXME can we make this threadsafe without a lock on the buffer?
241 * We can use compare and swap and atomic reads. Another idea is to
242 * not attach the caps to the buffer but use an event to signal a caps
245 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
247 g_return_if_fail (buffer != NULL);
249 gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
253 typedef struct _GstSubBuffer GstSubBuffer;
254 typedef struct _GstSubBufferClass GstSubBufferClass;
256 #define GST_TYPE_SUBBUFFER (gst_subbuffer_get_type())
258 #define GST_IS_SUBBUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_BUFFER))
259 #define GST_SUBBUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SUBBUFFER, GstSubBuffer))
268 struct _GstSubBufferClass
270 GstBufferClass buffer_class;
273 static void gst_subbuffer_init (GTypeInstance * instance, gpointer g_class);
276 gst_subbuffer_get_type (void)
278 static GType _gst_subbuffer_type;
280 if (G_UNLIKELY (_gst_subbuffer_type == 0)) {
281 static const GTypeInfo subbuffer_info = {
282 sizeof (GstSubBufferClass),
288 sizeof (GstSubBuffer),
294 _gst_subbuffer_type = g_type_register_static (GST_TYPE_BUFFER,
295 "GstSubBuffer", &subbuffer_info, 0);
297 return _gst_subbuffer_type;
301 gst_subbuffer_init (GTypeInstance * instance, gpointer g_class)
307 * gst_buffer_create_subbuffer:
308 * @parent: a parent #GstBuffer to create a subbuffer from.
309 * @offset: the offset into parent #GstBuffer.
310 * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
312 * Creates a sub-buffer from the parent at a given offset.
313 * This sub-buffer uses the actual memory space of the parent buffer.
314 * This function will copy the offset and timestamp field when the
315 * offset is 0, else they are set to _NONE.
316 * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
318 * Returns: the new #GstBuffer, or NULL if there was an error.
323 gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size)
325 GstSubBuffer *subbuffer;
328 g_return_val_if_fail (buffer != NULL, NULL);
329 g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL);
330 g_return_val_if_fail (size > 0, NULL);
331 g_return_val_if_fail (buffer->size >= offset + size, NULL);
333 /* find real parent */
334 if (GST_IS_SUBBUFFER (buffer)) {
335 parent = GST_SUBBUFFER (buffer)->parent;
339 gst_buffer_ref (parent);
341 /* create the new buffer */
342 subbuffer = (GstSubBuffer *) gst_mini_object_new (GST_TYPE_SUBBUFFER);
343 subbuffer->parent = parent;
345 GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", subbuffer,
348 /* set the right values in the child */
349 GST_BUFFER_DATA (GST_BUFFER (subbuffer)) = buffer->data + offset;
350 GST_BUFFER_SIZE (GST_BUFFER (subbuffer)) = size;
352 /* we can copy the timestamp and offset if the new buffer starts at
355 GST_BUFFER_TIMESTAMP (subbuffer) = GST_BUFFER_TIMESTAMP (buffer);
356 GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET (buffer);
358 GST_BUFFER_TIMESTAMP (subbuffer) = GST_CLOCK_TIME_NONE;
359 GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET_NONE;
362 GST_BUFFER_DURATION (subbuffer) = GST_CLOCK_TIME_NONE;
363 GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_NONE;
365 GST_BUFFER_CAPS (subbuffer) = NULL;
367 return GST_BUFFER (subbuffer);
371 * gst_buffer_is_span_fast:
372 * @buf1: a first source #GstBuffer.
373 * @buf2: the second source #GstBuffer.
375 * Determines whether a gst_buffer_span() can be done without copying
376 * the contents, that is, whether the data areas are contiguous.
378 * Returns: TRUE if the buffers are contiguous,
379 * FALSE if a copy would be required.
384 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
388 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
389 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
390 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
392 /* it's only fast if we have subbuffers of the same parent */
393 return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
394 (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
395 (buf1->buffer_private == buf2->buffer_private) &&
396 ((buf1->data + buf1->size) == buf2->data));
402 * @buf1: a first source #GstBuffer to merge.
403 * @offset: the offset in the first buffer from where the new
404 * buffer should start.
405 * @buf2: the second source #GstBuffer to merge.
406 * @len: the total length of the new buffer.
408 * Creates a new buffer that consists of part of buf1 and buf2.
409 * Logically, buf1 and buf2 are concatenated into a single larger
410 * buffer, and a new buffer is created at the given offset inside
411 * this space, with a given length.
413 * If the two source buffers are children of the same larger buffer,
414 * and are contiguous, the new buffer will be a child of the shared
415 * parent, and thus no copying is necessary. you can use
416 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
418 * Returns: the new #GstBuffer that spans the two source buffers.
423 gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
430 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
431 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
432 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
433 g_return_val_if_fail (len > 0, NULL);
434 g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
436 /* if the two buffers have the same parent and are adjacent */
437 if (gst_buffer_is_span_fast (buf1, buf2)) {
438 GstBuffer *parent = GST_BUFFER (buf1->buffer_private);
440 /* we simply create a subbuffer of the common parent */
441 newbuf = gst_buffer_create_sub (parent,
442 buf1->data - parent->data + offset, len);
444 GST_CAT_DEBUG (GST_CAT_BUFFER,
445 "slow path taken while spanning buffers %p and %p", buf1, buf2);
446 /* otherwise we simply have to brute-force copy the buffers */
447 newbuf = gst_buffer_new_and_alloc (len);
449 /* copy the first buffer's data across */
450 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
451 /* copy the second buffer's data across */
452 memcpy (newbuf->data + (buf1->size - offset), buf2->data,
453 len - (buf1->size - offset));
454 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
456 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
457 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
460 /* if we completely merged the two buffers (appended), we can
461 * calculate the duration too. Also make sure we's not messing with
462 * invalid DURATIONS */
463 if (offset == 0 && buf1->size + buf2->size == len) {
464 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
465 GST_BUFFER_DURATION_IS_VALID (buf2)) {
467 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
468 GST_BUFFER_DURATION (buf2);
470 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
472 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);