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.

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