Add wrappers around boost::filesystem methods that handle the
[libdcp.git] / src / cpl.cc
index 92fa70445d46f7de31cb3208780956c0b3c4d213..5467fef3b1db0b38ebf859e5d4adf623e7342dd7 100644 (file)
@@ -41,6 +41,8 @@
 #include "compose.hpp"
 #include "cpl.h"
 #include "dcp_assert.h"
+#include "equality_options.h"
+#include "filesystem.h"
 #include "local_time.h"
 #include "metadata.h"
 #include "raw_convert.h"
@@ -51,6 +53,7 @@
 #include "reel_sound_asset.h"
 #include "reel_subtitle_asset.h"
 #include "util.h"
+#include "version.h"
 #include "warnings.h"
 #include "xml.h"
 LIBDCP_DISABLE_WARNINGS
@@ -87,8 +90,8 @@ static string const smpte_335_ns = "http://www.smpte-ra.org/reg/335/2012";
 
 CPL::CPL (string annotation_text, ContentKind content_kind, Standard standard)
        /* default _content_title_text to annotation_text */
-       : _issuer ("libdcp" LIBDCP_VERSION)
-       , _creator ("libdcp" LIBDCP_VERSION)
+       : _issuer("libdcp", dcp::version)
+       , _creator("libdcp", dcp::version)
        , _issue_date (LocalTime().as_string())
        , _annotation_text (annotation_text)
        , _content_title_text (annotation_text)
@@ -106,7 +109,7 @@ CPL::CPL (boost::filesystem::path file)
        , _content_kind (ContentKind::FEATURE)
 {
        cxml::Document f ("CompositionPlaylist");
-       f.read_file (file);
+       f.read_file(dcp::filesystem::fix_long_path(file));
 
        if (f.namespace_uri() == cpl_interop_ns) {
                _standard = Standard::INTEROP;
@@ -122,7 +125,8 @@ CPL::CPL (boost::filesystem::path file)
        _creator = f.optional_string_child("Creator").get_value_or("");
        _issue_date = f.string_child ("IssueDate");
        _content_title_text = f.string_child ("ContentTitleText");
-       _content_kind = content_kind_from_string (f.string_child ("ContentKind"));
+       auto content_kind = f.node_child("ContentKind");
+       _content_kind = ContentKind(content_kind->content(), content_kind->optional_string_attribute("scope"));
        shared_ptr<cxml::Node> content_version = f.optional_node_child ("ContentVersion");
        if (content_version) {
                /* XXX: SMPTE should insist that Id is present */
@@ -138,10 +142,8 @@ CPL::CPL (boost::filesystem::path file)
                throw XMLError ("Missing ContentVersion tag in CPL");
        }
        auto rating_list = f.node_child ("RatingList");
-       if (rating_list) {
-               for (auto i: rating_list->node_children("Rating")) {
-                       _ratings.push_back (Rating(i));
-               }
+       for (auto i: rating_list->node_children("Rating")) {
+               _ratings.push_back (Rating(i));
        }
 
        for (auto i: f.node_child("ReelList")->node_children("Reel")) {
@@ -149,14 +151,13 @@ CPL::CPL (boost::filesystem::path file)
        }
 
        auto reel_list = f.node_child ("ReelList");
-       if (reel_list) {
-               auto reels = reel_list->node_children("Reel");
-               if (!reels.empty()) {
-                       auto asset_list = reels.front()->node_child("AssetList");
-                       auto metadata = asset_list->optional_node_child("CompositionMetadataAsset");
-                       if (metadata) {
-                               read_composition_metadata_asset (metadata);
-                       }
+       auto reels = reel_list->node_children("Reel");
+       if (!reels.empty()) {
+               auto asset_list = reels.front()->node_child("AssetList");
+               auto metadata = asset_list->optional_node_child("CompositionMetadataAsset");
+               if (metadata) {
+                       read_composition_metadata_asset (metadata);
+                       _read_composition_metadata = true;
                }
        }
 
@@ -176,7 +177,14 @@ CPL::add (std::shared_ptr<Reel> reel)
 
 
 void
-CPL::write_xml (boost::filesystem::path file, shared_ptr<const CertificateChain> signer) const
+CPL::set (std::vector<std::shared_ptr<Reel>> reels)
+{
+       _reels = reels;
+}
+
+
+void
+CPL::write_xml(boost::filesystem::path file, shared_ptr<const CertificateChain> signer, bool include_mca_subdescriptors) const
 {
        xmlpp::Document doc;
        xmlpp::Element* root;
@@ -194,7 +202,11 @@ CPL::write_xml (boost::filesystem::path file, shared_ptr<const CertificateChain>
        root->add_child("Issuer")->add_child_text (_issuer);
        root->add_child("Creator")->add_child_text (_creator);
        root->add_child("ContentTitleText")->add_child_text (_content_title_text);
-       root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
+       auto content_kind = root->add_child("ContentKind");
+       content_kind->add_child_text(_content_kind.name());
+       if (_content_kind.scope()) {
+               content_kind->set_attribute("scope", *_content_kind.scope());
+       }
        if (_content_versions.empty()) {
                ContentVersion cv;
                cv.as_xml (root);
@@ -217,7 +229,7 @@ CPL::write_xml (boost::filesystem::path file, shared_ptr<const CertificateChain>
        for (auto i: _reels) {
                auto asset_list = i->write_to_cpl (reel_list, _standard);
                if (first && _standard == Standard::SMPTE) {
-                       maybe_write_composition_metadata_asset (asset_list);
+                       maybe_write_composition_metadata_asset(asset_list, include_mca_subdescriptors);
                        first = false;
                }
        }
@@ -228,7 +240,7 @@ CPL::write_xml (boost::filesystem::path file, shared_ptr<const CertificateChain>
                signer->sign (root, _standard);
        }
 
-       doc.write_to_file_formatted (file.string(), "UTF-8");
+       doc.write_to_file_formatted(dcp::filesystem::fix_long_path(file).string(), "UTF-8");
 
        set_file (file);
 }
@@ -239,9 +251,13 @@ CPL::read_composition_metadata_asset (cxml::ConstNodePtr node)
 {
        _cpl_metadata_id = remove_urn_uuid(node->string_child("Id"));
 
-       auto fctt = node->node_child("FullContentTitleText");
-       _full_content_title_text = fctt->content();
-       _full_content_title_text_language = fctt->optional_string_attribute("language");
+       /* FullContentTitleText is compulsory but in DoM #2295 we saw a commercial tool which
+        * apparently didn't include it, so as usual we have to be defensive.
+        */
+       if (auto fctt = node->optional_node_child("FullContentTitleText")) {
+               _full_content_title_text = fctt->content();
+               _full_content_title_text_language = fctt->optional_string_attribute("language");
+       }
 
        _release_territory = node->optional_string_child("ReleaseTerritory");
        if (_release_territory) {
@@ -274,7 +290,16 @@ CPL::read_composition_metadata_asset (cxml::ConstNodePtr node)
                _luminance = Luminance (lum);
        }
 
-       _main_sound_configuration = node->optional_string_child("MainSoundConfiguration");
+       if (auto msc = node->optional_string_child("MainSoundConfiguration")) {
+               try {
+                       _main_sound_configuration = MainSoundConfiguration(*msc);
+               } catch (MainSoundConfigurationError& e) {
+                       /* With Interop DCPs this node may not make any sense, but that's OK */
+                       if (_standard == dcp::Standard::SMPTE) {
+                               throw e;
+                       }
+               }
+       }
 
        auto sr = node->optional_string_child("MainSoundSampleRate");
        if (sr) {
@@ -284,10 +309,12 @@ CPL::read_composition_metadata_asset (cxml::ConstNodePtr node)
                _main_sound_sample_rate = raw_convert<int>(sr_bits[0]);
        }
 
-       _main_picture_stored_area = dcp::Size (
-               node->node_child("MainPictureStoredArea")->number_child<int>("Width"),
-               node->node_child("MainPictureStoredArea")->number_child<int>("Height")
-               );
+       if (_standard == dcp::Standard::SMPTE) {
+               _main_picture_stored_area = dcp::Size (
+                       node->node_child("MainPictureStoredArea")->number_child<int>("Width"),
+                       node->node_child("MainPictureStoredArea")->number_child<int>("Height")
+                       );
+       }
 
        _main_picture_active_area = dcp::Size (
                node->node_child("MainPictureActiveArea")->number_child<int>("Width"),
@@ -316,6 +343,94 @@ CPL::read_composition_metadata_asset (cxml::ConstNodePtr node)
                        _additional_subtitle_languages.push_back (sll_split[i]);
                }
        }
+
+       auto eml = node->optional_node_child ("ExtensionMetadataList");
+       if (eml) {
+               for (auto i: eml->node_children("ExtensionMetadata")) {
+                       auto name = i->optional_string_child("Name");
+                       if (name && *name == "Sign Language Video") {
+                               auto property_list = i->node_child("PropertyList");
+                               for (auto j: property_list->node_children("Property")) {
+                                       auto name = j->optional_string_child("Name");
+                                       auto value = j->optional_string_child("Value");
+                                       if (name && value && *name == "Language Tag") {
+                                               _sign_language_video_language = *value;
+                                       }
+                               }
+                       }
+               }
+       }
+}
+
+
+void
+CPL::write_mca_subdescriptors(xmlpp::Element* parent, shared_ptr<const SoundAsset> asset) const
+{
+       auto reader = asset->start_read ();
+       ASDCP::MXF::SoundfieldGroupLabelSubDescriptor* soundfield;
+       ASDCP::Result_t r = reader->reader()->OP1aHeader().GetMDObjectByType(
+               asdcp_smpte_dict->ul(ASDCP::MDD_SoundfieldGroupLabelSubDescriptor),
+               reinterpret_cast<ASDCP::MXF::InterchangeObject**>(&soundfield)
+               );
+       if (KM_SUCCESS(r)) {
+               auto mca_subs = parent->add_child("mca:MCASubDescriptors");
+               mca_subs->set_namespace_declaration (mca_sub_descriptors_ns, "mca");
+               mca_subs->set_namespace_declaration (smpte_395_ns, "r0");
+               mca_subs->set_namespace_declaration (smpte_335_ns, "r1");
+               auto sf = mca_subs->add_child("SoundfieldGroupLabelSubDescriptor", "r0");
+               char buffer[64];
+               soundfield->InstanceUID.EncodeString(buffer, sizeof(buffer));
+               sf->add_child("InstanceID", "r1")->add_child_text("urn:uuid:" + string(buffer));
+               soundfield->MCALabelDictionaryID.EncodeString(buffer, sizeof(buffer));
+               sf->add_child("MCALabelDictionaryID", "r1")->add_child_text("urn:smpte:ul:" + string(buffer));
+               soundfield->MCALinkID.EncodeString(buffer, sizeof(buffer));
+               sf->add_child("MCALinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
+               soundfield->MCATagSymbol.EncodeString(buffer, sizeof(buffer));
+               sf->add_child("MCATagSymbol", "r1")->add_child_text(buffer);
+               if (!soundfield->MCATagName.empty()) {
+                       soundfield->MCATagName.get().EncodeString(buffer, sizeof(buffer));
+                       sf->add_child("MCATagName", "r1")->add_child_text(buffer);
+               }
+               if (!soundfield->RFC5646SpokenLanguage.empty()) {
+                       soundfield->RFC5646SpokenLanguage.get().EncodeString(buffer, sizeof(buffer));
+                       sf->add_child("RFC5646SpokenLanguage", "r1")->add_child_text(buffer);
+               }
+
+               /* Find the MCA subdescriptors in the MXF so that we can also write them here */
+               list<ASDCP::MXF::InterchangeObject*> channels;
+               auto r = reader->reader()->OP1aHeader().GetMDObjectsByType(
+                       asdcp_smpte_dict->ul(ASDCP::MDD_AudioChannelLabelSubDescriptor),
+                       channels
+                       );
+
+               for (auto i: channels) {
+                       auto channel = reinterpret_cast<ASDCP::MXF::AudioChannelLabelSubDescriptor*>(i);
+                       auto ch = mca_subs->add_child("AudioChannelLabelSubDescriptor", "r0");
+                       channel->InstanceUID.EncodeString(buffer, sizeof(buffer));
+                       ch->add_child("InstanceID", "r1")->add_child_text("urn:uuid:" + string(buffer));
+                       channel->MCALabelDictionaryID.EncodeString(buffer, sizeof(buffer));
+                       ch->add_child("MCALabelDictionaryID", "r1")->add_child_text("urn:smpte:ul:" + string(buffer));
+                       channel->MCALinkID.EncodeString(buffer, sizeof(buffer));
+                       ch->add_child("MCALinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
+                       channel->MCATagSymbol.EncodeString(buffer, sizeof(buffer));
+                       ch->add_child("MCATagSymbol", "r1")->add_child_text(buffer);
+                       if (!channel->MCATagName.empty()) {
+                               channel->MCATagName.get().EncodeString(buffer, sizeof(buffer));
+                               ch->add_child("MCATagName", "r1")->add_child_text(buffer);
+                       }
+                       if (!channel->MCAChannelID.empty()) {
+                               ch->add_child("MCAChannelID", "r1")->add_child_text(raw_convert<string>(channel->MCAChannelID.get()));
+                       }
+                       if (!channel->RFC5646SpokenLanguage.empty()) {
+                               channel->RFC5646SpokenLanguage.get().EncodeString(buffer, sizeof(buffer));
+                               ch->add_child("RFC5646SpokenLanguage", "r1")->add_child_text(buffer);
+                       }
+                       if (!channel->SoundfieldGroupLinkID.empty()) {
+                               channel->SoundfieldGroupLinkID.get().EncodeString(buffer, sizeof(buffer));
+                               ch->add_child("SoundfieldGroupLinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
+                       }
+               }
+       }
 }
 
 
@@ -324,7 +439,7 @@ CPL::read_composition_metadata_asset (cxml::ConstNodePtr node)
  *  is missing this method will do nothing.
  */
 void
-CPL::maybe_write_composition_metadata_asset (xmlpp::Element* node) const
+CPL::maybe_write_composition_metadata_asset(xmlpp::Element* node, bool include_mca_subdescriptors) const
 {
        if (
                !_main_sound_configuration ||
@@ -388,7 +503,9 @@ CPL::maybe_write_composition_metadata_asset (xmlpp::Element* node) const
                _luminance->as_xml (meta, "meta");
        }
 
-       meta->add_child("MainSoundConfiguration", "meta")->add_child_text(*_main_sound_configuration);
+       if (_main_sound_configuration) {
+               meta->add_child("MainSoundConfiguration", "meta")->add_child_text(_main_sound_configuration->to_string());
+       }
        meta->add_child("MainSoundSampleRate", "meta")->add_child_text(raw_convert<string>(*_main_sound_sample_rate) + " 1");
 
        auto stored = meta->add_child("MainPictureStoredArea", "meta");
@@ -437,73 +554,14 @@ CPL::maybe_write_composition_metadata_asset (xmlpp::Element* node) const
        /* SMPTE Bv2.1 8.6.3 */
        add_extension_metadata ("http://isdcf.com/ns/cplmd/app", "Application", "DCP Constraints Profile", "SMPTE-RDD-52:2020-Bv2.1");
 
+       if (_sign_language_video_language) {
+               add_extension_metadata ("http://isdcf.com/2017/10/SignLanguageVideo", "Sign Language Video", "Language Tag", *_sign_language_video_language);
+       }
+
        if (_reels.front()->main_sound()) {
                auto asset = _reels.front()->main_sound()->asset();
-               if (asset) {
-                       auto reader = asset->start_read ();
-                       ASDCP::MXF::SoundfieldGroupLabelSubDescriptor* soundfield;
-                       ASDCP::Result_t r = reader->reader()->OP1aHeader().GetMDObjectByType(
-                               asdcp_smpte_dict->ul(ASDCP::MDD_SoundfieldGroupLabelSubDescriptor),
-                               reinterpret_cast<ASDCP::MXF::InterchangeObject**>(&soundfield)
-                               );
-                       if (KM_SUCCESS(r)) {
-                               auto mca_subs = meta->add_child("mca:MCASubDescriptors");
-                               mca_subs->set_namespace_declaration (mca_sub_descriptors_ns, "mca");
-                               mca_subs->set_namespace_declaration (smpte_395_ns, "r0");
-                               mca_subs->set_namespace_declaration (smpte_335_ns, "r1");
-                               auto sf = mca_subs->add_child("SoundfieldGroupLabelSubDescriptor", "r0");
-                               char buffer[64];
-                               soundfield->InstanceUID.EncodeString(buffer, sizeof(buffer));
-                               sf->add_child("InstanceID", "r1")->add_child_text("urn:uuid:" + string(buffer));
-                               soundfield->MCALabelDictionaryID.EncodeString(buffer, sizeof(buffer));
-                               sf->add_child("MCALabelDictionaryID", "r1")->add_child_text("urn:smpte:ul:" + string(buffer));
-                               soundfield->MCALinkID.EncodeString(buffer, sizeof(buffer));
-                               sf->add_child("MCALinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
-                               soundfield->MCATagSymbol.EncodeString(buffer, sizeof(buffer));
-                               sf->add_child("MCATagSymbol", "r1")->add_child_text(buffer);
-                               if (!soundfield->MCATagName.empty()) {
-                                       soundfield->MCATagName.get().EncodeString(buffer, sizeof(buffer));
-                                       sf->add_child("MCATagName", "r1")->add_child_text(buffer);
-                               }
-                               if (!soundfield->RFC5646SpokenLanguage.empty()) {
-                                       soundfield->RFC5646SpokenLanguage.get().EncodeString(buffer, sizeof(buffer));
-                                       sf->add_child("RFC5646SpokenLanguage", "r1")->add_child_text(buffer);
-                               }
-
-                               list<ASDCP::MXF::InterchangeObject*> channels;
-                               auto r = reader->reader()->OP1aHeader().GetMDObjectsByType(
-                                       asdcp_smpte_dict->ul(ASDCP::MDD_AudioChannelLabelSubDescriptor),
-                                       channels
-                                       );
-
-                               for (auto i: channels) {
-                                       auto channel = reinterpret_cast<ASDCP::MXF::AudioChannelLabelSubDescriptor*>(i);
-                                       auto ch = mca_subs->add_child("AudioChannelLabelSubDescriptor", "r0");
-                                       channel->InstanceUID.EncodeString(buffer, sizeof(buffer));
-                                       ch->add_child("InstanceID", "r1")->add_child_text("urn:uuid:" + string(buffer));
-                                       channel->MCALabelDictionaryID.EncodeString(buffer, sizeof(buffer));
-                                       ch->add_child("MCALabelDictionaryID", "r1")->add_child_text("urn:smpte:ul:" + string(buffer));
-                                       channel->MCALinkID.EncodeString(buffer, sizeof(buffer));
-                                       ch->add_child("MCALinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
-                                       channel->MCATagSymbol.EncodeString(buffer, sizeof(buffer));
-                                       ch->add_child("MCATagSymbol", "r1")->add_child_text(buffer);
-                                       if (!channel->MCATagName.empty()) {
-                                               channel->MCATagName.get().EncodeString(buffer, sizeof(buffer));
-                                               ch->add_child("MCATagName", "r1")->add_child_text(buffer);
-                                       }
-                                       if (!channel->MCAChannelID.empty()) {
-                                               ch->add_child("MCAChannelID", "r1")->add_child_text(raw_convert<string>(channel->MCAChannelID.get()));
-                                       }
-                                       if (!channel->RFC5646SpokenLanguage.empty()) {
-                                               channel->RFC5646SpokenLanguage.get().EncodeString(buffer, sizeof(buffer));
-                                               ch->add_child("RFC5646SpokenLanguage", "r1")->add_child_text(buffer);
-                                       }
-                                       if (!channel->SoundfieldGroupLinkID.empty()) {
-                                               channel->SoundfieldGroupLinkID.get().EncodeString(buffer, sizeof(buffer));
-                                               ch->add_child("SoundfieldGroupLinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
-                                       }
-                               }
-                       }
+               if (asset && include_mca_subdescriptors) {
+                       write_mca_subdescriptors(meta, asset);
                }
        }
 }
@@ -552,7 +610,7 @@ CPL::reel_file_assets () const
 
 
 bool
-CPL::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
+CPL::equals(shared_ptr<const Asset> other, EqualityOptions const& opt, NoteHandler note) const
 {
        auto other_cpl = dynamic_pointer_cast<const CPL>(other);
        if (!other_cpl) {
@@ -683,7 +741,7 @@ CPL::unset_version_number ()
 void
 CPL::set_content_versions (vector<ContentVersion> v)
 {
-       set<string> ids;
+       std::set<string> ids;
        for (auto i: v) {
                if (!ids.insert(i.id).second) {
                        throw DuplicateIdError ("Duplicate ID in ContentVersion list");
@@ -713,3 +771,19 @@ CPL::set_additional_subtitle_languages (vector<dcp::LanguageTag> const& langs)
                _additional_subtitle_languages.push_back (i.to_string());
        }
 }
+
+
+void
+CPL::set_main_picture_active_area(dcp::Size area)
+{
+       if (area.width % 2) {
+               throw BadSettingError("Main picture active area width is not a multiple of 2");
+       }
+
+       if (area.height % 2) {
+               throw BadSettingError("Main picture active area height is not a multiple of 2");
+       }
+
+       _main_picture_active_area = area;
+}
+