Replace "signed short" typecast with "int16_t" as suggested by
[FFMpeg-mirror/DVCPRO-HD.git] / vhook / fish.c
blob1571cf9b95a13b9767d30b373cd8eee53e020c70
1 /*
2 * Fish Detector Hook
3 * Copyright (c) 2002 Philip Gladstone
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.
10 * It takes a multitude of arguments:
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
22 * This file is part of FFmpeg.
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.
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.
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
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>
47 #include "framehook.h"
48 #include "dsputil.h"
49 #include "avformat.h"
50 #include "swscale.h"
52 static int sws_flags = SWS_BICUBIC;
54 #define SCALEBITS 10
55 #define ONE_HALF (1 << (SCALEBITS - 1))
56 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
58 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
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;\
68 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
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];\
79 typedef struct {
80 int h; /* 0 .. 360 */
81 int s; /* 0 .. 255 */
82 int v; /* 0 .. 255 */
83 } HSV;
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 } ContextInfo;
99 static void dorange(const char *s, int *first, int *second, int maxval)
101 sscanf(s, "%d-%d", first, second);
102 if (*first > maxval)
103 *first = maxval;
104 if (*second > maxval)
105 *second = maxval;
108 void Release(void *ctx)
110 ContextInfo *ci;
111 ci = (ContextInfo *) ctx;
113 if (ctx) {
114 sws_freeContext(ci->toRGB_convert_ctx);
115 av_free(ctx);
119 int Configure(void **ctxp, int argc, char *argv[])
121 ContextInfo *ci;
122 int c;
124 *ctxp = av_mallocz(sizeof(ContextInfo));
125 ci = (ContextInfo *) *ctxp;
127 optind = 0;
129 ci->dir = "/tmp";
130 ci->threshold = 100;
131 ci->file_limit = 100;
132 ci->min_interval = 1000000;
133 ci->inset = 10; /* Percent */
135 while ((c = getopt(argc, argv, "w:i:dh:s:v:zl:t:D:")) > 0) {
136 switch (c) {
137 case 'h':
138 dorange(optarg, &ci->dark.h, &ci->bright.h, 360);
139 break;
140 case 's':
141 dorange(optarg, &ci->dark.s, &ci->bright.s, 255);
142 break;
143 case 'v':
144 dorange(optarg, &ci->dark.v, &ci->bright.v, 255);
145 break;
146 case 'z':
147 ci->zapping = 1;
148 break;
149 case 'l':
150 ci->file_limit = atoi(optarg);
151 break;
152 case 'i':
153 ci->min_interval = 1000000 * atof(optarg);
154 break;
155 case 't':
156 ci->threshold = atof(optarg) * 1000;
157 if (ci->threshold > 1000 || ci->threshold < 0) {
158 fprintf(stderr, "Invalid threshold value '%s' (range is 0-1)\n", optarg);
159 return -1;
161 break;
162 case 'w':
163 ci->min_width = atoi(optarg);
164 break;
165 case 'd':
166 ci->debug++;
167 break;
168 case 'D':
169 ci->dir = av_strdup(optarg);
170 break;
171 default:
172 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
173 return -1;
177 fprintf(stderr, "Fish detector configured:\n");
178 fprintf(stderr, " HSV range: %d,%d,%d - %d,%d,%d\n",
179 ci->dark.h,
180 ci->dark.s,
181 ci->dark.v,
182 ci->bright.h,
183 ci->bright.s,
184 ci->bright.v);
185 fprintf(stderr, " Threshold is %d%% pixels\n", ci->threshold / 10);
188 return 0;
191 static void get_hsv(HSV *hsv, int r, int g, int b)
193 int i, v, x, f;
195 x = (r < g) ? r : g;
196 if (b < x)
197 x = b;
198 v = (r > g) ? r : g;
199 if (b > v)
200 v = b;
202 if (v == x) {
203 hsv->h = 0;
204 hsv->s = 0;
205 hsv->v = v;
206 return;
209 if (r == v) {
210 f = g - b;
211 i = 0;
212 } else if (g == v) {
213 f = b - r;
214 i = 2 * 60;
215 } else {
216 f = r - g;
217 i = 4 * 60;
220 hsv->h = i + (60 * f) / (v - x);
221 if (hsv->h < 0)
222 hsv->h += 360;
224 hsv->s = (255 * (v - x)) / v;
225 hsv->v = v;
227 return;
230 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
232 ContextInfo *ci = (ContextInfo *) ctx;
233 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
234 int rowsize = picture->linesize[0];
236 #if 0
237 printf("pix_fmt = %d, width = %d, pts = %lld, ci->next_pts = %lld\n",
238 pix_fmt, width, pts, ci->next_pts);
239 #endif
241 if (pts < ci->next_pts)
242 return;
244 if (width < ci->min_width)
245 return;
247 ci->next_pts = pts + 1000000;
249 if (pix_fmt == PIX_FMT_YUV420P) {
250 uint8_t *y, *u, *v;
251 int width2 = width >> 1;
252 int inrange = 0;
253 int pixcnt;
254 int h;
255 int h_start, h_end;
256 int w_start, w_end;
258 h_end = 2 * ((ci->inset * height) / 200);
259 h_start = height - h_end;
261 w_end = (ci->inset * width2) / 100;
262 w_start = width2 - w_end;
264 pixcnt = ((h_start - h_end) >> 1) * (w_start - w_end);
266 y = picture->data[0] + h_end * picture->linesize[0] + w_end * 2;
267 u = picture->data[1] + h_end * picture->linesize[1] / 2 + w_end;
268 v = picture->data[2] + h_end * picture->linesize[2] / 2 + w_end;
270 for (h = h_start; h > h_end; h -= 2) {
271 int w;
273 for (w = w_start; w > w_end; w--) {
274 unsigned int r,g,b;
275 HSV hsv;
276 int cb, cr, yt, r_add, g_add, b_add;
278 YUV_TO_RGB1_CCIR(u[0], v[0]);
279 YUV_TO_RGB2_CCIR(r, g, b, y[0]);
281 get_hsv(&hsv, r, g, b);
283 if (ci->debug > 1)
284 fprintf(stderr, "(%d,%d,%d) -> (%d,%d,%d)\n",
285 r,g,b,hsv.h,hsv.s,hsv.v);
288 if (hsv.h >= ci->dark.h && hsv.h <= ci->bright.h &&
289 hsv.s >= ci->dark.s && hsv.s <= ci->bright.s &&
290 hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {
291 inrange++;
292 } else if (ci->zapping) {
293 y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 16;
294 u[0] = 128;
295 v[0] = 128;
298 y+= 2;
299 u++;
300 v++;
303 y += picture->linesize[0] * 2 - (w_start - w_end) * 2;
304 u += picture->linesize[1] - (w_start - w_end);
305 v += picture->linesize[2] - (w_start - w_end);
308 if (ci->debug)
309 fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
311 if (inrange * 1000 / pixcnt >= ci->threshold) {
312 /* Save to file */
313 int size;
314 char *buf;
315 AVPicture picture1;
316 static int frame_counter;
317 static int foundfile;
319 if ((frame_counter++ % 20) == 0) {
320 /* Check how many files we have */
321 DIR *d;
323 foundfile = 0;
325 d = opendir(ci->dir);
326 if (d) {
327 struct dirent *dent;
329 while ((dent = readdir(d))) {
330 if (strncmp("fishimg", dent->d_name, 7) == 0) {
331 if (strcmp(".ppm", dent->d_name + strlen(dent->d_name) - 4) == 0) {
332 foundfile++;
336 closedir(d);
340 if (foundfile < ci->file_limit) {
341 FILE *f;
342 char fname[256];
344 size = avpicture_get_size(PIX_FMT_RGB24, width, height);
345 buf = av_malloc(size);
347 avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
349 // if we already got a SWS context, let's realloc if is not re-useable
350 ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
351 width, height, pix_fmt,
352 width, height, PIX_FMT_RGB24,
353 sws_flags, NULL, NULL, NULL);
354 if (ci->toRGB_convert_ctx == NULL) {
355 av_log(NULL, AV_LOG_ERROR,
356 "Cannot initialize the toRGB conversion context\n");
357 return;
359 // img_convert parameters are 2 first destination, then 4 source
360 // sws_scale parameters are context, 4 first source, then 2 destination
361 sws_scale(ci->toRGB_convert_ctx,
362 picture->data, picture->linesize, 0, height,
363 picture1.data, picture1.linesize);
365 /* Write out the PPM file */
366 snprintf(fname, sizeof(fname), "%s/fishimg%ld_%"PRId64".ppm", ci->dir, (long)(av_gettime() / 1000000), pts);
367 f = fopen(fname, "w");
368 if (f) {
369 fprintf(f, "P6 %d %d 255\n", width, height);
370 fwrite(buf, width * height * 3, 1, f);
371 fclose(f);
374 av_free(buf);
375 ci->next_pts = pts + ci->min_interval;