Mercurial > mplayer.hg
annotate subreader.c @ 1671:e6804fef9061
print AVI info block (copyright,artist etc) (-v only)
author | arpi |
---|---|
date | Thu, 23 Aug 2001 21:10:31 +0000 |
parents | cbedcfab877b |
children | 827b551bbcab |
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 |
1640
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
243 p=line; |
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
244 // finds the body of the subtitle |
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
245 for (i=0; i<3; i++){ |
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
246 p=strchr(p,':')+1; |
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
247 } |
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
248 i=0; |
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
249 |
818 | 250 if (*p!='|') { |
251 // | |
252 next = p,i=0; | |
253 while ((next =sub_readtext (next, &(current->text[i])))) { | |
254 if (current->text[i]==ERR) {return ERR;} | |
255 i++; | |
1081 | 256 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;} |
818 | 257 } |
258 current->lines=i+1; | |
259 } | |
260 } | |
261 return current; | |
262 } | |
263 | |
850 | 264 subtitle *sub_read_line_rt(FILE *fd,subtitle *current) { |
265 //TODO: This format uses quite rich (sub/super)set of xhtml | |
266 // I couldn't check it since DTD is not included. | |
267 // WARNING: full XML parses can be required for proper parsing | |
268 char line[1001]; | |
269 int a1,a2,a3,a4,b1,b2,b3,b4; | |
270 char *p=NULL,*next=NULL; | |
271 int i,len,plen; | |
272 | |
273 bzero (current, sizeof(current)); | |
274 | |
275 while (!current->text[0]) { | |
276 if (!fgets (line, 1000, fd)) return NULL; | |
277 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0 | |
278 //to describe the same moment in time. Maybe there are even more formats in use. | |
279 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8) | |
280 plen=a1=a2=a3=a4=b1=b2=b3=b4=0; | |
281 if ( | |
282 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) && | |
283 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) && | |
284 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) && | |
285 ((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) && | |
286 ((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) | |
287 ) | |
288 continue; | |
289 current->start = a1*360000+a2*6000+a3*100+a4/10; | |
290 current->end = b1*360000+b2*6000+b3*100+b4/10; | |
291 p=line; p+=plen;i=0; | |
292 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml? | |
293 next = strstr(line,"<clear/>")+8;i=0; | |
294 while ((next =sub_readtext (next, &(current->text[i])))) { | |
295 if (current->text[i]==ERR) {return ERR;} | |
296 i++; | |
1081 | 297 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;} |
850 | 298 } |
299 current->lines=i+1; | |
300 } | |
301 return current; | |
302 } | |
303 | |
921 | 304 subtitle *sub_read_line_ssa(FILE *fd,subtitle *current) { |
305 int hour1, min1, sec1, hunsec1, | |
306 hour2, min2, sec2, hunsec2, nothing; | |
307 | |
308 char line[1000], | |
309 line2[1000]; | |
310 do { | |
311 if (!fgets (line, 1000, fd)) return NULL; | |
312 } while (sscanf (line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d," | |
313 "*Default,%d,%d,%d,%d,,%[^\n\r]", ¬hing, &hour1, &min1, | |
314 &sec1, &hunsec1, | |
315 &hour2, &min2, &sec2, &hunsec2, ¬hing, | |
316 ¬hing, ¬hing, ¬hing, line2) < 14); | |
317 current->lines=1; | |
318 current->start = 360000*hour1 + 6000*min1 + 100*sec1 + hunsec1; | |
319 current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2; | |
320 current->text[0]=(char *) malloc(strlen(line2)+1); | |
321 strcpy(current->text[0],line2); | |
818 | 322 |
921 | 323 return current; |
324 } | |
258 | 325 |
1081 | 326 subtitle *sub_read_line_dunnowhat(FILE *fd,subtitle *current) { |
327 char line[1001]; | |
328 char text[1001]; | |
329 | |
330 bzero (current, sizeof(current)); | |
331 | |
332 if (!fgets (line, 1000, fd)) | |
333 return NULL; | |
334 if (sscanf (line, "%ld,%ld,\"%[^\"]", &(current->start), | |
335 &(current->end), text) <3) | |
336 return ERR; | |
337 current->text[0] = strdup(text); | |
338 current->lines = 1; | |
339 | |
340 return current; | |
341 } | |
342 | |
258 | 343 int sub_autodetect (FILE *fd) { |
344 char line[1001]; | |
345 int i,j=0; | |
346 // char *p; | |
347 | |
624 | 348 while (j < 100) { |
258 | 349 j++; |
350 if (!fgets (line, 1000, fd)) | |
351 return -1; | |
352 | |
624 | 353 if (sscanf (line, "{%d}{%d}", &i, &i)==2) |
258 | 354 {sub_uses_time=0;return 0;} |
269 | 355 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) |
258 | 356 {sub_uses_time=1;return 1;} |
269 | 357 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) |
258 | 358 {sub_uses_time=1;return 2;} |
624 | 359 if (strstr (line, "<SAMI>")) |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
360 {sub_uses_time=1; return 3;} |
818 | 361 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3) |
362 {sub_uses_time=1;return 4;} | |
850 | 363 //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
|
364 // too weak test for RT |
18c43d261c35
corrected strcmp() bug, now it works again with every subs (it was broken)
laaz
parents:
896
diff
changeset
|
365 // Please someone who knows the format of RT... FIX IT!!! |
921 | 366 // 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
|
367 if ( *line == '<' ) |
850 | 368 {sub_uses_time=1;return 5;} |
921 | 369 |
370 // I have only seen only 1 piece of .ssa file. | |
371 // It may be not correct (tell me if it's not) | |
372 if (!memcmp(line, "Dialogue: Marked", 16)) | |
373 {sub_uses_time=1; return 6;} | |
1081 | 374 if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3) |
375 {sub_uses_time=0;return 7;} | |
258 | 376 } |
624 | 377 |
378 return -1; // too many bad lines | |
258 | 379 } |
380 | |
381 | |
382 subtitle* sub_read_file (char *filename) { | |
383 FILE *fd; | |
384 int n_max; | |
385 subtitle *first; | |
1081 | 386 subtitle * (*func[])(FILE *fd,subtitle *dest)= |
258 | 387 { |
388 sub_read_line_microdvd, | |
389 sub_read_line_subrip, | |
624 | 390 sub_read_line_third, |
818 | 391 sub_read_line_sami, |
850 | 392 sub_read_line_vplayer, |
921 | 393 sub_read_line_rt, |
1081 | 394 sub_read_line_ssa, |
395 sub_read_line_dunnowhat | |
258 | 396 }; |
397 | |
398 fd=fopen (filename, "r"); if (!fd) return NULL; | |
399 | |
400 sub_format=sub_autodetect (fd); | |
401 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;} | |
624 | 402 printf ("SUB: Detected subtitle file format: %d\n",sub_format); |
258 | 403 |
404 rewind (fd); | |
405 | |
406 sub_num=0;n_max=32; | |
407 first=(subtitle *)malloc(n_max*sizeof(subtitle)); | |
408 if(!first) return NULL; | |
409 | |
410 while(1){ | |
411 subtitle *sub; | |
412 if(sub_num>=n_max){ | |
413 n_max+=16; | |
414 first=realloc(first,n_max*sizeof(subtitle)); | |
415 } | |
416 sub=func[sub_format](fd,&first[sub_num]); | |
417 if(!sub) break; // EOF | |
418 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid | |
419 } | |
420 | |
421 fclose(fd); | |
422 | |
423 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); | |
269 | 424 printf ("SUB: Read %i subtitles", sub_num); |
624 | 425 if (sub_errs) printf (", %i bad line(s).\n", sub_errs); |
269 | 426 else printf (".\n"); |
258 | 427 |
428 return first; | |
429 } | |
430 | |
892 | 431 #if 0 |
509 | 432 char * strreplace( char * in,char * what,char * whereof ) |
433 { | |
434 int i; | |
435 char * tmp; | |
436 | |
437 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL; | |
438 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i]; | |
439 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0; | |
440 return in; | |
441 } | |
892 | 442 #endif |
509 | 443 |
892 | 444 char * sub_filename(char* path, char * fname ) |
509 | 445 { |
1501
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1081
diff
changeset
|
446 extern int sub_utf8; |
892 | 447 char * sub_name1; |
448 char * sub_name2; | |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
449 char * aviptr1, * aviptr2, * tmp; |
892 | 450 int i,j; |
451 FILE * f; | |
452 int pos=0; | |
453 char * sub_exts[] = | |
1501
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1081
diff
changeset
|
454 { ".utf", |
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1081
diff
changeset
|
455 ".UTF", |
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1081
diff
changeset
|
456 ".sub", |
509 | 457 ".SUB", |
458 ".srt", | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
459 ".SRT", |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
460 ".smi", |
850 | 461 ".SMI", |
462 ".rt", | |
463 ".RT", | |
464 ".txt", | |
1081 | 465 ".TXT", |
466 ".ssa", | |
467 ".SSA"}; | |
892 | 468 |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
469 |
509 | 470 if ( fname == NULL ) return NULL; |
892 | 471 |
472 sub_name1=strrchr(fname,'.'); | |
473 if (!sub_name1) return NULL; | |
474 pos=sub_name1-fname; | |
475 | |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
476 sub_name1=malloc(strlen(fname)+8); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
477 strcpy(sub_name1,fname); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
478 |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
479 sub_name2=malloc (strlen(path) + strlen(fname) + 8); |
1081 | 480 if ((tmp=strrchr(fname,'/'))) |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
481 sprintf (sub_name2, "%s%s", path, tmp+1); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
482 else |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
483 sprintf (sub_name2, "%s%s", path, fname); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
484 |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
485 aviptr1=strrchr(sub_name1,'.'); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
486 aviptr2=strrchr(sub_name2,'.'); |
892 | 487 |
488 for(j=0;j<=1;j++){ | |
489 char* sub_name=j?sub_name1:sub_name2; | |
490 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
|
491 strcpy(j?aviptr1:aviptr2,sub_exts[i]); |
935 | 492 // printf("trying: '%s'\n",sub_name); |
892 | 493 if((f=fopen( sub_name,"rt" ))) { |
509 | 494 fclose( f ); |
495 printf( "SUB: Detected sub file: %s\n",sub_name ); | |
1501
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1081
diff
changeset
|
496 if (i<2) sub_utf8=1; |
509 | 497 return sub_name; |
892 | 498 } |
509 | 499 } |
892 | 500 } |
501 | |
509 | 502 return NULL; |
503 } | |
504 | |
625 | 505 #if 0 |
258 | 506 int main(int argc, char **argv) { // for testing |
507 | |
508 int i,j; | |
509 subtitle *subs; | |
510 subtitle *egysub; | |
511 | |
512 if(argc<2){ | |
513 printf("\nUsage: subreader filename.sub\n\n"); | |
514 exit(1); | |
515 } | |
516 | |
624 | 517 subs=sub_read_file(argv[1]); |
258 | 518 if(!subs){ |
519 printf("Couldn't load file... let's write a bugreport :)\n"); | |
520 exit(1); | |
521 } | |
522 | |
523 for(j=0;j<sub_num;j++){ | |
524 egysub=&subs[j]; | |
1081 | 525 printf ("%i line%c (%li-%li) ", |
258 | 526 egysub->lines, |
527 (1==egysub->lines)?' ':'s', | |
528 egysub->start, | |
529 egysub->end); | |
530 for (i=0; i<egysub->lines; i++) { | |
531 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> "); | |
532 } | |
533 printf ("\n"); | |
534 } | |
535 | |
536 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); | |
537 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs); | |
538 return 0; | |
539 } | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
540 #endif |