annotate libass/ass.c @ 30256:c116c83f6268

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