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