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 );
72 add_integer( "sub-delay", 0, NULL,
73 N_("Subtitles delay"),
74 SUB_DELAY_LONGTEXT, VLC_TRUE );
75 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
76 SUB_TYPE_LONGTEXT, VLC_TRUE );
77 change_string_list( ppsz_sub_type, NULL, NULL );
78 set_callbacks( Open, Close );
80 add_shortcut( "subtitle" );
83 /*****************************************************************************
85 *****************************************************************************/
88 SUB_TYPE_UNKNOWN = -1,
107 static int TextLoad( text_t *, stream_t *s );
108 static void TextUnload( text_t * );
125 int64_t i_next_demux_date;
126 int64_t i_microsecperframe;
131 subtitle_t *subtitle;
136 static int ParseMicroDvd ( demux_t *, subtitle_t * );
137 static int ParseSubRip ( demux_t *, subtitle_t * );
138 static int ParseSubViewer ( demux_t *, subtitle_t * );
139 static int ParseSSA ( demux_t *, subtitle_t * );
140 static int ParseVplayer ( demux_t *, subtitle_t * );
141 static int ParseSami ( demux_t *, subtitle_t * );
142 static int ParseDVDSubtitle( demux_t *, subtitle_t * );
143 static int ParseMPL2 ( demux_t *, subtitle_t * );
147 const char *psz_type_name;
149 const char *psz_name;
150 int (*pf_read)( demux_t *, subtitle_t* );
151 } sub_read_subtitle_function [] =
153 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
154 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
155 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
156 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
157 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
158 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
159 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
160 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
161 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
162 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
163 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
166 static int Demux( demux_t * );
167 static int Control( demux_t *, int, va_list );
169 /*static void Fix( demux_t * );*/
171 /*****************************************************************************
173 *****************************************************************************/
174 static int Open ( vlc_object_t *p_this )
176 demux_t *p_demux = (demux_t*)p_this;
179 input_thread_t *p_input;
182 int (*pf_read)( demux_t *, subtitle_t* );
185 if( strcmp( p_demux->psz_demux, "subtitle" ) )
187 msg_Dbg( p_demux, "subtitle demux discarded" );
191 p_demux->pf_demux = Demux;
192 p_demux->pf_control = Control;
193 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
194 p_sys->psz_header = NULL;
195 p_sys->i_subtitle = 0;
196 p_sys->i_subtitles = 0;
197 p_sys->subtitle = NULL;
198 p_sys->i_microsecperframe = 40000;
201 p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
204 f_fps = var_GetFloat( p_input, "sub-original-fps" );
206 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
208 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
209 vlc_object_release( p_input );
212 /* Check for override of the fps */
213 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
216 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
217 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
220 /* Get or probe the type */
221 p_sys->i_type = SUB_TYPE_UNKNOWN;
222 psz_type = var_CreateGetString( p_demux, "sub-type" );
229 if( sub_read_subtitle_function[i].psz_type_name == NULL )
232 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
235 p_sys->i_type = sub_read_subtitle_function[i].i_type;
242 /* Probe if unknown type */
243 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
248 msg_Dbg( p_demux, "autodetecting subtitle format" );
249 for( i_try = 0; i_try < 256; i_try++ )
253 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
256 if( strcasestr( s, "<SAMI>" ) )
258 p_sys->i_type = SUB_TYPE_SAMI;
261 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
262 sscanf( s, "{%d}{}", &i_dummy ) == 1)
264 p_sys->i_type = SUB_TYPE_MICRODVD;
268 "%d:%d:%d,%d --> %d:%d:%d,%d",
269 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
270 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
272 p_sys->i_type = SUB_TYPE_SUBRIP;
275 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
277 p_sys->i_type = SUB_TYPE_SSA1;
280 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
282 p_sys->i_type = SUB_TYPE_ASS;
285 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
287 p_sys->i_type = SUB_TYPE_SSA2_4;
290 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
292 p_sys->i_type = SUB_TYPE_SSA2_4;
295 else if( !strncasecmp( s, "Dialogue:", 9 ) )
297 p_sys->i_type = SUB_TYPE_ASS;
300 else if( strcasestr( s, "[INFORMATION]" ) )
302 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
305 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
306 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
308 p_sys->i_type = SUB_TYPE_VPLAYER;
311 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
312 &i_dummy, &i_dummy ) == 4 )
314 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
317 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
318 sscanf( s, "[%d][]", &i_dummy ) == 1)
320 p_sys->i_type = SUB_TYPE_MPL2;
330 /* It will nearly always work even for non seekable stream thanks the
331 * caching system, and if it fails we lose just a few sub */
332 if( stream_Seek( p_demux->s, 0 ) )
334 msg_Warn( p_demux, "failed to rewind" );
337 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
339 msg_Err( p_demux, "failed to recognize subtitle type" );
346 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
348 msg_Dbg( p_demux, "detected %s format",
349 sub_read_subtitle_function[i].psz_name );
350 pf_read = sub_read_subtitle_function[i].pf_read;
355 msg_Dbg( p_demux, "loading all subtitles..." );
357 /* Load the whole file */
358 TextLoad( &p_sys->txt, p_demux->s );
363 if( p_sys->i_subtitles >= i_max )
366 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
367 sizeof(subtitle_t) * i_max ) ) )
369 msg_Err( p_demux, "out of memory");
370 if( p_sys->subtitle != NULL )
371 free( p_sys->subtitle );
372 TextUnload( &p_sys->txt );
378 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
381 p_sys->i_subtitles++;
384 TextUnload( &p_sys->txt );
386 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
388 /* Fix subtitle (order and time) *** */
389 p_sys->i_subtitle = 0;
391 if( p_sys->i_subtitles > 0 )
393 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
395 if( p_sys->i_length <= 0 )
396 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
399 /* *** add subtitle ES *** */
400 if( p_sys->i_type == SUB_TYPE_SSA1 ||
401 p_sys->i_type == SUB_TYPE_SSA2_4 ||
402 p_sys->i_type == SUB_TYPE_ASS )
404 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
408 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
410 if( p_sys->psz_header != NULL )
412 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
413 fmt.p_extra = strdup( p_sys->psz_header );
415 p_sys->es = es_out_Add( p_demux->out, &fmt );
420 /*****************************************************************************
421 * Close: Close subtitle demux
422 *****************************************************************************/
423 static void Close( vlc_object_t *p_this )
425 demux_t *p_demux = (demux_t*)p_this;
426 demux_sys_t *p_sys = p_demux->p_sys;
429 for( i = 0; i < p_sys->i_subtitles; i++ )
431 if( p_sys->subtitle[i].psz_text )
432 free( p_sys->subtitle[i].psz_text );
434 if( p_sys->subtitle )
435 free( p_sys->subtitle );
440 /*****************************************************************************
442 *****************************************************************************/
443 static int Control( demux_t *p_demux, int i_query, va_list args )
445 demux_sys_t *p_sys = p_demux->p_sys;
451 case DEMUX_GET_LENGTH:
452 pi64 = (int64_t*)va_arg( args, int64_t * );
453 *pi64 = p_sys->i_length;
457 pi64 = (int64_t*)va_arg( args, int64_t * );
458 if( p_sys->i_subtitle < p_sys->i_subtitles )
460 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
466 i64 = (int64_t)va_arg( args, int64_t );
467 p_sys->i_subtitle = 0;
468 while( p_sys->i_subtitle < p_sys->i_subtitles &&
469 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
474 if( p_sys->i_subtitle >= p_sys->i_subtitles )
478 case DEMUX_GET_POSITION:
479 pf = (double*)va_arg( args, double * );
480 if( p_sys->i_subtitle >= p_sys->i_subtitles )
484 else if( p_sys->i_subtitles > 0 )
486 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
487 (double)p_sys->i_length;
495 case DEMUX_SET_POSITION:
496 f = (double)va_arg( args, double );
497 i64 = f * p_sys->i_length;
499 p_sys->i_subtitle = 0;
500 while( p_sys->i_subtitle < p_sys->i_subtitles &&
501 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
505 if( p_sys->i_subtitle >= p_sys->i_subtitles )
509 case DEMUX_SET_NEXT_DEMUX_TIME:
510 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
515 case DEMUX_GET_ATTACHMENTS:
516 case DEMUX_GET_TITLE_INFO:
520 msg_Err( p_demux, "unknown query in subtitle control" );
525 /*****************************************************************************
526 * Demux: Send subtitle to decoder
527 *****************************************************************************/
528 static int Demux( demux_t *p_demux )
530 demux_sys_t *p_sys = p_demux->p_sys;
533 if( p_sys->i_subtitle >= p_sys->i_subtitles )
536 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
537 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
539 /* Should not happen */
540 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
543 while( p_sys->i_subtitle < p_sys->i_subtitles &&
544 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
547 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
556 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
562 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
568 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
569 p_block->i_dts = p_block->i_pts;
570 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
573 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
576 memcpy( p_block->p_buffer,
577 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
578 if( p_block->i_pts > 0 )
580 es_out_Send( p_demux->out, p_sys->es, p_block );
584 block_Release( p_block );
590 p_sys->i_next_demux_date = 0;
595 /*****************************************************************************
596 * Fix: fix time stamp and order of subtitle
597 *****************************************************************************/
598 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
599 static void Fix( demux_t *p_demux )
601 demux_sys_t *p_sys = p_demux->p_sys;
605 /* *** fix order (to be sure...) *** */
606 /* We suppose that there are near in order and this durty bubble sort
607 * wont take too much time
612 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
614 if( p_sys->subtitle[i_index].i_start <
615 p_sys->subtitle[i_index - 1].i_start )
619 p_sys->subtitle + i_index - 1,
620 sizeof( subtitle_t ) );
621 memcpy( p_sys->subtitle + i_index - 1,
622 p_sys->subtitle + i_index,
623 sizeof( subtitle_t ) );
624 memcpy( p_sys->subtitle + i_index,
626 sizeof( subtitle_t ) );
634 static int TextLoad( text_t *txt, stream_t *s )
640 txt->i_line_count = 0;
642 txt->line = calloc( i_line_max, sizeof( char * ) );
644 /* load the complete file */
647 char *psz = stream_ReadLine( s );
652 txt->line[txt->i_line_count++] = psz;
653 if( txt->i_line_count >= i_line_max )
656 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
660 if( txt->i_line_count <= 0 )
668 static void TextUnload( text_t *txt )
672 for( i = 0; i < txt->i_line_count; i++ )
674 free( txt->line[i] );
678 txt->i_line_count = 0;
681 static char *TextGetLine( text_t *txt )
683 if( txt->i_line >= txt->i_line_count )
686 return txt->line[txt->i_line++];
688 static void TextPreviousLine( text_t *txt )
690 if( txt->i_line > 0 )
694 /*****************************************************************************
695 * Specific Subtitle function
696 *****************************************************************************/
699 * {n1}{n2}Line1|Line2|Line3....
700 * where n1 and n2 are the video frame number (n2 can be empty)
702 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
704 demux_sys_t *p_sys = p_demux->p_sys;
705 text_t *txt = &p_sys->txt;
713 const char *s = TextGetLine( txt );
717 psz_text = malloc( strlen(s) + 1 );
723 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
724 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
727 if( i_start != 1 || i_stop != 1 )
730 /* We found a possible setting of the framerate "{1}{1}23.976" */
731 /* Check if it's usable, and if the sub-fps is not set */
732 f_fps = us_strtod( psz_text, NULL );
733 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
734 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
739 /* replace | by \n */
740 for( i = 0; psz_text[i] != '\0'; i++ )
742 if( psz_text[i] == '|' )
747 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
748 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
749 p_subtitle->psz_text = psz_text;
753 /* ParseSubRipSubViewer
756 * h1:m1:s1,d1 --> h2:m2:s2,d2
761 * Format SubViewer v1/v2
762 * h1:m1:s1.d1,h2:m2:s2.d2
767 * We ignore line number for SubRip
769 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
771 vlc_bool_t b_replace_br )
773 demux_sys_t *p_sys = p_demux->p_sys;
774 text_t *txt = &p_sys->txt;
779 const char *s = TextGetLine( txt );
780 int h1, m1, s1, d1, h2, m2, s2, d2;
785 if( sscanf( s, psz_fmt,
787 &h2, &m2, &s2, &d2 ) == 8 )
789 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
790 (int64_t)m1 * 60*1000 +
792 (int64_t)d1 ) * 1000;
794 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
795 (int64_t)m2 * 60*1000 +
797 (int64_t)d2 ) * 1000;
802 /* Now read text until an empty line */
803 psz_text = strdup("");
808 const char *s = TextGetLine( txt );
821 p_subtitle->psz_text = psz_text;
825 i_old = strlen( psz_text );
826 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
829 strcat( psz_text, s );
830 strcat( psz_text, "\n" );
832 /* replace [br] by \n */
837 while( ( p = strstr( psz_text, "[br]" ) ) )
840 memmove( p, &p[3], strlen(&p[3])+1 );
847 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
849 return ParseSubRipSubViewer( p_demux, p_subtitle,
850 "%d:%d:%d,%d --> %d:%d:%d,%d",
855 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
857 return ParseSubRipSubViewer( p_demux, p_subtitle,
858 "%d:%d:%d.%d,%d:%d:%d.%d",
864 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
866 demux_sys_t *p_sys = p_demux->p_sys;
867 text_t *txt = &p_sys->txt;
871 const char *s = TextGetLine( txt );
872 int h1, m1, s1, c1, h2, m2, s2, c2;
878 /* We expect (SSA2-4):
879 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
880 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
882 * 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.
886 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
887 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
889 psz_text = malloc( 2 + strlen( s ) + 1 );
894 "Dialogue: %*[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
899 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
900 /* (Layer comes from ASS specs ... it's empty for SSA.) */
901 if( p_sys->i_type == SUB_TYPE_SSA1 )
903 /* SSA1 has only 8 commas before the text starts, not 9 */
904 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
909 /* ReadOrder, Layer, %s(rest of fields) */
910 memmove( &psz_text[2], psz_text, strlen(psz_text)+1 );
915 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
916 (int64_t)m1 * 60*1000 +
918 (int64_t)c1 * 10 ) * 1000;
919 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
920 (int64_t)m2 * 60*1000 +
922 (int64_t)c2 * 10 ) * 1000;
923 p_subtitle->psz_text = psz_text;
928 /* All the other stuff we add to the header field */
929 if( !p_sys->psz_header )
930 p_sys->psz_header = strdup( "" );
931 if( !p_sys->psz_header )
935 realloc( p_sys->psz_header,
936 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
937 strcat( p_sys->psz_header, s );
938 strcat( p_sys->psz_header, "\n" );
944 * h:m:s:Line1|Line2|Line3....
946 * h:m:s Line1|Line2|Line3....
948 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
950 demux_sys_t *p_sys = p_demux->p_sys;
951 text_t *txt = &p_sys->txt;
957 const char *s = TextGetLine( txt );
963 psz_text = malloc( strlen( s ) + 1 );
967 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
968 &h1, &m1, &s1, psz_text ) == 4 )
970 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
971 (int64_t)m1 * 60*1000 +
972 (int64_t)s1 * 1000 ) * 1000;
973 p_subtitle->i_stop = 0;
979 /* replace | by \n */
980 for( i = 0; psz_text[i] != '\0'; i++ )
982 if( psz_text[i] == '|' )
985 p_subtitle->psz_text = psz_text;
991 static char *ParseSamiSearch( text_t *txt,
992 char *psz_start, const char *psz_str )
994 if( psz_start && strcasestr( psz_start, psz_str ) )
996 char *s = strcasestr( psz_start, psz_str );
997 return &s[strlen( psz_str )];
1002 char *p = TextGetLine( txt );
1006 if( strcasestr( p, psz_str ) )
1008 char *s = strcasestr( p, psz_str );
1009 return &s[strlen( psz_str )];
1013 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1015 demux_sys_t *p_sys = p_demux->p_sys;
1016 text_t *txt = &p_sys->txt;
1021 unsigned int i_text;
1022 char text[8192]; /* Arbitrary but should be long enough */
1024 /* search "Start=" */
1025 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1026 return VLC_EGENERIC;
1028 /* get start value */
1029 i_start = strtol( s, &s, 0 );
1032 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1033 return VLC_EGENERIC;
1036 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1037 return VLC_EGENERIC;
1041 /* now get all txt until a "Start=" line */
1045 /* Search non empty line */
1046 while( s && *s == '\0' )
1047 s = TextGetLine( txt );
1053 if( !strncasecmp( s, "<br", 3 ) )
1057 else if( strcasestr( s, "Start=" ) )
1059 TextPreviousLine( txt );
1062 s = ParseSamiSearch( txt, s, ">" );
1064 else if( !strncmp( s, " ", 6 ) )
1069 else if( *s == '\t' )
1079 if( c != '\0' && i_text+1 < sizeof(text) )
1082 text[i_text] = '\0';
1086 p_subtitle->i_start = i_start * 1000;
1087 p_subtitle->i_stop = 0;
1088 p_subtitle->psz_text = strdup( text );
1100 * TODO it can have a header
1107 * LANG support would be cool
1108 * CODEPAGE is probably mandatory FIXME
1110 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1112 demux_sys_t *p_sys = p_demux->p_sys;
1113 text_t *txt = &p_sys->txt;
1118 const char *s = TextGetLine( txt );
1122 return VLC_EGENERIC;
1126 &h1, &m1, &s1, &c1 ) == 4 )
1128 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1129 (int64_t)m1 * 60*1000 +
1130 (int64_t)s1 * 1000 +
1131 (int64_t)c1 * 10) * 1000;
1132 p_subtitle->i_stop = 0;
1137 /* Now read text until a line containing "}" */
1138 psz_text = strdup("");
1143 const char *s = TextGetLine( txt );
1150 return VLC_EGENERIC;
1153 i_len = strlen( s );
1154 if( i_len == 1 && s[0] == '}')
1156 p_subtitle->psz_text = psz_text;
1160 i_old = strlen( psz_text );
1161 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1164 strcat( psz_text, s );
1165 strcat( psz_text, "\n" );
1171 * [n1][n2]Line1|Line2|Line3...
1172 * where n1 and n2 are the video frame number (n2 can be empty)
1174 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle )
1176 demux_sys_t *p_sys = p_demux->p_sys;
1177 text_t *txt = &p_sys->txt;
1183 const char *s = TextGetLine( txt );
1188 return VLC_EGENERIC;
1190 psz_text = malloc( strlen(s) + 1 );
1196 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1197 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1199 p_subtitle->i_start = (int64_t)i_start * 100000;
1200 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1206 /* replace | by \n */
1207 for( i = 0; psz_text[i] != '\0'; )
1209 if( psz_text[i] == '|' )
1213 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1214 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1218 p_subtitle->psz_text = psz_text;