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.

262 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/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 SrtStack {
  42. char tag[128];
  43. char param[PARAM_NUMBER][128];
  44. } SrtStack;
  45. static void rstrip_spaces_buf(AVBPrint *buf)
  46. {
  47. while (buf->len > 0 && buf->str[buf->len - 1] == ' ')
  48. buf->str[--buf->len] = 0;
  49. }
  50. static void srt_to_ass(AVCodecContext *avctx, AVBPrint *dst,
  51. const char *in, int x1, int y1, int x2, int y2)
  52. {
  53. char *param, buffer[128], tmp[128];
  54. int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
  55. SrtStack stack[16];
  56. stack[0].tag[0] = 0;
  57. strcpy(stack[0].param[PARAM_SIZE], "{\\fs}");
  58. strcpy(stack[0].param[PARAM_COLOR], "{\\c}");
  59. strcpy(stack[0].param[PARAM_FACE], "{\\fn}");
  60. if (x1 >= 0 && y1 >= 0) {
  61. /* XXX: here we rescale coordinate assuming they are in DVD resolution
  62. * (720x480) since we don't have anything better */
  63. if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1) && x2 >= x1 && y2 >= y1) {
  64. /* text rectangle defined, write the text at the center of the rectangle */
  65. const int cx = x1 + (x2 - x1)/2;
  66. const int cy = y1 + (y2 - y1)/2;
  67. const int scaled_x = cx * ASS_DEFAULT_PLAYRESX / 720;
  68. const int scaled_y = cy * ASS_DEFAULT_PLAYRESY / 480;
  69. av_bprintf(dst, "{\\an5}{\\pos(%d,%d)}", scaled_x, scaled_y);
  70. } else {
  71. /* only the top left corner, assume the text starts in that corner */
  72. const int scaled_x = x1 * ASS_DEFAULT_PLAYRESX / 720;
  73. const int scaled_y = y1 * ASS_DEFAULT_PLAYRESY / 480;
  74. av_bprintf(dst, "{\\an1}{\\pos(%d,%d)}", scaled_x, scaled_y);
  75. }
  76. }
  77. for (; !end && *in; in++) {
  78. switch (*in) {
  79. case '\r':
  80. break;
  81. case '\n':
  82. if (line_start) {
  83. end = 1;
  84. break;
  85. }
  86. rstrip_spaces_buf(dst);
  87. av_bprintf(dst, "\\N");
  88. line_start = 1;
  89. break;
  90. case ' ':
  91. if (!line_start)
  92. av_bprint_chars(dst, *in, 1);
  93. break;
  94. case '{': /* skip all {\xxx} substrings except for {\an%d}
  95. and all microdvd like styles such as {Y:xxx} */
  96. len = 0;
  97. an += sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0;
  98. if ((an != 1 && (len = 0, sscanf(in, "{\\%*[^}]}%n", &len) >= 0 && len > 0)) ||
  99. (len = 0, sscanf(in, "{%*1[CcFfoPSsYy]:%*[^}]}%n", &len) >= 0 && len > 0)) {
  100. in += len - 1;
  101. } else
  102. av_bprint_chars(dst, *in, 1);
  103. break;
  104. case '<':
  105. tag_close = in[1] == '/';
  106. len = 0;
  107. if (sscanf(in+tag_close+1, "%127[^>]>%n", buffer, &len) >= 1 && len > 0) {
  108. if ((param = strchr(buffer, ' ')))
  109. *param++ = 0;
  110. if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
  111. ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, buffer))) {
  112. int i, j, unknown = 0;
  113. in += len + tag_close;
  114. if (!tag_close)
  115. memset(stack+sptr, 0, sizeof(*stack));
  116. if (!strcmp(buffer, "font")) {
  117. if (tag_close) {
  118. for (i=PARAM_NUMBER-1; i>=0; i--)
  119. if (stack[sptr-1].param[i][0])
  120. for (j=sptr-2; j>=0; j--)
  121. if (stack[j].param[i][0]) {
  122. av_bprintf(dst, "%s", stack[j].param[i]);
  123. break;
  124. }
  125. } else {
  126. while (param) {
  127. if (!strncmp(param, "size=", 5)) {
  128. unsigned font_size;
  129. param += 5 + (param[5] == '"');
  130. if (sscanf(param, "%u", &font_size) == 1) {
  131. snprintf(stack[sptr].param[PARAM_SIZE],
  132. sizeof(stack[0].param[PARAM_SIZE]),
  133. "{\\fs%u}", font_size);
  134. }
  135. } else if (!strncmp(param, "color=", 6)) {
  136. param += 6 + (param[6] == '"');
  137. snprintf(stack[sptr].param[PARAM_COLOR],
  138. sizeof(stack[0].param[PARAM_COLOR]),
  139. "{\\c&H%X&}",
  140. html_color_parse(avctx, param));
  141. } else if (!strncmp(param, "face=", 5)) {
  142. param += 5 + (param[5] == '"');
  143. len = strcspn(param,
  144. param[-1] == '"' ? "\"" :" ");
  145. av_strlcpy(tmp, param,
  146. FFMIN(sizeof(tmp), len+1));
  147. param += len;
  148. snprintf(stack[sptr].param[PARAM_FACE],
  149. sizeof(stack[0].param[PARAM_FACE]),
  150. "{\\fn%s}", tmp);
  151. }
  152. if ((param = strchr(param, ' ')))
  153. param++;
  154. }
  155. for (i=0; i<PARAM_NUMBER; i++)
  156. if (stack[sptr].param[i][0])
  157. av_bprintf(dst, "%s", stack[sptr].param[i]);
  158. }
  159. } else if (!buffer[1] && strspn(buffer, "bisu") == 1) {
  160. av_bprintf(dst, "{\\%c%d}", buffer[0], !tag_close);
  161. } else {
  162. unknown = 1;
  163. snprintf(tmp, sizeof(tmp), "</%s>", buffer);
  164. }
  165. if (tag_close) {
  166. sptr--;
  167. } else if (unknown && !strstr(in, tmp)) {
  168. in -= len + tag_close;
  169. av_bprint_chars(dst, *in, 1);
  170. } else
  171. av_strlcpy(stack[sptr++].tag, buffer,
  172. sizeof(stack[0].tag));
  173. break;
  174. }
  175. }
  176. default:
  177. av_bprint_chars(dst, *in, 1);
  178. break;
  179. }
  180. if (*in != ' ' && *in != '\r' && *in != '\n')
  181. line_start = 0;
  182. }
  183. while (dst->len >= 2 && !strncmp(&dst->str[dst->len - 2], "\\N", 2))
  184. dst->len -= 2;
  185. dst->str[dst->len] = 0;
  186. rstrip_spaces_buf(dst);
  187. }
  188. static int srt_decode_frame(AVCodecContext *avctx,
  189. void *data, int *got_sub_ptr, AVPacket *avpkt)
  190. {
  191. AVSubtitle *sub = data;
  192. AVBPrint buffer;
  193. int ts_start, ts_end, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
  194. int size, ret;
  195. const uint8_t *p = av_packet_get_side_data(avpkt, AV_PKT_DATA_SUBTITLE_POSITION, &size);
  196. if (p && size == 16) {
  197. x1 = AV_RL32(p );
  198. y1 = AV_RL32(p + 4);
  199. x2 = AV_RL32(p + 8);
  200. y2 = AV_RL32(p + 12);
  201. }
  202. if (avpkt->size <= 0)
  203. return avpkt->size;
  204. av_bprint_init(&buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
  205. // TODO: reindent
  206. // Do final divide-by-10 outside rescale to force rounding down.
  207. ts_start = av_rescale_q(avpkt->pts,
  208. avctx->time_base,
  209. (AVRational){1,100});
  210. ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
  211. avctx->time_base,
  212. (AVRational){1,100});
  213. srt_to_ass(avctx, &buffer, avpkt->data, x1, y1, x2, y2);
  214. ret = ff_ass_add_rect_bprint(sub, &buffer, ts_start, ts_end-ts_start);
  215. av_bprint_finalize(&buffer, NULL);
  216. if (ret < 0)
  217. return ret;
  218. *got_sub_ptr = sub->num_rects > 0;
  219. return avpkt->size;
  220. }
  221. #if CONFIG_SRT_DECODER
  222. /* deprecated decoder */
  223. AVCodec ff_srt_decoder = {
  224. .name = "srt",
  225. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  226. .type = AVMEDIA_TYPE_SUBTITLE,
  227. .id = AV_CODEC_ID_SUBRIP,
  228. .init = ff_ass_subtitle_header_default,
  229. .decode = srt_decode_frame,
  230. };
  231. #endif
  232. #if CONFIG_SUBRIP_DECODER
  233. AVCodec ff_subrip_decoder = {
  234. .name = "subrip",
  235. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  236. .type = AVMEDIA_TYPE_SUBTITLE,
  237. .id = AV_CODEC_ID_SUBRIP,
  238. .init = ff_ass_subtitle_header_default,
  239. .decode = srt_decode_frame,
  240. };
  241. #endif