1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/*
Copyright (C) 2023 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "compose.hpp"
#include "exceptions.h"
#include "raw_convert.h"
#include "scc_reader.h"
#include "sub_assert.h"
#include "util.h"
#include <boost/bind/bind.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <set>
#include <unordered_map>
using std::pair;
using std::set;
using std::string;
using std::unordered_map;
using std::vector;
using boost::function;
using boost::optional;
using namespace sub;
static Rational const frame_rate{30000, 1001};
/** @param s Subtitle string encoded in UTF-8 */
SCCReader::SCCReader(string s)
{
this->read(boost::bind(&get_line_string, &s));
}
/** @param f Subtitle file encoded in UTF-8 */
SCCReader::SCCReader(FILE* f)
{
this->read(boost::bind (&get_line_file, f));
}
int
convert_time(std::string time)
{
vector<string> parts;
boost::algorithm::split(parts, time, boost::is_any_of(":"));
if (parts.size() != 4) {
throw SCCReader(String::compose("Unrecognised time %1", time));
}
int seconds = raw_convert<int>(parts[0]) * 3600 + raw_convert<int>(parts[1]) * 60 + raw_convert<int>(parts[2]);
return std::round(seconds * frame_rate.fraction() + raw_convert<int>(parts[3]));
}
struct PAC
{
int row = 0;
int column = 0;
enum class Colour {
WHITE,
GREEN,
BLUE,
CYAN,
RED,
YELLOW,
MAGENTA
};
Colour colour = Colour::WHITE;
bool underline = false;
PAC() = default;
PAC(int row_, int column_, Colour colour_ = Colour::WHITE, bool underline_ = false)
: row(row_)
, column(column_)
, colour(colour_)
, underline(underline_)
{}
};
optional<PAC>
preamble_address_code(string part)
{
int bytes[2];
if (sscanf(part.c_str(), "%2x%2x", bytes, bytes + 1) < 2) {
throw SCCReader(String::compose("Failed to parse line part %1", part));
}
auto adjust = [](PAC& pac, int byte) {
switch (byte) {
case 0x91:
break;
case 0x92:
pac.row += 2;
break;
case 0x15:
pac.row += 4;
break;
case 0x16:
pac.row += 6;
break;
case 0x97:
pac.row += 8;
break;
case 0x10:
pac.row += 10;
break;
case 0x13:
pac.row += 11;
break;
case 0x94:
pac.row += 13;
break;
default:
return false;
}
return true;
};
PAC pac;
switch (bytes[1]) {
/* Column 0 - from http://www.theneitherworld.com/mcpoodle/SCC_TOOLS/DOCS/SCC_FORMAT.HTML */
case 0xd0:
case 0x70:
case 0x51:
case 0xf1:
case 0xc2:
case 0x62:
case 0x43:
case 0xe3:
case 0xc4:
case 0x64:
case 0x45:
case 0xe5:
case 0x46:
case 0xe6:
case 0xc7:
case 0x67:
case 0xc8:
case 0x68:
case 0x49:
case 0xe9:
case 0x4a:
case 0xea:
case 0xcb:
case 0x6b:
case 0x4c:
case 0xec:
case 0xcd:
case 0x6d:
/* RESUMEDIRECTCAPTIONING in ccextractor? */
// case 0x29:
/* No documentation found for these... */
// case 0x2f:
// case 0x40:
// case 0xe0:
pac.row = ((bytes[1] & 0x20) >> 5) + 1;
pac.underline = bytes[1] & 0x1;
switch (bytes[1] & 0xf) {
case 0x0:
case 0x1:
pac.colour = PAC::Colour::WHITE;
break;
case 0x2:
case 0x3:
pac.colour = PAC::Colour::GREEN;
break;
case 0x4:
case 0x5:
pac.colour = PAC::Colour::BLUE;
break;
case 0x6:
case 0x7:
pac.colour = PAC::Colour::CYAN;
break;
case 0x8:
case 0x9:
pac.colour = PAC::Colour::RED;
break;
case 0xa:
case 0xb:
pac.colour = PAC::Colour::YELLOW;
break;
case 0xc:
case 0xd:
pac.colour = PAC::Colour::MAGENTA;
break;
}
if (adjust(pac, bytes[0])) {
return pac;
}
}
switch (bytes[1]) {
/* Columns 4 - 28 */
case 0x52:
case 0xf2:
case 0xd3:
case 0x73:
case 0x54:
case 0xf4:
case 0xd5:
case 0x75:
case 0xd6:
case 0x76:
case 0x57:
case 0xf7:
case 0x58:
case 0xf8:
case 0xd9:
case 0x79:
case 0xda:
case 0x7a:
case 0x5b:
case 0xfb:
case 0xdc:
case 0x7c:
case 0x5d:
case 0xfd:
case 0x5e:
case 0xfe:
case 0xdf:
case 0x7f:
pac.row = ((bytes[1] & 0x20) >> 5) + 1;
pac.underline = bytes[1] & 0x1;
pac.column = (bytes[1] & 0xe) * 2;
if (adjust(pac, bytes[0])) {
return pac;
}
}
return {};
}
void
SCCReader::read(function<optional<string> ()> get_line)
{
set<string> ignore;
ignore.insert("102f");
ignore.insert("9420"); // RCL: resume caption loading; the next caption is "pop-on"
ignore.insert("9440"); // should be a line break?
set<string> line_break;
line_break.insert("94e0");
unordered_map<string, char> replace;
replace["9229"] = '\'';
bool got_header = false;
int to_fix = 0;
while (true) {
auto line = get_line ();
if (!line) {
break;
}
trim_right_if(*line, boost::is_any_of ("\n\r"));
if (*line == "Scenarist_SCC V1.0" && !got_header) {
got_header = true;
continue;
}
if (!got_header) {
throw SCCError("No header string found");
}
if (line->empty()) {
continue;
}
auto const tab = line->find('\t');
if (tab == string::npos) {
throw SCCError(String::compose("No tab character found in line %1", *line));
}
/* XXX: don't fuck about with this stuff, we just add a frame per two-bytes to the initial time,
* and probably do this NDF adjustment.
*/
int time_in_frames = convert_time(line->substr(0, tab));
vector<string> parts;
boost::algorithm::split(parts, line->substr(tab + 1), boost::is_any_of(" "));
vector<RawSubtitle> current_line;
optional<Time> from;
RawSubtitle current;
current.vertical_position.line = 0;
current.vertical_position.reference = TOP_OF_SUBTITLE;
auto maybe_add = [&]() {
if (!current.text.empty()) {
std::cerr << "Add " << current.from << " " << current.to << " " << current.text << "\n";
current_line.push_back(current);
}
current.text = "";
};
for (auto part: parts) {
if (ignore.find(part) != ignore.end()) {
++time_in_frames;
continue;
}
std::cerr << "___" << part << "___\n";
auto replace_iter = replace.find(part);
if (replace_iter != replace.end()) {
current.text = current.text.substr(0, current.text.length() - 1) + replace_iter->second;
++time_in_frames;
continue;
}
if (line_break.find(part) != line_break.end()) {
maybe_add();
current.vertical_position.line = current.vertical_position.line.get_value_or(0) + 1;
} else if (part == "97a1") {
/* TO: tab over
* Move one column over
*/
current.text += " ";
} else if (part == "97a2") {
/* TO: tab over
* Move two columns over
*/
current.text += " ";
} else if (part == "9723") {
/* TO: tab over
* Move three columns over
*/
current.text += " ";
} else if (part == "94ae") {
/* ENM: erase non-displayed memory */
current.text = "";
} else if (part == "942f") {
/* EOC: end of caption */
from = Time::from_frames(time_in_frames, frame_rate);
} else if (part == "942c") {
/* EDM: erase displayed memory */
from = Time::from_frames(time_in_frames, frame_rate);
} else if (part == "91ae" || part == "946e" || part == "94ce") {
maybe_add();
current.italic = true;
} else if (part == "9120") {
maybe_add();
current.italic = false;
} else if (auto pac = preamble_address_code(part)) {
// std::cerr << "-" << part << "-PAC row=" << pac->row << "\n";
maybe_add();
current.vertical_position.line = pac->row;
current.vertical_position.lines = 16;
} else {
int text[2];
if (sscanf(part.c_str(), "%2x%2x", text, text + 1) < 2) {
throw SCCReader(String::compose("Failed to parse line part %1", part));
}
for (int i = 0; i < 2; ++i) {
if (text[i] != 0x80) {
current.text += static_cast<unsigned char>(text[i] & 0x7f);
}
}
std::cerr << ">" << current.text << "\n";
}
time_in_frames++;
}
maybe_add();
for (auto i = _subs.rbegin(); i != _subs.rend(); ++i) {
if (to_fix == 0) {
break;
}
SUB_ASSERT(from);
i->to = *from;
--to_fix;
}
for (auto sub: current_line) {
SUB_ASSERT(from);
sub.from = *from;
_subs.push_back(sub);
}
to_fix = current_line.size();
}
}
|