refactor: VideoReader and videoin post-audio review cleanup
VideoReader: - Remove unused #include <iostream> - Remove dead AudioInfo::samples and ::channel_stride fields - Rename have_video → has_video (consistent with has_audio) - Rename get_next_frame → read_next (returns audio too, not just frames) - Fix outdated comment on read_next - Remove trailing blank line in allocate_audio_conversion_buffers videoin: - Remove redundant FFmpeg includes (VideoReader.hpp provides them) - Fix bug: mxlFlowWriterGetMaxWriteLengthSamples called with invalid audio_writer when mxlCreateFlowWriter fails — moved inside else branch - Rename call site: get_next_frame → read_next - Rename have_video → has_video at call sites - Use = nullptr for audio_writer (consistent with video_writer) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+5
-14
@@ -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 <string>
|
||||||
#include <mxl/flow.h>
|
#include <mxl/flow.h>
|
||||||
#include <mxl/time.h>
|
#include <mxl/time.h>
|
||||||
@@ -21,11 +13,11 @@ class VideoInNode : public dmf::NodeBase {
|
|||||||
log("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 && !video_reader.has_audio) {
|
if (!video_reader.has_video && !video_reader.has_audio) {
|
||||||
log("no video or audio stream found"); return;
|
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;
|
mxlFlowWriter video_writer = nullptr;
|
||||||
mxlFlowConfigInfo video_cfg{};
|
mxlFlowConfigInfo video_cfg{};
|
||||||
uint32_t video_stride = 0;
|
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);
|
video_stride, video_stride * static_cast<uint32_t>(height), video_cfg.discrete.grainCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
mxlFlowWriter audio_writer{};
|
mxlFlowWriter audio_writer = nullptr;
|
||||||
mxlFlowConfigInfo audio_cfg{};
|
mxlFlowConfigInfo audio_cfg{};
|
||||||
int sample_rate = video_reader.audio_info.sample_rate;
|
int sample_rate = video_reader.audio_info.sample_rate;
|
||||||
int channels = video_reader.audio_info.channels;
|
int channels = video_reader.audio_info.channels;
|
||||||
@@ -82,12 +74,11 @@ class VideoInNode : public dmf::NodeBase {
|
|||||||
} else {
|
} else {
|
||||||
log("audio channels=%u buffer=%u samples",
|
log("audio channels=%u buffer=%u samples",
|
||||||
audio_cfg.continuous.channelCount, audio_cfg.continuous.bufferLength);
|
audio_cfg.continuous.channelCount, audio_cfg.continuous.bufferLength);
|
||||||
}
|
|
||||||
|
|
||||||
size_t max_write = 0;
|
size_t max_write = 0;
|
||||||
mxlFlowWriterGetMaxWriteLengthSamples(audio_writer, &max_write);
|
mxlFlowWriterGetMaxWriteLengthSamples(audio_writer, &max_write);
|
||||||
max_audio_samples = static_cast<int>(max_write);
|
max_audio_samples = static_cast<int>(max_write);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
std::vector<uint8_t> audio_temp(max_audio_samples * channels * sizeof(float));
|
std::vector<uint8_t> audio_temp(max_audio_samples * channels * sizeof(float));
|
||||||
|
|
||||||
const mxlRational video_rate = {fps_num, fps_den};
|
const mxlRational video_rate = {fps_num, fps_den};
|
||||||
@@ -107,7 +98,7 @@ class VideoInNode : public dmf::NodeBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int out_samples_written = 0;
|
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,
|
has_video ? video_buf : nullptr,
|
||||||
video_stride,
|
video_stride,
|
||||||
has_audio ? audio_temp.data() : nullptr,
|
has_audio ? audio_temp.data() : nullptr,
|
||||||
|
|||||||
+9
-15
@@ -16,8 +16,6 @@ extern "C" {
|
|||||||
#include "Signal.hpp"
|
#include "Signal.hpp"
|
||||||
#include "V210.hpp"
|
#include "V210.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace dmf {
|
namespace dmf {
|
||||||
|
|
||||||
class VideoReader {
|
class VideoReader {
|
||||||
@@ -33,8 +31,6 @@ public:
|
|||||||
struct AudioInfo {
|
struct AudioInfo {
|
||||||
int sample_rate = 0;
|
int sample_rate = 0;
|
||||||
int channels = 0;
|
int channels = 0;
|
||||||
int samples = 0;
|
|
||||||
int channel_stride = 0; // floats between channel planes (NDI planar layout)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class FrameKind { None, Video, Audio };
|
enum class FrameKind { None, Video, Audio };
|
||||||
@@ -42,14 +38,14 @@ public:
|
|||||||
VideoInfo video_info{};
|
VideoInfo video_info{};
|
||||||
AudioInfo audio_info{};
|
AudioInfo audio_info{};
|
||||||
bool has_audio = false;
|
bool has_audio = false;
|
||||||
bool have_video = false;
|
bool has_video = false;
|
||||||
|
|
||||||
explicit VideoReader(const std::string& filename) {
|
explicit VideoReader(const std::string& filename) {
|
||||||
if (!open_file(filename))
|
if (!open_file(filename))
|
||||||
return;
|
return;
|
||||||
if (!have_video && !has_audio) return;
|
if (!has_video && !has_audio) return;
|
||||||
get_source_info();
|
get_source_info();
|
||||||
if (have_video) allocate_video_conversion_buffers();
|
if (has_video) allocate_video_conversion_buffers();
|
||||||
if (has_audio) allocate_audio_conversion_buffers();
|
if (has_audio) allocate_audio_conversion_buffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,12 +61,11 @@ public:
|
|||||||
av_packet_free(&packet);
|
av_packet_free(&packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true when a frame was decoded and written into video_buf.
|
// Returns Video or Audio when a frame/packet was decoded, None when g_running goes false.
|
||||||
// Returns false 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) {
|
||||||
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)) {
|
while (dmf::g_running.load(std::memory_order_relaxed)) {
|
||||||
// Drain any frames buffered in the decoder first
|
// 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) {
|
if (!video_buf) {
|
||||||
av_frame_unref(video_frame);
|
av_frame_unref(video_frame);
|
||||||
continue; // nowhere to write — discard frame
|
continue; // nowhere to write — discard frame
|
||||||
@@ -138,7 +133,7 @@ public:
|
|||||||
if (av_read_frame(format_context, packet) < 0) {
|
if (av_read_frame(format_context, packet) < 0) {
|
||||||
// EOF — loop back to start
|
// 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);
|
||||||
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);
|
if (has_audio) avcodec_flush_buffers(audio_codec_context);
|
||||||
swr_close(swr_audio_ctx);
|
swr_close(swr_audio_ctx);
|
||||||
swr_init(swr_audio_ctx);
|
swr_init(swr_audio_ctx);
|
||||||
@@ -184,7 +179,7 @@ private:
|
|||||||
const AVMediaType type = format_context->streams[i]->codecpar->codec_type;
|
const AVMediaType type = format_context->streams[i]->codecpar->codec_type;
|
||||||
if (type == AVMEDIA_TYPE_VIDEO && video_stream_index == -1) {
|
if (type == AVMEDIA_TYPE_VIDEO && video_stream_index == -1) {
|
||||||
video_stream_index = static_cast<int>(i);
|
video_stream_index = static_cast<int>(i);
|
||||||
have_video = true;
|
has_video = true;
|
||||||
} else if (type == AVMEDIA_TYPE_AUDIO && audio_stream_index == -1) {
|
} else if (type == AVMEDIA_TYPE_AUDIO && audio_stream_index == -1) {
|
||||||
audio_stream_index = static_cast<int>(i);
|
audio_stream_index = static_cast<int>(i);
|
||||||
has_audio = true;
|
has_audio = true;
|
||||||
@@ -225,7 +220,7 @@ private:
|
|||||||
video_info.fps_den = fps.den;
|
video_info.fps_den = fps.den;
|
||||||
video_info.pix_fmt = video_codec_context->pix_fmt;
|
video_info.pix_fmt = video_codec_context->pix_fmt;
|
||||||
} else {
|
} else {
|
||||||
have_video = false;
|
has_video = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// audio part
|
// audio part
|
||||||
@@ -279,7 +274,6 @@ private:
|
|||||||
if (swr_init(swr_audio_ctx) < 0) {
|
if (swr_init(swr_audio_ctx) < 0) {
|
||||||
throw std::runtime_error("Failed to create SwrContext");
|
throw std::runtime_error("Failed to create SwrContext");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user