Switch to UUIDs for Interop image subtitle identification (rather than indices)....
authorCarl Hetherington <cth@carlh.net>
Sun, 8 Jul 2018 23:43:13 +0000 (00:43 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 8 Jul 2018 23:43:13 +0000 (00:43 +0100)
src/interop_subtitle_asset.cc
src/object.h
src/smpte_subtitle_asset.cc
src/subtitle_asset.cc
src/subtitle_asset.h
src/subtitle_asset_internal.cc
src/subtitle_asset_internal.h
test/data/write_interop_subtitle_test3.xml
test/write_subtitle_test.cc

index 02e2d75b21b6edc0074b5ea56f742b5e45a3b924..2ef82c901e64b0b9ba9ea014d8a2fb312fd70a6d 100644 (file)
 #include "util.h"
 #include "font_asset.h"
 #include "dcp_assert.h"
+#include "compose.hpp"
 #include "subtitle_image.h"
 #include <libxml++/libxml++.h>
 #include <boost/foreach.hpp>
+#include <boost/weak_ptr.hpp>
 #include <cmath>
 #include <cstdio>
 
@@ -177,11 +179,12 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const
        _file = p;
 
        /* Image subtitles */
-       int n = 0;
        BOOST_FOREACH (shared_ptr<dcp::Subtitle> i, _subtitles) {
                shared_ptr<dcp::SubtitleImage> im = dynamic_pointer_cast<dcp::SubtitleImage> (i);
                if (im) {
-                       im->png_image().write (p.parent_path() / image_subtitle_file (n++));
+                       ImageUUIDMap::const_iterator uuid = _image_subtitle_uuid.find(im);
+                       DCP_ASSERT (uuid != _image_subtitle_uuid.end());
+                       im->png_image().write (p.parent_path() / String::compose("%1.png", uuid->second));
                }
        }
 
index b2d0ad35f995188b9c4e30b36ec7a1a17534f975..d44f1be2c6aa1f0280934d8660ed5de9cf8ffa0e 100644 (file)
@@ -46,6 +46,7 @@ class write_interop_subtitle_test2;
 class write_interop_subtitle_test3;
 class write_smpte_subtitle_test;
 class write_smpte_subtitle_test2;
+class write_smpte_subtitle_test3;
 
 namespace dcp {
 
@@ -70,6 +71,7 @@ protected:
        friend struct ::write_interop_subtitle_test3;
        friend struct ::write_smpte_subtitle_test;
        friend struct ::write_smpte_subtitle_test2;
+       friend struct ::write_smpte_subtitle_test3;
 
        /** ID */
        std::string _id;
index 07d8f7eb48ebee953246e92e3e981eae884582fe..267ff27e692f0a79efa321391f395e68d6318623 100644 (file)
@@ -44,7 +44,7 @@
 #include "util.h"
 #include "compose.hpp"
 #include "crypto_context.h"
-#include "crypto_context.h"
+#include "subtitle_image.h"
 #include <asdcp/AS_DCP.h>
 #include <asdcp/KM_util.h>
 #include <libxml++/libxml++.h>
@@ -304,6 +304,8 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
        descriptor.EditRate = ASDCP::Rational (_edit_rate.numerator, _edit_rate.denominator);
        descriptor.EncodingName = "UTF-8";
 
+       /* Font references */
+
        BOOST_FOREACH (shared_ptr<dcp::SMPTELoadFontNode> i, _load_font_nodes) {
                list<Font>::const_iterator j = _fonts.begin ();
                while (j != _fonts.end() && j->load_id != i->id) {
@@ -319,6 +321,17 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
                }
        }
 
+       /* Image subtitle references */
+
+       for (ImageUUIDMap::const_iterator i = _image_subtitle_uuid.begin(); i != _image_subtitle_uuid.end(); ++i) {
+               ASDCP::TimedText::TimedTextResourceDescriptor res;
+               unsigned int c;
+               Kumu::hex2bin (i->second.c_str(), res.ResourceID, Kumu::UUID_Length, &c);
+               DCP_ASSERT (c == Kumu::UUID_Length);
+               res.Type = ASDCP::TimedText::MT_PNG;
+               descriptor.ResourceList.push_back (res);
+       }
+
        descriptor.NamespaceName = "dcst";
        memcpy (descriptor.AssetID, writer_info.AssetUUID, ASDCP::UUIDlen);
        descriptor.ContainerDuration = _intrinsic_duration;
@@ -329,12 +342,13 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
                boost::throw_exception (FileError ("could not open subtitle MXF for writing", p.string(), r));
        }
 
-       /* XXX: no encryption */
        r = writer.WriteTimedTextResource (xml_as_string (), enc.context(), enc.hmac());
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (MXFFileError ("could not write XML to timed text resource", p.string(), r));
        }
 
+       /* Font payload */
+
        BOOST_FOREACH (shared_ptr<dcp::SMPTELoadFontNode> i, _load_font_nodes) {
                list<Font>::const_iterator j = _fonts.begin ();
                while (j != _fonts.end() && j->load_id != i->id) {
@@ -351,6 +365,18 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
                }
        }
 
+       /* Image subtitle payload */
+
+       for (ImageUUIDMap::const_iterator i = _image_subtitle_uuid.begin(); i != _image_subtitle_uuid.end(); ++i) {
+               ASDCP::TimedText::FrameBuffer buffer;
+               buffer.SetData (i->first->png_image().data().get(), i->first->png_image().size());
+               buffer.Size (i->first->png_image().size());
+               r = writer.WriteAncillaryResource (buffer, enc.context(), enc.hmac());
+               if (ASDCP_FAILURE(r)) {
+                       boost::throw_exception (MXFFileError ("could not write PNG data to timed text resource", p.string(), r));
+               }
+       }
+
        writer.Finalize ();
 
        _file = p;
index f93ffa85ab61f6f5bb515a9e7249f7a1f76521ba..5a096f204e55419b46bf13c001e2387ee4edac1f 100644 (file)
@@ -373,6 +373,11 @@ void
 SubtitleAsset::add (shared_ptr<Subtitle> s)
 {
        _subtitles.push_back (s);
+
+       shared_ptr<SubtitleImage> si = dynamic_pointer_cast<SubtitleImage> (s);
+       if (si) {
+               _image_subtitle_uuid[si] = make_uuid ();
+       }
 }
 
 Time
@@ -573,8 +578,10 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* xml_root, int time_code_rate, S
                shared_ptr<SubtitleImage> ii = dynamic_pointer_cast<SubtitleImage>(i);
                if (ii) {
                        text.reset ();
+                       ImageUUIDMap::const_iterator uuid = _image_subtitle_uuid.find(ii);
+                       DCP_ASSERT (uuid != _image_subtitle_uuid.end());
                        subtitle->children.push_back (
-                               shared_ptr<order::Image> (new order::Image (subtitle, ii->png_image(), ii->h_align(), ii->h_position(), ii->v_align(), ii->v_position()))
+                               shared_ptr<order::Image> (new order::Image (subtitle, uuid->second, ii->png_image(), ii->h_align(), ii->h_position(), ii->v_align(), ii->v_position()))
                                );
                }
        }
@@ -589,7 +596,6 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* xml_root, int time_code_rate, S
        context.time_code_rate = time_code_rate;
        context.standard = standard;
        context.spot_number = 1;
-       context.image_number = 0;
 
        root->write_xml (xml_root, context);
 }
index 1108514a3e67bbd66edbebda2198ec770b56ce1e..31cca18584b1f9fa69980de6aeddc574e987c81c 100644 (file)
@@ -56,6 +56,7 @@ namespace dcp
 {
 
 class SubtitleString;
+class SubtitleImage;
 class FontNode;
 class TextNode;
 class SubtitleNode;
@@ -164,6 +165,10 @@ protected:
        /** TTF font data that we need */
        std::list<Font> _fonts;
 
+       /** Map of image subtitles to UUIDs */
+       typedef std::map<boost::shared_ptr<dcp::SubtitleImage>, std::string> ImageUUIDMap;
+       ImageUUIDMap _image_subtitle_uuid;
+
 private:
        friend struct ::pull_fonts_test1;
        friend struct ::pull_fonts_test2;
index 13bdf6ab1de5a8d3803414401a86e756b68bbbf6..7b9e18e90e7aa11b11f296a2e0ef8e11ebc23c59 100644 (file)
@@ -238,13 +238,11 @@ order::Image::as_xml (xmlpp::Element* parent, Context& context) const
        xmlpp::Element* e = parent->add_child ("Image", context.xmlns());
 
        position_align (e, context, _h_align, _h_position, _v_align, _v_position);
-       e->add_child_text (image_subtitle_file (context.image_number++));
+       if (context.standard == SMPTE) {
+               e->add_child_text (_id);
+       } else {
+               e->add_child_text (_id + ".png");
+       }
 
        return e;
 }
-
-string
-dcp::image_subtitle_file (int n)
-{
-       return String::compose ("sub_%1.png", n);
-}
index e2b1602e475fc255ac0ca82ec98b72663355f715..b0034743421e4f393767cb561cd2c3a5229bdbfd 100644 (file)
@@ -60,7 +60,6 @@ struct Context
        int time_code_rate;
        Standard standard;
        int spot_number;
-       int image_number;
 };
 
 class Font
@@ -168,9 +167,10 @@ private:
 class Image : public Part
 {
 public:
-       Image (boost::shared_ptr<Part> parent, Data png_data, HAlign h_align, float h_position, VAlign v_align, float v_position)
+       Image (boost::shared_ptr<Part> parent, std::string id, Data png_data, HAlign h_align, float h_position, VAlign v_align, float v_position)
                : Part (parent)
                , _png_data (png_data)
+               , _id (id)
                , _h_align (h_align)
                , _h_position (h_position)
                , _v_align (v_align)
@@ -181,6 +181,7 @@ public:
 
 private:
        Data _png_data;
+       std::string _id; ///< the ID of this image (index for Interop, UUID for SMPTE)
        HAlign _h_align;
        float _h_position;
        VAlign _v_align;
@@ -189,8 +190,6 @@ private:
 
 }
 
-std::string image_subtitle_file (int n);
-
 }
 
 #endif
index e92685b8fe7fdedd9e827049edb838eb33ddc78e..7ba72e477757f5d5de0562a39740bcc3401727c1 100644 (file)
@@ -1,2 +1,2 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<DCSubtitle Version="1.0"><SubtitleID>a6c58cff-3e1e-4b38-acec-a42224475ef6</SubtitleID><MovieTitle>Test</MovieTitle><ReelNumber>1</ReelNumber><Language>EN</Language><Subtitle SpotNumber="1" TimeIn="00:04:09:229" TimeOut="00:04:11:229" FadeUpTime="0" FadeDownTime="0"><Image VAlign="top" VPosition="80">sub_0.png</Image></Subtitle></DCSubtitle>
+<DCSubtitle Version="1.0"><SubtitleID>a6c58cff-3e1e-4b38-acec-a42224475ef6</SubtitleID><MovieTitle>Test</MovieTitle><ReelNumber>1</ReelNumber><Language>EN</Language><Subtitle SpotNumber="1" TimeIn="00:04:09:229" TimeOut="00:04:11:229" FadeUpTime="0" FadeDownTime="0"><Image VAlign="top" VPosition="80">822bd341-c751-45b1-94d2-410e4ffcff1b.png</Image></Subtitle></DCSubtitle>
index dec815c079810945661c4e4c7cfbad7605e3057e..cf595faf75bf56d8507c490b5246de7b60355f49 100644 (file)
@@ -343,7 +343,7 @@ BOOST_AUTO_TEST_CASE (write_interop_subtitle_test3)
                dcp::file_to_string("build/test/write_interop_subtitle_test3/subs.xml"),
                list<string>()
                );
-       check_file ("build/test/write_interop_subtitle_test3/sub_0.png", "test/data/sub.png");
+       check_file ("build/test/write_interop_subtitle_test3/822bd341-c751-45b1-94d2-410e4ffcff1b.png", "test/data/sub.png");
 }
 
 /* Write some subtitle content as SMPTE XML and check that it is right */
@@ -637,3 +637,35 @@ BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test2)
                list<string> ()
                );
 }
+
+/* Write some subtitle content as SMPTE using bitmaps and check that it is right */
+BOOST_AUTO_TEST_CASE (write_smpte_subtitle_test3)
+{
+       dcp::SMPTESubtitleAsset c;
+       c.set_reel_number (1);
+       c.set_language ("EN");
+       c.set_content_title_text ("Test");
+
+       c.add (
+               shared_ptr<dcp::Subtitle> (
+                       new dcp::SubtitleImage (
+                               dcp::Data ("test/data/sub.png"),
+                               dcp::Time (0, 4,  9, 22, 24),
+                               dcp::Time (0, 4, 11, 22, 24),
+                               0,
+                               dcp::HALIGN_CENTER,
+                               0.8,
+                               dcp::VALIGN_TOP,
+                               dcp::Time (0, 0, 0, 0, 24),
+                               dcp::Time (0, 0, 0, 0, 24)
+                               )
+                       )
+               );
+
+       c._id = "a6c58cff-3e1e-4b38-acec-a42224475ef6";
+
+       boost::filesystem::create_directories ("build/test/write_smpte_subtitle_test3");
+       c.write ("build/test/write_smpte_subtitle_test3/subs.mxf");
+
+       /* XXX: check this result when we can read them back in again */
+}