annotate libass/ass.c @ 32332:7f082ae61e1c

Synced with help_mp-en.h rev. 32066
author jrash
date Thu, 30 Sep 2010 10:05:25 +0000
parents ac6e48baa03d
children 88eebbbbd6a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20008
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19905
diff changeset
1 /*
26723
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
3 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
4 * This file is part of libass.
26723
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
5 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
6 * libass is free software; you can redistribute it and/or modify
26723
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
7 * it under the terms of the GNU General Public License as published by
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
9 * (at your option) any later version.
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
10 *
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
11 * libass is distributed in the hope that it will be useful,
26723
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
14 * GNU General Public License for more details.
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
15 *
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
16 * You should have received a copy of the GNU General Public License along
26738
588ce97b44f2 Speak of libass instead of MPlayer in the libass license headers.
diego
parents: 26723
diff changeset
17 * with libass; if not, write to the Free Software Foundation, Inc.,
26723
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0f892cd714b2 Use standard license header.
diego
parents: 26593
diff changeset
19 */
20008
fa122b7c71c6 Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents: 19905
diff changeset
20
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
21 #include "config.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
22
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
23 #include <stdio.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
24 #include <stdlib.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
25 #include <string.h>
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
26 #include <strings.h>
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
27 #include <assert.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
28 #include <errno.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
29 #include <sys/types.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
30 #include <sys/stat.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
31 #include <unistd.h>
19374
acb26d9717da MinGW treats %lld as %d. Replace it with PRId64.
eugeni
parents: 18937
diff changeset
32 #include <inttypes.h>
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
33
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
34 #ifdef CONFIG_ICONV
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
35 #include <iconv.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
36 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
37
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
38 #include "ass.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
39 #include "ass_utils.h"
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
40 #include "ass_library.h"
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
41
31853
e64df5862cea Import libass 0.9.10
greg
parents: 31227
diff changeset
42 #define ass_atof(STR) (ass_strtod((STR),NULL))
e64df5862cea Import libass 0.9.10
greg
parents: 31227
diff changeset
43
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
44 typedef enum {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
45 PST_UNKNOWN = 0,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
46 PST_INFO,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
47 PST_STYLES,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
48 PST_EVENTS,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
49 PST_FONTS
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
50 } ParserState;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
51
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
52 struct parser_priv {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
53 ParserState state;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
54 char *fontname;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
55 char *fontdata;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
56 int fontdata_size;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
57 int fontdata_used;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
58 };
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
59
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
60 #define ASS_STYLES_ALLOC 20
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
61 #define ASS_EVENTS_ALLOC 200
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
62
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
63 void ass_free_track(ASS_Track *track)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
64 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
65 int i;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
66
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
67 if (track->parser_priv) {
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
68 free(track->parser_priv->fontname);
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
69 free(track->parser_priv->fontdata);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
70 free(track->parser_priv);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
71 }
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
72 free(track->style_format);
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
73 free(track->event_format);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
74 if (track->styles) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
75 for (i = 0; i < track->n_styles; ++i)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
76 ass_free_style(track, i);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
77 }
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
78 free(track->styles);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
79 if (track->events) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
80 for (i = 0; i < track->n_events; ++i)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
81 ass_free_event(track, i);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
82 }
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
83 free(track->events);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
84 free(track->name);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
85 free(track);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
86 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
87
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
88 /// \brief Allocate a new style struct
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
89 /// \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
90 /// \return style id
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
91 int ass_alloc_style(ASS_Track *track)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
92 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
93 int sid;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
94
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
95 assert(track->n_styles <= track->max_styles);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
96
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
97 if (track->n_styles == track->max_styles) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
98 track->max_styles += ASS_STYLES_ALLOC;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
99 track->styles =
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
100 (ASS_Style *) realloc(track->styles,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
101 sizeof(ASS_Style) *
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
102 track->max_styles);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
103 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
104
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
105 sid = track->n_styles++;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
106 memset(track->styles + sid, 0, sizeof(ASS_Style));
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
107 return sid;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
108 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
109
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
110 /// \brief Allocate a new event struct
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
111 /// \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
112 /// \return event id
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
113 int ass_alloc_event(ASS_Track *track)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
114 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
115 int eid;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
116
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
117 assert(track->n_events <= track->max_events);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
118
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
119 if (track->n_events == track->max_events) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
120 track->max_events += ASS_EVENTS_ALLOC;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
121 track->events =
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
122 (ASS_Event *) realloc(track->events,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
123 sizeof(ASS_Event) *
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
124 track->max_events);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
125 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
126
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
127 eid = track->n_events++;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
128 memset(track->events + eid, 0, sizeof(ASS_Event));
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
129 return eid;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
130 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
131
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
132 void ass_free_event(ASS_Track *track, int eid)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
133 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
134 ASS_Event *event = track->events + eid;
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
135
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
136 free(event->Name);
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
137 free(event->Effect);
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
138 free(event->Text);
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
139 free(event->render_priv);
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
140 }
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
141
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
142 void ass_free_style(ASS_Track *track, int sid)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
143 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
144 ASS_Style *style = track->styles + sid;
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
145
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
146 free(style->Name);
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
147 free(style->FontName);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
148 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
149
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
150 // ==============================================================================================
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
151
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
152 static void skip_spaces(char **str)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
153 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
154 char *p = *str;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
155 while ((*p == ' ') || (*p == '\t'))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
156 ++p;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
157 *str = p;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
158 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
159
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
160 static void rskip_spaces(char **str, char *limit)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
161 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
162 char *p = *str;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
163 while ((p >= limit) && ((*p == ' ') || (*p == '\t')))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
164 --p;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
165 *str = p;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
166 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
167
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
168 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
169 * \brief find style by name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
170 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
171 * \param name style name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
172 * \return index in track->styles
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
173 * Returnes 0 if no styles found => expects at least 1 style.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
174 * Parsing code always adds "Default" style in the end.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
175 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
176 static int lookup_style(ASS_Track *track, char *name)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
177 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
178 int i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
179 if (*name == '*')
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
180 ++name; // FIXME: what does '*' really mean ?
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
181 for (i = track->n_styles - 1; i >= 0; --i) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
182 // FIXME: mb strcasecmp ?
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
183 if (strcmp(track->styles[i].Name, name) == 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
184 return i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
185 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
186 i = track->default_style;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
187 ass_msg(track->library, MSGL_WARN,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
188 "[%p]: Warning: no style named '%s' found, using '%s'",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
189 track, name, track->styles[i].Name);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
190 return i; // use the first style
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
191 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
192
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
193 static uint32_t string2color(ASS_Library *library, char *p)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
194 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
195 uint32_t tmp;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
196 (void) strtocolor(library, &p, &tmp, 0);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
197 return tmp;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
198 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
199
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
200 static long long string2timecode(ASS_Library *library, char *p)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
201 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
202 unsigned h, m, s, ms;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
203 long long tm;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
204 int res = sscanf(p, "%1d:%2d:%2d.%2d", &h, &m, &s, &ms);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
205 if (res < 4) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
206 ass_msg(library, MSGL_WARN, "Bad timestamp");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
207 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
208 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
209 tm = ((h * 60 + m) * 60 + s) * 1000 + ms * 10;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
210 return tm;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
211 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
212
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
213 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
214 * \brief converts numpad-style align to align.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
215 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
216 static int numpad2align(int val)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
217 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
218 int res, v;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
219 v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
220 if (v != 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
221 v = 3 - v;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
222 res = ((val - 1) % 3) + 1; // horizontal alignment
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
223 res += v * 4;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
224 return res;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
225 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
226
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
227 #define NEXT(str,token) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
228 token = next_token(&str); \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
229 if (!token) break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
230
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
231 #define ANYVAL(name,func) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
232 } else if (strcasecmp(tname, #name) == 0) { \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
233 target->name = func(token); \
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
234 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
235
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
236 #define STRVAL(name) \
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
237 } else if (strcasecmp(tname, #name) == 0) { \
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
238 if (target->name != NULL) free(target->name); \
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
239 target->name = strdup(token); \
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
240 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
241
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
242 #define COLORVAL(name) \
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
243 } else if (strcasecmp(tname, #name) == 0) { \
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
244 target->name = string2color(track->library, token); \
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
245 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
246
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
247 #define INTVAL(name) ANYVAL(name,atoi)
31853
e64df5862cea Import libass 0.9.10
greg
parents: 31227
diff changeset
248 #define FPVAL(name) ANYVAL(name,ass_atof)
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
249 #define TIMEVAL(name) \
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
250 } else if (strcasecmp(tname, #name) == 0) { \
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
251 target->name = string2timecode(track->library, token); \
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
252 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
253
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
254 #define STYLEVAL(name) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
255 } else if (strcasecmp(tname, #name) == 0) { \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
256 target->name = lookup_style(track, token); \
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
257 ass_msg(track->library, MSGL_DBG2, "%s = %s", #name, token);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
258
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
259 #define ALIAS(alias,name) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
260 if (strcasecmp(tname, #alias) == 0) {tname = #name;}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
261
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
262 static char *next_token(char **str)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
263 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
264 char *p = *str;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
265 char *start;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
266 skip_spaces(&p);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
267 if (*p == '\0') {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
268 *str = p;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
269 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
270 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
271 start = p; // start of the token
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
272 for (; (*p != '\0') && (*p != ','); ++p) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
273 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
274 if (*p == '\0') {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
275 *str = p; // eos found, str will point to '\0' at exit
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
276 } else {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
277 *p = '\0';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
278 *str = p + 1; // ',' found, str will point to the next char (beginning of the next token)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
279 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
280 --p; // end of current token
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
281 rskip_spaces(&p, start);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
282 if (p < start)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
283 p = start; // empty token
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
284 else
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
285 ++p; // the first space character, or '\0'
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
286 *p = '\0';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
287 return start;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
288 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
289
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
290 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
291 * \brief Parse the tail of Dialogue line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
292 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
293 * \param event parsed data goes here
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
294 * \param str string to parse, zero-terminated
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
295 * \param n_ignored number of format options to skip at the beginning
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
296 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
297 static int process_event_tail(ASS_Track *track, ASS_Event *event,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
298 char *str, int n_ignored)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
299 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
300 char *token;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
301 char *tname;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
302 char *p = str;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
303 int i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
304 ASS_Event *target = event;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
305
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
306 char *format = strdup(track->event_format);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
307 char *q = format; // format scanning pointer
29472
ce53e67e8633 Fix crash when event format is not specified
greg
parents: 29263
diff changeset
308
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
309 if (track->n_styles == 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
310 // add "Default" style to the end
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
311 // will be used if track does not contain a default style (or even does not contain styles at all)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
312 int sid = ass_alloc_style(track);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
313 track->styles[sid].Name = strdup("Default");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
314 track->styles[sid].FontName = strdup("Arial");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
315 }
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
316
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
317 for (i = 0; i < n_ignored; ++i) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
318 NEXT(q, tname);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
319 }
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
320
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
321 while (1) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
322 NEXT(q, tname);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
323 if (strcasecmp(tname, "Text") == 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
324 char *last;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
325 event->Text = strdup(p);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
326 if (*event->Text != 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
327 last = event->Text + strlen(event->Text) - 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
328 if (last >= event->Text && *last == '\r')
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
329 *last = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
330 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
331 ass_msg(track->library, MSGL_DBG2, "Text = %s", event->Text);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
332 event->Duration -= event->Start;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
333 free(format);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
334 return 0; // "Text" is always the last
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
335 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
336 NEXT(p, token);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
337
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
338 ALIAS(End, Duration) // temporarily store end timecode in event->Duration
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
339 if (0) { // cool ;)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
340 INTVAL(Layer)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
341 STYLEVAL(Style)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
342 STRVAL(Name)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
343 STRVAL(Effect)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
344 INTVAL(MarginL)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
345 INTVAL(MarginR)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
346 INTVAL(MarginV)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
347 TIMEVAL(Start)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
348 TIMEVAL(Duration)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
349 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
350 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
351 free(format);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
352 return 1;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
353 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
354
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
355 /**
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
356 * \brief Parse command line style overrides (--ass-force-style option)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
357 * \param track track to apply overrides to
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
358 * The format for overrides is [StyleName.]Field=Value
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
359 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
360 void ass_process_force_style(ASS_Track *track)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
361 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
362 char **fs, *eq, *dt, *style, *tname, *token;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
363 ASS_Style *target;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
364 int sid;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
365 char **list = track->library->style_overrides;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
366
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
367 if (!list)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
368 return;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
369
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
370 for (fs = list; *fs; ++fs) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
371 eq = strrchr(*fs, '=');
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
372 if (!eq)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
373 continue;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
374 *eq = '\0';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
375 token = eq + 1;
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
376
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
377 if (!strcasecmp(*fs, "PlayResX"))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
378 track->PlayResX = atoi(token);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
379 else if (!strcasecmp(*fs, "PlayResY"))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
380 track->PlayResY = atoi(token);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
381 else if (!strcasecmp(*fs, "Timer"))
31853
e64df5862cea Import libass 0.9.10
greg
parents: 31227
diff changeset
382 track->Timer = ass_atof(token);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
383 else if (!strcasecmp(*fs, "WrapStyle"))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
384 track->WrapStyle = atoi(token);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
385 else if (!strcasecmp(*fs, "ScaledBorderAndShadow"))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
386 track->ScaledBorderAndShadow = parse_bool(token);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
387 else if (!strcasecmp(*fs, "Kerning"))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
388 track->Kerning = parse_bool(token);
25582
6b1d7568ae3d Allow overriding [Script Info] parameters with -ass-force-style option.
eugeni
parents: 25515
diff changeset
389
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
390 dt = strrchr(*fs, '.');
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
391 if (dt) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
392 *dt = '\0';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
393 style = *fs;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
394 tname = dt + 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
395 } else {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
396 style = NULL;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
397 tname = *fs;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
398 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
399 for (sid = 0; sid < track->n_styles; ++sid) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
400 if (style == NULL
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
401 || strcasecmp(track->styles[sid].Name, style) == 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
402 target = track->styles + sid;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
403 if (0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
404 STRVAL(FontName)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
405 COLORVAL(PrimaryColour)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
406 COLORVAL(SecondaryColour)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
407 COLORVAL(OutlineColour)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
408 COLORVAL(BackColour)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
409 FPVAL(FontSize)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
410 INTVAL(Bold)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
411 INTVAL(Italic)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
412 INTVAL(Underline)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
413 INTVAL(StrikeOut)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
414 FPVAL(Spacing)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
415 INTVAL(Angle)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
416 INTVAL(BorderStyle)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
417 INTVAL(Alignment)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
418 INTVAL(MarginL)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
419 INTVAL(MarginR)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
420 INTVAL(MarginV)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
421 INTVAL(Encoding)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
422 FPVAL(ScaleX)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
423 FPVAL(ScaleY)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
424 FPVAL(Outline)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
425 FPVAL(Shadow)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
426 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
427 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
428 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
429 *eq = '=';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
430 if (dt)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
431 *dt = '.';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
432 }
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
433 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
434
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
435 /**
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
436 * \brief Parse the Style line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
437 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
438 * \param str string to parse, zero-terminated
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
439 * Allocates a new style struct.
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
440 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
441 static int process_style(ASS_Track *track, char *str)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
442 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
443
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
444 char *token;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
445 char *tname;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
446 char *p = str;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
447 char *format;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
448 char *q; // format scanning pointer
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
449 int sid;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
450 ASS_Style *style;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
451 ASS_Style *target;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
452
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
453 if (!track->style_format) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
454 // no style format header
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
455 // probably an ancient script version
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
456 if (track->track_type == TRACK_TYPE_SSA)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
457 track->style_format =
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
458 strdup
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
459 ("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
460 "TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline,"
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
461 "Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
462 else
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
463 track->style_format =
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
464 strdup
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
465 ("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
466 "OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut,"
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
467 "ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow,"
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
468 "Alignment, MarginL, MarginR, MarginV, Encoding");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
469 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
470
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
471 q = format = strdup(track->style_format);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
472
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
473 ass_msg(track->library, MSGL_V, "[%p] Style: %s", track, str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
474
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
475 sid = ass_alloc_style(track);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
476
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
477 style = track->styles + sid;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
478 target = style;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
479
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
480 // fill style with some default values
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
481 style->ScaleX = 100.;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
482 style->ScaleY = 100.;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
483
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
484 while (1) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
485 NEXT(q, tname);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
486 NEXT(p, token);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
487
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
488 if (0) { // cool ;)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
489 STRVAL(Name)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
490 if ((strcmp(target->Name, "Default") == 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
491 || (strcmp(target->Name, "*Default") == 0))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
492 track->default_style = sid;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
493 STRVAL(FontName)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
494 COLORVAL(PrimaryColour)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
495 COLORVAL(SecondaryColour)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
496 COLORVAL(OutlineColour) // TertiaryColor
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
497 COLORVAL(BackColour)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
498 // SSA uses BackColour for both outline and shadow
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
499 // this will destroy SSA's TertiaryColour, but i'm not going to use it anyway
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
500 if (track->track_type == TRACK_TYPE_SSA)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
501 target->OutlineColour = target->BackColour;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
502 FPVAL(FontSize)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
503 INTVAL(Bold)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
504 INTVAL(Italic)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
505 INTVAL(Underline)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
506 INTVAL(StrikeOut)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
507 FPVAL(Spacing)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
508 INTVAL(Angle)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
509 INTVAL(BorderStyle)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
510 INTVAL(Alignment)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
511 if (track->track_type == TRACK_TYPE_ASS)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
512 target->Alignment = numpad2align(target->Alignment);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
513 INTVAL(MarginL)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
514 INTVAL(MarginR)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
515 INTVAL(MarginV)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
516 INTVAL(Encoding)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
517 FPVAL(ScaleX)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
518 FPVAL(ScaleY)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
519 FPVAL(Outline)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
520 FPVAL(Shadow)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
521 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
522 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
523 style->ScaleX /= 100.;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
524 style->ScaleY /= 100.;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
525 style->Bold = !!style->Bold;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
526 style->Italic = !!style->Italic;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
527 style->Underline = !!style->Underline;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
528 if (!style->Name)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
529 style->Name = strdup("Default");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
530 if (!style->FontName)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
531 style->FontName = strdup("Arial");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
532 free(format);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
533 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
534
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
535 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
536
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
537 static int process_styles_line(ASS_Track *track, char *str)
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
538 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
539 if (!strncmp(str, "Format:", 7)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
540 char *p = str + 7;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
541 skip_spaces(&p);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
542 track->style_format = strdup(p);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
543 ass_msg(track->library, MSGL_DBG2, "Style format: %s",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
544 track->style_format);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
545 } else if (!strncmp(str, "Style:", 6)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
546 char *p = str + 6;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
547 skip_spaces(&p);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
548 process_style(track, p);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
549 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
550 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
551 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
552
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
553 static int process_info_line(ASS_Track *track, char *str)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
554 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
555 if (!strncmp(str, "PlayResX:", 9)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
556 track->PlayResX = atoi(str + 9);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
557 } else if (!strncmp(str, "PlayResY:", 9)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
558 track->PlayResY = atoi(str + 9);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
559 } else if (!strncmp(str, "Timer:", 6)) {
31853
e64df5862cea Import libass 0.9.10
greg
parents: 31227
diff changeset
560 track->Timer = ass_atof(str + 6);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
561 } else if (!strncmp(str, "WrapStyle:", 10)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
562 track->WrapStyle = atoi(str + 10);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
563 } else if (!strncmp(str, "ScaledBorderAndShadow:", 22)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
564 track->ScaledBorderAndShadow = parse_bool(str + 22);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
565 } else if (!strncmp(str, "Kerning:", 8)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
566 track->Kerning = parse_bool(str + 8);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
567 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
568 return 0;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
569 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
570
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
571 static void event_format_fallback(ASS_Track *track)
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
572 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
573 track->parser_priv->state = PST_EVENTS;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
574 if (track->track_type == TRACK_TYPE_SSA)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
575 track->event_format = strdup("Format: Marked, Start, End, Style, "
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
576 "Name, MarginL, MarginR, MarginV, Effect, Text");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
577 else
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
578 track->event_format = strdup("Format: Layer, Start, End, Style, "
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
579 "Actor, MarginL, MarginR, MarginV, Effect, Text");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
580 ass_msg(track->library, MSGL_V,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
581 "No event format found, using fallback");
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
582 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
583
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
584 static int process_events_line(ASS_Track *track, char *str)
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
585 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
586 if (!strncmp(str, "Format:", 7)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
587 char *p = str + 7;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
588 skip_spaces(&p);
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
589 free(track->event_format);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
590 track->event_format = strdup(p);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
591 ass_msg(track->library, MSGL_DBG2, "Event format: %s", track->event_format);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
592 } else if (!strncmp(str, "Dialogue:", 9)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
593 // This should never be reached for embedded subtitles.
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
594 // They have slightly different format and are parsed in ass_process_chunk,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
595 // called directly from demuxer
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
596 int eid;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
597 ASS_Event *event;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
598
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
599 str += 9;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
600 skip_spaces(&str);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
601
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
602 eid = ass_alloc_event(track);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
603 event = track->events + eid;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
604
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
605 // We can't parse events with event_format
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
606 if (!track->event_format)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
607 event_format_fallback(track);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
608
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
609 process_event_tail(track, event, str, 0);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
610 } else {
31853
e64df5862cea Import libass 0.9.10
greg
parents: 31227
diff changeset
611 ass_msg(track->library, MSGL_V, "Not understood: '%.30s'", str);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
612 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
613 return 0;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
614 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
615
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
616 // Copied from mkvtoolnix
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
617 static unsigned char *decode_chars(unsigned char c1, unsigned char c2,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
618 unsigned char c3, unsigned char c4,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
619 unsigned char *dst, int cnt)
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
620 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
621 uint32_t value;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
622 unsigned char bytes[3];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
623 int i;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
624
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
625 value =
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
626 ((c1 - 33) << 18) + ((c2 - 33) << 12) + ((c3 - 33) << 6) + (c4 -
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
627 33);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
628 bytes[2] = value & 0xff;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
629 bytes[1] = (value & 0xff00) >> 8;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
630 bytes[0] = (value & 0xff0000) >> 16;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
631
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
632 for (i = 0; i < cnt; ++i)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
633 *dst++ = bytes[i];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
634 return dst;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
635 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
636
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
637 static int decode_font(ASS_Track *track)
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
638 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
639 unsigned char *p;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
640 unsigned char *q;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
641 int i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
642 int size; // original size
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
643 int dsize; // decoded size
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
644 unsigned char *buf = 0;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
645
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
646 ass_msg(track->library, MSGL_V, "Font: %d bytes encoded data",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
647 track->parser_priv->fontdata_used);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
648 size = track->parser_priv->fontdata_used;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
649 if (size % 4 == 1) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
650 ass_msg(track->library, MSGL_ERR, "Bad encoded data size");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
651 goto error_decode_font;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
652 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
653 buf = malloc(size / 4 * 3 + 2);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
654 q = buf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
655 for (i = 0, p = (unsigned char *) track->parser_priv->fontdata;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
656 i < size / 4; i++, p += 4) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
657 q = decode_chars(p[0], p[1], p[2], p[3], q, 3);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
658 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
659 if (size % 4 == 2) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
660 q = decode_chars(p[0], p[1], 0, 0, q, 1);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
661 } else if (size % 4 == 3) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
662 q = decode_chars(p[0], p[1], p[2], 0, q, 2);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
663 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
664 dsize = q - buf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
665 assert(dsize <= size / 4 * 3 + 2);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
666
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
667 if (track->library->extract_fonts) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
668 ass_add_font(track->library, track->parser_priv->fontname,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
669 (char *) buf, dsize);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
670 }
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
671
31875
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
672 error_decode_font:
ac6e48baa03d Import libass 0.9.11
greg
parents: 31853
diff changeset
673 free(buf);
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
674 free(track->parser_priv->fontname);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
675 free(track->parser_priv->fontdata);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
676 track->parser_priv->fontname = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
677 track->parser_priv->fontdata = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
678 track->parser_priv->fontdata_size = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
679 track->parser_priv->fontdata_used = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
680 return 0;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
681 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
682
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
683 static int process_fonts_line(ASS_Track *track, char *str)
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
684 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
685 int len;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
686
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
687 if (!strncmp(str, "fontname:", 9)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
688 char *p = str + 9;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
689 skip_spaces(&p);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
690 if (track->parser_priv->fontname) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
691 decode_font(track);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
692 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
693 track->parser_priv->fontname = strdup(p);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
694 ass_msg(track->library, MSGL_V, "Fontname: %s",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
695 track->parser_priv->fontname);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
696 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
697 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
698
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
699 if (!track->parser_priv->fontname) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
700 ass_msg(track->library, MSGL_V, "Not understood: '%s'", str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
701 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
702 }
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
703
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
704 len = strlen(str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
705 if (len > 80) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
706 ass_msg(track->library, MSGL_WARN, "Font line too long: %d, %s",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
707 len, str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
708 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
709 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
710 if (track->parser_priv->fontdata_used + len >
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
711 track->parser_priv->fontdata_size) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
712 track->parser_priv->fontdata_size += 100 * 1024;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
713 track->parser_priv->fontdata =
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
714 realloc(track->parser_priv->fontdata,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
715 track->parser_priv->fontdata_size);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
716 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
717 memcpy(track->parser_priv->fontdata + track->parser_priv->fontdata_used,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
718 str, len);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
719 track->parser_priv->fontdata_used += len;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
720
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
721 return 0;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
722 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
723
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
724 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
725 * \brief Parse a header line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
726 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
727 * \param str string to parse, zero-terminated
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
728 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
729 static int process_line(ASS_Track *track, char *str)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
730 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
731 if (!strncasecmp(str, "[Script Info]", 13)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
732 track->parser_priv->state = PST_INFO;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
733 } else if (!strncasecmp(str, "[V4 Styles]", 11)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
734 track->parser_priv->state = PST_STYLES;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
735 track->track_type = TRACK_TYPE_SSA;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
736 } else if (!strncasecmp(str, "[V4+ Styles]", 12)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
737 track->parser_priv->state = PST_STYLES;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
738 track->track_type = TRACK_TYPE_ASS;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
739 } else if (!strncasecmp(str, "[Events]", 8)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
740 track->parser_priv->state = PST_EVENTS;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
741 } else if (!strncasecmp(str, "[Fonts]", 7)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
742 track->parser_priv->state = PST_FONTS;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
743 } else {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
744 switch (track->parser_priv->state) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
745 case PST_INFO:
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
746 process_info_line(track, str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
747 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
748 case PST_STYLES:
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
749 process_styles_line(track, str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
750 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
751 case PST_EVENTS:
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
752 process_events_line(track, str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
753 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
754 case PST_FONTS:
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
755 process_fonts_line(track, str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
756 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
757 default:
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
758 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
759 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
760 }
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
761
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
762 // there is no explicit end-of-font marker in ssa/ass
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
763 if ((track->parser_priv->state != PST_FONTS)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
764 && (track->parser_priv->fontname))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
765 decode_font(track);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
766
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
767 return 0;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
768 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
769
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
770 static int process_text(ASS_Track *track, char *str)
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
771 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
772 char *p = str;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
773 while (1) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
774 char *q;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
775 while (1) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
776 if ((*p == '\r') || (*p == '\n'))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
777 ++p;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
778 else if (p[0] == '\xef' && p[1] == '\xbb' && p[2] == '\xbf')
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
779 p += 3; // U+FFFE (BOM)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
780 else
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
781 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
782 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
783 for (q = p; ((*q != '\0') && (*q != '\r') && (*q != '\n')); ++q) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
784 };
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
785 if (q == p)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
786 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
787 if (*q != '\0')
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
788 *(q++) = '\0';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
789 process_line(track, p);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
790 if (*q == '\0')
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
791 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
792 p = q;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
793 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
794 return 0;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
795 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
796
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
797 /**
27498
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
798 * \brief Process a chunk of subtitle stream data.
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
799 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
800 * \param data string to parse
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
801 * \param size length of data
27498
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
802 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
803 void ass_process_data(ASS_Track *track, char *data, int size)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
804 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
805 char *str = malloc(size + 1);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
806
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
807 memcpy(str, data, size);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
808 str[size] = '\0';
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
809
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
810 ass_msg(track->library, MSGL_V, "Event: %s", str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
811 process_text(track, str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
812 free(str);
27498
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
813 }
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
814
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
815 /**
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
816 * \brief Process CodecPrivate section of subtitle stream
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
817 * \param track track
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
818 * \param data string to parse
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
819 * \param size length of data
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
820 CodecPrivate section contains [Stream Info] and [V4+ Styles] ([V4 Styles] for SSA) sections
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
821 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
822 void ass_process_codec_private(ASS_Track *track, char *data, int size)
27498
d895515b366d libass: add a new ass_process_data() to process demuxed subtitle packets
aurel
parents: 27393
diff changeset
823 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
824 ass_process_data(track, data, size);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
825
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
826 // probably an mkv produced by ancient mkvtoolnix
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
827 // such files don't have [Events] and Format: headers
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
828 if (!track->event_format)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
829 event_format_fallback(track);
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
830
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
831 ass_process_force_style(track);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
832 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
833
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
834 static int check_duplicate_event(ASS_Track *track, int ReadOrder)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
835 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
836 int i;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
837 for (i = 0; i < track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
838 if (track->events[i].ReadOrder == ReadOrder)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
839 return 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
840 return 0;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
841 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
842
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
843 /**
25515
diego
parents: 24851
diff changeset
844 * \brief Process a chunk of subtitle stream data. In Matroska, this contains exactly 1 event (or a commentary).
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
845 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
846 * \param data string to parse
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
847 * \param size length of data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
848 * \param timecode starting time of the event (milliseconds)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
849 * \param duration duration of the event (milliseconds)
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
850 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
851 void ass_process_chunk(ASS_Track *track, char *data, int size,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
852 long long timecode, long long duration)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
853 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
854 char *str;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
855 int eid;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
856 char *p;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
857 char *token;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
858 ASS_Event *event;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
859
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
860 if (!track->event_format) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
861 ass_msg(track->library, MSGL_WARN, "Event format header missing");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
862 return;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
863 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
864
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
865 str = malloc(size + 1);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
866 memcpy(str, data, size);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
867 str[size] = '\0';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
868 ass_msg(track->library, MSGL_V, "Event at %" PRId64 ", +%" PRId64 ": %s",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
869 (int64_t) timecode, (int64_t) duration, str);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
870
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
871 eid = ass_alloc_event(track);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
872 event = track->events + eid;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
873
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
874 p = str;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
875
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
876 do {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
877 NEXT(p, token);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
878 event->ReadOrder = atoi(token);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
879 if (check_duplicate_event(track, event->ReadOrder))
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
880 break;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
881
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
882 NEXT(p, token);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
883 event->Layer = atoi(token);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
884
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
885 process_event_tail(track, event, p, 3);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
886
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
887 event->Start = timecode;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
888 event->Duration = duration;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
889
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
890 free(str);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
891 return;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
892 // dump_events(tid);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
893 } while (0);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
894 // some error
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
895 ass_free_event(track, eid);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
896 track->n_events--;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
897 free(str);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
898 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
899
31227
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
900 /**
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
901 * \brief Flush buffered events.
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
902 * \param track track
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
903 */
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
904 void ass_flush_events(ASS_Track *track)
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
905 {
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
906 if (track->events) {
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
907 int eid;
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
908 for (eid = 0; eid < track->n_events; eid++)
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
909 ass_free_event(track, eid);
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
910 track->n_events = 0;
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
911 }
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
912 }
ee7beb1a3a6e backport ass_flush_events() from upstream libass and make use of it
aurel
parents: 30200
diff changeset
913
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
914 #ifdef CONFIG_ICONV
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
915 /** \brief recode buffer to utf-8
20477
de4a66d99f41 Libass interface reworked:
eugeni
parents: 20008
diff changeset
916 * constraint: codepage != 0
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
917 * \param data pointer to text buffer
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
918 * \param size buffer size
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
919 * \return a pointer to recoded buffer, caller is responsible for freeing it
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
920 **/
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
921 static char *sub_recode(ASS_Library *library, char *data, size_t size,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
922 char *codepage)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
923 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
924 iconv_t icdsc;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
925 char *tocp = "UTF-8";
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
926 char *outbuf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
927 assert(codepage);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
928
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
929 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
930 const char *cp_tmp = codepage;
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
931 #ifdef CONFIG_ENCA
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
932 char enca_lang[3], enca_fallback[100];
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
933 if (sscanf(codepage, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
934 || sscanf(codepage, "ENCA:%2s:%99s", enca_lang,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
935 enca_fallback) == 2) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
936 cp_tmp =
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
937 ass_guess_buffer_cp(library, (unsigned char *) data, size,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
938 enca_lang, enca_fallback);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
939 }
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
940 #endif
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
941 if ((icdsc = iconv_open(tocp, cp_tmp)) != (iconv_t) (-1)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
942 ass_msg(library, MSGL_V, "Opened iconv descriptor");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
943 } else
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
944 ass_msg(library, MSGL_ERR, "Error opening iconv descriptor");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
945 }
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
946
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
947 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
948 size_t osize = size;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
949 size_t ileft = size;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
950 size_t oleft = size - 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
951 char *ip;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
952 char *op;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
953 size_t rc;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
954 int clear = 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
955
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
956 outbuf = malloc(osize);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
957 ip = data;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
958 op = outbuf;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
959
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
960 while (1) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
961 if (ileft)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
962 rc = iconv(icdsc, &ip, &ileft, &op, &oleft);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
963 else { // clear the conversion state and leave
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
964 clear = 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
965 rc = iconv(icdsc, NULL, NULL, &op, &oleft);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
966 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
967 if (rc == (size_t) (-1)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
968 if (errno == E2BIG) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
969 size_t offset = op - outbuf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
970 outbuf = (char *) realloc(outbuf, osize + size);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
971 op = outbuf + offset;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
972 osize += size;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
973 oleft += size;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
974 } else {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
975 ass_msg(library, MSGL_WARN, "Error recoding file");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
976 return NULL;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
977 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
978 } else if (clear)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
979 break;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
980 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
981 outbuf[osize - oleft - 1] = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
982 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
983
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
984 if (icdsc != (iconv_t) (-1)) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
985 (void) iconv_close(icdsc);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
986 icdsc = (iconv_t) (-1);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
987 ass_msg(library, MSGL_V, "Closed iconv descriptor");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
988 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
989
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
990 return outbuf;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
991 }
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
992 #endif // ICONV
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
993
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
994 /**
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
995 * \brief read file contents into newly allocated buffer
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
996 * \param fname file name
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
997 * \param bufsize out: file size
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
998 * \return pointer to file contents. Caller is responsible for its deallocation.
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
999 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1000 static char *read_file(ASS_Library *library, char *fname, size_t *bufsize)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1001 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1002 int res;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1003 long sz;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1004 long bytes_read;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1005 char *buf;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1006
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1007 FILE *fp = fopen(fname, "rb");
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1008 if (!fp) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1009 ass_msg(library, MSGL_WARN,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1010 "ass_read_file(%s): fopen failed", fname);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1011 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1012 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1013 res = fseek(fp, 0, SEEK_END);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1014 if (res == -1) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1015 ass_msg(library, MSGL_WARN,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1016 "ass_read_file(%s): fseek failed", fname);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1017 fclose(fp);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1018 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1019 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1020
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1021 sz = ftell(fp);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1022 rewind(fp);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1023
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1024 ass_msg(library, MSGL_V, "File size: %ld", sz);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1025
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1026 buf = malloc(sz + 1);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1027 assert(buf);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1028 bytes_read = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1029 do {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1030 res = fread(buf + bytes_read, 1, sz - bytes_read, fp);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1031 if (res <= 0) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1032 ass_msg(library, MSGL_INFO, "Read failed, %d: %s", errno,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1033 strerror(errno));
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1034 fclose(fp);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1035 free(buf);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1036 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1037 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1038 bytes_read += res;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1039 } while (sz - bytes_read > 0);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1040 buf[sz] = '\0';
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1041 fclose(fp);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1042
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1043 if (bufsize)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1044 *bufsize = sz;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1045 return buf;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1046 }
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1047
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1048 /*
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1049 * \param buf pointer to subtitle text in utf-8
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1050 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1051 static ASS_Track *parse_memory(ASS_Library *library, char *buf)
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1052 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1053 ASS_Track *track;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1054 int i;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1055
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1056 track = ass_new_track(library);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1057
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1058 // process header
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1059 process_text(track, buf);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
1060
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1061 // external SSA/ASS subs does not have ReadOrder field
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1062 for (i = 0; i < track->n_events; ++i)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1063 track->events[i].ReadOrder = i;
19905
5a24682d9dd5 Set ReadOrder values for external subtitles.
eugeni
parents: 19652
diff changeset
1064
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1065 // there is no explicit end-of-font marker in ssa/ass
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1066 if (track->parser_priv->fontname)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1067 decode_font(track);
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
1068
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1069 if (track->track_type == TRACK_TYPE_UNKNOWN) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1070 ass_free_track(track);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1071 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1072 }
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1073
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1074 ass_process_force_style(track);
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
1075
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1076 return track;
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1077 }
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1078
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1079 /**
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1080 * \brief Read subtitles from memory.
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1081 * \param library libass library object
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1082 * \param buf pointer to subtitles text
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1083 * \param bufsize size of buffer
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1084 * \param codepage recode buffer contents from given codepage
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1085 * \return newly allocated track
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1086 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1087 ASS_Track *ass_read_memory(ASS_Library *library, char *buf,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1088 size_t bufsize, char *codepage)
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1089 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1090 ASS_Track *track;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1091 int need_free = 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1092
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1093 if (!buf)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1094 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1095
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
1096 #ifdef CONFIG_ICONV
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1097 if (codepage) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1098 buf = sub_recode(library, buf, bufsize, codepage);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1099 if (!buf)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1100 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1101 else
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1102 need_free = 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1103 }
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1104 #endif
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1105 track = parse_memory(library, buf);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1106 if (need_free)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1107 free(buf);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1108 if (!track)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1109 return 0;
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1110
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1111 ass_msg(library, MSGL_INFO, "Added subtitle file: "
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1112 "<memory> (%d styles, %d events)",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1113 track->n_styles, track->n_events);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1114 return track;
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1115 }
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1116
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1117 static char *read_file_recode(ASS_Library *library, char *fname,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1118 char *codepage, size_t *size)
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1119 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1120 char *buf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1121 size_t bufsize;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1122
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1123 buf = read_file(library, fname, &bufsize);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1124 if (!buf)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1125 return 0;
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
1126 #ifdef CONFIG_ICONV
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1127 if (codepage) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1128 char *tmpbuf = sub_recode(library, buf, bufsize, codepage);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1129 free(buf);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1130 buf = tmpbuf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1131 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1132 if (!buf)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1133 return 0;
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1134 #endif
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1135 *size = bufsize;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1136 return buf;
23424
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1137 }
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1138
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1139 /**
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1140 * \brief Read subtitles from file.
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1141 * \param library libass library object
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1142 * \param fname file name
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1143 * \param codepage recode buffer contents from given codepage
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1144 * \return newly allocated track
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1145 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1146 ASS_Track *ass_read_file(ASS_Library *library, char *fname,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1147 char *codepage)
23424
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1148 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1149 char *buf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1150 ASS_Track *track;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1151 size_t bufsize;
23424
7286d245bf33 Move code for reading a file and recoding it to utf-8 to a separate function.
eugeni
parents: 23300
diff changeset
1152
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1153 buf = read_file_recode(library, fname, codepage, &bufsize);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1154 if (!buf)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1155 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1156 track = parse_memory(library, buf);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1157 free(buf);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1158 if (!track)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1159 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1160
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1161 track->name = strdup(fname);
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1162
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1163 ass_msg(library, MSGL_INFO,
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1164 "Added subtitle file: '%s' (%d styles, %d events)",
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1165 fname, track->n_styles, track->n_events);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1166
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1167 return track;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1168 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1169
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1170 /**
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1171 * \brief read styles from file into already initialized track
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1172 */
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1173 int ass_read_styles(ASS_Track *track, char *fname, char *codepage)
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1174 {
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1175 char *buf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1176 ParserState old_state;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1177 size_t sz;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1178
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1179 buf = read_file(track->library, fname, &sz);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1180 if (!buf)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1181 return 1;
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
1182 #ifdef CONFIG_ICONV
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1183 if (codepage) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1184 char *tmpbuf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1185 tmpbuf = sub_recode(track->library, buf, sz, codepage);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1186 free(buf);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1187 buf = tmpbuf;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1188 }
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1189 if (!buf)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1190 return 0;
20603
b0814eba65ec Implement loading subtitles from memory.
eugeni
parents: 20602
diff changeset
1191 #endif
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1192
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1193 old_state = track->parser_priv->state;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1194 track->parser_priv->state = PST_STYLES;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1195 process_text(track, buf);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1196 track->parser_priv->state = old_state;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1197
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1198 return 0;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1199 }
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
1200
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1201 long long ass_step_sub(ASS_Track *track, long long now, int movement)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1202 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1203 int i;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1204
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1205 if (movement == 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1206 return 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1207 if (track->n_events == 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1208 return 0;
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1209
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1210 if (movement < 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1211 for (i = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1212 (i < track->n_events)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1213 &&
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1214 ((long long) (track->events[i].Start +
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1215 track->events[i].Duration) <= now); ++i) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1216 } else
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1217 for (i = track->n_events - 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1218 (i >= 0) && ((long long) (track->events[i].Start) > now);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1219 --i) {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1220 }
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 28900
diff changeset
1221
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1222 // -1 and n_events are ok
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1223 assert(i >= -1);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1224 assert(i <= track->n_events);
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1225 i += movement;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1226 if (i < 0)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1227 i = 0;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1228 if (i >= track->n_events)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1229 i = track->n_events - 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1230 return ((long long) track->events[i].Start) - now;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1231 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1232
30200
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1233 ASS_Track *ass_new_track(ASS_Library *library)
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1234 {
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1235 ASS_Track *track = calloc(1, sizeof(ASS_Track));
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1236 track->library = library;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1237 track->ScaledBorderAndShadow = 1;
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1238 track->parser_priv = calloc(1, sizeof(ASS_ParserPriv));
48d020c5ceca Update internal libass copy to commit 8db4a5
greg
parents: 30066
diff changeset
1239 return track;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1240 }