annotate libass/ass.c @ 19470:b3939dba3c13

Allow to compile swscale tests
author lucabe
date Mon, 21 Aug 2006 12:15:29 +0000
parents 0797e1b4a4be
children 07209f48e527
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
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
21
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
22 #include "mp_msg.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
23 #include "ass.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
24 #include "ass_utils.h"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
25 #include "libvo/sub.h" // for utf8_get_char
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
26
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
27 char *get_path(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 #define ASS_STYLES_ALLOC 20
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
30 #define ASS_EVENTS_ALLOC 200
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
31
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
32 void ass_free_track(ass_track_t* track) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
33 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
34
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
35 if (track->style_format)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
36 free(track->style_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
37 if (track->event_format)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
38 free(track->event_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
39 if (track->styles) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
40 for (i = 0; i < track->n_styles; ++i) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
41 ass_style_t* style = track->styles + i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
42 if (style->Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
43 free(style->Name);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
44 if (style->FontName)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
45 free(style->FontName);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
46 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
47 free(track->styles);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
48 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
49 if (track->events) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
50 for (i = 0; i < track->n_events; ++i) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
51 ass_event_t* event = track->events + i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
52 if (event->Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
53 free(event->Name);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
54 if (event->Effect)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
55 free(event->Effect);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
56 if (event->Text)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
57 free(event->Text);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
58 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
59 free(track->events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
60 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
61 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
62
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
63 /// \brief Allocate a new style struct
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
64 /// \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
65 /// \return style id
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
66 int ass_alloc_style(ass_track_t* track) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
67 int sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
68
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
69 assert(track->n_styles <= track->max_styles);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
70
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
71 if (track->n_styles == track->max_styles) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
72 track->max_styles += ASS_STYLES_ALLOC;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
73 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
74 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
75
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
76 sid = track->n_styles++;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
77 memset(track->styles + sid, 0, sizeof(ass_style_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
78 return sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
79 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
80
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
81 /// \brief Allocate a new event struct
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
82 /// \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
83 /// \return event id
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
84 int ass_alloc_event(ass_track_t* track) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
85 int eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
86
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
87 assert(track->n_events <= track->max_events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
88
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
89 if (track->n_events == track->max_events) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
90 track->max_events += ASS_EVENTS_ALLOC;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
91 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
92 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
93
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
94 eid = track->n_events++;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
95 memset(track->events + eid, 0, sizeof(ass_event_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
96 return eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
97 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
98
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
99 static void free_event(ass_track_t* track, int eid) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
100 if (track->n_events > eid + 1) // not last event
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
101 memcpy(track->events + eid, track->events + eid + 1, sizeof(ass_event_t) * (track->n_events - eid - 1));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
102 track->n_events--;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
103 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
104
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
105 static int events_compare_f(const void* a_, const void* b_) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
106 ass_event_t* a = (ass_event_t*)a_;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
107 ass_event_t* b = (ass_event_t*)b_;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
108 if (a->Start < b->Start)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
109 return -1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
110 else if (a->Start > b->Start)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
111 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
112 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
113 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
114 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
115
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
116 /// \brief Sort events by start time
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
117 /// \param tid track id
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
118 static void sort_events(ass_track_t* track) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
119 qsort(track->events, track->n_events, sizeof(ass_event_t), events_compare_f);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
120 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
121
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
122 // ==============================================================================================
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
123
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
124 static void skip_spaces(char** str) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
125 char* p = *str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
126 while ((*p==' ') || (*p=='\t'))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
127 ++p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
128 *str = p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
129 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
130
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
131 static void rskip_spaces(char** str, char* limit) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
132 char* p = *str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
133 while ((p >= limit) && ((*p==' ') || (*p=='\t')))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
134 --p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
135 *str = p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
136 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
137
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
138 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
139 * \brief find style by name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
140 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
141 * \param name style name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
142 * \return index in track->styles
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
143 * Returnes 0 if no styles found => expects at least 1 style.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
144 * Parsing code always adds "Default" style in the end.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
145 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
146 static int lookup_style(ass_track_t* track, char* name) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
147 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
148 for (i=0; i<track->n_styles; ++i) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
149 // FIXME: mb strcasecmp ?
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
150 if (strcmp(track->styles[i].Name, name) == 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
151 return i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
152 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
153 i = track->default_style;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
154 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
155 return i; // use the first style
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
156 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
157
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
158 static uint32_t string2color(char* p) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
159 uint32_t tmp;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
160 (void)strtocolor(&p, &tmp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
161 return tmp;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
162 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
163
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
164 static long long string2timecode(char* p) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
165 unsigned h, m, s, ms;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
166 long long tm;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
167 int res = sscanf(p, "%1d:%2d:%2d.%2d", &h, &m, &s, &ms);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
168 if (res < 4) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
169 mp_msg(MSGT_GLOBAL, MSGL_WARN, "bad timestamp\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
170 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
171 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
172 tm = ((h * 60 + m) * 60 + s) * 1000 + ms * 10;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
173 return tm;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
174 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
175
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
176 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
177 * \brief converts numpad-style align to align.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
178 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
179 static int numpad2align(int val) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
180 int res, v;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
181 v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
182 if (v != 0) v = 3 - v;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
183 res = ((val - 1) % 3) + 1; // horizontal alignment
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
184 res += v*4;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
185 return res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
186 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
187
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
188 #define NEXT(str,token) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
189 token = next_token(&str); \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
190 if (!token) break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
191
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
192 #define ANYVAL(name,func) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
193 } else if (strcasecmp(tname, #name) == 0) { \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
194 target->name = func(token); \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
195 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "%s = %s\n", #name, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
196 #define STRVAL(name) ANYVAL(name,strdup)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
197 #define COLORVAL(name) ANYVAL(name,string2color)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
198 #define INTVAL(name) ANYVAL(name,atoi)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
199 #define FPVAL(name) ANYVAL(name,atof)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
200 #define TIMEVAL(name) ANYVAL(name,string2timecode)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
201 #define STYLEVAL(name) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
202 } else if (strcasecmp(tname, #name) == 0) { \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
203 target->name = lookup_style(track, token); \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
204 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "%s = %s\n", #name, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
205
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
206 #define ALIAS(alias,name) \
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
207 if (strcasecmp(tname, #alias) == 0) {tname = #name;}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
208
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
209 static char* next_token(char** str) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
210 char* p = *str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
211 char* start;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
212 skip_spaces(&p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
213 if (*p == '\0') {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
214 *str = p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
215 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
216 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
217 start = p; // start of the token
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
218 for (; (*p != '\0') && (*p != ','); ++p) {}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
219 if (*p == '\0') {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
220 *str = p; // eos found, str will point to '\0' at exit
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
221 } else {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
222 *p = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
223 *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
224 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
225 --p; // end of current token
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
226 rskip_spaces(&p, start);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
227 if (p < start)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
228 p = start; // empty token
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
229 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
230 ++p; // the first space character, or '\0'
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
231 *p = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
232 return start;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
233 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
234 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
235 * \brief Parse the tail of Dialogue line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
236 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
237 * \param event parsed data goes here
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
238 * \param str string to parse, zero-terminated
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
239 * \param n_ignored number of format options to skip at the beginning
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
240 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
241 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
242 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
243 char* token;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
244 char* tname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
245 char* p = str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
246 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
247 ass_event_t* target = event;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
248
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
249 char* format = strdup(track->event_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
250 char* q = format; // format scanning pointer
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
251
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
252 for (i = 0; i < n_ignored; ++i) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
253 NEXT(q, tname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
254 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
255
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
256 while (1) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
257 NEXT(q, tname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
258 if (strcasecmp(tname, "Text") == 0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
259 char* last;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
260 event->Text = strdup(p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
261 last = event->Text + strlen(event->Text) - 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
262 if (*last == '\r')
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
263 *last = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
264 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Text = %s\n", event->Text);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
265 event->Duration -= event->Start;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
266 free(format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
267 return 0; // "Text" is always the last
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
268 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
269 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
270
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
271 ALIAS(End,Duration) // temporarily store end timecode in event->Duration
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
272 if (0) { // cool ;)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
273 STYLEVAL(Style)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
274 STRVAL(Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
275 STRVAL(Effect)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
276 INTVAL(MarginL)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
277 INTVAL(MarginR)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
278 INTVAL(MarginV)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
279 TIMEVAL(Start)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
280 TIMEVAL(Duration)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
281 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
282 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
283 free(format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
284 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
285 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
286
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
287 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
288 * \brief Parse the Style line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
289 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
290 * \param str string to parse, zero-terminated
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
291 * Allocates a new style struct.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
292 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
293 static int process_style(ass_track_t* track, char *str)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
294 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
295
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
296 char* token;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
297 char* tname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
298 char* p = str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
299 char* format;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
300 char* q; // format scanning pointer
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
301 int sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
302 ass_style_t* style;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
303 ass_style_t* target;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
304
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
305 if (!track->style_format) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
306 // no style format header
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
307 // probably an ancient script version
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
308 if (track->track_type == TRACK_TYPE_SSA)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
309 track->style_format = strdup("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
310 "TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
311 "Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
312 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
313 track->style_format = strdup("Name, Fontname, Fontsize, PrimaryColour, SecondaryColour,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
314 "OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
315 "ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow,"
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
316 "Alignment, MarginL, MarginR, MarginV, Encoding");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
317 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
318
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
319 q = format = strdup(track->style_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
320
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
321 mp_msg(MSGT_GLOBAL, MSGL_V, "[%p] Style: %s\n", track, str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
322
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
323 sid = ass_alloc_style(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
324
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
325 style = track->styles + sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
326 target = style;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
327 // fill style with some default values
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
328 style->ScaleX = 100.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
329 style->ScaleY = 100.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
330
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
331 while (1) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
332 NEXT(q, tname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
333 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
334
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
335 // 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
336
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
337 if (0) { // cool ;)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
338 STRVAL(Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
339 if ((strcmp(target->Name, "Default")==0) || (strcmp(target->Name, "*Default")==0))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
340 track->default_style = sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
341 STRVAL(FontName)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
342 COLORVAL(PrimaryColour)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
343 COLORVAL(SecondaryColour)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
344 COLORVAL(OutlineColour) // TertiaryColor
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
345 COLORVAL(BackColour)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
346 // SSA uses BackColour for both outline and shadow
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
347 // 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
348 if (track->track_type == TRACK_TYPE_SSA)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
349 target->OutlineColour = target->BackColour;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
350 INTVAL(FontSize)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
351 INTVAL(Bold)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
352 INTVAL(Italic)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
353 INTVAL(Underline)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
354 INTVAL(StrikeOut)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
355 INTVAL(Spacing)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
356 INTVAL(Angle)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
357 INTVAL(BorderStyle)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
358 INTVAL(Alignment)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
359 if (track->track_type == TRACK_TYPE_ASS)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
360 target->Alignment = numpad2align(target->Alignment);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
361 INTVAL(MarginL)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
362 INTVAL(MarginR)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
363 INTVAL(MarginV)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
364 INTVAL(Encoding)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
365 FPVAL(ScaleX)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
366 FPVAL(ScaleY)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
367 FPVAL(Outline)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
368 FPVAL(Shadow)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
369 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
370 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
371 style->ScaleX /= 100.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
372 style->ScaleY /= 100.;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
373 if (!style->Name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
374 style->Name = strdup("Default");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
375 if (!style->FontName)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
376 style->FontName = strdup("Arial");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
377 free(format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
378 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
379
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
380 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
381
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
382 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
383 * \brief Parse a header line
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
384 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
385 * \param str string to parse, zero-terminated
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
386 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
387 static int process_header_line(ass_track_t* track, char *str)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
388 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
389 static int events_section_started = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
390
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
391 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "=== Header: %s\n", str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
392 if (strncmp(str, "PlayResX:", 9)==0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
393 track->PlayResX = atoi(str + 9);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
394 } else if (strncmp(str,"PlayResY:", 9)==0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
395 track->PlayResY = atoi(str + 9);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
396 } else if (strncmp(str,"Timer:", 6)==0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
397 track->Timer = atof(str + 6);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
398 } else if (strstr(str,"Styles]")) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
399 events_section_started = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
400 if (strchr(str, '+'))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
401 track->track_type = TRACK_TYPE_ASS;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
402 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
403 track->track_type = TRACK_TYPE_SSA;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
404 } else if (strncmp(str,"[Events]", 8)==0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
405 events_section_started = 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
406 } else if (strncmp(str,"Format:", 7)==0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
407 char* p = str + 7;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
408 skip_spaces(&p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
409 if (events_section_started) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
410 track->event_format = strdup(p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
411 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Event format: %s\n", track->event_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
412 } else {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
413 track->style_format = strdup(p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
414 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "Style format: %s\n", track->style_format);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
415 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
416 } else if (strncmp(str,"Style:", 6)==0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
417 char* p = str + 6;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
418 skip_spaces(&p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
419 process_style(track, p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
420 } else if (strncmp(str,"WrapStyle:", 10)==0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
421 track->WrapStyle = atoi(str + 10);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
422 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
423 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
424 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
425
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
426 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
427 * \brief Process CodecPrivate section of subtitle stream
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
428 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
429 * \param data string to parse
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
430 * \param size length of data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
431 CodecPrivate section contains [Stream Info] and [V4+ Styles] sections
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
432 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
433 void ass_process_chunk(ass_track_t* track, char *data, int size)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
434 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
435 char* str = malloc(size + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
436 char* p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
437 int sid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
438
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
439 memcpy(str, data, size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
440 str[size] = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
441
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
442 p = str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
443 while(1) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
444 char* q;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
445 for (;((*p=='\r')||(*p=='\n'));++p) {}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
446 for (q=p; ((*q!='\0')&&(*q!='\r')&&(*q!='\n')); ++q) {};
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
447 if (q==p)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
448 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
449 if (*q != '\0')
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
450 *(q++) = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
451 process_header_line(track, p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
452 if (*q == '\0')
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
453 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
454 p = q;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
455 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
456 free(str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
457
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
458 // add "Default" style to the end
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
459 // 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
460 sid = ass_alloc_style(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
461 track->styles[sid].Name = strdup("Default");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
462 track->styles[sid].FontName = strdup("Arial");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
463
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
464 if (!track->event_format) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
465 // probably an mkv produced by ancient mkvtoolnix
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
466 // such files don't have [Events] and Format: headers
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
467 if (track->track_type == TRACK_TYPE_SSA)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
468 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
469 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
470 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
471 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
472 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
473
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
474 static int check_duplicate_event(ass_track_t* track, int ReadOrder)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
475 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
476 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
477 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
478 if (track->events[i].ReadOrder == ReadOrder)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
479 return 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
480 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
481 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
482
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
483 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
484 * \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
485 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
486 * \param data string to parse
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
487 * \param size length of data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
488 * \param timecode starting time of the event (milliseconds)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
489 * \param duration duration of the event (milliseconds)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
490 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
491 void ass_process_line(ass_track_t* track, char *data, int size, long long timecode, long long duration)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
492 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
493 char* str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
494 int eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
495 char* p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
496 char* token;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
497 ass_event_t* event;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
498
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
499 if (!track->event_format) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
500 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Event format header missing\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
501 return;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
502 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
503
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
504 str = malloc(size + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
505 memcpy(str, data, size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
506 str[size] = '\0';
19378
d9d58ba1b833 Replace %lld with PRId64, part 2.
eugeni
parents: 19374
diff changeset
507 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
508
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
509 eid = ass_alloc_event(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
510 event = track->events + eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
511
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
512 p = str;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
513
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
514 do {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
515 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
516 event->ReadOrder = atoi(token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
517 if (check_duplicate_event(track, event->ReadOrder))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
518 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
519
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
520 NEXT(p, token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
521 event->Layer = atoi(token);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
522
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
523 process_event_tail(track, event, p, 3);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
524
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
525 event->Start = timecode;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
526 event->Duration = duration;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
527
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
528 free(str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
529 return;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
530 // dump_events(tid);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
531 } while (0);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
532 // some error
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
533 free_event(track, eid);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
534 free(str);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
535 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
536
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
537 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
538 * \brief Process a line from external file.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
539 * \param track track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
540 * \param str string to parse
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
541 * \param size length of data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
542 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
543 static void ass_process_external_line(ass_track_t* track, char *str, int size)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
544 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
545 int eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
546 ass_event_t* event;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
547
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
548 eid = ass_alloc_event(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
549 event = track->events + eid;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
550
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
551 if (strncmp("Dialogue:", str, 9) != 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
552 return;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
553
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
554 str += 9;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
555 while (*str == ' ') {++str;}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
556
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
557 process_event_tail(track, event, str, 0);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
558 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
559
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
560 #ifdef USE_ICONV
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
561 /** \brief recode buffer to utf-8
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
562 * constraint: sub_cp != 0
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
563 * \param data pointer to text buffer
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
564 * \param size buffer size
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
565 * \return a pointer to recoded buffer, caller is responsible for freeing it
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
566 **/
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
567 static char* sub_recode(char* data, size_t size)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
568 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
569 static iconv_t icdsc = (iconv_t)(-1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
570 char* tocp = "UTF-8";
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
571 char* outbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
572 assert(sub_cp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
573
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
574 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
575 char* cp_tmp = sub_cp;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
576 #ifdef HAVE_ENCA
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
577 char enca_lang[3], enca_fallback[100];
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
578 if (sscanf(sub_cp, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
579 || sscanf(sub_cp, "ENCA:%2s:%99s", enca_lang, enca_fallback) == 2) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
580 cp_tmp = guess_buffer_cp((unsigned char*)data, size, enca_lang, enca_fallback);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
581 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
582 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
583 if ((icdsc = iconv_open (tocp, cp_tmp)) != (iconv_t)(-1)){
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
584 mp_msg(MSGT_SUBREADER,MSGL_V,"LIBSUB: opened iconv descriptor.\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
585 } else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
586 mp_msg(MSGT_SUBREADER,MSGL_ERR,"LIBSUB: error opening iconv descriptor.\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
587 #ifdef HAVE_ENCA
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
588 if (cp_tmp) free(cp_tmp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
589 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
590 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
591
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
592 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
593 size_t osize = size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
594 size_t ileft = size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
595 size_t oleft = size - 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
596 char* ip;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
597 char* op;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
598 size_t rc;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
599
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
600 outbuf = malloc(size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
601 ip = data;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
602 op = outbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
603
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
604 while (ileft) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
605 rc = iconv(icdsc, &ip, &ileft, &op, &oleft);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
606 if (rc == (size_t)(-1)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
607 if (errno == E2BIG) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
608 int offset = op - outbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
609 outbuf = (char*)realloc(outbuf, osize + size);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
610 op = outbuf + offset;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
611 osize += size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
612 oleft += size;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
613 } else {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
614 mp_msg(MSGT_SUBREADER, MSGL_WARN, "LIBSUB: error recoding file.\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
615 return NULL;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
616 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
617 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
618 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
619 outbuf[osize - oleft - 1] = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
620 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
621
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
622 if (icdsc != (iconv_t)(-1)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
623 (void)iconv_close(icdsc);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
624 icdsc = (iconv_t)(-1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
625 mp_msg(MSGT_SUBREADER,MSGL_V,"LIBSUB: closed iconv descriptor.\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
626 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
627
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
628 return outbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
629 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
630 #endif // ICONV
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
631
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
632 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
633 * \brief Read subtitles from file.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
634 * \param fname file name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
635 * \return newly allocated track
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
636 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
637 ass_track_t* ass_read_file(char* fname)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
638 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
639 int res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
640 long sz;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
641 long bytes_read;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
642 char* buf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
643 char* p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
644 int events_reached;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
645 ass_track_t* track;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
646
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
647 FILE* fp = fopen(fname, "rb");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
648 if (!fp) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
649 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
650 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
651 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
652 res = fseek(fp, 0, SEEK_END);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
653 if (res == -1) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
654 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
655 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
656 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
657 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
658
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
659 sz = ftell(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
660 rewind(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
661
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
662 if (sz > 10*1024*1024) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
663 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
664 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
665 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
666 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
667
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
668 mp_msg(MSGT_GLOBAL, MSGL_V, "file size: %ld\n", sz);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
669
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
670 buf = malloc(sz + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
671 assert(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
672 bytes_read = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
673 do {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
674 res = fread(buf + bytes_read, 1, sz - bytes_read, fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
675 if (res <= 0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
676 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
677 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
678 free(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
679 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
680 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
681 bytes_read += res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
682 } while (sz - bytes_read > 0);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
683 buf[sz] = '\0';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
684 fclose(fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
685
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
686 #ifdef USE_ICONV
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
687 if (sub_cp) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
688 char* tmpbuf = sub_recode(buf, sz);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
689 free(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
690 if (!tmpbuf)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
691 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
692 buf = tmpbuf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
693 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
694 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
695
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
696 track = ass_new_track();
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
697 track->name = strdup(fname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
698
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
699 // process header
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
700 events_reached = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
701 p = buf;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
702 while (p && (*p)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
703 while (*p == '\n') {++p;}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
704 if (strncmp(p, "[Events]", 8) == 0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
705 events_reached = 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
706 } else if ((strncmp(p, "Format:", 7) == 0) && (events_reached)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
707 p = strchr(p, '\n');
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
708 if (p == 0) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
709 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Incomplete subtitles\n");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
710 free(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
711 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
712 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
713 ass_process_chunk(track, buf, p - buf + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
714 ++p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
715 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
716 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
717 p = strchr(p, '\n');
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
718 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
719 // process events
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
720 while (p && (*p)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
721 char* next;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
722 int len;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
723 while (*p == '\n') {++p;}
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
724 next = strchr(p, '\n');
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
725 len = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
726 if (next) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
727 len = next - p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
728 *next = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
729 } else {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
730 len = strlen(p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
731 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
732 ass_process_external_line(track, p, len);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
733 if (next) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
734 p = next + 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
735 continue;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
736 } else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
737 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
738 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
739
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
740 free(buf);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
741
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
742 if (!events_reached) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
743 ass_free_track(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
744 return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
745 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
746
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
747 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
748
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
749 sort_events(track);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
750
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
751 // dump_events(forced_tid);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
752 return track;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
753 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
754
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
755 static char* validate_fname(char* name)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
756 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
757 char* fname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
758 char* p;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
759 char* q;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
760 unsigned code;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
761 int sz = strlen(name);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
762
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
763 q = fname = malloc(sz + 1);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
764 p = name;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
765 while (*p) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
766 code = utf8_get_char(&p);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
767 if (code == 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
768 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
769 if ( (code > 0x7F) ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
770 (code == '\\') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
771 (code == '/') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
772 (code == ':') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
773 (code == '*') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
774 (code == '?') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
775 (code == '<') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
776 (code == '>') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
777 (code == '|') ||
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
778 (code == 0))
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
779 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
780 *q++ = '_';
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
781 } else {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
782 *q++ = code;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
783 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
784 if (p - name > sz)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
785 break;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
786 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
787 *q = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
788 return fname;
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 /**
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
792 * \brief Process embedded matroska font. Saves it to ~/.mplayer/fonts.
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
793 * \param name attachment name
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
794 * \param data binary font data
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
795 * \param data_size data size
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
796 */
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
797 void ass_process_font(const char* name, char* data, int data_size)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
798 {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
799 char buf[1000];
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
800 FILE* fp = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
801 int rc;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
802 struct stat st;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
803 char* fname;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
804
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
805 char* fonts_dir = get_path("fonts");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
806 rc = stat(fonts_dir, &st);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
807 if (rc) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
808 int res;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
809 #ifndef __MINGW32__
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
810 res = mkdir(fonts_dir, 0700);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
811 #else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
812 res = mkdir(fonts_dir);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
813 #endif
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
814 if (res) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
815 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Failed to create: %s\n", fonts_dir);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
816 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
817 } else if (!S_ISDIR(st.st_mode)) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
818 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Not a directory: %s\n", fonts_dir);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
819 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
820
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
821 fname = validate_fname((char*)name);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
822
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
823 snprintf(buf, 1000, "%s/%s", fonts_dir, fname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
824 free(fname);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
825 free(fonts_dir);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
826
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
827 fp = fopen(buf, "wb");
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
828 if (!fp) return;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
829
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
830 fwrite(data, data_size, 1, fp);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
831 fclose(fp);
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 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
835 int i;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
836
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
837 if (movement == 0) return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
838 if (track->n_events == 0) return 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
839
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
840 if (movement < 0)
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
841 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
842 else
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
843 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
844
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
845 // -1 and n_events are ok
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
846 assert(i >= -1); assert(i <= track->n_events);
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
847 i += movement;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
848 if (i < 0) i = 0;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
849 if (i >= track->n_events) i = track->n_events - 1;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
850 return ((long long)track->events[i].Start) - now;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
851 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
852
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
853 ass_track_t* ass_new_track(void) {
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
854 ass_track_t* track = calloc(1, sizeof(ass_track_t));
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
855 return track;
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
856 }
9e95ac641e77 Initial libass release (without mencoder support).
eugeni
parents:
diff changeset
857