Imported Debian version 2.4.3~trusty1
[deb_ffmpeg.git] / ffmpeg / libavcodec / ass.c
CommitLineData
2ba45a60
DM
1/*
2 * SSA/ASS common functions
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
22#include "avcodec.h"
23#include "ass.h"
24#include "libavutil/avassert.h"
25#include "libavutil/avstring.h"
26#include "libavutil/bprint.h"
27#include "libavutil/common.h"
28
29int 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,
33 int alignment)
34{
35 avctx->subtitle_header = av_asprintf(
36 "[Script Info]\r\n"
37 "ScriptType: v4.00+\r\n"
38 "PlayResX: 384\r\n"
39 "PlayResY: 288\r\n"
40 "\r\n"
41 "[V4+ Styles]\r\n"
42 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
43 "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 "\r\n"
45 "[Events]\r\n"
46 "Format: Layer, Start, End, Style, Text\r\n",
47 font, font_size, color, color, back_color, back_color,
48 -bold, -italic, -underline, alignment);
49
50 if (!avctx->subtitle_header)
51 return AVERROR(ENOMEM);
52 avctx->subtitle_header_size = strlen(avctx->subtitle_header);
53 return 0;
54}
55
56int ff_ass_subtitle_header_default(AVCodecContext *avctx)
57{
58 return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
59 ASS_DEFAULT_FONT_SIZE,
60 ASS_DEFAULT_COLOR,
61 ASS_DEFAULT_BACK_COLOR,
62 ASS_DEFAULT_BOLD,
63 ASS_DEFAULT_ITALIC,
64 ASS_DEFAULT_UNDERLINE,
65 ASS_DEFAULT_ALIGNMENT);
66}
67
68static void insert_ts(AVBPrint *buf, int ts)
69{
70 if (ts == -1) {
71 av_bprintf(buf, "9:59:59.99,");
72 } else {
73 int h, m, s;
74
75 h = ts/360000; ts -= 360000*h;
76 m = ts/ 6000; ts -= 6000*m;
77 s = ts/ 100; ts -= 100*s;
78 av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
79 }
80}
81
82int ff_ass_bprint_dialog(AVBPrint *buf, const char *dialog,
83 int ts_start, int duration, int raw)
84{
85 int dlen;
86
87 if (!raw || raw == 2) {
88 long int layer = 0;
89
90 if (raw == 2) {
91 /* skip ReadOrder */
92 dialog = strchr(dialog, ',');
93 if (!dialog)
94 return AVERROR_INVALIDDATA;
95 dialog++;
96
97 /* extract Layer or Marked */
98 layer = strtol(dialog, (char**)&dialog, 10);
99 if (*dialog != ',')
100 return AVERROR_INVALIDDATA;
101 dialog++;
102 }
103 av_bprintf(buf, "Dialogue: %ld,", layer);
104 insert_ts(buf, ts_start);
105 insert_ts(buf, duration == -1 ? -1 : ts_start + duration);
106 if (raw != 2)
107 av_bprintf(buf, "Default,");
108 }
109
110 dlen = strcspn(dialog, "\n");
111 dlen += dialog[dlen] == '\n';
112
113 av_bprintf(buf, "%.*s", dlen, dialog);
114 if (raw == 2)
115 av_bprintf(buf, "\r\n");
116
117 return dlen;
118}
119
120int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
121 int ts_start, int duration, int raw)
122{
123 AVBPrint buf;
124 int ret, dlen;
125 AVSubtitleRect **rects;
126
127 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
128 if ((ret = ff_ass_bprint_dialog(&buf, dialog, ts_start, duration, raw)) < 0)
129 goto err;
130 dlen = ret;
131 if (!av_bprint_is_complete(&buf))
132 goto errnomem;
133
134 rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
135 if (!rects)
136 goto errnomem;
137 sub->rects = rects;
138 sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
139 rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
140 rects[sub->num_rects]->type = SUBTITLE_ASS;
141 ret = av_bprint_finalize(&buf, &rects[sub->num_rects]->ass);
142 if (ret < 0)
143 goto err;
144 sub->num_rects++;
145 return dlen;
146
147errnomem:
148 ret = AVERROR(ENOMEM);
149err:
150 av_bprint_finalize(&buf, NULL);
151 return ret;
152}
153
154void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
155 const char *linebreaks, int keep_ass_markup)
156{
157 const char *p_end = p + size;
158
159 for (; p < p_end && *p; p++) {
160
161 /* forced custom line breaks, not accounted as "normal" EOL */
162 if (linebreaks && strchr(linebreaks, *p)) {
163 av_bprintf(buf, "\\N");
164
165 /* standard ASS escaping so random characters don't get mis-interpreted
166 * as ASS */
167 } else if (!keep_ass_markup && strchr("{}\\", *p)) {
168 av_bprintf(buf, "\\%c", *p);
169
170 /* some packets might end abruptly (no \0 at the end, like for example
171 * in some cases of demuxing from a classic video container), some
172 * might be terminated with \n or \r\n which we have to remove (for
173 * consistency with those who haven't), and we also have to deal with
174 * evil cases such as \r at the end of the buffer (and no \0 terminated
175 * character) */
176 } else if (p[0] == '\n') {
177 /* some stuff left so we can insert a line break */
178 if (p < p_end - 1)
179 av_bprintf(buf, "\\N");
180 } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
181 /* \r followed by a \n, we can skip it. We don't insert the \N yet
182 * because we don't know if it is followed by more text */
183 continue;
184
185 /* finally, a sane character */
186 } else {
187 av_bprint_chars(buf, *p, 1);
188 }
189 }
190 av_bprintf(buf, "\r\n");
191}