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