8#include <libavcodec/avcodec.h>
9#include <libavformat/avformat.h>
10#include <libavutil/hwcontext.h>
11#include <libavutil/imgutils.h>
12#include <libavutil/mathematics.h>
13#include <libavutil/opt.h>
14#include <libswscale/swscale.h>
17#include <condition_variable>
26#ifdef MXWRITE_HAS_CUDA_COPY
27#include <cuda_runtime.h>
116 bool open(
const std::string &filename,
int width,
int height,
float fps,
const char *crf);
126 bool open(
const std::string &filename,
int width,
int height,
float fps,
const EncodeOptions &opts);
131 void write(
void *rgba_buffer);
154 bool write_cuda_rgba(
void *cuda_rgba_buffer,
int src_stride,
bool bottom_up =
false);
164 bool open_ts(
const std::string &filename,
int width,
int height,
float fps,
const char *crf);
174 bool open_ts(
const std::string &filename,
int width,
int height,
float fps,
const EncodeOptions &opts);
210 int64_t frame_count = 0;
211 double last_duration = 0.0;
212 AVFormatContext *format_ctx =
nullptr;
213 AVCodecContext *codec_ctx =
nullptr;
214 AVStream *stream =
nullptr;
215 AVFrame *frameYUV =
nullptr;
216 AVFrame *frameRGBA =
nullptr;
217 AVFrame *frame10 =
nullptr;
218 AVFrame *upload_sw_frame =
nullptr;
219 AVBufferRef *hw_device_ctx =
nullptr;
220 AVBufferRef *hw_frames_ctx =
nullptr;
221 bool use_hw_encode =
false;
222#ifdef MXWRITE_HAS_CUDA_COPY
225 cudaStream_t cuda_upload_stream =
nullptr;
227 bool hdr_output =
false;
228 EncodeOptions::HdrInfo hdr_info;
229 SwsContext *sws_ctx =
nullptr;
230 AVRational time_base;
232 void calculateFPSFraction(
float fps,
int &fps_num,
int &fps_den);
233 std::chrono::steady_clock::time_point recordingStart;
235 std::queue<AVFrame *> encode_queue;
238 static constexpr size_t MAX_QUEUE_SIZE = 120;
239 std::condition_variable queue_cv;
240 std::jthread encode_thread;
242 std::mutex queue_mutex{};
243 std::mutex writer_mutex{};
244 bool stop_requested =
false;
245 std::atomic<bool> block_when_full{
false};
248 bool openInternal(
const std::string &filename,
int w,
int h,
float fps,
const EncodeOptions &opts,
bool ts_mode);
250 bool initHardwareEncoding();
252 void startEncoderThread();
254 void stopEncoderThread();
256 void encodeLoop(std::stop_token stop_token);
258 void encodeAndWriteFrame(AVFrame *in_frame);
260 void drainEncoderPackets();
262 void releaseFrame(AVFrame *f);
270extern void transfer_audio(std::string_view sourceAudioFile, std::string_view destVideoFile);
277extern void cleanup_contexts(AVFormatContext *source_ctx, AVFormatContext *dest_ctx, AVFormatContext *output_ctx);
bool is_hardware_encode() const
True when the active encoder backend is hardware (NVENC).
Writer()=default
Construct a closed writer.
bool get_block_when_full() const
Check whether the encoder queue blocks instead of dropping frames.
bool write_cuda_rgba(void *cuda_rgba_buffer, int src_stride, bool bottom_up=false)
Queue a CUDA RGBA frame for encoding.
void set_block_when_full(bool value)
If true, producer threads block when the encoder queue is full instead of dropping frames....
~Writer()
Close the writer on destruction if it is still open.
bool open(const std::string &filename, int width, int height, float fps, const char *crf)
Open an output file using the legacy CRF string interface.
void write(void *rgba_buffer)
Queue a host RGBA frame for immediate-mode encoding.
bool open_ts(const std::string &filename, int width, int height, float fps, const char *crf)
Open a timestamp-based output stream using the legacy CRF string interface.
bool is_open() const
Check whether the writer is currently open.
double get_duration() const
Return the encoded duration in seconds.
void close()
Close the writer and flush pending packets.
int64_t get_frame_count() const
Return the number of frames submitted to the writer.
void write_hdr_rgba16(void *rgba16_buffer)
Write a 16-bit RGBA frame that is already PQ- or HLG-encoded in BT.2020 primaries (8 bytes/pixel: R16...
void write_ts(void *rgba_buffer)
Queue a host RGBA frame using capture timestamps.
void transfer_audio(std::string_view sourceAudioFile, std::string_view destVideoFile)
Copy audio from one video file to another.
void cleanup_contexts(AVFormatContext *source_ctx, AVFormatContext *dest_ctx, AVFormatContext *output_ctx)
Free FFmpeg format contexts used during transfer operations.
std::vector< uint8_t > content_light
Raw AVContentLightMetadata side-data bytes, or empty.
int color_range
AVColorRange value. Raw AVMasteringDisplayMetadata side-data bytes, or empty.
std::vector< uint8_t > mastering_display
int color_trc
AVColorTransferCharacteristic value.
bool enabled
Enables the HDR output path.
int color_primaries
AVColorPrimaries value.
int color_space
AVColorSpace value.
User-configurable video encoder quality options.
std::string codec
Encoder selection policy or concrete NVENC codec.
std::string tune
Optional tuning mode.
bool realtime
Enable low-latency settings.
struct EncodeOptions::HdrInfo hdr
int crf
Constant Rate Factor.
bool block_when_full
Block producer threads instead of dropping when the encoder queue is full.
std::string preset
Encoder preset name.
Queue entry that stores a frame pointer and its capture timestamp.
void * data
Pointer to RGBA frame data owned by the producer.
std::chrono::steady_clock::time_point capture_time
Capture time for timestamp-based encoding.