1 /*****************************************************************************
2 * subtitle.c: Demux for subtitle text files.
3 *****************************************************************************
4 * Copyright (C) 1999-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <vlc_input.h>
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
39 #include <vlc_demux.h>
40 #include <vlc_charset.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t *p_this );
46 static void Close( vlc_object_t *p_this );
48 #define SUB_DELAY_LONGTEXT \
49 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
50 #define SUB_FPS_LONGTEXT \
51 N_("Override the normal frames per second settings. " \
52 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
53 #define SUB_TYPE_LONGTEXT \
54 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
55 "\"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
56 "\"sami\", \"dvdsubtitle\", \"mpl2\" and \"auto\" (meaning autodetection, this " \
57 "should always work).")
58 static const char *ppsz_sub_type[] =
60 "auto", "microdvd", "subrip", "subviewer", "ssa1",
61 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2"
65 set_shortname( _("Subtitles"));
66 set_description( _("Text subtitles parser") );
67 set_capability( "demux2", 0 );
68 set_category( CAT_INPUT );
69 set_subcategory( SUBCAT_INPUT_DEMUX );
70 add_float( "sub-fps", 0.0, NULL,
71 N_("Frames per second"),
72 SUB_FPS_LONGTEXT, VLC_TRUE );
73 add_integer( "sub-delay", 0, NULL,
74 N_("Subtitles delay"),
75 SUB_DELAY_LONGTEXT, VLC_TRUE );
76 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
77 SUB_TYPE_LONGTEXT, VLC_TRUE );
78 change_string_list( ppsz_sub_type, NULL, NULL );
79 set_callbacks( Open, Close );
81 add_shortcut( "subtitle" );
84 /*****************************************************************************
86 *****************************************************************************/
89 SUB_TYPE_UNKNOWN = -1,
108 static int TextLoad( text_t *, stream_t *s );
109 static void TextUnload( text_t * );
126 int64_t i_next_demux_date;
127 int64_t i_microsecperframe;
132 subtitle_t *subtitle;
137 static int ParseMicroDvd ( demux_t *, subtitle_t * );
138 static int ParseSubRip ( demux_t *, subtitle_t * );
139 static int ParseSubViewer ( demux_t *, subtitle_t * );
140 static int ParseSSA ( demux_t *, subtitle_t * );
141 static int ParseVplayer ( demux_t *, subtitle_t * );
142 static int ParseSami ( demux_t *, subtitle_t * );
143 static int ParseDVDSubtitle( demux_t *, subtitle_t * );
144 static int ParseMPL2 ( demux_t *, subtitle_t * );
148 const char *psz_type_name;
150 const char *psz_name;
151 int (*pf_read)( demux_t *, subtitle_t* );
152 } sub_read_subtitle_function [] =
154 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
155 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
156 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
157 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
158 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
159 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
160 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
161 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
162 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
163 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
164 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
167 static int Demux( demux_t * );
168 static int Control( demux_t *, int, va_list );
170 /*static void Fix( demux_t * );*/
172 /*****************************************************************************
174 *****************************************************************************/
175 static int Open ( vlc_object_t *p_this )
177 demux_t *p_demux = (demux_t*)p_this;
180 input_thread_t *p_input;
183 int (*pf_read)( demux_t *, subtitle_t* );
186 if( strcmp( p_demux->psz_demux, "subtitle" ) )
188 msg_Dbg( p_demux, "subtitle demux discarded" );
192 p_demux->pf_demux = Demux;
193 p_demux->pf_control = Control;
194 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
195 p_sys->psz_header = NULL;
196 p_sys->i_subtitle = 0;
197 p_sys->i_subtitles = 0;
198 p_sys->subtitle = NULL;
199 p_sys->i_microsecperframe = 40000;
202 p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
205 f_fps = var_GetFloat( p_input, "sub-original-fps" );
207 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
209 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
210 vlc_object_release( p_input );
213 /* Check for override of the fps */
214 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
217 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
218 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
221 /* Get or probe the type */
222 p_sys->i_type = SUB_TYPE_UNKNOWN;
223 psz_type = var_CreateGetString( p_demux, "sub-type" );
230 if( sub_read_subtitle_function[i].psz_type_name == NULL )
233 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
236 p_sys->i_type = sub_read_subtitle_function[i].i_type;
243 /* Probe if unknown type */
244 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
249 msg_Dbg( p_demux, "autodetecting subtitle format" );
250 for( i_try = 0; i_try < 256; i_try++ )
254 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
257 if( strcasestr( s, "<SAMI>" ) )
259 p_sys->i_type = SUB_TYPE_SAMI;
262 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
263 sscanf( s, "{%d}{}", &i_dummy ) == 1)
265 p_sys->i_type = SUB_TYPE_MICRODVD;
269 "%d:%d:%d,%d --> %d:%d:%d,%d",
270 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
271 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
273 p_sys->i_type = SUB_TYPE_SUBRIP;
276 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
278 p_sys->i_type = SUB_TYPE_SSA1;
281 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
283 p_sys->i_type = SUB_TYPE_ASS;
286 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
288 p_sys->i_type = SUB_TYPE_SSA2_4;
291 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
293 p_sys->i_type = SUB_TYPE_SSA2_4;
296 else if( !strncasecmp( s, "Dialogue:", 9 ) )
298 p_sys->i_type = SUB_TYPE_ASS;
301 else if( strcasestr( s, "[INFORMATION]" ) )
303 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
306 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
307 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
309 p_sys->i_type = SUB_TYPE_VPLAYER;
312 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
313 &i_dummy, &i_dummy ) == 4 )
315 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
318 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
319 sscanf( s, "[%d][]", &i_dummy ) == 1)
321 p_sys->i_type = SUB_TYPE_MPL2;
331 /* It will nearly always work even for non seekable stream thanks the
332 * caching system, and if it fails we lose just a few sub */
333 if( stream_Seek( p_demux->s, 0 ) )
335 msg_Warn( p_demux, "failed to rewind" );
338 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
340 msg_Err( p_demux, "failed to recognize subtitle type" );
347 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
349 msg_Dbg( p_demux, "detected %s format",
350 sub_read_subtitle_function[i].psz_name );
351 pf_read = sub_read_subtitle_function[i].pf_read;
356 msg_Dbg( p_demux, "loading all subtitles..." );
358 /* Load the whole file */
359 TextLoad( &p_sys->txt, p_demux->s );
364 if( p_sys->i_subtitles >= i_max )
367 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
368 sizeof(subtitle_t) * i_max ) ) )
370 msg_Err( p_demux, "out of memory");
371 if( p_sys->subtitle != NULL )
372 free( p_sys->subtitle );
373 TextUnload( &p_sys->txt );
379 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
382 p_sys->i_subtitles++;
385 TextUnload( &p_sys->txt );
387 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
389 /* Fix subtitle (order and time) *** */
390 p_sys->i_subtitle = 0;
392 if( p_sys->i_subtitles > 0 )
394 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
396 if( p_sys->i_length <= 0 )
397 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
400 /* *** add subtitle ES *** */
401 if( p_sys->i_type == SUB_TYPE_SSA1 ||
402 p_sys->i_type == SUB_TYPE_SSA2_4 ||
403 p_sys->i_type == SUB_TYPE_ASS )
405 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
409 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
411 if( p_sys->psz_header != NULL )
413 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
414 fmt.p_extra = strdup( p_sys->psz_header );
416 p_sys->es = es_out_Add( p_demux->out, &fmt );
421 /*****************************************************************************
422 * Close: Close subtitle demux
423 *****************************************************************************/
424 static void Close( vlc_object_t *p_this )
426 demux_t *p_demux = (demux_t*)p_this;
427 demux_sys_t *p_sys = p_demux->p_sys;
430 for( i = 0; i < p_sys->i_subtitles; i++ )
432 if( p_sys->subtitle[i].psz_text )
433 free( p_sys->subtitle[i].psz_text );
435 if( p_sys->subtitle )
436 free( p_sys->subtitle );
441 /*****************************************************************************
443 *****************************************************************************/
444 static int Control( demux_t *p_demux, int i_query, va_list args )
446 demux_sys_t *p_sys = p_demux->p_sys;
452 case DEMUX_GET_LENGTH:
453 pi64 = (int64_t*)va_arg( args, int64_t * );
454 *pi64 = p_sys->i_length;
458 pi64 = (int64_t*)va_arg( args, int64_t * );
459 if( p_sys->i_subtitle < p_sys->i_subtitles )
461 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
467 i64 = (int64_t)va_arg( args, int64_t );
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 )
475 if( p_sys->i_subtitle >= p_sys->i_subtitles )
479 case DEMUX_GET_POSITION:
480 pf = (double*)va_arg( args, double * );
481 if( p_sys->i_subtitle >= p_sys->i_subtitles )
485 else if( p_sys->i_subtitles > 0 )
487 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
488 (double)p_sys->i_length;
496 case DEMUX_SET_POSITION:
497 f = (double)va_arg( args, double );
498 i64 = f * p_sys->i_length;
500 p_sys->i_subtitle = 0;
501 while( p_sys->i_subtitle < p_sys->i_subtitles &&
502 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
506 if( p_sys->i_subtitle >= p_sys->i_subtitles )
510 case DEMUX_SET_NEXT_DEMUX_TIME:
511 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
516 case DEMUX_GET_ATTACHMENTS:
517 case DEMUX_GET_TITLE_INFO:
521 msg_Err( p_demux, "unknown query in subtitle control" );
526 /*****************************************************************************
527 * Demux: Send subtitle to decoder
528 *****************************************************************************/
529 static int Demux( demux_t *p_demux )
531 demux_sys_t *p_sys = p_demux->p_sys;
534 if( p_sys->i_subtitle >= p_sys->i_subtitles )
537 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
538 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
540 /* Should not happen */
541 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
544 while( p_sys->i_subtitle < p_sys->i_subtitles &&
545 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
548 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
557 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
563 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
569 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
570 p_block->i_dts = p_block->i_pts;
571 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
574 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
577 memcpy( p_block->p_buffer,
578 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
579 if( p_block->i_pts > 0 )
581 es_out_Send( p_demux->out, p_sys->es, p_block );
585 block_Release( p_block );
591 p_sys->i_next_demux_date = 0;
596 /*****************************************************************************
597 * Fix: fix time stamp and order of subtitle
598 *****************************************************************************/
599 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
600 static void Fix( demux_t *p_demux )
602 demux_sys_t *p_sys = p_demux->p_sys;
606 /* *** fix order (to be sure...) *** */
607 /* We suppose that there are near in order and this durty bubble sort
608 * wont take too much time
613 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
615 if( p_sys->subtitle[i_index].i_start <
616 p_sys->subtitle[i_index - 1].i_start )
620 p_sys->subtitle + i_index - 1,
621 sizeof( subtitle_t ) );
622 memcpy( p_sys->subtitle + i_index - 1,
623 p_sys->subtitle + i_index,
624 sizeof( subtitle_t ) );
625 memcpy( p_sys->subtitle + i_index,
627 sizeof( subtitle_t ) );
635 static int TextLoad( text_t *txt, stream_t *s )
641 txt->i_line_count = 0;
643 txt->line = calloc( i_line_max, sizeof( char * ) );
645 /* load the complete file */
648 char *psz = stream_ReadLine( s );
653 txt->line[txt->i_line_count++] = psz;
654 if( txt->i_line_count >= i_line_max )
657 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
661 if( txt->i_line_count <= 0 )
669 static void TextUnload( text_t *txt )
673 for( i = 0; i < txt->i_line_count; i++ )
675 free( txt->line[i] );
679 txt->i_line_count = 0;
682 static char *TextGetLine( text_t *txt )
684 if( txt->i_line >= txt->i_line_count )
687 return txt->line[txt->i_line++];
689 static void TextPreviousLine( text_t *txt )
691 if( txt->i_line > 0 )
695 /*****************************************************************************
696 * Specific Subtitle function
697 *****************************************************************************/
700 * {n1}{n2}Line1|Line2|Line3....
701 * where n1 and n2 are the video frame number (n2 can be empty)
703 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
705 demux_sys_t *p_sys = p_demux->p_sys;
706 text_t *txt = &p_sys->txt;
714 const char *s = TextGetLine( txt );
718 psz_text = malloc( strlen(s) + 1 );
724 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
725 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
728 if( i_start != 1 || i_stop != 1 )
731 /* We found a possible setting of the framerate "{1}{1}23.976" */
732 /* Check if it's usable, and if the sub-fps is not set */
733 f_fps = us_strtod( psz_text, NULL );
734 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
735 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
740 /* replace | by \n */
741 for( i = 0; psz_text[i] != '\0'; i++ )
743 if( psz_text[i] == '|' )
748 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
749 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
750 p_subtitle->psz_text = psz_text;
754 /* ParseSubRipSubViewer
757 * h1:m1:s1,d1 --> h2:m2:s2,d2
762 * Format SubViewer v1/v2
763 * h1:m1:s1.d1,h2:m2:s2.d2
768 * We ignore line number for SubRip
770 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
772 vlc_bool_t b_replace_br )
774 demux_sys_t *p_sys = p_demux->p_sys;
775 text_t *txt = &p_sys->txt;
780 const char *s = TextGetLine( txt );
781 int h1, m1, s1, d1, h2, m2, s2, d2;
786 if( sscanf( s, psz_fmt,
788 &h2, &m2, &s2, &d2 ) == 8 )
790 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
791 (int64_t)m1 * 60*1000 +
793 (int64_t)d1 ) * 1000;
795 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
796 (int64_t)m2 * 60*1000 +
798 (int64_t)d2 ) * 1000;
803 /* Now read text until an empty line */
804 psz_text = strdup("");
809 const char *s = TextGetLine( txt );
822 p_subtitle->psz_text = psz_text;
826 i_old = strlen( psz_text );
827 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
830 strcat( psz_text, s );
831 strcat( psz_text, "\n" );
833 /* replace [br] by \n */
838 while( ( p = strstr( psz_text, "[br]" ) ) )
841 memmove( p, &p[3], strlen(&p[3])+1 );
848 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
850 return ParseSubRipSubViewer( p_demux, p_subtitle,
851 "%d:%d:%d,%d --> %d:%d:%d,%d",
856 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
858 return ParseSubRipSubViewer( p_demux, p_subtitle,
859 "%d:%d:%d.%d,%d:%d:%d.%d",
865 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
867 demux_sys_t *p_sys = p_demux->p_sys;
868 text_t *txt = &p_sys->txt;
872 const char *s = TextGetLine( txt );
873 int h1, m1, s1, c1, h2, m2, s2, c2;
879 /* We expect (SSA2-4):
880 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
881 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
883 * SSA-1 is similar but only has 8 commas up untill the subtitle text. Probably the Effect field is no present, but not 100 % sure.
887 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
888 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
890 psz_text = malloc( 2 + strlen( s ) + 1 );
895 "Dialogue: %*[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
900 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
901 /* (Layer comes from ASS specs ... it's empty for SSA.) */
902 if( p_sys->i_type == SUB_TYPE_SSA1 )
904 /* SSA1 has only 8 commas before the text starts, not 9 */
905 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
910 /* ReadOrder, Layer, %s(rest of fields) */
911 memmove( &psz_text[2], psz_text, strlen(psz_text)+1 );
916 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
917 (int64_t)m1 * 60*1000 +
919 (int64_t)c1 * 10 ) * 1000;
920 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
921 (int64_t)m2 * 60*1000 +
923 (int64_t)c2 * 10 ) * 1000;
924 p_subtitle->psz_text = psz_text;
929 /* All the other stuff we add to the header field */
930 if( !p_sys->psz_header )
931 p_sys->psz_header = strdup( "" );
932 if( !p_sys->psz_header )
936 realloc( p_sys->psz_header,
937 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
938 strcat( p_sys->psz_header, s );
939 strcat( p_sys->psz_header, "\n" );
945 * h:m:s:Line1|Line2|Line3....
947 * h:m:s Line1|Line2|Line3....
949 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
951 demux_sys_t *p_sys = p_demux->p_sys;
952 text_t *txt = &p_sys->txt;
958 const char *s = TextGetLine( txt );
964 psz_text = malloc( strlen( s ) + 1 );
968 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
969 &h1, &m1, &s1, psz_text ) == 4 )
971 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
972 (int64_t)m1 * 60*1000 +
973 (int64_t)s1 * 1000 ) * 1000;
974 p_subtitle->i_stop = 0;
980 /* replace | by \n */
981 for( i = 0; psz_text[i] != '\0'; i++ )
983 if( psz_text[i] == '|' )
986 p_subtitle->psz_text = psz_text;
992 static char *ParseSamiSearch( text_t *txt,
993 char *psz_start, const char *psz_str )
995 if( psz_start && strcasestr( psz_start, psz_str ) )
997 char *s = strcasestr( psz_start, psz_str );
998 return &s[strlen( psz_str )];
1003 char *p = TextGetLine( txt );
1007 if( strcasestr( p, psz_str ) )
1009 char *s = strcasestr( p, psz_str );
1010 return &s[strlen( psz_str )];
1014 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1016 demux_sys_t *p_sys = p_demux->p_sys;
1017 text_t *txt = &p_sys->txt;
1022 unsigned int i_text;
1023 char text[8192]; /* Arbitrary but should be long enough */
1025 /* search "Start=" */
1026 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1027 return VLC_EGENERIC;
1029 /* get start value */
1030 i_start = strtol( s, &s, 0 );
1033 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1034 return VLC_EGENERIC;
1037 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1038 return VLC_EGENERIC;
1042 /* now get all txt until a "Start=" line */
1046 /* Search non empty line */
1047 while( s && *s == '\0' )
1048 s = TextGetLine( txt );
1054 if( !strncasecmp( s, "<br", 3 ) )
1058 else if( strcasestr( s, "Start=" ) )
1060 TextPreviousLine( txt );
1063 s = ParseSamiSearch( txt, s, ">" );
1065 else if( !strncmp( s, " ", 6 ) )
1070 else if( *s == '\t' )
1080 if( c != '\0' && i_text+1 < sizeof(text) )
1083 text[i_text] = '\0';
1087 p_subtitle->i_start = i_start * 1000;
1088 p_subtitle->i_stop = 0;
1089 p_subtitle->psz_text = strdup( text );
1101 * TODO it can have a header
1108 * LANG support would be cool
1109 * CODEPAGE is probably mandatory FIXME
1111 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1113 demux_sys_t *p_sys = p_demux->p_sys;
1114 text_t *txt = &p_sys->txt;
1119 const char *s = TextGetLine( txt );
1123 return VLC_EGENERIC;
1127 &h1, &m1, &s1, &c1 ) == 4 )
1129 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1130 (int64_t)m1 * 60*1000 +
1131 (int64_t)s1 * 1000 +
1132 (int64_t)c1 * 10) * 1000;
1133 p_subtitle->i_stop = 0;
1138 /* Now read text until a line containing "}" */
1139 psz_text = strdup("");
1144 const char *s = TextGetLine( txt );
1151 return VLC_EGENERIC;
1154 i_len = strlen( s );
1155 if( i_len == 1 && s[0] == '}')
1157 p_subtitle->psz_text = psz_text;
1161 i_old = strlen( psz_text );
1162 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1165 strcat( psz_text, s );
1166 strcat( psz_text, "\n" );
1172 * [n1][n2]Line1|Line2|Line3...
1173 * where n1 and n2 are the video frame number (n2 can be empty)
1175 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle )
1177 demux_sys_t *p_sys = p_demux->p_sys;
1178 text_t *txt = &p_sys->txt;
1184 const char *s = TextGetLine( txt );
1189 return VLC_EGENERIC;
1191 psz_text = malloc( strlen(s) + 1 );
1197 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1198 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1200 p_subtitle->i_start = (int64_t)i_start * 100000;
1201 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1207 /* replace | by \n */
1208 for( i = 0; psz_text[i] != '\0'; )
1210 if( psz_text[i] == '|' )
1214 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1215 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1219 p_subtitle->psz_text = psz_text;