Report failures to read resources from MXF files.
[libdcp.git] / src / smpte_subtitle_asset.cc
index 15a366ba153f7598bd61d86939ddd545de6d1676..b9d7e9a341d25fbf8fe396edaeb2d391e8e4feeb 100644 (file)
  */
 
 
-#include "smpte_subtitle_asset.h"
-#include "smpte_load_font_node.h"
-#include "exceptions.h"
-#include "xml.h"
-#include "raw_convert.h"
-#include "dcp_assert.h"
-#include "util.h"
 #include "compose.hpp"
 #include "crypto_context.h"
+#include "dcp_assert.h"
+#include "equality_options.h"
+#include "exceptions.h"
+#include "raw_convert.h"
+#include "smpte_load_font_node.h"
+#include "smpte_subtitle_asset.h"
 #include "subtitle_image.h"
+#include "util.h"
+#include "warnings.h"
+#include "xml.h"
+LIBDCP_DISABLE_WARNINGS
 #include <asdcp/AS_DCP.h>
 #include <asdcp/KM_util.h>
 #include <asdcp/KM_log.h>
 #include <libxml++/libxml++.h>
+LIBDCP_ENABLE_WARNINGS
 #include <boost/algorithm/string.hpp>
 
 
@@ -69,13 +73,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())
 {
 
@@ -100,10 +107,15 @@ 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, make_shared<DecryptionContext>(optional<Key>(), Standard::SMPTE));
+                       read_mxf_descriptor (reader);
+                       read_mxf_resources(reader, std::make_shared<DecryptionContext>(optional<Key>(), Standard::SMPTE));
+               } else {
+                       read_mxf_descriptor (reader);
                }
        } else {
                /* Plain XML */
@@ -112,7 +124,6 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
                        xml = make_shared<cxml::Document>("SubtitleReel");
                        xml->read_file (file);
                        parse_xml (xml);
-                       _id = _xml_id = remove_urn_uuid (xml->string_child ("Id"));
                } catch (cxml::Error& e) {
                        boost::throw_exception (
                                ReadError (
@@ -158,6 +169,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");
 
@@ -195,12 +215,12 @@ SMPTESubtitleAsset::parse_xml (shared_ptr<cxml::Document> xml)
        }
 
        /* Guess intrinsic duration */
-       _intrinsic_duration = latest_subtitle_out().as_editable_units (_edit_rate.numerator / _edit_rate.denominator);
+       _intrinsic_duration = latest_subtitle_out().as_editable_units_ceil(_edit_rate.numerator / _edit_rate.denominator);
 }
 
 
 void
-SMPTESubtitleAsset::read_mxf_descriptor (shared_ptr<ASDCP::TimedText::MXFReader> reader, shared_ptr<DecryptionContext> dec)
+SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader> reader, shared_ptr<DecryptionContext> dec)
 {
        ASDCP::TimedText::TimedTextDescriptor descriptor;
        reader->FillTimedTextDescriptor (descriptor);
@@ -213,15 +233,22 @@ SMPTESubtitleAsset::read_mxf_descriptor (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:
                {
@@ -231,7 +258,7 @@ SMPTESubtitleAsset::read_mxf_descriptor (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;
                }
@@ -243,7 +270,7 @@ SMPTESubtitleAsset::read_mxf_descriptor (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;
                }
@@ -251,8 +278,23 @@ SMPTESubtitleAsset::read_mxf_descriptor (shared_ptr<ASDCP::TimedText::MXFReader>
                        break;
                }
        }
+}
+
+
+void
+SMPTESubtitleAsset::read_mxf_descriptor (shared_ptr<ASDCP::TimedText::MXFReader> reader)
+{
+       ASDCP::TimedText::TimedTextDescriptor descriptor;
+       reader->FillTimedTextDescriptor (descriptor);
 
        _intrinsic_duration = descriptor.ContainerDuration;
+       /* The thing which is called AssetID in the descriptor is also known as the
+        * ResourceID of the MXF.  We store that, at present just for verification
+        * purposes.
+        */
+       char id[64];
+       Kumu::bin2UUIDhex (descriptor.AssetID, ASDCP::UUIDlen, id, sizeof(id));
+       _resource_id = id;
 }
 
 
@@ -287,11 +329,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, dec);
+       read_mxf_resources (reader, dec);
 }
 
 
@@ -319,37 +363,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()));
 }
 
 
@@ -396,9 +439,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;
 
@@ -411,7 +455,9 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
                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));
        }
@@ -456,7 +502,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;
@@ -547,5 +593,23 @@ void
 SMPTESubtitleAsset::add (shared_ptr<Subtitle> s)
 {
        SubtitleAsset::add (s);
-       _intrinsic_duration = latest_subtitle_out().as_editable_units (_edit_rate.numerator / _edit_rate.denominator);
+       _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);
 }