Commit | Line | Data |
---|---|---|
26b4bb70 PG |
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> | |
47930f09 | 38 | #include <unistd.h> |
26b4bb70 PG |
39 | #include <stdarg.h> |
40 | #include <string.h> | |
b55e4ef4 | 41 | #include <time.h> |
26b4bb70 PG |
42 | #include <stdio.h> |
43 | #include <dirent.h> | |
44 | ||
45 | #include "framehook.h" | |
46 | #include "dsputil.h" | |
47 | ||
25a2ee7d PG |
48 | #define SCALEBITS 10 |
49 | #define ONE_HALF (1 << (SCALEBITS - 1)) | |
50 | #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5)) | |
51 | ||
52 | #define YUV_TO_RGB1_CCIR(cb1, cr1)\ | |
53 | {\ | |
54 | cb = (cb1) - 128;\ | |
55 | cr = (cr1) - 128;\ | |
56 | r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\ | |
57 | g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \ | |
58 | ONE_HALF;\ | |
59 | b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\ | |
60 | } | |
61 | ||
62 | #define YUV_TO_RGB2_CCIR(r, g, b, y1)\ | |
63 | {\ | |
64 | yt = ((y1) - 16) * FIX(255.0/219.0);\ | |
65 | r = cm[(yt + r_add) >> SCALEBITS];\ | |
66 | g = cm[(yt + g_add) >> SCALEBITS];\ | |
67 | b = cm[(yt + b_add) >> SCALEBITS];\ | |
68 | } | |
69 | ||
26b4bb70 | 70 | |
25a2ee7d | 71 | |
26b4bb70 PG |
72 | |
73 | typedef struct { | |
74 | int h; /* 0 .. 360 */ | |
75 | int s; /* 0 .. 255 */ | |
76 | int v; /* 0 .. 255 */ | |
77 | } HSV; | |
78 | ||
79 | typedef struct { | |
80 | int zapping; | |
81 | int threshold; | |
82 | HSV dark, bright; | |
83 | char *dir; | |
84 | int file_limit; | |
85 | int debug; | |
86 | int min_interval; | |
0c1a9eda | 87 | int64_t next_pts; |
26b4bb70 PG |
88 | int inset; |
89 | int min_width; | |
90 | } ContextInfo; | |
91 | ||
92 | static void dorange(const char *s, int *first, int *second, int maxval) | |
93 | { | |
94 | sscanf(s, "%d-%d", first, second); | |
95 | if (*first > maxval) | |
96 | *first = maxval; | |
97 | if (*second > maxval) | |
98 | *second = maxval; | |
99 | } | |
100 | ||
6c11d48c PG |
101 | void Release(void *ctx) |
102 | { | |
103 | if (ctx) | |
104 | av_free(ctx); | |
105 | } | |
26b4bb70 PG |
106 | |
107 | int Configure(void **ctxp, int argc, char *argv[]) | |
108 | { | |
109 | ContextInfo *ci; | |
110 | int c; | |
111 | ||
112 | *ctxp = av_mallocz(sizeof(ContextInfo)); | |
113 | ci = (ContextInfo *) *ctxp; | |
114 | ||
115 | optind = 0; | |
116 | ||
117 | ci->dir = "/tmp"; | |
d9974a48 | 118 | ci->threshold = 100; |
26b4bb70 PG |
119 | ci->file_limit = 100; |
120 | ci->min_interval = 1000000; | |
121 | ci->inset = 10; /* Percent */ | |
122 | ||
123 | while ((c = getopt(argc, argv, "w:i:dh:s:v:zl:t:D:")) > 0) { | |
124 | switch (c) { | |
125 | case 'h': | |
126 | dorange(optarg, &ci->dark.h, &ci->bright.h, 360); | |
127 | break; | |
128 | case 's': | |
129 | dorange(optarg, &ci->dark.s, &ci->bright.s, 255); | |
130 | break; | |
131 | case 'v': | |
132 | dorange(optarg, &ci->dark.v, &ci->bright.v, 255); | |
133 | break; | |
134 | case 'z': | |
135 | ci->zapping = 1; | |
136 | break; | |
137 | case 'l': | |
138 | ci->file_limit = atoi(optarg); | |
139 | break; | |
140 | case 'i': | |
141 | ci->min_interval = 1000000 * atof(optarg); | |
142 | break; | |
143 | case 't': | |
144 | ci->threshold = atof(optarg) * 1000; | |
d9974a48 PG |
145 | if (ci->threshold > 1000 || ci->threshold < 0) { |
146 | fprintf(stderr, "Invalid threshold value '%s' (range is 0-1)\n", optarg); | |
147 | return -1; | |
148 | } | |
26b4bb70 PG |
149 | break; |
150 | case 'w': | |
151 | ci->min_width = atoi(optarg); | |
152 | break; | |
153 | case 'd': | |
154 | ci->debug++; | |
155 | break; | |
156 | case 'D': | |
e9a9e0c2 | 157 | ci->dir = av_strdup(optarg); |
26b4bb70 PG |
158 | break; |
159 | default: | |
160 | fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]); | |
161 | return -1; | |
162 | } | |
163 | } | |
164 | ||
165 | fprintf(stderr, "Fish detector configured:\n"); | |
166 | fprintf(stderr, " HSV range: %d,%d,%d - %d,%d,%d\n", | |
167 | ci->dark.h, | |
168 | ci->dark.s, | |
169 | ci->dark.v, | |
170 | ci->bright.h, | |
171 | ci->bright.s, | |
172 | ci->bright.v); | |
25a2ee7d PG |
173 | fprintf(stderr, " Threshold is %d%% pixels\n", ci->threshold / 10); |
174 | ||
26b4bb70 PG |
175 | |
176 | return 0; | |
177 | } | |
178 | ||
179 | static void get_hsv(HSV *hsv, int r, int g, int b) | |
180 | { | |
181 | int i, v, x, f; | |
182 | ||
183 | x = (r < g) ? r : g; | |
184 | if (b < x) | |
185 | x = b; | |
186 | v = (r > g) ? r : g; | |
187 | if (b > v) | |
188 | v = b; | |
189 | ||
190 | if (v == x) { | |
191 | hsv->h = 0; | |
192 | hsv->s = 0; | |
193 | hsv->v = v; | |
194 | return; | |
195 | } | |
196 | ||
197 | if (r == v) { | |
198 | f = g - b; | |
199 | i = 0; | |
200 | } else if (g == v) { | |
201 | f = b - r; | |
202 | i = 2 * 60; | |
203 | } else { | |
204 | f = r - g; | |
205 | i = 4 * 60; | |
206 | } | |
207 | ||
208 | hsv->h = i + (60 * f) / (v - x); | |
209 | if (hsv->h < 0) | |
210 | hsv->h += 360; | |
211 | ||
212 | hsv->s = (255 * (v - x)) / v; | |
213 | hsv->v = v; | |
214 | ||
215 | return; | |
216 | } | |
217 | ||
0c1a9eda | 218 | void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts) |
26b4bb70 PG |
219 | { |
220 | ContextInfo *ci = (ContextInfo *) ctx; | |
0c1a9eda | 221 | uint8_t *cm = cropTbl + MAX_NEG_CROP; |
26b4bb70 PG |
222 | int rowsize = picture->linesize[0]; |
223 | ||
d9974a48 PG |
224 | #if 0 |
225 | printf("pix_fmt = %d, width = %d, pts = %lld, ci->next_pts = %lld\n", | |
226 | pix_fmt, width, pts, ci->next_pts); | |
227 | #endif | |
228 | ||
26b4bb70 PG |
229 | if (pts < ci->next_pts) |
230 | return; | |
231 | ||
232 | if (width < ci->min_width) | |
233 | return; | |
234 | ||
235 | ci->next_pts = pts + 1000000; | |
236 | ||
237 | if (pix_fmt == PIX_FMT_YUV420P) { | |
0c1a9eda | 238 | uint8_t *y, *u, *v; |
26b4bb70 PG |
239 | int width2 = width >> 1; |
240 | int inrange = 0; | |
241 | int pixcnt; | |
242 | int h; | |
243 | int h_start, h_end; | |
244 | int w_start, w_end; | |
245 | ||
246 | h_end = 2 * ((ci->inset * height) / 200); | |
247 | h_start = height - h_end; | |
248 | ||
249 | w_end = (ci->inset * width2) / 100; | |
250 | w_start = width2 - w_end; | |
251 | ||
252 | pixcnt = ((h_start - h_end) >> 1) * (w_start - w_end); | |
253 | ||
d9974a48 | 254 | y = picture->data[0] + h_end * picture->linesize[0] + w_end * 2; |
25a2ee7d PG |
255 | u = picture->data[1] + h_end * picture->linesize[1] / 2 + w_end; |
256 | v = picture->data[2] + h_end * picture->linesize[2] / 2 + w_end; | |
26b4bb70 PG |
257 | |
258 | for (h = h_start; h > h_end; h -= 2) { | |
259 | int w; | |
260 | ||
261 | for (w = w_start; w > w_end; w--) { | |
25a2ee7d | 262 | unsigned int r,g,b; |
26b4bb70 | 263 | HSV hsv; |
25a2ee7d | 264 | int cb, cr, yt, r_add, g_add, b_add; |
26b4bb70 | 265 | |
25a2ee7d PG |
266 | YUV_TO_RGB1_CCIR(u[0], v[0]); |
267 | YUV_TO_RGB2_CCIR(r, g, b, y[0]); | |
26b4bb70 PG |
268 | |
269 | get_hsv(&hsv, r, g, b); | |
270 | ||
271 | if (ci->debug > 1) | |
272 | fprintf(stderr, "(%d,%d,%d) -> (%d,%d,%d)\n", | |
273 | r,g,b,hsv.h,hsv.s,hsv.v); | |
274 | ||
275 | ||
276 | if (hsv.h >= ci->dark.h && hsv.h <= ci->bright.h && | |
277 | hsv.s >= ci->dark.s && hsv.s <= ci->bright.s && | |
278 | hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) { | |
279 | inrange++; | |
280 | } else if (ci->zapping) { | |
25a2ee7d PG |
281 | y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 16; |
282 | u[0] = 128; | |
283 | v[0] = 128; | |
26b4bb70 PG |
284 | } |
285 | ||
286 | y+= 2; | |
287 | u++; | |
288 | v++; | |
289 | } | |
290 | ||
d9974a48 PG |
291 | y += picture->linesize[0] * 2 - (w_start - w_end) * 2; |
292 | u += picture->linesize[1] - (w_start - w_end); | |
293 | v += picture->linesize[2] - (w_start - w_end); | |
26b4bb70 PG |
294 | } |
295 | ||
d9974a48 PG |
296 | if (ci->debug) |
297 | fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt); | |
298 | ||
26b4bb70 PG |
299 | if (inrange * 1000 / pixcnt >= ci->threshold) { |
300 | /* Save to file */ | |
301 | int size; | |
302 | char *buf; | |
303 | AVPicture picture1; | |
304 | static int frame_counter; | |
305 | static int foundfile; | |
306 | ||
26b4bb70 PG |
307 | if ((frame_counter++ % 20) == 0) { |
308 | /* Check how many files we have */ | |
309 | DIR *d; | |
310 | ||
311 | foundfile = 0; | |
312 | ||
313 | d = opendir(ci->dir); | |
314 | if (d) { | |
315 | struct dirent *dent; | |
316 | ||
317 | while ((dent = readdir(d))) { | |
318 | if (strncmp("fishimg", dent->d_name, 7) == 0) { | |
319 | if (strcmp(".ppm", dent->d_name + strlen(dent->d_name) - 4) == 0) { | |
320 | foundfile++; | |
321 | } | |
322 | } | |
323 | } | |
324 | closedir(d); | |
325 | } | |
326 | } | |
327 | ||
328 | if (foundfile < ci->file_limit) { | |
329 | size = avpicture_get_size(PIX_FMT_RGB24, width, height); | |
330 | buf = av_malloc(size); | |
331 | ||
332 | avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height); | |
333 | if (img_convert(&picture1, PIX_FMT_RGB24, | |
334 | picture, pix_fmt, width, height) >= 0) { | |
335 | /* Write out the PPM file */ | |
336 | ||
337 | FILE *f; | |
338 | char fname[256]; | |
339 | ||
340 | sprintf(fname, "%s/fishimg%ld_%lld.ppm", ci->dir, time(0), pts); | |
341 | f = fopen(fname, "w"); | |
342 | if (f) { | |
343 | fprintf(f, "P6 %d %d 255\n", width, height); | |
344 | fwrite(buf, width * height * 3, 1, f); | |
345 | fclose(f); | |
346 | } | |
347 | } | |
348 | ||
349 | av_free(buf); | |
350 | ci->next_pts = pts + ci->min_interval; | |
351 | } | |
352 | } | |
353 | } | |
354 | } | |
355 |