Change license headers to say 'FFmpeg' instead of 'this program/this library'
[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 file is part of FFmpeg.
23 *
24 * FFmpeg is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2.1 of the License, or (at your option) any later version.
28 *
29 * FFmpeg is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public
35 * License along with FFmpeg; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 */
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <stdarg.h>
42 #include <string.h>
43 #include <time.h>
44 #include <stdio.h>
45 #include <dirent.h>
46
47 #include "framehook.h"
48 #include "dsputil.h"
49 #include "avformat.h"
50 #include "swscale.h"
51
52 static int sws_flags = SWS_BICUBIC;
53
54 #define SCALEBITS 10
55 #define ONE_HALF (1 << (SCALEBITS - 1))
56 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
57
58 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
59 {\
60 cb = (cb1) - 128;\
61 cr = (cr1) - 128;\
62 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
63 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
64 ONE_HALF;\
65 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
66 }
67
68 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
69 {\
70 yt = ((y1) - 16) * FIX(255.0/219.0);\
71 r = cm[(yt + r_add) >> SCALEBITS];\
72 g = cm[(yt + g_add) >> SCALEBITS];\
73 b = cm[(yt + b_add) >> SCALEBITS];\
74 }
75
76
77
78
79 typedef struct {
80 int h; /* 0 .. 360 */
81 int s; /* 0 .. 255 */
82 int v; /* 0 .. 255 */
83 } HSV;
84
85 typedef struct {
86 int zapping;
87 int threshold;
88 HSV dark, bright;
89 char *dir;
90 int file_limit;
91 int debug;
92 int min_interval;
93 int64_t next_pts;
94 int inset;
95 int min_width;
96 struct SwsContext *toRGB_convert_ctx;
97 enum PixelFormat sws_pix_fmt; // Sws_Context is opaque, we need to save
98 int sws_width, sws_height; // this to check if we can re-use contexts
99 } ContextInfo;
100
101 static void dorange(const char *s, int *first, int *second, int maxval)
102 {
103 sscanf(s, "%d-%d", first, second);
104 if (*first > maxval)
105 *first = maxval;
106 if (*second > maxval)
107 *second = maxval;
108 }
109
110 void Release(void *ctx)
111 {
112 ContextInfo *ci;
113 ci = (ContextInfo *) ctx;
114
115 if (ctx) {
116 sws_freeContext(ci->toRGB_convert_ctx);
117 av_free(ctx);
118 }
119 }
120
121 int Configure(void **ctxp, int argc, char *argv[])
122 {
123 ContextInfo *ci;
124 int c;
125
126 *ctxp = av_mallocz(sizeof(ContextInfo));
127 ci = (ContextInfo *) *ctxp;
128
129 optind = 0;
130
131 ci->dir = "/tmp";
132 ci->threshold = 100;
133 ci->file_limit = 100;
134 ci->min_interval = 1000000;
135 ci->inset = 10; /* Percent */
136
137 while ((c = getopt(argc, argv, "w:i:dh:s:v:zl:t:D:")) > 0) {
138 switch (c) {
139 case 'h':
140 dorange(optarg, &ci->dark.h, &ci->bright.h, 360);
141 break;
142 case 's':
143 dorange(optarg, &ci->dark.s, &ci->bright.s, 255);
144 break;
145 case 'v':
146 dorange(optarg, &ci->dark.v, &ci->bright.v, 255);
147 break;
148 case 'z':
149 ci->zapping = 1;
150 break;
151 case 'l':
152 ci->file_limit = atoi(optarg);
153 break;
154 case 'i':
155 ci->min_interval = 1000000 * atof(optarg);
156 break;
157 case 't':
158 ci->threshold = atof(optarg) * 1000;
159 if (ci->threshold > 1000 || ci->threshold < 0) {
160 fprintf(stderr, "Invalid threshold value '%s' (range is 0-1)\n", optarg);
161 return -1;
162 }
163 break;
164 case 'w':
165 ci->min_width = atoi(optarg);
166 break;
167 case 'd':
168 ci->debug++;
169 break;
170 case 'D':
171 ci->dir = av_strdup(optarg);
172 break;
173 default:
174 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
175 return -1;
176 }
177 }
178
179 fprintf(stderr, "Fish detector configured:\n");
180 fprintf(stderr, " HSV range: %d,%d,%d - %d,%d,%d\n",
181 ci->dark.h,
182 ci->dark.s,
183 ci->dark.v,
184 ci->bright.h,
185 ci->bright.s,
186 ci->bright.v);
187 fprintf(stderr, " Threshold is %d%% pixels\n", ci->threshold / 10);
188
189
190 return 0;
191 }
192
193 static void get_hsv(HSV *hsv, int r, int g, int b)
194 {
195 int i, v, x, f;
196
197 x = (r < g) ? r : g;
198 if (b < x)
199 x = b;
200 v = (r > g) ? r : g;
201 if (b > v)
202 v = b;
203
204 if (v == x) {
205 hsv->h = 0;
206 hsv->s = 0;
207 hsv->v = v;
208 return;
209 }
210
211 if (r == v) {
212 f = g - b;
213 i = 0;
214 } else if (g == v) {
215 f = b - r;
216 i = 2 * 60;
217 } else {
218 f = r - g;
219 i = 4 * 60;
220 }
221
222 hsv->h = i + (60 * f) / (v - x);
223 if (hsv->h < 0)
224 hsv->h += 360;
225
226 hsv->s = (255 * (v - x)) / v;
227 hsv->v = v;
228
229 return;
230 }
231
232 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
233 {
234 ContextInfo *ci = (ContextInfo *) ctx;
235 uint8_t *cm = cropTbl + MAX_NEG_CROP;
236 int rowsize = picture->linesize[0];
237
238 #if 0
239 printf("pix_fmt = %d, width = %d, pts = %lld, ci->next_pts = %lld\n",
240 pix_fmt, width, pts, ci->next_pts);
241 #endif
242
243 if (pts < ci->next_pts)
244 return;
245
246 if (width < ci->min_width)
247 return;
248
249 ci->next_pts = pts + 1000000;
250
251 if (pix_fmt == PIX_FMT_YUV420P) {
252 uint8_t *y, *u, *v;
253 int width2 = width >> 1;
254 int inrange = 0;
255 int pixcnt;
256 int h;
257 int h_start, h_end;
258 int w_start, w_end;
259
260 h_end = 2 * ((ci->inset * height) / 200);
261 h_start = height - h_end;
262
263 w_end = (ci->inset * width2) / 100;
264 w_start = width2 - w_end;
265
266 pixcnt = ((h_start - h_end) >> 1) * (w_start - w_end);
267
268 y = picture->data[0] + h_end * picture->linesize[0] + w_end * 2;
269 u = picture->data[1] + h_end * picture->linesize[1] / 2 + w_end;
270 v = picture->data[2] + h_end * picture->linesize[2] / 2 + w_end;
271
272 for (h = h_start; h > h_end; h -= 2) {
273 int w;
274
275 for (w = w_start; w > w_end; w--) {
276 unsigned int r,g,b;
277 HSV hsv;
278 int cb, cr, yt, r_add, g_add, b_add;
279
280 YUV_TO_RGB1_CCIR(u[0], v[0]);
281 YUV_TO_RGB2_CCIR(r, g, b, y[0]);
282
283 get_hsv(&hsv, r, g, b);
284
285 if (ci->debug > 1)
286 fprintf(stderr, "(%d,%d,%d) -> (%d,%d,%d)\n",
287 r,g,b,hsv.h,hsv.s,hsv.v);
288
289
290 if (hsv.h >= ci->dark.h && hsv.h <= ci->bright.h &&
291 hsv.s >= ci->dark.s && hsv.s <= ci->bright.s &&
292 hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {
293 inrange++;
294 } else if (ci->zapping) {
295 y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 16;
296 u[0] = 128;
297 v[0] = 128;
298 }
299
300 y+= 2;
301 u++;
302 v++;
303 }
304
305 y += picture->linesize[0] * 2 - (w_start - w_end) * 2;
306 u += picture->linesize[1] - (w_start - w_end);
307 v += picture->linesize[2] - (w_start - w_end);
308 }
309
310 if (ci->debug)
311 fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
312
313 if (inrange * 1000 / pixcnt >= ci->threshold) {
314 /* Save to file */
315 int size;
316 char *buf;
317 AVPicture picture1;
318 static int frame_counter;
319 static int foundfile;
320
321 if ((frame_counter++ % 20) == 0) {
322 /* Check how many files we have */
323 DIR *d;
324
325 foundfile = 0;
326
327 d = opendir(ci->dir);
328 if (d) {
329 struct dirent *dent;
330
331 while ((dent = readdir(d))) {
332 if (strncmp("fishimg", dent->d_name, 7) == 0) {
333 if (strcmp(".ppm", dent->d_name + strlen(dent->d_name) - 4) == 0) {
334 foundfile++;
335 }
336 }
337 }
338 closedir(d);
339 }
340 }
341
342 if (foundfile < ci->file_limit) {
343 size = avpicture_get_size(PIX_FMT_RGB24, width, height);
344 buf = av_malloc(size);
345
346 avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
347
348 // if we already got a SWS context, let's realloc if is not re-useable
349 if (ci->toRGB_convert_ctx != NULL) {
350 if ((ci->sws_pix_fmt != pix_fmt) ||
351 (ci->sws_width != width) || (ci->sws_height != height)) {
352 sws_freeContext(ci->toRGB_convert_ctx);
353 ci->toRGB_convert_ctx = NULL;
354 }
355 }
356 if (ci->toRGB_convert_ctx == NULL) {
357 ci->sws_pix_fmt = pix_fmt;
358 ci->sws_width = width;
359 ci->sws_height = height;
360 ci->toRGB_convert_ctx = sws_getContext(
361 ci->sws_width, ci->sws_height,
362 ci->sws_pix_fmt,
363 ci->sws_width, ci->sws_height,
364 PIX_FMT_RGB24,
365 sws_flags, NULL, NULL, NULL);
366 if (ci->toRGB_convert_ctx == NULL) {
367 av_log(NULL, AV_LOG_ERROR,
368 "Cannot initialize the toRGB conversion context\n");
369 exit(1);
370 }
371 }
372 // img_convert parameters are 2 first destination, then 4 source
373 // sws_scale parameters are context, 4 first source, then 2 destination
374 sws_scale(ci->toRGB_convert_ctx,
375 picture->data, picture->linesize, 0, ci->sws_height,
376 picture1.data, picture1.linesize);
377
378 /* Write out the PPM file */
379
380 FILE *f;
381 char fname[256];
382
383 snprintf(fname, sizeof(fname), "%s/fishimg%ld_%"PRId64".ppm", ci->dir, (long)(av_gettime() / 1000000), pts);
384 f = fopen(fname, "w");
385 if (f) {
386 fprintf(f, "P6 %d %d 255\n", width, height);
387 fwrite(buf, width * height * 3, 1, f);
388 fclose(f);
389 }
390
391 av_free(buf);
392 ci->next_pts = pts + ci->min_interval;
393 }
394 }
395 }
396 }
397