arduino-audio-tools
Loading...
Searching...
No Matches
MultiVideoDecoder.h
Go to the documentation of this file.
1#pragma once
2
5
6namespace audio_tools {
7
62 public:
76 void addDecoder(VideoDecoder &decoder) {
77 VideoFormat format = decoder.codecFormat();
78 for (int i = 0; i < decoders.size(); i++) {
79 if (decoders[i].format == format) {
80 decoders.erase(i);
81 break;
82 }
83 }
84 DecoderInfo info;
85 info.decoder = &decoder;
86 info.format = format;
87 decoders.push_back(info);
88 }
89
96 p_video_info_source = &source;
97 }
98
99 void setOutput(Print &out) override {
100 for (int i = 0; i < decoders.size(); i++) decoders[i].decoder->setOutput(out);
101 }
102
107 void setOutput(VideoOutput &out) override {
108 for (int i = 0; i < decoders.size(); i++) decoders[i].decoder->setOutput(out);
109 }
110
111 void setVideoFormat(VideoFormat format) override {
112 for (int i = 0; i < decoders.size(); i++)
113 decoders[i].decoder->setVideoFormat(format);
114 }
115
116 VideoInfo videoInfo() override {
117 return p_selected != nullptr ? p_selected->videoInfo() : VideoInfo{};
118 }
119
126
129 bool begin() override {
130 p_selected = nullptr;
132 is_first = true;
133 return true;
134 }
135
136 void end() override {
137 if (p_selected != nullptr) p_selected->end();
138 p_selected = nullptr;
140 is_first = true;
141 }
142
143 size_t write(const uint8_t *data, size_t len) override {
144 if (len == 0) return 0;
145 ensureSelected(data, len);
146 return p_selected != nullptr ? p_selected->write(data, len) : len;
147 }
148
149 void flush() override {
150 if (p_selected != nullptr) p_selected->flush();
151 }
152
153 void setSkipRender(bool skip) override {
154 if (p_selected != nullptr) p_selected->setSkipRender(skip);
155 }
156
166 bool isKeyFrame(const uint8_t *data, size_t len) override {
167 ensureSelected(data, len);
168 return p_selected != nullptr ? p_selected->isKeyFrame(data, len) : false;
169 }
170
171 uint64_t totalDecodeMs() const override {
172 return p_selected != nullptr ? p_selected->totalDecodeMs() : 0;
173 }
174
179
180 protected:
185
190 bool is_first = true;
191
192 void select(DecoderInfo &info) {
193 p_selected = info.decoder;
195 p_selected->begin();
196 LOGI("MultiVideoDecoder: selected decoder for format %d", (int)info.format);
197 }
198
205 void ensureSelected(const uint8_t *data, size_t len) {
206 if (!is_first) return;
207 is_first = false;
208 if (p_video_info_source != nullptr) {
210 for (int i = 0; i < decoders.size(); i++) {
211 if (decoders[i].format == source_format) {
212 LOGI("MultiVideoDecoder: format %d from VideoInfoSource",
213 (int)source_format);
214 select(decoders[i]);
215 break;
216 }
217 }
218 }
219 if (p_selected == nullptr) {
220 for (int i = 0; i < decoders.size(); i++) {
221 if (decoders[i].decoder->isValid(data, len)) {
222 select(decoders[i]);
223 break;
224 }
225 }
226 }
227 if (p_selected == nullptr) {
228 LOGE(
229 "MultiVideoDecoder: could not determine video format from "
230 "first %u bytes",
231 (unsigned)len);
232 }
233 }
234};
235
236} // namespace audio_tools
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition Arduino.h:56
Manages multiple VideoDecoders with automatic format detection - the video-side counterpart of MultiD...
Definition MultiVideoDecoder.h:61
void flush() override
Definition MultiVideoDecoder.h:149
void setOutput(Print &out) override
Defines the target each decoded picture is written to.
Definition MultiVideoDecoder.h:99
uint64_t totalDecodeMs() const override
Definition MultiVideoDecoder.h:171
VideoInfo videoInfo() override
Definition MultiVideoDecoder.h:116
VideoDecoder * p_selected
Definition MultiVideoDecoder.h:187
bool is_first
Definition MultiVideoDecoder.h:190
void ensureSelected(const uint8_t *data, size_t len)
Definition MultiVideoDecoder.h:205
VideoFormat selectedFormat() const
Definition MultiVideoDecoder.h:178
void end() override
Releases the decoder's resources.
Definition MultiVideoDecoder.h:136
size_t write(const uint8_t *data, size_t len) override
Definition MultiVideoDecoder.h:143
VideoFormat codecFormat() override
Definition MultiVideoDecoder.h:125
void addDecoder(VideoDecoder &decoder)
Definition MultiVideoDecoder.h:76
void setSkipRender(bool skip) override
Definition MultiVideoDecoder.h:153
Vector< DecoderInfo > decoders
Definition MultiVideoDecoder.h:186
void setVideoFormat(VideoFormat format) override
Definition MultiVideoDecoder.h:111
void setVideoInfoSource(VideoInfoSource &source)
Definition MultiVideoDecoder.h:95
bool isKeyFrame(const uint8_t *data, size_t len) override
Definition MultiVideoDecoder.h:166
VideoInfoSource * p_video_info_source
Definition MultiVideoDecoder.h:189
void select(DecoderInfo &info)
Definition MultiVideoDecoder.h:192
bool begin() override
Definition MultiVideoDecoder.h:129
void setOutput(VideoOutput &out) override
Definition MultiVideoDecoder.h:107
VideoFormat p_selected_format
Definition MultiVideoDecoder.h:188
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
Common interface for video decoders (e.g. H264Decoder, H264DecoderESP32S3 - CodecH264....
Definition CodecVideo.h:18
virtual VideoFormat codecFormat()=0
virtual void end()=0
Releases the decoder's resources.
virtual bool begin()=0
Initializes the decoder (allocates its picture buffers, etc).
virtual VideoInfo videoInfo()=0
Provider of VideoInfo (width/height/fps/format) for whoever needs to size buffers/panel setup against...
Definition VideoOutput.h:92
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 uint64_t totalDecodeMs() const
Definition VideoOutput.h:170
virtual void setSkipRender(bool skip)
Definition VideoOutput.h:121
virtual void flush()
Definition VideoOutput.h:113
virtual bool isKeyFrame(const uint8_t *data, size_t len)
Definition VideoOutput.h:135
VideoFormat
Video codec/pixel-format identifier, shared by two unrelated uses: the (single) video stream of a con...
Definition VideoOutput.h:29
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Definition MultiVideoDecoder.h:181
VideoFormat format
Definition MultiVideoDecoder.h:183
VideoDecoder * decoder
Definition MultiVideoDecoder.h:182
Basic video information (width/height/codec/frame size), analogous to AudioInfo - common to both Demu...
Definition VideoOutput.h:49
VideoFormat format
Video codec - VideoFormat::UNKNOWN if not (yet) determined.
Definition VideoOutput.h:58