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.

185 lines
7.3KB

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