Bibliothèque de traitements d'images en virgule flottante. http://la.buvette.org/photos/cumul/
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.

grabvidseq.c 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* V4L2 video picture grabber
  2. Origin :V4L2GRAB.C - patched by tTh
  3. Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@infradead.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/types.h>
  19. #include <sys/time.h>
  20. #include <sys/mman.h>
  21. #include <linux/videodev2.h>
  22. #include <libv4l2.h>
  23. #include "../floatimg.h"
  24. #include "funcs.h"
  25. /* --------------------------------------------------------------------- */
  26. /* compilation control */
  27. #define SAVE_AS_CUMUL 1
  28. #define SAVE_AS_PNM 0
  29. #define SAVE_AS_FIMG 0
  30. /* --------------------------------------------------------------------- */
  31. #define CLEAR(x) memset(&(x), 0, sizeof(x))
  32. struct buffer {
  33. void *start;
  34. size_t length;
  35. };
  36. int verbosity;
  37. /* --------------------------------------------------------------------- */
  38. static void xioctl(int fh, int request, void *arg)
  39. {
  40. int r;
  41. do {
  42. r = v4l2_ioctl(fh, request, arg);
  43. } while (r == -1 && ((errno == EINTR) || (errno == EAGAIN)));
  44. if (r == -1) {
  45. fprintf(stderr, "error %d, %s\n", errno, strerror(errno));
  46. sleep(1);
  47. exit(EXIT_FAILURE);
  48. }
  49. }
  50. /* --------------------------------------------------------------------- */
  51. void help(int v)
  52. {
  53. if (verbosity) {
  54. printf("compiled %s at %s\n", __DATE__, __TIME__);
  55. fimg_print_version(1);
  56. }
  57. puts("options :");
  58. puts("\t-d /dev/?\tselect video device");
  59. puts("\t-g\t\tconvert to gray");
  60. puts("\t-n NNN\t\thow many frames ?");
  61. puts("\t-O ./\t\tset Output dir");
  62. puts("\t-o bla.xxx\tset output filename");
  63. puts("\t-p NN.N\t\tperiod in seconds");
  64. puts("\t-s WxH\t\tsize of capture");
  65. puts("\t-c mode\t\tcontrast enhancement");
  66. puts("\t-u\t\ttry upscaling...");
  67. puts("\t-v\t\tincrease verbosity");
  68. if (verbosity) {
  69. puts("\n\t\tXXX list all the contrast modes, please\n");
  70. }
  71. exit(0);
  72. }
  73. /* --------------------------------------------------------------------- */
  74. int main(int argc, char **argv)
  75. {
  76. struct v4l2_format fmt;
  77. struct v4l2_buffer buf;
  78. struct v4l2_requestbuffers req;
  79. enum v4l2_buf_type type;
  80. fd_set fds;
  81. struct timeval tv;
  82. int r, fd = -1;
  83. unsigned int i, n_buffers;
  84. char *dev_name = "/dev/video0";
  85. // XXX FILE *fout;
  86. struct buffer *buffers;
  87. int foo;
  88. double period = 10.0; /* delai entre les captures */
  89. int nbre_capt = 1; /* nombre de captures */
  90. int opt;
  91. int width = 640;
  92. int height = 480;
  93. double t_final, maxvalue;
  94. int to_gray = 0;
  95. int upscaling = 0;
  96. int contrast = CONTRAST_NONE;
  97. int rotfactor = 0; /* only 0 or 90 here */
  98. char *dest_dir = "."; /* no trailing slash */
  99. char *outfile = "out.pnm";
  100. #if SAVE_AS_CUMUL
  101. FloatImg cumul, tmpfimg, *to_save;
  102. #endif
  103. while ((opt = getopt(argc, argv, "c:d:ghn:o:O:p:r:s:uv")) != -1) {
  104. switch(opt) {
  105. case 'c': contrast = fimg_id_contraste(optarg);
  106. if (contrast < 0) {
  107. fputs("unknow contrast\n", stderr);
  108. exit(1);
  109. }
  110. break;
  111. case 'd': dev_name = optarg; break;
  112. case 'g': to_gray = 1; break;
  113. case 'h': help(0); break;
  114. case 'n': nbre_capt = atoi(optarg); break;
  115. case 'O': dest_dir = optarg; break;
  116. case 'o': outfile = optarg; break;
  117. case 'p': foo = parse_double(optarg, &period);
  118. if (foo<0) {
  119. fprintf(stderr,
  120. "error parsing -p arg '%s'\n",
  121. optarg);
  122. exit(1);
  123. }
  124. period *= 1e6;
  125. break;
  126. case 'r': rotfactor = atoi(optarg); break;
  127. case 's': parse_WxH(optarg, &width, &height);
  128. break;
  129. case 'u': upscaling = 1; break;
  130. case 'v': verbosity++; break;
  131. default:
  132. fprintf(stderr, "option '%c' is wtf\n", opt);
  133. exit(1);
  134. }
  135. }
  136. if (verbosity > 1) {
  137. fprintf(stderr, "GrabVidSeq (%s, %s) pid=%d\n", __DATE__, __TIME__, getpid());
  138. fprintf(stderr, "grabing %d picz\n", nbre_capt);
  139. fprintf(stderr, "period is %.3f milliseconds\n", period/1e3);
  140. fprintf(stderr, "framesize is %dx%d\n", width, height);
  141. fprintf(stderr, "destdir is '%s'\n", dest_dir);
  142. if (upscaling) fprintf(stderr, "upscaling is on\n");
  143. }
  144. fd = v4l2_open(dev_name, O_RDWR | O_NONBLOCK, 0);
  145. if (fd < 0) {
  146. perror(dev_name);
  147. exit(EXIT_FAILURE);
  148. }
  149. CLEAR(fmt);
  150. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  151. fmt.fmt.pix.width = width;
  152. fmt.fmt.pix.height = height;
  153. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
  154. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  155. xioctl(fd, VIDIOC_S_FMT, &fmt);
  156. if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24) {
  157. /* are others formats usable ? */
  158. fprintf(stderr, "Libv4l didn't accept RGB24 format. Can't proceed.\n");
  159. exit(EXIT_FAILURE);
  160. }
  161. if ((fmt.fmt.pix.width != width) || (fmt.fmt.pix.height != height)) {
  162. fprintf(stderr, "Warning: driver is sending image at %dx%d\n",
  163. fmt.fmt.pix.width, fmt.fmt.pix.height);
  164. }
  165. CLEAR(req);
  166. req.count = 2;
  167. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  168. req.memory = V4L2_MEMORY_MMAP;
  169. xioctl(fd, VIDIOC_REQBUFS, &req);
  170. buffers = calloc(req.count, sizeof(*buffers));
  171. for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
  172. CLEAR(buf);
  173. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  174. buf.memory = V4L2_MEMORY_MMAP;
  175. buf.index = n_buffers;
  176. xioctl(fd, VIDIOC_QUERYBUF, &buf);
  177. buffers[n_buffers].length = buf.length;
  178. buffers[n_buffers].start = v4l2_mmap(NULL, buf.length,
  179. PROT_READ | PROT_WRITE, MAP_SHARED,
  180. fd, buf.m.offset);
  181. if (MAP_FAILED == buffers[n_buffers].start) {
  182. perror("v4l2_mmap");
  183. exit(EXIT_FAILURE);
  184. }
  185. }
  186. (void)fimg_timer_set(0);
  187. for (i = 0; i < n_buffers; ++i) {
  188. CLEAR(buf);
  189. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  190. buf.memory = V4L2_MEMORY_MMAP;
  191. buf.index = i;
  192. xioctl(fd, VIDIOC_QBUF, &buf);
  193. }
  194. #if SAVE_AS_CUMUL
  195. if (upscaling) {
  196. foo = fimg_create(&cumul,
  197. fmt.fmt.pix.width*2, fmt.fmt.pix.height*2,
  198. FIMG_TYPE_RGB);
  199. }
  200. else {
  201. foo = fimg_create(&cumul,
  202. fmt.fmt.pix.width, fmt.fmt.pix.height,
  203. FIMG_TYPE_RGB);
  204. }
  205. fimg_clear(&cumul);
  206. cumul.fval = 255.0;
  207. cumul.count = 0;
  208. to_save = &cumul;
  209. #endif
  210. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  211. xioctl(fd, VIDIOC_STREAMON, &type);
  212. #if 0
  213. if (verbosity) fprintf(stderr,"pid %d grabbing %d picz...\n",
  214. getpid(), nbre_capt);
  215. #endif
  216. for (i = 0; i < nbre_capt; i++) {
  217. do {
  218. FD_ZERO(&fds);
  219. FD_SET(fd, &fds);
  220. /* Timeout. */
  221. tv.tv_sec = 2;
  222. tv.tv_usec = 0;
  223. r = select(fd + 1, &fds, NULL, NULL, &tv);
  224. } while ((r == -1 && (errno = EINTR)));
  225. if (r == -1) {
  226. perror("select");
  227. return errno;
  228. }
  229. if(verbosity > 1) {
  230. printf("%6d / %6d %9.3f\r", i, nbre_capt,
  231. fimg_timer_get(0));
  232. }
  233. CLEAR(buf);
  234. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  235. buf.memory = V4L2_MEMORY_MMAP;
  236. xioctl(fd, VIDIOC_DQBUF, &buf);
  237. #if SAVE_AS_PNM
  238. /* at this time,we'v got a picture in-memory,
  239. so we can blast in on storage */
  240. sprintf(out_name, "%s/%05d.ppm", dest_dir, i);
  241. if (verbosity > 1) fprintf(stderr, "--> %s\n", out_name);
  242. fout = fopen(out_name, "w");
  243. if (!fout) {
  244. perror("Cannot open image");
  245. exit(EXIT_FAILURE);
  246. }
  247. fprintf(fout, "P6\n%d %d 255\n",
  248. fmt.fmt.pix.width, fmt.fmt.pix.height);
  249. fwrite(buffers[buf.index].start, buf.bytesused, 1, fout);
  250. fclose(fout);
  251. #endif
  252. #if SAVE_AS_CUMUL
  253. if (upscaling) {
  254. x_upscaler_0(buffers[buf.index].start,
  255. fmt.fmt.pix.width, fmt.fmt.pix.height, &cumul);
  256. }
  257. else {
  258. x_add_rgb2fimg(buffers[buf.index].start,
  259. fmt.fmt.pix.width, fmt.fmt.pix.height, &cumul);
  260. }
  261. #endif
  262. #if SAVE_AS_FIMG
  263. sprintf(out_name, "%s/%05d.fimg", dest_dir, i);
  264. if (verbosity > 1) fprintf(stderr, "--> %s\n", out_name);
  265. foo = x_rgb2file(buffers[buf.index].start,
  266. fmt.fmt.pix.width, fmt.fmt.pix.height,
  267. out_name);
  268. #endif
  269. if (nbre_capt > 1 && period > 1.0) {
  270. usleep(period);
  271. }
  272. xioctl(fd, VIDIOC_QBUF, &buf);
  273. }
  274. fflush(stdout);
  275. if (verbosity) {
  276. t_final = fimg_timer_get(0);
  277. fprintf(stderr, "pid %d : elapsed %.3g s -> %.2f fps\n", getpid(),
  278. t_final, (double)nbre_capt / t_final);
  279. }
  280. if (to_gray) {
  281. if (verbosity) fputs("converting to gray\n", stderr);
  282. foo = fimg_to_gray(&cumul);
  283. }
  284. #if SAVE_AS_CUMUL
  285. // save cumul to file
  286. if (verbosity) fprintf(stderr, "saving cumul to '%s'\n", outfile);
  287. /* ----- nouveau 15 nov 2019 */
  288. maxvalue = cumul.fval * cumul.count;
  289. if (verbosity) {
  290. fprintf(stderr, "computed maxvalue = %g\n", maxvalue);
  291. }
  292. switch (contrast) {
  293. case CONTRAST_NONE:
  294. // if (verbosity) fprintf(stderr, "contrast: none\n");
  295. break;
  296. case CONTRAST_SQRT:
  297. fimg_square_root(&cumul, NULL, maxvalue);
  298. break;
  299. case CONTRAST_POW2:
  300. fimg_power_2(&cumul, NULL, maxvalue);
  301. break;
  302. case CONTRAST_COS01:
  303. fimg_cos_01(&cumul, NULL, maxvalue);
  304. break;
  305. case CONTRAST_COS010:
  306. fimg_cos_010(&cumul, NULL, maxvalue);
  307. break;
  308. default:
  309. fprintf(stderr, "bad contrast method\n");
  310. break;
  311. }
  312. /* XXX warning, new from coronahome 26 mars 2020 */
  313. if (90 == rotfactor) {
  314. memset(&tmpfimg, 0, sizeof(FloatImg));
  315. foo = fimg_rotate_90(&cumul, &tmpfimg, 0);
  316. if (verbosity > 1) {
  317. fprintf(stderr, "dump rot90 %p\n", &tmpfimg);
  318. foo = fimg_save_as_png(&tmpfimg, "rot90.png", 0);
  319. }
  320. to_save = &tmpfimg;
  321. }
  322. foo = format_from_extension(outfile);
  323. switch (foo) {
  324. case FILE_TYPE_FIMG:
  325. foo = fimg_dump_to_file(&cumul, outfile, 0);
  326. break;
  327. case FILE_TYPE_PNM:
  328. foo = fimg_save_as_pnm(&cumul, outfile, 1);
  329. break;
  330. case FILE_TYPE_PNG:
  331. foo = fimg_save_as_png(&cumul, outfile, 0);
  332. break;
  333. default:
  334. fprintf(stderr, "can't save as %s\n", outfile);
  335. break;
  336. }
  337. // free buffers
  338. fimg_destroy(&cumul);
  339. #endif
  340. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  341. xioctl(fd, VIDIOC_STREAMOFF, &type);
  342. for (i = 0; i < n_buffers; ++i) {
  343. v4l2_munmap(buffers[i].start, buffers[i].length);
  344. }
  345. // free(buffers); /* atomic bombing */
  346. v4l2_close(fd);
  347. return 0;
  348. }