arduino-audio-tools
Loading...
Searching...
No Matches
CodecH264ESP32S3.h
Go to the documentation of this file.
1#pragma once
2
4#include "H264Decoder.h"
5#include "H264Encoder.h"
6
17namespace audio_tools {
18
54template <typename Alloc = H264_DEFAULT_ALLOCATOR>
56 public:
58 config_ = decoder_.defaultConfig();
59 config_.output_format = ESP_H264_RAW_FMT_RGB565_LE;
60 // esp_h264::H264Decoder::defaultConfig() sizes both buffers for VGA
61 // (400KB input, ~450KB output) - both grow on demand via
62 // std::vector::resize() if a frame ever exceeds them anyway (see
63 // decode()/processDecodedFrame() in H264Decoder.h), so nothing
64 // breaks if these prove too small; they only pre-size to avoid a
65 // one-time reallocation. Default to something that fits this
66 // library's actual target (small-panel TFTs, QCIF/CIF-ish streams)
67 // instead of paying for VGA-sized buffers on every board regardless
68 // of what it's actually decoding - override via
69 // setInputBufferSize()/setOutputBufferSize() only if you know you
70 // need more (e.g. a larger panel) and want to skip that reallocation.
71 config_.input_buffer_size = 64 * 1024;
72 config_.output_buffer_size = 100 * 1024;
73 config_.frame_callback = [this](const uint8_t *frame, uint32_t w,
74 uint32_t h, esp_h264_raw_format_t fmt) {
75 if (p_out == nullptr && p_out_video == nullptr) return;
76 size_t bytes = (size_t)(w * h * ESP_H264_GET_BPP_BY_PIC_TYPE(fmt));
77 if (p_out != nullptr) p_out->write(frame, bytes);
78 if (p_out_video != nullptr) p_out_video->write(frame, bytes);
79 };
80 }
81
83
87
89
93 void setOutput(Print &out) override { p_out = &out; }
94 void setOutput(VideoOutput &out) override { p_out_video = &out; }
95
102 void setVideoFormat(VideoFormat format) override {
103 switch (format) {
105 config_.output_format = ESP_H264_RAW_FMT_RGB565_LE;
106 break;
108 config_.output_format = ESP_H264_RAW_FMT_I420;
109 break;
110 default:
111 LOGW("H264DecoderESP32S3: unsupported VideoFormat %d", (int)format);
112 break;
113 }
114 }
115
120 VideoInfo videoInfo() override {
121 VideoInfo info;
122 info.format = config_.output_format == ESP_H264_RAW_FMT_I420
125 uint32_t w = 0, h = 0;
126 if (decoder_.getFrameDimensions(w, h)) {
127 info.width = (uint16_t)w;
128 info.height = (uint16_t)h;
129 }
130 return info;
131 }
132
140 void setInputBufferSize(size_t size) { config_.input_buffer_size = size; }
141
148 void setOutputBufferSize(size_t size) { config_.output_buffer_size = size; }
149
151 bool begin() override { return decoder_.begin(config_); }
152
154 void end() override { decoder_.end(); }
155
159 size_t write(const uint8_t *data, size_t len) override {
160 decoder_.decode(data, len);
161 return len;
162 }
163
166 void flush() override {}
167
169 uint32_t frameCount() const { return decoder_.getFrameCount(); }
171 uint32_t decodeErrors() const { return decoder_.getDecodeErrors(); }
172
174 bool isKeyFrame(const uint8_t *data, size_t len) override {
175 return isH264KeyFrame(data, len);
176 }
177
179 esp_h264::H264Decoder<Alloc> &driver() { return decoder_; }
180
181 protected:
182 esp_h264::H264Decoder<Alloc> decoder_;
183 typename esp_h264::H264Decoder<Alloc>::Config config_;
184 Print *p_out = nullptr;
186};
187
216template <typename Alloc = H264_DEFAULT_ALLOCATOR>
218 protected:
224 class DualOutputPrint : public Print {
225 public:
226 void setTargets(Print *out, VideoOutput *outVideo) {
227 p_out = out;
228 p_out_video = outVideo;
229 }
230 size_t write(uint8_t c) override { return write(&c, 1); }
231 size_t write(const uint8_t *data, size_t len) override {
232 size_t result = 0;
233 if (p_out != nullptr) result = p_out->write(data, len);
234 if (p_out_video != nullptr) result = p_out_video->write(data, len);
235 return result;
236 }
237
238 protected:
239 Print *p_out = nullptr;
241 };
242
243 public:
244 H264EncoderESP32S3() { config_ = encoder_.defaultConfig(); }
245
247
251
254 void setOutput(Print &out) override {
255 p_out = &out;
257 }
262
269 void setVideoFormat(VideoFormat format) override {
270 switch (format) {
274 input_format = format;
275 break;
276 default:
277 LOGW("H264EncoderESP32S3: unsupported VideoFormat %d", (int)format);
278 break;
279 }
280 }
281
286 VideoInfo videoInfo() override {
287 VideoInfo info;
289 info.width = (uint16_t)config_.width;
290 info.height = (uint16_t)config_.height;
291 return info;
292 }
293
296 void setSize(int width, int height) {
297 config_.width = width;
298 config_.height = height;
299 }
302 void setFrameRate(int fps) { config_.fps = fps; }
305 void setBitrate(int bitsPerSecond) { config_.bitrate = bitsPerSecond; }
308 void setGop(int gop) { config_.gop = gop; }
312 void setQpRange(int qpMin, int qpMax) {
313 config_.qp_min = qpMin;
314 config_.qp_max = qpMax;
315 }
319 void setOutputBufferSize(size_t size) { config_.outBufferSize = size; }
320
322 bool begin() override { return encoder_.begin(config_); }
323
325 void end() override { encoder_.end(); }
326
336 size_t write(const uint8_t *data, size_t len) override {
337 if (p_out == nullptr && p_out_video == nullptr) return 0;
338 bool ok = false;
339 switch (input_format) {
341 ok = encoder_.encode(data, len, dual_out_);
342 break;
344 ok = encoder_.encodeRGB565(data, len, dual_out_);
345 break;
347 ok = encoder_.encodeYUV422(data, len, dual_out_);
348 break;
349 default:
350 break;
351 }
352 return ok ? 1 : 0;
353 }
354
358 esp_h264::H264Encoder<Alloc> &driver() { return encoder_; }
359
360 protected:
361 esp_h264::H264Encoder<Alloc> encoder_;
362 typename esp_h264::H264Encoder<Alloc>::Config config_;
363 Print *p_out = nullptr;
367};
368
369} // namespace audio_tools
#define LOGW(...)
Definition AudioLoggerIDF.h:29
Definition Arduino.h:56
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
H.264 video decoder for ESP32-S3: wraps esp_h264::H264Decoder (https://github.com/pschatzmann/ESP32S3...
Definition CodecH264ESP32S3.h:55
void flush() override
Definition CodecH264ESP32S3.h:166
void setOutput(Print &out) override
Definition CodecH264ESP32S3.h:93
VideoInfo videoInfo() override
Definition CodecH264ESP32S3.h:120
uint32_t decodeErrors() const
Number of decode errors since begin().
Definition CodecH264ESP32S3.h:171
VideoOutput * p_out_video
Definition CodecH264ESP32S3.h:185
H264DecoderESP32S3()
Definition CodecH264ESP32S3.h:57
uint32_t frameCount() const
Number of frames successfully decoded since begin().
Definition CodecH264ESP32S3.h:169
esp_h264::H264Decoder< Alloc > decoder_
Definition CodecH264ESP32S3.h:182
void end() override
Releases the decoder's resources - see esp_h264::H264Decoder::end().
Definition CodecH264ESP32S3.h:154
size_t write(const uint8_t *data, size_t len) override
Definition CodecH264ESP32S3.h:159
VideoFormat codecFormat() override
Definition CodecH264ESP32S3.h:88
esp_h264::H264Decoder< Alloc > & driver()
Direct access to the wrapped esp_h264::H264Decoder.
Definition CodecH264ESP32S3.h:179
H264DecoderESP32S3(VideoOutput &out)
Definition CodecH264ESP32S3.h:84
Print * p_out
Definition CodecH264ESP32S3.h:184
void setVideoFormat(VideoFormat format) override
Definition CodecH264ESP32S3.h:102
esp_h264::H264Decoder< Alloc >::Config config_
Definition CodecH264ESP32S3.h:183
bool isKeyFrame(const uint8_t *data, size_t len) override
See VideoOutput::isKeyFrame() - scans for an Annex-B IDR slice NAL.
Definition CodecH264ESP32S3.h:174
bool begin() override
Initializes the decoder - see esp_h264::H264Decoder::begin().
Definition CodecH264ESP32S3.h:151
void setOutput(VideoOutput &out) override
Definition CodecH264ESP32S3.h:94
H264DecoderESP32S3(Print &out)
Definition CodecH264ESP32S3.h:82
void setInputBufferSize(size_t size)
Definition CodecH264ESP32S3.h:140
void setOutputBufferSize(size_t size)
Definition CodecH264ESP32S3.h:148
Definition CodecH264ESP32S3.h:224
size_t write(uint8_t c) override
Definition CodecH264ESP32S3.h:230
VideoOutput * p_out_video
Definition CodecH264ESP32S3.h:240
size_t write(const uint8_t *data, size_t len) override
Definition CodecH264ESP32S3.h:231
Print * p_out
Definition CodecH264ESP32S3.h:239
void setTargets(Print *out, VideoOutput *outVideo)
Definition CodecH264ESP32S3.h:226
H.264 video encoder for ESP32-S3: wraps esp_h264::H264Encoder (https://github.com/pschatzmann/ESP32S3...
Definition CodecH264ESP32S3.h:217
DualOutputPrint dual_out_
Definition CodecH264ESP32S3.h:365
void setOutput(Print &out) override
Definition CodecH264ESP32S3.h:254
VideoInfo videoInfo() override
Definition CodecH264ESP32S3.h:286
VideoFormat input_format
Definition CodecH264ESP32S3.h:366
void setSize(int width, int height)
Definition CodecH264ESP32S3.h:296
VideoOutput * p_out_video
Definition CodecH264ESP32S3.h:364
void setQpRange(int qpMin, int qpMax)
Definition CodecH264ESP32S3.h:312
void end() override
Releases the encoder's resources - see esp_h264::H264Encoder::end().
Definition CodecH264ESP32S3.h:325
size_t write(const uint8_t *data, size_t len) override
Definition CodecH264ESP32S3.h:336
esp_h264::H264Encoder< Alloc > encoder_
Definition CodecH264ESP32S3.h:361
void setOutput(VideoOutput &out)
Definition CodecH264ESP32S3.h:258
Print * p_out
Definition CodecH264ESP32S3.h:363
H264EncoderESP32S3(VideoOutput &out)
Definition CodecH264ESP32S3.h:248
void setVideoFormat(VideoFormat format) override
Definition CodecH264ESP32S3.h:269
H264EncoderESP32S3()
Definition CodecH264ESP32S3.h:244
void setFrameRate(int fps)
Definition CodecH264ESP32S3.h:302
bool begin() override
Initializes the encoder - see esp_h264::H264Encoder::begin().
Definition CodecH264ESP32S3.h:322
void setBitrate(int bitsPerSecond)
Definition CodecH264ESP32S3.h:305
esp_h264::H264Encoder< Alloc >::Config config_
Definition CodecH264ESP32S3.h:362
void setGop(int gop)
Definition CodecH264ESP32S3.h:308
H264EncoderESP32S3(Print &out)
Definition CodecH264ESP32S3.h:246
esp_h264::H264Encoder< Alloc > & driver()
Definition CodecH264ESP32S3.h:358
void setOutputBufferSize(size_t size)
Definition CodecH264ESP32S3.h:319
Common interface for video decoders (e.g. H264Decoder, H264DecoderESP32S3 - CodecH264....
Definition CodecVideo.h:18
Common interface for video encoders (e.g. H264Encoder, H264EncoderESP32S3 - CodecH264....
Definition CodecVideo.h:89
Provider of VideoInfo (width/height/fps/format) for whoever needs to size buffers/panel setup against...
Definition VideoOutput.h:92
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
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
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Basic video information (width/height/codec/frame size), analogous to AudioInfo - common to both Demu...
Definition VideoOutput.h:49
uint16_t height
Frame height in pixels.
Definition VideoOutput.h:53
uint16_t width
Frame width in pixels.
Definition VideoOutput.h:51
VideoFormat format
Video codec - VideoFormat::UNKNOWN if not (yet) determined.
Definition VideoOutput.h:58