1 /*****************************************************************************
2 * sub.c: subtitle demux for external subtitle files
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: sub.c,v 1.52 2004/02/22 15:59:53 fenrir Exp $
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", 25.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->p_input = p_input;
279 if( !psz_name || !*psz_name )
281 msg_Err( p_sub, "no subtitle file specified" );
285 /* *** load the file *** */
286 if( text_load( &txt, psz_name ) )
288 msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
292 msg_Dbg( p_sub, "opened `%s'", psz_name );
294 var_Get( p_sub, "sub-fps", &val );
295 if( val.i_int >= 1.0 )
297 i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
299 else if( val.f_float <= 0 )
301 i_microsecperframe = 40000; /* default: 25fps */
304 var_Get( p_sub, "sub-type", &val);
305 if( val.psz_string && *val.psz_string )
311 if( sub_read_subtitle_function[i].psz_type_name == NULL )
313 i_sub_type = SUB_TYPE_UNKNOWN;
316 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
319 i_sub_type = sub_read_subtitle_function[i].i_type;
326 i_sub_type = SUB_TYPE_UNKNOWN;
328 FREE( val.psz_string );
330 /* *** Now try to autodetect subtitle format *** */
331 if( i_sub_type == SUB_TYPE_UNKNOWN )
336 msg_Dbg( p_input, "trying to autodetect file format" );
337 for( i_try = 0; i_try < MAX_TRY; i_try++ )
341 if( ( s = text_get_line( &txt ) ) == NULL )
346 if( local_stristr( s, "<SAMI>" ) )
348 i_sub_type = SUB_TYPE_SAMI;
351 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
352 sscanf( s, "{%d}{}", &i_dummy ) == 1)
354 i_sub_type = SUB_TYPE_MICRODVD;
358 "%d:%d:%d,%d --> %d:%d:%d,%d",
359 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
360 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
362 i_sub_type = SUB_TYPE_SUBRIP;
366 "!: This is a Sub Station Alpha v%d.x script.",
371 i_sub_type = SUB_TYPE_SSA1;
375 i_sub_type = SUB_TYPE_SSA2_4; /* I hope this will work */
379 else if( local_stristr( s, "This is a Sub Station Alpha v4 script" ) )
381 i_sub_type = SUB_TYPE_SSA2_4; /* I hope this will work */
384 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
386 i_sub_type = SUB_TYPE_SSA2_4; /* could be wrong */
389 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
390 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
392 i_sub_type = SUB_TYPE_VPLAYER;
395 else if( local_stristr( s, "# VobSub index file" ) )
397 i_sub_type = SUB_TYPE_VOBSUB;
405 /* *** Load this file in memory *** */
408 if( sub_read_subtitle_function[i].i_type == SUB_TYPE_UNKNOWN )
410 msg_Dbg( p_input, "unknown subtitle file" );
415 if( sub_read_subtitle_function[i].i_type == i_sub_type )
417 msg_Dbg( p_input, "detected %s format",
418 sub_read_subtitle_function[i].psz_name );
419 p_sub->i_sub_type = i_sub_type;
420 pf_read_subtitle = sub_read_subtitle_function[i].pf_read_subtitle;
427 if( p_sub->i_subtitles >= i_max )
430 if( !( p_sub->subtitle = realloc( p_sub->subtitle,
431 sizeof(subtitle_t) * i_max ) ) )
433 msg_Err( p_sub, "out of memory");
437 if( pf_read_subtitle( p_sub, &txt,
438 p_sub->subtitle + p_sub->i_subtitles,
439 i_microsecperframe ) < 0 )
443 p_sub->i_subtitles++;
445 msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles );
447 /* *** Close the file *** */
450 /* *** fix subtitle (order and time) *** */
451 p_sub->i_subtitle = 0; /* will be modified by sub_fix */
453 if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
458 /* *** add subtitle ES *** */
459 if( p_sub->i_sub_type == SUB_TYPE_VOBSUB )
461 int i_len = strlen( psz_name );
462 char *psz_vobname = strdup(psz_name);
464 strcpy( psz_vobname + i_len - 4, ".sub" );
467 if( !( p_sub->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
469 msg_Err( p_sub, "couldn't open .sub Vobsub file: %s", psz_vobname );
473 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
475 else if( p_sub->i_sub_type == SUB_TYPE_SSA1 ||
476 p_sub->i_sub_type == SUB_TYPE_SSA2_4 )
478 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
482 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
484 if( p_sub->psz_header != NULL )
486 fmt.i_extra = strlen( p_sub->psz_header ) + 1;
487 fmt.p_extra = strdup( p_sub->psz_header );
489 p_sub->p_es = es_out_Add( p_input->p_es_out, &fmt );
490 p_sub->i_previously_selected = 0;
495 /*****************************************************************************
496 * sub_demux: Send subtitle to decoder until i_maxdate
497 *****************************************************************************/
498 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
500 input_thread_t *p_input = p_sub->p_input;
503 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_sub->p_es, &b );
504 if( b && !p_sub->i_previously_selected )
506 p_sub->i_previously_selected = 1;
507 p_sub->pf_seek( p_sub, i_maxdate );
510 else if( !b && p_sub->i_previously_selected )
512 p_sub->i_previously_selected = 0;
516 if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
518 while( p_sub->i_subtitle < p_sub->i_subtitles &&
519 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
522 int i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
531 if( ( p_block = block_New( p_sub->p_input, i_len ) ) == NULL )
537 /* XXX we should convert all demuxers to use es_out_Control to set pcr and
538 * then remove that */
540 input_ClockGetTS( p_sub->p_input,
541 p_sub->p_input->stream.p_selected_program,
542 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
544 if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
546 /* FIXME kludge i_dts means end of display... */
548 input_ClockGetTS( p_sub->p_input,
549 p_sub->p_input->stream.p_selected_program,
550 p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
553 memcpy( p_block->p_buffer, p_sub->subtitle[p_sub->i_subtitle].psz_text, i_len );
555 if( p_block->i_pts > 0 )
557 es_out_Send( p_input->p_es_out, p_sub->p_es, p_block );
561 block_Release( p_block );
568 while( p_sub->i_subtitle < p_sub->i_subtitles &&
569 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
571 int i_pos = p_sub->subtitle[p_sub->i_subtitle].i_vobsub_location;
575 /* first compute SPU size */
576 if( p_sub->i_subtitle + 1 < p_sub->i_subtitles )
578 i_size = p_sub->subtitle[p_sub->i_subtitle+1].i_vobsub_location - i_pos;
580 if( i_size <= 0 ) i_size = 65535; /* Invalid or EOF */
582 /* Seek at the right place (could be avoid if sub_seek is fixed to do his job) */
583 if( fseek( p_sub->p_vobsub_file, i_pos, SEEK_SET ) )
585 msg_Warn( p_sub, "cannot seek at right vobsub location %d", i_pos );
590 /* allocate a packet */
591 if( ( p_block = block_New( p_sub, i_size ) ) == NULL )
598 p_block->i_buffer = fread( p_block->p_buffer, 1, i_size, p_sub->p_vobsub_file );
599 if( p_block->i_buffer <= 6 )
601 block_Release( p_block );
608 input_ClockGetTS( p_sub->p_input,
609 p_sub->p_input->stream.p_selected_program,
610 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
612 /* demux this block */
613 DemuxVobSub( p_sub, p_block );
621 /*****************************************************************************
622 * sub_seek: Seek to i_date
623 *****************************************************************************/
624 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
626 /* should be fast enough... */
627 p_sub->i_subtitle = 0;
628 while( p_sub->i_subtitle < p_sub->i_subtitles &&
629 p_sub->subtitle[p_sub->i_subtitle].i_start < i_date )
636 /*****************************************************************************
637 * sub_close: Close subtitle demux
638 *****************************************************************************/
639 static void sub_close( subtitle_demux_t *p_sub )
641 if( p_sub->subtitle )
644 for( i = 0; i < p_sub->i_subtitles; i++ )
646 if( p_sub->subtitle[i].psz_text )
648 free( p_sub->subtitle[i].psz_text );
651 free( p_sub->subtitle );
653 if( p_sub->p_vobsub_file )
655 fclose( p_sub->p_vobsub_file );
659 /*****************************************************************************
660 * sub_fix: fix time stamp and order of subtitle
661 *****************************************************************************/
662 static void sub_fix( subtitle_demux_t *p_sub )
670 /* *** fix order (to be sure...) *** */
671 /* We suppose that there are near in order and this durty bubble sort
672 * wont take too much time
677 for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
679 if( p_sub->subtitle[i_index].i_start <
680 p_sub->subtitle[i_index - 1].i_start )
684 p_sub->subtitle + i_index - 1,
685 sizeof( subtitle_t ) );
686 memcpy( p_sub->subtitle + i_index - 1,
687 p_sub->subtitle + i_index,
688 sizeof( subtitle_t ) );
689 memcpy( p_sub->subtitle + i_index,
691 sizeof( subtitle_t ) );
697 /* *** and at the end add delay *** */
698 var_Get( p_sub, "sub-delay", &val );
699 i_delay = (mtime_t) val.i_int * 100000;
702 for( i = 0; i < p_sub->i_subtitles; i++ )
704 p_sub->subtitle[i].i_start += i_delay;
705 p_sub->subtitle[i].i_stop += i_delay;
706 if( p_sub->subtitle[i].i_start < 0 )
708 p_sub->i_subtitle = i + 1;
716 /*****************************************************************************
717 * Specific Subtitle function
718 *****************************************************************************/
719 static int sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
723 * {n1}{n2}Line1|Line2|Line3....
724 * where n1 and n2 are the video frame number...
729 char buffer_text[MAX_LINE + 1];
730 unsigned int i_start;
734 p_subtitle->i_start = 0;
735 p_subtitle->i_stop = 0;
736 p_subtitle->i_vobsub_location = 0;
737 p_subtitle->psz_text = NULL;
741 if( ( s = text_get_line( txt ) ) == NULL )
743 return( VLC_EGENERIC );
748 memset( buffer_text, '\0', MAX_LINE );
749 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
750 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
755 /* replace | by \n */
756 for( i = 0; i < strlen( buffer_text ); i++ )
758 if( buffer_text[i] == '|' )
760 buffer_text[i] = '\n';
763 p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
764 p_subtitle->i_stop = (mtime_t)i_stop * (mtime_t)i_microsecperframe;
765 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
769 static int sub_SubRipRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
773 * h1:m1:s1,d1 --> h2:m2:s2,d2
781 char buffer_text[ 10 * MAX_LINE];
786 p_subtitle->i_start = 0;
787 p_subtitle->i_stop = 0;
788 p_subtitle->i_vobsub_location = 0;
789 p_subtitle->psz_text = NULL;
793 int h1, m1, s1, d1, h2, m2, s2, d2;
794 if( ( s = text_get_line( txt ) ) == NULL )
796 return( VLC_EGENERIC );
799 "%d:%d:%d,%d --> %d:%d:%d,%d",
801 &h2, &m2, &s2, &d2 ) == 8 )
803 i_start = ( (mtime_t)h1 * 3600*1000 +
804 (mtime_t)m1 * 60*1000 +
806 (mtime_t)d1 ) * 1000;
808 i_stop = ( (mtime_t)h2 * 3600*1000 +
809 (mtime_t)m2 * 60*1000 +
811 (mtime_t)d2 ) * 1000;
813 /* Now read text until an empty line */
814 for( i_buffer_text = 0;; )
817 if( ( s = text_get_line( txt ) ) == NULL )
819 return( VLC_EGENERIC );
825 /* empty line -> end of this subtitle */
826 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
827 p_subtitle->i_start = i_start;
828 p_subtitle->i_stop = i_stop;
829 p_subtitle->psz_text = strdup( buffer_text );
834 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
836 memcpy( buffer_text + i_buffer_text,
839 i_buffer_text += i_len;
841 buffer_text[i_buffer_text] = '\n';
851 static int sub_SSARead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
853 char buffer_text[ 10 * MAX_LINE];
858 p_subtitle->i_start = 0;
859 p_subtitle->i_stop = 0;
860 p_subtitle->i_vobsub_location = 0;
861 p_subtitle->psz_text = NULL;
865 int h1, m1, s1, c1, h2, m2, s2, c2;
868 if( ( s = text_get_line( txt ) ) == NULL )
870 return( VLC_EGENERIC );
872 p_subtitle->psz_text = malloc( strlen( s ) );
875 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
879 buffer_text ) == 10 )
881 i_start = ( (mtime_t)h1 * 3600*1000 +
882 (mtime_t)m1 * 60*1000 +
884 (mtime_t)c1 * 10 ) * 1000;
886 i_stop = ( (mtime_t)h2 * 3600*1000 +
887 (mtime_t)m2 * 60*1000 +
889 (mtime_t)c2 * 10 ) * 1000;
891 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
892 if( p_sub->i_sub_type == SUB_TYPE_SSA1 )
894 sprintf( p_subtitle->psz_text, ",%d%s", i_dummy, strdup( buffer_text) );
898 sprintf( p_subtitle->psz_text, ",%d,%s", i_dummy, strdup( buffer_text) );
900 p_subtitle->i_start = i_start;
901 p_subtitle->i_stop = i_stop;
906 /* All the other stuff we add to the header field */
907 if( p_sub->psz_header != NULL )
909 if( !( p_sub->psz_header = realloc( p_sub->psz_header,
910 strlen( p_sub->psz_header ) + strlen( s ) + 2 ) ) )
912 msg_Err( p_sub, "out of memory");
915 p_sub->psz_header = strcat( p_sub->psz_header, strdup( s ) );
916 p_sub->psz_header = strcat( p_sub->psz_header, "\n" );
920 if( !( p_sub->psz_header = malloc( strlen( s ) + 2 ) ) )
922 msg_Err( p_sub, "out of memory");
925 p_sub->psz_header = strdup( s );
926 p_sub->psz_header = strcat( p_sub->psz_header, "\n" );
932 static int sub_Vplayer( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
936 * h:m:s:Line1|Line2|Line3....
938 * h:m:s Line1|Line2|Line3....
939 * where n1 and n2 are the video frame number...
943 char buffer_text[MAX_LINE + 1];
947 p_subtitle->i_start = 0;
948 p_subtitle->i_stop = 0;
949 p_subtitle->i_vobsub_location = 0;
950 p_subtitle->psz_text = NULL;
957 if( ( p = text_get_line( txt ) ) == NULL )
959 return( VLC_EGENERIC );
964 memset( buffer_text, '\0', MAX_LINE );
965 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
967 i_start = ( (mtime_t)h * 3600*1000 +
968 (mtime_t)m * 60*1000 +
969 (mtime_t)s * 1000 ) * 1000;
974 /* replace | by \n */
975 for( i = 0; i < strlen( buffer_text ); i++ )
977 if( buffer_text[i] == '|' )
979 buffer_text[i] = '\n';
982 p_subtitle->i_start = i_start;
984 p_subtitle->i_stop = 0;
985 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
989 static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
993 if( local_stristr( psz_start, psz_str ) )
995 char *s = local_stristr( psz_start, psz_str );
997 s += strlen( psz_str );
1005 if( ( p = text_get_line( txt ) ) == NULL )
1009 if( local_stristr( p, psz_str ) )
1011 char *s = local_stristr( p, psz_str );
1013 s += strlen( psz_str );
1020 static int sub_Sami( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
1026 char buffer_text[10*MAX_LINE + 1];
1028 p_subtitle->i_start = 0;
1029 p_subtitle->i_stop = 0;
1030 p_subtitle->i_vobsub_location = 0;
1031 p_subtitle->psz_text = NULL;
1034 if( i_text < 10*MAX_LINE ) \
1036 buffer_text[i_text++] = c; \
1037 buffer_text[i_text] = '\0'; \
1040 /* search "Start=" */
1041 if( !( p = sub_SamiSearch( txt, NULL, "Start=" ) ) )
1043 return VLC_EGENERIC;
1046 /* get start value */
1047 i_start = strtol( p, &p, 0 );
1050 if( !( p = sub_SamiSearch( txt, p, "<P" ) ) )
1052 return VLC_EGENERIC;
1055 if( !( p = sub_SamiSearch( txt, p, ">" ) ) )
1057 return VLC_EGENERIC;
1061 buffer_text[0] = '\0';
1062 /* now get all txt until a "Start=" line */
1069 if( !strncasecmp( p, "<br", 3 ) )
1073 else if( local_stristr( p, "Start=" ) )
1075 text_previous_line( txt );
1078 p = sub_SamiSearch( txt, p, ">" );
1080 else if( !strncmp( p, " ", 6 ) )
1085 else if( *p == '\t' )
1098 p = text_get_line( txt );
1107 p_subtitle->i_start = i_start * 1000;
1108 p_subtitle->i_stop = 0;
1109 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1111 return( VLC_SUCCESS );
1115 static int sub_VobSubIDX( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
1118 * Parse the idx file. Each line:
1119 * timestamp: hh:mm:ss:mss, filepos: loc
1120 * hexint is the hex location of the vobsub in the .sub file
1124 char buffer_text[MAX_LINE + 1];
1125 unsigned int i_start, i_location;
1127 p_subtitle->i_start = 0;
1128 p_subtitle->i_stop = 0;
1129 p_subtitle->i_vobsub_location = 0;
1130 p_subtitle->psz_text = NULL;
1134 unsigned int h, m, s, ms, loc;
1136 if( ( p = text_get_line( txt ) ) == NULL )
1138 return( VLC_EGENERIC );
1142 memset( buffer_text, '\0', MAX_LINE );
1143 if( sscanf( p, "timestamp: %d:%d:%d:%d, filepos: %x%[^\r\n]",
1144 &h, &m, &s, &ms, &loc, buffer_text ) == 5 )
1146 i_start = ( (mtime_t)h * 3600*1000 +
1147 (mtime_t)m * 60*1000 +
1149 (mtime_t)ms ) * 1000;
1154 p_subtitle->i_start = (mtime_t)i_start;
1155 p_subtitle->i_stop = 0;
1156 p_subtitle->psz_text = NULL;
1157 p_subtitle->i_vobsub_location = i_location;
1158 fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
1162 static int DemuxVobSub( subtitle_demux_t *p_demux, block_t *p_bk )
1164 uint8_t *p = p_bk->p_buffer;
1165 uint8_t *p_end = &p_bk->p_buffer[p_bk->i_buffer];
1169 int i_size = ps_pkt_size( p, p_end - p );
1178 if( p[0] != 0 || p[1] != 0 || p[2] != 0x01 )
1180 msg_Warn( p_demux, "invalid PES" );
1186 msg_Dbg( p_demux, "we don't need these ps packets (id=0x1%2.2x)", p[3] );
1191 /* Create a block */
1192 p_pkt = block_New( p_demux, i_size );
1193 memcpy( p_pkt->p_buffer, p, i_size);
1196 i_id = ps_pkt_id( p_pkt );
1197 if( (i_id&0xffe0) != 0xbd20 ||
1198 ps_pkt_parse_pes( p_pkt, 1 ) )
1200 block_Release( p_pkt );
1204 msg_Dbg( p_demux, "SPU track %d size %d", i_spu, i_size );
1206 /* FIXME i_spu == determines which of the spu tracks we will show. */
1207 if( p_demux->p_es && i_spu == 0 )
1209 p_pkt->i_pts = p_bk->i_pts;
1211 es_out_Send( p_demux->p_input->p_es_out, p_demux->p_es, p_pkt );
1213 p_bk->i_pts = 0; /* only first packet has a pts */
1217 block_Release( p_pkt );