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