Mercurial > mplayer.hg
annotate subreader.c @ 3716:f3f2566fa4aa
FreeBSD fix
author | nexus |
---|---|
date | Mon, 24 Dec 2001 16:24:35 +0000 |
parents | a33d0351630e |
children | a2325883c46c |
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 |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
15 #include "config.h" |
258 | 16 #include "subreader.h" |
17 | |
3701 | 18 #define ERR ((void *) -1) |
258 | 19 |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
20 #ifdef USE_ICONV |
2358 | 21 #ifdef __FreeBSD__ |
22 #include <giconv.h> | |
23 #else | |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
24 #include <iconv.h> |
2358 | 25 #endif |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
26 char *sub_cp=NULL; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
27 #endif |
258 | 28 |
2912 | 29 /* Maximal length of line of a subtitle */ |
30 #define LINE_LEN 1000 | |
2177 | 31 |
2178 | 32 static float mpsub_position=0; |
2177 | 33 |
258 | 34 int sub_uses_time=0; |
35 int sub_errs=0; | |
624 | 36 int sub_num=0; // number of subtitle structs |
3235
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
37 int sub_slacktime=2000; // 20 seconds |
2912 | 38 |
39 /* Use the SUB_* constant defined in the header file */ | |
40 int sub_format=SUB_INVALID; | |
624 | 41 |
3701 | 42 static int eol(char p) { |
624 | 43 return (p=='\r' || p=='\n' || p=='\0'); |
44 } | |
45 | |
3701 | 46 /* Remove leading and trailing space */ |
47 static void trail_space(char *s) { | |
48 int i = 0; | |
49 while (isspace(*s)) ++i; | |
50 if (i) strcpy(s, s + i); | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
51 i = strlen(s) - 1; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
52 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
|
53 } |
624 | 54 |
2343 | 55 |
624 | 56 subtitle *sub_read_line_sami(FILE *fd, subtitle *current) { |
2912 | 57 static char line[LINE_LEN+1]; |
3235
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
58 static char *s = NULL, *slacktime_s; |
2912 | 59 char text[LINE_LEN+1], *p, *q; |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
60 int state; |
624 | 61 |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
62 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
|
63 state = 0; |
624 | 64 |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
65 /* read the first line */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
66 if (!s) |
2912 | 67 if (!(s = fgets(line, LINE_LEN, fd))) return 0; |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
68 |
624 | 69 do { |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
70 switch (state) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
71 |
3235
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
72 case 0: /* find "START=" or "Slacktime:" */ |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
73 slacktime_s = strstr (s, "Slacktime:"); |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
74 if (slacktime_s) sub_slacktime = strtol (slacktime_s + 10, NULL, 0) / 10; |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
75 |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
76 s = strstr (s, "Start="); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
77 if (s) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
78 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
|
79 state = 1; continue; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
80 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
81 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
82 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
83 case 1: /* find "<P" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
84 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
|
85 break; |
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 2: /* find ">" */ |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
88 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
|
89 break; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
90 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
91 case 3: /* get all text until '<' appears */ |
3235
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
92 if (*s == '\0') break; |
2836
ec672ea5ac2c
Applied SAMI patch by Evgeny Chukreev <codedj at echo dot ru>
atmos4
parents:
2495
diff
changeset
|
93 else if (!strncasecmp (s, "<br>", 4)) { |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
94 *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
|
95 if (text[0] != '\0') |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
96 current->text[current->lines++] = strdup (text); |
2836
ec672ea5ac2c
Applied SAMI patch by Evgeny Chukreev <codedj at echo dot ru>
atmos4
parents:
2495
diff
changeset
|
97 s += 4; |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
98 } |
2836
ec672ea5ac2c
Applied SAMI patch by Evgeny Chukreev <codedj at echo dot ru>
atmos4
parents:
2495
diff
changeset
|
99 else if (*s == '<') { state = 4; } |
ec672ea5ac2c
Applied SAMI patch by Evgeny Chukreev <codedj at echo dot ru>
atmos4
parents:
2495
diff
changeset
|
100 else if (!strncasecmp (s, " ", 6)) { *p++ = ' '; s += 6; } |
ec672ea5ac2c
Applied SAMI patch by Evgeny Chukreev <codedj at echo dot ru>
atmos4
parents:
2495
diff
changeset
|
101 else if (*s == '\t') { *p++ = ' '; s++; } |
ec672ea5ac2c
Applied SAMI patch by Evgeny Chukreev <codedj at echo dot ru>
atmos4
parents:
2495
diff
changeset
|
102 else if (*s == '\r' || *s == '\n') { s++; } |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
103 else *p++ = *s++; |
2836
ec672ea5ac2c
Applied SAMI patch by Evgeny Chukreev <codedj at echo dot ru>
atmos4
parents:
2495
diff
changeset
|
104 |
ec672ea5ac2c
Applied SAMI patch by Evgeny Chukreev <codedj at echo dot ru>
atmos4
parents:
2495
diff
changeset
|
105 /* skip duplicated space */ |
ec672ea5ac2c
Applied SAMI patch by Evgeny Chukreev <codedj at echo dot ru>
atmos4
parents:
2495
diff
changeset
|
106 if (p > text + 2) if (*(p-1) == ' ' && *(p-2) == ' ') p--; |
ec672ea5ac2c
Applied SAMI patch by Evgeny Chukreev <codedj at echo dot ru>
atmos4
parents:
2495
diff
changeset
|
107 |
624 | 108 continue; |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
109 |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
110 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
|
111 q = strstr (s, "Start="); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
112 if (q) { |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
113 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
|
114 *p = '\0'; trail_space (text); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
115 if (text[0] != '\0') |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
116 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
|
117 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
|
118 state = 0; continue; |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
119 } |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
120 s = strchr (s, '>'); |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
121 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
|
122 break; |
624 | 123 } |
124 | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
125 /* read next line */ |
3235
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
126 if (state != 99 && !(s = fgets (line, LINE_LEN, fd))) |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
127 if (current->start > 0) break; // if it is the last subtitle |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
128 else return 0; |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
129 |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
130 } while (state != 99); |
624 | 131 |
3235
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
132 // For the last subtitle |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
133 if (current->end <= 0) { |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
134 current->end = current->start + sub_slacktime; |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
135 *p = '\0'; trail_space (text); |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
136 if (text[0] != '\0') |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
137 current->text[current->lines++] = strdup (text); |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
138 } |
0cf593b6bab0
patch fixes the showing last line of subtitles and support of SAMI Slacktime option by Evgeny Chukreev <codedj@echo.ru>
arpi
parents:
2915
diff
changeset
|
139 |
624 | 140 return current; |
141 } | |
258 | 142 |
143 | |
144 char *sub_readtext(char *source, char **dest) { | |
145 int len=0; | |
932 | 146 char *p=source; |
258 | 147 |
932 | 148 while ( !eol(*p) && *p!= '|' ) { |
149 p++,len++; | |
150 } | |
258 | 151 |
152 *dest= (char *)malloc (len+1); | |
153 if (!dest) {return ERR;} | |
154 | |
155 strncpy(*dest, source, len); | |
156 (*dest)[len]=0; | |
157 | |
158 while (*p=='\r' || *p=='\n' || *p=='|') p++; | |
159 | |
160 if (*p) return p; // not-last text field | |
161 else return NULL; // last text field | |
162 } | |
163 | |
164 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) { | |
2912 | 165 char line[LINE_LEN+1]; |
166 char line2[LINE_LEN+1]; | |
258 | 167 char *p, *next; |
168 int i; | |
169 | |
3701 | 170 memset(current, 0, sizeof(subtitle)); |
258 | 171 |
172 do { | |
2912 | 173 if (!fgets (line, LINE_LEN, fd)) return NULL; |
932 | 174 } while (sscanf (line, "{%ld}{%ld}%[^\r\n]", &(current->start), &(current->end),line2) <3); |
258 | 175 |
932 | 176 p=line2; |
258 | 177 |
178 next=p, i=0; | |
1081 | 179 while ((next =sub_readtext (next, &(current->text[i])))) { |
270 | 180 if (current->text[i]==ERR) {return ERR;} |
258 | 181 i++; |
1081 | 182 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;} |
258 | 183 } |
932 | 184 current->lines= ++i; |
258 | 185 |
186 return current; | |
187 } | |
188 | |
189 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) { | |
2912 | 190 char line[LINE_LEN+1]; |
258 | 191 int a1,a2,a3,a4,b1,b2,b3,b4; |
192 char *p=NULL, *q=NULL; | |
193 int len; | |
194 | |
3701 | 195 memset(current, 0, sizeof(subtitle)); |
258 | 196 |
1764 | 197 while (1) { |
2912 | 198 if (!fgets (line, LINE_LEN, fd)) return NULL; |
269 | 199 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue; |
258 | 200 current->start = a1*360000+a2*6000+a3*100+a4; |
201 current->end = b1*360000+b2*6000+b3*100+b4; | |
202 | |
2912 | 203 if (!fgets (line, LINE_LEN, fd)) return NULL; |
258 | 204 |
205 p=q=line; | |
206 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) { | |
207 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && strncmp(p,"[br]",4); p++,len++); | |
208 current->text[current->lines-1]=(char *)malloc (len+1); | |
209 if (!current->text[current->lines-1]) return ERR; | |
210 strncpy (current->text[current->lines-1], q, len); | |
270 | 211 current->text[current->lines-1][len]='\0'; |
258 | 212 if (!*p || *p=='\r' || *p=='\n') break; |
213 while (*p++!=']'); | |
214 } | |
1764 | 215 break; |
258 | 216 } |
217 return current; | |
218 } | |
219 | |
2912 | 220 subtitle *sub_read_line_subviewer(FILE *fd,subtitle *current) { |
221 char line[LINE_LEN+1]; | |
258 | 222 int a1,a2,a3,a4,b1,b2,b3,b4; |
223 char *p=NULL; | |
224 int i,len; | |
225 | |
3701 | 226 memset(current, '\0', sizeof(subtitle)); |
258 | 227 |
228 while (!current->text[0]) { | |
2912 | 229 if (!fgets (line, LINE_LEN, fd)) return NULL; |
269 | 230 if ((len=sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8) |
258 | 231 continue; |
232 current->start = a1*360000+a2*6000+a3*100+a4/10; | |
233 current->end = b1*360000+b2*6000+b3*100+b4/10; | |
234 for (i=0; i<SUB_MAX_TEXT;) { | |
2912 | 235 if (!fgets (line, LINE_LEN, fd)) break; |
258 | 236 len=0; |
237 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++); | |
238 if (len) { | |
239 current->text[i]=(char *)malloc (len+1); | |
240 if (!current->text[i]) return ERR; | |
270 | 241 strncpy (current->text[i], line, len); current->text[i][len]='\0'; |
258 | 242 i++; |
243 } else { | |
244 break; | |
245 } | |
246 } | |
247 current->lines=i; | |
248 } | |
249 return current; | |
250 } | |
251 | |
818 | 252 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) { |
2912 | 253 char line[LINE_LEN+1]; |
254 char line2[LINE_LEN+1]; | |
818 | 255 int a1,a2,a3,b1,b2,b3; |
1081 | 256 char *p=NULL, *next; |
858 | 257 int i,len,len2,plen; |
818 | 258 |
3701 | 259 memset(current, '\0', sizeof(subtitle)); |
818 | 260 |
261 while (!current->text[0]) { | |
2912 | 262 if (!fgets (line, LINE_LEN, fd)) return NULL; |
858 | 263 if ((len=sscanf (line, "%d:%d:%d:%n",&a1,&a2,&a3,&plen)) < 3) |
818 | 264 continue; |
2912 | 265 if (!fgets (line2, LINE_LEN, fd)) return NULL; |
818 | 266 if ((len2=sscanf (line2, "%d:%d:%d:",&b1,&b2,&b3)) < 3) |
267 continue; | |
268 // przewiñ o linijkê do ty³u: | |
269 fseek(fd,-strlen(line2),SEEK_CUR); | |
270 | |
271 current->start = a1*360000+a2*6000+a3*100; | |
272 current->end = b1*360000+b2*6000+b3*100; | |
896
d46de26aef48
there is another format that get detected as vplayers.
eyck
parents:
892
diff
changeset
|
273 if ((current->end - current->start) > 1000) {current->end = current->start + 1000;} // not too long though. |
818 | 274 // teraz czas na wkopiowanie stringu |
1640
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
275 p=line; |
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
276 // finds the body of the subtitle |
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
277 for (i=0; i<3; i++){ |
3433 | 278 p=strchr(p,':'); |
279 if (p==NULL) break; | |
280 ++p; | |
281 } | |
282 if (p==NULL) { | |
283 printf("SUB: Skipping incorrect subtitle line!\n"); | |
284 continue; | |
285 } | |
1640
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
286 i=0; |
cbedcfab877b
Fixup to vplayer subtitle submitted to sourceforge by Igor Wojnicki
eyck
parents:
1501
diff
changeset
|
287 |
818 | 288 if (*p!='|') { |
289 // | |
290 next = p,i=0; | |
291 while ((next =sub_readtext (next, &(current->text[i])))) { | |
292 if (current->text[i]==ERR) {return ERR;} | |
293 i++; | |
1081 | 294 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;} |
818 | 295 } |
296 current->lines=i+1; | |
297 } | |
298 } | |
299 return current; | |
300 } | |
301 | |
850 | 302 subtitle *sub_read_line_rt(FILE *fd,subtitle *current) { |
303 //TODO: This format uses quite rich (sub/super)set of xhtml | |
304 // I couldn't check it since DTD is not included. | |
305 // WARNING: full XML parses can be required for proper parsing | |
2912 | 306 char line[LINE_LEN+1]; |
850 | 307 int a1,a2,a3,a4,b1,b2,b3,b4; |
308 char *p=NULL,*next=NULL; | |
309 int i,len,plen; | |
310 | |
3701 | 311 memset(current, '\0', sizeof(subtitle)); |
850 | 312 |
313 while (!current->text[0]) { | |
2912 | 314 if (!fgets (line, LINE_LEN, fd)) return NULL; |
850 | 315 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0 |
316 //to describe the same moment in time. Maybe there are even more formats in use. | |
317 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8) | |
318 plen=a1=a2=a3=a4=b1=b2=b3=b4=0; | |
319 if ( | |
320 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) && | |
321 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) && | |
322 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) && | |
323 ((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) && | |
324 ((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) | |
325 ) | |
326 continue; | |
327 current->start = a1*360000+a2*6000+a3*100+a4/10; | |
328 current->end = b1*360000+b2*6000+b3*100+b4/10; | |
329 p=line; p+=plen;i=0; | |
330 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml? | |
331 next = strstr(line,"<clear/>")+8;i=0; | |
332 while ((next =sub_readtext (next, &(current->text[i])))) { | |
333 if (current->text[i]==ERR) {return ERR;} | |
334 i++; | |
1081 | 335 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return current;} |
850 | 336 } |
337 current->lines=i+1; | |
338 } | |
339 return current; | |
340 } | |
341 | |
921 | 342 subtitle *sub_read_line_ssa(FILE *fd,subtitle *current) { |
343 int hour1, min1, sec1, hunsec1, | |
344 hour2, min2, sec2, hunsec2, nothing; | |
2141 | 345 int num; |
921 | 346 |
2912 | 347 char line[LINE_LEN+1], |
348 line3[LINE_LEN+1], | |
2140 | 349 *line2; |
2141 | 350 char *tmp; |
351 | |
921 | 352 do { |
2912 | 353 if (!fgets (line, LINE_LEN, fd)) return NULL; |
921 | 354 } while (sscanf (line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d," |
2140 | 355 "%[^\n\r]", ¬hing, |
356 &hour1, &min1, &sec1, &hunsec1, | |
357 &hour2, &min2, &sec2, &hunsec2, | |
358 line3) < 9); | |
359 line2=strstr(line3,",,"); | |
360 if (!line2) return NULL; | |
361 line2 ++; | |
362 line2 ++; | |
363 | |
2141 | 364 current->lines=1;num=0; |
921 | 365 current->start = 360000*hour1 + 6000*min1 + 100*sec1 + hunsec1; |
366 current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2; | |
2141 | 367 |
368 while (tmp=strstr(line2, "\\n")) { | |
369 current->text[num]=(char *)malloc(tmp-line2+1); | |
370 strncpy (current->text[num], line2, tmp-line2); | |
371 current->text[num][tmp-line2]='\0'; | |
372 line2=tmp+2; | |
373 num++; | |
374 current->lines++; | |
375 if (current->lines >= SUB_MAX_TEXT) return current; | |
376 } | |
377 | |
378 | |
3701 | 379 current->text[num]=strdup(line2); |
818 | 380 |
921 | 381 return current; |
382 } | |
258 | 383 |
1081 | 384 subtitle *sub_read_line_dunnowhat(FILE *fd,subtitle *current) { |
2912 | 385 char line[LINE_LEN+1]; |
386 char text[LINE_LEN+1]; | |
1081 | 387 |
3701 | 388 memset(current, '\0', sizeof(subtitle)); |
1081 | 389 |
2912 | 390 if (!fgets (line, LINE_LEN, fd)) |
1081 | 391 return NULL; |
392 if (sscanf (line, "%ld,%ld,\"%[^\"]", &(current->start), | |
393 &(current->end), text) <3) | |
394 return ERR; | |
395 current->text[0] = strdup(text); | |
396 current->lines = 1; | |
397 | |
398 return current; | |
399 } | |
400 | |
2177 | 401 subtitle *sub_read_line_mpsub(FILE *fd, subtitle *current) { |
2912 | 402 char line[LINE_LEN+1]; |
2178 | 403 float a,b; |
404 int num=0; | |
2177 | 405 char *p, *q; |
406 | |
407 do | |
408 { | |
2912 | 409 if (!fgets(line, LINE_LEN, fd)) return NULL; |
2178 | 410 } while (sscanf (line, "%f %f", &a, &b) !=2); |
2177 | 411 |
2178 | 412 mpsub_position += (a*100.0); |
413 current->start=(int) mpsub_position; | |
414 mpsub_position += (b*100.0); | |
415 current->end=(int) mpsub_position; | |
2177 | 416 |
417 while (num < SUB_MAX_TEXT) { | |
2912 | 418 if (!fgets (line, LINE_LEN, fd)) return NULL; |
2177 | 419 p=line; |
420 while (isspace(*p)) p++; | |
421 if (eol(*p) && num > 0) return current; | |
422 if (eol(*p)) return NULL; | |
423 | |
424 for (q=p; !eol(*q); q++); | |
425 *q='\0'; | |
426 if (strlen(p)) { | |
427 current->text[num]=strdup(p); | |
2495 | 428 printf (">%s<\n",p); |
2177 | 429 current->lines = ++num; |
430 } else { | |
431 if (num) return current; | |
432 else return NULL; | |
433 } | |
434 } | |
435 } | |
436 | |
2343 | 437 subtitle *previous_aqt_sub = NULL; |
438 | |
439 subtitle *sub_read_line_aqt(FILE *fd,subtitle *current) { | |
2912 | 440 char line[LINE_LEN+1]; |
2343 | 441 |
3701 | 442 memset(current, '\0', sizeof(subtitle)); |
2343 | 443 |
444 while (1) { | |
445 // try to locate next subtitle | |
2912 | 446 if (!fgets (line, LINE_LEN, fd)) |
2343 | 447 return NULL; |
448 if (!(sscanf (line, "-->> %ld", &(current->start)) <1)) | |
449 break; | |
450 } | |
451 | |
452 if (previous_aqt_sub != NULL) | |
453 previous_aqt_sub->end = current->start-1; | |
454 | |
455 previous_aqt_sub = current; | |
456 | |
2912 | 457 if (!fgets (line, LINE_LEN, fd)) |
2343 | 458 return NULL; |
459 | |
2468 | 460 sub_readtext((char *) &line,¤t->text[0]); |
2343 | 461 current->lines = 1; |
462 current->end = current->start; // will be corrected by next subtitle | |
463 | |
2912 | 464 if (!fgets (line, LINE_LEN, fd)) |
2343 | 465 return current;; |
466 | |
2468 | 467 sub_readtext((char *) &line,¤t->text[1]); |
2343 | 468 current->lines = 2; |
469 | |
470 if ((current->text[0]=="") && (current->text[1]=="")) { | |
471 // void subtitle -> end of previous marked and exit | |
472 previous_aqt_sub = NULL; | |
473 return NULL; | |
474 } | |
475 | |
476 return current; | |
477 } | |
2177 | 478 |
258 | 479 int sub_autodetect (FILE *fd) { |
2912 | 480 char line[LINE_LEN+1]; |
258 | 481 int i,j=0; |
2177 | 482 char p; |
258 | 483 |
624 | 484 while (j < 100) { |
258 | 485 j++; |
2912 | 486 if (!fgets (line, LINE_LEN, fd)) |
487 return SUB_INVALID; | |
258 | 488 |
624 | 489 if (sscanf (line, "{%d}{%d}", &i, &i)==2) |
2912 | 490 {sub_uses_time=0;return SUB_MICRODVD;} |
269 | 491 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) |
2912 | 492 {sub_uses_time=1;return SUB_SUBRIP;} |
269 | 493 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8) |
2912 | 494 {sub_uses_time=1;return SUB_SUBVIEWER;} |
624 | 495 if (strstr (line, "<SAMI>")) |
2912 | 496 {sub_uses_time=1; return SUB_SAMI;} |
818 | 497 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3) |
2912 | 498 {sub_uses_time=1;return SUB_VPLAYER;} |
850 | 499 //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
|
500 // too weak test for RT |
18c43d261c35
corrected strcmp() bug, now it works again with every subs (it was broken)
laaz
parents:
896
diff
changeset
|
501 // Please someone who knows the format of RT... FIX IT!!! |
921 | 502 // 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
|
503 if ( *line == '<' ) |
2912 | 504 {sub_uses_time=1;return SUB_RT;} |
921 | 505 |
506 if (!memcmp(line, "Dialogue: Marked", 16)) | |
2912 | 507 {sub_uses_time=1; return SUB_SSA;} |
1081 | 508 if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3) |
2912 | 509 {sub_uses_time=0;return SUB_DUNNOWHAT;} |
2177 | 510 if (sscanf (line, "FORMAT=%d", &i) == 1) |
2912 | 511 {sub_uses_time=0; return SUB_MPSUB;} |
2177 | 512 if (sscanf (line, "FORMAT=TIM%c", &p)==1 && p=='E') |
2912 | 513 {sub_uses_time=1; return SUB_MPSUB;} |
2343 | 514 if (strstr (line, "-->>")) |
2912 | 515 {sub_uses_time=0; return SUB_MPSUB;} |
258 | 516 } |
624 | 517 |
2912 | 518 return SUB_INVALID; // too many bad lines |
258 | 519 } |
2449
7ef89d9b06ed
added DUMPSUBS recognition if we need compile only subreader.c alone
atlka
parents:
2358
diff
changeset
|
520 |
7ef89d9b06ed
added DUMPSUBS recognition if we need compile only subreader.c alone
atlka
parents:
2358
diff
changeset
|
521 #ifdef DUMPSUBS |
7ef89d9b06ed
added DUMPSUBS recognition if we need compile only subreader.c alone
atlka
parents:
2358
diff
changeset
|
522 int sub_utf8=0; |
7ef89d9b06ed
added DUMPSUBS recognition if we need compile only subreader.c alone
atlka
parents:
2358
diff
changeset
|
523 #else |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
524 extern int sub_utf8; |
2449
7ef89d9b06ed
added DUMPSUBS recognition if we need compile only subreader.c alone
atlka
parents:
2358
diff
changeset
|
525 #endif |
258 | 526 |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
527 #ifdef USE_ICONV |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
528 static iconv_t icdsc; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
529 |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
530 void subcp_open (void) |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
531 { |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
532 char *tocp = "UTF-8"; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
533 icdsc = (iconv_t)(-1); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
534 if (sub_cp){ |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
535 if ((icdsc = iconv_open (tocp, sub_cp)) != (iconv_t)(-1)){ |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
536 printf ("SUB: opened iconv descriptor.\n"); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
537 sub_utf8 = 2; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
538 } else |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
539 printf ("SUB: error opening iconv descriptor.\n"); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
540 } |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
541 } |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
542 |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
543 void subcp_close (void) |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
544 { |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
545 if (icdsc != (iconv_t)(-1)){ |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
546 (void) iconv_close (icdsc); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
547 printf ("SUB: closed iconv descriptor.\n"); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
548 } |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
549 } |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
550 |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
551 #define ICBUFFSIZE 512 |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
552 static char icbuffer[ICBUFFSIZE]; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
553 |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
554 subtitle* subcp_recode (subtitle *sub) |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
555 { |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
556 int l=sub->lines; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
557 size_t ileft, oleft, otlen; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
558 char *op, *ip, *ot; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
559 |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
560 while (l){ |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
561 op = icbuffer; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
562 ip = sub->text[--l]; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
563 ileft = strlen(ip); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
564 oleft = ICBUFFSIZE - 1; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
565 |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
566 if (iconv(icdsc, (const char **) &ip, &ileft, |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
567 &op, &oleft) == (size_t)(-1)) { |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
568 printf ("SUB: error recoding line.\n"); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
569 l++; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
570 break; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
571 } |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
572 if (!(ot = (char *)malloc(op - icbuffer + 1))){ |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
573 printf ("SUB: error allocating mem.\n"); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
574 l++; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
575 break; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
576 } |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
577 *op='\0' ; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
578 strcpy (ot, icbuffer); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
579 free (sub->text[l]); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
580 sub->text[l] = ot; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
581 } |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
582 if (l){ |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
583 for (l = sub->lines; l;) |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
584 free (sub->text[--l]); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
585 return ERR; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
586 } |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
587 return sub; |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
588 } |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
589 |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
590 #endif |
258 | 591 |
592 subtitle* sub_read_file (char *filename) { | |
593 FILE *fd; | |
594 int n_max; | |
595 subtitle *first; | |
1081 | 596 subtitle * (*func[])(FILE *fd,subtitle *dest)= |
258 | 597 { |
598 sub_read_line_microdvd, | |
599 sub_read_line_subrip, | |
2912 | 600 sub_read_line_subviewer, |
818 | 601 sub_read_line_sami, |
850 | 602 sub_read_line_vplayer, |
921 | 603 sub_read_line_rt, |
1081 | 604 sub_read_line_ssa, |
2177 | 605 sub_read_line_dunnowhat, |
2343 | 606 sub_read_line_mpsub, |
607 sub_read_line_aqt | |
608 | |
258 | 609 }; |
2915 | 610 if(filename==NULL) return NULL; //qnx segfault |
258 | 611 fd=fopen (filename, "r"); if (!fd) return NULL; |
612 | |
613 sub_format=sub_autodetect (fd); | |
2912 | 614 if (sub_format==SUB_INVALID) {printf ("SUB: Could not determine file format\n");return NULL;} |
624 | 615 printf ("SUB: Detected subtitle file format: %d\n",sub_format); |
258 | 616 |
617 rewind (fd); | |
618 | |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
619 #ifdef USE_ICONV |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
620 subcp_open(); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
621 #endif |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
622 |
258 | 623 sub_num=0;n_max=32; |
624 first=(subtitle *)malloc(n_max*sizeof(subtitle)); | |
625 if(!first) return NULL; | |
626 | |
627 while(1){ | |
628 subtitle *sub; | |
629 if(sub_num>=n_max){ | |
630 n_max+=16; | |
631 first=realloc(first,n_max*sizeof(subtitle)); | |
632 } | |
633 sub=func[sub_format](fd,&first[sub_num]); | |
634 if(!sub) break; // EOF | |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
635 #ifdef USE_ICONV |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
636 if ((sub!=ERR) && (sub_utf8 & 2)) sub=subcp_recode(sub); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
637 #endif |
258 | 638 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid |
639 } | |
640 | |
641 fclose(fd); | |
642 | |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
643 #ifdef USE_ICONV |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
644 subcp_close(); |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
645 #endif |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
646 |
258 | 647 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); |
269 | 648 printf ("SUB: Read %i subtitles", sub_num); |
624 | 649 if (sub_errs) printf (", %i bad line(s).\n", sub_errs); |
269 | 650 else printf (".\n"); |
258 | 651 |
2880 | 652 if(sub_num<=0){ |
653 free(first); | |
654 return NULL; | |
655 } | |
656 | |
258 | 657 return first; |
658 } | |
659 | |
892 | 660 #if 0 |
509 | 661 char * strreplace( char * in,char * what,char * whereof ) |
662 { | |
663 int i; | |
664 char * tmp; | |
665 | |
666 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL; | |
667 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i]; | |
668 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0; | |
669 return in; | |
670 } | |
892 | 671 #endif |
509 | 672 |
892 | 673 char * sub_filename(char* path, char * fname ) |
509 | 674 { |
892 | 675 char * sub_name1; |
676 char * sub_name2; | |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
677 char * aviptr1, * aviptr2, * tmp; |
892 | 678 int i,j; |
679 FILE * f; | |
680 int pos=0; | |
681 char * sub_exts[] = | |
1501
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1081
diff
changeset
|
682 { ".utf", |
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1081
diff
changeset
|
683 ".UTF", |
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1081
diff
changeset
|
684 ".sub", |
509 | 685 ".SUB", |
686 ".srt", | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
687 ".SRT", |
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
688 ".smi", |
850 | 689 ".SMI", |
690 ".rt", | |
691 ".RT", | |
692 ".txt", | |
1081 | 693 ".TXT", |
694 ".ssa", | |
2343 | 695 ".SSA", |
696 ".aqt", | |
697 ".AQT"}; | |
892 | 698 |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
699 |
509 | 700 if ( fname == NULL ) return NULL; |
892 | 701 |
702 sub_name1=strrchr(fname,'.'); | |
703 if (!sub_name1) return NULL; | |
704 pos=sub_name1-fname; | |
705 | |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
706 sub_name1=malloc(strlen(fname)+8); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
707 strcpy(sub_name1,fname); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
708 |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
709 sub_name2=malloc (strlen(path) + strlen(fname) + 8); |
1081 | 710 if ((tmp=strrchr(fname,'/'))) |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
711 sprintf (sub_name2, "%s%s", path, tmp+1); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
712 else |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
713 sprintf (sub_name2, "%s%s", path, fname); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
714 |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
715 aviptr1=strrchr(sub_name1,'.'); |
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
716 aviptr2=strrchr(sub_name2,'.'); |
892 | 717 |
718 for(j=0;j<=1;j++){ | |
719 char* sub_name=j?sub_name1:sub_name2; | |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
720 #ifdef USE_ICONV |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
721 for ( i=(sub_cp?2:0);i<(sizeof(sub_exts)/sizeof(char*));i++ ) { |
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
722 #else |
892 | 723 for ( i=0;i<(sizeof(sub_exts)/sizeof(char*));i++ ) { |
2151
a9d91476085a
modifications to use iconv(3) function to recode text of subs (autodetect)
atlka
parents:
2141
diff
changeset
|
724 #endif |
934
b2c7c4b49948
Gabucino (CGA user)'s request (finds default.subs well)
laaz
parents:
932
diff
changeset
|
725 strcpy(j?aviptr1:aviptr2,sub_exts[i]); |
935 | 726 // printf("trying: '%s'\n",sub_name); |
892 | 727 if((f=fopen( sub_name,"rt" ))) { |
509 | 728 fclose( f ); |
729 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
|
730 if (i<2) sub_utf8=1; |
509 | 731 return sub_name; |
892 | 732 } |
509 | 733 } |
892 | 734 } |
735 | |
509 | 736 return NULL; |
737 } | |
738 | |
1761 | 739 void list_sub_file(subtitle* subs){ |
740 int i,j; | |
741 | |
742 for(j=0;j<sub_num;j++){ | |
743 subtitle* egysub=&subs[j]; | |
744 printf ("%i line%c (%li-%li) ", | |
745 egysub->lines, | |
746 (1==egysub->lines)?' ':'s', | |
747 egysub->start, | |
748 egysub->end); | |
749 for (i=0; i<egysub->lines; i++) { | |
750 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> "); | |
751 } | |
752 printf ("\n"); | |
753 } | |
754 | |
755 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use"); | |
756 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs); | |
757 | |
758 } | |
759 | |
2178 | 760 void dump_mpsub(subtitle* subs){ |
761 int i,j; | |
762 FILE *fd; | |
763 float a,b; | |
764 | |
765 mpsub_position=0.0; | |
766 | |
767 fd=fopen ("dump.mpsub", "w"); | |
768 if (!fd) { | |
769 perror ("dump_mpsub: fopen"); | |
770 return; | |
771 } | |
772 | |
773 | |
774 if (sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n"); | |
2495 | 775 else fprintf (fd, "FORMAT=25\n\n"); // FIXME: fps |
2178 | 776 |
777 for(j=0;j<sub_num;j++){ | |
2495 | 778 subtitle* egysub=&subs[j]; |
779 if (sub_uses_time) { | |
780 a=((egysub->start-mpsub_position)/100.0); | |
781 b=((egysub->end-egysub->start)/100.0); | |
782 if ( (float)((int)a) == a) | |
783 fprintf (fd, "%.0f",a); | |
784 else | |
785 fprintf (fd, "%.2f",a); | |
786 | |
787 if ( (float)((int)b) == b) | |
788 fprintf (fd, " %.0f\n",b); | |
789 else | |
790 fprintf (fd, " %.2f\n",b); | |
791 } else { | |
792 fprintf (fd, "%ld %ld\n", egysub->start-mpsub_position, | |
793 egysub->end-egysub->start); | |
794 } | |
795 | |
796 mpsub_position = egysub->end; | |
797 for (i=0; i<egysub->lines; i++) { | |
798 fprintf (fd, "%s\n",egysub->text[i]); | |
799 } | |
800 fprintf (fd, "\n"); | |
2178 | 801 } |
802 fclose (fd); | |
803 printf ("Subtitles dumped in \'dump.mpsub\'.\n"); | |
804 } | |
805 | |
3543 | 806 void sub_free( subtitle * subs ) |
807 { | |
808 int i; | |
809 | |
810 if ( !subs ) return; | |
811 | |
812 sub_num=0; | |
813 sub_errs=0; | |
814 for ( i=0;i<subs->lines;i++ ) free( subs->text[i] ); | |
815 free( subs ); | |
816 subs=NULL; | |
817 } | |
2178 | 818 |
2449
7ef89d9b06ed
added DUMPSUBS recognition if we need compile only subreader.c alone
atlka
parents:
2358
diff
changeset
|
819 #ifdef DUMPSUBS |
258 | 820 int main(int argc, char **argv) { // for testing |
821 | |
822 int i,j; | |
823 subtitle *subs; | |
824 subtitle *egysub; | |
825 | |
826 if(argc<2){ | |
827 printf("\nUsage: subreader filename.sub\n\n"); | |
828 exit(1); | |
829 } | |
2449
7ef89d9b06ed
added DUMPSUBS recognition if we need compile only subreader.c alone
atlka
parents:
2358
diff
changeset
|
830 sub_cp = argv[2]; |
624 | 831 subs=sub_read_file(argv[1]); |
258 | 832 if(!subs){ |
833 printf("Couldn't load file... let's write a bugreport :)\n"); | |
834 exit(1); | |
835 } | |
1761 | 836 |
837 list_sub_file(subs); | |
258 | 838 |
839 return 0; | |
840 } | |
706
8a7666a78f83
better .smi support and display two-byte characters- patch by Sunjin Yang
arpi_esp
parents:
678
diff
changeset
|
841 #endif |