Made it work again -- it now detects fish! (This involved clearing out
[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 <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_t 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 void Release(void *ctx)
86 {
87 if (ctx)
88 av_free(ctx);
89 }
90
91 int Configure(void **ctxp, int argc, char *argv[])
92 {
93 ContextInfo *ci;
94 int c;
95
96 *ctxp = av_mallocz(sizeof(ContextInfo));
97 ci = (ContextInfo *) *ctxp;
98
99 optind = 0;
100
101 ci->dir = "/tmp";
102 ci->threshold = 100;
103 ci->file_limit = 100;
104 ci->min_interval = 1000000;
105 ci->inset = 10; /* Percent */
106
107 while ((c = getopt(argc, argv, "w:i:dh:s:v:zl:t:D:")) > 0) {
108 switch (c) {
109 case 'h':
110 dorange(optarg, &ci->dark.h, &ci->bright.h, 360);
111 break;
112 case 's':
113 dorange(optarg, &ci->dark.s, &ci->bright.s, 255);
114 break;
115 case 'v':
116 dorange(optarg, &ci->dark.v, &ci->bright.v, 255);
117 break;
118 case 'z':
119 ci->zapping = 1;
120 break;
121 case 'l':
122 ci->file_limit = atoi(optarg);
123 break;
124 case 'i':
125 ci->min_interval = 1000000 * atof(optarg);
126 break;
127 case 't':
128 ci->threshold = atof(optarg) * 1000;
129 if (ci->threshold > 1000 || ci->threshold < 0) {
130 fprintf(stderr, "Invalid threshold value '%s' (range is 0-1)\n", optarg);
131 return -1;
132 }
133 break;
134 case 'w':
135 ci->min_width = atoi(optarg);
136 break;
137 case 'd':
138 ci->debug++;
139 break;
140 case 'D':
141 ci->dir = av_strdup(optarg);
142 break;
143 default:
144 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
145 return -1;
146 }
147 }
148
149 fprintf(stderr, "Fish detector configured:\n");
150 fprintf(stderr, " HSV range: %d,%d,%d - %d,%d,%d\n",
151 ci->dark.h,
152 ci->dark.s,
153 ci->dark.v,
154 ci->bright.h,
155 ci->bright.s,
156 ci->bright.v);
157
158 return 0;
159 }
160
161 static void get_hsv(HSV *hsv, int r, int g, int b)
162 {
163 int i, v, x, f;
164
165 x = (r < g) ? r : g;
166 if (b < x)
167 x = b;
168 v = (r > g) ? r : g;
169 if (b > v)
170 v = b;
171
172 if (v == x) {
173 hsv->h = 0;
174 hsv->s = 0;
175 hsv->v = v;
176 return;
177 }
178
179 if (r == v) {
180 f = g - b;
181 i = 0;
182 } else if (g == v) {
183 f = b - r;
184 i = 2 * 60;
185 } else {
186 f = r - g;
187 i = 4 * 60;
188 }
189
190 hsv->h = i + (60 * f) / (v - x);
191 if (hsv->h < 0)
192 hsv->h += 360;
193
194 hsv->s = (255 * (v - x)) / v;
195 hsv->v = v;
196
197 return;
198 }
199
200 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
201 {
202 ContextInfo *ci = (ContextInfo *) ctx;
203 uint8_t *cm = cropTbl + MAX_NEG_CROP;
204 int rowsize = picture->linesize[0];
205
206 #if 0
207 printf("pix_fmt = %d, width = %d, pts = %lld, ci->next_pts = %lld\n",
208 pix_fmt, width, pts, ci->next_pts);
209 #endif
210
211 if (pts < ci->next_pts)
212 return;
213
214 if (width < ci->min_width)
215 return;
216
217 ci->next_pts = pts + 1000000;
218
219 if (pix_fmt == PIX_FMT_YUV420P) {
220 uint8_t *y, *u, *v;
221 int width2 = width >> 1;
222 int inrange = 0;
223 int pixcnt;
224 int h;
225 int h_start, h_end;
226 int w_start, w_end;
227
228 h_end = 2 * ((ci->inset * height) / 200);
229 h_start = height - h_end;
230
231 w_end = (ci->inset * width2) / 100;
232 w_start = width2 - w_end;
233
234 pixcnt = ((h_start - h_end) >> 1) * (w_start - w_end);
235
236 y = picture->data[0] + h_end * picture->linesize[0] + w_end * 2;
237 u = picture->data[1] + h_end * picture->linesize[1] + w_end;
238 v = picture->data[2] + h_end * picture->linesize[2] + w_end;
239
240 for (h = h_start; h > h_end; h -= 2) {
241 int w;
242
243 for (w = w_start; w > w_end; w--) {
244 int r,g,b;
245 int Y, U, V;
246 HSV hsv;
247
248 U = u[0] - 128;
249 V = v[0] - 128;
250
251 Y = (y[0] - 16) * C_Y;
252
253 r = cm[(Y + C_RV * V + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
254 g = cm[(Y + - C_GU * U - C_GV * V + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
255 b = cm[(Y + C_BU * U + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
256
257 get_hsv(&hsv, r, g, b);
258
259 if (ci->debug > 1)
260 fprintf(stderr, "(%d,%d,%d) -> (%d,%d,%d)\n",
261 r,g,b,hsv.h,hsv.s,hsv.v);
262
263
264 if (hsv.h >= ci->dark.h && hsv.h <= ci->bright.h &&
265 hsv.s >= ci->dark.s && hsv.s <= ci->bright.s &&
266 hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {
267 inrange++;
268 } else if (ci->zapping) {
269 y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 0;
270 }
271
272 y+= 2;
273 u++;
274 v++;
275 }
276
277 y += picture->linesize[0] * 2 - (w_start - w_end) * 2;
278 u += picture->linesize[1] - (w_start - w_end);
279 v += picture->linesize[2] - (w_start - w_end);
280 }
281
282 if (ci->debug)
283 fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
284
285 if (inrange * 1000 / pixcnt >= ci->threshold) {
286 /* Save to file */
287 int size;
288 char *buf;
289 AVPicture picture1;
290 static int frame_counter;
291 static int foundfile;
292
293 if ((frame_counter++ % 20) == 0) {
294 /* Check how many files we have */
295 DIR *d;
296
297 foundfile = 0;
298
299 d = opendir(ci->dir);
300 if (d) {
301 struct dirent *dent;
302
303 while ((dent = readdir(d))) {
304 if (strncmp("fishimg", dent->d_name, 7) == 0) {
305 if (strcmp(".ppm", dent->d_name + strlen(dent->d_name) - 4) == 0) {
306 foundfile++;
307 }
308 }
309 }
310 closedir(d);
311 }
312 }
313
314 if (foundfile < ci->file_limit) {
315 size = avpicture_get_size(PIX_FMT_RGB24, width, height);
316 buf = av_malloc(size);
317
318 avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
319 if (img_convert(&picture1, PIX_FMT_RGB24,
320 picture, pix_fmt, width, height) >= 0) {
321 /* Write out the PPM file */
322
323 FILE *f;
324 char fname[256];
325
326 sprintf(fname, "%s/fishimg%ld_%lld.ppm", ci->dir, time(0), pts);
327 f = fopen(fname, "w");
328 if (f) {
329 fprintf(f, "P6 %d %d 255\n", width, height);
330 fwrite(buf, width * height * 3, 1, f);
331 fclose(f);
332 }
333 }
334
335 av_free(buf);
336 ci->next_pts = pts + ci->min_interval;
337 }
338 }
339 }
340 }
341