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.

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