1 /*****************************************************************************
2 * subtitle.c: Demux for subtitle text files.
3 *****************************************************************************
4 * Copyright (C) 1999-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <vlc_input.h>
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
38 #include <vlc_demux.h>
39 #include <vlc_charset.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t *p_this );
45 static void Close( vlc_object_t *p_this );
47 #define SUB_DELAY_LONGTEXT \
48 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
49 #define SUB_FPS_LONGTEXT \
50 N_("Override the normal frames per second settings. " \
51 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
52 #define SUB_TYPE_LONGTEXT \
53 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
54 "\"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
55 "\"sami\", \"dvdsubtitle\", \"mpl2\" and \"auto\" (meaning autodetection, this " \
56 "should always work).")
57 static const char *ppsz_sub_type[] =
59 "auto", "microdvd", "subrip", "subviewer", "ssa1",
60 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2"
64 set_shortname( _("Subtitles"));
65 set_description( _("Text subtitles parser") );
66 set_capability( "demux2", 0 );
67 set_category( CAT_INPUT );
68 set_subcategory( SUBCAT_INPUT_DEMUX );
69 add_float( "sub-fps", 0.0, NULL,
70 N_("Frames per second"),
71 SUB_FPS_LONGTEXT, VLC_TRUE );
72 add_integer( "sub-delay", 0, NULL,
73 N_("Subtitles delay"),
74 SUB_DELAY_LONGTEXT, VLC_TRUE );
75 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
76 SUB_TYPE_LONGTEXT, VLC_TRUE );
77 change_string_list( ppsz_sub_type, NULL, NULL );
78 set_callbacks( Open, Close );
80 add_shortcut( "subtitle" );
83 /*****************************************************************************
85 *****************************************************************************/
88 SUB_TYPE_UNKNOWN = -1,
107 static int TextLoad( text_t *, stream_t *s );
108 static void TextUnload( text_t * );
125 int64_t i_next_demux_date;
126 int64_t i_microsecperframe;
131 subtitle_t *subtitle;
136 static int ParseMicroDvd ( demux_t *, subtitle_t * );
137 static int ParseSubRip ( demux_t *, subtitle_t * );
138 static int ParseSubViewer ( demux_t *, subtitle_t * );
139 static int ParseSSA ( demux_t *, subtitle_t * );
140 static int ParseVplayer ( demux_t *, subtitle_t * );
141 static int ParseSami ( demux_t *, subtitle_t * );
142 static int ParseDVDSubtitle( demux_t *, subtitle_t * );
143 static int ParseMPL2 ( demux_t *, subtitle_t * );
147 const char *psz_type_name;
149 const char *psz_name;
150 int (*pf_read)( demux_t *, subtitle_t* );
151 } sub_read_subtitle_function [] =
153 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
154 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
155 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
156 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
157 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
158 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
159 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
160 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
161 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
162 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
163 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
166 static int Demux( demux_t * );
167 static int Control( demux_t *, int, va_list );
169 /*static void Fix( demux_t * );*/
171 /*****************************************************************************
173 *****************************************************************************/
174 static int Open ( vlc_object_t *p_this )
176 demux_t *p_demux = (demux_t*)p_this;
181 int (*pf_read)( demux_t *, subtitle_t* );
184 if( !p_demux->b_force )
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->i_microsecperframe = 40000;
200 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
202 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
204 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
206 /* Check for override of the fps */
207 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
210 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
211 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
214 /* Get or probe the type */
215 p_sys->i_type = SUB_TYPE_UNKNOWN;
216 psz_type = var_CreateGetString( p_demux, "sub-type" );
223 if( sub_read_subtitle_function[i].psz_type_name == NULL )
226 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
229 p_sys->i_type = sub_read_subtitle_function[i].i_type;
236 /* Probe if unknown type */
237 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
242 msg_Dbg( p_demux, "autodetecting subtitle format" );
243 for( i_try = 0; i_try < 256; i_try++ )
247 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
250 if( strcasestr( s, "<SAMI>" ) )
252 p_sys->i_type = SUB_TYPE_SAMI;
255 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
256 sscanf( s, "{%d}{}", &i_dummy ) == 1)
258 p_sys->i_type = SUB_TYPE_MICRODVD;
262 "%d:%d:%d,%d --> %d:%d:%d,%d",
263 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
264 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
266 p_sys->i_type = SUB_TYPE_SUBRIP;
269 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
271 p_sys->i_type = SUB_TYPE_SSA1;
274 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
276 p_sys->i_type = SUB_TYPE_ASS;
279 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
281 p_sys->i_type = SUB_TYPE_SSA2_4;
284 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
286 p_sys->i_type = SUB_TYPE_SSA2_4;
289 else if( !strncasecmp( s, "Dialogue:", 9 ) )
291 p_sys->i_type = SUB_TYPE_ASS;
294 else if( strcasestr( s, "[INFORMATION]" ) )
296 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
299 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
300 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
302 p_sys->i_type = SUB_TYPE_VPLAYER;
305 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
306 &i_dummy, &i_dummy ) == 4 )
308 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
311 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
312 sscanf( s, "[%d][]", &i_dummy ) == 1)
314 p_sys->i_type = SUB_TYPE_MPL2;
324 /* It will nearly always work even for non seekable stream thanks the
325 * caching system, and if it fails we lose just a few sub */
326 if( stream_Seek( p_demux->s, 0 ) )
328 msg_Warn( p_demux, "failed to rewind" );
331 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
333 msg_Err( p_demux, "failed to recognize subtitle type" );
340 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
342 msg_Dbg( p_demux, "detected %s format",
343 sub_read_subtitle_function[i].psz_name );
344 pf_read = sub_read_subtitle_function[i].pf_read;
349 msg_Dbg( p_demux, "loading all subtitles..." );
351 /* Load the whole file */
352 TextLoad( &p_sys->txt, p_demux->s );
357 if( p_sys->i_subtitles >= i_max )
360 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
361 sizeof(subtitle_t) * i_max ) ) )
363 msg_Err( p_demux, "out of memory");
364 if( p_sys->subtitle != NULL )
365 free( p_sys->subtitle );
366 TextUnload( &p_sys->txt );
372 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) )
375 p_sys->i_subtitles++;
378 TextUnload( &p_sys->txt );
380 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
382 /* Fix subtitle (order and time) *** */
383 p_sys->i_subtitle = 0;
385 if( p_sys->i_subtitles > 0 )
387 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
389 if( p_sys->i_length <= 0 )
390 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
393 /* *** add subtitle ES *** */
394 if( p_sys->i_type == SUB_TYPE_SSA1 ||
395 p_sys->i_type == SUB_TYPE_SSA2_4 ||
396 p_sys->i_type == SUB_TYPE_ASS )
398 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
402 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
404 if( p_sys->psz_header != NULL )
406 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
407 fmt.p_extra = strdup( p_sys->psz_header );
409 p_sys->es = es_out_Add( p_demux->out, &fmt );
414 /*****************************************************************************
415 * Close: Close subtitle demux
416 *****************************************************************************/
417 static void Close( vlc_object_t *p_this )
419 demux_t *p_demux = (demux_t*)p_this;
420 demux_sys_t *p_sys = p_demux->p_sys;
423 for( i = 0; i < p_sys->i_subtitles; i++ )
425 if( p_sys->subtitle[i].psz_text )
426 free( p_sys->subtitle[i].psz_text );
428 if( p_sys->subtitle )
429 free( p_sys->subtitle );
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 )
468 if( p_sys->i_subtitle >= p_sys->i_subtitles )
472 case DEMUX_GET_POSITION:
473 pf = (double*)va_arg( args, double * );
474 if( p_sys->i_subtitle >= p_sys->i_subtitles )
478 else if( p_sys->i_subtitles > 0 )
480 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
481 (double)p_sys->i_length;
489 case DEMUX_SET_POSITION:
490 f = (double)va_arg( args, double );
491 i64 = f * p_sys->i_length;
493 p_sys->i_subtitle = 0;
494 while( p_sys->i_subtitle < p_sys->i_subtitles &&
495 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
499 if( p_sys->i_subtitle >= p_sys->i_subtitles )
503 case DEMUX_SET_NEXT_DEMUX_TIME:
504 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
509 case DEMUX_GET_ATTACHMENTS:
510 case DEMUX_GET_TITLE_INFO:
511 case DEMUX_HAS_UNSUPPORTED_META:
515 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
520 /*****************************************************************************
521 * Demux: Send subtitle to decoder
522 *****************************************************************************/
523 static int Demux( demux_t *p_demux )
525 demux_sys_t *p_sys = p_demux->p_sys;
528 if( p_sys->i_subtitle >= p_sys->i_subtitles )
531 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
532 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
534 /* Should not happen */
535 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
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;
551 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
557 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
563 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
564 p_block->i_dts = p_block->i_pts;
565 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
568 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
571 memcpy( p_block->p_buffer,
572 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
573 if( p_block->i_pts > 0 )
575 es_out_Send( p_demux->out, p_sys->es, p_block );
579 block_Release( p_block );
585 p_sys->i_next_demux_date = 0;
590 /*****************************************************************************
591 * Fix: fix time stamp and order of subtitle
592 *****************************************************************************/
593 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
594 static void Fix( demux_t *p_demux )
596 demux_sys_t *p_sys = p_demux->p_sys;
600 /* *** fix order (to be sure...) *** */
601 /* We suppose that there are near in order and this durty bubble sort
602 * wont take too much time
607 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
609 if( p_sys->subtitle[i_index].i_start <
610 p_sys->subtitle[i_index - 1].i_start )
614 p_sys->subtitle + i_index - 1,
615 sizeof( subtitle_t ) );
616 memcpy( p_sys->subtitle + i_index - 1,
617 p_sys->subtitle + i_index,
618 sizeof( subtitle_t ) );
619 memcpy( p_sys->subtitle + i_index,
621 sizeof( subtitle_t ) );
629 static int TextLoad( text_t *txt, stream_t *s )
635 txt->i_line_count = 0;
637 txt->line = calloc( i_line_max, sizeof( char * ) );
639 /* load the complete file */
642 char *psz = stream_ReadLine( s );
647 txt->line[txt->i_line_count++] = psz;
648 if( txt->i_line_count >= i_line_max )
651 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
655 if( txt->i_line_count <= 0 )
663 static void TextUnload( text_t *txt )
667 for( i = 0; i < txt->i_line_count; i++ )
669 free( txt->line[i] );
673 txt->i_line_count = 0;
676 static char *TextGetLine( text_t *txt )
678 if( txt->i_line >= txt->i_line_count )
681 return txt->line[txt->i_line++];
683 static void TextPreviousLine( text_t *txt )
685 if( txt->i_line > 0 )
689 /*****************************************************************************
690 * Specific Subtitle function
691 *****************************************************************************/
694 * {n1}{n2}Line1|Line2|Line3....
695 * where n1 and n2 are the video frame number (n2 can be empty)
697 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle )
699 demux_sys_t *p_sys = p_demux->p_sys;
700 text_t *txt = &p_sys->txt;
708 const char *s = TextGetLine( txt );
712 psz_text = malloc( strlen(s) + 1 );
718 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
719 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
722 if( i_start != 1 || i_stop != 1 )
725 /* We found a possible setting of the framerate "{1}{1}23.976" */
726 /* Check if it's usable, and if the sub-fps is not set */
727 f_fps = us_strtod( psz_text, NULL );
728 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
729 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
734 /* replace | by \n */
735 for( i = 0; psz_text[i] != '\0'; i++ )
737 if( psz_text[i] == '|' )
742 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
743 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
744 p_subtitle->psz_text = psz_text;
748 /* ParseSubRipSubViewer
751 * h1:m1:s1,d1 --> h2:m2:s2,d2
756 * Format SubViewer v1/v2
757 * h1:m1:s1.d1,h2:m2:s2.d2
762 * We ignore line number for SubRip
764 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
766 vlc_bool_t b_replace_br )
768 demux_sys_t *p_sys = p_demux->p_sys;
769 text_t *txt = &p_sys->txt;
774 const char *s = TextGetLine( txt );
775 int h1, m1, s1, d1, h2, m2, s2, d2;
780 if( sscanf( s, psz_fmt,
782 &h2, &m2, &s2, &d2 ) == 8 )
784 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
785 (int64_t)m1 * 60*1000 +
787 (int64_t)d1 ) * 1000;
789 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
790 (int64_t)m2 * 60*1000 +
792 (int64_t)d2 ) * 1000;
797 /* Now read text until an empty line */
798 psz_text = strdup("");
803 const char *s = TextGetLine( txt );
816 p_subtitle->psz_text = psz_text;
820 i_old = strlen( psz_text );
821 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
824 strcat( psz_text, s );
825 strcat( psz_text, "\n" );
827 /* replace [br] by \n */
832 while( ( p = strstr( psz_text, "[br]" ) ) )
835 memmove( p, &p[3], strlen(&p[3])+1 );
842 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle )
844 return ParseSubRipSubViewer( p_demux, p_subtitle,
845 "%d:%d:%d,%d --> %d:%d:%d,%d",
850 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle )
852 return ParseSubRipSubViewer( p_demux, p_subtitle,
853 "%d:%d:%d.%d,%d:%d:%d.%d",
859 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle )
861 demux_sys_t *p_sys = p_demux->p_sys;
862 text_t *txt = &p_sys->txt;
866 const char *s = TextGetLine( txt );
867 int h1, m1, s1, c1, h2, m2, s2, c2;
873 /* We expect (SSA2-4):
874 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
875 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
877 * SSA-1 is similar but only has 8 commas up untill the subtitle text. Probably the Effect field is no present, but not 100 % sure.
881 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
882 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
884 psz_text = malloc( 2 + strlen( s ) + 1 );
889 "Dialogue: %*[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
894 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
895 /* (Layer comes from ASS specs ... it's empty for SSA.) */
896 if( p_sys->i_type == SUB_TYPE_SSA1 )
898 /* SSA1 has only 8 commas before the text starts, not 9 */
899 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
904 /* ReadOrder, Layer, %s(rest of fields) */
905 memmove( &psz_text[2], psz_text, strlen(psz_text)+1 );
910 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
911 (int64_t)m1 * 60*1000 +
913 (int64_t)c1 * 10 ) * 1000;
914 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
915 (int64_t)m2 * 60*1000 +
917 (int64_t)c2 * 10 ) * 1000;
918 p_subtitle->psz_text = psz_text;
923 /* All the other stuff we add to the header field */
924 if( !p_sys->psz_header )
925 p_sys->psz_header = strdup( "" );
926 if( !p_sys->psz_header )
930 realloc( p_sys->psz_header,
931 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
932 strcat( p_sys->psz_header, s );
933 strcat( p_sys->psz_header, "\n" );
939 * h:m:s:Line1|Line2|Line3....
941 * h:m:s Line1|Line2|Line3....
943 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle )
945 demux_sys_t *p_sys = p_demux->p_sys;
946 text_t *txt = &p_sys->txt;
952 const char *s = TextGetLine( txt );
958 psz_text = malloc( strlen( s ) + 1 );
962 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
963 &h1, &m1, &s1, psz_text ) == 4 )
965 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
966 (int64_t)m1 * 60*1000 +
967 (int64_t)s1 * 1000 ) * 1000;
968 p_subtitle->i_stop = 0;
974 /* replace | by \n */
975 for( i = 0; psz_text[i] != '\0'; i++ )
977 if( psz_text[i] == '|' )
980 p_subtitle->psz_text = psz_text;
986 static char *ParseSamiSearch( text_t *txt,
987 char *psz_start, const char *psz_str )
989 if( psz_start && strcasestr( psz_start, psz_str ) )
991 char *s = strcasestr( psz_start, psz_str );
992 return &s[strlen( psz_str )];
997 char *p = TextGetLine( txt );
1001 if( strcasestr( p, psz_str ) )
1003 char *s = strcasestr( p, psz_str );
1004 return &s[strlen( psz_str )];
1008 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle )
1010 demux_sys_t *p_sys = p_demux->p_sys;
1011 text_t *txt = &p_sys->txt;
1016 unsigned int i_text;
1017 char text[8192]; /* Arbitrary but should be long enough */
1019 /* search "Start=" */
1020 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1021 return VLC_EGENERIC;
1023 /* get start value */
1024 i_start = strtol( s, &s, 0 );
1027 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1028 return VLC_EGENERIC;
1031 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1032 return VLC_EGENERIC;
1036 /* now get all txt until a "Start=" line */
1040 /* Search non empty line */
1041 while( s && *s == '\0' )
1042 s = TextGetLine( txt );
1048 if( !strncasecmp( s, "<br", 3 ) )
1052 else if( strcasestr( s, "Start=" ) )
1054 TextPreviousLine( txt );
1057 s = ParseSamiSearch( txt, s, ">" );
1059 else if( !strncmp( s, " ", 6 ) )
1064 else if( *s == '\t' )
1074 if( c != '\0' && i_text+1 < sizeof(text) )
1077 text[i_text] = '\0';
1081 p_subtitle->i_start = i_start * 1000;
1082 p_subtitle->i_stop = 0;
1083 p_subtitle->psz_text = strdup( text );
1095 * TODO it can have a header
1102 * LANG support would be cool
1103 * CODEPAGE is probably mandatory FIXME
1105 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle )
1107 demux_sys_t *p_sys = p_demux->p_sys;
1108 text_t *txt = &p_sys->txt;
1113 const char *s = TextGetLine( txt );
1117 return VLC_EGENERIC;
1121 &h1, &m1, &s1, &c1 ) == 4 )
1123 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1124 (int64_t)m1 * 60*1000 +
1125 (int64_t)s1 * 1000 +
1126 (int64_t)c1 * 10) * 1000;
1127 p_subtitle->i_stop = 0;
1132 /* Now read text until a line containing "}" */
1133 psz_text = strdup("");
1138 const char *s = TextGetLine( txt );
1145 return VLC_EGENERIC;
1148 i_len = strlen( s );
1149 if( i_len == 1 && s[0] == '}')
1151 p_subtitle->psz_text = psz_text;
1155 i_old = strlen( psz_text );
1156 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1159 strcat( psz_text, s );
1160 strcat( psz_text, "\n" );
1166 * [n1][n2]Line1|Line2|Line3...
1167 * where n1 and n2 are the video frame number (n2 can be empty)
1169 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle )
1171 demux_sys_t *p_sys = p_demux->p_sys;
1172 text_t *txt = &p_sys->txt;
1178 const char *s = TextGetLine( txt );
1183 return VLC_EGENERIC;
1185 psz_text = malloc( strlen(s) + 1 );
1191 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1192 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1194 p_subtitle->i_start = (int64_t)i_start * 100000;
1195 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1201 /* replace | by \n */
1202 for( i = 0; psz_text[i] != '\0'; )
1204 if( psz_text[i] == '|' )
1208 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1209 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1213 p_subtitle->psz_text = psz_text;