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