File: motion.h

package info (click to toggle)
motion 3.2.3-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,612 kB
  • ctags: 1,330
  • sloc: ansic: 11,669; sh: 2,906; makefile: 198
file content (319 lines) | stat: -rw-r--r-- 9,846 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*	motion.h
 *
 *	Include file for motion.c
 *      Copyright 2000 by Jeroen Vreeken (pe1rxq@amsat.org)
 *      This software is distributed under the GNU public license version 2
 *      See also the file 'COPYING'.
 *
 */

#ifndef _INCLUDE_MOTION_H
#define _INCLUDE_MOTION_H

/* Includes */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#define __USE_GNU
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <syslog.h>
#include <limits.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>

#define _LINUX_TIME_H 1
#if (!defined(WITHOUT_V4L)) && (!defined (__freebsd__))
#include <linux/videodev.h>
#endif

#include <pthread.h>

#ifdef HAVE_MYSQL
#include <mysql.h>
#endif

#ifdef HAVE_PGSQL
#include <libpq-fe.h>
#endif

#include "conf.h"
#include "webcam.h"
#include "webhttpd.h"

/* The macro below defines a version of sleep using nanosleep
 * If a signal such as SIG_CHLD interrupts the sleep we just continue sleeping
 */
#define SLEEP(seconds, nanoseconds) {              \
                struct timespec tv;                \
                tv.tv_sec = (seconds);             \
                tv.tv_nsec = (nanoseconds);        \
                while (nanosleep(&tv, &tv) == -1); \
        } 


#if defined(WITHOUT_V4L) || defined(__freebsd__)
 
#define VIDEO_PALETTE_GREY      1       /* Linear greyscale */
#define VIDEO_PALETTE_HI240     2       /* High 240 cube (BT848) */
#define VIDEO_PALETTE_RGB565    3       /* 565 16 bit RGB */
#define VIDEO_PALETTE_RGB24     4       /* 24bit RGB */
#define VIDEO_PALETTE_RGB32     5       /* 32bit RGB */
#define VIDEO_PALETTE_RGB555    6       /* 555 15bit RGB */
#define VIDEO_PALETTE_YUV422    7       /* YUV422 capture */
#define VIDEO_PALETTE_YUYV      8
#define VIDEO_PALETTE_UYVY      9       /* The great thing about standards is ... */
#define VIDEO_PALETTE_YUV420    10
#define VIDEO_PALETTE_YUV411    11      /* YUV411 capture */
#define VIDEO_PALETTE_RAW       12      /* RAW capture (BT848) */
#define VIDEO_PALETTE_YUV422P   13      /* YUV 4:2:2 Planar */
#define VIDEO_PALETTE_YUV411P   14      /* YUV 4:1:1 Planar */
#define VIDEO_PALETTE_YUV420P   15      /* YUV 4:2:0 Planar */
#define VIDEO_PALETTE_YUV410P   16      /* YUV 4:1:0 Planar */
#define VIDEO_PALETTE_PLANAR    13      /* start of planar entries */
#define VIDEO_PALETTE_COMPONENT 7       /* start of component entries */
#endif

/* Default picture settings */
#define DEF_WIDTH              352
#define DEF_HEIGHT             288
#define DEF_QUALITY             75
#define DEF_CHANGES           1500

#define DEF_MAXFRAMERATE       100
#define DEF_NOISELEVEL          32

/* Minimum time between two 'actions' (email, sms, external) */
#define DEF_GAP                 60  /* 1 minutes */
#define DEF_MAXMPEGTIME       3600  /* 60 minutes */

#define DEF_FFMPEG_BPS      400000
#define DEF_FFMPEG_VBR           0
#define DEF_FFMPEG_CODEC   "mpeg4"

#define THRESHOLD_TUNE_LENGTH  256

#define MISSING_FRAMES_TIMEOUT  30  /* When failing to get picture frame from camera
                                     * we reuse the previous frame until
                                     * MISSING_FRAMES_TIMEOUT seconds has passed
                                     * and then we show a grey image instead
                                     */

#define DEF_MAXSTREAMS          10  /* Maximum number of webcam clients per camera */
#define DEF_MAXWEBQUEUE         10  /* Maximum number of webcam client in queue */

#define DEF_TIMESTAMP      "%Y-%m-%d\\n%T"

#define DEF_SNAPPATH       "%v-%Y%m%d%H%M%S-snapshot"
#define DEF_JPEGPATH       "%v-%Y%m%d%H%M%S-%q"
#define DEF_MPEGPATH       "%v-%Y%m%d%H%M%S"
#define DEF_TIMEPATH       "%Y%m%d-timelapse"

#define DEF_TIMELAPSE_MODE "daily"

/* Filetype defines */
#define FTYPE_IMAGE            1
#define FTYPE_IMAGE_SNAPSHOT   2
#define FTYPE_IMAGE_MOTION     4
#define FTYPE_MPEG             8
#define FTYPE_MPEG_MOTION     16
#define FTYPE_MPEG_TIMELAPSE  32

#define FTYPE_MPEG_ANY    (FTYPE_MPEG | FTYPE_MPEG_MOTION | FTYPE_MPEG_TIMELAPSE)
#define FTYPE_IMAGE_ANY   (FTYPE_IMAGE | FTYPE_IMAGE_SNAPSHOT | FTYPE_IMAGE_MOTION)

#define NEWIMG_OFF       0
#define NEWIMG_ON        1
#define NEWIMG_FIRST     2
#define NEWIMG_BEST      3

#define LOCATE_OFF       0
#define LOCATE_ON        1
#define LOCATE_PREVIEW   2

#define LOCATE_NORMAL    0
#define LOCATE_BOTH      1

/* DIFFERENCES BETWEEN imgs.width, conf.width AND rotate_data.cap_width
 * (and the corresponding height values, of course)
 * ===========================================================================
 * Location      Purpose
 * 
 * conf          The values in conf reflect width and height set in the
 *               configuration file. These can be set via xmlrpc, but they are
 *               not used internally by Motion, so it won't break anything.
 *               These values are transferred to imgs in vid_start.
 *
 * imgs          The values in imgs are the actual output dimensions. Normally
 *               the output dimensions are the same as the capture dimensions,
 *               but for 90 or 270 degrees rotation, they are not. E.g., if 
 *               you capture at 320x240, and rotate 90 degrees, the output
 *               dimensions are 240x320.
 *               These values are set from the conf values in vid_start, or 
 *               from the first JPEG image in netcam_start. For 90 or 270 
 *               degrees rotation, they are swapped in rotate_init.
 *
 * rotate_data   The values in rotate_data are named cap_width and cap_height,
 *               and contain the capture dimensions. The difference between
 *               capture and output dimensions is explained above.
 *               These values are set in rotate_init.
 */

/* date/time drawing, draw.c */
int draw_text (unsigned char *image, int startx, int starty, int width, int height, char *text, int factor);
int initialize_chars(void);

struct images {
	unsigned char *image_ring_buffer; /* The base address of the image ring buffer */
	unsigned char *ref;               /* The reference frame */
	unsigned char *out;               /* Picture buffer for motion images */
	unsigned char *image_virgin;      /* Last picture frame with no text or locate overlay */
	unsigned char *preview_buffer;    /* Picture buffer for best image when enables */
	unsigned char *mask;              /* Buffer for the mask file */
	unsigned char *smartmask;
	unsigned char *smartmask_final;
	unsigned char *common_buffer;
	int *smartmask_buffer;
	int *labels;
	int *labelsize;
	int width;
	int height;
	int type;
	int size;
	int motionsize;
	int total_labels;
	int labelsize_max;
	int labelgroup_max;
	int labels_above;
	int largest_label;
	time_t *timestamp;
	int *shotstamp;
};

/* Contains data for image rotation, see rotate.c. */
struct rotdata {
	/* Temporary buffer for 90 and 270 degrees rotation. */
	unsigned char *temp_buf;
	/* Pointer to a rotate-180-degrees function. */
	void (*rotate_180)(unsigned char *, int);
	/* Degrees to rotate; copied from conf.rotate_deg. This is the value
	 * that is actually used. The value of conf.rotate_deg cannot be used
	 * because it can be changed by motion-control, and changing rotation
	 * while Motion is running just causes problems.
	 */
	int degrees;
	/* Capture width and height - different from output width and height if 
	 * rotating 90 or 270 degrees. */
	int cap_width;
	int cap_height;
};

#include "track.h"

#include "netcam.h"



/*
	these used to be global variables but now each thread will have its
	own context
 */
struct context {
	char conf_filename[PATH_MAX];
	int threadnr;
	int daemon;

	struct config conf;
	struct images imgs;
	struct trackoptions track;
	struct netcam_context *netcam;
	int precap_nr;
	int precap_cur;
	int new_img;
	int preview_shot;
	int preview_max;            /* Stores max diff number seen in an event of output_normal==best */
	int locate;
	struct coord location;      /* coordinates for center and size of last motion detection*/
	struct rotdata rotate_data; /* rotation data is thread-specific */

	int diffs;
	int noise;
	int threshold;
	int diffs_last[THRESHOLD_TUNE_LENGTH];
	int smartmask_speed;

	int snapshot;
	int makemovie;
	int finish;

	int event_nr;
	int prev_event;

	int shots;
	struct tm *currenttime;

	time_t currenttimep;
	time_t lasttime;
	time_t eventtime;
	time_t lastshot;
	time_t connectionlosttime;   /* timestamp from connection lost */

	int lastrate;
	int moved;
	int switched;
	int pause;
	int missing_frame_counter;   /* counts failed attempts to fetch picture frame from camera */

#ifdef __freebsd__
	int tuner_dev;
#endif
	int video_dev;
	int pipe;
	int mpipe;

	struct webcam webcam;
	int stream_count;
	
#if defined(HAVE_MYSQL) || defined(HAVE_PGSQL)
	int sql_mask;
#endif

#ifdef HAVE_MYSQL
	MYSQL *database;
#endif

#ifdef HAVE_PGSQL
	PGconn *database_pg;
#endif

#ifdef HAVE_FFMPEG
	struct ffmpeg *ffmpeg_new;
	struct ffmpeg *ffmpeg_motion;
	struct ffmpeg *ffmpeg_timelapse;
	struct ffmpeg *ffmpeg_smartmask;
	char newfilename[255];
	char motionfilename[255];
	char timelapsefilename[255];
#endif
};

extern pthread_mutex_t global_lock;
extern volatile int threads_running;
extern int debug_level;

int http_bindsock(struct context *cnt, int, int);
void * mymalloc(struct context *cnt, size_t size);
void * myrealloc(struct context *cnt, void *ptr, size_t size, const char *desc);
FILE * myfopen(struct context *cnt, char *path, char *mode);
size_t mystrftime(struct context *cnt, char *s, size_t max, char *userformat,
                  const struct tm *tm);
int create_path(struct context *cnt, char *);
void motion_log (struct context *, int, int, const char *, ...);
#endif /* _INCLUDE_MOTION_H */