gst/gstbuffer.*: Avoid typechecking in places where not needed.
[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 #include "gst_private.h"
24
25 #include "gstbuffer.h"
26 #include "gstinfo.h"
27 #include "gstutils.h"
28 #include "gstminiobject.h"
29
30 #ifndef GST_DISABLE_TRACE
31 /* #define GST_WITH_ALLOC_TRACE  */
32 #include "gsttrace.h"
33
34 static GstAllocTrace *_gst_buffer_trace;
35 #endif
36
37 static void gst_buffer_init (GTypeInstance * instance, gpointer g_class);
38 static void gst_buffer_class_init (gpointer g_class, gpointer class_data);
39 static void gst_buffer_finalize (GstBuffer * buffer);
40 static GstBuffer *_gst_buffer_copy (GstBuffer * buffer);
41
42
43 void
44 _gst_buffer_initialize (void)
45 {
46   gst_buffer_get_type ();
47
48 #ifndef GST_DISABLE_TRACE
49   _gst_buffer_trace = gst_alloc_trace_register (GST_BUFFER_TRACE_NAME);
50 #endif
51 }
52
53 GType
54 gst_buffer_get_type (void)
55 {
56   static GType _gst_buffer_type;
57
58   if (G_UNLIKELY (_gst_buffer_type == 0)) {
59     static const GTypeInfo buffer_info = {
60       sizeof (GstBufferClass),
61       NULL,
62       NULL,
63       gst_buffer_class_init,
64       NULL,
65       NULL,
66       sizeof (GstBuffer),
67       0,
68       gst_buffer_init,
69       NULL
70     };
71
72     _gst_buffer_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
73         "GstBuffer", &buffer_info, 0);
74   }
75   return _gst_buffer_type;
76 }
77
78 static void
79 gst_buffer_class_init (gpointer g_class, gpointer class_data)
80 {
81   GstBufferClass *buffer_class = GST_BUFFER_CLASS (g_class);
82
83   buffer_class->mini_object_class.copy =
84       (GstMiniObjectCopyFunction) _gst_buffer_copy;
85   buffer_class->mini_object_class.finalize =
86       (GstMiniObjectFinalizeFunction) gst_buffer_finalize;
87
88 }
89
90 static void
91 gst_buffer_finalize (GstBuffer * buffer)
92 {
93   g_return_if_fail (buffer != NULL);
94
95   /* free our data */
96   if (buffer->malloc_data) {
97     g_free (buffer->malloc_data);
98   }
99
100   gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
101 }
102
103 static GstBuffer *
104 _gst_buffer_copy (GstBuffer * buffer)
105 {
106   GstBuffer *copy;
107   guint mask;
108
109   g_return_val_if_fail (buffer != NULL, NULL);
110
111   /* create a fresh new buffer */
112   copy = gst_buffer_new ();
113
114   GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", buffer, copy);
115
116   /* copy relevant flags */
117   mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
118       GST_BUFFER_FLAG_DELTA_UNIT;
119   GST_MINI_OBJECT (copy)->flags |= GST_MINI_OBJECT (buffer)->flags & mask;
120
121   /* we simply copy everything from our parent */
122   if (buffer->malloc_data) {
123     copy->malloc_data = g_memdup (buffer->data, buffer->size);
124     copy->data = copy->malloc_data;
125   }
126
127   copy->size = buffer->size;
128
129   GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
130   GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
131   GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
132   GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
133
134   if (GST_BUFFER_CAPS (buffer))
135     GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
136   else
137     GST_BUFFER_CAPS (copy) = NULL;
138
139   return copy;
140 }
141
142 static void
143 gst_buffer_init (GTypeInstance * instance, gpointer g_class)
144 {
145   GstBuffer *buffer;
146
147   buffer = (GstBuffer *) instance;
148
149   GST_CAT_LOG (GST_CAT_BUFFER, "init %p", buffer);
150
151   GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
152   GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
153   GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
154   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
155 }
156
157 /**
158  * gst_buffer_new:
159  *
160  * Creates a newly allocated buffer without any data.
161  *
162  * Returns: the new #GstBuffer.
163  * 
164  * MT safe.
165  */
166 GstBuffer *
167 gst_buffer_new (void)
168 {
169   GstBuffer *newbuf;
170
171   newbuf = (GstBuffer *) gst_mini_object_new (GST_TYPE_BUFFER);
172
173   GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
174
175   return newbuf;
176 }
177
178 /**
179  * gst_buffer_new_and_alloc:
180  * @size: the size of the new buffer's data.
181  *
182  * Creates a newly allocated buffer with data of the given size.
183  *
184  * Returns: the new #GstBuffer.
185  * 
186  * MT safe.
187  */
188 GstBuffer *
189 gst_buffer_new_and_alloc (guint size)
190 {
191   GstBuffer *newbuf;
192
193   newbuf = gst_buffer_new ();
194
195   newbuf->malloc_data = g_malloc (size);
196   GST_BUFFER_DATA (newbuf) = newbuf->malloc_data;
197   GST_BUFFER_SIZE (newbuf) = size;
198
199   return newbuf;
200 }
201
202
203 /**
204  * gst_buffer_get_caps:
205  * @buffer: a #GstBuffer to get the caps of.
206  *
207  * Gets the media type of the buffer. This can be NULL if there
208  * is not media type attached to this buffer or when the media
209  * type is the same as the previous received buffer.
210  *
211  * This function does not increment the refcount of the caps. The
212  * caps pointer will therefore remain valid until the buffer is 
213  * unreffed.
214  *
215  * Returns: the #GstCaps, or NULL if there was an error or there
216  * were no caps on this buffer.
217  */
218 /* FIXME can we make this threadsafe without a lock on the buffer?
219  * We can use compare and swap and atomic reads. */
220 GstCaps *
221 gst_buffer_get_caps (GstBuffer * buffer)
222 {
223   g_return_val_if_fail (buffer != NULL, NULL);
224
225   return GST_BUFFER_CAPS (buffer);
226 }
227
228 /**
229  * gst_buffer_set_caps:
230  * @buffer: a #GstBuffer to set the caps of.
231  * @caps: a #GstCaps to set.
232  *
233  * Sets the media type on the buffer. The caps' refcount will
234  * be increased and any previous caps on the buffer will be
235  * unreffed.
236  */
237 /* FIXME can we make this threadsafe without a lock on the buffer? 
238  * We can use compare and swap and atomic reads. Another idea is to
239  * not attach the caps to the buffer but use an event to signal a caps
240  * change. */
241 void
242 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
243 {
244   g_return_if_fail (buffer != NULL);
245
246   gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
247 }
248
249
250 typedef struct _GstSubBuffer GstSubBuffer;
251 typedef struct _GstSubBufferClass GstSubBufferClass;
252
253 #define GST_TYPE_SUBBUFFER                         (gst_subbuffer_get_type())
254
255 #define GST_IS_SUBBUFFER(obj)  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SUBBUFFER))
256 #define GST_SUBBUFFER(obj)     (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SUBBUFFER, GstSubBuffer))
257
258 struct _GstSubBuffer
259 {
260   GstBuffer buffer;
261
262   GstBuffer *parent;
263 };
264
265 struct _GstSubBufferClass
266 {
267   GstBufferClass buffer_class;
268 };
269
270 static GstBufferClass *sub_parent_class;
271
272 static void gst_subbuffer_init (GTypeInstance * instance, gpointer g_class);
273 static void gst_subbuffer_class_init (gpointer g_class, gpointer class_data);
274 static void gst_subbuffer_finalize (GstSubBuffer * buffer);
275
276 static GType
277 gst_subbuffer_get_type (void)
278 {
279   static GType _gst_subbuffer_type = 0;
280
281   if (G_UNLIKELY (_gst_subbuffer_type == 0)) {
282     static const GTypeInfo subbuffer_info = {
283       sizeof (GstSubBufferClass),
284       NULL,
285       NULL,
286       gst_subbuffer_class_init,
287       NULL,
288       NULL,
289       sizeof (GstSubBuffer),
290       0,
291       gst_subbuffer_init,
292       NULL
293     };
294
295     _gst_subbuffer_type = g_type_register_static (GST_TYPE_BUFFER,
296         "GstSubBuffer", &subbuffer_info, 0);
297   }
298   return _gst_subbuffer_type;
299 }
300
301 static void
302 gst_subbuffer_class_init (gpointer g_class, gpointer class_data)
303 {
304   GstBufferClass *buffer_class = GST_BUFFER_CLASS (g_class);
305
306   sub_parent_class = g_type_class_ref (GST_TYPE_BUFFER);
307
308   buffer_class->mini_object_class.finalize =
309       (GstMiniObjectFinalizeFunction) gst_subbuffer_finalize;
310 }
311
312 static void
313 gst_subbuffer_finalize (GstSubBuffer * buffer)
314 {
315   gst_buffer_unref (buffer->parent);
316
317   GST_MINI_OBJECT_CLASS (sub_parent_class)->finalize (GST_MINI_OBJECT (buffer));
318 }
319
320 static void
321 gst_subbuffer_init (GTypeInstance * instance, gpointer g_class)
322 {
323 }
324
325 /**
326  * gst_buffer_create_subbuffer:
327  * @parent: a parent #GstBuffer to create a subbuffer from.
328  * @offset: the offset into parent #GstBuffer.
329  * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
330  *
331  * Creates a sub-buffer from the parent at a given offset.
332  * This sub-buffer uses the actual memory space of the parent buffer.
333  * This function will copy the offset and timestamp field when the 
334  * offset is 0, else they are set to _NONE.
335  * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
336  *
337  * Returns: the new #GstBuffer, or NULL if there was an error.
338  * 
339  * MT safe.
340  */
341 GstBuffer *
342 gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size)
343 {
344   GstSubBuffer *subbuffer;
345   GstBuffer *parent;
346
347   g_return_val_if_fail (buffer != NULL, NULL);
348   g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL);
349   g_return_val_if_fail (size > 0, NULL);
350   g_return_val_if_fail (buffer->size >= offset + size, NULL);
351
352   /* find real parent */
353   if (GST_IS_SUBBUFFER (buffer)) {
354     parent = GST_SUBBUFFER (buffer)->parent;
355   } else {
356     parent = buffer;
357   }
358   gst_buffer_ref (parent);
359
360   /* create the new buffer */
361   subbuffer = (GstSubBuffer *) gst_mini_object_new (GST_TYPE_SUBBUFFER);
362   subbuffer->parent = parent;
363
364   GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", subbuffer,
365       parent);
366
367   /* set the right values in the child */
368   GST_BUFFER_DATA (GST_BUFFER_CAST (subbuffer)) = buffer->data + offset;
369   GST_BUFFER_SIZE (GST_BUFFER_CAST (subbuffer)) = size;
370
371   /* we can copy the timestamp and offset if the new buffer starts at
372    * offset 0 */
373   if (offset == 0) {
374     GST_BUFFER_TIMESTAMP (subbuffer) = GST_BUFFER_TIMESTAMP (buffer);
375     GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET (buffer);
376   } else {
377     GST_BUFFER_TIMESTAMP (subbuffer) = GST_CLOCK_TIME_NONE;
378     GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET_NONE;
379   }
380
381   GST_BUFFER_DURATION (subbuffer) = GST_CLOCK_TIME_NONE;
382   GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_NONE;
383
384   GST_BUFFER_CAPS (subbuffer) = NULL;
385
386   return GST_BUFFER_CAST (subbuffer);
387 }
388
389 /**
390  * gst_buffer_is_span_fast:
391  * @buf1: a first source #GstBuffer.
392  * @buf2: the second source #GstBuffer.
393  *
394  * Determines whether a gst_buffer_span() can be done without copying
395  * the contents, that is, whether the data areas are contiguous.
396  *
397  * Returns: TRUE if the buffers are contiguous, 
398  * FALSE if a copy would be required.
399  * 
400  * MT safe.
401  */
402 gboolean
403 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
404 {
405   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
406   g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
407   g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
408
409   /* it's only fast if we have subbuffers of the same parent */
410   return (GST_IS_SUBBUFFER (buf1) &&
411       GST_IS_SUBBUFFER (buf2) &&
412       (GST_SUBBUFFER (buf1)->parent == GST_SUBBUFFER (buf2)->parent) &&
413       ((buf1->data + buf1->size) == buf2->data));
414 }
415
416 /**
417  * gst_buffer_span:
418  * @buf1: a first source #GstBuffer to merge.
419  * @offset: the offset in the first buffer from where the new
420  * buffer should start.
421  * @buf2: the second source #GstBuffer to merge.
422  * @len: the total length of the new buffer.
423  *
424  * Creates a new buffer that consists of part of buf1 and buf2.
425  * Logically, buf1 and buf2 are concatenated into a single larger
426  * buffer, and a new buffer is created at the given offset inside
427  * this space, with a given length.
428  *
429  * If the two source buffers are children of the same larger buffer,
430  * and are contiguous, the new buffer will be a child of the shared
431  * parent, and thus no copying is necessary. you can use 
432  * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
433  *
434  * Returns: the new #GstBuffer that spans the two source buffers.
435  * 
436  * MT safe.
437  */
438 GstBuffer *
439 gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
440     guint32 len)
441 {
442   GstBuffer *newbuf;
443
444   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
445   g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
446   g_return_val_if_fail (buf2->mini_object.refcount > 0, FALSE);
447   g_return_val_if_fail (len > 0, NULL);
448   g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
449
450   /* if the two buffers have the same parent and are adjacent */
451   if (gst_buffer_is_span_fast (buf1, buf2)) {
452     GstBuffer *parent = GST_SUBBUFFER (buf1)->parent;
453
454     /* we simply create a subbuffer of the common parent */
455     newbuf = gst_buffer_create_sub (parent,
456         buf1->data - parent->data + offset, len);
457   } else {
458     GST_CAT_DEBUG (GST_CAT_BUFFER,
459         "slow path taken while spanning buffers %p and %p", buf1, buf2);
460     /* otherwise we simply have to brute-force copy the buffers */
461     newbuf = gst_buffer_new_and_alloc (len);
462
463     /* copy the first buffer's data across */
464     memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
465     /* copy the second buffer's data across */
466     memcpy (newbuf->data + (buf1->size - offset), buf2->data,
467         len - (buf1->size - offset));
468     /* if the offset is 0, the new buffer has the same timestamp as buf1 */
469     if (offset == 0) {
470       GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
471       GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
472     }
473   }
474   /* if we completely merged the two buffers (appended), we can
475    * calculate the duration too. Also make sure we's not messing with
476    * invalid DURATIONS */
477   if (offset == 0 && buf1->size + buf2->size == len) {
478     if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
479         GST_BUFFER_DURATION_IS_VALID (buf2)) {
480       /* add duration */
481       GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
482           GST_BUFFER_DURATION (buf2);
483     }
484     if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
485       /* add offset_end */
486       GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
487     }
488   }
489
490   return newbuf;
491 }