annotate libass/ass.c @ 19657:a75995bf41e6

Remove outdated references to CVS.
author diego
date Sun, 03 Sep 2006 19:30:57 +0000
parents 2c016957360a
children 5a24682d9dd5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1 #include "config.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
2
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
3 #include <stdio.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
4 #include <stdlib.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
5 #include <string.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
6 #include <assert.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
7 #include <errno.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
8 #include <sys/types.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
9 #include <sys/stat.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
10 #include <unistd.h>
19374
acb26d9717da MinGW treats %lld as %d. Replace it with PRId64.
eugeni
parents: 18937
diff changeset
11 #include <inttypes.h>
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
12
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
13 #ifdef HAVE_ENCA
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
14 #include "subreader.h" // for guess_buffer_cp
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
15 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
16
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
17 #ifdef USE_ICONV
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
18 #include <iconv.h>
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
19 extern char *sub_cp;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
20 #endif
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
21 extern int extract_embedded_fonts;
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
22 extern char** ass_force_style_list;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
23
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
24 #include "mp_msg.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
25 #include "ass.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
26 #include "ass_utils.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
27 #include "libvo/sub.h" // for utf8_get_char
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
28
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
29 char *get_path(char *);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
30
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
31 typedef enum {PST_UNKNOWN = 0, PST_INFO, PST_STYLES, PST_EVENTS, PST_FONTS} parser_state_t;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
32
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
33 struct parser_priv_s {
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
34 parser_state_t state;
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
35 char* fontname;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
36 char* fontdata;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
37 int fontdata_size;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
38 int fontdata_used;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
39 };
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
40
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
41 #define ASS_STYLES_ALLOC 20
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
42 #define ASS_EVENTS_ALLOC 200
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
43
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
44 void ass_free_track(ass_track_t* track) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
45 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
46
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
47 if (track->parser_priv) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
48 if (track->parser_priv->fontname)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
49 free(track->parser_priv->fontname);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
50 if (track->parser_priv->fontdata)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
51 free(track->parser_priv->fontdata);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
52 free(track->parser_priv);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
53 }
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
54 if (track->style_format)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
55 free(track->style_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
56 if (track->event_format)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
57 free(track->event_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
58 if (track->styles) {
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
59 for (i = 0; i < track->n_styles; ++i)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
60 ass_free_style(track, i);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
61 free(track->styles);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
62 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
63 if (track->events) {
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
64 for (i = 0; i < track->n_events; ++i)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
65 ass_free_event(track, i);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
66 free(track->events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
67 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
68 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
69
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
70 /// \brief Allocate a new style struct
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
71 /// \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
72 /// \return style id
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
73 int ass_alloc_style(ass_track_t* track) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
74 int sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
75
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
76 assert(track->n_styles <= track->max_styles);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
77
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
78 if (track->n_styles == track->max_styles) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
79 track->max_styles += ASS_STYLES_ALLOC;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
80 track->styles = (ass_style_t*)realloc(track->styles, sizeof(ass_style_t)*track->max_styles);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
81 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
82
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
83 sid = track->n_styles++;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
84 memset(track->styles + sid, 0, sizeof(ass_style_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
85 return sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
86 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
87
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
88 /// \brief Allocate a new event struct
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
89 /// \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
90 /// \return event id
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
91 int ass_alloc_event(ass_track_t* track) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
92 int eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
93
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
94 assert(track->n_events <= track->max_events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
95
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
96 if (track->n_events == track->max_events) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
97 track->max_events += ASS_EVENTS_ALLOC;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
98 track->events = (ass_event_t*)realloc(track->events, sizeof(ass_event_t)*track->max_events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
99 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
100
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
101 eid = track->n_events++;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
102 memset(track->events + eid, 0, sizeof(ass_event_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
103 return eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
104 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
105
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
106 void ass_free_event(ass_track_t* track, int eid) {
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
107 ass_event_t* event = track->events + eid;
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
108 if (event->Name)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
109 free(event->Name);
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
110 if (event->Effect)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
111 free(event->Effect);
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
112 if (event->Text)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
113 free(event->Text);
19638
a3473d990fed Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents: 19635
diff changeset
114 if (event->render_priv)
a3473d990fed Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents: 19635
diff changeset
115 free(event->render_priv);
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
116 }
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
117
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
118 void ass_free_style(ass_track_t* track, int sid) {
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
119 ass_style_t* style = track->styles + sid;
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
120 if (style->Name)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
121 free(style->Name);
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
122 if (style->FontName)
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
123 free(style->FontName);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
124 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
125
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
126 // ==============================================================================================
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
127
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
128 static void skip_spaces(char** str) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
129 char* p = *str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
130 while ((*p==' ') || (*p=='\t'))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
131 ++p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
132 *str = p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
133 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
134
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
135 static void rskip_spaces(char** str, char* limit) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
136 char* p = *str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
137 while ((p >= limit) && ((*p==' ') || (*p=='\t')))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
138 --p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
139 *str = p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
140 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
141
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
142 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
143 * \brief find style by name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
144 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
145 * \param name style name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
146 * \return index in track->styles
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
147 * Returnes 0 if no styles found => expects at least 1 style.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
148 * Parsing code always adds "Default" style in the end.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
149 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
150 static int lookup_style(ass_track_t* track, char* name) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
151 int i;
19567
1a03b2c8ac28 Ignore '*' at the beginning of style name.
eugeni
parents: 19495
diff changeset
152 if (*name == '*') ++name; // FIXME: what does '*' really mean ?
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
153 for (i=0; i<track->n_styles; ++i) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
154 // FIXME: mb strcasecmp ?
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
155 if (strcmp(track->styles[i].Name, name) == 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
156 return i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
157 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
158 i = track->default_style;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
159 mp_msg(MSGT_GLOBAL, MSGL_WARN, "[%p] Warning: no style named '%s' found, using '%s'\n", track, name, track->styles[i].Name);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
160 return i; // use the first style
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
161 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
162
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
163 static uint32_t string2color(char* p) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
164 uint32_t tmp;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
165 (void)strtocolor(&p, &tmp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
166 return tmp;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
167 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
168
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
169 static long long string2timecode(char* p) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
170 unsigned h, m, s, ms;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
171 long long tm;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
172 int res = sscanf(p, "%1d:%2d:%2d.%2d", &h, &m, &s, &ms);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
173 if (res < 4) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
174 mp_msg(MSGT_GLOBAL, MSGL_WARN, "bad timestamp\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
175 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
176 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
177 tm = ((h * 60 + m) * 60 + s) * 1000 + ms * 10;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
178 return tm;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
179 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
180
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
181 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
182 * \brief converts numpad-style align to align.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
183 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
184 static int numpad2align(int val) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
185 int res, v;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
186 v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
187 if (v != 0) v = 3 - v;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
188 res = ((val - 1) % 3) + 1; // horizontal alignment
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
189 res += v*4;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
190 return res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
191 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
192
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
193 #define NEXT(str,token) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
194 token = next_token(&str); \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
195 if (!token) break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
196
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
197 #define ANYVAL(name,func) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
198 } else if (strcasecmp(tname, #name) == 0) { \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
199 target->name = func(token); \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
200 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "%s = %s\n", #name, token);
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
201
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
202 #define STRVAL(name) \
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
203 } else if (strcasecmp(tname, #name) == 0) { \
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
204 if (target->name != NULL) free(target->name); \
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
205 target->name = strdup(token); \
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
206 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "%s = %s\n", #name, token);
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
207
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
208 #define COLORVAL(name) ANYVAL(name,string2color)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
209 #define INTVAL(name) ANYVAL(name,atoi)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
210 #define FPVAL(name) ANYVAL(name,atof)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
211 #define TIMEVAL(name) ANYVAL(name,string2timecode)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
212 #define STYLEVAL(name) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
213 } else if (strcasecmp(tname, #name) == 0) { \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
214 target->name = lookup_style(track, token); \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
215 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "%s = %s\n", #name, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
216
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
217 #define ALIAS(alias,name) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
218 if (strcasecmp(tname, #alias) == 0) {tname = #name;}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
219
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
220 static char* next_token(char** str) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
221 char* p = *str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
222 char* start;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
223 skip_spaces(&p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
224 if (*p == '\0') {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
225 *str = p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
226 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
227 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
228 start = p; // start of the token
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
229 for (; (*p != '\0') && (*p != ','); ++p) {}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
230 if (*p == '\0') {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
231 *str = p; // eos found, str will point to '\0' at exit
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
232 } else {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
233 *p = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
234 *str = p + 1; // ',' found, str will point to the next char (beginning of the next token)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
235 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
236 --p; // end of current token
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
237 rskip_spaces(&p, start);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
238 if (p < start)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
239 p = start; // empty token
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
240 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
241 ++p; // the first space character, or '\0'
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
242 *p = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
243 return start;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
244 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
245 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
246 * \brief Parse the tail of Dialogue line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
247 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
248 * \param event parsed data goes here
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
249 * \param str string to parse, zero-terminated
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
250 * \param n_ignored number of format options to skip at the beginning
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
251 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
252 static int process_event_tail(ass_track_t* track, ass_event_t* event, char* str, int n_ignored)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
253 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
254 char* token;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
255 char* tname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
256 char* p = str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
257 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
258 ass_event_t* target = event;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
259
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
260 char* format = strdup(track->event_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
261 char* q = format; // format scanning pointer
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
262
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
263 for (i = 0; i < n_ignored; ++i) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
264 NEXT(q, tname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
265 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
266
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
267 while (1) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
268 NEXT(q, tname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
269 if (strcasecmp(tname, "Text") == 0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
270 char* last;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
271 event->Text = strdup(p);
19612
dcd0f218008f Bugfix: potential write of unallocated memory.
eugeni
parents: 19584
diff changeset
272 if (*event->Text != 0) {
dcd0f218008f Bugfix: potential write of unallocated memory.
eugeni
parents: 19584
diff changeset
273 last = event->Text + strlen(event->Text) - 1;
dcd0f218008f Bugfix: potential write of unallocated memory.
eugeni
parents: 19584
diff changeset
274 if (last >= event->Text && *last == '\r')
dcd0f218008f Bugfix: potential write of unallocated memory.
eugeni
parents: 19584
diff changeset
275 *last = 0;
dcd0f218008f Bugfix: potential write of unallocated memory.
eugeni
parents: 19584
diff changeset
276 }
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
277 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Text = %s\n", event->Text);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
278 event->Duration -= event->Start;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
279 free(format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
280 return 0; // "Text" is always the last
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
281 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
282 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
283
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
284 ALIAS(End,Duration) // temporarily store end timecode in event->Duration
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
285 if (0) { // cool ;)
19635
4a64fecfb425 Read layer info from external subs.
eugeni
parents: 19612
diff changeset
286 INTVAL(Layer)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
287 STYLEVAL(Style)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
288 STRVAL(Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
289 STRVAL(Effect)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
290 INTVAL(MarginL)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
291 INTVAL(MarginR)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
292 INTVAL(MarginV)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
293 TIMEVAL(Start)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
294 TIMEVAL(Duration)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
295 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
296 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
297 free(format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
298 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
299 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
300
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
301 /**
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
302 * \brief Parse command line style overrides (--ass-force-style option)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
303 * \param track track to apply overrides to
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
304 * The format for overrides is [StyleName.]Field=Value
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
305 */
19584
586e7d259d05 Apply -ass-force-style also to tracks generated from subdata.
eugeni
parents: 19567
diff changeset
306 void process_force_style(ass_track_t* track) {
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
307 char **fs, *eq, *dt, *style, *tname, *token;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
308 ass_style_t* target;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
309 int sid;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
310
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
311 if (!ass_force_style_list) return;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
312
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
313 for (fs = ass_force_style_list; *fs; ++fs) {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
314 eq = strchr(*fs, '=');
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
315 if (!eq)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
316 continue;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
317 *eq = '\0';
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
318 token = eq + 1;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
319
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
320 dt = strchr(*fs, '.');
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
321 if (dt) {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
322 *dt = '\0';
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
323 style = *fs;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
324 tname = dt + 1;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
325 } else {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
326 style = NULL;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
327 tname = *fs;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
328 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
329 for (sid = 0; sid < track->n_styles; ++sid) {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
330 if (style == NULL || strcasecmp(track->styles[sid].Name, style) == 0) {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
331 target = track->styles + sid;
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
332 if (0) {
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
333 STRVAL(FontName)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
334 COLORVAL(PrimaryColour)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
335 COLORVAL(SecondaryColour)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
336 COLORVAL(OutlineColour)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
337 COLORVAL(BackColour)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
338 INTVAL(FontSize)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
339 INTVAL(Bold)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
340 INTVAL(Italic)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
341 INTVAL(Underline)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
342 INTVAL(StrikeOut)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
343 INTVAL(Spacing)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
344 INTVAL(Angle)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
345 INTVAL(BorderStyle)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
346 INTVAL(Alignment)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
347 INTVAL(MarginL)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
348 INTVAL(MarginR)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
349 INTVAL(MarginV)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
350 INTVAL(Encoding)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
351 FPVAL(ScaleX)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
352 FPVAL(ScaleY)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
353 FPVAL(Outline)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
354 FPVAL(Shadow)
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
355 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
356 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
357 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
358 *eq = '=';
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
359 if (dt) *dt = '.';
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
360 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
361 }
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
362
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
363 /**
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
364 * \brief Parse the Style line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
365 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
366 * \param str string to parse, zero-terminated
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
367 * Allocates a new style struct.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
368 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
369 static int process_style(ass_track_t* track, char *str)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
370 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
371
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
372 char* token;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
373 char* tname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
374 char* p = str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
375 char* format;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
376 char* q; // format scanning pointer
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
377 int sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
378 ass_style_t* style;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
379 ass_style_t* target;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
380
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
381 if (!track->style_format) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
382 // no style format header
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
383 // probably an ancient script version
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
384 if (track->track_type == TRACK_TYPE_SSA)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
385 track->style_format = strdup("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
386 "TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
387 "Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
388 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
389 track->style_format = strdup("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
390 "OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
391 "ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
392 "Alignment, MarginL, MarginR, MarginV, Encoding");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
393 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
394
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
395 q = format = strdup(track->style_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
396
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
397 mp_msg(MSGT_GLOBAL, MSGL_V, "[%p] Style: %s\n", track, str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
398
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
399 sid = ass_alloc_style(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
400
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
401 style = track->styles + sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
402 target = style;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
403 // fill style with some default values
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
404 style->ScaleX = 100.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
405 style->ScaleY = 100.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
406
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
407 while (1) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
408 NEXT(q, tname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
409 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
410
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
411 // ALIAS(TertiaryColour,OutlineColour) // ignore TertiaryColour; it appears only in SSA, and is overridden by BackColour
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
412
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
413 if (0) { // cool ;)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
414 STRVAL(Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
415 if ((strcmp(target->Name, "Default")==0) || (strcmp(target->Name, "*Default")==0))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
416 track->default_style = sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
417 STRVAL(FontName)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
418 COLORVAL(PrimaryColour)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
419 COLORVAL(SecondaryColour)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
420 COLORVAL(OutlineColour) // TertiaryColor
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
421 COLORVAL(BackColour)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
422 // SSA uses BackColour for both outline and shadow
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
423 // this will destroy SSA's TertiaryColour, but i'm not going to use it anyway
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
424 if (track->track_type == TRACK_TYPE_SSA)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
425 target->OutlineColour = target->BackColour;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
426 INTVAL(FontSize)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
427 INTVAL(Bold)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
428 INTVAL(Italic)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
429 INTVAL(Underline)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
430 INTVAL(StrikeOut)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
431 INTVAL(Spacing)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
432 INTVAL(Angle)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
433 INTVAL(BorderStyle)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
434 INTVAL(Alignment)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
435 if (track->track_type == TRACK_TYPE_ASS)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
436 target->Alignment = numpad2align(target->Alignment);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
437 INTVAL(MarginL)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
438 INTVAL(MarginR)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
439 INTVAL(MarginV)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
440 INTVAL(Encoding)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
441 FPVAL(ScaleX)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
442 FPVAL(ScaleY)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
443 FPVAL(Outline)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
444 FPVAL(Shadow)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
445 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
446 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
447 style->ScaleX /= 100.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
448 style->ScaleY /= 100.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
449 if (!style->Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
450 style->Name = strdup("Default");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
451 if (!style->FontName)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
452 style->FontName = strdup("Arial");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
453 free(format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
454 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
455
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
456 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
457
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
458 static int process_styles_line(ass_track_t* track, char *str)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
459 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
460 if (!strncmp(str,"Format:", 7)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
461 char* p = str + 7;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
462 skip_spaces(&p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
463 track->style_format = strdup(p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
464 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Style format: %s\n", track->style_format);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
465 } else if (!strncmp(str,"Style:", 6)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
466 char* p = str + 6;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
467 skip_spaces(&p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
468 process_style(track, p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
469 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
470 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
471 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
472
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
473 static int process_info_line(ass_track_t* track, char *str)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
474 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
475 if (!strncmp(str, "PlayResX:", 9)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
476 track->PlayResX = atoi(str + 9);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
477 } else if (!strncmp(str,"PlayResY:", 9)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
478 track->PlayResY = atoi(str + 9);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
479 } else if (!strncmp(str,"Timer:", 6)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
480 track->Timer = atof(str + 6);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
481 } else if (!strncmp(str,"WrapStyle:", 10)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
482 track->WrapStyle = atoi(str + 10);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
483 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
484 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
485 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
486
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
487 static int process_events_line(ass_track_t* track, char *str)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
488 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
489 if (!strncmp(str, "Format:", 7)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
490 char* p = str + 7;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
491 skip_spaces(&p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
492 track->event_format = strdup(p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
493 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Event format: %s\n", track->event_format);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
494 } else if (!strncmp(str, "Dialogue:", 9)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
495 // This should never be reached for embedded subtitles.
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
496 // They have slightly different format and are parsed in ass_process_chunk,
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
497 // called directly from demuxer
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
498 int eid;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
499 ass_event_t* event;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
500
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
501 str += 9;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
502 skip_spaces(&str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
503
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
504 eid = ass_alloc_event(track);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
505 event = track->events + eid;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
506
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
507 process_event_tail(track, event, str, 0);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
508 } else {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
509 mp_msg(MSGT_GLOBAL, MSGL_V, "Not understood: %s \n", str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
510 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
511 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
512 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
513
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
514 // Copied from mkvtoolnix
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
515 static unsigned char* decode_chars(unsigned char c1, unsigned char c2,
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
516 unsigned char c3, unsigned char c4, unsigned char* dst, int cnt)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
517 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
518 uint32_t value;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
519 unsigned char bytes[3];
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
520 int i;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
521
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
522 value = ((c1 - 33) << 18) + ((c2 - 33) << 12) + ((c3 - 33) << 6) + (c4 - 33);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
523 bytes[2] = value & 0xff;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
524 bytes[1] = (value & 0xff00) >> 8;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
525 bytes[0] = (value & 0xff0000) >> 16;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
526
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
527 for (i = 0; i < cnt; ++i)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
528 *dst++ = bytes[i];
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
529 return dst;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
530 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
531
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
532 static int decode_font(ass_track_t* track)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
533 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
534 unsigned char* p;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
535 unsigned char* q;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
536 int i;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
537 int size; // original size
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
538 int dsize; // decoded size
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
539 unsigned char* buf = 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
540
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
541 mp_msg(MSGT_GLOBAL, MSGL_V, "font: %d bytes encoded data \n", track->parser_priv->fontdata_used);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
542 size = track->parser_priv->fontdata_used;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
543 if (size % 4 == 1) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
544 mp_msg(MSGT_GLOBAL, MSGL_ERR, "bad encoded data size\n");
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
545 goto error_decode_font;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
546 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
547 buf = malloc(size / 4 * 3 + 2);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
548 q = buf;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
549 for (i = 0, p = (unsigned char*)track->parser_priv->fontdata; i < size / 4; i++, p+=4) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
550 q = decode_chars(p[0], p[1], p[2], p[3], q, 3);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
551 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
552 if (size % 4 == 2) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
553 q = decode_chars(p[0], p[1], 0, 0, q, 1);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
554 } else if (size % 4 == 3) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
555 q = decode_chars(p[0], p[1], p[2], 0, q, 2);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
556 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
557 dsize = q - buf;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
558 assert(dsize <= size / 4 * 3 + 2);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
559
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
560 if (extract_embedded_fonts)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
561 ass_process_font(track->parser_priv->fontname, (char*)buf, dsize);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
562
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
563 error_decode_font:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
564 if (buf) free(buf);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
565 free(track->parser_priv->fontname);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
566 free(track->parser_priv->fontdata);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
567 track->parser_priv->fontname = 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
568 track->parser_priv->fontdata = 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
569 track->parser_priv->fontdata_size = 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
570 track->parser_priv->fontdata_used = 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
571 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
572 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
573
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
574 static char* validate_fname(char* name);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
575
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
576 static int process_fonts_line(ass_track_t* track, char *str)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
577 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
578 int len;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
579
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
580 if (!strncmp(str, "fontname:", 9)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
581 char* p = str + 9;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
582 skip_spaces(&p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
583 if (track->parser_priv->fontname) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
584 decode_font(track);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
585 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
586 track->parser_priv->fontname = validate_fname(p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
587 mp_msg(MSGT_GLOBAL, MSGL_V, "fontname: %s\n", track->parser_priv->fontname);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
588 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
589 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
590
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
591 if (!track->parser_priv->fontname) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
592 mp_msg(MSGT_GLOBAL, MSGL_V, "Not understood: %s \n", str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
593 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
594 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
595
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
596 len = strlen(str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
597 if (len > 80) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
598 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Font line too long: %d, %s\n", len, str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
599 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
600 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
601 if (track->parser_priv->fontdata_used + len > track->parser_priv->fontdata_size) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
602 track->parser_priv->fontdata_size += 100 * 1024;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
603 track->parser_priv->fontdata = realloc(track->parser_priv->fontdata, track->parser_priv->fontdata_size);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
604 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
605 memcpy(track->parser_priv->fontdata + track->parser_priv->fontdata_used, str, len);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
606 track->parser_priv->fontdata_used += len;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
607
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
608 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
609 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
610
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
611 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
612 * \brief Parse a header line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
613 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
614 * \param str string to parse, zero-terminated
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
615 */
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
616 static int process_line(ass_track_t* track, char *str)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
617 {
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
618 if (strstr(str, "[Script Info]")) { // FIXME: strstr to skip possible BOM at the beginning of the script
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
619 track->parser_priv->state = PST_INFO;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
620 } else if (!strncmp(str, "[V4 Styles]", 11)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
621 track->parser_priv->state = PST_STYLES;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
622 track->track_type = TRACK_TYPE_SSA;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
623 } else if (!strncmp(str, "[V4+ Styles]", 12)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
624 track->parser_priv->state = PST_STYLES;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
625 track->track_type = TRACK_TYPE_ASS;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
626 } else if (!strncmp(str, "[Events]", 8)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
627 track->parser_priv->state = PST_EVENTS;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
628 } else if (!strncmp(str, "[Fonts]", 7)) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
629 track->parser_priv->state = PST_FONTS;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
630 } else {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
631 switch (track->parser_priv->state) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
632 case PST_INFO:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
633 process_info_line(track, str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
634 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
635 case PST_STYLES:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
636 process_styles_line(track, str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
637 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
638 case PST_EVENTS:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
639 process_events_line(track, str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
640 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
641 case PST_FONTS:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
642 process_fonts_line(track, str);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
643 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
644 default:
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
645 break;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
646 }
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
647 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
648
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
649 // there is no explicit end-of-font marker in ssa/ass
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
650 if ((track->parser_priv->state != PST_FONTS) && (track->parser_priv->fontname))
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
651 decode_font(track);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
652
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
653 return 0;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
654 }
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
655
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
656 static int process_text(ass_track_t* track, char* str)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
657 {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
658 char* p = str;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
659 while(1) {
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
660 char* q;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
661 for (;((*p=='\r')||(*p=='\n'));++p) {}
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
662 for (q=p; ((*q!='\0')&&(*q!='\r')&&(*q!='\n')); ++q) {};
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
663 if (q==p)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
664 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
665 if (*q != '\0')
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
666 *(q++) = '\0';
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
667 process_line(track, p);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
668 if (*q == '\0')
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
669 break;
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
670 p = q;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
671 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
672 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
673 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
674
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
675 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
676 * \brief Process CodecPrivate section of subtitle stream
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
677 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
678 * \param data string to parse
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
679 * \param size length of data
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
680 CodecPrivate section contains [Stream Info] and [V4+ Styles] ([V4 Styles] for SSA) sections
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
681 */
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
682 void ass_process_codec_private(ass_track_t* track, char *data, int size)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
683 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
684 char* str = malloc(size + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
685 int sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
686
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
687 memcpy(str, data, size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
688 str[size] = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
689
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
690 process_text(track, str);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
691 free(str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
692
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
693 // add "Default" style to the end
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
694 // will be used if track does not contain a default style (or even does not contain styles at all)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
695 sid = ass_alloc_style(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
696 track->styles[sid].Name = strdup("Default");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
697 track->styles[sid].FontName = strdup("Arial");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
698
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
699 if (!track->event_format) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
700 // probably an mkv produced by ancient mkvtoolnix
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
701 // such files don't have [Events] and Format: headers
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
702 track->parser_priv->state = PST_EVENTS;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
703 if (track->track_type == TRACK_TYPE_SSA)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
704 track->event_format = strdup("Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
705 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
706 track->event_format = strdup("Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
707 }
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
708
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
709 process_force_style(track);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
710 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
711
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
712 static int check_duplicate_event(ass_track_t* track, int ReadOrder)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
713 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
714 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
715 for (i = 0; i<track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
716 if (track->events[i].ReadOrder == ReadOrder)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
717 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
718 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
719 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
720
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
721 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
722 * \brief Process a chunk of subtitle stream data. In matroska, this containes exactly 1 event (or a commentary)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
723 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
724 * \param data string to parse
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
725 * \param size length of data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
726 * \param timecode starting time of the event (milliseconds)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
727 * \param duration duration of the event (milliseconds)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
728 */
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
729 void ass_process_chunk(ass_track_t* track, char *data, int size, long long timecode, long long duration)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
730 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
731 char* str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
732 int eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
733 char* p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
734 char* token;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
735 ass_event_t* event;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
736
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
737 if (!track->event_format) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
738 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Event format header missing\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
739 return;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
740 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
741
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
742 str = malloc(size + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
743 memcpy(str, data, size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
744 str[size] = '\0';
19378
d9d58ba1b833 Replace %lld with PRId64, part 2.
eugeni
parents: 19374
diff changeset
745 mp_msg(MSGT_GLOBAL, MSGL_V, "event at %" PRId64 ", +%" PRId64 ": %s \n", (int64_t)timecode, (int64_t)duration, str);
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
746
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
747 eid = ass_alloc_event(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
748 event = track->events + eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
749
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
750 p = str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
751
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
752 do {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
753 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
754 event->ReadOrder = atoi(token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
755 if (check_duplicate_event(track, event->ReadOrder))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
756 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
757
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
758 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
759 event->Layer = atoi(token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
760
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
761 process_event_tail(track, event, p, 3);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
762
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
763 event->Start = timecode;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
764 event->Duration = duration;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
765
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
766 free(str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
767 return;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
768 // dump_events(tid);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
769 } while (0);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
770 // some error
19474
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
771 ass_free_event(track, eid);
07209f48e527 Add public functions for removal of styles and events.
eugeni
parents: 19405
diff changeset
772 track->n_events--;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
773 free(str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
774 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
775
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
776 #ifdef USE_ICONV
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
777 /** \brief recode buffer to utf-8
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
778 * constraint: sub_cp != 0
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
779 * \param data pointer to text buffer
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
780 * \param size buffer size
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
781 * \return a pointer to recoded buffer, caller is responsible for freeing it
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
782 **/
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
783 static char* sub_recode(char* data, size_t size)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
784 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
785 static iconv_t icdsc = (iconv_t)(-1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
786 char* tocp = "UTF-8";
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
787 char* outbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
788 assert(sub_cp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
789
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
790 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
791 char* cp_tmp = sub_cp;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
792 #ifdef HAVE_ENCA
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
793 char enca_lang[3], enca_fallback[100];
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
794 if (sscanf(sub_cp, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
795 || sscanf(sub_cp, "ENCA:%2s:%99s", enca_lang, enca_fallback) == 2) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
796 cp_tmp = guess_buffer_cp((unsigned char*)data, size, enca_lang, enca_fallback);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
797 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
798 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
799 if ((icdsc = iconv_open (tocp, cp_tmp)) != (iconv_t)(-1)){
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
800 mp_msg(MSGT_SUBREADER,MSGL_V,"LIBSUB: opened iconv descriptor.\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
801 } else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
802 mp_msg(MSGT_SUBREADER,MSGL_ERR,"LIBSUB: error opening iconv descriptor.\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
803 #ifdef HAVE_ENCA
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
804 if (cp_tmp) free(cp_tmp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
805 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
806 }
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 size_t osize = size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
810 size_t ileft = size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
811 size_t oleft = size - 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
812 char* ip;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
813 char* op;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
814 size_t rc;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
815
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
816 outbuf = malloc(size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
817 ip = data;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
818 op = outbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
819
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
820 while (ileft) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
821 rc = iconv(icdsc, &ip, &ileft, &op, &oleft);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
822 if (rc == (size_t)(-1)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
823 if (errno == E2BIG) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
824 int offset = op - outbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
825 outbuf = (char*)realloc(outbuf, osize + size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
826 op = outbuf + offset;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
827 osize += size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
828 oleft += size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
829 } else {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
830 mp_msg(MSGT_SUBREADER, MSGL_WARN, "LIBSUB: error recoding file.\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
831 return NULL;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
832 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
833 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
834 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
835 outbuf[osize - oleft - 1] = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
836 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
837
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
838 if (icdsc != (iconv_t)(-1)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
839 (void)iconv_close(icdsc);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
840 icdsc = (iconv_t)(-1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
841 mp_msg(MSGT_SUBREADER,MSGL_V,"LIBSUB: closed iconv descriptor.\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
842 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
843
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
844 return outbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
845 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
846 #endif // ICONV
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
847
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
848 /**
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
849 * \brief read file contents into newly allocated buffer, recoding to utf-8
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
850 */
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
851 static char* read_file(char* fname)
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
852 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
853 int res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
854 long sz;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
855 long bytes_read;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
856 char* buf;
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
857
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
858 FILE* fp = fopen(fname, "rb");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
859 if (!fp) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
860 mp_msg(MSGT_GLOBAL, MSGL_WARN, "ass_read_file(%s): fopen failed\n", fname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
861 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
862 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
863 res = fseek(fp, 0, SEEK_END);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
864 if (res == -1) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
865 mp_msg(MSGT_GLOBAL, MSGL_WARN, "ass_read_file(%s): fseek failed\n", fname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
866 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
867 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
868 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
869
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
870 sz = ftell(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
871 rewind(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
872
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
873 if (sz > 10*1024*1024) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
874 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ass_read_file(%s): Refusing to load subtitles larger than 10M\n", fname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
875 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
876 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
877 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
878
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
879 mp_msg(MSGT_GLOBAL, MSGL_V, "file size: %ld\n", sz);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
880
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
881 buf = malloc(sz + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
882 assert(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
883 bytes_read = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
884 do {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
885 res = fread(buf + bytes_read, 1, sz - bytes_read, fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
886 if (res <= 0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
887 mp_msg(MSGT_GLOBAL, MSGL_INFO, "Read failed, %d: %s\n", errno, strerror(errno));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
888 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
889 free(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
890 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
891 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
892 bytes_read += res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
893 } while (sz - bytes_read > 0);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
894 buf[sz] = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
895 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
896
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
897 #ifdef USE_ICONV
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
898 if (sub_cp) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
899 char* tmpbuf = sub_recode(buf, sz);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
900 free(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
901 buf = tmpbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
902 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
903 #endif
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
904 return buf;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
905 }
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
906
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
907 /**
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
908 * \brief Read subtitles from file.
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
909 * \param fname file name
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
910 * \return newly allocated track
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
911 */
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
912 ass_track_t* ass_read_file(char* fname)
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
913 {
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
914 char* buf;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
915 ass_track_t* track;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
916
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
917 buf = read_file(fname);
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
918 if (!buf)
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
919 return 0;
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
920
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
921 track = ass_new_track();
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
922 track->name = strdup(fname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
923
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
924 // process header
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
925 process_text(track, buf);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
926
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
927 // there is no explicit end-of-font marker in ssa/ass
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
928 if (track->parser_priv->fontname)
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
929 decode_font(track);
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
930
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
931 free(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
932
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
933 if (track->track_type == TRACK_TYPE_UNKNOWN) {
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
934 ass_free_track(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
935 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
936 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
937
19495
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
938 process_force_style(track);
31ac2e1a5695 New cmdline option: -ass-force-style.
eugeni
parents: 19492
diff changeset
939
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
940 mp_msg(MSGT_GLOBAL, MSGL_INFO, "LIBASS: added subtitle file: %s (%d styles, %d events)\n", fname, track->n_styles, track->n_events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
941
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
942 // dump_events(forced_tid);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
943 return track;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
944 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
945
19652
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
946 /**
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
947 * \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
948 */
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
949 int ass_read_styles(ass_track_t* track, char* fname)
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
950 {
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
951 char* buf;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
952 parser_state_t old_state;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
953
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
954 buf = read_file(fname);
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
955 if (!buf)
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
956 return 1;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
957
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
958 old_state = track->parser_priv->state;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
959 track->parser_priv->state = PST_STYLES;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
960 process_text(track, buf);
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
961 track->parser_priv->state = old_state;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
962
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
963 return 0;
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
964 }
2c016957360a Add -ass-styles option. It allows to load styles from a file and use them
eugeni
parents: 19639
diff changeset
965
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
966 static char* validate_fname(char* name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
967 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
968 char* fname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
969 char* p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
970 char* q;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
971 unsigned code;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
972 int sz = strlen(name);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
973
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
974 q = fname = malloc(sz + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
975 p = name;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
976 while (*p) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
977 code = utf8_get_char(&p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
978 if (code == 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
979 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
980 if ( (code > 0x7F) ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
981 (code == '\\') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
982 (code == '/') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
983 (code == ':') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
984 (code == '*') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
985 (code == '?') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
986 (code == '<') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
987 (code == '>') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
988 (code == '|') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
989 (code == 0))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
990 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
991 *q++ = '_';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
992 } else {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
993 *q++ = code;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
994 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
995 if (p - name > sz)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
996 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
997 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
998 *q = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
999 return fname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1000 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1001
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1002 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1003 * \brief Process embedded matroska font. Saves it to ~/.mplayer/fonts.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1004 * \param name attachment name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1005 * \param data binary font data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1006 * \param data_size data size
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1007 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1008 void ass_process_font(const char* name, char* data, int data_size)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1009 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1010 char buf[1000];
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1011 FILE* fp = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1012 int rc;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1013 struct stat st;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1014 char* fname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1015
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1016 char* fonts_dir = get_path("fonts");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1017 rc = stat(fonts_dir, &st);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1018 if (rc) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1019 int res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1020 #ifndef __MINGW32__
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1021 res = mkdir(fonts_dir, 0700);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1022 #else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1023 res = mkdir(fonts_dir);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1024 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1025 if (res) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1026 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Failed to create: %s\n", fonts_dir);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1027 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1028 } else if (!S_ISDIR(st.st_mode)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1029 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Not a directory: %s\n", fonts_dir);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1030 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1031
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1032 fname = validate_fname((char*)name);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1033
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1034 snprintf(buf, 1000, "%s/%s", fonts_dir, fname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1035 free(fname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1036 free(fonts_dir);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1037
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1038 fp = fopen(buf, "wb");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1039 if (!fp) return;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1040
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1041 fwrite(data, data_size, 1, fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1042 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1043 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1044
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1045 long long ass_step_sub(ass_track_t* track, long long now, int movement) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1046 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1047
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1048 if (movement == 0) return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1049 if (track->n_events == 0) return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1050
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1051 if (movement < 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1052 for (i = 0; (i < track->n_events) && ((long long)(track->events[i].Start + track->events[i].Duration) <= now); ++i) {}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1053 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1054 for (i = track->n_events - 1; (i >= 0) && ((long long)(track->events[i].Start) > now); --i) {}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1055
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1056 // -1 and n_events are ok
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1057 assert(i >= -1); assert(i <= track->n_events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1058 i += movement;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1059 if (i < 0) i = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1060 if (i >= track->n_events) i = track->n_events - 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1061 return ((long long)track->events[i].Start) - now;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1062 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1063
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1064 ass_track_t* ass_new_track(void) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1065 ass_track_t* track = calloc(1, sizeof(ass_track_t));
19492
c8daf3471201 SSA/ASS parser reworked, with 2 main results:
eugeni
parents: 19474
diff changeset
1066 track->parser_priv = calloc(1, sizeof(parser_priv_t));
18937
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1067 return track;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1068 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
1069