v4l2-helpers.h 8.5 KB

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