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.

179 lines
7.2KB

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