refactor: VideoReader and videoin cleanup

VideoReader:
- Guard get_source_info/allocate_conversion_buffers behind have_video
  check — prevents crash on audio-only files
- Remove unused audiobuf parameter from get_next_frame
- Remove dead return statements after throw
- Remove unused SourceInfo::stride field and AudioInfo struct
- Pass const std::string& instead of by value in constructor/open_file
- Remove redundant struct keyword on SourceInfo source_info{}
- Fix video_stream_index never guarded against -1 in open_file
- Check av_image_alloc and avcodec_parameters_to_context return values
- Remove extra av_packet_unref after seek (was harmless but confusing)
- Use SWS_BILINEAR for sws_getContext flags instead of 0
- Replace NULL with nullptr in sws_getContext
- Remove unused #include <libavutil/pixdesc.h>

videoin main.cpp:
- Early return if have_video is false after open
- Inline make_video_flow_def call (remove intermediate variable)
- Add grain count log line matching other nodes
- Change continue to break when get_next_frame returns false
- Make video_rate const
- Remove double blank line before main()
- Remove trailing spaces

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
JohannesItten
2026-07-03 19:13:19 +03:00
parent bb7d1bb5a3
commit 96475acf62
2 changed files with 161 additions and 196 deletions
+22 -29
View File
@@ -4,7 +4,6 @@ extern "C" {
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/pixdesc.h>
}
#include <string>
@@ -19,51 +18,45 @@ class VideoInNode : public dmf::NodeBase {
void run() override {
const std::string filename = config().value("file", std::string{});
if (filename.empty()) { log("config missing 'file'"); return; }
log("VideoIn Node started with file: %s", filename.c_str());
log("file: %s", filename.c_str());
dmf::VideoReader video_reader(filename);
if (!video_reader.have_video) { log("no video stream found"); return; }
const auto video_flow_info = config().at("video_flow_id");
const auto video_flow_id = video_flow_info.at("id").get<std::string>();
const int width = video_flow_info.value("width", video_reader.source_info.width);
const int height = video_flow_info.value("height", video_reader.source_info.height);
const int fps_num = video_flow_info.value("fps_num", video_reader.source_info.fps_num);
const int fps_den = video_flow_info.value("fps_den", video_reader.source_info.fps_den);
const int width = video_flow_info.value("width", video_reader.source_info.width);
const int height = video_flow_info.value("height", video_reader.source_info.height);
const int fps_num = video_flow_info.value("fps_num", video_reader.source_info.fps_num);
const int fps_den = video_flow_info.value("fps_den", video_reader.source_info.fps_den);
mxlFlowWriter video_writer = nullptr;
mxlFlowConfigInfo video_config = {};
std::string video_flow_def = dmf::make_video_flow_def(
video_flow_id,
node_id(),
width,
height,
fps_num,
fps_den
);
log("video flow=%s %dx%d @ %d/%d fps", video_flow_id.c_str(), width, height, fps_num, fps_den);
mxlFlowWriter video_writer = nullptr;
mxlFlowConfigInfo video_cfg = {};
bool created = false;
mxlStatus vst = mxlCreateFlowWriter(
instance(),
video_flow_def.c_str(),
"",
&video_writer,
&video_config,
&created
);
dmf::make_video_flow_def(video_flow_id, node_id(), width, height, fps_num, fps_den).c_str(),
"", &video_writer, &video_cfg, &created);
if (vst != MXL_STATUS_OK) {
log("mxlCreateFlowWriter failed (%s)", dmf::mxl_status_str(vst));
return;
}
const uint32_t video_stride = video_config.discrete.sliceSizes[0];
const uint32_t video_stride = video_cfg.discrete.sliceSizes[0];
log("video stride=%u B/line grain=%u B ring=%u grains",
video_stride, video_stride * static_cast<uint32_t>(height), video_cfg.discrete.grainCount);
mxlRational video_rate = {fps_num, fps_den};
const mxlRational video_rate = {fps_num, fps_den};
uint64_t video_index = mxlGetCurrentIndex(&video_rate);
while (dmf::g_running.load(std::memory_order_relaxed)) {
uint8_t* buf = nullptr;
uint8_t* buf = nullptr;
mxlGrainInfo grain{};
vst = mxlFlowWriterOpenGrain(video_writer, video_index, &grain, &buf);
if (vst == MXL_STATUS_OK) {
if (!video_reader.get_next_frame(buf, video_stride, nullptr)) continue;
grain.flags = 0;
if (!video_reader.get_next_frame(buf, video_stride)) break;
grain.flags = 0;
grain.validSlices = grain.totalSlices;
mxlFlowWriterCommitGrain(video_writer, &grain);
}
@@ -72,12 +65,12 @@ class VideoInNode : public dmf::NodeBase {
video_index++;
}
log("stopped at video_index=%llu", video_index);
mxlReleaseFlowWriter(instance(), video_writer);
}
};
int main() {
int main() {
VideoInNode node;
return node.execute();
}