1 /* GStreamer
2  * Copyright (C) <2014> Collabora Ltd.
3  *   Author: Matthieu Bouron <matthieu.bouron@gmail.com>
4  * Copyright (C) 2015, Matthew Waters <matthew@centricular.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "gstvideoaffinetransformationmeta.h"
26 
27 #include <string.h>
28 
29 /**
30  * SECTION:gstvideoaffinetransformationmeta
31  * @title: GstVideoAffineTransformationMeta
32  * @short_description: GstMeta for video affine transformation
33  *
34  */
35 
36 GType
gst_video_affine_transformation_meta_api_get_type(void)37 gst_video_affine_transformation_meta_api_get_type (void)
38 {
39   static volatile GType type = 0;
40   static const gchar *tags[] =
41       { GST_META_TAG_VIDEO_STR, GST_META_TAG_VIDEO_ORIENTATION_STR,
42     GST_META_TAG_VIDEO_ORIENTATION_STR, NULL
43   };
44 
45   if (g_once_init_enter (&type)) {
46     GType _type =
47         gst_meta_api_type_register ("GstVideoAffineTransformationAPI", tags);
48     g_once_init_leave (&type, _type);
49   }
50   return type;
51 }
52 
53 static gboolean
gst_video_affine_transformation_meta_transform(GstBuffer * dest,GstMeta * meta,GstBuffer * buffer,GQuark type,gpointer data)54 gst_video_affine_transformation_meta_transform (GstBuffer * dest,
55     GstMeta * meta, GstBuffer * buffer, GQuark type, gpointer data)
56 {
57   GstVideoAffineTransformationMeta *dmeta, *smeta;
58 
59   smeta = (GstVideoAffineTransformationMeta *) meta;
60 
61   if (GST_META_TRANSFORM_IS_COPY (type)) {
62     dmeta =
63         (GstVideoAffineTransformationMeta *) gst_buffer_add_meta (dest,
64         GST_VIDEO_AFFINE_TRANSFORMATION_META_INFO, NULL);
65 
66     if (!dmeta)
67       return FALSE;
68 
69     memcpy (dmeta->matrix, smeta->matrix, sizeof (dmeta->matrix[0]) * 16);
70   }
71   return TRUE;
72 }
73 
74 static gboolean
gst_video_affine_transformation_meta_init(GstMeta * meta,gpointer params,GstBuffer * buffer)75 gst_video_affine_transformation_meta_init (GstMeta * meta, gpointer params,
76     GstBuffer * buffer)
77 {
78   GstVideoAffineTransformationMeta *af_meta =
79       (GstVideoAffineTransformationMeta *) meta;
80   gfloat matrix[] = {
81     1.0f, 0.0f, 0.0f, 0.0f,
82     0.0f, 1.0f, 0.0f, 0.0f,
83     0.0f, 0.0f, 1.0f, 0.0f,
84     0.0f, 0.0f, 0.0f, 1.0f
85   };
86 
87   memcpy (af_meta->matrix, matrix, sizeof (matrix[0]) * 16);
88 
89   return TRUE;
90 }
91 
92 const GstMetaInfo *
gst_video_affine_transformation_meta_get_info(void)93 gst_video_affine_transformation_meta_get_info (void)
94 {
95   static const GstMetaInfo *info = NULL;
96 
97   if (g_once_init_enter ((GstMetaInfo **) & info)) {
98     const GstMetaInfo *meta =
99         gst_meta_register (GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE,
100         "GstVideoAffineTransformationMeta",
101         sizeof (GstVideoAffineTransformationMeta),
102         gst_video_affine_transformation_meta_init,
103         NULL,
104         gst_video_affine_transformation_meta_transform);
105     g_once_init_leave ((GstMetaInfo **) & info, (GstMetaInfo *) meta);
106   }
107   return info;
108 }
109 
110 /**
111  * gst_buffer_add_video_affine_transformation_meta:
112  * @buffer: a #GstBuffer
113  *
114  * Attaches GstVideoAffineTransformationMeta metadata to @buffer with
115  * the given parameters.
116  *
117  * Returns: (transfer none): the #GstVideoAffineTransformationMeta on @buffer.
118  *
119  * Since: 1.8
120  */
121 GstVideoAffineTransformationMeta *
gst_buffer_add_video_affine_transformation_meta(GstBuffer * buffer)122 gst_buffer_add_video_affine_transformation_meta (GstBuffer * buffer)
123 {
124   GstVideoAffineTransformationMeta *meta;
125 
126   g_return_val_if_fail (buffer != NULL, NULL);
127 
128   meta =
129       (GstVideoAffineTransformationMeta *) gst_buffer_add_meta (buffer,
130       GST_VIDEO_AFFINE_TRANSFORMATION_META_INFO, NULL);
131 
132   if (!meta)
133     return NULL;
134 
135   return meta;
136 }
137 
138 /**
139  * gst_video_affine_transformation_meta_apply_matrix:
140  * @meta: a #GstVideoAffineTransformationMeta
141  * @matrix: (array fixed-size=16): a 4x4 transformation matrix to be applied
142  *
143  * Apply a transformation using the given 4x4 transformation matrix.
144  * Performs the multiplication, meta->matrix X matrix.
145  *
146  * Since: 1.8
147  */
gst_video_affine_transformation_meta_apply_matrix(GstVideoAffineTransformationMeta * meta,const gfloat matrix[16])148 void gst_video_affine_transformation_meta_apply_matrix
149     (GstVideoAffineTransformationMeta * meta, const gfloat matrix[16])
150 {
151   gfloat res[16] = { 0.0f };
152   int i, j, k;
153 
154   for (i = 0; i < 4; i++) {
155     for (j = 0; j < 4; j++) {
156       for (k = 0; k < 4; k++) {
157         res[i + (j * 4)] += meta->matrix[i + (k * 4)] * matrix[k + (j * 4)];
158       }
159     }
160   }
161 
162   memcpy (meta->matrix, res, sizeof (meta->matrix[0]) * 16);
163 }
164