Support SSA subtitles embedded within FFmpeg files.
[dcpomatic.git] / src / lib / ffmpeg_subtitle_stream.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "ffmpeg_subtitle_stream.h"
21 #include "raw_convert.h"
22 #include <libxml++/libxml++.h>
23 #include <boost/foreach.hpp>
24 #include <iostream>
25
26 using std::string;
27 using std::map;
28 using std::list;
29 using std::cout;
30
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.
34  */
35 FFmpegSubtitleStream::FFmpegSubtitleStream (cxml::ConstNodePtr node, int version)
36         : FFmpegStream (node)
37 {
38         if (version == 32) {
39                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Period")) {
40                         /* In version 32 we assumed that from times were unique, so they were
41                            used as identifiers.  All subtitles were image subtitles.
42                         */
43                         add_image_subtitle (
44                                 raw_convert<string> (i->string_child ("From")),
45                                 ContentTimePeriod (
46                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
47                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
48                                         )
49                                 );
50                 }
51         } else {
52                 /* In version 33 we use a hash of various parts of the subtitle as the id.
53                    <Subtitle> was initially used for image subtitles; later we have
54                    <ImageSubtitle> and <TextSubtitle>
55                 */
56                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Subtitle")) {
57                         add_image_subtitle (
58                                 raw_convert<string> (i->string_child ("Id")),
59                                 ContentTimePeriod (
60                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
61                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
62                                         )
63                                 );
64                 }
65
66                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("ImageSubtitle")) {
67                         add_image_subtitle (
68                                 raw_convert<string> (i->string_child ("Id")),
69                                 ContentTimePeriod (
70                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
71                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
72                                         )
73                                 );
74                 }
75
76                 BOOST_FOREACH (cxml::NodePtr i, node->node_children ("TextSubtitle")) {
77                         add_text_subtitle (
78                                 raw_convert<string> (i->string_child ("Id")),
79                                 ContentTimePeriod (
80                                         ContentTime (i->number_child<ContentTime::Type> ("From")),
81                                         ContentTime (i->number_child<ContentTime::Type> ("To"))
82                                         )
83                                 );
84                 }
85         }
86 }
87
88 void
89 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
90 {
91         FFmpegStream::as_xml (root);
92
93         as_xml (root, _image_subtitles, "ImageSubtitle");
94         as_xml (root, _text_subtitles, "TextSubtitle");
95 }
96
97 void
98 FFmpegSubtitleStream::as_xml (xmlpp::Node* root, PeriodMap const & subs, string node_name) const
99 {
100         for (PeriodMap::const_iterator i = subs.begin(); i != subs.end(); ++i) {
101                 xmlpp::Node* node = root->add_child (node_name);
102                 node->add_child("Id")->add_child_text (i->first);
103                 node->add_child("From")->add_child_text (raw_convert<string> (i->second.from.get ()));
104                 node->add_child("To")->add_child_text (raw_convert<string> (i->second.to.get ()));
105         }
106 }
107
108 void
109 FFmpegSubtitleStream::add_image_subtitle (string id, ContentTimePeriod period)
110 {
111         DCPOMATIC_ASSERT (_image_subtitles.find (id) == _image_subtitles.end ());
112         _image_subtitles[id] = period;
113 }
114
115 void
116 FFmpegSubtitleStream::add_text_subtitle (string id, ContentTimePeriod period)
117 {
118         DCPOMATIC_ASSERT (_text_subtitles.find (id) == _text_subtitles.end ());
119         _text_subtitles[id] = period;
120 }
121
122 list<ContentTimePeriod>
123 FFmpegSubtitleStream::image_subtitles_during (ContentTimePeriod period, bool starting) const
124 {
125         return subtitles_during (period, starting, _image_subtitles);
126 }
127
128 list<ContentTimePeriod>
129 FFmpegSubtitleStream::text_subtitles_during (ContentTimePeriod period, bool starting) const
130 {
131         return subtitles_during (period, starting, _text_subtitles);
132 }
133
134 list<ContentTimePeriod>
135 FFmpegSubtitleStream::subtitles_during (ContentTimePeriod period, bool starting, PeriodMap const & subs) const
136 {
137         list<ContentTimePeriod> d;
138
139         /* XXX: inefficient */
140         for (map<string, ContentTimePeriod>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
141                 if ((starting && period.contains (i->second.from)) || (!starting && period.overlaps (i->second))) {
142                         d.push_back (i->second);
143                 }
144         }
145
146         return d;
147 }
148
149 ContentTime
150 FFmpegSubtitleStream::find_subtitle_to (string id) const
151 {
152         PeriodMap::const_iterator i = _image_subtitles.find (id);
153         if (i != _image_subtitles.end ()) {
154                 return i->second.to;
155         }
156
157         i = _text_subtitles.find (id);
158         DCPOMATIC_ASSERT (i != _text_subtitles.end ());
159         return i->second.to;
160 }
161
162 /** Add some offset to all the times in the stream */
163 void
164 FFmpegSubtitleStream::add_offset (ContentTime offset)
165 {
166         for (PeriodMap::iterator i = _image_subtitles.begin(); i != _image_subtitles.end(); ++i) {
167                 i->second.from += offset;
168                 i->second.to += offset;
169         }
170
171         for (PeriodMap::iterator i = _text_subtitles.begin(); i != _text_subtitles.end(); ++i) {
172                 i->second.from += offset;
173                 i->second.to += offset;
174         }
175 }