2 * Subtitle reader with format autodetection
5 * Some code cleanup & realloc() by A'rpi/ESP-team
15 #include <sys/types.h>
20 #include "subreader.h"
26 #define ERR ((void *) -1)
33 #include <fribidi/fribidi.h>
34 char *fribidi_charset
= NULL
;
38 extern char* dvdsub_lang
;
40 /* Maximal length of line of a subtitle */
42 static float mpsub_position
=0;
43 static float mpsub_multiplier
=1.;
44 static int sub_slacktime
= 20000; //20 sec
46 int sub_no_text_pp
=0; // 1 => do not apply text post-processing
47 // like {\...} elimination in SSA format.
49 int sub_match_fuzziness
=0; // level of sub name matching fuzziness
51 /* Use the SUB_* constant defined in the header file */
52 int sub_format
=SUB_INVALID
;
55 Some subtitling formats, namely AQT and Subrip09, define the end of a
56 subtitle as the beginning of the following. Since currently we read one
57 subtitle at time, for these format we keep two global *subtitle,
58 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
59 so we can change its end when we read current subtitle starting time.
60 When USE_SORTSUB is defined, we use a single global unsigned long,
61 previous_sub_end, for both (and even future) formats, to store the end of
62 the previous sub: it is initialized to 0 in sub_read_file and eventually
63 modified by sub_read_aqt_line or sub_read_subrip09_line.
65 unsigned long previous_sub_end
;
68 static int eol(char p
) {
69 return (p
=='\r' || p
=='\n' || p
=='\0');
72 /* Remove leading and trailing space */
73 static void trail_space(char *s
) {
75 while (isspace(s
[i
])) ++i
;
76 if (i
) strcpy(s
, s
+ i
);
78 while (i
> 0 && isspace(s
[i
])) s
[i
--] = '\0';
81 static char *stristr(const char *haystack
, const char *needle
) {
83 const char *p
= haystack
;
85 if (!(haystack
&& needle
)) return NULL
;
89 if (strncasecmp(p
, needle
, len
) == 0) return (char*)p
;
96 subtitle
*sub_read_line_sami(FILE *fd
, subtitle
*current
) {
97 static char line
[LINE_LEN
+1];
98 static char *s
= NULL
, *slacktime_s
;
99 char text
[LINE_LEN
+1], *p
=NULL
, *q
;
102 current
->lines
= current
->start
= current
->end
= 0;
105 /* read the first line */
107 if (!(s
= fgets(line
, LINE_LEN
, fd
))) return 0;
112 case 0: /* find "START=" or "Slacktime:" */
113 slacktime_s
= stristr (s
, "Slacktime:");
115 sub_slacktime
= strtol (slacktime_s
+10, NULL
, 0) / 10;
117 s
= stristr (s
, "Start=");
119 current
->start
= strtol (s
+ 6, &s
, 0) / 10;
121 for (; *s
!= '>' && *s
!= '\0'; s
++);
127 case 1: /* find (optionnal) "<P", skip other TAGs */
128 for (; *s
== ' ' || *s
== '\t'; s
++); /* strip blanks, if any */
129 if (*s
== '\0') break;
130 if (*s
!= '<') { state
= 3; p
= text
; continue; } /* not a TAG */
132 if (*s
== 'P' || *s
== 'p') { s
++; state
= 2; continue; } /* found '<P' */
133 for (; *s
!= '>' && *s
!= '\0'; s
++); /* skip remains of non-<P> TAG */
139 case 2: /* find ">" */
140 if ((s
= strchr (s
, '>'))) { s
++; state
= 3; p
= text
; continue; }
143 case 3: /* get all text until '<' appears */
144 if (*s
== '\0') break;
145 else if (!strncasecmp (s
, "<br>", 4)) {
146 *p
= '\0'; p
= text
; trail_space (text
);
148 current
->text
[current
->lines
++] = strdup (text
);
151 else if ((*s
== '{') && !sub_no_text_pp
) { state
= 5; ++s
; continue; }
152 else if (*s
== '<') { state
= 4; }
153 else if (!strncasecmp (s
, " ", 6)) { *p
++ = ' '; s
+= 6; }
154 else if (*s
== '\t') { *p
++ = ' '; s
++; }
155 else if (*s
== '\r' || *s
== '\n') { s
++; }
158 /* skip duplicated space */
159 if (p
> text
+ 2) if (*(p
-1) == ' ' && *(p
-2) == ' ') p
--;
163 case 4: /* get current->end or skip <TAG> */
164 q
= stristr (s
, "Start=");
166 current
->end
= strtol (q
+ 6, &q
, 0) / 10 - 1;
167 *p
= '\0'; trail_space (text
);
169 current
->text
[current
->lines
++] = strdup (text
);
170 if (current
->lines
> 0) { state
= 99; break; }
174 if (s
) { s
++; state
= 3; continue; }
176 case 5: /* get rid of {...} text */
177 if (*s
== '}') state
= 3;
183 if (state
!= 99 && !(s
= fgets (line
, LINE_LEN
, fd
))) {
184 if (current
->start
> 0) {
185 break; // if it is the last subtitle
191 } while (state
!= 99);
193 // For the last subtitle
194 if (current
->end
<= 0) {
195 current
->end
= current
->start
+ sub_slacktime
;
196 *p
= '\0'; trail_space (text
);
198 current
->text
[current
->lines
++] = strdup (text
);
205 char *sub_readtext(char *source
, char **dest
) {
209 // printf("src=%p dest=%p \n",source,dest);
211 while ( !eol(*p
) && *p
!= '|' ) {
215 *dest
= (char *)malloc (len
+1);
216 if (!dest
) {return ERR
;}
218 strncpy(*dest
, source
, len
);
221 while (*p
=='\r' || *p
=='\n' || *p
=='|') p
++;
223 if (*p
) return p
; // not-last text field
224 else return NULL
; // last text field
227 subtitle
*sub_read_line_microdvd(FILE *fd
,subtitle
*current
) {
228 char line
[LINE_LEN
+1];
229 char line2
[LINE_LEN
+1];
234 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
235 } while ((sscanf (line
,
237 &(current
->start
), line2
) < 2) &&
239 "{%ld}{%ld}%[^\r\n]",
240 &(current
->start
), &(current
->end
), line2
) < 3));
245 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
246 if (current
->text
[i
]==ERR
) {return ERR
;}
248 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
255 subtitle
*sub_read_line_mpl2(FILE *fd
,subtitle
*current
) {
256 char line
[LINE_LEN
+1];
257 char line2
[LINE_LEN
+1];
262 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
263 } while ((sscanf (line
,
264 "[%ld][%ld]%[^\r\n]",
265 &(current
->start
), &(current
->end
), line2
) < 3));
266 current
->start
*= 10;
271 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
272 if (current
->text
[i
]==ERR
) {return ERR
;}
274 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
281 subtitle
*sub_read_line_subrip(FILE *fd
, subtitle
*current
) {
282 char line
[LINE_LEN
+1];
283 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
284 char *p
=NULL
, *q
=NULL
;
288 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
289 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d",&a1
,&a2
,&a3
,&a4
,&b1
,&b2
,&b3
,&b4
) < 8) continue;
290 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
;
291 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
;
293 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
296 for (current
->lines
=1; current
->lines
< SUB_MAX_TEXT
; current
->lines
++) {
297 for (q
=p
,len
=0; *p
&& *p
!='\r' && *p
!='\n' && *p
!='|' && strncmp(p
,"[br]",4); p
++,len
++);
298 current
->text
[current
->lines
-1]=(char *)malloc (len
+1);
299 if (!current
->text
[current
->lines
-1]) return ERR
;
300 strncpy (current
->text
[current
->lines
-1], q
, len
);
301 current
->text
[current
->lines
-1][len
]='\0';
302 if (!*p
|| *p
=='\r' || *p
=='\n') break;
304 else while (*p
++!=']');
311 subtitle
*sub_read_line_subviewer(FILE *fd
,subtitle
*current
) {
312 char line
[LINE_LEN
+1];
313 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
317 while (!current
->text
[0]) {
318 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
319 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)
321 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
322 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
323 for (i
=0; i
<SUB_MAX_TEXT
;) {
324 if (!fgets (line
, LINE_LEN
, fd
)) break;
326 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; p
++,len
++);
329 char *curptr
=current
->text
[i
]=(char *)malloc (len
+1);
330 if (!current
->text
[i
]) return ERR
;
331 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
333 /* let's filter html tags ::atmos */
360 subtitle
*sub_read_line_subviewer2(FILE *fd
,subtitle
*current
) {
361 char line
[LINE_LEN
+1];
366 while (!current
->text
[0]) {
367 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
370 if ((len
=sscanf (line
, "{T %d:%d:%d:%d",&a1
,&a2
,&a3
,&a4
)) < 4)
372 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
373 for (i
=0; i
<SUB_MAX_TEXT
;) {
374 if (!fgets (line
, LINE_LEN
, fd
)) break;
375 if (line
[0]=='}') break;
377 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; ++p
,++len
);
379 current
->text
[i
]=(char *)malloc (len
+1);
380 if (!current
->text
[i
]) return ERR
;
381 strncpy (current
->text
[i
], line
, len
); current
->text
[i
][len
]='\0';
393 subtitle
*sub_read_line_vplayer(FILE *fd
,subtitle
*current
) {
394 char line
[LINE_LEN
+1];
396 char *p
=NULL
, *next
,separator
;
399 while (!current
->text
[0]) {
400 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
401 if ((len
=sscanf (line
, "%d:%d:%d%c%n",&a1
,&a2
,&a3
,&separator
,&plen
)) < 4)
404 if (!(current
->start
= a1
*360000+a2
*6000+a3
*100))
408 // finds the body of the subtitle
415 printf("SUB: Skipping incorrect subtitle line!\n");
419 // by wodzu: hey! this time we know what length it has! what is
420 // that magic for? it can't deal with space instead of third
421 // colon! look, what simple it can be:
428 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
429 if (current
->text
[i
]==ERR
) {return ERR
;}
431 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
439 subtitle
*sub_read_line_rt(FILE *fd
,subtitle
*current
) {
440 //TODO: This format uses quite rich (sub/super)set of xhtml
441 // I couldn't check it since DTD is not included.
442 // WARNING: full XML parses can be required for proper parsing
443 char line
[LINE_LEN
+1];
444 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
445 char *p
=NULL
,*next
=NULL
;
448 while (!current
->text
[0]) {
449 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
450 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
451 //to describe the same moment in time. Maybe there are even more formats in use.
452 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
453 plen
=a1
=a2
=a3
=a4
=b1
=b2
=b3
=b4
=0;
455 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b3
,&b4
,&plen
)) < 4) &&
456 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
457 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&plen
)) < 4) &&
458 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
459 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
460 ((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) &&
461 ((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) &&
462 //now try it without end time
463 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&plen
)) < 2) &&
464 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&plen
)) < 2) &&
465 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&a4
,&plen
)) < 3) &&
466 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1
,&a2
,&a3
,&a4
,&plen
)) < 4)
469 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
470 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
471 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0)
472 current
->end
= current
->start
+200;
474 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
475 next
= strstr(line
,"<clear/>");
476 if(next
&& strlen(next
)>8){
478 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
479 if (current
->text
[i
]==ERR
) {return ERR
;}
481 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
489 subtitle
*sub_read_line_ssa(FILE *fd
,subtitle
*current
) {
491 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
492 * other Sub Station Alpha scripts have only 8 commas before subtitle
493 * Reading the "ScriptType:" field is not reliable since many scripts appear
496 * http://www.scriptclub.org is a good place to find more examples
497 * http://www.eswat.demon.co.uk is where the SSA specs can be found
500 static int max_comma
= 32; /* let's use 32 for the case that the */
501 /* amount of commas increase with newer SSA versions */
503 int hour1
, min1
, sec1
, hunsec1
,
504 hour2
, min2
, sec2
, hunsec2
, nothing
;
507 char line
[LINE_LEN
+1],
513 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
514 } while (sscanf (line
, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
515 "%[^\n\r]", ¬hing
,
516 &hour1
, &min1
, &sec1
, &hunsec1
,
517 &hour2
, &min2
, &sec2
, &hunsec2
,
520 sscanf (line
, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
521 "%[^\n\r]", ¬hing
,
522 &hour1
, &min1
, &sec1
, &hunsec1
,
523 &hour2
, &min2
, &sec2
, &hunsec2
,
526 line2
=strchr(line3
, ',');
528 for (comma
= 4; comma
< max_comma
; comma
++)
531 if(!(tmp
=strchr(++tmp
, ','))) break;
532 if(*(++tmp
) == ' ') break;
533 /* a space after a comma means we're already in a sentence */
537 if(comma
< max_comma
)max_comma
= comma
;
538 /* eliminate the trailing comma */
539 if(*line2
== ',') line2
++;
541 current
->lines
=0;num
=0;
542 current
->start
= 360000*hour1
+ 6000*min1
+ 100*sec1
+ hunsec1
;
543 current
->end
= 360000*hour2
+ 6000*min2
+ 100*sec2
+ hunsec2
;
545 while (((tmp
=strstr(line2
, "\\n")) != NULL
) || ((tmp
=strstr(line2
, "\\N")) != NULL
) ){
546 current
->text
[num
]=(char *)malloc(tmp
-line2
+1);
547 strncpy (current
->text
[num
], line2
, tmp
-line2
);
548 current
->text
[num
][tmp
-line2
]='\0';
552 if (current
->lines
>= SUB_MAX_TEXT
) return current
;
555 current
->text
[num
]=strdup(line2
);
561 void sub_pp_ssa(subtitle
*sub
) {
566 /* eliminate any text enclosed with {}, they are font and color settings */
567 so
=de
=sub
->text
[--l
];
569 if(*so
== '{' && so
[1]=='\\') {
570 for (start
=so
; *so
&& *so
!='}'; so
++);
571 if(*so
) so
++; else so
=start
;
583 * PJS subtitles reader.
584 * That's the "Phoenix Japanimation Society" format.
585 * I found some of them in http://www.scriptsclub.org/ (used for anime).
586 * The time is in tenths of second.
588 * by set, based on code by szabi (dunnowhat sub format ;-)
590 subtitle
*sub_read_line_pjs(FILE *fd
,subtitle
*current
) {
591 char line
[LINE_LEN
+1];
592 char text
[LINE_LEN
+1], *s
, *d
;
594 if (!fgets (line
, LINE_LEN
, fd
))
597 for (s
=line
; *s
&& isspace(*s
); s
++);
598 /* allow empty lines at the end of the file */
602 if (sscanf (s
, "%ld,%ld,", &(current
->start
),
603 &(current
->end
)) <2) {
606 /* the files I have are in tenths of second */
607 current
->start
*= 10;
609 /* walk to the beggining of the string */
610 for (; *s
; s
++) if (*s
==',') break;
612 for (s
++; *s
; s
++) if (*s
==',') break;
618 /* copy the string to the text buffer */
619 for (s
++, d
=text
; *s
&& *s
!='"'; s
++, d
++)
622 current
->text
[0] = strdup(text
);
628 subtitle
*sub_read_line_mpsub(FILE *fd
, subtitle
*current
) {
629 char line
[LINE_LEN
+1];
636 if (!fgets(line
, LINE_LEN
, fd
)) return NULL
;
637 } while (sscanf (line
, "%f %f", &a
, &b
) !=2);
639 mpsub_position
+= a
*mpsub_multiplier
;
640 current
->start
=(int) mpsub_position
;
641 mpsub_position
+= b
*mpsub_multiplier
;
642 current
->end
=(int) mpsub_position
;
644 while (num
< SUB_MAX_TEXT
) {
645 if (!fgets (line
, LINE_LEN
, fd
)) {
646 if (num
== 0) return NULL
;
650 while (isspace(*p
)) p
++;
651 if (eol(*p
) && num
> 0) return current
;
652 if (eol(*p
)) return NULL
;
654 for (q
=p
; !eol(*q
); q
++);
657 current
->text
[num
]=strdup(p
);
658 // printf (">%s<\n",p);
659 current
->lines
= ++num
;
661 if (num
) return current
;
665 return NULL
; // we should have returned before if it's OK
669 //we don't need this if we use previous_sub_end
670 subtitle
*previous_aqt_sub
= NULL
;
673 subtitle
*sub_read_line_aqt(FILE *fd
,subtitle
*current
) {
674 char line
[LINE_LEN
+1];
679 // try to locate next subtitle
680 if (!fgets (line
, LINE_LEN
, fd
))
682 if (!(sscanf (line
, "-->> %ld", &(current
->start
)) <1))
687 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
689 if (previous_aqt_sub
!= NULL
)
690 previous_aqt_sub
->end
= current
->start
-1;
692 previous_aqt_sub
= current
;
695 if (!fgets (line
, LINE_LEN
, fd
))
698 sub_readtext((char *) &line
,¤t
->text
[0]);
700 current
->end
= current
->start
; // will be corrected by next subtitle
702 if (!fgets (line
, LINE_LEN
, fd
))
706 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
707 if (current
->text
[i
]==ERR
) {return ERR
;}
709 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
713 if ((current
->text
[0]=="") && (current
->text
[1]=="")) {
715 previous_sub_end
= 0;
717 // void subtitle -> end of previous marked and exit
718 previous_aqt_sub
= NULL
;
727 subtitle
*previous_subrip09_sub
= NULL
;
730 subtitle
*sub_read_line_subrip09(FILE *fd
,subtitle
*current
) {
731 char line
[LINE_LEN
+1];
737 // try to locate next subtitle
738 if (!fgets (line
, LINE_LEN
, fd
))
740 if (!((len
=sscanf (line
, "[%d:%d:%d]",&a1
,&a2
,&a3
)) < 3))
744 current
->start
= a1
*360000+a2
*6000+a3
*100;
747 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
749 if (previous_subrip09_sub
!= NULL
)
750 previous_subrip09_sub
->end
= current
->start
-1;
752 previous_subrip09_sub
= current
;
755 if (!fgets (line
, LINE_LEN
, fd
))
760 current
->text
[0]=""; // just to be sure that string is clear
762 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
763 if (current
->text
[i
]==ERR
) {return ERR
;}
765 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
769 if ((current
->text
[0]=="") && (i
==0)) {
771 previous_sub_end
= 0;
773 // void subtitle -> end of previous marked and exit
774 previous_subrip09_sub
= NULL
;
782 subtitle
*sub_read_line_jacosub(FILE * fd
, subtitle
* current
)
784 char line1
[LINE_LEN
], line2
[LINE_LEN
], directive
[LINE_LEN
], *p
, *q
;
785 unsigned a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, comment
= 0;
786 static unsigned jacoTimeres
= 30;
787 static int jacoShift
= 0;
789 bzero(current
, sizeof(subtitle
));
790 bzero(line1
, LINE_LEN
);
791 bzero(line2
, LINE_LEN
);
792 bzero(directive
, LINE_LEN
);
793 while (!current
->text
[0]) {
794 if (!fgets(line1
, LINE_LEN
, fd
)) {
798 (line1
, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1
, &a2
, &a3
, &a4
,
799 &b1
, &b2
, &b3
, &b4
, line2
) < 9) {
800 if (sscanf(line1
, "@%u @%u %[^\n\r]", &a4
, &b4
, line2
) < 3) {
801 if (line1
[0] == '#') {
802 int hours
= 0, minutes
= 0, seconds
, delta
, inverter
=
804 unsigned units
= jacoShift
;
805 switch (toupper(line1
[1])) {
807 if (isalpha(line1
[2])) {
812 if (sscanf(&line1
[delta
], "%d", &hours
)) {
817 if (sscanf(&line1
[delta
], "%*d:%d", &minutes
)) {
819 (&line1
[delta
], "%*d:%*d:%d",
821 sscanf(&line1
[delta
], "%*d:%*d:%*d.%d",
825 sscanf(&line1
[delta
], "%d:%d.%d",
826 &minutes
, &seconds
, &units
);
831 sscanf(&line1
[delta
], "%d.%d", &seconds
,
836 ((hours
* 3600 + minutes
* 60 +
837 seconds
) * jacoTimeres
+
842 if (isalpha(line1
[2])) {
847 sscanf(&line1
[delta
], "%u", &jacoTimeres
);
854 (unsigned long) ((a4
+ jacoShift
) * 100.0 /
857 (unsigned long) ((b4
+ jacoShift
) * 100.0 /
863 long) (((a1
* 3600 + a2
* 60 + a3
) * jacoTimeres
+ a4
+
864 jacoShift
) * 100.0 / jacoTimeres
);
867 long) (((b1
* 3600 + b2
* 60 + b3
) * jacoTimeres
+ b4
+
868 jacoShift
) * 100.0 / jacoTimeres
);
872 while ((*p
== ' ') || (*p
== '\t')) {
875 if (isalpha(*p
)||*p
== '[') {
878 if (sscanf(p
, "%s %[^\n\r]", directive
, line1
) < 2)
879 return (subtitle
*) ERR
;
880 jLength
= strlen(directive
);
881 for (cont
= 0; cont
< jLength
; ++cont
) {
882 if (isalpha(*(directive
+ cont
)))
883 *(directive
+ cont
) = toupper(*(directive
+ cont
));
885 if ((strstr(directive
, "RDB") != NULL
)
886 || (strstr(directive
, "RDC") != NULL
)
887 || (strstr(directive
, "RLB") != NULL
)
888 || (strstr(directive
, "RLG") != NULL
)) {
891 if (strstr(directive
, "JL") != NULL
) {
892 current
->alignment
= SUB_ALIGNMENT_HLEFT
;
893 } else if (strstr(directive
, "JR") != NULL
) {
894 current
->alignment
= SUB_ALIGNMENT_HRIGHT
;
896 current
->alignment
= SUB_ALIGNMENT_HCENTER
;
898 strcpy(line2
, line1
);
901 for (q
= line1
; (!eol(*p
)) && (current
->lines
< SUB_MAX_TEXT
); ++p
) {
909 //the next line to get rid of a blank after the comment
910 if ((*(p
+ 1)) == ' ')
922 if ((*(p
+ 1) == ' ') || (*(p
+ 1) == '\t'))
930 if (*(p
+ 1) == 'n') {
933 current
->text
[current
->lines
++] = strdup(line1
);
937 if ((toupper(*(p
+ 1)) == 'C')
938 || (toupper(*(p
+ 1)) == 'F')) {
942 if ((*(p
+ 1) == 'B') || (*(p
+ 1) == 'b') || (*(p
+ 1) == 'D') || //actually this means "insert current date here"
943 (*(p
+ 1) == 'I') || (*(p
+ 1) == 'i') || (*(p
+ 1) == 'N') || (*(p
+ 1) == 'T') || //actually this means "insert current time here"
944 (*(p
+ 1) == 'U') || (*(p
+ 1) == 'u')) {
948 if ((*(p
+ 1) == '\\') ||
949 (*(p
+ 1) == '~') || (*(p
+ 1) == '{')) {
951 } else if (eol(*(p
+ 1))) {
952 if (!fgets(directive
, LINE_LEN
, fd
))
954 trail_space(directive
);
955 strncat(line2
, directive
,
956 (LINE_LEN
> 511) ? LINE_LEN
: 511);
967 current
->text
[current
->lines
] = strdup(line1
);
973 int sub_autodetect (FILE *fd
, int *uses_time
) {
974 char line
[LINE_LEN
+1];
980 if (!fgets (line
, LINE_LEN
, fd
))
983 if (sscanf (line
, "{%d}{%d}", &i
, &i
)==2)
984 {*uses_time
=0;return SUB_MICRODVD
;}
985 if (sscanf (line
, "{%d}{}", &i
)==1)
986 {*uses_time
=0;return SUB_MICRODVD
;}
987 if (sscanf (line
, "[%d][%d]", &i
, &i
)==2)
988 {*uses_time
=1;return SUB_MPL2
;}
989 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
)==8)
990 {*uses_time
=1;return SUB_SUBRIP
;}
991 if (sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i
, &i
, &i
, (char *)&i
, &i
, &i
, &i
, &i
, (char *)&i
, &i
)==10)
992 {*uses_time
=1;return SUB_SUBVIEWER
;}
993 if (sscanf (line
, "{T %d:%d:%d:%d",&i
, &i
, &i
, &i
)==4)
994 {*uses_time
=1;return SUB_SUBVIEWER2
;}
995 if (strstr (line
, "<SAMI>"))
996 {*uses_time
=1; return SUB_SAMI
;}
997 if (sscanf(line
, "%d:%d:%d.%d %d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
) == 8)
998 {*uses_time
= 1; return SUB_JACOSUB
;}
999 if (sscanf(line
, "@%d @%d", &i
, &i
) == 2)
1000 {*uses_time
= 1; return SUB_JACOSUB
;}
1001 if (sscanf (line
, "%d:%d:%d:", &i
, &i
, &i
)==3)
1002 {*uses_time
=1;return SUB_VPLAYER
;}
1003 if (sscanf (line
, "%d:%d:%d ", &i
, &i
, &i
)==3)
1004 {*uses_time
=1;return SUB_VPLAYER
;}
1005 //TODO: just checking if first line of sub starts with "<" is WAY
1006 // too weak test for RT
1007 // Please someone who knows the format of RT... FIX IT!!!
1008 // It may conflict with other sub formats in the future (actually it doesn't)
1010 {*uses_time
=1;return SUB_RT
;}
1012 if (!memcmp(line
, "Dialogue: Marked", 16))
1013 {*uses_time
=1; return SUB_SSA
;}
1014 if (!memcmp(line
, "Dialogue: ", 10))
1015 {*uses_time
=1; return SUB_SSA
;}
1016 if (sscanf (line
, "%d,%d,\"%c", &i
, &i
, (char *) &i
) == 3)
1017 {*uses_time
=1;return SUB_PJS
;}
1018 if (sscanf (line
, "FORMAT=%d", &i
) == 1)
1019 {*uses_time
=0; return SUB_MPSUB
;}
1020 if (sscanf (line
, "FORMAT=TIM%c", &p
)==1 && p
=='E')
1021 {*uses_time
=1; return SUB_MPSUB
;}
1022 if (strstr (line
, "-->>"))
1023 {*uses_time
=0; return SUB_AQTITLE
;}
1024 if (sscanf (line
, "[%d:%d:%d]", &i
, &i
, &i
)==3)
1025 {*uses_time
=1;return SUB_SUBRIP09
;}
1028 return SUB_INVALID
; // too many bad lines
1034 extern int sub_utf8
;
1035 int sub_utf8_prev
=0;
1038 extern float sub_delay
;
1039 extern float sub_fps
;
1042 static iconv_t icdsc
= (iconv_t
)(-1);
1045 void subcp_open_noenca ()
1047 char enca_lang
[100], enca_fallback
[100];
1049 if (sscanf(sub_cp
, "enca:%2s:%s", enca_lang
, enca_fallback
) == 2
1050 || sscanf(sub_cp
, "ENCA:%2s:%s", enca_lang
, enca_fallback
) == 2) {
1051 subcp_open(enca_fallback
);
1058 void subcp_open_noenca ()
1064 void subcp_open (char *current_sub_cp
)
1066 char *tocp
= "UTF-8";
1068 if (current_sub_cp
){
1069 if ((icdsc
= iconv_open (tocp
, current_sub_cp
)) != (iconv_t
)(-1)){
1070 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
1073 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
1077 void subcp_close (void)
1079 if (icdsc
!= (iconv_t
)(-1)){
1080 (void) iconv_close (icdsc
);
1081 icdsc
= (iconv_t
)(-1);
1082 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: closed iconv descriptor.\n");
1086 #define ICBUFFSIZE 512
1087 static char icbuffer
[ICBUFFSIZE
];
1089 subtitle
* subcp_recode (subtitle
*sub
)
1092 size_t ileft
, oleft
;
1097 ip
= sub
->text
[--l
];
1099 oleft
= ICBUFFSIZE
- 1;
1101 if (iconv(icdsc
, &ip
, &ileft
,
1102 &op
, &oleft
) == (size_t)(-1)) {
1103 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line (1).\n");
1107 if (!(ot
= (char *)malloc(op
- icbuffer
+ 1))){
1108 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1113 strcpy (ot
, icbuffer
);
1114 free (sub
->text
[l
]);
1118 for (l
= sub
->lines
; l
;)
1119 free (sub
->text
[--l
]);
1126 subtitle
* subcp_recode1 (subtitle
*sub
)
1129 size_t ileft
, oleft
;
1131 if(icdsc
== (iconv_t
)(-1)) return sub
;
1134 char *ip
= icbuffer
;
1135 char *op
= sub
->text
[--l
];
1138 oleft
= ICBUFFSIZE
- 1;
1140 if (iconv(icdsc
, &ip
, &ileft
,
1141 &op
, &oleft
) == (size_t)(-1)) {
1142 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: error recoding line (2).\n");
1153 #define max(a,b) (((a)>(b))?(a):(b))
1155 subtitle
* sub_fribidi (subtitle
*sub
, int sub_utf8
)
1157 FriBidiChar logical
[LINE_LEN
+1], visual
[LINE_LEN
+1]; // Hopefully these two won't smash the stack
1158 char *ip
= NULL
, *op
= NULL
;
1159 FriBidiCharType base
;
1160 size_t len
,orig_len
;
1163 fribidi_boolean log2vis
;
1164 if(flip_hebrew
) { // Please fix the indentation someday
1165 fribidi_set_mirroring (FRIBIDI_TRUE
);
1166 fribidi_set_reorder_nsm (FRIBIDI_FALSE
);
1168 if( sub_utf8
== 0 ) {
1169 char_set_num
= fribidi_parse_charset (fribidi_charset
?fribidi_charset
:"ISO8859-8");
1171 char_set_num
= fribidi_parse_charset ("UTF-8");
1174 ip
= sub
->text
[--l
];
1175 orig_len
= len
= strlen( ip
); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1176 if(len
> LINE_LEN
) {
1177 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: sub->text is longer than LINE_LEN.\n");
1181 len
= fribidi_charset_to_unicode (char_set_num
, ip
, len
, logical
);
1182 base
= FRIBIDI_TYPE_ON
;
1183 log2vis
= fribidi_log2vis (logical
, len
, &base
,
1185 visual
, NULL
, NULL
, NULL
);
1187 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
,
1189 if((op
= (char*)malloc(sizeof(char)*(max(2*orig_len
,2*len
) + 1))) == NULL
) {
1190 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1194 fribidi_unicode_to_charset ( char_set_num
, visual
, len
,op
);
1200 for (l
= sub
->lines
; l
;)
1201 free (sub
->text
[--l
]);
1210 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1211 int sub_num
, int sub_uses_time
) {
1215 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1216 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1220 if (sub
->end
<= sub
->start
){
1221 sub
->end
= sub
->start
+ subfms
;
1228 if ((sub
->end
> nextsub
->start
) && (sub
->end
<= nextsub
->start
+ overlap
)) {
1229 // these subtitles overlap for less than 0.2 seconds
1230 // and would result in very short overlapping subtitle
1231 // so let's fix the problem here, before overlapping code
1232 // get its hands on them
1233 unsigned delta
= sub
->end
- nextsub
->start
, half
= delta
/ 2;
1234 sub
->end
-= half
+ 1;
1235 nextsub
->start
+= delta
- half
;
1237 if (sub
->end
>= nextsub
->start
){
1238 sub
->end
= nextsub
->start
- 1;
1239 if (sub
->end
- sub
->start
> subfms
)
1240 sub
->end
= sub
->start
+ subfms
;
1247 * Movies are often converted from FILM (24 fps)
1248 * to PAL (25) by simply speeding it up, so we
1249 * to multiply the original timestmaps by
1250 * (Movie's FPS / Subtitle's (guessed) FPS)
1251 * so eg. for 23.98 fps movie and PAL time based
1252 * subtitles we say -subfps 25 and we're fine!
1255 /* timed sub fps correction ::atmos */
1256 if(sub_uses_time
&& sub_fps
) {
1257 sub
->start
*= sub_fps
/fps
;
1258 sub
->end
*= sub_fps
/fps
;
1264 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Adjusted %d subtitle(s).\n", n
);
1268 subtitle
* (*read
)(FILE *fd
,subtitle
*dest
);
1269 void (*post
)(subtitle
*dest
);
1274 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1275 void* guess_cp(FILE *fd
, char *preferred_language
, char *fallback
)
1277 const char **languages
;
1278 size_t langcnt
, buflen
;
1279 EncaAnalyser analyser
;
1280 EncaEncoding encoding
;
1281 unsigned char *buffer
;
1282 char *detected_sub_cp
= NULL
;
1285 buffer
= (unsigned char*)malloc(MAX_GUESS_BUFFER_SIZE
*sizeof(char));
1286 buflen
= fread(buffer
, 1, MAX_GUESS_BUFFER_SIZE
, fd
);
1288 languages
= enca_get_languages(&langcnt
);
1289 mp_msg(MSGT_SUBREADER
, MSGL_V
, "ENCA supported languages: ");
1290 for (i
= 0; i
< langcnt
; i
++) {
1291 mp_msg(MSGT_SUBREADER
, MSGL_V
, "%s ", languages
[i
]);
1293 mp_msg(MSGT_SUBREADER
, MSGL_V
, "\n");
1295 for (i
= 0; i
< langcnt
; i
++) {
1296 if (strcasecmp(languages
[i
], preferred_language
) != 0) continue;
1297 analyser
= enca_analyser_alloc(languages
[i
]);
1298 encoding
= enca_analyse_const(analyser
, buffer
, buflen
);
1299 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detected charset: %s\n", enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
));
1300 detected_sub_cp
= strdup(enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
));
1301 enca_analyser_free(analyser
);
1308 if (!detected_sub_cp
) detected_sub_cp
= strdup(fallback
);
1310 return detected_sub_cp
;
1314 sub_data
* sub_read_file (char *filename
, float fps
) {
1315 //filename is assumed to be malloc'ed, free() is used in sub_free()
1317 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1318 subtitle
*first
, *second
, *sub
, *return_sub
;
1319 sub_data
*subt_data
;
1320 char enca_lang
[100], enca_fallback
[100];
1321 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1322 char *current_sub_cp
=NULL
;
1323 struct subreader sr
[]=
1325 { sub_read_line_microdvd
, NULL
, "microdvd" },
1326 { sub_read_line_subrip
, NULL
, "subrip" },
1327 { sub_read_line_subviewer
, NULL
, "subviewer" },
1328 { sub_read_line_sami
, NULL
, "sami" },
1329 { sub_read_line_vplayer
, NULL
, "vplayer" },
1330 { sub_read_line_rt
, NULL
, "rt" },
1331 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1332 { sub_read_line_pjs
, NULL
, "pjs" },
1333 { sub_read_line_mpsub
, NULL
, "mpsub" },
1334 { sub_read_line_aqt
, NULL
, "aqt" },
1335 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1336 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1337 { sub_read_line_jacosub
, NULL
, "jacosub" },
1338 { sub_read_line_mpl2
, NULL
, "mpl2" }
1340 struct subreader
*srp
;
1342 if(filename
==NULL
) return NULL
; //qnx segfault
1343 fd
=fopen (filename
, "r"); if (!fd
) return NULL
;
1345 sub_format
=sub_autodetect (fd
, &uses_time
);
1346 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1347 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1349 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Detected subtitle file format: %s\n", srp
->name
);
1355 if (sscanf(sub_cp
, "enca:%2s:%s", enca_lang
, enca_fallback
) == 2
1356 || sscanf(sub_cp
, "ENCA:%2s:%s", enca_lang
, enca_fallback
) == 2) {
1357 current_sub_cp
= guess_cp(fd
, enca_lang
, enca_fallback
);
1359 current_sub_cp
= sub_cp
? strdup(sub_cp
) : NULL
;
1362 current_sub_cp
= sub_cp
? strdup(sub_cp
) : NULL
;
1365 sub_utf8_prev
=sub_utf8
;
1369 if ((l
=strlen(filename
))>4){
1370 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1372 if (!strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1377 if (k
<0) subcp_open(current_sub_cp
);
1380 if (current_sub_cp
) free(current_sub_cp
);
1383 first
=(subtitle
*)malloc(n_max
*sizeof(subtitle
));
1387 sub_utf8
=sub_utf8_prev
;
1393 sub
= (subtitle
*)malloc(sizeof(subtitle
));
1394 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1395 //as the beginning of the following
1396 previous_sub_end
= 0;
1401 first
=realloc(first
,n_max
*sizeof(subtitle
));
1404 sub
= &first
[sub_num
];
1406 memset(sub
, '\0', sizeof(subtitle
));
1407 sub
=srp
->read(fd
,sub
);
1408 if(!sub
) break; // EOF
1410 if ((sub
!=ERR
) && (sub_utf8
& 2)) sub
=subcp_recode(sub
);
1413 if (sub
!=ERR
) sub
=sub_fribidi(sub
,sub_utf8
);
1420 if ( first
) free(first
);
1423 // Apply any post processing that needs recoding first
1424 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1426 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1427 first
[sub_num
].start
= sub
->start
;
1428 first
[sub_num
].end
= sub
->end
;
1429 first
[sub_num
].lines
= sub
->lines
;
1430 first
[sub_num
].alignment
= sub
->alignment
;
1431 for(i
= 0; i
< sub
->lines
; ++i
){
1432 first
[sub_num
].text
[i
] = sub
->text
[i
];
1434 if (previous_sub_end
){
1435 first
[sub_num
- 1].end
= previous_sub_end
;
1436 previous_sub_end
= 0;
1439 for(j
= sub_num
- 1; j
>= 0; --j
){
1440 first
[j
+ 1].start
= first
[j
].start
;
1441 first
[j
+ 1].end
= first
[j
].end
;
1442 first
[j
+ 1].lines
= first
[j
].lines
;
1443 first
[j
+ 1].alignment
= first
[j
].alignment
;
1444 for(i
= 0; i
< first
[j
].lines
; ++i
){
1445 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1447 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1448 first
[j
].start
= sub
->start
;
1449 first
[j
].end
= sub
->end
;
1450 first
[j
].lines
= sub
->lines
;
1451 first
[j
].alignment
= sub
->alignment
;
1452 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1453 first
[j
].text
[i
] = sub
->text
[i
];
1455 if (previous_sub_end
){
1456 first
[j
].end
= first
[j
- 1].end
;
1457 first
[j
- 1].end
= previous_sub_end
;
1458 previous_sub_end
= 0;
1465 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1474 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1475 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Read %i subtitles", sub_num
);
1476 if (sub_errs
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,", %i bad line(s).\n", sub_errs
);
1477 else mp_msg(MSGT_SUBREADER
,MSGL_INFO
,".\n");
1484 // we do overlap if the user forced it (suboverlap_enable == 2) or
1485 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1486 // this is because usually overlapping subtitles are found in these formats,
1487 // while in others they are probably result of bad timing
1488 if ((suboverlap_enabled
== 2) ||
1489 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1490 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1491 // here we manage overlapping subtitles
1496 // for each subtitle in first[] we deal with its 'block' of
1498 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1499 unsigned long global_start
= first
[sub_first
].start
,
1500 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1501 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1502 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1503 char real_block
= 1;
1505 // here we find the number of subtitles inside the 'block'
1506 // and its span interval. this works well only with sorted
1508 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1510 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1511 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1512 global_start
= first
[sub_first
+ sub_to_add
].start
;
1514 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1515 global_end
= first
[sub_first
+ sub_to_add
].end
;
1519 // we need a structure to keep trace of the screen lines
1520 // used by the subs, a 'placeholder'
1521 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1522 // from a block of sub_to_add+1 subs
1523 placeholder
= (int **) malloc(sizeof(int *) * counter
);
1524 for (i
= 0; i
< counter
; ++i
) {
1525 placeholder
[i
] = (int *) malloc(sizeof(int) * lines_to_add
);
1526 for (j
= 0; j
< lines_to_add
; ++j
) {
1527 placeholder
[i
][j
] = -1;
1532 local_end
= global_start
- 1;
1536 // here we find the beginning and the end of a new
1537 // subtitle in the block
1538 local_start
= local_end
+ 1;
1539 local_end
= global_end
;
1540 for (j
= 0; j
<= sub_to_add
; ++j
) {
1541 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1542 local_end
= first
[sub_first
+ j
].start
- 1;
1543 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1544 local_end
= first
[sub_first
+ j
].end
;
1547 // here we allocate the screen lines to subs we must
1548 // display in current local_start-local_end interval.
1549 // if the subs were yet presents in the previous interval
1550 // they keep the same lines, otherside they get unused lines
1551 for (j
= 0; j
<= sub_to_add
; ++j
) {
1552 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1553 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1556 int fragment_position
= -1;
1558 // if this is not the first new sub of the block
1559 // we find if this sub was present in the previous
1562 for (i
= 0; i
< lines_to_add
; ++i
) {
1563 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1564 placeholder
[counter
][i
] = sub_first
+ j
;
1571 // we are looking for the shortest among all groups of
1572 // sequential blank lines whose length is greater than or
1573 // equal to sub_lines. we store in fragment_position the
1574 // position of the shortest group, in fragment_length its
1575 // length, and in tmp the length of the group currently
1577 for (i
= 0; i
< lines_to_add
; ++i
) {
1578 if (placeholder
[counter
][i
] == -1) {
1579 // placeholder[counter][i] is part of the current group
1583 if (tmp
== sub_lines
) {
1584 // current group's size fits exactly the one we
1585 // need, so we stop looking
1586 fragment_position
= i
- tmp
;
1590 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1591 // current group is the best we found till here,
1592 // but is still bigger than the one we are looking
1593 // for, so we keep on looking
1594 fragment_length
= tmp
;
1595 fragment_position
= i
- tmp
;
1598 // current group doesn't fit at all, so we forget it
1604 // last screen line is blank, a group ends with it
1605 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1606 fragment_position
= i
- tmp
;
1609 if (fragment_position
== -1) {
1610 // it was not possible to find free screen line(s) for a subtitle,
1611 // usually this means a bug in the code; however we do not overlap
1612 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1613 higher_line
= SUB_MAX_TEXT
+ 1;
1616 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1617 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1622 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1623 if (placeholder
[counter
][j
] != -1)
1628 if (higher_line
>= SUB_MAX_TEXT
) {
1629 // the 'block' has too much lines, so we don't overlap the
1631 second
= (subtitle
*) realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1632 for (j
= 0; j
<= sub_to_add
; ++j
) {
1634 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1635 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1636 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1637 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1638 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1639 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1640 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1643 sub_num
+= sub_to_add
+ 1;
1644 sub_first
+= sub_to_add
;
1649 // we read the placeholder structure and create the new
1651 second
= (subtitle
*) realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1652 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1653 second
[sub_num
].start
= local_start
;
1654 second
[sub_num
].end
= local_end
;
1655 second
[sub_num
].alignment
= SUB_ALIGNMENT_HCENTER
;
1656 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1657 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1658 if (placeholder
[counter
][j
] != -1) {
1659 int lines
= first
[placeholder
[counter
][j
]].lines
;
1660 for (ls
= 0; ls
< lines
; ++ls
) {
1661 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1665 second
[sub_num
].text
[i
++] = strdup(" ");
1670 } while (local_end
< global_end
);
1672 for (i
= 0; i
< counter
; ++i
)
1673 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1675 counter
= 2 * sub_to_add
+ 1;
1676 for (i
= 0; i
< counter
; ++i
) {
1677 free(placeholder
[i
]);
1680 sub_first
+= sub_to_add
;
1683 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1684 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1685 free(first
[j
].text
[i
]);
1690 return_sub
= second
;
1691 } else { //if(suboverlap_enabled)
1692 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1695 if (return_sub
== NULL
) return NULL
;
1696 subt_data
= (sub_data
*)malloc(sizeof(sub_data
));
1697 subt_data
->filename
= filename
;
1698 subt_data
->sub_uses_time
= uses_time
;
1699 subt_data
->sub_num
= sub_num
;
1700 subt_data
->sub_errs
= sub_errs
;
1701 subt_data
->subtitles
= return_sub
;
1706 char * strreplace( char * in
,char * what
,char * whereof
)
1711 if ( ( in
== NULL
)||( what
== NULL
)||( whereof
== NULL
)||( ( tmp
=strstr( in
,what
) ) == NULL
) ) return NULL
;
1712 for( i
=0;i
<strlen( whereof
);i
++ ) tmp
[i
]=whereof
[i
];
1713 if ( strlen( what
) > strlen( whereof
) ) tmp
[i
]=0;
1719 static void strcpy_trim(char *d
, char *s
)
1721 // skip leading whitespace
1722 while (*s
&& !isalnum(*s
)) {
1727 while (*s
&& isalnum(*s
)) {
1732 // trim excess whitespace
1733 while (*s
&& !isalnum(*s
)) {
1742 static void strcpy_strip_ext(char *d
, char *s
)
1744 char *tmp
= strrchr(s
,'.');
1749 strncpy(d
, s
, tmp
-s
);
1758 static void strcpy_get_ext(char *d
, char *s
)
1760 char *tmp
= strrchr(s
,'.');
1769 static int whiteonly(char *s
)
1772 if (isalnum(*s
)) return 0;
1778 typedef struct _subfn
1784 static int compare_sub_priority(const void *a
, const void *b
)
1786 if (((subfn
*)a
)->priority
> ((subfn
*)b
)->priority
) {
1788 } else if (((subfn
*)a
)->priority
< ((subfn
*)b
)->priority
) {
1791 return strcoll(((subfn
*)a
)->fname
, ((subfn
*)b
)->fname
);
1795 char** sub_filenames(char* path
, char *fname
)
1797 char *f_dir
, *f_fname
, *f_fname_noext
, *f_fname_trim
, *tmp
, *tmp_sub_id
;
1798 char *tmp_fname_noext
, *tmp_fname_trim
, *tmp_fname_ext
, *tmpresult
;
1800 int len
, pos
, found
, i
, j
;
1801 char * sub_exts
[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL
};
1812 len
= (strlen(fname
) > 256 ? strlen(fname
) : 256)
1813 +(strlen(path
) > 256 ? strlen(path
) : 256)+2;
1815 f_dir
= (char*)malloc(len
);
1816 f_fname
= (char*)malloc(len
);
1817 f_fname_noext
= (char*)malloc(len
);
1818 f_fname_trim
= (char*)malloc(len
);
1820 tmp_fname_noext
= (char*)malloc(len
);
1821 tmp_fname_trim
= (char*)malloc(len
);
1822 tmp_fname_ext
= (char*)malloc(len
);
1824 tmpresult
= (char*)malloc(len
);
1826 result
= (subfn
*)malloc(sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1827 memset(result
, 0, sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1831 tmp
= strrchr(fname
,'/');
1833 if(!tmp
)tmp
= strrchr(fname
,'\\');
1836 // extract filename & dirname from fname
1838 strcpy(f_fname
, tmp
+1);
1840 strncpy(f_dir
, fname
, pos
+1);
1843 strcpy(f_fname
, fname
);
1844 strcpy(f_dir
, "./");
1847 strcpy_strip_ext(f_fname_noext
, f_fname
);
1848 strcpy_trim(f_fname_trim
, f_fname_noext
);
1851 if (dvdsub_lang
&& !whiteonly(dvdsub_lang
)) {
1852 tmp_sub_id
= (char*)malloc(strlen(dvdsub_lang
)+1);
1853 strcpy_trim(tmp_sub_id
, dvdsub_lang
);
1857 // 1 = any subtitle file
1858 // 2 = any sub file containing movie name
1859 // 3 = sub file containing movie name and the lang extension
1860 for (j
= 0; j
<= 1; j
++) {
1861 d
= opendir(j
== 0 ? f_dir
: path
);
1863 while ((de
= readdir(d
))) {
1864 // retrieve various parts of the filename
1865 strcpy_strip_ext(tmp_fname_noext
, de
->d_name
);
1866 strcpy_get_ext(tmp_fname_ext
, de
->d_name
);
1867 strcpy_trim(tmp_fname_trim
, tmp_fname_noext
);
1869 // does it end with a subtitle extension?
1873 for (i
= ((sub_cp
&& strncasecmp(sub_cp
, "enca", 4) != 0) ? 3 : 0); sub_exts
[i
]; i
++) {
1875 for (i
= (sub_cp
? 3 : 0); sub_exts
[i
]; i
++) {
1878 for (i
= 0; sub_exts
[i
]; i
++) {
1880 if (strcmp(sub_exts
[i
], tmp_fname_ext
) == 0) {
1886 // we have a (likely) subtitle file
1889 if (!prio
&& tmp_sub_id
)
1891 sprintf(tmpresult
, "%s %s", f_fname_trim
, tmp_sub_id
);
1892 printf("dvdsublang...%s\n", tmpresult
);
1893 if (strcmp(tmp_fname_trim
, tmpresult
) == 0 && sub_match_fuzziness
>= 1) {
1894 // matches the movie name + lang extension
1898 if (!prio
&& strcmp(tmp_fname_trim
, f_fname_trim
) == 0) {
1899 // matches the movie name
1902 if (!prio
&& (tmp
= strstr(tmp_fname_trim
, f_fname_trim
)) && (sub_match_fuzziness
>= 1)) {
1903 // contains the movie name
1904 tmp
+= strlen(f_fname_trim
);
1905 if (tmp_sub_id
&& strstr(tmp
, tmp_sub_id
)) {
1906 // with sub_id specified prefer localized subtitles
1908 } else if ((tmp_sub_id
== NULL
) && whiteonly(tmp
)) {
1909 // without sub_id prefer "plain" name
1912 // with no localized subs found, try any else instead
1917 // doesn't contain the movie name
1918 // don't try in the mplayer subtitle directory
1919 if ((j
== 0) && (sub_match_fuzziness
>= 2)) {
1927 if (i
<3){ // prefer UTF-8 coded
1931 sprintf(tmpresult
, "%s%s", j
== 0 ? f_dir
: path
, de
->d_name
);
1932 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1933 if ((f
= fopen(tmpresult
, "rt"))) {
1935 result
[subcnt
].priority
= prio
;
1936 result
[subcnt
].fname
= strdup(tmpresult
);
1942 if (subcnt
>= MAX_SUBTITLE_FILES
) break;
1949 if (tmp_sub_id
) free(tmp_sub_id
);
1953 free(f_fname_noext
);
1956 free(tmp_fname_noext
);
1957 free(tmp_fname_trim
);
1958 free(tmp_fname_ext
);
1962 qsort(result
, subcnt
, sizeof(subfn
), compare_sub_priority
);
1964 result2
= (char**)malloc(sizeof(char*)*(subcnt
+1));
1965 memset(result2
, 0, sizeof(char*)*(subcnt
+1));
1967 for (i
= 0; i
< subcnt
; i
++) {
1968 result2
[i
] = result
[i
].fname
;
1970 result2
[subcnt
] = NULL
;
1977 void list_sub_file(sub_data
* subd
){
1979 subtitle
*subs
= subd
->subtitles
;
1981 for(j
=0; j
< subd
->sub_num
; j
++){
1982 subtitle
* egysub
=&subs
[j
];
1983 printf ("%i line%c (%li-%li)\n",
1985 (1==egysub
->lines
)?' ':'s',
1988 for (i
=0; i
<egysub
->lines
; i
++) {
1989 printf ("\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
1994 printf ("Subtitle format %s time.\n",
1995 subd
->sub_uses_time
? "uses":"doesn't use");
1996 printf ("Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
1999 void dump_srt(sub_data
* subd
, float fps
){
2005 subtitle
*subs
= subd
->subtitles
;
2007 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2009 fd
=fopen("dumpsub.srt","w");
2012 perror("dump_srt: fopen");
2015 for(i
=0; i
< subd
->sub_num
; i
++)
2017 onesub
=subs
+i
; //=&subs[i];
2018 fprintf(fd
,"%d\n",i
+1);//line number
2021 if (!subd
->sub_uses_time
)
2022 temp
= temp
* 100 / sub_fps
;
2023 temp
-= sub_delay
* 100;
2024 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2025 m
=temp
/6000; temp
%=6000; //m =1*100*60
2026 s
=temp
/100; temp
%=100; //s =1*100
2027 ms
=temp
*10; //ms=1*10
2028 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
2031 if (!subd
->sub_uses_time
)
2032 temp
= temp
* 100 / sub_fps
;
2033 temp
-= sub_delay
* 100;
2034 h
=temp
/360000;temp
%=360000;
2035 m
=temp
/6000; temp
%=6000;
2036 s
=temp
/100; temp
%=100;
2038 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
2040 for(j
=0;j
<onesub
->lines
;j
++)
2041 fprintf(fd
,"%s\n",onesub
->text
[j
]);
2046 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
2049 void dump_mpsub(sub_data
* subd
, float fps
){
2053 subtitle
*subs
= subd
->subtitles
;
2055 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
2056 if (sub_fps
==0) sub_fps
=fps
;
2058 fd
=fopen ("dump.mpsub", "w");
2060 perror ("dump_mpsub: fopen");
2065 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
2066 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
2068 for(j
=0; j
< subd
->sub_num
; j
++){
2069 subtitle
* egysub
=&subs
[j
];
2070 if (subd
->sub_uses_time
) {
2071 a
=((egysub
->start
-mpsub_position
)/100.0);
2072 b
=((egysub
->end
-egysub
->start
)/100.0);
2073 if ( (float)((int)a
) == a
)
2074 fprintf (fd
, "%.0f",a
);
2076 fprintf (fd
, "%.2f",a
);
2078 if ( (float)((int)b
) == b
)
2079 fprintf (fd
, " %.0f\n",b
);
2081 fprintf (fd
, " %.2f\n",b
);
2083 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
2084 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
2087 mpsub_position
= egysub
->end
;
2088 for (i
=0; i
<egysub
->lines
; i
++) {
2089 fprintf (fd
, "%s\n",egysub
->text
[i
]);
2094 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
2097 void dump_microdvd(sub_data
* subd
, float fps
) {
2100 subtitle
*subs
= subd
->subtitles
;
2103 fd
= fopen("dumpsub.txt", "w");
2105 perror("dumpsub.txt: fopen");
2108 delay
= sub_delay
* sub_fps
;
2109 for (i
= 0; i
< subd
->sub_num
; ++i
) {
2111 start
= subs
[i
].start
;
2113 if (subd
->sub_uses_time
) {
2114 start
= start
* sub_fps
/ 100 ;
2115 end
= end
* sub_fps
/ 100;
2118 start
= start
* sub_fps
/ fps
;
2119 end
= end
* sub_fps
/ fps
;
2123 fprintf(fd
, "{%d}{%d}", start
, end
);
2124 for (j
= 0; j
< subs
[i
].lines
; ++j
)
2125 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
2129 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
2132 void dump_jacosub(sub_data
* subd
, float fps
) {
2138 subtitle
*subs
= subd
->subtitles
;
2140 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2142 fd
=fopen("dumpsub.jss","w");
2145 perror("dump_jacosub: fopen");
2148 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
2149 for(i
=0; i
< subd
->sub_num
; i
++)
2151 onesub
=subs
+i
; //=&subs[i];
2154 if (!subd
->sub_uses_time
)
2155 temp
= temp
* 100 / sub_fps
;
2156 temp
-= sub_delay
* 100;
2157 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2158 m
=temp
/6000; temp
%=6000; //m =1*100*60
2159 s
=temp
/100; temp
%=100; //s =1*100
2161 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
2164 if (!subd
->sub_uses_time
)
2165 temp
= temp
* 100 / sub_fps
;
2166 temp
-= sub_delay
* 100;
2167 h
=temp
/360000;temp
%=360000;
2168 m
=temp
/6000; temp
%=6000;
2169 s
=temp
/100; temp
%=100;
2171 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
2173 for(j
=0;j
<onesub
->lines
;j
++)
2174 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
2179 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2182 void dump_sami(sub_data
* subd
, float fps
) {
2187 subtitle
*subs
= subd
->subtitles
;
2189 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2191 fd
=fopen("dumpsub.smi","w");
2194 perror("dump_jacosub: fopen");
2197 fprintf(fd
, "<SAMI>\n"
2199 " <STYLE TYPE=\"Text/css\">\n"
2201 " 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"
2202 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2207 for(i
=0; i
< subd
->sub_num
; i
++)
2209 onesub
=subs
+i
; //=&subs[i];
2212 if (!subd
->sub_uses_time
)
2213 temp
= temp
* 100 / sub_fps
;
2214 temp
-= sub_delay
* 100;
2215 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2216 "\t <P>", temp
* 10);
2218 for(j
=0;j
<onesub
->lines
;j
++)
2219 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2224 if (!subd
->sub_uses_time
)
2225 temp
= temp
* 100 / sub_fps
;
2226 temp
-= sub_delay
* 100;
2227 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2228 "\t <P> \n", temp
* 10);
2230 fprintf(fd
, "</BODY>\n"
2233 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2236 void sub_free( sub_data
* subd
)
2240 if ( !subd
) return;
2242 if (subd
->subtitles
) {
2243 for (i
=0; i
< subd
->subtitles
->lines
; i
++) free( subd
->subtitles
->text
[i
] );
2244 free( subd
->subtitles
);
2246 if (subd
->filename
) free( subd
->filename
);
2251 int main(int argc
, char **argv
) { // for testing
2255 printf("\nUsage: subreader filename.sub\n\n");
2259 subd
= sub_read_file(argv
[1]);
2261 printf("Couldn't load file.\n");
2265 list_sub_file(subd
);