gst/gstbuffer.c (gst_buffer_get_caps): Like all our _get accessors returning refcount...
[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  * SECTION:gstbuffer
24  * @short_description: Data-passing buffer type, supporting sub-buffers.
25  * @see_also: #GstPad, #GstMiniObject
26  *
27  * Buffers are the basic unit of data transfer in GStreamer.  The GstBuffer type
28  * provides all the state necessary to define a region of memory as part of a
29  * stream.  Sub-buffers are also supported, allowing a smaller region of a
30  * buffer to become its own buffer, with mechanisms in place to ensure that
31  * neither memory space goes away. 
32  * 
33  * Buffers are usually created with gst_buffer_new(). After a buffer has been 
34  * created one will typically allocate memory for it and set the size of the 
35  * buffer data.  The following example creates a buffer that can hold a given
36  * video frame with a given width, height and bits per plane.
37  * <example>
38  * <title>Creating a buffer for a video frame</title>
39  *   <programlisting>
40  *   GstBuffer *buffer;
41  *   gint size, width, height, bpp;
42  *   
43  *   ...
44  *   
45  *   size = width * height * bpp;
46  *   
47  *   buffer = gst_buffer_new ();
48  *   GST_BUFFER_SIZE (buffer) = size;
49  *   GST_BUFFER_MALLOCDATA (buffer) = g_alloc (size);
50  *   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
51  *   ...
52  *   </programlisting>
53  * </example>
54  * 
55  * Alternatively, use gst_buffer_new_and_alloc() 
56  * to create a buffer with preallocated data of a given size.
57  * 
58  * If an element knows what pad you will push the buffer out on, it should use
59  * gst_pad_alloc_buffer() instead to create a buffer.  This allows downstream
60  * elements to provide special buffers to write in, like hardware buffers.
61  * 
62  * gst_buffer_ref() is used to increase the refcount of a buffer. This must be
63  * done when you want to keep a handle to the buffer after pushing it to the
64  * next element.
65  * 
66  * To efficiently create a smaller buffer out of an existing one, you can
67  * use gst_buffer_create_sub().
68  * 
69  * If the plug-in wants to modify the buffer in-place, it should first obtain
70  * a buffer that is safe to modify by using gst_buffer_make_writable().  This
71  * function is optimized so that a copy will only be made when it is necessary.
72  * 
73  * Several flags of the buffer can be set and unset with the GST_BUFFER_FLAG_SET()
74  * and GST_BUFFER_FLAG_UNSET() macros. Use GST_BUFFER_FLAG_IS_SET() to test it
75  * a certain #GstBufferFlag is set.
76  * 
77  * Buffers can be efficiently merged into a larger buffer with gst_buffer_merge()
78  * and gst_buffer_span() if the gst_buffer_is_span_fast() function returns TRUE.
79  * 
80  * An element should either unref the buffer or push it out on a src pad
81  * using gst_pad_push() (see #GstPad).
82  * 
83  * Buffers usually are freed by unreffing them with gst_buffer_unref().
84  * Do not use gst_buffer_free() : this function effectively frees the buffer
85  * regardless of the refcount, which is dangerous.
86  *
87  * Last reviewed on August 12th, 2004 (0.8.5)
88  *
89  */
90 #include "gst_private.h"
91
92 #include "gstbuffer.h"
93 #include "gstinfo.h"
94 #include "gstutils.h"
95 #include "gstminiobject.h"
96
97
98 static void gst_buffer_init (GTypeInstance * instance, gpointer g_class);
99 static void gst_buffer_class_init (gpointer g_class, gpointer class_data);
100 static void gst_buffer_finalize (GstBuffer * buffer);
101 static GstBuffer *_gst_buffer_copy (GstBuffer * buffer);
102
103
104 void
105 _gst_buffer_initialize (void)
106 {
107   gpointer ptr;
108
109   gst_buffer_get_type ();
110
111   /* the GstMiniObject types need to be class_ref'd once before it can be
112    * done from multiple threads;
113    * see http://bugzilla.gnome.org/show_bug.cgi?id=304551 */
114   ptr = g_type_class_ref (GST_TYPE_BUFFER);
115   g_type_class_unref (ptr);
116 }
117
118 GType
119 gst_buffer_get_type (void)
120 {
121   static GType _gst_buffer_type;
122
123   if (G_UNLIKELY (_gst_buffer_type == 0)) {
124     static const GTypeInfo buffer_info = {
125       sizeof (GstBufferClass),
126       NULL,
127       NULL,
128       gst_buffer_class_init,
129       NULL,
130       NULL,
131       sizeof (GstBuffer),
132       0,
133       gst_buffer_init,
134       NULL
135     };
136
137     _gst_buffer_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
138         "GstBuffer", &buffer_info, 0);
139   }
140   return _gst_buffer_type;
141 }
142
143 static void
144 gst_buffer_class_init (gpointer g_class, gpointer class_data)
145 {
146   GstBufferClass *buffer_class = GST_BUFFER_CLASS (g_class);
147
148   buffer_class->mini_object_class.copy =
149       (GstMiniObjectCopyFunction) _gst_buffer_copy;
150   buffer_class->mini_object_class.finalize =
151       (GstMiniObjectFinalizeFunction) gst_buffer_finalize;
152
153 }
154
155 static void
156 gst_buffer_finalize (GstBuffer * buffer)
157 {
158   g_return_if_fail (buffer != NULL);
159
160   /* free our data */
161   if (buffer->malloc_data) {
162     g_free (buffer->malloc_data);
163   }
164
165   gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
166 }
167
168 static GstBuffer *
169 _gst_buffer_copy (GstBuffer * buffer)
170 {
171   GstBuffer *copy;
172   guint mask;
173
174   g_return_val_if_fail (buffer != NULL, NULL);
175
176   /* create a fresh new buffer */
177   copy = gst_buffer_new ();
178
179   GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", buffer, copy);
180
181   /* copy relevant flags */
182   mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
183       GST_BUFFER_FLAG_DELTA_UNIT;
184   GST_MINI_OBJECT (copy)->flags |= GST_MINI_OBJECT (buffer)->flags & mask;
185
186   /* we simply copy everything from our parent */
187   copy->data = g_memdup (buffer->data, buffer->size);
188   /* make sure it gets freed (even if the parent is subclassed, we return a
189      normal buffer */
190   copy->malloc_data = copy->data;
191
192   copy->size = buffer->size;
193
194   GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
195   GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
196   GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
197   GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
198
199   if (GST_BUFFER_CAPS (buffer))
200     GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
201   else
202     GST_BUFFER_CAPS (copy) = NULL;
203
204   return copy;
205 }
206
207 static void
208 gst_buffer_init (GTypeInstance * instance, gpointer g_class)
209 {
210   GstBuffer *buffer;
211
212   buffer = (GstBuffer *) instance;
213
214   GST_CAT_LOG (GST_CAT_BUFFER, "init %p", buffer);
215
216   GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
217   GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
218   GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
219   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
220 }
221
222 /**
223  * gst_buffer_new:
224  *
225  * Creates a newly allocated buffer without any data.
226  *
227  * Returns: the new #GstBuffer.
228  *
229  * MT safe.
230  */
231 GstBuffer *
232 gst_buffer_new (void)
233 {
234   GstBuffer *newbuf;
235
236   newbuf = (GstBuffer *) gst_mini_object_new (GST_TYPE_BUFFER);
237
238   GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
239
240   return newbuf;
241 }
242
243 /**
244  * gst_buffer_new_and_alloc:
245  * @size: the size of the new buffer's data.
246  *
247  * Creates a newly allocated buffer with data of the given size.
248  *
249  * Returns: the new #GstBuffer.
250  *
251  * MT safe.
252  */
253 GstBuffer *
254 gst_buffer_new_and_alloc (guint size)
255 {
256   GstBuffer *newbuf;
257
258   newbuf = gst_buffer_new ();
259
260   newbuf->malloc_data = g_malloc (size);
261   GST_BUFFER_DATA (newbuf) = newbuf->malloc_data;
262   GST_BUFFER_SIZE (newbuf) = size;
263
264   GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
265
266   return newbuf;
267 }
268
269 /**
270  * gst_buffer_get_caps:
271  * @buffer: a #GstBuffer to get the caps of.
272  *
273  * Gets the media type of the buffer. This can be NULL if there
274  * is not media type attached to this buffer or when the media
275  * type is the same as the previous received buffer.
276  *
277  * Returns: a reference to the #GstCaps, or NULL if there were no caps on this
278  * buffer.
279  */
280 /* FIXME can we make this threadsafe without a lock on the buffer?
281  * We can use compare and swap and atomic reads. */
282 GstCaps *
283 gst_buffer_get_caps (GstBuffer * buffer)
284 {
285   GstCaps *ret;
286
287   g_return_val_if_fail (buffer != NULL, NULL);
288
289   ret = GST_BUFFER_CAPS (buffer);
290
291   if (ret)
292     gst_caps_ref (ret);
293
294   return ret;
295 }
296
297 /**
298  * gst_buffer_set_caps:
299  * @buffer: a #GstBuffer to set the caps of.
300  * @caps: a #GstCaps to set.
301  *
302  * Sets the media type on the buffer. The caps' refcount will
303  * be increased and any previous caps on the buffer will be
304  * unreffed.
305  */
306 /* FIXME can we make this threadsafe without a lock on the buffer?
307  * We can use compare and swap and atomic reads. Another idea is to
308  * not attach the caps to the buffer but use an event to signal a caps
309  * change. */
310 void
311 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
312 {
313   g_return_if_fail (buffer != NULL);
314
315   gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
316 }
317
318
319 typedef struct _GstSubBuffer GstSubBuffer;
320 typedef struct _GstSubBufferClass GstSubBufferClass;
321
322 #define GST_TYPE_SUBBUFFER                         (gst_subbuffer_get_type())
323
324 #define GST_IS_SUBBUFFER(obj)  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SUBBUFFER))
325 #define GST_SUBBUFFER(obj)     (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SUBBUFFER, GstSubBuffer))
326
327 struct _GstSubBuffer
328 {
329   GstBuffer buffer;
330
331   GstBuffer *parent;
332 };
333
334 struct _GstSubBufferClass
335 {
336   GstBufferClass buffer_class;
337 };
338
339 static GstBufferClass *sub_parent_class;
340
341 static void gst_subbuffer_init (GTypeInstance * instance, gpointer g_class);
342 static void gst_subbuffer_class_init (gpointer g_class, gpointer class_data);
343 static void gst_subbuffer_finalize (GstSubBuffer * buffer);
344
345 static GType
346 gst_subbuffer_get_type (void)
347 {
348   static GType _gst_subbuffer_type = 0;
349
350   if (G_UNLIKELY (_gst_subbuffer_type == 0)) {
351     static const GTypeInfo subbuffer_info = {
352       sizeof (GstSubBufferClass),
353       NULL,
354       NULL,
355       gst_subbuffer_class_init,
356       NULL,
357       NULL,
358       sizeof (GstSubBuffer),
359       0,
360       gst_subbuffer_init,
361       NULL
362     };
363
364     _gst_subbuffer_type = g_type_register_static (GST_TYPE_BUFFER,
365         "GstSubBuffer", &subbuffer_info, 0);
366   }
367   return _gst_subbuffer_type;
368 }
369
370 static void
371 gst_subbuffer_class_init (gpointer g_class, gpointer class_data)
372 {
373   GstBufferClass *buffer_class = GST_BUFFER_CLASS (g_class);
374
375   sub_parent_class = g_type_class_ref (GST_TYPE_BUFFER);
376
377   buffer_class->mini_object_class.finalize =
378       (GstMiniObjectFinalizeFunction) gst_subbuffer_finalize;
379 }
380
381 static void
382 gst_subbuffer_finalize (GstSubBuffer * buffer)
383 {
384   gst_buffer_unref (buffer->parent);
385
386   GST_MINI_OBJECT_CLASS (sub_parent_class)->finalize (GST_MINI_OBJECT (buffer));
387 }
388
389 static void
390 gst_subbuffer_init (GTypeInstance * instance, gpointer g_class)
391 {
392 }
393
394 /**
395  * gst_buffer_create_sub:
396  * @parent: a parent #GstBuffer to create a subbuffer from.
397  * @offset: the offset into parent #GstBuffer.
398  * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
399  *
400  * Creates a sub-buffer from the parent at a given offset.
401  * This sub-buffer uses the actual memory space of the parent buffer.
402  * This function will copy the offset and timestamp field when the
403  * offset is 0, else they are set to _NONE.
404  * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
405  *
406  * Returns: the new #GstBuffer, or NULL if there was an error.
407  *
408  * MT safe.
409  */
410 GstBuffer *
411 gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size)
412 {
413   GstSubBuffer *subbuffer;
414   GstBuffer *parent;
415
416   g_return_val_if_fail (buffer != NULL, NULL);
417   g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL);
418   g_return_val_if_fail (size > 0, NULL);
419   g_return_val_if_fail (buffer->size >= offset + size, NULL);
420
421   /* find real parent */
422   if (GST_IS_SUBBUFFER (buffer)) {
423     parent = GST_SUBBUFFER (buffer)->parent;
424   } else {
425     parent = buffer;
426   }
427   gst_buffer_ref (parent);
428
429   /* create the new buffer */
430   subbuffer = (GstSubBuffer *) gst_mini_object_new (GST_TYPE_SUBBUFFER);
431   subbuffer->parent = parent;
432
433   GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", subbuffer,
434       parent);
435
436   /* set the right values in the child */
437   GST_BUFFER_DATA (GST_BUFFER_CAST (subbuffer)) = buffer->data + offset;
438   GST_BUFFER_SIZE (GST_BUFFER_CAST (subbuffer)) = size;
439
440   /* we can copy the timestamp and offset if the new buffer starts at
441    * offset 0 */
442   if (offset == 0) {
443     GST_BUFFER_TIMESTAMP (subbuffer) = GST_BUFFER_TIMESTAMP (buffer);
444     GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET (buffer);
445   } else {
446     GST_BUFFER_TIMESTAMP (subbuffer) = GST_CLOCK_TIME_NONE;
447     GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET_NONE;
448   }
449
450   GST_BUFFER_DURATION (subbuffer) = GST_CLOCK_TIME_NONE;
451   GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_NONE;
452
453   GST_BUFFER_CAPS (subbuffer) = NULL;
454
455   return GST_BUFFER_CAST (subbuffer);
456 }
457
458 /**
459  * gst_buffer_is_span_fast:
460  * @buf1: a first source #GstBuffer.
461  * @buf2: the second source #GstBuffer.
462  *
463  * Determines whether a gst_buffer_span() can be done without copying
464  * the contents, that is, whether the data areas are contiguous.
465  *
466  * Returns: TRUE if the buffers are contiguous,
467  * FALSE if a copy would be required.
468  *
469  * MT safe.
470  */
471 gboolean
472 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
473 {
474   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
475   g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
476   g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
477
478   /* it's only fast if we have subbuffers of the same parent */
479   return (GST_IS_SUBBUFFER (buf1) &&
480       GST_IS_SUBBUFFER (buf2) &&
481       (GST_SUBBUFFER (buf1)->parent == GST_SUBBUFFER (buf2)->parent) &&
482       ((buf1->data + buf1->size) == buf2->data));
483 }
484
485 /**
486  * gst_buffer_span:
487  * @buf1: a first source #GstBuffer to merge.
488  * @offset: the offset in the first buffer from where the new
489  * buffer should start.
490  * @buf2: the second source #GstBuffer to merge.
491  * @len: the total length of the new buffer.
492  *
493  * Creates a new buffer that consists of part of buf1 and buf2.
494  * Logically, buf1 and buf2 are concatenated into a single larger
495  * buffer, and a new buffer is created at the given offset inside
496  * this space, with a given length.
497  *
498  * If the two source buffers are children of the same larger buffer,
499  * and are contiguous, the new buffer will be a child of the shared
500  * parent, and thus no copying is necessary. you can use
501  * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
502  *
503  * Returns: the new #GstBuffer that spans the two source buffers.
504  *
505  * MT safe.
506  */
507 GstBuffer *
508 gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
509     guint32 len)
510 {
511   GstBuffer *newbuf;
512
513   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
514   g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
515   g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
516   g_return_val_if_fail (len > 0, NULL);
517   g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
518
519   /* if the two buffers have the same parent and are adjacent */
520   if (gst_buffer_is_span_fast (buf1, buf2)) {
521     GstBuffer *parent = GST_SUBBUFFER (buf1)->parent;
522
523     /* we simply create a subbuffer of the common parent */
524     newbuf = gst_buffer_create_sub (parent,
525         buf1->data - parent->data + offset, len);
526   } else {
527     GST_CAT_DEBUG (GST_CAT_BUFFER,
528         "slow path taken while spanning buffers %p and %p", buf1, buf2);
529     /* otherwise we simply have to brute-force copy the buffers */
530     newbuf = gst_buffer_new_and_alloc (len);
531
532     /* copy the first buffer's data across */
533     memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
534     /* copy the second buffer's data across */
535     memcpy (newbuf->data + (buf1->size - offset), buf2->data,
536         len - (buf1->size - offset));
537     /* if the offset is 0, the new buffer has the same timestamp as buf1 */
538     if (offset == 0) {
539       GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
540       GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
541     }
542   }
543   /* if we completely merged the two buffers (appended), we can
544    * calculate the duration too. Also make sure we's not messing with
545    * invalid DURATIONS */
546   if (offset == 0 && buf1->size + buf2->size == len) {
547     if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
548         GST_BUFFER_DURATION_IS_VALID (buf2)) {
549       /* add duration */
550       GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
551           GST_BUFFER_DURATION (buf2);
552     }
553     if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
554       /* add offset_end */
555       GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
556     }
557   }
558
559   return newbuf;
560 }