Applied changes to make it compile on darwin
[libav.git] / vhook / fish.c
1 /*
2 * Fish Detector Hook
3 * Copyright (c) 2002 Philip Gladstone
4 *
5 * This file implements a fish detector. It is used to see when a
6 * goldfish passes in front of the camera. It does this by counting
7 * the number of input pixels that fall within a particular HSV
8 * range.
9 *
10 * It takes a multitude of arguments:
11 *
12 * -h <num>-<num> the range of H values that are fish
13 * -s <num>-<num> the range of S values that are fish
14 * -v <num>-<num> the range of V values that are fish
15 * -z zap all non-fish values to black
16 * -l <num> limit the number of saved files to <num>
17 * -i <num> only check frames every <num> seconds
18 * -t <num> the threshold for the amount of fish pixels (range 0-1)
19 * -d turn debugging on
20 * -D <directory> where to put the fish images
21 *
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License as published by the Free Software Foundation; either
25 * version 2 of the License, or (at your option) any later version.
26 *
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * Lesser General Public License for more details.
31 *
32 * You should have received a copy of the GNU Lesser General Public
33 * License along with this library; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 */
36 #include <stdlib.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <sys/time.h>
42 #include <stdio.h>
43 #include <dirent.h>
44
45 #include "framehook.h"
46 #include "dsputil.h"
47
48 #define SCALE_BITS 10
49
50 #define C_Y (76309 >> (16 - SCALE_BITS))
51 #define C_RV (117504 >> (16 - SCALE_BITS))
52 #define C_BU (138453 >> (16 - SCALE_BITS))
53 #define C_GU (13954 >> (16 - SCALE_BITS))
54 #define C_GV (34903 >> (16 - SCALE_BITS))
55
56
57 typedef struct {
58 int h; /* 0 .. 360 */
59 int s; /* 0 .. 255 */
60 int v; /* 0 .. 255 */
61 } HSV;
62
63 typedef struct {
64 int zapping;
65 int threshold;
66 HSV dark, bright;
67 char *dir;
68 int file_limit;
69 int debug;
70 int min_interval;
71 INT64 next_pts;
72 int inset;
73 int min_width;
74 } ContextInfo;
75
76 static void dorange(const char *s, int *first, int *second, int maxval)
77 {
78 sscanf(s, "%d-%d", first, second);
79 if (*first > maxval)
80 *first = maxval;
81 if (*second > maxval)
82 *second = maxval;
83 }
84
85
86 int Configure(void **ctxp, int argc, char *argv[])
87 {
88 ContextInfo *ci;
89 int c;
90
91 *ctxp = av_mallocz(sizeof(ContextInfo));
92 ci = (ContextInfo *) *ctxp;
93
94 optind = 0;
95
96 ci->dir = "/tmp";
97 ci->threshold = 1000;
98 ci->file_limit = 100;
99 ci->min_interval = 1000000;
100 ci->inset = 10; /* Percent */
101
102 while ((c = getopt(argc, argv, "w:i:dh:s:v:zl:t:D:")) > 0) {
103 switch (c) {
104 case 'h':
105 dorange(optarg, &ci->dark.h, &ci->bright.h, 360);
106 break;
107 case 's':
108 dorange(optarg, &ci->dark.s, &ci->bright.s, 255);
109 break;
110 case 'v':
111 dorange(optarg, &ci->dark.v, &ci->bright.v, 255);
112 break;
113 case 'z':
114 ci->zapping = 1;
115 break;
116 case 'l':
117 ci->file_limit = atoi(optarg);
118 break;
119 case 'i':
120 ci->min_interval = 1000000 * atof(optarg);
121 break;
122 case 't':
123 ci->threshold = atof(optarg) * 1000;
124 break;
125 case 'w':
126 ci->min_width = atoi(optarg);
127 break;
128 case 'd':
129 ci->debug++;
130 break;
131 case 'D':
132 ci->dir = strdup(optarg);
133 break;
134 default:
135 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
136 return -1;
137 }
138 }
139
140 fprintf(stderr, "Fish detector configured:\n");
141 fprintf(stderr, " HSV range: %d,%d,%d - %d,%d,%d\n",
142 ci->dark.h,
143 ci->dark.s,
144 ci->dark.v,
145 ci->bright.h,
146 ci->bright.s,
147 ci->bright.v);
148
149 return 0;
150 }
151
152 static void get_hsv(HSV *hsv, int r, int g, int b)
153 {
154 int i, v, x, f;
155
156 x = (r < g) ? r : g;
157 if (b < x)
158 x = b;
159 v = (r > g) ? r : g;
160 if (b > v)
161 v = b;
162
163 if (v == x) {
164 hsv->h = 0;
165 hsv->s = 0;
166 hsv->v = v;
167 return;
168 }
169
170 if (r == v) {
171 f = g - b;
172 i = 0;
173 } else if (g == v) {
174 f = b - r;
175 i = 2 * 60;
176 } else {
177 f = r - g;
178 i = 4 * 60;
179 }
180
181 hsv->h = i + (60 * f) / (v - x);
182 if (hsv->h < 0)
183 hsv->h += 360;
184
185 hsv->s = (255 * (v - x)) / v;
186 hsv->v = v;
187
188 return;
189 }
190
191 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, INT64 pts)
192 {
193 ContextInfo *ci = (ContextInfo *) ctx;
194 UINT8 *cm = cropTbl + MAX_NEG_CROP;
195 int rowsize = picture->linesize[0];
196
197 if (pts < ci->next_pts)
198 return;
199
200 if (width < ci->min_width)
201 return;
202
203 ci->next_pts = pts + 1000000;
204
205 if (pix_fmt == PIX_FMT_YUV420P) {
206 UINT8 *y, *u, *v;
207 int width2 = width >> 1;
208 int inrange = 0;
209 int pixcnt;
210 int h;
211 int h_start, h_end;
212 int w_start, w_end;
213
214 h_end = 2 * ((ci->inset * height) / 200);
215 h_start = height - h_end;
216
217 w_end = (ci->inset * width2) / 100;
218 w_start = width2 - w_end;
219
220 pixcnt = ((h_start - h_end) >> 1) * (w_start - w_end);
221
222 y = picture->data[0];
223 u = picture->data[1];
224 v = picture->data[2];
225
226 for (h = h_start; h > h_end; h -= 2) {
227 int w;
228
229 for (w = w_start; w > w_end; w--) {
230 int r,g,b;
231 int Y, U, V;
232 HSV hsv;
233
234 U = u[0] - 128;
235 V = v[0] - 128;
236
237 Y = (y[0] - 16) * C_Y;
238
239 r = cm[(Y + C_RV * V + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
240 g = cm[(Y + - C_GU * U - C_GV * V + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
241 b = cm[(Y + C_BU * U + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
242
243 get_hsv(&hsv, r, g, b);
244
245 if (ci->debug > 1)
246 fprintf(stderr, "(%d,%d,%d) -> (%d,%d,%d)\n",
247 r,g,b,hsv.h,hsv.s,hsv.v);
248
249
250 if (hsv.h >= ci->dark.h && hsv.h <= ci->bright.h &&
251 hsv.s >= ci->dark.s && hsv.s <= ci->bright.s &&
252 hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {
253 inrange++;
254 } else if (ci->zapping) {
255 y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 0;
256 }
257
258 y+= 2;
259 u++;
260 v++;
261 }
262
263 y += picture->linesize[0] * 2 - width;
264 u += picture->linesize[1] - width2;
265 v += picture->linesize[2] - width2;
266 }
267
268 if (inrange * 1000 / pixcnt >= ci->threshold) {
269 /* Save to file */
270 int size;
271 char *buf;
272 AVPicture picture1;
273 static int frame_counter;
274 static int foundfile;
275
276 if (ci->debug)
277 fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
278
279 if ((frame_counter++ % 20) == 0) {
280 /* Check how many files we have */
281 DIR *d;
282
283 foundfile = 0;
284
285 d = opendir(ci->dir);
286 if (d) {
287 struct dirent *dent;
288
289 while ((dent = readdir(d))) {
290 if (strncmp("fishimg", dent->d_name, 7) == 0) {
291 if (strcmp(".ppm", dent->d_name + strlen(dent->d_name) - 4) == 0) {
292 foundfile++;
293 }
294 }
295 }
296 closedir(d);
297 }
298 }
299
300 if (foundfile < ci->file_limit) {
301 size = avpicture_get_size(PIX_FMT_RGB24, width, height);
302 buf = av_malloc(size);
303
304 avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
305 if (img_convert(&picture1, PIX_FMT_RGB24,
306 picture, pix_fmt, width, height) >= 0) {
307 /* Write out the PPM file */
308
309 FILE *f;
310 char fname[256];
311
312 sprintf(fname, "%s/fishimg%ld_%lld.ppm", ci->dir, time(0), pts);
313 f = fopen(fname, "w");
314 if (f) {
315 fprintf(f, "P6 %d %d 255\n", width, height);
316 fwrite(buf, width * height * 3, 1, f);
317 fclose(f);
318 }
319 }
320
321 av_free(buf);
322 ci->next_pts = pts + ci->min_interval;
323 }
324 }
325 }
326 }
327
328 /* To ensure correct typing */
329 FrameHookConfigureFn ConfigureFn = Configure;
330 FrameHookProcessFn ProcessFn = Process;