arduino-audio-tools
Loading...
Searching...
No Matches
Video.h
Go to the documentation of this file.
1#pragma once
7#include "stdint.h"
8
15namespace audio_tools {
16
22
27inline size_t videoFrameSizeBytes(VideoFormat format, uint16_t width,
28 uint16_t height) {
29 size_t px = (size_t)width * (size_t)height;
30 switch (format) {
34 return px * 3;
37 return px * 2;
39 return px + px / 2;
40 default:
41 return 0;
42 }
43}
44
52inline bool isH264KeyFrame(const uint8_t* data, size_t len) {
53 for (size_t i = 0; i + 3 < len; i++) {
54 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1) {
55 if ((data[i + 3] & 0x1F) == 5) return true; // IDR slice
56 i += 2;
57 }
58 }
59 return false;
60}
61
71inline bool isMpeg1KeyFrame(const uint8_t* data, size_t len) {
72 for (size_t i = 0; i + 5 < len; i++) {
73 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1 &&
74 data[i + 3] == 0x00) {
75 return ((data[i + 5] >> 3) & 0x07) == 1; // I-picture
76 }
77 }
78 return false;
79}
80
93 public:
94 virtual ~VideoFrameSource() = default;
99 virtual const uint8_t* nextFrame(size_t& len) = 0;
105 virtual VideoInfo videoInfo() = 0;
106
108 bool writeTo(Print& out) {
109 size_t len;
110 const uint8_t* frame = nextFrame(len);
111 if (frame == nullptr || len == 0) return false;
112 size_t written = out.write(frame, len);
113 return written = len;
114 }
115};
116
145 public:
147 VideoFrameMeter(Print& target) : p_target(&target) {}
148
157
158 size_t write(const uint8_t* data, size_t len) override {
159 uint32_t now = millis();
160 if (has_last_call) {
162 }
163 last_call_ms = now;
164 has_last_call = true;
165
166 // VideoOutput and Print are unrelated types (no common base), so the
167 // constructor that received a VideoOutput& only has somewhere to
168 // store it as p_target_video, not p_target - check that one first.
169 uint32_t forwardStart = millis();
170 size_t result = 0;
171 if (p_target_video != nullptr) {
172 result = p_target_video->write(data, len);
173 } else if (p_target != nullptr) {
174 result = p_target->write(data, len);
175 }
176 total_forward_ms += millis() - forwardStart;
177
178 count_++;
179 return result;
180 }
181
182 void flush() override {
183 if (p_target_video != nullptr) p_target_video->flush();
184 }
185 void setSkipRender(bool skip) override {
186 if (p_target_video != nullptr) p_target_video->setSkipRender(skip);
187 }
188
190 uint32_t count() const { return count_; }
191
192 float fpsMax() const {
193 return count_ > 0 && total_forward_ms > 0
194 ? (1000.0f * count_) / total_forward_ms
195 : 0.0f;
196 }
197
198 float fpsAvg() const {
199 return count_ > 1 && total_turnaround_ms > 0
200 ? (1000.0f * (count_ - 1)) / total_turnaround_ms
201 : 0.0f;
202 }
203
208 float timeWriteMs() const {
209 return count_ > 0 ? (float)total_forward_ms / count_ : 0.0f;
210 }
211
216 float timeTurnaroundMs() const {
217 return count_ > 1 ? (float)total_turnaround_ms / (count_ - 1) : 0.0f;
218 }
219
229 Print& asPrint() { return print_view_; }
230
231 protected:
232 class PrintView : public Print {
233 public:
234 explicit PrintView(VideoFrameMeter& meter) : meter_(meter) {}
235 size_t write(const uint8_t* data, size_t len) override {
236 return meter_.write(data, len);
237 }
238 size_t write(uint8_t b) override { return meter_.write(&b, 1); }
239
240 private:
241 VideoFrameMeter& meter_;
242 };
243
244 Print* p_target = nullptr;
246 uint32_t count_ = 0;
247 uint32_t last_call_ms = 0;
248 bool has_last_call = false;
249 uint32_t total_forward_ms = 0;
252};
253
254} // namespace audio_tools
Definition Arduino.h:56
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
size_t write(uint8_t b) override
Definition Video.h:238
PrintView(VideoFrameMeter &meter)
Definition Video.h:234
size_t write(const uint8_t *data, size_t len) override
Definition Video.h:235
Transparent write() wrapper that measures throughput/turnaround while forwarding every call unchanged...
Definition Video.h:144
void flush() override
Definition Video.h:182
uint32_t count() const
Total number of write() calls forwarded so far.
Definition Video.h:190
float timeWriteMs() const
Definition Video.h:208
uint32_t last_call_ms
Definition Video.h:247
bool has_last_call
Definition Video.h:248
uint32_t count_
Definition Video.h:246
float fpsAvg() const
Definition Video.h:198
size_t write(const uint8_t *data, size_t len) override
Definition Video.h:158
PrintView print_view_
Definition Video.h:251
VideoFrameMeter(VideoOutput &target)
Definition Video.h:156
uint32_t total_turnaround_ms
Definition Video.h:250
VideoFrameMeter(Print &target)
Definition Video.h:147
void setSkipRender(bool skip) override
Definition Video.h:185
VideoOutput * p_target_video
Definition Video.h:245
float timeTurnaroundMs() const
Definition Video.h:216
Print & asPrint()
Definition Video.h:229
Print * p_target
Definition Video.h:244
float fpsMax() const
Definition Video.h:192
uint32_t total_forward_ms
Definition Video.h:249
Pull-based provider of one already-encoded video/image frame at a time - e.g. wraps a camera capture ...
Definition Video.h:92
virtual const uint8_t * nextFrame(size_t &len)=0
bool writeTo(Print &out)
Write the frame to the indicated destination.
Definition Video.h:108
virtual ~VideoFrameSource()=default
virtual VideoInfo videoInfo()=0
Abstract class for video playback. This class is used to assemble a complete video frame in memory....
Definition VideoOutput.h:107
virtual size_t write(const uint8_t *data, size_t len)=0
virtual void setSkipRender(bool skip)
Definition VideoOutput.h:121
virtual void flush()
Definition VideoOutput.h:113
VideoFormat
Video codec/pixel-format identifier, shared by two unrelated uses: the (single) video stream of a con...
Definition VideoOutput.h:29
bool isH264KeyFrame(const uint8_t *data, size_t len)
True if the given Annex-B H.264 access unit contains an IDR slice NAL unit (nal_unit_type 5) - the re...
Definition Video.h:52
bool isMpeg1KeyFrame(const uint8_t *data, size_t len)
True if the given MPEG-1/2 video access unit's picture header declares picture_coding_type == 1 (I-pi...
Definition Video.h:71
StreamContentType
Which track write() feeds, for muxers (MuxerAVI, MuxerMP4) that double as a plain,...
Definition Video.h:21
size_t videoFrameSizeBytes(VideoFormat format, uint16_t width, uint16_t height)
Fixed per-frame size (bytes) for a raw/uncompressed VideoFormat at the given resolution - 0 for compr...
Definition Video.h:27
@ Audio
Definition Video.h:21
@ Video
Definition Video.h:21
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
uint32_t millis()
Returns the milliseconds since the start.
Definition Arduino.h:260
Basic video information (width/height/codec/frame size), analogous to AudioInfo - common to both Demu...
Definition VideoOutput.h:49