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"
31 GType _gst_buffer_type;
32 GType _gst_buffer_pool_type;
34 #ifndef GST_DISABLE_TRACE
35 /* #define GST_WITH_ALLOC_TRACE */
38 static GstAllocTrace *_gst_buffer_trace;
41 static GstMemChunk *chunk;
44 _gst_buffer_initialize (void)
46 _gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
47 (GBoxedCopyFunc) gst_data_ref,
48 (GBoxedFreeFunc) gst_data_unref);
50 _gst_buffer_pool_type = g_boxed_type_register_static ("GstBufferPool",
51 (GBoxedCopyFunc) gst_data_ref,
52 (GBoxedFreeFunc) gst_data_unref);
54 #ifndef GST_DISABLE_TRACE
55 _gst_buffer_trace = gst_alloc_trace_register (GST_BUFFER_TRACE_NAME);
58 chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer),
59 sizeof (GstBuffer) * 200, 0);
61 GST_CAT_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
65 gst_buffer_get_type (void)
67 return _gst_buffer_type;
71 _gst_buffer_sub_free (GstBuffer *buffer)
73 gst_data_unref (GST_DATA (buffer->pool_private));
75 GST_BUFFER_DATA (buffer) = NULL;
76 GST_BUFFER_SIZE (buffer) = 0;
78 _GST_DATA_DISPOSE (GST_DATA (buffer));
80 gst_mem_chunk_free (chunk, GST_DATA (buffer));
81 #ifndef GST_DISABLE_TRACE
82 gst_alloc_trace_free (_gst_buffer_trace, buffer);
87 * gst_buffer_default_free:
88 * @buffer: a #GstBuffer to free.
90 * Frees the memory associated with the buffer including the buffer data,
91 * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
92 * This function is used by buffer pools.
95 gst_buffer_default_free (GstBuffer *buffer)
97 g_return_if_fail (buffer != NULL);
100 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer))
101 g_free (GST_BUFFER_DATA (buffer));
103 /* set to safe values */
104 GST_BUFFER_DATA (buffer) = NULL;
105 GST_BUFFER_SIZE (buffer) = 0;
107 _GST_DATA_DISPOSE (GST_DATA (buffer));
109 gst_mem_chunk_free (chunk, GST_DATA (buffer));
110 #ifndef GST_DISABLE_TRACE
111 gst_alloc_trace_free (_gst_buffer_trace, buffer);
117 * @dest: buffer to stamp
118 * @src: buffer to stamp from
120 * Copies additional information (timestamps and offsets) from one buffer to
124 gst_buffer_stamp (GstBuffer *dest, const GstBuffer *src)
126 g_return_if_fail (dest != NULL);
127 g_return_if_fail (src != NULL);
129 GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
130 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
131 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
132 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
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);
168 gst_buffer_stamp (copy, buffer);
169 GST_BUFFER_BUFFERPOOL (copy) = NULL;
170 GST_BUFFER_POOL_PRIVATE (copy) = NULL;
178 * Creates a newly allocated buffer without any data.
180 * Returns: the new #GstBuffer.
183 gst_buffer_new (void)
187 newbuf = gst_mem_chunk_alloc (chunk);
188 #ifndef GST_DISABLE_TRACE
189 gst_alloc_trace_new (_gst_buffer_trace, newbuf);
192 GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
194 _GST_DATA_INIT (GST_DATA (newbuf),
197 (GstDataFreeFunction) gst_buffer_default_free,
198 (GstDataCopyFunction) gst_buffer_default_copy);
200 GST_BUFFER_DATA (newbuf) = NULL;
201 GST_BUFFER_SIZE (newbuf) = 0;
202 GST_BUFFER_MAXSIZE (newbuf) = GST_BUFFER_MAXSIZE_NONE;
203 GST_BUFFER_TIMESTAMP (newbuf) = GST_CLOCK_TIME_NONE;
204 GST_BUFFER_DURATION (newbuf) = GST_CLOCK_TIME_NONE;
205 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET_NONE;
206 GST_BUFFER_OFFSET_END (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_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.
243 * This function will copy the offset and timestamp field when the
244 * offset is 0, else they are set to _NONE.
245 * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
247 * Returns: the new #GstBuffer, or NULL if there was an error.
250 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
253 gpointer buffer_data;
255 g_return_val_if_fail (parent != NULL, NULL);
256 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
257 g_return_val_if_fail (size > 0, NULL);
258 g_return_val_if_fail (parent->size >= offset + size, NULL);
260 /* remember the data for the new buffer */
261 buffer_data = parent->data + offset;
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));
269 /* create the new buffer */
270 buffer = gst_mem_chunk_alloc (chunk);
271 #ifndef GST_DISABLE_TRACE
272 gst_alloc_trace_new (_gst_buffer_trace, buffer);
275 GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p", buffer);
277 /* make sure nobody overwrites data in the new buffer
278 * by setting the READONLY flag */
279 _GST_DATA_INIT (GST_DATA (buffer),
281 GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
282 GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
283 (GstDataFreeFunction) _gst_buffer_sub_free,
284 (GstDataCopyFunction) gst_buffer_default_copy);
286 /* set the right values in the child */
287 GST_BUFFER_DATA (buffer) = buffer_data;
288 GST_BUFFER_SIZE (buffer) = size;
289 GST_BUFFER_MAXSIZE (buffer) = size;
290 GST_BUFFER_BUFFERPOOL (buffer) = NULL;
291 GST_BUFFER_POOL_PRIVATE (buffer) = parent;
292 /* we can copy the timestamp and offset if the new buffer starts at
295 GST_BUFFER_TIMESTAMP (buffer) = GST_BUFFER_TIMESTAMP (parent);
296 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET (parent);
299 GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
300 GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
303 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
304 GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
305 /* make sure nobody overwrites data as it would overwrite in the parent.
306 * data in parent cannot be overwritten because we hold a ref */
307 GST_DATA_FLAG_SET (parent, GST_DATA_READONLY);
315 * @buf1: a first source #GstBuffer to merge.
316 * @buf2: the second source #GstBuffer to merge.
318 * Create a new buffer that is the concatenation of the two source
319 * buffers. The original source buffers will not be modified or
322 * Internally is nothing more than a specialized gst_buffer_span(),
323 * so the same optimizations can occur.
325 * Returns: the new #GstBuffer that's the concatenation of the source buffers.
328 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
332 /* we're just a specific case of the more general gst_buffer_span() */
333 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
339 * gst_buffer_is_span_fast:
340 * @buf1: a first source #GstBuffer.
341 * @buf2: the second source #GstBuffer.
343 * Determines whether a gst_buffer_span() is free (as in free beer),
344 * or requires a memcpy.
346 * Returns: TRUE if the buffers are contiguous,
347 * FALSE if a copy would be required.
350 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
352 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
353 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
354 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
356 /* it's only fast if we have subbuffers of the same parent */
357 return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
358 (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
359 (buf1->pool_private == buf2->pool_private) &&
360 ((buf1->data + buf1->size) == buf2->data));
365 * @buf1: a first source #GstBuffer to merge.
366 * @offset: the offset in the first buffer from where the new
367 * buffer should start.
368 * @buf2: the second source #GstBuffer to merge.
369 * @len: the total length of the new buffer.
371 * Creates a new buffer that consists of part of buf1 and buf2.
372 * Logically, buf1 and buf2 are concatenated into a single larger
373 * buffer, and a new buffer is created at the given offset inside
374 * this space, with a given length.
376 * If the two source buffers are children of the same larger buffer,
377 * and are contiguous, the new buffer will be a child of the shared
378 * parent, and thus no copying is necessary. you can use
379 * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
381 * Returns: the new #GstBuffer that spans the two source buffers.
384 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
388 g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
389 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
390 g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
391 g_return_val_if_fail (len > 0, NULL);
392 g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
394 /* if the two buffers have the same parent and are adjacent */
395 if (gst_buffer_is_span_fast (buf1, buf2)) {
396 GstBuffer *parent = GST_BUFFER (buf1->pool_private);
397 /* we simply create a subbuffer of the common parent */
398 newbuf = gst_buffer_create_sub (parent,
399 buf1->data - parent->data + offset, len);
402 GST_CAT_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p",
404 /* otherwise we simply have to brute-force copy the buffers */
405 newbuf = gst_buffer_new_and_alloc (len);
407 /* copy the first buffer's data across */
408 memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
409 /* copy the second buffer's data across */
410 memcpy (newbuf->data + (buf1->size - offset), buf2->data,
411 len - (buf1->size - offset));
412 /* if the offset is 0, the new buffer has the same timestamp as buf1 */
414 GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
415 GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
418 /* if we completely merged the two buffers (appended), we can
419 * calculate the duration too. Also make sure we's not messing with
420 * invalid DURATIONS */
421 if (offset == 0 && buf1->size + buf2->size == len) {
422 if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
423 GST_BUFFER_DURATION_IS_VALID (buf2)) {
425 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
426 GST_BUFFER_DURATION (buf2);
428 if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
430 GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
438 gst_buffer_pool_get_type (void)
440 return _gst_buffer_pool_type;