merge in tagging
[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_OFFSET_END (copy)   = GST_BUFFER_OFFSET_END (buffer);
171   GST_BUFFER_BUFFERPOOL (copy)   = NULL;
172   GST_BUFFER_POOL_PRIVATE (copy) = NULL;
173
174   return copy;
175 }
176
177 /**
178  * gst_buffer_new:
179  *
180  * Creates a newly allocated buffer without any data.
181  *
182  * Returns: the new #GstBuffer.
183  */
184 GstBuffer*
185 gst_buffer_new (void)
186 {
187   GstBuffer *newbuf;
188   
189   newbuf = gst_mem_chunk_alloc (chunk);
190 #ifndef GST_DISABLE_TRACE
191   gst_alloc_trace_new (_gst_buffer_trace, newbuf);
192 #endif
193
194   GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
195
196   _GST_DATA_INIT (GST_DATA (newbuf), 
197                   _gst_buffer_type,
198                   0,
199                   (GstDataFreeFunction) gst_buffer_default_free,
200                   (GstDataCopyFunction) gst_buffer_default_copy);
201
202   GST_BUFFER_DATA (newbuf)         = NULL;
203   GST_BUFFER_SIZE (newbuf)         = 0;
204   GST_BUFFER_MAXSIZE (newbuf)      = GST_BUFFER_MAXSIZE_NONE;
205   GST_BUFFER_TIMESTAMP (newbuf)    = GST_CLOCK_TIME_NONE;
206   GST_BUFFER_DURATION (newbuf)     = GST_CLOCK_TIME_NONE;
207   GST_BUFFER_OFFSET (newbuf)       = GST_BUFFER_OFFSET_NONE;
208   GST_BUFFER_OFFSET_END (newbuf)   = GST_BUFFER_OFFSET_NONE;
209   GST_BUFFER_BUFFERPOOL (newbuf)   = NULL;
210   GST_BUFFER_POOL_PRIVATE (newbuf) = NULL;
211
212   return newbuf;
213 }
214
215 /**
216  * gst_buffer_new_and_alloc:
217  * @size: the size of the new buffer's data.
218  *
219  * Creates a newly allocated buffer with data of the given size.
220  *
221  * Returns: the new #GstBuffer.
222  */
223 GstBuffer*
224 gst_buffer_new_and_alloc (guint size)
225 {
226   GstBuffer *newbuf;
227
228   newbuf = gst_buffer_new ();
229
230   GST_BUFFER_DATA (newbuf)    = g_malloc (size);
231   GST_BUFFER_SIZE (newbuf)    = size;
232   GST_BUFFER_MAXSIZE (newbuf) = size;
233
234   return newbuf;
235 }
236
237 /**
238  * gst_buffer_new_from_pool:
239  * @pool: a #GstBufferPool to use.
240  * @offset: the offset of the new buffer.
241  * @size: the size of the new buffer.
242  *
243  * Creates a newly allocated buffer using the specified buffer pool, 
244  * offset and size.
245  *
246  * Returns: the new #GstBuffer, or NULL if there was an error.
247  */
248 GstBuffer*
249 gst_buffer_new_from_pool (GstBufferPool *pool, 
250                           guint64 offset, guint size)
251 {
252   GstBuffer *buffer;
253   
254   g_return_val_if_fail (pool != NULL, NULL);
255
256   gst_data_ref (GST_DATA (pool));
257
258   buffer = pool->buffer_new (pool, offset, size, pool->user_data);
259   if (!buffer)
260     return NULL;
261
262   GST_BUFFER_BUFFERPOOL (buffer) = pool;
263
264   /* override the buffer refcount functions with those from the pool (if any) */
265   if (pool->buffer_free)
266     GST_DATA (buffer)->free = (GstDataFreeFunction)_gst_buffer_free_to_pool;
267   if (pool->buffer_copy)
268     GST_DATA (buffer)->copy = (GstDataCopyFunction)_gst_buffer_copy_from_pool;
269
270   return buffer;
271 }
272
273 /**
274  * gst_buffer_create_sub:
275  * @parent: a parent #GstBuffer to create a subbuffer from.
276  * @offset: the offset into parent #GstBuffer.
277  * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
278  *
279  * Creates a sub-buffer from the parent at a given offset.
280  * This sub-buffer uses the actual memory space of the parent buffer.
281  * This function will copy the offset and timestamp field when the 
282  * offset is 0, else they are set to _NONE.
283  * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
284  *
285  * Returns: the new #GstBuffer, or NULL if there was an error.
286  */
287 GstBuffer*
288 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
289 {
290   GstBuffer *buffer;
291   gpointer buffer_data;
292               
293   g_return_val_if_fail (parent != NULL, NULL);
294   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
295   g_return_val_if_fail (size > 0, NULL);
296   g_return_val_if_fail (parent->size >= offset + size, NULL);
297
298   /* remember the data for the new buffer */
299   buffer_data = parent->data + offset;
300   /* make sure we're child not child from a child buffer */
301   while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
302     parent = GST_BUFFER (parent->pool_private);
303   }
304   /* ref the real parent */
305   gst_data_ref (GST_DATA (parent));
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   GST_BUFFER_OFFSET_END (buffer)   = GST_BUFFER_OFFSET_NONE;
343   /* make sure nobody overwrites data as it would overwrite in the parent.
344    * data in parent cannot be overwritten because we hold a ref */
345   GST_DATA_FLAG_SET (parent, GST_DATA_READONLY);
346
347   return buffer;
348 }
349
350
351 /**
352  * gst_buffer_merge:
353  * @buf1: a first source #GstBuffer to merge.
354  * @buf2: the second source #GstBuffer to merge.
355  *
356  * Create a new buffer that is the concatenation of the two source
357  * buffers.  The original source buffers will not be modified or
358  * unref'd.
359  *
360  * Internally is nothing more than a specialized gst_buffer_span(),
361  * so the same optimizations can occur.
362  *
363  * Returns: the new #GstBuffer that's the concatenation of the source buffers.
364  */
365 GstBuffer*
366 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
367 {
368   GstBuffer *result;
369
370   /* we're just a specific case of the more general gst_buffer_span() */
371   result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
372
373   return result;
374 }
375
376 /**
377  * gst_buffer_is_span_fast:
378  * @buf1: a first source #GstBuffer.
379  * @buf2: the second source #GstBuffer.
380  *
381  * Determines whether a gst_buffer_span() is free (as in free beer), 
382  * or requires a memcpy. 
383  *
384  * Returns: TRUE if the buffers are contiguous, 
385  * FALSE if a copy would be required.
386  */
387 gboolean
388 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
389 {
390   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
391   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
392   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
393
394   /* it's only fast if we have subbuffers of the same parent */
395   return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
396           (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
397           (buf1->pool_private == buf2->pool_private) &&
398           ((buf1->data + buf1->size) == buf2->data));
399 }
400
401 /**
402  * gst_buffer_span:
403  * @buf1: a first source #GstBuffer to merge.
404  * @offset: the offset in the first buffer from where the new
405  * buffer should start.
406  * @buf2: the second source #GstBuffer to merge.
407  * @len: the total length of the new buffer.
408  *
409  * Creates a new buffer that consists of part of buf1 and buf2.
410  * Logically, buf1 and buf2 are concatenated into a single larger
411  * buffer, and a new buffer is created at the given offset inside
412  * this space, with a given length.
413  *
414  * If the two source buffers are children of the same larger buffer,
415  * and are contiguous, the new buffer will be a child of the shared
416  * parent, and thus no copying is necessary. you can use 
417  * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
418  *
419  * Returns: the new #GstBuffer that spans the two source buffers.
420  */
421 GstBuffer*
422 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
423 {
424   GstBuffer *newbuf;
425
426   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
427   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
428   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
429   g_return_val_if_fail (len > 0, NULL);
430   g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
431
432   /* if the two buffers have the same parent and are adjacent */
433   if (gst_buffer_is_span_fast (buf1, buf2)) {
434     GstBuffer *parent = GST_BUFFER (buf1->pool_private);
435     /* we simply create a subbuffer of the common parent */
436     newbuf = gst_buffer_create_sub (parent, 
437                                     buf1->data - parent->data + offset, len);
438   }
439   else {
440     GST_CAT_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p", 
441                buf1, buf2);
442     /* otherwise we simply have to brute-force copy the buffers */
443     newbuf = gst_buffer_new_and_alloc (len);
444
445     /* copy the first buffer's data across */
446     memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
447     /* copy the second buffer's data across */
448     memcpy (newbuf->data + (buf1->size - offset), buf2->data, 
449             len - (buf1->size - offset));
450     /* if the offset is 0, the new buffer has the same timestamp as buf1 */
451     if (offset == 0) {
452       GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
453       GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
454     }
455   }
456   /* if we completely merged the two buffers (appended), we can
457    * calculate the duration too. Also make sure we's not messing with
458    * invalid DURATIONS */
459   if (offset == 0 && buf1->size + buf2->size == len) {
460     if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
461         GST_BUFFER_DURATION_IS_VALID (buf2)) {
462       /* add duration */
463       GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) + 
464                                     GST_BUFFER_DURATION (buf2);
465     }
466     if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
467       /* add offset_end */
468       GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
469     }
470   }
471
472   return newbuf;
473 }
474
475 GType
476 gst_buffer_pool_get_type (void)
477 {
478   return _gst_buffer_pool_type;
479 }
480
481 /**
482  * gst_buffer_pool_default_free:
483  * @pool: a #GstBufferPool to free.
484  *
485  * Frees the memory associated with the bufferpool.
486  */
487 void
488 gst_buffer_pool_default_free (GstBufferPool *pool)
489 {
490   g_return_if_fail (pool != NULL);
491
492   _GST_DATA_DISPOSE (GST_DATA (pool));
493   g_free (pool);
494 #ifndef GST_DISABLE_TRACE
495   gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
496 #endif
497 }
498
499 /** 
500  * gst_buffer_pool_new:
501  * @free: the #GstDataFreeFunction to free the buffer pool.
502  * @copy: the #GstDataCopyFunction to copy the buffer pool.
503  * @buffer_new: the #GstBufferPoolBufferNewFunction to create a new buffer 
504  *              from this pool
505  * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
506  *               from this pool
507  * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer 
508  *               in this pool
509  * @user_data: the user data gpointer passed to buffer_* functions.
510  *
511  * Creates a new buffer pool with the given functions.
512  *
513  * Returns: a new #GstBufferPool, or NULL on error.
514  */
515 GstBufferPool*  
516 gst_buffer_pool_new (GstDataFreeFunction free,
517                      GstDataCopyFunction copy,
518                      GstBufferPoolBufferNewFunction buffer_new,
519                      GstBufferPoolBufferCopyFunction buffer_copy,
520                      GstBufferPoolBufferFreeFunction buffer_free,
521                      gpointer user_data)
522 {
523   GstBufferPool *pool;
524
525   /* we need at least a buffer_new function */
526   g_return_val_if_fail (buffer_new != NULL, NULL);
527
528   pool = g_new0 (GstBufferPool, 1);
529 #ifndef GST_DISABLE_TRACE
530   gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
531 #endif
532
533   GST_CAT_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
534         
535   /* init data struct */
536   _GST_DATA_INIT (GST_DATA (pool), 
537                   _gst_buffer_pool_type,
538                   0,
539                   (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
540                   copy);
541             
542   /* set functions */
543   pool->buffer_new = buffer_new;
544   pool->buffer_copy = buffer_copy;
545   pool->buffer_free = buffer_free;
546   pool->user_data = user_data;
547                     
548   return pool;
549 }
550
551 /**
552  * gst_buffer_pool_is_active:
553  * @pool: the #GstBufferPool to query.
554  *
555  * Queries if the given buffer pool is active.
556  *
557  * Returns: TRUE if the pool is active.
558  */
559 gboolean
560 gst_buffer_pool_is_active (GstBufferPool *pool)
561 {
562   g_return_val_if_fail (pool != NULL, FALSE);
563
564   return pool->active;
565 }
566
567 /**
568  * gst_buffer_pool_set_active:
569  * @pool: a #GstBufferPool to set the activity status on.
570  * @active: the new status of the pool.
571  *
572  * Sets the given pool to the active or inactive state depending on the
573  * active parameter.
574  */
575 void
576 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
577 {
578   g_return_if_fail (pool != NULL);
579
580   pool->active = active;
581 }
582
583 /**
584  * gst_buffer_pool_set_user_data:
585  * @pool: the #GstBufferPool to set the user data for.
586  * @user_data: the user_data to set on the buffer pool.
587  *
588  * Sets the given user data on the buffer pool.
589  */
590 void
591 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
592 {
593   g_return_if_fail (pool != NULL);
594
595   pool->user_data = user_data;
596 }
597
598 /**
599  * gst_buffer_pool_get_user_data:
600  * @pool: the #GstBufferPool to get the user data for.
601  *
602  * Gets the user data of the buffer pool.
603  * 
604  * Returns: the user data associated with this buffer pool.
605  */
606 gpointer
607 gst_buffer_pool_get_user_data (GstBufferPool *pool)
608 {
609   g_return_val_if_fail (pool != NULL, NULL);
610
611   return pool->user_data;
612 }