Make jump-to-first-subtitle also work with DCP subs.
[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 c = load (content->path(0));
45         c->fix_empty_font_ids ();
46         _subtitles = c->subtitles ();
47         _next = _subtitles.begin ();
48
49         ContentTime first;
50         if (_next != _subtitles.end()) {
51                 first = content_time_period(*_next).from;
52         }
53         text.push_back (make_shared<TextDecoder>(this, content->only_text(), first));
54
55         /* The fonts that are included in content's file; if it's interop there will be none
56          * (as the fonts are held in separate assets).
57          */
58         auto fonts_in_asset = c->font_data();
59
60         /* Fonts specified in the TextContent */
61         list<shared_ptr<dcpomatic::Font>> fonts_in_content;
62         for (auto i: content->text) {
63                 auto this_fonts = i->fonts();
64                 std::copy(this_fonts.begin(), this_fonts.end(), std::back_inserter(fonts_in_content));
65         }
66         fonts_in_content.sort();
67         fonts_in_content.unique();
68
69         /* Find a font for each <LoadFont> Node */
70         for (auto i: c->load_font_nodes()) {
71                 bool done = false;
72                 for (auto j: fonts_in_content) {
73                         if (j->id() == i->id && j->file()) {
74                                 // One was specified in the content
75                                 _fonts.push_back (FontData(i->id, dcp::ArrayData(*j->file())));
76                                 done = true;
77                         }
78                 }
79                 if (!done) {
80                         if (fonts_in_asset.find(i->id) != fonts_in_asset.end()) {
81                                 // One was included in the subtitle file
82                                 _fonts.push_back (FontData(i->id, fonts_in_asset[i->id]));
83                                 done = true;
84                         }
85                 }
86                 if (!done) {
87                         // Give up and add a default
88                         _fonts.push_back (FontData(i->id, dcp::ArrayData(default_font_file())));
89                 }
90         }
91 }
92
93
94 void
95 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
96 {
97         Decoder::seek (time, accurate);
98
99         _next = _subtitles.begin ();
100         auto i = _subtitles.begin ();
101         while (i != _subtitles.end() && ContentTime::from_seconds ((*_next)->in().as_seconds()) < time) {
102                 ++i;
103         }
104 }
105
106
107 bool
108 DCPSubtitleDecoder::pass ()
109 {
110         if (_next == _subtitles.end ()) {
111                 return true;
112         }
113
114         /* Gather all subtitles with the same time period that are next
115            on the list.  We must emit all subtitles for the same time
116            period with the same emit*() call otherwise the
117            TextDecoder will assume there is nothing else at the
118            time of emitting the first.
119         */
120
121         list<dcp::SubtitleString> s;
122         list<dcp::SubtitleImage> i;
123         auto const p = content_time_period (*_next);
124
125         while (_next != _subtitles.end () && content_time_period (*_next) == p) {
126                 auto ns = dynamic_pointer_cast<const dcp::SubtitleString>(*_next);
127                 if (ns) {
128                         s.push_back (*ns);
129                         ++_next;
130                 } else {
131                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
132                            this would need to be done both here and in DCPDecoder.
133                         */
134
135                         auto ni = dynamic_pointer_cast<const dcp::SubtitleImage>(*_next);
136                         if (ni) {
137                                 emit_subtitle_image (p, *ni, film()->frame_size(), only_text());
138                                 ++_next;
139                         }
140                 }
141         }
142
143         only_text()->emit_plain (p, s);
144         return false;
145 }
146
147
148 ContentTimePeriod
149 DCPSubtitleDecoder::content_time_period (shared_ptr<const dcp::Subtitle> s) const
150 {
151         return {
152                 ContentTime::from_seconds(s->in().as_seconds()),
153                 ContentTime::from_seconds(s->out().as_seconds())
154         };
155 }
156
157
158 vector<dcpomatic::FontData>
159 DCPSubtitleDecoder::fonts () const
160 {
161         return _fonts;
162 }
163
164
165 /** @return time of first subtitle, if there is one */
166 optional<ContentTime>
167 DCPSubtitleDecoder::first () const
168 {
169         if (_subtitles.empty()) {
170                 return {};
171         }
172
173         return ContentTime::from_seconds(_subtitles[0]->in().as_seconds());
174 }