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 *****************************************************************************/
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 "Apply a delay to all subtitles (in 1/10s, eg 100 means 10s)."
48 #define SUB_FPS_LONGTEXT \
49 "Override the normal frames per second settings. " \
50 "This will only work with MicroDVD and SubRIP (SRT) subtitles."
51 #define SUB_TYPE_LONGTEXT \
52 "Force the subtiles format. Valid values are : \"microdvd\", \"subrip\"," \
53 "\"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
54 "\"sami\", \"dvdsubtitle\" and \"auto\" (meaning autodetection, this " \
55 "should always work)."
56 static char *ppsz_sub_type[] =
58 "auto", "microdvd", "subrip", "subviewer", "ssa1",
59 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle"
63 set_shortname( _("Subtitles"));
64 set_description( _("Text subtitles parser") );
65 set_capability( "demux2", 0 );
66 set_category( CAT_INPUT );
67 set_subcategory( SUBCAT_INPUT_DEMUX );
68 add_float( "sub-fps", 0.0, NULL,
69 N_("Frames per second"),
70 SUB_FPS_LONGTEXT, VLC_TRUE );
71 add_integer( "sub-delay", 0, NULL,
72 N_("Subtitles delay"),
73 SUB_DELAY_LONGTEXT, VLC_TRUE );
74 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
75 SUB_TYPE_LONGTEXT, VLC_TRUE );
76 change_string_list( ppsz_sub_type, 0, 0 );
77 set_callbacks( Open, Close );
79 add_shortcut( "subtitle" );
82 /*****************************************************************************
84 *****************************************************************************/
87 SUB_TYPE_UNKNOWN = -1,
105 static int TextLoad( text_t *, stream_t *s );
106 static void TextUnload( text_t * );
123 int64_t i_next_demux_date;
125 int64_t i_microsecperframe;
126 int64_t i_original_mspf;
131 subtitle_t *subtitle;
136 static int ParseMicroDvd ( demux_t *, subtitle_t * );
137 static int ParseSubRip ( demux_t *, subtitle_t * );
138 static int ParseSubViewer ( demux_t *, subtitle_t * );
139 static int ParseSSA ( demux_t *, subtitle_t * );
140 static int ParseVplayer ( demux_t *, subtitle_t * );
141 static int ParseSami ( demux_t *, subtitle_t * );
142 static int ParseDVDSubtitle( demux_t *, subtitle_t * );
149 int (*pf_read)( demux_t *, subtitle_t* );
150 } sub_read_subtitle_function [] =
152 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
153 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
154 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
155 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
156 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
157 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
158 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
159 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
160 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
161 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
164 static int Demux( demux_t * );
165 static int Control( demux_t *, int, va_list );
167 /*static void Fix( demux_t * );*/
169 /*****************************************************************************
171 *****************************************************************************/
172 static int Open ( vlc_object_t *p_this )
174 demux_t *p_demux = (demux_t*)p_this;
179 int (*pf_read)( demux_t *, subtitle_t* );
182 if( strcmp( p_demux->psz_demux, "subtitle" ) )
184 msg_Dbg( p_demux, "subtitle demux discarded" );
188 p_demux->pf_demux = Demux;
189 p_demux->pf_control = Control;
190 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
191 p_sys->psz_header = NULL;
192 p_sys->i_subtitle = 0;
193 p_sys->i_subtitles= 0;
194 p_sys->subtitle = NULL;
198 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
201 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
205 p_sys->i_microsecperframe = 0;
208 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
211 p_sys->i_original_mspf = (int64_t)( (float)1000000 / f_fps );
215 p_sys->i_original_mspf = 0;
218 /* Get or probe the type */
219 p_sys->i_type = SUB_TYPE_UNKNOWN;
220 psz_type = var_CreateGetString( p_demux, "sub-type" );
227 if( sub_read_subtitle_function[i].psz_type_name == NULL )
230 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
233 p_sys->i_type = sub_read_subtitle_function[i].i_type;
240 /* Probe if unknown type */
241 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
246 msg_Dbg( p_demux, "autodetecting subtitle format" );
247 for( i_try = 0; i_try < 256; i_try++ )
251 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
254 if( strcasestr( s, "<SAMI>" ) )
256 p_sys->i_type = SUB_TYPE_SAMI;
259 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
260 sscanf( s, "{%d}{}", &i_dummy ) == 1)
262 p_sys->i_type = SUB_TYPE_MICRODVD;
266 "%d:%d:%d,%d --> %d:%d:%d,%d",
267 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
268 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
270 p_sys->i_type = SUB_TYPE_SUBRIP;
273 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
275 p_sys->i_type = SUB_TYPE_SSA1;
278 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
280 p_sys->i_type = SUB_TYPE_ASS;
283 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
285 p_sys->i_type = SUB_TYPE_SSA2_4;
288 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
290 p_sys->i_type = SUB_TYPE_SSA2_4;
293 else if( !strncasecmp( s, "Dialogue:", 9 ) )
295 p_sys->i_type = SUB_TYPE_ASS;
298 else if( strcasestr( s, "[INFORMATION]" ) )
300 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
303 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
304 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
306 p_sys->i_type = SUB_TYPE_VPLAYER;
309 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
310 &i_dummy, &i_dummy ) == 4 )
312 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
322 /* It will nearly always work even for non seekable stream thanks the
323 * caching system, and if it fails we loose just a few sub */
324 if( stream_Seek( p_demux->s, 0 ) )
326 msg_Warn( p_demux, "failed to rewind" );
329 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
331 msg_Err( p_demux, "failed to recognize subtitle type" );
338 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
340 msg_Dbg( p_demux, "detected %s format",
341 sub_read_subtitle_function[i].psz_name );
342 pf_read = sub_read_subtitle_function[i].pf_read;
347 msg_Dbg( p_demux, "loading all subtitles..." );
349 /* Load the whole file */
350 TextLoad( &p_sys->txt, p_demux->s );
355 if( p_sys->i_subtitles >= i_max )
358 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
359 sizeof(subtitle_t) * i_max ) ) )
361 msg_Err( p_demux, "out of memory");
362 if( p_sys->subtitle != NULL )
363 free( p_sys->subtitle );
364 TextUnload( &p_sys->txt );
370 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
373 p_sys->i_subtitles++;
376 TextUnload( &p_sys->txt );
378 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
380 /* Fix subtitle (order and time) *** */
381 p_sys->i_subtitle = 0;
383 if( p_sys->i_subtitles > 0 )
385 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
387 if( p_sys->i_length <= 0 )
388 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
391 /* *** add subtitle ES *** */
392 if( p_sys->i_type == SUB_TYPE_SSA1 ||
393 p_sys->i_type == SUB_TYPE_SSA2_4 ||
394 p_sys->i_type == SUB_TYPE_ASS )
396 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
400 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
402 if( p_sys->psz_header != NULL )
404 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
405 fmt.p_extra = strdup( p_sys->psz_header );
407 p_sys->es = es_out_Add( p_demux->out, &fmt );
412 /*****************************************************************************
413 * Close: Close subtitle demux
414 *****************************************************************************/
415 static void Close( vlc_object_t *p_this )
417 demux_t *p_demux = (demux_t*)p_this;
418 demux_sys_t *p_sys = p_demux->p_sys;
421 for( i = 0; i < p_sys->i_subtitles; i++ )
423 if( p_sys->subtitle[i].psz_text )
424 free( p_sys->subtitle[i].psz_text );
426 if( p_sys->subtitle )
427 free( p_sys->subtitle );
432 /*****************************************************************************
434 *****************************************************************************/
435 static int Control( demux_t *p_demux, int i_query, va_list args )
437 demux_sys_t *p_sys = p_demux->p_sys;
443 case DEMUX_GET_LENGTH:
444 pi64 = (int64_t*)va_arg( args, int64_t * );
445 *pi64 = p_sys->i_length;
449 pi64 = (int64_t*)va_arg( args, int64_t * );
450 if( p_sys->i_subtitle < p_sys->i_subtitles )
452 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
458 i64 = (int64_t)va_arg( args, int64_t );
459 p_sys->i_subtitle = 0;
460 while( p_sys->i_subtitle < p_sys->i_subtitles &&
461 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
466 if( p_sys->i_subtitle >= p_sys->i_subtitles )
470 case DEMUX_GET_POSITION:
471 pf = (double*)va_arg( args, double * );
472 if( p_sys->i_subtitle >= p_sys->i_subtitles )
476 else if( p_sys->i_subtitles > 0 )
478 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
479 (double)p_sys->i_length;
487 case DEMUX_SET_POSITION:
488 f = (double)va_arg( args, double );
489 i64 = f * p_sys->i_length;
491 p_sys->i_subtitle = 0;
492 while( p_sys->i_subtitle < p_sys->i_subtitles &&
493 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
497 if( p_sys->i_subtitle >= p_sys->i_subtitles )
501 case DEMUX_SET_NEXT_DEMUX_TIME:
502 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
507 case DEMUX_GET_TITLE_INFO:
511 msg_Err( p_demux, "unknown query in subtitle control" );
516 /*****************************************************************************
517 * Demux: Send subtitle to decoder
518 *****************************************************************************/
519 static int Demux( demux_t *p_demux )
521 demux_sys_t *p_sys = p_demux->p_sys;
524 if( p_sys->i_subtitle >= p_sys->i_subtitles )
527 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
528 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
530 /* Should not happen */
531 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
534 while( p_sys->i_subtitle < p_sys->i_subtitles &&
535 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
538 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
547 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
553 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
559 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
560 p_block->i_dts = p_block->i_pts;
561 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
564 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
567 memcpy( p_block->p_buffer,
568 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
569 if( p_block->i_pts > 0 )
571 es_out_Send( p_demux->out, p_sys->es, p_block );
575 block_Release( p_block );
581 p_sys->i_next_demux_date = 0;
586 /*****************************************************************************
587 * Fix: fix time stamp and order of subtitle
588 *****************************************************************************/
589 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
590 static void Fix( demux_t *p_demux )
592 demux_sys_t *p_sys = p_demux->p_sys;
596 /* *** fix order (to be sure...) *** */
597 /* We suppose that there are near in order and this durty bubble sort
598 * wont take too much time
603 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
605 if( p_sys->subtitle[i_index].i_start <
606 p_sys->subtitle[i_index - 1].i_start )
610 p_sys->subtitle + i_index - 1,
611 sizeof( subtitle_t ) );
612 memcpy( p_sys->subtitle + i_index - 1,
613 p_sys->subtitle + i_index,
614 sizeof( subtitle_t ) );
615 memcpy( p_sys->subtitle + i_index,
617 sizeof( subtitle_t ) );
625 static int TextLoad( text_t *txt, stream_t *s )
631 txt->i_line_count = 0;
633 txt->line = calloc( i_line_max, sizeof( char * ) );
635 /* load the complete file */
638 char *psz = stream_ReadLine( s );
643 txt->line[txt->i_line_count++] = psz;
644 if( txt->i_line_count >= i_line_max )
647 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
651 if( txt->i_line_count <= 0 )
659 static void TextUnload( text_t *txt )
663 for( i = 0; i < txt->i_line_count; i++ )
665 free( txt->line[i] );
669 txt->i_line_count = 0;
672 static char *TextGetLine( text_t *txt )
674 if( txt->i_line >= txt->i_line_count )
677 return txt->line[txt->i_line++];
679 static void TextPreviousLine( text_t *txt )
681 if( txt->i_line > 0 )
685 /*****************************************************************************
686 * Specific Subtitle function
687 *****************************************************************************/
688 #define MAX_LINE 8192
689 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
691 demux_sys_t *p_sys = p_demux->p_sys;
692 text_t *txt = &p_sys->txt;
695 * {n1}{n2}Line1|Line2|Line3....
696 * where n1 and n2 are the video frame number...
697 * {n2} can also be {}
701 char buffer_text[MAX_LINE + 1];
706 int i_microsecperframe = 40000; /* default to 25 fps */
707 if( p_sys->i_microsecperframe > 0 )
708 i_microsecperframe = p_sys->i_microsecperframe;
710 p_subtitle->i_start = 0;
711 p_subtitle->i_stop = 0;
712 p_subtitle->psz_text = NULL;
716 if( ( s = TextGetLine( txt ) ) == NULL )
718 return( VLC_EGENERIC );
723 memset( buffer_text, '\0', MAX_LINE );
724 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
725 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
730 /* replace | by \n */
731 for( i = 0; i < strlen( buffer_text ); i++ )
733 if( buffer_text[i] == '|' )
735 buffer_text[i] = '\n';
739 p_subtitle->i_start = (int64_t)i_start * i_microsecperframe;
740 p_subtitle->i_stop = (int64_t)i_stop * i_microsecperframe;
741 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
745 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
747 demux_sys_t *p_sys = p_demux->p_sys;
748 text_t *txt = &p_sys->txt;
752 * h1:m1:s1,d1 --> h2:m2:s2,d2
760 char buffer_text[ 10 * MAX_LINE];
765 p_subtitle->i_start = 0;
766 p_subtitle->i_stop = 0;
767 p_subtitle->psz_text = NULL;
771 int h1, m1, s1, d1, h2, m2, s2, d2;
772 if( ( s = TextGetLine( txt ) ) == NULL )
774 return( VLC_EGENERIC );
777 "%d:%d:%d,%d --> %d:%d:%d,%d",
779 &h2, &m2, &s2, &d2 ) == 8 )
781 i_start = ( (int64_t)h1 * 3600*1000 +
782 (int64_t)m1 * 60*1000 +
784 (int64_t)d1 ) * 1000;
786 i_stop = ( (int64_t)h2 * 3600*1000 +
787 (int64_t)m2 * 60*1000 +
789 (int64_t)d2 ) * 1000;
791 /* Now read text until an empty line */
792 for( i_buffer_text = 0;; )
795 if( ( s = TextGetLine( txt ) ) == NULL )
797 return( VLC_EGENERIC );
803 /* empty line -> end of this subtitle */
804 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
805 p_subtitle->i_start = i_start;
806 p_subtitle->i_stop = i_stop;
807 p_subtitle->psz_text = strdup( buffer_text );
808 /* If framerate is available, use sub-fps */
809 if( p_sys->i_microsecperframe != 0 &&
810 p_sys->i_original_mspf != 0)
812 p_subtitle->i_start = (int64_t)i_start *
813 p_sys->i_microsecperframe/
814 p_sys->i_original_mspf;
815 p_subtitle->i_stop = (int64_t)i_stop *
816 p_sys->i_microsecperframe /
817 p_sys->i_original_mspf;
823 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
825 memcpy( buffer_text + i_buffer_text,
828 i_buffer_text += i_len;
830 buffer_text[i_buffer_text] = '\n';
839 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
841 demux_sys_t *p_sys = p_demux->p_sys;
842 text_t *txt = &p_sys->txt;
845 * h1:m1:s1.d1,h2:m2:s2.d2
850 * ( works with subviewer and subviewer v2 )
853 char buffer_text[ 10 * MAX_LINE];
858 p_subtitle->i_start = 0;
859 p_subtitle->i_stop = 0;
860 p_subtitle->psz_text = NULL;
864 int h1, m1, s1, d1, h2, m2, s2, d2;
865 if( ( s = TextGetLine( txt ) ) == NULL )
867 return( VLC_EGENERIC );
870 "%d:%d:%d.%d,%d:%d:%d.%d",
872 &h2, &m2, &s2, &d2 ) == 8 )
874 i_start = ( (int64_t)h1 * 3600*1000 +
875 (int64_t)m1 * 60*1000 +
877 (int64_t)d1 ) * 1000;
879 i_stop = ( (int64_t)h2 * 3600*1000 +
880 (int64_t)m2 * 60*1000 +
882 (int64_t)d2 ) * 1000;
884 /* Now read text until an empty line */
885 for( i_buffer_text = 0;; )
888 if( ( s = TextGetLine( txt ) ) == NULL )
890 return( VLC_EGENERIC );
896 /* empty line -> end of this subtitle */
897 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
898 p_subtitle->i_start = i_start;
899 p_subtitle->i_stop = i_stop;
901 /* replace [br] by \n */
902 for( i = 0; i < i_buffer_text - 3; i++ )
904 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
905 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
907 char *temp = buffer_text + i + 1;
908 buffer_text[i] = '\n';
909 memmove( temp, temp+3, strlen( temp ) -3 );
910 temp[strlen( temp )-3] = '\0';
913 p_subtitle->psz_text = strdup( buffer_text );
918 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
920 memcpy( buffer_text + i_buffer_text,
923 i_buffer_text += i_len;
925 buffer_text[i_buffer_text] = '\n';
935 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
937 demux_sys_t *p_sys = p_demux->p_sys;
938 text_t *txt = &p_sys->txt;
940 char buffer_text[ 10 * MAX_LINE];
941 char buffer_text2[ 10 * MAX_LINE];
946 p_subtitle->i_start = 0;
947 p_subtitle->i_stop = 0;
948 p_subtitle->psz_text = NULL;
952 int h1, m1, s1, c1, h2, m2, s2, c2;
954 if( ( s = TextGetLine( txt ) ) == NULL )
956 return( VLC_EGENERIC );
958 p_subtitle->psz_text = malloc( strlen( s ) );
960 /* We expect (SSA2-4):
961 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
962 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
964 * 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.
968 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
969 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
972 "Dialogue: %[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
976 buffer_text ) == 10 )
978 i_start = ( (int64_t)h1 * 3600*1000 +
979 (int64_t)m1 * 60*1000 +
981 (int64_t)c1 * 10 ) * 1000;
983 i_stop = ( (int64_t)h2 * 3600*1000 +
984 (int64_t)m2 * 60*1000 +
986 (int64_t)c2 * 10 ) * 1000;
988 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
989 /* (Layer comes from ASS specs ... it's empty for SSA.) */
990 if( p_sys->i_type == SUB_TYPE_SSA1 )
992 sprintf( p_subtitle->psz_text,
993 ",%s", strdup( buffer_text) ); /* SSA1 has only 8 commas before the text starts, not 9 */
997 sprintf( p_subtitle->psz_text,
998 ",,%s", strdup( buffer_text) ); /* ReadOrder, Layer, %s(rest of fields) */
1000 p_subtitle->i_start = i_start;
1001 p_subtitle->i_stop = i_stop;
1006 /* All the other stuff we add to the header field */
1007 if( p_sys->psz_header != NULL )
1009 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
1010 strlen( p_sys->psz_header ) + 1 + strlen( s ) + 2 ) ) )
1012 msg_Err( p_demux, "out of memory");
1015 p_sys->psz_header = strcat( p_sys->psz_header, s );
1016 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1020 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
1022 msg_Err( p_demux, "out of memory");
1025 p_sys->psz_header = s;
1026 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1032 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1034 demux_sys_t *p_sys = p_demux->p_sys;
1035 text_t *txt = &p_sys->txt;
1039 * h:m:s:Line1|Line2|Line3....
1041 * h:m:s Line1|Line2|Line3....
1045 char buffer_text[MAX_LINE + 1];
1049 p_subtitle->i_start = 0;
1050 p_subtitle->i_stop = 0;
1051 p_subtitle->psz_text = NULL;
1058 if( ( p = TextGetLine( txt ) ) == NULL )
1060 return( VLC_EGENERIC );
1065 memset( buffer_text, '\0', MAX_LINE );
1066 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1068 i_start = ( (int64_t)h * 3600*1000 +
1069 (int64_t)m * 60*1000 +
1070 (int64_t)s * 1000 ) * 1000;
1075 /* replace | by \n */
1076 for( i = 0; i < strlen( buffer_text ); i++ )
1078 if( buffer_text[i] == '|' )
1080 buffer_text[i] = '\n';
1083 p_subtitle->i_start = i_start;
1085 p_subtitle->i_stop = 0;
1086 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1090 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1094 if( strcasestr( psz_start, psz_str ) )
1096 char *s = strcasestr( psz_start, psz_str );
1098 s += strlen( psz_str );
1106 if( ( p = TextGetLine( txt ) ) == NULL )
1110 if( strcasestr( p, psz_str ) )
1112 char *s = strcasestr( p, psz_str );
1114 s += strlen( psz_str );
1121 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1123 demux_sys_t *p_sys = p_demux->p_sys;
1124 text_t *txt = &p_sys->txt;
1130 char buffer_text[10*MAX_LINE + 1];
1132 p_subtitle->i_start = 0;
1133 p_subtitle->i_stop = 0;
1134 p_subtitle->psz_text = NULL;
1137 if( i_text < 10*MAX_LINE ) \
1139 buffer_text[i_text++] = c; \
1140 buffer_text[i_text] = '\0'; \
1143 /* search "Start=" */
1144 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1146 return VLC_EGENERIC;
1149 /* get start value */
1150 i_start = strtol( p, &p, 0 );
1153 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1155 return VLC_EGENERIC;
1158 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1160 return VLC_EGENERIC;
1164 buffer_text[0] = '\0';
1165 /* now get all txt until a "Start=" line */
1172 if( !strncasecmp( p, "<br", 3 ) )
1176 else if( strcasestr( p, "Start=" ) )
1178 TextPreviousLine( txt );
1181 p = ParseSamiSearch( txt, p, ">" );
1183 else if( !strncmp( p, " ", 6 ) )
1188 else if( *p == '\t' )
1201 p = TextGetLine( txt );
1210 p_subtitle->i_start = i_start * 1000;
1211 p_subtitle->i_stop = 0;
1212 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1214 return( VLC_SUCCESS );
1218 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1220 demux_sys_t *p_sys = p_demux->p_sys;
1221 text_t *txt = &p_sys->txt;
1232 char buffer_text[ 10 * MAX_LINE];
1236 p_subtitle->i_start = 0;
1237 p_subtitle->i_stop = 0;
1238 p_subtitle->psz_text = NULL;
1243 if( ( s = TextGetLine( txt ) ) == NULL )
1245 return( VLC_EGENERIC );
1249 &h1, &m1, &s1, &c1 ) == 4 )
1251 i_start = ( (int64_t)h1 * 3600*1000 +
1252 (int64_t)m1 * 60*1000 +
1253 (int64_t)s1 * 1000 +
1254 (int64_t)c1 * 10) * 1000;
1256 /* Now read text until a line containing "}" */
1257 for( i_buffer_text = 0;; )
1260 if( ( s = TextGetLine( txt ) ) == NULL )
1262 return( VLC_EGENERIC );
1265 i_len = strlen( s );
1266 if( i_len == 1 && s[0] == '}' )
1268 /* "}" -> end of this subtitle */
1269 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
1270 p_subtitle->i_start = i_start;
1271 p_subtitle->i_stop = 0;
1272 p_subtitle->psz_text = strdup( buffer_text );
1277 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
1279 memcpy( buffer_text + i_buffer_text,
1282 i_buffer_text += i_len;
1284 buffer_text[i_buffer_text] = '\n';