More automated renaming.
[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
32 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const DCPSubtitleContent> content, shared_ptr<Log> log)
33 {
34         shared_ptr<dcp::SubtitleAsset> c (load (content->path (0)));
35         _subtitles = c->subtitles ();
36         _next = _subtitles.begin ();
37
38         ContentTime first;
39         if (_next != _subtitles.end()) {
40                 first = content_time_period(*_next).from;
41         }
42         caption.push_back (shared_ptr<TextDecoder> (new TextDecoder (this, content->only_caption(), log, first)));
43 }
44
45 void
46 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
47 {
48         Decoder::seek (time, accurate);
49
50         _next = _subtitles.begin ();
51         list<shared_ptr<dcp::Subtitle> >::const_iterator i = _subtitles.begin ();
52         while (i != _subtitles.end() && ContentTime::from_seconds ((*_next)->in().as_seconds()) < time) {
53                 ++i;
54         }
55 }
56
57 bool
58 DCPSubtitleDecoder::pass ()
59 {
60         if (_next == _subtitles.end ()) {
61                 return true;
62         }
63
64         /* Gather all subtitles with the same time period that are next
65            on the list.  We must emit all subtitles for the same time
66            period with the same plain_text() call otherwise the
67            TextDecoder will assume there is nothing else at the
68            time of emit the first.
69         */
70
71         list<dcp::SubtitleString> s;
72         ContentTimePeriod const p = content_time_period (*_next);
73
74         while (_next != _subtitles.end () && content_time_period (*_next) == p) {
75                 shared_ptr<dcp::SubtitleString> ns = dynamic_pointer_cast<dcp::SubtitleString>(*_next);
76                 if (ns) {
77                         s.push_back (*ns);
78                         ++_next;
79                 }
80
81                 /* XXX: image subtitles */
82         }
83
84         only_caption()->emit_plain (p, s);
85         return false;
86 }
87
88 ContentTimePeriod
89 DCPSubtitleDecoder::content_time_period (shared_ptr<dcp::Subtitle> s) const
90 {
91         return ContentTimePeriod (
92                 ContentTime::from_seconds (s->in().as_seconds ()),
93                 ContentTime::from_seconds (s->out().as_seconds ())
94                 );
95 }