Mercurial > mplayer.hg
annotate libass/ass.c @ 19612:dcd0f218008f
Bugfix: potential write of unallocated memory.
author | eugeni |
---|---|
date | Thu, 31 Aug 2006 22:45:20 +0000 |
parents | 586e7d259d05 |
children | 4a64fecfb425 |
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; | |
19567 | 165 if (*name == '*') ++name; // FIXME: what does '*' really mean ? |
18937 | 166 for (i=0; i<track->n_styles; ++i) { |
167 // FIXME: mb strcasecmp ? | |
168 if (strcmp(track->styles[i].Name, name) == 0) | |
169 return i; | |
170 } | |
171 i = track->default_style; | |
172 mp_msg(MSGT_GLOBAL, MSGL_WARN, "[%p] Warning: no style named '%s' found, using '%s'\n", track, name, track->styles[i].Name); | |
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) { | |
187 mp_msg(MSGT_GLOBAL, MSGL_WARN, "bad timestamp\n"); | |
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); \ | |
213 mp_msg(MSGT_GLOBAL, 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); \ | |
219 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "%s = %s\n", #name, token); | |
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); \ | |
228 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "%s = %s\n", #name, token); | |
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 | |
276 for (i = 0; i < n_ignored; ++i) { | |
277 NEXT(q, tname); | |
278 } | |
279 | |
280 while (1) { | |
281 NEXT(q, tname); | |
282 if (strcasecmp(tname, "Text") == 0) { | |
283 char* last; | |
284 event->Text = strdup(p); | |
19612 | 285 if (*event->Text != 0) { |
286 last = event->Text + strlen(event->Text) - 1; | |
287 if (last >= event->Text && *last == '\r') | |
288 *last = 0; | |
289 } | |
18937 | 290 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Text = %s\n", event->Text); |
291 event->Duration -= event->Start; | |
292 free(format); | |
293 return 0; // "Text" is always the last | |
294 } | |
295 NEXT(p, token); | |
296 | |
297 ALIAS(End,Duration) // temporarily store end timecode in event->Duration | |
298 if (0) { // cool ;) | |
299 STYLEVAL(Style) | |
300 STRVAL(Name) | |
301 STRVAL(Effect) | |
302 INTVAL(MarginL) | |
303 INTVAL(MarginR) | |
304 INTVAL(MarginV) | |
305 TIMEVAL(Start) | |
306 TIMEVAL(Duration) | |
307 } | |
308 } | |
309 free(format); | |
310 return 1; | |
311 } | |
312 | |
313 /** | |
19495 | 314 * \brief Parse command line style overrides (--ass-force-style option) |
315 * \param track track to apply overrides to | |
316 * The format for overrides is [StyleName.]Field=Value | |
317 */ | |
19584
586e7d259d05
Apply -ass-force-style also to tracks generated from subdata.
eugeni
parents:
19567
diff
changeset
|
318 void process_force_style(ass_track_t* track) { |
19495 | 319 char **fs, *eq, *dt, *style, *tname, *token; |
320 ass_style_t* target; | |
321 int sid; | |
322 | |
323 if (!ass_force_style_list) return; | |
324 | |
325 for (fs = ass_force_style_list; *fs; ++fs) { | |
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) | |
355 INTVAL(Spacing) | |
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 | |
409 mp_msg(MSGT_GLOBAL, MSGL_V, "[%p] Style: %s\n", track, str); | |
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) | |
443 INTVAL(Spacing) | |
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.; | |
461 if (!style->Name) | |
462 style->Name = strdup("Default"); | |
463 if (!style->FontName) | |
464 style->FontName = strdup("Arial"); | |
465 free(format); | |
466 return 0; | |
467 | |
468 } | |
469 | |
19492 | 470 static int process_styles_line(ass_track_t* track, char *str) |
471 { | |
472 if (!strncmp(str,"Format:", 7)) { | |
473 char* p = str + 7; | |
474 skip_spaces(&p); | |
475 track->style_format = strdup(p); | |
476 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Style format: %s\n", track->style_format); | |
477 } else if (!strncmp(str,"Style:", 6)) { | |
478 char* p = str + 6; | |
479 skip_spaces(&p); | |
480 process_style(track, p); | |
481 } | |
482 return 0; | |
483 } | |
484 | |
485 static int process_info_line(ass_track_t* track, char *str) | |
486 { | |
487 if (!strncmp(str, "PlayResX:", 9)) { | |
488 track->PlayResX = atoi(str + 9); | |
489 } else if (!strncmp(str,"PlayResY:", 9)) { | |
490 track->PlayResY = atoi(str + 9); | |
491 } else if (!strncmp(str,"Timer:", 6)) { | |
492 track->Timer = atof(str + 6); | |
493 } else if (!strncmp(str,"WrapStyle:", 10)) { | |
494 track->WrapStyle = atoi(str + 10); | |
495 } | |
496 return 0; | |
497 } | |
498 | |
499 static int process_events_line(ass_track_t* track, char *str) | |
500 { | |
501 if (!strncmp(str, "Format:", 7)) { | |
502 char* p = str + 7; | |
503 skip_spaces(&p); | |
504 track->event_format = strdup(p); | |
505 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Event format: %s\n", track->event_format); | |
506 } else if (!strncmp(str, "Dialogue:", 9)) { | |
507 // This should never be reached for embedded subtitles. | |
508 // They have slightly different format and are parsed in ass_process_chunk, | |
509 // called directly from demuxer | |
510 int eid; | |
511 ass_event_t* event; | |
512 | |
513 str += 9; | |
514 skip_spaces(&str); | |
515 | |
516 eid = ass_alloc_event(track); | |
517 event = track->events + eid; | |
518 | |
519 process_event_tail(track, event, str, 0); | |
520 } else { | |
521 mp_msg(MSGT_GLOBAL, MSGL_V, "Not understood: %s \n", str); | |
522 } | |
523 return 0; | |
524 } | |
525 | |
526 // Copied from mkvtoolnix | |
527 static unsigned char* decode_chars(unsigned char c1, unsigned char c2, | |
528 unsigned char c3, unsigned char c4, unsigned char* dst, int cnt) | |
529 { | |
530 uint32_t value; | |
531 unsigned char bytes[3]; | |
532 int i; | |
533 | |
534 value = ((c1 - 33) << 18) + ((c2 - 33) << 12) + ((c3 - 33) << 6) + (c4 - 33); | |
535 bytes[2] = value & 0xff; | |
536 bytes[1] = (value & 0xff00) >> 8; | |
537 bytes[0] = (value & 0xff0000) >> 16; | |
538 | |
539 for (i = 0; i < cnt; ++i) | |
540 *dst++ = bytes[i]; | |
541 return dst; | |
542 } | |
543 | |
544 static int decode_font(ass_track_t* track) | |
545 { | |
546 unsigned char* p; | |
547 unsigned char* q; | |
548 int i; | |
549 int size; // original size | |
550 int dsize; // decoded size | |
551 unsigned char* buf = 0; | |
552 | |
553 mp_msg(MSGT_GLOBAL, MSGL_V, "font: %d bytes encoded data \n", track->parser_priv->fontdata_used); | |
554 size = track->parser_priv->fontdata_used; | |
555 if (size % 4 == 1) { | |
556 mp_msg(MSGT_GLOBAL, MSGL_ERR, "bad encoded data size\n"); | |
557 goto error_decode_font; | |
558 } | |
559 buf = malloc(size / 4 * 3 + 2); | |
560 q = buf; | |
561 for (i = 0, p = (unsigned char*)track->parser_priv->fontdata; i < size / 4; i++, p+=4) { | |
562 q = decode_chars(p[0], p[1], p[2], p[3], q, 3); | |
563 } | |
564 if (size % 4 == 2) { | |
565 q = decode_chars(p[0], p[1], 0, 0, q, 1); | |
566 } else if (size % 4 == 3) { | |
567 q = decode_chars(p[0], p[1], p[2], 0, q, 2); | |
568 } | |
569 dsize = q - buf; | |
570 assert(dsize <= size / 4 * 3 + 2); | |
571 | |
572 if (extract_embedded_fonts) | |
573 ass_process_font(track->parser_priv->fontname, (char*)buf, dsize); | |
574 | |
575 error_decode_font: | |
576 if (buf) free(buf); | |
577 free(track->parser_priv->fontname); | |
578 free(track->parser_priv->fontdata); | |
579 track->parser_priv->fontname = 0; | |
580 track->parser_priv->fontdata = 0; | |
581 track->parser_priv->fontdata_size = 0; | |
582 track->parser_priv->fontdata_used = 0; | |
583 return 0; | |
584 } | |
585 | |
586 static char* validate_fname(char* name); | |
587 | |
588 static int process_fonts_line(ass_track_t* track, char *str) | |
589 { | |
590 int len; | |
591 | |
592 if (!strncmp(str, "fontname:", 9)) { | |
593 char* p = str + 9; | |
594 skip_spaces(&p); | |
595 if (track->parser_priv->fontname) { | |
596 decode_font(track); | |
597 } | |
598 track->parser_priv->fontname = validate_fname(p); | |
599 mp_msg(MSGT_GLOBAL, MSGL_V, "fontname: %s\n", track->parser_priv->fontname); | |
600 return 0; | |
601 } | |
602 | |
603 if (!track->parser_priv->fontname) { | |
604 mp_msg(MSGT_GLOBAL, MSGL_V, "Not understood: %s \n", str); | |
605 return 0; | |
606 } | |
607 | |
608 len = strlen(str); | |
609 if (len > 80) { | |
610 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Font line too long: %d, %s\n", len, str); | |
611 return 0; | |
612 } | |
613 if (track->parser_priv->fontdata_used + len > track->parser_priv->fontdata_size) { | |
614 track->parser_priv->fontdata_size += 100 * 1024; | |
615 track->parser_priv->fontdata = realloc(track->parser_priv->fontdata, track->parser_priv->fontdata_size); | |
616 } | |
617 memcpy(track->parser_priv->fontdata + track->parser_priv->fontdata_used, str, len); | |
618 track->parser_priv->fontdata_used += len; | |
619 | |
620 return 0; | |
621 } | |
622 | |
18937 | 623 /** |
624 * \brief Parse a header line | |
625 * \param track track | |
626 * \param str string to parse, zero-terminated | |
627 */ | |
19492 | 628 static int process_line(ass_track_t* track, char *str) |
18937 | 629 { |
19492 | 630 if (strstr(str, "[Script Info]")) { // FIXME: strstr to skip possible BOM at the beginning of the script |
631 track->parser_priv->state = PST_INFO; | |
632 } else if (!strncmp(str, "[V4 Styles]", 11)) { | |
633 track->parser_priv->state = PST_STYLES; | |
634 track->track_type = TRACK_TYPE_SSA; | |
635 } else if (!strncmp(str, "[V4+ Styles]", 12)) { | |
636 track->parser_priv->state = PST_STYLES; | |
637 track->track_type = TRACK_TYPE_ASS; | |
638 } else if (!strncmp(str, "[Events]", 8)) { | |
639 track->parser_priv->state = PST_EVENTS; | |
640 } else if (!strncmp(str, "[Fonts]", 7)) { | |
641 track->parser_priv->state = PST_FONTS; | |
642 } else { | |
643 switch (track->parser_priv->state) { | |
644 case PST_INFO: | |
645 process_info_line(track, str); | |
646 break; | |
647 case PST_STYLES: | |
648 process_styles_line(track, str); | |
649 break; | |
650 case PST_EVENTS: | |
651 process_events_line(track, str); | |
652 break; | |
653 case PST_FONTS: | |
654 process_fonts_line(track, str); | |
655 break; | |
656 default: | |
657 break; | |
18937 | 658 } |
19492 | 659 } |
660 | |
661 // there is no explicit end-of-font marker in ssa/ass | |
662 if ((track->parser_priv->state != PST_FONTS) && (track->parser_priv->fontname)) | |
663 decode_font(track); | |
664 | |
665 return 0; | |
666 } | |
667 | |
668 static int process_text(ass_track_t* track, char* str) | |
669 { | |
670 char* p = str; | |
671 while(1) { | |
672 char* q; | |
673 for (;((*p=='\r')||(*p=='\n'));++p) {} | |
674 for (q=p; ((*q!='\0')&&(*q!='\r')&&(*q!='\n')); ++q) {}; | |
675 if (q==p) | |
676 break; | |
677 if (*q != '\0') | |
678 *(q++) = '\0'; | |
679 process_line(track, p); | |
680 if (*q == '\0') | |
681 break; | |
682 p = q; | |
18937 | 683 } |
684 return 0; | |
685 } | |
686 | |
687 /** | |
688 * \brief Process CodecPrivate section of subtitle stream | |
689 * \param track track | |
690 * \param data string to parse | |
691 * \param size length of data | |
19492 | 692 CodecPrivate section contains [Stream Info] and [V4+ Styles] ([V4 Styles] for SSA) sections |
18937 | 693 */ |
19492 | 694 void ass_process_codec_private(ass_track_t* track, char *data, int size) |
18937 | 695 { |
696 char* str = malloc(size + 1); | |
697 int sid; | |
698 | |
699 memcpy(str, data, size); | |
700 str[size] = '\0'; | |
701 | |
19492 | 702 process_text(track, str); |
18937 | 703 free(str); |
704 | |
705 // add "Default" style to the end | |
706 // will be used if track does not contain a default style (or even does not contain styles at all) | |
707 sid = ass_alloc_style(track); | |
708 track->styles[sid].Name = strdup("Default"); | |
709 track->styles[sid].FontName = strdup("Arial"); | |
710 | |
711 if (!track->event_format) { | |
712 // probably an mkv produced by ancient mkvtoolnix | |
713 // such files don't have [Events] and Format: headers | |
19492 | 714 track->parser_priv->state = PST_EVENTS; |
18937 | 715 if (track->track_type == TRACK_TYPE_SSA) |
716 track->event_format = strdup("Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"); | |
717 else | |
718 track->event_format = strdup("Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text"); | |
719 } | |
19495 | 720 |
721 process_force_style(track); | |
18937 | 722 } |
723 | |
724 static int check_duplicate_event(ass_track_t* track, int ReadOrder) | |
725 { | |
726 int i; | |
727 for (i = 0; i<track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with | |
728 if (track->events[i].ReadOrder == ReadOrder) | |
729 return 1; | |
730 return 0; | |
731 } | |
732 | |
733 /** | |
734 * \brief Process a chunk of subtitle stream data. In matroska, this containes exactly 1 event (or a commentary) | |
735 * \param track track | |
736 * \param data string to parse | |
737 * \param size length of data | |
738 * \param timecode starting time of the event (milliseconds) | |
739 * \param duration duration of the event (milliseconds) | |
740 */ | |
19492 | 741 void ass_process_chunk(ass_track_t* track, char *data, int size, long long timecode, long long duration) |
18937 | 742 { |
743 char* str; | |
744 int eid; | |
745 char* p; | |
746 char* token; | |
747 ass_event_t* event; | |
748 | |
749 if (!track->event_format) { | |
750 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Event format header missing\n"); | |
751 return; | |
752 } | |
753 | |
754 str = malloc(size + 1); | |
755 memcpy(str, data, size); | |
756 str[size] = '\0'; | |
19378 | 757 mp_msg(MSGT_GLOBAL, MSGL_V, "event at %" PRId64 ", +%" PRId64 ": %s \n", (int64_t)timecode, (int64_t)duration, str); |
18937 | 758 |
759 eid = ass_alloc_event(track); | |
760 event = track->events + eid; | |
761 | |
762 p = str; | |
763 | |
764 do { | |
765 NEXT(p, token); | |
766 event->ReadOrder = atoi(token); | |
767 if (check_duplicate_event(track, event->ReadOrder)) | |
768 break; | |
769 | |
770 NEXT(p, token); | |
771 event->Layer = atoi(token); | |
772 | |
773 process_event_tail(track, event, p, 3); | |
774 | |
775 event->Start = timecode; | |
776 event->Duration = duration; | |
777 | |
778 free(str); | |
779 return; | |
780 // dump_events(tid); | |
781 } while (0); | |
782 // some error | |
19474
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
783 ass_free_event(track, eid); |
07209f48e527
Add public functions for removal of styles and events.
eugeni
parents:
19405
diff
changeset
|
784 track->n_events--; |
18937 | 785 free(str); |
786 } | |
787 | |
788 #ifdef USE_ICONV | |
789 /** \brief recode buffer to utf-8 | |
790 * constraint: sub_cp != 0 | |
791 * \param data pointer to text buffer | |
792 * \param size buffer size | |
793 * \return a pointer to recoded buffer, caller is responsible for freeing it | |
794 **/ | |
795 static char* sub_recode(char* data, size_t size) | |
796 { | |
797 static iconv_t icdsc = (iconv_t)(-1); | |
798 char* tocp = "UTF-8"; | |
799 char* outbuf; | |
800 assert(sub_cp); | |
801 | |
802 { | |
803 char* cp_tmp = sub_cp; | |
804 #ifdef HAVE_ENCA | |
805 char enca_lang[3], enca_fallback[100]; | |
806 if (sscanf(sub_cp, "enca:%2s:%99s", enca_lang, enca_fallback) == 2 | |
807 || sscanf(sub_cp, "ENCA:%2s:%99s", enca_lang, enca_fallback) == 2) { | |
808 cp_tmp = guess_buffer_cp((unsigned char*)data, size, enca_lang, enca_fallback); | |
809 } | |
810 #endif | |
811 if ((icdsc = iconv_open (tocp, cp_tmp)) != (iconv_t)(-1)){ | |
812 mp_msg(MSGT_SUBREADER,MSGL_V,"LIBSUB: opened iconv descriptor.\n"); | |
813 } else | |
814 mp_msg(MSGT_SUBREADER,MSGL_ERR,"LIBSUB: error opening iconv descriptor.\n"); | |
815 #ifdef HAVE_ENCA | |
816 if (cp_tmp) free(cp_tmp); | |
817 #endif | |
818 } | |
819 | |
820 { | |
821 size_t osize = size; | |
822 size_t ileft = size; | |
823 size_t oleft = size - 1; | |
824 char* ip; | |
825 char* op; | |
826 size_t rc; | |
827 | |
828 outbuf = malloc(size); | |
829 ip = data; | |
830 op = outbuf; | |
831 | |
832 while (ileft) { | |
833 rc = iconv(icdsc, &ip, &ileft, &op, &oleft); | |
834 if (rc == (size_t)(-1)) { | |
835 if (errno == E2BIG) { | |
836 int offset = op - outbuf; | |
837 outbuf = (char*)realloc(outbuf, osize + size); | |
838 op = outbuf + offset; | |
839 osize += size; | |
840 oleft += size; | |
841 } else { | |
842 mp_msg(MSGT_SUBREADER, MSGL_WARN, "LIBSUB: error recoding file.\n"); | |
843 return NULL; | |
844 } | |
845 } | |
846 } | |
847 outbuf[osize - oleft - 1] = 0; | |
848 } | |
849 | |
850 if (icdsc != (iconv_t)(-1)) { | |
851 (void)iconv_close(icdsc); | |
852 icdsc = (iconv_t)(-1); | |
853 mp_msg(MSGT_SUBREADER,MSGL_V,"LIBSUB: closed iconv descriptor.\n"); | |
854 } | |
855 | |
856 return outbuf; | |
857 } | |
858 #endif // ICONV | |
859 | |
860 /** | |
861 * \brief Read subtitles from file. | |
862 * \param fname file name | |
863 * \return newly allocated track | |
864 */ | |
865 ass_track_t* ass_read_file(char* fname) | |
866 { | |
867 int res; | |
868 long sz; | |
869 long bytes_read; | |
870 char* buf; | |
871 ass_track_t* track; | |
872 | |
873 FILE* fp = fopen(fname, "rb"); | |
874 if (!fp) { | |
875 mp_msg(MSGT_GLOBAL, MSGL_WARN, "ass_read_file(%s): fopen failed\n", fname); | |
876 return 0; | |
877 } | |
878 res = fseek(fp, 0, SEEK_END); | |
879 if (res == -1) { | |
880 mp_msg(MSGT_GLOBAL, MSGL_WARN, "ass_read_file(%s): fseek failed\n", fname); | |
881 fclose(fp); | |
882 return 0; | |
883 } | |
884 | |
885 sz = ftell(fp); | |
886 rewind(fp); | |
887 | |
888 if (sz > 10*1024*1024) { | |
889 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ass_read_file(%s): Refusing to load subtitles larger than 10M\n", fname); | |
890 fclose(fp); | |
891 return 0; | |
892 } | |
893 | |
894 mp_msg(MSGT_GLOBAL, MSGL_V, "file size: %ld\n", sz); | |
895 | |
896 buf = malloc(sz + 1); | |
897 assert(buf); | |
898 bytes_read = 0; | |
899 do { | |
900 res = fread(buf + bytes_read, 1, sz - bytes_read, fp); | |
901 if (res <= 0) { | |
902 mp_msg(MSGT_GLOBAL, MSGL_INFO, "Read failed, %d: %s\n", errno, strerror(errno)); | |
903 fclose(fp); | |
904 free(buf); | |
905 return 0; | |
906 } | |
907 bytes_read += res; | |
908 } while (sz - bytes_read > 0); | |
909 buf[sz] = '\0'; | |
910 fclose(fp); | |
911 | |
912 #ifdef USE_ICONV | |
913 if (sub_cp) { | |
914 char* tmpbuf = sub_recode(buf, sz); | |
915 free(buf); | |
916 if (!tmpbuf) | |
917 return 0; | |
918 buf = tmpbuf; | |
919 } | |
920 #endif | |
921 | |
922 track = ass_new_track(); | |
923 track->name = strdup(fname); | |
924 | |
925 // process header | |
19492 | 926 process_text(track, buf); |
927 | |
928 // there is no explicit end-of-font marker in ssa/ass | |
929 if (track->parser_priv->fontname) | |
930 decode_font(track); | |
931 | |
18937 | 932 free(buf); |
933 | |
19492 | 934 if (track->track_type == TRACK_TYPE_UNKNOWN) { |
18937 | 935 ass_free_track(track); |
936 return 0; | |
937 } | |
938 | |
19495 | 939 process_force_style(track); |
940 | |
18937 | 941 mp_msg(MSGT_GLOBAL, MSGL_INFO, "LIBASS: added subtitle file: %s (%d styles, %d events)\n", fname, track->n_styles, track->n_events); |
942 | |
943 sort_events(track); | |
944 | |
945 // dump_events(forced_tid); | |
946 return track; | |
947 } | |
948 | |
949 static char* validate_fname(char* name) | |
950 { | |
951 char* fname; | |
952 char* p; | |
953 char* q; | |
954 unsigned code; | |
955 int sz = strlen(name); | |
956 | |
957 q = fname = malloc(sz + 1); | |
958 p = name; | |
959 while (*p) { | |
960 code = utf8_get_char(&p); | |
961 if (code == 0) | |
962 break; | |
963 if ( (code > 0x7F) || | |
964 (code == '\\') || | |
965 (code == '/') || | |
966 (code == ':') || | |
967 (code == '*') || | |
968 (code == '?') || | |
969 (code == '<') || | |
970 (code == '>') || | |
971 (code == '|') || | |
972 (code == 0)) | |
973 { | |
974 *q++ = '_'; | |
975 } else { | |
976 *q++ = code; | |
977 } | |
978 if (p - name > sz) | |
979 break; | |
980 } | |
981 *q = 0; | |
982 return fname; | |
983 } | |
984 | |
985 /** | |
986 * \brief Process embedded matroska font. Saves it to ~/.mplayer/fonts. | |
987 * \param name attachment name | |
988 * \param data binary font data | |
989 * \param data_size data size | |
990 */ | |
991 void ass_process_font(const char* name, char* data, int data_size) | |
992 { | |
993 char buf[1000]; | |
994 FILE* fp = 0; | |
995 int rc; | |
996 struct stat st; | |
997 char* fname; | |
998 | |
999 char* fonts_dir = get_path("fonts"); | |
1000 rc = stat(fonts_dir, &st); | |
1001 if (rc) { | |
1002 int res; | |
1003 #ifndef __MINGW32__ | |
1004 res = mkdir(fonts_dir, 0700); | |
1005 #else | |
1006 res = mkdir(fonts_dir); | |
1007 #endif | |
1008 if (res) { | |
1009 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Failed to create: %s\n", fonts_dir); | |
1010 } | |
1011 } else if (!S_ISDIR(st.st_mode)) { | |
1012 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Not a directory: %s\n", fonts_dir); | |
1013 } | |
1014 | |
1015 fname = validate_fname((char*)name); | |
1016 | |
1017 snprintf(buf, 1000, "%s/%s", fonts_dir, fname); | |
1018 free(fname); | |
1019 free(fonts_dir); | |
1020 | |
1021 fp = fopen(buf, "wb"); | |
1022 if (!fp) return; | |
1023 | |
1024 fwrite(data, data_size, 1, fp); | |
1025 fclose(fp); | |
1026 } | |
1027 | |
1028 long long ass_step_sub(ass_track_t* track, long long now, int movement) { | |
1029 int i; | |
1030 | |
1031 if (movement == 0) return 0; | |
1032 if (track->n_events == 0) return 0; | |
1033 | |
1034 if (movement < 0) | |
1035 for (i = 0; (i < track->n_events) && ((long long)(track->events[i].Start + track->events[i].Duration) <= now); ++i) {} | |
1036 else | |
1037 for (i = track->n_events - 1; (i >= 0) && ((long long)(track->events[i].Start) > now); --i) {} | |
1038 | |
1039 // -1 and n_events are ok | |
1040 assert(i >= -1); assert(i <= track->n_events); | |
1041 i += movement; | |
1042 if (i < 0) i = 0; | |
1043 if (i >= track->n_events) i = track->n_events - 1; | |
1044 return ((long long)track->events[i].Start) - now; | |
1045 } | |
1046 | |
1047 ass_track_t* ass_new_track(void) { | |
1048 ass_track_t* track = calloc(1, sizeof(ass_track_t)); | |
19492 | 1049 track->parser_priv = calloc(1, sizeof(parser_priv_t)); |
18937 | 1050 return track; |
1051 } | |
1052 |