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
+14 -21
View File
@@ -4,7 +4,6 @@ extern "C" {
#include <libavutil/avutil.h> #include <libavutil/avutil.h>
#include <libswscale/swscale.h> #include <libswscale/swscale.h>
#include <libavutil/imgutils.h> #include <libavutil/imgutils.h>
#include <libavutil/pixdesc.h>
} }
#include <string> #include <string>
@@ -19,8 +18,10 @@ class VideoInNode : public dmf::NodeBase {
void run() override { void run() override {
const std::string filename = config().value("file", std::string{}); const std::string filename = config().value("file", std::string{});
if (filename.empty()) { log("config missing 'file'"); return; } 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); 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_info = config().at("video_flow_id");
const auto video_flow_id = video_flow_info.at("id").get<std::string>(); const auto video_flow_id = video_flow_info.at("id").get<std::string>();
@@ -29,32 +30,24 @@ class VideoInNode : public dmf::NodeBase {
const int fps_num = video_flow_info.value("fps_num", video_reader.source_info.fps_num); 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 fps_den = video_flow_info.value("fps_den", video_reader.source_info.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; mxlFlowWriter video_writer = nullptr;
mxlFlowConfigInfo video_config = {}; mxlFlowConfigInfo video_cfg = {};
std::string video_flow_def = dmf::make_video_flow_def(
video_flow_id,
node_id(),
width,
height,
fps_num,
fps_den
);
bool created = false; bool created = false;
mxlStatus vst = mxlCreateFlowWriter( mxlStatus vst = mxlCreateFlowWriter(
instance(), instance(),
video_flow_def.c_str(), dmf::make_video_flow_def(video_flow_id, node_id(), width, height, fps_num, fps_den).c_str(),
"", "", &video_writer, &video_cfg, &created);
&video_writer,
&video_config,
&created
);
if (vst != MXL_STATUS_OK) { if (vst != MXL_STATUS_OK) {
log("mxlCreateFlowWriter failed (%s)", dmf::mxl_status_str(vst)); log("mxlCreateFlowWriter failed (%s)", dmf::mxl_status_str(vst));
return; 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); uint64_t video_index = mxlGetCurrentIndex(&video_rate);
while (dmf::g_running.load(std::memory_order_relaxed)) { while (dmf::g_running.load(std::memory_order_relaxed)) {
@@ -62,7 +55,7 @@ class VideoInNode : public dmf::NodeBase {
mxlGrainInfo grain{}; mxlGrainInfo grain{};
vst = mxlFlowWriterOpenGrain(video_writer, video_index, &grain, &buf); vst = mxlFlowWriterOpenGrain(video_writer, video_index, &grain, &buf);
if (vst == MXL_STATUS_OK) { if (vst == MXL_STATUS_OK) {
if (!video_reader.get_next_frame(buf, video_stride, nullptr)) continue; if (!video_reader.get_next_frame(buf, video_stride)) break;
grain.flags = 0; grain.flags = 0;
grain.validSlices = grain.totalSlices; grain.validSlices = grain.totalSlices;
mxlFlowWriterCommitGrain(video_writer, &grain); mxlFlowWriterCommitGrain(video_writer, &grain);
@@ -72,11 +65,11 @@ class VideoInNode : public dmf::NodeBase {
video_index++; video_index++;
} }
log("stopped at video_index=%llu", video_index);
mxlReleaseFlowWriter(instance(), video_writer); mxlReleaseFlowWriter(instance(), video_writer);
} }
}; };
int main() { int main() {
VideoInNode node; VideoInNode node;
return node.execute(); return node.execute();
+35 -63
View File
@@ -4,9 +4,8 @@ extern "C" {
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
#include <libavutil/avutil.h> #include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h> #include <libavutil/imgutils.h>
#include <libavutil/pixdesc.h> #include <libswscale/swscale.h>
} }
#include <stdexcept> #include <stdexcept>
@@ -15,6 +14,7 @@ extern "C" {
#include "V210.hpp" #include "V210.hpp"
namespace dmf { namespace dmf {
class VideoReader { class VideoReader {
public: public:
struct SourceInfo { struct SourceInfo {
@@ -22,28 +22,21 @@ class VideoReader {
int height = 0; int height = 0;
int fps_num = 0; int fps_num = 0;
int fps_den = 0; int fps_den = 0;
int stride = 0;
AVPixelFormat pix_fmt{}; AVPixelFormat pix_fmt{};
}; };
struct AudioInfo { SourceInfo source_info{};
int sample_rate = 0;
int channels = 0;
int samples = 0;
int channel_stride = 0; // floats between channel planes
};
struct SourceInfo source_info{};
bool has_audio = false; bool has_audio = false;
bool have_video = false; bool have_video = false;
VideoReader(std::string filename) { explicit VideoReader(const std::string& filename) {
if (!open_file(filename)) { if (!open_file(filename))
return; return;
} if (have_video) {
get_source_info(); get_source_info();
allocate_conversion_buffers(); allocate_conversion_buffers();
} }
}
~VideoReader() { ~VideoReader() {
avcodec_free_context(&codec_context); avcodec_free_context(&codec_context);
@@ -54,9 +47,11 @@ class VideoReader {
av_packet_free(&packet); av_packet_free(&packet);
} }
bool get_next_frame(uint8_t* video_buf, uint32_t mxl_stride, uint8_t* audiobuf) { // Returns true when a frame was decoded and written into video_buf.
// Returns false when g_running goes false.
bool get_next_frame(uint8_t* video_buf, uint32_t mxl_stride) {
while (dmf::g_running.load(std::memory_order_relaxed)) { while (dmf::g_running.load(std::memory_order_relaxed)) {
// Try to get a buffered frame from previous packet first // Drain any frames buffered in the decoder first
if (avcodec_receive_frame(codec_context, frame) == 0) { if (avcodec_receive_frame(codec_context, frame) == 0) {
sws_scale( sws_scale(
sws_ctx, sws_ctx,
@@ -82,13 +77,13 @@ class VideoReader {
av_frame_unref(frame); av_frame_unref(frame);
return true; return true;
} }
// No buffered frame — read next packet // No buffered frame — read next packet
av_packet_unref(packet); av_packet_unref(packet);
if (av_read_frame(format_context, packet) < 0) { if (av_read_frame(format_context, packet) < 0) {
// EOF — seek back to start and keep going // EOF — loop back to start
avformat_seek_file(format_context, -1, 0, 0, 0, AVSEEK_FLAG_BACKWARD); avformat_seek_file(format_context, -1, 0, 0, 0, AVSEEK_FLAG_BACKWARD);
avcodec_flush_buffers(codec_context); avcodec_flush_buffers(codec_context);
av_packet_unref(packet);
continue; continue;
} }
if (packet->stream_index != video_stream_index) continue; if (packet->stream_index != video_stream_index) continue;
@@ -105,71 +100,56 @@ class VideoReader {
int video_stream_index = -1; int video_stream_index = -1;
int audio_stream_index = -1; int audio_stream_index = -1;
// conversion data SwsContext* sws_ctx = nullptr;
struct SwsContext *sws_ctx{};
int p10_linesizes[4] = {0, 0, 0, 0}; int p10_linesizes[4] = {0, 0, 0, 0};
uint8_t* p10_data[4] = {nullptr, nullptr, nullptr, nullptr}; uint8_t* p10_data[4] = {nullptr, nullptr, nullptr, nullptr};
bool open_file(std::string filename) { bool open_file(const std::string& filename) {
if (avformat_open_input(&format_context, filename.c_str(), nullptr, nullptr) != 0) { if (avformat_open_input(&format_context, filename.c_str(), nullptr, nullptr) != 0)
throw std::runtime_error("Could not open file: " + filename); throw std::runtime_error("Could not open file: " + filename);
return false;
}
// Find stream info
if (avformat_find_stream_info(format_context, nullptr) < 0) { if (avformat_find_stream_info(format_context, nullptr) < 0) {
avformat_close_input(&format_context); avformat_close_input(&format_context);
throw std::runtime_error("Could not find stream info"); throw std::runtime_error("Could not find stream info");
return false;
} }
// Find streams for (unsigned int i = 0; i < format_context->nb_streams; ++i) {
for (unsigned int i = 0; i < format_context->nb_streams; i++) { const AVMediaType type = format_context->streams[i]->codecpar->codec_type;
AVMediaType data_type = format_context->streams[i]->codecpar->codec_type; if (type == AVMEDIA_TYPE_VIDEO && video_stream_index == -1) {
if (data_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = static_cast<int>(i);
video_stream_index = i;
have_video = true; have_video = true;
} else if (data_type == AVMEDIA_TYPE_AUDIO && audio_stream_index == -1) { } else if (type == AVMEDIA_TYPE_AUDIO && audio_stream_index == -1) {
// TODO: show list of available audio tracks and allow user to pick audio_stream_index = static_cast<int>(i);
// or handle multiple audio streams
audio_stream_index = i;
has_audio = true; has_audio = true;
} }
} }
if (video_stream_index == -1 && audio_stream_index == -1) { if (video_stream_index == -1 && audio_stream_index == -1) {
avformat_close_input(&format_context); avformat_close_input(&format_context);
throw std::runtime_error("No audio/video stream found"); throw std::runtime_error("No audio/video stream found in: " + filename);
return false;
} }
return true; return true;
} }
void get_source_info() { void get_source_info() {
// Get codec parameters
AVCodecParameters* codec_params = format_context->streams[video_stream_index]->codecpar; AVCodecParameters* codec_params = format_context->streams[video_stream_index]->codecpar;
const AVCodec* codec = avcodec_find_decoder(codec_params->codec_id); const AVCodec* codec = avcodec_find_decoder(codec_params->codec_id);
if (!codec)
if (!codec) {
avformat_close_input(&format_context);
throw std::runtime_error("Unsupported codec"); throw std::runtime_error("Unsupported codec");
}
// Open codec
codec_context = avcodec_alloc_context3(codec); codec_context = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(codec_context, codec_params); if (avcodec_parameters_to_context(codec_context, codec_params) < 0)
throw std::runtime_error("Could not copy codec parameters");
if (avcodec_open2(codec_context, codec, nullptr) < 0) { if (avcodec_open2(codec_context, codec, nullptr) < 0) {
avcodec_free_context(&codec_context); avcodec_free_context(&codec_context);
avformat_close_input(&format_context);
throw std::runtime_error("Could not open codec"); throw std::runtime_error("Could not open codec");
} }
AVRational fps = codec_context->framerate; AVRational fps = codec_context->framerate;
if (fps.num == 0 || fps.den == 0) { if (fps.num == 0 || fps.den == 0)
fps = format_context->streams[video_stream_index]->avg_frame_rate; fps = format_context->streams[video_stream_index]->avg_frame_rate;
}
source_info.width = codec_context->width; source_info.width = codec_context->width;
source_info.height = codec_context->height; source_info.height = codec_context->height;
@@ -178,28 +158,20 @@ class VideoReader {
source_info.pix_fmt = codec_context->pix_fmt; source_info.pix_fmt = codec_context->pix_fmt;
} }
void allocate_conversion_buffers() void allocate_conversion_buffers() {
{
// Create Sws context to convert to planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), LE
sws_ctx = sws_getContext( sws_ctx = sws_getContext(
source_info.width, source_info.height, source_info.pix_fmt, source_info.width, source_info.height, source_info.pix_fmt,
source_info.width, source_info.height, AV_PIX_FMT_YUV422P10LE, source_info.width, source_info.height, AV_PIX_FMT_YUV422P10LE,
0, NULL, NULL, NULL SWS_BILINEAR, nullptr, nullptr, nullptr
); );
if (!sws_ctx)
if (!sws_ctx) {
throw std::runtime_error("Failed to create SwsContext"); throw std::runtime_error("Failed to create SwsContext");
return;
}
// Allocate P10 image (freed in destructor via av_freep(&p10_data[0])) if (av_image_alloc(p10_data, p10_linesizes,
av_image_alloc(
p10_data, p10_linesizes,
source_info.width, source_info.height, source_info.width, source_info.height,
AV_PIX_FMT_YUV422P10LE, 64 AV_PIX_FMT_YUV422P10LE, 64) < 0)
); throw std::runtime_error("Failed to allocate YUV422P10 buffer");
} }
}; };
}
} // namespace dmf