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