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