UVC修改相机分辨率帧率
熟悉UVC规范的都知道,UVC摄像头或相机可以通过UVC视频流接口支持多种分辨率和格式,但是一般都会有一个默认的视频数据格式和分辨率。
如本人手中的一个UVC摄像头,可以看到其支持多种格式和分辨率,我们可以通过potplayer来查看。
可以看到,其支持MJPG,YUY2两种各式和多种的分辨率。
当然,这些信息也是可通过USBTREEViewer来查看其视频流接口的描述符。
----- Video Streaming MJPEG Format Type Descriptor ----
bLength : 0x0B (11 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x06 (Format MJPEG)
bFormatIndex : 0x01 (1)
bNumFrameDescriptors : 0x05 (5)
bmFlags : 0x00 (Sample size is not fixed)
bDefaultFrameIndex : 0x01 (1)
bAspectRatioX : 0x00
bAspectRatioY : 0x00
bmInterlaceFlags : 0x00
D0 IL stream or variable: 0 (no)
D1 Fields per frame : 0 (2 fields)
D2 Field 1 first : 0 (no)
D3 Reserved : 0
D4..5 Field pattern : 0 (Field 1 only)
D6..7 Display Mode : 0 (Bob only)
bCopyProtect : 0x00 (No restrictions)
*!*ERROR: no Color Matching Descriptor for this format
Data (HexDump) : 0B 24 06 01 05 00 01 00 00 00 00 .$.........
----- Video Streaming MJPEG Frame Type Descriptor -----
---> This is the Default (optimum) Frame index
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x07 (MJPEG Frame Type)
bFrameIndex : 0x01
bmCapabilities : 0x00
wWidth : 0x0500 (1280)
wHeight : 0x02D0 (720)
dwMinBitRate : 0x1A5E0000 (442368000 bps -> 55.296 MB/s)
dwMaxBitRate : 0x1A5E0000 (442368000 bps -> 55.296 MB/s)
dwMaxVideoFrameBufferSize: 0x001C224D (1843789 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 07 01 00 00 05 D0 02 00 00 5E 1A 00 00 5E .$.........^...^
1A 4D 22 1C 00 15 16 05 00 01 15 16 05 00 .M"...........
----- Video Streaming MJPEG Frame Type Descriptor -----
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x07 (MJPEG Frame Type)
bFrameIndex : 0x02
bmCapabilities : 0x00
wWidth : 0x03C0 (960)
wHeight : 0x021C (540)
dwMinBitRate : 0x0ED4E000 (248832000 bps -> 31.104 MB/s)
dwMaxBitRate : 0x0ED4E000 (248832000 bps -> 31.104 MB/s)
dwMaxVideoFrameBufferSize: 0x000FD44D (1037389 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 07 02 00 C0 03 1C 02 00 E0 D4 0E 00 E0 D4 .$..............
0E 4D D4 0F 00 15 16 05 00 01 15 16 05 00 .M............
----- Video Streaming MJPEG Frame Type Descriptor -----
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x07 (MJPEG Frame Type)
bFrameIndex : 0x03
bmCapabilities : 0x00
wWidth : 0x0350 (848)
wHeight : 0x01E0 (480)
dwMinBitRate : 0x0BA54000 (195379200 bps -> 24.422 MB/s)
dwMaxBitRate : 0x0BA54000 (195379200 bps -> 24.422 MB/s)
dwMaxVideoFrameBufferSize: 0x000C6E4D (814669 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 07 03 00 50 03 E0 01 00 40 A5 0B 00 40 A5 .$...P....@...@.
0B 4D 6E 0C 00 15 16 05 00 01 15 16 05 00 .Mn...........
----- Video Streaming MJPEG Frame Type Descriptor -----
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x07 (MJPEG Frame Type)
bFrameIndex : 0x04
bmCapabilities : 0x00
wWidth : 0x0280 (640)
wHeight : 0x01E0 (480)
dwMinBitRate : 0x08CA0000 (147456000 bps -> 18.432 MB/s)
dwMaxBitRate : 0x08CA0000 (147456000 bps -> 18.432 MB/s)
dwMaxVideoFrameBufferSize: 0x0009624D (614989 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 07 04 00 80 02 E0 01 00 00 CA 08 00 00 CA .$..............
08 4D 62 09 00 15 16 05 00 01 15 16 05 00 .Mb...........
----- Video Streaming MJPEG Frame Type Descriptor -----
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x07 (MJPEG Frame Type)
bFrameIndex : 0x05
bmCapabilities : 0x00
wWidth : 0x0280 (640)
wHeight : 0x0168 (360)
dwMinBitRate : 0x06978000 (110592000 bps -> 13.824 MB/s)
dwMaxBitRate : 0x06978000 (110592000 bps -> 13.824 MB/s)
dwMaxVideoFrameBufferSize: 0x00070A4D (461389 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 07 05 00 80 02 68 01 00 80 97 06 00 80 97 .$.....h........
06 4D 0A 07 00 15 16 05 00 01 15 16 05 00 .M............
------- VS Uncompressed Format Type Descriptor --------
bLength : 0x1B (27 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x04 (Uncompressed Format Type)
bFormatIndex : 0x02 (2)
bNumFrameDescriptors : 0x07 (7 Frame Descriptors)
guidFormat : {32595559-0000-0010-8000-00AA00389B71} (YUY2)
bBitsPerPixel : 0x10 (16 bits per pixel)
bDefaultFrameIndex : 0x01 (Index 1)
bAspectRatioX : 0x00
bAspectRatioY : 0x00
bmInterlaceFlags : 0x00
D0 IL stream or variable: 0 (no)
D1 Fields per frame : 0 (2 fields)
D2 Field 1 first : 0 (no)
D3 Reserved : 0
D4..5 Field pattern : 0 (Field 1 only)
D6..7 Display Mode : 0 (Bob only)
bCopyProtect : 0x00 (No restrictions)
Data (HexDump) : 1B 24 04 02 07 59 55 59 32 00 00 10 00 80 00 00 .$...YUY2.......
AA 00 38 9B 71 10 01 00 00 00 00 ..8.q......
-------- VS Uncompressed Frame Type Descriptor --------
---> This is the Default (optimum) Frame index
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x05 (Uncompressed Frame Type)
bFrameIndex : 0x01
bmCapabilities : 0x00
wWidth : 0x0500 (1280)
wHeight : 0x02D0 (720)
dwMinBitRate : 0x08CA0000 (147456000 bps -> 18.432 MB/s)
dwMaxBitRate : 0x08CA0000 (147456000 bps -> 18.432 MB/s)
dwMaxVideoFrameBufferSize: 0x001C2000 (1843200 bytes)
dwDefaultFrameInterval : 0x000F4240 (100.0000 ms -> 10.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x000F4240 (100.0000 ms -> 10.000 fps)
Data (HexDump) : 1E 24 05 01 00 00 05 D0 02 00 00 CA 08 00 00 CA .$..............
08 00 20 1C 00 40 42 0F 00 01 40 42 0F 00 .. ..@B...@B..
-------- VS Uncompressed Frame Type Descriptor --------
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x05 (Uncompressed Frame Type)
bFrameIndex : 0x02
bmCapabilities : 0x00
wWidth : 0x0280 (640)
wHeight : 0x01E0 (480)
dwMinBitRate : 0x08CA0000 (147456000 bps -> 18.432 MB/s)
dwMaxBitRate : 0x08CA0000 (147456000 bps -> 18.432 MB/s)
dwMaxVideoFrameBufferSize: 0x00096000 (614400 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 05 02 00 80 02 E0 01 00 00 CA 08 00 00 CA .$..............
08 00 60 09 00 15 16 05 00 01 15 16 05 00 ..`...........
-------- VS Uncompressed Frame Type Descriptor --------
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x05 (Uncompressed Frame Type)
bFrameIndex : 0x03
bmCapabilities : 0x00
wWidth : 0x0280 (640)
wHeight : 0x0168 (360)
dwMinBitRate : 0x06978000 (110592000 bps -> 13.824 MB/s)
dwMaxBitRate : 0x06978000 (110592000 bps -> 13.824 MB/s)
dwMaxVideoFrameBufferSize: 0x00070800 (460800 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 05 03 00 80 02 68 01 00 80 97 06 00 80 97 .$.....h........
06 00 08 07 00 15 16 05 00 01 15 16 05 00 ..............
-------- VS Uncompressed Frame Type Descriptor --------
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x05 (Uncompressed Frame Type)
bFrameIndex : 0x04
bmCapabilities : 0x00
wWidth : 0x01A8 (424)
wHeight : 0x00F0 (240)
dwMinBitRate : 0x02E95000 (48844800 bps -> 6.105 MB/s)
dwMaxBitRate : 0x02E95000 (48844800 bps -> 6.105 MB/s)
dwMaxVideoFrameBufferSize: 0x00031B00 (203520 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 05 04 00 A8 01 F0 00 00 50 E9 02 00 50 E9 .$........P...P.
02 00 1B 03 00 15 16 05 00 01 15 16 05 00 ..............
-------- VS Uncompressed Frame Type Descriptor --------
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x05 (Uncompressed Frame Type)
bFrameIndex : 0x05
bmCapabilities : 0x00
wWidth : 0x0140 (320)
wHeight : 0x00F0 (240)
dwMinBitRate : 0x02328000 (36864000 bps -> 4.608 MB/s)
dwMaxBitRate : 0x02328000 (36864000 bps -> 4.608 MB/s)
dwMaxVideoFrameBufferSize: 0x00025800 (153600 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 05 05 00 40 01 F0 00 00 80 32 02 00 80 32 .$...@.....2...2
02 00 58 02 00 15 16 05 00 01 15 16 05 00 ..X...........
-------- VS Uncompressed Frame Type Descriptor --------
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x05 (Uncompressed Frame Type)
bFrameIndex : 0x06
bmCapabilities : 0x00
wWidth : 0x0140 (320)
wHeight : 0x00B4 (180)
dwMinBitRate : 0x01A5E000 (27648000 bps -> 3.456 MB/s)
dwMaxBitRate : 0x01A5E000 (27648000 bps -> 3.456 MB/s)
dwMaxVideoFrameBufferSize: 0x0001C200 (115200 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 05 06 00 40 01 B4 00 00 E0 A5 01 00 E0 A5 .$...@..........
01 00 C2 01 00 15 16 05 00 01 15 16 05 00 ..............
-------- VS Uncompressed Frame Type Descriptor --------
bLength : 0x1E (30 bytes)
bDescriptorType : 0x24 (Video Streaming Interface)
bDescriptorSubtype : 0x05 (Uncompressed Frame Type)
bFrameIndex : 0x07
bmCapabilities : 0x00
wWidth : 0x00A0 (160)
wHeight : 0x0078 (120)
dwMinBitRate : 0x008CA000 (9216000 bps -> 1.152 MB/s)
dwMaxBitRate : 0x008CA000 (9216000 bps -> 1.152 MB/s)
dwMaxVideoFrameBufferSize: 0x00009600 (38400 bytes)
dwDefaultFrameInterval : 0x00051615 (33.3333 ms -> 30.000 fps)
bFrameIntervalType : 0x01 (1 discrete frame interval supported)
adwFrameInterval[1] : 0x00051615 (33.3333 ms -> 30.000 fps)
Data (HexDump) : 1E 24 05 07 00 A0 00 78 00 00 A0 8C 00 00 A0 8C .$.....x........
00 00 96 00 00 15 16 05 00 01 15 16 05 00 ..............
可以看到,这是和通过PotPlayer枚举出来的是一样的,其中MJPG的1280x720x30的为默认分辨率。其对应的FormatIndex=1,FrameIndex=1,我们通过Potplayer打开其默认格式(推荐),通过BUSHOUND抓包来分析其USB视频流接口协商过程。
Length Phase Data Description
-------- ----- -------------------------------------------------- ----------------
CTL 21 01 00 05 00 02 01 00 SET CUR
1 OUT 02 .
CTL a1 81 00 01 01 00 1a 00 GET CUR
26 IN 00 00 01 01 80 1a 06 00 00 00 00 00 00 00 1e 00 ................
00 00 00 00 00 00 00 00 00 00 ..........
CTL 21 01 00 01 01 00 1a 00 SET CUR
26 OUT 00 00 01 01 15 16 05 00 00 00 00 00 00 00 1e 00 ................
00 00 00 00 00 00 00 00 00 00 ..........
CTL a1 81 00 01 01 00 1a 00 GET CUR
26 IN 00 00 01 01 15 16 05 00 00 00 00 00 00 00 1e 00 ................
00 00 4d 22 1c 00 00 0c 00 00 ..M"......
CTL a1 83 00 01 01 00 1a 00 GET MAX
26 IN 00 00 01 01 15 16 05 00 00 00 00 00 00 00 00 00 ................
00 00 4d 22 1c 00 00 0c 00 00 ..M"......
CTL a1 82 00 01 01 00 1a 00 GET MIN
26 IN 00 00 01 01 15 16 05 00 00 00 00 00 00 00 00 00 ................
00 00 4d 22 1c 00 00 0c 00 00 ..M"......
CTL 21 01 00 01 01 00 1a 00 SET CUR
26 OUT 00 00 01 01 15 16 05 00 00 00 00 00 00 00 1e 00 ................
00 00 4d 22 1c 00 00 00 00 00 ..M"......
CTL a1 81 00 01 01 00 1a 00 GET CUR
26 IN 00 00 01 01 15 16 05 00 00 00 00 00 00 00 1e 00 ................
00 00 4d 22 1c 00 00 0c 00 00 ..M"......
CTL 21 01 00 02 01 00 1a 00 SET CUR
26 OUT 00 00 01 01 15 16 05 00 00 00 00 00 00 00 1e 00 ................
00 00 4d 22 1c 00 00 0c 00 00 ..M"......
使用UVC视频协商工具 http://www.usbzh.com/tool/uvc.html 分析其最后的VS_COMMIT_CONTROL数据内容:
----------------------------------
USB中文网 http://www.usbzh.com
----------------------------------
============UVC1.0============
0x00, 0x00, UINT16 bmHint;
0x01, UINT8 bFormatIndex=1,
0x01, UINT8 bFrameIndex=1,
0x15, 0x16, 0x05, 0x00, UINT32 dwFrameInterval=333333,;33.3333,ms/帧
0x00, 0x00, UINT16 wKeyFrameRate=0,;缩格式中只有第1帧为关键帧;
0x00, 0x00, UINT16 wPFrameRate=0,;压缩格式P帧速率;
0x00, 0x00, UINT16 wCompQuality=0,,压缩质量1-10000;
0x1E, 0x00, UINT16 wCompWindowSize;30,,平均比特率控制的窗口大小;
0x00, 0x00, UINT16 wDelay;0,,内部视频流接口延迟(毫秒);
0x4D, 0x22, 0x1C, 0x00, UINT32 dwMaxVideoFrameSize;1843789,Bytes
0x00, 0x0C, 0x00, 0x00, UINT32 dwMaxPayloadTransferSize;3072,Bytes
// 26 bytes
可以看到,其bFormatIndex=1和bFrameIndex=1。
但是如果我们改变相机分辨率呢?
这个其实很简单,只选在重新打开摄像头时,选择其对应的格式和分辨率即可,这样USB视频驱动会自动与固件协商相应的格式进行通讯。
修改帧率也是一样的,只是VS_COMMIT_CONTROL的数据中的dwFrameInterval改成其指定的即可。
如我们想打开YUY2 1280x720x10的格式,其协VS_COMMIT_CONTROL数据内容如下:
Length Phase Data Descript
-------- ----- -------------------------------------------------- --------
CTL 21 01 00 05 00 02 01 00 SET CUR
1 OUT 02 .
CTL a1 81 00 01 01 00 1a 00 GET CUR
26 IN 00 00 01 01 15 16 05 00 00 00 00 00 00 00 1e 00 ........
00 00 4d 22 1c 00 00 0c 00 00 ..M"....
CTL 21 01 00 01 01 00 1a 00 SET CUR
26 OUT 00 00 02 01 40 42 0f 00 00 00 00 00 00 00 1e 00 ....@B..
00 00 4d 22 1c 00 00 00 00 00 ..M"....
CTL a1 81 00 01 01 00 1a 00 GET CUR
26 IN 00 00 02 01 40 42 0f 00 00 00 00 00 00 00 1e 00 ....@B..
00 00 00 20 1c 00 00 0c 00 00 ... ....
CTL a1 83 00 01 01 00 1a 00 GET MAX
26 IN 00 00 02 01 40 42 0f 00 00 00 00 00 00 00 00 00 ....@B..
00 00 00 20 1c 00 00 0c 00 00 ... ....
CTL a1 82 00 01 01 00 1a 00 GET MIN
26 IN 00 00 02 01 40 42 0f 00 00 00 00 00 00 00 00 00 ....@B..
00 00 00 20 1c 00 00 0c 00 00 ... ....
CTL 21 01 00 01 01 00 1a 00 SET CUR
26 OUT 00 00 02 01 40 42 0f 00 00 00 00 00 00 00 1e 00 ....@B..
00 00 00 20 1c 00 00 00 00 00 ... ....
CTL a1 81 00 01 01 00 1a 00 GET CUR
26 IN 00 00 02 01 40 42 0f 00 00 00 00 00 00 00 1e 00 ....@B..
00 00 00 20 1c 00 00 0c 00 00 ... ....
CTL 21 01 00 02 01 00 1a 00 SET CUR
26 OUT 00 00 02 01 40 42 0f 00 00 00 00 00 00 00 1e 00 ....@B..
00 00 00 20 1c 00 00 0c 00 00 ... ....
分析其协VS_COMMIT_CONTROL数据内容
----------------------------------
USB中文网 http://www.usbzh.com
----------------------------------
============UVC1.0============
0x00, 0x00, UINT16 bmHint;
0x02, UINT8 bFormatIndex=2,
0x01, UINT8 bFrameIndex=1,
0x40, 0x42, 0x0F, 0x00, UINT32 dwFrameInterval=1000000,;100,ms/帧
0x00, 0x00, UINT16 wKeyFrameRate=0,;缩格式中只有第1帧为关键帧;
0x00, 0x00, UINT16 wPFrameRate=0,;压缩格式P帧速率;
0x00, 0x00, UINT16 wCompQuality=0,,压缩质量1-10000;
0x1E, 0x00, UINT16 wCompWindowSize;30,,平均比特率控制的窗口大小;
0x00, 0x00, UINT16 wDelay;0,,内部视频流接口延迟(毫秒);
0x00, 0x20, 0x1C, 0x00, UINT32 dwMaxVideoFrameSize;1843200,Bytes
0x00, 0x0C, 0x00, 0x00, UINT32 dwMaxPayloadTransferSize;3072,Bytes
// 26 bytes
可以看到,bFormatIndex=2, bFrameIndex=1,并且UINT32 dwFrameInterval=1000000,;100,ms/帧,这个和我们的视频接口描述符中的地应一致。