Rename Time::round_up to Time::ceil.
[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 #include "lib/film.h"
22 #include "lib/ratio.h"
23 #include "lib/ffmpeg_content.h"
24 #include "lib/image_content.h"
25 #include "lib/dcp_content_type.h"
26 #include "lib/dcp_content.h"
27 #include "lib/video_content.h"
28 #include "lib/text_subtitle_content.h"
29 #include "test.h"
30 #include <boost/test/unit_test.hpp>
31 #include <boost/foreach.hpp>
32
33 using std::list;
34 using boost::shared_ptr;
35
36 /** Test Film::reels() */
37 BOOST_AUTO_TEST_CASE (reels_test1)
38 {
39         shared_ptr<Film> film = new_test_film ("reels_test1");
40         film->set_container (Ratio::from_id ("185"));
41         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test.mp4"));
42         film->examine_and_add_content (A);
43         shared_ptr<FFmpegContent> B (new FFmpegContent (film, "test/data/test.mp4"));
44         film->examine_and_add_content (B);
45         wait_for_jobs ();
46         BOOST_CHECK_EQUAL (A->full_length().get(), 288000);
47
48         film->set_reel_type (REELTYPE_SINGLE);
49         list<DCPTimePeriod> r = film->reels ();
50         BOOST_CHECK_EQUAL (r.size(), 1);
51         BOOST_CHECK_EQUAL (r.front().from.get(), 0);
52         BOOST_CHECK_EQUAL (r.front().to.get(), 288000 * 2);
53
54         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
55         r = film->reels ();
56         BOOST_CHECK_EQUAL (r.size(), 2);
57         BOOST_CHECK_EQUAL (r.front().from.get(), 0);
58         BOOST_CHECK_EQUAL (r.front().to.get(), 288000);
59         BOOST_CHECK_EQUAL (r.back().from.get(), 288000);
60         BOOST_CHECK_EQUAL (r.back().to.get(), 288000 * 2);
61
62         film->set_j2k_bandwidth (100000000);
63         film->set_reel_type (REELTYPE_BY_LENGTH);
64         /* This is just over 2.5s at 100Mbit/s; should correspond to 60 frames */
65         film->set_reel_length (31253154);
66         r = film->reels ();
67         BOOST_CHECK_EQUAL (r.size(), 3);
68         list<DCPTimePeriod>::const_iterator i = r.begin ();
69         BOOST_CHECK_EQUAL (i->from.get(), 0);
70         BOOST_CHECK_EQUAL (i->to.get(), DCPTime::from_frames(60, 24).get());
71         ++i;
72         BOOST_CHECK_EQUAL (i->from.get(), DCPTime::from_frames(60, 24).get());
73         BOOST_CHECK_EQUAL (i->to.get(), DCPTime::from_frames(120, 24).get());
74         ++i;
75         BOOST_CHECK_EQUAL (i->from.get(), DCPTime::from_frames(120, 24).get());
76         BOOST_CHECK_EQUAL (i->to.get(), DCPTime::from_frames(144, 24).get());
77 }
78
79 /** Make a short DCP with multi reels split by video content, then import
80  *  this into a new project and make a new DCP referencing it.
81  */
82 BOOST_AUTO_TEST_CASE (reels_test2)
83 {
84         shared_ptr<Film> film = new_test_film ("reels_test2");
85         film->set_name ("reels_test2");
86         film->set_container (Ratio::from_id ("185"));
87         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
88
89         {
90                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_red.png"));
91                 film->examine_and_add_content (c);
92                 wait_for_jobs ();
93                 c->video->set_length (24);
94         }
95
96         {
97                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_green.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_blue.png"));
105                 film->examine_and_add_content (c);
106                 wait_for_jobs ();
107                 c->video->set_length (24);
108         }
109
110         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
111         wait_for_jobs ();
112
113         film->make_dcp ();
114         wait_for_jobs ();
115
116         check_dcp ("test/data/reels_test2", film->dir (film->dcp_name()));
117
118         shared_ptr<Film> film2 = new_test_film ("reels_test2b");
119         film2->set_name ("reels_test2b");
120         film2->set_container (Ratio::from_id ("185"));
121         film2->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
122         film2->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
123
124         shared_ptr<DCPContent> c (new DCPContent (film2, film->dir (film->dcp_name ())));
125         film2->examine_and_add_content (c);
126         wait_for_jobs ();
127
128         list<DCPTimePeriod> r = film2->reels ();
129         BOOST_CHECK_EQUAL (r.size(), 3);
130         list<DCPTimePeriod>::const_iterator i = r.begin ();
131         BOOST_CHECK_EQUAL (i->from.get(), 0);
132         BOOST_CHECK_EQUAL (i->to.get(), 96000);
133         ++i;
134         BOOST_CHECK_EQUAL (i->from.get(), 96000);
135         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 2);
136         ++i;
137         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 2);
138         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 3);
139
140         c->set_reference_video (true);
141         c->set_reference_audio (true);
142
143         film2->make_dcp ();
144         wait_for_jobs ();
145 }
146
147 /** Check that REELTYPE_BY_VIDEO_CONTENT adds an extra reel, if necessary, at the end
148  *  of all the video content to mop up anything afterward.
149  */
150 BOOST_AUTO_TEST_CASE (reels_test3)
151 {
152         shared_ptr<Film> film = new_test_film ("reels_test3");
153         film->set_name ("reels_test3");
154         film->set_container (Ratio::from_id ("185"));
155         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
156         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
157
158         shared_ptr<Content> dcp (new DCPContent (film, "test/data/reels_test2"));
159         film->examine_and_add_content (dcp);
160         shared_ptr<Content> sub (new TextSubtitleContent (film, "test/data/subrip.srt"));
161         film->examine_and_add_content (sub);
162         wait_for_jobs ();
163
164         list<DCPTimePeriod> reels = film->reels();
165         BOOST_REQUIRE_EQUAL (reels.size(), 4);
166         list<DCPTimePeriod>::const_iterator i = reels.begin ();
167         BOOST_CHECK_EQUAL (i->from.get(), 0);
168         BOOST_CHECK_EQUAL (i->to.get(), 96000);
169         ++i;
170         BOOST_CHECK_EQUAL (i->from.get(), 96000);
171         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 2);
172         ++i;
173         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 2);
174         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 3);
175         ++i;
176         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 3);
177         BOOST_CHECK_EQUAL (i->to.get(), sub->full_length().ceil(film->video_frame_rate()).get());
178 }
179
180 /** Check creation of a multi-reel DCP with a single .srt subtitle file;
181  *  make sure that the reel subtitle timing is done right.
182  */
183 BOOST_AUTO_TEST_CASE (reels_test4)
184 {
185         shared_ptr<Film> film = new_test_film ("reels_test4");
186         film->set_name ("reels_test4");
187         film->set_container (Ratio::from_id ("185"));
188         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
189         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
190
191         /* 4 piece of 1s-long content */
192         shared_ptr<ImageContent> content[4];
193         for (int i = 0; i < 4; ++i) {
194                 content[i].reset (new ImageContent (film, "test/data/flat_green.png"));
195                 film->examine_and_add_content (content[i]);
196                 wait_for_jobs ();
197                 content[i]->video->set_length (24);
198         }
199
200         shared_ptr<TextSubtitleContent> subs (new TextSubtitleContent (film, "test/data/subrip3.srt"));
201         film->examine_and_add_content (subs);
202         wait_for_jobs ();
203
204         list<DCPTimePeriod> reels = film->reels();
205         BOOST_REQUIRE_EQUAL (reels.size(), 4);
206         list<DCPTimePeriod>::const_iterator i = reels.begin ();
207         BOOST_CHECK_EQUAL (i->from.get(), 0);
208         BOOST_CHECK_EQUAL (i->to.get(), 96000);
209         ++i;
210         BOOST_CHECK_EQUAL (i->from.get(), 96000);
211         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 2);
212         ++i;
213         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 2);
214         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 3);
215         ++i;
216         BOOST_CHECK_EQUAL (i->from.get(), 96000 * 3);
217         BOOST_CHECK_EQUAL (i->to.get(), 96000 * 4);
218
219         film->make_dcp ();
220         wait_for_jobs ();
221
222         check_dcp ("test/data/reels_test4", film->dir (film->dcp_name()));
223 }
224
225 BOOST_AUTO_TEST_CASE (reels_test5)
226 {
227         shared_ptr<Film> film = new_test_film ("reels_test4");
228         shared_ptr<DCPContent> dcp (new DCPContent (film, "test/data/reels_test4"));
229         film->examine_and_add_content (dcp);
230         wait_for_jobs ();
231
232         dcp->set_position(DCPTime(123));
233
234         {
235                 list<DCPTimePeriod> p = dcp->reels ();
236                 BOOST_REQUIRE_EQUAL (p.size(), 4);
237                 list<DCPTimePeriod>::const_iterator i = p.begin();
238                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 0), DCPTime(123 + 96000)));
239                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 96000), DCPTime(123 + 192000)));
240                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 192000), DCPTime(123 + 288000)));
241                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 288000), DCPTime(123 + 384000)));
242         }
243
244         {
245                 dcp->set_trim_start (ContentTime::from_seconds (0.5));
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(123 + 0), DCPTime(123 + 48000)));
250                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 48000), DCPTime(123 + 144000)));
251                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 144000), DCPTime(123 + 240000)));
252                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 240000), DCPTime(123 + 336000)));
253         }
254
255         {
256                 dcp->set_trim_end (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(123 + 0), DCPTime(123 + 48000)));
261                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 48000), DCPTime(123 + 144000)));
262                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 144000), DCPTime(123 + 240000)));
263                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 240000), DCPTime(123 + 288000)));
264         }
265
266         {
267                 dcp->set_trim_start (ContentTime::from_seconds (1.5));
268                 list<DCPTimePeriod> p = dcp->reels ();
269                 BOOST_REQUIRE_EQUAL (p.size(), 3);
270                 list<DCPTimePeriod>::const_iterator i = p.begin();
271                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 0), DCPTime(123 + 48000)));
272                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 48000), DCPTime(123 + 144000)));
273                 BOOST_CHECK (*i++ == DCPTimePeriod (DCPTime(123 + 144000), DCPTime(123 + 192000)));
274         }
275 }