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