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
; ///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
39 extern char* dvdsub_lang
;
41 /* Maximal length of line of a subtitle */
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
;
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
;
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
) {
76 while (isspace(s
[i
])) ++i
;
77 if (i
) strcpy(s
, s
+ i
);
79 while (i
> 0 && isspace(s
[i
])) s
[i
--] = '\0';
82 static char *stristr(const char *haystack
, const char *needle
) {
84 const char *p
= haystack
;
86 if (!(haystack
&& needle
)) return NULL
;
90 if (strncasecmp(p
, needle
, len
) == 0) return (char*)p
;
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
;
103 current
->lines
= current
->start
= current
->end
= 0;
104 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
107 /* read the first line */
109 if (!(s
= fgets(line
, LINE_LEN
, fd
))) return 0;
114 case 0: /* find "START=" or "Slacktime:" */
115 slacktime_s
= stristr (s
, "Slacktime:");
117 sub_slacktime
= strtol (slacktime_s
+10, NULL
, 0) / 10;
119 s
= stristr (s
, "Start=");
121 current
->start
= strtol (s
+ 6, &s
, 0) / 10;
123 for (; *s
!= '>' && *s
!= '\0'; s
++);
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 */
134 if (*s
== 'P' || *s
== 'p') { s
++; state
= 2; continue; } /* found '<P' */
135 for (; *s
!= '>' && *s
!= '\0'; s
++); /* skip remains of non-<P> TAG */
141 case 2: /* find ">" */
142 if ((s
= strchr (s
, '>'))) { s
++; state
= 3; p
= text
; continue; }
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
);
150 current
->text
[current
->lines
++] = strdup (text
);
153 else if ((*s
== '{') && !sub_no_text_pp
) { state
= 5; ++s
; continue; }
154 else if (*s
== '<') { state
= 4; }
155 else if (!strncasecmp (s
, " ", 6)) { *p
++ = ' '; s
+= 6; }
156 else if (*s
== '\t') { *p
++ = ' '; s
++; }
157 else if (*s
== '\r' || *s
== '\n') { s
++; }
160 /* skip duplicated space */
161 if (p
> text
+ 2) if (*(p
-1) == ' ' && *(p
-2) == ' ') p
--;
165 case 4: /* get current->end or skip <TAG> */
166 q
= stristr (s
, "Start=");
168 current
->end
= strtol (q
+ 6, &q
, 0) / 10 - 1;
169 *p
= '\0'; trail_space (text
);
171 current
->text
[current
->lines
++] = strdup (text
);
172 if (current
->lines
> 0) { state
= 99; break; }
176 if (s
) { s
++; state
= 3; continue; }
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
;
184 if (stristr(s
, "\\a2") != NULL
) {
185 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
187 } else if (stristr(s
, "\\a3") != NULL
) {
188 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
190 } else if ((stristr(s
, "\\a4") != NULL
) || (stristr(s
, "\\a5") != NULL
) || (stristr(s
, "\\a8") != NULL
)) {
191 current
->alignment
= SUB_ALIGNMENT_TOPLEFT
;
193 } else if (stristr(s
, "\\a6") != NULL
) {
194 current
->alignment
= SUB_ALIGNMENT_TOPCENTER
;
196 } else if (stristr(s
, "\\a7") != NULL
) {
197 current
->alignment
= SUB_ALIGNMENT_TOPRIGHT
;
199 } else if (stristr(s
, "\\a9") != NULL
) {
200 current
->alignment
= SUB_ALIGNMENT_MIDDLELEFT
;
202 } else if (stristr(s
, "\\a10") != NULL
) {
203 current
->alignment
= SUB_ALIGNMENT_MIDDLECENTER
;
205 } else if (stristr(s
, "\\a11") != NULL
) {
206 current
->alignment
= SUB_ALIGNMENT_MIDDLERIGHT
;
210 if (*s
== '}') state
= 3;
216 if (state
!= 99 && !(s
= fgets (line
, LINE_LEN
, fd
))) {
217 if (current
->start
> 0) {
218 break; // if it is the last subtitle
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
);
231 current
->text
[current
->lines
++] = strdup (text
);
238 char *sub_readtext(char *source
, char **dest
) {
242 // printf("src=%p dest=%p \n",source,dest);
244 while ( !eol(*p
) && *p
!= '|' ) {
248 *dest
= (char *)malloc (len
+1);
249 if (!dest
) {return ERR
;}
251 strncpy(*dest
, source
, len
);
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];
267 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
268 } while ((sscanf (line
,
270 &(current
->start
), line2
) < 2) &&
272 "{%ld}{%ld}%[^\r\n]",
273 &(current
->start
), &(current
->end
), line2
) < 3));
278 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
279 if (current
->text
[i
]==ERR
) {return ERR
;}
281 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
288 subtitle
*sub_read_line_mpl2(FILE *fd
,subtitle
*current
) {
289 char line
[LINE_LEN
+1];
290 char line2
[LINE_LEN
+1];
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;
304 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
305 if (current
->text
[i
]==ERR
) {return ERR
;}
307 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;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
;
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
;
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;
337 else while (*p
++!=']');
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
;
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)
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;
359 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; p
++,len
++);
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';
366 /* let's filter html tags ::atmos */
393 subtitle
*sub_read_line_subviewer2(FILE *fd
,subtitle
*current
) {
394 char line
[LINE_LEN
+1];
399 while (!current
->text
[0]) {
400 if (!fgets (line
, LINE_LEN
, fd
)) return NULL
;
403 if ((len
=sscanf (line
, "{T %d:%d:%d:%d",&a1
,&a2
,&a3
,&a4
)) < 4)
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;
410 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; ++p
,++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';
426 subtitle
*sub_read_line_vplayer(FILE *fd
,subtitle
*current
) {
427 char line
[LINE_LEN
+1];
429 char *p
=NULL
, *next
,separator
;
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)
437 if (!(current
->start
= a1
*360000+a2
*6000+a3
*100))
441 // finds the body of the subtitle
448 printf("SUB: Skipping incorrect subtitle line!\n");
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:
461 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
462 if (current
->text
[i
]==ERR
) {return ERR
;}
464 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;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
;
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;
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)
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;
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){
511 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
512 if (current
->text
[i
]==ERR
) {return ERR
;}
514 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;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
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
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
;
540 char line
[LINE_LEN
+1],
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]", ¬hing
,
549 &hour1
, &min1
, &sec1
, &hunsec1
,
550 &hour2
, &min2
, &sec2
, &hunsec2
,
553 sscanf (line
, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
554 "%[^\n\r]", ¬hing
,
555 &hour1
, &min1
, &sec1
, &hunsec1
,
556 &hour2
, &min2
, &sec2
, &hunsec2
,
559 line2
=strchr(line3
, ',');
561 for (comma
= 4; comma
< max_comma
; comma
++)
564 if(!(tmp
=strchr(++tmp
, ','))) break;
565 if(*(++tmp
) == ' ') break;
566 /* a space after a comma means we're already in a sentence */
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';
585 if (current
->lines
>= SUB_MAX_TEXT
) return current
;
588 current
->text
[num
]=strdup(line2
);
594 void sub_pp_ssa(subtitle
*sub
) {
599 /* eliminate any text enclosed with {}, they are font and color settings */
600 so
=de
=sub
->text
[--l
];
602 if(*so
== '{' && so
[1]=='\\') {
603 for (start
=so
; *so
&& *so
!='}'; so
++);
604 if(*so
) so
++; else so
=start
;
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
))
630 for (s
=line
; *s
&& isspace(*s
); s
++);
631 /* allow empty lines at the end of the file */
635 if (sscanf (s
, "%ld,%ld,", &(current
->start
),
636 &(current
->end
)) <2) {
639 /* the files I have are in tenths of second */
640 current
->start
*= 10;
642 /* walk to the beggining of the string */
643 for (; *s
; s
++) if (*s
==',') break;
645 for (s
++; *s
; s
++) if (*s
==',') break;
651 /* copy the string to the text buffer */
652 for (s
++, d
=text
; *s
&& *s
!='"'; s
++, d
++)
655 current
->text
[0] = strdup(text
);
661 subtitle
*sub_read_line_mpsub(FILE *fd
, subtitle
*current
) {
662 char line
[LINE_LEN
+1];
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
;
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
++);
690 current
->text
[num
]=strdup(p
);
691 // printf (">%s<\n",p);
692 current
->lines
= ++num
;
694 if (num
) return current
;
698 return NULL
; // we should have returned before if it's OK
702 //we don't need this if we use previous_sub_end
703 subtitle
*previous_aqt_sub
= NULL
;
706 subtitle
*sub_read_line_aqt(FILE *fd
,subtitle
*current
) {
707 char line
[LINE_LEN
+1];
712 // try to locate next subtitle
713 if (!fgets (line
, LINE_LEN
, fd
))
715 if (!(sscanf (line
, "-->> %ld", &(current
->start
)) <1))
720 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
722 if (previous_aqt_sub
!= NULL
)
723 previous_aqt_sub
->end
= current
->start
-1;
725 previous_aqt_sub
= current
;
728 if (!fgets (line
, LINE_LEN
, fd
))
731 sub_readtext((char *) &line
,¤t
->text
[0]);
733 current
->end
= current
->start
; // will be corrected by next subtitle
735 if (!fgets (line
, LINE_LEN
, fd
))
739 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
740 if (current
->text
[i
]==ERR
) {return ERR
;}
742 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
746 if ((current
->text
[0]=="") && (current
->text
[1]=="")) {
748 previous_sub_end
= 0;
750 // void subtitle -> end of previous marked and exit
751 previous_aqt_sub
= NULL
;
760 subtitle
*previous_subrip09_sub
= NULL
;
763 subtitle
*sub_read_line_subrip09(FILE *fd
,subtitle
*current
) {
764 char line
[LINE_LEN
+1];
770 // try to locate next subtitle
771 if (!fgets (line
, LINE_LEN
, fd
))
773 if (!((len
=sscanf (line
, "[%d:%d:%d]",&a1
,&a2
,&a3
)) < 3))
777 current
->start
= a1
*360000+a2
*6000+a3
*100;
780 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
782 if (previous_subrip09_sub
!= NULL
)
783 previous_subrip09_sub
->end
= current
->start
-1;
785 previous_subrip09_sub
= current
;
788 if (!fgets (line
, LINE_LEN
, fd
))
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
;}
798 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
802 if ((current
->text
[0]=="") && (i
==0)) {
804 previous_sub_end
= 0;
806 // void subtitle -> end of previous marked and exit
807 previous_subrip09_sub
= NULL
;
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
)) {
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])) {
840 if (isalpha(line1
[2])) {
845 if (sscanf(&line1
[delta
], "%d", &hours
)) {
850 if (sscanf(&line1
[delta
], "%*d:%d", &minutes
)) {
852 (&line1
[delta
], "%*d:%*d:%d",
854 sscanf(&line1
[delta
], "%*d:%*d:%*d.%d",
858 sscanf(&line1
[delta
], "%d:%d.%d",
859 &minutes
, &seconds
, &units
);
864 sscanf(&line1
[delta
], "%d.%d", &seconds
,
869 ((hours
* 3600 + minutes
* 60 +
870 seconds
) * jacoTimeres
+
875 if (isalpha(line1
[2])) {
880 sscanf(&line1
[delta
], "%u", &jacoTimeres
);
887 (unsigned long) ((a4
+ jacoShift
) * 100.0 /
890 (unsigned long) ((b4
+ jacoShift
) * 100.0 /
896 long) (((a1
* 3600 + a2
* 60 + a3
) * jacoTimeres
+ a4
+
897 jacoShift
) * 100.0 / jacoTimeres
);
900 long) (((b1
* 3600 + b2
* 60 + b3
) * jacoTimeres
+ b4
+
901 jacoShift
) * 100.0 / jacoTimeres
);
905 while ((*p
== ' ') || (*p
== '\t')) {
908 if (isalpha(*p
)||*p
== '[') {
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
)) {
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
;
929 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
931 strcpy(line2
, line1
);
934 for (q
= line1
; (!eol(*p
)) && (current
->lines
< SUB_MAX_TEXT
); ++p
) {
942 //the next line to get rid of a blank after the comment
943 if ((*(p
+ 1)) == ' ')
955 if ((*(p
+ 1) == ' ') || (*(p
+ 1) == '\t'))
963 if (*(p
+ 1) == 'n') {
966 current
->text
[current
->lines
++] = strdup(line1
);
970 if ((toupper(*(p
+ 1)) == 'C')
971 || (toupper(*(p
+ 1)) == 'F')) {
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')) {
981 if ((*(p
+ 1) == '\\') ||
982 (*(p
+ 1) == '~') || (*(p
+ 1) == '{')) {
984 } else if (eol(*(p
+ 1))) {
985 if (!fgets(directive
, LINE_LEN
, fd
))
987 trail_space(directive
);
988 strncat(line2
, directive
,
989 (LINE_LEN
> 511) ? LINE_LEN
: 511);
1000 current
->text
[current
->lines
] = strdup(line1
);
1006 int sub_autodetect (FILE *fd
, int *uses_time
) {
1007 char line
[LINE_LEN
+1];
1013 if (!fgets (line
, LINE_LEN
, fd
))
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)
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
1067 extern int sub_utf8
;
1068 int sub_utf8_prev
=0;
1071 extern float sub_delay
;
1072 extern float sub_fps
;
1075 static iconv_t icdsc
= (iconv_t
)(-1);
1077 void subcp_open (FILE *enca_fd
)
1079 char *tocp
= "UTF-8";
1082 char *cp_tmp
= sub_cp
;
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) {
1089 cp_tmp
= guess_cp(enca_fd
, enca_lang
, enca_fallback
);
1092 cp_tmp
= enca_fallback
;
1096 if ((icdsc
= iconv_open (tocp
, cp_tmp
)) != (iconv_t
)(-1)){
1097 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
1100 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
1102 if (free_cp_tmp
&& cp_tmp
) free(cp_tmp
);
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
)
1122 size_t ileft
, oleft
;
1127 ip
= sub
->text
[--l
];
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");
1137 if (!(ot
= (char *)malloc(op
- icbuffer
+ 1))){
1138 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1143 strcpy (ot
, icbuffer
);
1144 free (sub
->text
[l
]);
1148 for (l
= sub
->lines
; l
;)
1149 free (sub
->text
[--l
]);
1156 subtitle
* subcp_recode1 (subtitle
*sub
)
1159 size_t ileft
, oleft
;
1161 if(icdsc
== (iconv_t
)(-1)) return sub
;
1164 char *ip
= icbuffer
;
1165 char *op
= sub
->text
[--l
];
1166 strlcpy(ip
, op
, ICBUFFSIZE
);
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");
1183 #define max(a,b) (((a)>(b))?(a):(b))
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
;
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");
1201 char_set_num
= fribidi_parse_charset ("UTF-8");
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");
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
,
1215 visual
, NULL
, NULL
, NULL
);
1217 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, 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");
1224 fribidi_unicode_to_charset ( char_set_num
, visual
, len
,op
);
1230 for (l
= sub
->lines
; l
;)
1231 free (sub
->text
[--l
]);
1240 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1241 int sub_num
, int sub_uses_time
) {
1245 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1246 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1250 if (sub
->end
<= sub
->start
){
1251 sub
->end
= sub
->start
+ subfms
;
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
;
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
;
1294 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Adjusted %d subtitle(s).\n", n
);
1298 subtitle
* (*read
)(FILE *fd
,subtitle
*dest
);
1299 void (*post
)(subtitle
*dest
);
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
;
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
++) {
1328 if (strcasecmp(languages
[i
], preferred_language
) != 0) continue;
1329 analyser
= enca_analyser_alloc(languages
[i
]);
1330 encoding
= enca_analyse_const(analyser
, buffer
, buflen
);
1331 tmp
= enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
);
1332 if (tmp
&& encoding
.charset
!= ENCA_CS_UNKNOWN
) {
1333 detected_sub_cp
= strdup(tmp
);
1334 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detected charset: %s\n", tmp
);
1336 enca_analyser_free(analyser
);
1343 if (!detected_sub_cp
) {
1344 detected_sub_cp
= strdup(fallback
);
1345 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detection failed: fallback to %s\n", fallback
);
1348 return detected_sub_cp
;
1352 sub_data
* sub_read_file (char *filename
, float fps
) {
1354 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1355 subtitle
*first
, *second
, *sub
, *return_sub
;
1356 sub_data
*subt_data
;
1357 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1358 struct subreader sr
[]=
1360 { sub_read_line_microdvd
, NULL
, "microdvd" },
1361 { sub_read_line_subrip
, NULL
, "subrip" },
1362 { sub_read_line_subviewer
, NULL
, "subviewer" },
1363 { sub_read_line_sami
, NULL
, "sami" },
1364 { sub_read_line_vplayer
, NULL
, "vplayer" },
1365 { sub_read_line_rt
, NULL
, "rt" },
1366 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1367 { sub_read_line_pjs
, NULL
, "pjs" },
1368 { sub_read_line_mpsub
, NULL
, "mpsub" },
1369 { sub_read_line_aqt
, NULL
, "aqt" },
1370 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1371 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1372 { sub_read_line_jacosub
, NULL
, "jacosub" },
1373 { sub_read_line_mpl2
, NULL
, "mpl2" }
1375 struct subreader
*srp
;
1377 if(filename
==NULL
) return NULL
; //qnx segfault
1378 fd
=fopen (filename
, "r"); if (!fd
) return NULL
;
1380 sub_format
=sub_autodetect (fd
, &uses_time
);
1381 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1382 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1384 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Detected subtitle file format: %s\n", srp
->name
);
1389 sub_utf8_prev
=sub_utf8
;
1393 if ((l
=strlen(filename
))>4){
1394 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1396 if (!strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1401 if (k
<0) subcp_open(fd
);
1406 first
=(subtitle
*)malloc(n_max
*sizeof(subtitle
));
1410 sub_utf8
=sub_utf8_prev
;
1416 sub
= (subtitle
*)malloc(sizeof(subtitle
));
1417 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1418 //as the beginning of the following
1419 previous_sub_end
= 0;
1424 first
=realloc(first
,n_max
*sizeof(subtitle
));
1427 sub
= &first
[sub_num
];
1429 memset(sub
, '\0', sizeof(subtitle
));
1430 sub
=srp
->read(fd
,sub
);
1431 if(!sub
) break; // EOF
1433 if ((sub
!=ERR
) && (sub_utf8
& 2)) sub
=subcp_recode(sub
);
1436 if (sub
!=ERR
) sub
=sub_fribidi(sub
,sub_utf8
);
1443 if ( first
) free(first
);
1446 // Apply any post processing that needs recoding first
1447 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1449 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1450 first
[sub_num
].start
= sub
->start
;
1451 first
[sub_num
].end
= sub
->end
;
1452 first
[sub_num
].lines
= sub
->lines
;
1453 first
[sub_num
].alignment
= sub
->alignment
;
1454 for(i
= 0; i
< sub
->lines
; ++i
){
1455 first
[sub_num
].text
[i
] = sub
->text
[i
];
1457 if (previous_sub_end
){
1458 first
[sub_num
- 1].end
= previous_sub_end
;
1459 previous_sub_end
= 0;
1462 for(j
= sub_num
- 1; j
>= 0; --j
){
1463 first
[j
+ 1].start
= first
[j
].start
;
1464 first
[j
+ 1].end
= first
[j
].end
;
1465 first
[j
+ 1].lines
= first
[j
].lines
;
1466 first
[j
+ 1].alignment
= first
[j
].alignment
;
1467 for(i
= 0; i
< first
[j
].lines
; ++i
){
1468 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1470 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1471 first
[j
].start
= sub
->start
;
1472 first
[j
].end
= sub
->end
;
1473 first
[j
].lines
= sub
->lines
;
1474 first
[j
].alignment
= sub
->alignment
;
1475 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1476 first
[j
].text
[i
] = sub
->text
[i
];
1478 if (previous_sub_end
){
1479 first
[j
].end
= first
[j
- 1].end
;
1480 first
[j
- 1].end
= previous_sub_end
;
1481 previous_sub_end
= 0;
1488 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1497 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1498 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Read %i subtitles", sub_num
);
1499 if (sub_errs
) mp_msg(MSGT_SUBREADER
,MSGL_INFO
,", %i bad line(s).\n", sub_errs
);
1500 else mp_msg(MSGT_SUBREADER
,MSGL_INFO
,".\n");
1507 // we do overlap if the user forced it (suboverlap_enable == 2) or
1508 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1509 // this is because usually overlapping subtitles are found in these formats,
1510 // while in others they are probably result of bad timing
1511 if ((suboverlap_enabled
== 2) ||
1512 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1513 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1514 // here we manage overlapping subtitles
1519 // for each subtitle in first[] we deal with its 'block' of
1521 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1522 unsigned long global_start
= first
[sub_first
].start
,
1523 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1524 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1525 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1526 char real_block
= 1;
1528 // here we find the number of subtitles inside the 'block'
1529 // and its span interval. this works well only with sorted
1531 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1533 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1534 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1535 global_start
= first
[sub_first
+ sub_to_add
].start
;
1537 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1538 global_end
= first
[sub_first
+ sub_to_add
].end
;
1542 // we need a structure to keep trace of the screen lines
1543 // used by the subs, a 'placeholder'
1544 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1545 // from a block of sub_to_add+1 subs
1546 placeholder
= (int **) malloc(sizeof(int *) * counter
);
1547 for (i
= 0; i
< counter
; ++i
) {
1548 placeholder
[i
] = (int *) malloc(sizeof(int) * lines_to_add
);
1549 for (j
= 0; j
< lines_to_add
; ++j
) {
1550 placeholder
[i
][j
] = -1;
1555 local_end
= global_start
- 1;
1559 // here we find the beginning and the end of a new
1560 // subtitle in the block
1561 local_start
= local_end
+ 1;
1562 local_end
= global_end
;
1563 for (j
= 0; j
<= sub_to_add
; ++j
) {
1564 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1565 local_end
= first
[sub_first
+ j
].start
- 1;
1566 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1567 local_end
= first
[sub_first
+ j
].end
;
1570 // here we allocate the screen lines to subs we must
1571 // display in current local_start-local_end interval.
1572 // if the subs were yet presents in the previous interval
1573 // they keep the same lines, otherside they get unused lines
1574 for (j
= 0; j
<= sub_to_add
; ++j
) {
1575 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1576 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1579 int fragment_position
= -1;
1581 // if this is not the first new sub of the block
1582 // we find if this sub was present in the previous
1585 for (i
= 0; i
< lines_to_add
; ++i
) {
1586 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1587 placeholder
[counter
][i
] = sub_first
+ j
;
1594 // we are looking for the shortest among all groups of
1595 // sequential blank lines whose length is greater than or
1596 // equal to sub_lines. we store in fragment_position the
1597 // position of the shortest group, in fragment_length its
1598 // length, and in tmp the length of the group currently
1600 for (i
= 0; i
< lines_to_add
; ++i
) {
1601 if (placeholder
[counter
][i
] == -1) {
1602 // placeholder[counter][i] is part of the current group
1606 if (tmp
== sub_lines
) {
1607 // current group's size fits exactly the one we
1608 // need, so we stop looking
1609 fragment_position
= i
- tmp
;
1613 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1614 // current group is the best we found till here,
1615 // but is still bigger than the one we are looking
1616 // for, so we keep on looking
1617 fragment_length
= tmp
;
1618 fragment_position
= i
- tmp
;
1621 // current group doesn't fit at all, so we forget it
1627 // last screen line is blank, a group ends with it
1628 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1629 fragment_position
= i
- tmp
;
1632 if (fragment_position
== -1) {
1633 // it was not possible to find free screen line(s) for a subtitle,
1634 // usually this means a bug in the code; however we do not overlap
1635 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1636 higher_line
= SUB_MAX_TEXT
+ 1;
1639 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1640 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1645 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1646 if (placeholder
[counter
][j
] != -1)
1651 if (higher_line
>= SUB_MAX_TEXT
) {
1652 // the 'block' has too much lines, so we don't overlap the
1654 second
= (subtitle
*) realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1655 for (j
= 0; j
<= sub_to_add
; ++j
) {
1657 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1658 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1659 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1660 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1661 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1662 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1663 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1666 sub_num
+= sub_to_add
+ 1;
1667 sub_first
+= sub_to_add
;
1672 // we read the placeholder structure and create the new
1674 second
= (subtitle
*) realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1675 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1676 second
[sub_num
].start
= local_start
;
1677 second
[sub_num
].end
= local_end
;
1678 second
[sub_num
].alignment
= first
[sub_first
].alignment
;
1679 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1680 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1681 if (placeholder
[counter
][j
] != -1) {
1682 int lines
= first
[placeholder
[counter
][j
]].lines
;
1683 for (ls
= 0; ls
< lines
; ++ls
) {
1684 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1688 second
[sub_num
].text
[i
++] = strdup(" ");
1693 } while (local_end
< global_end
);
1695 for (i
= 0; i
< counter
; ++i
)
1696 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1698 counter
= 2 * sub_to_add
+ 1;
1699 for (i
= 0; i
< counter
; ++i
) {
1700 free(placeholder
[i
]);
1703 sub_first
+= sub_to_add
;
1706 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1707 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1708 free(first
[j
].text
[i
]);
1713 return_sub
= second
;
1714 } else { //if(suboverlap_enabled)
1715 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1718 if (return_sub
== NULL
) return NULL
;
1719 subt_data
= (sub_data
*)malloc(sizeof(sub_data
));
1720 subt_data
->filename
= strdup(filename
);
1721 subt_data
->sub_uses_time
= uses_time
;
1722 subt_data
->sub_num
= sub_num
;
1723 subt_data
->sub_errs
= sub_errs
;
1724 subt_data
->subtitles
= return_sub
;
1729 char * strreplace( char * in
,char * what
,char * whereof
)
1734 if ( ( in
== NULL
)||( what
== NULL
)||( whereof
== NULL
)||( ( tmp
=strstr( in
,what
) ) == NULL
) ) return NULL
;
1735 for( i
=0;i
<strlen( whereof
);i
++ ) tmp
[i
]=whereof
[i
];
1736 if ( strlen( what
) > strlen( whereof
) ) tmp
[i
]=0;
1742 static void strcpy_trim(char *d
, char *s
)
1744 // skip leading whitespace
1745 while (*s
&& !isalnum(*s
)) {
1750 while (*s
&& isalnum(*s
)) {
1755 // trim excess whitespace
1756 while (*s
&& !isalnum(*s
)) {
1765 static void strcpy_strip_ext(char *d
, char *s
)
1767 char *tmp
= strrchr(s
,'.');
1772 strncpy(d
, s
, tmp
-s
);
1781 static void strcpy_get_ext(char *d
, char *s
)
1783 char *tmp
= strrchr(s
,'.');
1792 static int whiteonly(char *s
)
1795 if (isalnum(*s
)) return 0;
1801 typedef struct _subfn
1807 static int compare_sub_priority(const void *a
, const void *b
)
1809 if (((subfn
*)a
)->priority
> ((subfn
*)b
)->priority
) {
1811 } else if (((subfn
*)a
)->priority
< ((subfn
*)b
)->priority
) {
1814 return strcoll(((subfn
*)a
)->fname
, ((subfn
*)b
)->fname
);
1818 char** sub_filenames(char* path
, char *fname
)
1820 char *f_dir
, *f_fname
, *f_fname_noext
, *f_fname_trim
, *tmp
, *tmp_sub_id
;
1821 char *tmp_fname_noext
, *tmp_fname_trim
, *tmp_fname_ext
, *tmpresult
;
1823 int len
, pos
, found
, i
, j
;
1824 char * sub_exts
[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL
};
1835 len
= (strlen(fname
) > 256 ? strlen(fname
) : 256)
1836 +(strlen(path
) > 256 ? strlen(path
) : 256)+2;
1838 f_dir
= (char*)malloc(len
);
1839 f_fname
= (char*)malloc(len
);
1840 f_fname_noext
= (char*)malloc(len
);
1841 f_fname_trim
= (char*)malloc(len
);
1843 tmp_fname_noext
= (char*)malloc(len
);
1844 tmp_fname_trim
= (char*)malloc(len
);
1845 tmp_fname_ext
= (char*)malloc(len
);
1847 tmpresult
= (char*)malloc(len
);
1849 result
= (subfn
*)malloc(sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1850 memset(result
, 0, sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1854 tmp
= strrchr(fname
,'/');
1856 if(!tmp
)tmp
= strrchr(fname
,'\\');
1859 // extract filename & dirname from fname
1861 strcpy(f_fname
, tmp
+1);
1863 strncpy(f_dir
, fname
, pos
+1);
1866 strcpy(f_fname
, fname
);
1867 strcpy(f_dir
, "./");
1870 strcpy_strip_ext(f_fname_noext
, f_fname
);
1871 strcpy_trim(f_fname_trim
, f_fname_noext
);
1874 if (dvdsub_lang
&& !whiteonly(dvdsub_lang
)) {
1875 tmp_sub_id
= (char*)malloc(strlen(dvdsub_lang
)+1);
1876 strcpy_trim(tmp_sub_id
, dvdsub_lang
);
1880 // 1 = any subtitle file
1881 // 2 = any sub file containing movie name
1882 // 3 = sub file containing movie name and the lang extension
1883 for (j
= 0; j
<= 1; j
++) {
1884 d
= opendir(j
== 0 ? f_dir
: path
);
1886 while ((de
= readdir(d
))) {
1887 // retrieve various parts of the filename
1888 strcpy_strip_ext(tmp_fname_noext
, de
->d_name
);
1889 strcpy_get_ext(tmp_fname_ext
, de
->d_name
);
1890 strcpy_trim(tmp_fname_trim
, tmp_fname_noext
);
1892 // does it end with a subtitle extension?
1896 for (i
= ((sub_cp
&& strncasecmp(sub_cp
, "enca", 4) != 0) ? 3 : 0); sub_exts
[i
]; i
++) {
1898 for (i
= (sub_cp
? 3 : 0); sub_exts
[i
]; i
++) {
1901 for (i
= 0; sub_exts
[i
]; i
++) {
1903 if (strcasecmp(sub_exts
[i
], tmp_fname_ext
) == 0) {
1909 // we have a (likely) subtitle file
1912 if (!prio
&& tmp_sub_id
)
1914 sprintf(tmpresult
, "%s %s", f_fname_trim
, tmp_sub_id
);
1915 printf("dvdsublang...%s\n", tmpresult
);
1916 if (strcmp(tmp_fname_trim
, tmpresult
) == 0 && sub_match_fuzziness
>= 1) {
1917 // matches the movie name + lang extension
1921 if (!prio
&& strcmp(tmp_fname_trim
, f_fname_trim
) == 0) {
1922 // matches the movie name
1925 if (!prio
&& (tmp
= strstr(tmp_fname_trim
, f_fname_trim
)) && (sub_match_fuzziness
>= 1)) {
1926 // contains the movie name
1927 tmp
+= strlen(f_fname_trim
);
1928 if (tmp_sub_id
&& strstr(tmp
, tmp_sub_id
)) {
1929 // with sub_id specified prefer localized subtitles
1931 } else if ((tmp_sub_id
== NULL
) && whiteonly(tmp
)) {
1932 // without sub_id prefer "plain" name
1935 // with no localized subs found, try any else instead
1940 // doesn't contain the movie name
1941 // don't try in the mplayer subtitle directory
1942 if ((j
== 0) && (sub_match_fuzziness
>= 2)) {
1950 if (i
<3){ // prefer UTF-8 coded
1954 sprintf(tmpresult
, "%s%s", j
== 0 ? f_dir
: path
, de
->d_name
);
1955 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1956 if ((f
= fopen(tmpresult
, "rt"))) {
1958 result
[subcnt
].priority
= prio
;
1959 result
[subcnt
].fname
= strdup(tmpresult
);
1965 if (subcnt
>= MAX_SUBTITLE_FILES
) break;
1972 if (tmp_sub_id
) free(tmp_sub_id
);
1976 free(f_fname_noext
);
1979 free(tmp_fname_noext
);
1980 free(tmp_fname_trim
);
1981 free(tmp_fname_ext
);
1985 qsort(result
, subcnt
, sizeof(subfn
), compare_sub_priority
);
1987 result2
= (char**)malloc(sizeof(char*)*(subcnt
+1));
1988 memset(result2
, 0, sizeof(char*)*(subcnt
+1));
1990 for (i
= 0; i
< subcnt
; i
++) {
1991 result2
[i
] = result
[i
].fname
;
1993 result2
[subcnt
] = NULL
;
2000 void list_sub_file(sub_data
* subd
){
2002 subtitle
*subs
= subd
->subtitles
;
2004 for(j
=0; j
< subd
->sub_num
; j
++){
2005 subtitle
* egysub
=&subs
[j
];
2006 printf ("%i line%c (%li-%li)\n",
2008 (1==egysub
->lines
)?' ':'s',
2011 for (i
=0; i
<egysub
->lines
; i
++) {
2012 printf ("\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
2017 printf ("Subtitle format %s time.\n",
2018 subd
->sub_uses_time
? "uses":"doesn't use");
2019 printf ("Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
2022 void dump_srt(sub_data
* subd
, float fps
){
2028 subtitle
*subs
= subd
->subtitles
;
2030 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2032 fd
=fopen("dumpsub.srt","w");
2035 perror("dump_srt: fopen");
2038 for(i
=0; i
< subd
->sub_num
; i
++)
2040 onesub
=subs
+i
; //=&subs[i];
2041 fprintf(fd
,"%d\n",i
+1);//line number
2044 if (!subd
->sub_uses_time
)
2045 temp
= temp
* 100 / sub_fps
;
2046 temp
-= sub_delay
* 100;
2047 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2048 m
=temp
/6000; temp
%=6000; //m =1*100*60
2049 s
=temp
/100; temp
%=100; //s =1*100
2050 ms
=temp
*10; //ms=1*10
2051 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
2054 if (!subd
->sub_uses_time
)
2055 temp
= temp
* 100 / sub_fps
;
2056 temp
-= sub_delay
* 100;
2057 h
=temp
/360000;temp
%=360000;
2058 m
=temp
/6000; temp
%=6000;
2059 s
=temp
/100; temp
%=100;
2061 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
2063 for(j
=0;j
<onesub
->lines
;j
++)
2064 fprintf(fd
,"%s\n",onesub
->text
[j
]);
2069 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
2072 void dump_mpsub(sub_data
* subd
, float fps
){
2076 subtitle
*subs
= subd
->subtitles
;
2078 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
2079 if (sub_fps
==0) sub_fps
=fps
;
2081 fd
=fopen ("dump.mpsub", "w");
2083 perror ("dump_mpsub: fopen");
2088 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
2089 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
2091 for(j
=0; j
< subd
->sub_num
; j
++){
2092 subtitle
* egysub
=&subs
[j
];
2093 if (subd
->sub_uses_time
) {
2094 a
=((egysub
->start
-mpsub_position
)/100.0);
2095 b
=((egysub
->end
-egysub
->start
)/100.0);
2096 if ( (float)((int)a
) == a
)
2097 fprintf (fd
, "%.0f",a
);
2099 fprintf (fd
, "%.2f",a
);
2101 if ( (float)((int)b
) == b
)
2102 fprintf (fd
, " %.0f\n",b
);
2104 fprintf (fd
, " %.2f\n",b
);
2106 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
2107 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
2110 mpsub_position
= egysub
->end
;
2111 for (i
=0; i
<egysub
->lines
; i
++) {
2112 fprintf (fd
, "%s\n",egysub
->text
[i
]);
2117 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
2120 void dump_microdvd(sub_data
* subd
, float fps
) {
2123 subtitle
*subs
= subd
->subtitles
;
2126 fd
= fopen("dumpsub.txt", "w");
2128 perror("dumpsub.txt: fopen");
2131 delay
= sub_delay
* sub_fps
;
2132 for (i
= 0; i
< subd
->sub_num
; ++i
) {
2134 start
= subs
[i
].start
;
2136 if (subd
->sub_uses_time
) {
2137 start
= start
* sub_fps
/ 100 ;
2138 end
= end
* sub_fps
/ 100;
2141 start
= start
* sub_fps
/ fps
;
2142 end
= end
* sub_fps
/ fps
;
2146 fprintf(fd
, "{%d}{%d}", start
, end
);
2147 for (j
= 0; j
< subs
[i
].lines
; ++j
)
2148 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
2152 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
2155 void dump_jacosub(sub_data
* subd
, float fps
) {
2161 subtitle
*subs
= subd
->subtitles
;
2163 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2165 fd
=fopen("dumpsub.jss","w");
2168 perror("dump_jacosub: fopen");
2171 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
2172 for(i
=0; i
< subd
->sub_num
; i
++)
2174 onesub
=subs
+i
; //=&subs[i];
2177 if (!subd
->sub_uses_time
)
2178 temp
= temp
* 100 / sub_fps
;
2179 temp
-= sub_delay
* 100;
2180 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2181 m
=temp
/6000; temp
%=6000; //m =1*100*60
2182 s
=temp
/100; temp
%=100; //s =1*100
2184 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
2187 if (!subd
->sub_uses_time
)
2188 temp
= temp
* 100 / sub_fps
;
2189 temp
-= sub_delay
* 100;
2190 h
=temp
/360000;temp
%=360000;
2191 m
=temp
/6000; temp
%=6000;
2192 s
=temp
/100; temp
%=100;
2194 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
2196 for(j
=0;j
<onesub
->lines
;j
++)
2197 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
2202 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2205 void dump_sami(sub_data
* subd
, float fps
) {
2210 subtitle
*subs
= subd
->subtitles
;
2212 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2214 fd
=fopen("dumpsub.smi","w");
2217 perror("dump_jacosub: fopen");
2220 fprintf(fd
, "<SAMI>\n"
2222 " <STYLE TYPE=\"Text/css\">\n"
2224 " 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"
2225 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2230 for(i
=0; i
< subd
->sub_num
; i
++)
2232 onesub
=subs
+i
; //=&subs[i];
2235 if (!subd
->sub_uses_time
)
2236 temp
= temp
* 100 / sub_fps
;
2237 temp
-= sub_delay
* 100;
2238 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2239 "\t <P>", temp
* 10);
2241 for(j
=0;j
<onesub
->lines
;j
++)
2242 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2247 if (!subd
->sub_uses_time
)
2248 temp
= temp
* 100 / sub_fps
;
2249 temp
-= sub_delay
* 100;
2250 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2251 "\t <P> \n", temp
* 10);
2253 fprintf(fd
, "</BODY>\n"
2256 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2259 void sub_free( sub_data
* subd
)
2263 if ( !subd
) return;
2265 if (subd
->subtitles
) {
2266 for (i
=0; i
< subd
->subtitles
->lines
; i
++) free( subd
->subtitles
->text
[i
] );
2267 free( subd
->subtitles
);
2269 if (subd
->filename
) free( subd
->filename
);
2274 int main(int argc
, char **argv
) { // for testing
2278 printf("\nUsage: subreader filename.sub\n\n");
2282 subd
= sub_read_file(argv
[1]);
2284 printf("Couldn't load file.\n");
2288 list_sub_file(subd
);