Add subtitle analysis so that the outline of all subtitles
[dcpomatic.git] / src / lib / analyse_subtitles_job.cc
diff --git a/src/lib/analyse_subtitles_job.cc b/src/lib/analyse_subtitles_job.cc
new file mode 100644 (file)
index 0000000..7a1c4ab
--- /dev/null
@@ -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);
+                       }
+               }
+       }
+}
+