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