eb870c24164e3bb362b8385d2ff8f6162b8c23d5
[dcpomatic.git] / src / lib / ffmpeg_subtitle_stream.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "ffmpeg_subtitle_stream.h"
22 #include "raw_convert.h"
23 #include <libxml++/libxml++.h>
24 #include <boost/foreach.hpp>
25 #include <iostream>
26
27 using std::string;
28 using std::map;
29 using std::list;
30 using std::cout;
31 using std::make_pair;
32
33 /** Construct a SubtitleStream from a value returned from to_string().
34  *  @param t String returned from to_string().
35  *  @param v State file version.
36  */
37 FFmpegSubtitleStream::FFmpegSubtitleStream (cxml::ConstNodePtr node, int version)
38         : FFmpegStream (node)
39 {
40         if (version == 32) {
41                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Period")) {
42                         /* In version 32 we assumed that from times were unique, so they were
43                            used as identifiers.  All subtitles were image subtitles.
44                         */
45                         add_image_subtitle (
46                                 raw_convert<string> (i->string_child ("From")),
47                                 ContentTimePeriod (
48                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
49                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
50                                         )
51                                 );
52                 }
53         } else {
54                 /* In version 33 we use a hash of various parts of the subtitle as the id.
55                    <Subtitle> was initially used for image subtitles; later we have
56                    <ImageSubtitle> and <TextSubtitle>
57                 */
58                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Subtitle")) {
59                         add_image_subtitle (
60                                 raw_convert<string> (i->string_child ("Id")),
61                                 ContentTimePeriod (
62                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
63                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
64                                         )
65                                 );
66                 }
67
68                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("ImageSubtitle")) {
69                         add_image_subtitle (
70                                 raw_convert<string> (i->string_child ("Id")),
71                                 ContentTimePeriod (
72                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
73                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
74                                         )
75                                 );
76                 }
77
78                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("TextSubtitle")) {
79                         add_text_subtitle (
80                                 raw_convert<string> (i->string_child ("Id")),
81                                 ContentTimePeriod (
82                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
83                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
84                                         )
85                                 );
86                 }
87
88                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Colour")) {
89                         _colours[RGBA(i->node_child("From"))] = RGBA (i->node_child("To"));
90                 }
91         }
92 }
93
94 void
95 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
96 {
97         FFmpegStream::as_xml (root);
98
99         as_xml (root, _image_subtitles, "ImageSubtitle");
100         as_xml (root, _text_subtitles, "TextSubtitle");
101
102         for (map<RGBA, RGBA>::const_iterator i = _colours.begin(); i != _colours.end(); ++i) {
103                 xmlpp::Node* node = root->add_child("Colour");
104                 i->first.as_xml (node->add_child("From"));
105                 i->second.as_xml (node->add_child("To"));
106         }
107 }
108
109 void
110 FFmpegSubtitleStream::as_xml (xmlpp::Node* root, PeriodMap const & subs, string node_name) const
111 {
112         for (PeriodMap::const_iterator i = subs.begin(); i != subs.end(); ++i) {
113                 xmlpp::Node* node = root->add_child (node_name);
114                 node->add_child("Id")->add_child_text (i->first);
115                 node->add_child("From")->add_child_text (raw_convert<string> (i->second.from.get ()));
116                 node->add_child("To")->add_child_text (raw_convert<string> (i->second.to.get ()));
117         }
118 }
119
120 void
121 FFmpegSubtitleStream::add_image_subtitle (string id, ContentTimePeriod period)
122 {
123         DCPOMATIC_ASSERT (_image_subtitles.find (id) == _image_subtitles.end ());
124         _image_subtitles[id] = period;
125 }
126
127 void
128 FFmpegSubtitleStream::add_text_subtitle (string id, ContentTimePeriod period)
129 {
130         DCPOMATIC_ASSERT (_text_subtitles.find (id) == _text_subtitles.end ());
131         _text_subtitles[id] = period;
132 }
133
134 list<ContentTimePeriod>
135 FFmpegSubtitleStream::image_subtitles_during (ContentTimePeriod period, bool starting) const
136 {
137         return subtitles_during (period, starting, _image_subtitles);
138 }
139
140 list<ContentTimePeriod>
141 FFmpegSubtitleStream::text_subtitles_during (ContentTimePeriod period, bool starting) const
142 {
143         return subtitles_during (period, starting, _text_subtitles);
144 }
145
146 struct PeriodSorter
147 {
148         bool operator() (ContentTimePeriod const & a, ContentTimePeriod const & b) {
149                 return a.from < b.from;
150         }
151 };
152
153 list<ContentTimePeriod>
154 FFmpegSubtitleStream::subtitles_during (ContentTimePeriod period, bool starting, PeriodMap const & subs) const
155 {
156         list<ContentTimePeriod> d;
157
158         /* XXX: inefficient */
159         for (map<string, ContentTimePeriod>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
160                 if ((starting && period.contains(i->second.from)) || (!starting && period.overlap(i->second))) {
161                         d.push_back (i->second);
162                 }
163         }
164
165         d.sort (PeriodSorter ());
166
167         return d;
168 }
169
170 ContentTime
171 FFmpegSubtitleStream::find_subtitle_to (string id) const
172 {
173         PeriodMap::const_iterator i = _image_subtitles.find (id);
174         if (i != _image_subtitles.end ()) {
175                 return i->second.to;
176         }
177
178         i = _text_subtitles.find (id);
179         DCPOMATIC_ASSERT (i != _text_subtitles.end ());
180         return i->second.to;
181 }
182
183 /** Add some offset to all the times in the stream */
184 void
185 FFmpegSubtitleStream::add_offset (ContentTime offset)
186 {
187         for (PeriodMap::iterator i = _image_subtitles.begin(); i != _image_subtitles.end(); ++i) {
188                 i->second.from += offset;
189                 i->second.to += offset;
190         }
191
192         for (PeriodMap::iterator i = _text_subtitles.begin(); i != _text_subtitles.end(); ++i) {
193                 i->second.from += offset;
194                 i->second.to += offset;
195         }
196 }
197
198 map<RGBA, RGBA>
199 FFmpegSubtitleStream::colours () const
200 {
201         return _colours;
202 }
203
204 void
205 FFmpegSubtitleStream::set_colour (RGBA from, RGBA to)
206 {
207         _colours[from] = to;
208 }
209
210 bool
211 FFmpegSubtitleStream::has_text () const
212 {
213         return !_text_subtitles.empty ();
214 }
215
216 bool
217 FFmpegSubtitleStream::has_image () const
218 {
219         return !_image_subtitles.empty ();
220 }