- Add more --disable options
[platform/upstream/gstreamer.git] / gst / gstbuffer.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstbuffer.c: Buffer operations
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /* this file makes too much noise for most debugging sessions */
24 #define GST_DEBUG_FORCE_DISABLE
25 #include "gst_private.h"
26
27 #include "gstatomic_impl.h"
28 #include "gstdata_private.h"
29 #include "gstbuffer.h"
30 #include "gstmemchunk.h"
31 #include "gstlog.h"
32 #include "gstbufferpool-default.h"
33
34 GType _gst_buffer_type;
35 GType _gst_buffer_pool_type;
36
37 #ifndef GST_DISABLE_TRACE
38 /* #define GST_WITH_ALLOC_TRACE  */
39 #include "gsttrace.h"
40
41 static GstAllocTrace *_gst_buffer_trace;
42 static GstAllocTrace *_gst_buffer_pool_trace;
43 #endif
44
45 static GstMemChunk *chunk;
46
47 void
48 _gst_buffer_initialize (void)
49 {
50   _gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
51                        (GBoxedCopyFunc) gst_data_ref,
52                        (GBoxedFreeFunc) gst_data_unref);
53
54   _gst_buffer_pool_type = g_boxed_type_register_static ("GstBufferPool",
55                             (GBoxedCopyFunc) gst_data_ref,
56                             (GBoxedFreeFunc) gst_data_unref);
57
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);
61 #endif
62
63   chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer), 
64                              sizeof (GstBuffer) * 200, 0);
65
66   GST_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
67 }
68
69 static void
70 _gst_buffer_free_to_pool (GstBuffer *buffer)
71 {
72   GstBufferPool *pool = buffer->pool;
73
74   pool->buffer_free (pool, buffer, pool->user_data);
75
76   gst_data_unref (GST_DATA (pool));
77 }
78
79 static void
80 _gst_buffer_sub_free (GstBuffer *buffer)
81 {
82   gst_data_unref (GST_DATA (buffer->pool_private));
83
84   GST_BUFFER_DATA (buffer) = NULL;
85   GST_BUFFER_SIZE (buffer) = 0;
86
87   _GST_DATA_DISPOSE (GST_DATA (buffer));
88   
89   gst_mem_chunk_free (chunk, GST_DATA (buffer));
90 #ifndef GST_DISABLE_TRACE
91   gst_alloc_trace_free (_gst_buffer_trace, buffer);
92 #endif
93 }
94
95 /**
96  * gst_buffer_default_free:
97  * @buffer: a #GstBuffer to free.
98  *
99  * Frees the memory associated with the buffer including the buffer data,
100  * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
101  * This function is used by buffer pools.
102  */
103 void
104 gst_buffer_default_free (GstBuffer *buffer)
105 {
106   g_return_if_fail (buffer != NULL);
107
108   /* free our data */
109   if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer)) 
110     g_free (GST_BUFFER_DATA (buffer));
111
112   /* set to safe values */
113   GST_BUFFER_DATA (buffer) = NULL;
114   GST_BUFFER_SIZE (buffer) = 0;
115
116   _GST_DATA_DISPOSE (GST_DATA (buffer));
117
118   gst_mem_chunk_free (chunk, GST_DATA (buffer));
119 #ifndef GST_DISABLE_TRACE
120   gst_alloc_trace_free (_gst_buffer_trace, buffer);
121 #endif
122 }
123
124 static GstBuffer*
125 _gst_buffer_copy_from_pool (GstBuffer *buffer)
126 {
127   return buffer->pool->buffer_copy (buffer->pool, buffer, buffer->pool->user_data);
128 }
129
130 /**
131  * gst_buffer_default_copy:
132  * @buffer: a #GstBuffer to make a copy of.
133  *
134  * Make a full newly allocated copy of the given buffer, data and all.
135  * This function is used by buffer pools.
136  *
137  * Returns: the new #GstBuffer.
138  */
139 GstBuffer*
140 gst_buffer_default_copy (GstBuffer *buffer)
141 {
142   GstBuffer *copy;
143
144   g_return_val_if_fail (buffer != NULL, NULL);
145
146   /* create a fresh new buffer */
147   copy = gst_buffer_new ();
148
149   /* we simply copy everything from our parent */
150   GST_BUFFER_DATA (copy)        = g_memdup (GST_BUFFER_DATA (buffer), 
151                                             GST_BUFFER_SIZE (buffer));
152   GST_BUFFER_SIZE (copy)        = GST_BUFFER_SIZE (buffer);
153   GST_BUFFER_MAXSIZE (copy)     = GST_BUFFER_MAXSIZE (buffer);
154   GST_BUFFER_TIMESTAMP (copy)   = GST_BUFFER_TIMESTAMP (buffer);
155   GST_BUFFER_OFFSET (copy)      = GST_BUFFER_OFFSET (buffer);
156
157   return copy;
158 }
159
160 /**
161  * gst_buffer_new:
162  *
163  * Creates a newly allocated buffer without any data.
164  *
165  * Returns: the new #GstBuffer.
166  */
167 GstBuffer*
168 gst_buffer_new (void)
169 {
170   GstBuffer *buf;
171   
172   buf = gst_mem_chunk_alloc0 (chunk);
173 #ifndef GST_DISABLE_TRACE
174   gst_alloc_trace_new (_gst_buffer_trace, buf);
175 #endif
176
177   GST_DEBUG (GST_CAT_BUFFER, "new %p", buf);
178
179   _GST_DATA_INIT (GST_DATA (buf), 
180                   _gst_buffer_type,
181                   0,
182                   (GstDataFreeFunction) gst_buffer_default_free,
183                   (GstDataCopyFunction) gst_buffer_default_copy);
184
185   GST_BUFFER_BUFFERPOOL (buf) = NULL;
186   GST_BUFFER_POOL_PRIVATE (buf) = NULL;
187
188   return buf;
189 }
190
191 /**
192  * gst_buffer_new_and_alloc:
193  * @size: the size of the new buffer's data.
194  *
195  * Creates a newly allocated buffer with data of the given size.
196  *
197  * Returns: the new #GstBuffer.
198  */
199 GstBuffer*
200 gst_buffer_new_and_alloc (guint size)
201 {
202   GstBuffer *new;
203
204   new = gst_buffer_new ();
205
206   GST_BUFFER_DATA (new) = g_malloc (size);
207   GST_BUFFER_SIZE (new) = size;
208
209   return new;
210 }
211
212 /**
213  * gst_buffer_new_from_pool:
214  * @pool: a #GstBufferPool to use.
215  * @offset: the offset of the new buffer.
216  * @size: the size of the new buffer.
217  *
218  * Creates a newly allocated buffer using the specified buffer pool, 
219  * offset and size.
220  *
221  * Returns: the new #GstBuffer, or NULL if there was an error.
222  */
223 GstBuffer*
224 gst_buffer_new_from_pool (GstBufferPool *pool, 
225                           guint64 offset, guint size)
226 {
227   GstBuffer *buffer;
228   
229   g_return_val_if_fail (pool != NULL, NULL);
230
231   buffer = pool->buffer_new (pool, offset, size, pool->user_data);
232   if (!buffer)
233     return NULL;
234
235   GST_BUFFER_BUFFERPOOL (buffer) = pool;
236   gst_data_ref (GST_DATA (pool));
237
238   /* override the buffer refcount functions with those from the pool (if any) */
239   if (pool->buffer_free)
240     GST_DATA (buffer)->free = (GstDataFreeFunction)_gst_buffer_free_to_pool;
241   if (pool->buffer_copy)
242     GST_DATA (buffer)->copy = (GstDataCopyFunction)_gst_buffer_copy_from_pool;
243
244   return buffer;
245 }
246
247 /**
248  * gst_buffer_create_sub:
249  * @parent: a parent #GstBuffer to create a subbuffer from.
250  * @offset: the offset into parent #GstBuffer.
251  * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
252  *
253  * Creates a sub-buffer from the parent at a given offset.
254  * This sub-buffer uses the actual memory space of the parent buffer.
255  *
256  * Returns: the new #GstBuffer, or NULL if there was an error.
257  */
258 GstBuffer*
259 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
260 {
261   GstBuffer *buffer;
262   gpointer buffer_data;
263   guint64 parent_offset;
264               
265   g_return_val_if_fail (parent != NULL, NULL);
266   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
267   g_return_val_if_fail (size > 0, NULL);
268   g_return_val_if_fail (parent->size >= offset + size, NULL);
269
270   /* remember the data for the new buffer */
271   buffer_data = parent->data + offset;
272   parent_offset = GST_BUFFER_OFFSET (parent);
273   /* make sure we're child not child from a child buffer */
274   while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
275     parent = GST_BUFFER (parent->pool_private);
276   }
277   /* ref the real parent */
278   gst_data_ref (GST_DATA (parent));
279   /* make sure nobody overwrites data in the parent */
280   if (!GST_DATA_IS_READONLY (parent))
281     GST_DATA_FLAG_SET(parent, GST_DATA_READONLY);
282
283   /* create the new buffer */
284   buffer = gst_mem_chunk_alloc0 (chunk);
285 #ifndef GST_DISABLE_TRACE
286   gst_alloc_trace_new (_gst_buffer_trace, buffer);
287 #endif
288
289   GST_DEBUG (GST_CAT_BUFFER, "new %p", buf);
290
291   /* make sure nobody overwrites data in the new buffer 
292    * by setting the READONLY flag */
293   _GST_DATA_INIT (GST_DATA (buffer), 
294                   _gst_buffer_type,
295                   GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
296                   GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
297                   (GstDataFreeFunction) _gst_buffer_sub_free,
298                   (GstDataCopyFunction) gst_buffer_default_copy);
299
300   GST_BUFFER_OFFSET (buffer) = parent_offset + offset;
301   GST_BUFFER_TIMESTAMP (buffer) = -1;
302   GST_BUFFER_BUFFERPOOL (buffer) = NULL;
303   GST_BUFFER_POOL_PRIVATE (buffer) = parent;
304
305   /* set the right values in the child */
306   buffer->data = buffer_data;
307   buffer->size = size;
308
309   return buffer;
310 }
311
312
313 /**
314  * gst_buffer_merge:
315  * @buf1: a first source #GstBuffer to merge.
316  * @buf2: the second source #GstBuffer to merge.
317  *
318  * Create a new buffer that is the concatenation of the two source
319  * buffers.  The original source buffers will not be modified or
320  * unref'd.
321  *
322  * Internally is nothing more than a specialized gst_buffer_span(),
323  * so the same optimizations can occur.
324  *
325  * Returns: the new #GstBuffer that's the concatenation of the source buffers.
326  */
327 GstBuffer*
328 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
329 {
330   GstBuffer *result;
331
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);
334
335   return result;
336 }
337
338 /**
339  * gst_buffer_is_span_fast:
340  * @buf1: a first source #GstBuffer.
341  * @buf2: the second source #GstBuffer.
342  *
343  * Determines whether a gst_buffer_span() is free (as in free beer), 
344  * or requires a memcpy. 
345  *
346  * Returns: TRUE if the buffers are contiguous, 
347  * FALSE if a copy would be required.
348  */
349 gboolean
350 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
351 {
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);
355
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));
361 }
362
363 /**
364  * gst_buffer_span:
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.
370  *
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.
375  *
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.
380  *
381  * Returns: the new #GstBuffer that spans the two source buffers.
382  */
383 GstBuffer*
384 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
385 {
386   GstBuffer *newbuf;
387
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
393   /* if the two buffers have the same parent and are adjacent */
394   if (gst_buffer_is_span_fast (buf1, buf2)) {
395     GstBuffer *parent = GST_BUFFER (buf1->pool_private);
396     /* we simply create a subbuffer of the common parent */
397     newbuf = gst_buffer_create_sub (parent, 
398                                     buf1->data - parent->data + offset, len);
399   }
400   else {
401     GST_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p", 
402                buf1, buf2);
403     /* otherwise we simply have to brute-force copy the buffers */
404     newbuf = gst_buffer_new_and_alloc (len);
405
406     /* copy relevant stuff from data struct of buffer1 */
407     GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1) + offset;
408
409     /* copy the first buffer's data across */
410     memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
411     /* copy the second buffer's data across */
412     memcpy (newbuf->data + (buf1->size - offset), buf2->data, 
413             len - (buf1->size - offset));
414   }
415   /* if the offset is 0, the new buffer has the same timestamp as buf1 */
416   if (offset == 0)
417     GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
418
419   return newbuf;
420 }
421
422 /**
423  * gst_buffer_pool_default_free:
424  * @pool: a #GstBufferPool to free.
425  *
426  * Frees the memory associated with the bufferpool.
427  */
428 void
429 gst_buffer_pool_default_free (GstBufferPool *pool)
430 {
431   g_return_if_fail (pool != NULL);
432
433   _GST_DATA_DISPOSE (GST_DATA (pool));
434   g_free (pool);
435 #ifndef GST_DISABLE_TRACE
436   gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
437 #endif
438 }
439
440 /** 
441  * gst_buffer_pool_new:
442  * @free: the #GstDataFreeFunction to free the buffer pool.
443  * @copy: the #GstDataCopyFunction to copy the buffer pool.
444  * @buffer_new: the #GstBufferPoolBufferNewFunction to create a new buffer 
445  *              from this pool
446  * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
447  *               from this pool
448  * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer 
449  *               in this pool
450  * @user_data: the user data gpointer passed to buffer_* functions.
451  *
452  * Creates a new buffer pool with the given functions.
453  *
454  * Returns: a new #GstBufferPool, or NULL on error.
455  */
456 GstBufferPool*  
457 gst_buffer_pool_new (GstDataFreeFunction free,
458                      GstDataCopyFunction copy,
459                      GstBufferPoolBufferNewFunction buffer_new,
460                      GstBufferPoolBufferCopyFunction buffer_copy,
461                      GstBufferPoolBufferFreeFunction buffer_free,
462                      gpointer user_data)
463 {
464   GstBufferPool *pool;
465
466   /* we need at least a buffer_new function */
467   g_return_val_if_fail (buffer_new != NULL, NULL);
468
469   pool = g_new0 (GstBufferPool, 1);
470 #ifndef GST_DISABLE_TRACE
471   gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
472 #endif
473
474   GST_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
475         
476   /* init data struct */
477   _GST_DATA_INIT (GST_DATA (pool), 
478                   _gst_buffer_pool_type,
479                   0,
480                   (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
481                   copy);
482             
483   /* set functions */
484   pool->buffer_new = buffer_new;
485   pool->buffer_copy = buffer_copy;
486   pool->buffer_free = buffer_free;
487   pool->user_data = user_data;
488                     
489   return pool;
490 }
491
492 /**
493  * gst_buffer_pool_is_active:
494  * @pool: the #GstBufferPool to query.
495  *
496  * Queries if the given buffer pool is active.
497  *
498  * Returns: TRUE if the pool is active.
499  */
500 gboolean
501 gst_buffer_pool_is_active (GstBufferPool *pool)
502 {
503   g_return_val_if_fail (pool != NULL, FALSE);
504
505   return pool->active;
506 }
507
508 /**
509  * gst_buffer_pool_set_active:
510  * @pool: a #GstBufferPool to set the activity status on.
511  * @active: the new status of the pool.
512  *
513  * Sets the given pool to the active or inactive state depending on the
514  * active parameter.
515  */
516 void
517 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
518 {
519   g_return_if_fail (pool != NULL);
520
521   pool->active = active;
522 }
523
524 /**
525  * gst_buffer_pool_set_user_data:
526  * @pool: the #GstBufferPool to set the user data for.
527  * @user_data: the user_data to set on the buffer pool.
528  *
529  * Sets the given user data on the buffer pool.
530  */
531 void
532 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
533 {
534   g_return_if_fail (pool != NULL);
535
536   pool->user_data = user_data;
537 }
538
539 /**
540  * gst_buffer_pool_get_user_data:
541  * @pool: the #GstBufferPool to get the user data for.
542  *
543  * Gets the user data of the buffer pool.
544  * 
545  * Returns: the user data associated with this buffer pool.
546  */
547 gpointer
548 gst_buffer_pool_get_user_data (GstBufferPool *pool)
549 {
550   g_return_val_if_fail (pool != NULL, NULL);
551
552   return pool->user_data;
553 }
554