2 Copyright (C) 2014-2021 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/>.
22 #include "dcp_subtitle_content.h"
23 #include "dcp_subtitle_decoder.h"
26 #include "text_content.h"
28 #include <dcp/interop_subtitle_asset.h>
29 #include <dcp/load_font_node.h>
32 using std::dynamic_pointer_cast;
34 using std::make_shared;
35 using std::shared_ptr;
38 using boost::optional;
39 using namespace dcpomatic;
42 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const Film> film, shared_ptr<const DCPSubtitleContent> content)
45 /* Load the XML or MXF file */
46 auto const asset = load (content->path(0));
47 asset->fix_empty_font_ids ();
48 _subtitles = asset->subtitles ();
49 _next = _subtitles.begin ();
51 _subtitle_standard = asset->subtitle_standard();
53 text.push_back (make_shared<TextDecoder>(this, content->only_text()));
59 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
61 Decoder::seek (time, accurate);
63 _next = _subtitles.begin ();
64 auto i = _subtitles.begin ();
65 while (i != _subtitles.end() && ContentTime::from_seconds ((*_next)->in().as_seconds()) < time) {
74 DCPSubtitleDecoder::pass ()
76 if (_next == _subtitles.end ()) {
80 /* Gather all subtitles with the same time period that are next
81 on the list. We must emit all subtitles for the same time
82 period with the same emit*() call otherwise the
83 TextDecoder will assume there is nothing else at the
84 time of emitting the first.
87 vector<dcp::SubtitleString> s;
88 vector<dcp::SubtitleImage> i;
89 auto const p = content_time_period (*_next);
91 while (_next != _subtitles.end () && content_time_period (*_next) == p) {
92 auto ns = dynamic_pointer_cast<const dcp::SubtitleString>(*_next);
97 /* XXX: perhaps these image subs should also be collected together like the string ones are;
98 this would need to be done both here and in DCPDecoder.
101 auto ni = dynamic_pointer_cast<const dcp::SubtitleImage>(*_next);
103 emit_subtitle_image (p, *ni, film()->frame_size(), only_text());
109 only_text()->emit_plain(p, s, _subtitle_standard);
118 DCPSubtitleDecoder::content_time_period (shared_ptr<const dcp::Subtitle> s) const
121 ContentTime::from_seconds(s->in().as_seconds()),
122 ContentTime::from_seconds(s->out().as_seconds())
127 /** @return time of first subtitle, if there is one */
128 optional<ContentTime>
129 DCPSubtitleDecoder::first () const
131 if (_subtitles.empty()) {
135 return ContentTime::from_seconds(_subtitles[0]->in().as_seconds());
140 DCPSubtitleDecoder::update_position()
142 if (_next != _subtitles.end()) {
143 only_text()->maybe_set_position(
144 ContentTime::from_seconds((*_next)->in().as_seconds())