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