v4l2-helpers.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * Start the video capture on the device.
  119. *
  120. * This enqueues the memory mapped buffers and instructs the device to start
  121. * the video stream.
  122. *
  123. * @param dev handle for the v4l2 device
  124. * @param buf buffer data
  125. *
  126. * @return negative on failure
  127. */
  128. int_fast32_t v4l2_start_capture(int_fast32_t dev, struct v4l2_buffer_data *buf);
  129. /**
  130. * Stop the video capture on the device.
  131. *
  132. * @param dev handle for the v4l2 device
  133. *
  134. * @return negative on failure
  135. */
  136. int_fast32_t v4l2_stop_capture(int_fast32_t dev);
  137. /**
  138. * Create memory mapping for buffers
  139. *
  140. * This tries to map at least 2, preferably 4, buffers to application memory.
  141. *
  142. * @param dev handle for the v4l2 device
  143. * @param buf buffer data
  144. *
  145. * @return negative on failure
  146. */
  147. int_fast32_t v4l2_create_mmap(int_fast32_t dev, struct v4l2_buffer_data *buf);
  148. /**
  149. * Destroy the memory mapping for buffers
  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_destroy_mmap(struct v4l2_buffer_data *buf);
  157. /**
  158. * Set the video input on the device.
  159. *
  160. * If the action succeeds input is set to the currently selected input.
  161. *
  162. * @param dev handle for the v4l2 device
  163. * @param input index of the input or -1 to leave it as is
  164. *
  165. * @return negative on failure
  166. */
  167. int_fast32_t v4l2_set_input(int_fast32_t dev, int *input);
  168. #ifdef __cplusplus
  169. }
  170. #endif