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;
306 /* It will nearly always work even for non seekable stream thanks the
307 * caching system, and if it fails we loose just a few sub */
308 if( stream_Seek( p_demux->s, 0 ) )
310 msg_Warn( p_demux, "failed to rewind" );
313 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
315 msg_Err( p_demux, "failed to recognize subtitle type" );
321 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
323 msg_Dbg( p_demux, "detected %s format",
324 sub_read_subtitle_function[i].psz_name );
325 pf_read = sub_read_subtitle_function[i].pf_read;
330 msg_Dbg( p_demux, "loading all subtitles..." );
332 /* Load the whole file */
333 TextLoad( &p_sys->txt, p_demux->s );
338 if( p_sys->i_subtitles >= i_max )
341 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
342 sizeof(subtitle_t) * i_max ) ) )
344 msg_Err( p_demux, "out of memory");
349 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
352 p_sys->i_subtitles++;
355 TextUnload( &p_sys->txt );
357 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
359 /* Fix subtitle (order and time) *** */
360 p_sys->i_subtitle = 0;
361 if( p_sys->i_type != SUB_TYPE_VOBSUB )
366 if( p_sys->i_subtitles > 0 )
368 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
370 if( p_sys->i_length <= 0 )
371 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
374 /* *** add subtitle ES *** */
375 if( p_sys->i_type == SUB_TYPE_VOBSUB )
377 int i_len = strlen( p_demux->psz_path );
378 char *psz_vobname = strdup( p_demux->psz_path );
380 strcpy( psz_vobname + i_len - 4, ".sub" );
383 if( !( p_sys->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
385 msg_Err( p_demux, "couldn't open .sub Vobsub file: %s",
390 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
392 else if( p_sys->i_type == SUB_TYPE_SSA1 ||
393 p_sys->i_type == SUB_TYPE_SSA2_4 )
395 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
399 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
401 if( p_sys->psz_header != NULL )
403 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
404 fmt.p_extra = strdup( p_sys->psz_header );
406 p_sys->es = es_out_Add( p_demux->out, &fmt );
411 /*****************************************************************************
412 * Close: Close subtitle demux
413 *****************************************************************************/
414 static void Close( vlc_object_t *p_this )
416 demux_t *p_demux = (demux_t*)p_this;
417 demux_sys_t *p_sys = p_demux->p_sys;
420 for( i = 0; i < p_sys->i_subtitles; i++ )
422 if( p_sys->subtitle[i].psz_text )
423 free( p_sys->subtitle[i].psz_text );
425 if( p_sys->subtitle )
426 free( p_sys->subtitle );
428 if( p_sys->p_vobsub_file )
429 fclose( p_sys->p_vobsub_file );
434 /*****************************************************************************
436 *****************************************************************************/
437 static int Control( demux_t *p_demux, int i_query, va_list args )
439 demux_sys_t *p_sys = p_demux->p_sys;
445 case DEMUX_GET_LENGTH:
446 pi64 = (int64_t*)va_arg( args, int64_t * );
447 *pi64 = p_sys->i_length;
451 pi64 = (int64_t*)va_arg( args, int64_t * );
452 if( p_sys->i_subtitle < p_sys->i_subtitles )
454 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
460 i64 = (int64_t)va_arg( args, int64_t );
461 p_sys->i_subtitle = 0;
462 while( p_sys->i_subtitle < p_sys->i_subtitles &&
463 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
467 if( p_sys->i_subtitle >= p_sys->i_subtitles )
471 case DEMUX_GET_POSITION:
472 pf = (double*)va_arg( args, double * );
473 if( p_sys->i_subtitle >= p_sys->i_subtitles )
477 else if( p_sys->i_subtitles > 0 )
479 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
480 (double)p_sys->i_length;
488 case DEMUX_SET_POSITION:
489 f = (double)va_arg( args, double );
490 i64 = f * p_sys->i_length;
492 p_sys->i_subtitle = 0;
493 while( p_sys->i_subtitle < p_sys->i_subtitles &&
494 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
498 if( p_sys->i_subtitle >= p_sys->i_subtitles )
502 case DEMUX_SET_NEXT_DEMUX_TIME:
503 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
508 case DEMUX_GET_TITLE_INFO:
512 msg_Err( p_demux, "unknown query in subtitle control" );
517 /*****************************************************************************
518 * Demux: Send subtitle to decoder
519 *****************************************************************************/
520 static int Demux( demux_t *p_demux )
522 demux_sys_t *p_sys = p_demux->p_sys;
525 if( p_sys->i_subtitle >= p_sys->i_subtitles )
528 i_maxdate = p_sys->i_next_demux_date;
529 msg_Dbg( p_demux, "sub demux until %lld", i_maxdate );
530 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
532 /* Should not happen */
533 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
536 if( p_sys->i_type != SUB_TYPE_VOBSUB )
538 while( p_sys->i_subtitle < p_sys->i_subtitles &&
539 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
542 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
544 msg_Dbg( p_demux, "sub[%d] '%s'", p_sys->i_subtitle, p_sys->subtitle[p_sys->i_subtitle].psz_text );
552 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
558 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
564 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
565 p_block->i_dts = p_block->i_pts;
566 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
569 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
572 memcpy( p_block->p_buffer,
573 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
574 if( p_block->i_pts > 0 )
576 es_out_Send( p_demux->out, p_sys->es, p_block );
580 block_Release( p_block );
587 while( p_sys->i_subtitle < p_sys->i_subtitles &&
588 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
590 int i_pos = p_sys->subtitle[p_sys->i_subtitle].i_vobsub_location;
594 /* first compute SPU size */
595 if( p_sys->i_subtitle + 1 < p_sys->i_subtitles )
597 i_size = p_sys->subtitle[p_sys->i_subtitle+1].i_vobsub_location - i_pos;
599 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
601 /* Seek at the right place */
602 if( fseek( p_sys->p_vobsub_file, i_pos, SEEK_SET ) )
605 "cannot seek at right vobsub location %d", i_pos );
610 /* allocate a packet */
611 if( ( p_block = block_New( p_demux, i_size ) ) == NULL )
618 p_block->i_buffer = fread( p_block->p_buffer, 1, i_size,
619 p_sys->p_vobsub_file );
620 if( p_block->i_buffer <= 6 )
622 block_Release( p_block );
628 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
630 /* demux this block */
631 DemuxVobSub( p_demux, p_block );
638 p_sys->i_next_demux_date = 0;
643 /*****************************************************************************
644 * Fix: fix time stamp and order of subtitle
645 *****************************************************************************/
646 static void Fix( demux_t *p_demux )
648 demux_sys_t *p_sys = p_demux->p_sys;
652 /* *** fix order (to be sure...) *** */
653 /* We suppose that there are near in order and this durty bubble sort
654 * wont take too much time
659 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
661 if( p_sys->subtitle[i_index].i_start <
662 p_sys->subtitle[i_index - 1].i_start )
666 p_sys->subtitle + i_index - 1,
667 sizeof( subtitle_t ) );
668 memcpy( p_sys->subtitle + i_index - 1,
669 p_sys->subtitle + i_index,
670 sizeof( subtitle_t ) );
671 memcpy( p_sys->subtitle + i_index,
673 sizeof( subtitle_t ) );
680 static int TextLoad( text_t *txt, stream_t *s )
686 txt->i_line_count = 0;
688 txt->line = calloc( i_line_max, sizeof( char * ) );
690 /* load the complete file */
693 char *psz = stream_ReadLine( s );
698 txt->line[txt->i_line_count++] = psz;
699 if( txt->i_line_count >= i_line_max )
702 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
706 if( txt->i_line_count <= 0 )
714 static void TextUnload( text_t *txt )
718 for( i = 0; i < txt->i_line_count; i++ )
720 free( txt->line[i] );
724 txt->i_line_count = 0;
727 static char *TextGetLine( text_t *txt )
729 if( txt->i_line >= txt->i_line_count )
732 return txt->line[txt->i_line++];
734 static void TextPreviousLine( text_t *txt )
736 if( txt->i_line > 0 )
740 /*****************************************************************************
741 * Specific Subtitle function
742 *****************************************************************************/
743 #define MAX_LINE 8192
744 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
746 demux_sys_t *p_sys = p_demux->p_sys;
747 text_t *txt = &p_sys->txt;
750 * {n1}{n2}Line1|Line2|Line3....
751 * where n1 and n2 are the video frame number...
756 char buffer_text[MAX_LINE + 1];
757 unsigned int i_start;
761 p_subtitle->i_start = 0;
762 p_subtitle->i_stop = 0;
763 p_subtitle->i_vobsub_location = 0;
764 p_subtitle->psz_text = NULL;
768 if( ( s = TextGetLine( txt ) ) == NULL )
770 return( VLC_EGENERIC );
775 memset( buffer_text, '\0', MAX_LINE );
776 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
777 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
782 /* replace | by \n */
783 for( i = 0; i < strlen( buffer_text ); i++ )
785 if( buffer_text[i] == '|' )
787 buffer_text[i] = '\n';
791 p_subtitle->i_start = (mtime_t)i_start * p_sys->i_microsecperframe;
792 p_subtitle->i_stop = (mtime_t)i_stop * p_sys->i_microsecperframe;
793 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
797 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
799 demux_sys_t *p_sys = p_demux->p_sys;
800 text_t *txt = &p_sys->txt;
804 * h1:m1:s1,d1 --> h2:m2:s2,d2
812 char buffer_text[ 10 * MAX_LINE];
817 p_subtitle->i_start = 0;
818 p_subtitle->i_stop = 0;
819 p_subtitle->i_vobsub_location = 0;
820 p_subtitle->psz_text = NULL;
824 int h1, m1, s1, d1, h2, m2, s2, d2;
825 if( ( s = TextGetLine( txt ) ) == NULL )
827 return( VLC_EGENERIC );
830 "%d:%d:%d,%d --> %d:%d:%d,%d",
832 &h2, &m2, &s2, &d2 ) == 8 )
834 i_start = ( (mtime_t)h1 * 3600*1000 +
835 (mtime_t)m1 * 60*1000 +
837 (mtime_t)d1 ) * 1000;
839 i_stop = ( (mtime_t)h2 * 3600*1000 +
840 (mtime_t)m2 * 60*1000 +
842 (mtime_t)d2 ) * 1000;
844 /* Now read text until an empty line */
845 for( i_buffer_text = 0;; )
848 if( ( s = TextGetLine( txt ) ) == NULL )
850 return( VLC_EGENERIC );
856 /* empty line -> end of this subtitle */
857 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
858 p_subtitle->i_start = i_start;
859 p_subtitle->i_stop = i_stop;
860 p_subtitle->psz_text = strdup( buffer_text );
861 /* If framerate is available, use sub-fps */
862 if( p_sys->i_microsecperframe != 0 &&
863 p_sys->i_original_mspf != 0)
865 p_subtitle->i_start = (mtime_t)i_start *
866 p_sys->i_original_mspf /
867 p_sys->i_microsecperframe;
868 p_subtitle->i_stop = (mtime_t)i_stop *
869 p_sys->i_original_mspf /
870 p_sys->i_microsecperframe;
876 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
878 memcpy( buffer_text + i_buffer_text,
881 i_buffer_text += i_len;
883 buffer_text[i_buffer_text] = '\n';
892 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
894 demux_sys_t *p_sys = p_demux->p_sys;
895 text_t *txt = &p_sys->txt;
898 * h1:m1:s1.d1,h2:m2:s2.d2
903 * ( works with subviewer and subviewer v2 )
906 char buffer_text[ 10 * MAX_LINE];
911 p_subtitle->i_start = 0;
912 p_subtitle->i_stop = 0;
913 p_subtitle->i_vobsub_location = 0;
914 p_subtitle->psz_text = NULL;
918 int h1, m1, s1, d1, h2, m2, s2, d2;
919 if( ( s = TextGetLine( txt ) ) == NULL )
921 return( VLC_EGENERIC );
924 "%d:%d:%d.%d,%d:%d:%d.%d",
926 &h2, &m2, &s2, &d2 ) == 8 )
928 i_start = ( (mtime_t)h1 * 3600*1000 +
929 (mtime_t)m1 * 60*1000 +
931 (mtime_t)d1 ) * 1000;
933 i_stop = ( (mtime_t)h2 * 3600*1000 +
934 (mtime_t)m2 * 60*1000 +
936 (mtime_t)d2 ) * 1000;
938 /* Now read text until an empty line */
939 for( i_buffer_text = 0;; )
942 if( ( s = TextGetLine( txt ) ) == NULL )
944 return( VLC_EGENERIC );
950 /* empty line -> end of this subtitle */
951 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
952 p_subtitle->i_start = i_start;
953 p_subtitle->i_stop = i_stop;
955 /* replace [br] by \n */
956 for( i = 0; i < strlen( buffer_text ) - 3; i++ )
958 if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' &&
959 buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' )
961 char *temp = buffer_text + i + 1;
962 buffer_text[i] = '\n';
963 memmove( temp, temp+3, strlen( temp-3 ));
966 p_subtitle->psz_text = strdup( buffer_text );
971 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
973 memcpy( buffer_text + i_buffer_text,
976 i_buffer_text += i_len;
978 buffer_text[i_buffer_text] = '\n';
988 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
990 demux_sys_t *p_sys = p_demux->p_sys;
991 text_t *txt = &p_sys->txt;
993 char buffer_text[ 10 * MAX_LINE];
998 p_subtitle->i_start = 0;
999 p_subtitle->i_stop = 0;
1000 p_subtitle->i_vobsub_location = 0;
1001 p_subtitle->psz_text = NULL;
1005 int h1, m1, s1, c1, h2, m2, s2, c2;
1008 if( ( s = TextGetLine( txt ) ) == NULL )
1010 return( VLC_EGENERIC );
1012 p_subtitle->psz_text = malloc( strlen( s ) );
1015 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
1019 buffer_text ) == 10 )
1021 i_start = ( (mtime_t)h1 * 3600*1000 +
1022 (mtime_t)m1 * 60*1000 +
1023 (mtime_t)s1 * 1000 +
1024 (mtime_t)c1 * 10 ) * 1000;
1026 i_stop = ( (mtime_t)h2 * 3600*1000 +
1027 (mtime_t)m2 * 60*1000 +
1028 (mtime_t)s2 * 1000 +
1029 (mtime_t)c2 * 10 ) * 1000;
1031 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1032 if( p_sys->i_type == SUB_TYPE_SSA1 )
1034 sprintf( p_subtitle->psz_text,
1035 ",%d%s", i_dummy, strdup( buffer_text) );
1039 sprintf( p_subtitle->psz_text,
1040 ",%d,%s", i_dummy, strdup( buffer_text) );
1042 p_subtitle->i_start = i_start;
1043 p_subtitle->i_stop = i_stop;
1048 /* All the other stuff we add to the header field */
1049 if( p_sys->psz_header != NULL )
1051 if( !( p_sys->psz_header = realloc( p_sys->psz_header,
1052 strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) )
1054 msg_Err( p_demux, "out of memory");
1057 p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) );
1058 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1062 if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) )
1064 msg_Err( p_demux, "out of memory");
1067 p_sys->psz_header = strdup( s );
1068 p_sys->psz_header = strcat( p_sys->psz_header, "\n" );
1074 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
1076 demux_sys_t *p_sys = p_demux->p_sys;
1077 text_t *txt = &p_sys->txt;
1081 * h:m:s:Line1|Line2|Line3....
1083 * h:m:s Line1|Line2|Line3....
1087 char buffer_text[MAX_LINE + 1];
1091 p_subtitle->i_start = 0;
1092 p_subtitle->i_stop = 0;
1093 p_subtitle->i_vobsub_location = 0;
1094 p_subtitle->psz_text = NULL;
1101 if( ( p = TextGetLine( txt ) ) == NULL )
1103 return( VLC_EGENERIC );
1108 memset( buffer_text, '\0', MAX_LINE );
1109 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
1111 i_start = ( (mtime_t)h * 3600*1000 +
1112 (mtime_t)m * 60*1000 +
1113 (mtime_t)s * 1000 ) * 1000;
1118 /* replace | by \n */
1119 for( i = 0; i < strlen( buffer_text ); i++ )
1121 if( buffer_text[i] == '|' )
1123 buffer_text[i] = '\n';
1126 p_subtitle->i_start = i_start;
1128 p_subtitle->i_stop = 0;
1129 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
1133 static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str )
1137 if( strcasestr( psz_start, psz_str ) )
1139 char *s = strcasestr( psz_start, psz_str );
1141 s += strlen( psz_str );
1149 if( ( p = TextGetLine( txt ) ) == NULL )
1153 if( strcasestr( p, psz_str ) )
1155 char *s = strcasestr( p, psz_str );
1157 s += strlen( psz_str );
1164 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1166 demux_sys_t *p_sys = p_demux->p_sys;
1167 text_t *txt = &p_sys->txt;
1173 char buffer_text[10*MAX_LINE + 1];
1175 p_subtitle->i_start = 0;
1176 p_subtitle->i_stop = 0;
1177 p_subtitle->i_vobsub_location = 0;
1178 p_subtitle->psz_text = NULL;
1181 if( i_text < 10*MAX_LINE ) \
1183 buffer_text[i_text++] = c; \
1184 buffer_text[i_text] = '\0'; \
1187 /* search "Start=" */
1188 if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1190 return VLC_EGENERIC;
1193 /* get start value */
1194 i_start = strtol( p, &p, 0 );
1197 if( !( p = ParseSamiSearch( txt, p, "<P" ) ) )
1199 return VLC_EGENERIC;
1202 if( !( p = ParseSamiSearch( txt, p, ">" ) ) )
1204 return VLC_EGENERIC;
1208 buffer_text[0] = '\0';
1209 /* now get all txt until a "Start=" line */
1216 if( !strncasecmp( p, "<br", 3 ) )
1220 else if( strcasestr( p, "Start=" ) )
1222 TextPreviousLine( txt );
1225 p = ParseSamiSearch( txt, p, ">" );
1227 else if( !strncmp( p, " ", 6 ) )
1232 else if( *p == '\t' )
1245 p = TextGetLine( txt );
1254 p_subtitle->i_start = i_start * 1000;
1255 p_subtitle->i_stop = 0;
1256 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1258 return( VLC_SUCCESS );
1262 static int ParseVobSubIDX( demux_t *p_demux, subtitle_t *p_subtitle )
1264 demux_sys_t *p_sys = p_demux->p_sys;
1265 text_t *txt = &p_sys->txt;
1268 * Parse the idx file. Each line:
1269 * timestamp: hh:mm:ss:mss, filepos: loc
1270 * hexint is the hex location of the vobsub in the .sub file
1274 char buffer_text[MAX_LINE + 1];
1275 unsigned int i_start, i_location;
1277 p_subtitle->i_start = 0;
1278 p_subtitle->i_stop = 0;
1279 p_subtitle->i_vobsub_location = 0;
1280 p_subtitle->psz_text = NULL;
1284 unsigned int h, m, s, ms, loc;
1286 if( ( p = TextGetLine( txt ) ) == NULL )
1288 return( VLC_EGENERIC );
1292 memset( buffer_text, '\0', MAX_LINE );
1293 if( sscanf( p, "timestamp: %d:%d:%d:%d, filepos: %x%[^\r\n]",
1294 &h, &m, &s, &ms, &loc, buffer_text ) == 5 )
1296 i_start = ( (mtime_t)h * 3600*1000 +
1297 (mtime_t)m * 60*1000 +
1299 (mtime_t)ms ) * 1000;
1304 p_subtitle->i_start = (mtime_t)i_start;
1305 p_subtitle->i_stop = 0;
1306 p_subtitle->psz_text = NULL;
1307 p_subtitle->i_vobsub_location = i_location;
1308 fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
1312 static int DemuxVobSub( demux_t *p_demux, block_t *p_bk )
1314 demux_sys_t *p_sys = p_demux->p_sys;
1315 uint8_t *p = p_bk->p_buffer;
1316 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
1320 int i_size = ps_pkt_size( p, p_end - p );
1329 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
1331 msg_Warn( p_demux, "invalid PES" );
1337 msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
1342 /* Create a block */
1343 p_pkt = block_New( p_demux, i_size );
1344 memcpy( p_pkt->p_buffer, p, i_size);
1347 i_id = ps_pkt_id( p_pkt );
1348 if( (i_id&0xffe0) != 0xbd20 ||
1349 ps_pkt_parse_pes( p_pkt, 1 ) )
1351 block_Release( p_pkt );
1355 msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
1357 /* FIXME i_spu == determines which of the spu tracks we will show. */
1358 if( p_sys->es && i_spu == 0 )
1360 p_pkt->i_dts = p_pkt->i_pts = p_bk->i_pts;
1361 p_pkt->i_length = 0;
1362 es_out_Send( p_demux->out, p_sys->es, p_pkt );
1364 p_bk->i_pts = 0; /* only first packet has a pts */
1368 block_Release( p_pkt );