v4l2-helpers.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #pragma once
  15. #include <linux/videodev2.h>
  16. #include <libv4l2.h>
  17. #include <obs-module.h>
  18. #include <media-io/video-io.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /**
  23. * Data structure for mapped buffers
  24. */
  25. struct v4l2_mmap_info {
  26. /** length of the mapped buffer */
  27. size_t length;
  28. /** start address of the mapped buffer */
  29. void *start;
  30. };
  31. /**
  32. * Data structure for buffer info
  33. */
  34. struct v4l2_buffer_data {
  35. /** number of mapped buffers */
  36. uint_fast32_t count;
  37. /** memory info for mapped buffers */
  38. struct v4l2_mmap_info *info;
  39. };
  40. /**
  41. * Convert v4l2 pixel format to obs video format
  42. *
  43. * @param format v4l2 format id
  44. *
  45. * @return obs video_format id
  46. */
  47. static inline enum video_format v4l2_to_obs_video_format(uint_fast32_t format)
  48. {
  49. switch (format) {
  50. case V4L2_PIX_FMT_YVYU:
  51. return VIDEO_FORMAT_YVYU;
  52. case V4L2_PIX_FMT_YUYV:
  53. return VIDEO_FORMAT_YUY2;
  54. case V4L2_PIX_FMT_UYVY:
  55. return VIDEO_FORMAT_UYVY;
  56. case V4L2_PIX_FMT_NV12:
  57. return VIDEO_FORMAT_NV12;
  58. case V4L2_PIX_FMT_YUV420:
  59. return VIDEO_FORMAT_I420;
  60. case V4L2_PIX_FMT_YVU420:
  61. return VIDEO_FORMAT_I420;
  62. #ifdef V4L2_PIX_FMT_XBGR32
  63. case V4L2_PIX_FMT_XBGR32:
  64. return VIDEO_FORMAT_BGRX;
  65. #endif
  66. #ifdef V4L2_PIX_FMT_ABGR32
  67. case V4L2_PIX_FMT_ABGR32:
  68. return VIDEO_FORMAT_BGRA;
  69. #endif
  70. case V4L2_PIX_FMT_BGR24:
  71. return VIDEO_FORMAT_BGR3;
  72. default:
  73. return VIDEO_FORMAT_NONE;
  74. }
  75. }
  76. /**
  77. * Fixed framesizes for devices that don't support enumerating discrete values.
  78. *
  79. * The framesizes in this array are packed, the width encoded in the high word
  80. * and the height in the low word.
  81. * The array is terminated with a zero.
  82. */
  83. static const int v4l2_framesizes[] = {
  84. /* 4:3 */
  85. 160 << 16 | 120, 320 << 16 | 240, 480 << 16 | 320, 640 << 16 | 480,
  86. 800 << 16 | 600, 1024 << 16 | 768, 1280 << 16 | 960, 1440 << 16 | 1050,
  87. 1440 << 16 | 1080, 1600 << 16 | 1200,
  88. /* 16:9 */
  89. 640 << 16 | 360, 960 << 16 | 540, 1280 << 16 | 720, 1600 << 16 | 900,
  90. 1920 << 16 | 1080, 1920 << 16 | 1200, 2560 << 16 | 1440,
  91. 3840 << 16 | 2160,
  92. /* 21:9 */
  93. 2560 << 16 | 1080, 3440 << 16 | 1440, 5120 << 16 | 2160,
  94. /* tv */
  95. 432 << 16 | 520, 480 << 16 | 320, 480 << 16 | 530, 486 << 16 | 440,
  96. 576 << 16 | 310, 576 << 16 | 520, 576 << 16 | 570, 720 << 16 | 576,
  97. 1024 << 16 | 576,
  98. 0};
  99. /**
  100. * Fixed framerates for devices that don't support enumerating discrete values.
  101. *
  102. * The framerates in this array are packed, the numerator encoded in the high
  103. * word and the denominator in the low word.
  104. * The array is terminated with a zero.
  105. */
  106. static const int v4l2_framerates[] = {1 << 16 | 60,
  107. 1 << 16 | 50,
  108. 1 << 16 | 30,
  109. 1 << 16 | 25,
  110. 1 << 16 | 20,
  111. 1 << 16 | 15,
  112. 1 << 16 | 10,
  113. 1 << 16 | 5,
  114. 0};
  115. /**
  116. * Pack two integer values into one
  117. *
  118. * Obviously the input integers have to be truncated in order to fit into
  119. * one. The effective 16bits left are still enough to handle resolutions and
  120. * framerates just fine.
  121. *
  122. * @param a integer one
  123. * @param b integer two
  124. *
  125. * @return the packed integer
  126. */
  127. static inline int v4l2_pack_tuple(int a, int b)
  128. {
  129. return (a << 16) | (b & 0xffff);
  130. }
  131. /**
  132. * Unpack two integer values from one
  133. *
  134. * @see v4l2_pack_tuple
  135. *
  136. * @param a pointer to integer a
  137. * @param b pointer to integer b
  138. * @param packed the packed integer
  139. */
  140. static void v4l2_unpack_tuple(int *a, int *b, int packed)
  141. {
  142. *a = packed >> 16;
  143. *b = packed & 0xffff;
  144. }
  145. /**
  146. * Start the video capture on the device.
  147. *
  148. * This enqueues the memory mapped buffers and instructs the device to start
  149. * the video stream.
  150. *
  151. * @param dev handle for the v4l2 device
  152. * @param buf buffer data
  153. *
  154. * @return negative on failure
  155. */
  156. int_fast32_t v4l2_start_capture(int_fast32_t dev, struct v4l2_buffer_data *buf);
  157. /**
  158. * Stop the video capture on the device.
  159. *
  160. * @param dev handle for the v4l2 device
  161. *
  162. * @return negative on failure
  163. */
  164. int_fast32_t v4l2_stop_capture(int_fast32_t dev);
  165. /**
  166. * Resets video capture on the device.
  167. *
  168. * This runs stop and start capture again. Stop dequeues the buffers and start
  169. * enqueues the memory mapped buffers and instructs the device to start
  170. * the video stream.
  171. *
  172. * @param dev handle for the v4l2 device
  173. * @param buf buffer data
  174. *
  175. * @return negative on failure
  176. */
  177. int_fast32_t v4l2_reset_capture(int_fast32_t dev, struct v4l2_buffer_data *buf);
  178. #ifdef _DEBUG
  179. /**
  180. * Query the status of all buffers.
  181. * Only used for debug purposes.
  182. *
  183. * @param dev handle for the v4l2 device
  184. * @param buf_data buffer data
  185. *
  186. * @return negative on failure
  187. */
  188. int_fast32_t v4l2_query_all_buffers(int_fast32_t dev,
  189. struct v4l2_buffer_data *buf_data);
  190. #endif
  191. /**
  192. * Create memory mapping for buffers
  193. *
  194. * This tries to map at least 2, preferably 4, buffers to application memory.
  195. *
  196. * @param dev handle for the v4l2 device
  197. * @param buf buffer data
  198. *
  199. * @return negative on failure
  200. */
  201. int_fast32_t v4l2_create_mmap(int_fast32_t dev, struct v4l2_buffer_data *buf);
  202. /**
  203. * Destroy the memory mapping for buffers
  204. *
  205. * @param buf buffer data
  206. *
  207. * @return negative on failure
  208. */
  209. int_fast32_t v4l2_destroy_mmap(struct v4l2_buffer_data *buf);
  210. /**
  211. * Set the video input on the device.
  212. *
  213. * If the action succeeds input is set to the currently selected input.
  214. *
  215. * @param dev handle for the v4l2 device
  216. * @param input index of the input or -1 to leave it as is
  217. *
  218. * @return negative on failure
  219. */
  220. int_fast32_t v4l2_set_input(int_fast32_t dev, int *input);
  221. /**
  222. * Get capabilities for an input.
  223. *
  224. * @param dev handle for the v4l2 device
  225. * @param input index of the input or -1 to use the currently selected
  226. * @param caps capabilities for the input
  227. *
  228. * @return negative on failure
  229. */
  230. int_fast32_t v4l2_get_input_caps(int_fast32_t dev, int input, uint32_t *caps);
  231. /**
  232. * Set the video format on the device.
  233. *
  234. * If the action succeeds resolution, pixelformat and bytesperline are set
  235. * to the used values.
  236. *
  237. * @param dev handle for the v4l2 device
  238. * @param resolution packed value of the resolution or -1 to leave as is
  239. * @param pixelformat index of the pixelformat or -1 to leave as is
  240. * @param bytesperline this will be set accordingly on success
  241. *
  242. * @return negative on failure
  243. */
  244. int_fast32_t v4l2_set_format(int_fast32_t dev, int *resolution,
  245. int *pixelformat, int *bytesperline);
  246. /**
  247. * Set the framerate on the device.
  248. *
  249. * If the action succeeds framerate is set to the used value.
  250. *
  251. * @param dev handle to the v4l2 device
  252. * @param framerate packed value of the framerate or -1 to leave as is
  253. *
  254. * @return negative on failure
  255. */
  256. int_fast32_t v4l2_set_framerate(int_fast32_t dev, int *framerate);
  257. /**
  258. * Set a video standard on the device.
  259. *
  260. * If the action succeeds standard is set to the used video standard id.
  261. *
  262. * @param dev handle to the v4l2 device
  263. * @param standard id of the standard to use or -1 to leave as is
  264. *
  265. * @return negative on failure
  266. */
  267. int_fast32_t v4l2_set_standard(int_fast32_t dev, int *standard);
  268. /**
  269. * Get the dv timing for an input with a specified index
  270. *
  271. * @param dev handle to the v4l2 device
  272. * @param dvt pointer to the timing structure to fill
  273. * @param index index of the dv timing to fetch
  274. *
  275. * @return negative on failure
  276. */
  277. int_fast32_t v4l2_enum_dv_timing(int_fast32_t dev, struct v4l2_dv_timings *dvt,
  278. int index);
  279. /**
  280. * Set a dv timing on the device
  281. *
  282. * Currently standard will not be changed on success or error.
  283. *
  284. * @param dev handle to the v4l2 device
  285. * @param timing index of the timing to use or -1 to leave as is
  286. *
  287. * @return negative on failure
  288. */
  289. int_fast32_t v4l2_set_dv_timing(int_fast32_t dev, int *timing);
  290. #ifdef __cplusplus
  291. }
  292. #endif