MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxwrite-nanobind.hpp
Go to the documentation of this file.
1#include <nanobind/nanobind.h>
2#include <nanobind/ndarray.h>
3#include <nanobind/stl/chrono.h>
4#include <nanobind/stl/string.h>
5#include <nanobind/stl/string_view.h>
6#include <nanobind/stl/vector.h>
7
8#include "mxwrite.hpp"
9
10namespace nb = nanobind;
11
12NB_MODULE(mxwrite_ext, m) {
13 m.doc() = "Python bindings for MXWrite FFmpeg video writer";
14
15 nb::class_<Frame_Data>(m, "FrameData").def(nb::init<>()).def_rw("capture_time", &Frame_Data::capture_time).def_prop_rw("data", [](Frame_Data &f) { return nb::capsule(f.data); }, [](Frame_Data &f, nb::capsule c) { f.data = c.data(); });
16
17 nb::class_<EncoderInfo>(m, "EncoderInfo").def(nb::init<>()).def_rw("name", &EncoderInfo::name).def_rw("long_name", &EncoderInfo::long_name).def_rw("codec_name", &EncoderInfo::codec_name).def_rw("pixel_formats", &EncoderInfo::pixel_formats).def_rw("hardware", &EncoderInfo::hardware).def_rw("experimental", &EncoderInfo::experimental);
18
19 nb::class_<EncoderOptionInfo>(m, "EncoderOptionInfo").def(nb::init<>()).def_rw("name", &EncoderOptionInfo::name).def_rw("type", &EncoderOptionInfo::type).def_rw("default_value", &EncoderOptionInfo::default_value).def_rw("minimum", &EncoderOptionInfo::minimum).def_rw("maximum", &EncoderOptionInfo::maximum).def_rw("choices", &EncoderOptionInfo::choices).def_rw("help", &EncoderOptionInfo::help);
20
21 m.def("available_video_encoders", &available_video_encoders);
22 m.def("video_encoder_options", &video_encoder_options, nb::arg("encoder_name"));
23
24 nb::class_<EncodeOptions::HdrInfo>(m, "HdrInfo").def(nb::init<>()).def_rw("enabled", &EncodeOptions::HdrInfo::enabled).def_rw("color_primaries", &EncodeOptions::HdrInfo::color_primaries).def_rw("color_trc", &EncodeOptions::HdrInfo::color_trc).def_rw("color_space", &EncodeOptions::HdrInfo::color_space).def_rw("color_range", &EncodeOptions::HdrInfo::color_range).def_rw("mastering_display", &EncodeOptions::HdrInfo::mastering_display).def_rw("content_light", &EncodeOptions::HdrInfo::content_light);
25
26 nb::class_<EncodeOptions>(m, "EncodeOptions").def(nb::init<>()).def_rw("preset", &EncodeOptions::preset).def_rw("tune", &EncodeOptions::tune).def_rw("crf", &EncodeOptions::crf).def_rw("bit_rate", &EncodeOptions::bit_rate).def_rw("codec", &EncodeOptions::codec).def_rw("ffmpeg_options", &EncodeOptions::ffmpeg_options).def_rw("realtime", &EncodeOptions::realtime).def_rw("block_when_full", &EncodeOptions::block_when_full).def_rw("hdr", &EncodeOptions::hdr);
27
28 nb::class_<Writer>(m, "Writer")
29 .def(nb::init<>())
30
31 .def("open", nb::overload_cast<const std::string &, int, int, float, const char *>(&Writer::open), nb::arg("filename"), nb::arg("width"), nb::arg("height"), nb::arg("fps"), nb::arg("crf"))
32 .def("open", nb::overload_cast<const std::string &, int, int, float, const EncodeOptions &>(&Writer::open), nb::arg("filename"), nb::arg("width"), nb::arg("height"), nb::arg("fps"), nb::arg("opts"))
33
34 .def("open_ts", nb::overload_cast<const std::string &, int, int, float, const char *>(&Writer::open_ts), nb::arg("filename"), nb::arg("width"), nb::arg("height"), nb::arg("fps"), nb::arg("crf"))
35 .def("open_ts", nb::overload_cast<const std::string &, int, int, float, const EncodeOptions &>(&Writer::open_ts), nb::arg("filename"), nb::arg("width"), nb::arg("height"), nb::arg("fps"), nb::arg("opts"))
36
37 .def(
38 "write",
39 [](Writer &w, nb::ndarray<uint8_t, nb::c_contig, nb::device::cpu> arr) { w.write(arr.data()); },
40 nb::arg("rgba_buffer"))
41
42 .def(
43 "write_at_pts",
44 [](Writer &w, nb::ndarray<uint8_t, nb::c_contig, nb::device::cpu> arr, int64_t pts) { w.write_at_pts(arr.data(), pts); },
45 nb::arg("rgba_buffer"),
46 nb::arg("pts"))
47
48 .def(
49 "write_ts",
50 [](Writer &w, nb::ndarray<uint8_t, nb::c_contig, nb::device::cpu> arr) { w.write_ts(arr.data()); },
51 nb::arg("rgba_buffer"))
52
53 .def(
54 "write_hdr_rgba16",
55 [](Writer &w, nb::ndarray<uint16_t, nb::c_contig, nb::device::cpu> arr) { w.write_hdr_rgba16(arr.data()); },
56 nb::arg("rgba16_buffer"))
57
58 .def(
59 "write_hdr_rgba16_at_pts",
60 [](Writer &w, nb::ndarray<uint16_t, nb::c_contig, nb::device::cpu> arr, int64_t pts) { w.write_hdr_rgba16_at_pts(arr.data(), pts); },
61 nb::arg("rgba16_buffer"),
62 nb::arg("pts"))
63
64#ifdef MXWRITE_HAS_CUDA_COPY
65 .def(
66 "write_cuda_rgba",
67 [](Writer &w, nb::ndarray<uint8_t, nb::c_contig, nb::device::cuda> arr, int src_stride, bool bottom_up) { return w.write_cuda_rgba(arr.data(), src_stride, bottom_up); },
68 nb::arg("cuda_rgba_buffer"),
69 nb::arg("src_stride"),
70 nb::arg("bottom_up") = false)
71
72 .def(
73 "write_cuda_rgba_at_pts",
74 [](Writer &w, nb::ndarray<uint8_t, nb::c_contig, nb::device::cuda> arr, int src_stride, int64_t pts, bool bottom_up) { return w.write_cuda_rgba_at_pts(arr.data(), src_stride, pts, bottom_up); },
75 nb::arg("cuda_rgba_buffer"),
76 nb::arg("src_stride"),
77 nb::arg("pts"),
78 nb::arg("bottom_up") = false)
79#endif
80
81 .def("close", &Writer::close)
82 .def("is_open", &Writer::is_open)
83 .def("is_hardware_encode", &Writer::is_hardware_encode)
84 .def("set_block_when_full", &Writer::set_block_when_full, nb::arg("value"))
85 .def("get_block_when_full", &Writer::get_block_when_full)
86 .def("get_frame_count", &Writer::get_frame_count)
87 .def("get_bytes_written", &Writer::get_bytes_written)
88 .def("get_duration", &Writer::get_duration);
89
90 m.def("transfer_audio", &transfer_audio, nb::arg("sourceAudioFile"), nb::arg("destVideoFile"));
91
92 m.def("cleanup_contexts", [](nb::capsule source_ctx, nb::capsule dest_ctx, nb::capsule output_ctx) { cleanup_contexts(static_cast<AVFormatContext *>(source_ctx.data()), static_cast<AVFormatContext *>(dest_ctx.data()), static_cast<AVFormatContext *>(output_ctx.data())); });
93}
FFmpeg-backed RGBA video writer.
Definition mxwrite.hpp:142
std::uint64_t get_bytes_written() const
Return the current logical byte position of the output muxer.
Definition mxwrite.hpp:262
bool is_hardware_encode() const
True when FFmpeg identifies the active encoder as hardware or hybrid.
Definition mxwrite.hpp:246
void write_hdr_rgba16_at_pts(void *rgba16_buffer, int64_t pts)
Queue a 16-bit HDR RGBA frame with an explicit presentation timestamp.
Definition mxwrite.cpp:1862
bool get_block_when_full() const
Check whether the encoder queue blocks instead of dropping frames.
Definition mxwrite.hpp:252
bool write_cuda_rgba(void *cuda_rgba_buffer, int src_stride, bool bottom_up=false)
Queue a CUDA RGBA frame for encoding.
Definition mxwrite.cpp:1936
void set_block_when_full(bool value)
If true, keep one pending frame and pace producer threads to the encoder instead of dropping frames....
Definition mxwrite.hpp:250
void write_at_pts(void *rgba_buffer, int64_t pts)
Queue a host RGBA frame with an explicit presentation timestamp.
Definition mxwrite.cpp:1753
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.
Definition mxwrite.cpp:977
void write(void *rgba_buffer)
Queue a host RGBA frame for immediate-mode encoding.
Definition mxwrite.cpp:1751
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.
Definition mxwrite.cpp:998
bool is_open() const
Check whether the writer is currently open.
Definition mxwrite.hpp:244
double get_duration() const
Return the encoded duration in seconds.
Definition mxwrite.cpp:2300
bool write_cuda_rgba_at_pts(void *cuda_rgba_buffer, int src_stride, int64_t pts, bool bottom_up=false)
Queue a CUDA RGBA frame with an explicit presentation timestamp.
Definition mxwrite.cpp:1938
void close()
Close the writer and flush pending packets.
Definition mxwrite.cpp:2239
int64_t get_frame_count() const
Return the output timeline length in nominal frame ticks.
Definition mxwrite.hpp:260
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...
Definition mxwrite.cpp:1860
void write_ts(void *rgba_buffer)
Queue a host RGBA frame using capture timestamps.
Definition mxwrite.cpp:2049
NB_MODULE(mxvk_ext, module)
void transfer_audio(std::string_view sourceAudioFile, std::string_view destVideoFile)
Copy audio from one video file to another.
Definition mxwrite.cpp:221
void cleanup_contexts(AVFormatContext *source_ctx, AVFormatContext *dest_ctx, AVFormatContext *output_ctx)
Free FFmpeg format contexts used during transfer operations.
Definition mxwrite.cpp:209
std::vector< EncoderInfo > available_video_encoders()
Definition mxwrite.cpp:920
std::vector< EncoderOptionInfo > video_encoder_options(std::string_view encoder_name)
Return the options exposed by one registered video encoder.
Definition mxwrite.cpp:958
FFmpeg-based video writer used by MXWrite.
std::vector< uint8_t > content_light
Raw AVContentLightMetadata side-data bytes, or empty.
Definition mxwrite.hpp:131
int color_range
AVColorRange value. Raw AVMasteringDisplayMetadata side-data bytes, or empty.
Definition mxwrite.hpp:127
std::vector< uint8_t > mastering_display
Definition mxwrite.hpp:129
int color_trc
AVColorTransferCharacteristic value.
Definition mxwrite.hpp:125
bool enabled
Enables the HDR output path.
Definition mxwrite.hpp:123
int color_primaries
AVColorPrimaries value.
Definition mxwrite.hpp:124
int color_space
AVColorSpace value.
Definition mxwrite.hpp:126
std::string codec
Encoder selection policy or exact FFmpeg encoder name.
Definition mxwrite.hpp:99
std::int64_t bit_rate
Target bits/second; 0 selects CRF/CQ.
Definition mxwrite.hpp:98
std::string tune
Optional tuning mode.
Definition mxwrite.hpp:96
bool realtime
Enable low-latency settings.
Definition mxwrite.hpp:101
std::string ffmpeg_options
Additional FFmpeg-style video encoder options.
Definition mxwrite.hpp:100
struct EncodeOptions::HdrInfo hdr
int crf
Constant Rate Factor.
Definition mxwrite.hpp:97
bool block_when_full
Pace producers to encoder throughput instead of dropping frames.
Definition mxwrite.hpp:102
std::string preset
Encoder preset name.
Definition mxwrite.hpp:95
std::string long_name
Human-readable encoder description.
Definition mxwrite.hpp:41
std::string pixel_formats
Comma-separated supported input pixel formats.
Definition mxwrite.hpp:43
std::string name
Exact libavcodec encoder name (for example, libx265).
Definition mxwrite.hpp:40
std::string codec_name
Encoded format name (for example, hevc or av1).
Definition mxwrite.hpp:42
bool experimental
True when FFmpeg marks the encoder experimental.
Definition mxwrite.hpp:45
bool hardware
True for hardware or hybrid encoders.
Definition mxwrite.hpp:44
std::string minimum
Minimum value for numeric options.
Definition mxwrite.hpp:53
std::string name
Option name accepted by EncodeOptions::ffmpeg_options.
Definition mxwrite.hpp:50
std::string help
Human-readable FFmpeg option description.
Definition mxwrite.hpp:56
std::string choices
Comma-separated named values for enum-like options.
Definition mxwrite.hpp:55
std::string type
FFmpeg option type.
Definition mxwrite.hpp:51
std::string maximum
Maximum value for numeric options.
Definition mxwrite.hpp:54
std::string default_value
Encoder default, when it can be represented as text.
Definition mxwrite.hpp:52
Queue entry that stores a frame pointer and its capture timestamp.
Definition mxwrite.hpp:33
void * data
Pointer to RGBA frame data owned by the producer.
Definition mxwrite.hpp:34
std::chrono::steady_clock::time_point capture_time
Capture time for timestamp-based encoding.
Definition mxwrite.hpp:35