V4l2Device.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* ---------------------------------------------------------------------------
  2. ** This software is in the public domain, furnished "as is", without technical
  3. ** support, and with no warranty, express or implied, as to its usefulness for
  4. ** any purpose.
  5. **
  6. ** V4l2Device.h
  7. **
  8. ** V4L2 wrapper
  9. **
  10. ** -------------------------------------------------------------------------*/
  11. #ifndef V4L2_DEVICE
  12. #define V4L2_DEVICE
  13. #include <string>
  14. #include <list>
  15. #include <linux/videodev2.h>
  16. #include <fcntl.h>
  17. #ifndef V4L2_PIX_FMT_VP8
  18. #define V4L2_PIX_FMT_VP8 v4l2_fourcc('V', 'P', '8', '0')
  19. #endif
  20. #ifndef V4L2_PIX_FMT_VP9
  21. #define V4L2_PIX_FMT_VP9 v4l2_fourcc('V', 'P', '9', '0')
  22. #endif
  23. #ifndef V4L2_PIX_FMT_HEVC
  24. #define V4L2_PIX_FMT_HEVC v4l2_fourcc('H', 'E', 'V', 'C')
  25. #endif
  26. // ---------------------------------
  27. // V4L2 Device parameters
  28. // ---------------------------------
  29. /*
  30. * YUV 格式对照表,根据Qt v4l2 test benchmark软件(debain类系统 apt-get install qv4l2 安装)的capture image format一栏进行对应
  31. 2.6.1. Packed YUV formats
  32. 2.6.2. V4L2_PIX_FMT_GREY (‘GREY’)
  33. 2.6.3. V4L2_PIX_FMT_Y10 (‘Y10 ‘)
  34. 2.6.4. V4L2_PIX_FMT_Y12 (‘Y12 ‘)
  35. 2.6.5. V4L2_PIX_FMT_Y10BPACK (‘Y10B’)
  36. 2.6.6. V4L2_PIX_FMT_Y16 (‘Y16 ‘)
  37. 2.6.7. V4L2_PIX_FMT_Y16_BE (‘Y16 ‘ | (1 << 31))
  38. 2.6.8. V4L2_PIX_FMT_Y8I (‘Y8I ‘)
  39. 2.6.9. V4L2_PIX_FMT_Y12I (‘Y12I’)
  40. 2.6.10. V4L2_PIX_FMT_UV8 (‘UV8’)
  41. 2.6.11. V4L2_PIX_FMT_YUYV (‘YUYV’)
  42. 2.6.12. V4L2_PIX_FMT_UYVY (‘UYVY’)
  43. 2.6.13. V4L2_PIX_FMT_YVYU (‘YVYU’)
  44. 2.6.14. V4L2_PIX_FMT_VYUY (‘VYUY’)
  45. 2.6.15. V4L2_PIX_FMT_Y41P (‘Y41P’)
  46. 2.6.16. V4L2_PIX_FMT_YVU420 (‘YV12’), V4L2_PIX_FMT_YUV420 (‘YU12’)
  47. 2.6.17. V4L2_PIX_FMT_YUV420M (‘YM12’), V4L2_PIX_FMT_YVU420M (‘YM21’)
  48. 2.6.18. V4L2_PIX_FMT_YUV422M (‘YM16’), V4L2_PIX_FMT_YVU422M (‘YM61’)
  49. 2.6.19. V4L2_PIX_FMT_YUV444M (‘YM24’), V4L2_PIX_FMT_YVU444M (‘YM42’)
  50. 2.6.20. V4L2_PIX_FMT_YVU410 (‘YVU9’), V4L2_PIX_FMT_YUV410 (‘YUV9’)
  51. 2.6.21. V4L2_PIX_FMT_YUV422P (‘422P’)
  52. 2.6.22. V4L2_PIX_FMT_YUV411P (‘411P’)
  53. 2.6.23. V4L2_PIX_FMT_NV12 (‘NV12’), V4L2_PIX_FMT_NV21 (‘NV21’)
  54. 2.6.24. V4L2_PIX_FMT_NV12M (‘NM12’), V4L2_PIX_FMT_NV21M (‘NM21’), V4L2_PIX_FMT_NV12MT_16X16
  55. 2.6.25. V4L2_PIX_FMT_NV12MT (‘TM12’)
  56. 2.6.26. V4L2_PIX_FMT_NV16 (‘NV16’), V4L2_PIX_FMT_NV61 (‘NV61’)
  57. 2.6.27. V4L2_PIX_FMT_NV16M (‘NM16’), V4L2_PIX_FMT_NV61M (‘NM61’)
  58. 2.6.28. V4L2_PIX_FMT_NV24 (‘NV24’), V4L2_PIX_FMT_NV42 (‘NV42’)
  59. 2.6.29. V4L2_PIX_FMT_M420 (‘M420’)
  60. */
  61. struct V4L2DeviceParameters
  62. { /**
  63. * @brief V4L2DeviceParameters
  64. * @param devname 设备名,如输入"/dev/video0"
  65. * @param formatList 多个图像帧格式
  66. * @param width 设置的输出图像宽,并不一定生效。
  67. * @param height 设置的输出图像高,并不一定生效。
  68. * @param fps 设置的输出图像帧率,并不一定生效。
  69. * @param m_exp_mode 曝光模式
  70. * @param m_exp_level 曝光登记
  71. * @param m_exp_abs 曝光绝对值
  72. * @param input_index 单设备多输入的情况下,指定输入index的参数,如果单设备单输入,直接传入参数0
  73. * @param verbose
  74. * @param openFlags
  75. */
  76. V4L2DeviceParameters(const char* devname, const std::list<unsigned int> & formatList, unsigned int width, unsigned int height, int fps,
  77. __s32 exp_mode, int exp_level = 0, int exp_abs = 5,
  78. unsigned int input_index = 0, int verbose = 0, int openFlags = O_RDWR | O_NONBLOCK) :
  79. m_devName(devname), m_inputIndex(input_index), m_formatList(formatList), m_width(width), m_height(height), m_fps(fps),
  80. m_verbose(verbose), m_openFlags(openFlags),m_exp_mode(exp_mode),m_exp_level(exp_level),m_exp_abs(exp_abs) {}
  81. /**
  82. * @brief V4L2DeviceParameters
  83. * @param devname
  84. * @param format 图像帧格式
  85. * @param width 设置的输出图像宽,并不一定生效。
  86. * @param height 设置的输出图像高,并不一定生效。
  87. * @param fps 设置的输出图像帧率,并不一定生效。
  88. * @param m_exp_mode 曝光模式
  89. * @param m_exp_level 曝光登记
  90. * @param m_exp_abs 曝光绝对值
  91. * @param input_index 单设备多输入的情况下,指定输入index的参数,如果单设备单输入,直接传入参数0
  92. * @param verbose
  93. * @param openFlags
  94. */
  95. V4L2DeviceParameters(const char* devname, unsigned int format, unsigned int width, unsigned int height, int fps,
  96. __s32 exp_mode, int exp_level = 0, int exp_abs = 5,
  97. unsigned int input_index = 0, int verbose = 0, int openFlags = O_RDWR | O_NONBLOCK) :
  98. m_devName(devname), m_inputIndex(input_index), m_width(width), m_height(height), m_fps(fps), m_verbose(verbose), m_openFlags(openFlags),
  99. m_exp_mode(exp_mode),m_exp_level(exp_level),m_exp_abs(exp_abs){
  100. if (format) {
  101. m_formatList.push_back(format);
  102. }
  103. }
  104. std::string m_devName;
  105. unsigned int m_inputIndex;
  106. std::list<unsigned int> m_formatList;
  107. unsigned int m_width;
  108. unsigned int m_height;
  109. int m_fps;
  110. int m_verbose;
  111. int m_openFlags;
  112. int m_exp_level;
  113. int m_exp_abs;
  114. __s32 m_exp_mode;
  115. };
  116. // ---------------------------------
  117. // V4L2 Device
  118. // ---------------------------------
  119. class V4l2Device
  120. {
  121. friend class V4l2Capture;
  122. friend class V4l2Output;
  123. protected:
  124. void close();
  125. int initdevice(const char *dev_name , unsigned int mandatoryCapabilities );
  126. int checkCapabilities(int fd, unsigned int mandatoryCapabilities);
  127. int configureFormat(int fd);
  128. int configureFormat(int fd, unsigned int format, unsigned int width, unsigned int height);
  129. int configureParam(int fd);
  130. int configureExp(int fd);
  131. virtual bool init(unsigned int mandatoryCapabilities);
  132. virtual size_t writeInternal(char*, size_t) { return -1; }
  133. virtual bool startPartialWrite(void) { return false; }
  134. virtual size_t writePartialInternal(char*, size_t) { return -1; }
  135. virtual bool endPartialWrite(void) { return false; }
  136. virtual size_t readInternal(char*, size_t) { return -1; }
  137. public:
  138. V4l2Device(const V4L2DeviceParameters& params, v4l2_buf_type deviceType);
  139. virtual ~V4l2Device();
  140. virtual bool isReady() { return (m_fd != -1); }
  141. virtual bool start() { return true; }
  142. virtual bool stop() { return true; }
  143. unsigned int getBufferSize() { return m_bufferSize; }
  144. unsigned int getFormat() { return m_format; }
  145. unsigned int getWidth() { return m_width; }
  146. unsigned int getHeight() { return m_height; }
  147. unsigned char *getBusInfo() { return bus_info; }
  148. int getFd() { return m_fd; }
  149. void queryFormat();
  150. protected:
  151. V4L2DeviceParameters m_params;
  152. int m_fd;
  153. v4l2_buf_type m_deviceType;
  154. unsigned int m_bufferSize;
  155. unsigned int m_format;
  156. unsigned int m_width;
  157. unsigned int m_height;
  158. struct v4l2_buffer m_partialWriteBuf;
  159. bool m_partialWriteInProgress;
  160. unsigned char bus_info[32];
  161. };
  162. #endif