2 Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp 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.
11 libdcp 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.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
35 #include "smpte_load_font_node.h"
36 #include "smpte_subtitle_asset.h"
37 #include "stream_operators.h"
38 #include "subtitle_image.h"
41 #include <boost/optional/optional_io.hpp>
42 #include <boost/test/unit_test.hpp>
45 using std::make_shared;
47 using std::shared_ptr;
48 using std::dynamic_pointer_cast;
50 using boost::optional;
53 BOOST_AUTO_TEST_CASE (smpte_subtitle_id_test)
55 dcp::SMPTESubtitleAsset subs;
57 make_shared<dcp::SubtitleString>(
63 dcp::Time(0, 1, 2, 3, 24),
64 dcp::Time(0, 2, 2, 3, 24),
73 dcp::Time(0, 0, 0, 0, 24),
74 dcp::Time(0, 0, 0, 0, 24),
78 subs.write("build/test/smpte_subtitle_id_test.mxf");
80 dcp::SMPTESubtitleAsset check("build/test/smpte_subtitle_id_test.mxf");
81 BOOST_CHECK(check.id() != check.xml_id());
85 /** Check reading of a SMPTE subtitle file */
86 BOOST_AUTO_TEST_CASE (read_smpte_subtitle_test)
88 dcp::SMPTESubtitleAsset sc (
91 "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV" /
92 "8b48f6ae-c74b-4b80-b994-a8236bbbad74_sub.mxf"
95 BOOST_CHECK_EQUAL (sc.id(), "8b48f6ae-c74b-4b80-b994-a8236bbbad74");
96 BOOST_CHECK_EQUAL (sc.content_title_text(), "Journey to Jah");
97 BOOST_REQUIRE (sc.annotation_text());
98 BOOST_CHECK_EQUAL (sc.annotation_text().get(), "Journey to Jah");
99 BOOST_CHECK_EQUAL (sc.issue_date(), dcp::LocalTime ("2014-02-25T11:22:48.000-00:00"));
100 BOOST_REQUIRE (sc.reel_number());
101 BOOST_CHECK_EQUAL (sc.reel_number().get(), 1);
102 BOOST_REQUIRE (sc.language ());
103 BOOST_CHECK_EQUAL (sc.language().get (), "de");
104 BOOST_CHECK_EQUAL (sc.edit_rate(), dcp::Fraction (25, 1));
105 BOOST_CHECK_EQUAL (sc.time_code_rate(), 25);
106 BOOST_CHECK_EQUAL (sc.start_time(), dcp::Time (0, 0, 0, 0, 25));
107 auto lfn = sc.load_font_nodes ();
108 BOOST_REQUIRE_EQUAL (lfn.size(), 1);
109 shared_ptr<dcp::SMPTELoadFontNode> smpte_lfn = dynamic_pointer_cast<dcp::SMPTELoadFontNode> (lfn.front ());
110 BOOST_REQUIRE (smpte_lfn);
111 BOOST_CHECK_EQUAL (smpte_lfn->id, "theFontId");
112 BOOST_CHECK_EQUAL (smpte_lfn->urn, "9118bbce-4105-4a05-b37c-a5a6f75e1fea");
113 BOOST_REQUIRE_EQUAL (sc.subtitles().size(), 63);
114 BOOST_REQUIRE (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().front()));
115 BOOST_CHECK_EQUAL (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().front())->text(), "Noch mal.");
116 BOOST_CHECK_EQUAL (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().front())->space_before(), 0.0f);
117 BOOST_CHECK_EQUAL (sc.subtitles().front()->in(), dcp::Time (0, 0, 25, 12, 25));
118 BOOST_CHECK_EQUAL (sc.subtitles().front()->out(), dcp::Time (0, 0, 26, 4, 25));
119 BOOST_REQUIRE (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().back()));
120 BOOST_CHECK_EQUAL (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().back())->text(), "Prochainement");
121 BOOST_CHECK_EQUAL (dynamic_pointer_cast<const dcp::SubtitleString>(sc.subtitles().back())->space_before(), 0.0f);
122 BOOST_CHECK_EQUAL (sc.subtitles().back()->in(), dcp::Time (0, 1, 57, 17, 25));
123 BOOST_CHECK_EQUAL (sc.subtitles().back()->out(), dcp::Time (0, 1, 58, 12, 25));
127 /** And another one featuring <Font> within <Text> and some <Space> */
128 BOOST_AUTO_TEST_CASE (read_smpte_subtitle_test2)
130 dcp::SMPTESubtitleAsset sc (private_test / "olsson.xml");
132 auto subs = sc.subtitles();
133 BOOST_REQUIRE_EQUAL (subs.size(), 6);
135 auto is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
137 BOOST_CHECK_EQUAL (is->text(), "Testing is ");
138 BOOST_CHECK (!is->italic());
139 BOOST_CHECK_CLOSE (is->space_before(), 0, 0.1);
141 is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
143 BOOST_CHECK_EQUAL (is->text(), "really");
144 BOOST_CHECK (is->italic());
145 BOOST_CHECK_CLOSE (is->space_before(), 0, 0.1);
147 is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
149 BOOST_CHECK_EQUAL (is->text(), " fun!");
150 BOOST_CHECK (!is->italic());
151 BOOST_CHECK_CLOSE (is->space_before(), 5, 0.1);
153 is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
155 BOOST_CHECK_EQUAL (is->text(), "This is the ");
156 BOOST_CHECK (!is->italic());
157 BOOST_CHECK_CLOSE (is->space_before(), 0, 0.1);
159 is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
161 BOOST_CHECK_EQUAL (is->text(), "second");
162 BOOST_CHECK (is->italic());
163 BOOST_CHECK_CLOSE (is->space_before(), 0, 0.1);
165 is = dynamic_pointer_cast<const dcp::SubtitleString>(subs[i]);
167 BOOST_CHECK_EQUAL (is->text(), " line!");
168 BOOST_CHECK (!is->italic());
169 BOOST_CHECK_CLOSE (is->space_before(), 0, 0.1);
173 /* Write some subtitle content as SMPTE XML and check that it is right */
174 BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test)
176 dcp::SMPTESubtitleAsset c;
177 c.set_reel_number (1);
178 c.set_language (dcp::LanguageTag("en"));
179 c.set_content_title_text ("Test");
180 c.set_issue_date (dcp::LocalTime ("2016-04-01T03:52:00+00:00"));
183 make_shared<dcp::SubtitleString> (
188 dcp::Colour (255, 255, 255),
191 dcp::Time (0, 4, 9, 22, 24),
192 dcp::Time (0, 4, 11, 22, 24),
200 dcp::Colour (0, 0, 0),
201 dcp::Time (0, 0, 0, 0, 24),
202 dcp::Time (0, 0, 0, 0, 24),
208 make_shared<dcp::SubtitleString>(
209 boost::optional<string> (),
213 dcp::Colour (128, 0, 64),
216 dcp::Time (5, 41, 0, 21, 24),
217 dcp::Time (6, 12, 15, 21, 24),
225 dcp::Colour (1, 2, 3),
226 dcp::Time (1, 2, 3, 4, 24),
227 dcp::Time (5, 6, 7, 8, 24),
233 make_shared<dcp::SubtitleString>(
234 boost::optional<string> (),
238 dcp::Colour (128, 0, 64),
241 dcp::Time (5, 41, 0, 21, 24),
242 dcp::Time (6, 12, 15, 21, 24),
250 dcp::Colour (1, 2, 3),
251 dcp::Time (1, 2, 3, 4, 24),
252 dcp::Time (5, 6, 7, 8, 24),
257 c._xml_id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
260 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
261 "<SubtitleReel xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
262 "<Id>urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6</Id>"
263 "<ContentTitleText>Test</ContentTitleText>"
264 "<IssueDate>2016-04-01T03:52:00.000+00:00</IssueDate>"
265 "<ReelNumber>1</ReelNumber>"
266 "<Language>en</Language>"
267 "<EditRate>24 1</EditRate>"
268 "<TimeCodeRate>24</TimeCodeRate>"
270 "<Font AspectAdjust=\"1.0\" Color=\"FFFFFFFF\" Effect=\"none\" EffectColor=\"FF000000\" ID=\"Frutiger\" Italic=\"no\" Script=\"normal\" Size=\"48\" Underline=\"no\" Weight=\"normal\">"
271 "<Subtitle SpotNumber=\"1\" TimeIn=\"00:04:09:22\" TimeOut=\"00:04:11:22\" FadeUpTime=\"00:00:00:00\" FadeDownTime=\"00:00:00:00\">"
272 "<Text Valign=\"top\" Vposition=\"80\">Hello world</Text>"
275 "<Font AspectAdjust=\"1.0\" Color=\"FF800040\" Effect=\"border\" EffectColor=\"FF010203\" Italic=\"yes\" Script=\"normal\" Size=\"91\" Underline=\"yes\" Weight=\"bold\">"
276 "<Subtitle SpotNumber=\"2\" TimeIn=\"05:41:00:21\" TimeOut=\"06:12:15:21\" FadeUpTime=\"01:02:03:04\" FadeDownTime=\"05:06:07:08\">"
277 "<Text Valign=\"bottom\" Vposition=\"40\" Direction=\"rtl\">What's going <Space Size=\"4.2\"/>on</Text>"
287 /* Write some subtitle content as SMPTE XML and check that it is right.
288 This includes in-line font changes.
290 BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test2)
292 dcp::SMPTESubtitleAsset c;
293 c.set_reel_number (1);
294 c.set_language (dcp::LanguageTag("en"));
295 c.set_content_title_text ("Test");
296 c.set_issue_date (dcp::LocalTime ("2016-04-01T03:52:00+00:00"));
299 make_shared<dcp::SubtitleString>(
304 dcp::Colour (255, 255, 255),
307 dcp::Time (0, 0, 1, 0, 24),
308 dcp::Time (0, 0, 9, 0, 24),
316 dcp::Colour (0, 0, 0),
317 dcp::Time (0, 0, 0, 0, 24),
318 dcp::Time (0, 0, 0, 0, 24),
324 make_shared<dcp::SubtitleString>(
329 dcp::Colour (255, 255, 255),
332 dcp::Time (0, 0, 1, 0, 24),
333 dcp::Time (0, 0, 9, 0, 24),
341 dcp::Colour (0, 0, 0),
342 dcp::Time (0, 0, 0, 0, 24),
343 dcp::Time (0, 0, 0, 0, 24),
349 make_shared<dcp::SubtitleString>(
354 dcp::Colour (255, 255, 255),
357 dcp::Time (0, 0, 1, 0, 24),
358 dcp::Time (0, 0, 9, 0, 24),
366 dcp::Colour (0, 0, 0),
367 dcp::Time (0, 0, 0, 0, 24),
368 dcp::Time (0, 0, 0, 0, 24),
374 make_shared<dcp::SubtitleString>(
379 dcp::Colour (255, 255, 255),
382 dcp::Time (0, 0, 1, 0, 24),
383 dcp::Time (0, 0, 9, 0, 24),
391 dcp::Colour (0, 0, 0),
392 dcp::Time (0, 0, 0, 0, 24),
393 dcp::Time (0, 0, 0, 0, 24),
399 make_shared<dcp::SubtitleString>(
404 dcp::Colour (255, 255, 255),
407 dcp::Time (0, 0, 1, 0, 24),
408 dcp::Time (0, 0, 9, 0, 24),
416 dcp::Colour (0, 0, 0),
417 dcp::Time (0, 0, 0, 0, 24),
418 dcp::Time (0, 0, 0, 0, 24),
424 make_shared<dcp::SubtitleString>(
429 dcp::Colour (255, 255, 255),
432 dcp::Time (0, 0, 1, 0, 24),
433 dcp::Time (0, 0, 9, 0, 24),
441 dcp::Colour (0, 0, 0),
442 dcp::Time (0, 0, 0, 0, 24),
443 dcp::Time (0, 0, 0, 0, 24),
448 c._xml_id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
452 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
453 "<SubtitleReel xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
454 "<Id>urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6</Id>"
455 "<ContentTitleText>Test</ContentTitleText>"
456 "<IssueDate>2016-04-01T03:52:00.000+00:00</IssueDate>"
457 "<ReelNumber>1</ReelNumber>"
458 "<Language>en</Language>"
459 "<EditRate>24 1</EditRate>"
460 "<TimeCodeRate>24</TimeCodeRate>"
462 "<Font AspectAdjust=\"1.0\" Color=\"FFFFFFFF\" Effect=\"none\" EffectColor=\"FF000000\" ID=\"Arial\" Script=\"normal\" Size=\"48\" Underline=\"no\" Weight=\"normal\">"
463 "<Subtitle SpotNumber=\"1\" TimeIn=\"00:00:01:00\" TimeOut=\"00:00:09:00\" FadeUpTime=\"00:00:00:00\" FadeDownTime=\"00:00:00:00\">"
464 "<Text Valign=\"top\" Vposition=\"80\">"
465 "<Font Italic=\"no\">Testing is </Font>"
466 "<Font Italic=\"yes\">really</Font>"
467 "<Font Italic=\"no\"> fun</Font>"
469 "<Text Valign=\"top\" Vposition=\"90\">"
470 "<Font Italic=\"no\">This is the </Font>"
471 "<Font Italic=\"yes\">second</Font>"
472 "<Font Italic=\"no\"> line</Font>"
482 /* Write some subtitle content as SMPTE using bitmaps and check that it is right */
483 BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test3)
485 dcp::SMPTESubtitleAsset c;
486 c.set_reel_number (1);
487 c.set_language (dcp::LanguageTag("en"));
488 c.set_content_title_text ("Test");
489 c.set_start_time (dcp::Time());
491 boost::filesystem::path const sub_image = "test/data/sub.png";
494 make_shared<dcp::SubtitleImage>(
495 dcp::ArrayData(sub_image),
496 dcp::Time (0, 4, 9, 22, 24),
497 dcp::Time (0, 4, 11, 22, 24),
502 dcp::Time (0, 0, 0, 0, 24),
503 dcp::Time (0, 0, 0, 0, 24)
507 c._id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
509 boost::filesystem::path path = "build/test/write_smpte_subtitle_test3";
510 boost::filesystem::create_directories (path);
511 c.write (path / "subs.mxf");
513 dcp::SMPTESubtitleAsset read_back (path / "subs.mxf");
514 auto subs = read_back.subtitles ();
515 BOOST_REQUIRE_EQUAL (subs.size(), 1U);
516 auto image = dynamic_pointer_cast<const dcp::SubtitleImage>(subs[0]);
517 BOOST_REQUIRE (image);
519 BOOST_CHECK (image->png_image() == dcp::ArrayData(sub_image));
520 BOOST_CHECK (image->in() == dcp::Time(0, 4, 9, 22, 24));
521 BOOST_CHECK (image->out() == dcp::Time(0, 4, 11, 22, 24));
522 BOOST_CHECK_CLOSE (image->h_position(), 0.0, 1);
523 BOOST_CHECK (image->h_align() == dcp::HAlign::CENTER);
524 BOOST_CHECK_CLOSE (image->v_position(), 0.8, 1);
525 BOOST_CHECK (image->v_align() == dcp::VAlign::TOP);
526 BOOST_CHECK (image->fade_up_time() == dcp::Time(0, 0, 0, 0, 24));
527 BOOST_CHECK (image->fade_down_time() == dcp::Time(0, 0, 0, 0, 24));
531 /* Some closed caption systems require the <Text> elements to be written in order of their
532 * vertical position (see DoM bug #2106).
534 BOOST_AUTO_TEST_CASE (write_subtitles_in_vertical_order_with_top_alignment)
536 dcp::SMPTESubtitleAsset c;
537 c.set_reel_number (1);
538 c.set_language (dcp::LanguageTag("en"));
539 c.set_content_title_text ("Test");
540 c.set_issue_date (dcp::LocalTime ("2016-04-01T03:52:00+00:00"));
543 make_shared<dcp::SubtitleString>(
548 dcp::Colour (255, 255, 255),
551 dcp::Time (0, 0, 1, 0, 24),
552 dcp::Time (0, 0, 9, 0, 24),
560 dcp::Colour (0, 0, 0),
561 dcp::Time (0, 0, 0, 0, 24),
562 dcp::Time (0, 0, 0, 0, 24),
568 make_shared<dcp::SubtitleString>(
573 dcp::Colour (255, 255, 255),
576 dcp::Time (0, 0, 1, 0, 24),
577 dcp::Time (0, 0, 9, 0, 24),
585 dcp::Colour (0, 0, 0),
586 dcp::Time (0, 0, 0, 0, 24),
587 dcp::Time (0, 0, 0, 0, 24),
592 c._xml_id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
596 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
597 "<SubtitleReel xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
598 "<Id>urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6</Id>"
599 "<ContentTitleText>Test</ContentTitleText>"
600 "<IssueDate>2016-04-01T03:52:00.000+00:00</IssueDate>"
601 "<ReelNumber>1</ReelNumber>"
602 "<Language>en</Language>"
603 "<EditRate>24 1</EditRate>"
604 "<TimeCodeRate>24</TimeCodeRate>"
606 "<Font AspectAdjust=\"1.0\" Color=\"FFFFFFFF\" Effect=\"none\" EffectColor=\"FF000000\" ID=\"Arial\" Italic=\"no\" Script=\"normal\" Size=\"48\" Underline=\"no\" Weight=\"normal\">"
607 "<Subtitle SpotNumber=\"1\" TimeIn=\"00:00:01:00\" TimeOut=\"00:00:09:00\" FadeUpTime=\"00:00:00:00\" FadeDownTime=\"00:00:00:00\">"
608 "<Text Valign=\"top\" Vposition=\"80\">Top line</Text>"
609 "<Text Valign=\"top\" Vposition=\"90\">Bottom line</Text>"
619 /* See the test above */
620 BOOST_AUTO_TEST_CASE (write_subtitles_in_vertical_order_with_bottom_alignment)
622 dcp::SMPTESubtitleAsset c;
623 c.set_reel_number (1);
624 c.set_language (dcp::LanguageTag("en"));
625 c.set_content_title_text ("Test");
626 c.set_issue_date (dcp::LocalTime ("2016-04-01T03:52:00+00:00"));
629 make_shared<dcp::SubtitleString>(
634 dcp::Colour (255, 255, 255),
637 dcp::Time (0, 0, 1, 0, 24),
638 dcp::Time (0, 0, 9, 0, 24),
646 dcp::Colour (0, 0, 0),
647 dcp::Time (0, 0, 0, 0, 24),
648 dcp::Time (0, 0, 0, 0, 24),
654 make_shared<dcp::SubtitleString>(
659 dcp::Colour (255, 255, 255),
662 dcp::Time (0, 0, 1, 0, 24),
663 dcp::Time (0, 0, 9, 0, 24),
671 dcp::Colour (0, 0, 0),
672 dcp::Time (0, 0, 0, 0, 24),
673 dcp::Time (0, 0, 0, 0, 24),
678 c._xml_id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
682 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
683 "<SubtitleReel xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
684 "<Id>urn:uuid:a6c58cff-3e1e-4b38-acec-a42224475ef6</Id>"
685 "<ContentTitleText>Test</ContentTitleText>"
686 "<IssueDate>2016-04-01T03:52:00.000+00:00</IssueDate>"
687 "<ReelNumber>1</ReelNumber>"
688 "<Language>en</Language>"
689 "<EditRate>24 1</EditRate>"
690 "<TimeCodeRate>24</TimeCodeRate>"
692 "<Font AspectAdjust=\"1.0\" Color=\"FFFFFFFF\" Effect=\"none\" EffectColor=\"FF000000\" ID=\"Arial\" Italic=\"no\" Script=\"normal\" Size=\"48\" Underline=\"no\" Weight=\"normal\">"
693 "<Subtitle SpotNumber=\"1\" TimeIn=\"00:00:01:00\" TimeOut=\"00:00:09:00\" FadeUpTime=\"00:00:00:00\" FadeDownTime=\"00:00:00:00\">"
694 "<Text Valign=\"bottom\" Vposition=\"80\">Top line</Text>"
695 "<Text Valign=\"bottom\" Vposition=\"70\">Bottom line</Text>"