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 *****************************************************************************/
33 #include <vlc_input.h>
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
42 #include <vlc_demux.h>
43 #include <vlc_charset.h>
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 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
53 #define SUB_FPS_LONGTEXT \
54 N_("Override the normal frames per second settings. " \
55 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
56 #define SUB_TYPE_LONGTEXT \
57 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
58 "\"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
59 "\"sami\", \"dvdsubtitle\", \"mpl2\" and \"auto\" (meaning autodetection, this " \
60 "should always work).")
61 static const char *ppsz_sub_type[] =
63 "auto", "microdvd", "subrip", "subviewer", "ssa1",
64 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2"
68 set_shortname( _("Subtitles"));
69 set_description( _("Text subtitles parser") );
70 set_capability( "demux2", 0 );
71 set_category( CAT_INPUT );
72 set_subcategory( SUBCAT_INPUT_DEMUX );
73 add_float( "sub-fps", 0.0, NULL,
74 N_("Frames per second"),
75 SUB_FPS_LONGTEXT, VLC_TRUE );
76 add_integer( "sub-delay", 0, NULL,
77 N_("Subtitles delay"),
78 SUB_DELAY_LONGTEXT, VLC_TRUE );
79 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
80 SUB_TYPE_LONGTEXT, VLC_TRUE );
81 change_string_list( ppsz_sub_type, NULL, NULL );
82 set_callbacks( Open, Close );
84 add_shortcut( "subtitle" );
87 /*****************************************************************************
89 *****************************************************************************/
92 SUB_TYPE_UNKNOWN = -1,
101 SUB_TYPE_DVDSUBTITLE,
111 static int TextLoad( text_t *, stream_t *s );
112 static void TextUnload( text_t * );
129 int64_t i_next_demux_date;
130 int64_t i_microsecperframe;
135 subtitle_t *subtitle;
140 static int ParseMicroDvd ( demux_t *, subtitle_t * );
141 static int ParseSubRip ( demux_t *, subtitle_t * );
142 static int ParseSubViewer ( demux_t *, subtitle_t * );
143 static int ParseSSA ( demux_t *, subtitle_t * );
144 static int ParseVplayer ( demux_t *, subtitle_t * );
145 static int ParseSami ( demux_t *, subtitle_t * );
146 static int ParseDVDSubtitle( demux_t *, subtitle_t * );
147 static int ParseMPL2 ( demux_t *, subtitle_t * );
151 const char *psz_type_name;
153 const char *psz_name;
154 int (*pf_read)( demux_t *, subtitle_t* );
155 } sub_read_subtitle_function [] =
157 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
158 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
159 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
160 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
161 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
162 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
163 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
164 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
165 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
166 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
167 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
170 static int Demux( demux_t * );
171 static int Control( demux_t *, int, va_list );
173 /*static void Fix( demux_t * );*/
175 /*****************************************************************************
177 *****************************************************************************/
178 static int Open ( vlc_object_t *p_this )
180 demux_t *p_demux = (demux_t*)p_this;
185 int (*pf_read)( demux_t *, subtitle_t* );
188 if( !p_demux->b_force )
190 msg_Dbg( p_demux, "subtitle demux discarded" );
194 p_demux->pf_demux = Demux;
195 p_demux->pf_control = Control;
196 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
197 p_sys->psz_header = NULL;
198 p_sys->i_subtitle = 0;
199 p_sys->i_subtitles = 0;
200 p_sys->subtitle = NULL;
201 p_sys->i_microsecperframe = 40000;
204 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
206 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
208 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
210 /* Check for override of the fps */
211 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
214 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
215 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
218 /* Get or probe the type */
219 p_sys->i_type = SUB_TYPE_UNKNOWN;
220 psz_type = var_CreateGetString( p_demux, "sub-type" );
227 if( sub_read_subtitle_function[i].psz_type_name == NULL )
230 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
233 p_sys->i_type = sub_read_subtitle_function[i].i_type;
240 /* Probe if unknown type */
241 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
246 msg_Dbg( p_demux, "autodetecting subtitle format" );
247 for( i_try = 0; i_try < 256; i_try++ )
251 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
254 if( strcasestr( s, "<SAMI>" ) )
256 p_sys->i_type = SUB_TYPE_SAMI;
259 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
260 sscanf( s, "{%d}{}", &i_dummy ) == 1)
262 p_sys->i_type = SUB_TYPE_MICRODVD;
266 "%d:%d:%d,%d --> %d:%d:%d,%d",
267 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
268 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
270 p_sys->i_type = SUB_TYPE_SUBRIP;
273 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
275 p_sys->i_type = SUB_TYPE_SSA1;
278 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
280 p_sys->i_type = SUB_TYPE_ASS;
283 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
285 p_sys->i_type = SUB_TYPE_SSA2_4;
288 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
290 p_sys->i_type = SUB_TYPE_SSA2_4;
293 else if( !strncasecmp( s, "Dialogue:", 9 ) )
295 p_sys->i_type = SUB_TYPE_ASS;
298 else if( strcasestr( s, "[INFORMATION]" ) )
300 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
303 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
304 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
306 p_sys->i_type = SUB_TYPE_VPLAYER;
309 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
310 &i_dummy, &i_dummy ) == 4 )
312 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
315 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
316 sscanf( s, "[%d][]", &i_dummy ) == 1)
318 p_sys->i_type = SUB_TYPE_MPL2;
328 /* It will nearly always work even for non seekable stream thanks the
329 * caching system, and if it fails we lose just a few sub */
330 if( stream_Seek( p_demux->s, 0 ) )
332 msg_Warn( p_demux, "failed to rewind" );
335 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
337 msg_Err( p_demux, "failed to recognize subtitle type" );
344 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
346 msg_Dbg( p_demux, "detected %s format",
347 sub_read_subtitle_function[i].psz_name );
348 pf_read = sub_read_subtitle_function[i].pf_read;
353 msg_Dbg( p_demux, "loading all subtitles..." );
355 /* Load the whole file */
356 TextLoad( &p_sys->txt, p_demux->s );
361 if( p_sys->i_subtitles >= i_max )
364 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
365 sizeof(subtitle_t) * i_max ) ) )
367 msg_Err( p_demux, "out of memory");
368 if( p_sys->subtitle != NULL )
369 free( p_sys->subtitle );
370 TextUnload( &p_sys->txt );
376 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
379 p_sys->i_subtitles++;
382 TextUnload( &p_sys->txt );
384 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
386 /* Fix subtitle (order and time) *** */
387 p_sys->i_subtitle = 0;
389 if( p_sys->i_subtitles > 0 )
391 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
393 if( p_sys->i_length <= 0 )
394 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
397 /* *** add subtitle ES *** */
398 if( p_sys->i_type == SUB_TYPE_SSA1 ||
399 p_sys->i_type == SUB_TYPE_SSA2_4 ||
400 p_sys->i_type == SUB_TYPE_ASS )
402 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
406 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
408 if( p_sys->psz_header != NULL )
410 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
411 fmt.p_extra = strdup( p_sys->psz_header );
413 p_sys->es = es_out_Add( p_demux->out, &fmt );
418 /*****************************************************************************
419 * Close: Close subtitle demux
420 *****************************************************************************/
421 static void Close( vlc_object_t *p_this )
423 demux_t *p_demux = (demux_t*)p_this;
424 demux_sys_t *p_sys = p_demux->p_sys;
427 for( i = 0; i < p_sys->i_subtitles; i++ )
429 if( p_sys->subtitle[i].psz_text )
430 free( p_sys->subtitle[i].psz_text );
432 if( p_sys->subtitle )
433 free( p_sys->subtitle );
438 /*****************************************************************************
440 *****************************************************************************/
441 static int Control( demux_t *p_demux, int i_query, va_list args )
443 demux_sys_t *p_sys = p_demux->p_sys;
449 case DEMUX_GET_LENGTH:
450 pi64 = (int64_t*)va_arg( args, int64_t * );
451 *pi64 = p_sys->i_length;
455 pi64 = (int64_t*)va_arg( args, int64_t * );
456 if( p_sys->i_subtitle < p_sys->i_subtitles )
458 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
464 i64 = (int64_t)va_arg( args, int64_t );
465 p_sys->i_subtitle = 0;
466 while( p_sys->i_subtitle < p_sys->i_subtitles &&
467 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
472 if( p_sys->i_subtitle >= p_sys->i_subtitles )
476 case DEMUX_GET_POSITION:
477 pf = (double*)va_arg( args, double * );
478 if( p_sys->i_subtitle >= p_sys->i_subtitles )
482 else if( p_sys->i_subtitles > 0 )
484 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
485 (double)p_sys->i_length;
493 case DEMUX_SET_POSITION:
494 f = (double)va_arg( args, double );
495 i64 = f * p_sys->i_length;
497 p_sys->i_subtitle = 0;
498 while( p_sys->i_subtitle < p_sys->i_subtitles &&
499 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
503 if( p_sys->i_subtitle >= p_sys->i_subtitles )
507 case DEMUX_SET_NEXT_DEMUX_TIME:
508 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
513 case DEMUX_GET_ATTACHMENTS:
514 case DEMUX_GET_TITLE_INFO:
515 case DEMUX_HAS_UNSUPPORTED_META:
519 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
524 /*****************************************************************************
525 * Demux: Send subtitle to decoder
526 *****************************************************************************/
527 static int Demux( demux_t *p_demux )
529 demux_sys_t *p_sys = p_demux->p_sys;
532 if( p_sys->i_subtitle >= p_sys->i_subtitles )
535 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
536 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
538 /* Should not happen */
539 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
542 while( p_sys->i_subtitle < p_sys->i_subtitles &&
543 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
546 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
555 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
561 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
567 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
568 p_block->i_dts = p_block->i_pts;
569 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
572 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
575 memcpy( p_block->p_buffer,
576 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
577 if( p_block->i_pts > 0 )
579 es_out_Send( p_demux->out, p_sys->es, p_block );
583 block_Release( p_block );
589 p_sys->i_next_demux_date = 0;
594 /*****************************************************************************
595 * Fix: fix time stamp and order of subtitle
596 *****************************************************************************/
597 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
598 static void Fix( demux_t *p_demux )
600 demux_sys_t *p_sys = p_demux->p_sys;
604 /* *** fix order (to be sure...) *** */
605 /* We suppose that there are near in order and this durty bubble sort
606 * wont take too much time
611 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
613 if( p_sys->subtitle[i_index].i_start <
614 p_sys->subtitle[i_index - 1].i_start )
618 p_sys->subtitle + i_index - 1,
619 sizeof( subtitle_t ) );
620 memcpy( p_sys->subtitle + i_index - 1,
621 p_sys->subtitle + i_index,
622 sizeof( subtitle_t ) );
623 memcpy( p_sys->subtitle + i_index,
625 sizeof( subtitle_t ) );
633 static int TextLoad( text_t *txt, stream_t *s )
639 txt->i_line_count = 0;
641 txt->line = calloc( i_line_max, sizeof( char * ) );
643 /* load the complete file */
646 char *psz = stream_ReadLine( s );
651 txt->line[txt->i_line_count++] = psz;
652 if( txt->i_line_count >= i_line_max )
655 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
659 if( txt->i_line_count <= 0 )
667 static void TextUnload( text_t *txt )
671 for( i = 0; i < txt->i_line_count; i++ )
673 free( txt->line[i] );
677 txt->i_line_count = 0;
680 static char *TextGetLine( text_t *txt )
682 if( txt->i_line >= txt->i_line_count )
685 return txt->line[txt->i_line++];
687 static void TextPreviousLine( text_t *txt )
689 if( txt->i_line > 0 )
693 /*****************************************************************************
694 * Specific Subtitle function
695 *****************************************************************************/
698 * {n1}{n2}Line1|Line2|Line3....
699 * where n1 and n2 are the video frame number (n2 can be empty)
701 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
703 demux_sys_t *p_sys = p_demux->p_sys;
704 text_t *txt = &p_sys->txt;
712 const char *s = TextGetLine( txt );
716 psz_text = malloc( strlen(s) + 1 );
722 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
723 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
726 if( i_start != 1 || i_stop != 1 )
729 /* We found a possible setting of the framerate "{1}{1}23.976" */
730 /* Check if it's usable, and if the sub-fps is not set */
731 f_fps = us_strtod( psz_text, NULL );
732 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
733 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
738 /* replace | by \n */
739 for( i = 0; psz_text[i] != '\0'; i++ )
741 if( psz_text[i] == '|' )
746 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
747 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
748 p_subtitle->psz_text = psz_text;
752 /* ParseSubRipSubViewer
755 * h1:m1:s1,d1 --> h2:m2:s2,d2
760 * Format SubViewer v1/v2
761 * h1:m1:s1.d1,h2:m2:s2.d2
766 * We ignore line number for SubRip
768 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
770 vlc_bool_t b_replace_br )
772 demux_sys_t *p_sys = p_demux->p_sys;
773 text_t *txt = &p_sys->txt;
778 const char *s = TextGetLine( txt );
779 int h1, m1, s1, d1, h2, m2, s2, d2;
784 if( sscanf( s, psz_fmt,
786 &h2, &m2, &s2, &d2 ) == 8 )
788 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
789 (int64_t)m1 * 60*1000 +
791 (int64_t)d1 ) * 1000;
793 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
794 (int64_t)m2 * 60*1000 +
796 (int64_t)d2 ) * 1000;
801 /* Now read text until an empty line */
802 psz_text = strdup("");
807 const char *s = TextGetLine( txt );
820 p_subtitle->psz_text = psz_text;
824 i_old = strlen( psz_text );
825 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
828 strcat( psz_text, s );
829 strcat( psz_text, "\n" );
831 /* replace [br] by \n */
836 while( ( p = strstr( psz_text, "[br]" ) ) )
839 memmove( p, &p[3], strlen(&p[3])+1 );
846 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
848 return ParseSubRipSubViewer( p_demux, p_subtitle,
849 "%d:%d:%d,%d --> %d:%d:%d,%d",
854 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
856 return ParseSubRipSubViewer( p_demux, p_subtitle,
857 "%d:%d:%d.%d,%d:%d:%d.%d",
863 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
865 demux_sys_t *p_sys = p_demux->p_sys;
866 text_t *txt = &p_sys->txt;
870 const char *s = TextGetLine( txt );
871 int h1, m1, s1, c1, h2, m2, s2, c2;
877 /* We expect (SSA2-4):
878 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
879 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
881 * 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.
885 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
886 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
888 psz_text = malloc( 2 + strlen( s ) + 1 );
893 "Dialogue: %*[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
898 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
899 /* (Layer comes from ASS specs ... it's empty for SSA.) */
900 if( p_sys->i_type == SUB_TYPE_SSA1 )
902 /* SSA1 has only 8 commas before the text starts, not 9 */
903 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
908 /* ReadOrder, Layer, %s(rest of fields) */
909 memmove( &psz_text[2], psz_text, strlen(psz_text)+1 );
914 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
915 (int64_t)m1 * 60*1000 +
917 (int64_t)c1 * 10 ) * 1000;
918 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
919 (int64_t)m2 * 60*1000 +
921 (int64_t)c2 * 10 ) * 1000;
922 p_subtitle->psz_text = psz_text;
927 /* All the other stuff we add to the header field */
928 if( !p_sys->psz_header )
929 p_sys->psz_header = strdup( "" );
930 if( !p_sys->psz_header )
934 realloc( p_sys->psz_header,
935 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
936 strcat( p_sys->psz_header, s );
937 strcat( p_sys->psz_header, "\n" );
943 * h:m:s:Line1|Line2|Line3....
945 * h:m:s Line1|Line2|Line3....
947 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
949 demux_sys_t *p_sys = p_demux->p_sys;
950 text_t *txt = &p_sys->txt;
956 const char *s = TextGetLine( txt );
962 psz_text = malloc( strlen( s ) + 1 );
966 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
967 &h1, &m1, &s1, psz_text ) == 4 )
969 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
970 (int64_t)m1 * 60*1000 +
971 (int64_t)s1 * 1000 ) * 1000;
972 p_subtitle->i_stop = 0;
978 /* replace | by \n */
979 for( i = 0; psz_text[i] != '\0'; i++ )
981 if( psz_text[i] == '|' )
984 p_subtitle->psz_text = psz_text;
990 static char *ParseSamiSearch( text_t *txt,
991 char *psz_start, const char *psz_str )
993 if( psz_start && strcasestr( psz_start, psz_str ) )
995 char *s = strcasestr( psz_start, psz_str );
996 return &s[strlen( psz_str )];
1001 char *p = TextGetLine( txt );
1005 if( strcasestr( p, psz_str ) )
1007 char *s = strcasestr( p, psz_str );
1008 return &s[strlen( psz_str )];
1012 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1014 demux_sys_t *p_sys = p_demux->p_sys;
1015 text_t *txt = &p_sys->txt;
1020 unsigned int i_text;
1021 char text[8192]; /* Arbitrary but should be long enough */
1023 /* search "Start=" */
1024 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1025 return VLC_EGENERIC;
1027 /* get start value */
1028 i_start = strtol( s, &s, 0 );
1031 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1032 return VLC_EGENERIC;
1035 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1036 return VLC_EGENERIC;
1040 /* now get all txt until a "Start=" line */
1044 /* Search non empty line */
1045 while( s && *s == '\0' )
1046 s = TextGetLine( txt );
1052 if( !strncasecmp( s, "<br", 3 ) )
1056 else if( strcasestr( s, "Start=" ) )
1058 TextPreviousLine( txt );
1061 s = ParseSamiSearch( txt, s, ">" );
1063 else if( !strncmp( s, " ", 6 ) )
1068 else if( *s == '\t' )
1078 if( c != '\0' && i_text+1 < sizeof(text) )
1081 text[i_text] = '\0';
1085 p_subtitle->i_start = i_start * 1000;
1086 p_subtitle->i_stop = 0;
1087 p_subtitle->psz_text = strdup( text );
1099 * TODO it can have a header
1106 * LANG support would be cool
1107 * CODEPAGE is probably mandatory FIXME
1109 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1111 demux_sys_t *p_sys = p_demux->p_sys;
1112 text_t *txt = &p_sys->txt;
1117 const char *s = TextGetLine( txt );
1121 return VLC_EGENERIC;
1125 &h1, &m1, &s1, &c1 ) == 4 )
1127 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1128 (int64_t)m1 * 60*1000 +
1129 (int64_t)s1 * 1000 +
1130 (int64_t)c1 * 10) * 1000;
1131 p_subtitle->i_stop = 0;
1136 /* Now read text until a line containing "}" */
1137 psz_text = strdup("");
1142 const char *s = TextGetLine( txt );
1149 return VLC_EGENERIC;
1152 i_len = strlen( s );
1153 if( i_len == 1 && s[0] == '}')
1155 p_subtitle->psz_text = psz_text;
1159 i_old = strlen( psz_text );
1160 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1163 strcat( psz_text, s );
1164 strcat( psz_text, "\n" );
1170 * [n1][n2]Line1|Line2|Line3...
1171 * where n1 and n2 are the video frame number (n2 can be empty)
1173 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle )
1175 demux_sys_t *p_sys = p_demux->p_sys;
1176 text_t *txt = &p_sys->txt;
1182 const char *s = TextGetLine( txt );
1187 return VLC_EGENERIC;
1189 psz_text = malloc( strlen(s) + 1 );
1195 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1196 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1198 p_subtitle->i_start = (int64_t)i_start * 100000;
1199 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1205 /* replace | by \n */
1206 for( i = 0; psz_text[i] != '\0'; )
1208 if( psz_text[i] == '|' )
1212 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1213 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1217 p_subtitle->psz_text = psz_text;