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