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
60 public:
74 void addDecoder(VideoDecoder &decoder) {
75 VideoFormat format = decoder.codecFormat();
76 for (int i = 0; i < decoders.size(); i++) {
77 if (decoders[i].format == format) {
78 decoders.erase(i);
79 break;
80 }
81 }
82 DecoderInfo info;
83 info.decoder = &decoder;
84 info.format = format;
85 decoders.push_back(info);
86 }
87
94 p_video_info_source = &source;
95 }
96
97 void setOutput(Print &out) override {
98 for (int i = 0; i < decoders.size(); i++) decoders[i].decoder->setOutput(out);
99 }
100
105 void setOutput(VideoOutput &out) override {
106 for (int i = 0; i < decoders.size(); i++) decoders[i].decoder->setOutput(out);
107 }
108
109 void setVideoFormat(VideoFormat format) override {
110 for (int i = 0; i < decoders.size(); i++)
111 decoders[i].decoder->setVideoFormat(format);
112 }
113
114 VideoInfo videoInfo() override {
115 return p_selected != nullptr ? p_selected->videoInfo() : VideoInfo{};
116 }
117
124
127 bool begin() override {
128 p_selected = nullptr;
130 is_first = true;
131 return true;
132 }
133
134 void end() override {
135 if (p_selected != nullptr) p_selected->end();
136 p_selected = nullptr;
138 is_first = true;
139 }
140
141 size_t write(const uint8_t *data, size_t len) override {
142 if (len == 0) return 0;
143 if (is_first) {
144 is_first = false;
145 if (p_video_info_source != nullptr) {
147 for (int i = 0; i < decoders.size(); i++) {
148 if (decoders[i].format == source_format) {
149 LOGI("MultiVideoDecoder: format %d from VideoInfoSource",
150 (int)source_format);
151 select(decoders[i]);
152 break;
153 }
154 }
155 }
156 if (p_selected == nullptr) {
157 for (int i = 0; i < decoders.size(); i++) {
158 if (decoders[i].decoder->isValid(data, len)) {
159 select(decoders[i]);
160 break;
161 }
162 }
163 }
164 if (p_selected == nullptr) {
165 LOGE(
166 "MultiVideoDecoder: could not determine video format from "
167 "first %u bytes",
168 (unsigned)len);
169 }
170 }
171 return p_selected != nullptr ? p_selected->write(data, len) : len;
172 }
173
174 void flush() override {
175 if (p_selected != nullptr) p_selected->flush();
176 }
177
178 void setSkipRender(bool skip) override {
179 if (p_selected != nullptr) p_selected->setSkipRender(skip);
180 }
181
182 bool isKeyFrame(const uint8_t *data, size_t len) override {
183 return p_selected != nullptr ? p_selected->isKeyFrame(data, len) : false;
184 }
185
189
190 protected:
195
200 bool is_first = true;
201
202 void select(DecoderInfo &info) {
203 p_selected = info.decoder;
205 p_selected->begin();
206 LOGI("MultiVideoDecoder: selected decoder for format %d", (int)info.format);
207 }
208};
209
210} // 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:59
void flush() override
Definition MultiVideoDecoder.h:174
void setOutput(Print &out) override
Defines the target each decoded picture is written to.
Definition MultiVideoDecoder.h:97
VideoInfo videoInfo() override
Definition MultiVideoDecoder.h:114
VideoDecoder * p_selected
Definition MultiVideoDecoder.h:197
bool is_first
Definition MultiVideoDecoder.h:200
VideoFormat selectedFormat() const
Definition MultiVideoDecoder.h:188
void end() override
Releases the decoder's resources.
Definition MultiVideoDecoder.h:134
size_t write(const uint8_t *data, size_t len) override
Definition MultiVideoDecoder.h:141
VideoFormat codecFormat() override
Definition MultiVideoDecoder.h:123
void addDecoder(VideoDecoder &decoder)
Definition MultiVideoDecoder.h:74
void setSkipRender(bool skip) override
Definition MultiVideoDecoder.h:178
Vector< DecoderInfo > decoders
Definition MultiVideoDecoder.h:196
void setVideoFormat(VideoFormat format) override
Definition MultiVideoDecoder.h:109
void setVideoInfoSource(VideoInfoSource &source)
Definition MultiVideoDecoder.h:93
bool isKeyFrame(const uint8_t *data, size_t len) override
Definition MultiVideoDecoder.h:182
VideoInfoSource * p_video_info_source
Definition MultiVideoDecoder.h:199
void select(DecoderInfo &info)
Definition MultiVideoDecoder.h:202
bool begin() override
Definition MultiVideoDecoder.h:127
void setOutput(VideoOutput &out) override
Definition MultiVideoDecoder.h:105
VideoFormat p_selected_format
Definition MultiVideoDecoder.h:198
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
Definition Video.h:195
virtual VideoInfo videoInfo()=0
Abstract class for video playback. This class is used to assemble a complete video frame in memory....
Definition Video.h:210
virtual size_t write(const uint8_t *data, size_t len)=0
virtual void setSkipRender(bool skip)
Definition Video.h:224
virtual void flush()
Definition Video.h:216
virtual bool isKeyFrame(const uint8_t *data, size_t len)
Definition Video.h:238
VideoFormat
Video codec/pixel-format identifier, shared by two unrelated uses: the (single) video stream of a con...
Definition Video.h:43
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Definition MultiVideoDecoder.h:191
VideoFormat format
Definition MultiVideoDecoder.h:193
VideoDecoder * decoder
Definition MultiVideoDecoder.h:192
Basic video information (width/height/codec/frame size), analogous to AudioInfo - common to both Demu...
Definition Video.h:121
VideoFormat format
Video codec - VideoFormat::UNKNOWN if not (yet) determined.
Definition Video.h:130