Rename some stuff Content -> Asset.
[libdcp.git] / test / dcp_test.cc
1 /*
2     Copyright (C) 2013-2015 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 "dcp.h"
21 #include "metadata.h"
22 #include "cpl.h"
23 #include "mono_picture_mxf.h"
24 #include "stereo_picture_mxf.h"
25 #include "picture_mxf_writer.h"
26 #include "sound_mxf_writer.h"
27 #include "sound_mxf.h"
28 #include "reel.h"
29 #include "test.h"
30 #include "file.h"
31 #include "reel_mono_picture_asset.h"
32 #include "reel_stereo_picture_asset.h"
33 #include "reel_sound_asset.h"
34 #include "KM_util.h"
35 #include <sndfile.h>
36 #include <boost/test/unit_test.hpp>
37
38 using std::string;
39 using boost::shared_ptr;
40
41 /** Test creation of a 2D DCP from very simple inputs */
42 BOOST_AUTO_TEST_CASE (dcp_test1)
43 {
44         Kumu::libdcp_test = true;
45
46         /* Some known metadata */
47         dcp::XMLMetadata xml_meta;
48         xml_meta.issuer = "OpenDCP 0.0.25";
49         xml_meta.creator = "OpenDCP 0.0.25";
50         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
51         dcp::MXFMetadata mxf_meta;
52         mxf_meta.company_name = "OpenDCP";
53         mxf_meta.product_name = "OpenDCP";
54         mxf_meta.product_version = "0.0.25";
55
56         /* We're making build/test/DCP/dcp_test1 */
57         boost::filesystem::remove_all ("build/test/DCP/dcp_test1");
58         boost::filesystem::create_directories ("build/test/DCP/dcp_test1");
59         dcp::DCP d ("build/test/DCP/dcp_test1");
60         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
61         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
62         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
63         cpl->set_metadata (xml_meta);
64
65         shared_ptr<dcp::MonoPictureMXF> mp (new dcp::MonoPictureMXF (dcp::Fraction (24, 1)));
66         mp->set_metadata (mxf_meta);
67         shared_ptr<dcp::PictureMXFWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test1/video.mxf", dcp::SMPTE, false);
68         dcp::File j2c ("test/data/32x32_red_square.j2c");
69         for (int i = 0; i < 24; ++i) {
70                 picture_writer->write (j2c.data (), j2c.size ());
71         }
72         picture_writer->finalize ();
73
74         shared_ptr<dcp::SoundMXF> ms (new dcp::SoundMXF (dcp::Fraction (24, 1), 48000, 1));
75         ms->set_metadata (mxf_meta);
76         shared_ptr<dcp::SoundMXFWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test1/audio.mxf", dcp::SMPTE);
77
78         SF_INFO info;
79         info.format = 0;
80         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
81         BOOST_CHECK (sndfile);
82         float buffer[4096*6];
83         float* channels[1];
84         channels[0] = buffer;
85         while (1) {
86                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
87                 sound_writer->write (channels, N);
88                 if (N < 4096) {
89                         break;
90                 }
91         }
92         
93         sound_writer->finalize ();
94         
95         cpl->add (shared_ptr<dcp::Reel> (
96                           new dcp::Reel (
97                                   shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
98                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
99                                   shared_ptr<dcp::ReelSubtitleAsset> ()
100                                   )
101                           ));
102                   
103         d.add (cpl);
104         d.add (mp);
105         d.add (ms);
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.sh */
110 }
111
112 /** Test creation of a 3D DCP from very simple inputs */
113 BOOST_AUTO_TEST_CASE (dcp_test2)
114 {
115         Kumu::libdcp_test = true;
116
117         /* Some known metadata */
118         dcp::XMLMetadata xml_meta;
119         xml_meta.issuer = "OpenDCP 0.0.25";
120         xml_meta.creator = "OpenDCP 0.0.25";
121         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
122         dcp::MXFMetadata mxf_meta;
123         mxf_meta.company_name = "OpenDCP";
124         mxf_meta.product_name = "OpenDCP";
125         mxf_meta.product_version = "0.0.25";
126
127         /* We're making build/test/DCP/dcp_test2 */
128         boost::filesystem::remove_all ("build/test/DCP/dcp_test2");
129         boost::filesystem::create_directories ("build/test/DCP/dcp_test2");
130         dcp::DCP d ("build/test/DCP/dcp_test2");
131         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
132         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
133         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
134         cpl->set_metadata (xml_meta);
135
136         shared_ptr<dcp::StereoPictureMXF> mp (new dcp::StereoPictureMXF (dcp::Fraction (24, 1)));
137         mp->set_metadata (mxf_meta);
138         shared_ptr<dcp::PictureMXFWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test2/video.mxf", dcp::SMPTE, false);
139         dcp::File j2c ("test/data/32x32_red_square.j2c");
140         for (int i = 0; i < 24; ++i) {
141                 /* Left */
142                 picture_writer->write (j2c.data (), j2c.size ());
143                 /* Right */
144                 picture_writer->write (j2c.data (), j2c.size ());
145         }
146         picture_writer->finalize ();
147
148         shared_ptr<dcp::SoundMXF> ms (new dcp::SoundMXF (dcp::Fraction (24, 1), 48000, 1));
149         ms->set_metadata (mxf_meta);
150         shared_ptr<dcp::SoundMXFWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test2/audio.mxf", dcp::SMPTE);
151
152         SF_INFO info;
153         info.format = 0;
154         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
155         BOOST_CHECK (sndfile);
156         float buffer[4096*6];
157         float* channels[1];
158         channels[0] = buffer;
159         while (1) {
160                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
161                 sound_writer->write (channels, N);
162                 if (N < 4096) {
163                         break;
164                 }
165         }
166         
167         sound_writer->finalize ();
168         
169         cpl->add (shared_ptr<dcp::Reel> (
170                           new dcp::Reel (
171                                   shared_ptr<dcp::ReelStereoPictureAsset> (new dcp::ReelStereoPictureAsset (mp, 0)),
172                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
173                                   shared_ptr<dcp::ReelSubtitleAsset> ()
174                                   )
175                           ));
176                   
177         d.add (cpl);
178         d.add (mp);
179         d.add (ms);
180
181         d.write_xml (dcp::SMPTE, xml_meta);
182
183         /* build/test/DCP/dcp_test2 is checked against test/ref/DCP/dcp_test2 by run-tests.sh */
184 }
185
186 static void
187 note (dcp::NoteType, string)
188 {
189
190 }
191
192 /** Test comparison of a DCP with itself */
193 BOOST_AUTO_TEST_CASE (dcp_test3)
194 {
195         dcp::DCP A ("test/ref/DCP/dcp_test1");
196         A.read ();
197         dcp::DCP B ("test/ref/DCP/dcp_test1");
198         B.read ();
199         
200         BOOST_CHECK (A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
201 }
202
203 /** Test comparison of a DCP with a different DCP */
204 BOOST_AUTO_TEST_CASE (dcp_test4)
205 {
206         dcp::DCP A ("test/ref/DCP/dcp_test1");
207         A.read ();
208         dcp::DCP B ("test/ref/DCP/dcp_test2");
209         B.read ();
210         
211         BOOST_CHECK (!A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
212 }
213