92fb9657c143019486a6ff22b992926ca56f3570
[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::string;
34 using boost::shared_ptr;
35 using boost::weak_ptr;
36 #if BOOST_VERSION >= 106100
37 using namespace boost::placeholders;
38 #endif
39
40 AnalyseSubtitlesJob::AnalyseSubtitlesJob (shared_ptr<const Film> film, shared_ptr<Content> content)
41         : Job (film)
42         , _content (content)
43         , _path (_film->subtitle_analysis_path(content))
44 {
45 }
46
47
48 string
49 AnalyseSubtitlesJob::name () const
50 {
51         return _("Analysing subtitles");
52 }
53
54
55 string
56 AnalyseSubtitlesJob::json_name () const
57 {
58         return N_("analyse_subtitles");
59 }
60
61
62 void
63 AnalyseSubtitlesJob::run ()
64 {
65         shared_ptr<Playlist> playlist (new Playlist());
66         shared_ptr<Content> content = _content.lock ();
67         DCPOMATIC_ASSERT (content);
68         playlist->add (_film, content);
69
70         shared_ptr<Player> player (new Player(_film, playlist));
71         player->set_ignore_audio ();
72         player->set_fast ();
73         player->set_play_referenced ();
74         player->Text.connect (bind(&AnalyseSubtitlesJob::analyse, this, _1, _2));
75
76         set_progress_unknown ();
77
78         if (!content->text.empty()) {
79                 while (!player->pass ()) {}
80         }
81
82         SubtitleAnalysis analysis (_bounding_box, content->text.front()->x_offset(), content->text.front()->y_offset());
83         analysis.write (_path);
84
85         set_progress (1);
86         set_state (FINISHED_OK);
87 }
88
89
90 void
91 AnalyseSubtitlesJob::analyse (PlayerText text, TextType type)
92 {
93         if (type != TEXT_OPEN_SUBTITLE) {
94                 return;
95         }
96
97         BOOST_FOREACH (BitmapText const& i, text.bitmap) {
98                 if (!_bounding_box) {
99                         _bounding_box = i.rectangle;
100                 } else {
101                         _bounding_box->extend (i.rectangle);
102                 }
103         }
104
105         if (!text.string.empty()) {
106                 /* We can provide dummy values for time and frame rate here as they are only used to calculate fades */
107                 dcp::Size const frame = _film->frame_size();
108                 BOOST_FOREACH (PositionImage i, render_text(text.string, text.fonts, frame, dcpomatic::DCPTime(), 24)) {
109                         dcpomatic::Rect<double> rect (
110                                         double(i.position.x) / frame.width, double(i.position.y) / frame.height,
111                                         double(i.image->size().width) / frame.width, double(i.image->size().height) / frame.height
112                                         );
113                         if (!_bounding_box) {
114                                 _bounding_box = rect;
115                         } else {
116                                 _bounding_box->extend (rect);
117                         }
118                 }
119         }
120 }
121