From 7b4225aa1f946d8ba64d4c3fcc07cb449c903cad Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 24 Aug 2021 13:26:28 +0200 Subject: [PATCH] lib/ffmpeg/Filter: add ParseSingleInOut() Merge some duplicate code. --- src/filter/plugins/FfmpegFilterPlugin.cxx | 12 +----------- src/filter/plugins/HdcdFilterPlugin.cxx | 13 +------------ src/lib/ffmpeg/Filter.cxx | 13 +++++++++++++ src/lib/ffmpeg/Filter.hxx | 3 +++ 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/filter/plugins/FfmpegFilterPlugin.cxx b/src/filter/plugins/FfmpegFilterPlugin.cxx index 0cbd85821..db377ca5e 100644 --- a/src/filter/plugins/FfmpegFilterPlugin.cxx +++ b/src/filter/plugins/FfmpegFilterPlugin.cxx @@ -47,17 +47,7 @@ PreparedFfmpegFilter::Open(AudioFormat &in_audio_format) auto &buffer_sink = Ffmpeg::MakeAudioBufferSink(*graph); - Ffmpeg::FilterInOut io_sink("out", buffer_sink); - Ffmpeg::FilterInOut io_src("in", buffer_src); - auto io = graph.Parse(graph_string, std::move(io_sink), - std::move(io_src)); - - if (io.first.get() != nullptr) - throw std::runtime_error("FFmpeg filter has an open input"); - - if (io.second.get() != nullptr) - throw std::runtime_error("FFmpeg filter has an open output"); - + graph.ParseSingleInOut(graph_string, buffer_sink, buffer_src); graph.CheckAndConfigure(); const auto out_audio_format = diff --git a/src/filter/plugins/HdcdFilterPlugin.cxx b/src/filter/plugins/HdcdFilterPlugin.cxx index a1781f34d..29aedc40a 100644 --- a/src/filter/plugins/HdcdFilterPlugin.cxx +++ b/src/filter/plugins/HdcdFilterPlugin.cxx @@ -48,18 +48,7 @@ OpenHdcdFilter(AudioFormat &in_audio_format) auto &buffer_sink = Ffmpeg::MakeAudioBufferSink(*graph); - Ffmpeg::FilterInOut io_sink("out", buffer_sink); - Ffmpeg::FilterInOut io_src("in", buffer_src); - - auto io = graph.Parse(hdcd_graph, std::move(io_sink), - std::move(io_src)); - - if (io.first.get() != nullptr) - throw std::runtime_error("FFmpeg filter has an open input"); - - if (io.second.get() != nullptr) - throw std::runtime_error("FFmpeg filter has an open output"); - + graph.ParseSingleInOut(hdcd_graph, buffer_sink, buffer_src); graph.CheckAndConfigure(); auto out_audio_format = in_audio_format; diff --git a/src/lib/ffmpeg/Filter.cxx b/src/lib/ffmpeg/Filter.cxx index c5e7a3a5c..51e43dc3d 100644 --- a/src/lib/ffmpeg/Filter.cxx +++ b/src/lib/ffmpeg/Filter.cxx @@ -100,4 +100,17 @@ MakeAudioBufferSink(AVFilterGraph &graph_ctx) graph_ctx); } +void +FilterGraph::ParseSingleInOut(const char *filters, AVFilterContext &in, + AVFilterContext &out) +{ + auto [inputs, outputs] = Parse(filters, {"out", in}, {"in", out}); + + if (inputs.get() != nullptr) + throw std::runtime_error("FFmpeg filter has an open input"); + + if (outputs.get() != nullptr) + throw std::runtime_error("FFmpeg filter has an open output"); +} + } // namespace Ffmpeg diff --git a/src/lib/ffmpeg/Filter.hxx b/src/lib/ffmpeg/Filter.hxx index 0cac102f7..f57af3c1b 100644 --- a/src/lib/ffmpeg/Filter.hxx +++ b/src/lib/ffmpeg/Filter.hxx @@ -138,6 +138,9 @@ public: return std::make_pair(std::move(inputs), std::move(outputs)); } + void ParseSingleInOut(const char *filters, AVFilterContext &in, + AVFilterContext &out); + std::pair Parse(const char *filters) { AVFilterInOut *inputs, *outputs; int err = avfilter_graph_parse2(graph, filters,