2000-12-28 22:12:02 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wtay@chello.be>
|
|
|
|
*
|
|
|
|
* gstbuffer.c: Buffer operations
|
2000-01-30 09:03:00 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2000-12-28 22:12:02 +00:00
|
|
|
#include "gst_private.h"
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2002-12-31 03:21:08 +00:00
|
|
|
#include "gstatomic_impl.h"
|
2002-07-08 19:22:02 +00:00
|
|
|
#include "gstdata_private.h"
|
2000-12-15 01:57:34 +00:00
|
|
|
#include "gstbuffer.h"
|
2002-07-08 19:22:02 +00:00
|
|
|
#include "gstmemchunk.h"
|
2003-06-29 14:05:49 +00:00
|
|
|
#include "gstinfo.h"
|
2000-12-28 22:12:02 +00:00
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
GType _gst_buffer_type = 0;
|
2001-10-17 10:21:27 +00:00
|
|
|
|
2003-02-10 20:32:32 +00:00
|
|
|
#ifndef GST_DISABLE_TRACE
|
|
|
|
/* #define GST_WITH_ALLOC_TRACE */
|
|
|
|
#include "gsttrace.h"
|
|
|
|
|
2003-02-02 19:40:10 +00:00
|
|
|
static GstAllocTrace *_gst_buffer_trace;
|
2003-02-10 20:32:32 +00:00
|
|
|
#endif
|
2002-07-08 19:22:02 +00:00
|
|
|
|
|
|
|
static GstMemChunk *chunk;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2004-04-05 18:39:01 +00:00
|
|
|
static GstBuffer *gst_buffer_alloc_chunk (void);
|
|
|
|
static void gst_buffer_free_chunk (GstBuffer * buffer);
|
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
void
|
|
|
|
_gst_buffer_initialize (void)
|
2000-11-11 15:13:50 +00:00
|
|
|
{
|
2004-04-13 02:22:02 +00:00
|
|
|
gst_buffer_get_type ();
|
2002-07-08 19:22:02 +00:00
|
|
|
|
2003-02-10 20:32:32 +00:00
|
|
|
#ifndef GST_DISABLE_TRACE
|
2003-02-02 19:40:10 +00:00
|
|
|
_gst_buffer_trace = gst_alloc_trace_register (GST_BUFFER_TRACE_NAME);
|
2003-02-10 20:32:32 +00:00
|
|
|
#endif
|
2002-07-08 19:22:02 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer),
|
|
|
|
sizeof (GstBuffer) * 200, 0);
|
2002-07-08 19:22:02 +00:00
|
|
|
|
2004-02-20 13:18:32 +00:00
|
|
|
GST_CAT_LOG (GST_CAT_BUFFER, "Buffers are initialized now");
|
2001-12-22 21:18:17 +00:00
|
|
|
}
|
|
|
|
|
2003-03-29 11:58:15 +00:00
|
|
|
GType
|
|
|
|
gst_buffer_get_type (void)
|
|
|
|
{
|
2005-03-07 18:27:42 +00:00
|
|
|
if (G_UNLIKELY (_gst_buffer_type == 0)) {
|
2004-04-13 02:22:02 +00:00
|
|
|
_gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
|
|
|
|
(GBoxedCopyFunc) gst_data_copy, (GBoxedFreeFunc) gst_data_unref);
|
|
|
|
}
|
2003-03-29 11:58:15 +00:00
|
|
|
return _gst_buffer_type;
|
|
|
|
}
|
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
static void
|
2004-03-13 15:27:01 +00:00
|
|
|
_gst_buffer_sub_free (GstBuffer * buffer)
|
2002-07-08 19:22:02 +00:00
|
|
|
{
|
2004-01-11 22:11:35 +00:00
|
|
|
gst_data_unref (GST_DATA (buffer->buffer_private));
|
2002-07-08 19:22:02 +00:00
|
|
|
|
|
|
|
GST_BUFFER_DATA (buffer) = NULL;
|
|
|
|
GST_BUFFER_SIZE (buffer) = 0;
|
2005-03-07 18:27:42 +00:00
|
|
|
if (GST_BUFFER_CAPS (buffer))
|
|
|
|
gst_caps_unref (GST_BUFFER_CAPS (buffer));
|
2002-07-08 19:22:02 +00:00
|
|
|
|
|
|
|
_GST_DATA_DISPOSE (GST_DATA (buffer));
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2004-04-05 18:39:01 +00:00
|
|
|
gst_buffer_free_chunk (buffer);
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2000-09-14 20:31:03 +00:00
|
|
|
/**
|
2002-07-13 23:12:22 +00:00
|
|
|
* gst_buffer_default_free:
|
2002-08-30 14:54:58 +00:00
|
|
|
* @buffer: a #GstBuffer to free.
|
2000-09-14 20:31:03 +00:00
|
|
|
*
|
2002-08-30 14:54:58 +00:00
|
|
|
* Frees the memory associated with the buffer including the buffer data,
|
2002-07-08 19:22:02 +00:00
|
|
|
* unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2000-09-14 20:31:03 +00:00
|
|
|
*/
|
2002-07-08 19:22:02 +00:00
|
|
|
void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_buffer_default_free (GstBuffer * buffer)
|
2000-08-14 10:55:35 +00:00
|
|
|
{
|
2002-11-22 23:15:14 +00:00
|
|
|
g_return_if_fail (buffer != NULL);
|
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
/* free our data */
|
2004-01-12 01:41:23 +00:00
|
|
|
if (GST_BUFFER_FREE_DATA_FUNC (buffer)) {
|
|
|
|
GST_BUFFER_FREE_DATA_FUNC (buffer) (buffer);
|
2004-03-13 15:27:01 +00:00
|
|
|
} else if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE)) {
|
2002-07-08 19:22:02 +00:00
|
|
|
g_free (GST_BUFFER_DATA (buffer));
|
2004-01-12 01:41:23 +00:00
|
|
|
}
|
2001-08-27 04:19:58 +00:00
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
/* set to safe values */
|
|
|
|
GST_BUFFER_DATA (buffer) = NULL;
|
|
|
|
GST_BUFFER_SIZE (buffer) = 0;
|
2005-03-07 18:27:42 +00:00
|
|
|
if (GST_BUFFER_CAPS (buffer))
|
|
|
|
gst_caps_unref (GST_BUFFER_CAPS (buffer));
|
2001-10-17 10:21:27 +00:00
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
_GST_DATA_DISPOSE (GST_DATA (buffer));
|
|
|
|
|
2004-04-05 18:39:01 +00:00
|
|
|
gst_buffer_free_chunk (buffer);
|
2000-08-14 10:55:35 +00:00
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2000-01-30 09:03:00 +00:00
|
|
|
/**
|
2002-07-13 23:12:22 +00:00
|
|
|
* gst_buffer_default_copy:
|
2002-08-30 14:54:58 +00:00
|
|
|
* @buffer: a #GstBuffer to make a copy of.
|
2000-01-30 09:03:00 +00:00
|
|
|
*
|
2002-07-08 19:22:02 +00:00
|
|
|
* Make a full newly allocated copy of the given buffer, data and all.
|
2005-03-07 18:27:42 +00:00
|
|
|
* Note that the caps on the buffer are not copied but their refcount
|
|
|
|
* is increased.
|
2000-01-30 09:03:00 +00:00
|
|
|
*
|
2002-08-30 14:54:58 +00:00
|
|
|
* Returns: the new #GstBuffer.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2000-01-30 09:03:00 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstBuffer *
|
|
|
|
gst_buffer_default_copy (GstBuffer * buffer)
|
2000-11-11 15:13:50 +00:00
|
|
|
{
|
2002-07-08 19:22:02 +00:00
|
|
|
GstBuffer *copy;
|
2004-05-20 17:44:03 +00:00
|
|
|
guint16 flags;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2002-11-22 23:15:14 +00:00
|
|
|
g_return_val_if_fail (buffer != NULL, NULL);
|
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
/* create a fresh new buffer */
|
2004-04-05 18:39:01 +00:00
|
|
|
copy = gst_buffer_alloc_chunk ();
|
2003-05-06 21:03:34 +00:00
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", buffer, copy);
|
|
|
|
|
2004-05-20 17:44:03 +00:00
|
|
|
/* copy relevant flags */
|
2005-03-07 18:27:42 +00:00
|
|
|
flags = GST_DATA_FLAG_SHIFT (GST_BUFFER_PREROLL) |
|
2004-08-16 10:35:36 +00:00
|
|
|
GST_DATA_FLAG_SHIFT (GST_BUFFER_IN_CAPS) |
|
|
|
|
GST_DATA_FLAG_SHIFT (GST_BUFFER_DELTA_UNIT);
|
2004-06-09 16:04:37 +00:00
|
|
|
flags = GST_BUFFER_FLAGS (buffer) & flags;
|
2004-05-20 17:44:03 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
_GST_DATA_INIT (GST_DATA (copy),
|
|
|
|
_gst_buffer_type,
|
2004-05-20 17:44:03 +00:00
|
|
|
flags,
|
2004-03-13 15:27:01 +00:00
|
|
|
(GstDataFreeFunction) gst_buffer_default_free,
|
|
|
|
(GstDataCopyFunction) gst_buffer_default_copy);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
/* we simply copy everything from our parent */
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_BUFFER_DATA (copy) = g_memdup (GST_BUFFER_DATA (buffer),
|
|
|
|
GST_BUFFER_SIZE (buffer));
|
|
|
|
GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
|
|
|
|
GST_BUFFER_MAXSIZE (copy) = GST_BUFFER_SIZE (buffer);
|
2003-12-22 01:39:35 +00:00
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
|
|
|
|
GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
|
|
|
|
GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
|
|
|
|
GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
|
|
|
|
|
2004-01-11 22:11:35 +00:00
|
|
|
GST_BUFFER_FREE_DATA_FUNC (copy) = NULL;
|
|
|
|
GST_BUFFER_PRIVATE (copy) = NULL;
|
2005-03-07 18:27:42 +00:00
|
|
|
if (GST_BUFFER_CAPS (buffer))
|
|
|
|
GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
return copy;
|
|
|
|
}
|
2001-10-17 10:21:27 +00:00
|
|
|
|
2004-04-05 18:39:01 +00:00
|
|
|
static GstBuffer *
|
|
|
|
gst_buffer_alloc_chunk (void)
|
|
|
|
{
|
|
|
|
GstBuffer *newbuf;
|
|
|
|
|
|
|
|
newbuf = gst_mem_chunk_alloc (chunk);
|
|
|
|
#ifndef GST_DISABLE_TRACE
|
|
|
|
gst_alloc_trace_new (_gst_buffer_trace, newbuf);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return newbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_buffer_free_chunk (GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
gst_mem_chunk_free (chunk, GST_DATA (buffer));
|
|
|
|
#ifndef GST_DISABLE_TRACE
|
|
|
|
gst_alloc_trace_free (_gst_buffer_trace, buffer);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-08-14 10:55:35 +00:00
|
|
|
/**
|
2002-07-08 19:22:02 +00:00
|
|
|
* gst_buffer_new:
|
2000-08-14 10:55:35 +00:00
|
|
|
*
|
2002-07-08 19:22:02 +00:00
|
|
|
* Creates a newly allocated buffer without any data.
|
2000-08-14 10:55:35 +00:00
|
|
|
*
|
2002-08-30 14:54:58 +00:00
|
|
|
* Returns: the new #GstBuffer.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2000-08-14 10:55:35 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstBuffer *
|
2002-07-08 19:22:02 +00:00
|
|
|
gst_buffer_new (void)
|
2000-11-11 15:13:50 +00:00
|
|
|
{
|
2003-05-24 10:09:39 +00:00
|
|
|
GstBuffer *newbuf;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2004-04-05 18:39:01 +00:00
|
|
|
newbuf = gst_buffer_alloc_chunk ();
|
2003-02-10 20:32:32 +00:00
|
|
|
|
2003-06-29 14:05:49 +00:00
|
|
|
GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
|
2000-08-14 10:55:35 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
_GST_DATA_INIT (GST_DATA (newbuf),
|
|
|
|
_gst_buffer_type,
|
|
|
|
0,
|
|
|
|
(GstDataFreeFunction) gst_buffer_default_free,
|
|
|
|
(GstDataCopyFunction) gst_buffer_default_copy);
|
|
|
|
|
|
|
|
GST_BUFFER_DATA (newbuf) = NULL;
|
|
|
|
GST_BUFFER_SIZE (newbuf) = 0;
|
|
|
|
GST_BUFFER_MAXSIZE (newbuf) = GST_BUFFER_MAXSIZE_NONE;
|
|
|
|
GST_BUFFER_TIMESTAMP (newbuf) = GST_CLOCK_TIME_NONE;
|
|
|
|
GST_BUFFER_DURATION (newbuf) = GST_CLOCK_TIME_NONE;
|
|
|
|
GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET_NONE;
|
|
|
|
GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_NONE;
|
2004-01-11 22:11:35 +00:00
|
|
|
GST_BUFFER_FREE_DATA_FUNC (newbuf) = NULL;
|
|
|
|
GST_BUFFER_PRIVATE (newbuf) = NULL;
|
2005-03-07 18:27:42 +00:00
|
|
|
GST_BUFFER_CAPS (newbuf) = NULL;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2003-05-24 10:09:39 +00:00
|
|
|
return newbuf;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2000-03-27 19:53:43 +00:00
|
|
|
/**
|
2002-07-08 19:22:02 +00:00
|
|
|
* gst_buffer_new_and_alloc:
|
2002-08-30 14:54:58 +00:00
|
|
|
* @size: the size of the new buffer's data.
|
2002-07-08 19:22:02 +00:00
|
|
|
*
|
|
|
|
* Creates a newly allocated buffer with data of the given size.
|
2000-03-27 19:53:43 +00:00
|
|
|
*
|
2002-08-30 14:54:58 +00:00
|
|
|
* Returns: the new #GstBuffer.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2000-03-27 19:53:43 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstBuffer *
|
2002-07-08 19:22:02 +00:00
|
|
|
gst_buffer_new_and_alloc (guint size)
|
2000-11-11 15:13:50 +00:00
|
|
|
{
|
2003-05-24 10:09:39 +00:00
|
|
|
GstBuffer *newbuf;
|
2002-07-08 19:22:02 +00:00
|
|
|
|
2003-05-24 10:09:39 +00:00
|
|
|
newbuf = gst_buffer_new ();
|
2002-07-08 19:22:02 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_BUFFER_DATA (newbuf) = g_malloc (size);
|
|
|
|
GST_BUFFER_SIZE (newbuf) = size;
|
2003-05-24 10:09:39 +00:00
|
|
|
GST_BUFFER_MAXSIZE (newbuf) = size;
|
2002-07-08 19:22:02 +00:00
|
|
|
|
2003-05-24 10:09:39 +00:00
|
|
|
return newbuf;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_buffer_get_caps:
|
|
|
|
* @buffer: a #GstBuffer to get the caps of.
|
|
|
|
*
|
|
|
|
* Gets the media type of the buffer. This can be NULL if there
|
|
|
|
* is not media type attached to this buffer or when the media
|
|
|
|
* type is the same as the previous received buffer.
|
|
|
|
*
|
|
|
|
* This function does not increment the refcount of the caps. The
|
|
|
|
* caps pointer will therefore remain valid until the buffer is
|
|
|
|
* unreffed.
|
|
|
|
*
|
|
|
|
* Returns: the #GstCaps, or NULL if there was an error or there
|
|
|
|
* were no caps on this buffer.
|
|
|
|
*/
|
2005-03-10 12:51:45 +00:00
|
|
|
/* FIXME can we make this threadsafe without a lock on the buffer?
|
|
|
|
* We can use compare and swap and atomic reads. */
|
2005-03-07 18:27:42 +00:00
|
|
|
GstCaps *
|
|
|
|
gst_buffer_get_caps (GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (buffer != NULL, NULL);
|
|
|
|
|
|
|
|
return GST_BUFFER_CAPS (buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_buffer_set_caps:
|
|
|
|
* @buffer: a #GstBuffer to set the caps of.
|
|
|
|
* @caps: a #GstCaps to set.
|
|
|
|
*
|
|
|
|
* Sets the media type on the buffer. The caps' refcount will
|
|
|
|
* be increased and any previous caps on the buffer will be
|
|
|
|
* unreffed.
|
|
|
|
*/
|
2005-03-10 12:51:45 +00:00
|
|
|
/* FIXME can we make this threadsafe without a lock on the buffer?
|
|
|
|
* We can use compare and swap and atomic reads. Another idea is to
|
|
|
|
* not attach the caps to the buffer but use an event to signal a caps
|
|
|
|
* change. */
|
2005-03-07 18:27:42 +00:00
|
|
|
void
|
|
|
|
gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstCaps *oldcaps;
|
|
|
|
|
|
|
|
g_return_if_fail (buffer != NULL);
|
|
|
|
|
|
|
|
/* get old caps */
|
|
|
|
oldcaps = GST_BUFFER_CAPS (buffer);
|
|
|
|
/* ref new caps if any */
|
|
|
|
if (caps)
|
|
|
|
caps = gst_caps_ref (caps);
|
|
|
|
/* set caps */
|
|
|
|
GST_BUFFER_CAPS (buffer) = caps;
|
|
|
|
|
|
|
|
/* unref old caps if any */
|
|
|
|
if (oldcaps) {
|
|
|
|
gst_caps_unref (oldcaps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-27 19:53:43 +00:00
|
|
|
/**
|
2002-07-08 19:22:02 +00:00
|
|
|
* gst_buffer_create_sub:
|
2002-08-30 14:54:58 +00:00
|
|
|
* @parent: a parent #GstBuffer to create a subbuffer from.
|
|
|
|
* @offset: the offset into parent #GstBuffer.
|
|
|
|
* @size: the size of the new #GstBuffer sub-buffer (with size > 0).
|
2000-03-27 19:53:43 +00:00
|
|
|
*
|
2002-07-08 19:22:02 +00:00
|
|
|
* Creates a sub-buffer from the parent at a given offset.
|
|
|
|
* This sub-buffer uses the actual memory space of the parent buffer.
|
2003-05-24 10:09:39 +00:00
|
|
|
* This function will copy the offset and timestamp field when the
|
|
|
|
* offset is 0, else they are set to _NONE.
|
|
|
|
* The duration field of the new buffer are set to GST_CLOCK_TIME_NONE.
|
2002-07-08 19:22:02 +00:00
|
|
|
*
|
2002-08-30 14:54:58 +00:00
|
|
|
* Returns: the new #GstBuffer, or NULL if there was an error.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2000-03-27 19:53:43 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstBuffer *
|
|
|
|
gst_buffer_create_sub (GstBuffer * parent, guint offset, guint size)
|
2000-11-11 15:13:50 +00:00
|
|
|
{
|
2002-07-08 19:22:02 +00:00
|
|
|
GstBuffer *buffer;
|
|
|
|
gpointer buffer_data;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
g_return_val_if_fail (parent != NULL, NULL);
|
|
|
|
g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (parent) > 0, NULL);
|
|
|
|
g_return_val_if_fail (size > 0, NULL);
|
|
|
|
g_return_val_if_fail (parent->size >= offset + size, NULL);
|
|
|
|
|
|
|
|
/* remember the data for the new buffer */
|
|
|
|
buffer_data = parent->data + offset;
|
|
|
|
/* make sure we're child not child from a child buffer */
|
|
|
|
while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
|
2004-01-11 22:11:35 +00:00
|
|
|
parent = GST_BUFFER (parent->buffer_private);
|
2002-07-08 19:22:02 +00:00
|
|
|
}
|
|
|
|
/* ref the real parent */
|
|
|
|
gst_data_ref (GST_DATA (parent));
|
|
|
|
|
|
|
|
/* create the new buffer */
|
2004-04-05 18:39:01 +00:00
|
|
|
buffer = gst_buffer_alloc_chunk ();
|
2003-02-10 20:32:32 +00:00
|
|
|
|
2004-02-20 13:18:32 +00:00
|
|
|
GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", buffer, parent);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2002-08-30 14:54:58 +00:00
|
|
|
/* make sure nobody overwrites data in the new buffer
|
|
|
|
* by setting the READONLY flag */
|
2004-03-13 15:27:01 +00:00
|
|
|
_GST_DATA_INIT (GST_DATA (buffer),
|
|
|
|
_gst_buffer_type,
|
|
|
|
GST_DATA_FLAG_SHIFT (GST_BUFFER_SUBBUFFER) |
|
|
|
|
GST_DATA_FLAG_SHIFT (GST_DATA_READONLY),
|
|
|
|
(GstDataFreeFunction) _gst_buffer_sub_free,
|
|
|
|
(GstDataCopyFunction) gst_buffer_default_copy);
|
2002-07-08 19:22:02 +00:00
|
|
|
|
|
|
|
/* set the right values in the child */
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_BUFFER_DATA (buffer) = buffer_data;
|
|
|
|
GST_BUFFER_SIZE (buffer) = size;
|
|
|
|
GST_BUFFER_MAXSIZE (buffer) = size;
|
2004-01-11 22:11:35 +00:00
|
|
|
GST_BUFFER_FREE_DATA_FUNC (buffer) = NULL;
|
|
|
|
GST_BUFFER_PRIVATE (buffer) = parent;
|
2003-05-24 10:09:39 +00:00
|
|
|
/* we can copy the timestamp and offset if the new buffer starts at
|
|
|
|
* offset 0 */
|
|
|
|
if (offset == 0) {
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buffer) = GST_BUFFER_TIMESTAMP (parent);
|
|
|
|
GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET (parent);
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
|
|
|
|
GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
|
2003-05-24 10:09:39 +00:00
|
|
|
}
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
|
|
|
|
GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
|
2004-01-11 22:37:03 +00:00
|
|
|
|
|
|
|
if (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_READONLY)) {
|
|
|
|
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_READONLY);
|
|
|
|
}
|
2005-03-07 18:27:42 +00:00
|
|
|
GST_BUFFER_CAPS (buffer) = NULL;
|
2003-10-28 20:25:30 +00:00
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
return buffer;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2001-10-21 18:00:31 +00:00
|
|
|
/**
|
|
|
|
* gst_buffer_is_span_fast:
|
2002-08-30 14:54:58 +00:00
|
|
|
* @buf1: a first source #GstBuffer.
|
|
|
|
* @buf2: the second source #GstBuffer.
|
2001-09-10 19:46:01 +00:00
|
|
|
*
|
2004-03-26 03:46:16 +00:00
|
|
|
* Determines whether a gst_buffer_span() can be done without copying
|
|
|
|
* the contents, that is, whether the data areas are contiguous.
|
2001-09-10 19:46:01 +00:00
|
|
|
*
|
2002-08-30 14:54:58 +00:00
|
|
|
* Returns: TRUE if the buffers are contiguous,
|
|
|
|
* FALSE if a copy would be required.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2001-09-10 19:46:01 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
|
2001-09-10 19:46:01 +00:00
|
|
|
{
|
2002-11-22 23:15:14 +00:00
|
|
|
g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
|
2002-07-08 19:22:02 +00:00
|
|
|
g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
|
|
|
|
g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
|
2001-10-17 10:21:27 +00:00
|
|
|
|
2002-07-08 19:22:02 +00:00
|
|
|
/* it's only fast if we have subbuffers of the same parent */
|
|
|
|
return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
|
2004-03-13 15:27:01 +00:00
|
|
|
(GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
|
|
|
|
(buf1->buffer_private == buf2->buffer_private) &&
|
|
|
|
((buf1->data + buf1->size) == buf2->data));
|
2001-09-10 19:46:01 +00:00
|
|
|
}
|
|
|
|
|
2001-09-04 04:34:32 +00:00
|
|
|
/**
|
|
|
|
* gst_buffer_span:
|
2002-08-30 14:54:58 +00:00
|
|
|
* @buf1: a first source #GstBuffer to merge.
|
|
|
|
* @offset: the offset in the first buffer from where the new
|
|
|
|
* buffer should start.
|
|
|
|
* @buf2: the second source #GstBuffer to merge.
|
|
|
|
* @len: the total length of the new buffer.
|
2001-09-04 04:34:32 +00:00
|
|
|
*
|
2002-04-17 15:19:56 +00:00
|
|
|
* Creates a new buffer that consists of part of buf1 and buf2.
|
2001-09-04 04:34:32 +00:00
|
|
|
* Logically, buf1 and buf2 are concatenated into a single larger
|
|
|
|
* buffer, and a new buffer is created at the given offset inside
|
|
|
|
* this space, with a given length.
|
|
|
|
*
|
|
|
|
* If the two source buffers are children of the same larger buffer,
|
|
|
|
* and are contiguous, the new buffer will be a child of the shared
|
2002-07-13 23:12:22 +00:00
|
|
|
* parent, and thus no copying is necessary. you can use
|
|
|
|
* gst_buffer_is_span_fast() to determine if a memcpy will be needed.
|
2001-09-04 04:34:32 +00:00
|
|
|
*
|
2002-08-30 14:54:58 +00:00
|
|
|
* Returns: the new #GstBuffer that spans the two source buffers.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2001-09-04 04:34:32 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstBuffer *
|
|
|
|
gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
|
|
|
|
guint32 len)
|
2001-08-11 08:25:05 +00:00
|
|
|
{
|
|
|
|
GstBuffer *newbuf;
|
|
|
|
|
gst/: Aplied part of patch #157127: Cleanup of issues reported by sparse.
Original commit message from CVS:
reviewed by: Wim Taymans, Ronald Bultje.
* gst/cothreads.c: (cothread_create):
* gst/gstbin.c: (gst_bin_add_func), (gst_bin_remove_func),
(gst_bin_child_state_change_func):
* gst/gstbuffer.c: (gst_buffer_span):
* gst/gstelement.c: (gst_element_get_index),
(gst_element_get_event_masks), (gst_element_get_query_types),
(gst_element_get_formats):
* gst/gsterror.c: (_gst_core_errors_init),
(_gst_library_errors_init), (_gst_resource_errors_init),
(_gst_stream_errors_init):
* gst/gstobject.c: (gst_object_default_deep_notify):
* gst/gstpad.c: (gst_pad_get_event_masks),
(gst_pad_get_internal_links_default):
* gst/gstplugin.c: (gst_plugin_register_func),
(gst_plugin_get_module):
* gst/gststructure.c: (gst_structure_get_string),
(gst_structure_get_abbrs), (gst_structure_from_abbr),
(gst_structure_to_abbr):
* gst/gstutils.c: (gst_print_element_args):
* gst/schedulers/gstoptimalscheduler.c: (add_to_group),
(setup_group_scheduler), (gst_opt_scheduler_iterate):
Aplied part of patch #157127: Cleanup of issues reported by
sparse.
Also do not try to use cothreads when there is no cothread
context yet.
2004-11-02 15:02:12 +00:00
|
|
|
g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
|
2002-07-08 19:22:02 +00:00
|
|
|
g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, NULL);
|
|
|
|
g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, NULL);
|
|
|
|
g_return_val_if_fail (len > 0, NULL);
|
2003-05-24 10:09:39 +00:00
|
|
|
g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
|
2001-08-11 08:25:05 +00:00
|
|
|
|
2001-12-14 22:59:21 +00:00
|
|
|
/* if the two buffers have the same parent and are adjacent */
|
2002-07-13 23:12:22 +00:00
|
|
|
if (gst_buffer_is_span_fast (buf1, buf2)) {
|
2004-01-11 22:11:35 +00:00
|
|
|
GstBuffer *parent = GST_BUFFER (buf1->buffer_private);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2001-12-14 22:59:21 +00:00
|
|
|
/* we simply create a subbuffer of the common parent */
|
2004-03-13 15:27:01 +00:00
|
|
|
newbuf = gst_buffer_create_sub (parent,
|
2004-03-15 19:27:17 +00:00
|
|
|
buf1->data - parent->data + offset, len);
|
2004-03-13 15:27:01 +00:00
|
|
|
} else {
|
|
|
|
GST_CAT_DEBUG (GST_CAT_BUFFER,
|
2004-03-15 19:27:17 +00:00
|
|
|
"slow path taken while spanning buffers %p and %p", buf1, buf2);
|
2001-12-14 22:59:21 +00:00
|
|
|
/* otherwise we simply have to brute-force copy the buffers */
|
2002-07-08 19:22:02 +00:00
|
|
|
newbuf = gst_buffer_new_and_alloc (len);
|
|
|
|
|
2001-12-14 22:59:21 +00:00
|
|
|
/* copy the first buffer's data across */
|
2002-07-08 19:22:02 +00:00
|
|
|
memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
|
2001-12-14 22:59:21 +00:00
|
|
|
/* copy the second buffer's data across */
|
2004-03-13 15:27:01 +00:00
|
|
|
memcpy (newbuf->data + (buf1->size - offset), buf2->data,
|
2004-03-15 19:27:17 +00:00
|
|
|
len - (buf1->size - offset));
|
2003-05-24 10:09:39 +00:00
|
|
|
/* if the offset is 0, the new buffer has the same timestamp as buf1 */
|
|
|
|
if (offset == 0) {
|
|
|
|
GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
|
|
|
|
GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if we completely merged the two buffers (appended), we can
|
2003-05-24 11:04:57 +00:00
|
|
|
* calculate the duration too. Also make sure we's not messing with
|
|
|
|
* invalid DURATIONS */
|
2003-11-24 02:09:23 +00:00
|
|
|
if (offset == 0 && buf1->size + buf2->size == len) {
|
|
|
|
if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
|
2004-03-15 19:27:17 +00:00
|
|
|
GST_BUFFER_DURATION_IS_VALID (buf2)) {
|
2003-11-24 02:09:23 +00:00
|
|
|
/* add duration */
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
|
2004-03-15 19:27:17 +00:00
|
|
|
GST_BUFFER_DURATION (buf2);
|
2003-11-24 02:09:23 +00:00
|
|
|
}
|
|
|
|
if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
|
|
|
|
/* add offset_end */
|
|
|
|
GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
|
|
|
|
}
|
2001-10-17 10:21:27 +00:00
|
|
|
}
|
2001-08-11 08:25:05 +00:00
|
|
|
|
|
|
|
return newbuf;
|
|
|
|
}
|