Rename some methods.
[dcpomatic.git] / src / lib / dcp_subtitle_decoder.cc
1 /*
2     Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "dcp_subtitle_decoder.h"
21 #include "dcp_subtitle_content.h"
22 #include <dcp/interop_subtitle_asset.h>
23 #include <iostream>
24
25 using std::list;
26 using std::cout;
27 using boost::shared_ptr;
28 using boost::bind;
29
30 DCPSubtitleDecoder::DCPSubtitleDecoder (shared_ptr<const DCPSubtitleContent> content)
31 {
32         subtitle.reset (
33                 new SubtitleDecoder (
34                         this,
35                         content->subtitle,
36                         bind (&DCPSubtitleDecoder::image_subtitles_during, this, _1, _2),
37                         bind (&DCPSubtitleDecoder::text_subtitles_during, this, _1, _2)
38                         )
39                 );
40
41         shared_ptr<dcp::SubtitleAsset> c (load (content->path (0)));
42         _subtitles = c->subtitles ();
43         _next = _subtitles.begin ();
44 }
45
46 void
47 DCPSubtitleDecoder::seek (ContentTime time, bool accurate)
48 {
49         subtitle->seek (time, accurate);
50
51         _next = _subtitles.begin ();
52         list<dcp::SubtitleString>::const_iterator i = _subtitles.begin ();
53         while (i != _subtitles.end() && ContentTime::from_seconds (_next->in().as_seconds()) < time) {
54                 ++i;
55         }
56 }
57
58 bool
59 DCPSubtitleDecoder::pass (PassReason, bool)
60 {
61         if (_next == _subtitles.end ()) {
62                 return true;
63         }
64
65         /* Gather all subtitles with the same time period that are next
66            on the list.  We must emit all subtitles for the same time
67            period with the same text_subtitle() call otherwise the
68            SubtitleDecoder will assume there is nothing else at the
69            time of emit the first.
70         */
71
72         list<dcp::SubtitleString> s;
73         ContentTimePeriod const p = content_time_period (*_next);
74
75         while (_next != _subtitles.end () && content_time_period (*_next) == p) {
76                 s.push_back (*_next);
77                 ++_next;
78         }
79
80         subtitle->give_text (p, s);
81
82         return false;
83 }
84
85 list<ContentTimePeriod>
86 DCPSubtitleDecoder::image_subtitles_during (ContentTimePeriod, bool) const
87 {
88         return list<ContentTimePeriod> ();
89 }
90
91 list<ContentTimePeriod>
92 DCPSubtitleDecoder::text_subtitles_during (ContentTimePeriod p, bool starting) const
93 {
94         /* XXX: inefficient */
95
96         list<ContentTimePeriod> d;
97
98         for (list<dcp::SubtitleString>::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
99                 ContentTimePeriod period = content_time_period (*i);
100                 if ((starting && p.contains (period.from)) || (!starting && p.overlaps (period))) {
101                         d.push_back (period);
102                 }
103         }
104
105         return d;
106 }
107
108 ContentTimePeriod
109 DCPSubtitleDecoder::content_time_period (dcp::SubtitleString s) const
110 {
111         return ContentTimePeriod (
112                 ContentTime::from_seconds (s.in().as_seconds ()),
113                 ContentTime::from_seconds (s.out().as_seconds ())
114                 );
115 }