* Fish Detector Hook
* Copyright (c) 2002 Philip Gladstone
*
- * This file implements a fish detector. It is used to see when a
+ * This file implements a fish detector. It is used to see when a
* goldfish passes in front of the camera. It does this by counting
* the number of input pixels that fall within a particular HSV
* range.
* -d turn debugging on
* -D <directory> where to put the fish images
*
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <fcntl.h>
#include "framehook.h"
#include "dsputil.h"
+#include "avformat.h"
+#include "swscale.h"
+
+static int sws_flags = SWS_BICUBIC;
#define SCALEBITS 10
#define ONE_HALF (1 << (SCALEBITS - 1))
}
-
-
+
+
typedef struct {
int h; /* 0 .. 360 */
int s; /* 0 .. 255 */
int v; /* 0 .. 255 */
} HSV;
-
+
typedef struct {
int zapping;
int threshold;
int64_t next_pts;
int inset;
int min_width;
+ struct SwsContext *toRGB_convert_ctx;
} ContextInfo;
static void dorange(const char *s, int *first, int *second, int maxval)
void Release(void *ctx)
{
- if (ctx)
+ ContextInfo *ci;
+ ci = (ContextInfo *) ctx;
+
+ if (ctx) {
+ sws_freeContext(ci->toRGB_convert_ctx);
av_free(ctx);
+ }
}
int Configure(void **ctxp, int argc, char *argv[])
static void get_hsv(HSV *hsv, int r, int g, int b)
{
int i, v, x, f;
-
+
x = (r < g) ? r : g;
if (b < x)
x = b;
v = (r > g) ? r : g;
if (b > v)
v = b;
-
+
if (v == x) {
hsv->h = 0;
hsv->s = 0;
hsv->v = v;
return;
}
-
+
if (r == v) {
f = g - b;
i = 0;
f = r - g;
i = 4 * 60;
}
-
+
hsv->h = i + (60 * f) / (v - x);
if (hsv->h < 0)
hsv->h += 360;
hsv->s = (255 * (v - x)) / v;
hsv->v = v;
-
+
return;
-}
+}
void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
{
ContextInfo *ci = (ContextInfo *) ctx;
- uint8_t *cm = cropTbl + MAX_NEG_CROP;
+ uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
int rowsize = picture->linesize[0];
#if 0
if (width < ci->min_width)
return;
- ci->next_pts = pts + 1000000;
+ ci->next_pts = pts + 1000000;
if (pix_fmt == PIX_FMT_YUV420P) {
uint8_t *y, *u, *v;
get_hsv(&hsv, r, g, b);
- if (ci->debug > 1)
+ if (ci->debug > 1)
fprintf(stderr, "(%d,%d,%d) -> (%d,%d,%d)\n",
r,g,b,hsv.h,hsv.s,hsv.v);
if (hsv.h >= ci->dark.h && hsv.h <= ci->bright.h &&
hsv.s >= ci->dark.s && hsv.s <= ci->bright.s &&
- hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {
+ hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {
inrange++;
} else if (ci->zapping) {
y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 16;
v += picture->linesize[2] - (w_start - w_end);
}
- if (ci->debug)
+ if (ci->debug)
fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
if (inrange * 1000 / pixcnt >= ci->threshold) {
}
if (foundfile < ci->file_limit) {
+ FILE *f;
+ char fname[256];
+
size = avpicture_get_size(PIX_FMT_RGB24, width, height);
buf = av_malloc(size);
avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
- if (img_convert(&picture1, PIX_FMT_RGB24,
- picture, pix_fmt, width, height) >= 0) {
- /* Write out the PPM file */
- FILE *f;
- char fname[256];
+ // if we already got a SWS context, let's realloc if is not re-useable
+ ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
+ width, height, pix_fmt,
+ width, height, PIX_FMT_RGB24,
+ sws_flags, NULL, NULL, NULL);
+ if (ci->toRGB_convert_ctx == NULL) {
+ av_log(NULL, AV_LOG_ERROR,
+ "Cannot initialize the toRGB conversion context\n");
+ return;
+ }
+ // img_convert parameters are 2 first destination, then 4 source
+ // sws_scale parameters are context, 4 first source, then 2 destination
+ sws_scale(ci->toRGB_convert_ctx,
+ picture->data, picture->linesize, 0, height,
+ picture1.data, picture1.linesize);
- sprintf(fname, "%s/fishimg%ld_%lld.ppm", ci->dir, time(0), pts);
+ /* Write out the PPM file */
+ snprintf(fname, sizeof(fname), "%s/fishimg%ld_%"PRId64".ppm", ci->dir, (long)(av_gettime() / 1000000), pts);
f = fopen(fname, "w");
if (f) {
fprintf(f, "P6 %d %d 255\n", width, height);
fwrite(buf, width * height * 3, 1, f);
fclose(f);
}
- }
av_free(buf);
- ci->next_pts = pts + ci->min_interval;
+ ci->next_pts = pts + ci->min_interval;
}
}
}