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
48template <typename Alloc = H264_DEFAULT_ALLOCATOR>
50 public:
52 config_ = decoder_.defaultConfig();
53 config_.output_format = ESP_H264_RAW_FMT_RGB565_LE;
54 config_.frame_callback = [this](const uint8_t *frame, uint32_t w,
55 uint32_t h, esp_h264_raw_format_t fmt) {
56 if (p_out == nullptr && p_out_video == nullptr) return;
57 size_t bytes = (size_t)(w * h * ESP_H264_GET_BPP_BY_PIC_TYPE(fmt));
58 if (p_out != nullptr) p_out->write(frame, bytes);
59 if (p_out_video != nullptr) p_out_video->write(frame, bytes);
60 };
61 }
62
64
68
72 void setOutput(Print &out) override { p_out = &out; }
73 void setOutput(VideoOutput &out) { p_out_video = &out; }
74
81 void setVideoFormat(VideoFormat format) override {
82 switch (format) {
84 config_.output_format = ESP_H264_RAW_FMT_RGB565_LE;
85 break;
87 config_.output_format = ESP_H264_RAW_FMT_I420;
88 break;
89 default:
90 LOGW("H264DecoderESP32S3: unsupported VideoFormat %d", (int)format);
91 break;
92 }
93 }
94
99 VideoInfo videoInfo() override {
100 VideoInfo info;
101 info.format = config_.output_format == ESP_H264_RAW_FMT_I420
104 uint32_t w = 0, h = 0;
105 if (decoder_.getFrameDimensions(w, h)) {
106 info.width = (uint16_t)w;
107 info.height = (uint16_t)h;
108 }
109 return info;
110 }
111
115 void setInputBufferSize(size_t size) { config_.input_buffer_size = size; }
116
120 void setOutputBufferSize(size_t size) { config_.output_buffer_size = size; }
121
123 bool begin() override { return decoder_.begin(config_); }
124
126 void end() override { decoder_.end(); }
127
131 size_t write(const uint8_t *data, size_t len) override {
132 decoder_.decode(data, len);
133 return len;
134 }
135
138 void flush() override {}
139
141 uint32_t frameCount() const { return decoder_.getFrameCount(); }
143 uint32_t decodeErrors() const { return decoder_.getDecodeErrors(); }
144
146 esp_h264::H264Decoder<Alloc> &driver() { return decoder_; }
147
148 protected:
149 esp_h264::H264Decoder<Alloc> decoder_;
150 typename esp_h264::H264Decoder<Alloc>::Config config_;
151 Print *p_out = nullptr;
153};
154
183template <typename Alloc = H264_DEFAULT_ALLOCATOR>
185 protected:
191 class DualOutputPrint : public Print {
192 public:
193 void setTargets(Print *out, VideoOutput *outVideo) {
194 p_out = out;
195 p_out_video = outVideo;
196 }
197 size_t write(uint8_t c) override { return write(&c, 1); }
198 size_t write(const uint8_t *data, size_t len) override {
199 size_t result = 0;
200 if (p_out != nullptr) result = p_out->write(data, len);
201 if (p_out_video != nullptr) result = p_out_video->write(data, len);
202 return result;
203 }
204
205 protected:
206 Print *p_out = nullptr;
208 };
209
210 public:
211 H264EncoderESP32S3() { config_ = encoder_.defaultConfig(); }
212
214
218
221 void setOutput(Print &out) override {
222 p_out = &out;
224 }
229
236 void setVideoFormat(VideoFormat format) override {
237 switch (format) {
241 input_format = format;
242 break;
243 default:
244 LOGW("H264EncoderESP32S3: unsupported VideoFormat %d", (int)format);
245 break;
246 }
247 }
248
253 VideoInfo videoInfo() override {
254 VideoInfo info;
256 info.width = (uint16_t)config_.width;
257 info.height = (uint16_t)config_.height;
258 return info;
259 }
260
263 void setSize(int width, int height) {
264 config_.width = width;
265 config_.height = height;
266 }
269 void setFrameRate(int fps) { config_.fps = fps; }
272 void setBitrate(int bitsPerSecond) { config_.bitrate = bitsPerSecond; }
275 void setGop(int gop) { config_.gop = gop; }
279 void setQpRange(int qpMin, int qpMax) {
280 config_.qp_min = qpMin;
281 config_.qp_max = qpMax;
282 }
286 void setOutputBufferSize(size_t size) { config_.outBufferSize = size; }
287
289 bool begin() override { return encoder_.begin(config_); }
290
292 void end() override { encoder_.end(); }
293
303 size_t write(const uint8_t *data, size_t len) override {
304 if (p_out == nullptr && p_out_video == nullptr) return 0;
305 bool ok = false;
306 switch (input_format) {
308 ok = encoder_.encode(data, len, dual_out_);
309 break;
311 ok = encoder_.encodeRGB565(data, len, dual_out_);
312 break;
314 ok = encoder_.encodeYUV422(data, len, dual_out_);
315 break;
316 default:
317 break;
318 }
319 return ok ? 1 : 0;
320 }
321
325 esp_h264::H264Encoder<Alloc> &driver() { return encoder_; }
326
327 protected:
328 esp_h264::H264Encoder<Alloc> encoder_;
329 typename esp_h264::H264Encoder<Alloc>::Config config_;
330 Print *p_out = nullptr;
334};
335
336} // 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:49
void flush() override
Definition CodecH264ESP32S3.h:138
void setOutput(Print &out) override
Definition CodecH264ESP32S3.h:72
VideoInfo videoInfo() override
Definition CodecH264ESP32S3.h:99
uint32_t decodeErrors() const
Number of decode errors since begin().
Definition CodecH264ESP32S3.h:143
VideoOutput * p_out_video
Definition CodecH264ESP32S3.h:152
H264DecoderESP32S3()
Definition CodecH264ESP32S3.h:51
uint32_t frameCount() const
Number of frames successfully decoded since begin().
Definition CodecH264ESP32S3.h:141
esp_h264::H264Decoder< Alloc > decoder_
Definition CodecH264ESP32S3.h:149
void end() override
Releases the decoder's resources - see esp_h264::H264Decoder::end().
Definition CodecH264ESP32S3.h:126
size_t write(const uint8_t *data, size_t len) override
Definition CodecH264ESP32S3.h:131
void setOutput(VideoOutput &out)
Definition CodecH264ESP32S3.h:73
esp_h264::H264Decoder< Alloc > & driver()
Direct access to the wrapped esp_h264::H264Decoder.
Definition CodecH264ESP32S3.h:146
H264DecoderESP32S3(VideoOutput &out)
Definition CodecH264ESP32S3.h:65
Print * p_out
Definition CodecH264ESP32S3.h:151
void setVideoFormat(VideoFormat format) override
Definition CodecH264ESP32S3.h:81
esp_h264::H264Decoder< Alloc >::Config config_
Definition CodecH264ESP32S3.h:150
bool begin() override
Initializes the decoder - see esp_h264::H264Decoder::begin().
Definition CodecH264ESP32S3.h:123
H264DecoderESP32S3(Print &out)
Definition CodecH264ESP32S3.h:63
void setInputBufferSize(size_t size)
Definition CodecH264ESP32S3.h:115
void setOutputBufferSize(size_t size)
Definition CodecH264ESP32S3.h:120
Definition CodecH264ESP32S3.h:191
size_t write(uint8_t c) override
Definition CodecH264ESP32S3.h:197
VideoOutput * p_out_video
Definition CodecH264ESP32S3.h:207
size_t write(const uint8_t *data, size_t len) override
Definition CodecH264ESP32S3.h:198
Print * p_out
Definition CodecH264ESP32S3.h:206
void setTargets(Print *out, VideoOutput *outVideo)
Definition CodecH264ESP32S3.h:193
H.264 video encoder for ESP32-S3: wraps esp_h264::H264Encoder (https://github.com/pschatzmann/ESP32S3...
Definition CodecH264ESP32S3.h:184
DualOutputPrint dual_out_
Definition CodecH264ESP32S3.h:332
void setOutput(Print &out) override
Definition CodecH264ESP32S3.h:221
VideoInfo videoInfo() override
Definition CodecH264ESP32S3.h:253
VideoFormat input_format
Definition CodecH264ESP32S3.h:333
void setSize(int width, int height)
Definition CodecH264ESP32S3.h:263
VideoOutput * p_out_video
Definition CodecH264ESP32S3.h:331
void setQpRange(int qpMin, int qpMax)
Definition CodecH264ESP32S3.h:279
void end() override
Releases the encoder's resources - see esp_h264::H264Encoder::end().
Definition CodecH264ESP32S3.h:292
size_t write(const uint8_t *data, size_t len) override
Definition CodecH264ESP32S3.h:303
esp_h264::H264Encoder< Alloc > encoder_
Definition CodecH264ESP32S3.h:328
void setOutput(VideoOutput &out)
Definition CodecH264ESP32S3.h:225
Print * p_out
Definition CodecH264ESP32S3.h:330
H264EncoderESP32S3(VideoOutput &out)
Definition CodecH264ESP32S3.h:215
void setVideoFormat(VideoFormat format) override
Definition CodecH264ESP32S3.h:236
H264EncoderESP32S3()
Definition CodecH264ESP32S3.h:211
void setFrameRate(int fps)
Definition CodecH264ESP32S3.h:269
bool begin() override
Initializes the encoder - see esp_h264::H264Encoder::begin().
Definition CodecH264ESP32S3.h:289
void setBitrate(int bitsPerSecond)
Definition CodecH264ESP32S3.h:272
esp_h264::H264Encoder< Alloc >::Config config_
Definition CodecH264ESP32S3.h:329
void setGop(int gop)
Definition CodecH264ESP32S3.h:275
H264EncoderESP32S3(Print &out)
Definition CodecH264ESP32S3.h:213
esp_h264::H264Encoder< Alloc > & driver()
Definition CodecH264ESP32S3.h:325
void setOutputBufferSize(size_t size)
Definition CodecH264ESP32S3.h:286
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:59
Abstract class for video playback. This class is used to assemble a complete video frame in memory....
Definition Video.h:192
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 Video.h:43
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 Video.h:102
uint16_t height
Frame height in pixels.
Definition Video.h:106
uint16_t width
Frame width in pixels.
Definition Video.h:104
VideoFormat format
Video codec - VideoFormat::UNKNOWN if not (yet) determined.
Definition Video.h:111