2 * SSA/ASS common functions
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/avassert.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/bprint.h"
27 #include "libavutil/common.h"
29 int ff_ass_subtitle_header(AVCodecContext *avctx,
30 const char *font, int font_size,
31 int color, int back_color,
32 int bold, int italic, int underline,
35 avctx->subtitle_header = av_asprintf(
37 "ScriptType: v4.00+\r\n"
40 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
41 "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"
44 "Format: Layer, Start, End, Style, Text\r\n",
45 font, font_size, color, color, back_color, back_color,
46 -bold, -italic, -underline, alignment);
48 if (!avctx->subtitle_header)
49 return AVERROR(ENOMEM);
50 avctx->subtitle_header_size = strlen(avctx->subtitle_header);
54 int ff_ass_subtitle_header_default(AVCodecContext *avctx)
56 return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
57 ASS_DEFAULT_FONT_SIZE,
59 ASS_DEFAULT_BACK_COLOR,
62 ASS_DEFAULT_UNDERLINE,
63 ASS_DEFAULT_ALIGNMENT);
66 static void insert_ts(AVBPrint *buf, int ts)
69 av_bprintf(buf, "9:59:59.99,");
73 h = ts/360000; ts -= 360000*h;
74 m = ts/ 6000; ts -= 6000*m;
75 s = ts/ 100; ts -= 100*s;
76 av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
80 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
81 int ts_start, int duration, int raw)
85 AVSubtitleRect **rects;
87 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
88 if (!raw || raw == 2) {
93 dialog = strchr(dialog, ',');
95 return AVERROR_INVALIDDATA;
98 /* extract Layer or Marked */
99 layer = strtol(dialog, (char**)&dialog, 10);
101 return AVERROR_INVALIDDATA;
104 av_bprintf(&buf, "Dialogue: %ld,", layer);
105 insert_ts(&buf, ts_start);
106 insert_ts(&buf, duration == -1 ? -1 : ts_start + duration);
108 av_bprintf(&buf, "Default,");
111 dlen = strcspn(dialog, "\n");
112 dlen += dialog[dlen] == '\n';
114 av_bprintf(&buf, "%.*s", dlen, dialog);
116 av_bprintf(&buf, "\r\n");
117 if (!av_bprint_is_complete(&buf))
118 return AVERROR(ENOMEM);
120 rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
122 return AVERROR(ENOMEM);
124 sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
125 rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
126 rects[sub->num_rects]->type = SUBTITLE_ASS;
127 ret = av_bprint_finalize(&buf, &rects[sub->num_rects]->ass);