16a738a5135eedc5407e55f6185554faefc14d1a
[dcpomatic.git] / src / lib / dcp_subtitle_decoder.cc
1 /*
2     Copyright (C) 2014-2020 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 #include "dcp_subtitle_decoder.h"
22 #include "dcp_subtitle_content.h"
23 #include <dcp/interop_subtitle_asset.h>
24 #include <dcp/load_font_node.h>
25 #include <iostream>
26
27 using std::cout;
28 using std::list;
29 using std::map;
30 using std::string;
31 using std::vector;
32 using boost::shared_ptr;
33 using boost::dynamic_pointer_cast;
34 using boost::bind;
35 using namespace dcpomatic;
36
37 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const Film> film, shared_ptr<const DCPSubtitleContent> content)
38         : Decoder (film)
39 {
40         shared_ptr<dcp::SubtitleAsset> c (load (content->path (0)));
41         c->fix_empty_font_ids ();
42         _subtitles = c->subtitles ();
43         _next = _subtitles.begin ();
44
45         ContentTime first;
46         if (_next != _subtitles.end()) {
47                 first = content_time_period(*_next).from;
48         }
49         text.push_back (shared_ptr<TextDecoder> (new TextDecoder (this, content->only_text(), first)));
50
51         map<string, dcp::ArrayData> fm = c->font_data();
52         for (map<string, dcp::ArrayData>::const_iterator j = fm.begin(); j != fm.end(); ++j) {
53                 _fonts.push_back (FontData(j->first, j->second));
54         }
55
56         /* Add a default font for any LoadFont nodes in our file which we haven't yet found fonts for */
57         BOOST_FOREACH (shared_ptr<dcp::LoadFontNode> i, c->load_font_nodes()) {
58                 if (fm.find(i->id) == fm.end()) {
59                         _fonts.push_back (FontData(i->id, dcp::ArrayData(default_font_file())));
60                 }
61         }
62 }
63
64 void
65 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
66 {
67         Decoder::seek (time, accurate);
68
69         _next = _subtitles.begin ();
70         list<shared_ptr<dcp::Subtitle> >::const_iterator i = _subtitles.begin ();
71         while (i != _subtitles.end() && ContentTime::from_seconds ((*_next)->in().as_seconds()) < time) {
72                 ++i;
73         }
74 }
75
76 bool
77 DCPSubtitleDecoder::pass ()
78 {
79         if (_next == _subtitles.end ()) {
80                 return true;
81         }
82
83         /* Gather all subtitles with the same time period that are next
84            on the list.  We must emit all subtitles for the same time
85            period with the same emit*() call otherwise the
86            TextDecoder will assume there is nothing else at the
87            time of emitting the first.
88         */
89
90         list<dcp::SubtitleString> s;
91         list<dcp::SubtitleImage> i;
92         ContentTimePeriod const p = content_time_period (*_next);
93
94         while (_next != _subtitles.end () && content_time_period (*_next) == p) {
95                 shared_ptr<dcp::SubtitleString> ns = dynamic_pointer_cast<dcp::SubtitleString>(*_next);
96                 if (ns) {
97                         s.push_back (*ns);
98                         ++_next;
99                 } else {
100                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
101                            this would need to be done both here and in DCPDecoder.
102                         */
103
104                         shared_ptr<dcp::SubtitleImage> ni = dynamic_pointer_cast<dcp::SubtitleImage>(*_next);
105                         if (ni) {
106                                 emit_subtitle_image (p, *ni, film()->frame_size(), only_text());
107                                 ++_next;
108                         }
109                 }
110         }
111
112         only_text()->emit_plain (p, s);
113         return false;
114 }
115
116 ContentTimePeriod
117 DCPSubtitleDecoder::content_time_period (shared_ptr<dcp::Subtitle> s) const
118 {
119         return ContentTimePeriod (
120                 ContentTime::from_seconds (s->in().as_seconds ()),
121                 ContentTime::from_seconds (s->out().as_seconds ())
122                 );
123 }
124
125
126 vector<dcpomatic::FontData>
127 DCPSubtitleDecoder::fonts () const
128 {
129         return _fonts;
130 }
131