obvious gcc warning fix, approved by Nico
[mplayer/glamo.git] / subreader.c
blobab1323eba3871f76b97d2adfa2c6ac3d08e8744e
1 /*
2 * Subtitle reader with format autodetection
4 * Written by laaz
5 * Some code cleanup & realloc() by A'rpi/ESP-team
7 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
15 #include <sys/types.h>
16 #include <dirent.h>
18 #include "config.h"
19 #include "mp_msg.h"
20 #include "subreader.h"
22 #ifdef HAVE_ENCA
23 #include <enca.h>
24 #endif
26 #define ERR ((void *) -1)
28 #ifdef USE_ICONV
29 #include <iconv.h>
30 char *sub_cp=NULL;
31 #endif
32 #ifdef USE_FRIBIDI
33 #include <fribidi/fribidi.h>
34 char *fribidi_charset = NULL; ///character set that will be passed to FriBiDi
35 int flip_hebrew = 1; ///flip subtitles using fribidi
36 int fribidi_flip_commas = 0; ///flip comma when fribidi is used
37 #endif
39 extern char* dvdsub_lang;
41 /* Maximal length of line of a subtitle */
42 #define LINE_LEN 1000
43 static float mpsub_position=0;
44 static float mpsub_multiplier=1.;
45 static int sub_slacktime = 20000; //20 sec
47 int sub_no_text_pp=0; // 1 => do not apply text post-processing
48 // like {\...} elimination in SSA format.
50 int sub_match_fuzziness=0; // level of sub name matching fuzziness
52 /* Use the SUB_* constant defined in the header file */
53 int sub_format=SUB_INVALID;
54 #ifdef USE_SORTSUB
55 /*
56 Some subtitling formats, namely AQT and Subrip09, define the end of a
57 subtitle as the beginning of the following. Since currently we read one
58 subtitle at time, for these format we keep two global *subtitle,
59 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
60 so we can change its end when we read current subtitle starting time.
61 When USE_SORTSUB is defined, we use a single global unsigned long,
62 previous_sub_end, for both (and even future) formats, to store the end of
63 the previous sub: it is initialized to 0 in sub_read_file and eventually
64 modified by sub_read_aqt_line or sub_read_subrip09_line.
66 unsigned long previous_sub_end;
67 #endif
69 static int eol(char p) {
70 return (p=='\r' || p=='\n' || p=='\0');
73 /* Remove leading and trailing space */
74 static void trail_space(char *s) {
75 int i = 0;
76 while (isspace(s[i])) ++i;
77 if (i) strcpy(s, s + i);
78 i = strlen(s) - 1;
79 while (i > 0 && isspace(s[i])) s[i--] = '\0';
82 static char *stristr(const char *haystack, const char *needle) {
83 int len = 0;
84 const char *p = haystack;
86 if (!(haystack && needle)) return NULL;
88 len=strlen(needle);
89 while (*p != '\0') {
90 if (strncasecmp(p, needle, len) == 0) return (char*)p;
91 p++;
94 return NULL;
97 subtitle *sub_read_line_sami(FILE *fd, subtitle *current) {
98 static char line[LINE_LEN+1];
99 static char *s = NULL, *slacktime_s;
100 char text[LINE_LEN+1], *p=NULL, *q;
101 int state;
103 current->lines = current->start = current->end = 0;
104 current->alignment = SUB_ALIGNMENT_BOTTOMCENTER;
105 state = 0;
107 /* read the first line */
108 if (!s)
109 if (!(s = fgets(line, LINE_LEN, fd))) return 0;
111 do {
112 switch (state) {
114 case 0: /* find "START=" or "Slacktime:" */
115 slacktime_s = stristr (s, "Slacktime:");
116 if (slacktime_s)
117 sub_slacktime = strtol (slacktime_s+10, NULL, 0) / 10;
119 s = stristr (s, "Start=");
120 if (s) {
121 current->start = strtol (s + 6, &s, 0) / 10;
122 /* eat '>' */
123 for (; *s != '>' && *s != '\0'; s++);
124 s++;
125 state = 1; continue;
127 break;
129 case 1: /* find (optionnal) "<P", skip other TAGs */
130 for (; *s == ' ' || *s == '\t'; s++); /* strip blanks, if any */
131 if (*s == '\0') break;
132 if (*s != '<') { state = 3; p = text; continue; } /* not a TAG */
133 s++;
134 if (*s == 'P' || *s == 'p') { s++; state = 2; continue; } /* found '<P' */
135 for (; *s != '>' && *s != '\0'; s++); /* skip remains of non-<P> TAG */
136 if (s == '\0')
137 break;
138 s++;
139 continue;
141 case 2: /* find ">" */
142 if ((s = strchr (s, '>'))) { s++; state = 3; p = text; continue; }
143 break;
145 case 3: /* get all text until '<' appears */
146 if (*s == '\0') break;
147 else if (!strncasecmp (s, "<br>", 4)) {
148 *p = '\0'; p = text; trail_space (text);
149 if (text[0] != '\0')
150 current->text[current->lines++] = strdup (text);
151 s += 4;
153 else if ((*s == '{') && !sub_no_text_pp) { state = 5; ++s; continue; }
154 else if (*s == '<') { state = 4; }
155 else if (!strncasecmp (s, "&nbsp;", 6)) { *p++ = ' '; s += 6; }
156 else if (*s == '\t') { *p++ = ' '; s++; }
157 else if (*s == '\r' || *s == '\n') { s++; }
158 else *p++ = *s++;
160 /* skip duplicated space */
161 if (p > text + 2) if (*(p-1) == ' ' && *(p-2) == ' ') p--;
163 continue;
165 case 4: /* get current->end or skip <TAG> */
166 q = stristr (s, "Start=");
167 if (q) {
168 current->end = strtol (q + 6, &q, 0) / 10 - 1;
169 *p = '\0'; trail_space (text);
170 if (text[0] != '\0')
171 current->text[current->lines++] = strdup (text);
172 if (current->lines > 0) { state = 99; break; }
173 state = 0; continue;
175 s = strchr (s, '>');
176 if (s) { s++; state = 3; continue; }
177 break;
178 case 5: /* get rid of {...} text, but read the alignment code */
179 if ((*s == '\\') && (*(s + 1) == 'a') && !sub_no_text_pp) {
180 if (stristr(s, "\\a1") != NULL) {
181 current->alignment = SUB_ALIGNMENT_BOTTOMLEFT;
182 s = s + 3;
184 if (stristr(s, "\\a2") != NULL) {
185 current->alignment = SUB_ALIGNMENT_BOTTOMCENTER;
186 s = s + 3;
187 } else if (stristr(s, "\\a3") != NULL) {
188 current->alignment = SUB_ALIGNMENT_BOTTOMRIGHT;
189 s = s + 3;
190 } else if ((stristr(s, "\\a4") != NULL) || (stristr(s, "\\a5") != NULL) || (stristr(s, "\\a8") != NULL)) {
191 current->alignment = SUB_ALIGNMENT_TOPLEFT;
192 s = s + 3;
193 } else if (stristr(s, "\\a6") != NULL) {
194 current->alignment = SUB_ALIGNMENT_TOPCENTER;
195 s = s + 3;
196 } else if (stristr(s, "\\a7") != NULL) {
197 current->alignment = SUB_ALIGNMENT_TOPRIGHT;
198 s = s + 3;
199 } else if (stristr(s, "\\a9") != NULL) {
200 current->alignment = SUB_ALIGNMENT_MIDDLELEFT;
201 s = s + 3;
202 } else if (stristr(s, "\\a10") != NULL) {
203 current->alignment = SUB_ALIGNMENT_MIDDLECENTER;
204 s = s + 4;
205 } else if (stristr(s, "\\a11") != NULL) {
206 current->alignment = SUB_ALIGNMENT_MIDDLERIGHT;
207 s = s + 4;
210 if (*s == '}') state = 3;
211 ++s;
212 continue;
215 /* read next line */
216 if (state != 99 && !(s = fgets (line, LINE_LEN, fd))) {
217 if (current->start > 0) {
218 break; // if it is the last subtitle
219 } else {
220 return 0;
224 } while (state != 99);
226 // For the last subtitle
227 if (current->end <= 0) {
228 current->end = current->start + sub_slacktime;
229 *p = '\0'; trail_space (text);
230 if (text[0] != '\0')
231 current->text[current->lines++] = strdup (text);
234 return current;
238 char *sub_readtext(char *source, char **dest) {
239 int len=0;
240 char *p=source;
242 // printf("src=%p dest=%p \n",source,dest);
244 while ( !eol(*p) && *p!= '|' ) {
245 p++,len++;
248 *dest= (char *)malloc (len+1);
249 if (!dest) {return ERR;}
251 strncpy(*dest, source, len);
252 (*dest)[len]=0;
254 while (*p=='\r' || *p=='\n' || *p=='|') p++;
256 if (*p) return p; // not-last text field
257 else return NULL; // last text field
260 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) {
261 char line[LINE_LEN+1];
262 char line2[LINE_LEN+1];
263 char *p, *next;
264 int i;
266 do {
267 if (!fgets (line, LINE_LEN, fd)) return NULL;
268 } while ((sscanf (line,
269 "{%ld}{}%[^\r\n]",
270 &(current->start), line2) < 2) &&
271 (sscanf (line,
272 "{%ld}{%ld}%[^\r\n]",
273 &(current->start), &(current->end), line2) < 3));
275 p=line2;
277 next=p, i=0;
278 while ((next =sub_readtext (next, &(current->text[i])))) {
279 if (current->text[i]==ERR) {return ERR;}
280 i++;
281 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
283 current->lines= ++i;
285 return current;
288 subtitle *sub_read_line_mpl2(FILE *fd,subtitle *current) {
289 char line[LINE_LEN+1];
290 char line2[LINE_LEN+1];
291 char *p, *next;
292 int i;
294 do {
295 if (!fgets (line, LINE_LEN, fd)) return NULL;
296 } while ((sscanf (line,
297 "[%ld][%ld]%[^\r\n]",
298 &(current->start), &(current->end), line2) < 3));
299 current->start *= 10;
300 current->end *= 10;
301 p=line2;
303 next=p, i=0;
304 while ((next =sub_readtext (next, &(current->text[i])))) {
305 if (current->text[i]==ERR) {return ERR;}
306 i++;
307 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
309 current->lines= ++i;
311 return current;
314 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) {
315 char line[LINE_LEN+1];
316 int a1,a2,a3,a4,b1,b2,b3,b4;
317 char *p=NULL, *q=NULL;
318 int len;
320 while (1) {
321 if (!fgets (line, LINE_LEN, fd)) return NULL;
322 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
323 current->start = a1*360000+a2*6000+a3*100+a4;
324 current->end = b1*360000+b2*6000+b3*100+b4;
326 if (!fgets (line, LINE_LEN, fd)) return NULL;
328 p=q=line;
329 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) {
330 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && *p!='|' && strncmp(p,"[br]",4); p++,len++);
331 current->text[current->lines-1]=(char *)malloc (len+1);
332 if (!current->text[current->lines-1]) return ERR;
333 strncpy (current->text[current->lines-1], q, len);
334 current->text[current->lines-1][len]='\0';
335 if (!*p || *p=='\r' || *p=='\n') break;
336 if (*p=='|') p++;
337 else while (*p++!=']');
339 break;
341 return current;
344 subtitle *sub_read_line_subviewer(FILE *fd,subtitle *current) {
345 char line[LINE_LEN+1];
346 int a1,a2,a3,a4,b1,b2,b3,b4;
347 char *p=NULL;
348 int i,len;
350 while (!current->text[0]) {
351 if (!fgets (line, LINE_LEN, fd)) return NULL;
352 if ((len=sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d",&a1,&a2,&a3,(char *)&i,&a4,&b1,&b2,&b3,(char *)&i,&b4)) < 10)
353 continue;
354 current->start = a1*360000+a2*6000+a3*100+a4/10;
355 current->end = b1*360000+b2*6000+b3*100+b4/10;
356 for (i=0; i<SUB_MAX_TEXT;) {
357 if (!fgets (line, LINE_LEN, fd)) break;
358 len=0;
359 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
360 if (len) {
361 int j=0,skip=0;
362 char *curptr=current->text[i]=(char *)malloc (len+1);
363 if (!current->text[i]) return ERR;
364 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
365 for(; j<len; j++) {
366 /* let's filter html tags ::atmos */
367 if(line[j]=='>') {
368 skip=0;
369 continue;
371 if(line[j]=='<') {
372 skip=1;
373 continue;
375 if(skip) {
376 continue;
378 *curptr=line[j];
379 curptr++;
381 *curptr='\0';
383 i++;
384 } else {
385 break;
388 current->lines=i;
390 return current;
393 subtitle *sub_read_line_subviewer2(FILE *fd,subtitle *current) {
394 char line[LINE_LEN+1];
395 int a1,a2,a3,a4;
396 char *p=NULL;
397 int i,len;
399 while (!current->text[0]) {
400 if (!fgets (line, LINE_LEN, fd)) return NULL;
401 if (line[0]!='{')
402 continue;
403 if ((len=sscanf (line, "{T %d:%d:%d:%d",&a1,&a2,&a3,&a4)) < 4)
404 continue;
405 current->start = a1*360000+a2*6000+a3*100+a4/10;
406 for (i=0; i<SUB_MAX_TEXT;) {
407 if (!fgets (line, LINE_LEN, fd)) break;
408 if (line[0]=='}') break;
409 len=0;
410 for (p=line; *p!='\n' && *p!='\r' && *p; ++p,++len);
411 if (len) {
412 current->text[i]=(char *)malloc (len+1);
413 if (!current->text[i]) return ERR;
414 strncpy (current->text[i], line, len); current->text[i][len]='\0';
415 ++i;
416 } else {
417 break;
420 current->lines=i;
422 return current;
426 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) {
427 char line[LINE_LEN+1];
428 int a1,a2,a3;
429 char *p=NULL, *next,separator;
430 int i,len,plen;
432 while (!current->text[0]) {
433 if (!fgets (line, LINE_LEN, fd)) return NULL;
434 if ((len=sscanf (line, "%d:%d:%d%c%n",&a1,&a2,&a3,&separator,&plen)) < 4)
435 continue;
437 if (!(current->start = a1*360000+a2*6000+a3*100))
438 continue;
439 /* removed by wodzu
440 p=line;
441 // finds the body of the subtitle
442 for (i=0; i<3; i++){
443 p=strchr(p,':');
444 if (p==NULL) break;
445 ++p;
447 if (p==NULL) {
448 printf("SUB: Skipping incorrect subtitle line!\n");
449 continue;
452 // by wodzu: hey! this time we know what length it has! what is
453 // that magic for? it can't deal with space instead of third
454 // colon! look, what simple it can be:
455 p = &line[ plen ];
457 i=0;
458 if (*p!='|') {
460 next = p,i=0;
461 while ((next =sub_readtext (next, &(current->text[i])))) {
462 if (current->text[i]==ERR) {return ERR;}
463 i++;
464 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
466 current->lines=i+1;
469 return current;
472 subtitle *sub_read_line_rt(FILE *fd,subtitle *current) {
473 //TODO: This format uses quite rich (sub/super)set of xhtml
474 // I couldn't check it since DTD is not included.
475 // WARNING: full XML parses can be required for proper parsing
476 char line[LINE_LEN+1];
477 int a1,a2,a3,a4,b1,b2,b3,b4;
478 char *p=NULL,*next=NULL;
479 int i,len,plen;
481 while (!current->text[0]) {
482 if (!fgets (line, LINE_LEN, fd)) return NULL;
483 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
484 //to describe the same moment in time. Maybe there are even more formats in use.
485 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
486 plen=a1=a2=a3=a4=b1=b2=b3=b4=0;
487 if (
488 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b3,&b4,&plen)) < 4) &&
489 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b2,&b3,&b4,&plen)) < 5) &&
490 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) &&
491 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) &&
492 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
493 ((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) &&
494 ((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) &&
495 //now try it without end time
496 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&plen)) < 2) &&
497 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&plen)) < 2) &&
498 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&plen)) < 3) &&
499 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&plen)) < 4)
501 continue;
502 current->start = a1*360000+a2*6000+a3*100+a4/10;
503 current->end = b1*360000+b2*6000+b3*100+b4/10;
504 if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)
505 current->end = current->start+200;
506 p=line; p+=plen;i=0;
507 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
508 next = strstr(line,"<clear/>");
509 if(next && strlen(next)>8){
510 next+=8;i=0;
511 while ((next =sub_readtext (next, &(current->text[i])))) {
512 if (current->text[i]==ERR) {return ERR;}
513 i++;
514 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
517 current->lines=i+1;
519 return current;
522 subtitle *sub_read_line_ssa(FILE *fd,subtitle *current) {
524 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
525 * other Sub Station Alpha scripts have only 8 commas before subtitle
526 * Reading the "ScriptType:" field is not reliable since many scripts appear
527 * w/o it
529 * http://www.scriptclub.org is a good place to find more examples
530 * http://www.eswat.demon.co.uk is where the SSA specs can be found
532 int comma;
533 static int max_comma = 32; /* let's use 32 for the case that the */
534 /* amount of commas increase with newer SSA versions */
536 int hour1, min1, sec1, hunsec1,
537 hour2, min2, sec2, hunsec2, nothing;
538 int num;
540 char line[LINE_LEN+1],
541 line3[LINE_LEN+1],
542 *line2;
543 char *tmp;
545 do {
546 if (!fgets (line, LINE_LEN, fd)) return NULL;
547 } while (sscanf (line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
548 "%[^\n\r]", &nothing,
549 &hour1, &min1, &sec1, &hunsec1,
550 &hour2, &min2, &sec2, &hunsec2,
551 line3) < 9
553 sscanf (line, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
554 "%[^\n\r]", &nothing,
555 &hour1, &min1, &sec1, &hunsec1,
556 &hour2, &min2, &sec2, &hunsec2,
557 line3) < 9 );
559 line2=strchr(line3, ',');
561 for (comma = 4; comma < max_comma; comma ++)
563 tmp = line2;
564 if(!(tmp=strchr(++tmp, ','))) break;
565 if(*(++tmp) == ' ') break;
566 /* a space after a comma means we're already in a sentence */
567 line2 = tmp;
570 if(comma < max_comma)max_comma = comma;
571 /* eliminate the trailing comma */
572 if(*line2 == ',') line2++;
574 current->lines=0;num=0;
575 current->start = 360000*hour1 + 6000*min1 + 100*sec1 + hunsec1;
576 current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2;
578 while (((tmp=strstr(line2, "\\n")) != NULL) || ((tmp=strstr(line2, "\\N")) != NULL) ){
579 current->text[num]=(char *)malloc(tmp-line2+1);
580 strncpy (current->text[num], line2, tmp-line2);
581 current->text[num][tmp-line2]='\0';
582 line2=tmp+2;
583 num++;
584 current->lines++;
585 if (current->lines >= SUB_MAX_TEXT) return current;
588 current->text[num]=strdup(line2);
589 current->lines++;
591 return current;
594 void sub_pp_ssa(subtitle *sub) {
595 int l=sub->lines;
596 char *so,*de,*start;
598 while (l){
599 /* eliminate any text enclosed with {}, they are font and color settings */
600 so=de=sub->text[--l];
601 while (*so) {
602 if(*so == '{' && so[1]=='\\') {
603 for (start=so; *so && *so!='}'; so++);
604 if(*so) so++; else so=start;
606 if(*so) {
607 *de=*so;
608 so++; de++;
611 *de=*so;
616 * PJS subtitles reader.
617 * That's the "Phoenix Japanimation Society" format.
618 * I found some of them in http://www.scriptsclub.org/ (used for anime).
619 * The time is in tenths of second.
621 * by set, based on code by szabi (dunnowhat sub format ;-)
623 subtitle *sub_read_line_pjs(FILE *fd,subtitle *current) {
624 char line[LINE_LEN+1];
625 char text[LINE_LEN+1], *s, *d;
627 if (!fgets (line, LINE_LEN, fd))
628 return NULL;
629 /* skip spaces */
630 for (s=line; *s && isspace(*s); s++);
631 /* allow empty lines at the end of the file */
632 if (*s==0)
633 return NULL;
634 /* get the time */
635 if (sscanf (s, "%ld,%ld,", &(current->start),
636 &(current->end)) <2) {
637 return ERR;
639 /* the files I have are in tenths of second */
640 current->start *= 10;
641 current->end *= 10;
642 /* walk to the beggining of the string */
643 for (; *s; s++) if (*s==',') break;
644 if (*s) {
645 for (s++; *s; s++) if (*s==',') break;
646 if (*s) s++;
648 if (*s!='"') {
649 return ERR;
651 /* copy the string to the text buffer */
652 for (s++, d=text; *s && *s!='"'; s++, d++)
653 *d=*s;
654 *d=0;
655 current->text[0] = strdup(text);
656 current->lines = 1;
658 return current;
661 subtitle *sub_read_line_mpsub(FILE *fd, subtitle *current) {
662 char line[LINE_LEN+1];
663 float a,b;
664 int num=0;
665 char *p, *q;
669 if (!fgets(line, LINE_LEN, fd)) return NULL;
670 } while (sscanf (line, "%f %f", &a, &b) !=2);
672 mpsub_position += a*mpsub_multiplier;
673 current->start=(int) mpsub_position;
674 mpsub_position += b*mpsub_multiplier;
675 current->end=(int) mpsub_position;
677 while (num < SUB_MAX_TEXT) {
678 if (!fgets (line, LINE_LEN, fd)) {
679 if (num == 0) return NULL;
680 else return current;
682 p=line;
683 while (isspace(*p)) p++;
684 if (eol(*p) && num > 0) return current;
685 if (eol(*p)) return NULL;
687 for (q=p; !eol(*q); q++);
688 *q='\0';
689 if (strlen(p)) {
690 current->text[num]=strdup(p);
691 // printf (">%s<\n",p);
692 current->lines = ++num;
693 } else {
694 if (num) return current;
695 else return NULL;
698 return NULL; // we should have returned before if it's OK
701 #ifndef USE_SORTSUB
702 //we don't need this if we use previous_sub_end
703 subtitle *previous_aqt_sub = NULL;
704 #endif
706 subtitle *sub_read_line_aqt(FILE *fd,subtitle *current) {
707 char line[LINE_LEN+1];
708 char *next;
709 int i;
711 while (1) {
712 // try to locate next subtitle
713 if (!fgets (line, LINE_LEN, fd))
714 return NULL;
715 if (!(sscanf (line, "-->> %ld", &(current->start)) <1))
716 break;
719 #ifdef USE_SORTSUB
720 previous_sub_end = (current->start) ? current->start - 1 : 0;
721 #else
722 if (previous_aqt_sub != NULL)
723 previous_aqt_sub->end = current->start-1;
725 previous_aqt_sub = current;
726 #endif
728 if (!fgets (line, LINE_LEN, fd))
729 return NULL;
731 sub_readtext((char *) &line,&current->text[0]);
732 current->lines = 1;
733 current->end = current->start; // will be corrected by next subtitle
735 if (!fgets (line, LINE_LEN, fd))
736 return current;
738 next = line,i=1;
739 while ((next =sub_readtext (next, &(current->text[i])))) {
740 if (current->text[i]==ERR) {return ERR;}
741 i++;
742 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
744 current->lines=i+1;
746 if ((current->text[0]=="") && (current->text[1]=="")) {
747 #ifdef USE_SORTSUB
748 previous_sub_end = 0;
749 #else
750 // void subtitle -> end of previous marked and exit
751 previous_aqt_sub = NULL;
752 #endif
753 return NULL;
756 return current;
759 #ifndef USE_SORTSUB
760 subtitle *previous_subrip09_sub = NULL;
761 #endif
763 subtitle *sub_read_line_subrip09(FILE *fd,subtitle *current) {
764 char line[LINE_LEN+1];
765 int a1,a2,a3;
766 char * next=NULL;
767 int i,len;
769 while (1) {
770 // try to locate next subtitle
771 if (!fgets (line, LINE_LEN, fd))
772 return NULL;
773 if (!((len=sscanf (line, "[%d:%d:%d]",&a1,&a2,&a3)) < 3))
774 break;
777 current->start = a1*360000+a2*6000+a3*100;
779 #ifdef USE_SORTSUB
780 previous_sub_end = (current->start) ? current->start - 1 : 0;
781 #else
782 if (previous_subrip09_sub != NULL)
783 previous_subrip09_sub->end = current->start-1;
785 previous_subrip09_sub = current;
786 #endif
788 if (!fgets (line, LINE_LEN, fd))
789 return NULL;
791 next = line,i=0;
793 current->text[0]=""; // just to be sure that string is clear
795 while ((next =sub_readtext (next, &(current->text[i])))) {
796 if (current->text[i]==ERR) {return ERR;}
797 i++;
798 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
800 current->lines=i+1;
802 if ((current->text[0]=="") && (i==0)) {
803 #ifdef USE_SORTSUB
804 previous_sub_end = 0;
805 #else
806 // void subtitle -> end of previous marked and exit
807 previous_subrip09_sub = NULL;
808 #endif
809 return NULL;
812 return current;
815 subtitle *sub_read_line_jacosub(FILE * fd, subtitle * current)
817 char line1[LINE_LEN], line2[LINE_LEN], directive[LINE_LEN], *p, *q;
818 unsigned a1, a2, a3, a4, b1, b2, b3, b4, comment = 0;
819 static unsigned jacoTimeres = 30;
820 static int jacoShift = 0;
822 memset(current, 0, sizeof(subtitle));
823 memset(line1, 0, LINE_LEN);
824 memset(line2, 0, LINE_LEN);
825 memset(directive, 0, LINE_LEN);
826 while (!current->text[0]) {
827 if (!fgets(line1, LINE_LEN, fd)) {
828 return NULL;
830 if (sscanf
831 (line1, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1, &a2, &a3, &a4,
832 &b1, &b2, &b3, &b4, line2) < 9) {
833 if (sscanf(line1, "@%u @%u %[^\n\r]", &a4, &b4, line2) < 3) {
834 if (line1[0] == '#') {
835 int hours = 0, minutes = 0, seconds, delta, inverter =
837 unsigned units = jacoShift;
838 switch (toupper(line1[1])) {
839 case 'S':
840 if (isalpha(line1[2])) {
841 delta = 6;
842 } else {
843 delta = 2;
845 if (sscanf(&line1[delta], "%d", &hours)) {
846 if (hours < 0) {
847 hours *= -1;
848 inverter = -1;
850 if (sscanf(&line1[delta], "%*d:%d", &minutes)) {
851 if (sscanf
852 (&line1[delta], "%*d:%*d:%d",
853 &seconds)) {
854 sscanf(&line1[delta], "%*d:%*d:%*d.%d",
855 &units);
856 } else {
857 hours = 0;
858 sscanf(&line1[delta], "%d:%d.%d",
859 &minutes, &seconds, &units);
860 minutes *= inverter;
862 } else {
863 hours = minutes = 0;
864 sscanf(&line1[delta], "%d.%d", &seconds,
865 &units);
866 seconds *= inverter;
868 jacoShift =
869 ((hours * 3600 + minutes * 60 +
870 seconds) * jacoTimeres +
871 units) * inverter;
873 break;
874 case 'T':
875 if (isalpha(line1[2])) {
876 delta = 8;
877 } else {
878 delta = 2;
880 sscanf(&line1[delta], "%u", &jacoTimeres);
881 break;
884 continue;
885 } else {
886 current->start =
887 (unsigned long) ((a4 + jacoShift) * 100.0 /
888 jacoTimeres);
889 current->end =
890 (unsigned long) ((b4 + jacoShift) * 100.0 /
891 jacoTimeres);
893 } else {
894 current->start =
895 (unsigned
896 long) (((a1 * 3600 + a2 * 60 + a3) * jacoTimeres + a4 +
897 jacoShift) * 100.0 / jacoTimeres);
898 current->end =
899 (unsigned
900 long) (((b1 * 3600 + b2 * 60 + b3) * jacoTimeres + b4 +
901 jacoShift) * 100.0 / jacoTimeres);
903 current->lines = 0;
904 p = line2;
905 while ((*p == ' ') || (*p == '\t')) {
906 ++p;
908 if (isalpha(*p)||*p == '[') {
909 int cont, jLength;
911 if (sscanf(p, "%s %[^\n\r]", directive, line1) < 2)
912 return (subtitle *) ERR;
913 jLength = strlen(directive);
914 for (cont = 0; cont < jLength; ++cont) {
915 if (isalpha(*(directive + cont)))
916 *(directive + cont) = toupper(*(directive + cont));
918 if ((strstr(directive, "RDB") != NULL)
919 || (strstr(directive, "RDC") != NULL)
920 || (strstr(directive, "RLB") != NULL)
921 || (strstr(directive, "RLG") != NULL)) {
922 continue;
924 if (strstr(directive, "JL") != NULL) {
925 current->alignment = SUB_ALIGNMENT_BOTTOMLEFT;
926 } else if (strstr(directive, "JR") != NULL) {
927 current->alignment = SUB_ALIGNMENT_BOTTOMRIGHT;
928 } else {
929 current->alignment = SUB_ALIGNMENT_BOTTOMCENTER;
931 strcpy(line2, line1);
932 p = line2;
934 for (q = line1; (!eol(*p)) && (current->lines < SUB_MAX_TEXT); ++p) {
935 switch (*p) {
936 case '{':
937 comment++;
938 break;
939 case '}':
940 if (comment) {
941 --comment;
942 //the next line to get rid of a blank after the comment
943 if ((*(p + 1)) == ' ')
944 p++;
946 break;
947 case '~':
948 if (!comment) {
949 *q = ' ';
950 ++q;
952 break;
953 case ' ':
954 case '\t':
955 if ((*(p + 1) == ' ') || (*(p + 1) == '\t'))
956 break;
957 if (!comment) {
958 *q = ' ';
959 ++q;
961 break;
962 case '\\':
963 if (*(p + 1) == 'n') {
964 *q = '\0';
965 q = line1;
966 current->text[current->lines++] = strdup(line1);
967 ++p;
968 break;
970 if ((toupper(*(p + 1)) == 'C')
971 || (toupper(*(p + 1)) == 'F')) {
972 ++p,++p;
973 break;
975 if ((*(p + 1) == 'B') || (*(p + 1) == 'b') || (*(p + 1) == 'D') || //actually this means "insert current date here"
976 (*(p + 1) == 'I') || (*(p + 1) == 'i') || (*(p + 1) == 'N') || (*(p + 1) == 'T') || //actually this means "insert current time here"
977 (*(p + 1) == 'U') || (*(p + 1) == 'u')) {
978 ++p;
979 break;
981 if ((*(p + 1) == '\\') ||
982 (*(p + 1) == '~') || (*(p + 1) == '{')) {
983 ++p;
984 } else if (eol(*(p + 1))) {
985 if (!fgets(directive, LINE_LEN, fd))
986 return NULL;
987 trail_space(directive);
988 strncat(line2, directive,
989 (LINE_LEN > 511) ? LINE_LEN : 511);
990 break;
992 default:
993 if (!comment) {
994 *q = *p;
995 ++q;
997 } //-- switch
998 } //-- for
999 *q = '\0';
1000 current->text[current->lines] = strdup(line1);
1001 } //-- while
1002 current->lines++;
1003 return current;
1006 int sub_autodetect (FILE *fd, int *uses_time) {
1007 char line[LINE_LEN+1];
1008 int i,j=0;
1009 char p;
1011 while (j < 100) {
1012 j++;
1013 if (!fgets (line, LINE_LEN, fd))
1014 return SUB_INVALID;
1016 if (sscanf (line, "{%d}{%d}", &i, &i)==2)
1017 {*uses_time=0;return SUB_MICRODVD;}
1018 if (sscanf (line, "{%d}{}", &i)==1)
1019 {*uses_time=0;return SUB_MICRODVD;}
1020 if (sscanf (line, "[%d][%d]", &i, &i)==2)
1021 {*uses_time=1;return SUB_MPL2;}
1022 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
1023 {*uses_time=1;return SUB_SUBRIP;}
1024 if (sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i, &i, &i, (char *)&i, &i, &i, &i, &i, (char *)&i, &i)==10)
1025 {*uses_time=1;return SUB_SUBVIEWER;}
1026 if (sscanf (line, "{T %d:%d:%d:%d",&i, &i, &i, &i)==4)
1027 {*uses_time=1;return SUB_SUBVIEWER2;}
1028 if (strstr (line, "<SAMI>"))
1029 {*uses_time=1; return SUB_SAMI;}
1030 if (sscanf(line, "%d:%d:%d.%d %d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i) == 8)
1031 {*uses_time = 1; return SUB_JACOSUB;}
1032 if (sscanf(line, "@%d @%d", &i, &i) == 2)
1033 {*uses_time = 1; return SUB_JACOSUB;}
1034 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3)
1035 {*uses_time=1;return SUB_VPLAYER;}
1036 if (sscanf (line, "%d:%d:%d ", &i, &i, &i )==3)
1037 {*uses_time=1;return SUB_VPLAYER;}
1038 //TODO: just checking if first line of sub starts with "<" is WAY
1039 // too weak test for RT
1040 // Please someone who knows the format of RT... FIX IT!!!
1041 // It may conflict with other sub formats in the future (actually it doesn't)
1042 if ( *line == '<' )
1043 {*uses_time=1;return SUB_RT;}
1045 if (!memcmp(line, "Dialogue: Marked", 16))
1046 {*uses_time=1; return SUB_SSA;}
1047 if (!memcmp(line, "Dialogue: ", 10))
1048 {*uses_time=1; return SUB_SSA;}
1049 if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3)
1050 {*uses_time=1;return SUB_PJS;}
1051 if (sscanf (line, "FORMAT=%d", &i) == 1)
1052 {*uses_time=0; return SUB_MPSUB;}
1053 if (sscanf (line, "FORMAT=TIM%c", &p)==1 && p=='E')
1054 {*uses_time=1; return SUB_MPSUB;}
1055 if (strstr (line, "-->>"))
1056 {*uses_time=0; return SUB_AQTITLE;}
1057 if (sscanf (line, "[%d:%d:%d]", &i, &i, &i)==3)
1058 {*uses_time=1;return SUB_SUBRIP09;}
1061 return SUB_INVALID; // too many bad lines
1064 #ifdef DUMPSUBS
1065 int sub_utf8=0;
1066 #else
1067 extern int sub_utf8;
1068 int sub_utf8_prev=0;
1069 #endif
1071 extern float sub_delay;
1072 extern float sub_fps;
1074 #ifdef USE_ICONV
1075 static iconv_t icdsc = (iconv_t)(-1);
1077 void subcp_open (FILE *enca_fd)
1079 char *tocp = "UTF-8";
1081 if (sub_cp){
1082 char *cp_tmp = sub_cp;
1083 #ifdef HAVE_ENCA
1084 char enca_lang[3], enca_fallback[100];
1085 int free_cp_tmp = 0;
1086 if (sscanf(sub_cp, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
1087 || sscanf(sub_cp, "ENCA:%2s:%99s", enca_lang, enca_fallback) == 2) {
1088 if (enca_fd) {
1089 cp_tmp = guess_cp(enca_fd, enca_lang, enca_fallback);
1090 free_cp_tmp = 1;
1091 } else {
1092 cp_tmp = enca_fallback;
1095 #endif
1096 if ((icdsc = iconv_open (tocp, cp_tmp)) != (iconv_t)(-1)){
1097 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: opened iconv descriptor.\n");
1098 sub_utf8 = 2;
1099 } else
1100 mp_msg(MSGT_SUBREADER,MSGL_ERR,"SUB: error opening iconv descriptor.\n");
1101 #ifdef HAVE_ENCA
1102 if (free_cp_tmp && cp_tmp) free(cp_tmp);
1103 #endif
1107 void subcp_close (void)
1109 if (icdsc != (iconv_t)(-1)){
1110 (void) iconv_close (icdsc);
1111 icdsc = (iconv_t)(-1);
1112 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: closed iconv descriptor.\n");
1116 #define ICBUFFSIZE 512
1117 static char icbuffer[ICBUFFSIZE];
1119 subtitle* subcp_recode (subtitle *sub)
1121 int l=sub->lines;
1122 size_t ileft, oleft;
1123 char *op, *ip, *ot;
1125 while (l){
1126 op = icbuffer;
1127 ip = sub->text[--l];
1128 ileft = strlen(ip);
1129 oleft = ICBUFFSIZE - 1;
1131 if (iconv(icdsc, &ip, &ileft,
1132 &op, &oleft) == (size_t)(-1)) {
1133 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error recoding line (1).\n");
1134 l++;
1135 break;
1137 if (!(ot = (char *)malloc(op - icbuffer + 1))){
1138 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1139 l++;
1140 break;
1142 *op='\0' ;
1143 strcpy (ot, icbuffer);
1144 free (sub->text[l]);
1145 sub->text[l] = ot;
1147 if (l){
1148 for (l = sub->lines; l;)
1149 free (sub->text[--l]);
1150 return ERR;
1152 return sub;
1155 // for demux_ogg.c:
1156 subtitle* subcp_recode1 (subtitle *sub)
1158 int l=sub->lines;
1159 size_t ileft, oleft;
1161 if(icdsc == (iconv_t)(-1)) return sub;
1163 while (l){
1164 char *ip = icbuffer;
1165 char *op = sub->text[--l];
1166 strlcpy(ip, op, ICBUFFSIZE);
1167 ileft = strlen(ip);
1168 oleft = ICBUFFSIZE - 1;
1170 if (iconv(icdsc, &ip, &ileft,
1171 &op, &oleft) == (size_t)(-1)) {
1172 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: error recoding line (2).\n");
1173 return sub;
1175 *op='\0' ;
1177 return sub;
1179 #endif
1181 #ifdef USE_FRIBIDI
1182 #ifndef max
1183 #define max(a,b) (((a)>(b))?(a):(b))
1184 #endif
1185 subtitle* sub_fribidi (subtitle *sub, int sub_utf8)
1187 FriBidiChar logical[LINE_LEN+1], visual[LINE_LEN+1]; // Hopefully these two won't smash the stack
1188 char *ip = NULL, *op = NULL;
1189 FriBidiCharType base;
1190 size_t len,orig_len;
1191 int l=sub->lines;
1192 int char_set_num;
1193 fribidi_boolean log2vis;
1194 if(flip_hebrew) { // Please fix the indentation someday
1195 fribidi_set_mirroring (FRIBIDI_TRUE);
1196 fribidi_set_reorder_nsm (FRIBIDI_FALSE);
1198 if( sub_utf8 == 0 ) {
1199 char_set_num = fribidi_parse_charset (fribidi_charset?fribidi_charset:"ISO8859-8");
1200 }else {
1201 char_set_num = fribidi_parse_charset ("UTF-8");
1203 while (l) {
1204 ip = sub->text[--l];
1205 orig_len = len = strlen( ip ); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1206 if(len > LINE_LEN) {
1207 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: sub->text is longer than LINE_LEN.\n");
1208 l++;
1209 break;
1211 len = fribidi_charset_to_unicode (char_set_num, ip, len, logical);
1212 base = fribidi_flip_commas?FRIBIDI_TYPE_ON:FRIBIDI_TYPE_L;
1213 log2vis = fribidi_log2vis (logical, len, &base,
1214 /* output */
1215 visual, NULL, NULL, NULL);
1216 if(log2vis) {
1217 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL,
1218 NULL);
1219 if((op = (char*)malloc(sizeof(char)*(max(2*orig_len,2*len) + 1))) == NULL) {
1220 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1221 l++;
1222 break;
1224 fribidi_unicode_to_charset ( char_set_num, visual, len,op);
1225 free (ip);
1226 sub->text[l] = op;
1229 if (l){
1230 for (l = sub->lines; l;)
1231 free (sub->text[--l]);
1232 return ERR;
1235 return sub;
1238 #endif
1240 static void adjust_subs_time(subtitle* sub, float subtime, float fps, int block,
1241 int sub_num, int sub_uses_time) {
1242 int n,m;
1243 subtitle* nextsub;
1244 int i = sub_num;
1245 unsigned long subfms = (sub_uses_time ? 100 : fps) * subtime;
1246 unsigned long overlap = (sub_uses_time ? 100 : fps) / 5; // 0.2s
1248 n=m=0;
1249 if (i) for (;;){
1250 if (sub->end <= sub->start){
1251 sub->end = sub->start + subfms;
1252 m++;
1253 n++;
1255 if (!--i) break;
1256 nextsub = sub + 1;
1257 if(block){
1258 if ((sub->end > nextsub->start) && (sub->end <= nextsub->start + overlap)) {
1259 // these subtitles overlap for less than 0.2 seconds
1260 // and would result in very short overlapping subtitle
1261 // so let's fix the problem here, before overlapping code
1262 // get its hands on them
1263 unsigned delta = sub->end - nextsub->start, half = delta / 2;
1264 sub->end -= half + 1;
1265 nextsub->start += delta - half;
1267 if (sub->end >= nextsub->start){
1268 sub->end = nextsub->start - 1;
1269 if (sub->end - sub->start > subfms)
1270 sub->end = sub->start + subfms;
1271 if (!m)
1272 n++;
1276 /* Theory:
1277 * Movies are often converted from FILM (24 fps)
1278 * to PAL (25) by simply speeding it up, so we
1279 * to multiply the original timestmaps by
1280 * (Movie's FPS / Subtitle's (guessed) FPS)
1281 * so eg. for 23.98 fps movie and PAL time based
1282 * subtitles we say -subfps 25 and we're fine!
1285 /* timed sub fps correction ::atmos */
1286 if(sub_uses_time && sub_fps) {
1287 sub->start *= sub_fps/fps;
1288 sub->end *= sub_fps/fps;
1291 sub = nextsub;
1292 m = 0;
1294 if (n) mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Adjusted %d subtitle(s).\n", n);
1297 struct subreader {
1298 subtitle * (*read)(FILE *fd,subtitle *dest);
1299 void (*post)(subtitle *dest);
1300 const char *name;
1303 #ifdef HAVE_ENCA
1304 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1305 void* guess_cp(FILE *fd, char *preferred_language, char *fallback)
1307 const char **languages;
1308 size_t langcnt, buflen;
1309 EncaAnalyser analyser;
1310 EncaEncoding encoding;
1311 unsigned char *buffer;
1312 char *detected_sub_cp = NULL;
1313 int i;
1315 buffer = (unsigned char*)malloc(MAX_GUESS_BUFFER_SIZE*sizeof(char));
1316 buflen = fread(buffer, 1, MAX_GUESS_BUFFER_SIZE, fd);
1318 languages = enca_get_languages(&langcnt);
1319 mp_msg(MSGT_SUBREADER, MSGL_V, "ENCA supported languages: ");
1320 for (i = 0; i < langcnt; i++) {
1321 mp_msg(MSGT_SUBREADER, MSGL_V, "%s ", languages[i]);
1323 mp_msg(MSGT_SUBREADER, MSGL_V, "\n");
1325 for (i = 0; i < langcnt; i++) {
1326 if (strcasecmp(languages[i], preferred_language) != 0) continue;
1327 analyser = enca_analyser_alloc(languages[i]);
1328 encoding = enca_analyse_const(analyser, buffer, buflen);
1329 mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detected charset: %s\n", enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV));
1330 detected_sub_cp = strdup(enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV));
1331 enca_analyser_free(analyser);
1334 free(languages);
1335 free(buffer);
1336 rewind(fd);
1338 if (!detected_sub_cp) detected_sub_cp = strdup(fallback);
1340 return detected_sub_cp;
1342 #endif
1344 sub_data* sub_read_file (char *filename, float fps) {
1345 //filename is assumed to be malloc'ed, free() is used in sub_free()
1346 FILE *fd;
1347 int n_max, n_first, i, j, sub_first, sub_orig;
1348 subtitle *first, *second, *sub, *return_sub;
1349 sub_data *subt_data;
1350 int uses_time = 0, sub_num = 0, sub_errs = 0;
1351 struct subreader sr[]=
1353 { sub_read_line_microdvd, NULL, "microdvd" },
1354 { sub_read_line_subrip, NULL, "subrip" },
1355 { sub_read_line_subviewer, NULL, "subviewer" },
1356 { sub_read_line_sami, NULL, "sami" },
1357 { sub_read_line_vplayer, NULL, "vplayer" },
1358 { sub_read_line_rt, NULL, "rt" },
1359 { sub_read_line_ssa, sub_pp_ssa, "ssa" },
1360 { sub_read_line_pjs, NULL, "pjs" },
1361 { sub_read_line_mpsub, NULL, "mpsub" },
1362 { sub_read_line_aqt, NULL, "aqt" },
1363 { sub_read_line_subviewer2, NULL, "subviewer 2.0" },
1364 { sub_read_line_subrip09, NULL, "subrip 0.9" },
1365 { sub_read_line_jacosub, NULL, "jacosub" },
1366 { sub_read_line_mpl2, NULL, "mpl2" }
1368 struct subreader *srp;
1370 if(filename==NULL) return NULL; //qnx segfault
1371 fd=fopen (filename, "r"); if (!fd) return NULL;
1373 sub_format=sub_autodetect (fd, &uses_time);
1374 mpsub_multiplier = (uses_time ? 100.0 : 1.0);
1375 if (sub_format==SUB_INVALID) {mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: Could not determine file format\n");return NULL;}
1376 srp=sr+sub_format;
1377 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Detected subtitle file format: %s\n", srp->name);
1379 rewind (fd);
1381 #ifdef USE_ICONV
1382 sub_utf8_prev=sub_utf8;
1384 int l,k;
1385 k = -1;
1386 if ((l=strlen(filename))>4){
1387 char *exts[] = {".utf", ".utf8", ".utf-8" };
1388 for (k=3;--k>=0;)
1389 if (!strcasecmp(filename+(l - strlen(exts[k])), exts[k])){
1390 sub_utf8 = 1;
1391 break;
1394 if (k<0) subcp_open(fd);
1396 #endif
1398 sub_num=0;n_max=32;
1399 first=(subtitle *)malloc(n_max*sizeof(subtitle));
1400 if(!first){
1401 #ifdef USE_ICONV
1402 subcp_close();
1403 sub_utf8=sub_utf8_prev;
1404 #endif
1405 return NULL;
1408 #ifdef USE_SORTSUB
1409 sub = (subtitle *)malloc(sizeof(subtitle));
1410 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1411 //as the beginning of the following
1412 previous_sub_end = 0;
1413 #endif
1414 while(1){
1415 if(sub_num>=n_max){
1416 n_max+=16;
1417 first=realloc(first,n_max*sizeof(subtitle));
1419 #ifndef USE_SORTSUB
1420 sub = &first[sub_num];
1421 #endif
1422 memset(sub, '\0', sizeof(subtitle));
1423 sub=srp->read(fd,sub);
1424 if(!sub) break; // EOF
1425 #ifdef USE_ICONV
1426 if ((sub!=ERR) && (sub_utf8 & 2)) sub=subcp_recode(sub);
1427 #endif
1428 #ifdef USE_FRIBIDI
1429 if (sub!=ERR) sub=sub_fribidi(sub,sub_utf8);
1430 #endif
1431 if ( sub == ERR )
1433 #ifdef USE_ICONV
1434 subcp_close();
1435 #endif
1436 if ( first ) free(first);
1437 return NULL;
1439 // Apply any post processing that needs recoding first
1440 if ((sub!=ERR) && !sub_no_text_pp && srp->post) srp->post(sub);
1441 #ifdef USE_SORTSUB
1442 if(!sub_num || (first[sub_num - 1].start <= sub->start)){
1443 first[sub_num].start = sub->start;
1444 first[sub_num].end = sub->end;
1445 first[sub_num].lines = sub->lines;
1446 first[sub_num].alignment = sub->alignment;
1447 for(i = 0; i < sub->lines; ++i){
1448 first[sub_num].text[i] = sub->text[i];
1450 if (previous_sub_end){
1451 first[sub_num - 1].end = previous_sub_end;
1452 previous_sub_end = 0;
1454 } else {
1455 for(j = sub_num - 1; j >= 0; --j){
1456 first[j + 1].start = first[j].start;
1457 first[j + 1].end = first[j].end;
1458 first[j + 1].lines = first[j].lines;
1459 first[j + 1].alignment = first[j].alignment;
1460 for(i = 0; i < first[j].lines; ++i){
1461 first[j + 1].text[i] = first[j].text[i];
1463 if(!j || (first[j - 1].start <= sub->start)){
1464 first[j].start = sub->start;
1465 first[j].end = sub->end;
1466 first[j].lines = sub->lines;
1467 first[j].alignment = sub->alignment;
1468 for(i = 0; i < SUB_MAX_TEXT; ++i){
1469 first[j].text[i] = sub->text[i];
1471 if (previous_sub_end){
1472 first[j].end = first[j - 1].end;
1473 first[j - 1].end = previous_sub_end;
1474 previous_sub_end = 0;
1476 break;
1480 #endif
1481 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
1484 fclose(fd);
1486 #ifdef USE_ICONV
1487 subcp_close();
1488 #endif
1490 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1491 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Read %i subtitles", sub_num);
1492 if (sub_errs) mp_msg(MSGT_SUBREADER,MSGL_INFO,", %i bad line(s).\n", sub_errs);
1493 else mp_msg(MSGT_SUBREADER,MSGL_INFO,".\n");
1495 if(sub_num<=0){
1496 free(first);
1497 return NULL;
1500 // we do overlap if the user forced it (suboverlap_enable == 2) or
1501 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1502 // this is because usually overlapping subtitles are found in these formats,
1503 // while in others they are probably result of bad timing
1504 if ((suboverlap_enabled == 2) ||
1505 ((suboverlap_enabled) && ((sub_format == SUB_JACOSUB) || (sub_format == SUB_SSA)))) {
1506 adjust_subs_time(first, 6.0, fps, 0, sub_num, uses_time);/*~6 secs AST*/
1507 // here we manage overlapping subtitles
1508 sub_orig = sub_num;
1509 n_first = sub_num;
1510 sub_num = 0;
1511 second = NULL;
1512 // for each subtitle in first[] we deal with its 'block' of
1513 // bonded subtitles
1514 for (sub_first = 0; sub_first < n_first; ++sub_first) {
1515 unsigned long global_start = first[sub_first].start,
1516 global_end = first[sub_first].end, local_start, local_end;
1517 int lines_to_add = first[sub_first].lines, sub_to_add = 0,
1518 **placeholder = NULL, higher_line = 0, counter, start_block_sub = sub_num;
1519 char real_block = 1;
1521 // here we find the number of subtitles inside the 'block'
1522 // and its span interval. this works well only with sorted
1523 // subtitles
1524 while ((sub_first + sub_to_add + 1 < n_first) && (first[sub_first + sub_to_add + 1].start < global_end)) {
1525 ++sub_to_add;
1526 lines_to_add += first[sub_first + sub_to_add].lines;
1527 if (first[sub_first + sub_to_add].start < global_start) {
1528 global_start = first[sub_first + sub_to_add].start;
1530 if (first[sub_first + sub_to_add].end > global_end) {
1531 global_end = first[sub_first + sub_to_add].end;
1535 // we need a structure to keep trace of the screen lines
1536 // used by the subs, a 'placeholder'
1537 counter = 2 * sub_to_add + 1; // the maximum number of subs derived
1538 // from a block of sub_to_add+1 subs
1539 placeholder = (int **) malloc(sizeof(int *) * counter);
1540 for (i = 0; i < counter; ++i) {
1541 placeholder[i] = (int *) malloc(sizeof(int) * lines_to_add);
1542 for (j = 0; j < lines_to_add; ++j) {
1543 placeholder[i][j] = -1;
1547 counter = 0;
1548 local_end = global_start - 1;
1549 do {
1550 int ls;
1552 // here we find the beginning and the end of a new
1553 // subtitle in the block
1554 local_start = local_end + 1;
1555 local_end = global_end;
1556 for (j = 0; j <= sub_to_add; ++j) {
1557 if ((first[sub_first + j].start - 1 > local_start) && (first[sub_first + j].start - 1 < local_end)) {
1558 local_end = first[sub_first + j].start - 1;
1559 } else if ((first[sub_first + j].end > local_start) && (first[sub_first + j].end < local_end)) {
1560 local_end = first[sub_first + j].end;
1563 // here we allocate the screen lines to subs we must
1564 // display in current local_start-local_end interval.
1565 // if the subs were yet presents in the previous interval
1566 // they keep the same lines, otherside they get unused lines
1567 for (j = 0; j <= sub_to_add; ++j) {
1568 if ((first[sub_first + j].start <= local_end) && (first[sub_first + j].end > local_start)) {
1569 unsigned long sub_lines = first[sub_first + j].lines, fragment_length = lines_to_add + 1,
1570 tmp = 0;
1571 char boolean = 0;
1572 int fragment_position = -1;
1574 // if this is not the first new sub of the block
1575 // we find if this sub was present in the previous
1576 // new sub
1577 if (counter)
1578 for (i = 0; i < lines_to_add; ++i) {
1579 if (placeholder[counter - 1][i] == sub_first + j) {
1580 placeholder[counter][i] = sub_first + j;
1581 boolean = 1;
1584 if (boolean)
1585 continue;
1587 // we are looking for the shortest among all groups of
1588 // sequential blank lines whose length is greater than or
1589 // equal to sub_lines. we store in fragment_position the
1590 // position of the shortest group, in fragment_length its
1591 // length, and in tmp the length of the group currently
1592 // examinated
1593 for (i = 0; i < lines_to_add; ++i) {
1594 if (placeholder[counter][i] == -1) {
1595 // placeholder[counter][i] is part of the current group
1596 // of blank lines
1597 ++tmp;
1598 } else {
1599 if (tmp == sub_lines) {
1600 // current group's size fits exactly the one we
1601 // need, so we stop looking
1602 fragment_position = i - tmp;
1603 tmp = 0;
1604 break;
1606 if ((tmp) && (tmp > sub_lines) && (tmp < fragment_length)) {
1607 // current group is the best we found till here,
1608 // but is still bigger than the one we are looking
1609 // for, so we keep on looking
1610 fragment_length = tmp;
1611 fragment_position = i - tmp;
1612 tmp = 0;
1613 } else {
1614 // current group doesn't fit at all, so we forget it
1615 tmp = 0;
1619 if (tmp) {
1620 // last screen line is blank, a group ends with it
1621 if ((tmp >= sub_lines) && (tmp < fragment_length)) {
1622 fragment_position = i - tmp;
1625 if (fragment_position == -1) {
1626 // it was not possible to find free screen line(s) for a subtitle,
1627 // usually this means a bug in the code; however we do not overlap
1628 mp_msg(MSGT_SUBREADER, MSGL_WARN, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1629 higher_line = SUB_MAX_TEXT + 1;
1630 break;
1631 } else {
1632 for (tmp = 0; tmp < sub_lines; ++tmp) {
1633 placeholder[counter][fragment_position + tmp] = sub_first + j;
1638 for (j = higher_line + 1; j < lines_to_add; ++j) {
1639 if (placeholder[counter][j] != -1)
1640 higher_line = j;
1641 else
1642 break;
1644 if (higher_line >= SUB_MAX_TEXT) {
1645 // the 'block' has too much lines, so we don't overlap the
1646 // subtitles
1647 second = (subtitle *) realloc(second, (sub_num + sub_to_add + 1) * sizeof(subtitle));
1648 for (j = 0; j <= sub_to_add; ++j) {
1649 int ls;
1650 memset(&second[sub_num + j], '\0', sizeof(subtitle));
1651 second[sub_num + j].start = first[sub_first + j].start;
1652 second[sub_num + j].end = first[sub_first + j].end;
1653 second[sub_num + j].lines = first[sub_first + j].lines;
1654 second[sub_num + j].alignment = first[sub_first + j].alignment;
1655 for (ls = 0; ls < second[sub_num + j].lines; ls++) {
1656 second[sub_num + j].text[ls] = strdup(first[sub_first + j].text[ls]);
1659 sub_num += sub_to_add + 1;
1660 sub_first += sub_to_add;
1661 real_block = 0;
1662 break;
1665 // we read the placeholder structure and create the new
1666 // subs.
1667 second = (subtitle *) realloc(second, (sub_num + 1) * sizeof(subtitle));
1668 memset(&second[sub_num], '\0', sizeof(subtitle));
1669 second[sub_num].start = local_start;
1670 second[sub_num].end = local_end;
1671 second[sub_num].alignment = first[sub_first].alignment;
1672 n_max = (lines_to_add < SUB_MAX_TEXT) ? lines_to_add : SUB_MAX_TEXT;
1673 for (i = 0, j = 0; j < n_max; ++j) {
1674 if (placeholder[counter][j] != -1) {
1675 int lines = first[placeholder[counter][j]].lines;
1676 for (ls = 0; ls < lines; ++ls) {
1677 second[sub_num].text[i++] = strdup(first[placeholder[counter][j]].text[ls]);
1679 j += lines - 1;
1680 } else {
1681 second[sub_num].text[i++] = strdup(" ");
1684 ++sub_num;
1685 ++counter;
1686 } while (local_end < global_end);
1687 if (real_block)
1688 for (i = 0; i < counter; ++i)
1689 second[start_block_sub + i].lines = higher_line + 1;
1691 counter = 2 * sub_to_add + 1;
1692 for (i = 0; i < counter; ++i) {
1693 free(placeholder[i]);
1695 free(placeholder);
1696 sub_first += sub_to_add;
1699 for (j = sub_orig - 1; j >= 0; --j) {
1700 for (i = first[j].lines - 1; i >= 0; --i) {
1701 free(first[j].text[i]);
1704 free(first);
1706 return_sub = second;
1707 } else { //if(suboverlap_enabled)
1708 adjust_subs_time(first, 6.0, fps, 1, sub_num, uses_time);/*~6 secs AST*/
1709 return_sub = first;
1711 if (return_sub == NULL) return NULL;
1712 subt_data = (sub_data *)malloc(sizeof(sub_data));
1713 subt_data->filename = filename;
1714 subt_data->sub_uses_time = uses_time;
1715 subt_data->sub_num = sub_num;
1716 subt_data->sub_errs = sub_errs;
1717 subt_data->subtitles = return_sub;
1718 return subt_data;
1721 #if 0
1722 char * strreplace( char * in,char * what,char * whereof )
1724 int i;
1725 char * tmp;
1727 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL;
1728 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i];
1729 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0;
1730 return in;
1732 #endif
1735 static void strcpy_trim(char *d, char *s)
1737 // skip leading whitespace
1738 while (*s && !isalnum(*s)) {
1739 s++;
1741 for (;;) {
1742 // copy word
1743 while (*s && isalnum(*s)) {
1744 *d = tolower(*s);
1745 s++; d++;
1747 if (*s == 0) break;
1748 // trim excess whitespace
1749 while (*s && !isalnum(*s)) {
1750 s++;
1752 if (*s == 0) break;
1753 *d++ = ' ';
1755 *d = 0;
1758 static void strcpy_strip_ext(char *d, char *s)
1760 char *tmp = strrchr(s,'.');
1761 if (!tmp) {
1762 strcpy(d, s);
1763 return;
1764 } else {
1765 strncpy(d, s, tmp-s);
1766 d[tmp-s] = 0;
1768 while (*d) {
1769 *d = tolower(*d);
1770 d++;
1774 static void strcpy_get_ext(char *d, char *s)
1776 char *tmp = strrchr(s,'.');
1777 if (!tmp) {
1778 strcpy(d, "");
1779 return;
1780 } else {
1781 strcpy(d, tmp+1);
1785 static int whiteonly(char *s)
1787 while (*s) {
1788 if (isalnum(*s)) return 0;
1789 s++;
1791 return 1;
1794 typedef struct _subfn
1796 int priority;
1797 char *fname;
1798 } subfn;
1800 static int compare_sub_priority(const void *a, const void *b)
1802 if (((subfn*)a)->priority > ((subfn*)b)->priority) {
1803 return -1;
1804 } else if (((subfn*)a)->priority < ((subfn*)b)->priority) {
1805 return 1;
1806 } else {
1807 return strcoll(((subfn*)a)->fname, ((subfn*)b)->fname);
1811 char** sub_filenames(char* path, char *fname)
1813 char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
1814 char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult;
1816 int len, pos, found, i, j;
1817 char * sub_exts[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
1818 subfn *result;
1819 char **result2;
1821 int subcnt;
1823 FILE *f;
1825 DIR *d;
1826 struct dirent *de;
1828 len = (strlen(fname) > 256 ? strlen(fname) : 256)
1829 +(strlen(path) > 256 ? strlen(path) : 256)+2;
1831 f_dir = (char*)malloc(len);
1832 f_fname = (char*)malloc(len);
1833 f_fname_noext = (char*)malloc(len);
1834 f_fname_trim = (char*)malloc(len);
1836 tmp_fname_noext = (char*)malloc(len);
1837 tmp_fname_trim = (char*)malloc(len);
1838 tmp_fname_ext = (char*)malloc(len);
1840 tmpresult = (char*)malloc(len);
1842 result = (subfn*)malloc(sizeof(subfn)*MAX_SUBTITLE_FILES);
1843 memset(result, 0, sizeof(subfn)*MAX_SUBTITLE_FILES);
1845 subcnt = 0;
1847 tmp = strrchr(fname,'/');
1848 #ifdef WIN32
1849 if(!tmp)tmp = strrchr(fname,'\\');
1850 #endif
1852 // extract filename & dirname from fname
1853 if (tmp) {
1854 strcpy(f_fname, tmp+1);
1855 pos = tmp - fname;
1856 strncpy(f_dir, fname, pos+1);
1857 f_dir[pos+1] = 0;
1858 } else {
1859 strcpy(f_fname, fname);
1860 strcpy(f_dir, "./");
1863 strcpy_strip_ext(f_fname_noext, f_fname);
1864 strcpy_trim(f_fname_trim, f_fname_noext);
1866 tmp_sub_id = NULL;
1867 if (dvdsub_lang && !whiteonly(dvdsub_lang)) {
1868 tmp_sub_id = (char*)malloc(strlen(dvdsub_lang)+1);
1869 strcpy_trim(tmp_sub_id, dvdsub_lang);
1872 // 0 = nothing
1873 // 1 = any subtitle file
1874 // 2 = any sub file containing movie name
1875 // 3 = sub file containing movie name and the lang extension
1876 for (j = 0; j <= 1; j++) {
1877 d = opendir(j == 0 ? f_dir : path);
1878 if (d) {
1879 while ((de = readdir(d))) {
1880 // retrieve various parts of the filename
1881 strcpy_strip_ext(tmp_fname_noext, de->d_name);
1882 strcpy_get_ext(tmp_fname_ext, de->d_name);
1883 strcpy_trim(tmp_fname_trim, tmp_fname_noext);
1885 // does it end with a subtitle extension?
1886 found = 0;
1887 #ifdef USE_ICONV
1888 #ifdef HAVE_ENCA
1889 for (i = ((sub_cp && strncasecmp(sub_cp, "enca", 4) != 0) ? 3 : 0); sub_exts[i]; i++) {
1890 #else
1891 for (i = (sub_cp ? 3 : 0); sub_exts[i]; i++) {
1892 #endif
1893 #else
1894 for (i = 0; sub_exts[i]; i++) {
1895 #endif
1896 if (strcasecmp(sub_exts[i], tmp_fname_ext) == 0) {
1897 found = 1;
1898 break;
1902 // we have a (likely) subtitle file
1903 if (found) {
1904 int prio = 0;
1905 if (!prio && tmp_sub_id)
1907 sprintf(tmpresult, "%s %s", f_fname_trim, tmp_sub_id);
1908 printf("dvdsublang...%s\n", tmpresult);
1909 if (strcmp(tmp_fname_trim, tmpresult) == 0 && sub_match_fuzziness >= 1) {
1910 // matches the movie name + lang extension
1911 prio = 5;
1914 if (!prio && strcmp(tmp_fname_trim, f_fname_trim) == 0) {
1915 // matches the movie name
1916 prio = 4;
1918 if (!prio && (tmp = strstr(tmp_fname_trim, f_fname_trim)) && (sub_match_fuzziness >= 1)) {
1919 // contains the movie name
1920 tmp += strlen(f_fname_trim);
1921 if (tmp_sub_id && strstr(tmp, tmp_sub_id)) {
1922 // with sub_id specified prefer localized subtitles
1923 prio = 3;
1924 } else if ((tmp_sub_id == NULL) && whiteonly(tmp)) {
1925 // without sub_id prefer "plain" name
1926 prio = 3;
1927 } else {
1928 // with no localized subs found, try any else instead
1929 prio = 2;
1932 if (!prio) {
1933 // doesn't contain the movie name
1934 // don't try in the mplayer subtitle directory
1935 if ((j == 0) && (sub_match_fuzziness >= 2)) {
1936 prio = 1;
1940 if (prio) {
1941 prio += prio;
1942 #ifdef USE_ICONV
1943 if (i<3){ // prefer UTF-8 coded
1944 prio++;
1946 #endif
1947 sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name);
1948 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1949 if ((f = fopen(tmpresult, "rt"))) {
1950 fclose(f);
1951 result[subcnt].priority = prio;
1952 result[subcnt].fname = strdup(tmpresult);
1953 subcnt++;
1958 if (subcnt >= MAX_SUBTITLE_FILES) break;
1960 closedir(d);
1965 if (tmp_sub_id) free(tmp_sub_id);
1967 free(f_dir);
1968 free(f_fname);
1969 free(f_fname_noext);
1970 free(f_fname_trim);
1972 free(tmp_fname_noext);
1973 free(tmp_fname_trim);
1974 free(tmp_fname_ext);
1976 free(tmpresult);
1978 qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
1980 result2 = (char**)malloc(sizeof(char*)*(subcnt+1));
1981 memset(result2, 0, sizeof(char*)*(subcnt+1));
1983 for (i = 0; i < subcnt; i++) {
1984 result2[i] = result[i].fname;
1986 result2[subcnt] = NULL;
1988 free(result);
1990 return result2;
1993 void list_sub_file(sub_data* subd){
1994 int i,j;
1995 subtitle *subs = subd->subtitles;
1997 for(j=0; j < subd->sub_num; j++){
1998 subtitle* egysub=&subs[j];
1999 printf ("%i line%c (%li-%li)\n",
2000 egysub->lines,
2001 (1==egysub->lines)?' ':'s',
2002 egysub->start,
2003 egysub->end);
2004 for (i=0; i<egysub->lines; i++) {
2005 printf ("\t\t%d: %s%s", i,egysub->text[i], i==egysub->lines-1?"":" \n ");
2007 printf ("\n");
2010 printf ("Subtitle format %s time.\n",
2011 subd->sub_uses_time ? "uses":"doesn't use");
2012 printf ("Read %i subtitles, %i errors.\n", subd->sub_num, subd->sub_errs);
2015 void dump_srt(sub_data* subd, float fps){
2016 int i,j;
2017 int h,m,s,ms;
2018 FILE * fd;
2019 subtitle * onesub;
2020 unsigned long temp;
2021 subtitle *subs = subd->subtitles;
2023 if (!subd->sub_uses_time && sub_fps == 0)
2024 sub_fps = fps;
2025 fd=fopen("dumpsub.srt","w");
2026 if(!fd)
2028 perror("dump_srt: fopen");
2029 return;
2031 for(i=0; i < subd->sub_num; i++)
2033 onesub=subs+i; //=&subs[i];
2034 fprintf(fd,"%d\n",i+1);//line number
2036 temp=onesub->start;
2037 if (!subd->sub_uses_time)
2038 temp = temp * 100 / sub_fps;
2039 temp -= sub_delay * 100;
2040 h=temp/360000;temp%=360000; //h =1*100*60*60
2041 m=temp/6000; temp%=6000; //m =1*100*60
2042 s=temp/100; temp%=100; //s =1*100
2043 ms=temp*10; //ms=1*10
2044 fprintf(fd,"%02d:%02d:%02d,%03d --> ",h,m,s,ms);
2046 temp=onesub->end;
2047 if (!subd->sub_uses_time)
2048 temp = temp * 100 / sub_fps;
2049 temp -= sub_delay * 100;
2050 h=temp/360000;temp%=360000;
2051 m=temp/6000; temp%=6000;
2052 s=temp/100; temp%=100;
2053 ms=temp*10;
2054 fprintf(fd,"%02d:%02d:%02d,%03d\n",h,m,s,ms);
2056 for(j=0;j<onesub->lines;j++)
2057 fprintf(fd,"%s\n",onesub->text[j]);
2059 fprintf(fd,"\n");
2061 fclose(fd);
2062 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
2065 void dump_mpsub(sub_data* subd, float fps){
2066 int i,j;
2067 FILE *fd;
2068 float a,b;
2069 subtitle *subs = subd->subtitles;
2071 mpsub_position = subd->sub_uses_time? (sub_delay*100) : (sub_delay*fps);
2072 if (sub_fps==0) sub_fps=fps;
2074 fd=fopen ("dump.mpsub", "w");
2075 if (!fd) {
2076 perror ("dump_mpsub: fopen");
2077 return;
2081 if (subd->sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n");
2082 else fprintf (fd, "FORMAT=%5.2f\n\n", fps);
2084 for(j=0; j < subd->sub_num; j++){
2085 subtitle* egysub=&subs[j];
2086 if (subd->sub_uses_time) {
2087 a=((egysub->start-mpsub_position)/100.0);
2088 b=((egysub->end-egysub->start)/100.0);
2089 if ( (float)((int)a) == a)
2090 fprintf (fd, "%.0f",a);
2091 else
2092 fprintf (fd, "%.2f",a);
2094 if ( (float)((int)b) == b)
2095 fprintf (fd, " %.0f\n",b);
2096 else
2097 fprintf (fd, " %.2f\n",b);
2098 } else {
2099 fprintf (fd, "%ld %ld\n", (long)((egysub->start*(fps/sub_fps))-((mpsub_position*(fps/sub_fps)))),
2100 (long)(((egysub->end)-(egysub->start))*(fps/sub_fps)));
2103 mpsub_position = egysub->end;
2104 for (i=0; i<egysub->lines; i++) {
2105 fprintf (fd, "%s\n",egysub->text[i]);
2107 fprintf (fd, "\n");
2109 fclose (fd);
2110 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
2113 void dump_microdvd(sub_data* subd, float fps) {
2114 int i, delay;
2115 FILE *fd;
2116 subtitle *subs = subd->subtitles;
2117 if (sub_fps == 0)
2118 sub_fps = fps;
2119 fd = fopen("dumpsub.txt", "w");
2120 if (!fd) {
2121 perror("dumpsub.txt: fopen");
2122 return;
2124 delay = sub_delay * sub_fps;
2125 for (i = 0; i < subd->sub_num; ++i) {
2126 int j, start, end;
2127 start = subs[i].start;
2128 end = subs[i].end;
2129 if (subd->sub_uses_time) {
2130 start = start * sub_fps / 100 ;
2131 end = end * sub_fps / 100;
2133 else {
2134 start = start * sub_fps / fps;
2135 end = end * sub_fps / fps;
2137 start -= delay;
2138 end -= delay;
2139 fprintf(fd, "{%d}{%d}", start, end);
2140 for (j = 0; j < subs[i].lines; ++j)
2141 fprintf(fd, "%s%s", j ? "|" : "", subs[i].text[j]);
2142 fprintf(fd, "\n");
2144 fclose(fd);
2145 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
2148 void dump_jacosub(sub_data* subd, float fps) {
2149 int i,j;
2150 int h,m,s,cs;
2151 FILE * fd;
2152 subtitle * onesub;
2153 unsigned long temp;
2154 subtitle *subs = subd->subtitles;
2156 if (!subd->sub_uses_time && sub_fps == 0)
2157 sub_fps = fps;
2158 fd=fopen("dumpsub.jss","w");
2159 if(!fd)
2161 perror("dump_jacosub: fopen");
2162 return;
2164 fprintf(fd, "#TIMERES %d\n", (subd->sub_uses_time) ? 100 : (int)sub_fps);
2165 for(i=0; i < subd->sub_num; i++)
2167 onesub=subs+i; //=&subs[i];
2169 temp=onesub->start;
2170 if (!subd->sub_uses_time)
2171 temp = temp * 100 / sub_fps;
2172 temp -= sub_delay * 100;
2173 h=temp/360000;temp%=360000; //h =1*100*60*60
2174 m=temp/6000; temp%=6000; //m =1*100*60
2175 s=temp/100; temp%=100; //s =1*100
2176 cs=temp; //cs=1*10
2177 fprintf(fd,"%02d:%02d:%02d.%02d ",h,m,s,cs);
2179 temp=onesub->end;
2180 if (!subd->sub_uses_time)
2181 temp = temp * 100 / sub_fps;
2182 temp -= sub_delay * 100;
2183 h=temp/360000;temp%=360000;
2184 m=temp/6000; temp%=6000;
2185 s=temp/100; temp%=100;
2186 cs=temp;
2187 fprintf(fd,"%02d:%02d:%02d.%02d {~} ",h,m,s,cs);
2189 for(j=0;j<onesub->lines;j++)
2190 fprintf(fd,"%s%s",j ? "\\n" : "", onesub->text[j]);
2192 fprintf(fd,"\n");
2194 fclose(fd);
2195 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2198 void dump_sami(sub_data* subd, float fps) {
2199 int i,j;
2200 FILE * fd;
2201 subtitle * onesub;
2202 unsigned long temp;
2203 subtitle *subs = subd->subtitles;
2205 if (!subd->sub_uses_time && sub_fps == 0)
2206 sub_fps = fps;
2207 fd=fopen("dumpsub.smi","w");
2208 if(!fd)
2210 perror("dump_jacosub: fopen");
2211 return;
2213 fprintf(fd, "<SAMI>\n"
2214 "<HEAD>\n"
2215 " <STYLE TYPE=\"Text/css\">\n"
2216 " <!--\n"
2217 " P {margin-left: 29pt; margin-right: 29pt; font-size: 24pt; text-align: center; font-family: Tahoma; font-weight: bold; color: #FCDD03; background-color: #000000;}\n"
2218 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2219 " -->\n"
2220 " </STYLE>\n"
2221 "</HEAD>\n"
2222 "<BODY>\n");
2223 for(i=0; i < subd->sub_num; i++)
2225 onesub=subs+i; //=&subs[i];
2227 temp=onesub->start;
2228 if (!subd->sub_uses_time)
2229 temp = temp * 100 / sub_fps;
2230 temp -= sub_delay * 100;
2231 fprintf(fd,"\t<SYNC Start=%lu>\n"
2232 "\t <P>", temp * 10);
2234 for(j=0;j<onesub->lines;j++)
2235 fprintf(fd,"%s%s",j ? "<br>" : "", onesub->text[j]);
2237 fprintf(fd,"\n");
2239 temp=onesub->end;
2240 if (!subd->sub_uses_time)
2241 temp = temp * 100 / sub_fps;
2242 temp -= sub_delay * 100;
2243 fprintf(fd,"\t<SYNC Start=%lu>\n"
2244 "\t <P>&nbsp;\n", temp * 10);
2246 fprintf(fd, "</BODY>\n"
2247 "</SAMI>\n");
2248 fclose(fd);
2249 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2252 void sub_free( sub_data * subd )
2254 int i;
2256 if ( !subd ) return;
2258 if (subd->subtitles) {
2259 for (i=0; i < subd->subtitles->lines; i++) free( subd->subtitles->text[i] );
2260 free( subd->subtitles );
2262 if (subd->filename) free( subd->filename );
2263 free( subd );
2266 #ifdef DUMPSUBS
2267 int main(int argc, char **argv) { // for testing
2268 sub_data *subd;
2270 if(argc<2){
2271 printf("\nUsage: subreader filename.sub\n\n");
2272 exit(1);
2274 sub_cp = argv[2];
2275 subd = sub_read_file(argv[1]);
2276 if(!subd){
2277 printf("Couldn't load file.\n");
2278 exit(1);
2281 list_sub_file(subd);
2283 return 0;
2285 #endif