b5d37826f47a8f7a3b8269bace461992d2e19f17
[libdcp.git] / test / dcp_test.cc
1 /*
2     Copyright (C) 2013-2017 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "dcp.h"
21 #include "metadata.h"
22 #include "cpl.h"
23 #include "mono_picture_asset.h"
24 #include "stereo_picture_asset.h"
25 #include "picture_asset_writer.h"
26 #include "sound_asset_writer.h"
27 #include "sound_asset.h"
28 #include "atmos_asset.h"
29 #include "reel.h"
30 #include "test.h"
31 #include "file.h"
32 #include "reel_mono_picture_asset.h"
33 #include "reel_stereo_picture_asset.h"
34 #include "reel_sound_asset.h"
35 #include "reel_atmos_asset.h"
36 #include <asdcp/KM_util.h>
37 #include <sndfile.h>
38 #include <boost/test/unit_test.hpp>
39
40 using std::string;
41 using boost::shared_ptr;
42
43 static shared_ptr<dcp::DCP>
44 make_simple (boost::filesystem::path path)
45 {
46         Kumu::cth_test = true;
47
48         /* Some known metadata */
49         dcp::XMLMetadata xml_meta;
50         xml_meta.annotation_text = "A Test DCP";
51         xml_meta.issuer = "OpenDCP 0.0.25";
52         xml_meta.creator = "OpenDCP 0.0.25";
53         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
54         dcp::MXFMetadata mxf_meta;
55         mxf_meta.company_name = "OpenDCP";
56         mxf_meta.product_name = "OpenDCP";
57         mxf_meta.product_version = "0.0.25";
58
59         /* We're making build/test/DCP/dcp_test1 */
60         boost::filesystem::remove_all (path);
61         boost::filesystem::create_directories (path);
62         shared_ptr<dcp::DCP> d (new dcp::DCP (path));
63         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
64         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
65         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
66         cpl->set_metadata (xml_meta);
67
68         shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
69         mp->set_metadata (mxf_meta);
70         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write (path / "video.mxf", false);
71         dcp::File j2c ("test/data/32x32_red_square.j2c");
72         for (int i = 0; i < 24; ++i) {
73                 picture_writer->write (j2c.data (), j2c.size ());
74         }
75         picture_writer->finalize ();
76
77         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1, dcp::SMPTE));
78         ms->set_metadata (mxf_meta);
79         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write (path / "audio.mxf");
80
81         SF_INFO info;
82         info.format = 0;
83         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
84         BOOST_CHECK (sndfile);
85         float buffer[4096*6];
86         float* channels[1];
87         channels[0] = buffer;
88         while (1) {
89                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
90                 sound_writer->write (channels, N);
91                 if (N < 4096) {
92                         break;
93                 }
94         }
95
96         sound_writer->finalize ();
97
98         cpl->add (shared_ptr<dcp::Reel> (
99                           new dcp::Reel (
100                                   shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
101                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0))
102                                   )
103                           ));
104
105         d->add (cpl);
106         return d;
107 }
108
109 /** Test creation of a 2D SMPTE DCP from very simple inputs */
110 BOOST_AUTO_TEST_CASE (dcp_test1)
111 {
112         dcp::XMLMetadata xml_meta;
113         xml_meta.annotation_text = "Created by libdcp";
114         xml_meta.issuer = "OpenDCP 0.0.25";
115         xml_meta.creator = "OpenDCP 0.0.25";
116         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
117         make_simple("build/test/DCP/dcp_test1")->write_xml (dcp::SMPTE, xml_meta);
118         /* build/test/DCP/dcp_test1 is checked against test/ref/DCP/dcp_test1 by run/tests */
119 }
120
121 /** Test creation of a 3D DCP from very simple inputs */
122 BOOST_AUTO_TEST_CASE (dcp_test2)
123 {
124         Kumu::cth_test = true;
125
126         /* Some known metadata */
127         dcp::XMLMetadata xml_meta;
128         xml_meta.annotation_text = "A Test DCP";
129         xml_meta.issuer = "OpenDCP 0.0.25";
130         xml_meta.creator = "OpenDCP 0.0.25";
131         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
132         dcp::MXFMetadata mxf_meta;
133         mxf_meta.company_name = "OpenDCP";
134         mxf_meta.product_name = "OpenDCP";
135         mxf_meta.product_version = "0.0.25";
136
137         /* We're making build/test/DCP/dcp_test2 */
138         boost::filesystem::remove_all ("build/test/DCP/dcp_test2");
139         boost::filesystem::create_directories ("build/test/DCP/dcp_test2");
140         dcp::DCP d ("build/test/DCP/dcp_test2");
141         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
142         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
143         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
144         cpl->set_metadata (xml_meta);
145
146         shared_ptr<dcp::StereoPictureAsset> mp (new dcp::StereoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
147         mp->set_metadata (mxf_meta);
148         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test2/video.mxf", false);
149         dcp::File j2c ("test/data/32x32_red_square.j2c");
150         for (int i = 0; i < 24; ++i) {
151                 /* Left */
152                 picture_writer->write (j2c.data (), j2c.size ());
153                 /* Right */
154                 picture_writer->write (j2c.data (), j2c.size ());
155         }
156         picture_writer->finalize ();
157
158         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1, dcp::SMPTE));
159         ms->set_metadata (mxf_meta);
160         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test2/audio.mxf");
161
162         SF_INFO info;
163         info.format = 0;
164         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
165         BOOST_CHECK (sndfile);
166         float buffer[4096*6];
167         float* channels[1];
168         channels[0] = buffer;
169         while (1) {
170                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
171                 sound_writer->write (channels, N);
172                 if (N < 4096) {
173                         break;
174                 }
175         }
176
177         sound_writer->finalize ();
178
179         cpl->add (shared_ptr<dcp::Reel> (
180                           new dcp::Reel (
181                                   shared_ptr<dcp::ReelStereoPictureAsset> (new dcp::ReelStereoPictureAsset (mp, 0)),
182                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0))
183                                   )
184                           ));
185
186         d.add (cpl);
187
188         xml_meta.annotation_text = "Created by libdcp";
189         d.write_xml (dcp::SMPTE, xml_meta);
190
191         /* build/test/DCP/dcp_test2 is checked against test/ref/DCP/dcp_test2 by run/tests */
192 }
193
194 static void
195 note (dcp::NoteType, string)
196 {
197
198 }
199
200 /** Test comparison of a DCP with itself */
201 BOOST_AUTO_TEST_CASE (dcp_test3)
202 {
203         dcp::DCP A ("test/ref/DCP/dcp_test1");
204         A.read ();
205         dcp::DCP B ("test/ref/DCP/dcp_test1");
206         B.read ();
207
208         BOOST_CHECK (A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
209 }
210
211 /** Test comparison of a DCP with a different DCP */
212 BOOST_AUTO_TEST_CASE (dcp_test4)
213 {
214         dcp::DCP A ("test/ref/DCP/dcp_test1");
215         A.read ();
216         dcp::DCP B ("test/ref/DCP/dcp_test2");
217         B.read ();
218
219         BOOST_CHECK (!A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
220 }
221
222 /** Test creation of a 2D DCP with an Atmos track */
223 BOOST_AUTO_TEST_CASE (dcp_test5)
224 {
225         Kumu::cth_test = true;
226
227         /* Some known metadata */
228         dcp::XMLMetadata xml_meta;
229         xml_meta.annotation_text = "A Test DCP";
230         xml_meta.issuer = "OpenDCP 0.0.25";
231         xml_meta.creator = "OpenDCP 0.0.25";
232         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
233         dcp::MXFMetadata mxf_meta;
234         mxf_meta.company_name = "OpenDCP";
235         mxf_meta.product_name = "OpenDCP";
236         mxf_meta.product_version = "0.0.25";
237
238         /* We're making build/test/DCP/dcp_test5 */
239         boost::filesystem::remove_all ("build/test/DCP/dcp_test5");
240         boost::filesystem::create_directories ("build/test/DCP/dcp_test5");
241         dcp::DCP d ("build/test/DCP/dcp_test5");
242         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
243         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
244         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
245         cpl->set_metadata (xml_meta);
246
247         shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
248         mp->set_metadata (mxf_meta);
249         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test5/video.mxf", false);
250         dcp::File j2c ("test/data/32x32_red_square.j2c");
251         for (int i = 0; i < 24; ++i) {
252                 picture_writer->write (j2c.data (), j2c.size ());
253         }
254         picture_writer->finalize ();
255
256         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1, dcp::SMPTE));
257         ms->set_metadata (mxf_meta);
258         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test5/audio.mxf");
259
260         SF_INFO info;
261         info.format = 0;
262         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
263         BOOST_CHECK (sndfile);
264         float buffer[4096*6];
265         float* channels[1];
266         channels[0] = buffer;
267         while (true) {
268                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
269                 sound_writer->write (channels, N);
270                 if (N < 4096) {
271                         break;
272                 }
273         }
274
275         sound_writer->finalize ();
276
277         shared_ptr<dcp::AtmosAsset> am (new dcp::AtmosAsset (private_test / "20160218_NameOfFilm_FTR_OV_EN_A_dcs_r01.mxf"));
278
279         cpl->add (shared_ptr<dcp::Reel> (
280                           new dcp::Reel (
281                                   shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
282                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
283                                   shared_ptr<dcp::ReelSubtitleAsset> (),
284                                   shared_ptr<dcp::ReelAtmosAsset> (new dcp::ReelAtmosAsset (am, 0))
285                                   )
286                           ));
287
288         d.add (cpl);
289
290         xml_meta.annotation_text = "Created by libdcp";
291         d.write_xml (dcp::SMPTE, xml_meta);
292
293         /* build/test/DCP/dcp_test5 is checked against test/ref/DCP/dcp_test5 by run/tests */
294 }
295
296 /** Basic tests of reading a 2D DCP with an Atmos track */
297 BOOST_AUTO_TEST_CASE (dcp_test6)
298 {
299         dcp::DCP dcp ("test/ref/DCP/dcp_test5");
300         dcp.read ();
301
302         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1);
303         BOOST_REQUIRE_EQUAL (dcp.cpls().front()->reels().size(), 1);
304         BOOST_CHECK (dcp.cpls().front()->reels().front()->main_picture());
305         BOOST_CHECK (dcp.cpls().front()->reels().front()->main_sound());
306         BOOST_CHECK (!dcp.cpls().front()->reels().front()->main_subtitle());
307         BOOST_CHECK (dcp.cpls().front()->reels().front()->atmos());
308 }
309
310 /** Test creation of a 2D Interop DCP from very simple inputs */
311 BOOST_AUTO_TEST_CASE (dcp_test7)
312 {
313         dcp::XMLMetadata xml_meta;
314         xml_meta.annotation_text = "Created by libdcp";
315         xml_meta.issuer = "OpenDCP 0.0.25";
316         xml_meta.creator = "OpenDCP 0.0.25";
317         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
318         make_simple("build/test/DCP/dcp_test7")->write_xml (dcp::INTEROP, xml_meta);
319         /* build/test/DCP/dcp_test7 is checked against test/ref/DCP/dcp_test7 by run/tests */
320 }
321
322 /** Test reading of a DCP with multiple PKLs */
323 BOOST_AUTO_TEST_CASE (dcp_test8)
324 {
325         dcp::DCP dcp (private_test / "data/SMPTE_TST-B1PB2P_S_EN-EN-CCAP_5171-HI-VI_2K_ISDCF_20151123_DPPT_SMPTE_combo/");
326         dcp.read ();
327
328         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 2);
329 }
330
331
332 /** Test reading a DCP whose ASSETMAP contains assets not used by any PKL */
333 BOOST_AUTO_TEST_CASE (dcp_things_in_assetmap_not_in_pkl)
334 {
335         dcp::DCP dcp ("test/data/extra_assetmap");
336         BOOST_CHECK_NO_THROW (dcp.read());
337 }