use constant macros where possible
[sparrow.git] / gstsparrow.h
blob365b220e94bc3549c6c4c054d049bb464b87150e
1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David Schleef <ds@schleef.org>
4 * Copyright (C) <2003> Arwed v. Merkatz <v.merkatz@gmx.net>
5 * Copyright (C) <2006> Mark Nauwelaerts <manauw@skynet.be>
6 * Copyright (C) <2010> Douglas Bagnall <douglas@halo.gen.nz>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 #ifndef __GST_VIDEO_SPARROW_H__
26 #define __GST_VIDEO_SPARROW_H__
28 #include <gst/video/gstvideofilter.h>
29 #include <sys/time.h>
31 G_BEGIN_DECLS
33 #define SPARROW_PPM_DEBUG 1
35 #define TIMER_LOG_FILE "/tmp/timer.log"
37 #include "sparrowconfig.h"
38 #include "dSFMT/dSFMT.h"
39 #include "cv.h"
41 #ifndef UNUSED
42 #define UNUSED __attribute__ ((unused))
43 #else
44 #warning UNUSED is set
45 #endif
47 /* the common recommendation for function visibility is to default to 'hidden'
48 and specifically mark the unhidden ('default') ones, but this might muck
49 with gstreamer macros, some of which declare functions, and most sparrow
50 functions are static anyway, so it is simpler to whitelist visibility.
52 http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fvisibility-2135
54 (actually, it seems like all functions are invisible or static, except the
55 ones that gstreamer makes in macros).
57 #ifndef INVISIBLE
58 #define INVISIBLE __attribute__ ((visibility("hidden")))
59 #else
60 #warning INVISIBLE is set
61 #endif
64 typedef guint32 pix_t;
65 #define PIXSIZE (sizeof(pix_t))
68 #define CALIBRATE_ON_MIN_T 2
69 #define CALIBRATE_ON_MAX_T 7
70 #define CALIBRATE_OFF_MIN_T 2
71 #define CALIBRATE_OFF_MAX_T 9
72 #define CALIBRATE_SELF_SIZE 24
74 #define CALIBRATE_MAX_VOTE_ERROR 5
75 #define CALIBRATE_MAX_BEST_ERROR 2
76 #define CALIBRATE_INITIAL_WAIT 72
77 #define CALIBRATE_RETRY_WAIT 16
79 #define CALIBRATE_SIGNAL_THRESHOLD 200
81 #define SPARROW_N_IPL_IN 3
83 #define MAX_CALIBRATE_SHAPES 4
85 #define FAKE_OTHER_PROJECTION 1
87 #define WAIT_COUNTDOWN (MAX(CALIBRATE_OFF_MAX_T, CALIBRATE_ON_MAX_T) + 3)
89 #define GST_TYPE_SPARROW \
90 (gst_sparrow_get_type())
91 #define GST_SPARROW(obj) \
92 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SPARROW,GstSparrow))
93 #define GST_SPARROW_CLASS(klass) \
94 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SPARROW,GstSparrowClass))
95 #define GST_IS_SPARROW(obj) \
96 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SPARROW))
97 #define GST_IS_SPARROW_CLASS(klass) \
98 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SPARROW))
102 typedef enum {
103 SPARROW_STATUS_QUO = 0,
104 SPARROW_INIT,
105 SPARROW_FIND_SELF,
106 SPARROW_FIND_SCREEN,
107 SPARROW_FIND_EDGES,
108 SPARROW_PICK_COLOUR,
109 SPARROW_WAIT_FOR_GRID,
110 SPARROW_FIND_GRID,
111 SPARROW_PLAY,
114 SPARROW_NEXT_STATE /*magical last state: alias for next in sequence */
115 } sparrow_state;
117 typedef enum {
118 SPARROW_WHITE = 0,
119 SPARROW_GREEN,
120 SPARROW_MAGENTA,
121 } sparrow_colour;
123 #define MAX_CALIBRATION_LAG 12
124 typedef struct lag_times_s {
125 //guint32 hits;
126 guint64 record;
127 } lag_times_t;
129 typedef struct sparrow_format_s {
130 gint32 width;
131 gint32 height;
132 guint32 pixcount;
133 guint32 size;
135 guint32 rshift;
136 guint32 gshift;
137 guint32 bshift;
138 guint32 rmask;
139 guint32 gmask;
140 guint32 bmask;
141 guint32 colours[3];
142 } sparrow_format;
144 enum calibration_shape {
145 NO_SHAPE = 0,
146 VERTICAL_LINE,
147 HORIZONTAL_LINE,
148 FULLSCREEN,
149 RECTANGLE,
152 typedef struct sparrow_shape_s {
153 /*Calibration shape definition -- a rectangle.*/
154 enum calibration_shape shape;
155 gint x;
156 gint y;
157 gint w;
158 gint h;
159 } sparrow_shape_t;
161 typedef enum sparrow_axis_s {
162 SPARROW_VERTICAL,
163 SPARROW_HORIZONTAL,
164 } sparrow_axis_t;
167 typedef struct sparrow_calibrate_s {
168 /*calibration state, and shape and pattern definition */
169 gboolean on; /*for calibration pattern */
170 gint wait;
171 guint32 transitions;
172 guint32 incolour;
173 guint32 outcolour;
174 } sparrow_calibrate_t;
176 typedef struct sparrow_find_screen_s {
177 IplImage *green;
178 IplImage *working;
179 IplImage *mask;
180 } sparrow_find_screen_t;
182 /* a mesh of these contains the mapping from input to output.
183 stored in a fixed point notation.
185 #define SPARROW_FIXED_POINT 8
187 typedef struct sparrow_corner_s {
188 int x;
189 int y;
190 int xdelta;
191 int ydelta;
192 /* mark edge pieces? */
193 } sparrow_corner_t;
196 typedef struct _GstSparrow GstSparrow;
197 typedef struct _GstSparrowClass GstSparrowClass;
200 * GstSparrow:
202 * Opaque data structure.
204 struct _GstSparrow
206 GstVideoFilter videofilter;
208 sparrow_format in;
209 sparrow_format out;
210 sparrow_shape_t shapes[MAX_CALIBRATE_SHAPES];
211 int n_shapes;
213 sparrow_calibrate_t calibrate;
215 /*some calibration modes have big unwieldy structs that attach here */
216 void *helper_struct;
218 /* properties */
219 gint calibrate_flag; /*whether to calibrate */
221 /* misc */
222 dsfmt_t *dsfmt; /*rng*/
224 /*state */
225 sparrow_state state;
227 lag_times_t *lag_table;
228 guint64 lag_record;
229 guint32 lag;
231 gint32 countdown; /*intra-state timing*/
233 /*buffer pointers for previous frames */
234 guint8 *in_frame;
235 guint8 *prev_frame;
236 guint8 *work_frame;
237 guint8 *debug_frame;
239 GstBuffer *in_buffer;
240 GstBuffer *prev_buffer;
241 /*don't need work_buffer */
243 IplImage *in_ipl[SPARROW_N_IPL_IN];
245 gboolean debug;
247 guint32 rng_seed;
248 guint32 colour;
249 guint32 frame_count;
250 struct timeval timer_start;
251 struct timeval timer_stop;
252 sparrow_find_screen_t findscreen;
254 guint8 *screenmask;
255 IplImage *screenmask_ipl;
257 gboolean use_timer;
258 FILE * timer_log;
260 sparrow_corner_t *mesh;
263 struct _GstSparrowClass
265 GstVideoFilterClass parent_class;
268 GType gst_sparrow_get_type(void);
271 GST_DEBUG_CATEGORY_EXTERN (sparrow_debug);
272 #define GST_CAT_DEFAULT sparrow_debug
274 /* GstSparrow signals and args */
275 enum
277 /* FILL ME */
278 LAST_SIGNAL
281 enum
283 PROP_0,
284 PROP_CALIBRATE,
285 PROP_DEBUG,
286 PROP_TIMER,
287 PROP_RNG_SEED
290 #define DEFAULT_PROP_CALIBRATE TRUE
291 #define DEFAULT_PROP_DEBUG FALSE
292 #define DEFAULT_PROP_TIMER FALSE
293 #define DEFAULT_PROP_RNG_SEED -1
295 /*timing utility code */
296 #define TIME_TRANSFORM 1
298 #define TIMER_START(sparrow) do{ \
299 if (sparrow->timer_log){ \
300 if ((sparrow)->timer_start.tv_sec){ \
301 GST_DEBUG("timer already running!\n"); \
303 else { \
304 gettimeofday(&((sparrow)->timer_start), NULL); \
307 } while (0)
309 static inline void
310 TIMER_STOP(GstSparrow *sparrow)
312 if (sparrow->timer_log){
313 struct timeval *start = &(sparrow->timer_start);
314 struct timeval *stop = &(sparrow->timer_stop);
315 if (start->tv_sec == 0){
316 GST_DEBUG("the timer isn't running!\n");
317 return;
319 gettimeofday(stop, NULL);
320 guint32 t = ((stop->tv_sec - start->tv_sec) * 1000000 +
321 stop->tv_usec - start->tv_usec);
323 #if SPARROW_NOISY_DEBUG
324 GST_DEBUG("took %u microseconds (%0.5f of a frame)\n",
325 t, (double)t * (25.0 / 1000000.0));
326 #endif
328 fprintf(sparrow->timer_log, "%d %6d\n", sparrow->state, t);
329 fflush(sparrow->timer_log);
330 start->tv_sec = 0; /* mark it as unused */
334 /* GST_DISABLE_GST_DEBUG is set in gstreamer compilation. If it is set, we
335 need our own debug channel. */
336 #ifdef GST_DISABLE_GST_DEBUG
338 #undef GST_DEBUG
340 static FILE *_sparrow_bloody_debug_flags = NULL;
341 static void
342 GST_DEBUG(const char *msg, ...){
343 if (! _sparrow_bloody_debug_flags){
344 _sparrow_bloody_debug_flags = fopen("/tmp/sparrow.log", "wb");
345 if (! _sparrow_bloody_debug_flags){
346 exit(1);
349 va_list argp;
350 va_start(argp, msg);
351 vfprintf(_sparrow_bloody_debug_flags, msg, argp);
352 va_end(argp);
353 fflush(_sparrow_bloody_debug_flags);
356 #define GST_ERROR GST_DEBUG
357 #define GST_WARNING GST_DEBUG
358 #define GST_INFO GST_DEBUG
359 #define GST_LOG GST_DEBUG
360 #define GST_FIXME GST_DEBUG
362 #endif
363 #define LOG_LINENO() GST_DEBUG("%-25s line %4d \n", __func__, __LINE__ );
366 G_END_DECLS
367 #endif /* __GST_VIDEO_SPARROW_H__ */