diff --git a/nodes/videoin/main.cpp b/nodes/videoin/main.cpp index 315b485..0361a42 100644 --- a/nodes/videoin/main.cpp +++ b/nodes/videoin/main.cpp @@ -1,11 +1,3 @@ -extern "C" { - #include - #include - #include - #include - #include -} - #include #include #include @@ -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(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,11 +74,10 @@ 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(max_write); } - - size_t max_write = 0; - mxlFlowWriterGetMaxWriteLengthSamples(audio_writer, &max_write); - max_audio_samples = static_cast(max_write); } std::vector audio_temp(max_audio_samples * channels * sizeof(float)); @@ -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, diff --git a/shared/VideoReader.hpp b/shared/VideoReader.hpp index 3c620fc..2ae087d 100644 --- a/shared/VideoReader.hpp +++ b/shared/VideoReader.hpp @@ -16,8 +16,6 @@ extern "C" { #include "Signal.hpp" #include "V210.hpp" -#include - namespace dmf { class VideoReader { @@ -31,10 +29,8 @@ public: }; struct AudioInfo { - int sample_rate = 0; - int channels = 0; - int samples = 0; - int channel_stride = 0; // floats between channel planes (NDI planar layout) + int sample_rate = 0; + int channels = 0; }; 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(i); - have_video = true; + has_video = true; } else if (type == AVMEDIA_TYPE_AUDIO && audio_stream_index == -1) { audio_stream_index = static_cast(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"); } - } };