1 /*****************************************************************************
2 * subtitle.c: Demux for text and vobsub 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 #include <sys/types.h>
35 #include <vlc/input.h>
36 #include "vlc_video.h"
40 #if (!defined( WIN32 ) || defined(__MINGW32__))
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t *p_this );
48 static void Close( vlc_object_t *p_this );
50 #define SUB_DELAY_LONGTEXT \
51 "Delay subtitles (in 1/10s)"
52 #define SUB_FPS_LONGTEXT \
53 "Override frames per second. " \
54 "It will only work with MicroDVD subtitles."
55 #define SUB_TYPE_LONGTEXT \
56 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
57 "\"sami\" (auto for autodetection, it should always work)."
58 static char *ppsz_sub_type[] =
60 "auto", "microdvd", "subrip", "subviewer", "ssa1",
61 "ssa2-4", "vplayer", "sami", "vobsub"
65 set_description( _("Text subtitles demux") );
66 set_capability( "demux2", 0 );
67 add_float( "sub-fps", 0.0, NULL,
68 N_("Frames per second"),
69 SUB_FPS_LONGTEXT, VLC_TRUE );
70 add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
71 SUB_TYPE_LONGTEXT, VLC_TRUE );
72 change_string_list( ppsz_sub_type, 0, 0 );
73 set_callbacks( Open, Close );
75 add_shortcut( "subtitle" );
76 add_shortcut( "vobsub" );
79 /*****************************************************************************
81 *****************************************************************************/
84 SUB_TYPE_UNKNOWN = -1,
101 static int TextLoad( text_t *, stream_t *s );
102 static void TextUnload( text_t * );
110 int i_vobsub_location;
120 int64_t i_next_demux_date;
122 int64_t i_microsecperframe;
127 subtitle_t *subtitle;
129 mtime_t i_original_mspf;
134 static int ParseMicroDvd ( demux_t *, subtitle_t * );
135 static int ParseSubRip ( demux_t *, subtitle_t * );
136 static int ParseSubViewer( demux_t *, subtitle_t * );
137 static int ParseSSA ( demux_t *, subtitle_t * );
138 static int ParseVplayer ( demux_t *, subtitle_t * );
139 static int ParseSami ( demux_t *, subtitle_t * );
140 static int ParseVobSubIDX( demux_t *, subtitle_t * );
142 static int DemuxVobSub( demux_t *, block_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 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
158 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
159 { "vobsub", SUB_TYPE_VOBSUB, "VobSub", ParseVobSubIDX },
160 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
163 static int Demux( demux_t * );
164 static int Control( demux_t *, int, va_list );
166 static void Fix( demux_t * );
168 #define DVD_VIDEO_LB_LEN 2048
170 /*****************************************************************************
172 *****************************************************************************/
173 static int Open ( vlc_object_t *p_this )
175 demux_t *p_demux = (demux_t*)p_this;
180 int (*pf_read)( demux_t *, subtitle_t* );
183 if( strcmp( p_demux->psz_demux, "subtitle" ) &&
184 strcmp( p_demux->psz_demux, "vobsub" ) )
186 msg_Dbg( p_demux, "subtitle demux discarded" );
190 p_demux->pf_demux = Demux;
191 p_demux->pf_control = Control;
192 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
193 p_sys->psz_header = NULL;
194 p_sys->i_subtitle = 0;
195 p_sys->i_subtitles= 0;
196 p_sys->subtitle = NULL;
197 p_sys->p_vobsub_file = NULL;
198 p_sys->i_original_mspf = 0;
202 p_sys->i_microsecperframe = 40000; /* default to 25 fps */
203 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
206 p_sys->i_microsecperframe = (mtime_t)( (float)1000000 / f_fps );
209 /* Get or probe the type */
210 p_sys->i_type = SUB_TYPE_UNKNOWN;
211 psz_type = var_CreateGetString( p_demux, "sub-type" );
218 if( sub_read_subtitle_function[i].psz_type_name == NULL )
221 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
224 p_sys->i_type = sub_read_subtitle_function[i].i_type;
231 /* Probe if unknown type */
232 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
237 msg_Dbg( p_demux, "autodetecting subtitle format" );
238 for( i_try = 0; i_try < 256; i_try++ )
242 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
245 if( strcasestr( s, "<SAMI>" ) )
247 p_sys->i_type = SUB_TYPE_SAMI;
250 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
251 sscanf( s, "{%d}{}", &i_dummy ) == 1)
253 p_sys->i_type = SUB_TYPE_MICRODVD;
257 "%d:%d:%d,%d --> %d:%d:%d,%d",
258 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
259 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
261 p_sys->i_type = SUB_TYPE_SUBRIP;
265 "!: This is a Sub Station Alpha v%d.x script.",
270 p_sys->i_type = SUB_TYPE_SSA1;
274 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
278 else if( strcasestr( s, "This is a Sub Station Alpha v4 script" ) )
280 p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */
283 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
285 p_sys->i_type = SUB_TYPE_SSA2_4; /* could be wrong */
288 else if( strcasestr( s, "[INFORMATION]" ) )
290 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
293 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
294 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
296 p_sys->i_type = SUB_TYPE_VPLAYER;
299 else if( strcasestr( s, "# VobSub index file" ) )
301 p_sys->i_type = SUB_TYPE_VOBSUB;
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;
366 if( p_sys->i_type != SUB_TYPE_VOBSUB )
371 if( p_sys->i_subtitles > 0 )
373 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
375 if( p_sys->i_length <= 0 )
376 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
379 /* *** add subtitle ES *** */
380 if( p_sys->i_type == SUB_TYPE_VOBSUB )
382 int i_len = strlen( p_demux->psz_path );
383 char *psz_vobname = strdup( p_demux->psz_path );
385 strcpy( psz_vobname + i_len - 4, ".sub" );
388 if( !( p_sys->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
390 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
395 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
397 else if( p_sys->i_type == SUB_TYPE_SSA1 ||
398 p_sys->i_type == SUB_TYPE_SSA2_4 )
400 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
404 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
406 if( p_sys->psz_header != NULL )
408 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
409 fmt.p_extra = strdup( p_sys->psz_header );
411 p_sys->es = es_out_Add( p_demux->out, &fmt );
416 /*****************************************************************************
417 * Close: Close subtitle demux
418 *****************************************************************************/
419 static void Close( vlc_object_t *p_this )
421 demux_t *p_demux = (demux_t*)p_this;
422 demux_sys_t *p_sys = p_demux->p_sys;
425 for( i = 0; i < p_sys->i_subtitles; i++ )
427 if( p_sys->subtitle[i].psz_text )
428 free( p_sys->subtitle[i].psz_text );
430 if( p_sys->subtitle )
431 free( p_sys->subtitle );
433 if( p_sys->p_vobsub_file )
434 fclose( p_sys->p_vobsub_file );
439 /*****************************************************************************
441 *****************************************************************************/
442 static int Control( demux_t *p_demux, int i_query, va_list args )
444 demux_sys_t *p_sys = p_demux->p_sys;
450 case DEMUX_GET_LENGTH:
451 pi64 = (int64_t*)va_arg( args, int64_t * );
452 *pi64 = p_sys->i_length;
456 pi64 = (int64_t*)va_arg( args, int64_t * );
457 if( p_sys->i_subtitle < p_sys->i_subtitles )
459 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
465 i64 = (int64_t)va_arg( args, int64_t );
466 p_sys->i_subtitle = 0;
467 while( p_sys->i_subtitle < p_sys->i_subtitles &&
468 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
473 if( p_sys->i_subtitle >= p_sys->i_subtitles )
477 case DEMUX_GET_POSITION:
478 pf = (double*)va_arg( args, double * );
479 if( p_sys->i_subtitle >= p_sys->i_subtitles )
483 else if( p_sys->i_subtitles > 0 )
485 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
486 (double)p_sys->i_length;
494 case DEMUX_SET_POSITION:
495 f = (double)va_arg( args, double );
496 i64 = f * p_sys->i_length;
498 p_sys->i_subtitle = 0;
499 while( p_sys->i_subtitle < p_sys->i_subtitles &&
500 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
504 if( p_sys->i_subtitle >= p_sys->i_subtitles )
508 case DEMUX_SET_NEXT_DEMUX_TIME:
509 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
514 case DEMUX_GET_TITLE_INFO:
518 msg_Err( p_demux, "unknown query in subtitle control" );
523 /*****************************************************************************
524 * Demux: Send subtitle to decoder
525 *****************************************************************************/
526 static int Demux( demux_t *p_demux )
528 demux_sys_t *p_sys = p_demux->p_sys;
531 if( p_sys->i_subtitle >= p_sys->i_subtitles )
534 i_maxdate = p_sys->i_next_demux_date;
535 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
537 /* Should not happen */
538 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
541 if( p_sys->i_type != SUB_TYPE_VOBSUB )
543 while( p_sys->i_subtitle < p_sys->i_subtitles &&
544 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
547 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
556 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
562 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
568 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
569 p_block->i_dts = p_block->i_pts;
570 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
573 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
576 memcpy( p_block->p_buffer,
577 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
578 if( p_block->i_pts > 0 )
580 es_out_Send( p_demux->out, p_sys->es, p_block );
584 block_Release( p_block );
591 while( p_sys->i_subtitle < p_sys->i_subtitles &&
592 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
594 int i_pos = p_sys->subtitle[p_sys->i_subtitle].i_vobsub_location;
598 /* first compute SPU size */
599 if( p_sys->i_subtitle + 1 < p_sys->i_subtitles )
601 i_size = p_sys->subtitle[p_sys->i_subtitle+1].i_vobsub_location - i_pos;
603 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
605 /* Seek at the right place */
606 if( fseek( p_sys->p_vobsub_file, i_pos, SEEK_SET ) )
609 "cannot seek at right vobsub location %d", i_pos );
614 /* allocate a packet */
615 if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
622 p_block->i_buffer = fread( p_block->p_buffer, 1, i_size,
623 p_sys->p_vobsub_file );
624 if( p_block->i_buffer <= 6 )
626 block_Release( p_block );
632 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
634 /* demux this block */
635 DemuxVobSub( p_demux, p_block );
642 p_sys->i_next_demux_date = 0;
647 /*****************************************************************************
648 * Fix: fix time stamp and order of subtitle
649 *****************************************************************************/
650 static void Fix( demux_t *p_demux )
652 demux_sys_t *p_sys = p_demux->p_sys;
656 /* *** fix order (to be sure...) *** */
657 /* We suppose that there are near in order and this durty bubble sort
658 * wont take too much time
663 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
665 if( p_sys->subtitle[i_index].i_start <
666 p_sys->subtitle[i_index - 1].i_start )
670 p_sys->subtitle + i_index - 1,
671 sizeof( subtitle_t ) );
672 memcpy( p_sys->subtitle + i_index - 1,
673 p_sys->subtitle + i_index,
674 sizeof( subtitle_t ) );
675 memcpy( p_sys->subtitle + i_index,
677 sizeof( subtitle_t ) );
684 static int TextLoad( text_t *txt, stream_t *s )
690 txt->i_line_count = 0;
692 txt->line = calloc( i_line_max, sizeof( char * ) );
694 /* load the complete file */
697 char *psz = stream_ReadLine( s );
702 txt->line[txt->i_line_count++] = psz;
703 if( txt->i_line_count >= i_line_max )
706 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
710 if( txt->i_line_count <= 0 )
718 static void TextUnload( text_t *txt )
722 for( i = 0; i < txt->i_line_count; i++ )
724 free( txt->line[i] );
728 txt->i_line_count = 0;
731 static char *TextGetLine( text_t *txt )
733 if( txt->i_line >= txt->i_line_count )
736 return txt->line[txt->i_line++];
738 static void TextPreviousLine( text_t *txt )
740 if( txt->i_line > 0 )
744 /*****************************************************************************
745 * Specific Subtitle function
746 *****************************************************************************/
747 #define MAX_LINE 8192
748 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
750 demux_sys_t *p_sys = p_demux->p_sys;
751 text_t *txt = &p_sys->txt;
754 * {n1}{n2}Line1|Line2|Line3....
755 * where n1 and n2 are the video frame number...
760 char buffer_text[MAX_LINE + 1];
761 unsigned int i_start;
765 p_subtitle->i_start = 0;
766 p_subtitle->i_stop = 0;
767 p_subtitle->i_vobsub_location = 0;
768 p_subtitle->psz_text = NULL;
772 if( ( s = TextGetLine( txt ) ) == NULL )
774 return( VLC_EGENERIC );
779 memset( buffer_text, '\0', MAX_LINE );
780 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
781 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
786 /* replace | by \n */
787 for( i = 0; i < strlen( buffer_text ); i++ )
789 if( buffer_text[i] == '|' )
791 buffer_text[i] = '\n';
795 p_subtitle->i_start = (mtime_t)i_start * p_sys->i_microsecperframe;
796 p_subtitle->i_stop = (mtime_t)i_stop * p_sys->i_microsecperframe;
797 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
801 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
803 demux_sys_t *p_sys = p_demux->p_sys;
804 text_t *txt = &p_sys->txt;
808 * h1:m1:s1,d1 --> h2:m2:s2,d2
816 char buffer_text[ 10 * MAX_LINE];
821 p_subtitle->i_start = 0;
822 p_subtitle->i_stop = 0;
823 p_subtitle->i_vobsub_location = 0;
824 p_subtitle->psz_text = NULL;
828 int h1, m1, s1, d1, h2, m2, s2, d2;
829 if( ( s = TextGetLine( txt ) ) == NULL )
831 return( VLC_EGENERIC );
834 "%d:%d:%d,%d --> %d:%d:%d,%d",
836 &h2, &m2, &s2, &d2 ) == 8 )
838 i_start = ( (mtime_t)h1 * 3600*1000 +
839 (mtime_t)m1 * 60*1000 +
841 (mtime_t)d1 ) * 1000;
843 i_stop = ( (mtime_t)h2 * 3600*1000 +
844 (mtime_t)m2 * 60*1000 +
846 (mtime_t)d2 ) * 1000;
848 /* Now read text until an empty line */
849 for( i_buffer_text = 0;; )
852 if( ( s = TextGetLine( txt ) ) == NULL )
854 return( VLC_EGENERIC );
860 /* empty line -> end of this subtitle */
861 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
862 p_subtitle->i_start = i_start;
863 p_subtitle->i_stop = i_stop;
864 p_subtitle->psz_text = strdup( buffer_text );
865 /* If framerate is available, use sub-fps */
866 if( p_sys->i_microsecperframe != 0 &&
867 p_sys->i_original_mspf != 0)
869 p_subtitle->i_start = (mtime_t)i_start *
870 p_sys->i_original_mspf /
871 p_sys->i_microsecperframe;
872 p_subtitle->i_stop = (mtime_t)i_stop *
873 p_sys->i_original_mspf /
874 p_sys->i_microsecperframe;
880 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
882 memcpy( buffer_text + i_buffer_text,
885 i_buffer_text += i_len;
887 buffer_text[i_buffer_text] = '\n';
896 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
898 demux_sys_t *p_sys = p_demux->p_sys;
899 text_t *txt = &p_sys->txt;
902 * h1:m1:s1.d1,h2:m2:s2.d2
907 * ( works with subviewer and subviewer v2 )
910 char buffer_text[ 10 * MAX_LINE];
915 p_subtitle->i_start = 0;
916 p_subtitle->i_stop = 0;
917 p_subtitle->i_vobsub_location = 0;
918 p_subtitle->psz_text = NULL;
922 int h1, m1, s1, d1, h2, m2, s2, d2;
923 if( ( s = TextGetLine( txt ) ) == NULL )
925 return( VLC_EGENERIC );
928 "%d:%d:%d.%d,%d:%d:%d.%d",
930 &h2, &m2, &s2, &d2 ) == 8 )
932 i_start = ( (mtime_t)h1 * 3600*1000 +
933 (mtime_t)m1 * 60*1000 +
935 (mtime_t)d1 ) * 1000;
937 i_stop = ( (mtime_t)h2 * 3600*1000 +
938 (mtime_t)m2 * 60*1000 +
940 (mtime_t)d2 ) * 1000;
942 /* Now read text until an empty line */
943 for( i_buffer_text = 0;; )
946 if( ( s = TextGetLine( txt ) ) == NULL )
948 return( VLC_EGENERIC );
954 /* empty line -> end of this subtitle */
955 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
956 p_subtitle->i_start = i_start;
957 p_subtitle->i_stop = i_stop;
959 /* replace [br] by \n */
960 for( i = 0; i < strlen( buffer_text ) - 3; i++ )
962 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
963 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
965 char *temp = buffer_text + i + 1;
966 buffer_text[i] = '\n';
967 memmove( temp, temp+3, strlen( temp-3 ));
970 p_subtitle->psz_text = strdup( buffer_text );
975 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
977 memcpy( buffer_text + i_buffer_text,
980 i_buffer_text += i_len;
982 buffer_text[i_buffer_text] = '\n';
992 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
994 demux_sys_t *p_sys = p_demux->p_sys;
995 text_t *txt = &p_sys->txt;
997 char buffer_text[ 10 * MAX_LINE];
1002 p_subtitle->i_start = 0;
1003 p_subtitle->i_stop = 0;
1004 p_subtitle->i_vobsub_location = 0;
1005 p_subtitle->psz_text = NULL;
1009 int h1, m1, s1, c1, h2, m2, s2, c2;
1012 if( ( s = TextGetLine( txt ) ) == NULL )
1014 return( VLC_EGENERIC );
1016 p_subtitle->psz_text = malloc( strlen( s ) );
1019 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
1023 buffer_text ) == 10 )
1025 i_start = ( (mtime_t)h1 * 3600*1000 +
1026 (mtime_t)m1 * 60*1000 +
1027 (mtime_t)s1 * 1000 +
1028 (mtime_t)c1 * 10 ) * 1000;
1030 i_stop = ( (mtime_t)h2 * 3600*1000 +
1031 (mtime_t)m2 * 60*1000 +
1032 (mtime_t)s2 * 1000 +
1033 (mtime_t)c2 * 10 ) * 1000;
1035 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1036 if( p_sys->i_type == SUB_TYPE_SSA1 )
1038 sprintf( p_subtitle->psz_text,
1039 ",%d%s", i_dummy, strdup( buffer_text) );
1043 sprintf( p_subtitle->psz_text,
1044 ",%d,%s", i_dummy, strdup( buffer_text) );
1046 p_subtitle->i_start = i_start;
1047 p_subtitle->i_stop = i_stop;
1052 /* All the other stuff we add to the header field */
1053 if( p_sys->psz_header != NULL )
1055 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
1056 strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
1058 msg_Err( p_demux, "out of memory");
1061 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
1062 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1066 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
1068 msg_Err( p_demux, "out of memory");
1071 p_sys->psz_header = strdup( s );
1072 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1078 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1080 demux_sys_t *p_sys = p_demux->p_sys;
1081 text_t *txt = &p_sys->txt;
1085 * h:m:s:Line1|Line2|Line3....
1087 * h:m:s Line1|Line2|Line3....
1091 char buffer_text[MAX_LINE + 1];
1095 p_subtitle->i_start = 0;
1096 p_subtitle->i_stop = 0;
1097 p_subtitle->i_vobsub_location = 0;
1098 p_subtitle->psz_text = NULL;
1105 if( ( p = TextGetLine( txt ) ) == NULL )
1107 return( VLC_EGENERIC );
1112 memset( buffer_text, '\0', MAX_LINE );
1113 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1115 i_start = ( (mtime_t)h * 3600*1000 +
1116 (mtime_t)m * 60*1000 +
1117 (mtime_t)s * 1000 ) * 1000;
1122 /* replace | by \n */
1123 for( i = 0; i < strlen( buffer_text ); i++ )
1125 if( buffer_text[i] == '|' )
1127 buffer_text[i] = '\n';
1130 p_subtitle->i_start = i_start;
1132 p_subtitle->i_stop = 0;
1133 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1137 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1141 if( strcasestr( psz_start, psz_str ) )
1143 char *s = strcasestr( psz_start, psz_str );
1145 s += strlen( psz_str );
1153 if( ( p = TextGetLine( txt ) ) == NULL )
1157 if( strcasestr( p, psz_str ) )
1159 char *s = strcasestr( p, psz_str );
1161 s += strlen( psz_str );
1168 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1170 demux_sys_t *p_sys = p_demux->p_sys;
1171 text_t *txt = &p_sys->txt;
1177 char buffer_text[10*MAX_LINE + 1];
1179 p_subtitle->i_start = 0;
1180 p_subtitle->i_stop = 0;
1181 p_subtitle->i_vobsub_location = 0;
1182 p_subtitle->psz_text = NULL;
1185 if( i_text < 10*MAX_LINE ) \
1187 buffer_text[i_text++] = c; \
1188 buffer_text[i_text] = '\0'; \
1191 /* search "Start=" */
1192 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1194 return VLC_EGENERIC;
1197 /* get start value */
1198 i_start = strtol( p, &p, 0 );
1201 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1203 return VLC_EGENERIC;
1206 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1208 return VLC_EGENERIC;
1212 buffer_text[0] = '\0';
1213 /* now get all txt until a "Start=" line */
1220 if( !strncasecmp( p, "<br", 3 ) )
1224 else if( strcasestr( p, "Start=" ) )
1226 TextPreviousLine( txt );
1229 p = ParseSamiSearch( txt, p, ">" );
1231 else if( !strncmp( p, " ", 6 ) )
1236 else if( *p == '\t' )
1249 p = TextGetLine( txt );
1258 p_subtitle->i_start = i_start * 1000;
1259 p_subtitle->i_stop = 0;
1260 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1262 return( VLC_SUCCESS );
1266 static int ParseVobSubIDX( demux_t *p_demux, subtitle_t *p_subtitle )
1268 demux_sys_t *p_sys = p_demux->p_sys;
1269 text_t *txt = &p_sys->txt;
1272 * Parse the idx file. Each line:
1273 * timestamp: hh:mm:ss:mss, filepos: loc
1274 * hexint is the hex location of the vobsub in the .sub file
1278 char buffer_text[MAX_LINE + 1];
1279 unsigned int i_start, i_location;
1281 p_subtitle->i_start = 0;
1282 p_subtitle->i_stop = 0;
1283 p_subtitle->i_vobsub_location = 0;
1284 p_subtitle->psz_text = NULL;
1288 unsigned int h, m, s, ms, loc;
1290 if( ( p = TextGetLine( txt ) ) == NULL )
1292 return( VLC_EGENERIC );
1296 memset( buffer_text, '\0', MAX_LINE );
1297 if( sscanf( p, "timestamp: %d:%d:%d:%d, filepos: %x%[^\r\n]",
1298 &h, &m, &s, &ms, &loc, buffer_text ) == 5 )
1300 i_start = ( (mtime_t)h * 3600*1000 +
1301 (mtime_t)m * 60*1000 +
1303 (mtime_t)ms ) * 1000;
1308 p_subtitle->i_start = (mtime_t)i_start;
1309 p_subtitle->i_stop = 0;
1310 p_subtitle->psz_text = NULL;
1311 p_subtitle->i_vobsub_location = i_location;
1312 fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
1316 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
1318 demux_sys_t *p_sys = p_demux->p_sys;
1319 uint8_t *p = p_bk->p_buffer;
1320 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
1324 int i_size = ps_pkt_size( p, p_end - p );
1333 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
1335 msg_Warn( p_demux, "invalid PES" );
1341 msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
1346 /* Create a block */
1347 p_pkt = block_New( p_demux, i_size );
1348 memcpy( p_pkt->p_buffer, p, i_size);
1351 i_id = ps_pkt_id( p_pkt );
1352 if( (i_id&0xffe0) != 0xbd20 ||
1353 ps_pkt_parse_pes( p_pkt, 1 ) )
1355 block_Release( p_pkt );
1359 msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
1361 /* FIXME i_spu == determines which of the spu tracks we will show. */
1362 if( p_sys->es && i_spu == 0 )
1364 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
1365 p_pkt->i_length = 0;
1366 es_out_Send( p_demux->out, p_sys->es, p_pkt );
1368 p_bk->i_pts = 0; /* only first packet has a pts */
1372 block_Release( p_pkt );