Put caption details inside their own tag in the metadata.
[dcpomatic.git] / src / lib / caption_content.cc
1 /*
2     Copyright (C) 2013-2018 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 "caption_content.h"
22 #include "util.h"
23 #include "exceptions.h"
24 #include "font.h"
25 #include "content.h"
26 #include <dcp/raw_convert.h>
27 #include <libcxml/cxml.h>
28 #include <libxml++/libxml++.h>
29 #include <boost/foreach.hpp>
30 #include <iostream>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::vector;
36 using std::cout;
37 using std::list;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40 using boost::optional;
41 using dcp::raw_convert;
42
43 int const CaptionContentProperty::X_OFFSET = 500;
44 int const CaptionContentProperty::Y_OFFSET = 501;
45 int const CaptionContentProperty::X_SCALE = 502;
46 int const CaptionContentProperty::Y_SCALE = 503;
47 int const CaptionContentProperty::USE = 504;
48 int const CaptionContentProperty::BURN = 505;
49 int const CaptionContentProperty::LANGUAGE = 506;
50 int const CaptionContentProperty::FONTS = 507;
51 int const CaptionContentProperty::COLOUR = 508;
52 int const CaptionContentProperty::EFFECT = 509;
53 int const CaptionContentProperty::EFFECT_COLOUR = 510;
54 int const CaptionContentProperty::LINE_SPACING = 511;
55 int const CaptionContentProperty::FADE_IN = 512;
56 int const CaptionContentProperty::FADE_OUT = 513;
57 int const CaptionContentProperty::OUTLINE_WIDTH = 514;
58 int const CaptionContentProperty::TYPE = 515;
59
60 CaptionContent::CaptionContent (Content* parent)
61         : ContentPart (parent)
62         , _use (false)
63         , _burn (false)
64         , _x_offset (0)
65         , _y_offset (0)
66         , _x_scale (1)
67         , _y_scale (1)
68         , _line_spacing (1)
69         , _outline_width (2)
70         , _type (CAPTION_OPEN)
71 {
72
73 }
74
75 shared_ptr<CaptionContent>
76 CaptionContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
77 {
78         if (version < 34) {
79                 /* With old metadata FFmpeg content has the subtitle-related tags even with no
80                    subtitle streams, so check for that.
81                 */
82                 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
83                         return shared_ptr<CaptionContent> ();
84                 }
85
86                 /* Otherwise we can drop through to the newer logic */
87         }
88
89         if (version < 37) {
90                 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
91                         return shared_ptr<CaptionContent> ();
92                 }
93                 shared_ptr<CaptionContent> (new CaptionContent (parent, node, version));
94         }
95
96         if (!node->node_child("Caption")) {
97                 return shared_ptr<CaptionContent> ();
98         }
99
100         return shared_ptr<CaptionContent> (new CaptionContent (parent, node->node_child("Caption"), version));
101 }
102
103 CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int version)
104         : ContentPart (parent)
105         , _use (false)
106         , _burn (false)
107         , _x_offset (0)
108         , _y_offset (0)
109         , _x_scale (1)
110         , _y_scale (1)
111         , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
112         , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (2))
113         , _type (CAPTION_OPEN)
114 {
115         if (version >= 37) {
116                 _use = node->bool_child ("Use");
117                 _burn = node->bool_child ("Burn");
118         } else if (version >= 32) {
119                 _use = node->bool_child ("UseSubtitles");
120                 _burn = node->bool_child ("BurnSubtitles");
121         }
122
123         if (version >= 37) {
124                 _x_offset = node->number_child<double> ("XOffset");
125                 _y_offset = node->number_child<double> ("YOffset");
126         } else if (version >= 7) {
127                 _x_offset = node->number_child<double> ("SubtitleXOffset");
128                 _y_offset = node->number_child<double> ("SubtitleYOffset");
129         } else {
130                 _y_offset = node->number_child<double> ("SubtitleOffset");
131         }
132
133         if (node->optional_bool_child("Outline").get_value_or(false)) {
134                 _effect = dcp::BORDER;
135         } else if (node->optional_bool_child("Shadow").get_value_or(false)) {
136                 _effect = dcp::SHADOW;
137         } else {
138                 _effect = dcp::NONE;
139         }
140
141         optional<string> effect = node->optional_string_child("Effect");
142         if (effect) {
143                 if (*effect == "none") {
144                         _effect = dcp::NONE;
145                 } else if (*effect == "outline") {
146                         _effect = dcp::BORDER;
147                 } else if (*effect == "shadow") {
148                         _effect = dcp::SHADOW;
149                 }
150         }
151
152         if (version >= 37) {
153                 _x_scale = node->number_child<double> ("XScale");
154                 _y_scale = node->number_child<double> ("YScale");
155         } else if (version >= 10) {
156                 _x_scale = node->number_child<double> ("SubtitleXScale");
157                 _y_scale = node->number_child<double> ("SubtitleYScale");
158         } else {
159                 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
160         }
161
162         optional<int> r = node->optional_number_child<int>("Red");
163         optional<int> g = node->optional_number_child<int>("Green");
164         optional<int> b = node->optional_number_child<int>("Blue");
165         if (r && g && b) {
166                 _colour = dcp::Colour (*r, *g, *b);
167         }
168
169         if (version >= 36) {
170                 optional<int> er = node->optional_number_child<int>("EffectRed");
171                 optional<int> eg = node->optional_number_child<int>("EffectGreen");
172                 optional<int> eb = node->optional_number_child<int>("EffectBlue");
173                 if (er && eg && eb) {
174                         _effect_colour = dcp::Colour (*er, *eg, *eb);
175                 }
176         } else {
177                 _effect_colour = dcp::Colour (
178                         node->optional_number_child<int>("OutlineRed").get_value_or(255),
179                         node->optional_number_child<int>("OutlineGreen").get_value_or(255),
180                         node->optional_number_child<int>("OutlineBlue").get_value_or(255)
181                         );
182         }
183
184         optional<Frame> fi;
185         if (version >= 37) {
186                 fi = node->optional_number_child<Frame>("FadeIn");
187         } else {
188                 fi = node->optional_number_child<Frame>("SubtitleFadeIn");
189         }
190         if (fi) {
191                 _fade_in = ContentTime (*fi);
192         }
193
194         optional<Frame> fo;
195         if (version >= 37) {
196                 fo = node->optional_number_child<Frame>("FadeOut");
197         } else {
198                 fo = node->optional_number_child<Frame>("SubtitleFadeOut");
199         }
200         if (fo) {
201                 _fade_out = ContentTime (*fo);
202         }
203
204         if (version >= 37) {
205                 _language = node->optional_string_child ("Language").get_value_or ("");
206         } else {
207                 _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
208         }
209
210         list<cxml::NodePtr> fonts = node->node_children ("Font");
211         for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
212                 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
213         }
214
215         connect_to_fonts ();
216
217         _type = string_to_caption_type (node->optional_string_child("Type").get_value_or("open"));
218 }
219
220
221 /** _mutex must not be held on entry */
222 void
223 CaptionContent::as_xml (xmlpp::Node* root) const
224 {
225         boost::mutex::scoped_lock lm (_mutex);
226
227         xmlpp::Element* caption = root->add_child ("Caption");
228
229         caption->add_child("Use")->add_child_text (_use ? "1" : "0");
230         caption->add_child("Burn")->add_child_text (_burn ? "1" : "0");
231         caption->add_child("XOffset")->add_child_text (raw_convert<string> (_x_offset));
232         caption->add_child("YOffset")->add_child_text (raw_convert<string> (_y_offset));
233         caption->add_child("XScale")->add_child_text (raw_convert<string> (_x_scale));
234         caption->add_child("YScale")->add_child_text (raw_convert<string> (_y_scale));
235         caption->add_child("Language")->add_child_text (_language);
236         if (_colour) {
237                 caption->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
238                 caption->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
239                 caption->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
240         }
241         if (_effect) {
242                 switch (*_effect) {
243                 case dcp::NONE:
244                         caption->add_child("Effect")->add_child_text("none");
245                         break;
246                 case dcp::BORDER:
247                         caption->add_child("Effect")->add_child_text("outline");
248                         break;
249                 case dcp::SHADOW:
250                         caption->add_child("Effect")->add_child_text("shadow");
251                         break;
252                 }
253         }
254         if (_effect_colour) {
255                 caption->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
256                 caption->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
257                 caption->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
258         }
259         caption->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
260         if (_fade_in) {
261                 caption->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
262         }
263         if (_fade_out) {
264                 caption->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
265         }
266         caption->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
267
268         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
269                 (*i)->as_xml (caption->add_child("Font"));
270         }
271
272         caption->add_child("Type")->add_child_text (caption_type_to_string(_type));
273 }
274
275 string
276 CaptionContent::identifier () const
277 {
278         string s = raw_convert<string> (x_scale())
279                 + "_" + raw_convert<string> (y_scale())
280                 + "_" + raw_convert<string> (x_offset())
281                 + "_" + raw_convert<string> (y_offset())
282                 + "_" + raw_convert<string> (line_spacing())
283                 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
284                 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
285                 + "_" + raw_convert<string> (outline_width())
286                 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
287                 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::NONE)))
288                 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string());
289
290         /* XXX: I suppose really _fonts shouldn't be in here, since not all
291            types of subtitle content involve fonts.
292         */
293         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
294                 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
295                         s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
296                 }
297         }
298
299         /* The language is for metadata only, and doesn't affect
300            how this content looks.
301         */
302
303         return s;
304 }
305
306 void
307 CaptionContent::add_font (shared_ptr<Font> font)
308 {
309         _fonts.push_back (font);
310         connect_to_fonts ();
311 }
312
313 void
314 CaptionContent::connect_to_fonts ()
315 {
316         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
317                 i.disconnect ();
318         }
319
320         _font_connections.clear ();
321
322         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
323                 _font_connections.push_back (i->Changed.connect (boost::bind (&CaptionContent::font_changed, this)));
324         }
325 }
326
327 void
328 CaptionContent::font_changed ()
329 {
330         _parent->signal_changed (CaptionContentProperty::FONTS);
331 }
332
333 void
334 CaptionContent::set_colour (dcp::Colour colour)
335 {
336         maybe_set (_colour, colour, CaptionContentProperty::COLOUR);
337 }
338
339 void
340 CaptionContent::unset_colour ()
341 {
342         maybe_set (_colour, optional<dcp::Colour>(), CaptionContentProperty::COLOUR);
343 }
344
345 void
346 CaptionContent::set_effect (dcp::Effect e)
347 {
348         maybe_set (_effect, e, CaptionContentProperty::EFFECT);
349 }
350
351 void
352 CaptionContent::unset_effect ()
353 {
354         maybe_set (_effect, optional<dcp::Effect>(), CaptionContentProperty::EFFECT);
355 }
356
357 void
358 CaptionContent::set_effect_colour (dcp::Colour colour)
359 {
360         maybe_set (_effect_colour, colour, CaptionContentProperty::EFFECT_COLOUR);
361 }
362
363 void
364 CaptionContent::unset_effect_colour ()
365 {
366         maybe_set (_effect_colour, optional<dcp::Colour>(), CaptionContentProperty::EFFECT_COLOUR);
367 }
368
369 void
370 CaptionContent::set_use (bool u)
371 {
372         maybe_set (_use, u, CaptionContentProperty::USE);
373 }
374
375 void
376 CaptionContent::set_burn (bool b)
377 {
378         maybe_set (_burn, b, CaptionContentProperty::BURN);
379 }
380
381 void
382 CaptionContent::set_x_offset (double o)
383 {
384         maybe_set (_x_offset, o, CaptionContentProperty::X_OFFSET);
385 }
386
387 void
388 CaptionContent::set_y_offset (double o)
389 {
390         maybe_set (_y_offset, o, CaptionContentProperty::Y_OFFSET);
391 }
392
393 void
394 CaptionContent::set_x_scale (double s)
395 {
396         maybe_set (_x_scale, s, CaptionContentProperty::X_SCALE);
397 }
398
399 void
400 CaptionContent::set_y_scale (double s)
401 {
402         maybe_set (_y_scale, s, CaptionContentProperty::Y_SCALE);
403 }
404
405 void
406 CaptionContent::set_language (string language)
407 {
408         maybe_set (_language, language, CaptionContentProperty::LANGUAGE);
409 }
410
411 void
412 CaptionContent::set_line_spacing (double s)
413 {
414         maybe_set (_line_spacing, s, CaptionContentProperty::LINE_SPACING);
415 }
416
417 void
418 CaptionContent::set_fade_in (ContentTime t)
419 {
420         maybe_set (_fade_in, t, CaptionContentProperty::FADE_IN);
421 }
422
423 void
424 CaptionContent::unset_fade_in ()
425 {
426         maybe_set (_fade_in, optional<ContentTime>(), CaptionContentProperty::FADE_IN);
427 }
428
429 void
430 CaptionContent::set_fade_out (ContentTime t)
431 {
432         maybe_set (_fade_out, t, CaptionContentProperty::FADE_OUT);
433 }
434
435 void
436 CaptionContent::unset_fade_out ()
437 {
438         maybe_set (_fade_out, optional<ContentTime>(), CaptionContentProperty::FADE_OUT);
439 }
440
441 void
442 CaptionContent::set_type (CaptionType type)
443 {
444         maybe_set (_type, type, CaptionContentProperty::TYPE);
445 }
446
447 void
448 CaptionContent::set_outline_width (int w)
449 {
450         maybe_set (_outline_width, w, CaptionContentProperty::OUTLINE_WIDTH);
451 }
452
453 void
454 CaptionContent::take_settings_from (shared_ptr<const CaptionContent> c)
455 {
456         set_use (c->_use);
457         set_burn (c->_burn);
458         set_x_offset (c->_x_offset);
459         set_y_offset (c->_y_offset);
460         set_x_scale (c->_x_scale);
461         set_y_scale (c->_y_scale);
462         maybe_set (_fonts, c->_fonts, CaptionContentProperty::FONTS);
463         if (c->_colour) {
464                 set_colour (*c->_colour);
465         } else {
466                 unset_colour ();
467         }
468         if (c->_effect) {
469                 set_effect (*c->_effect);
470         }
471         if (c->_effect_colour) {
472                 set_effect_colour (*c->_effect_colour);
473         } else {
474                 unset_effect_colour ();
475         }
476         set_line_spacing (c->_line_spacing);
477         if (c->_fade_in) {
478                 set_fade_in (*c->_fade_in);
479         }
480         if (c->_fade_out) {
481                 set_fade_out (*c->_fade_out);
482         }
483         set_outline_width (c->_outline_width);
484 }