gl_common: minor cleanup/refactor
[mplayer.git] / sub / subreader.c
blob68c4ecbaf3264a438f0cbbaac746bde7d52ef58a
1 /*
2 * Subtitle reader with format autodetection
4 * Copyright (c) 2001 laaz
5 * Some code cleanup & realloc() by A'rpi/ESP-team
7 * This file is part of MPlayer.
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <dirent.h>
30 #include "config.h"
31 #include "mp_msg.h"
32 #include "subreader.h"
33 #include "mpcommon.h"
34 #include "subassconvert.h"
35 #include "options.h"
36 #include "stream/stream.h"
37 #include "libavutil/common.h"
38 #include "libavutil/avstring.h"
40 #ifdef CONFIG_ENCA
41 #include <enca.h>
42 #endif
44 #define ERR ((void *) -1)
46 #ifdef CONFIG_ICONV
47 #include <iconv.h>
48 char *sub_cp=NULL;
49 #endif
51 int suboverlap_enabled = 1;
53 // Parameter struct for the format-specific readline functions
54 struct readline_args {
55 int utf16;
56 struct MPOpts *opts;
59 /* Maximal length of line of a subtitle */
60 #define LINE_LEN 1000
61 static float mpsub_position=0;
62 static float mpsub_multiplier=1.;
63 static int sub_slacktime = 20000; //20 sec
65 int sub_no_text_pp=0; // 1 => do not apply text post-processing
66 // like {\...} elimination in SSA format.
68 int sub_match_fuzziness=0; // level of sub name matching fuzziness
70 /* Use the SUB_* constant defined in the header file */
71 int sub_format=SUB_INVALID;
72 #ifdef CONFIG_SORTSUB
74 Some subtitling formats, namely AQT and Subrip09, define the end of a
75 subtitle as the beginning of the following. Since currently we read one
76 subtitle at time, for these format we keep two global *subtitle,
77 previous_aqt_sub and previous_subrip09_sub, pointing to previous subtitle,
78 so we can change its end when we read current subtitle starting time.
79 When CONFIG_SORTSUB is defined, we use a single global unsigned long,
80 previous_sub_end, for both (and even future) formats, to store the end of
81 the previous sub: it is initialized to 0 in sub_read_file and eventually
82 modified by sub_read_aqt_line or sub_read_subrip09_line.
84 unsigned long previous_sub_end;
85 #endif
87 static int eol(char p) {
88 return p=='\r' || p=='\n' || p=='\0';
91 /* Remove leading and trailing space */
92 static void trail_space(char *s) {
93 int i = 0;
94 while (isspace(s[i])) ++i;
95 if (i) strcpy(s, s + i);
96 i = strlen(s) - 1;
97 while (i > 0 && isspace(s[i])) s[i--] = '\0';
100 static char *stristr(const char *haystack, const char *needle) {
101 int len = 0;
102 const char *p = haystack;
104 if (!(haystack && needle)) return NULL;
106 len=strlen(needle);
107 while (*p != '\0') {
108 if (strncasecmp(p, needle, len) == 0) return (char*)p;
109 p++;
112 return NULL;
115 static void sami_add_line(subtitle *current, char *buffer, char **pos) {
116 char *p = *pos;
117 *p = 0;
118 trail_space(buffer);
119 if (*buffer && current->lines < SUB_MAX_TEXT)
120 current->text[current->lines++] = strdup(buffer);
121 *pos = buffer;
124 static subtitle *sub_read_line_sami(stream_t* st, subtitle *current,
125 struct readline_args *args)
127 int utf16 = args->utf16;
128 static char line[LINE_LEN+1];
129 static char *s = NULL, *slacktime_s;
130 char text[LINE_LEN+1], *p=NULL, *q;
131 int state;
133 current->lines = current->start = current->end = 0;
134 current->alignment = SUB_ALIGNMENT_BOTTOMCENTER;
135 state = 0;
137 /* read the first line */
138 if (!s)
139 if (!(s = stream_read_line(st, line, LINE_LEN, utf16))) return 0;
141 do {
142 switch (state) {
144 case 0: /* find "START=" or "Slacktime:" */
145 slacktime_s = stristr (s, "Slacktime:");
146 if (slacktime_s)
147 sub_slacktime = strtol (slacktime_s+10, NULL, 0) / 10;
149 s = stristr (s, "Start=");
150 if (s) {
151 current->start = strtol (s + 6, &s, 0) / 10;
152 /* eat '>' */
153 for (; *s != '>' && *s != '\0'; s++);
154 s++;
155 state = 1; continue;
157 break;
159 case 1: /* find (optional) "<P", skip other TAGs */
160 for (; *s == ' ' || *s == '\t'; s++); /* strip blanks, if any */
161 if (*s == '\0') break;
162 if (*s != '<') { state = 3; p = text; continue; } /* not a TAG */
163 s++;
164 if (*s == 'P' || *s == 'p') { s++; state = 2; continue; } /* found '<P' */
165 for (; *s != '>' && *s != '\0'; s++); /* skip remains of non-<P> TAG */
166 if (s == '\0')
167 break;
168 s++;
169 continue;
171 case 2: /* find ">" */
172 if ((s = strchr (s, '>'))) { s++; state = 3; p = text; continue; }
173 break;
175 case 3: /* get all text until '<' appears */
176 if (p - text >= LINE_LEN)
177 sami_add_line(current, text, &p);
178 if (*s == '\0') break;
179 else if (!strncasecmp (s, "<br>", 4)) {
180 sami_add_line(current, text, &p);
181 s += 4;
183 else if ((*s == '{') && !sub_no_text_pp) { state = 5; ++s; continue; }
184 else if (*s == '<') { state = 4; }
185 else if (!strncasecmp (s, "&nbsp;", 6)) { *p++ = ' '; s += 6; }
186 else if (*s == '\t') { *p++ = ' '; s++; }
187 else if (*s == '\r' || *s == '\n') { s++; }
188 else *p++ = *s++;
190 /* skip duplicated space */
191 if (p > text + 2) if (*(p-1) == ' ' && *(p-2) == ' ') p--;
193 continue;
195 case 4: /* get current->end or skip <TAG> */
196 q = stristr (s, "Start=");
197 if (q) {
198 current->end = strtol (q + 6, &q, 0) / 10 - 1;
199 *p = '\0'; trail_space (text);
200 if (text[0] != '\0')
201 current->text[current->lines++] = strdup (text);
202 if (current->lines > 0) { state = 99; break; }
203 state = 0; continue;
205 s = strchr (s, '>');
206 if (s) { s++; state = 3; continue; }
207 break;
208 case 5: /* get rid of {...} text, but read the alignment code */
209 if ((*s == '\\') && (*(s + 1) == 'a') && !sub_no_text_pp) {
210 if (stristr(s, "\\a1") != NULL) {
211 current->alignment = SUB_ALIGNMENT_BOTTOMLEFT;
212 s = s + 3;
214 if (stristr(s, "\\a2") != NULL) {
215 current->alignment = SUB_ALIGNMENT_BOTTOMCENTER;
216 s = s + 3;
217 } else if (stristr(s, "\\a3") != NULL) {
218 current->alignment = SUB_ALIGNMENT_BOTTOMRIGHT;
219 s = s + 3;
220 } else if ((stristr(s, "\\a4") != NULL) || (stristr(s, "\\a5") != NULL) || (stristr(s, "\\a8") != NULL)) {
221 current->alignment = SUB_ALIGNMENT_TOPLEFT;
222 s = s + 3;
223 } else if (stristr(s, "\\a6") != NULL) {
224 current->alignment = SUB_ALIGNMENT_TOPCENTER;
225 s = s + 3;
226 } else if (stristr(s, "\\a7") != NULL) {
227 current->alignment = SUB_ALIGNMENT_TOPRIGHT;
228 s = s + 3;
229 } else if (stristr(s, "\\a9") != NULL) {
230 current->alignment = SUB_ALIGNMENT_MIDDLELEFT;
231 s = s + 3;
232 } else if (stristr(s, "\\a10") != NULL) {
233 current->alignment = SUB_ALIGNMENT_MIDDLECENTER;
234 s = s + 4;
235 } else if (stristr(s, "\\a11") != NULL) {
236 current->alignment = SUB_ALIGNMENT_MIDDLERIGHT;
237 s = s + 4;
240 if (*s == '}') state = 3;
241 ++s;
242 continue;
245 /* read next line */
246 if (state != 99 && !(s = stream_read_line (st, line, LINE_LEN, utf16))) {
247 if (current->start > 0) {
248 break; // if it is the last subtitle
249 } else {
250 return 0;
254 } while (state != 99);
256 // For the last subtitle
257 if (current->end <= 0) {
258 current->end = current->start + sub_slacktime;
259 sami_add_line(current, text, &p);
262 return current;
266 static char *sub_readtext(char *source, char **dest) {
267 int len=0;
268 char *p=source;
270 // printf("src=%p dest=%p \n",source,dest);
272 while ( !eol(*p) && *p!= '|' ) {
273 p++,len++;
276 *dest= malloc (len+1);
277 if (!dest) {return ERR;}
279 strncpy(*dest, source, len);
280 (*dest)[len]=0;
282 while (*p=='\r' || *p=='\n' || *p=='|') p++;
284 if (*p) return p; // not-last text field
285 else return NULL; // last text field
288 static subtitle *sub_read_line_microdvd(stream_t *st,subtitle *current,
289 struct readline_args *args)
291 int utf16 = args->utf16;
292 char line[LINE_LEN+1];
293 char line2[LINE_LEN+1];
294 char *p, *next;
295 int i;
297 do {
298 if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
299 } while ((sscanf (line,
300 "{%ld}{}%[^\r\n]",
301 &(current->start), line2) < 2) &&
302 (sscanf (line,
303 "{%ld}{%ld}%[^\r\n]",
304 &(current->start), &(current->end), line2) < 3));
306 if (args->opts->ass_enabled) {
307 subassconvert_microdvd(line2, line, LINE_LEN + 1);
308 p = line;
309 } else
310 p = line2;
312 next=p, i=0;
313 while ((next =sub_readtext (next, &(current->text[i])))) {
314 if (current->text[i]==ERR) {return ERR;}
315 i++;
316 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
318 current->lines= ++i;
320 return current;
323 static subtitle *sub_read_line_mpl2(stream_t *st,subtitle *current,
324 struct readline_args *args)
326 int utf16 = args->utf16;
327 char line[LINE_LEN+1];
328 char line2[LINE_LEN+1];
329 char *p, *next;
330 int i;
332 do {
333 if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
334 } while ((sscanf (line,
335 "[%ld][%ld]%[^\r\n]",
336 &(current->start), &(current->end), line2) < 3));
337 current->start *= 10;
338 current->end *= 10;
339 p=line2;
341 next=p, i=0;
342 while ((next =sub_readtext (next, &(current->text[i])))) {
343 if (current->text[i]==ERR) {return ERR;}
344 i++;
345 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
347 current->lines= ++i;
349 return current;
352 static subtitle *sub_read_line_subrip(stream_t* st, subtitle *current,
353 struct readline_args *args)
355 int utf16 = args->utf16;
356 char line[LINE_LEN+1];
357 int a1,a2,a3,a4,b1,b2,b3,b4;
358 char *p=NULL, *q=NULL;
359 int len;
361 while (1) {
362 if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
363 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
364 current->start = a1*360000+a2*6000+a3*100+a4;
365 current->end = b1*360000+b2*6000+b3*100+b4;
367 if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
369 p=q=line;
370 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) {
371 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && *p!='|' && strncmp(p,"[br]",4); p++,len++);
372 current->text[current->lines-1]=malloc (len+1);
373 if (!current->text[current->lines-1]) return ERR;
374 strncpy (current->text[current->lines-1], q, len);
375 current->text[current->lines-1][len]='\0';
376 if (!*p || *p=='\r' || *p=='\n') break;
377 if (*p=='|') p++;
378 else while (*p++!=']');
380 break;
382 return current;
385 static subtitle *sub_ass_read_line_subviewer(stream_t *st, subtitle *current,
386 struct readline_args *args)
388 int utf16 = args->utf16;
389 int a1, a2, a3, a4, b1, b2, b3, b4, j = 0;
391 while (!current->text[0]) {
392 char line[LINE_LEN + 1], full_line[LINE_LEN + 1], sep;
393 int i;
395 /* Parse SubRip header */
396 if (!stream_read_line(st, line, LINE_LEN, utf16))
397 return NULL;
398 if (sscanf(line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d",
399 &a1, &a2, &a3, &sep, &a4, &b1, &b2, &b3, &sep, &b4) < 10)
400 continue;
402 current->start = a1 * 360000 + a2 * 6000 + a3 * 100 + a4 / 10;
403 current->end = b1 * 360000 + b2 * 6000 + b3 * 100 + b4 / 10;
405 /* Concat lines */
406 full_line[0] = 0;
407 for (i = 0; i < SUB_MAX_TEXT; i++) {
408 int blank = 1, len = 0;
409 char *p;
411 if (!stream_read_line(st, line, LINE_LEN, utf16))
412 break;
414 for (p = line; *p != '\n' && *p != '\r' && *p; p++, len++)
415 if (*p != ' ' && *p != '\t')
416 blank = 0;
418 if (blank)
419 break;
421 *p = 0;
423 if (!(j + 1 + len < sizeof(full_line) - 1))
424 break;
426 if (j != 0)
427 full_line[j++] = '\n';
428 strcpy(&full_line[j], line);
429 j += len;
432 /* Use the ASS/SSA converter to transform the whole lines */
433 if (full_line[0]) {
434 char converted_line[LINE_LEN + 1];
435 subassconvert_subrip(full_line, converted_line, LINE_LEN + 1);
436 current->text[0] = strdup(converted_line);
437 current->lines = 1;
440 return current;
443 static subtitle *sub_read_line_subviewer(stream_t *st,subtitle *current,
444 struct readline_args *args)
446 int utf16 = args->utf16;
447 char line[LINE_LEN+1];
448 int a1,a2,a3,a4,b1,b2,b3,b4;
449 char *p=NULL;
450 int i,len;
452 if (args->opts->ass_enabled)
453 return sub_ass_read_line_subviewer(st, current, args);
454 while (!current->text[0]) {
455 if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
456 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)
457 continue;
458 current->start = a1*360000+a2*6000+a3*100+a4/10;
459 current->end = b1*360000+b2*6000+b3*100+b4/10;
460 for (i=0; i<SUB_MAX_TEXT;) {
461 int blank = 1;
462 if (!stream_read_line (st, line, LINE_LEN, utf16)) break;
463 len=0;
464 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++)
465 if (*p != ' ' && *p != '\t')
466 blank = 0;
467 if (len && !blank) {
468 int j=0,skip=0;
469 char *curptr=current->text[i]=malloc (len+1);
470 if (!current->text[i]) return ERR;
471 //strncpy (current->text[i], line, len); current->text[i][len]='\0';
472 for(; j<len; j++) {
473 /* let's filter html tags ::atmos */
474 if(line[j]=='>') {
475 skip=0;
476 continue;
478 if(line[j]=='<') {
479 skip=1;
480 continue;
482 if(skip) {
483 continue;
485 *curptr=line[j];
486 curptr++;
488 *curptr='\0';
490 i++;
491 } else {
492 break;
495 current->lines=i;
497 return current;
500 static subtitle *sub_read_line_subviewer2(stream_t *st,subtitle *current,
501 struct readline_args *args)
503 int utf16 = args->utf16;
504 char line[LINE_LEN+1];
505 int a1,a2,a3,a4;
506 char *p=NULL;
507 int i,len;
509 while (!current->text[0]) {
510 if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
511 if (line[0]!='{')
512 continue;
513 if ((len=sscanf (line, "{T %d:%d:%d:%d",&a1,&a2,&a3,&a4)) < 4)
514 continue;
515 current->start = a1*360000+a2*6000+a3*100+a4/10;
516 for (i=0; i<SUB_MAX_TEXT;) {
517 if (!stream_read_line (st, line, LINE_LEN, utf16)) break;
518 if (line[0]=='}') break;
519 len=0;
520 for (p=line; *p!='\n' && *p!='\r' && *p; ++p,++len);
521 if (len) {
522 current->text[i]=malloc (len+1);
523 if (!current->text[i]) return ERR;
524 strncpy (current->text[i], line, len); current->text[i][len]='\0';
525 ++i;
526 } else {
527 break;
530 current->lines=i;
532 return current;
536 static subtitle *sub_read_line_vplayer(stream_t *st,subtitle *current,
537 struct readline_args *args)
539 int utf16 = args->utf16;
540 char line[LINE_LEN+1];
541 int a1,a2,a3;
542 char *p=NULL, *next,separator;
543 int i,len,plen;
545 while (!current->text[0]) {
546 if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
547 if ((len=sscanf (line, "%d:%d:%d%c%n",&a1,&a2,&a3,&separator,&plen)) < 4)
548 continue;
550 if (!(current->start = a1*360000+a2*6000+a3*100))
551 continue;
552 /* removed by wodzu
553 p=line;
554 // finds the body of the subtitle
555 for (i=0; i<3; i++){
556 p=strchr(p,':');
557 if (p==NULL) break;
558 ++p;
560 if (p==NULL) {
561 printf("SUB: Skipping incorrect subtitle line!\n");
562 continue;
565 // by wodzu: hey! this time we know what length it has! what is
566 // that magic for? it can't deal with space instead of third
567 // colon! look, what simple it can be:
568 p = &line[ plen ];
570 i=0;
571 if (*p!='|') {
573 next = p,i=0;
574 while ((next =sub_readtext (next, &(current->text[i])))) {
575 if (current->text[i]==ERR) {return ERR;}
576 i++;
577 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
579 current->lines=i+1;
582 return current;
585 static subtitle *sub_read_line_rt(stream_t *st,subtitle *current,
586 struct readline_args *args)
588 int utf16 = args->utf16;
590 //TODO: This format uses quite rich (sub/super)set of xhtml
591 // I couldn't check it since DTD is not included.
592 // WARNING: full XML parses can be required for proper parsing
593 char line[LINE_LEN+1];
594 int a1,a2,a3,a4,b1,b2,b3,b4;
595 char *p=NULL,*next=NULL;
596 int i,len,plen;
598 while (!current->text[0]) {
599 if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
600 //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0
601 //to describe the same moment in time. Maybe there are even more formats in use.
602 //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
603 plen=a1=a2=a3=a4=b1=b2=b3=b4=0;
604 if (
605 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b3,&b4,&plen)) < 4) &&
606 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b2,&b3,&b4,&plen)) < 5) &&
607 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) &&
608 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) &&
609 // ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) &&
610 ((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) &&
611 ((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) &&
612 //now try it without end time
613 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&plen)) < 2) &&
614 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&plen)) < 2) &&
615 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&plen)) < 3) &&
616 ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&plen)) < 4)
618 continue;
619 current->start = a1*360000+a2*6000+a3*100+a4/10;
620 current->end = b1*360000+b2*6000+b3*100+b4/10;
621 if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)
622 current->end = current->start+200;
623 p=line; p+=plen;i=0;
624 // TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
625 next = strstr(line,"<clear/>");
626 if(next && strlen(next)>8){
627 next+=8;i=0;
628 while ((next =sub_readtext (next, &(current->text[i])))) {
629 if (current->text[i]==ERR) {return ERR;}
630 i++;
631 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
634 current->lines=i+1;
636 return current;
639 static subtitle *sub_read_line_ssa(stream_t *st,subtitle *current,
640 struct readline_args *args)
643 * Sub Station Alpha v4 (and v2?) scripts have 9 commas before subtitle
644 * other Sub Station Alpha scripts have only 8 commas before subtitle
645 * Reading the "ScriptType:" field is not reliable since many scripts appear
646 * w/o it
648 * http://www.scriptclub.org is a good place to find more examples
649 * http://www.eswat.demon.co.uk is where the SSA specs can be found
651 int utf16 = args->utf16;
652 int comma;
653 static int max_comma = 32; /* let's use 32 for the case that the */
654 /* amount of commas increase with newer SSA versions */
656 int hour1, min1, sec1, hunsec1,
657 hour2, min2, sec2, hunsec2, nothing;
658 int num;
660 char line[LINE_LEN+1],
661 line3[LINE_LEN+1],
662 *line2;
663 char *tmp;
665 do {
666 if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
667 } while (sscanf (line, "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d"
668 "%[^\n\r]", &nothing,
669 &hour1, &min1, &sec1, &hunsec1,
670 &hour2, &min2, &sec2, &hunsec2,
671 line3) < 9
673 sscanf (line, "Dialogue: %d,%d:%d:%d.%d,%d:%d:%d.%d"
674 "%[^\n\r]", &nothing,
675 &hour1, &min1, &sec1, &hunsec1,
676 &hour2, &min2, &sec2, &hunsec2,
677 line3) < 9 );
679 line2=strchr(line3, ',');
680 if (!line2) return NULL;
682 for (comma = 4; comma < max_comma; comma ++)
684 tmp = line2;
685 if(!(tmp=strchr(++tmp, ','))) break;
686 if(*(++tmp) == ' ') break;
687 /* a space after a comma means we're already in a sentence */
688 line2 = tmp;
691 if(comma < max_comma)max_comma = comma;
692 /* eliminate the trailing comma */
693 if(*line2 == ',') line2++;
695 current->lines=0;num=0;
696 current->start = 360000*hour1 + 6000*min1 + 100*sec1 + hunsec1;
697 current->end = 360000*hour2 + 6000*min2 + 100*sec2 + hunsec2;
699 while (((tmp=strstr(line2, "\\n")) != NULL) || ((tmp=strstr(line2, "\\N")) != NULL) ){
700 current->text[num]=malloc(tmp-line2+1);
701 strncpy (current->text[num], line2, tmp-line2);
702 current->text[num][tmp-line2]='\0';
703 line2=tmp+2;
704 num++;
705 current->lines++;
706 if (current->lines >= SUB_MAX_TEXT) return current;
709 current->text[num]=strdup(line2);
710 current->lines++;
712 return current;
715 static void sub_pp_ssa(subtitle *sub) {
716 int l=sub->lines;
717 char *so,*de,*start;
719 while (l){
720 /* eliminate any text enclosed with {}, they are font and color settings */
721 so=de=sub->text[--l];
722 while (*so) {
723 if(*so == '{' && so[1]=='\\') {
724 for (start=so; *so && *so!='}'; so++);
725 if(*so) so++; else so=start;
727 if(*so) {
728 *de=*so;
729 so++; de++;
732 *de=*so;
737 * PJS subtitles reader.
738 * That's the "Phoenix Japanimation Society" format.
739 * I found some of them in http://www.scriptsclub.org/ (used for anime).
740 * The time is in tenths of second.
742 * by set, based on code by szabi (dunnowhat sub format ;-)
744 static subtitle *sub_read_line_pjs(stream_t *st,subtitle *current,
745 struct readline_args *args)
747 int utf16 = args->utf16;
748 char line[LINE_LEN+1];
749 char text[LINE_LEN+1], *s, *d;
751 if (!stream_read_line (st, line, LINE_LEN, utf16))
752 return NULL;
753 /* skip spaces */
754 for (s=line; *s && isspace(*s); s++);
755 /* allow empty lines at the end of the file */
756 if (*s==0)
757 return NULL;
758 /* get the time */
759 if (sscanf (s, "%ld,%ld,", &(current->start),
760 &(current->end)) <2) {
761 return ERR;
763 /* the files I have are in tenths of second */
764 current->start *= 10;
765 current->end *= 10;
766 /* walk to the beggining of the string */
767 for (; *s; s++) if (*s==',') break;
768 if (*s) {
769 for (s++; *s; s++) if (*s==',') break;
770 if (*s) s++;
772 if (*s!='"') {
773 return ERR;
775 /* copy the string to the text buffer */
776 for (s++, d=text; *s && *s!='"'; s++, d++)
777 *d=*s;
778 *d=0;
779 current->text[0] = strdup(text);
780 current->lines = 1;
782 return current;
785 static subtitle *sub_read_line_mpsub(stream_t *st, subtitle *current,
786 struct readline_args *args)
788 int utf16 = args->utf16;
789 char line[LINE_LEN+1];
790 float a,b;
791 int num=0;
792 char *p, *q;
796 if (!stream_read_line(st, line, LINE_LEN, utf16)) return NULL;
797 } while (sscanf (line, "%f %f", &a, &b) !=2);
799 mpsub_position += a*mpsub_multiplier;
800 current->start=(int) mpsub_position;
801 mpsub_position += b*mpsub_multiplier;
802 current->end=(int) mpsub_position;
804 while (num < SUB_MAX_TEXT) {
805 if (!stream_read_line (st, line, LINE_LEN, utf16)) {
806 if (num == 0) return NULL;
807 else return current;
809 p=line;
810 while (isspace(*p)) p++;
811 if (eol(*p) && num > 0) return current;
812 if (eol(*p)) return NULL;
814 for (q=p; !eol(*q); q++);
815 *q='\0';
816 if (strlen(p)) {
817 current->text[num]=strdup(p);
818 // printf (">%s<\n",p);
819 current->lines = ++num;
820 } else {
821 if (num) return current;
822 else return NULL;
825 return NULL; // we should have returned before if it's OK
828 #ifndef CONFIG_SORTSUB
829 //we don't need this if we use previous_sub_end
830 subtitle *previous_aqt_sub = NULL;
831 #endif
833 static subtitle *sub_read_line_aqt(stream_t *st,subtitle *current,
834 struct readline_args *args)
836 int utf16 = args->utf16;
837 char line[LINE_LEN+1];
838 char *next;
839 int i;
841 while (1) {
842 // try to locate next subtitle
843 if (!stream_read_line (st, line, LINE_LEN, utf16))
844 return NULL;
845 if (!(sscanf (line, "-->> %ld", &(current->start)) <1))
846 break;
849 #ifdef CONFIG_SORTSUB
850 previous_sub_end = (current->start) ? current->start - 1 : 0;
851 #else
852 if (previous_aqt_sub != NULL)
853 previous_aqt_sub->end = current->start-1;
855 previous_aqt_sub = current;
856 #endif
858 if (!stream_read_line (st, line, LINE_LEN, utf16))
859 return NULL;
861 sub_readtext((char *) &line,&current->text[0]);
862 current->lines = 1;
863 current->end = current->start; // will be corrected by next subtitle
865 if (!stream_read_line (st, line, LINE_LEN, utf16))
866 return current;
868 next = line,i=1;
869 while ((next =sub_readtext (next, &(current->text[i])))) {
870 if (current->text[i]==ERR) {return ERR;}
871 i++;
872 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
874 current->lines=i+1;
876 if (!strlen(current->text[0]) && !strlen(current->text[1])) {
877 #ifdef CONFIG_SORTSUB
878 previous_sub_end = 0;
879 #else
880 // void subtitle -> end of previous marked and exit
881 previous_aqt_sub = NULL;
882 #endif
883 return NULL;
886 return current;
889 #ifndef CONFIG_SORTSUB
890 subtitle *previous_subrip09_sub = NULL;
891 #endif
893 static subtitle *sub_read_line_subrip09(stream_t *st,subtitle *current,
894 struct readline_args *args)
896 int utf16 = args->utf16;
897 char line[LINE_LEN+1];
898 int a1,a2,a3;
899 char * next=NULL;
900 int i,len;
902 while (1) {
903 // try to locate next subtitle
904 if (!stream_read_line (st, line, LINE_LEN, utf16))
905 return NULL;
906 if (!((len=sscanf (line, "[%d:%d:%d]",&a1,&a2,&a3)) < 3))
907 break;
910 current->start = a1*360000+a2*6000+a3*100;
912 #ifdef CONFIG_SORTSUB
913 previous_sub_end = (current->start) ? current->start - 1 : 0;
914 #else
915 if (previous_subrip09_sub != NULL)
916 previous_subrip09_sub->end = current->start-1;
918 previous_subrip09_sub = current;
919 #endif
921 if (!stream_read_line (st, line, LINE_LEN, utf16))
922 return NULL;
924 next = line,i=0;
926 current->text[0]=""; // just to be sure that string is clear
928 while ((next =sub_readtext (next, &(current->text[i])))) {
929 if (current->text[i]==ERR) {return ERR;}
930 i++;
931 if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
933 current->lines=i+1;
935 if (!strlen(current->text[0]) && (i==0)) {
936 #ifdef CONFIG_SORTSUB
937 previous_sub_end = 0;
938 #else
939 // void subtitle -> end of previous marked and exit
940 previous_subrip09_sub = NULL;
941 #endif
942 return NULL;
945 return current;
948 static subtitle *sub_read_line_jacosub(stream_t* st, subtitle * current,
949 struct readline_args *args)
951 int utf16 = args->utf16;
952 char line1[LINE_LEN], line2[LINE_LEN], directive[LINE_LEN], *p, *q;
953 unsigned a1, a2, a3, a4, b1, b2, b3, b4, comment = 0;
954 static unsigned jacoTimeres = 30;
955 static int jacoShift = 0;
957 memset(current, 0, sizeof(subtitle));
958 memset(line1, 0, LINE_LEN);
959 memset(line2, 0, LINE_LEN);
960 memset(directive, 0, LINE_LEN);
961 while (!current->text[0]) {
962 if (!stream_read_line(st, line1, LINE_LEN, utf16)) {
963 return NULL;
965 if (sscanf
966 (line1, "%u:%u:%u.%u %u:%u:%u.%u %[^\n\r]", &a1, &a2, &a3, &a4,
967 &b1, &b2, &b3, &b4, line2) < 9) {
968 if (sscanf(line1, "@%u @%u %[^\n\r]", &a4, &b4, line2) < 3) {
969 if (line1[0] == '#') {
970 int hours = 0, minutes = 0, seconds, delta, inverter =
972 unsigned units = jacoShift;
973 switch (toupper(line1[1])) {
974 case 'S':
975 if (isalpha(line1[2])) {
976 delta = 6;
977 } else {
978 delta = 2;
980 if (sscanf(&line1[delta], "%d", &hours)) {
981 if (hours < 0) {
982 hours *= -1;
983 inverter = -1;
985 if (sscanf(&line1[delta], "%*d:%d", &minutes)) {
986 if (sscanf
987 (&line1[delta], "%*d:%*d:%d",
988 &seconds)) {
989 sscanf(&line1[delta], "%*d:%*d:%*d.%d",
990 &units);
991 } else {
992 hours = 0;
993 sscanf(&line1[delta], "%d:%d.%d",
994 &minutes, &seconds, &units);
995 minutes *= inverter;
997 } else {
998 hours = minutes = 0;
999 sscanf(&line1[delta], "%d.%d", &seconds,
1000 &units);
1001 seconds *= inverter;
1003 jacoShift =
1004 ((hours * 3600 + minutes * 60 +
1005 seconds) * jacoTimeres +
1006 units) * inverter;
1008 break;
1009 case 'T':
1010 if (isalpha(line1[2])) {
1011 delta = 8;
1012 } else {
1013 delta = 2;
1015 sscanf(&line1[delta], "%u", &jacoTimeres);
1016 break;
1019 continue;
1020 } else {
1021 current->start =
1022 (unsigned long) ((a4 + jacoShift) * 100.0 /
1023 jacoTimeres);
1024 current->end =
1025 (unsigned long) ((b4 + jacoShift) * 100.0 /
1026 jacoTimeres);
1028 } else {
1029 current->start =
1030 (unsigned
1031 long) (((a1 * 3600 + a2 * 60 + a3) * jacoTimeres + a4 +
1032 jacoShift) * 100.0 / jacoTimeres);
1033 current->end =
1034 (unsigned
1035 long) (((b1 * 3600 + b2 * 60 + b3) * jacoTimeres + b4 +
1036 jacoShift) * 100.0 / jacoTimeres);
1038 current->lines = 0;
1039 p = line2;
1040 while ((*p == ' ') || (*p == '\t')) {
1041 ++p;
1043 if (isalpha(*p)||*p == '[') {
1044 int cont, jLength;
1046 if (sscanf(p, "%s %[^\n\r]", directive, line1) < 2)
1047 return (subtitle *) ERR;
1048 jLength = strlen(directive);
1049 for (cont = 0; cont < jLength; ++cont) {
1050 if (isalpha(*(directive + cont)))
1051 *(directive + cont) = toupper(*(directive + cont));
1053 if ((strstr(directive, "RDB") != NULL)
1054 || (strstr(directive, "RDC") != NULL)
1055 || (strstr(directive, "RLB") != NULL)
1056 || (strstr(directive, "RLG") != NULL)) {
1057 continue;
1059 if (strstr(directive, "JL") != NULL) {
1060 current->alignment = SUB_ALIGNMENT_BOTTOMLEFT;
1061 } else if (strstr(directive, "JR") != NULL) {
1062 current->alignment = SUB_ALIGNMENT_BOTTOMRIGHT;
1063 } else {
1064 current->alignment = SUB_ALIGNMENT_BOTTOMCENTER;
1066 strcpy(line2, line1);
1067 p = line2;
1069 for (q = line1; (!eol(*p)) && (current->lines < SUB_MAX_TEXT); ++p) {
1070 switch (*p) {
1071 case '{':
1072 comment++;
1073 break;
1074 case '}':
1075 if (comment) {
1076 --comment;
1077 //the next line to get rid of a blank after the comment
1078 if ((*(p + 1)) == ' ')
1079 p++;
1081 break;
1082 case '~':
1083 if (!comment) {
1084 *q = ' ';
1085 ++q;
1087 break;
1088 case ' ':
1089 case '\t':
1090 if ((*(p + 1) == ' ') || (*(p + 1) == '\t'))
1091 break;
1092 if (!comment) {
1093 *q = ' ';
1094 ++q;
1096 break;
1097 case '\\':
1098 if (*(p + 1) == 'n') {
1099 *q = '\0';
1100 q = line1;
1101 current->text[current->lines++] = strdup(line1);
1102 ++p;
1103 break;
1105 if ((toupper(*(p + 1)) == 'C')
1106 || (toupper(*(p + 1)) == 'F')) {
1107 ++p,++p;
1108 break;
1110 if ((*(p + 1) == 'B') || (*(p + 1) == 'b') || (*(p + 1) == 'D') || //actually this means "insert current date here"
1111 (*(p + 1) == 'I') || (*(p + 1) == 'i') || (*(p + 1) == 'N') || (*(p + 1) == 'T') || //actually this means "insert current time here"
1112 (*(p + 1) == 'U') || (*(p + 1) == 'u')) {
1113 ++p;
1114 break;
1116 if ((*(p + 1) == '\\') ||
1117 (*(p + 1) == '~') || (*(p + 1) == '{')) {
1118 ++p;
1119 } else if (eol(*(p + 1))) {
1120 if (!stream_read_line(st, directive, LINE_LEN, utf16))
1121 return NULL;
1122 trail_space(directive);
1123 av_strlcat(line2, directive, LINE_LEN);
1124 break;
1126 default:
1127 if (!comment) {
1128 *q = *p;
1129 ++q;
1131 } //-- switch
1132 } //-- for
1133 *q = '\0';
1134 current->text[current->lines] = strdup(line1);
1135 } //-- while
1136 current->lines++;
1137 return current;
1140 static int sub_autodetect (stream_t* st, int *uses_time, int utf16) {
1141 char line[LINE_LEN+1];
1142 int i,j=0;
1144 while (j < 100) {
1145 j++;
1146 if (!stream_read_line (st, line, LINE_LEN, utf16))
1147 return SUB_INVALID;
1149 if (sscanf (line, "{%d}{%d}", &i, &i)==2)
1150 {*uses_time=0;return SUB_MICRODVD;}
1151 if (sscanf (line, "{%d}{}", &i)==1)
1152 {*uses_time=0;return SUB_MICRODVD;}
1153 if (sscanf (line, "[%d][%d]", &i, &i)==2)
1154 {*uses_time=1;return SUB_MPL2;}
1155 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
1156 {*uses_time=1;return SUB_SUBRIP;}
1157 if (sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d", &i, &i, &i, (char *)&i, &i, &i, &i, &i, (char *)&i, &i)==10)
1158 {*uses_time=1;return SUB_SUBVIEWER;}
1159 if (sscanf (line, "{T %d:%d:%d:%d",&i, &i, &i, &i)==4)
1160 {*uses_time=1;return SUB_SUBVIEWER2;}
1161 if (strstr (line, "<SAMI>"))
1162 {*uses_time=1; return SUB_SAMI;}
1163 if (sscanf(line, "%d:%d:%d.%d %d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i) == 8)
1164 {*uses_time = 1; return SUB_JACOSUB;}
1165 if (sscanf(line, "@%d @%d", &i, &i) == 2)
1166 {*uses_time = 1; return SUB_JACOSUB;}
1167 if (sscanf (line, "%d:%d:%d:", &i, &i, &i )==3)
1168 {*uses_time=1;return SUB_VPLAYER;}
1169 if (sscanf (line, "%d:%d:%d ", &i, &i, &i )==3)
1170 {*uses_time=1;return SUB_VPLAYER;}
1171 if (!strncasecmp(line, "<window", 7))
1172 {*uses_time=1;return SUB_RT;}
1173 if (!memcmp(line, "Dialogue: Marked", 16))
1174 {*uses_time=1; return SUB_SSA;}
1175 if (!memcmp(line, "Dialogue: ", 10))
1176 {*uses_time=1; return SUB_SSA;}
1177 if (sscanf (line, "%d,%d,\"%c", &i, &i, (char *) &i) == 3)
1178 {*uses_time=1;return SUB_PJS;}
1179 if (sscanf (line, "FORMAT=%d", &i) == 1)
1180 {*uses_time=0; return SUB_MPSUB;}
1181 if (!memcmp(line, "FORMAT=TIME", 11))
1182 {*uses_time=1; return SUB_MPSUB;}
1183 if (strstr (line, "-->>"))
1184 {*uses_time=0; return SUB_AQTITLE;}
1185 if (sscanf (line, "[%d:%d:%d]", &i, &i, &i)==3)
1186 {*uses_time=1;return SUB_SUBRIP09;}
1189 return SUB_INVALID; // too many bad lines
1192 extern int sub_utf8;
1193 int sub_utf8_prev=0;
1195 extern float sub_delay;
1196 extern float sub_fps;
1198 #ifdef CONFIG_ICONV
1199 static iconv_t icdsc = (iconv_t)(-1);
1201 void subcp_open (stream_t *st)
1203 char *tocp = "UTF-8";
1205 if (sub_cp){
1206 const char *cp_tmp = sub_cp;
1207 #ifdef CONFIG_ENCA
1208 char enca_lang[3], enca_fallback[100];
1209 if (sscanf(sub_cp, "enca:%2s:%99s", enca_lang, enca_fallback) == 2
1210 || sscanf(sub_cp, "ENCA:%2s:%99s", enca_lang, enca_fallback) == 2) {
1211 if (st && st->flags & MP_STREAM_SEEK ) {
1212 cp_tmp = guess_cp(st, enca_lang, enca_fallback);
1213 } else {
1214 cp_tmp = enca_fallback;
1215 if (st)
1216 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: enca failed, stream must be seekable.\n");
1219 #endif
1220 if ((icdsc = iconv_open (tocp, cp_tmp)) != (iconv_t)(-1)){
1221 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: opened iconv descriptor.\n");
1222 sub_utf8 = 2;
1223 } else
1224 mp_msg(MSGT_SUBREADER,MSGL_ERR,"SUB: error opening iconv descriptor.\n");
1228 void subcp_close (void)
1230 if (icdsc != (iconv_t)(-1)){
1231 (void) iconv_close (icdsc);
1232 icdsc = (iconv_t)(-1);
1233 mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: closed iconv descriptor.\n");
1237 subtitle* subcp_recode (subtitle *sub)
1239 int l=sub->lines;
1240 size_t ileft, oleft;
1241 char *op, *ip, *ot;
1242 if(icdsc == (iconv_t)(-1)) return sub;
1244 while (l){
1245 ip = sub->text[--l];
1246 ileft = strlen(ip);
1247 oleft = 4 * ileft;
1249 if (!(ot = malloc(oleft + 1))){
1250 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error allocating mem.\n");
1251 continue;
1253 op = ot;
1254 if (iconv(icdsc, &ip, &ileft,
1255 &op, &oleft) == (size_t)(-1)) {
1256 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error recoding line.\n");
1257 free(ot);
1258 continue;
1260 // In some stateful encodings, we must clear the state to handle the last character
1261 if (iconv(icdsc, NULL, NULL,
1262 &op, &oleft) == (size_t)(-1)) {
1263 mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: error recoding line, can't clear encoding state.\n");
1265 *op='\0' ;
1266 free (sub->text[l]);
1267 sub->text[l] = ot;
1269 return sub;
1271 #endif
1273 static void adjust_subs_time(subtitle* sub, float subtime, float fps, int block,
1274 int sub_num, int sub_uses_time) {
1275 int n,m;
1276 subtitle* nextsub;
1277 int i = sub_num;
1278 unsigned long subfms = (sub_uses_time ? 100 : fps) * subtime;
1279 unsigned long overlap = (sub_uses_time ? 100 : fps) / 5; // 0.2s
1281 n=m=0;
1282 if (i) for (;;){
1283 if (sub->end <= sub->start){
1284 sub->end = sub->start + subfms;
1285 m++;
1286 n++;
1288 if (!--i) break;
1289 nextsub = sub + 1;
1290 if(block){
1291 if ((sub->end > nextsub->start) && (sub->end <= nextsub->start + overlap)) {
1292 // these subtitles overlap for less than 0.2 seconds
1293 // and would result in very short overlapping subtitle
1294 // so let's fix the problem here, before overlapping code
1295 // get its hands on them
1296 unsigned delta = sub->end - nextsub->start, half = delta / 2;
1297 sub->end -= half + 1;
1298 nextsub->start += delta - half;
1300 if (sub->end >= nextsub->start){
1301 sub->end = nextsub->start - 1;
1302 if (sub->end - sub->start > subfms)
1303 sub->end = sub->start + subfms;
1304 if (!m)
1305 n++;
1309 /* Theory:
1310 * Movies are often converted from FILM (24 fps)
1311 * to PAL (25) by simply speeding it up, so we
1312 * to multiply the original timestmaps by
1313 * (Movie's FPS / Subtitle's (guessed) FPS)
1314 * so eg. for 23.98 fps movie and PAL time based
1315 * subtitles we say -subfps 25 and we're fine!
1318 /* timed sub fps correction ::atmos */
1319 /* the frame-based case is handled in mpcommon.c
1320 * where find_sub is called */
1321 if(sub_uses_time && sub_fps) {
1322 sub->start *= sub_fps/fps;
1323 sub->end *= sub_fps/fps;
1326 sub = nextsub;
1327 m = 0;
1329 if (n) mp_msg(MSGT_SUBREADER,MSGL_V,"SUB: Adjusted %d subtitle(s).\n", n);
1332 struct subreader {
1333 subtitle * (*read)(stream_t *st, subtitle *dest,
1334 struct readline_args *args);
1335 void (*post)(subtitle *dest);
1336 const char *name;
1339 #ifdef CONFIG_ENCA
1340 const char* guess_buffer_cp(unsigned char* buffer, int buflen, const char *preferred_language, const char *fallback)
1342 const char **languages;
1343 size_t langcnt;
1344 EncaAnalyser analyser;
1345 EncaEncoding encoding;
1346 const char *detected_sub_cp = NULL;
1347 int i;
1349 languages = enca_get_languages(&langcnt);
1350 mp_msg(MSGT_SUBREADER, MSGL_V, "ENCA supported languages: ");
1351 for (i = 0; i < langcnt; i++) {
1352 mp_msg(MSGT_SUBREADER, MSGL_V, "%s ", languages[i]);
1354 mp_msg(MSGT_SUBREADER, MSGL_V, "\n");
1356 for (i = 0; i < langcnt; i++) {
1357 if (strcasecmp(languages[i], preferred_language) != 0) continue;
1358 analyser = enca_analyser_alloc(languages[i]);
1359 encoding = enca_analyse_const(analyser, buffer, buflen);
1360 enca_analyser_free(analyser);
1361 if (encoding.charset != ENCA_CS_UNKNOWN) {
1362 detected_sub_cp = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV);
1363 break;
1367 free(languages);
1369 if (!detected_sub_cp) {
1370 detected_sub_cp = fallback;
1371 mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detection failed: fallback to %s\n", fallback);
1372 }else{
1373 mp_msg(MSGT_SUBREADER, MSGL_INFO, "ENCA detected charset: %s\n", detected_sub_cp);
1376 return detected_sub_cp;
1379 #define MAX_GUESS_BUFFER_SIZE (256*1024)
1380 const char* guess_cp(stream_t *st, const char *preferred_language, const char *fallback)
1382 size_t buflen;
1383 unsigned char *buffer;
1384 const char *detected_sub_cp = NULL;
1386 buffer = malloc(MAX_GUESS_BUFFER_SIZE);
1387 buflen = stream_read(st,buffer, MAX_GUESS_BUFFER_SIZE);
1389 detected_sub_cp = guess_buffer_cp(buffer, buflen, preferred_language, fallback);
1391 free(buffer);
1392 stream_reset(st);
1393 stream_seek(st,0);
1395 return detected_sub_cp;
1397 #undef MAX_GUESS_BUFFER_SIZE
1398 #endif
1400 sub_data* sub_read_file(char *filename, float fps, struct MPOpts *opts)
1402 int utf16;
1403 stream_t* fd;
1404 int n_max, n_first, i, j, sub_first, sub_orig;
1405 subtitle *first, *second, *sub, *return_sub, *alloced_sub = NULL;
1406 sub_data *subt_data;
1407 int uses_time = 0, sub_num = 0, sub_errs = 0;
1408 static const struct subreader sr[]=
1410 { sub_read_line_microdvd, NULL, "microdvd" },
1411 { sub_read_line_subrip, NULL, "subrip" },
1412 { sub_read_line_subviewer, NULL, "subviewer" },
1413 { sub_read_line_sami, NULL, "sami" },
1414 { sub_read_line_vplayer, NULL, "vplayer" },
1415 { sub_read_line_rt, NULL, "rt" },
1416 { sub_read_line_ssa, sub_pp_ssa, "ssa" },
1417 { sub_read_line_pjs, NULL, "pjs" },
1418 { sub_read_line_mpsub, NULL, "mpsub" },
1419 { sub_read_line_aqt, NULL, "aqt" },
1420 { sub_read_line_subviewer2, NULL, "subviewer 2.0" },
1421 { sub_read_line_subrip09, NULL, "subrip 0.9" },
1422 { sub_read_line_jacosub, NULL, "jacosub" },
1423 { sub_read_line_mpl2, NULL, "mpl2" }
1425 const struct subreader *srp;
1427 if(filename==NULL) return NULL; //qnx segfault
1428 fd=open_stream (filename, NULL, NULL); if (!fd) return NULL;
1430 sub_format = SUB_INVALID;
1431 for (utf16 = 0; sub_format == SUB_INVALID && utf16 < 3; utf16++) {
1432 sub_format=sub_autodetect (fd, &uses_time, utf16);
1433 stream_reset(fd);
1434 stream_seek(fd,0);
1436 utf16--;
1438 mpsub_multiplier = (uses_time ? 100.0 : 1.0);
1439 if (sub_format==SUB_INVALID) {mp_msg(MSGT_SUBREADER,MSGL_WARN,"SUB: Could not determine file format\n");return NULL;}
1440 srp=sr+sub_format;
1441 mp_msg(MSGT_SUBREADER, MSGL_V, "SUB: Detected subtitle file format: %s\n", srp->name);
1443 #ifdef CONFIG_ICONV
1444 sub_utf8_prev=sub_utf8;
1446 int l,k;
1447 k = -1;
1448 if ((l=strlen(filename))>4){
1449 char *exts[] = {".utf", ".utf8", ".utf-8" };
1450 for (k=3;--k>=0;)
1451 if (l >= strlen(exts[k]) && !strcasecmp(filename+(l - strlen(exts[k])), exts[k])){
1452 sub_utf8 = 1;
1453 break;
1456 if (k<0) subcp_open(fd);
1458 #endif
1460 sub_num=0;n_max=32;
1461 first=malloc(n_max*sizeof(subtitle));
1462 if(!first){
1463 #ifdef CONFIG_ICONV
1464 subcp_close();
1465 sub_utf8=sub_utf8_prev;
1466 #endif
1467 return NULL;
1470 #ifdef CONFIG_SORTSUB
1471 alloced_sub =
1472 sub = malloc(sizeof(subtitle));
1473 //This is to deal with those formats (AQT & Subrip) which define the end of a subtitle
1474 //as the beginning of the following
1475 previous_sub_end = 0;
1476 #endif
1477 while(1){
1478 if(sub_num>=n_max){
1479 n_max+=16;
1480 first=realloc(first,n_max*sizeof(subtitle));
1482 #ifndef CONFIG_SORTSUB
1483 sub = &first[sub_num];
1484 #endif
1485 memset(sub, '\0', sizeof(subtitle));
1486 sub=srp->read(fd, sub, &(struct readline_args){utf16, opts});
1487 if(!sub) break; // EOF
1488 #ifdef CONFIG_ICONV
1489 if ((sub!=ERR) && sub_utf8 == 2) sub=subcp_recode(sub);
1490 #endif
1491 if ( sub == ERR )
1493 #ifdef CONFIG_ICONV
1494 subcp_close();
1495 #endif
1496 free(first);
1497 free(alloced_sub);
1498 return NULL;
1500 // Apply any post processing that needs recoding first
1501 if ((sub!=ERR) && !sub_no_text_pp && srp->post) srp->post(sub);
1502 #ifdef CONFIG_SORTSUB
1503 if(!sub_num || (first[sub_num - 1].start <= sub->start)){
1504 first[sub_num].start = sub->start;
1505 first[sub_num].end = sub->end;
1506 first[sub_num].lines = sub->lines;
1507 first[sub_num].alignment = sub->alignment;
1508 for(i = 0; i < sub->lines; ++i){
1509 first[sub_num].text[i] = sub->text[i];
1511 if (previous_sub_end){
1512 first[sub_num - 1].end = previous_sub_end;
1513 previous_sub_end = 0;
1515 } else {
1516 for(j = sub_num - 1; j >= 0; --j){
1517 first[j + 1].start = first[j].start;
1518 first[j + 1].end = first[j].end;
1519 first[j + 1].lines = first[j].lines;
1520 first[j + 1].alignment = first[j].alignment;
1521 for(i = 0; i < first[j].lines; ++i){
1522 first[j + 1].text[i] = first[j].text[i];
1524 if(!j || (first[j - 1].start <= sub->start)){
1525 first[j].start = sub->start;
1526 first[j].end = sub->end;
1527 first[j].lines = sub->lines;
1528 first[j].alignment = sub->alignment;
1529 for(i = 0; i < SUB_MAX_TEXT; ++i){
1530 first[j].text[i] = sub->text[i];
1532 if (previous_sub_end){
1533 first[j].end = first[j - 1].end;
1534 first[j - 1].end = previous_sub_end;
1535 previous_sub_end = 0;
1537 break;
1541 #endif
1542 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
1545 free_stream(fd);
1547 #ifdef CONFIG_ICONV
1548 subcp_close();
1549 #endif
1550 free(alloced_sub);
1552 // printf ("SUB: Subtitle format %s time.\n", uses_time?"uses":"doesn't use");
1553 mp_msg(MSGT_SUBREADER, MSGL_V,"SUB: Read %i subtitles, %i bad line(s).\n",
1554 sub_num, sub_errs);
1556 if(sub_num<=0){
1557 free(first);
1558 return NULL;
1561 // we do overlap if the user forced it (suboverlap_enable == 2) or
1562 // the user didn't forced no-overlapsub and the format is Jacosub or Ssa.
1563 // this is because usually overlapping subtitles are found in these formats,
1564 // while in others they are probably result of bad timing
1565 if ((suboverlap_enabled == 2) ||
1566 ((suboverlap_enabled) && ((sub_format == SUB_JACOSUB) || (sub_format == SUB_SSA)))) {
1567 adjust_subs_time(first, 6.0, fps, 0, sub_num, uses_time);/*~6 secs AST*/
1568 // here we manage overlapping subtitles
1569 sub_orig = sub_num;
1570 n_first = sub_num;
1571 sub_num = 0;
1572 second = NULL;
1573 // for each subtitle in first[] we deal with its 'block' of
1574 // bonded subtitles
1575 for (sub_first = 0; sub_first < n_first; ++sub_first) {
1576 unsigned long global_start = first[sub_first].start,
1577 global_end = first[sub_first].end, local_start, local_end;
1578 int lines_to_add = first[sub_first].lines, sub_to_add = 0,
1579 **placeholder = NULL, higher_line = 0, counter, start_block_sub = sub_num;
1580 char real_block = 1;
1582 // here we find the number of subtitles inside the 'block'
1583 // and its span interval. this works well only with sorted
1584 // subtitles
1585 while ((sub_first + sub_to_add + 1 < n_first) && (first[sub_first + sub_to_add + 1].start < global_end)) {
1586 ++sub_to_add;
1587 lines_to_add += first[sub_first + sub_to_add].lines;
1588 if (first[sub_first + sub_to_add].start < global_start) {
1589 global_start = first[sub_first + sub_to_add].start;
1591 if (first[sub_first + sub_to_add].end > global_end) {
1592 global_end = first[sub_first + sub_to_add].end;
1596 /* Avoid n^2 memory use for the "placeholder" data structure
1597 * below with subtitles that have a huge number of
1598 * consecutive overlapping lines. */
1599 lines_to_add = FFMIN(lines_to_add, SUB_MAX_TEXT);
1601 // we need a structure to keep trace of the screen lines
1602 // used by the subs, a 'placeholder'
1603 counter = 2 * sub_to_add + 1; // the maximum number of subs derived
1604 // from a block of sub_to_add+1 subs
1605 placeholder = malloc(sizeof(int *) * counter);
1606 for (i = 0; i < counter; ++i) {
1607 placeholder[i] = malloc(sizeof(int) * lines_to_add);
1608 for (j = 0; j < lines_to_add; ++j) {
1609 placeholder[i][j] = -1;
1613 counter = 0;
1614 local_end = global_start - 1;
1615 do {
1616 int ls;
1618 // here we find the beginning and the end of a new
1619 // subtitle in the block
1620 local_start = local_end + 1;
1621 local_end = global_end;
1622 for (j = 0; j <= sub_to_add; ++j) {
1623 if ((first[sub_first + j].start - 1 > local_start) && (first[sub_first + j].start - 1 < local_end)) {
1624 local_end = first[sub_first + j].start - 1;
1625 } else if ((first[sub_first + j].end > local_start) && (first[sub_first + j].end < local_end)) {
1626 local_end = first[sub_first + j].end;
1629 // here we allocate the screen lines to subs we must
1630 // display in current local_start-local_end interval.
1631 // if the subs were yet presents in the previous interval
1632 // they keep the same lines, otherside they get unused lines
1633 for (j = 0; j <= sub_to_add; ++j) {
1634 if ((first[sub_first + j].start <= local_end) && (first[sub_first + j].end > local_start)) {
1635 unsigned long sub_lines = first[sub_first + j].lines, fragment_length = lines_to_add + 1,
1636 tmp = 0;
1637 char boolean = 0;
1638 int fragment_position = -1;
1640 // if this is not the first new sub of the block
1641 // we find if this sub was present in the previous
1642 // new sub
1643 if (counter)
1644 for (i = 0; i < lines_to_add; ++i) {
1645 if (placeholder[counter - 1][i] == sub_first + j) {
1646 placeholder[counter][i] = sub_first + j;
1647 boolean = 1;
1650 if (boolean)
1651 continue;
1653 // we are looking for the shortest among all groups of
1654 // sequential blank lines whose length is greater than or
1655 // equal to sub_lines. we store in fragment_position the
1656 // position of the shortest group, in fragment_length its
1657 // length, and in tmp the length of the group currently
1658 // examinated
1659 for (i = 0; i < lines_to_add; ++i) {
1660 if (placeholder[counter][i] == -1) {
1661 // placeholder[counter][i] is part of the current group
1662 // of blank lines
1663 ++tmp;
1664 } else {
1665 if (tmp == sub_lines) {
1666 // current group's size fits exactly the one we
1667 // need, so we stop looking
1668 fragment_position = i - tmp;
1669 tmp = 0;
1670 break;
1672 if ((tmp) && (tmp > sub_lines) && (tmp < fragment_length)) {
1673 // current group is the best we found till here,
1674 // but is still bigger than the one we are looking
1675 // for, so we keep on looking
1676 fragment_length = tmp;
1677 fragment_position = i - tmp;
1678 tmp = 0;
1679 } else {
1680 // current group doesn't fit at all, so we forget it
1681 tmp = 0;
1685 if (tmp) {
1686 // last screen line is blank, a group ends with it
1687 if ((tmp >= sub_lines) && (tmp < fragment_length)) {
1688 fragment_position = i - tmp;
1691 if (fragment_position == -1) {
1692 // it was not possible to find free screen line(s) for a subtitle,
1693 // usually this means a bug in the code; however we do not overlap
1694 mp_msg(MSGT_SUBREADER, MSGL_WARN, "SUB: we could not find a suitable position for an overlapping subtitle\n");
1695 higher_line = SUB_MAX_TEXT + 1;
1696 break;
1697 } else {
1698 for (tmp = 0; tmp < sub_lines; ++tmp) {
1699 placeholder[counter][fragment_position + tmp] = sub_first + j;
1704 for (j = higher_line + 1; j < lines_to_add; ++j) {
1705 if (placeholder[counter][j] != -1)
1706 higher_line = j;
1707 else
1708 break;
1710 if (higher_line >= SUB_MAX_TEXT) {
1711 // the 'block' has too much lines, so we don't overlap the
1712 // subtitles
1713 second = realloc(second, (sub_num + sub_to_add + 1) * sizeof(subtitle));
1714 for (j = 0; j <= sub_to_add; ++j) {
1715 int ls;
1716 memset(&second[sub_num + j], '\0', sizeof(subtitle));
1717 second[sub_num + j].start = first[sub_first + j].start;
1718 second[sub_num + j].end = first[sub_first + j].end;
1719 second[sub_num + j].lines = first[sub_first + j].lines;
1720 second[sub_num + j].alignment = first[sub_first + j].alignment;
1721 for (ls = 0; ls < second[sub_num + j].lines; ls++) {
1722 second[sub_num + j].text[ls] = strdup(first[sub_first + j].text[ls]);
1725 sub_num += sub_to_add + 1;
1726 sub_first += sub_to_add;
1727 real_block = 0;
1728 break;
1731 // we read the placeholder structure and create the new
1732 // subs.
1733 second = realloc(second, (sub_num + 1) * sizeof(subtitle));
1734 memset(&second[sub_num], '\0', sizeof(subtitle));
1735 second[sub_num].start = local_start;
1736 second[sub_num].end = local_end;
1737 second[sub_num].alignment = first[sub_first].alignment;
1738 n_max = (lines_to_add < SUB_MAX_TEXT) ? lines_to_add : SUB_MAX_TEXT;
1739 for (i = 0, j = 0; j < n_max; ++j) {
1740 if (placeholder[counter][j] != -1) {
1741 int lines = first[placeholder[counter][j]].lines;
1742 for (ls = 0; ls < lines; ++ls) {
1743 second[sub_num].text[i++] = strdup(first[placeholder[counter][j]].text[ls]);
1745 j += lines - 1;
1746 } else {
1747 second[sub_num].text[i++] = strdup(" ");
1750 ++sub_num;
1751 ++counter;
1752 } while (local_end < global_end);
1753 if (real_block)
1754 for (i = 0; i < counter; ++i)
1755 second[start_block_sub + i].lines = higher_line + 1;
1757 counter = 2 * sub_to_add + 1;
1758 for (i = 0; i < counter; ++i) {
1759 free(placeholder[i]);
1761 free(placeholder);
1762 sub_first += sub_to_add;
1765 for (j = sub_orig - 1; j >= 0; --j) {
1766 for (i = first[j].lines - 1; i >= 0; --i) {
1767 free(first[j].text[i]);
1770 free(first);
1772 return_sub = second;
1773 } else { //if(suboverlap_enabled)
1774 adjust_subs_time(first, 6.0, fps, 1, sub_num, uses_time);/*~6 secs AST*/
1775 return_sub = first;
1777 if (return_sub == NULL) return NULL;
1778 subt_data = malloc(sizeof(sub_data));
1779 subt_data->filename = strdup(filename);
1780 subt_data->sub_uses_time = uses_time;
1781 subt_data->sub_num = sub_num;
1782 subt_data->sub_errs = sub_errs;
1783 subt_data->subtitles = return_sub;
1784 return subt_data;
1787 void list_sub_file(sub_data* subd){
1788 int i,j;
1789 subtitle *subs = subd->subtitles;
1791 for(j=0; j < subd->sub_num; j++){
1792 subtitle* egysub=&subs[j];
1793 mp_msg(MSGT_SUBREADER,MSGL_INFO,"%i line%c (%li-%li)\n",
1794 egysub->lines,
1795 (1==egysub->lines)?' ':'s',
1796 egysub->start,
1797 egysub->end);
1798 for (i=0; i<egysub->lines; i++) {
1799 mp_msg(MSGT_SUBREADER,MSGL_INFO,"\t\t%d: %s%s", i,egysub->text[i], i==egysub->lines-1?"":" \n ");
1801 mp_msg(MSGT_SUBREADER,MSGL_INFO,"\n");
1804 mp_msg(MSGT_SUBREADER,MSGL_INFO,"Subtitle format %s time.\n",
1805 subd->sub_uses_time ? "uses":"doesn't use");
1806 mp_msg(MSGT_SUBREADER,MSGL_INFO,"Read %i subtitles, %i errors.\n", subd->sub_num, subd->sub_errs);
1809 void dump_srt(sub_data* subd, float fps){
1810 int i,j;
1811 int h,m,s,ms;
1812 FILE * fd;
1813 subtitle * onesub;
1814 unsigned long temp;
1815 subtitle *subs = subd->subtitles;
1817 if (!subd->sub_uses_time && sub_fps == 0)
1818 sub_fps = fps;
1819 fd=fopen("dumpsub.srt","w");
1820 if(!fd)
1822 perror("dump_srt: fopen");
1823 return;
1825 for(i=0; i < subd->sub_num; i++)
1827 onesub=subs+i; //=&subs[i];
1828 fprintf(fd,"%d\n",i+1);//line number
1830 temp=onesub->start;
1831 if (!subd->sub_uses_time)
1832 temp = temp * 100 / sub_fps;
1833 temp -= sub_delay * 100;
1834 h=temp/360000;temp%=360000; //h =1*100*60*60
1835 m=temp/6000; temp%=6000; //m =1*100*60
1836 s=temp/100; temp%=100; //s =1*100
1837 ms=temp*10; //ms=1*10
1838 fprintf(fd,"%02d:%02d:%02d,%03d --> ",h,m,s,ms);
1840 temp=onesub->end;
1841 if (!subd->sub_uses_time)
1842 temp = temp * 100 / sub_fps;
1843 temp -= sub_delay * 100;
1844 h=temp/360000;temp%=360000;
1845 m=temp/6000; temp%=6000;
1846 s=temp/100; temp%=100;
1847 ms=temp*10;
1848 fprintf(fd,"%02d:%02d:%02d,%03d\n",h,m,s,ms);
1850 for(j=0;j<onesub->lines;j++)
1851 fprintf(fd,"%s\n",onesub->text[j]);
1853 fprintf(fd,"\n");
1855 fclose(fd);
1856 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");
1859 void dump_mpsub(sub_data* subd, float fps){
1860 int i,j;
1861 FILE *fd;
1862 float a,b;
1863 subtitle *subs = subd->subtitles;
1865 mpsub_position = subd->sub_uses_time? (sub_delay*100) : (sub_delay*fps);
1866 if (sub_fps==0) sub_fps=fps;
1868 fd=fopen ("dump.mpsub", "w");
1869 if (!fd) {
1870 perror ("dump_mpsub: fopen");
1871 return;
1875 if (subd->sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n");
1876 else fprintf (fd, "FORMAT=%5.2f\n\n", fps);
1878 for(j=0; j < subd->sub_num; j++){
1879 subtitle* egysub=&subs[j];
1880 if (subd->sub_uses_time) {
1881 a=((egysub->start-mpsub_position)/100.0);
1882 b=((egysub->end-egysub->start)/100.0);
1883 if ( (float)((int)a) == a)
1884 fprintf (fd, "%.0f",a);
1885 else
1886 fprintf (fd, "%.2f",a);
1888 if ( (float)((int)b) == b)
1889 fprintf (fd, " %.0f\n",b);
1890 else
1891 fprintf (fd, " %.2f\n",b);
1892 } else {
1893 fprintf (fd, "%ld %ld\n", (long)((egysub->start*(fps/sub_fps))-((mpsub_position*(fps/sub_fps)))),
1894 (long)(((egysub->end)-(egysub->start))*(fps/sub_fps)));
1897 mpsub_position = egysub->end;
1898 for (i=0; i<egysub->lines; i++) {
1899 fprintf (fd, "%s\n",egysub->text[i]);
1901 fprintf (fd, "\n");
1903 fclose (fd);
1904 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");
1907 void dump_microdvd(sub_data* subd, float fps) {
1908 int i, delay;
1909 FILE *fd;
1910 subtitle *subs = subd->subtitles;
1911 if (sub_fps == 0)
1912 sub_fps = fps;
1913 fd = fopen("dumpsub.sub", "w");
1914 if (!fd) {
1915 perror("dumpsub.sub: fopen");
1916 return;
1918 delay = sub_delay * sub_fps;
1919 for (i = 0; i < subd->sub_num; ++i) {
1920 int j, start, end;
1921 start = subs[i].start;
1922 end = subs[i].end;
1923 if (subd->sub_uses_time) {
1924 start = start * sub_fps / 100 ;
1925 end = end * sub_fps / 100;
1927 else {
1928 start = start * sub_fps / fps;
1929 end = end * sub_fps / fps;
1931 start -= delay;
1932 end -= delay;
1933 fprintf(fd, "{%d}{%d}", start, end);
1934 for (j = 0; j < subs[i].lines; ++j)
1935 fprintf(fd, "%s%s", j ? "|" : "", subs[i].text[j]);
1936 fprintf(fd, "\n");
1938 fclose(fd);
1939 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.sub\'.\n");
1942 void dump_jacosub(sub_data* subd, float fps) {
1943 int i,j;
1944 int h,m,s,cs;
1945 FILE * fd;
1946 subtitle * onesub;
1947 unsigned long temp;
1948 subtitle *subs = subd->subtitles;
1950 if (!subd->sub_uses_time && sub_fps == 0)
1951 sub_fps = fps;
1952 fd=fopen("dumpsub.jss","w");
1953 if(!fd)
1955 perror("dump_jacosub: fopen");
1956 return;
1958 fprintf(fd, "#TIMERES %d\n", (subd->sub_uses_time) ? 100 : (int)sub_fps);
1959 for(i=0; i < subd->sub_num; i++)
1961 onesub=subs+i; //=&subs[i];
1963 temp=onesub->start;
1964 if (!subd->sub_uses_time)
1965 temp = temp * 100 / sub_fps;
1966 temp -= sub_delay * 100;
1967 h=temp/360000;temp%=360000; //h =1*100*60*60
1968 m=temp/6000; temp%=6000; //m =1*100*60
1969 s=temp/100; temp%=100; //s =1*100
1970 cs=temp; //cs=1*10
1971 fprintf(fd,"%02d:%02d:%02d.%02d ",h,m,s,cs);
1973 temp=onesub->end;
1974 if (!subd->sub_uses_time)
1975 temp = temp * 100 / sub_fps;
1976 temp -= sub_delay * 100;
1977 h=temp/360000;temp%=360000;
1978 m=temp/6000; temp%=6000;
1979 s=temp/100; temp%=100;
1980 cs=temp;
1981 fprintf(fd,"%02d:%02d:%02d.%02d {~} ",h,m,s,cs);
1983 for(j=0;j<onesub->lines;j++)
1984 fprintf(fd,"%s%s",j ? "\\n" : "", onesub->text[j]);
1986 fprintf(fd,"\n");
1988 fclose(fd);
1989 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");
1992 void dump_sami(sub_data* subd, float fps) {
1993 int i,j;
1994 FILE * fd;
1995 subtitle * onesub;
1996 unsigned long temp;
1997 subtitle *subs = subd->subtitles;
1999 if (!subd->sub_uses_time && sub_fps == 0)
2000 sub_fps = fps;
2001 fd=fopen("dumpsub.smi","w");
2002 if(!fd)
2004 perror("dump_jacosub: fopen");
2005 return;
2007 fprintf(fd, "<SAMI>\n"
2008 "<HEAD>\n"
2009 " <STYLE TYPE=\"Text/css\">\n"
2010 " <!--\n"
2011 " 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"
2012 " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n"
2013 " -->\n"
2014 " </STYLE>\n"
2015 "</HEAD>\n"
2016 "<BODY>\n");
2017 for(i=0; i < subd->sub_num; i++)
2019 onesub=subs+i; //=&subs[i];
2021 temp=onesub->start;
2022 if (!subd->sub_uses_time)
2023 temp = temp * 100 / sub_fps;
2024 temp -= sub_delay * 100;
2025 fprintf(fd,"\t<SYNC Start=%lu>\n"
2026 "\t <P>", temp * 10);
2028 for(j=0;j<onesub->lines;j++)
2029 fprintf(fd,"%s%s",j ? "<br>" : "", onesub->text[j]);
2031 fprintf(fd,"\n");
2033 temp=onesub->end;
2034 if (!subd->sub_uses_time)
2035 temp = temp * 100 / sub_fps;
2036 temp -= sub_delay * 100;
2037 fprintf(fd,"\t<SYNC Start=%lu>\n"
2038 "\t <P>&nbsp;\n", temp * 10);
2040 fprintf(fd, "</BODY>\n"
2041 "</SAMI>\n");
2042 fclose(fd);
2043 mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");
2046 void sub_free( sub_data * subd )
2048 int i, j;
2050 if ( !subd ) return;
2052 for (i = 0; i < subd->sub_num; i++)
2053 for (j = 0; j < subd->subtitles[i].lines; j++)
2054 free( subd->subtitles[i].text[j] );
2055 free( subd->subtitles );
2056 free( subd->filename );
2057 free( subd );
2060 #define MAX_SUBLINE 512
2062 * \brief parse text and append it to subtitle in sub
2063 * \param sub subtitle struct to add text to
2064 * \param txt text to parse
2065 * \param len length of text in txt
2066 * \param endpts pts at which this subtitle text should be removed again
2068 * <> and {} are interpreted as comment delimiters, "\n", "\N", '\n', '\r'
2069 * and '\0' are interpreted as newlines, duplicate, leading and trailing
2070 * newlines are ignored.
2072 void sub_add_text(subtitle *sub, const char *txt, int len, double endpts) {
2073 int comment = 0;
2074 int double_newline = 1; // ignore newlines at the beginning
2075 int i, pos;
2076 char *buf;
2077 if (sub->lines >= SUB_MAX_TEXT) return;
2078 pos = 0;
2079 buf = malloc(MAX_SUBLINE + 1);
2080 sub->text[sub->lines] = buf;
2081 sub->endpts[sub->lines] = endpts;
2082 for (i = 0; i < len && pos < MAX_SUBLINE; i++) {
2083 char c = txt[i];
2084 if (c == '<') comment |= 1;
2085 if (c == '{') comment |= 2;
2086 if (comment) {
2087 if (c == '}') comment &= ~2;
2088 if (c == '>') comment &= ~1;
2089 continue;
2091 if (pos == MAX_SUBLINE - 1) {
2092 i--;
2093 c = 0;
2095 if (c == '\\' && i + 1 < len) {
2096 c = txt[++i];
2097 if (c == 'n' || c == 'N') c = 0;
2099 if (c == '\n' || c == '\r') c = 0;
2100 if (c) {
2101 double_newline = 0;
2102 buf[pos++] = c;
2103 } else if (!double_newline) {
2104 if (sub->lines >= SUB_MAX_TEXT - 1) {
2105 mp_msg(MSGT_VO, MSGL_WARN, "Too many subtitle lines\n");
2106 break;
2108 double_newline = 1;
2109 buf[pos] = 0;
2110 sub->lines++;
2111 pos = 0;
2112 buf = malloc(MAX_SUBLINE + 1);
2113 sub->text[sub->lines] = buf;
2114 sub->endpts[sub->lines] = endpts;
2117 buf[pos] = 0;
2118 if (sub->lines < SUB_MAX_TEXT &&
2119 strlen(sub->text[sub->lines]))
2120 sub->lines++;
2124 * \brief remove outdated subtitle lines.
2125 * \param sub subtitle struct to modify
2126 * \param pts current pts. All lines with endpts <= this will be removed.
2127 * Use MP_NOPTS_VALUE to remove all lines
2128 * \return 1 if sub was modified, 0 otherwise.
2130 int sub_clear_text(subtitle *sub, double pts) {
2131 int i = 0;
2132 int changed = 0;
2133 while (i < sub->lines) {
2134 double endpts = sub->endpts[i];
2135 if (pts == MP_NOPTS_VALUE || (endpts != MP_NOPTS_VALUE && pts >= endpts)) {
2136 int j;
2137 free(sub->text[i]);
2138 for (j = i + 1; j < sub->lines; j++) {
2139 sub->text[j - 1] = sub->text[j];
2140 sub->endpts[j - 1] = sub->endpts[j];
2142 sub->lines--;
2143 changed = 1;
2144 } else
2145 i++;
2147 return changed;