2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
6 * libass is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * libass is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with libass; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/types.h>
38 #include "ass_utils.h"
39 #include "ass_library.h"
57 #define ASS_STYLES_ALLOC 20
58 #define ASS_EVENTS_ALLOC 200
60 void ass_free_track(ASS_Track
*track
)
64 if (track
->parser_priv
) {
65 if (track
->parser_priv
->fontname
)
66 free(track
->parser_priv
->fontname
);
67 if (track
->parser_priv
->fontdata
)
68 free(track
->parser_priv
->fontdata
);
69 free(track
->parser_priv
);
71 if (track
->style_format
)
72 free(track
->style_format
);
73 if (track
->event_format
)
74 free(track
->event_format
);
76 for (i
= 0; i
< track
->n_styles
; ++i
)
77 ass_free_style(track
, i
);
81 for (i
= 0; i
< track
->n_events
; ++i
)
82 ass_free_event(track
, i
);
89 /// \brief Allocate a new style struct
90 /// \param track track
92 int ass_alloc_style(ASS_Track
*track
)
96 assert(track
->n_styles
<= track
->max_styles
);
98 if (track
->n_styles
== track
->max_styles
) {
99 track
->max_styles
+= ASS_STYLES_ALLOC
;
101 (ASS_Style
*) realloc(track
->styles
,
106 sid
= track
->n_styles
++;
107 memset(track
->styles
+ sid
, 0, sizeof(ASS_Style
));
111 /// \brief Allocate a new event struct
112 /// \param track track
114 int ass_alloc_event(ASS_Track
*track
)
118 assert(track
->n_events
<= track
->max_events
);
120 if (track
->n_events
== track
->max_events
) {
121 track
->max_events
+= ASS_EVENTS_ALLOC
;
123 (ASS_Event
*) realloc(track
->events
,
128 eid
= track
->n_events
++;
129 memset(track
->events
+ eid
, 0, sizeof(ASS_Event
));
133 void ass_free_event(ASS_Track
*track
, int eid
)
135 ASS_Event
*event
= track
->events
+ eid
;
142 if (event
->render_priv
)
143 free(event
->render_priv
);
146 void ass_free_style(ASS_Track
*track
, int sid
)
148 ASS_Style
*style
= track
->styles
+ sid
;
152 free(style
->FontName
);
155 // ==============================================================================================
157 static void skip_spaces(char **str
)
160 while ((*p
== ' ') || (*p
== '\t'))
165 static void rskip_spaces(char **str
, char *limit
)
168 while ((p
>= limit
) && ((*p
== ' ') || (*p
== '\t')))
174 * \brief find style by name
176 * \param name style name
177 * \return index in track->styles
178 * Returnes 0 if no styles found => expects at least 1 style.
179 * Parsing code always adds "Default" style in the end.
181 static int lookup_style(ASS_Track
*track
, char *name
)
185 ++name
; // FIXME: what does '*' really mean ?
186 for (i
= track
->n_styles
- 1; i
>= 0; --i
) {
187 // FIXME: mb strcasecmp ?
188 if (strcmp(track
->styles
[i
].Name
, name
) == 0)
191 i
= track
->default_style
;
192 ass_msg(track
->library
, MSGL_WARN
,
193 "[%p]: Warning: no style named '%s' found, using '%s'",
194 track
, name
, track
->styles
[i
].Name
);
195 return i
; // use the first style
198 static uint32_t string2color(ASS_Library
*library
, char *p
)
201 (void) strtocolor(library
, &p
, &tmp
, 0);
205 static long long string2timecode(ASS_Library
*library
, char *p
)
207 unsigned h
, m
, s
, ms
;
209 int res
= sscanf(p
, "%1d:%2d:%2d.%2d", &h
, &m
, &s
, &ms
);
211 ass_msg(library
, MSGL_WARN
, "Bad timestamp");
214 tm
= ((h
* 60 + m
) * 60 + s
) * 1000 + ms
* 10;
219 * \brief converts numpad-style align to align.
221 static int numpad2align(int val
)
224 v
= (val
- 1) / 3; // 0, 1 or 2 for vertical alignment
227 res
= ((val
- 1) % 3) + 1; // horizontal alignment
232 #define NEXT(str,token) \
233 token = next_token(&str); \
236 #define ANYVAL(name,func) \
237 } else if (strcasecmp(tname, #name) == 0) { \
238 target->name = func(token); \
239 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
241 #define STRVAL(name) \
242 } else if (strcasecmp(tname, #name) == 0) { \
243 if (target->name != NULL) free(target->name); \
244 target->name = strdup(token); \
245 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
247 #define COLORVAL(name) \
248 } else if (strcasecmp(tname, #name) == 0) { \
249 target->name = string2color(track->library, token); \
250 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
252 #define INTVAL(name) ANYVAL(name,atoi)
253 #define FPVAL(name) ANYVAL(name,atof)
254 #define TIMEVAL(name) \
255 } else if (strcasecmp(tname, #name) == 0) { \
256 target->name = string2timecode(track->library, token); \
257 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
259 #define STYLEVAL(name) \
260 } else if (strcasecmp(tname, #name) == 0) { \
261 target->name = lookup_style(track, token); \
262 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
264 #define ALIAS(alias,name) \
265 if (strcasecmp(tname, #alias) == 0) {tname = #name;}
267 static char *next_token(char **str
)
276 start
= p
; // start of the token
277 for (; (*p
!= '\0') && (*p
!= ','); ++p
) {
280 *str
= p
; // eos found, str will point to '\0' at exit
283 *str
= p
+ 1; // ',' found, str will point to the next char (beginning of the next token)
285 --p
; // end of current token
286 rskip_spaces(&p
, start
);
288 p
= start
; // empty token
290 ++p
; // the first space character, or '\0'
296 * \brief Parse the tail of Dialogue line
298 * \param event parsed data goes here
299 * \param str string to parse, zero-terminated
300 * \param n_ignored number of format options to skip at the beginning
302 static int process_event_tail(ASS_Track
*track
, ASS_Event
*event
,
303 char *str
, int n_ignored
)
309 ASS_Event
*target
= event
;
311 char *format
= strdup(track
->event_format
);
312 char *q
= format
; // format scanning pointer
314 if (track
->n_styles
== 0) {
315 // add "Default" style to the end
316 // will be used if track does not contain a default style (or even does not contain styles at all)
317 int sid
= ass_alloc_style(track
);
318 track
->styles
[sid
].Name
= strdup("Default");
319 track
->styles
[sid
].FontName
= strdup("Arial");
322 for (i
= 0; i
< n_ignored
; ++i
) {
328 if (strcasecmp(tname
, "Text") == 0) {
330 event
->Text
= strdup(p
);
331 if (*event
->Text
!= 0) {
332 last
= event
->Text
+ strlen(event
->Text
) - 1;
333 if (last
>= event
->Text
&& *last
== '\r')
336 ass_msg(track
->library
, MSGL_DBG2
, "Text = %s", event
->Text
);
337 event
->Duration
-= event
->Start
;
339 return 0; // "Text" is always the last
343 ALIAS(End
, Duration
) // temporarily store end timecode in event->Duration
361 * \brief Parse command line style overrides (--ass-force-style option)
362 * \param track track to apply overrides to
363 * The format for overrides is [StyleName.]Field=Value
365 void ass_process_force_style(ASS_Track
*track
)
367 char **fs
, *eq
, *dt
, *style
, *tname
, *token
;
370 char **list
= track
->library
->style_overrides
;
375 for (fs
= list
; *fs
; ++fs
) {
376 eq
= strrchr(*fs
, '=');
382 if (!strcasecmp(*fs
, "PlayResX"))
383 track
->PlayResX
= atoi(token
);
384 else if (!strcasecmp(*fs
, "PlayResY"))
385 track
->PlayResY
= atoi(token
);
386 else if (!strcasecmp(*fs
, "Timer"))
387 track
->Timer
= atof(token
);
388 else if (!strcasecmp(*fs
, "WrapStyle"))
389 track
->WrapStyle
= atoi(token
);
390 else if (!strcasecmp(*fs
, "ScaledBorderAndShadow"))
391 track
->ScaledBorderAndShadow
= parse_bool(token
);
393 dt
= strrchr(*fs
, '.');
402 for (sid
= 0; sid
< track
->n_styles
; ++sid
) {
404 || strcasecmp(track
->styles
[sid
].Name
, style
) == 0) {
405 target
= track
->styles
+ sid
;
408 COLORVAL(PrimaryColour
)
409 COLORVAL(SecondaryColour
)
410 COLORVAL(OutlineColour
)
439 * \brief Parse the Style line
441 * \param str string to parse, zero-terminated
442 * Allocates a new style struct.
444 static int process_style(ASS_Track
*track
, char *str
)
451 char *q
; // format scanning pointer
456 if (!track
->style_format
) {
457 // no style format header
458 // probably an ancient script version
459 if (track
->track_type
== TRACK_TYPE_SSA
)
460 track
->style_format
=
462 ("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
463 "TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline,"
464 "Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding");
466 track
->style_format
=
468 ("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
469 "OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut,"
470 "ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow,"
471 "Alignment, MarginL, MarginR, MarginV, Encoding");
474 q
= format
= strdup(track
->style_format
);
476 ass_msg(track
->library
, MSGL_V
, "[%p] Style: %s", track
, str
);
478 sid
= ass_alloc_style(track
);
480 style
= track
->styles
+ sid
;
483 // fill style with some default values
484 style
->ScaleX
= 100.;
485 style
->ScaleY
= 100.;
493 if ((strcmp(target
->Name
, "Default") == 0)
494 || (strcmp(target
->Name
, "*Default") == 0))
495 track
->default_style
= sid
;
497 COLORVAL(PrimaryColour
)
498 COLORVAL(SecondaryColour
)
499 COLORVAL(OutlineColour
) // TertiaryColor
501 // SSA uses BackColour for both outline and shadow
502 // this will destroy SSA's TertiaryColour, but i'm not going to use it anyway
503 if (track
->track_type
== TRACK_TYPE_SSA
)
504 target
->OutlineColour
= target
->BackColour
;
514 if (track
->track_type
== TRACK_TYPE_ASS
)
515 target
->Alignment
= numpad2align(target
->Alignment
);
526 style
->ScaleX
/= 100.;
527 style
->ScaleY
/= 100.;
528 style
->Bold
= !!style
->Bold
;
529 style
->Italic
= !!style
->Italic
;
530 style
->Underline
= !!style
->Underline
;
532 style
->Name
= strdup("Default");
533 if (!style
->FontName
)
534 style
->FontName
= strdup("Arial");
535 // skip '@' at the start of the font name
536 if (*style
->FontName
== '@') {
538 style
->FontName
= strdup(p
+ 1);
546 static int process_styles_line(ASS_Track
*track
, char *str
)
548 if (!strncmp(str
, "Format:", 7)) {
551 track
->style_format
= strdup(p
);
552 ass_msg(track
->library
, MSGL_DBG2
, "Style format: %s",
553 track
->style_format
);
554 } else if (!strncmp(str
, "Style:", 6)) {
557 process_style(track
, p
);
562 static int process_info_line(ASS_Track
*track
, char *str
)
564 if (!strncmp(str
, "PlayResX:", 9)) {
565 track
->PlayResX
= atoi(str
+ 9);
566 } else if (!strncmp(str
, "PlayResY:", 9)) {
567 track
->PlayResY
= atoi(str
+ 9);
568 } else if (!strncmp(str
, "Timer:", 6)) {
569 track
->Timer
= atof(str
+ 6);
570 } else if (!strncmp(str
, "WrapStyle:", 10)) {
571 track
->WrapStyle
= atoi(str
+ 10);
572 } else if (!strncmp(str
, "ScaledBorderAndShadow:", 22)) {
573 track
->ScaledBorderAndShadow
= parse_bool(str
+ 22);
578 static void event_format_fallback(ASS_Track
*track
)
580 track
->parser_priv
->state
= PST_EVENTS
;
581 if (track
->track_type
== TRACK_TYPE_SSA
)
582 track
->event_format
= strdup("Format: Marked, Start, End, Style, "
583 "Name, MarginL, MarginR, MarginV, Effect, Text");
585 track
->event_format
= strdup("Format: Layer, Start, End, Style, "
586 "Actor, MarginL, MarginR, MarginV, Effect, Text");
587 ass_msg(track
->library
, MSGL_V
,
588 "No event format found, using fallback");
591 static int process_events_line(ASS_Track
*track
, char *str
)
593 if (!strncmp(str
, "Format:", 7)) {
596 track
->event_format
= strdup(p
);
597 ass_msg(track
->library
, MSGL_DBG2
, "Event format: %s", track
->event_format
);
598 } else if (!strncmp(str
, "Dialogue:", 9)) {
599 // This should never be reached for embedded subtitles.
600 // They have slightly different format and are parsed in ass_process_chunk,
601 // called directly from demuxer
608 eid
= ass_alloc_event(track
);
609 event
= track
->events
+ eid
;
611 // We can't parse events with event_format
612 if (!track
->event_format
)
613 event_format_fallback(track
);
615 process_event_tail(track
, event
, str
, 0);
617 ass_msg(track
->library
, MSGL_V
, "Not understood: '%s'", str
);
622 // Copied from mkvtoolnix
623 static unsigned char *decode_chars(unsigned char c1
, unsigned char c2
,
624 unsigned char c3
, unsigned char c4
,
625 unsigned char *dst
, int cnt
)
628 unsigned char bytes
[3];
632 ((c1
- 33) << 18) + ((c2
- 33) << 12) + ((c3
- 33) << 6) + (c4
-
634 bytes
[2] = value
& 0xff;
635 bytes
[1] = (value
& 0xff00) >> 8;
636 bytes
[0] = (value
& 0xff0000) >> 16;
638 for (i
= 0; i
< cnt
; ++i
)
643 static int decode_font(ASS_Track
*track
)
648 int size
; // original size
649 int dsize
; // decoded size
650 unsigned char *buf
= 0;
652 ass_msg(track
->library
, MSGL_V
, "Font: %d bytes encoded data",
653 track
->parser_priv
->fontdata_used
);
654 size
= track
->parser_priv
->fontdata_used
;
656 ass_msg(track
->library
, MSGL_ERR
, "Bad encoded data size");
657 goto error_decode_font
;
659 buf
= malloc(size
/ 4 * 3 + 2);
661 for (i
= 0, p
= (unsigned char *) track
->parser_priv
->fontdata
;
662 i
< size
/ 4; i
++, p
+= 4) {
663 q
= decode_chars(p
[0], p
[1], p
[2], p
[3], q
, 3);
666 q
= decode_chars(p
[0], p
[1], 0, 0, q
, 1);
667 } else if (size
% 4 == 3) {
668 q
= decode_chars(p
[0], p
[1], p
[2], 0, q
, 2);
671 assert(dsize
<= size
/ 4 * 3 + 2);
673 if (track
->library
->extract_fonts
) {
674 ass_add_font(track
->library
, track
->parser_priv
->fontname
,
675 (char *) buf
, dsize
);
682 free(track
->parser_priv
->fontname
);
683 free(track
->parser_priv
->fontdata
);
684 track
->parser_priv
->fontname
= 0;
685 track
->parser_priv
->fontdata
= 0;
686 track
->parser_priv
->fontdata_size
= 0;
687 track
->parser_priv
->fontdata_used
= 0;
691 static int process_fonts_line(ASS_Track
*track
, char *str
)
695 if (!strncmp(str
, "fontname:", 9)) {
698 if (track
->parser_priv
->fontname
) {
701 track
->parser_priv
->fontname
= strdup(p
);
702 ass_msg(track
->library
, MSGL_V
, "Fontname: %s",
703 track
->parser_priv
->fontname
);
707 if (!track
->parser_priv
->fontname
) {
708 ass_msg(track
->library
, MSGL_V
, "Not understood: '%s'", str
);
714 ass_msg(track
->library
, MSGL_WARN
, "Font line too long: %d, %s",
718 if (track
->parser_priv
->fontdata_used
+ len
>
719 track
->parser_priv
->fontdata_size
) {
720 track
->parser_priv
->fontdata_size
+= 100 * 1024;
721 track
->parser_priv
->fontdata
=
722 realloc(track
->parser_priv
->fontdata
,
723 track
->parser_priv
->fontdata_size
);
725 memcpy(track
->parser_priv
->fontdata
+ track
->parser_priv
->fontdata_used
,
727 track
->parser_priv
->fontdata_used
+= len
;
733 * \brief Parse a header line
735 * \param str string to parse, zero-terminated
737 static int process_line(ASS_Track
*track
, char *str
)
739 if (!strncasecmp(str
, "[Script Info]", 13)) {
740 track
->parser_priv
->state
= PST_INFO
;
741 } else if (!strncasecmp(str
, "[V4 Styles]", 11)) {
742 track
->parser_priv
->state
= PST_STYLES
;
743 track
->track_type
= TRACK_TYPE_SSA
;
744 } else if (!strncasecmp(str
, "[V4+ Styles]", 12)) {
745 track
->parser_priv
->state
= PST_STYLES
;
746 track
->track_type
= TRACK_TYPE_ASS
;
747 } else if (!strncasecmp(str
, "[Events]", 8)) {
748 track
->parser_priv
->state
= PST_EVENTS
;
749 } else if (!strncasecmp(str
, "[Fonts]", 7)) {
750 track
->parser_priv
->state
= PST_FONTS
;
752 switch (track
->parser_priv
->state
) {
754 process_info_line(track
, str
);
757 process_styles_line(track
, str
);
760 process_events_line(track
, str
);
763 process_fonts_line(track
, str
);
770 // there is no explicit end-of-font marker in ssa/ass
771 if ((track
->parser_priv
->state
!= PST_FONTS
)
772 && (track
->parser_priv
->fontname
))
778 static int process_text(ASS_Track
*track
, char *str
)
784 if ((*p
== '\r') || (*p
== '\n'))
786 else if (p
[0] == '\xef' && p
[1] == '\xbb' && p
[2] == '\xbf')
787 p
+= 3; // U+FFFE (BOM)
791 for (q
= p
; ((*q
!= '\0') && (*q
!= '\r') && (*q
!= '\n')); ++q
) {
797 process_line(track
, p
);
806 * \brief Process a chunk of subtitle stream data.
808 * \param data string to parse
809 * \param size length of data
811 void ass_process_data(ASS_Track
*track
, char *data
, int size
)
813 char *str
= malloc(size
+ 1);
815 memcpy(str
, data
, size
);
818 ass_msg(track
->library
, MSGL_V
, "Event: %s", str
);
819 process_text(track
, str
);
824 * \brief Process CodecPrivate section of subtitle stream
826 * \param data string to parse
827 * \param size length of data
828 CodecPrivate section contains [Stream Info] and [V4+ Styles] ([V4 Styles] for SSA) sections
830 void ass_process_codec_private(ASS_Track
*track
, char *data
, int size
)
832 ass_process_data(track
, data
, size
);
834 // probably an mkv produced by ancient mkvtoolnix
835 // such files don't have [Events] and Format: headers
836 if (!track
->event_format
)
837 event_format_fallback(track
);
839 ass_process_force_style(track
);
842 static int check_duplicate_event(ASS_Track
*track
, int ReadOrder
)
845 for (i
= 0; i
< track
->n_events
- 1; ++i
) // ignoring last event, it is the one we are comparing with
846 if (track
->events
[i
].ReadOrder
== ReadOrder
)
852 * \brief Process a chunk of subtitle stream data. In Matroska, this contains exactly 1 event (or a commentary).
854 * \param data string to parse
855 * \param size length of data
856 * \param timecode starting time of the event (milliseconds)
857 * \param duration duration of the event (milliseconds)
859 void ass_process_chunk(ASS_Track
*track
, char *data
, int size
,
860 long long timecode
, long long duration
)
868 if (!track
->event_format
) {
869 ass_msg(track
->library
, MSGL_WARN
, "Event format header missing");
873 str
= malloc(size
+ 1);
874 memcpy(str
, data
, size
);
876 ass_msg(track
->library
, MSGL_V
, "Event at %" PRId64
", +%" PRId64
": %s",
877 (int64_t) timecode
, (int64_t) duration
, str
);
879 eid
= ass_alloc_event(track
);
880 event
= track
->events
+ eid
;
886 event
->ReadOrder
= atoi(token
);
887 if (check_duplicate_event(track
, event
->ReadOrder
))
891 event
->Layer
= atoi(token
);
893 process_event_tail(track
, event
, p
, 3);
895 event
->Start
= timecode
;
896 event
->Duration
= duration
;
903 ass_free_event(track
, eid
);
909 /** \brief recode buffer to utf-8
910 * constraint: codepage != 0
911 * \param data pointer to text buffer
912 * \param size buffer size
913 * \return a pointer to recoded buffer, caller is responsible for freeing it
915 static char *sub_recode(ASS_Library
*library
, char *data
, size_t size
,
919 char *tocp
= "UTF-8";
924 const char *cp_tmp
= codepage
;
926 char enca_lang
[3], enca_fallback
[100];
927 if (sscanf(codepage
, "enca:%2s:%99s", enca_lang
, enca_fallback
) == 2
928 || sscanf(codepage
, "ENCA:%2s:%99s", enca_lang
,
929 enca_fallback
) == 2) {
931 ass_guess_buffer_cp(library
, (unsigned char *) data
, size
,
932 enca_lang
, enca_fallback
);
935 if ((icdsc
= iconv_open(tocp
, cp_tmp
)) != (iconv_t
) (-1)) {
936 ass_msg(library
, MSGL_V
, "Opened iconv descriptor");
938 ass_msg(library
, MSGL_ERR
, "Error opening iconv descriptor");
944 size_t oleft
= size
- 1;
950 outbuf
= malloc(osize
);
956 rc
= iconv(icdsc
, &ip
, &ileft
, &op
, &oleft
);
957 else { // clear the conversion state and leave
959 rc
= iconv(icdsc
, NULL
, NULL
, &op
, &oleft
);
961 if (rc
== (size_t) (-1)) {
962 if (errno
== E2BIG
) {
963 size_t offset
= op
- outbuf
;
964 outbuf
= (char *) realloc(outbuf
, osize
+ size
);
965 op
= outbuf
+ offset
;
969 ass_msg(library
, MSGL_WARN
, "Error recoding file");
975 outbuf
[osize
- oleft
- 1] = 0;
978 if (icdsc
!= (iconv_t
) (-1)) {
979 (void) iconv_close(icdsc
);
980 icdsc
= (iconv_t
) (-1);
981 ass_msg(library
, MSGL_V
, "Closed iconv descriptor");
989 * \brief read file contents into newly allocated buffer
990 * \param fname file name
991 * \param bufsize out: file size
992 * \return pointer to file contents. Caller is responsible for its deallocation.
994 static char *read_file(ASS_Library
*library
, char *fname
, size_t *bufsize
)
1001 FILE *fp
= fopen(fname
, "rb");
1003 ass_msg(library
, MSGL_WARN
,
1004 "ass_read_file(%s): fopen failed", fname
);
1007 res
= fseek(fp
, 0, SEEK_END
);
1009 ass_msg(library
, MSGL_WARN
,
1010 "ass_read_file(%s): fseek failed", fname
);
1018 if (sz
> 10 * 1024 * 1024) {
1019 ass_msg(library
, MSGL_INFO
,
1020 "ass_read_file(%s): Refusing to load subtitles "
1021 "larger than 10MiB", fname
);
1026 ass_msg(library
, MSGL_V
, "File size: %ld", sz
);
1028 buf
= malloc(sz
+ 1);
1032 res
= fread(buf
+ bytes_read
, 1, sz
- bytes_read
, fp
);
1034 ass_msg(library
, MSGL_INFO
, "Read failed, %d: %s", errno
,
1041 } while (sz
- bytes_read
> 0);
1051 * \param buf pointer to subtitle text in utf-8
1053 static ASS_Track
*parse_memory(ASS_Library
*library
, char *buf
)
1058 track
= ass_new_track(library
);
1061 process_text(track
, buf
);
1063 // external SSA/ASS subs does not have ReadOrder field
1064 for (i
= 0; i
< track
->n_events
; ++i
)
1065 track
->events
[i
].ReadOrder
= i
;
1067 // there is no explicit end-of-font marker in ssa/ass
1068 if (track
->parser_priv
->fontname
)
1071 if (track
->track_type
== TRACK_TYPE_UNKNOWN
) {
1072 ass_free_track(track
);
1076 ass_process_force_style(track
);
1082 * \brief Read subtitles from memory.
1083 * \param library libass library object
1084 * \param buf pointer to subtitles text
1085 * \param bufsize size of buffer
1086 * \param codepage recode buffer contents from given codepage
1087 * \return newly allocated track
1089 ASS_Track
*ass_read_memory(ASS_Library
*library
, char *buf
,
1090 size_t bufsize
, char *codepage
)
1100 buf
= sub_recode(library
, buf
, bufsize
, codepage
);
1106 track
= parse_memory(library
, buf
);
1112 ass_msg(library
, MSGL_INFO
, "Added subtitle file: "
1113 "<memory> (%d styles, %d events)",
1114 track
->n_styles
, track
->n_events
);
1118 static char *read_file_recode(ASS_Library
*library
, char *fname
,
1119 char *codepage
, size_t *size
)
1124 buf
= read_file(library
, fname
, &bufsize
);
1129 char *tmpbuf
= sub_recode(library
, buf
, bufsize
, codepage
);
1141 * \brief Read subtitles from file.
1142 * \param library libass library object
1143 * \param fname file name
1144 * \param codepage recode buffer contents from given codepage
1145 * \return newly allocated track
1147 ASS_Track
*ass_read_file(ASS_Library
*library
, char *fname
,
1154 buf
= read_file_recode(library
, fname
, codepage
, &bufsize
);
1157 track
= parse_memory(library
, buf
);
1162 track
->name
= strdup(fname
);
1164 ass_msg(library
, MSGL_INFO
,
1165 "Added subtitle file: '%s' (%d styles, %d events)",
1166 fname
, track
->n_styles
, track
->n_events
);
1172 * \brief read styles from file into already initialized track
1174 int ass_read_styles(ASS_Track
*track
, char *fname
, char *codepage
)
1177 ParserState old_state
;
1180 buf
= read_file(track
->library
, fname
, &sz
);
1186 tmpbuf
= sub_recode(track
->library
, buf
, sz
, codepage
);
1194 old_state
= track
->parser_priv
->state
;
1195 track
->parser_priv
->state
= PST_STYLES
;
1196 process_text(track
, buf
);
1197 track
->parser_priv
->state
= old_state
;
1202 long long ass_step_sub(ASS_Track
*track
, long long now
, int movement
)
1208 if (track
->n_events
== 0)
1213 (i
< track
->n_events
)
1215 ((long long) (track
->events
[i
].Start
+
1216 track
->events
[i
].Duration
) <= now
); ++i
) {
1218 for (i
= track
->n_events
- 1;
1219 (i
>= 0) && ((long long) (track
->events
[i
].Start
) > now
);
1223 // -1 and n_events are ok
1225 assert(i
<= track
->n_events
);
1229 if (i
>= track
->n_events
)
1230 i
= track
->n_events
- 1;
1231 return ((long long) track
->events
[i
].Start
) - now
;
1234 ASS_Track
*ass_new_track(ASS_Library
*library
)
1236 ASS_Track
*track
= calloc(1, sizeof(ASS_Track
));
1237 track
->library
= library
;
1238 track
->ScaledBorderAndShadow
= 1;
1239 track
->parser_priv
= calloc(1, sizeof(ASS_ParserPriv
));