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.

281 lines
11KB

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