1 /*****************************************************************************
2 * subtitle.c: Demux for subtitle text files.
3 *****************************************************************************
4 * Copyright (C) 1999-2004 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>
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
40 #include <vlc_demux.h>
41 #include <vlc_charset.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t *p_this );
47 static void Close( vlc_object_t *p_this );
49 #define SUB_DELAY_LONGTEXT \
50 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
51 #define SUB_FPS_LONGTEXT \
52 N_("Override the normal frames per second settings. " \
53 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
54 #define SUB_TYPE_LONGTEXT \
55 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
56 "\"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
57 "\"sami\", \"dvdsubtitle\" and \"auto\" (meaning autodetection, this " \
58 "should always work).")
59 static const char *ppsz_sub_type[] =
61 "auto", "microdvd", "subrip", "subviewer", "ssa1",
62 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle"
66 set_shortname( _("Subtitles"));
67 set_description( _("Text subtitles parser") );
68 set_capability( "demux2", 0 );
69 set_category( CAT_INPUT );
70 set_subcategory( SUBCAT_INPUT_DEMUX );
71 add_float( "sub-fps", 0.0, NULL,
72 N_("Frames per second"),
73 SUB_FPS_LONGTEXT, VLC_TRUE );
74 add_integer( "sub-delay", 0, NULL,
75 N_("Subtitles delay"),
76 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 );
80 set_callbacks( Open, Close );
82 add_shortcut( "subtitle" );
85 /*****************************************************************************
87 *****************************************************************************/
90 SUB_TYPE_UNKNOWN = -1,
108 static int TextLoad( text_t *, stream_t *s );
109 static void TextUnload( text_t * );
126 int64_t i_next_demux_date;
127 int64_t i_microsecperframe;
132 subtitle_t *subtitle;
137 static int ParseMicroDvd ( demux_t *, subtitle_t * );
138 static int ParseSubRip ( demux_t *, subtitle_t * );
139 static int ParseSubViewer ( demux_t *, subtitle_t * );
140 static int ParseSSA ( demux_t *, subtitle_t * );
141 static int ParseVplayer ( demux_t *, subtitle_t * );
142 static int ParseSami ( demux_t *, subtitle_t * );
143 static int ParseDVDSubtitle( demux_t *, subtitle_t * );
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 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
165 static int Demux( demux_t * );
166 static int Control( demux_t *, int, va_list );
168 /*static void Fix( demux_t * );*/
170 /*****************************************************************************
172 *****************************************************************************/
173 static int Open ( vlc_object_t *p_this )
175 demux_t *p_demux = (demux_t*)p_this;
178 input_thread_t *p_input;
181 int (*pf_read)( demux_t *, subtitle_t* );
184 if( strcmp( p_demux->psz_demux, "subtitle" ) )
186 msg_Dbg( p_demux, "subtitle demux discarded" );
190 p_demux->pf_demux = Demux;
191 p_demux->pf_control = Control;
192 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
193 p_sys->psz_header = NULL;
194 p_sys->i_subtitle = 0;
195 p_sys->i_subtitles = 0;
196 p_sys->subtitle = NULL;
197 p_sys->i_microsecperframe = 0;
200 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
203 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
204 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
207 p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
210 f_fps = var_GetFloat( p_input, "sub-original-fps" );
212 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
214 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
215 vlc_object_release( p_input );
218 /* Check for override of the fps */
219 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
222 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / 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;
329 /* It will nearly always work even for non seekable stream thanks the
330 * caching system, and if it fails we lose just a few sub */
331 if( stream_Seek( p_demux->s, 0 ) )
333 msg_Warn( p_demux, "failed to rewind" );
336 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
338 msg_Err( p_demux, "failed to recognize subtitle type" );
345 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
347 msg_Dbg( p_demux, "detected %s format",
348 sub_read_subtitle_function[i].psz_name );
349 pf_read = sub_read_subtitle_function[i].pf_read;
354 msg_Dbg( p_demux, "loading all subtitles..." );
356 /* Load the whole file */
357 TextLoad( &p_sys->txt, p_demux->s );
362 if( p_sys->i_subtitles >= i_max )
365 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
366 sizeof(subtitle_t) * i_max ) ) )
368 msg_Err( p_demux, "out of memory");
369 if( p_sys->subtitle != NULL )
370 free( p_sys->subtitle );
371 TextUnload( &p_sys->txt );
377 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
380 p_sys->i_subtitles++;
383 TextUnload( &p_sys->txt );
385 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
387 /* Fix subtitle (order and time) *** */
388 p_sys->i_subtitle = 0;
390 if( p_sys->i_subtitles > 0 )
392 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
394 if( p_sys->i_length <= 0 )
395 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
398 /* *** add subtitle ES *** */
399 if( p_sys->i_type == SUB_TYPE_SSA1 ||
400 p_sys->i_type == SUB_TYPE_SSA2_4 ||
401 p_sys->i_type == SUB_TYPE_ASS )
403 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
407 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
409 if( p_sys->psz_header != NULL )
411 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
412 fmt.p_extra = strdup( p_sys->psz_header );
414 p_sys->es = es_out_Add( p_demux->out, &fmt );
419 /*****************************************************************************
420 * Close: Close subtitle demux
421 *****************************************************************************/
422 static void Close( vlc_object_t *p_this )
424 demux_t *p_demux = (demux_t*)p_this;
425 demux_sys_t *p_sys = p_demux->p_sys;
428 for( i = 0; i < p_sys->i_subtitles; i++ )
430 if( p_sys->subtitle[i].psz_text )
431 free( p_sys->subtitle[i].psz_text );
433 if( p_sys->subtitle )
434 free( p_sys->subtitle );
439 /*****************************************************************************
441 *****************************************************************************/
442 static int Control( demux_t *p_demux, int i_query, va_list args )
444 demux_sys_t *p_sys = p_demux->p_sys;
450 case DEMUX_GET_LENGTH:
451 pi64 = (int64_t*)va_arg( args, int64_t * );
452 *pi64 = p_sys->i_length;
456 pi64 = (int64_t*)va_arg( args, int64_t * );
457 if( p_sys->i_subtitle < p_sys->i_subtitles )
459 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
465 i64 = (int64_t)va_arg( args, int64_t );
466 p_sys->i_subtitle = 0;
467 while( p_sys->i_subtitle < p_sys->i_subtitles &&
468 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
473 if( p_sys->i_subtitle >= p_sys->i_subtitles )
477 case DEMUX_GET_POSITION:
478 pf = (double*)va_arg( args, double * );
479 if( p_sys->i_subtitle >= p_sys->i_subtitles )
483 else if( p_sys->i_subtitles > 0 )
485 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
486 (double)p_sys->i_length;
494 case DEMUX_SET_POSITION:
495 f = (double)va_arg( args, double );
496 i64 = f * p_sys->i_length;
498 p_sys->i_subtitle = 0;
499 while( p_sys->i_subtitle < p_sys->i_subtitles &&
500 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
504 if( p_sys->i_subtitle >= p_sys->i_subtitles )
508 case DEMUX_SET_NEXT_DEMUX_TIME:
509 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
514 case DEMUX_GET_TITLE_INFO:
518 msg_Err( p_demux, "unknown query in subtitle control" );
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 *****************************************************************************/
695 #define MAX_LINE 8192
696 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
698 demux_sys_t *p_sys = p_demux->p_sys;
699 text_t *txt = &p_sys->txt;
702 * {n1}{n2}Line1|Line2|Line3....
703 * where n1 and n2 are the video frame number...
704 * {n2} can also be {}
708 char buffer_text[MAX_LINE + 1];
713 /* Try sub-fps value if set, movie rate if know, else 25fps (40000) */
714 int i_microsecperframe = p_sys->i_original_mspf > 0 ? p_sys->i_original_mspf : 40000;
715 if( p_sys->i_microsecperframe > 0 )
716 i_microsecperframe = p_sys->i_microsecperframe;
718 p_subtitle->i_start = 0;
719 p_subtitle->i_stop = 0;
720 p_subtitle->psz_text = NULL;
725 if( ( s = TextGetLine( txt ) ) == NULL )
727 return( VLC_EGENERIC );
732 memset( buffer_text, '\0', MAX_LINE );
733 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
734 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
739 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 float tmp = us_strtod( buffer_text, NULL );
744 if( tmp > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
745 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / tmp );
749 /* replace | by \n */
750 for( i = 0; i < strlen( buffer_text ); i++ )
752 if( buffer_text[i] == '|' )
754 buffer_text[i] = '\n';
758 p_subtitle->i_start = (int64_t)i_start * p_sys->i_microsecperframe;
759 p_subtitle->i_stop = (int64_t)i_stop * p_sys->i_microsecperframe;
760 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
764 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
766 demux_sys_t *p_sys = p_demux->p_sys;
767 text_t *txt = &p_sys->txt;
771 * h1:m1:s1,d1 --> h2:m2:s2,d2
779 char buffer_text[ 10 * MAX_LINE];
784 p_subtitle->i_start = 0;
785 p_subtitle->i_stop = 0;
786 p_subtitle->psz_text = NULL;
790 int h1, m1, s1, d1, h2, m2, s2, d2;
791 if( ( s = TextGetLine( txt ) ) == NULL )
793 return( VLC_EGENERIC );
796 "%d:%d:%d,%d --> %d:%d:%d,%d",
798 &h2, &m2, &s2, &d2 ) == 8 )
800 i_start = ( (int64_t)h1 * 3600*1000 +
801 (int64_t)m1 * 60*1000 +
803 (int64_t)d1 ) * 1000;
805 i_stop = ( (int64_t)h2 * 3600*1000 +
806 (int64_t)m2 * 60*1000 +
808 (int64_t)d2 ) * 1000;
810 /* Now read text until an empty line */
811 for( i_buffer_text = 0;; )
814 if( ( s = TextGetLine( txt ) ) == NULL )
816 return( VLC_EGENERIC );
822 /* empty line -> end of this subtitle */
823 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
824 p_subtitle->i_start = i_start;
825 p_subtitle->i_stop = i_stop;
826 p_subtitle->psz_text = strdup( buffer_text );
831 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
833 memcpy( buffer_text + i_buffer_text,
836 i_buffer_text += i_len;
838 buffer_text[i_buffer_text] = '\n';
847 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
849 demux_sys_t *p_sys = p_demux->p_sys;
850 text_t *txt = &p_sys->txt;
853 * h1:m1:s1.d1,h2:m2:s2.d2
858 * ( works with subviewer and subviewer v2 )
861 char buffer_text[ 10 * MAX_LINE];
866 p_subtitle->i_start = 0;
867 p_subtitle->i_stop = 0;
868 p_subtitle->psz_text = NULL;
872 int h1, m1, s1, d1, h2, m2, s2, d2;
873 if( ( s = TextGetLine( txt ) ) == NULL )
875 return( VLC_EGENERIC );
878 "%d:%d:%d.%d,%d:%d:%d.%d",
880 &h2, &m2, &s2, &d2 ) == 8 )
882 i_start = ( (int64_t)h1 * 3600*1000 +
883 (int64_t)m1 * 60*1000 +
885 (int64_t)d1 ) * 1000;
887 i_stop = ( (int64_t)h2 * 3600*1000 +
888 (int64_t)m2 * 60*1000 +
890 (int64_t)d2 ) * 1000;
892 /* Now read text until an empty line */
893 for( i_buffer_text = 0;; )
896 if( ( s = TextGetLine( txt ) ) == NULL )
898 return( VLC_EGENERIC );
904 /* empty line -> end of this subtitle */
905 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
906 p_subtitle->i_start = i_start;
907 p_subtitle->i_stop = i_stop;
909 /* replace [br] by \n */
910 for( i = 0; i < i_buffer_text - 3; i++ )
912 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
913 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
915 char *temp = buffer_text + i + 1;
916 buffer_text[i] = '\n';
917 memmove( temp, temp+3, strlen( temp ) -3 );
918 temp[strlen( temp )-3] = '\0';
921 p_subtitle->psz_text = strdup( buffer_text );
926 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
928 memcpy( buffer_text + i_buffer_text,
931 i_buffer_text += i_len;
933 buffer_text[i_buffer_text] = '\n';
943 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
945 demux_sys_t *p_sys = p_demux->p_sys;
946 text_t *txt = &p_sys->txt;
948 char buffer_text[ 10 * MAX_LINE];
949 char buffer_text2[ 10 * MAX_LINE];
954 p_subtitle->i_start = 0;
955 p_subtitle->i_stop = 0;
956 p_subtitle->psz_text = NULL;
960 int h1, m1, s1, c1, h2, m2, s2, c2;
962 if( ( s = TextGetLine( txt ) ) == NULL )
964 return( VLC_EGENERIC );
966 p_subtitle->psz_text = malloc( strlen( s ) );
968 /* We expect (SSA2-4):
969 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
970 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
972 * 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.
976 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
977 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
980 "Dialogue: %[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
984 buffer_text ) == 10 )
986 i_start = ( (int64_t)h1 * 3600*1000 +
987 (int64_t)m1 * 60*1000 +
989 (int64_t)c1 * 10 ) * 1000;
991 i_stop = ( (int64_t)h2 * 3600*1000 +
992 (int64_t)m2 * 60*1000 +
994 (int64_t)c2 * 10 ) * 1000;
996 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
997 /* (Layer comes from ASS specs ... it's empty for SSA.) */
998 if( p_sys->i_type == SUB_TYPE_SSA1 )
1000 sprintf( p_subtitle->psz_text,
1001 ",%s", strdup( buffer_text) ); /* SSA1 has only 8 commas before the text starts, not 9 */
1005 sprintf( p_subtitle->psz_text,
1006 ",,%s", strdup( buffer_text) ); /* ReadOrder, Layer, %s(rest of fields) */
1008 p_subtitle->i_start = i_start;
1009 p_subtitle->i_stop = i_stop;
1014 /* All the other stuff we add to the header field */
1015 if( p_sys->psz_header != NULL )
1017 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
1018 strlen( p_sys->psz_header ) + 1 + strlen( s ) + 2 ) ) )
1020 msg_Err( p_demux, "out of memory");
1023 p_sys->psz_header = strcat( p_sys->psz_header, s );
1024 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1028 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
1030 msg_Err( p_demux, "out of memory");
1033 p_sys->psz_header = s;
1034 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1040 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1042 demux_sys_t *p_sys = p_demux->p_sys;
1043 text_t *txt = &p_sys->txt;
1047 * h:m:s:Line1|Line2|Line3....
1049 * h:m:s Line1|Line2|Line3....
1053 char buffer_text[MAX_LINE + 1];
1057 p_subtitle->i_start = 0;
1058 p_subtitle->i_stop = 0;
1059 p_subtitle->psz_text = NULL;
1066 if( ( p = TextGetLine( txt ) ) == NULL )
1068 return( VLC_EGENERIC );
1073 memset( buffer_text, '\0', MAX_LINE );
1074 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1076 i_start = ( (int64_t)h * 3600*1000 +
1077 (int64_t)m * 60*1000 +
1078 (int64_t)s * 1000 ) * 1000;
1083 /* replace | by \n */
1084 for( i = 0; i < strlen( buffer_text ); i++ )
1086 if( buffer_text[i] == '|' )
1088 buffer_text[i] = '\n';
1091 p_subtitle->i_start = i_start;
1093 p_subtitle->i_stop = 0;
1094 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1098 static char *ParseSamiSearch( text_t *txt, char *psz_start, const char *psz_str )
1102 if( strcasestr( psz_start, psz_str ) )
1104 char *s = strcasestr( psz_start, psz_str );
1106 s += strlen( psz_str );
1114 if( ( p = TextGetLine( txt ) ) == NULL )
1118 if( strcasestr( p, psz_str ) )
1120 char *s = strcasestr( p, psz_str );
1122 s += strlen( psz_str );
1129 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1131 demux_sys_t *p_sys = p_demux->p_sys;
1132 text_t *txt = &p_sys->txt;
1138 char buffer_text[10*MAX_LINE + 1];
1140 p_subtitle->i_start = 0;
1141 p_subtitle->i_stop = 0;
1142 p_subtitle->psz_text = NULL;
1145 if( i_text < 10*MAX_LINE ) \
1147 buffer_text[i_text++] = c; \
1148 buffer_text[i_text] = '\0'; \
1151 /* search "Start=" */
1152 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1154 return VLC_EGENERIC;
1157 /* get start value */
1158 i_start = strtol( p, &p, 0 );
1161 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1163 return VLC_EGENERIC;
1166 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1168 return VLC_EGENERIC;
1172 buffer_text[0] = '\0';
1173 /* now get all txt until a "Start=" line */
1180 if( !strncasecmp( p, "<br", 3 ) )
1184 else if( strcasestr( p, "Start=" ) )
1186 TextPreviousLine( txt );
1189 p = ParseSamiSearch( txt, p, ">" );
1191 else if( !strncmp( p, " ", 6 ) )
1196 else if( *p == '\t' )
1209 p = TextGetLine( txt );
1218 p_subtitle->i_start = i_start * 1000;
1219 p_subtitle->i_stop = 0;
1220 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1222 return( VLC_SUCCESS );
1226 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1228 demux_sys_t *p_sys = p_demux->p_sys;
1229 text_t *txt = &p_sys->txt;
1240 char buffer_text[ 10 * MAX_LINE];
1244 p_subtitle->i_start = 0;
1245 p_subtitle->i_stop = 0;
1246 p_subtitle->psz_text = NULL;
1251 if( ( s = TextGetLine( txt ) ) == NULL )
1253 return( VLC_EGENERIC );
1257 &h1, &m1, &s1, &c1 ) == 4 )
1259 i_start = ( (int64_t)h1 * 3600*1000 +
1260 (int64_t)m1 * 60*1000 +
1261 (int64_t)s1 * 1000 +
1262 (int64_t)c1 * 10) * 1000;
1264 /* Now read text until a line containing "}" */
1265 for( i_buffer_text = 0;; )
1268 if( ( s = TextGetLine( txt ) ) == NULL )
1270 return( VLC_EGENERIC );
1273 i_len = strlen( s );
1274 if( i_len == 1 && s[0] == '}' )
1276 /* "}" -> end of this subtitle */
1277 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
1278 p_subtitle->i_start = i_start;
1279 p_subtitle->i_stop = 0;
1280 p_subtitle->psz_text = strdup( buffer_text );
1285 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
1287 memcpy( buffer_text + i_buffer_text,
1290 i_buffer_text += i_len;
1292 buffer_text[i_buffer_text] = '\n';