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.

242 lines
9.2KB

  1. /*
  2. * SubRip subtitle decoder
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/parseutils.h"
  23. #include "avcodec.h"
  24. #include "ass.h"
  25. static int html_color_parse(AVCodecContext *avctx, const char *str)
  26. {
  27. uint8_t rgba[4];
  28. if (av_parse_color(rgba, str, strcspn(str, "\" >"), avctx) < 0)
  29. return -1;
  30. return rgba[0] | rgba[1] << 8 | rgba[2] << 16;
  31. }
  32. enum {
  33. PARAM_UNKNOWN = -1,
  34. PARAM_SIZE,
  35. PARAM_COLOR,
  36. PARAM_FACE,
  37. PARAM_NUMBER
  38. };
  39. typedef struct {
  40. char tag[128];
  41. char param[PARAM_NUMBER][128];
  42. } SrtStack;
  43. static const char *srt_to_ass(AVCodecContext *avctx, char *out, char *out_end,
  44. const char *in, int x1, int y1, int x2, int y2)
  45. {
  46. char c, *param, buffer[128], tmp[128];
  47. int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
  48. SrtStack stack[16];
  49. stack[0].tag[0] = 0;
  50. strcpy(stack[0].param[PARAM_SIZE], "{\\fs}");
  51. strcpy(stack[0].param[PARAM_COLOR], "{\\c}");
  52. strcpy(stack[0].param[PARAM_FACE], "{\\fn}");
  53. if (x1 >= 0 && y1 >= 0) {
  54. if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1))
  55. out += snprintf(out, out_end-out,
  56. "{\\an1}{\\move(%d,%d,%d,%d)}", x1, y1, x2, y2);
  57. else
  58. out += snprintf(out, out_end-out, "{\\an1}{\\pos(%d,%d)}", x1, y1);
  59. }
  60. for (; out < out_end && !end && *in; in++) {
  61. switch (*in) {
  62. case '\r':
  63. break;
  64. case '\n':
  65. if (line_start) {
  66. end = 1;
  67. break;
  68. }
  69. while (out[-1] == ' ')
  70. out--;
  71. out += snprintf(out, out_end-out, "\\N");
  72. line_start = 1;
  73. break;
  74. case ' ':
  75. if (!line_start)
  76. *out++ = *in;
  77. break;
  78. case '{': /* skip all {\xxx} substrings except for {\an%d}
  79. and all microdvd like styles such as {Y:xxx} */
  80. an += sscanf(in, "{\\an%*1u}%c", &c) == 1;
  81. if ((an != 1 && sscanf(in, "{\\%*[^}]}%n%c", &len, &c) > 0) ||
  82. sscanf(in, "{%*1[CcFfoPSsYy]:%*[^}]}%n%c", &len, &c) > 0) {
  83. in += len - 1;
  84. } else
  85. *out++ = *in;
  86. break;
  87. case '<':
  88. tag_close = in[1] == '/';
  89. if (sscanf(in+tag_close+1, "%127[^>]>%n%c", buffer, &len,&c) >= 2) {
  90. if ((param = strchr(buffer, ' ')))
  91. *param++ = 0;
  92. if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
  93. ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, buffer))) {
  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(buffer, "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. out += snprintf(out, out_end-out,
  105. "%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(avctx, 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. out += snprintf(out, out_end-out,
  141. "%s", stack[sptr].param[i]);
  142. }
  143. } else if (!buffer[1] && strspn(buffer, "bisu") == 1) {
  144. out += snprintf(out, out_end-out,
  145. "{\\%c%d}", buffer[0], !tag_close);
  146. } else {
  147. unknown = 1;
  148. snprintf(tmp, sizeof(tmp), "</%s>", buffer);
  149. }
  150. if (tag_close) {
  151. sptr--;
  152. } else if (unknown && !strstr(in, tmp)) {
  153. in -= len + tag_close;
  154. *out++ = *in;
  155. } else
  156. av_strlcpy(stack[sptr++].tag, buffer,
  157. sizeof(stack[0].tag));
  158. break;
  159. }
  160. }
  161. default:
  162. *out++ = *in;
  163. break;
  164. }
  165. if (*in != ' ' && *in != '\r' && *in != '\n')
  166. line_start = 0;
  167. }
  168. out = FFMIN(out, out_end-3);
  169. while (!strncmp(out-2, "\\N", 2))
  170. out -= 2;
  171. while (out[-1] == ' ')
  172. out--;
  173. out += snprintf(out, out_end-out, "\r\n");
  174. return in;
  175. }
  176. static const char *read_ts(const char *buf, int *ts_start, int *ts_end,
  177. int *x1, int *y1, int *x2, int *y2)
  178. {
  179. int i, hs, ms, ss, he, me, se;
  180. for (i=0; i<2; i++) {
  181. /* try to read timestamps in either the first or second line */
  182. int c = sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
  183. "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
  184. &hs, &ms, &ss, ts_start, &he, &me, &se, ts_end,
  185. x1, x2, y1, y2);
  186. buf += strcspn(buf, "\n") + 1;
  187. if (c >= 8) {
  188. *ts_start = 100*(ss + 60*(ms + 60*hs)) + *ts_start/10;
  189. *ts_end = 100*(se + 60*(me + 60*he)) + *ts_end /10;
  190. return buf;
  191. }
  192. }
  193. return NULL;
  194. }
  195. static int srt_decode_frame(AVCodecContext *avctx,
  196. void *data, int *got_sub_ptr, AVPacket *avpkt)
  197. {
  198. AVSubtitle *sub = data;
  199. int ts_start, ts_end, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
  200. char buffer[2048];
  201. const char *ptr = avpkt->data;
  202. const char *end = avpkt->data + avpkt->size;
  203. if (avpkt->size <= 0)
  204. return avpkt->size;
  205. ff_ass_init(sub);
  206. while (ptr < end && *ptr) {
  207. ptr = read_ts(ptr, &ts_start, &ts_end, &x1, &y1, &x2, &y2);
  208. if (!ptr)
  209. break;
  210. ptr = srt_to_ass(avctx, buffer, buffer+sizeof(buffer), ptr,
  211. x1, y1, x2, y2);
  212. ff_ass_add_rect(sub, buffer, ts_start, ts_end, 0);
  213. }
  214. *got_sub_ptr = sub->num_rects > 0;
  215. return avpkt->size;
  216. }
  217. AVCodec ff_srt_decoder = {
  218. .name = "srt",
  219. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  220. .type = AVMEDIA_TYPE_SUBTITLE,
  221. .id = CODEC_ID_SRT,
  222. .init = ff_ass_subtitle_header_default,
  223. .decode = srt_decode_frame,
  224. };