• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <2010> Thiago Santos <thiago.sousa.santos@collabora.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "gstgeometrictransform.h"
25 #include "geometricmath.h"
26 #include <string.h>
27 
28 GST_DEBUG_CATEGORY_STATIC (geometric_transform_debug);
29 #define GST_CAT_DEFAULT geometric_transform_debug
30 
31 static GstStaticPadTemplate gst_geometric_transform_src_template =
32 GST_STATIC_PAD_TEMPLATE ("src",
33     GST_PAD_SRC,
34     GST_PAD_ALWAYS,
35     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ ARGB, BGR, BGRA, BGRx, RGB, "
36             "RGBA, RGBx, AYUV, xBGR, xRGB, GRAY8, GRAY16_BE, GRAY16_LE }"))
37     );
38 
39 static GstStaticPadTemplate gst_geometric_transform_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ ARGB, BGR, BGRA, BGRx, RGB, "
44             "RGBA, RGBx, AYUV, xBGR, xRGB, GRAY8, GRAY16_BE, GRAY16_LE }"))
45     );
46 
47 static GstVideoFilterClass *parent_class = NULL;
48 
49 enum
50 {
51   PROP_0,
52   PROP_OFF_EDGE_PIXELS
53 };
54 
55 #define GST_GT_OFF_EDGES_PIXELS_METHOD_TYPE ( \
56     gst_geometric_transform_off_edges_pixels_method_get_type())
57 static GType
gst_geometric_transform_off_edges_pixels_method_get_type(void)58 gst_geometric_transform_off_edges_pixels_method_get_type (void)
59 {
60   static GType method_type = 0;
61 
62   static const GEnumValue method_types[] = {
63     {GST_GT_OFF_EDGES_PIXELS_IGNORE, "Ignore", "ignore"},
64     {GST_GT_OFF_EDGES_PIXELS_CLAMP, "Clamp", "clamp"},
65     {GST_GT_OFF_EDGES_PIXELS_WRAP, "Wrap", "wrap"},
66     {0, NULL, NULL}
67   };
68 
69   if (!method_type) {
70     method_type =
71         g_enum_register_static ("GstGeometricTransformOffEdgesPixelsMethod",
72         method_types);
73   }
74   return method_type;
75 }
76 
77 #define DEFAULT_OFF_EDGE_PIXELS GST_GT_OFF_EDGES_PIXELS_IGNORE
78 
79 /* must be called with the object lock */
80 static gboolean
gst_geometric_transform_generate_map(GstGeometricTransform * gt)81 gst_geometric_transform_generate_map (GstGeometricTransform * gt)
82 {
83   gint x, y;
84   gdouble in_x, in_y;
85   gboolean ret = TRUE;
86   GstGeometricTransformClass *klass;
87   gdouble *ptr;
88 
89   GST_INFO_OBJECT (gt, "Generating new transform map");
90 
91   /* cleanup old map */
92   g_free (gt->map);
93   gt->map = NULL;
94 
95   klass = GST_GEOMETRIC_TRANSFORM_GET_CLASS (gt);
96 
97   /* subclass must have defined the map_func */
98   g_return_val_if_fail (klass->map_func, FALSE);
99 
100   /*
101    * (x,y) pairs of the inverse mapping
102    */
103   gt->map = g_malloc0 (sizeof (gdouble) * gt->width * gt->height * 2);
104   ptr = gt->map;
105 
106   for (y = 0; y < gt->height; y++) {
107     for (x = 0; x < gt->width; x++) {
108       if (!klass->map_func (gt, x, y, &in_x, &in_y)) {
109         /* child should have warned */
110         ret = FALSE;
111         goto end;
112       }
113 
114       ptr[0] = in_x;
115       ptr[1] = in_y;
116       ptr += 2;
117     }
118   }
119 
120 end:
121   if (!ret) {
122     GST_WARNING_OBJECT (gt, "Generating transform map failed");
123     g_free (gt->map);
124     gt->map = NULL;
125   } else
126     gt->needs_remap = FALSE;
127   return ret;
128 }
129 
130 static gboolean
gst_geometric_transform_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)131 gst_geometric_transform_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
132     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
133 {
134   GstGeometricTransform *gt;
135   gboolean ret = TRUE;
136   gint old_width;
137   gint old_height;
138   GstGeometricTransformClass *klass;
139 
140   gt = GST_GEOMETRIC_TRANSFORM_CAST (vfilter);
141   klass = GST_GEOMETRIC_TRANSFORM_GET_CLASS (gt);
142 
143   old_width = gt->width;
144   old_height = gt->height;
145 
146   gt->width = in_info->width;
147   gt->height = in_info->height;
148   gt->row_stride = in_info->stride[0];
149   gt->pixel_stride = GST_VIDEO_INFO_COMP_PSTRIDE (in_info, 0);
150 
151   /* regenerate the map */
152   GST_OBJECT_LOCK (gt);
153   if (gt->map == NULL || old_width == 0 || old_height == 0
154       || gt->width != old_width || gt->height != old_height) {
155     if (klass->prepare_func)
156       if (!klass->prepare_func (gt)) {
157         GST_OBJECT_UNLOCK (gt);
158         return FALSE;
159       }
160     if (gt->precalc_map)
161       gst_geometric_transform_generate_map (gt);
162   }
163   GST_OBJECT_UNLOCK (gt);
164   return ret;
165 }
166 
167 static void
gst_geometric_transform_do_map(GstGeometricTransform * gt,guint8 * in_data,guint8 * out_data,gint x,gint y,gdouble in_x,gdouble in_y)168 gst_geometric_transform_do_map (GstGeometricTransform * gt, guint8 * in_data,
169     guint8 * out_data, gint x, gint y, gdouble in_x, gdouble in_y)
170 {
171   gint in_offset;
172   gint out_offset;
173 
174   out_offset = y * gt->row_stride + x * gt->pixel_stride;
175 
176   /* operate on out of edge pixels */
177   switch (gt->off_edge_pixels) {
178     case GST_GT_OFF_EDGES_PIXELS_CLAMP:
179       in_x = CLAMP (in_x, 0, gt->width - 1);
180       in_y = CLAMP (in_y, 0, gt->height - 1);
181       break;
182 
183     case GST_GT_OFF_EDGES_PIXELS_WRAP:
184       in_x = gst_gm_mod_float (in_x, gt->width);
185       in_y = gst_gm_mod_float (in_y, gt->height);
186       if (in_x < 0)
187         in_x += gt->width;
188       if (in_y < 0)
189         in_y += gt->height;
190       break;
191 
192     default:
193       break;
194   }
195 
196   {
197     gint trunc_x = (gint) in_x;
198     gint trunc_y = (gint) in_y;
199     /* only set the values if the values are valid */
200     if (trunc_x >= 0 && trunc_x < gt->width && trunc_y >= 0 &&
201         trunc_y < gt->height) {
202       in_offset = trunc_y * gt->row_stride + trunc_x * gt->pixel_stride;
203 
204       memcpy (out_data + out_offset, in_data + in_offset, gt->pixel_stride);
205     }
206   }
207 }
208 
209 static void
gst_geometric_transform_before_transform(GstBaseTransform * trans,GstBuffer * outbuf)210 gst_geometric_transform_before_transform (GstBaseTransform * trans,
211     GstBuffer * outbuf)
212 {
213   GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM_CAST (trans);
214   GstClockTime timestamp, stream_time;
215 
216   timestamp = GST_BUFFER_TIMESTAMP (outbuf);
217   stream_time =
218       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
219 
220   GST_DEBUG_OBJECT (gt, "sync to %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp));
221 
222   if (GST_CLOCK_TIME_IS_VALID (stream_time))
223     gst_object_sync_values (GST_OBJECT (gt), stream_time);
224 }
225 
226 static GstFlowReturn
gst_geometric_transform_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)227 gst_geometric_transform_transform_frame (GstVideoFilter * vfilter,
228     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
229 {
230   GstGeometricTransform *gt;
231   GstGeometricTransformClass *klass;
232   gint x, y, i;
233   GstFlowReturn ret = GST_FLOW_OK;
234   gdouble *ptr;
235   guint8 *in_data;
236   guint8 *out_data;
237 
238   gt = GST_GEOMETRIC_TRANSFORM_CAST (vfilter);
239   klass = GST_GEOMETRIC_TRANSFORM_GET_CLASS (gt);
240 
241   in_data = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
242   out_data = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
243 
244   if (GST_VIDEO_FRAME_FORMAT (out_frame) == GST_VIDEO_FORMAT_AYUV) {
245     /* in AYUV black is not just all zeros:
246      * 0x10 is black for Y,
247      * 0x80 is black for Cr and Cb */
248     for (i = 0; i < out_frame->map[0].size; i += 4)
249       GST_WRITE_UINT32_BE (out_data + i, 0xff108080);
250   } else {
251     memset (out_data, 0, out_frame->map[0].size);
252   }
253 
254   GST_OBJECT_LOCK (gt);
255   if (gt->precalc_map) {
256     if (gt->needs_remap) {
257       if (klass->prepare_func)
258         if (!klass->prepare_func (gt)) {
259           ret = FALSE;
260           goto end;
261         }
262       gst_geometric_transform_generate_map (gt);
263     }
264     g_return_val_if_fail (gt->map, GST_FLOW_ERROR);
265     ptr = gt->map;
266     for (y = 0; y < gt->height; y++) {
267       for (x = 0; x < gt->width; x++) {
268         /* do the mapping */
269         gst_geometric_transform_do_map (gt, in_data, out_data, x, y, ptr[0],
270             ptr[1]);
271         ptr += 2;
272       }
273     }
274   } else {
275     for (y = 0; y < gt->height; y++) {
276       for (x = 0; x < gt->width; x++) {
277         gdouble in_x, in_y;
278 
279         if (klass->map_func (gt, x, y, &in_x, &in_y)) {
280           gst_geometric_transform_do_map (gt, in_data, out_data, x, y, in_x,
281               in_y);
282         } else {
283           GST_WARNING_OBJECT (gt, "Failed to do mapping for %d %d", x, y);
284           ret = GST_FLOW_ERROR;
285           goto end;
286         }
287       }
288     }
289   }
290 end:
291   GST_OBJECT_UNLOCK (gt);
292   return ret;
293 }
294 
295 static void
gst_geometric_transform_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)296 gst_geometric_transform_set_property (GObject * object, guint prop_id,
297     const GValue * value, GParamSpec * pspec)
298 {
299   GstGeometricTransform *gt;
300 
301   gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
302 
303   switch (prop_id) {
304     case PROP_OFF_EDGE_PIXELS:
305       GST_OBJECT_LOCK (gt);
306       gt->off_edge_pixels = g_value_get_enum (value);
307       GST_OBJECT_UNLOCK (gt);
308       break;
309     default:
310       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
311       break;
312   }
313 }
314 
315 static void
gst_geometric_transform_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)316 gst_geometric_transform_get_property (GObject * object, guint prop_id,
317     GValue * value, GParamSpec * pspec)
318 {
319   GstGeometricTransform *gt;
320 
321   gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
322 
323   switch (prop_id) {
324     case PROP_OFF_EDGE_PIXELS:
325       g_value_set_enum (value, gt->off_edge_pixels);
326       break;
327     default:
328       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329       break;
330   }
331 }
332 
333 
334 static gboolean
gst_geometric_transform_stop(GstBaseTransform * trans)335 gst_geometric_transform_stop (GstBaseTransform * trans)
336 {
337   GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM_CAST (trans);
338 
339   GST_INFO_OBJECT (gt, "Deleting transform map");
340 
341   gt->width = 0;
342   gt->height = 0;
343 
344   g_free (gt->map);
345   gt->map = NULL;
346 
347   return TRUE;
348 }
349 
350 static void
gst_geometric_transform_base_init(gpointer g_class)351 gst_geometric_transform_base_init (gpointer g_class)
352 {
353   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
354 
355   gst_element_class_add_static_pad_template (element_class,
356       &gst_geometric_transform_sink_template);
357   gst_element_class_add_static_pad_template (element_class,
358       &gst_geometric_transform_src_template);
359 }
360 
361 static void
gst_geometric_transform_class_init(gpointer klass,gpointer class_data)362 gst_geometric_transform_class_init (gpointer klass, gpointer class_data)
363 {
364   GObjectClass *obj_class;
365   GstBaseTransformClass *trans_class;
366   GstVideoFilterClass *vfilter_class;
367 
368   obj_class = (GObjectClass *) klass;
369   trans_class = (GstBaseTransformClass *) klass;
370   vfilter_class = (GstVideoFilterClass *) klass;
371 
372   parent_class = g_type_class_peek_parent (klass);
373 
374   obj_class->set_property = gst_geometric_transform_set_property;
375   obj_class->get_property = gst_geometric_transform_get_property;
376 
377   trans_class->stop = GST_DEBUG_FUNCPTR (gst_geometric_transform_stop);
378   trans_class->before_transform =
379       GST_DEBUG_FUNCPTR (gst_geometric_transform_before_transform);
380 
381   vfilter_class->set_info =
382       GST_DEBUG_FUNCPTR (gst_geometric_transform_set_info);
383   vfilter_class->transform_frame =
384       GST_DEBUG_FUNCPTR (gst_geometric_transform_transform_frame);
385 
386   g_object_class_install_property (obj_class, PROP_OFF_EDGE_PIXELS,
387       g_param_spec_enum ("off-edge-pixels", "Off edge pixels",
388           "What to do with off edge pixels",
389           GST_GT_OFF_EDGES_PIXELS_METHOD_TYPE, DEFAULT_OFF_EDGE_PIXELS,
390           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
391 
392   gst_type_mark_as_plugin_api (GST_GT_OFF_EDGES_PIXELS_METHOD_TYPE, 0);
393   gst_type_mark_as_plugin_api (GST_TYPE_GEOMETRIC_TRANSFORM, 0);
394 }
395 
396 static void
gst_geometric_transform_init(GTypeInstance * instance,gpointer g_class)397 gst_geometric_transform_init (GTypeInstance * instance, gpointer g_class)
398 {
399   GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM_CAST (instance);
400 
401   gt->off_edge_pixels = DEFAULT_OFF_EDGE_PIXELS;
402   gt->precalc_map = TRUE;
403   gt->needs_remap = TRUE;
404 }
405 
406 GType
gst_geometric_transform_get_type(void)407 gst_geometric_transform_get_type (void)
408 {
409   static GType geometric_transform_type = 0;
410 
411   if (!geometric_transform_type) {
412     static const GTypeInfo geometric_transform_info = {
413       sizeof (GstGeometricTransformClass),
414       gst_geometric_transform_base_init,
415       NULL,
416       gst_geometric_transform_class_init,
417       NULL,
418       NULL,
419       sizeof (GstGeometricTransform),
420       0,
421       gst_geometric_transform_init,
422     };
423 
424     geometric_transform_type = g_type_register_static (GST_TYPE_VIDEO_FILTER,
425         "GstGeometricTransform", &geometric_transform_info,
426         G_TYPE_FLAG_ABSTRACT);
427 
428     GST_DEBUG_CATEGORY_INIT (geometric_transform_debug, "geometrictransform", 0,
429         "Base class for geometric transform elements");
430   }
431   return geometric_transform_type;
432 }
433 
434 /*
435  * Must be called with the object lock
436  */
437 void
gst_geometric_transform_set_need_remap(GstGeometricTransform * gt)438 gst_geometric_transform_set_need_remap (GstGeometricTransform * gt)
439 {
440   gt->needs_remap = TRUE;
441 }
442