42#if defined(ARDUINO) && !defined(HOST)
53#include "../TinyUVC.h"
55#if !defined(TINYUVC_NO_JPEG) && defined(ARDUINO_ARCH_ESP32) && \
56 __has_include("img_converters.h")
57#include "img_converters.h"
58#define TINYUVC_JPEG_ESP32 1
60#define TINYUVC_JPEG_ESP32 0
63#if !defined(TINYUVC_NO_JPEG) && __has_include(<TinyJPEGEncoder.h>)
64#include <TinyJPEGEncoder.h>
65#define TINYUVC_JPEG_TINYJPEG 1
67#define TINYUVC_JPEG_TINYJPEG 0
71#define TINYUVC_HAS_JPEG (TINYUVC_JPEG_ESP32 || TINYUVC_JPEG_TINYJPEG)
76template <
typename RGB_T>
83 static constexpr size_t bytes = 2;
90 static constexpr size_t bytes = 3;
109template <
typename RGB_T = tinygpu::RGB565>
116#if TINYUVC_JPEG_TINYJPEG
117 if (encoder_ !=
nullptr) {
118 encoder_->~TinyJPEGEncoder();
128 std::initializer_list<uint8_t> fps,
129 uint32_t maxJpegSize = 0) {
131#if TINYUVC_JPEG_TINYJPEG
133 TINYUVC_LOGE(
"MJPEG wider than JE_MAX_WIDTH (%d)", (
int)JE_MAX_WIDTH);
138 TINYUVC_LOGE(
"MJPEG needs a JPEG encoder: include TinyJPEGEncoder.h");
141 if (maxJpegSize == 0) maxJpegSize = (uint32_t)
width *
height;
146 uint8_t fps, uint32_t maxJpegSize = 0) {
154 std::initializer_list<uint8_t> fps) {
183 for (
auto &b :
buffers_) b.setMemoryType(type);
191 bool writeData(tinygpu::ISurface<RGB_T> &surface)
override {
193 if (surface.width() !=
width() || surface.height() !=
height()) {
199 if (buf ==
nullptr)
return false;
207 bool writeData(tinygpu::ISurface<RGB_T> &surface,
size_t x,
217 const size_t w = std::min(surface.width(),
width() - x);
218 const size_t h = std::min(surface.height(),
height() - y);
219 const uint8_t *src = surface.data();
220 for (
size_t row = 0; row < h; row++) {
222 src + row * surface.width() * bpp, w * bpp);
235 if (buf ==
nullptr)
return false;
285#if TINYUVC_JPEG_TINYJPEG
286 tinyjpeg::TinyJPEGEncoder *encoder_ =
nullptr;
305 if (data ==
nullptr)
return 0;
310 memcpy(sink->
data + index, data, len);
311 if (index + len > sink->
len) sink->
len = index + len;
362 if (buf.
capacity() >= bytes)
return &buf;
364 if (buf.
reserve(bytes))
return &buf;
375 TINYUVC_LOGE(
"UVCDisplayDriver: no memory for %u bytes", (
unsigned)bytes);
381 const uint16_t w = (uint16_t)
width(), h = (uint16_t)
height();
382#if TINYUVC_JPEG_ESP32
386 if (buf ==
nullptr)
return false;
388 const bool ok = fmt2jpg_cb((uint8_t *)pixels, (
size_t)w * h * 2, w, h,
391 if (!ok || sink.overflow) {
393 sink.overflow ?
": increase maxJpegSize" :
"");
401#if TINYUVC_JPEG_TINYJPEG
402 JEPixelFormat format = JE_FMT_RGB888;
406 const size_t bytes = (size_t)w * h * 2;
412 for (
size_t i = 0; i < bytes; i += 2) {
413 const uint8_t hi = pixels[i];
414 dst[i] = pixels[i + 1];
418 format = JE_FMT_RGB565;
422 if (buf ==
nullptr)
return false;
423 if (encoder_ ==
nullptr) {
428 if (mem ==
nullptr) {
432 encoder_ =
new (mem) tinyjpeg::TinyJPEGEncoder();
437 encoder_->encodeJpg(pixels, w, h, format, buf->
data(), maxJpeg, len);
439 TINYUVC_LOGW(
"JPEG encoding failed (%d): increase maxJpegSize?", (
int)r);
Integration with TinyGPU (https://github.com/pschatzmann/TinyGPU): UVCDisplayDriver is a TinyGPU Disp...
#define TINYUVC_JPEG_TINYJPEG
Definition TinyGPU.h:67
#define TINYUVC_JPEG_ESP32
Definition TinyGPU.h:60
#define TINYUVC_LOGE(...)
Definition UVCLogger.h:75
#define TINYUVC_LOGW(...)
Definition UVCLogger.h:77
USB Video Class (UVC 1.1) device - a USB webcam.
Definition UVCDevice.h:95
uint8_t formatCount() const
Definition UVCDevice.h:340
bool isStreaming() const
true while the host is streaming (between commit and stop)
Definition UVCDevice.h:261
const UVCFormatSpec * formats() const
Formats as defined with addResolution()
Definition UVCDevice.h:339
UVCStreamSettings settings() const
Format, resolution and frame rate the host selected.
Definition UVCDevice.h:272
bool addResolution(UVCFormat format, uint16_t width, uint16_t height, uint8_t fps, uint32_t maxFrameSize=0)
Definition UVCDevice.h:107
bool writeFrame(const uint8_t *data, size_t len, PixelFormat fmt)
Sends a frame. Returns false (and the frame is not sent) if the device is not ready or the frame does...
Definition UVCDevice.h:287
bool waitReady(uint32_t timeoutMs)
Waits until isReady() (true) or the timeout or end of streaming (false)
Definition UVCDevice.h:323
bool isSending() const
Definition UVCDevice.h:269
bool begin()
Definition UVCDevice.h:198
bool isReady() const
Definition UVCDevice.h:265
TinyGPU DisplayDriver that sends the written surfaces as webcam frames to the USB host: uncompressed ...
Definition TinyGPU.h:110
bool setAddressWindow(size_t, size_t, size_t, size_t) override
Definition TinyGPU.h:475
uint32_t dropped_
Definition TinyGPU.h:321
void setMemoryType(UVCMemoryType type)
Where the frame buffers are allocated (default: PSRAM if available)
Definition TinyGPU.h:181
bool addResolution(UVCFormat format, uint16_t width, uint16_t height, uint8_t fps, uint32_t maxJpegSize=0)
Definition TinyGPU.h:145
uint32_t mismatched_
Definition TinyGPU.h:322
bool flush()
Sends the frame composed with writeData(surface, x, y)
Definition TinyGPU.h:229
uint32_t frames() const
Definition TinyGPU.h:265
size_t workBytes_
size of the frame in work_ (0: none)
Definition TinyGPU.h:282
UVCFrameBuffer swap_
RGB565 in TinyJPEG's byte order.
Definition TinyGPU.h:284
bool sizeChanged(size_t width, size_t height) const
true when the host selected a different size than width x height
Definition TinyGPU.h:246
uint32_t dropped() const
Definition TinyGPU.h:266
void end() override
Definition TinyGPU.h:161
UVCDevice & device()
Definition TinyGPU.h:272
bool send(UVCFrameBuffer &buf, size_t len, PixelFormat format)
Definition TinyGPU.h:454
size_t lastJpegSize_
Definition TinyGPU.h:323
void setJpegQuality(uint8_t quality)
JPEG quality 1..100 (default 80)
Definition TinyGPU.h:178
void setPacing(bool pacing)
Wait for the frame interval the host selected (default true)
Definition TinyGPU.h:172
bool sent_
Definition TinyGPU.h:317
bool writeData(tinygpu::ISurface< RGB_T > &surface) override
Definition TinyGPU.h:191
bool writeData(tinygpu::ISurface< RGB_T > &surface, size_t x, size_t y) override
Definition TinyGPU.h:207
size_t lastJpegSize() const
Size of the last JPEG.
Definition TinyGPU.h:270
bool addResolution(uint16_t width, uint16_t height, std::initializer_list< uint8_t > fps)
Definition TinyGPU.h:153
uint32_t timeoutMs_
Definition TinyGPU.h:318
bool addResolution(uint16_t width, uint16_t height, uint8_t fps)
Uncompressed (YUY2) resolution.
Definition TinyGPU.h:150
bool sendJpeg(const uint8_t *pixels)
Definition TinyGPU.h:380
Size selected() const
The resolution the host selected, or the first one.
Definition TinyGPU.h:333
bool begin() override
Starts the UVC device (if it is not running yet)
Definition TinyGPU.h:159
bool waitSent(uint32_t timeoutMs)
Waits until the device finished reading the previous frame.
Definition TinyGPU.h:256
static size_t jpegSinkWrite(void *arg, size_t index, const void *data, size_t len)
Definition TinyGPU.h:302
size_t height() const override
Definition TinyGPU.h:243
int next_
Definition TinyGPU.h:278
static constexpr bool useEsp32Jpeg()
esp32-camera's encoder: RGB565 only (in TinyGPU's byte order)
Definition TinyGPU.h:290
UVCFrameBuffer work_
frames composed by partial writes
Definition TinyGPU.h:280
bool addResolution(UVCFormat format, uint16_t width, uint16_t height, std::initializer_list< uint8_t > fps, uint32_t maxJpegSize=0)
Definition TinyGPU.h:127
uint8_t quality_
Definition TinyGPU.h:314
UVCDevice & uvc_
Definition TinyGPU.h:275
void setTimeout(uint32_t ms)
How long writeData() waits for the previous frame (default 1000 ms)
Definition TinyGPU.h:175
bool isJpeg() const
true if the host selected MJPEG
Definition TinyGPU.h:251
uint32_t frames_
Definition TinyGPU.h:320
size_t rawBytes() const
Definition TinyGPU.h:354
uint32_t lastUs_
Definition TinyGPU.h:319
UVCFrameBuffer * nextBuffer(size_t bytes)
Definition TinyGPU.h:360
UVCDisplayDriver(UVCDevice &uvc)
Definition TinyGPU.h:112
bool pacing_
Definition TinyGPU.h:315
uint32_t mismatched() const
Surfaces that were rejected because of their size.
Definition TinyGPU.h:268
size_t width() const override
Definition TinyGPU.h:242
bool dirty_
Definition TinyGPU.h:316
UVCFrameBuffer buffers_[2]
frames handed to the device: one is sent while the next is prepared
Definition TinyGPU.h:277
~UVCDisplayDriver() override
Definition TinyGPU.h:113
A growing byte buffer for one frame, allocated with UVCMemory (so in PSRAM where available)....
Definition UVCMemory.h:95
bool reserve(size_t size)
Makes sure that size bytes fit without a new allocation.
Definition UVCMemory.h:106
size_t capacity() const
Definition UVCMemory.h:145
void release()
Frees the memory.
Definition UVCMemory.h:136
void setMemoryType(UVCMemoryType type)
Definition UVCMemory.h:102
uint8_t * data()
Definition UVCMemory.h:142
static uint8_t * alloc(size_t size, UVCMemoryType type=UVCMemoryType::Auto)
Definition UVCMemory.h:51
static void free(void *p)
Definition UVCMemory.h:60
TinyUVC: USB Video Class (UVC) device for Arduino and the desktop.
UVCMemoryType
Definition UVCMemory.h:29
@ RAMOnly
internal RAM only
UVCFormat
Video format offered to the USB host.
Definition UVCDefs.h:78
@ MJPEG
Motion JPEG: every frame is a complete JPEG image.
@ YUY2
Uncompressed YUV 4:2:2 (YUYV), 16 bits per pixel.
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)
@ RGB888
24 bit, byte order R G B
@ RGB565_BE
16 bit RGB565, big endian (high byte first, esp32-camera)
Output of fmt2jpg_cb() into a frame buffer.
Definition TinyGPU.h:295
size_t len
Definition TinyGPU.h:298
bool overflow
Definition TinyGPU.h:299
size_t capacity
Definition TinyGPU.h:297
uint8_t * data
Definition TinyGPU.h:296
A resolution with the max frame size reported to the host.
Definition TinyGPU.h:326
uint32_t maxFrameSize
Definition TinyGPU.h:329
size_t width
Definition TinyGPU.h:327
size_t height
Definition TinyGPU.h:328
One resolution (UVC frame descriptor) with its supported frame rates.
Definition UVCDefs.h:148
uint16_t width
Definition UVCDefs.h:149
uint16_t height
Definition UVCDefs.h:150
uint32_t maxFrameSize
Max size of one frame in bytes, reported to the host for buffer sizing.
Definition UVCDefs.h:152
The format, resolution and frame rate the host selected.
Definition UVCDefs.h:201
uint8_t frameIndex
Definition UVCDefs.h:209
uint8_t formatIndex
1-based UVC indexes as used in the probe/commit control
Definition UVCDefs.h:208
UVCFormat format
Definition UVCDefs.h:202
uint32_t interval
Frame interval in 100ns units.
Definition UVCDefs.h:206
Maps a TinyGPU color type to the matching PixelFormat.
Definition TinyGPU.h:77