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