2 Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "active_captions.h"
23 #include "caption_content.h"
24 #include <boost/shared_ptr.hpp>
25 #include <boost/weak_ptr.hpp>
30 using boost::weak_ptr;
31 using boost::shared_ptr;
32 using boost::optional;
34 /** Get the subtitles that should be burnt into a given period.
35 * @param period Period of interest.
36 * @param always_burn_captions Always burn captions even if their content is not set to burn.
39 ActiveCaptions::get_burnt (DCPTimePeriod period, bool always_burn_captions) const
41 list<PlayerCaption> ps;
43 for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
45 shared_ptr<Piece> piece = i->first.lock ();
50 if (!piece->content->caption->use() || (!always_burn_captions && !piece->content->caption->burn())) {
51 /* Not burning this piece */
55 BOOST_FOREACH (Period j, i->second) {
56 DCPTimePeriod test (j.from, j.to.get_value_or(DCPTime::max()));
57 optional<DCPTimePeriod> overlap = period.overlap (test);
58 if (overlap && overlap->duration() > DCPTime(period.duration().get() / 2)) {
59 ps.push_back (j.subs);
67 /** Remove subtitles that finish before a given time from our list.
68 * @param time Time to remove before.
71 ActiveCaptions::clear_before (DCPTime time)
74 for (Map::const_iterator i = _data.begin(); i != _data.end(); ++i) {
76 BOOST_FOREACH (Period j, i->second) {
77 if (!j.to || j.to.get() >= time) {
82 updated[i->first] = as;
88 /** Add a new subtitle with a from time.
89 * @param piece Piece that the subtitle is from.
90 * @param ps Subtitles.
91 * @param from From time for these subtitles.
94 ActiveCaptions::add_from (weak_ptr<Piece> piece, PlayerCaption ps, DCPTime from)
96 if (_data.find(piece) == _data.end()) {
97 _data[piece] = list<Period>();
99 _data[piece].push_back (Period (ps, from));
102 /** Add the to time for the last subtitle added from a piece.
103 * @param piece Piece that the subtitle is from.
104 * @param to To time for the last subtitle submitted to add_from for this piece.
105 * @return Return the corresponding subtitles and their from time.
107 pair<PlayerCaption, DCPTime>
108 ActiveCaptions::add_to (weak_ptr<Piece> piece, DCPTime to)
110 DCPOMATIC_ASSERT (_data.find(piece) != _data.end());
112 _data[piece].back().to = to;
114 BOOST_FOREACH (TextCaption& i, _data[piece].back().subs.text) {
115 i.set_out (dcp::Time(to.seconds(), 1000));
118 return make_pair (_data[piece].back().subs, _data[piece].back().from);
121 /** @param piece A piece.
122 * @return true if we have any active subtitles from this piece.
125 ActiveCaptions::have (weak_ptr<Piece> piece) const
127 Map::const_iterator i = _data.find(piece);
128 if (i == _data.end()) {
132 return !i->second.empty();
136 ActiveCaptions::clear ()