2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 /* based on the Area Based Deinterlacer (for RGB frames) */
20 /* (a VirtualDub filter) from Gunnar Thalin <guth@home.se> */
27 #include "gstdeinterlace.h"
29 /* elementfactory information */
30 static GstElementDetails deinterlace_details = {
36 "Wim Taymans <wim.taymans@chello.be>",
41 /* Filter signals and args */
55 GST_PAD_TEMPLATE_FACTORY (deinterlace_src_factory,
62 "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")),
63 "width", GST_PROPS_INT_POSITIVE,
64 "height", GST_PROPS_INT_POSITIVE
68 GST_PAD_TEMPLATE_FACTORY (deinterlace_sink_factory,
75 "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")),
76 "width", GST_PROPS_INT_POSITIVE,
77 "height", GST_PROPS_INT_POSITIVE
81 static GType gst_deinterlace_get_type (void);
83 static void gst_deinterlace_class_init (GstDeInterlaceClass *klass);
84 static void gst_deinterlace_init (GstDeInterlace *filter);
86 static void gst_deinterlace_set_property (GObject *object, guint prop_id,
87 const GValue *value, GParamSpec *pspec);
88 static void gst_deinterlace_get_property (GObject *object, guint prop_id,
89 GValue *value, GParamSpec *pspec);
91 static void gst_deinterlace_chain (GstPad *pad, GstBuffer *buf);
93 static GstElementClass *parent_class = NULL;
94 /*static guint gst_filter_signals[LAST_SIGNAL] = { 0 }; */
97 gst_deinterlace_get_type(void) {
98 static GType deinterlace_type = 0;
100 if (!deinterlace_type) {
101 static const GTypeInfo deinterlace_info = {
102 sizeof(GstDeInterlaceClass), NULL,
104 (GClassInitFunc)gst_deinterlace_class_init,
107 sizeof(GstDeInterlace),
109 (GInstanceInitFunc)gst_deinterlace_init,
111 deinterlace_type = g_type_register_static(GST_TYPE_ELEMENT, "GstDeInterlace", &deinterlace_info, 0);
113 return deinterlace_type;
117 gst_deinterlace_class_init (GstDeInterlaceClass *klass)
119 GObjectClass *gobject_class;
120 GstElementClass *gstelement_class;
122 gobject_class = (GObjectClass*)klass;
123 gstelement_class = (GstElementClass*)klass;
125 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
127 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DI_ONLY,
128 g_param_spec_boolean("di_area_only","di_area_only","di_area_only",
129 TRUE,G_PARAM_READWRITE)); /* CHECKME */
130 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BLEND,
131 g_param_spec_boolean("blend","blend","blend",
132 TRUE,G_PARAM_READWRITE)); /* CHECKME */
133 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_THRESHOLD,
134 g_param_spec_int("threshold","threshold","threshold",
135 G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
136 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_EDGE_DETECT,
137 g_param_spec_int("edge_detect","edge_detect","edge_detect",
138 G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
140 gobject_class->set_property = gst_deinterlace_set_property;
141 gobject_class->get_property = gst_deinterlace_get_property;
144 static GstPadLinkReturn
145 gst_deinterlace_sinkconnect (GstPad *pad, GstCaps *caps)
147 GstDeInterlace *filter;
149 filter = GST_DEINTERLACE(gst_pad_get_parent (pad));
151 if (!GST_CAPS_IS_FIXED (caps))
152 return GST_PAD_LINK_DELAYED;
154 gst_caps_get_int (caps, "width", &filter->width);
155 gst_caps_get_int (caps, "height", &filter->height);
157 if (filter->picsize != (filter->width*filter->height)) {
160 filter->picsize = filter->width*filter->height;
161 filter->src = g_malloc(filter->picsize);
163 return gst_pad_try_set_caps (filter->srcpad, caps);
167 gst_deinterlace_init (GstDeInterlace *filter)
169 filter->sinkpad = gst_pad_new_from_template(deinterlace_sink_factory (),"sink");
170 gst_pad_set_chain_function(filter->sinkpad,gst_deinterlace_chain);
171 gst_pad_set_link_function(filter->sinkpad,gst_deinterlace_sinkconnect);
172 gst_element_add_pad(GST_ELEMENT(filter),filter->sinkpad);
174 filter->srcpad = gst_pad_new_from_template(deinterlace_src_factory (),"src");
175 gst_element_add_pad(GST_ELEMENT(filter),filter->srcpad);
177 filter->show_deinterlaced_area_only = FALSE;
178 filter->blend = FALSE;
179 /*filter->threshold_blend = 0; */
180 filter->threshold = 50;
181 filter->edge_detect = 25;
188 gst_deinterlace_chain (GstPad *pad, GstBuffer *buf)
190 GstDeInterlace *filter;
192 guchar *psrc1, *psrc2, *psrc3, *pdst1, *yuvptr, *src;
193 gint iInterlaceValue0, iInterlaceValue1, iInterlaceValue2;
196 guchar *y_dst, *y_src;
201 gboolean bShowDeinterlacedAreaOnly;
203 g_return_if_fail (pad != NULL);
204 g_return_if_fail (GST_IS_PAD (pad));
205 g_return_if_fail (buf != NULL);
207 filter = GST_DEINTERLACE (gst_pad_get_parent (pad));
209 bBlend = filter->blend;
210 iThreshold = filter->threshold;
211 iEdgeDetect = filter->edge_detect;
212 width = filter->width;
213 height = filter->height;
215 yuvptr = GST_BUFFER_DATA (buf);
216 bShowDeinterlacedAreaOnly = filter->show_deinterlaced_area_only;
218 memcpy(filter->src, yuvptr, filter->picsize);
220 y_dst = yuvptr; /* dst y pointer */
221 /* we should not change u,v because one u, v value stands for */
222 /* 2 pixels per 2 lines = 4 pixel and we don't want to change */
228 iThreshold = iThreshold * iThreshold * 4;
229 /* We don't want an integer overflow in the interlace calculation. */
230 if (iEdgeDetect > 180)
232 iEdgeDetect = iEdgeDetect * iEdgeDetect;
234 y1 = 0; /* Avoid compiler warning. The value is not used. */
235 for (x = 0; x < width; x++) {
238 psrc2 = psrc3 + y_line;
241 iInterlaceValue1 = iInterlaceValue2 = 0;
242 for (y = 0; y <= height; y++) {
245 psrc3 = psrc3 + y_line;
249 if (y < height - 1) {
255 iInterlaceValue0 = iInterlaceValue1;
256 iInterlaceValue1 = iInterlaceValue2;
259 iInterlaceValue2 = ((y1 - y2) * (y3 - y2) -
260 ((iEdgeDetect * (y1 - y3) * (y1 - y3)) >> 12))*10;
262 iInterlaceValue2 = 0;
265 if (iInterlaceValue0 + 2 * iInterlaceValue1 + iInterlaceValue2 > iThreshold) {
267 *pdst1 = (unsigned char)((y0 + 2*y1 + y2) >> 2);
269 /* this method seems to work better than blending if the */
270 /* quality is pretty bad and the half pics don't fit together */
271 if ((y % 2)==1) { /* if odd simply copy the value */
273 /**pdst1 = 0; // FIXME this is for adjusting an initial iThreshold */
274 } else { /* even interpolate the even line (upper + lower)/2 */
275 *pdst1 = (unsigned char)((y0 + y2) >> 1);
276 /**pdst1 = 0; // FIXME this is for adjusting an initial iThreshold */
280 /* so we went below the treshold and therefore we don't have to */
281 /* change anything */
282 if (bShowDeinterlacedAreaOnly) {
283 /* this is for testing to see how we should tune the treshhold */
284 /* and shows as the things that haven't change because the */
285 /* threshhold was to low?? (or shows that everything is ok :-) */
286 *pdst1 = 0; /* blank the point and so the interlac area */
291 pdst1 = pdst1 + y_line;
296 gst_pad_push (filter->srcpad, buf);
300 gst_deinterlace_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
302 GstDeInterlace *filter;
304 /* it's not null if we got it, but it might not be ours */
305 g_return_if_fail(GST_IS_DEINTERLACE(object));
307 filter = GST_DEINTERLACE(object);
312 filter->show_deinterlaced_area_only = g_value_get_boolean (value);
315 filter->blend = g_value_get_boolean (value);
318 filter->threshold = g_value_get_int (value);
320 case ARG_EDGE_DETECT:
321 filter->edge_detect = g_value_get_int (value);
329 gst_deinterlace_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
331 GstDeInterlace *filter;
333 /* it's not null if we got it, but it might not be ours */
334 g_return_if_fail(GST_IS_DEINTERLACE(object));
336 filter = GST_DEINTERLACE(object);
340 g_value_set_boolean (value, filter->show_deinterlaced_area_only);
343 g_value_set_boolean (value, filter->blend);
346 g_value_set_int (value, filter->threshold);
348 case ARG_EDGE_DETECT:
349 g_value_set_int (value, filter->edge_detect);
352 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
358 plugin_init (GModule *module, GstPlugin *plugin)
360 GstElementFactory *factory;
362 factory = gst_element_factory_new("deinterlace",GST_TYPE_DEINTERLACE,
363 &deinterlace_details);
364 g_return_val_if_fail(factory != NULL, FALSE);
366 gst_element_factory_add_pad_template (factory,
367 GST_PAD_TEMPLATE_GET (deinterlace_src_factory));
368 gst_element_factory_add_pad_template (factory,
369 GST_PAD_TEMPLATE_GET (deinterlace_sink_factory));
371 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
376 GstPluginDesc plugin_desc = {