1 /*****************************************************************************
2 * subtitle.c: Demux for subtitle text files.
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
37 #include <vlc/input.h>
38 #include "vlc_video.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t *p_this );
44 static void Close( vlc_object_t *p_this );
46 #define SUB_DELAY_LONGTEXT \
47 "Delay subtitles (in 1/10s)"
48 #define SUB_FPS_LONGTEXT \
49 "Override frames per second. " \
50 "It will only work with MicroDVD subtitles."
51 #define SUB_TYPE_LONGTEXT \
52 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
53 "\"sami\" (auto for autodetection, it should always work)."
54 static char *ppsz_sub_type[] =
56 "auto", "microdvd", "subrip", "subviewer", "ssa1",
57 "ssa2-4", "ass", "vplayer", "sami"
61 set_shortname( _("Subtitles"));
62 set_description( _("Text subtitles demux") );
63 set_capability( "demux2", 0 );
64 set_category( CAT_INPUT );
65 set_subcategory( SUBCAT_INPUT_DEMUX );
66 add_float( "sub-fps", 0.0, NULL,
67 N_("Frames per second"),
68 SUB_FPS_LONGTEXT, VLC_TRUE );
69 add_integer( "sub-delay", 0, NULL,
70 N_("Subtitles delay"),
71 SUB_DELAY_LONGTEXT, VLC_TRUE );
72 add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
73 SUB_TYPE_LONGTEXT, VLC_TRUE );
74 change_string_list( ppsz_sub_type, 0, 0 );
75 set_callbacks( Open, Close );
77 add_shortcut( "subtitle" );
80 /*****************************************************************************
82 *****************************************************************************/
85 SUB_TYPE_UNKNOWN = -1,
102 static int TextLoad( text_t *, stream_t *s );
103 static void TextUnload( text_t * );
120 int64_t i_next_demux_date;
122 int64_t i_microsecperframe;
123 int64_t i_original_mspf;
128 subtitle_t *subtitle;
133 static int ParseMicroDvd ( demux_t *, subtitle_t * );
134 static int ParseSubRip ( demux_t *, subtitle_t * );
135 static int ParseSubViewer( demux_t *, subtitle_t * );
136 static int ParseSSA ( demux_t *, subtitle_t * );
137 static int ParseVplayer ( demux_t *, subtitle_t * );
138 static int ParseSami ( demux_t *, subtitle_t * );
145 int (*pf_read)( demux_t *, subtitle_t* );
146 } sub_read_subtitle_function [] =
148 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
149 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
150 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer",ParseSubViewer },
151 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
152 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4",ParseSSA },
153 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
154 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
155 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
156 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
159 static int Demux( demux_t * );
160 static int Control( demux_t *, int, va_list );
162 static void Fix( demux_t * );
164 /*****************************************************************************
166 *****************************************************************************/
167 static int Open ( vlc_object_t *p_this )
169 demux_t *p_demux = (demux_t*)p_this;
174 int (*pf_read)( demux_t *, subtitle_t* );
177 if( strcmp( p_demux->psz_demux, "subtitle" ) )
179 msg_Dbg( p_demux, "subtitle demux discarded" );
183 p_demux->pf_demux = Demux;
184 p_demux->pf_control = Control;
185 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
186 p_sys->psz_header = NULL;
187 p_sys->i_subtitle = 0;
188 p_sys->i_subtitles= 0;
189 p_sys->subtitle = NULL;
193 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
196 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
200 p_sys->i_microsecperframe = 0;
203 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
206 p_sys->i_original_mspf = (int64_t)( (float)1000000 / f_fps );
210 p_sys->i_original_mspf = 0;
213 /* Get or probe the type */
214 p_sys->i_type = SUB_TYPE_UNKNOWN;
215 psz_type = var_CreateGetString( p_demux, "sub-type" );
222 if( sub_read_subtitle_function[i].psz_type_name == NULL )
225 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
228 p_sys->i_type = sub_read_subtitle_function[i].i_type;
235 /* Probe if unknown type */
236 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
241 msg_Dbg( p_demux, "autodetecting subtitle format" );
242 for( i_try = 0; i_try < 256; i_try++ )
246 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
249 if( strcasestr( s, "<SAMI>" ) )
251 p_sys->i_type = SUB_TYPE_SAMI;
254 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
255 sscanf( s, "{%d}{}", &i_dummy ) == 1)
257 p_sys->i_type = SUB_TYPE_MICRODVD;
261 "%d:%d:%d,%d --> %d:%d:%d,%d",
262 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
263 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
265 p_sys->i_type = SUB_TYPE_SUBRIP;
268 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
270 p_sys->i_type = SUB_TYPE_SSA1;
273 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
275 p_sys->i_type = SUB_TYPE_ASS;
278 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
280 p_sys->i_type = SUB_TYPE_SSA2_4;
283 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
285 p_sys->i_type = SUB_TYPE_SSA2_4;
288 else if( !strncasecmp( s, "Dialogue:", 9 ) )
290 p_sys->i_type = SUB_TYPE_ASS;
293 else if( strcasestr( s, "[INFORMATION]" ) )
295 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
298 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
299 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
301 p_sys->i_type = SUB_TYPE_VPLAYER;
311 /* It will nearly always work even for non seekable stream thanks the
312 * caching system, and if it fails we loose just a few sub */
313 if( stream_Seek( p_demux->s, 0 ) )
315 msg_Warn( p_demux, "failed to rewind" );
318 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
320 msg_Err( p_demux, "failed to recognize subtitle type" );
326 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
328 msg_Dbg( p_demux, "detected %s format",
329 sub_read_subtitle_function[i].psz_name );
330 pf_read = sub_read_subtitle_function[i].pf_read;
335 msg_Dbg( p_demux, "loading all subtitles..." );
337 /* Load the whole file */
338 TextLoad( &p_sys->txt, p_demux->s );
343 if( p_sys->i_subtitles >= i_max )
346 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
347 sizeof(subtitle_t) * i_max ) ) )
349 msg_Err( p_demux, "out of memory");
354 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
357 p_sys->i_subtitles++;
360 TextUnload( &p_sys->txt );
362 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
364 /* Fix subtitle (order and time) *** */
365 p_sys->i_subtitle = 0;
367 if( p_sys->i_subtitles > 0 )
369 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
371 if( p_sys->i_length <= 0 )
372 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
375 /* *** add subtitle ES *** */
376 if( p_sys->i_type == SUB_TYPE_SSA1 ||
377 p_sys->i_type == SUB_TYPE_SSA2_4 ||
378 p_sys->i_type == SUB_TYPE_ASS )
380 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
384 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
386 if( p_sys->psz_header != NULL )
388 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
389 fmt.p_extra = strdup( p_sys->psz_header );
391 p_sys->es = es_out_Add( p_demux->out, &fmt );
396 /*****************************************************************************
397 * Close: Close subtitle demux
398 *****************************************************************************/
399 static void Close( vlc_object_t *p_this )
401 demux_t *p_demux = (demux_t*)p_this;
402 demux_sys_t *p_sys = p_demux->p_sys;
405 for( i = 0; i < p_sys->i_subtitles; i++ )
407 if( p_sys->subtitle[i].psz_text )
408 free( p_sys->subtitle[i].psz_text );
410 if( p_sys->subtitle )
411 free( p_sys->subtitle );
416 /*****************************************************************************
418 *****************************************************************************/
419 static int Control( demux_t *p_demux, int i_query, va_list args )
421 demux_sys_t *p_sys = p_demux->p_sys;
427 case DEMUX_GET_LENGTH:
428 pi64 = (int64_t*)va_arg( args, int64_t * );
429 *pi64 = p_sys->i_length;
433 pi64 = (int64_t*)va_arg( args, int64_t * );
434 if( p_sys->i_subtitle < p_sys->i_subtitles )
436 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
442 i64 = (int64_t)va_arg( args, int64_t );
443 p_sys->i_subtitle = 0;
444 while( p_sys->i_subtitle < p_sys->i_subtitles &&
445 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
450 if( p_sys->i_subtitle >= p_sys->i_subtitles )
454 case DEMUX_GET_POSITION:
455 pf = (double*)va_arg( args, double * );
456 if( p_sys->i_subtitle >= p_sys->i_subtitles )
460 else if( p_sys->i_subtitles > 0 )
462 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
463 (double)p_sys->i_length;
471 case DEMUX_SET_POSITION:
472 f = (double)va_arg( args, double );
473 i64 = f * p_sys->i_length;
475 p_sys->i_subtitle = 0;
476 while( p_sys->i_subtitle < p_sys->i_subtitles &&
477 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
481 if( p_sys->i_subtitle >= p_sys->i_subtitles )
485 case DEMUX_SET_NEXT_DEMUX_TIME:
486 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
491 case DEMUX_GET_TITLE_INFO:
495 msg_Err( p_demux, "unknown query in subtitle control" );
500 /*****************************************************************************
501 * Demux: Send subtitle to decoder
502 *****************************************************************************/
503 static int Demux( demux_t *p_demux )
505 demux_sys_t *p_sys = p_demux->p_sys;
508 if( p_sys->i_subtitle >= p_sys->i_subtitles )
511 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
512 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
514 /* Should not happen */
515 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
518 while( p_sys->i_subtitle < p_sys->i_subtitles &&
519 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
522 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
531 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
537 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
543 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
544 p_block->i_dts = p_block->i_pts;
545 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
548 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
551 memcpy( p_block->p_buffer,
552 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
553 if( p_block->i_pts > 0 )
555 es_out_Send( p_demux->out, p_sys->es, p_block );
559 block_Release( p_block );
565 p_sys->i_next_demux_date = 0;
570 /*****************************************************************************
571 * Fix: fix time stamp and order of subtitle
572 *****************************************************************************/
573 static void Fix( demux_t *p_demux )
575 demux_sys_t *p_sys = p_demux->p_sys;
579 /* *** fix order (to be sure...) *** */
580 /* We suppose that there are near in order and this durty bubble sort
581 * wont take too much time
586 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
588 if( p_sys->subtitle[i_index].i_start <
589 p_sys->subtitle[i_index - 1].i_start )
593 p_sys->subtitle + i_index - 1,
594 sizeof( subtitle_t ) );
595 memcpy( p_sys->subtitle + i_index - 1,
596 p_sys->subtitle + i_index,
597 sizeof( subtitle_t ) );
598 memcpy( p_sys->subtitle + i_index,
600 sizeof( subtitle_t ) );
607 static int TextLoad( text_t *txt, stream_t *s )
613 txt->i_line_count = 0;
615 txt->line = calloc( i_line_max, sizeof( char * ) );
617 /* load the complete file */
620 char *psz = stream_ReadLine( s );
625 txt->line[txt->i_line_count++] = psz;
626 if( txt->i_line_count >= i_line_max )
629 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
633 if( txt->i_line_count <= 0 )
641 static void TextUnload( text_t *txt )
645 for( i = 0; i < txt->i_line_count; i++ )
647 free( txt->line[i] );
651 txt->i_line_count = 0;
654 static char *TextGetLine( text_t *txt )
656 if( txt->i_line >= txt->i_line_count )
659 return txt->line[txt->i_line++];
661 static void TextPreviousLine( text_t *txt )
663 if( txt->i_line > 0 )
667 /*****************************************************************************
668 * Specific Subtitle function
669 *****************************************************************************/
670 #define MAX_LINE 8192
671 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
673 demux_sys_t *p_sys = p_demux->p_sys;
674 text_t *txt = &p_sys->txt;
677 * {n1}{n2}Line1|Line2|Line3....
678 * where n1 and n2 are the video frame number...
679 * {n2} can also be {}
683 char buffer_text[MAX_LINE + 1];
688 int i_microsecperframe = 40000; /* default to 25 fps */
689 if( p_sys->i_microsecperframe > 0 )
690 i_microsecperframe = p_sys->i_microsecperframe;
692 p_subtitle->i_start = 0;
693 p_subtitle->i_stop = 0;
694 p_subtitle->psz_text = NULL;
698 if( ( s = TextGetLine( txt ) ) == NULL )
700 return( VLC_EGENERIC );
705 memset( buffer_text, '\0', MAX_LINE );
706 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
707 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
712 /* replace | by \n */
713 for( i = 0; i < strlen( buffer_text ); i++ )
715 if( buffer_text[i] == '|' )
717 buffer_text[i] = '\n';
721 p_subtitle->i_start = (int64_t)i_start * i_microsecperframe;
722 p_subtitle->i_stop = (int64_t)i_stop * i_microsecperframe;
723 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
727 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
729 demux_sys_t *p_sys = p_demux->p_sys;
730 text_t *txt = &p_sys->txt;
734 * h1:m1:s1,d1 --> h2:m2:s2,d2
742 char buffer_text[ 10 * MAX_LINE];
747 p_subtitle->i_start = 0;
748 p_subtitle->i_stop = 0;
749 p_subtitle->psz_text = NULL;
753 int h1, m1, s1, d1, h2, m2, s2, d2;
754 if( ( s = TextGetLine( txt ) ) == NULL )
756 return( VLC_EGENERIC );
759 "%d:%d:%d,%d --> %d:%d:%d,%d",
761 &h2, &m2, &s2, &d2 ) == 8 )
763 i_start = ( (int64_t)h1 * 3600*1000 +
764 (int64_t)m1 * 60*1000 +
766 (int64_t)d1 ) * 1000;
768 i_stop = ( (int64_t)h2 * 3600*1000 +
769 (int64_t)m2 * 60*1000 +
771 (int64_t)d2 ) * 1000;
773 /* Now read text until an empty line */
774 for( i_buffer_text = 0;; )
777 if( ( s = TextGetLine( txt ) ) == NULL )
779 return( VLC_EGENERIC );
785 /* empty line -> end of this subtitle */
786 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
787 p_subtitle->i_start = i_start;
788 p_subtitle->i_stop = i_stop;
789 p_subtitle->psz_text = strdup( buffer_text );
790 /* If framerate is available, use sub-fps */
791 if( p_sys->i_microsecperframe != 0 &&
792 p_sys->i_original_mspf != 0)
794 p_subtitle->i_start = (int64_t)i_start *
795 p_sys->i_microsecperframe/
796 p_sys->i_original_mspf;
797 p_subtitle->i_stop = (int64_t)i_stop *
798 p_sys->i_microsecperframe /
799 p_sys->i_original_mspf;
805 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
807 memcpy( buffer_text + i_buffer_text,
810 i_buffer_text += i_len;
812 buffer_text[i_buffer_text] = '\n';
821 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
823 demux_sys_t *p_sys = p_demux->p_sys;
824 text_t *txt = &p_sys->txt;
827 * h1:m1:s1.d1,h2:m2:s2.d2
832 * ( works with subviewer and subviewer v2 )
835 char buffer_text[ 10 * MAX_LINE];
840 p_subtitle->i_start = 0;
841 p_subtitle->i_stop = 0;
842 p_subtitle->psz_text = NULL;
846 int h1, m1, s1, d1, h2, m2, s2, d2;
847 if( ( s = TextGetLine( txt ) ) == NULL )
849 return( VLC_EGENERIC );
852 "%d:%d:%d.%d,%d:%d:%d.%d",
854 &h2, &m2, &s2, &d2 ) == 8 )
856 i_start = ( (int64_t)h1 * 3600*1000 +
857 (int64_t)m1 * 60*1000 +
859 (int64_t)d1 ) * 1000;
861 i_stop = ( (int64_t)h2 * 3600*1000 +
862 (int64_t)m2 * 60*1000 +
864 (int64_t)d2 ) * 1000;
866 /* Now read text until an empty line */
867 for( i_buffer_text = 0;; )
870 if( ( s = TextGetLine( txt ) ) == NULL )
872 return( VLC_EGENERIC );
878 /* empty line -> end of this subtitle */
879 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
880 p_subtitle->i_start = i_start;
881 p_subtitle->i_stop = i_stop;
883 /* replace [br] by \n */
884 for( i = 0; i < i_buffer_text - 3; i++ )
886 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
887 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
889 char *temp = buffer_text + i + 1;
890 buffer_text[i] = '\n';
891 memmove( temp, temp+3, strlen( temp ) -3 );
892 temp[strlen( temp )-3] = '\0';
895 p_subtitle->psz_text = strdup( buffer_text );
900 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
902 memcpy( buffer_text + i_buffer_text,
905 i_buffer_text += i_len;
907 buffer_text[i_buffer_text] = '\n';
917 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
919 demux_sys_t *p_sys = p_demux->p_sys;
920 text_t *txt = &p_sys->txt;
922 char buffer_text[ 10 * MAX_LINE];
923 char buffer_text2[ 10 * MAX_LINE];
928 p_subtitle->i_start = 0;
929 p_subtitle->i_stop = 0;
930 p_subtitle->psz_text = NULL;
934 int h1, m1, s1, c1, h2, m2, s2, c2;
936 if( ( s = TextGetLine( txt ) ) == NULL )
938 return( VLC_EGENERIC );
940 p_subtitle->psz_text = malloc( strlen( s ) );
942 /* We expect (SSA2-4):
943 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
944 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
946 * 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.
950 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
951 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
954 "Dialogue: %[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
958 buffer_text ) == 10 )
960 i_start = ( (int64_t)h1 * 3600*1000 +
961 (int64_t)m1 * 60*1000 +
963 (int64_t)c1 * 10 ) * 1000;
965 i_stop = ( (int64_t)h2 * 3600*1000 +
966 (int64_t)m2 * 60*1000 +
968 (int64_t)c2 * 10 ) * 1000;
970 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
971 /* (Layer comes from ASS specs ... it's empty for SSA.) */
972 if( p_sys->i_type == SUB_TYPE_SSA1 )
974 sprintf( p_subtitle->psz_text,
975 ",%s", strdup( buffer_text) ); /* SSA1 has only 8 commas before the text starts, not 9 */
979 sprintf( p_subtitle->psz_text,
980 ",,%s", strdup( buffer_text) ); /* ReadOrder, Layer, %s(rest of fields) */
982 p_subtitle->i_start = i_start;
983 p_subtitle->i_stop = i_stop;
988 /* All the other stuff we add to the header field */
989 if( p_sys->psz_header != NULL )
991 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
992 strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
994 msg_Err( p_demux, "out of memory");
997 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
998 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1002 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
1004 msg_Err( p_demux, "out of memory");
1007 p_sys->psz_header = strdup( s );
1008 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1014 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1016 demux_sys_t *p_sys = p_demux->p_sys;
1017 text_t *txt = &p_sys->txt;
1021 * h:m:s:Line1|Line2|Line3....
1023 * h:m:s Line1|Line2|Line3....
1027 char buffer_text[MAX_LINE + 1];
1031 p_subtitle->i_start = 0;
1032 p_subtitle->i_stop = 0;
1033 p_subtitle->psz_text = NULL;
1040 if( ( p = TextGetLine( txt ) ) == NULL )
1042 return( VLC_EGENERIC );
1047 memset( buffer_text, '\0', MAX_LINE );
1048 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1050 i_start = ( (int64_t)h * 3600*1000 +
1051 (int64_t)m * 60*1000 +
1052 (int64_t)s * 1000 ) * 1000;
1057 /* replace | by \n */
1058 for( i = 0; i < strlen( buffer_text ); i++ )
1060 if( buffer_text[i] == '|' )
1062 buffer_text[i] = '\n';
1065 p_subtitle->i_start = i_start;
1067 p_subtitle->i_stop = 0;
1068 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1072 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1076 if( strcasestr( psz_start, psz_str ) )
1078 char *s = strcasestr( psz_start, psz_str );
1080 s += strlen( psz_str );
1088 if( ( p = TextGetLine( txt ) ) == NULL )
1092 if( strcasestr( p, psz_str ) )
1094 char *s = strcasestr( p, psz_str );
1096 s += strlen( psz_str );
1103 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1105 demux_sys_t *p_sys = p_demux->p_sys;
1106 text_t *txt = &p_sys->txt;
1112 char buffer_text[10*MAX_LINE + 1];
1114 p_subtitle->i_start = 0;
1115 p_subtitle->i_stop = 0;
1116 p_subtitle->psz_text = NULL;
1119 if( i_text < 10*MAX_LINE ) \
1121 buffer_text[i_text++] = c; \
1122 buffer_text[i_text] = '\0'; \
1125 /* search "Start=" */
1126 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1128 return VLC_EGENERIC;
1131 /* get start value */
1132 i_start = strtol( p, &p, 0 );
1135 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1137 return VLC_EGENERIC;
1140 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1142 return VLC_EGENERIC;
1146 buffer_text[0] = '\0';
1147 /* now get all txt until a "Start=" line */
1154 if( !strncasecmp( p, "<br", 3 ) )
1158 else if( strcasestr( p, "Start=" ) )
1160 TextPreviousLine( txt );
1163 p = ParseSamiSearch( txt, p, ">" );
1165 else if( !strncmp( p, " ", 6 ) )
1170 else if( *p == '\t' )
1183 p = TextGetLine( txt );
1192 p_subtitle->i_start = i_start * 1000;
1193 p_subtitle->i_stop = 0;
1194 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1196 return( VLC_SUCCESS );