1 /*****************************************************************************
2 * sub.c: subtitle demux for external subtitle 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 *****************************************************************************/
30 #include <sys/types.h>
34 #include <vlc/input.h>
35 #include "vlc_video.h"
40 #if (!defined( WIN32 ) || defined(__MINGW32__))
44 #define DVD_VIDEO_LB_LEN 2048
46 static int Open ( vlc_object_t *p_this );
48 static int sub_open ( subtitle_demux_t *p_sub,
49 input_thread_t *p_input,
51 mtime_t i_microsecperframe );
52 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate );
53 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date );
54 static void sub_close( subtitle_demux_t *p_sub );
56 static void sub_fix( subtitle_demux_t *p_sub );
58 static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1",
59 "ssa2-4", "vplayer", "sami", "vobsub" };
61 /*****************************************************************************
63 *****************************************************************************/
64 #define SUB_DELAY_LONGTEXT \
65 "Delay subtitles (in 1/10s)"
66 #define SUB_FPS_LONGTEXT \
67 "Override frames per second. " \
68 "It will only work with MicroDVD subtitles."
69 #define SUB_TYPE_LONGTEXT \
70 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
71 "\"sami\" (auto for autodetection, it should always work)."
74 set_description( _("Text subtitles demux") );
75 set_capability( "subtitle demux", 12 );
76 add_float( "sub-fps", 0.0, NULL,
77 N_("Frames per second"),
78 SUB_FPS_LONGTEXT, VLC_TRUE );
79 add_integer( "sub-delay", 0, NULL,
80 N_("Delay subtitles (in 1/10s)"),
81 SUB_DELAY_LONGTEXT, VLC_TRUE );
82 add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
83 SUB_TYPE_LONGTEXT, VLC_TRUE );
84 change_string_list( ppsz_sub_type, 0, 0 );
85 set_callbacks( Open, NULL );
88 /*****************************************************************************
90 *****************************************************************************/
91 static int Open ( vlc_object_t *p_this )
93 subtitle_demux_t *p_sub = (subtitle_demux_t*)p_this;
95 p_sub->pf_open = sub_open;
96 p_sub->pf_demux = sub_demux;
97 p_sub->pf_seek = sub_seek;
98 p_sub->pf_close = sub_close;
100 /* Initialize the variables */
101 var_Create( p_this, "sub-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
102 var_Create( p_this, "sub-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
103 var_Create( p_this, "sub-type", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
108 #define MAX_LINE 2048
110 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
119 static int text_load( text_t *txt, char *psz_name )
126 txt->i_line_count = 0;
128 txt->line = calloc( i_line_max, sizeof( char * ) );
131 if( !( f = fopen( psz_name, "rb" ) ) )
136 /* load the complete file */
142 if( fgets( buffer, 8096, f ) <= 0)
146 while( ( p = strchr( buffer, '\r' ) ) )
150 while( ( p = strchr( buffer, '\n' ) ) )
155 txt->line[txt->i_line_count++] = strdup( buffer );
157 if( txt->i_line_count >= i_line_max )
160 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
166 if( txt->i_line_count <= 0 )
169 return( VLC_EGENERIC );
172 return( VLC_SUCCESS );
174 static void text_unload( text_t *txt )
178 for( i = 0; i < txt->i_line_count; i++ )
180 FREE( txt->line[i] );
184 txt->i_line_count = 0;
187 static char *text_get_line( text_t *txt )
189 if( txt->i_line >= txt->i_line_count )
194 return( txt->line[txt->i_line++] );
196 static void text_previous_line( text_t *txt )
198 if( txt->i_line > 0 )
203 static void text_rewind( text_t *txt )
208 static int sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
209 static int sub_SubRipRead ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
210 static int sub_SSARead ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
211 static int sub_Vplayer ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
212 static int sub_Sami ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
213 static int sub_VobSubIDX ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
215 static int DemuxVobSub ( subtitle_demux_t *, block_t * );
222 int (*pf_read_subtitle) ( subtitle_demux_t *, text_t *, subtitle_t*, mtime_t );
223 } sub_read_subtitle_function [] =
225 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", sub_MicroDvdRead },
226 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", sub_SubRipRead },
227 { "ssa1", SUB_TYPE_SSA1, "SSA-1", sub_SSARead },
228 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4",sub_SSARead },
229 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", sub_Vplayer },
230 { "sami", SUB_TYPE_SAMI, "SAMI", sub_Sami },
231 { "vobsub", SUB_TYPE_VOBSUB, "VobSub", sub_VobSubIDX },
232 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
235 static char * local_stristr( char *psz_big, char *psz_little)
237 char *p_pos = psz_big;
239 if (!psz_big || !psz_little || !*psz_little) return psz_big;
243 if (toupper(*p_pos) == toupper(*psz_little))
245 char * psz_cur1 = p_pos + 1;
246 char * psz_cur2 = psz_little + 1;
247 while (*psz_cur1 && *psz_cur2 && toupper(*psz_cur1) == toupper(*psz_cur2))
252 if (!*psz_cur2) return p_pos;
259 /*****************************************************************************
260 * sub_open: Open a subtitle file and add subtitle ES
261 *****************************************************************************/
262 static int sub_open( subtitle_demux_t *p_sub, input_thread_t *p_input,
263 char *psz_name, mtime_t i_microsecperframe )
268 int i, i_sub_type, i_max;
269 int (*pf_read_subtitle)( subtitle_demux_t *, text_t *, subtitle_t *,
272 p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
274 p_sub->i_subtitles = 0;
275 p_sub->subtitle = NULL;
276 p_sub->p_vobsub_file = 0;
277 p_sub->i_original_mspf = i_microsecperframe;
278 p_sub->p_input = p_input;
280 if( !psz_name || !*psz_name )
282 msg_Err( p_sub, "no subtitle file specified" );
286 /* *** load the file *** */
287 if( text_load( &txt, psz_name ) )
289 msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
293 msg_Dbg( p_sub, "opened `%s'", psz_name );
295 var_Get( p_sub, "sub-fps", &val );
296 if( val.i_int >= 1.0 )
298 i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
300 else if( val.f_float == 0 )
303 i_microsecperframe = 0;
305 else if( val.f_float <= 0 )
307 /* invalid value, default = 25fps */
308 i_microsecperframe = 40000;
311 var_Get( p_sub, "sub-type", &val);
312 if( val.psz_string && *val.psz_string )
318 if( sub_read_subtitle_function[i].psz_type_name == NULL )
320 i_sub_type = SUB_TYPE_UNKNOWN;
323 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
326 i_sub_type = sub_read_subtitle_function[i].i_type;
333 i_sub_type = SUB_TYPE_UNKNOWN;
335 FREE( val.psz_string );
337 /* *** Now try to autodetect subtitle format *** */
338 if( i_sub_type == SUB_TYPE_UNKNOWN )
343 msg_Dbg( p_input, "trying to autodetect file format" );
344 for( i_try = 0; i_try < MAX_TRY; i_try++ )
348 if( ( s = text_get_line( &txt ) ) == NULL )
353 if( local_stristr( s, "<SAMI>" ) )
355 i_sub_type = SUB_TYPE_SAMI;
358 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
359 sscanf( s, "{%d}{}", &i_dummy ) == 1)
361 i_sub_type = SUB_TYPE_MICRODVD;
365 "%d:%d:%d,%d --> %d:%d:%d,%d",
366 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
367 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
369 i_sub_type = SUB_TYPE_SUBRIP;
373 "!: This is a Sub Station Alpha v%d.x script.",
378 i_sub_type = SUB_TYPE_SSA1;
382 i_sub_type = SUB_TYPE_SSA2_4; /* I hope this will work */
386 else if( local_stristr( s, "This is a Sub Station Alpha v4 script" ) )
388 i_sub_type = SUB_TYPE_SSA2_4; /* I hope this will work */
391 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
393 i_sub_type = SUB_TYPE_SSA2_4; /* could be wrong */
396 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
397 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
399 i_sub_type = SUB_TYPE_VPLAYER;
402 else if( local_stristr( s, "# VobSub index file" ) )
404 i_sub_type = SUB_TYPE_VOBSUB;
412 /* *** Load this file in memory *** */
415 if( sub_read_subtitle_function[i].i_type == SUB_TYPE_UNKNOWN )
417 msg_Dbg( p_input, "unknown subtitle file" );
422 if( sub_read_subtitle_function[i].i_type == i_sub_type )
424 msg_Dbg( p_input, "detected %s format",
425 sub_read_subtitle_function[i].psz_name );
426 p_sub->i_sub_type = i_sub_type;
427 pf_read_subtitle = sub_read_subtitle_function[i].pf_read_subtitle;
434 if( p_sub->i_subtitles >= i_max )
437 if( !( p_sub->subtitle = realloc( p_sub->subtitle,
438 sizeof(subtitle_t) * i_max ) ) )
440 msg_Err( p_sub, "out of memory");
444 if( pf_read_subtitle( p_sub, &txt,
445 p_sub->subtitle + p_sub->i_subtitles,
446 i_microsecperframe ) < 0 )
450 p_sub->i_subtitles++;
452 msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles );
454 /* *** Close the file *** */
457 /* *** fix subtitle (order and time) *** */
458 p_sub->i_subtitle = 0; /* will be modified by sub_fix */
460 if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
465 /* *** add subtitle ES *** */
466 if( p_sub->i_sub_type == SUB_TYPE_VOBSUB )
468 int i_len = strlen( psz_name );
469 char *psz_vobname = strdup(psz_name);
471 strcpy( psz_vobname + i_len - 4, ".sub" );
474 if( !( p_sub->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
476 msg_Err( p_sub, "couldn't open .sub Vobsub file: %s", psz_vobname );
480 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
482 else if( p_sub->i_sub_type == SUB_TYPE_SSA1 ||
483 p_sub->i_sub_type == SUB_TYPE_SSA2_4 )
485 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
489 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
491 if( p_sub->psz_header != NULL )
493 fmt.i_extra = strlen( p_sub->psz_header ) + 1;
494 fmt.p_extra = strdup( p_sub->psz_header );
496 p_sub->p_es = es_out_Add( p_input->p_es_out, &fmt );
497 p_sub->i_previously_selected = 0;
502 /*****************************************************************************
503 * sub_demux: Send subtitle to decoder until i_maxdate
504 *****************************************************************************/
505 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
507 input_thread_t *p_input = p_sub->p_input;
512 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_sub->p_es, &b );
513 if( b && !p_sub->i_previously_selected )
515 p_sub->i_previously_selected = 1;
516 p_sub->pf_seek( p_sub, i_maxdate );
519 else if( !b && p_sub->i_previously_selected )
521 p_sub->i_previously_selected = 0;
525 if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
527 var_Get( p_sub, "sub-delay", &val );
528 i_delay = (mtime_t) val.i_int * 100000;
529 while( p_sub->i_subtitle < p_sub->i_subtitles &&
530 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate - i_delay )
533 int i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
542 if( ( p_block = block_New( p_sub->p_input, i_len ) ) == NULL )
548 /* XXX we should convert all demuxers to use es_out_Control to set * pcr and then remove that */
551 p_sub->subtitle[p_sub->i_subtitle].i_start += i_delay;
552 p_sub->subtitle[p_sub->i_subtitle].i_stop += i_delay;
556 input_ClockGetTS( p_sub->p_input,
557 p_sub->p_input->stream.p_selected_program,
558 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
560 if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
562 /* FIXME kludge i_dts means end of display... */
564 input_ClockGetTS( p_sub->p_input,
565 p_sub->p_input->stream.p_selected_program,
566 p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
569 memcpy( p_block->p_buffer, p_sub->subtitle[p_sub->i_subtitle].psz_text, i_len );
570 if( p_block->i_pts > 0 )
572 es_out_Send( p_input->p_es_out, p_sub->p_es, p_block );
576 block_Release( p_block );
583 while( p_sub->i_subtitle < p_sub->i_subtitles &&
584 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
586 int i_pos = p_sub->subtitle[p_sub->i_subtitle].i_vobsub_location;
590 /* first compute SPU size */
591 if( p_sub->i_subtitle + 1 < p_sub->i_subtitles )
593 i_size = p_sub->subtitle[p_sub->i_subtitle+1].i_vobsub_location - i_pos;
595 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
597 /* Seek at the right place (could be avoid if sub_seek is fixed to do his job) */
598 if( fseek( p_sub->p_vobsub_file, i_pos, SEEK_SET ) )
600 msg_Warn( p_sub, "cannot seek at right vobsub location %d", i_pos );
605 /* allocate a packet */
606 if( ( p_block = block_New( p_sub, i_size ) ) == NULL )
613 p_block->i_buffer = fread( p_block->p_buffer, 1, i_size, p_sub->p_vobsub_file );
614 if( p_block->i_buffer <= 6 )
616 block_Release( p_block );
623 input_ClockGetTS( p_sub->p_input,
624 p_sub->p_input->stream.p_selected_program,
625 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
627 /* demux this block */
628 DemuxVobSub( p_sub, p_block );
636 /*****************************************************************************
637 * sub_seek: Seek to i_date
638 *****************************************************************************/
639 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
641 /* should be fast enough... */
642 p_sub->i_subtitle = 0;
643 while( p_sub->i_subtitle < p_sub->i_subtitles &&
644 p_sub->subtitle[p_sub->i_subtitle].i_start < i_date )
651 /*****************************************************************************
652 * sub_close: Close subtitle demux
653 *****************************************************************************/
654 static void sub_close( subtitle_demux_t *p_sub )
656 if( p_sub->subtitle )
659 for( i = 0; i < p_sub->i_subtitles; i++ )
661 if( p_sub->subtitle[i].psz_text )
663 free( p_sub->subtitle[i].psz_text );
666 free( p_sub->subtitle );
668 if( p_sub->p_vobsub_file )
670 fclose( p_sub->p_vobsub_file );
674 /*****************************************************************************
675 * sub_fix: fix time stamp and order of subtitle
676 *****************************************************************************/
677 static void sub_fix( subtitle_demux_t *p_sub )
685 /* *** fix order (to be sure...) *** */
686 /* We suppose that there are near in order and this durty bubble sort
687 * wont take too much time
692 for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
694 if( p_sub->subtitle[i_index].i_start <
695 p_sub->subtitle[i_index - 1].i_start )
699 p_sub->subtitle + i_index - 1,
700 sizeof( subtitle_t ) );
701 memcpy( p_sub->subtitle + i_index - 1,
702 p_sub->subtitle + i_index,
703 sizeof( subtitle_t ) );
704 memcpy( p_sub->subtitle + i_index,
706 sizeof( subtitle_t ) );
712 /* *** and at the end add delay *** */
713 var_Get( p_sub, "sub-delay", &val );
714 i_delay = (mtime_t) val.i_int * 100000;
717 for( i = 0; i < p_sub->i_subtitles; i++ )
719 p_sub->subtitle[i].i_start += i_delay;
720 p_sub->subtitle[i].i_stop += i_delay;
721 if( p_sub->subtitle[i].i_start < 0 )
723 p_sub->i_subtitle = i + 1;
731 /*****************************************************************************
732 * Specific Subtitle function
733 *****************************************************************************/
734 static int sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
738 * {n1}{n2}Line1|Line2|Line3....
739 * where n1 and n2 are the video frame number...
744 char buffer_text[MAX_LINE + 1];
745 unsigned int i_start;
749 p_subtitle->i_start = 0;
750 p_subtitle->i_stop = 0;
751 p_subtitle->i_vobsub_location = 0;
752 p_subtitle->psz_text = NULL;
756 if( ( s = text_get_line( txt ) ) == NULL )
758 return( VLC_EGENERIC );
763 memset( buffer_text, '\0', MAX_LINE );
764 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
765 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
770 /* replace | by \n */
771 for( i = 0; i < strlen( buffer_text ); i++ )
773 if( buffer_text[i] == '|' )
775 buffer_text[i] = '\n';
778 if( i_microsecperframe == 0)
780 i_microsecperframe = 40000;
782 p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
783 p_subtitle->i_stop = (mtime_t)i_stop * (mtime_t)i_microsecperframe;
784 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
788 static int sub_SubRipRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
792 * h1:m1:s1,d1 --> h2:m2:s2,d2
800 char buffer_text[ 10 * MAX_LINE];
805 p_subtitle->i_start = 0;
806 p_subtitle->i_stop = 0;
807 p_subtitle->i_vobsub_location = 0;
808 p_subtitle->psz_text = NULL;
812 int h1, m1, s1, d1, h2, m2, s2, d2;
813 if( ( s = text_get_line( txt ) ) == NULL )
815 return( VLC_EGENERIC );
818 "%d:%d:%d,%d --> %d:%d:%d,%d",
820 &h2, &m2, &s2, &d2 ) == 8 )
822 i_start = ( (mtime_t)h1 * 3600*1000 +
823 (mtime_t)m1 * 60*1000 +
825 (mtime_t)d1 ) * 1000;
827 i_stop = ( (mtime_t)h2 * 3600*1000 +
828 (mtime_t)m2 * 60*1000 +
830 (mtime_t)d2 ) * 1000;
832 /* Now read text until an empty line */
833 for( i_buffer_text = 0;; )
836 if( ( s = text_get_line( txt ) ) == NULL )
838 return( VLC_EGENERIC );
844 /* empty line -> end of this subtitle */
845 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
846 p_subtitle->i_start = i_start;
847 p_subtitle->i_stop = i_stop;
848 p_subtitle->psz_text = strdup( buffer_text );
849 /* If framerate is available, use sub-fps */
850 if( i_microsecperframe != 0 && p_sub->i_original_mspf != 0)
852 p_subtitle->i_start = (mtime_t)i_start *
853 (mtime_t)p_sub->i_original_mspf /
854 (mtime_t)i_microsecperframe;
855 p_subtitle->i_stop = (mtime_t)i_stop *
856 (mtime_t)p_sub->i_original_mspf /
857 (mtime_t)i_microsecperframe;
863 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
865 memcpy( buffer_text + i_buffer_text,
868 i_buffer_text += i_len;
870 buffer_text[i_buffer_text] = '\n';
880 static int sub_SSARead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
882 char buffer_text[ 10 * MAX_LINE];
887 p_subtitle->i_start = 0;
888 p_subtitle->i_stop = 0;
889 p_subtitle->i_vobsub_location = 0;
890 p_subtitle->psz_text = NULL;
894 int h1, m1, s1, c1, h2, m2, s2, c2;
897 if( ( s = text_get_line( txt ) ) == NULL )
899 return( VLC_EGENERIC );
901 p_subtitle->psz_text = malloc( strlen( s ) );
904 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
908 buffer_text ) == 10 )
910 i_start = ( (mtime_t)h1 * 3600*1000 +
911 (mtime_t)m1 * 60*1000 +
913 (mtime_t)c1 * 10 ) * 1000;
915 i_stop = ( (mtime_t)h2 * 3600*1000 +
916 (mtime_t)m2 * 60*1000 +
918 (mtime_t)c2 * 10 ) * 1000;
920 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
921 if( p_sub->i_sub_type == SUB_TYPE_SSA1 )
923 sprintf( p_subtitle->psz_text, ",%d%s", i_dummy, strdup( buffer_text) );
927 sprintf( p_subtitle->psz_text, ",%d,%s", i_dummy, strdup( buffer_text) );
929 p_subtitle->i_start = i_start;
930 p_subtitle->i_stop = i_stop;
935 /* All the other stuff we add to the header field */
936 if( p_sub->psz_header != NULL )
938 if( !( p_sub->psz_header = realloc( p_sub->psz_header,
939 strlen( p_sub->psz_header ) + strlen( s ) + 2 ) ) )
941 msg_Err( p_sub, "out of memory");
944 p_sub->psz_header = strcat( p_sub->psz_header, strdup( s ) );
945 p_sub->psz_header = strcat( p_sub->psz_header, "\n" );
949 if( !( p_sub->psz_header = malloc( strlen( s ) + 2 ) ) )
951 msg_Err( p_sub, "out of memory");
954 p_sub->psz_header = strdup( s );
955 p_sub->psz_header = strcat( p_sub->psz_header, "\n" );
961 static int sub_Vplayer( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
965 * h:m:s:Line1|Line2|Line3....
967 * h:m:s Line1|Line2|Line3....
968 * where n1 and n2 are the video frame number...
972 char buffer_text[MAX_LINE + 1];
976 p_subtitle->i_start = 0;
977 p_subtitle->i_stop = 0;
978 p_subtitle->i_vobsub_location = 0;
979 p_subtitle->psz_text = NULL;
986 if( ( p = text_get_line( txt ) ) == NULL )
988 return( VLC_EGENERIC );
993 memset( buffer_text, '\0', MAX_LINE );
994 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
996 i_start = ( (mtime_t)h * 3600*1000 +
997 (mtime_t)m * 60*1000 +
998 (mtime_t)s * 1000 ) * 1000;
1003 /* replace | by \n */
1004 for( i = 0; i < strlen( buffer_text ); i++ )
1006 if( buffer_text[i] == '|' )
1008 buffer_text[i] = '\n';
1011 p_subtitle->i_start = i_start;
1013 p_subtitle->i_stop = 0;
1014 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1018 static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
1022 if( local_stristr( psz_start, psz_str ) )
1024 char *s = local_stristr( psz_start, psz_str );
1026 s += strlen( psz_str );
1034 if( ( p = text_get_line( txt ) ) == NULL )
1038 if( local_stristr( p, psz_str ) )
1040 char *s = local_stristr( p, psz_str );
1042 s += strlen( psz_str );
1049 static int sub_Sami( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
1055 char buffer_text[10*MAX_LINE + 1];
1057 p_subtitle->i_start = 0;
1058 p_subtitle->i_stop = 0;
1059 p_subtitle->i_vobsub_location = 0;
1060 p_subtitle->psz_text = NULL;
1063 if( i_text < 10*MAX_LINE ) \
1065 buffer_text[i_text++] = c; \
1066 buffer_text[i_text] = '\0'; \
1069 /* search "Start=" */
1070 if( !( p = sub_SamiSearch( txt, NULL, "Start=" ) ) )
1072 return VLC_EGENERIC;
1075 /* get start value */
1076 i_start = strtol( p, &p, 0 );
1079 if( !( p = sub_SamiSearch( txt, p, "<P" ) ) )
1081 return VLC_EGENERIC;
1084 if( !( p = sub_SamiSearch( txt, p, ">" ) ) )
1086 return VLC_EGENERIC;
1090 buffer_text[0] = '\0';
1091 /* now get all txt until a "Start=" line */
1098 if( !strncasecmp( p, "<br", 3 ) )
1102 else if( local_stristr( p, "Start=" ) )
1104 text_previous_line( txt );
1107 p = sub_SamiSearch( txt, p, ">" );
1109 else if( !strncmp( p, " ", 6 ) )
1114 else if( *p == '\t' )
1127 p = text_get_line( txt );
1136 p_subtitle->i_start = i_start * 1000;
1137 p_subtitle->i_stop = 0;
1138 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1140 return( VLC_SUCCESS );
1144 static int sub_VobSubIDX( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
1147 * Parse the idx file. Each line:
1148 * timestamp: hh:mm:ss:mss, filepos: loc
1149 * hexint is the hex location of the vobsub in the .sub file
1153 char buffer_text[MAX_LINE + 1];
1154 unsigned int i_start, i_location;
1156 p_subtitle->i_start = 0;
1157 p_subtitle->i_stop = 0;
1158 p_subtitle->i_vobsub_location = 0;
1159 p_subtitle->psz_text = NULL;
1163 unsigned int h, m, s, ms, loc;
1165 if( ( p = text_get_line( txt ) ) == NULL )
1167 return( VLC_EGENERIC );
1171 memset( buffer_text, '\0', MAX_LINE );
1172 if( sscanf( p, "timestamp: %d:%d:%d:%d, filepos: %x%[^\r\n]",
1173 &h, &m, &s, &ms, &loc, buffer_text ) == 5 )
1175 i_start = ( (mtime_t)h * 3600*1000 +
1176 (mtime_t)m * 60*1000 +
1178 (mtime_t)ms ) * 1000;
1183 p_subtitle->i_start = (mtime_t)i_start;
1184 p_subtitle->i_stop = 0;
1185 p_subtitle->psz_text = NULL;
1186 p_subtitle->i_vobsub_location = i_location;
1187 fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
1191 static int DemuxVobSub( subtitle_demux_t *p_demux, block_t *p_bk )
1193 uint8_t *p = p_bk->p_buffer;
1194 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
1198 int i_size = ps_pkt_size( p, p_end - p );
1207 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
1209 msg_Warn( p_demux, "invalid PES" );
1215 msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
1220 /* Create a block */
1221 p_pkt = block_New( p_demux, i_size );
1222 memcpy( p_pkt->p_buffer, p, i_size);
1225 i_id = ps_pkt_id( p_pkt );
1226 if( (i_id&0xffe0) != 0xbd20 ||
1227 ps_pkt_parse_pes( p_pkt, 1 ) )
1229 block_Release( p_pkt );
1233 msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
1235 /* FIXME i_spu == determines which of the spu tracks we will show. */
1236 if( p_demux->p_es && i_spu == 0 )
1238 p_pkt->i_pts = p_bk->i_pts;
1240 es_out_Send( p_demux->p_input->p_es_out, p_demux->p_es, p_pkt );
1242 p_bk->i_pts = 0; /* only first packet has a pts */
1246 block_Release( p_pkt );