1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2003 VideoLAN
5 * $Id: sub.c,v 1.41 2004/01/25 20:05:29 hartman Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <sys/types.h>
33 #include <vlc/input.h>
34 #include "vlc_video.h"
39 #if (!defined( WIN32 ) || defined(__MINGW32__))
43 static int Open ( vlc_object_t *p_this );
45 static int sub_open ( subtitle_demux_t *p_sub,
46 input_thread_t *p_input,
48 mtime_t i_microsecperframe,
50 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate );
51 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date );
52 static void sub_close( subtitle_demux_t *p_sub );
54 static void sub_fix( subtitle_demux_t *p_sub );
56 static char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "ssa1",
57 "ssa2-4", "vplayer", "sami", "vobsub" };
59 /*****************************************************************************
61 *****************************************************************************/
62 #define SUB_DELAY_LONGTEXT \
63 "Delay subtitles (in 1/10s)"
64 #define SUB_FPS_LONGTEXT \
65 "Override frames per second. " \
66 "It will only work with MicroDVD subtitles."
67 #define SUB_TYPE_LONGTEXT \
68 "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \
69 "\"sami\" (auto for autodetection, it should always work)."
72 set_description( _("Text subtitles demux") );
73 set_capability( "subtitle demux", 12 );
74 add_float( "sub-fps", 25.0, NULL,
75 N_("Frames per second"),
76 SUB_FPS_LONGTEXT, VLC_TRUE );
77 add_integer( "sub-delay", 0, NULL,
78 N_("Delay subtitles (in 1/10s)"),
79 SUB_DELAY_LONGTEXT, VLC_TRUE );
80 add_string( "sub-type", "auto", NULL, "Subtitles fileformat",
81 SUB_TYPE_LONGTEXT, VLC_TRUE );
82 change_string_list( ppsz_sub_type, 0, 0 );
83 set_callbacks( Open, NULL );
86 /*****************************************************************************
88 *****************************************************************************/
89 static int Open ( vlc_object_t *p_this )
91 subtitle_demux_t *p_sub = (subtitle_demux_t*)p_this;
93 p_sub->pf_open = sub_open;
94 p_sub->pf_demux = sub_demux;
95 p_sub->pf_seek = sub_seek;
96 p_sub->pf_close = sub_close;
98 /* Initialize the variables */
99 var_Create( p_this, "sub-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
100 var_Create( p_this, "sub-delay", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
101 var_Create( p_this, "sub-type", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
106 #define MAX_LINE 2048
108 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
117 static int text_load( text_t *txt, char *psz_name )
124 txt->i_line_count = 0;
126 txt->line = calloc( i_line_max, sizeof( char * ) );
129 if( !( f = fopen( psz_name, "rb" ) ) )
134 /* load the complete file */
140 if( fgets( buffer, 8096, f ) <= 0)
144 while( ( p = strchr( buffer, '\r' ) ) )
148 while( ( p = strchr( buffer, '\n' ) ) )
153 txt->line[txt->i_line_count++] = strdup( buffer );
155 if( txt->i_line_count >= i_line_max )
158 txt->line = realloc( txt->line, i_line_max * sizeof( char*) );
160 printf( "hoi, %s", txt->line );
165 if( txt->i_line_count <= 0 )
168 return( VLC_EGENERIC );
171 return( VLC_SUCCESS );
173 static void text_unload( text_t *txt )
177 for( i = 0; i < txt->i_line_count; i++ )
179 FREE( txt->line[i] );
183 txt->i_line_count = 0;
186 static char *text_get_line( text_t *txt )
188 if( txt->i_line >= txt->i_line_count )
193 return( txt->line[txt->i_line++] );
195 static void text_previous_line( text_t *txt )
197 if( txt->i_line > 0 )
202 static void text_rewind( text_t *txt )
207 static int sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
208 static int sub_SubRipRead ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
209 static int sub_SSARead ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
210 static int sub_Vplayer ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
211 static int sub_Sami ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
212 static int sub_VobSub ( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe );
219 int (*pf_read_subtitle) ( subtitle_demux_t *, text_t *, subtitle_t*, mtime_t );
220 } sub_read_subtitle_function [] =
222 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", sub_MicroDvdRead },
223 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", sub_SubRipRead },
224 { "ssa1", SUB_TYPE_SSA1, "SSA-1", sub_SSARead },
225 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4",sub_SSARead },
226 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", sub_Vplayer },
227 { "sami", SUB_TYPE_SAMI, "SAMI", sub_Sami },
228 { "vobsub", SUB_TYPE_VOBSUB, "VobSub", sub_VobSub },
229 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
232 static char * local_stristr( char *psz_big, char *psz_little)
234 char *p_pos = psz_big;
236 if (!psz_big || !psz_little || !*psz_little) return psz_big;
240 if (toupper(*p_pos) == toupper(*psz_little))
242 char * psz_cur1 = p_pos + 1;
243 char * psz_cur2 = psz_little + 1;
244 while (*psz_cur1 && *psz_cur2 && toupper(*psz_cur1) == toupper(*psz_cur2))
249 if (!*psz_cur2) return p_pos;
256 /*****************************************************************************
257 * sub_open: Open a subtitle file and add subtitle ES
258 *****************************************************************************/
259 static int sub_open ( subtitle_demux_t *p_sub,
260 input_thread_t *p_input,
262 mtime_t i_microsecperframe,
273 int (*pf_read_subtitle)( subtitle_demux_t *, text_t *, subtitle_t *, mtime_t ) = NULL;
275 p_sub->i_sub_type = SUB_TYPE_UNKNOWN;
277 p_sub->i_subtitles = 0;
278 p_sub->subtitle = NULL;
279 p_sub->p_input = p_input;
281 if( !psz_name || !*psz_name )
283 msg_Err( p_sub, "no subtitle file specified" );
284 if( psz_name ) free( psz_name );
288 /* *** load the file *** */
289 if( text_load( &txt, psz_name ) )
291 msg_Err( p_sub, "cannot open `%s' subtitle file", psz_name );
296 msg_Dbg( p_sub, "opened `%s'", psz_name );
297 psz_vobname = strdup( psz_name );
300 var_Get( p_sub, "sub-fps", &val );
301 if( val.i_int >= 1.0 )
303 i_microsecperframe = (mtime_t)( (float)1000000 / val.f_float );
305 else if( val.f_float <= 0 )
307 i_microsecperframe = 40000; /* default: 25fps */
310 var_Get( p_sub, "sub-type", &val);
311 if( val.psz_string && *val.psz_string )
317 if( sub_read_subtitle_function[i].psz_type_name == NULL )
319 i_sub_type = SUB_TYPE_UNKNOWN;
322 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
325 i_sub_type = sub_read_subtitle_function[i].i_type;
332 i_sub_type = SUB_TYPE_UNKNOWN;
334 FREE( val.psz_string );
336 /* *** Now try to autodetect subtitle format *** */
337 if( i_sub_type == SUB_TYPE_UNKNOWN )
342 msg_Dbg( p_input, "trying to autodetect file format" );
343 for( i_try = 0; i_try < MAX_TRY; i_try++ )
347 if( ( s = text_get_line( &txt ) ) == NULL )
352 if( local_stristr( s, "<SAMI>" ) )
354 i_sub_type = SUB_TYPE_SAMI;
357 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
358 sscanf( s, "{%d}{}", &i_dummy ) == 1)
360 i_sub_type = SUB_TYPE_MICRODVD;
364 "%d:%d:%d,%d --> %d:%d:%d,%d",
365 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
366 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
368 i_sub_type = SUB_TYPE_SUBRIP;
372 "!: This is a Sub Station Alpha v%d.x script.",
377 i_sub_type = SUB_TYPE_SSA1;
381 i_sub_type = SUB_TYPE_SSA2_4; /* I hope this will work */
385 else if( local_stristr( s, "This is a Sub Station Alpha v4 script" ) )
387 i_sub_type = SUB_TYPE_SSA2_4; /* I hope this will work */
390 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
392 i_sub_type = SUB_TYPE_SSA2_4; /* could be wrong */
395 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
396 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
398 i_sub_type = SUB_TYPE_VPLAYER;
401 else if( local_stristr( s, "# VobSub index file" ) )
403 i_sub_type = SUB_TYPE_VOBSUB;
411 /* *** Load this file in memory *** */
414 if( sub_read_subtitle_function[i].i_type == SUB_TYPE_UNKNOWN )
416 msg_Dbg( p_input, "unknown subtitle file" );
421 if( sub_read_subtitle_function[i].i_type == i_sub_type )
423 msg_Dbg( p_input, "detected %s format",
424 sub_read_subtitle_function[i].psz_name );
425 p_sub->i_sub_type = i_sub_type;
426 pf_read_subtitle = sub_read_subtitle_function[i].pf_read_subtitle;
433 if( p_sub->i_subtitles >= i_max )
436 if( p_sub->subtitle )
438 if( !( p_sub->subtitle = realloc( p_sub->subtitle,
439 sizeof( subtitle_t ) * i_max ) ) )
441 msg_Err( p_sub, "out of memory");
447 if( !( p_sub->subtitle = malloc( sizeof( subtitle_t ) * i_max ) ) )
449 msg_Err( p_sub, "out of memory");
454 if( pf_read_subtitle( p_sub, &txt,
455 p_sub->subtitle + p_sub->i_subtitles,
456 i_microsecperframe ) < 0 )
460 p_sub->i_subtitles++;
462 msg_Dbg( p_sub, "loaded %d subtitles", p_sub->i_subtitles );
464 /* *** Close the file *** */
467 /* *** fix subtitle (order and time) *** */
468 p_sub->i_subtitle = 0; /* will be modified by sub_fix */
469 if( p_sub->i_sub_type != SUB_TYPE_VOBSUB )
474 /* *** add subtitle ES *** */
475 if( p_sub->i_sub_type == SUB_TYPE_VOBSUB )
477 int i_len = strlen( psz_vobname );
478 char *extension = psz_vobname + i_len - 4;
480 strcpy( extension, ".sub" );
483 if( !( p_sub->p_vobsub_file = fopen( psz_vobname, "rb" ) ) )
485 msg_Err( p_sub, "couldn't open .sub Vobsub file: %s", psz_vobname );
487 if( psz_vobname ) free( psz_vobname );
489 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
491 else if( p_sub->i_sub_type == SUB_TYPE_SSA1 ||
492 p_sub->i_sub_type == SUB_TYPE_SSA2_4 )
494 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
498 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
500 if( p_sub->psz_header != NULL )
502 fmt.i_extra = strlen( p_sub->psz_header ) + 1;
503 fmt.p_extra = strdup( p_sub->psz_header );
505 p_sub->p_es = es_out_Add( p_input->p_es_out, &fmt );
506 p_sub->i_previously_selected = 0;
511 /*****************************************************************************
512 * sub_demux: Send subtitle to decoder until i_maxdate
513 *****************************************************************************/
514 static int sub_demux( subtitle_demux_t *p_sub, mtime_t i_maxdate )
516 input_thread_t *p_input = p_sub->p_input;
519 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_sub->p_es, &b );
520 if( b && !p_sub->i_previously_selected )
522 p_sub->i_previously_selected = 1;
523 p_sub->pf_seek( p_sub, i_maxdate );
526 else if( !b && p_sub->i_previously_selected )
528 p_sub->i_previously_selected = 0;
532 while( p_sub->i_subtitle < p_sub->i_subtitles &&
533 p_sub->subtitle[p_sub->i_subtitle].i_start < i_maxdate )
538 i_len = strlen( p_sub->subtitle[p_sub->i_subtitle].psz_text ) + 1;
547 if( !( p_block = block_New( p_sub->p_input, i_len ) ) )
554 input_ClockGetTS( p_sub->p_input,
555 p_sub->p_input->stream.p_selected_program,
556 p_sub->subtitle[p_sub->i_subtitle].i_start*9/100);
557 if( p_sub->subtitle[p_sub->i_subtitle].i_stop > 0 )
560 * i_dts means end of display...
563 input_ClockGetTS( p_sub->p_input,
564 p_sub->p_input->stream.p_selected_program,
565 p_sub->subtitle[p_sub->i_subtitle].i_stop *9/100);
572 memcpy( p_block->p_buffer,
573 p_sub->subtitle[p_sub->i_subtitle].psz_text, i_len );
575 if( p_block->i_pts > 0 )
577 es_out_Send( p_input->p_es_out, p_sub->p_es, p_block );
581 block_Release( p_block );
589 /*****************************************************************************
590 * sub_seek: Seek to i_date
591 *****************************************************************************/
592 static int sub_seek ( subtitle_demux_t *p_sub, mtime_t i_date )
594 /* should be fast enough... */
595 p_sub->i_subtitle = 0;
596 while( p_sub->i_subtitle < p_sub->i_subtitles &&
597 p_sub->subtitle[p_sub->i_subtitle].i_start < i_date )
604 /*****************************************************************************
605 * sub_close: Close subtitle demux
606 *****************************************************************************/
607 static void sub_close( subtitle_demux_t *p_sub )
609 if( p_sub->subtitle )
612 for( i = 0; i < p_sub->i_subtitles; i++ )
614 if( p_sub->subtitle[i].psz_text )
616 free( p_sub->subtitle[i].psz_text );
619 free( p_sub->subtitle );
621 if( p_sub->p_vobsub_file )
623 fclose( p_sub->p_vobsub_file );
627 /*****************************************************************************
628 * sub_fix: fix time stamp and order of subtitle
629 *****************************************************************************/
630 static void sub_fix( subtitle_demux_t *p_sub )
638 /* *** fix order (to be sure...) *** */
639 /* We suppose that there are near in order and this durty bubble sort
640 * wont take too much time
645 for( i_index = 1; i_index < p_sub->i_subtitles; i_index++ )
647 if( p_sub->subtitle[i_index].i_start <
648 p_sub->subtitle[i_index - 1].i_start )
652 p_sub->subtitle + i_index - 1,
653 sizeof( subtitle_t ) );
654 memcpy( p_sub->subtitle + i_index - 1,
655 p_sub->subtitle + i_index,
656 sizeof( subtitle_t ) );
657 memcpy( p_sub->subtitle + i_index,
659 sizeof( subtitle_t ) );
665 /* *** and at the end add delay *** */
666 var_Get( p_sub, "sub-delay", &val );
667 i_delay = (mtime_t) val.i_int * 100000;
670 for( i = 0; i < p_sub->i_subtitles; i++ )
672 p_sub->subtitle[i].i_start += i_delay;
673 p_sub->subtitle[i].i_stop += i_delay;
674 if( p_sub->subtitle[i].i_start < 0 )
676 p_sub->i_subtitle = i + 1;
684 /*****************************************************************************
685 * Specific Subtitle function
686 *****************************************************************************/
687 static int sub_MicroDvdRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
691 * {n1}{n2}Line1|Line2|Line3....
692 * where n1 and n2 are the video frame number...
697 char buffer_text[MAX_LINE + 1];
698 unsigned int i_start;
704 if( ( s = text_get_line( txt ) ) == NULL )
706 return( VLC_EGENERIC );
711 memset( buffer_text, '\0', MAX_LINE );
712 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 ||
713 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3)
718 /* replace | by \n */
719 for( i = 0; i < strlen( buffer_text ); i++ )
721 if( buffer_text[i] == '|' )
723 buffer_text[i] = '\n';
726 p_subtitle->i_start = (mtime_t)i_start * (mtime_t)i_microsecperframe;
727 p_subtitle->i_stop = (mtime_t)i_stop * (mtime_t)i_microsecperframe;
728 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
732 static int sub_SubRipRead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
736 * h1:m1:s1,d1 --> h2:m2:s2,d2
744 char buffer_text[ 10 * MAX_LINE];
751 int h1, m1, s1, d1, h2, m2, s2, d2;
752 if( ( s = text_get_line( txt ) ) == NULL )
754 return( VLC_EGENERIC );
757 "%d:%d:%d,%d --> %d:%d:%d,%d",
759 &h2, &m2, &s2, &d2 ) == 8 )
761 i_start = ( (mtime_t)h1 * 3600*1000 +
762 (mtime_t)m1 * 60*1000 +
764 (mtime_t)d1 ) * 1000;
766 i_stop = ( (mtime_t)h2 * 3600*1000 +
767 (mtime_t)m2 * 60*1000 +
769 (mtime_t)d2 ) * 1000;
771 /* Now read text until an empty line */
772 for( i_buffer_text = 0;; )
775 if( ( s = text_get_line( txt ) ) == NULL )
777 return( VLC_EGENERIC );
783 /* empty line -> end of this subtitle */
784 buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0';
785 p_subtitle->i_start = i_start;
786 p_subtitle->i_stop = i_stop;
787 p_subtitle->psz_text = strdup( buffer_text );
792 if( i_buffer_text + i_len + 1 < 10 * MAX_LINE )
794 memcpy( buffer_text + i_buffer_text,
797 i_buffer_text += i_len;
799 buffer_text[i_buffer_text] = '\n';
809 static int sub_SSARead( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
811 char buffer_text[ 10 * MAX_LINE];
818 int h1, m1, s1, c1, h2, m2, s2, c2;
821 if( ( s = text_get_line( txt ) ) == NULL )
823 return( VLC_EGENERIC );
825 p_subtitle->psz_text = malloc( strlen( s ) );
828 "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]",
832 buffer_text ) == 10 )
834 i_start = ( (mtime_t)h1 * 3600*1000 +
835 (mtime_t)m1 * 60*1000 +
837 (mtime_t)c1 * 10 ) * 1000;
839 i_stop = ( (mtime_t)h2 * 3600*1000 +
840 (mtime_t)m2 * 60*1000 +
842 (mtime_t)c2 * 10 ) * 1000;
844 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
845 if( p_sub->i_sub_type == SUB_TYPE_SSA1 )
847 sprintf( p_subtitle->psz_text, ",%d%s", i_dummy, strdup( buffer_text) );
851 sprintf( p_subtitle->psz_text, ",%d,%s", i_dummy, strdup( buffer_text) );
853 p_subtitle->i_start = i_start;
854 p_subtitle->i_stop = i_stop;
859 /* All the other stuff we add to the header field */
860 if( p_sub->psz_header != NULL )
862 if( !( p_sub->psz_header = realloc( p_sub->psz_header,
863 strlen( p_sub->psz_header ) + strlen( s ) + 2 ) ) )
865 msg_Err( p_sub, "out of memory");
868 p_sub->psz_header = strcat( p_sub->psz_header, strdup( s ) );
869 p_sub->psz_header = strcat( p_sub->psz_header, "\n" );
873 if( !( p_sub->psz_header = malloc( strlen( s ) + 2 ) ) )
875 msg_Err( p_sub, "out of memory");
878 p_sub->psz_header = strdup( s );
879 p_sub->psz_header = strcat( p_sub->psz_header, "\n" );
885 static int sub_Vplayer( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
889 * h:m:s:Line1|Line2|Line3....
891 * h:m:s Line1|Line2|Line3....
892 * where n1 and n2 are the video frame number...
896 char buffer_text[MAX_LINE + 1];
905 if( ( p = text_get_line( txt ) ) == NULL )
907 return( VLC_EGENERIC );
912 memset( buffer_text, '\0', MAX_LINE );
913 if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 )
915 i_start = ( (mtime_t)h * 3600*1000 +
916 (mtime_t)m * 60*1000 +
917 (mtime_t)s * 1000 ) * 1000;
922 /* replace | by \n */
923 for( i = 0; i < strlen( buffer_text ); i++ )
925 if( buffer_text[i] == '|' )
927 buffer_text[i] = '\n';
930 p_subtitle->i_start = i_start;
932 p_subtitle->i_stop = 0;
933 p_subtitle->psz_text = strndup( buffer_text, MAX_LINE );
937 static char *sub_SamiSearch( text_t *txt, char *psz_start, char *psz_str )
941 if( local_stristr( psz_start, psz_str ) )
943 char *s = local_stristr( psz_start, psz_str );
945 s += strlen( psz_str );
953 if( ( p = text_get_line( txt ) ) == NULL )
957 if( local_stristr( p, psz_str ) )
959 char *s = local_stristr( p, psz_str );
961 s += strlen( psz_str );
968 static int sub_Sami( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe )
974 char buffer_text[10*MAX_LINE + 1];
976 if( i_text < 10*MAX_LINE ) \
978 buffer_text[i_text++] = c; \
979 buffer_text[i_text] = '\0'; \
982 /* search "Start=" */
983 if( !( p = sub_SamiSearch( txt, NULL, "Start=" ) ) )
988 /* get start value */
989 i_start = strtol( p, &p, 0 );
992 if( !( p = sub_SamiSearch( txt, p, "<P" ) ) )
997 if( !( p = sub_SamiSearch( txt, p, ">" ) ) )
1003 buffer_text[0] = '\0';
1004 /* now get all txt until a "Start=" line */
1011 if( !strncasecmp( p, "<br", 3 ) )
1015 else if( local_stristr( p, "Start=" ) )
1017 text_previous_line( txt );
1020 p = sub_SamiSearch( txt, p, ">" );
1022 else if( !strncmp( p, " ", 6 ) )
1027 else if( *p == '\t' )
1040 p = text_get_line( txt );
1049 p_subtitle->i_start = i_start * 1000;
1050 p_subtitle->i_stop = 0;
1051 p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE );
1053 return( VLC_SUCCESS );
1057 static int sub_VobSub( subtitle_demux_t *p_sub, text_t *txt, subtitle_t *p_subtitle, mtime_t i_microsecperframe)
1060 * Parse the idx file. Each line:
1061 * timestamp: hh:mm:ss:mss, filepos: loc
1062 * hexint is the hex location of the vobsub in the .sub file
1067 char buffer_text[MAX_LINE + 1];
1068 unsigned int i_start, i_location;
1072 unsigned int h, m, s, ms, loc;
1074 if( ( p = text_get_line( txt ) ) == NULL )
1076 return( VLC_EGENERIC );
1080 memset( buffer_text, '\0', MAX_LINE );
1081 if( sscanf( p, "timestamp: %d:%d:%d:%d, filepos: %x%[^\r\n]",
1082 &h, &m, &s, &ms, &loc, buffer_text ) == 5 )
1084 i_start = ( (mtime_t)h * 3600*1000 +
1085 (mtime_t)m * 60*1000 +
1087 (mtime_t)ms ) * 1000;
1092 p_subtitle->i_start = (mtime_t)i_start;
1093 p_subtitle->i_stop = 0;
1094 p_subtitle->i_vobsub_location = i_location;
1095 fprintf( stderr, "time: %x, location: %x\n", i_start, i_location );
1100 #define mpeg_read(buf, num) _mpeg_read(srcbuf, size, srcpos, buf, num)
1101 static unsigned int _mpeg_read(unsigned char *srcbufunsigned int, unsigned int size,
1102 unsigned int &srcpos, unsigned char *dstbuf,
1104 unsigned int real_num;
1106 if ((srcpos + num) >= size)
1107 real_num = size - srcpos;
1110 memcpy(dstbuf, &srcbuf[srcpos], real_num);
1116 #define mpeg_getc() _mpeg_getch(srcbuf, size, srcpos)
1117 static int _mpeg_getch(unsigned char *srcbuf, unsigned int size,
1118 unsigned int &srcpos) {
1121 if (mpeg_read(&c, 1) != 1)
1126 #define mpeg_seek(b, w) _mpeg_seek(size, srcpos, b, w)
1127 static int _mpeg_seek(unsigned int size, unsigned int &srcpos, unsigned int num,
1129 unsigned int new_pos;
1131 if (whence == SEEK_SET)
1133 else if (whence == SEEK_CUR)
1134 new_pos = srcpos + num;
1138 if (new_pos >= size) {
1147 #define mpeg_tell() srcpos
1150 static int mpeg_run(demuxer_t *demuxer, unsigned char *srcbuf, unsigned int size) {
1151 unsigned int len, idx, version, srcpos, packet_size;
1154 /* Goto start of a packet, it starts with 0x000001?? */
1155 /*const unsigned char wanted[] = { 0, 0, 1 };
1156 unsigned char buf[5];
1159 mkv_demuxer_t *mkv_d;
1161 mkv_d = (mkv_demuxer_t *)demuxer->priv;
1167 if (mpeg_read(buf, 4) != 4)
1169 while (memcmp(buf, wanted, sizeof(wanted)) != 0) {
1173 memmove(buf, buf + 1, 3);
1177 case 0xb9: */ /* System End Code */
1181 case 0xba: */ /* Packet start code */
1185 if ((c & 0xc0) == 0x40)
1187 else if ((c & 0xf0) == 0x20)
1190 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub: Unsupported MPEG "
1191 "version: 0x%02x\n", c);
1196 if (mpeg_seek(9, SEEK_CUR))
1198 } else if (version == 2) {
1199 if (mpeg_seek(7, SEEK_CUR))
1205 case 0xbd: */ /* packet */
1206 /* if (mpeg_read(buf, 2) != 2)
1208 len = buf[0] << 8 | buf[1];
1213 if ((c & 0xC0) == 0x40) {*/ /* skip STD scale & size */
1214 /* if (mpeg_getc() < 0)
1220 if ((c & 0xf0) == 0x20) { */ /* System-1 stream timestamp */
1221 /* Do we need this? */
1223 } else if ((c & 0xf0) == 0x30) {
1224 */ /* Do we need this? */
1226 } else if ((c & 0xc0) == 0x80) {*/ /* System-2 (.VOB) stream */
1227 /* unsigned int pts_flags, hdrlen, dataidx;
1236 dataidx = mpeg_tell() + hdrlen;
1237 if (dataidx > idx + len) {
1238 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub: Invalid header "
1239 "length: %d (total length: %d, idx: %d, dataidx: %d)\n",
1240 hdrlen, len, idx, dataidx);
1243 if ((pts_flags & 0xc0) == 0x80) {
1244 if (mpeg_read(buf, 5) != 5)
1246 if (!(((buf[0] & 0xf0) == 0x20) && (buf[0] & 1) && (buf[2] & 1) &&
1248 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub PTS error: 0x%02x "
1249 "%02x%02x %02x%02x \n",
1250 buf[0], buf[1], buf[2], buf[3], buf[4]);
1253 pts = ((buf[0] & 0x0e) << 29 | buf[1] << 22 |
1254 (buf[2] & 0xfe) << 14 | buf[3] << 7 | (buf[4] >> 1));
1255 } else*/ /* if ((pts_flags & 0xc0) == 0xc0) */// {
1259 mpeg_seek(dataidx, SEEK_SET);
1262 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub: Bogus aid %d\n", aid);
1265 packet_size = len - ((unsigned int)mpeg_tell() - idx);
1267 dp = new_demux_packet(packet_size);
1269 dp->pts = mkv_d->last_pts;
1270 if (mpeg_read(dp->buffer, packet_size) != packet_size) {
1271 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub: mpeg_read failure");
1275 ds_add_packet(ds, dp);
1280 case 0xbe: */ /* Padding */
1281 /* if (mpeg_read(buf, 2) != 2)
1283 len = buf[0] << 8 | buf[1];
1284 if ((len > 0) && mpeg_seek(len, SEEK_CUR))
1289 if ((0xc0 <= buf[3]) && (buf[3] < 0xf0)) {
1290 */ /* MPEG audio or video */
1291 /* if (mpeg_read(buf, 2) != 2)
1293 len = (buf[0] << 8) | buf[1];
1294 if ((len > 0) && mpeg_seek(len, SEEK_CUR))
1299 mp_msg(MSGT_DEMUX, MSGL_ERR, "[mkv] VobSub: unknown header "
1300 "0x%02X%02X%02X%02X\n", buf[0], buf[1], buf[2], buf[3]);