* Boston, MA 02111-1307, USA.
*/
-/* this file makes too much noise for most debugging sessions */
-#define GST_DEBUG_FORCE_DISABLE
+/**
+ * SECTION:gstbuffer
+ * @short_description: Data-passing buffer type, supporting sub-buffers.
+ * @see_also: #GstPad, #GstMiniObject
+ *
+ * Buffers are the basic unit of data transfer in GStreamer. The #GstBuffer
+ * type provides all the state necessary to define a region of memory as part
+ * of a stream. Sub-buffers are also supported, allowing a smaller region of a
+ * buffer to become its own buffer, with mechanisms in place to ensure that
+ * neither memory space goes away prematurely.
+ *
+ * Buffers are usually created with gst_buffer_new(). After a buffer has been
+ * created one will typically allocate memory for it and set the size of the
+ * buffer data. The following example creates a buffer that can hold a given
+ * video frame with a given width, height and bits per plane.
+ * <example>
+ * <title>Creating a buffer for a video frame</title>
+ * <programlisting>
+ * GstBuffer *buffer;
+ * gint size, width, height, bpp;
+ * ...
+ * size = width * height * bpp;
+ * buffer = gst_buffer_new ();
+ * GST_BUFFER_SIZE (buffer) = size;
+ * GST_BUFFER_MALLOCDATA (buffer) = g_malloc (size);
+ * GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
+ * ...
+ * </programlisting>
+ * </example>
+ *
+ * Alternatively, use gst_buffer_new_and_alloc()
+ * to create a buffer with preallocated data of a given size.
+ *
+ * The data pointed to by the buffer can be retrieved with the GST_BUFFER_DATA()
+ * macro. The size of the data can be found with GST_BUFFER_SIZE(). For buffers
+ * of size 0, the data pointer is undefined (usually NULL) and should never be used.
+ *
+ * If an element knows what pad you will push the buffer out on, it should use
+ * gst_pad_alloc_buffer() instead to create a buffer. This allows downstream
+ * elements to provide special buffers to write in, like hardware buffers.
+ *
+ * A buffer has a pointer to a #GstCaps describing the media type of the data
+ * in the buffer. Attach caps to the buffer with gst_buffer_set_caps(); this
+ * is typically done before pushing out a buffer using gst_pad_push() so that
+ * the downstream element knows the type of the buffer.
+ *
+ * A buffer will usually have a timestamp, and a duration, but neither of these
+ * are guaranteed (they may be set to #GST_CLOCK_TIME_NONE). Whenever a
+ * meaningful value can be given for these, they should be set. The timestamp
+ * and duration are measured in nanoseconds (they are #GstClockTime values).
+ *
+ * A buffer can also have one or both of a start and an end offset. These are
+ * media-type specific. For video buffers, the start offset will generally be
+ * the frame number. For audio buffers, it will be the number of samples
+ * produced so far. For compressed data, it could be the byte offset in a
+ * source or destination file. Likewise, the end offset will be the offset of
+ * the end of the buffer. These can only be meaningfully interpreted if you
+ * know the media type of the buffer (the #GstCaps set on it). Either or both
+ * can be set to #GST_BUFFER_OFFSET_NONE.
+ *
+ * gst_buffer_ref() is used to increase the refcount of a buffer. This must be
+ * done when you want to keep a handle to the buffer after pushing it to the
+ * next element.
+ *
+ * To efficiently create a smaller buffer out of an existing one, you can
+ * use gst_buffer_create_sub().
+ *
+ * If a plug-in wants to modify the buffer data in-place, it should first obtain
+ * a buffer that is safe to modify by using gst_buffer_make_writable(). This
+ * function is optimized so that a copy will only be made when it is necessary.
+ *
+ * A plugin that only wishes to modify the metadata of a buffer, such as the
+ * offset, timestamp or caps, should use gst_buffer_make_metadata_writable(),
+ * which will create a subbuffer of the original buffer to ensure the caller
+ * has sole ownership, and not copy the buffer data.
+ *
+ * Several flags of the buffer can be set and unset with the
+ * GST_BUFFER_FLAG_SET() and GST_BUFFER_FLAG_UNSET() macros. Use
+ * GST_BUFFER_FLAG_IS_SET() to test if a certain #GstBufferFlag is set.
+ *
+ * Buffers can be efficiently merged into a larger buffer with
+ * gst_buffer_merge() and gst_buffer_span() if the gst_buffer_is_span_fast()
+ * function returns TRUE.
+ *
+ * An element should either unref the buffer or push it out on a src pad
+ * using gst_pad_push() (see #GstPad).
+ *
+ * Buffers are usually freed by unreffing them with gst_buffer_unref(). When
+ * the refcount drops to 0, any data pointed to by GST_BUFFER_MALLOCDATA() will
+ * also be freed.
+ *
+ * Last reviewed on August 11th, 2006 (0.10.10)
+ */
#include "gst_private.h"
-#include "gstatomic_impl.h"
-#include "gstdata_private.h"
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
#include "gstbuffer.h"
-#include "gstmemchunk.h"
-#include "gstlog.h"
-#include "gstbufferpool-default.h"
+#include "gstinfo.h"
+#include "gstutils.h"
+#include "gstminiobject.h"
+#include "gstversion.h"
-GType _gst_buffer_type;
-GType _gst_buffer_pool_type;
+static void gst_buffer_finalize (GstBuffer * buffer);
+static GstBuffer *_gst_buffer_copy (GstBuffer * buffer);
-#ifndef GST_DISABLE_TRACE
-/* #define GST_WITH_ALLOC_TRACE */
-#include "gsttrace.h"
+static GType _gst_buffer_type = 0;
-static GstAllocTrace *_gst_buffer_trace;
-static GstAllocTrace *_gst_buffer_pool_trace;
+/* buffer alignment in bytes
+ * an alignment of 8 would be the same as malloc() guarantees
+ */
+#ifdef HAVE_POSIX_MEMALIGN
+#if defined(BUFFER_ALIGNMENT_MALLOC)
+static size_t _gst_buffer_data_alignment = 8;
+#elif defined(BUFFER_ALIGNMENT_PAGESIZE)
+static size_t _gst_buffer_data_alignment = 0;
+#elif defined(BUFFER_ALIGNMENT)
+static size_t _gst_buffer_data_alignment = BUFFER_ALIGNMENT;
+#else
+#error "No buffer alignment configured"
#endif
-static GstMemChunk *chunk;
+static inline gboolean
+aligned_malloc (gpointer * memptr, guint size)
+{
+ gint res;
+
+ res = posix_memalign (memptr, _gst_buffer_data_alignment, size);
+ return (res == 0);
+}
+
+#endif /* HAVE_POSIX_MEMALIGN */
void
_gst_buffer_initialize (void)
{
- _gst_buffer_type = g_boxed_type_register_static ("GstBuffer",
- (GBoxedCopyFunc) gst_data_ref,
- (GBoxedFreeFunc) gst_data_unref);
-
- _gst_buffer_pool_type = g_boxed_type_register_static ("GstBufferPool",
- (GBoxedCopyFunc) gst_data_ref,
- (GBoxedFreeFunc) gst_data_unref);
-
-#ifndef GST_DISABLE_TRACE
- _gst_buffer_trace = gst_alloc_trace_register (GST_BUFFER_TRACE_NAME);
- _gst_buffer_pool_trace = gst_alloc_trace_register (GST_BUFFER_POOL_TRACE_NAME);
+ /* the GstMiniObject types need to be class_ref'd once before it can be
+ * done from multiple threads;
+ * see http://bugzilla.gnome.org/show_bug.cgi?id=304551 */
+ g_type_class_ref (gst_buffer_get_type ());
+#ifdef HAVE_GETPAGESIZE
+#ifdef BUFFER_ALIGNMENT_PAGESIZE
+ _gst_buffer_data_alignment = getpagesize ();
#endif
+#endif
+}
- chunk = gst_mem_chunk_new ("GstBufferChunk", sizeof (GstBuffer),
- sizeof (GstBuffer) * 200, 0);
-
- GST_INFO (GST_CAT_BUFFER, "Buffers are initialized now");
+#define _do_init \
+{ \
+ _gst_buffer_type = g_define_type_id; \
}
-GType
-gst_buffer_get_type (void)
+G_DEFINE_TYPE_WITH_CODE (GstBuffer, gst_buffer, GST_TYPE_MINI_OBJECT, _do_init);
+
+static void
+gst_buffer_class_init (GstBufferClass * klass)
{
- return _gst_buffer_type;
+ klass->mini_object_class.copy = (GstMiniObjectCopyFunction) _gst_buffer_copy;
+ klass->mini_object_class.finalize =
+ (GstMiniObjectFinalizeFunction) gst_buffer_finalize;
}
static void
-_gst_buffer_free_to_pool (GstBuffer *buffer)
+gst_buffer_finalize (GstBuffer * buffer)
{
- GstBufferPool *pool = buffer->pool;
+ g_return_if_fail (buffer != NULL);
- pool->buffer_free (pool, buffer, pool->user_data);
+ GST_CAT_LOG (GST_CAT_BUFFER, "finalize %p", buffer);
- gst_data_unref (GST_DATA (pool));
-}
+ /* free our data */
+ if (G_LIKELY (buffer->malloc_data))
+ buffer->free_func (buffer->malloc_data);
-static void
-_gst_buffer_sub_free (GstBuffer *buffer)
-{
- gst_data_unref (GST_DATA (buffer->pool_private));
+ gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
- GST_BUFFER_DATA (buffer) = NULL;
- GST_BUFFER_SIZE (buffer) = 0;
+ if (buffer->parent)
+ gst_buffer_unref (buffer->parent);
- _GST_DATA_DISPOSE (GST_DATA (buffer));
-
- gst_mem_chunk_free (chunk, GST_DATA (buffer));
-#ifndef GST_DISABLE_TRACE
- gst_alloc_trace_free (_gst_buffer_trace, buffer);
-#endif
+/* ((GstMiniObjectClass *) */
+/* gst_buffer_parent_class)->finalize (GST_MINI_OBJECT_CAST (buffer)); */
}
/**
- * gst_buffer_default_free:
- * @buffer: a #GstBuffer to free.
+ * gst_buffer_copy_metadata:
+ * @dest: a destination #GstBuffer
+ * @src: a source #GstBuffer
+ * @flags: flags indicating what metadata fields should be copied.
+ *
+ * Copies the metadata from @src into @dest. The data, size and mallocdata
+ * fields are not copied.
+ *
+ * @flags indicate which fields will be copied. Use #GST_BUFFER_COPY_ALL to copy
+ * all the metadata fields.
*
- * Frees the memory associated with the buffer including the buffer data,
- * unless the GST_BUFFER_DONTFREE flags was set or the buffer data is NULL.
- * This function is used by buffer pools.
+ * This function is typically called from a custom buffer copy function after
+ * creating @dest and setting the data, size, mallocdata.
+ *
+ * Since: 0.10.13
*/
void
-gst_buffer_default_free (GstBuffer *buffer)
+gst_buffer_copy_metadata (GstBuffer * dest, const GstBuffer * src,
+ GstBufferCopyFlags flags)
{
- g_return_if_fail (buffer != NULL);
+ g_return_if_fail (dest != NULL);
+ g_return_if_fail (src != NULL);
- /* free our data */
- if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_DONTFREE) && GST_BUFFER_DATA (buffer))
- g_free (GST_BUFFER_DATA (buffer));
+ /* nothing to copy if the buffers are the same */
+ if (G_UNLIKELY (dest == src))
+ return;
- /* set to safe values */
- GST_BUFFER_DATA (buffer) = NULL;
- GST_BUFFER_SIZE (buffer) = 0;
+#if GST_VERSION_NANO == 1
+ /* we enable this extra debugging in git versions only for now */
+ g_warn_if_fail (gst_buffer_is_metadata_writable (dest));
+#endif
- _GST_DATA_DISPOSE (GST_DATA (buffer));
+ GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", src, dest);
- gst_mem_chunk_free (chunk, GST_DATA (buffer));
-#ifndef GST_DISABLE_TRACE
- gst_alloc_trace_free (_gst_buffer_trace, buffer);
-#endif
-}
+ if (flags & GST_BUFFER_COPY_FLAGS) {
+ guint mask;
-static GstBuffer*
-_gst_buffer_copy_from_pool (GstBuffer *buffer)
-{
- return buffer->pool->buffer_copy (buffer->pool, buffer, buffer->pool->user_data);
+ /* copy relevant flags */
+ mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
+ GST_BUFFER_FLAG_DELTA_UNIT | GST_BUFFER_FLAG_DISCONT |
+ GST_BUFFER_FLAG_GAP | GST_BUFFER_FLAG_MEDIA1 |
+ GST_BUFFER_FLAG_MEDIA2 | GST_BUFFER_FLAG_MEDIA3;
+ GST_MINI_OBJECT_FLAGS (dest) |= GST_MINI_OBJECT_FLAGS (src) & mask;
+ }
+
+ if (flags & GST_BUFFER_COPY_TIMESTAMPS) {
+ GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
+ GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
+ GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
+ GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
+ }
+
+ if (flags & GST_BUFFER_COPY_CAPS) {
+ gst_caps_replace (&GST_BUFFER_CAPS (dest), GST_BUFFER_CAPS (src));
+ }
}
-/**
- * gst_buffer_default_copy:
- * @buffer: a #GstBuffer to make a copy of.
- *
- * Make a full newly allocated copy of the given buffer, data and all.
- * This function is used by buffer pools.
- *
- * Returns: the new #GstBuffer.
- */
-GstBuffer*
-gst_buffer_default_copy (GstBuffer *buffer)
+static GstBuffer *
+_gst_buffer_copy (GstBuffer * buffer)
{
GstBuffer *copy;
copy = gst_buffer_new ();
/* we simply copy everything from our parent */
- 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_MAXSIZE (buffer);
- GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
- GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
+#ifdef HAVE_POSIX_MEMALIGN
+ {
+ gpointer memptr = NULL;
+
+ if (G_LIKELY (buffer->size)) {
+ if (G_UNLIKELY (!aligned_malloc (&memptr, buffer->size))) {
+ /* terminate on error like g_memdup() would */
+ g_error ("%s: failed to allocate %u bytes", G_STRLOC, buffer->size);
+ } else {
+ memcpy (memptr, buffer->data, buffer->size);
+ }
+ }
+ copy->data = (guint8 *) memptr;
+ GST_BUFFER_FREE_FUNC (copy) = free;
+ }
+#else
+ copy->data = g_memdup (buffer->data, buffer->size);
+#endif
+
+ /* make sure it gets freed (even if the parent is subclassed, we return a
+ normal buffer) */
+ copy->malloc_data = copy->data;
+ copy->size = buffer->size;
+
+ gst_buffer_copy_metadata (copy, buffer, GST_BUFFER_COPY_ALL);
return copy;
}
+static void
+gst_buffer_init (GstBuffer * buffer)
+{
+ GST_CAT_LOG (GST_CAT_BUFFER, "init %p", buffer);
+
+ GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
+ GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
+ GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
+ GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
+ GST_BUFFER_FREE_FUNC (buffer) = g_free;
+}
+
/**
* gst_buffer_new:
*
* Creates a newly allocated buffer without any data.
*
- * Returns: the new #GstBuffer.
+ * MT safe.
+ *
+ * Returns: (transfer full): the new #GstBuffer.
*/
-GstBuffer*
+GstBuffer *
gst_buffer_new (void)
{
- GstBuffer *buf;
-
- buf = gst_mem_chunk_alloc0 (chunk);
-#ifndef GST_DISABLE_TRACE
- gst_alloc_trace_new (_gst_buffer_trace, buf);
-#endif
-
- GST_DEBUG (GST_CAT_BUFFER, "new %p", buf);
+ GstBuffer *newbuf;
- _GST_DATA_INIT (GST_DATA (buf),
- _gst_buffer_type,
- 0,
- (GstDataFreeFunction) gst_buffer_default_free,
- (GstDataCopyFunction) gst_buffer_default_copy);
+ newbuf = (GstBuffer *) gst_mini_object_new (_gst_buffer_type);
- GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
+ GST_CAT_LOG (GST_CAT_BUFFER, "new %p", newbuf);
- return buf;
+ return newbuf;
}
/**
* @size: the size of the new buffer's data.
*
* Creates a newly allocated buffer with data of the given size.
+ * The buffer memory is not cleared. If the requested amount of
+ * memory can't be allocated, the program will abort. Use
+ * gst_buffer_try_new_and_alloc() if you want to handle this case
+ * gracefully or have gotten the size to allocate from an untrusted
+ * source such as a media stream.
+ *
+ *
+ * Note that when @size == 0, the buffer data pointer will be NULL.
*
- * Returns: the new #GstBuffer.
+ * MT safe.
+ *
+ * Returns: (transfer full): the new #GstBuffer.
*/
-GstBuffer*
+GstBuffer *
gst_buffer_new_and_alloc (guint size)
{
- GstBuffer *new;
+ GstBuffer *newbuf;
- new = gst_buffer_new ();
+ newbuf = gst_buffer_new ();
- GST_BUFFER_DATA (new) = g_malloc (size);
- GST_BUFFER_SIZE (new) = size;
+#ifdef HAVE_POSIX_MEMALIGN
+ {
+ gpointer memptr = NULL;
- return new;
+ if (G_LIKELY (size)) {
+ if (G_UNLIKELY (!aligned_malloc (&memptr, size))) {
+ /* terminate on error like g_memdup() would */
+ g_error ("%s: failed to allocate %u bytes", G_STRLOC, size);
+ }
+ }
+ newbuf->malloc_data = (guint8 *) memptr;
+ GST_BUFFER_FREE_FUNC (newbuf) = free;
+ }
+#else
+ newbuf->malloc_data = g_malloc (size);
+#endif
+ GST_BUFFER_DATA (newbuf) = newbuf->malloc_data;
+ GST_BUFFER_SIZE (newbuf) = size;
+
+ GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
+
+ return newbuf;
}
/**
- * gst_buffer_new_from_pool:
- * @pool: a #GstBufferPool to use.
- * @offset: the offset of the new buffer.
- * @size: the size of the new buffer.
+ * gst_buffer_try_new_and_alloc:
+ * @size: the size of the new buffer's data.
+ *
+ * Tries to create a newly allocated buffer with data of the given size. If
+ * the requested amount of memory can't be allocated, NULL will be returned.
+ * The buffer memory is not cleared.
+ *
+ * Note that when @size == 0, the buffer data pointer will be NULL.
*
- * Creates a newly allocated buffer using the specified buffer pool,
- * offset and size.
+ * MT safe.
*
- * Returns: the new #GstBuffer, or NULL if there was an error.
+ * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
+ * be allocated.
+ *
+ * Since: 0.10.13
*/
-GstBuffer*
-gst_buffer_new_from_pool (GstBufferPool *pool,
- guint64 offset, guint size)
+GstBuffer *
+gst_buffer_try_new_and_alloc (guint size)
{
- GstBuffer *buffer;
-
- g_return_val_if_fail (pool != NULL, NULL);
+ GstBuffer *newbuf;
+ guint8 *malloc_data;
+#ifdef HAVE_POSIX_MEMALIGN
+ gpointer memptr = NULL;
+
+ if (G_LIKELY (size)) {
+ if (G_UNLIKELY (!aligned_malloc (&memptr, size))) {
+ GST_CAT_WARNING (GST_CAT_BUFFER, "failed to allocate %d bytes", size);
+ return NULL;
+ }
+ }
+ malloc_data = (guint8 *) memptr;
+#else
+ malloc_data = g_try_malloc (size);
- buffer = pool->buffer_new (pool, offset, size, pool->user_data);
- if (!buffer)
+ if (G_UNLIKELY (malloc_data == NULL && size != 0)) {
+ GST_CAT_WARNING (GST_CAT_BUFFER, "failed to allocate %d bytes", size);
return NULL;
+ }
+#endif
- GST_BUFFER_BUFFERPOOL (buffer) = pool;
- gst_data_ref (GST_DATA (pool));
+ /* FIXME: there's no g_type_try_create_instance() in GObject yet, so this
+ * will still abort if a new GstBuffer structure can't be allocated */
+ newbuf = gst_buffer_new ();
- /* override the buffer refcount functions with those from the pool (if any) */
- if (pool->buffer_free)
- GST_DATA (buffer)->free = (GstDataFreeFunction)_gst_buffer_free_to_pool;
- if (pool->buffer_copy)
- GST_DATA (buffer)->copy = (GstDataCopyFunction)_gst_buffer_copy_from_pool;
+ GST_BUFFER_MALLOCDATA (newbuf) = malloc_data;
+ GST_BUFFER_DATA (newbuf) = malloc_data;
+ GST_BUFFER_SIZE (newbuf) = size;
+#ifdef HAVE_POSIX_MEMALIGN
+ GST_BUFFER_FREE_FUNC (newbuf) = free;
+#endif
- return buffer;
+ GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
+
+ return newbuf;
}
/**
- * gst_buffer_create_sub:
- * @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).
+ * gst_buffer_get_caps:
+ * @buffer: a #GstBuffer.
*
- * Creates a sub-buffer from the parent at a given offset.
- * This sub-buffer uses the actual memory space of the parent buffer.
+ * Gets the media type of the buffer. This can be NULL if there
+ * is no media type attached to this buffer.
*
- * Returns: the new #GstBuffer, or NULL if there was an error.
+ * Returns: (transfer full): a reference to the #GstCaps. unref after usage.
+ * Returns NULL if there were no caps on this buffer.
+ */
+/* this is not made atomic because if the buffer were reffed from multiple
+ * threads, it would have a refcount > 2 and thus be immutable.
*/
-GstBuffer*
-gst_buffer_create_sub (GstBuffer *parent, guint offset, guint size)
+GstCaps *
+gst_buffer_get_caps (GstBuffer * buffer)
{
- GstBuffer *buffer;
- gpointer buffer_data;
- guint64 parent_offset;
-
- 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;
- parent_offset = GST_BUFFER_OFFSET (parent);
- /* make sure we're child not child from a child buffer */
- while (GST_BUFFER_FLAG_IS_SET (parent, GST_BUFFER_SUBBUFFER)) {
- parent = GST_BUFFER (parent->pool_private);
- }
- /* ref the real parent */
- gst_data_ref (GST_DATA (parent));
- /* make sure nobody overwrites data in the parent */
- if (!GST_DATA_IS_READONLY (parent))
- GST_DATA_FLAG_SET(parent, GST_DATA_READONLY);
+ GstCaps *ret;
- /* create the new buffer */
- buffer = gst_mem_chunk_alloc0 (chunk);
-#ifndef GST_DISABLE_TRACE
- gst_alloc_trace_new (_gst_buffer_trace, buffer);
+ g_return_val_if_fail (buffer != NULL, NULL);
+
+ ret = GST_BUFFER_CAPS (buffer);
+
+ if (ret)
+ gst_caps_ref (ret);
+
+ return ret;
+}
+
+/**
+ * gst_buffer_set_caps:
+ * @buffer: a #GstBuffer.
+ * @caps: (transfer none): a #GstCaps.
+ *
+ * Sets the media type on the buffer. The refcount of the caps will
+ * be increased and any previous caps on the buffer will be
+ * unreffed.
+ */
+/* this is not made atomic because if the buffer were reffed from multiple
+ * threads, it would have a refcount > 2 and thus be immutable.
+ */
+void
+gst_buffer_set_caps (GstBuffer * buffer, GstCaps * caps)
+{
+ g_return_if_fail (buffer != NULL);
+ g_return_if_fail (caps == NULL || GST_CAPS_IS_SIMPLE (caps));
+
+#if GST_VERSION_NANO == 1
+ /* we enable this extra debugging in git versions only for now */
+ g_warn_if_fail (gst_buffer_is_metadata_writable (buffer));
+ /* FIXME: would be nice to also check if caps are fixed here, but expensive */
#endif
- GST_DEBUG (GST_CAT_BUFFER, "new %p", buf);
+ gst_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
+}
+
+/**
+ * gst_buffer_is_metadata_writable:
+ * @buf: a #GstBuffer
+ *
+ * Similar to gst_buffer_is_writable, but this only ensures that the
+ * refcount of the buffer is 1, indicating that the caller is the sole
+ * owner and can change the buffer metadata, such as caps and timestamps.
+ *
+ * Returns: TRUE if the metadata is writable.
+ */
+gboolean
+gst_buffer_is_metadata_writable (GstBuffer * buf)
+{
+ return (GST_MINI_OBJECT_REFCOUNT_VALUE (GST_MINI_OBJECT_CAST (buf)) == 1);
+}
- /* make sure nobody overwrites data in the new buffer
- * by setting the READONLY flag */
- _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);
+/**
+ * gst_buffer_make_metadata_writable:
+ * @buf: (transfer full): a #GstBuffer
+ *
+ * Similar to gst_buffer_make_writable, but does not ensure that the buffer
+ * data array is writable. Instead, this just ensures that the returned buffer
+ * is solely owned by the caller, by creating a subbuffer of the original
+ * buffer if necessary.
+ *
+ * After calling this function, @buf should not be referenced anymore. The
+ * result of this function has guaranteed writable metadata.
+ *
+ * Returns: (transfer full): a new #GstBuffer with writable metadata, which
+ * may or may not be the same as @buf.
+ */
+GstBuffer *
+gst_buffer_make_metadata_writable (GstBuffer * buf)
+{
+ GstBuffer *ret;
- GST_BUFFER_OFFSET (buffer) = parent_offset + offset;
- GST_BUFFER_TIMESTAMP (buffer) = -1;
- GST_BUFFER_BUFFERPOOL (buffer) = NULL;
- GST_BUFFER_POOL_PRIVATE (buffer) = parent;
+ if (gst_buffer_is_metadata_writable (buf)) {
+ ret = buf;
+ } else {
+ ret = gst_buffer_create_sub (buf, 0, GST_BUFFER_SIZE (buf));
- /* set the right values in the child */
- buffer->data = buffer_data;
- buffer->size = size;
+ gst_buffer_unref (buf);
+ }
- return buffer;
+ return ret;
}
+#define GST_IS_SUBBUFFER(obj) (GST_BUFFER_CAST(obj)->parent != NULL)
/**
- * gst_buffer_merge:
- * @buf1: a first source #GstBuffer to merge.
- * @buf2: the second source #GstBuffer to merge.
+ * gst_buffer_create_sub:
+ * @parent: a #GstBuffer.
+ * @offset: the offset into parent #GstBuffer at which the new sub-buffer
+ * begins.
+ * @size: the size of the new #GstBuffer sub-buffer, in bytes.
*
- * Create a new buffer that is the concatenation of the two source
- * buffers. The original source buffers will not be modified or
- * unref'd.
+ * Creates a sub-buffer from @parent at @offset and @size.
+ * This sub-buffer uses the actual memory space of the parent buffer.
+ * This function will copy the offset and timestamp fields when the
+ * offset is 0. If not, they will be set to #GST_CLOCK_TIME_NONE and
+ * #GST_BUFFER_OFFSET_NONE.
+ * If @offset equals 0 and @size equals the total size of @buffer, the
+ * duration and offset end fields are also copied. If not they will be set
+ * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
*
- * Internally is nothing more than a specialized gst_buffer_span(),
- * so the same optimizations can occur.
+ * MT safe.
*
- * Returns: the new #GstBuffer that's the concatenation of the source buffers.
+ * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
+ * invalid.
*/
-GstBuffer*
-gst_buffer_merge (GstBuffer *buf1, GstBuffer *buf2)
+GstBuffer *
+gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size)
{
- GstBuffer *result;
+ GstBuffer *subbuffer;
+ GstBuffer *parent;
+ gboolean complete;
- /* we're just a specific case of the more general gst_buffer_span() */
- result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
+ g_return_val_if_fail (buffer != NULL, NULL);
+ g_return_val_if_fail (buffer->mini_object.refcount, NULL);
+ g_return_val_if_fail (buffer->size >= offset + size, NULL);
+
+ /* find real parent */
+ if (GST_IS_SUBBUFFER (buffer)) {
+ parent = buffer->parent;
+ } else {
+ parent = buffer;
+ }
+ gst_buffer_ref (parent);
+
+ /* create the new buffer */
+ subbuffer = gst_buffer_new ();
+ subbuffer->parent = parent;
+ GST_BUFFER_FLAG_SET (subbuffer, GST_BUFFER_FLAG_READONLY);
+
+ GST_CAT_LOG (GST_CAT_BUFFER, "new subbuffer %p (parent %p)", subbuffer,
+ parent);
+
+ /* set the right values in the child */
+ GST_BUFFER_DATA (subbuffer) = buffer->data + offset;
+ GST_BUFFER_SIZE (subbuffer) = size;
+
+ if ((offset == 0) && (size == GST_BUFFER_SIZE (buffer))) {
+ /* copy all the flags except IN_CAPS */
+ GST_BUFFER_FLAG_SET (subbuffer, GST_BUFFER_FLAGS (buffer));
+ GST_BUFFER_FLAG_UNSET (subbuffer, GST_BUFFER_FLAG_IN_CAPS);
+ } else {
+ /* copy only PREROLL & GAP flags */
+ GST_BUFFER_FLAG_SET (subbuffer, (GST_BUFFER_FLAGS (buffer) &
+ (GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_GAP)));
+ }
+
+ /* we can copy the timestamp and offset if the new buffer starts at
+ * offset 0 */
+ if (offset == 0) {
+ GST_BUFFER_TIMESTAMP (subbuffer) = GST_BUFFER_TIMESTAMP (buffer);
+ GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET (buffer);
+ complete = (buffer->size == size);
+ } else {
+ GST_BUFFER_TIMESTAMP (subbuffer) = GST_CLOCK_TIME_NONE;
+ GST_BUFFER_OFFSET (subbuffer) = GST_BUFFER_OFFSET_NONE;
+ complete = FALSE;
+ }
- return result;
+ if (complete) {
+ GstCaps *caps;
+
+ /* if we copied the complete buffer we can copy the duration,
+ * offset_end and caps as well */
+ GST_BUFFER_DURATION (subbuffer) = GST_BUFFER_DURATION (buffer);
+ GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_END (buffer);
+ if ((caps = GST_BUFFER_CAPS (buffer)))
+ gst_caps_ref (caps);
+ GST_BUFFER_CAPS (subbuffer) = caps;
+ } else {
+ GST_BUFFER_DURATION (subbuffer) = GST_CLOCK_TIME_NONE;
+ GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_NONE;
+ GST_BUFFER_CAPS (subbuffer) = NULL;
+ }
+ return subbuffer;
}
/**
* gst_buffer_is_span_fast:
- * @buf1: a first source #GstBuffer.
- * @buf2: the second source #GstBuffer.
+ * @buf1: the first #GstBuffer.
+ * @buf2: the second #GstBuffer.
*
- * Determines whether a gst_buffer_span() is free (as in free beer),
- * or requires a memcpy.
+ * Determines whether a gst_buffer_span() can be done without copying
+ * the contents, that is, whether the data areas are contiguous sub-buffers of
+ * the same buffer.
*
- * Returns: TRUE if the buffers are contiguous,
+ * MT safe.
+ * Returns: TRUE if the buffers are contiguous,
* FALSE if a copy would be required.
*/
gboolean
-gst_buffer_is_span_fast (GstBuffer *buf1, GstBuffer *buf2)
+gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
{
g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
- g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf1) > 0, FALSE);
- g_return_val_if_fail (GST_BUFFER_REFCOUNT_VALUE (buf2) > 0, FALSE);
+ g_return_val_if_fail (buf1->mini_object.refcount, FALSE);
+ g_return_val_if_fail (buf2->mini_object.refcount, FALSE);
/* it's only fast if we have subbuffers of the same parent */
- return ((GST_BUFFER_FLAG_IS_SET (buf1, GST_BUFFER_SUBBUFFER)) &&
- (GST_BUFFER_FLAG_IS_SET (buf2, GST_BUFFER_SUBBUFFER)) &&
- (buf1->pool_private == buf2->pool_private) &&
- ((buf1->data + buf1->size) == buf2->data));
+ return (GST_IS_SUBBUFFER (buf1) &&
+ GST_IS_SUBBUFFER (buf2) && (buf1->parent == buf2->parent)
+ && ((buf1->data + buf1->size) == buf2->data));
}
/**
* gst_buffer_span:
- * @buf1: a first source #GstBuffer to merge.
+ * @buf1: the 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.
*
* 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
- * parent, and thus no copying is necessary. you can use
+ * parent, and thus no copying is necessary. you can use
* gst_buffer_is_span_fast() to determine if a memcpy will be needed.
*
- * Returns: the new #GstBuffer that spans the two source buffers.
+ * MT safe.
+ *
+ * Returns: (transfer full): the new #GstBuffer that spans the two source
+ * buffers, or NULL if the arguments are invalid.
*/
-GstBuffer*
-gst_buffer_span (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len)
+GstBuffer *
+gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
+ guint32 len)
{
GstBuffer *newbuf;
- g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
- 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);
+ g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
+ g_return_val_if_fail (buf1->mini_object.refcount, NULL);
+ g_return_val_if_fail (buf2->mini_object.refcount, NULL);
+ g_return_val_if_fail (len, NULL);
+ g_return_val_if_fail (len <= buf1->size + buf2->size - offset, NULL);
/* if the two buffers have the same parent and are adjacent */
if (gst_buffer_is_span_fast (buf1, buf2)) {
- GstBuffer *parent = GST_BUFFER (buf1->pool_private);
+ GstBuffer *parent = buf1->parent;
+
/* we simply create a subbuffer of the common parent */
- newbuf = gst_buffer_create_sub (parent,
- buf1->data - parent->data + offset, len);
- }
- else {
- GST_DEBUG (GST_CAT_BUFFER, "slow path taken while spanning buffers %p and %p",
- buf1, buf2);
+ newbuf = gst_buffer_create_sub (parent,
+ buf1->data - parent->data + offset, len);
+ } else {
+ GST_CAT_DEBUG (GST_CAT_BUFFER,
+ "slow path taken while spanning buffers %p and %p", buf1, buf2);
/* otherwise we simply have to brute-force copy the buffers */
newbuf = gst_buffer_new_and_alloc (len);
- /* copy relevant stuff from data struct of buffer1 */
- GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1) + offset;
-
/* copy the first buffer's data across */
memcpy (newbuf->data, buf1->data + offset, buf1->size - offset);
/* copy the second buffer's data across */
- memcpy (newbuf->data + (buf1->size - offset), buf2->data,
- len - (buf1->size - offset));
+ memcpy (newbuf->data + (buf1->size - offset), buf2->data,
+ len - (buf1->size - offset));
}
/* if the offset is 0, the new buffer has the same timestamp as buf1 */
- if (offset == 0)
+ if (offset == 0) {
+ GST_BUFFER_OFFSET (newbuf) = GST_BUFFER_OFFSET (buf1);
GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf1);
- return newbuf;
-}
-
-GType
-gst_buffer_pool_get_type (void)
-{
- return _gst_buffer_pool_type;
-}
-
-/**
- * gst_buffer_pool_default_free:
- * @pool: a #GstBufferPool to free.
- *
- * Frees the memory associated with the bufferpool.
- */
-void
-gst_buffer_pool_default_free (GstBufferPool *pool)
-{
- g_return_if_fail (pool != NULL);
-
- _GST_DATA_DISPOSE (GST_DATA (pool));
- g_free (pool);
-#ifndef GST_DISABLE_TRACE
- gst_alloc_trace_free (_gst_buffer_pool_trace, pool);
-#endif
-}
-
-/**
- * gst_buffer_pool_new:
- * @free: the #GstDataFreeFunction to free the buffer pool.
- * @copy: the #GstDataCopyFunction to copy the buffer pool.
- * @buffer_new: the #GstBufferPoolBufferNewFunction to create a new buffer
- * from this pool
- * @buffer_copy: the #GstBufferPoolBufferCopyFunction to copy a buffer
- * from this pool
- * @buffer_free: the #GstBufferPoolBufferFreeFunction to free a buffer
- * in this pool
- * @user_data: the user data gpointer passed to buffer_* functions.
- *
- * Creates a new buffer pool with the given functions.
- *
- * Returns: a new #GstBufferPool, or NULL on error.
- */
-GstBufferPool*
-gst_buffer_pool_new (GstDataFreeFunction free,
- GstDataCopyFunction copy,
- GstBufferPoolBufferNewFunction buffer_new,
- GstBufferPoolBufferCopyFunction buffer_copy,
- GstBufferPoolBufferFreeFunction buffer_free,
- gpointer user_data)
-{
- GstBufferPool *pool;
-
- /* we need at least a buffer_new function */
- g_return_val_if_fail (buffer_new != NULL, NULL);
-
- pool = g_new0 (GstBufferPool, 1);
-#ifndef GST_DISABLE_TRACE
- gst_alloc_trace_new (_gst_buffer_pool_trace, pool);
-#endif
-
- GST_DEBUG (GST_CAT_BUFFER, "allocating new buffer pool %p\n", pool);
-
- /* init data struct */
- _GST_DATA_INIT (GST_DATA (pool),
- _gst_buffer_pool_type,
- 0,
- (free ? free : (GstDataFreeFunction) gst_buffer_pool_default_free),
- copy);
-
- /* set functions */
- pool->buffer_new = buffer_new;
- pool->buffer_copy = buffer_copy;
- pool->buffer_free = buffer_free;
- pool->user_data = user_data;
-
- return pool;
-}
-
-/**
- * gst_buffer_pool_is_active:
- * @pool: the #GstBufferPool to query.
- *
- * Queries if the given buffer pool is active.
- *
- * Returns: TRUE if the pool is active.
- */
-gboolean
-gst_buffer_pool_is_active (GstBufferPool *pool)
-{
- g_return_val_if_fail (pool != NULL, FALSE);
-
- return pool->active;
-}
-
-/**
- * gst_buffer_pool_set_active:
- * @pool: a #GstBufferPool to set the activity status on.
- * @active: the new status of the pool.
- *
- * Sets the given pool to the active or inactive state depending on the
- * active parameter.
- */
-void
-gst_buffer_pool_set_active (GstBufferPool *pool, gboolean active)
-{
- g_return_if_fail (pool != NULL);
-
- pool->active = active;
-}
-
-/**
- * gst_buffer_pool_set_user_data:
- * @pool: the #GstBufferPool to set the user data for.
- * @user_data: the user_data to set on the buffer pool.
- *
- * Sets the given user data on the buffer pool.
- */
-void
-gst_buffer_pool_set_user_data (GstBufferPool *pool, gpointer user_data)
-{
- g_return_if_fail (pool != NULL);
-
- pool->user_data = user_data;
-}
-
-/**
- * gst_buffer_pool_get_user_data:
- * @pool: the #GstBufferPool to get the user data for.
- *
- * Gets the user data of the buffer pool.
- *
- * Returns: the user data associated with this buffer pool.
- */
-gpointer
-gst_buffer_pool_get_user_data (GstBufferPool *pool)
-{
- g_return_val_if_fail (pool != NULL, NULL);
+ /* if we completely merged the two buffers (appended), we can
+ * calculate the duration too. Also make sure we's not messing with
+ * invalid DURATIONS */
+ if (buf1->size + buf2->size == len) {
+ if (GST_BUFFER_DURATION_IS_VALID (buf1) &&
+ GST_BUFFER_DURATION_IS_VALID (buf2)) {
+ /* add duration */
+ GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf1) +
+ GST_BUFFER_DURATION (buf2);
+ }
+ if (GST_BUFFER_OFFSET_END_IS_VALID (buf2)) {
+ /* add offset_end */
+ GST_BUFFER_OFFSET_END (newbuf) = GST_BUFFER_OFFSET_END (buf2);
+ }
+ }
+ }
- return pool->user_data;
+ return newbuf;
}
-