Mercurial > mplayer.hg
annotate subreader.c @ 1209:18539f68dd37
ao_subdevice support
author | arpi_esp |
---|---|
date | Sat, 23 Jun 2001 19:40:04 +0000 |
parents | 48cd2c5a9542 |
children | d40f2b686846 |
rev | line source |
---|---|
258 | 1 /* |
2 * Subtitle reader with format autodetection | |
3 * | |
4 * Written by laaz | |
5 * Some code cleanup & realloc() by A'rpi/ESP-team | |
1081 | 6 * dunnowhat sub format by szabi |
258 | 7 */ |
8 | |
9 | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <string.h> | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
13 #include <ctype.h> |
258 | 14 |
15 #include "subreader.h" | |
16 | |
17 #define ERR (void *)-1 | |
18 | |
19 | |
20 int sub_uses_time=0; | |
21 int sub_errs=0; | |
624 | 22 int sub_num=0; // number of subtitle structs |
23 int sub_format=-1; // 0 for microdvd | |
24 // 1 for SubRip | |
921 | 25 // 2 for SubViewer |
624 | 26 // 3 for SAMI (smi) |
818 | 27 // 4 for vplayer format |
850 | 28 // 5 for RT format |
921 | 29 // 6 for ssa (Sub Station Alpha) |
1081 | 30 // 7 for ... erm ... dunnowhat. tell me if you know |
624 | 31 |
32 int eol(char p) { | |
33 return (p=='\r' || p=='\n' || p=='\0'); | |
34 } | |
35 | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
36 static inline void trail_space(char *s) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
37 int i; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
38 while (isspace(*s)) strcpy(s, s + 1); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
39 i = strlen(s) - 1; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
40 while (i > 0 && isspace(s[i])) s[i--] = '\0'; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
41 } |
624 | 42 |
43 subtitle *sub_read_line_sami(FILE *fd, subtitle *current) { | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
44 static char line[1001]; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
45 static char *s = NULL; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
46 char text[1000], *p, *q; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
47 int state; |
624 | 48 |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
49 current->lines = current->start = current->end = 0; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
50 state = 0; |
624 | 51 |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
52 /* read the first line */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
53 if (!s) |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
54 if (!(s = fgets(line, 1000, fd))) return 0; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
55 |
624 | 56 do { |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
57 switch (state) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
58 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
59 case 0: /* find "START=" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
60 s = strstr (s, "Start="); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
61 if (s) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
62 current->start = strtol (s + 6, &s, 0) / 10; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
63 state = 1; continue; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
64 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
65 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
66 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
67 case 1: /* find "<P" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
68 if ((s = strstr (s, "<P"))) { s += 2; state = 2; continue; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
69 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
70 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
71 case 2: /* find ">" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
72 if ((s = strchr (s, '>'))) { s++; state = 3; p = text; continue; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
73 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
74 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
75 case 3: /* get all text until '<' appears */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
76 if (*s == '\0') { break; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
77 else if (*s == '<') { state = 4; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
78 else if (!strncasecmp (s, " ", 6)) { *p++ = ' '; s += 6; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
79 else if (*s == '\r') { s++; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
80 else if (!strncasecmp (s, "<br>", 4) || *s == '\n') { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
81 *p = '\0'; p = text; trail_space (text); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
82 if (text[0] != '\0') |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
83 current->text[current->lines++] = strdup (text); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
84 if (*s == '\n') s++; else s += 4; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
85 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
86 else *p++ = *s++; |
624 | 87 continue; |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
88 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
89 case 4: /* get current->end or skip <TAG> */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
90 q = strstr (s, "Start="); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
91 if (q) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
92 current->end = strtol (q + 6, &q, 0) / 10 - 1; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
93 *p = '\0'; trail_space (text); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
94 if (text[0] != '\0') |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
95 current->text[current->lines++] = strdup (text); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
96 if (current->lines > 0) { state = 99; break; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
97 state = 0; continue; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
98 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
99 s = strchr (s, '>'); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
100 if (s) { s++; state = 3; continue; } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
101 break; |
624 | 102 } |
103 | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
104 /* read next line */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
105 if (state != 99 && !(s = fgets (line, 1000, fd))) return 0; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
106 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
107 } while (state != 99); |
624 | 108 |
109 return current; | |
110 } | |
258 | 111 |
112 | |
113 char *sub_readtext(char *source, char **dest) { | |
114 int len=0; | |
932 | 115 char *p=source; |
258 | 116 |
932 | 117 while ( !eol(*p) && *p!= '|' ) { |
118 p++,len++; | |
119 } | |
258 | 120 |
121 *dest= (char *)malloc (len+1); | |
122 if (!dest) {return ERR;} | |
123 | |
124 strncpy(*dest, source, len); | |
125 (*dest)[len]=0; | |
126 | |
127 while (*p=='\r' || *p=='\n' || *p=='|') p++; | |
128 | |
129 if (*p) return p; // not-last text field | |
130 else return NULL; // last text field | |
131 } | |
132 | |
133 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) { | |
134 char line[1001]; | |
135 char line2[1001]; | |
136 char *p, *next; | |
137 int i; | |
138 | |
139 bzero (current, sizeof(current)); | |
140 | |
141 do { | |
142 if (!fgets (line, 1000, fd)) return NULL; | |
932 | 143 } while (sscanf (line, "{%ld}{%ld}%[^\r\n]", &(current->start), &(current->end),line2) <3); |
258 | 144 |
932 | 145 p=line2; |
258 | 146 |
147 next=p, i=0; | |
1081 | 148 while ((next =sub_readtext (next, &(current->text[i])))) { |
270 | 149 if (current->text[i]==ERR) {return ERR;} |
258 | 150 i++; |
1081 | 151 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;} |
258 | 152 } |
932 | 153 current->lines= ++i; |
258 | 154 |
155 return current; | |
156 } | |
157 | |
158 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) { | |
159 char line[1001]; | |
160 int a1,a2,a3,a4,b1,b2,b3,b4; | |
161 char *p=NULL, *q=NULL; | |
162 int len; | |
163 | |
164 bzero (current, sizeof(current)); | |
165 | |
166 while (!current->text[0]) { | |
167 if (!fgets (line, 1000, fd)) return NULL; | |
269 | 168 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue; |
258 | 169 current->start = a1*360000+a2*6000+a3*100+a4; |
170 current->end = b1*360000+b2*6000+b3*100+b4; | |
171 | |
172 if (!fgets (line, 1000, fd)) return NULL; | |
173 | |
174 p=q=line; | |
175 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) { | |
176 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && strncmp(p,"[br]",4); p++,len++); | |
177 current->text[current->lines-1]=(char *)malloc (len+1); | |
178 if (!current->text[current->lines-1]) return ERR; | |
179 strncpy (current->text[current->lines-1], q, len); | |
270 | 180 current->text[current->lines-1][len]='\0'; |
258 | 181 if (!*p || *p=='\r' || *p=='\n') break; |
182 while (*p++!=']'); | |
183 } | |
184 } | |
185 return current; | |
186 } | |
187 | |
188 subtitle *sub_read_line_third(FILE *fd,subtitle *current) { | |
189 char line[1001]; | |
190 int a1,a2,a3,a4,b1,b2,b3,b4; | |
191 char *p=NULL; | |
192 int i,len; | |
193 | |
194 bzero (current, sizeof(current)); | |
195 | |
196 while (!current->text[0]) { | |
197 if (!fgets (line, 1000, fd)) return NULL; | |
269 | 198 if ((len=sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8) |
258 | 199 continue; |
200 current->start = a1*360000+a2*6000+a3*100+a4/10; | |
201 current->end = b1*360000+b2*6000+b3*100+b4/10; | |
202 for (i=0; i<SUB_MAX_TEXT;) { | |
269 | 203 if (!fgets (line, 1000, fd)) break; |
258 | 204 len=0; |
205 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++); | |
206 if (len) { | |
207 current->text[i]=(char *)malloc (len+1); | |
208 if (!current->text[i]) return ERR; | |
270 | 209 strncpy (current->text[i], line, len); current->text[i][len]='\0'; |
258 | 210 i++; |
211 } else { | |
212 break; | |
213 } | |
214 } | |
215 current->lines=i; | |
216 } | |
217 return current; | |
218 } | |
219 | |
818 | 220 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) { |
221 char line[1001]; | |
222 char line2[1001]; | |
223 int a1,a2,a3,b1,b2,b3; | |
1081 | 224 char *p=NULL, *next; |
858 | 225 int i,len,len2,plen; |
818 | 226 |
227 bzero (current, sizeof(current)); | |
228 | |
229 while (!current->text[0]) { | |
230 if (!fgets (line, 1000, fd)) return NULL; | |
858 | 231 if ((len=sscanf (line, "%d:%d:%d:%n",&a1,&a2,&a3,&plen)) < 3) |
818 | 232 continue; |
233 if (!fgets (line2, 1000, fd)) return NULL; | |
234 if ((len2=sscanf (line2, "%d:%d:%d:",&b1,&b2,&b3)) < 3) | |
235 continue; | |
236 // przewiñ o linijkê do ty³u: | |
237 fseek(fd,-strlen(line2),SEEK_CUR); | |
238 | |
239 current->start = a1*360000+a2*6000+a3*100; | |
240 current->end = b1*360000+b2*6000+b3*100; | |
896
d46de26aef48
there is another format that get detected as vplayers.
eyck
parents:
892
diff
changeset
|
241 if ((current->end - current->start) > 1000) {current->end = current->start + 1000;} // not too long though. |
818 | 242 // teraz czas na wkopiowanie stringu |
858 | 243 p=line; p+=plen;i=0; |
818 | 244 if (*p!='|') { |
245 // | |
246 next = p,i=0; | |
247 while ((next =sub_readtext (next, &(current->text[i])))) { | |
248 if (current->text[i]==ERR) {return ERR;} | |
249 i++; | |
1081 | 250 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;} |
818 | 251 } |
252 current->lines=i+1; | |
253 } | |
254 } | |
255 return current; | |
256 } | |
257 | |
850 | 258 subtitle *sub_read_line_rt(FILE *fd,subtitle *current) { |
259 //TODO: This format uses quite rich (sub/super)set of xhtml | |
260 // I couldn't check it since DTD is not included. | |
261 // WARNING: full XML parses can be required for proper parsing | |
262 char line[1001]; | |
263 int a1,a2,a3,a4,b1,b2,b3,b4; | |
264 char *p=NULL,*next=NULL; | |
265 int i,len,plen; | |
266 | |
267 bzero (current, sizeof(current)); | |
268 | |
269 while (!current->text[0]) { | |
270 if (!fgets (line, 1000, fd)) return NULL; | |
271 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0 | |
272 //to describe the same moment in time. Maybe there are even more formats in use. | |
273 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8) | |
274 plen=a1=a2=a3=a4=b1=b2=b3=b4=0; | |
275 if ( | |
276 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) && | |
277 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) && | |
278 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) && | |
279 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&b4,&plen)) < 6) && | |
280 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\" %*[Ee]nd=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4,&plen)) < 8) | |
281 ) | |
282 continue; | |
283 current->start = a1*360000+a2*6000+a3*100+a4/10; | |
284 current->end = b1*360000+b2*6000+b3*100+b4/10; | |
285 p=line; p+=plen;i=0; | |
286 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml? | |
287 next = strstr(line,"<clear/>")+8;i=0; | |
288 while ((next =sub_readtext (next, &(current->text[i])))) { | |
289 if (current->text[i]==ERR) {return ERR;} | |
290 i++; | |
1081 | 291 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;} |
850 | 292 } |
293 current->lines=i+1; | |
294 } | |
295 return current; | |
296 } | |
297 | |
921 | 298 subtitle *sub_read_line_ssa(FILE *fd,subtitle *current) { |
299 int hour1, min1, sec1, hunsec1, | |
300 hour2, min2, sec2, hunsec2, nothing; | |
301 | |
302 char line[1000], | |
303 line2[1000]; | |
304 do { | |
305 if (!fgets (line, 1000, fd)) return NULL; | |
306 } while (sscanf (line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d," | |
307 "*Default,%d,%d,%d,%d,,%[^\n\r]", ¬hing, &hour1, &min1, | |
308 &sec1, &hunsec1, | |
309 &hour2, &min2, &sec2, &hunsec2, ¬hing, | |
310 ¬hing, ¬hing, ¬hing, line2) < 14); | |
311 current->lines=1; | |
312 current->start = 360000*hour1 + 6000*min1 + 100*sec1 + hunsec1; | |
313 current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2; | |
314 current->text[0]=(char *) malloc(strlen(line2)+1); | |
315 strcpy(current->text[0],line2); | |
818 | 316 |
921 | 317 return current; |
318 } | |
258 | 319 |
1081 | 320 subtitle *sub_read_line_dunnowhat(FILE *fd,subtitle *current) { |
321 char line[1001]; | |
322 char text[1001]; | |
323 | |
324 bzero (current, sizeof(current)); | |
325 | |
326 if (!fgets (line, 1000, fd)) | |
327 return NULL; | |
328 if (sscanf (line, "%ld,%ld,\"%[^\"]", &(current->start), | |
329 &(current->end), text) <3) | |
330 return ERR; | |
331 current->text[0] = strdup(text); | |
332 current->lines = 1; | |
333 | |
334 return current; | |
335 } | |
336 | |
258 | 337 int sub_autodetect (FILE *fd) { |
338 char line[1001]; | |
339 int i,j=0; | |
340 // char *p; | |
341 | |
624 | 342 while (j < 100) { |
258 | 343 j++; |
344 if (!fgets (line, 1000, fd)) | |
345 return -1; | |
346 | |
624 | 347 if (sscanf (line, "{%d}{%d}", &i, &i)==2) |
258 | 348 {sub_uses_time=0;return 0;} |
269 | 349 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) |
258 | 350 {sub_uses_time=1;return 1;} |
269 | 351 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) |
258 | 352 {sub_uses_time=1;return 2;} |
624 | 353 if (strstr (line, "<SAMI>")) |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
354 {sub_uses_time=1; return 3;} |
818 | 355 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3) |
356 {sub_uses_time=1;return 4;} | |
850 | 357 //TODO: just checking if first line of sub starts with "<" is WAY |
913
18c43d261c35
corrected strcmp() bug, now it works again with every subs (it was broken)
laaz
parents:
896
diff
changeset
|
358 // too weak test for RT |
18c43d261c35
corrected strcmp() bug, now it works again with every subs (it was broken)
laaz
parents:
896
diff
changeset
|
359 // Please someone who knows the format of RT... FIX IT!!! |
921 | 360 // It may conflict with other sub formats in the future (actually it doesn't) |
913
18c43d261c35
corrected strcmp() bug, now it works again with every subs (it was broken)
laaz
parents:
896
diff
changeset
|
361 if ( *line == '<' ) |
850 | 362 {sub_uses_time=1;return 5;} |
921 | 363 |
364 // I have only seen only 1 piece of .ssa file. | |
365 // It may be not correct (tell me if it's not) | |
366 if (!memcmp(line, "Dialogue: Marked", 16)) | |
367 {sub_uses_time=1; return 6;} | |
1081 | 368 if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3) |
369 {sub_uses_time=0;return 7;} | |
258 | 370 } |
624 | 371 |
372 return -1; // too many bad lines | |
258 | 373 } |
374 | |
375 | |
376 subtitle* sub_read_file (char *filename) { | |
377 FILE *fd; | |
378 int n_max; | |
379 subtitle *first; | |
1081 | 380 subtitle * (*func[])(FILE *fd,subtitle *dest)= |
258 | 381 { |
382 sub_read_line_microdvd, | |
383 sub_read_line_subrip, | |
624 | 384 sub_read_line_third, |
818 | 385 sub_read_line_sami, |
850 | 386 sub_read_line_vplayer, |
921 | 387 sub_read_line_rt, |
1081 | 388 sub_read_line_ssa, |
389 sub_read_line_dunnowhat | |
258 | 390 }; |
391 | |
392 fd=fopen (filename, "r"); if (!fd) return NULL; | |
393 | |
394 sub_format=sub_autodetect (fd); | |
395 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;} | |
624 | 396 printf ("SUB: Detected subtitle file format: %d\n",sub_format); |
258 | 397 |
398 rewind (fd); | |
399 | |
400 sub_num=0;n_max=32; | |
401 first=(subtitle *)malloc(n_max*sizeof(subtitle)); | |
402 if(!first) return NULL; | |
403 | |
404 while(1){ | |
405 subtitle *sub; | |
406 if(sub_num>=n_max){ | |
407 n_max+=16; | |
408 first=realloc(first,n_max*sizeof(subtitle)); | |
409 } | |
410 sub=func[sub_format](fd,&first[sub_num]); | |
411 if(!sub) break; // EOF | |
412 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid | |
413 } | |
414 | |
415 fclose(fd); | |
416 | |
417 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); | |
269 | 418 printf ("SUB: Read %i subtitles", sub_num); |
624 | 419 if (sub_errs) printf (", %i bad line(s).\n", sub_errs); |
269 | 420 else printf (".\n"); |
258 | 421 |
422 return first; | |
423 } | |
424 | |
892 | 425 #if 0 |
509 | 426 char * strreplace( char * in,char * what,char * whereof ) |
427 { | |
428 int i; | |
429 char * tmp; | |
430 | |
431 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL; | |
432 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i]; | |
433 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0; | |
434 return in; | |
435 } | |
892 | 436 #endif |
509 | 437 |
892 | 438 char * sub_filename(char* path, char * fname ) |
509 | 439 { |
892 | 440 char * sub_name1; |
441 char * sub_name2; | |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
442 char * aviptr1, * aviptr2, * tmp; |
892 | 443 int i,j; |
444 FILE * f; | |
445 int pos=0; | |
446 char * sub_exts[] = | |
509 | 447 { ".sub", |
448 ".SUB", | |
449 ".srt", | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
450 ".SRT", |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
451 ".smi", |
850 | 452 ".SMI", |
453 ".rt", | |
454 ".RT", | |
455 ".txt", | |
1081 | 456 ".TXT", |
457 ".ssa", | |
458 ".SSA"}; | |
892 | 459 |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
460 |
509 | 461 if ( fname == NULL ) return NULL; |
892 | 462 |
463 sub_name1=strrchr(fname,'.'); | |
464 if (!sub_name1) return NULL; | |
465 pos=sub_name1-fname; | |
466 | |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
467 sub_name1=malloc(strlen(fname)+8); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
468 strcpy(sub_name1,fname); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
469 |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
470 sub_name2=malloc (strlen(path) + strlen(fname) + 8); |
1081 | 471 if ((tmp=strrchr(fname,'/'))) |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
472 sprintf (sub_name2, "%s%s", path, tmp+1); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
473 else |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
474 sprintf (sub_name2, "%s%s", path, fname); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
475 |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
476 aviptr1=strrchr(sub_name1,'.'); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
477 aviptr2=strrchr(sub_name2,'.'); |
892 | 478 |
479 for(j=0;j<=1;j++){ | |
480 char* sub_name=j?sub_name1:sub_name2; | |
481 for ( i=0;i<(sizeof(sub_exts)/sizeof(char*));i++ ) { | |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
482 strcpy(j?aviptr1:aviptr2,sub_exts[i]); |
935 | 483 // printf("trying: '%s'\n",sub_name); |
892 | 484 if((f=fopen( sub_name,"rt" ))) { |
509 | 485 fclose( f ); |
486 printf( "SUB: Detected sub file: %s\n",sub_name ); | |
487 return sub_name; | |
892 | 488 } |
509 | 489 } |
892 | 490 } |
491 | |
509 | 492 return NULL; |
493 } | |
494 | |
625 | 495 #if 0 |
258 | 496 int main(int argc, char **argv) { // for testing |
497 | |
498 int i,j; | |
499 subtitle *subs; | |
500 subtitle *egysub; | |
501 | |
502 if(argc<2){ | |
503 printf("\nUsage: subreader filename.sub\n\n"); | |
504 exit(1); | |
505 } | |
506 | |
624 | 507 subs=sub_read_file(argv[1]); |
258 | 508 if(!subs){ |
509 printf("Couldn't load file... let's write a bugreport :)\n"); | |
510 exit(1); | |
511 } | |
512 | |
513 for(j=0;j<sub_num;j++){ | |
514 egysub=&subs[j]; | |
1081 | 515 printf ("%i line%c (%li-%li) ", |
258 | 516 egysub->lines, |
517 (1==egysub->lines)?' ':'s', | |
518 egysub->start, | |
519 egysub->end); | |
520 for (i=0; i<egysub->lines; i++) { | |
521 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> "); | |
522 } | |
523 printf ("\n"); | |
524 } | |
525 | |
526 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); | |
527 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs); | |
528 return 0; | |
529 } | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
530 #endif |