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_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 mtime_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 p_sys->i_microsecperframe = 40000; /* default to 25 fps */
192 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
195 p_sys->i_microsecperframe = (mtime_t)( (float)1000000 / f_fps );
198 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
201 p_sys->i_original_mspf = (mtime_t)( (float)1000000 / f_fps );
205 p_sys->i_original_mspf = 0;
208 /* Get or probe the type */
209 p_sys->i_type = SUB_TYPE_UNKNOWN;
210 psz_type = var_CreateGetString( p_demux, "sub-type" );
217 if( sub_read_subtitle_function[i].psz_type_name == NULL )
220 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
223 p_sys->i_type = sub_read_subtitle_function[i].i_type;
230 /* Probe if unknown type */
231 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
236 msg_Dbg( p_demux, "autodetecting subtitle format" );
237 for( i_try = 0; i_try < 256; i_try++ )
241 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
244 if( strcasestr( s, "<SAMI>" ) )
246 p_sys->i_type = SUB_TYPE_SAMI;
249 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
250 sscanf( s, "{%d}{}", &i_dummy ) == 1)
252 p_sys->i_type = SUB_TYPE_MICRODVD;
256 "%d:%d:%d,%d --> %d:%d:%d,%d",
257 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
258 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
260 p_sys->i_type = SUB_TYPE_SUBRIP;
264 "!: This is a Sub Station Alpha v%d.x script.",
269 p_sys->i_type = SUB_TYPE_SSA1;
273 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
277 else if( strcasestr( s, "This is a Sub Station Alpha v4 script" ) )
279 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
282 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
284 p_sys->i_type = SUB_TYPE_SSA2_4; /* could be wrong */
287 else if( strcasestr( s, "[INFORMATION]" ) )
289 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
292 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
293 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
295 p_sys->i_type = SUB_TYPE_VPLAYER;
305 /* It will nearly always work even for non seekable stream thanks the
306 * caching system, and if it fails we loose just a few sub */
307 if( stream_Seek( p_demux->s, 0 ) )
309 msg_Warn( p_demux, "failed to rewind" );
312 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
314 msg_Err( p_demux, "failed to recognize subtitle type" );
320 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
322 msg_Dbg( p_demux, "detected %s format",
323 sub_read_subtitle_function[i].psz_name );
324 pf_read = sub_read_subtitle_function[i].pf_read;
329 msg_Dbg( p_demux, "loading all subtitles..." );
331 /* Load the whole file */
332 TextLoad( &p_sys->txt, p_demux->s );
337 if( p_sys->i_subtitles >= i_max )
340 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
341 sizeof(subtitle_t) * i_max ) ) )
343 msg_Err( p_demux, "out of memory");
348 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
351 p_sys->i_subtitles++;
354 TextUnload( &p_sys->txt );
356 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
358 /* Fix subtitle (order and time) *** */
359 p_sys->i_subtitle = 0;
361 if( p_sys->i_subtitles > 0 )
363 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
365 if( p_sys->i_length <= 0 )
366 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
369 /* *** add subtitle ES *** */
370 if( p_sys->i_type == SUB_TYPE_SSA1 ||
371 p_sys->i_type == SUB_TYPE_SSA2_4 )
373 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
377 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
379 if( p_sys->psz_header != NULL )
381 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
382 fmt.p_extra = strdup( p_sys->psz_header );
384 p_sys->es = es_out_Add( p_demux->out, &fmt );
389 /*****************************************************************************
390 * Close: Close subtitle demux
391 *****************************************************************************/
392 static void Close( vlc_object_t *p_this )
394 demux_t *p_demux = (demux_t*)p_this;
395 demux_sys_t *p_sys = p_demux->p_sys;
398 for( i = 0; i < p_sys->i_subtitles; i++ )
400 if( p_sys->subtitle[i].psz_text )
401 free( p_sys->subtitle[i].psz_text );
403 if( p_sys->subtitle )
404 free( p_sys->subtitle );
409 /*****************************************************************************
411 *****************************************************************************/
412 static int Control( demux_t *p_demux, int i_query, va_list args )
414 demux_sys_t *p_sys = p_demux->p_sys;
420 case DEMUX_GET_LENGTH:
421 pi64 = (int64_t*)va_arg( args, int64_t * );
422 *pi64 = p_sys->i_length;
426 pi64 = (int64_t*)va_arg( args, int64_t * );
427 if( p_sys->i_subtitle < p_sys->i_subtitles )
429 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
435 i64 = (int64_t)va_arg( args, int64_t );
436 p_sys->i_subtitle = 0;
437 while( p_sys->i_subtitle < p_sys->i_subtitles &&
438 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
443 if( p_sys->i_subtitle >= p_sys->i_subtitles )
447 case DEMUX_GET_POSITION:
448 pf = (double*)va_arg( args, double * );
449 if( p_sys->i_subtitle >= p_sys->i_subtitles )
453 else if( p_sys->i_subtitles > 0 )
455 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
456 (double)p_sys->i_length;
464 case DEMUX_SET_POSITION:
465 f = (double)va_arg( args, double );
466 i64 = f * p_sys->i_length;
468 p_sys->i_subtitle = 0;
469 while( p_sys->i_subtitle < p_sys->i_subtitles &&
470 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
474 if( p_sys->i_subtitle >= p_sys->i_subtitles )
478 case DEMUX_SET_NEXT_DEMUX_TIME:
479 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
484 case DEMUX_GET_TITLE_INFO:
488 msg_Err( p_demux, "unknown query in subtitle control" );
493 /*****************************************************************************
494 * Demux: Send subtitle to decoder
495 *****************************************************************************/
496 static int Demux( demux_t *p_demux )
498 demux_sys_t *p_sys = p_demux->p_sys;
501 if( p_sys->i_subtitle >= p_sys->i_subtitles )
504 i_maxdate = p_sys->i_next_demux_date;
505 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
507 /* Should not happen */
508 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
511 while( p_sys->i_subtitle < p_sys->i_subtitles &&
512 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
515 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
524 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
530 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
536 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
537 p_block->i_dts = p_block->i_pts;
538 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
541 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
544 memcpy( p_block->p_buffer,
545 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
546 if( p_block->i_pts > 0 )
548 es_out_Send( p_demux->out, p_sys->es, p_block );
552 block_Release( p_block );
558 p_sys->i_next_demux_date = 0;
563 /*****************************************************************************
564 * Fix: fix time stamp and order of subtitle
565 *****************************************************************************/
566 static void Fix( demux_t *p_demux )
568 demux_sys_t *p_sys = p_demux->p_sys;
572 /* *** fix order (to be sure...) *** */
573 /* We suppose that there are near in order and this durty bubble sort
574 * wont take too much time
579 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
581 if( p_sys->subtitle[i_index].i_start <
582 p_sys->subtitle[i_index - 1].i_start )
586 p_sys->subtitle + i_index - 1,
587 sizeof( subtitle_t ) );
588 memcpy( p_sys->subtitle + i_index - 1,
589 p_sys->subtitle + i_index,
590 sizeof( subtitle_t ) );
591 memcpy( p_sys->subtitle + i_index,
593 sizeof( subtitle_t ) );
600 static int TextLoad( text_t *txt, stream_t *s )
606 txt->i_line_count = 0;
608 txt->line = calloc( i_line_max, sizeof( char * ) );
610 /* load the complete file */
613 char *psz = stream_ReadLine( s );
618 txt->line[txt->i_line_count++] = psz;
619 if( txt->i_line_count >= i_line_max )
622 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
626 if( txt->i_line_count <= 0 )
634 static void TextUnload( text_t *txt )
638 for( i = 0; i < txt->i_line_count; i++ )
640 free( txt->line[i] );
644 txt->i_line_count = 0;
647 static char *TextGetLine( text_t *txt )
649 if( txt->i_line >= txt->i_line_count )
652 return txt->line[txt->i_line++];
654 static void TextPreviousLine( text_t *txt )
656 if( txt->i_line > 0 )
660 /*****************************************************************************
661 * Specific Subtitle function
662 *****************************************************************************/
663 #define MAX_LINE 8192
664 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
666 demux_sys_t *p_sys = p_demux->p_sys;
667 text_t *txt = &p_sys->txt;
670 * {n1}{n2}Line1|Line2|Line3....
671 * where n1 and n2 are the video frame number...
676 char buffer_text[MAX_LINE + 1];
677 unsigned int i_start;
681 p_subtitle->i_start = 0;
682 p_subtitle->i_stop = 0;
683 p_subtitle->psz_text = NULL;
687 if( ( s = TextGetLine( txt ) ) == NULL )
689 return( VLC_EGENERIC );
694 memset( buffer_text, '\0', MAX_LINE );
695 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
696 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
701 /* replace | by \n */
702 for( i = 0; i < strlen( buffer_text ); i++ )
704 if( buffer_text[i] == '|' )
706 buffer_text[i] = '\n';
710 p_subtitle->i_start = (mtime_t)i_start * p_sys->i_microsecperframe;
711 p_subtitle->i_stop = (mtime_t)i_stop * p_sys->i_microsecperframe;
712 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
716 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
718 demux_sys_t *p_sys = p_demux->p_sys;
719 text_t *txt = &p_sys->txt;
723 * h1:m1:s1,d1 --> h2:m2:s2,d2
731 char buffer_text[ 10 * MAX_LINE];
736 p_subtitle->i_start = 0;
737 p_subtitle->i_stop = 0;
738 p_subtitle->psz_text = NULL;
742 int h1, m1, s1, d1, h2, m2, s2, d2;
743 if( ( s = TextGetLine( txt ) ) == NULL )
745 return( VLC_EGENERIC );
748 "%d:%d:%d,%d --> %d:%d:%d,%d",
750 &h2, &m2, &s2, &d2 ) == 8 )
752 i_start = ( (mtime_t)h1 * 3600*1000 +
753 (mtime_t)m1 * 60*1000 +
755 (mtime_t)d1 ) * 1000;
757 i_stop = ( (mtime_t)h2 * 3600*1000 +
758 (mtime_t)m2 * 60*1000 +
760 (mtime_t)d2 ) * 1000;
762 /* Now read text until an empty line */
763 for( i_buffer_text = 0;; )
766 if( ( s = TextGetLine( txt ) ) == NULL )
768 return( VLC_EGENERIC );
774 /* empty line -> end of this subtitle */
775 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
776 p_subtitle->i_start = i_start;
777 p_subtitle->i_stop = i_stop;
778 p_subtitle->psz_text = strdup( buffer_text );
779 /* If framerate is available, use sub-fps */
780 if( p_sys->i_microsecperframe != 0 &&
781 p_sys->i_original_mspf != 0)
783 p_subtitle->i_start = (mtime_t)i_start *
784 p_sys->i_microsecperframe/
785 p_sys->i_original_mspf;
786 p_subtitle->i_stop = (mtime_t)i_stop *
787 p_sys->i_microsecperframe /
788 p_sys->i_original_mspf;
794 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
796 memcpy( buffer_text + i_buffer_text,
799 i_buffer_text += i_len;
801 buffer_text[i_buffer_text] = '\n';
810 static int ParseSubViewer( 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;
816 * h1:m1:s1.d1,h2:m2:s2.d2
821 * ( works with subviewer and subviewer v2 )
824 char buffer_text[ 10 * MAX_LINE];
829 p_subtitle->i_start = 0;
830 p_subtitle->i_stop = 0;
831 p_subtitle->psz_text = NULL;
835 int h1, m1, s1, d1, h2, m2, s2, d2;
836 if( ( s = TextGetLine( txt ) ) == NULL )
838 return( VLC_EGENERIC );
841 "%d:%d:%d.%d,%d:%d:%d.%d",
843 &h2, &m2, &s2, &d2 ) == 8 )
845 i_start = ( (mtime_t)h1 * 3600*1000 +
846 (mtime_t)m1 * 60*1000 +
848 (mtime_t)d1 ) * 1000;
850 i_stop = ( (mtime_t)h2 * 3600*1000 +
851 (mtime_t)m2 * 60*1000 +
853 (mtime_t)d2 ) * 1000;
855 /* Now read text until an empty line */
856 for( i_buffer_text = 0;; )
859 if( ( s = TextGetLine( txt ) ) == NULL )
861 return( VLC_EGENERIC );
867 /* empty line -> end of this subtitle */
868 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
869 p_subtitle->i_start = i_start;
870 p_subtitle->i_stop = i_stop;
872 /* replace [br] by \n */
873 for( i = 0; i < i_buffer_text - 3; i++ )
875 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
876 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
878 char *temp = buffer_text + i + 1;
879 buffer_text[i] = '\n';
880 memmove( temp, temp+3, strlen( temp ) -3 );
881 temp[strlen( temp )-3] = '\0';
884 p_subtitle->psz_text = strdup( buffer_text );
889 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
891 memcpy( buffer_text + i_buffer_text,
894 i_buffer_text += i_len;
896 buffer_text[i_buffer_text] = '\n';
906 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;
911 char buffer_text[ 10 * MAX_LINE];
916 p_subtitle->i_start = 0;
917 p_subtitle->i_stop = 0;
918 p_subtitle->psz_text = NULL;
922 int h1, m1, s1, c1, h2, m2, s2, c2;
925 if( ( s = TextGetLine( txt ) ) == NULL )
927 return( VLC_EGENERIC );
929 p_subtitle->psz_text = malloc( strlen( s ) );
932 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
936 buffer_text ) == 10 )
938 i_start = ( (mtime_t)h1 * 3600*1000 +
939 (mtime_t)m1 * 60*1000 +
941 (mtime_t)c1 * 10 ) * 1000;
943 i_stop = ( (mtime_t)h2 * 3600*1000 +
944 (mtime_t)m2 * 60*1000 +
946 (mtime_t)c2 * 10 ) * 1000;
948 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
949 if( p_sys->i_type == SUB_TYPE_SSA1 )
951 sprintf( p_subtitle->psz_text,
952 ",%d%s", i_dummy, strdup( buffer_text) );
956 sprintf( p_subtitle->psz_text,
957 ",%d,%s", i_dummy, strdup( buffer_text) );
959 p_subtitle->i_start = i_start;
960 p_subtitle->i_stop = i_stop;
965 /* All the other stuff we add to the header field */
966 if( p_sys->psz_header != NULL )
968 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
969 strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
971 msg_Err( p_demux, "out of memory");
974 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
975 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
979 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
981 msg_Err( p_demux, "out of memory");
984 p_sys->psz_header = strdup( s );
985 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
991 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
993 demux_sys_t *p_sys = p_demux->p_sys;
994 text_t *txt = &p_sys->txt;
998 * h:m:s:Line1|Line2|Line3....
1000 * h:m:s Line1|Line2|Line3....
1004 char buffer_text[MAX_LINE + 1];
1008 p_subtitle->i_start = 0;
1009 p_subtitle->i_stop = 0;
1010 p_subtitle->psz_text = NULL;
1017 if( ( p = TextGetLine( txt ) ) == NULL )
1019 return( VLC_EGENERIC );
1024 memset( buffer_text, '\0', MAX_LINE );
1025 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1027 i_start = ( (mtime_t)h * 3600*1000 +
1028 (mtime_t)m * 60*1000 +
1029 (mtime_t)s * 1000 ) * 1000;
1034 /* replace | by \n */
1035 for( i = 0; i < strlen( buffer_text ); i++ )
1037 if( buffer_text[i] == '|' )
1039 buffer_text[i] = '\n';
1042 p_subtitle->i_start = i_start;
1044 p_subtitle->i_stop = 0;
1045 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1049 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1053 if( strcasestr( psz_start, psz_str ) )
1055 char *s = strcasestr( psz_start, psz_str );
1057 s += strlen( psz_str );
1065 if( ( p = TextGetLine( txt ) ) == NULL )
1069 if( strcasestr( p, psz_str ) )
1071 char *s = strcasestr( p, psz_str );
1073 s += strlen( psz_str );
1080 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1082 demux_sys_t *p_sys = p_demux->p_sys;
1083 text_t *txt = &p_sys->txt;
1089 char buffer_text[10*MAX_LINE + 1];
1091 p_subtitle->i_start = 0;
1092 p_subtitle->i_stop = 0;
1093 p_subtitle->psz_text = NULL;
1096 if( i_text < 10*MAX_LINE ) \
1098 buffer_text[i_text++] = c; \
1099 buffer_text[i_text] = '\0'; \
1102 /* search "Start=" */
1103 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1105 return VLC_EGENERIC;
1108 /* get start value */
1109 i_start = strtol( p, &p, 0 );
1112 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1114 return VLC_EGENERIC;
1117 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1119 return VLC_EGENERIC;
1123 buffer_text[0] = '\0';
1124 /* now get all txt until a "Start=" line */
1131 if( !strncasecmp( p, "<br", 3 ) )
1135 else if( strcasestr( p, "Start=" ) )
1137 TextPreviousLine( txt );
1140 p = ParseSamiSearch( txt, p, ">" );
1142 else if( !strncmp( p, " ", 6 ) )
1147 else if( *p == '\t' )
1160 p = TextGetLine( txt );
1169 p_subtitle->i_start = i_start * 1000;
1170 p_subtitle->i_stop = 0;
1171 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1173 return( VLC_SUCCESS );