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