edgetv: Clean up edgetv element and fix memory leak
[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 gboolean
112 gst_edgetv_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
113     guint * size)
114 {
115   GstEdgeTV *filter = GST_EDGETV (btrans);
116   GstStructure *structure;
117   gboolean ret = FALSE;
118   gint width, height;
119
120   structure = gst_caps_get_structure (caps, 0);
121
122   if (gst_structure_get_int (structure, "width", &width) &&
123       gst_structure_get_int (structure, "height", &height)) {
124     *size = width * height * 32 / 8;
125     ret = TRUE;
126     GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
127         width, height);
128   }
129
130   return ret;
131 }
132
133 static GstFlowReturn
134 gst_edgetv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
135 {
136   GstEdgeTV *filter = GST_EDGETV (trans);
137   gint x, y, r, g, b;
138   guint32 *src, *dest;
139   guint32 p, q;
140   guint32 v0, v1, v2, v3;
141   GstFlowReturn ret = GST_FLOW_OK;
142
143   src = (guint32 *) GST_BUFFER_DATA (in);
144   dest = (guint32 *) GST_BUFFER_DATA (out);
145
146   src += filter->width * 4 + 4;
147   dest += filter->width * 4 + 4;
148
149   for (y = 1; y < filter->map_height - 1; y++) {
150     for (x = 1; x < filter->map_width - 1; x++) {
151       p = *src;
152       q = *(src - 4);
153
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);
158       r *= r;
159       g *= g;
160       b *= b;
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' */
164       if (r > 127)
165         r = 127;
166       if (g > 127)
167         g = 127;
168       if (b > 255)
169         b = 255;
170       v2 = (r << 17) | (g << 9) | b;
171
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);
177       r *= r;
178       g *= g;
179       b *= b;
180       r = r >> 5;
181       g = g >> 5;
182       b = b >> 4;
183       if (r > 127)
184         r = 127;
185       if (g > 127)
186         g = 127;
187       if (b > 255)
188         b = 255;
189       v3 = (r << 17) | (g << 9) | b;
190
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;
195       r = v0 + v1;
196       g = r & 0x01010100;
197       dest[0] = r | (g - (g >> 8));
198       r = v0 + v3;
199       g = r & 0x01010100;
200       dest[1] = r | (g - (g >> 8));
201       dest[2] = v3;
202       dest[3] = v3;
203       r = v2 + v1;
204       g = r & 0x01010100;
205       dest[filter->width] = r | (g - (g >> 8));
206       r = v2 + v3;
207       g = r & 0x01010100;
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;
215
216       src += 4;
217       dest += 4;
218     }
219     src += filter->width * 3 + 8 + filter->video_width_margin;
220     dest += filter->width * 3 + 8 + filter->video_width_margin;
221   }
222
223   return ret;
224 }
225
226 static gboolean
227 gst_edgetv_start (GstBaseTransform * trans)
228 {
229   GstEdgeTV *edgetv = GST_EDGETV (trans);
230
231   if (edgetv->map)
232     memset (edgetv->map, 0,
233         edgetv->map_width * edgetv->map_height * sizeof (guint32) * 2);
234   return TRUE;
235 }
236
237 static void
238 gst_edgetv_finalize (GObject * object)
239 {
240   GstEdgeTV *edgetv = GST_EDGETV (object);
241
242   g_free (edgetv->map);
243   edgetv->map = NULL;
244
245   G_OBJECT_CLASS (parent_class)->finalize (object);
246 }
247
248 static void
249 gst_edgetv_base_init (gpointer g_class)
250 {
251   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
252
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>");
256
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));
261 }
262
263 static void
264 gst_edgetv_class_init (GstEdgeTVClass * klass)
265 {
266   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
267   GObjectClass *gobject_class = (GObjectClass *) klass;
268
269   gobject_class->finalize = gst_edgetv_finalize;
270
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);
275 }
276
277 static void
278 gst_edgetv_init (GstEdgeTV * edgetv, GstEdgeTVClass * klass)
279 {
280 }