videometa: add GstVideoAffineTransformationMeta
authorMatthew Waters <matthew@centricular.com>
Mon, 2 Nov 2015 12:12:19 +0000 (23:12 +1100)
committerMatthew Waters <matthew@centricular.com>
Tue, 10 Nov 2015 13:19:25 +0000 (00:19 +1100)
Adds a simple 4x4 affine transformations meta for passing arbitrary
transformations on buffers.

Based on patch by Matthieu Bouron

https://bugzilla.gnome.org/show_bug.cgi?id=731791

docs/libs/gst-plugins-base-libs-docs.sgml
docs/libs/gst-plugins-base-libs-sections.txt
gst-libs/gst/video/Makefile.am
gst-libs/gst/video/gstvideoaffinetransformationmeta.c [new file with mode: 0644]
gst-libs/gst/video/gstvideoaffinetransformationmeta.h [new file with mode: 0644]
win32/common/libgstvideo.def

index 7767abf..640fba0 100644 (file)
       </para>
       <xi:include href="xml/gstvideo.xml" />
       <xi:include href="xml/gstvideometa.xml" />
+      <xi:include href="xml/gstvideoaffinetransformationmeta.xml" />
       <xi:include href="xml/gstvideooverlaycomposition.xml" />
       <xi:include href="xml/gstvideofilter.xml" />
       <xi:include href="xml/gstvideodither.xml" />
index e437882..fd1e381 100644 (file)
@@ -2626,6 +2626,18 @@ gst_video_gl_texture_upload_meta_get_info
 </SECTION>
 
 <SECTION>
+<FILE>gstvideoaffinetransformationmeta</FILE>
+<INCLUDE>gst/video/gstvideoaffinetransformationmeta.h</INCLUDE>
+GstVideoAffineTransformationMeta
+gst_buffer_add_video_affine_transformation_meta
+gst_buffer_get_video_affine_transformation_meta
+gst_video_affine_transformation_meta_apply_matrix
+<SUBSECTION Standard>
+gst_video_affine_transformation_meta_api_get_type
+gst_video_affine_transformation_meta_get_info
+</SECTION>
+
+<SECTION>
 <FILE>gstvideooverlaycomposition</FILE>
 <INCLUDE>gst/video/video-overlay-composition.h</INCLUDE>
 <SUBSECTION composition>
index 5d31fa1..c26be81 100644 (file)
@@ -35,6 +35,7 @@ libgstvideo_@GST_API_VERSION@_la_SOURCES = \
        gstvideosink.c          \
        gstvideofilter.c        \
        convertframe.c          \
+       gstvideoaffinetransformationmeta.c \
        gstvideometa.c          \
        gstvideopool.c          \
        videoorientation.c      \
@@ -69,6 +70,7 @@ libgstvideo_@GST_API_VERSION@include_HEADERS = \
        gstvideosink.h          \
        gstvideofilter.h        \
        gstvideometa.h          \
+       gstvideoaffinetransformationmeta.h \
        gstvideopool.h          \
        videoorientation.h      \
        videooverlay.h          \
diff --git a/gst-libs/gst/video/gstvideoaffinetransformationmeta.c b/gst-libs/gst/video/gstvideoaffinetransformationmeta.c
new file mode 100644 (file)
index 0000000..c485310
--- /dev/null
@@ -0,0 +1,152 @@
+/* GStreamer
+ * Copyright (C) <2014> Collabora Ltd.
+ *   Author: Matthieu Bouron <matthieu.bouron@gmail.com>
+ * Copyright (C) 2015, Matthew Waters <matthew@centricular.com>
+ *
+ * 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., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "gstvideoaffinetransformationmeta.h"
+
+#include <string.h>
+
+GType
+gst_video_affine_transformation_meta_api_get_type (void)
+{
+  static volatile GType type = 0;
+  static const gchar *tags[] =
+      { GST_META_TAG_VIDEO_STR, GST_META_TAG_VIDEO_ORIENTATION_STR,
+    GST_META_TAG_VIDEO_ORIENTATION_STR, NULL
+  };
+
+  if (g_once_init_enter (&type)) {
+    GType _type =
+        gst_meta_api_type_register ("GstVideoAffineTransformationAPI", tags);
+    g_once_init_leave (&type, _type);
+  }
+  return type;
+}
+
+static gboolean
+gst_video_affine_transformation_meta_transform (GstBuffer * dest,
+    GstMeta * meta, GstBuffer * buffer, GQuark type, gpointer data)
+{
+  GstVideoAffineTransformationMeta *dmeta, *smeta;
+
+  smeta = (GstVideoAffineTransformationMeta *) meta;
+
+  if (GST_META_TRANSFORM_IS_COPY (type)) {
+    dmeta =
+        (GstVideoAffineTransformationMeta *) gst_buffer_add_meta (dest,
+        GST_VIDEO_AFFINE_TRANSFORMATION_META_INFO, NULL);
+
+    if (!dmeta)
+      return FALSE;
+
+    memcpy (dmeta->matrix, smeta->matrix, sizeof (dmeta->matrix[0]) * 16);
+  }
+  return TRUE;
+}
+
+static gboolean
+gst_video_affine_transformation_meta_init (GstMeta * meta, gpointer params,
+    GstBuffer * buffer)
+{
+  GstVideoAffineTransformationMeta *af_meta =
+      (GstVideoAffineTransformationMeta *) meta;
+  gfloat matrix[] = {
+    1.0f, 0.0f, 0.0f, 0.0f,
+    0.0f, 1.0f, 0.0f, 0.0f,
+    0.0f, 0.0f, 1.0f, 0.0f,
+    0.0f, 0.0f, 0.0f, 1.0f
+  };
+
+  memcpy (af_meta->matrix, matrix, sizeof (matrix[0]) * 16);
+
+  return TRUE;
+}
+
+const GstMetaInfo *
+gst_video_affine_transformation_meta_get_info (void)
+{
+  static const GstMetaInfo *info = NULL;
+
+  if (g_once_init_enter (&info)) {
+    const GstMetaInfo *meta =
+        gst_meta_register (GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE,
+        "GstVideoAffineTransformationMeta",
+        sizeof (GstVideoAffineTransformationMeta),
+        gst_video_affine_transformation_meta_init,
+        NULL,
+        gst_video_affine_transformation_meta_transform);
+    g_once_init_leave (&info, meta);
+  }
+  return info;
+}
+
+/**
+ * gst_buffer_add_video_affine_transformation_meta
+ * @buffer: a #GstBuffer
+ *
+ * Attaches GstVideoAffineTransformationMeta metadata to @buffer with
+ * the given parameters.
+ *
+ * Returns: the #GstVideoAffineTransformationMeta on @buffer.
+ *
+ * Since: 1.8
+ */
+GstVideoAffineTransformationMeta *
+gst_buffer_add_video_affine_transformation_meta (GstBuffer * buffer)
+{
+  GstVideoAffineTransformationMeta *meta;
+
+  g_return_val_if_fail (buffer != NULL, NULL);
+
+  meta =
+      (GstVideoAffineTransformationMeta *) gst_buffer_add_meta (buffer,
+      GST_VIDEO_AFFINE_TRANSFORMATION_META_INFO, NULL);
+
+  if (!meta)
+    return NULL;
+
+  return meta;
+}
+
+/**
+ * gst_video_affine_transformation_meta_apply_matrix:
+ * @meta: a #GstVideoAffineTransformationMeta
+ * @matrix: a 4x4 transformation matrix to be applied
+ *
+ * Apply a transformation using the given 4x4 transformation matrix
+ *
+ * Since: 1.8
+ */
+void gst_video_affine_transformation_meta_apply_matrix
+    (GstVideoAffineTransformationMeta * meta, const gfloat matrix[16])
+{
+  gfloat res[16] = { 0.0f };
+  int i, j, k;
+
+  for (i = 0; i < 4; i++) {
+    for (j = 0; j < 4; j++) {
+      for (k = 0; k < 4; k++) {
+        res[i + (j * 4)] += meta->matrix[i + (k * 4)] * matrix[k + (j * 4)];
+      }
+    }
+  }
+
+  memcpy (meta->matrix, res, sizeof (meta->matrix[0]) * 16);
+}
diff --git a/gst-libs/gst/video/gstvideoaffinetransformationmeta.h b/gst-libs/gst/video/gstvideoaffinetransformationmeta.h
new file mode 100644 (file)
index 0000000..3838a01
--- /dev/null
@@ -0,0 +1,70 @@
+/* GStreamer
+ * Copyright (C) Collabora Ltd.
+ *   Author: Matthieu Bouron <matthieu.bouron@collabora.com>
+ * Copyright (C) 2015, Matthew Waters <matthew@centricular.com>
+ *
+ * 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., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __GST_VIDEO_AFFINE_TRANSFORMATION_META_H__
+#define __GST_VIDEO_AFFINE_TRANSFORMATION_META_H__
+
+#include <gst/gst.h>
+#include <gst/video/video.h>
+
+G_BEGIN_DECLS
+
+#define GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE (gst_video_affine_transformation_meta_api_get_type())
+#define GST_VIDEO_AFFINE_TRANSFORMATION_META_INFO  (gst_video_affine_transformation_meta_get_info())
+
+typedef struct _GstVideoAffineTransformationMeta GstVideoAffineTransformationMeta;
+typedef gboolean (*GstVideoAffineTransformationGetMatrix) (GstVideoAffineTransformationMeta * meta, gfloat * matrix);
+
+#define GST_CAPS_FEATURE_META_GST_VIDEO_AFFINE_TRANSFORMATION_META "meta:GstVideoAffineTransformation"
+#define GST_BUFFER_POOL_OPTION_VIDEO_AFFINE_TRANSFORMATION_META "GstBufferPoolOptionVideoAffineTransformation"
+
+/**
+ * GstVideoAffineTransformation:
+ * @meta: parent #GstMeta
+ * @matrix: the column-major 4x4 transformation matrix
+ *
+ * Extra buffer metadata for performing an affine transformation using a 4x4
+ * matrix. The transformation matrix can be composed with
+ * gst_video_affine_transformation_meta_apply_matrix().
+ *
+ * Since: 1.8
+ */
+
+struct _GstVideoAffineTransformationMeta
+{
+  GstMeta meta;
+
+  gfloat matrix[16];
+};
+
+GType gst_video_affine_transformation_meta_api_get_type          (void);
+const GstMetaInfo *gst_video_affine_transformation_meta_get_info (void);
+
+#define gst_buffer_get_video_affine_transformation_meta(b) \
+    ((GstVideoAffineTransformationMeta *)gst_buffer_get_meta((b),GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE))
+GstVideoAffineTransformationMeta *gst_buffer_add_video_affine_transformation_meta (GstBuffer * buffer);
+
+void gst_video_affine_transformation_meta_apply_matrix                           (GstVideoAffineTransformationMeta * meta,
+                                                                                  const gfloat matrix[16]);
+
+G_END_DECLS
+
+#endif /* __GST_VIDEO_AFFINE_TRANSFORMATION_META_H__ */
index 39b031f..7b25d99 100644 (file)
@@ -1,5 +1,6 @@
 EXPORTS
        _gst_video_decoder_error
+       gst_buffer_add_video_affine_transformation_meta
        gst_buffer_add_video_gl_texture_upload_meta
        gst_buffer_add_video_meta
        gst_buffer_add_video_meta_full
@@ -51,6 +52,9 @@ EXPORTS
        gst_navigation_send_event
        gst_navigation_send_key_event
        gst_navigation_send_mouse_event
+       gst_video_affine_transformation_meta_api_get_type
+       gst_video_affine_transformation_meta_apply_matrix
+       gst_video_affine_transformation_meta_get_info
        gst_video_alignment_reset
        gst_video_alpha_mode_get_type
        gst_video_blend