TinyUVC 0.1.0
Header-only USB Video Class (UVC) device: turn an MCU into a USB webcam
Loading...
Searching...
No Matches
UVCPayload.h
Go to the documentation of this file.
1#pragma once
6#include "UVCConvert.h"
7#include "UVCDefs.h"
8
9namespace tinyuvc {
10
22 public:
26 void begin(const uint8_t *data, size_t len, PixelFormat fmt, bool fid,
27 bool withPts = false, uint32_t pts = 0) {
28 data_ = data;
29 fmt_ = fmt;
30 fid_ = fid;
31 withPts_ = withPts;
32 pts_ = pts;
33 pos_ = 0;
34 if (fmt == PixelFormat::MJPEG || fmt == PixelFormat::YUY2) {
35 outLen_ = len;
36 srcPairBytes_ = 0;
37 } else {
38 srcPairBytes_ = (uint8_t)(2 * bytesPerPixel(fmt));
39 outLen_ = (len / srcPairBytes_) * 4;
40 }
41 active_ = true;
42 }
43
45 uint8_t headerLength() const { return withPts_ ? 6 : 2; }
46
49 size_t fill(uint8_t *buf, size_t maxLen) {
50 if (!active_) return 0;
51 const uint8_t hdr = headerLength();
52 if (maxLen <= (size_t)hdr + 4) return 0;
53 size_t space = maxLen - hdr;
54 size_t remaining = outLen_ - pos_;
55 size_t n;
56 if (srcPairBytes_ == 0) {
57 n = remaining < space ? remaining : space;
58 memcpy(buf + hdr, data_ + pos_, n);
59 } else {
60 space &= ~(size_t)3; // whole pixel pairs only
61 n = remaining < space ? remaining : space;
62 const uint8_t *src = data_ + (pos_ / 4) * srcPairBytes_;
63 UVCConvert::toYUY2(fmt_, src, buf + hdr, n / 4);
64 }
65 pos_ += n;
66 const bool last = pos_ >= outLen_;
67 buf[0] = hdr;
68 buf[1] = (uint8_t)(uvc_spec::HDR_EOH | (fid_ ? uvc_spec::HDR_FID : 0) |
69 (last ? uvc_spec::HDR_EOF : 0) | (withPts_ ? uvc_spec::HDR_PTS : 0));
70 if (withPts_) le::put32(buf + 2, pts_);
71 if (last) active_ = false;
72 return hdr + n;
73 }
74
76 bool active() const { return active_; }
78 void abort() { active_ = false; }
80 size_t outputLength() const { return outLen_; }
81
82 private:
83 const uint8_t *data_ = nullptr;
84 size_t outLen_ = 0;
85 size_t pos_ = 0;
87 uint8_t srcPairBytes_ = 0;
88 bool fid_ = false;
89 bool withPts_ = false;
90 bool active_ = false;
91 uint32_t pts_ = 0;
92};
93
94} // namespace tinyuvc
Conversion of raw pixel formats to YUY2 (YUV 4:2:2, Y0 U Y1 V), pixel pair by pixel pair,...
USB Video Class 1.1 constants and the basic TinyUVC data types.
static void toYUY2(PixelFormat fmt, const uint8_t *src, uint8_t *dst, size_t pairs)
Definition UVCConvert.h:18
Produces the UVC payloads of one frame. Each payload starts with a 2 byte header (6 bytes with a pres...
Definition UVCPayload.h:21
bool active() const
true while the frame still has data to send
Definition UVCPayload.h:76
void abort()
Aborts the current frame.
Definition UVCPayload.h:78
size_t fill(uint8_t *buf, size_t maxLen)
Definition UVCPayload.h:49
size_t outputLength() const
Size of the frame data as it is sent (after any conversion)
Definition UVCPayload.h:80
void begin(const uint8_t *data, size_t len, PixelFormat fmt, bool fid, bool withPts=false, uint32_t pts=0)
Definition UVCPayload.h:26
uint8_t headerLength() const
Header length in bytes.
Definition UVCPayload.h:45
void put32(uint8_t *p, uint32_t v)
Definition UVCDefs.h:229
constexpr uint8_t HDR_EOF
Definition UVCDefs.h:62
constexpr uint8_t HDR_EOH
Definition UVCDefs.h:67
constexpr uint8_t HDR_PTS
Definition UVCDefs.h:63
constexpr uint8_t HDR_FID
Definition UVCDefs.h:61
TinyUVC: USB Video Class (UVC) device for Arduino and the desktop.
constexpr uint8_t bytesPerPixel(PixelFormat fmt)
Bytes per pixel of a raw pixel format (0 for JPEG)
Definition UVCDefs.h:103
PixelFormat
Pixel format of the frame data handed to UVCDevice::writeFrame(). MJPEG (a JPEG image) is sent as is....
Definition UVCDefs.h:92
@ MJPEG
one complete JPEG image (for UVCFormat::MJPEG)
@ YUY2
packed YUV 4:2:2, byte order Y0 U Y1 V