2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
40 #include "asset_factory.h"
41 #include "atmos_asset.h"
42 #include "certificate_chain.h"
43 #include "compose.hpp"
46 #include "dcp_assert.h"
47 #include "decrypted_kdm.h"
48 #include "decrypted_kdm_key.h"
49 #include "exceptions.h"
50 #include "font_asset.h"
51 #include "interop_subtitle_asset.h"
53 #include "mono_picture_asset.h"
54 #include "picture_asset.h"
56 #include "raw_convert.h"
57 #include "reel_asset.h"
58 #include "reel_subtitle_asset.h"
59 #include "smpte_subtitle_asset.h"
60 #include "sound_asset.h"
61 #include "stereo_picture_asset.h"
65 LIBDCP_DISABLE_WARNINGS
66 #include <asdcp/AS_DCP.h>
67 LIBDCP_ENABLE_WARNINGS
68 #include <xmlsec/xmldsig.h>
69 #include <xmlsec/app.h>
70 LIBDCP_DISABLE_WARNINGS
71 #include <libxml++/libxml++.h>
72 LIBDCP_ENABLE_WARNINGS
73 #include <boost/filesystem.hpp>
74 #include <boost/algorithm/string.hpp>
85 using std::make_shared;
87 using std::shared_ptr;
88 using std::dynamic_pointer_cast;
89 using boost::optional;
90 using boost::algorithm::starts_with;
94 static string const assetmap_interop_ns = "http://www.digicine.com/PROTO-ASDCP-AM-20040311#";
95 static string const assetmap_smpte_ns = "http://www.smpte-ra.org/schemas/429-9/2007/AM";
96 static string const volindex_interop_ns = "http://www.digicine.com/PROTO-ASDCP-VL-20040311#";
97 static string const volindex_smpte_ns = "http://www.smpte-ra.org/schemas/429-9/2007/AM";
100 DCP::DCP (boost::filesystem::path directory)
101 : _directory (directory)
103 if (!boost::filesystem::exists (directory)) {
104 boost::filesystem::create_directories (directory);
107 _directory = boost::filesystem::canonical (_directory);
112 DCP::read (vector<dcp::VerificationNote>* notes, bool ignore_incorrect_picture_mxf_type)
114 /* Read the ASSETMAP and PKL */
116 if (boost::filesystem::exists (_directory / "ASSETMAP")) {
117 _asset_map = _directory / "ASSETMAP";
118 } else if (boost::filesystem::exists (_directory / "ASSETMAP.xml")) {
119 _asset_map = _directory / "ASSETMAP.xml";
121 boost::throw_exception (MissingAssetmapError(_directory));
124 cxml::Document asset_map ("AssetMap");
126 asset_map.read_file (_asset_map.get());
127 if (asset_map.namespace_uri() == assetmap_interop_ns) {
128 _standard = Standard::INTEROP;
129 } else if (asset_map.namespace_uri() == assetmap_smpte_ns) {
130 _standard = Standard::SMPTE;
132 boost::throw_exception (XMLError ("Unrecognised Assetmap namespace " + asset_map.namespace_uri()));
135 auto asset_nodes = asset_map.node_child("AssetList")->node_children ("Asset");
136 map<string, boost::filesystem::path> paths;
137 vector<boost::filesystem::path> pkl_paths;
138 for (auto i: asset_nodes) {
139 if (i->node_child("ChunkList")->node_children("Chunk").size() != 1) {
140 boost::throw_exception (XMLError ("unsupported asset chunk count"));
142 auto p = i->node_child("ChunkList")->node_child("Chunk")->string_child ("Path");
143 if (starts_with (p, "file://")) {
146 switch (*_standard) {
147 case Standard::INTEROP:
148 if (i->optional_node_child("PackingList")) {
149 pkl_paths.push_back (p);
151 paths.insert (make_pair(remove_urn_uuid(i->string_child("Id")), p));
154 case Standard::SMPTE:
156 auto pkl_bool = i->optional_string_child("PackingList");
157 if (pkl_bool && *pkl_bool == "true") {
158 pkl_paths.push_back (p);
160 paths.insert (make_pair(remove_urn_uuid(i->string_child("Id")), p));
167 if (pkl_paths.empty()) {
168 boost::throw_exception (XMLError ("No packing lists found in asset map"));
171 for (auto i: pkl_paths) {
172 _pkls.push_back (make_shared<PKL>(_directory / i));
176 paths - map of files in the DCP that are not PKLs; key is ID, value is path.
177 _pkls - PKL objects for each PKL.
179 Read all the assets from the asset map.
182 /* Make a list of non-CPL/PKL assets so that we can resolve the references
185 vector<shared_ptr<Asset>> other_assets;
187 for (auto i: paths) {
188 auto path = _directory / i.second;
190 if (i.second.empty()) {
191 /* I can't see how this is valid, but it's
192 been seen in the wild with a DCP that
193 claims to come from ClipsterDCI 5.10.0.5.
196 notes->push_back ({VerificationNote::Type::WARNING, VerificationNote::Code::EMPTY_ASSET_PATH});
201 if (!boost::filesystem::exists(path)) {
203 notes->push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::MISSING_ASSET, path});
208 /* Find the <Type> for this asset from the PKL that contains the asset */
209 optional<string> pkl_type;
210 for (auto j: _pkls) {
211 pkl_type = j->type(i.first);
218 /* This asset is in the ASSETMAP but not mentioned in any PKL so we don't
219 * need to worry about it.
224 if (*pkl_type == CPL::static_pkl_type(*_standard) || *pkl_type == InteropSubtitleAsset::static_pkl_type(*_standard)) {
225 auto p = new xmlpp::DomParser;
227 p->parse_file (path.string());
228 } catch (std::exception& e) {
230 throw ReadError(String::compose("XML error in %1", path.string()), e.what());
233 auto const root = p->get_document()->get_root_node()->get_name();
236 if (root == "CompositionPlaylist") {
237 auto cpl = make_shared<CPL>(path);
238 if (_standard && cpl->standard() != _standard.get() && notes) {
239 notes->push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::MISMATCHED_STANDARD});
241 _cpls.push_back (cpl);
242 } else if (root == "DCSubtitle") {
243 if (_standard && _standard.get() == Standard::SMPTE && notes) {
244 notes->push_back (VerificationNote(VerificationNote::Type::ERROR, VerificationNote::Code::MISMATCHED_STANDARD));
246 other_assets.push_back (make_shared<InteropSubtitleAsset>(path));
249 *pkl_type == PictureAsset::static_pkl_type(*_standard) ||
250 *pkl_type == SoundAsset::static_pkl_type(*_standard) ||
251 *pkl_type == AtmosAsset::static_pkl_type(*_standard) ||
252 *pkl_type == SMPTESubtitleAsset::static_pkl_type(*_standard)
255 other_assets.push_back (asset_factory(path, ignore_incorrect_picture_mxf_type));
256 } else if (*pkl_type == FontAsset::static_pkl_type(*_standard)) {
257 other_assets.push_back (make_shared<FontAsset>(i.first, path));
258 } else if (*pkl_type == "image/png") {
259 /* It's an Interop PNG subtitle; let it go */
261 throw ReadError (String::compose("Unknown asset type %1 in PKL", *pkl_type));
265 resolve_refs (other_assets);
267 /* While we've got the ASSETMAP lets look and see if this DCP refers to things that are not in its ASSETMAP */
269 for (auto i: cpls()) {
270 for (auto j: i->reel_file_assets()) {
271 if (!j->asset_ref().resolved() && paths.find(j->asset_ref().id()) == paths.end()) {
272 notes->push_back (VerificationNote(VerificationNote::Type::WARNING, VerificationNote::Code::EXTERNAL_ASSET, j->asset_ref().id()));
281 DCP::resolve_refs (vector<shared_ptr<Asset>> assets)
283 for (auto i: cpls()) {
284 i->resolve_refs (assets);
290 DCP::equals (DCP const & other, EqualityOptions opt, NoteHandler note) const
293 auto b = other.cpls ();
295 if (a.size() != b.size()) {
296 note (NoteType::ERROR, String::compose ("CPL counts differ: %1 vs %2", a.size(), b.size()));
304 while (j != b.end() && !(*j)->equals (i, opt, note)) {
318 DCP::add (shared_ptr<CPL> cpl)
320 _cpls.push_back (cpl);
325 DCP::any_encrypted () const
327 for (auto i: cpls()) {
328 if (i->any_encrypted()) {
338 DCP::all_encrypted () const
340 for (auto i: cpls()) {
341 if (!i->all_encrypted()) {
351 DCP::add (DecryptedKDM const & kdm)
353 auto keys = kdm.keys ();
355 for (auto i: cpls()) {
356 for (auto const& j: kdm.keys()) {
357 if (j.cpl_id() == i->id()) {
365 /** Write the VOLINDEX file.
366 * @param standard DCP standard to use (INTEROP or SMPTE)
369 DCP::write_volindex (Standard standard) const
373 case Standard::INTEROP:
376 case Standard::SMPTE:
384 xmlpp::Element* root;
387 case Standard::INTEROP:
388 root = doc.create_root_node ("VolumeIndex", volindex_interop_ns);
390 case Standard::SMPTE:
391 root = doc.create_root_node ("VolumeIndex", volindex_smpte_ns);
397 root->add_child("Index")->add_child_text ("1");
398 doc.write_to_file_formatted (p.string (), "UTF-8");
403 DCP::write_assetmap (
404 Standard standard, string pkl_uuid, boost::filesystem::path pkl_path,
405 string issuer, string creator, string issue_date, string annotation_text
411 case Standard::INTEROP:
414 case Standard::SMPTE:
422 xmlpp::Element* root;
425 case Standard::INTEROP:
426 root = doc.create_root_node ("AssetMap", assetmap_interop_ns);
428 case Standard::SMPTE:
429 root = doc.create_root_node ("AssetMap", assetmap_smpte_ns);
435 root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
436 root->add_child("AnnotationText")->add_child_text (annotation_text);
439 case Standard::INTEROP:
440 root->add_child("VolumeCount")->add_child_text ("1");
441 root->add_child("IssueDate")->add_child_text (issue_date);
442 root->add_child("Issuer")->add_child_text (issuer);
443 root->add_child("Creator")->add_child_text (creator);
445 case Standard::SMPTE:
446 root->add_child("Creator")->add_child_text (creator);
447 root->add_child("VolumeCount")->add_child_text ("1");
448 root->add_child("IssueDate")->add_child_text (issue_date);
449 root->add_child("Issuer")->add_child_text (issuer);
455 auto asset_list = root->add_child ("AssetList");
457 auto asset = asset_list->add_child ("Asset");
458 asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
459 asset->add_child("PackingList")->add_child_text ("true");
460 auto chunk_list = asset->add_child ("ChunkList");
461 auto chunk = chunk_list->add_child ("Chunk");
462 chunk->add_child("Path")->add_child_text (pkl_path.filename().string());
463 chunk->add_child("VolumeIndex")->add_child_text ("1");
464 chunk->add_child("Offset")->add_child_text ("0");
465 chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (pkl_path)));
467 for (auto i: assets()) {
468 i->write_to_assetmap (asset_list, _directory);
471 doc.write_to_file_formatted (p.string (), "UTF-8");
481 string annotation_text,
482 shared_ptr<const CertificateChain> signer,
483 NameFormat name_format
487 throw MiscError ("Cannot write DCP with no CPLs.");
490 auto standard = std::accumulate (
491 std::next(_cpls.begin()), _cpls.end(), _cpls[0]->standard(),
492 [](Standard s, shared_ptr<CPL> c) {
493 if (s != c->standard()) {
494 throw MiscError ("Cannot make DCP with mixed Interop and SMPTE CPLs.");
500 for (auto i: cpls()) {
501 NameFormat::Map values;
503 i->write_xml (_directory / (name_format.get(values, "_" + i->id() + ".xml")), signer);
509 pkl = make_shared<PKL>(standard, annotation_text, issue_date, issuer, creator);
510 _pkls.push_back (pkl);
511 for (auto i: assets()) {
512 i->add_to_pkl (pkl, _directory);
515 pkl = _pkls.front ();
518 NameFormat::Map values;
520 auto pkl_path = _directory / name_format.get(values, "_" + pkl->id() + ".xml");
521 pkl->write (pkl_path, signer);
523 write_volindex (standard);
524 write_assetmap (standard, pkl->id(), pkl_path, issuer, creator, issue_date, annotation_text);
528 vector<shared_ptr<CPL>>
535 vector<shared_ptr<Asset>>
536 DCP::assets (bool ignore_unresolved) const
538 vector<shared_ptr<Asset>> assets;
539 for (auto i: cpls()) {
540 assets.push_back (i);
541 for (auto j: i->reel_file_assets()) {
542 if (ignore_unresolved && !j->asset_ref().resolved()) {
546 auto const id = j->asset_ref().id();
547 auto already_got = false;
548 for (auto k: assets) {
555 auto o = j->asset_ref().asset();
556 assets.push_back (o);
557 /* More Interop special-casing */
558 auto sub = dynamic_pointer_cast<InteropSubtitleAsset>(o);
560 sub->add_font_assets (assets);
570 /** Given a list of files that make up 1 or more DCPs, return the DCP directories */
571 vector<boost::filesystem::path>
572 DCP::directories_from_files (vector<boost::filesystem::path> files)
574 vector<boost::filesystem::path> d;
575 for (auto i: files) {
576 if (i.filename() == "ASSETMAP" || i.filename() == "ASSETMAP.xml") {
577 d.push_back (i.parent_path ());