2 Copyright (C) 2014 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 /** @file src/stl_binary_writer.cc
21 * @brief Writer for STL binary files.
24 #include "stl_binary_writer.h"
28 #include "compose.hpp"
29 #include "sub_assert.h"
30 #include <boost/locale.hpp>
45 using boost::locale::conv::utf_to_utf;
46 using boost::optional;
49 /** Arbitrary number which to divide the screen into rows; e.g.
50 * 64 here would mean that there are 64 addressable vertical positions
51 * on the screen, each 1/64th of the screen height tall.
53 * The magic 23 makes our output agree more closely with
54 * AnnotationEdit, which makes life easier when testing.
56 static int const ROWS = 23;
59 put_string (char* p, string s)
61 memcpy (p, s.c_str (), s.length ());
65 put_string (char* p, unsigned int n, string s)
67 SUB_ASSERT (s.length() <= n);
69 memcpy (p, s.c_str (), s.length ());
70 memset (p + s.length(), ' ', n - s.length ());
74 put_int_as_string (char* p, int v, unsigned int n)
76 locked_stringstream s;
77 /* Be careful to ensure we get no thousands separators */
78 s.imbue (std::locale::classic ());
79 s << setw (n) << setfill ('0');
81 SUB_ASSERT (s.str().length() == n);
82 put_string (p, s.str ());
86 put_int_as_int (char* p, int v, unsigned int n)
88 for (unsigned int i = 0; i < n; ++i) {
89 *p++ = (v & ((1 << ((i + 1) * 8)) - 1)) >> (i * 8);
94 vertical_position (sub::Line const & line)
97 if (line.vertical_position.proportional) {
98 switch (line.vertical_position.reference.get_value_or (TOP_OF_SCREEN)) {
100 vp = rint (line.vertical_position.proportional.get() * ROWS);
102 case VERTICAL_CENTRE_OF_SCREEN:
103 vp = rint (line.vertical_position.proportional.get() * ROWS + (ROWS / 2.0));
105 case BOTTOM_OF_SCREEN:
106 vp = rint (ROWS - (line.vertical_position.proportional.get() * ROWS));
111 } else if (line.vertical_position.line) {
112 float const prop = float (line.vertical_position.line.get()) / line.vertical_position.lines.get ();
113 switch (line.vertical_position.reference.get_value_or (TOP_OF_SCREEN)) {
117 case VERTICAL_CENTRE_OF_SCREEN:
118 vp = (prop + 0.5) * ROWS;
120 case BOTTOM_OF_SCREEN:
121 vp = (1 - prop) * ROWS;
131 /** @param language ISO 3-character country code for the language of the subtitles */
133 sub::write_stl_binary (
134 list<Subtitle> subtitles,
135 float frames_per_second,
137 string original_programme_title,
138 string original_episode_title,
139 string translated_programme_title,
140 string translated_episode_title,
141 string translator_name,
142 string translator_contact_details,
143 string creation_date,
144 string revision_date,
146 string country_of_origin,
149 string editor_contact_details,
150 boost::filesystem::path file_name
153 SUB_ASSERT (original_programme_title.size() <= 32);
154 SUB_ASSERT (original_episode_title.size() <= 32);
155 SUB_ASSERT (translated_programme_title.size() <= 32);
156 SUB_ASSERT (translated_episode_title.size() <= 32);
157 SUB_ASSERT (translator_name.size() <= 32);
158 SUB_ASSERT (translator_contact_details.size() <= 32);
159 SUB_ASSERT (creation_date.size() == 6);
160 SUB_ASSERT (revision_date.size() == 6);
161 SUB_ASSERT (revision_number <= 99);
162 SUB_ASSERT (country_of_origin.size() == 3);
163 SUB_ASSERT (publisher.size() <= 32);
164 SUB_ASSERT (editor_name.size() <= 32);
165 SUB_ASSERT (editor_contact_details.size() <= 32);
167 char* buffer = new char[1024];
168 memset (buffer, 0, 1024);
169 ofstream output (file_name.string().c_str ());
170 STLBinaryTables tables;
172 /* Find the longest subtitle in characters */
176 for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
177 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
179 for (list<Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
180 t += k->text.size ();
182 longest = std::max (longest, t);
187 put_string (buffer + 0, "850");
188 /* Disk format code */
189 put_string (buffer + 3, stl_frame_rate_to_dfc (frames_per_second));
190 /* Display standard code: open subtitling */
191 put_string (buffer + 11, "0");
192 /* Character code table: Latin (ISO 6937) */
193 put_string (buffer + 12, "00");
194 put_string (buffer + 14, tables.language_enum_to_file (language));
195 put_string (buffer + 16, 32, original_programme_title);
196 put_string (buffer + 48, 32, original_episode_title);
197 put_string (buffer + 80, 32, translated_programme_title);
198 put_string (buffer + 112, 32, translated_episode_title);
199 put_string (buffer + 144, 32, translator_name);
200 put_string (buffer + 176, 32, translator_contact_details);
201 /* Subtitle list reference code */
202 put_string (buffer + 208, "0000000000000000");
203 put_string (buffer + 224, creation_date);
204 put_string (buffer + 230, revision_date);
205 put_int_as_string (buffer + 236, revision_number, 2);
207 put_int_as_string (buffer + 238, subtitles.size(), 5);
208 /* Total number of subtitles */
209 put_int_as_string (buffer + 243, subtitles.size(), 5);
210 /* Total number of subtitle groups */
211 put_string (buffer + 248, "001");
212 /* Maximum number of displayable characters in any text row */
213 put_int_as_string (buffer + 251, longest, 2);
214 /* Maximum number of displayable rows */
215 put_int_as_string (buffer + 253, ROWS, 2);
216 /* Time code status */
217 put_string (buffer + 255, "1");
218 /* Start-of-programme time code */
219 put_string (buffer + 256, "00000000");
220 /* First-in-cue time code */
221 put_string (buffer + 264, "00000000");
222 /* Total number of disks */
223 put_string (buffer + 272, "1");
224 /* Disk sequence number */
225 put_string (buffer + 273, "1");
226 put_string (buffer + 274, 3, country_of_origin);
227 put_string (buffer + 277, 32, publisher);
228 put_string (buffer + 309, 32, editor_name);
229 put_string (buffer + 341, 32, editor_contact_details);
231 output.write (buffer, 1024);
233 for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
235 /* Find the top vertical position of this subtitle */
237 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
238 int const vp = vertical_position (*j);
239 if (!top || vp < top.get ()) {
244 memset (buffer, 0, 1024);
246 /* Subtitle group number */
247 put_int_as_int (buffer + 0, 1, 1);
248 /* Subtitle number */
249 put_int_as_int (buffer + 1, 0, 2);
250 /* Extension block number. Use 0xff here to indicate that it is the last TTI
251 block in this subtitle "set", as we only ever use one.
253 put_int_as_int (buffer + 3, 255, 1);
254 /* Cumulative status */
255 put_int_as_int (buffer + 4, tables.cumulative_status_enum_to_file (CUMULATIVE_STATUS_NOT_CUMULATIVE), 1);
257 put_int_as_int (buffer + 5, i->from.hours(), 1);
258 put_int_as_int (buffer + 6, i->from.minutes(), 1);
259 put_int_as_int (buffer + 7, i->from.seconds(), 1);
260 put_int_as_int (buffer + 8, i->from.frames_at(sub::Rational (frames_per_second * 1000, 1000)), 1);
262 put_int_as_int (buffer + 9, i->to.hours(), 1);
263 put_int_as_int (buffer + 10, i->to.minutes(), 1);
264 put_int_as_int (buffer + 11, i->to.seconds(), 1);
265 put_int_as_int (buffer + 12, i->to.frames_at(sub::Rational (frames_per_second * 1000, 1000)), 1);
266 /* Vertical position */
267 put_int_as_int (buffer + 13, top.get(), 1);
269 /* Justification code */
270 /* XXX: this assumes the first line has the right value */
271 switch (i->lines.front().horizontal_position.reference) {
273 put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_LEFT), 1);
275 case HORIZONTAL_CENTRE_OF_SCREEN:
276 put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_CENTRE), 1);
278 case RIGHT_OF_SCREEN:
279 put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_RIGHT), 1);
284 put_int_as_int (buffer + 15, tables.comment_enum_to_file (COMMENT_NO), 1);
289 bool underline = false;
290 optional<int> last_vp;
292 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
294 /* CR/LF down to this line */
295 int const vp = vertical_position (*j);
298 for (int i = last_vp.get(); i < vp; ++i) {
305 for (list<Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
306 if (k->underline && !underline) {
309 } else if (underline && !k->underline) {
313 if (k->italic && !italic) {
316 } else if (italic && !k->italic) {
321 text += utf16_to_iso6937 (utf_to_utf<wchar_t> (k->text));
325 /* Turn italic/underline off before the end of this subtitle */
335 if (text.length() > 111) {
336 text = text.substr (111);
339 while (text.length() < 112) {
343 put_string (buffer + 16, text);
344 output.write (buffer, 128);