ce10ee28b137b32622e2ec661042bbb526499ff1
[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.issuer = "OpenDCP 0.0.25";
51         xml_meta.creator = "OpenDCP 0.0.25";
52         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
53         dcp::MXFMetadata mxf_meta;
54         mxf_meta.company_name = "OpenDCP";
55         mxf_meta.product_name = "OpenDCP";
56         mxf_meta.product_version = "0.0.25";
57
58         /* We're making build/test/DCP/dcp_test1 */
59         boost::filesystem::remove_all ("build/test/DCP/dcp_test1");
60         boost::filesystem::create_directories ("build/test/DCP/dcp_test1");
61         dcp::DCP d ("build/test/DCP/dcp_test1");
62         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
63         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
64         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
65         cpl->set_metadata (xml_meta);
66
67         shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1)));
68         mp->set_metadata (mxf_meta);
69         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test1/video.mxf", dcp::SMPTE, false);
70         dcp::File j2c ("test/data/32x32_red_square.j2c");
71         for (int i = 0; i < 24; ++i) {
72                 picture_writer->write (j2c.data (), j2c.size ());
73         }
74         picture_writer->finalize ();
75
76         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1));
77         ms->set_metadata (mxf_meta);
78         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test1/audio.mxf", dcp::SMPTE);
79
80         SF_INFO info;
81         info.format = 0;
82         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
83         BOOST_CHECK (sndfile);
84         float buffer[4096*6];
85         float* channels[1];
86         channels[0] = buffer;
87         while (1) {
88                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
89                 sound_writer->write (channels, N);
90                 if (N < 4096) {
91                         break;
92                 }
93         }
94
95         sound_writer->finalize ();
96
97         cpl->add (shared_ptr<dcp::Reel> (
98                           new dcp::Reel (
99                                   shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
100                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0))
101                                   )
102                           ));
103
104         d.add (cpl);
105
106         d.write_xml (dcp::SMPTE, xml_meta);
107
108         /* build/test/DCP/dcp_test1 is checked against test/ref/DCP/dcp_test1 by run/tests */
109 }
110
111 /** Test creation of a 3D DCP from very simple inputs */
112 BOOST_AUTO_TEST_CASE (dcp_test2)
113 {
114         Kumu::cth_test = true;
115
116         /* Some known metadata */
117         dcp::XMLMetadata xml_meta;
118         xml_meta.issuer = "OpenDCP 0.0.25";
119         xml_meta.creator = "OpenDCP 0.0.25";
120         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
121         dcp::MXFMetadata mxf_meta;
122         mxf_meta.company_name = "OpenDCP";
123         mxf_meta.product_name = "OpenDCP";
124         mxf_meta.product_version = "0.0.25";
125
126         /* We're making build/test/DCP/dcp_test2 */
127         boost::filesystem::remove_all ("build/test/DCP/dcp_test2");
128         boost::filesystem::create_directories ("build/test/DCP/dcp_test2");
129         dcp::DCP d ("build/test/DCP/dcp_test2");
130         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
131         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
132         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
133         cpl->set_metadata (xml_meta);
134
135         shared_ptr<dcp::StereoPictureAsset> mp (new dcp::StereoPictureAsset (dcp::Fraction (24, 1)));
136         mp->set_metadata (mxf_meta);
137         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test2/video.mxf", dcp::SMPTE, false);
138         dcp::File j2c ("test/data/32x32_red_square.j2c");
139         for (int i = 0; i < 24; ++i) {
140                 /* Left */
141                 picture_writer->write (j2c.data (), j2c.size ());
142                 /* Right */
143                 picture_writer->write (j2c.data (), j2c.size ());
144         }
145         picture_writer->finalize ();
146
147         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1));
148         ms->set_metadata (mxf_meta);
149         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test2/audio.mxf", dcp::SMPTE);
150
151         SF_INFO info;
152         info.format = 0;
153         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
154         BOOST_CHECK (sndfile);
155         float buffer[4096*6];
156         float* channels[1];
157         channels[0] = buffer;
158         while (1) {
159                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
160                 sound_writer->write (channels, N);
161                 if (N < 4096) {
162                         break;
163                 }
164         }
165
166         sound_writer->finalize ();
167
168         cpl->add (shared_ptr<dcp::Reel> (
169                           new dcp::Reel (
170                                   shared_ptr<dcp::ReelStereoPictureAsset> (new dcp::ReelStereoPictureAsset (mp, 0)),
171                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0))
172                                   )
173                           ));
174
175         d.add (cpl);
176
177         d.write_xml (dcp::SMPTE, xml_meta);
178
179         /* build/test/DCP/dcp_test2 is checked against test/ref/DCP/dcp_test2 by run/tests */
180 }
181
182 static void
183 note (dcp::NoteType, string)
184 {
185
186 }
187
188 /** Test comparison of a DCP with itself */
189 BOOST_AUTO_TEST_CASE (dcp_test3)
190 {
191         dcp::DCP A ("test/ref/DCP/dcp_test1");
192         A.read ();
193         dcp::DCP B ("test/ref/DCP/dcp_test1");
194         B.read ();
195
196         BOOST_CHECK (A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
197 }
198
199 /** Test comparison of a DCP with a different DCP */
200 BOOST_AUTO_TEST_CASE (dcp_test4)
201 {
202         dcp::DCP A ("test/ref/DCP/dcp_test1");
203         A.read ();
204         dcp::DCP B ("test/ref/DCP/dcp_test2");
205         B.read ();
206
207         BOOST_CHECK (!A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
208 }
209
210 /** Test creation of a 2D DCP with an Atmos track */
211 BOOST_AUTO_TEST_CASE (dcp_test5)
212 {
213         Kumu::cth_test = true;
214
215         /* Some known metadata */
216         dcp::XMLMetadata xml_meta;
217         xml_meta.issuer = "OpenDCP 0.0.25";
218         xml_meta.creator = "OpenDCP 0.0.25";
219         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
220         dcp::MXFMetadata mxf_meta;
221         mxf_meta.company_name = "OpenDCP";
222         mxf_meta.product_name = "OpenDCP";
223         mxf_meta.product_version = "0.0.25";
224
225         /* We're making build/test/DCP/dcp_test5 */
226         boost::filesystem::remove_all ("build/test/DCP/dcp_test5");
227         boost::filesystem::create_directories ("build/test/DCP/dcp_test5");
228         dcp::DCP d ("build/test/DCP/dcp_test5");
229         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
230         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
231         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
232         cpl->set_metadata (xml_meta);
233
234         shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1)));
235         mp->set_metadata (mxf_meta);
236         shared_ptr<dcp::PictureAssetWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test5/video.mxf", dcp::SMPTE, false);
237         dcp::File j2c ("test/data/32x32_red_square.j2c");
238         for (int i = 0; i < 24; ++i) {
239                 picture_writer->write (j2c.data (), j2c.size ());
240         }
241         picture_writer->finalize ();
242
243         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1));
244         ms->set_metadata (mxf_meta);
245         shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test5/audio.mxf", dcp::SMPTE);
246
247         SF_INFO info;
248         info.format = 0;
249         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
250         BOOST_CHECK (sndfile);
251         float buffer[4096*6];
252         float* channels[1];
253         channels[0] = buffer;
254         while (true) {
255                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
256                 sound_writer->write (channels, N);
257                 if (N < 4096) {
258                         break;
259                 }
260         }
261
262         sound_writer->finalize ();
263
264         shared_ptr<dcp::AtmosAsset> am (new dcp::AtmosAsset (private_test / "20160218_NameOfFilm_FTR_OV_EN_A_dcs_r01.mxf"));
265
266         cpl->add (shared_ptr<dcp::Reel> (
267                           new dcp::Reel (
268                                   shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
269                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
270                                   shared_ptr<dcp::ReelSubtitleAsset> (),
271                                   shared_ptr<dcp::ReelAtmosAsset> (new dcp::ReelAtmosAsset (am, 0))
272                                   )
273                           ));
274
275         d.add (cpl);
276
277         d.write_xml (dcp::SMPTE, xml_meta);
278
279         /* build/test/DCP/dcp_test5 is checked against test/ref/DCP/dcp_test5 by run/tests */
280 }