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
28
30 int play_res_x, int play_res_y,
31 const char *font, int font_size,
32 int primary_color, int secondary_color,
33 int outline_color, int back_color,
34 int bold, int italic, int underline,
35 int border_style, int alignment)
36 {
38 "[Script Info]\n"
39 "; Script generated by FFmpeg/Lavc%s\n"
40 "ScriptType: v4.00+\n"
41 "PlayResX: %d\n"
42 "PlayResY: %d\n"
43 "ScaledBorderAndShadow: yes\n"
44 "YCbCr Matrix: None\n"
45 "\n"
46 "[V4+ Styles]\n"
47
48 /* ASS (v4+) header */
49 "Format: Name, "
50 "Fontname, Fontsize, "
51 "PrimaryColour, SecondaryColour, OutlineColour, BackColour, "
52 "Bold, Italic, Underline, StrikeOut, "
53 "ScaleX, ScaleY, "
54 "Spacing, Angle, "
55 "BorderStyle, Outline, Shadow, "
56 "Alignment, MarginL, MarginR, MarginV, "
57 "Encoding\n"
58
59 "Style: "
60 "Default," /* Name */
61 "%s,%d," /* Font{name,size} */
62 "&H%x,&H%x,&H%x,&H%x," /* {Primary,Secondary,Outline,Back}Colour */
63 "%d,%d,%d,0," /* Bold, Italic, Underline, StrikeOut */
64 "100,100," /* Scale{X,Y} */
65 "0,0," /* Spacing, Angle */
66 "%d,1,0," /* BorderStyle, Outline, Shadow */
67 "%d,10,10,10," /* Alignment, Margin[LRV] */
68 "1\n" /* Encoding */
69
70 "\n"
71 "[Events]\n"
72 "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n",
74 play_res_x, play_res_y, font, font_size,
75 primary_color, secondary_color, outline_color, back_color,
76 -bold, -italic, -underline, border_style, alignment);
77
81 return 0;
82 }
83
85 const char *font, int font_size,
86 int color,
int back_color,
87 int bold, int italic, int underline,
88 int border_style, int alignment)
89 {
93 back_color, back_color,
94 bold, italic, underline,
95 border_style, alignment);
96 }
97
99 {
109 }
110
112 const char *speaker, const char *text)
113 {
115 readorder, layer, style ? style : "Default",
116 speaker ? speaker : "", text);
117 }
118
120 int readorder, int layer, const char *style,
121 const char *speaker, unsigned *nb_rect_allocated)
122 {
124 char *ass_str;
125 uint64_t new_nb = 0;
126
129
130 if (nb_rect_allocated && *nb_rect_allocated <= sub->num_rects) {
131 if (sub->
num_rects < UINT_MAX / 17 * 16) {
133 } else
134 new_nb = UINT_MAX;
135 } else if (!nb_rect_allocated)
137
138 if (new_nb) {
140 if (!rects)
142 if (nb_rect_allocated)
143 *nb_rect_allocated = new_nb;
145 }
146
153 if (!ass_str)
156 return 0;
157 }
158
160 int readorder, int layer, const char *style,
161 const char *speaker)
162 {
164 }
165
167 {
171 }
172
174 const char *linebreaks, int keep_ass_markup)
175 {
176 const char *p_end =
p +
size;
177
178 for (;
p < p_end && *
p;
p++) {
179
180 /* forced custom line breaks, not accounted as "normal" EOL */
181 if (linebreaks && strchr(linebreaks, *
p)) {
183
184 /* cancel curly brackets to avoid bogus override tag blocks
185 * hiding text. Standard ASS has no character escapes,
186 * though (only) libass provides \{ and \}.
187 * Unpaired closing brackets don't need escaping at all though and
188 * to make the situation less bad in standard ASS insert an empty block
189 */
190 }
else if (!keep_ass_markup && *
p ==
'{') {
192
193 /* append word-joiner U+2060 as UTF-8 to break up sequences like \N */
194 }
else if (!keep_ass_markup && *
p ==
'\\') {
195 if (p_end -
p <= 3 || strncmp(
p + 1,
"\xe2\x81\xa0", 3))
197 else
199
200 /* some packets might end abruptly (no 0円 at the end, like for example
201 * in some cases of demuxing from a classic video container), some
202 * might be terminated with \n or \r\n which we have to remove (for
203 * consistency with those who haven't), and we also have to deal with
204 * evil cases such as \r at the end of the buffer (and no 0円 terminated
205 * character) */
206 }
else if (
p[0] ==
'\n') {
207 /* some stuff left so we can insert a line break */
210 }
else if (
p[0] ==
'\r' &&
p < p_end - 1 &&
p[1] ==
'\n') {
211 /* \r followed by a \n, we can skip it. We don't insert the \N yet
212 * because we don't know if it is followed by more text */
213 continue;
214
215 /* finally, a sane character */
216 } else {
218 }
219 }
220 }