Missed update to private test repo version.
[dcpomatic.git] / src / lib / string_text_file_decoder.cc
1 /*
2     Copyright (C) 2014-2021 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
22 #include "string_text_file_decoder.h"
23 #include "string_text_file_content.h"
24 #include "text_content.h"
25 #include "text_decoder.h"
26 #include <dcp/subtitle_string.h>
27 #include <iostream>
28
29
30 using std::cout;
31 using std::make_shared;
32 using std::shared_ptr;
33 using std::string;
34 using std::vector;
35 using namespace dcpomatic;
36
37
38 StringTextFileDecoder::StringTextFileDecoder (shared_ptr<const Film> film, shared_ptr<const StringTextFileContent> content)
39         : Decoder (film)
40         , StringTextFile (content)
41         , _next (0)
42 {
43         text.push_back (make_shared<TextDecoder>(this, content->only_text()));
44         update_position();
45 }
46
47
48 void
49 StringTextFileDecoder::seek (ContentTime time, bool accurate)
50 {
51         /* It's worth back-tracking a little here as decoding is cheap and it's nice if we don't miss
52            too many subtitles when seeking.
53         */
54         time -= ContentTime::from_seconds (5);
55         if (time < ContentTime()) {
56                 time = ContentTime();
57         }
58
59         Decoder::seek (time, accurate);
60
61         _next = 0;
62         while (_next < _subtitles.size() && ContentTime::from_seconds (_subtitles[_next].from.all_as_seconds ()) < time) {
63                 ++_next;
64         }
65
66         update_position();
67 }
68
69
70 bool
71 StringTextFileDecoder::pass ()
72 {
73         if (_next >= _subtitles.size ()) {
74                 return true;
75         }
76
77         ContentTimePeriod const p = content_time_period (_subtitles[_next]);
78         only_text()->emit_plain (p, _subtitles[_next]);
79
80         ++_next;
81
82         update_position();
83
84         return false;
85 }
86
87
88 ContentTimePeriod
89 StringTextFileDecoder::content_time_period (sub::Subtitle s) const
90 {
91         return ContentTimePeriod (
92                 ContentTime::from_seconds (s.from.all_as_seconds()),
93                 ContentTime::from_seconds (s.to.all_as_seconds())
94                 );
95 }
96
97
98 void
99 StringTextFileDecoder::update_position ()
100 {
101         if (_next < _subtitles.size()) {
102                 only_text()->maybe_set_position(
103                         ContentTime::from_seconds(_subtitles[_next].from.all_as_seconds())
104                         );
105         }
106 }
107