MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk::VK_FF_Capture Class Reference

FFmpeg-backed video-file capture source. More...

#include <mxvk/include/mxvk/mxvk_ff_capture.hpp>

Public Member Functions

void close ()
 Close the active file and release decoder resources.
double fps () const
 Source frame rate, falling back to 30 fps when unknown.
int height () const
 Source height in pixels.
bool is_open () const
 Check whether a decoder is open.
bool open (const std::string &filename)
 Open a video file.
VK_FF_Captureoperator= (const VK_FF_Capture &)=delete
VK_FF_Captureoperator= (VK_FF_Capture &&)=delete
bool readRgba (std::vector< uint8_t > &rgba, int &width, int &height, int &pitch, bool flipY=false)
 Decode the next frame as tightly packed RGBA8.
bool using_hardware_decode () const
 True when CUDA hardware decode is active.
 VK_FF_Capture ()=default
 Construct a closed capture source.
 VK_FF_Capture (const VK_FF_Capture &)=delete
 VK_FF_Capture (VK_FF_Capture &&)=delete
int width () const
 Source width in pixels.
 ~VK_FF_Capture ()
 Close and release FFmpeg resources.

Detailed Description

FFmpeg-backed video-file capture source.

VK_FF_Capture opens video files directly through FFmpeg, prefers CUDA hardware decoding when available, and returns tightly packed RGBA8 frames suitable for Vulkan staging uploads or encoder input.

Definition at line 32 of file mxvk_ff_capture.hpp.

Constructor & Destructor Documentation

◆ VK_FF_Capture() [1/3]

mxvk::VK_FF_Capture::VK_FF_Capture ( )
default

Construct a closed capture source.

◆ ~VK_FF_Capture()

mxvk::VK_FF_Capture::~VK_FF_Capture ( )

Close and release FFmpeg resources.

Definition at line 58 of file mxvk_ff_capture.cpp.

58 {
59 close();
60 }
void close()
Close the active file and release decoder resources.

◆ VK_FF_Capture() [2/3]

mxvk::VK_FF_Capture::VK_FF_Capture ( const VK_FF_Capture & )
delete

◆ VK_FF_Capture() [3/3]

mxvk::VK_FF_Capture::VK_FF_Capture ( VK_FF_Capture && )
delete

Member Function Documentation

◆ close()

void mxvk::VK_FF_Capture::close ( )

Close the active file and release decoder resources.

Definition at line 143 of file mxvk_ff_capture.cpp.

143 {
144 if (swsCtx != nullptr) {
145 sws_freeContext(swsCtx);
146 swsCtx = nullptr;
147 }
148 if (hwDeviceCtx != nullptr) {
149 av_buffer_unref(&hwDeviceCtx);
150 }
151 if (swFrame != nullptr) {
152 av_frame_free(&swFrame);
153 }
154 if (frame != nullptr) {
155 av_frame_free(&frame);
156 }
157 if (packet != nullptr) {
158 av_packet_free(&packet);
159 }
160 if (codecCtx != nullptr) {
161 avcodec_free_context(&codecCtx);
162 }
163 if (formatCtx != nullptr) {
164 avformat_close_input(&formatCtx);
165 }
166 videoStream = -1;
167 frameWidth = 0;
168 frameHeight = 0;
169 frameFps = 30.0;
170 hwPixFmt = AV_PIX_FMT_NONE;
171 hardwareDecode = false;
172 }

◆ fps()

double mxvk::VK_FF_Capture::fps ( ) const
inlinenodiscard

Source frame rate, falling back to 30 fps when unknown.

Definition at line 80 of file mxvk_ff_capture.hpp.

80{ return frameFps; }

◆ height()

int mxvk::VK_FF_Capture::height ( ) const
inlinenodiscard

Source height in pixels.

Definition at line 78 of file mxvk_ff_capture.hpp.

78{ return frameHeight; }

◆ is_open()

bool mxvk::VK_FF_Capture::is_open ( ) const
inlinenodiscard

Check whether a decoder is open.

Definition at line 52 of file mxvk_ff_capture.hpp.

52{ return formatCtx != nullptr && codecCtx != nullptr; }

◆ open()

bool mxvk::VK_FF_Capture::open ( const std::string & filename)

Open a video file.

Parameters
filenamePath to the input video file.
Returns
true on success.

Definition at line 62 of file mxvk_ff_capture.cpp.

62 {
63 close();
64
65 if (avformat_open_input(&formatCtx, filename.c_str(), nullptr, nullptr) < 0) {
66 std::cout << std::format("mxvk_ff_capture: failed to open file: {}\n", filename);
67 close();
68 return false;
69 }
70
71 if (avformat_find_stream_info(formatCtx, nullptr) < 0) {
72 std::cout << std::format("mxvk_ff_capture: failed to read stream info: {}\n", filename);
73 close();
74 return false;
75 }
76
77 const int streamIndex = av_find_best_stream(formatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
78 if (streamIndex < 0) {
79 std::cout << std::format("mxvk_ff_capture: no video stream found: {}\n", filename);
80 close();
81 return false;
82 }
83 videoStream = streamIndex;
84
85 AVStream *stream = formatCtx->streams[videoStream];
86 const AVCodecParameters *codecParams = stream->codecpar;
87 const AVCodec *decoder = avcodec_find_decoder(codecParams->codec_id);
88 if (decoder == nullptr) {
89 std::cout << "mxvk_ff_capture: no decoder found for video stream\n";
90 close();
91 return false;
92 }
93
94 codecCtx = avcodec_alloc_context3(decoder);
95 if (codecCtx == nullptr || avcodec_parameters_to_context(codecCtx, codecParams) < 0) {
96 std::cout << "mxvk_ff_capture: failed to create decoder context\n";
97 close();
98 return false;
99 }
100
101 codecCtx->opaque = this;
102 if (initHardwareDevice(decoder)) {
103 codecCtx->get_format = &VK_FF_Capture::chooseHwFormat;
104 codecCtx->hw_device_ctx = av_buffer_ref(hwDeviceCtx);
105 hardwareDecode = codecCtx->hw_device_ctx != nullptr;
106 }
107
108 if (avcodec_open2(codecCtx, decoder, nullptr) < 0) {
109 std::cout << "mxvk_ff_capture: failed to open decoder\n";
110 close();
111 return false;
112 }
113
114 packet = av_packet_alloc();
115 frame = av_frame_alloc();
116 swFrame = av_frame_alloc();
117 if (packet == nullptr || frame == nullptr || swFrame == nullptr) {
118 std::cout << "mxvk_ff_capture: failed to allocate decoder frames\n";
119 close();
120 return false;
121 }
122
123 frameWidth = codecCtx->width;
124 frameHeight = codecCtx->height;
125 frameFps = rationalToDouble(stream->avg_frame_rate);
126 if (frameFps <= 0.0) {
127 frameFps = rationalToDouble(stream->r_frame_rate);
128 }
129 if (frameFps <= 0.0) {
130 frameFps = 30.0;
131 }
132
133 std::cout << std::format(
134 "mxvk_ff_capture: opened {} ({}x{}, {:.3f} fps, decode={})\n",
135 filename,
136 frameWidth,
137 frameHeight,
138 frameFps,
139 hardwareDecode ? "cuda" : "software");
140 return true;
141 }

◆ operator=() [1/2]

VK_FF_Capture & mxvk::VK_FF_Capture::operator= ( const VK_FF_Capture & )
delete

◆ operator=() [2/2]

VK_FF_Capture & mxvk::VK_FF_Capture::operator= ( VK_FF_Capture && )
delete

◆ readRgba()

bool mxvk::VK_FF_Capture::readRgba ( std::vector< uint8_t > & rgba,
int & width,
int & height,
int & pitch,
bool flipY = false )

Decode the next frame as tightly packed RGBA8.

Parameters
rgbaOutput byte buffer, resized to pitch * height.
widthOutput frame width in pixels.
heightOutput frame height in pixels.
pitchOutput row pitch in bytes.
flipYFlip rows vertically after conversion when true.
Returns
true when a frame was decoded, false on EOF or failure.

Definition at line 174 of file mxvk_ff_capture.cpp.

174 {
175 if (!is_open()) {
176 return false;
177 }
178
179 if (!decodeNextFrame()) {
180 return false;
181 }
182 const bool converted = convertFrameToRgba(frame, rgba, width, height, pitch, flipY);
183 av_frame_unref(frame);
184 return converted;
185 }
bool is_open() const
Check whether a decoder is open.
int width() const
Source width in pixels.
int height() const
Source height in pixels.

◆ using_hardware_decode()

bool mxvk::VK_FF_Capture::using_hardware_decode ( ) const
inlinenodiscard

True when CUDA hardware decode is active.

Definition at line 82 of file mxvk_ff_capture.hpp.

82{ return hardwareDecode; }

◆ width()

int mxvk::VK_FF_Capture::width ( ) const
inlinenodiscard

Source width in pixels.

Definition at line 76 of file mxvk_ff_capture.hpp.

76{ return frameWidth; }

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