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.

195 lines
7.5KB

  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. int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
  49. {
  50. char *param, buffer[128], tmp[128];
  51. int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
  52. SrtStack stack[16];
  53. int closing_brace_missing = 0;
  54. stack[0].tag[0] = 0;
  55. strcpy(stack[0].param[PARAM_SIZE], "{\\fs}");
  56. strcpy(stack[0].param[PARAM_COLOR], "{\\c}");
  57. strcpy(stack[0].param[PARAM_FACE], "{\\fn}");
  58. for (; !end && *in; in++) {
  59. switch (*in) {
  60. case '\r':
  61. break;
  62. case '\n':
  63. if (line_start) {
  64. end = 1;
  65. break;
  66. }
  67. rstrip_spaces_buf(dst);
  68. av_bprintf(dst, "\\N");
  69. line_start = 1;
  70. break;
  71. case ' ':
  72. if (!line_start)
  73. av_bprint_chars(dst, *in, 1);
  74. break;
  75. case '{': /* skip all {\xxx} substrings except for {\an%d}
  76. and all microdvd like styles such as {Y:xxx} */
  77. len = 0;
  78. an += sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0;
  79. if (!closing_brace_missing) {
  80. if ( (an != 1 && in[1] == '\\')
  81. || (in[1] && strchr("CcFfoPSsYy", in[1]) && in[2] == ':')) {
  82. char *bracep = strchr(in+2, '}');
  83. if (bracep) {
  84. in = bracep;
  85. break;
  86. } else
  87. closing_brace_missing = 1;
  88. }
  89. }
  90. av_bprint_chars(dst, *in, 1);
  91. break;
  92. case '<':
  93. tag_close = in[1] == '/';
  94. len = 0;
  95. if (sscanf(in+tag_close+1, "%127[^>]>%n", buffer, &len) >= 1 && len > 0) {
  96. const char *tagname = buffer;
  97. while (*tagname == ' ')
  98. tagname++;
  99. if ((param = strchr(tagname, ' ')))
  100. *param++ = 0;
  101. if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
  102. ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, tagname))) {
  103. int i, j, unknown = 0;
  104. in += len + tag_close;
  105. if (!tag_close)
  106. memset(stack+sptr, 0, sizeof(*stack));
  107. if (!strcmp(tagname, "font")) {
  108. if (tag_close) {
  109. for (i=PARAM_NUMBER-1; i>=0; i--)
  110. if (stack[sptr-1].param[i][0])
  111. for (j=sptr-2; j>=0; j--)
  112. if (stack[j].param[i][0]) {
  113. av_bprintf(dst, "%s", stack[j].param[i]);
  114. break;
  115. }
  116. } else {
  117. while (param) {
  118. if (!strncmp(param, "size=", 5)) {
  119. unsigned font_size;
  120. param += 5 + (param[5] == '"');
  121. if (sscanf(param, "%u", &font_size) == 1) {
  122. snprintf(stack[sptr].param[PARAM_SIZE],
  123. sizeof(stack[0].param[PARAM_SIZE]),
  124. "{\\fs%u}", font_size);
  125. }
  126. } else if (!strncmp(param, "color=", 6)) {
  127. param += 6 + (param[6] == '"');
  128. snprintf(stack[sptr].param[PARAM_COLOR],
  129. sizeof(stack[0].param[PARAM_COLOR]),
  130. "{\\c&H%X&}",
  131. html_color_parse(log_ctx, param));
  132. } else if (!strncmp(param, "face=", 5)) {
  133. param += 5 + (param[5] == '"');
  134. len = strcspn(param,
  135. param[-1] == '"' ? "\"" :" ");
  136. av_strlcpy(tmp, param,
  137. FFMIN(sizeof(tmp), len+1));
  138. param += len;
  139. snprintf(stack[sptr].param[PARAM_FACE],
  140. sizeof(stack[0].param[PARAM_FACE]),
  141. "{\\fn%s}", tmp);
  142. }
  143. if ((param = strchr(param, ' ')))
  144. param++;
  145. }
  146. for (i=0; i<PARAM_NUMBER; i++)
  147. if (stack[sptr].param[i][0])
  148. av_bprintf(dst, "%s", stack[sptr].param[i]);
  149. }
  150. } else if (tagname[0] && !tagname[1] && strspn(tagname, "bisu") == 1) {
  151. av_bprintf(dst, "{\\%c%d}", tagname[0], !tag_close);
  152. } else {
  153. unknown = 1;
  154. snprintf(tmp, sizeof(tmp), "</%s>", tagname);
  155. }
  156. if (tag_close) {
  157. sptr--;
  158. } else if (unknown && !strstr(in, tmp)) {
  159. in -= len + tag_close;
  160. av_bprint_chars(dst, *in, 1);
  161. } else
  162. av_strlcpy(stack[sptr++].tag, tagname,
  163. sizeof(stack[0].tag));
  164. break;
  165. }
  166. }
  167. default:
  168. av_bprint_chars(dst, *in, 1);
  169. break;
  170. }
  171. if (*in != ' ' && *in != '\r' && *in != '\n')
  172. line_start = 0;
  173. }
  174. if (!av_bprint_is_complete(dst))
  175. return AVERROR(ENOMEM);
  176. while (dst->len >= 2 && !strncmp(&dst->str[dst->len - 2], "\\N", 2))
  177. dst->len -= 2;
  178. dst->str[dst->len] = 0;
  179. rstrip_spaces_buf(dst);
  180. return 0;
  181. }