We don't want junk in unused parts of the BITMAPINFOHEADER
[mplayer/greg.git] / subreader.c
blob3bc8f583aa97b6aaa56c6a30861fe8c73b1f653a
1 /*
2 * Subtitle reader with format autodetection
4 * Written by laaz
5 * Some code cleanup & realloc() by A'rpi/ESP-team
7 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
15 #include <sys/types.h>
16 #include <dirent.h>
18 #include "config.h"
19 #include "mp_msg.h"
20 #include "subreader.h"
22 #ifdef HAVE_ENCA
23 #include <enca.h>
24 #endif
26 #define ERR ((void *) -1)
28 #ifdef USE_ICONV
29 #include <iconv.h>
30 char *sub_cp=NULL;
31 #endif
32 #ifdef USE_FRIBIDI
33 #include <fribidi/fribidi.h>
34 char *fribidi_charset = NULL;
35 int flip_hebrew = 1;
36 #endif
38 extern char* dvdsub_lang;
40 /* Maximal length of line of a subtitle */
41 #define LINE_LEN 1000
42 static float mpsub_position=0;
43 static float mpsub_multiplier=1.;
44 static int sub_slacktime = 20000; //20 sec
46 int sub_no_text_pp=0; // 1 => do not apply text post-processing
47 // like {\...} elimination in SSA format.
49 int sub_match_fuzziness=0; // level of sub name matching fuzziness
51 /* Use the SUB_* constant defined in the header file */
52 int sub_format=SUB_INVALID;
53 #ifdef USE_SORTSUB
54 /*
55 Some subtitling formats, namely AQT and Subrip09, define the end of a
56 subtitle as the beginning of the following. Since currently we read one
57 subtitle at time, for these format we keep two global *subtitle,
58 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
59 so we can change its end when we read current subtitle starting time.
60 When USE_SORTSUB is defined, we use a single global unsigned long,
61 previous_sub_end, for both (and even future) formats, to store the end of
62 the previous sub: it is initialized to 0 in sub_read_file and eventually
63 modified by sub_read_aqt_line or sub_read_subrip09_line.
65 unsigned long previous_sub_end;
66 #endif
68 static int eol(char p) {
69 return (p=='\r' || p=='\n' || p=='\0');
72 /* Remove leading and trailing space */
73 static void trail_space(char *s) {
74 int i = 0;
75 while (isspace(s[i])) ++i;
76 if (i) strcpy(s, s + i);
77 i = strlen(s) - 1;
78 while (i > 0 && isspace(s[i])) s[i--] = '\0';
81 static char *stristr(const char *haystack, const char *needle) {
82 int len = 0;
83 const char *p = haystack;
85 if (!(haystack && needle)) return NULL;
87 len=strlen(needle);
88 while (*p != '\0') {
89 if (strncasecmp(p, needle, len) == 0) return (char*)p;
90 p++;
93 return NULL;
96 subtitle *sub_read_line_sami(FILE *fd, subtitle *current) {
97 static char line[LINE_LEN+1];
98 static char *s = NULL, *slacktime_s;
99 char text[LINE_LEN+1], *p=NULL, *q;
100 int state;
102 current->lines = current->start = current->end = 0;
103 state = 0;
105 /* read the first line */
106 if (!s)
107 if (!(s = fgets(line, LINE_LEN, fd))) return 0;
109 do {
110 switch (state) {
112 case 0: /* find "START=" or "Slacktime:" */
113 slacktime_s = stristr (s, "Slacktime:");
114 if (slacktime_s)
115 sub_slacktime = strtol (slacktime_s+10, NULL, 0) / 10;
117 s = stristr (s, "Start=");
118 if (s) {
119 current->start = strtol (s + 6, &s, 0) / 10;
120 /* eat '>' */
121 for (; *s != '>' && *s != '\0'; s++);
122 s++;
123 state = 1; continue;
125 break;
127 case 1: /* find (optionnal) "<P", skip other TAGs */
128 for (; *s == ' ' || *s == '\t'; s++); /* strip blanks, if any */
129 if (*s == '\0') break;
130 if (*s != '<') { state = 3; p = text; continue; } /* not a TAG */
131 s++;
132 if (*s == 'P' || *s == 'p') { s++; state = 2; continue; } /* found '<P' */
133 for (; *s != '>' && *s != '\0'; s++); /* skip remains of non-<P> TAG */
134 if (s == '\0')
135 break;
136 s++;
137 continue;
139 case 2: /* find ">" */
140 if ((s = strchr (s, '>'))) { s++; state = 3; p = text; continue; }
141 break;
143 case 3: /* get all text until '<' appears */
144 if (*s == '\0') break;
145 else if (!strncasecmp (s, "<br>", 4)) {
146 *p = '\0'; p = text; trail_space (text);
147 if (text[0] != '\0')
148 current->text[current->lines++] = strdup (text);
149 s += 4;
151 else if ((*s == '{') && !sub_no_text_pp) { state = 5; ++s; continue; }
152 else if (*s == '<') { state = 4; }
153 else if (!strncasecmp (s, "&nbsp;", 6)) { *p++ = ' '; s += 6; }
154 else if (*s == '\t') { *p++ = ' '; s++; }
155 else if (*s == '\r' || *s == '\n') { s++; }
156 else *p++ = *s++;
158 /* skip duplicated space */
159 if (p > text + 2) if (*(p-1) == ' ' && *(p-2) == ' ') p--;
161 continue;
163 case 4: /* get current->end or skip <TAG> */
164 q = stristr (s, "Start=");
165 if (q) {
166 current->end = strtol (q + 6, &q, 0) / 10 - 1;
167 *p = '\0'; trail_space (text);
168 if (text[0] != '\0')
169 current->text[current->lines++] = strdup (text);
170 if (current->lines > 0) { state = 99; break; }
171 state = 0; continue;
173 s = strchr (s, '>');
174 if (s) { s++; state = 3; continue; }
175 break;
176 case 5: /* get rid of {...} text */
177 if (*s == '}') state = 3;
178 ++s;
179 continue;
182 /* read next line */
183 if (state != 99 && !(s = fgets (line, LINE_LEN, fd))) {
184 if (current->start > 0) {
185 break; // if it is the last subtitle
186 } else {
187 return 0;
191 } while (state != 99);
193 // For the last subtitle
194 if (current->end <= 0) {
195 current->end = current->start + sub_slacktime;
196 *p = '\0'; trail_space (text);
197 if (text[0] != '\0')
198 current->text[current->lines++] = strdup (text);
201 return current;
205 char *sub_readtext(char *source, char **dest) {
206 int len=0;
207 char *p=source;
209 // printf("src=%p dest=%p \n",source,dest);
211 while ( !eol(*p) && *p!= '|' ) {
212 p++,len++;
215 *dest= (char *)malloc (len+1);
216 if (!dest) {return ERR;}
218 strncpy(*dest, source, len);
219 (*dest)[len]=0;
221 while (*p=='\r' || *p=='\n' || *p=='|') p++;
223 if (*p) return p; // not-last text field
224 else return NULL; // last text field
227 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) {
228 char line[LINE_LEN+1];
229 char line2[LINE_LEN+1];
230 char *p, *next;
231 int i;
233 do {
234 if (!fgets (line, LINE_LEN, fd)) return NULL;
235 } while ((sscanf (line,
236 "{%ld}{}%[^\r\n]",
237 &(current->start), line2) < 2) &&
238 (sscanf (line,
239 "{%ld}{%ld}%[^\r\n]",
240 &(current->start), &(current->end), line2) < 3));
242 p=line2;
244 next=p, i=0;
245 while ((next =sub_readtext (next, &(current->text[i])))) {
246 if (current->text[i]==ERR) {return ERR;}
247 i++;
248 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
250 current->lines= ++i;
252 return current;
255 subtitle *sub_read_line_mpl2(FILE *fd,subtitle *current) {
256 char line[LINE_LEN+1];
257 char line2[LINE_LEN+1];
258 char *p, *next;
259 int i;
261 do {
262 if (!fgets (line, LINE_LEN, fd)) return NULL;
263 } while ((sscanf (line,
264 "[%ld][%ld]%[^\r\n]",
265 &(current->start), &(current->end), line2) < 3));
266 current->start *= 10;
267 current->end *= 10;
268 p=line2;
270 next=p, i=0;
271 while ((next =sub_readtext (next, &(current->text[i])))) {
272 if (current->text[i]==ERR) {return ERR;}
273 i++;
274 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
276 current->lines= ++i;
278 return current;
281 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) {
282 char line[LINE_LEN+1];
283 int a1,a2,a3,a4,b1,b2,b3,b4;
284 char *p=NULL, *q=NULL;
285 int len;
287 while (1) {
288 if (!fgets (line, LINE_LEN, fd)) return NULL;
289 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
290 current->start = a1*360000+a2*6000+a3*100+a4;
291 current->end = b1*360000+b2*6000+b3*100+b4;
293 if (!fgets (line, LINE_LEN, fd)) return NULL;
295 p=q=line;
296 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) {
297 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && *p!='|' && strncmp(p,"[br]",4); p++,len++);
298 current->text[current->lines-1]=(char *)malloc (len+1);
299 if (!current->text[current->lines-1]) return ERR;
300 strncpy (current->text[current->lines-1], q, len);
301 current->text[current->lines-1][len]='\0';
302 if (!*p || *p=='\r' || *p=='\n') break;
303 if (*p=='|') p++;
304 else while (*p++!=']');
306 break;
308 return current;
311 subtitle *sub_read_line_subviewer(FILE *fd,subtitle *current) {
312 char line[LINE_LEN+1];
313 int a1,a2,a3,a4,b1,b2,b3,b4;
314 char *p=NULL;
315 int i,len;
317 while (!current->text[0]) {
318 if (!fgets (line, LINE_LEN, fd)) return NULL;
319 if ((len=sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d",&a1,&a2,&a3,(char *)&i,&a4,&b1,&b2,&b3,(char *)&i,&b4)) < 10)
320 continue;
321 current->start = a1*360000+a2*6000+a3*100+a4/10;
322 current->end = b1*360000+b2*6000+b3*100+b4/10;
323 for (i=0; i<SUB_MAX_TEXT;) {
324 if (!fgets (line, LINE_LEN, fd)) break;
325 len=0;
326 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
327 if (len) {
328 int j=0,skip=0;
329 char *curptr=current->text[i]=(char *)malloc (len+1);
330 if (!current->text[i]) return ERR;
331 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
332 for(; j<len; j++) {
333 /* let's filter html tags ::atmos */
334 if(line[j]=='>') {
335 skip=0;
336 continue;
338 if(line[j]=='<') {
339 skip=1;
340 continue;
342 if(skip) {
343 continue;
345 *curptr=line[j];
346 curptr++;
348 *curptr='\0';
350 i++;
351 } else {
352 break;
355 current->lines=i;
357 return current;
360 subtitle *sub_read_line_subviewer2(FILE *fd,subtitle *current) {
361 char line[LINE_LEN+1];
362 int a1,a2,a3,a4;
363 char *p=NULL;
364 int i,len;
366 while (!current->text[0]) {
367 if (!fgets (line, LINE_LEN, fd)) return NULL;
368 if (line[0]!='{')
369 continue;
370 if ((len=sscanf (line, "{T %d:%d:%d:%d",&a1,&a2,&a3,&a4)) < 4)
371 continue;
372 current->start = a1*360000+a2*6000+a3*100+a4/10;
373 for (i=0; i<SUB_MAX_TEXT;) {
374 if (!fgets (line, LINE_LEN, fd)) break;
375 if (line[0]=='}') break;
376 len=0;
377 for (p=line; *p!='\n' && *p!='\r' && *p; ++p,++len);
378 if (len) {
379 current->text[i]=(char *)malloc (len+1);
380 if (!current->text[i]) return ERR;
381 strncpy (current->text[i], line, len); current->text[i][len]='\0';
382 ++i;
383 } else {
384 break;
387 current->lines=i;
389 return current;
393 subtitle *sub_read_line_vplayer(FILE *fd,subtitle *current) {
394 char line[LINE_LEN+1];
395 int a1,a2,a3;
396 char *p=NULL, *next,separator;
397 int i,len,plen;
399 while (!current->text[0]) {
400 if (!fgets (line, LINE_LEN, fd)) return NULL;
401 if ((len=sscanf (line, "%d:%d:%d%c%n",&a1,&a2,&a3,&separator,&plen)) < 4)
402 continue;
404 if (!(current->start = a1*360000+a2*6000+a3*100))
405 continue;
406 /* removed by wodzu
407 p=line;
408 // finds the body of the subtitle
409 for (i=0; i<3; i++){
410 p=strchr(p,':');
411 if (p==NULL) break;
412 ++p;
414 if (p==NULL) {
415 printf("SUB: Skipping incorrect subtitle line!\n");
416 continue;
419 // by wodzu: hey! this time we know what length it has! what is
420 // that magic for? it can't deal with space instead of third
421 // colon! look, what simple it can be:
422 p = &line[ plen ];
424 i=0;
425 if (*p!='|') {
427 next = p,i=0;
428 while ((next =sub_readtext (next, &(current->text[i])))) {
429 if (current->text[i]==ERR) {return ERR;}
430 i++;
431 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
433 current->lines=i+1;
436 return current;
439 subtitle *sub_read_line_rt(FILE *fd,subtitle *current) {
440 //TODO: This format uses quite rich (sub/super)set of xhtml
441 // I couldn't check it since DTD is not included.
442 // WARNING: full XML parses can be required for proper parsing
443 char line[LINE_LEN+1];
444 int a1,a2,a3,a4,b1,b2,b3,b4;
445 char *p=NULL,*next=NULL;
446 int i,len,plen;
448 while (!current->text[0]) {
449 if (!fgets (line, LINE_LEN, fd)) return NULL;
450 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
451 //to describe the same moment in time. Maybe there are even more formats in use.
452 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
453 plen=a1=a2=a3=a4=b1=b2=b3=b4=0;
454 if (
455 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b3,&b4,&plen)) < 4) &&
456 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b2,&b3,&b4,&plen)) < 5) &&
457 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) &&
458 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) &&
459 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
460 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&b4,&plen)) < 6) &&
461 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\" %*[Ee]nd=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4,&plen)) < 8) &&
462 //now try it without end time
463 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&plen)) < 2) &&
464 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&plen)) < 2) &&
465 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&plen)) < 3) &&
466 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&plen)) < 4)
468 continue;
469 current->start = a1*360000+a2*6000+a3*100+a4/10;
470 current->end = b1*360000+b2*6000+b3*100+b4/10;
471 if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)
472 current->end = current->start+200;
473 p=line; p+=plen;i=0;
474 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
475 next = strstr(line,"<clear/>");
476 if(next && strlen(next)>8){
477 next+=8;i=0;
478 while ((next =sub_readtext (next, &(current->text[i])))) {
479 if (current->text[i]==ERR) {return ERR;}
480 i++;
481 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
484 current->lines=i+1;
486 return current;
489 subtitle *sub_read_line_ssa(FILE *fd,subtitle *current) {
491 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
492 * other Sub Station Alpha scripts have only 8 commas before subtitle
493 * Reading the "ScriptType:" field is not reliable since many scripts appear
494 * w/o it
496 * http://www.scriptclub.org is a good place to find more examples
497 * http://www.eswat.demon.co.uk is where the SSA specs can be found
499 int comma;
500 static int max_comma = 32; /* let's use 32 for the case that the */
501 /* amount of commas increase with newer SSA versions */
503 int hour1, min1, sec1, hunsec1,
504 hour2, min2, sec2, hunsec2, nothing;
505 int num;
507 char line[LINE_LEN+1],
508 line3[LINE_LEN+1],
509 *line2;
510 char *tmp;
512 do {
513 if (!fgets (line, LINE_LEN, fd)) return NULL;
514 } while (sscanf (line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,"
515 "%[^\n\r]", &nothing,
516 &hour1, &min1, &sec1, &hunsec1,
517 &hour2, &min2, &sec2, &hunsec2,
518 line3) < 9
520 sscanf (line, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d,"
521 "%[^\n\r]", &nothing,
522 &hour1, &min1, &sec1, &hunsec1,
523 &hour2, &min2, &sec2, &hunsec2,
524 line3) < 9 );
526 line2=strchr(line3, ',');
528 for (comma = 4; comma < max_comma; comma ++)
530 tmp = line2;
531 if(!(tmp=strchr(++tmp, ','))) break;
532 if(*(++tmp) == ' ') break;
533 /* a space after a comma means we're already in a sentence */
534 line2 = tmp;
537 if(comma < max_comma)max_comma = comma;
538 /* eliminate the trailing comma */
539 if(*line2 == ',') line2++;
541 current->lines=0;num=0;
542 current->start = 360000*hour1 + 6000*min1 + 100*sec1 + hunsec1;
543 current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2;
545 while (((tmp=strstr(line2, "\\n")) != NULL) || ((tmp=strstr(line2, "\\N")) != NULL) ){
546 current->text[num]=(char *)malloc(tmp-line2+1);
547 strncpy (current->text[num], line2, tmp-line2);
548 current->text[num][tmp-line2]='\0';
549 line2=tmp+2;
550 num++;
551 current->lines++;
552 if (current->lines >= SUB_MAX_TEXT) return current;
555 current->text[num]=strdup(line2);
556 current->lines++;
558 return current;
561 void sub_pp_ssa(subtitle *sub) {
562 int l=sub->lines;
563 char *so,*de,*start;
565 while (l){
566 /* eliminate any text enclosed with {}, they are font and color settings */
567 so=de=sub->text[--l];
568 while (*so) {
569 if(*so == '{' && so[1]=='\\') {
570 for (start=so; *so && *so!='}'; so++);
571 if(*so) so++; else so=start;
573 if(*so) {
574 *de=*so;
575 so++; de++;
578 *de=*so;
583 * PJS subtitles reader.
584 * That's the "Phoenix Japanimation Society" format.
585 * I found some of them in http://www.scriptsclub.org/ (used for anime).
586 * The time is in tenths of second.
588 * by set, based on code by szabi (dunnowhat sub format ;-)
590 subtitle *sub_read_line_pjs(FILE *fd,subtitle *current) {
591 char line[LINE_LEN+1];
592 char text[LINE_LEN+1], *s, *d;
594 if (!fgets (line, LINE_LEN, fd))
595 return NULL;
596 /* skip spaces */
597 for (s=line; *s && isspace(*s); s++);
598 /* allow empty lines at the end of the file */
599 if (*s==0)
600 return NULL;
601 /* get the time */
602 if (sscanf (s, "%ld,%ld,", &(current->start),
603 &(current->end)) <2) {
604 return ERR;
606 /* the files I have are in tenths of second */
607 current->start *= 10;
608 current->end *= 10;
609 /* walk to the beggining of the string */
610 for (; *s; s++) if (*s==',') break;
611 if (*s) {
612 for (s++; *s; s++) if (*s==',') break;
613 if (*s) s++;
615 if (*s!='"') {
616 return ERR;
618 /* copy the string to the text buffer */
619 for (s++, d=text; *s && *s!='"'; s++, d++)
620 *d=*s;
621 *d=0;
622 current->text[0] = strdup(text);
623 current->lines = 1;
625 return current;
628 subtitle *sub_read_line_mpsub(FILE *fd, subtitle *current) {
629 char line[LINE_LEN+1];
630 float a,b;
631 int num=0;
632 char *p, *q;
636 if (!fgets(line, LINE_LEN, fd)) return NULL;
637 } while (sscanf (line, "%f %f", &a, &b) !=2);
639 mpsub_position += a*mpsub_multiplier;
640 current->start=(int) mpsub_position;
641 mpsub_position += b*mpsub_multiplier;
642 current->end=(int) mpsub_position;
644 while (num < SUB_MAX_TEXT) {
645 if (!fgets (line, LINE_LEN, fd)) {
646 if (num == 0) return NULL;
647 else return current;
649 p=line;
650 while (isspace(*p)) p++;
651 if (eol(*p) && num > 0) return current;
652 if (eol(*p)) return NULL;
654 for (q=p; !eol(*q); q++);
655 *q='\0';
656 if (strlen(p)) {
657 current->text[num]=strdup(p);
658 // printf (">%s<\n",p);
659 current->lines = ++num;
660 } else {
661 if (num) return current;
662 else return NULL;
665 return NULL; // we should have returned before if it's OK
668 #ifndef USE_SORTSUB
669 //we don't need this if we use previous_sub_end
670 subtitle *previous_aqt_sub = NULL;
671 #endif
673 subtitle *sub_read_line_aqt(FILE *fd,subtitle *current) {
674 char line[LINE_LEN+1];
675 char *next;
676 int i;
678 while (1) {
679 // try to locate next subtitle
680 if (!fgets (line, LINE_LEN, fd))
681 return NULL;
682 if (!(sscanf (line, "-->> %ld", &(current->start)) <1))
683 break;
686 #ifdef USE_SORTSUB
687 previous_sub_end = (current->start) ? current->start - 1 : 0;
688 #else
689 if (previous_aqt_sub != NULL)
690 previous_aqt_sub->end = current->start-1;
692 previous_aqt_sub = current;
693 #endif
695 if (!fgets (line, LINE_LEN, fd))
696 return NULL;
698 sub_readtext((char *) &line,&current->text[0]);
699 current->lines = 1;
700 current->end = current->start; // will be corrected by next subtitle
702 if (!fgets (line, LINE_LEN, fd))
703 return current;
705 next = line,i=1;
706 while ((next =sub_readtext (next, &(current->text[i])))) {
707 if (current->text[i]==ERR) {return ERR;}
708 i++;
709 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
711 current->lines=i+1;
713 if ((current->text[0]=="") && (current->text[1]=="")) {
714 #ifdef USE_SORTSUB
715 previous_sub_end = 0;
716 #else
717 // void subtitle -> end of previous marked and exit
718 previous_aqt_sub = NULL;
719 #endif
720 return NULL;
723 return current;
726 #ifndef USE_SORTSUB
727 subtitle *previous_subrip09_sub = NULL;
728 #endif
730 subtitle *sub_read_line_subrip09(FILE *fd,subtitle *current) {
731 char line[LINE_LEN+1];
732 int a1,a2,a3;
733 char * next=NULL;
734 int i,len;
736 while (1) {
737 // try to locate next subtitle
738 if (!fgets (line, LINE_LEN, fd))
739 return NULL;
740 if (!((len=sscanf (line, "[%d:%d:%d]",&a1,&a2,&a3)) < 3))
741 break;
744 current->start = a1*360000+a2*6000+a3*100;
746 #ifdef USE_SORTSUB
747 previous_sub_end = (current->start) ? current->start - 1 : 0;
748 #else
749 if (previous_subrip09_sub != NULL)
750 previous_subrip09_sub->end = current->start-1;
752 previous_subrip09_sub = current;
753 #endif
755 if (!fgets (line, LINE_LEN, fd))
756 return NULL;
758 next = line,i=0;
760 current->text[0]=""; // just to be sure that string is clear
762 while ((next =sub_readtext (next, &(current->text[i])))) {
763 if (current->text[i]==ERR) {return ERR;}
764 i++;
765 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
767 current->lines=i+1;
769 if ((current->text[0]=="") && (i==0)) {
770 #ifdef USE_SORTSUB
771 previous_sub_end = 0;
772 #else
773 // void subtitle -> end of previous marked and exit
774 previous_subrip09_sub = NULL;
775 #endif
776 return NULL;
779 return current;
782 subtitle *sub_read_line_jacosub(FILE * fd, subtitle * current)
784 char line1[LINE_LEN], line2[LINE_LEN], directive[LINE_LEN], *p, *q;
785 unsigned a1, a2, a3, a4, b1, b2, b3, b4, comment = 0;
786 static unsigned jacoTimeres = 30;
787 static int jacoShift = 0;
789 bzero(current, sizeof(subtitle));
790 bzero(line1, LINE_LEN);
791 bzero(line2, LINE_LEN);
792 bzero(directive, LINE_LEN);
793 while (!current->text[0]) {
794 if (!fgets(line1, LINE_LEN, fd)) {
795 return NULL;
797 if (sscanf
798 (line1, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1, &a2, &a3, &a4,
799 &b1, &b2, &b3, &b4, line2) < 9) {
800 if (sscanf(line1, "@%u @%u %[^\n\r]", &a4, &b4, line2) < 3) {
801 if (line1[0] == '#') {
802 int hours = 0, minutes = 0, seconds, delta, inverter =
804 unsigned units = jacoShift;
805 switch (toupper(line1[1])) {
806 case 'S':
807 if (isalpha(line1[2])) {
808 delta = 6;
809 } else {
810 delta = 2;
812 if (sscanf(&line1[delta], "%d", &hours)) {
813 if (hours < 0) {
814 hours *= -1;
815 inverter = -1;
817 if (sscanf(&line1[delta], "%*d:%d", &minutes)) {
818 if (sscanf
819 (&line1[delta], "%*d:%*d:%d",
820 &seconds)) {
821 sscanf(&line1[delta], "%*d:%*d:%*d.%d",
822 &units);
823 } else {
824 hours = 0;
825 sscanf(&line1[delta], "%d:%d.%d",
826 &minutes, &seconds, &units);
827 minutes *= inverter;
829 } else {
830 hours = minutes = 0;
831 sscanf(&line1[delta], "%d.%d", &seconds,
832 &units);
833 seconds *= inverter;
835 jacoShift =
836 ((hours * 3600 + minutes * 60 +
837 seconds) * jacoTimeres +
838 units) * inverter;
840 break;
841 case 'T':
842 if (isalpha(line1[2])) {
843 delta = 8;
844 } else {
845 delta = 2;
847 sscanf(&line1[delta], "%u", &jacoTimeres);
848 break;
851 continue;
852 } else {
853 current->start =
854 (unsigned long) ((a4 + jacoShift) * 100.0 /
855 jacoTimeres);
856 current->end =
857 (unsigned long) ((b4 + jacoShift) * 100.0 /
858 jacoTimeres);
860 } else {
861 current->start =
862 (unsigned
863 long) (((a1 * 3600 + a2 * 60 + a3) * jacoTimeres + a4 +
864 jacoShift) * 100.0 / jacoTimeres);
865 current->end =
866 (unsigned
867 long) (((b1 * 3600 + b2 * 60 + b3) * jacoTimeres + b4 +
868 jacoShift) * 100.0 / jacoTimeres);
870 current->lines = 0;
871 p = line2;
872 while ((*p == ' ') || (*p == '\t')) {
873 ++p;
875 if (isalpha(*p)||*p == '[') {
876 int cont, jLength;
878 if (sscanf(p, "%s %[^\n\r]", directive, line1) < 2)
879 return (subtitle *) ERR;
880 jLength = strlen(directive);
881 for (cont = 0; cont < jLength; ++cont) {
882 if (isalpha(*(directive + cont)))
883 *(directive + cont) = toupper(*(directive + cont));
885 if ((strstr(directive, "RDB") != NULL)
886 || (strstr(directive, "RDC") != NULL)
887 || (strstr(directive, "RLB") != NULL)
888 || (strstr(directive, "RLG") != NULL)) {
889 continue;
891 if (strstr(directive, "JL") != NULL) {
892 current->alignment = SUB_ALIGNMENT_HLEFT;
893 } else if (strstr(directive, "JR") != NULL) {
894 current->alignment = SUB_ALIGNMENT_HRIGHT;
895 } else {
896 current->alignment = SUB_ALIGNMENT_HCENTER;
898 strcpy(line2, line1);
899 p = line2;
901 for (q = line1; (!eol(*p)) && (current->lines < SUB_MAX_TEXT); ++p) {
902 switch (*p) {
903 case '{':
904 comment++;
905 break;
906 case '}':
907 if (comment) {
908 --comment;
909 //the next line to get rid of a blank after the comment
910 if ((*(p + 1)) == ' ')
911 p++;
913 break;
914 case '~':
915 if (!comment) {
916 *q = ' ';
917 ++q;
919 break;
920 case ' ':
921 case '\t':
922 if ((*(p + 1) == ' ') || (*(p + 1) == '\t'))
923 break;
924 if (!comment) {
925 *q = ' ';
926 ++q;
928 break;
929 case '\\':
930 if (*(p + 1) == 'n') {
931 *q = '\0';
932 q = line1;
933 current->text[current->lines++] = strdup(line1);
934 ++p;
935 break;
937 if ((toupper(*(p + 1)) == 'C')
938 || (toupper(*(p + 1)) == 'F')) {
939 ++p,++p;
940 break;
942 if ((*(p + 1) == 'B') || (*(p + 1) == 'b') || (*(p + 1) == 'D') || //actually this means "insert current date here"
943 (*(p + 1) == 'I') || (*(p + 1) == 'i') || (*(p + 1) == 'N') || (*(p + 1) == 'T') || //actually this means "insert current time here"
944 (*(p + 1) == 'U') || (*(p + 1) == 'u')) {
945 ++p;
946 break;
948 if ((*(p + 1) == '\\') ||
949 (*(p + 1) == '~') || (*(p + 1) == '{')) {
950 ++p;
951 } else if (eol(*(p + 1))) {
952 if (!fgets(directive, LINE_LEN, fd))
953 return NULL;
954 trail_space(directive);
955 strncat(line2, directive,
956 (LINE_LEN > 511) ? LINE_LEN : 511);
957 break;
959 default:
960 if (!comment) {
961 *q = *p;
962 ++q;
964 } //-- switch
965 } //-- for
966 *q = '\0';
967 current->text[current->lines] = strdup(line1);
968 } //-- while
969 current->lines++;
970 return current;
973 int sub_autodetect (FILE *fd, int *uses_time) {
974 char line[LINE_LEN+1];
975 int i,j=0;
976 char p;
978 while (j < 100) {
979 j++;
980 if (!fgets (line, LINE_LEN, fd))
981 return SUB_INVALID;
983 if (sscanf (line, "{%d}{%d}", &i, &i)==2)
984 {*uses_time=0;return SUB_MICRODVD;}
985 if (sscanf (line, "{%d}{}", &i)==1)
986 {*uses_time=0;return SUB_MICRODVD;}
987 if (sscanf (line, "[%d][%d]", &i, &i)==2)
988 {*uses_time=1;return SUB_MPL2;}
989 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
990 {*uses_time=1;return SUB_SUBRIP;}
991 if (sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i, &i, &i, (char *)&i, &i, &i, &i, &i, (char *)&i, &i)==10)
992 {*uses_time=1;return SUB_SUBVIEWER;}
993 if (sscanf (line, "{T %d:%d:%d:%d",&i, &i, &i, &i)==4)
994 {*uses_time=1;return SUB_SUBVIEWER2;}
995 if (strstr (line, "<SAMI>"))
996 {*uses_time=1; return SUB_SAMI;}
997 if (sscanf(line, "%d:%d:%d.%d %d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i) == 8)
998 {*uses_time = 1; return SUB_JACOSUB;}
999 if (sscanf(line, "@%d @%d", &i, &i) == 2)
1000 {*uses_time = 1; return SUB_JACOSUB;}
1001 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3)
1002 {*uses_time=1;return SUB_VPLAYER;}
1003 if (sscanf (line, "%d:%d:%d ", &i, &i, &i )==3)
1004 {*uses_time=1;return SUB_VPLAYER;}
1005 //TODO: just checking if first line of sub starts with "<" is WAY
1006 // too weak test for RT
1007 // Please someone who knows the format of RT... FIX IT!!!
1008 // It may conflict with other sub formats in the future (actually it doesn't)
1009 if ( *line == '<' )
1010 {*uses_time=1;return SUB_RT;}
1012 if (!memcmp(line, "Dialogue: Marked", 16))
1013 {*uses_time=1; return SUB_SSA;}
1014 if (!memcmp(line, "Dialogue: ", 10))
1015 {*uses_time=1; return SUB_SSA;}
1016 if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3)
1017 {*uses_time=1;return SUB_PJS;}
1018 if (sscanf (line, "FORMAT=%d", &i) == 1)
1019 {*uses_time=0; return SUB_MPSUB;}
1020 if (sscanf (line, "FORMAT=TIM%c", &p)==1 && p=='E')
1021 {*uses_time=1; return SUB_MPSUB;}
1022 if (strstr (line, "-->>"))
1023 {*uses_time=0; return SUB_AQTITLE;}
1024 if (sscanf (line, "[%d:%d:%d]", &i, &i, &i)==3)
1025 {*uses_time=1;return SUB_SUBRIP09;}
1028 return SUB_INVALID; // too many bad lines
1031 #ifdef DUMPSUBS
1032 int sub_utf8=0;
1033 #else
1034 extern int sub_utf8;
1035 int sub_utf8_prev=0;
1036 #endif
1038 extern float sub_delay;
1039 extern float sub_fps;
1041 #ifdef USE_ICONV
1042 static iconv_t icdsc = (iconv_t)(-1);
1044 #ifdef HAVE_ENCA
1045 void subcp_open_noenca ()
1047 char enca_lang[100], enca_fallback[100];
1048 if (sub_cp) {
1049 if (sscanf(sub_cp, "enca:%2s:%s", enca_lang, enca_fallback) == 2
1050 || sscanf(sub_cp, "ENCA:%2s:%s", enca_lang, enca_fallback) == 2) {
1051 subcp_open(enca_fallback);
1052 } else {
1053 subcp_open(sub_cp);
1057 #else
1058 void subcp_open_noenca ()
1060 subcp_open(sub_cp);
1062 #endif
1064 void subcp_open (char *current_sub_cp)
1066 char *tocp = "UTF-8";
1068 if (current_sub_cp){
1069 if ((icdsc = iconv_open (tocp, current_sub_cp)) != (iconv_t)(-1)){
1070 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: opened iconv descriptor.\n");
1071 sub_utf8 = 2;
1072 } else
1073 mp_msg(MSGT_SUBREADER,MSGL_ERR,"SUB: error opening iconv descriptor.\n");
1077 void subcp_close (void)
1079 if (icdsc != (iconv_t)(-1)){
1080 (void) iconv_close (icdsc);
1081 icdsc = (iconv_t)(-1);
1082 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: closed iconv descriptor.\n");
1086 #define ICBUFFSIZE 512
1087 static char icbuffer[ICBUFFSIZE];
1089 subtitle* subcp_recode (subtitle *sub)
1091 int l=sub->lines;
1092 size_t ileft, oleft;
1093 char *op, *ip, *ot;
1095 while (l){
1096 op = icbuffer;
1097 ip = sub->text[--l];
1098 ileft = strlen(ip);
1099 oleft = ICBUFFSIZE - 1;
1101 if (iconv(icdsc, &ip, &ileft,
1102 &op, &oleft) == (size_t)(-1)) {
1103 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error recoding line (1).\n");
1104 l++;
1105 break;
1107 if (!(ot = (char *)malloc(op - icbuffer + 1))){
1108 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1109 l++;
1110 break;
1112 *op='\0' ;
1113 strcpy (ot, icbuffer);
1114 free (sub->text[l]);
1115 sub->text[l] = ot;
1117 if (l){
1118 for (l = sub->lines; l;)
1119 free (sub->text[--l]);
1120 return ERR;
1122 return sub;
1125 // for demux_ogg.c:
1126 subtitle* subcp_recode1 (subtitle *sub)
1128 int l=sub->lines;
1129 size_t ileft, oleft;
1131 if(icdsc == (iconv_t)(-1)) return sub;
1133 while (l){
1134 char *ip = icbuffer;
1135 char *op = sub->text[--l];
1136 strcpy(ip, op);
1137 ileft = strlen(ip);
1138 oleft = ICBUFFSIZE - 1;
1140 if (iconv(icdsc, &ip, &ileft,
1141 &op, &oleft) == (size_t)(-1)) {
1142 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: error recoding line (2).\n");
1143 return sub;
1145 *op='\0' ;
1147 return sub;
1149 #endif
1151 #ifdef USE_FRIBIDI
1152 #ifndef max
1153 #define max(a,b) (((a)>(b))?(a):(b))
1154 #endif
1155 subtitle* sub_fribidi (subtitle *sub, int sub_utf8)
1157 FriBidiChar logical[LINE_LEN+1], visual[LINE_LEN+1]; // Hopefully these two won't smash the stack
1158 char *ip = NULL, *op = NULL;
1159 FriBidiCharType base;
1160 size_t len,orig_len;
1161 int l=sub->lines;
1162 int char_set_num;
1163 fribidi_boolean log2vis;
1164 if(flip_hebrew) { // Please fix the indentation someday
1165 fribidi_set_mirroring (FRIBIDI_TRUE);
1166 fribidi_set_reorder_nsm (FRIBIDI_FALSE);
1168 if( sub_utf8 == 0 ) {
1169 char_set_num = fribidi_parse_charset (fribidi_charset?fribidi_charset:"ISO8859-8");
1170 }else {
1171 char_set_num = fribidi_parse_charset ("UTF-8");
1173 while (l) {
1174 ip = sub->text[--l];
1175 orig_len = len = strlen( ip ); // We assume that we don't use full unicode, only UTF-8 or ISO8859-x
1176 if(len > LINE_LEN) {
1177 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: sub->text is longer than LINE_LEN.\n");
1178 l++;
1179 break;
1181 len = fribidi_charset_to_unicode (char_set_num, ip, len, logical);
1182 base = FRIBIDI_TYPE_ON;
1183 log2vis = fribidi_log2vis (logical, len, &base,
1184 /* output */
1185 visual, NULL, NULL, NULL);
1186 if(log2vis) {
1187 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL,
1188 NULL);
1189 if((op = (char*)malloc(sizeof(char)*(max(2*orig_len,2*len) + 1))) == NULL) {
1190 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1191 l++;
1192 break;
1194 fribidi_unicode_to_charset ( char_set_num, visual, len,op);
1195 free (ip);
1196 sub->text[l] = op;
1199 if (l){
1200 for (l = sub->lines; l;)
1201 free (sub->text[--l]);
1202 return ERR;
1205 return sub;
1208 #endif
1210 static void adjust_subs_time(subtitle* sub, float subtime, float fps, int block,
1211 int sub_num, int sub_uses_time) {
1212 int n,m;
1213 subtitle* nextsub;
1214 int i = sub_num;
1215 unsigned long subfms = (sub_uses_time ? 100 : fps) * subtime;
1216 unsigned long overlap = (sub_uses_time ? 100 : fps) / 5; // 0.2s
1218 n=m=0;
1219 if (i) for (;;){
1220 if (sub->end <= sub->start){
1221 sub->end = sub->start + subfms;
1222 m++;
1223 n++;
1225 if (!--i) break;
1226 nextsub = sub + 1;
1227 if(block){
1228 if ((sub->end > nextsub->start) && (sub->end <= nextsub->start + overlap)) {
1229 // these subtitles overlap for less than 0.2 seconds
1230 // and would result in very short overlapping subtitle
1231 // so let's fix the problem here, before overlapping code
1232 // get its hands on them
1233 unsigned delta = sub->end - nextsub->start, half = delta / 2;
1234 sub->end -= half + 1;
1235 nextsub->start += delta - half;
1237 if (sub->end >= nextsub->start){
1238 sub->end = nextsub->start - 1;
1239 if (sub->end - sub->start > subfms)
1240 sub->end = sub->start + subfms;
1241 if (!m)
1242 n++;
1246 /* Theory:
1247 * Movies are often converted from FILM (24 fps)
1248 * to PAL (25) by simply speeding it up, so we
1249 * to multiply the original timestmaps by
1250 * (Movie's FPS / Subtitle's (guessed) FPS)
1251 * so eg. for 23.98 fps movie and PAL time based
1252 * subtitles we say -subfps 25 and we're fine!
1255 /* timed sub fps correction ::atmos */
1256 if(sub_uses_time && sub_fps) {
1257 sub->start *= sub_fps/fps;
1258 sub->end *= sub_fps/fps;
1261 sub = nextsub;
1262 m = 0;
1264 if (n) mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Adjusted %d subtitle(s).\n", n);
1267 struct subreader {
1268 subtitle * (*read)(FILE *fd,subtitle *dest);
1269 void (*post)(subtitle *dest);
1270 const char *name;
1273 #ifdef HAVE_ENCA
1274 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1275 void* guess_cp(FILE *fd, char *preferred_language, char *fallback)
1277 const char **languages;
1278 size_t langcnt, buflen;
1279 EncaAnalyser analyser;
1280 EncaEncoding encoding;
1281 unsigned char *buffer;
1282 char *detected_sub_cp = NULL;
1283 int i;
1285 buffer = (unsigned char*)malloc(MAX_GUESS_BUFFER_SIZE*sizeof(char));
1286 buflen = fread(buffer, 1, MAX_GUESS_BUFFER_SIZE, fd);
1288 languages = enca_get_languages(&langcnt);
1289 mp_msg(MSGT_SUBREADER, MSGL_V, "ENCA supported languages: ");
1290 for (i = 0; i < langcnt; i++) {
1291 mp_msg(MSGT_SUBREADER, MSGL_V, "%s ", languages[i]);
1293 mp_msg(MSGT_SUBREADER, MSGL_V, "\n");
1295 for (i = 0; i < langcnt; i++) {
1296 if (strcasecmp(languages[i], preferred_language) != 0) continue;
1297 analyser = enca_analyser_alloc(languages[i]);
1298 encoding = enca_analyse_const(analyser, buffer, buflen);
1299 mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detected charset: %s\n", enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV));
1300 detected_sub_cp = strdup(enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV));
1301 enca_analyser_free(analyser);
1304 free(languages);
1305 free(buffer);
1306 rewind(fd);
1308 if (!detected_sub_cp) detected_sub_cp = strdup(fallback);
1310 return detected_sub_cp;
1312 #endif
1314 sub_data* sub_read_file (char *filename, float fps) {
1315 //filename is assumed to be malloc'ed, free() is used in sub_free()
1316 FILE *fd;
1317 int n_max, n_first, i, j, sub_first, sub_orig;
1318 subtitle *first, *second, *sub, *return_sub;
1319 sub_data *subt_data;
1320 char enca_lang[100], enca_fallback[100];
1321 int uses_time = 0, sub_num = 0, sub_errs = 0;
1322 char *current_sub_cp=NULL;
1323 struct subreader sr[]=
1325 { sub_read_line_microdvd, NULL, "microdvd" },
1326 { sub_read_line_subrip, NULL, "subrip" },
1327 { sub_read_line_subviewer, NULL, "subviewer" },
1328 { sub_read_line_sami, NULL, "sami" },
1329 { sub_read_line_vplayer, NULL, "vplayer" },
1330 { sub_read_line_rt, NULL, "rt" },
1331 { sub_read_line_ssa, sub_pp_ssa, "ssa" },
1332 { sub_read_line_pjs, NULL, "pjs" },
1333 { sub_read_line_mpsub, NULL, "mpsub" },
1334 { sub_read_line_aqt, NULL, "aqt" },
1335 { sub_read_line_subviewer2, NULL, "subviewer 2.0" },
1336 { sub_read_line_subrip09, NULL, "subrip 0.9" },
1337 { sub_read_line_jacosub, NULL, "jacosub" },
1338 { sub_read_line_mpl2, NULL, "mpl2" }
1340 struct subreader *srp;
1342 if(filename==NULL) return NULL; //qnx segfault
1343 fd=fopen (filename, "r"); if (!fd) return NULL;
1345 sub_format=sub_autodetect (fd, &uses_time);
1346 mpsub_multiplier = (uses_time ? 100.0 : 1.0);
1347 if (sub_format==SUB_INVALID) {mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: Could not determine file format\n");return NULL;}
1348 srp=sr+sub_format;
1349 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Detected subtitle file format: %s\n", srp->name);
1351 rewind (fd);
1353 #ifdef USE_ICONV
1354 #ifdef HAVE_ENCA
1355 if (sscanf(sub_cp, "enca:%2s:%s", enca_lang, enca_fallback) == 2
1356 || sscanf(sub_cp, "ENCA:%2s:%s", enca_lang, enca_fallback) == 2) {
1357 current_sub_cp = guess_cp(fd, enca_lang, enca_fallback);
1358 } else {
1359 current_sub_cp = sub_cp ? strdup(sub_cp) : NULL;
1361 #else
1362 current_sub_cp = sub_cp ? strdup(sub_cp) : NULL;
1363 #endif
1365 sub_utf8_prev=sub_utf8;
1367 int l,k;
1368 k = -1;
1369 if ((l=strlen(filename))>4){
1370 char *exts[] = {".utf", ".utf8", ".utf-8" };
1371 for (k=3;--k>=0;)
1372 if (!strcasecmp(filename+(l - strlen(exts[k])), exts[k])){
1373 sub_utf8 = 1;
1374 break;
1377 if (k<0) subcp_open(current_sub_cp);
1379 #endif
1380 if (current_sub_cp) free(current_sub_cp);
1382 sub_num=0;n_max=32;
1383 first=(subtitle *)malloc(n_max*sizeof(subtitle));
1384 if(!first){
1385 #ifdef USE_ICONV
1386 subcp_close();
1387 sub_utf8=sub_utf8_prev;
1388 #endif
1389 return NULL;
1392 #ifdef USE_SORTSUB
1393 sub = (subtitle *)malloc(sizeof(subtitle));
1394 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1395 //as the beginning of the following
1396 previous_sub_end = 0;
1397 #endif
1398 while(1){
1399 if(sub_num>=n_max){
1400 n_max+=16;
1401 first=realloc(first,n_max*sizeof(subtitle));
1403 #ifndef USE_SORTSUB
1404 sub = &first[sub_num];
1405 #endif
1406 memset(sub, '\0', sizeof(subtitle));
1407 sub=srp->read(fd,sub);
1408 if(!sub) break; // EOF
1409 #ifdef USE_ICONV
1410 if ((sub!=ERR) && (sub_utf8 & 2)) sub=subcp_recode(sub);
1411 #endif
1412 #ifdef USE_FRIBIDI
1413 if (sub!=ERR) sub=sub_fribidi(sub,sub_utf8);
1414 #endif
1415 if ( sub == ERR )
1417 #ifdef USE_ICONV
1418 subcp_close();
1419 #endif
1420 if ( first ) free(first);
1421 return NULL;
1423 // Apply any post processing that needs recoding first
1424 if ((sub!=ERR) && !sub_no_text_pp && srp->post) srp->post(sub);
1425 #ifdef USE_SORTSUB
1426 if(!sub_num || (first[sub_num - 1].start <= sub->start)){
1427 first[sub_num].start = sub->start;
1428 first[sub_num].end = sub->end;
1429 first[sub_num].lines = sub->lines;
1430 first[sub_num].alignment = sub->alignment;
1431 for(i = 0; i < sub->lines; ++i){
1432 first[sub_num].text[i] = sub->text[i];
1434 if (previous_sub_end){
1435 first[sub_num - 1].end = previous_sub_end;
1436 previous_sub_end = 0;
1438 } else {
1439 for(j = sub_num - 1; j >= 0; --j){
1440 first[j + 1].start = first[j].start;
1441 first[j + 1].end = first[j].end;
1442 first[j + 1].lines = first[j].lines;
1443 first[j + 1].alignment = first[j].alignment;
1444 for(i = 0; i < first[j].lines; ++i){
1445 first[j + 1].text[i] = first[j].text[i];
1447 if(!j || (first[j - 1].start <= sub->start)){
1448 first[j].start = sub->start;
1449 first[j].end = sub->end;
1450 first[j].lines = sub->lines;
1451 first[j].alignment = sub->alignment;
1452 for(i = 0; i < SUB_MAX_TEXT; ++i){
1453 first[j].text[i] = sub->text[i];
1455 if (previous_sub_end){
1456 first[j].end = first[j - 1].end;
1457 first[j - 1].end = previous_sub_end;
1458 previous_sub_end = 0;
1460 break;
1464 #endif
1465 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
1468 fclose(fd);
1470 #ifdef USE_ICONV
1471 subcp_close();
1472 #endif
1474 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1475 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Read %i subtitles", sub_num);
1476 if (sub_errs) mp_msg(MSGT_SUBREADER,MSGL_INFO,", %i bad line(s).\n", sub_errs);
1477 else mp_msg(MSGT_SUBREADER,MSGL_INFO,".\n");
1479 if(sub_num<=0){
1480 free(first);
1481 return NULL;
1484 // we do overlap if the user forced it (suboverlap_enable == 2) or
1485 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1486 // this is because usually overlapping subtitles are found in these formats,
1487 // while in others they are probably result of bad timing
1488 if ((suboverlap_enabled == 2) ||
1489 ((suboverlap_enabled) && ((sub_format == SUB_JACOSUB) || (sub_format == SUB_SSA)))) {
1490 adjust_subs_time(first, 6.0, fps, 0, sub_num, uses_time);/*~6 secs AST*/
1491 // here we manage overlapping subtitles
1492 sub_orig = sub_num;
1493 n_first = sub_num;
1494 sub_num = 0;
1495 second = NULL;
1496 // for each subtitle in first[] we deal with its 'block' of
1497 // bonded subtitles
1498 for (sub_first = 0; sub_first < n_first; ++sub_first) {
1499 unsigned long global_start = first[sub_first].start,
1500 global_end = first[sub_first].end, local_start, local_end;
1501 int lines_to_add = first[sub_first].lines, sub_to_add = 0,
1502 **placeholder = NULL, higher_line = 0, counter, start_block_sub = sub_num;
1503 char real_block = 1;
1505 // here we find the number of subtitles inside the 'block'
1506 // and its span interval. this works well only with sorted
1507 // subtitles
1508 while ((sub_first + sub_to_add + 1 < n_first) && (first[sub_first + sub_to_add + 1].start < global_end)) {
1509 ++sub_to_add;
1510 lines_to_add += first[sub_first + sub_to_add].lines;
1511 if (first[sub_first + sub_to_add].start < global_start) {
1512 global_start = first[sub_first + sub_to_add].start;
1514 if (first[sub_first + sub_to_add].end > global_end) {
1515 global_end = first[sub_first + sub_to_add].end;
1519 // we need a structure to keep trace of the screen lines
1520 // used by the subs, a 'placeholder'
1521 counter = 2 * sub_to_add + 1; // the maximum number of subs derived
1522 // from a block of sub_to_add+1 subs
1523 placeholder = (int **) malloc(sizeof(int *) * counter);
1524 for (i = 0; i < counter; ++i) {
1525 placeholder[i] = (int *) malloc(sizeof(int) * lines_to_add);
1526 for (j = 0; j < lines_to_add; ++j) {
1527 placeholder[i][j] = -1;
1531 counter = 0;
1532 local_end = global_start - 1;
1533 do {
1534 int ls;
1536 // here we find the beginning and the end of a new
1537 // subtitle in the block
1538 local_start = local_end + 1;
1539 local_end = global_end;
1540 for (j = 0; j <= sub_to_add; ++j) {
1541 if ((first[sub_first + j].start - 1 > local_start) && (first[sub_first + j].start - 1 < local_end)) {
1542 local_end = first[sub_first + j].start - 1;
1543 } else if ((first[sub_first + j].end > local_start) && (first[sub_first + j].end < local_end)) {
1544 local_end = first[sub_first + j].end;
1547 // here we allocate the screen lines to subs we must
1548 // display in current local_start-local_end interval.
1549 // if the subs were yet presents in the previous interval
1550 // they keep the same lines, otherside they get unused lines
1551 for (j = 0; j <= sub_to_add; ++j) {
1552 if ((first[sub_first + j].start <= local_end) && (first[sub_first + j].end > local_start)) {
1553 unsigned long sub_lines = first[sub_first + j].lines, fragment_length = lines_to_add + 1,
1554 tmp = 0;
1555 char boolean = 0;
1556 int fragment_position = -1;
1558 // if this is not the first new sub of the block
1559 // we find if this sub was present in the previous
1560 // new sub
1561 if (counter)
1562 for (i = 0; i < lines_to_add; ++i) {
1563 if (placeholder[counter - 1][i] == sub_first + j) {
1564 placeholder[counter][i] = sub_first + j;
1565 boolean = 1;
1568 if (boolean)
1569 continue;
1571 // we are looking for the shortest among all groups of
1572 // sequential blank lines whose length is greater than or
1573 // equal to sub_lines. we store in fragment_position the
1574 // position of the shortest group, in fragment_length its
1575 // length, and in tmp the length of the group currently
1576 // examinated
1577 for (i = 0; i < lines_to_add; ++i) {
1578 if (placeholder[counter][i] == -1) {
1579 // placeholder[counter][i] is part of the current group
1580 // of blank lines
1581 ++tmp;
1582 } else {
1583 if (tmp == sub_lines) {
1584 // current group's size fits exactly the one we
1585 // need, so we stop looking
1586 fragment_position = i - tmp;
1587 tmp = 0;
1588 break;
1590 if ((tmp) && (tmp > sub_lines) && (tmp < fragment_length)) {
1591 // current group is the best we found till here,
1592 // but is still bigger than the one we are looking
1593 // for, so we keep on looking
1594 fragment_length = tmp;
1595 fragment_position = i - tmp;
1596 tmp = 0;
1597 } else {
1598 // current group doesn't fit at all, so we forget it
1599 tmp = 0;
1603 if (tmp) {
1604 // last screen line is blank, a group ends with it
1605 if ((tmp >= sub_lines) && (tmp < fragment_length)) {
1606 fragment_position = i - tmp;
1609 if (fragment_position == -1) {
1610 // it was not possible to find free screen line(s) for a subtitle,
1611 // usually this means a bug in the code; however we do not overlap
1612 mp_msg(MSGT_SUBREADER, MSGL_WARN, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1613 higher_line = SUB_MAX_TEXT + 1;
1614 break;
1615 } else {
1616 for (tmp = 0; tmp < sub_lines; ++tmp) {
1617 placeholder[counter][fragment_position + tmp] = sub_first + j;
1622 for (j = higher_line + 1; j < lines_to_add; ++j) {
1623 if (placeholder[counter][j] != -1)
1624 higher_line = j;
1625 else
1626 break;
1628 if (higher_line >= SUB_MAX_TEXT) {
1629 // the 'block' has too much lines, so we don't overlap the
1630 // subtitles
1631 second = (subtitle *) realloc(second, (sub_num + sub_to_add + 1) * sizeof(subtitle));
1632 for (j = 0; j <= sub_to_add; ++j) {
1633 int ls;
1634 memset(&second[sub_num + j], '\0', sizeof(subtitle));
1635 second[sub_num + j].start = first[sub_first + j].start;
1636 second[sub_num + j].end = first[sub_first + j].end;
1637 second[sub_num + j].lines = first[sub_first + j].lines;
1638 second[sub_num + j].alignment = first[sub_first + j].alignment;
1639 for (ls = 0; ls < second[sub_num + j].lines; ls++) {
1640 second[sub_num + j].text[ls] = strdup(first[sub_first + j].text[ls]);
1643 sub_num += sub_to_add + 1;
1644 sub_first += sub_to_add;
1645 real_block = 0;
1646 break;
1649 // we read the placeholder structure and create the new
1650 // subs.
1651 second = (subtitle *) realloc(second, (sub_num + 1) * sizeof(subtitle));
1652 memset(&second[sub_num], '\0', sizeof(subtitle));
1653 second[sub_num].start = local_start;
1654 second[sub_num].end = local_end;
1655 second[sub_num].alignment = SUB_ALIGNMENT_HCENTER;
1656 n_max = (lines_to_add < SUB_MAX_TEXT) ? lines_to_add : SUB_MAX_TEXT;
1657 for (i = 0, j = 0; j < n_max; ++j) {
1658 if (placeholder[counter][j] != -1) {
1659 int lines = first[placeholder[counter][j]].lines;
1660 for (ls = 0; ls < lines; ++ls) {
1661 second[sub_num].text[i++] = strdup(first[placeholder[counter][j]].text[ls]);
1663 j += lines - 1;
1664 } else {
1665 second[sub_num].text[i++] = strdup(" ");
1668 ++sub_num;
1669 ++counter;
1670 } while (local_end < global_end);
1671 if (real_block)
1672 for (i = 0; i < counter; ++i)
1673 second[start_block_sub + i].lines = higher_line + 1;
1675 counter = 2 * sub_to_add + 1;
1676 for (i = 0; i < counter; ++i) {
1677 free(placeholder[i]);
1679 free(placeholder);
1680 sub_first += sub_to_add;
1683 for (j = sub_orig - 1; j >= 0; --j) {
1684 for (i = first[j].lines - 1; i >= 0; --i) {
1685 free(first[j].text[i]);
1688 free(first);
1690 return_sub = second;
1691 } else { //if(suboverlap_enabled)
1692 adjust_subs_time(first, 6.0, fps, 1, sub_num, uses_time);/*~6 secs AST*/
1693 return_sub = first;
1695 if (return_sub == NULL) return NULL;
1696 subt_data = (sub_data *)malloc(sizeof(sub_data));
1697 subt_data->filename = filename;
1698 subt_data->sub_uses_time = uses_time;
1699 subt_data->sub_num = sub_num;
1700 subt_data->sub_errs = sub_errs;
1701 subt_data->subtitles = return_sub;
1702 return subt_data;
1705 #if 0
1706 char * strreplace( char * in,char * what,char * whereof )
1708 int i;
1709 char * tmp;
1711 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL;
1712 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i];
1713 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0;
1714 return in;
1716 #endif
1719 static void strcpy_trim(char *d, char *s)
1721 // skip leading whitespace
1722 while (*s && !isalnum(*s)) {
1723 s++;
1725 for (;;) {
1726 // copy word
1727 while (*s && isalnum(*s)) {
1728 *d = tolower(*s);
1729 s++; d++;
1731 if (*s == 0) break;
1732 // trim excess whitespace
1733 while (*s && !isalnum(*s)) {
1734 s++;
1736 if (*s == 0) break;
1737 *d++ = ' ';
1739 *d = 0;
1742 static void strcpy_strip_ext(char *d, char *s)
1744 char *tmp = strrchr(s,'.');
1745 if (!tmp) {
1746 strcpy(d, s);
1747 return;
1748 } else {
1749 strncpy(d, s, tmp-s);
1750 d[tmp-s] = 0;
1752 while (*d) {
1753 *d = tolower(*d);
1754 d++;
1758 static void strcpy_get_ext(char *d, char *s)
1760 char *tmp = strrchr(s,'.');
1761 if (!tmp) {
1762 strcpy(d, "");
1763 return;
1764 } else {
1765 strcpy(d, tmp+1);
1769 static int whiteonly(char *s)
1771 while (*s) {
1772 if (isalnum(*s)) return 0;
1773 s++;
1775 return 1;
1778 typedef struct _subfn
1780 int priority;
1781 char *fname;
1782 } subfn;
1784 static int compare_sub_priority(const void *a, const void *b)
1786 if (((subfn*)a)->priority > ((subfn*)b)->priority) {
1787 return -1;
1788 } else if (((subfn*)a)->priority < ((subfn*)b)->priority) {
1789 return 1;
1790 } else {
1791 return strcoll(((subfn*)a)->fname, ((subfn*)b)->fname);
1795 char** sub_filenames(char* path, char *fname)
1797 char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
1798 char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult;
1800 int len, pos, found, i, j;
1801 char * sub_exts[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
1802 subfn *result;
1803 char **result2;
1805 int subcnt;
1807 FILE *f;
1809 DIR *d;
1810 struct dirent *de;
1812 len = (strlen(fname) > 256 ? strlen(fname) : 256)
1813 +(strlen(path) > 256 ? strlen(path) : 256)+2;
1815 f_dir = (char*)malloc(len);
1816 f_fname = (char*)malloc(len);
1817 f_fname_noext = (char*)malloc(len);
1818 f_fname_trim = (char*)malloc(len);
1820 tmp_fname_noext = (char*)malloc(len);
1821 tmp_fname_trim = (char*)malloc(len);
1822 tmp_fname_ext = (char*)malloc(len);
1824 tmpresult = (char*)malloc(len);
1826 result = (subfn*)malloc(sizeof(subfn)*MAX_SUBTITLE_FILES);
1827 memset(result, 0, sizeof(subfn)*MAX_SUBTITLE_FILES);
1829 subcnt = 0;
1831 tmp = strrchr(fname,'/');
1832 #ifdef WIN32
1833 if(!tmp)tmp = strrchr(fname,'\\');
1834 #endif
1836 // extract filename & dirname from fname
1837 if (tmp) {
1838 strcpy(f_fname, tmp+1);
1839 pos = tmp - fname;
1840 strncpy(f_dir, fname, pos+1);
1841 f_dir[pos+1] = 0;
1842 } else {
1843 strcpy(f_fname, fname);
1844 strcpy(f_dir, "./");
1847 strcpy_strip_ext(f_fname_noext, f_fname);
1848 strcpy_trim(f_fname_trim, f_fname_noext);
1850 tmp_sub_id = NULL;
1851 if (dvdsub_lang && !whiteonly(dvdsub_lang)) {
1852 tmp_sub_id = (char*)malloc(strlen(dvdsub_lang)+1);
1853 strcpy_trim(tmp_sub_id, dvdsub_lang);
1856 // 0 = nothing
1857 // 1 = any subtitle file
1858 // 2 = any sub file containing movie name
1859 // 3 = sub file containing movie name and the lang extension
1860 for (j = 0; j <= 1; j++) {
1861 d = opendir(j == 0 ? f_dir : path);
1862 if (d) {
1863 while ((de = readdir(d))) {
1864 // retrieve various parts of the filename
1865 strcpy_strip_ext(tmp_fname_noext, de->d_name);
1866 strcpy_get_ext(tmp_fname_ext, de->d_name);
1867 strcpy_trim(tmp_fname_trim, tmp_fname_noext);
1869 // does it end with a subtitle extension?
1870 found = 0;
1871 #ifdef USE_ICONV
1872 #ifdef HAVE_ENCA
1873 for (i = ((sub_cp && strncasecmp(sub_cp, "enca", 4) != 0) ? 3 : 0); sub_exts[i]; i++) {
1874 #else
1875 for (i = (sub_cp ? 3 : 0); sub_exts[i]; i++) {
1876 #endif
1877 #else
1878 for (i = 0; sub_exts[i]; i++) {
1879 #endif
1880 if (strcmp(sub_exts[i], tmp_fname_ext) == 0) {
1881 found = 1;
1882 break;
1886 // we have a (likely) subtitle file
1887 if (found) {
1888 int prio = 0;
1889 if (!prio && tmp_sub_id)
1891 sprintf(tmpresult, "%s %s", f_fname_trim, tmp_sub_id);
1892 printf("dvdsublang...%s\n", tmpresult);
1893 if (strcmp(tmp_fname_trim, tmpresult) == 0 && sub_match_fuzziness >= 1) {
1894 // matches the movie name + lang extension
1895 prio = 5;
1898 if (!prio && strcmp(tmp_fname_trim, f_fname_trim) == 0) {
1899 // matches the movie name
1900 prio = 4;
1902 if (!prio && (tmp = strstr(tmp_fname_trim, f_fname_trim)) && (sub_match_fuzziness >= 1)) {
1903 // contains the movie name
1904 tmp += strlen(f_fname_trim);
1905 if (tmp_sub_id && strstr(tmp, tmp_sub_id)) {
1906 // with sub_id specified prefer localized subtitles
1907 prio = 3;
1908 } else if ((tmp_sub_id == NULL) && whiteonly(tmp)) {
1909 // without sub_id prefer "plain" name
1910 prio = 3;
1911 } else {
1912 // with no localized subs found, try any else instead
1913 prio = 2;
1916 if (!prio) {
1917 // doesn't contain the movie name
1918 // don't try in the mplayer subtitle directory
1919 if ((j == 0) && (sub_match_fuzziness >= 2)) {
1920 prio = 1;
1924 if (prio) {
1925 prio += prio;
1926 #ifdef USE_ICONV
1927 if (i<3){ // prefer UTF-8 coded
1928 prio++;
1930 #endif
1931 sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name);
1932 // fprintf(stderr, "%s priority %d\n", tmpresult, prio);
1933 if ((f = fopen(tmpresult, "rt"))) {
1934 fclose(f);
1935 result[subcnt].priority = prio;
1936 result[subcnt].fname = strdup(tmpresult);
1937 subcnt++;
1942 if (subcnt >= MAX_SUBTITLE_FILES) break;
1944 closedir(d);
1949 if (tmp_sub_id) free(tmp_sub_id);
1951 free(f_dir);
1952 free(f_fname);
1953 free(f_fname_noext);
1954 free(f_fname_trim);
1956 free(tmp_fname_noext);
1957 free(tmp_fname_trim);
1958 free(tmp_fname_ext);
1960 free(tmpresult);
1962 qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
1964 result2 = (char**)malloc(sizeof(char*)*(subcnt+1));
1965 memset(result2, 0, sizeof(char*)*(subcnt+1));
1967 for (i = 0; i < subcnt; i++) {
1968 result2[i] = result[i].fname;
1970 result2[subcnt] = NULL;
1972 free(result);
1974 return result2;
1977 void list_sub_file(sub_data* subd){
1978 int i,j;
1979 subtitle *subs = subd->subtitles;
1981 for(j=0; j < subd->sub_num; j++){
1982 subtitle* egysub=&subs[j];
1983 printf ("%i line%c (%li-%li)\n",
1984 egysub->lines,
1985 (1==egysub->lines)?' ':'s',
1986 egysub->start,
1987 egysub->end);
1988 for (i=0; i<egysub->lines; i++) {
1989 printf ("\t\t%d: %s%s", i,egysub->text[i], i==egysub->lines-1?"":" \n ");
1991 printf ("\n");
1994 printf ("Subtitle format %s time.\n",
1995 subd->sub_uses_time ? "uses":"doesn't use");
1996 printf ("Read %i subtitles, %i errors.\n", subd->sub_num, subd->sub_errs);
1999 void dump_srt(sub_data* subd, float fps){
2000 int i,j;
2001 int h,m,s,ms;
2002 FILE * fd;
2003 subtitle * onesub;
2004 unsigned long temp;
2005 subtitle *subs = subd->subtitles;
2007 if (!subd->sub_uses_time && sub_fps == 0)
2008 sub_fps = fps;
2009 fd=fopen("dumpsub.srt","w");
2010 if(!fd)
2012 perror("dump_srt: fopen");
2013 return;
2015 for(i=0; i < subd->sub_num; i++)
2017 onesub=subs+i; //=&subs[i];
2018 fprintf(fd,"%d\n",i+1);//line number
2020 temp=onesub->start;
2021 if (!subd->sub_uses_time)
2022 temp = temp * 100 / sub_fps;
2023 temp -= sub_delay * 100;
2024 h=temp/360000;temp%=360000; //h =1*100*60*60
2025 m=temp/6000; temp%=6000; //m =1*100*60
2026 s=temp/100; temp%=100; //s =1*100
2027 ms=temp*10; //ms=1*10
2028 fprintf(fd,"%02d:%02d:%02d,%03d --> ",h,m,s,ms);
2030 temp=onesub->end;
2031 if (!subd->sub_uses_time)
2032 temp = temp * 100 / sub_fps;
2033 temp -= sub_delay * 100;
2034 h=temp/360000;temp%=360000;
2035 m=temp/6000; temp%=6000;
2036 s=temp/100; temp%=100;
2037 ms=temp*10;
2038 fprintf(fd,"%02d:%02d:%02d,%03d\n",h,m,s,ms);
2040 for(j=0;j<onesub->lines;j++)
2041 fprintf(fd,"%s\n",onesub->text[j]);
2043 fprintf(fd,"\n");
2045 fclose(fd);
2046 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
2049 void dump_mpsub(sub_data* subd, float fps){
2050 int i,j;
2051 FILE *fd;
2052 float a,b;
2053 subtitle *subs = subd->subtitles;
2055 mpsub_position = subd->sub_uses_time? (sub_delay*100) : (sub_delay*fps);
2056 if (sub_fps==0) sub_fps=fps;
2058 fd=fopen ("dump.mpsub", "w");
2059 if (!fd) {
2060 perror ("dump_mpsub: fopen");
2061 return;
2065 if (subd->sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n");
2066 else fprintf (fd, "FORMAT=%5.2f\n\n", fps);
2068 for(j=0; j < subd->sub_num; j++){
2069 subtitle* egysub=&subs[j];
2070 if (subd->sub_uses_time) {
2071 a=((egysub->start-mpsub_position)/100.0);
2072 b=((egysub->end-egysub->start)/100.0);
2073 if ( (float)((int)a) == a)
2074 fprintf (fd, "%.0f",a);
2075 else
2076 fprintf (fd, "%.2f",a);
2078 if ( (float)((int)b) == b)
2079 fprintf (fd, " %.0f\n",b);
2080 else
2081 fprintf (fd, " %.2f\n",b);
2082 } else {
2083 fprintf (fd, "%ld %ld\n", (long)((egysub->start*(fps/sub_fps))-((mpsub_position*(fps/sub_fps)))),
2084 (long)(((egysub->end)-(egysub->start))*(fps/sub_fps)));
2087 mpsub_position = egysub->end;
2088 for (i=0; i<egysub->lines; i++) {
2089 fprintf (fd, "%s\n",egysub->text[i]);
2091 fprintf (fd, "\n");
2093 fclose (fd);
2094 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
2097 void dump_microdvd(sub_data* subd, float fps) {
2098 int i, delay;
2099 FILE *fd;
2100 subtitle *subs = subd->subtitles;
2101 if (sub_fps == 0)
2102 sub_fps = fps;
2103 fd = fopen("dumpsub.txt", "w");
2104 if (!fd) {
2105 perror("dumpsub.txt: fopen");
2106 return;
2108 delay = sub_delay * sub_fps;
2109 for (i = 0; i < subd->sub_num; ++i) {
2110 int j, start, end;
2111 start = subs[i].start;
2112 end = subs[i].end;
2113 if (subd->sub_uses_time) {
2114 start = start * sub_fps / 100 ;
2115 end = end * sub_fps / 100;
2117 else {
2118 start = start * sub_fps / fps;
2119 end = end * sub_fps / fps;
2121 start -= delay;
2122 end -= delay;
2123 fprintf(fd, "{%d}{%d}", start, end);
2124 for (j = 0; j < subs[i].lines; ++j)
2125 fprintf(fd, "%s%s", j ? "|" : "", subs[i].text[j]);
2126 fprintf(fd, "\n");
2128 fclose(fd);
2129 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");
2132 void dump_jacosub(sub_data* subd, float fps) {
2133 int i,j;
2134 int h,m,s,cs;
2135 FILE * fd;
2136 subtitle * onesub;
2137 unsigned long temp;
2138 subtitle *subs = subd->subtitles;
2140 if (!subd->sub_uses_time && sub_fps == 0)
2141 sub_fps = fps;
2142 fd=fopen("dumpsub.jss","w");
2143 if(!fd)
2145 perror("dump_jacosub: fopen");
2146 return;
2148 fprintf(fd, "#TIMERES %d\n", (subd->sub_uses_time) ? 100 : (int)sub_fps);
2149 for(i=0; i < subd->sub_num; i++)
2151 onesub=subs+i; //=&subs[i];
2153 temp=onesub->start;
2154 if (!subd->sub_uses_time)
2155 temp = temp * 100 / sub_fps;
2156 temp -= sub_delay * 100;
2157 h=temp/360000;temp%=360000; //h =1*100*60*60
2158 m=temp/6000; temp%=6000; //m =1*100*60
2159 s=temp/100; temp%=100; //s =1*100
2160 cs=temp; //cs=1*10
2161 fprintf(fd,"%02d:%02d:%02d.%02d ",h,m,s,cs);
2163 temp=onesub->end;
2164 if (!subd->sub_uses_time)
2165 temp = temp * 100 / sub_fps;
2166 temp -= sub_delay * 100;
2167 h=temp/360000;temp%=360000;
2168 m=temp/6000; temp%=6000;
2169 s=temp/100; temp%=100;
2170 cs=temp;
2171 fprintf(fd,"%02d:%02d:%02d.%02d {~} ",h,m,s,cs);
2173 for(j=0;j<onesub->lines;j++)
2174 fprintf(fd,"%s%s",j ? "\\n" : "", onesub->text[j]);
2176 fprintf(fd,"\n");
2178 fclose(fd);
2179 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
2182 void dump_sami(sub_data* subd, float fps) {
2183 int i,j;
2184 FILE * fd;
2185 subtitle * onesub;
2186 unsigned long temp;
2187 subtitle *subs = subd->subtitles;
2189 if (!subd->sub_uses_time && sub_fps == 0)
2190 sub_fps = fps;
2191 fd=fopen("dumpsub.smi","w");
2192 if(!fd)
2194 perror("dump_jacosub: fopen");
2195 return;
2197 fprintf(fd, "<SAMI>\n"
2198 "<HEAD>\n"
2199 " <STYLE TYPE=\"Text/css\">\n"
2200 " <!--\n"
2201 " P {margin-left: 29pt; margin-right: 29pt; font-size: 24pt; text-align: center; font-family: Tahoma; font-weight: bold; color: #FCDD03; background-color: #000000;}\n"
2202 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2203 " -->\n"
2204 " </STYLE>\n"
2205 "</HEAD>\n"
2206 "<BODY>\n");
2207 for(i=0; i < subd->sub_num; i++)
2209 onesub=subs+i; //=&subs[i];
2211 temp=onesub->start;
2212 if (!subd->sub_uses_time)
2213 temp = temp * 100 / sub_fps;
2214 temp -= sub_delay * 100;
2215 fprintf(fd,"\t<SYNC Start=%lu>\n"
2216 "\t <P>", temp * 10);
2218 for(j=0;j<onesub->lines;j++)
2219 fprintf(fd,"%s%s",j ? "<br>" : "", onesub->text[j]);
2221 fprintf(fd,"\n");
2223 temp=onesub->end;
2224 if (!subd->sub_uses_time)
2225 temp = temp * 100 / sub_fps;
2226 temp -= sub_delay * 100;
2227 fprintf(fd,"\t<SYNC Start=%lu>\n"
2228 "\t <P>&nbsp;\n", temp * 10);
2230 fprintf(fd, "</BODY>\n"
2231 "</SAMI>\n");
2232 fclose(fd);
2233 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2236 void sub_free( sub_data * subd )
2238 int i;
2240 if ( !subd ) return;
2242 if (subd->subtitles) {
2243 for (i=0; i < subd->subtitles->lines; i++) free( subd->subtitles->text[i] );
2244 free( subd->subtitles );
2246 if (subd->filename) free( subd->filename );
2247 free( subd );
2250 #ifdef DUMPSUBS
2251 int main(int argc, char **argv) { // for testing
2252 sub_data *subd;
2254 if(argc<2){
2255 printf("\nUsage: subreader filename.sub\n\n");
2256 exit(1);
2258 sub_cp = argv[2];
2259 subd = sub_read_file(argv[1]);
2260 if(!subd){
2261 printf("Couldn't load file.\n");
2262 exit(1);
2265 list_sub_file(subd);
2267 return 0;
2269 #endif