You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
11KB

  1. /*
  2. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  3. * Copyright (c) 2017 Clément Bœsch <u@pkh.me>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/parseutils.h"
  25. #include "htmlsubtitles.h"
  26. #include <ctype.h>
  27. static int html_color_parse(void *log_ctx, const char *str)
  28. {
  29. uint8_t rgba[4];
  30. int nb_sharps = 0;
  31. while (str[nb_sharps] == '#')
  32. nb_sharps++;
  33. str += FFMAX(0, nb_sharps - 1);
  34. if (av_parse_color(rgba, str, strcspn(str, "\" >"), log_ctx) < 0)
  35. return -1;
  36. return rgba[0] | rgba[1] << 8 | rgba[2] << 16;
  37. }
  38. static void rstrip_spaces_buf(AVBPrint *buf)
  39. {
  40. if (av_bprint_is_complete(buf))
  41. while (buf->len > 0 && buf->str[buf->len - 1] == ' ')
  42. buf->str[--buf->len] = 0;
  43. }
  44. /*
  45. * Fast code for scanning text enclosed in braces. Functionally
  46. * equivalent to this sscanf call:
  47. *
  48. * sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0
  49. */
  50. static int scanbraces(const char* in) {
  51. if (strncmp(in, "{\\an", 4) != 0) {
  52. return 0;
  53. }
  54. if (!av_isdigit(in[4])) {
  55. return 0;
  56. }
  57. if (in[5] != '}') {
  58. return 0;
  59. }
  60. return 1;
  61. }
  62. /* skip all {\xxx} substrings except for {\an%d}
  63. and all microdvd like styles such as {Y:xxx} */
  64. static void handle_open_brace(AVBPrint *dst, const char **inp, int *an, int *closing_brace_missing)
  65. {
  66. const char *in = *inp;
  67. *an += scanbraces(in);
  68. if (!*closing_brace_missing) {
  69. if ( (*an != 1 && in[1] == '\\')
  70. || (in[1] && strchr("CcFfoPSsYy", in[1]) && in[2] == ':')) {
  71. char *bracep = strchr(in+2, '}');
  72. if (bracep) {
  73. *inp = bracep;
  74. return;
  75. } else
  76. *closing_brace_missing = 1;
  77. }
  78. }
  79. av_bprint_chars(dst, *in, 1);
  80. }
  81. struct font_tag {
  82. char face[128];
  83. int size;
  84. uint32_t color;
  85. };
  86. /*
  87. * Fast code for scanning the rest of a tag. Functionally equivalent to
  88. * this sscanf call:
  89. *
  90. * sscanf(in, "%127[^<>]>%n", buffer, lenp) == 2
  91. */
  92. static int scantag(const char* in, char* buffer, int* lenp) {
  93. int len;
  94. for (len = 0; len < 128; len++) {
  95. const char c = *in++;
  96. switch (c) {
  97. case '\0':
  98. return 0;
  99. case '<':
  100. return 0;
  101. case '>':
  102. buffer[len] = '\0';
  103. *lenp = len+1;
  104. return 1;
  105. default:
  106. break;
  107. }
  108. buffer[len] = c;
  109. }
  110. return 0;
  111. }
  112. /*
  113. * The general politic of the convert is to mask unsupported tags or formatting
  114. * errors (but still alert the user/subtitles writer with an error/warning)
  115. * without dropping any actual text content for the final user.
  116. */
  117. int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
  118. {
  119. char *param, buffer[128];
  120. int len, tag_close, sptr = 0, line_start = 1, an = 0, end = 0;
  121. int closing_brace_missing = 0;
  122. int i, likely_a_tag;
  123. /*
  124. * state stack is only present for fonts since they are the only tags where
  125. * the state is not binary. Here is a typical use case:
  126. *
  127. * <font color="red" size=10>
  128. * red 10
  129. * <font size=50> RED AND BIG </font>
  130. * red 10 again
  131. * </font>
  132. *
  133. * On the other hand, using the state system for all the tags should be
  134. * avoided because it breaks wrongly nested tags such as:
  135. *
  136. * <b> foo <i> bar </b> bla </i>
  137. *
  138. * We don't want to break here; instead, we will treat all these tags as
  139. * binary state markers. Basically, "<b>" will activate bold, and "</b>"
  140. * will deactivate it, whatever the current state.
  141. *
  142. * This will also prevents cases where we have a random closing tag
  143. * remaining after the opening one was dropped. Yes, this happens and we
  144. * still don't want to print a "</b>" at the end of the dialog event.
  145. */
  146. struct font_tag stack[16];
  147. memset(&stack[0], 0, sizeof(stack[0]));
  148. for (; !end && *in; in++) {
  149. switch (*in) {
  150. case '\r':
  151. break;
  152. case '\n':
  153. if (line_start) {
  154. end = 1;
  155. break;
  156. }
  157. rstrip_spaces_buf(dst);
  158. av_bprintf(dst, "\\N");
  159. line_start = 1;
  160. break;
  161. case ' ':
  162. if (!line_start)
  163. av_bprint_chars(dst, *in, 1);
  164. break;
  165. case '{':
  166. handle_open_brace(dst, &in, &an, &closing_brace_missing);
  167. break;
  168. case '<':
  169. /*
  170. * "<<" are likely latin guillemets in ASCII or some kind of random
  171. * style effect; see sub/badsyntax.srt in the FATE samples
  172. * directory for real test cases.
  173. */
  174. likely_a_tag = 1;
  175. for (i = 0; in[1] == '<'; i++) {
  176. av_bprint_chars(dst, '<', 1);
  177. likely_a_tag = 0;
  178. in++;
  179. }
  180. tag_close = in[1] == '/';
  181. if (tag_close)
  182. likely_a_tag = 1;
  183. av_assert0(in[0] == '<');
  184. len = 0;
  185. if (scantag(in+tag_close+1, buffer, &len) && len > 0) {
  186. const int skip = len + tag_close;
  187. const char *tagname = buffer;
  188. while (*tagname == ' ') {
  189. likely_a_tag = 0;
  190. tagname++;
  191. }
  192. if ((param = strchr(tagname, ' ')))
  193. *param++ = 0;
  194. /* Check if this is likely a tag */
  195. #define LIKELY_A_TAG_CHAR(x) (((x) >= '0' && (x) <= '9') || \
  196. ((x) >= 'a' && (x) <= 'z') || \
  197. ((x) >= 'A' && (x) <= 'Z') || \
  198. (x) == '_' || (x) == '/')
  199. for (i = 0; tagname[i]; i++) {
  200. if (!LIKELY_A_TAG_CHAR(tagname[i])) {
  201. likely_a_tag = 0;
  202. break;
  203. }
  204. }
  205. if (!av_strcasecmp(tagname, "font")) {
  206. if (tag_close && sptr > 0) {
  207. struct font_tag *cur_tag = &stack[sptr--];
  208. struct font_tag *last_tag = &stack[sptr];
  209. if (cur_tag->size) {
  210. if (!last_tag->size)
  211. av_bprintf(dst, "{\\fs}");
  212. else if (last_tag->size != cur_tag->size)
  213. av_bprintf(dst, "{\\fs%d}", last_tag->size);
  214. }
  215. if (cur_tag->color & 0xff000000) {
  216. if (!(last_tag->color & 0xff000000))
  217. av_bprintf(dst, "{\\c}");
  218. else if (last_tag->color != cur_tag->color)
  219. av_bprintf(dst, "{\\c&H%"PRIX32"&}", last_tag->color & 0xffffff);
  220. }
  221. if (cur_tag->face[0]) {
  222. if (!last_tag->face[0])
  223. av_bprintf(dst, "{\\fn}");
  224. else if (strcmp(last_tag->face, cur_tag->face))
  225. av_bprintf(dst, "{\\fn%s}", last_tag->face);
  226. }
  227. } else if (!tag_close && sptr < FF_ARRAY_ELEMS(stack) - 1) {
  228. struct font_tag *new_tag = &stack[sptr + 1];
  229. *new_tag = stack[sptr++];
  230. while (param) {
  231. if (!av_strncasecmp(param, "size=", 5)) {
  232. param += 5 + (param[5] == '"');
  233. if (sscanf(param, "%u", &new_tag->size) == 1)
  234. av_bprintf(dst, "{\\fs%u}", new_tag->size);
  235. } else if (!av_strncasecmp(param, "color=", 6)) {
  236. int color;
  237. param += 6 + (param[6] == '"');
  238. color = html_color_parse(log_ctx, param);
  239. if (color >= 0) {
  240. new_tag->color = 0xff000000 | color;
  241. av_bprintf(dst, "{\\c&H%"PRIX32"&}", new_tag->color & 0xffffff);
  242. }
  243. } else if (!av_strncasecmp(param, "face=", 5)) {
  244. param += 5 + (param[5] == '"');
  245. len = strcspn(param,
  246. param[-1] == '"' ? "\"" :" ");
  247. av_strlcpy(new_tag->face, param,
  248. FFMIN(sizeof(new_tag->face), len+1));
  249. param += len;
  250. av_bprintf(dst, "{\\fn%s}", new_tag->face);
  251. }
  252. if ((param = strchr(param, ' ')))
  253. param++;
  254. }
  255. }
  256. in += skip;
  257. } else if (tagname[0] && !tagname[1] && strchr("bisu", av_tolower(tagname[0]))) {
  258. av_bprintf(dst, "{\\%c%d}", (char)av_tolower(tagname[0]), !tag_close);
  259. in += skip;
  260. } else if (!av_strncasecmp(tagname, "br", 2) &&
  261. (!tagname[2] || (tagname[2] == '/' && !tagname[3]))) {
  262. av_bprintf(dst, "\\N");
  263. in += skip;
  264. } else if (likely_a_tag) {
  265. if (!tag_close) // warn only once
  266. av_log(log_ctx, AV_LOG_WARNING, "Unrecognized tag %s\n", tagname);
  267. in += skip;
  268. } else {
  269. av_bprint_chars(dst, '<', 1);
  270. }
  271. } else {
  272. av_bprint_chars(dst, *in, 1);
  273. }
  274. break;
  275. default:
  276. av_bprint_chars(dst, *in, 1);
  277. break;
  278. }
  279. if (*in != ' ' && *in != '\r' && *in != '\n')
  280. line_start = 0;
  281. }
  282. if (!av_bprint_is_complete(dst))
  283. return AVERROR(ENOMEM);
  284. while (dst->len >= 2 && !strncmp(&dst->str[dst->len - 2], "\\N", 2))
  285. dst->len -= 2;
  286. dst->str[dst->len] = 0;
  287. rstrip_spaces_buf(dst);
  288. return 0;
  289. }