Supporters update.
[dcpomatic.git] / src / lib / dcp_subtitle_decoder.cc
1 /*
2     Copyright (C) 2014-2021 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
22 #include "dcp_subtitle_content.h"
23 #include "dcp_subtitle_decoder.h"
24 #include "font.h"
25 #include "text_content.h"
26 #include <dcp/interop_subtitle_asset.h>
27 #include <dcp/load_font_node.h>
28
29
30 using std::dynamic_pointer_cast;
31 using std::list;
32 using std::make_shared;
33 using std::shared_ptr;
34 using std::string;
35 using std::vector;
36 using boost::optional;
37 using namespace dcpomatic;
38
39
40 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const Film> film, shared_ptr<const DCPSubtitleContent> content)
41         : Decoder (film)
42 {
43         /* Load the XML or MXF file */
44         auto const asset = load (content->path(0));
45         asset->fix_empty_font_ids ();
46         _subtitles = asset->subtitles ();
47         _next = _subtitles.begin ();
48
49         if (dynamic_pointer_cast<dcp::InteropSubtitleAsset>(asset)) {
50                 _standard = dcp::Standard::INTEROP;
51         } else {
52                 _standard = dcp::Standard::SMPTE;
53         }
54
55         text.push_back (make_shared<TextDecoder>(this, content->only_text()));
56         update_position();
57 }
58
59
60 void
61 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
62 {
63         Decoder::seek (time, accurate);
64
65         _next = _subtitles.begin ();
66         auto i = _subtitles.begin ();
67         while (i != _subtitles.end() && ContentTime::from_seconds ((*_next)->in().as_seconds()) < time) {
68                 ++i;
69         }
70
71         update_position();
72 }
73
74
75 bool
76 DCPSubtitleDecoder::pass ()
77 {
78         if (_next == _subtitles.end ()) {
79                 return true;
80         }
81
82         /* Gather all subtitles with the same time period that are next
83            on the list.  We must emit all subtitles for the same time
84            period with the same emit*() call otherwise the
85            TextDecoder will assume there is nothing else at the
86            time of emitting the first.
87         */
88
89         vector<dcp::SubtitleString> s;
90         vector<dcp::SubtitleImage> i;
91         auto const p = content_time_period (*_next);
92
93         while (_next != _subtitles.end () && content_time_period (*_next) == p) {
94                 auto ns = dynamic_pointer_cast<const dcp::SubtitleString>(*_next);
95                 if (ns) {
96                         s.push_back (*ns);
97                         ++_next;
98                 } else {
99                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
100                            this would need to be done both here and in DCPDecoder.
101                         */
102
103                         auto ni = dynamic_pointer_cast<const dcp::SubtitleImage>(*_next);
104                         if (ni) {
105                                 emit_subtitle_image (p, *ni, film()->frame_size(), only_text());
106                                 ++_next;
107                         }
108                 }
109         }
110
111         only_text()->emit_plain(p, s, _standard);
112
113         update_position();
114
115         return false;
116 }
117
118
119 ContentTimePeriod
120 DCPSubtitleDecoder::content_time_period (shared_ptr<const dcp::Subtitle> s) const
121 {
122         return {
123                 ContentTime::from_seconds(s->in().as_seconds()),
124                 ContentTime::from_seconds(s->out().as_seconds())
125         };
126 }
127
128
129 /** @return time of first subtitle, if there is one */
130 optional<ContentTime>
131 DCPSubtitleDecoder::first () const
132 {
133         if (_subtitles.empty()) {
134                 return {};
135         }
136
137         return ContentTime::from_seconds(_subtitles[0]->in().as_seconds());
138 }
139
140
141 void
142 DCPSubtitleDecoder::update_position()
143 {
144         if (_next != _subtitles.end()) {
145                 only_text()->maybe_set_position(
146                         ContentTime::from_seconds((*_next)->in().as_seconds())
147                         );
148         }
149 }
150