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.

270 lines
10KB

  1. /*
  2. * SubRip subtitle decoder
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 {
  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. snprintf(out, out_end-out,
  57. "{\\an1}{\\move(%d,%d,%d,%d)}", x1, y1, x2, y2);
  58. else
  59. snprintf(out, out_end-out, "{\\an1}{\\pos(%d,%d)}", x1, y1);
  60. out += strlen(out);
  61. }
  62. for (; out < out_end && !end && *in; in++) {
  63. switch (*in) {
  64. case '\r':
  65. break;
  66. case '\n':
  67. if (line_start) {
  68. end = 1;
  69. break;
  70. }
  71. while (out[-1] == ' ')
  72. out--;
  73. snprintf(out, out_end-out, "\\N");
  74. if(out<out_end) out += strlen(out);
  75. line_start = 1;
  76. break;
  77. case ' ':
  78. if (!line_start)
  79. *out++ = *in;
  80. break;
  81. case '{': /* skip all {\xxx} substrings except for {\an%d}
  82. and all microdvd like styles such as {Y:xxx} */
  83. an += sscanf(in, "{\\an%*1u}%c", &c) == 1;
  84. if ((an != 1 && sscanf(in, "{\\%*[^}]}%n%c", &len, &c) > 0) ||
  85. sscanf(in, "{%*1[CcFfoPSsYy]:%*[^}]}%n%c", &len, &c) > 0) {
  86. in += len - 1;
  87. } else
  88. *out++ = *in;
  89. break;
  90. case '<':
  91. tag_close = in[1] == '/';
  92. if (sscanf(in+tag_close+1, "%127[^>]>%n%c", buffer, &len,&c) >= 2) {
  93. if ((param = strchr(buffer, ' ')))
  94. *param++ = 0;
  95. if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
  96. ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, buffer))) {
  97. int i, j, unknown = 0;
  98. in += len + tag_close;
  99. if (!tag_close)
  100. memset(stack+sptr, 0, sizeof(*stack));
  101. if (!strcmp(buffer, "font")) {
  102. if (tag_close) {
  103. for (i=PARAM_NUMBER-1; i>=0; i--)
  104. if (stack[sptr-1].param[i][0])
  105. for (j=sptr-2; j>=0; j--)
  106. if (stack[j].param[i][0]) {
  107. snprintf(out, out_end-out,
  108. "%s", stack[j].param[i]);
  109. if(out<out_end) out += strlen(out);
  110. break;
  111. }
  112. } else {
  113. while (param) {
  114. if (!strncmp(param, "size=", 5)) {
  115. unsigned font_size;
  116. param += 5 + (param[5] == '"');
  117. if (sscanf(param, "%u", &font_size) == 1) {
  118. snprintf(stack[sptr].param[PARAM_SIZE],
  119. sizeof(stack[0].param[PARAM_SIZE]),
  120. "{\\fs%u}", font_size);
  121. }
  122. } else if (!strncmp(param, "color=", 6)) {
  123. param += 6 + (param[6] == '"');
  124. snprintf(stack[sptr].param[PARAM_COLOR],
  125. sizeof(stack[0].param[PARAM_COLOR]),
  126. "{\\c&H%X&}",
  127. html_color_parse(avctx, param));
  128. } else if (!strncmp(param, "face=", 5)) {
  129. param += 5 + (param[5] == '"');
  130. len = strcspn(param,
  131. param[-1] == '"' ? "\"" :" ");
  132. av_strlcpy(tmp, param,
  133. FFMIN(sizeof(tmp), len+1));
  134. param += len;
  135. snprintf(stack[sptr].param[PARAM_FACE],
  136. sizeof(stack[0].param[PARAM_FACE]),
  137. "{\\fn%s}", tmp);
  138. }
  139. if ((param = strchr(param, ' ')))
  140. param++;
  141. }
  142. for (i=0; i<PARAM_NUMBER; i++)
  143. if (stack[sptr].param[i][0]) {
  144. snprintf(out, out_end-out,
  145. "%s", stack[sptr].param[i]);
  146. if(out<out_end) out += strlen(out);
  147. }
  148. }
  149. } else if (!buffer[1] && strspn(buffer, "bisu") == 1) {
  150. snprintf(out, out_end-out,
  151. "{\\%c%d}", buffer[0], !tag_close);
  152. if(out<out_end) out += strlen(out);
  153. } else {
  154. unknown = 1;
  155. snprintf(tmp, sizeof(tmp), "</%s>", buffer);
  156. }
  157. if (tag_close) {
  158. sptr--;
  159. } else if (unknown && !strstr(in, tmp)) {
  160. in -= len + tag_close;
  161. *out++ = *in;
  162. } else
  163. av_strlcpy(stack[sptr++].tag, buffer,
  164. sizeof(stack[0].tag));
  165. break;
  166. }
  167. }
  168. default:
  169. *out++ = *in;
  170. break;
  171. }
  172. if (*in != ' ' && *in != '\r' && *in != '\n')
  173. line_start = 0;
  174. }
  175. out = FFMIN(out, out_end-3);
  176. while (!strncmp(out-2, "\\N", 2))
  177. out -= 2;
  178. while (out[-1] == ' ')
  179. out--;
  180. snprintf(out, out_end-out, "\r\n");
  181. return in;
  182. }
  183. static const char *read_ts(const char *buf, int *ts_start, int *ts_end,
  184. int *x1, int *y1, int *x2, int *y2)
  185. {
  186. int i, hs, ms, ss, he, me, se;
  187. for (i=0; i<2; i++) {
  188. /* try to read timestamps in either the first or second line */
  189. int c = sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
  190. "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
  191. &hs, &ms, &ss, ts_start, &he, &me, &se, ts_end,
  192. x1, x2, y1, y2);
  193. buf += strcspn(buf, "\n") + 1;
  194. if (c >= 8) {
  195. *ts_start = 100*(ss + 60*(ms + 60*hs)) + *ts_start/10;
  196. *ts_end = 100*(se + 60*(me + 60*he)) + *ts_end /10;
  197. return buf;
  198. }
  199. }
  200. return NULL;
  201. }
  202. static int srt_decode_frame(AVCodecContext *avctx,
  203. void *data, int *got_sub_ptr, AVPacket *avpkt)
  204. {
  205. AVSubtitle *sub = data;
  206. int ts_start, ts_end, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
  207. char buffer[2048];
  208. const char *ptr = avpkt->data;
  209. const char *end = avpkt->data + avpkt->size;
  210. if (avpkt->size <= 0)
  211. return avpkt->size;
  212. while (ptr < end && *ptr) {
  213. if (avctx->codec->id == CODEC_ID_SRT) {
  214. ptr = read_ts(ptr, &ts_start, &ts_end, &x1, &y1, &x2, &y2);
  215. if (!ptr)
  216. break;
  217. } else {
  218. // Do final divide-by-10 outside rescale to force rounding down.
  219. ts_start = av_rescale_q(avpkt->pts,
  220. avctx->time_base,
  221. (AVRational){1,100});
  222. ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
  223. avctx->time_base,
  224. (AVRational){1,100});
  225. }
  226. ptr = srt_to_ass(avctx, buffer, buffer+sizeof(buffer), ptr,
  227. x1, y1, x2, y2);
  228. ff_ass_add_rect(sub, buffer, ts_start, ts_end-ts_start, 0);
  229. }
  230. *got_sub_ptr = sub->num_rects > 0;
  231. return avpkt->size;
  232. }
  233. #if CONFIG_SRT_DECODER
  234. AVCodec ff_srt_decoder = {
  235. .name = "srt",
  236. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle with embedded timing"),
  237. .type = AVMEDIA_TYPE_SUBTITLE,
  238. .id = AV_CODEC_ID_SRT,
  239. .init = ff_ass_subtitle_header_default,
  240. .decode = srt_decode_frame,
  241. };
  242. #endif
  243. #if CONFIG_SUBRIP_DECODER
  244. AVCodec ff_subrip_decoder = {
  245. .name = "subrip",
  246. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  247. .type = AVMEDIA_TYPE_SUBTITLE,
  248. .id = AV_CODEC_ID_SUBRIP,
  249. .init = ff_ass_subtitle_header_default,
  250. .decode = srt_decode_frame,
  251. };
  252. #endif