* 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.
*
* 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
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
-#include <sys/time.h>
+#include <time.h>
#include <stdio.h>
#include <dirent.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))
+#define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
+
+#define YUV_TO_RGB1_CCIR(cb1, cr1)\
+{\
+ cb = (cb1) - 128;\
+ cr = (cr1) - 128;\
+ r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
+ g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
+ ONE_HALF;\
+ b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
+}
+
+#define YUV_TO_RGB2_CCIR(r, g, b, y1)\
+{\
+ yt = ((y1) - 16) * FIX(255.0/219.0);\
+ r = cm[(yt + r_add) >> SCALEBITS];\
+ g = cm[(yt + g_add) >> SCALEBITS];\
+ b = cm[(yt + b_add) >> SCALEBITS];\
+}
+
+
-#define SCALE_BITS 10
-
-#define C_Y (76309 >> (16 - SCALE_BITS))
-#define C_RV (117504 >> (16 - SCALE_BITS))
-#define C_BU (138453 >> (16 - SCALE_BITS))
-#define C_GU (13954 >> (16 - SCALE_BITS))
-#define C_GV (34903 >> (16 - SCALE_BITS))
-
typedef struct {
int h; /* 0 .. 360 */
int s; /* 0 .. 255 */
int v; /* 0 .. 255 */
} HSV;
-
+
typedef struct {
int zapping;
int threshold;
int file_limit;
int debug;
int min_interval;
- INT64 next_pts;
+ int64_t next_pts;
int inset;
int min_width;
+ struct SwsContext *toRGB_convert_ctx;
+ enum PixelFormat sws_pix_fmt; // Sws_Context is opaque, we need to save
+ int sws_width, sws_height; // this to check if we can re-use contexts
} ContextInfo;
static void dorange(const char *s, int *first, int *second, int maxval)
*second = maxval;
}
+void Release(void *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[])
{
optind = 0;
ci->dir = "/tmp";
- ci->threshold = 1000;
+ ci->threshold = 100;
ci->file_limit = 100;
ci->min_interval = 1000000;
ci->inset = 10; /* Percent */
break;
case 't':
ci->threshold = atof(optarg) * 1000;
+ if (ci->threshold > 1000 || ci->threshold < 0) {
+ fprintf(stderr, "Invalid threshold value '%s' (range is 0-1)\n", optarg);
+ return -1;
+ }
break;
case 'w':
ci->min_width = atoi(optarg);
ci->debug++;
break;
case 'D':
- ci->dir = strdup(optarg);
+ ci->dir = av_strdup(optarg);
break;
default:
fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
ci->bright.h,
ci->bright.s,
ci->bright.v);
+ fprintf(stderr, " Threshold is %d%% pixels\n", ci->threshold / 10);
+
return 0;
}
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 pts)
+void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
{
ContextInfo *ci = (ContextInfo *) ctx;
- UINT8 *cm = cropTbl + MAX_NEG_CROP;
+ uint8_t *cm = cropTbl + MAX_NEG_CROP;
int rowsize = picture->linesize[0];
+#if 0
+ printf("pix_fmt = %d, width = %d, pts = %lld, ci->next_pts = %lld\n",
+ pix_fmt, width, pts, ci->next_pts);
+#endif
+
if (pts < ci->next_pts)
return;
if (width < ci->min_width)
return;
- ci->next_pts = pts + 1000000;
+ ci->next_pts = pts + 1000000;
if (pix_fmt == PIX_FMT_YUV420P) {
- UINT8 *y, *u, *v;
+ uint8_t *y, *u, *v;
int width2 = width >> 1;
int inrange = 0;
int pixcnt;
pixcnt = ((h_start - h_end) >> 1) * (w_start - w_end);
- y = picture->data[0];
- u = picture->data[1];
- v = picture->data[2];
+ y = picture->data[0] + h_end * picture->linesize[0] + w_end * 2;
+ u = picture->data[1] + h_end * picture->linesize[1] / 2 + w_end;
+ v = picture->data[2] + h_end * picture->linesize[2] / 2 + w_end;
for (h = h_start; h > h_end; h -= 2) {
int w;
for (w = w_start; w > w_end; w--) {
- int r,g,b;
- int Y, U, V;
+ unsigned int r,g,b;
HSV hsv;
+ int cb, cr, yt, r_add, g_add, b_add;
- U = u[0] - 128;
- V = v[0] - 128;
-
- Y = (y[0] - 16) * C_Y;
-
- r = cm[(Y + C_RV * V + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
- g = cm[(Y + - C_GU * U - C_GV * V + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
- b = cm[(Y + C_BU * U + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
+ YUV_TO_RGB1_CCIR(u[0], v[0]);
+ YUV_TO_RGB2_CCIR(r, g, b, y[0]);
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] = 0;
+ y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 16;
+ u[0] = 128;
+ v[0] = 128;
}
y+= 2;
v++;
}
- y += picture->linesize[0] * 2 - width;
- u += picture->linesize[1] - width2;
- v += picture->linesize[2] - width2;
+ y += picture->linesize[0] * 2 - (w_start - w_end) * 2;
+ u += picture->linesize[1] - (w_start - w_end);
+ v += picture->linesize[2] - (w_start - w_end);
}
+ if (ci->debug)
+ fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
+
if (inrange * 1000 / pixcnt >= ci->threshold) {
/* Save to file */
int size;
static int frame_counter;
static int foundfile;
- if (ci->debug)
- fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
-
if ((frame_counter++ % 20) == 0) {
/* Check how many files we have */
DIR *d;
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) {
+
+ // if we already got a SWS context, let's realloc if is not re-useable
+ if (ci->toRGB_convert_ctx != NULL) {
+ if ((ci->sws_pix_fmt != pix_fmt) ||
+ (ci->sws_width != width) || (ci->sws_height != height)) {
+ sws_freeContext(ci->toRGB_convert_ctx);
+ ci->toRGB_convert_ctx = NULL;
+ }
+ }
+ if (ci->toRGB_convert_ctx == NULL) {
+ ci->sws_pix_fmt = pix_fmt;
+ ci->sws_width = width;
+ ci->sws_height = height;
+ ci->toRGB_convert_ctx = sws_getContext(
+ ci->sws_width, ci->sws_height,
+ ci->sws_pix_fmt,
+ ci->sws_width, ci->sws_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");
+ exit(1);
+ }
+ }
+ // 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, ci->sws_height,
+ picture1.data, picture1.linesize);
+
/* Write out the PPM file */
FILE *f;
char fname[256];
- sprintf(fname, "%s/fishimg%ld_%lld.ppm", ci->dir, time(0), pts);
+ 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;
}
}
}
}
-/* To ensure correct typing */
-FrameHookConfigureFn ConfigureFn = Configure;
-FrameHookProcessFn ProcessFn = Process;