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 #include <sys/types.h>
35 #include <vlc/input.h>
36 #include "vlc_video.h"
39 #if (!defined( WIN32 ) || defined(__MINGW32__))
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t *p_this );
47 static void Close( vlc_object_t *p_this );
49 #define SUB_DELAY_LONGTEXT \
50 "Delay subtitles (in 1/10s)"
51 #define SUB_FPS_LONGTEXT \
52 "Override frames per second. " \
53 "It will only work with MicroDVD subtitles."
54 #define SUB_TYPE_LONGTEXT \
55 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
56 "\"sami\" (auto for autodetection, it should always work)."
57 static char *ppsz_sub_type[] =
59 "auto", "microdvd", "subrip", "subviewer", "ssa1",
60 "ssa2-4", "vplayer", "sami"
64 set_description( _("Text subtitles demux") );
65 set_capability( "demux2", 0 );
66 add_float( "sub-fps", 0.0, NULL,
67 N_("Frames per second"),
68 SUB_FPS_LONGTEXT, VLC_TRUE );
69 add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
70 SUB_TYPE_LONGTEXT, VLC_TRUE );
71 change_string_list( ppsz_sub_type, 0, 0 );
72 set_callbacks( Open, Close );
74 add_shortcut( "subtitle" );
77 /*****************************************************************************
79 *****************************************************************************/
82 SUB_TYPE_UNKNOWN = -1,
98 static int TextLoad( text_t *, stream_t *s );
99 static void TextUnload( text_t * );
116 int64_t i_next_demux_date;
118 int64_t i_microsecperframe;
123 subtitle_t *subtitle;
124 mtime_t i_original_mspf;
129 static int ParseMicroDvd ( demux_t *, subtitle_t * );
130 static int ParseSubRip ( demux_t *, subtitle_t * );
131 static int ParseSubViewer( demux_t *, subtitle_t * );
132 static int ParseSSA ( demux_t *, subtitle_t * );
133 static int ParseVplayer ( demux_t *, subtitle_t * );
134 static int ParseSami ( demux_t *, subtitle_t * );
141 int (*pf_read)( demux_t *, subtitle_t* );
142 } sub_read_subtitle_function [] =
144 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
145 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
146 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer",ParseSubViewer },
147 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
148 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4",ParseSSA },
149 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
150 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
151 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
154 static int Demux( demux_t * );
155 static int Control( demux_t *, int, va_list );
157 static void Fix( demux_t * );
159 /*****************************************************************************
161 *****************************************************************************/
162 static int Open ( vlc_object_t *p_this )
164 demux_t *p_demux = (demux_t*)p_this;
169 int (*pf_read)( demux_t *, subtitle_t* );
172 if( strcmp( p_demux->psz_demux, "subtitle" ) )
174 msg_Dbg( p_demux, "subtitle demux discarded" );
178 p_demux->pf_demux = Demux;
179 p_demux->pf_control = Control;
180 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
181 p_sys->psz_header = NULL;
182 p_sys->i_subtitle = 0;
183 p_sys->i_subtitles= 0;
184 p_sys->subtitle = NULL;
185 p_sys->i_original_mspf = 0;
189 p_sys->i_microsecperframe = 40000; /* default to 25 fps */
190 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
193 p_sys->i_microsecperframe = (mtime_t)( (float)1000000 / f_fps );
196 /* Get or probe the type */
197 p_sys->i_type = SUB_TYPE_UNKNOWN;
198 psz_type = var_CreateGetString( p_demux, "sub-type" );
205 if( sub_read_subtitle_function[i].psz_type_name == NULL )
208 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
211 p_sys->i_type = sub_read_subtitle_function[i].i_type;
218 /* Probe if unknown type */
219 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
224 msg_Dbg( p_demux, "autodetecting subtitle format" );
225 for( i_try = 0; i_try < 256; i_try++ )
229 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
232 if( strcasestr( s, "<SAMI>" ) )
234 p_sys->i_type = SUB_TYPE_SAMI;
237 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
238 sscanf( s, "{%d}{}", &i_dummy ) == 1)
240 p_sys->i_type = SUB_TYPE_MICRODVD;
244 "%d:%d:%d,%d --> %d:%d:%d,%d",
245 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
246 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
248 p_sys->i_type = SUB_TYPE_SUBRIP;
252 "!: This is a Sub Station Alpha v%d.x script.",
257 p_sys->i_type = SUB_TYPE_SSA1;
261 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
265 else if( strcasestr( s, "This is a Sub Station Alpha v4 script" ) )
267 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
270 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
272 p_sys->i_type = SUB_TYPE_SSA2_4; /* could be wrong */
275 else if( strcasestr( s, "[INFORMATION]" ) )
277 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
280 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
281 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
283 p_sys->i_type = SUB_TYPE_VPLAYER;
293 /* It will nearly always work even for non seekable stream thanks the
294 * caching system, and if it fails we loose just a few sub */
295 if( stream_Seek( p_demux->s, 0 ) )
297 msg_Warn( p_demux, "failed to rewind" );
300 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
302 msg_Err( p_demux, "failed to recognize subtitle type" );
308 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
310 msg_Dbg( p_demux, "detected %s format",
311 sub_read_subtitle_function[i].psz_name );
312 pf_read = sub_read_subtitle_function[i].pf_read;
317 msg_Dbg( p_demux, "loading all subtitles..." );
319 /* Load the whole file */
320 TextLoad( &p_sys->txt, p_demux->s );
325 if( p_sys->i_subtitles >= i_max )
328 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
329 sizeof(subtitle_t) * i_max ) ) )
331 msg_Err( p_demux, "out of memory");
336 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
339 p_sys->i_subtitles++;
342 TextUnload( &p_sys->txt );
344 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
346 /* Fix subtitle (order and time) *** */
347 p_sys->i_subtitle = 0;
349 if( p_sys->i_subtitles > 0 )
351 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
353 if( p_sys->i_length <= 0 )
354 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
357 /* *** add subtitle ES *** */
358 if( p_sys->i_type == SUB_TYPE_SSA1 ||
359 p_sys->i_type == SUB_TYPE_SSA2_4 )
361 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
365 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
367 if( p_sys->psz_header != NULL )
369 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
370 fmt.p_extra = strdup( p_sys->psz_header );
372 p_sys->es = es_out_Add( p_demux->out, &fmt );
377 /*****************************************************************************
378 * Close: Close subtitle demux
379 *****************************************************************************/
380 static void Close( vlc_object_t *p_this )
382 demux_t *p_demux = (demux_t*)p_this;
383 demux_sys_t *p_sys = p_demux->p_sys;
386 for( i = 0; i < p_sys->i_subtitles; i++ )
388 if( p_sys->subtitle[i].psz_text )
389 free( p_sys->subtitle[i].psz_text );
391 if( p_sys->subtitle )
392 free( p_sys->subtitle );
397 /*****************************************************************************
399 *****************************************************************************/
400 static int Control( demux_t *p_demux, int i_query, va_list args )
402 demux_sys_t *p_sys = p_demux->p_sys;
408 case DEMUX_GET_LENGTH:
409 pi64 = (int64_t*)va_arg( args, int64_t * );
410 *pi64 = p_sys->i_length;
414 pi64 = (int64_t*)va_arg( args, int64_t * );
415 if( p_sys->i_subtitle < p_sys->i_subtitles )
417 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
423 i64 = (int64_t)va_arg( args, int64_t );
424 p_sys->i_subtitle = 0;
425 while( p_sys->i_subtitle < p_sys->i_subtitles &&
426 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
431 if( p_sys->i_subtitle >= p_sys->i_subtitles )
435 case DEMUX_GET_POSITION:
436 pf = (double*)va_arg( args, double * );
437 if( p_sys->i_subtitle >= p_sys->i_subtitles )
441 else if( p_sys->i_subtitles > 0 )
443 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
444 (double)p_sys->i_length;
452 case DEMUX_SET_POSITION:
453 f = (double)va_arg( args, double );
454 i64 = f * p_sys->i_length;
456 p_sys->i_subtitle = 0;
457 while( p_sys->i_subtitle < p_sys->i_subtitles &&
458 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
462 if( p_sys->i_subtitle >= p_sys->i_subtitles )
466 case DEMUX_SET_NEXT_DEMUX_TIME:
467 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
472 case DEMUX_GET_TITLE_INFO:
476 msg_Err( p_demux, "unknown query in subtitle control" );
481 /*****************************************************************************
482 * Demux: Send subtitle to decoder
483 *****************************************************************************/
484 static int Demux( demux_t *p_demux )
486 demux_sys_t *p_sys = p_demux->p_sys;
489 if( p_sys->i_subtitle >= p_sys->i_subtitles )
492 i_maxdate = p_sys->i_next_demux_date;
493 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
495 /* Should not happen */
496 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
499 while( p_sys->i_subtitle < p_sys->i_subtitles &&
500 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
503 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
512 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
518 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
524 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
525 p_block->i_dts = p_block->i_pts;
526 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
529 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
532 memcpy( p_block->p_buffer,
533 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
534 if( p_block->i_pts > 0 )
536 es_out_Send( p_demux->out, p_sys->es, p_block );
540 block_Release( p_block );
546 p_sys->i_next_demux_date = 0;
551 /*****************************************************************************
552 * Fix: fix time stamp and order of subtitle
553 *****************************************************************************/
554 static void Fix( demux_t *p_demux )
556 demux_sys_t *p_sys = p_demux->p_sys;
560 /* *** fix order (to be sure...) *** */
561 /* We suppose that there are near in order and this durty bubble sort
562 * wont take too much time
567 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
569 if( p_sys->subtitle[i_index].i_start <
570 p_sys->subtitle[i_index - 1].i_start )
574 p_sys->subtitle + i_index - 1,
575 sizeof( subtitle_t ) );
576 memcpy( p_sys->subtitle + i_index - 1,
577 p_sys->subtitle + i_index,
578 sizeof( subtitle_t ) );
579 memcpy( p_sys->subtitle + i_index,
581 sizeof( subtitle_t ) );
588 static int TextLoad( text_t *txt, stream_t *s )
594 txt->i_line_count = 0;
596 txt->line = calloc( i_line_max, sizeof( char * ) );
598 /* load the complete file */
601 char *psz = stream_ReadLine( s );
606 txt->line[txt->i_line_count++] = psz;
607 if( txt->i_line_count >= i_line_max )
610 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
614 if( txt->i_line_count <= 0 )
622 static void TextUnload( text_t *txt )
626 for( i = 0; i < txt->i_line_count; i++ )
628 free( txt->line[i] );
632 txt->i_line_count = 0;
635 static char *TextGetLine( text_t *txt )
637 if( txt->i_line >= txt->i_line_count )
640 return txt->line[txt->i_line++];
642 static void TextPreviousLine( text_t *txt )
644 if( txt->i_line > 0 )
648 /*****************************************************************************
649 * Specific Subtitle function
650 *****************************************************************************/
651 #define MAX_LINE 8192
652 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
654 demux_sys_t *p_sys = p_demux->p_sys;
655 text_t *txt = &p_sys->txt;
658 * {n1}{n2}Line1|Line2|Line3....
659 * where n1 and n2 are the video frame number...
664 char buffer_text[MAX_LINE + 1];
665 unsigned int i_start;
669 p_subtitle->i_start = 0;
670 p_subtitle->i_stop = 0;
671 p_subtitle->psz_text = NULL;
675 if( ( s = TextGetLine( txt ) ) == NULL )
677 return( VLC_EGENERIC );
682 memset( buffer_text, '\0', MAX_LINE );
683 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
684 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
689 /* replace | by \n */
690 for( i = 0; i < strlen( buffer_text ); i++ )
692 if( buffer_text[i] == '|' )
694 buffer_text[i] = '\n';
698 p_subtitle->i_start = (mtime_t)i_start * p_sys->i_microsecperframe;
699 p_subtitle->i_stop = (mtime_t)i_stop * p_sys->i_microsecperframe;
700 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
704 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
706 demux_sys_t *p_sys = p_demux->p_sys;
707 text_t *txt = &p_sys->txt;
711 * h1:m1:s1,d1 --> h2:m2:s2,d2
719 char buffer_text[ 10 * MAX_LINE];
724 p_subtitle->i_start = 0;
725 p_subtitle->i_stop = 0;
726 p_subtitle->psz_text = NULL;
730 int h1, m1, s1, d1, h2, m2, s2, d2;
731 if( ( s = TextGetLine( txt ) ) == NULL )
733 return( VLC_EGENERIC );
736 "%d:%d:%d,%d --> %d:%d:%d,%d",
738 &h2, &m2, &s2, &d2 ) == 8 )
740 i_start = ( (mtime_t)h1 * 3600*1000 +
741 (mtime_t)m1 * 60*1000 +
743 (mtime_t)d1 ) * 1000;
745 i_stop = ( (mtime_t)h2 * 3600*1000 +
746 (mtime_t)m2 * 60*1000 +
748 (mtime_t)d2 ) * 1000;
750 /* Now read text until an empty line */
751 for( i_buffer_text = 0;; )
754 if( ( s = TextGetLine( txt ) ) == NULL )
756 return( VLC_EGENERIC );
762 /* empty line -> end of this subtitle */
763 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
764 p_subtitle->i_start = i_start;
765 p_subtitle->i_stop = i_stop;
766 p_subtitle->psz_text = strdup( buffer_text );
767 /* If framerate is available, use sub-fps */
768 if( p_sys->i_microsecperframe != 0 &&
769 p_sys->i_original_mspf != 0)
771 p_subtitle->i_start = (mtime_t)i_start *
772 p_sys->i_original_mspf /
773 p_sys->i_microsecperframe;
774 p_subtitle->i_stop = (mtime_t)i_stop *
775 p_sys->i_original_mspf /
776 p_sys->i_microsecperframe;
782 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
784 memcpy( buffer_text + i_buffer_text,
787 i_buffer_text += i_len;
789 buffer_text[i_buffer_text] = '\n';
798 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
800 demux_sys_t *p_sys = p_demux->p_sys;
801 text_t *txt = &p_sys->txt;
804 * h1:m1:s1.d1,h2:m2:s2.d2
809 * ( works with subviewer and subviewer v2 )
812 char buffer_text[ 10 * MAX_LINE];
817 p_subtitle->i_start = 0;
818 p_subtitle->i_stop = 0;
819 p_subtitle->psz_text = NULL;
823 int h1, m1, s1, d1, h2, m2, s2, d2;
824 if( ( s = TextGetLine( txt ) ) == NULL )
826 return( VLC_EGENERIC );
829 "%d:%d:%d.%d,%d:%d:%d.%d",
831 &h2, &m2, &s2, &d2 ) == 8 )
833 i_start = ( (mtime_t)h1 * 3600*1000 +
834 (mtime_t)m1 * 60*1000 +
836 (mtime_t)d1 ) * 1000;
838 i_stop = ( (mtime_t)h2 * 3600*1000 +
839 (mtime_t)m2 * 60*1000 +
841 (mtime_t)d2 ) * 1000;
843 /* Now read text until an empty line */
844 for( i_buffer_text = 0;; )
847 if( ( s = TextGetLine( txt ) ) == NULL )
849 return( VLC_EGENERIC );
855 /* empty line -> end of this subtitle */
856 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
857 p_subtitle->i_start = i_start;
858 p_subtitle->i_stop = i_stop;
860 /* replace [br] by \n */
861 for( i = 0; i < i_buffer_text - 3; i++ )
863 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
864 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
866 char *temp = buffer_text + i + 1;
867 buffer_text[i] = '\n';
868 memmove( temp, temp+3, strlen( temp ) -3 );
869 temp[strlen( temp )-3] = '\0';
872 p_subtitle->psz_text = strdup( buffer_text );
877 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
879 memcpy( buffer_text + i_buffer_text,
882 i_buffer_text += i_len;
884 buffer_text[i_buffer_text] = '\n';
894 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
896 demux_sys_t *p_sys = p_demux->p_sys;
897 text_t *txt = &p_sys->txt;
899 char buffer_text[ 10 * MAX_LINE];
904 p_subtitle->i_start = 0;
905 p_subtitle->i_stop = 0;
906 p_subtitle->psz_text = NULL;
910 int h1, m1, s1, c1, h2, m2, s2, c2;
913 if( ( s = TextGetLine( txt ) ) == NULL )
915 return( VLC_EGENERIC );
917 p_subtitle->psz_text = malloc( strlen( s ) );
920 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
924 buffer_text ) == 10 )
926 i_start = ( (mtime_t)h1 * 3600*1000 +
927 (mtime_t)m1 * 60*1000 +
929 (mtime_t)c1 * 10 ) * 1000;
931 i_stop = ( (mtime_t)h2 * 3600*1000 +
932 (mtime_t)m2 * 60*1000 +
934 (mtime_t)c2 * 10 ) * 1000;
936 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
937 if( p_sys->i_type == SUB_TYPE_SSA1 )
939 sprintf( p_subtitle->psz_text,
940 ",%d%s", i_dummy, strdup( buffer_text) );
944 sprintf( p_subtitle->psz_text,
945 ",%d,%s", i_dummy, strdup( buffer_text) );
947 p_subtitle->i_start = i_start;
948 p_subtitle->i_stop = i_stop;
953 /* All the other stuff we add to the header field */
954 if( p_sys->psz_header != NULL )
956 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
957 strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
959 msg_Err( p_demux, "out of memory");
962 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
963 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
967 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
969 msg_Err( p_demux, "out of memory");
972 p_sys->psz_header = strdup( s );
973 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
979 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
981 demux_sys_t *p_sys = p_demux->p_sys;
982 text_t *txt = &p_sys->txt;
986 * h:m:s:Line1|Line2|Line3....
988 * h:m:s Line1|Line2|Line3....
992 char buffer_text[MAX_LINE + 1];
996 p_subtitle->i_start = 0;
997 p_subtitle->i_stop = 0;
998 p_subtitle->psz_text = NULL;
1005 if( ( p = TextGetLine( txt ) ) == NULL )
1007 return( VLC_EGENERIC );
1012 memset( buffer_text, '\0', MAX_LINE );
1013 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1015 i_start = ( (mtime_t)h * 3600*1000 +
1016 (mtime_t)m * 60*1000 +
1017 (mtime_t)s * 1000 ) * 1000;
1022 /* replace | by \n */
1023 for( i = 0; i < strlen( buffer_text ); i++ )
1025 if( buffer_text[i] == '|' )
1027 buffer_text[i] = '\n';
1030 p_subtitle->i_start = i_start;
1032 p_subtitle->i_stop = 0;
1033 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1037 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1041 if( strcasestr( psz_start, psz_str ) )
1043 char *s = strcasestr( psz_start, psz_str );
1045 s += strlen( psz_str );
1053 if( ( p = TextGetLine( txt ) ) == NULL )
1057 if( strcasestr( p, psz_str ) )
1059 char *s = strcasestr( p, psz_str );
1061 s += strlen( psz_str );
1068 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1070 demux_sys_t *p_sys = p_demux->p_sys;
1071 text_t *txt = &p_sys->txt;
1077 char buffer_text[10*MAX_LINE + 1];
1079 p_subtitle->i_start = 0;
1080 p_subtitle->i_stop = 0;
1081 p_subtitle->psz_text = NULL;
1084 if( i_text < 10*MAX_LINE ) \
1086 buffer_text[i_text++] = c; \
1087 buffer_text[i_text] = '\0'; \
1090 /* search "Start=" */
1091 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1093 return VLC_EGENERIC;
1096 /* get start value */
1097 i_start = strtol( p, &p, 0 );
1100 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1102 return VLC_EGENERIC;
1105 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1107 return VLC_EGENERIC;
1111 buffer_text[0] = '\0';
1112 /* now get all txt until a "Start=" line */
1119 if( !strncasecmp( p, "<br", 3 ) )
1123 else if( strcasestr( p, "Start=" ) )
1125 TextPreviousLine( txt );
1128 p = ParseSamiSearch( txt, p, ">" );
1130 else if( !strncmp( p, " ", 6 ) )
1135 else if( *p == '\t' )
1148 p = TextGetLine( txt );
1157 p_subtitle->i_start = i_start * 1000;
1158 p_subtitle->i_stop = 0;
1159 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1161 return( VLC_SUCCESS );