Rename video/audio/subtitle part methods.
[dcpomatic.git] / test / reels_test.cc
1 /*
2     Copyright (C) 2015-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/film.h"
21 #include "lib/ratio.h"
22 #include "lib/ffmpeg_content.h"
23 #include "lib/image_content.h"
24 #include "lib/dcp_content_type.h"
25 #include "lib/dcp_content.h"
26 #include "lib/video_content.h"
27 #include "lib/text_subtitle_content.h"
28 #include "test.h"
29 #include <boost/test/unit_test.hpp>
30 #include <boost/foreach.hpp>
31
32 using std::list;
33 using boost::shared_ptr;
34
35 /** Test Film::reels() */
36 BOOST_AUTO_TEST_CASE (reels_test1)
37 {
38         shared_ptr<Film> film = new_test_film ("reels_test1");
39         film->set_container (Ratio::from_id ("185"));
40         shared_ptr<FFmpegContent> A (new FFmpegContent (film, "test/data/test.mp4"));
41         film->examine_and_add_content (A);
42         shared_ptr<FFmpegContent> B (new FFmpegContent (film, "test/data/test.mp4"));
43         film->examine_and_add_content (B);
44         wait_for_jobs ();
45         BOOST_CHECK_EQUAL (A->full_length(), DCPTime (288000));
46
47         film->set_reel_type (REELTYPE_SINGLE);
48         list<DCPTimePeriod> r = film->reels ();
49         BOOST_CHECK_EQUAL (r.size(), 1);
50         BOOST_CHECK_EQUAL (r.front().from, DCPTime (0));
51         BOOST_CHECK_EQUAL (r.front().to, DCPTime (288000 * 2));
52
53         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
54         r = film->reels ();
55         BOOST_CHECK_EQUAL (r.size(), 2);
56         BOOST_CHECK_EQUAL (r.front().from, DCPTime (0));
57         BOOST_CHECK_EQUAL (r.front().to, DCPTime (288000));
58         BOOST_CHECK_EQUAL (r.back().from, DCPTime (288000));
59         BOOST_CHECK_EQUAL (r.back().to, DCPTime (288000 * 2));
60
61         film->set_j2k_bandwidth (100000000);
62         film->set_reel_type (REELTYPE_BY_LENGTH);
63         /* This is just over 2.5s at 100Mbit/s; should correspond to 60 frames */
64         film->set_reel_length (31253154);
65         r = film->reels ();
66         BOOST_CHECK_EQUAL (r.size(), 3);
67         list<DCPTimePeriod>::const_iterator i = r.begin ();
68         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
69         BOOST_CHECK_EQUAL (i->to, DCPTime::from_frames (60, 24));
70         ++i;
71         BOOST_CHECK_EQUAL (i->from, DCPTime::from_frames (60, 24));
72         BOOST_CHECK_EQUAL (i->to, DCPTime::from_frames (120, 24));
73         ++i;
74         BOOST_CHECK_EQUAL (i->from, DCPTime::from_frames (120, 24));
75         BOOST_CHECK_EQUAL (i->to, DCPTime::from_frames (144, 24));
76 }
77
78 /** Make a short DCP with multi reels split by video content, then import
79  *  this into a new project and make a new DCP referencing it.
80  */
81 BOOST_AUTO_TEST_CASE (reels_test2)
82 {
83         shared_ptr<Film> film = new_test_film ("reels_test2");
84         film->set_name ("reels_test2");
85         film->set_container (Ratio::from_id ("185"));
86         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
87
88         {
89                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_red.png"));
90                 film->examine_and_add_content (c);
91                 wait_for_jobs ();
92                 c->video->set_length (24);
93         }
94
95         {
96                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_green.png"));
97                 film->examine_and_add_content (c);
98                 wait_for_jobs ();
99                 c->video->set_length (24);
100         }
101
102         {
103                 shared_ptr<ImageContent> c (new ImageContent (film, "test/data/flat_blue.png"));
104                 film->examine_and_add_content (c);
105                 wait_for_jobs ();
106                 c->video->set_length (24);
107         }
108
109         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
110         wait_for_jobs ();
111
112         film->make_dcp ();
113         wait_for_jobs ();
114
115         check_dcp ("test/data/reels_test2", film->dir (film->dcp_name()));
116
117         shared_ptr<Film> film2 = new_test_film ("reels_test2b");
118         film2->set_name ("reels_test2b");
119         film2->set_container (Ratio::from_id ("185"));
120         film2->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
121         film2->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
122
123         shared_ptr<DCPContent> c (new DCPContent (film2, film->dir (film->dcp_name ())));
124         film2->examine_and_add_content (c);
125         wait_for_jobs ();
126
127         list<DCPTimePeriod> r = film2->reels ();
128         BOOST_CHECK_EQUAL (r.size(), 3);
129         list<DCPTimePeriod>::const_iterator i = r.begin ();
130         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
131         BOOST_CHECK_EQUAL (i->to, DCPTime (96000));
132         ++i;
133         BOOST_CHECK_EQUAL (i->from, DCPTime (96000));
134         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 2));
135         ++i;
136         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 2));
137         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 3));
138
139         c->set_reference_video (true);
140         c->set_reference_audio (true);
141
142         film2->make_dcp ();
143         wait_for_jobs ();
144 }
145
146 /** Check that REELTYPE_BY_VIDEO_CONTENT adds an extra reel, if necessary, at the end
147  *  of all the video content to mop up anything afterward.
148  */
149 BOOST_AUTO_TEST_CASE (reels_test3)
150 {
151         shared_ptr<Film> film = new_test_film ("reels_test3");
152         film->set_name ("reels_test3");
153         film->set_container (Ratio::from_id ("185"));
154         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
155         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
156
157         shared_ptr<Content> dcp (new DCPContent (film, "test/data/reels_test2"));
158         film->examine_and_add_content (dcp);
159         shared_ptr<Content> sub (new TextSubtitleContent (film, "test/data/subrip.srt"));
160         film->examine_and_add_content (sub);
161         wait_for_jobs ();
162
163         list<DCPTimePeriod> reels = film->reels();
164         BOOST_REQUIRE_EQUAL (reels.size(), 4);
165         list<DCPTimePeriod>::const_iterator i = reels.begin ();
166         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
167         BOOST_CHECK_EQUAL (i->to, DCPTime (96000));
168         ++i;
169         BOOST_CHECK_EQUAL (i->from, DCPTime (96000));
170         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 2));
171         ++i;
172         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 2));
173         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 3));
174         ++i;
175         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 3));
176         BOOST_CHECK_EQUAL (i->to, sub->full_length().round_up (film->video_frame_rate()));
177 }
178
179 /** Check creation of a multi-reel DCP with a single .srt subtitle file;
180  *  make sure that the reel subtitle timing is done right.
181  */
182 BOOST_AUTO_TEST_CASE (reels_test4)
183 {
184         shared_ptr<Film> film = new_test_film ("reels_test4");
185         film->set_name ("reels_test4");
186         film->set_container (Ratio::from_id ("185"));
187         film->set_dcp_content_type (DCPContentType::from_pretty_name ("Test"));
188         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
189
190         /* 4 piece of 1s-long content */
191         shared_ptr<ImageContent> content[4];
192         for (int i = 0; i < 4; ++i) {
193                 content[i].reset (new ImageContent (film, "test/data/flat_green.png"));
194                 film->examine_and_add_content (content[i]);
195                 wait_for_jobs ();
196                 content[i]->video->set_length (24);
197         }
198
199         shared_ptr<TextSubtitleContent> subs (new TextSubtitleContent (film, "test/data/subrip3.srt"));
200         film->examine_and_add_content (subs);
201         wait_for_jobs ();
202
203         list<DCPTimePeriod> reels = film->reels();
204         BOOST_REQUIRE_EQUAL (reels.size(), 4);
205         list<DCPTimePeriod>::const_iterator i = reels.begin ();
206         BOOST_CHECK_EQUAL (i->from, DCPTime (0));
207         BOOST_CHECK_EQUAL (i->to, DCPTime (96000));
208         ++i;
209         BOOST_CHECK_EQUAL (i->from, DCPTime (96000));
210         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 2));
211         ++i;
212         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 2));
213         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 3));
214         ++i;
215         BOOST_CHECK_EQUAL (i->from, DCPTime (96000 * 3));
216         BOOST_CHECK_EQUAL (i->to, DCPTime (96000 * 4));
217
218         film->make_dcp ();
219         wait_for_jobs ();
220
221         check_dcp ("test/data/reels_test4", film->dir (film->dcp_name()));
222 }