beginning of start up script
[sparrow.git] / gstsparrow.c
bloba33aa9c57192a2b36b2e653bc4363c9ae9ede6b1
1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David Schleef <ds@schleef.org>
4 * Copyright (C) <2010> Douglas Bagnall <douglas@halo.gen.nz>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 /**
24 * SECTION:element-sparrow
26 * Performs sparrow correction on a video stream.
28 * <refsect2>
29 * <title>Example launch line</title>
30 * |[
31 * gst-launch videotestsrc ! ffmpegcolorspace ! sparrow ! ximagesink
32 * ]|
33 * </refsect2>
36 #include "sparrow.h"
37 #include "gstsparrow.h"
38 #include <gst/video/gstvideofilter.h>
39 #include <gst/video/video.h>
40 GST_DEBUG_CATEGORY (sparrow_debug);
42 #include <string.h>
43 #include <math.h>
45 /* static_functions */
46 static void gst_sparrow_base_init(gpointer g_class);
47 static void gst_sparrow_class_init(GstSparrowClass *g_class);
48 static void gst_sparrow_init(GstSparrow *sparrow, GstSparrowClass *g_class);
49 static void gst_sparrow_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
50 static void gst_sparrow_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
51 static gboolean gst_sparrow_set_caps(GstBaseTransform *base, GstCaps *incaps, GstCaps *outcaps);
52 static GstFlowReturn gst_sparrow_transform(GstBaseTransform *base, GstBuffer *inbuf, GstBuffer *outbuf);
53 static gboolean plugin_init(GstPlugin *plugin);
56 //#include <sparrow.c>
57 #include <sparrow.h>
60 /* the capabilities of the inputs and outputs.
62 * Use RGB, not YUV, because inverting video is trivial in RGB, not so in YUV
64 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
65 GST_PAD_SINK,
66 GST_PAD_ALWAYS,
67 GST_STATIC_CAPS (
68 GST_VIDEO_CAPS_xBGR "; " GST_VIDEO_CAPS_xRGB "; "
69 GST_VIDEO_CAPS_BGRx "; " GST_VIDEO_CAPS_RGBx)
72 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
73 GST_PAD_SRC,
74 GST_PAD_ALWAYS,
75 GST_STATIC_CAPS (
76 GST_VIDEO_CAPS_xBGR "; " GST_VIDEO_CAPS_xRGB "; "
77 GST_VIDEO_CAPS_BGRx "; " GST_VIDEO_CAPS_RGBx)
81 GST_BOILERPLATE (GstSparrow, gst_sparrow, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
83 /* plugin_init - registers plugin (once)
84 XXX_base_init - for the gobject class (once)
85 XXX_class_init - for global state (once)
86 XXX_init - for each plugin instance
89 static void
90 gst_sparrow_base_init (gpointer g_class)
92 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
94 gst_element_class_set_details_simple (element_class, "Video sparrow correction",
95 "Filter/Effect/Video",
96 "Adds sparrows to a video stream",
97 "Douglas Bagnall <douglas@halo.gen.nz>");
99 gst_element_class_add_pad_template (element_class,
100 gst_static_pad_template_get (&sink_factory));
101 gst_element_class_add_pad_template (element_class,
102 gst_static_pad_template_get (&src_factory));
104 GST_INFO("gst base init\n");
108 /* Clean up */
109 static void
110 gst_sparrow_finalize (GObject * obj){
111 GST_DEBUG("in gst_sparrow_finalize!\n");
112 GstSparrow *sparrow = GST_SPARROW(obj);
113 sparrow_finalise(sparrow);
116 static void
117 gst_sparrow_class_init (GstSparrowClass * g_class)
119 GObjectClass *gobject_class;
120 GstBaseTransformClass *trans_class;
122 gobject_class = G_OBJECT_CLASS (g_class);
123 trans_class = GST_BASE_TRANSFORM_CLASS (g_class);
125 gobject_class->set_property = gst_sparrow_set_property;
126 gobject_class->get_property = gst_sparrow_get_property;
127 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_sparrow_finalize);
129 g_object_class_install_property (gobject_class, PROP_DEBUG,
130 g_param_spec_boolean ("debug", "Debug", "save PPM files of internal state [off]",
131 DEFAULT_PROP_DEBUG,
132 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
134 g_object_class_install_property (gobject_class, PROP_TIMER,
135 g_param_spec_boolean ("timer", "Timer", "Time each transform [off]",
136 DEFAULT_PROP_TIMER,
137 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
139 g_object_class_install_property (gobject_class, PROP_RNG_SEED,
140 g_param_spec_uint ("rngseed", "RNGSeed", "Seed for the random number generator [-1, meaning auto]",
141 0, (guint32)-1, (guint32)DEFAULT_PROP_RNG_SEED,
142 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
144 g_object_class_install_property (gobject_class, PROP_COLOUR,
145 g_param_spec_uint ("colour", "Colour", "Colour for calibration (" QUOTE(SPARROW_GREEN)
146 " for green, " QUOTE(SPARROW_MAGENTA) "for magenta) [" QUOTE(DEFAULT_PROP_COLOUR) "]",
147 0, SPARROW_LAST_COLOUR - 1, (guint32)DEFAULT_PROP_COLOUR,
148 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
150 g_object_class_install_property (gobject_class, PROP_RELOAD,
151 g_param_spec_string ("reload", "Reload", "reload calibration from this file, don't calibrate [None]",
152 DEFAULT_PROP_RELOAD, G_PARAM_READWRITE));
154 g_object_class_install_property (gobject_class, PROP_SAVE,
155 g_param_spec_string ("save", "Save", "save calibration details to this file [None]",
156 DEFAULT_PROP_SAVE, G_PARAM_READWRITE));
158 g_object_class_install_property (gobject_class, PROP_SERIAL,
159 g_param_spec_boolean("serial", "Serial calibration",
160 "Calibrate the projectors one after another, rather than both at once",
161 DEFAULT_PROP_SERIAL, G_PARAM_READWRITE));
163 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_sparrow_set_caps);
164 trans_class->transform = GST_DEBUG_FUNCPTR (gst_sparrow_transform);
165 GST_INFO("gst class init\n");
168 static void
169 gst_sparrow_init (GstSparrow * sparrow, GstSparrowClass * g_class)
171 GST_INFO("gst sparrow init\n");
172 /*disallow resizing */
173 gst_pad_use_fixed_caps(GST_BASE_TRANSFORM_SRC_PAD(sparrow));
176 static inline void
177 set_string_prop(const GValue *value, const char **target){
178 const char *s = g_value_dup_string(value);
179 size_t len = strlen(s);
180 if(len){
181 *target = s;
185 static void
186 gst_sparrow_set_property (GObject * object, guint prop_id, const GValue * value,
187 GParamSpec * pspec)
189 GstSparrow *sparrow;
191 g_return_if_fail (GST_IS_SPARROW (object));
192 sparrow = GST_SPARROW (object);
193 guint val;
194 GST_DEBUG("gst_sparrow_set_property\n");
195 if (value){
196 switch (prop_id) {
197 case PROP_DEBUG:
198 sparrow->debug = g_value_get_boolean(value);
199 GST_DEBUG("debug_value is %d\n", sparrow->debug);
200 break;
201 case PROP_TIMER:
202 sparrow->use_timer = g_value_get_boolean(value);
203 GST_DEBUG("timer_value is %d\n", sparrow->use_timer);
204 break;
205 case PROP_RNG_SEED:
206 sparrow->rng_seed = g_value_get_uint(value);
207 GST_DEBUG("rng seed is %d\n", sparrow->rng_seed);
208 break;
209 case PROP_COLOUR:
210 val = g_value_get_uint(value);
211 if (val < SPARROW_LAST_COLOUR){
212 sparrow->colour = val;
214 GST_DEBUG("colour is %d\n", sparrow->colour);
215 if(sparrow->colour == SPARROW_GREEN)
216 GST_DEBUG("green!");
217 else if(sparrow->colour == SPARROW_MAGENTA)
218 GST_DEBUG("magenta!");
219 else
220 GST_DEBUG("UNKNOWN COLOUR");
221 break;
222 case PROP_RELOAD:
223 set_string_prop(value, &sparrow->reload);
224 GST_DEBUG("reload is %s\n", sparrow->reload);
225 break;
226 case PROP_SAVE:
227 set_string_prop(value, &sparrow->save);
228 GST_DEBUG("save is %s\n", sparrow->save);
229 break;
230 case PROP_SERIAL:
231 sparrow->serial = g_value_get_boolean(value);
232 GST_DEBUG("serial is %d\n", sparrow->serial);
233 break;
234 default:
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 break;
241 static void
242 gst_sparrow_get_property (GObject * object, guint prop_id, GValue * value,
243 GParamSpec * pspec)
245 GstSparrow *sparrow;
247 g_return_if_fail (GST_IS_SPARROW (object));
248 sparrow = GST_SPARROW (object);
250 switch (prop_id) {
251 case PROP_DEBUG:
252 g_value_set_boolean(value, sparrow->debug);
253 break;
254 case PROP_TIMER:
255 g_value_set_boolean(value, sparrow->use_timer);
256 break;
257 case PROP_RNG_SEED:
258 g_value_set_uint(value, sparrow->rng_seed);
259 break;
260 case PROP_COLOUR:
261 g_value_set_uint(value, sparrow->colour);
262 break;
263 case PROP_RELOAD:
264 g_value_set_string(value, sparrow->reload);
265 break;
266 case PROP_SAVE:
267 g_value_set_string(value, sparrow->save);
268 break;
269 case PROP_SERIAL:
270 g_value_set_boolean(value, sparrow->serial);
271 break;
272 default:
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274 break;
280 static gboolean
281 gst_sparrow_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps)
283 GST_INFO("set_caps\n");
284 GstSparrow *sparrow = GST_SPARROW(base);
286 GST_DEBUG_OBJECT (sparrow,
287 "set_caps: \nin %" GST_PTR_FORMAT " \nout %" GST_PTR_FORMAT, incaps, outcaps);
289 /* set_caps gets called after set_property, so it is a good place for hooks
290 that depend on properties and that need to be run before everything
291 starts. */
293 return sparrow_init(sparrow, incaps, outcaps);
298 static GstFlowReturn
299 gst_sparrow_transform (GstBaseTransform * base, GstBuffer *inbuf, GstBuffer *outbuf)
301 GstSparrow *sparrow = GST_SPARROW(base);
302 guint insize = GST_BUFFER_SIZE(inbuf);
303 guint outsize = GST_BUFFER_SIZE(outbuf);
305 if (insize != sparrow->in.size || outsize != sparrow->out.size)
306 goto wrong_size;
308 sparrow_transform(sparrow, inbuf, outbuf);
309 return GST_FLOW_OK;
311 /* ERRORS */
312 wrong_size:
314 GST_ELEMENT_ERROR (sparrow, STREAM, FORMAT,
315 (NULL), ("Invalid buffer size(s)\nIN: size %d, expected %d\nOUT: size %d, expected %d",
316 insize, sparrow->in.size, outsize, sparrow->out.size));
317 return GST_FLOW_ERROR;
322 static gboolean
323 plugin_init (GstPlugin * plugin)
325 GST_DEBUG_CATEGORY_INIT (sparrow_debug, "sparrow", 0, "sparrow");
326 GST_INFO("gst plugin init\n");
328 return gst_element_register (plugin, "sparrow", GST_RANK_NONE, GST_TYPE_SPARROW);
331 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
332 GST_VERSION_MINOR,
333 "sparrow",
334 "Add sparrows to video streams",
335 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);