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