effectv: Add basic documentation for the effectv elements
[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 static GstStaticPadTemplate gst_edgetv_src_template =
54 GST_STATIC_PAD_TEMPLATE ("src",
55     GST_PAD_SRC,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
58     );
59
60 static GstStaticPadTemplate gst_edgetv_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
65     );
66
67 static gboolean
68 gst_edgetv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
69     GstCaps * outcaps)
70 {
71   GstEdgeTV *edgetv = GST_EDGETV (btrans);
72   GstStructure *structure;
73   gboolean ret = FALSE;
74
75   structure = gst_caps_get_structure (incaps, 0);
76
77   if (gst_structure_get_int (structure, "width", &edgetv->width) &&
78       gst_structure_get_int (structure, "height", &edgetv->height)) {
79     guint map_size;
80
81     edgetv->map_width = edgetv->width / 4;
82     edgetv->map_height = edgetv->height / 4;
83     edgetv->video_width_margin = edgetv->width % 4;
84
85     map_size = edgetv->map_width * edgetv->map_height * sizeof (guint32) * 2;
86
87     g_free (edgetv->map);
88     edgetv->map = (guint32 *) g_malloc0 (map_size);
89     ret = TRUE;
90   }
91
92   return ret;
93 }
94
95 static GstFlowReturn
96 gst_edgetv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
97 {
98   GstEdgeTV *filter = GST_EDGETV (trans);
99   gint x, y, r, g, b;
100   guint32 *src, *dest;
101   guint32 p, q;
102   guint32 v0, v1, v2, v3;
103   GstFlowReturn ret = GST_FLOW_OK;
104
105   src = (guint32 *) GST_BUFFER_DATA (in);
106   dest = (guint32 *) GST_BUFFER_DATA (out);
107
108   src += filter->width * 4 + 4;
109   dest += filter->width * 4 + 4;
110
111   for (y = 1; y < filter->map_height - 1; y++) {
112     for (x = 1; x < filter->map_width - 1; x++) {
113       p = *src;
114       q = *(src - 4);
115
116       /* difference between the current pixel and right neighbor. */
117       r = ((p & 0xff0000) - (q & 0xff0000)) >> 16;
118       g = ((p & 0xff00) - (q & 0xff00)) >> 8;
119       b = (p & 0xff) - (q & 0xff);
120       r *= r;
121       g *= g;
122       b *= b;
123       r = r >> 5;               /* To lack the lower bit for saturated addition,  */
124       g = g >> 5;               /* devide the value with 32, instead of 16. It is */
125       b = b >> 4;               /* same as `v2 &= 0xfefeff' */
126       if (r > 127)
127         r = 127;
128       if (g > 127)
129         g = 127;
130       if (b > 255)
131         b = 255;
132       v2 = (r << 17) | (g << 9) | b;
133
134       /* difference between the current pixel and upper neighbor. */
135       q = *(src - filter->width * 4);
136       r = ((p & 0xff0000) - (q & 0xff0000)) >> 16;
137       g = ((p & 0xff00) - (q & 0xff00)) >> 8;
138       b = (p & 0xff) - (q & 0xff);
139       r *= r;
140       g *= g;
141       b *= b;
142       r = r >> 5;
143       g = g >> 5;
144       b = b >> 4;
145       if (r > 127)
146         r = 127;
147       if (g > 127)
148         g = 127;
149       if (b > 255)
150         b = 255;
151       v3 = (r << 17) | (g << 9) | b;
152
153       v0 = filter->map[(y - 1) * filter->map_width * 2 + x * 2];
154       v1 = filter->map[y * filter->map_width * 2 + (x - 1) * 2 + 1];
155       filter->map[y * filter->map_width * 2 + x * 2] = v2;
156       filter->map[y * filter->map_width * 2 + x * 2 + 1] = v3;
157       r = v0 + v1;
158       g = r & 0x01010100;
159       dest[0] = r | (g - (g >> 8));
160       r = v0 + v3;
161       g = r & 0x01010100;
162       dest[1] = r | (g - (g >> 8));
163       dest[2] = v3;
164       dest[3] = v3;
165       r = v2 + v1;
166       g = r & 0x01010100;
167       dest[filter->width] = r | (g - (g >> 8));
168       r = v2 + v3;
169       g = r & 0x01010100;
170       dest[filter->width + 1] = r | (g - (g >> 8));
171       dest[filter->width + 2] = v3;
172       dest[filter->width + 3] = v3;
173       dest[filter->width * 2] = v2;
174       dest[filter->width * 2 + 1] = v2;
175       dest[filter->width * 3] = v2;
176       dest[filter->width * 3 + 1] = v2;
177
178       src += 4;
179       dest += 4;
180     }
181     src += filter->width * 3 + 8 + filter->video_width_margin;
182     dest += filter->width * 3 + 8 + filter->video_width_margin;
183   }
184
185   return ret;
186 }
187
188 static gboolean
189 gst_edgetv_start (GstBaseTransform * trans)
190 {
191   GstEdgeTV *edgetv = GST_EDGETV (trans);
192
193   if (edgetv->map)
194     memset (edgetv->map, 0,
195         edgetv->map_width * edgetv->map_height * sizeof (guint32) * 2);
196   return TRUE;
197 }
198
199 static void
200 gst_edgetv_finalize (GObject * object)
201 {
202   GstEdgeTV *edgetv = GST_EDGETV (object);
203
204   g_free (edgetv->map);
205   edgetv->map = NULL;
206
207   G_OBJECT_CLASS (parent_class)->finalize (object);
208 }
209
210 static void
211 gst_edgetv_base_init (gpointer g_class)
212 {
213   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
214
215   gst_element_class_set_details_simple (element_class, "EdgeTV effect",
216       "Filter/Effect/Video",
217       "Apply edge detect on video", "Wim Taymans <wim.taymans@chello.be>");
218
219   gst_element_class_add_pad_template (element_class,
220       gst_static_pad_template_get (&gst_edgetv_sink_template));
221   gst_element_class_add_pad_template (element_class,
222       gst_static_pad_template_get (&gst_edgetv_src_template));
223 }
224
225 static void
226 gst_edgetv_class_init (GstEdgeTVClass * klass)
227 {
228   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
229   GObjectClass *gobject_class = (GObjectClass *) klass;
230
231   gobject_class->finalize = gst_edgetv_finalize;
232
233   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_edgetv_set_caps);
234   trans_class->transform = GST_DEBUG_FUNCPTR (gst_edgetv_transform);
235   trans_class->start = GST_DEBUG_FUNCPTR (gst_edgetv_start);
236 }
237
238 static void
239 gst_edgetv_init (GstEdgeTV * edgetv, GstEdgeTVClass * klass)
240 {
241 }