3 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 * Alternatively, the contents of this file may be used under the
24 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
25 * which case the following provisions apply instead of the ones
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Library General Public
30 * License as published by the Free Software Foundation; either
31 * version 2 of the License, or (at your option) any later version.
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Library General Public License for more details.
38 * You should have received a copy of the GNU Library General Public
39 * License along with this library; if not, write to the
40 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
41 * Boston, MA 02111-1307, USA.
44 /* TODO opencv can do scaling for some cases */
52 #include "gstopencvvideofilter.h"
53 #include "gstopencvutils.h"
55 GST_DEBUG_CATEGORY_STATIC (gst_opencv_video_filter_debug);
56 #define GST_CAT_DEFAULT gst_opencv_video_filter_debug
58 /* Filter signals and args */
70 static GstElementClass *parent_class = NULL;
72 static void gst_opencv_video_filter_class_init (GstOpencvVideoFilterClass *
74 static void gst_opencv_video_filter_init (GstOpencvVideoFilter * trans,
75 GstOpencvVideoFilterClass * klass);
76 static void gst_opencv_video_filter_base_init (gpointer gclass);
78 static gboolean gst_opencv_video_filter_set_caps (GstBaseTransform * trans,
79 GstCaps * incaps, GstCaps * outcaps);
80 static GstFlowReturn gst_opencv_video_filter_transform_ip (GstBaseTransform *
81 trans, GstBuffer * buf);
82 static GstFlowReturn gst_opencv_video_filter_transform (GstBaseTransform *
83 trans, GstBuffer * inbuf, GstBuffer * outbuf);
85 static void gst_opencv_video_filter_set_property (GObject * object,
86 guint prop_id, const GValue * value, GParamSpec * pspec);
87 static void gst_opencv_video_filter_get_property (GObject * object,
88 guint prop_id, GValue * value, GParamSpec * pspec);
91 gst_opencv_video_filter_get_type (void)
93 static volatile gsize opencv_base_transform_type = 0;
95 if (g_once_init_enter (&opencv_base_transform_type)) {
97 static const GTypeInfo opencv_base_transform_info = {
98 sizeof (GstOpencvVideoFilterClass),
99 (GBaseInitFunc) gst_opencv_video_filter_base_init,
101 (GClassInitFunc) gst_opencv_video_filter_class_init,
104 sizeof (GstOpencvVideoFilter),
106 (GInstanceInitFunc) gst_opencv_video_filter_init,
109 _type = g_type_register_static (GST_TYPE_VIDEO_FILTER,
110 "GstOpencvVideoFilter", &opencv_base_transform_info,
111 G_TYPE_FLAG_ABSTRACT);
112 g_once_init_leave (&opencv_base_transform_type, _type);
114 return opencv_base_transform_type;
119 gst_opencv_video_filter_finalize (GObject * obj)
121 GstOpencvVideoFilter *transform = GST_OPENCV_VIDEO_FILTER (obj);
123 if (transform->cvImage)
124 cvReleaseImage (&transform->cvImage);
126 G_OBJECT_CLASS (parent_class)->finalize (obj);
129 /* GObject vmethod implementations */
131 gst_opencv_video_filter_base_init (gpointer gclass)
136 gst_opencv_video_filter_class_init (GstOpencvVideoFilterClass * klass)
138 GObjectClass *gobject_class;
139 GstBaseTransformClass *basetrans_class;
141 gobject_class = (GObjectClass *) klass;
142 basetrans_class = (GstBaseTransformClass *) klass;
143 parent_class = g_type_class_peek_parent (klass);
145 GST_DEBUG_CATEGORY_INIT (gst_opencv_video_filter_debug,
146 "opencvbasetransform", 0, "opencvbasetransform element");
148 gobject_class->finalize =
149 GST_DEBUG_FUNCPTR (gst_opencv_video_filter_finalize);
150 gobject_class->set_property = gst_opencv_video_filter_set_property;
151 gobject_class->get_property = gst_opencv_video_filter_get_property;
153 basetrans_class->transform = gst_opencv_video_filter_transform;
154 basetrans_class->transform_ip = gst_opencv_video_filter_transform_ip;
155 basetrans_class->set_caps = gst_opencv_video_filter_set_caps;
159 gst_opencv_video_filter_init (GstOpencvVideoFilter * transform,
160 GstOpencvVideoFilterClass * bclass)
165 gst_opencv_video_filter_transform (GstBaseTransform * trans,
166 GstBuffer * inbuf, GstBuffer * outbuf)
168 GstOpencvVideoFilter *transform;
169 GstOpencvVideoFilterClass *fclass;
171 transform = GST_OPENCV_VIDEO_FILTER (trans);
172 fclass = GST_OPENCV_VIDEO_FILTER_GET_CLASS (transform);
174 g_return_val_if_fail (fclass->cv_trans_func != NULL, GST_FLOW_ERROR);
175 g_return_val_if_fail (transform->cvImage != NULL, GST_FLOW_ERROR);
176 g_return_val_if_fail (transform->out_cvImage != NULL, GST_FLOW_ERROR);
178 transform->cvImage->imageData = (char *) GST_BUFFER_DATA (inbuf);
179 transform->out_cvImage->imageData = (char *) GST_BUFFER_DATA (outbuf);
180 return fclass->cv_trans_func (transform, inbuf, transform->cvImage, outbuf,
181 transform->out_cvImage);
185 gst_opencv_video_filter_transform_ip (GstBaseTransform * trans,
188 GstOpencvVideoFilter *transform;
189 GstOpencvVideoFilterClass *fclass;
191 transform = GST_OPENCV_VIDEO_FILTER (trans);
192 fclass = GST_OPENCV_VIDEO_FILTER_GET_CLASS (transform);
194 g_return_val_if_fail (fclass->cv_trans_ip_func != NULL, GST_FLOW_ERROR);
195 g_return_val_if_fail (transform->cvImage != NULL, GST_FLOW_ERROR);
197 /* TODO this is not always needed and should be solved at BaseTransform
199 buffer = gst_buffer_make_writable (buffer);
201 transform->cvImage->imageData = (char *) GST_BUFFER_DATA (buffer);
203 /* FIXME how to release buffer? */
204 return fclass->cv_trans_ip_func (transform, buffer, transform->cvImage);
208 gst_opencv_video_filter_set_caps (GstBaseTransform * trans, GstCaps * incaps,
211 GstOpencvVideoFilter *transform = GST_OPENCV_VIDEO_FILTER (trans);
212 GstOpencvVideoFilterClass *klass =
213 GST_OPENCV_VIDEO_FILTER_GET_CLASS (transform);
214 gint in_width, in_height;
215 gint in_depth, in_channels;
216 gint out_width, out_height;
217 gint out_depth, out_channels;
218 GError *in_err = NULL;
219 GError *out_err = NULL;
221 if (!gst_opencv_parse_iplimage_params_from_caps (incaps, &in_width,
222 &in_height, &in_depth, &in_channels, &in_err)) {
223 GST_WARNING_OBJECT (transform, "Failed to parse input caps: %s",
225 g_error_free (in_err);
229 if (!gst_opencv_parse_iplimage_params_from_caps (outcaps, &out_width,
230 &out_height, &out_depth, &out_channels, &out_err)) {
231 GST_WARNING_OBJECT (transform, "Failed to parse output caps: %s",
233 g_error_free (out_err);
237 if (klass->cv_set_caps) {
238 if (!klass->cv_set_caps (transform, in_width, in_height, in_depth,
239 in_channels, out_width, out_height, out_depth, out_channels))
243 if (transform->cvImage) {
244 cvReleaseImage (&transform->cvImage);
246 if (transform->out_cvImage) {
247 cvReleaseImage (&transform->out_cvImage);
251 cvCreateImageHeader (cvSize (in_width, in_height), in_depth, in_channels);
252 transform->out_cvImage =
253 cvCreateImageHeader (cvSize (out_width, out_height), out_depth,
256 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (transform),
257 transform->in_place);
262 gst_opencv_video_filter_set_property (GObject * object, guint prop_id,
263 const GValue * value, GParamSpec * pspec)
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273 gst_opencv_video_filter_get_property (GObject * object, guint prop_id,
274 GValue * value, GParamSpec * pspec)
278 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 gst_opencv_video_filter_set_in_place (GstOpencvVideoFilter * transform,
287 transform->in_place = ip;
288 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (transform), ip);