check/Makefile.am: remove GstData checks
[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_DATA (buffer) = NULL;
152   //GST_BUFFER_SIZE (buffer) = 0;
153   GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
154   GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
155   GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
156   GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
157   //GST_BUFFER_CAPS (buffer) = NULL;
158 }
159
160 /**
161  * gst_buffer_new:
162  *
163  * Creates a newly allocated buffer without any data.
164  *
165  * Returns: the new #GstBuffer.
166  * 
167  * MT safe.
168  */
169 GstBuffer *
170 gst_buffer_new (void)
171 {
172   GstBuffer *newbuf;
173
174   newbuf = (GstBuffer *) gst_mini_object_new (GST_TYPE_BUFFER);
175
176   GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
177
178   return newbuf;
179 }
180
181 /**
182  * gst_buffer_new_and_alloc:
183  * @size: the size of the new buffer's data.
184  *
185  * Creates a newly allocated buffer with data of the given size.
186  *
187  * Returns: the new #GstBuffer.
188  * 
189  * MT safe.
190  */
191 GstBuffer *
192 gst_buffer_new_and_alloc (guint size)
193 {
194   GstBuffer *newbuf;
195
196   newbuf = gst_buffer_new ();
197
198   newbuf->malloc_data = g_malloc (size);
199   GST_BUFFER_DATA (newbuf) = newbuf->malloc_data;
200   GST_BUFFER_SIZE (newbuf) = size;
201
202   return newbuf;
203 }
204
205
206 /**
207  * gst_buffer_get_caps:
208  * @buffer: a #GstBuffer to get the caps of.
209  *
210  * Gets the media type of the buffer. This can be NULL if there
211  * is not media type attached to this buffer or when the media
212  * type is the same as the previous received buffer.
213  *
214  * This function does not increment the refcount of the caps. The
215  * caps pointer will therefore remain valid until the buffer is 
216  * unreffed.
217  *
218  * Returns: the #GstCaps, or NULL if there was an error or there
219  * were no caps on this buffer.
220  */
221 /* FIXME can we make this threadsafe without a lock on the buffer?
222  * We can use compare and swap and atomic reads. */
223 GstCaps *
224 gst_buffer_get_caps (GstBuffer * buffer)
225 {
226   g_return_val_if_fail (buffer != NULL, NULL);
227
228   return GST_BUFFER_CAPS (buffer);
229 }
230
231 /**
232  * gst_buffer_set_caps:
233  * @buffer: a #GstBuffer to set the caps of.
234  * @caps: a #GstCaps to set.
235  *
236  * Sets the media type on the buffer. The caps' refcount will
237  * be increased and any previous caps on the buffer will be
238  * unreffed.
239  */
240 /* FIXME can we make this threadsafe without a lock on the buffer? 
241  * We can use compare and swap and atomic reads. Another idea is to
242  * not attach the caps to the buffer but use an event to signal a caps
243  * change. */
244 void
245 gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
246 {
247   g_return_if_fail (buffer != NULL);
248
249   gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
250 }
251
252
253 typedef struct _GstSubBuffer GstSubBuffer;
254 typedef struct _GstSubBufferClass GstSubBufferClass;
255
256 #define GST_TYPE_SUBBUFFER                         (gst_subbuffer_get_type())
257
258 #define GST_IS_SUBBUFFER(obj)  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_BUFFER))
259 #define GST_SUBBUFFER(obj)     (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SUBBUFFER, GstSubBuffer))
260
261 struct _GstSubBuffer
262 {
263   GstBuffer buffer;
264
265   GstBuffer *parent;
266 };
267
268 struct _GstSubBufferClass
269 {
270   GstBufferClass buffer_class;
271 };
272
273 static void gst_subbuffer_init (GTypeInstance * instance, gpointer g_class);
274
275 static GType
276 gst_subbuffer_get_type (void)
277 {
278   static GType _gst_subbuffer_type;
279
280   if (G_UNLIKELY (_gst_subbuffer_type == 0)) {
281     static const GTypeInfo subbuffer_info = {
282       sizeof (GstSubBufferClass),
283       NULL,
284       NULL,
285       NULL,
286       NULL,
287       NULL,
288       sizeof (GstSubBuffer),
289       0,
290       gst_subbuffer_init,
291       NULL
292     };
293
294     _gst_subbuffer_type = g_type_register_static (GST_TYPE_BUFFER,
295         "GstSubBuffer", &subbuffer_info, 0);
296   }
297   return _gst_subbuffer_type;
298 }
299
300 static void
301 gst_subbuffer_init (GTypeInstance * instance, gpointer g_class)
302 {
303
304 }
305
306 /**
307  * gst_buffer_create_subbuffer:
308  * @parent: a parent #GstBuffer to create a subbuffer from.
309  * @offset: the offset into parent #GstBuffer.
310  * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
311  *
312  * Creates a sub-buffer from the parent at a given offset.
313  * This sub-buffer uses the actual memory space of the parent buffer.
314  * This function will copy the offset and timestamp field when the 
315  * offset is 0, else they are set to _NONE.
316  * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
317  *
318  * Returns: the new #GstBuffer, or NULL if there was an error.
319  * 
320  * MT safe.
321  */
322 GstBuffer *
323 gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size)
324 {
325   GstSubBuffer *subbuffer;
326   GstBuffer *parent;
327
328   g_return_val_if_fail (buffer != NULL, NULL);
329   g_return_val_if_fail (buffer->mini_object.refcount > 0, NULL);
330   g_return_val_if_fail (size > 0, NULL);
331   g_return_val_if_fail (buffer->size >= offset + size, NULL);
332
333   /* find real parent */
334   if (GST_IS_SUBBUFFER (buffer)) {
335     parent = GST_SUBBUFFER (buffer)->parent;
336   } else {
337     parent = buffer;
338   }
339   gst_buffer_ref (parent);
340
341   /* create the new buffer */
342   subbuffer = (GstSubBuffer *) gst_mini_object_new (GST_TYPE_SUBBUFFER);
343   subbuffer->parent = parent;
344
345   GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", subbuffer,
346       parent);
347
348   /* set the right values in the child */
349   GST_BUFFER_DATA (GST_BUFFER (subbuffer)) = buffer->data + offset;
350   GST_BUFFER_SIZE (GST_BUFFER (subbuffer)) = size;
351
352   /* we can copy the timestamp and offset if the new buffer starts at
353    * offset 0 */
354   if (offset == 0) {
355     GST_BUFFER_TIMESTAMP (subbuffer) = GST_BUFFER_TIMESTAMP (buffer);
356     GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET (buffer);
357   } else {
358     GST_BUFFER_TIMESTAMP (subbuffer) = GST_CLOCK_TIME_NONE;
359     GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET_NONE;
360   }
361
362   GST_BUFFER_DURATION (subbuffer) = GST_CLOCK_TIME_NONE;
363   GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_NONE;
364
365   GST_BUFFER_CAPS (subbuffer) = NULL;
366
367   return GST_BUFFER (subbuffer);
368 }
369
370 /**
371  * gst_buffer_is_span_fast:
372  * @buf1: a first source #GstBuffer.
373  * @buf2: the second source #GstBuffer.
374  *
375  * Determines whether a gst_buffer_span() can be done without copying
376  * the contents, that is, whether the data areas are contiguous.
377  *
378  * Returns: TRUE if the buffers are contiguous, 
379  * FALSE if a copy would be required.
380  * 
381  * MT safe.
382  */
383 gboolean
384 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
385 {
386   return FALSE;
387 #if 0
388   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
389   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
390   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
391
392   /* it's only fast if we have subbuffers of the same parent */
393   return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
394       (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
395       (buf1->buffer_private == buf2->buffer_private) &&
396       ((buf1->data + buf1->size) == buf2->data));
397 #endif
398 }
399
400 /**
401  * gst_buffer_span:
402  * @buf1: a first source #GstBuffer to merge.
403  * @offset: the offset in the first buffer from where the new
404  * buffer should start.
405  * @buf2: the second source #GstBuffer to merge.
406  * @len: the total length of the new buffer.
407  *
408  * Creates a new buffer that consists of part of buf1 and buf2.
409  * Logically, buf1 and buf2 are concatenated into a single larger
410  * buffer, and a new buffer is created at the given offset inside
411  * this space, with a given length.
412  *
413  * If the two source buffers are children of the same larger buffer,
414  * and are contiguous, the new buffer will be a child of the shared
415  * parent, and thus no copying is necessary. you can use 
416  * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
417  *
418  * Returns: the new #GstBuffer that spans the two source buffers.
419  * 
420  * MT safe.
421  */
422 GstBuffer *
423 gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
424     guint32 len)
425 {
426   return NULL;
427 #if 0
428   GstBuffer *newbuf;
429
430   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
431   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
432   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
433   g_return_val_if_fail (len > 0, NULL);
434   g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
435
436   /* if the two buffers have the same parent and are adjacent */
437   if (gst_buffer_is_span_fast (buf1, buf2)) {
438     GstBuffer *parent = GST_BUFFER (buf1->buffer_private);
439
440     /* we simply create a subbuffer of the common parent */
441     newbuf = gst_buffer_create_sub (parent,
442         buf1->data - parent->data + offset, len);
443   } else {
444     GST_CAT_DEBUG (GST_CAT_BUFFER,
445         "slow path taken while spanning buffers %p and %p", buf1, buf2);
446     /* otherwise we simply have to brute-force copy the buffers */
447     newbuf = gst_buffer_new_and_alloc (len);
448
449     /* copy the first buffer's data across */
450     memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
451     /* copy the second buffer's data across */
452     memcpy (newbuf->data + (buf1->size - offset), buf2->data,
453         len - (buf1->size - offset));
454     /* if the offset is 0, the new buffer has the same timestamp as buf1 */
455     if (offset == 0) {
456       GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
457       GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
458     }
459   }
460   /* if we completely merged the two buffers (appended), we can
461    * calculate the duration too. Also make sure we's not messing with
462    * invalid DURATIONS */
463   if (offset == 0 && buf1->size + buf2->size == len) {
464     if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
465         GST_BUFFER_DURATION_IS_VALID (buf2)) {
466       /* add duration */
467       GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
468           GST_BUFFER_DURATION (buf2);
469     }
470     if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
471       /* add offset_end */
472       GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
473     }
474   }
475
476   return newbuf;
477 #endif
478 }