2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "ffmpeg_subtitle_stream.h"
21 #include "raw_convert.h"
22 #include <libxml++/libxml++.h>
23 #include <boost/foreach.hpp>
31 /** Construct a SubtitleStream from a value returned from to_string().
32 * @param t String returned from to_string().
33 * @param v State file version.
35 FFmpegSubtitleStream::FFmpegSubtitleStream (cxml::ConstNodePtr node, int version)
39 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Period")) {
40 /* In version 32 we assumed that from times were unique, so they weer
44 raw_convert<string> (i->string_child ("From")),
46 ContentTime (i->number_child<ContentTime::Type> ("From")),
47 ContentTime (i->number_child<ContentTime::Type> ("To"))
52 /* In version 33 we use a hash of various parts of the subtitle as the id */
53 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Subtitle")) {
55 raw_convert<string> (i->string_child ("Id")),
57 ContentTime (i->number_child<ContentTime::Type> ("From")),
58 ContentTime (i->number_child<ContentTime::Type> ("To"))
66 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
68 FFmpegStream::as_xml (root);
70 for (map<string, ContentTimePeriod>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
71 xmlpp::Node* node = root->add_child ("Subtitle");
72 node->add_child("Id")->add_child_text (i->first);
73 node->add_child("From")->add_child_text (raw_convert<string> (i->second.from.get ()));
74 node->add_child("To")->add_child_text (raw_convert<string> (i->second.to.get ()));
79 FFmpegSubtitleStream::add_subtitle (string id, ContentTimePeriod period)
81 DCPOMATIC_ASSERT (_subtitles.find (id) == _subtitles.end ());
82 _subtitles[id] = period;
85 list<ContentTimePeriod>
86 FFmpegSubtitleStream::subtitles_during (ContentTimePeriod period, bool starting) const
88 list<ContentTimePeriod> d;
90 /* XXX: inefficient */
91 for (map<string, ContentTimePeriod>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
92 if ((starting && period.contains (i->second.from)) || (!starting && period.overlaps (i->second))) {
93 d.push_back (i->second);
101 FFmpegSubtitleStream::find_subtitle_to (string id) const
103 map<string, ContentTimePeriod>::const_iterator i = _subtitles.find (id);
104 DCPOMATIC_ASSERT (i != _subtitles.end ());
108 /** Add some offset to all the times in the stream */
110 FFmpegSubtitleStream::add_offset (ContentTime offset)
112 for (map<string, ContentTimePeriod>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
113 i->second.from += offset;
114 i->second.to += offset;