add default bufferpool, clean up some code, add bufferpool testing to fakesrc
[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 "gstdata_private.h"
28 #include "gstbuffer.h"
29 #include "gstmemchunk.h"
30 #include "gstlog.h"
31 #include "gstbufferpool-default.h"
32
33 GType _gst_buffer_type;
34 GType _gst_buffer_pool_type;
35
36 static gint _gst_buffer_live;
37 static gint _gst_buffer_pool_live;
38
39 static GstMemChunk *chunk;
40
41 void
42 _gst_buffer_initialize (void)
43 {
44   _gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
45                                                    (GBoxedCopyFunc) gst_data_ref,
46                                                    (GBoxedFreeFunc) gst_data_unref);
47
48   _gst_buffer_pool_type = g_boxed_type_register_static ("GstBufferPool",
49                                                    (GBoxedCopyFunc) gst_data_ref,
50                                                    (GBoxedFreeFunc) gst_data_unref);
51
52   _gst_buffer_live = 0;
53
54   chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer), sizeof (GstBuffer) * 200, 0);
55
56   GST_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
57 }
58
59 /**
60  * gst_buffer_print_stats:
61  *
62  * Logs statistics about live buffers (using g_log).
63  */
64 void
65 gst_buffer_print_stats (void)
66 {
67   g_log (g_log_domain_gstreamer, G_LOG_LEVEL_INFO,
68                   "%d live buffer(s)", _gst_buffer_live);
69   g_log (g_log_domain_gstreamer, G_LOG_LEVEL_INFO,
70                   "%d live bufferpool(s)", _gst_buffer_pool_live);
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   _gst_buffer_live--;
95 }
96
97 /**
98  * gst_buffer_default_free:
99  * @buffer: a #GstBuffer to free
100  *
101  * Free the memory associated with the buffer including the buffer data,
102  * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
103  * This function is used by bufferpools.
104  */
105 void
106 gst_buffer_default_free (GstBuffer *buffer)
107 {
108   /* free our data */
109   if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer)) 
110     g_free (GST_BUFFER_DATA (buffer));
111
112   /* set to safe values */
113   GST_BUFFER_DATA (buffer) = NULL;
114   GST_BUFFER_SIZE (buffer) = 0;
115
116   _GST_DATA_DISPOSE (GST_DATA (buffer));
117
118   gst_mem_chunk_free (chunk, GST_DATA (buffer));
119   _gst_buffer_live--;
120 }
121
122 static GstBuffer*
123 _gst_buffer_copy_from_pool (GstBuffer *buffer)
124 {
125   return buffer->pool->buffer_copy (buffer->pool, buffer, buffer->pool->user_data);
126 }
127
128 /**
129  * gst_buffer_default_copy:
130  * @buffer: a #GstBuffer to make a copy of
131  *
132  * Make a full newly allocated copy of the given buffer, data and all.
133  * This function is used by bufferpools.
134  *
135  * Returns: new #GstBuffer
136  */
137 GstBuffer*
138 gst_buffer_default_copy (GstBuffer *buffer)
139 {
140   GstBuffer *copy;
141
142   /* create a fresh new buffer */
143   copy = gst_buffer_new ();
144
145   /* we simply copy everything from our parent */
146   GST_BUFFER_DATA (copy)        = g_memdup (GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
147   GST_BUFFER_SIZE (copy)        = GST_BUFFER_SIZE (buffer);
148   GST_BUFFER_MAXSIZE (copy)     = GST_BUFFER_MAXSIZE (buffer);
149   GST_BUFFER_TIMESTAMP (copy)   = GST_BUFFER_TIMESTAMP (buffer);
150   GST_BUFFER_OFFSET (copy)      = GST_BUFFER_OFFSET (buffer);
151
152   return copy;
153 }
154
155 /**
156  * gst_buffer_new:
157  *
158  * Creates a newly allocated buffer without any data.
159  *
160  * Returns: new #GstBuffer
161  */
162 GstBuffer*
163 gst_buffer_new (void)
164 {
165   GstBuffer *new;
166   
167   new = gst_mem_chunk_alloc0 (chunk);
168   _gst_buffer_live++;
169
170   _GST_DATA_INIT (GST_DATA (new), 
171                   _gst_buffer_type,
172                   0,
173                   (GstDataFreeFunction) gst_buffer_default_free,
174                   (GstDataCopyFunction) gst_buffer_default_copy);
175
176   GST_BUFFER_BUFFERPOOL (new) = NULL;
177   GST_BUFFER_POOL_PRIVATE (new) = NULL;
178
179   return new;
180 }
181
182 /**
183  * gst_buffer_new_and_alloc:
184  * @size: the size of the buffer and the memory to allocate
185  *
186  * Creates a newly allocated buffer with data of the given size.
187  *
188  * Returns: new #GstBuffer
189  */
190 GstBuffer*
191 gst_buffer_new_and_alloc (guint size)
192 {
193   GstBuffer *new;
194
195   new = gst_buffer_new ();
196
197   GST_BUFFER_DATA (new) = g_malloc (size);
198   GST_BUFFER_SIZE (new) = size;
199
200   return new;
201 }
202
203 /**
204  * gst_buffer_new_from_pool:
205  * @pool: the buffer pool to use
206  * @offset: the offset of the new buffer
207  * @size: the size of the new buffer
208  *
209  * Creates a newly allocated buffer using the specified bufferpool, offset and size.
210  *
211  * Returns: new #GstBuffer
212  */
213 GstBuffer*
214 gst_buffer_new_from_pool (GstBufferPool *pool, guint64 offset, guint size)
215 {
216   GstBuffer *buffer;
217   
218   g_return_val_if_fail (pool != NULL, NULL);
219
220   buffer = pool->buffer_new (pool, offset, size, pool->user_data);
221   if (!buffer)
222     return NULL;
223
224   GST_BUFFER_BUFFERPOOL (buffer) = pool;
225   gst_data_ref (GST_DATA (pool));
226
227   /* override the buffer refcount functions with those from the pool (if any) */
228   if (pool->buffer_free)
229     GST_DATA (buffer)->free = (GstDataFreeFunction)_gst_buffer_free_to_pool;
230   if (pool->buffer_copy)
231     GST_DATA (buffer)->copy = (GstDataCopyFunction)_gst_buffer_copy_from_pool;
232
233   return buffer;
234 }
235
236 /**
237  * gst_buffer_create_sub:
238  * @parent: parent #GstBuffer
239  * @offset: offset into parent #GstBuffer
240  * @size: size of new sub-buffer
241  *
242  * Creates a sub-buffer from the parent at a given offset.
243  * This sub-buffer uses the actual memory space of the parent buffer.
244  *
245  * Returns: a new #GstBuffer
246  */
247 GstBuffer*
248 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
249 {
250   GstBuffer *buffer;
251   gpointer buffer_data;
252   guint64 parent_offset;
253               
254   g_return_val_if_fail (parent != NULL, NULL);
255   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
256   g_return_val_if_fail (size > 0, NULL);
257   g_return_val_if_fail (parent->size >= offset + size, NULL);
258
259   /* remember the data for the new buffer */
260   buffer_data = parent->data + offset;
261   parent_offset = GST_BUFFER_OFFSET (parent);
262   /* make sure we're child not child from a child buffer */
263   while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
264     parent = GST_BUFFER (parent->pool_private);
265   }
266   /* ref the real parent */
267   gst_data_ref (GST_DATA (parent));
268   /* make sure nobody overwrites data in the parent */
269   if (!GST_DATA_IS_READONLY (parent))
270     GST_DATA_FLAG_SET(parent, GST_DATA_READONLY);
271
272   /* create the new buffer */
273   buffer = gst_mem_chunk_alloc0 (chunk);
274   _gst_buffer_live++;
275
276   /* make sure nobody overwrites data in the new buffer 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: first source #GstBuffer to merge
300  * @buf2: 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: a 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   /* we're just a specific case of the more general gst_buffer_span() */
316   result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
317
318   return result;
319 }
320
321 /**
322  * gst_buffer_is_span_fast:
323  * @buf1: first source buffer
324  * @buf2: second source buffer
325  *
326  * Determines whether a gst_buffer_span() is free (as in free beer), 
327  * or requires a memcpy. 
328  *
329  * Returns: TRUE if the buffers are contiguous, FALSE if a copy would be required.
330  */
331 gboolean
332 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
333 {
334   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
335   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
336
337   /* it's only fast if we have subbuffers of the same parent */
338   return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
339           (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
340           (buf1->pool_private == buf2->pool_private) &&
341           ((buf1->data + buf1->size) == buf2->data));
342 }
343
344 /**
345  * gst_buffer_span:
346  * @buf1: first source #GstBuffer to merge
347  * @offset: offset in first buffer to start new buffer
348  * @buf2: second source #GstBuffer to merge
349  * @len: length of new buffer
350  *
351  * Creates a new buffer that consists of part of buf1 and buf2.
352  * Logically, buf1 and buf2 are concatenated into a single larger
353  * buffer, and a new buffer is created at the given offset inside
354  * this space, with a given length.
355  *
356  * If the two source buffers are children of the same larger buffer,
357  * and are contiguous, the new buffer will be a child of the shared
358  * parent, and thus no copying is necessary. you can use 
359  * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
360  *
361  * Returns: a new #GstBuffer that spans the two source buffers
362  */
363 GstBuffer*
364 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
365 {
366   GstBuffer *newbuf;
367
368   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
369   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
370   g_return_val_if_fail (len > 0, NULL);
371
372   /* if the two buffers have the same parent and are adjacent */
373   if (gst_buffer_is_span_fast (buf1, buf2)) {
374     GstBuffer *parent = GST_BUFFER (buf1->pool_private);
375     /* we simply create a subbuffer of the common parent */
376     newbuf = gst_buffer_create_sub (parent, buf1->data - parent->data + offset, len);
377   }
378   else {
379     GST_DEBUG (GST_CAT_BUFFER,"slow path taken while spanning buffers %p and %p", buffer, append);
380     /* otherwise we simply have to brute-force copy the buffers */
381     newbuf = gst_buffer_new_and_alloc (len);
382
383     /* copy relevant stuff from data struct of buffer1 */
384     GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1) + offset;
385
386     /* copy the first buffer's data across */
387     memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
388     /* copy the second buffer's data across */
389     memcpy (newbuf->data + (buf1->size - offset), buf2->data, len - (buf1->size - offset));
390   }
391   /* if the offset is 0, the new buffer has the same timestamp as buf1 */
392   if (offset == 0)
393     GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
394
395   return newbuf;
396 }
397
398 void
399 gst_buffer_pool_default_free (GstBufferPool *pool)
400 {
401   _GST_DATA_DISPOSE (GST_DATA (pool));
402   g_free (pool);
403   _gst_buffer_pool_live--;
404 }
405
406 /** 
407  * gst_buffer_pool_new:
408  * @free: The function to free the bufferpool
409  * @copy: The function to copy the bufferpool
410  * @buffer_new: the function to create a new buffer from this pool
411  * @buffer_copy: the function to copy a buffer from this pool
412  * @buffer_free: the function to free a buffer in this pool
413  * @user_data: user data passed to buffer_* functions
414  *
415  * Create a new bufferpool with the given functions.
416  *
417  * Returns: a new GstBufferPool or NULL on error.
418  */
419 GstBufferPool*  
420 gst_buffer_pool_new (GstDataFreeFunction free,
421                      GstDataCopyFunction copy,
422                      GstBufferPoolBufferNewFunction buffer_new,
423                      GstBufferPoolBufferCopyFunction buffer_copy,
424                      GstBufferPoolBufferFreeFunction buffer_free,
425                      gpointer user_data)
426 {
427   GstBufferPool *pool;
428
429   /* we need at least a buffer_new function */
430   g_return_val_if_fail (buffer_new != NULL, NULL);
431
432   pool = g_new0 (GstBufferPool, 1);
433   _gst_buffer_pool_live++;
434
435   GST_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
436         
437   /* init data struct */
438   _GST_DATA_INIT (GST_DATA (pool), 
439                   _gst_buffer_pool_type,
440                   0,
441                   (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
442                   copy);
443             
444   /* set functions */
445   pool->buffer_new = buffer_new;
446   pool->buffer_copy = buffer_copy;
447   pool->buffer_free = buffer_free;
448   pool->user_data = user_data;
449                     
450   return pool;
451 }
452
453 /**
454  * gst_buffer_pool_is_active:
455  * @pool: the pool to query
456  *
457  * Query if the geven bufferpool is active.
458  *
459  * Returns: TRUE if the pool is active.
460  */
461 gboolean
462 gst_buffer_pool_is_active (GstBufferPool *pool)
463 {
464   return pool->active;
465 }
466
467 /**
468  * gst_buffer_pool_set_active:
469  * @pool: the pool to activate
470  * @active: new status of the pool
471  *
472  * Set the given pool to the active or inactive state depending on the
473  * activate parameter
474  */
475 void
476 gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
477 {
478   pool->active = active;
479 }
480
481 /**
482  * gst_buffer_pool_set_user_data:
483  * @pool: the pool set the user data for
484  * @user_data: the user_data to set
485  *
486  * Set the given user data to the bufferpool
487  */
488 void
489 gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
490 {
491   pool->user_data = user_data;
492 }
493
494 /**
495  * gst_buffer_pool_get_user_data:
496  * @pool: the pool get the user data for
497  *
498  * Get the user data of the bufferpool
499  * 
500  * Returns: the user data associated with this bufferpool
501  */
502 gpointer
503 gst_buffer_pool_get_user_data (GstBufferPool *pool)
504 {
505   return pool->user_data;
506 }
507
508 /**
509  * gst_buffer_pool_get_default:
510  * @size: The size of the buffers to allocate from this pool
511  * @numbuffers: The number of buffer to preallocate in the pool
512  *
513  * Create a pool with buffers of the given size.
514  * 
515  * Returns: A new bufferpool to create buffers of the given size.
516  */
517 GstBufferPool*  
518 gst_buffer_pool_get_default (guint size, guint numbuffers)
519 {
520   return _gst_buffer_pool_get_default (size, numbuffers);
521 }