Split InputReader into InputReader and StreamInputReader.
[libsub.git] / src / stl_binary_reader.cc
1 /*
2     Copyright (C) 2014-2020 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 "stl_binary_reader.h"
21 #include "exceptions.h"
22 #include "iso6937.h"
23 #include "stl_util.h"
24 #include "compose.hpp"
25 #include <boost/lexical_cast.hpp>
26 #include <boost/algorithm/string.hpp>
27 #include <boost/locale.hpp>
28 #include <iostream>
29
30 using std::map;
31 using std::vector;
32 using std::cout;
33 using std::string;
34 using std::istream;
35 using boost::lexical_cast;
36 using boost::algorithm::replace_all;
37 using boost::is_any_of;
38 using boost::locale::conv::utf_to_utf;
39 using namespace sub;
40
41 class InputReader : public boost::noncopyable
42 {
43 public:
44         InputReader ()
45                 : _buffer (new unsigned char[1024])
46         {
47
48         }
49
50         virtual ~InputReader ()
51         {
52                 delete[] _buffer;
53         }
54
55         virtual void read (int size, string what) = 0;
56
57         string get_string (int offset, int length) const
58         {
59                 string s;
60                 for (int i = 0; i < length; ++i) {
61                         s += _buffer[offset + i];
62                 }
63
64                 return s;
65         }
66
67         int get_int (int offset, int length) const
68         {
69                 int v = 0;
70                 for (int i = 0; i < length; ++i) {
71                         v |= _buffer[offset + i] << (8 * i);
72                 }
73
74                 return v;
75         }
76
77         Time get_timecode (int offset, int frame_rate) const
78         {
79                 return Time::from_hmsf (_buffer[offset], _buffer[offset + 1], _buffer[offset + 2], _buffer[offset + 3], Rational (frame_rate, 1));
80         }
81
82 protected:
83         unsigned char* _buffer;
84 };
85
86
87 class StreamInputReader : public InputReader
88 {
89 public:
90         StreamInputReader (istream& in)
91                 : _in (in)
92         {
93
94         }
95
96         void read (int size, string what)
97         {
98                 _in.read (reinterpret_cast<char *>(_buffer), size);
99                 if (_in.gcount() != size) {
100                         throw STLError (String::compose("Could not read %1 block from binary STL file", what));
101                 }
102         }
103
104 private:
105         std::istream& _in;
106 };
107
108 STLBinaryReader::STLBinaryReader (istream& in)
109 {
110         StreamInputReader reader (in);
111         reader.read (1024, "GSI");
112
113         code_page_number = atoi (reader.get_string(0, 3).c_str());
114         frame_rate = stl_dfc_to_frame_rate (reader.get_string(3, 8));
115         display_standard = _tables.display_standard_file_to_enum (reader.get_string(11, 1));
116         language_group = _tables.language_group_file_to_enum (reader.get_string(12, 2));
117         language = _tables.language_file_to_enum (reader.get_string(14, 2));
118         original_programme_title = reader.get_string(16, 32);
119         original_episode_title = reader.get_string(48, 32);
120         translated_programme_title = reader.get_string(80, 32);
121         translated_episode_title = reader.get_string(112, 32);
122         translator_name = reader.get_string(144, 32);
123         translator_contact_details = reader.get_string(176, 32);
124         subtitle_list_reference_code = reader.get_string(208, 16);
125         creation_date = reader.get_string(224, 6);
126         revision_date = reader.get_string(230, 6);
127         revision_number = reader.get_string(236, 2);
128
129         tti_blocks = atoi (reader.get_string(238, 5).c_str());
130         number_of_subtitles = atoi (reader.get_string(243, 5).c_str());
131         subtitle_groups = atoi (reader.get_string(248, 3).c_str());
132         maximum_characters = atoi (reader.get_string(251, 2).c_str());
133         maximum_rows = atoi (reader.get_string(253, 2).c_str());
134         timecode_status = _tables.timecode_status_file_to_enum (reader.get_string(255, 1));
135         start_of_programme = reader.get_string(256, 8);
136         first_in_cue = reader.get_string(264, 8);
137         disks = atoi (reader.get_string(272, 1).c_str());
138         disk_sequence_number = atoi (reader.get_string(273, 1).c_str());
139         country_of_origin = reader.get_string(274, 3);
140         publisher = reader.get_string(277, 32);
141         editor_name = reader.get_string(309, 32);
142         editor_contact_details = reader.get_string(341, 32);
143
144         for (int i = 0; i < tti_blocks; ++i) {
145
146                 reader.read (128, "TTI");
147
148                 if (_tables.comment_file_to_enum (reader.get_int(15, 1)) == COMMENT_YES) {
149                         continue;
150                 }
151
152                 string const whole = reader.get_string(16, 112);
153
154                 /* Split the text up into lines (8Ah is a new line) */
155                 vector<string> lines;
156                 split (lines, whole, is_any_of ("\x8a"));
157
158                 /* Italic / underline specifications can span lines, so we need to track them
159                    outside the lines loop.
160                 */
161                 bool italic = false;
162                 bool underline = false;
163
164                 for (size_t i = 0; i < lines.size(); ++i) {
165                         RawSubtitle sub;
166                         sub.from = reader.get_timecode(5, frame_rate);
167                         sub.to = reader.get_timecode(9, frame_rate);
168                         sub.vertical_position.line = reader.get_int(13, 1) + i;
169                         sub.vertical_position.lines = maximum_rows;
170                         sub.vertical_position.reference = TOP_OF_SCREEN;
171                         sub.italic = italic;
172                         sub.underline = underline;
173
174                         /* XXX: not sure what to do with JC = 0, "unchanged presentation" */
175                         int const h = reader.get_int(14, 1);
176                         switch (h) {
177                         case 0:
178                         case 2:
179                                 sub.horizontal_position.reference = HORIZONTAL_CENTRE_OF_SCREEN;
180                                 break;
181                         case 1:
182                                 sub.horizontal_position.reference = LEFT_OF_SCREEN;
183                                 break;
184                         case 3:
185                                 sub.horizontal_position.reference = RIGHT_OF_SCREEN;
186                                 break;
187                         }
188
189                         /* Loop over characters */
190                         string text;
191                         for (size_t j = 0; j < lines[i].size(); ++j) {
192
193                                 unsigned char const c = static_cast<unsigned char> (lines[i][j]);
194
195                                 if (c == 0x8f) {
196                                         /* Unused space i.e. end of line */
197                                         break;
198                                 }
199
200                                 if (c >= 0x80 && c <= 0x83) {
201                                         /* Italic or underline control code */
202                                         sub.text = utf_to_utf<char> (iso6937_to_utf16 (text.c_str()));
203                                         _subs.push_back (sub);
204                                         text.clear ();
205                                 }
206
207                                 switch (c) {
208                                 case 0x80:
209                                         italic = true;
210                                         break;
211                                 case 0x81:
212                                         italic = false;
213                                         break;
214                                 case 0x82:
215                                         underline = true;
216                                         break;
217                                 case 0x83:
218                                         underline = false;
219                                         break;
220                                 default:
221                                         text += lines[i][j];
222                                         break;
223                                 }
224
225                                 sub.italic = italic;
226                                 sub.underline = underline;
227                         }
228
229                         if (!text.empty ()) {
230                                 sub.text = utf_to_utf<char> (iso6937_to_utf16 (text.c_str()));
231                                 _subs.push_back (sub);
232                         }
233
234                         /* XXX: justification */
235                 }
236         }
237 }
238
239 map<string, string>
240 STLBinaryReader::metadata () const
241 {
242         map<string, string> m;
243
244         m["Code page number"] = lexical_cast<string> (code_page_number);
245         m["Frame rate"] = lexical_cast<string> (frame_rate);
246         m["Display standard"] = _tables.display_standard_enum_to_description (display_standard);
247         m["Language group"] = _tables.language_group_enum_to_description (language_group);
248         m["Language"] = _tables.language_enum_to_description (language);
249         m["Original programme title"] = original_programme_title;
250         m["Original episode title"] = original_episode_title;
251         m["Translated programme title"] = translated_programme_title;
252         m["Translated episode title"] = translated_episode_title;
253         m["Translator name"] = translator_name;
254         m["Translator contact details"] = translator_contact_details;
255         m["Subtitle list reference code"] = subtitle_list_reference_code;
256         m["Creation date"] = creation_date;
257         m["Revision date"] = revision_date;
258         m["Revision number"] = revision_number;
259         m["TTI blocks"] = lexical_cast<string> (tti_blocks);
260         m["Number of subtitles"] = lexical_cast<string> (number_of_subtitles);
261         m["Subtitle groups"] = lexical_cast<string> (subtitle_groups);
262         m["Maximum characters"] = lexical_cast<string> (maximum_characters);
263         m["Maximum rows"] = lexical_cast<string> (maximum_rows);
264         m["Timecode status"] = _tables.timecode_status_enum_to_description (timecode_status);
265         m["Start of programme"] = start_of_programme;
266         m["First in cue"] = first_in_cue;
267         m["Disks"] = lexical_cast<string> (disks);
268         m["Disk sequence number"] = lexical_cast<string> (disk_sequence_number);
269         m["Country of origin"] = country_of_origin;
270         m["Publisher"] = publisher;
271         m["Editor name"] = editor_name;
272         m["Editor contact details"] = editor_contact_details;
273
274         return m;
275 }