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