1
0

v4l2-helpers.c 6.3 KB

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