2 * SubRip subtitle encoder
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/avstring.h"
25 #include "ass_split.h"
29 #define SRT_STACK_SIZE 64
32 AVCodecContext *avctx;
33 ASSSplitContext *ass_ctx;
39 char stack[SRT_STACK_SIZE];
41 int alignment_applied;
46 __attribute__ ((__format__ (__printf__, 2, 3)))
48 static void srt_print(SRTContext *s, const char *str, ...)
52 s->ptr += vsnprintf(s->ptr, s->end - s->ptr, str, vargs);
56 static int srt_stack_push(SRTContext *s, const char c)
58 if (s->stack_ptr >= SRT_STACK_SIZE)
60 s->stack[s->stack_ptr++] = c;
64 static char srt_stack_pop(SRTContext *s)
66 if (s->stack_ptr <= 0)
68 return s->stack[--s->stack_ptr];
71 static int srt_stack_find(SRTContext *s, const char c)
74 for (i = s->stack_ptr-1; i >= 0; i--)
80 static void srt_close_tag(SRTContext *s, char tag)
82 srt_print(s, "</%c%s>", tag, tag == 'f' ? "ont" : "");
85 static void srt_stack_push_pop(SRTContext *s, const char c, int close)
88 int i = c ? srt_stack_find(s, c) : 0;
91 while (s->stack_ptr != i)
92 srt_close_tag(s, srt_stack_pop(s));
93 } else if (srt_stack_push(s, c) < 0)
94 av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
97 static void srt_style_apply(SRTContext *s, const char *style)
99 ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
101 int c = st->primary_color & 0xFFFFFF;
102 if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT) ||
103 st->font_size != ASS_DEFAULT_FONT_SIZE ||
104 c != ASS_DEFAULT_COLOR) {
105 srt_print(s, "<font");
106 if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT))
107 srt_print(s, " face=\"%s\"", st->font_name);
108 if (st->font_size != ASS_DEFAULT_FONT_SIZE)
109 srt_print(s, " size=\"%d\"", st->font_size);
110 if (c != ASS_DEFAULT_COLOR)
111 srt_print(s, " color=\"#%06x\"",
112 (c & 0xFF0000) >> 16 | c & 0xFF00 | (c & 0xFF) << 16);
114 srt_stack_push(s, 'f');
116 if (st->bold != ASS_DEFAULT_BOLD) {
118 srt_stack_push(s, 'b');
120 if (st->italic != ASS_DEFAULT_ITALIC) {
122 srt_stack_push(s, 'i');
124 if (st->underline != ASS_DEFAULT_UNDERLINE) {
126 srt_stack_push(s, 'u');
128 if (st->alignment != ASS_DEFAULT_ALIGNMENT) {
129 srt_print(s, "{\\an%d}", st->alignment);
130 s->alignment_applied = 1;
136 static av_cold int srt_encode_init(AVCodecContext *avctx)
138 SRTContext *s = avctx->priv_data;
140 s->ass_ctx = ff_ass_split(avctx->subtitle_header);
141 return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
144 static void srt_text_cb(void *priv, const char *text, int len)
146 SRTContext *s = priv;
147 av_strlcpy(s->ptr, text, FFMIN(s->end-s->ptr, len+1));
151 static void srt_new_line_cb(void *priv, int forced)
153 srt_print(priv, "\r\n");
156 static void srt_style_cb(void *priv, char style, int close)
158 srt_stack_push_pop(priv, style, close);
160 srt_print(priv, "<%c>", style);
163 static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
167 srt_stack_push_pop(priv, 'f', color == 0xFFFFFFFF);
168 if (color != 0xFFFFFFFF)
169 srt_print(priv, "<font color=\"#%06x\">",
170 (color & 0xFF0000) >> 16 | color & 0xFF00 | (color & 0xFF) << 16);
173 static void srt_font_name_cb(void *priv, const char *name)
175 srt_stack_push_pop(priv, 'f', !name);
177 srt_print(priv, "<font face=\"%s\">", name);
180 static void srt_font_size_cb(void *priv, int size)
182 srt_stack_push_pop(priv, 'f', size < 0);
184 srt_print(priv, "<font size=\"%d\">", size);
187 static void srt_alignment_cb(void *priv, int alignment)
189 SRTContext *s = priv;
190 if (!s->alignment_applied && alignment >= 0) {
191 srt_print(s, "{\\an%d}", alignment);
192 s->alignment_applied = 1;
196 static void srt_cancel_overrides_cb(void *priv, const char *style)
198 srt_stack_push_pop(priv, 0, 1);
199 srt_style_apply(priv, style);
202 static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2,
205 SRTContext *s = priv;
207 int len = snprintf(buffer, sizeof(buffer),
208 " X1:%03u X2:%03u Y1:%03u Y2:%03u", x1, x2, y1, y2);
209 if (s->end - s->ptr > len) {
210 memmove(s->dialog_start+len, s->dialog_start, s->ptr-s->dialog_start+1);
211 memcpy(s->dialog_start, buffer, len);
216 static void srt_end_cb(void *priv)
218 srt_stack_push_pop(priv, 0, 1);
219 srt_print(priv, "\r\n\r\n");
222 static const ASSCodesCallbacks srt_callbacks = {
224 .new_line = srt_new_line_cb,
225 .style = srt_style_cb,
226 .color = srt_color_cb,
227 .font_name = srt_font_name_cb,
228 .font_size = srt_font_size_cb,
229 .alignment = srt_alignment_cb,
230 .cancel_overrides = srt_cancel_overrides_cb,
235 static int srt_encode_frame(AVCodecContext *avctx,
236 unsigned char *buf, int bufsize, void *data)
238 SRTContext *s = avctx->priv_data;
239 AVSubtitle *sub = data;
244 s->end = s->ptr + sizeof(s->buffer);
246 for (i=0; i<sub->num_rects; i++) {
248 if (sub->rects[i]->type != SUBTITLE_ASS) {
249 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
250 return AVERROR(ENOSYS);
253 dialog = ff_ass_split_dialog(s->ass_ctx, sub->rects[i]->ass, 0, &num);
254 for (; dialog && num--; dialog++) {
255 if (avctx->codec->id == CODEC_ID_SRT) {
256 int sh, sm, ss, sc = 10 * dialog->start;
257 int eh, em, es, ec = 10 * dialog->end;
258 sh = sc/3600000; sc -= 3600000*sh;
259 sm = sc/ 60000; sc -= 60000*sm;
260 ss = sc/ 1000; sc -= 1000*ss;
261 eh = ec/3600000; ec -= 3600000*eh;
262 em = ec/ 60000; ec -= 60000*em;
263 es = ec/ 1000; ec -= 1000*es;
264 srt_print(s,"%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n",
265 ++s->count, sh, sm, ss, sc, eh, em, es, ec);
267 s->alignment_applied = 0;
268 s->dialog_start = s->ptr - 2;
269 srt_style_apply(s, dialog->style);
270 ff_ass_split_override_codes(&srt_callbacks, s, dialog->text);
274 if (s->ptr == s->buffer)
277 len = av_strlcpy(buf, s->buffer, bufsize);
279 if (len > bufsize-1) {
280 av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
287 static int srt_encode_close(AVCodecContext *avctx)
289 SRTContext *s = avctx->priv_data;
290 ff_ass_split_free(s->ass_ctx);
294 #if CONFIG_SRT_ENCODER
295 AVCodec ff_srt_encoder = {
297 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle with embedded timing"),
298 .type = AVMEDIA_TYPE_SUBTITLE,
299 .id = AV_CODEC_ID_SRT,
300 .priv_data_size = sizeof(SRTContext),
301 .init = srt_encode_init,
302 .encode = srt_encode_frame,
303 .close = srt_encode_close,
307 #if CONFIG_SUBRIP_ENCODER
308 AVCodec ff_subrip_encoder = {
310 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
311 .type = AVMEDIA_TYPE_SUBTITLE,
312 .id = AV_CODEC_ID_SUBRIP,
313 .priv_data_size = sizeof(SRTContext),
314 .init = srt_encode_init,
315 .encode = srt_encode_frame,
316 .close = srt_encode_close,