d705d8769c528877814a684dfe43dca7677ca91f
[dcpomatic.git] / test / reels_test.cc
1 /*
2     Copyright (C) 2015-2016 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/reels_test.cc
22  *  @brief Check manipulation of reels in various ways.
23  *  @ingroup specific
24  */
25
26 #include "lib/film.h"
27 #include "lib/ratio.h"
28 #include "lib/ffmpeg_content.h"
29 #include "lib/image_content.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/dcp_content.h"
32 #include "lib/video_content.h"
33 #include "lib/text_subtitle_content.h"
34 #include "lib/content_factory.h"
35 #include "test.h"
36 #include <boost/test/unit_test.hpp>
37 #include <boost/foreach.hpp>
38
39 using std::list;
40 using std::cout;
41 using boost::shared_ptr;
42
43 /** Test Film::reels() */
44 BOOST_AUTO_TEST_CASE (reels_test1)
45 {
46         shared_ptr<Film> film = new_test_film ("reels_test1");
47         film->set_container (Ratio::from_id ("185"));
48         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test.mp4"));
49         film->examine_and_add_content (A);
50         shared_ptr<FFmpegContent> B (new FFmpegContent (film, "test/data/test.mp4"));
51         film->examine_and_add_content (B);
52         wait_for_jobs ();
53         BOOST_CHECK_EQUAL (A->full_length().get(), 288000);
54
55         film->set_reel_type (REELTYPE_SINGLE);
56         list<DCPTimePeriod> r = film->reels ();
57         BOOST_CHECK_EQUAL (r.size(), 1);
58         BOOST_CHECK_EQUAL (r.front().from.get(), 0);
59         BOOST_CHECK_EQUAL (r.front().to.get(), 288000 * 2);
60
61         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
62         r = film->reels ();
63         BOOST_CHECK_EQUAL (r.size(), 2);
64         BOOST_CHECK_EQUAL (r.front().from.get(), 0);
65         BOOST_CHECK_EQUAL (r.front().to.get(), 288000);
66         BOOST_CHECK_EQUAL (r.back().from.get(), 288000);
67         BOOST_CHECK_EQUAL (r.back().to.get(), 288000 * 2);
68
69         film->set_j2k_bandwidth (100000000);
70         film->set_reel_type (REELTYPE_BY_LENGTH);
71         /* This is just over 2.5s at 100Mbit/s; should correspond to 60 frames */
72         film->set_reel_length (31253154);
73         r = film->reels ();
74         BOOST_CHECK_EQUAL (r.size(), 3);
75         list<DCPTimePeriod>::const_iterator i = r.begin ();
76         BOOST_CHECK_EQUAL (i->from.get(), 0);
77         BOOST_CHECK_EQUAL (i->to.get(), DCPTime::from_frames(60, 24).get());
78         ++i;
79         BOOST_CHECK_EQUAL (i->from.get(), DCPTime::from_frames(60, 24).get());
80         BOOST_CHECK_EQUAL (i->to.get(), DCPTime::from_frames(120, 24).get());
81         ++i;
82         BOOST_CHECK_EQUAL (i->from.get(), DCPTime::from_frames(120, 24).get());
83         BOOST_CHECK_EQUAL (i->to.get(), DCPTime::from_frames(144, 24).get());
84 }
85
86 /** Make a short DCP with multi reels split by video content, then import
87  *  this into a new project and make a new DCP referencing it.
88  */
89 BOOST_AUTO_TEST_CASE (reels_test2)
90 {
91         shared_ptr<Film> film = new_test_film ("reels_test2");
92         film->set_name ("reels_test2");
93         film->set_container (Ratio::from_id ("185"));
94         film->set_interop (false);
95         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
96
97         {
98                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_red.png"));
99                 film->examine_and_add_content (c);
100                 wait_for_jobs ();
101                 c->video->set_length (24);
102         }
103
104         {
105                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_green.png"));
106                 film->examine_and_add_content (c);
107                 wait_for_jobs ();
108                 c->video->set_length (24);
109         }
110
111         {
112                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_blue.png"));
113                 film->examine_and_add_content (c);
114                 wait_for_jobs ();
115                 c->video->set_length (24);
116         }
117
118         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
119         wait_for_jobs ();
120
121         film->make_dcp ();
122         wait_for_jobs ();
123
124         check_dcp ("test/data/reels_test2", film->dir (film->dcp_name()));
125
126         shared_ptr<Film> film2 = new_test_film ("reels_test2b");
127         film2->set_name ("reels_test2b");
128         film2->set_container (Ratio::from_id ("185"));
129         film2->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
130         film2->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
131
132         shared_ptr<DCPContent> c (new DCPContent (film2, film->dir (film->dcp_name ())));
133         film2->examine_and_add_content (c);
134         BOOST_REQUIRE (!wait_for_jobs ());
135
136         list<DCPTimePeriod> r = film2->reels ();
137         BOOST_CHECK_EQUAL (r.size(), 3);
138         list<DCPTimePeriod>::const_iterator i = r.begin ();
139         BOOST_CHECK_EQUAL (i->from.get(), 0);
140         BOOST_CHECK_EQUAL (i->to.get(), 96000);
141         ++i;
142         BOOST_CHECK_EQUAL (i->from.get(), 96000);
143         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 2);
144         ++i;
145         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 2);
146         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 3);
147
148         c->set_reference_video (true);
149         c->set_reference_audio (true);
150
151         film2->make_dcp ();
152         wait_for_jobs ();
153 }
154
155 /** Check that REELTYPE_BY_VIDEO_CONTENT adds an extra reel, if necessary, at the end
156  *  of all the video content to mop up anything afterward.
157  */
158 BOOST_AUTO_TEST_CASE (reels_test3)
159 {
160         shared_ptr<Film> film = new_test_film ("reels_test3");
161         film->set_name ("reels_test3");
162         film->set_container (Ratio::from_id ("185"));
163         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
164         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
165
166         shared_ptr<Content> dcp (new DCPContent (film, "test/data/reels_test2"));
167         film->examine_and_add_content (dcp);
168         shared_ptr<Content> sub (new TextSubtitleContent (film, "test/data/subrip.srt"));
169         film->examine_and_add_content (sub);
170         wait_for_jobs ();
171
172         list<DCPTimePeriod> reels = film->reels();
173         BOOST_REQUIRE_EQUAL (reels.size(), 4);
174         list<DCPTimePeriod>::const_iterator i = reels.begin ();
175         BOOST_CHECK_EQUAL (i->from.get(), 0);
176         BOOST_CHECK_EQUAL (i->to.get(), 96000);
177         ++i;
178         BOOST_CHECK_EQUAL (i->from.get(), 96000);
179         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 2);
180         ++i;
181         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 2);
182         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 3);
183         ++i;
184         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 3);
185         BOOST_CHECK_EQUAL (i->to.get(), sub->full_length().ceil(film->video_frame_rate()).get());
186 }
187
188 /** Check creation of a multi-reel DCP with a single .srt subtitle file;
189  *  make sure that the reel subtitle timing is done right.
190  */
191 BOOST_AUTO_TEST_CASE (reels_test4)
192 {
193         shared_ptr<Film> film = new_test_film ("reels_test4");
194         film->set_name ("reels_test4");
195         film->set_container (Ratio::from_id ("185"));
196         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
197         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
198         film->set_interop (false);
199
200         /* 4 piece of 1s-long content */
201         shared_ptr<ImageContent> content[4];
202         for (int i = 0; i < 4; ++i) {
203                 content[i].reset (new ImageContent (film, "test/data/flat_green.png"));
204                 film->examine_and_add_content (content[i]);
205                 wait_for_jobs ();
206                 content[i]->video->set_length (24);
207         }
208
209         shared_ptr<TextSubtitleContent> subs (new TextSubtitleContent (film, "test/data/subrip3.srt"));
210         film->examine_and_add_content (subs);
211         wait_for_jobs ();
212
213         list<DCPTimePeriod> reels = film->reels();
214         BOOST_REQUIRE_EQUAL (reels.size(), 4);
215         list<DCPTimePeriod>::const_iterator i = reels.begin ();
216         BOOST_CHECK_EQUAL (i->from.get(), 0);
217         BOOST_CHECK_EQUAL (i->to.get(), 96000);
218         ++i;
219         BOOST_CHECK_EQUAL (i->from.get(), 96000);
220         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 2);
221         ++i;
222         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 2);
223         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 3);
224         ++i;
225         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 3);
226         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 4);
227
228         film->make_dcp ();
229         wait_for_jobs ();
230
231         check_dcp ("test/data/reels_test4", film->dir (film->dcp_name()));
232 }
233
234 BOOST_AUTO_TEST_CASE (reels_test5)
235 {
236         shared_ptr<Film> film = new_test_film ("reels_test5");
237         film->set_sequence (false);
238         shared_ptr<DCPContent> dcp (new DCPContent (film, "test/data/reels_test4"));
239         film->examine_and_add_content (dcp);
240         BOOST_REQUIRE (!wait_for_jobs ());
241
242         /* Set to 2123 but it will be rounded up to the next frame (4000) */
243         dcp->set_position(DCPTime(2123));
244
245         {
246                 list<DCPTimePeriod> p = dcp->reels ();
247                 BOOST_REQUIRE_EQUAL (p.size(), 4);
248                 list<DCPTimePeriod>::const_iterator i = p.begin();
249                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 0), DCPTime(4000 + 96000)));
250                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 96000), DCPTime(4000 + 192000)));
251                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 192000), DCPTime(4000 + 288000)));
252                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 288000), DCPTime(4000 + 384000)));
253         }
254
255         {
256                 dcp->set_trim_start (ContentTime::from_seconds (0.5));
257                 list<DCPTimePeriod> p = dcp->reels ();
258                 BOOST_REQUIRE_EQUAL (p.size(), 4);
259                 list<DCPTimePeriod>::const_iterator i = p.begin();
260                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 0), DCPTime(4000 + 48000)));
261                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 48000), DCPTime(4000 + 144000)));
262                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 144000), DCPTime(4000 + 240000)));
263                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 240000), DCPTime(4000 + 336000)));
264         }
265
266         {
267                 dcp->set_trim_end (ContentTime::from_seconds (0.5));
268                 list<DCPTimePeriod> p = dcp->reels ();
269                 BOOST_REQUIRE_EQUAL (p.size(), 4);
270                 list<DCPTimePeriod>::const_iterator i = p.begin();
271                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 0), DCPTime(4000 + 48000)));
272                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 48000), DCPTime(4000 + 144000)));
273                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 144000), DCPTime(4000 + 240000)));
274                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 240000), DCPTime(4000 + 288000)));
275         }
276
277         {
278                 dcp->set_trim_start (ContentTime::from_seconds (1.5));
279                 list<DCPTimePeriod> p = dcp->reels ();
280                 BOOST_REQUIRE_EQUAL (p.size(), 3);
281                 list<DCPTimePeriod>::const_iterator i = p.begin();
282                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 0), DCPTime(4000 + 48000)));
283                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 48000), DCPTime(4000 + 144000)));
284                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(4000 + 144000), DCPTime(4000 + 192000)));
285         }
286 }
287
288 /** Check reel split with a muxed video/audio source */
289 BOOST_AUTO_TEST_CASE (reels_test6)
290 {
291         shared_ptr<Film> film = new_test_film ("reels_test6");
292         film->set_name ("reels_test6");
293         film->set_container (Ratio::from_id ("185"));
294         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
295         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test2.mp4"));
296         film->examine_and_add_content (A);
297         BOOST_REQUIRE (!wait_for_jobs ());
298
299         film->set_j2k_bandwidth (100000000);
300         film->set_reel_type (REELTYPE_BY_LENGTH);
301         /* This is just over 2.5s at 100Mbit/s; should correspond to 60 frames */
302         film->set_reel_length (31253154);
303         film->make_dcp ();
304         BOOST_REQUIRE (!wait_for_jobs ());
305 }
306
307 /** Check the case where the last bit of audio hangs over the end of the video
308  *  and we are using REELTYPE_BY_VIDEO_CONTENT.
309  */
310 BOOST_AUTO_TEST_CASE (reels_test7)
311 {
312         shared_ptr<Film> film = new_test_film ("reels_test7");
313         film->set_name ("reels_test7");
314         film->set_container (Ratio::from_id ("185"));
315         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
316         shared_ptr<Content> A = content_factory(film, "test/data/flat_red.png").front();
317         film->examine_and_add_content (A);
318         BOOST_REQUIRE (!wait_for_jobs ());
319         shared_ptr<Content> B = content_factory(film, "test/data/awkward_length.wav").front();
320         film->examine_and_add_content (B);
321         BOOST_REQUIRE (!wait_for_jobs ());
322         film->set_video_frame_rate (24);
323         A->video->set_length (3 * 24);
324
325         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
326         BOOST_REQUIRE_EQUAL (film->reels().size(), 2);
327         BOOST_CHECK (film->reels().front() == DCPTimePeriod(DCPTime(0), DCPTime::from_frames(3 * 24, 24)));
328         BOOST_CHECK (film->reels().back() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
329
330         film->make_dcp ();
331         BOOST_REQUIRE (!wait_for_jobs ());
332 }
333
334 /** Check a reels-related error; make_dcp() would raise a ProgrammingError */
335 BOOST_AUTO_TEST_CASE (reels_test8)
336 {
337         shared_ptr<Film> film = new_test_film ("reels_test8");
338         film->set_name ("reels_test8");
339         film->set_container (Ratio::from_id ("185"));
340         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
341         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test2.mp4"));
342         film->examine_and_add_content (A);
343         BOOST_REQUIRE (!wait_for_jobs ());
344
345         A->set_trim_end (ContentTime::from_seconds (1));
346         film->make_dcp ();
347         BOOST_REQUIRE (!wait_for_jobs ());
348 }
349
350 /** Check another reels-related error; make_dcp() would raise a ProgrammingError */
351 BOOST_AUTO_TEST_CASE (reels_test9)
352 {
353         shared_ptr<Film> film = new_test_film2("reels_test9a");
354         shared_ptr<FFmpegContent> A(new FFmpegContent(film, "test/data/flat_red.png"));
355         film->examine_and_add_content(A);
356         BOOST_REQUIRE(!wait_for_jobs());
357         A->video->set_length(5 * 24);
358         film->set_video_frame_rate(24);
359         film->make_dcp();
360         BOOST_REQUIRE(!wait_for_jobs());
361
362         shared_ptr<Film> film2 = new_test_film2("reels_test9b");
363         shared_ptr<DCPContent> B(new DCPContent(film2, film->dir(film->dcp_name())));
364         film2->examine_and_add_content(B);
365         film2->examine_and_add_content(content_factory(film, "test/data/dcp_sub4.xml").front());
366         B->set_reference_video(true);
367         B->set_reference_audio(true);
368         BOOST_REQUIRE(!wait_for_jobs());
369         film2->set_reel_type(REELTYPE_BY_VIDEO_CONTENT);
370         film2->write_metadata();
371         film2->make_dcp();
372         BOOST_REQUIRE(!wait_for_jobs());
373 }