v4l2-helpers.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <leonhard@in-verted.de>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <sys/mman.h>
  15. #include <util/bmem.h>
  16. #include "v4l2-helpers.h"
  17. #define blog(level, msg, ...) blog(level, "v4l2-helpers: " msg, ##__VA_ARGS__)
  18. int_fast32_t v4l2_start_capture(int_fast32_t dev, struct v4l2_buffer_data *buf)
  19. {
  20. enum v4l2_buf_type type;
  21. struct v4l2_buffer enq;
  22. memset(&enq, 0, sizeof(enq));
  23. enq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  24. enq.memory = V4L2_MEMORY_MMAP;
  25. for (enq.index = 0; enq.index < buf->count; ++enq.index) {
  26. if (v4l2_ioctl(dev, VIDIOC_QBUF, &enq) < 0) {
  27. blog(LOG_ERROR, "unable to queue buffer");
  28. return -1;
  29. }
  30. }
  31. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  32. if (v4l2_ioctl(dev, VIDIOC_STREAMON, &type) < 0) {
  33. blog(LOG_ERROR, "unable to start stream");
  34. return -1;
  35. }
  36. return 0;
  37. }
  38. int_fast32_t v4l2_stop_capture(int_fast32_t dev)
  39. {
  40. enum v4l2_buf_type type;
  41. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  42. if (v4l2_ioctl(dev, VIDIOC_STREAMOFF, &type) < 0) {
  43. blog(LOG_ERROR, "unable to stop stream");
  44. return -1;
  45. }
  46. return 0;
  47. }
  48. int_fast32_t v4l2_reset_capture(int_fast32_t dev, struct v4l2_buffer_data *buf)
  49. {
  50. blog(LOG_DEBUG, "attempting to reset capture");
  51. if (v4l2_stop_capture(dev) < 0)
  52. return -1;
  53. if (v4l2_start_capture(dev, buf) < 0)
  54. return -1;
  55. return 0;
  56. }
  57. #ifdef _DEBUG
  58. int_fast32_t v4l2_query_all_buffers(int_fast32_t dev, struct v4l2_buffer_data *buf_data)
  59. {
  60. struct v4l2_buffer buf;
  61. blog(LOG_DEBUG, "attempting to read buffer data for %ld buffers", buf_data->count);
  62. for (uint_fast32_t i = 0; i < buf_data->count; i++) {
  63. buf.index = i;
  64. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  65. buf.memory = V4L2_MEMORY_MMAP;
  66. if (v4l2_ioctl(dev, VIDIOC_QUERYBUF, &buf) < 0) {
  67. blog(LOG_DEBUG, "failed to read buffer data for buffer #%ld", i);
  68. } else {
  69. blog(LOG_DEBUG,
  70. "query buf #%ld info: ts: %06ld buf id #%d, flags 0x%08X, seq #%d, len %d, used %d", i,
  71. buf.timestamp.tv_usec, buf.index, buf.flags, buf.sequence, buf.length, buf.bytesused);
  72. }
  73. }
  74. return 0;
  75. }
  76. #endif
  77. int_fast32_t v4l2_create_mmap(int_fast32_t dev, struct v4l2_buffer_data *buf)
  78. {
  79. struct v4l2_requestbuffers req;
  80. struct v4l2_buffer map;
  81. memset(&req, 0, sizeof(req));
  82. req.count = 4;
  83. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  84. req.memory = V4L2_MEMORY_MMAP;
  85. if (v4l2_ioctl(dev, VIDIOC_REQBUFS, &req) < 0) {
  86. blog(LOG_ERROR, "Request for buffers failed !");
  87. return -1;
  88. }
  89. if (req.count < 2) {
  90. blog(LOG_ERROR, "Device returned less than 2 buffers");
  91. return -1;
  92. }
  93. buf->count = req.count;
  94. buf->info = bzalloc(req.count * sizeof(struct v4l2_mmap_info));
  95. memset(&map, 0, sizeof(map));
  96. map.type = req.type;
  97. map.memory = req.memory;
  98. for (map.index = 0; map.index < req.count; ++map.index) {
  99. if (v4l2_ioctl(dev, VIDIOC_QUERYBUF, &map) < 0) {
  100. blog(LOG_ERROR, "Failed to query buffer details");
  101. return -1;
  102. }
  103. buf->info[map.index].length = map.length;
  104. buf->info[map.index].start =
  105. v4l2_mmap(NULL, map.length, PROT_READ | PROT_WRITE, MAP_SHARED, dev, map.m.offset);
  106. if (buf->info[map.index].start == MAP_FAILED) {
  107. blog(LOG_ERROR, "mmap for buffer failed");
  108. return -1;
  109. }
  110. }
  111. return 0;
  112. }
  113. int_fast32_t v4l2_destroy_mmap(struct v4l2_buffer_data *buf)
  114. {
  115. for (uint_fast32_t i = 0; i < buf->count; ++i) {
  116. if (buf->info[i].start != MAP_FAILED && buf->info[i].start != 0)
  117. v4l2_munmap(buf->info[i].start, buf->info[i].length);
  118. }
  119. if (buf->count) {
  120. bfree(buf->info);
  121. buf->count = 0;
  122. }
  123. return 0;
  124. }
  125. int_fast32_t v4l2_set_input(int_fast32_t dev, int *input)
  126. {
  127. if (!dev || !input)
  128. return -1;
  129. return (*input == -1) ? v4l2_ioctl(dev, VIDIOC_G_INPUT, input) : v4l2_ioctl(dev, VIDIOC_S_INPUT, input);
  130. }
  131. int_fast32_t v4l2_get_input_caps(int_fast32_t dev, int input, uint32_t *caps)
  132. {
  133. if (!dev || !caps)
  134. return -1;
  135. if (input == -1) {
  136. if (v4l2_ioctl(dev, VIDIOC_G_INPUT, &input) < 0)
  137. return -1;
  138. }
  139. struct v4l2_input in;
  140. memset(&in, 0, sizeof(in));
  141. in.index = input;
  142. if (v4l2_ioctl(dev, VIDIOC_ENUMINPUT, &in) < 0)
  143. return -1;
  144. *caps = in.capabilities;
  145. return 0;
  146. }
  147. int_fast32_t v4l2_set_format(int_fast32_t dev, int64_t *resolution, int *pixelformat, int *bytesperline)
  148. {
  149. bool set = false;
  150. int width, height;
  151. struct v4l2_format fmt;
  152. if (!dev || !resolution || !pixelformat || !bytesperline)
  153. return -1;
  154. /* We need to set the type in order to query the settings */
  155. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  156. if (v4l2_ioctl(dev, VIDIOC_G_FMT, &fmt) < 0)
  157. return -1;
  158. if (*resolution != -1) {
  159. v4l2_unpack_tuple(&width, &height, *resolution);
  160. fmt.fmt.pix.width = width;
  161. fmt.fmt.pix.height = height;
  162. set = true;
  163. }
  164. if (*pixelformat != -1) {
  165. fmt.fmt.pix.pixelformat = *pixelformat;
  166. set = true;
  167. }
  168. if (set && (v4l2_ioctl(dev, VIDIOC_S_FMT, &fmt) < 0))
  169. return -1;
  170. *resolution = v4l2_pack_tuple(fmt.fmt.pix.width, fmt.fmt.pix.height);
  171. *pixelformat = fmt.fmt.pix.pixelformat;
  172. *bytesperline = fmt.fmt.pix.bytesperline;
  173. return 0;
  174. }
  175. int_fast32_t v4l2_set_framerate(int_fast32_t dev, int64_t *framerate)
  176. {
  177. bool set = false;
  178. int num, denom;
  179. struct v4l2_streamparm par;
  180. if (!dev || !framerate)
  181. return -1;
  182. /* We need to set the type in order to query the stream settings */
  183. par.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  184. if (v4l2_ioctl(dev, VIDIOC_G_PARM, &par) < 0)
  185. return -1;
  186. if (*framerate != -1) {
  187. v4l2_unpack_tuple(&num, &denom, *framerate);
  188. par.parm.capture.timeperframe.numerator = num;
  189. par.parm.capture.timeperframe.denominator = denom;
  190. set = true;
  191. }
  192. if (set && (v4l2_ioctl(dev, VIDIOC_S_PARM, &par) < 0))
  193. return -1;
  194. *framerate =
  195. v4l2_pack_tuple(par.parm.capture.timeperframe.numerator, par.parm.capture.timeperframe.denominator);
  196. return 0;
  197. }
  198. int_fast32_t v4l2_set_standard(int_fast32_t dev, int *standard)
  199. {
  200. if (!dev || !standard)
  201. return -1;
  202. if (*standard == -1) {
  203. if (v4l2_ioctl(dev, VIDIOC_G_STD, standard) < 0)
  204. return -1;
  205. } else {
  206. if (v4l2_ioctl(dev, VIDIOC_S_STD, standard) < 0)
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. int_fast32_t v4l2_enum_dv_timing(int_fast32_t dev, struct v4l2_dv_timings *dvt, int index)
  212. {
  213. #if !defined(VIDIOC_ENUM_DV_TIMINGS) || !defined(V4L2_IN_CAP_DV_TIMINGS)
  214. UNUSED_PARAMETER(dev);
  215. UNUSED_PARAMETER(dvt);
  216. UNUSED_PARAMETER(index);
  217. return -1;
  218. #else
  219. if (!dev || !dvt)
  220. return -1;
  221. struct v4l2_enum_dv_timings iter;
  222. memset(&iter, 0, sizeof(iter));
  223. iter.index = index;
  224. if (v4l2_ioctl(dev, VIDIOC_ENUM_DV_TIMINGS, &iter) < 0)
  225. return -1;
  226. memcpy(dvt, &iter.timings, sizeof(struct v4l2_dv_timings));
  227. return 0;
  228. #endif
  229. }
  230. int_fast32_t v4l2_set_dv_timing(int_fast32_t dev, int *timing)
  231. {
  232. if (!dev || !timing)
  233. return -1;
  234. if (*timing == -1)
  235. return 0;
  236. struct v4l2_dv_timings dvt;
  237. if (v4l2_enum_dv_timing(dev, &dvt, *timing) < 0)
  238. return -1;
  239. if (v4l2_ioctl(dev, VIDIOC_S_DV_TIMINGS, &dvt) < 0)
  240. return -1;
  241. return 0;
  242. }