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_BUFFERPOOL (copy) = NULL;
171 GST_BUFFER_POOL_PRIVATE (copy) = NULL;
179 * Creates a newly allocated buffer without any data.
181 * Returns: the new #GstBuffer.
184 gst_buffer_new (void)
188 newbuf = gst_mem_chunk_alloc (chunk);
189 #ifndef GST_DISABLE_TRACE
190 gst_alloc_trace_new (_gst_buffer_trace, newbuf);
193 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
195 _GST_DATA_INIT (GST_DATA (newbuf),
198 (GstDataFreeFunction) gst_buffer_default_free,
199 (GstDataCopyFunction) gst_buffer_default_copy);
201 GST_BUFFER_DATA (newbuf) = NULL;
202 GST_BUFFER_SIZE (newbuf) = 0;
203 GST_BUFFER_MAXSIZE (newbuf) = GST_BUFFER_MAXSIZE_NONE;
204 GST_BUFFER_TIMESTAMP (newbuf) = GST_CLOCK_TIME_NONE;
205 GST_BUFFER_DURATION (newbuf) = GST_CLOCK_TIME_NONE;
206 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET_NONE;
207 GST_BUFFER_BUFFERPOOL (newbuf) = NULL;
208 GST_BUFFER_POOL_PRIVATE (newbuf) = NULL;
214 * gst_buffer_new_and_alloc:
215 * @size: the size of the new buffer's data.
217 * Creates a newly allocated buffer with data of the given size.
219 * Returns: the new #GstBuffer.
222 gst_buffer_new_and_alloc (guint size)
226 newbuf = gst_buffer_new ();
228 GST_BUFFER_DATA (newbuf) = g_malloc (size);
229 GST_BUFFER_SIZE (newbuf) = size;
230 GST_BUFFER_MAXSIZE (newbuf) = size;
236 * gst_buffer_new_from_pool:
237 * @pool: a #GstBufferPool to use.
238 * @offset: the offset of the new buffer.
239 * @size: the size of the new buffer.
241 * Creates a newly allocated buffer using the specified buffer pool,
244 * Returns: the new #GstBuffer, or NULL if there was an error.
247 gst_buffer_new_from_pool (GstBufferPool *pool,
248 guint64 offset, guint size)
252 g_return_val_if_fail (pool != NULL, NULL);
254 gst_data_ref (GST_DATA (pool));
256 buffer = pool->buffer_new (pool, offset, size, pool->user_data);
260 GST_BUFFER_BUFFERPOOL (buffer) = pool;
262 /* override the buffer refcount functions with those from the pool (if any) */
263 if (pool->buffer_free)
264 GST_DATA (buffer)->free = (GstDataFreeFunction)_gst_buffer_free_to_pool;
265 if (pool->buffer_copy)
266 GST_DATA (buffer)->copy = (GstDataCopyFunction)_gst_buffer_copy_from_pool;
272 * gst_buffer_create_sub:
273 * @parent: a parent #GstBuffer to create a subbuffer from.
274 * @offset: the offset into parent #GstBuffer.
275 * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
277 * Creates a sub-buffer from the parent at a given offset.
278 * This sub-buffer uses the actual memory space of the parent buffer.
279 * This function will copy the offset and timestamp field when the
280 * offset is 0, else they are set to _NONE.
281 * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
283 * Returns: the new #GstBuffer, or NULL if there was an error.
286 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
289 gpointer buffer_data;
291 g_return_val_if_fail (parent != NULL, NULL);
292 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
293 g_return_val_if_fail (size > 0, NULL);
294 g_return_val_if_fail (parent->size >= offset + size, NULL);
296 /* remember the data for the new buffer */
297 buffer_data = parent->data + offset;
298 /* make sure we're child not child from a child buffer */
299 while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
300 parent = GST_BUFFER (parent->pool_private);
302 /* ref the real parent */
303 gst_data_ref (GST_DATA (parent));
304 /* make sure nobody overwrites data in the parent */
305 GST_DATA_FLAG_SET (parent, GST_DATA_READONLY);
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;
349 * @buf1: a first source #GstBuffer to merge.
350 * @buf2: the second source #GstBuffer to merge.
352 * Create a new buffer that is the concatenation of the two source
353 * buffers. The original source buffers will not be modified or
356 * Internally is nothing more than a specialized gst_buffer_span(),
357 * so the same optimizations can occur.
359 * Returns: the new #GstBuffer that's the concatenation of the source buffers.
362 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
366 /* we're just a specific case of the more general gst_buffer_span() */
367 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
373 * gst_buffer_is_span_fast:
374 * @buf1: a first source #GstBuffer.
375 * @buf2: the second source #GstBuffer.
377 * Determines whether a gst_buffer_span() is free (as in free beer),
378 * or requires a memcpy.
380 * Returns: TRUE if the buffers are contiguous,
381 * FALSE if a copy would be required.
384 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
386 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
387 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
388 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
390 /* it's only fast if we have subbuffers of the same parent */
391 return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
392 (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
393 (buf1->pool_private == buf2->pool_private) &&
394 ((buf1->data + buf1->size) == buf2->data));
399 * @buf1: a first source #GstBuffer to merge.
400 * @offset: the offset in the first buffer from where the new
401 * buffer should start.
402 * @buf2: the second source #GstBuffer to merge.
403 * @len: the total length of the new buffer.
405 * Creates a new buffer that consists of part of buf1 and buf2.
406 * Logically, buf1 and buf2 are concatenated into a single larger
407 * buffer, and a new buffer is created at the given offset inside
408 * this space, with a given length.
410 * If the two source buffers are children of the same larger buffer,
411 * and are contiguous, the new buffer will be a child of the shared
412 * parent, and thus no copying is necessary. you can use
413 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
415 * Returns: the new #GstBuffer that spans the two source buffers.
418 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
422 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
423 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
424 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
425 g_return_val_if_fail (len > 0, NULL);
426 g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
428 /* if the two buffers have the same parent and are adjacent */
429 if (gst_buffer_is_span_fast (buf1, buf2)) {
430 GstBuffer *parent = GST_BUFFER (buf1->pool_private);
431 /* we simply create a subbuffer of the common parent */
432 newbuf = gst_buffer_create_sub (parent,
433 buf1->data - parent->data + offset, len);
436 GST_CAT_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p",
438 /* otherwise we simply have to brute-force copy the buffers */
439 newbuf = gst_buffer_new_and_alloc (len);
441 /* copy the first buffer's data across */
442 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
443 /* copy the second buffer's data across */
444 memcpy (newbuf->data + (buf1->size - offset), buf2->data,
445 len - (buf1->size - offset));
446 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
448 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
449 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
452 /* if we completely merged the two buffers (appended), we can
453 * calculate the duration too. Also make sure we's not messing with
454 * invalid DURATIONS */
455 if (offset == 0 && buf1->size + buf2->size == len &&
456 GST_BUFFER_DURATION_IS_VALID (buf1) &&
457 GST_BUFFER_DURATION_IS_VALID (buf2))
460 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
461 GST_BUFFER_DURATION (buf2);
468 gst_buffer_pool_get_type (void)
470 return _gst_buffer_pool_type;
474 * gst_buffer_pool_default_free:
475 * @pool: a #GstBufferPool to free.
477 * Frees the memory associated with the bufferpool.
480 gst_buffer_pool_default_free (GstBufferPool *pool)
482 g_return_if_fail (pool != NULL);
484 _GST_DATA_DISPOSE (GST_DATA (pool));
486 #ifndef GST_DISABLE_TRACE
487 gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
492 * gst_buffer_pool_new:
493 * @free: the #GstDataFreeFunction to free the buffer pool.
494 * @copy: the #GstDataCopyFunction to copy the buffer pool.
495 * @buffer_new: the #GstBufferPoolBufferNewFunction to create a new buffer
497 * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
499 * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer
501 * @user_data: the user data gpointer passed to buffer_* functions.
503 * Creates a new buffer pool with the given functions.
505 * Returns: a new #GstBufferPool, or NULL on error.
508 gst_buffer_pool_new (GstDataFreeFunction free,
509 GstDataCopyFunction copy,
510 GstBufferPoolBufferNewFunction buffer_new,
511 GstBufferPoolBufferCopyFunction buffer_copy,
512 GstBufferPoolBufferFreeFunction buffer_free,
517 /* we need at least a buffer_new function */
518 g_return_val_if_fail (buffer_new != NULL, NULL);
520 pool = g_new0 (GstBufferPool, 1);
521 #ifndef GST_DISABLE_TRACE
522 gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
525 GST_CAT_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
527 /* init data struct */
528 _GST_DATA_INIT (GST_DATA (pool),
529 _gst_buffer_pool_type,
531 (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
535 pool->buffer_new = buffer_new;
536 pool->buffer_copy = buffer_copy;
537 pool->buffer_free = buffer_free;
538 pool->user_data = user_data;
544 * gst_buffer_pool_is_active:
545 * @pool: the #GstBufferPool to query.
547 * Queries if the given buffer pool is active.
549 * Returns: TRUE if the pool is active.
552 gst_buffer_pool_is_active (GstBufferPool *pool)
554 g_return_val_if_fail (pool != NULL, FALSE);
560 * gst_buffer_pool_set_active:
561 * @pool: a #GstBufferPool to set the activity status on.
562 * @active: the new status of the pool.
564 * Sets the given pool to the active or inactive state depending on the
568 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
570 g_return_if_fail (pool != NULL);
572 pool->active = active;
576 * gst_buffer_pool_set_user_data:
577 * @pool: the #GstBufferPool to set the user data for.
578 * @user_data: the user_data to set on the buffer pool.
580 * Sets the given user data on the buffer pool.
583 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
585 g_return_if_fail (pool != NULL);
587 pool->user_data = user_data;
591 * gst_buffer_pool_get_user_data:
592 * @pool: the #GstBufferPool to get the user data for.
594 * Gets the user data of the buffer pool.
596 * Returns: the user data associated with this buffer pool.
599 gst_buffer_pool_get_user_data (GstBufferPool *pool)
601 g_return_val_if_fail (pool != NULL, NULL);
603 return pool->user_data;