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