effectv: Use controller where possible, optimize a bit and make properties threadsafe
[platform/upstream/gst-plugins-good.git] / gst / effectv / gstedge.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
5  * EffecTV:
6  * Copyright (C) 2001-2002 FUKUCHI Kentarou
7  *
8  * EdgeTV - detects edge and display it in good old computer way
9  *
10  * EffecTV is free software. This library is free software;
11  * you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  * 
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  * 
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26
27 /**
28  * SECTION:element-edgetv
29  *
30  * EdgeTV detects edges and display it in good old low resolution
31  * computer way.
32  *
33  * <refsect2>
34  * <title>Example launch line</title>
35  * |[
36  * gst-launch -v videotestsrc ! edgetv ! ffmpegcolorspace ! autovideosink
37  * ]| This pipeline shows the effect of edgetv on a test stream.
38  * </refsect2>
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <string.h>
46
47 #include "gstedge.h"
48
49 #include <gst/video/video.h>
50
51 GST_BOILERPLATE (GstEdgeTV, gst_edgetv, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
52
53 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
54 #define CAPS_STR GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_RGBx
55 #else
56 #define CAPS_STR GST_VIDEO_CAPS_xBGR ";" GST_VIDEO_CAPS_xRGB
57 #endif
58
59 static GstStaticPadTemplate gst_edgetv_src_template =
60 GST_STATIC_PAD_TEMPLATE ("src",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS (CAPS_STR)
64     );
65
66 static GstStaticPadTemplate gst_edgetv_sink_template =
67 GST_STATIC_PAD_TEMPLATE ("sink",
68     GST_PAD_SINK,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS (CAPS_STR)
71     );
72
73 static gboolean
74 gst_edgetv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
75     GstCaps * outcaps)
76 {
77   GstEdgeTV *edgetv = GST_EDGETV (btrans);
78   GstStructure *structure;
79   gboolean ret = FALSE;
80
81   structure = gst_caps_get_structure (incaps, 0);
82
83   GST_OBJECT_LOCK (edgetv);
84   if (gst_structure_get_int (structure, "width", &edgetv->width) &&
85       gst_structure_get_int (structure, "height", &edgetv->height)) {
86     guint map_size;
87
88     edgetv->map_width = edgetv->width / 4;
89     edgetv->map_height = edgetv->height / 4;
90     edgetv->video_width_margin = edgetv->width % 4;
91
92     map_size = edgetv->map_width * edgetv->map_height * sizeof (guint32) * 2;
93
94     g_free (edgetv->map);
95     edgetv->map = (guint32 *) g_malloc0 (map_size);
96     ret = TRUE;
97   }
98   GST_OBJECT_UNLOCK (edgetv);
99
100   return ret;
101 }
102
103 static GstFlowReturn
104 gst_edgetv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
105 {
106   GstEdgeTV *filter = GST_EDGETV (trans);
107   gint x, y, r, g, b;
108   guint32 *src, *dest;
109   guint32 p, q;
110   guint32 v0, v1, v2, v3;
111   gint height, width, map_height, map_width;
112   gint video_width_margin;
113   guint32 *map;
114   GstFlowReturn ret = GST_FLOW_OK;
115
116   src = (guint32 *) GST_BUFFER_DATA (in);
117   dest = (guint32 *) GST_BUFFER_DATA (out);
118
119   GST_OBJECT_LOCK (filter);
120   map = filter->map;
121   height = filter->height;
122   width = filter->width;
123   map_height = filter->map_height;
124   map_width = filter->map_width;
125   video_width_margin = filter->video_width_margin;
126   src += width * 4 + 4;
127   dest += width * 4 + 4;
128
129   for (y = 1; y < map_height - 1; y++) {
130     for (x = 1; x < map_width - 1; x++) {
131       p = *src;
132       q = *(src - 4);
133
134       /* difference between the current pixel and right neighbor. */
135       r = ((p & 0xff0000) - (q & 0xff0000)) >> 16;
136       g = ((p & 0xff00) - (q & 0xff00)) >> 8;
137       b = (p & 0xff) - (q & 0xff);
138       r *= r;
139       g *= g;
140       b *= b;
141       r = r >> 5;               /* To lack the lower bit for saturated addition,  */
142       g = g >> 5;               /* devide the value with 32, instead of 16. It is */
143       b = b >> 4;               /* same as `v2 &= 0xfefeff' */
144       if (r > 127)
145         r = 127;
146       if (g > 127)
147         g = 127;
148       if (b > 255)
149         b = 255;
150       v2 = (r << 17) | (g << 9) | b;
151
152       /* difference between the current pixel and upper neighbor. */
153       q = *(src - width * 4);
154       r = ((p & 0xff0000) - (q & 0xff0000)) >> 16;
155       g = ((p & 0xff00) - (q & 0xff00)) >> 8;
156       b = (p & 0xff) - (q & 0xff);
157       r *= r;
158       g *= g;
159       b *= b;
160       r = r >> 5;
161       g = g >> 5;
162       b = b >> 4;
163       if (r > 127)
164         r = 127;
165       if (g > 127)
166         g = 127;
167       if (b > 255)
168         b = 255;
169       v3 = (r << 17) | (g << 9) | b;
170
171       v0 = map[(y - 1) * map_width * 2 + x * 2];
172       v1 = map[y * map_width * 2 + (x - 1) * 2 + 1];
173       map[y * map_width * 2 + x * 2] = v2;
174       map[y * map_width * 2 + x * 2 + 1] = v3;
175       r = v0 + v1;
176       g = r & 0x01010100;
177       dest[0] = r | (g - (g >> 8));
178       r = v0 + v3;
179       g = r & 0x01010100;
180       dest[1] = r | (g - (g >> 8));
181       dest[2] = v3;
182       dest[3] = v3;
183       r = v2 + v1;
184       g = r & 0x01010100;
185       dest[width] = r | (g - (g >> 8));
186       r = v2 + v3;
187       g = r & 0x01010100;
188       dest[width + 1] = r | (g - (g >> 8));
189       dest[width + 2] = v3;
190       dest[width + 3] = v3;
191       dest[width * 2] = v2;
192       dest[width * 2 + 1] = v2;
193       dest[width * 3] = v2;
194       dest[width * 3 + 1] = v2;
195
196       src += 4;
197       dest += 4;
198     }
199     src += width * 3 + 8 + video_width_margin;
200     dest += width * 3 + 8 + video_width_margin;
201   }
202   GST_OBJECT_UNLOCK (filter);
203
204   return ret;
205 }
206
207 static gboolean
208 gst_edgetv_start (GstBaseTransform * trans)
209 {
210   GstEdgeTV *edgetv = GST_EDGETV (trans);
211
212   if (edgetv->map)
213     memset (edgetv->map, 0,
214         edgetv->map_width * edgetv->map_height * sizeof (guint32) * 2);
215   return TRUE;
216 }
217
218 static void
219 gst_edgetv_finalize (GObject * object)
220 {
221   GstEdgeTV *edgetv = GST_EDGETV (object);
222
223   g_free (edgetv->map);
224   edgetv->map = NULL;
225
226   G_OBJECT_CLASS (parent_class)->finalize (object);
227 }
228
229 static void
230 gst_edgetv_base_init (gpointer g_class)
231 {
232   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
233
234   gst_element_class_set_details_simple (element_class, "EdgeTV effect",
235       "Filter/Effect/Video",
236       "Apply edge detect on video", "Wim Taymans <wim.taymans@chello.be>");
237
238   gst_element_class_add_pad_template (element_class,
239       gst_static_pad_template_get (&gst_edgetv_sink_template));
240   gst_element_class_add_pad_template (element_class,
241       gst_static_pad_template_get (&gst_edgetv_src_template));
242 }
243
244 static void
245 gst_edgetv_class_init (GstEdgeTVClass * klass)
246 {
247   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
248   GObjectClass *gobject_class = (GObjectClass *) klass;
249
250   gobject_class->finalize = gst_edgetv_finalize;
251
252   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_edgetv_set_caps);
253   trans_class->transform = GST_DEBUG_FUNCPTR (gst_edgetv_transform);
254   trans_class->start = GST_DEBUG_FUNCPTR (gst_edgetv_start);
255 }
256
257 static void
258 gst_edgetv_init (GstEdgeTV * edgetv, GstEdgeTVClass * klass)
259 {
260 }