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 "gstdata_private.h"
26 #include "gstbuffer.h"
27 #include "gstmemchunk.h"
31 GType _gst_buffer_type = 0;
33 #ifndef GST_DISABLE_TRACE
34 /* #define GST_WITH_ALLOC_TRACE */
37 static GstAllocTrace *_gst_buffer_trace;
40 static GstMemChunk *chunk;
42 static GstBuffer *gst_buffer_alloc_chunk (void);
43 static void gst_buffer_free_chunk (GstBuffer * buffer);
46 _gst_buffer_initialize (void)
48 gst_buffer_get_type ();
50 #ifndef GST_DISABLE_TRACE
51 _gst_buffer_trace = gst_alloc_trace_register (GST_BUFFER_TRACE_NAME);
54 chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer),
55 sizeof (GstBuffer) * 200, 0);
57 GST_CAT_LOG (GST_CAT_BUFFER, "Buffers are initialized now");
61 gst_buffer_get_type (void)
63 if (G_UNLIKELY (_gst_buffer_type == 0)) {
64 _gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
65 (GBoxedCopyFunc) gst_data_copy, (GBoxedFreeFunc) gst_data_unref);
67 return _gst_buffer_type;
71 _gst_buffer_sub_free (GstBuffer * buffer)
73 gst_data_unref (GST_DATA (buffer->buffer_private));
75 GST_BUFFER_DATA (buffer) = NULL;
76 GST_BUFFER_SIZE (buffer) = 0;
77 gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
79 _GST_DATA_DISPOSE (GST_DATA (buffer));
81 gst_buffer_free_chunk (buffer);
85 * gst_buffer_default_free:
86 * @buffer: a #GstBuffer to free.
88 * Frees the memory associated with the buffer including the buffer data,
89 * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
94 gst_buffer_default_free (GstBuffer * buffer)
96 g_return_if_fail (buffer != NULL);
99 if (GST_BUFFER_FREE_DATA_FUNC (buffer)) {
100 GST_BUFFER_FREE_DATA_FUNC (buffer) (buffer);
101 } else if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE)) {
102 g_free (GST_BUFFER_DATA (buffer));
105 /* set to safe values */
106 GST_BUFFER_DATA (buffer) = NULL;
107 GST_BUFFER_SIZE (buffer) = 0;
108 gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
110 _GST_DATA_DISPOSE (GST_DATA (buffer));
112 gst_buffer_free_chunk (buffer);
117 * gst_buffer_default_copy:
118 * @buffer: a #GstBuffer to make a copy of.
120 * Make a full newly allocated copy of the given buffer, data and all.
121 * Note that the caps on the buffer are not copied but their refcount
124 * Returns: the new #GstBuffer.
129 gst_buffer_default_copy (GstBuffer * buffer)
134 g_return_val_if_fail (buffer != NULL, NULL);
136 /* create a fresh new buffer */
137 copy = gst_buffer_alloc_chunk ();
139 GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", buffer, copy);
141 /* copy relevant flags */
142 flags = GST_DATA_FLAG_SHIFT (GST_BUFFER_PREROLL) |
143 GST_DATA_FLAG_SHIFT (GST_BUFFER_IN_CAPS) |
144 GST_DATA_FLAG_SHIFT (GST_BUFFER_DELTA_UNIT);
145 flags = GST_BUFFER_FLAGS (buffer) & flags;
147 _GST_DATA_INIT (GST_DATA (copy),
150 (GstDataFreeFunction) gst_buffer_default_free,
151 (GstDataCopyFunction) gst_buffer_default_copy);
153 /* we simply copy everything from our parent */
154 GST_BUFFER_DATA (copy) = g_memdup (GST_BUFFER_DATA (buffer),
155 GST_BUFFER_SIZE (buffer));
156 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
157 GST_BUFFER_MAXSIZE (copy) = GST_BUFFER_SIZE (buffer);
159 GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
160 GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
161 GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
162 GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
164 GST_BUFFER_FREE_DATA_FUNC (copy) = NULL;
165 GST_BUFFER_PRIVATE (copy) = NULL;
166 if (GST_BUFFER_CAPS (buffer))
167 GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
169 GST_BUFFER_CAPS (copy) = NULL;
175 gst_buffer_alloc_chunk (void)
179 newbuf = gst_mem_chunk_alloc (chunk);
180 #ifndef GST_DISABLE_TRACE
181 gst_alloc_trace_new (_gst_buffer_trace, newbuf);
188 gst_buffer_free_chunk (GstBuffer * buffer)
190 gst_mem_chunk_free (chunk, GST_DATA (buffer));
191 #ifndef GST_DISABLE_TRACE
192 gst_alloc_trace_free (_gst_buffer_trace, buffer);
199 * Creates a newly allocated buffer without any data.
201 * Returns: the new #GstBuffer.
206 gst_buffer_new (void)
210 newbuf = gst_buffer_alloc_chunk ();
212 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
214 _GST_DATA_INIT (GST_DATA (newbuf),
217 (GstDataFreeFunction) gst_buffer_default_free,
218 (GstDataCopyFunction) gst_buffer_default_copy);
220 GST_BUFFER_DATA (newbuf) = NULL;
221 GST_BUFFER_SIZE (newbuf) = 0;
222 GST_BUFFER_MAXSIZE (newbuf) = GST_BUFFER_MAXSIZE_NONE;
223 GST_BUFFER_TIMESTAMP (newbuf) = GST_CLOCK_TIME_NONE;
224 GST_BUFFER_DURATION (newbuf) = GST_CLOCK_TIME_NONE;
225 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET_NONE;
226 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_NONE;
227 GST_BUFFER_FREE_DATA_FUNC (newbuf) = NULL;
228 GST_BUFFER_PRIVATE (newbuf) = NULL;
229 GST_BUFFER_CAPS (newbuf) = NULL;
235 * gst_buffer_new_and_alloc:
236 * @size: the size of the new buffer's data.
238 * Creates a newly allocated buffer with data of the given size.
240 * Returns: the new #GstBuffer.
245 gst_buffer_new_and_alloc (guint size)
249 newbuf = gst_buffer_new ();
251 GST_BUFFER_DATA (newbuf) = g_malloc (size);
252 GST_BUFFER_SIZE (newbuf) = size;
253 GST_BUFFER_MAXSIZE (newbuf) = size;
260 * gst_buffer_get_caps:
261 * @buffer: a #GstBuffer to get the caps of.
263 * Gets the media type of the buffer. This can be NULL if there
264 * is not media type attached to this buffer or when the media
265 * type is the same as the previous received buffer.
267 * This function does not increment the refcount of the caps. The
268 * caps pointer will therefore remain valid until the buffer is
271 * Returns: the #GstCaps, or NULL if there was an error or there
272 * were no caps on this buffer.
274 /* FIXME can we make this threadsafe without a lock on the buffer?
275 * We can use compare and swap and atomic reads. */
277 gst_buffer_get_caps (GstBuffer * buffer)
279 g_return_val_if_fail (buffer != NULL, NULL);
281 return GST_BUFFER_CAPS (buffer);
285 * gst_buffer_set_caps:
286 * @buffer: a #GstBuffer to set the caps of.
287 * @caps: a #GstCaps to set.
289 * Sets the media type on the buffer. The caps' refcount will
290 * be increased and any previous caps on the buffer will be
293 /* FIXME can we make this threadsafe without a lock on the buffer?
294 * We can use compare and swap and atomic reads. Another idea is to
295 * not attach the caps to the buffer but use an event to signal a caps
298 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
300 g_return_if_fail (buffer != NULL);
302 gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
306 * gst_buffer_create_sub:
307 * @parent: a parent #GstBuffer to create a subbuffer from.
308 * @offset: the offset into parent #GstBuffer.
309 * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
311 * Creates a sub-buffer from the parent at a given offset.
312 * This sub-buffer uses the actual memory space of the parent buffer.
313 * This function will copy the offset and timestamp field when the
314 * offset is 0, else they are set to _NONE.
315 * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
317 * Returns: the new #GstBuffer, or NULL if there was an error.
322 gst_buffer_create_sub (GstBuffer * parent, guint offset, guint size)
325 gpointer buffer_data;
327 g_return_val_if_fail (parent != NULL, NULL);
328 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
329 g_return_val_if_fail (size > 0, NULL);
330 g_return_val_if_fail (parent->size >= offset + size, NULL);
332 /* remember the data for the new buffer */
333 buffer_data = parent->data + offset;
334 /* make sure we're child not child from a child buffer */
335 while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
336 parent = GST_BUFFER (parent->buffer_private);
338 /* ref the real parent */
339 gst_data_ref (GST_DATA (parent));
341 /* create the new buffer */
342 buffer = gst_buffer_alloc_chunk ();
344 GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", buffer, parent);
346 /* make sure nobody overwrites data in the new buffer
347 * by setting the READONLY flag */
348 _GST_DATA_INIT (GST_DATA (buffer),
350 GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
351 GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
352 (GstDataFreeFunction) _gst_buffer_sub_free,
353 (GstDataCopyFunction) gst_buffer_default_copy);
355 /* set the right values in the child */
356 GST_BUFFER_DATA (buffer) = buffer_data;
357 GST_BUFFER_SIZE (buffer) = size;
358 GST_BUFFER_MAXSIZE (buffer) = size;
359 GST_BUFFER_FREE_DATA_FUNC (buffer) = NULL;
360 GST_BUFFER_PRIVATE (buffer) = parent;
361 /* we can copy the timestamp and offset if the new buffer starts at
364 GST_BUFFER_TIMESTAMP (buffer) = GST_BUFFER_TIMESTAMP (parent);
365 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET (parent);
367 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
368 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
371 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
372 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
374 if (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_READONLY)) {
375 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_READONLY);
377 GST_BUFFER_CAPS (buffer) = NULL;
383 * gst_buffer_is_span_fast:
384 * @buf1: a first source #GstBuffer.
385 * @buf2: the second source #GstBuffer.
387 * Determines whether a gst_buffer_span() can be done without copying
388 * the contents, that is, whether the data areas are contiguous.
390 * Returns: TRUE if the buffers are contiguous,
391 * FALSE if a copy would be required.
396 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
398 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
399 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
400 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
402 /* it's only fast if we have subbuffers of the same parent */
403 return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
404 (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
405 (buf1->buffer_private == buf2->buffer_private) &&
406 ((buf1->data + buf1->size) == buf2->data));
411 * @buf1: a first source #GstBuffer to merge.
412 * @offset: the offset in the first buffer from where the new
413 * buffer should start.
414 * @buf2: the second source #GstBuffer to merge.
415 * @len: the total length of the new buffer.
417 * Creates a new buffer that consists of part of buf1 and buf2.
418 * Logically, buf1 and buf2 are concatenated into a single larger
419 * buffer, and a new buffer is created at the given offset inside
420 * this space, with a given length.
422 * If the two source buffers are children of the same larger buffer,
423 * and are contiguous, the new buffer will be a child of the shared
424 * parent, and thus no copying is necessary. you can use
425 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
427 * Returns: the new #GstBuffer that spans the two source buffers.
432 gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
437 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
438 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
439 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
440 g_return_val_if_fail (len > 0, NULL);
441 g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
443 /* if the two buffers have the same parent and are adjacent */
444 if (gst_buffer_is_span_fast (buf1, buf2)) {
445 GstBuffer *parent = GST_BUFFER (buf1->buffer_private);
447 /* we simply create a subbuffer of the common parent */
448 newbuf = gst_buffer_create_sub (parent,
449 buf1->data - parent->data + offset, len);
451 GST_CAT_DEBUG (GST_CAT_BUFFER,
452 "slow path taken while spanning buffers %p and %p", buf1, buf2);
453 /* otherwise we simply have to brute-force copy the buffers */
454 newbuf = gst_buffer_new_and_alloc (len);
456 /* copy the first buffer's data across */
457 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
458 /* copy the second buffer's data across */
459 memcpy (newbuf->data + (buf1->size - offset), buf2->data,
460 len - (buf1->size - offset));
461 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
463 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
464 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
467 /* if we completely merged the two buffers (appended), we can
468 * calculate the duration too. Also make sure we's not messing with
469 * invalid DURATIONS */
470 if (offset == 0 && buf1->size + buf2->size == len) {
471 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
472 GST_BUFFER_DURATION_IS_VALID (buf2)) {
474 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
475 GST_BUFFER_DURATION (buf2);
477 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
479 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);