1 /*****************************************************************************
2 * subtitle.c: Demux for subtitle text files.
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
37 #include <vlc/input.h>
38 #include "vlc_video.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t *p_this );
44 static void Close( vlc_object_t *p_this );
46 #define SUB_DELAY_LONGTEXT \
47 "Delay subtitles (in 1/10s)"
48 #define SUB_FPS_LONGTEXT \
49 "Override frames per second. " \
50 "It will only work with MicroDVD subtitles."
51 #define SUB_TYPE_LONGTEXT \
52 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
53 "\"sami\" (auto for autodetection, it should always work)."
54 static char *ppsz_sub_type[] =
56 "auto", "microdvd", "subrip", "subviewer", "ssa1",
57 "ssa2-4", "vplayer", "sami"
61 set_shortname( _("Subtitles"));
62 set_description( _("Text subtitles demux") );
63 set_capability( "demux2", 0 );
64 set_category( CAT_INPUT );
65 set_subcategory( SUBCAT_INPUT_DEMUX );
66 add_float( "sub-fps", 0.0, NULL,
67 N_("Frames per second"),
68 SUB_FPS_LONGTEXT, VLC_TRUE );
69 add_integer( "sub-delay", 0, NULL,
70 N_("Subtitles delay"),
71 SUB_DELAY_LONGTEXT, VLC_TRUE );
72 add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
73 SUB_TYPE_LONGTEXT, VLC_TRUE );
74 change_string_list( ppsz_sub_type, 0, 0 );
75 set_callbacks( Open, Close );
77 add_shortcut( "subtitle" );
80 /*****************************************************************************
82 *****************************************************************************/
85 SUB_TYPE_UNKNOWN = -1,
101 static int TextLoad( text_t *, stream_t *s );
102 static void TextUnload( text_t * );
119 int64_t i_next_demux_date;
121 int64_t i_microsecperframe;
122 int64_t i_original_mspf;
127 subtitle_t *subtitle;
132 static int ParseMicroDvd ( demux_t *, subtitle_t * );
133 static int ParseSubRip ( demux_t *, subtitle_t * );
134 static int ParseSubViewer( demux_t *, subtitle_t * );
135 static int ParseSSA ( demux_t *, subtitle_t * );
136 static int ParseVplayer ( demux_t *, subtitle_t * );
137 static int ParseSami ( demux_t *, subtitle_t * );
144 int (*pf_read)( demux_t *, subtitle_t* );
145 } sub_read_subtitle_function [] =
147 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
148 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
149 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer",ParseSubViewer },
150 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
151 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4",ParseSSA },
152 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
153 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
154 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
157 static int Demux( demux_t * );
158 static int Control( demux_t *, int, va_list );
160 static void Fix( demux_t * );
162 /*****************************************************************************
164 *****************************************************************************/
165 static int Open ( vlc_object_t *p_this )
167 demux_t *p_demux = (demux_t*)p_this;
172 int (*pf_read)( demux_t *, subtitle_t* );
175 if( strcmp( p_demux->psz_demux, "subtitle" ) )
177 msg_Dbg( p_demux, "subtitle demux discarded" );
181 p_demux->pf_demux = Demux;
182 p_demux->pf_control = Control;
183 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
184 p_sys->psz_header = NULL;
185 p_sys->i_subtitle = 0;
186 p_sys->i_subtitles= 0;
187 p_sys->subtitle = NULL;
191 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
194 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
198 p_sys->i_microsecperframe = 0;
201 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
204 p_sys->i_original_mspf = (int64_t)( (float)1000000 / f_fps );
208 p_sys->i_original_mspf = 0;
211 /* Get or probe the type */
212 p_sys->i_type = SUB_TYPE_UNKNOWN;
213 psz_type = var_CreateGetString( p_demux, "sub-type" );
220 if( sub_read_subtitle_function[i].psz_type_name == NULL )
223 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
226 p_sys->i_type = sub_read_subtitle_function[i].i_type;
233 /* Probe if unknown type */
234 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
239 msg_Dbg( p_demux, "autodetecting subtitle format" );
240 for( i_try = 0; i_try < 256; i_try++ )
244 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
247 if( strcasestr( s, "<SAMI>" ) )
249 p_sys->i_type = SUB_TYPE_SAMI;
252 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
253 sscanf( s, "{%d}{}", &i_dummy ) == 1)
255 p_sys->i_type = SUB_TYPE_MICRODVD;
259 "%d:%d:%d,%d --> %d:%d:%d,%d",
260 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
261 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
263 p_sys->i_type = SUB_TYPE_SUBRIP;
267 "!: This is a Sub Station Alpha v%d.x script.",
272 p_sys->i_type = SUB_TYPE_SSA1;
276 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
280 else if( strcasestr( s, "This is a Sub Station Alpha v4 script" ) )
282 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
285 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
287 p_sys->i_type = SUB_TYPE_SSA2_4; /* could be wrong */
290 else if( strcasestr( s, "[INFORMATION]" ) )
292 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
295 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
296 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
298 p_sys->i_type = SUB_TYPE_VPLAYER;
308 /* It will nearly always work even for non seekable stream thanks the
309 * caching system, and if it fails we loose just a few sub */
310 if( stream_Seek( p_demux->s, 0 ) )
312 msg_Warn( p_demux, "failed to rewind" );
315 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
317 msg_Err( p_demux, "failed to recognize subtitle type" );
323 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
325 msg_Dbg( p_demux, "detected %s format",
326 sub_read_subtitle_function[i].psz_name );
327 pf_read = sub_read_subtitle_function[i].pf_read;
332 msg_Dbg( p_demux, "loading all subtitles..." );
334 /* Load the whole file */
335 TextLoad( &p_sys->txt, p_demux->s );
340 if( p_sys->i_subtitles >= i_max )
343 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
344 sizeof(subtitle_t) * i_max ) ) )
346 msg_Err( p_demux, "out of memory");
351 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
354 p_sys->i_subtitles++;
357 TextUnload( &p_sys->txt );
359 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
361 /* Fix subtitle (order and time) *** */
362 p_sys->i_subtitle = 0;
364 if( p_sys->i_subtitles > 0 )
366 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
368 if( p_sys->i_length <= 0 )
369 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
372 /* *** add subtitle ES *** */
373 if( p_sys->i_type == SUB_TYPE_SSA1 ||
374 p_sys->i_type == SUB_TYPE_SSA2_4 )
376 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
380 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
382 if( p_sys->psz_header != NULL )
384 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
385 fmt.p_extra = strdup( p_sys->psz_header );
387 p_sys->es = es_out_Add( p_demux->out, &fmt );
392 /*****************************************************************************
393 * Close: Close subtitle demux
394 *****************************************************************************/
395 static void Close( vlc_object_t *p_this )
397 demux_t *p_demux = (demux_t*)p_this;
398 demux_sys_t *p_sys = p_demux->p_sys;
401 for( i = 0; i < p_sys->i_subtitles; i++ )
403 if( p_sys->subtitle[i].psz_text )
404 free( p_sys->subtitle[i].psz_text );
406 if( p_sys->subtitle )
407 free( p_sys->subtitle );
412 /*****************************************************************************
414 *****************************************************************************/
415 static int Control( demux_t *p_demux, int i_query, va_list args )
417 demux_sys_t *p_sys = p_demux->p_sys;
423 case DEMUX_GET_LENGTH:
424 pi64 = (int64_t*)va_arg( args, int64_t * );
425 *pi64 = p_sys->i_length;
429 pi64 = (int64_t*)va_arg( args, int64_t * );
430 if( p_sys->i_subtitle < p_sys->i_subtitles )
432 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
438 i64 = (int64_t)va_arg( args, int64_t );
439 p_sys->i_subtitle = 0;
440 while( p_sys->i_subtitle < p_sys->i_subtitles &&
441 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
446 if( p_sys->i_subtitle >= p_sys->i_subtitles )
450 case DEMUX_GET_POSITION:
451 pf = (double*)va_arg( args, double * );
452 if( p_sys->i_subtitle >= p_sys->i_subtitles )
456 else if( p_sys->i_subtitles > 0 )
458 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
459 (double)p_sys->i_length;
467 case DEMUX_SET_POSITION:
468 f = (double)va_arg( args, double );
469 i64 = f * p_sys->i_length;
471 p_sys->i_subtitle = 0;
472 while( p_sys->i_subtitle < p_sys->i_subtitles &&
473 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
477 if( p_sys->i_subtitle >= p_sys->i_subtitles )
481 case DEMUX_SET_NEXT_DEMUX_TIME:
482 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
487 case DEMUX_GET_TITLE_INFO:
491 msg_Err( p_demux, "unknown query in subtitle control" );
496 /*****************************************************************************
497 * Demux: Send subtitle to decoder
498 *****************************************************************************/
499 static int Demux( demux_t *p_demux )
501 demux_sys_t *p_sys = p_demux->p_sys;
504 if( p_sys->i_subtitle >= p_sys->i_subtitles )
507 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
508 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
510 /* Should not happen */
511 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
514 while( p_sys->i_subtitle < p_sys->i_subtitles &&
515 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
518 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
527 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
533 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
539 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
540 p_block->i_dts = p_block->i_pts;
541 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
544 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
547 memcpy( p_block->p_buffer,
548 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
549 if( p_block->i_pts > 0 )
551 es_out_Send( p_demux->out, p_sys->es, p_block );
555 block_Release( p_block );
561 p_sys->i_next_demux_date = 0;
566 /*****************************************************************************
567 * Fix: fix time stamp and order of subtitle
568 *****************************************************************************/
569 static void Fix( demux_t *p_demux )
571 demux_sys_t *p_sys = p_demux->p_sys;
575 /* *** fix order (to be sure...) *** */
576 /* We suppose that there are near in order and this durty bubble sort
577 * wont take too much time
582 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
584 if( p_sys->subtitle[i_index].i_start <
585 p_sys->subtitle[i_index - 1].i_start )
589 p_sys->subtitle + i_index - 1,
590 sizeof( subtitle_t ) );
591 memcpy( p_sys->subtitle + i_index - 1,
592 p_sys->subtitle + i_index,
593 sizeof( subtitle_t ) );
594 memcpy( p_sys->subtitle + i_index,
596 sizeof( subtitle_t ) );
603 static int TextLoad( text_t *txt, stream_t *s )
609 txt->i_line_count = 0;
611 txt->line = calloc( i_line_max, sizeof( char * ) );
613 /* load the complete file */
616 char *psz = stream_ReadLine( s );
621 txt->line[txt->i_line_count++] = psz;
622 if( txt->i_line_count >= i_line_max )
625 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
629 if( txt->i_line_count <= 0 )
637 static void TextUnload( text_t *txt )
641 for( i = 0; i < txt->i_line_count; i++ )
643 free( txt->line[i] );
647 txt->i_line_count = 0;
650 static char *TextGetLine( text_t *txt )
652 if( txt->i_line >= txt->i_line_count )
655 return txt->line[txt->i_line++];
657 static void TextPreviousLine( text_t *txt )
659 if( txt->i_line > 0 )
663 /*****************************************************************************
664 * Specific Subtitle function
665 *****************************************************************************/
666 #define MAX_LINE 8192
667 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
669 demux_sys_t *p_sys = p_demux->p_sys;
670 text_t *txt = &p_sys->txt;
673 * {n1}{n2}Line1|Line2|Line3....
674 * where n1 and n2 are the video frame number...
679 char buffer_text[MAX_LINE + 1];
684 int i_microsecperframe = 40000; /* default to 25 fps */
685 if( p_sys->i_microsecperframe > 0 )
686 i_microsecperframe = p_sys->i_microsecperframe;
688 p_subtitle->i_start = 0;
689 p_subtitle->i_stop = 0;
690 p_subtitle->psz_text = NULL;
694 if( ( s = TextGetLine( txt ) ) == NULL )
696 return( VLC_EGENERIC );
701 memset( buffer_text, '\0', MAX_LINE );
702 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
703 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
708 /* replace | by \n */
709 for( i = 0; i < strlen( buffer_text ); i++ )
711 if( buffer_text[i] == '|' )
713 buffer_text[i] = '\n';
717 p_subtitle->i_start = (int64_t)i_start * i_microsecperframe;
718 p_subtitle->i_stop = (int64_t)i_stop * i_microsecperframe;
719 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
723 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
725 demux_sys_t *p_sys = p_demux->p_sys;
726 text_t *txt = &p_sys->txt;
730 * h1:m1:s1,d1 --> h2:m2:s2,d2
738 char buffer_text[ 10 * MAX_LINE];
743 p_subtitle->i_start = 0;
744 p_subtitle->i_stop = 0;
745 p_subtitle->psz_text = NULL;
749 int h1, m1, s1, d1, h2, m2, s2, d2;
750 if( ( s = TextGetLine( txt ) ) == NULL )
752 return( VLC_EGENERIC );
755 "%d:%d:%d,%d --> %d:%d:%d,%d",
757 &h2, &m2, &s2, &d2 ) == 8 )
759 i_start = ( (int64_t)h1 * 3600*1000 +
760 (int64_t)m1 * 60*1000 +
762 (int64_t)d1 ) * 1000;
764 i_stop = ( (int64_t)h2 * 3600*1000 +
765 (int64_t)m2 * 60*1000 +
767 (int64_t)d2 ) * 1000;
769 /* Now read text until an empty line */
770 for( i_buffer_text = 0;; )
773 if( ( s = TextGetLine( txt ) ) == NULL )
775 return( VLC_EGENERIC );
781 /* empty line -> end of this subtitle */
782 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
783 p_subtitle->i_start = i_start;
784 p_subtitle->i_stop = i_stop;
785 p_subtitle->psz_text = strdup( buffer_text );
786 /* If framerate is available, use sub-fps */
787 if( p_sys->i_microsecperframe != 0 &&
788 p_sys->i_original_mspf != 0)
790 p_subtitle->i_start = (int64_t)i_start *
791 p_sys->i_microsecperframe/
792 p_sys->i_original_mspf;
793 p_subtitle->i_stop = (int64_t)i_stop *
794 p_sys->i_microsecperframe /
795 p_sys->i_original_mspf;
801 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
803 memcpy( buffer_text + i_buffer_text,
806 i_buffer_text += i_len;
808 buffer_text[i_buffer_text] = '\n';
817 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
819 demux_sys_t *p_sys = p_demux->p_sys;
820 text_t *txt = &p_sys->txt;
823 * h1:m1:s1.d1,h2:m2:s2.d2
828 * ( works with subviewer and subviewer v2 )
831 char buffer_text[ 10 * MAX_LINE];
836 p_subtitle->i_start = 0;
837 p_subtitle->i_stop = 0;
838 p_subtitle->psz_text = NULL;
842 int h1, m1, s1, d1, h2, m2, s2, d2;
843 if( ( s = TextGetLine( txt ) ) == NULL )
845 return( VLC_EGENERIC );
848 "%d:%d:%d.%d,%d:%d:%d.%d",
850 &h2, &m2, &s2, &d2 ) == 8 )
852 i_start = ( (int64_t)h1 * 3600*1000 +
853 (int64_t)m1 * 60*1000 +
855 (int64_t)d1 ) * 1000;
857 i_stop = ( (int64_t)h2 * 3600*1000 +
858 (int64_t)m2 * 60*1000 +
860 (int64_t)d2 ) * 1000;
862 /* Now read text until an empty line */
863 for( i_buffer_text = 0;; )
866 if( ( s = TextGetLine( txt ) ) == NULL )
868 return( VLC_EGENERIC );
874 /* empty line -> end of this subtitle */
875 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
876 p_subtitle->i_start = i_start;
877 p_subtitle->i_stop = i_stop;
879 /* replace [br] by \n */
880 for( i = 0; i < i_buffer_text - 3; i++ )
882 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
883 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
885 char *temp = buffer_text + i + 1;
886 buffer_text[i] = '\n';
887 memmove( temp, temp+3, strlen( temp ) -3 );
888 temp[strlen( temp )-3] = '\0';
891 p_subtitle->psz_text = strdup( buffer_text );
896 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
898 memcpy( buffer_text + i_buffer_text,
901 i_buffer_text += i_len;
903 buffer_text[i_buffer_text] = '\n';
913 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
915 demux_sys_t *p_sys = p_demux->p_sys;
916 text_t *txt = &p_sys->txt;
918 char buffer_text[ 10 * MAX_LINE];
923 p_subtitle->i_start = 0;
924 p_subtitle->i_stop = 0;
925 p_subtitle->psz_text = NULL;
929 int h1, m1, s1, c1, h2, m2, s2, c2;
932 if( ( s = TextGetLine( txt ) ) == NULL )
934 return( VLC_EGENERIC );
936 p_subtitle->psz_text = malloc( strlen( s ) );
939 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
943 buffer_text ) == 10 )
945 i_start = ( (int64_t)h1 * 3600*1000 +
946 (int64_t)m1 * 60*1000 +
948 (int64_t)c1 * 10 ) * 1000;
950 i_stop = ( (int64_t)h2 * 3600*1000 +
951 (int64_t)m2 * 60*1000 +
953 (int64_t)c2 * 10 ) * 1000;
955 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
956 if( p_sys->i_type == SUB_TYPE_SSA1 )
958 sprintf( p_subtitle->psz_text,
959 ",%d%s", i_dummy, strdup( buffer_text) );
963 sprintf( p_subtitle->psz_text,
964 ",%d,%s", i_dummy, strdup( buffer_text) );
966 p_subtitle->i_start = i_start;
967 p_subtitle->i_stop = i_stop;
972 /* All the other stuff we add to the header field */
973 if( p_sys->psz_header != NULL )
975 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
976 strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
978 msg_Err( p_demux, "out of memory");
981 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
982 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
986 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
988 msg_Err( p_demux, "out of memory");
991 p_sys->psz_header = strdup( s );
992 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
998 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1000 demux_sys_t *p_sys = p_demux->p_sys;
1001 text_t *txt = &p_sys->txt;
1005 * h:m:s:Line1|Line2|Line3....
1007 * h:m:s Line1|Line2|Line3....
1011 char buffer_text[MAX_LINE + 1];
1015 p_subtitle->i_start = 0;
1016 p_subtitle->i_stop = 0;
1017 p_subtitle->psz_text = NULL;
1024 if( ( p = TextGetLine( txt ) ) == NULL )
1026 return( VLC_EGENERIC );
1031 memset( buffer_text, '\0', MAX_LINE );
1032 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1034 i_start = ( (int64_t)h * 3600*1000 +
1035 (int64_t)m * 60*1000 +
1036 (int64_t)s * 1000 ) * 1000;
1041 /* replace | by \n */
1042 for( i = 0; i < strlen( buffer_text ); i++ )
1044 if( buffer_text[i] == '|' )
1046 buffer_text[i] = '\n';
1049 p_subtitle->i_start = i_start;
1051 p_subtitle->i_stop = 0;
1052 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1056 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1060 if( strcasestr( psz_start, psz_str ) )
1062 char *s = strcasestr( psz_start, psz_str );
1064 s += strlen( psz_str );
1072 if( ( p = TextGetLine( txt ) ) == NULL )
1076 if( strcasestr( p, psz_str ) )
1078 char *s = strcasestr( p, psz_str );
1080 s += strlen( psz_str );
1087 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1089 demux_sys_t *p_sys = p_demux->p_sys;
1090 text_t *txt = &p_sys->txt;
1096 char buffer_text[10*MAX_LINE + 1];
1098 p_subtitle->i_start = 0;
1099 p_subtitle->i_stop = 0;
1100 p_subtitle->psz_text = NULL;
1103 if( i_text < 10*MAX_LINE ) \
1105 buffer_text[i_text++] = c; \
1106 buffer_text[i_text] = '\0'; \
1109 /* search "Start=" */
1110 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1112 return VLC_EGENERIC;
1115 /* get start value */
1116 i_start = strtol( p, &p, 0 );
1119 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1121 return VLC_EGENERIC;
1124 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1126 return VLC_EGENERIC;
1130 buffer_text[0] = '\0';
1131 /* now get all txt until a "Start=" line */
1138 if( !strncasecmp( p, "<br", 3 ) )
1142 else if( strcasestr( p, "Start=" ) )
1144 TextPreviousLine( txt );
1147 p = ParseSamiSearch( txt, p, ">" );
1149 else if( !strncmp( p, " ", 6 ) )
1154 else if( *p == '\t' )
1167 p = TextGetLine( txt );
1176 p_subtitle->i_start = i_start * 1000;
1177 p_subtitle->i_stop = 0;
1178 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1180 return( VLC_SUCCESS );