2 Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 /** @file test/dcp_subtitle_test.cc
23 * @brief Test DCP subtitle content in various ways.
28 #include "lib/content_text.h"
29 #include "lib/dcp_content.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/dcp_decoder.h"
32 #include "lib/dcp_subtitle_content.h"
33 #include "lib/dcp_subtitle_decoder.h"
36 #include "lib/ratio.h"
37 #include "lib/text_content.h"
38 #include "lib/text_decoder.h"
40 #include <dcp/mono_picture_asset.h>
41 #include <dcp/openjpeg_image.h>
42 #include <dcp/smpte_subtitle_asset.h>
43 #include <boost/test/unit_test.hpp>
49 using std::make_shared;
50 using std::shared_ptr;
52 using boost::optional;
53 #if BOOST_VERSION >= 106100
54 using namespace boost::placeholders;
56 using namespace dcpomatic;
59 optional<ContentStringText> stored;
63 store (ContentStringText sub)
68 for (auto i: sub.subs) {
69 stored->subs.push_back (i);
75 /** Test pass-through of a very simple DCP subtitle file */
76 BOOST_AUTO_TEST_CASE (dcp_subtitle_test)
78 auto film = new_test_film ("dcp_subtitle_test");
79 film->set_container (Ratio::from_id ("185"));
80 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
81 film->set_name ("frobozz");
82 film->set_interop (false);
83 auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub.xml");
84 film->examine_and_add_content (content);
85 BOOST_REQUIRE (!wait_for_jobs ());
87 BOOST_CHECK_EQUAL (content->full_length(film).get(), DCPTime::from_seconds(2).get());
89 content->only_text()->set_use (true);
90 content->only_text()->set_burn (false);
94 dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
95 dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
96 dcp::VerificationNote::Code::MISSING_CPL_METADATA
99 check_dcp ("test/data/dcp_subtitle_test", film->dir(film->dcp_name()));
103 /** Test parsing of a subtitle within an existing DCP */
104 BOOST_AUTO_TEST_CASE (dcp_subtitle_within_dcp_test)
106 auto film = new_test_film ("dcp_subtitle_within_dcp_test");
107 film->set_container (Ratio::from_id ("185"));
108 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
109 film->set_name ("frobozz");
110 auto content = make_shared<DCPContent>(TestPaths::private_data() / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV");
111 film->examine_and_add_content (content);
112 BOOST_REQUIRE (!wait_for_jobs ());
114 auto decoder = make_shared<DCPDecoder>(film, content, false, false, shared_ptr<DCPDecoder>());
115 decoder->only_text()->PlainStart.connect (bind (store, _1));
117 stored = optional<ContentStringText> ();
118 while (!decoder->pass() && !stored) {}
120 BOOST_REQUIRE (stored);
121 BOOST_REQUIRE_EQUAL (stored->subs.size(), 2U);
122 BOOST_CHECK_EQUAL (stored->subs.front().text(), "Noch mal.");
123 BOOST_CHECK_EQUAL (stored->subs.back().text(), "Encore une fois.");
126 /** Test subtitles whose text includes things like <b> */
127 BOOST_AUTO_TEST_CASE (dcp_subtitle_test2)
129 auto film = new_test_film ("dcp_subtitle_test2");
130 film->set_container (Ratio::from_id ("185"));
131 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
132 film->set_name ("frobozz");
133 auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub2.xml");
134 film->examine_and_add_content (content);
135 BOOST_REQUIRE (!wait_for_jobs ());
137 auto decoder = make_shared<DCPSubtitleDecoder>(film, content);
138 decoder->only_text()->PlainStart.connect (bind (store, _1));
140 stored = optional<ContentStringText> ();
141 while (!decoder->pass()) {
142 if (stored && stored->from() == ContentTime(0)) {
143 /* Text passed around by the player should be unescaped */
144 BOOST_CHECK_EQUAL(stored->subs.front().text(), "<b>Hello world!</b>");
150 /** Test a failure case */
151 BOOST_AUTO_TEST_CASE (dcp_subtitle_test3)
153 auto film = new_test_film ("dcp_subtitle_test3");
154 film->set_container (Ratio::from_id ("185"));
155 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
156 film->set_name ("frobozz");
157 film->set_interop (true);
158 auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub3.xml");
159 film->examine_and_add_content (content);
160 BOOST_REQUIRE (!wait_for_jobs ());
162 make_and_verify_dcp (film, { dcp::VerificationNote::Code::INVALID_STANDARD });
164 auto decoder = make_shared<DCPSubtitleDecoder>(film, content);
165 stored = optional<ContentStringText> ();
166 while (!decoder->pass ()) {
167 decoder->only_text()->PlainStart.connect (bind (store, _1));
168 if (stored && stored->from() == ContentTime::from_seconds(0.08)) {
169 auto s = stored->subs;
171 BOOST_CHECK_EQUAL (i->text(), "This");
173 BOOST_REQUIRE (i != s.end ());
174 BOOST_CHECK_EQUAL (i->text(), " is ");
176 BOOST_REQUIRE (i != s.end ());
177 BOOST_CHECK_EQUAL (i->text(), "wrong.");
179 BOOST_REQUIRE (i == s.end ());
185 /** Check that Interop DCPs aren't made with more than one <LoadFont> (#1273) */
186 BOOST_AUTO_TEST_CASE (dcp_subtitle_test4)
188 auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub3.xml");
189 auto content2 = make_shared<DCPSubtitleContent>("test/data/dcp_sub3.xml");
190 auto film = new_test_film2 ("dcp_subtitle_test4", {content, content2});
191 film->set_interop (true);
193 content->only_text()->add_font(make_shared<Font>("font1"));
194 content2->only_text()->add_font(make_shared<Font>("font2"));
196 make_and_verify_dcp (film, { dcp::VerificationNote::Code::INVALID_STANDARD });
198 cxml::Document doc ("DCSubtitle");
199 doc.read_file (subtitle_file (film));
200 BOOST_REQUIRE_EQUAL (doc.node_children("LoadFont").size(), 1U);
206 check_font_tags (vector<cxml::NodePtr> nodes)
208 for (auto i: nodes) {
209 if (i->name() == "Font") {
210 BOOST_CHECK (!i->optional_string_attribute("Id") || i->string_attribute("Id") != "");
212 check_font_tags (i->node_children());
217 /** Check that imported <LoadFont> tags with empty IDs (or corresponding Font tags with empty IDs)
218 * are not passed through into the DCP.
220 BOOST_AUTO_TEST_CASE (dcp_subtitle_test5)
222 auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub6.xml");
223 auto film = new_test_film2 ("dcp_subtitle_test5", {content});
224 film->set_interop (true);
226 make_and_verify_dcp (film, { dcp::VerificationNote::Code::INVALID_STANDARD });
228 cxml::Document doc ("DCSubtitle");
229 doc.read_file (subtitle_file(film));
230 BOOST_REQUIRE_EQUAL (doc.node_children("LoadFont").size(), 1U);
231 BOOST_CHECK (doc.node_children("LoadFont").front()->string_attribute("Id") != "");
233 check_font_tags (doc.node_children());
237 /** Check that fonts specified in the DoM content are used in the output and not ignored (#2074) */
238 BOOST_AUTO_TEST_CASE (test_font_override)
240 auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub4.xml");
241 auto film = new_test_film2("test_font_override", {content});
242 film->set_interop(true);
244 BOOST_REQUIRE_EQUAL(content->text.size(), 1U);
245 content->text.front()->get_font("theFontId")->set_file("test/data/Inconsolata-VF.ttf");
247 make_and_verify_dcp (film, { dcp::VerificationNote::Code::INVALID_STANDARD });
248 check_file (subtitle_file(film).parent_path() / "font_0.ttf", "test/data/Inconsolata-VF.ttf");
252 BOOST_AUTO_TEST_CASE(entity_from_dcp_source)
254 std::ofstream source_xml("build/test/entity_from_dcp_source.xml");
256 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
257 << "<SubtitleReel xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
258 << "<Id>urn:uuid:9c0a0a67-ffd8-4c65-8b5a-c6be3ef182c5</Id>\n"
259 << "<ContentTitleText>DCP</ContentTitleText>\n"
260 << "<IssueDate>2022-11-30T18:13:56.000+01:00</IssueDate>\n"
261 << "<ReelNumber>1</ReelNumber>\n"
262 << "<EditRate>24 1</EditRate>\n"
263 << "<TimeCodeRate>24</TimeCodeRate>\n"
264 << "<StartTime>00:00:00:00</StartTime>\n"
265 << "<LoadFont ID=\"font\">urn:uuid:899e5c59-50f6-467b-985b-8282c020e1ee</LoadFont>\n"
266 << "<SubtitleList>\n"
267 << "<Font AspectAdjust=\"1.0\" Color=\"FFFFFFFF\" Effect=\"none\" EffectColor=\"FF000000\" ID=\"font\" Italic=\"no\" Script=\"normal\" Size=\"48\" Underline=\"no\" Weight=\"normal\">\n"
268 << "<Subtitle SpotNumber=\"1\" TimeIn=\"00:00:00:00\" TimeOut=\"00:00:10:00\" FadeUpTime=\"00:00:00:00\" FadeDownTime=\"00:00:00:00\">\n"
269 << "<Text Valign=\"top\" Vposition=\"82.7273\">Hello & world</Text>\n"
272 << "</SubtitleList>\n"
273 << "</SubtitleReel>\n";
276 auto content = make_shared<DCPSubtitleContent>("build/test/entity_from_dcp_source.xml");
277 auto film = new_test_film2("entity_from_dcp_source", { content });
278 film->set_interop(false);
279 content->only_text()->set_use(true);
280 content->only_text()->set_burn(false);
281 make_and_verify_dcp (
284 dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
285 dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
286 dcp::VerificationNote::Code::MISSING_CPL_METADATA,
287 dcp::VerificationNote::Code::INVALID_SUBTITLE_DURATION,
288 dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
291 dcp::SMPTESubtitleAsset check(dcp_file(film, "sub_"));
292 auto subs = check.subtitles();
293 BOOST_REQUIRE_EQUAL(subs.size(), 1U);
294 auto sub = std::dynamic_pointer_cast<const dcp::SubtitleString>(subs[0]);
296 /* libdcp::SubtitleAsset gets the text from the XML with get_content(), which
297 * resolves the 5 predefined entities & " < > ' so we shouldn't see any
300 BOOST_CHECK_EQUAL(sub->text(), "Hello & world");
302 /* It should be escaped in the raw XML though */
303 BOOST_REQUIRE(static_cast<bool>(check.raw_xml()));
304 BOOST_CHECK(check.raw_xml()->find("Hello & world") != std::string::npos);
306 /* Remake with burn */
307 content->only_text()->set_burn(true);
308 boost::filesystem::remove_all(film->dir(film->dcp_name()));
309 make_and_verify_dcp (
312 dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
313 dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
314 dcp::VerificationNote::Code::MISSING_CPL_METADATA,
315 dcp::VerificationNote::Code::INVALID_SUBTITLE_DURATION,
316 dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
319 dcp::MonoPictureAsset burnt(dcp_file(film, "j2c_"));
320 auto frame = burnt.start_read()->get_frame(12)->xyz_image();
321 auto const size = frame->size();
323 for (auto y = 0; y < size.height; ++y) {
324 for (auto x = 0; x < size.width; ++x) {
325 max_X = std::max(frame->data(0)[x + y * size.width], max_X);
329 /* Check that the subtitle got rendered to the image; if the escaping of the & is wrong Pango
330 * will throw errors and nothing will be rendered.
332 BOOST_CHECK(max_X > 100);