In-line run of subs_in_out so that it gets the environment more easily.
[libdcp.git] / src / smpte_subtitle_asset.cc
index dc8acf51db120e1413a473e6a9cbb0add587fa6a..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 */
@@ -103,11 +108,13 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
                _id = read_writer_info (info);
                if (!_key_id) {
                        /* Not encrypted; read it in now */
-                       reader->ReadTimedTextResource (_raw_xml);
-                       xml->read_string (_raw_xml);
+                       string xml_string;
+                       reader->ReadTimedTextResource (xml_string);
+                       _raw_xml = xml_string;
+                       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);
                }
@@ -116,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 (
@@ -137,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);
                                        }
                                }
@@ -163,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");
 
@@ -218,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:
                {
@@ -236,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;
                }
@@ -248,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;
                }
@@ -283,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.
@@ -297,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 (
@@ -307,10 +331,13 @@ SMPTESubtitleAsset::set_key (Key key)
        }
 
        auto dec = make_shared<DecryptionContext>(key, Standard::SMPTE);
-       reader->ReadTimedTextResource (_raw_xml, dec->context(), dec->hmac());
+       string xml_string;
+       reader->ReadTimedTextResource (xml_string, dec->context(), dec->hmac());
+       _raw_xml = xml_string;
        auto xml = make_shared<cxml::Document>("SubtitleReel");
-       xml->read_string (_raw_xml);
+       xml->read_string (xml_string);
        parse_xml (xml);
+       read_mxf_descriptor(reader);
        read_mxf_resources (reader, dec);
 }
 
@@ -329,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);
 }
@@ -339,37 +366,36 @@ string
 SMPTESubtitleAsset::xml_as_string () const
 {
        xmlpp::Document doc;
-       auto root = doc.create_root_node ("dcst:SubtitleReel");
-       root->set_namespace_declaration (subtitle_smpte_ns, "dcst");
-       root->set_namespace_declaration ("http://www.w3.org/2001/XMLSchema", "xs");
+       auto root = doc.create_root_node ("SubtitleReel");
 
-       root->add_child("Id", "dcst")->add_child_text ("urn:uuid:" + _xml_id);
-       root->add_child("ContentTitleText", "dcst")->add_child_text (_content_title_text);
+       DCP_ASSERT (_xml_id);
+       root->add_child("Id")->add_child_text("urn:uuid:" + *_xml_id);
+       root->add_child("ContentTitleText")->add_child_text(_content_title_text);
        if (_annotation_text) {
-               root->add_child("AnnotationText", "dcst")->add_child_text (_annotation_text.get ());
+               root->add_child("AnnotationText")->add_child_text(_annotation_text.get());
        }
-       root->add_child("IssueDate", "dcst")->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", "dcst")->add_child_text (raw_convert<string> (_reel_number.get ()));
+               root->add_child("ReelNumber")->add_child_text(raw_convert<string>(_reel_number.get()));
        }
        if (_language) {
-               root->add_child("Language", "dcst")->add_child_text (_language.get ());
+               root->add_child("Language")->add_child_text(_language.get());
        }
-       root->add_child("EditRate", "dcst")->add_child_text (_edit_rate.as_string ());
-       root->add_child("TimeCodeRate", "dcst")->add_child_text (raw_convert<string> (_time_code_rate));
+       root->add_child("EditRate")->add_child_text(_edit_rate.as_string());
+       root->add_child("TimeCodeRate")->add_child_text(raw_convert<string>(_time_code_rate));
        if (_start_time) {
-               root->add_child("StartTime", "dcst")->add_child_text(_start_time.get().as_string(Standard::SMPTE));
+               root->add_child("StartTime")->add_child_text(_start_time.get().as_string(Standard::SMPTE));
        }
 
        for (auto i: _load_font_nodes) {
-               auto load_font = root->add_child("LoadFont", "dcst");
+               auto load_font = root->add_child("LoadFont");
                load_font->add_child_text ("urn:uuid:" + i->urn);
                load_font->set_attribute ("ID", i->id);
        }
 
-       subtitles_as_xml (root->add_child("SubtitleList", "dcst"), _time_code_rate, Standard::SMPTE);
+       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()));
 }
 
 
@@ -416,9 +442,10 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
                }
        }
 
-       descriptor.NamespaceName = subtitle_smpte_ns;
+       descriptor.NamespaceName = schema_namespace();
        unsigned int c;
-       Kumu::hex2bin (_xml_id.c_str(), descriptor.AssetID, ASDCP::UUIDlen, &c);
+       DCP_ASSERT (_xml_id);
+       Kumu::hex2bin (_xml_id->c_str(), descriptor.AssetID, ASDCP::UUIDlen, &c);
        DCP_ASSERT (c == Kumu::UUID_Length);
        descriptor.ContainerDuration = _intrinsic_duration;
 
@@ -426,12 +453,14 @@ 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));
        }
 
-       r = writer.WriteTimedTextResource (xml_as_string (), enc.context(), enc.hmac());
+       _raw_xml = xml_as_string ();
+
+       r = writer.WriteTimedTextResource (*_raw_xml, enc.context(), enc.hmac());
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (MXFFileError ("could not write XML to timed text resource", p.string(), r));
        }
@@ -476,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;
@@ -569,3 +598,21 @@ SMPTESubtitleAsset::add (shared_ptr<Subtitle> s)
        SubtitleAsset::add (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);
+}