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