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 Set up default style
168 * \param style style to edit to defaults
169 * The parameters are mostly taken directly from VSFilter source for
170 * best compatibility.
172 static void set_default_style(ASS_Style *style)
174 style->Name = strdup("Default");
175 style->FontName = strdup("Arial");
176 style->FontSize = 18;
177 style->PrimaryColour = 0xffffff00;
178 style->SecondaryColour = 0x00ffff00;
179 style->OutlineColour = 0x00000000;
180 style->BackColour = 0x00000080;
185 style->BorderStyle = 1;
188 style->Alignment = 2;
189 style->MarginL = style->MarginR = style->MarginV = 20;
193 * \brief find style by name
195 * \param name style name
196 * \return index in track->styles
197 * Returnes 0 if no styles found => expects at least 1 style.
198 * Parsing code always adds "Default" style in the end.
200 static int lookup_style(ASS_Track *track, char *name)
204 ++name; // FIXME: what does '*' really mean ?
205 for (i = track->n_styles - 1; i >= 0; --i) {
206 if (strcmp(track->styles[i].Name, name) == 0)
209 i = track->default_style;
210 ass_msg(track->library, MSGL_WARN,
211 "[%p]: Warning: no style named '%s' found, using '%s'",
212 track, name, track->styles[i].Name);
213 return i; // use the first style
216 static uint32_t string2color(ASS_Library *library, char *p)
219 (void) strtocolor(library, &p, &tmp, 0);
223 static long long string2timecode(ASS_Library *library, char *p)
225 unsigned h, m, s, ms;
227 int res = sscanf(p, "%1d:%2d:%2d.%2d", &h, &m, &s, &ms);
229 ass_msg(library, MSGL_WARN, "Bad timestamp");
232 tm = ((h * 60 + m) * 60 + s) * 1000 + ms * 10;
237 * \brief converts numpad-style align to align.
239 static int numpad2align(int val)
242 v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment
245 res = ((val - 1) % 3) + 1; // horizontal alignment
250 #define NEXT(str,token) \
251 token = next_token(&str); \
254 #define ANYVAL(name,func) \
255 } else if (strcasecmp(tname, #name) == 0) { \
256 target->name = func(token); \
257 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
259 #define STRVAL(name) \
260 } else if (strcasecmp(tname, #name) == 0) { \
261 if (target->name != NULL) free(target->name); \
262 target->name = strdup(token); \
263 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
265 #define COLORVAL(name) \
266 } else if (strcasecmp(tname, #name) == 0) { \
267 target->name = string2color(track->library, token); \
268 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
270 #define INTVAL(name) ANYVAL(name,atoi)
271 #define FPVAL(name) ANYVAL(name,ass_atof)
272 #define TIMEVAL(name) \
273 } else if (strcasecmp(tname, #name) == 0) { \
274 target->name = string2timecode(track->library, token); \
275 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
277 #define STYLEVAL(name) \
278 } else if (strcasecmp(tname, #name) == 0) { \
279 target->name = lookup_style(track, token); \
280 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
282 #define ALIAS(alias,name) \
283 if (strcasecmp(tname, #alias) == 0) {tname = #name;}
285 static char *next_token(char **str)
294 start = p; // start of the token
295 for (; (*p != '\0') && (*p != ','); ++p) {
298 *str = p; // eos found, str will point to '\0' at exit
301 *str = p + 1; // ',' found, str will point to the next char (beginning of the next token)
303 --p; // end of current token
304 rskip_spaces(&p, start);
306 p = start; // empty token
308 ++p; // the first space character, or '\0'
314 * \brief Parse the tail of Dialogue line
316 * \param event parsed data goes here
317 * \param str string to parse, zero-terminated
318 * \param n_ignored number of format options to skip at the beginning
320 static int process_event_tail(ASS_Track *track, ASS_Event *event,
321 char *str, int n_ignored)
327 ASS_Event *target = event;
329 char *format = strdup(track->event_format);
330 char *q = format; // format scanning pointer
332 if (track->n_styles == 0) {
333 // add "Default" style to the end
334 // will be used if track does not contain a default style (or even does not contain styles at all)
335 int sid = ass_alloc_style(track);
336 set_default_style(&track->styles[sid]);
337 track->default_style = sid;
340 for (i = 0; i < n_ignored; ++i) {
346 if (strcasecmp(tname, "Text") == 0) {
348 event->Text = strdup(p);
349 if (*event->Text != 0) {
350 last = event->Text + strlen(event->Text) - 1;
351 if (last >= event->Text && *last == '\r')
354 ass_msg(track->library, MSGL_DBG2, "Text = %s", event->Text);
355 event->Duration -= event->Start;
357 return 0; // "Text" is always the last
361 ALIAS(End, Duration) // temporarily store end timecode in event->Duration
379 * \brief Parse command line style overrides (--ass-force-style option)
380 * \param track track to apply overrides to
381 * The format for overrides is [StyleName.]Field=Value
383 void ass_process_force_style(ASS_Track *track)
385 char **fs, *eq, *dt, *style, *tname, *token;
388 char **list = track->library->style_overrides;
393 for (fs = list; *fs; ++fs) {
394 eq = strrchr(*fs, '=');
400 if (!strcasecmp(*fs, "PlayResX"))
401 track->PlayResX = atoi(token);
402 else if (!strcasecmp(*fs, "PlayResY"))
403 track->PlayResY = atoi(token);
404 else if (!strcasecmp(*fs, "Timer"))
405 track->Timer = ass_atof(token);
406 else if (!strcasecmp(*fs, "WrapStyle"))
407 track->WrapStyle = atoi(token);
408 else if (!strcasecmp(*fs, "ScaledBorderAndShadow"))
409 track->ScaledBorderAndShadow = parse_bool(token);
410 else if (!strcasecmp(*fs, "Kerning"))
411 track->Kerning = parse_bool(token);
413 dt = strrchr(*fs, '.');
422 for (sid = 0; sid < track->n_styles; ++sid) {
424 || strcasecmp(track->styles[sid].Name, style) == 0) {
425 target = track->styles + sid;
428 COLORVAL(PrimaryColour)
429 COLORVAL(SecondaryColour)
430 COLORVAL(OutlineColour)
459 * \brief Parse the Style line
461 * \param str string to parse, zero-terminated
462 * Allocates a new style struct.
464 static int process_style(ASS_Track *track, char *str)
471 char *q; // format scanning pointer
476 if (!track->style_format) {
477 // no style format header
478 // probably an ancient script version
479 if (track->track_type == TRACK_TYPE_SSA)
480 track->style_format =
482 ("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
483 "TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline,"
484 "Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding");
486 track->style_format =
488 ("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
489 "OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut,"
490 "ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow,"
491 "Alignment, MarginL, MarginR, MarginV, Encoding");
494 q = format = strdup(track->style_format);
496 // Add default style first
497 if (track->n_styles == 0) {
498 // will be used if track does not contain a default style (or even does not contain styles at all)
499 int sid = ass_alloc_style(track);
500 set_default_style(&track->styles[sid]);
501 track->default_style = sid;
504 ass_msg(track->library, MSGL_V, "[%p] Style: %s", track, str);
506 sid = ass_alloc_style(track);
508 style = track->styles + sid;
511 // fill style with some default values
512 style->ScaleX = 100.;
513 style->ScaleY = 100.;
521 if ((strcmp(target->Name, "Default") == 0)
522 || (strcmp(target->Name, "*Default") == 0))
523 track->default_style = sid;
525 COLORVAL(PrimaryColour)
526 COLORVAL(SecondaryColour)
527 COLORVAL(OutlineColour) // TertiaryColor
529 // SSA uses BackColour for both outline and shadow
530 // this will destroy SSA's TertiaryColour, but i'm not going to use it anyway
531 if (track->track_type == TRACK_TYPE_SSA)
532 target->OutlineColour = target->BackColour;
542 if (track->track_type == TRACK_TYPE_ASS)
543 target->Alignment = numpad2align(target->Alignment);
554 style->ScaleX /= 100.;
555 style->ScaleY /= 100.;
556 style->Bold = !!style->Bold;
557 style->Italic = !!style->Italic;
558 style->Underline = !!style->Underline;
560 style->Name = strdup("Default");
561 if (!style->FontName)
562 style->FontName = strdup("Arial");
568 static int process_styles_line(ASS_Track *track, char *str)
570 if (!strncmp(str, "Format:", 7)) {
573 track->style_format = strdup(p);
574 ass_msg(track->library, MSGL_DBG2, "Style format: %s",
575 track->style_format);
576 } else if (!strncmp(str, "Style:", 6)) {
579 process_style(track, p);
584 static int process_info_line(ASS_Track *track, char *str)
586 if (!strncmp(str, "PlayResX:", 9)) {
587 track->PlayResX = atoi(str + 9);
588 } else if (!strncmp(str, "PlayResY:", 9)) {
589 track->PlayResY = atoi(str + 9);
590 } else if (!strncmp(str, "Timer:", 6)) {
591 track->Timer = ass_atof(str + 6);
592 } else if (!strncmp(str, "WrapStyle:", 10)) {
593 track->WrapStyle = atoi(str + 10);
594 } else if (!strncmp(str, "ScaledBorderAndShadow:", 22)) {
595 track->ScaledBorderAndShadow = parse_bool(str + 22);
596 } else if (!strncmp(str, "Kerning:", 8)) {
597 track->Kerning = parse_bool(str + 8);
602 static void event_format_fallback(ASS_Track *track)
604 track->parser_priv->state = PST_EVENTS;
605 if (track->track_type == TRACK_TYPE_SSA)
606 track->event_format = strdup("Format: Marked, Start, End, Style, "
607 "Name, MarginL, MarginR, MarginV, Effect, Text");
609 track->event_format = strdup("Format: Layer, Start, End, Style, "
610 "Actor, MarginL, MarginR, MarginV, Effect, Text");
611 ass_msg(track->library, MSGL_V,
612 "No event format found, using fallback");
615 static int process_events_line(ASS_Track *track, char *str)
617 if (!strncmp(str, "Format:", 7)) {
620 free(track->event_format);
621 track->event_format = strdup(p);
622 ass_msg(track->library, MSGL_DBG2, "Event format: %s", track->event_format);
623 } else if (!strncmp(str, "Dialogue:", 9)) {
624 // This should never be reached for embedded subtitles.
625 // They have slightly different format and are parsed in ass_process_chunk,
626 // called directly from demuxer
633 eid = ass_alloc_event(track);
634 event = track->events + eid;
636 // We can't parse events with event_format
637 if (!track->event_format)
638 event_format_fallback(track);
640 process_event_tail(track, event, str, 0);
642 ass_msg(track->library, MSGL_V, "Not understood: '%.30s'", str);
647 // Copied from mkvtoolnix
648 static unsigned char *decode_chars(unsigned char c1, unsigned char c2,
649 unsigned char c3, unsigned char c4,
650 unsigned char *dst, int cnt)
653 unsigned char bytes[3];
657 ((c1 - 33) << 18) + ((c2 - 33) << 12) + ((c3 - 33) << 6) + (c4 -
659 bytes[2] = value & 0xff;
660 bytes[1] = (value & 0xff00) >> 8;
661 bytes[0] = (value & 0xff0000) >> 16;
663 for (i = 0; i < cnt; ++i)
668 static int decode_font(ASS_Track *track)
673 int size; // original size
674 int dsize; // decoded size
675 unsigned char *buf = 0;
677 ass_msg(track->library, MSGL_V, "Font: %d bytes encoded data",
678 track->parser_priv->fontdata_used);
679 size = track->parser_priv->fontdata_used;
681 ass_msg(track->library, MSGL_ERR, "Bad encoded data size");
682 goto error_decode_font;
684 buf = malloc(size / 4 * 3 + 2);
686 for (i = 0, p = (unsigned char *) track->parser_priv->fontdata;
687 i < size / 4; i++, p += 4) {
688 q = decode_chars(p[0], p[1], p[2], p[3], q, 3);
691 q = decode_chars(p[0], p[1], 0, 0, q, 1);
692 } else if (size % 4 == 3) {
693 q = decode_chars(p[0], p[1], p[2], 0, q, 2);
696 assert(dsize <= size / 4 * 3 + 2);
698 if (track->library->extract_fonts) {
699 ass_add_font(track->library, track->parser_priv->fontname,
700 (char *) buf, dsize);
705 free(track->parser_priv->fontname);
706 free(track->parser_priv->fontdata);
707 track->parser_priv->fontname = 0;
708 track->parser_priv->fontdata = 0;
709 track->parser_priv->fontdata_size = 0;
710 track->parser_priv->fontdata_used = 0;
714 static int process_fonts_line(ASS_Track *track, char *str)
718 if (!strncmp(str, "fontname:", 9)) {
721 if (track->parser_priv->fontname) {
724 track->parser_priv->fontname = strdup(p);
725 ass_msg(track->library, MSGL_V, "Fontname: %s",
726 track->parser_priv->fontname);
730 if (!track->parser_priv->fontname) {
731 ass_msg(track->library, MSGL_V, "Not understood: '%s'", str);
737 ass_msg(track->library, MSGL_WARN, "Font line too long: %d, %s",
741 if (track->parser_priv->fontdata_used + len >
742 track->parser_priv->fontdata_size) {
743 track->parser_priv->fontdata_size += 100 * 1024;
744 track->parser_priv->fontdata =
745 realloc(track->parser_priv->fontdata,
746 track->parser_priv->fontdata_size);
748 memcpy(track->parser_priv->fontdata + track->parser_priv->fontdata_used,
750 track->parser_priv->fontdata_used += len;
756 * \brief Parse a header line
758 * \param str string to parse, zero-terminated
760 static int process_line(ASS_Track *track, char *str)
762 if (!strncasecmp(str, "[Script Info]", 13)) {
763 track->parser_priv->state = PST_INFO;
764 } else if (!strncasecmp(str, "[V4 Styles]", 11)) {
765 track->parser_priv->state = PST_STYLES;
766 track->track_type = TRACK_TYPE_SSA;
767 } else if (!strncasecmp(str, "[V4+ Styles]", 12)) {
768 track->parser_priv->state = PST_STYLES;
769 track->track_type = TRACK_TYPE_ASS;
770 } else if (!strncasecmp(str, "[Events]", 8)) {
771 track->parser_priv->state = PST_EVENTS;
772 } else if (!strncasecmp(str, "[Fonts]", 7)) {
773 track->parser_priv->state = PST_FONTS;
775 switch (track->parser_priv->state) {
777 process_info_line(track, str);
780 process_styles_line(track, str);
783 process_events_line(track, str);
786 process_fonts_line(track, str);
793 // there is no explicit end-of-font marker in ssa/ass
794 if ((track->parser_priv->state != PST_FONTS)
795 && (track->parser_priv->fontname))
801 static int process_text(ASS_Track *track, char *str)
807 if ((*p == '\r') || (*p == '\n'))
809 else if (p[0] == '\xef' && p[1] == '\xbb' && p[2] == '\xbf')
810 p += 3; // U+FFFE (BOM)
814 for (q = p; ((*q != '\0') && (*q != '\r') && (*q != '\n')); ++q) {
820 process_line(track, p);
829 * \brief Process a chunk of subtitle stream data.
831 * \param data string to parse
832 * \param size length of data
834 void ass_process_data(ASS_Track *track, char *data, int size)
836 char *str = malloc(size + 1);
838 memcpy(str, data, size);
841 ass_msg(track->library, MSGL_V, "Event: %s", str);
842 process_text(track, str);
847 * \brief Process CodecPrivate section of subtitle stream
849 * \param data string to parse
850 * \param size length of data
851 CodecPrivate section contains [Stream Info] and [V4+ Styles] ([V4 Styles] for SSA) sections
853 void ass_process_codec_private(ASS_Track *track, char *data, int size)
855 ass_process_data(track, data, size);
857 // probably an mkv produced by ancient mkvtoolnix
858 // such files don't have [Events] and Format: headers
859 if (!track->event_format)
860 event_format_fallback(track);
862 ass_process_force_style(track);
865 static int check_duplicate_event(ASS_Track *track, int ReadOrder)
868 for (i = 0; i < track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
869 if (track->events[i].ReadOrder == ReadOrder)
875 * \brief Process a chunk of subtitle stream data. In Matroska, this contains exactly 1 event (or a commentary).
877 * \param data string to parse
878 * \param size length of data
879 * \param timecode starting time of the event (milliseconds)
880 * \param duration duration of the event (milliseconds)
882 void ass_process_chunk(ASS_Track *track, char *data, int size,
883 long long timecode, long long duration)
891 if (!track->event_format) {
892 ass_msg(track->library, MSGL_WARN, "Event format header missing");
896 str = malloc(size + 1);
897 memcpy(str, data, size);
899 ass_msg(track->library, MSGL_V, "Event at %" PRId64 ", +%" PRId64 ": %s",
900 (int64_t) timecode, (int64_t) duration, str);
902 eid = ass_alloc_event(track);
903 event = track->events + eid;
909 event->ReadOrder = atoi(token);
910 if (check_duplicate_event(track, event->ReadOrder))
914 event->Layer = atoi(token);
916 process_event_tail(track, event, p, 3);
918 event->Start = timecode;
919 event->Duration = duration;
926 ass_free_event(track, eid);
932 * \brief Flush buffered events.
935 void ass_flush_events(ASS_Track *track)
939 for (eid = 0; eid < track->n_events; eid++)
940 ass_free_event(track, eid);
946 /** \brief recode buffer to utf-8
947 * constraint: codepage != 0
948 * \param data pointer to text buffer
949 * \param size buffer size
950 * \return a pointer to recoded buffer, caller is responsible for freeing it
952 static char *sub_recode(ASS_Library *library, char *data, size_t size,
956 char *tocp = "UTF-8";
961 const char *cp_tmp = codepage;
963 char enca_lang[3], enca_fallback[100];
964 if (sscanf(codepage, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
965 || sscanf(codepage, "ENCA:%2s:%99s", enca_lang,
966 enca_fallback) == 2) {
968 ass_guess_buffer_cp(library, (unsigned char *) data, size,
969 enca_lang, enca_fallback);
972 if ((icdsc = iconv_open(tocp, cp_tmp)) != (iconv_t) (-1)) {
973 ass_msg(library, MSGL_V, "Opened iconv descriptor");
975 ass_msg(library, MSGL_ERR, "Error opening iconv descriptor");
981 size_t oleft = size - 1;
987 outbuf = malloc(osize);
993 rc = iconv(icdsc, &ip, &ileft, &op, &oleft);
994 else { // clear the conversion state and leave
996 rc = iconv(icdsc, NULL, NULL, &op, &oleft);
998 if (rc == (size_t) (-1)) {
999 if (errno == E2BIG) {
1000 size_t offset = op - outbuf;
1001 outbuf = (char *) realloc(outbuf, osize + size);
1002 op = outbuf + offset;
1006 ass_msg(library, MSGL_WARN, "Error recoding file");
1012 outbuf[osize - oleft - 1] = 0;
1015 if (icdsc != (iconv_t) (-1)) {
1016 (void) iconv_close(icdsc);
1017 icdsc = (iconv_t) (-1);
1018 ass_msg(library, MSGL_V, "Closed iconv descriptor");
1026 * \brief read file contents into newly allocated buffer
1027 * \param fname file name
1028 * \param bufsize out: file size
1029 * \return pointer to file contents. Caller is responsible for its deallocation.
1031 static char *read_file(ASS_Library *library, char *fname, size_t *bufsize)
1038 FILE *fp = fopen(fname, "rb");
1040 ass_msg(library, MSGL_WARN,
1041 "ass_read_file(%s): fopen failed", fname);
1044 res = fseek(fp, 0, SEEK_END);
1046 ass_msg(library, MSGL_WARN,
1047 "ass_read_file(%s): fseek failed", fname);
1055 ass_msg(library, MSGL_V, "File size: %ld", sz);
1057 buf = malloc(sz + 1);
1061 res = fread(buf + bytes_read, 1, sz - bytes_read, fp);
1063 ass_msg(library, MSGL_INFO, "Read failed, %d: %s", errno,
1070 } while (sz - bytes_read > 0);
1080 * \param buf pointer to subtitle text in utf-8
1082 static ASS_Track *parse_memory(ASS_Library *library, char *buf)
1087 track = ass_new_track(library);
1090 process_text(track, buf);
1092 // external SSA/ASS subs does not have ReadOrder field
1093 for (i = 0; i < track->n_events; ++i)
1094 track->events[i].ReadOrder = i;
1096 // there is no explicit end-of-font marker in ssa/ass
1097 if (track->parser_priv->fontname)
1100 if (track->track_type == TRACK_TYPE_UNKNOWN) {
1101 ass_free_track(track);
1105 ass_process_force_style(track);
1111 * \brief Read subtitles from memory.
1112 * \param library libass library object
1113 * \param buf pointer to subtitles text
1114 * \param bufsize size of buffer
1115 * \param codepage recode buffer contents from given codepage
1116 * \return newly allocated track
1118 ASS_Track *ass_read_memory(ASS_Library *library, char *buf,
1119 size_t bufsize, char *codepage)
1129 buf = sub_recode(library, buf, bufsize, codepage);
1136 track = parse_memory(library, buf);
1142 ass_msg(library, MSGL_INFO, "Added subtitle file: "
1143 "<memory> (%d styles, %d events)",
1144 track->n_styles, track->n_events);
1148 static char *read_file_recode(ASS_Library *library, char *fname,
1149 char *codepage, size_t *size)
1154 buf = read_file(library, fname, &bufsize);
1159 char *tmpbuf = sub_recode(library, buf, bufsize, codepage);
1171 * \brief Read subtitles from file.
1172 * \param library libass library object
1173 * \param fname file name
1174 * \param codepage recode buffer contents from given codepage
1175 * \return newly allocated track
1177 ASS_Track *ass_read_file(ASS_Library *library, char *fname,
1184 buf = read_file_recode(library, fname, codepage, &bufsize);
1187 track = parse_memory(library, buf);
1192 track->name = strdup(fname);
1194 ass_msg(library, MSGL_INFO,
1195 "Added subtitle file: '%s' (%d styles, %d events)",
1196 fname, track->n_styles, track->n_events);
1202 * \brief read styles from file into already initialized track
1204 int ass_read_styles(ASS_Track *track, char *fname, char *codepage)
1207 ParserState old_state;
1210 buf = read_file(track->library, fname, &sz);
1216 tmpbuf = sub_recode(track->library, buf, sz, codepage);
1224 old_state = track->parser_priv->state;
1225 track->parser_priv->state = PST_STYLES;
1226 process_text(track, buf);
1227 track->parser_priv->state = old_state;
1232 long long ass_step_sub(ASS_Track *track, long long now, int movement)
1238 if (track->n_events == 0)
1243 (i < track->n_events)
1245 ((long long) (track->events[i].Start +
1246 track->events[i].Duration) <= now); ++i) {
1248 for (i = track->n_events - 1;
1249 (i >= 0) && ((long long) (track->events[i].Start) > now);
1253 // -1 and n_events are ok
1255 assert(i <= track->n_events);
1259 if (i >= track->n_events)
1260 i = track->n_events - 1;
1261 return ((long long) track->events[i].Start) - now;
1264 ASS_Track *ass_new_track(ASS_Library *library)
1266 ASS_Track *track = calloc(1, sizeof(ASS_Track));
1267 track->library = library;
1268 track->ScaledBorderAndShadow = 1;
1269 track->parser_priv = calloc(1, sizeof(ASS_ParserPriv));
1274 * \brief Prepare track for rendering
1276 void ass_lazy_track_init(ASS_Library *lib, ASS_Track *track)
1278 if (track->PlayResX && track->PlayResY)
1280 if (!track->PlayResX && !track->PlayResY) {
1281 ass_msg(lib, MSGL_WARN,
1282 "Neither PlayResX nor PlayResY defined. Assuming 384x288");
1283 track->PlayResX = 384;
1284 track->PlayResY = 288;
1286 if (!track->PlayResY && track->PlayResX == 1280) {
1287 track->PlayResY = 1024;
1288 ass_msg(lib, MSGL_WARN,
1289 "PlayResY undefined, setting to %d", track->PlayResY);
1290 } else if (!track->PlayResY) {
1291 track->PlayResY = track->PlayResX * 3 / 4;
1292 ass_msg(lib, MSGL_WARN,
1293 "PlayResY undefined, setting to %d", track->PlayResY);
1294 } else if (!track->PlayResX && track->PlayResY == 1024) {
1295 track->PlayResX = 1280;
1296 ass_msg(lib, MSGL_WARN,
1297 "PlayResX undefined, setting to %d", track->PlayResX);
1298 } else if (!track->PlayResX) {
1299 track->PlayResX = track->PlayResY * 4 / 3;
1300 ass_msg(lib, MSGL_WARN,
1301 "PlayResX undefined, setting to %d", track->PlayResX);