Feature/video in #3
+44
-18
@@ -21,19 +21,27 @@ class VideoInNode : public dmf::NodeBase {
|
||||
log("file: %s", filename.c_str());
|
||||
|
||||
dmf::VideoReader video_reader(filename);
|
||||
if (!video_reader.have_video) { log("no video stream found"); return; }
|
||||
if (!video_reader.have_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;
|
||||
mxlFlowWriter video_writer = nullptr;
|
||||
mxlFlowConfigInfo video_cfg{};
|
||||
uint32_t video_stride = 0;
|
||||
int width = 0, height = 0, fps_num = 25, fps_den = 1;
|
||||
std::string video_flow_id;
|
||||
|
||||
if (has_video) {
|
||||
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.video_info.width);
|
||||
const int height = video_flow_info.value("height", video_reader.video_info.height);
|
||||
const int fps_num = video_flow_info.value("fps_num", video_reader.video_info.fps_num);
|
||||
const int fps_den = video_flow_info.value("fps_den", video_reader.video_info.fps_den);
|
||||
video_flow_id = video_flow_info.at("id").get<std::string>();
|
||||
width = video_flow_info.value("width", video_reader.video_info.width);
|
||||
height = video_flow_info.value("height", video_reader.video_info.height);
|
||||
fps_num = video_flow_info.value("fps_num", video_reader.video_info.fps_num);
|
||||
fps_den = video_flow_info.value("fps_den", video_reader.video_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;
|
||||
mxlFlowConfigInfo video_cfg = {};
|
||||
bool created = false;
|
||||
mxlStatus vst = mxlCreateFlowWriter(
|
||||
instance(),
|
||||
@@ -43,9 +51,10 @@ class VideoInNode : public dmf::NodeBase {
|
||||
log("mxlCreateFlowWriter failed (%s)", dmf::mxl_status_str(vst));
|
||||
return;
|
||||
}
|
||||
const uint32_t video_stride = video_cfg.discrete.sliceSizes[0];
|
||||
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);
|
||||
}
|
||||
|
||||
mxlFlowWriter audio_writer{};
|
||||
mxlFlowConfigInfo audio_cfg{};
|
||||
@@ -61,6 +70,7 @@ class VideoInNode : public dmf::NodeBase {
|
||||
|
||||
log("audio flow=%s %d Hz %dch %d-bit", audio_flow_id.c_str(), sample_rate, channels, bit_depth);
|
||||
|
||||
bool created = false;
|
||||
mxlStatus ast = mxlCreateFlowWriter(
|
||||
instance(),
|
||||
dmf::make_audio_flow_def(audio_flow_id, node_id(), sample_rate, channels, bit_depth,
|
||||
@@ -81,21 +91,24 @@ class VideoInNode : public dmf::NodeBase {
|
||||
std::vector<uint8_t> audio_temp(max_audio_samples * channels * sizeof(float));
|
||||
|
||||
const mxlRational video_rate = {fps_num, fps_den};
|
||||
const mxlRational audio_rate = {sample_rate, 1};
|
||||
uint64_t audio_index = 0;
|
||||
if (has_audio) {
|
||||
const mxlRational audio_rate = {sample_rate, 1};
|
||||
audio_index = mxlGetCurrentIndex(&audio_rate);
|
||||
}
|
||||
uint64_t video_index = mxlGetCurrentIndex(&video_rate);
|
||||
uint64_t video_index = has_video ? mxlGetCurrentIndex(&video_rate) : 0;
|
||||
|
||||
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
||||
uint8_t* video_buf = nullptr;
|
||||
mxlGrainInfo grain{};
|
||||
mxlStatus vst = MXL_ERR_UNSUPPORTED_OPERATION;
|
||||
if (has_video) {
|
||||
vst = mxlFlowWriterOpenGrain(video_writer, video_index, &grain, &video_buf);
|
||||
}
|
||||
|
||||
int out_samples_written = 0;
|
||||
dmf::VideoReader::FrameKind frame_kind = video_reader.get_next_frame(
|
||||
video_buf,
|
||||
has_video ? video_buf : nullptr,
|
||||
video_stride,
|
||||
has_audio ? audio_temp.data() : nullptr,
|
||||
max_audio_samples,
|
||||
@@ -105,20 +118,33 @@ class VideoInNode : public dmf::NodeBase {
|
||||
if (frame_kind == dmf::VideoReader::FrameKind::None) break;
|
||||
|
||||
if (frame_kind == dmf::VideoReader::FrameKind::Video) {
|
||||
if (vst == MXL_STATUS_OK) {
|
||||
if (has_video && vst == MXL_STATUS_OK) {
|
||||
grain.flags = 0;
|
||||
grain.validSlices = grain.totalSlices;
|
||||
mxlFlowWriterCommitGrain(video_writer, &grain);
|
||||
}
|
||||
if (has_video) {
|
||||
const uint64_t ns = mxlGetNsUntilIndex(video_index + 1, &video_rate);
|
||||
if (ns > 0 && ns < 2'000'000'000ULL) mxlSleepForNs(ns);
|
||||
video_index++;
|
||||
}
|
||||
} else if (frame_kind == dmf::VideoReader::FrameKind::Audio) {
|
||||
mxlFlowWriterCancelGrain(video_writer);
|
||||
if (has_video && vst == MXL_STATUS_OK) mxlFlowWriterCancelGrain(video_writer);
|
||||
// Wait for audio clock to catch up — prevents TOO_EARLY and sample loss
|
||||
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
||||
const uint64_t audio_now = mxlGetCurrentIndex(&audio_rate);
|
||||
if (audio_index + static_cast<uint64_t>(out_samples_written) <= audio_now) break;
|
||||
const uint64_t ns = mxlGetNsUntilIndex(audio_index + out_samples_written, &audio_rate);
|
||||
if (ns > 0 && ns < 2'000'000'000ULL) mxlSleepForNs(ns);
|
||||
else break;
|
||||
}
|
||||
if (!dmf::g_running.load(std::memory_order_relaxed)) break;
|
||||
|
||||
mxlMutableWrappedMultiBufferSlice slice{};
|
||||
mxlStatus ast = mxlFlowWriterOpenSamples(audio_writer, audio_index, out_samples_written, &slice);
|
||||
if (ast != MXL_STATUS_OK) {
|
||||
log("audio OpenSamples failed (%s) index=%llu", dmf::mxl_status_str(ast), audio_index);
|
||||
log("audio OpenSamples failed (%s) index=%llu — skipping", dmf::mxl_status_str(ast), audio_index);
|
||||
audio_index += out_samples_written; // advance even on failure — keeps alignment
|
||||
continue;
|
||||
}
|
||||
// copy per channel from audio_temp into slice
|
||||
@@ -145,9 +171,9 @@ class VideoInNode : public dmf::NodeBase {
|
||||
}
|
||||
}
|
||||
|
||||
log("stopped at video_index=%llu", video_index);
|
||||
mxlReleaseFlowWriter(instance(), video_writer);
|
||||
mxlReleaseFlowWriter(instance(), audio_writer);
|
||||
log("stopped at video_index=%llu", static_cast<unsigned long long>(video_index));
|
||||
if (has_video) mxlReleaseFlowWriter(instance(), video_writer);
|
||||
if (has_audio) mxlReleaseFlowWriter(instance(), audio_writer);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+12
-11
@@ -47,19 +47,15 @@ public:
|
||||
explicit VideoReader(const std::string& filename) {
|
||||
if (!open_file(filename))
|
||||
return;
|
||||
if (have_video) {
|
||||
if (!have_video && !has_audio) return;
|
||||
get_source_info();
|
||||
if (have_video) {
|
||||
allocate_video_conversion_buffers();
|
||||
}
|
||||
if (has_audio) {
|
||||
allocate_audio_conversion_buffers();
|
||||
}
|
||||
}
|
||||
if (have_video) allocate_video_conversion_buffers();
|
||||
if (has_audio) allocate_audio_conversion_buffers();
|
||||
}
|
||||
|
||||
~VideoReader() {
|
||||
avcodec_free_context(&video_codec_context);
|
||||
avcodec_free_context(&audio_codec_context);
|
||||
avformat_close_input(&format_context);
|
||||
sws_freeContext(sws_video_ctx);
|
||||
swr_free(&swr_audio_ctx);
|
||||
@@ -74,7 +70,7 @@ public:
|
||||
FrameKind get_next_frame(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 (avcodec_receive_frame(video_codec_context, video_frame) == 0) {
|
||||
if (have_video && avcodec_receive_frame(video_codec_context, video_frame) == 0) {
|
||||
if (!video_buf) {
|
||||
av_frame_unref(video_frame);
|
||||
continue; // nowhere to write — discard frame
|
||||
@@ -104,7 +100,7 @@ public:
|
||||
return FrameKind::Video;
|
||||
}
|
||||
|
||||
if (avcodec_receive_frame(audio_codec_context, audio_frame) == 0) {
|
||||
if (has_audio && avcodec_receive_frame(audio_codec_context, audio_frame) == 0) {
|
||||
if (!audio_buf) {
|
||||
av_frame_unref(audio_frame);
|
||||
continue; // nowhere to write — discard frame
|
||||
@@ -142,7 +138,8 @@ 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);
|
||||
avcodec_flush_buffers(video_codec_context);
|
||||
if (have_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);
|
||||
continue;
|
||||
@@ -203,6 +200,7 @@ private:
|
||||
}
|
||||
|
||||
void get_source_info() {
|
||||
if (video_stream_index != -1) {
|
||||
AVCodecParameters* video_codec_params = format_context->streams[video_stream_index]->codecpar;
|
||||
const AVCodec* video_codec = avcodec_find_decoder(video_codec_params->codec_id);
|
||||
if (!video_codec)
|
||||
@@ -226,6 +224,9 @@ private:
|
||||
video_info.fps_num = fps.num;
|
||||
video_info.fps_den = fps.den;
|
||||
video_info.pix_fmt = video_codec_context->pix_fmt;
|
||||
} else {
|
||||
have_video = false;
|
||||
}
|
||||
|
||||
// audio part
|
||||
if (audio_stream_index == -1) return;
|
||||
|
||||
+3
-3
@@ -1,18 +1,18 @@
|
||||
{
|
||||
"nodes": [
|
||||
{ "id": "videoin", "type": "videoin", "params": {"file": "/home/itten/test-vid/2.ts"} },
|
||||
{ "id": "videoin", "type": "videoin", "params": {"file": "/home/itten/test-vid/1.ts"} },
|
||||
{ "id": "ndiout", "type": "ndiout", "params": {} }
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"from": "videoin", "from_port": "video_flow_id",
|
||||
"to": "ndiout", "to_port": "video_flow_id",
|
||||
"format": { "kind": "video", "width": 1920, "height": 1080, "fps_num": 60, "fps_den": 1 }
|
||||
"format": { "kind": "video", "width": 1920, "height": 1080, "fps_num": 24, "fps_den": 1 }
|
||||
},
|
||||
{
|
||||
"from": "videoin", "from_port": "audio_flow_id",
|
||||
"to": "ndiout", "to_port": "audio_flow_id",
|
||||
"format": { "kind": "audio", "sample_rate": 44100, "channels": 2, "bit_depth": 32 }
|
||||
"format": { "kind": "audio", "sample_rate": 48000, "channels": 6, "bit_depth": 32 }
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user