2 * SSA/ASS common funtions
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"
26 int ff_ass_subtitle_header(AVCodecContext *avctx,
27 const char *font, int font_size,
28 int color, int back_color,
29 int bold, int italic, int underline,
32 avctx->subtitle_header = av_asprintf(
34 "ScriptType: v4.00+\r\n"
37 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
38 "Style: Default,%s,%d,&H%x,&H%x,&H%x,&H%x,%d,%d,%d,1,1,0,%d,10,10,10,0,0\r\n"
41 "Format: Layer, Start, End, Text\r\n",
42 font, font_size, color, color, back_color, back_color,
43 -bold, -italic, -underline, alignment);
45 if (!avctx->subtitle_header)
46 return AVERROR(ENOMEM);
47 avctx->subtitle_header_size = strlen(avctx->subtitle_header);
51 int ff_ass_subtitle_header_default(AVCodecContext *avctx)
53 return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
54 ASS_DEFAULT_FONT_SIZE,
56 ASS_DEFAULT_BACK_COLOR,
59 ASS_DEFAULT_UNDERLINE,
60 ASS_DEFAULT_ALIGNMENT);
63 static int ts_to_string(char *str, int strlen, int ts)
66 h = ts/360000; ts -= 360000*h;
67 m = ts/ 6000; ts -= 6000*m;
68 s = ts/ 100; ts -= 100*s;
69 return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts);
72 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
73 int ts_start, int ts_end, int raw)
75 int len = 0, dlen, duration = ts_end - ts_start;
76 char s_start[16], s_end[16], header[48] = {0};
77 AVSubtitleRect **rects;
80 ts_to_string(s_start, sizeof(s_start), ts_start);
81 ts_to_string(s_end, sizeof(s_end), ts_end );
82 len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,",
86 dlen = strcspn(dialog, "\n");
87 dlen += dialog[dlen] == '\n';
89 rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
91 return AVERROR(ENOMEM);
93 sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
94 rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
95 rects[sub->num_rects]->type = SUBTITLE_ASS;
96 rects[sub->num_rects]->ass = av_malloc(len + dlen + 1);
97 strcpy (rects[sub->num_rects]->ass , header);
98 av_strlcpy(rects[sub->num_rects]->ass + len, dialog, dlen + 1);