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"
21 #include "stream/stream.h"
22 #include "libavutil/common.h"
23 #include "libavutil/avstring.h"
29 #define ERR ((void *) -1)
36 #include <fribidi/fribidi.h>
37 char *fribidi_charset
= NULL
; ///character set that will be passed to FriBiDi
38 int flip_hebrew
= 1; ///flip subtitles using fribidi
39 int fribidi_flip_commas
= 0; ///flip comma when fribidi is used
42 extern char* dvdsub_lang
;
44 /* Maximal length of line of a subtitle */
46 static float mpsub_position
=0;
47 static float mpsub_multiplier
=1.;
48 static int sub_slacktime
= 20000; //20 sec
50 int sub_no_text_pp
=0; // 1 => do not apply text post-processing
51 // like {\...} elimination in SSA format.
53 int sub_match_fuzziness
=0; // level of sub name matching fuzziness
55 /* Use the SUB_* constant defined in the header file */
56 int sub_format
=SUB_INVALID
;
59 Some subtitling formats, namely AQT and Subrip09, define the end of a
60 subtitle as the beginning of the following. Since currently we read one
61 subtitle at time, for these format we keep two global *subtitle,
62 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
63 so we can change its end when we read current subtitle starting time.
64 When CONFIG_SORTSUB is defined, we use a single global unsigned long,
65 previous_sub_end, for both (and even future) formats, to store the end of
66 the previous sub: it is initialized to 0 in sub_read_file and eventually
67 modified by sub_read_aqt_line or sub_read_subrip09_line.
69 unsigned long previous_sub_end
;
72 static int eol(char p
) {
73 return p
=='\r' || p
=='\n' || p
=='\0';
76 /* Remove leading and trailing space */
77 static void trail_space(char *s
) {
79 while (isspace(s
[i
])) ++i
;
80 if (i
) strcpy(s
, s
+ i
);
82 while (i
> 0 && isspace(s
[i
])) s
[i
--] = '\0';
85 static char *stristr(const char *haystack
, const char *needle
) {
87 const char *p
= haystack
;
89 if (!(haystack
&& needle
)) return NULL
;
93 if (strncasecmp(p
, needle
, len
) == 0) return (char*)p
;
100 static subtitle
*sub_read_line_sami(stream_t
* st
, subtitle
*current
) {
101 static char line
[LINE_LEN
+1];
102 static char *s
= NULL
, *slacktime_s
;
103 char text
[LINE_LEN
+1], *p
=NULL
, *q
;
106 current
->lines
= current
->start
= current
->end
= 0;
107 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
110 /* read the first line */
112 if (!(s
= stream_read_line(st
, line
, LINE_LEN
))) return 0;
117 case 0: /* find "START=" or "Slacktime:" */
118 slacktime_s
= stristr (s
, "Slacktime:");
120 sub_slacktime
= strtol (slacktime_s
+10, NULL
, 0) / 10;
122 s
= stristr (s
, "Start=");
124 current
->start
= strtol (s
+ 6, &s
, 0) / 10;
126 for (; *s
!= '>' && *s
!= '\0'; s
++);
132 case 1: /* find (optionnal) "<P", skip other TAGs */
133 for (; *s
== ' ' || *s
== '\t'; s
++); /* strip blanks, if any */
134 if (*s
== '\0') break;
135 if (*s
!= '<') { state
= 3; p
= text
; continue; } /* not a TAG */
137 if (*s
== 'P' || *s
== 'p') { s
++; state
= 2; continue; } /* found '<P' */
138 for (; *s
!= '>' && *s
!= '\0'; s
++); /* skip remains of non-<P> TAG */
144 case 2: /* find ">" */
145 if ((s
= strchr (s
, '>'))) { s
++; state
= 3; p
= text
; continue; }
148 case 3: /* get all text until '<' appears */
149 if (*s
== '\0') break;
150 else if (!strncasecmp (s
, "<br>", 4)) {
151 *p
= '\0'; p
= text
; trail_space (text
);
153 current
->text
[current
->lines
++] = strdup (text
);
156 else if ((*s
== '{') && !sub_no_text_pp
) { state
= 5; ++s
; continue; }
157 else if (*s
== '<') { state
= 4; }
158 else if (!strncasecmp (s
, " ", 6)) { *p
++ = ' '; s
+= 6; }
159 else if (*s
== '\t') { *p
++ = ' '; s
++; }
160 else if (*s
== '\r' || *s
== '\n') { s
++; }
163 /* skip duplicated space */
164 if (p
> text
+ 2) if (*(p
-1) == ' ' && *(p
-2) == ' ') p
--;
168 case 4: /* get current->end or skip <TAG> */
169 q
= stristr (s
, "Start=");
171 current
->end
= strtol (q
+ 6, &q
, 0) / 10 - 1;
172 *p
= '\0'; trail_space (text
);
174 current
->text
[current
->lines
++] = strdup (text
);
175 if (current
->lines
> 0) { state
= 99; break; }
179 if (s
) { s
++; state
= 3; continue; }
181 case 5: /* get rid of {...} text, but read the alignment code */
182 if ((*s
== '\\') && (*(s
+ 1) == 'a') && !sub_no_text_pp
) {
183 if (stristr(s
, "\\a1") != NULL
) {
184 current
->alignment
= SUB_ALIGNMENT_BOTTOMLEFT
;
187 if (stristr(s
, "\\a2") != NULL
) {
188 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
190 } else if (stristr(s
, "\\a3") != NULL
) {
191 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
193 } else if ((stristr(s
, "\\a4") != NULL
) || (stristr(s
, "\\a5") != NULL
) || (stristr(s
, "\\a8") != NULL
)) {
194 current
->alignment
= SUB_ALIGNMENT_TOPLEFT
;
196 } else if (stristr(s
, "\\a6") != NULL
) {
197 current
->alignment
= SUB_ALIGNMENT_TOPCENTER
;
199 } else if (stristr(s
, "\\a7") != NULL
) {
200 current
->alignment
= SUB_ALIGNMENT_TOPRIGHT
;
202 } else if (stristr(s
, "\\a9") != NULL
) {
203 current
->alignment
= SUB_ALIGNMENT_MIDDLELEFT
;
205 } else if (stristr(s
, "\\a10") != NULL
) {
206 current
->alignment
= SUB_ALIGNMENT_MIDDLECENTER
;
208 } else if (stristr(s
, "\\a11") != NULL
) {
209 current
->alignment
= SUB_ALIGNMENT_MIDDLERIGHT
;
213 if (*s
== '}') state
= 3;
219 if (state
!= 99 && !(s
= stream_read_line (st
, line
, LINE_LEN
))) {
220 if (current
->start
> 0) {
221 break; // if it is the last subtitle
227 } while (state
!= 99);
229 // For the last subtitle
230 if (current
->end
<= 0) {
231 current
->end
= current
->start
+ sub_slacktime
;
232 *p
= '\0'; trail_space (text
);
234 current
->text
[current
->lines
++] = strdup (text
);
241 static char *sub_readtext(char *source
, char **dest
) {
245 // printf("src=%p dest=%p \n",source,dest);
247 while ( !eol(*p
) && *p
!= '|' ) {
251 *dest
= malloc (len
+1);
252 if (!dest
) {return ERR
;}
254 strncpy(*dest
, source
, len
);
257 while (*p
=='\r' || *p
=='\n' || *p
=='|') p
++;
259 if (*p
) return p
; // not-last text field
260 else return NULL
; // last text field
263 static subtitle
*sub_read_line_microdvd(stream_t
*st
,subtitle
*current
) {
264 char line
[LINE_LEN
+1];
265 char line2
[LINE_LEN
+1];
270 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
271 } while ((sscanf (line
,
273 &(current
->start
), line2
) < 2) &&
275 "{%ld}{%ld}%[^\r\n]",
276 &(current
->start
), &(current
->end
), line2
) < 3));
281 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
282 if (current
->text
[i
]==ERR
) {return ERR
;}
284 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
291 static subtitle
*sub_read_line_mpl2(stream_t
*st
,subtitle
*current
) {
292 char line
[LINE_LEN
+1];
293 char line2
[LINE_LEN
+1];
298 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
299 } while ((sscanf (line
,
300 "[%ld][%ld]%[^\r\n]",
301 &(current
->start
), &(current
->end
), line2
) < 3));
302 current
->start
*= 10;
307 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
308 if (current
->text
[i
]==ERR
) {return ERR
;}
310 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
317 static subtitle
*sub_read_line_subrip(stream_t
* st
, subtitle
*current
) {
318 char line
[LINE_LEN
+1];
319 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
320 char *p
=NULL
, *q
=NULL
;
324 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
325 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d",&a1
,&a2
,&a3
,&a4
,&b1
,&b2
,&b3
,&b4
) < 8) continue;
326 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
;
327 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
;
329 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
332 for (current
->lines
=1; current
->lines
< SUB_MAX_TEXT
; current
->lines
++) {
333 for (q
=p
,len
=0; *p
&& *p
!='\r' && *p
!='\n' && *p
!='|' && strncmp(p
,"[br]",4); p
++,len
++);
334 current
->text
[current
->lines
-1]=malloc (len
+1);
335 if (!current
->text
[current
->lines
-1]) return ERR
;
336 strncpy (current
->text
[current
->lines
-1], q
, len
);
337 current
->text
[current
->lines
-1][len
]='\0';
338 if (!*p
|| *p
=='\r' || *p
=='\n') break;
340 else while (*p
++!=']');
347 static subtitle
*sub_read_line_subviewer(stream_t
*st
,subtitle
*current
) {
348 char line
[LINE_LEN
+1];
349 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
353 while (!current
->text
[0]) {
354 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
355 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)
357 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
358 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
359 for (i
=0; i
<SUB_MAX_TEXT
;) {
361 if (!stream_read_line (st
, line
, LINE_LEN
)) break;
363 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; p
++,len
++)
364 if (*p
!= ' ' && *p
!= '\t')
368 char *curptr
=current
->text
[i
]=malloc (len
+1);
369 if (!current
->text
[i
]) return ERR
;
370 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
372 /* let's filter html tags ::atmos */
399 static subtitle
*sub_read_line_subviewer2(stream_t
*st
,subtitle
*current
) {
400 char line
[LINE_LEN
+1];
405 while (!current
->text
[0]) {
406 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
409 if ((len
=sscanf (line
, "{T %d:%d:%d:%d",&a1
,&a2
,&a3
,&a4
)) < 4)
411 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
412 for (i
=0; i
<SUB_MAX_TEXT
;) {
413 if (!stream_read_line (st
, line
, LINE_LEN
)) break;
414 if (line
[0]=='}') break;
416 for (p
=line
; *p
!='\n' && *p
!='\r' && *p
; ++p
,++len
);
418 current
->text
[i
]=malloc (len
+1);
419 if (!current
->text
[i
]) return ERR
;
420 strncpy (current
->text
[i
], line
, len
); current
->text
[i
][len
]='\0';
432 static subtitle
*sub_read_line_vplayer(stream_t
*st
,subtitle
*current
) {
433 char line
[LINE_LEN
+1];
435 char *p
=NULL
, *next
,separator
;
438 while (!current
->text
[0]) {
439 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
440 if ((len
=sscanf (line
, "%d:%d:%d%c%n",&a1
,&a2
,&a3
,&separator
,&plen
)) < 4)
443 if (!(current
->start
= a1
*360000+a2
*6000+a3
*100))
447 // finds the body of the subtitle
454 printf("SUB: Skipping incorrect subtitle line!\n");
458 // by wodzu: hey! this time we know what length it has! what is
459 // that magic for? it can't deal with space instead of third
460 // colon! look, what simple it can be:
467 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
468 if (current
->text
[i
]==ERR
) {return ERR
;}
470 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
478 static subtitle
*sub_read_line_rt(stream_t
*st
,subtitle
*current
) {
479 //TODO: This format uses quite rich (sub/super)set of xhtml
480 // I couldn't check it since DTD is not included.
481 // WARNING: full XML parses can be required for proper parsing
482 char line
[LINE_LEN
+1];
483 int a1
,a2
,a3
,a4
,b1
,b2
,b3
,b4
;
484 char *p
=NULL
,*next
=NULL
;
487 while (!current
->text
[0]) {
488 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
489 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
490 //to describe the same moment in time. Maybe there are even more formats in use.
491 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
492 plen
=a1
=a2
=a3
=a4
=b1
=b2
=b3
=b4
=0;
494 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b3
,&b4
,&plen
)) < 4) &&
495 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
496 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&plen
)) < 4) &&
497 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&b2
,&b3
,&b4
,&plen
)) < 5) &&
498 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
499 ((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) &&
500 ((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) &&
501 //now try it without end time
502 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3
,&a4
,&plen
)) < 2) &&
503 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2
,&a3
,&plen
)) < 2) &&
504 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2
,&a3
,&a4
,&plen
)) < 3) &&
505 ((len
=sscanf (line
, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1
,&a2
,&a3
,&a4
,&plen
)) < 4)
508 current
->start
= a1
*360000+a2
*6000+a3
*100+a4
/10;
509 current
->end
= b1
*360000+b2
*6000+b3
*100+b4
/10;
510 if (b1
== 0 && b2
== 0 && b3
== 0 && b4
== 0)
511 current
->end
= current
->start
+200;
513 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
514 next
= strstr(line
,"<clear/>");
515 if(next
&& strlen(next
)>8){
517 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
518 if (current
->text
[i
]==ERR
) {return ERR
;}
520 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
528 static subtitle
*sub_read_line_ssa(stream_t
*st
,subtitle
*current
) {
530 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
531 * other Sub Station Alpha scripts have only 8 commas before subtitle
532 * Reading the "ScriptType:" field is not reliable since many scripts appear
535 * http://www.scriptclub.org is a good place to find more examples
536 * http://www.eswat.demon.co.uk is where the SSA specs can be found
539 static int max_comma
= 32; /* let's use 32 for the case that the */
540 /* amount of commas increase with newer SSA versions */
542 int hour1
, min1
, sec1
, hunsec1
,
543 hour2
, min2
, sec2
, hunsec2
, nothing
;
546 char line
[LINE_LEN
+1],
552 if (!stream_read_line (st
, line
, LINE_LEN
)) return NULL
;
553 } while (sscanf (line
, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
554 "%[^\n\r]", ¬hing
,
555 &hour1
, &min1
, &sec1
, &hunsec1
,
556 &hour2
, &min2
, &sec2
, &hunsec2
,
559 sscanf (line
, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
560 "%[^\n\r]", ¬hing
,
561 &hour1
, &min1
, &sec1
, &hunsec1
,
562 &hour2
, &min2
, &sec2
, &hunsec2
,
565 line2
=strchr(line3
, ',');
567 for (comma
= 4; comma
< max_comma
; comma
++)
570 if(!(tmp
=strchr(++tmp
, ','))) break;
571 if(*(++tmp
) == ' ') break;
572 /* a space after a comma means we're already in a sentence */
576 if(comma
< max_comma
)max_comma
= comma
;
577 /* eliminate the trailing comma */
578 if(*line2
== ',') line2
++;
580 current
->lines
=0;num
=0;
581 current
->start
= 360000*hour1
+ 6000*min1
+ 100*sec1
+ hunsec1
;
582 current
->end
= 360000*hour2
+ 6000*min2
+ 100*sec2
+ hunsec2
;
584 while (((tmp
=strstr(line2
, "\\n")) != NULL
) || ((tmp
=strstr(line2
, "\\N")) != NULL
) ){
585 current
->text
[num
]=malloc(tmp
-line2
+1);
586 strncpy (current
->text
[num
], line2
, tmp
-line2
);
587 current
->text
[num
][tmp
-line2
]='\0';
591 if (current
->lines
>= SUB_MAX_TEXT
) return current
;
594 current
->text
[num
]=strdup(line2
);
600 static void sub_pp_ssa(subtitle
*sub
) {
605 /* eliminate any text enclosed with {}, they are font and color settings */
606 so
=de
=sub
->text
[--l
];
608 if(*so
== '{' && so
[1]=='\\') {
609 for (start
=so
; *so
&& *so
!='}'; so
++);
610 if(*so
) so
++; else so
=start
;
622 * PJS subtitles reader.
623 * That's the "Phoenix Japanimation Society" format.
624 * I found some of them in http://www.scriptsclub.org/ (used for anime).
625 * The time is in tenths of second.
627 * by set, based on code by szabi (dunnowhat sub format ;-)
629 static subtitle
*sub_read_line_pjs(stream_t
*st
,subtitle
*current
) {
630 char line
[LINE_LEN
+1];
631 char text
[LINE_LEN
+1], *s
, *d
;
633 if (!stream_read_line (st
, line
, LINE_LEN
))
636 for (s
=line
; *s
&& isspace(*s
); s
++);
637 /* allow empty lines at the end of the file */
641 if (sscanf (s
, "%ld,%ld,", &(current
->start
),
642 &(current
->end
)) <2) {
645 /* the files I have are in tenths of second */
646 current
->start
*= 10;
648 /* walk to the beggining of the string */
649 for (; *s
; s
++) if (*s
==',') break;
651 for (s
++; *s
; s
++) if (*s
==',') break;
657 /* copy the string to the text buffer */
658 for (s
++, d
=text
; *s
&& *s
!='"'; s
++, d
++)
661 current
->text
[0] = strdup(text
);
667 static subtitle
*sub_read_line_mpsub(stream_t
*st
, subtitle
*current
) {
668 char line
[LINE_LEN
+1];
675 if (!stream_read_line(st
, line
, LINE_LEN
)) return NULL
;
676 } while (sscanf (line
, "%f %f", &a
, &b
) !=2);
678 mpsub_position
+= a
*mpsub_multiplier
;
679 current
->start
=(int) mpsub_position
;
680 mpsub_position
+= b
*mpsub_multiplier
;
681 current
->end
=(int) mpsub_position
;
683 while (num
< SUB_MAX_TEXT
) {
684 if (!stream_read_line (st
, line
, LINE_LEN
)) {
685 if (num
== 0) return NULL
;
689 while (isspace(*p
)) p
++;
690 if (eol(*p
) && num
> 0) return current
;
691 if (eol(*p
)) return NULL
;
693 for (q
=p
; !eol(*q
); q
++);
696 current
->text
[num
]=strdup(p
);
697 // printf (">%s<\n",p);
698 current
->lines
= ++num
;
700 if (num
) return current
;
704 return NULL
; // we should have returned before if it's OK
707 #ifndef CONFIG_SORTSUB
708 //we don't need this if we use previous_sub_end
709 subtitle
*previous_aqt_sub
= NULL
;
712 static subtitle
*sub_read_line_aqt(stream_t
*st
,subtitle
*current
) {
713 char line
[LINE_LEN
+1];
718 // try to locate next subtitle
719 if (!stream_read_line (st
, line
, LINE_LEN
))
721 if (!(sscanf (line
, "-->> %ld", &(current
->start
)) <1))
725 #ifdef CONFIG_SORTSUB
726 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
728 if (previous_aqt_sub
!= NULL
)
729 previous_aqt_sub
->end
= current
->start
-1;
731 previous_aqt_sub
= current
;
734 if (!stream_read_line (st
, line
, LINE_LEN
))
737 sub_readtext((char *) &line
,¤t
->text
[0]);
739 current
->end
= current
->start
; // will be corrected by next subtitle
741 if (!stream_read_line (st
, line
, LINE_LEN
))
745 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
746 if (current
->text
[i
]==ERR
) {return ERR
;}
748 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
752 if (!strlen(current
->text
[0]) && !strlen(current
->text
[1])) {
753 #ifdef CONFIG_SORTSUB
754 previous_sub_end
= 0;
756 // void subtitle -> end of previous marked and exit
757 previous_aqt_sub
= NULL
;
765 #ifndef CONFIG_SORTSUB
766 subtitle
*previous_subrip09_sub
= NULL
;
769 static subtitle
*sub_read_line_subrip09(stream_t
*st
,subtitle
*current
) {
770 char line
[LINE_LEN
+1];
776 // try to locate next subtitle
777 if (!stream_read_line (st
, line
, LINE_LEN
))
779 if (!((len
=sscanf (line
, "[%d:%d:%d]",&a1
,&a2
,&a3
)) < 3))
783 current
->start
= a1
*360000+a2
*6000+a3
*100;
785 #ifdef CONFIG_SORTSUB
786 previous_sub_end
= (current
->start
) ? current
->start
- 1 : 0;
788 if (previous_subrip09_sub
!= NULL
)
789 previous_subrip09_sub
->end
= current
->start
-1;
791 previous_subrip09_sub
= current
;
794 if (!stream_read_line (st
, line
, LINE_LEN
))
799 current
->text
[0]=""; // just to be sure that string is clear
801 while ((next
=sub_readtext (next
, &(current
->text
[i
])))) {
802 if (current
->text
[i
]==ERR
) {return ERR
;}
804 if (i
>=SUB_MAX_TEXT
) { mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"Too many lines in a subtitle\n");current
->lines
=i
;return current
;}
808 if (!strlen(current
->text
[0]) && (i
==0)) {
809 #ifdef CONFIG_SORTSUB
810 previous_sub_end
= 0;
812 // void subtitle -> end of previous marked and exit
813 previous_subrip09_sub
= NULL
;
821 static subtitle
*sub_read_line_jacosub(stream_t
* st
, subtitle
* current
)
823 char line1
[LINE_LEN
], line2
[LINE_LEN
], directive
[LINE_LEN
], *p
, *q
;
824 unsigned a1
, a2
, a3
, a4
, b1
, b2
, b3
, b4
, comment
= 0;
825 static unsigned jacoTimeres
= 30;
826 static int jacoShift
= 0;
828 memset(current
, 0, sizeof(subtitle
));
829 memset(line1
, 0, LINE_LEN
);
830 memset(line2
, 0, LINE_LEN
);
831 memset(directive
, 0, LINE_LEN
);
832 while (!current
->text
[0]) {
833 if (!stream_read_line(st
, line1
, LINE_LEN
)) {
837 (line1
, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1
, &a2
, &a3
, &a4
,
838 &b1
, &b2
, &b3
, &b4
, line2
) < 9) {
839 if (sscanf(line1
, "@%u @%u %[^\n\r]", &a4
, &b4
, line2
) < 3) {
840 if (line1
[0] == '#') {
841 int hours
= 0, minutes
= 0, seconds
, delta
, inverter
=
843 unsigned units
= jacoShift
;
844 switch (toupper(line1
[1])) {
846 if (isalpha(line1
[2])) {
851 if (sscanf(&line1
[delta
], "%d", &hours
)) {
856 if (sscanf(&line1
[delta
], "%*d:%d", &minutes
)) {
858 (&line1
[delta
], "%*d:%*d:%d",
860 sscanf(&line1
[delta
], "%*d:%*d:%*d.%d",
864 sscanf(&line1
[delta
], "%d:%d.%d",
865 &minutes
, &seconds
, &units
);
870 sscanf(&line1
[delta
], "%d.%d", &seconds
,
875 ((hours
* 3600 + minutes
* 60 +
876 seconds
) * jacoTimeres
+
881 if (isalpha(line1
[2])) {
886 sscanf(&line1
[delta
], "%u", &jacoTimeres
);
893 (unsigned long) ((a4
+ jacoShift
) * 100.0 /
896 (unsigned long) ((b4
+ jacoShift
) * 100.0 /
902 long) (((a1
* 3600 + a2
* 60 + a3
) * jacoTimeres
+ a4
+
903 jacoShift
) * 100.0 / jacoTimeres
);
906 long) (((b1
* 3600 + b2
* 60 + b3
) * jacoTimeres
+ b4
+
907 jacoShift
) * 100.0 / jacoTimeres
);
911 while ((*p
== ' ') || (*p
== '\t')) {
914 if (isalpha(*p
)||*p
== '[') {
917 if (sscanf(p
, "%s %[^\n\r]", directive
, line1
) < 2)
918 return (subtitle
*) ERR
;
919 jLength
= strlen(directive
);
920 for (cont
= 0; cont
< jLength
; ++cont
) {
921 if (isalpha(*(directive
+ cont
)))
922 *(directive
+ cont
) = toupper(*(directive
+ cont
));
924 if ((strstr(directive
, "RDB") != NULL
)
925 || (strstr(directive
, "RDC") != NULL
)
926 || (strstr(directive
, "RLB") != NULL
)
927 || (strstr(directive
, "RLG") != NULL
)) {
930 if (strstr(directive
, "JL") != NULL
) {
931 current
->alignment
= SUB_ALIGNMENT_BOTTOMLEFT
;
932 } else if (strstr(directive
, "JR") != NULL
) {
933 current
->alignment
= SUB_ALIGNMENT_BOTTOMRIGHT
;
935 current
->alignment
= SUB_ALIGNMENT_BOTTOMCENTER
;
937 strcpy(line2
, line1
);
940 for (q
= line1
; (!eol(*p
)) && (current
->lines
< SUB_MAX_TEXT
); ++p
) {
948 //the next line to get rid of a blank after the comment
949 if ((*(p
+ 1)) == ' ')
961 if ((*(p
+ 1) == ' ') || (*(p
+ 1) == '\t'))
969 if (*(p
+ 1) == 'n') {
972 current
->text
[current
->lines
++] = strdup(line1
);
976 if ((toupper(*(p
+ 1)) == 'C')
977 || (toupper(*(p
+ 1)) == 'F')) {
981 if ((*(p
+ 1) == 'B') || (*(p
+ 1) == 'b') || (*(p
+ 1) == 'D') || //actually this means "insert current date here"
982 (*(p
+ 1) == 'I') || (*(p
+ 1) == 'i') || (*(p
+ 1) == 'N') || (*(p
+ 1) == 'T') || //actually this means "insert current time here"
983 (*(p
+ 1) == 'U') || (*(p
+ 1) == 'u')) {
987 if ((*(p
+ 1) == '\\') ||
988 (*(p
+ 1) == '~') || (*(p
+ 1) == '{')) {
990 } else if (eol(*(p
+ 1))) {
991 if (!stream_read_line(st
, directive
, LINE_LEN
))
993 trail_space(directive
);
994 av_strlcat(line2
, directive
, LINE_LEN
);
1005 current
->text
[current
->lines
] = strdup(line1
);
1011 static int sub_autodetect (stream_t
* st
, int *uses_time
) {
1012 char line
[LINE_LEN
+1];
1018 if (!stream_read_line (st
, line
, LINE_LEN
))
1021 if (sscanf (line
, "{%d}{%d}", &i
, &i
)==2)
1022 {*uses_time
=0;return SUB_MICRODVD
;}
1023 if (sscanf (line
, "{%d}{}", &i
)==1)
1024 {*uses_time
=0;return SUB_MICRODVD
;}
1025 if (sscanf (line
, "[%d][%d]", &i
, &i
)==2)
1026 {*uses_time
=1;return SUB_MPL2
;}
1027 if (sscanf (line
, "%d:%d:%d.%d,%d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
)==8)
1028 {*uses_time
=1;return SUB_SUBRIP
;}
1029 if (sscanf (line
, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i
, &i
, &i
, (char *)&i
, &i
, &i
, &i
, &i
, (char *)&i
, &i
)==10)
1030 {*uses_time
=1;return SUB_SUBVIEWER
;}
1031 if (sscanf (line
, "{T %d:%d:%d:%d",&i
, &i
, &i
, &i
)==4)
1032 {*uses_time
=1;return SUB_SUBVIEWER2
;}
1033 if (strstr (line
, "<SAMI>"))
1034 {*uses_time
=1; return SUB_SAMI
;}
1035 if (sscanf(line
, "%d:%d:%d.%d %d:%d:%d.%d", &i
, &i
, &i
, &i
, &i
, &i
, &i
, &i
) == 8)
1036 {*uses_time
= 1; return SUB_JACOSUB
;}
1037 if (sscanf(line
, "@%d @%d", &i
, &i
) == 2)
1038 {*uses_time
= 1; return SUB_JACOSUB
;}
1039 if (sscanf (line
, "%d:%d:%d:", &i
, &i
, &i
)==3)
1040 {*uses_time
=1;return SUB_VPLAYER
;}
1041 if (sscanf (line
, "%d:%d:%d ", &i
, &i
, &i
)==3)
1042 {*uses_time
=1;return SUB_VPLAYER
;}
1043 //TODO: just checking if first line of sub starts with "<" is WAY
1044 // too weak test for RT
1045 // Please someone who knows the format of RT... FIX IT!!!
1046 // It may conflict with other sub formats in the future (actually it doesn't)
1048 {*uses_time
=1;return SUB_RT
;}
1050 if (!memcmp(line
, "Dialogue: Marked", 16))
1051 {*uses_time
=1; return SUB_SSA
;}
1052 if (!memcmp(line
, "Dialogue: ", 10))
1053 {*uses_time
=1; return SUB_SSA
;}
1054 if (sscanf (line
, "%d,%d,\"%c", &i
, &i
, (char *) &i
) == 3)
1055 {*uses_time
=1;return SUB_PJS
;}
1056 if (sscanf (line
, "FORMAT=%d", &i
) == 1)
1057 {*uses_time
=0; return SUB_MPSUB
;}
1058 if (sscanf (line
, "FORMAT=TIM%c", &p
)==1 && p
=='E')
1059 {*uses_time
=1; return SUB_MPSUB
;}
1060 if (strstr (line
, "-->>"))
1061 {*uses_time
=0; return SUB_AQTITLE
;}
1062 if (sscanf (line
, "[%d:%d:%d]", &i
, &i
, &i
)==3)
1063 {*uses_time
=1;return SUB_SUBRIP09
;}
1066 return SUB_INVALID
; // too many bad lines
1069 extern int sub_utf8
;
1070 int sub_utf8_prev
=0;
1072 extern float sub_delay
;
1073 extern float sub_fps
;
1076 static iconv_t icdsc
= (iconv_t
)(-1);
1078 void subcp_open (stream_t
*st
)
1080 char *tocp
= "UTF-8";
1083 const char *cp_tmp
= sub_cp
;
1085 char enca_lang
[3], enca_fallback
[100];
1086 if (sscanf(sub_cp
, "enca:%2s:%99s", enca_lang
, enca_fallback
) == 2
1087 || sscanf(sub_cp
, "ENCA:%2s:%99s", enca_lang
, enca_fallback
) == 2) {
1088 if (st
&& st
->flags
& MP_STREAM_SEEK
) {
1089 cp_tmp
= guess_cp(st
, enca_lang
, enca_fallback
);
1091 cp_tmp
= enca_fallback
;
1093 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: enca failed, stream must be seekable.\n");
1097 if ((icdsc
= iconv_open (tocp
, cp_tmp
)) != (iconv_t
)(-1)){
1098 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: opened iconv descriptor.\n");
1101 mp_msg(MSGT_SUBREADER
,MSGL_ERR
,"SUB: error opening iconv descriptor.\n");
1105 void subcp_close (void)
1107 if (icdsc
!= (iconv_t
)(-1)){
1108 (void) iconv_close (icdsc
);
1109 icdsc
= (iconv_t
)(-1);
1110 mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: closed iconv descriptor.\n");
1114 subtitle
* subcp_recode (subtitle
*sub
)
1117 size_t ileft
, oleft
;
1119 if(icdsc
== (iconv_t
)(-1)) return sub
;
1122 ip
= sub
->text
[--l
];
1126 if (!(ot
= malloc(oleft
+ 1))){
1127 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1131 if (iconv(icdsc
, &ip
, &ileft
,
1132 &op
, &oleft
) == (size_t)(-1)) {
1133 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line.\n");
1137 // In some stateful encodings, we must clear the state to handle the last character
1138 if (iconv(icdsc
, NULL
, NULL
,
1139 &op
, &oleft
) == (size_t)(-1)) {
1140 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error recoding line, can't clear encoding state.\n");
1143 free (sub
->text
[l
]);
1150 #ifdef CONFIG_FRIBIDI
1152 * Do conversion necessary for right-to-left language support via fribidi.
1153 * @param sub subtitle to convert
1154 * @param sub_utf8 whether the subtitle is encoded in UTF-8
1155 * @param from first new subtitle, all lines before this are assumed to be already converted
1157 static subtitle
* sub_fribidi (subtitle
*sub
, int sub_utf8
, int from
)
1159 FriBidiChar logical
[LINE_LEN
+1], visual
[LINE_LEN
+1]; // Hopefully these two won't smash the stack
1160 char *ip
= NULL
, *op
= NULL
;
1161 FriBidiCharType base
;
1162 size_t len
,orig_len
;
1165 fribidi_boolean log2vis
;
1168 fribidi_set_mirroring(1);
1169 fribidi_set_reorder_nsm(0);
1171 if( sub_utf8
== 0 ) {
1172 char_set_num
= fribidi_parse_charset (fribidi_charset
?fribidi_charset
:"ISO8859-8");
1174 char_set_num
= fribidi_parse_charset ("UTF-8");
1177 ip
= sub
->text
[--l
];
1178 orig_len
= len
= strlen( ip
); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1179 if(len
> LINE_LEN
) {
1180 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: sub->text is longer than LINE_LEN.\n");
1184 len
= fribidi_charset_to_unicode (char_set_num
, ip
, len
, logical
);
1185 base
= fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
1186 log2vis
= fribidi_log2vis (logical
, len
, &base
,
1188 visual
, NULL
, NULL
, NULL
);
1190 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
,
1192 if((op
= malloc((FFMAX(2*orig_len
,2*len
) + 1))) == NULL
) {
1193 mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: error allocating mem.\n");
1197 fribidi_unicode_to_charset ( char_set_num
, visual
, len
,op
);
1203 for (l
= sub
->lines
; l
;)
1204 free (sub
->text
[--l
]);
1212 static void adjust_subs_time(subtitle
* sub
, float subtime
, float fps
, int block
,
1213 int sub_num
, int sub_uses_time
) {
1217 unsigned long subfms
= (sub_uses_time
? 100 : fps
) * subtime
;
1218 unsigned long overlap
= (sub_uses_time
? 100 : fps
) / 5; // 0.2s
1222 if (sub
->end
<= sub
->start
){
1223 sub
->end
= sub
->start
+ subfms
;
1230 if ((sub
->end
> nextsub
->start
) && (sub
->end
<= nextsub
->start
+ overlap
)) {
1231 // these subtitles overlap for less than 0.2 seconds
1232 // and would result in very short overlapping subtitle
1233 // so let's fix the problem here, before overlapping code
1234 // get its hands on them
1235 unsigned delta
= sub
->end
- nextsub
->start
, half
= delta
/ 2;
1236 sub
->end
-= half
+ 1;
1237 nextsub
->start
+= delta
- half
;
1239 if (sub
->end
>= nextsub
->start
){
1240 sub
->end
= nextsub
->start
- 1;
1241 if (sub
->end
- sub
->start
> subfms
)
1242 sub
->end
= sub
->start
+ subfms
;
1249 * Movies are often converted from FILM (24 fps)
1250 * to PAL (25) by simply speeding it up, so we
1251 * to multiply the original timestmaps by
1252 * (Movie's FPS / Subtitle's (guessed) FPS)
1253 * so eg. for 23.98 fps movie and PAL time based
1254 * subtitles we say -subfps 25 and we're fine!
1257 /* timed sub fps correction ::atmos */
1258 /* the frame-based case is handled in mpcommon.c
1259 * where find_sub is called */
1260 if(sub_uses_time
&& sub_fps
) {
1261 sub
->start
*= sub_fps
/fps
;
1262 sub
->end
*= sub_fps
/fps
;
1268 if (n
) mp_msg(MSGT_SUBREADER
,MSGL_V
,"SUB: Adjusted %d subtitle(s).\n", n
);
1272 subtitle
* (*read
)(stream_t
*st
,subtitle
*dest
);
1273 void (*post
)(subtitle
*dest
);
1278 const char* guess_buffer_cp(unsigned char* buffer
, int buflen
, const char *preferred_language
, const char *fallback
)
1280 const char **languages
;
1282 EncaAnalyser analyser
;
1283 EncaEncoding encoding
;
1284 const char *detected_sub_cp
= NULL
;
1287 languages
= enca_get_languages(&langcnt
);
1288 mp_msg(MSGT_SUBREADER
, MSGL_V
, "ENCA supported languages: ");
1289 for (i
= 0; i
< langcnt
; i
++) {
1290 mp_msg(MSGT_SUBREADER
, MSGL_V
, "%s ", languages
[i
]);
1292 mp_msg(MSGT_SUBREADER
, MSGL_V
, "\n");
1294 for (i
= 0; i
< langcnt
; i
++) {
1295 if (strcasecmp(languages
[i
], preferred_language
) != 0) continue;
1296 analyser
= enca_analyser_alloc(languages
[i
]);
1297 encoding
= enca_analyse_const(analyser
, buffer
, buflen
);
1298 enca_analyser_free(analyser
);
1299 if (encoding
.charset
!= ENCA_CS_UNKNOWN
) {
1300 detected_sub_cp
= enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
);
1307 if (!detected_sub_cp
) {
1308 detected_sub_cp
= fallback
;
1309 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detection failed: fallback to %s\n", fallback
);
1311 mp_msg(MSGT_SUBREADER
, MSGL_INFO
, "ENCA detected charset: %s\n", detected_sub_cp
);
1314 return detected_sub_cp
;
1317 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1318 const char* guess_cp(stream_t
*st
, const char *preferred_language
, const char *fallback
)
1321 unsigned char *buffer
;
1322 const char *detected_sub_cp
= NULL
;
1324 buffer
= malloc(MAX_GUESS_BUFFER_SIZE
);
1325 buflen
= stream_read(st
,buffer
, MAX_GUESS_BUFFER_SIZE
);
1327 detected_sub_cp
= guess_buffer_cp(buffer
, buflen
, preferred_language
, fallback
);
1333 return detected_sub_cp
;
1335 #undef MAX_GUESS_BUFFER_SIZE
1338 sub_data
* sub_read_file (char *filename
, float fps
) {
1340 int n_max
, n_first
, i
, j
, sub_first
, sub_orig
;
1341 subtitle
*first
, *second
, *sub
, *return_sub
, *alloced_sub
= NULL
;
1342 sub_data
*subt_data
;
1343 int uses_time
= 0, sub_num
= 0, sub_errs
= 0;
1344 struct subreader sr
[]=
1346 { sub_read_line_microdvd
, NULL
, "microdvd" },
1347 { sub_read_line_subrip
, NULL
, "subrip" },
1348 { sub_read_line_subviewer
, NULL
, "subviewer" },
1349 { sub_read_line_sami
, NULL
, "sami" },
1350 { sub_read_line_vplayer
, NULL
, "vplayer" },
1351 { sub_read_line_rt
, NULL
, "rt" },
1352 { sub_read_line_ssa
, sub_pp_ssa
, "ssa" },
1353 { sub_read_line_pjs
, NULL
, "pjs" },
1354 { sub_read_line_mpsub
, NULL
, "mpsub" },
1355 { sub_read_line_aqt
, NULL
, "aqt" },
1356 { sub_read_line_subviewer2
, NULL
, "subviewer 2.0" },
1357 { sub_read_line_subrip09
, NULL
, "subrip 0.9" },
1358 { sub_read_line_jacosub
, NULL
, "jacosub" },
1359 { sub_read_line_mpl2
, NULL
, "mpl2" }
1361 struct subreader
*srp
;
1363 if(filename
==NULL
) return NULL
; //qnx segfault
1365 fd
=open_stream (filename
, NULL
, &i
); if (!fd
) return NULL
;
1367 sub_format
=sub_autodetect (fd
, &uses_time
);
1368 mpsub_multiplier
= (uses_time
? 100.0 : 1.0);
1369 if (sub_format
==SUB_INVALID
) {mp_msg(MSGT_SUBREADER
,MSGL_WARN
,"SUB: Could not determine file format\n");return NULL
;}
1371 mp_msg(MSGT_SUBREADER
, MSGL_V
, "SUB: Detected subtitle file format: %s\n", srp
->name
);
1377 sub_utf8_prev
=sub_utf8
;
1381 if ((l
=strlen(filename
))>4){
1382 char *exts
[] = {".utf", ".utf8", ".utf-8" };
1384 if (l
>= strlen(exts
[k
]) && !strcasecmp(filename
+(l
- strlen(exts
[k
])), exts
[k
])){
1389 if (k
<0) subcp_open(fd
);
1394 first
=malloc(n_max
*sizeof(subtitle
));
1398 sub_utf8
=sub_utf8_prev
;
1403 #ifdef CONFIG_SORTSUB
1405 sub
= malloc(sizeof(subtitle
));
1406 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1407 //as the beginning of the following
1408 previous_sub_end
= 0;
1413 first
=realloc(first
,n_max
*sizeof(subtitle
));
1415 #ifndef CONFIG_SORTSUB
1416 sub
= &first
[sub_num
];
1418 memset(sub
, '\0', sizeof(subtitle
));
1419 sub
=srp
->read(fd
,sub
);
1420 if(!sub
) break; // EOF
1422 if ((sub
!=ERR
) && (sub_utf8
& 2)) sub
=subcp_recode(sub
);
1424 #ifdef CONFIG_FRIBIDI
1425 if (sub
!=ERR
) sub
=sub_fribidi(sub
,sub_utf8
,0);
1432 if ( first
) free(first
);
1436 // Apply any post processing that needs recoding first
1437 if ((sub
!=ERR
) && !sub_no_text_pp
&& srp
->post
) srp
->post(sub
);
1438 #ifdef CONFIG_SORTSUB
1439 if(!sub_num
|| (first
[sub_num
- 1].start
<= sub
->start
)){
1440 first
[sub_num
].start
= sub
->start
;
1441 first
[sub_num
].end
= sub
->end
;
1442 first
[sub_num
].lines
= sub
->lines
;
1443 first
[sub_num
].alignment
= sub
->alignment
;
1444 for(i
= 0; i
< sub
->lines
; ++i
){
1445 first
[sub_num
].text
[i
] = sub
->text
[i
];
1447 if (previous_sub_end
){
1448 first
[sub_num
- 1].end
= previous_sub_end
;
1449 previous_sub_end
= 0;
1452 for(j
= sub_num
- 1; j
>= 0; --j
){
1453 first
[j
+ 1].start
= first
[j
].start
;
1454 first
[j
+ 1].end
= first
[j
].end
;
1455 first
[j
+ 1].lines
= first
[j
].lines
;
1456 first
[j
+ 1].alignment
= first
[j
].alignment
;
1457 for(i
= 0; i
< first
[j
].lines
; ++i
){
1458 first
[j
+ 1].text
[i
] = first
[j
].text
[i
];
1460 if(!j
|| (first
[j
- 1].start
<= sub
->start
)){
1461 first
[j
].start
= sub
->start
;
1462 first
[j
].end
= sub
->end
;
1463 first
[j
].lines
= sub
->lines
;
1464 first
[j
].alignment
= sub
->alignment
;
1465 for(i
= 0; i
< SUB_MAX_TEXT
; ++i
){
1466 first
[j
].text
[i
] = sub
->text
[i
];
1468 if (previous_sub_end
){
1469 first
[j
].end
= first
[j
- 1].end
;
1470 first
[j
- 1].end
= previous_sub_end
;
1471 previous_sub_end
= 0;
1478 if(sub
==ERR
) ++sub_errs
; else ++sub_num
; // Error vs. Valid
1488 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1489 mp_msg(MSGT_SUBREADER
, MSGL_V
,"SUB: Read %i subtitles, %i bad line(s).\n",
1497 // we do overlap if the user forced it (suboverlap_enable == 2) or
1498 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1499 // this is because usually overlapping subtitles are found in these formats,
1500 // while in others they are probably result of bad timing
1501 if ((suboverlap_enabled
== 2) ||
1502 ((suboverlap_enabled
) && ((sub_format
== SUB_JACOSUB
) || (sub_format
== SUB_SSA
)))) {
1503 adjust_subs_time(first
, 6.0, fps
, 0, sub_num
, uses_time
);/*~6 secs AST*/
1504 // here we manage overlapping subtitles
1509 // for each subtitle in first[] we deal with its 'block' of
1511 for (sub_first
= 0; sub_first
< n_first
; ++sub_first
) {
1512 unsigned long global_start
= first
[sub_first
].start
,
1513 global_end
= first
[sub_first
].end
, local_start
, local_end
;
1514 int lines_to_add
= first
[sub_first
].lines
, sub_to_add
= 0,
1515 **placeholder
= NULL
, higher_line
= 0, counter
, start_block_sub
= sub_num
;
1516 char real_block
= 1;
1518 // here we find the number of subtitles inside the 'block'
1519 // and its span interval. this works well only with sorted
1521 while ((sub_first
+ sub_to_add
+ 1 < n_first
) && (first
[sub_first
+ sub_to_add
+ 1].start
< global_end
)) {
1523 lines_to_add
+= first
[sub_first
+ sub_to_add
].lines
;
1524 if (first
[sub_first
+ sub_to_add
].start
< global_start
) {
1525 global_start
= first
[sub_first
+ sub_to_add
].start
;
1527 if (first
[sub_first
+ sub_to_add
].end
> global_end
) {
1528 global_end
= first
[sub_first
+ sub_to_add
].end
;
1532 /* Avoid n^2 memory use for the "placeholder" data structure
1533 * below with subtitles that have a huge number of
1534 * consecutive overlapping lines. */
1535 lines_to_add
= FFMIN(lines_to_add
, SUB_MAX_TEXT
);
1537 // we need a structure to keep trace of the screen lines
1538 // used by the subs, a 'placeholder'
1539 counter
= 2 * sub_to_add
+ 1; // the maximum number of subs derived
1540 // from a block of sub_to_add+1 subs
1541 placeholder
= malloc(sizeof(int *) * counter
);
1542 for (i
= 0; i
< counter
; ++i
) {
1543 placeholder
[i
] = malloc(sizeof(int) * lines_to_add
);
1544 for (j
= 0; j
< lines_to_add
; ++j
) {
1545 placeholder
[i
][j
] = -1;
1550 local_end
= global_start
- 1;
1554 // here we find the beginning and the end of a new
1555 // subtitle in the block
1556 local_start
= local_end
+ 1;
1557 local_end
= global_end
;
1558 for (j
= 0; j
<= sub_to_add
; ++j
) {
1559 if ((first
[sub_first
+ j
].start
- 1 > local_start
) && (first
[sub_first
+ j
].start
- 1 < local_end
)) {
1560 local_end
= first
[sub_first
+ j
].start
- 1;
1561 } else if ((first
[sub_first
+ j
].end
> local_start
) && (first
[sub_first
+ j
].end
< local_end
)) {
1562 local_end
= first
[sub_first
+ j
].end
;
1565 // here we allocate the screen lines to subs we must
1566 // display in current local_start-local_end interval.
1567 // if the subs were yet presents in the previous interval
1568 // they keep the same lines, otherside they get unused lines
1569 for (j
= 0; j
<= sub_to_add
; ++j
) {
1570 if ((first
[sub_first
+ j
].start
<= local_end
) && (first
[sub_first
+ j
].end
> local_start
)) {
1571 unsigned long sub_lines
= first
[sub_first
+ j
].lines
, fragment_length
= lines_to_add
+ 1,
1574 int fragment_position
= -1;
1576 // if this is not the first new sub of the block
1577 // we find if this sub was present in the previous
1580 for (i
= 0; i
< lines_to_add
; ++i
) {
1581 if (placeholder
[counter
- 1][i
] == sub_first
+ j
) {
1582 placeholder
[counter
][i
] = sub_first
+ j
;
1589 // we are looking for the shortest among all groups of
1590 // sequential blank lines whose length is greater than or
1591 // equal to sub_lines. we store in fragment_position the
1592 // position of the shortest group, in fragment_length its
1593 // length, and in tmp the length of the group currently
1595 for (i
= 0; i
< lines_to_add
; ++i
) {
1596 if (placeholder
[counter
][i
] == -1) {
1597 // placeholder[counter][i] is part of the current group
1601 if (tmp
== sub_lines
) {
1602 // current group's size fits exactly the one we
1603 // need, so we stop looking
1604 fragment_position
= i
- tmp
;
1608 if ((tmp
) && (tmp
> sub_lines
) && (tmp
< fragment_length
)) {
1609 // current group is the best we found till here,
1610 // but is still bigger than the one we are looking
1611 // for, so we keep on looking
1612 fragment_length
= tmp
;
1613 fragment_position
= i
- tmp
;
1616 // current group doesn't fit at all, so we forget it
1622 // last screen line is blank, a group ends with it
1623 if ((tmp
>= sub_lines
) && (tmp
< fragment_length
)) {
1624 fragment_position
= i
- tmp
;
1627 if (fragment_position
== -1) {
1628 // it was not possible to find free screen line(s) for a subtitle,
1629 // usually this means a bug in the code; however we do not overlap
1630 mp_msg(MSGT_SUBREADER
, MSGL_WARN
, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1631 higher_line
= SUB_MAX_TEXT
+ 1;
1634 for (tmp
= 0; tmp
< sub_lines
; ++tmp
) {
1635 placeholder
[counter
][fragment_position
+ tmp
] = sub_first
+ j
;
1640 for (j
= higher_line
+ 1; j
< lines_to_add
; ++j
) {
1641 if (placeholder
[counter
][j
] != -1)
1646 if (higher_line
>= SUB_MAX_TEXT
) {
1647 // the 'block' has too much lines, so we don't overlap the
1649 second
= (subtitle
*) realloc(second
, (sub_num
+ sub_to_add
+ 1) * sizeof(subtitle
));
1650 for (j
= 0; j
<= sub_to_add
; ++j
) {
1652 memset(&second
[sub_num
+ j
], '\0', sizeof(subtitle
));
1653 second
[sub_num
+ j
].start
= first
[sub_first
+ j
].start
;
1654 second
[sub_num
+ j
].end
= first
[sub_first
+ j
].end
;
1655 second
[sub_num
+ j
].lines
= first
[sub_first
+ j
].lines
;
1656 second
[sub_num
+ j
].alignment
= first
[sub_first
+ j
].alignment
;
1657 for (ls
= 0; ls
< second
[sub_num
+ j
].lines
; ls
++) {
1658 second
[sub_num
+ j
].text
[ls
] = strdup(first
[sub_first
+ j
].text
[ls
]);
1661 sub_num
+= sub_to_add
+ 1;
1662 sub_first
+= sub_to_add
;
1667 // we read the placeholder structure and create the new
1669 second
= (subtitle
*) realloc(second
, (sub_num
+ 1) * sizeof(subtitle
));
1670 memset(&second
[sub_num
], '\0', sizeof(subtitle
));
1671 second
[sub_num
].start
= local_start
;
1672 second
[sub_num
].end
= local_end
;
1673 second
[sub_num
].alignment
= first
[sub_first
].alignment
;
1674 n_max
= (lines_to_add
< SUB_MAX_TEXT
) ? lines_to_add
: SUB_MAX_TEXT
;
1675 for (i
= 0, j
= 0; j
< n_max
; ++j
) {
1676 if (placeholder
[counter
][j
] != -1) {
1677 int lines
= first
[placeholder
[counter
][j
]].lines
;
1678 for (ls
= 0; ls
< lines
; ++ls
) {
1679 second
[sub_num
].text
[i
++] = strdup(first
[placeholder
[counter
][j
]].text
[ls
]);
1683 second
[sub_num
].text
[i
++] = strdup(" ");
1688 } while (local_end
< global_end
);
1690 for (i
= 0; i
< counter
; ++i
)
1691 second
[start_block_sub
+ i
].lines
= higher_line
+ 1;
1693 counter
= 2 * sub_to_add
+ 1;
1694 for (i
= 0; i
< counter
; ++i
) {
1695 free(placeholder
[i
]);
1698 sub_first
+= sub_to_add
;
1701 for (j
= sub_orig
- 1; j
>= 0; --j
) {
1702 for (i
= first
[j
].lines
- 1; i
>= 0; --i
) {
1703 free(first
[j
].text
[i
]);
1708 return_sub
= second
;
1709 } else { //if(suboverlap_enabled)
1710 adjust_subs_time(first
, 6.0, fps
, 1, sub_num
, uses_time
);/*~6 secs AST*/
1713 if (return_sub
== NULL
) return NULL
;
1714 subt_data
= malloc(sizeof(sub_data
));
1715 subt_data
->filename
= strdup(filename
);
1716 subt_data
->sub_uses_time
= uses_time
;
1717 subt_data
->sub_num
= sub_num
;
1718 subt_data
->sub_errs
= sub_errs
;
1719 subt_data
->subtitles
= return_sub
;
1724 char * strreplace( char * in
,char * what
,char * whereof
)
1729 if ( ( in
== NULL
)||( what
== NULL
)||( whereof
== NULL
)||( ( tmp
=strstr( in
,what
) ) == NULL
) ) return NULL
;
1730 for( i
=0;i
<strlen( whereof
);i
++ ) tmp
[i
]=whereof
[i
];
1731 if ( strlen( what
) > strlen( whereof
) ) tmp
[i
]=0;
1737 static void strcpy_trim(char *d
, char *s
)
1739 // skip leading whitespace
1740 while (*s
&& isspace(*s
)) {
1745 while (*s
&& !isspace(*s
)) {
1750 // trim excess whitespace
1751 while (*s
&& isspace(*s
)) {
1760 static void strcpy_strip_ext(char *d
, char *s
)
1762 char *tmp
= strrchr(s
,'.');
1767 strncpy(d
, s
, tmp
-s
);
1776 static void strcpy_get_ext(char *d
, char *s
)
1778 char *tmp
= strrchr(s
,'.');
1787 static int whiteonly(char *s
)
1790 if (!isspace(*s
)) return 0;
1796 typedef struct subfn
1802 static int compare_sub_priority(const void *a
, const void *b
)
1804 if (((const subfn
*)a
)->priority
> ((const subfn
*)b
)->priority
) {
1806 } else if (((const subfn
*)a
)->priority
< ((const subfn
*)b
)->priority
) {
1809 return strcoll(((const subfn
*)a
)->fname
, ((const subfn
*)b
)->fname
);
1813 char** sub_filenames(const char* path
, char *fname
)
1815 char *f_dir
, *f_fname
, *f_fname_noext
, *f_fname_trim
, *tmp
, *tmp_sub_id
;
1816 char *tmp_fname_noext
, *tmp_fname_trim
, *tmp_fname_ext
, *tmpresult
;
1818 int len
, pos
, found
, i
, j
;
1819 char * sub_exts
[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL
};
1830 len
= (strlen(fname
) > 256 ? strlen(fname
) : 256)
1831 +(strlen(path
) > 256 ? strlen(path
) : 256)+2;
1833 f_dir
= malloc(len
);
1834 f_fname
= malloc(len
);
1835 f_fname_noext
= malloc(len
);
1836 f_fname_trim
= malloc(len
);
1838 tmp_fname_noext
= malloc(len
);
1839 tmp_fname_trim
= malloc(len
);
1840 tmp_fname_ext
= malloc(len
);
1842 tmpresult
= malloc(len
);
1844 result
= malloc(sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1845 memset(result
, 0, sizeof(subfn
)*MAX_SUBTITLE_FILES
);
1849 tmp
= strrchr(fname
,'/');
1850 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__OS2__)
1851 if(!tmp
)tmp
= strrchr(fname
,'\\');
1852 if(!tmp
)tmp
= strrchr(fname
,':');
1855 // extract filename & dirname from fname
1857 strcpy(f_fname
, tmp
+1);
1859 strncpy(f_dir
, fname
, pos
+1);
1862 strcpy(f_fname
, fname
);
1863 strcpy(f_dir
, "./");
1866 strcpy_strip_ext(f_fname_noext
, f_fname
);
1867 strcpy_trim(f_fname_trim
, f_fname_noext
);
1870 if (dvdsub_lang
&& !whiteonly(dvdsub_lang
)) {
1871 tmp_sub_id
= malloc(strlen(dvdsub_lang
)+1);
1872 strcpy_trim(tmp_sub_id
, dvdsub_lang
);
1876 // 1 = any subtitle file
1877 // 2 = any sub file containing movie name
1878 // 3 = sub file containing movie name and the lang extension
1879 for (j
= 0; j
<= 1; j
++) {
1880 d
= opendir(j
== 0 ? f_dir
: path
);
1882 while ((de
= readdir(d
))) {
1883 // retrieve various parts of the filename
1884 strcpy_strip_ext(tmp_fname_noext
, de
->d_name
);
1885 strcpy_get_ext(tmp_fname_ext
, de
->d_name
);
1886 strcpy_trim(tmp_fname_trim
, tmp_fname_noext
);
1888 // does it end with a subtitle extension?
1892 for (i
= ((sub_cp
&& strncasecmp(sub_cp
, "enca", 4) != 0) ? 3 : 0); sub_exts
[i
]; i
++) {
1894 for (i
= (sub_cp
? 3 : 0); sub_exts
[i
]; i
++) {
1897 for (i
= 0; sub_exts
[i
]; i
++) {
1899 if (strcasecmp(sub_exts
[i
], tmp_fname_ext
) == 0) {
1905 // we have a (likely) subtitle file
1908 if (!prio
&& tmp_sub_id
)
1910 sprintf(tmpresult
, "%s %s", f_fname_trim
, tmp_sub_id
);
1911 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"dvdsublang...%s\n", tmpresult
);
1912 if (strcmp(tmp_fname_trim
, tmpresult
) == 0 && sub_match_fuzziness
>= 1) {
1913 // matches the movie name + lang extension
1917 if (!prio
&& strcmp(tmp_fname_trim
, f_fname_trim
) == 0) {
1918 // matches the movie name
1921 if (!prio
&& (tmp
= strstr(tmp_fname_trim
, f_fname_trim
)) && (sub_match_fuzziness
>= 1)) {
1922 // contains the movie name
1923 tmp
+= strlen(f_fname_trim
);
1924 if (tmp_sub_id
&& strstr(tmp
, tmp_sub_id
)) {
1925 // with sub_id specified prefer localized subtitles
1927 } else if ((tmp_sub_id
== NULL
) && whiteonly(tmp
)) {
1928 // without sub_id prefer "plain" name
1931 // with no localized subs found, try any else instead
1936 // doesn't contain the movie name
1937 // don't try in the mplayer subtitle directory
1938 if ((j
== 0) && (sub_match_fuzziness
>= 2)) {
1946 if (i
<3){ // prefer UTF-8 coded
1950 sprintf(tmpresult
, "%s%s", j
== 0 ? f_dir
: path
, de
->d_name
);
1951 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1952 if ((f
= fopen(tmpresult
, "rt"))) {
1954 result
[subcnt
].priority
= prio
;
1955 result
[subcnt
].fname
= strdup(tmpresult
);
1961 if (subcnt
>= MAX_SUBTITLE_FILES
) break;
1968 if (tmp_sub_id
) free(tmp_sub_id
);
1972 free(f_fname_noext
);
1975 free(tmp_fname_noext
);
1976 free(tmp_fname_trim
);
1977 free(tmp_fname_ext
);
1981 qsort(result
, subcnt
, sizeof(subfn
), compare_sub_priority
);
1983 result2
= malloc(sizeof(char*)*(subcnt
+1));
1984 memset(result2
, 0, sizeof(char*)*(subcnt
+1));
1986 for (i
= 0; i
< subcnt
; i
++) {
1987 result2
[i
] = result
[i
].fname
;
1989 result2
[subcnt
] = NULL
;
1996 void list_sub_file(sub_data
* subd
){
1998 subtitle
*subs
= subd
->subtitles
;
2000 for(j
=0; j
< subd
->sub_num
; j
++){
2001 subtitle
* egysub
=&subs
[j
];
2002 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"%i line%c (%li-%li)\n",
2004 (1==egysub
->lines
)?' ':'s',
2007 for (i
=0; i
<egysub
->lines
; i
++) {
2008 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"\t\t%d: %s%s", i
,egysub
->text
[i
], i
==egysub
->lines
-1?"":" \n ");
2010 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"\n");
2013 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"Subtitle format %s time.\n",
2014 subd
->sub_uses_time
? "uses":"doesn't use");
2015 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"Read %i subtitles, %i errors.\n", subd
->sub_num
, subd
->sub_errs
);
2018 void dump_srt(sub_data
* subd
, float fps
){
2024 subtitle
*subs
= subd
->subtitles
;
2026 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2028 fd
=fopen("dumpsub.srt","w");
2031 perror("dump_srt: fopen");
2034 for(i
=0; i
< subd
->sub_num
; i
++)
2036 onesub
=subs
+i
; //=&subs[i];
2037 fprintf(fd
,"%d\n",i
+1);//line number
2040 if (!subd
->sub_uses_time
)
2041 temp
= temp
* 100 / sub_fps
;
2042 temp
-= sub_delay
* 100;
2043 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2044 m
=temp
/6000; temp
%=6000; //m =1*100*60
2045 s
=temp
/100; temp
%=100; //s =1*100
2046 ms
=temp
*10; //ms=1*10
2047 fprintf(fd
,"%02d:%02d:%02d,%03d --> ",h
,m
,s
,ms
);
2050 if (!subd
->sub_uses_time
)
2051 temp
= temp
* 100 / sub_fps
;
2052 temp
-= sub_delay
* 100;
2053 h
=temp
/360000;temp
%=360000;
2054 m
=temp
/6000; temp
%=6000;
2055 s
=temp
/100; temp
%=100;
2057 fprintf(fd
,"%02d:%02d:%02d,%03d\n",h
,m
,s
,ms
);
2059 for(j
=0;j
<onesub
->lines
;j
++)
2060 fprintf(fd
,"%s\n",onesub
->text
[j
]);
2065 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
2068 void dump_mpsub(sub_data
* subd
, float fps
){
2072 subtitle
*subs
= subd
->subtitles
;
2074 mpsub_position
= subd
->sub_uses_time
? (sub_delay
*100) : (sub_delay
*fps
);
2075 if (sub_fps
==0) sub_fps
=fps
;
2077 fd
=fopen ("dump.mpsub", "w");
2079 perror ("dump_mpsub: fopen");
2084 if (subd
->sub_uses_time
) fprintf (fd
,"FORMAT=TIME\n\n");
2085 else fprintf (fd
, "FORMAT=%5.2f\n\n", fps
);
2087 for(j
=0; j
< subd
->sub_num
; j
++){
2088 subtitle
* egysub
=&subs
[j
];
2089 if (subd
->sub_uses_time
) {
2090 a
=((egysub
->start
-mpsub_position
)/100.0);
2091 b
=((egysub
->end
-egysub
->start
)/100.0);
2092 if ( (float)((int)a
) == a
)
2093 fprintf (fd
, "%.0f",a
);
2095 fprintf (fd
, "%.2f",a
);
2097 if ( (float)((int)b
) == b
)
2098 fprintf (fd
, " %.0f\n",b
);
2100 fprintf (fd
, " %.2f\n",b
);
2102 fprintf (fd
, "%ld %ld\n", (long)((egysub
->start
*(fps
/sub_fps
))-((mpsub_position
*(fps
/sub_fps
)))),
2103 (long)(((egysub
->end
)-(egysub
->start
))*(fps
/sub_fps
)));
2106 mpsub_position
= egysub
->end
;
2107 for (i
=0; i
<egysub
->lines
; i
++) {
2108 fprintf (fd
, "%s\n",egysub
->text
[i
]);
2113 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
2116 void dump_microdvd(sub_data
* subd
, float fps
) {
2119 subtitle
*subs
= subd
->subtitles
;
2122 fd
= fopen("dumpsub.sub", "w");
2124 perror("dumpsub.sub: fopen");
2127 delay
= sub_delay
* sub_fps
;
2128 for (i
= 0; i
< subd
->sub_num
; ++i
) {
2130 start
= subs
[i
].start
;
2132 if (subd
->sub_uses_time
) {
2133 start
= start
* sub_fps
/ 100 ;
2134 end
= end
* sub_fps
/ 100;
2137 start
= start
* sub_fps
/ fps
;
2138 end
= end
* sub_fps
/ fps
;
2142 fprintf(fd
, "{%d}{%d}", start
, end
);
2143 for (j
= 0; j
< subs
[i
].lines
; ++j
)
2144 fprintf(fd
, "%s%s", j
? "|" : "", subs
[i
].text
[j
]);
2148 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.sub\'.\n");
2151 void dump_jacosub(sub_data
* subd
, float fps
) {
2157 subtitle
*subs
= subd
->subtitles
;
2159 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2161 fd
=fopen("dumpsub.jss","w");
2164 perror("dump_jacosub: fopen");
2167 fprintf(fd
, "#TIMERES %d\n", (subd
->sub_uses_time
) ? 100 : (int)sub_fps
);
2168 for(i
=0; i
< subd
->sub_num
; i
++)
2170 onesub
=subs
+i
; //=&subs[i];
2173 if (!subd
->sub_uses_time
)
2174 temp
= temp
* 100 / sub_fps
;
2175 temp
-= sub_delay
* 100;
2176 h
=temp
/360000;temp
%=360000; //h =1*100*60*60
2177 m
=temp
/6000; temp
%=6000; //m =1*100*60
2178 s
=temp
/100; temp
%=100; //s =1*100
2180 fprintf(fd
,"%02d:%02d:%02d.%02d ",h
,m
,s
,cs
);
2183 if (!subd
->sub_uses_time
)
2184 temp
= temp
* 100 / sub_fps
;
2185 temp
-= sub_delay
* 100;
2186 h
=temp
/360000;temp
%=360000;
2187 m
=temp
/6000; temp
%=6000;
2188 s
=temp
/100; temp
%=100;
2190 fprintf(fd
,"%02d:%02d:%02d.%02d {~} ",h
,m
,s
,cs
);
2192 for(j
=0;j
<onesub
->lines
;j
++)
2193 fprintf(fd
,"%s%s",j
? "\\n" : "", onesub
->text
[j
]);
2198 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2201 void dump_sami(sub_data
* subd
, float fps
) {
2206 subtitle
*subs
= subd
->subtitles
;
2208 if (!subd
->sub_uses_time
&& sub_fps
== 0)
2210 fd
=fopen("dumpsub.smi","w");
2213 perror("dump_jacosub: fopen");
2216 fprintf(fd
, "<SAMI>\n"
2218 " <STYLE TYPE=\"Text/css\">\n"
2220 " 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"
2221 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2226 for(i
=0; i
< subd
->sub_num
; i
++)
2228 onesub
=subs
+i
; //=&subs[i];
2231 if (!subd
->sub_uses_time
)
2232 temp
= temp
* 100 / sub_fps
;
2233 temp
-= sub_delay
* 100;
2234 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2235 "\t <P>", temp
* 10);
2237 for(j
=0;j
<onesub
->lines
;j
++)
2238 fprintf(fd
,"%s%s",j
? "<br>" : "", onesub
->text
[j
]);
2243 if (!subd
->sub_uses_time
)
2244 temp
= temp
* 100 / sub_fps
;
2245 temp
-= sub_delay
* 100;
2246 fprintf(fd
,"\t<SYNC Start=%lu>\n"
2247 "\t <P> \n", temp
* 10);
2249 fprintf(fd
, "</BODY>\n"
2252 mp_msg(MSGT_SUBREADER
,MSGL_INFO
,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2255 void sub_free( sub_data
* subd
)
2259 if ( !subd
) return;
2261 for (i
= 0; i
< subd
->sub_num
; i
++)
2262 for (j
= 0; j
< subd
->subtitles
[i
].lines
; j
++)
2263 free( subd
->subtitles
[i
].text
[j
] );
2264 free( subd
->subtitles
);
2265 free( subd
->filename
);
2269 #define MAX_SUBLINE 512
2271 * \brief parse text and append it to subtitle in sub
2272 * \param sub subtitle struct to add text to
2273 * \param txt text to parse
2274 * \param len length of text in txt
2275 * \param endpts pts at which this subtitle text should be removed again
2277 * <> and {} are interpreted as comment delimiters, "\n", "\N", '\n', '\r'
2278 * and '\0' are interpreted as newlines, duplicate, leading and trailing
2279 * newlines are ignored.
2281 void sub_add_text(subtitle
*sub
, const char *txt
, int len
, double endpts
) {
2283 int double_newline
= 1; // ignore newlines at the beginning
2286 int orig_lines
= sub
->lines
;
2287 if (sub
->lines
>= SUB_MAX_TEXT
) return;
2289 buf
= malloc(MAX_SUBLINE
+ 1);
2290 sub
->text
[sub
->lines
] = buf
;
2291 sub
->endpts
[sub
->lines
] = endpts
;
2292 for (i
= 0; i
< len
&& pos
< MAX_SUBLINE
; i
++) {
2294 if (c
== '<') comment
|= 1;
2295 if (c
== '{') comment
|= 2;
2297 if (c
== '}') comment
&= ~2;
2298 if (c
== '>') comment
&= ~1;
2301 if (pos
== MAX_SUBLINE
- 1) {
2305 if (c
== '\\' && i
+ 1 < len
) {
2307 if (c
== 'n' || c
== 'N') c
= 0;
2309 if (c
== '\n' || c
== '\r') c
= 0;
2313 } else if (!double_newline
) {
2314 if (sub
->lines
>= SUB_MAX_TEXT
- 1) {
2315 mp_msg(MSGT_VO
, MSGL_WARN
, "Too many subtitle lines\n");
2322 buf
= malloc(MAX_SUBLINE
+ 1);
2323 sub
->text
[sub
->lines
] = buf
;
2324 sub
->endpts
[sub
->lines
] = endpts
;
2328 if (sub
->lines
< SUB_MAX_TEXT
&&
2329 strlen(sub
->text
[sub
->lines
]))
2331 #ifdef CONFIG_FRIBIDI
2332 sub
= sub_fribidi(sub
, sub_utf8
, orig_lines
);
2336 #define MP_NOPTS_VALUE (-1LL<<63)
2338 * \brief remove outdated subtitle lines.
2339 * \param sub subtitle struct to modify
2340 * \param pts current pts. All lines with endpts <= this will be removed.
2341 * Use MP_NOPTS_VALUE to remove all lines
2342 * \return 1 if sub was modified, 0 otherwise.
2344 int sub_clear_text(subtitle
*sub
, double pts
) {
2347 while (i
< sub
->lines
) {
2348 double endpts
= sub
->endpts
[i
];
2349 if (pts
== MP_NOPTS_VALUE
|| (endpts
!= MP_NOPTS_VALUE
&& pts
>= endpts
)) {
2352 for (j
= i
+ 1; j
< sub
->lines
; j
++) {
2353 sub
->text
[j
- 1] = sub
->text
[j
];
2354 sub
->endpts
[j
- 1] = sub
->endpts
[j
];