Fill test disk partitions with random noise to expose more bugs.
[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 <boost/test/unit_test.hpp>
36 #include <boost/algorithm/string.hpp>
37 #include <list>
38
39
40 using std::string;
41 using std::list;
42 using std::shared_ptr;
43 using std::make_shared;
44 using namespace dcpomatic;
45
46
47 /** Make a very short DCP with a single subtitle from .srt with no specified fonts */
48 BOOST_AUTO_TEST_CASE (srt_subtitle_test)
49 {
50         auto film = new_test_film ("srt_subtitle_test");
51         film->set_container (Ratio::from_id ("185"));
52         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
53         film->set_name ("frobozz");
54         film->set_audio_channels (6);
55         film->set_interop (false);
56         auto content = make_shared<StringTextFileContent>("test/data/subrip2.srt");
57         film->examine_and_add_content (content);
58         BOOST_REQUIRE (!wait_for_jobs());
59
60         content->only_text()->set_use (true);
61         content->only_text()->set_burn (false);
62         make_and_verify_dcp (
63                 film,
64                 {
65                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
66                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
67                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
68                 });
69
70
71         /* Should be blank video with a subtitle MXF */
72         check_dcp ("test/data/srt_subtitle_test", film->dir (film->dcp_name ()));
73 }
74
75
76 /** Same again but with a `font' specified */
77 BOOST_AUTO_TEST_CASE (srt_subtitle_test2)
78 {
79         auto film = new_test_film ("srt_subtitle_test2");
80         film->set_container (Ratio::from_id ("185"));
81         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
82         film->set_name ("frobozz");
83         film->set_audio_channels (6);
84         film->set_interop (false);
85         auto content = make_shared<StringTextFileContent> ("test/data/subrip2.srt");
86         film->examine_and_add_content (content);
87         BOOST_REQUIRE (!wait_for_jobs());
88
89         content->only_text()->set_use (true);
90         content->only_text()->set_burn (false);
91         /* Use test/data/subrip2.srt as if it were a font file  */
92         content->only_text()->fonts().front()->set_file("test/data/subrip2.srt");
93
94         make_and_verify_dcp (
95                 film,
96                 {
97                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
98                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
99                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
100                 });
101
102         /* Should be blank video with a subtitle MXF */
103         check_dcp ("test/data/srt_subtitle_test2", film->dir (film->dcp_name ()));
104 }
105
106
107 static void
108 check_subtitle_file (shared_ptr<Film> film, boost::filesystem::path ref)
109 {
110         /* Find the subtitle file and check it */
111         check_xml (subtitle_file(film), ref, {"SubtitleID"});
112 }
113
114
115 /** Make another DCP with a longer .srt file */
116 BOOST_AUTO_TEST_CASE (srt_subtitle_test3)
117 {
118         Cleanup cl;
119
120         auto content = make_shared<StringTextFileContent>(TestPaths::private_data() / "Ankoemmling_short.srt");
121         auto film = new_test_film2 ("srt_subtitle_test3", { content }, &cl);
122
123         film->set_name ("frobozz");
124         film->set_interop (true);
125         film->set_audio_channels (6);
126
127         content->only_text()->set_use (true);
128         content->only_text()->set_burn (false);
129
130         make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_STANDARD});
131
132         check_subtitle_file (film, TestPaths::private_data() / "Ankoemmling_short.xml");
133
134         cl.run ();
135 }
136
137
138 /** Build a small DCP with no picture and a single subtitle overlaid onto it */
139 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
140 {
141         auto film = new_test_film ("srt_subtitle_test4");
142         film->set_container (Ratio::from_id ("185"));
143         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
144         film->set_name ("frobozz");
145         film->set_interop (false);
146         auto content = make_shared<StringTextFileContent>("test/data/subrip2.srt");
147         content->only_text()->set_use (true);
148         content->only_text()->set_burn (false);
149         film->examine_and_add_content (content);
150         BOOST_REQUIRE (!wait_for_jobs());
151         make_and_verify_dcp (
152                 film,
153                 {
154                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
155                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
156                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
157                 });
158
159         /* Should be blank video with MXF subtitles */
160         check_dcp ("test/data/xml_subtitle_test", film->dir (film->dcp_name ()));
161 }
162
163
164 /** Check the subtitle XML when there are two subtitle files in the project */
165 BOOST_AUTO_TEST_CASE (srt_subtitle_test5)
166 {
167         auto film = new_test_film ("srt_subtitle_test5");
168         film->set_container (Ratio::from_id ("185"));
169         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
170         film->set_name ("frobozz");
171         film->set_interop (true);
172         film->set_sequence (false);
173         for (auto i = 0; i < 2; ++i) {
174                 auto content = make_shared<StringTextFileContent>("test/data/subrip2.srt");
175                 content->only_text()->set_use (true);
176                 content->only_text()->set_burn (false);
177                 film->examine_and_add_content (content);
178                 BOOST_REQUIRE (!wait_for_jobs());
179                 content->set_position (film, DCPTime());
180         }
181         make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_STANDARD});
182
183         check_dcp ("test/data/xml_subtitle_test2", film->dir (film->dcp_name ()));
184 }
185
186
187 BOOST_AUTO_TEST_CASE (srt_subtitle_test6)
188 {
189         auto content = make_shared<StringTextFileContent>("test/data/frames.srt");
190         auto film = new_test_film2 ("srt_subtitle_test6", {content});
191         film->set_interop (false);
192         content->only_text()->set_use (true);
193         content->only_text()->set_burn (false);
194         make_and_verify_dcp (
195                 film,
196                 {
197                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
198                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
199                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
200                         dcp::VerificationNote::Code::INVALID_SUBTITLE_DURATION,
201                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
202                 });
203
204         check_dcp ("test/data/srt_subtitle_test6", film->dir(film->dcp_name()));
205 }
206
207
208 #if 0
209 /* XXX: this is disabled; there is some difference in font rendering
210    between the test machine and others.
211 */
212
213 /** Test rendering of a SubRip subtitle */
214 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
215 {
216         shared_ptr<Film> film = new_test_film ("subrip_render_test");
217         shared_ptr<StringTextFile> content (new StringTextFile("test/data/subrip.srt"));
218         content->examine (shared_ptr<Job> (), true);
219         BOOST_CHECK_EQUAL (content->full_length(), DCPTime::from_seconds ((3 * 60) + 56.471));
220
221         shared_ptr<SubRipDecoder> decoder (new SubRipDecoder (content));
222         list<ContentStringText> cts = decoder->get_plain_texts (
223                 ContentTimePeriod (
224                         ContentTime::from_seconds (109), ContentTime::from_seconds (110)
225                         ), false
226                 );
227         BOOST_CHECK_EQUAL (cts.size(), 1);
228
229         PositionImage image = render_text (cts.front().subs, dcp::Size (1998, 1080));
230         write_image (image.image, "build/test/subrip_render_test.png");
231         check_file ("build/test/subrip_render_test.png", "test/data/subrip_render_test.png");
232 }
233 #endif