v4l2-helpers.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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), PACK64(800, 600), PACK64(1024, 768),
  88. PACK64(1280, 960), PACK64(1440, 1050), PACK64(1440, 1080), PACK64(1600, 1200),
  89. /* 16:9 */
  90. PACK64(640, 360), PACK64(960, 540), PACK64(1280, 720), PACK64(1600, 900), PACK64(1920, 1080),
  91. PACK64(1920, 1200), PACK64(2560, 1440), PACK64(3840, 2160),
  92. /* 21:9 */
  93. PACK64(2560, 1080), PACK64(3440, 1440), PACK64(5120, 2160),
  94. /* tv */
  95. PACK64(432, 520), PACK64(480, 320), PACK64(480, 530), PACK64(486, 440), PACK64(576, 310), PACK64(576, 520),
  96. PACK64(576, 570), PACK64(720, 576), PACK64(1024, 576),
  97. 0};
  98. /**
  99. * Fixed framerates for devices that don't support enumerating discrete values.
  100. *
  101. * The framerates in this array are packed, the numerator encoded in the high
  102. * word and the denominator in the low word.
  103. * The array is terminated with a zero.
  104. */
  105. static const int64_t v4l2_framerates[] = {PACK64(1, 60),
  106. PACK64(1, 50),
  107. PACK64(1, 30),
  108. PACK64(1, 25),
  109. PACK64(1, 20),
  110. PACK64(1, 15),
  111. PACK64(1, 10),
  112. PACK64(1, 5),
  113. 0};
  114. /**
  115. * Pack two integer values into one
  116. *
  117. * Obviously the input integers have to be truncated in order to fit into
  118. * one. The effective 16bits left are still enough to handle resolutions and
  119. * framerates just fine.
  120. *
  121. * @param a integer one
  122. * @param b integer two
  123. *
  124. * @return the packed integer
  125. */
  126. static inline int64_t v4l2_pack_tuple(int32_t a, int32_t b)
  127. {
  128. return PACK64(a, b);
  129. }
  130. /**
  131. * Unpack two integer values from one
  132. *
  133. * @see v4l2_pack_tuple
  134. *
  135. * @param a pointer to integer a
  136. * @param b pointer to integer b
  137. * @param packed the packed integer
  138. */
  139. static void v4l2_unpack_tuple(int32_t *a, int32_t *b, int64_t packed)
  140. {
  141. // Since we changed from 32 to 64 bits, handle old values too.
  142. if ((packed & 0xffffffff00000000) == 0) {
  143. *a = (int32_t)(packed >> 16);
  144. *b = (int32_t)(packed & 0xffff);
  145. } else {
  146. *a = (int32_t)(packed >> 32);
  147. *b = (int32_t)(packed & 0xffffffff);
  148. }
  149. }
  150. /**
  151. * Start the video capture on the device.
  152. *
  153. * This enqueues the memory mapped buffers and instructs the device to start
  154. * the video stream.
  155. *
  156. * @param dev handle for the v4l2 device
  157. * @param buf buffer data
  158. *
  159. * @return negative on failure
  160. */
  161. int_fast32_t v4l2_start_capture(int_fast32_t dev, struct v4l2_buffer_data *buf);
  162. /**
  163. * Stop the video capture on the device.
  164. *
  165. * @param dev handle for the v4l2 device
  166. *
  167. * @return negative on failure
  168. */
  169. int_fast32_t v4l2_stop_capture(int_fast32_t dev);
  170. /**
  171. * Resets video capture on the device.
  172. *
  173. * This runs stop and start capture again. Stop dequeues the buffers and start
  174. * enqueues the memory mapped buffers and instructs the device to start
  175. * the video stream.
  176. *
  177. * @param dev handle for the v4l2 device
  178. * @param buf buffer data
  179. *
  180. * @return negative on failure
  181. */
  182. int_fast32_t v4l2_reset_capture(int_fast32_t dev, struct v4l2_buffer_data *buf);
  183. #ifdef _DEBUG
  184. /**
  185. * Query the status of all buffers.
  186. * Only used for debug purposes.
  187. *
  188. * @param dev handle for the v4l2 device
  189. * @param buf_data buffer data
  190. *
  191. * @return negative on failure
  192. */
  193. int_fast32_t v4l2_query_all_buffers(int_fast32_t dev, struct v4l2_buffer_data *buf_data);
  194. #endif
  195. /**
  196. * Create memory mapping for buffers
  197. *
  198. * This tries to map at least 2, preferably 4, buffers to application memory.
  199. *
  200. * @param dev handle for the v4l2 device
  201. * @param buf buffer data
  202. *
  203. * @return negative on failure
  204. */
  205. int_fast32_t v4l2_create_mmap(int_fast32_t dev, struct v4l2_buffer_data *buf);
  206. /**
  207. * Destroy the memory mapping for buffers
  208. *
  209. * @param buf buffer data
  210. *
  211. * @return negative on failure
  212. */
  213. int_fast32_t v4l2_destroy_mmap(struct v4l2_buffer_data *buf);
  214. /**
  215. * Set the video input on the device.
  216. *
  217. * If the action succeeds input is set to the currently selected input.
  218. *
  219. * @param dev handle for the v4l2 device
  220. * @param input index of the input or -1 to leave it as is
  221. *
  222. * @return negative on failure
  223. */
  224. int_fast32_t v4l2_set_input(int_fast32_t dev, int *input);
  225. /**
  226. * Get capabilities for an input.
  227. *
  228. * @param dev handle for the v4l2 device
  229. * @param input index of the input or -1 to use the currently selected
  230. * @param caps capabilities for the input
  231. *
  232. * @return negative on failure
  233. */
  234. int_fast32_t v4l2_get_input_caps(int_fast32_t dev, int input, uint32_t *caps);
  235. /**
  236. * Set the video format on the device.
  237. *
  238. * If the action succeeds resolution, pixelformat and bytesperline are set
  239. * to the used values.
  240. *
  241. * @param dev handle for the v4l2 device
  242. * @param resolution packed value of the resolution or -1 to leave as is
  243. * @param pixelformat index of the pixelformat or -1 to leave as is
  244. * @param bytesperline this will be set accordingly on success
  245. *
  246. * @return negative on failure
  247. */
  248. int_fast32_t v4l2_set_format(int_fast32_t dev, int64_t *resolution, int *pixelformat, int *bytesperline);
  249. /**
  250. * Set the framerate on the device.
  251. *
  252. * If the action succeeds framerate is set to the used value.
  253. *
  254. * @param dev handle to the v4l2 device
  255. * @param framerate packed value of the framerate or -1 to leave as is
  256. *
  257. * @return negative on failure
  258. */
  259. int_fast32_t v4l2_set_framerate(int_fast32_t dev, int64_t *framerate);
  260. /**
  261. * Set a video standard on the device.
  262. *
  263. * If the action succeeds standard is set to the used video standard id.
  264. *
  265. * @param dev handle to the v4l2 device
  266. * @param standard id of the standard to use or -1 to leave as is
  267. *
  268. * @return negative on failure
  269. */
  270. int_fast32_t v4l2_set_standard(int_fast32_t dev, int *standard);
  271. /**
  272. * Get the dv timing for an input with a specified index
  273. *
  274. * @param dev handle to the v4l2 device
  275. * @param dvt pointer to the timing structure to fill
  276. * @param index index of the dv timing to fetch
  277. *
  278. * @return negative on failure
  279. */
  280. int_fast32_t v4l2_enum_dv_timing(int_fast32_t dev, struct v4l2_dv_timings *dvt, int index);
  281. /**
  282. * Set a dv timing on the device
  283. *
  284. * Currently standard will not be changed on success or error.
  285. *
  286. * @param dev handle to the v4l2 device
  287. * @param timing index of the timing to use or -1 to leave as is
  288. *
  289. * @return negative on failure
  290. */
  291. int_fast32_t v4l2_set_dv_timing(int_fast32_t dev, int *timing);
  292. #ifdef __cplusplus
  293. }
  294. #endif