d95e859db8b44aedebb8de4668e1d9eb1c8c6361
[dcpomatic.git] / src / lib / analyse_subtitles_job.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "analyse_subtitles_job.h"
22 #include "playlist.h"
23 #include "player.h"
24 #include "subtitle_analysis.h"
25 #include "bitmap_text.h"
26 #include "render_text.h"
27 #include "text_content.h"
28 #include "image.h"
29 #include <iostream>
30
31 #include "i18n.h"
32
33 using std::make_shared;
34 using std::shared_ptr;
35 using std::string;
36 using std::weak_ptr;
37 #if BOOST_VERSION >= 106100
38 using namespace boost::placeholders;
39 #endif
40
41 AnalyseSubtitlesJob::AnalyseSubtitlesJob (shared_ptr<const Film> film, shared_ptr<Content> content)
42         : Job (film)
43         , _content (content)
44         , _path (_film->subtitle_analysis_path(content))
45 {
46 }
47
48
49 string
50 AnalyseSubtitlesJob::name () const
51 {
52         return _("Analysing subtitles");
53 }
54
55
56 string
57 AnalyseSubtitlesJob::json_name () const
58 {
59         return N_("analyse_subtitles");
60 }
61
62
63 void
64 AnalyseSubtitlesJob::run ()
65 {
66         auto playlist = make_shared<Playlist>();
67         auto content = _content.lock ();
68         DCPOMATIC_ASSERT (content);
69         playlist->add (_film, content);
70
71         auto player = make_shared<Player>(_film, playlist);
72         player->set_ignore_audio ();
73         player->set_fast ();
74         player->set_play_referenced ();
75         player->Text.connect (bind(&AnalyseSubtitlesJob::analyse, this, _1, _2));
76
77         set_progress_unknown ();
78
79         if (!content->text.empty()) {
80                 while (!player->pass ()) {}
81         }
82
83         SubtitleAnalysis analysis (_bounding_box, content->text.front()->x_offset(), content->text.front()->y_offset());
84         analysis.write (_path);
85
86         set_progress (1);
87         set_state (FINISHED_OK);
88 }
89
90
91 void
92 AnalyseSubtitlesJob::analyse (PlayerText text, TextType type)
93 {
94         if (type != TextType::OPEN_SUBTITLE) {
95                 return;
96         }
97
98         for (auto const& i: text.bitmap) {
99                 if (!_bounding_box) {
100                         _bounding_box = i.rectangle;
101                 } else {
102                         _bounding_box->extend (i.rectangle);
103                 }
104         }
105
106         if (!text.string.empty()) {
107                 /* We can provide dummy values for time and frame rate here as they are only used to calculate fades */
108                 dcp::Size const frame = _film->frame_size();
109                 for (auto i: render_text(text.string, text.fonts, frame, dcpomatic::DCPTime(), 24)) {
110                         dcpomatic::Rect<double> rect (
111                                         double(i.position.x) / frame.width, double(i.position.y) / frame.height,
112                                         double(i.image->size().width) / frame.width, double(i.image->size().height) / frame.height
113                                         );
114                         if (!_bounding_box) {
115                                 _bounding_box = rect;
116                         } else {
117                                 _bounding_box->extend (rect);
118                         }
119                 }
120         }
121 }
122