Add a basic stereo test.
[libdcp.git] / test / dcp_test.cc
1 /*
2     Copyright (C) 2013-2014 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 "subtitle_content.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 "KM_util.h"
36 #include <sndfile.h>
37 #include <boost/test/unit_test.hpp>
38
39 using std::string;
40 using boost::shared_ptr;
41
42 /** Test creation of a 2D DCP from very simple inputs */
43 BOOST_AUTO_TEST_CASE (dcp_test1)
44 {
45         Kumu::libdcp_test = true;
46
47         /* Some known metadata */
48         dcp::XMLMetadata xml_meta;
49         xml_meta.issuer = "OpenDCP 0.0.25";
50         xml_meta.creator = "OpenDCP 0.0.25";
51         xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
52         dcp::MXFMetadata mxf_meta;
53         mxf_meta.company_name = "OpenDCP";
54         mxf_meta.product_name = "OpenDCP";
55         mxf_meta.product_version = "0.0.25";
56
57         /* We're making build/test/DCP/dcp_test1 */
58         boost::filesystem::remove_all ("build/test/DCP/dcp_test1");
59         boost::filesystem::create_directories ("build/test/DCP/dcp_test1");
60         dcp::DCP d ("build/test/DCP/dcp_test1");
61         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
62         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
63         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
64         cpl->set_metadata (xml_meta);
65
66         shared_ptr<dcp::MonoPictureMXF> mp (new dcp::MonoPictureMXF (dcp::Fraction (24, 1)));
67         mp->set_metadata (mxf_meta);
68         shared_ptr<dcp::PictureMXFWriter> picture_writer = mp->start_write ("build/test/DCP/dcp_test1/video.mxf", dcp::SMPTE, false);
69         dcp::File j2c ("test/data/32x32_red_square.j2c");
70         for (int i = 0; i < 24; ++i) {
71                 picture_writer->write (j2c.data (), j2c.size ());
72         }
73         picture_writer->finalize ();
74
75         shared_ptr<dcp::SoundMXF> ms (new dcp::SoundMXF (dcp::Fraction (24, 1), 48000, 1));
76         ms->set_metadata (mxf_meta);
77         shared_ptr<dcp::SoundMXFWriter> sound_writer = ms->start_write ("build/test/DCP/dcp_test1/audio.mxf", dcp::SMPTE);
78
79         SF_INFO info;
80         info.format = 0;
81         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
82         BOOST_CHECK (sndfile);
83         float buffer[4096*6];
84         float* channels[1];
85         channels[0] = buffer;
86         while (1) {
87                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
88                 sound_writer->write (channels, N);
89                 if (N < 4096) {
90                         break;
91                 }
92         }
93         
94         sound_writer->finalize ();
95         
96         cpl->add (shared_ptr<dcp::Reel> (
97                           new dcp::Reel (
98                                   shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
99                                   shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
100                                   shared_ptr<dcp::ReelSubtitleAsset> ()
101                                   )
102                           ));
103                   
104         d.add (cpl);
105         d.add (mp);
106         d.add (ms);
107
108         d.write_xml (dcp::SMPTE, xml_meta);
109
110         /* build/test/DCP/dcp_test1 is checked against test/ref/DCP/dcp_test1 by run-tests.sh */
111 }
112
113 /** Test creation of a 3D DCP from very simple inputs */
114 BOOST_AUTO_TEST_CASE (dcp_test2)
115 {
116         Kumu::libdcp_test = true;
117
118         /* Some known metadata */
119         dcp::XMLMetadata xml_meta;
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::StereoPictureMXF> mp (new dcp::StereoPictureMXF (dcp::Fraction (24, 1)));
138         mp->set_metadata (mxf_meta);
139         shared_ptr<dcp::PictureMXFWriter> 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::SoundMXF> ms (new dcp::SoundMXF (dcp::Fraction (24, 1), 48000, 1));
150         ms->set_metadata (mxf_meta);
151         shared_ptr<dcp::SoundMXFWriter> 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                                   shared_ptr<dcp::ReelSubtitleAsset> ()
175                                   )
176                           ));
177                   
178         d.add (cpl);
179         d.add (mp);
180         d.add (ms);
181
182         d.write_xml (dcp::SMPTE, xml_meta);
183
184         /* build/test/DCP/dcp_test2 is checked against test/ref/DCP/dcp_test2 by run-tests.sh */
185 }
186
187 static void
188 note (dcp::NoteType, string s)
189 {
190
191 }
192
193 /** Test comparison of a DCP with itself */
194 BOOST_AUTO_TEST_CASE (dcp_test3)
195 {
196         dcp::DCP A ("test/ref/DCP/dcp_test1");
197         A.read ();
198         dcp::DCP B ("test/ref/DCP/dcp_test1");
199         B.read ();
200         
201         BOOST_CHECK (A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
202 }
203
204 /** Test comparison of a DCP with a different DCP */
205 BOOST_AUTO_TEST_CASE (dcp_test4)
206 {
207         dcp::DCP A ("test/ref/DCP/dcp_test1");
208         A.read ();
209         dcp::DCP B ("test/ref/DCP/dcp_test2");
210         B.read ();
211         
212         BOOST_CHECK (!A.equals (B, dcp::EqualityOptions(), boost::bind (&note, _1, _2)));
213 }
214