merge TYPEFIND branch. Major changes:
[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
305   /* create the new buffer */
306   buffer = gst_mem_chunk_alloc (chunk);
307 #ifndef GST_DISABLE_TRACE
308   gst_alloc_trace_new (_gst_buffer_trace, buffer);
309 #endif
310
311   GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p", buffer);
312
313   /* make sure nobody overwrites data in the new buffer 
314    * by setting the READONLY flag */
315   _GST_DATA_INIT (GST_DATA (buffer), 
316                   _gst_buffer_type,
317                   GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
318                   GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
319                   (GstDataFreeFunction) _gst_buffer_sub_free,
320                   (GstDataCopyFunction) gst_buffer_default_copy);
321
322   /* set the right values in the child */
323   GST_BUFFER_DATA (buffer)         = buffer_data;
324   GST_BUFFER_SIZE (buffer)         = size;
325   GST_BUFFER_MAXSIZE (buffer)      = size;
326   GST_BUFFER_BUFFERPOOL (buffer)   = NULL;
327   GST_BUFFER_POOL_PRIVATE (buffer) = parent;
328   /* we can copy the timestamp and offset if the new buffer starts at
329    * offset 0 */
330   if (offset == 0) {
331     GST_BUFFER_TIMESTAMP (buffer)    = GST_BUFFER_TIMESTAMP (parent);
332     GST_BUFFER_OFFSET (buffer)       = GST_BUFFER_OFFSET (parent);
333   }
334   else {
335     GST_BUFFER_TIMESTAMP (buffer)    = GST_CLOCK_TIME_NONE;
336     GST_BUFFER_OFFSET (buffer)       = GST_BUFFER_OFFSET_NONE;
337   }
338   GST_BUFFER_DURATION (buffer)     = GST_CLOCK_TIME_NONE;
339
340   /* make sure nobody overwrites data as it would overwrite in the parent.
341    * data in parent cannot be overwritten because we hold a ref */
342   GST_DATA_FLAG_SET (parent, GST_DATA_READONLY);
343
344   return buffer;
345 }
346
347
348 /**
349  * gst_buffer_merge:
350  * @buf1: a first source #GstBuffer to merge.
351  * @buf2: the second source #GstBuffer to merge.
352  *
353  * Create a new buffer that is the concatenation of the two source
354  * buffers.  The original source buffers will not be modified or
355  * unref'd.
356  *
357  * Internally is nothing more than a specialized gst_buffer_span(),
358  * so the same optimizations can occur.
359  *
360  * Returns: the new #GstBuffer that's the concatenation of the source buffers.
361  */
362 GstBuffer*
363 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
364 {
365   GstBuffer *result;
366
367   /* we're just a specific case of the more general gst_buffer_span() */
368   result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
369
370   return result;
371 }
372
373 /**
374  * gst_buffer_is_span_fast:
375  * @buf1: a first source #GstBuffer.
376  * @buf2: the second source #GstBuffer.
377  *
378  * Determines whether a gst_buffer_span() is free (as in free beer), 
379  * or requires a memcpy. 
380  *
381  * Returns: TRUE if the buffers are contiguous, 
382  * FALSE if a copy would be required.
383  */
384 gboolean
385 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
386 {
387   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
388   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
389   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
390
391   /* it's only fast if we have subbuffers of the same parent */
392   return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
393           (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
394           (buf1->pool_private == buf2->pool_private) &&
395           ((buf1->data + buf1->size) == buf2->data));
396 }
397
398 /**
399  * gst_buffer_span:
400  * @buf1: a first source #GstBuffer to merge.
401  * @offset: the offset in the first buffer from where the new
402  * buffer should start.
403  * @buf2: the second source #GstBuffer to merge.
404  * @len: the total length of the new buffer.
405  *
406  * Creates a new buffer that consists of part of buf1 and buf2.
407  * Logically, buf1 and buf2 are concatenated into a single larger
408  * buffer, and a new buffer is created at the given offset inside
409  * this space, with a given length.
410  *
411  * If the two source buffers are children of the same larger buffer,
412  * and are contiguous, the new buffer will be a child of the shared
413  * parent, and thus no copying is necessary. you can use 
414  * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
415  *
416  * Returns: the new #GstBuffer that spans the two source buffers.
417  */
418 GstBuffer*
419 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
420 {
421   GstBuffer *newbuf;
422
423   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
424   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
425   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
426   g_return_val_if_fail (len > 0, NULL);
427   g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
428
429   /* if the two buffers have the same parent and are adjacent */
430   if (gst_buffer_is_span_fast (buf1, buf2)) {
431     GstBuffer *parent = GST_BUFFER (buf1->pool_private);
432     /* we simply create a subbuffer of the common parent */
433     newbuf = gst_buffer_create_sub (parent, 
434                                     buf1->data - parent->data + offset, len);
435   }
436   else {
437     GST_CAT_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p", 
438                buf1, buf2);
439     /* otherwise we simply have to brute-force copy the buffers */
440     newbuf = gst_buffer_new_and_alloc (len);
441
442     /* copy the first buffer's data across */
443     memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
444     /* copy the second buffer's data across */
445     memcpy (newbuf->data + (buf1->size - offset), buf2->data, 
446             len - (buf1->size - offset));
447     /* if the offset is 0, the new buffer has the same timestamp as buf1 */
448     if (offset == 0) {
449       GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
450       GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
451     }
452   }
453   /* if we completely merged the two buffers (appended), we can
454    * calculate the duration too. Also make sure we's not messing with
455    * invalid DURATIONS */
456   if (offset == 0 && buf1->size + buf2->size == len &&
457       GST_BUFFER_DURATION_IS_VALID (buf1) &&
458       GST_BUFFER_DURATION_IS_VALID (buf2)) 
459   {
460     /* add duration */
461     GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) + 
462                                    GST_BUFFER_DURATION (buf2);
463   }
464
465   return newbuf;
466 }
467
468 GType
469 gst_buffer_pool_get_type (void)
470 {
471   return _gst_buffer_pool_type;
472 }
473
474 /**
475  * gst_buffer_pool_default_free:
476  * @pool: a #GstBufferPool to free.
477  *
478  * Frees the memory associated with the bufferpool.
479  */
480 void
481 gst_buffer_pool_default_free (GstBufferPool *pool)
482 {
483   g_return_if_fail (pool != NULL);
484
485   _GST_DATA_DISPOSE (GST_DATA (pool));
486   g_free (pool);
487 #ifndef GST_DISABLE_TRACE
488   gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
489 #endif
490 }
491
492 /** 
493  * gst_buffer_pool_new:
494  * @free: the #GstDataFreeFunction to free the buffer pool.
495  * @copy: the #GstDataCopyFunction to copy the buffer pool.
496  * @buffer_new: the #GstBufferPoolBufferNewFunction to create a new buffer 
497  *              from this pool
498  * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
499  *               from this pool
500  * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer 
501  *               in this pool
502  * @user_data: the user data gpointer passed to buffer_* functions.
503  *
504  * Creates a new buffer pool with the given functions.
505  *
506  * Returns: a new #GstBufferPool, or NULL on error.
507  */
508 GstBufferPool*  
509 gst_buffer_pool_new (GstDataFreeFunction free,
510                      GstDataCopyFunction copy,
511                      GstBufferPoolBufferNewFunction buffer_new,
512                      GstBufferPoolBufferCopyFunction buffer_copy,
513                      GstBufferPoolBufferFreeFunction buffer_free,
514                      gpointer user_data)
515 {
516   GstBufferPool *pool;
517
518   /* we need at least a buffer_new function */
519   g_return_val_if_fail (buffer_new != NULL, NULL);
520
521   pool = g_new0 (GstBufferPool, 1);
522 #ifndef GST_DISABLE_TRACE
523   gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
524 #endif
525
526   GST_CAT_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
527         
528   /* init data struct */
529   _GST_DATA_INIT (GST_DATA (pool), 
530                   _gst_buffer_pool_type,
531                   0,
532                   (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
533                   copy);
534             
535   /* set functions */
536   pool->buffer_new = buffer_new;
537   pool->buffer_copy = buffer_copy;
538   pool->buffer_free = buffer_free;
539   pool->user_data = user_data;
540                     
541   return pool;
542 }
543
544 /**
545  * gst_buffer_pool_is_active:
546  * @pool: the #GstBufferPool to query.
547  *
548  * Queries if the given buffer pool is active.
549  *
550  * Returns: TRUE if the pool is active.
551  */
552 gboolean
553 gst_buffer_pool_is_active (GstBufferPool *pool)
554 {
555   g_return_val_if_fail (pool != NULL, FALSE);
556
557   return pool->active;
558 }
559
560 /**
561  * gst_buffer_pool_set_active:
562  * @pool: a #GstBufferPool to set the activity status on.
563  * @active: the new status of the pool.
564  *
565  * Sets the given pool to the active or inactive state depending on the
566  * active parameter.
567  */
568 void
569 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
570 {
571   g_return_if_fail (pool != NULL);
572
573   pool->active = active;
574 }
575
576 /**
577  * gst_buffer_pool_set_user_data:
578  * @pool: the #GstBufferPool to set the user data for.
579  * @user_data: the user_data to set on the buffer pool.
580  *
581  * Sets the given user data on the buffer pool.
582  */
583 void
584 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
585 {
586   g_return_if_fail (pool != NULL);
587
588   pool->user_data = user_data;
589 }
590
591 /**
592  * gst_buffer_pool_get_user_data:
593  * @pool: the #GstBufferPool to get the user data for.
594  *
595  * Gets the user data of the buffer pool.
596  * 
597  * Returns: the user data associated with this buffer pool.
598  */
599 gpointer
600 gst_buffer_pool_get_user_data (GstBufferPool *pool)
601 {
602   g_return_val_if_fail (pool != NULL, NULL);
603
604   return pool->user_data;
605 }