Get ccaps by asking the Player, rather than by listening to its emissions,
[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, CaptionType original_type)
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 (original_type)
71         , _original_type (original_type)
72 {
73
74 }
75
76 /** @return CaptionContents from node or <Caption> nodes under node (according to version).
77  *  The list could be empty if no CaptionContents are found.
78  */
79 list<shared_ptr<CaptionContent> >
80 CaptionContent::from_xml (Content* parent, cxml::ConstNodePtr node, int version)
81 {
82         if (version < 34) {
83                 /* With old metadata FFmpeg content has the subtitle-related tags even with no
84                    subtitle streams, so check for that.
85                 */
86                 if (node->string_child("Type") == "FFmpeg" && node->node_children("SubtitleStream").empty()) {
87                         return list<shared_ptr<CaptionContent> >();
88                 }
89
90                 /* Otherwise we can drop through to the newer logic */
91         }
92
93         if (version < 37) {
94                 if (!node->optional_number_child<double>("SubtitleXOffset") && !node->optional_number_child<double>("SubtitleOffset")) {
95                         return list<shared_ptr<CaptionContent> >();
96                 }
97                 list<shared_ptr<CaptionContent> > c;
98                 c.push_back (shared_ptr<CaptionContent> (new CaptionContent (parent, node, version)));
99                 return c;
100         }
101
102         if (!node->optional_node_child("Caption")) {
103                 return list<shared_ptr<CaptionContent> >();
104         }
105
106         list<shared_ptr<CaptionContent> > c;
107         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("Caption")) {
108                 c.push_back (shared_ptr<CaptionContent> (new CaptionContent (parent, i, version)));
109         }
110         return c;
111 }
112
113 CaptionContent::CaptionContent (Content* parent, cxml::ConstNodePtr node, int version)
114         : ContentPart (parent)
115         , _use (false)
116         , _burn (false)
117         , _x_offset (0)
118         , _y_offset (0)
119         , _x_scale (1)
120         , _y_scale (1)
121         , _line_spacing (node->optional_number_child<double>("LineSpacing").get_value_or (1))
122         , _outline_width (node->optional_number_child<int>("OutlineWidth").get_value_or (2))
123         , _type (CAPTION_OPEN)
124 {
125         if (version >= 37) {
126                 _use = node->bool_child ("Use");
127                 _burn = node->bool_child ("Burn");
128         } else if (version >= 32) {
129                 _use = node->bool_child ("UseSubtitles");
130                 _burn = node->bool_child ("BurnSubtitles");
131         }
132
133         if (version >= 37) {
134                 _x_offset = node->number_child<double> ("XOffset");
135                 _y_offset = node->number_child<double> ("YOffset");
136         } else if (version >= 7) {
137                 _x_offset = node->number_child<double> ("SubtitleXOffset");
138                 _y_offset = node->number_child<double> ("SubtitleYOffset");
139         } else {
140                 _y_offset = node->number_child<double> ("SubtitleOffset");
141         }
142
143         if (node->optional_bool_child("Outline").get_value_or(false)) {
144                 _effect = dcp::BORDER;
145         } else if (node->optional_bool_child("Shadow").get_value_or(false)) {
146                 _effect = dcp::SHADOW;
147         } else {
148                 _effect = dcp::NONE;
149         }
150
151         optional<string> effect = node->optional_string_child("Effect");
152         if (effect) {
153                 if (*effect == "none") {
154                         _effect = dcp::NONE;
155                 } else if (*effect == "outline") {
156                         _effect = dcp::BORDER;
157                 } else if (*effect == "shadow") {
158                         _effect = dcp::SHADOW;
159                 }
160         }
161
162         if (version >= 37) {
163                 _x_scale = node->number_child<double> ("XScale");
164                 _y_scale = node->number_child<double> ("YScale");
165         } else if (version >= 10) {
166                 _x_scale = node->number_child<double> ("SubtitleXScale");
167                 _y_scale = node->number_child<double> ("SubtitleYScale");
168         } else {
169                 _x_scale = _y_scale = node->number_child<double> ("SubtitleScale");
170         }
171
172         optional<int> r = node->optional_number_child<int>("Red");
173         optional<int> g = node->optional_number_child<int>("Green");
174         optional<int> b = node->optional_number_child<int>("Blue");
175         if (r && g && b) {
176                 _colour = dcp::Colour (*r, *g, *b);
177         }
178
179         if (version >= 36) {
180                 optional<int> er = node->optional_number_child<int>("EffectRed");
181                 optional<int> eg = node->optional_number_child<int>("EffectGreen");
182                 optional<int> eb = node->optional_number_child<int>("EffectBlue");
183                 if (er && eg && eb) {
184                         _effect_colour = dcp::Colour (*er, *eg, *eb);
185                 }
186         } else {
187                 _effect_colour = dcp::Colour (
188                         node->optional_number_child<int>("OutlineRed").get_value_or(255),
189                         node->optional_number_child<int>("OutlineGreen").get_value_or(255),
190                         node->optional_number_child<int>("OutlineBlue").get_value_or(255)
191                         );
192         }
193
194         optional<Frame> fi;
195         if (version >= 37) {
196                 fi = node->optional_number_child<Frame>("FadeIn");
197         } else {
198                 fi = node->optional_number_child<Frame>("SubtitleFadeIn");
199         }
200         if (fi) {
201                 _fade_in = ContentTime (*fi);
202         }
203
204         optional<Frame> fo;
205         if (version >= 37) {
206                 fo = node->optional_number_child<Frame>("FadeOut");
207         } else {
208                 fo = node->optional_number_child<Frame>("SubtitleFadeOut");
209         }
210         if (fo) {
211                 _fade_out = ContentTime (*fo);
212         }
213
214         if (version >= 37) {
215                 _language = node->optional_string_child ("Language").get_value_or ("");
216         } else {
217                 _language = node->optional_string_child ("SubtitleLanguage").get_value_or ("");
218         }
219
220         list<cxml::NodePtr> fonts = node->node_children ("Font");
221         for (list<cxml::NodePtr>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
222                 _fonts.push_back (shared_ptr<Font> (new Font (*i)));
223         }
224
225         connect_to_fonts ();
226
227         _type = string_to_caption_type (node->optional_string_child("Type").get_value_or("open"));
228         _original_type = string_to_caption_type (node->optional_string_child("Type").get_value_or("open"));
229 }
230
231 CaptionContent::CaptionContent (Content* parent, vector<shared_ptr<Content> > c)
232         : ContentPart (parent)
233 {
234         /* This constructor is for join which is only supported for content types
235            that have a single caption, so we can use only_caption() here.
236         */
237         shared_ptr<CaptionContent> ref = c[0]->only_caption();
238         DCPOMATIC_ASSERT (ref);
239         list<shared_ptr<Font> > ref_fonts = ref->fonts ();
240
241         for (size_t i = 1; i < c.size(); ++i) {
242
243                 if (c[i]->only_caption()->use() != ref->use()) {
244                         throw JoinError (_("Content to be joined must have the same 'use subtitles' setting."));
245                 }
246
247                 if (c[i]->only_caption()->burn() != ref->burn()) {
248                         throw JoinError (_("Content to be joined must have the same 'burn subtitles' setting."));
249                 }
250
251                 if (c[i]->only_caption()->x_offset() != ref->x_offset()) {
252                         throw JoinError (_("Content to be joined must have the same subtitle X offset."));
253                 }
254
255                 if (c[i]->only_caption()->y_offset() != ref->y_offset()) {
256                         throw JoinError (_("Content to be joined must have the same subtitle Y offset."));
257                 }
258
259                 if (c[i]->only_caption()->x_scale() != ref->x_scale()) {
260                         throw JoinError (_("Content to be joined must have the same subtitle X scale."));
261                 }
262
263                 if (c[i]->only_caption()->y_scale() != ref->y_scale()) {
264                         throw JoinError (_("Content to be joined must have the same subtitle Y scale."));
265                 }
266
267                 if (c[i]->only_caption()->line_spacing() != ref->line_spacing()) {
268                         throw JoinError (_("Content to be joined must have the same subtitle line spacing."));
269                 }
270
271                 if ((c[i]->only_caption()->fade_in() != ref->fade_in()) || (c[i]->only_caption()->fade_out() != ref->fade_out())) {
272                         throw JoinError (_("Content to be joined must have the same subtitle fades."));
273                 }
274
275                 if ((c[i]->only_caption()->outline_width() != ref->outline_width())) {
276                         throw JoinError (_("Content to be joined must have the same outline width."));
277                 }
278
279                 list<shared_ptr<Font> > fonts = c[i]->only_caption()->fonts ();
280                 if (fonts.size() != ref_fonts.size()) {
281                         throw JoinError (_("Content to be joined must use the same fonts."));
282                 }
283
284                 list<shared_ptr<Font> >::const_iterator j = ref_fonts.begin ();
285                 list<shared_ptr<Font> >::const_iterator k = fonts.begin ();
286
287                 while (j != ref_fonts.end ()) {
288                         if (**j != **k) {
289                                 throw JoinError (_("Content to be joined must use the same fonts."));
290                         }
291                         ++j;
292                         ++k;
293                 }
294         }
295
296         _use = ref->use ();
297         _burn = ref->burn ();
298         _x_offset = ref->x_offset ();
299         _y_offset = ref->y_offset ();
300         _x_scale = ref->x_scale ();
301         _y_scale = ref->y_scale ();
302         _language = ref->language ();
303         _fonts = ref_fonts;
304         _line_spacing = ref->line_spacing ();
305         _fade_in = ref->fade_in ();
306         _fade_out = ref->fade_out ();
307         _outline_width = ref->outline_width ();
308
309         connect_to_fonts ();
310 }
311
312 /** _mutex must not be held on entry */
313 void
314 CaptionContent::as_xml (xmlpp::Node* root) const
315 {
316         boost::mutex::scoped_lock lm (_mutex);
317
318         xmlpp::Element* caption = root->add_child ("Caption");
319
320         caption->add_child("Use")->add_child_text (_use ? "1" : "0");
321         caption->add_child("Burn")->add_child_text (_burn ? "1" : "0");
322         caption->add_child("XOffset")->add_child_text (raw_convert<string> (_x_offset));
323         caption->add_child("YOffset")->add_child_text (raw_convert<string> (_y_offset));
324         caption->add_child("XScale")->add_child_text (raw_convert<string> (_x_scale));
325         caption->add_child("YScale")->add_child_text (raw_convert<string> (_y_scale));
326         caption->add_child("Language")->add_child_text (_language);
327         if (_colour) {
328                 caption->add_child("Red")->add_child_text (raw_convert<string> (_colour->r));
329                 caption->add_child("Green")->add_child_text (raw_convert<string> (_colour->g));
330                 caption->add_child("Blue")->add_child_text (raw_convert<string> (_colour->b));
331         }
332         if (_effect) {
333                 switch (*_effect) {
334                 case dcp::NONE:
335                         caption->add_child("Effect")->add_child_text("none");
336                         break;
337                 case dcp::BORDER:
338                         caption->add_child("Effect")->add_child_text("outline");
339                         break;
340                 case dcp::SHADOW:
341                         caption->add_child("Effect")->add_child_text("shadow");
342                         break;
343                 }
344         }
345         if (_effect_colour) {
346                 caption->add_child("EffectRed")->add_child_text (raw_convert<string> (_effect_colour->r));
347                 caption->add_child("EffectGreen")->add_child_text (raw_convert<string> (_effect_colour->g));
348                 caption->add_child("EffectBlue")->add_child_text (raw_convert<string> (_effect_colour->b));
349         }
350         caption->add_child("LineSpacing")->add_child_text (raw_convert<string> (_line_spacing));
351         if (_fade_in) {
352                 caption->add_child("FadeIn")->add_child_text (raw_convert<string> (_fade_in->get()));
353         }
354         if (_fade_out) {
355                 caption->add_child("FadeOut")->add_child_text (raw_convert<string> (_fade_out->get()));
356         }
357         caption->add_child("OutlineWidth")->add_child_text (raw_convert<string> (_outline_width));
358
359         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
360                 (*i)->as_xml (caption->add_child("Font"));
361         }
362
363         caption->add_child("Type")->add_child_text (caption_type_to_string(_type));
364         caption->add_child("OriginalType")->add_child_text (caption_type_to_string(_original_type));
365 }
366
367 string
368 CaptionContent::identifier () const
369 {
370         string s = raw_convert<string> (x_scale())
371                 + "_" + raw_convert<string> (y_scale())
372                 + "_" + raw_convert<string> (x_offset())
373                 + "_" + raw_convert<string> (y_offset())
374                 + "_" + raw_convert<string> (line_spacing())
375                 + "_" + raw_convert<string> (fade_in().get_value_or(ContentTime()).get())
376                 + "_" + raw_convert<string> (fade_out().get_value_or(ContentTime()).get())
377                 + "_" + raw_convert<string> (outline_width())
378                 + "_" + raw_convert<string> (colour().get_value_or(dcp::Colour(255, 255, 255)).to_argb_string())
379                 + "_" + raw_convert<string> (dcp::effect_to_string(effect().get_value_or(dcp::NONE)))
380                 + "_" + raw_convert<string> (effect_colour().get_value_or(dcp::Colour(0, 0, 0)).to_argb_string());
381
382         /* XXX: I suppose really _fonts shouldn't be in here, since not all
383            types of subtitle content involve fonts.
384         */
385         BOOST_FOREACH (shared_ptr<Font> f, _fonts) {
386                 for (int i = 0; i < FontFiles::VARIANTS; ++i) {
387                         s += "_" + f->file(static_cast<FontFiles::Variant>(i)).get_value_or("Default").string();
388                 }
389         }
390
391         /* The language is for metadata only, and doesn't affect
392            how this content looks.
393         */
394
395         return s;
396 }
397
398 void
399 CaptionContent::add_font (shared_ptr<Font> font)
400 {
401         _fonts.push_back (font);
402         connect_to_fonts ();
403 }
404
405 void
406 CaptionContent::connect_to_fonts ()
407 {
408         BOOST_FOREACH (boost::signals2::connection& i, _font_connections) {
409                 i.disconnect ();
410         }
411
412         _font_connections.clear ();
413
414         BOOST_FOREACH (shared_ptr<Font> i, _fonts) {
415                 _font_connections.push_back (i->Changed.connect (boost::bind (&CaptionContent::font_changed, this)));
416         }
417 }
418
419 void
420 CaptionContent::font_changed ()
421 {
422         _parent->signal_changed (CaptionContentProperty::FONTS);
423 }
424
425 void
426 CaptionContent::set_colour (dcp::Colour colour)
427 {
428         maybe_set (_colour, colour, CaptionContentProperty::COLOUR);
429 }
430
431 void
432 CaptionContent::unset_colour ()
433 {
434         maybe_set (_colour, optional<dcp::Colour>(), CaptionContentProperty::COLOUR);
435 }
436
437 void
438 CaptionContent::set_effect (dcp::Effect e)
439 {
440         maybe_set (_effect, e, CaptionContentProperty::EFFECT);
441 }
442
443 void
444 CaptionContent::unset_effect ()
445 {
446         maybe_set (_effect, optional<dcp::Effect>(), CaptionContentProperty::EFFECT);
447 }
448
449 void
450 CaptionContent::set_effect_colour (dcp::Colour colour)
451 {
452         maybe_set (_effect_colour, colour, CaptionContentProperty::EFFECT_COLOUR);
453 }
454
455 void
456 CaptionContent::unset_effect_colour ()
457 {
458         maybe_set (_effect_colour, optional<dcp::Colour>(), CaptionContentProperty::EFFECT_COLOUR);
459 }
460
461 void
462 CaptionContent::set_use (bool u)
463 {
464         maybe_set (_use, u, CaptionContentProperty::USE);
465 }
466
467 void
468 CaptionContent::set_burn (bool b)
469 {
470         maybe_set (_burn, b, CaptionContentProperty::BURN);
471 }
472
473 void
474 CaptionContent::set_x_offset (double o)
475 {
476         maybe_set (_x_offset, o, CaptionContentProperty::X_OFFSET);
477 }
478
479 void
480 CaptionContent::set_y_offset (double o)
481 {
482         maybe_set (_y_offset, o, CaptionContentProperty::Y_OFFSET);
483 }
484
485 void
486 CaptionContent::set_x_scale (double s)
487 {
488         maybe_set (_x_scale, s, CaptionContentProperty::X_SCALE);
489 }
490
491 void
492 CaptionContent::set_y_scale (double s)
493 {
494         maybe_set (_y_scale, s, CaptionContentProperty::Y_SCALE);
495 }
496
497 void
498 CaptionContent::set_language (string language)
499 {
500         maybe_set (_language, language, CaptionContentProperty::LANGUAGE);
501 }
502
503 void
504 CaptionContent::set_line_spacing (double s)
505 {
506         maybe_set (_line_spacing, s, CaptionContentProperty::LINE_SPACING);
507 }
508
509 void
510 CaptionContent::set_fade_in (ContentTime t)
511 {
512         maybe_set (_fade_in, t, CaptionContentProperty::FADE_IN);
513 }
514
515 void
516 CaptionContent::unset_fade_in ()
517 {
518         maybe_set (_fade_in, optional<ContentTime>(), CaptionContentProperty::FADE_IN);
519 }
520
521 void
522 CaptionContent::set_fade_out (ContentTime t)
523 {
524         maybe_set (_fade_out, t, CaptionContentProperty::FADE_OUT);
525 }
526
527 void
528 CaptionContent::unset_fade_out ()
529 {
530         maybe_set (_fade_out, optional<ContentTime>(), CaptionContentProperty::FADE_OUT);
531 }
532
533 void
534 CaptionContent::set_type (CaptionType type)
535 {
536         maybe_set (_type, type, CaptionContentProperty::TYPE);
537 }
538
539 void
540 CaptionContent::set_outline_width (int w)
541 {
542         maybe_set (_outline_width, w, CaptionContentProperty::OUTLINE_WIDTH);
543 }
544
545 void
546 CaptionContent::take_settings_from (shared_ptr<const CaptionContent> c)
547 {
548         set_use (c->_use);
549         set_burn (c->_burn);
550         set_x_offset (c->_x_offset);
551         set_y_offset (c->_y_offset);
552         set_x_scale (c->_x_scale);
553         set_y_scale (c->_y_scale);
554         maybe_set (_fonts, c->_fonts, CaptionContentProperty::FONTS);
555         if (c->_colour) {
556                 set_colour (*c->_colour);
557         } else {
558                 unset_colour ();
559         }
560         if (c->_effect) {
561                 set_effect (*c->_effect);
562         }
563         if (c->_effect_colour) {
564                 set_effect_colour (*c->_effect_colour);
565         } else {
566                 unset_effect_colour ();
567         }
568         set_line_spacing (c->_line_spacing);
569         if (c->_fade_in) {
570                 set_fade_in (*c->_fade_in);
571         }
572         if (c->_fade_out) {
573                 set_fade_out (*c->_fade_out);
574         }
575         set_outline_width (c->_outline_width);
576 }