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