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