arduino-audio-tools
Loading...
Searching...
No Matches
CodecH264ESP32P4.h
Go to the documentation of this file.
1#pragma once
2
4#include "H264EncoderP4.h"
5
21namespace audio_tools {
22
53template <typename Alloc = H264P4_DEFAULT_ALLOCATOR>
55 protected:
62 class DualOutputPrint : public Print {
63 public:
64 void setTargets(Print *out, VideoOutput *outVideo) {
65 p_out = out;
66 p_out_video = outVideo;
67 }
68 size_t write(uint8_t c) override { return write(&c, 1); }
69 size_t write(const uint8_t *data, size_t len) override {
70 size_t result = 0;
71 if (p_out != nullptr) result = p_out->write(data, len);
72 if (p_out_video != nullptr) result = p_out_video->write(data, len);
73 return result;
74 }
75
76 protected:
77 Print *p_out = nullptr;
79 };
80
81 public:
82 H264EncoderESP32P4() { config_ = encoder_.defaultConfig(); }
83
85
89
92 void setOutput(Print &out) override {
93 p_out = &out;
95 }
100
108 void setVideoFormat(VideoFormat format) override {
109 switch (format) {
111 config_.pic_type = ESP_H264_RAW_FMT_O_UYY_E_VYY;
112 input_format = format;
113 break;
115 config_.pic_type = ESP_H264_RAW_FMT_RGB565_LE;
116 input_format = format;
117 break;
119 config_.pic_type = ESP_H264_RAW_FMT_YUYV;
120 input_format = format;
121 break;
122 default:
123 LOGW("H264EncoderESP32P4: unsupported VideoFormat %d", (int)format);
124 break;
125 }
126 }
127
132 VideoInfo videoInfo() override {
133 VideoInfo info;
135 info.width = (uint16_t)config_.width;
136 info.height = (uint16_t)config_.height;
137 return info;
138 }
139
143 void setSize(int width, int height) {
144 config_.width = width;
145 config_.height = height;
146 }
149 void setFrameRate(int fps) { config_.fps = fps; }
152 void setBitrate(int bitsPerSecond) { config_.bitrate = bitsPerSecond; }
156 void setGop(int gop) { config_.gop = gop; }
160 void setQpRange(int qpMin, int qpMax) {
161 config_.qp_min = qpMin;
162 config_.qp_max = qpMax;
163 }
167 void setOutputBufferSize(size_t size) { config_.outBufferSize = size; }
168
170 bool begin() override { return encoder_.begin(config_); }
171
174 void end() override { encoder_.end(); }
175
185 size_t write(const uint8_t *data, size_t len) override {
186 if (p_out == nullptr && p_out_video == nullptr) return 0;
187 bool ok = false;
188 switch (input_format) {
189 case VideoFormat::I420: {
190 size_t ySize = (size_t)config_.width * config_.height;
191 size_t cSize = ySize / 4;
192 if (len < ySize + 2 * cSize) break;
193 const uint8_t *srcY = data;
194 const uint8_t *srcU = data + ySize;
195 const uint8_t *srcV = data + ySize + cSize;
196 ok = encoder_.encodeYUV420Planar(srcY, config_.width, srcU, srcV,
197 config_.width / 2, dual_out_);
198 break;
199 }
202 ok = encoder_.encode(data, len, dual_out_);
203 break;
204 default:
205 break;
206 }
207 return ok ? 1 : 0;
208 }
209
212 esp_h264_frame_type_t lastFrameType() const {
213 return encoder_.lastFrameType();
214 }
215
217 esp_h264_p4::H264EncoderP4<Alloc> &driver() { return encoder_; }
218
219 protected:
220 esp_h264_p4::H264EncoderP4<Alloc> encoder_;
221 typename esp_h264_p4::H264EncoderP4<Alloc>::Config config_;
222 Print *p_out = nullptr;
226};
227
228} // 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
Definition CodecH264ESP32P4.h:62
size_t write(uint8_t c) override
Definition CodecH264ESP32P4.h:68
VideoOutput * p_out_video
Definition CodecH264ESP32P4.h:78
size_t write(const uint8_t *data, size_t len) override
Definition CodecH264ESP32P4.h:69
Print * p_out
Definition CodecH264ESP32P4.h:77
void setTargets(Print *out, VideoOutput *outVideo)
Definition CodecH264ESP32P4.h:64
H.264 video encoder for ESP32-P4: wraps esp_h264_p4::H264EncoderP4 (https://github....
Definition CodecH264ESP32P4.h:54
DualOutputPrint dual_out_
Definition CodecH264ESP32P4.h:224
void setOutput(Print &out) override
Definition CodecH264ESP32P4.h:92
VideoInfo videoInfo() override
Definition CodecH264ESP32P4.h:132
esp_h264_frame_type_t lastFrameType() const
Definition CodecH264ESP32P4.h:212
H264EncoderESP32P4(VideoOutput &out)
Definition CodecH264ESP32P4.h:86
VideoFormat input_format
Definition CodecH264ESP32P4.h:225
void setSize(int width, int height)
Definition CodecH264ESP32P4.h:143
VideoOutput * p_out_video
Definition CodecH264ESP32P4.h:223
H264EncoderESP32P4(Print &out)
Definition CodecH264ESP32P4.h:84
void setQpRange(int qpMin, int qpMax)
Definition CodecH264ESP32P4.h:160
esp_h264_p4::H264EncoderP4< Alloc >::Config config_
Definition CodecH264ESP32P4.h:221
void end() override
Definition CodecH264ESP32P4.h:174
H264EncoderESP32P4()
Definition CodecH264ESP32P4.h:82
size_t write(const uint8_t *data, size_t len) override
Definition CodecH264ESP32P4.h:185
void setOutput(VideoOutput &out)
Definition CodecH264ESP32P4.h:96
esp_h264_p4::H264EncoderP4< Alloc > & driver()
Direct access to the wrapped esp_h264_p4::H264EncoderP4.
Definition CodecH264ESP32P4.h:217
Print * p_out
Definition CodecH264ESP32P4.h:222
void setVideoFormat(VideoFormat format) override
Definition CodecH264ESP32P4.h:108
void setFrameRate(int fps)
Definition CodecH264ESP32P4.h:149
esp_h264_p4::H264EncoderP4< Alloc > encoder_
Definition CodecH264ESP32P4.h:220
bool begin() override
Initializes the encoder - see esp_h264_p4::H264EncoderP4::begin().
Definition CodecH264ESP32P4.h:170
void setBitrate(int bitsPerSecond)
Definition CodecH264ESP32P4.h:152
void setGop(int gop)
Definition CodecH264ESP32P4.h:156
void setOutputBufferSize(size_t size)
Definition CodecH264ESP32P4.h:167
Common interface for video encoders (e.g. H264Encoder, H264EncoderESP32S3 - CodecH264....
Definition CodecVideo.h:89
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
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