2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 * Copyright (C) 2001-2002 FUKUCHI Kentarou
8 * EdgeTV - detects edge and display it in good old computer way
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.
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.
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.
35 #include <gst/video/video.h>
36 #include <gst/video/gstvideofilter.h>
38 #define GST_TYPE_EDGETV \
39 (gst_edgetv_get_type())
40 #define GST_EDGETV(obj) \
41 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_EDGETV,GstEdgeTV))
42 #define GST_EDGETV_CLASS(klass) \
43 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_EDGETV,GstEdgeTVClass))
44 #define GST_IS_EDGETV(obj) \
45 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_EDGETV))
46 #define GST_IS_EDGETV_CLASS(klass) \
47 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_EDGETV))
49 typedef struct _GstEdgeTV GstEdgeTV;
50 typedef struct _GstEdgeTVClass GstEdgeTVClass;
54 GstVideoFilter videofilter;
57 gint map_width, map_height;
59 gint video_width_margin;
62 struct _GstEdgeTVClass
64 GstVideoFilterClass parent_class;
67 GST_BOILERPLATE (GstEdgeTV, gst_edgetv, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
69 static GstStaticPadTemplate gst_edgetv_src_template =
70 GST_STATIC_PAD_TEMPLATE ("src",
73 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
76 static GstStaticPadTemplate gst_edgetv_sink_template =
77 GST_STATIC_PAD_TEMPLATE ("sink",
80 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
84 gst_edgetv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
87 GstEdgeTV *edgetv = GST_EDGETV (btrans);
88 GstStructure *structure;
91 structure = gst_caps_get_structure (incaps, 0);
93 if (gst_structure_get_int (structure, "width", &edgetv->width) &&
94 gst_structure_get_int (structure, "height", &edgetv->height)) {
97 edgetv->map_width = edgetv->width / 4;
98 edgetv->map_height = edgetv->height / 4;
99 edgetv->video_width_margin = edgetv->width % 4;
101 map_size = edgetv->map_width * edgetv->map_height * sizeof (guint32) * 2;
103 g_free (edgetv->map);
104 edgetv->map = (guint32 *) g_malloc0 (map_size);
112 gst_edgetv_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
115 GstEdgeTV *filter = GST_EDGETV (btrans);
116 GstStructure *structure;
117 gboolean ret = FALSE;
120 structure = gst_caps_get_structure (caps, 0);
122 if (gst_structure_get_int (structure, "width", &width) &&
123 gst_structure_get_int (structure, "height", &height)) {
124 *size = width * height * 32 / 8;
126 GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
134 gst_edgetv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
136 GstEdgeTV *filter = GST_EDGETV (trans);
140 guint32 v0, v1, v2, v3;
141 GstFlowReturn ret = GST_FLOW_OK;
143 src = (guint32 *) GST_BUFFER_DATA (in);
144 dest = (guint32 *) GST_BUFFER_DATA (out);
146 src += filter->width * 4 + 4;
147 dest += filter->width * 4 + 4;
149 for (y = 1; y < filter->map_height - 1; y++) {
150 for (x = 1; x < filter->map_width - 1; x++) {
154 /* difference between the current pixel and right neighbor. */
155 r = ((p & 0xff0000) - (q & 0xff0000)) >> 16;
156 g = ((p & 0xff00) - (q & 0xff00)) >> 8;
157 b = (p & 0xff) - (q & 0xff);
161 r = r >> 5; /* To lack the lower bit for saturated addition, */
162 g = g >> 5; /* devide the value with 32, instead of 16. It is */
163 b = b >> 4; /* same as `v2 &= 0xfefeff' */
170 v2 = (r << 17) | (g << 9) | b;
172 /* difference between the current pixel and upper neighbor. */
173 q = *(src - filter->width * 4);
174 r = ((p & 0xff0000) - (q & 0xff0000)) >> 16;
175 g = ((p & 0xff00) - (q & 0xff00)) >> 8;
176 b = (p & 0xff) - (q & 0xff);
189 v3 = (r << 17) | (g << 9) | b;
191 v0 = filter->map[(y - 1) * filter->map_width * 2 + x * 2];
192 v1 = filter->map[y * filter->map_width * 2 + (x - 1) * 2 + 1];
193 filter->map[y * filter->map_width * 2 + x * 2] = v2;
194 filter->map[y * filter->map_width * 2 + x * 2 + 1] = v3;
197 dest[0] = r | (g - (g >> 8));
200 dest[1] = r | (g - (g >> 8));
205 dest[filter->width] = r | (g - (g >> 8));
208 dest[filter->width + 1] = r | (g - (g >> 8));
209 dest[filter->width + 2] = v3;
210 dest[filter->width + 3] = v3;
211 dest[filter->width * 2] = v2;
212 dest[filter->width * 2 + 1] = v2;
213 dest[filter->width * 3] = v2;
214 dest[filter->width * 3 + 1] = v2;
219 src += filter->width * 3 + 8 + filter->video_width_margin;
220 dest += filter->width * 3 + 8 + filter->video_width_margin;
227 gst_edgetv_start (GstBaseTransform * trans)
229 GstEdgeTV *edgetv = GST_EDGETV (trans);
232 memset (edgetv->map, 0,
233 edgetv->map_width * edgetv->map_height * sizeof (guint32) * 2);
238 gst_edgetv_finalize (GObject * object)
240 GstEdgeTV *edgetv = GST_EDGETV (object);
242 g_free (edgetv->map);
245 G_OBJECT_CLASS (parent_class)->finalize (object);
249 gst_edgetv_base_init (gpointer g_class)
251 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
253 gst_element_class_set_details_simple (element_class, "EdgeTV effect",
254 "Filter/Effect/Video",
255 "Apply edge detect on video", "Wim Taymans <wim.taymans@chello.be>");
257 gst_element_class_add_pad_template (element_class,
258 gst_static_pad_template_get (&gst_edgetv_sink_template));
259 gst_element_class_add_pad_template (element_class,
260 gst_static_pad_template_get (&gst_edgetv_src_template));
264 gst_edgetv_class_init (GstEdgeTVClass * klass)
266 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
267 GObjectClass *gobject_class = (GObjectClass *) klass;
269 gobject_class->finalize = gst_edgetv_finalize;
271 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_edgetv_set_caps);
272 trans_class->get_unit_size = GST_DEBUG_FUNCPTR (gst_edgetv_get_unit_size);
273 trans_class->transform = GST_DEBUG_FUNCPTR (gst_edgetv_transform);
274 trans_class->start = GST_DEBUG_FUNCPTR (gst_edgetv_start);
278 gst_edgetv_init (GstEdgeTV * edgetv, GstEdgeTVClass * klass)