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