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