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.

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