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 /* #define GST_WITH_ALLOC_TRACE */
37 GType _gst_buffer_type;
38 GType _gst_buffer_pool_type;
40 static GstAllocTrace *_gst_buffer_trace;
41 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 _gst_buffer_trace = gst_alloc_trace_register (GST_BUFFER_TRACE_NAME);
57 _gst_buffer_pool_trace = gst_alloc_trace_register (GST_BUFFER_POOL_TRACE_NAME);
59 chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer),
60 sizeof (GstBuffer) * 200, 0);
62 GST_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
66 _gst_buffer_free_to_pool (GstBuffer *buffer)
68 GstBufferPool *pool = buffer->pool;
70 pool->buffer_free (pool, buffer, pool->user_data);
72 gst_data_unref (GST_DATA (pool));
76 _gst_buffer_sub_free (GstBuffer *buffer)
78 gst_data_unref (GST_DATA (buffer->pool_private));
80 GST_BUFFER_DATA (buffer) = NULL;
81 GST_BUFFER_SIZE (buffer) = 0;
83 _GST_DATA_DISPOSE (GST_DATA (buffer));
85 gst_mem_chunk_free (chunk, GST_DATA (buffer));
86 gst_alloc_trace_free (_gst_buffer_trace, buffer);
90 * gst_buffer_default_free:
91 * @buffer: a #GstBuffer to free.
93 * Frees the memory associated with the buffer including the buffer data,
94 * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
95 * This function is used by buffer pools.
98 gst_buffer_default_free (GstBuffer *buffer)
100 g_return_if_fail (buffer != NULL);
103 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer))
104 g_free (GST_BUFFER_DATA (buffer));
106 /* set to safe values */
107 GST_BUFFER_DATA (buffer) = NULL;
108 GST_BUFFER_SIZE (buffer) = 0;
110 _GST_DATA_DISPOSE (GST_DATA (buffer));
112 gst_mem_chunk_free (chunk, GST_DATA (buffer));
113 gst_alloc_trace_free (_gst_buffer_trace, buffer);
117 _gst_buffer_copy_from_pool (GstBuffer *buffer)
119 return buffer->pool->buffer_copy (buffer->pool, buffer, buffer->pool->user_data);
123 * gst_buffer_default_copy:
124 * @buffer: a #GstBuffer to make a copy of.
126 * Make a full newly allocated copy of the given buffer, data and all.
127 * This function is used by buffer pools.
129 * Returns: the new #GstBuffer.
132 gst_buffer_default_copy (GstBuffer *buffer)
136 g_return_val_if_fail (buffer != NULL, NULL);
138 /* create a fresh new buffer */
139 copy = gst_buffer_new ();
141 /* we simply copy everything from our parent */
142 GST_BUFFER_DATA (copy) = g_memdup (GST_BUFFER_DATA (buffer),
143 GST_BUFFER_SIZE (buffer));
144 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
145 GST_BUFFER_MAXSIZE (copy) = GST_BUFFER_MAXSIZE (buffer);
146 GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
147 GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
155 * Creates a newly allocated buffer without any data.
157 * Returns: the new #GstBuffer.
160 gst_buffer_new (void)
164 buf = gst_mem_chunk_alloc0 (chunk);
165 gst_alloc_trace_new (_gst_buffer_trace, buf);
167 _GST_DATA_INIT (GST_DATA (buf),
170 (GstDataFreeFunction) gst_buffer_default_free,
171 (GstDataCopyFunction) gst_buffer_default_copy);
173 GST_BUFFER_BUFFERPOOL (buf) = NULL;
174 GST_BUFFER_POOL_PRIVATE (buf) = NULL;
180 * gst_buffer_new_and_alloc:
181 * @size: the size of the new buffer's data.
183 * Creates a newly allocated buffer with data of the given size.
185 * Returns: the new #GstBuffer.
188 gst_buffer_new_and_alloc (guint size)
192 new = gst_buffer_new ();
194 GST_BUFFER_DATA (new) = g_malloc (size);
195 GST_BUFFER_SIZE (new) = size;
201 * gst_buffer_new_from_pool:
202 * @pool: a #GstBufferPool to use.
203 * @offset: the offset of the new buffer.
204 * @size: the size of the new buffer.
206 * Creates a newly allocated buffer using the specified buffer pool,
209 * Returns: the new #GstBuffer, or NULL if there was an error.
212 gst_buffer_new_from_pool (GstBufferPool *pool,
213 guint64 offset, guint size)
217 g_return_val_if_fail (pool != NULL, NULL);
219 buffer = pool->buffer_new (pool, offset, size, pool->user_data);
223 GST_BUFFER_BUFFERPOOL (buffer) = pool;
224 gst_data_ref (GST_DATA (pool));
226 /* override the buffer refcount functions with those from the pool (if any) */
227 if (pool->buffer_free)
228 GST_DATA (buffer)->free = (GstDataFreeFunction)_gst_buffer_free_to_pool;
229 if (pool->buffer_copy)
230 GST_DATA (buffer)->copy = (GstDataCopyFunction)_gst_buffer_copy_from_pool;
236 * gst_buffer_create_sub:
237 * @parent: a parent #GstBuffer to create a subbuffer from.
238 * @offset: the offset into parent #GstBuffer.
239 * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
241 * Creates a sub-buffer from the parent at a given offset.
242 * This sub-buffer uses the actual memory space of the parent buffer.
244 * Returns: the new #GstBuffer, or NULL if there was an error.
247 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
250 gpointer buffer_data;
251 guint64 parent_offset;
253 g_return_val_if_fail (parent != NULL, NULL);
254 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
255 g_return_val_if_fail (size > 0, NULL);
256 g_return_val_if_fail (parent->size >= offset + size, NULL);
258 /* remember the data for the new buffer */
259 buffer_data = parent->data + offset;
260 parent_offset = GST_BUFFER_OFFSET (parent);
261 /* make sure we're child not child from a child buffer */
262 while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
263 parent = GST_BUFFER (parent->pool_private);
265 /* ref the real parent */
266 gst_data_ref (GST_DATA (parent));
267 /* make sure nobody overwrites data in the parent */
268 if (!GST_DATA_IS_READONLY (parent))
269 GST_DATA_FLAG_SET(parent, GST_DATA_READONLY);
271 /* create the new buffer */
272 buffer = gst_mem_chunk_alloc0 (chunk);
273 gst_alloc_trace_new (_gst_buffer_trace, buffer);
275 /* make sure nobody overwrites data in the new buffer
276 * by setting the READONLY flag */
277 _GST_DATA_INIT (GST_DATA (buffer),
279 GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
280 GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
281 (GstDataFreeFunction) _gst_buffer_sub_free,
282 (GstDataCopyFunction) gst_buffer_default_copy);
284 GST_BUFFER_OFFSET (buffer) = parent_offset + offset;
285 GST_BUFFER_TIMESTAMP (buffer) = -1;
286 GST_BUFFER_BUFFERPOOL (buffer) = NULL;
287 GST_BUFFER_POOL_PRIVATE (buffer) = parent;
289 /* set the right values in the child */
290 buffer->data = buffer_data;
299 * @buf1: a first source #GstBuffer to merge.
300 * @buf2: the second source #GstBuffer to merge.
302 * Create a new buffer that is the concatenation of the two source
303 * buffers. The original source buffers will not be modified or
306 * Internally is nothing more than a specialized gst_buffer_span(),
307 * so the same optimizations can occur.
309 * Returns: the new #GstBuffer that's the concatenation of the source buffers.
312 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
316 /* we're just a specific case of the more general gst_buffer_span() */
317 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
323 * gst_buffer_is_span_fast:
324 * @buf1: a first source #GstBuffer.
325 * @buf2: the second source #GstBuffer.
327 * Determines whether a gst_buffer_span() is free (as in free beer),
328 * or requires a memcpy.
330 * Returns: TRUE if the buffers are contiguous,
331 * FALSE if a copy would be required.
334 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
336 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
337 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
338 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
340 /* it's only fast if we have subbuffers of the same parent */
341 return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
342 (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
343 (buf1->pool_private == buf2->pool_private) &&
344 ((buf1->data + buf1->size) == buf2->data));
349 * @buf1: a first source #GstBuffer to merge.
350 * @offset: the offset in the first buffer from where the new
351 * buffer should start.
352 * @buf2: the second source #GstBuffer to merge.
353 * @len: the total length of the new buffer.
355 * Creates a new buffer that consists of part of buf1 and buf2.
356 * Logically, buf1 and buf2 are concatenated into a single larger
357 * buffer, and a new buffer is created at the given offset inside
358 * this space, with a given length.
360 * If the two source buffers are children of the same larger buffer,
361 * and are contiguous, the new buffer will be a child of the shared
362 * parent, and thus no copying is necessary. you can use
363 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
365 * Returns: the new #GstBuffer that spans the two source buffers.
368 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
372 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
373 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
374 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
375 g_return_val_if_fail (len > 0, NULL);
377 /* if the two buffers have the same parent and are adjacent */
378 if (gst_buffer_is_span_fast (buf1, buf2)) {
379 GstBuffer *parent = GST_BUFFER (buf1->pool_private);
380 /* we simply create a subbuffer of the common parent */
381 newbuf = gst_buffer_create_sub (parent,
382 buf1->data - parent->data + offset, len);
385 GST_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p",
387 /* otherwise we simply have to brute-force copy the buffers */
388 newbuf = gst_buffer_new_and_alloc (len);
390 /* copy relevant stuff from data struct of buffer1 */
391 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1) + offset;
393 /* copy the first buffer's data across */
394 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
395 /* copy the second buffer's data across */
396 memcpy (newbuf->data + (buf1->size - offset), buf2->data,
397 len - (buf1->size - offset));
399 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
401 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
407 * gst_buffer_pool_default_free:
408 * @pool: a #GstBufferPool to free.
410 * Frees the memory associated with the bufferpool.
413 gst_buffer_pool_default_free (GstBufferPool *pool)
415 g_return_if_fail (pool != NULL);
417 _GST_DATA_DISPOSE (GST_DATA (pool));
419 gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
423 * gst_buffer_pool_new:
424 * @free: the #GstDataFreeFunction to free the buffer pool.
425 * @copy: the #GstDataCopyFunction to copy the buffer pool.
426 * @buffer_new: the #GstBufferPoolBufferNewFunction to create a new buffer
428 * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
430 * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer
432 * @user_data: the user data gpointer passed to buffer_* functions.
434 * Creates a new buffer pool with the given functions.
436 * Returns: a new #GstBufferPool, or NULL on error.
439 gst_buffer_pool_new (GstDataFreeFunction free,
440 GstDataCopyFunction copy,
441 GstBufferPoolBufferNewFunction buffer_new,
442 GstBufferPoolBufferCopyFunction buffer_copy,
443 GstBufferPoolBufferFreeFunction buffer_free,
448 /* we need at least a buffer_new function */
449 g_return_val_if_fail (buffer_new != NULL, NULL);
451 pool = g_new0 (GstBufferPool, 1);
452 gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
454 GST_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
456 /* init data struct */
457 _GST_DATA_INIT (GST_DATA (pool),
458 _gst_buffer_pool_type,
460 (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
464 pool->buffer_new = buffer_new;
465 pool->buffer_copy = buffer_copy;
466 pool->buffer_free = buffer_free;
467 pool->user_data = user_data;
473 * gst_buffer_pool_is_active:
474 * @pool: the #GstBufferPool to query.
476 * Queries if the given buffer pool is active.
478 * Returns: TRUE if the pool is active.
481 gst_buffer_pool_is_active (GstBufferPool *pool)
483 g_return_val_if_fail (pool != NULL, FALSE);
489 * gst_buffer_pool_set_active:
490 * @pool: a #GstBufferPool to set the activity status on.
491 * @active: the new status of the pool.
493 * Sets the given pool to the active or inactive state depending on the
497 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
499 g_return_if_fail (pool != NULL);
501 pool->active = active;
505 * gst_buffer_pool_set_user_data:
506 * @pool: the #GstBufferPool to set the user data for.
507 * @user_data: the user_data to set on the buffer pool.
509 * Sets the given user data on the buffer pool.
512 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
514 g_return_if_fail (pool != NULL);
516 pool->user_data = user_data;
520 * gst_buffer_pool_get_user_data:
521 * @pool: the #GstBufferPool to get the user data for.
523 * Gets the user data of the buffer pool.
525 * Returns: the user data associated with this buffer pool.
528 gst_buffer_pool_get_user_data (GstBufferPool *pool)
530 g_return_val_if_fail (pool != NULL, NULL);
532 return pool->user_data;