- replace old tracing with common GstTrace
[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 /* #define GST_WITH_ALLOC_TRACE */
35 #include "gsttrace.h"
36
37 GType _gst_buffer_type;
38 GType _gst_buffer_pool_type;
39
40 static GstAllocTrace *_gst_buffer_trace;
41 static GstAllocTrace *_gst_buffer_pool_trace;
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   _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);
58
59   chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer), 
60                              sizeof (GstBuffer) * 200, 0);
61
62   GST_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
63 }
64
65 static void
66 _gst_buffer_free_to_pool (GstBuffer *buffer)
67 {
68   GstBufferPool *pool = buffer->pool;
69
70   pool->buffer_free (pool, buffer, pool->user_data);
71
72   gst_data_unref (GST_DATA (pool));
73 }
74
75 static void
76 _gst_buffer_sub_free (GstBuffer *buffer)
77 {
78   gst_data_unref (GST_DATA (buffer->pool_private));
79
80   GST_BUFFER_DATA (buffer) = NULL;
81   GST_BUFFER_SIZE (buffer) = 0;
82
83   _GST_DATA_DISPOSE (GST_DATA (buffer));
84   
85   gst_mem_chunk_free (chunk, GST_DATA (buffer));
86   gst_alloc_trace_free (_gst_buffer_trace, buffer);
87 }
88
89 /**
90  * gst_buffer_default_free:
91  * @buffer: a #GstBuffer to free.
92  *
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.
96  */
97 void
98 gst_buffer_default_free (GstBuffer *buffer)
99 {
100   g_return_if_fail (buffer != NULL);
101
102   /* free our data */
103   if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer)) 
104     g_free (GST_BUFFER_DATA (buffer));
105
106   /* set to safe values */
107   GST_BUFFER_DATA (buffer) = NULL;
108   GST_BUFFER_SIZE (buffer) = 0;
109
110   _GST_DATA_DISPOSE (GST_DATA (buffer));
111
112   gst_mem_chunk_free (chunk, GST_DATA (buffer));
113   gst_alloc_trace_free (_gst_buffer_trace, buffer);
114 }
115
116 static GstBuffer*
117 _gst_buffer_copy_from_pool (GstBuffer *buffer)
118 {
119   return buffer->pool->buffer_copy (buffer->pool, buffer, buffer->pool->user_data);
120 }
121
122 /**
123  * gst_buffer_default_copy:
124  * @buffer: a #GstBuffer to make a copy of.
125  *
126  * Make a full newly allocated copy of the given buffer, data and all.
127  * This function is used by buffer pools.
128  *
129  * Returns: the new #GstBuffer.
130  */
131 GstBuffer*
132 gst_buffer_default_copy (GstBuffer *buffer)
133 {
134   GstBuffer *copy;
135
136   g_return_val_if_fail (buffer != NULL, NULL);
137
138   /* create a fresh new buffer */
139   copy = gst_buffer_new ();
140
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);
148
149   return copy;
150 }
151
152 /**
153  * gst_buffer_new:
154  *
155  * Creates a newly allocated buffer without any data.
156  *
157  * Returns: the new #GstBuffer.
158  */
159 GstBuffer*
160 gst_buffer_new (void)
161 {
162   GstBuffer *buf;
163   
164   buf = gst_mem_chunk_alloc0 (chunk);
165   gst_alloc_trace_new (_gst_buffer_trace, buf);
166
167   _GST_DATA_INIT (GST_DATA (buf), 
168                   _gst_buffer_type,
169                   0,
170                   (GstDataFreeFunction) gst_buffer_default_free,
171                   (GstDataCopyFunction) gst_buffer_default_copy);
172
173   GST_BUFFER_BUFFERPOOL (buf) = NULL;
174   GST_BUFFER_POOL_PRIVATE (buf) = NULL;
175
176   return buf;
177 }
178
179 /**
180  * gst_buffer_new_and_alloc:
181  * @size: the size of the new buffer's data.
182  *
183  * Creates a newly allocated buffer with data of the given size.
184  *
185  * Returns: the new #GstBuffer.
186  */
187 GstBuffer*
188 gst_buffer_new_and_alloc (guint size)
189 {
190   GstBuffer *new;
191
192   new = gst_buffer_new ();
193
194   GST_BUFFER_DATA (new) = g_malloc (size);
195   GST_BUFFER_SIZE (new) = size;
196
197   return new;
198 }
199
200 /**
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.
205  *
206  * Creates a newly allocated buffer using the specified buffer pool, 
207  * offset and size.
208  *
209  * Returns: the new #GstBuffer, or NULL if there was an error.
210  */
211 GstBuffer*
212 gst_buffer_new_from_pool (GstBufferPool *pool, 
213                           guint64 offset, guint size)
214 {
215   GstBuffer *buffer;
216   
217   g_return_val_if_fail (pool != NULL, NULL);
218
219   buffer = pool->buffer_new (pool, offset, size, pool->user_data);
220   if (!buffer)
221     return NULL;
222
223   GST_BUFFER_BUFFERPOOL (buffer) = pool;
224   gst_data_ref (GST_DATA (pool));
225
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;
231
232   return buffer;
233 }
234
235 /**
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).
240  *
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  *
244  * Returns: the new #GstBuffer, or NULL if there was an error.
245  */
246 GstBuffer*
247 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
248 {
249   GstBuffer *buffer;
250   gpointer buffer_data;
251   guint64 parent_offset;
252               
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);
257
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);
264   }
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);
270
271   /* create the new buffer */
272   buffer = gst_mem_chunk_alloc0 (chunk);
273   gst_alloc_trace_new (_gst_buffer_trace, buffer);
274
275   /* make sure nobody overwrites data in the new buffer 
276    * by setting the READONLY flag */
277   _GST_DATA_INIT (GST_DATA (buffer), 
278                   _gst_buffer_type,
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);
283
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;
288
289   /* set the right values in the child */
290   buffer->data = buffer_data;
291   buffer->size = size;
292
293   return buffer;
294 }
295
296
297 /**
298  * gst_buffer_merge:
299  * @buf1: a first source #GstBuffer to merge.
300  * @buf2: the second source #GstBuffer to merge.
301  *
302  * Create a new buffer that is the concatenation of the two source
303  * buffers.  The original source buffers will not be modified or
304  * unref'd.
305  *
306  * Internally is nothing more than a specialized gst_buffer_span(),
307  * so the same optimizations can occur.
308  *
309  * Returns: the new #GstBuffer that's the concatenation of the source buffers.
310  */
311 GstBuffer*
312 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
313 {
314   GstBuffer *result;
315
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);
318
319   return result;
320 }
321
322 /**
323  * gst_buffer_is_span_fast:
324  * @buf1: a first source #GstBuffer.
325  * @buf2: the second source #GstBuffer.
326  *
327  * Determines whether a gst_buffer_span() is free (as in free beer), 
328  * or requires a memcpy. 
329  *
330  * Returns: TRUE if the buffers are contiguous, 
331  * FALSE if a copy would be required.
332  */
333 gboolean
334 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
335 {
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);
339
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));
345 }
346
347 /**
348  * gst_buffer_span:
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.
354  *
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.
359  *
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.
364  *
365  * Returns: the new #GstBuffer that spans the two source buffers.
366  */
367 GstBuffer*
368 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
369 {
370   GstBuffer *newbuf;
371
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);
376
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);
383   }
384   else {
385     GST_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p", 
386                buf1, buf2);
387     /* otherwise we simply have to brute-force copy the buffers */
388     newbuf = gst_buffer_new_and_alloc (len);
389
390     /* copy relevant stuff from data struct of buffer1 */
391     GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1) + offset;
392
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));
398   }
399   /* if the offset is 0, the new buffer has the same timestamp as buf1 */
400   if (offset == 0)
401     GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
402
403   return newbuf;
404 }
405
406 /**
407  * gst_buffer_pool_default_free:
408  * @pool: a #GstBufferPool to free.
409  *
410  * Frees the memory associated with the bufferpool.
411  */
412 void
413 gst_buffer_pool_default_free (GstBufferPool *pool)
414 {
415   g_return_if_fail (pool != NULL);
416
417   _GST_DATA_DISPOSE (GST_DATA (pool));
418   g_free (pool);
419   gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
420 }
421
422 /** 
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 
427  *              from this pool
428  * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
429  *               from this pool
430  * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer 
431  *               in this pool
432  * @user_data: the user data gpointer passed to buffer_* functions.
433  *
434  * Creates a new buffer pool with the given functions.
435  *
436  * Returns: a new #GstBufferPool, or NULL on error.
437  */
438 GstBufferPool*  
439 gst_buffer_pool_new (GstDataFreeFunction free,
440                      GstDataCopyFunction copy,
441                      GstBufferPoolBufferNewFunction buffer_new,
442                      GstBufferPoolBufferCopyFunction buffer_copy,
443                      GstBufferPoolBufferFreeFunction buffer_free,
444                      gpointer user_data)
445 {
446   GstBufferPool *pool;
447
448   /* we need at least a buffer_new function */
449   g_return_val_if_fail (buffer_new != NULL, NULL);
450
451   pool = g_new0 (GstBufferPool, 1);
452   gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
453
454   GST_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
455         
456   /* init data struct */
457   _GST_DATA_INIT (GST_DATA (pool), 
458                   _gst_buffer_pool_type,
459                   0,
460                   (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
461                   copy);
462             
463   /* set functions */
464   pool->buffer_new = buffer_new;
465   pool->buffer_copy = buffer_copy;
466   pool->buffer_free = buffer_free;
467   pool->user_data = user_data;
468                     
469   return pool;
470 }
471
472 /**
473  * gst_buffer_pool_is_active:
474  * @pool: the #GstBufferPool to query.
475  *
476  * Queries if the given buffer pool is active.
477  *
478  * Returns: TRUE if the pool is active.
479  */
480 gboolean
481 gst_buffer_pool_is_active (GstBufferPool *pool)
482 {
483   g_return_val_if_fail (pool != NULL, FALSE);
484
485   return pool->active;
486 }
487
488 /**
489  * gst_buffer_pool_set_active:
490  * @pool: a #GstBufferPool to set the activity status on.
491  * @active: the new status of the pool.
492  *
493  * Sets the given pool to the active or inactive state depending on the
494  * active parameter.
495  */
496 void
497 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
498 {
499   g_return_if_fail (pool != NULL);
500
501   pool->active = active;
502 }
503
504 /**
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.
508  *
509  * Sets the given user data on the buffer pool.
510  */
511 void
512 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
513 {
514   g_return_if_fail (pool != NULL);
515
516   pool->user_data = user_data;
517 }
518
519 /**
520  * gst_buffer_pool_get_user_data:
521  * @pool: the #GstBufferPool to get the user data for.
522  *
523  * Gets the user data of the buffer pool.
524  * 
525  * Returns: the user data associated with this buffer pool.
526  */
527 gpointer
528 gst_buffer_pool_get_user_data (GstBufferPool *pool)
529 {
530   g_return_val_if_fail (pool != NULL, NULL);
531
532   return pool->user_data;
533 }
534