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