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.

205 lines
7.7KB

  1. /*
  2. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avstring.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/parseutils.h"
  23. #include "htmlsubtitles.h"
  24. static int html_color_parse(void *log_ctx, const char *str)
  25. {
  26. uint8_t rgba[4];
  27. if (av_parse_color(rgba, str, strcspn(str, "\" >"), log_ctx) < 0)
  28. return -1;
  29. return rgba[0] | rgba[1] << 8 | rgba[2] << 16;
  30. }
  31. enum {
  32. PARAM_UNKNOWN = -1,
  33. PARAM_SIZE,
  34. PARAM_COLOR,
  35. PARAM_FACE,
  36. PARAM_NUMBER
  37. };
  38. typedef struct SrtStack {
  39. char tag[128];
  40. char param[PARAM_NUMBER][128];
  41. } SrtStack;
  42. static void rstrip_spaces_buf(AVBPrint *buf)
  43. {
  44. if (av_bprint_is_complete(buf))
  45. while (buf->len > 0 && buf->str[buf->len - 1] == ' ')
  46. buf->str[--buf->len] = 0;
  47. }
  48. /* skip all {\xxx} substrings except for {\an%d}
  49. and all microdvd like styles such as {Y:xxx} */
  50. static void handle_open_brace(AVBPrint *dst, const char **inp, int *an, int *closing_brace_missing)
  51. {
  52. int len = 0;
  53. const char *in = *inp;
  54. *an += sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0;
  55. if (!*closing_brace_missing) {
  56. if ( (*an != 1 && in[1] == '\\')
  57. || (in[1] && strchr("CcFfoPSsYy", in[1]) && in[2] == ':')) {
  58. char *bracep = strchr(in+2, '}');
  59. if (bracep) {
  60. *inp = bracep;
  61. return;
  62. } else
  63. *closing_brace_missing = 1;
  64. }
  65. }
  66. av_bprint_chars(dst, *in, 1);
  67. }
  68. int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
  69. {
  70. char *param, buffer[128], tmp[128];
  71. int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
  72. SrtStack stack[16];
  73. int closing_brace_missing = 0;
  74. stack[0].tag[0] = 0;
  75. strcpy(stack[0].param[PARAM_SIZE], "{\\fs}");
  76. strcpy(stack[0].param[PARAM_COLOR], "{\\c}");
  77. strcpy(stack[0].param[PARAM_FACE], "{\\fn}");
  78. for (; !end && *in; in++) {
  79. switch (*in) {
  80. case '\r':
  81. break;
  82. case '\n':
  83. if (line_start) {
  84. end = 1;
  85. break;
  86. }
  87. rstrip_spaces_buf(dst);
  88. av_bprintf(dst, "\\N");
  89. line_start = 1;
  90. break;
  91. case ' ':
  92. if (!line_start)
  93. av_bprint_chars(dst, *in, 1);
  94. break;
  95. case '{':
  96. handle_open_brace(dst, &in, &an, &closing_brace_missing);
  97. break;
  98. case '<':
  99. tag_close = in[1] == '/';
  100. len = 0;
  101. if (sscanf(in+tag_close+1, "%127[^>]>%n", buffer, &len) >= 1 && len > 0) {
  102. const char *tagname = buffer;
  103. while (*tagname == ' ')
  104. tagname++;
  105. if ((param = strchr(tagname, ' ')))
  106. *param++ = 0;
  107. if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
  108. ( tag_close && sptr > 0 && !av_strcasecmp(stack[sptr-1].tag, tagname))) {
  109. int i, j, unknown = 0;
  110. in += len + tag_close;
  111. if (!tag_close)
  112. memset(stack+sptr, 0, sizeof(*stack));
  113. if (!av_strcasecmp(tagname, "font")) {
  114. if (tag_close) {
  115. for (i=PARAM_NUMBER-1; i>=0; i--)
  116. if (stack[sptr-1].param[i][0])
  117. for (j=sptr-2; j>=0; j--)
  118. if (stack[j].param[i][0]) {
  119. av_bprintf(dst, "%s", stack[j].param[i]);
  120. break;
  121. }
  122. } else {
  123. while (param) {
  124. if (!av_strncasecmp(param, "size=", 5)) {
  125. unsigned font_size;
  126. param += 5 + (param[5] == '"');
  127. if (sscanf(param, "%u", &font_size) == 1) {
  128. snprintf(stack[sptr].param[PARAM_SIZE],
  129. sizeof(stack[0].param[PARAM_SIZE]),
  130. "{\\fs%u}", font_size);
  131. }
  132. } else if (!av_strncasecmp(param, "color=", 6)) {
  133. param += 6 + (param[6] == '"');
  134. snprintf(stack[sptr].param[PARAM_COLOR],
  135. sizeof(stack[0].param[PARAM_COLOR]),
  136. "{\\c&H%X&}",
  137. html_color_parse(log_ctx, param));
  138. } else if (!av_strncasecmp(param, "face=", 5)) {
  139. param += 5 + (param[5] == '"');
  140. len = strcspn(param,
  141. param[-1] == '"' ? "\"" :" ");
  142. av_strlcpy(tmp, param,
  143. FFMIN(sizeof(tmp), len+1));
  144. param += len;
  145. snprintf(stack[sptr].param[PARAM_FACE],
  146. sizeof(stack[0].param[PARAM_FACE]),
  147. "{\\fn%s}", tmp);
  148. }
  149. if ((param = strchr(param, ' ')))
  150. param++;
  151. }
  152. for (i=0; i<PARAM_NUMBER; i++)
  153. if (stack[sptr].param[i][0])
  154. av_bprintf(dst, "%s", stack[sptr].param[i]);
  155. }
  156. } else if (tagname[0] && !tagname[1] && av_stristr("bisu", tagname)) {
  157. av_bprintf(dst, "{\\%c%d}", (char)av_tolower(tagname[0]), !tag_close);
  158. } else if (!av_strcasecmp(tagname, "br")) {
  159. av_bprintf(dst, "\\N");
  160. } else {
  161. unknown = 1;
  162. snprintf(tmp, sizeof(tmp), "</%s>", tagname);
  163. }
  164. if (tag_close) {
  165. sptr--;
  166. } else if (unknown && !strstr(in, tmp)) {
  167. in -= len + tag_close;
  168. av_bprint_chars(dst, *in, 1);
  169. } else
  170. av_strlcpy(stack[sptr++].tag, tagname,
  171. sizeof(stack[0].tag));
  172. break;
  173. }
  174. }
  175. default:
  176. av_bprint_chars(dst, *in, 1);
  177. break;
  178. }
  179. if (*in != ' ' && *in != '\r' && *in != '\n')
  180. line_start = 0;
  181. }
  182. if (!av_bprint_is_complete(dst))
  183. return AVERROR(ENOMEM);
  184. while (dst->len >= 2 && !strncmp(&dst->str[dst->len - 2], "\\N", 2))
  185. dst->len -= 2;
  186. dst->str[dst->len] = 0;
  187. rstrip_spaces_buf(dst);
  188. return 0;
  189. }