Supporters update.
[dcpomatic.git] / test / subtitle_reel_test.cc
1 /*
2     Copyright (C) 2019-2020 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 #include "lib/content_factory.h"
22 #include "lib/dcp_subtitle_content.h"
23 #include "lib/film.h"
24 #include "lib/image_content.h"
25 #include "lib/text_content.h"
26 #include "lib/video_content.h"
27 #include "test.h"
28 #include <dcp/dcp.h>
29 #include <dcp/cpl.h>
30 #include <dcp/reel.h>
31 #include <dcp/interop_subtitle_asset.h>
32 #include <dcp/reel_closed_caption_asset.h>
33 #include <dcp/reel_subtitle_asset.h>
34 #include <boost/test/unit_test.hpp>
35
36
37 using std::list;
38 using std::make_shared;
39 using std::string;
40 using boost::optional;
41
42
43 /* Check that timings are done correctly for multi-reel DCPs with PNG subs */
44 BOOST_AUTO_TEST_CASE (subtitle_reel_test)
45 {
46         auto film = new_test_film2 ("subtitle_reel_test");
47         film->set_interop (true);
48         auto red_a = make_shared<ImageContent>("test/data/flat_red.png");
49         auto red_b = make_shared<ImageContent>("test/data/flat_red.png");
50         auto sub_a = make_shared<DCPSubtitleContent>("test/data/png_subs/subs.xml");
51         auto sub_b = make_shared<DCPSubtitleContent>("test/data/png_subs/subs.xml");
52
53         film->examine_and_add_content (red_a);
54         film->examine_and_add_content (red_b);
55         film->examine_and_add_content (sub_a);
56         film->examine_and_add_content (sub_b);
57
58         BOOST_REQUIRE (!wait_for_jobs());
59
60         red_a->set_position (film, dcpomatic::DCPTime());
61         red_a->video->set_length (240);
62         sub_a->set_position (film, dcpomatic::DCPTime());
63         sub_a->only_text()->set_language(dcp::LanguageTag("de"));
64         red_b->set_position (film, dcpomatic::DCPTime::from_seconds(10));
65         red_b->video->set_length (240);
66         sub_b->set_position (film, dcpomatic::DCPTime::from_seconds(10));
67         sub_b->only_text()->set_language(dcp::LanguageTag("de"));
68
69         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
70
71         make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_STANDARD});
72
73         dcp::DCP dcp ("build/test/subtitle_reel_test/" + film->dcp_name());
74         dcp.read ();
75         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1U);
76         auto cpl = dcp.cpls().front();
77
78         auto reels = cpl->reels ();
79         BOOST_REQUIRE_EQUAL (reels.size(), 2U);
80         auto i = reels.begin ();
81         BOOST_REQUIRE ((*i)->main_subtitle());
82         BOOST_REQUIRE ((*i)->main_subtitle()->asset());
83         auto A = std::dynamic_pointer_cast<dcp::InteropSubtitleAsset>((*i)->main_subtitle()->asset());
84         BOOST_REQUIRE (A);
85         ++i;
86         BOOST_REQUIRE ((*i)->main_subtitle());
87         BOOST_REQUIRE ((*i)->main_subtitle()->asset());
88         auto B = std::dynamic_pointer_cast<dcp::InteropSubtitleAsset>((*i)->main_subtitle()->asset());
89         BOOST_REQUIRE (B);
90
91         BOOST_REQUIRE_EQUAL (A->subtitles().size(), 1U);
92         BOOST_REQUIRE_EQUAL (B->subtitles().size(), 1U);
93
94         /* These times should be the same as they are should be offset from the start of the reel */
95         BOOST_CHECK (A->subtitles().front()->in() == B->subtitles().front()->in());
96 }
97
98
99
100 /** Check that with a SMPTE DCP if we have subtitles in one reel, all reels have a
101  *  SubtitleAsset (even if it's empty); SMPTE Bv2.1 section 8.3.1.
102  */
103 BOOST_AUTO_TEST_CASE (subtitle_in_all_reels_test)
104 {
105         auto film = new_test_film2 ("subtitle_in_all_reels_test");
106         film->set_interop (false);
107         film->set_sequence (false);
108         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
109         for (int i = 0; i < 3; ++i) {
110                 auto video = content_factory("test/data/flat_red.png")[0];
111                 film->examine_and_add_content (video);
112                 BOOST_REQUIRE (!wait_for_jobs());
113                 video->video->set_length (15 * 24);
114                 video->set_position (film, dcpomatic::DCPTime::from_seconds(15 * i));
115         }
116         auto subs = content_factory("test/data/15s.srt")[0];
117         film->examine_and_add_content (subs);
118         BOOST_REQUIRE (!wait_for_jobs());
119         make_and_verify_dcp (
120                 film,
121                 {
122                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
123                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
124                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING
125                 });
126
127         dcp::DCP dcp ("build/test/subtitle_in_all_reels_test/" + film->dcp_name());
128         dcp.read ();
129         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1U);
130         auto cpl = dcp.cpls()[0];
131         BOOST_REQUIRE_EQUAL (cpl->reels().size(), 3U);
132
133         for (auto i: cpl->reels()) {
134                 BOOST_CHECK (i->main_subtitle());
135         }
136 }
137
138
139 /** Check that with a SMPTE DCP if we have closed captions in one reel, all reels have a
140  *  ClosedCaptionAssets for the same set of tracks (even if they are empty); SMPTE Bv2.1 section 8.3.1.
141  */
142 BOOST_AUTO_TEST_CASE (closed_captions_in_all_reels_test)
143 {
144         auto film = new_test_film2 ("closed_captions_in_all_reels_test");
145         film->set_interop (false);
146         film->set_sequence (false);
147         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
148
149         for (int i = 0; i < 3; ++i) {
150                 auto video = content_factory("test/data/flat_red.png")[0];
151                 film->examine_and_add_content (video);
152                 BOOST_REQUIRE (!wait_for_jobs());
153                 video->video->set_length (15 * 24);
154                 video->set_position (film, dcpomatic::DCPTime::from_seconds(15 * i));
155         }
156
157         auto ccap1 = content_factory("test/data/15s.srt")[0];
158         film->examine_and_add_content (ccap1);
159         BOOST_REQUIRE (!wait_for_jobs());
160         ccap1->text.front()->set_type (TextType::CLOSED_CAPTION);
161         ccap1->text.front()->set_dcp_track (DCPTextTrack("Test", dcp::LanguageTag("de-DE")));
162
163         auto ccap2 = content_factory("test/data/15s.srt")[0];
164         film->examine_and_add_content (ccap2);
165         BOOST_REQUIRE (!wait_for_jobs());
166         ccap2->text.front()->set_type (TextType::CLOSED_CAPTION);
167         ccap2->text.front()->set_dcp_track (DCPTextTrack("Other", dcp::LanguageTag("en-GB")));
168
169         make_and_verify_dcp (
170                 film,
171                 {
172                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
173                         dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING
174                 },
175                 true,
176                 /* ClairMeta gives an error with multiple ClosedCaption assets */
177                 false
178                 );
179
180         dcp::DCP dcp ("build/test/closed_captions_in_all_reels_test/" + film->dcp_name());
181         dcp.read ();
182         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1U);
183         auto cpl = dcp.cpls().front();
184         BOOST_REQUIRE_EQUAL (cpl->reels().size(), 3U);
185
186         for (auto i: cpl->reels()) {
187                 BOOST_REQUIRE_EQUAL (i->closed_captions().size(), 2U);
188                 auto first = i->closed_captions().front()->language();
189                 auto second = i->closed_captions().back()->language();
190                 BOOST_REQUIRE (first);
191                 BOOST_REQUIRE (second);
192                 BOOST_CHECK (
193                         (*first == "en-GB" && *second == "de-DE") ||
194                         (*first == "de-DE" && *second == "en-GB")
195                         );
196         }
197 }
198
199
200 BOOST_AUTO_TEST_CASE (subtitles_split_at_reel_boundaries)
201 {
202         auto film = new_test_film2 ("subtitles_split_at_reel_boundaries");
203         film->set_interop (true);
204
205         film->set_sequence (false);
206         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
207
208         for (int i = 0; i < 3; ++i) {
209                 auto video = content_factory("test/data/flat_red.png")[0];
210                 film->examine_and_add_content (video);
211                 BOOST_REQUIRE (!wait_for_jobs());
212                 video->video->set_length (15 * 24);
213                 video->set_position (film, dcpomatic::DCPTime::from_seconds(15 * i));
214         }
215
216         auto subtitle = content_factory("test/data/45s.srt")[0];
217         film->examine_and_add_content (subtitle);
218         BOOST_REQUIRE (!wait_for_jobs());
219         subtitle->only_text()->set_language(dcp::LanguageTag("de"));
220
221         make_and_verify_dcp (film, { dcp::VerificationNote::Code::INVALID_STANDARD });
222
223         dcp::DCP dcp (film->dir(film->dcp_name()));
224         dcp.read();
225         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1U);
226         auto cpl = dcp.cpls()[0];
227         BOOST_REQUIRE_EQUAL (cpl->reels().size(), 3U);
228
229         for (auto i: cpl->reels()) {
230                 auto reel_sub = i->main_subtitle();
231                 BOOST_REQUIRE (reel_sub);
232                 auto sub = reel_sub->asset();
233                 BOOST_REQUIRE (sub);
234                 BOOST_CHECK_EQUAL (sub->subtitles().size(), 1U);
235         }
236 }
237