1
0

v4l2-helpers.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. default: return VIDEO_FORMAT_NONE;
  57. }
  58. }
  59. /**
  60. * Fixed framesizes for devices that don't support enumerating discrete values.
  61. *
  62. * The framesizes in this array are packed, the width encoded in the high word
  63. * and the height in the low word.
  64. * The array is terminated with a zero.
  65. */
  66. static const int v4l2_framesizes[] =
  67. {
  68. /* 4:3 */
  69. 160<<16 | 120,
  70. 320<<16 | 240,
  71. 480<<16 | 320,
  72. 640<<16 | 480,
  73. 800<<16 | 600,
  74. 1024<<16 | 768,
  75. 1280<<16 | 960,
  76. 1440<<16 | 1050,
  77. 1440<<16 | 1080,
  78. 1600<<16 | 1200,
  79. /* 16:9 */
  80. 640<<16 | 360,
  81. 960<<16 | 540,
  82. 1280<<16 | 720,
  83. 1600<<16 | 900,
  84. 1920<<16 | 1080,
  85. 1920<<16 | 1200,
  86. /* tv */
  87. 432<<16 | 520,
  88. 480<<16 | 320,
  89. 480<<16 | 530,
  90. 486<<16 | 440,
  91. 576<<16 | 310,
  92. 576<<16 | 520,
  93. 576<<16 | 570,
  94. 720<<16 | 576,
  95. 1024<<16 | 576,
  96. 0
  97. };
  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 int v4l2_framerates[] =
  106. {
  107. 1<<16 | 60,
  108. 1<<16 | 50,
  109. 1<<16 | 30,
  110. 1<<16 | 25,
  111. 1<<16 | 20,
  112. 1<<16 | 15,
  113. 1<<16 | 10,
  114. 1<<16 | 5,
  115. 0
  116. };
  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 int v4l2_pack_tuple(int a, int b)
  130. {
  131. return (a << 16) | (b & 0xffff);
  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(int *a, int *b, int packed)
  143. {
  144. *a = packed >> 16;
  145. *b = packed & 0xffff;
  146. }
  147. /**
  148. * Start the video capture on the device.
  149. *
  150. * This enqueues the memory mapped buffers and instructs the device to start
  151. * the video stream.
  152. *
  153. * @param dev handle for the v4l2 device
  154. * @param buf buffer data
  155. *
  156. * @return negative on failure
  157. */
  158. int_fast32_t v4l2_start_capture(int_fast32_t dev, struct v4l2_buffer_data *buf);
  159. /**
  160. * Stop the video capture on the device.
  161. *
  162. * @param dev handle for the v4l2 device
  163. *
  164. * @return negative on failure
  165. */
  166. int_fast32_t v4l2_stop_capture(int_fast32_t dev);
  167. /**
  168. * Create memory mapping for buffers
  169. *
  170. * This tries to map at least 2, preferably 4, buffers to application memory.
  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_create_mmap(int_fast32_t dev, struct v4l2_buffer_data *buf);
  178. /**
  179. * Destroy the memory mapping for buffers
  180. *
  181. * @param dev handle for the v4l2 device
  182. * @param buf buffer data
  183. *
  184. * @return negative on failure
  185. */
  186. int_fast32_t v4l2_destroy_mmap(struct v4l2_buffer_data *buf);
  187. /**
  188. * Set the video input on the device.
  189. *
  190. * If the action succeeds input is set to the currently selected input.
  191. *
  192. * @param dev handle for the v4l2 device
  193. * @param input index of the input or -1 to leave it as is
  194. *
  195. * @return negative on failure
  196. */
  197. int_fast32_t v4l2_set_input(int_fast32_t dev, int *input);
  198. /**
  199. * Get capabilities for an input.
  200. *
  201. * @param dev handle for the v4l2 device
  202. * @param input index of the input or -1 to use the currently selected
  203. * @param caps capabilities for the input
  204. *
  205. * @return negative on failure
  206. */
  207. int_fast32_t v4l2_get_input_caps(int_fast32_t dev, int input, uint32_t *caps);
  208. /**
  209. * Set the video format on the device.
  210. *
  211. * If the action succeeds resolution, pixelformat and bytesperline are set
  212. * to the used values.
  213. *
  214. * @param dev handle for the v4l2 device
  215. * @param resolution packed value of the resolution or -1 to leave as is
  216. * @param pixelformat index of the pixelformat or -1 to leave as is
  217. * @param bytesperline this will be set accordingly on success
  218. *
  219. * @return negative on failure
  220. */
  221. int_fast32_t v4l2_set_format(int_fast32_t dev, int *resolution,
  222. int *pixelformat, int *bytesperline);
  223. /**
  224. * Set the framerate on the device.
  225. *
  226. * If the action succeeds framerate is set to the used value.
  227. *
  228. * @param dev handle to the v4l2 device
  229. * @param framerate packed value of the framerate or -1 to leave as is
  230. *
  231. * @return negative on failure
  232. */
  233. int_fast32_t v4l2_set_framerate(int_fast32_t dev, int *framerate);
  234. /**
  235. * Set a video standard on the device.
  236. *
  237. * If the action succeeds standard is set to the used video standard id.
  238. *
  239. * @param dev handle to the v4l2 device
  240. * @param standard id of the standard to use or -1 to leave as is
  241. *
  242. * @return negative on failure
  243. */
  244. int_fast32_t v4l2_set_standard(int_fast32_t dev, int *standard);
  245. /**
  246. * Get the dv timing for an input with a specified index
  247. *
  248. * @param dev handle to the v4l2 device
  249. * @param timing pointer to the timing structure to fill
  250. * @param index index of the dv timing to fetch
  251. *
  252. * @return negative on failure
  253. */
  254. int_fast32_t v4l2_enum_dv_timing(int_fast32_t dev, struct v4l2_dv_timings *dvt,
  255. int index);
  256. /**
  257. * Set a dv timing on the device
  258. *
  259. * Currently standard will not be changed on success or error.
  260. *
  261. * @param dev handle to the v4l2 device
  262. * @param timing index of the timing to use or -1 to leave as is
  263. *
  264. * @return negative on failure
  265. */
  266. int_fast32_t v4l2_set_dv_timing(int_fast32_t dev, int *timing);
  267. #ifdef __cplusplus
  268. }
  269. #endif