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.

272 lines
9.9KB

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