From bdbec5b501af2c020dd783eb8b0b8c18c34b6552 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 6 Oct 2022 23:38:07 +0200 Subject: [PATCH] Extract VideoFilterGraphSet. --- src/lib/ffmpeg_decoder.cc | 19 +--------- src/lib/ffmpeg_decoder.h | 3 +- src/lib/video_filter_graph_set.cc | 62 +++++++++++++++++++++++++++++++ src/lib/video_filter_graph_set.h | 61 ++++++++++++++++++++++++++++++ src/lib/wscript | 1 + 5 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 src/lib/video_filter_graph_set.cc create mode 100644 src/lib/video_filter_graph_set.h diff --git a/src/lib/ffmpeg_decoder.cc b/src/lib/ffmpeg_decoder.cc index 986e51ab6..27b7aa7b7 100644 --- a/src/lib/ffmpeg_decoder.cc +++ b/src/lib/ffmpeg_decoder.cc @@ -77,6 +77,7 @@ using namespace dcpomatic; FFmpegDecoder::FFmpegDecoder (shared_ptr film, shared_ptr c, bool fast) : FFmpeg (c) , Decoder (film) + , _filter_graphs(c->filters(), dcp::Fraction(lrint(_ffmpeg_content->video_frame_rate().get_value_or(24) * 1000), 1000)) { if (c->video && c->video->use()) { video = make_shared(this, c); @@ -577,23 +578,7 @@ FFmpegDecoder::decode_and_process_video_packet (AVPacket* packet) void FFmpegDecoder::process_video_frame () { - shared_ptr graph; - - auto i = _filter_graphs.begin(); - while (i != _filter_graphs.end() && !(*i)->can_process(dcp::Size(_video_frame->width, _video_frame->height), (AVPixelFormat) _video_frame->format)) { - ++i; - } - - if (i == _filter_graphs.end ()) { - dcp::Fraction vfr (lrint(_ffmpeg_content->video_frame_rate().get() * 1000), 1000); - graph = make_shared(dcp::Size(_video_frame->width, _video_frame->height), (AVPixelFormat) _video_frame->format, vfr); - graph->setup (_ffmpeg_content->filters ()); - _filter_graphs.push_back (graph); - LOG_GENERAL (N_("New graph for %1x%2, pixel format %3"), _video_frame->width, _video_frame->height, _video_frame->format); - } else { - graph = *i; - } - + auto graph = _filter_graphs.get(dcp::Size(_video_frame->width, _video_frame->height), static_cast(_video_frame->format)); auto images = graph->process (_video_frame); for (auto const& i: images) { diff --git a/src/lib/ffmpeg_decoder.h b/src/lib/ffmpeg_decoder.h index c6d76f1e8..9de44333c 100644 --- a/src/lib/ffmpeg_decoder.h +++ b/src/lib/ffmpeg_decoder.h @@ -27,6 +27,7 @@ #include "bitmap_text.h" #include "decoder.h" #include "ffmpeg.h" +#include "video_filter_graph_set.h" #include "util.h" extern "C" { #include @@ -76,7 +77,7 @@ private: void maybe_add_subtitle (); - std::list> _filter_graphs; + VideoFilterGraphSet _filter_graphs; dcpomatic::ContentTime _pts_offset; boost::optional _current_subtitle_to; diff --git a/src/lib/video_filter_graph_set.cc b/src/lib/video_filter_graph_set.cc new file mode 100644 index 000000000..dbae17d4c --- /dev/null +++ b/src/lib/video_filter_graph_set.cc @@ -0,0 +1,62 @@ +/* + Copyright (C) 2022 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + + +#include "dcpomatic_log.h" +#include "video_filter_graph.h" +#include "video_filter_graph_set.h" + +#include "i18n.h" + + +using std::make_shared; +using std::shared_ptr; + + +shared_ptr +VideoFilterGraphSet::get(dcp::Size size, AVPixelFormat format) +{ + auto graph = std::find_if( + _graphs.begin(), + _graphs.end(), + [size, format](shared_ptr g) { + return g->can_process(size, format); + }); + + if (graph != _graphs.end()) { + return *graph; + } + + auto new_graph = make_shared(size, format, _frame_rate); + new_graph->setup(_filters); + _graphs.push_back(new_graph); + + LOG_GENERAL(N_("New graph for %1x%2, pixel format %3"), size.width, size.height, static_cast(format)); + + return new_graph; +} + + +void +VideoFilterGraphSet::clear() +{ + _graphs.clear(); +} + diff --git a/src/lib/video_filter_graph_set.h b/src/lib/video_filter_graph_set.h new file mode 100644 index 000000000..935378432 --- /dev/null +++ b/src/lib/video_filter_graph_set.h @@ -0,0 +1,61 @@ +/* + Copyright (C) 2022 Carl Hetherington + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see . + +*/ + + +#ifndef DCPOMATIC_VIDEO_FILTER_GRAPH_SET_H +#define DCPOMATIC_VIDEO_FILTER_GRAPH_SET_H + + +#include +extern "C" { +#include +} +#include +#include + + +class Filter; +class VideoFilterGraph; + + +class VideoFilterGraphSet +{ +public: + VideoFilterGraphSet(std::vector filters, dcp::Fraction frame_rate) + : _filters(filters) + , _frame_rate(frame_rate) + {} + + VideoFilterGraphSet(VideoFilterGraphSet const&) = delete; + VideoFilterGraphSet& operator=(VideoFilterGraphSet const&) = delete; + + std::shared_ptr get(dcp::Size size, AVPixelFormat format); + + void clear(); + +private: + std::vector _filters; + dcp::Fraction _frame_rate; + std::vector> _graphs; +}; + + +#endif + diff --git a/src/lib/wscript b/src/lib/wscript index 55c4e735f..26fcb21e5 100644 --- a/src/lib/wscript +++ b/src/lib/wscript @@ -194,6 +194,7 @@ sources = """ video_content.cc video_decoder.cc video_filter_graph.cc + video_filter_graph_set.cc video_mxf_content.cc video_mxf_decoder.cc video_mxf_examiner.cc -- 2.30.2