Fix erroneous reports of unresolved assets when checking OV/VF pairs.
[libdcp.git] / src / dcp.cc
index bb4a712826ee272529f5d53aa262da46fd483666..2011f04e52cdb515af362abc8939268d3c8d9fa1 100644 (file)
 
     You should have received a copy of the GNU General Public License
     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
+
+    In addition, as a special exception, the copyright holders give
+    permission to link the code of portions of this program with the
+    OpenSSL library under certain conditions as described in each
+    individual source file, and distribute linked combinations
+    including the two.
+
+    You must obey the GNU General Public License in all respects
+    for all of the code used other than OpenSSL.  If you modify
+    file(s) with this exception, you may extend this exception to your
+    version of the file(s), but you are not obligated to do so.  If you
+    do not wish to do so, delete this exception statement from your
+    version.  If you delete this exception statement from all source
+    files in the program, then also delete it here.
 */
 
 /** @file  src/dcp.cc
@@ -24,6 +38,7 @@
 #include "raw_convert.h"
 #include "dcp.h"
 #include "sound_asset.h"
+#include "atmos_asset.h"
 #include "picture_asset.h"
 #include "interop_subtitle_asset.h"
 #include "smpte_subtitle_asset.h"
 #include "cpl.h"
 #include "certificate_chain.h"
 #include "compose.hpp"
-#include "AS_DCP.h"
 #include "decrypted_kdm.h"
 #include "decrypted_kdm_key.h"
 #include "dcp_assert.h"
 #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>
 #include <libxml++/libxml++.h>
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
 #include <boost/foreach.hpp>
-#include <iostream>
 
 using std::string;
 using std::list;
+using std::vector;
 using std::cout;
-using std::ostream;
 using std::make_pair;
 using std::map;
-using std::cout;
 using std::cerr;
 using std::exception;
 using boost::shared_ptr;
 using boost::dynamic_pointer_cast;
+using boost::optional;
 using boost::algorithm::starts_with;
 using namespace dcp;
 
 static string const assetmap_interop_ns = "http://www.digicine.com/PROTO-ASDCP-AM-20040311#";
 static string const assetmap_smpte_ns   = "http://www.smpte-ra.org/schemas/429-9/2007/AM";
-static string const pkl_interop_ns      = "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#";
-static string const pkl_smpte_ns        = "http://www.smpte-ra.org/schemas/429-8/2007/PKL";
-static string const volindex_interop_ns = "http://www.digicine.com/PROTO-ASDCP-AM-20040311#";
+static string const volindex_interop_ns = "http://www.digicine.com/PROTO-ASDCP-VL-20040311#";
 static string const volindex_smpte_ns   = "http://www.smpte-ra.org/schemas/429-9/2007/AM";
 
 DCP::DCP (boost::filesystem::path directory)
@@ -97,7 +111,7 @@ survivable_error (bool keep_going, dcp::DCP::ReadErrors* errors, T const & e)
 void
 DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mxf_type)
 {
-       /* Read the ASSETMAP */
+       /* Read the ASSETMAP and PKL */
 
        boost::filesystem::path asset_map_file;
        if (boost::filesystem::exists (_directory / "ASSETMAP")) {
@@ -121,6 +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;
+       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"));
@@ -129,15 +144,43 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
                if (starts_with (p, "file://")) {
                        p = p.substr (7);
                }
-               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;
+               }
+               }
        }
 
-       /* Read all the assets from the asset map */
-       /* XXX: I think we should be looking at the PKL here to decide type, not
-          the extension of the file.
-       */
+       if (pkl_paths.empty()) {
+               boost::throw_exception (XMLError ("No packing lists found in asset map"));
+       }
 
-       /* Make a list of non-CPL assets so that we can resolve the references
+       BOOST_FOREACH (boost::filesystem::path i, pkl_paths) {
+               _pkls.push_back (shared_ptr<PKL>(new PKL(_directory / i)));
+       }
+
+       /* 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.
+        */
+
+       /* Make a list of non-CPL/PKL assets so that we can resolve the references
           from the CPLs.
        */
        list<shared_ptr<Asset> > other_assets;
@@ -145,18 +188,43 @@ 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;
                }
 
-               if (boost::filesystem::extension (path) == ".xml") {
+               /* 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) {
+                       /* 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());
                        } catch (std::exception& e) {
                                delete p;
-                               continue;
+                               throw DCPReadError(String::compose("XML error in %1", path.string()), e.what());
                        }
 
                        string const root = p->get_document()->get_root_node()->get_name ();
@@ -174,47 +242,20 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
                                }
                                other_assets.push_back (shared_ptr<InteropSubtitleAsset> (new InteropSubtitleAsset (path)));
                        }
-               } else if (boost::filesystem::extension (path) == ".mxf") {
-
-                       /* 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;
-                               default:
-                                       throw DCPReadError ("Unknown MXF essence type");
-                       }
-               } else if (boost::filesystem::extension (path) == ".ttf") {
+               } 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)
+                       ) {
+
+                       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));
                }
        }
 
@@ -276,6 +317,10 @@ DCP::encrypted () const
        return false;
 }
 
+/** Add a KDM to decrypt this DCP.  This method must be called after DCP::read()
+ *  or the KDM you specify will be ignored.
+ *  @param kdm KDM to use.
+ */
 void
 DCP::add (DecryptedKDM const & kdm)
 {
@@ -290,47 +335,6 @@ DCP::add (DecryptedKDM const & kdm)
        }
 }
 
-boost::filesystem::path
-DCP::write_pkl (Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const CertificateChain> signer) const
-{
-       boost::filesystem::path p = _directory;
-       p /= String::compose ("pkl_%1.xml", pkl_uuid);
-
-       xmlpp::Document doc;
-       xmlpp::Element* pkl;
-       if (standard == INTEROP) {
-               pkl = doc.create_root_node("PackingList", pkl_interop_ns);
-       } else {
-               pkl = doc.create_root_node("PackingList", pkl_smpte_ns);
-       }
-
-       if (signer) {
-               pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
-       }
-
-       pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
-
-       /* XXX: this is a bit of a hack */
-       DCP_ASSERT (cpls().size() > 0);
-       pkl->add_child("AnnotationText")->add_child_text (cpls().front()->annotation_text ());
-
-       pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
-       pkl->add_child("Issuer")->add_child_text (metadata.issuer);
-       pkl->add_child("Creator")->add_child_text (metadata.creator);
-
-       xmlpp::Element* asset_list = pkl->add_child("AssetList");
-       BOOST_FOREACH (shared_ptr<Asset> i, assets ()) {
-               i->write_to_pkl (asset_list, _directory, standard);
-       }
-
-       if (signer) {
-               signer->sign (pkl, standard);
-       }
-
-       doc.write_to_file (p.string (), "UTF-8");
-       return p.string ();
-}
-
 /** Write the VOLINDEX file.
  *  @param standard DCP standard to use (INTEROP or SMPTE)
  */
@@ -364,11 +368,11 @@ 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
-DCP::write_assetmap (Standard standard, string pkl_uuid, int pkl_length, XMLMetadata metadata) const
+DCP::write_assetmap (Standard standard, string pkl_uuid, boost::filesystem::path pkl_path, XMLMetadata metadata) const
 {
        boost::filesystem::path p = _directory;
 
@@ -398,7 +402,7 @@ DCP::write_assetmap (Standard standard, string pkl_uuid, int pkl_length, XMLMeta
        }
 
        root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
-       root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
+       root->add_child("AnnotationText")->add_child_text (metadata.annotation_text);
 
        switch (standard) {
        case INTEROP:
@@ -424,17 +428,16 @@ DCP::write_assetmap (Standard standard, string pkl_uuid, int pkl_length, XMLMeta
        asset->add_child("PackingList")->add_child_text ("true");
        xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
        xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
-       chunk->add_child("Path")->add_child_text ("pkl_" + pkl_uuid + ".xml");
+       chunk->add_child("Path")->add_child_text (pkl_path.filename().string());
        chunk->add_child("VolumeIndex")->add_child_text ("1");
        chunk->add_child("Offset")->add_child_text ("0");
-       chunk->add_child("Length")->add_child_text (raw_convert<string> (pkl_length));
+       chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (pkl_path)));
 
        BOOST_FOREACH (shared_ptr<Asset> i, assets ()) {
                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.
@@ -446,19 +449,35 @@ void
 DCP::write_xml (
        Standard standard,
        XMLMetadata metadata,
-       shared_ptr<const CertificateChain> signer
+       shared_ptr<const CertificateChain> signer,
+       NameFormat name_format
        )
 {
        BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
-               string const filename = "cpl_" + i->id() + ".xml";
-               i->write_xml (_directory / filename, standard, signer);
+               NameFormat::Map values;
+               values['t'] = "cpl";
+               i->write_xml (_directory / (name_format.get(values, "_" + i->id() + ".xml")), standard, signer);
+       }
+
+       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);
+               }
+        } else {
+               pkl = _pkls.front ();
        }
 
-       string const pkl_uuid = make_uuid ();
-       boost::filesystem::path const pkl_path = write_pkl (standard, pkl_uuid, metadata, signer);
+       NameFormat::Map values;
+       values['t'] = "pkl";
+       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_uuid, boost::filesystem::file_size (pkl_path), metadata);
+       write_assetmap (standard, pkl->id(), pkl_path, metadata);
 }
 
 list<shared_ptr<CPL> >
@@ -467,14 +486,20 @@ DCP::cpls () const
        return _cpls;
 }
 
-/** @return All assets (including CPLs) */
+/** @param ignore_unresolved true to silently ignore unresolved assets, otherwise
+ *  an exception is thrown if they are found.
+ *  @return All assets (including CPLs).
+ */
 list<shared_ptr<Asset> >
-DCP::assets () const
+DCP::assets (bool ignore_unresolved) const
 {
        list<shared_ptr<Asset> > assets;
        BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
                assets.push_back (i);
                BOOST_FOREACH (shared_ptr<const ReelAsset> j, i->reel_assets ()) {
+                       if (ignore_unresolved && !j->asset_ref().resolved()) {
+                               continue;
+                       }
                        shared_ptr<Asset> o = j->asset_ref().asset ();
                        assets.push_back (o);
                        /* More Interop special-casing */
@@ -487,3 +512,16 @@ DCP::assets () const
 
        return assets;
 }
+
+/** Given a list of files that make up 1 or more DCPs, return the DCP directories */
+vector<boost::filesystem::path>
+DCP::directories_from_files (vector<boost::filesystem::path> files)
+{
+       vector<boost::filesystem::path> d;
+       BOOST_FOREACH (boost::filesystem::path i, files) {
+               if (i.filename() == "ASSETMAP" || i.filename() == "ASSETMAP.xml") {
+                       d.push_back (i.parent_path ());
+               }
+       }
+       return d;
+}