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