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.

333 lines
9.7KB

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