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 "gstdata_private.h"
28 #include "gstbuffer.h"
29 #include "gstmemchunk.h"
31 #include "gstbufferpool-default.h"
33 GType _gst_buffer_type;
34 GType _gst_buffer_pool_type;
36 static gint _gst_buffer_live;
37 static gint _gst_buffer_pool_live;
39 static GstMemChunk *chunk;
42 _gst_buffer_initialize (void)
44 _gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
45 (GBoxedCopyFunc) gst_data_ref,
46 (GBoxedFreeFunc) gst_data_unref);
48 _gst_buffer_pool_type = g_boxed_type_register_static ("GstBufferPool",
49 (GBoxedCopyFunc) gst_data_ref,
50 (GBoxedFreeFunc) gst_data_unref);
54 chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer), sizeof (GstBuffer) * 200, 0);
56 GST_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
60 * gst_buffer_print_stats:
62 * Logs statistics about live buffers (using g_log).
65 gst_buffer_print_stats (void)
67 g_log (g_log_domain_gstreamer, G_LOG_LEVEL_INFO,
68 "%d live buffer(s)", _gst_buffer_live);
69 g_log (g_log_domain_gstreamer, G_LOG_LEVEL_INFO,
70 "%d live bufferpool(s)", _gst_buffer_pool_live);
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));
98 * gst_buffer_default_free:
99 * @buffer: a #GstBuffer to free
101 * Free the memory associated with the buffer including the buffer data,
102 * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
103 * This function is used by bufferpools.
106 gst_buffer_default_free (GstBuffer *buffer)
109 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer))
110 g_free (GST_BUFFER_DATA (buffer));
112 /* set to safe values */
113 GST_BUFFER_DATA (buffer) = NULL;
114 GST_BUFFER_SIZE (buffer) = 0;
116 _GST_DATA_DISPOSE (GST_DATA (buffer));
118 gst_mem_chunk_free (chunk, GST_DATA (buffer));
123 _gst_buffer_copy_from_pool (GstBuffer *buffer)
125 return buffer->pool->buffer_copy (buffer->pool, buffer, buffer->pool->user_data);
129 * gst_buffer_default_copy:
130 * @buffer: a #GstBuffer to make a copy of
132 * Make a full newly allocated copy of the given buffer, data and all.
133 * This function is used by bufferpools.
135 * Returns: new #GstBuffer
138 gst_buffer_default_copy (GstBuffer *buffer)
142 /* create a fresh new buffer */
143 copy = gst_buffer_new ();
145 /* we simply copy everything from our parent */
146 GST_BUFFER_DATA (copy) = g_memdup (GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
147 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
148 GST_BUFFER_MAXSIZE (copy) = GST_BUFFER_MAXSIZE (buffer);
149 GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
150 GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
158 * Creates a newly allocated buffer without any data.
160 * Returns: new #GstBuffer
163 gst_buffer_new (void)
167 new = gst_mem_chunk_alloc0 (chunk);
170 _GST_DATA_INIT (GST_DATA (new),
173 (GstDataFreeFunction) gst_buffer_default_free,
174 (GstDataCopyFunction) gst_buffer_default_copy);
176 GST_BUFFER_BUFFERPOOL (new) = NULL;
177 GST_BUFFER_POOL_PRIVATE (new) = NULL;
183 * gst_buffer_new_and_alloc:
184 * @size: the size of the buffer and the memory to allocate
186 * Creates a newly allocated buffer with data of the given size.
188 * Returns: new #GstBuffer
191 gst_buffer_new_and_alloc (guint size)
195 new = gst_buffer_new ();
197 GST_BUFFER_DATA (new) = g_malloc (size);
198 GST_BUFFER_SIZE (new) = size;
204 * gst_buffer_new_from_pool:
205 * @pool: the buffer pool to use
206 * @offset: the offset of the new buffer
207 * @size: the size of the new buffer
209 * Creates a newly allocated buffer using the specified bufferpool, offset and size.
211 * Returns: new #GstBuffer
214 gst_buffer_new_from_pool (GstBufferPool *pool, guint64 offset, guint size)
218 g_return_val_if_fail (pool != NULL, NULL);
220 buffer = pool->buffer_new (pool, offset, size, pool->user_data);
224 GST_BUFFER_BUFFERPOOL (buffer) = pool;
225 gst_data_ref (GST_DATA (pool));
227 /* override the buffer refcount functions with those from the pool (if any) */
228 if (pool->buffer_free)
229 GST_DATA (buffer)->free = (GstDataFreeFunction)_gst_buffer_free_to_pool;
230 if (pool->buffer_copy)
231 GST_DATA (buffer)->copy = (GstDataCopyFunction)_gst_buffer_copy_from_pool;
237 * gst_buffer_create_sub:
238 * @parent: parent #GstBuffer
239 * @offset: offset into parent #GstBuffer
240 * @size: size of new sub-buffer
242 * Creates a sub-buffer from the parent at a given offset.
243 * This sub-buffer uses the actual memory space of the parent buffer.
245 * Returns: a new #GstBuffer
248 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
251 gpointer buffer_data;
252 guint64 parent_offset;
254 g_return_val_if_fail (parent != NULL, NULL);
255 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
256 g_return_val_if_fail (size > 0, NULL);
257 g_return_val_if_fail (parent->size >= offset + size, NULL);
259 /* remember the data for the new buffer */
260 buffer_data = parent->data + offset;
261 parent_offset = GST_BUFFER_OFFSET (parent);
262 /* make sure we're child not child from a child buffer */
263 while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
264 parent = GST_BUFFER (parent->pool_private);
266 /* ref the real parent */
267 gst_data_ref (GST_DATA (parent));
268 /* make sure nobody overwrites data in the parent */
269 if (!GST_DATA_IS_READONLY (parent))
270 GST_DATA_FLAG_SET(parent, GST_DATA_READONLY);
272 /* create the new buffer */
273 buffer = gst_mem_chunk_alloc0 (chunk);
276 /* make sure nobody overwrites data in the new buffer 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: first source #GstBuffer to merge
300 * @buf2: 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: a new #GstBuffer that's the concatenation of the source buffers
312 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
315 /* we're just a specific case of the more general gst_buffer_span() */
316 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
322 * gst_buffer_is_span_fast:
323 * @buf1: first source buffer
324 * @buf2: second source buffer
326 * Determines whether a gst_buffer_span() is free (as in free beer),
327 * or requires a memcpy.
329 * Returns: TRUE if the buffers are contiguous, FALSE if a copy would be required.
332 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
334 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
335 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
337 /* it's only fast if we have subbuffers of the same parent */
338 return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
339 (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
340 (buf1->pool_private == buf2->pool_private) &&
341 ((buf1->data + buf1->size) == buf2->data));
346 * @buf1: first source #GstBuffer to merge
347 * @offset: offset in first buffer to start new buffer
348 * @buf2: second source #GstBuffer to merge
349 * @len: length of new buffer
351 * Creates a new buffer that consists of part of buf1 and buf2.
352 * Logically, buf1 and buf2 are concatenated into a single larger
353 * buffer, and a new buffer is created at the given offset inside
354 * this space, with a given length.
356 * If the two source buffers are children of the same larger buffer,
357 * and are contiguous, the new buffer will be a child of the shared
358 * parent, and thus no copying is necessary. you can use
359 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
361 * Returns: a new #GstBuffer that spans the two source buffers
364 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
368 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
369 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
370 g_return_val_if_fail (len > 0, NULL);
372 /* if the two buffers have the same parent and are adjacent */
373 if (gst_buffer_is_span_fast (buf1, buf2)) {
374 GstBuffer *parent = GST_BUFFER (buf1->pool_private);
375 /* we simply create a subbuffer of the common parent */
376 newbuf = gst_buffer_create_sub (parent, buf1->data - parent->data + offset, len);
379 GST_DEBUG (GST_CAT_BUFFER,"slow path taken while spanning buffers %p and %p", buffer, append);
380 /* otherwise we simply have to brute-force copy the buffers */
381 newbuf = gst_buffer_new_and_alloc (len);
383 /* copy relevant stuff from data struct of buffer1 */
384 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1) + offset;
386 /* copy the first buffer's data across */
387 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
388 /* copy the second buffer's data across */
389 memcpy (newbuf->data + (buf1->size - offset), buf2->data, len - (buf1->size - offset));
391 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
393 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
399 gst_buffer_pool_default_free (GstBufferPool *pool)
401 _GST_DATA_DISPOSE (GST_DATA (pool));
403 _gst_buffer_pool_live--;
407 * gst_buffer_pool_new:
408 * @free: The function to free the bufferpool
409 * @copy: The function to copy the bufferpool
410 * @buffer_new: the function to create a new buffer from this pool
411 * @buffer_copy: the function to copy a buffer from this pool
412 * @buffer_free: the function to free a buffer in this pool
413 * @user_data: user data passed to buffer_* functions
415 * Create a new bufferpool with the given functions.
417 * Returns: a new GstBufferPool or NULL on error.
420 gst_buffer_pool_new (GstDataFreeFunction free,
421 GstDataCopyFunction copy,
422 GstBufferPoolBufferNewFunction buffer_new,
423 GstBufferPoolBufferCopyFunction buffer_copy,
424 GstBufferPoolBufferFreeFunction buffer_free,
429 /* we need at least a buffer_new function */
430 g_return_val_if_fail (buffer_new != NULL, NULL);
432 pool = g_new0 (GstBufferPool, 1);
433 _gst_buffer_pool_live++;
435 GST_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
437 /* init data struct */
438 _GST_DATA_INIT (GST_DATA (pool),
439 _gst_buffer_pool_type,
441 (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
445 pool->buffer_new = buffer_new;
446 pool->buffer_copy = buffer_copy;
447 pool->buffer_free = buffer_free;
448 pool->user_data = user_data;
454 * gst_buffer_pool_is_active:
455 * @pool: the pool to query
457 * Query if the geven bufferpool is active.
459 * Returns: TRUE if the pool is active.
462 gst_buffer_pool_is_active (GstBufferPool *pool)
468 * gst_buffer_pool_set_active:
469 * @pool: the pool to activate
470 * @active: new status of the pool
472 * Set the given pool to the active or inactive state depending on the
476 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
478 pool->active = active;
482 * gst_buffer_pool_set_user_data:
483 * @pool: the pool set the user data for
484 * @user_data: the user_data to set
486 * Set the given user data to the bufferpool
489 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
491 pool->user_data = user_data;
495 * gst_buffer_pool_get_user_data:
496 * @pool: the pool get the user data for
498 * Get the user data of the bufferpool
500 * Returns: the user data associated with this bufferpool
503 gst_buffer_pool_get_user_data (GstBufferPool *pool)
505 return pool->user_data;
509 * gst_buffer_pool_get_default:
510 * @size: The size of the buffers to allocate from this pool
511 * @numbuffers: The number of buffer to preallocate in the pool
513 * Create a pool with buffers of the given size.
515 * Returns: A new bufferpool to create buffers of the given size.
518 gst_buffer_pool_get_default (guint size, guint numbuffers)
520 return _gst_buffer_pool_get_default (size, numbuffers);