getframe.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include <libmegapixels.h>
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include <string.h>
  5. #include <linux/videodev2.h>
  6. #include <sys/ioctl.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <errno.h>
  10. #include <sys/mman.h>
  11. #include <getopt.h>
  12. #include <ctype.h>
  13. struct buffer {
  14. void *start;
  15. size_t length;
  16. };
  17. struct buffer *buffers;
  18. int
  19. xioctl(int fd, int request, void *arg)
  20. {
  21. int r;
  22. do {
  23. r = ioctl(fd, request, arg);
  24. } while (r == -1 && errno == EINTR);
  25. return r;
  26. }
  27. void
  28. usage(char *name)
  29. {
  30. fprintf(stderr, "Usage: %s [-h] [-n offset] [-c camera] [-o file]\n", name);
  31. fprintf(stderr, "Get a frame using libmegapixels\n\n");
  32. fprintf(stderr, "Arguments:\n");
  33. fprintf(stderr, " -n offset Get the Nth frame from the output (defaults to 5)\n");
  34. fprintf(stderr, " -c camera Use a specific camera number\n");
  35. fprintf(stderr, " -m modenum Use another camera mode than the first\n");
  36. fprintf(stderr, " -o file File to store the frame in\n");
  37. fprintf(stderr, " -h Display this help text\n");
  38. }
  39. int
  40. main(int argc, char *argv[])
  41. {
  42. int c;
  43. int camera_id = 0;
  44. long res;
  45. char *end;
  46. char *outfile = NULL;
  47. int count = 5;
  48. int mode_idx = 0;
  49. while ((c = getopt(argc, argv, "hc:n:o:m:")) != -1) {
  50. switch (c) {
  51. case 'c':
  52. res = strtol(optarg, &end, 10);
  53. if (end == optarg || end == NULL || *end != '\0') {
  54. fprintf(stderr, "Invalid number for -c\n");
  55. return 1;
  56. }
  57. camera_id = (int) res;
  58. break;
  59. case 'o':
  60. outfile = optarg;
  61. break;
  62. case 'n':
  63. res = strtol(optarg, &end, 10);
  64. if (end == optarg || end == NULL || *end != '\0') {
  65. fprintf(stderr, "Invalid number for -n\n");
  66. return 1;
  67. }
  68. count = (int) res;
  69. break;
  70. case 'm':
  71. res = strtol(optarg, &end, 10);
  72. if (end == optarg || end == NULL || *end != '\0') {
  73. fprintf(stderr, "Invalid number for -m\n");
  74. return 1;
  75. }
  76. mode_idx = (int) res;
  77. break;
  78. case 'h':
  79. usage(argv[0]);
  80. return 0;
  81. break;
  82. case '?':
  83. if (optopt == 'd' || optopt == 'l') {
  84. fprintf(stderr, "Option -%c requires an argument.\n", optopt);
  85. } else if (isprint(optopt)) {
  86. fprintf(stderr, "Unknown option '-%c'\n", optopt);
  87. } else {
  88. fprintf(stderr, "Unknown option character x%x\n", optopt);
  89. }
  90. return 1;
  91. default:
  92. usage(argv[0]);
  93. return 1;
  94. }
  95. }
  96. char configpath[PATH_MAX];
  97. int ret = libmegapixels_find_config(configpath);
  98. libmegapixels_devconfig *config = {0};
  99. libmegapixels_init(&config);
  100. if (ret) {
  101. printf("Using config: %s\n", configpath);
  102. libmegapixels_load_file(config, configpath);
  103. } else {
  104. fprintf(stderr, "No config found for this device\n");
  105. }
  106. libmegapixels_load_uvc(config);
  107. if (config->count == 0) {
  108. fprintf(stderr, "No valid camera configuration\n");
  109. return 1;
  110. }
  111. if (camera_id > config->count - 1) {
  112. fprintf(stderr, "Camera id %d does not exist\n", camera_id);
  113. return 1;
  114. }
  115. libmegapixels_camera *camera = config->cameras[camera_id];
  116. if (libmegapixels_open(camera) != 0) {
  117. fprintf(stderr, "Could not open default camera\n");
  118. return 1;
  119. }
  120. if (mode_idx > camera->num_modes) {
  121. fprintf(stderr, "Invalid mode index: %d\n", mode_idx);
  122. }
  123. libmegapixels_mode *mode = camera->modes[mode_idx];
  124. struct v4l2_format format = {0};
  125. unsigned int frame_size = libmegapixels_select_mode(camera, mode, &format);
  126. if (frame_size == 0) {
  127. fprintf(stderr, "Could not select mode\n");
  128. return 1;
  129. }
  130. // Do the reqular V4L2 stuff to get a frame
  131. struct v4l2_capability cap;
  132. int mplanes = 0;
  133. enum v4l2_buf_type buftype = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  134. if (ioctl(camera->video_fd, VIDIOC_QUERYCAP, &cap) != 0) {
  135. fprintf(stderr, "VIDIOC_QUERYCAP failed: %s\n", strerror(errno));
  136. return 1;
  137. }
  138. if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  139. fprintf(stderr, "Device does not support streaming i/o\n");
  140. return 1;
  141. }
  142. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  143. if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
  144. mplanes = 1;
  145. buftype = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  146. } else {
  147. fprintf(stderr, "Device does not support V4L2_CAP_VIDEO_CAPTURE\n");
  148. return 1;
  149. }
  150. }
  151. struct v4l2_requestbuffers req = {0};
  152. req.count = 4;
  153. req.type = buftype;
  154. req.memory = V4L2_MEMORY_MMAP;
  155. if (xioctl(camera->video_fd, VIDIOC_REQBUFS, &req) == -1) {
  156. fprintf(stderr, "VIDIOC_REQBUFS failed: %s\n", strerror(errno));
  157. return 1;
  158. }
  159. buffers = calloc(req.count, sizeof(*buffers));
  160. for (int i = 0; i < req.count; i++) {
  161. struct v4l2_buffer buf = {0};
  162. buf.type = buftype;
  163. buf.memory = V4L2_MEMORY_MMAP;
  164. buf.index = i;
  165. struct v4l2_plane planes[1];
  166. if (mplanes) {
  167. buf.m.planes = planes;
  168. buf.length = 1;
  169. }
  170. if (xioctl(camera->video_fd, VIDIOC_QUERYBUF, &buf) == -1) {
  171. fprintf(stderr, "VIDIOC_QUERYBUF failed: %s\n", strerror(errno));
  172. return 1;
  173. }
  174. unsigned int offset;
  175. if (mplanes) {
  176. buffers[i].length = planes[0].length;
  177. offset = planes[0].m.mem_offset;
  178. } else {
  179. buffers[i].length = buf.length;
  180. offset = buf.m.offset;
  181. }
  182. buffers[i].start = mmap(NULL, buffers[i].length, PROT_READ | PROT_WRITE, MAP_SHARED, camera->video_fd, offset);
  183. if (buffers[i].start == MAP_FAILED) {
  184. fprintf(stderr, "mmap() failed\n");
  185. return 1;
  186. }
  187. }
  188. for (int i = 0; i < req.count; i++) {
  189. struct v4l2_buffer qbuf = {0};
  190. qbuf.type = buftype;
  191. qbuf.memory = V4L2_MEMORY_MMAP;
  192. qbuf.index = i;
  193. if (mplanes) {
  194. struct v4l2_plane qplanes[1];
  195. qbuf.m.planes = qplanes;
  196. qbuf.length = 1;
  197. }
  198. if (xioctl(camera->video_fd, VIDIOC_QBUF, &qbuf) == -1) {
  199. fprintf(stderr, "VIDIOC_QBUF failed: %s\n", strerror(errno));
  200. return 1;
  201. }
  202. }
  203. enum v4l2_buf_type type = buftype;
  204. if (xioctl(camera->video_fd, VIDIOC_STREAMON, &type) == -1) {
  205. fprintf(stderr, "VIDIOC_STREAMON failed: %s\n", strerror(errno));
  206. return 1;
  207. }
  208. while (count-- > 0) {
  209. while (1) {
  210. fd_set(fds);
  211. FD_ZERO(&fds);
  212. FD_SET(camera->video_fd, &fds);
  213. int sr = select(FD_SETSIZE, &fds, NULL, NULL, NULL);
  214. if (sr == -1) {
  215. if (errno == EINTR) {
  216. count++;
  217. continue;
  218. }
  219. fprintf(stderr, "select() failed: %s\n", strerror(errno));
  220. return 1;
  221. }
  222. struct v4l2_buffer buf = {0};
  223. buf.type = buftype;
  224. buf.memory = V4L2_MEMORY_MMAP;
  225. if (mplanes) {
  226. struct v4l2_plane dqplanes[1];
  227. buf.m.planes = dqplanes;
  228. buf.length = 1;
  229. }
  230. if (xioctl(camera->video_fd, VIDIOC_DQBUF, &buf) == -1) {
  231. fprintf(stderr, "VIDIOC_DQBUF failed\n");
  232. return 1;
  233. }
  234. fprintf(stderr, "received frame\n");
  235. if (count == 0 && outfile != NULL) {
  236. FILE *fp = fopen(outfile, "w");
  237. fwrite(buffers[buf.index].start, buf.bytesused, 1, fp);
  238. fclose(fp);
  239. printf("Stored frame to: %s\n", outfile);
  240. printf("Format: %dx%d\n", mode->width, mode->height);
  241. char fourcc[5] = {0};
  242. fourcc[0] = (char) (mode->v4l_pixfmt & 0xff);
  243. fourcc[1] = (char) ((mode->v4l_pixfmt >> 8) & 0xff);
  244. fourcc[2] = (char) ((mode->v4l_pixfmt >> 16) & 0xff);
  245. fourcc[3] = (char) ((mode->v4l_pixfmt >> 24) & 0xff);
  246. printf("Pixfmt: %s\n", fourcc);
  247. }
  248. if (xioctl(camera->video_fd, VIDIOC_QBUF, &buf) == -1) {
  249. fprintf(stderr, "VIDIOC_DQBUF failed\n");
  250. return 1;
  251. }
  252. break;
  253. }
  254. }
  255. return 0;
  256. }