diff options
| author | Carl Hetherington <cth@carlh.net> | 2020-04-23 00:11:38 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2020-04-24 00:46:31 +0200 |
| commit | cef7a679a59044a5c807768042deecfd56ec6fc2 (patch) | |
| tree | b0e020a2a70db228df0e96fd85b63a84d7d69b64 /src/lib | |
| parent | 87d5a977da0696fbe73b96b2679b7cbb471e7255 (diff) | |
Add subtitle analysis so that the outline of all subtitles
in a piece of content can be overlaid onto the preview (#1233).
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/analyse_subtitles_job.cc | 118 | ||||
| -rw-r--r-- | src/lib/analyse_subtitles_job.h | 48 | ||||
| -rw-r--r-- | src/lib/film.cc | 35 | ||||
| -rw-r--r-- | src/lib/film.h | 1 | ||||
| -rw-r--r-- | src/lib/job_manager.cc | 39 | ||||
| -rw-r--r-- | src/lib/job_manager.h | 10 | ||||
| -rw-r--r-- | src/lib/subtitle_analysis.cc | 75 | ||||
| -rw-r--r-- | src/lib/subtitle_analysis.h | 69 | ||||
| -rw-r--r-- | src/lib/wscript | 2 |
9 files changed, 395 insertions, 2 deletions
diff --git a/src/lib/analyse_subtitles_job.cc b/src/lib/analyse_subtitles_job.cc new file mode 100644 index 000000000..7a1c4ab00 --- /dev/null +++ b/src/lib/analyse_subtitles_job.cc @@ -0,0 +1,118 @@ +/* + Copyright (C) 2020 Carl Hetherington <cth@carlh.net> + + 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 <http://www.gnu.org/licenses/>. + +*/ + +#include "analyse_subtitles_job.h" +#include "playlist.h" +#include "player.h" +#include "subtitle_analysis.h" +#include "bitmap_text.h" +#include "render_text.h" +#include "text_content.h" +#include "image.h" +#include <iostream> + +#include "i18n.h" + +using std::string; +using boost::shared_ptr; +using boost::weak_ptr; + +AnalyseSubtitlesJob::AnalyseSubtitlesJob (shared_ptr<const Film> film, shared_ptr<Content> content) + : Job (film) + , _content (content) + , _path (_film->subtitle_analysis_path(content)) +{ +} + + +string +AnalyseSubtitlesJob::name () const +{ + return _("Analysing subtitles"); +} + + +string +AnalyseSubtitlesJob::json_name () const +{ + return N_("analyse_subtitles"); +} + + +void +AnalyseSubtitlesJob::run () +{ + shared_ptr<Playlist> playlist (new Playlist()); + shared_ptr<Content> content = _content.lock (); + DCPOMATIC_ASSERT (content); + playlist->add (_film, content); + + shared_ptr<Player> player (new Player(_film, playlist)); + player->set_ignore_audio (); + player->set_fast (); + player->set_play_referenced (); + player->Text.connect (bind(&AnalyseSubtitlesJob::analyse, this, _1, _2)); + + set_progress_unknown (); + + if (!content->text.empty()) { + while (!player->pass ()) {} + } + + SubtitleAnalysis analysis (_bounding_box, content->text.front()->x_offset(), content->text.front()->y_offset()); + analysis.write (_path); + + set_progress (1); + set_state (FINISHED_OK); +} + + +void +AnalyseSubtitlesJob::analyse (PlayerText text, TextType type) +{ + if (type != TEXT_OPEN_SUBTITLE) { + return; + } + + BOOST_FOREACH (BitmapText const& i, text.bitmap) { + if (!_bounding_box) { + _bounding_box = i.rectangle; + } else { + _bounding_box->extend (i.rectangle); + } + } + + if (!text.string.empty()) { + /* We can provide dummy values for time and frame rate here as they are only used to calculate fades */ + dcp::Size const frame = _film->frame_size(); + BOOST_FOREACH (PositionImage i, render_text(text.string, text.fonts, frame, dcpomatic::DCPTime(), 24)) { + dcpomatic::Rect<double> rect ( + double(i.position.x) / frame.width, double(i.position.y) / frame.height, + double(i.image->size().width) / frame.width, double(i.image->size().height) / frame.height + ); + if (!_bounding_box) { + _bounding_box = rect; + } else { + _bounding_box->extend (rect); + } + } + } +} + diff --git a/src/lib/analyse_subtitles_job.h b/src/lib/analyse_subtitles_job.h new file mode 100644 index 000000000..88b5d23dc --- /dev/null +++ b/src/lib/analyse_subtitles_job.h @@ -0,0 +1,48 @@ +/* + Copyright (C) 2020 Carl Hetherington <cth@carlh.net> + + 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 <http://www.gnu.org/licenses/>. + +*/ + +#include "job.h" +#include "types.h" +#include "player_text.h" + +class Film; +class Content; + +class AnalyseSubtitlesJob : public Job +{ +public: + AnalyseSubtitlesJob (boost::shared_ptr<const Film> film, boost::shared_ptr<Content> content); + + std::string name () const; + std::string json_name () const; + void run (); + + boost::filesystem::path path () const { + return _path; + } + +private: + void analyse (PlayerText text, TextType type); + + boost::weak_ptr<Content> _content; + boost::filesystem::path _path; + boost::optional<dcpomatic::Rect<double> > _bounding_box; +}; + diff --git a/src/lib/film.cc b/src/lib/film.cc index a89d58a1f..a24e9aa30 100644 --- a/src/lib/film.cc +++ b/src/lib/film.cc @@ -54,6 +54,8 @@ #include "cinema.h" #include "change_signaller.h" #include "check_content_change_job.h" +#include "ffmpeg_subtitle_stream.h" +#include "font.h" #include <libcxml/cxml.h> #include <dcp/cpl.h> #include <dcp/certificate_chain.h> @@ -302,6 +304,39 @@ Film::audio_analysis_path (shared_ptr<const Playlist> playlist) const return p; } + +boost::filesystem::path +Film::subtitle_analysis_path (shared_ptr<const Content> content) const +{ + boost::filesystem::path p = dir ("analysis"); + + Digester digester; + digester.add (content->digest()); + + if (!content->text.empty()) { + shared_ptr<TextContent> tc = content->text.front(); + digester.add (tc->x_scale()); + digester.add (tc->y_scale()); + BOOST_FOREACH (shared_ptr<dcpomatic::Font> i, tc->fonts()) { + digester.add (i->id()); + } + if (tc->effect()) { + digester.add (tc->effect().get()); + } + digester.add (tc->line_spacing()); + digester.add (tc->outline_width()); + } + + shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent>(content); + if (fc) { + digester.add (fc->subtitle_stream()->identifier()); + } + + p /= digester.get (); + return p; +} + + /** Add suitable Jobs to the JobManager to create a DCP for this Film. * @param gui true if this is being called from a GUI tool. * @param check true to check the content in the project for changes before making the DCP. diff --git a/src/lib/film.h b/src/lib/film.h index 86e9be6d9..6cce07c17 100644 --- a/src/lib/film.h +++ b/src/lib/film.h @@ -106,6 +106,7 @@ public: boost::filesystem::path internal_video_asset_filename (dcpomatic::DCPTimePeriod p) const; boost::filesystem::path audio_analysis_path (boost::shared_ptr<const Playlist>) const; + boost::filesystem::path subtitle_analysis_path (boost::shared_ptr<const Content>) const; void send_dcp_to_tms (); void make_dcp (bool gui = false, bool check = true); diff --git a/src/lib/job_manager.cc b/src/lib/job_manager.cc index d95f95a24..c40178f41 100644 --- a/src/lib/job_manager.cc +++ b/src/lib/job_manager.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net> + Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net> This file is part of DCP-o-matic. @@ -26,6 +26,7 @@ #include "job.h" #include "cross.h" #include "analyse_audio_job.h" +#include "analyse_subtitles_job.h" #include "film.h" #include <boost/thread.hpp> #include <boost/foreach.hpp> @@ -250,6 +251,42 @@ JobManager::analyse_audio ( emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job))); } + +void +JobManager::analyse_subtitles ( + shared_ptr<const Film> film, + shared_ptr<Content> content, + boost::signals2::connection& connection, + function<void()> ready + ) +{ + { + boost::mutex::scoped_lock lm (_mutex); + + BOOST_FOREACH (shared_ptr<Job> i, _jobs) { + shared_ptr<AnalyseSubtitlesJob> a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i); + if (a && a->path() == film->subtitle_analysis_path(content)) { + i->when_finished (connection, ready); + return; + } + } + } + + shared_ptr<AnalyseSubtitlesJob> job; + + { + boost::mutex::scoped_lock lm (_mutex); + + job.reset (new AnalyseSubtitlesJob(film, content)); + connection = job->Finished.connect (ready); + _jobs.push_back (job); + _empty_condition.notify_all (); + } + + emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job))); +} + + void JobManager::increase_priority (shared_ptr<Job> job) { diff --git a/src/lib/job_manager.h b/src/lib/job_manager.h index d4287f874..dd7a28db2 100644 --- a/src/lib/job_manager.h +++ b/src/lib/job_manager.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net> + Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net> This file is part of DCP-o-matic. @@ -32,6 +32,7 @@ class Job; class Film; class Playlist; +class Content; struct threed_test7; extern bool wait_for_jobs (); @@ -64,6 +65,13 @@ public: boost::function<void()> ready ); + void analyse_subtitles ( + boost::shared_ptr<const Film> film, + boost::shared_ptr<Content> content, + boost::signals2::connection& connection, + boost::function<void()> ready + ); + boost::signals2::signal<void (boost::weak_ptr<Job>)> JobAdded; boost::signals2::signal<void ()> JobsReordered; boost::signals2::signal<void (boost::optional<std::string>, boost::optional<std::string>)> ActiveJobsChanged; diff --git a/src/lib/subtitle_analysis.cc b/src/lib/subtitle_analysis.cc new file mode 100644 index 000000000..91fec90b0 --- /dev/null +++ b/src/lib/subtitle_analysis.cc @@ -0,0 +1,75 @@ +/* + Copyright (C) 2020 Carl Hetherington <cth@carlh.net> + + 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 <http://www.gnu.org/licenses/>. + +*/ + +#include "subtitle_analysis.h" +#include "exceptions.h" +#include <libcxml/cxml.h> +#include <dcp/raw_convert.h> +#include <libxml++/libxml++.h> + +using std::string; +using dcp::raw_convert; +using boost::shared_ptr; + +int const SubtitleAnalysis::_current_state_version = 1; + + +SubtitleAnalysis::SubtitleAnalysis (boost::filesystem::path path) +{ + cxml::Document f ("SubtitleAnalysis"); + + f.read_file (path); + + if (f.optional_number_child<int>("Version").get_value_or(1) < _current_state_version) { + /* Too old. Throw an exception so that this analysis is re-run. */ + throw OldFormatError ("Audio analysis file is too old"); + } + + cxml::NodePtr bounding_box = f.optional_node_child("BoundingBox"); + if (bounding_box) { + _bounding_box = dcpomatic::Rect<double> (); + _bounding_box->x = bounding_box->number_child<double>("X"); + _bounding_box->y = bounding_box->number_child<double>("Y"); + _bounding_box->width = bounding_box->number_child<double>("Width"); + _bounding_box->height = bounding_box->number_child<double>("Height"); + } +} + + +void +SubtitleAnalysis::write (boost::filesystem::path path) const +{ + shared_ptr<xmlpp::Document> doc (new xmlpp::Document); + xmlpp::Element* root = doc->create_root_node ("SubtitleAnalysis"); + + root->add_child("Version")->add_child_text (raw_convert<string>(_current_state_version)); + + if (_bounding_box) { + xmlpp::Element* bounding_box = root->add_child("BoundingBox"); + bounding_box->add_child("X")->add_child_text(raw_convert<string>(_bounding_box->x)); + bounding_box->add_child("Y")->add_child_text(raw_convert<string>(_bounding_box->y)); + bounding_box->add_child("Width")->add_child_text(raw_convert<string>(_bounding_box->width)); + bounding_box->add_child("Height")->add_child_text(raw_convert<string>(_bounding_box->height)); + } + + doc->write_to_file_formatted (path.string()); +} + + diff --git a/src/lib/subtitle_analysis.h b/src/lib/subtitle_analysis.h new file mode 100644 index 000000000..47eb52411 --- /dev/null +++ b/src/lib/subtitle_analysis.h @@ -0,0 +1,69 @@ +/* + Copyright (C) 2020 Carl Hetherington <cth@carlh.net> + + 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 <http://www.gnu.org/licenses/>. + +*/ + +#include "rect.h" +#include <boost/noncopyable.hpp> +#include <boost/filesystem.hpp> + + +/** @class SubtitleAnalysis + * @brief Class to store the results of a SubtitleAnalysisJob. + */ + +class SubtitleAnalysis : public boost::noncopyable +{ +public: + explicit SubtitleAnalysis (boost::filesystem::path path); + + SubtitleAnalysis ( + boost::optional<dcpomatic::Rect<double> > bounding_box, + double analysis_x_offset_, + double analysis_y_offset_ + ) + : _bounding_box (bounding_box) + , _analysis_x_offset (analysis_x_offset_) + , _analysis_y_offset (analysis_y_offset_) + {} + + void write (boost::filesystem::path path) const; + + boost::optional<dcpomatic::Rect<double> > bounding_box () const { + return _bounding_box; + } + + double analysis_x_offset () const { + return _analysis_x_offset; + } + + double analysis_y_offset () const { + return _analysis_x_offset; + } + +private: + /** Smallest box which surrounds all subtitles in our content, + * expressed as a proportion of screen size (i.e. 0 is left hand side/top, + * 1 is right hand side/bottom), or empty if no subtitles were found. + */ + boost::optional<dcpomatic::Rect<double> > _bounding_box; + double _analysis_x_offset; + double _analysis_y_offset; + + static int const _current_state_version; +}; diff --git a/src/lib/wscript b/src/lib/wscript index 17f96d61e..802020527 100644 --- a/src/lib/wscript +++ b/src/lib/wscript @@ -23,6 +23,7 @@ import i18n sources = """ active_text.cc analyse_audio_job.cc + analyse_subtitles_job.cc analytics.cc atmos_mxf_content.cc atomicity_checker.cc @@ -157,6 +158,7 @@ sources = """ string_text_file.cc string_text_file_content.cc string_text_file_decoder.cc + subtitle_analysis.cc subtitle_encoder.cc text_ring_buffers.cc timer.cc |
