GST_DEBUG reorganization containing loads of stuff:
[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 #include "gst_private.h"
24
25 #include "gstatomic_impl.h"
26 #include "gstdata_private.h"
27 #include "gstbuffer.h"
28 #include "gstmemchunk.h"
29 #include "gstinfo.h"
30 #include "gstbufferpool-default.h"
31
32 GType _gst_buffer_type;
33 GType _gst_buffer_pool_type;
34
35 #ifndef GST_DISABLE_TRACE
36 /* #define GST_WITH_ALLOC_TRACE  */
37 #include "gsttrace.h"
38
39 static GstAllocTrace *_gst_buffer_trace;
40 static GstAllocTrace *_gst_buffer_pool_trace;
41 #endif
42
43 static GstMemChunk *chunk;
44
45 void
46 _gst_buffer_initialize (void)
47 {
48   _gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
49                        (GBoxedCopyFunc) gst_data_ref,
50                        (GBoxedFreeFunc) gst_data_unref);
51
52   _gst_buffer_pool_type = g_boxed_type_register_static ("GstBufferPool",
53                             (GBoxedCopyFunc) gst_data_ref,
54                             (GBoxedFreeFunc) gst_data_unref);
55
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);
59 #endif
60
61   chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer), 
62                              sizeof (GstBuffer) * 200, 0);
63
64   GST_CAT_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
65 }
66
67 GType
68 gst_buffer_get_type (void)
69 {
70   return _gst_buffer_type;
71 }
72
73 static void
74 _gst_buffer_free_to_pool (GstBuffer *buffer)
75 {
76   GstBufferPool *pool = buffer->pool;
77
78   pool->buffer_free (pool, buffer, pool->user_data);
79
80   gst_data_unref (GST_DATA (pool));
81 }
82
83 static void
84 _gst_buffer_sub_free (GstBuffer *buffer)
85 {
86   gst_data_unref (GST_DATA (buffer->pool_private));
87
88   GST_BUFFER_DATA (buffer) = NULL;
89   GST_BUFFER_SIZE (buffer) = 0;
90
91   _GST_DATA_DISPOSE (GST_DATA (buffer));
92   
93   gst_mem_chunk_free (chunk, GST_DATA (buffer));
94 #ifndef GST_DISABLE_TRACE
95   gst_alloc_trace_free (_gst_buffer_trace, buffer);
96 #endif
97 }
98
99 /**
100  * gst_buffer_default_free:
101  * @buffer: a #GstBuffer to free.
102  *
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.
106  */
107 void
108 gst_buffer_default_free (GstBuffer *buffer)
109 {
110   g_return_if_fail (buffer != NULL);
111
112   /* free our data */
113   if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer)) 
114     g_free (GST_BUFFER_DATA (buffer));
115
116   /* set to safe values */
117   GST_BUFFER_DATA (buffer) = NULL;
118   GST_BUFFER_SIZE (buffer) = 0;
119
120   _GST_DATA_DISPOSE (GST_DATA (buffer));
121
122   gst_mem_chunk_free (chunk, GST_DATA (buffer));
123 #ifndef GST_DISABLE_TRACE
124   gst_alloc_trace_free (_gst_buffer_trace, buffer);
125 #endif
126 }
127
128 static GstBuffer*
129 _gst_buffer_copy_from_pool (GstBuffer *buffer)
130 {
131   return buffer->pool->buffer_copy (buffer->pool, buffer, buffer->pool->user_data);
132 }
133
134 /**
135  * gst_buffer_default_copy:
136  * @buffer: a #GstBuffer to make a copy of.
137  *
138  * Make a full newly allocated copy of the given buffer, data and all.
139  * This function is used by buffer pools.
140  *
141  * Returns: the new #GstBuffer.
142  */
143 GstBuffer*
144 gst_buffer_default_copy (GstBuffer *buffer)
145 {
146   GstBuffer *copy;
147
148   g_return_val_if_fail (buffer != NULL, NULL);
149
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);
154 #endif
155
156   _GST_DATA_INIT (GST_DATA (copy), 
157                   _gst_buffer_type,
158                   0,
159                   (GstDataFreeFunction) gst_buffer_default_free,
160                   (GstDataCopyFunction) gst_buffer_default_copy);
161
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;
172
173   return copy;
174 }
175
176 /**
177  * gst_buffer_new:
178  *
179  * Creates a newly allocated buffer without any data.
180  *
181  * Returns: the new #GstBuffer.
182  */
183 GstBuffer*
184 gst_buffer_new (void)
185 {
186   GstBuffer *newbuf;
187   
188   newbuf = gst_mem_chunk_alloc (chunk);
189 #ifndef GST_DISABLE_TRACE
190   gst_alloc_trace_new (_gst_buffer_trace, newbuf);
191 #endif
192
193   GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
194
195   _GST_DATA_INIT (GST_DATA (newbuf), 
196                   _gst_buffer_type,
197                   0,
198                   (GstDataFreeFunction) gst_buffer_default_free,
199                   (GstDataCopyFunction) gst_buffer_default_copy);
200
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;
209
210   return newbuf;
211 }
212
213 /**
214  * gst_buffer_new_and_alloc:
215  * @size: the size of the new buffer's data.
216  *
217  * Creates a newly allocated buffer with data of the given size.
218  *
219  * Returns: the new #GstBuffer.
220  */
221 GstBuffer*
222 gst_buffer_new_and_alloc (guint size)
223 {
224   GstBuffer *newbuf;
225
226   newbuf = gst_buffer_new ();
227
228   GST_BUFFER_DATA (newbuf)    = g_malloc (size);
229   GST_BUFFER_SIZE (newbuf)    = size;
230   GST_BUFFER_MAXSIZE (newbuf) = size;
231
232   return newbuf;
233 }
234
235 /**
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.
240  *
241  * Creates a newly allocated buffer using the specified buffer pool, 
242  * offset and size.
243  *
244  * Returns: the new #GstBuffer, or NULL if there was an error.
245  */
246 GstBuffer*
247 gst_buffer_new_from_pool (GstBufferPool *pool, 
248                           guint64 offset, guint size)
249 {
250   GstBuffer *buffer;
251   
252   g_return_val_if_fail (pool != NULL, NULL);
253
254   gst_data_ref (GST_DATA (pool));
255
256   buffer = pool->buffer_new (pool, offset, size, pool->user_data);
257   if (!buffer)
258     return NULL;
259
260   GST_BUFFER_BUFFERPOOL (buffer) = pool;
261
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;
267
268   return buffer;
269 }
270
271 /**
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).
276  *
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.
282  *
283  * Returns: the new #GstBuffer, or NULL if there was an error.
284  */
285 GstBuffer*
286 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
287 {
288   GstBuffer *buffer;
289   gpointer buffer_data;
290               
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);
295
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);
301   }
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);
306
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);
311 #endif
312
313   GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p", buffer);
314
315   /* make sure nobody overwrites data in the new buffer 
316    * by setting the READONLY flag */
317   _GST_DATA_INIT (GST_DATA (buffer), 
318                   _gst_buffer_type,
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);
323
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
331    * offset 0 */
332   if (offset == 0) {
333     GST_BUFFER_TIMESTAMP (buffer)    = GST_BUFFER_TIMESTAMP (parent);
334     GST_BUFFER_OFFSET (buffer)       = GST_BUFFER_OFFSET (parent);
335   }
336   else {
337     GST_BUFFER_TIMESTAMP (buffer)    = GST_CLOCK_TIME_NONE;
338     GST_BUFFER_OFFSET (buffer)       = GST_BUFFER_OFFSET_NONE;
339   }
340
341   GST_BUFFER_DURATION (buffer)     = GST_CLOCK_TIME_NONE;
342
343   return buffer;
344 }
345
346
347 /**
348  * gst_buffer_merge:
349  * @buf1: a first source #GstBuffer to merge.
350  * @buf2: the second source #GstBuffer to merge.
351  *
352  * Create a new buffer that is the concatenation of the two source
353  * buffers.  The original source buffers will not be modified or
354  * unref'd.
355  *
356  * Internally is nothing more than a specialized gst_buffer_span(),
357  * so the same optimizations can occur.
358  *
359  * Returns: the new #GstBuffer that's the concatenation of the source buffers.
360  */
361 GstBuffer*
362 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
363 {
364   GstBuffer *result;
365
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);
368
369   return result;
370 }
371
372 /**
373  * gst_buffer_is_span_fast:
374  * @buf1: a first source #GstBuffer.
375  * @buf2: the second source #GstBuffer.
376  *
377  * Determines whether a gst_buffer_span() is free (as in free beer), 
378  * or requires a memcpy. 
379  *
380  * Returns: TRUE if the buffers are contiguous, 
381  * FALSE if a copy would be required.
382  */
383 gboolean
384 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
385 {
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);
389
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));
395 }
396
397 /**
398  * gst_buffer_span:
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.
404  *
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.
409  *
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.
414  *
415  * Returns: the new #GstBuffer that spans the two source buffers.
416  */
417 GstBuffer*
418 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
419 {
420   GstBuffer *newbuf;
421
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);
427
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);
434   }
435   else {
436     GST_CAT_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p", 
437                buf1, buf2);
438     /* otherwise we simply have to brute-force copy the buffers */
439     newbuf = gst_buffer_new_and_alloc (len);
440
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 */
447     if (offset == 0) {
448       GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
449       GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
450     }
451   }
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)) 
458   {
459     /* add duration */
460     GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) + 
461                                    GST_BUFFER_DURATION (buf2);
462   }
463
464   return newbuf;
465 }
466
467 GType
468 gst_buffer_pool_get_type (void)
469 {
470   return _gst_buffer_pool_type;
471 }
472
473 /**
474  * gst_buffer_pool_default_free:
475  * @pool: a #GstBufferPool to free.
476  *
477  * Frees the memory associated with the bufferpool.
478  */
479 void
480 gst_buffer_pool_default_free (GstBufferPool *pool)
481 {
482   g_return_if_fail (pool != NULL);
483
484   _GST_DATA_DISPOSE (GST_DATA (pool));
485   g_free (pool);
486 #ifndef GST_DISABLE_TRACE
487   gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
488 #endif
489 }
490
491 /** 
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 
496  *              from this pool
497  * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
498  *               from this pool
499  * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer 
500  *               in this pool
501  * @user_data: the user data gpointer passed to buffer_* functions.
502  *
503  * Creates a new buffer pool with the given functions.
504  *
505  * Returns: a new #GstBufferPool, or NULL on error.
506  */
507 GstBufferPool*  
508 gst_buffer_pool_new (GstDataFreeFunction free,
509                      GstDataCopyFunction copy,
510                      GstBufferPoolBufferNewFunction buffer_new,
511                      GstBufferPoolBufferCopyFunction buffer_copy,
512                      GstBufferPoolBufferFreeFunction buffer_free,
513                      gpointer user_data)
514 {
515   GstBufferPool *pool;
516
517   /* we need at least a buffer_new function */
518   g_return_val_if_fail (buffer_new != NULL, NULL);
519
520   pool = g_new0 (GstBufferPool, 1);
521 #ifndef GST_DISABLE_TRACE
522   gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
523 #endif
524
525   GST_CAT_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
526         
527   /* init data struct */
528   _GST_DATA_INIT (GST_DATA (pool), 
529                   _gst_buffer_pool_type,
530                   0,
531                   (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
532                   copy);
533             
534   /* set functions */
535   pool->buffer_new = buffer_new;
536   pool->buffer_copy = buffer_copy;
537   pool->buffer_free = buffer_free;
538   pool->user_data = user_data;
539                     
540   return pool;
541 }
542
543 /**
544  * gst_buffer_pool_is_active:
545  * @pool: the #GstBufferPool to query.
546  *
547  * Queries if the given buffer pool is active.
548  *
549  * Returns: TRUE if the pool is active.
550  */
551 gboolean
552 gst_buffer_pool_is_active (GstBufferPool *pool)
553 {
554   g_return_val_if_fail (pool != NULL, FALSE);
555
556   return pool->active;
557 }
558
559 /**
560  * gst_buffer_pool_set_active:
561  * @pool: a #GstBufferPool to set the activity status on.
562  * @active: the new status of the pool.
563  *
564  * Sets the given pool to the active or inactive state depending on the
565  * active parameter.
566  */
567 void
568 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
569 {
570   g_return_if_fail (pool != NULL);
571
572   pool->active = active;
573 }
574
575 /**
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.
579  *
580  * Sets the given user data on the buffer pool.
581  */
582 void
583 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
584 {
585   g_return_if_fail (pool != NULL);
586
587   pool->user_data = user_data;
588 }
589
590 /**
591  * gst_buffer_pool_get_user_data:
592  * @pool: the #GstBufferPool to get the user data for.
593  *
594  * Gets the user data of the buffer pool.
595  * 
596  * Returns: the user data associated with this buffer pool.
597  */
598 gpointer
599 gst_buffer_pool_get_user_data (GstBufferPool *pool)
600 {
601   g_return_val_if_fail (pool != NULL, NULL);
602
603   return pool->user_data;
604 }