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