Don't emit black to fill up to a frame that we're not going to emit.
[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_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
95
96         {
97                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_red.png"));
98                 film->examine_and_add_content (c);
99                 wait_for_jobs ();
100                 c->video->set_length (24);
101         }
102
103         {
104                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_green.png"));
105                 film->examine_and_add_content (c);
106                 wait_for_jobs ();
107                 c->video->set_length (24);
108         }
109
110         {
111                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_blue.png"));
112                 film->examine_and_add_content (c);
113                 wait_for_jobs ();
114                 c->video->set_length (24);
115         }
116
117         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
118         wait_for_jobs ();
119
120         film->make_dcp ();
121         wait_for_jobs ();
122
123         check_dcp ("test/data/reels_test2", film->dir (film->dcp_name()));
124
125         shared_ptr<Film> film2 = new_test_film ("reels_test2b");
126         film2->set_name ("reels_test2b");
127         film2->set_container (Ratio::from_id ("185"));
128         film2->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
129         film2->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
130
131         shared_ptr<DCPContent> c (new DCPContent (film2, film->dir (film->dcp_name ())));
132         film2->examine_and_add_content (c);
133         BOOST_REQUIRE (!wait_for_jobs ());
134
135         list<DCPTimePeriod> r = film2->reels ();
136         BOOST_CHECK_EQUAL (r.size(), 3);
137         list<DCPTimePeriod>::const_iterator i = r.begin ();
138         BOOST_CHECK_EQUAL (i->from.get(), 0);
139         BOOST_CHECK_EQUAL (i->to.get(), 96000);
140         ++i;
141         BOOST_CHECK_EQUAL (i->from.get(), 96000);
142         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 2);
143         ++i;
144         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 2);
145         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 3);
146
147         c->set_reference_video (true);
148         c->set_reference_audio (true);
149
150         film2->make_dcp ();
151         wait_for_jobs ();
152 }
153
154 /** Check that REELTYPE_BY_VIDEO_CONTENT adds an extra reel, if necessary, at the end
155  *  of all the video content to mop up anything afterward.
156  */
157 BOOST_AUTO_TEST_CASE (reels_test3)
158 {
159         shared_ptr<Film> film = new_test_film ("reels_test3");
160         film->set_name ("reels_test3");
161         film->set_container (Ratio::from_id ("185"));
162         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
163         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
164
165         shared_ptr<Content> dcp (new DCPContent (film, "test/data/reels_test2"));
166         film->examine_and_add_content (dcp);
167         shared_ptr<Content> sub (new TextSubtitleContent (film, "test/data/subrip.srt"));
168         film->examine_and_add_content (sub);
169         wait_for_jobs ();
170
171         list<DCPTimePeriod> reels = film->reels();
172         BOOST_REQUIRE_EQUAL (reels.size(), 4);
173         list<DCPTimePeriod>::const_iterator i = reels.begin ();
174         BOOST_CHECK_EQUAL (i->from.get(), 0);
175         BOOST_CHECK_EQUAL (i->to.get(), 96000);
176         ++i;
177         BOOST_CHECK_EQUAL (i->from.get(), 96000);
178         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 2);
179         ++i;
180         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 2);
181         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 3);
182         ++i;
183         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 3);
184         BOOST_CHECK_EQUAL (i->to.get(), sub->full_length().ceil(film->video_frame_rate()).get());
185 }
186
187 /** Check creation of a multi-reel DCP with a single .srt subtitle file;
188  *  make sure that the reel subtitle timing is done right.
189  */
190 BOOST_AUTO_TEST_CASE (reels_test4)
191 {
192         shared_ptr<Film> film = new_test_film ("reels_test4");
193         film->set_name ("reels_test4");
194         film->set_container (Ratio::from_id ("185"));
195         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
196         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
197
198         /* 4 piece of 1s-long content */
199         shared_ptr<ImageContent> content[4];
200         for (int i = 0; i < 4; ++i) {
201                 content[i].reset (new ImageContent (film, "test/data/flat_green.png"));
202                 film->examine_and_add_content (content[i]);
203                 wait_for_jobs ();
204                 content[i]->video->set_length (24);
205         }
206
207         shared_ptr<TextSubtitleContent> subs (new TextSubtitleContent (film, "test/data/subrip3.srt"));
208         film->examine_and_add_content (subs);
209         wait_for_jobs ();
210
211         list<DCPTimePeriod> reels = film->reels();
212         BOOST_REQUIRE_EQUAL (reels.size(), 4);
213         list<DCPTimePeriod>::const_iterator i = reels.begin ();
214         BOOST_CHECK_EQUAL (i->from.get(), 0);
215         BOOST_CHECK_EQUAL (i->to.get(), 96000);
216         ++i;
217         BOOST_CHECK_EQUAL (i->from.get(), 96000);
218         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 2);
219         ++i;
220         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 2);
221         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 3);
222         ++i;
223         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 3);
224         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 4);
225
226         film->make_dcp ();
227         wait_for_jobs ();
228
229         check_dcp ("test/data/reels_test4", film->dir (film->dcp_name()));
230 }
231
232 BOOST_AUTO_TEST_CASE (reels_test5)
233 {
234         shared_ptr<Film> film = new_test_film ("reels_test4");
235         shared_ptr<DCPContent> dcp (new DCPContent (film, "test/data/reels_test4"));
236         film->examine_and_add_content (dcp);
237         wait_for_jobs ();
238
239         dcp->set_position(DCPTime(123));
240
241         {
242                 list<DCPTimePeriod> p = dcp->reels ();
243                 BOOST_REQUIRE_EQUAL (p.size(), 4);
244                 list<DCPTimePeriod>::const_iterator i = p.begin();
245                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 0), DCPTime(123 + 96000)));
246                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 96000), DCPTime(123 + 192000)));
247                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 192000), DCPTime(123 + 288000)));
248                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 288000), DCPTime(123 + 384000)));
249         }
250
251         {
252                 dcp->set_trim_start (ContentTime::from_seconds (0.5));
253                 list<DCPTimePeriod> p = dcp->reels ();
254                 BOOST_REQUIRE_EQUAL (p.size(), 4);
255                 list<DCPTimePeriod>::const_iterator i = p.begin();
256                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 0), DCPTime(123 + 48000)));
257                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 48000), DCPTime(123 + 144000)));
258                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 144000), DCPTime(123 + 240000)));
259                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 240000), DCPTime(123 + 336000)));
260         }
261
262         {
263                 dcp->set_trim_end (ContentTime::from_seconds (0.5));
264                 list<DCPTimePeriod> p = dcp->reels ();
265                 BOOST_REQUIRE_EQUAL (p.size(), 4);
266                 list<DCPTimePeriod>::const_iterator i = p.begin();
267                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 0), DCPTime(123 + 48000)));
268                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 48000), DCPTime(123 + 144000)));
269                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 144000), DCPTime(123 + 240000)));
270                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 240000), DCPTime(123 + 288000)));
271         }
272
273         {
274                 dcp->set_trim_start (ContentTime::from_seconds (1.5));
275                 list<DCPTimePeriod> p = dcp->reels ();
276                 BOOST_REQUIRE_EQUAL (p.size(), 3);
277                 list<DCPTimePeriod>::const_iterator i = p.begin();
278                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 0), DCPTime(123 + 48000)));
279                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 48000), DCPTime(123 + 144000)));
280                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 144000), DCPTime(123 + 192000)));
281         }
282 }
283
284 /** Check reel split with a muxed video/audio source */
285 BOOST_AUTO_TEST_CASE (reels_test6)
286 {
287         shared_ptr<Film> film = new_test_film ("reels_test6");
288         film->set_name ("reels_test6");
289         film->set_container (Ratio::from_id ("185"));
290         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
291         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test2.mp4"));
292         film->examine_and_add_content (A);
293         BOOST_REQUIRE (!wait_for_jobs ());
294
295         film->set_j2k_bandwidth (100000000);
296         film->set_reel_type (REELTYPE_BY_LENGTH);
297         /* This is just over 2.5s at 100Mbit/s; should correspond to 60 frames */
298         film->set_reel_length (31253154);
299         film->make_dcp ();
300         BOOST_REQUIRE (!wait_for_jobs ());
301 }
302
303 /** Check the case where the last bit of audio hangs over the end of the video
304  *  and we are using REELTYPE_BY_VIDEO_CONTENT.
305  */
306 BOOST_AUTO_TEST_CASE (reels_test7)
307 {
308         shared_ptr<Film> film = new_test_film ("reels_test7");
309         film->set_name ("reels_test7");
310         film->set_container (Ratio::from_id ("185"));
311         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
312         shared_ptr<Content> A = content_factory(film, "test/data/flat_red.png").front();
313         film->examine_and_add_content (A);
314         BOOST_REQUIRE (!wait_for_jobs ());
315         shared_ptr<Content> B = content_factory(film, "test/data/awkward_length.wav").front();
316         film->examine_and_add_content (B);
317         BOOST_REQUIRE (!wait_for_jobs ());
318         film->set_video_frame_rate (24);
319         A->video->set_length (3 * 24);
320
321         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
322         BOOST_REQUIRE_EQUAL (film->reels().size(), 2);
323         BOOST_CHECK (film->reels().front() == DCPTimePeriod(DCPTime(0), DCPTime::from_frames(3 * 24, 24)));
324         BOOST_CHECK (film->reels().back() == DCPTimePeriod(DCPTime::from_frames(3 * 24, 24), DCPTime::from_frames(3 * 24 + 1, 24)));
325
326         film->make_dcp ();
327         BOOST_REQUIRE (!wait_for_jobs ());
328 }
329
330 /** Check a reels-related error; make_dcp() would raise a ProgrammingError */
331 BOOST_AUTO_TEST_CASE (reels_test8)
332 {
333         shared_ptr<Film> film = new_test_film ("reels_test8");
334         film->set_name ("reels_test8");
335         film->set_container (Ratio::from_id ("185"));
336         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
337         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test2.mp4"));
338         film->examine_and_add_content (A);
339         BOOST_REQUIRE (!wait_for_jobs ());
340
341         A->set_trim_end (ContentTime::from_seconds (1));
342         cout << to_string(A->length_after_trim()) << "\n";
343         film->make_dcp ();
344         BOOST_REQUIRE (!wait_for_jobs ());
345 }