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
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 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.
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.
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
45 #include "framehook.h"
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))
76 static void dorange(const char *s
, int *first
, int *second
, int maxval
)
78 sscanf(s
, "%d-%d", first
, second
);
85 void Release(void *ctx
)
91 int Configure(void **ctxp
, int argc
, char *argv
[])
96 *ctxp
= av_mallocz(sizeof(ContextInfo
));
97 ci
= (ContextInfo
*) *ctxp
;
102 ci
->threshold
= 1000;
103 ci
->file_limit
= 100;
104 ci
->min_interval
= 1000000;
105 ci
->inset
= 10; /* Percent */
107 while ((c
= getopt(argc
, argv
, "w:i:dh:s:v:zl:t:D:")) > 0) {
110 dorange(optarg
, &ci
->dark
.h
, &ci
->bright
.h
, 360);
113 dorange(optarg
, &ci
->dark
.s
, &ci
->bright
.s
, 255);
116 dorange(optarg
, &ci
->dark
.v
, &ci
->bright
.v
, 255);
122 ci
->file_limit
= atoi(optarg
);
125 ci
->min_interval
= 1000000 * atof(optarg
);
128 ci
->threshold
= atof(optarg
) * 1000;
131 ci
->min_width
= atoi(optarg
);
137 ci
->dir
= av_strdup(optarg
);
140 fprintf(stderr
, "Unrecognized argument '%s'\n", argv
[optind
]);
145 fprintf(stderr
, "Fish detector configured:\n");
146 fprintf(stderr
, " HSV range: %d,%d,%d - %d,%d,%d\n",
157 static void get_hsv(HSV
*hsv
, int r
, int g
, int b
)
186 hsv
->h
= i
+ (60 * f
) / (v
- x
);
190 hsv
->s
= (255 * (v
- x
)) / v
;
196 void Process(void *ctx
, AVPicture
*picture
, enum PixelFormat pix_fmt
, int width
, int height
, INT64 pts
)
198 ContextInfo
*ci
= (ContextInfo
*) ctx
;
199 UINT8
*cm
= cropTbl
+ MAX_NEG_CROP
;
200 int rowsize
= picture
->linesize
[0];
202 if (pts
< ci
->next_pts
)
205 if (width
< ci
->min_width
)
208 ci
->next_pts
= pts
+ 1000000;
210 if (pix_fmt
== PIX_FMT_YUV420P
) {
212 int width2
= width
>> 1;
219 h_end
= 2 * ((ci
->inset
* height
) / 200);
220 h_start
= height
- h_end
;
222 w_end
= (ci
->inset
* width2
) / 100;
223 w_start
= width2
- w_end
;
225 pixcnt
= ((h_start
- h_end
) >> 1) * (w_start
- w_end
);
227 y
= picture
->data
[0];
228 u
= picture
->data
[1];
229 v
= picture
->data
[2];
231 for (h
= h_start
; h
> h_end
; h
-= 2) {
234 for (w
= w_start
; w
> w_end
; w
--) {
242 Y
= (y
[0] - 16) * C_Y
;
244 r
= cm
[(Y
+ C_RV
* V
+ (1 << (SCALE_BITS
- 1))) >> SCALE_BITS
];
245 g
= cm
[(Y
+ - C_GU
* U
- C_GV
* V
+ (1 << (SCALE_BITS
- 1))) >> SCALE_BITS
];
246 b
= cm
[(Y
+ C_BU
* U
+ (1 << (SCALE_BITS
- 1))) >> SCALE_BITS
];
248 get_hsv(&hsv
, r
, g
, b
);
251 fprintf(stderr
, "(%d,%d,%d) -> (%d,%d,%d)\n",
252 r
,g
,b
,hsv
.h
,hsv
.s
,hsv
.v
);
255 if (hsv
.h
>= ci
->dark
.h
&& hsv
.h
<= ci
->bright
.h
&&
256 hsv
.s
>= ci
->dark
.s
&& hsv
.s
<= ci
->bright
.s
&&
257 hsv
.v
>= ci
->dark
.v
&& hsv
.v
<= ci
->bright
.v
) {
259 } else if (ci
->zapping
) {
260 y
[0] = y
[1] = y
[rowsize
] = y
[rowsize
+ 1] = 0;
268 y
+= picture
->linesize
[0] * 2 - width
;
269 u
+= picture
->linesize
[1] - width2
;
270 v
+= picture
->linesize
[2] - width2
;
273 if (inrange
* 1000 / pixcnt
>= ci
->threshold
) {
278 static int frame_counter
;
279 static int foundfile
;
282 fprintf(stderr
, "Fish: Inrange=%d of %d = %d threshold\n", inrange
, pixcnt
, 1000 * inrange
/ pixcnt
);
284 if ((frame_counter
++ % 20) == 0) {
285 /* Check how many files we have */
290 d
= opendir(ci
->dir
);
294 while ((dent
= readdir(d
))) {
295 if (strncmp("fishimg", dent
->d_name
, 7) == 0) {
296 if (strcmp(".ppm", dent
->d_name
+ strlen(dent
->d_name
) - 4) == 0) {
305 if (foundfile
< ci
->file_limit
) {
306 size
= avpicture_get_size(PIX_FMT_RGB24
, width
, height
);
307 buf
= av_malloc(size
);
309 avpicture_fill(&picture1
, buf
, PIX_FMT_RGB24
, width
, height
);
310 if (img_convert(&picture1
, PIX_FMT_RGB24
,
311 picture
, pix_fmt
, width
, height
) >= 0) {
312 /* Write out the PPM file */
317 sprintf(fname
, "%s/fishimg%ld_%lld.ppm", ci
->dir
, time(0), pts
);
318 f
= fopen(fname
, "w");
320 fprintf(f
, "P6 %d %d 255\n", width
, height
);
321 fwrite(buf
, width
* height
* 3, 1, f
);
327 ci
->next_pts
= pts
+ ci
->min_interval
;