2 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/avstring.h"
22 #include "libavutil/common.h"
23 #include "libavutil/parseutils.h"
24 #include "htmlsubtitles.h"
26 static int html_color_parse(void *log_ctx, const char *str)
29 if (av_parse_color(rgba, str, strcspn(str, "\" >"), log_ctx) < 0)
31 return rgba[0] | rgba[1] << 8 | rgba[2] << 16;
42 typedef struct SrtStack {
44 char param[PARAM_NUMBER][128];
47 static void rstrip_spaces_buf(AVBPrint *buf)
49 if (av_bprint_is_complete(buf))
50 while (buf->len > 0 && buf->str[buf->len - 1] == ' ')
51 buf->str[--buf->len] = 0;
54 int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
56 char *param, buffer[128], tmp[128];
57 int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
61 strcpy(stack[0].param[PARAM_SIZE], "{\\fs}");
62 strcpy(stack[0].param[PARAM_COLOR], "{\\c}");
63 strcpy(stack[0].param[PARAM_FACE], "{\\fn}");
65 for (; !end && *in; in++) {
74 rstrip_spaces_buf(dst);
75 av_bprintf(dst, "\\N");
80 av_bprint_chars(dst, *in, 1);
82 case '{': /* skip all {\xxx} substrings except for {\an%d}
83 and all microdvd like styles such as {Y:xxx} */
85 an += sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0;
86 if ((an != 1 && (len = 0, sscanf(in, "{\\%*[^}]}%n", &len) >= 0 && len > 0)) ||
87 (len = 0, sscanf(in, "{%*1[CcFfoPSsYy]:%*[^}]}%n", &len) >= 0 && len > 0)) {
90 av_bprint_chars(dst, *in, 1);
93 tag_close = in[1] == '/';
95 if (sscanf(in+tag_close+1, "%127[^>]>%n", buffer, &len) >= 1 && len > 0) {
96 const char *tagname = buffer;
97 while (*tagname == ' ')
99 if ((param = strchr(tagname, ' ')))
101 if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
102 ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, tagname))) {
103 int i, j, unknown = 0;
104 in += len + tag_close;
106 memset(stack+sptr, 0, sizeof(*stack));
107 if (!strcmp(tagname, "font")) {
109 for (i=PARAM_NUMBER-1; i>=0; i--)
110 if (stack[sptr-1].param[i][0])
111 for (j=sptr-2; j>=0; j--)
112 if (stack[j].param[i][0]) {
113 av_bprintf(dst, "%s", stack[j].param[i]);
118 if (!strncmp(param, "size=", 5)) {
120 param += 5 + (param[5] == '"');
121 if (sscanf(param, "%u", &font_size) == 1) {
122 snprintf(stack[sptr].param[PARAM_SIZE],
123 sizeof(stack[0].param[PARAM_SIZE]),
124 "{\\fs%u}", font_size);
126 } else if (!strncmp(param, "color=", 6)) {
127 param += 6 + (param[6] == '"');
128 snprintf(stack[sptr].param[PARAM_COLOR],
129 sizeof(stack[0].param[PARAM_COLOR]),
131 html_color_parse(log_ctx, param));
132 } else if (!strncmp(param, "face=", 5)) {
133 param += 5 + (param[5] == '"');
135 param[-1] == '"' ? "\"" :" ");
136 av_strlcpy(tmp, param,
137 FFMIN(sizeof(tmp), len+1));
139 snprintf(stack[sptr].param[PARAM_FACE],
140 sizeof(stack[0].param[PARAM_FACE]),
143 if ((param = strchr(param, ' ')))
146 for (i=0; i<PARAM_NUMBER; i++)
147 if (stack[sptr].param[i][0])
148 av_bprintf(dst, "%s", stack[sptr].param[i]);
150 } else if (tagname[0] && !tagname[1] && strspn(tagname, "bisu") == 1) {
151 av_bprintf(dst, "{\\%c%d}", tagname[0], !tag_close);
154 snprintf(tmp, sizeof(tmp), "</%s>", tagname);
158 } else if (unknown && !strstr(in, tmp)) {
159 in -= len + tag_close;
160 av_bprint_chars(dst, *in, 1);
162 av_strlcpy(stack[sptr++].tag, tagname,
163 sizeof(stack[0].tag));
168 av_bprint_chars(dst, *in, 1);
171 if (*in != ' ' && *in != '\r' && *in != '\n')
175 if (!av_bprint_is_complete(dst))
176 return AVERROR(ENOMEM);
178 while (dst->len >= 2 && !strncmp(&dst->str[dst->len - 2], "\\N", 2))
180 dst->str[dst->len] = 0;
181 rstrip_spaces_buf(dst);