TinyUVC 0.1.0
Header-only USB Video Class (UVC) device: turn an MCU into a USB webcam
Loading...
Searching...
No Matches
UVCMemory.h
Go to the documentation of this file.
1#pragma once
14#include <stdint.h>
15#include <stdlib.h>
16#include <string.h>
17
18#if defined(ARDUINO_ARCH_ESP32)
19#include <Arduino.h>
20#include "esp_heap_caps.h"
21#elif defined(ARDUINO_ARCH_RP2040)
22#include <Arduino.h>
23#endif
24
25namespace tinyuvc {
26
29enum class UVCMemoryType {
30 Auto,
31 PSRAMOnly,
32 RAMOnly
33};
34
36class UVCMemory {
37 public:
39 static bool hasPSRAM() {
40#if defined(ARDUINO_ARCH_ESP32)
41 return psramFound();
42#elif defined(ARDUINO_ARCH_RP2040) && defined(RP2350_PSRAM_CS)
43 return rp2040.getPSRAMSize() > 0;
44#else
45 return false;
46#endif
47 }
48
51 static uint8_t *alloc(size_t size, UVCMemoryType type = UVCMemoryType::Auto) {
52 if (size == 0) return nullptr;
53 if (type != UVCMemoryType::RAMOnly) {
54 uint8_t *p = allocPSRAM(size);
55 if (p != nullptr || type == UVCMemoryType::PSRAMOnly) return p;
56 }
57 return allocRAM(size);
58 }
59
60 static void free(void *p) {
61 // ESP-IDF and arduino-pico release PSRAM with the normal free()
62 ::free(p);
63 }
64
65 protected:
66 static uint8_t *allocPSRAM(size_t size) {
67#if defined(ARDUINO_ARCH_ESP32)
68 if (!psramFound()) return nullptr;
69 return (uint8_t *)heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
70#elif defined(ARDUINO_ARCH_RP2040) && defined(RP2350_PSRAM_CS)
71 if (rp2040.getPSRAMSize() == 0) return nullptr;
72 return (uint8_t *)pmalloc(size);
73#else
74 (void)size;
75 return nullptr;
76#endif
77 }
78
79 static uint8_t *allocRAM(size_t size) {
80#if defined(ARDUINO_ARCH_ESP32)
81 // plain malloc() may hand out PSRAM as well (CONFIG_SPIRAM_USE_MALLOC)
82 return (uint8_t *)heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
83#else
84 return (uint8_t *)malloc(size);
85#endif
86 }
87};
88
96 public:
97 UVCFrameBuffer() = default;
98 UVCFrameBuffer(const UVCFrameBuffer &) = delete;
101
102 void setMemoryType(UVCMemoryType type) { type_ = type; }
103 UVCMemoryType memoryType() const { return type_; }
104
106 bool reserve(size_t size) {
107 if (size <= capacity_) return true;
108 uint8_t *p = UVCMemory::alloc(size, type_);
109 if (p == nullptr) return false;
110 if (size_ > 0) memcpy(p, data_, size_);
112 data_ = p;
113 capacity_ = size;
114 return true;
115 }
116
118 bool append(const uint8_t *data, size_t len) {
119 const size_t needed = size_ + len;
120 if (needed > capacity_) {
121 // grow by 50% to limit the number of copies; fall back to the
122 // exact size if that is too much
123 size_t cap = capacity_ + capacity_ / 2;
124 if (cap < needed) cap = needed;
125 if (cap < 4096) cap = 4096;
126 if (!reserve(cap) && !reserve(needed)) return false;
127 }
128 memcpy(data_ + size_, data, len);
129 size_ = needed;
130 return true;
131 }
132
133 void clear() { size_ = 0; }
134
136 void release() {
138 data_ = nullptr;
139 size_ = capacity_ = 0;
140 }
141
142 uint8_t *data() { return data_; }
143 const uint8_t *data() const { return data_; }
144 size_t size() const { return size_; }
145 size_t capacity() const { return capacity_; }
146 bool empty() const { return size_ == 0; }
147
148 protected:
149 uint8_t *data_ = nullptr;
150 size_t size_ = 0;
151 size_t capacity_ = 0;
153};
154
155} // namespace tinyuvc
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
~UVCFrameBuffer()
Definition UVCMemory.h:100
UVCMemoryType type_
Definition UVCMemory.h:152
size_t capacity() const
Definition UVCMemory.h:145
void release()
Frees the memory.
Definition UVCMemory.h:136
const uint8_t * data() const
Definition UVCMemory.h:143
size_t capacity_
Definition UVCMemory.h:151
void setMemoryType(UVCMemoryType type)
Definition UVCMemory.h:102
UVCFrameBuffer(const UVCFrameBuffer &)=delete
uint8_t * data_
Definition UVCMemory.h:149
UVCMemoryType memoryType() const
Definition UVCMemory.h:103
size_t size() const
Definition UVCMemory.h:144
UVCFrameBuffer & operator=(const UVCFrameBuffer &)=delete
uint8_t * data()
Definition UVCMemory.h:142
bool append(const uint8_t *data, size_t len)
Appends data; false if no memory is left (the buffer is unchanged)
Definition UVCMemory.h:118
void clear()
Definition UVCMemory.h:133
size_t size_
Definition UVCMemory.h:150
bool empty() const
Definition UVCMemory.h:146
Allocates frame sized buffers in PSRAM or RAM (see UVCMemoryType)
Definition UVCMemory.h:36
static uint8_t * allocPSRAM(size_t size)
Definition UVCMemory.h:66
static uint8_t * allocRAM(size_t size)
Definition UVCMemory.h:79
static bool hasPSRAM()
true if PSRAM is available
Definition UVCMemory.h:39
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
@ Auto
PSRAM if the board has it, else RAM.
@ PSRAMOnly
PSRAM only: the allocation fails without PSRAM.