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.

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