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 "gstatomic_impl.h"
26 #include "gstdata_private.h"
27 #include "gstbuffer.h"
28 #include "gstmemchunk.h"
31 GType _gst_buffer_type;
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 (_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;
78 _GST_DATA_DISPOSE (GST_DATA (buffer));
80 gst_buffer_free_chunk (buffer);
84 * gst_buffer_default_free:
85 * @buffer: a #GstBuffer to free.
87 * Frees the memory associated with the buffer including the buffer data,
88 * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
91 gst_buffer_default_free (GstBuffer * buffer)
93 g_return_if_fail (buffer != NULL);
96 if (GST_BUFFER_FREE_DATA_FUNC (buffer)) {
97 GST_BUFFER_FREE_DATA_FUNC (buffer) (buffer);
98 } else if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE)) {
99 g_free (GST_BUFFER_DATA (buffer));
102 /* set to safe values */
103 GST_BUFFER_DATA (buffer) = NULL;
104 GST_BUFFER_SIZE (buffer) = 0;
106 _GST_DATA_DISPOSE (GST_DATA (buffer));
108 gst_buffer_free_chunk (buffer);
113 * @dest: buffer to stamp
114 * @src: buffer to stamp from
116 * Copies additional information (timestamps and offsets) from one buffer to
120 gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src)
122 g_return_if_fail (dest != NULL);
123 g_return_if_fail (src != NULL);
125 GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
126 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
127 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
128 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
132 * gst_buffer_default_copy:
133 * @buffer: a #GstBuffer to make a copy of.
135 * Make a full newly allocated copy of the given buffer, data and all.
137 * Returns: the new #GstBuffer.
140 gst_buffer_default_copy (GstBuffer * buffer)
145 g_return_val_if_fail (buffer != NULL, NULL);
147 /* create a fresh new buffer */
148 copy = gst_buffer_alloc_chunk ();
150 /* copy relevant flags */
151 flags = GST_DATA_FLAG_SHIFT (GST_BUFFER_KEY_UNIT) |
152 GST_DATA_FLAG_SHIFT (GST_BUFFER_IN_CAPS);
154 _GST_DATA_INIT (GST_DATA (copy),
157 (GstDataFreeFunction) gst_buffer_default_free,
158 (GstDataCopyFunction) gst_buffer_default_copy);
160 /* we simply copy everything from our parent */
161 GST_BUFFER_DATA (copy) = g_memdup (GST_BUFFER_DATA (buffer),
162 GST_BUFFER_SIZE (buffer));
163 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
164 GST_BUFFER_MAXSIZE (copy) = GST_BUFFER_SIZE (buffer);
166 gst_buffer_stamp (copy, buffer);
167 GST_BUFFER_FREE_DATA_FUNC (copy) = NULL;
168 GST_BUFFER_PRIVATE (copy) = NULL;
174 gst_buffer_alloc_chunk (void)
178 newbuf = gst_mem_chunk_alloc (chunk);
179 #ifndef GST_DISABLE_TRACE
180 gst_alloc_trace_new (_gst_buffer_trace, newbuf);
187 gst_buffer_free_chunk (GstBuffer * buffer)
189 gst_mem_chunk_free (chunk, GST_DATA (buffer));
190 #ifndef GST_DISABLE_TRACE
191 gst_alloc_trace_free (_gst_buffer_trace, buffer);
198 * Creates a newly allocated buffer without any data.
200 * Returns: the new #GstBuffer.
203 gst_buffer_new (void)
207 newbuf = gst_buffer_alloc_chunk ();
209 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
211 _GST_DATA_INIT (GST_DATA (newbuf),
214 (GstDataFreeFunction) gst_buffer_default_free,
215 (GstDataCopyFunction) gst_buffer_default_copy);
217 GST_BUFFER_DATA (newbuf) = NULL;
218 GST_BUFFER_SIZE (newbuf) = 0;
219 GST_BUFFER_MAXSIZE (newbuf) = GST_BUFFER_MAXSIZE_NONE;
220 GST_BUFFER_TIMESTAMP (newbuf) = GST_CLOCK_TIME_NONE;
221 GST_BUFFER_DURATION (newbuf) = GST_CLOCK_TIME_NONE;
222 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET_NONE;
223 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_NONE;
224 GST_BUFFER_FREE_DATA_FUNC (newbuf) = NULL;
225 GST_BUFFER_PRIVATE (newbuf) = NULL;
231 * gst_buffer_new_and_alloc:
232 * @size: the size of the new buffer's data.
234 * Creates a newly allocated buffer with data of the given size.
236 * Returns: the new #GstBuffer.
239 gst_buffer_new_and_alloc (guint size)
243 newbuf = gst_buffer_new ();
245 GST_BUFFER_DATA (newbuf) = g_malloc (size);
246 GST_BUFFER_SIZE (newbuf) = size;
247 GST_BUFFER_MAXSIZE (newbuf) = size;
253 * gst_buffer_create_sub:
254 * @parent: a parent #GstBuffer to create a subbuffer from.
255 * @offset: the offset into parent #GstBuffer.
256 * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
258 * Creates a sub-buffer from the parent at a given offset.
259 * This sub-buffer uses the actual memory space of the parent buffer.
260 * This function will copy the offset and timestamp field when the
261 * offset is 0, else they are set to _NONE.
262 * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
264 * Returns: the new #GstBuffer, or NULL if there was an error.
267 gst_buffer_create_sub (GstBuffer * parent, guint offset, guint size)
270 gpointer buffer_data;
272 g_return_val_if_fail (parent != NULL, NULL);
273 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
274 g_return_val_if_fail (size > 0, NULL);
275 g_return_val_if_fail (parent->size >= offset + size, NULL);
277 /* remember the data for the new buffer */
278 buffer_data = parent->data + offset;
279 /* make sure we're child not child from a child buffer */
280 while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
281 parent = GST_BUFFER (parent->buffer_private);
283 /* ref the real parent */
284 gst_data_ref (GST_DATA (parent));
286 /* create the new buffer */
287 buffer = gst_buffer_alloc_chunk ();
289 GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", buffer, parent);
291 /* make sure nobody overwrites data in the new buffer
292 * by setting the READONLY flag */
293 _GST_DATA_INIT (GST_DATA (buffer),
295 GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
296 GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
297 (GstDataFreeFunction) _gst_buffer_sub_free,
298 (GstDataCopyFunction) gst_buffer_default_copy);
300 /* set the right values in the child */
301 GST_BUFFER_DATA (buffer) = buffer_data;
302 GST_BUFFER_SIZE (buffer) = size;
303 GST_BUFFER_MAXSIZE (buffer) = size;
304 GST_BUFFER_FREE_DATA_FUNC (buffer) = NULL;
305 GST_BUFFER_PRIVATE (buffer) = parent;
306 /* we can copy the timestamp and offset if the new buffer starts at
309 GST_BUFFER_TIMESTAMP (buffer) = GST_BUFFER_TIMESTAMP (parent);
310 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET (parent);
312 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
313 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
316 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
317 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
319 if (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_DONTKEEP)) {
320 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_DONTKEEP);
322 if (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_READONLY)) {
323 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_READONLY);
332 * @buf1: a first source #GstBuffer to merge.
333 * @buf2: the second source #GstBuffer to merge.
335 * Create a new buffer that is the concatenation of the two source
336 * buffers. The original source buffers will not be modified or
339 * WARNING: Incorrect use of this function can lead to memory leaks.
340 * It is recommended to use gst_buffer_join() instead of this function.
342 * If the buffers point to contiguous areas of memory, the buffer
343 * is created without copying the data.
345 * Returns: the new #GstBuffer that's the concatenation of the source buffers.
348 gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2)
352 /* we're just a specific case of the more general gst_buffer_span() */
353 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
360 * @buf1: a first source #GstBuffer to merge.
361 * @buf2: the second source #GstBuffer to merge.
363 * Create a new buffer that is the concatenation of the two source
364 * buffers. The original buffers are unreferenced.
366 * If the buffers point to contiguous areas of memory, the buffer
367 * is created without copying the data.
369 * Returns: the new #GstBuffer that's the concatenation of the source buffers.
372 gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2)
376 /* we're just a specific case of the more general gst_buffer_span() */
377 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
379 gst_buffer_unref (buf1);
380 gst_buffer_unref (buf2);
386 * gst_buffer_is_span_fast:
387 * @buf1: a first source #GstBuffer.
388 * @buf2: the second source #GstBuffer.
390 * Determines whether a gst_buffer_span() can be done without copying
391 * the contents, that is, whether the data areas are contiguous.
393 * Returns: TRUE if the buffers are contiguous,
394 * FALSE if a copy would be required.
397 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
399 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
400 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
401 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
403 /* it's only fast if we have subbuffers of the same parent */
404 return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
405 (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
406 (buf1->buffer_private == buf2->buffer_private) &&
407 ((buf1->data + buf1->size) == buf2->data));
412 * @buf1: a first source #GstBuffer to merge.
413 * @offset: the offset in the first buffer from where the new
414 * buffer should start.
415 * @buf2: the second source #GstBuffer to merge.
416 * @len: the total length of the new buffer.
418 * Creates a new buffer that consists of part of buf1 and buf2.
419 * Logically, buf1 and buf2 are concatenated into a single larger
420 * buffer, and a new buffer is created at the given offset inside
421 * this space, with a given length.
423 * If the two source buffers are children of the same larger buffer,
424 * and are contiguous, the new buffer will be a child of the shared
425 * parent, and thus no copying is necessary. you can use
426 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
428 * Returns: the new #GstBuffer that spans the two source buffers.
431 gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
436 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
437 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
438 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
439 g_return_val_if_fail (len > 0, NULL);
440 g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
442 /* if the two buffers have the same parent and are adjacent */
443 if (gst_buffer_is_span_fast (buf1, buf2)) {
444 GstBuffer *parent = GST_BUFFER (buf1->buffer_private);
446 /* we simply create a subbuffer of the common parent */
447 newbuf = gst_buffer_create_sub (parent,
448 buf1->data - parent->data + offset, len);
450 GST_CAT_DEBUG (GST_CAT_BUFFER,
451 "slow path taken while spanning buffers %p and %p", buf1, buf2);
452 /* otherwise we simply have to brute-force copy the buffers */
453 newbuf = gst_buffer_new_and_alloc (len);
455 /* copy the first buffer's data across */
456 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
457 /* copy the second buffer's data across */
458 memcpy (newbuf->data + (buf1->size - offset), buf2->data,
459 len - (buf1->size - offset));
460 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
462 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
463 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
466 /* if we completely merged the two buffers (appended), we can
467 * calculate the duration too. Also make sure we's not messing with
468 * invalid DURATIONS */
469 if (offset == 0 && buf1->size + buf2->size == len) {
470 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
471 GST_BUFFER_DURATION_IS_VALID (buf2)) {
473 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
474 GST_BUFFER_DURATION (buf2);
476 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
478 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);