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.

273 lines
10.0KB

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