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\", \"aqt\", \"pjs\" 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",
69 set_shortname( _("Subtitles"));
70 set_description( _("Text subtitles parser") );
71 set_capability( "demux", 0 );
72 set_category( CAT_INPUT );
73 set_subcategory( SUBCAT_INPUT_DEMUX );
74 add_float( "sub-fps", 0.0, NULL,
75 N_("Frames per second"),
76 SUB_FPS_LONGTEXT, true );
77 add_integer( "sub-delay", 0, NULL,
78 N_("Subtitles delay"),
79 SUB_DELAY_LONGTEXT, true );
80 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
81 SUB_TYPE_LONGTEXT, true );
82 change_string_list( ppsz_sub_type, NULL, NULL );
83 set_callbacks( Open, Close );
85 add_shortcut( "subtitle" );
88 /*****************************************************************************
90 *****************************************************************************/
93 SUB_TYPE_UNKNOWN = -1,
102 SUB_TYPE_DVDSUBTITLE,
114 static int TextLoad( text_t *, stream_t *s );
115 static void TextUnload( text_t * );
132 int64_t i_next_demux_date;
133 int64_t i_microsecperframe;
138 subtitle_t *subtitle;
143 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
144 static int ParseSubRip ( demux_t *, subtitle_t *, int );
145 static int ParseSubViewer ( demux_t *, subtitle_t *, int );
146 static int ParseSSA ( demux_t *, subtitle_t *, int );
147 static int ParseVplayer ( demux_t *, subtitle_t *, int );
148 static int ParseSami ( demux_t *, subtitle_t *, int );
149 static int ParseDVDSubtitle( demux_t *, subtitle_t *, int );
150 static int ParseMPL2 ( demux_t *, subtitle_t *, int );
151 static int ParseAQT ( demux_t *, subtitle_t *, int );
152 static int ParsePJS ( demux_t *, subtitle_t *, int );
156 const char *psz_type_name;
158 const char *psz_name;
159 int (*pf_read)( demux_t *, subtitle_t*, int );
160 } sub_read_subtitle_function [] =
162 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
163 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
164 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
165 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
166 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
167 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
168 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
169 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
170 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
171 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
172 { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT },
173 { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS },
174 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
177 static int Demux( demux_t * );
178 static int Control( demux_t *, int, va_list );
180 /*static void Fix( demux_t * );*/
182 /*****************************************************************************
184 *****************************************************************************/
185 static int Open ( vlc_object_t *p_this )
187 demux_t *p_demux = (demux_t*)p_this;
192 int (*pf_read)( demux_t *, subtitle_t*, int );
195 if( !p_demux->b_force )
197 msg_Dbg( p_demux, "subtitle demux discarded" );
201 p_demux->pf_demux = Demux;
202 p_demux->pf_control = Control;
203 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
204 p_sys->psz_header = NULL;
205 p_sys->i_subtitle = 0;
206 p_sys->i_subtitles = 0;
207 p_sys->subtitle = NULL;
208 p_sys->i_microsecperframe = 40000;
211 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
213 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
215 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
217 /* Check for override of the fps */
218 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
221 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
222 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
225 /* Get or probe the type */
226 p_sys->i_type = SUB_TYPE_UNKNOWN;
227 psz_type = var_CreateGetString( p_demux, "sub-type" );
234 if( sub_read_subtitle_function[i].psz_type_name == NULL )
237 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
240 p_sys->i_type = sub_read_subtitle_function[i].i_type;
247 /* Probe if unknown type */
248 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
253 msg_Dbg( p_demux, "autodetecting subtitle format" );
254 for( i_try = 0; i_try < 256; i_try++ )
258 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
261 if( strcasestr( s, "<SAMI>" ) )
263 p_sys->i_type = SUB_TYPE_SAMI;
266 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
267 sscanf( s, "{%d}{}", &i_dummy ) == 1)
269 p_sys->i_type = SUB_TYPE_MICRODVD;
273 "%d:%d:%d,%d --> %d:%d:%d,%d",
274 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
275 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
277 p_sys->i_type = SUB_TYPE_SUBRIP;
280 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
282 p_sys->i_type = SUB_TYPE_SSA1;
285 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
287 p_sys->i_type = SUB_TYPE_ASS;
290 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
292 p_sys->i_type = SUB_TYPE_SSA2_4;
295 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
297 p_sys->i_type = SUB_TYPE_SSA2_4;
300 else if( !strncasecmp( s, "Dialogue:", 9 ) )
302 p_sys->i_type = SUB_TYPE_ASS;
305 else if( strcasestr( s, "[INFORMATION]" ) )
307 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
310 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
311 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
313 p_sys->i_type = SUB_TYPE_VPLAYER;
316 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
317 &i_dummy, &i_dummy ) == 4 )
319 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
322 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
323 sscanf( s, "[%d][]", &i_dummy ) == 1)
325 p_sys->i_type = SUB_TYPE_MPL2;
328 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
330 p_sys->i_type = SUB_TYPE_AQT;
332 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
334 p_sys->i_type = SUB_TYPE_PJS;
343 /* It will nearly always work even for non seekable stream thanks the
344 * caching system, and if it fails we lose just a few sub */
345 if( stream_Seek( p_demux->s, 0 ) )
347 msg_Warn( p_demux, "failed to rewind" );
350 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
352 msg_Err( p_demux, "failed to recognize subtitle type" );
359 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
361 msg_Dbg( p_demux, "detected %s format",
362 sub_read_subtitle_function[i].psz_name );
363 pf_read = sub_read_subtitle_function[i].pf_read;
368 msg_Dbg( p_demux, "loading all subtitles..." );
370 /* Load the whole file */
371 TextLoad( &p_sys->txt, p_demux->s );
376 if( p_sys->i_subtitles >= i_max )
379 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
380 sizeof(subtitle_t) * i_max ) ) )
382 msg_Err( p_demux, "out of memory");
383 free( p_sys->subtitle );
384 TextUnload( &p_sys->txt );
390 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
391 p_sys->i_subtitles ) )
394 p_sys->i_subtitles++;
397 TextUnload( &p_sys->txt );
399 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
401 /* Fix subtitle (order and time) *** */
402 p_sys->i_subtitle = 0;
404 if( p_sys->i_subtitles > 0 )
406 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
408 if( p_sys->i_length <= 0 )
409 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
412 /* *** add subtitle ES *** */
413 if( p_sys->i_type == SUB_TYPE_SSA1 ||
414 p_sys->i_type == SUB_TYPE_SSA2_4 ||
415 p_sys->i_type == SUB_TYPE_ASS )
417 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
421 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
423 if( p_sys->psz_header != NULL )
425 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
426 fmt.p_extra = strdup( p_sys->psz_header );
428 p_sys->es = es_out_Add( p_demux->out, &fmt );
433 /*****************************************************************************
434 * Close: Close subtitle demux
435 *****************************************************************************/
436 static void Close( vlc_object_t *p_this )
438 demux_t *p_demux = (demux_t*)p_this;
439 demux_sys_t *p_sys = p_demux->p_sys;
442 for( i = 0; i < p_sys->i_subtitles; i++ )
443 free( p_sys->subtitle[i].psz_text );
444 free( p_sys->subtitle );
449 /*****************************************************************************
451 *****************************************************************************/
452 static int Control( demux_t *p_demux, int i_query, va_list args )
454 demux_sys_t *p_sys = p_demux->p_sys;
460 case DEMUX_GET_LENGTH:
461 pi64 = (int64_t*)va_arg( args, int64_t * );
462 *pi64 = p_sys->i_length;
466 pi64 = (int64_t*)va_arg( args, int64_t * );
467 if( p_sys->i_subtitle < p_sys->i_subtitles )
469 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
475 i64 = (int64_t)va_arg( args, int64_t );
476 p_sys->i_subtitle = 0;
477 while( p_sys->i_subtitle < p_sys->i_subtitles &&
478 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
483 if( p_sys->i_subtitle >= p_sys->i_subtitles )
487 case DEMUX_GET_POSITION:
488 pf = (double*)va_arg( args, double * );
489 if( p_sys->i_subtitle >= p_sys->i_subtitles )
493 else if( p_sys->i_subtitles > 0 )
495 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
496 (double)p_sys->i_length;
504 case DEMUX_SET_POSITION:
505 f = (double)va_arg( args, double );
506 i64 = f * p_sys->i_length;
508 p_sys->i_subtitle = 0;
509 while( p_sys->i_subtitle < p_sys->i_subtitles &&
510 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
514 if( p_sys->i_subtitle >= p_sys->i_subtitles )
518 case DEMUX_SET_NEXT_DEMUX_TIME:
519 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
524 case DEMUX_GET_ATTACHMENTS:
525 case DEMUX_GET_TITLE_INFO:
526 case DEMUX_HAS_UNSUPPORTED_META:
530 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
535 /*****************************************************************************
536 * Demux: Send subtitle to decoder
537 *****************************************************************************/
538 static int Demux( demux_t *p_demux )
540 demux_sys_t *p_sys = p_demux->p_sys;
543 if( p_sys->i_subtitle >= p_sys->i_subtitles )
546 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
547 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
549 /* Should not happen */
550 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
553 while( p_sys->i_subtitle < p_sys->i_subtitles &&
554 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
557 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
566 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
572 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
578 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
579 p_block->i_dts = p_block->i_pts;
580 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
583 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
586 memcpy( p_block->p_buffer,
587 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
588 if( p_block->i_pts > 0 )
590 es_out_Send( p_demux->out, p_sys->es, p_block );
594 block_Release( p_block );
600 p_sys->i_next_demux_date = 0;
605 /*****************************************************************************
606 * Fix: fix time stamp and order of subtitle
607 *****************************************************************************/
608 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
609 static void Fix( demux_t *p_demux )
611 demux_sys_t *p_sys = p_demux->p_sys;
615 /* *** fix order (to be sure...) *** */
616 /* We suppose that there are near in order and this durty bubble sort
617 * wont take too much time
622 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
624 if( p_sys->subtitle[i_index].i_start <
625 p_sys->subtitle[i_index - 1].i_start )
629 p_sys->subtitle + i_index - 1,
630 sizeof( subtitle_t ) );
631 memcpy( p_sys->subtitle + i_index - 1,
632 p_sys->subtitle + i_index,
633 sizeof( subtitle_t ) );
634 memcpy( p_sys->subtitle + i_index,
636 sizeof( subtitle_t ) );
644 static int TextLoad( text_t *txt, stream_t *s )
650 txt->i_line_count = 0;
652 txt->line = calloc( i_line_max, sizeof( char * ) );
654 /* load the complete file */
657 char *psz = stream_ReadLine( s );
662 txt->line[txt->i_line_count++] = psz;
663 if( txt->i_line_count >= i_line_max )
666 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
670 if( txt->i_line_count <= 0 )
678 static void TextUnload( text_t *txt )
682 for( i = 0; i < txt->i_line_count; i++ )
684 free( txt->line[i] );
688 txt->i_line_count = 0;
691 static char *TextGetLine( text_t *txt )
693 if( txt->i_line >= txt->i_line_count )
696 return txt->line[txt->i_line++];
698 static void TextPreviousLine( text_t *txt )
700 if( txt->i_line > 0 )
704 /*****************************************************************************
705 * Specific Subtitle function
706 *****************************************************************************/
709 * {n1}{n2}Line1|Line2|Line3....
710 * where n1 and n2 are the video frame number (n2 can be empty)
712 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
715 demux_sys_t *p_sys = p_demux->p_sys;
716 text_t *txt = &p_sys->txt;
724 const char *s = TextGetLine( txt );
728 psz_text = malloc( strlen(s) + 1 );
734 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
735 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
738 if( i_start != 1 || i_stop != 1 )
741 /* We found a possible setting of the framerate "{1}{1}23.976" */
742 /* Check if it's usable, and if the sub-fps is not set */
743 f_fps = us_strtod( psz_text, NULL );
744 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
745 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
750 /* replace | by \n */
751 for( i = 0; psz_text[i] != '\0'; i++ )
753 if( psz_text[i] == '|' )
758 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
759 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
760 p_subtitle->psz_text = psz_text;
764 /* ParseSubRipSubViewer
767 * h1:m1:s1,d1 --> h2:m2:s2,d2
772 * Format SubViewer v1/v2
773 * h1:m1:s1.d1,h2:m2:s2.d2
778 * We ignore line number for SubRip
780 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
784 demux_sys_t *p_sys = p_demux->p_sys;
785 text_t *txt = &p_sys->txt;
790 const char *s = TextGetLine( txt );
791 int h1, m1, s1, d1, h2, m2, s2, d2;
796 if( sscanf( s, psz_fmt,
798 &h2, &m2, &s2, &d2 ) == 8 )
800 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
801 (int64_t)m1 * 60*1000 +
803 (int64_t)d1 ) * 1000;
805 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
806 (int64_t)m2 * 60*1000 +
808 (int64_t)d2 ) * 1000;
813 /* Now read text until an empty line */
814 psz_text = strdup("");
819 const char *s = TextGetLine( txt );
832 p_subtitle->psz_text = psz_text;
836 i_old = strlen( psz_text );
837 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
840 strcat( psz_text, s );
841 strcat( psz_text, "\n" );
843 /* replace [br] by \n */
848 while( ( p = strstr( psz_text, "[br]" ) ) )
851 memmove( p, &p[3], strlen(&p[3])+1 );
858 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
861 return ParseSubRipSubViewer( p_demux, p_subtitle,
862 "%d:%d:%d,%d --> %d:%d:%d,%d",
867 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
870 return ParseSubRipSubViewer( p_demux, p_subtitle,
871 "%d:%d:%d.%d,%d:%d:%d.%d",
877 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
880 demux_sys_t *p_sys = p_demux->p_sys;
881 text_t *txt = &p_sys->txt;
885 const char *s = TextGetLine( txt );
886 int h1, m1, s1, c1, h2, m2, s2, c2;
893 /* We expect (SSA2-4):
894 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
895 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
897 * 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.
901 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
902 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
905 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
906 psz_text = malloc( strlen(s) );
911 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
917 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
918 /* (Layer comes from ASS specs ... it's empty for SSA.) */
919 if( p_sys->i_type == SUB_TYPE_SSA1 )
921 /* SSA1 has only 8 commas before the text starts, not 9 */
922 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
927 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
929 /* ReadOrder, Layer, %s(rest of fields) */
930 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
931 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
932 memcpy( psz_text, temp, strlen(temp) );
935 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
936 (int64_t)m1 * 60*1000 +
938 (int64_t)c1 * 10 ) * 1000;
939 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
940 (int64_t)m2 * 60*1000 +
942 (int64_t)c2 * 10 ) * 1000;
943 p_subtitle->psz_text = psz_text;
948 /* All the other stuff we add to the header field */
949 if( !p_sys->psz_header )
950 p_sys->psz_header = strdup( "" );
951 if( !p_sys->psz_header )
955 realloc( p_sys->psz_header,
956 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
957 strcat( p_sys->psz_header, s );
958 strcat( p_sys->psz_header, "\n" );
964 * h:m:s:Line1|Line2|Line3....
966 * h:m:s Line1|Line2|Line3....
968 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
971 demux_sys_t *p_sys = p_demux->p_sys;
972 text_t *txt = &p_sys->txt;
978 const char *s = TextGetLine( txt );
984 psz_text = malloc( strlen( s ) + 1 );
988 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
989 &h1, &m1, &s1, psz_text ) == 4 )
991 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
992 (int64_t)m1 * 60*1000 +
993 (int64_t)s1 * 1000 ) * 1000;
994 p_subtitle->i_stop = 0;
1000 /* replace | by \n */
1001 for( i = 0; psz_text[i] != '\0'; i++ )
1003 if( psz_text[i] == '|' )
1006 p_subtitle->psz_text = psz_text;
1012 static char *ParseSamiSearch( text_t *txt,
1013 char *psz_start, const char *psz_str )
1015 if( psz_start && strcasestr( psz_start, psz_str ) )
1017 char *s = strcasestr( psz_start, psz_str );
1018 return &s[strlen( psz_str )];
1023 char *p = TextGetLine( txt );
1027 if( strcasestr( p, psz_str ) )
1029 char *s = strcasestr( p, psz_str );
1030 return &s[strlen( psz_str )];
1034 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1036 demux_sys_t *p_sys = p_demux->p_sys;
1037 text_t *txt = &p_sys->txt;
1042 unsigned int i_text;
1043 char text[8192]; /* Arbitrary but should be long enough */
1045 /* search "Start=" */
1046 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1047 return VLC_EGENERIC;
1049 /* get start value */
1050 i_start = strtol( s, &s, 0 );
1053 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1054 return VLC_EGENERIC;
1057 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1058 return VLC_EGENERIC;
1062 /* now get all txt until a "Start=" line */
1066 /* Search non empty line */
1067 while( s && *s == '\0' )
1068 s = TextGetLine( txt );
1074 if( !strncasecmp( s, "<br", 3 ) )
1078 else if( strcasestr( s, "Start=" ) )
1080 TextPreviousLine( txt );
1083 s = ParseSamiSearch( txt, s, ">" );
1085 else if( !strncmp( s, " ", 6 ) )
1090 else if( *s == '\t' )
1100 if( c != '\0' && i_text+1 < sizeof(text) )
1103 text[i_text] = '\0';
1107 p_subtitle->i_start = i_start * 1000;
1108 p_subtitle->i_stop = 0;
1109 p_subtitle->psz_text = strdup( text );
1121 * TODO it can have a header
1128 * LANG support would be cool
1129 * CODEPAGE is probably mandatory FIXME
1131 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1134 demux_sys_t *p_sys = p_demux->p_sys;
1135 text_t *txt = &p_sys->txt;
1140 const char *s = TextGetLine( txt );
1144 return VLC_EGENERIC;
1148 &h1, &m1, &s1, &c1 ) == 4 )
1150 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1151 (int64_t)m1 * 60*1000 +
1152 (int64_t)s1 * 1000 +
1153 (int64_t)c1 * 10) * 1000;
1154 p_subtitle->i_stop = 0;
1159 /* Now read text until a line containing "}" */
1160 psz_text = strdup("");
1165 const char *s = TextGetLine( txt );
1172 return VLC_EGENERIC;
1175 i_len = strlen( s );
1176 if( i_len == 1 && s[0] == '}')
1178 p_subtitle->psz_text = psz_text;
1182 i_old = strlen( psz_text );
1183 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1186 strcat( psz_text, s );
1187 strcat( psz_text, "\n" );
1193 * [n1][n2]Line1|Line2|Line3...
1194 * where n1 and n2 are the video frame number (n2 can be empty)
1196 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1198 demux_sys_t *p_sys = p_demux->p_sys;
1199 text_t *txt = &p_sys->txt;
1205 const char *s = TextGetLine( txt );
1210 return VLC_EGENERIC;
1212 psz_text = malloc( strlen(s) + 1 );
1218 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1219 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1221 p_subtitle->i_start = (int64_t)i_start * 100000;
1222 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1228 for( i = 0; psz_text[i] != '\0'; )
1230 /* replace | by \n */
1231 if( psz_text[i] == '|' )
1235 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1236 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1240 p_subtitle->psz_text = psz_text;
1244 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1246 demux_sys_t *p_sys = p_demux->p_sys;
1247 text_t *txt = &p_sys->txt;
1248 char *psz_text = strdup( "" );
1250 int i_firstline = 1;
1256 const char *s = TextGetLine( txt );
1259 return VLC_EGENERIC;
1262 if( sscanf (s, "-->> %d", &t) == 1)
1264 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1265 p_subtitle->i_stop = 0;
1267 /* Starting of a subtitle */
1272 /* We have been too far: end of the subtitle, begin of next */
1282 i_old = strlen( psz_text ) + 1;
1283 psz_text = realloc( psz_text, i_old + strlen( s ) + 1 );
1286 strcat( psz_text, s );
1287 strcat( psz_text, "\n" );
1288 if( txt->i_line == txt->i_line_count )
1292 p_subtitle->psz_text = psz_text;
1296 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1298 demux_sys_t *p_sys = p_demux->p_sys;
1299 text_t *txt = &p_sys->txt;
1304 const char *s = TextGetLine( txt );
1308 return VLC_EGENERIC;
1310 psz_text = malloc( strlen(s) + 1 );
1313 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1315 /* 1/10th of second ? Frame based ? FIXME */
1316 p_subtitle->i_start = 10 * t1;
1317 p_subtitle->i_stop = 10 * t2;
1318 /* Remove latest " */
1319 psz_text[ strlen(psz_text) - 1 ] = '\0 ';
1325 p_subtitle->psz_text = psz_text;
1326 msg_Dbg( p_demux, "%s", psz_text );