MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MayaFlux::IO::VideoEncodeContext Class Reference

RAII owner of one video stream's encoder and pixel-format converter on the write path. More...

#include <VideoEncodeContext.hpp>

+ Collaboration diagram for MayaFlux::IO::VideoEncodeContext:

Public Member Functions

void close ()
 Release all owned resources.
 
bool drain (FFmpegMuxContext &mux)
 Flush all buffered frames from the encoder to the mux.
 
bool encode_frame (const uint8_t *src_data, size_t src_size, uint32_t src_width, uint32_t src_height, FFmpegMuxContext &mux)
 Encode one raw pixel frame into the mux context.
 
uint32_t height () const
 
bool is_valid () const
 True if codec, scaler, and scratch frame are all ready.
 
const std::string & last_error () const
 
bool open (FFmpegMuxContext &mux, uint32_t width, uint32_t height, double frame_rate, AVPixelFormat src_pixel_format, AVCodecID codec_id)
 Open the encoder and register a video stream in the mux context.
 
VideoEncodeContextoperator= (const VideoEncodeContext &)=delete
 
VideoEncodeContextoperator= (VideoEncodeContext &&)=delete
 
 VideoEncodeContext ()=default
 
 VideoEncodeContext (const VideoEncodeContext &)=delete
 
 VideoEncodeContext (VideoEncodeContext &&)=delete
 
uint32_t width () const
 
 ~VideoEncodeContext ()
 

Public Attributes

AVCodecContext * codec_context {}
 Owned; freed in destructor.
 
SwsContext * sws_context {}
 Owned; freed in destructor.
 

Private Member Functions

bool drain_packets (FFmpegMuxContext &mux)
 

Private Attributes

int m_cached_src_height { -1 }
 
int m_cached_src_width { -1 }
 
uint32_t m_height {}
 
std::string m_last_error
 
int64_t m_pts {}
 
AVFrame * m_scratch_frame {}
 Owned scratch buffer for encoder input.
 
AVPixelFormat m_src_pixel_fmt { AV_PIX_FMT_BGRA }
 
int m_src_src_bpp { 4 }
 
AVStream * m_stream {}
 Weak ref into FFmpegMuxContext; not owned.
 
int m_stream_index { -1 }
 
uint32_t m_width {}
 

Detailed Description

RAII owner of one video stream's encoder and pixel-format converter on the write path.

Encapsulates all video-stream-specific FFmpeg objects:

  • AVCodecContext for the selected or inferred encoder
  • SwsContext converting from the source pixel format (e.g. AV_PIX_FMT_BGRA, as delivered by the Vulkan swapchain readback) to the encoder's required format (typically AV_PIX_FMT_YUV420P for H.264/H.265)
  • AVFrame scratch buffer for sws_scale output
  • Monotonically increasing PTS counter

Does NOT own AVFormatContext — that belongs to FFmpegMuxContext.

Open sequence:

  1. FFmpegMuxContext::open()
  2. VideoEncodeContext::open() — adds stream, configures codec + sws
  3. FFmpegMuxContext::write_header()
  4. encode_frame() loop
  5. drain() — flushes encoder delay
  6. FFmpegMuxContext::close() — writes trailer

The source pixel format passed to open() must match the format delivered to every subsequent encode_frame() call. Swapchain BGRA readbacks map to AV_PIX_FMT_BGRA; RGBA readbacks to AV_PIX_FMT_RGBA.

Not copyable or movable; always stack- or member-allocated inside the owner (VideoFileWriter worker thread).

The associated FFmpegMuxContext must outlive this object.

Definition at line 44 of file VideoEncodeContext.hpp.


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