Missed update to private test repo version.
[dcpomatic.git] / src / lib / subtitle_encoder.cc
1 /*
2     Copyright (C) 2019-2021 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
22 #include "compose.hpp"
23 #include "film.h"
24 #include "job.h"
25 #include "player.h"
26 #include "subtitle_encoder.h"
27 #include <dcp/filesystem.h>
28 #include <dcp/interop_subtitle_asset.h>
29 #include <dcp/raw_convert.h>
30 #include <dcp/smpte_subtitle_asset.h>
31 #include <boost/filesystem.hpp>
32 #include <boost/bind/bind.hpp>
33
34 #include "i18n.h"
35
36
37 using std::make_pair;
38 using std::make_shared;
39 using std::pair;
40 using std::shared_ptr;
41 using std::string;
42 using std::vector;
43 using boost::optional;
44 #if BOOST_VERSION >= 106100
45 using namespace boost::placeholders;
46 #endif
47 using dcp::raw_convert;
48
49
50 /** @param output Directory, if there will be multiple output files, or a filename.
51  *  @param initial_name Hint that may be used to create filenames, if @ref output is a directory.
52  *  @param include_font true to refer to and export any font file (for Interop; ignored for SMPTE).
53  */
54 SubtitleEncoder::SubtitleEncoder (shared_ptr<const Film> film, shared_ptr<Job> job, boost::filesystem::path output, string initial_name, bool split_reels, bool include_font)
55         : Encoder (film, job)
56         , _split_reels (split_reels)
57         , _include_font (include_font)
58         , _reel_index (0)
59         , _length (film->length())
60 {
61         _player.set_play_referenced();
62         _player.set_ignore_video();
63         _player.set_ignore_audio();
64         _player.Text.connect(boost::bind(&SubtitleEncoder::text, this, _1, _2, _3, _4));
65
66         string const extension = film->interop() ? ".xml" : ".mxf";
67
68         int const files = split_reels ? film->reels().size() : 1;
69         for (int i = 0; i < files; ++i) {
70
71                 boost::filesystem::path filename = output;
72                 if (dcp::filesystem::is_directory(filename)) {
73                         if (files > 1) {
74                                 /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
75                                 /// which reel it is.  Preserve the %1; it will be replaced with the reel number.
76                                 filename /= String::compose("%1_reel%2", initial_name, i + 1);
77                         } else {
78                                 filename /= initial_name;
79                         }
80                 }
81
82                 _assets.push_back(make_pair(shared_ptr<dcp::SubtitleAsset>(), dcp::filesystem::change_extension(filename, extension)));
83         }
84
85         for (auto i: film->reels()) {
86                 _reels.push_back (i);
87         }
88
89         _default_font = dcp::ArrayData (default_font_file());
90 }
91
92
93 void
94 SubtitleEncoder::go ()
95 {
96         {
97                 shared_ptr<Job> job = _job.lock ();
98                 DCPOMATIC_ASSERT (job);
99                 job->sub (_("Extracting"));
100         }
101
102         _reel_index = 0;
103
104         while (!_player.pass()) {}
105
106         int reel = 0;
107         for (auto& i: _assets) {
108                 if (!i.first) {
109                         /* No subtitles arrived for this asset; make an empty one so we write something to the output */
110                         if (_film->interop()) {
111                                 auto s = make_shared<dcp::InteropSubtitleAsset>();
112                                 s->set_movie_title (_film->name());
113                                 s->set_reel_number (raw_convert<string>(reel + 1));
114                                 i.first = s;
115                         } else {
116                                 auto s = make_shared<dcp::SMPTESubtitleAsset>();
117                                 s->set_content_title_text (_film->name());
118                                 s->set_reel_number (reel + 1);
119                                 i.first = s;
120                         }
121                 }
122
123                 if (!_film->interop() || _include_font) {
124                         for (auto j: _player.get_subtitle_fonts()) {
125                                 i.first->add_font(j->id(), j->data().get_value_or(_default_font));
126                         }
127                 }
128
129                 i.first->write (i.second);
130                 ++reel;
131         }
132 }
133
134
135 void
136 SubtitleEncoder::text (PlayerText subs, TextType type, optional<DCPTextTrack> track, dcpomatic::DCPTimePeriod period)
137 {
138         if (type != TextType::OPEN_SUBTITLE) {
139                 return;
140         }
141
142         if (!_assets[_reel_index].first) {
143                 shared_ptr<dcp::SubtitleAsset> asset;
144                 auto lang = _film->subtitle_languages ();
145                 if (_film->interop ()) {
146                         auto s = make_shared<dcp::InteropSubtitleAsset>();
147                         s->set_movie_title (_film->name());
148                         if (lang.first) {
149                                 s->set_language (lang.first->to_string());
150                         }
151                         s->set_reel_number (raw_convert<string>(_reel_index + 1));
152                         _assets[_reel_index].first = s;
153                 } else {
154                         auto s = make_shared<dcp::SMPTESubtitleAsset>();
155                         s->set_content_title_text (_film->name());
156                         if (lang.first) {
157                                 s->set_language (*lang.first);
158                         } else if (track->language) {
159                                 s->set_language (track->language.get());
160                         }
161                         s->set_edit_rate (dcp::Fraction (_film->video_frame_rate(), 1));
162                         s->set_reel_number (_reel_index + 1);
163                         s->set_time_code_rate (_film->video_frame_rate());
164                         s->set_start_time (dcp::Time());
165                         if (_film->encrypted ()) {
166                                 s->set_key (_film->key ());
167                         }
168                         _assets[_reel_index].first = s;
169                 }
170         }
171
172         for (auto i: subs.string) {
173                 /* XXX: couldn't / shouldn't we use period here rather than getting time from the subtitle? */
174                 i.set_in  (i.in());
175                 i.set_out (i.out());
176                 if (_film->interop() && !_include_font) {
177                         i.unset_font ();
178                 }
179                 _assets[_reel_index].first->add (make_shared<dcp::SubtitleString>(i));
180         }
181
182         if (_split_reels && (_reel_index < int(_reels.size()) - 1) && period.from > _reels[_reel_index].from) {
183                 ++_reel_index;
184         }
185
186         _last = period.from;
187
188         auto job = _job.lock ();
189         if (job) {
190                 job->set_progress (float(period.from.get()) / _length.get());
191         }
192 }
193
194
195 Frame
196 SubtitleEncoder::frames_done () const
197 {
198         if (!_last) {
199                 return 0;
200         }
201
202         /* XXX: assuming 24fps here but I don't think it matters */
203         return _last->seconds() * 24;
204 }