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.

319 lines
9.1KB

  1. /*
  2. * SubRip subtitle encoder
  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 <stdarg.h>
  22. #include "avcodec.h"
  23. #include "libavutil/avstring.h"
  24. #include "ass_split.h"
  25. #include "ass.h"
  26. #define SRT_STACK_SIZE 64
  27. typedef struct {
  28. AVCodecContext *avctx;
  29. ASSSplitContext *ass_ctx;
  30. char buffer[2048];
  31. char *ptr;
  32. char *end;
  33. char *dialog_start;
  34. int count;
  35. char stack[SRT_STACK_SIZE];
  36. int stack_ptr;
  37. int alignment_applied;
  38. } SRTContext;
  39. #ifdef __GNUC__
  40. __attribute__ ((__format__ (__printf__, 2, 3)))
  41. #endif
  42. static void srt_print(SRTContext *s, const char *str, ...)
  43. {
  44. va_list vargs;
  45. va_start(vargs, str);
  46. s->ptr += vsnprintf(s->ptr, s->end - s->ptr, str, vargs);
  47. va_end(vargs);
  48. }
  49. static int srt_stack_push(SRTContext *s, const char c)
  50. {
  51. if (s->stack_ptr >= SRT_STACK_SIZE)
  52. return -1;
  53. s->stack[s->stack_ptr++] = c;
  54. return 0;
  55. }
  56. static char srt_stack_pop(SRTContext *s)
  57. {
  58. if (s->stack_ptr <= 0)
  59. return 0;
  60. return s->stack[--s->stack_ptr];
  61. }
  62. static int srt_stack_find(SRTContext *s, const char c)
  63. {
  64. int i;
  65. for (i = s->stack_ptr-1; i >= 0; i--)
  66. if (s->stack[i] == c)
  67. break;
  68. return i;
  69. }
  70. static void srt_close_tag(SRTContext *s, char tag)
  71. {
  72. srt_print(s, "</%c%s>", tag, tag == 'f' ? "ont" : "");
  73. }
  74. static void srt_stack_push_pop(SRTContext *s, const char c, int close)
  75. {
  76. if (close) {
  77. int i = c ? srt_stack_find(s, c) : 0;
  78. if (i < 0)
  79. return;
  80. while (s->stack_ptr != i)
  81. srt_close_tag(s, srt_stack_pop(s));
  82. } else if (srt_stack_push(s, c) < 0)
  83. av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
  84. }
  85. static void srt_style_apply(SRTContext *s, const char *style)
  86. {
  87. ASSStyle *st = ass_style_get(s->ass_ctx, style);
  88. if (st) {
  89. int c = st->primary_color & 0xFFFFFF;
  90. if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT) ||
  91. st->font_size != ASS_DEFAULT_FONT_SIZE ||
  92. c != ASS_DEFAULT_COLOR) {
  93. srt_print(s, "<font");
  94. if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT))
  95. srt_print(s, " face=\"%s\"", st->font_name);
  96. if (st->font_size != ASS_DEFAULT_FONT_SIZE)
  97. srt_print(s, " size=\"%d\"", st->font_size);
  98. if (c != ASS_DEFAULT_COLOR)
  99. srt_print(s, " color=\"#%06x\"",
  100. (c & 0xFF0000) >> 16 | c & 0xFF00 | (c & 0xFF) << 16);
  101. srt_print(s, ">");
  102. srt_stack_push(s, 'f');
  103. }
  104. if (st->bold != ASS_DEFAULT_BOLD) {
  105. srt_print(s, "<b>");
  106. srt_stack_push(s, 'b');
  107. }
  108. if (st->italic != ASS_DEFAULT_ITALIC) {
  109. srt_print(s, "<i>");
  110. srt_stack_push(s, 'i');
  111. }
  112. if (st->underline != ASS_DEFAULT_UNDERLINE) {
  113. srt_print(s, "<u>");
  114. srt_stack_push(s, 'u');
  115. }
  116. if (st->alignment != ASS_DEFAULT_ALIGNMENT) {
  117. srt_print(s, "{\\an%d}", st->alignment);
  118. s->alignment_applied = 1;
  119. }
  120. }
  121. }
  122. static av_cold int srt_encode_init(AVCodecContext *avctx)
  123. {
  124. SRTContext *s = avctx->priv_data;
  125. s->avctx = avctx;
  126. s->ass_ctx = ff_ass_split(avctx->subtitle_header);
  127. return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
  128. }
  129. static void srt_text_cb(void *priv, const char *text, int len)
  130. {
  131. SRTContext *s = priv;
  132. av_strlcpy(s->ptr, text, FFMIN(s->end-s->ptr, len+1));
  133. s->ptr += len;
  134. }
  135. static void srt_new_line_cb(void *priv, int forced)
  136. {
  137. srt_print(priv, "\r\n");
  138. }
  139. static void srt_style_cb(void *priv, char style, int close)
  140. {
  141. srt_stack_push_pop(priv, style, close);
  142. if (!close)
  143. srt_print(priv, "<%c>", style);
  144. }
  145. static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
  146. {
  147. if (color_id > 1)
  148. return;
  149. srt_stack_push_pop(priv, 'f', color == 0xFFFFFFFF);
  150. if (color != 0xFFFFFFFF)
  151. srt_print(priv, "<font color=\"#%06x\">",
  152. (color & 0xFF0000) >> 16 | color & 0xFF00 | (color & 0xFF) << 16);
  153. }
  154. static void srt_font_name_cb(void *priv, const char *name)
  155. {
  156. srt_stack_push_pop(priv, 'f', !name);
  157. if (name)
  158. srt_print(priv, "<font face=\"%s\">", name);
  159. }
  160. static void srt_font_size_cb(void *priv, int size)
  161. {
  162. srt_stack_push_pop(priv, 'f', size < 0);
  163. if (size >= 0)
  164. srt_print(priv, "<font size=\"%d\">", size);
  165. }
  166. static void srt_alignment_cb(void *priv, int alignment)
  167. {
  168. SRTContext *s = priv;
  169. if (!s->alignment_applied && alignment >= 0) {
  170. srt_print(s, "{\\an%d}", alignment);
  171. s->alignment_applied = 1;
  172. }
  173. }
  174. static void srt_cancel_overrides_cb(void *priv, const char *style)
  175. {
  176. srt_stack_push_pop(priv, 0, 1);
  177. srt_style_apply(priv, style);
  178. }
  179. static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2,
  180. int t1, int t2)
  181. {
  182. SRTContext *s = priv;
  183. char buffer[32];
  184. int len = snprintf(buffer, sizeof(buffer),
  185. " X1:%03u X2:%03u Y1:%03u Y2:%03u", x1, x2, y1, y2);
  186. if (s->end - s->ptr > len) {
  187. memmove(s->dialog_start+len, s->dialog_start, s->ptr-s->dialog_start+1);
  188. memcpy(s->dialog_start, buffer, len);
  189. s->ptr += len;
  190. }
  191. }
  192. static void srt_end_cb(void *priv)
  193. {
  194. srt_stack_push_pop(priv, 0, 1);
  195. srt_print(priv, "\r\n\r\n");
  196. }
  197. static const ASSCodesCallbacks srt_callbacks = {
  198. .text = srt_text_cb,
  199. .new_line = srt_new_line_cb,
  200. .style = srt_style_cb,
  201. .color = srt_color_cb,
  202. .font_name = srt_font_name_cb,
  203. .font_size = srt_font_size_cb,
  204. .alignment = srt_alignment_cb,
  205. .cancel_overrides = srt_cancel_overrides_cb,
  206. .move = srt_move_cb,
  207. .end = srt_end_cb,
  208. };
  209. static int srt_encode_frame(AVCodecContext *avctx,
  210. unsigned char *buf, int bufsize, void *data)
  211. {
  212. SRTContext *s = avctx->priv_data;
  213. AVSubtitle *sub = data;
  214. ASSDialog *dialog;
  215. int i, len, num;
  216. s->ptr = s->buffer;
  217. s->end = s->ptr + sizeof(s->buffer);
  218. for (i=0; i<sub->num_rects; i++) {
  219. if (sub->rects[i]->type != SUBTITLE_ASS) {
  220. av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
  221. return AVERROR(ENOSYS);
  222. }
  223. dialog = ff_ass_split_dialog(s->ass_ctx, sub->rects[i]->ass, 0, &num);
  224. for (; dialog && num--; dialog++) {
  225. if (avctx->codec->id == CODEC_ID_SRT) {
  226. int sh, sm, ss, sc = 10 * dialog->start;
  227. int eh, em, es, ec = 10 * dialog->end;
  228. sh = sc/3600000; sc -= 3600000*sh;
  229. sm = sc/ 60000; sc -= 60000*sm;
  230. ss = sc/ 1000; sc -= 1000*ss;
  231. eh = ec/3600000; ec -= 3600000*eh;
  232. em = ec/ 60000; ec -= 60000*em;
  233. es = ec/ 1000; ec -= 1000*es;
  234. srt_print(s,"%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n",
  235. ++s->count, sh, sm, ss, sc, eh, em, es, ec);
  236. }
  237. s->alignment_applied = 0;
  238. s->dialog_start = s->ptr - 2;
  239. srt_style_apply(s, dialog->style);
  240. ff_ass_split_override_codes(&srt_callbacks, s, dialog->text);
  241. }
  242. }
  243. if (s->ptr == s->buffer)
  244. return 0;
  245. len = av_strlcpy(buf, s->buffer, bufsize);
  246. if (len > bufsize-1) {
  247. av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
  248. return -1;
  249. }
  250. return len;
  251. }
  252. static int srt_encode_close(AVCodecContext *avctx)
  253. {
  254. SRTContext *s = avctx->priv_data;
  255. ff_ass_split_free(s->ass_ctx);
  256. return 0;
  257. }
  258. #if CONFIG_SRT_ENCODER
  259. AVCodec ff_srt_encoder = {
  260. .name = "srt",
  261. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle with embedded timing"),
  262. .type = AVMEDIA_TYPE_SUBTITLE,
  263. .id = AV_CODEC_ID_SRT,
  264. .priv_data_size = sizeof(SRTContext),
  265. .init = srt_encode_init,
  266. .encode = srt_encode_frame,
  267. .close = srt_encode_close,
  268. };
  269. #endif
  270. #if CONFIG_SUBRIP_ENCODER
  271. AVCodec ff_subrip_encoder = {
  272. .name = "subrip",
  273. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  274. .type = AVMEDIA_TYPE_SUBTITLE,
  275. .id = AV_CODEC_ID_SUBRIP,
  276. .priv_data_size = sizeof(SRTContext),
  277. .init = srt_encode_init,
  278. .encode = srt_encode_frame,
  279. .close = srt_encode_close,
  280. };
  281. #endif