Fill test disk partitions with random noise to expose more bugs.
[dcpomatic.git] / test / vf_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/vf_Test.cc
23  *  @brief Various VF-related tests.
24  *  @ingroup feature
25  */
26
27
28 #include "lib/content_factory.h"
29 #include "lib/dcp_content.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/ffmpeg_content.h"
32 #include "lib/film.h"
33 #include "lib/player.h"
34 #include "lib/referenced_reel_asset.h"
35 #include "lib/video_content.h"
36 #include "test.h"
37 #include <dcp/cpl.h>
38 #include <dcp/reel.h>
39 #include <dcp/reel_picture_asset.h>
40 #include <dcp/reel_sound_asset.h>
41 #include <boost/test/unit_test.hpp>
42 #include <iostream>
43
44
45 using std::cout;
46 using std::dynamic_pointer_cast;
47 using std::list;
48 using std::make_shared;
49 using std::shared_ptr;
50 using std::string;
51 using namespace dcpomatic;
52
53
54 /** Test the logic which decides whether a DCP can be referenced or not */
55 BOOST_AUTO_TEST_CASE (vf_test1)
56 {
57         auto film = new_test_film ("vf_test1");
58         film->set_interop (false);
59         auto dcp = make_shared<DCPContent>("test/data/reels_test2");
60         film->examine_and_add_content (dcp);
61         BOOST_REQUIRE (!wait_for_jobs());
62
63         /* Multi-reel DCP can't be referenced if we are using a single reel for the project */
64         film->set_reel_type (ReelType::SINGLE);
65         string why_not;
66         BOOST_CHECK (!dcp->can_reference_video(film, why_not));
67         BOOST_CHECK (!dcp->can_reference_audio(film, why_not));
68         BOOST_CHECK (!dcp->can_reference_text(film, TextType::OPEN_SUBTITLE, why_not));
69         BOOST_CHECK (!dcp->can_reference_text(film, TextType::CLOSED_CAPTION, why_not));
70
71         /* Multi-reel DCP can be referenced if we are using by-video-content */
72         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
73         BOOST_CHECK (dcp->can_reference_video(film, why_not));
74         BOOST_CHECK (dcp->can_reference_audio(film, why_not));
75         /* (but reels_test2 has no texts to reference) */
76         BOOST_CHECK (!dcp->can_reference_text(film, TextType::OPEN_SUBTITLE, why_not));
77         BOOST_CHECK (!dcp->can_reference_text(film, TextType::CLOSED_CAPTION, why_not));
78
79         auto other = make_shared<FFmpegContent>("test/data/test.mp4");
80         film->examine_and_add_content (other);
81         BOOST_REQUIRE (!wait_for_jobs());
82         BOOST_CHECK (!other->audio);
83
84         /* Not possible if there is overlap; we only check video here as that's all test.mp4 has */
85         other->set_position (film, DCPTime());
86         BOOST_CHECK (!dcp->can_reference_video(film, why_not));
87
88         /* This should not be considered an overlap */
89         other->set_position (film, dcp->end(film));
90         BOOST_CHECK (dcp->can_reference_video(film, why_not));
91         BOOST_CHECK (dcp->can_reference_audio(film, why_not));
92         /* (reels_test2 has no texts to reference) */
93         BOOST_CHECK (!dcp->can_reference_text(film, TextType::OPEN_SUBTITLE, why_not));
94         BOOST_CHECK (!dcp->can_reference_text(film, TextType::CLOSED_CAPTION, why_not));
95 }
96
97
98 /** Make a OV with video and audio and a VF referencing the OV and adding subs */
99 BOOST_AUTO_TEST_CASE (vf_test2)
100 {
101         /* Make the OV */
102         auto ov = new_test_film ("vf_test2_ov");
103         ov->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
104         ov->set_name ("vf_test2_ov");
105         auto video = content_factory("test/data/flat_red.png").front();
106         ov->examine_and_add_content (video);
107         BOOST_REQUIRE (!wait_for_jobs());
108         video->video->set_length (24 * 5);
109         auto audio = content_factory("test/data/white.wav").front();
110         ov->examine_and_add_content (audio);
111         BOOST_REQUIRE (!wait_for_jobs());
112         make_and_verify_dcp (ov);
113
114         /* Make the VF */
115         auto vf = new_test_film ("vf_test2_vf");
116         vf->set_name ("vf_test2_vf");
117         vf->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
118         vf->set_reel_type (ReelType::BY_VIDEO_CONTENT);
119         auto dcp = make_shared<DCPContent>(ov->dir(ov->dcp_name()));
120         vf->examine_and_add_content (dcp);
121         BOOST_REQUIRE (!wait_for_jobs());
122         dcp->set_reference_video (true);
123         dcp->set_reference_audio (true);
124         auto sub = content_factory("test/data/subrip4.srt").front();
125         vf->examine_and_add_content (sub);
126         BOOST_REQUIRE (!wait_for_jobs());
127         make_and_verify_dcp (
128                 vf,
129                 {
130                         dcp::VerificationNote::Code::EXTERNAL_ASSET,
131                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
132                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
133                         dcp::VerificationNote::Code::INVALID_SUBTITLE_DURATION
134                 });
135
136         dcp::DCP ov_c (ov->dir(ov->dcp_name()));
137         ov_c.read ();
138         BOOST_REQUIRE_EQUAL (ov_c.cpls().size(), 1U);
139         BOOST_REQUIRE_EQUAL (ov_c.cpls()[0]->reels().size(), 1U);
140         BOOST_REQUIRE (ov_c.cpls()[0]->reels()[0]->main_picture());
141         string const pic_id = ov_c.cpls()[0]->reels()[0]->main_picture()->id();
142         BOOST_REQUIRE (ov_c.cpls()[0]->reels()[0]->main_sound());
143         string const sound_id = ov_c.cpls()[0]->reels()[0]->main_sound()->id();
144         BOOST_REQUIRE (!ov_c.cpls()[0]->reels()[0]->main_subtitle());
145
146         dcp::DCP vf_c (vf->dir(vf->dcp_name()));
147         vf_c.read ();
148         BOOST_REQUIRE_EQUAL (vf_c.cpls().size(), 1U);
149         BOOST_REQUIRE_EQUAL (vf_c.cpls()[0]->reels().size(), 1U);
150         BOOST_REQUIRE (vf_c.cpls()[0]->reels()[0]->main_picture());
151         BOOST_CHECK_EQUAL (vf_c.cpls()[0]->reels()[0]->main_picture()->id(), pic_id);
152         BOOST_REQUIRE (vf_c.cpls()[0]->reels()[0]->main_sound());
153         BOOST_CHECK_EQUAL (vf_c.cpls()[0]->reels()[0]->main_sound()->id(), sound_id);
154         BOOST_REQUIRE (vf_c.cpls()[0]->reels()[0]->main_subtitle());
155 }
156
157
158 /** Test creation of a VF using a trimmed OV; the output should have entry point /
159  *  duration altered to effect the trimming.
160  */
161 BOOST_AUTO_TEST_CASE (vf_test3)
162 {
163         /* Make the OV */
164         auto ov = new_test_film ("vf_test3_ov");
165         ov->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
166         ov->set_name ("vf_test3_ov");
167         auto video = content_factory("test/data/flat_red.png").front();
168         ov->examine_and_add_content (video);
169         BOOST_REQUIRE (!wait_for_jobs());
170         video->video->set_length (24 * 5);
171         auto audio = content_factory("test/data/white.wav").front();
172         ov->examine_and_add_content (audio);
173         BOOST_REQUIRE (!wait_for_jobs());
174         make_and_verify_dcp (ov);
175
176         /* Make the VF */
177         auto vf = new_test_film ("vf_test3_vf");
178         vf->set_name ("vf_test3_vf");
179         vf->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
180         vf->set_reel_type (ReelType::BY_VIDEO_CONTENT);
181         auto dcp = make_shared<DCPContent>(ov->dir(ov->dcp_name()));
182         BOOST_REQUIRE (dcp);
183         dcp->set_trim_start (ContentTime::from_seconds (1));
184         dcp->set_trim_end (ContentTime::from_seconds (1));
185         vf->examine_and_add_content (dcp);
186         BOOST_REQUIRE (!wait_for_jobs());
187         dcp->set_reference_video (true);
188         dcp->set_reference_audio (true);
189         make_and_verify_dcp (vf, {dcp::VerificationNote::Code::EXTERNAL_ASSET});
190
191         dcp::DCP vf_c (vf->dir(vf->dcp_name()));
192         vf_c.read ();
193         BOOST_REQUIRE_EQUAL (vf_c.cpls().size(), 1U);
194         BOOST_REQUIRE_EQUAL (vf_c.cpls()[0]->reels().size(), 1U);
195         BOOST_REQUIRE (vf_c.cpls()[0]->reels()[0]->main_picture());
196         BOOST_CHECK_EQUAL (vf_c.cpls()[0]->reels()[0]->main_picture()->entry_point().get_value_or(0), 24);
197         BOOST_CHECK_EQUAL (vf_c.cpls()[0]->reels()[0]->main_picture()->actual_duration(), 72);
198         BOOST_REQUIRE (vf_c.cpls()[0]->reels()[0]->main_sound());
199         BOOST_CHECK_EQUAL (vf_c.cpls()[0]->reels()[0]->main_sound()->entry_point().get_value_or(0), 24);
200         BOOST_CHECK_EQUAL (vf_c.cpls()[0]->reels()[0]->main_sound()->actual_duration(), 72);
201 }
202
203
204 /** Make a OV with video and audio and a VF referencing the OV and adding some more video */
205 BOOST_AUTO_TEST_CASE (vf_test4)
206 {
207         /* Make the OV */
208         auto ov = new_test_film ("vf_test4_ov");
209         ov->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
210         ov->set_name ("vf_test4_ov");
211         auto video = content_factory("test/data/flat_red.png").front();
212         ov->examine_and_add_content (video);
213         BOOST_REQUIRE (!wait_for_jobs());
214         video->video->set_length (24 * 5);
215         auto audio = content_factory("test/data/white.wav").front();
216         ov->examine_and_add_content (audio);
217         BOOST_REQUIRE (!wait_for_jobs());
218         make_and_verify_dcp (ov);
219
220         /* Make the VF */
221         auto vf = new_test_film ("vf_test4_vf");
222         vf->set_name ("vf_test4_vf");
223         vf->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
224         vf->set_reel_type (ReelType::BY_VIDEO_CONTENT);
225         vf->set_sequence (false);
226         auto dcp = make_shared<DCPContent>(ov->dir(ov->dcp_name()));
227         BOOST_REQUIRE (dcp);
228         vf->examine_and_add_content (dcp);
229         BOOST_REQUIRE (!wait_for_jobs());
230         dcp->set_position(vf, DCPTime::from_seconds(10));
231         dcp->set_reference_video (true);
232         dcp->set_reference_audio (true);
233         auto more_video = content_factory("test/data/flat_red.png").front();
234         vf->examine_and_add_content (more_video);
235         BOOST_REQUIRE (!wait_for_jobs());
236         more_video->set_position (vf, DCPTime());
237         vf->write_metadata ();
238         make_and_verify_dcp (vf, {dcp::VerificationNote::Code::EXTERNAL_ASSET});
239
240         dcp::DCP ov_c (ov->dir(ov->dcp_name()));
241         ov_c.read ();
242         BOOST_REQUIRE_EQUAL (ov_c.cpls().size(), 1U);
243         BOOST_REQUIRE_EQUAL (ov_c.cpls()[0]->reels().size(), 1U);
244         BOOST_REQUIRE (ov_c.cpls()[0]->reels()[0]->main_picture());
245         string const pic_id = ov_c.cpls()[0]->reels()[0]->main_picture()->id();
246         BOOST_REQUIRE (ov_c.cpls()[0]->reels()[0]->main_sound());
247         string const sound_id = ov_c.cpls()[0]->reels()[0]->main_sound()->id();
248         BOOST_REQUIRE (!ov_c.cpls()[0]->reels()[0]->main_subtitle());
249
250         dcp::DCP vf_c (vf->dir (vf->dcp_name ()));
251         vf_c.read ();
252         BOOST_REQUIRE_EQUAL (vf_c.cpls().size(), 1U);
253         BOOST_REQUIRE_EQUAL (vf_c.cpls()[0]->reels().size(), 2U);
254         BOOST_REQUIRE (vf_c.cpls()[0]->reels().back()->main_picture());
255         BOOST_CHECK_EQUAL (vf_c.cpls()[0]->reels().back()->main_picture()->id(), pic_id);
256         BOOST_REQUIRE (vf_c.cpls()[0]->reels().back()->main_sound());
257         BOOST_CHECK_EQUAL (vf_c.cpls()[0]->reels().back()->main_sound()->id(), sound_id);
258 }
259
260
261 /** Test bug #1495 */
262 BOOST_AUTO_TEST_CASE (vf_test5)
263 {
264         /* Make the OV */
265         auto ov = new_test_film ("vf_test5_ov");
266         ov->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
267         ov->set_reel_type (ReelType::BY_VIDEO_CONTENT);
268         for (int i = 0; i < 3; ++i) {
269                 auto video = content_factory("test/data/flat_red.png").front();
270                 ov->examine_and_add_content (video);
271                 BOOST_REQUIRE (!wait_for_jobs());
272                 video->video->set_length (24 * 10);
273         }
274
275         BOOST_REQUIRE (!wait_for_jobs());
276         make_and_verify_dcp (ov);
277
278         /* Make the VF */
279         auto vf = new_test_film ("vf_test5_vf");
280         vf->set_name ("vf_test5_vf");
281         vf->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
282         vf->set_reel_type (ReelType::BY_VIDEO_CONTENT);
283         vf->set_sequence (false);
284         auto dcp = make_shared<DCPContent>(ov->dir(ov->dcp_name()));
285         BOOST_REQUIRE (dcp);
286         vf->examine_and_add_content (dcp);
287         BOOST_REQUIRE (!wait_for_jobs());
288         dcp->set_reference_video (true);
289         dcp->set_reference_audio (true);
290         dcp->set_trim_end (ContentTime::from_seconds(15));
291         make_and_verify_dcp (vf, {dcp::VerificationNote::Code::EXTERNAL_ASSET});
292
293         /* Check that the selected reel assets are right */
294         auto player = make_shared<Player>(vf, Image::Alignment::COMPACT);
295         auto a = player->get_reel_assets();
296         BOOST_REQUIRE_EQUAL (a.size(), 4U);
297         auto i = a.begin();
298         BOOST_CHECK (i->period == DCPTimePeriod(DCPTime(0), DCPTime(960000)));
299         ++i;
300         BOOST_CHECK (i->period == DCPTimePeriod(DCPTime(0), DCPTime(960000)));
301         ++i;
302         BOOST_CHECK (i->period == DCPTimePeriod(DCPTime(960000), DCPTime(1440000)));
303         ++i;
304         BOOST_CHECK (i->period == DCPTimePeriod(DCPTime(960000), DCPTime(1440000)));
305         ++i;
306 }
307
308
309 /** Test bug #1528 */
310 BOOST_AUTO_TEST_CASE (vf_test6)
311 {
312         /* Make the OV */
313         auto ov = new_test_film ("vf_test6_ov");
314         ov->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
315         ov->set_reel_type (ReelType::BY_VIDEO_CONTENT);
316         auto video = content_factory("test/data/flat_red.png").front();
317         ov->examine_and_add_content (video);
318         BOOST_REQUIRE (!wait_for_jobs());
319         video->video->set_length (24 * 10);
320         make_and_verify_dcp (ov);
321
322         /* Make the VF */
323         auto vf = new_test_film ("vf_test6_vf");
324         vf->set_name ("vf_test6_vf");
325         vf->set_dcp_content_type (DCPContentType::from_isdcf_name("TST"));
326         vf->set_reel_type (ReelType::BY_VIDEO_CONTENT);
327         vf->set_sequence (false);
328         auto dcp = make_shared<DCPContent>(ov->dir(ov->dcp_name()));
329         vf->examine_and_add_content (dcp);
330         BOOST_REQUIRE (!wait_for_jobs());
331         dcp->set_reference_video (true);
332         dcp->set_reference_audio (true);
333
334         auto sub = content_factory("test/data/15s.srt").front();
335         vf->examine_and_add_content (sub);
336         BOOST_REQUIRE (!wait_for_jobs());
337
338         make_and_verify_dcp (
339                 vf,
340                 {
341                         dcp::VerificationNote::Code::EXTERNAL_ASSET,
342                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
343                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME
344                 });
345 }
346
347
348 /** Test bug #1643 (the second part; referring fails if there are gaps) */
349 BOOST_AUTO_TEST_CASE (vf_test7)
350 {
351         /* First OV */
352         auto ov1 = new_test_film2 ("vf_test7_ov1", {content_factory("test/data/flat_red.png").front()});
353         ov1->set_video_frame_rate (24);
354         make_and_verify_dcp (ov1);
355
356         /* Second OV */
357         auto ov2 = new_test_film2 ("vf_test7_ov2", {content_factory("test/data/flat_red.png").front()});
358         ov2->set_video_frame_rate (24);
359         make_and_verify_dcp (ov2);
360
361         /* VF */
362         auto ov1_dcp = make_shared<DCPContent>(ov1->dir(ov1->dcp_name()));
363         auto ov2_dcp = make_shared<DCPContent>(ov2->dir(ov2->dcp_name()));
364         auto vf = new_test_film2 ("vf_test7_vf", {ov1_dcp, ov2_dcp});
365         vf->set_reel_type (ReelType::BY_VIDEO_CONTENT);
366         ov1_dcp->set_reference_video (true);
367         ov2_dcp->set_reference_video (true);
368         ov1_dcp->set_position (vf, DCPTime::from_seconds(1));
369         ov2_dcp->set_position (vf, DCPTime::from_seconds(20));
370         vf->write_metadata ();
371         make_and_verify_dcp (vf);
372 }
373
374
375 /** Test bug #2116 */
376 BOOST_AUTO_TEST_CASE (test_vf_with_trimmed_multi_reel_dcp)
377 {
378         /* Make an OV with 3 reels */
379         std::vector<std::shared_ptr<Content>> ov_content;
380         for (int i = 0; i < 3; ++i) {
381                 auto c = content_factory("test/data/flat_red.png").front();
382                 c->video->set_length(240);
383                 ov_content.push_back(c);
384         }
385         auto ov = new_test_film2 ("test_vf_with_trimmed_multi_reel_dcp_ov", ov_content);
386         ov->set_reel_type(ReelType::BY_VIDEO_CONTENT);
387         make_and_verify_dcp (ov);
388
389         /* Make a VF with a specific arrangement */
390         auto vf_image = content_factory("test/data/flat_red.png").front();
391         auto vf_dcp = make_shared<DCPContent>(ov->dir(ov->dcp_name()));
392         auto vf = new_test_film2 ("test_vf_with_trimmed_multi_reel_dcp_vf", { vf_image, vf_dcp });
393         vf->set_reel_type(ReelType::BY_VIDEO_CONTENT);
394         vf_dcp->set_reference_video(true);
395         vf_dcp->set_reference_audio(true);
396         vf_dcp->set_trim_start(ContentTime::from_seconds(10));
397         vf_dcp->set_position(vf, DCPTime::from_seconds(10));
398         make_and_verify_dcp (vf, { dcp::VerificationNote::Code::EXTERNAL_ASSET });
399 }
400