v4l2-helpers.h 7.3 KB

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