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>
38 #include "ass_utils.h"
39 #include "ass_library.h"
41 #define ass_atof(STR) (ass_strtod((STR),NULL))
59 #define ASS_STYLES_ALLOC 20
60 #define ASS_EVENTS_ALLOC 200
62 void ass_free_track(ASS_Track *track)
66 if (track->parser_priv) {
67 free(track->parser_priv->fontname);
68 free(track->parser_priv->fontdata);
69 free(track->parser_priv);
71 free(track->style_format);
72 free(track->event_format);
73 free(track->Language);
75 for (i = 0; i < track->n_styles; ++i)
76 ass_free_style(track, i);
80 for (i = 0; i < track->n_events; ++i)
81 ass_free_event(track, i);
88 /// \brief Allocate a new style struct
89 /// \param track track
91 int ass_alloc_style(ASS_Track *track)
95 assert(track->n_styles <= track->max_styles);
97 if (track->n_styles == track->max_styles) {
98 track->max_styles += ASS_STYLES_ALLOC;
100 (ASS_Style *) realloc(track->styles,
105 sid = track->n_styles++;
106 memset(track->styles + sid, 0, sizeof(ASS_Style));
110 /// \brief Allocate a new event struct
111 /// \param track track
113 int ass_alloc_event(ASS_Track *track)
117 assert(track->n_events <= track->max_events);
119 if (track->n_events == track->max_events) {
120 track->max_events += ASS_EVENTS_ALLOC;
122 (ASS_Event *) realloc(track->events,
127 eid = track->n_events++;
128 memset(track->events + eid, 0, sizeof(ASS_Event));
132 void ass_free_event(ASS_Track *track, int eid)
134 ASS_Event *event = track->events + eid;
139 free(event->render_priv);
142 void ass_free_style(ASS_Track *track, int sid)
144 ASS_Style *style = track->styles + sid;
147 free(style->FontName);
150 // ==============================================================================================
152 static void skip_spaces(char **str)
155 while ((*p == ' ') || (*p == '\t'))
160 static void rskip_spaces(char **str, char *limit)
163 while ((p >= limit) && ((*p == ' ') || (*p == '\t')))
169 * \brief Set up default style
170 * \param style style to edit to defaults
171 * The parameters are mostly taken directly from VSFilter source for
172 * best compatibility.
174 static void set_default_style(ASS_Style *style)
176 style->Name = strdup("Default");
177 style->FontName = strdup("Arial");
178 style->FontSize = 18;
179 style->PrimaryColour = 0xffffff00;
180 style->SecondaryColour = 0x00ffff00;
181 style->OutlineColour = 0x00000000;
182 style->BackColour = 0x00000080;
187 style->BorderStyle = 1;
190 style->Alignment = 2;
191 style->MarginL = style->MarginR = style->MarginV = 20;
194 static uint32_t string2color(ASS_Library *library, char *p)
197 (void) strtocolor(library, &p, &tmp, 0);
201 static long long string2timecode(ASS_Library *library, char *p)
203 unsigned h, m, s, ms;
205 int res = sscanf(p, "%1d:%2d:%2d.%2d", &h, &m, &s, &ms);
207 ass_msg(library, MSGL_WARN, "Bad timestamp");
210 tm = ((h * 60 + m) * 60 + s) * 1000 + ms * 10;
215 * \brief converts numpad-style align to align.
217 static int numpad2align(int val)
220 v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment
223 res = ((val - 1) % 3) + 1; // horizontal alignment
228 #define NEXT(str,token) \
229 token = next_token(&str); \
232 #define ANYVAL(name,func) \
233 } else if (strcasecmp(tname, #name) == 0) { \
234 target->name = func(token); \
235 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
237 #define STRVAL(name) \
238 } else if (strcasecmp(tname, #name) == 0) { \
239 if (target->name != NULL) free(target->name); \
240 target->name = strdup(token); \
241 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
243 #define COLORVAL(name) \
244 } else if (strcasecmp(tname, #name) == 0) { \
245 target->name = string2color(track->library, token); \
246 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
248 #define INTVAL(name) ANYVAL(name,atoi)
249 #define FPVAL(name) ANYVAL(name,ass_atof)
250 #define TIMEVAL(name) \
251 } else if (strcasecmp(tname, #name) == 0) { \
252 target->name = string2timecode(track->library, token); \
253 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
255 #define STYLEVAL(name) \
256 } else if (strcasecmp(tname, #name) == 0) { \
257 target->name = lookup_style(track, token); \
258 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
260 #define ALIAS(alias,name) \
261 if (strcasecmp(tname, #alias) == 0) {tname = #name;}
263 static char *next_token(char **str)
272 start = p; // start of the token
273 for (; (*p != '\0') && (*p != ','); ++p) {
276 *str = p; // eos found, str will point to '\0' at exit
279 *str = p + 1; // ',' found, str will point to the next char (beginning of the next token)
281 --p; // end of current token
282 rskip_spaces(&p, start);
284 p = start; // empty token
286 ++p; // the first space character, or '\0'
292 * \brief Parse the tail of Dialogue line
294 * \param event parsed data goes here
295 * \param str string to parse, zero-terminated
296 * \param n_ignored number of format options to skip at the beginning
298 static int process_event_tail(ASS_Track *track, ASS_Event *event,
299 char *str, int n_ignored)
305 ASS_Event *target = event;
307 char *format = strdup(track->event_format);
308 char *q = format; // format scanning pointer
310 if (track->n_styles == 0) {
311 // add "Default" style to the end
312 // will be used if track does not contain a default style (or even does not contain styles at all)
313 int sid = ass_alloc_style(track);
314 set_default_style(&track->styles[sid]);
315 track->default_style = sid;
318 for (i = 0; i < n_ignored; ++i) {
324 if (strcasecmp(tname, "Text") == 0) {
326 event->Text = strdup(p);
327 if (*event->Text != 0) {
328 last = event->Text + strlen(event->Text) - 1;
329 if (last >= event->Text && *last == '\r')
332 ass_msg(track->library, MSGL_DBG2, "Text = %s", event->Text);
333 event->Duration -= event->Start;
335 return 0; // "Text" is always the last
339 ALIAS(End, Duration) // temporarily store end timecode in event->Duration
357 * \brief Parse command line style overrides (--ass-force-style option)
358 * \param track track to apply overrides to
359 * The format for overrides is [StyleName.]Field=Value
361 void ass_process_force_style(ASS_Track *track)
363 char **fs, *eq, *dt, *style, *tname, *token;
366 char **list = track->library->style_overrides;
371 for (fs = list; *fs; ++fs) {
372 eq = strrchr(*fs, '=');
378 if (!strcasecmp(*fs, "PlayResX"))
379 track->PlayResX = atoi(token);
380 else if (!strcasecmp(*fs, "PlayResY"))
381 track->PlayResY = atoi(token);
382 else if (!strcasecmp(*fs, "Timer"))
383 track->Timer = ass_atof(token);
384 else if (!strcasecmp(*fs, "WrapStyle"))
385 track->WrapStyle = atoi(token);
386 else if (!strcasecmp(*fs, "ScaledBorderAndShadow"))
387 track->ScaledBorderAndShadow = parse_bool(token);
388 else if (!strcasecmp(*fs, "Kerning"))
389 track->Kerning = parse_bool(token);
391 dt = strrchr(*fs, '.');
400 for (sid = 0; sid < track->n_styles; ++sid) {
402 || strcasecmp(track->styles[sid].Name, style) == 0) {
403 target = track->styles + sid;
406 COLORVAL(PrimaryColour)
407 COLORVAL(SecondaryColour)
408 COLORVAL(OutlineColour)
438 * \brief Parse the Style line
440 * \param str string to parse, zero-terminated
441 * Allocates a new style struct.
443 static int process_style(ASS_Track *track, char *str)
450 char *q; // format scanning pointer
455 if (!track->style_format) {
456 // no style format header
457 // probably an ancient script version
458 if (track->track_type == TRACK_TYPE_SSA)
459 track->style_format =
461 ("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
462 "TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline,"
463 "Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding");
465 track->style_format =
467 ("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
468 "OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut,"
469 "ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow,"
470 "Alignment, MarginL, MarginR, MarginV, Encoding");
473 q = format = strdup(track->style_format);
475 // Add default style first
476 if (track->n_styles == 0) {
477 // will be used if track does not contain a default style (or even does not contain styles at all)
478 int sid = ass_alloc_style(track);
479 set_default_style(&track->styles[sid]);
480 track->default_style = sid;
483 ass_msg(track->library, MSGL_V, "[%p] Style: %s", track, str);
485 sid = ass_alloc_style(track);
487 style = track->styles + sid;
490 // fill style with some default values
491 style->ScaleX = 100.;
492 style->ScaleY = 100.;
500 if ((strcmp(target->Name, "Default") == 0)
501 || (strcmp(target->Name, "*Default") == 0))
502 track->default_style = sid;
504 COLORVAL(PrimaryColour)
505 COLORVAL(SecondaryColour)
506 COLORVAL(OutlineColour) // TertiaryColor
508 // SSA uses BackColour for both outline and shadow
509 // this will destroy SSA's TertiaryColour, but i'm not going to use it anyway
510 if (track->track_type == TRACK_TYPE_SSA)
511 target->OutlineColour = target->BackColour;
521 if (track->track_type == TRACK_TYPE_ASS)
522 target->Alignment = numpad2align(target->Alignment);
533 style->ScaleX /= 100.;
534 style->ScaleY /= 100.;
535 style->Bold = !!style->Bold;
536 style->Italic = !!style->Italic;
537 style->Underline = !!style->Underline;
539 style->Name = strdup("Default");
540 if (!style->FontName)
541 style->FontName = strdup("Arial");
547 static int process_styles_line(ASS_Track *track, char *str)
549 if (!strncmp(str, "Format:", 7)) {
552 track->style_format = strdup(p);
553 ass_msg(track->library, MSGL_DBG2, "Style format: %s",
554 track->style_format);
555 } else if (!strncmp(str, "Style:", 6)) {
558 process_style(track, p);
563 static int process_info_line(ASS_Track *track, char *str)
565 if (!strncmp(str, "PlayResX:", 9)) {
566 track->PlayResX = atoi(str + 9);
567 } else if (!strncmp(str, "PlayResY:", 9)) {
568 track->PlayResY = atoi(str + 9);
569 } else if (!strncmp(str, "Timer:", 6)) {
570 track->Timer = ass_atof(str + 6);
571 } else if (!strncmp(str, "WrapStyle:", 10)) {
572 track->WrapStyle = atoi(str + 10);
573 } else if (!strncmp(str, "ScaledBorderAndShadow:", 22)) {
574 track->ScaledBorderAndShadow = parse_bool(str + 22);
575 } else if (!strncmp(str, "Kerning:", 8)) {
576 track->Kerning = parse_bool(str + 8);
577 } else if (!strncmp(str, "Language:", 9)) {
579 while (*p && isspace(*p)) p++;
580 track->Language = malloc(3);
581 strncpy(track->Language, p, 2);
582 track->Language[2] = 0;
587 static void event_format_fallback(ASS_Track *track)
589 track->parser_priv->state = PST_EVENTS;
590 if (track->track_type == TRACK_TYPE_SSA)
591 track->event_format = strdup("Marked, Start, End, Style, "
592 "Name, MarginL, MarginR, MarginV, Effect, Text");
594 track->event_format = strdup("Layer, Start, End, Style, "
595 "Actor, MarginL, MarginR, MarginV, Effect, Text");
596 ass_msg(track->library, MSGL_V,
597 "No event format found, using fallback");
600 static int process_events_line(ASS_Track *track, char *str)
602 if (!strncmp(str, "Format:", 7)) {
605 free(track->event_format);
606 track->event_format = strdup(p);
607 ass_msg(track->library, MSGL_DBG2, "Event format: %s", track->event_format);
608 } else if (!strncmp(str, "Dialogue:", 9)) {
609 // This should never be reached for embedded subtitles.
610 // They have slightly different format and are parsed in ass_process_chunk,
611 // called directly from demuxer
618 eid = ass_alloc_event(track);
619 event = track->events + eid;
621 // We can't parse events with event_format
622 if (!track->event_format)
623 event_format_fallback(track);
625 process_event_tail(track, event, str, 0);
627 ass_msg(track->library, MSGL_V, "Not understood: '%.30s'", str);
632 // Copied from mkvtoolnix
633 static unsigned char *decode_chars(unsigned char c1, unsigned char c2,
634 unsigned char c3, unsigned char c4,
635 unsigned char *dst, int cnt)
638 unsigned char bytes[3];
642 ((c1 - 33) << 18) + ((c2 - 33) << 12) + ((c3 - 33) << 6) + (c4 -
644 bytes[2] = value & 0xff;
645 bytes[1] = (value & 0xff00) >> 8;
646 bytes[0] = (value & 0xff0000) >> 16;
648 for (i = 0; i < cnt; ++i)
653 static int decode_font(ASS_Track *track)
658 int size; // original size
659 int dsize; // decoded size
660 unsigned char *buf = 0;
662 ass_msg(track->library, MSGL_V, "Font: %d bytes encoded data",
663 track->parser_priv->fontdata_used);
664 size = track->parser_priv->fontdata_used;
666 ass_msg(track->library, MSGL_ERR, "Bad encoded data size");
667 goto error_decode_font;
669 buf = malloc(size / 4 * 3 + 2);
671 for (i = 0, p = (unsigned char *) track->parser_priv->fontdata;
672 i < size / 4; i++, p += 4) {
673 q = decode_chars(p[0], p[1], p[2], p[3], q, 3);
676 q = decode_chars(p[0], p[1], 0, 0, q, 1);
677 } else if (size % 4 == 3) {
678 q = decode_chars(p[0], p[1], p[2], 0, q, 2);
681 assert(dsize <= size / 4 * 3 + 2);
683 if (track->library->extract_fonts) {
684 ass_add_font(track->library, track->parser_priv->fontname,
685 (char *) buf, dsize);
690 free(track->parser_priv->fontname);
691 free(track->parser_priv->fontdata);
692 track->parser_priv->fontname = 0;
693 track->parser_priv->fontdata = 0;
694 track->parser_priv->fontdata_size = 0;
695 track->parser_priv->fontdata_used = 0;
699 static int process_fonts_line(ASS_Track *track, char *str)
703 if (!strncmp(str, "fontname:", 9)) {
706 if (track->parser_priv->fontname) {
709 track->parser_priv->fontname = strdup(p);
710 ass_msg(track->library, MSGL_V, "Fontname: %s",
711 track->parser_priv->fontname);
715 if (!track->parser_priv->fontname) {
716 ass_msg(track->library, MSGL_V, "Not understood: '%s'", str);
722 ass_msg(track->library, MSGL_WARN, "Font line too long: %d, %s",
726 if (track->parser_priv->fontdata_used + len >
727 track->parser_priv->fontdata_size) {
728 track->parser_priv->fontdata_size += 100 * 1024;
729 track->parser_priv->fontdata =
730 realloc(track->parser_priv->fontdata,
731 track->parser_priv->fontdata_size);
733 memcpy(track->parser_priv->fontdata + track->parser_priv->fontdata_used,
735 track->parser_priv->fontdata_used += len;
741 * \brief Parse a header line
743 * \param str string to parse, zero-terminated
745 static int process_line(ASS_Track *track, char *str)
747 if (!strncasecmp(str, "[Script Info]", 13)) {
748 track->parser_priv->state = PST_INFO;
749 } else if (!strncasecmp(str, "[V4 Styles]", 11)) {
750 track->parser_priv->state = PST_STYLES;
751 track->track_type = TRACK_TYPE_SSA;
752 } else if (!strncasecmp(str, "[V4+ Styles]", 12)) {
753 track->parser_priv->state = PST_STYLES;
754 track->track_type = TRACK_TYPE_ASS;
755 } else if (!strncasecmp(str, "[Events]", 8)) {
756 track->parser_priv->state = PST_EVENTS;
757 } else if (!strncasecmp(str, "[Fonts]", 7)) {
758 track->parser_priv->state = PST_FONTS;
760 switch (track->parser_priv->state) {
762 process_info_line(track, str);
765 process_styles_line(track, str);
768 process_events_line(track, str);
771 process_fonts_line(track, str);
778 // there is no explicit end-of-font marker in ssa/ass
779 if ((track->parser_priv->state != PST_FONTS)
780 && (track->parser_priv->fontname))
786 static int process_text(ASS_Track *track, char *str)
792 if ((*p == '\r') || (*p == '\n'))
794 else if (p[0] == '\xef' && p[1] == '\xbb' && p[2] == '\xbf')
795 p += 3; // U+FFFE (BOM)
799 for (q = p; ((*q != '\0') && (*q != '\r') && (*q != '\n')); ++q) {
805 process_line(track, p);
814 * \brief Process a chunk of subtitle stream data.
816 * \param data string to parse
817 * \param size length of data
819 void ass_process_data(ASS_Track *track, char *data, int size)
821 char *str = malloc(size + 1);
823 memcpy(str, data, size);
826 ass_msg(track->library, MSGL_V, "Event: %s", str);
827 process_text(track, str);
832 * \brief Process CodecPrivate section of subtitle stream
834 * \param data string to parse
835 * \param size length of data
836 CodecPrivate section contains [Stream Info] and [V4+ Styles] ([V4 Styles] for SSA) sections
838 void ass_process_codec_private(ASS_Track *track, char *data, int size)
840 ass_process_data(track, data, size);
842 // probably an mkv produced by ancient mkvtoolnix
843 // such files don't have [Events] and Format: headers
844 if (!track->event_format)
845 event_format_fallback(track);
847 ass_process_force_style(track);
850 static int check_duplicate_event(ASS_Track *track, int ReadOrder)
853 for (i = 0; i < track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
854 if (track->events[i].ReadOrder == ReadOrder)
860 * \brief Process a chunk of subtitle stream data. In Matroska, this contains exactly 1 event (or a commentary).
862 * \param data string to parse
863 * \param size length of data
864 * \param timecode starting time of the event (milliseconds)
865 * \param duration duration of the event (milliseconds)
867 void ass_process_chunk(ASS_Track *track, char *data, int size,
868 long long timecode, long long duration)
876 if (!track->event_format) {
877 ass_msg(track->library, MSGL_WARN, "Event format header missing");
881 str = malloc(size + 1);
882 memcpy(str, data, size);
884 ass_msg(track->library, MSGL_V, "Event at %" PRId64 ", +%" PRId64 ": %s",
885 (int64_t) timecode, (int64_t) duration, str);
887 eid = ass_alloc_event(track);
888 event = track->events + eid;
894 event->ReadOrder = atoi(token);
895 if (check_duplicate_event(track, event->ReadOrder))
899 event->Layer = atoi(token);
901 process_event_tail(track, event, p, 3);
903 event->Start = timecode;
904 event->Duration = duration;
911 ass_free_event(track, eid);
917 * \brief Flush buffered events.
920 void ass_flush_events(ASS_Track *track)
924 for (eid = 0; eid < track->n_events; eid++)
925 ass_free_event(track, eid);
931 /** \brief recode buffer to utf-8
932 * constraint: codepage != 0
933 * \param data pointer to text buffer
934 * \param size buffer size
935 * \return a pointer to recoded buffer, caller is responsible for freeing it
937 static char *sub_recode(ASS_Library *library, char *data, size_t size,
941 char *tocp = "UTF-8";
946 const char *cp_tmp = codepage;
948 char enca_lang[3], enca_fallback[100];
949 if (sscanf(codepage, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
950 || sscanf(codepage, "ENCA:%2s:%99s", enca_lang,
951 enca_fallback) == 2) {
953 ass_guess_buffer_cp(library, (unsigned char *) data, size,
954 enca_lang, enca_fallback);
957 if ((icdsc = iconv_open(tocp, cp_tmp)) != (iconv_t) (-1)) {
958 ass_msg(library, MSGL_V, "Opened iconv descriptor");
960 ass_msg(library, MSGL_ERR, "Error opening iconv descriptor");
966 size_t oleft = size - 1;
972 outbuf = malloc(osize);
978 rc = iconv(icdsc, &ip, &ileft, &op, &oleft);
979 else { // clear the conversion state and leave
981 rc = iconv(icdsc, NULL, NULL, &op, &oleft);
983 if (rc == (size_t) (-1)) {
984 if (errno == E2BIG) {
985 size_t offset = op - outbuf;
986 outbuf = (char *) realloc(outbuf, osize + size);
987 op = outbuf + offset;
991 ass_msg(library, MSGL_WARN, "Error recoding file");
999 outbuf[osize - oleft - 1] = 0;
1003 if (icdsc != (iconv_t) (-1)) {
1004 (void) iconv_close(icdsc);
1005 icdsc = (iconv_t) (-1);
1006 ass_msg(library, MSGL_V, "Closed iconv descriptor");
1014 * \brief read file contents into newly allocated buffer
1015 * \param fname file name
1016 * \param bufsize out: file size
1017 * \return pointer to file contents. Caller is responsible for its deallocation.
1019 static char *read_file(ASS_Library *library, char *fname, size_t *bufsize)
1026 FILE *fp = fopen(fname, "rb");
1028 ass_msg(library, MSGL_WARN,
1029 "ass_read_file(%s): fopen failed", fname);
1032 res = fseek(fp, 0, SEEK_END);
1034 ass_msg(library, MSGL_WARN,
1035 "ass_read_file(%s): fseek failed", fname);
1043 ass_msg(library, MSGL_V, "File size: %ld", sz);
1045 buf = malloc(sz + 1);
1049 res = fread(buf + bytes_read, 1, sz - bytes_read, fp);
1051 ass_msg(library, MSGL_INFO, "Read failed, %d: %s", errno,
1058 } while (sz - bytes_read > 0);
1068 * \param buf pointer to subtitle text in utf-8
1070 static ASS_Track *parse_memory(ASS_Library *library, char *buf)
1075 track = ass_new_track(library);
1078 process_text(track, buf);
1080 // external SSA/ASS subs does not have ReadOrder field
1081 for (i = 0; i < track->n_events; ++i)
1082 track->events[i].ReadOrder = i;
1084 // there is no explicit end-of-font marker in ssa/ass
1085 if (track->parser_priv->fontname)
1088 if (track->track_type == TRACK_TYPE_UNKNOWN) {
1089 ass_free_track(track);
1093 ass_process_force_style(track);
1099 * \brief Read subtitles from memory.
1100 * \param library libass library object
1101 * \param buf pointer to subtitles text
1102 * \param bufsize size of buffer
1103 * \param codepage recode buffer contents from given codepage
1104 * \return newly allocated track
1106 ASS_Track *ass_read_memory(ASS_Library *library, char *buf,
1107 size_t bufsize, char *codepage)
1117 buf = sub_recode(library, buf, bufsize, codepage);
1124 track = parse_memory(library, buf);
1130 ass_msg(library, MSGL_INFO, "Added subtitle file: "
1131 "<memory> (%d styles, %d events)",
1132 track->n_styles, track->n_events);
1136 static char *read_file_recode(ASS_Library *library, char *fname,
1137 char *codepage, size_t *size)
1142 buf = read_file(library, fname, &bufsize);
1147 char *tmpbuf = sub_recode(library, buf, bufsize, codepage);
1159 * \brief Read subtitles from file.
1160 * \param library libass library object
1161 * \param fname file name
1162 * \param codepage recode buffer contents from given codepage
1163 * \return newly allocated track
1165 ASS_Track *ass_read_file(ASS_Library *library, char *fname,
1172 buf = read_file_recode(library, fname, codepage, &bufsize);
1175 track = parse_memory(library, buf);
1180 track->name = strdup(fname);
1182 ass_msg(library, MSGL_INFO,
1183 "Added subtitle file: '%s' (%d styles, %d events)",
1184 fname, track->n_styles, track->n_events);
1190 * \brief read styles from file into already initialized track
1192 int ass_read_styles(ASS_Track *track, char *fname, char *codepage)
1195 ParserState old_state;
1198 buf = read_file(track->library, fname, &sz);
1204 tmpbuf = sub_recode(track->library, buf, sz, codepage);
1212 old_state = track->parser_priv->state;
1213 track->parser_priv->state = PST_STYLES;
1214 process_text(track, buf);
1215 track->parser_priv->state = old_state;
1220 long long ass_step_sub(ASS_Track *track, long long now, int movement)
1226 if (track->n_events == 0)
1231 (i < track->n_events)
1233 ((long long) (track->events[i].Start +
1234 track->events[i].Duration) <= now); ++i) {
1236 for (i = track->n_events - 1;
1237 (i >= 0) && ((long long) (track->events[i].Start) > now);
1241 // -1 and n_events are ok
1243 assert(i <= track->n_events);
1247 if (i >= track->n_events)
1248 i = track->n_events - 1;
1249 return ((long long) track->events[i].Start) - now;
1252 ASS_Track *ass_new_track(ASS_Library *library)
1254 ASS_Track *track = calloc(1, sizeof(ASS_Track));
1255 track->library = library;
1256 track->ScaledBorderAndShadow = 1;
1257 track->parser_priv = calloc(1, sizeof(ASS_ParserPriv));
1262 * \brief Prepare track for rendering
1264 void ass_lazy_track_init(ASS_Library *lib, ASS_Track *track)
1266 if (track->PlayResX && track->PlayResY)
1268 if (!track->PlayResX && !track->PlayResY) {
1269 ass_msg(lib, MSGL_WARN,
1270 "Neither PlayResX nor PlayResY defined. Assuming 384x288");
1271 track->PlayResX = 384;
1272 track->PlayResY = 288;
1274 if (!track->PlayResY && track->PlayResX == 1280) {
1275 track->PlayResY = 1024;
1276 ass_msg(lib, MSGL_WARN,
1277 "PlayResY undefined, setting to %d", track->PlayResY);
1278 } else if (!track->PlayResY) {
1279 track->PlayResY = track->PlayResX * 3 / 4;
1280 ass_msg(lib, MSGL_WARN,
1281 "PlayResY undefined, setting to %d", track->PlayResY);
1282 } else if (!track->PlayResX && track->PlayResY == 1024) {
1283 track->PlayResX = 1280;
1284 ass_msg(lib, MSGL_WARN,
1285 "PlayResX undefined, setting to %d", track->PlayResX);
1286 } else if (!track->PlayResX) {
1287 track->PlayResX = track->PlayResY * 4 / 3;
1288 ass_msg(lib, MSGL_WARN,
1289 "PlayResX undefined, setting to %d", track->PlayResX);