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>
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
38 #include <vlc_demux.h>
39 #include <vlc_charset.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t *p_this );
45 static void Close( vlc_object_t *p_this );
47 #define SUB_DELAY_LONGTEXT \
48 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
49 #define SUB_FPS_LONGTEXT \
50 N_("Override the normal frames per second settings. " \
51 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
52 #define SUB_TYPE_LONGTEXT \
53 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
54 "\"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
55 "\"sami\", \"dvdsubtitle\", \"mpl2\" and \"auto\" (meaning autodetection, this " \
56 "should always work).")
57 static const char *ppsz_sub_type[] =
59 "auto", "microdvd", "subrip", "subviewer", "ssa1",
60 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2"
64 set_shortname( _("Subtitles"));
65 set_description( _("Text subtitles parser") );
66 set_capability( "demux2", 0 );
67 set_category( CAT_INPUT );
68 set_subcategory( SUBCAT_INPUT_DEMUX );
69 add_float( "sub-fps", 0.0, NULL,
70 N_("Frames per second"),
71 SUB_FPS_LONGTEXT, VLC_TRUE );
73 add_integer( "sub-delay", 0, NULL,
74 N_("Subtitles delay"),
75 SUB_DELAY_LONGTEXT, VLC_TRUE );
77 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
78 SUB_TYPE_LONGTEXT, VLC_TRUE );
79 change_string_list( ppsz_sub_type, NULL, NULL );
81 set_callbacks( Open, Close );
83 add_shortcut( "subtitle" );
86 /*****************************************************************************
88 *****************************************************************************/
91 SUB_TYPE_UNKNOWN = -1,
100 SUB_TYPE_DVDSUBTITLE,
110 static int TextLoad( text_t *, stream_t *s );
111 static void TextUnload( text_t * );
128 int64_t i_next_demux_date;
129 int64_t i_microsecperframe;
134 subtitle_t *subtitle;
139 static int ParseMicroDvd ( demux_t *, subtitle_t * );
140 static int ParseSubRip ( demux_t *, subtitle_t * );
141 static int ParseSubViewer ( demux_t *, subtitle_t * );
142 static int ParseSSA ( demux_t *, subtitle_t * );
143 static int ParseVplayer ( demux_t *, subtitle_t * );
144 static int ParseSami ( demux_t *, subtitle_t * );
145 static int ParseDVDSubtitle( demux_t *, subtitle_t * );
146 static int ParseMPL2 ( demux_t *, subtitle_t * );
150 const char *psz_type_name;
152 const char *psz_name;
153 int (*pf_read)( demux_t *, subtitle_t* );
154 } sub_read_subtitle_function [] =
156 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
157 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
158 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
159 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
160 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
161 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
162 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
163 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
164 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
165 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
166 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
169 static int Demux( demux_t * );
170 static int Control( demux_t *, int, va_list );
172 /*static void Fix( demux_t * );*/
174 /*****************************************************************************
176 *****************************************************************************/
177 static int Open ( vlc_object_t *p_this )
179 demux_t *p_demux = (demux_t*)p_this;
184 int (*pf_read)( demux_t *, subtitle_t* );
187 if( !p_demux->b_force )
189 msg_Dbg( p_demux, "subtitle demux discarded" );
193 p_demux->pf_demux = Demux;
194 p_demux->pf_control = Control;
195 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
196 p_sys->psz_header = NULL;
197 p_sys->i_subtitle = 0;
198 p_sys->i_subtitles = 0;
199 p_sys->subtitle = NULL;
200 p_sys->i_microsecperframe = 40000;
203 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
205 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
207 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
209 /* Check for override of the fps */
210 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
213 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
214 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
217 /* Get or probe the type */
218 p_sys->i_type = SUB_TYPE_UNKNOWN;
219 psz_type = var_CreateGetString( p_demux, "sub-type" );
226 if( sub_read_subtitle_function[i].psz_type_name == NULL )
229 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
232 p_sys->i_type = sub_read_subtitle_function[i].i_type;
239 /* Probe if unknown type */
240 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
245 msg_Dbg( p_demux, "autodetecting subtitle format" );
246 for( i_try = 0; i_try < 256; i_try++ )
250 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
253 if( strcasestr( s, "<SAMI>" ) )
255 p_sys->i_type = SUB_TYPE_SAMI;
258 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
259 sscanf( s, "{%d}{}", &i_dummy ) == 1)
261 p_sys->i_type = SUB_TYPE_MICRODVD;
265 "%d:%d:%d,%d --> %d:%d:%d,%d",
266 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
267 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
269 p_sys->i_type = SUB_TYPE_SUBRIP;
272 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
274 p_sys->i_type = SUB_TYPE_SSA1;
277 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
279 p_sys->i_type = SUB_TYPE_ASS;
282 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
284 p_sys->i_type = SUB_TYPE_SSA2_4;
287 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
289 p_sys->i_type = SUB_TYPE_SSA2_4;
292 else if( !strncasecmp( s, "Dialogue:", 9 ) )
294 p_sys->i_type = SUB_TYPE_ASS;
297 else if( strcasestr( s, "[INFORMATION]" ) )
299 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
302 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
303 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
305 p_sys->i_type = SUB_TYPE_VPLAYER;
308 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
309 &i_dummy, &i_dummy ) == 4 )
311 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
314 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
315 sscanf( s, "[%d][]", &i_dummy ) == 1)
317 p_sys->i_type = SUB_TYPE_MPL2;
327 /* It will nearly always work even for non seekable stream thanks the
328 * caching system, and if it fails we lose just a few sub */
329 if( stream_Seek( p_demux->s, 0 ) )
331 msg_Warn( p_demux, "failed to rewind" );
334 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
336 msg_Err( p_demux, "failed to recognize subtitle type" );
343 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
345 msg_Dbg( p_demux, "detected %s format",
346 sub_read_subtitle_function[i].psz_name );
347 pf_read = sub_read_subtitle_function[i].pf_read;
352 msg_Dbg( p_demux, "loading all subtitles..." );
354 /* Load the whole file */
355 TextLoad( &p_sys->txt, p_demux->s );
360 if( p_sys->i_subtitles >= i_max )
363 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
364 sizeof(subtitle_t) * i_max ) ) )
366 msg_Err( p_demux, "out of memory");
367 if( p_sys->subtitle != NULL )
368 free( p_sys->subtitle );
369 TextUnload( &p_sys->txt );
375 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
378 p_sys->i_subtitles++;
381 TextUnload( &p_sys->txt );
383 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
385 /* Fix subtitle (order and time) *** */
386 p_sys->i_subtitle = 0;
388 if( p_sys->i_subtitles > 0 )
390 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
392 if( p_sys->i_length <= 0 )
393 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
396 /* *** add subtitle ES *** */
397 if( p_sys->i_type == SUB_TYPE_SSA1 ||
398 p_sys->i_type == SUB_TYPE_SSA2_4 ||
399 p_sys->i_type == SUB_TYPE_ASS )
401 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
405 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
407 if( p_sys->psz_header != NULL )
409 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
410 fmt.p_extra = strdup( p_sys->psz_header );
412 p_sys->es = es_out_Add( p_demux->out, &fmt );
417 /*****************************************************************************
418 * Close: Close subtitle demux
419 *****************************************************************************/
420 static void Close( vlc_object_t *p_this )
422 demux_t *p_demux = (demux_t*)p_this;
423 demux_sys_t *p_sys = p_demux->p_sys;
426 for( i = 0; i < p_sys->i_subtitles; i++ )
428 if( p_sys->subtitle[i].psz_text )
429 free( p_sys->subtitle[i].psz_text );
431 if( p_sys->subtitle )
432 free( p_sys->subtitle );
437 /*****************************************************************************
439 *****************************************************************************/
440 static int Control( demux_t *p_demux, int i_query, va_list args )
442 demux_sys_t *p_sys = p_demux->p_sys;
448 case DEMUX_GET_LENGTH:
449 pi64 = (int64_t*)va_arg( args, int64_t * );
450 *pi64 = p_sys->i_length;
454 pi64 = (int64_t*)va_arg( args, int64_t * );
455 if( p_sys->i_subtitle < p_sys->i_subtitles )
457 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
463 i64 = (int64_t)va_arg( args, int64_t );
464 p_sys->i_subtitle = 0;
465 while( p_sys->i_subtitle < p_sys->i_subtitles &&
466 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
471 if( p_sys->i_subtitle >= p_sys->i_subtitles )
475 case DEMUX_GET_POSITION:
476 pf = (double*)va_arg( args, double * );
477 if( p_sys->i_subtitle >= p_sys->i_subtitles )
481 else if( p_sys->i_subtitles > 0 )
483 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
484 (double)p_sys->i_length;
492 case DEMUX_SET_POSITION:
493 f = (double)va_arg( args, double );
494 i64 = f * p_sys->i_length;
496 p_sys->i_subtitle = 0;
497 while( p_sys->i_subtitle < p_sys->i_subtitles &&
498 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
502 if( p_sys->i_subtitle >= p_sys->i_subtitles )
506 case DEMUX_SET_NEXT_DEMUX_TIME:
507 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
512 case DEMUX_GET_ATTACHMENTS:
513 case DEMUX_GET_TITLE_INFO:
514 case DEMUX_HAS_UNSUPPORTED_META:
518 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
523 /*****************************************************************************
524 * Demux: Send subtitle to decoder
525 *****************************************************************************/
526 static int Demux( demux_t *p_demux )
528 demux_sys_t *p_sys = p_demux->p_sys;
531 if( p_sys->i_subtitle >= p_sys->i_subtitles )
534 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
535 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
537 /* Should not happen */
538 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
541 while( p_sys->i_subtitle < p_sys->i_subtitles &&
542 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
545 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
554 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
560 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
566 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
567 p_block->i_dts = p_block->i_pts;
568 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
571 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
574 memcpy( p_block->p_buffer,
575 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
576 if( p_block->i_pts > 0 )
578 es_out_Send( p_demux->out, p_sys->es, p_block );
582 block_Release( p_block );
588 p_sys->i_next_demux_date = 0;
593 /*****************************************************************************
594 * Fix: fix time stamp and order of subtitle
595 *****************************************************************************/
596 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
597 static void Fix( demux_t *p_demux )
599 demux_sys_t *p_sys = p_demux->p_sys;
603 /* *** fix order (to be sure...) *** */
604 /* We suppose that there are near in order and this durty bubble sort
605 * wont take too much time
610 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
612 if( p_sys->subtitle[i_index].i_start <
613 p_sys->subtitle[i_index - 1].i_start )
617 p_sys->subtitle + i_index - 1,
618 sizeof( subtitle_t ) );
619 memcpy( p_sys->subtitle + i_index - 1,
620 p_sys->subtitle + i_index,
621 sizeof( subtitle_t ) );
622 memcpy( p_sys->subtitle + i_index,
624 sizeof( subtitle_t ) );
632 static int TextLoad( text_t *txt, stream_t *s )
638 txt->i_line_count = 0;
640 txt->line = calloc( i_line_max, sizeof( char * ) );
642 /* load the complete file */
645 char *psz = stream_ReadLine( s );
650 txt->line[txt->i_line_count++] = psz;
651 if( txt->i_line_count >= i_line_max )
654 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
658 if( txt->i_line_count <= 0 )
666 static void TextUnload( text_t *txt )
670 for( i = 0; i < txt->i_line_count; i++ )
672 free( txt->line[i] );
676 txt->i_line_count = 0;
679 static char *TextGetLine( text_t *txt )
681 if( txt->i_line >= txt->i_line_count )
684 return txt->line[txt->i_line++];
686 static void TextPreviousLine( text_t *txt )
688 if( txt->i_line > 0 )
692 /*****************************************************************************
693 * Specific Subtitle function
694 *****************************************************************************/
697 * {n1}{n2}Line1|Line2|Line3....
698 * where n1 and n2 are the video frame number (n2 can be empty)
700 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
702 demux_sys_t *p_sys = p_demux->p_sys;
703 text_t *txt = &p_sys->txt;
711 const char *s = TextGetLine( txt );
715 psz_text = malloc( strlen(s) + 1 );
721 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
722 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
725 if( i_start != 1 || i_stop != 1 )
728 /* We found a possible setting of the framerate "{1}{1}23.976" */
729 /* Check if it's usable, and if the sub-fps is not set */
730 f_fps = us_strtod( psz_text, NULL );
731 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
732 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
737 /* replace | by \n */
738 for( i = 0; psz_text[i] != '\0'; i++ )
740 if( psz_text[i] == '|' )
745 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
746 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
747 p_subtitle->psz_text = psz_text;
751 /* ParseSubRipSubViewer
754 * h1:m1:s1,d1 --> h2:m2:s2,d2
759 * Format SubViewer v1/v2
760 * h1:m1:s1.d1,h2:m2:s2.d2
765 * We ignore line number for SubRip
767 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
769 vlc_bool_t b_replace_br )
771 demux_sys_t *p_sys = p_demux->p_sys;
772 text_t *txt = &p_sys->txt;
777 const char *s = TextGetLine( txt );
778 int h1, m1, s1, d1, h2, m2, s2, d2;
783 if( sscanf( s, psz_fmt,
785 &h2, &m2, &s2, &d2 ) == 8 )
787 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
788 (int64_t)m1 * 60*1000 +
790 (int64_t)d1 ) * 1000;
792 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
793 (int64_t)m2 * 60*1000 +
795 (int64_t)d2 ) * 1000;
800 /* Now read text until an empty line */
801 psz_text = strdup("");
806 const char *s = TextGetLine( txt );
819 p_subtitle->psz_text = psz_text;
823 i_old = strlen( psz_text );
824 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
827 strcat( psz_text, s );
828 strcat( psz_text, "\n" );
830 /* replace [br] by \n */
835 while( ( p = strstr( psz_text, "[br]" ) ) )
838 memmove( p, &p[3], strlen(&p[3])+1 );
845 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
847 return ParseSubRipSubViewer( p_demux, p_subtitle,
848 "%d:%d:%d,%d --> %d:%d:%d,%d",
853 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
855 return ParseSubRipSubViewer( p_demux, p_subtitle,
856 "%d:%d:%d.%d,%d:%d:%d.%d",
862 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
864 demux_sys_t *p_sys = p_demux->p_sys;
865 text_t *txt = &p_sys->txt;
869 const char *s = TextGetLine( txt );
870 int h1, m1, s1, c1, h2, m2, s2, c2;
876 /* We expect (SSA2-4):
877 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
878 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
880 * 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.
884 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
885 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
887 psz_text = malloc( 2 + strlen( s ) + 1 );
892 "Dialogue: %*[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
897 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
898 /* (Layer comes from ASS specs ... it's empty for SSA.) */
899 if( p_sys->i_type == SUB_TYPE_SSA1 )
901 /* SSA1 has only 8 commas before the text starts, not 9 */
902 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
907 /* ReadOrder, Layer, %s(rest of fields) */
908 memmove( &psz_text[2], psz_text, strlen(psz_text)+1 );
913 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
914 (int64_t)m1 * 60*1000 +
916 (int64_t)c1 * 10 ) * 1000;
917 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
918 (int64_t)m2 * 60*1000 +
920 (int64_t)c2 * 10 ) * 1000;
921 p_subtitle->psz_text = psz_text;
926 /* All the other stuff we add to the header field */
927 if( !p_sys->psz_header )
928 p_sys->psz_header = strdup( "" );
929 if( !p_sys->psz_header )
933 realloc( p_sys->psz_header,
934 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
935 strcat( p_sys->psz_header, s );
936 strcat( p_sys->psz_header, "\n" );
942 * h:m:s:Line1|Line2|Line3....
944 * h:m:s Line1|Line2|Line3....
946 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
948 demux_sys_t *p_sys = p_demux->p_sys;
949 text_t *txt = &p_sys->txt;
955 const char *s = TextGetLine( txt );
961 psz_text = malloc( strlen( s ) + 1 );
965 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
966 &h1, &m1, &s1, psz_text ) == 4 )
968 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
969 (int64_t)m1 * 60*1000 +
970 (int64_t)s1 * 1000 ) * 1000;
971 p_subtitle->i_stop = 0;
977 /* replace | by \n */
978 for( i = 0; psz_text[i] != '\0'; i++ )
980 if( psz_text[i] == '|' )
983 p_subtitle->psz_text = psz_text;
989 static char *ParseSamiSearch( text_t *txt,
990 char *psz_start, const char *psz_str )
992 if( psz_start && strcasestr( psz_start, psz_str ) )
994 char *s = strcasestr( psz_start, psz_str );
995 return &s[strlen( psz_str )];
1000 char *p = TextGetLine( txt );
1004 if( strcasestr( p, psz_str ) )
1006 char *s = strcasestr( p, psz_str );
1007 return &s[strlen( psz_str )];
1011 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1013 demux_sys_t *p_sys = p_demux->p_sys;
1014 text_t *txt = &p_sys->txt;
1019 unsigned int i_text;
1020 char text[8192]; /* Arbitrary but should be long enough */
1022 /* search "Start=" */
1023 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1024 return VLC_EGENERIC;
1026 /* get start value */
1027 i_start = strtol( s, &s, 0 );
1030 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1031 return VLC_EGENERIC;
1034 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1035 return VLC_EGENERIC;
1039 /* now get all txt until a "Start=" line */
1043 /* Search non empty line */
1044 while( s && *s == '\0' )
1045 s = TextGetLine( txt );
1051 if( !strncasecmp( s, "<br", 3 ) )
1055 else if( strcasestr( s, "Start=" ) )
1057 TextPreviousLine( txt );
1060 s = ParseSamiSearch( txt, s, ">" );
1062 else if( !strncmp( s, " ", 6 ) )
1067 else if( *s == '\t' )
1077 if( c != '\0' && i_text+1 < sizeof(text) )
1080 text[i_text] = '\0';
1084 p_subtitle->i_start = i_start * 1000;
1085 p_subtitle->i_stop = 0;
1086 p_subtitle->psz_text = strdup( text );
1098 * TODO it can have a header
1105 * LANG support would be cool
1106 * CODEPAGE is probably mandatory FIXME
1108 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1110 demux_sys_t *p_sys = p_demux->p_sys;
1111 text_t *txt = &p_sys->txt;
1116 const char *s = TextGetLine( txt );
1120 return VLC_EGENERIC;
1124 &h1, &m1, &s1, &c1 ) == 4 )
1126 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1127 (int64_t)m1 * 60*1000 +
1128 (int64_t)s1 * 1000 +
1129 (int64_t)c1 * 10) * 1000;
1130 p_subtitle->i_stop = 0;
1135 /* Now read text until a line containing "}" */
1136 psz_text = strdup("");
1141 const char *s = TextGetLine( txt );
1148 return VLC_EGENERIC;
1151 i_len = strlen( s );
1152 if( i_len == 1 && s[0] == '}')
1154 p_subtitle->psz_text = psz_text;
1158 i_old = strlen( psz_text );
1159 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1162 strcat( psz_text, s );
1163 strcat( psz_text, "\n" );
1169 * [n1][n2]Line1|Line2|Line3...
1170 * where n1 and n2 are the video frame number (n2 can be empty)
1172 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle )
1174 demux_sys_t *p_sys = p_demux->p_sys;
1175 text_t *txt = &p_sys->txt;
1181 const char *s = TextGetLine( txt );
1186 return VLC_EGENERIC;
1188 psz_text = malloc( strlen(s) + 1 );
1194 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1195 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1197 p_subtitle->i_start = (int64_t)i_start * 100000;
1198 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1204 /* replace | by \n */
1205 for( i = 0; psz_text[i] != '\0'; )
1207 if( psz_text[i] == '|' )
1211 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1212 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1216 p_subtitle->psz_text = psz_text;