Re-read MXF descriptor after adding a key to a SMPTE subtitle asset (DoM #2660).
[libdcp.git] / src / smpte_subtitle_asset.cc
index f09d99f37ff21e474fc913704c55b17a1ef87809..0ff1d7ef5048996e7861e2bd7c02e453e68d66de 100644 (file)
@@ -40,7 +40,9 @@
 #include "compose.hpp"
 #include "crypto_context.h"
 #include "dcp_assert.h"
+#include "equality_options.h"
 #include "exceptions.h"
+#include "filesystem.h"
 #include "raw_convert.h"
 #include "smpte_load_font_node.h"
 #include "smpte_subtitle_asset.h"
@@ -72,13 +74,16 @@ using boost::starts_with;
 using namespace dcp;
 
 
-static string const subtitle_smpte_ns = "http://www.smpte-ra.org/schemas/428-7/2010/DCST";
+static string const subtitle_smpte_ns_2007 = "http://www.smpte-ra.org/schemas/428-7/2007/DCST";
+static string const subtitle_smpte_ns_2010 = "http://www.smpte-ra.org/schemas/428-7/2010/DCST";
+static string const subtitle_smpte_ns_2014 = "http://www.smpte-ra.org/schemas/428-7/2014/DCST";
 
 
-SMPTESubtitleAsset::SMPTESubtitleAsset ()
-       : MXF (Standard::SMPTE)
+SMPTESubtitleAsset::SMPTESubtitleAsset(SubtitleStandard standard)
+       : MXF(Standard::SMPTE)
        , _edit_rate (24, 1)
        , _time_code_rate (24)
+       , _subtitle_standard(standard)
        , _xml_id (make_uuid())
 {
 
@@ -94,7 +99,7 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
        auto r = Kumu::RESULT_OK;
        {
                ASDCPErrorSuspender sus;
-               r = reader->OpenRead (_file->string().c_str ());
+               r = reader->OpenRead(dcp::filesystem::fix_long_path(*_file).string().c_str());
        }
        if (!ASDCP_FAILURE(r)) {
                /* MXF-wrapped */
@@ -109,7 +114,7 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
                        xml->read_string (xml_string);
                        parse_xml (xml);
                        read_mxf_descriptor (reader);
-                       read_mxf_resources (reader, make_shared<DecryptionContext>(optional<Key>(), Standard::SMPTE));
+                       read_mxf_resources(reader, std::make_shared<DecryptionContext>(optional<Key>(), Standard::SMPTE));
                } else {
                        read_mxf_descriptor (reader);
                }
@@ -118,7 +123,7 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
                try {
                        _raw_xml = dcp::file_to_string (file);
                        xml = make_shared<cxml::Document>("SubtitleReel");
-                       xml->read_file (file);
+                       xml->read_file(dcp::filesystem::fix_long_path(file));
                        parse_xml (xml);
                } catch (cxml::Error& e) {
                        boost::throw_exception (
@@ -139,11 +144,11 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
                        if (im && im->png_image().size() == 0) {
                                /* Even more dubious; allow <id>.png or urn:uuid:<id>.png */
                                auto p = file.parent_path() / String::compose("%1.png", im->id());
-                               if (boost::filesystem::is_regular_file(p)) {
+                               if (filesystem::is_regular_file(p)) {
                                        im->read_png_file (p);
                                } else if (starts_with (im->id(), "urn:uuid:")) {
                                        p = file.parent_path() / String::compose("%1.png", remove_urn_uuid(im->id()));
-                                       if (boost::filesystem::is_regular_file(p)) {
+                                       if (filesystem::is_regular_file(p)) {
                                                im->read_png_file (p);
                                        }
                                }
@@ -165,6 +170,15 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
 void
 SMPTESubtitleAsset::parse_xml (shared_ptr<cxml::Document> xml)
 {
+       if (xml->namespace_uri() == subtitle_smpte_ns_2007) {
+               _subtitle_standard = SubtitleStandard::SMPTE_2007;
+       } else if (xml->namespace_uri() == subtitle_smpte_ns_2010) {
+               _subtitle_standard = SubtitleStandard::SMPTE_2010;
+       } else if (xml->namespace_uri() == subtitle_smpte_ns_2014) {
+               _subtitle_standard = SubtitleStandard::SMPTE_2014;
+       } else {
+               throw XMLError("Unrecognised subtitle namespace " + xml->namespace_uri());
+       }
        _xml_id = remove_urn_uuid(xml->string_child("Id"));
        _load_font_nodes = type_children<dcp::SMPTELoadFontNode> (xml, "LoadFont");
 
@@ -220,15 +234,22 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader>
                ++i) {
 
                ASDCP::TimedText::FrameBuffer buffer;
-               buffer.Capacity (10 * 1024 * 1024);
-               reader->ReadAncillaryResource (i->ResourceID, buffer, dec->context(), dec->hmac());
+               buffer.Capacity(32 * 1024 * 1024);
+               auto const result = reader->ReadAncillaryResource(i->ResourceID, buffer, dec->context(), dec->hmac());
+               if (ASDCP_FAILURE(result)) {
+                       switch (i->Type) {
+                       case ASDCP::TimedText::MT_OPENTYPE:
+                               throw ReadError(String::compose("Could not read font from MXF file (%1)", static_cast<int>(result)));
+                       case ASDCP::TimedText::MT_PNG:
+                               throw ReadError(String::compose("Could not read subtitle image from MXF file (%1)", static_cast<int>(result)));
+                       default:
+                               throw ReadError(String::compose("Could not read resource from MXF file (%1)", static_cast<int>(result)));
+                       }
+               }
 
                char id[64];
                Kumu::bin2UUIDhex (i->ResourceID, ASDCP::UUIDlen, id, sizeof(id));
 
-               shared_array<uint8_t> data (new uint8_t[buffer.Size()]);
-               memcpy (data.get(), buffer.RoData(), buffer.Size());
-
                switch (i->Type) {
                case ASDCP::TimedText::MT_OPENTYPE:
                {
@@ -238,7 +259,7 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader>
                        }
 
                        if (j != _load_font_nodes.end ()) {
-                               _fonts.push_back (Font ((*j)->id, (*j)->urn, ArrayData (data, buffer.Size ())));
+                               _fonts.push_back(Font((*j)->id, (*j)->urn, ArrayData(buffer.RoData(), buffer.Size())));
                        }
                        break;
                }
@@ -250,7 +271,7 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader>
                        }
 
                        if (j != _subtitles.end()) {
-                               dynamic_pointer_cast<SubtitleImage>(*j)->set_png_image (ArrayData(data, buffer.Size()));
+                               dynamic_pointer_cast<SubtitleImage>(*j)->set_png_image(ArrayData(buffer.RoData(), buffer.Size()));
                        }
                        break;
                }
@@ -285,10 +306,11 @@ SMPTESubtitleAsset::set_key (Key key)
           have read that file.
        */
        auto const had_key = static_cast<bool>(_key);
+       auto const had_key_id = static_cast<bool>(_key_id);
 
        MXF::set_key (key);
 
-       if (!_key_id || !_file || had_key) {
+       if (!had_key_id || !_file || had_key) {
                /* Either we don't have any data to read, it wasn't
                   encrypted, or we've already read it, so we don't
                   need to do anything else.
@@ -299,7 +321,7 @@ SMPTESubtitleAsset::set_key (Key key)
        /* Our data was encrypted; now we can decrypt it */
 
        auto reader = make_shared<ASDCP::TimedText::MXFReader>();
-       auto r = reader->OpenRead (_file->string().c_str ());
+       auto r = reader->OpenRead(dcp::filesystem::fix_long_path(*_file).string().c_str());
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (
                        ReadError (
@@ -315,6 +337,7 @@ SMPTESubtitleAsset::set_key (Key key)
        auto xml = make_shared<cxml::Document>("SubtitleReel");
        xml->read_string (xml_string);
        parse_xml (xml);
+       read_mxf_descriptor(reader);
        read_mxf_resources (reader, dec);
 }
 
@@ -333,7 +356,7 @@ SMPTESubtitleAsset::valid_mxf (boost::filesystem::path file)
 {
        ASDCP::TimedText::MXFReader reader;
        Kumu::DefaultLogSink().UnsetFilterFlag(Kumu::LOG_ALLOW_ALL);
-       auto r = reader.OpenRead (file.string().c_str ());
+       auto r = reader.OpenRead(dcp::filesystem::fix_long_path(file).string().c_str());
        Kumu::DefaultLogSink().SetFilterFlag(Kumu::LOG_ALLOW_ALL);
        return !ASDCP_FAILURE (r);
 }
@@ -344,8 +367,6 @@ SMPTESubtitleAsset::xml_as_string () const
 {
        xmlpp::Document doc;
        auto root = doc.create_root_node ("SubtitleReel");
-       root->set_namespace_declaration (subtitle_smpte_ns);
-       root->set_namespace_declaration ("http://www.w3.org/2001/XMLSchema", "xs");
 
        DCP_ASSERT (_xml_id);
        root->add_child("Id")->add_child_text("urn:uuid:" + *_xml_id);
@@ -353,7 +374,7 @@ SMPTESubtitleAsset::xml_as_string () const
        if (_annotation_text) {
                root->add_child("AnnotationText")->add_child_text(_annotation_text.get());
        }
-       root->add_child("IssueDate")->add_child_text(_issue_date.as_string(true));
+       root->add_child("IssueDate")->add_child_text(_issue_date.as_string(false, false));
        if (_reel_number) {
                root->add_child("ReelNumber")->add_child_text(raw_convert<string>(_reel_number.get()));
        }
@@ -374,7 +395,7 @@ SMPTESubtitleAsset::xml_as_string () const
 
        subtitles_as_xml (root->add_child("SubtitleList"), _time_code_rate, Standard::SMPTE);
 
-       return doc.write_to_string ("UTF-8");
+       return format_xml(doc, std::make_pair(string{}, schema_namespace()));
 }
 
 
@@ -421,7 +442,7 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
                }
        }
 
-       descriptor.NamespaceName = subtitle_smpte_ns;
+       descriptor.NamespaceName = schema_namespace();
        unsigned int c;
        DCP_ASSERT (_xml_id);
        Kumu::hex2bin (_xml_id->c_str(), descriptor.AssetID, ASDCP::UUIDlen, &c);
@@ -432,7 +453,7 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
        /* This header size is a guess.  Empirically it seems that each subtitle reference is 90 bytes, and we need some extra.
           The default size is not enough for some feature-length PNG sub projects (see DCP-o-matic #1561).
        */
-       ASDCP::Result_t r = writer.OpenWrite (p.string().c_str(), writer_info, descriptor, _subtitles.size() * 90 + 16384);
+       ASDCP::Result_t r = writer.OpenWrite(dcp::filesystem::fix_long_path(p).string().c_str(), writer_info, descriptor, _subtitles.size() * 90 + 16384);
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (FileError ("could not open subtitle MXF for writing", p.string(), r));
        }
@@ -484,7 +505,7 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
 }
 
 bool
-SMPTESubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
+SMPTESubtitleAsset::equals(shared_ptr<const Asset> other_asset, EqualityOptions const& options, NoteHandler note) const
 {
        if (!SubtitleAsset::equals (other_asset, options, note)) {
                return false;
@@ -578,3 +599,20 @@ SMPTESubtitleAsset::add (shared_ptr<Subtitle> s)
        _intrinsic_duration = latest_subtitle_out().as_editable_units_ceil(_edit_rate.numerator / _edit_rate.denominator);
 }
 
+
+string
+SMPTESubtitleAsset::schema_namespace() const
+{
+       switch (_subtitle_standard) {
+       case SubtitleStandard::SMPTE_2007:
+               return subtitle_smpte_ns_2007;
+       case SubtitleStandard::SMPTE_2010:
+               return subtitle_smpte_ns_2010;
+       case SubtitleStandard::SMPTE_2014:
+               return subtitle_smpte_ns_2014;
+       default:
+               DCP_ASSERT(false);
+       }
+
+       DCP_ASSERT(false);
+}