fish.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 file is part of FFmpeg.
  23. *
  24. * FFmpeg is free software; you can redistribute it and/or
  25. * modify it under the terms of the GNU Lesser General Public
  26. * License as published by the Free Software Foundation; either
  27. * version 2.1 of the License, or (at your option) any later version.
  28. *
  29. * FFmpeg is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. * Lesser General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Lesser General Public
  35. * License along with FFmpeg; if not, write to the Free Software
  36. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  37. */
  38. #include <stdlib.h>
  39. #include <fcntl.h>
  40. #include <unistd.h>
  41. #include <stdarg.h>
  42. #include <string.h>
  43. #include <time.h>
  44. #include <stdio.h>
  45. #include <dirent.h>
  46. #include "framehook.h"
  47. #include "dsputil.h"
  48. #include "avformat.h"
  49. #include "swscale.h"
  50. static int sws_flags = SWS_BICUBIC;
  51. #define SCALEBITS 10
  52. #define ONE_HALF (1 << (SCALEBITS - 1))
  53. #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
  54. #define YUV_TO_RGB1_CCIR(cb1, cr1)\
  55. {\
  56. cb = (cb1) - 128;\
  57. cr = (cr1) - 128;\
  58. r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
  59. g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
  60. ONE_HALF;\
  61. b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
  62. }
  63. #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
  64. {\
  65. yt = ((y1) - 16) * FIX(255.0/219.0);\
  66. r = cm[(yt + r_add) >> SCALEBITS];\
  67. g = cm[(yt + g_add) >> SCALEBITS];\
  68. b = cm[(yt + b_add) >> SCALEBITS];\
  69. }
  70. typedef struct {
  71. int h; /* 0 .. 360 */
  72. int s; /* 0 .. 255 */
  73. int v; /* 0 .. 255 */
  74. } HSV;
  75. typedef struct {
  76. int zapping;
  77. int threshold;
  78. HSV dark, bright;
  79. char *dir;
  80. int file_limit;
  81. int debug;
  82. int min_interval;
  83. int64_t next_pts;
  84. int inset;
  85. int min_width;
  86. struct SwsContext *toRGB_convert_ctx;
  87. } ContextInfo;
  88. static void dorange(const char *s, int *first, int *second, int maxval)
  89. {
  90. sscanf(s, "%d-%d", first, second);
  91. if (*first > maxval)
  92. *first = maxval;
  93. if (*second > maxval)
  94. *second = maxval;
  95. }
  96. void Release(void *ctx)
  97. {
  98. ContextInfo *ci;
  99. ci = (ContextInfo *) ctx;
  100. if (ctx) {
  101. sws_freeContext(ci->toRGB_convert_ctx);
  102. av_free(ctx);
  103. }
  104. }
  105. int Configure(void **ctxp, int argc, char *argv[])
  106. {
  107. ContextInfo *ci;
  108. int c;
  109. *ctxp = av_mallocz(sizeof(ContextInfo));
  110. ci = (ContextInfo *) *ctxp;
  111. optind = 1;
  112. ci->dir = "/tmp";
  113. ci->threshold = 100;
  114. ci->file_limit = 100;
  115. ci->min_interval = 1000000;
  116. ci->inset = 10; /* Percent */
  117. while ((c = getopt(argc, argv, "w:i:dh:s:v:zl:t:D:")) > 0) {
  118. switch (c) {
  119. case 'h':
  120. dorange(optarg, &ci->dark.h, &ci->bright.h, 360);
  121. break;
  122. case 's':
  123. dorange(optarg, &ci->dark.s, &ci->bright.s, 255);
  124. break;
  125. case 'v':
  126. dorange(optarg, &ci->dark.v, &ci->bright.v, 255);
  127. break;
  128. case 'z':
  129. ci->zapping = 1;
  130. break;
  131. case 'l':
  132. ci->file_limit = atoi(optarg);
  133. break;
  134. case 'i':
  135. ci->min_interval = 1000000 * atof(optarg);
  136. break;
  137. case 't':
  138. ci->threshold = atof(optarg) * 1000;
  139. if (ci->threshold > 1000 || ci->threshold < 0) {
  140. fprintf(stderr, "Invalid threshold value '%s' (range is 0-1)\n", optarg);
  141. return -1;
  142. }
  143. break;
  144. case 'w':
  145. ci->min_width = atoi(optarg);
  146. break;
  147. case 'd':
  148. ci->debug++;
  149. break;
  150. case 'D':
  151. ci->dir = av_strdup(optarg);
  152. break;
  153. default:
  154. fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
  155. return -1;
  156. }
  157. }
  158. fprintf(stderr, "Fish detector configured:\n");
  159. fprintf(stderr, " HSV range: %d,%d,%d - %d,%d,%d\n",
  160. ci->dark.h,
  161. ci->dark.s,
  162. ci->dark.v,
  163. ci->bright.h,
  164. ci->bright.s,
  165. ci->bright.v);
  166. fprintf(stderr, " Threshold is %d%% pixels\n", ci->threshold / 10);
  167. return 0;
  168. }
  169. static void get_hsv(HSV *hsv, int r, int g, int b)
  170. {
  171. int i, v, x, f;
  172. x = (r < g) ? r : g;
  173. if (b < x)
  174. x = b;
  175. v = (r > g) ? r : g;
  176. if (b > v)
  177. v = b;
  178. if (v == x) {
  179. hsv->h = 0;
  180. hsv->s = 0;
  181. hsv->v = v;
  182. return;
  183. }
  184. if (r == v) {
  185. f = g - b;
  186. i = 0;
  187. } else if (g == v) {
  188. f = b - r;
  189. i = 2 * 60;
  190. } else {
  191. f = r - g;
  192. i = 4 * 60;
  193. }
  194. hsv->h = i + (60 * f) / (v - x);
  195. if (hsv->h < 0)
  196. hsv->h += 360;
  197. hsv->s = (255 * (v - x)) / v;
  198. hsv->v = v;
  199. return;
  200. }
  201. void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
  202. {
  203. ContextInfo *ci = (ContextInfo *) ctx;
  204. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  205. int rowsize = picture->linesize[0];
  206. #if 0
  207. printf("pix_fmt = %d, width = %d, pts = %lld, ci->next_pts = %lld\n",
  208. pix_fmt, width, pts, ci->next_pts);
  209. #endif
  210. if (pts < ci->next_pts)
  211. return;
  212. if (width < ci->min_width)
  213. return;
  214. ci->next_pts = pts + 1000000;
  215. if (pix_fmt == PIX_FMT_YUV420P) {
  216. uint8_t *y, *u, *v;
  217. int width2 = width >> 1;
  218. int inrange = 0;
  219. int pixcnt;
  220. int h;
  221. int h_start, h_end;
  222. int w_start, w_end;
  223. h_end = 2 * ((ci->inset * height) / 200);
  224. h_start = height - h_end;
  225. w_end = (ci->inset * width2) / 100;
  226. w_start = width2 - w_end;
  227. pixcnt = ((h_start - h_end) >> 1) * (w_start - w_end);
  228. y = picture->data[0] + h_end * picture->linesize[0] + w_end * 2;
  229. u = picture->data[1] + h_end * picture->linesize[1] / 2 + w_end;
  230. v = picture->data[2] + h_end * picture->linesize[2] / 2 + w_end;
  231. for (h = h_start; h > h_end; h -= 2) {
  232. int w;
  233. for (w = w_start; w > w_end; w--) {
  234. unsigned int r,g,b;
  235. HSV hsv;
  236. int cb, cr, yt, r_add, g_add, b_add;
  237. YUV_TO_RGB1_CCIR(u[0], v[0]);
  238. YUV_TO_RGB2_CCIR(r, g, b, y[0]);
  239. get_hsv(&hsv, r, g, b);
  240. if (ci->debug > 1)
  241. fprintf(stderr, "(%d,%d,%d) -> (%d,%d,%d)\n",
  242. r,g,b,hsv.h,hsv.s,hsv.v);
  243. if (hsv.h >= ci->dark.h && hsv.h <= ci->bright.h &&
  244. hsv.s >= ci->dark.s && hsv.s <= ci->bright.s &&
  245. hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {
  246. inrange++;
  247. } else if (ci->zapping) {
  248. y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 16;
  249. u[0] = 128;
  250. v[0] = 128;
  251. }
  252. y+= 2;
  253. u++;
  254. v++;
  255. }
  256. y += picture->linesize[0] * 2 - (w_start - w_end) * 2;
  257. u += picture->linesize[1] - (w_start - w_end);
  258. v += picture->linesize[2] - (w_start - w_end);
  259. }
  260. if (ci->debug)
  261. fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
  262. if (inrange * 1000 / pixcnt >= ci->threshold) {
  263. /* Save to file */
  264. int size;
  265. char *buf;
  266. AVPicture picture1;
  267. static int frame_counter;
  268. static int foundfile;
  269. if ((frame_counter++ % 20) == 0) {
  270. /* Check how many files we have */
  271. DIR *d;
  272. foundfile = 0;
  273. d = opendir(ci->dir);
  274. if (d) {
  275. struct dirent *dent;
  276. while ((dent = readdir(d))) {
  277. if (strncmp("fishimg", dent->d_name, 7) == 0) {
  278. if (strcmp(".ppm", dent->d_name + strlen(dent->d_name) - 4) == 0) {
  279. foundfile++;
  280. }
  281. }
  282. }
  283. closedir(d);
  284. }
  285. }
  286. if (foundfile < ci->file_limit) {
  287. FILE *f;
  288. char fname[256];
  289. size = avpicture_get_size(PIX_FMT_RGB24, width, height);
  290. buf = av_malloc(size);
  291. avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
  292. // if we already got a SWS context, let's realloc if is not re-useable
  293. ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
  294. width, height, pix_fmt,
  295. width, height, PIX_FMT_RGB24,
  296. sws_flags, NULL, NULL, NULL);
  297. if (ci->toRGB_convert_ctx == NULL) {
  298. av_log(NULL, AV_LOG_ERROR,
  299. "Cannot initialize the toRGB conversion context\n");
  300. return;
  301. }
  302. // img_convert parameters are 2 first destination, then 4 source
  303. // sws_scale parameters are context, 4 first source, then 2 destination
  304. sws_scale(ci->toRGB_convert_ctx,
  305. picture->data, picture->linesize, 0, height,
  306. picture1.data, picture1.linesize);
  307. /* Write out the PPM file */
  308. snprintf(fname, sizeof(fname), "%s/fishimg%ld_%"PRId64".ppm", ci->dir, (long)(av_gettime() / 1000000), pts);
  309. f = fopen(fname, "w");
  310. if (f) {
  311. fprintf(f, "P6 %d %d 255\n", width, height);
  312. fwrite(buf, width * height * 3, 1, f);
  313. fclose(f);
  314. }
  315. av_free(buf);
  316. ci->next_pts = pts + ci->min_interval;
  317. }
  318. }
  319. }
  320. }