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.

345 lines
9.8KB

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