Inherit some of parent's buffer flags.
[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;
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 void
43 _gst_buffer_initialize (void)
44 {
45   _gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
46                        (GBoxedCopyFunc) gst_data_ref,
47                        (GBoxedFreeFunc) gst_data_unref);
48
49 #ifndef GST_DISABLE_TRACE
50   _gst_buffer_trace = gst_alloc_trace_register (GST_BUFFER_TRACE_NAME);
51 #endif
52
53   chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer), 
54                              sizeof (GstBuffer) * 200, 0);
55
56   GST_CAT_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
57 }
58
59 GType
60 gst_buffer_get_type (void)
61 {
62   return _gst_buffer_type;
63 }
64
65 static void
66 _gst_buffer_sub_free (GstBuffer *buffer)
67 {
68   gst_data_unref (GST_DATA (buffer->buffer_private));
69
70   GST_BUFFER_DATA (buffer) = NULL;
71   GST_BUFFER_SIZE (buffer) = 0;
72
73   _GST_DATA_DISPOSE (GST_DATA (buffer));
74   
75   gst_mem_chunk_free (chunk, GST_DATA (buffer));
76 #ifndef GST_DISABLE_TRACE
77   gst_alloc_trace_free (_gst_buffer_trace, buffer);
78 #endif
79 }
80
81 /**
82  * gst_buffer_default_free:
83  * @buffer: a #GstBuffer to free.
84  *
85  * Frees the memory associated with the buffer including the buffer data,
86  * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
87  */
88 void
89 gst_buffer_default_free (GstBuffer *buffer)
90 {
91   g_return_if_fail (buffer != NULL);
92
93   /* free our data */
94   if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer)) 
95     g_free (GST_BUFFER_DATA (buffer));
96
97   /* set to safe values */
98   GST_BUFFER_DATA (buffer) = NULL;
99   GST_BUFFER_SIZE (buffer) = 0;
100
101   _GST_DATA_DISPOSE (GST_DATA (buffer));
102
103   gst_mem_chunk_free (chunk, GST_DATA (buffer));
104 #ifndef GST_DISABLE_TRACE
105   gst_alloc_trace_free (_gst_buffer_trace, buffer);
106 #endif
107 }
108
109 /**
110  * gst_buffer_stamp:
111  * @dest: buffer to stamp
112  * @src: buffer to stamp from
113  *
114  * Copies additional information (timestamps and offsets) from one buffer to
115  * the other.
116  */
117 void
118 gst_buffer_stamp (GstBuffer *dest, const GstBuffer *src)
119 {
120   g_return_if_fail (dest != NULL);
121   g_return_if_fail (src != NULL);
122   
123   GST_BUFFER_TIMESTAMP (dest)    = GST_BUFFER_TIMESTAMP (src);
124   GST_BUFFER_DURATION (dest)     = GST_BUFFER_DURATION (src);
125   GST_BUFFER_OFFSET (dest)       = GST_BUFFER_OFFSET (src);
126   GST_BUFFER_OFFSET_END (dest)   = GST_BUFFER_OFFSET_END (src);
127 }
128 /**
129  * gst_buffer_default_copy:
130  * @buffer: a #GstBuffer to make a copy of.
131  *
132  * Make a full newly allocated copy of the given buffer, data and all.
133  *
134  * Returns: the new #GstBuffer.
135  */
136 GstBuffer*
137 gst_buffer_default_copy (GstBuffer *buffer)
138 {
139   GstBuffer *copy;
140
141   g_return_val_if_fail (buffer != NULL, NULL);
142
143   /* create a fresh new buffer */
144   copy = gst_mem_chunk_alloc (chunk);
145 #ifndef GST_DISABLE_TRACE
146   gst_alloc_trace_new (_gst_buffer_trace, copy);
147 #endif
148
149   _GST_DATA_INIT (GST_DATA (copy), 
150                   _gst_buffer_type,
151                   0,
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_stamp (copy, buffer);
162   GST_BUFFER_FREE_DATA_FUNC (copy) = NULL;
163   GST_BUFFER_PRIVATE (copy) = NULL;
164
165   return copy;
166 }
167
168 /**
169  * gst_buffer_new:
170  *
171  * Creates a newly allocated buffer without any data.
172  *
173  * Returns: the new #GstBuffer.
174  */
175 GstBuffer*
176 gst_buffer_new (void)
177 {
178   GstBuffer *newbuf;
179   
180   newbuf = gst_mem_chunk_alloc (chunk);
181 #ifndef GST_DISABLE_TRACE
182   gst_alloc_trace_new (_gst_buffer_trace, newbuf);
183 #endif
184
185   GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
186
187   _GST_DATA_INIT (GST_DATA (newbuf), 
188                   _gst_buffer_type,
189                   0,
190                   (GstDataFreeFunction) gst_buffer_default_free,
191                   (GstDataCopyFunction) gst_buffer_default_copy);
192
193   GST_BUFFER_DATA (newbuf)         = NULL;
194   GST_BUFFER_SIZE (newbuf)         = 0;
195   GST_BUFFER_MAXSIZE (newbuf)      = GST_BUFFER_MAXSIZE_NONE;
196   GST_BUFFER_TIMESTAMP (newbuf)    = GST_CLOCK_TIME_NONE;
197   GST_BUFFER_DURATION (newbuf)     = GST_CLOCK_TIME_NONE;
198   GST_BUFFER_OFFSET (newbuf)       = GST_BUFFER_OFFSET_NONE;
199   GST_BUFFER_OFFSET_END (newbuf)   = GST_BUFFER_OFFSET_NONE;
200   GST_BUFFER_FREE_DATA_FUNC (newbuf) = NULL;
201   GST_BUFFER_PRIVATE (newbuf) = NULL;
202
203   return newbuf;
204 }
205
206 /**
207  * gst_buffer_new_and_alloc:
208  * @size: the size of the new buffer's data.
209  *
210  * Creates a newly allocated buffer with data of the given size.
211  *
212  * Returns: the new #GstBuffer.
213  */
214 GstBuffer*
215 gst_buffer_new_and_alloc (guint size)
216 {
217   GstBuffer *newbuf;
218
219   newbuf = gst_buffer_new ();
220
221   GST_BUFFER_DATA (newbuf)    = g_malloc (size);
222   GST_BUFFER_SIZE (newbuf)    = size;
223   GST_BUFFER_MAXSIZE (newbuf) = size;
224
225   return newbuf;
226 }
227
228 /**
229  * gst_buffer_create_sub:
230  * @parent: a parent #GstBuffer to create a subbuffer from.
231  * @offset: the offset into parent #GstBuffer.
232  * @size: the size of the new #GstBuffer sub-buffer (with size > 0).
233  *
234  * Creates a sub-buffer from the parent at a given offset.
235  * This sub-buffer uses the actual memory space of the parent buffer.
236  * This function will copy the offset and timestamp field when the 
237  * offset is 0, else they are set to _NONE.
238  * The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
239  *
240  * Returns: the new #GstBuffer, or NULL if there was an error.
241  */
242 GstBuffer*
243 gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
244 {
245   GstBuffer *buffer;
246   gpointer buffer_data;
247               
248   g_return_val_if_fail (parent != NULL, NULL);
249   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
250   g_return_val_if_fail (size > 0, NULL);
251   g_return_val_if_fail (parent->size >= offset + size, NULL);
252
253   /* remember the data for the new buffer */
254   buffer_data = parent->data + offset;
255   /* make sure we're child not child from a child buffer */
256   while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
257     parent = GST_BUFFER (parent->buffer_private);
258   }
259   /* ref the real parent */
260   gst_data_ref (GST_DATA (parent));
261
262   /* create the new buffer */
263   buffer = gst_mem_chunk_alloc (chunk);
264 #ifndef GST_DISABLE_TRACE
265   gst_alloc_trace_new (_gst_buffer_trace, buffer);
266 #endif
267
268   GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p", buffer);
269
270   /* make sure nobody overwrites data in the new buffer 
271    * by setting the READONLY flag */
272   _GST_DATA_INIT (GST_DATA (buffer), 
273                   _gst_buffer_type,
274                   GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
275                   GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
276                   (GstDataFreeFunction) _gst_buffer_sub_free,
277                   (GstDataCopyFunction) gst_buffer_default_copy);
278
279   /* set the right values in the child */
280   GST_BUFFER_DATA (buffer)         = buffer_data;
281   GST_BUFFER_SIZE (buffer)         = size;
282   GST_BUFFER_MAXSIZE (buffer)      = size;
283   GST_BUFFER_FREE_DATA_FUNC (buffer) = NULL;
284   GST_BUFFER_PRIVATE (buffer) = parent;
285   /* we can copy the timestamp and offset if the new buffer starts at
286    * offset 0 */
287   if (offset == 0) {
288     GST_BUFFER_TIMESTAMP (buffer)    = GST_BUFFER_TIMESTAMP (parent);
289     GST_BUFFER_OFFSET (buffer)       = GST_BUFFER_OFFSET (parent);
290   }
291   else {
292     GST_BUFFER_TIMESTAMP (buffer)    = GST_CLOCK_TIME_NONE;
293     GST_BUFFER_OFFSET (buffer)       = GST_BUFFER_OFFSET_NONE;
294   }
295
296   GST_BUFFER_DURATION (buffer)     = GST_CLOCK_TIME_NONE;
297   GST_BUFFER_OFFSET_END (buffer)   = GST_BUFFER_OFFSET_NONE;
298
299   if (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_DONTKEEP)) {
300     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_DONTKEEP);
301   }
302   if (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_READONLY)) {
303     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_READONLY);
304   }
305
306   return buffer;
307 }
308
309
310 /**
311  * gst_buffer_merge:
312  * @buf1: a first source #GstBuffer to merge.
313  * @buf2: the second source #GstBuffer to merge.
314  *
315  * Create a new buffer that is the concatenation of the two source
316  * buffers.  The original source buffers will not be modified or
317  * unref'd.
318  *
319  * Internally is nothing more than a specialized gst_buffer_span(),
320  * so the same optimizations can occur.
321  *
322  * Returns: the new #GstBuffer that's the concatenation of the source buffers.
323  */
324 GstBuffer*
325 gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
326 {
327   GstBuffer *result;
328
329   /* we're just a specific case of the more general gst_buffer_span() */
330   result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
331
332   return result;
333 }
334
335 /**
336  * gst_buffer_is_span_fast:
337  * @buf1: a first source #GstBuffer.
338  * @buf2: the second source #GstBuffer.
339  *
340  * Determines whether a gst_buffer_span() is free (as in free beer), 
341  * or requires a memcpy. 
342  *
343  * Returns: TRUE if the buffers are contiguous, 
344  * FALSE if a copy would be required.
345  */
346 gboolean
347 gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
348 {
349   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
350   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
351   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
352
353   /* it's only fast if we have subbuffers of the same parent */
354   return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
355           (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
356           (buf1->buffer_private == buf2->buffer_private) &&
357           ((buf1->data + buf1->size) == buf2->data));
358 }
359
360 /**
361  * gst_buffer_span:
362  * @buf1: a first source #GstBuffer to merge.
363  * @offset: the offset in the first buffer from where the new
364  * buffer should start.
365  * @buf2: the second source #GstBuffer to merge.
366  * @len: the total length of the new buffer.
367  *
368  * Creates a new buffer that consists of part of buf1 and buf2.
369  * Logically, buf1 and buf2 are concatenated into a single larger
370  * buffer, and a new buffer is created at the given offset inside
371  * this space, with a given length.
372  *
373  * If the two source buffers are children of the same larger buffer,
374  * and are contiguous, the new buffer will be a child of the shared
375  * parent, and thus no copying is necessary. you can use 
376  * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
377  *
378  * Returns: the new #GstBuffer that spans the two source buffers.
379  */
380 GstBuffer*
381 gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
382 {
383   GstBuffer *newbuf;
384
385   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
386   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
387   g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
388   g_return_val_if_fail (len > 0, NULL);
389   g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
390
391   /* if the two buffers have the same parent and are adjacent */
392   if (gst_buffer_is_span_fast (buf1, buf2)) {
393     GstBuffer *parent = GST_BUFFER (buf1->buffer_private);
394     /* we simply create a subbuffer of the common parent */
395     newbuf = gst_buffer_create_sub (parent, 
396                                     buf1->data - parent->data + offset, len);
397   }
398   else {
399     GST_CAT_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p", 
400                buf1, buf2);
401     /* otherwise we simply have to brute-force copy the buffers */
402     newbuf = gst_buffer_new_and_alloc (len);
403
404     /* copy the first buffer's data across */
405     memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
406     /* copy the second buffer's data across */
407     memcpy (newbuf->data + (buf1->size - offset), buf2->data, 
408             len - (buf1->size - offset));
409     /* if the offset is 0, the new buffer has the same timestamp as buf1 */
410     if (offset == 0) {
411       GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
412       GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
413     }
414   }
415   /* if we completely merged the two buffers (appended), we can
416    * calculate the duration too. Also make sure we's not messing with
417    * invalid DURATIONS */
418   if (offset == 0 && buf1->size + buf2->size == len) {
419     if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
420         GST_BUFFER_DURATION_IS_VALID (buf2)) {
421       /* add duration */
422       GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) + 
423                                     GST_BUFFER_DURATION (buf2);
424     }
425     if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
426       /* add offset_end */
427       GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
428     }
429   }
430
431   return newbuf;
432 }
433