Feature/video in #3

Merged
itten merged 12 commits from feature/video-in into main 2026-07-05 12:57:40 +03:00
2 changed files with 18 additions and 33 deletions
Showing only changes of commit 0298cf9b48 - Show all commits
+5 -14
View File
@@ -1,11 +1,3 @@
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
}
#include <string>
#include <mxl/flow.h>
#include <mxl/time.h>
@@ -21,11 +13,11 @@ class VideoInNode : public dmf::NodeBase {
log("file: %s", filename.c_str());
dmf::VideoReader video_reader(filename);
if (!video_reader.have_video && !video_reader.has_audio) {
if (!video_reader.has_video && !video_reader.has_audio) {
log("no video or audio stream found"); return;
}
const bool has_video = config().contains("video_flow_id") && video_reader.have_video;
const bool has_video = config().contains("video_flow_id") && video_reader.has_video;
mxlFlowWriter video_writer = nullptr;
mxlFlowConfigInfo video_cfg{};
uint32_t video_stride = 0;
@@ -56,7 +48,7 @@ class VideoInNode : public dmf::NodeBase {
video_stride, video_stride * static_cast<uint32_t>(height), video_cfg.discrete.grainCount);
}
mxlFlowWriter audio_writer{};
mxlFlowWriter audio_writer = nullptr;
mxlFlowConfigInfo audio_cfg{};
int sample_rate = video_reader.audio_info.sample_rate;
int channels = video_reader.audio_info.channels;
@@ -82,12 +74,11 @@ class VideoInNode : public dmf::NodeBase {
} else {
log("audio channels=%u buffer=%u samples",
audio_cfg.continuous.channelCount, audio_cfg.continuous.bufferLength);
}
size_t max_write = 0;
mxlFlowWriterGetMaxWriteLengthSamples(audio_writer, &max_write);
max_audio_samples = static_cast<int>(max_write);
}
}
std::vector<uint8_t> audio_temp(max_audio_samples * channels * sizeof(float));
const mxlRational video_rate = {fps_num, fps_den};
@@ -107,7 +98,7 @@ class VideoInNode : public dmf::NodeBase {
}
int out_samples_written = 0;
dmf::VideoReader::FrameKind frame_kind = video_reader.get_next_frame(
dmf::VideoReader::FrameKind frame_kind = video_reader.read_next(
has_video ? video_buf : nullptr,
video_stride,
has_audio ? audio_temp.data() : nullptr,
+9 -15
View File
@@ -16,8 +16,6 @@ extern "C" {
#include "Signal.hpp"
#include "V210.hpp"
#include <iostream>
namespace dmf {
class VideoReader {
@@ -33,8 +31,6 @@ public:
struct AudioInfo {
int sample_rate = 0;
int channels = 0;
int samples = 0;
int channel_stride = 0; // floats between channel planes (NDI planar layout)
};
enum class FrameKind { None, Video, Audio };
@@ -42,14 +38,14 @@ public:
VideoInfo video_info{};
AudioInfo audio_info{};
bool has_audio = false;
bool have_video = false;
bool has_video = false;
explicit VideoReader(const std::string& filename) {
if (!open_file(filename))
return;
if (!have_video && !has_audio) return;
if (!has_video && !has_audio) return;
get_source_info();
if (have_video) allocate_video_conversion_buffers();
if (has_video) allocate_video_conversion_buffers();
if (has_audio) allocate_audio_conversion_buffers();
}
@@ -65,12 +61,11 @@ public:
av_packet_free(&packet);
}
// Returns true when a frame was decoded and written into video_buf.
// Returns false when g_running goes false.
FrameKind get_next_frame(uint8_t* video_buf, uint32_t mxl_stride, uint8_t* audio_buf, int max_audio_samples, int& out_samples_written) {
// Returns Video or Audio when a frame/packet was decoded, None when g_running goes false.
FrameKind read_next(uint8_t* video_buf, uint32_t mxl_stride, uint8_t* audio_buf, int max_audio_samples, int& out_samples_written) {
while (dmf::g_running.load(std::memory_order_relaxed)) {
// Drain any frames buffered in the decoder first
if (have_video && avcodec_receive_frame(video_codec_context, video_frame) == 0) {
if (has_video && avcodec_receive_frame(video_codec_context, video_frame) == 0) {
if (!video_buf) {
av_frame_unref(video_frame);
continue; // nowhere to write — discard frame
@@ -138,7 +133,7 @@ public:
if (av_read_frame(format_context, packet) < 0) {
// EOF — loop back to start
avformat_seek_file(format_context, -1, 0, 0, 0, AVSEEK_FLAG_BACKWARD);
if (have_video) avcodec_flush_buffers(video_codec_context);
if (has_video) avcodec_flush_buffers(video_codec_context);
if (has_audio) avcodec_flush_buffers(audio_codec_context);
swr_close(swr_audio_ctx);
swr_init(swr_audio_ctx);
@@ -184,7 +179,7 @@ private:
const AVMediaType type = format_context->streams[i]->codecpar->codec_type;
if (type == AVMEDIA_TYPE_VIDEO && video_stream_index == -1) {
video_stream_index = static_cast<int>(i);
have_video = true;
has_video = true;
} else if (type == AVMEDIA_TYPE_AUDIO && audio_stream_index == -1) {
audio_stream_index = static_cast<int>(i);
has_audio = true;
@@ -225,7 +220,7 @@ private:
video_info.fps_den = fps.den;
video_info.pix_fmt = video_codec_context->pix_fmt;
} else {
have_video = false;
has_video = false;
}
// audio part
@@ -279,7 +274,6 @@ private:
if (swr_init(swr_audio_ctx) < 0) {
throw std::runtime_error("Failed to create SwrContext");
}
}
};