summaryrefslogtreecommitdiff
path: root/src/stl_binary_writer.cc
blob: 8cc59d1b44ba70ea805f17e45ecc2f55c7cf639e (plain)
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
/*
    Copyright (C) 2014-2020 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.

*/

/** @file  src/stl_binary_writer.cc
 *  @brief Writer for STL binary files.
 */

#include "stl_binary_writer.h"
#include "subtitle.h"
#include "iso6937.h"
#include "stl_util.h"
#include "sub_assert.h"
#include <dcp/compose.h>
#include <boost/locale.hpp>
#include <boost/algorithm/string.hpp>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <set>
#include <vector>

using std::set;
using std::ofstream;
using std::string;
using std::setw;
using std::setfill;
using std::max;
using std::cout;
using std::vector;
using boost::locale::conv::utf_to_utf;
using boost::optional;
using namespace sub;

/** Arbitrary number which to divide the screen into rows; e.g.
 *  64 here would mean that there are 64 addressable vertical positions
 *  on the screen, each 1/64th of the screen height tall.
 *
 *  The magic 23 makes our output agree more closely with
 *  AnnotationEdit, which makes life easier when testing.
 */
static int const ROWS = 23;

static void
put_string (char* p, string s)
{
	memcpy (p, s.c_str (), s.length ());
}

static void
put_string (char* p, unsigned int n, string s)
{
	SUB_ASSERT (s.length() <= n);

	memcpy (p, s.c_str (), s.length ());
	memset (p + s.length(), ' ', n - s.length ());
}

/** @param v Value
 *  @param n Width to zero-pad v to.
 */
static void
put_int_as_string (char* p, int v, unsigned int n)
{
	char buffer[64];

	switch (n) {
	case 2:
		snprintf (buffer, sizeof(buffer), "%02d", v);
		break;
	case 5:
		snprintf (buffer, sizeof(buffer), "%05d", v);
		break;
	default:
		SUB_ASSERT (false);
	}

	string s = buffer;

	struct lconv* lc = localeconv ();
	boost::algorithm::replace_all (s, lc->thousands_sep, "");
	boost::algorithm::replace_all (s, lc->decimal_point, ".");

	put_string (p, s);
}

static void
put_int_as_int (char* p, int v, unsigned int n)
{
	for (unsigned int i = 0; i < n; ++i) {
		*p++ = (v & ((1 << ((i + 1) * 8)) - 1)) >> (i * 8);
	}
}

static int
vertical_position (sub::Line const & line)
{
	int vp = 0;
	if (line.vertical_position.proportional) {
		switch (line.vertical_position.reference.get_value_or (TOP_OF_SCREEN)) {
		case TOP_OF_SCREEN:
			vp = rint (line.vertical_position.proportional.get() * ROWS);
			break;
		case VERTICAL_CENTRE_OF_SCREEN:
			vp = rint (line.vertical_position.proportional.get() * ROWS + (ROWS / 2.0));
			break;
		case BOTTOM_OF_SCREEN:
			vp = rint (ROWS - (line.vertical_position.proportional.get() * ROWS));
			break;
		default:
			break;
		}
	} else if (line.vertical_position.line) {
		float const prop = float (line.vertical_position.line.get()) / line.vertical_position.lines.get ();
		switch (line.vertical_position.reference.get_value_or (TOP_OF_SCREEN)) {
		case TOP_OF_SCREEN:
			vp = prop * ROWS;
			break;
		case VERTICAL_CENTRE_OF_SCREEN:
			vp = (prop + 0.5) * ROWS;
			break;
		case BOTTOM_OF_SCREEN:
			vp = (1 - prop) * ROWS;
			break;
		default:
			break;
		}
	}

	return vp;
}

vector<char*>
make_tti_blocks (vector<Subtitle> const& subtitles, STLBinaryTables const& tables, float frames_per_second)
{
	static int const tti_size = 128;
	vector<char*> tti;

	/* Buffer to build the TTI blocks in */
	char buffer[tti_size];

	for (auto const& i: subtitles) {

		/* Find the top vertical position of this subtitle */
		optional<int> top;
		for (auto const& j: i.lines) {
			int const vp = vertical_position (j);
			if (!top || vp < top.get ()) {
				top = vp;
			}
		}

		/* Work out the text */
		string text;
		bool italic = false;
		bool underline = false;
		optional<int> last_vp;

		for (auto const& j: i.lines) {

			/* CR/LF down to this line */
			int const vp = vertical_position (j);

			if (last_vp) {
				for (int k = last_vp.get(); k < vp; ++k) {
					text += "\x8A";
				}
			}

			last_vp = vp;

			for (auto const& k: j.blocks) {
				if (k.underline && !underline) {
					text += "\x82";
					underline = true;
				} else if (underline && !k.underline) {
					text += "\x83";
					underline = false;
				}
				if (k.italic && !italic) {
					text += "\x80";
					italic = true;
				} else if (italic && !k.italic) {
					text += "\x81";
					italic = false;
				}

				text += utf16_to_iso6937 (utf_to_utf<wchar_t> (k.text));
			}
		}

		/* Turn italic/underline off before the end of this subtitle */
		if (underline) {
			text += "\x83";
		}
		if (italic) {
			text += "\x81";
		}

		/* Make sure there's at least one end-of-line */
		text += "\x8F";

		/* Now write this text in 112 byte chunks (TTI blocks).  Only the first TTI
		   block's cumulative status, timecodes, vertical position, justification code
		   and comment flag are taken into account by the reader.
		   */

		/* Set up the first part of the block */

		/* XXX: these should increment, surely! */
		/* Subtitle group number */
		put_int_as_int (buffer + 0, 1, 1);
		/* Subtitle number */
		put_int_as_int (buffer + 1, 0, 2);
		/* Cumulative status */
		put_int_as_int (buffer + 4, tables.cumulative_status_enum_to_file (CUMULATIVE_STATUS_NOT_CUMULATIVE), 1);
		/* Time code in */
		put_int_as_int (buffer + 5, i.from.hours(), 1);
		put_int_as_int (buffer + 6, i.from.minutes(), 1);
		put_int_as_int (buffer + 7, i.from.seconds(), 1);
		put_int_as_int (buffer + 8, i.from.frames_at(sub::Rational(frames_per_second * 1000, 1000)), 1);
		/* Time code out */
		put_int_as_int (buffer + 9, i.to.hours(), 1);
		put_int_as_int (buffer + 10, i.to.minutes(), 1);
		put_int_as_int (buffer + 11, i.to.seconds(), 1);
		put_int_as_int (buffer + 12, i.to.frames_at(sub::Rational(frames_per_second * 1000, 1000)), 1);
		/* Vertical position */
		put_int_as_int (buffer + 13, top.get(), 1);

		/* Justification code */
		/* XXX: this assumes the first line has the right value */
		switch (i.lines.front().horizontal_position.reference) {
			case LEFT_OF_SCREEN:
				put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_LEFT), 1);
				break;
			case HORIZONTAL_CENTRE_OF_SCREEN:
				put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_CENTRE), 1);
				break;
			case RIGHT_OF_SCREEN:
				put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_RIGHT), 1);
				break;
		}

		/* Comment flag */
		put_int_as_int (buffer + 15, tables.comment_enum_to_file (COMMENT_NO), 1);

		/* Now make as many blocks as are needed to add all the text */
		size_t const block_size = 112;
		size_t offset = 0;
		int block_number = 0;
		while (offset < text.length()) {
			size_t this_time = std::min(block_size, text.length() - offset);
			put_string (buffer + 16, text.substr(offset, this_time) + string(block_size - this_time, '\x8f'));
			offset += this_time;

			/* Extension block number.  Count up from 0 but use 0xff for the last one */
			put_int_as_int (buffer + 3, offset == text.length() ? 0xff : block_number, 1);
			++block_number;

			char* finished = new char[tti_size];
			memcpy (finished, buffer, tti_size);
			tti.push_back (finished);
		}
	}

	return tti;
}



/** @param language ISO 3-character country code for the language of the subtitles */
void
sub::write_stl_binary (
		vector<Subtitle> subtitles,
		float frames_per_second,
		Language language,
		string original_programme_title,
		string original_episode_title,
		string translated_programme_title,
		string translated_episode_title,
		string translator_name,
		string translator_contact_details,
		string creation_date,
		string revision_date,
		int revision_number,
		string country_of_origin,
		string publisher,
		string editor_name,
		string editor_contact_details,
		boost::filesystem::path file_name
		)
{
	SUB_ASSERT (original_programme_title.size() <= 32);
	SUB_ASSERT (original_episode_title.size() <= 32);
	SUB_ASSERT (translated_programme_title.size() <= 32);
	SUB_ASSERT (translated_episode_title.size() <= 32);
	SUB_ASSERT (translator_name.size() <= 32);
	SUB_ASSERT (translator_contact_details.size() <= 32);
	SUB_ASSERT (creation_date.size() == 6);
	SUB_ASSERT (revision_date.size() == 6);
	SUB_ASSERT (revision_number <= 99);
	SUB_ASSERT (country_of_origin.size() == 3);
	SUB_ASSERT (publisher.size() <= 32);
	SUB_ASSERT (editor_name.size() <= 32);
	SUB_ASSERT (editor_contact_details.size() <= 32);

	char buffer[1024];
	memset (buffer, 0, 1024);
	STLBinaryTables tables;

	/* Find the longest subtitle in characters */

	int longest = 0;

	for (auto const& i: subtitles) {
		for (auto const& j: i.lines) {
			int t = 0;
			for (auto const& k: j.blocks) {
				t += k.text.size ();
			}
			longest = std::max (longest, t);
		}
	}

	vector<char*> tti_blocks = make_tti_blocks (subtitles, tables, frames_per_second);

	/* Code page: 850 */
	put_string (buffer + 0, "850");
	/* Disk format code */
	put_string (buffer + 3, stl_frame_rate_to_dfc (frames_per_second));
	/* Display standard code: open subtitling */
	put_string (buffer + 11, "0");
	/* Character code table: Latin (ISO 6937) */
	put_string (buffer + 12, "00");
	put_string (buffer + 14, tables.language_enum_to_file (language));
	put_string (buffer + 16, 32, original_programme_title);
	put_string (buffer + 48, 32, original_episode_title);
	put_string (buffer + 80, 32, translated_programme_title);
	put_string (buffer + 112, 32, translated_episode_title);
	put_string (buffer + 144, 32, translator_name);
	put_string (buffer + 176, 32, translator_contact_details);
	/* Subtitle list reference code */
	put_string (buffer + 208, "0000000000000000");
	put_string (buffer + 224, creation_date);
	put_string (buffer + 230, revision_date);
	put_int_as_string (buffer + 236, revision_number, 2);
	/* TTI blocks */
	put_int_as_string (buffer + 238, tti_blocks.size(), 5);
	/* Total number of subtitles */
	put_int_as_string (buffer + 243, subtitles.size(), 5);
	/* Total number of subtitle groups */
	put_string (buffer + 248, "001");
	/* Maximum number of displayable characters in any text row */
	put_int_as_string (buffer + 251, longest, 2);
	/* Maximum number of displayable rows */
	put_int_as_string (buffer + 253, ROWS, 2);
	/* Time code status */
	put_string (buffer + 255, "1");
	/* Start-of-programme time code */
	put_string (buffer + 256, "00000000");
	/* First-in-cue time code */
	put_string (buffer + 264, "00000000");
	/* Total number of disks */
	put_string (buffer + 272, "1");
	/* Disk sequence number */
	put_string (buffer + 273, "1");
	put_string (buffer + 274, 3, country_of_origin);
	put_string (buffer + 277, 32, publisher);
	put_string (buffer + 309, 32, editor_name);
	put_string (buffer + 341, 32, editor_contact_details);

	ofstream output (file_name.string().c_str());
	output.write (buffer, 1024);
	for (auto i: tti_blocks) {
		output.write (i, 128);
		delete[] i;
	}
}