fish.c 10.0 KB

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