v4l2-helpers.c 7.1 KB

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