shouldn't fuss about not needing to ref at all
[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 "gstbuffer.h"
28
29
30 GType _gst_buffer_type;
31
32 static GMemChunk *_gst_buffer_chunk;
33 static GMutex *_gst_buffer_chunk_lock;
34 static gint _gst_buffer_live;
35
36 void 
37 _gst_buffer_initialize (void) 
38 {
39   int buffersize = sizeof(GstBuffer);
40   static const GTypeInfo buffer_info = {
41     0, /* sizeof(class), */
42     NULL,
43     NULL,
44     NULL,
45     NULL,
46     NULL,
47     0, /* sizeof(object), */
48     0,
49     NULL,
50     NULL,
51   };
52
53   /* round up to the nearest 32 bytes for cache-line and other efficiencies */
54   buffersize = (((buffersize-1) / 32) + 1) * 32;
55
56   _gst_buffer_chunk = g_mem_chunk_new ("GstBuffer", buffersize,
57     buffersize * 32, G_ALLOC_AND_FREE);
58
59   _gst_buffer_chunk_lock = g_mutex_new ();
60
61   _gst_buffer_type = g_type_register_static (G_TYPE_INT, "GstBuffer", &buffer_info, 0);
62
63   _gst_buffer_live = 0;
64 }
65
66 /**
67  * gst_buffer_print_stats:
68  *
69  * Print statistics about live buffers.
70  */
71 void
72 gst_buffer_print_stats (void)
73 {
74   g_log (g_log_domain_gstreamer, G_LOG_LEVEL_INFO, 
75                   "%d live buffers", _gst_buffer_live);
76 }
77
78 /**
79  * gst_buffer_new:
80  *
81  * Create a new buffer.
82  *
83  * Returns: new buffer
84  */
85 GstBuffer*
86 gst_buffer_new (void)
87 {
88   GstBuffer *buffer;
89
90   g_mutex_lock (_gst_buffer_chunk_lock);
91   buffer = g_mem_chunk_alloc (_gst_buffer_chunk);
92   _gst_buffer_live++;
93   g_mutex_unlock (_gst_buffer_chunk_lock);
94   GST_INFO (GST_CAT_BUFFER,"creating new buffer %p",buffer);
95
96   GST_DATA_TYPE(buffer) = _gst_buffer_type;
97
98   buffer->lock = g_mutex_new ();
99 #ifdef HAVE_ATOMIC_H
100   atomic_set (&buffer->refcount, 1);
101 #else
102   buffer->refcount = 1;
103 #endif
104   buffer->flags = 0;
105   buffer->data = NULL;
106   buffer->size = 0;
107   buffer->maxsize = 0;
108   buffer->offset = -1;
109   buffer->timestamp = 0;
110   buffer->parent = NULL;
111   buffer->pool = NULL;
112   buffer->pool_private = NULL;
113   buffer->free = NULL;
114   buffer->copy = NULL;
115
116   return buffer;
117 }
118
119 /**
120  * gst_buffer_new_from_pool:
121  * @pool: the buffer pool to use
122  * @offset: the offset of the new buffer
123  * @size: the size of the new buffer
124  *
125  * Create a new buffer using the specified bufferpool, offset and size.
126  *
127  * Returns: new buffer
128  */
129 GstBuffer*
130 gst_buffer_new_from_pool (GstBufferPool *pool, guint32 offset, guint32 size)
131 {
132   GstBuffer *buffer;
133
134   g_return_val_if_fail (pool != NULL, NULL);
135   g_return_val_if_fail (pool->buffer_new != NULL, NULL);
136   
137   buffer = pool->buffer_new (pool, offset, size, pool->user_data);
138   buffer->pool = pool;
139   buffer->free = pool->buffer_free;
140   buffer->copy = pool->buffer_copy;
141   
142   GST_INFO (GST_CAT_BUFFER,"creating new buffer %p from pool %p (size %x, offset %x)", 
143                   buffer, pool, size, offset);
144
145   return buffer;
146 }
147
148 /**
149  * gst_buffer_create_sub:
150  * @parent: parent buffer
151  * @offset: offset into parent buffer
152  * @size: size of new subbuffer
153  *
154  * Creates a sub-buffer from the parent at a given offset.
155  *
156  * Returns: new buffer
157  */
158 GstBuffer*
159 gst_buffer_create_sub (GstBuffer *parent,
160                        guint32 offset,
161                        guint32 size) 
162 {
163   GstBuffer *buffer;
164
165   g_return_val_if_fail (parent != NULL, NULL);
166   g_return_val_if_fail (GST_BUFFER_REFCOUNT(parent) > 0, NULL);
167   g_return_val_if_fail (size > 0, NULL);
168   g_return_val_if_fail ((offset+size) <= parent->size, NULL);
169
170   g_mutex_lock (_gst_buffer_chunk_lock);
171   buffer = g_mem_chunk_alloc (_gst_buffer_chunk);
172   _gst_buffer_live++;
173   g_mutex_unlock (_gst_buffer_chunk_lock);
174   GST_INFO (GST_CAT_BUFFER,"creating new subbuffer %p from parent %p (size %u, offset %u)", 
175                   buffer, parent, size, offset);
176
177   GST_DATA_TYPE(buffer) = _gst_buffer_type;
178   buffer->lock = g_mutex_new ();
179 #ifdef HAVE_ATOMIC_H
180   atomic_set (&buffer->refcount, 1);
181 #else
182   buffer->refcount = 1;
183 #endif
184
185   /* copy flags and type from parent, for lack of better */
186   buffer->flags = parent->flags;
187
188   /* set the data pointer, size, offset, and maxsize */
189   buffer->data = parent->data + offset;
190   buffer->size = size;
191   buffer->maxsize = parent->size - offset;
192
193   /* deal with bogus/unknown offsets */
194   if (parent->offset != (guint32)-1)
195     buffer->offset = parent->offset + offset;
196   else
197     buffer->offset = (guint32)-1;
198
199   /* again, for lack of better, copy parent's timestamp */
200   buffer->timestamp = parent->timestamp;
201   buffer->maxage = parent->maxage;
202
203   /* if the parent buffer is a subbuffer itself, use its parent, a real buffer */
204   if (parent->parent != NULL)
205     parent = parent->parent;
206
207   /* set parentage and reference the parent */
208   buffer->parent = parent;
209   gst_buffer_ref (parent);
210
211   buffer->pool = NULL;
212
213   return buffer;
214 }
215
216
217 /* FIXME FIXME: how does this overlap with the newly-added gst_buffer_span() ??? */
218 /**
219  * gst_buffer_append:
220  * @buffer: a buffer
221  * @append: the buffer to append
222  *
223  * Creates a new buffer by appending the data of append to the
224  * existing data of buffer.
225  *
226  * Returns: new buffer
227  */
228 GstBuffer*
229 gst_buffer_append (GstBuffer *buffer, 
230                    GstBuffer *append) 
231 {
232   guint size;
233   GstBuffer *newbuf;
234
235   g_return_val_if_fail (buffer != NULL, NULL);
236   g_return_val_if_fail (append != NULL, NULL);
237   g_return_val_if_fail (buffer->pool == NULL, NULL);
238   g_return_val_if_fail (GST_BUFFER_REFCOUNT(buffer) > 0, NULL);
239   g_return_val_if_fail (GST_BUFFER_REFCOUNT(append) > 0, NULL);
240
241   GST_INFO (GST_CAT_BUFFER,"appending buffers %p and %p",buffer,append);
242
243   GST_BUFFER_LOCK (buffer);
244   /* the buffer is not used by anyone else */
245   if (GST_BUFFER_REFCOUNT (buffer) == 1 && buffer->parent == NULL 
246           && !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE)) {
247     /* save the old size */
248     size = buffer->size;
249     buffer->size += append->size;
250     buffer->data = g_realloc (buffer->data, buffer->size);
251     memcpy(buffer->data + size, append->data, append->size);
252     GST_BUFFER_UNLOCK (buffer);
253   }
254   /* the buffer is used, create a new one */
255   else {
256     newbuf = gst_buffer_new ();
257     newbuf->size = buffer->size+append->size;
258     newbuf->data = g_malloc (newbuf->size);
259     memcpy (newbuf->data, buffer->data, buffer->size);
260     memcpy (newbuf->data+buffer->size, append->data, append->size);
261     GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buffer);
262     GST_BUFFER_UNLOCK (buffer);
263     gst_buffer_unref (buffer);
264     buffer = newbuf;
265   }
266   return buffer;
267 }
268
269 /**
270  * gst_buffer_destroy:
271  * @buffer: the GstBuffer to destroy
272  *
273  * destroy the buffer
274  */
275 void 
276 gst_buffer_destroy (GstBuffer *buffer) 
277 {
278
279   g_return_if_fail (buffer != NULL);
280   
281   GST_INFO (GST_CAT_BUFFER, "freeing %sbuffer %p",
282             (buffer->parent?"sub":""),
283             buffer);
284   
285   /* free the data only if there is some, DONTFREE isn't set, and not sub */
286   if (GST_BUFFER_DATA (buffer) &&
287       !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) &&
288       (buffer->parent == NULL)) {
289     /* if there's a free function, use it */
290     if (buffer->free != NULL) {
291       (buffer->free)(buffer);
292     } else {
293       g_free (GST_BUFFER_DATA (buffer));
294     }
295   }
296
297   /* unreference the parent if there is one */
298   if (buffer->parent != NULL)
299     gst_buffer_unref (buffer->parent);
300
301   g_mutex_free (buffer->lock);
302   /* g_print("freed mutex\n"); */
303
304 #ifdef GST_DEBUG_ENABLED
305   /* make it hard to reuse by mistake */
306   memset (buffer, 0, sizeof (GstBuffer));
307 #endif
308
309   /* remove it entirely from memory */
310   g_mutex_lock (_gst_buffer_chunk_lock);
311   g_mem_chunk_free (_gst_buffer_chunk,buffer);
312   _gst_buffer_live--;
313   g_mutex_unlock (_gst_buffer_chunk_lock);
314 }
315
316 /**
317  * gst_buffer_ref:
318  * @buffer: the GstBuffer to reference
319  *
320  * Increment the refcount of this buffer.
321  */
322 void 
323 gst_buffer_ref (GstBuffer *buffer) 
324 {
325   g_return_if_fail (buffer != NULL);
326
327   GST_INFO (GST_CAT_BUFFER, "ref buffer %p, current count is %d", buffer,GST_BUFFER_REFCOUNT(buffer));
328   g_return_if_fail (GST_BUFFER_REFCOUNT(buffer) > 0);
329
330 #ifdef HAVE_ATOMIC_H
331   atomic_inc (&(buffer->refcount));
332 #else
333   GST_BUFFER_LOCK (buffer);
334   buffer->refcount++;
335   GST_BUFFER_UNLOCK (buffer);
336 #endif
337 }
338
339 /**
340  * gst_buffer_ref_by_count:
341  * @buffer: the GstBuffer to reference
342  * @count: a number
343  *
344  * Increment the refcount of this buffer by the given number.
345  */
346 void
347 gst_buffer_ref_by_count (GstBuffer *buffer, gint count)
348 {
349   g_return_if_fail (buffer != NULL);
350   if (count == 0) return; /* no error when no need to ref */
351   g_return_if_fail (count > 0);
352
353 #ifdef HAVE_ATOMIC_H
354   g_return_if_fail (atomic_read (&(buffer->refcount)) > 0);
355   atomic_add (count, &(buffer->refcount));
356 #else
357   g_return_if_fail (buffer->refcount > 0);
358   GST_BUFFER_LOCK (buffer);
359   buffer->refcount += count;
360   GST_BUFFER_UNLOCK (buffer);
361 #endif
362 }
363
364 /**
365  * gst_buffer_unref:
366  * @buffer: the GstBuffer to unref
367  *
368  * Decrement the refcount of this buffer. If the refcount is
369  * zero, the buffer will be destroyed.
370  */
371 void 
372 gst_buffer_unref (GstBuffer *buffer) 
373 {
374   gint zero;
375
376   g_return_if_fail (buffer != NULL);
377
378   GST_INFO (GST_CAT_BUFFER, "unref buffer %p, current count is %d", buffer,GST_BUFFER_REFCOUNT(buffer));
379   g_return_if_fail (GST_BUFFER_REFCOUNT(buffer) > 0);
380
381 #ifdef HAVE_ATOMIC_H
382   zero = atomic_dec_and_test (&(buffer->refcount));
383 #else
384   GST_BUFFER_LOCK (buffer);
385   buffer->refcount--;
386   zero = (buffer->refcount == 0);
387   GST_BUFFER_UNLOCK (buffer);
388 #endif
389
390   /* if we ended up with the refcount at zero, destroy the buffer */
391   if (zero) {
392     gst_buffer_destroy (buffer);
393   }
394 }
395
396 /**
397  * gst_buffer_copy:
398  * @buffer: the orignal GstBuffer to make a copy of
399  *
400  * Make a full copy of the give buffer, data and all.
401  *
402  * Returns: new buffer
403  */
404 GstBuffer *
405 gst_buffer_copy (GstBuffer *buffer)
406 {
407   GstBuffer *newbuf;
408
409   g_return_val_if_fail (GST_BUFFER_REFCOUNT(buffer) > 0, NULL);
410
411   /* if a copy function exists, use it, else copy the bytes */
412   if (buffer->copy != NULL) {
413     newbuf = (buffer->copy)(buffer);
414   } else {
415     /* allocate a new buffer */
416     newbuf = gst_buffer_new();
417
418     /* copy the absolute size */
419     newbuf->size = buffer->size;
420     /* allocate space for the copy */
421     newbuf->data = (guchar *)g_malloc (buffer->size);
422     /* copy the data straight across */
423     memcpy(newbuf->data,buffer->data,buffer->size);
424     /* the new maxsize is the same as the size, since we just malloc'd it */
425     newbuf->maxsize = newbuf->size;
426   }
427   newbuf->offset = buffer->offset;
428   newbuf->timestamp = buffer->timestamp;
429   newbuf->maxage = buffer->maxage;
430
431   /* since we just created a new buffer, so we have no ties to old stuff */
432   newbuf->parent = NULL;
433   newbuf->pool = NULL;
434
435   return newbuf;
436 }
437
438 /**
439  * gst_buffer_is_span_fast:
440  * @buf1: first source buffer
441  * @buf2: second source buffer
442  *
443  * Determines whether a gst_buffer_span is free, or requires a memcpy. 
444  *
445  * Returns: TRUE if the buffers are contiguous, FALSE if a copy would be required.
446  */
447 gboolean
448 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
449 {
450   g_return_val_if_fail (GST_BUFFER_REFCOUNT(buf1) > 0, FALSE);
451   g_return_val_if_fail (GST_BUFFER_REFCOUNT(buf2) > 0, FALSE);
452
453   return (buf1->parent && buf2->parent && 
454           (buf1->parent == buf2->parent) &&
455           ((buf1->data + buf1->size) == buf2->data));
456 }
457
458
459 /**
460  * gst_buffer_span:
461  * @buf1: first source buffer to merge
462  * @offset: offset in first buffer to start new buffer
463  * @buf2: second source buffer to merge
464  * @len: length of new buffer
465  *
466  * Create a new buffer that consists of part of buf1 and buf2.
467  * Logically, buf1 and buf2 are concatenated into a single larger
468  * buffer, and a new buffer is created at the given offset inside
469  * this space, with a given length.
470  *
471  * If the two source buffers are children of the same larger buffer,
472  * and are contiguous, the new buffer will be a child of the shared
473  * parent, and thus no copying is necessary.
474  *
475  * Returns: new buffer that spans the two source buffers
476  */
477 /* FIXME need to think about CoW and such... */
478 GstBuffer *
479 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
480 {
481   GstBuffer *newbuf;
482
483   g_return_val_if_fail (GST_BUFFER_REFCOUNT(buf1) > 0, NULL);
484   g_return_val_if_fail (GST_BUFFER_REFCOUNT(buf2) > 0, NULL);
485
486   /* make sure buf1 has a lower address than buf2 */
487   if (buf1->data > buf2->data) {
488     GstBuffer *tmp = buf1;
489     /* g_print ("swapping buffers\n"); */
490     buf1 = buf2;
491     buf2 = tmp;
492   }
493
494   /* if the two buffers have the same parent and are adjacent */
495   if (gst_buffer_is_span_fast(buf1,buf2)) {
496     /* we simply create a subbuffer of the common parent */
497     newbuf = gst_buffer_create_sub (buf1->parent, buf1->data - (buf1->parent->data) + offset, len);
498   }
499   else {
500     /* g_print ("slow path taken in buffer_span\n"); */
501     /* otherwise we simply have to brute-force copy the buffers */
502     newbuf = gst_buffer_new ();
503
504     /* put in new size */
505     newbuf->size = len;
506     /* allocate space for the copy */
507     newbuf->data = (guchar *)g_malloc(len);
508     /* copy the first buffer's data across */
509     memcpy(newbuf->data, buf1->data + offset, buf1->size - offset);
510     /* copy the second buffer's data across */
511     memcpy(newbuf->data + (buf1->size - offset), buf2->data, len - (buf1->size - offset));
512
513     if (newbuf->offset != (guint32)-1)
514       newbuf->offset = buf1->offset + offset;
515     newbuf->timestamp = buf1->timestamp;
516     if (buf2->maxage > buf1->maxage) newbuf->maxage = buf2->maxage;
517     else newbuf->maxage = buf1->maxage;
518
519   }
520
521   return newbuf;
522 }
523
524
525 /**
526  * gst_buffer_merge:
527  * @buf1: first source buffer to merge
528  * @buf2: second source buffer to merge
529  *
530  * Create a new buffer that is the concatenation of the two source
531  * buffers.  The original source buffers will not be modified or
532  * unref'd.
533  *
534  * Internally is nothing more than a specialized gst_buffer_span,
535  * so the same optimizations can occur.
536  *
537  * Returns: new buffer that's the concatenation of the source buffers
538  */
539 GstBuffer *
540 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
541 {
542   GstBuffer *result;
543   /* we're just a specific case of the more general gst_buffer_span() */
544   result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
545
546   GST_BUFFER_TIMESTAMP (result) = GST_BUFFER_TIMESTAMP (buf1);
547
548   return result;
549 }