Mercurial > mplayer.hg
annotate libass/ass.c @ 20540:91c99853793c
Synced to 26.10.2006 with respect to later utf-8 conversion.
author | jheryan |
---|---|
date | Tue, 31 Oct 2006 13:16:12 +0000 |
parents | 5cbf1c33a668 |
children | f672f6921ced |
rev | line source |
---|---|
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*- |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
2 // vim:ts=8:sw=8:noet:ai: |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
3 /* |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
5 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
6 This program is free software; you can redistribute it and/or modify |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
7 it under the terms of the GNU General Public License as published by |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
8 the Free Software Foundation; either version 2 of the License, or |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
9 (at your option) any later version. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
10 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
11 This program is distributed in the hope that it will be useful, |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
14 GNU General Public License for more details. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
15 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
16 You should have received a copy of the GNU General Public License |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
17 along with this program; if not, write to the Free Software |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
19 */ |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19905
diff
changeset
|
20 |
18937 | 21 #include "config.h" |
22 | |
23 #include <stdio.h> | |
24 #include <stdlib.h> | |
25 #include <string.h> | |
26 #include <assert.h> | |
27 #include <errno.h> | |
28 #include <sys/types.h> | |
29 #include <sys/stat.h> | |
30 #include <unistd.h> | |
19374 | 31 #include <inttypes.h> |
18937 | 32 |
20503 | 33 #ifdef HAVE_ENCA |
34 #include "subreader.h" // for guess_buffer_cp | |
35 #endif | |
36 | |
18937 | 37 #ifdef USE_ICONV |
38 #include <iconv.h> | |
39 #endif | |
40 | |
41 #include "mp_msg.h" | |
42 #include "ass.h" | |
43 #include "ass_utils.h" | |
20477 | 44 #include "ass_library.h" |
20503 | 45 #include "libvo/sub.h" // for utf8_get_char |
18937 | 46 |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
47 typedef enum {PST_UNKNOWN = 0, PST_INFO, PST_STYLES, PST_EVENTS, PST_FONTS} parser_state_t; |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
48 |
19492 | 49 struct parser_priv_s { |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
50 parser_state_t state; |
19492 | 51 char* fontname; |
52 char* fontdata; | |
53 int fontdata_size; | |
54 int fontdata_used; | |
55 }; | |
56 | |
18937 | 57 #define ASS_STYLES_ALLOC 20 |
58 #define ASS_EVENTS_ALLOC 200 | |
59 | |
60 void ass_free_track(ass_track_t* track) { | |
61 int i; | |
62 | |
19492 | 63 if (track->parser_priv) { |
64 if (track->parser_priv->fontname) | |
65 free(track->parser_priv->fontname); | |
66 if (track->parser_priv->fontdata) | |
67 free(track->parser_priv->fontdata); | |
68 free(track->parser_priv); | |
69 } | |
18937 | 70 if (track->style_format) |
71 free(track->style_format); | |
72 if (track->event_format) | |
73 free(track->event_format); | |
74 if (track->styles) { | |
19474
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
75 for (i = 0; i < track->n_styles; ++i) |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
76 ass_free_style(track, i); |
18937 | 77 free(track->styles); |
78 } | |
79 if (track->events) { | |
19474
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
80 for (i = 0; i < track->n_events; ++i) |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
81 ass_free_event(track, i); |
18937 | 82 free(track->events); |
83 } | |
84 } | |
85 | |
86 /// \brief Allocate a new style struct | |
87 /// \param track track | |
88 /// \return style id | |
89 int ass_alloc_style(ass_track_t* track) { | |
90 int sid; | |
91 | |
92 assert(track->n_styles <= track->max_styles); | |
93 | |
94 if (track->n_styles == track->max_styles) { | |
95 track->max_styles += ASS_STYLES_ALLOC; | |
96 track->styles = (ass_style_t*)realloc(track->styles, sizeof(ass_style_t)*track->max_styles); | |
97 } | |
98 | |
99 sid = track->n_styles++; | |
100 memset(track->styles + sid, 0, sizeof(ass_style_t)); | |
101 return sid; | |
102 } | |
103 | |
104 /// \brief Allocate a new event struct | |
105 /// \param track track | |
106 /// \return event id | |
107 int ass_alloc_event(ass_track_t* track) { | |
108 int eid; | |
109 | |
110 assert(track->n_events <= track->max_events); | |
111 | |
112 if (track->n_events == track->max_events) { | |
113 track->max_events += ASS_EVENTS_ALLOC; | |
114 track->events = (ass_event_t*)realloc(track->events, sizeof(ass_event_t)*track->max_events); | |
115 } | |
116 | |
117 eid = track->n_events++; | |
118 memset(track->events + eid, 0, sizeof(ass_event_t)); | |
119 return eid; | |
120 } | |
121 | |
19474
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
122 void ass_free_event(ass_track_t* track, int eid) { |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
123 ass_event_t* event = track->events + eid; |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
124 if (event->Name) |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
125 free(event->Name); |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
126 if (event->Effect) |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
127 free(event->Effect); |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
128 if (event->Text) |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
129 free(event->Text); |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19635
diff
changeset
|
130 if (event->render_priv) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19635
diff
changeset
|
131 free(event->render_priv); |
19474
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
132 } |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
133 |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
134 void ass_free_style(ass_track_t* track, int sid) { |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
135 ass_style_t* style = track->styles + sid; |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
136 if (style->Name) |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
137 free(style->Name); |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
138 if (style->FontName) |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
139 free(style->FontName); |
18937 | 140 } |
141 | |
142 // ============================================================================================== | |
143 | |
144 static void skip_spaces(char** str) { | |
145 char* p = *str; | |
146 while ((*p==' ') || (*p=='\t')) | |
147 ++p; | |
148 *str = p; | |
149 } | |
150 | |
151 static void rskip_spaces(char** str, char* limit) { | |
152 char* p = *str; | |
153 while ((p >= limit) && ((*p==' ') || (*p=='\t'))) | |
154 --p; | |
155 *str = p; | |
156 } | |
157 | |
158 /** | |
159 * \brief find style by name | |
160 * \param track track | |
161 * \param name style name | |
162 * \return index in track->styles | |
163 * Returnes 0 if no styles found => expects at least 1 style. | |
164 * Parsing code always adds "Default" style in the end. | |
165 */ | |
166 static int lookup_style(ass_track_t* track, char* name) { | |
167 int i; | |
19567 | 168 if (*name == '*') ++name; // FIXME: what does '*' really mean ? |
18937 | 169 for (i=0; i<track->n_styles; ++i) { |
170 // FIXME: mb strcasecmp ? | |
171 if (strcmp(track->styles[i].Name, name) == 0) | |
172 return i; | |
173 } | |
174 i = track->default_style; | |
175 mp_msg(MSGT_GLOBAL, MSGL_WARN, "[%p] Warning: no style named '%s' found, using '%s'\n", track, name, track->styles[i].Name); | |
176 return i; // use the first style | |
177 } | |
178 | |
179 static uint32_t string2color(char* p) { | |
180 uint32_t tmp; | |
181 (void)strtocolor(&p, &tmp); | |
182 return tmp; | |
183 } | |
184 | |
185 static long long string2timecode(char* p) { | |
186 unsigned h, m, s, ms; | |
187 long long tm; | |
188 int res = sscanf(p, "%1d:%2d:%2d.%2d", &h, &m, &s, &ms); | |
189 if (res < 4) { | |
190 mp_msg(MSGT_GLOBAL, MSGL_WARN, "bad timestamp\n"); | |
191 return 0; | |
192 } | |
193 tm = ((h * 60 + m) * 60 + s) * 1000 + ms * 10; | |
194 return tm; | |
195 } | |
196 | |
197 /** | |
198 * \brief converts numpad-style align to align. | |
199 */ | |
200 static int numpad2align(int val) { | |
201 int res, v; | |
202 v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment | |
203 if (v != 0) v = 3 - v; | |
204 res = ((val - 1) % 3) + 1; // horizontal alignment | |
205 res += v*4; | |
206 return res; | |
207 } | |
208 | |
209 #define NEXT(str,token) \ | |
210 token = next_token(&str); \ | |
211 if (!token) break; | |
212 | |
213 #define ANYVAL(name,func) \ | |
214 } else if (strcasecmp(tname, #name) == 0) { \ | |
215 target->name = func(token); \ | |
216 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "%s = %s\n", #name, token); | |
19495 | 217 |
218 #define STRVAL(name) \ | |
219 } else if (strcasecmp(tname, #name) == 0) { \ | |
220 if (target->name != NULL) free(target->name); \ | |
221 target->name = strdup(token); \ | |
222 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "%s = %s\n", #name, token); | |
223 | |
18937 | 224 #define COLORVAL(name) ANYVAL(name,string2color) |
225 #define INTVAL(name) ANYVAL(name,atoi) | |
226 #define FPVAL(name) ANYVAL(name,atof) | |
227 #define TIMEVAL(name) ANYVAL(name,string2timecode) | |
228 #define STYLEVAL(name) \ | |
229 } else if (strcasecmp(tname, #name) == 0) { \ | |
230 target->name = lookup_style(track, token); \ | |
231 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "%s = %s\n", #name, token); | |
232 | |
233 #define ALIAS(alias,name) \ | |
234 if (strcasecmp(tname, #alias) == 0) {tname = #name;} | |
235 | |
236 static char* next_token(char** str) { | |
237 char* p = *str; | |
238 char* start; | |
239 skip_spaces(&p); | |
240 if (*p == '\0') { | |
241 *str = p; | |
242 return 0; | |
243 } | |
244 start = p; // start of the token | |
245 for (; (*p != '\0') && (*p != ','); ++p) {} | |
246 if (*p == '\0') { | |
247 *str = p; // eos found, str will point to '\0' at exit | |
248 } else { | |
249 *p = '\0'; | |
250 *str = p + 1; // ',' found, str will point to the next char (beginning of the next token) | |
251 } | |
252 --p; // end of current token | |
253 rskip_spaces(&p, start); | |
254 if (p < start) | |
255 p = start; // empty token | |
256 else | |
257 ++p; // the first space character, or '\0' | |
258 *p = '\0'; | |
259 return start; | |
260 } | |
261 /** | |
262 * \brief Parse the tail of Dialogue line | |
263 * \param track track | |
264 * \param event parsed data goes here | |
265 * \param str string to parse, zero-terminated | |
266 * \param n_ignored number of format options to skip at the beginning | |
267 */ | |
268 static int process_event_tail(ass_track_t* track, ass_event_t* event, char* str, int n_ignored) | |
269 { | |
270 char* token; | |
271 char* tname; | |
272 char* p = str; | |
273 int i; | |
274 ass_event_t* target = event; | |
275 | |
276 char* format = strdup(track->event_format); | |
277 char* q = format; // format scanning pointer | |
278 | |
279 for (i = 0; i < n_ignored; ++i) { | |
280 NEXT(q, tname); | |
281 } | |
282 | |
283 while (1) { | |
284 NEXT(q, tname); | |
285 if (strcasecmp(tname, "Text") == 0) { | |
286 char* last; | |
287 event->Text = strdup(p); | |
19612 | 288 if (*event->Text != 0) { |
289 last = event->Text + strlen(event->Text) - 1; | |
290 if (last >= event->Text && *last == '\r') | |
291 *last = 0; | |
292 } | |
18937 | 293 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Text = %s\n", event->Text); |
294 event->Duration -= event->Start; | |
295 free(format); | |
296 return 0; // "Text" is always the last | |
297 } | |
298 NEXT(p, token); | |
299 | |
300 ALIAS(End,Duration) // temporarily store end timecode in event->Duration | |
301 if (0) { // cool ;) | |
19635 | 302 INTVAL(Layer) |
18937 | 303 STYLEVAL(Style) |
304 STRVAL(Name) | |
305 STRVAL(Effect) | |
306 INTVAL(MarginL) | |
307 INTVAL(MarginR) | |
308 INTVAL(MarginV) | |
309 TIMEVAL(Start) | |
310 TIMEVAL(Duration) | |
311 } | |
312 } | |
313 free(format); | |
314 return 1; | |
315 } | |
316 | |
317 /** | |
19495 | 318 * \brief Parse command line style overrides (--ass-force-style option) |
319 * \param track track to apply overrides to | |
320 * The format for overrides is [StyleName.]Field=Value | |
321 */ | |
19584
586e7d259d05
Apply -ass-force-style also to tracks generated from subdata.
eugeni
parents:
19567
diff
changeset
|
322 void process_force_style(ass_track_t* track) { |
19495 | 323 char **fs, *eq, *dt, *style, *tname, *token; |
324 ass_style_t* target; | |
325 int sid; | |
20477 | 326 char** list = track->library->style_overrides; |
19495 | 327 |
20477 | 328 if (!list) return; |
19495 | 329 |
20477 | 330 for (fs = list; *fs; ++fs) { |
19495 | 331 eq = strchr(*fs, '='); |
332 if (!eq) | |
333 continue; | |
334 *eq = '\0'; | |
335 token = eq + 1; | |
336 | |
337 dt = strchr(*fs, '.'); | |
338 if (dt) { | |
339 *dt = '\0'; | |
340 style = *fs; | |
341 tname = dt + 1; | |
342 } else { | |
343 style = NULL; | |
344 tname = *fs; | |
345 } | |
346 for (sid = 0; sid < track->n_styles; ++sid) { | |
347 if (style == NULL || strcasecmp(track->styles[sid].Name, style) == 0) { | |
348 target = track->styles + sid; | |
349 if (0) { | |
350 STRVAL(FontName) | |
351 COLORVAL(PrimaryColour) | |
352 COLORVAL(SecondaryColour) | |
353 COLORVAL(OutlineColour) | |
354 COLORVAL(BackColour) | |
355 INTVAL(FontSize) | |
356 INTVAL(Bold) | |
357 INTVAL(Italic) | |
358 INTVAL(Underline) | |
359 INTVAL(StrikeOut) | |
360 INTVAL(Spacing) | |
361 INTVAL(Angle) | |
362 INTVAL(BorderStyle) | |
363 INTVAL(Alignment) | |
364 INTVAL(MarginL) | |
365 INTVAL(MarginR) | |
366 INTVAL(MarginV) | |
367 INTVAL(Encoding) | |
368 FPVAL(ScaleX) | |
369 FPVAL(ScaleY) | |
370 FPVAL(Outline) | |
371 FPVAL(Shadow) | |
372 } | |
373 } | |
374 } | |
375 *eq = '='; | |
376 if (dt) *dt = '.'; | |
377 } | |
378 } | |
379 | |
380 /** | |
18937 | 381 * \brief Parse the Style line |
382 * \param track track | |
383 * \param str string to parse, zero-terminated | |
384 * Allocates a new style struct. | |
385 */ | |
386 static int process_style(ass_track_t* track, char *str) | |
387 { | |
388 | |
389 char* token; | |
390 char* tname; | |
391 char* p = str; | |
392 char* format; | |
393 char* q; // format scanning pointer | |
394 int sid; | |
395 ass_style_t* style; | |
396 ass_style_t* target; | |
397 | |
398 if (!track->style_format) { | |
399 // no style format header | |
400 // probably an ancient script version | |
401 if (track->track_type == TRACK_TYPE_SSA) | |
402 track->style_format = strdup("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour," | |
403 "TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline," | |
404 "Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding"); | |
405 else | |
406 track->style_format = strdup("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour," | |
407 "OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut," | |
408 "ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow," | |
409 "Alignment, MarginL, MarginR, MarginV, Encoding"); | |
410 } | |
411 | |
412 q = format = strdup(track->style_format); | |
413 | |
414 mp_msg(MSGT_GLOBAL, MSGL_V, "[%p] Style: %s\n", track, str); | |
415 | |
416 sid = ass_alloc_style(track); | |
417 | |
418 style = track->styles + sid; | |
419 target = style; | |
420 // fill style with some default values | |
421 style->ScaleX = 100.; | |
422 style->ScaleY = 100.; | |
423 | |
424 while (1) { | |
425 NEXT(q, tname); | |
426 NEXT(p, token); | |
427 | |
428 // ALIAS(TertiaryColour,OutlineColour) // ignore TertiaryColour; it appears only in SSA, and is overridden by BackColour | |
429 | |
430 if (0) { // cool ;) | |
431 STRVAL(Name) | |
432 if ((strcmp(target->Name, "Default")==0) || (strcmp(target->Name, "*Default")==0)) | |
433 track->default_style = sid; | |
434 STRVAL(FontName) | |
435 COLORVAL(PrimaryColour) | |
436 COLORVAL(SecondaryColour) | |
437 COLORVAL(OutlineColour) // TertiaryColor | |
438 COLORVAL(BackColour) | |
439 // SSA uses BackColour for both outline and shadow | |
440 // this will destroy SSA's TertiaryColour, but i'm not going to use it anyway | |
441 if (track->track_type == TRACK_TYPE_SSA) | |
442 target->OutlineColour = target->BackColour; | |
443 INTVAL(FontSize) | |
444 INTVAL(Bold) | |
445 INTVAL(Italic) | |
446 INTVAL(Underline) | |
447 INTVAL(StrikeOut) | |
448 INTVAL(Spacing) | |
449 INTVAL(Angle) | |
450 INTVAL(BorderStyle) | |
451 INTVAL(Alignment) | |
452 if (track->track_type == TRACK_TYPE_ASS) | |
453 target->Alignment = numpad2align(target->Alignment); | |
454 INTVAL(MarginL) | |
455 INTVAL(MarginR) | |
456 INTVAL(MarginV) | |
457 INTVAL(Encoding) | |
458 FPVAL(ScaleX) | |
459 FPVAL(ScaleY) | |
460 FPVAL(Outline) | |
461 FPVAL(Shadow) | |
462 } | |
463 } | |
464 style->ScaleX /= 100.; | |
465 style->ScaleY /= 100.; | |
466 if (!style->Name) | |
467 style->Name = strdup("Default"); | |
468 if (!style->FontName) | |
469 style->FontName = strdup("Arial"); | |
470 free(format); | |
471 return 0; | |
472 | |
473 } | |
474 | |
19492 | 475 static int process_styles_line(ass_track_t* track, char *str) |
476 { | |
477 if (!strncmp(str,"Format:", 7)) { | |
478 char* p = str + 7; | |
479 skip_spaces(&p); | |
480 track->style_format = strdup(p); | |
481 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Style format: %s\n", track->style_format); | |
482 } else if (!strncmp(str,"Style:", 6)) { | |
483 char* p = str + 6; | |
484 skip_spaces(&p); | |
485 process_style(track, p); | |
486 } | |
487 return 0; | |
488 } | |
489 | |
490 static int process_info_line(ass_track_t* track, char *str) | |
491 { | |
492 if (!strncmp(str, "PlayResX:", 9)) { | |
493 track->PlayResX = atoi(str + 9); | |
494 } else if (!strncmp(str,"PlayResY:", 9)) { | |
495 track->PlayResY = atoi(str + 9); | |
496 } else if (!strncmp(str,"Timer:", 6)) { | |
497 track->Timer = atof(str + 6); | |
498 } else if (!strncmp(str,"WrapStyle:", 10)) { | |
499 track->WrapStyle = atoi(str + 10); | |
500 } | |
501 return 0; | |
502 } | |
503 | |
504 static int process_events_line(ass_track_t* track, char *str) | |
505 { | |
506 if (!strncmp(str, "Format:", 7)) { | |
507 char* p = str + 7; | |
508 skip_spaces(&p); | |
509 track->event_format = strdup(p); | |
510 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Event format: %s\n", track->event_format); | |
511 } else if (!strncmp(str, "Dialogue:", 9)) { | |
512 // This should never be reached for embedded subtitles. | |
513 // They have slightly different format and are parsed in ass_process_chunk, | |
514 // called directly from demuxer | |
515 int eid; | |
516 ass_event_t* event; | |
517 | |
518 str += 9; | |
519 skip_spaces(&str); | |
520 | |
521 eid = ass_alloc_event(track); | |
522 event = track->events + eid; | |
523 | |
524 process_event_tail(track, event, str, 0); | |
525 } else { | |
526 mp_msg(MSGT_GLOBAL, MSGL_V, "Not understood: %s \n", str); | |
527 } | |
528 return 0; | |
529 } | |
530 | |
531 // Copied from mkvtoolnix | |
532 static unsigned char* decode_chars(unsigned char c1, unsigned char c2, | |
533 unsigned char c3, unsigned char c4, unsigned char* dst, int cnt) | |
534 { | |
535 uint32_t value; | |
536 unsigned char bytes[3]; | |
537 int i; | |
538 | |
539 value = ((c1 - 33) << 18) + ((c2 - 33) << 12) + ((c3 - 33) << 6) + (c4 - 33); | |
540 bytes[2] = value & 0xff; | |
541 bytes[1] = (value & 0xff00) >> 8; | |
542 bytes[0] = (value & 0xff0000) >> 16; | |
543 | |
544 for (i = 0; i < cnt; ++i) | |
545 *dst++ = bytes[i]; | |
546 return dst; | |
547 } | |
548 | |
549 static int decode_font(ass_track_t* track) | |
550 { | |
551 unsigned char* p; | |
552 unsigned char* q; | |
553 int i; | |
554 int size; // original size | |
555 int dsize; // decoded size | |
556 unsigned char* buf = 0; | |
557 | |
558 mp_msg(MSGT_GLOBAL, MSGL_V, "font: %d bytes encoded data \n", track->parser_priv->fontdata_used); | |
559 size = track->parser_priv->fontdata_used; | |
560 if (size % 4 == 1) { | |
561 mp_msg(MSGT_GLOBAL, MSGL_ERR, "bad encoded data size\n"); | |
562 goto error_decode_font; | |
563 } | |
564 buf = malloc(size / 4 * 3 + 2); | |
565 q = buf; | |
566 for (i = 0, p = (unsigned char*)track->parser_priv->fontdata; i < size / 4; i++, p+=4) { | |
567 q = decode_chars(p[0], p[1], p[2], p[3], q, 3); | |
568 } | |
569 if (size % 4 == 2) { | |
570 q = decode_chars(p[0], p[1], 0, 0, q, 1); | |
571 } else if (size % 4 == 3) { | |
572 q = decode_chars(p[0], p[1], p[2], 0, q, 2); | |
573 } | |
574 dsize = q - buf; | |
575 assert(dsize <= size / 4 * 3 + 2); | |
576 | |
20477 | 577 if (track->library->extract_fonts) |
578 ass_process_font(track->library, track->parser_priv->fontname, (char*)buf, dsize); | |
19492 | 579 |
580 error_decode_font: | |
581 if (buf) free(buf); | |
582 free(track->parser_priv->fontname); | |
583 free(track->parser_priv->fontdata); | |
584 track->parser_priv->fontname = 0; | |
585 track->parser_priv->fontdata = 0; | |
586 track->parser_priv->fontdata_size = 0; | |
587 track->parser_priv->fontdata_used = 0; | |
588 return 0; | |
589 } | |
590 | |
591 static char* validate_fname(char* name); | |
592 | |
593 static int process_fonts_line(ass_track_t* track, char *str) | |
594 { | |
595 int len; | |
596 | |
597 if (!strncmp(str, "fontname:", 9)) { | |
598 char* p = str + 9; | |
599 skip_spaces(&p); | |
600 if (track->parser_priv->fontname) { | |
601 decode_font(track); | |
602 } | |
603 track->parser_priv->fontname = validate_fname(p); | |
604 mp_msg(MSGT_GLOBAL, MSGL_V, "fontname: %s\n", track->parser_priv->fontname); | |
605 return 0; | |
606 } | |
607 | |
608 if (!track->parser_priv->fontname) { | |
609 mp_msg(MSGT_GLOBAL, MSGL_V, "Not understood: %s \n", str); | |
610 return 0; | |
611 } | |
612 | |
613 len = strlen(str); | |
614 if (len > 80) { | |
615 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Font line too long: %d, %s\n", len, str); | |
616 return 0; | |
617 } | |
618 if (track->parser_priv->fontdata_used + len > track->parser_priv->fontdata_size) { | |
619 track->parser_priv->fontdata_size += 100 * 1024; | |
620 track->parser_priv->fontdata = realloc(track->parser_priv->fontdata, track->parser_priv->fontdata_size); | |
621 } | |
622 memcpy(track->parser_priv->fontdata + track->parser_priv->fontdata_used, str, len); | |
623 track->parser_priv->fontdata_used += len; | |
624 | |
625 return 0; | |
626 } | |
627 | |
18937 | 628 /** |
629 * \brief Parse a header line | |
630 * \param track track | |
631 * \param str string to parse, zero-terminated | |
632 */ | |
19492 | 633 static int process_line(ass_track_t* track, char *str) |
18937 | 634 { |
19492 | 635 if (strstr(str, "[Script Info]")) { // FIXME: strstr to skip possible BOM at the beginning of the script |
636 track->parser_priv->state = PST_INFO; | |
637 } else if (!strncmp(str, "[V4 Styles]", 11)) { | |
638 track->parser_priv->state = PST_STYLES; | |
639 track->track_type = TRACK_TYPE_SSA; | |
640 } else if (!strncmp(str, "[V4+ Styles]", 12)) { | |
641 track->parser_priv->state = PST_STYLES; | |
642 track->track_type = TRACK_TYPE_ASS; | |
643 } else if (!strncmp(str, "[Events]", 8)) { | |
644 track->parser_priv->state = PST_EVENTS; | |
645 } else if (!strncmp(str, "[Fonts]", 7)) { | |
646 track->parser_priv->state = PST_FONTS; | |
647 } else { | |
648 switch (track->parser_priv->state) { | |
649 case PST_INFO: | |
650 process_info_line(track, str); | |
651 break; | |
652 case PST_STYLES: | |
653 process_styles_line(track, str); | |
654 break; | |
655 case PST_EVENTS: | |
656 process_events_line(track, str); | |
657 break; | |
658 case PST_FONTS: | |
659 process_fonts_line(track, str); | |
660 break; | |
661 default: | |
662 break; | |
18937 | 663 } |
19492 | 664 } |
665 | |
666 // there is no explicit end-of-font marker in ssa/ass | |
667 if ((track->parser_priv->state != PST_FONTS) && (track->parser_priv->fontname)) | |
668 decode_font(track); | |
669 | |
670 return 0; | |
671 } | |
672 | |
673 static int process_text(ass_track_t* track, char* str) | |
674 { | |
675 char* p = str; | |
676 while(1) { | |
677 char* q; | |
678 for (;((*p=='\r')||(*p=='\n'));++p) {} | |
679 for (q=p; ((*q!='\0')&&(*q!='\r')&&(*q!='\n')); ++q) {}; | |
680 if (q==p) | |
681 break; | |
682 if (*q != '\0') | |
683 *(q++) = '\0'; | |
684 process_line(track, p); | |
685 if (*q == '\0') | |
686 break; | |
687 p = q; | |
18937 | 688 } |
689 return 0; | |
690 } | |
691 | |
692 /** | |
693 * \brief Process CodecPrivate section of subtitle stream | |
694 * \param track track | |
695 * \param data string to parse | |
696 * \param size length of data | |
19492 | 697 CodecPrivate section contains [Stream Info] and [V4+ Styles] ([V4 Styles] for SSA) sections |
18937 | 698 */ |
19492 | 699 void ass_process_codec_private(ass_track_t* track, char *data, int size) |
18937 | 700 { |
701 char* str = malloc(size + 1); | |
702 int sid; | |
703 | |
704 memcpy(str, data, size); | |
705 str[size] = '\0'; | |
706 | |
19492 | 707 process_text(track, str); |
18937 | 708 free(str); |
709 | |
710 // add "Default" style to the end | |
711 // will be used if track does not contain a default style (or even does not contain styles at all) | |
712 sid = ass_alloc_style(track); | |
713 track->styles[sid].Name = strdup("Default"); | |
714 track->styles[sid].FontName = strdup("Arial"); | |
715 | |
716 if (!track->event_format) { | |
717 // probably an mkv produced by ancient mkvtoolnix | |
718 // such files don't have [Events] and Format: headers | |
19492 | 719 track->parser_priv->state = PST_EVENTS; |
18937 | 720 if (track->track_type == TRACK_TYPE_SSA) |
721 track->event_format = strdup("Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"); | |
722 else | |
723 track->event_format = strdup("Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text"); | |
724 } | |
19495 | 725 |
726 process_force_style(track); | |
18937 | 727 } |
728 | |
729 static int check_duplicate_event(ass_track_t* track, int ReadOrder) | |
730 { | |
731 int i; | |
732 for (i = 0; i<track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with | |
733 if (track->events[i].ReadOrder == ReadOrder) | |
734 return 1; | |
735 return 0; | |
736 } | |
737 | |
738 /** | |
739 * \brief Process a chunk of subtitle stream data. In matroska, this containes exactly 1 event (or a commentary) | |
740 * \param track track | |
741 * \param data string to parse | |
742 * \param size length of data | |
743 * \param timecode starting time of the event (milliseconds) | |
744 * \param duration duration of the event (milliseconds) | |
745 */ | |
19492 | 746 void ass_process_chunk(ass_track_t* track, char *data, int size, long long timecode, long long duration) |
18937 | 747 { |
748 char* str; | |
749 int eid; | |
750 char* p; | |
751 char* token; | |
752 ass_event_t* event; | |
753 | |
754 if (!track->event_format) { | |
755 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Event format header missing\n"); | |
756 return; | |
757 } | |
758 | |
759 str = malloc(size + 1); | |
760 memcpy(str, data, size); | |
761 str[size] = '\0'; | |
19378 | 762 mp_msg(MSGT_GLOBAL, MSGL_V, "event at %" PRId64 ", +%" PRId64 ": %s \n", (int64_t)timecode, (int64_t)duration, str); |
18937 | 763 |
764 eid = ass_alloc_event(track); | |
765 event = track->events + eid; | |
766 | |
767 p = str; | |
768 | |
769 do { | |
770 NEXT(p, token); | |
771 event->ReadOrder = atoi(token); | |
772 if (check_duplicate_event(track, event->ReadOrder)) | |
773 break; | |
774 | |
775 NEXT(p, token); | |
776 event->Layer = atoi(token); | |
777 | |
778 process_event_tail(track, event, p, 3); | |
779 | |
780 event->Start = timecode; | |
781 event->Duration = duration; | |
782 | |
783 free(str); | |
784 return; | |
785 // dump_events(tid); | |
786 } while (0); | |
787 // some error | |
19474
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
788 ass_free_event(track, eid); |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
789 track->n_events--; |
18937 | 790 free(str); |
791 } | |
792 | |
793 #ifdef USE_ICONV | |
794 /** \brief recode buffer to utf-8 | |
20477 | 795 * constraint: codepage != 0 |
18937 | 796 * \param data pointer to text buffer |
797 * \param size buffer size | |
798 * \return a pointer to recoded buffer, caller is responsible for freeing it | |
799 **/ | |
20477 | 800 static char* sub_recode(char* data, size_t size, char* codepage) |
18937 | 801 { |
802 static iconv_t icdsc = (iconv_t)(-1); | |
803 char* tocp = "UTF-8"; | |
804 char* outbuf; | |
20477 | 805 assert(codepage); |
18937 | 806 |
807 { | |
20477 | 808 char* cp_tmp = codepage; |
18937 | 809 #ifdef HAVE_ENCA |
810 char enca_lang[3], enca_fallback[100]; | |
20477 | 811 if (sscanf(codepage, "enca:%2s:%99s", enca_lang, enca_fallback) == 2 |
812 || sscanf(codepage, "ENCA:%2s:%99s", enca_lang, enca_fallback) == 2) { | |
20503 | 813 cp_tmp = guess_buffer_cp((unsigned char*)data, size, enca_lang, enca_fallback); |
18937 | 814 } |
815 #endif | |
816 if ((icdsc = iconv_open (tocp, cp_tmp)) != (iconv_t)(-1)){ | |
817 mp_msg(MSGT_SUBREADER,MSGL_V,"LIBSUB: opened iconv descriptor.\n"); | |
818 } else | |
819 mp_msg(MSGT_SUBREADER,MSGL_ERR,"LIBSUB: error opening iconv descriptor.\n"); | |
820 #ifdef HAVE_ENCA | |
821 if (cp_tmp) free(cp_tmp); | |
822 #endif | |
823 } | |
824 | |
825 { | |
826 size_t osize = size; | |
827 size_t ileft = size; | |
828 size_t oleft = size - 1; | |
829 char* ip; | |
830 char* op; | |
831 size_t rc; | |
832 | |
833 outbuf = malloc(size); | |
834 ip = data; | |
835 op = outbuf; | |
836 | |
837 while (ileft) { | |
838 rc = iconv(icdsc, &ip, &ileft, &op, &oleft); | |
839 if (rc == (size_t)(-1)) { | |
840 if (errno == E2BIG) { | |
841 int offset = op - outbuf; | |
842 outbuf = (char*)realloc(outbuf, osize + size); | |
843 op = outbuf + offset; | |
844 osize += size; | |
845 oleft += size; | |
846 } else { | |
847 mp_msg(MSGT_SUBREADER, MSGL_WARN, "LIBSUB: error recoding file.\n"); | |
848 return NULL; | |
849 } | |
850 } | |
851 } | |
852 outbuf[osize - oleft - 1] = 0; | |
853 } | |
854 | |
855 if (icdsc != (iconv_t)(-1)) { | |
856 (void)iconv_close(icdsc); | |
857 icdsc = (iconv_t)(-1); | |
858 mp_msg(MSGT_SUBREADER,MSGL_V,"LIBSUB: closed iconv descriptor.\n"); | |
859 } | |
860 | |
861 return outbuf; | |
862 } | |
863 #endif // ICONV | |
864 | |
865 /** | |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
866 * \brief read file contents into newly allocated buffer, recoding to utf-8 |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
867 */ |
20477 | 868 static char* read_file(char* fname, char* codepage) |
18937 | 869 { |
870 int res; | |
871 long sz; | |
872 long bytes_read; | |
873 char* buf; | |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
874 |
18937 | 875 FILE* fp = fopen(fname, "rb"); |
876 if (!fp) { | |
877 mp_msg(MSGT_GLOBAL, MSGL_WARN, "ass_read_file(%s): fopen failed\n", fname); | |
878 return 0; | |
879 } | |
880 res = fseek(fp, 0, SEEK_END); | |
881 if (res == -1) { | |
882 mp_msg(MSGT_GLOBAL, MSGL_WARN, "ass_read_file(%s): fseek failed\n", fname); | |
883 fclose(fp); | |
884 return 0; | |
885 } | |
886 | |
887 sz = ftell(fp); | |
888 rewind(fp); | |
889 | |
890 if (sz > 10*1024*1024) { | |
891 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ass_read_file(%s): Refusing to load subtitles larger than 10M\n", fname); | |
892 fclose(fp); | |
893 return 0; | |
894 } | |
895 | |
896 mp_msg(MSGT_GLOBAL, MSGL_V, "file size: %ld\n", sz); | |
897 | |
898 buf = malloc(sz + 1); | |
899 assert(buf); | |
900 bytes_read = 0; | |
901 do { | |
902 res = fread(buf + bytes_read, 1, sz - bytes_read, fp); | |
903 if (res <= 0) { | |
904 mp_msg(MSGT_GLOBAL, MSGL_INFO, "Read failed, %d: %s\n", errno, strerror(errno)); | |
905 fclose(fp); | |
906 free(buf); | |
907 return 0; | |
908 } | |
909 bytes_read += res; | |
910 } while (sz - bytes_read > 0); | |
911 buf[sz] = '\0'; | |
912 fclose(fp); | |
913 | |
914 #ifdef USE_ICONV | |
20477 | 915 if (codepage) { |
916 char* tmpbuf = sub_recode(buf, sz, codepage); | |
18937 | 917 free(buf); |
918 buf = tmpbuf; | |
919 } | |
920 #endif | |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
921 return buf; |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
922 } |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
923 |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
924 /** |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
925 * \brief Read subtitles from file. |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
926 * \param fname file name |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
927 * \return newly allocated track |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
928 */ |
20477 | 929 ass_track_t* ass_read_file(ass_library_t* library, char* fname, char* codepage) |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
930 { |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
931 char* buf; |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
932 ass_track_t* track; |
19905 | 933 int i; |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
934 |
20477 | 935 buf = read_file(fname, codepage); |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
936 if (!buf) |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
937 return 0; |
18937 | 938 |
20477 | 939 track = ass_new_track(library); |
18937 | 940 track->name = strdup(fname); |
941 | |
942 // process header | |
19492 | 943 process_text(track, buf); |
944 | |
19905 | 945 // external SSA/ASS subs does not have ReadOrder field |
946 for (i = 0; i < track->n_events; ++i) | |
947 track->events[i].ReadOrder = i; | |
948 | |
19492 | 949 // there is no explicit end-of-font marker in ssa/ass |
950 if (track->parser_priv->fontname) | |
951 decode_font(track); | |
952 | |
18937 | 953 free(buf); |
954 | |
19492 | 955 if (track->track_type == TRACK_TYPE_UNKNOWN) { |
18937 | 956 ass_free_track(track); |
957 return 0; | |
958 } | |
959 | |
19495 | 960 process_force_style(track); |
961 | |
18937 | 962 mp_msg(MSGT_GLOBAL, MSGL_INFO, "LIBASS: added subtitle file: %s (%d styles, %d events)\n", fname, track->n_styles, track->n_events); |
963 | |
964 // dump_events(forced_tid); | |
965 return track; | |
966 } | |
967 | |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
968 /** |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
969 * \brief read styles from file into already initialized track |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
970 */ |
20477 | 971 int ass_read_styles(ass_track_t* track, char* fname, char* codepage) |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
972 { |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
973 char* buf; |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
974 parser_state_t old_state; |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
975 |
20477 | 976 buf = read_file(fname, codepage); |
19652
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
977 if (!buf) |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
978 return 1; |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
979 |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
980 old_state = track->parser_priv->state; |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
981 track->parser_priv->state = PST_STYLES; |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
982 process_text(track, buf); |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
983 track->parser_priv->state = old_state; |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
984 |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
985 return 0; |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
986 } |
2c016957360a
Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents:
19639
diff
changeset
|
987 |
18937 | 988 static char* validate_fname(char* name) |
989 { | |
990 char* fname; | |
991 char* p; | |
992 char* q; | |
993 unsigned code; | |
994 int sz = strlen(name); | |
995 | |
996 q = fname = malloc(sz + 1); | |
997 p = name; | |
998 while (*p) { | |
20503 | 999 code = utf8_get_char(&p); |
18937 | 1000 if (code == 0) |
1001 break; | |
1002 if ( (code > 0x7F) || | |
1003 (code == '\\') || | |
1004 (code == '/') || | |
1005 (code == ':') || | |
1006 (code == '*') || | |
1007 (code == '?') || | |
1008 (code == '<') || | |
1009 (code == '>') || | |
1010 (code == '|') || | |
1011 (code == 0)) | |
1012 { | |
1013 *q++ = '_'; | |
1014 } else { | |
1015 *q++ = code; | |
1016 } | |
1017 if (p - name > sz) | |
1018 break; | |
1019 } | |
1020 *q = 0; | |
1021 return fname; | |
1022 } | |
1023 | |
1024 /** | |
1025 * \brief Process embedded matroska font. Saves it to ~/.mplayer/fonts. | |
1026 * \param name attachment name | |
1027 * \param data binary font data | |
1028 * \param data_size data size | |
1029 */ | |
20477 | 1030 void ass_process_font(ass_library_t* library, const char* name, char* data, int data_size) |
18937 | 1031 { |
1032 char buf[1000]; | |
1033 FILE* fp = 0; | |
1034 int rc; | |
1035 struct stat st; | |
1036 char* fname; | |
20477 | 1037 const char* fonts_dir = library->fonts_dir; |
18937 | 1038 |
20477 | 1039 if (!fonts_dir) |
1040 return; | |
18937 | 1041 rc = stat(fonts_dir, &st); |
1042 if (rc) { | |
1043 int res; | |
1044 #ifndef __MINGW32__ | |
1045 res = mkdir(fonts_dir, 0700); | |
1046 #else | |
1047 res = mkdir(fonts_dir); | |
1048 #endif | |
1049 if (res) { | |
1050 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Failed to create: %s\n", fonts_dir); | |
1051 } | |
1052 } else if (!S_ISDIR(st.st_mode)) { | |
1053 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Not a directory: %s\n", fonts_dir); | |
1054 } | |
1055 | |
1056 fname = validate_fname((char*)name); | |
1057 | |
1058 snprintf(buf, 1000, "%s/%s", fonts_dir, fname); | |
1059 free(fname); | |
1060 | |
1061 fp = fopen(buf, "wb"); | |
1062 if (!fp) return; | |
1063 | |
1064 fwrite(data, data_size, 1, fp); | |
1065 fclose(fp); | |
1066 } | |
1067 | |
1068 long long ass_step_sub(ass_track_t* track, long long now, int movement) { | |
1069 int i; | |
1070 | |
1071 if (movement == 0) return 0; | |
1072 if (track->n_events == 0) return 0; | |
1073 | |
1074 if (movement < 0) | |
1075 for (i = 0; (i < track->n_events) && ((long long)(track->events[i].Start + track->events[i].Duration) <= now); ++i) {} | |
1076 else | |
1077 for (i = track->n_events - 1; (i >= 0) && ((long long)(track->events[i].Start) > now); --i) {} | |
1078 | |
1079 // -1 and n_events are ok | |
1080 assert(i >= -1); assert(i <= track->n_events); | |
1081 i += movement; | |
1082 if (i < 0) i = 0; | |
1083 if (i >= track->n_events) i = track->n_events - 1; | |
1084 return ((long long)track->events[i].Start) - now; | |
1085 } | |
1086 | |
20477 | 1087 ass_track_t* ass_new_track(ass_library_t* library) { |
18937 | 1088 ass_track_t* track = calloc(1, sizeof(ass_track_t)); |
20477 | 1089 track->library = library; |
19492 | 1090 track->parser_priv = calloc(1, sizeof(parser_priv_t)); |
18937 | 1091 return track; |
1092 } | |
1093 |