2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include <sys/types.h>
37 #include "ass_utils.h"
38 #include "ass_library.h"
40 #define ass_atof(STR) (ass_strtod((STR),NULL))
58 #define ASS_STYLES_ALLOC 20
59 #define ASS_EVENTS_ALLOC 200
61 void ass_free_track(ASS_Track
*track
)
65 if (track
->parser_priv
) {
66 free(track
->parser_priv
->fontname
);
67 free(track
->parser_priv
->fontdata
);
68 free(track
->parser_priv
);
70 free(track
->style_format
);
71 free(track
->event_format
);
73 for (i
= 0; i
< track
->n_styles
; ++i
)
74 ass_free_style(track
, i
);
78 for (i
= 0; i
< track
->n_events
; ++i
)
79 ass_free_event(track
, i
);
86 /// \brief Allocate a new style struct
87 /// \param track track
89 int ass_alloc_style(ASS_Track
*track
)
93 assert(track
->n_styles
<= track
->max_styles
);
95 if (track
->n_styles
== track
->max_styles
) {
96 track
->max_styles
+= ASS_STYLES_ALLOC
;
98 (ASS_Style
*) realloc(track
->styles
,
103 sid
= track
->n_styles
++;
104 memset(track
->styles
+ sid
, 0, sizeof(ASS_Style
));
108 /// \brief Allocate a new event struct
109 /// \param track track
111 int ass_alloc_event(ASS_Track
*track
)
115 assert(track
->n_events
<= track
->max_events
);
117 if (track
->n_events
== track
->max_events
) {
118 track
->max_events
+= ASS_EVENTS_ALLOC
;
120 (ASS_Event
*) realloc(track
->events
,
125 eid
= track
->n_events
++;
126 memset(track
->events
+ eid
, 0, sizeof(ASS_Event
));
130 void ass_free_event(ASS_Track
*track
, int eid
)
132 ASS_Event
*event
= track
->events
+ eid
;
137 free(event
->render_priv
);
140 void ass_free_style(ASS_Track
*track
, int sid
)
142 ASS_Style
*style
= track
->styles
+ sid
;
145 free(style
->FontName
);
148 // ==============================================================================================
150 static void skip_spaces(char **str
)
153 while ((*p
== ' ') || (*p
== '\t'))
158 static void rskip_spaces(char **str
, char *limit
)
161 while ((p
>= limit
) && ((*p
== ' ') || (*p
== '\t')))
167 * \brief find style by name
169 * \param name style name
170 * \return index in track->styles
171 * Returnes 0 if no styles found => expects at least 1 style.
172 * Parsing code always adds "Default" style in the end.
174 static int lookup_style(ASS_Track
*track
, char *name
)
178 ++name
; // FIXME: what does '*' really mean ?
179 for (i
= track
->n_styles
- 1; i
>= 0; --i
) {
180 // FIXME: mb strcasecmp ?
181 if (strcmp(track
->styles
[i
].Name
, name
) == 0)
184 i
= track
->default_style
;
185 ass_msg(track
->library
, MSGL_WARN
,
186 "[%p]: Warning: no style named '%s' found, using '%s'",
187 track
, name
, track
->styles
[i
].Name
);
188 return i
; // use the first style
191 static uint32_t string2color(ASS_Library
*library
, char *p
)
194 (void) strtocolor(library
, &p
, &tmp
, 0);
198 static long long string2timecode(ASS_Library
*library
, char *p
)
200 unsigned h
, m
, s
, ms
;
202 int res
= sscanf(p
, "%1d:%2d:%2d.%2d", &h
, &m
, &s
, &ms
);
204 ass_msg(library
, MSGL_WARN
, "Bad timestamp");
207 tm
= ((h
* 60 + m
) * 60 + s
) * 1000 + ms
* 10;
212 * \brief converts numpad-style align to align.
214 static int numpad2align(int val
)
217 v
= (val
- 1) / 3; // 0, 1 or 2 for vertical alignment
220 res
= ((val
- 1) % 3) + 1; // horizontal alignment
225 #define NEXT(str,token) \
226 token = next_token(&str); \
229 #define ANYVAL(name,func) \
230 } else if (strcasecmp(tname, #name) == 0) { \
231 target->name = func(token); \
232 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
234 #define STRVAL(name) \
235 } else if (strcasecmp(tname, #name) == 0) { \
236 if (target->name != NULL) free(target->name); \
237 target->name = strdup(token); \
238 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
240 #define COLORVAL(name) \
241 } else if (strcasecmp(tname, #name) == 0) { \
242 target->name = string2color(track->library, token); \
243 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
245 #define INTVAL(name) ANYVAL(name,atoi)
246 #define FPVAL(name) ANYVAL(name,ass_atof)
247 #define TIMEVAL(name) \
248 } else if (strcasecmp(tname, #name) == 0) { \
249 target->name = string2timecode(track->library, token); \
250 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
252 #define STYLEVAL(name) \
253 } else if (strcasecmp(tname, #name) == 0) { \
254 target->name = lookup_style(track, token); \
255 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
257 #define ALIAS(alias,name) \
258 if (strcasecmp(tname, #alias) == 0) {tname = #name;}
260 static char *next_token(char **str
)
269 start
= p
; // start of the token
270 for (; (*p
!= '\0') && (*p
!= ','); ++p
) {
273 *str
= p
; // eos found, str will point to '\0' at exit
276 *str
= p
+ 1; // ',' found, str will point to the next char (beginning of the next token)
278 --p
; // end of current token
279 rskip_spaces(&p
, start
);
281 p
= start
; // empty token
283 ++p
; // the first space character, or '\0'
289 * \brief Parse the tail of Dialogue line
291 * \param event parsed data goes here
292 * \param str string to parse, zero-terminated
293 * \param n_ignored number of format options to skip at the beginning
295 static int process_event_tail(ASS_Track
*track
, ASS_Event
*event
,
296 char *str
, int n_ignored
)
302 ASS_Event
*target
= event
;
304 char *format
= strdup(track
->event_format
);
305 char *q
= format
; // format scanning pointer
307 if (track
->n_styles
== 0) {
308 // add "Default" style to the end
309 // will be used if track does not contain a default style (or even does not contain styles at all)
310 int sid
= ass_alloc_style(track
);
311 track
->styles
[sid
].Name
= strdup("Default");
312 track
->styles
[sid
].FontName
= strdup("Arial");
315 for (i
= 0; i
< n_ignored
; ++i
) {
321 if (strcasecmp(tname
, "Text") == 0) {
323 event
->Text
= strdup(p
);
324 if (*event
->Text
!= 0) {
325 last
= event
->Text
+ strlen(event
->Text
) - 1;
326 if (last
>= event
->Text
&& *last
== '\r')
329 ass_msg(track
->library
, MSGL_DBG2
, "Text = %s", event
->Text
);
330 event
->Duration
-= event
->Start
;
332 return 0; // "Text" is always the last
336 ALIAS(End
, Duration
) // temporarily store end timecode in event->Duration
354 * \brief Parse command line style overrides (--ass-force-style option)
355 * \param track track to apply overrides to
356 * The format for overrides is [StyleName.]Field=Value
358 void ass_process_force_style(ASS_Track
*track
)
360 char **fs
, *eq
, *dt
, *style
, *tname
, *token
;
363 char **list
= track
->library
->style_overrides
;
368 for (fs
= list
; *fs
; ++fs
) {
369 eq
= strrchr(*fs
, '=');
375 if (!strcasecmp(*fs
, "PlayResX"))
376 track
->PlayResX
= atoi(token
);
377 else if (!strcasecmp(*fs
, "PlayResY"))
378 track
->PlayResY
= atoi(token
);
379 else if (!strcasecmp(*fs
, "Timer"))
380 track
->Timer
= ass_atof(token
);
381 else if (!strcasecmp(*fs
, "WrapStyle"))
382 track
->WrapStyle
= atoi(token
);
383 else if (!strcasecmp(*fs
, "ScaledBorderAndShadow"))
384 track
->ScaledBorderAndShadow
= parse_bool(token
);
385 else if (!strcasecmp(*fs
, "Kerning"))
386 track
->Kerning
= parse_bool(token
);
388 dt
= strrchr(*fs
, '.');
397 for (sid
= 0; sid
< track
->n_styles
; ++sid
) {
399 || strcasecmp(track
->styles
[sid
].Name
, style
) == 0) {
400 target
= track
->styles
+ sid
;
403 COLORVAL(PrimaryColour
)
404 COLORVAL(SecondaryColour
)
405 COLORVAL(OutlineColour
)
434 * \brief Parse the Style line
436 * \param str string to parse, zero-terminated
437 * Allocates a new style struct.
439 static int process_style(ASS_Track
*track
, char *str
)
446 char *q
; // format scanning pointer
451 if (!track
->style_format
) {
452 // no style format header
453 // probably an ancient script version
454 if (track
->track_type
== TRACK_TYPE_SSA
)
455 track
->style_format
=
457 ("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
458 "TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline,"
459 "Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding");
461 track
->style_format
=
463 ("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
464 "OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut,"
465 "ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow,"
466 "Alignment, MarginL, MarginR, MarginV, Encoding");
469 q
= format
= strdup(track
->style_format
);
471 ass_msg(track
->library
, MSGL_V
, "[%p] Style: %s", track
, str
);
473 sid
= ass_alloc_style(track
);
475 style
= track
->styles
+ sid
;
478 // fill style with some default values
479 style
->ScaleX
= 100.;
480 style
->ScaleY
= 100.;
488 if ((strcmp(target
->Name
, "Default") == 0)
489 || (strcmp(target
->Name
, "*Default") == 0))
490 track
->default_style
= sid
;
492 COLORVAL(PrimaryColour
)
493 COLORVAL(SecondaryColour
)
494 COLORVAL(OutlineColour
) // TertiaryColor
496 // SSA uses BackColour for both outline and shadow
497 // this will destroy SSA's TertiaryColour, but i'm not going to use it anyway
498 if (track
->track_type
== TRACK_TYPE_SSA
)
499 target
->OutlineColour
= target
->BackColour
;
509 if (track
->track_type
== TRACK_TYPE_ASS
)
510 target
->Alignment
= numpad2align(target
->Alignment
);
521 style
->ScaleX
/= 100.;
522 style
->ScaleY
/= 100.;
523 style
->Bold
= !!style
->Bold
;
524 style
->Italic
= !!style
->Italic
;
525 style
->Underline
= !!style
->Underline
;
527 style
->Name
= strdup("Default");
528 if (!style
->FontName
)
529 style
->FontName
= strdup("Arial");
535 static int process_styles_line(ASS_Track
*track
, char *str
)
537 if (!strncmp(str
, "Format:", 7)) {
540 track
->style_format
= strdup(p
);
541 ass_msg(track
->library
, MSGL_DBG2
, "Style format: %s",
542 track
->style_format
);
543 } else if (!strncmp(str
, "Style:", 6)) {
546 process_style(track
, p
);
551 static int process_info_line(ASS_Track
*track
, char *str
)
553 if (!strncmp(str
, "PlayResX:", 9)) {
554 track
->PlayResX
= atoi(str
+ 9);
555 } else if (!strncmp(str
, "PlayResY:", 9)) {
556 track
->PlayResY
= atoi(str
+ 9);
557 } else if (!strncmp(str
, "Timer:", 6)) {
558 track
->Timer
= ass_atof(str
+ 6);
559 } else if (!strncmp(str
, "WrapStyle:", 10)) {
560 track
->WrapStyle
= atoi(str
+ 10);
561 } else if (!strncmp(str
, "ScaledBorderAndShadow:", 22)) {
562 track
->ScaledBorderAndShadow
= parse_bool(str
+ 22);
563 } else if (!strncmp(str
, "Kerning:", 8)) {
564 track
->Kerning
= parse_bool(str
+ 8);
569 static void event_format_fallback(ASS_Track
*track
)
571 track
->parser_priv
->state
= PST_EVENTS
;
572 if (track
->track_type
== TRACK_TYPE_SSA
)
573 track
->event_format
= strdup("Format: Marked, Start, End, Style, "
574 "Name, MarginL, MarginR, MarginV, Effect, Text");
576 track
->event_format
= strdup("Format: Layer, Start, End, Style, "
577 "Actor, MarginL, MarginR, MarginV, Effect, Text");
578 ass_msg(track
->library
, MSGL_V
,
579 "No event format found, using fallback");
582 static int process_events_line(ASS_Track
*track
, char *str
)
584 if (!strncmp(str
, "Format:", 7)) {
587 free(track
->event_format
);
588 track
->event_format
= strdup(p
);
589 ass_msg(track
->library
, MSGL_DBG2
, "Event format: %s", track
->event_format
);
590 } else if (!strncmp(str
, "Dialogue:", 9)) {
591 // This should never be reached for embedded subtitles.
592 // They have slightly different format and are parsed in ass_process_chunk,
593 // called directly from demuxer
600 eid
= ass_alloc_event(track
);
601 event
= track
->events
+ eid
;
603 // We can't parse events with event_format
604 if (!track
->event_format
)
605 event_format_fallback(track
);
607 process_event_tail(track
, event
, str
, 0);
609 ass_msg(track
->library
, MSGL_V
, "Not understood: '%.30s'", str
);
614 // Copied from mkvtoolnix
615 static unsigned char *decode_chars(unsigned char c1
, unsigned char c2
,
616 unsigned char c3
, unsigned char c4
,
617 unsigned char *dst
, int cnt
)
620 unsigned char bytes
[3];
624 ((c1
- 33) << 18) + ((c2
- 33) << 12) + ((c3
- 33) << 6) + (c4
-
626 bytes
[2] = value
& 0xff;
627 bytes
[1] = (value
& 0xff00) >> 8;
628 bytes
[0] = (value
& 0xff0000) >> 16;
630 for (i
= 0; i
< cnt
; ++i
)
635 static int decode_font(ASS_Track
*track
)
640 int size
; // original size
641 int dsize
; // decoded size
642 unsigned char *buf
= 0;
644 ass_msg(track
->library
, MSGL_V
, "Font: %d bytes encoded data",
645 track
->parser_priv
->fontdata_used
);
646 size
= track
->parser_priv
->fontdata_used
;
648 ass_msg(track
->library
, MSGL_ERR
, "Bad encoded data size");
649 goto error_decode_font
;
651 buf
= malloc(size
/ 4 * 3 + 2);
653 for (i
= 0, p
= (unsigned char *) track
->parser_priv
->fontdata
;
654 i
< size
/ 4; i
++, p
+= 4) {
655 q
= decode_chars(p
[0], p
[1], p
[2], p
[3], q
, 3);
658 q
= decode_chars(p
[0], p
[1], 0, 0, q
, 1);
659 } else if (size
% 4 == 3) {
660 q
= decode_chars(p
[0], p
[1], p
[2], 0, q
, 2);
663 assert(dsize
<= size
/ 4 * 3 + 2);
665 if (track
->library
->extract_fonts
) {
666 ass_add_font(track
->library
, track
->parser_priv
->fontname
,
667 (char *) buf
, dsize
);
672 free(track
->parser_priv
->fontname
);
673 free(track
->parser_priv
->fontdata
);
674 track
->parser_priv
->fontname
= 0;
675 track
->parser_priv
->fontdata
= 0;
676 track
->parser_priv
->fontdata_size
= 0;
677 track
->parser_priv
->fontdata_used
= 0;
681 static int process_fonts_line(ASS_Track
*track
, char *str
)
685 if (!strncmp(str
, "fontname:", 9)) {
688 if (track
->parser_priv
->fontname
) {
691 track
->parser_priv
->fontname
= strdup(p
);
692 ass_msg(track
->library
, MSGL_V
, "Fontname: %s",
693 track
->parser_priv
->fontname
);
697 if (!track
->parser_priv
->fontname
) {
698 ass_msg(track
->library
, MSGL_V
, "Not understood: '%s'", str
);
704 ass_msg(track
->library
, MSGL_WARN
, "Font line too long: %d, %s",
708 if (track
->parser_priv
->fontdata_used
+ len
>
709 track
->parser_priv
->fontdata_size
) {
710 track
->parser_priv
->fontdata_size
+= 100 * 1024;
711 track
->parser_priv
->fontdata
=
712 realloc(track
->parser_priv
->fontdata
,
713 track
->parser_priv
->fontdata_size
);
715 memcpy(track
->parser_priv
->fontdata
+ track
->parser_priv
->fontdata_used
,
717 track
->parser_priv
->fontdata_used
+= len
;
723 * \brief Parse a header line
725 * \param str string to parse, zero-terminated
727 static int process_line(ASS_Track
*track
, char *str
)
729 if (!strncasecmp(str
, "[Script Info]", 13)) {
730 track
->parser_priv
->state
= PST_INFO
;
731 } else if (!strncasecmp(str
, "[V4 Styles]", 11)) {
732 track
->parser_priv
->state
= PST_STYLES
;
733 track
->track_type
= TRACK_TYPE_SSA
;
734 } else if (!strncasecmp(str
, "[V4+ Styles]", 12)) {
735 track
->parser_priv
->state
= PST_STYLES
;
736 track
->track_type
= TRACK_TYPE_ASS
;
737 } else if (!strncasecmp(str
, "[Events]", 8)) {
738 track
->parser_priv
->state
= PST_EVENTS
;
739 } else if (!strncasecmp(str
, "[Fonts]", 7)) {
740 track
->parser_priv
->state
= PST_FONTS
;
742 switch (track
->parser_priv
->state
) {
744 process_info_line(track
, str
);
747 process_styles_line(track
, str
);
750 process_events_line(track
, str
);
753 process_fonts_line(track
, str
);
760 // there is no explicit end-of-font marker in ssa/ass
761 if ((track
->parser_priv
->state
!= PST_FONTS
)
762 && (track
->parser_priv
->fontname
))
768 static int process_text(ASS_Track
*track
, char *str
)
774 if ((*p
== '\r') || (*p
== '\n'))
776 else if (p
[0] == '\xef' && p
[1] == '\xbb' && p
[2] == '\xbf')
777 p
+= 3; // U+FFFE (BOM)
781 for (q
= p
; ((*q
!= '\0') && (*q
!= '\r') && (*q
!= '\n')); ++q
) {
787 process_line(track
, p
);
796 * \brief Process a chunk of subtitle stream data.
798 * \param data string to parse
799 * \param size length of data
801 void ass_process_data(ASS_Track
*track
, char *data
, int size
)
803 char *str
= malloc(size
+ 1);
805 memcpy(str
, data
, size
);
808 ass_msg(track
->library
, MSGL_V
, "Event: %s", str
);
809 process_text(track
, str
);
814 * \brief Process CodecPrivate section of subtitle stream
816 * \param data string to parse
817 * \param size length of data
818 CodecPrivate section contains [Stream Info] and [V4+ Styles] ([V4 Styles] for SSA) sections
820 void ass_process_codec_private(ASS_Track
*track
, char *data
, int size
)
822 ass_process_data(track
, data
, size
);
824 // probably an mkv produced by ancient mkvtoolnix
825 // such files don't have [Events] and Format: headers
826 if (!track
->event_format
)
827 event_format_fallback(track
);
829 ass_process_force_style(track
);
832 static int check_duplicate_event(ASS_Track
*track
, int ReadOrder
)
835 for (i
= 0; i
< track
->n_events
- 1; ++i
) // ignoring last event, it is the one we are comparing with
836 if (track
->events
[i
].ReadOrder
== ReadOrder
)
842 * \brief Process a chunk of subtitle stream data. In Matroska, this contains exactly 1 event (or a commentary).
844 * \param data string to parse
845 * \param size length of data
846 * \param timecode starting time of the event (milliseconds)
847 * \param duration duration of the event (milliseconds)
849 void ass_process_chunk(ASS_Track
*track
, char *data
, int size
,
850 long long timecode
, long long duration
)
858 if (!track
->event_format
) {
859 ass_msg(track
->library
, MSGL_WARN
, "Event format header missing");
863 str
= malloc(size
+ 1);
864 memcpy(str
, data
, size
);
866 ass_msg(track
->library
, MSGL_V
, "Event at %" PRId64
", +%" PRId64
": %s",
867 (int64_t) timecode
, (int64_t) duration
, str
);
869 eid
= ass_alloc_event(track
);
870 event
= track
->events
+ eid
;
876 event
->ReadOrder
= atoi(token
);
877 if (check_duplicate_event(track
, event
->ReadOrder
))
881 event
->Layer
= atoi(token
);
883 process_event_tail(track
, event
, p
, 3);
885 event
->Start
= timecode
;
886 event
->Duration
= duration
;
893 ass_free_event(track
, eid
);
899 * \brief Flush buffered events.
902 void ass_flush_events(ASS_Track
*track
)
906 for (eid
= 0; eid
< track
->n_events
; eid
++)
907 ass_free_event(track
, eid
);
913 /** \brief recode buffer to utf-8
914 * constraint: codepage != 0
915 * \param data pointer to text buffer
916 * \param size buffer size
917 * \return a pointer to recoded buffer, caller is responsible for freeing it
919 static char *sub_recode(ASS_Library
*library
, char *data
, size_t size
,
923 char *tocp
= "UTF-8";
928 const char *cp_tmp
= codepage
;
930 char enca_lang
[3], enca_fallback
[100];
931 if (sscanf(codepage
, "enca:%2s:%99s", enca_lang
, enca_fallback
) == 2
932 || sscanf(codepage
, "ENCA:%2s:%99s", enca_lang
,
933 enca_fallback
) == 2) {
935 ass_guess_buffer_cp(library
, (unsigned char *) data
, size
,
936 enca_lang
, enca_fallback
);
939 if ((icdsc
= iconv_open(tocp
, cp_tmp
)) != (iconv_t
) (-1)) {
940 ass_msg(library
, MSGL_V
, "Opened iconv descriptor");
942 ass_msg(library
, MSGL_ERR
, "Error opening iconv descriptor");
948 size_t oleft
= size
- 1;
954 outbuf
= malloc(osize
);
960 rc
= iconv(icdsc
, &ip
, &ileft
, &op
, &oleft
);
961 else { // clear the conversion state and leave
963 rc
= iconv(icdsc
, NULL
, NULL
, &op
, &oleft
);
965 if (rc
== (size_t) (-1)) {
966 if (errno
== E2BIG
) {
967 size_t offset
= op
- outbuf
;
968 outbuf
= (char *) realloc(outbuf
, osize
+ size
);
969 op
= outbuf
+ offset
;
973 ass_msg(library
, MSGL_WARN
, "Error recoding file");
979 outbuf
[osize
- oleft
- 1] = 0;
982 if (icdsc
!= (iconv_t
) (-1)) {
983 (void) iconv_close(icdsc
);
984 icdsc
= (iconv_t
) (-1);
985 ass_msg(library
, MSGL_V
, "Closed iconv descriptor");
993 * \brief read file contents into newly allocated buffer
994 * \param fname file name
995 * \param bufsize out: file size
996 * \return pointer to file contents. Caller is responsible for its deallocation.
998 static char *read_file(ASS_Library
*library
, char *fname
, size_t *bufsize
)
1005 FILE *fp
= fopen(fname
, "rb");
1007 ass_msg(library
, MSGL_WARN
,
1008 "ass_read_file(%s): fopen failed", fname
);
1011 res
= fseek(fp
, 0, SEEK_END
);
1013 ass_msg(library
, MSGL_WARN
,
1014 "ass_read_file(%s): fseek failed", fname
);
1022 ass_msg(library
, MSGL_V
, "File size: %ld", sz
);
1024 buf
= malloc(sz
+ 1);
1028 res
= fread(buf
+ bytes_read
, 1, sz
- bytes_read
, fp
);
1030 ass_msg(library
, MSGL_INFO
, "Read failed, %d: %s", errno
,
1037 } while (sz
- bytes_read
> 0);
1047 * \param buf pointer to subtitle text in utf-8
1049 static ASS_Track
*parse_memory(ASS_Library
*library
, char *buf
)
1054 track
= ass_new_track(library
);
1057 process_text(track
, buf
);
1059 // external SSA/ASS subs does not have ReadOrder field
1060 for (i
= 0; i
< track
->n_events
; ++i
)
1061 track
->events
[i
].ReadOrder
= i
;
1063 // there is no explicit end-of-font marker in ssa/ass
1064 if (track
->parser_priv
->fontname
)
1067 if (track
->track_type
== TRACK_TYPE_UNKNOWN
) {
1068 ass_free_track(track
);
1072 ass_process_force_style(track
);
1078 * \brief Read subtitles from memory.
1079 * \param library libass library object
1080 * \param buf pointer to subtitles text
1081 * \param bufsize size of buffer
1082 * \param codepage recode buffer contents from given codepage
1083 * \return newly allocated track
1085 ASS_Track
*ass_read_memory(ASS_Library
*library
, char *buf
,
1086 size_t bufsize
, char *codepage
)
1096 buf
= sub_recode(library
, buf
, bufsize
, codepage
);
1103 track
= parse_memory(library
, buf
);
1109 ass_msg(library
, MSGL_INFO
, "Added subtitle file: "
1110 "<memory> (%d styles, %d events)",
1111 track
->n_styles
, track
->n_events
);
1115 static char *read_file_recode(ASS_Library
*library
, char *fname
,
1116 char *codepage
, size_t *size
)
1121 buf
= read_file(library
, fname
, &bufsize
);
1126 char *tmpbuf
= sub_recode(library
, buf
, bufsize
, codepage
);
1138 * \brief Read subtitles from file.
1139 * \param library libass library object
1140 * \param fname file name
1141 * \param codepage recode buffer contents from given codepage
1142 * \return newly allocated track
1144 ASS_Track
*ass_read_file(ASS_Library
*library
, char *fname
,
1151 buf
= read_file_recode(library
, fname
, codepage
, &bufsize
);
1154 track
= parse_memory(library
, buf
);
1159 track
->name
= strdup(fname
);
1161 ass_msg(library
, MSGL_INFO
,
1162 "Added subtitle file: '%s' (%d styles, %d events)",
1163 fname
, track
->n_styles
, track
->n_events
);
1169 * \brief read styles from file into already initialized track
1171 int ass_read_styles(ASS_Track
*track
, char *fname
, char *codepage
)
1174 ParserState old_state
;
1177 buf
= read_file(track
->library
, fname
, &sz
);
1183 tmpbuf
= sub_recode(track
->library
, buf
, sz
, codepage
);
1191 old_state
= track
->parser_priv
->state
;
1192 track
->parser_priv
->state
= PST_STYLES
;
1193 process_text(track
, buf
);
1194 track
->parser_priv
->state
= old_state
;
1199 long long ass_step_sub(ASS_Track
*track
, long long now
, int movement
)
1205 if (track
->n_events
== 0)
1210 (i
< track
->n_events
)
1212 ((long long) (track
->events
[i
].Start
+
1213 track
->events
[i
].Duration
) <= now
); ++i
) {
1215 for (i
= track
->n_events
- 1;
1216 (i
>= 0) && ((long long) (track
->events
[i
].Start
) > now
);
1220 // -1 and n_events are ok
1222 assert(i
<= track
->n_events
);
1226 if (i
>= track
->n_events
)
1227 i
= track
->n_events
- 1;
1228 return ((long long) track
->events
[i
].Start
) - now
;
1231 ASS_Track
*ass_new_track(ASS_Library
*library
)
1233 ASS_Track
*track
= calloc(1, sizeof(ASS_Track
));
1234 track
->library
= library
;
1235 track
->ScaledBorderAndShadow
= 1;
1236 track
->parser_priv
= calloc(1, sizeof(ASS_ParserPriv
));