1d3a53609e39d87f27ce12aea1dec4a3055938b4
[dcpomatic.git] / src / lib / active_captions.cc
1 /*
2     Copyright (C) 2017-2018 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 "active_captions.h"
22 #include "caption_content.h"
23 #include <boost/shared_ptr.hpp>
24 #include <boost/weak_ptr.hpp>
25
26 using std::list;
27 using std::pair;
28 using std::make_pair;
29 using boost::weak_ptr;
30 using boost::shared_ptr;
31 using boost::optional;
32
33 void
34 ActiveCaptions::add (DCPTimePeriod period, list<PlayerCaption>& pc, list<Period> p) const
35 {
36         BOOST_FOREACH (Period i, p) {
37                 DCPTimePeriod test (i.from, i.to.get_value_or(DCPTime::max()));
38                 optional<DCPTimePeriod> overlap = period.overlap (test);
39                 if (overlap && overlap->duration() > DCPTime(period.duration().get() / 2)) {
40                         pc.push_back (i.subs);
41                 }
42         }
43 }
44
45 list<PlayerCaption>
46 ActiveCaptions::get (DCPTimePeriod period) const
47 {
48         list<PlayerCaption> ps;
49
50         for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
51
52                 shared_ptr<const CaptionContent> caption = i->first.lock ();
53                 if (!caption || !caption->use()) {
54                         continue;
55                 }
56
57                 add (period, ps, i->second);
58         }
59
60         return ps;
61 }
62
63 /** Get the open captions that should be burnt into a given period.
64  *  @param period Period of interest.
65  *  @param always_burn_captions Always burn captions even if their content is not set to burn.
66  */
67 list<PlayerCaption>
68 ActiveCaptions::get_burnt (DCPTimePeriod period, bool always_burn_captions) const
69 {
70         list<PlayerCaption> ps;
71
72         for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
73
74                 shared_ptr<const CaptionContent> caption = i->first.lock ();
75                 if (!caption) {
76                         continue;
77                 }
78
79                 if (!caption->use() || (!always_burn_captions && !caption->burn())) {
80                         /* Not burning this content */
81                         continue;
82                 }
83
84                 add (period, ps, i->second);
85         }
86
87         return ps;
88 }
89
90 /** Remove subtitles that finish before a given time from our list.
91  *  @param time Time to remove before.
92  */
93 void
94 ActiveCaptions::clear_before (DCPTime time)
95 {
96         Map updated;
97         for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
98                 list<Period> as;
99                 BOOST_FOREACH (Period j, i->second) {
100                         if (!j.to || j.to.get() >= time) {
101                                 as.push_back (j);
102                         }
103                 }
104                 if (!as.empty ()) {
105                         updated[i->first] = as;
106                 }
107         }
108         _data = updated;
109 }
110
111 /** Add a new subtitle with a from time.
112  *  @param content Content that the subtitle is from.
113  *  @param ps Subtitles.
114  *  @param from From time for these subtitles.
115  */
116 void
117 ActiveCaptions::add_from (weak_ptr<const CaptionContent> content, PlayerCaption ps, DCPTime from)
118 {
119         if (_data.find(content) == _data.end()) {
120                 _data[content] = list<Period>();
121         }
122         _data[content].push_back (Period (ps, from));
123 }
124
125 /** Add the to time for the last subtitle added from a piece of content.
126  *  @param content Content that the subtitle is from.
127  *  @param to To time for the last subtitle submitted to add_from for this content.
128  *  @return Return the corresponding subtitles and their from time.
129  */
130 pair<PlayerCaption, DCPTime>
131 ActiveCaptions::add_to (weak_ptr<const CaptionContent> content, DCPTime to)
132 {
133         DCPOMATIC_ASSERT (_data.find(content) != _data.end());
134
135         _data[content].back().to = to;
136
137         BOOST_FOREACH (TextCaption& i, _data[content].back().subs.text) {
138                 i.set_out (dcp::Time(to.seconds(), 1000));
139         }
140
141         return make_pair (_data[content].back().subs, _data[content].back().from);
142 }
143
144 /** @param content Some content.
145  *  @return true if we have any active subtitles from this content.
146  */
147 bool
148 ActiveCaptions::have (weak_ptr<const CaptionContent> content) const
149 {
150         Map::const_iterator i = _data.find(content);
151         if (i == _data.end()) {
152                 return false;
153         }
154
155         return !i->second.empty();
156 }
157
158 void
159 ActiveCaptions::clear ()
160 {
161         _data.clear ();
162 }