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.

356 lines
10KB

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