2 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
3 * Copyright (c) 2017 Clément Bœsch <u@pkh.me>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avassert.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/common.h"
25 #include "libavutil/parseutils.h"
26 #include "htmlsubtitles.h"
28 static int html_color_parse(void *log_ctx, const char *str)
32 while (str[nb_sharps] == '#')
34 str += FFMAX(0, nb_sharps - 1);
35 if (av_parse_color(rgba, str, strcspn(str, "\" >"), log_ctx) < 0)
37 return rgba[0] | rgba[1] << 8 | rgba[2] << 16;
40 static void rstrip_spaces_buf(AVBPrint *buf)
42 if (av_bprint_is_complete(buf))
43 while (buf->len > 0 && buf->str[buf->len - 1] == ' ')
44 buf->str[--buf->len] = 0;
47 /* skip all {\xxx} substrings except for {\an%d}
48 and all microdvd like styles such as {Y:xxx} */
49 static void handle_open_brace(AVBPrint *dst, const char **inp, int *an, int *closing_brace_missing)
52 const char *in = *inp;
54 *an += sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0;
56 if (!*closing_brace_missing) {
57 if ( (*an != 1 && in[1] == '\\')
58 || (in[1] && strchr("CcFfoPSsYy", in[1]) && in[2] == ':')) {
59 char *bracep = strchr(in+2, '}');
64 *closing_brace_missing = 1;
68 av_bprint_chars(dst, *in, 1);
78 * The general politic of the convert is to mask unsupported tags or formatting
79 * errors (but still alert the user/subtitles writer with an error/warning)
80 * without dropping any actual text content for the final user.
82 int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
84 char *param, buffer[128];
85 int len, tag_close, sptr = 0, line_start = 1, an = 0, end = 0;
86 int closing_brace_missing = 0;
90 * state stack is only present for fonts since they are the only tags where
91 * the state is not binary. Here is a typical use case:
93 * <font color="red" size=10>
95 * <font size=50> RED AND BIG </font>
99 * On the other hand, using the state system for all the tags should be
100 * avoided because it breaks wrongly nested tags such as:
102 * <b> foo <i> bar </b> bla </i>
104 * We don't want to break here; instead, we will treat all these tags as
105 * binary state markers. Basically, "<b>" will activate bold, and "</b>"
106 * will deactivate it, whatever the current state.
108 * This will also prevents cases where we have a random closing tag
109 * remaining after the opening one was dropped. Yes, this happens and we
110 * still don't want to print a "</b>" at the end of the dialog event.
112 struct font_tag stack[16];
114 memset(&stack[0], 0, sizeof(stack[0]));
116 for (; !end && *in; in++) {
125 rstrip_spaces_buf(dst);
126 av_bprintf(dst, "\\N");
131 av_bprint_chars(dst, *in, 1);
134 handle_open_brace(dst, &in, &an, &closing_brace_missing);
138 * "<<" are likely latin guillemets in ASCII or some kind of random
139 * style effect; see sub/badsyntax.srt in the FATE samples
140 * directory for real test cases.
144 for (i = 0; in[1] == '<'; i++) {
145 av_bprint_chars(dst, '<', 1);
150 tag_close = in[1] == '/';
154 av_assert0(in[0] == '<');
158 if (sscanf(in+tag_close+1, "%127[^<>]>%n", buffer, &len) >= 1 && len > 0) {
159 const int skip = len + tag_close;
160 const char *tagname = buffer;
161 while (*tagname == ' ') {
165 if ((param = strchr(tagname, ' ')))
168 /* Check if this is likely a tag */
169 #define LIKELY_A_TAG_CHAR(x) (((x) >= '0' && (x) <= '9') || \
170 ((x) >= 'a' && (x) <= 'z') || \
171 ((x) >= 'A' && (x) <= 'Z') || \
172 (x) == '_' || (x) == '/')
173 for (i = 0; tagname[i]; i++) {
174 if (!LIKELY_A_TAG_CHAR(tagname[i])) {
180 if (!av_strcasecmp(tagname, "font")) {
181 if (tag_close && sptr > 0) {
182 struct font_tag *cur_tag = &stack[sptr--];
183 struct font_tag *last_tag = &stack[sptr];
187 av_bprintf(dst, "{\\fs}");
188 else if (last_tag->size != cur_tag->size)
189 av_bprintf(dst, "{\\fs%d}", last_tag->size);
192 if (cur_tag->color & 0xff000000) {
193 if (!(last_tag->color & 0xff000000))
194 av_bprintf(dst, "{\\c}");
195 else if (last_tag->color != cur_tag->color)
196 av_bprintf(dst, "{\\c&H%"PRIX32"&}", last_tag->color & 0xffffff);
199 if (cur_tag->face[0]) {
200 if (!last_tag->face[0])
201 av_bprintf(dst, "{\\fn}");
202 else if (strcmp(last_tag->face, cur_tag->face))
203 av_bprintf(dst, "{\\fn%s}", last_tag->face);
205 } else if (!tag_close && sptr < FF_ARRAY_ELEMS(stack) - 1) {
206 struct font_tag *new_tag = &stack[sptr + 1];
208 *new_tag = stack[sptr++];
211 if (!av_strncasecmp(param, "size=", 5)) {
212 param += 5 + (param[5] == '"');
213 if (sscanf(param, "%u", &new_tag->size) == 1)
214 av_bprintf(dst, "{\\fs%u}", new_tag->size);
215 } else if (!av_strncasecmp(param, "color=", 6)) {
217 param += 6 + (param[6] == '"');
218 color = html_color_parse(log_ctx, param);
220 new_tag->color = 0xff000000 | color;
221 av_bprintf(dst, "{\\c&H%"PRIX32"&}", new_tag->color & 0xffffff);
223 } else if (!av_strncasecmp(param, "face=", 5)) {
224 param += 5 + (param[5] == '"');
226 param[-1] == '"' ? "\"" :" ");
227 av_strlcpy(new_tag->face, param,
228 FFMIN(sizeof(new_tag->face), len+1));
230 av_bprintf(dst, "{\\fn%s}", new_tag->face);
232 if ((param = strchr(param, ' ')))
237 } else if (tagname[0] && !tagname[1] && strchr("bisu", av_tolower(tagname[0]))) {
238 av_bprintf(dst, "{\\%c%d}", (char)av_tolower(tagname[0]), !tag_close);
240 } else if (!av_strncasecmp(tagname, "br", 2) &&
241 (!tagname[2] || (tagname[2] == '/' && !tagname[3]))) {
242 av_bprintf(dst, "\\N");
244 } else if (likely_a_tag) {
245 if (!tag_close) // warn only once
246 av_log(log_ctx, AV_LOG_WARNING, "Unrecognized tag %s\n", tagname);
249 av_bprint_chars(dst, '<', 1);
252 av_bprint_chars(dst, *in, 1);
256 av_bprint_chars(dst, *in, 1);
259 if (*in != ' ' && *in != '\r' && *in != '\n')
263 if (!av_bprint_is_complete(dst))
264 return AVERROR(ENOMEM);
266 while (dst->len >= 2 && !strncmp(&dst->str[dst->len - 2], "\\N", 2))
268 dst->str[dst->len] = 0;
269 rstrip_spaces_buf(dst);