Setup SubtitleDecoder::_position correctly (in some cases).
[dcpomatic.git] / src / lib / dcp_subtitle_decoder.cc
1 /*
2     Copyright (C) 2014-2016 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::bind;
30
31 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const DCPSubtitleContent> content, shared_ptr<Log> log)
32 {
33         shared_ptr<dcp::SubtitleAsset> c (load (content->path (0)));
34         _subtitles = c->subtitles ();
35         _next = _subtitles.begin ();
36
37         ContentTime first;
38         if (_next != _subtitles.end()) {
39                 first = content_time_period(*_next).from;
40         }
41         subtitle.reset (new SubtitleDecoder (this, content->subtitle, log, first));
42 }
43
44 void
45 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
46 {
47         Decoder::seek (time, accurate);
48
49         _next = _subtitles.begin ();
50         list<dcp::SubtitleString>::const_iterator i = _subtitles.begin ();
51         while (i != _subtitles.end() && ContentTime::from_seconds (_next->in().as_seconds()) < time) {
52                 ++i;
53         }
54 }
55
56 bool
57 DCPSubtitleDecoder::pass ()
58 {
59         if (_next == _subtitles.end ()) {
60                 return true;
61         }
62
63         /* Gather all subtitles with the same time period that are next
64            on the list.  We must emit all subtitles for the same time
65            period with the same text_subtitle() call otherwise the
66            SubtitleDecoder will assume there is nothing else at the
67            time of emit the first.
68         */
69
70         list<dcp::SubtitleString> s;
71         ContentTimePeriod const p = content_time_period (*_next);
72
73         while (_next != _subtitles.end () && content_time_period (*_next) == p) {
74                 s.push_back (*_next);
75                 ++_next;
76         }
77
78         subtitle->emit_text (p, s);
79         return false;
80 }
81
82 ContentTimePeriod
83 DCPSubtitleDecoder::content_time_period (dcp::SubtitleString s) const
84 {
85         return ContentTimePeriod (
86                 ContentTime::from_seconds (s.in().as_seconds ()),
87                 ContentTime::from_seconds (s.out().as_seconds ())
88                 );
89 }