kerndeint adaptive deinterlacer
[mplayer/greg.git] / subreader.c
blob078b53f228f8ed8db23893d0e4699e6ca55bd002
1 /*
2 * Subtitle reader with format autodetection
4 * Written by laaz
5 * Some code cleanup & realloc() by A'rpi/ESP-team
6 * dunnowhat sub format by szabi
7 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
15 #include <sys/types.h>
16 #include <dirent.h>
18 #include "config.h"
19 #include "mp_msg.h"
20 #include "subreader.h"
22 #define ERR ((void *) -1)
24 #ifdef USE_ICONV
25 #include <iconv.h>
26 char *sub_cp=NULL;
27 #endif
28 #ifdef USE_FRIBIDI
29 #include <fribidi/fribidi.h>
30 char *fribidi_charset = NULL;
31 int flip_hebrew = 1;
32 #endif
34 extern char* dvdsub_lang;
36 /* Maximal length of line of a subtitle */
37 #define LINE_LEN 1000
38 static float mpsub_position=0;
39 static float mpsub_multiplier=1.;
40 static int sub_slacktime = 20000; //20 sec
42 int sub_no_text_pp=0; // 1 => do not apply text post-processing
43 // like {\...} elimination in SSA format.
45 int sub_match_fuzziness=0; // level of sub name matching fuzziness
47 /* Use the SUB_* constant defined in the header file */
48 int sub_format=SUB_INVALID;
49 #ifdef USE_SORTSUB
50 /*
51 Some subtitling formats, namely AQT and Subrip09, define the end of a
52 subtitle as the beginning of the following. Since currently we read one
53 subtitle at time, for these format we keep two global *subtitle,
54 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
55 so we can change its end when we read current subtitle starting time.
56 When USE_SORTSUB is defined, we use a single global unsigned long,
57 previous_sub_end, for both (and even future) formats, to store the end of
58 the previous sub: it is initialized to 0 in sub_read_file and eventually
59 modified by sub_read_aqt_line or sub_read_subrip09_line.
61 unsigned long previous_sub_end;
62 #endif
64 static int eol(char p) {
65 return (p=='\r' || p=='\n' || p=='\0');
68 /* Remove leading and trailing space */
69 static void trail_space(char *s) {
70 int i = 0;
71 while (isspace(s[i])) ++i;
72 if (i) strcpy(s, s + i);
73 i = strlen(s) - 1;
74 while (i > 0 && isspace(s[i])) s[i--] = '\0';
77 static char *stristr(const char *haystack, const char *needle) {
78 int len = 0;
79 const char *p = haystack;
81 if (!(haystack && needle)) return NULL;
83 len=strlen(needle);
84 while (*p != '\0') {
85 if (strncasecmp(p, needle, len) == 0) return (char*)p;
86 p++;
89 return NULL;
92 subtitle *sub_read_line_sami(FILE *fd, subtitle *current) {
93 static char line[LINE_LEN+1];
94 static char *s = NULL, *slacktime_s;
95 char text[LINE_LEN+1], *p=NULL, *q;
96 int state;
98 current->lines = current->start = current->end = 0;
99 state = 0;
101 /* read the first line */
102 if (!s)
103 if (!(s = fgets(line, LINE_LEN, fd))) return 0;
105 do {
106 switch (state) {
108 case 0: /* find "START=" or "Slacktime:" */
109 slacktime_s = stristr (s, "Slacktime:");
110 if (slacktime_s)
111 sub_slacktime = strtol (slacktime_s+10, NULL, 0) / 10;
113 s = stristr (s, "Start=");
114 if (s) {
115 current->start = strtol (s + 6, &s, 0) / 10;
116 /* eat '>' */
117 for (; *s != '>' && *s != '\0'; s++);
118 s++;
119 state = 1; continue;
121 break;
123 case 1: /* find (optionnal) "<P", skip other TAGs */
124 for (; *s == ' ' || *s == '\t'; s++); /* strip blanks, if any */
125 if (*s == '\0') break;
126 if (*s != '<') { state = 3; p = text; continue; } /* not a TAG */
127 s++;
128 if (*s == 'P' || *s == 'p') { s++; state = 2; continue; } /* found '<P' */
129 for (; *s != '>' && *s != '\0'; s++); /* skip remains of non-<P> TAG */
130 if (s == '\0')
131 break;
132 s++;
133 continue;
135 case 2: /* find ">" */
136 if ((s = strchr (s, '>'))) { s++; state = 3; p = text; continue; }
137 break;
139 case 3: /* get all text until '<' appears */
140 if (*s == '\0') break;
141 else if (!strncasecmp (s, "<br>", 4)) {
142 *p = '\0'; p = text; trail_space (text);
143 if (text[0] != '\0')
144 current->text[current->lines++] = strdup (text);
145 s += 4;
147 else if (*s == '<') { state = 4; }
148 else if (!strncasecmp (s, "&nbsp;", 6)) { *p++ = ' '; s += 6; }
149 else if (*s == '\t') { *p++ = ' '; s++; }
150 else if (*s == '\r' || *s == '\n') { s++; }
151 else *p++ = *s++;
153 /* skip duplicated space */
154 if (p > text + 2) if (*(p-1) == ' ' && *(p-2) == ' ') p--;
156 continue;
158 case 4: /* get current->end or skip <TAG> */
159 q = stristr (s, "Start=");
160 if (q) {
161 current->end = strtol (q + 6, &q, 0) / 10 - 1;
162 *p = '\0'; trail_space (text);
163 if (text[0] != '\0')
164 current->text[current->lines++] = strdup (text);
165 if (current->lines > 0) { state = 99; break; }
166 state = 0; continue;
168 s = strchr (s, '>');
169 if (s) { s++; state = 3; continue; }
170 break;
173 /* read next line */
174 if (state != 99 && !(s = fgets (line, LINE_LEN, fd))) {
175 if (current->start > 0) {
176 break; // if it is the last subtitle
177 } else {
178 return 0;
182 } while (state != 99);
184 // For the last subtitle
185 if (current->end <= 0) {
186 current->end = current->start + sub_slacktime;
187 *p = '\0'; trail_space (text);
188 if (text[0] != '\0')
189 current->text[current->lines++] = strdup (text);
192 return current;
196 char *sub_readtext(char *source, char **dest) {
197 int len=0;
198 char *p=source;
200 // printf("src=%p dest=%p \n",source,dest);
202 while ( !eol(*p) && *p!= '|' ) {
203 p++,len++;
206 *dest= (char *)malloc (len+1);
207 if (!dest) {return ERR;}
209 strncpy(*dest, source, len);
210 (*dest)[len]=0;
212 while (*p=='\r' || *p=='\n' || *p=='|') p++;
214 if (*p) return p; // not-last text field
215 else return NULL; // last text field
218 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) {
219 char line[LINE_LEN+1];
220 char line2[LINE_LEN+1];
221 char *p, *next;
222 int i;
224 do {
225 if (!fgets (line, LINE_LEN, fd)) return NULL;
226 } while ((sscanf (line,
227 "{%ld}{}%[^\r\n]",
228 &(current->start), line2) < 2) &&
229 (sscanf (line,
230 "{%ld}{%ld}%[^\r\n]",
231 &(current->start), &(current->end), line2) < 3));
233 p=line2;
235 next=p, i=0;
236 while ((next =sub_readtext (next, &(current->text[i])))) {
237 if (current->text[i]==ERR) {return ERR;}
238 i++;
239 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
241 current->lines= ++i;
243 return current;
246 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) {
247 char line[LINE_LEN+1];
248 int a1,a2,a3,a4,b1,b2,b3,b4;
249 char *p=NULL, *q=NULL;
250 int len;
252 while (1) {
253 if (!fgets (line, LINE_LEN, fd)) return NULL;
254 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
255 current->start = a1*360000+a2*6000+a3*100+a4;
256 current->end = b1*360000+b2*6000+b3*100+b4;
258 if (!fgets (line, LINE_LEN, fd)) return NULL;
260 p=q=line;
261 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) {
262 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && *p!='|' && strncmp(p,"[br]",4); p++,len++);
263 current->text[current->lines-1]=(char *)malloc (len+1);
264 if (!current->text[current->lines-1]) return ERR;
265 strncpy (current->text[current->lines-1], q, len);
266 current->text[current->lines-1][len]='\0';
267 if (!*p || *p=='\r' || *p=='\n') break;
268 if (*p=='|') p++;
269 else while (*p++!=']');
271 break;
273 return current;
276 subtitle *sub_read_line_subviewer(FILE *fd,subtitle *current) {
277 char line[LINE_LEN+1];
278 int a1,a2,a3,a4,b1,b2,b3,b4;
279 char *p=NULL;
280 int i,len;
282 while (!current->text[0]) {
283 if (!fgets (line, LINE_LEN, fd)) return NULL;
284 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)
285 continue;
286 current->start = a1*360000+a2*6000+a3*100+a4/10;
287 current->end = b1*360000+b2*6000+b3*100+b4/10;
288 for (i=0; i<SUB_MAX_TEXT;) {
289 if (!fgets (line, LINE_LEN, fd)) break;
290 len=0;
291 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
292 if (len) {
293 int j=0,skip=0;
294 char *curptr=current->text[i]=(char *)malloc (len+1);
295 if (!current->text[i]) return ERR;
296 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
297 for(; j<len; j++) {
298 /* let's filter html tags ::atmos */
299 if(line[j]=='>') {
300 skip=0;
301 continue;
303 if(line[j]=='<') {
304 skip=1;
305 continue;
307 if(skip) {
308 continue;
310 *curptr=line[j];
311 curptr++;
313 *curptr='\0';
315 i++;
316 } else {
317 break;
320 current->lines=i;
322 return current;
325 subtitle *sub_read_line_subviewer2(FILE *fd,subtitle *current) {
326 char line[LINE_LEN+1];
327 int a1,a2,a3,a4;
328 char *p=NULL;
329 int i,len;
331 while (!current->text[0]) {
332 if (!fgets (line, LINE_LEN, fd)) return NULL;
333 if (line[0]!='{')
334 continue;
335 if ((len=sscanf (line, "{T %d:%d:%d:%d",&a1,&a2,&a3,&a4)) < 4)
336 continue;
337 current->start = a1*360000+a2*6000+a3*100+a4/10;
338 for (i=0; i<SUB_MAX_TEXT;) {
339 if (!fgets (line, LINE_LEN, fd)) break;
340 if (line[0]=='}') break;
341 len=0;
342 for (p=line; *p!='\n' && *p!='\r' && *p; ++p,++len);
343 if (len) {
344 current->text[i]=(char *)malloc (len+1);
345 if (!current->text[i]) return ERR;
346 strncpy (current->text[i], line, len); current->text[i][len]='\0';
347 ++i;
348 } else {
349 break;
352 current->lines=i;
354 return current;
358 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) {
359 char line[LINE_LEN+1];
360 int a1,a2,a3;
361 char *p=NULL, *next,separator;
362 int i,len,plen;
364 while (!current->text[0]) {
365 if (!fgets (line, LINE_LEN, fd)) return NULL;
366 if ((len=sscanf (line, "%d:%d:%d%c%n",&a1,&a2,&a3,&separator,&plen)) < 4)
367 continue;
369 if (!(current->start = a1*360000+a2*6000+a3*100))
370 continue;
371 /* removed by wodzu
372 p=line;
373 // finds the body of the subtitle
374 for (i=0; i<3; i++){
375 p=strchr(p,':');
376 if (p==NULL) break;
377 ++p;
379 if (p==NULL) {
380 printf("SUB: Skipping incorrect subtitle line!\n");
381 continue;
384 // by wodzu: hey! this time we know what length it has! what is
385 // that magic for? it can't deal with space instead of third
386 // colon! look, what simple it can be:
387 p = &line[ plen ];
389 i=0;
390 if (*p!='|') {
392 next = p,i=0;
393 while ((next =sub_readtext (next, &(current->text[i])))) {
394 if (current->text[i]==ERR) {return ERR;}
395 i++;
396 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
398 current->lines=i+1;
401 return current;
404 subtitle *sub_read_line_rt(FILE *fd,subtitle *current) {
405 //TODO: This format uses quite rich (sub/super)set of xhtml
406 // I couldn't check it since DTD is not included.
407 // WARNING: full XML parses can be required for proper parsing
408 char line[LINE_LEN+1];
409 int a1,a2,a3,a4,b1,b2,b3,b4;
410 char *p=NULL,*next=NULL;
411 int i,len,plen;
413 while (!current->text[0]) {
414 if (!fgets (line, LINE_LEN, fd)) return NULL;
415 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
416 //to describe the same moment in time. Maybe there are even more formats in use.
417 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
418 plen=a1=a2=a3=a4=b1=b2=b3=b4=0;
419 if (
420 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b3,&b4,&plen)) < 4) &&
421 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b2,&b3,&b4,&plen)) < 5) &&
422 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) &&
423 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) &&
424 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
425 ((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) &&
426 ((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) &&
427 //now try it without end time
428 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&plen)) < 2) &&
429 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&plen)) < 2) &&
430 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&plen)) < 3) &&
431 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&plen)) < 4)
433 continue;
434 current->start = a1*360000+a2*6000+a3*100+a4/10;
435 current->end = b1*360000+b2*6000+b3*100+b4/10;
436 if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)
437 current->end = current->start+200;
438 p=line; p+=plen;i=0;
439 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
440 next = strstr(line,"<clear/>");
441 if(next && strlen(next)>8){
442 next+=8;i=0;
443 while ((next =sub_readtext (next, &(current->text[i])))) {
444 if (current->text[i]==ERR) {return ERR;}
445 i++;
446 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
449 current->lines=i+1;
451 return current;
454 subtitle *sub_read_line_ssa(FILE *fd,subtitle *current) {
456 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
457 * other Sub Station Alpha scripts have only 8 commas before subtitle
458 * Reading the "ScriptType:" field is not reliable since many scripts appear
459 * w/o it
461 * http://www.scriptclub.org is a good place to find more examples
462 * http://www.eswat.demon.co.uk is where the SSA specs can be found
464 int comma;
465 static int max_comma = 32; /* let's use 32 for the case that the */
466 /* amount of commas increase with newer SSA versions */
468 int hour1, min1, sec1, hunsec1,
469 hour2, min2, sec2, hunsec2, nothing;
470 int num;
472 char line[LINE_LEN+1],
473 line3[LINE_LEN+1],
474 *line2;
475 char *tmp;
477 do {
478 if (!fgets (line, LINE_LEN, fd)) return NULL;
479 } while (sscanf (line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
480 "%[^\n\r]", &nothing,
481 &hour1, &min1, &sec1, &hunsec1,
482 &hour2, &min2, &sec2, &hunsec2,
483 line3) < 9
485 sscanf (line, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
486 "%[^\n\r]", &nothing,
487 &hour1, &min1, &sec1, &hunsec1,
488 &hour2, &min2, &sec2, &hunsec2,
489 line3) < 9 );
491 line2=strchr(line3, ',');
493 for (comma = 4; comma < max_comma; comma ++)
495 tmp = line2;
496 if(!(tmp=strchr(++tmp, ','))) break;
497 if(*(++tmp) == ' ') break;
498 /* a space after a comma means we're already in a sentence */
499 line2 = tmp;
502 if(comma < max_comma)max_comma = comma;
503 /* eliminate the trailing comma */
504 if(*line2 == ',') line2++;
506 current->lines=0;num=0;
507 current->start = 360000*hour1 + 6000*min1 + 100*sec1 + hunsec1;
508 current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2;
510 while (((tmp=strstr(line2, "\\n")) != NULL) || ((tmp=strstr(line2, "\\N")) != NULL) ){
511 current->text[num]=(char *)malloc(tmp-line2+1);
512 strncpy (current->text[num], line2, tmp-line2);
513 current->text[num][tmp-line2]='\0';
514 line2=tmp+2;
515 num++;
516 current->lines++;
517 if (current->lines >= SUB_MAX_TEXT) return current;
520 current->text[num]=strdup(line2);
521 current->lines++;
523 return current;
526 void sub_pp_ssa(subtitle *sub) {
527 int l=sub->lines;
528 char *so,*de,*start;
530 while (l){
531 /* eliminate any text enclosed with {}, they are font and color settings */
532 so=de=sub->text[--l];
533 while (*so) {
534 if(*so == '{' && so[1]=='\\') {
535 for (start=so; *so && *so!='}'; so++);
536 if(*so) so++; else so=start;
538 if(*so) {
539 *de=*so;
540 so++; de++;
543 *de=*so;
547 subtitle *sub_read_line_dunnowhat(FILE *fd,subtitle *current) {
548 char line[LINE_LEN+1];
549 char text[LINE_LEN+1];
551 if (!fgets (line, LINE_LEN, fd))
552 return NULL;
553 if (sscanf (line, "%ld,%ld,\"%[^\"]", &(current->start),
554 &(current->end), text) <3)
555 return ERR;
556 current->text[0] = strdup(text);
557 current->lines = 1;
559 return current;
562 subtitle *sub_read_line_mpsub(FILE *fd, subtitle *current) {
563 char line[LINE_LEN+1];
564 float a,b;
565 int num=0;
566 char *p, *q;
570 if (!fgets(line, LINE_LEN, fd)) return NULL;
571 } while (sscanf (line, "%f %f", &a, &b) !=2);
573 mpsub_position += a*mpsub_multiplier;
574 current->start=(int) mpsub_position;
575 mpsub_position += b*mpsub_multiplier;
576 current->end=(int) mpsub_position;
578 while (num < SUB_MAX_TEXT) {
579 if (!fgets (line, LINE_LEN, fd)) {
580 if (num == 0) return NULL;
581 else return current;
583 p=line;
584 while (isspace(*p)) p++;
585 if (eol(*p) && num > 0) return current;
586 if (eol(*p)) return NULL;
588 for (q=p; !eol(*q); q++);
589 *q='\0';
590 if (strlen(p)) {
591 current->text[num]=strdup(p);
592 // printf (">%s<\n",p);
593 current->lines = ++num;
594 } else {
595 if (num) return current;
596 else return NULL;
599 return NULL; // we should have returned before if it's OK
602 #ifndef USE_SORTSUB
603 //we don't need this if we use previous_sub_end
604 subtitle *previous_aqt_sub = NULL;
605 #endif
607 subtitle *sub_read_line_aqt(FILE *fd,subtitle *current) {
608 char line[LINE_LEN+1];
609 char *next;
610 int i;
612 while (1) {
613 // try to locate next subtitle
614 if (!fgets (line, LINE_LEN, fd))
615 return NULL;
616 if (!(sscanf (line, "-->> %ld", &(current->start)) <1))
617 break;
620 #ifdef USE_SORTSUB
621 previous_sub_end = (current->start) ? current->start - 1 : 0;
622 #else
623 if (previous_aqt_sub != NULL)
624 previous_aqt_sub->end = current->start-1;
626 previous_aqt_sub = current;
627 #endif
629 if (!fgets (line, LINE_LEN, fd))
630 return NULL;
632 sub_readtext((char *) &line,&current->text[0]);
633 current->lines = 1;
634 current->end = current->start; // will be corrected by next subtitle
636 if (!fgets (line, LINE_LEN, fd))
637 return current;
639 next = line,i=1;
640 while ((next =sub_readtext (next, &(current->text[i])))) {
641 if (current->text[i]==ERR) {return ERR;}
642 i++;
643 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
645 current->lines=i+1;
647 if ((current->text[0]=="") && (current->text[1]=="")) {
648 #ifdef USE_SORTSUB
649 previous_sub_end = 0;
650 #else
651 // void subtitle -> end of previous marked and exit
652 previous_aqt_sub = NULL;
653 #endif
654 return NULL;
657 return current;
660 #ifndef USE_SORTSUB
661 subtitle *previous_subrip09_sub = NULL;
662 #endif
664 subtitle *sub_read_line_subrip09(FILE *fd,subtitle *current) {
665 char line[LINE_LEN+1];
666 int a1,a2,a3;
667 char * next=NULL;
668 int i,len;
670 while (1) {
671 // try to locate next subtitle
672 if (!fgets (line, LINE_LEN, fd))
673 return NULL;
674 if (!((len=sscanf (line, "[%d:%d:%d]",&a1,&a2,&a3)) < 3))
675 break;
678 current->start = a1*360000+a2*6000+a3*100;
680 #ifdef USE_SORTSUB
681 previous_sub_end = (current->start) ? current->start - 1 : 0;
682 #else
683 if (previous_subrip09_sub != NULL)
684 previous_subrip09_sub->end = current->start-1;
686 previous_subrip09_sub = current;
687 #endif
689 if (!fgets (line, LINE_LEN, fd))
690 return NULL;
692 next = line,i=0;
694 current->text[0]=""; // just to be sure that string is clear
696 while ((next =sub_readtext (next, &(current->text[i])))) {
697 if (current->text[i]==ERR) {return ERR;}
698 i++;
699 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
701 current->lines=i+1;
703 if ((current->text[0]=="") && (i==0)) {
704 #ifdef USE_SORTSUB
705 previous_sub_end = 0;
706 #else
707 // void subtitle -> end of previous marked and exit
708 previous_subrip09_sub = NULL;
709 #endif
710 return NULL;
713 return current;
716 subtitle *sub_read_line_jacosub(FILE * fd, subtitle * current)
718 char line1[LINE_LEN], line2[LINE_LEN], directive[LINE_LEN], *p, *q;
719 unsigned a1, a2, a3, a4, b1, b2, b3, b4, comment = 0;
720 static unsigned jacoTimeres = 30;
721 static int jacoShift = 0;
723 bzero(current, sizeof(subtitle));
724 bzero(line1, LINE_LEN);
725 bzero(line2, LINE_LEN);
726 bzero(directive, LINE_LEN);
727 while (!current->text[0]) {
728 if (!fgets(line1, LINE_LEN, fd)) {
729 return NULL;
731 if (sscanf
732 (line1, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1, &a2, &a3, &a4,
733 &b1, &b2, &b3, &b4, line2) < 9) {
734 if (sscanf(line1, "@%u @%u %[^\n\r]", &a4, &b4, line2) < 3) {
735 if (line1[0] == '#') {
736 int hours = 0, minutes = 0, seconds, delta, inverter =
738 unsigned units = jacoShift;
739 switch (toupper(line1[1])) {
740 case 'S':
741 if (isalpha(line1[2])) {
742 delta = 6;
743 } else {
744 delta = 2;
746 if (sscanf(&line1[delta], "%d", &hours)) {
747 if (hours < 0) {
748 hours *= -1;
749 inverter = -1;
751 if (sscanf(&line1[delta], "%*d:%d", &minutes)) {
752 if (sscanf
753 (&line1[delta], "%*d:%*d:%d",
754 &seconds)) {
755 sscanf(&line1[delta], "%*d:%*d:%*d.%d",
756 &units);
757 } else {
758 hours = 0;
759 sscanf(&line1[delta], "%d:%d.%d",
760 &minutes, &seconds, &units);
761 minutes *= inverter;
763 } else {
764 hours = minutes = 0;
765 sscanf(&line1[delta], "%d.%d", &seconds,
766 &units);
767 seconds *= inverter;
769 jacoShift =
770 ((hours * 3600 + minutes * 60 +
771 seconds) * jacoTimeres +
772 units) * inverter;
774 break;
775 case 'T':
776 if (isalpha(line1[2])) {
777 delta = 8;
778 } else {
779 delta = 2;
781 sscanf(&line1[delta], "%u", &jacoTimeres);
782 break;
785 continue;
786 } else {
787 current->start =
788 (unsigned long) ((a4 + jacoShift) * 100.0 /
789 jacoTimeres);
790 current->end =
791 (unsigned long) ((b4 + jacoShift) * 100.0 /
792 jacoTimeres);
794 } else {
795 current->start =
796 (unsigned
797 long) (((a1 * 3600 + a2 * 60 + a3) * jacoTimeres + a4 +
798 jacoShift) * 100.0 / jacoTimeres);
799 current->end =
800 (unsigned
801 long) (((b1 * 3600 + b2 * 60 + b3) * jacoTimeres + b4 +
802 jacoShift) * 100.0 / jacoTimeres);
804 current->lines = 0;
805 p = line2;
806 while ((*p == ' ') || (*p == '\t')) {
807 ++p;
809 if (isalpha(*p)||*p == '[') {
810 int cont, jLength;
812 if (sscanf(p, "%s %[^\n\r]", directive, line1) < 2)
813 return (subtitle *) ERR;
814 jLength = strlen(directive);
815 for (cont = 0; cont < jLength; ++cont) {
816 if (isalpha(*(directive + cont)))
817 *(directive + cont) = toupper(*(directive + cont));
819 if ((strstr(directive, "RDB") != NULL)
820 || (strstr(directive, "RDC") != NULL)
821 || (strstr(directive, "RLB") != NULL)
822 || (strstr(directive, "RLG") != NULL)) {
823 continue;
825 if (strstr(directive, "JL") != NULL) {
826 current->alignment = SUB_ALIGNMENT_HLEFT;
827 } else if (strstr(directive, "JR") != NULL) {
828 current->alignment = SUB_ALIGNMENT_HRIGHT;
829 } else {
830 current->alignment = SUB_ALIGNMENT_HCENTER;
832 strcpy(line2, line1);
833 p = line2;
835 for (q = line1; (!eol(*p)) && (current->lines < SUB_MAX_TEXT); ++p) {
836 switch (*p) {
837 case '{':
838 comment++;
839 break;
840 case '}':
841 if (comment) {
842 --comment;
843 //the next line to get rid of a blank after the comment
844 if ((*(p + 1)) == ' ')
845 p++;
847 break;
848 case '~':
849 if (!comment) {
850 *q = ' ';
851 ++q;
853 break;
854 case ' ':
855 case '\t':
856 if ((*(p + 1) == ' ') || (*(p + 1) == '\t'))
857 break;
858 if (!comment) {
859 *q = ' ';
860 ++q;
862 break;
863 case '\\':
864 if (*(p + 1) == 'n') {
865 *q = '\0';
866 q = line1;
867 current->text[current->lines++] = strdup(line1);
868 ++p;
869 break;
871 if ((toupper(*(p + 1)) == 'C')
872 || (toupper(*(p + 1)) == 'F')) {
873 ++p,++p;
874 break;
876 if ((*(p + 1) == 'B') || (*(p + 1) == 'b') || (*(p + 1) == 'D') || //actually this means "insert current date here"
877 (*(p + 1) == 'I') || (*(p + 1) == 'i') || (*(p + 1) == 'N') || (*(p + 1) == 'T') || //actually this means "insert current time here"
878 (*(p + 1) == 'U') || (*(p + 1) == 'u')) {
879 ++p;
880 break;
882 if ((*(p + 1) == '\\') ||
883 (*(p + 1) == '~') || (*(p + 1) == '{')) {
884 ++p;
885 } else if (eol(*(p + 1))) {
886 if (!fgets(directive, LINE_LEN, fd))
887 return NULL;
888 trail_space(directive);
889 strncat(line2, directive,
890 (LINE_LEN > 511) ? LINE_LEN : 511);
891 break;
893 default:
894 if (!comment) {
895 *q = *p;
896 ++q;
898 } //-- switch
899 } //-- for
900 *q = '\0';
901 current->text[current->lines] = strdup(line1);
902 } //-- while
903 current->lines++;
904 return current;
907 int sub_autodetect (FILE *fd, int *uses_time) {
908 char line[LINE_LEN+1];
909 int i,j=0;
910 char p;
912 while (j < 100) {
913 j++;
914 if (!fgets (line, LINE_LEN, fd))
915 return SUB_INVALID;
917 if (sscanf (line, "{%d}{%d}", &i, &i)==2)
918 {*uses_time=0;return SUB_MICRODVD;}
919 if (sscanf (line, "{%d}{}", &i)==1)
920 {*uses_time=0;return SUB_MICRODVD;}
921 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
922 {*uses_time=1;return SUB_SUBRIP;}
923 if (sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i, &i, &i, (char *)&i, &i, &i, &i, &i, (char *)&i, &i)==10)
924 {*uses_time=1;return SUB_SUBVIEWER;}
925 if (sscanf (line, "{T %d:%d:%d:%d",&i, &i, &i, &i)==4)
926 {*uses_time=1;return SUB_SUBVIEWER2;}
927 if (strstr (line, "<SAMI>"))
928 {*uses_time=1; return SUB_SAMI;}
929 if (sscanf(line, "%d:%d:%d.%d %d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i) == 8)
930 {*uses_time = 1; return SUB_JACOSUB;}
931 if (sscanf(line, "@%d @%d", &i, &i) == 2)
932 {*uses_time = 1; return SUB_JACOSUB;}
933 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3)
934 {*uses_time=1;return SUB_VPLAYER;}
935 if (sscanf (line, "%d:%d:%d ", &i, &i, &i )==3)
936 {*uses_time=1;return SUB_VPLAYER;}
937 //TODO: just checking if first line of sub starts with "<" is WAY
938 // too weak test for RT
939 // Please someone who knows the format of RT... FIX IT!!!
940 // It may conflict with other sub formats in the future (actually it doesn't)
941 if ( *line == '<' )
942 {*uses_time=1;return SUB_RT;}
944 if (!memcmp(line, "Dialogue: Marked", 16))
945 {*uses_time=1; return SUB_SSA;}
946 if (!memcmp(line, "Dialogue: ", 10))
947 {*uses_time=1; return SUB_SSA;}
948 if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3)
949 {*uses_time=0;return SUB_DUNNOWHAT;}
950 if (sscanf (line, "FORMAT=%d", &i) == 1)
951 {*uses_time=0; return SUB_MPSUB;}
952 if (sscanf (line, "FORMAT=TIM%c", &p)==1 && p=='E')
953 {*uses_time=1; return SUB_MPSUB;}
954 if (strstr (line, "-->>"))
955 {*uses_time=0; return SUB_AQTITLE;}
956 if (sscanf (line, "[%d:%d:%d]", &i, &i, &i)==3)
957 {*uses_time=1;return SUB_SUBRIP09;}
960 return SUB_INVALID; // too many bad lines
963 #ifdef DUMPSUBS
964 int sub_utf8=0;
965 #else
966 extern int sub_utf8;
967 int sub_utf8_prev=0;
968 #endif
970 extern float sub_delay;
971 extern float sub_fps;
973 #ifdef USE_ICONV
974 static iconv_t icdsc = (iconv_t)(-1);
976 void subcp_open (void)
978 char *tocp = "UTF-8";
980 if (sub_cp){
981 if ((icdsc = iconv_open (tocp, sub_cp)) != (iconv_t)(-1)){
982 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: opened iconv descriptor.\n");
983 sub_utf8 = 2;
984 } else
985 mp_msg(MSGT_SUBREADER,MSGL_ERR,"SUB: error opening iconv descriptor.\n");
989 void subcp_close (void)
991 if (icdsc != (iconv_t)(-1)){
992 (void) iconv_close (icdsc);
993 icdsc = (iconv_t)(-1);
994 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: closed iconv descriptor.\n");
998 #define ICBUFFSIZE 512
999 static char icbuffer[ICBUFFSIZE];
1001 subtitle* subcp_recode (subtitle *sub)
1003 int l=sub->lines;
1004 size_t ileft, oleft;
1005 char *op, *ip, *ot;
1007 while (l){
1008 op = icbuffer;
1009 ip = sub->text[--l];
1010 ileft = strlen(ip);
1011 oleft = ICBUFFSIZE - 1;
1013 if (iconv(icdsc, &ip, &ileft,
1014 &op, &oleft) == (size_t)(-1)) {
1015 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error recoding line (1).\n");
1016 l++;
1017 break;
1019 if (!(ot = (char *)malloc(op - icbuffer + 1))){
1020 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1021 l++;
1022 break;
1024 *op='\0' ;
1025 strcpy (ot, icbuffer);
1026 free (sub->text[l]);
1027 sub->text[l] = ot;
1029 if (l){
1030 for (l = sub->lines; l;)
1031 free (sub->text[--l]);
1032 return ERR;
1034 return sub;
1037 // for demux_ogg.c:
1038 subtitle* subcp_recode1 (subtitle *sub)
1040 int l=sub->lines;
1041 size_t ileft, oleft;
1043 if(icdsc == (iconv_t)(-1)) return sub;
1045 while (l){
1046 char *ip = icbuffer;
1047 char *op = sub->text[--l];
1048 strcpy(ip, op);
1049 ileft = strlen(ip);
1050 oleft = ICBUFFSIZE - 1;
1052 if (iconv(icdsc, &ip, &ileft,
1053 &op, &oleft) == (size_t)(-1)) {
1054 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: error recoding line (2).\n");
1055 return sub;
1057 *op='\0' ;
1059 return sub;
1061 #endif
1063 #ifdef USE_FRIBIDI
1064 #ifndef max
1065 #define max(a,b) (((a)>(b))?(a):(b))
1066 #endif
1067 subtitle* sub_fribidi (subtitle *sub, int sub_utf8)
1069 FriBidiChar logical[LINE_LEN+1], visual[LINE_LEN+1]; // Hopefully these two won't smash the stack
1070 char *ip = NULL, *op = NULL;
1071 FriBidiCharType base;
1072 size_t len,orig_len;
1073 int l=sub->lines;
1074 int char_set_num;
1075 fribidi_boolean log2vis;
1076 if(flip_hebrew) { // Please fix the indentation someday
1077 fribidi_set_mirroring (FRIBIDI_TRUE);
1078 fribidi_set_reorder_nsm (FRIBIDI_FALSE);
1080 if( sub_utf8 == 0 ) {
1081 char_set_num = fribidi_parse_charset (fribidi_charset?fribidi_charset:"ISO8859-8");
1082 }else {
1083 char_set_num = fribidi_parse_charset ("UTF-8");
1085 while (l) {
1086 ip = sub->text[--l];
1087 orig_len = len = strlen( ip ); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1088 if(len > LINE_LEN) {
1089 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: sub->text is longer than LINE_LEN.\n");
1090 l++;
1091 break;
1093 len = fribidi_charset_to_unicode (char_set_num, ip, len, logical);
1094 base = FRIBIDI_TYPE_ON;
1095 log2vis = fribidi_log2vis (logical, len, &base,
1096 /* output */
1097 visual, NULL, NULL, NULL);
1098 if(log2vis) {
1099 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL,
1100 NULL);
1101 if((op = (char*)malloc(sizeof(char)*(max(2*orig_len,2*len) + 1))) == NULL) {
1102 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1103 l++;
1104 break;
1106 fribidi_unicode_to_charset ( char_set_num, visual, len,op);
1107 free (ip);
1108 sub->text[l] = op;
1111 if (l){
1112 for (l = sub->lines; l;)
1113 free (sub->text[--l]);
1114 return ERR;
1117 return sub;
1120 #endif
1122 static void adjust_subs_time(subtitle* sub, float subtime, float fps, int block,
1123 int sub_num, int sub_uses_time) {
1124 int n,m;
1125 subtitle* nextsub;
1126 int i = sub_num;
1127 unsigned long subfms = (sub_uses_time ? 100 : fps) * subtime;
1128 unsigned long overlap = (sub_uses_time ? 100 : fps) / 5; // 0.2s
1130 n=m=0;
1131 if (i) for (;;){
1132 if (sub->end <= sub->start){
1133 sub->end = sub->start + subfms;
1134 m++;
1135 n++;
1137 if (!--i) break;
1138 nextsub = sub + 1;
1139 if(block){
1140 if ((sub->end > nextsub->start) && (sub->end <= nextsub->start + overlap)) {
1141 // these subtitles overlap for less than 0.2 seconds
1142 // and would result in very short overlapping subtitle
1143 // so let's fix the problem here, before overlapping code
1144 // get its hands on them
1145 unsigned delta = sub->end - nextsub->start, half = delta / 2;
1146 sub->end -= half + 1;
1147 nextsub->start += delta - half;
1149 if (sub->end >= nextsub->start){
1150 sub->end = nextsub->start - 1;
1151 if (sub->end - sub->start > subfms)
1152 sub->end = sub->start + subfms;
1153 if (!m)
1154 n++;
1158 /* Theory:
1159 * Movies are often converted from FILM (24 fps)
1160 * to PAL (25) by simply speeding it up, so we
1161 * to multiply the original timestmaps by
1162 * (Movie's FPS / Subtitle's (guessed) FPS)
1163 * so eg. for 23.98 fps movie and PAL time based
1164 * subtitles we say -subfps 25 and we're fine!
1167 /* timed sub fps correction ::atmos */
1168 if(sub_uses_time && sub_fps) {
1169 sub->start *= sub_fps/fps;
1170 sub->end *= sub_fps/fps;
1173 sub = nextsub;
1174 m = 0;
1176 if (n) mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Adjusted %d subtitle(s).\n", n);
1179 struct subreader {
1180 subtitle * (*read)(FILE *fd,subtitle *dest);
1181 void (*post)(subtitle *dest);
1182 const char *name;
1185 sub_data* sub_read_file (char *filename, float fps) {
1186 //filename is assumed to be malloc'ed, free() is used in sub_free()
1187 FILE *fd;
1188 int n_max, n_first, i, j, sub_first, sub_orig;
1189 subtitle *first, *second, *sub, *return_sub;
1190 sub_data *subt_data;
1191 int uses_time = 0, sub_num = 0, sub_errs = 0;
1192 struct subreader sr[]=
1194 { sub_read_line_microdvd, NULL, "microdvd" },
1195 { sub_read_line_subrip, NULL, "subrip" },
1196 { sub_read_line_subviewer, NULL, "subviewer" },
1197 { sub_read_line_sami, NULL, "sami" },
1198 { sub_read_line_vplayer, NULL, "vplayer" },
1199 { sub_read_line_rt, NULL, "rt" },
1200 { sub_read_line_ssa, sub_pp_ssa, "ssa" },
1201 { sub_read_line_dunnowhat, NULL, "dunnowhat" },
1202 { sub_read_line_mpsub, NULL, "mpsub" },
1203 { sub_read_line_aqt, NULL, "aqt" },
1204 { sub_read_line_subviewer2, NULL, "subviewer 2.0" },
1205 { sub_read_line_subrip09, NULL, "subrip 0.9" },
1206 { sub_read_line_jacosub, NULL, "jacosub" }
1208 struct subreader *srp;
1210 if(filename==NULL) return NULL; //qnx segfault
1211 fd=fopen (filename, "r"); if (!fd) return NULL;
1213 sub_format=sub_autodetect (fd, &uses_time);
1214 mpsub_multiplier = (uses_time ? 100.0 : 1.0);
1215 if (sub_format==SUB_INVALID) {mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: Could not determine file format\n");return NULL;}
1216 srp=sr+sub_format;
1217 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Detected subtitle file format: %s\n", srp->name);
1219 rewind (fd);
1221 #ifdef USE_ICONV
1222 sub_utf8_prev=sub_utf8;
1224 int l,k;
1225 k = -1;
1226 if ((l=strlen(filename))>4){
1227 char *exts[] = {".utf", ".utf8", ".utf-8" };
1228 for (k=3;--k>=0;)
1229 if (!strcasecmp(filename+(l - strlen(exts[k])), exts[k])){
1230 sub_utf8 = 1;
1231 break;
1234 if (k<0) subcp_open();
1236 #endif
1238 sub_num=0;n_max=32;
1239 first=(subtitle *)malloc(n_max*sizeof(subtitle));
1240 if(!first){
1241 #ifdef USE_ICONV
1242 subcp_close();
1243 sub_utf8=sub_utf8_prev;
1244 #endif
1245 return NULL;
1248 #ifdef USE_SORTSUB
1249 sub = (subtitle *)malloc(sizeof(subtitle));
1250 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1251 //as the beginning of the following
1252 previous_sub_end = 0;
1253 #endif
1254 while(1){
1255 if(sub_num>=n_max){
1256 n_max+=16;
1257 first=realloc(first,n_max*sizeof(subtitle));
1259 #ifndef USE_SORTSUB
1260 sub = &first[sub_num];
1261 #endif
1262 memset(sub, '\0', sizeof(subtitle));
1263 sub=srp->read(fd,sub);
1264 if(!sub) break; // EOF
1265 #ifdef USE_ICONV
1266 if ((sub!=ERR) && (sub_utf8 & 2)) sub=subcp_recode(sub);
1267 #endif
1268 #ifdef USE_FRIBIDI
1269 if (sub!=ERR) sub=sub_fribidi(sub,sub_utf8);
1270 #endif
1271 if ( sub == ERR )
1273 #ifdef USE_ICONV
1274 subcp_close();
1275 #endif
1276 if ( first ) free(first);
1277 return NULL;
1279 // Apply any post processing that needs recoding first
1280 if ((sub!=ERR) && !sub_no_text_pp && srp->post) srp->post(sub);
1281 #ifdef USE_SORTSUB
1282 if(!sub_num || (first[sub_num - 1].start <= sub->start)){
1283 first[sub_num].start = sub->start;
1284 first[sub_num].end = sub->end;
1285 first[sub_num].lines = sub->lines;
1286 first[sub_num].alignment = sub->alignment;
1287 for(i = 0; i < sub->lines; ++i){
1288 first[sub_num].text[i] = sub->text[i];
1290 if (previous_sub_end){
1291 first[sub_num - 1].end = previous_sub_end;
1292 previous_sub_end = 0;
1294 } else {
1295 for(j = sub_num - 1; j >= 0; --j){
1296 first[j + 1].start = first[j].start;
1297 first[j + 1].end = first[j].end;
1298 first[j + 1].lines = first[j].lines;
1299 first[j + 1].alignment = first[j].alignment;
1300 for(i = 0; i < first[j].lines; ++i){
1301 first[j + 1].text[i] = first[j].text[i];
1303 if(!j || (first[j - 1].start <= sub->start)){
1304 first[j].start = sub->start;
1305 first[j].end = sub->end;
1306 first[j].lines = sub->lines;
1307 first[j].alignment = sub->alignment;
1308 for(i = 0; i < SUB_MAX_TEXT; ++i){
1309 first[j].text[i] = sub->text[i];
1311 if (previous_sub_end){
1312 first[j].end = first[j - 1].end;
1313 first[j - 1].end = previous_sub_end;
1314 previous_sub_end = 0;
1316 break;
1320 #endif
1321 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
1324 fclose(fd);
1326 #ifdef USE_ICONV
1327 subcp_close();
1328 #endif
1330 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1331 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Read %i subtitles", sub_num);
1332 if (sub_errs) mp_msg(MSGT_SUBREADER,MSGL_INFO,", %i bad line(s).\n", sub_errs);
1333 else mp_msg(MSGT_SUBREADER,MSGL_INFO,".\n");
1335 if(sub_num<=0){
1336 free(first);
1337 return NULL;
1340 // we do overlap if the user forced it (suboverlap_enable == 2) or
1341 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1342 // this is because usually overlapping subtitles are found in these formats,
1343 // while in others they are probably result of bad timing
1344 if ((suboverlap_enabled == 2) ||
1345 ((suboverlap_enabled) && ((sub_format == SUB_JACOSUB) || (sub_format == SUB_SSA)))) {
1346 adjust_subs_time(first, 6.0, fps, 0, sub_num, uses_time);/*~6 secs AST*/
1347 // here we manage overlapping subtitles
1348 sub_orig = sub_num;
1349 n_first = sub_num;
1350 sub_num = 0;
1351 second = NULL;
1352 // for each subtitle in first[] we deal with its 'block' of
1353 // bonded subtitles
1354 for (sub_first = 0; sub_first < n_first; ++sub_first) {
1355 unsigned long global_start = first[sub_first].start,
1356 global_end = first[sub_first].end, local_start, local_end;
1357 int lines_to_add = first[sub_first].lines, sub_to_add = 0,
1358 **placeholder = NULL, higher_line = 0, counter, start_block_sub = sub_num;
1359 char real_block = 1;
1361 // here we find the number of subtitles inside the 'block'
1362 // and its span interval. this works well only with sorted
1363 // subtitles
1364 while ((sub_first + sub_to_add + 1 < n_first) && (first[sub_first + sub_to_add + 1].start < global_end)) {
1365 ++sub_to_add;
1366 lines_to_add += first[sub_first + sub_to_add].lines;
1367 if (first[sub_first + sub_to_add].start < global_start) {
1368 global_start = first[sub_first + sub_to_add].start;
1370 if (first[sub_first + sub_to_add].end > global_end) {
1371 global_end = first[sub_first + sub_to_add].end;
1375 // we need a structure to keep trace of the screen lines
1376 // used by the subs, a 'placeholder'
1377 counter = 2 * sub_to_add + 1; // the maximum number of subs derived
1378 // from a block of sub_to_add+1 subs
1379 placeholder = (int **) malloc(sizeof(int *) * counter);
1380 for (i = 0; i < counter; ++i) {
1381 placeholder[i] = (int *) malloc(sizeof(int) * lines_to_add);
1382 for (j = 0; j < lines_to_add; ++j) {
1383 placeholder[i][j] = -1;
1387 counter = 0;
1388 local_end = global_start - 1;
1389 do {
1390 int ls;
1392 // here we find the beginning and the end of a new
1393 // subtitle in the block
1394 local_start = local_end + 1;
1395 local_end = global_end;
1396 for (j = 0; j <= sub_to_add; ++j) {
1397 if ((first[sub_first + j].start - 1 > local_start) && (first[sub_first + j].start - 1 < local_end)) {
1398 local_end = first[sub_first + j].start - 1;
1399 } else if ((first[sub_first + j].end > local_start) && (first[sub_first + j].end < local_end)) {
1400 local_end = first[sub_first + j].end;
1403 // here we allocate the screen lines to subs we must
1404 // display in current local_start-local_end interval.
1405 // if the subs were yet presents in the previous interval
1406 // they keep the same lines, otherside they get unused lines
1407 for (j = 0; j <= sub_to_add; ++j) {
1408 if ((first[sub_first + j].start <= local_end) && (first[sub_first + j].end > local_start)) {
1409 unsigned long sub_lines = first[sub_first + j].lines, fragment_length = lines_to_add + 1,
1410 tmp = 0;
1411 char boolean = 0;
1412 int fragment_position = -1;
1414 // if this is not the first new sub of the block
1415 // we find if this sub was present in the previous
1416 // new sub
1417 if (counter)
1418 for (i = 0; i < lines_to_add; ++i) {
1419 if (placeholder[counter - 1][i] == sub_first + j) {
1420 placeholder[counter][i] = sub_first + j;
1421 boolean = 1;
1424 if (boolean)
1425 continue;
1427 // we are looking for the shortest among all groups of
1428 // sequential blank lines whose length is greater than or
1429 // equal to sub_lines. we store in fragment_position the
1430 // position of the shortest group, in fragment_length its
1431 // length, and in tmp the length of the group currently
1432 // examinated
1433 for (i = 0; i < lines_to_add; ++i) {
1434 if (placeholder[counter][i] == -1) {
1435 // placeholder[counter][i] is part of the current group
1436 // of blank lines
1437 ++tmp;
1438 } else {
1439 if (tmp == sub_lines) {
1440 // current group's size fits exactly the one we
1441 // need, so we stop looking
1442 fragment_position = i - tmp;
1443 tmp = 0;
1444 break;
1446 if ((tmp) && (tmp > sub_lines) && (tmp < fragment_length)) {
1447 // current group is the best we found till here,
1448 // but is still bigger than the one we are looking
1449 // for, so we keep on looking
1450 fragment_length = tmp;
1451 fragment_position = i - tmp;
1452 tmp = 0;
1453 } else {
1454 // current group doesn't fit at all, so we forget it
1455 tmp = 0;
1459 if (tmp) {
1460 // last screen line is blank, a group ends with it
1461 if ((tmp >= sub_lines) && (tmp < fragment_length)) {
1462 fragment_position = i - tmp;
1465 if (fragment_position == -1) {
1466 // it was not possible to find free screen line(s) for a subtitle,
1467 // usually this means a bug in the code; however we do not overlap
1468 mp_msg(MSGT_SUBREADER, MSGL_WARN, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1469 higher_line = SUB_MAX_TEXT + 1;
1470 break;
1471 } else {
1472 for (tmp = 0; tmp < sub_lines; ++tmp) {
1473 placeholder[counter][fragment_position + tmp] = sub_first + j;
1478 for (j = higher_line + 1; j < lines_to_add; ++j) {
1479 if (placeholder[counter][j] != -1)
1480 higher_line = j;
1481 else
1482 break;
1484 if (higher_line >= SUB_MAX_TEXT) {
1485 // the 'block' has too much lines, so we don't overlap the
1486 // subtitles
1487 second = (subtitle *) realloc(second, (sub_num + sub_to_add + 1) * sizeof(subtitle));
1488 for (j = 0; j <= sub_to_add; ++j) {
1489 int ls;
1490 memset(&second[sub_num + j], '\0', sizeof(subtitle));
1491 second[sub_num + j].start = first[sub_first + j].start;
1492 second[sub_num + j].end = first[sub_first + j].end;
1493 second[sub_num + j].lines = first[sub_first + j].lines;
1494 second[sub_num + j].alignment = first[sub_first + j].alignment;
1495 for (ls = 0; ls < second[sub_num + j].lines; ls++) {
1496 second[sub_num + j].text[ls] = strdup(first[sub_first + j].text[ls]);
1499 sub_num += sub_to_add + 1;
1500 sub_first += sub_to_add;
1501 real_block = 0;
1502 break;
1505 // we read the placeholder structure and create the new
1506 // subs.
1507 second = (subtitle *) realloc(second, (sub_num + 1) * sizeof(subtitle));
1508 memset(&second[sub_num], '\0', sizeof(subtitle));
1509 second[sub_num].start = local_start;
1510 second[sub_num].end = local_end;
1511 second[sub_num].alignment = SUB_ALIGNMENT_HCENTER;
1512 n_max = (lines_to_add < SUB_MAX_TEXT) ? lines_to_add : SUB_MAX_TEXT;
1513 for (i = 0, j = 0; j < n_max; ++j) {
1514 if (placeholder[counter][j] != -1) {
1515 int lines = first[placeholder[counter][j]].lines;
1516 for (ls = 0; ls < lines; ++ls) {
1517 second[sub_num].text[i++] = strdup(first[placeholder[counter][j]].text[ls]);
1519 j += lines - 1;
1520 } else {
1521 second[sub_num].text[i++] = strdup(" ");
1524 ++sub_num;
1525 ++counter;
1526 } while (local_end < global_end);
1527 if (real_block)
1528 for (i = 0; i < counter; ++i)
1529 second[start_block_sub + i].lines = higher_line + 1;
1531 counter = 2 * sub_to_add + 1;
1532 for (i = 0; i < counter; ++i) {
1533 free(placeholder[i]);
1535 free(placeholder);
1536 sub_first += sub_to_add;
1539 for (j = sub_orig - 1; j >= 0; --j) {
1540 for (i = first[j].lines - 1; i >= 0; --i) {
1541 free(first[j].text[i]);
1544 free(first);
1546 return_sub = second;
1547 } else { //if(suboverlap_enabled)
1548 adjust_subs_time(first, 6.0, fps, 1, sub_num, uses_time);/*~6 secs AST*/
1549 return_sub = first;
1551 if (return_sub == NULL) return NULL;
1552 subt_data = (sub_data *)malloc(sizeof(sub_data));
1553 subt_data->filename = filename;
1554 subt_data->sub_uses_time = uses_time;
1555 subt_data->sub_num = sub_num;
1556 subt_data->sub_errs = sub_errs;
1557 subt_data->subtitles = return_sub;
1558 return subt_data;
1561 #if 0
1562 char * strreplace( char * in,char * what,char * whereof )
1564 int i;
1565 char * tmp;
1567 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL;
1568 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i];
1569 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0;
1570 return in;
1572 #endif
1575 static void strcpy_trim(char *d, char *s)
1577 // skip leading whitespace
1578 while (*s && !isalnum(*s)) {
1579 s++;
1581 for (;;) {
1582 // copy word
1583 while (*s && isalnum(*s)) {
1584 *d = tolower(*s);
1585 s++; d++;
1587 if (*s == 0) break;
1588 // trim excess whitespace
1589 while (*s && !isalnum(*s)) {
1590 s++;
1592 if (*s == 0) break;
1593 *d++ = ' ';
1595 *d = 0;
1598 static void strcpy_strip_ext(char *d, char *s)
1600 char *tmp = strrchr(s,'.');
1601 if (!tmp) {
1602 strcpy(d, s);
1603 return;
1604 } else {
1605 strncpy(d, s, tmp-s);
1606 d[tmp-s] = 0;
1608 while (*d) {
1609 *d = tolower(*d);
1610 d++;
1614 static void strcpy_get_ext(char *d, char *s)
1616 char *tmp = strrchr(s,'.');
1617 if (!tmp) {
1618 strcpy(d, "");
1619 return;
1620 } else {
1621 strcpy(d, tmp+1);
1625 static int whiteonly(char *s)
1627 while (*s) {
1628 if (isalnum(*s)) return 0;
1629 s++;
1631 return 1;
1634 typedef struct _subfn
1636 int priority;
1637 char *fname;
1638 } subfn;
1640 static int compare_sub_priority(const void *a, const void *b)
1642 if (((subfn*)a)->priority > ((subfn*)b)->priority) {
1643 return -1;
1644 } else if (((subfn*)a)->priority < ((subfn*)b)->priority) {
1645 return 1;
1646 } else {
1647 return strcoll(((subfn*)a)->fname, ((subfn*)b)->fname);
1651 char** sub_filenames(char* path, char *fname)
1653 char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
1654 char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult;
1656 int len, pos, found, i, j;
1657 char * sub_exts[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
1658 subfn *result;
1659 char **result2;
1661 int subcnt;
1663 FILE *f;
1665 DIR *d;
1666 struct dirent *de;
1668 len = (strlen(fname) > 256 ? strlen(fname) : 256)
1669 +(strlen(path) > 256 ? strlen(path) : 256)+2;
1671 f_dir = (char*)malloc(len);
1672 f_fname = (char*)malloc(len);
1673 f_fname_noext = (char*)malloc(len);
1674 f_fname_trim = (char*)malloc(len);
1676 tmp_fname_noext = (char*)malloc(len);
1677 tmp_fname_trim = (char*)malloc(len);
1678 tmp_fname_ext = (char*)malloc(len);
1680 tmpresult = (char*)malloc(len);
1682 result = (subfn*)malloc(sizeof(subfn)*MAX_SUBTITLE_FILES);
1683 memset(result, 0, sizeof(subfn)*MAX_SUBTITLE_FILES);
1685 subcnt = 0;
1687 tmp = strrchr(fname,'/');
1688 #ifdef WIN32
1689 if(!tmp)tmp = strrchr(fname,'\\');
1690 #endif
1692 // extract filename & dirname from fname
1693 if (tmp) {
1694 strcpy(f_fname, tmp+1);
1695 pos = tmp - fname;
1696 strncpy(f_dir, fname, pos+1);
1697 f_dir[pos+1] = 0;
1698 } else {
1699 strcpy(f_fname, fname);
1700 strcpy(f_dir, "./");
1703 strcpy_strip_ext(f_fname_noext, f_fname);
1704 strcpy_trim(f_fname_trim, f_fname_noext);
1706 tmp_sub_id = NULL;
1707 if (dvdsub_lang && !whiteonly(dvdsub_lang)) {
1708 tmp_sub_id = (char*)malloc(strlen(dvdsub_lang)+1);
1709 strcpy_trim(tmp_sub_id, dvdsub_lang);
1712 // 0 = nothing
1713 // 1 = any subtitle file
1714 // 2 = any sub file containing movie name
1715 // 3 = sub file containing movie name and the lang extension
1716 for (j = 0; j <= 1; j++) {
1717 d = opendir(j == 0 ? f_dir : path);
1718 if (d) {
1719 while ((de = readdir(d))) {
1720 // retrieve various parts of the filename
1721 strcpy_strip_ext(tmp_fname_noext, de->d_name);
1722 strcpy_get_ext(tmp_fname_ext, de->d_name);
1723 strcpy_trim(tmp_fname_trim, tmp_fname_noext);
1725 // does it end with a subtitle extension?
1726 found = 0;
1727 #ifdef USE_ICONV
1728 for (i = (sub_cp ? 3 : 0); sub_exts[i]; i++) {
1729 #else
1730 for (i = 0; sub_exts[i]; i++) {
1731 #endif
1732 if (strcmp(sub_exts[i], tmp_fname_ext) == 0) {
1733 found = 1;
1734 break;
1738 // we have a (likely) subtitle file
1739 if (found) {
1740 int prio = 0;
1741 if (!prio && tmp_sub_id)
1743 sprintf(tmpresult, "%s %s", f_fname_trim, tmp_sub_id);
1744 printf("dvdsublang...%s\n", tmpresult);
1745 if (strcmp(tmp_fname_trim, tmpresult) == 0 && sub_match_fuzziness >= 1) {
1746 // matches the movie name + lang extension
1747 prio = 5;
1750 if (!prio && strcmp(tmp_fname_trim, f_fname_trim) == 0) {
1751 // matches the movie name
1752 prio = 4;
1754 if (!prio && (tmp = strstr(tmp_fname_trim, f_fname_trim)) && (sub_match_fuzziness >= 1)) {
1755 // contains the movie name
1756 tmp += strlen(f_fname_trim);
1757 if (tmp_sub_id && strstr(tmp, tmp_sub_id)) {
1758 // with sub_id specified prefer localized subtitles
1759 prio = 3;
1760 } else if ((tmp_sub_id == NULL) && whiteonly(tmp)) {
1761 // without sub_id prefer "plain" name
1762 prio = 3;
1763 } else {
1764 // with no localized subs found, try any else instead
1765 prio = 2;
1768 if (!prio) {
1769 // doesn't contain the movie name
1770 // don't try in the mplayer subtitle directory
1771 if ((j == 0) && (sub_match_fuzziness >= 2)) {
1772 prio = 1;
1776 if (prio) {
1777 prio += prio;
1778 #ifdef USE_ICONV
1779 if (i<3){ // prefer UTF-8 coded
1780 prio++;
1782 #endif
1783 sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name);
1784 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1785 if ((f = fopen(tmpresult, "rt"))) {
1786 fclose(f);
1787 result[subcnt].priority = prio;
1788 result[subcnt].fname = strdup(tmpresult);
1789 subcnt++;
1794 if (subcnt >= MAX_SUBTITLE_FILES) break;
1796 closedir(d);
1801 if (tmp_sub_id) free(tmp_sub_id);
1803 free(f_dir);
1804 free(f_fname);
1805 free(f_fname_noext);
1806 free(f_fname_trim);
1808 free(tmp_fname_noext);
1809 free(tmp_fname_trim);
1810 free(tmp_fname_ext);
1812 free(tmpresult);
1814 qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
1816 result2 = (char**)malloc(sizeof(char*)*(subcnt+1));
1817 memset(result2, 0, sizeof(char*)*(subcnt+1));
1819 for (i = 0; i < subcnt; i++) {
1820 result2[i] = result[i].fname;
1822 result2[subcnt] = NULL;
1824 free(result);
1826 return result2;
1829 void list_sub_file(sub_data* subd){
1830 int i,j;
1831 subtitle *subs = subd->subtitles;
1833 for(j=0; j < subd->sub_num; j++){
1834 subtitle* egysub=&subs[j];
1835 printf ("%i line%c (%li-%li)\n",
1836 egysub->lines,
1837 (1==egysub->lines)?' ':'s',
1838 egysub->start,
1839 egysub->end);
1840 for (i=0; i<egysub->lines; i++) {
1841 printf ("\t\t%d: %s%s", i,egysub->text[i], i==egysub->lines-1?"":" \n ");
1843 printf ("\n");
1846 printf ("Subtitle format %s time.\n",
1847 subd->sub_uses_time ? "uses":"doesn't use");
1848 printf ("Read %i subtitles, %i errors.\n", subd->sub_num, subd->sub_errs);
1851 void dump_srt(sub_data* subd, float fps){
1852 int i,j;
1853 int h,m,s,ms;
1854 FILE * fd;
1855 subtitle * onesub;
1856 unsigned long temp;
1857 subtitle *subs = subd->subtitles;
1859 if (!subd->sub_uses_time && sub_fps == 0)
1860 sub_fps = fps;
1861 fd=fopen("dumpsub.srt","w");
1862 if(!fd)
1864 perror("dump_srt: fopen");
1865 return;
1867 for(i=0; i < subd->sub_num; i++)
1869 onesub=subs+i; //=&subs[i];
1870 fprintf(fd,"%d\n",i+1);//line number
1872 temp=onesub->start;
1873 if (!subd->sub_uses_time)
1874 temp = temp * 100 / sub_fps;
1875 temp -= sub_delay * 100;
1876 h=temp/360000;temp%=360000; //h =1*100*60*60
1877 m=temp/6000; temp%=6000; //m =1*100*60
1878 s=temp/100; temp%=100; //s =1*100
1879 ms=temp*10; //ms=1*10
1880 fprintf(fd,"%02d:%02d:%02d,%03d --> ",h,m,s,ms);
1882 temp=onesub->end;
1883 if (!subd->sub_uses_time)
1884 temp = temp * 100 / sub_fps;
1885 temp -= sub_delay * 100;
1886 h=temp/360000;temp%=360000;
1887 m=temp/6000; temp%=6000;
1888 s=temp/100; temp%=100;
1889 ms=temp*10;
1890 fprintf(fd,"%02d:%02d:%02d,%03d\n",h,m,s,ms);
1892 for(j=0;j<onesub->lines;j++)
1893 fprintf(fd,"%s\n",onesub->text[j]);
1895 fprintf(fd,"\n");
1897 fclose(fd);
1898 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
1901 void dump_mpsub(sub_data* subd, float fps){
1902 int i,j;
1903 FILE *fd;
1904 float a,b;
1905 subtitle *subs = subd->subtitles;
1907 mpsub_position = subd->sub_uses_time? (sub_delay*100) : (sub_delay*fps);
1908 if (sub_fps==0) sub_fps=fps;
1910 fd=fopen ("dump.mpsub", "w");
1911 if (!fd) {
1912 perror ("dump_mpsub: fopen");
1913 return;
1917 if (subd->sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n");
1918 else fprintf (fd, "FORMAT=%5.2f\n\n", fps);
1920 for(j=0; j < subd->sub_num; j++){
1921 subtitle* egysub=&subs[j];
1922 if (subd->sub_uses_time) {
1923 a=((egysub->start-mpsub_position)/100.0);
1924 b=((egysub->end-egysub->start)/100.0);
1925 if ( (float)((int)a) == a)
1926 fprintf (fd, "%.0f",a);
1927 else
1928 fprintf (fd, "%.2f",a);
1930 if ( (float)((int)b) == b)
1931 fprintf (fd, " %.0f\n",b);
1932 else
1933 fprintf (fd, " %.2f\n",b);
1934 } else {
1935 fprintf (fd, "%ld %ld\n", (long)((egysub->start*(fps/sub_fps))-((mpsub_position*(fps/sub_fps)))),
1936 (long)(((egysub->end)-(egysub->start))*(fps/sub_fps)));
1939 mpsub_position = egysub->end;
1940 for (i=0; i<egysub->lines; i++) {
1941 fprintf (fd, "%s\n",egysub->text[i]);
1943 fprintf (fd, "\n");
1945 fclose (fd);
1946 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
1949 void dump_microdvd(sub_data* subd, float fps) {
1950 int i, delay;
1951 FILE *fd;
1952 subtitle *subs = subd->subtitles;
1953 if (sub_fps == 0)
1954 sub_fps = fps;
1955 fd = fopen("dumpsub.txt", "w");
1956 if (!fd) {
1957 perror("dumpsub.txt: fopen");
1958 return;
1960 delay = sub_delay * sub_fps;
1961 for (i = 0; i < subd->sub_num; ++i) {
1962 int j, start, end;
1963 start = subs[i].start;
1964 end = subs[i].end;
1965 if (subd->sub_uses_time) {
1966 start = start * sub_fps / 100 ;
1967 end = end * sub_fps / 100;
1969 else {
1970 start = start * sub_fps / fps;
1971 end = end * sub_fps / fps;
1973 start -= delay;
1974 end -= delay;
1975 fprintf(fd, "{%d}{%d}", start, end);
1976 for (j = 0; j < subs[i].lines; ++j)
1977 fprintf(fd, "%s%s", j ? "|" : "", subs[i].text[j]);
1978 fprintf(fd, "\n");
1980 fclose(fd);
1981 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
1984 void dump_jacosub(sub_data* subd, float fps) {
1985 int i,j;
1986 int h,m,s,cs;
1987 FILE * fd;
1988 subtitle * onesub;
1989 unsigned long temp;
1990 subtitle *subs = subd->subtitles;
1992 if (!subd->sub_uses_time && sub_fps == 0)
1993 sub_fps = fps;
1994 fd=fopen("dumpsub.jss","w");
1995 if(!fd)
1997 perror("dump_jacosub: fopen");
1998 return;
2000 fprintf(fd, "#TIMERES %d\n", (subd->sub_uses_time) ? 100 : (int)sub_fps);
2001 for(i=0; i < subd->sub_num; i++)
2003 onesub=subs+i; //=&subs[i];
2005 temp=onesub->start;
2006 if (!subd->sub_uses_time)
2007 temp = temp * 100 / sub_fps;
2008 temp -= sub_delay * 100;
2009 h=temp/360000;temp%=360000; //h =1*100*60*60
2010 m=temp/6000; temp%=6000; //m =1*100*60
2011 s=temp/100; temp%=100; //s =1*100
2012 cs=temp; //cs=1*10
2013 fprintf(fd,"%02d:%02d:%02d.%02d ",h,m,s,cs);
2015 temp=onesub->end;
2016 if (!subd->sub_uses_time)
2017 temp = temp * 100 / sub_fps;
2018 temp -= sub_delay * 100;
2019 h=temp/360000;temp%=360000;
2020 m=temp/6000; temp%=6000;
2021 s=temp/100; temp%=100;
2022 cs=temp;
2023 fprintf(fd,"%02d:%02d:%02d.%02d {~} ",h,m,s,cs);
2025 for(j=0;j<onesub->lines;j++)
2026 fprintf(fd,"%s%s",j ? "\\n" : "", onesub->text[j]);
2028 fprintf(fd,"\n");
2030 fclose(fd);
2031 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2034 void dump_sami(sub_data* subd, float fps) {
2035 int i,j;
2036 FILE * fd;
2037 subtitle * onesub;
2038 unsigned long temp;
2039 subtitle *subs = subd->subtitles;
2041 if (!subd->sub_uses_time && sub_fps == 0)
2042 sub_fps = fps;
2043 fd=fopen("dumpsub.smi","w");
2044 if(!fd)
2046 perror("dump_jacosub: fopen");
2047 return;
2049 fprintf(fd, "<SAMI>\n"
2050 "<HEAD>\n"
2051 " <STYLE TYPE=\"Text/css\">\n"
2052 " <!--\n"
2053 " 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"
2054 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2055 " -->\n"
2056 " </STYLE>\n"
2057 "</HEAD>\n"
2058 "<BODY>\n");
2059 for(i=0; i < subd->sub_num; i++)
2061 onesub=subs+i; //=&subs[i];
2063 temp=onesub->start;
2064 if (!subd->sub_uses_time)
2065 temp = temp * 100 / sub_fps;
2066 temp -= sub_delay * 100;
2067 fprintf(fd,"\t<SYNC Start=%lu>\n"
2068 "\t <P>", temp * 10);
2070 for(j=0;j<onesub->lines;j++)
2071 fprintf(fd,"%s%s",j ? "<br>" : "", onesub->text[j]);
2073 fprintf(fd,"\n");
2075 temp=onesub->end;
2076 if (!subd->sub_uses_time)
2077 temp = temp * 100 / sub_fps;
2078 temp -= sub_delay * 100;
2079 fprintf(fd,"\t<SYNC Start=%lu>\n"
2080 "\t <P>&nbsp;\n", temp * 10);
2082 fprintf(fd, "</BODY>\n"
2083 "</SAMI>\n");
2084 fclose(fd);
2085 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2088 void sub_free( sub_data * subd )
2090 int i;
2092 if ( !subd ) return;
2094 if (subd->subtitles) {
2095 for (i=0; i < subd->subtitles->lines; i++) free( subd->subtitles->text[i] );
2096 free( subd->subtitles );
2098 if (subd->filename) free( subd->filename );
2099 free( subd );
2102 #ifdef DUMPSUBS
2103 int main(int argc, char **argv) { // for testing
2104 sub_data *subd;
2106 if(argc<2){
2107 printf("\nUsage: subreader filename.sub\n\n");
2108 exit(1);
2110 sub_cp = argv[2];
2111 subd = sub_read_file(argv[1]);
2112 if(!subd){
2113 printf("Couldn't load file.\n");
2114 exit(1);
2117 list_sub_file(subd);
2119 return 0;
2121 #endif