arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members

H.264 video encoder: wraps TinyH264Encoder (https://github.com/pschatzmann/TinyH264). Configure via setSize()/ setQp() (or setTargetBitrate() for rate control) same as TinyH264Encoder and setVideoFormat() for the raw picture format write() expects (I420 - planar Y/U/V concatenated in one buffer - by default), then feed raw pictures via write() - the resulting Annex-B bitstream is written to the Print target configured via setOutput() (e.g. a Muxer's addVideoFrame() by way of an adapter, or any other Print) instead of a caller-supplied buffer. More...

#include <CodecH264.h>

Inheritance diagram for H264Encoder:
VideoEncoder

Public Member Functions

 H264Encoder (int width=0, int height=0, int keyframeInterval=0)
 
 H264Encoder (Print &out)
 
 H264Encoder (VideoOutput &out)
 
bool begin () override
 
bool begin (bool reserveColorConversionScratch)
 See TinyH264Encoder::begin()
 
tinyh264::TinyH264Encoder< tinyh264::PSRAMAllocatorESP32< uint8_t > > & driver ()
 
void end () override
 See TinyH264Encoder::end()
 
int lastQp () const
 The QP actually used by the most recent write() call.
 
void setBitstreamBufferSize (size_t size)
 
void setKeyframeInterval (int frames)
 See TinyH264Encoder::setKeyframeInterval()
 
void setOutput (Print &out) override
 
void setOutput (VideoOutput &out)
 
void setPackedStride (int stride)
 See TinyH264Encoder::setPackedStride()
 
void setQp (int qp)
 See TinyH264Encoder::setQp()
 
void setSize (int width, int height)
 See TinyH264Encoder::setSize()
 
void setStride (int strideY, int strideC)
 See TinyH264Encoder::setStride()
 
void setTargetBitrate (int bitsPerSecond, double fps)
 See TinyH264Encoder::setTargetBitrate()
 
void setVideoFormat (VideoFormat format) override
 
VideoInfo videoInfo () override
 
size_t write (const uint8_t *data, size_t len) override
 

Static Public Attributes

static constexpr size_t kDefaultBitstreamBufferSize = 16384
 

Protected Member Functions

template<typename Fn >
size_t writeOut (Fn encodeInto)
 

Protected Attributes

Vector< uint8_t > bitstream
 
tinyh264::TinyH264Encoder< tinyh264::PSRAMAllocatorESP32< uint8_t > > encoder_
 
int height_ = 0
 
VideoFormat input_format = VideoFormat::I420
 
Printp_out = nullptr
 
VideoOutputp_out_video = nullptr
 
int width_ = 0
 

Static Protected Attributes

static constexpr int kMaxGrowRetries = 4
 

Detailed Description

H.264 video encoder: wraps TinyH264Encoder (https://github.com/pschatzmann/TinyH264). Configure via setSize()/ setQp() (or setTargetBitrate() for rate control) same as TinyH264Encoder and setVideoFormat() for the raw picture format write() expects (I420 - planar Y/U/V concatenated in one buffer - by default), then feed raw pictures via write() - the resulting Annex-B bitstream is written to the Print target configured via setOutput() (e.g. a Muxer's addVideoFrame() by way of an adapter, or any other Print) instead of a caller-supplied buffer.

The internal bitstream scratch buffer starts at kDefaultBitstreamBufferSize and grows (doubling, up to a few attempts) whenever an encode call comes back short - the same "call again with a bigger buffer" recovery TinyH264Encoder's own encodeFrame() family documents, done automatically so callers don't need to size it by hand. Use setBitstreamBufferSize() to pre-size it (e.g. to avoid the first frame paying for a grow-and-retry).

Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ H264Encoder() [1/3]

H264Encoder ( int  width = 0,
int  height = 0,
int  keyframeInterval = 0 
)
inline

◆ H264Encoder() [2/3]

H264Encoder ( Print out)
inline

◆ H264Encoder() [3/3]

H264Encoder ( VideoOutput out)
inline

Member Function Documentation

◆ begin() [1/2]

bool begin ( )
inlineoverridevirtual

See TinyH264Encoder::begin() - equivalent to begin(reserveColorConversionScratch=false); see that overload if you need to pass true.

Implements VideoEncoder.

◆ begin() [2/2]

bool begin ( bool  reserveColorConversionScratch)
inline

See TinyH264Encoder::begin()

◆ driver()

tinyh264::TinyH264Encoder< tinyh264::PSRAMAllocatorESP32< uint8_t > > & driver ( )
inline

Direct access to the wrapped TinyH264Encoder, e.g. for its width()/height()/y()/u()/v() reconstructed-picture accessors.

◆ end()

void end ( )
inlineoverridevirtual

See TinyH264Encoder::end()

Implements VideoEncoder.

◆ lastQp()

int lastQp ( ) const
inline

The QP actually used by the most recent write() call.

◆ setBitstreamBufferSize()

void setBitstreamBufferSize ( size_t  size)
inline

Pre-sizes the internal bitstream scratch buffer encodeFrame() and its color-format siblings encode into before writing the result to setOutput() - call before the first encode call to avoid paying for the automatic grow-and-retry on it (see class comment). Shrinks are ignored (the buffer never releases memory on its own).

◆ setKeyframeInterval()

void setKeyframeInterval ( int  frames)
inline

See TinyH264Encoder::setKeyframeInterval()

◆ setOutput() [1/2]

void setOutput ( Print out)
inlineoverridevirtual

Defines the target the encoded bitstream is written to, one write() call per write() call that actually produced output.

Implements VideoEncoder.

◆ setOutput() [2/2]

void setOutput ( VideoOutput out)
inline

◆ setPackedStride()

void setPackedStride ( int  stride)
inline

See TinyH264Encoder::setPackedStride()

◆ setQp()

void setQp ( int  qp)
inline

See TinyH264Encoder::setQp()

◆ setSize()

void setSize ( int  width,
int  height 
)
inline

See TinyH264Encoder::setSize()

◆ setStride()

void setStride ( int  strideY,
int  strideC 
)
inline

See TinyH264Encoder::setStride()

◆ setTargetBitrate()

void setTargetBitrate ( int  bitsPerSecond,
double  fps 
)
inline

See TinyH264Encoder::setTargetBitrate()

◆ setVideoFormat()

void setVideoFormat ( VideoFormat  format)
inlineoverridevirtual

Selects the raw picture format write() expects - VideoFormat::I420 (the default: planar Y/U/V, concatenated Y then U then V in one buffer, split via setSize()'s width/height), YUV422 (packed YUYV), RGB565/RGB666/RGB888 (packed) - see TinyH264Encoder's own encodeFrame()-family doc comments (h264_encoder.h) for exactly what each byte layout is. Any other VideoFormat is logged and ignored (previous format kept).

Implements VideoEncoder.

◆ videoInfo()

VideoInfo videoInfo ( )
inlineoverridevirtual

Reports the codec/dimensions of the bitstream written to setOutput()'s target - format is always VideoFormat::H264 (NOT setVideoFormat()'s raw input format); width/height match setSize() (0 if that was never called).

Implements VideoEncoder.

◆ write()

size_t write ( const uint8_t *  data,
size_t  len 
)
inlineoverridevirtual

Encodes one raw picture, in setVideoFormat()'s format (I420 by default), sized for setSize()'s width/height, and writes the resulting Annex-B bitstream to setOutput() - returns the number of bytes written (0 if nothing was, e.g. setSize()/setOutput() was never called, or len doesn't match the configured format/size). len is only used to validate the I420 case (the other formats are packed buffers TinyH264Encoder itself size-checks internally); see TinyH264Encoder's own encodeFrame()-family doc comments (h264_encoder.h) for exactly what each byte layout/size is.

Implements VideoEncoder.

◆ writeOut()

template<typename Fn >
size_t writeOut ( Fn  encodeInto)
inlineprotected

Member Data Documentation

◆ bitstream

Vector<uint8_t> bitstream
protected

◆ encoder_

tinyh264::TinyH264Encoder< tinyh264::PSRAMAllocatorESP32<uint8_t> > encoder_
protected

◆ height_

int height_ = 0
protected

◆ input_format

VideoFormat input_format = VideoFormat::I420
protected

◆ kDefaultBitstreamBufferSize

constexpr size_t kDefaultBitstreamBufferSize = 16384
staticconstexpr

Default size (bytes) of the internal bitstream scratch buffer - see setBitstreamBufferSize().

◆ kMaxGrowRetries

constexpr int kMaxGrowRetries = 4
staticconstexprprotected

Number of times to double the bitstream buffer and retry an undersized encode call before giving up (see class comment).

◆ p_out

Print* p_out = nullptr
protected

◆ p_out_video

VideoOutput* p_out_video = nullptr
protected

◆ width_

int width_ = 0
protected

The documentation for this class was generated from the following file: