Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / ffmpeg_subtitle_stream.cc
index 3d8fd4e8375466710cb51ffd4a6a7a4ac950c75e..e12075581b12705a71d53153904218a6950b1cce 100644 (file)
 */
 
 #include "ffmpeg_subtitle_stream.h"
+#include "raw_convert.h"
+#include <libxml++/libxml++.h>
+#include <boost/foreach.hpp>
+#include <iostream>
+
+using std::string;
+using std::map;
+using std::list;
+using std::cout;
 
 /** Construct a SubtitleStream from a value returned from to_string().
  *  @param t String returned from to_string().
 FFmpegSubtitleStream::FFmpegSubtitleStream (cxml::ConstNodePtr node)
        : FFmpegStream (node)
 {
-       
+       BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Period")) {
+               add_subtitle (
+                       ContentTimePeriod (
+                               ContentTime (i->number_child<ContentTime::Type> ("From")),
+                               ContentTime (i->number_child<ContentTime::Type> ("To"))
+                               )
+                       );
+       }
 }
 
 void
 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
 {
        FFmpegStream::as_xml (root);
+
+       for (map<ContentTime, ContentTime>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+               xmlpp::Node* node = root->add_child ("Period");
+               node->add_child("From")->add_child_text (raw_convert<string> (i->first.get ()));
+               node->add_child("To")->add_child_text (raw_convert<string> (i->second.get ()));
+       }
+}
+
+void
+FFmpegSubtitleStream::add_subtitle (ContentTimePeriod period)
+{
+       DCPOMATIC_ASSERT (_subtitles.find (period.from) == _subtitles.end ());
+       _subtitles[period.from] = period.to;
+}
+
+list<ContentTimePeriod>
+FFmpegSubtitleStream::subtitles_during (ContentTimePeriod period, bool starting) const
+{
+       list<ContentTimePeriod> d;
+
+       /* XXX: inefficient */
+       for (map<ContentTime, ContentTime>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+               if ((starting && period.contains (i->first)) || (!starting && period.overlaps (ContentTimePeriod (i->first, i->second)))) {
+                       d.push_back (ContentTimePeriod (i->first, i->second));
+               }
+       }
+
+       return d;
+}
+
+ContentTime
+FFmpegSubtitleStream::find_subtitle_to (ContentTime from) const
+{
+       map<ContentTime, ContentTime>::const_iterator i = _subtitles.find (from);
+       DCPOMATIC_ASSERT (i != _subtitles.end ());
+       return i->second;
+}
+
+/** Add some offset to all the times in the stream */
+void
+FFmpegSubtitleStream::add_offset (ContentTime offset)
+{
+       map<ContentTime, ContentTime> fixed;
+       for (map<ContentTime, ContentTime>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
+               fixed[i->first + offset] = i->second + offset;
+       }
+       _subtitles = fixed;
 }