Use an enum class for Marker.
[libdcp.git] / src / smpte_subtitle_asset.cc
index ae473071c84d6a281e2aa1244912ca38cd1be234..7604fd397ee50a8f553c5ba8d4d2bf5e2b0f6b12 100644 (file)
@@ -56,11 +56,11 @@ using std::string;
 using std::list;
 using std::vector;
 using std::map;
-using boost::shared_ptr;
+using std::shared_ptr;
 using boost::split;
 using boost::is_any_of;
 using boost::shared_array;
-using boost::dynamic_pointer_cast;
+using std::dynamic_pointer_cast;
 using boost::optional;
 using boost::starts_with;
 using namespace dcp;
@@ -86,7 +86,11 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
        shared_ptr<cxml::Document> xml (new cxml::Document ("SubtitleReel"));
 
        shared_ptr<ASDCP::TimedText::MXFReader> reader (new ASDCP::TimedText::MXFReader ());
-       Kumu::Result_t r = reader->OpenRead (_file->string().c_str ());
+       Kumu::Result_t r = Kumu::RESULT_OK;
+       {
+               ASDCPErrorSuspender sus;
+               r = reader->OpenRead (_file->string().c_str ());
+       }
        if (!ASDCP_FAILURE (r)) {
                /* MXF-wrapped */
                ASDCP::WriterInfo info;
@@ -94,22 +98,22 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
                _id = read_writer_info (info);
                if (!_key_id) {
                        /* Not encrypted; read it in now */
-                       string s;
-                       reader->ReadTimedTextResource (s);
-                       xml->read_string (s);
+                       reader->ReadTimedTextResource (_raw_xml);
+                       xml->read_string (_raw_xml);
                        parse_xml (xml);
                        read_mxf_descriptor (reader, shared_ptr<DecryptionContext> (new DecryptionContext (optional<Key>(), SMPTE)));
                }
        } else {
                /* Plain XML */
                try {
+                       _raw_xml = dcp::file_to_string (file);
                        xml.reset (new 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 (
-                               DCPReadError (
+                               ReadError (
                                        String::compose (
                                                "Failed to read subtitle file %1; MXF failed with %2, XML failed with %3",
                                                file, static_cast<int> (r), e.what ()
@@ -136,6 +140,7 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
                                }
                        }
                }
+               _standard = dcp::SMPTE;
        }
 
        /* Check that all required image data have been found */
@@ -178,7 +183,7 @@ SMPTESubtitleAsset::parse_xml (shared_ptr<cxml::Document> xml)
 
        /* Now we need to drop down to xmlpp */
 
-       list<ParseState> ps;
+       vector<ParseState> ps;
        xmlpp::Node::NodeList c = xml->node()->get_children ();
        for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
                xmlpp::Element const * e = dynamic_cast<xmlpp::Element const *> (*i);
@@ -197,7 +202,7 @@ SMPTESubtitleAsset::read_mxf_descriptor (shared_ptr<ASDCP::TimedText::MXFReader>
        ASDCP::TimedText::TimedTextDescriptor descriptor;
        reader->FillTimedTextDescriptor (descriptor);
 
-       /* Load fonts */
+       /* Load fonts and images */
 
        for (
                ASDCP::TimedText::ResourceList_t::const_iterator i = descriptor.ResourceList.begin();
@@ -217,25 +222,25 @@ SMPTESubtitleAsset::read_mxf_descriptor (shared_ptr<ASDCP::TimedText::MXFReader>
                switch (i->Type) {
                case ASDCP::TimedText::MT_OPENTYPE:
                {
-                       list<shared_ptr<SMPTELoadFontNode> >::const_iterator j = _load_font_nodes.begin ();
+                       auto j = _load_font_nodes.begin();
                        while (j != _load_font_nodes.end() && (*j)->urn != id) {
                                ++j;
                        }
 
                        if (j != _load_font_nodes.end ()) {
-                               _fonts.push_back (Font ((*j)->id, (*j)->urn, Data (data, buffer.Size ())));
+                               _fonts.push_back (Font ((*j)->id, (*j)->urn, ArrayData (data, buffer.Size ())));
                        }
                        break;
                }
                case ASDCP::TimedText::MT_PNG:
                {
-                       list<shared_ptr<Subtitle> >::const_iterator j = _subtitles.begin ();
+                       auto j = _subtitles.begin();
                        while (j != _subtitles.end() && ((!dynamic_pointer_cast<SubtitleImage>(*j)) || dynamic_pointer_cast<SubtitleImage>(*j)->id() != id)) {
                                ++j;
                        }
 
                        if (j != _subtitles.end()) {
-                               dynamic_pointer_cast<SubtitleImage>(*j)->set_png_image (Data(data, buffer.Size()));
+                               dynamic_pointer_cast<SubtitleImage>(*j)->set_png_image (ArrayData(data, buffer.Size()));
                        }
                        break;
                }
@@ -272,25 +277,24 @@ SMPTESubtitleAsset::set_key (Key key)
        Kumu::Result_t r = reader->OpenRead (_file->string().c_str ());
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (
-                       DCPReadError (
+                       ReadError (
                                String::compose ("Could not read encrypted subtitle MXF (%1)", static_cast<int> (r))
                                )
                        );
        }
 
-       string s;
        shared_ptr<DecryptionContext> dec (new DecryptionContext (key, SMPTE));
-       reader->ReadTimedTextResource (s, dec->context(), dec->hmac());
+       reader->ReadTimedTextResource (_raw_xml, dec->context(), dec->hmac());
        shared_ptr<cxml::Document> xml (new cxml::Document ("SubtitleReel"));
-       xml->read_string (s);
+       xml->read_string (_raw_xml);
        parse_xml (xml);
        read_mxf_descriptor (reader, dec);
 }
 
-list<shared_ptr<LoadFontNode> >
+vector<shared_ptr<LoadFontNode>>
 SMPTESubtitleAsset::load_font_nodes () const
 {
-       list<shared_ptr<LoadFontNode> > lf;
+       vector<shared_ptr<LoadFontNode>> lf;
        copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
        return lf;
 }
@@ -358,7 +362,7 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
        /* Font references */
 
        BOOST_FOREACH (shared_ptr<dcp::SMPTELoadFontNode> i, _load_font_nodes) {
-               list<Font>::const_iterator j = _fonts.begin ();
+               auto j = _fonts.begin();
                while (j != _fonts.end() && j->load_id != i->id) {
                        ++j;
                }
@@ -393,7 +397,10 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
        descriptor.ContainerDuration = _intrinsic_duration;
 
        ASDCP::TimedText::MXFWriter writer;
-       ASDCP::Result_t r = writer.OpenWrite (p.string().c_str(), writer_info, descriptor);
+       /* 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);
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (FileError ("could not open subtitle MXF for writing", p.string(), r));
        }
@@ -406,13 +413,14 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
        /* Font payload */
 
        BOOST_FOREACH (shared_ptr<dcp::SMPTELoadFontNode> i, _load_font_nodes) {
-               list<Font>::const_iterator j = _fonts.begin ();
+               auto j = _fonts.begin();
                while (j != _fonts.end() && j->load_id != i->id) {
                        ++j;
                }
                if (j != _fonts.end ()) {
                        ASDCP::TimedText::FrameBuffer buffer;
-                       buffer.SetData (j->data.data().get(), j->data.size());
+                       ArrayData data_copy(j->data);
+                       buffer.SetData (data_copy.data(), data_copy.size());
                        buffer.Size (j->data.size());
                        r = writer.WriteAncillaryResource (buffer, enc.context(), enc.hmac());
                        if (ASDCP_FAILURE (r)) {
@@ -427,7 +435,7 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
                shared_ptr<SubtitleImage> si = dynamic_pointer_cast<SubtitleImage>(i);
                if (si) {
                        ASDCP::TimedText::FrameBuffer buffer;
-                       buffer.SetData (si->png_image().data().get(), si->png_image().size());
+                       buffer.SetData (si->png_image().data(), si->png_image().size());
                        buffer.Size (si->png_image().size());
                        r = writer.WriteAncillaryResource (buffer, enc.context(), enc.hmac());
                        if (ASDCP_FAILURE(r)) {
@@ -454,8 +462,8 @@ SMPTESubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions
                return false;
        }
 
-       list<shared_ptr<SMPTELoadFontNode> >::const_iterator i = _load_font_nodes.begin ();
-       list<shared_ptr<SMPTELoadFontNode> >::const_iterator j = other->_load_font_nodes.begin ();
+       auto i = _load_font_nodes.begin();
+       auto j = other->_load_font_nodes.begin();
 
        while (i != _load_font_nodes.end ()) {
                if (j == other->_load_font_nodes.end ()) {
@@ -478,7 +486,7 @@ SMPTESubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions
        }
 
        if (_language != other->_language) {
-               note (DCP_ERROR, "Subtitle languages differ");
+               note (DCP_ERROR, String::compose("Subtitle languages differ (`%1' vs `%2')", _language.get_value_or("[none]"), other->_language.get_value_or("[none]")));
                return false;
        }
 
@@ -520,10 +528,10 @@ SMPTESubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions
 }
 
 void
-SMPTESubtitleAsset::add_font (string load_id, boost::filesystem::path file)
+SMPTESubtitleAsset::add_font (string load_id, dcp::ArrayData data)
 {
        string const uuid = make_uuid ();
-       _fonts.push_back (Font (load_id, uuid, file));
+       _fonts.push_back (Font(load_id, uuid, data));
        _load_font_nodes.push_back (shared_ptr<SMPTELoadFontNode> (new SMPTELoadFontNode (load_id, uuid)));
 }