A number of SMPTE subtitle syntax fixes.
authorCarl Hetherington <cth@carlh.net>
Sat, 12 Sep 2015 13:36:52 +0000 (14:36 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 12 Sep 2015 13:36:52 +0000 (14:36 +0100)
src/dcp_time.cc
src/dcp_time.h
src/interop_subtitle_asset.cc
src/smpte_subtitle_asset.cc
src/subtitle_asset.cc
src/subtitle_asset.h
test/dcp_time_test.cc

index 5e2e3121e741af24f52fac305583ecb829567c5c..19ab2de105857c1414b5f486ea4823db9d078a31 100644 (file)
@@ -250,15 +250,21 @@ dcp::operator/ (Time a, Time const & b)
        return float (at) / bt;
 }
 
-/** @return A string of the form h:m:s:e padded as in 00:00:00:000 */
+/** @return A string of the form h:m:s:e padded as in 00:00:00:000 (for Interop) or 00:00:00:00 (for SMPTE) */
 string
-Time::as_string () const
+Time::as_string (Standard standard) const
 {
        stringstream str;
        str << setw(2) << setfill('0') << h << ":"
            << setw(2) << setfill('0') << m << ":"
-           << setw(2) << setfill('0') << s << ":"
-           << setw(3) << setfill('0') << e;
+           << setw(2) << setfill('0') << s << ":";
+
+       if (standard == SMPTE) {
+               str << setw(2) << setfill('0') << e;
+       } else {
+               str << setw(3) << setfill('0') << e;
+       }
+
        return str.str ();
 }
 
index 1221fb328a33168f2aa73a22e2201f960e30e441..270177dd653edb65ad94f63222391ce216575afa 100644 (file)
@@ -24,6 +24,7 @@
 #ifndef LIBDCP_TIME_H
 #define LIBDCP_TIME_H
 
+#include "types.h"
 #include <stdint.h>
 #include <string>
 #include <iostream>
@@ -71,7 +72,7 @@ public:
        int e; ///<   editable units (where 1 editable unit is 1 / tcr_ seconds)
        int tcr; ///< timecode rate: the number of editable units per second.
 
-       std::string as_string () const;
+       std::string as_string (Standard standard) const;
        double as_seconds () const;
        int64_t as_editable_units (int tcr_) const;
        Time rebase (int tcr_) const;
index fbdd839b81218b2876ef6568a603c8b2583d7796..c64796126724609cd403dbbb088a6325f7d5faba 100644 (file)
@@ -84,7 +84,7 @@ InteropSubtitleAsset::xml_as_string () const
                load_font->set_attribute ("URI", (*i)->uri);
        }
 
-       subtitles_as_xml (root, 250, "");
+       subtitles_as_xml (root, 250, INTEROP);
 
        return doc.write_to_string_formatted ("UTF-8");
 }
index c792506a5ac4ef75e654054473f1397f1d512fe2..57caf927c0aa2ed0078442794b4dbfe62a46a627 100644 (file)
@@ -202,7 +202,7 @@ SMPTESubtitleAsset::xml_as_string () const
        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));
        if (_start_time) {
-               root->add_child("StartTime", "dcst")->add_child_text (_start_time.get().as_string ());
+               root->add_child("StartTime", "dcst")->add_child_text (_start_time.get().as_string (SMPTE));
        }
 
        BOOST_FOREACH (shared_ptr<SMPTELoadFontNode> i, _load_font_nodes) {
@@ -211,7 +211,7 @@ SMPTESubtitleAsset::xml_as_string () const
                load_font->set_attribute ("ID", i->id);
        }
 
-       subtitles_as_xml (root->add_child ("SubtitleList", "dcst"), _time_code_rate, "dcst");
+       subtitles_as_xml (root->add_child ("SubtitleList", "dcst"), _time_code_rate, SMPTE);
 
        return doc.write_to_string_formatted ("UTF-8");
 }
index 077b52889ee0993caeada9b7d1d592c57a003076..6a57f455a833af928d8736cd49827ec1e009e823 100644 (file)
@@ -208,12 +208,17 @@ struct SubtitleSorter {
        }
 };
 
+/** @param standard Standard (INTEROP or SMPTE); this is used rather than putting things in the child
+ *  class because the differences between the two are fairly subtle.
+ */
 void
-SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, string xmlns) const
+SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const
 {
        list<SubtitleString> sorted = _subtitles;
        sorted.sort (SubtitleSorter ());
 
+       string const xmlns = standard == SMPTE ? "dcst" : "";
+
        /* XXX: script, underlined, weight not supported */
 
        optional<string> font;
@@ -261,7 +266,11 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, strin
                if (!font_element || font_changed) {
                        font_element = root->add_child ("Font", xmlns);
                        if (font) {
-                               font_element->set_attribute ("Id", font.get ());
+                               if (standard == SMPTE) {
+                                       font_element->set_attribute ("ID", font.get ());
+                               } else {
+                                       font_element->set_attribute ("Id", font.get ());
+                               }
                        }
                        font_element->set_attribute ("Italic", italic ? "yes" : "no");
                        font_element->set_attribute ("Color", colour.to_argb_string());
@@ -272,7 +281,11 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, strin
                        font_element->set_attribute ("Effect", effect_to_string (effect));
                        font_element->set_attribute ("EffectColor", effect_colour.to_argb_string());
                        font_element->set_attribute ("Script", "normal");
-                       font_element->set_attribute ("Underlined", "no");
+                       if (standard == SMPTE) {
+                               font_element->set_attribute ("Underline", "no");
+                       } else {
+                               font_element->set_attribute ("Underlined", "no");
+                       }
                        font_element->set_attribute ("Weight", "normal");
                }
 
@@ -285,10 +298,15 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, strin
 
                        subtitle_element = font_element->add_child ("Subtitle", xmlns);
                        subtitle_element->set_attribute ("SpotNumber", raw_convert<string> (spot_number++));
-                       subtitle_element->set_attribute ("TimeIn", i->in().rebase(time_code_rate).as_string());
-                       subtitle_element->set_attribute ("TimeOut", i->out().rebase(time_code_rate).as_string());
-                       subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i->fade_up_time().as_editable_units(time_code_rate)));
-                       subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i->fade_down_time().as_editable_units(time_code_rate)));
+                       subtitle_element->set_attribute ("TimeIn", i->in().rebase(time_code_rate).as_string(standard));
+                       subtitle_element->set_attribute ("TimeOut", i->out().rebase(time_code_rate).as_string(standard));
+                       if (standard == SMPTE) {
+                               subtitle_element->set_attribute ("FadeUpTime", i->fade_up_time().rebase(time_code_rate).as_string(standard));
+                               subtitle_element->set_attribute ("FadeDownTime", i->fade_down_time().rebase(time_code_rate).as_string(standard));
+                       } else {
+                               subtitle_element->set_attribute ("FadeUpTime", raw_convert<string> (i->fade_up_time().as_editable_units(time_code_rate)));
+                               subtitle_element->set_attribute ("FadeDownTime", raw_convert<string> (i->fade_down_time().as_editable_units(time_code_rate)));
+                       }
 
                        last_in = i->in ();
                        last_out = i->out ();
@@ -298,16 +316,36 @@ SubtitleAsset::subtitles_as_xml (xmlpp::Element* root, int time_code_rate, strin
 
                xmlpp::Element* text = subtitle_element->add_child ("Text", xmlns);
                if (i->h_align() != HALIGN_CENTER) {
-                       text->set_attribute ("HAlign", halign_to_string (i->h_align ()));
+                       if (standard == SMPTE) {
+                               text->set_attribute ("Halign", halign_to_string (i->h_align ()));
+                       } else {
+                               text->set_attribute ("HAlign", halign_to_string (i->h_align ()));
+                       }
                }
                if (i->h_position() > ALIGN_EPSILON) {
-                       text->set_attribute ("HPosition", raw_convert<string> (i->h_position() * 100, 6));
+                       if (standard == SMPTE) {
+                               text->set_attribute ("Hposition", raw_convert<string> (i->h_position() * 100, 6));
+                       } else {
+                               text->set_attribute ("HPosition", raw_convert<string> (i->h_position() * 100, 6));
+                       }
+               }
+               if (standard == SMPTE) {
+                       text->set_attribute ("Valign", valign_to_string (i->v_align()));
+               } else {
+                       text->set_attribute ("VAlign", valign_to_string (i->v_align()));
                }
-               text->set_attribute ("VAlign", valign_to_string (i->v_align()));
                if (i->v_position() > ALIGN_EPSILON) {
-                       text->set_attribute ("VPosition", raw_convert<string> (i->v_position() * 100, 6));
+                       if (standard == SMPTE) {
+                               text->set_attribute ("Vposition", raw_convert<string> (i->v_position() * 100, 6));
+                       } else {
+                               text->set_attribute ("VPosition", raw_convert<string> (i->v_position() * 100, 6));
+                       }
                } else {
-                       text->set_attribute ("VPosition", "0");
+                       if (standard == SMPTE) {
+                               text->set_attribute ("Vposition", "0");
+                       } else {
+                               text->set_attribute ("VPosition", "0");
+                       }
                }
                text->add_child_text (i->text());
        }
index 69bf40f86e56d56798e75135d8f097fef9e1e687..88dc427bda616ac058a684c931d263957ebed83f 100644 (file)
@@ -84,7 +84,7 @@ protected:
        friend struct ::smpte_dcp_font_test;
 
        void parse_subtitles (boost::shared_ptr<cxml::Document> xml, std::list<boost::shared_ptr<FontNode> > font_nodes);
-       void subtitles_as_xml (xmlpp::Element* root, int time_code_rate, std::string xmlns) const;
+       void subtitles_as_xml (xmlpp::Element* root, int time_code_rate, Standard standard) const;
 
        /** All our subtitles, in no particular order */
        std::list<SubtitleString> _subtitles;
index 1fc9471d7ff9a64de3d1643e8148462315daffad..926dd0242086077778b268e7916a4c5d995016ed 100644 (file)
@@ -30,7 +30,7 @@ BOOST_AUTO_TEST_CASE (dcp_time)
        BOOST_CHECK_EQUAL (t.s, 34);
        BOOST_CHECK_EQUAL (t.m, 18);
        BOOST_CHECK_EQUAL (t.h, 11);
-       BOOST_CHECK_EQUAL (t.as_string(), "11:18:34:073");
+       BOOST_CHECK_EQUAL (t.as_string(dcp::INTEROP), "11:18:34:073");
 
        /* Use a tcr of 24 so that the editable event is a frame */
        dcp::Time a (3, 2, 3, 4, 24);
@@ -41,7 +41,7 @@ BOOST_AUTO_TEST_CASE (dcp_time)
        BOOST_CHECK_EQUAL (r.m, 58);
        BOOST_CHECK_EQUAL (r.s, 58);
        BOOST_CHECK_EQUAL (r.e, 23);
-       BOOST_CHECK_EQUAL (r.as_string(), "00:58:58:023");
+       BOOST_CHECK_EQUAL (r.as_string(dcp::INTEROP), "00:58:58:023");
 
        /* Different tcr (25) */
        a = dcp::Time (1, 58, 56, 2, 25);
@@ -51,7 +51,7 @@ BOOST_AUTO_TEST_CASE (dcp_time)
        BOOST_CHECK_EQUAL (r.m, 6);
        BOOST_CHECK_EQUAL (r.s, 8);
        BOOST_CHECK_EQUAL (r.e, 3);
-       BOOST_CHECK_EQUAL (r.as_string(), "03:06:08:003");
+       BOOST_CHECK_EQUAL (r.as_string(dcp::INTEROP), "03:06:08:003");
 
        /* Another arbitrary tcr (30) */
        a = dcp::Time (24, 12, 6, 3, 30);