1 /*****************************************************************************
2 * subtitle.c: Demux for subtitle text files.
3 *****************************************************************************
4 * Copyright (C) 1999-2007 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Derk-Jan Hartman <hartman at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
43 #include <vlc_demux.h>
44 #include <vlc_charset.h>
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t *p_this );
50 static void Close( vlc_object_t *p_this );
52 #define SUB_DELAY_LONGTEXT \
53 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
54 #define SUB_FPS_LONGTEXT \
55 N_("Override the normal frames per second settings. " \
56 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
57 #define SUB_TYPE_LONGTEXT \
58 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
59 "\"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
60 "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\" and \"auto\" (meaning autodetection, this " \
61 "should always work).")
62 static const char *ppsz_sub_type[] =
64 "auto", "microdvd", "subrip", "subviewer", "ssa1",
65 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
70 set_shortname( _("Subtitles"));
71 set_description( _("Text subtitles parser") );
72 set_capability( "demux", 0 );
73 set_category( CAT_INPUT );
74 set_subcategory( SUBCAT_INPUT_DEMUX );
75 add_float( "sub-fps", 0.0, NULL,
76 N_("Frames per second"),
77 SUB_FPS_LONGTEXT, true );
78 add_integer( "sub-delay", 0, NULL,
79 N_("Subtitles delay"),
80 SUB_DELAY_LONGTEXT, true );
81 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
82 SUB_TYPE_LONGTEXT, true );
83 change_string_list( ppsz_sub_type, NULL, NULL );
84 set_callbacks( Open, Close );
86 add_shortcut( "subtitle" );
89 /*****************************************************************************
91 *****************************************************************************/
94 SUB_TYPE_UNKNOWN = -1,
102 SUB_TYPE_SUBVIEWER, //SUBVIEWER 2!
103 SUB_TYPE_DVDSUBTITLE,
118 static int TextLoad( text_t *, stream_t *s );
119 static void TextUnload( text_t * );
136 int64_t i_next_demux_date;
137 int64_t i_microsecperframe;
142 subtitle_t *subtitle;
147 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
148 static int ParseSubRip ( demux_t *, subtitle_t *, int );
149 static int ParseSubViewer ( demux_t *, subtitle_t *, int );
150 static int ParseSSA ( demux_t *, subtitle_t *, int );
151 static int ParseVplayer ( demux_t *, subtitle_t *, int );
152 static int ParseSami ( demux_t *, subtitle_t *, int );
153 static int ParseDVDSubtitle( demux_t *, subtitle_t *, int );
154 static int ParseMPL2 ( demux_t *, subtitle_t *, int );
155 static int ParseAQT ( demux_t *, subtitle_t *, int );
156 static int ParsePJS ( demux_t *, subtitle_t *, int );
157 static int ParseMPSub ( demux_t *, subtitle_t *, int );
158 static int ParseJSS ( demux_t *, subtitle_t *, int );
162 const char *psz_type_name;
164 const char *psz_name;
165 int (*pf_read)( demux_t *, subtitle_t*, int );
166 } sub_read_subtitle_function [] =
168 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
169 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
170 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
171 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
172 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
173 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
174 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
175 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
176 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
177 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
178 { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT },
179 { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS },
180 { "mpsub", SUB_TYPE_MPSUB, "MPSub", ParseMPSub },
181 { "jacosub", SUB_TYPE_JACOSUB, "JacoSub", ParseJSS },
182 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
193 static int Demux( demux_t * );
194 static int Control( demux_t *, int, va_list );
196 /*static void Fix( demux_t * );*/
198 /*****************************************************************************
200 *****************************************************************************/
201 static int Open ( vlc_object_t *p_this )
203 demux_t *p_demux = (demux_t*)p_this;
208 int (*pf_read)( demux_t *, subtitle_t*, int );
211 if( !p_demux->b_force )
213 msg_Dbg( p_demux, "subtitle demux discarded" );
217 p_demux->pf_demux = Demux;
218 p_demux->pf_control = Control;
219 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
220 p_sys->psz_header = NULL;
221 p_sys->i_subtitle = 0;
222 p_sys->i_subtitles = 0;
223 p_sys->subtitle = NULL;
224 p_sys->i_microsecperframe = 40000;
227 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
229 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
231 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
233 /* Check for override of the fps */
234 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
237 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
238 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
241 /* Get or probe the type */
242 p_sys->i_type = SUB_TYPE_UNKNOWN;
243 psz_type = var_CreateGetString( p_demux, "sub-type" );
250 if( sub_read_subtitle_function[i].psz_type_name == NULL )
253 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
256 p_sys->i_type = sub_read_subtitle_function[i].i_type;
263 /* Probe if unknown type */
264 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
269 msg_Dbg( p_demux, "autodetecting subtitle format" );
270 for( i_try = 0; i_try < 256; i_try++ )
275 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
278 if( strcasestr( s, "<SAMI>" ) )
280 p_sys->i_type = SUB_TYPE_SAMI;
283 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
284 sscanf( s, "{%d}{}", &i_dummy ) == 1)
286 p_sys->i_type = SUB_TYPE_MICRODVD;
290 "%d:%d:%d,%d --> %d:%d:%d,%d",
291 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
292 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
294 p_sys->i_type = SUB_TYPE_SUBRIP;
297 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
299 p_sys->i_type = SUB_TYPE_SSA1;
302 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
304 p_sys->i_type = SUB_TYPE_ASS;
307 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
309 p_sys->i_type = SUB_TYPE_SSA2_4;
312 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
314 p_sys->i_type = SUB_TYPE_SSA2_4;
317 else if( !strncasecmp( s, "Dialogue:", 9 ) )
319 p_sys->i_type = SUB_TYPE_ASS;
322 else if( strcasestr( s, "[INFORMATION]" ) )
324 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
327 else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d", &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy ) == 7 ||
328 sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
330 p_sys->i_type = SUB_TYPE_JACOSUB;
332 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
333 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
335 p_sys->i_type = SUB_TYPE_VPLAYER;
338 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
339 &i_dummy, &i_dummy ) == 4 )
341 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
344 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
345 sscanf( s, "[%d][]", &i_dummy ) == 1)
347 p_sys->i_type = SUB_TYPE_MPL2;
350 else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
351 ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
354 p_sys->i_type = SUB_TYPE_MPSUB;
356 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
358 p_sys->i_type = SUB_TYPE_AQT;
360 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
362 p_sys->i_type = SUB_TYPE_PJS;
371 /* It will nearly always work even for non seekable stream thanks the
372 * caching system, and if it fails we lose just a few sub */
373 if( stream_Seek( p_demux->s, 0 ) )
375 msg_Warn( p_demux, "failed to rewind" );
378 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
380 msg_Err( p_demux, "failed to recognize subtitle type" );
387 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
389 msg_Dbg( p_demux, "detected %s format",
390 sub_read_subtitle_function[i].psz_name );
391 pf_read = sub_read_subtitle_function[i].pf_read;
396 msg_Dbg( p_demux, "loading all subtitles..." );
398 /* Load the whole file */
399 TextLoad( &p_sys->txt, p_demux->s );
404 if( p_sys->i_subtitles >= i_max )
407 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
408 sizeof(subtitle_t) * i_max ) ) )
410 msg_Err( p_demux, "out of memory");
411 free( p_sys->subtitle );
412 TextUnload( &p_sys->txt );
418 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
419 p_sys->i_subtitles ) )
422 p_sys->i_subtitles++;
425 TextUnload( &p_sys->txt );
427 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
429 /* Fix subtitle (order and time) *** */
430 p_sys->i_subtitle = 0;
432 if( p_sys->i_subtitles > 0 )
434 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
436 if( p_sys->i_length <= 0 )
437 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
440 /* *** add subtitle ES *** */
441 if( p_sys->i_type == SUB_TYPE_SSA1 ||
442 p_sys->i_type == SUB_TYPE_SSA2_4 ||
443 p_sys->i_type == SUB_TYPE_ASS )
445 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
449 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
451 if( p_sys->psz_header != NULL )
453 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
454 fmt.p_extra = strdup( p_sys->psz_header );
456 p_sys->es = es_out_Add( p_demux->out, &fmt );
461 /*****************************************************************************
462 * Close: Close subtitle demux
463 *****************************************************************************/
464 static void Close( vlc_object_t *p_this )
466 demux_t *p_demux = (demux_t*)p_this;
467 demux_sys_t *p_sys = p_demux->p_sys;
470 for( i = 0; i < p_sys->i_subtitles; i++ )
471 free( p_sys->subtitle[i].psz_text );
472 free( p_sys->subtitle );
477 /*****************************************************************************
479 *****************************************************************************/
480 static int Control( demux_t *p_demux, int i_query, va_list args )
482 demux_sys_t *p_sys = p_demux->p_sys;
488 case DEMUX_GET_LENGTH:
489 pi64 = (int64_t*)va_arg( args, int64_t * );
490 *pi64 = p_sys->i_length;
494 pi64 = (int64_t*)va_arg( args, int64_t * );
495 if( p_sys->i_subtitle < p_sys->i_subtitles )
497 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
503 i64 = (int64_t)va_arg( args, int64_t );
504 p_sys->i_subtitle = 0;
505 while( p_sys->i_subtitle < p_sys->i_subtitles &&
506 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
511 if( p_sys->i_subtitle >= p_sys->i_subtitles )
515 case DEMUX_GET_POSITION:
516 pf = (double*)va_arg( args, double * );
517 if( p_sys->i_subtitle >= p_sys->i_subtitles )
521 else if( p_sys->i_subtitles > 0 )
523 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
524 (double)p_sys->i_length;
532 case DEMUX_SET_POSITION:
533 f = (double)va_arg( args, double );
534 i64 = f * p_sys->i_length;
536 p_sys->i_subtitle = 0;
537 while( p_sys->i_subtitle < p_sys->i_subtitles &&
538 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
542 if( p_sys->i_subtitle >= p_sys->i_subtitles )
546 case DEMUX_SET_NEXT_DEMUX_TIME:
547 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
552 case DEMUX_GET_ATTACHMENTS:
553 case DEMUX_GET_TITLE_INFO:
554 case DEMUX_HAS_UNSUPPORTED_META:
558 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
563 /*****************************************************************************
564 * Demux: Send subtitle to decoder
565 *****************************************************************************/
566 static int Demux( demux_t *p_demux )
568 demux_sys_t *p_sys = p_demux->p_sys;
571 if( p_sys->i_subtitle >= p_sys->i_subtitles )
574 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
575 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
577 /* Should not happen */
578 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
581 while( p_sys->i_subtitle < p_sys->i_subtitles &&
582 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
585 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
594 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
600 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
606 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
607 p_block->i_dts = p_block->i_pts;
608 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
611 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
614 memcpy( p_block->p_buffer,
615 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
616 if( p_block->i_pts > 0 )
618 es_out_Send( p_demux->out, p_sys->es, p_block );
622 block_Release( p_block );
628 p_sys->i_next_demux_date = 0;
633 /*****************************************************************************
634 * Fix: fix time stamp and order of subtitle
635 *****************************************************************************/
636 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
637 static void Fix( demux_t *p_demux )
639 demux_sys_t *p_sys = p_demux->p_sys;
643 /* *** fix order (to be sure...) *** */
644 /* We suppose that there are near in order and this durty bubble sort
645 * wont take too much time
650 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
652 if( p_sys->subtitle[i_index].i_start <
653 p_sys->subtitle[i_index - 1].i_start )
657 p_sys->subtitle + i_index - 1,
658 sizeof( subtitle_t ) );
659 memcpy( p_sys->subtitle + i_index - 1,
660 p_sys->subtitle + i_index,
661 sizeof( subtitle_t ) );
662 memcpy( p_sys->subtitle + i_index,
664 sizeof( subtitle_t ) );
672 static int TextLoad( text_t *txt, stream_t *s )
678 txt->i_line_count = 0;
680 txt->line = calloc( i_line_max, sizeof( char * ) );
682 /* load the complete file */
685 char *psz = stream_ReadLine( s );
690 txt->line[txt->i_line_count++] = psz;
691 if( txt->i_line_count >= i_line_max )
694 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
698 if( txt->i_line_count <= 0 )
706 static void TextUnload( text_t *txt )
710 for( i = 0; i < txt->i_line_count; i++ )
712 free( txt->line[i] );
716 txt->i_line_count = 0;
719 static char *TextGetLine( text_t *txt )
721 if( txt->i_line >= txt->i_line_count )
724 return txt->line[txt->i_line++];
726 static void TextPreviousLine( text_t *txt )
728 if( txt->i_line > 0 )
732 /*****************************************************************************
733 * Specific Subtitle function
734 *****************************************************************************/
737 * {n1}{n2}Line1|Line2|Line3....
738 * where n1 and n2 are the video frame number (n2 can be empty)
740 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
743 demux_sys_t *p_sys = p_demux->p_sys;
744 text_t *txt = &p_sys->txt;
752 const char *s = TextGetLine( txt );
756 psz_text = malloc( strlen(s) + 1 );
762 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
763 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
766 if( i_start != 1 || i_stop != 1 )
769 /* We found a possible setting of the framerate "{1}{1}23.976" */
770 /* Check if it's usable, and if the sub-fps is not set */
771 f_fps = us_strtod( psz_text, NULL );
772 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
773 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
778 /* replace | by \n */
779 for( i = 0; psz_text[i] != '\0'; i++ )
781 if( psz_text[i] == '|' )
786 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
787 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
788 p_subtitle->psz_text = psz_text;
792 /* ParseSubRipSubViewer
795 * h1:m1:s1,d1 --> h2:m2:s2,d2
800 * Format SubViewer v1/v2
801 * h1:m1:s1.d1,h2:m2:s2.d2
806 * We ignore line number for SubRip
808 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
812 demux_sys_t *p_sys = p_demux->p_sys;
813 text_t *txt = &p_sys->txt;
818 const char *s = TextGetLine( txt );
819 int h1, m1, s1, d1, h2, m2, s2, d2;
824 if( sscanf( s, psz_fmt,
826 &h2, &m2, &s2, &d2 ) == 8 )
828 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
829 (int64_t)m1 * 60*1000 +
831 (int64_t)d1 ) * 1000;
833 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
834 (int64_t)m2 * 60*1000 +
836 (int64_t)d2 ) * 1000;
841 /* Now read text until an empty line */
842 psz_text = strdup("");
847 const char *s = TextGetLine( txt );
860 p_subtitle->psz_text = psz_text;
864 i_old = strlen( psz_text );
865 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
868 strcat( psz_text, s );
869 strcat( psz_text, "\n" );
871 /* replace [br] by \n */
876 while( ( p = strstr( psz_text, "[br]" ) ) )
879 memmove( p, &p[3], strlen(&p[3])+1 );
886 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
889 return ParseSubRipSubViewer( p_demux, p_subtitle,
890 "%d:%d:%d,%d --> %d:%d:%d,%d",
895 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
898 return ParseSubRipSubViewer( p_demux, p_subtitle,
899 "%d:%d:%d.%d,%d:%d:%d.%d",
905 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
908 demux_sys_t *p_sys = p_demux->p_sys;
909 text_t *txt = &p_sys->txt;
913 const char *s = TextGetLine( txt );
914 int h1, m1, s1, c1, h2, m2, s2, c2;
921 /* We expect (SSA2-4):
922 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
923 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
925 * SSA-1 is similar but only has 8 commas up untill the subtitle text. Probably the Effect field is no present, but not 100 % sure.
929 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
930 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
933 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
934 psz_text = malloc( strlen(s) );
939 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
945 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
946 /* (Layer comes from ASS specs ... it's empty for SSA.) */
947 if( p_sys->i_type == SUB_TYPE_SSA1 )
949 /* SSA1 has only 8 commas before the text starts, not 9 */
950 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
955 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
957 /* ReadOrder, Layer, %s(rest of fields) */
958 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
959 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
960 memcpy( psz_text, temp, strlen(temp) );
963 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
964 (int64_t)m1 * 60*1000 +
966 (int64_t)c1 * 10 ) * 1000;
967 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
968 (int64_t)m2 * 60*1000 +
970 (int64_t)c2 * 10 ) * 1000;
971 p_subtitle->psz_text = psz_text;
976 /* All the other stuff we add to the header field */
977 if( !p_sys->psz_header )
978 p_sys->psz_header = strdup( "" );
979 if( !p_sys->psz_header )
983 realloc( p_sys->psz_header,
984 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
985 strcat( p_sys->psz_header, s );
986 strcat( p_sys->psz_header, "\n" );
992 * h:m:s:Line1|Line2|Line3....
994 * h:m:s Line1|Line2|Line3....
996 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
999 demux_sys_t *p_sys = p_demux->p_sys;
1000 text_t *txt = &p_sys->txt;
1006 const char *s = TextGetLine( txt );
1010 return VLC_EGENERIC;
1012 psz_text = malloc( strlen( s ) + 1 );
1016 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1017 &h1, &m1, &s1, psz_text ) == 4 )
1019 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1020 (int64_t)m1 * 60*1000 +
1021 (int64_t)s1 * 1000 ) * 1000;
1022 p_subtitle->i_stop = 0;
1028 /* replace | by \n */
1029 for( i = 0; psz_text[i] != '\0'; i++ )
1031 if( psz_text[i] == '|' )
1034 p_subtitle->psz_text = psz_text;
1040 static char *ParseSamiSearch( text_t *txt,
1041 char *psz_start, const char *psz_str )
1043 if( psz_start && strcasestr( psz_start, psz_str ) )
1045 char *s = strcasestr( psz_start, psz_str );
1046 return &s[strlen( psz_str )];
1051 char *p = TextGetLine( txt );
1055 if( strcasestr( p, psz_str ) )
1057 char *s = strcasestr( p, psz_str );
1058 return &s[strlen( psz_str )];
1062 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1064 demux_sys_t *p_sys = p_demux->p_sys;
1065 text_t *txt = &p_sys->txt;
1070 unsigned int i_text;
1071 char text[8192]; /* Arbitrary but should be long enough */
1073 /* search "Start=" */
1074 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1075 return VLC_EGENERIC;
1077 /* get start value */
1078 i_start = strtol( s, &s, 0 );
1081 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1082 return VLC_EGENERIC;
1085 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1086 return VLC_EGENERIC;
1090 /* now get all txt until a "Start=" line */
1094 /* Search non empty line */
1095 while( s && *s == '\0' )
1096 s = TextGetLine( txt );
1102 if( !strncasecmp( s, "<br", 3 ) )
1106 else if( strcasestr( s, "Start=" ) )
1108 TextPreviousLine( txt );
1111 s = ParseSamiSearch( txt, s, ">" );
1113 else if( !strncmp( s, " ", 6 ) )
1118 else if( *s == '\t' )
1128 if( c != '\0' && i_text+1 < sizeof(text) )
1131 text[i_text] = '\0';
1135 p_subtitle->i_start = i_start * 1000;
1136 p_subtitle->i_stop = 0;
1137 p_subtitle->psz_text = strdup( text );
1149 * TODO it can have a header
1156 * LANG support would be cool
1157 * CODEPAGE is probably mandatory FIXME
1159 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1162 demux_sys_t *p_sys = p_demux->p_sys;
1163 text_t *txt = &p_sys->txt;
1168 const char *s = TextGetLine( txt );
1172 return VLC_EGENERIC;
1176 &h1, &m1, &s1, &c1 ) == 4 )
1178 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1179 (int64_t)m1 * 60*1000 +
1180 (int64_t)s1 * 1000 +
1181 (int64_t)c1 * 10) * 1000;
1182 p_subtitle->i_stop = 0;
1187 /* Now read text until a line containing "}" */
1188 psz_text = strdup("");
1193 const char *s = TextGetLine( txt );
1200 return VLC_EGENERIC;
1203 i_len = strlen( s );
1204 if( i_len == 1 && s[0] == '}')
1206 p_subtitle->psz_text = psz_text;
1210 i_old = strlen( psz_text );
1211 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1214 strcat( psz_text, s );
1215 strcat( psz_text, "\n" );
1221 * [n1][n2]Line1|Line2|Line3...
1222 * where n1 and n2 are the video frame number (n2 can be empty)
1224 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1226 demux_sys_t *p_sys = p_demux->p_sys;
1227 text_t *txt = &p_sys->txt;
1233 const char *s = TextGetLine( txt );
1238 return VLC_EGENERIC;
1240 psz_text = malloc( strlen(s) + 1 );
1246 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1247 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1249 p_subtitle->i_start = (int64_t)i_start * 100000;
1250 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1256 for( i = 0; psz_text[i] != '\0'; )
1258 /* replace | by \n */
1259 if( psz_text[i] == '|' )
1263 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1264 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1268 p_subtitle->psz_text = psz_text;
1272 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1274 demux_sys_t *p_sys = p_demux->p_sys;
1275 text_t *txt = &p_sys->txt;
1276 char *psz_text = strdup( "" );
1278 int i_firstline = 1;
1284 const char *s = TextGetLine( txt );
1287 return VLC_EGENERIC;
1290 if( sscanf (s, "-->> %d", &t) == 1)
1292 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1293 p_subtitle->i_stop = 0;
1295 /* Starting of a subtitle */
1300 /* We have been too far: end of the subtitle, begin of next */
1310 i_old = strlen( psz_text ) + 1;
1311 psz_text = realloc( psz_text, i_old + strlen( s ) + 1 );
1314 strcat( psz_text, s );
1315 strcat( psz_text, "\n" );
1316 if( txt->i_line == txt->i_line_count )
1320 p_subtitle->psz_text = psz_text;
1324 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1326 demux_sys_t *p_sys = p_demux->p_sys;
1327 text_t *txt = &p_sys->txt;
1332 const char *s = TextGetLine( txt );
1336 return VLC_EGENERIC;
1338 psz_text = malloc( strlen(s) + 1 );
1341 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1343 /* 1/10th of second ? Frame based ? FIXME */
1344 p_subtitle->i_start = 10 * t1;
1345 p_subtitle->i_stop = 10 * t2;
1346 /* Remove latest " */
1347 psz_text[ strlen(psz_text) - 1 ] = '\0';
1353 p_subtitle->psz_text = psz_text;
1354 msg_Dbg( p_demux, "%s", psz_text );
1358 static float mpsub_total = 0.0;
1359 static float mpsub_factor = 0.0;
1361 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1363 demux_sys_t *p_sys = p_demux->p_sys;
1364 text_t *txt = &p_sys->txt;
1365 char *psz_text = strdup( "" );
1369 const char *s = TextGetLine( txt );
1375 return VLC_EGENERIC;
1376 if( strstr( s, "FORMAT" ) )
1378 if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1380 mpsub_factor = 100.0;
1384 psz_temp = malloc( strlen(s) );
1385 if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1388 f_fps = us_strtod( psz_temp, NULL );
1389 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1390 var_SetFloat( p_demux, "sub-fps", f_fps );
1399 if( sscanf (s, "%f %f", &f1, &f2 ) == 2 )
1401 mpsub_total += f1 * mpsub_factor;
1402 p_subtitle->i_start = (int64_t)(10000.0 * mpsub_total);
1403 mpsub_total += f2 * mpsub_factor;
1404 p_subtitle->i_stop = (int64_t)(10000.0 * mpsub_total);
1411 const char *s = TextGetLine( txt );
1414 return VLC_EGENERIC;
1416 int i_len = strlen( s );
1420 int i_old = strlen( psz_text );
1422 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1426 strcat( psz_text, s );
1427 strcat( psz_text, "\n" );
1430 p_subtitle->psz_text = psz_text;
1434 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1436 demux_sys_t *p_sys = p_demux->p_sys;
1437 text_t *txt = &p_sys->txt;
1438 char *psz_text, *psz_orig;
1439 char *psz_text2, *psz_orig2;
1440 int h1, h2, m1, m2, s1, s2, f1, f2;
1441 static int i_comment = 0;
1443 static int jss_time_resolution = 30;
1444 static int jss_time_shift = 0;
1446 /* Parse the main lines */
1449 const char *s = TextGetLine( txt );
1451 return VLC_EGENERIC;
1453 psz_text = malloc( strlen( s ) + 1 );
1454 psz_orig = psz_text;
1456 if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1457 &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1459 p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1460 (int64_t)( ( f1 + jss_time_shift ) / jss_time_resolution ) )
1462 p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1463 (int64_t)( ( f2 + jss_time_shift ) / jss_time_resolution ) )
1466 else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1468 p_subtitle->i_start = (int64_t)(
1469 ( f1 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
1470 p_subtitle->i_stop = (int64_t)(
1471 ( f2 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
1473 else if( s[0] == '#' )
1475 int h = 0, m =0, sec = 1, f = 1;
1479 strcpy( psz_text, s );
1481 switch( toupper( psz_text[1] ) )
1484 shift = isalpha( psz_text[2] ) ? 6 : 2 ;
1486 if( sscanf( &psz_text[shift], "%d", &h ) )
1488 /* Negative shifting */
1495 if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1497 if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1499 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1504 sscanf( &psz_text[shift], "%d:%d.%d", &m, &sec, &f );
1511 sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1514 jss_time_shift = ( ( h * 3600 + m * 60 + sec )
1515 * jss_time_resolution + f ) * inv;
1520 shift = isalpha( psz_text[2] ) ? 8 : 2 ;
1522 sscanf( &psz_text[shift], "%d", &jss_time_resolution );
1535 /* Skip the blanks */
1536 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1538 /* Parse the directives */
1539 if( isalpha( *psz_text ) || *psz_text == '[' )
1541 while( *psz_text != ' ' )
1544 /* Directives are NOT parsed yet */
1545 /* directive = malloc( strlen( psz_text ) + 1 );
1546 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1549 /* Skip the blanks after directives */
1550 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1552 psz_text2 = calloc( strlen( psz_text) + 1, 1 );
1553 psz_orig2 = psz_text2;
1555 for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1566 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1578 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1587 if( (*(psz_text + 1 ) ) == 'n' )
1594 if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
1595 ( toupper(*(psz_text + 1 ) ) == 'F' ) )
1597 psz_text++; psz_text++;
1600 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1601 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1602 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1603 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1608 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1609 (*(psz_text + 1 ) ) == '\\' )
1611 else if( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n'
1612 || *(psz_text + 1 ) == '\0' )
1614 char *s2 = TextGetLine( txt );
1616 return VLC_EGENERIC;
1618 while ( *s2 == ' ' ) s2++;
1620 /* Here to parse the second line, we should add s2 to
1621 psz_text and go on the for( ) line 1556 in order to
1622 parse the next line.
1628 *psz_text2 = *psz_text;
1635 p_subtitle->psz_text = psz_orig2;