b3240491f829dd0d64fa92f0128569e13b5214d4
[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_decoder.h"
23 #include "dcp_subtitle_content.h"
24 #include <dcp/interop_subtitle_asset.h>
25 #include <dcp/load_font_node.h>
26 #include <iostream>
27
28
29 using std::cout;
30 using std::list;
31 using std::string;
32 using std::vector;
33 using std::shared_ptr;
34 using std::dynamic_pointer_cast;
35 using std::make_shared;
36 using namespace dcpomatic;
37
38
39 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const Film> film, shared_ptr<const DCPSubtitleContent> content)
40         : Decoder (film)
41 {
42         auto c = load (content->path(0));
43         c->fix_empty_font_ids ();
44         _subtitles = c->subtitles ();
45         _next = _subtitles.begin ();
46
47         ContentTime first;
48         if (_next != _subtitles.end()) {
49                 first = content_time_period(*_next).from;
50         }
51         text.push_back (make_shared<TextDecoder>(this, content->only_text(), first));
52
53         auto fm = c->font_data();
54         for (auto const& i: fm) {
55                 _fonts.push_back (FontData(i.first, i.second));
56         }
57
58         /* Add a default font for any LoadFont nodes in our file which we haven't yet found fonts for */
59         for (auto i: c->load_font_nodes()) {
60                 if (fm.find(i->id) == fm.end()) {
61                         _fonts.push_back (FontData(i->id, dcp::ArrayData(default_font_file())));
62                 }
63         }
64 }
65
66
67 void
68 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
69 {
70         Decoder::seek (time, accurate);
71
72         _next = _subtitles.begin ();
73         auto i = _subtitles.begin ();
74         while (i != _subtitles.end() && ContentTime::from_seconds ((*_next)->in().as_seconds()) < time) {
75                 ++i;
76         }
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         list<dcp::SubtitleString> s;
95         list<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                         s.push_back (*ns);
102                         ++_next;
103                 } else {
104                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
105                            this would need to be done both here and in DCPDecoder.
106                         */
107
108                         auto ni = dynamic_pointer_cast<const dcp::SubtitleImage>(*_next);
109                         if (ni) {
110                                 emit_subtitle_image (p, *ni, film()->frame_size(), only_text());
111                                 ++_next;
112                         }
113                 }
114         }
115
116         only_text()->emit_plain (p, s);
117         return false;
118 }
119
120
121 ContentTimePeriod
122 DCPSubtitleDecoder::content_time_period (shared_ptr<const dcp::Subtitle> s) const
123 {
124         return {
125                 ContentTime::from_seconds(s->in().as_seconds()),
126                 ContentTime::from_seconds(s->out().as_seconds())
127         };
128 }
129
130
131 vector<dcpomatic::FontData>
132 DCPSubtitleDecoder::fonts () const
133 {
134         return _fonts;
135 }
136