Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.git] / gst / videofilters / gstvideofilter2.c
1 /* GStreamer
2  * Copyright (C) 2011 David Schleef <ds@schleef.org>
3  *
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.
8  *
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.
13  *
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., 51 Franklin Street, Suite 500,
17  * Boston, MA 02110-1335, USA.
18  */
19 /**
20  * SECTION:element-gstvideofilter2
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/gst.h>
29 #include <gst/base/gstbasetransform.h>
30 #include <gst/video/video.h>
31 #include "gstvideofilter2.h"
32
33 GST_DEBUG_CATEGORY_STATIC (gst_video_filter2_debug_category);
34 #define GST_CAT_DEFAULT gst_video_filter2_debug_category
35
36 /* prototypes */
37
38
39 static void gst_video_filter2_set_property (GObject * object,
40     guint property_id, const GValue * value, GParamSpec * pspec);
41 static void gst_video_filter2_get_property (GObject * object,
42     guint property_id, GValue * value, GParamSpec * pspec);
43 static void gst_video_filter2_dispose (GObject * object);
44 static void gst_video_filter2_finalize (GObject * object);
45
46 static GstCaps *gst_video_filter2_transform_caps (GstBaseTransform * trans,
47     GstPadDirection direction, GstCaps * caps);
48 static gboolean
49 gst_video_filter2_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
50     guint * size);
51 static gboolean
52 gst_video_filter2_set_caps (GstBaseTransform * trans, GstCaps * incaps,
53     GstCaps * outcaps);
54 static gboolean gst_video_filter2_start (GstBaseTransform * trans);
55 static gboolean gst_video_filter2_stop (GstBaseTransform * trans);
56 static GstFlowReturn gst_video_filter2_transform (GstBaseTransform * trans,
57     GstBuffer * inbuf, GstBuffer * outbuf);
58 static GstFlowReturn gst_video_filter2_transform_ip (GstBaseTransform * trans,
59     GstBuffer * buf);
60
61 enum
62 {
63   PROP_0
64 };
65
66
67 /* class initialization */
68
69 #define DEBUG_INIT(bla) \
70   GST_DEBUG_CATEGORY_INIT (gst_video_filter2_debug_category, "videofilter2", 0, \
71       "debug category for videofilter2 element");
72
73 GST_BOILERPLATE_FULL (GstVideoFilter2, gst_video_filter2, GstBaseTransform,
74     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
75
76 static void
77 gst_video_filter2_base_init (gpointer g_class)
78 {
79   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
80   int i;
81   GstCaps *caps = NULL;
82   GstPadTemplate *pad_template;
83
84   caps = gst_caps_new_empty ();
85   for (i = GST_VIDEO_FORMAT_I420; i <= GST_VIDEO_FORMAT_I420; i++) {
86     gst_caps_append (caps, gst_video_format_new_template_caps (i));
87   }
88
89   pad_template =
90       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
91       gst_caps_ref (caps));
92   gst_element_class_add_pad_template (element_class, pad_template);
93   gst_object_unref (pad_template);
94   pad_template =
95       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
96   gst_element_class_add_pad_template (element_class, pad_template);
97   gst_object_unref (pad_template);
98 }
99
100 static void
101 gst_video_filter2_class_init (GstVideoFilter2Class * klass)
102 {
103   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
104   GstBaseTransformClass *base_transform_class =
105       GST_BASE_TRANSFORM_CLASS (klass);
106
107   gobject_class->set_property = gst_video_filter2_set_property;
108   gobject_class->get_property = gst_video_filter2_get_property;
109   gobject_class->dispose = gst_video_filter2_dispose;
110   gobject_class->finalize = gst_video_filter2_finalize;
111   base_transform_class->transform_caps =
112       GST_DEBUG_FUNCPTR (gst_video_filter2_transform_caps);
113   base_transform_class->get_unit_size =
114       GST_DEBUG_FUNCPTR (gst_video_filter2_get_unit_size);
115   base_transform_class->set_caps =
116       GST_DEBUG_FUNCPTR (gst_video_filter2_set_caps);
117   base_transform_class->start = GST_DEBUG_FUNCPTR (gst_video_filter2_start);
118   base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_video_filter2_stop);
119   base_transform_class->transform =
120       GST_DEBUG_FUNCPTR (gst_video_filter2_transform);
121   base_transform_class->transform_ip =
122       GST_DEBUG_FUNCPTR (gst_video_filter2_transform_ip);
123
124 }
125
126 static void
127 gst_video_filter2_init (GstVideoFilter2 * videofilter2,
128     GstVideoFilter2Class * videofilter2_class)
129 {
130
131   gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (videofilter2), TRUE);
132 }
133
134 void
135 gst_video_filter2_set_property (GObject * object, guint property_id,
136     const GValue * value, GParamSpec * pspec)
137 {
138   g_return_if_fail (GST_IS_VIDEO_FILTER2 (object));
139
140   switch (property_id) {
141     default:
142       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
143       break;
144   }
145 }
146
147 void
148 gst_video_filter2_get_property (GObject * object, guint property_id,
149     GValue * value, GParamSpec * pspec)
150 {
151   g_return_if_fail (GST_IS_VIDEO_FILTER2 (object));
152
153   switch (property_id) {
154     default:
155       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
156       break;
157   }
158 }
159
160 void
161 gst_video_filter2_dispose (GObject * object)
162 {
163   g_return_if_fail (GST_IS_VIDEO_FILTER2 (object));
164
165   /* clean up as possible.  may be called multiple times */
166
167   G_OBJECT_CLASS (parent_class)->dispose (object);
168 }
169
170 void
171 gst_video_filter2_finalize (GObject * object)
172 {
173   g_return_if_fail (GST_IS_VIDEO_FILTER2 (object));
174
175   /* clean up object here */
176
177   G_OBJECT_CLASS (parent_class)->finalize (object);
178 }
179
180 static GstCaps *
181 gst_video_filter2_transform_caps (GstBaseTransform * trans,
182     GstPadDirection direction, GstCaps * caps)
183 {
184   return gst_caps_ref (caps);
185 }
186
187 static gboolean
188 gst_video_filter2_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
189     guint * size)
190 {
191   GstVideoFormat format;
192   gint width, height;
193   gboolean ret;
194
195   ret = gst_video_format_parse_caps (caps, &format, &width, &height);
196   *size = gst_video_format_get_size (format, width, height);
197
198   return ret;
199 }
200
201 static gboolean
202 gst_video_filter2_set_caps (GstBaseTransform * trans, GstCaps * incaps,
203     GstCaps * outcaps)
204 {
205   GstVideoFilter2 *videofilter2;
206   gboolean ret;
207   int width;
208   int height;
209   GstVideoFormat format;
210
211   g_return_val_if_fail (GST_IS_VIDEO_FILTER2 (trans), FALSE);
212   videofilter2 = GST_VIDEO_FILTER2 (trans);
213
214   ret = gst_video_format_parse_caps (incaps, &format, &width, &height);
215
216   if (ret) {
217     videofilter2->format = format;
218     videofilter2->width = width;
219     videofilter2->height = height;
220   }
221
222   return ret;
223 }
224
225 static gboolean
226 gst_video_filter2_start (GstBaseTransform * trans)
227 {
228
229   return FALSE;
230 }
231
232 static gboolean
233 gst_video_filter2_stop (GstBaseTransform * trans)
234 {
235
236   return FALSE;
237 }
238
239 static GstFlowReturn
240 gst_video_filter2_transform (GstBaseTransform * trans, GstBuffer * inbuf,
241     GstBuffer * outbuf)
242 {
243
244   return GST_FLOW_ERROR;
245 }
246
247 static GstFlowReturn
248 gst_video_filter2_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
249 {
250   GstVideoFilter2 *video_filter2 = GST_VIDEO_FILTER2 (trans);
251   GstVideoFilter2Class *klass =
252       GST_VIDEO_FILTER2_CLASS (G_OBJECT_GET_CLASS (trans));
253   int i;
254   GstFlowReturn ret;
255
256   for (i = 0; klass->functions[i].format != GST_VIDEO_FORMAT_UNKNOWN; i++) {
257     if (klass->functions[i].format == video_filter2->format) {
258       ret = klass->functions[i].filter_ip (video_filter2, buf, 0,
259           video_filter2->height);
260       return ret;
261     }
262   }
263
264   return GST_FLOW_ERROR;
265 }
266
267 /* API */
268
269 void
270 gst_video_filter2_class_add_functions (GstVideoFilter2Class * klass,
271     const GstVideoFilter2Functions * functions)
272 {
273   klass->functions = functions;
274 }