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 int64_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 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
196 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
200 p_sys->i_microsecperframe = 0;
203 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
206 p_sys->i_original_mspf = (int64_t)( (float)1000000 / f_fps );
210 p_sys->i_original_mspf = 0;
213 /* Get or probe the type */
214 p_sys->i_type = SUB_TYPE_UNKNOWN;
215 psz_type = var_CreateGetString( p_demux, "sub-type" );
222 if( sub_read_subtitle_function[i].psz_type_name == NULL )
225 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
228 p_sys->i_type = sub_read_subtitle_function[i].i_type;
235 /* Probe if unknown type */
236 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
241 msg_Dbg( p_demux, "autodetecting subtitle format" );
242 for( i_try = 0; i_try < 256; i_try++ )
246 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
249 if( strcasestr( s, "<SAMI>" ) )
251 p_sys->i_type = SUB_TYPE_SAMI;
254 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
255 sscanf( s, "{%d}{}", &i_dummy ) == 1)
257 p_sys->i_type = SUB_TYPE_MICRODVD;
261 "%d:%d:%d,%d --> %d:%d:%d,%d",
262 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
263 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
265 p_sys->i_type = SUB_TYPE_SUBRIP;
269 "!: This is a Sub Station Alpha v%d.x script.",
274 p_sys->i_type = SUB_TYPE_SSA1;
278 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
282 else if( strcasestr( s, "This is a Sub Station Alpha v4 script" ) )
284 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
287 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
289 p_sys->i_type = SUB_TYPE_SSA2_4; /* could be wrong */
292 else if( strcasestr( s, "[INFORMATION]" ) )
294 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
297 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
298 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
300 p_sys->i_type = SUB_TYPE_VPLAYER;
310 /* It will nearly always work even for non seekable stream thanks the
311 * caching system, and if it fails we loose just a few sub */
312 if( stream_Seek( p_demux->s, 0 ) )
314 msg_Warn( p_demux, "failed to rewind" );
317 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
319 msg_Err( p_demux, "failed to recognize subtitle type" );
325 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
327 msg_Dbg( p_demux, "detected %s format",
328 sub_read_subtitle_function[i].psz_name );
329 pf_read = sub_read_subtitle_function[i].pf_read;
334 msg_Dbg( p_demux, "loading all subtitles..." );
336 /* Load the whole file */
337 TextLoad( &p_sys->txt, p_demux->s );
342 if( p_sys->i_subtitles >= i_max )
345 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
346 sizeof(subtitle_t) * i_max ) ) )
348 msg_Err( p_demux, "out of memory");
353 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
356 p_sys->i_subtitles++;
359 TextUnload( &p_sys->txt );
361 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
363 /* Fix subtitle (order and time) *** */
364 p_sys->i_subtitle = 0;
366 if( p_sys->i_subtitles > 0 )
368 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
370 if( p_sys->i_length <= 0 )
371 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
374 /* *** add subtitle ES *** */
375 if( p_sys->i_type == SUB_TYPE_SSA1 ||
376 p_sys->i_type == SUB_TYPE_SSA2_4 )
378 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
382 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
384 if( p_sys->psz_header != NULL )
386 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
387 fmt.p_extra = strdup( p_sys->psz_header );
389 p_sys->es = es_out_Add( p_demux->out, &fmt );
394 /*****************************************************************************
395 * Close: Close subtitle demux
396 *****************************************************************************/
397 static void Close( vlc_object_t *p_this )
399 demux_t *p_demux = (demux_t*)p_this;
400 demux_sys_t *p_sys = p_demux->p_sys;
403 for( i = 0; i < p_sys->i_subtitles; i++ )
405 if( p_sys->subtitle[i].psz_text )
406 free( p_sys->subtitle[i].psz_text );
408 if( p_sys->subtitle )
409 free( p_sys->subtitle );
414 /*****************************************************************************
416 *****************************************************************************/
417 static int Control( demux_t *p_demux, int i_query, va_list args )
419 demux_sys_t *p_sys = p_demux->p_sys;
425 case DEMUX_GET_LENGTH:
426 pi64 = (int64_t*)va_arg( args, int64_t * );
427 *pi64 = p_sys->i_length;
431 pi64 = (int64_t*)va_arg( args, int64_t * );
432 if( p_sys->i_subtitle < p_sys->i_subtitles )
434 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
440 i64 = (int64_t)va_arg( args, int64_t );
441 p_sys->i_subtitle = 0;
442 while( p_sys->i_subtitle < p_sys->i_subtitles &&
443 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
448 if( p_sys->i_subtitle >= p_sys->i_subtitles )
452 case DEMUX_GET_POSITION:
453 pf = (double*)va_arg( args, double * );
454 if( p_sys->i_subtitle >= p_sys->i_subtitles )
458 else if( p_sys->i_subtitles > 0 )
460 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
461 (double)p_sys->i_length;
469 case DEMUX_SET_POSITION:
470 f = (double)va_arg( args, double );
471 i64 = f * p_sys->i_length;
473 p_sys->i_subtitle = 0;
474 while( p_sys->i_subtitle < p_sys->i_subtitles &&
475 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
479 if( p_sys->i_subtitle >= p_sys->i_subtitles )
483 case DEMUX_SET_NEXT_DEMUX_TIME:
484 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
489 case DEMUX_GET_TITLE_INFO:
493 msg_Err( p_demux, "unknown query in subtitle control" );
498 /*****************************************************************************
499 * Demux: Send subtitle to decoder
500 *****************************************************************************/
501 static int Demux( demux_t *p_demux )
503 demux_sys_t *p_sys = p_demux->p_sys;
506 if( p_sys->i_subtitle >= p_sys->i_subtitles )
509 i_maxdate = p_sys->i_next_demux_date;
510 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
512 /* Should not happen */
513 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
516 while( p_sys->i_subtitle < p_sys->i_subtitles &&
517 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
520 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
529 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
535 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
541 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
542 p_block->i_dts = p_block->i_pts;
543 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
546 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
549 memcpy( p_block->p_buffer,
550 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
551 if( p_block->i_pts > 0 )
553 es_out_Send( p_demux->out, p_sys->es, p_block );
557 block_Release( p_block );
563 p_sys->i_next_demux_date = 0;
568 /*****************************************************************************
569 * Fix: fix time stamp and order of subtitle
570 *****************************************************************************/
571 static void Fix( demux_t *p_demux )
573 demux_sys_t *p_sys = p_demux->p_sys;
577 /* *** fix order (to be sure...) *** */
578 /* We suppose that there are near in order and this durty bubble sort
579 * wont take too much time
584 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
586 if( p_sys->subtitle[i_index].i_start <
587 p_sys->subtitle[i_index - 1].i_start )
591 p_sys->subtitle + i_index - 1,
592 sizeof( subtitle_t ) );
593 memcpy( p_sys->subtitle + i_index - 1,
594 p_sys->subtitle + i_index,
595 sizeof( subtitle_t ) );
596 memcpy( p_sys->subtitle + i_index,
598 sizeof( subtitle_t ) );
605 static int TextLoad( text_t *txt, stream_t *s )
611 txt->i_line_count = 0;
613 txt->line = calloc( i_line_max, sizeof( char * ) );
615 /* load the complete file */
618 char *psz = stream_ReadLine( s );
623 txt->line[txt->i_line_count++] = psz;
624 if( txt->i_line_count >= i_line_max )
627 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
631 if( txt->i_line_count <= 0 )
639 static void TextUnload( text_t *txt )
643 for( i = 0; i < txt->i_line_count; i++ )
645 free( txt->line[i] );
649 txt->i_line_count = 0;
652 static char *TextGetLine( text_t *txt )
654 if( txt->i_line >= txt->i_line_count )
657 return txt->line[txt->i_line++];
659 static void TextPreviousLine( text_t *txt )
661 if( txt->i_line > 0 )
665 /*****************************************************************************
666 * Specific Subtitle function
667 *****************************************************************************/
668 #define MAX_LINE 8192
669 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
671 demux_sys_t *p_sys = p_demux->p_sys;
672 text_t *txt = &p_sys->txt;
675 * {n1}{n2}Line1|Line2|Line3....
676 * where n1 and n2 are the video frame number...
681 char buffer_text[MAX_LINE + 1];
686 int i_microsecperframe = 40000; /* default to 25 fps */
687 if( p_sys->i_microsecperframe > 0 )
688 i_microsecperframe = p_sys->i_microsecperframe;
690 p_subtitle->i_start = 0;
691 p_subtitle->i_stop = 0;
692 p_subtitle->psz_text = NULL;
696 if( ( s = TextGetLine( txt ) ) == NULL )
698 return( VLC_EGENERIC );
703 memset( buffer_text, '\0', MAX_LINE );
704 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
705 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
710 /* replace | by \n */
711 for( i = 0; i < strlen( buffer_text ); i++ )
713 if( buffer_text[i] == '|' )
715 buffer_text[i] = '\n';
719 p_subtitle->i_start = (int64_t)i_start * i_microsecperframe;
720 p_subtitle->i_stop = (int64_t)i_stop * i_microsecperframe;
721 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
725 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
727 demux_sys_t *p_sys = p_demux->p_sys;
728 text_t *txt = &p_sys->txt;
732 * h1:m1:s1,d1 --> h2:m2:s2,d2
740 char buffer_text[ 10 * MAX_LINE];
745 p_subtitle->i_start = 0;
746 p_subtitle->i_stop = 0;
747 p_subtitle->psz_text = NULL;
751 int h1, m1, s1, d1, h2, m2, s2, d2;
752 if( ( s = TextGetLine( txt ) ) == NULL )
754 return( VLC_EGENERIC );
757 "%d:%d:%d,%d --> %d:%d:%d,%d",
759 &h2, &m2, &s2, &d2 ) == 8 )
761 i_start = ( (int64_t)h1 * 3600*1000 +
762 (int64_t)m1 * 60*1000 +
764 (int64_t)d1 ) * 1000;
766 i_stop = ( (int64_t)h2 * 3600*1000 +
767 (int64_t)m2 * 60*1000 +
769 (int64_t)d2 ) * 1000;
771 /* Now read text until an empty line */
772 for( i_buffer_text = 0;; )
775 if( ( s = TextGetLine( txt ) ) == NULL )
777 return( VLC_EGENERIC );
783 /* empty line -> end of this subtitle */
784 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
785 p_subtitle->i_start = i_start;
786 p_subtitle->i_stop = i_stop;
787 p_subtitle->psz_text = strdup( buffer_text );
788 /* If framerate is available, use sub-fps */
789 if( p_sys->i_microsecperframe != 0 &&
790 p_sys->i_original_mspf != 0)
792 p_subtitle->i_start = (int64_t)i_start *
793 p_sys->i_microsecperframe/
794 p_sys->i_original_mspf;
795 p_subtitle->i_stop = (int64_t)i_stop *
796 p_sys->i_microsecperframe /
797 p_sys->i_original_mspf;
803 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
805 memcpy( buffer_text + i_buffer_text,
808 i_buffer_text += i_len;
810 buffer_text[i_buffer_text] = '\n';
819 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
821 demux_sys_t *p_sys = p_demux->p_sys;
822 text_t *txt = &p_sys->txt;
825 * h1:m1:s1.d1,h2:m2:s2.d2
830 * ( works with subviewer and subviewer v2 )
833 char buffer_text[ 10 * MAX_LINE];
838 p_subtitle->i_start = 0;
839 p_subtitle->i_stop = 0;
840 p_subtitle->psz_text = NULL;
844 int h1, m1, s1, d1, h2, m2, s2, d2;
845 if( ( s = TextGetLine( txt ) ) == NULL )
847 return( VLC_EGENERIC );
850 "%d:%d:%d.%d,%d:%d:%d.%d",
852 &h2, &m2, &s2, &d2 ) == 8 )
854 i_start = ( (int64_t)h1 * 3600*1000 +
855 (int64_t)m1 * 60*1000 +
857 (int64_t)d1 ) * 1000;
859 i_stop = ( (int64_t)h2 * 3600*1000 +
860 (int64_t)m2 * 60*1000 +
862 (int64_t)d2 ) * 1000;
864 /* Now read text until an empty line */
865 for( i_buffer_text = 0;; )
868 if( ( s = TextGetLine( txt ) ) == NULL )
870 return( VLC_EGENERIC );
876 /* empty line -> end of this subtitle */
877 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
878 p_subtitle->i_start = i_start;
879 p_subtitle->i_stop = i_stop;
881 /* replace [br] by \n */
882 for( i = 0; i < i_buffer_text - 3; i++ )
884 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
885 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
887 char *temp = buffer_text + i + 1;
888 buffer_text[i] = '\n';
889 memmove( temp, temp+3, strlen( temp ) -3 );
890 temp[strlen( temp )-3] = '\0';
893 p_subtitle->psz_text = strdup( buffer_text );
898 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
900 memcpy( buffer_text + i_buffer_text,
903 i_buffer_text += i_len;
905 buffer_text[i_buffer_text] = '\n';
915 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
917 demux_sys_t *p_sys = p_demux->p_sys;
918 text_t *txt = &p_sys->txt;
920 char buffer_text[ 10 * MAX_LINE];
925 p_subtitle->i_start = 0;
926 p_subtitle->i_stop = 0;
927 p_subtitle->psz_text = NULL;
931 int h1, m1, s1, c1, h2, m2, s2, c2;
934 if( ( s = TextGetLine( txt ) ) == NULL )
936 return( VLC_EGENERIC );
938 p_subtitle->psz_text = malloc( strlen( s ) );
941 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
945 buffer_text ) == 10 )
947 i_start = ( (int64_t)h1 * 3600*1000 +
948 (int64_t)m1 * 60*1000 +
950 (int64_t)c1 * 10 ) * 1000;
952 i_stop = ( (int64_t)h2 * 3600*1000 +
953 (int64_t)m2 * 60*1000 +
955 (int64_t)c2 * 10 ) * 1000;
957 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
958 if( p_sys->i_type == SUB_TYPE_SSA1 )
960 sprintf( p_subtitle->psz_text,
961 ",%d%s", i_dummy, strdup( buffer_text) );
965 sprintf( p_subtitle->psz_text,
966 ",%d,%s", i_dummy, strdup( buffer_text) );
968 p_subtitle->i_start = i_start;
969 p_subtitle->i_stop = i_stop;
974 /* All the other stuff we add to the header field */
975 if( p_sys->psz_header != NULL )
977 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
978 strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
980 msg_Err( p_demux, "out of memory");
983 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
984 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
988 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
990 msg_Err( p_demux, "out of memory");
993 p_sys->psz_header = strdup( s );
994 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1000 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1002 demux_sys_t *p_sys = p_demux->p_sys;
1003 text_t *txt = &p_sys->txt;
1007 * h:m:s:Line1|Line2|Line3....
1009 * h:m:s Line1|Line2|Line3....
1013 char buffer_text[MAX_LINE + 1];
1017 p_subtitle->i_start = 0;
1018 p_subtitle->i_stop = 0;
1019 p_subtitle->psz_text = NULL;
1026 if( ( p = TextGetLine( txt ) ) == NULL )
1028 return( VLC_EGENERIC );
1033 memset( buffer_text, '\0', MAX_LINE );
1034 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1036 i_start = ( (int64_t)h * 3600*1000 +
1037 (int64_t)m * 60*1000 +
1038 (int64_t)s * 1000 ) * 1000;
1043 /* replace | by \n */
1044 for( i = 0; i < strlen( buffer_text ); i++ )
1046 if( buffer_text[i] == '|' )
1048 buffer_text[i] = '\n';
1051 p_subtitle->i_start = i_start;
1053 p_subtitle->i_stop = 0;
1054 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1058 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1062 if( strcasestr( psz_start, psz_str ) )
1064 char *s = strcasestr( psz_start, psz_str );
1066 s += strlen( psz_str );
1074 if( ( p = TextGetLine( txt ) ) == NULL )
1078 if( strcasestr( p, psz_str ) )
1080 char *s = strcasestr( p, psz_str );
1082 s += strlen( psz_str );
1089 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1091 demux_sys_t *p_sys = p_demux->p_sys;
1092 text_t *txt = &p_sys->txt;
1098 char buffer_text[10*MAX_LINE + 1];
1100 p_subtitle->i_start = 0;
1101 p_subtitle->i_stop = 0;
1102 p_subtitle->psz_text = NULL;
1105 if( i_text < 10*MAX_LINE ) \
1107 buffer_text[i_text++] = c; \
1108 buffer_text[i_text] = '\0'; \
1111 /* search "Start=" */
1112 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1114 return VLC_EGENERIC;
1117 /* get start value */
1118 i_start = strtol( p, &p, 0 );
1121 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1123 return VLC_EGENERIC;
1126 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1128 return VLC_EGENERIC;
1132 buffer_text[0] = '\0';
1133 /* now get all txt until a "Start=" line */
1140 if( !strncasecmp( p, "<br", 3 ) )
1144 else if( strcasestr( p, "Start=" ) )
1146 TextPreviousLine( txt );
1149 p = ParseSamiSearch( txt, p, ">" );
1151 else if( !strncmp( p, " ", 6 ) )
1156 else if( *p == '\t' )
1169 p = TextGetLine( txt );
1178 p_subtitle->i_start = i_start * 1000;
1179 p_subtitle->i_stop = 0;
1180 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1182 return( VLC_SUCCESS );