TinyUVC 0.1.0
Header-only USB Video Class (UVC) device: turn an MCU into a USB webcam
Loading...
Searching...
No Matches
UVCDescriptor.h
Go to the documentation of this file.
1#pragma once
9#include "UVCDefs.h"
10
11namespace tinyuvc {
12
17 uint8_t controlInterface = 0;
19 uint8_t endpointIn = 0x81;
21 uint16_t maxPacketSize = 64;
23 uint8_t stringIndex = 0;
25 const UVCFormatSpec *formats = nullptr;
26 uint8_t formatCount = 0;
27};
28
34 public:
36 static constexpr uint8_t kInterfaceCount = 2;
37
39 static uint16_t length(const UVCDescriptorParams &p) {
40 return build(p, nullptr, 0);
41 }
42
45 static uint16_t build(const UVCDescriptorParams &p, uint8_t *buf,
46 uint16_t bufLen) {
47 Writer w(buf, bufLen);
48 const uint8_t itfVC = p.controlInterface;
49 const uint8_t itfVS = (uint8_t)(itfVC + 1);
50
51 // --- Interface Association Descriptor
52 w.u8(8);
54 w.u8(itfVC);
55 w.u8(kInterfaceCount);
58 w.u8(0); // protocol undefined
59 w.u8(p.stringIndex);
60
61 // --- Standard VideoControl interface
62 interfaceDesc(w, itfVC, 0, uvc_spec::SC_VIDEOCONTROL, p.stringIndex);
63
64 // --- Class specific VideoControl interface
65 const uint16_t vcHeaderLen = 13; // with one baInterfaceNr
66 const uint16_t vcTotal = vcHeaderLen + kCameraTerminalLen + kOutputTerminalLen;
67 w.u8(vcHeaderLen);
70 w.u16(uvc_spec::BCD_UVC);
71 w.u16(vcTotal);
72 w.u32(kClockFrequency);
73 w.u8(1); // bInCollection
74 w.u8(itfVS); // baInterfaceNr(1)
75
76 // Camera (input) terminal: no controls
77 w.u8(kCameraTerminalLen);
82 w.u8(0); // bAssocTerminal
83 w.u8(0); // iTerminal
84 w.u16(0); // wObjectiveFocalLengthMin
85 w.u16(0); // wObjectiveFocalLengthMax
86 w.u16(0); // wOcularFocalLength
87 w.u8(3); // bControlSize
88 w.u8(0);
89 w.u8(0);
90 w.u8(0); // bmControls
91
92 // Output terminal (USB streaming), fed by the camera terminal
93 w.u8(kOutputTerminalLen);
98 w.u8(0); // bAssocTerminal
100 w.u8(0); // iTerminal
101
102 // --- Standard VideoStreaming interface: alt 0 with the bulk endpoint
103 interfaceDesc(w, itfVS, 1, uvc_spec::SC_VIDEOSTREAMING, 0);
104
105 // --- Class specific VideoStreaming interface
106 const uint8_t nFormats = p.formatCount;
107 const uint16_t vsHeaderLen = (uint16_t)(13 + nFormats);
108 uint16_t vsTotal = vsHeaderLen;
109 for (int f = 0; f < nFormats; f++) vsTotal += formatBlockLength(p.formats[f]);
110 w.u8((uint8_t)vsHeaderLen);
113 w.u8(nFormats);
114 w.u16(vsTotal);
115 w.u8(p.endpointIn);
116 w.u8(0); // bmInfo: no dynamic format change
118 w.u8(0); // bStillCaptureMethod
119 w.u8(0); // bTriggerSupport
120 w.u8(0); // bTriggerUsage
121 w.u8(1); // bControlSize
122 for (int f = 0; f < nFormats; f++) w.u8(0); // bmaControls
123
124 for (int f = 0; f < nFormats; f++) formatBlock(w, p.formats[f], (uint8_t)(f + 1));
125
126 // --- Bulk IN endpoint
127 w.u8(7);
129 w.u8(p.endpointIn);
130 w.u8(0x02); // bulk
131 w.u16(p.maxPacketSize);
132 w.u8(0); // bInterval
133
134 return w.ok() ? w.length() : 0;
135 }
136
138 static constexpr uint32_t kClockFrequency = 1000000;
139
140 private:
141 static constexpr uint8_t kCameraTerminalLen = 18;
142 static constexpr uint8_t kOutputTerminalLen = 9;
143 static constexpr uint8_t kMjpegFormatLen = 11;
144 static constexpr uint8_t kYuy2FormatLen = 27;
145 static constexpr uint8_t kColorMatchingLen = 6;
146
148 class Writer {
149 public:
150 Writer(uint8_t *buf, uint16_t cap) : buf_(buf), cap_(cap) {}
151 void u8(uint8_t v) {
152 if (buf_ != nullptr) {
153 if (len_ < cap_)
154 buf_[len_] = v;
155 else
156 overflow_ = true;
157 }
158 len_++;
159 }
160 void u16(uint16_t v) {
161 u8((uint8_t)v);
162 u8((uint8_t)(v >> 8));
163 }
164 void u32(uint32_t v) {
165 u16((uint16_t)v);
166 u16((uint16_t)(v >> 16));
167 }
168 uint16_t length() const { return len_; }
169 bool ok() const { return !overflow_; }
170
171 private:
172 uint8_t *buf_;
173 uint16_t cap_;
174 uint16_t len_ = 0;
175 bool overflow_ = false;
176 };
177
178 static void interfaceDesc(Writer &w, uint8_t num, uint8_t numEndpoints,
179 uint8_t subclass, uint8_t str) {
180 w.u8(9);
182 w.u8(num);
183 w.u8(0); // bAlternateSetting
184 w.u8(numEndpoints);
185 w.u8(uvc_spec::CC_VIDEO);
186 w.u8(subclass);
187 w.u8(0); // protocol: PC_PROTOCOL_UNDEFINED (UVC 1.1)
188 w.u8(str);
189 }
190
191 static uint16_t frameLength(const UVCFrameSpec &fr) {
192 return (uint16_t)(26 + 4 * fr.intervalCount);
193 }
194
195 static uint16_t formatBlockLength(const UVCFormatSpec &fmt) {
196 uint16_t len = fmt.format == UVCFormat::MJPEG ? kMjpegFormatLen : kYuy2FormatLen;
197 for (int i = 0; i < fmt.frameCount; i++) len += frameLength(fmt.frames[i]);
198 return (uint16_t)(len + kColorMatchingLen);
199 }
200
201 static void formatBlock(Writer &w, const UVCFormatSpec &fmt, uint8_t index) {
202 const bool mjpeg = fmt.format == UVCFormat::MJPEG;
203 if (mjpeg) {
204 w.u8(kMjpegFormatLen);
207 w.u8(index);
208 w.u8(fmt.frameCount);
209 w.u8(0); // bmFlags: no fixed size samples
210 w.u8(1); // bDefaultFrameIndex
211 w.u8(0); // bAspectRatioX
212 w.u8(0); // bAspectRatioY
213 w.u8(0); // bmInterlaceFlags
214 w.u8(0); // bCopyProtect
215 } else {
216 static const uint8_t kGuidYUY2[16] = {'Y', 'U', 'Y', '2', 0x00, 0x00,
217 0x10, 0x00, 0x80, 0x00, 0x00, 0xAA,
218 0x00, 0x38, 0x9B, 0x71};
219 w.u8(kYuy2FormatLen);
222 w.u8(index);
223 w.u8(fmt.frameCount);
224 for (uint8_t b : kGuidYUY2) w.u8(b);
225 w.u8(16); // bBitsPerPixel
226 w.u8(1); // bDefaultFrameIndex
227 w.u8(0); // bAspectRatioX
228 w.u8(0); // bAspectRatioY
229 w.u8(0); // bmInterlaceFlags
230 w.u8(0); // bCopyProtect
231 }
232
233 for (int i = 0; i < fmt.frameCount; i++) {
234 const UVCFrameSpec &fr = fmt.frames[i];
235 const uint32_t maxFps = intervalToFps(fr.minInterval());
236 const uint32_t minFps = intervalToFps(fr.maxInterval());
237 w.u8((uint8_t)frameLength(fr));
239 w.u8(mjpeg ? uvc_spec::VS_FRAME_MJPEG : uvc_spec::VS_FRAME_UNCOMPRESSED);
240 w.u8((uint8_t)(i + 1)); // bFrameIndex
241 w.u8(0); // bmCapabilities
242 w.u16(fr.width);
243 w.u16(fr.height);
244 w.u32(fr.maxFrameSize * 8 * (minFps ? minFps : 1)); // dwMinBitRate
245 w.u32(fr.maxFrameSize * 8 * (maxFps ? maxFps : 1)); // dwMaxBitRate
246 w.u32(fr.maxFrameSize); // dwMaxVideoFrameBufferSize
247 w.u32(fr.intervals[0]); // dwDefaultFrameInterval
248 w.u8(fr.intervalCount); // bFrameIntervalType: discrete
249 for (int j = 0; j < fr.intervalCount; j++) w.u32(fr.intervals[j]);
250 }
251
252 // Color matching: BT.709 primaries / transfer, SMPTE 170M (BT.601) matrix
253 w.u8(kColorMatchingLen);
256 w.u8(1);
257 w.u8(1);
258 w.u8(4);
259 }
260};
261
262} // namespace tinyuvc
USB Video Class 1.1 constants and the basic TinyUVC data types.
Writes the UVC function descriptors (everything that belongs into the configuration descriptor after ...
Definition UVCDescriptor.h:33
static constexpr uint8_t kInterfaceCount
Number of interfaces the function uses.
Definition UVCDescriptor.h:36
static uint16_t length(const UVCDescriptorParams &p)
Length in bytes of the descriptors for the given parameters.
Definition UVCDescriptor.h:39
static uint16_t build(const UVCDescriptorParams &p, uint8_t *buf, uint16_t bufLen)
Definition UVCDescriptor.h:45
static constexpr uint32_t kClockFrequency
dwClockFrequency reported in the headers: 1 MHz (PTS in microseconds)
Definition UVCDescriptor.h:138
constexpr uint16_t TT_STREAMING
Definition UVCDefs.h:39
constexpr uint8_t SC_VIDEOSTREAMING
Definition UVCDefs.h:20
constexpr uint8_t VS_FRAME_UNCOMPRESSED
Definition UVCDefs.h:34
constexpr uint8_t VS_FORMAT_MJPEG
Definition UVCDefs.h:35
constexpr uint8_t SC_VIDEOCONTROL
Definition UVCDefs.h:19
constexpr uint8_t VS_FRAME_MJPEG
Definition UVCDefs.h:36
constexpr uint8_t DESC_INTERFACE
Definition UVCDefs.h:23
constexpr uint8_t CC_VIDEO
Definition UVCDefs.h:18
constexpr uint16_t ITT_CAMERA
Definition UVCDefs.h:40
constexpr uint16_t BCD_UVC
bcdUVC reported in the VideoControl header
Definition UVCDefs.h:74
constexpr uint8_t VS_INPUT_HEADER
Definition UVCDefs.h:32
constexpr uint8_t DESC_IAD
Definition UVCDefs.h:25
constexpr uint8_t VS_COLORFORMAT
Definition UVCDefs.h:37
constexpr uint8_t VS_FORMAT_UNCOMPRESSED
Definition UVCDefs.h:33
constexpr uint8_t SC_VIDEO_INTERFACE_COLLECTION
Definition UVCDefs.h:21
constexpr uint8_t DESC_ENDPOINT
Definition UVCDefs.h:24
constexpr uint8_t VC_INPUT_TERMINAL
Definition UVCDefs.h:29
constexpr uint8_t VC_HEADER
Definition UVCDefs.h:28
constexpr uint8_t VC_OUTPUT_TERMINAL
Definition UVCDefs.h:30
constexpr uint8_t CS_INTERFACE
Definition UVCDefs.h:26
constexpr uint8_t ID_OUTPUT_TERMINAL
Definition UVCDefs.h:70
constexpr uint8_t ID_CAMERA_TERMINAL
Definition UVCDefs.h:69
TinyUVC: USB Video Class (UVC) device for Arduino and the desktop.
@ MJPEG
Motion JPEG: every frame is a complete JPEG image.
constexpr uint32_t intervalToFps(uint32_t interval)
Frame rate for a frame interval in 100ns units.
Definition UVCDefs.h:143
Input of UVCDescriptorBuilder::build()
Definition UVCDescriptor.h:14
uint8_t formatCount
Definition UVCDescriptor.h:26
uint8_t stringIndex
String index for the function and interface names (0 = none)
Definition UVCDescriptor.h:23
uint8_t controlInterface
Definition UVCDescriptor.h:17
const UVCFormatSpec * formats
The formats to offer.
Definition UVCDescriptor.h:25
uint16_t maxPacketSize
Max packet size of the bulk endpoint (64 full speed, 512 high speed)
Definition UVCDescriptor.h:21
uint8_t endpointIn
Address of the bulk IN endpoint (with bit 7 set)
Definition UVCDescriptor.h:19
One video format (UVC format descriptor) with its resolutions.
Definition UVCDefs.h:194