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