Clarify some documentation slightly.
[dcpomatic.git] / test / srt_subtitle_test.cc
1 /*
2     Copyright (C) 2015-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/srt_subtitle_test.cc
23  *  @brief Test writing DCPs with subtitles from .srt.
24  *  @ingroup feature
25  */
26
27
28 #include "lib/dcp_content_type.h"
29 #include "lib/film.h"
30 #include "lib/font.h"
31 #include "lib/ratio.h"
32 #include "lib/string_text_file_content.h"
33 #include "lib/text_content.h"
34 #include "test.h"
35 #include <dcp/smpte_subtitle_asset.h>
36 #include <dcp/subtitle_string.h>
37 #include <boost/algorithm/string.hpp>
38 #include <boost/test/unit_test.hpp>
39 #include <list>
40
41
42 using std::list;
43 using std::make_shared;
44 using std::shared_ptr;
45 using std::string;
46 using namespace dcpomatic;
47
48
49 /** Make a very short DCP with a single subtitle from .srt with no specified fonts */
50 BOOST_AUTO_TEST_CASE (srt_subtitle_test)
51 {
52         auto content = make_shared<StringTextFileContent>("test/data/subrip2.srt");
53         auto film = new_test_film("srt_subtitle_test", { content });
54         film->set_dcp_content_type(DCPContentType::from_isdcf_name("TLR"));
55         film->set_name("frobozz");
56         film->set_audio_channels(16);
57
58         content->only_text()->set_use (true);
59         content->only_text()->set_burn (false);
60         make_and_verify_dcp (
61                 film,
62                 {
63                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
64                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
65                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
66                 });
67
68
69         /* Should be blank video with a subtitle MXF */
70         check_dcp ("test/data/srt_subtitle_test", film->dir (film->dcp_name ()));
71 }
72
73
74 /** Same again but with a `font' specified */
75 BOOST_AUTO_TEST_CASE (srt_subtitle_test2)
76 {
77         auto content = make_shared<StringTextFileContent> ("test/data/subrip2.srt");
78         auto film = new_test_film("srt_subtitle_test2", { content });
79         film->set_dcp_content_type(DCPContentType::from_isdcf_name("TLR"));
80         film->set_name("frobozz");
81         film->set_audio_channels (6);
82
83         content->only_text()->set_use (true);
84         content->only_text()->set_burn (false);
85         /* Use test/data/subrip2.srt as if it were a font file  */
86         content->only_text()->fonts().front()->set_file("test/data/subrip2.srt");
87
88         make_and_verify_dcp (
89                 film,
90                 {
91                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
92                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
93                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
94                 },
95                 true,
96                 /* ClairMeta tries to inspect the font file and fails because it isn't one */
97                 false
98                 );
99
100         /* Should be blank video with a subtitle MXF; sound is irrelevant */
101         check_dcp("test/data/srt_subtitle_test2", film->dir(film->dcp_name()), true);
102 }
103
104
105 static void
106 check_subtitle_file (shared_ptr<Film> film, boost::filesystem::path ref)
107 {
108         /* Find the subtitle file and check it */
109         check_xml (subtitle_file(film), ref, {"SubtitleID"});
110 }
111
112
113 /** Make another DCP with a longer .srt file */
114 BOOST_AUTO_TEST_CASE (srt_subtitle_test3)
115 {
116         Cleanup cl;
117
118         auto content = make_shared<StringTextFileContent>(TestPaths::private_data() / "Ankoemmling_short.srt");
119         auto film = new_test_film("srt_subtitle_test3", { content }, &cl);
120
121         film->set_name ("frobozz");
122         film->set_interop (true);
123         film->set_audio_channels (6);
124
125         content->only_text()->set_use (true);
126         content->only_text()->set_burn (false);
127         content->only_text()->set_language(dcp::LanguageTag("de"));
128
129         make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_STANDARD});
130
131         check_subtitle_file (film, TestPaths::private_data() / "Ankoemmling_short.xml");
132
133         cl.run ();
134 }
135
136
137 /** Build a small DCP with no picture and a single subtitle overlaid onto it */
138 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
139 {
140         auto content = make_shared<StringTextFileContent>("test/data/subrip2.srt");
141         auto film = new_test_film("srt_subtitle_test4", { content });
142         film->set_dcp_content_type(DCPContentType::from_isdcf_name("TLR"));
143         film->set_name("frobozz");
144         content->only_text()->set_use (true);
145         content->only_text()->set_burn (false);
146         make_and_verify_dcp (
147                 film,
148                 {
149                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
150                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
151                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
152                 });
153
154         /* Should be blank video with MXF subtitles; sound is irrelevant */
155         check_dcp("test/data/xml_subtitle_test", film->dir(film->dcp_name()), true);
156 }
157
158
159 /** Check the subtitle XML when there are two subtitle files in the project */
160 BOOST_AUTO_TEST_CASE (srt_subtitle_test5)
161 {
162         auto film = new_test_film("srt_subtitle_test5");
163         film->set_dcp_content_type(DCPContentType::from_isdcf_name("TLR"));
164         film->set_name("frobozz");
165         film->set_interop (true);
166         film->set_sequence (false);
167         film->set_audio_channels(6);
168         for (auto i = 0; i < 2; ++i) {
169                 auto content = make_shared<StringTextFileContent>("test/data/subrip2.srt");
170                 content->only_text()->set_use (true);
171                 content->only_text()->set_burn (false);
172                 content->only_text()->set_language(dcp::LanguageTag("de"));
173                 film->examine_and_add_content (content);
174                 BOOST_REQUIRE (!wait_for_jobs());
175                 content->set_position (film, DCPTime());
176         }
177         make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_STANDARD});
178
179         check_dcp ("test/data/xml_subtitle_test2", film->dir (film->dcp_name ()));
180 }
181
182
183 BOOST_AUTO_TEST_CASE (srt_subtitle_test6)
184 {
185         auto content = make_shared<StringTextFileContent>("test/data/frames.srt");
186         auto film = new_test_film("srt_subtitle_test6", {content});
187         film->set_interop (false);
188         content->only_text()->set_use (true);
189         content->only_text()->set_burn (false);
190         make_and_verify_dcp (
191                 film,
192                 {
193                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
194                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
195                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
196                         dcp::VerificationNote::Code::INVALID_SUBTITLE_DURATION,
197                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
198                 });
199
200         /* This test is concerned with the subtitles, so we'll ignore any
201          * differences in sound between the DCP and the reference to avoid test
202          * failures for unrelated reasons.
203          */
204         check_dcp("test/data/srt_subtitle_test6", film->dir(film->dcp_name()), true);
205 }
206
207
208 /** Test a case where a & in srt ended up in the SMPTE subtitle as &amp;amp */
209 BOOST_AUTO_TEST_CASE(srt_subtitle_entity)
210 {
211         std::ofstream srt("build/test/srt_subtitle_entity.srt");
212         srt << "1\n";
213         srt << "00:00:01,000 -> 00:00:10,000\n";
214         srt << "Hello & world\n";
215         srt.close();
216
217         auto content = make_shared<StringTextFileContent>("build/test/srt_subtitle_entity.srt");
218         auto film = new_test_film("srt_subtitle_entity", { content });
219         film->set_interop(false);
220         content->only_text()->set_use(true);
221         content->only_text()->set_burn(false);
222         make_and_verify_dcp (
223                 film,
224                 {
225                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
226                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
227                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
228                         dcp::VerificationNote::Code::INVALID_SUBTITLE_DURATION,
229                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
230                 });
231
232         dcp::SMPTESubtitleAsset check(dcp_file(film, "sub_"));
233         auto subs = check.subtitles();
234         BOOST_REQUIRE_EQUAL(subs.size(), 1U);
235         auto sub = std::dynamic_pointer_cast<const dcp::SubtitleString>(subs[0]);
236         BOOST_REQUIRE(sub);
237         /* libdcp::SubtitleAsset gets the text from the XML with get_content(), which
238          * resolves the 5 predefined entities & " < > ' so we shouldn't see any
239          * entity here.
240          */
241         BOOST_CHECK_EQUAL(sub->text(), "Hello & world");
242
243         /* It should be escaped in the raw XML though */
244         BOOST_REQUIRE(static_cast<bool>(check.raw_xml()));
245         BOOST_CHECK(check.raw_xml()->find("Hello &amp; world") != string::npos);
246 }
247
248
249 /** A control code in a .srt file should not make it into the XML */
250 BOOST_AUTO_TEST_CASE(srt_subtitle_control_code)
251 {
252         std::ofstream srt("build/test/srt_subtitle_control_code.srt");
253         srt << "1\n";
254         srt << "00:00:01,000 -> 00:00:10,000\n";
255         srt << "Hello \x0c world\n";
256         srt.close();
257
258         auto content = make_shared<StringTextFileContent>("build/test/srt_subtitle_control_code.srt");
259         auto film = new_test_film("srt_subtitle_control_code", { content });
260         film->set_interop(false);
261         content->only_text()->set_use(true);
262         content->only_text()->set_burn(false);
263         make_and_verify_dcp (
264                 film,
265                 {
266                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
267                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
268                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
269                 });
270 }
271
272
273 #if 0
274 /* XXX: this is disabled; there is some difference in font rendering
275    between the test machine and others.
276 */
277
278 /** Test rendering of a SubRip subtitle */
279 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
280 {
281         shared_ptr<StringTextFile> content (new StringTextFile("test/data/subrip.srt"));
282         shared_ptr<Film> film = new_test_film("subrip_render_test", { content });
283         content->examine (shared_ptr<Job> (), true);
284         BOOST_CHECK_EQUAL (content->full_length(), DCPTime::from_seconds ((3 * 60) + 56.471));
285
286         shared_ptr<SubRipDecoder> decoder (new SubRipDecoder (content));
287         list<ContentStringText> cts = decoder->get_plain_texts (
288                 ContentTimePeriod (
289                         ContentTime::from_seconds (109), ContentTime::from_seconds (110)
290                         ), false
291                 );
292         BOOST_CHECK_EQUAL (cts.size(), 1);
293
294         PositionImage image = render_text (cts.front().subs, dcp::Size (1998, 1080));
295         write_image (image.image, "build/test/subrip_render_test.png");
296         check_file ("build/test/subrip_render_test.png", "test/data/subrip_render_test.png");
297 }
298 #endif