ao_pulse: support native mute control
[mplayer.git] / sub / subassconvert.c
blob2a56b46022174684e092aa1aa369465aabb14e6e
1 /*
2 * Subtitles converter to SSA/ASS in order to allow special formatting
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <string.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
28 #include "mp_msg.h"
29 #include "subassconvert.h"
30 #include "bstr.h"
31 #include "libavutil/common.h"
33 struct line {
34 char *buf;
35 int bufsize;
36 int len;
39 #ifdef __GNUC__
40 static void append_text(struct line *dst, char *fmt, ...) __attribute__ ((format(printf, 2, 3)));
41 #endif
43 static void append_text(struct line *dst, char *fmt, ...)
45 va_list va;
46 va_start(va, fmt);
47 int ret = vsnprintf(dst->buf + dst->len, dst->bufsize - dst->len, fmt, va);
48 if (ret < 0)
49 goto out;
50 dst->len += ret;
51 if (dst->len > dst->bufsize)
52 dst->len = dst->bufsize;
53 out:
54 va_end(va);
57 static int indexof(const char *s, int c)
59 char *f = strchr(s, c);
60 return f ? (f - s) : -1;
66 * SubRip
68 * Support basic tags (italic, bold, underline, strike-through)
69 * and font tag with size, color and face attributes.
73 struct font_tag {
74 int size;
75 uint32_t color;
76 struct bstr face;
77 bool has_size : 1;
78 bool has_color : 1;
79 bool has_face : 1;
82 static const struct tag_conv {
83 char *from;
84 char *to;
85 } subrip_basic_tags[] = {
86 {"<i>", "{\\i1}"}, {"</i>", "{\\i0}"},
87 {"<b>", "{\\b1}"}, {"</b>", "{\\b0}"},
88 {"<u>", "{\\u1}"}, {"</u>", "{\\u0}"},
89 {"<s>", "{\\s1}"}, {"</s>", "{\\s0}"},
90 {"{", "\\{"}, {"}", "\\}"},
91 {"\r\n", "\\N"}, {"\n", "\\N"}, {"\r", "\\N"},
94 static const struct {
95 char *s;
96 uint32_t v;
97 } subrip_web_colors[] = {
98 /* 16 named HTML colors in BGR format */
99 {"red", 0x0000ff}, {"blue", 0xff0000}, {"lime", 0x00ff00},
100 {"aqua", 0xffff00}, {"purple", 0x800080}, {"yellow", 0x00ffff},
101 {"fuchsia", 0xff00ff}, {"white", 0xffffff}, {"gray", 0x808080},
102 {"maroon", 0x000080}, {"olive", 0x008080}, {"black", 0x000000},
103 {"silver", 0xc0c0c0}, {"teal", 0x808000}, {"green", 0x008000},
104 {"navy", 0x800000}
107 #define SUBRIP_MAX_STACKED_FONT_TAGS 16
109 void subassconvert_subrip(const char *orig, char *dest, int dest_buffer_size)
111 /* line is not const to avoid warnings with strtol, etc.
112 * orig content won't be changed */
113 char *line = (char *)orig;
114 struct line new_line = {
115 .buf = dest,
116 .bufsize = dest_buffer_size,
118 struct font_tag font_stack[SUBRIP_MAX_STACKED_FONT_TAGS + 1];
119 font_stack[0] = (struct font_tag){0}; // type with all defaults
120 int sp = 0;
122 while (*line && new_line.len < new_line.bufsize - 1) {
123 char *orig_line = line;
125 for (int i = 0; i < FF_ARRAY_ELEMS(subrip_basic_tags); i++) {
126 const struct tag_conv *tag = &subrip_basic_tags[i];
127 int from_len = strlen(tag->from);
128 if (strncmp(line, tag->from, from_len) == 0) {
129 append_text(&new_line, "%s", tag->to);
130 line += from_len;
134 if (strncmp(line, "</font>", 7) == 0) {
135 /* Closing font tag */
136 line += 7;
138 if (sp > 0) {
139 struct font_tag *tag = &font_stack[sp];
140 struct font_tag *last_tag = &tag[-1];
141 sp--;
143 if (tag->has_size) {
144 if (!last_tag->has_size)
145 append_text(&new_line, "{\\fs}");
146 else if (last_tag->size != tag->size)
147 append_text(&new_line, "{\\fs%d}", last_tag->size);
150 if (tag->has_color) {
151 if (!last_tag->has_color)
152 append_text(&new_line, "{\\c}");
153 else if (last_tag->color != tag->color)
154 append_text(&new_line, "{\\c&H%06X&}", last_tag->color);
157 if (tag->has_face) {
158 if (!last_tag->has_face)
159 append_text(&new_line, "{\\fn}");
160 else if (bstrcmp(last_tag->face, tag->face) != 0)
161 append_text(&new_line, "{\\fn%.*s}",
162 BSTR_P(last_tag->face));
165 } else if (strncmp(line, "<font ", 6) == 0
166 && sp + 1 < FF_ARRAY_ELEMS(font_stack)) {
167 /* Opening font tag */
168 char *potential_font_tag_start = line;
169 int len_backup = new_line.len;
170 struct font_tag *tag = &font_stack[sp + 1];
171 bool has_valid_attr = false;
173 *tag = tag[-1]; // keep values from previous tag
174 line += 6;
176 while (*line && *line != '>') {
177 if (strncmp(line, "size=\"", 6) == 0) {
178 line += 6;
179 tag->size = strtol(line, &line, 10);
180 if (*line != '"')
181 break;
182 append_text(&new_line, "{\\fs%d}", tag->size);
183 tag->has_size = true;
184 has_valid_attr = true;
185 } else if (strncmp(line, "color=\"", 7) == 0) {
186 line += 7;
187 if (*line == '#') {
188 // #RRGGBB format
189 line++;
190 tag->color = strtol(line, &line, 16) & 0x00ffffff;
191 if (*line != '"')
192 break;
193 tag->color = ((tag->color & 0xff) << 16)
194 | (tag->color & 0xff00)
195 | ((tag->color & 0xff0000) >> 16);
196 } else {
197 // Standard web colors
198 int len = indexof(line, '"');
199 if (len <= 0)
200 break;
201 for (int i = 0; i < FF_ARRAY_ELEMS(subrip_web_colors); i++) {
202 char *color = subrip_web_colors[i].s;
203 if (strlen(color) == len
204 && strncasecmp(line, color, len) == 0) {
205 tag->color = subrip_web_colors[i].v;
206 goto foundcolor;
210 /* We didn't find any matching color */
211 mp_tmsg(MSGT_SUBREADER, MSGL_WARN,
212 "SubRip: unknown font color in subtitle: %s\n", orig);
213 append_text(&new_line, "{\\c}");
214 line += len + 1;
215 continue;
217 foundcolor:
218 line += len;
220 append_text(&new_line, "{\\c&H%06X&}", tag->color);
221 tag->has_color = true;
222 has_valid_attr = true;
223 } else if (strncmp(line, "face=\"", 6) == 0) {
224 /* Font face attribute */
225 line += 6;
226 int len = indexof(line, '"');
227 if (len <= 0)
228 break;
229 tag->face.start = line;
230 tag->face.len = len;
231 line += len;
232 append_text(&new_line, "{\\fn%.*s}", BSTR_P(tag->face));
233 tag->has_face = true;
234 has_valid_attr = true;
236 line++;
239 if (!has_valid_attr || *line != '>') { /* Not valid font tag */
240 line = potential_font_tag_start;
241 new_line.len = len_backup;
242 } else {
243 sp++;
244 line++;
248 /* Tag conversion code didn't match */
249 if (line == orig_line)
250 new_line.buf[new_line.len++] = *line++;
252 new_line.buf[new_line.len] = 0;
257 * MicroDVD
259 * Based on the specifications found here:
260 * https://trac.videolan.org/vlc/ticket/1825#comment:6
263 struct microdvd_tag {
264 char key;
265 int persistent;
266 uint32_t data1;
267 uint32_t data2;
268 struct bstr data_string;
271 #define MICRODVD_PERSISTENT_OFF 0
272 #define MICRODVD_PERSISTENT_ON 1
273 #define MICRODVD_PERSISTENT_OPENED 2
275 // Color, Font, Size, cHarset, stYle, Position, cOordinate
276 #define MICRODVD_TAGS "cfshyYpo"
278 static void microdvd_set_tag(struct microdvd_tag *tags, struct microdvd_tag tag)
280 int tag_index = indexof(MICRODVD_TAGS, tag.key);
282 if (tag_index < 0)
283 return;
284 memcpy(&tags[tag_index], &tag, sizeof(tag));
287 // italic, bold, underline, strike-through
288 #define MICRODVD_STYLES "ibus"
290 static char *microdvd_load_tags(struct microdvd_tag *tags, char *s)
292 while (*s == '{') {
293 char *start = s;
294 char tag_char = *(s + 1);
295 struct microdvd_tag tag = {0};
297 if (!tag_char || *(s + 2) != ':')
298 break;
299 s += 3;
301 switch (tag_char) {
303 /* Style */
304 case 'Y':
305 tag.persistent = MICRODVD_PERSISTENT_ON;
306 case 'y':
307 while (*s && *s != '}') {
308 int style_index = indexof(MICRODVD_STYLES, *s);
310 if (style_index >= 0)
311 tag.data1 |= (1 << style_index);
312 s++;
314 if (*s != '}')
315 break;
316 /* We must distinguish persistent and non-persistent styles
317 * to handle this kind of style tags: {y:ib}{Y:us} */
318 tag.key = tag_char;
319 break;
321 /* Color */
322 case 'C':
323 tag.persistent = MICRODVD_PERSISTENT_ON;
324 case 'c':
325 tag.data1 = strtol(s, &s, 16) & 0x00ffffff;
326 if (*s != '}')
327 break;
328 tag.key = 'c';
329 break;
331 /* Font name */
332 case 'F':
333 tag.persistent = MICRODVD_PERSISTENT_ON;
334 case 'f':
336 int len = indexof(s, '}');
337 if (len < 0)
338 break;
339 tag.data_string.start = s;
340 tag.data_string.len = len;
341 s += len;
342 tag.key = 'f';
343 break;
346 /* Font size */
347 case 'S':
348 tag.persistent = MICRODVD_PERSISTENT_ON;
349 case 's':
350 tag.data1 = strtol(s, &s, 10);
351 if (*s != '}')
352 break;
353 tag.key = 's';
354 break;
356 /* Charset */
357 case 'H':
359 //TODO: not yet handled, just parsed.
360 int len = indexof(s, '}');
361 if (len < 0)
362 break;
363 tag.data_string.start = s;
364 tag.data_string.len = len;
365 s += len;
366 tag.key = 'h';
367 break;
370 /* Position */
371 case 'P':
372 tag.persistent = MICRODVD_PERSISTENT_ON;
373 tag.data1 = (*s++ == '1');
374 if (*s != '}')
375 break;
376 tag.key = 'p';
377 break;
379 /* Coordinates */
380 case 'o':
381 tag.persistent = MICRODVD_PERSISTENT_ON;
382 tag.data1 = strtol(s, &s, 10);
383 if (*s != ',')
384 break;
385 s++;
386 tag.data2 = strtol(s, &s, 10);
387 if (*s != '}')
388 break;
389 tag.key = 'o';
390 break;
392 default: /* Unknown tag, we consider it to be text */
393 break;
396 if (tag.key == 0)
397 return start;
399 microdvd_set_tag(tags, tag);
400 s++;
402 return s;
405 static void microdvd_open_tags(struct line *new_line, struct microdvd_tag *tags)
407 for (int i = 0; i < sizeof(MICRODVD_TAGS) - 1; i++) {
408 if (tags[i].persistent == MICRODVD_PERSISTENT_OPENED)
409 continue;
410 switch (tags[i].key) {
411 case 'Y':
412 case 'y':
413 for (int sidx = 0; sidx < sizeof(MICRODVD_STYLES) - 1; sidx++)
414 if (tags[i].data1 & (1 << sidx))
415 append_text(new_line, "{\\%c1}", MICRODVD_STYLES[sidx]);
416 break;
418 case 'c':
419 append_text(new_line, "{\\c&H%06X&}", tags[i].data1);
420 break;
422 case 'f':
423 append_text(new_line, "{\\fn%.*s}", BSTR_P(tags[i].data_string));
424 break;
426 case 's':
427 append_text(new_line, "{\\fs%d}", tags[i].data1);
428 break;
430 case 'p':
431 if (tags[i].data1 == 0)
432 append_text(new_line, "{\\an8}");
433 break;
435 case 'o':
436 append_text(new_line, "{\\pos(%d,%d)}",
437 tags[i].data1, tags[i].data2);
438 break;
440 if (tags[i].persistent == MICRODVD_PERSISTENT_ON)
441 tags[i].persistent = MICRODVD_PERSISTENT_OPENED;
445 static void microdvd_close_no_persistent_tags(struct line *new_line,
446 struct microdvd_tag *tags)
448 int i;
450 for (i = sizeof(MICRODVD_TAGS) - 2; i; i--) {
451 if (tags[i].persistent != MICRODVD_PERSISTENT_OFF)
452 continue;
453 switch (tags[i].key) {
455 case 'y':
456 for (int sidx = sizeof(MICRODVD_STYLES) - 2; sidx >= 0; sidx--)
457 if (tags[i].data1 & (1 << sidx))
458 append_text(new_line, "{\\%c0}", MICRODVD_STYLES[sidx]);
459 break;
461 case 'c':
462 append_text(new_line, "{\\c}");
463 break;
465 case 'f':
466 append_text(new_line, "{\\fn}");
467 break;
469 case 's':
470 append_text(new_line, "{\\fs}");
471 break;
473 tags[i].key = 0;
477 void subassconvert_microdvd(const char *orig, char *dest, int dest_buffer_size)
479 /* line is not const to avoid warnings with strtol, etc.
480 * orig content won't be changed */
481 char *line = (char *)orig;
482 struct line new_line = {
483 .buf = dest,
484 .bufsize = dest_buffer_size,
486 struct microdvd_tag tags[sizeof(MICRODVD_TAGS) - 1] = {{0}};
488 while (*line) {
489 line = microdvd_load_tags(tags, line);
490 microdvd_open_tags(&new_line, tags);
492 while (*line && *line != '|')
493 new_line.buf[new_line.len++] = *line++;
495 if (*line == '|') {
496 microdvd_close_no_persistent_tags(&new_line, tags);
497 append_text(&new_line, "\\N");
498 line++;
501 new_line.buf[new_line.len] = 0;