only update the DURATION field when appending buffers and both buffers have a valid...
[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 GType
70 gst_buffer_get_type (void)
71 {
72   return _gst_buffer_type;
73 }
74
75 static void
76 _gst_buffer_free_to_pool (GstBuffer *buffer)
77 {
78   GstBufferPool *pool = buffer->pool;
79
80   pool->buffer_free (pool, buffer, pool->user_data);
81
82   gst_data_unref (GST_DATA (pool));
83 }
84
85 static void
86 _gst_buffer_sub_free (GstBuffer *buffer)
87 {
88   gst_data_unref (GST_DATA (buffer->pool_private));
89
90   GST_BUFFER_DATA (buffer) = NULL;
91   GST_BUFFER_SIZE (buffer) = 0;
92
93   _GST_DATA_DISPOSE (GST_DATA (buffer));
94   
95   gst_mem_chunk_free (chunk, GST_DATA (buffer));
96 #ifndef GST_DISABLE_TRACE
97   gst_alloc_trace_free (_gst_buffer_trace, buffer);
98 #endif
99 }
100
101 /**
102  * gst_buffer_default_free:
103  * @buffer: a #GstBuffer to free.
104  *
105  * Frees the memory associated with the buffer including the buffer data,
106  * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
107  * This function is used by buffer pools.
108  */
109 void
110 gst_buffer_default_free (GstBuffer *buffer)
111 {
112   g_return_if_fail (buffer != NULL);
113
114   /* free our data */
115   if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer)) 
116     g_free (GST_BUFFER_DATA (buffer));
117
118   /* set to safe values */
119   GST_BUFFER_DATA (buffer) = NULL;
120   GST_BUFFER_SIZE (buffer) = 0;
121
122   _GST_DATA_DISPOSE (GST_DATA (buffer));
123
124   gst_mem_chunk_free (chunk, GST_DATA (buffer));
125 #ifndef GST_DISABLE_TRACE
126   gst_alloc_trace_free (_gst_buffer_trace, buffer);
127 #endif
128 }
129
130 static GstBuffer*
131 _gst_buffer_copy_from_pool (GstBuffer *buffer)
132 {
133   return buffer->pool->buffer_copy (buffer->pool, buffer, buffer->pool->user_data);
134 }
135
136 /**
137  * gst_buffer_default_copy:
138  * @buffer: a #GstBuffer to make a copy of.
139  *
140  * Make a full newly allocated copy of the given buffer, data and all.
141  * This function is used by buffer pools.
142  *
143  * Returns: the new #GstBuffer.
144  */
145 GstBuffer*
146 gst_buffer_default_copy (GstBuffer *buffer)
147 {
148   GstBuffer *copy;
149
150   g_return_val_if_fail (buffer != NULL, NULL);
151
152   /* create a fresh new buffer */
153   copy = gst_mem_chunk_alloc (chunk);
154 #ifndef GST_DISABLE_TRACE
155   gst_alloc_trace_new (_gst_buffer_trace, copy);
156 #endif
157
158   _GST_DATA_INIT (GST_DATA (copy), 
159                   _gst_buffer_type,
160                   0,
161                   (GstDataFreeFunction) gst_buffer_default_free,
162                   (GstDataCopyFunction) gst_buffer_default_copy);
163
164   /* we simply copy everything from our parent */
165   GST_BUFFER_DATA (copy)         = g_memdup (GST_BUFFER_DATA (buffer), 
166                                              GST_BUFFER_SIZE (buffer));
167   GST_BUFFER_SIZE (copy)         = GST_BUFFER_SIZE (buffer);
168   GST_BUFFER_MAXSIZE (copy)      = GST_BUFFER_SIZE (buffer);
169   GST_BUFFER_TIMESTAMP (copy)    = GST_BUFFER_TIMESTAMP (buffer);
170   GST_BUFFER_DURATION (copy)     = GST_BUFFER_DURATION (buffer);
171   GST_BUFFER_OFFSET (copy)       = GST_BUFFER_OFFSET (buffer);
172   GST_BUFFER_BUFFERPOOL (copy)   = NULL;
173   GST_BUFFER_POOL_PRIVATE (copy) = NULL;
174
175   return copy;
176 }
177
178 /**
179  * gst_buffer_new:
180  *
181  * Creates a newly allocated buffer without any data.
182  *
183  * Returns: the new #GstBuffer.
184  */
185 GstBuffer*
186 gst_buffer_new (void)
187 {
188   GstBuffer *newbuf;
189   
190   newbuf = gst_mem_chunk_alloc (chunk);
191 #ifndef GST_DISABLE_TRACE
192   gst_alloc_trace_new (_gst_buffer_trace, newbuf);
193 #endif
194
195   GST_DEBUG (GST_CAT_BUFFER, "new %p", newbuf);
196
197   _GST_DATA_INIT (GST_DATA (newbuf), 
198                   _gst_buffer_type,
199                   0,
200                   (GstDataFreeFunction) gst_buffer_default_free,
201                   (GstDataCopyFunction) gst_buffer_default_copy);
202
203   GST_BUFFER_DATA (newbuf)         = NULL;
204   GST_BUFFER_SIZE (newbuf)         = 0;
205   GST_BUFFER_MAXSIZE (newbuf)      = GST_BUFFER_MAXSIZE_NONE;
206   GST_BUFFER_TIMESTAMP (newbuf)    = GST_CLOCK_TIME_NONE;
207   GST_BUFFER_DURATION (newbuf)     = GST_CLOCK_TIME_NONE;
208   GST_BUFFER_OFFSET (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   /* make sure nobody overwrites data in the parent */
307   GST_DATA_FLAG_SET (parent, GST_DATA_READONLY);
308
309   /* create the new buffer */
310   buffer = gst_mem_chunk_alloc (chunk);
311 #ifndef GST_DISABLE_TRACE
312   gst_alloc_trace_new (_gst_buffer_trace, buffer);
313 #endif
314
315   GST_DEBUG (GST_CAT_BUFFER, "new %p", buf);
316
317   /* make sure nobody overwrites data in the new buffer 
318    * by setting the READONLY flag */
319   _GST_DATA_INIT (GST_DATA (buffer), 
320                   _gst_buffer_type,
321                   GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
322                   GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
323                   (GstDataFreeFunction) _gst_buffer_sub_free,
324                   (GstDataCopyFunction) gst_buffer_default_copy);
325
326   /* set the right values in the child */
327   GST_BUFFER_DATA (buffer)         = buffer_data;
328   GST_BUFFER_SIZE (buffer)         = size;
329   GST_BUFFER_MAXSIZE (buffer)      = size;
330   GST_BUFFER_BUFFERPOOL (buffer)   = NULL;
331   GST_BUFFER_POOL_PRIVATE (buffer) = parent;
332   /* we can copy the timestamp and offset if the new buffer starts at
333    * offset 0 */
334   if (offset == 0) {
335     GST_BUFFER_TIMESTAMP (buffer)    = GST_BUFFER_TIMESTAMP (parent);
336     GST_BUFFER_OFFSET (buffer)       = GST_BUFFER_OFFSET (parent);
337   }
338   else {
339     GST_BUFFER_TIMESTAMP (buffer)    = GST_CLOCK_TIME_NONE;
340     GST_BUFFER_OFFSET (buffer)       = GST_BUFFER_OFFSET_NONE;
341   }
342
343   GST_BUFFER_DURATION (buffer)     = GST_CLOCK_TIME_NONE;
344
345   return buffer;
346 }
347
348
349 /**
350  * gst_buffer_merge:
351  * @buf1: a first source #GstBuffer to merge.
352  * @buf2: the second source #GstBuffer to merge.
353  *
354  * Create a new buffer that is the concatenation of the two source
355  * buffers.  The original source buffers will not be modified or
356  * unref'd.
357  *
358  * Internally is nothing more than a specialized gst_buffer_span(),
359  * so the same optimizations can occur.
360  *
361  * Returns: the new #GstBuffer that's the concatenation of the source buffers.
362  */
363 GstBuffer*
364 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
365 {
366   GstBuffer *result;
367
368   /* we're just a specific case of the more general gst_buffer_span() */
369   result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
370
371   return result;
372 }
373
374 /**
375  * gst_buffer_is_span_fast:
376  * @buf1: a first source #GstBuffer.
377  * @buf2: the second source #GstBuffer.
378  *
379  * Determines whether a gst_buffer_span() is free (as in free beer), 
380  * or requires a memcpy. 
381  *
382  * Returns: TRUE if the buffers are contiguous, 
383  * FALSE if a copy would be required.
384  */
385 gboolean
386 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
387 {
388   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
389   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
390   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
391
392   /* it's only fast if we have subbuffers of the same parent */
393   return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
394           (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
395           (buf1->pool_private == buf2->pool_private) &&
396           ((buf1->data + buf1->size) == buf2->data));
397 }
398
399 /**
400  * gst_buffer_span:
401  * @buf1: a first source #GstBuffer to merge.
402  * @offset: the offset in the first buffer from where the new
403  * buffer should start.
404  * @buf2: the second source #GstBuffer to merge.
405  * @len: the total length of the new buffer.
406  *
407  * Creates a new buffer that consists of part of buf1 and buf2.
408  * Logically, buf1 and buf2 are concatenated into a single larger
409  * buffer, and a new buffer is created at the given offset inside
410  * this space, with a given length.
411  *
412  * If the two source buffers are children of the same larger buffer,
413  * and are contiguous, the new buffer will be a child of the shared
414  * parent, and thus no copying is necessary. you can use 
415  * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
416  *
417  * Returns: the new #GstBuffer that spans the two source buffers.
418  */
419 GstBuffer*
420 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
421 {
422   GstBuffer *newbuf;
423
424   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
425   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
426   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
427   g_return_val_if_fail (len > 0, NULL);
428   g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
429
430   /* if the two buffers have the same parent and are adjacent */
431   if (gst_buffer_is_span_fast (buf1, buf2)) {
432     GstBuffer *parent = GST_BUFFER (buf1->pool_private);
433     /* we simply create a subbuffer of the common parent */
434     newbuf = gst_buffer_create_sub (parent, 
435                                     buf1->data - parent->data + offset, len);
436   }
437   else {
438     GST_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p", 
439                buf1, buf2);
440     /* otherwise we simply have to brute-force copy the buffers */
441     newbuf = gst_buffer_new_and_alloc (len);
442
443     /* copy the first buffer's data across */
444     memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
445     /* copy the second buffer's data across */
446     memcpy (newbuf->data + (buf1->size - offset), buf2->data, 
447             len - (buf1->size - offset));
448     /* if the offset is 0, the new buffer has the same timestamp as buf1 */
449     if (offset == 0) {
450       GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
451       GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
452     }
453   }
454   /* if we completely merged the two buffers (appended), we can
455    * calculate the duration too. Also make sure we's not messing with
456    * invalid DURATIONS */
457   if (offset == 0 && buf1->size + buf2->size == len &&
458       GST_BUFFER_DURATION_IS_VALID (buf1) &&
459       GST_BUFFER_DURATION_IS_VALID (buf2)) 
460   {
461     /* add duration */
462     GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) + 
463                                    GST_BUFFER_DURATION (buf2);
464   }
465
466   return newbuf;
467 }
468
469 GType
470 gst_buffer_pool_get_type (void)
471 {
472   return _gst_buffer_pool_type;
473 }
474
475 /**
476  * gst_buffer_pool_default_free:
477  * @pool: a #GstBufferPool to free.
478  *
479  * Frees the memory associated with the bufferpool.
480  */
481 void
482 gst_buffer_pool_default_free (GstBufferPool *pool)
483 {
484   g_return_if_fail (pool != NULL);
485
486   _GST_DATA_DISPOSE (GST_DATA (pool));
487   g_free (pool);
488 #ifndef GST_DISABLE_TRACE
489   gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
490 #endif
491 }
492
493 /** 
494  * gst_buffer_pool_new:
495  * @free: the #GstDataFreeFunction to free the buffer pool.
496  * @copy: the #GstDataCopyFunction to copy the buffer pool.
497  * @buffer_new: the #GstBufferPoolBufferNewFunction to create a new buffer 
498  *              from this pool
499  * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
500  *               from this pool
501  * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer 
502  *               in this pool
503  * @user_data: the user data gpointer passed to buffer_* functions.
504  *
505  * Creates a new buffer pool with the given functions.
506  *
507  * Returns: a new #GstBufferPool, or NULL on error.
508  */
509 GstBufferPool*  
510 gst_buffer_pool_new (GstDataFreeFunction free,
511                      GstDataCopyFunction copy,
512                      GstBufferPoolBufferNewFunction buffer_new,
513                      GstBufferPoolBufferCopyFunction buffer_copy,
514                      GstBufferPoolBufferFreeFunction buffer_free,
515                      gpointer user_data)
516 {
517   GstBufferPool *pool;
518
519   /* we need at least a buffer_new function */
520   g_return_val_if_fail (buffer_new != NULL, NULL);
521
522   pool = g_new0 (GstBufferPool, 1);
523 #ifndef GST_DISABLE_TRACE
524   gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
525 #endif
526
527   GST_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
528         
529   /* init data struct */
530   _GST_DATA_INIT (GST_DATA (pool), 
531                   _gst_buffer_pool_type,
532                   0,
533                   (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
534                   copy);
535             
536   /* set functions */
537   pool->buffer_new = buffer_new;
538   pool->buffer_copy = buffer_copy;
539   pool->buffer_free = buffer_free;
540   pool->user_data = user_data;
541                     
542   return pool;
543 }
544
545 /**
546  * gst_buffer_pool_is_active:
547  * @pool: the #GstBufferPool to query.
548  *
549  * Queries if the given buffer pool is active.
550  *
551  * Returns: TRUE if the pool is active.
552  */
553 gboolean
554 gst_buffer_pool_is_active (GstBufferPool *pool)
555 {
556   g_return_val_if_fail (pool != NULL, FALSE);
557
558   return pool->active;
559 }
560
561 /**
562  * gst_buffer_pool_set_active:
563  * @pool: a #GstBufferPool to set the activity status on.
564  * @active: the new status of the pool.
565  *
566  * Sets the given pool to the active or inactive state depending on the
567  * active parameter.
568  */
569 void
570 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
571 {
572   g_return_if_fail (pool != NULL);
573
574   pool->active = active;
575 }
576
577 /**
578  * gst_buffer_pool_set_user_data:
579  * @pool: the #GstBufferPool to set the user data for.
580  * @user_data: the user_data to set on the buffer pool.
581  *
582  * Sets the given user data on the buffer pool.
583  */
584 void
585 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
586 {
587   g_return_if_fail (pool != NULL);
588
589   pool->user_data = user_data;
590 }
591
592 /**
593  * gst_buffer_pool_get_user_data:
594  * @pool: the #GstBufferPool to get the user data for.
595  *
596  * Gets the user data of the buffer pool.
597  * 
598  * Returns: the user data associated with this buffer pool.
599  */
600 gpointer
601 gst_buffer_pool_get_user_data (GstBufferPool *pool)
602 {
603   g_return_val_if_fail (pool != NULL, NULL);
604
605   return pool->user_data;
606 }