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"
30 #include "gstbufferpool-default.h"
32 GType _gst_buffer_type;
33 GType _gst_buffer_pool_type;
35 #ifndef GST_DISABLE_TRACE
36 /* #define GST_WITH_ALLOC_TRACE */
39 static GstAllocTrace *_gst_buffer_trace;
40 static GstAllocTrace *_gst_buffer_pool_trace;
43 static GstMemChunk *chunk;
46 _gst_buffer_initialize (void)
48 _gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
49 (GBoxedCopyFunc) gst_data_ref,
50 (GBoxedFreeFunc) gst_data_unref);
52 _gst_buffer_pool_type = g_boxed_type_register_static ("GstBufferPool",
53 (GBoxedCopyFunc) gst_data_ref,
54 (GBoxedFreeFunc) gst_data_unref);
56 #ifndef GST_DISABLE_TRACE
57 _gst_buffer_trace = gst_alloc_trace_register (GST_BUFFER_TRACE_NAME);
58 _gst_buffer_pool_trace = gst_alloc_trace_register (GST_BUFFER_POOL_TRACE_NAME);
61 chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer),
62 sizeof (GstBuffer) * 200, 0);
64 GST_CAT_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
68 gst_buffer_get_type (void)
70 return _gst_buffer_type;
74 _gst_buffer_free_to_pool (GstBuffer *buffer)
76 GstBufferPool *pool = buffer->pool;
78 pool->buffer_free (pool, buffer, pool->user_data);
80 gst_data_unref (GST_DATA (pool));
84 _gst_buffer_sub_free (GstBuffer *buffer)
86 gst_data_unref (GST_DATA (buffer->pool_private));
88 GST_BUFFER_DATA (buffer) = NULL;
89 GST_BUFFER_SIZE (buffer) = 0;
91 _GST_DATA_DISPOSE (GST_DATA (buffer));
93 gst_mem_chunk_free (chunk, GST_DATA (buffer));
94 #ifndef GST_DISABLE_TRACE
95 gst_alloc_trace_free (_gst_buffer_trace, buffer);
100 * gst_buffer_default_free:
101 * @buffer: a #GstBuffer to free.
103 * Frees the memory associated with the buffer including the buffer data,
104 * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
105 * This function is used by buffer pools.
108 gst_buffer_default_free (GstBuffer *buffer)
110 g_return_if_fail (buffer != NULL);
113 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer))
114 g_free (GST_BUFFER_DATA (buffer));
116 /* set to safe values */
117 GST_BUFFER_DATA (buffer) = NULL;
118 GST_BUFFER_SIZE (buffer) = 0;
120 _GST_DATA_DISPOSE (GST_DATA (buffer));
122 gst_mem_chunk_free (chunk, GST_DATA (buffer));
123 #ifndef GST_DISABLE_TRACE
124 gst_alloc_trace_free (_gst_buffer_trace, buffer);
129 _gst_buffer_copy_from_pool (GstBuffer *buffer)
131 return buffer->pool->buffer_copy (buffer->pool, buffer, buffer->pool->user_data);
135 * gst_buffer_default_copy:
136 * @buffer: a #GstBuffer to make a copy of.
138 * Make a full newly allocated copy of the given buffer, data and all.
139 * This function is used by buffer pools.
141 * Returns: the new #GstBuffer.
144 gst_buffer_default_copy (GstBuffer *buffer)
148 g_return_val_if_fail (buffer != NULL, NULL);
150 /* create a fresh new buffer */
151 copy = gst_mem_chunk_alloc (chunk);
152 #ifndef GST_DISABLE_TRACE
153 gst_alloc_trace_new (_gst_buffer_trace, copy);
156 _GST_DATA_INIT (GST_DATA (copy),
159 (GstDataFreeFunction) gst_buffer_default_free,
160 (GstDataCopyFunction) gst_buffer_default_copy);
162 /* we simply copy everything from our parent */
163 GST_BUFFER_DATA (copy) = g_memdup (GST_BUFFER_DATA (buffer),
164 GST_BUFFER_SIZE (buffer));
165 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
166 GST_BUFFER_MAXSIZE (copy) = GST_BUFFER_SIZE (buffer);
167 GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
168 GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
169 GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
170 GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
171 GST_BUFFER_BUFFERPOOL (copy) = NULL;
172 GST_BUFFER_POOL_PRIVATE (copy) = NULL;
180 * Creates a newly allocated buffer without any data.
182 * Returns: the new #GstBuffer.
185 gst_buffer_new (void)
189 newbuf = gst_mem_chunk_alloc (chunk);
190 #ifndef GST_DISABLE_TRACE
191 gst_alloc_trace_new (_gst_buffer_trace, newbuf);
194 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
196 _GST_DATA_INIT (GST_DATA (newbuf),
199 (GstDataFreeFunction) gst_buffer_default_free,
200 (GstDataCopyFunction) gst_buffer_default_copy);
202 GST_BUFFER_DATA (newbuf) = NULL;
203 GST_BUFFER_SIZE (newbuf) = 0;
204 GST_BUFFER_MAXSIZE (newbuf) = GST_BUFFER_MAXSIZE_NONE;
205 GST_BUFFER_TIMESTAMP (newbuf) = GST_CLOCK_TIME_NONE;
206 GST_BUFFER_DURATION (newbuf) = GST_CLOCK_TIME_NONE;
207 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET_NONE;
208 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_NONE;
209 GST_BUFFER_BUFFERPOOL (newbuf) = NULL;
210 GST_BUFFER_POOL_PRIVATE (newbuf) = NULL;
216 * gst_buffer_new_and_alloc:
217 * @size: the size of the new buffer's data.
219 * Creates a newly allocated buffer with data of the given size.
221 * Returns: the new #GstBuffer.
224 gst_buffer_new_and_alloc (guint size)
228 newbuf = gst_buffer_new ();
230 GST_BUFFER_DATA (newbuf) = g_malloc (size);
231 GST_BUFFER_SIZE (newbuf) = size;
232 GST_BUFFER_MAXSIZE (newbuf) = size;
238 * gst_buffer_new_from_pool:
239 * @pool: a #GstBufferPool to use.
240 * @offset: the offset of the new buffer.
241 * @size: the size of the new buffer.
243 * Creates a newly allocated buffer using the specified buffer pool,
246 * Returns: the new #GstBuffer, or NULL if there was an error.
249 gst_buffer_new_from_pool (GstBufferPool *pool,
250 guint64 offset, guint size)
254 g_return_val_if_fail (pool != NULL, NULL);
256 gst_data_ref (GST_DATA (pool));
258 buffer = pool->buffer_new (pool, offset, size, pool->user_data);
262 GST_BUFFER_BUFFERPOOL (buffer) = pool;
264 /* override the buffer refcount functions with those from the pool (if any) */
265 if (pool->buffer_free)
266 GST_DATA (buffer)->free = (GstDataFreeFunction)_gst_buffer_free_to_pool;
267 if (pool->buffer_copy)
268 GST_DATA (buffer)->copy = (GstDataCopyFunction)_gst_buffer_copy_from_pool;
274 * gst_buffer_create_sub:
275 * @parent: a parent #GstBuffer to create a subbuffer from.
276 * @offset: the offset into parent #GstBuffer.
277 * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
279 * Creates a sub-buffer from the parent at a given offset.
280 * This sub-buffer uses the actual memory space of the parent buffer.
281 * This function will copy the offset and timestamp field when the
282 * offset is 0, else they are set to _NONE.
283 * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
285 * Returns: the new #GstBuffer, or NULL if there was an error.
288 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
291 gpointer buffer_data;
293 g_return_val_if_fail (parent != NULL, NULL);
294 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
295 g_return_val_if_fail (size > 0, NULL);
296 g_return_val_if_fail (parent->size >= offset + size, NULL);
298 /* remember the data for the new buffer */
299 buffer_data = parent->data + offset;
300 /* make sure we're child not child from a child buffer */
301 while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
302 parent = GST_BUFFER (parent->pool_private);
304 /* ref the real parent */
305 gst_data_ref (GST_DATA (parent));
307 /* create the new buffer */
308 buffer = gst_mem_chunk_alloc (chunk);
309 #ifndef GST_DISABLE_TRACE
310 gst_alloc_trace_new (_gst_buffer_trace, buffer);
313 GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p", buffer);
315 /* make sure nobody overwrites data in the new buffer
316 * by setting the READONLY flag */
317 _GST_DATA_INIT (GST_DATA (buffer),
319 GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
320 GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
321 (GstDataFreeFunction) _gst_buffer_sub_free,
322 (GstDataCopyFunction) gst_buffer_default_copy);
324 /* set the right values in the child */
325 GST_BUFFER_DATA (buffer) = buffer_data;
326 GST_BUFFER_SIZE (buffer) = size;
327 GST_BUFFER_MAXSIZE (buffer) = size;
328 GST_BUFFER_BUFFERPOOL (buffer) = NULL;
329 GST_BUFFER_POOL_PRIVATE (buffer) = parent;
330 /* we can copy the timestamp and offset if the new buffer starts at
333 GST_BUFFER_TIMESTAMP (buffer) = GST_BUFFER_TIMESTAMP (parent);
334 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET (parent);
337 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
338 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
341 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
342 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
343 /* make sure nobody overwrites data as it would overwrite in the parent.
344 * data in parent cannot be overwritten because we hold a ref */
345 GST_DATA_FLAG_SET (parent, GST_DATA_READONLY);
353 * @buf1: a first source #GstBuffer to merge.
354 * @buf2: the second source #GstBuffer to merge.
356 * Create a new buffer that is the concatenation of the two source
357 * buffers. The original source buffers will not be modified or
360 * Internally is nothing more than a specialized gst_buffer_span(),
361 * so the same optimizations can occur.
363 * Returns: the new #GstBuffer that's the concatenation of the source buffers.
366 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
370 /* we're just a specific case of the more general gst_buffer_span() */
371 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
377 * gst_buffer_is_span_fast:
378 * @buf1: a first source #GstBuffer.
379 * @buf2: the second source #GstBuffer.
381 * Determines whether a gst_buffer_span() is free (as in free beer),
382 * or requires a memcpy.
384 * Returns: TRUE if the buffers are contiguous,
385 * FALSE if a copy would be required.
388 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
390 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
391 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
392 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
394 /* it's only fast if we have subbuffers of the same parent */
395 return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
396 (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
397 (buf1->pool_private == buf2->pool_private) &&
398 ((buf1->data + buf1->size) == buf2->data));
403 * @buf1: a first source #GstBuffer to merge.
404 * @offset: the offset in the first buffer from where the new
405 * buffer should start.
406 * @buf2: the second source #GstBuffer to merge.
407 * @len: the total length of the new buffer.
409 * Creates a new buffer that consists of part of buf1 and buf2.
410 * Logically, buf1 and buf2 are concatenated into a single larger
411 * buffer, and a new buffer is created at the given offset inside
412 * this space, with a given length.
414 * If the two source buffers are children of the same larger buffer,
415 * and are contiguous, the new buffer will be a child of the shared
416 * parent, and thus no copying is necessary. you can use
417 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
419 * Returns: the new #GstBuffer that spans the two source buffers.
422 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
426 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
427 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
428 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
429 g_return_val_if_fail (len > 0, NULL);
430 g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
432 /* if the two buffers have the same parent and are adjacent */
433 if (gst_buffer_is_span_fast (buf1, buf2)) {
434 GstBuffer *parent = GST_BUFFER (buf1->pool_private);
435 /* we simply create a subbuffer of the common parent */
436 newbuf = gst_buffer_create_sub (parent,
437 buf1->data - parent->data + offset, len);
440 GST_CAT_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p",
442 /* otherwise we simply have to brute-force copy the buffers */
443 newbuf = gst_buffer_new_and_alloc (len);
445 /* copy the first buffer's data across */
446 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
447 /* copy the second buffer's data across */
448 memcpy (newbuf->data + (buf1->size - offset), buf2->data,
449 len - (buf1->size - offset));
450 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
452 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
453 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
456 /* if we completely merged the two buffers (appended), we can
457 * calculate the duration too. Also make sure we's not messing with
458 * invalid DURATIONS */
459 if (offset == 0 && buf1->size + buf2->size == len) {
460 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
461 GST_BUFFER_DURATION_IS_VALID (buf2)) {
463 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
464 GST_BUFFER_DURATION (buf2);
466 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
468 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
476 gst_buffer_pool_get_type (void)
478 return _gst_buffer_pool_type;
482 * gst_buffer_pool_default_free:
483 * @pool: a #GstBufferPool to free.
485 * Frees the memory associated with the bufferpool.
488 gst_buffer_pool_default_free (GstBufferPool *pool)
490 g_return_if_fail (pool != NULL);
492 _GST_DATA_DISPOSE (GST_DATA (pool));
494 #ifndef GST_DISABLE_TRACE
495 gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
500 * gst_buffer_pool_new:
501 * @free: the #GstDataFreeFunction to free the buffer pool.
502 * @copy: the #GstDataCopyFunction to copy the buffer pool.
503 * @buffer_new: the #GstBufferPoolBufferNewFunction to create a new buffer
505 * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
507 * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer
509 * @user_data: the user data gpointer passed to buffer_* functions.
511 * Creates a new buffer pool with the given functions.
513 * Returns: a new #GstBufferPool, or NULL on error.
516 gst_buffer_pool_new (GstDataFreeFunction free,
517 GstDataCopyFunction copy,
518 GstBufferPoolBufferNewFunction buffer_new,
519 GstBufferPoolBufferCopyFunction buffer_copy,
520 GstBufferPoolBufferFreeFunction buffer_free,
525 /* we need at least a buffer_new function */
526 g_return_val_if_fail (buffer_new != NULL, NULL);
528 pool = g_new0 (GstBufferPool, 1);
529 #ifndef GST_DISABLE_TRACE
530 gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
533 GST_CAT_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
535 /* init data struct */
536 _GST_DATA_INIT (GST_DATA (pool),
537 _gst_buffer_pool_type,
539 (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
543 pool->buffer_new = buffer_new;
544 pool->buffer_copy = buffer_copy;
545 pool->buffer_free = buffer_free;
546 pool->user_data = user_data;
552 * gst_buffer_pool_is_active:
553 * @pool: the #GstBufferPool to query.
555 * Queries if the given buffer pool is active.
557 * Returns: TRUE if the pool is active.
560 gst_buffer_pool_is_active (GstBufferPool *pool)
562 g_return_val_if_fail (pool != NULL, FALSE);
568 * gst_buffer_pool_set_active:
569 * @pool: a #GstBufferPool to set the activity status on.
570 * @active: the new status of the pool.
572 * Sets the given pool to the active or inactive state depending on the
576 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
578 g_return_if_fail (pool != NULL);
580 pool->active = active;
584 * gst_buffer_pool_set_user_data:
585 * @pool: the #GstBufferPool to set the user data for.
586 * @user_data: the user_data to set on the buffer pool.
588 * Sets the given user data on the buffer pool.
591 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
593 g_return_if_fail (pool != NULL);
595 pool->user_data = user_data;
599 * gst_buffer_pool_get_user_data:
600 * @pool: the #GstBufferPool to get the user data for.
602 * Gets the user data of the buffer pool.
604 * Returns: the user data associated with this buffer pool.
607 gst_buffer_pool_get_user_data (GstBufferPool *pool)
609 g_return_val_if_fail (pool != NULL, NULL);
611 return pool->user_data;