Fix erroneous reports of unresolved assets when checking OV/VF pairs.
[libdcp.git] / src / dcp.cc
index 232c144304d831bd7ee7241ba210723893ce952f..2011f04e52cdb515af362abc8939268d3c8d9fa1 100644 (file)
@@ -57,6 +57,7 @@
 #include "reel_asset.h"
 #include "font_asset.h"
 #include "pkl.h"
+#include "asset_factory.h"
 #include <asdcp/AS_DCP.h>
 #include <xmlsec/xmldsig.h>
 #include <xmlsec/app.h>
@@ -134,7 +135,7 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
 
        list<shared_ptr<cxml::Node> > asset_nodes = asset_map.node_child("AssetList")->node_children ("Asset");
        map<string, boost::filesystem::path> paths;
-       optional<boost::filesystem::path> pkl_path;
+       list<boost::filesystem::path> pkl_paths;
        BOOST_FOREACH (shared_ptr<cxml::Node> i, asset_nodes) {
                if (i->node_child("ChunkList")->node_children("Chunk").size() != 1) {
                        boost::throw_exception (XMLError ("unsupported asset chunk count"));
@@ -143,21 +144,41 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
                if (starts_with (p, "file://")) {
                        p = p.substr (7);
                }
-               optional<string> pkl_bool = i->optional_string_child("PackingList");
-               if (pkl_bool && *pkl_bool == "true") {
-                       pkl_path = p;
-               } else {
-                       paths.insert (make_pair (remove_urn_uuid (i->string_child ("Id")), p));
+               switch (*_standard) {
+               case INTEROP:
+                       if (i->optional_node_child("PackingList")) {
+                               pkl_paths.push_back (p);
+                       } else {
+                               paths.insert (make_pair (remove_urn_uuid (i->string_child ("Id")), p));
+                       }
+                       break;
+               case SMPTE:
+               {
+                       optional<string> pkl_bool = i->optional_string_child("PackingList");
+                       if (pkl_bool && *pkl_bool == "true") {
+                               pkl_paths.push_back (p);
+                       } else {
+                               paths.insert (make_pair (remove_urn_uuid (i->string_child ("Id")), p));
+                       }
+                       break;
                }
+               }
+       }
+
+       if (pkl_paths.empty()) {
+               boost::throw_exception (XMLError ("No packing lists found in asset map"));
        }
 
-       if (!pkl_path) {
-               boost::throw_exception (XMLError ("No packing list found in asset map"));
+       BOOST_FOREACH (boost::filesystem::path i, pkl_paths) {
+               _pkls.push_back (shared_ptr<PKL>(new PKL(_directory / i)));
        }
 
-       _pkl.reset (new PKL (_directory / *pkl_path));
+       /* Now we have:
+            paths - files in the DCP that are not PKLs.
+            _pkls - PKL objects for each PKL.
 
-       /* Read all the assets from the asset map */
+          Read all the assets from the asset map.
+        */
 
        /* Make a list of non-CPL/PKL assets so that we can resolve the references
           from the CPLs.
@@ -167,14 +188,37 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
        for (map<string, boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
                boost::filesystem::path path = _directory / i->second;
 
-               if (!boost::filesystem::exists (path)) {
+               if (i->second.empty()) {
+                       /* I can't see how this is valid, but it's
+                          been seen in the wild with a DCP that
+                          claims to come from ClipsterDCI 5.10.0.5.
+                       */
+                       survivable_error (keep_going, errors, EmptyAssetPathError(i->first));
+                       continue;
+               }
+
+               if (!boost::filesystem::exists(path)) {
                        survivable_error (keep_going, errors, MissingAssetError (path));
                        continue;
                }
 
-               string const pkl_type = _pkl->type(i->first);
+               /* Find the <Type> for this asset from the PKL that contains the asset */
+               optional<string> pkl_type;
+               BOOST_FOREACH (shared_ptr<PKL> j, _pkls) {
+                       pkl_type = j->type(i->first);
+                       if (pkl_type) {
+                               break;
+                       }
+               }
 
-               if (pkl_type == CPL::static_pkl_type(*_standard) || pkl_type == InteropSubtitleAsset::static_pkl_type(*_standard)) {
+               if (!pkl_type) {
+                       /* This asset is in the ASSETMAP but not mentioned in any PKL so we don't
+                        * need to worry about it.
+                        */
+                       continue;
+               }
+
+               if (*pkl_type == CPL::static_pkl_type(*_standard) || *pkl_type == InteropSubtitleAsset::static_pkl_type(*_standard)) {
                        xmlpp::DomParser* p = new xmlpp::DomParser;
                        try {
                                p->parse_file (path.string());
@@ -199,54 +243,19 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
                                other_assets.push_back (shared_ptr<InteropSubtitleAsset> (new InteropSubtitleAsset (path)));
                        }
                } else if (
-                       pkl_type == PictureAsset::static_pkl_type(*_standard) ||
-                       pkl_type == SoundAsset::static_pkl_type(*_standard) ||
-                       pkl_type == AtmosAsset::static_pkl_type(*_standard) ||
-                       pkl_type == SMPTESubtitleAsset::static_pkl_type(*_standard)
+                       *pkl_type == PictureAsset::static_pkl_type(*_standard) ||
+                       *pkl_type == SoundAsset::static_pkl_type(*_standard) ||
+                       *pkl_type == AtmosAsset::static_pkl_type(*_standard) ||
+                       *pkl_type == SMPTESubtitleAsset::static_pkl_type(*_standard)
                        ) {
 
-                       /* XXX: asdcplib does not appear to support discovery of read MXFs standard
-                          (Interop / SMPTE)
-                       */
-
-                       ASDCP::EssenceType_t type;
-                       if (ASDCP::EssenceType (path.string().c_str(), type) != ASDCP::RESULT_OK) {
-                               throw DCPReadError ("Could not find essence type");
-                       }
-                       switch (type) {
-                               case ASDCP::ESS_UNKNOWN:
-                               case ASDCP::ESS_MPEG2_VES:
-                                       throw DCPReadError ("MPEG2 video essences are not supported");
-                               case ASDCP::ESS_JPEG_2000:
-                                       try {
-                                               other_assets.push_back (shared_ptr<MonoPictureAsset> (new MonoPictureAsset (path)));
-                                       } catch (dcp::MXFFileError& e) {
-                                               if (ignore_incorrect_picture_mxf_type && e.number() == ASDCP::RESULT_SFORMAT) {
-                                                       /* Tried to load it as mono but the error says it's stereo; try that instead */
-                                                       other_assets.push_back (shared_ptr<StereoPictureAsset> (new StereoPictureAsset (path)));
-                                               } else {
-                                                       throw;
-                                               }
-                                       }
-                                       break;
-                               case ASDCP::ESS_PCM_24b_48k:
-                               case ASDCP::ESS_PCM_24b_96k:
-                                       other_assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (path)));
-                                       break;
-                               case ASDCP::ESS_JPEG_2000_S:
-                                       other_assets.push_back (shared_ptr<StereoPictureAsset> (new StereoPictureAsset (path)));
-                                       break;
-                               case ASDCP::ESS_TIMED_TEXT:
-                                       other_assets.push_back (shared_ptr<SMPTESubtitleAsset> (new SMPTESubtitleAsset (path)));
-                                       break;
-                               case ASDCP::ESS_DCDATA_DOLBY_ATMOS:
-                                       other_assets.push_back (shared_ptr<AtmosAsset> (new AtmosAsset (path)));
-                                       break;
-                               default:
-                                       throw DCPReadError (String::compose ("Unknown MXF essence type %1 in %2", int(type), path.string()));
-                       }
-               } else if (pkl_type == FontAsset::static_pkl_type(*_standard)) {
+                       other_assets.push_back (asset_factory(path, ignore_incorrect_picture_mxf_type));
+               } else if (*pkl_type == FontAsset::static_pkl_type(*_standard)) {
                        other_assets.push_back (shared_ptr<FontAsset> (new FontAsset (i->first, path)));
+               } else if (*pkl_type == "image/png") {
+                       /* It's an Interop PNG subtitle; let it go */
+               } else {
+                       throw DCPReadError (String::compose("Unknown asset type %1 in PKL", *pkl_type));
                }
        }
 
@@ -359,7 +368,7 @@ DCP::write_volindex (Standard standard) const
        }
 
        root->add_child("Index")->add_child_text ("1");
-       doc.write_to_file (p.string (), "UTF-8");
+       doc.write_to_file_formatted (p.string (), "UTF-8");
 }
 
 void
@@ -428,8 +437,7 @@ DCP::write_assetmap (Standard standard, string pkl_uuid, boost::filesystem::path
                i->write_to_assetmap (asset_list, _directory);
        }
 
-       /* This must not be the _formatted version otherwise signature digests will be wrong */
-       doc.write_to_file (p.string (), "UTF-8");
+       doc.write_to_file_formatted (p.string (), "UTF-8");
 }
 
 /** Write all the XML files for this DCP.
@@ -451,20 +459,25 @@ DCP::write_xml (
                i->write_xml (_directory / (name_format.get(values, "_" + i->id() + ".xml")), standard, signer);
        }
 
-       if (!_pkl) {
-               _pkl.reset (new PKL (standard, metadata.annotation_text, metadata.issue_date, metadata.issuer, metadata.creator));
+       shared_ptr<PKL> pkl;
+
+       if (_pkls.empty()) {
+               pkl.reset (new PKL (standard, metadata.annotation_text, metadata.issue_date, metadata.issuer, metadata.creator));
+               _pkls.push_back (pkl);
                BOOST_FOREACH (shared_ptr<Asset> i, assets ()) {
-                       i->add_to_pkl (_pkl, _directory);
+                       i->add_to_pkl (pkl, _directory);
                }
+        } else {
+               pkl = _pkls.front ();
        }
 
        NameFormat::Map values;
        values['t'] = "pkl";
-       boost::filesystem::path pkl_path = _directory / name_format.get(values, "_" + _pkl->id() + ".xml");
-       _pkl->write (pkl_path, signer);
+       boost::filesystem::path pkl_path = _directory / name_format.get(values, "_" + pkl->id() + ".xml");
+       pkl->write (pkl_path, signer);
 
        write_volindex (standard);
-       write_assetmap (standard, _pkl->id(), pkl_path, metadata);
+       write_assetmap (standard, pkl->id(), pkl_path, metadata);
 }
 
 list<shared_ptr<CPL> >