You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
9.4KB

  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 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.
  26. *
  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.
  31. *
  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
  35. */
  36. #include <stdlib.h>
  37. #include <fcntl.h>
  38. #include <unistd.h>
  39. #include <stdarg.h>
  40. #include <string.h>
  41. #include <time.h>
  42. #include <stdio.h>
  43. #include <dirent.h>
  44. #include "framehook.h"
  45. #include "dsputil.h"
  46. #define SCALE_BITS 10
  47. #define C_Y (76309 >> (16 - SCALE_BITS))
  48. #define C_RV (117504 >> (16 - SCALE_BITS))
  49. #define C_BU (138453 >> (16 - SCALE_BITS))
  50. #define C_GU (13954 >> (16 - SCALE_BITS))
  51. #define C_GV (34903 >> (16 - SCALE_BITS))
  52. typedef struct {
  53. int h; /* 0 .. 360 */
  54. int s; /* 0 .. 255 */
  55. int v; /* 0 .. 255 */
  56. } HSV;
  57. typedef struct {
  58. int zapping;
  59. int threshold;
  60. HSV dark, bright;
  61. char *dir;
  62. int file_limit;
  63. int debug;
  64. int min_interval;
  65. INT64 next_pts;
  66. int inset;
  67. int min_width;
  68. } ContextInfo;
  69. static void dorange(const char *s, int *first, int *second, int maxval)
  70. {
  71. sscanf(s, "%d-%d", first, second);
  72. if (*first > maxval)
  73. *first = maxval;
  74. if (*second > maxval)
  75. *second = maxval;
  76. }
  77. void Release(void *ctx)
  78. {
  79. if (ctx)
  80. av_free(ctx);
  81. }
  82. int Configure(void **ctxp, int argc, char *argv[])
  83. {
  84. ContextInfo *ci;
  85. int c;
  86. *ctxp = av_mallocz(sizeof(ContextInfo));
  87. ci = (ContextInfo *) *ctxp;
  88. optind = 0;
  89. ci->dir = "/tmp";
  90. ci->threshold = 1000;
  91. ci->file_limit = 100;
  92. ci->min_interval = 1000000;
  93. ci->inset = 10; /* Percent */
  94. while ((c = getopt(argc, argv, "w:i:dh:s:v:zl:t:D:")) > 0) {
  95. switch (c) {
  96. case 'h':
  97. dorange(optarg, &ci->dark.h, &ci->bright.h, 360);
  98. break;
  99. case 's':
  100. dorange(optarg, &ci->dark.s, &ci->bright.s, 255);
  101. break;
  102. case 'v':
  103. dorange(optarg, &ci->dark.v, &ci->bright.v, 255);
  104. break;
  105. case 'z':
  106. ci->zapping = 1;
  107. break;
  108. case 'l':
  109. ci->file_limit = atoi(optarg);
  110. break;
  111. case 'i':
  112. ci->min_interval = 1000000 * atof(optarg);
  113. break;
  114. case 't':
  115. ci->threshold = atof(optarg) * 1000;
  116. break;
  117. case 'w':
  118. ci->min_width = atoi(optarg);
  119. break;
  120. case 'd':
  121. ci->debug++;
  122. break;
  123. case 'D':
  124. ci->dir = strdup(optarg);
  125. break;
  126. default:
  127. fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
  128. return -1;
  129. }
  130. }
  131. fprintf(stderr, "Fish detector configured:\n");
  132. fprintf(stderr, " HSV range: %d,%d,%d - %d,%d,%d\n",
  133. ci->dark.h,
  134. ci->dark.s,
  135. ci->dark.v,
  136. ci->bright.h,
  137. ci->bright.s,
  138. ci->bright.v);
  139. return 0;
  140. }
  141. static void get_hsv(HSV *hsv, int r, int g, int b)
  142. {
  143. int i, v, x, f;
  144. x = (r < g) ? r : g;
  145. if (b < x)
  146. x = b;
  147. v = (r > g) ? r : g;
  148. if (b > v)
  149. v = b;
  150. if (v == x) {
  151. hsv->h = 0;
  152. hsv->s = 0;
  153. hsv->v = v;
  154. return;
  155. }
  156. if (r == v) {
  157. f = g - b;
  158. i = 0;
  159. } else if (g == v) {
  160. f = b - r;
  161. i = 2 * 60;
  162. } else {
  163. f = r - g;
  164. i = 4 * 60;
  165. }
  166. hsv->h = i + (60 * f) / (v - x);
  167. if (hsv->h < 0)
  168. hsv->h += 360;
  169. hsv->s = (255 * (v - x)) / v;
  170. hsv->v = v;
  171. return;
  172. }
  173. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, INT64 pts)
  174. {
  175. ContextInfo *ci = (ContextInfo *) ctx;
  176. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  177. int rowsize = picture->linesize[0];
  178. if (pts < ci->next_pts)
  179. return;
  180. if (width < ci->min_width)
  181. return;
  182. ci->next_pts = pts + 1000000;
  183. if (pix_fmt == PIX_FMT_YUV420P) {
  184. UINT8 *y, *u, *v;
  185. int width2 = width >> 1;
  186. int inrange = 0;
  187. int pixcnt;
  188. int h;
  189. int h_start, h_end;
  190. int w_start, w_end;
  191. h_end = 2 * ((ci->inset * height) / 200);
  192. h_start = height - h_end;
  193. w_end = (ci->inset * width2) / 100;
  194. w_start = width2 - w_end;
  195. pixcnt = ((h_start - h_end) >> 1) * (w_start - w_end);
  196. y = picture->data[0];
  197. u = picture->data[1];
  198. v = picture->data[2];
  199. for (h = h_start; h > h_end; h -= 2) {
  200. int w;
  201. for (w = w_start; w > w_end; w--) {
  202. int r,g,b;
  203. int Y, U, V;
  204. HSV hsv;
  205. U = u[0] - 128;
  206. V = v[0] - 128;
  207. Y = (y[0] - 16) * C_Y;
  208. r = cm[(Y + C_RV * V + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
  209. g = cm[(Y + - C_GU * U - C_GV * V + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
  210. b = cm[(Y + C_BU * U + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
  211. get_hsv(&hsv, r, g, b);
  212. if (ci->debug > 1)
  213. fprintf(stderr, "(%d,%d,%d) -> (%d,%d,%d)\n",
  214. r,g,b,hsv.h,hsv.s,hsv.v);
  215. if (hsv.h >= ci->dark.h && hsv.h <= ci->bright.h &&
  216. hsv.s >= ci->dark.s && hsv.s <= ci->bright.s &&
  217. hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {
  218. inrange++;
  219. } else if (ci->zapping) {
  220. y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 0;
  221. }
  222. y+= 2;
  223. u++;
  224. v++;
  225. }
  226. y += picture->linesize[0] * 2 - width;
  227. u += picture->linesize[1] - width2;
  228. v += picture->linesize[2] - width2;
  229. }
  230. if (inrange * 1000 / pixcnt >= ci->threshold) {
  231. /* Save to file */
  232. int size;
  233. char *buf;
  234. AVPicture picture1;
  235. static int frame_counter;
  236. static int foundfile;
  237. if (ci->debug)
  238. fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
  239. if ((frame_counter++ % 20) == 0) {
  240. /* Check how many files we have */
  241. DIR *d;
  242. foundfile = 0;
  243. d = opendir(ci->dir);
  244. if (d) {
  245. struct dirent *dent;
  246. while ((dent = readdir(d))) {
  247. if (strncmp("fishimg", dent->d_name, 7) == 0) {
  248. if (strcmp(".ppm", dent->d_name + strlen(dent->d_name) - 4) == 0) {
  249. foundfile++;
  250. }
  251. }
  252. }
  253. closedir(d);
  254. }
  255. }
  256. if (foundfile < ci->file_limit) {
  257. size = avpicture_get_size(PIX_FMT_RGB24, width, height);
  258. buf = av_malloc(size);
  259. avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
  260. if (img_convert(&picture1, PIX_FMT_RGB24,
  261. picture, pix_fmt, width, height) >= 0) {
  262. /* Write out the PPM file */
  263. FILE *f;
  264. char fname[256];
  265. sprintf(fname, "%s/fishimg%ld_%lld.ppm", ci->dir, time(0), pts);
  266. f = fopen(fname, "w");
  267. if (f) {
  268. fprintf(f, "P6 %d %d 255\n", width, height);
  269. fwrite(buf, width * height * 3, 1, f);
  270. fclose(f);
  271. }
  272. }
  273. av_free(buf);
  274. ci->next_pts = pts + ci->min_interval;
  275. }
  276. }
  277. }
  278. }