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