v4l2-helpers.c 7.3 KB

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