Accessor for ClosedCaptionsDialog.
[dcpomatic.git] / src / lib / dcp_subtitle_decoder.cc
1 /*
2     Copyright (C) 2014-2018 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 <iostream>
25
26 using std::list;
27 using std::cout;
28 using boost::shared_ptr;
29 using boost::dynamic_pointer_cast;
30 using boost::bind;
31 using namespace dcpomatic;
32
33 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const Film> film, shared_ptr<const DCPSubtitleContent> content)
34         : Decoder (film)
35 {
36         shared_ptr<dcp::SubtitleAsset> c (load (content->path (0)));
37         c->fix_empty_font_ids ();
38         _subtitles = c->subtitles ();
39         _next = _subtitles.begin ();
40
41         ContentTime first;
42         if (_next != _subtitles.end()) {
43                 first = content_time_period(*_next).from;
44         }
45         text.push_back (shared_ptr<TextDecoder> (new TextDecoder (this, content->only_text(), first)));
46 }
47
48 void
49 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
50 {
51         Decoder::seek (time, accurate);
52
53         _next = _subtitles.begin ();
54         list<shared_ptr<dcp::Subtitle> >::const_iterator i = _subtitles.begin ();
55         while (i != _subtitles.end() && ContentTime::from_seconds ((*_next)->in().as_seconds()) < time) {
56                 ++i;
57         }
58 }
59
60 bool
61 DCPSubtitleDecoder::pass ()
62 {
63         if (_next == _subtitles.end ()) {
64                 return true;
65         }
66
67         /* Gather all subtitles with the same time period that are next
68            on the list.  We must emit all subtitles for the same time
69            period with the same emit*() call otherwise the
70            TextDecoder will assume there is nothing else at the
71            time of emitting the first.
72         */
73
74         list<dcp::SubtitleString> s;
75         list<dcp::SubtitleImage> i;
76         ContentTimePeriod const p = content_time_period (*_next);
77
78         while (_next != _subtitles.end () && content_time_period (*_next) == p) {
79                 shared_ptr<dcp::SubtitleString> ns = dynamic_pointer_cast<dcp::SubtitleString>(*_next);
80                 if (ns) {
81                         s.push_back (*ns);
82                         ++_next;
83                 } else {
84                         /* XXX: perhaps these image subs should also be collected together like the string ones are;
85                            this would need to be done both here and in DCPDecoder.
86                         */
87
88                         shared_ptr<dcp::SubtitleImage> ni = dynamic_pointer_cast<dcp::SubtitleImage>(*_next);
89                         if (ni) {
90                                 emit_subtitle_image (p, *ni, film()->frame_size(), only_text());
91                                 ++_next;
92                         }
93                 }
94         }
95
96         only_text()->emit_plain (p, s);
97         return false;
98 }
99
100 ContentTimePeriod
101 DCPSubtitleDecoder::content_time_period (shared_ptr<dcp::Subtitle> s) const
102 {
103         return ContentTimePeriod (
104                 ContentTime::from_seconds (s->in().as_seconds ()),
105                 ContentTime::from_seconds (s->out().as_seconds ())
106                 );
107 }