docs: gst: more gobject introspection annotations
[platform/upstream/gstreamer.git] / gst / gstbuffer.c
index b56b16c..871b34d 100644 (file)
  */
 #include "gst_private.h"
 
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
 #include "gstbuffer.h"
 #include "gstinfo.h"
 #include "gstutils.h"
 #include "gstminiobject.h"
+#include "gstversion.h"
 
 static void gst_buffer_finalize (GstBuffer * buffer);
 static GstBuffer *_gst_buffer_copy (GstBuffer * buffer);
-static GType gst_subbuffer_get_type (void);
 
-static GType _gst_subbuffer_type = 0;
 static GType _gst_buffer_type = 0;
 
+/* 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 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)
 {
@@ -134,7 +165,11 @@ _gst_buffer_initialize (void)
    * done from multiple threads;
    * see http://bugzilla.gnome.org/show_bug.cgi?id=304551 */
   g_type_class_ref (gst_buffer_get_type ());
-  g_type_class_ref (gst_subbuffer_get_type ());
+#ifdef HAVE_GETPAGESIZE
+#ifdef BUFFER_ALIGNMENT_PAGESIZE
+  _gst_buffer_data_alignment = getpagesize ();
+#endif
+#endif
 }
 
 #define _do_init \
@@ -165,6 +200,9 @@ gst_buffer_finalize (GstBuffer * buffer)
 
   gst_caps_replace (&GST_BUFFER_CAPS (buffer), NULL);
 
+  if (buffer->parent)
+    gst_buffer_unref (buffer->parent);
+
 /*   ((GstMiniObjectClass *) */
 /*       gst_buffer_parent_class)->finalize (GST_MINI_OBJECT_CAST (buffer)); */
 }
@@ -199,7 +237,7 @@ gst_buffer_copy_metadata (GstBuffer * dest, const GstBuffer * src,
 
 #if GST_VERSION_NANO == 1
   /* we enable this extra debugging in git versions only for now */
-  g_return_if_fail (gst_buffer_is_metadata_writable (dest));
+  g_warn_if_fail (gst_buffer_is_metadata_writable (dest));
 #endif
 
   GST_CAT_LOG (GST_CAT_BUFFER, "copy %p to %p", src, dest);
@@ -238,7 +276,25 @@ _gst_buffer_copy (GstBuffer * buffer)
   copy = gst_buffer_new ();
 
   /* we simply copy everything from our parent */
+#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;
@@ -267,7 +323,8 @@ gst_buffer_init (GstBuffer * buffer)
  * Creates a newly allocated buffer without any data.
  *
  * MT safe.
- * Returns: the new #GstBuffer.
+ *
+ * Returns: (transfer full): the new #GstBuffer.
  */
 GstBuffer *
 gst_buffer_new (void)
@@ -296,7 +353,8 @@ gst_buffer_new (void)
  * Note that when @size == 0, the buffer data pointer will be NULL.
  *
  * MT safe.
- * Returns: the new #GstBuffer.
+ *
+ * Returns: (transfer full): the new #GstBuffer.
  */
 GstBuffer *
 gst_buffer_new_and_alloc (guint size)
@@ -305,7 +363,22 @@ gst_buffer_new_and_alloc (guint size)
 
   newbuf = gst_buffer_new ();
 
+#ifdef HAVE_POSIX_MEMALIGN
+  {
+    gpointer memptr = NULL;
+
+    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;
 
@@ -326,7 +399,8 @@ gst_buffer_new_and_alloc (guint size)
  *
  * MT safe.
  *
- * Returns: a new #GstBuffer, or NULL if the memory couldn't be allocated.
+ * Returns: (transfer full): a new #GstBuffer, or NULL if the memory couldn't
+ *     be allocated.
  *
  * Since: 0.10.13
  */
@@ -335,13 +409,24 @@ gst_buffer_try_new_and_alloc (guint size)
 {
   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);
 
   if (G_UNLIKELY (malloc_data == NULL && size != 0)) {
     GST_CAT_WARNING (GST_CAT_BUFFER, "failed to allocate %d bytes", size);
     return NULL;
   }
+#endif
 
   /* 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 */
@@ -350,6 +435,9 @@ gst_buffer_try_new_and_alloc (guint size)
   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
 
   GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);
 
@@ -363,7 +451,7 @@ gst_buffer_try_new_and_alloc (guint size)
  * Gets the media type of the buffer. This can be NULL if there
  * is no media type attached to this buffer.
  *
- * Returns: a reference to the #GstCaps. unref after usage.
+ * 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
@@ -387,7 +475,7 @@ gst_buffer_get_caps (GstBuffer * buffer)
 /**
  * gst_buffer_set_caps:
  * @buffer: a #GstBuffer.
- * @caps: a #GstCaps.
+ * @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
@@ -400,9 +488,12 @@ 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_return_if_fail (gst_buffer_is_metadata_writable (buffer));
+  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_caps_replace (&GST_BUFFER_CAPS (buffer), caps);
@@ -426,7 +517,7 @@ gst_buffer_is_metadata_writable (GstBuffer * buf)
 
 /**
  * gst_buffer_make_metadata_writable:
- * @buf: a #GstBuffer
+ * @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
@@ -436,7 +527,8 @@ gst_buffer_is_metadata_writable (GstBuffer * buf)
  * After calling this function, @buf should not be referenced anymore. The
  * result of this function has guaranteed writable metadata.
  *
- * Returns: A new #GstBuffer with 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)
@@ -454,55 +546,7 @@ gst_buffer_make_metadata_writable (GstBuffer * buf)
   return ret;
 }
 
-typedef struct _GstSubBuffer GstSubBuffer;
-typedef struct _GstSubBufferClass GstSubBufferClass;
-
-#define GST_IS_SUBBUFFER(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), _gst_subbuffer_type))
-#define GST_SUBBUFFER_CAST(obj) ((GstSubBuffer *)(obj))
-
-struct _GstSubBuffer
-{
-  GstBuffer buffer;
-
-  GstBuffer *parent;
-};
-
-struct _GstSubBufferClass
-{
-  GstBufferClass buffer_class;
-};
-
-static void gst_subbuffer_finalize (GstSubBuffer * buffer);
-
-#define _do_init_sub \
-{ \
-  _gst_subbuffer_type = g_define_type_id; \
-}
-
-G_DEFINE_TYPE_WITH_CODE (GstSubBuffer, gst_subbuffer, GST_TYPE_BUFFER,
-    _do_init_sub);
-
-static void
-gst_subbuffer_class_init (GstSubBufferClass * klass)
-{
-  klass->buffer_class.mini_object_class.finalize =
-      (GstMiniObjectFinalizeFunction) gst_subbuffer_finalize;
-}
-
-static void
-gst_subbuffer_finalize (GstSubBuffer * buffer)
-{
-  gst_buffer_unref (buffer->parent);
-
-  ((GstMiniObjectClass *) gst_subbuffer_parent_class)->finalize
-      (GST_MINI_OBJECT_CAST (buffer));
-}
-
-static void
-gst_subbuffer_init (GstSubBuffer * instance)
-{
-  GST_BUFFER_FLAG_SET (GST_BUFFER_CAST (instance), GST_BUFFER_FLAG_READONLY);
-}
+#define GST_IS_SUBBUFFER(obj)   (GST_BUFFER_CAST(obj)->parent != NULL)
 
 /**
  * gst_buffer_create_sub:
@@ -521,38 +565,40 @@ gst_subbuffer_init (GstSubBuffer * instance)
  * to #GST_CLOCK_TIME_NONE and #GST_BUFFER_OFFSET_NONE.
  *
  * MT safe.
- * Returns: the new #GstBuffer.
- * Returns NULL if the arguments were invalid.
+ *
+ * Returns: (transfer full): the new #GstBuffer or NULL if the arguments were
+ *     invalid.
  */
 GstBuffer *
 gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size)
 {
-  GstSubBuffer *subbuffer;
+  GstBuffer *subbuffer;
   GstBuffer *parent;
   gboolean complete;
 
   g_return_val_if_fail (buffer != NULL, NULL);
-  g_return_val_if_fail (buffer->mini_object.refcount > 0, 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 = GST_SUBBUFFER_CAST (buffer)->parent;
+    parent = buffer->parent;
   } else {
     parent = buffer;
   }
   gst_buffer_ref (parent);
 
   /* create the new buffer */
-  subbuffer = (GstSubBuffer *) gst_mini_object_new (_gst_subbuffer_type);
+  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 (GST_BUFFER_CAST (subbuffer)) = buffer->data + offset;
-  GST_BUFFER_SIZE (GST_BUFFER_CAST (subbuffer)) = size;
+  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 */
@@ -591,7 +637,7 @@ gst_buffer_create_sub (GstBuffer * buffer, guint offset, guint size)
     GST_BUFFER_OFFSET_END (subbuffer) = GST_BUFFER_OFFSET_NONE;
     GST_BUFFER_CAPS (subbuffer) = NULL;
   }
-  return GST_BUFFER_CAST (subbuffer);
+  return subbuffer;
 }
 
 /**
@@ -611,13 +657,12 @@ gboolean
 gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
 {
   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, FALSE);
-  g_return_val_if_fail (buf1->mini_object.refcount > 0, FALSE);
-  g_return_val_if_fail (buf2->mini_object.refcount > 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_IS_SUBBUFFER (buf1) &&
-      GST_IS_SUBBUFFER (buf2) &&
-      (GST_SUBBUFFER_CAST (buf1)->parent == GST_SUBBUFFER_CAST (buf2)->parent)
+      GST_IS_SUBBUFFER (buf2) && (buf1->parent == buf2->parent)
       && ((buf1->data + buf1->size) == buf2->data));
 }
 
@@ -640,8 +685,9 @@ gst_buffer_is_span_fast (GstBuffer * buf1, GstBuffer * buf2)
  * gst_buffer_is_span_fast() to determine if a memcpy will be needed.
  *
  * MT safe.
- * Returns: the new #GstBuffer that spans the two source buffers.
- * Returns NULL if the arguments are invalid.
+ *
+ * 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,
@@ -650,14 +696,14 @@ gst_buffer_span (GstBuffer * buf1, guint32 offset, GstBuffer * buf2,
   GstBuffer *newbuf;
 
   g_return_val_if_fail (buf1 != NULL && buf2 != NULL, NULL);
-  g_return_val_if_fail (buf1->mini_object.refcount > 0, NULL);
-  g_return_val_if_fail (buf2->mini_object.refcount > 0, NULL);
-  g_return_val_if_fail (len > 0, 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_SUBBUFFER_CAST (buf1)->parent;
+    GstBuffer *parent = buf1->parent;
 
     /* we simply create a subbuffer of the common parent */
     newbuf = gst_buffer_create_sub (parent,