Add run/tools/dcpmap
[libdcp.git] / test / mca_test.cc
1 /*
2     Copyright (C) 2020-2021 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     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #include "compose.hpp"
36 #include "cpl.h"
37 #include "reel.h"
38 #include "reel_sound_asset.h"
39 #include "sound_asset.h"
40 #include "sound_asset_writer.h"
41 #include "test.h"
42 #include "warnings.h"
43 #include <libcxml/cxml.h>
44 LIBDCP_DISABLE_WARNINGS
45 #include <asdcp/Metadata.h>
46 #include <libxml++/libxml++.h>
47 LIBDCP_ENABLE_WARNINGS
48 #include <boost/test/unit_test.hpp>
49
50
51 using std::list;
52 using std::make_shared;
53 using std::shared_ptr;
54 using std::string;
55 using std::vector;
56
57
58 /** Check that when we read a MXF and write its MCA metadata to a CPL we get the same answer
59  *  as the original MXF for that CPL (for a couple of different MXFs).
60  */
61 BOOST_AUTO_TEST_CASE (parse_mca_descriptors_from_mxf_test)
62 {
63         for (int i = 1; i < 3; ++i) {
64                 auto sound_asset = make_shared<dcp::SoundAsset>(private_test / "data" / dcp::String::compose("51_sound_with_mca_%1.mxf", i));
65                 auto reel_sound_asset = make_shared<dcp::ReelSoundAsset>(sound_asset, 0);
66                 auto reel = make_shared<dcp::Reel>();
67                 reel->add (black_picture_asset(dcp::String::compose("build/test/parse_mca_descriptors_from_mxf_test%1", i), 24));
68                 reel->add (reel_sound_asset);
69
70                 dcp::CPL cpl("", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE);
71                 cpl.add (reel);
72                 cpl.set_main_sound_configuration(dcp::MainSoundConfiguration("51/L,R,C,LFE,Ls,Rs"));
73                 cpl.set_main_sound_sample_rate(48000);
74                 cpl.set_main_picture_stored_area(dcp::Size(1998, 1080));
75                 cpl.set_main_picture_active_area(dcp::Size(1998, 1080));
76                 cpl.write_xml (dcp::String::compose("build/test/parse_mca_descriptors_from_mxf_test%1/cpl.xml", i), shared_ptr<dcp::CertificateChain>());
77
78                 cxml::Document ref("CompositionPlaylist", private_test / dcp::String::compose("51_sound_with_mca_%1.cpl", i));
79                 cxml::Document check("CompositionPlaylist", dcp::String::compose("build/test/parse_mca_descriptors_from_mxf_test%1/cpl.xml", i));
80
81                 vector<string> ignore;
82                 check_xml (
83                         dynamic_cast<xmlpp::Element*>(
84                                 ref.node_child("ReelList")->node_children("Reel").front()->node_child("AssetList")->node_child("CompositionMetadataAsset")->node_child("MCASubDescriptors")->node()
85                                 ),
86                         dynamic_cast<xmlpp::Element*>(
87                                 check.node_child("ReelList")->node_children("Reel").front()->node_child("AssetList")->node_child("CompositionMetadataAsset")->node_child("MCASubDescriptors")->node()
88                                 ),
89                         ignore,
90                         true
91                         );
92         }
93 }
94
95
96 /** Reproduce the MCA tags from one of the example files using libdcp */
97 BOOST_AUTO_TEST_CASE (write_mca_descriptors_to_mxf_test)
98 {
99         auto sound_asset = make_shared<dcp::SoundAsset>(dcp::Fraction(24, 1), 48000, 6, dcp::LanguageTag("en-US"), dcp::Standard::SMPTE);
100         auto writer = sound_asset->start_write("build/test/write_mca_descriptors_to_mxf_test.mxf", {}, dcp::SoundAsset::AtmosSync::DISABLED, dcp::SoundAsset::MCASubDescriptors::ENABLED);
101
102         float* samples[6];
103         for (int i = 0; i < 6; ++i) {
104                 samples[i] = new float[2000];
105                 memset (samples[i], 0, 2000 * sizeof(float));
106         }
107         for (int i = 0; i < 24; ++i) {
108                 writer->write(samples, 6, 2000);
109         }
110         for (int i = 0; i < 6; ++i) {
111                 delete[] samples[i];
112         }
113
114         writer->finalize();
115
116         /* Make a CPL as a roundabout way to read the metadata we just wrote to the MXF */
117
118         auto reel_sound_asset = make_shared<dcp::ReelSoundAsset>(sound_asset, 0);
119         auto reel = make_shared<dcp::Reel>();
120         reel->add (black_picture_asset("build/test/write_mca_descriptors_to_mxf_test", 24));
121         reel->add (reel_sound_asset);
122
123         dcp::CPL cpl("", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE);
124         cpl.add (reel);
125         cpl.set_main_sound_configuration(dcp::MainSoundConfiguration("51/L,R,C,LFE,Ls,Rs"));
126         cpl.set_main_sound_sample_rate(48000);
127         cpl.set_main_picture_stored_area(dcp::Size(1998, 1080));
128         cpl.set_main_picture_active_area(dcp::Size(1998, 1080));
129         cpl.write_xml ("build/test/write_mca_descriptors_to_mxf_test/cpl.xml", shared_ptr<dcp::CertificateChain>());
130
131         cxml::Document ref("CompositionPlaylist", private_test / "51_sound_with_mca_1.cpl");
132         cxml::Document check("CompositionPlaylist", "build/test/write_mca_descriptors_to_mxf_test/cpl.xml");
133
134         check_xml (
135                 dynamic_cast<xmlpp::Element*>(
136                         ref.node_child("ReelList")->node_children("Reel")[0]->node_child("AssetList")->node_child("CompositionMetadataAsset")->node_child("MCASubDescriptors")->node()
137                         ),
138                 dynamic_cast<xmlpp::Element*>(
139                         check.node_child("ReelList")->node_children("Reel")[0]->node_child("AssetList")->node_child("CompositionMetadataAsset")->node_child("MCASubDescriptors")->node()
140                         ),
141                 { "InstanceID", "MCALinkID", "SoundfieldGroupLinkID" },
142                 true
143                 );
144 }
145
146
147 static
148 void
149 check_mca_descriptors(int suffix, vector<dcp::Channel> extra_active_channels, vector<string> expected_mca_tag_symbols)
150 {
151         auto const dir = boost::filesystem::path(dcp::String::compose("build/test/check_mca_descriptors_%1", suffix));
152         boost::filesystem::remove_all(dir);
153         boost::filesystem::create_directories(dir);
154
155         auto sound_asset = make_shared<dcp::SoundAsset>(dcp::Fraction(24, 1), 48000, 16, dcp::LanguageTag("en-US"), dcp::Standard::SMPTE);
156         auto writer = sound_asset->start_write(dir / "mxf.mxf", extra_active_channels, dcp::SoundAsset::AtmosSync::DISABLED, dcp::SoundAsset::MCASubDescriptors::ENABLED);
157
158         vector<vector<float>> samples(6);
159         float* pointers[6];
160         for (int i = 0; i < 6; ++i) {
161                 samples[i].resize(2000);
162                 pointers[i] = samples[i].data();
163         }
164         for (int i = 0; i < 24; ++i) {
165                 writer->write(pointers, 6, 2000);
166         }
167         writer->finalize();
168
169         /* Check MXF */
170
171         Kumu::FileReaderFactory factory;
172         auto reader = new ASDCP::PCM::MXFReader(factory);
173         reader->OpenRead(boost::filesystem::path(dir / "mxf.mxf").string());
174
175         list<ASDCP::MXF::InterchangeObject*> channels;
176         auto const r = reader->OP1aHeader().GetMDObjectsByType(
177                 dcp::asdcp_smpte_dict->ul(ASDCP::MDD_AudioChannelLabelSubDescriptor),
178                 channels
179                 );
180         BOOST_REQUIRE(KM_SUCCESS(r));
181
182         vector<string> mxf_mca_tag_symbols;
183         for (auto channel: channels) {
184                 auto audio_channel = reinterpret_cast<ASDCP::MXF::AudioChannelLabelSubDescriptor*>(channel);
185                 char buffer[64];
186                 audio_channel->MCATagSymbol.EncodeString(buffer, sizeof(buffer));
187                 mxf_mca_tag_symbols.push_back(buffer);
188         }
189
190         BOOST_CHECK(expected_mca_tag_symbols == mxf_mca_tag_symbols);
191
192         /* Check CPL */
193
194         auto reel_sound_asset = make_shared<dcp::ReelSoundAsset>(sound_asset, 0);
195         auto reel = make_shared<dcp::Reel>();
196         reel->add(black_picture_asset(dir / "dcp", 24));
197         reel->add(reel_sound_asset);
198
199         dcp::CPL cpl("", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE);
200         cpl.add(reel);
201         cpl.set_main_sound_configuration(dcp::MainSoundConfiguration("51/L,R,C,LFE,Ls,Rs"));
202         cpl.set_main_sound_sample_rate(48000);
203         cpl.set_main_picture_stored_area(dcp::Size(1998, 1080));
204         cpl.set_main_picture_active_area(dcp::Size(1998, 1080));
205         cpl.write_xml(dir / "dcp" / "cpl.xml", shared_ptr<dcp::CertificateChain>());
206
207         cxml::Document check("CompositionPlaylist", dir / "dcp" / "cpl.xml");
208         vector<string> cpl_mca_tag_symbols;
209
210         for (auto node: check.node_child("ReelList")->node_child("Reel")->node_child("AssetList")->node_child("CompositionMetadataAsset")->node_child("MCASubDescriptors")->node_children("AudioChannelLabelSubDescriptor")) {
211                 cpl_mca_tag_symbols.push_back(node->string_child("MCATagSymbol"));
212         }
213
214         BOOST_CHECK(expected_mca_tag_symbols == cpl_mca_tag_symbols);
215 }
216
217
218 BOOST_AUTO_TEST_CASE(write_correct_mca_descriptors)
219 {
220         int suffix = 0;
221
222         check_mca_descriptors(
223                 suffix++,
224                 {}, { "chL", "chR", "chC", "chLFE", "chLs", "chRs" }
225                 );
226
227         check_mca_descriptors(
228                 suffix++,
229                 { dcp::Channel::HI }, { "chL", "chR", "chC", "chLFE", "chLs", "chRs", "chHI" }
230                 );
231
232         check_mca_descriptors(
233                 suffix++,
234                 { dcp::Channel::VI }, { "chL", "chR", "chC", "chLFE", "chLs", "chRs", "chVIN" }
235                 );
236
237         check_mca_descriptors(
238                 suffix++,
239                 { dcp::Channel::BSL }, { "chL", "chR", "chC", "chLFE", "chLss", "chRss", "chLrs" }
240                 );
241
242         check_mca_descriptors(
243                 suffix++,
244                 { dcp::Channel::BSR }, { "chL", "chR", "chC", "chLFE", "chLss", "chRss", "chRrs" }
245                 );
246
247         check_mca_descriptors(
248                 suffix++,
249                 { dcp::Channel::HI, dcp::Channel::VI }, { "chL", "chR", "chC", "chLFE", "chLs", "chRs", "chHI", "chVIN" }
250                 );
251
252         check_mca_descriptors(
253                 suffix++,
254                 { dcp::Channel::HI, dcp::Channel::VI, dcp::Channel::BSL, dcp::Channel::BSR }, { "chL", "chR", "chC", "chLFE", "chLss", "chRss", "chHI", "chVIN", "chLrs", "chRrs" }
255                 );
256
257         check_mca_descriptors(
258                 suffix++,
259                 { dcp::Channel::BSL, dcp::Channel::BSR }, { "chL", "chR", "chC", "chLFE", "chLss", "chRss", "chLrs", "chRrs" }
260                 );
261
262         /* Duplicates should be ignored */
263         check_mca_descriptors(
264                 suffix++,
265                 { dcp::Channel::HI, dcp::Channel::HI }, { "chL", "chR", "chC", "chLFE", "chLs", "chRs", "chHI" }
266                 );
267 }
268