• c 查询v4l2 各种参数


    1. #include <stdio.h>
    2. #include <sys/types.h>
    3. #include <sys/stat.h>
    4. #include <fcntl.h>
    5. #include <stdlib.h>
    6. #include <unistd.h>
    7. #include <sys/ioctl.h>
    8. #include <linux/videodev2.h>
    9. #include <string.h>
    10. #include <sys/mman.h>
    11. #include <sys/time.h>
    12. int main(void) {
    13. int fd = open("/dev/video0", O_RDWR);
    14. struct v4l2_capability { // VIDIOC_QUERYCAP
    15. __u8 driver[16]; //uvc
    16. __u8 card[32];
    17. __u8 bus_info[32];
    18. __u32 version;
    19. __u32 capabilities; //功能
    20. __u32 device_caps;
    21. __u32 reserved[3];
    22. };
    23. struct v4l2_capability cap;
    24. int t = ioctl(fd, VIDIOC_QUERYCAP, &cap);
    25. if (t < 0) {
    26. puts("cap error");
    27. }
    28. printf("%s\n", cap.driver);
    29. printf("%s\n", cap.card);
    30. printf("%s\n", cap.bus_info);
    31. printf("%d\n", cap.version);
    32. printf("%x\n", cap.capabilities); //84a00001 1:视频输入卡
    33. printf("%x\n", cap.device_caps); //4200001
    34. printf("%d\n", cap.reserved[0]);
    35. printf("%d\n", cap.reserved[1]);
    36. printf("%d\n", cap.reserved[2]);
    37. puts("--------------fmtdesc--------------------------------------");
    38. struct v4l2_fmtdesc { //VIDIOC_ENUM_FMT
    39. __u32 index; /* Format number */
    40. __u32 type; /* enum v4l2_buf_type */
    41. __u32 flags;
    42. __u8 description[32]; /* Description string */
    43. __u32 pixelformat; /* Format fourcc */
    44. __u32 mbus_code; /* Media bus code */
    45. __u32 reserved[3];
    46. }fmtdesc;
    47. fmtdesc.index=0; //0输入index的值,开始查询,本机index=0,1
    48. fmtdesc.type=1;
    49. int t1=ioctl(fd,VIDIOC_ENUM_FMT,&fmtdesc);
    50. if(t1<0){
    51. puts("fmtdesc error");
    52. }
    53. printf("%s\n",fmtdesc.description); //0:yuv422,1:mjpg
    54. fmtdesc.index=1; //0输入index的值,开始查询,本机index=0,1
    55. fmtdesc.type=1;
    56. int t2=ioctl(fd,VIDIOC_ENUM_FMT,&fmtdesc);
    57. if(t2<0){
    58. puts("fmtdesc error");
    59. }
    60. printf("%s\n",fmtdesc.description); //0:yuv422,1:mjpg
    61. puts("-------------------frame_format--------------------------");
    62. struct v4l2_format {
    63. __u32 type;
    64. union {
    65. // struct v4l2_pix_format pix; /* V4L2_BUF_TYPE_VIDEO_CAPTURE */
    66. struct v4l2_pix_format {
    67. __u32 width;
    68. __u32 height;
    69. __u32 pixelformat;
    70. __u32 field; /* enum v4l2_field */
    71. __u32 bytesperline; /* for padding, zero if unused */
    72. __u32 sizeimage;
    73. __u32 colorspace; /* enum v4l2_colorspace */
    74. __u32 priv; /* private data, depends on pixelformat */
    75. __u32 flags; /* format flags (V4L2_PIX_FMT_FLAG_*) */
    76. union {
    77. /* enum v4l2_ycbcr_encoding */
    78. __u32 ycbcr_enc;
    79. /* enum v4l2_hsv_encoding */
    80. __u32 hsv_enc;
    81. };
    82. __u32 quantization; /* enum v4l2_quantization */
    83. __u32 xfer_func; /* enum v4l2_xfer_func */
    84. }pix;
    85. struct v4l2_pix_format_mplane pix_mp; /* V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE */
    86. struct v4l2_window win; /* V4L2_BUF_TYPE_VIDEO_OVERLAY */
    87. struct v4l2_vbi_format vbi; /* V4L2_BUF_TYPE_VBI_CAPTURE */
    88. struct v4l2_sliced_vbi_format sliced; /* V4L2_BUF_TYPE_SLICED_VBI_CAPTURE */
    89. struct v4l2_sdr_format sdr; /* V4L2_BUF_TYPE_SDR_CAPTURE */
    90. struct v4l2_meta_format meta; /* V4L2_BUF_TYPE_META_CAPTURE */
    91. __u8 raw_data[200]; /* user-defined */
    92. } fmt;
    93. }format;
    94. format.type=1;
    95. int t3=ioctl(fd,VIDIOC_G_FMT,&format);
    96. if(t3<0){
    97. puts("format error");
    98. }
    99. printf("%d\n",format.fmt.pix.width); //2560 支持最大宽度
    100. printf("%d\n",format.fmt.pix.height); //1440 支持图像的最高宽度
    101. printf("%x\n",format.fmt.pix.pixelformat); //v4l2_fourcc值 47504a4d 4d=M 4a=J 50=P 47=G 16进制小序排列(反向)
    102. printf("%d\n",format.fmt.pix.field); //1=V4L2_FIELD_NONE
    103. printf("%d\n",format.fmt.pix.sizeimage); //7372800=2560*1440*16/8 每帧支持最大字节数
    104. puts("--------------------frame_size-------------------------------------");
    105. struct v4l2_frmsizeenum {
    106. __u32 index; /* Frame size number */
    107. __u32 pixel_format; /* Pixel format */
    108. __u32 type; /* Frame size type the device supports. */
    109. union { /* Frame size */
    110. // struct v4l2_frmsize_discrete discrete;
    111. struct v4l2_frmsize_discrete {
    112. __u32 width; /* Frame width [pixel] */
    113. __u32 height; /* Frame height [pixel] */
    114. }discrete;
    115. struct v4l2_frmsize_stepwise stepwise;
    116. };
    117. __u32 reserved[2]; /* Reserved space for future use */
    118. }frmsize;
    119. frmsize.index=0;
    120. frmsize.pixel_format=0x47504a4d;
    121. int t4=ioctl(fd,VIDIOC_ENUM_FRAMESIZES,&frmsize);
    122. if(t4<0){
    123. puts("frmsize error");
    124. }
    125. printf("%d\n",frmsize.discrete.width);
    126. return 0;
    127. }

  • 相关阅读:
    7.Redis常用配置命令
    【MAVEN依赖冲突如何解决】
    七、文件包含漏洞
    绕过Windows 11安装限制,Rufus带给你“奇迹”,低配电脑的春天
    uniapp问题归类
    Android maven could not get http://192.xx
    解决 Android WebView 多进程导致App崩溃
    新手教学系列——高效管理MongoDB数据:批量插入与更新的实战技巧
    数据冷热分离方案
    docker 部署 mysql8.0.30
  • 原文地址:https://blog.csdn.net/m0_59802969/article/details/134471728