Fix test churn.
[libdcp.git] / test / dcp_test.cc
1 /*
2     Copyright (C) 2013-2015 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 /** Test creation of a 2D DCP from very simple inputs */
44 BOOST_AUTO_TEST_CASE (dcp_test1)
45 {
46         Kumu::cth_test = true;
47
48         /* Some known metadata */
49         dcp::XMLMetadata xml_meta;
50         xml_meta.annotation_text = "Created by libdcp";
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 ("build/test/DCP/dcp_test1");
61         boost::filesystem::create_directories ("build/test/DCP/dcp_test1");
62         dcp::DCP d ("build/test/DCP/dcp_test1");
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)));
69         mp->set_metadata (mxf_meta);
70         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test1/video.mxf", dcp::SMPTE, 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));
78         ms->set_metadata (mxf_meta);
79         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test1/audio.mxf", dcp::SMPTE);
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
107         d.write_xml (dcp::SMPTE, xml_meta);
108
109         /* build/test/DCP/dcp_test1 is checked against test/ref/DCP/dcp_test1 by run/tests */
110 }
111
112 /** Test creation of a 3D DCP from very simple inputs */
113 BOOST_AUTO_TEST_CASE (dcp_test2)
114 {
115         Kumu::cth_test = true;
116
117         /* Some known metadata */
118         dcp::XMLMetadata xml_meta;
119         xml_meta.annotation_text = "Created by libdcp";
120         xml_meta.issuer = "OpenDCP 0.0.25";
121         xml_meta.creator = "OpenDCP 0.0.25";
122         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
123         dcp::MXFMetadata mxf_meta;
124         mxf_meta.company_name = "OpenDCP";
125         mxf_meta.product_name = "OpenDCP";
126         mxf_meta.product_version = "0.0.25";
127
128         /* We're making build/test/DCP/dcp_test2 */
129         boost::filesystem::remove_all ("build/test/DCP/dcp_test2");
130         boost::filesystem::create_directories ("build/test/DCP/dcp_test2");
131         dcp::DCP d ("build/test/DCP/dcp_test2");
132         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
133         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
134         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
135         cpl->set_metadata (xml_meta);
136
137         shared_ptr<dcp::StereoPictureAsset> mp (new dcp::StereoPictureAsset (dcp::Fraction (24, 1)));
138         mp->set_metadata (mxf_meta);
139         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test2/video.mxf", dcp::SMPTE, false);
140         dcp::File j2c ("test/data/32x32_red_square.j2c");
141         for (int i = 0; i < 24; ++i) {
142                 /* Left */
143                 picture_writer->write (j2c.data (), j2c.size ());
144                 /* Right */
145                 picture_writer->write (j2c.data (), j2c.size ());
146         }
147         picture_writer->finalize ();
148
149         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1));
150         ms->set_metadata (mxf_meta);
151         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test2/audio.mxf", dcp::SMPTE);
152
153         SF_INFO info;
154         info.format = 0;
155         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
156         BOOST_CHECK (sndfile);
157         float buffer[4096*6];
158         float* channels[1];
159         channels[0] = buffer;
160         while (1) {
161                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
162                 sound_writer->write (channels, N);
163                 if (N < 4096) {
164                         break;
165                 }
166         }
167
168         sound_writer->finalize ();
169
170         cpl->add (shared_ptr<dcp::Reel> (
171                           new dcp::Reel (
172                                   shared_ptr<dcp::ReelStereoPictureAsset> (new dcp::ReelStereoPictureAsset (mp, 0)),
173                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0))
174                                   )
175                           ));
176
177         d.add (cpl);
178
179         d.write_xml (dcp::SMPTE, xml_meta);
180
181         /* build/test/DCP/dcp_test2 is checked against test/ref/DCP/dcp_test2 by run/tests */
182 }
183
184 static void
185 note (dcp::NoteType, string)
186 {
187
188 }
189
190 /** Test comparison of a DCP with itself */
191 BOOST_AUTO_TEST_CASE (dcp_test3)
192 {
193         dcp::DCP A ("test/ref/DCP/dcp_test1");
194         A.read ();
195         dcp::DCP B ("test/ref/DCP/dcp_test1");
196         B.read ();
197
198         BOOST_CHECK (A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
199 }
200
201 /** Test comparison of a DCP with a different DCP */
202 BOOST_AUTO_TEST_CASE (dcp_test4)
203 {
204         dcp::DCP A ("test/ref/DCP/dcp_test1");
205         A.read ();
206         dcp::DCP B ("test/ref/DCP/dcp_test2");
207         B.read ();
208
209         BOOST_CHECK (!A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
210 }
211
212 /** Test creation of a 2D DCP with an Atmos track */
213 BOOST_AUTO_TEST_CASE (dcp_test5)
214 {
215         Kumu::cth_test = true;
216
217         /* Some known metadata */
218         dcp::XMLMetadata xml_meta;
219         xml_meta.annotation_text = "Created by libdcp";
220         xml_meta.issuer = "OpenDCP 0.0.25";
221         xml_meta.creator = "OpenDCP 0.0.25";
222         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
223         dcp::MXFMetadata mxf_meta;
224         mxf_meta.company_name = "OpenDCP";
225         mxf_meta.product_name = "OpenDCP";
226         mxf_meta.product_version = "0.0.25";
227
228         /* We're making build/test/DCP/dcp_test5 */
229         boost::filesystem::remove_all ("build/test/DCP/dcp_test5");
230         boost::filesystem::create_directories ("build/test/DCP/dcp_test5");
231         dcp::DCP d ("build/test/DCP/dcp_test5");
232         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
233         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
234         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
235         cpl->set_metadata (xml_meta);
236
237         shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1)));
238         mp->set_metadata (mxf_meta);
239         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test5/video.mxf", dcp::SMPTE, false);
240         dcp::File j2c ("test/data/32x32_red_square.j2c");
241         for (int i = 0; i < 24; ++i) {
242                 picture_writer->write (j2c.data (), j2c.size ());
243         }
244         picture_writer->finalize ();
245
246         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1));
247         ms->set_metadata (mxf_meta);
248         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test5/audio.mxf", dcp::SMPTE);
249
250         SF_INFO info;
251         info.format = 0;
252         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
253         BOOST_CHECK (sndfile);
254         float buffer[4096*6];
255         float* channels[1];
256         channels[0] = buffer;
257         while (true) {
258                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
259                 sound_writer->write (channels, N);
260                 if (N < 4096) {
261                         break;
262                 }
263         }
264
265         sound_writer->finalize ();
266
267         shared_ptr<dcp::AtmosAsset> am (new dcp::AtmosAsset (private_test / "20160218_NameOfFilm_FTR_OV_EN_A_dcs_r01.mxf"));
268
269         cpl->add (shared_ptr<dcp::Reel> (
270                           new dcp::Reel (
271                                   shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
272                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
273                                   shared_ptr<dcp::ReelSubtitleAsset> (),
274                                   shared_ptr<dcp::ReelAtmosAsset> (new dcp::ReelAtmosAsset (am, 0))
275                                   )
276                           ));
277
278         d.add (cpl);
279
280         d.write_xml (dcp::SMPTE, xml_meta);
281
282         /* build/test/DCP/dcp_test5 is checked against test/ref/DCP/dcp_test5 by run/tests */
283 }
284
285 /** Basic tests of reading a 2D DCP with an Atmos track */
286 BOOST_AUTO_TEST_CASE (dcp_test6)
287 {
288         dcp::DCP dcp ("test/ref/DCP/dcp_test5");
289         dcp.read ();
290
291         BOOST_REQUIRE_EQUAL (dcp.cpls().size(), 1);
292         BOOST_REQUIRE_EQUAL (dcp.cpls().front()->reels().size(), 1);
293         BOOST_CHECK (dcp.cpls().front()->reels().front()->main_picture());
294         BOOST_CHECK (dcp.cpls().front()->reels().front()->main_sound());
295         BOOST_CHECK (!dcp.cpls().front()->reels().front()->main_subtitle());
296         BOOST_CHECK (dcp.cpls().front()->reels().front()->atmos());
297 }