fish.c 11 KB

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