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 /* this file makes too much noise for most debugging sessions */
24 #define GST_DEBUG_FORCE_DISABLE
25 #include "gst_private.h"
27 #include "gstatomic_impl.h"
28 #include "gstdata_private.h"
29 #include "gstbuffer.h"
30 #include "gstmemchunk.h"
32 #include "gstbufferpool-default.h"
34 GType _gst_buffer_type;
35 GType _gst_buffer_pool_type;
37 #ifndef GST_DISABLE_TRACE
38 /* #define GST_WITH_ALLOC_TRACE */
41 static GstAllocTrace *_gst_buffer_trace;
42 static GstAllocTrace *_gst_buffer_pool_trace;
45 static GstMemChunk *chunk;
48 _gst_buffer_initialize (void)
50 _gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
51 (GBoxedCopyFunc) gst_data_ref,
52 (GBoxedFreeFunc) gst_data_unref);
54 _gst_buffer_pool_type = g_boxed_type_register_static ("GstBufferPool",
55 (GBoxedCopyFunc) gst_data_ref,
56 (GBoxedFreeFunc) gst_data_unref);
58 #ifndef GST_DISABLE_TRACE
59 _gst_buffer_trace = gst_alloc_trace_register (GST_BUFFER_TRACE_NAME);
60 _gst_buffer_pool_trace = gst_alloc_trace_register (GST_BUFFER_POOL_TRACE_NAME);
63 chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer),
64 sizeof (GstBuffer) * 200, 0);
66 GST_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
70 gst_buffer_get_type (void)
72 return _gst_buffer_type;
76 _gst_buffer_free_to_pool (GstBuffer *buffer)
78 GstBufferPool *pool = buffer->pool;
80 pool->buffer_free (pool, buffer, pool->user_data);
82 gst_data_unref (GST_DATA (pool));
86 _gst_buffer_sub_free (GstBuffer *buffer)
88 gst_data_unref (GST_DATA (buffer->pool_private));
90 GST_BUFFER_DATA (buffer) = NULL;
91 GST_BUFFER_SIZE (buffer) = 0;
93 _GST_DATA_DISPOSE (GST_DATA (buffer));
95 gst_mem_chunk_free (chunk, GST_DATA (buffer));
96 #ifndef GST_DISABLE_TRACE
97 gst_alloc_trace_free (_gst_buffer_trace, buffer);
102 * gst_buffer_default_free:
103 * @buffer: a #GstBuffer to free.
105 * Frees the memory associated with the buffer including the buffer data,
106 * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
107 * This function is used by buffer pools.
110 gst_buffer_default_free (GstBuffer *buffer)
112 g_return_if_fail (buffer != NULL);
115 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer))
116 g_free (GST_BUFFER_DATA (buffer));
118 /* set to safe values */
119 GST_BUFFER_DATA (buffer) = NULL;
120 GST_BUFFER_SIZE (buffer) = 0;
122 _GST_DATA_DISPOSE (GST_DATA (buffer));
124 gst_mem_chunk_free (chunk, GST_DATA (buffer));
125 #ifndef GST_DISABLE_TRACE
126 gst_alloc_trace_free (_gst_buffer_trace, buffer);
131 _gst_buffer_copy_from_pool (GstBuffer *buffer)
133 return buffer->pool->buffer_copy (buffer->pool, buffer, buffer->pool->user_data);
137 * gst_buffer_default_copy:
138 * @buffer: a #GstBuffer to make a copy of.
140 * Make a full newly allocated copy of the given buffer, data and all.
141 * This function is used by buffer pools.
143 * Returns: the new #GstBuffer.
146 gst_buffer_default_copy (GstBuffer *buffer)
150 g_return_val_if_fail (buffer != NULL, NULL);
152 /* create a fresh new buffer */
153 copy = gst_buffer_new ();
155 /* we simply copy everything from our parent */
156 GST_BUFFER_DATA (copy) = g_memdup (GST_BUFFER_DATA (buffer),
157 GST_BUFFER_SIZE (buffer));
158 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
159 GST_BUFFER_MAXSIZE (copy) = GST_BUFFER_MAXSIZE (buffer);
160 GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
161 GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
169 * Creates a newly allocated buffer without any data.
171 * Returns: the new #GstBuffer.
174 gst_buffer_new (void)
178 buf = gst_mem_chunk_alloc0 (chunk);
179 #ifndef GST_DISABLE_TRACE
180 gst_alloc_trace_new (_gst_buffer_trace, buf);
183 GST_DEBUG (GST_CAT_BUFFER, "new %p", buf);
185 _GST_DATA_INIT (GST_DATA (buf),
188 (GstDataFreeFunction) gst_buffer_default_free,
189 (GstDataCopyFunction) gst_buffer_default_copy);
191 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
197 * gst_buffer_new_and_alloc:
198 * @size: the size of the new buffer's data.
200 * Creates a newly allocated buffer with data of the given size.
202 * Returns: the new #GstBuffer.
205 gst_buffer_new_and_alloc (guint size)
209 new = gst_buffer_new ();
211 GST_BUFFER_DATA (new) = g_malloc (size);
212 GST_BUFFER_SIZE (new) = size;
218 * gst_buffer_new_from_pool:
219 * @pool: a #GstBufferPool to use.
220 * @offset: the offset of the new buffer.
221 * @size: the size of the new buffer.
223 * Creates a newly allocated buffer using the specified buffer pool,
226 * Returns: the new #GstBuffer, or NULL if there was an error.
229 gst_buffer_new_from_pool (GstBufferPool *pool,
230 guint64 offset, guint size)
234 g_return_val_if_fail (pool != NULL, NULL);
236 buffer = pool->buffer_new (pool, offset, size, pool->user_data);
240 GST_BUFFER_BUFFERPOOL (buffer) = pool;
241 gst_data_ref (GST_DATA (pool));
243 /* override the buffer refcount functions with those from the pool (if any) */
244 if (pool->buffer_free)
245 GST_DATA (buffer)->free = (GstDataFreeFunction)_gst_buffer_free_to_pool;
246 if (pool->buffer_copy)
247 GST_DATA (buffer)->copy = (GstDataCopyFunction)_gst_buffer_copy_from_pool;
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.
261 * Returns: the new #GstBuffer, or NULL if there was an error.
264 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
267 gpointer buffer_data;
268 guint64 parent_offset;
270 g_return_val_if_fail (parent != NULL, NULL);
271 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
272 g_return_val_if_fail (size > 0, NULL);
273 g_return_val_if_fail (parent->size >= offset + size, NULL);
275 /* remember the data for the new buffer */
276 buffer_data = parent->data + offset;
277 parent_offset = GST_BUFFER_OFFSET (parent);
278 /* make sure we're child not child from a child buffer */
279 while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
280 parent = GST_BUFFER (parent->pool_private);
282 /* ref the real parent */
283 gst_data_ref (GST_DATA (parent));
284 /* make sure nobody overwrites data in the parent */
285 GST_DATA_FLAG_SET (parent, GST_DATA_READONLY);
287 /* create the new buffer */
288 buffer = gst_mem_chunk_alloc0 (chunk);
289 #ifndef GST_DISABLE_TRACE
290 gst_alloc_trace_new (_gst_buffer_trace, buffer);
293 GST_DEBUG (GST_CAT_BUFFER, "new %p", buf);
295 /* make sure nobody overwrites data in the new buffer
296 * by setting the READONLY flag */
297 _GST_DATA_INIT (GST_DATA (buffer),
299 GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
300 GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
301 (GstDataFreeFunction) _gst_buffer_sub_free,
302 (GstDataCopyFunction) gst_buffer_default_copy);
304 GST_BUFFER_OFFSET (buffer) = parent_offset + offset;
305 GST_BUFFER_TIMESTAMP (buffer) = -1;
306 GST_BUFFER_BUFFERPOOL (buffer) = NULL;
307 GST_BUFFER_POOL_PRIVATE (buffer) = parent;
309 /* set the right values in the child */
310 buffer->data = buffer_data;
319 * @buf1: a first source #GstBuffer to merge.
320 * @buf2: the second source #GstBuffer to merge.
322 * Create a new buffer that is the concatenation of the two source
323 * buffers. The original source buffers will not be modified or
326 * Internally is nothing more than a specialized gst_buffer_span(),
327 * so the same optimizations can occur.
329 * Returns: the new #GstBuffer that's the concatenation of the source buffers.
332 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
336 /* we're just a specific case of the more general gst_buffer_span() */
337 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
343 * gst_buffer_is_span_fast:
344 * @buf1: a first source #GstBuffer.
345 * @buf2: the second source #GstBuffer.
347 * Determines whether a gst_buffer_span() is free (as in free beer),
348 * or requires a memcpy.
350 * Returns: TRUE if the buffers are contiguous,
351 * FALSE if a copy would be required.
354 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
356 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
357 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
358 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
360 /* it's only fast if we have subbuffers of the same parent */
361 return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
362 (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
363 (buf1->pool_private == buf2->pool_private) &&
364 ((buf1->data + buf1->size) == buf2->data));
369 * @buf1: a first source #GstBuffer to merge.
370 * @offset: the offset in the first buffer from where the new
371 * buffer should start.
372 * @buf2: the second source #GstBuffer to merge.
373 * @len: the total length of the new buffer.
375 * Creates a new buffer that consists of part of buf1 and buf2.
376 * Logically, buf1 and buf2 are concatenated into a single larger
377 * buffer, and a new buffer is created at the given offset inside
378 * this space, with a given length.
380 * If the two source buffers are children of the same larger buffer,
381 * and are contiguous, the new buffer will be a child of the shared
382 * parent, and thus no copying is necessary. you can use
383 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
385 * Returns: the new #GstBuffer that spans the two source buffers.
388 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
392 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
393 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
394 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
395 g_return_val_if_fail (len > 0, NULL);
397 /* if the two buffers have the same parent and are adjacent */
398 if (gst_buffer_is_span_fast (buf1, buf2)) {
399 GstBuffer *parent = GST_BUFFER (buf1->pool_private);
400 /* we simply create a subbuffer of the common parent */
401 newbuf = gst_buffer_create_sub (parent,
402 buf1->data - parent->data + offset, len);
405 GST_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p",
407 /* otherwise we simply have to brute-force copy the buffers */
408 newbuf = gst_buffer_new_and_alloc (len);
410 /* copy relevant stuff from data struct of buffer1 */
411 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1) + offset;
413 /* copy the first buffer's data across */
414 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
415 /* copy the second buffer's data across */
416 memcpy (newbuf->data + (buf1->size - offset), buf2->data,
417 len - (buf1->size - offset));
419 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
421 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
427 gst_buffer_pool_get_type (void)
429 return _gst_buffer_pool_type;
433 * gst_buffer_pool_default_free:
434 * @pool: a #GstBufferPool to free.
436 * Frees the memory associated with the bufferpool.
439 gst_buffer_pool_default_free (GstBufferPool *pool)
441 g_return_if_fail (pool != NULL);
443 _GST_DATA_DISPOSE (GST_DATA (pool));
445 #ifndef GST_DISABLE_TRACE
446 gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
451 * gst_buffer_pool_new:
452 * @free: the #GstDataFreeFunction to free the buffer pool.
453 * @copy: the #GstDataCopyFunction to copy the buffer pool.
454 * @buffer_new: the #GstBufferPoolBufferNewFunction to create a new buffer
456 * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
458 * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer
460 * @user_data: the user data gpointer passed to buffer_* functions.
462 * Creates a new buffer pool with the given functions.
464 * Returns: a new #GstBufferPool, or NULL on error.
467 gst_buffer_pool_new (GstDataFreeFunction free,
468 GstDataCopyFunction copy,
469 GstBufferPoolBufferNewFunction buffer_new,
470 GstBufferPoolBufferCopyFunction buffer_copy,
471 GstBufferPoolBufferFreeFunction buffer_free,
476 /* we need at least a buffer_new function */
477 g_return_val_if_fail (buffer_new != NULL, NULL);
479 pool = g_new0 (GstBufferPool, 1);
480 #ifndef GST_DISABLE_TRACE
481 gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
484 GST_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
486 /* init data struct */
487 _GST_DATA_INIT (GST_DATA (pool),
488 _gst_buffer_pool_type,
490 (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
494 pool->buffer_new = buffer_new;
495 pool->buffer_copy = buffer_copy;
496 pool->buffer_free = buffer_free;
497 pool->user_data = user_data;
503 * gst_buffer_pool_is_active:
504 * @pool: the #GstBufferPool to query.
506 * Queries if the given buffer pool is active.
508 * Returns: TRUE if the pool is active.
511 gst_buffer_pool_is_active (GstBufferPool *pool)
513 g_return_val_if_fail (pool != NULL, FALSE);
519 * gst_buffer_pool_set_active:
520 * @pool: a #GstBufferPool to set the activity status on.
521 * @active: the new status of the pool.
523 * Sets the given pool to the active or inactive state depending on the
527 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
529 g_return_if_fail (pool != NULL);
531 pool->active = active;
535 * gst_buffer_pool_set_user_data:
536 * @pool: the #GstBufferPool to set the user data for.
537 * @user_data: the user_data to set on the buffer pool.
539 * Sets the given user data on the buffer pool.
542 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
544 g_return_if_fail (pool != NULL);
546 pool->user_data = user_data;
550 * gst_buffer_pool_get_user_data:
551 * @pool: the #GstBufferPool to get the user data for.
553 * Gets the user data of the buffer pool.
555 * Returns: the user data associated with this buffer pool.
558 gst_buffer_pool_get_user_data (GstBufferPool *pool)
560 g_return_val_if_fail (pool != NULL, NULL);
562 return pool->user_data;