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>
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\", \"mpl2\" 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", "mpl2"
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,
109 static int TextLoad( text_t *, stream_t *s );
110 static void TextUnload( text_t * );
127 int64_t i_next_demux_date;
128 int64_t i_microsecperframe;
133 subtitle_t *subtitle;
138 static int ParseMicroDvd ( demux_t *, subtitle_t * );
139 static int ParseSubRip ( demux_t *, subtitle_t * );
140 static int ParseSubViewer ( demux_t *, subtitle_t * );
141 static int ParseSSA ( demux_t *, subtitle_t * );
142 static int ParseVplayer ( demux_t *, subtitle_t * );
143 static int ParseSami ( demux_t *, subtitle_t * );
144 static int ParseDVDSubtitle( demux_t *, subtitle_t * );
145 static int ParseMPL2 ( demux_t *, subtitle_t * );
149 const char *psz_type_name;
151 const char *psz_name;
152 int (*pf_read)( demux_t *, subtitle_t* );
153 } sub_read_subtitle_function [] =
155 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
156 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
157 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
158 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
159 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
160 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
161 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
162 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
163 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
164 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
165 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
168 static int Demux( demux_t * );
169 static int Control( demux_t *, int, va_list );
171 /*static void Fix( demux_t * );*/
173 /*****************************************************************************
175 *****************************************************************************/
176 static int Open ( vlc_object_t *p_this )
178 demux_t *p_demux = (demux_t*)p_this;
181 input_thread_t *p_input;
184 int (*pf_read)( demux_t *, subtitle_t* );
187 if( strcmp( p_demux->psz_demux, "subtitle" ) )
189 msg_Dbg( p_demux, "subtitle demux discarded" );
193 p_demux->pf_demux = Demux;
194 p_demux->pf_control = Control;
195 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
196 p_sys->psz_header = NULL;
197 p_sys->i_subtitle = 0;
198 p_sys->i_subtitles = 0;
199 p_sys->subtitle = NULL;
200 p_sys->i_microsecperframe = 40000;
203 p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
206 f_fps = var_GetFloat( p_input, "sub-original-fps" );
208 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
210 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
211 vlc_object_release( p_input );
214 /* Check for override of the fps */
215 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
218 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
219 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
222 /* Get or probe the type */
223 p_sys->i_type = SUB_TYPE_UNKNOWN;
224 psz_type = var_CreateGetString( p_demux, "sub-type" );
231 if( sub_read_subtitle_function[i].psz_type_name == NULL )
234 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
237 p_sys->i_type = sub_read_subtitle_function[i].i_type;
244 /* Probe if unknown type */
245 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
250 msg_Dbg( p_demux, "autodetecting subtitle format" );
251 for( i_try = 0; i_try < 256; i_try++ )
255 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
258 if( strcasestr( s, "<SAMI>" ) )
260 p_sys->i_type = SUB_TYPE_SAMI;
263 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
264 sscanf( s, "{%d}{}", &i_dummy ) == 1)
266 p_sys->i_type = SUB_TYPE_MICRODVD;
270 "%d:%d:%d,%d --> %d:%d:%d,%d",
271 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
272 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
274 p_sys->i_type = SUB_TYPE_SUBRIP;
277 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
279 p_sys->i_type = SUB_TYPE_SSA1;
282 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
284 p_sys->i_type = SUB_TYPE_ASS;
287 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
289 p_sys->i_type = SUB_TYPE_SSA2_4;
292 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
294 p_sys->i_type = SUB_TYPE_SSA2_4;
297 else if( !strncasecmp( s, "Dialogue:", 9 ) )
299 p_sys->i_type = SUB_TYPE_ASS;
302 else if( strcasestr( s, "[INFORMATION]" ) )
304 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
307 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
308 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
310 p_sys->i_type = SUB_TYPE_VPLAYER;
313 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
314 &i_dummy, &i_dummy ) == 4 )
316 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
319 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
320 sscanf( s, "[%d][]", &i_dummy ) == 1)
322 p_sys->i_type = SUB_TYPE_MPL2;
332 /* It will nearly always work even for non seekable stream thanks the
333 * caching system, and if it fails we lose just a few sub */
334 if( stream_Seek( p_demux->s, 0 ) )
336 msg_Warn( p_demux, "failed to rewind" );
339 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
341 msg_Err( p_demux, "failed to recognize subtitle type" );
348 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
350 msg_Dbg( p_demux, "detected %s format",
351 sub_read_subtitle_function[i].psz_name );
352 pf_read = sub_read_subtitle_function[i].pf_read;
357 msg_Dbg( p_demux, "loading all subtitles..." );
359 /* Load the whole file */
360 TextLoad( &p_sys->txt, p_demux->s );
365 if( p_sys->i_subtitles >= i_max )
368 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
369 sizeof(subtitle_t) * i_max ) ) )
371 msg_Err( p_demux, "out of memory");
372 if( p_sys->subtitle != NULL )
373 free( p_sys->subtitle );
374 TextUnload( &p_sys->txt );
380 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
383 p_sys->i_subtitles++;
386 TextUnload( &p_sys->txt );
388 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
390 /* Fix subtitle (order and time) *** */
391 p_sys->i_subtitle = 0;
393 if( p_sys->i_subtitles > 0 )
395 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
397 if( p_sys->i_length <= 0 )
398 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
401 /* *** add subtitle ES *** */
402 if( p_sys->i_type == SUB_TYPE_SSA1 ||
403 p_sys->i_type == SUB_TYPE_SSA2_4 ||
404 p_sys->i_type == SUB_TYPE_ASS )
406 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
410 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
412 if( p_sys->psz_header != NULL )
414 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
415 fmt.p_extra = strdup( p_sys->psz_header );
417 p_sys->es = es_out_Add( p_demux->out, &fmt );
422 /*****************************************************************************
423 * Close: Close subtitle demux
424 *****************************************************************************/
425 static void Close( vlc_object_t *p_this )
427 demux_t *p_demux = (demux_t*)p_this;
428 demux_sys_t *p_sys = p_demux->p_sys;
431 for( i = 0; i < p_sys->i_subtitles; i++ )
433 if( p_sys->subtitle[i].psz_text )
434 free( p_sys->subtitle[i].psz_text );
436 if( p_sys->subtitle )
437 free( p_sys->subtitle );
442 /*****************************************************************************
444 *****************************************************************************/
445 static int Control( demux_t *p_demux, int i_query, va_list args )
447 demux_sys_t *p_sys = p_demux->p_sys;
453 case DEMUX_GET_LENGTH:
454 pi64 = (int64_t*)va_arg( args, int64_t * );
455 *pi64 = p_sys->i_length;
459 pi64 = (int64_t*)va_arg( args, int64_t * );
460 if( p_sys->i_subtitle < p_sys->i_subtitles )
462 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
468 i64 = (int64_t)va_arg( args, int64_t );
469 p_sys->i_subtitle = 0;
470 while( p_sys->i_subtitle < p_sys->i_subtitles &&
471 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
476 if( p_sys->i_subtitle >= p_sys->i_subtitles )
480 case DEMUX_GET_POSITION:
481 pf = (double*)va_arg( args, double * );
482 if( p_sys->i_subtitle >= p_sys->i_subtitles )
486 else if( p_sys->i_subtitles > 0 )
488 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
489 (double)p_sys->i_length;
497 case DEMUX_SET_POSITION:
498 f = (double)va_arg( args, double );
499 i64 = f * p_sys->i_length;
501 p_sys->i_subtitle = 0;
502 while( p_sys->i_subtitle < p_sys->i_subtitles &&
503 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
507 if( p_sys->i_subtitle >= p_sys->i_subtitles )
511 case DEMUX_SET_NEXT_DEMUX_TIME:
512 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
517 case DEMUX_GET_TITLE_INFO:
521 msg_Err( p_demux, "unknown query in subtitle control" );
526 /*****************************************************************************
527 * Demux: Send subtitle to decoder
528 *****************************************************************************/
529 static int Demux( demux_t *p_demux )
531 demux_sys_t *p_sys = p_demux->p_sys;
534 if( p_sys->i_subtitle >= p_sys->i_subtitles )
537 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
538 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
540 /* Should not happen */
541 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
544 while( p_sys->i_subtitle < p_sys->i_subtitles &&
545 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
548 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
557 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
563 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
569 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
570 p_block->i_dts = p_block->i_pts;
571 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
574 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
577 memcpy( p_block->p_buffer,
578 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
579 if( p_block->i_pts > 0 )
581 es_out_Send( p_demux->out, p_sys->es, p_block );
585 block_Release( p_block );
591 p_sys->i_next_demux_date = 0;
596 /*****************************************************************************
597 * Fix: fix time stamp and order of subtitle
598 *****************************************************************************/
599 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
600 static void Fix( demux_t *p_demux )
602 demux_sys_t *p_sys = p_demux->p_sys;
606 /* *** fix order (to be sure...) *** */
607 /* We suppose that there are near in order and this durty bubble sort
608 * wont take too much time
613 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
615 if( p_sys->subtitle[i_index].i_start <
616 p_sys->subtitle[i_index - 1].i_start )
620 p_sys->subtitle + i_index - 1,
621 sizeof( subtitle_t ) );
622 memcpy( p_sys->subtitle + i_index - 1,
623 p_sys->subtitle + i_index,
624 sizeof( subtitle_t ) );
625 memcpy( p_sys->subtitle + i_index,
627 sizeof( subtitle_t ) );
635 static int TextLoad( text_t *txt, stream_t *s )
641 txt->i_line_count = 0;
643 txt->line = calloc( i_line_max, sizeof( char * ) );
645 /* load the complete file */
648 char *psz = stream_ReadLine( s );
653 txt->line[txt->i_line_count++] = psz;
654 if( txt->i_line_count >= i_line_max )
657 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
661 if( txt->i_line_count <= 0 )
669 static void TextUnload( text_t *txt )
673 for( i = 0; i < txt->i_line_count; i++ )
675 free( txt->line[i] );
679 txt->i_line_count = 0;
682 static char *TextGetLine( text_t *txt )
684 if( txt->i_line >= txt->i_line_count )
687 return txt->line[txt->i_line++];
689 static void TextPreviousLine( text_t *txt )
691 if( txt->i_line > 0 )
695 /*****************************************************************************
696 * Specific Subtitle function
697 *****************************************************************************/
698 #define MAX_LINE 8192
699 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
701 demux_sys_t *p_sys = p_demux->p_sys;
702 text_t *txt = &p_sys->txt;
705 * {n1}{n2}Line1|Line2|Line3....
706 * where n1 and n2 are the video frame number...
707 * {n2} can also be {}
711 char buffer_text[MAX_LINE + 1];
716 p_subtitle->i_start = 0;
717 p_subtitle->i_stop = 0;
718 p_subtitle->psz_text = NULL;
723 if( ( s = TextGetLine( txt ) ) == NULL )
725 return( VLC_EGENERIC );
730 memset( buffer_text, '\0', MAX_LINE );
731 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
732 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
737 if( i_start == 1 && i_stop == 1 )
739 /* We found a possible setting of the framerate "{1}{1}23.976" */
740 /* Check if it's usable, and if the sub-fps is not set */
741 float tmp = us_strtod( buffer_text, NULL );
742 if( tmp > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
743 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / tmp );
747 /* replace | by \n */
748 for( i = 0; i < strlen( buffer_text ); i++ )
750 if( buffer_text[i] == '|' )
752 buffer_text[i] = '\n';
756 p_subtitle->i_start = (int64_t)i_start * p_sys->i_microsecperframe;
757 p_subtitle->i_stop = (int64_t)i_stop * p_sys->i_microsecperframe;
758 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
762 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
764 demux_sys_t *p_sys = p_demux->p_sys;
765 text_t *txt = &p_sys->txt;
769 * h1:m1:s1,d1 --> h2:m2:s2,d2
777 char buffer_text[ 10 * MAX_LINE];
782 p_subtitle->i_start = 0;
783 p_subtitle->i_stop = 0;
784 p_subtitle->psz_text = NULL;
788 int h1, m1, s1, d1, h2, m2, s2, d2;
789 if( ( s = TextGetLine( txt ) ) == NULL )
791 return( VLC_EGENERIC );
794 "%d:%d:%d,%d --> %d:%d:%d,%d",
796 &h2, &m2, &s2, &d2 ) == 8 )
798 i_start = ( (int64_t)h1 * 3600*1000 +
799 (int64_t)m1 * 60*1000 +
801 (int64_t)d1 ) * 1000;
803 i_stop = ( (int64_t)h2 * 3600*1000 +
804 (int64_t)m2 * 60*1000 +
806 (int64_t)d2 ) * 1000;
808 /* Now read text until an empty line */
809 for( i_buffer_text = 0;; )
812 if( ( s = TextGetLine( txt ) ) == NULL )
814 return( VLC_EGENERIC );
820 /* empty line -> end of this subtitle */
821 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
822 p_subtitle->i_start = i_start;
823 p_subtitle->i_stop = i_stop;
824 p_subtitle->psz_text = strdup( buffer_text );
829 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
831 memcpy( buffer_text + i_buffer_text,
834 i_buffer_text += i_len;
836 buffer_text[i_buffer_text] = '\n';
845 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
847 demux_sys_t *p_sys = p_demux->p_sys;
848 text_t *txt = &p_sys->txt;
851 * h1:m1:s1.d1,h2:m2:s2.d2
856 * ( works with subviewer and subviewer v2 )
859 char buffer_text[ 10 * MAX_LINE];
864 p_subtitle->i_start = 0;
865 p_subtitle->i_stop = 0;
866 p_subtitle->psz_text = NULL;
870 int h1, m1, s1, d1, h2, m2, s2, d2;
871 if( ( s = TextGetLine( txt ) ) == NULL )
873 return( VLC_EGENERIC );
876 "%d:%d:%d.%d,%d:%d:%d.%d",
878 &h2, &m2, &s2, &d2 ) == 8 )
880 i_start = ( (int64_t)h1 * 3600*1000 +
881 (int64_t)m1 * 60*1000 +
883 (int64_t)d1 ) * 1000;
885 i_stop = ( (int64_t)h2 * 3600*1000 +
886 (int64_t)m2 * 60*1000 +
888 (int64_t)d2 ) * 1000;
890 /* Now read text until an empty line */
891 for( i_buffer_text = 0;; )
894 if( ( s = TextGetLine( txt ) ) == NULL )
896 return( VLC_EGENERIC );
902 /* empty line -> end of this subtitle */
903 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
904 p_subtitle->i_start = i_start;
905 p_subtitle->i_stop = i_stop;
907 /* replace [br] by \n */
908 for( i = 0; i < i_buffer_text - 3; i++ )
910 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
911 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
913 char *temp = buffer_text + i + 1;
914 buffer_text[i] = '\n';
915 memmove( temp, temp+3, strlen( temp ) -3 );
916 temp[strlen( temp )-3] = '\0';
919 p_subtitle->psz_text = strdup( buffer_text );
924 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
926 memcpy( buffer_text + i_buffer_text,
929 i_buffer_text += i_len;
931 buffer_text[i_buffer_text] = '\n';
941 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
943 demux_sys_t *p_sys = p_demux->p_sys;
944 text_t *txt = &p_sys->txt;
946 char buffer_text[ 10 * MAX_LINE];
947 char buffer_text2[ 10 * MAX_LINE];
952 p_subtitle->i_start = 0;
953 p_subtitle->i_stop = 0;
954 p_subtitle->psz_text = NULL;
958 int h1, m1, s1, c1, h2, m2, s2, c2;
960 if( ( s = TextGetLine( txt ) ) == NULL )
962 return( VLC_EGENERIC );
964 p_subtitle->psz_text = malloc( strlen( s ) );
966 /* We expect (SSA2-4):
967 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
968 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
970 * 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.
974 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
975 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
978 "Dialogue: %[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
982 buffer_text ) == 10 )
984 i_start = ( (int64_t)h1 * 3600*1000 +
985 (int64_t)m1 * 60*1000 +
987 (int64_t)c1 * 10 ) * 1000;
989 i_stop = ( (int64_t)h2 * 3600*1000 +
990 (int64_t)m2 * 60*1000 +
992 (int64_t)c2 * 10 ) * 1000;
994 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
995 /* (Layer comes from ASS specs ... it's empty for SSA.) */
996 if( p_sys->i_type == SUB_TYPE_SSA1 )
998 sprintf( p_subtitle->psz_text,
999 ",%s", strdup( buffer_text) ); /* SSA1 has only 8 commas before the text starts, not 9 */
1003 sprintf( p_subtitle->psz_text,
1004 ",,%s", strdup( buffer_text) ); /* ReadOrder, Layer, %s(rest of fields) */
1006 p_subtitle->i_start = i_start;
1007 p_subtitle->i_stop = i_stop;
1012 /* All the other stuff we add to the header field */
1013 if( p_sys->psz_header != NULL )
1015 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
1016 strlen( p_sys->psz_header ) + 1 + strlen( s ) + 2 ) ) )
1018 msg_Err( p_demux, "out of memory");
1021 p_sys->psz_header = strcat( p_sys->psz_header, s );
1022 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1026 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
1028 msg_Err( p_demux, "out of memory");
1031 p_sys->psz_header = s;
1032 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1038 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1040 demux_sys_t *p_sys = p_demux->p_sys;
1041 text_t *txt = &p_sys->txt;
1045 * h:m:s:Line1|Line2|Line3....
1047 * h:m:s Line1|Line2|Line3....
1051 char buffer_text[MAX_LINE + 1];
1055 p_subtitle->i_start = 0;
1056 p_subtitle->i_stop = 0;
1057 p_subtitle->psz_text = NULL;
1064 if( ( p = TextGetLine( txt ) ) == NULL )
1066 return( VLC_EGENERIC );
1071 memset( buffer_text, '\0', MAX_LINE );
1072 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1074 i_start = ( (int64_t)h * 3600*1000 +
1075 (int64_t)m * 60*1000 +
1076 (int64_t)s * 1000 ) * 1000;
1081 /* replace | by \n */
1082 for( i = 0; i < strlen( buffer_text ); i++ )
1084 if( buffer_text[i] == '|' )
1086 buffer_text[i] = '\n';
1089 p_subtitle->i_start = i_start;
1091 p_subtitle->i_stop = 0;
1092 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1096 static char *ParseSamiSearch( text_t *txt, char *psz_start, const char *psz_str )
1100 if( strcasestr( psz_start, psz_str ) )
1102 char *s = strcasestr( psz_start, psz_str );
1104 s += strlen( psz_str );
1112 if( ( p = TextGetLine( txt ) ) == NULL )
1116 if( strcasestr( p, psz_str ) )
1118 char *s = strcasestr( p, psz_str );
1120 s += strlen( psz_str );
1127 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1129 demux_sys_t *p_sys = p_demux->p_sys;
1130 text_t *txt = &p_sys->txt;
1136 char buffer_text[10*MAX_LINE + 1];
1138 p_subtitle->i_start = 0;
1139 p_subtitle->i_stop = 0;
1140 p_subtitle->psz_text = NULL;
1143 if( i_text < 10*MAX_LINE ) \
1145 buffer_text[i_text++] = c; \
1146 buffer_text[i_text] = '\0'; \
1149 /* search "Start=" */
1150 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1152 return VLC_EGENERIC;
1155 /* get start value */
1156 i_start = strtol( p, &p, 0 );
1159 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1161 return VLC_EGENERIC;
1164 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1166 return VLC_EGENERIC;
1170 buffer_text[0] = '\0';
1171 /* now get all txt until a "Start=" line */
1178 if( !strncasecmp( p, "<br", 3 ) )
1182 else if( strcasestr( p, "Start=" ) )
1184 TextPreviousLine( txt );
1187 p = ParseSamiSearch( txt, p, ">" );
1189 else if( !strncmp( p, " ", 6 ) )
1194 else if( *p == '\t' )
1207 p = TextGetLine( txt );
1216 p_subtitle->i_start = i_start * 1000;
1217 p_subtitle->i_stop = 0;
1218 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1220 return( VLC_SUCCESS );
1224 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1226 demux_sys_t *p_sys = p_demux->p_sys;
1227 text_t *txt = &p_sys->txt;
1238 char buffer_text[ 10 * MAX_LINE];
1242 p_subtitle->i_start = 0;
1243 p_subtitle->i_stop = 0;
1244 p_subtitle->psz_text = NULL;
1249 if( ( s = TextGetLine( txt ) ) == NULL )
1251 return( VLC_EGENERIC );
1255 &h1, &m1, &s1, &c1 ) == 4 )
1257 i_start = ( (int64_t)h1 * 3600*1000 +
1258 (int64_t)m1 * 60*1000 +
1259 (int64_t)s1 * 1000 +
1260 (int64_t)c1 * 10) * 1000;
1262 /* Now read text until a line containing "}" */
1263 for( i_buffer_text = 0;; )
1266 if( ( s = TextGetLine( txt ) ) == NULL )
1268 return( VLC_EGENERIC );
1271 i_len = strlen( s );
1272 if( i_len == 1 && s[0] == '}' )
1274 /* "}" -> end of this subtitle */
1275 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
1276 p_subtitle->i_start = i_start;
1277 p_subtitle->i_stop = 0;
1278 p_subtitle->psz_text = strdup( buffer_text );
1283 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
1285 memcpy( buffer_text + i_buffer_text,
1288 i_buffer_text += i_len;
1290 buffer_text[i_buffer_text] = '\n';
1299 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle )
1301 demux_sys_t *p_sys = p_demux->p_sys;
1302 text_t *txt = &p_sys->txt;
1305 * [n1][n2]Line1|Line2|Line3....
1306 * where n1 and n2 are the video frame number...
1307 * [n2] can also be []
1311 char buffer_text[MAX_LINE + 1];
1316 p_subtitle->i_start = 0;
1317 p_subtitle->i_stop = 0;
1318 p_subtitle->psz_text = NULL;
1322 if( ( s = TextGetLine( txt ) ) == NULL )
1324 return( VLC_EGENERIC );
1329 memset( buffer_text, '\0', MAX_LINE );
1330 if( sscanf( s, "[%d][]%[^\r\n]", &i_start, buffer_text ) == 2 ||
1331 sscanf( s, "[%d][%d]%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
1337 /* replace | by \n */
1338 for( i = 0; i < strlen( buffer_text ); i++ )
1340 if( buffer_text[i] == '|' )
1342 buffer_text[i] = '\n';
1345 p_subtitle->i_start = (int64_t)i_start * 100000;
1346 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1347 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );