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"
41 #if (!defined( WIN32 ) || defined(__MINGW32__))
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 "Delay subtitles (in 1/10s)"
53 #define SUB_FPS_LONGTEXT \
54 "Override frames per second. " \
55 "It will only work with MicroDVD subtitles."
56 #define SUB_TYPE_LONGTEXT \
57 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
58 "\"sami\" (auto for autodetection, it should always work)."
59 static char *ppsz_sub_type[] =
61 "auto", "microdvd", "subrip", "subviewer", "ssa1",
62 "ssa2-4", "vplayer", "sami"
66 set_description( _("Text subtitles demux") );
67 set_capability( "demux2", 0 );
68 add_float( "sub-fps", 0.0, NULL,
69 N_("Frames per second"),
70 SUB_FPS_LONGTEXT, VLC_TRUE );
71 add_integer( "sub-delay", 0, NULL,
72 N_("Subtitles delay"),
73 SUB_DELAY_LONGTEXT, VLC_TRUE );
74 add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
75 SUB_TYPE_LONGTEXT, VLC_TRUE );
76 change_string_list( ppsz_sub_type, 0, 0 );
77 set_callbacks( Open, Close );
79 add_shortcut( "subtitle" );
82 /*****************************************************************************
84 *****************************************************************************/
87 SUB_TYPE_UNKNOWN = -1,
103 static int TextLoad( text_t *, stream_t *s );
104 static void TextUnload( text_t * );
121 int64_t i_next_demux_date;
123 int64_t i_microsecperframe;
124 mtime_t i_original_mspf;
129 subtitle_t *subtitle;
134 static int ParseMicroDvd ( demux_t *, subtitle_t * );
135 static int ParseSubRip ( demux_t *, subtitle_t * );
136 static int ParseSubViewer( demux_t *, subtitle_t * );
137 static int ParseSSA ( demux_t *, subtitle_t * );
138 static int ParseVplayer ( demux_t *, subtitle_t * );
139 static int ParseSami ( demux_t *, subtitle_t * );
146 int (*pf_read)( demux_t *, subtitle_t* );
147 } sub_read_subtitle_function [] =
149 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
150 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
151 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer",ParseSubViewer },
152 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
153 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4",ParseSSA },
154 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
155 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
156 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
159 static int Demux( demux_t * );
160 static int Control( demux_t *, int, va_list );
162 static void Fix( demux_t * );
164 /*****************************************************************************
166 *****************************************************************************/
167 static int Open ( vlc_object_t *p_this )
169 demux_t *p_demux = (demux_t*)p_this;
174 int (*pf_read)( demux_t *, subtitle_t* );
177 if( strcmp( p_demux->psz_demux, "subtitle" ) )
179 msg_Dbg( p_demux, "subtitle demux discarded" );
183 p_demux->pf_demux = Demux;
184 p_demux->pf_control = Control;
185 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
186 p_sys->psz_header = NULL;
187 p_sys->i_subtitle = 0;
188 p_sys->i_subtitles= 0;
189 p_sys->subtitle = NULL;
193 p_sys->i_microsecperframe = 40000; /* default to 25 fps */
194 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
197 p_sys->i_microsecperframe = (mtime_t)( (float)1000000 / f_fps );
200 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
203 p_sys->i_original_mspf = (mtime_t)( (float)1000000 / f_fps );
207 p_sys->i_original_mspf = 0;
210 /* Get or probe the type */
211 p_sys->i_type = SUB_TYPE_UNKNOWN;
212 psz_type = var_CreateGetString( p_demux, "sub-type" );
219 if( sub_read_subtitle_function[i].psz_type_name == NULL )
222 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
225 p_sys->i_type = sub_read_subtitle_function[i].i_type;
232 /* Probe if unknown type */
233 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
238 msg_Dbg( p_demux, "autodetecting subtitle format" );
239 for( i_try = 0; i_try < 256; i_try++ )
243 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
246 if( strcasestr( s, "<SAMI>" ) )
248 p_sys->i_type = SUB_TYPE_SAMI;
251 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
252 sscanf( s, "{%d}{}", &i_dummy ) == 1)
254 p_sys->i_type = SUB_TYPE_MICRODVD;
258 "%d:%d:%d,%d --> %d:%d:%d,%d",
259 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
260 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
262 p_sys->i_type = SUB_TYPE_SUBRIP;
266 "!: This is a Sub Station Alpha v%d.x script.",
271 p_sys->i_type = SUB_TYPE_SSA1;
275 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
279 else if( strcasestr( s, "This is a Sub Station Alpha v4 script" ) )
281 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
284 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
286 p_sys->i_type = SUB_TYPE_SSA2_4; /* could be wrong */
289 else if( strcasestr( s, "[INFORMATION]" ) )
291 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
294 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
295 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
297 p_sys->i_type = SUB_TYPE_VPLAYER;
307 /* It will nearly always work even for non seekable stream thanks the
308 * caching system, and if it fails we loose just a few sub */
309 if( stream_Seek( p_demux->s, 0 ) )
311 msg_Warn( p_demux, "failed to rewind" );
314 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
316 msg_Err( p_demux, "failed to recognize subtitle type" );
322 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
324 msg_Dbg( p_demux, "detected %s format",
325 sub_read_subtitle_function[i].psz_name );
326 pf_read = sub_read_subtitle_function[i].pf_read;
331 msg_Dbg( p_demux, "loading all subtitles..." );
333 /* Load the whole file */
334 TextLoad( &p_sys->txt, p_demux->s );
339 if( p_sys->i_subtitles >= i_max )
342 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
343 sizeof(subtitle_t) * i_max ) ) )
345 msg_Err( p_demux, "out of memory");
350 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
353 p_sys->i_subtitles++;
356 TextUnload( &p_sys->txt );
358 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
360 /* Fix subtitle (order and time) *** */
361 p_sys->i_subtitle = 0;
363 if( p_sys->i_subtitles > 0 )
365 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
367 if( p_sys->i_length <= 0 )
368 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
371 /* *** add subtitle ES *** */
372 if( p_sys->i_type == SUB_TYPE_SSA1 ||
373 p_sys->i_type == SUB_TYPE_SSA2_4 )
375 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
379 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
381 if( p_sys->psz_header != NULL )
383 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
384 fmt.p_extra = strdup( p_sys->psz_header );
386 p_sys->es = es_out_Add( p_demux->out, &fmt );
391 /*****************************************************************************
392 * Close: Close subtitle demux
393 *****************************************************************************/
394 static void Close( vlc_object_t *p_this )
396 demux_t *p_demux = (demux_t*)p_this;
397 demux_sys_t *p_sys = p_demux->p_sys;
400 for( i = 0; i < p_sys->i_subtitles; i++ )
402 if( p_sys->subtitle[i].psz_text )
403 free( p_sys->subtitle[i].psz_text );
405 if( p_sys->subtitle )
406 free( p_sys->subtitle );
411 /*****************************************************************************
413 *****************************************************************************/
414 static int Control( demux_t *p_demux, int i_query, va_list args )
416 demux_sys_t *p_sys = p_demux->p_sys;
422 case DEMUX_GET_LENGTH:
423 pi64 = (int64_t*)va_arg( args, int64_t * );
424 *pi64 = p_sys->i_length;
428 pi64 = (int64_t*)va_arg( args, int64_t * );
429 if( p_sys->i_subtitle < p_sys->i_subtitles )
431 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
437 i64 = (int64_t)va_arg( args, int64_t );
438 p_sys->i_subtitle = 0;
439 while( p_sys->i_subtitle < p_sys->i_subtitles &&
440 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
445 if( p_sys->i_subtitle >= p_sys->i_subtitles )
449 case DEMUX_GET_POSITION:
450 pf = (double*)va_arg( args, double * );
451 if( p_sys->i_subtitle >= p_sys->i_subtitles )
455 else if( p_sys->i_subtitles > 0 )
457 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
458 (double)p_sys->i_length;
466 case DEMUX_SET_POSITION:
467 f = (double)va_arg( args, double );
468 i64 = f * p_sys->i_length;
470 p_sys->i_subtitle = 0;
471 while( p_sys->i_subtitle < p_sys->i_subtitles &&
472 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
476 if( p_sys->i_subtitle >= p_sys->i_subtitles )
480 case DEMUX_SET_NEXT_DEMUX_TIME:
481 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
486 case DEMUX_GET_TITLE_INFO:
490 msg_Err( p_demux, "unknown query in subtitle control" );
495 /*****************************************************************************
496 * Demux: Send subtitle to decoder
497 *****************************************************************************/
498 static int Demux( demux_t *p_demux )
500 demux_sys_t *p_sys = p_demux->p_sys;
503 if( p_sys->i_subtitle >= p_sys->i_subtitles )
506 i_maxdate = p_sys->i_next_demux_date;
507 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
509 /* Should not happen */
510 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
513 while( p_sys->i_subtitle < p_sys->i_subtitles &&
514 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
517 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
526 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
532 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
538 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
539 p_block->i_dts = p_block->i_pts;
540 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
543 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
546 memcpy( p_block->p_buffer,
547 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
548 if( p_block->i_pts > 0 )
550 es_out_Send( p_demux->out, p_sys->es, p_block );
554 block_Release( p_block );
560 p_sys->i_next_demux_date = 0;
565 /*****************************************************************************
566 * Fix: fix time stamp and order of subtitle
567 *****************************************************************************/
568 static void Fix( demux_t *p_demux )
570 demux_sys_t *p_sys = p_demux->p_sys;
574 /* *** fix order (to be sure...) *** */
575 /* We suppose that there are near in order and this durty bubble sort
576 * wont take too much time
581 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
583 if( p_sys->subtitle[i_index].i_start <
584 p_sys->subtitle[i_index - 1].i_start )
588 p_sys->subtitle + i_index - 1,
589 sizeof( subtitle_t ) );
590 memcpy( p_sys->subtitle + i_index - 1,
591 p_sys->subtitle + i_index,
592 sizeof( subtitle_t ) );
593 memcpy( p_sys->subtitle + i_index,
595 sizeof( subtitle_t ) );
602 static int TextLoad( text_t *txt, stream_t *s )
608 txt->i_line_count = 0;
610 txt->line = calloc( i_line_max, sizeof( char * ) );
612 /* load the complete file */
615 char *psz = stream_ReadLine( s );
620 txt->line[txt->i_line_count++] = psz;
621 if( txt->i_line_count >= i_line_max )
624 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
628 if( txt->i_line_count <= 0 )
636 static void TextUnload( text_t *txt )
640 for( i = 0; i < txt->i_line_count; i++ )
642 free( txt->line[i] );
646 txt->i_line_count = 0;
649 static char *TextGetLine( text_t *txt )
651 if( txt->i_line >= txt->i_line_count )
654 return txt->line[txt->i_line++];
656 static void TextPreviousLine( text_t *txt )
658 if( txt->i_line > 0 )
662 /*****************************************************************************
663 * Specific Subtitle function
664 *****************************************************************************/
665 #define MAX_LINE 8192
666 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
668 demux_sys_t *p_sys = p_demux->p_sys;
669 text_t *txt = &p_sys->txt;
672 * {n1}{n2}Line1|Line2|Line3....
673 * where n1 and n2 are the video frame number...
678 char buffer_text[MAX_LINE + 1];
679 unsigned int i_start;
683 p_subtitle->i_start = 0;
684 p_subtitle->i_stop = 0;
685 p_subtitle->psz_text = NULL;
689 if( ( s = TextGetLine( txt ) ) == NULL )
691 return( VLC_EGENERIC );
696 memset( buffer_text, '\0', MAX_LINE );
697 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
698 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
703 /* replace | by \n */
704 for( i = 0; i < strlen( buffer_text ); i++ )
706 if( buffer_text[i] == '|' )
708 buffer_text[i] = '\n';
712 p_subtitle->i_start = (mtime_t)i_start * p_sys->i_microsecperframe;
713 p_subtitle->i_stop = (mtime_t)i_stop * p_sys->i_microsecperframe;
714 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
718 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
720 demux_sys_t *p_sys = p_demux->p_sys;
721 text_t *txt = &p_sys->txt;
725 * h1:m1:s1,d1 --> h2:m2:s2,d2
733 char buffer_text[ 10 * MAX_LINE];
738 p_subtitle->i_start = 0;
739 p_subtitle->i_stop = 0;
740 p_subtitle->psz_text = NULL;
744 int h1, m1, s1, d1, h2, m2, s2, d2;
745 if( ( s = TextGetLine( txt ) ) == NULL )
747 return( VLC_EGENERIC );
750 "%d:%d:%d,%d --> %d:%d:%d,%d",
752 &h2, &m2, &s2, &d2 ) == 8 )
754 i_start = ( (mtime_t)h1 * 3600*1000 +
755 (mtime_t)m1 * 60*1000 +
757 (mtime_t)d1 ) * 1000;
759 i_stop = ( (mtime_t)h2 * 3600*1000 +
760 (mtime_t)m2 * 60*1000 +
762 (mtime_t)d2 ) * 1000;
764 /* Now read text until an empty line */
765 for( i_buffer_text = 0;; )
768 if( ( s = TextGetLine( txt ) ) == NULL )
770 return( VLC_EGENERIC );
776 /* empty line -> end of this subtitle */
777 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
778 p_subtitle->i_start = i_start;
779 p_subtitle->i_stop = i_stop;
780 p_subtitle->psz_text = strdup( buffer_text );
781 /* If framerate is available, use sub-fps */
782 if( p_sys->i_microsecperframe != 0 &&
783 p_sys->i_original_mspf != 0)
785 p_subtitle->i_start = (mtime_t)i_start *
786 p_sys->i_microsecperframe/
787 p_sys->i_original_mspf;
788 p_subtitle->i_stop = (mtime_t)i_stop *
789 p_sys->i_microsecperframe /
790 p_sys->i_original_mspf;
796 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
798 memcpy( buffer_text + i_buffer_text,
801 i_buffer_text += i_len;
803 buffer_text[i_buffer_text] = '\n';
812 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
814 demux_sys_t *p_sys = p_demux->p_sys;
815 text_t *txt = &p_sys->txt;
818 * h1:m1:s1.d1,h2:m2:s2.d2
823 * ( works with subviewer and subviewer v2 )
826 char buffer_text[ 10 * MAX_LINE];
831 p_subtitle->i_start = 0;
832 p_subtitle->i_stop = 0;
833 p_subtitle->psz_text = NULL;
837 int h1, m1, s1, d1, h2, m2, s2, d2;
838 if( ( s = TextGetLine( txt ) ) == NULL )
840 return( VLC_EGENERIC );
843 "%d:%d:%d.%d,%d:%d:%d.%d",
845 &h2, &m2, &s2, &d2 ) == 8 )
847 i_start = ( (mtime_t)h1 * 3600*1000 +
848 (mtime_t)m1 * 60*1000 +
850 (mtime_t)d1 ) * 1000;
852 i_stop = ( (mtime_t)h2 * 3600*1000 +
853 (mtime_t)m2 * 60*1000 +
855 (mtime_t)d2 ) * 1000;
857 /* Now read text until an empty line */
858 for( i_buffer_text = 0;; )
861 if( ( s = TextGetLine( txt ) ) == NULL )
863 return( VLC_EGENERIC );
869 /* empty line -> end of this subtitle */
870 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
871 p_subtitle->i_start = i_start;
872 p_subtitle->i_stop = i_stop;
874 /* replace [br] by \n */
875 for( i = 0; i < i_buffer_text - 3; i++ )
877 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
878 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
880 char *temp = buffer_text + i + 1;
881 buffer_text[i] = '\n';
882 memmove( temp, temp+3, strlen( temp ) -3 );
883 temp[strlen( temp )-3] = '\0';
886 p_subtitle->psz_text = strdup( buffer_text );
891 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
893 memcpy( buffer_text + i_buffer_text,
896 i_buffer_text += i_len;
898 buffer_text[i_buffer_text] = '\n';
908 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
910 demux_sys_t *p_sys = p_demux->p_sys;
911 text_t *txt = &p_sys->txt;
913 char buffer_text[ 10 * MAX_LINE];
918 p_subtitle->i_start = 0;
919 p_subtitle->i_stop = 0;
920 p_subtitle->psz_text = NULL;
924 int h1, m1, s1, c1, h2, m2, s2, c2;
927 if( ( s = TextGetLine( txt ) ) == NULL )
929 return( VLC_EGENERIC );
931 p_subtitle->psz_text = malloc( strlen( s ) );
934 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
938 buffer_text ) == 10 )
940 i_start = ( (mtime_t)h1 * 3600*1000 +
941 (mtime_t)m1 * 60*1000 +
943 (mtime_t)c1 * 10 ) * 1000;
945 i_stop = ( (mtime_t)h2 * 3600*1000 +
946 (mtime_t)m2 * 60*1000 +
948 (mtime_t)c2 * 10 ) * 1000;
950 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
951 if( p_sys->i_type == SUB_TYPE_SSA1 )
953 sprintf( p_subtitle->psz_text,
954 ",%d%s", i_dummy, strdup( buffer_text) );
958 sprintf( p_subtitle->psz_text,
959 ",%d,%s", i_dummy, strdup( buffer_text) );
961 p_subtitle->i_start = i_start;
962 p_subtitle->i_stop = i_stop;
967 /* All the other stuff we add to the header field */
968 if( p_sys->psz_header != NULL )
970 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
971 strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
973 msg_Err( p_demux, "out of memory");
976 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
977 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
981 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
983 msg_Err( p_demux, "out of memory");
986 p_sys->psz_header = strdup( s );
987 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
993 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
995 demux_sys_t *p_sys = p_demux->p_sys;
996 text_t *txt = &p_sys->txt;
1000 * h:m:s:Line1|Line2|Line3....
1002 * h:m:s Line1|Line2|Line3....
1006 char buffer_text[MAX_LINE + 1];
1010 p_subtitle->i_start = 0;
1011 p_subtitle->i_stop = 0;
1012 p_subtitle->psz_text = NULL;
1019 if( ( p = TextGetLine( txt ) ) == NULL )
1021 return( VLC_EGENERIC );
1026 memset( buffer_text, '\0', MAX_LINE );
1027 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1029 i_start = ( (mtime_t)h * 3600*1000 +
1030 (mtime_t)m * 60*1000 +
1031 (mtime_t)s * 1000 ) * 1000;
1036 /* replace | by \n */
1037 for( i = 0; i < strlen( buffer_text ); i++ )
1039 if( buffer_text[i] == '|' )
1041 buffer_text[i] = '\n';
1044 p_subtitle->i_start = i_start;
1046 p_subtitle->i_stop = 0;
1047 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1051 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1055 if( strcasestr( psz_start, psz_str ) )
1057 char *s = strcasestr( psz_start, psz_str );
1059 s += strlen( psz_str );
1067 if( ( p = TextGetLine( txt ) ) == NULL )
1071 if( strcasestr( p, psz_str ) )
1073 char *s = strcasestr( p, psz_str );
1075 s += strlen( psz_str );
1082 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1084 demux_sys_t *p_sys = p_demux->p_sys;
1085 text_t *txt = &p_sys->txt;
1091 char buffer_text[10*MAX_LINE + 1];
1093 p_subtitle->i_start = 0;
1094 p_subtitle->i_stop = 0;
1095 p_subtitle->psz_text = NULL;
1098 if( i_text < 10*MAX_LINE ) \
1100 buffer_text[i_text++] = c; \
1101 buffer_text[i_text] = '\0'; \
1104 /* search "Start=" */
1105 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1107 return VLC_EGENERIC;
1110 /* get start value */
1111 i_start = strtol( p, &p, 0 );
1114 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1116 return VLC_EGENERIC;
1119 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1121 return VLC_EGENERIC;
1125 buffer_text[0] = '\0';
1126 /* now get all txt until a "Start=" line */
1133 if( !strncasecmp( p, "<br", 3 ) )
1137 else if( strcasestr( p, "Start=" ) )
1139 TextPreviousLine( txt );
1142 p = ParseSamiSearch( txt, p, ">" );
1144 else if( !strncmp( p, " ", 6 ) )
1149 else if( *p == '\t' )
1162 p = TextGetLine( txt );
1171 p_subtitle->i_start = i_start * 1000;
1172 p_subtitle->i_stop = 0;
1173 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1175 return( VLC_SUCCESS );