Rename video/audio/subtitle part methods.
[dcpomatic.git] / src / lib / subtitle_content.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "subtitle_content.h"
21 #include "util.h"
22 #include "exceptions.h"
23 #include "safe_stringstream.h"
24 #include "font.h"
25 #include "raw_convert.h"
26 #include "content.h"
27 #include "film.h"
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
30 #include <boost/foreach.hpp>
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::vector;
37 using std::cout;
38 using std::list;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41
42 int const SubtitleContentProperty::X_OFFSET = 500;
43 int const SubtitleContentProperty::Y_OFFSET = 501;
44 int const SubtitleContentProperty::X_SCALE = 502;
45 int const SubtitleContentProperty::Y_SCALE = 503;
46 int const SubtitleContentProperty::USE = 504;
47 int const SubtitleContentProperty::BURN = 505;
48 int const SubtitleContentProperty::LANGUAGE = 506;
49 int const SubtitleContentProperty::FONTS = 507;
50 int const SubtitleContentProperty::VIDEO_FRAME_RATE = 508;
51 int const SubtitleContentProperty::COLOUR = 509;
52 int const SubtitleContentProperty::OUTLINE = 510;
53 int const SubtitleContentProperty::OUTLINE_COLOUR = 511;
54
55 SubtitleContent::SubtitleContent (Content* parent, shared_ptr<const Film> film)
56         : ContentPart (parent, film)
57         , _use (false)
58         , _burn (false)
59         , _x_offset (0)
60         , _y_offset (0)
61         , _x_scale (1)
62         , _y_scale (1)
63         , _colour (255, 255, 255)
64         , _outline (false)
65         , _outline_colour (0, 0, 0)
66 {
67
68 }
69
70 SubtitleContent::SubtitleContent (Content* parent, shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
71         : ContentPart (parent, film)
72         , _use (false)
73         , _burn (false)
74         , _x_offset (0)
75         , _y_offset (0)
76         , _x_scale (1)
77         , _y_scale (1)
78         , _colour (
79                 node->optional_number_child<int>("Red").get_value_or(255),
80                 node->optional_number_child<int>("Green").get_value_or(255),
81                 node->optional_number_child<int>("Blue").get_value_or(255)
82                 )
83         , _outline (node->optional_bool_child("Outline").get_value_or(false))
84         , _outline_colour (
85                 node->optional_number_child<int>("OutlineRed").get_value_or(255),
86                 node->optional_number_child<int>("OutlineGreen").get_value_or(255),
87                 node->optional_number_child<int>("OutlineBlue").get_value_or(255)
88                 )
89         , _frame_rate (node->optional_number_child<double>("SubtitleFrameRate"))
90 {
91         if (version >= 32) {
92                 _use = node->bool_child ("UseSubtitles");
93                 _burn = node->bool_child ("BurnSubtitles");
94         }
95
96         if (version >= 7) {
97                 _x_offset = node->number_child<double> ("SubtitleXOffset");
98                 _y_offset = node->number_child<double> ("SubtitleYOffset");
99         } else {
100                 _y_offset = node->number_child<double> ("SubtitleOffset");
101         }
102
103         if (version >= 10) {
104                 _x_scale = node->number_child<double> ("SubtitleXScale");
105                 _y_scale = node->number_child<double> ("SubtitleYScale");
106         } else {
107                 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
108         }
109
110         _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
111
112         list<cxml::NodePtr> fonts = node->node_children ("Font");
113         for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
114                 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
115         }
116
117         connect_to_fonts ();
118 }
119
120 SubtitleContent::SubtitleContent (Content* parent, shared_ptr<const Film> film, vector<shared_ptr<Content> > c)
121         : ContentPart (parent, film)
122 {
123         shared_ptr<SubtitleContent> ref = c[0]->subtitle;
124         DCPOMATIC_ASSERT (ref);
125         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
126
127         for (size_t i = 1; i < c.size(); ++i) {
128
129                 if (c[i]->subtitle->use() != ref->use()) {
130                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
131                 }
132
133                 if (c[i]->subtitle->burn() != ref->burn()) {
134                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
135                 }
136
137                 if (c[i]->subtitle->x_offset() != ref->x_offset()) {
138                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
139                 }
140
141                 if (c[i]->subtitle->y_offset() != ref->y_offset()) {
142                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
143                 }
144
145                 if (c[i]->subtitle->x_scale() != ref->x_scale()) {
146                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
147                 }
148
149                 if (c[i]->subtitle->y_scale() != ref->y_scale()) {
150                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
151                 }
152
153                 list<shared_ptr<Font> > fonts = c[i]->subtitle->fonts ();
154                 if (fonts.size() != ref_fonts.size()) {
155                         throw JoinError (_("Content to be joined must use the same fonts."));
156                 }
157
158                 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
159                 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
160
161                 while (j != ref_fonts.end ()) {
162                         if (**j != **k) {
163                                 throw JoinError (_("Content to be joined must use the same fonts."));
164                         }
165                         ++j;
166                         ++k;
167                 }
168         }
169
170         _use = ref->use ();
171         _burn = ref->burn ();
172         _x_offset = ref->x_offset ();
173         _y_offset = ref->y_offset ();
174         _x_scale = ref->x_scale ();
175         _y_scale = ref->y_scale ();
176         _language = ref->language ();
177         _fonts = ref_fonts;
178
179         connect_to_fonts ();
180 }
181
182 /** _mutex must not be held on entry */
183 void
184 SubtitleContent::as_xml (xmlpp::Node* root) const
185 {
186         boost::mutex::scoped_lock lm (_mutex);
187
188         root->add_child("UseSubtitles")->add_child_text (raw_convert<string> (_use));
189         root->add_child("BurnSubtitles")->add_child_text (raw_convert<string> (_burn));
190         root->add_child("SubtitleXOffset")->add_child_text (raw_convert<string> (_x_offset));
191         root->add_child("SubtitleYOffset")->add_child_text (raw_convert<string> (_y_offset));
192         root->add_child("SubtitleXScale")->add_child_text (raw_convert<string> (_x_scale));
193         root->add_child("SubtitleYScale")->add_child_text (raw_convert<string> (_y_scale));
194         root->add_child("SubtitleLanguage")->add_child_text (_language);
195         root->add_child("Red")->add_child_text (raw_convert<string> (_colour.r));
196         root->add_child("Green")->add_child_text (raw_convert<string> (_colour.g));
197         root->add_child("Blue")->add_child_text (raw_convert<string> (_colour.b));
198         root->add_child("Outline")->add_child_text (raw_convert<string> (_outline));
199         root->add_child("OutlineRed")->add_child_text (raw_convert<string> (_outline_colour.r));
200         root->add_child("OutlineGreen")->add_child_text (raw_convert<string> (_outline_colour.g));
201         root->add_child("OutlineBlue")->add_child_text (raw_convert<string> (_outline_colour.b));
202
203         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
204                 (*i)->as_xml (root->add_child("Font"));
205         }
206 }
207
208 string
209 SubtitleContent::identifier () const
210 {
211         SafeStringStream s;
212         s << raw_convert<string> (x_scale())
213           << "_" << raw_convert<string> (y_scale())
214           << "_" << raw_convert<string> (x_offset())
215           << "_" << raw_convert<string> (y_offset());
216
217         /* XXX: I suppose really _fonts shouldn't be in here, since not all
218            types of subtitle content involve fonts.
219         */
220         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
221                 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
222                         s << "_" << f->file(static_cast<FontFiles::Variant>(i)).get_value_or ("Default");
223                 }
224         }
225
226         /* The language is for metadata only, and doesn't affect
227            how this content looks.
228         */
229
230         return s.str ();
231 }
232
233 void
234 SubtitleContent::add_font (shared_ptr<Font> font)
235 {
236         _fonts.push_back (font);
237         connect_to_fonts ();
238 }
239
240 void
241 SubtitleContent::connect_to_fonts ()
242 {
243         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
244                 i.disconnect ();
245         }
246
247         _font_connections.clear ();
248
249         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
250                 _font_connections.push_back (i->Changed.connect (boost::bind (&SubtitleContent::font_changed, this)));
251         }
252 }
253
254 void
255 SubtitleContent::font_changed ()
256 {
257         _parent->signal_changed (SubtitleContentProperty::FONTS);
258 }
259
260 void
261 SubtitleContent::set_colour (dcp::Colour colour)
262 {
263         maybe_set (_colour, colour, SubtitleContentProperty::COLOUR);
264 }
265
266 void
267 SubtitleContent::set_outline (bool o)
268 {
269         maybe_set (_outline, o, SubtitleContentProperty::OUTLINE);
270 }
271
272 void
273 SubtitleContent::set_outline_colour (dcp::Colour colour)
274 {
275         maybe_set (_outline_colour, colour, SubtitleContentProperty::OUTLINE_COLOUR);
276 }
277
278 void
279 SubtitleContent::set_use (bool u)
280 {
281         maybe_set (_use, u, SubtitleContentProperty::USE);
282 }
283
284 void
285 SubtitleContent::set_burn (bool b)
286 {
287         maybe_set (_burn, b, SubtitleContentProperty::BURN);
288 }
289
290 void
291 SubtitleContent::set_x_offset (double o)
292 {
293         maybe_set (_x_offset, o, SubtitleContentProperty::X_OFFSET);
294 }
295
296 void
297 SubtitleContent::set_y_offset (double o)
298 {
299         maybe_set (_y_offset, o, SubtitleContentProperty::Y_OFFSET);
300 }
301
302 void
303 SubtitleContent::set_x_scale (double s)
304 {
305         maybe_set (_x_scale, s, SubtitleContentProperty::X_SCALE);
306 }
307
308 void
309 SubtitleContent::set_y_scale (double s)
310 {
311         maybe_set (_y_scale, s, SubtitleContentProperty::Y_SCALE);
312 }
313
314 void
315 SubtitleContent::set_language (string language)
316 {
317         maybe_set (_language, language, SubtitleContentProperty::LANGUAGE);
318 }
319
320 void
321 SubtitleContent::set_video_frame_rate (double r)
322 {
323         maybe_set (_frame_rate, r, SubtitleContentProperty::VIDEO_FRAME_RATE);
324 }
325
326 double
327 SubtitleContent::video_frame_rate () const
328 {
329         {
330                 boost::mutex::scoped_lock lm (_mutex);
331                 if (_frame_rate) {
332                         return _frame_rate.get ();
333                 }
334         }
335
336         /* No frame rate specified, so assume this content has been
337            prepared for any concurrent video content.
338         */
339         shared_ptr<const Film> film = _film.lock ();
340         DCPOMATIC_ASSERT (film);
341         return film->active_frame_rate_change(_parent->position()).source;
342 }