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