2 Copyright (C) 2014-2020 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "stl_binary_reader.h"
21 #include "exceptions.h"
24 #include "compose.hpp"
25 #include <boost/lexical_cast.hpp>
26 #include <boost/algorithm/string.hpp>
27 #include <boost/locale.hpp>
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;
41 class InputReader : public boost::noncopyable
44 InputReader (istream& in)
46 , _buffer (new unsigned char[1024])
57 void read (int size, string what)
59 _in.read (reinterpret_cast<char *>(_buffer), size);
60 if (_in.gcount() != size) {
61 throw STLError (String::compose("Could not read %1 block from binary STL file", what));
65 string get_string (int offset, int length) const
68 for (int i = 0; i < length; ++i) {
69 s += _buffer[offset + i];
75 int get_int (int offset, int length) const
78 for (int i = 0; i < length; ++i) {
79 v |= _buffer[offset + i] << (8 * i);
85 Time get_timecode (int offset, int frame_rate) const
87 return Time::from_hmsf (_buffer[offset], _buffer[offset + 1], _buffer[offset + 2], _buffer[offset + 3], Rational (frame_rate, 1));
92 unsigned char* _buffer;
95 STLBinaryReader::STLBinaryReader (istream& in)
97 InputReader reader (in);
98 reader.read (1024, "GSI");
100 code_page_number = atoi (reader.get_string(0, 3).c_str());
101 frame_rate = stl_dfc_to_frame_rate (reader.get_string(3, 8));
102 display_standard = _tables.display_standard_file_to_enum (reader.get_string(11, 1));
103 language_group = _tables.language_group_file_to_enum (reader.get_string(12, 2));
104 language = _tables.language_file_to_enum (reader.get_string(14, 2));
105 original_programme_title = reader.get_string(16, 32);
106 original_episode_title = reader.get_string(48, 32);
107 translated_programme_title = reader.get_string(80, 32);
108 translated_episode_title = reader.get_string(112, 32);
109 translator_name = reader.get_string(144, 32);
110 translator_contact_details = reader.get_string(176, 32);
111 subtitle_list_reference_code = reader.get_string(208, 16);
112 creation_date = reader.get_string(224, 6);
113 revision_date = reader.get_string(230, 6);
114 revision_number = reader.get_string(236, 2);
116 tti_blocks = atoi (reader.get_string(238, 5).c_str());
117 number_of_subtitles = atoi (reader.get_string(243, 5).c_str());
118 subtitle_groups = atoi (reader.get_string(248, 3).c_str());
119 maximum_characters = atoi (reader.get_string(251, 2).c_str());
120 maximum_rows = atoi (reader.get_string(253, 2).c_str());
121 timecode_status = _tables.timecode_status_file_to_enum (reader.get_string(255, 1));
122 start_of_programme = reader.get_string(256, 8);
123 first_in_cue = reader.get_string(264, 8);
124 disks = atoi (reader.get_string(272, 1).c_str());
125 disk_sequence_number = atoi (reader.get_string(273, 1).c_str());
126 country_of_origin = reader.get_string(274, 3);
127 publisher = reader.get_string(277, 32);
128 editor_name = reader.get_string(309, 32);
129 editor_contact_details = reader.get_string(341, 32);
131 for (int i = 0; i < tti_blocks; ++i) {
133 reader.read (128, "TTI");
135 if (_tables.comment_file_to_enum (reader.get_int(15, 1)) == COMMENT_YES) {
139 string const whole = reader.get_string(16, 112);
141 /* Split the text up into lines (8Ah is a new line) */
142 vector<string> lines;
143 split (lines, whole, is_any_of ("\x8a"));
145 /* Italic / underline specifications can span lines, so we need to track them
146 outside the lines loop.
149 bool underline = false;
151 for (size_t i = 0; i < lines.size(); ++i) {
153 sub.from = reader.get_timecode(5, frame_rate);
154 sub.to = reader.get_timecode(9, frame_rate);
155 sub.vertical_position.line = reader.get_int(13, 1) + i;
156 sub.vertical_position.lines = maximum_rows;
157 sub.vertical_position.reference = TOP_OF_SCREEN;
159 sub.underline = underline;
161 /* XXX: not sure what to do with JC = 0, "unchanged presentation" */
162 int const h = reader.get_int(14, 1);
166 sub.horizontal_position.reference = HORIZONTAL_CENTRE_OF_SCREEN;
169 sub.horizontal_position.reference = LEFT_OF_SCREEN;
172 sub.horizontal_position.reference = RIGHT_OF_SCREEN;
176 /* Loop over characters */
178 for (size_t j = 0; j < lines[i].size(); ++j) {
180 unsigned char const c = static_cast<unsigned char> (lines[i][j]);
183 /* Unused space i.e. end of line */
187 if (c >= 0x80 && c <= 0x83) {
188 /* Italic or underline control code */
189 sub.text = utf_to_utf<char> (iso6937_to_utf16 (text.c_str()));
190 _subs.push_back (sub);
213 sub.underline = underline;
216 if (!text.empty ()) {
217 sub.text = utf_to_utf<char> (iso6937_to_utf16 (text.c_str()));
218 _subs.push_back (sub);
221 /* XXX: justification */
227 STLBinaryReader::metadata () const
229 map<string, string> m;
231 m["Code page number"] = lexical_cast<string> (code_page_number);
232 m["Frame rate"] = lexical_cast<string> (frame_rate);
233 m["Display standard"] = _tables.display_standard_enum_to_description (display_standard);
234 m["Language group"] = _tables.language_group_enum_to_description (language_group);
235 m["Language"] = _tables.language_enum_to_description (language);
236 m["Original programme title"] = original_programme_title;
237 m["Original episode title"] = original_episode_title;
238 m["Translated programme title"] = translated_programme_title;
239 m["Translated episode title"] = translated_episode_title;
240 m["Translator name"] = translator_name;
241 m["Translator contact details"] = translator_contact_details;
242 m["Subtitle list reference code"] = subtitle_list_reference_code;
243 m["Creation date"] = creation_date;
244 m["Revision date"] = revision_date;
245 m["Revision number"] = revision_number;
246 m["TTI blocks"] = lexical_cast<string> (tti_blocks);
247 m["Number of subtitles"] = lexical_cast<string> (number_of_subtitles);
248 m["Subtitle groups"] = lexical_cast<string> (subtitle_groups);
249 m["Maximum characters"] = lexical_cast<string> (maximum_characters);
250 m["Maximum rows"] = lexical_cast<string> (maximum_rows);
251 m["Timecode status"] = _tables.timecode_status_enum_to_description (timecode_status);
252 m["Start of programme"] = start_of_programme;
253 m["First in cue"] = first_in_cue;
254 m["Disks"] = lexical_cast<string> (disks);
255 m["Disk sequence number"] = lexical_cast<string> (disk_sequence_number);
256 m["Country of origin"] = country_of_origin;
257 m["Publisher"] = publisher;
258 m["Editor name"] = editor_name;
259 m["Editor contact details"] = editor_contact_details;