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