Fix problems with subtitles when there is a non-zero PTS offset
[dcpomatic.git] / src / lib / ffmpeg_subtitle_stream.cc
1 /*
2     Copyright (C) 2013-2014 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)
36         : FFmpegStream (node)
37 {
38         BOOST_FOREACH (cxml::NodePtr i, node->node_children ("Period")) {
39                 add_subtitle (
40                         ContentTimePeriod (
41                                 ContentTime (i->number_child<ContentTime::Type> ("From")),
42                                 ContentTime (i->number_child<ContentTime::Type> ("To"))
43                                 )
44                         );
45         }
46 }
47
48 void
49 FFmpegSubtitleStream::as_xml (xmlpp::Node* root) const
50 {
51         FFmpegStream::as_xml (root);
52
53         for (map<ContentTime, ContentTime>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
54                 xmlpp::Node* node = root->add_child ("Period");
55                 node->add_child("From")->add_child_text (raw_convert<string> (i->first.get ()));
56                 node->add_child("To")->add_child_text (raw_convert<string> (i->second.get ()));
57         }
58 }
59
60 void
61 FFmpegSubtitleStream::add_subtitle (ContentTimePeriod period)
62 {
63         DCPOMATIC_ASSERT (_subtitles.find (period.from) == _subtitles.end ());
64         _subtitles[period.from] = period.to;
65 }
66
67 list<ContentTimePeriod>
68 FFmpegSubtitleStream::subtitles_during (ContentTimePeriod period, bool starting) const
69 {
70         list<ContentTimePeriod> d;
71
72         /* XXX: inefficient */
73         for (map<ContentTime, ContentTime>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
74                 if ((starting && period.contains (i->first)) || (!starting && period.overlaps (ContentTimePeriod (i->first, i->second)))) {
75                         d.push_back (ContentTimePeriod (i->first, i->second));
76                 }
77         }
78
79         return d;
80 }
81
82 ContentTime
83 FFmpegSubtitleStream::find_subtitle_to (ContentTime from) const
84 {
85         map<ContentTime, ContentTime>::const_iterator i = _subtitles.find (from);
86         DCPOMATIC_ASSERT (i != _subtitles.end ());
87         return i->second;
88 }
89
90 /** Add some offset to all the times in the stream */
91 void
92 FFmpegSubtitleStream::add_offset (ContentTime offset)
93 {
94         map<ContentTime, ContentTime> fixed;
95         for (map<ContentTime, ContentTime>::iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
96                 fixed[i->first + offset] = i->second + offset;
97         }
98         _subtitles = fixed;
99 }