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