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.

237 lines
6.5KB

  1. /*
  2. * WebVTT subtitle encoder
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  4. * Copyright (c) 2014 Aman Gupta <ffmpeg@tmm1.net>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdarg.h>
  23. #include "avcodec.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/bprint.h"
  26. #include "ass_split.h"
  27. #include "ass.h"
  28. #define WEBVTT_STACK_SIZE 64
  29. typedef struct {
  30. AVCodecContext *avctx;
  31. ASSSplitContext *ass_ctx;
  32. AVBPrint buffer;
  33. unsigned timestamp_end;
  34. int count;
  35. char stack[WEBVTT_STACK_SIZE];
  36. int stack_ptr;
  37. } WebVTTContext;
  38. #ifdef __GNUC__
  39. __attribute__ ((__format__ (__printf__, 2, 3)))
  40. #endif
  41. static void webvtt_print(WebVTTContext *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 webvtt_stack_push(WebVTTContext *s, const char c)
  49. {
  50. if (s->stack_ptr >= WEBVTT_STACK_SIZE)
  51. return -1;
  52. s->stack[s->stack_ptr++] = c;
  53. return 0;
  54. }
  55. static char webvtt_stack_pop(WebVTTContext *s)
  56. {
  57. if (s->stack_ptr <= 0)
  58. return 0;
  59. return s->stack[--s->stack_ptr];
  60. }
  61. static int webvtt_stack_find(WebVTTContext *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 webvtt_close_tag(WebVTTContext *s, char tag)
  70. {
  71. webvtt_print(s, "</%c>", tag);
  72. }
  73. static void webvtt_stack_push_pop(WebVTTContext *s, const char c, int close)
  74. {
  75. if (close) {
  76. int i = c ? webvtt_stack_find(s, c) : 0;
  77. if (i < 0)
  78. return;
  79. while (s->stack_ptr != i)
  80. webvtt_close_tag(s, webvtt_stack_pop(s));
  81. } else if (webvtt_stack_push(s, c) < 0)
  82. av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
  83. }
  84. static void webvtt_style_apply(WebVTTContext *s, const char *style)
  85. {
  86. ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
  87. if (st) {
  88. if (st->bold != ASS_DEFAULT_BOLD) {
  89. webvtt_print(s, "<b>");
  90. webvtt_stack_push(s, 'b');
  91. }
  92. if (st->italic != ASS_DEFAULT_ITALIC) {
  93. webvtt_print(s, "<i>");
  94. webvtt_stack_push(s, 'i');
  95. }
  96. if (st->underline != ASS_DEFAULT_UNDERLINE) {
  97. webvtt_print(s, "<u>");
  98. webvtt_stack_push(s, 'u');
  99. }
  100. }
  101. }
  102. static void webvtt_text_cb(void *priv, const char *text, int len)
  103. {
  104. WebVTTContext *s = priv;
  105. av_bprint_append_data(&s->buffer, text, len);
  106. }
  107. static void webvtt_new_line_cb(void *priv, int forced)
  108. {
  109. webvtt_print(priv, "\n");
  110. }
  111. static void webvtt_style_cb(void *priv, char style, int close)
  112. {
  113. if (style == 's') // strikethrough unsupported
  114. return;
  115. webvtt_stack_push_pop(priv, style, close);
  116. if (!close)
  117. webvtt_print(priv, "<%c>", style);
  118. }
  119. static void webvtt_cancel_overrides_cb(void *priv, const char *style)
  120. {
  121. webvtt_stack_push_pop(priv, 0, 1);
  122. webvtt_style_apply(priv, style);
  123. }
  124. static void webvtt_end_cb(void *priv)
  125. {
  126. webvtt_stack_push_pop(priv, 0, 1);
  127. }
  128. static const ASSCodesCallbacks webvtt_callbacks = {
  129. .text = webvtt_text_cb,
  130. .new_line = webvtt_new_line_cb,
  131. .style = webvtt_style_cb,
  132. .color = NULL,
  133. .font_name = NULL,
  134. .font_size = NULL,
  135. .alignment = NULL,
  136. .cancel_overrides = webvtt_cancel_overrides_cb,
  137. .move = NULL,
  138. .end = webvtt_end_cb,
  139. };
  140. static int webvtt_encode_frame(AVCodecContext *avctx,
  141. unsigned char *buf, int bufsize, const AVSubtitle *sub)
  142. {
  143. WebVTTContext *s = avctx->priv_data;
  144. ASSDialog *dialog;
  145. int i;
  146. av_bprint_clear(&s->buffer);
  147. for (i=0; i<sub->num_rects; i++) {
  148. const char *ass = sub->rects[i]->ass;
  149. if (sub->rects[i]->type != SUBTITLE_ASS) {
  150. av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
  151. return AVERROR(ENOSYS);
  152. }
  153. #if FF_API_ASS_TIMING
  154. if (!strncmp(ass, "Dialogue: ", 10)) {
  155. int num;
  156. dialog = ff_ass_split_dialog(s->ass_ctx, ass, 0, &num);
  157. // TODO reindent
  158. for (; dialog && num--; dialog++) {
  159. webvtt_style_apply(s, dialog->style);
  160. ff_ass_split_override_codes(&webvtt_callbacks, s, dialog->text);
  161. }
  162. } else {
  163. #endif
  164. dialog = ff_ass_split_dialog2(s->ass_ctx, ass);
  165. if (!dialog)
  166. return AVERROR(ENOMEM);
  167. webvtt_style_apply(s, dialog->style);
  168. ff_ass_split_override_codes(&webvtt_callbacks, s, dialog->text);
  169. ff_ass_free_dialog(&dialog);
  170. #if FF_API_ASS_TIMING
  171. }
  172. #endif
  173. }
  174. if (!av_bprint_is_complete(&s->buffer))
  175. return AVERROR(ENOMEM);
  176. if (!s->buffer.len)
  177. return 0;
  178. if (s->buffer.len > bufsize) {
  179. av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
  180. return -1;
  181. }
  182. memcpy(buf, s->buffer.str, s->buffer.len);
  183. return s->buffer.len;
  184. }
  185. static int webvtt_encode_close(AVCodecContext *avctx)
  186. {
  187. WebVTTContext *s = avctx->priv_data;
  188. ff_ass_split_free(s->ass_ctx);
  189. av_bprint_finalize(&s->buffer, NULL);
  190. return 0;
  191. }
  192. static av_cold int webvtt_encode_init(AVCodecContext *avctx)
  193. {
  194. WebVTTContext *s = avctx->priv_data;
  195. s->avctx = avctx;
  196. s->ass_ctx = ff_ass_split(avctx->subtitle_header);
  197. av_bprint_init(&s->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
  198. return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
  199. }
  200. AVCodec ff_webvtt_encoder = {
  201. .name = "webvtt",
  202. .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
  203. .type = AVMEDIA_TYPE_SUBTITLE,
  204. .id = AV_CODEC_ID_WEBVTT,
  205. .priv_data_size = sizeof(WebVTTContext),
  206. .init = webvtt_encode_init,
  207. .encode_sub = webvtt_encode_frame,
  208. .close = webvtt_encode_close,
  209. };