Supporters update.
[dcpomatic.git] / test / dcp_subtitle_test.cc
1 /*
2     Copyright (C) 2014-2021 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
22 /** @file  test/dcp_subtitle_test.cc
23  *  @brief Test DCP subtitle content in various ways.
24  *  @ingroup feature
25  */
26
27
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"
34 #include "lib/film.h"
35 #include "lib/font.h"
36 #include "lib/ratio.h"
37 #include "lib/text_content.h"
38 #include "lib/text_decoder.h"
39 #include "test.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>
44 #include <iostream>
45
46
47 using std::cout;
48 using std::list;
49 using std::make_shared;
50 using std::shared_ptr;
51 using std::vector;
52 using boost::optional;
53 #if BOOST_VERSION >= 106100
54 using namespace boost::placeholders;
55 #endif
56 using namespace dcpomatic;
57
58
59 optional<ContentStringText> stored;
60
61
62 static void
63 store (ContentStringText sub)
64 {
65         if (!stored) {
66                 stored = sub;
67         } else {
68                 for (auto i: sub.subs) {
69                         stored->subs.push_back (i);
70                 }
71         }
72 }
73
74
75 /** Test pass-through of a very simple DCP subtitle file */
76 BOOST_AUTO_TEST_CASE (dcp_subtitle_test)
77 {
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 ());
86
87         BOOST_CHECK_EQUAL (content->full_length(film).get(), DCPTime::from_seconds(2).get());
88
89         content->only_text()->set_use (true);
90         content->only_text()->set_burn (false);
91         make_and_verify_dcp (
92                 film,
93                 {
94                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
95                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
96                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
97                 });
98
99         /* This test is concerned with the subtitles, so we'll ignore any
100          * differences in sound between the DCP and the reference to avoid test
101          * failures for unrelated reasons.
102          */
103         check_dcp("test/data/dcp_subtitle_test", film->dir(film->dcp_name()), true);
104 }
105
106
107 /** Test parsing of a subtitle within an existing DCP */
108 BOOST_AUTO_TEST_CASE (dcp_subtitle_within_dcp_test)
109 {
110         auto film = new_test_film ("dcp_subtitle_within_dcp_test");
111         film->set_container (Ratio::from_id ("185"));
112         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
113         film->set_name ("frobozz");
114         auto content = make_shared<DCPContent>(TestPaths::private_data() / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV");
115         film->examine_and_add_content (content);
116         BOOST_REQUIRE (!wait_for_jobs ());
117
118         auto decoder = make_shared<DCPDecoder>(film, content, false, false, shared_ptr<DCPDecoder>());
119         decoder->only_text()->PlainStart.connect (bind (store, _1));
120
121         stored = optional<ContentStringText> ();
122         while (!decoder->pass() && !stored) {}
123
124         BOOST_REQUIRE (stored);
125         BOOST_REQUIRE_EQUAL (stored->subs.size(), 2U);
126         BOOST_CHECK_EQUAL (stored->subs.front().text(), "Noch mal.");
127         BOOST_CHECK_EQUAL (stored->subs.back().text(), "Encore une fois.");
128 }
129
130 /** Test subtitles whose text includes things like &lt;b&gt; */
131 BOOST_AUTO_TEST_CASE (dcp_subtitle_test2)
132 {
133         auto film = new_test_film ("dcp_subtitle_test2");
134         film->set_container (Ratio::from_id ("185"));
135         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
136         film->set_name ("frobozz");
137         auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub2.xml");
138         film->examine_and_add_content (content);
139         BOOST_REQUIRE (!wait_for_jobs ());
140
141         auto decoder = make_shared<DCPSubtitleDecoder>(film, content);
142         decoder->only_text()->PlainStart.connect (bind (store, _1));
143
144         stored = optional<ContentStringText> ();
145         while (!decoder->pass()) {
146                 if (stored && stored->from() == ContentTime(0)) {
147                         /* Text passed around by the player should be unescaped */
148                         BOOST_CHECK_EQUAL(stored->subs.front().text(), "<b>Hello world!</b>");
149                 }
150         }
151 }
152
153
154 /** Test a failure case */
155 BOOST_AUTO_TEST_CASE (dcp_subtitle_test3)
156 {
157         auto film = new_test_film ("dcp_subtitle_test3");
158         film->set_container (Ratio::from_id ("185"));
159         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
160         film->set_name ("frobozz");
161         film->set_interop (true);
162         auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub3.xml");
163         film->examine_and_add_content (content);
164         content->only_text()->set_language(dcp::LanguageTag("de"));
165         BOOST_REQUIRE (!wait_for_jobs ());
166
167         make_and_verify_dcp (film, { dcp::VerificationNote::Code::INVALID_STANDARD });
168
169         auto decoder = make_shared<DCPSubtitleDecoder>(film, content);
170         stored = optional<ContentStringText> ();
171         while (!decoder->pass ()) {
172                 decoder->only_text()->PlainStart.connect (bind (store, _1));
173                 if (stored && stored->from() == ContentTime::from_seconds(0.08)) {
174                         auto s = stored->subs;
175                         auto i = s.begin ();
176                         BOOST_CHECK_EQUAL (i->text(), "This");
177                         ++i;
178                         BOOST_REQUIRE (i != s.end ());
179                         BOOST_CHECK_EQUAL (i->text(), " is ");
180                         ++i;
181                         BOOST_REQUIRE (i != s.end ());
182                         BOOST_CHECK_EQUAL (i->text(), "wrong.");
183                         ++i;
184                         BOOST_REQUIRE (i == s.end ());
185                 }
186         }
187 }
188
189
190 /** Check that Interop DCPs aren't made with more than one <LoadFont> (#1273) */
191 BOOST_AUTO_TEST_CASE (dcp_subtitle_test4)
192 {
193         auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub3.xml");
194         auto content2 = make_shared<DCPSubtitleContent>("test/data/dcp_sub3.xml");
195         auto film = new_test_film2 ("dcp_subtitle_test4", {content, content2});
196         film->set_interop (true);
197
198         content->only_text()->add_font(make_shared<Font>("font1"));
199         content2->only_text()->add_font(make_shared<Font>("font2"));
200         content->only_text()->set_language(dcp::LanguageTag("de"));
201         content2->only_text()->set_language(dcp::LanguageTag("de"));
202
203         make_and_verify_dcp (film, { dcp::VerificationNote::Code::INVALID_STANDARD });
204
205         cxml::Document doc ("DCSubtitle");
206         doc.read_file (subtitle_file (film));
207         BOOST_REQUIRE_EQUAL (doc.node_children("LoadFont").size(), 1U);
208 }
209
210
211 static
212 void
213 check_font_tags (vector<cxml::NodePtr> nodes)
214 {
215         for (auto i: nodes) {
216                 if (i->name() == "Font") {
217                         BOOST_CHECK (!i->optional_string_attribute("Id") || i->string_attribute("Id") != "");
218                 }
219                 check_font_tags (i->node_children());
220         }
221 }
222
223
224 /** Check that imported <LoadFont> tags with empty IDs (or corresponding Font tags with empty IDs)
225  *  are not passed through into the DCP.
226  */
227 BOOST_AUTO_TEST_CASE (dcp_subtitle_test5)
228 {
229         auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub6.xml");
230         auto film = new_test_film2 ("dcp_subtitle_test5", {content});
231         film->set_interop (true);
232         content->only_text()->set_language(dcp::LanguageTag("de"));
233
234         make_and_verify_dcp (film, { dcp::VerificationNote::Code::INVALID_STANDARD });
235
236         cxml::Document doc ("DCSubtitle");
237         doc.read_file (subtitle_file(film));
238         BOOST_REQUIRE_EQUAL (doc.node_children("LoadFont").size(), 1U);
239         BOOST_CHECK (doc.node_children("LoadFont").front()->string_attribute("Id") != "");
240
241         check_font_tags (doc.node_children());
242 }
243
244
245 /** Check that fonts specified in the DoM content are used in the output and not ignored (#2074) */
246 BOOST_AUTO_TEST_CASE (test_font_override)
247 {
248         auto content = make_shared<DCPSubtitleContent>("test/data/dcp_sub4.xml");
249         auto film = new_test_film2("test_font_override", {content});
250         film->set_interop(true);
251         content->only_text()->set_language(dcp::LanguageTag("de"));
252
253         BOOST_REQUIRE_EQUAL(content->text.size(), 1U);
254         auto font = content->text.front()->get_font("0_theFontId");
255         BOOST_REQUIRE(font);
256         font->set_file("test/data/Inconsolata-VF.ttf");
257
258         make_and_verify_dcp (film, { dcp::VerificationNote::Code::INVALID_STANDARD });
259         check_file (subtitle_file(film).parent_path() / "font_0.ttf", "test/data/Inconsolata-VF.ttf");
260 }
261
262
263 BOOST_AUTO_TEST_CASE(entity_from_dcp_source)
264 {
265         std::ofstream source_xml("build/test/entity_from_dcp_source.xml");
266         source_xml
267                 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
268                 << "<SubtitleReel xmlns=\"http://www.smpte-ra.org/schemas/428-7/2010/DCST\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
269                 << "<Id>urn:uuid:9c0a0a67-ffd8-4c65-8b5a-c6be3ef182c5</Id>\n"
270                 << "<ContentTitleText>DCP</ContentTitleText>\n"
271                 << "<IssueDate>2022-11-30T18:13:56.000+01:00</IssueDate>\n"
272                 << "<ReelNumber>1</ReelNumber>\n"
273                 << "<EditRate>24 1</EditRate>\n"
274                 << "<TimeCodeRate>24</TimeCodeRate>\n"
275                 << "<StartTime>00:00:00:00</StartTime>\n"
276                 << "<LoadFont ID=\"font\">urn:uuid:899e5c59-50f6-467b-985b-8282c020e1ee</LoadFont>\n"
277                 << "<SubtitleList>\n"
278                 << "<Font AspectAdjust=\"1.0\" Color=\"FFFFFFFF\" Effect=\"none\" EffectColor=\"FF000000\" ID=\"font\" Italic=\"no\" Script=\"normal\" Size=\"48\" Underline=\"no\" Weight=\"normal\">\n"
279                 << "<Subtitle SpotNumber=\"1\" TimeIn=\"00:00:00:00\" TimeOut=\"00:00:10:00\" FadeUpTime=\"00:00:00:00\" FadeDownTime=\"00:00:00:00\">\n"
280                 << "<Text Valign=\"top\" Vposition=\"82.7273\">Hello &amp; world</Text>\n"
281                 << "</Subtitle>\n"
282                 << "</Font>\n"
283                 << "</SubtitleList>\n"
284                 << "</SubtitleReel>\n";
285         source_xml.close();
286
287         auto content = make_shared<DCPSubtitleContent>("build/test/entity_from_dcp_source.xml");
288         auto film = new_test_film2("entity_from_dcp_source", { content });
289         film->set_interop(false);
290         content->only_text()->set_use(true);
291         content->only_text()->set_burn(false);
292         make_and_verify_dcp (
293                 film,
294                 {
295                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
296                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
297                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
298                         dcp::VerificationNote::Code::INVALID_SUBTITLE_DURATION,
299                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
300                 });
301
302         dcp::SMPTESubtitleAsset check(dcp_file(film, "sub_"));
303         auto subs = check.subtitles();
304         BOOST_REQUIRE_EQUAL(subs.size(), 1U);
305         auto sub = std::dynamic_pointer_cast<const dcp::SubtitleString>(subs[0]);
306         BOOST_REQUIRE(sub);
307         /* libdcp::SubtitleAsset gets the text from the XML with get_content(), which
308          * resolves the 5 predefined entities & " < > ' so we shouldn't see any
309          * entity here.
310          */
311         BOOST_CHECK_EQUAL(sub->text(), "Hello & world");
312
313         /* It should be escaped in the raw XML though */
314         BOOST_REQUIRE(static_cast<bool>(check.raw_xml()));
315         BOOST_CHECK(check.raw_xml()->find("Hello &amp; world") != std::string::npos);
316
317         /* Remake with burn */
318         content->only_text()->set_burn(true);
319         boost::filesystem::remove_all(film->dir(film->dcp_name()));
320         make_and_verify_dcp (
321                 film,
322                 {
323                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
324                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
325                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
326                         dcp::VerificationNote::Code::INVALID_SUBTITLE_DURATION,
327                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
328                 });
329
330         dcp::MonoPictureAsset burnt(dcp_file(film, "j2c_"));
331         auto frame = burnt.start_read()->get_frame(12)->xyz_image();
332         auto const size = frame->size();
333         int max_X = 0;
334         for (auto y = 0; y < size.height; ++y) {
335                 for (auto x = 0; x < size.width; ++x) {
336                         max_X = std::max(frame->data(0)[x + y * size.width], max_X);
337                 }
338         }
339
340         /* Check that the subtitle got rendered to the image; if the escaping of the & is wrong Pango
341          * will throw errors and nothing will be rendered.
342          */
343         BOOST_CHECK(max_X > 100);
344 }
345