7#include "TinyMPGDecoder.h"
8#include "TinyMPGEncoder.h"
11#ifndef MPG_DEFAULT_ALLOCATOR
13# define MPG_DEFAULT_ALLOCATOR tinympg::PSRAMAllocatorESP32<uint8_t>
15# define MPG_DEFAULT_ALLOCATOR tinympg::StdAllocator<uint8_t>
68 LOGW(
"MPGDecoder: unsupported VideoFormat %d", (
int)format);
88 size_t write(
const uint8_t *data,
size_t len)
override {
97 tinympg::TinyMPGDecoder<MPG_DEFAULT_ALLOCATOR> &
driver() {
return decoder_; }
100 tinympg::TinyMPGDecoder<MPG_DEFAULT_ALLOCATOR>
decoder_;
106 static void onFrame(tinympg::TinyMPGDecoder<MPG_DEFAULT_ALLOCATOR> &decoder,
111 void writeFrame(tinympg::TinyMPGDecoder<MPG_DEFAULT_ALLOCATOR> &decoder) {
114 size_t w = decoder.width();
115 size_t h = decoder.height();
120 size_t needed = w * h;
124 LOGE(
"MPGDecoder: frame buffer allocation failed (RGB565)");
133 size_t needed = w * h * 3;
137 LOGE(
"MPGDecoder: frame buffer allocation failed (RGB888)");
146 size_t needed = w * h * 3;
150 LOGE(
"MPGDecoder: frame buffer allocation failed (RGB666)");
159 size_t needed = w * h + 2 * (w / 2) * (h / 2);
163 LOGE(
"MPGDecoder: frame buffer allocation failed (I420)");
178 if (
p_out !=
nullptr) result =
p_out->
write((
const uint8_t *)data, len);
225 LOGW(
"MPGEncoder: unsupported VideoFormat %d", (
int)format);
252 LOGE(
"MPGEncoder: bitstream buffer allocation failed");
258 LOGE(
"MPGEncoder: invalid size - call setSize() first");
262 LOGE(
"MPGEncoder: failed to allocate bitstream buffer");
270 size_t write(
const uint8_t *data,
size_t len)
override {
273 LOGE(
"MPGEncoder: invalid size - call setSize() first");
277 LOGE(
"MPGEncoder: bitstream buffer not available");
284 size_t uv_size = y_size / 4;
285 size_t min_size = y_size + uv_size + uv_size;
286 if (len < min_size) {
287 LOGE(
"MPGEncoder: input buffer too small for I420 frame");
290 const uint8_t *srcY = data;
291 const uint8_t *srcU = data + y_size;
292 const uint8_t *srcV = data + y_size + uv_size;
293 return writeOut([&](uint8_t *dst,
size_t cap) {
294 return encoder_.encodeFrame(srcY, srcU, srcV, dst, cap);
298 return writeOut([&](uint8_t *dst,
size_t cap) {
299 return encoder_.encodeFrameYuv422(data, dst, cap);
302 return writeOut([&](uint8_t *dst,
size_t cap) {
303 return encoder_.encodeFrameRgb565((
const uint16_t *)data, dst, cap);
306 return writeOut([&](uint8_t *dst,
size_t cap) {
307 return encoder_.encodeFrameRgb666(data, dst, cap);
310 return writeOut([&](uint8_t *dst,
size_t cap) {
311 return encoder_.encodeFrameRgb888(data, dst, cap);
324 tinympg::TinyMPGEncoder<MPG_DEFAULT_ALLOCATOR>
encoder_;
332 template <
typename Fn>
336 for (
int attempt = 0; encoded == 0 && attempt <
kMaxGrowRetries; ++attempt) {
339 LOGE(
"MPGEncoder: bitstream buffer grow failed");
345 LOGE(
"MPGEncoder: encode failed (status=%d)", (
int)
encoder_.lastStatus());
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define LOGE(...)
Definition AudioLoggerIDF.h:30
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
VideoFormat
Video codec/pixel-format identifier, shared by two unrelated uses: the (single) video stream of a con...
Definition Video.h:43