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