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 *****************************************************************************/
33 #include <vlc_input.h>
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
42 #include <vlc_demux.h>
43 #include <vlc_charset.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t *p_this );
49 static void Close( vlc_object_t *p_this );
51 #define SUB_DELAY_LONGTEXT \
52 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
53 #define SUB_FPS_LONGTEXT \
54 N_("Override the normal frames per second settings. " \
55 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
56 #define SUB_TYPE_LONGTEXT \
57 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
58 "\"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
59 "\"sami\", \"dvdsubtitle\", \"mpl2\" and \"auto\" (meaning autodetection, this " \
60 "should always work).")
61 static const char *ppsz_sub_type[] =
63 "auto", "microdvd", "subrip", "subviewer", "ssa1",
64 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2"
68 set_shortname( _("Subtitles"));
69 set_description( _("Text subtitles parser") );
70 set_capability( "demux2", 0 );
71 set_category( CAT_INPUT );
72 set_subcategory( SUBCAT_INPUT_DEMUX );
73 add_float( "sub-fps", 0.0, NULL,
74 N_("Frames per second"),
75 SUB_FPS_LONGTEXT, VLC_TRUE );
76 add_integer( "sub-delay", 0, NULL,
77 N_("Subtitles delay"),
78 SUB_DELAY_LONGTEXT, VLC_TRUE );
79 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
80 SUB_TYPE_LONGTEXT, VLC_TRUE );
81 change_string_list( ppsz_sub_type, NULL, NULL );
82 set_callbacks( Open, Close );
84 add_shortcut( "subtitle" );
87 /*****************************************************************************
89 *****************************************************************************/
92 SUB_TYPE_UNKNOWN = -1,
101 SUB_TYPE_DVDSUBTITLE,
111 static int TextLoad( text_t *, stream_t *s );
112 static void TextUnload( text_t * );
129 int64_t i_next_demux_date;
130 int64_t i_microsecperframe;
135 subtitle_t *subtitle;
140 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
141 static int ParseSubRip ( demux_t *, subtitle_t *, int );
142 static int ParseSubViewer ( demux_t *, subtitle_t *, int );
143 static int ParseSSA ( demux_t *, subtitle_t *, int );
144 static int ParseVplayer ( demux_t *, subtitle_t *, int );
145 static int ParseSami ( demux_t *, subtitle_t *, int );
146 static int ParseDVDSubtitle( demux_t *, subtitle_t *, int );
147 static int ParseMPL2 ( demux_t *, subtitle_t *, int );
151 const char *psz_type_name;
153 const char *psz_name;
154 int (*pf_read)( demux_t *, subtitle_t*, int );
155 } sub_read_subtitle_function [] =
157 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
158 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
159 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
160 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
161 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
162 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
163 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
164 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
165 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
166 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
167 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
170 static int Demux( demux_t * );
171 static int Control( demux_t *, int, va_list );
173 /*static void Fix( demux_t * );*/
175 /*****************************************************************************
177 *****************************************************************************/
178 static int Open ( vlc_object_t *p_this )
180 demux_t *p_demux = (demux_t*)p_this;
185 int (*pf_read)( demux_t *, subtitle_t*, int );
188 if( !p_demux->b_force )
190 msg_Dbg( p_demux, "subtitle demux discarded" );
194 p_demux->pf_demux = Demux;
195 p_demux->pf_control = Control;
196 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
197 p_sys->psz_header = NULL;
198 p_sys->i_subtitle = 0;
199 p_sys->i_subtitles = 0;
200 p_sys->subtitle = NULL;
201 p_sys->i_microsecperframe = 40000;
204 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
206 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
208 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
210 /* Check for override of the fps */
211 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
214 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
215 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
218 /* Get or probe the type */
219 p_sys->i_type = SUB_TYPE_UNKNOWN;
220 psz_type = var_CreateGetString( p_demux, "sub-type" );
227 if( sub_read_subtitle_function[i].psz_type_name == NULL )
230 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
233 p_sys->i_type = sub_read_subtitle_function[i].i_type;
240 /* Probe if unknown type */
241 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
246 msg_Dbg( p_demux, "autodetecting subtitle format" );
247 for( i_try = 0; i_try < 256; i_try++ )
251 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
254 if( strcasestr( s, "<SAMI>" ) )
256 p_sys->i_type = SUB_TYPE_SAMI;
259 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
260 sscanf( s, "{%d}{}", &i_dummy ) == 1)
262 p_sys->i_type = SUB_TYPE_MICRODVD;
266 "%d:%d:%d,%d --> %d:%d:%d,%d",
267 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
268 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
270 p_sys->i_type = SUB_TYPE_SUBRIP;
273 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
275 p_sys->i_type = SUB_TYPE_SSA1;
278 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
280 p_sys->i_type = SUB_TYPE_ASS;
283 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
285 p_sys->i_type = SUB_TYPE_SSA2_4;
288 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
290 p_sys->i_type = SUB_TYPE_SSA2_4;
293 else if( !strncasecmp( s, "Dialogue:", 9 ) )
295 p_sys->i_type = SUB_TYPE_ASS;
298 else if( strcasestr( s, "[INFORMATION]" ) )
300 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
303 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
304 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
306 p_sys->i_type = SUB_TYPE_VPLAYER;
309 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
310 &i_dummy, &i_dummy ) == 4 )
312 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
315 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
316 sscanf( s, "[%d][]", &i_dummy ) == 1)
318 p_sys->i_type = SUB_TYPE_MPL2;
328 /* It will nearly always work even for non seekable stream thanks the
329 * caching system, and if it fails we lose just a few sub */
330 if( stream_Seek( p_demux->s, 0 ) )
332 msg_Warn( p_demux, "failed to rewind" );
335 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
337 msg_Err( p_demux, "failed to recognize subtitle type" );
344 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
346 msg_Dbg( p_demux, "detected %s format",
347 sub_read_subtitle_function[i].psz_name );
348 pf_read = sub_read_subtitle_function[i].pf_read;
353 msg_Dbg( p_demux, "loading all subtitles..." );
355 /* Load the whole file */
356 TextLoad( &p_sys->txt, p_demux->s );
361 if( p_sys->i_subtitles >= i_max )
364 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
365 sizeof(subtitle_t) * i_max ) ) )
367 msg_Err( p_demux, "out of memory");
368 free( p_sys->subtitle );
369 TextUnload( &p_sys->txt );
375 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
376 p_sys->i_subtitles ) )
379 p_sys->i_subtitles++;
382 TextUnload( &p_sys->txt );
384 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
386 /* Fix subtitle (order and time) *** */
387 p_sys->i_subtitle = 0;
389 if( p_sys->i_subtitles > 0 )
391 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
393 if( p_sys->i_length <= 0 )
394 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
397 /* *** add subtitle ES *** */
398 if( p_sys->i_type == SUB_TYPE_SSA1 ||
399 p_sys->i_type == SUB_TYPE_SSA2_4 ||
400 p_sys->i_type == SUB_TYPE_ASS )
402 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
406 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
408 if( p_sys->psz_header != NULL )
410 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
411 fmt.p_extra = strdup( p_sys->psz_header );
413 p_sys->es = es_out_Add( p_demux->out, &fmt );
418 /*****************************************************************************
419 * Close: Close subtitle demux
420 *****************************************************************************/
421 static void Close( vlc_object_t *p_this )
423 demux_t *p_demux = (demux_t*)p_this;
424 demux_sys_t *p_sys = p_demux->p_sys;
427 for( i = 0; i < p_sys->i_subtitles; i++ )
428 free( p_sys->subtitle[i].psz_text );
429 free( p_sys->subtitle );
434 /*****************************************************************************
436 *****************************************************************************/
437 static int Control( demux_t *p_demux, int i_query, va_list args )
439 demux_sys_t *p_sys = p_demux->p_sys;
445 case DEMUX_GET_LENGTH:
446 pi64 = (int64_t*)va_arg( args, int64_t * );
447 *pi64 = p_sys->i_length;
451 pi64 = (int64_t*)va_arg( args, int64_t * );
452 if( p_sys->i_subtitle < p_sys->i_subtitles )
454 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
460 i64 = (int64_t)va_arg( args, int64_t );
461 p_sys->i_subtitle = 0;
462 while( p_sys->i_subtitle < p_sys->i_subtitles &&
463 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
468 if( p_sys->i_subtitle >= p_sys->i_subtitles )
472 case DEMUX_GET_POSITION:
473 pf = (double*)va_arg( args, double * );
474 if( p_sys->i_subtitle >= p_sys->i_subtitles )
478 else if( p_sys->i_subtitles > 0 )
480 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
481 (double)p_sys->i_length;
489 case DEMUX_SET_POSITION:
490 f = (double)va_arg( args, double );
491 i64 = f * p_sys->i_length;
493 p_sys->i_subtitle = 0;
494 while( p_sys->i_subtitle < p_sys->i_subtitles &&
495 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
499 if( p_sys->i_subtitle >= p_sys->i_subtitles )
503 case DEMUX_SET_NEXT_DEMUX_TIME:
504 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
509 case DEMUX_GET_ATTACHMENTS:
510 case DEMUX_GET_TITLE_INFO:
511 case DEMUX_HAS_UNSUPPORTED_META:
515 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
520 /*****************************************************************************
521 * Demux: Send subtitle to decoder
522 *****************************************************************************/
523 static int Demux( demux_t *p_demux )
525 demux_sys_t *p_sys = p_demux->p_sys;
528 if( p_sys->i_subtitle >= p_sys->i_subtitles )
531 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
532 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
534 /* Should not happen */
535 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
538 while( p_sys->i_subtitle < p_sys->i_subtitles &&
539 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
542 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
551 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
557 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
563 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
564 p_block->i_dts = p_block->i_pts;
565 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
568 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
571 memcpy( p_block->p_buffer,
572 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
573 if( p_block->i_pts > 0 )
575 es_out_Send( p_demux->out, p_sys->es, p_block );
579 block_Release( p_block );
585 p_sys->i_next_demux_date = 0;
590 /*****************************************************************************
591 * Fix: fix time stamp and order of subtitle
592 *****************************************************************************/
593 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
594 static void Fix( demux_t *p_demux )
596 demux_sys_t *p_sys = p_demux->p_sys;
600 /* *** fix order (to be sure...) *** */
601 /* We suppose that there are near in order and this durty bubble sort
602 * wont take too much time
607 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
609 if( p_sys->subtitle[i_index].i_start <
610 p_sys->subtitle[i_index - 1].i_start )
614 p_sys->subtitle + i_index - 1,
615 sizeof( subtitle_t ) );
616 memcpy( p_sys->subtitle + i_index - 1,
617 p_sys->subtitle + i_index,
618 sizeof( subtitle_t ) );
619 memcpy( p_sys->subtitle + i_index,
621 sizeof( subtitle_t ) );
629 static int TextLoad( text_t *txt, stream_t *s )
635 txt->i_line_count = 0;
637 txt->line = calloc( i_line_max, sizeof( char * ) );
639 /* load the complete file */
642 char *psz = stream_ReadLine( s );
647 txt->line[txt->i_line_count++] = psz;
648 if( txt->i_line_count >= i_line_max )
651 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
655 if( txt->i_line_count <= 0 )
663 static void TextUnload( text_t *txt )
667 for( i = 0; i < txt->i_line_count; i++ )
669 free( txt->line[i] );
673 txt->i_line_count = 0;
676 static char *TextGetLine( text_t *txt )
678 if( txt->i_line >= txt->i_line_count )
681 return txt->line[txt->i_line++];
683 static void TextPreviousLine( text_t *txt )
685 if( txt->i_line > 0 )
689 /*****************************************************************************
690 * Specific Subtitle function
691 *****************************************************************************/
694 * {n1}{n2}Line1|Line2|Line3....
695 * where n1 and n2 are the video frame number (n2 can be empty)
697 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
700 demux_sys_t *p_sys = p_demux->p_sys;
701 text_t *txt = &p_sys->txt;
709 const char *s = TextGetLine( txt );
713 psz_text = malloc( strlen(s) + 1 );
719 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
720 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
723 if( i_start != 1 || i_stop != 1 )
726 /* We found a possible setting of the framerate "{1}{1}23.976" */
727 /* Check if it's usable, and if the sub-fps is not set */
728 f_fps = us_strtod( psz_text, NULL );
729 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
730 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
735 /* replace | by \n */
736 for( i = 0; psz_text[i] != '\0'; i++ )
738 if( psz_text[i] == '|' )
743 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
744 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
745 p_subtitle->psz_text = psz_text;
749 /* ParseSubRipSubViewer
752 * h1:m1:s1,d1 --> h2:m2:s2,d2
757 * Format SubViewer v1/v2
758 * h1:m1:s1.d1,h2:m2:s2.d2
763 * We ignore line number for SubRip
765 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
767 vlc_bool_t b_replace_br )
769 demux_sys_t *p_sys = p_demux->p_sys;
770 text_t *txt = &p_sys->txt;
775 const char *s = TextGetLine( txt );
776 int h1, m1, s1, d1, h2, m2, s2, d2;
781 if( sscanf( s, psz_fmt,
783 &h2, &m2, &s2, &d2 ) == 8 )
785 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
786 (int64_t)m1 * 60*1000 +
788 (int64_t)d1 ) * 1000;
790 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
791 (int64_t)m2 * 60*1000 +
793 (int64_t)d2 ) * 1000;
798 /* Now read text until an empty line */
799 psz_text = strdup("");
804 const char *s = TextGetLine( txt );
817 p_subtitle->psz_text = psz_text;
821 i_old = strlen( psz_text );
822 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
825 strcat( psz_text, s );
826 strcat( psz_text, "\n" );
828 /* replace [br] by \n */
833 while( ( p = strstr( psz_text, "[br]" ) ) )
836 memmove( p, &p[3], strlen(&p[3])+1 );
843 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
846 return ParseSubRipSubViewer( p_demux, p_subtitle,
847 "%d:%d:%d,%d --> %d:%d:%d,%d",
852 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
855 return ParseSubRipSubViewer( p_demux, p_subtitle,
856 "%d:%d:%d.%d,%d:%d:%d.%d",
862 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
865 demux_sys_t *p_sys = p_demux->p_sys;
866 text_t *txt = &p_sys->txt;
870 const char *s = TextGetLine( txt );
871 int h1, m1, s1, c1, h2, m2, s2, c2;
878 /* We expect (SSA2-4):
879 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
880 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
882 * 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.
886 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
887 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
890 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
891 psz_text = malloc( strlen(s) );
896 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
902 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
903 /* (Layer comes from ASS specs ... it's empty for SSA.) */
904 if( p_sys->i_type == SUB_TYPE_SSA1 )
906 /* SSA1 has only 8 commas before the text starts, not 9 */
907 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
912 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
914 /* ReadOrder, Layer, %s(rest of fields) */
915 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
916 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
917 memcpy( psz_text, temp, strlen(temp) );
920 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
921 (int64_t)m1 * 60*1000 +
923 (int64_t)c1 * 10 ) * 1000;
924 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
925 (int64_t)m2 * 60*1000 +
927 (int64_t)c2 * 10 ) * 1000;
928 p_subtitle->psz_text = psz_text;
933 /* All the other stuff we add to the header field */
934 if( !p_sys->psz_header )
935 p_sys->psz_header = strdup( "" );
936 if( !p_sys->psz_header )
940 realloc( p_sys->psz_header,
941 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
942 strcat( p_sys->psz_header, s );
943 strcat( p_sys->psz_header, "\n" );
949 * h:m:s:Line1|Line2|Line3....
951 * h:m:s Line1|Line2|Line3....
953 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
956 demux_sys_t *p_sys = p_demux->p_sys;
957 text_t *txt = &p_sys->txt;
963 const char *s = TextGetLine( txt );
969 psz_text = malloc( strlen( s ) + 1 );
973 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
974 &h1, &m1, &s1, psz_text ) == 4 )
976 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
977 (int64_t)m1 * 60*1000 +
978 (int64_t)s1 * 1000 ) * 1000;
979 p_subtitle->i_stop = 0;
985 /* replace | by \n */
986 for( i = 0; psz_text[i] != '\0'; i++ )
988 if( psz_text[i] == '|' )
991 p_subtitle->psz_text = psz_text;
997 static char *ParseSamiSearch( text_t *txt,
998 char *psz_start, const char *psz_str )
1000 if( psz_start && strcasestr( psz_start, psz_str ) )
1002 char *s = strcasestr( psz_start, psz_str );
1003 return &s[strlen( psz_str )];
1008 char *p = TextGetLine( txt );
1012 if( strcasestr( p, psz_str ) )
1014 char *s = strcasestr( p, psz_str );
1015 return &s[strlen( psz_str )];
1019 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1021 demux_sys_t *p_sys = p_demux->p_sys;
1022 text_t *txt = &p_sys->txt;
1027 unsigned int i_text;
1028 char text[8192]; /* Arbitrary but should be long enough */
1030 /* search "Start=" */
1031 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1032 return VLC_EGENERIC;
1034 /* get start value */
1035 i_start = strtol( s, &s, 0 );
1038 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1039 return VLC_EGENERIC;
1042 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1043 return VLC_EGENERIC;
1047 /* now get all txt until a "Start=" line */
1051 /* Search non empty line */
1052 while( s && *s == '\0' )
1053 s = TextGetLine( txt );
1059 if( !strncasecmp( s, "<br", 3 ) )
1063 else if( strcasestr( s, "Start=" ) )
1065 TextPreviousLine( txt );
1068 s = ParseSamiSearch( txt, s, ">" );
1070 else if( !strncmp( s, " ", 6 ) )
1075 else if( *s == '\t' )
1085 if( c != '\0' && i_text+1 < sizeof(text) )
1088 text[i_text] = '\0';
1092 p_subtitle->i_start = i_start * 1000;
1093 p_subtitle->i_stop = 0;
1094 p_subtitle->psz_text = strdup( text );
1106 * TODO it can have a header
1113 * LANG support would be cool
1114 * CODEPAGE is probably mandatory FIXME
1116 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1119 demux_sys_t *p_sys = p_demux->p_sys;
1120 text_t *txt = &p_sys->txt;
1125 const char *s = TextGetLine( txt );
1129 return VLC_EGENERIC;
1133 &h1, &m1, &s1, &c1 ) == 4 )
1135 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1136 (int64_t)m1 * 60*1000 +
1137 (int64_t)s1 * 1000 +
1138 (int64_t)c1 * 10) * 1000;
1139 p_subtitle->i_stop = 0;
1144 /* Now read text until a line containing "}" */
1145 psz_text = strdup("");
1150 const char *s = TextGetLine( txt );
1157 return VLC_EGENERIC;
1160 i_len = strlen( s );
1161 if( i_len == 1 && s[0] == '}')
1163 p_subtitle->psz_text = psz_text;
1167 i_old = strlen( psz_text );
1168 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1171 strcat( psz_text, s );
1172 strcat( psz_text, "\n" );
1178 * [n1][n2]Line1|Line2|Line3...
1179 * where n1 and n2 are the video frame number (n2 can be empty)
1181 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1183 demux_sys_t *p_sys = p_demux->p_sys;
1184 text_t *txt = &p_sys->txt;
1190 const char *s = TextGetLine( txt );
1195 return VLC_EGENERIC;
1197 psz_text = malloc( strlen(s) + 1 );
1203 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1204 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1206 p_subtitle->i_start = (int64_t)i_start * 100000;
1207 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1213 /* replace | by \n */
1214 for( i = 0; psz_text[i] != '\0'; )
1216 if( psz_text[i] == '|' )
1220 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1221 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1225 p_subtitle->psz_text = psz_text;