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