1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2003 VideoLAN
5 * $Id: sub.c,v 1.22 2003/08/23 22:02:45 hartman Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <sys/types.h>
33 #include <vlc/input.h>
34 #include "vlc_video.h"
38 #if (!defined( WIN32 ) || defined(__MINGW32__))
42 static int Open ( vlc_object_t *p_this );
44 static int sub_open ( subtitle_demux_t *p_sub,
45 input_thread_t *p_input,
47 mtime_t i_microsecperframe );
48 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate );
49 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date );
50 static void sub_close( subtitle_demux_t *p_sub );
52 static void sub_fix( subtitle_demux_t *p_sub );
54 static char *ppsz_sub_type[] = { "microdvd", "subrip", "ssa1", "ssa2-4", "vplayer", "sami", NULL };
57 /*****************************************************************************
59 *****************************************************************************/
60 #define SUB_DELAY_LONGTEXT \
61 "Delay subtitles (in 1/10s)"
62 #define SUB_FPS_LONGTEXT \
63 "Override frames per second. " \
64 "It will only work with MicroDVD subtitles."
65 #define SUB_TYPE_LONGTEXT \
66 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
67 "\"sami\" (nothing for autodetection, it should always work)."
68 #define SUB_AUTO_LONGTEXT \
69 "Automatically detect a subtitle file, if no subtitle filename is" \
73 set_description( _("Text subtitles demux") );
74 set_capability( "subtitle demux", 12 );
75 add_category_hint( "Subtitles", NULL, VLC_TRUE );
76 add_file( "sub-file", NULL, NULL,
77 "Subtitles file name", "Subtitles file name", VLC_TRUE );
78 add_bool( "sub-autodetect-file", VLC_TRUE, NULL, "Autodetect subtitle filename",
79 SUB_AUTO_LONGTEXT, VLC_FALSE );
80 add_float( "sub-fps", 0.0, NULL,
82 SUB_FPS_LONGTEXT, VLC_TRUE );
83 add_integer( "sub-delay", 0, NULL,
84 "Delay subtitles (in 1/10s)",
85 SUB_DELAY_LONGTEXT, VLC_TRUE );
86 add_string_from_list( "sub-type", NULL, ppsz_sub_type, NULL,
88 SUB_TYPE_LONGTEXT, VLC_TRUE );
89 set_callbacks( Open, NULL );
92 /*****************************************************************************
94 *****************************************************************************/
95 static int Open ( vlc_object_t *p_this )
97 subtitle_demux_t *p_sub = (subtitle_demux_t*)p_this;
99 p_sub->pf_open = sub_open;
100 p_sub->pf_demux = sub_demux;
101 p_sub->pf_seek = sub_seek;
102 p_sub->pf_close = sub_close;
104 /* Initialize the variables */
105 var_Create( p_this, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
106 var_Create( p_this, "sub-autodetect-file", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
107 var_Create( p_this, "sub-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
108 var_Create( p_this, "sub-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
109 var_Create( p_this, "sub-type", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
114 #define MAX_LINE 2048
116 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
125 static int text_load( text_t *txt, char *psz_name )
132 txt->i_line_count = 0;
134 txt->line = calloc( i_line_max, sizeof( char * ) );
137 if( !( f = fopen( psz_name, "rb" ) ) )
142 /* load the complete file */
148 if( fgets( buffer, 8096, f ) <= 0)
152 while( ( p = strchr( buffer, '\r' ) ) )
156 while( ( p = strchr( buffer, '\n' ) ) )
161 txt->line[txt->i_line_count++] = strdup( buffer );
163 if( txt->i_line_count >= i_line_max )
166 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
172 if( txt->i_line_count <= 0 )
175 return( VLC_EGENERIC );
178 return( VLC_SUCCESS );
180 static void text_unload( text_t *txt )
184 for( i = 0; i < txt->i_line_count; i++ )
186 FREE( txt->line[i] );
190 txt->i_line_count = 0;
193 static char *text_get_line( text_t *txt )
195 if( txt->i_line >= txt->i_line_count )
200 return( txt->line[txt->i_line++] );
202 static void text_previous_line( text_t *txt )
204 if( txt->i_line > 0 )
209 static void text_rewind( text_t *txt )
214 static int sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
215 static int sub_SubRipRead ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
216 static int sub_SSA1Read ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
217 static int sub_SSA2_4Read ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
218 static int sub_Vplayer ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
219 static int sub_Sami ( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
226 int (*pf_read_subtitle) ( text_t *, subtitle_t*, mtime_t );
227 } sub_read_subtitle_function [] =
229 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", sub_MicroDvdRead },
230 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", sub_SubRipRead },
231 { "ssa1", SUB_TYPE_SSA1, "SSA-1", sub_SSA1Read },
232 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4",sub_SSA2_4Read },
233 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", sub_Vplayer },
234 { "sami", SUB_TYPE_SAMI, "SAMI", sub_Sami },
235 { NULL, SUB_TYPE_UNKNOWN, "Unknow", NULL }
238 /*****************************************************************************
239 * sub_detect: Use the original filename to find a subtitle file
240 *****************************************************************************/
241 char* sub_detect( subtitle_demux_t *p_sub, char *psz_filename)
244 struct dirent *p_dir_afile;
245 char * ppsz_sub_exts[] = { "sub", "srt", "smi", "ssa", NULL};
246 char *psz_result, *psz_basename, *psz_dir, *psz_file_noext, *psz_extension;
250 if( psz_filename && *psz_filename )
253 psz_basename = strrchr( psz_filename , '\\' );
255 psz_basename = strrchr( psz_filename , '/' );
259 i_dirlen = ( 1 + psz_basename ) - psz_filename;
260 psz_dir = (char*)malloc( i_dirlen + 1 );
266 strncpy( psz_dir, psz_filename, i_dirlen );
267 psz_dir[i_dirlen] = '\0';
272 psz_basename = psz_filename;
275 psz_extension = strrchr( psz_basename , '.' );
279 size_t i_baselen = ( 1 + psz_extension ) - psz_basename;
281 psz_file_noext = (char*)malloc( i_baselen + 1 );
282 if( !psz_file_noext )
286 strncpy( psz_file_noext, psz_basename, i_baselen );
287 psz_file_noext[i_baselen] = '\0';
292 p_dir_handle = opendir( psz_dir );
296 while(( p_dir_afile = readdir( p_dir_handle ))) {
297 char* psz_afile_ext = strrchr( p_dir_afile->d_name, '.' );
302 for (i = 0; ppsz_sub_exts[i]; i++) {
303 if( strcmp( ppsz_sub_exts[i], psz_afile_ext ) == 0 ) {
310 msg_Dbg( p_sub, "autodetected subtitlefile: %s", strdup( p_dir_afile->d_name ) );
313 psz_result = (char*)malloc( i_dirlen + strlen( p_dir_afile->d_name ) +1 );
314 strncpy( psz_result, psz_dir, i_dirlen );
315 char *psz_append = psz_result + i_dirlen;
316 strncpy( psz_append, p_dir_afile->d_name, strlen( p_dir_afile->d_name ) );
317 psz_result[i_dirlen + strlen( p_dir_afile->d_name )] = '\0';
320 else return strdup( p_dir_afile->d_name );
324 closedir( p_dir_handle );
331 /*****************************************************************************
332 * sub_open: Open a subtitle file and add subtitle ES
333 *****************************************************************************/
334 static int sub_open ( subtitle_demux_t *p_sub,
335 input_thread_t *p_input,
337 mtime_t i_microsecperframe )
345 int (*pf_read_subtitle)( text_t *, subtitle_t *, mtime_t ) = NULL;
347 p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
349 p_sub->i_subtitles = 0;
350 p_sub->subtitle = NULL;
351 p_sub->p_input = p_input;
353 if( !psz_name || !*psz_name)
355 var_Get( p_sub, "sub-file", &val );
356 if( !val.psz_string || !*val.psz_string )
358 var_Get( p_sub, "sub-autodetect-file", &val );
361 psz_name = strdup( sub_detect( p_sub, p_input->psz_source));
362 if( !psz_name || !*psz_name ) return VLC_EGENERIC;
366 if( val.psz_string ) free( val.psz_string );
372 psz_name = strdup( val.psz_string );
373 if( val.psz_string ) free( val.psz_string );
377 /* *** load the file *** */
378 if( text_load( &txt, psz_name ) )
380 msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
384 msg_Dbg( p_sub, "opened `%s'", psz_name );
387 var_Get( p_sub, "sub-fps", &val );
388 if( val.i_int >= 1.0 )
390 var_Get( p_sub, "sub-fps", &val );
391 i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
393 else if( i_microsecperframe <= 0 )
395 i_microsecperframe = 40000; /* default: 25fps */
398 var_Get( p_sub, "sub-type", &val);
399 if( val.psz_string && *val.psz_string )
405 if( sub_read_subtitle_function[i].psz_type_name == NULL )
407 i_sub_type = SUB_TYPE_UNKNOWN;
410 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
413 i_sub_type = sub_read_subtitle_function[i].i_type;
420 i_sub_type = SUB_TYPE_UNKNOWN;
422 FREE( val.psz_string );
424 /* *** Now try to autodetect subtitle format *** */
425 if( i_sub_type == SUB_TYPE_UNKNOWN )
430 msg_Dbg( p_input, "trying to autodetect file format" );
431 for( i_try = 0; i_try < MAX_TRY; i_try++ )
435 if( ( s = text_get_line( &txt ) ) == NULL )
440 if( strstr( s, "<SAMI>" ) )
442 i_sub_type = SUB_TYPE_SAMI;
445 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
446 sscanf( s, "{%d}{}", &i_dummy ) == 1)
448 i_sub_type = SUB_TYPE_MICRODVD;
452 "%d:%d:%d,%d --> %d:%d:%d,%d",
453 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
454 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
456 i_sub_type = SUB_TYPE_SUBRIP;
460 "!: This is a Sub Station Alpha v%d.x script.",
465 i_sub_type = SUB_TYPE_SSA1;
469 i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
473 else if( strstr( s, "This is a Sub Station Alpha v4 script" ) )
475 i_sub_type = SUB_TYPE_SSA2_4; // I hop this will work
477 else if( !strncmp( s, "Dialogue: Marked", 16 ) )
479 i_sub_type = SUB_TYPE_SSA2_4; // could be wrong
482 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
483 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
485 i_sub_type = SUB_TYPE_VPLAYER;
493 /* *** Load this file in memory *** */
496 if( sub_read_subtitle_function[i].i_type == SUB_TYPE_UNKNOWN )
498 msg_Dbg( p_input, "unknown subtitle file" );
503 if( sub_read_subtitle_function[i].i_type == i_sub_type )
505 msg_Dbg( p_input, "detected %s format",
506 sub_read_subtitle_function[i].psz_name );
507 pf_read_subtitle = sub_read_subtitle_function[i].pf_read_subtitle;
514 if( p_sub->i_subtitles >= i_max )
517 if( p_sub->subtitle )
519 p_sub->subtitle = realloc( p_sub->subtitle,
520 sizeof( subtitle_t ) * i_max );
524 p_sub->subtitle = malloc( sizeof( subtitle_t ) * i_max );
527 if( pf_read_subtitle( &txt,
528 p_sub->subtitle + p_sub->i_subtitles,
529 i_microsecperframe ) < 0 )
533 p_sub->i_subtitles++;
535 msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles );
537 /* *** Close the file *** */
540 /* *** fix subtitle (order and time) *** */
541 p_sub->i_subtitle = 0; // will be modified by sub_fix
544 /* *** add subtitle ES *** */
545 vlc_mutex_lock( &p_input->stream.stream_lock );
546 p_sub->p_es = input_AddES( p_input, p_input->stream.p_selected_program,
549 vlc_mutex_unlock( &p_input->stream.stream_lock );
551 p_sub->p_es->i_stream_id = 0xff; // FIXME
552 p_sub->p_es->i_fourcc = VLC_FOURCC( 's','u','b','t' );
554 p_sub->i_previously_selected = 0;
558 /*****************************************************************************
559 * sub_demux: Send subtitle to decoder until i_maxdate
560 *****************************************************************************/
561 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
563 if( p_sub->p_es->p_decoder_fifo && !p_sub->i_previously_selected )
565 p_sub->i_previously_selected = 1;
566 p_sub->pf_seek( p_sub, i_maxdate );
569 else if( !p_sub->p_es->p_decoder_fifo && p_sub->i_previously_selected )
571 p_sub->i_previously_selected = 0;
575 while( p_sub->i_subtitle < p_sub->i_subtitles &&
576 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
579 data_packet_t *p_data;
583 i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
591 if( !( p_pes = input_NewPES( p_sub->p_input->p_method_data ) ) )
597 if( !( p_data = input_NewPacket( p_sub->p_input->p_method_data,
600 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
604 p_data->p_payload_end = p_data->p_payload_start + i_len;
607 input_ClockGetTS( p_sub->p_input,
608 p_sub->p_input->stream.p_selected_program,
609 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
610 if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
613 * i_dts means end of display...
616 input_ClockGetTS( p_sub->p_input,
617 p_sub->p_input->stream.p_selected_program,
618 p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
625 p_pes->i_nb_data = 1;
627 p_pes->p_last = p_data;
628 p_pes->i_pes_size = i_len;
630 memcpy( p_data->p_payload_start,
631 p_sub->subtitle[p_sub->i_subtitle].psz_text,
633 if( p_sub->p_es->p_decoder_fifo && p_pes->i_pts > 0 )
636 input_DecodePES( p_sub->p_es->p_decoder_fifo, p_pes );
640 input_DeletePES( p_sub->p_input->p_method_data, p_pes );
648 /*****************************************************************************
649 * sub_seek: Seek to i_date
650 *****************************************************************************/
651 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
653 /* should be fast enough... */
654 p_sub->i_subtitle = 0;
655 while( p_sub->i_subtitle < p_sub->i_subtitles &&
656 p_sub->subtitle[p_sub->i_subtitle].i_start < i_date )
664 /*****************************************************************************
665 * sub_close: Close subtitle demux
666 *****************************************************************************/
667 static void sub_close( subtitle_demux_t *p_sub )
669 if( p_sub->subtitle )
672 for( i = 0; i < p_sub->i_subtitles; i++ )
674 if( p_sub->subtitle[i].psz_text )
676 free( p_sub->subtitle[i].psz_text );
679 free( p_sub->subtitle );
682 /*****************************************************************************
684 * sub_fix: fix time stamp and order of subtitle
685 *****************************************************************************/
686 static void sub_fix( subtitle_demux_t *p_sub )
694 /* *** fix order (to be sure...) *** */
695 /* We suppose that there are near in order and this durty bubble sort
696 * wont take too much time
701 for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
703 if( p_sub->subtitle[i_index].i_start <
704 p_sub->subtitle[i_index - 1].i_start )
708 p_sub->subtitle + i_index - 1,
709 sizeof( subtitle_t ) );
710 memcpy( p_sub->subtitle + i_index - 1,
711 p_sub->subtitle + i_index,
712 sizeof( subtitle_t ) );
713 memcpy( p_sub->subtitle + i_index,
715 sizeof( subtitle_t ) );
721 /* *** and at the end add delay *** */
722 var_Get( p_sub, "sub-delay", &val );
723 i_delay = (mtime_t) val.i_int * 100000;
726 for( i = 0; i < p_sub->i_subtitles; i++ )
728 p_sub->subtitle[i].i_start += i_delay;
729 p_sub->subtitle[i].i_stop += i_delay;
730 if( p_sub->subtitle[i].i_start < 0 )
732 p_sub->i_subtitle = i + 1;
740 /*****************************************************************************
741 * Specific Subtitle function
742 *****************************************************************************/
743 static int sub_MicroDvdRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
747 * {n1}{n2}Line1|Line2|Line3....
748 * where n1 and n2 are the video frame number...
753 char buffer_text[MAX_LINE + 1];
760 if( ( s = text_get_line( txt ) ) == NULL )
762 return( VLC_EGENERIC );
767 memset( buffer_text, '\0', MAX_LINE );
768 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
769 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
774 /* replace | by \n */
775 for( i = 0; i < strlen( buffer_text ); i++ )
777 if( buffer_text[i] == '|' )
779 buffer_text[i] = '\n';
782 p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
783 p_subtitle->i_stop = (mtime_t)i_stop * (mtime_t)i_microsecperframe;
784 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
788 static int sub_SubRipRead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
792 * h1:m1:s1,d1 --> h2:m2:s2,d2
800 char buffer_text[ 10 * MAX_LINE];
807 int h1, m1, s1, d1, h2, m2, s2, d2;
808 if( ( s = text_get_line( txt ) ) == NULL )
810 return( VLC_EGENERIC );
813 "%d:%d:%d,%d --> %d:%d:%d,%d",
815 &h2, &m2, &s2, &d2 ) == 8 )
817 i_start = ( (mtime_t)h1 * 3600*1000 +
818 (mtime_t)m1 * 60*1000 +
820 (mtime_t)d1 ) * 1000;
822 i_stop = ( (mtime_t)h2 * 3600*1000 +
823 (mtime_t)m2 * 60*1000 +
825 (mtime_t)d2 ) * 1000;
827 /* Now read text until an empty line */
828 for( i_buffer_text = 0;; )
831 if( ( s = text_get_line( txt ) ) == NULL )
833 return( VLC_EGENERIC );
839 // empty line -> end of this subtitle
840 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
841 p_subtitle->i_start = i_start;
842 p_subtitle->i_stop = i_stop;
843 p_subtitle->psz_text = strdup( buffer_text );
848 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
850 memcpy( buffer_text + i_buffer_text,
853 i_buffer_text += i_len;
855 buffer_text[i_buffer_text] = '\n';
865 static int sub_SSARead( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe, int i_comma_count )
867 char buffer_text[ 10 * MAX_LINE];
877 int h1, m1, s1, c1, h2, m2, s2, c2;
880 if( ( s = text_get_line( txt ) ) == NULL )
882 return( VLC_EGENERIC );
885 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
889 buffer_text ) == 10 )
891 i_start = ( (mtime_t)h1 * 3600*1000 +
892 (mtime_t)m1 * 60*1000 +
894 (mtime_t)c1 * 10 ) * 1000;
896 i_stop = ( (mtime_t)h2 * 3600*1000 +
897 (mtime_t)m2 * 60*1000 +
899 (mtime_t)c2 * 10 ) * 1000;
901 p_buffer_text = buffer_text;
903 while( i_comma < i_comma_count &&
904 *p_buffer_text != '\0' )
906 if( *p_buffer_text == ',' )
912 p_subtitle->psz_text = malloc( strlen( p_buffer_text ) + 1);
914 while( *p_buffer_text )
916 if( *p_buffer_text == '\\' && ( *p_buffer_text =='n' || *p_buffer_text =='N' ) )
918 p_subtitle->psz_text[i_text] = '\n';
922 else if( *p_buffer_text == '{' && *p_buffer_text == '\\')
924 while( *p_buffer_text && *p_buffer_text != '}' )
931 p_subtitle->psz_text[i_text] = *p_buffer_text;
936 p_subtitle->psz_text[i_text] = '\0';
937 p_subtitle->i_start = i_start;
938 p_subtitle->i_stop = i_stop;
944 static int sub_SSA1Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
946 return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 8 ) );
948 static int sub_SSA2_4Read( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
950 return( sub_SSARead( txt, p_subtitle, i_microsecperframe, 9 ) );
953 static int sub_Vplayer( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
957 * h:m:s:Line1|Line2|Line3....
959 * h:m:s Line1|Line2|Line3....
960 * where n1 and n2 are the video frame number...
964 char buffer_text[MAX_LINE + 1];
973 if( ( p = text_get_line( txt ) ) == NULL )
975 return( VLC_EGENERIC );
980 memset( buffer_text, '\0', MAX_LINE );
981 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
983 i_start = ( (mtime_t)h * 3600*1000 +
984 (mtime_t)m * 60*1000 +
985 (mtime_t)s * 1000 ) * 1000;
990 /* replace | by \n */
991 for( i = 0; i < strlen( buffer_text ); i++ )
993 if( buffer_text[i] == '|' )
995 buffer_text[i] = '\n';
998 p_subtitle->i_start = i_start;
1000 p_subtitle->i_stop = 0;
1001 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1005 static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
1009 if( strstr( psz_start, psz_str ) )
1011 char *s = strstr( psz_start, psz_str );
1013 s += strlen( psz_str );
1021 if( ( p = text_get_line( txt ) ) == NULL )
1025 if( strstr( p, psz_str ) )
1027 char *s = strstr( p, psz_str );
1029 s += strlen( psz_str );
1036 static int sub_Sami( text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
1042 char buffer_text[10*MAX_LINE + 1];
1044 if( i_text < 10*MAX_LINE ) \
1046 buffer_text[i_text++] = c; \
1047 buffer_text[i_text] = '\0'; \
1050 /* search "Start=" */
1051 if( !( p = sub_SamiSearch( txt, NULL, "Start=" ) ) )
1053 return VLC_EGENERIC;
1056 /* get start value */
1057 i_start = strtol( p, &p, 0 );
1060 if( !( p = sub_SamiSearch( txt, p, "<P" ) ) )
1062 return VLC_EGENERIC;
1065 if( !( p = sub_SamiSearch( txt, p, ">" ) ) )
1067 return VLC_EGENERIC;
1071 buffer_text[0] = '\0';
1072 /* now get all txt until a "Start=" line */
1079 if( !strncmp( p, "<br", 3 ) || !strncmp( p, "<BR", 3 ) )
1083 else if( strstr( p, "Start=" ) )
1085 text_previous_line( txt );
1088 p = sub_SamiSearch( txt, p, ">" );
1090 else if( !strncmp( p, " ", 6 ) )
1095 else if( *p == '\t' )
1108 p = text_get_line( txt );
1117 p_subtitle->i_start = i_start * 1000;
1118 p_subtitle->i_stop = 0;
1119 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1121 return( VLC_SUCCESS );