5#include "TinyH264Decoder.h"
6#include "TinyH264Encoder.h"
20#ifndef H264_DEFAULT_ALLOCATOR
22#define H264_DEFAULT_ALLOCATOR tinyh264::PSRAMAllocatorESP32<uint8_t>
24#define H264_DEFAULT_ALLOCATOR tinyh264::StdAllocator<uint8_t>
77 for (
size_t i = 0; i + 3 < len; i++) {
78 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1) {
80 if ((header & 0x80) == 0) {
81 uint8_t nal_type = header & 0x1F;
82 if (nal_type >= 1 && nal_type <= 23)
return true;
112 LOGW(
"H264Decoder: unsupported VideoFormat %d", (
int)format);
190 tinyh264::TinyH264Decoder<H264_DEFAULT_ALLOCATOR>
decoder_;
196 static void onFrame(tinyh264::TinyH264Decoder<H264_DEFAULT_ALLOCATOR> &decoder,
216 size_t needed = w * h;
220 LOGE(
"H264Decoder: frame buffer allocation failed (RGB565)");
229 size_t needed = w * h * 3;
233 LOGE(
"H264Decoder: frame buffer allocation failed (RGB666)");
242 size_t needed = w * h * 3;
246 LOGE(
"H264Decoder: frame buffer allocation failed (RGB888)");
255 size_t needed = w * h + 2 * (w / 2) * (h / 2);
259 LOGE(
"H264Decoder: frame buffer allocation failed (I420)");
312 H264Encoder(
int width = 0,
int height = 0,
int keyframeInterval = 0)
313 :
encoder_(width, height, keyframeInterval) {
348 LOGW(
"H264Encoder: unsupported VideoFormat %d", (
int)format);
375 encoder_.setTargetBitrate(bitsPerSecond, fps);
379 encoder_.setKeyframeInterval(frames);
383 encoder_.setStride(strideY, strideC);
397 LOGE(
"H264Encoder: bitstream buffer allocation failed");
407 bool begin(
bool reserveColorConversionScratch) {
408 encoder_.begin(reserveColorConversionScratch);
431 if (len < ySize + 2 * cSize)
return 0;
433 const uint8_t *srcU = data + ySize;
434 const uint8_t *srcV = data + ySize + cSize;
436 return encoder_.encodeFrame(srcY, srcU, srcV, dst, cap);
441 return encoder_.encodeFrameYuv422(data, dst, cap);
445 return encoder_.encodeFrameRgb565((
const uint16_t *)data, dst, cap);
449 return encoder_.encodeFrameRgb666(data, dst, cap);
453 return encoder_.encodeFrameRgb888(data, dst, cap);
469 tinyh264::TinyH264Encoder<H264_DEFAULT_ALLOCATOR>
encoder_;
477 template <
typename Fn>
483 LOGE(
"H264Encoder: bitstream buffer allocation failed");
488 for (
int attempt = 0; n == 0 && attempt <
kMaxGrowRetries; ++attempt) {
491 LOGE(
"H264Encoder: bitstream buffer grow failed");
#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 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