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