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>
9 * Jean-Baptiste Kempf <jb@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_input.h>
37 #include <vlc_memory.h>
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
44 #include <vlc_demux.h>
45 #include <vlc_charset.h>
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t *p_this );
51 static void Close( vlc_object_t *p_this );
53 #define SUB_DELAY_LONGTEXT \
54 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
55 #define SUB_FPS_LONGTEXT \
56 N_("Override the normal frames per second settings. " \
57 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
58 #define SUB_TYPE_LONGTEXT \
59 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
60 "\"subrip\", \"subviewer\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\", " \
61 "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\", "\
62 "\"mpsub\", \"jacosub\", \"psb\", \"realtext\", \"dks\", \"subviewer1\", " \
63 " and \"auto\" (meaning autodetection, this should always work).")
65 static const char *const ppsz_sub_type[] =
67 "auto", "microdvd", "subrip", "subviewer", "ssa1",
68 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
69 "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
74 set_shortname( N_("Subtitles"))
75 set_description( N_("Text subtitles parser") )
76 set_capability( "demux", 0 )
77 set_category( CAT_INPUT )
78 set_subcategory( SUBCAT_INPUT_DEMUX )
79 add_float( "sub-fps", 0.0, NULL,
80 N_("Frames per second"),
81 SUB_FPS_LONGTEXT, true )
82 add_integer( "sub-delay", 0, NULL,
83 N_("Subtitles delay"),
84 SUB_DELAY_LONGTEXT, true )
85 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
86 SUB_TYPE_LONGTEXT, true )
87 change_string_list( ppsz_sub_type, NULL, NULL )
88 set_callbacks( Open, Close )
90 add_shortcut( "subtitle" )
93 /*****************************************************************************
95 *****************************************************************************/
98 SUB_TYPE_UNKNOWN = -1,
101 SUB_TYPE_SUBRIP_DOT, /* Invalid SubRip file (dot instead of comma) */
107 SUB_TYPE_SUBVIEWER, /* SUBVIEWER 2 */
108 SUB_TYPE_DVDSUBTITLE, /* Mplayer calls it subviewer2 */
117 SUB_TYPE_SUBVIEW1 /* SUBVIEWER 1 - mplayer calls it subrip09,
118 and Gnome subtitles SubViewer 1.0 */
128 static int TextLoad( text_t *, stream_t *s );
129 static void TextUnload( text_t * );
146 int64_t i_next_demux_date;
147 int64_t i_microsecperframe;
152 subtitle_t *subtitle;
162 int i_time_resolution;
174 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
175 static int ParseSubRip ( demux_t *, subtitle_t *, int );
176 static int ParseSubRipDot ( demux_t *, subtitle_t *, int );
177 static int ParseSubViewer ( demux_t *, subtitle_t *, int );
178 static int ParseSSA ( demux_t *, subtitle_t *, int );
179 static int ParseVplayer ( demux_t *, subtitle_t *, int );
180 static int ParseSami ( demux_t *, subtitle_t *, int );
181 static int ParseDVDSubtitle( demux_t *, subtitle_t *, int );
182 static int ParseMPL2 ( demux_t *, subtitle_t *, int );
183 static int ParseAQT ( demux_t *, subtitle_t *, int );
184 static int ParsePJS ( demux_t *, subtitle_t *, int );
185 static int ParseMPSub ( demux_t *, subtitle_t *, int );
186 static int ParseJSS ( demux_t *, subtitle_t *, int );
187 static int ParsePSB ( demux_t *, subtitle_t *, int );
188 static int ParseRealText ( demux_t *, subtitle_t *, int );
189 static int ParseDKS ( demux_t *, subtitle_t *, int );
190 static int ParseSubViewer1 ( demux_t *, subtitle_t *, int );
194 const char *psz_type_name;
196 const char *psz_name;
197 int (*pf_read)( demux_t *, subtitle_t*, int );
198 } sub_read_subtitle_function [] =
200 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
201 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
202 { "subrip-dot", SUB_TYPE_SUBRIP_DOT, "SubRIP(Dot)", ParseSubRipDot },
203 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
204 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
205 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
206 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
207 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
208 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
209 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
210 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
211 { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT },
212 { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS },
213 { "mpsub", SUB_TYPE_MPSUB, "MPSub", ParseMPSub },
214 { "jacosub", SUB_TYPE_JACOSUB, "JacoSub", ParseJSS },
215 { "psb", SUB_TYPE_PSB, "PowerDivx", ParsePSB },
216 { "realtext", SUB_TYPE_RT, "RealText", ParseRealText },
217 { "dks", SUB_TYPE_DKS, "DKS", ParseDKS },
218 { "subviewer1", SUB_TYPE_SUBVIEW1, "Subviewer 1", ParseSubViewer1 },
219 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
221 /* When adding support for more formats, be sure to add their file extension
222 * to src/input/subtitles.c to enable auto-detection.
225 static int Demux( demux_t * );
226 static int Control( demux_t *, int, va_list );
228 /*static void Fix( demux_t * );*/
230 /*****************************************************************************
232 *****************************************************************************/
233 static int Open ( vlc_object_t *p_this )
235 demux_t *p_demux = (demux_t*)p_this;
240 int (*pf_read)( demux_t *, subtitle_t*, int );
243 if( !p_demux->b_force )
245 msg_Dbg( p_demux, "subtitle demux discarded" );
249 p_demux->pf_demux = Demux;
250 p_demux->pf_control = Control;
251 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
255 p_sys->psz_header = NULL;
256 p_sys->i_subtitle = 0;
257 p_sys->i_subtitles = 0;
258 p_sys->subtitle = NULL;
259 p_sys->i_microsecperframe = 40000;
261 p_sys->jss.b_inited = false;
262 p_sys->mpsub.b_inited = false;
265 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); /* FIXME */
267 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
269 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
271 /* Check for override of the fps */
272 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
275 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
276 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
279 /* Get or probe the type */
280 p_sys->i_type = SUB_TYPE_UNKNOWN;
281 psz_type = var_CreateGetString( p_demux, "sub-type" );
282 if( psz_type && *psz_type )
288 if( sub_read_subtitle_function[i].psz_type_name == NULL )
291 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
294 p_sys->i_type = sub_read_subtitle_function[i].i_type;
301 /* Probe if unknown type */
302 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
307 msg_Dbg( p_demux, "autodetecting subtitle format" );
308 for( i_try = 0; i_try < 256; i_try++ )
313 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
316 if( strcasestr( s, "<SAMI>" ) )
318 p_sys->i_type = SUB_TYPE_SAMI;
321 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
322 sscanf( s, "{%d}{}", &i_dummy ) == 1)
324 p_sys->i_type = SUB_TYPE_MICRODVD;
328 "%d:%d:%d,%d --> %d:%d:%d,%d",
329 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
330 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
332 p_sys->i_type = SUB_TYPE_SUBRIP;
336 "%d:%d:%d.%d --> %d:%d:%d.%d",
337 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
338 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
340 msg_Err( p_demux, "Detected invalid SubRip file, playing anyway" );
341 p_sys->i_type = SUB_TYPE_SUBRIP_DOT;
344 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
346 p_sys->i_type = SUB_TYPE_SSA1;
349 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
351 p_sys->i_type = SUB_TYPE_ASS;
354 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
356 p_sys->i_type = SUB_TYPE_SSA2_4;
359 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
361 p_sys->i_type = SUB_TYPE_SSA2_4;
364 else if( !strncasecmp( s, "Dialogue:", 9 ) )
366 p_sys->i_type = SUB_TYPE_ASS;
369 else if( strcasestr( s, "[INFORMATION]" ) )
371 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
374 else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d",
375 &i_dummy, &i_dummy, &i_dummy, &i_dummy,
376 &i_dummy, &i_dummy, &i_dummy ) == 7 ||
377 sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
379 p_sys->i_type = SUB_TYPE_JACOSUB;
382 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
383 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
385 p_sys->i_type = SUB_TYPE_VPLAYER;
388 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
389 &i_dummy, &i_dummy ) == 4 )
391 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
394 else if( sscanf( s, "[%d:%d:%d]%c",
395 &i_dummy, &i_dummy, &i_dummy, &p_dummy ) == 4 )
397 p_sys->i_type = SUB_TYPE_DKS;
400 else if( strstr( s, "*** START SCRIPT" ) )
402 p_sys->i_type = SUB_TYPE_SUBVIEW1;
405 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
406 sscanf( s, "[%d][]", &i_dummy ) == 1)
408 p_sys->i_type = SUB_TYPE_MPL2;
411 else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
412 ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
415 p_sys->i_type = SUB_TYPE_MPSUB;
418 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
420 p_sys->i_type = SUB_TYPE_AQT;
423 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
425 p_sys->i_type = SUB_TYPE_PJS;
428 else if( sscanf( s, "{%d:%d:%d}",
429 &i_dummy, &i_dummy, &i_dummy ) == 3 )
431 p_sys->i_type = SUB_TYPE_PSB;
434 else if( strcasestr( s, "<time" ) )
436 p_sys->i_type = SUB_TYPE_RT;
446 /* It will nearly always work even for non seekable stream thanks the
447 * caching system, and if it fails we lose just a few sub */
448 if( stream_Seek( p_demux->s, 0 ) )
450 msg_Warn( p_demux, "failed to rewind" );
454 /* Quit on unknown subtitles */
455 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
457 msg_Warn( p_demux, "failed to recognize subtitle type" );
464 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
466 msg_Dbg( p_demux, "detected %s format",
467 sub_read_subtitle_function[i].psz_name );
468 pf_read = sub_read_subtitle_function[i].pf_read;
473 msg_Dbg( p_demux, "loading all subtitles..." );
475 /* Load the whole file */
476 TextLoad( &p_sys->txt, p_demux->s );
481 if( p_sys->i_subtitles >= i_max )
484 if( !( p_sys->subtitle = realloc_or_free( p_sys->subtitle,
485 sizeof(subtitle_t) * i_max ) ) )
487 TextUnload( &p_sys->txt );
493 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
494 p_sys->i_subtitles ) )
497 p_sys->i_subtitles++;
500 TextUnload( &p_sys->txt );
502 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
504 /* Fix subtitle (order and time) *** */
505 p_sys->i_subtitle = 0;
507 if( p_sys->i_subtitles > 0 )
509 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
511 if( p_sys->i_length <= 0 )
512 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
515 /* *** add subtitle ES *** */
516 if( p_sys->i_type == SUB_TYPE_SSA1 ||
517 p_sys->i_type == SUB_TYPE_SSA2_4 ||
518 p_sys->i_type == SUB_TYPE_ASS )
520 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
524 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
526 if( p_sys->psz_header != NULL )
528 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
529 fmt.p_extra = strdup( p_sys->psz_header );
531 p_sys->es = es_out_Add( p_demux->out, &fmt );
536 /*****************************************************************************
537 * Close: Close subtitle demux
538 *****************************************************************************/
539 static void Close( vlc_object_t *p_this )
541 demux_t *p_demux = (demux_t*)p_this;
542 demux_sys_t *p_sys = p_demux->p_sys;
545 for( i = 0; i < p_sys->i_subtitles; i++ )
546 free( p_sys->subtitle[i].psz_text );
547 free( p_sys->subtitle );
552 /*****************************************************************************
554 *****************************************************************************/
555 static int Control( demux_t *p_demux, int i_query, va_list args )
557 demux_sys_t *p_sys = p_demux->p_sys;
563 case DEMUX_GET_LENGTH:
564 pi64 = (int64_t*)va_arg( args, int64_t * );
565 *pi64 = p_sys->i_length;
569 pi64 = (int64_t*)va_arg( args, int64_t * );
570 if( p_sys->i_subtitle < p_sys->i_subtitles )
572 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
578 i64 = (int64_t)va_arg( args, int64_t );
579 p_sys->i_subtitle = 0;
580 while( p_sys->i_subtitle < p_sys->i_subtitles )
582 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
584 if( p_subtitle->i_start > i64 )
586 if( p_subtitle->i_stop > p_subtitle->i_start && p_subtitle->i_stop > i64 )
592 if( p_sys->i_subtitle >= p_sys->i_subtitles )
596 case DEMUX_GET_POSITION:
597 pf = (double*)va_arg( args, double * );
598 if( p_sys->i_subtitle >= p_sys->i_subtitles )
602 else if( p_sys->i_subtitles > 0 )
604 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
605 (double)p_sys->i_length;
613 case DEMUX_SET_POSITION:
614 f = (double)va_arg( args, double );
615 i64 = f * p_sys->i_length;
617 p_sys->i_subtitle = 0;
618 while( p_sys->i_subtitle < p_sys->i_subtitles &&
619 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
623 if( p_sys->i_subtitle >= p_sys->i_subtitles )
627 case DEMUX_SET_NEXT_DEMUX_TIME:
628 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
633 case DEMUX_GET_ATTACHMENTS:
634 case DEMUX_GET_TITLE_INFO:
635 case DEMUX_HAS_UNSUPPORTED_META:
636 case DEMUX_CAN_RECORD:
640 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
645 /*****************************************************************************
646 * Demux: Send subtitle to decoder
647 *****************************************************************************/
648 static int Demux( demux_t *p_demux )
650 demux_sys_t *p_sys = p_demux->p_sys;
653 if( p_sys->i_subtitle >= p_sys->i_subtitles )
656 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
657 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
659 /* Should not happen */
660 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
663 while( p_sys->i_subtitle < p_sys->i_subtitles &&
664 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
666 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
669 int i_len = strlen( p_subtitle->psz_text ) + 1;
671 if( i_len <= 1 || p_subtitle->i_start < 0 )
677 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
684 p_block->i_pts = VLC_TS_0 + p_subtitle->i_start;
685 if( p_subtitle->i_stop >= 0 && p_subtitle->i_stop >= p_subtitle->i_start )
686 p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start;
688 memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len );
690 es_out_Send( p_demux->out, p_sys->es, p_block );
696 p_sys->i_next_demux_date = 0;
701 /*****************************************************************************
702 * Fix: fix time stamp and order of subtitle
703 *****************************************************************************/
704 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
705 static void Fix( demux_t *p_demux )
707 demux_sys_t *p_sys = p_demux->p_sys;
711 /* *** fix order (to be sure...) *** */
712 /* We suppose that there are near in order and this durty bubble sort
713 * wont take too much time
718 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
720 if( p_sys->subtitle[i_index].i_start <
721 p_sys->subtitle[i_index - 1].i_start )
725 p_sys->subtitle + i_index - 1,
726 sizeof( subtitle_t ) );
727 memcpy( p_sys->subtitle + i_index - 1,
728 p_sys->subtitle + i_index,
729 sizeof( subtitle_t ) );
730 memcpy( p_sys->subtitle + i_index,
732 sizeof( subtitle_t ) );
740 static int TextLoad( text_t *txt, stream_t *s )
746 txt->i_line_count = 0;
748 txt->line = calloc( i_line_max, sizeof( char * ) );
752 /* load the complete file */
755 char *psz = stream_ReadLine( s );
760 txt->line[txt->i_line_count++] = psz;
761 if( txt->i_line_count >= i_line_max )
764 txt->line = realloc_or_free( txt->line, i_line_max * sizeof( char * ) );
770 if( txt->i_line_count <= 0 )
778 static void TextUnload( text_t *txt )
782 for( i = 0; i < txt->i_line_count; i++ )
784 free( txt->line[i] );
788 txt->i_line_count = 0;
791 static char *TextGetLine( text_t *txt )
793 if( txt->i_line >= txt->i_line_count )
796 return txt->line[txt->i_line++];
798 static void TextPreviousLine( text_t *txt )
800 if( txt->i_line > 0 )
804 /*****************************************************************************
805 * Specific Subtitle function
806 *****************************************************************************/
809 * {n1}{n2}Line1|Line2|Line3....
810 * where n1 and n2 are the video frame number (n2 can be empty)
812 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
816 demux_sys_t *p_sys = p_demux->p_sys;
817 text_t *txt = &p_sys->txt;
825 const char *s = TextGetLine( txt );
829 psz_text = malloc( strlen(s) + 1 );
835 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
836 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
839 if( i_start != 1 || i_stop != 1 )
842 /* We found a possible setting of the framerate "{1}{1}23.976" */
843 /* Check if it's usable, and if the sub-fps is not set */
844 f_fps = us_strtod( psz_text, NULL );
845 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
846 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
851 /* replace | by \n */
852 for( i = 0; psz_text[i] != '\0'; i++ )
854 if( psz_text[i] == '|' )
859 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
860 p_subtitle->i_stop = i_stop >= 0 ? (i_stop * p_sys->i_microsecperframe) : -1;
861 p_subtitle->psz_text = psz_text;
865 /* ParseSubRipSubViewer
868 * h1:m1:s1,d1 --> h2:m2:s2,d2
873 * Format SubViewer v1/v2
874 * h1:m1:s1.d1,h2:m2:s2.d2
879 * We ignore line number for SubRip
881 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
885 demux_sys_t *p_sys = p_demux->p_sys;
886 text_t *txt = &p_sys->txt;
891 const char *s = TextGetLine( txt );
892 int h1, m1, s1, d1, h2, m2, s2, d2;
897 if( sscanf( s, psz_fmt,
899 &h2, &m2, &s2, &d2 ) == 8 )
901 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
902 (int64_t)m1 * 60*1000 +
904 (int64_t)d1 ) * 1000;
906 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
907 (int64_t)m2 * 60*1000 +
909 (int64_t)d2 ) * 1000;
910 if( p_subtitle->i_start < p_subtitle->i_stop )
915 /* Now read text until an empty line */
916 psz_text = strdup("");
922 const char *s = TextGetLine( txt );
926 i_len = s ? strlen( s ) : 0;
929 p_subtitle->psz_text = psz_text;
933 i_old = strlen( psz_text );
934 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
939 strcat( psz_text, s );
940 strcat( psz_text, "\n" );
942 /* replace [br] by \n */
947 while( ( p = strstr( psz_text, "[br]" ) ) )
950 memmove( p, &p[3], strlen(&p[3])+1 );
957 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
961 return ParseSubRipSubViewer( p_demux, p_subtitle,
962 "%d:%d:%d,%d --> %d:%d:%d,%d",
966 * Special version for buggy file using '.' instead of ','
968 static int ParseSubRipDot( demux_t *p_demux, subtitle_t *p_subtitle,
972 return ParseSubRipSubViewer( p_demux, p_subtitle,
973 "%d:%d:%d.%d --> %d:%d:%d.%d",
978 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
983 return ParseSubRipSubViewer( p_demux, p_subtitle,
984 "%d:%d:%d.%d,%d:%d:%d.%d",
990 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
993 demux_sys_t *p_sys = p_demux->p_sys;
994 text_t *txt = &p_sys->txt;
998 const char *s = TextGetLine( txt );
999 int h1, m1, s1, c1, h2, m2, s2, c2;
1004 return VLC_EGENERIC;
1006 /* We expect (SSA2-4):
1007 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1008 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1010 * 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.
1014 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1015 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1018 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
1019 psz_text = malloc( strlen(s) );
1024 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
1030 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1031 /* (Layer comes from ASS specs ... it's empty for SSA.) */
1032 if( p_sys->i_type == SUB_TYPE_SSA1 )
1034 /* SSA1 has only 8 commas before the text starts, not 9 */
1035 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
1040 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
1042 /* ReadOrder, Layer, %s(rest of fields) */
1043 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
1044 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
1045 memcpy( psz_text, temp, strlen(temp) );
1048 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1049 (int64_t)m1 * 60*1000 +
1050 (int64_t)s1 * 1000 +
1051 (int64_t)c1 * 10 ) * 1000;
1052 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1053 (int64_t)m2 * 60*1000 +
1054 (int64_t)s2 * 1000 +
1055 (int64_t)c2 * 10 ) * 1000;
1056 p_subtitle->psz_text = psz_text;
1061 /* All the other stuff we add to the header field */
1063 if( asprintf( &psz_header, "%s%s\n",
1064 p_sys->psz_header ? p_sys->psz_header : "", s ) == -1 )
1066 p_sys->psz_header = psz_header;
1072 * h:m:s:Line1|Line2|Line3....
1074 * h:m:s Line1|Line2|Line3....
1076 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1079 VLC_UNUSED( i_idx );
1081 demux_sys_t *p_sys = p_demux->p_sys;
1082 text_t *txt = &p_sys->txt;
1088 const char *s = TextGetLine( txt );
1092 return VLC_EGENERIC;
1094 psz_text = malloc( strlen( s ) + 1 );
1098 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1099 &h1, &m1, &s1, psz_text ) == 4 )
1101 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1102 (int64_t)m1 * 60*1000 +
1103 (int64_t)s1 * 1000 ) * 1000;
1104 p_subtitle->i_stop = -1;
1110 /* replace | by \n */
1111 for( i = 0; psz_text[i] != '\0'; i++ )
1113 if( psz_text[i] == '|' )
1116 p_subtitle->psz_text = psz_text;
1122 static char *ParseSamiSearch( text_t *txt,
1123 char *psz_start, const char *psz_str )
1125 if( psz_start && strcasestr( psz_start, psz_str ) )
1127 char *s = strcasestr( psz_start, psz_str );
1128 return &s[strlen( psz_str )];
1133 char *p = TextGetLine( txt );
1137 if( strcasestr( p, psz_str ) )
1139 char *s = strcasestr( p, psz_str );
1140 return &s[strlen( psz_str )];
1144 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1146 VLC_UNUSED( i_idx );
1147 demux_sys_t *p_sys = p_demux->p_sys;
1148 text_t *txt = &p_sys->txt;
1153 unsigned int i_text;
1154 char text[8192]; /* Arbitrary but should be long enough */
1156 /* search "Start=" */
1157 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1158 return VLC_EGENERIC;
1160 /* get start value */
1161 i_start = strtol( s, &s, 0 );
1164 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1165 return VLC_EGENERIC;
1168 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1169 return VLC_EGENERIC;
1173 /* now get all txt until a "Start=" line */
1177 /* Search non empty line */
1178 while( s && *s == '\0' )
1179 s = TextGetLine( txt );
1185 if( !strncasecmp( s, "<br", 3 ) )
1189 else if( strcasestr( s, "Start=" ) )
1191 TextPreviousLine( txt );
1194 s = ParseSamiSearch( txt, s, ">" );
1196 else if( !strncmp( s, " ", 6 ) )
1201 else if( *s == '\t' )
1211 if( c != '\0' && i_text+1 < sizeof(text) )
1214 text[i_text] = '\0';
1218 p_subtitle->i_start = i_start * 1000;
1219 p_subtitle->i_stop = -1;
1220 p_subtitle->psz_text = strdup( text );
1232 * TODO it can have a header
1239 * LANG support would be cool
1240 * CODEPAGE is probably mandatory FIXME
1242 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1245 VLC_UNUSED( i_idx );
1247 demux_sys_t *p_sys = p_demux->p_sys;
1248 text_t *txt = &p_sys->txt;
1253 const char *s = TextGetLine( txt );
1257 return VLC_EGENERIC;
1261 &h1, &m1, &s1, &c1 ) == 4 )
1263 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1264 (int64_t)m1 * 60*1000 +
1265 (int64_t)s1 * 1000 +
1266 (int64_t)c1 * 10) * 1000;
1267 p_subtitle->i_stop = -1;
1272 /* Now read text until a line containing "}" */
1273 psz_text = strdup("");
1278 const char *s = TextGetLine( txt );
1285 return VLC_EGENERIC;
1288 i_len = strlen( s );
1289 if( i_len == 1 && s[0] == '}')
1291 p_subtitle->psz_text = psz_text;
1295 i_old = strlen( psz_text );
1296 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1299 strcat( psz_text, s );
1300 strcat( psz_text, "\n" );
1306 * [n1][n2]Line1|Line2|Line3...
1307 * where n1 and n2 are the video frame number (n2 can be empty)
1309 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1311 VLC_UNUSED( i_idx );
1313 demux_sys_t *p_sys = p_demux->p_sys;
1314 text_t *txt = &p_sys->txt;
1320 const char *s = TextGetLine( txt );
1325 return VLC_EGENERIC;
1327 psz_text = malloc( strlen(s) + 1 );
1333 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1334 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1336 p_subtitle->i_start = (int64_t)i_start * 100000;
1337 p_subtitle->i_stop = i_stop >= 0 ? ((int64_t)i_stop * 100000) : -1;
1343 for( i = 0; psz_text[i] != '\0'; )
1345 /* replace | by \n */
1346 if( psz_text[i] == '|' )
1350 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1351 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1355 p_subtitle->psz_text = psz_text;
1359 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1361 VLC_UNUSED( i_idx );
1363 demux_sys_t *p_sys = p_demux->p_sys;
1364 text_t *txt = &p_sys->txt;
1365 char *psz_text = strdup( "" );
1367 int i_firstline = 1;
1373 const char *s = TextGetLine( txt );
1378 return VLC_EGENERIC;
1382 if( sscanf (s, "-->> %d", &t) == 1)
1384 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1385 p_subtitle->i_stop = -1;
1387 /* Starting of a subtitle */
1392 /* We have been too far: end of the subtitle, begin of next */
1395 TextPreviousLine( txt );
1402 i_old = strlen( psz_text ) + 1;
1403 psz_text = realloc_or_free( psz_text, i_old + strlen( s ) + 1 );
1406 strcat( psz_text, s );
1407 strcat( psz_text, "\n" );
1408 if( txt->i_line == txt->i_line_count )
1412 p_subtitle->psz_text = psz_text;
1416 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1418 VLC_UNUSED( i_idx );
1420 demux_sys_t *p_sys = p_demux->p_sys;
1421 text_t *txt = &p_sys->txt;
1427 const char *s = TextGetLine( txt );
1431 return VLC_EGENERIC;
1433 psz_text = malloc( strlen(s) + 1 );
1438 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1440 /* 1/10th of second ? Frame based ? FIXME */
1441 p_subtitle->i_start = 10 * t1;
1442 p_subtitle->i_stop = 10 * t2;
1443 /* Remove latest " */
1444 psz_text[ strlen(psz_text) - 1 ] = '\0';
1451 /* replace | by \n */
1452 for( i = 0; psz_text[i] != '\0'; i++ )
1454 if( psz_text[i] == '|' )
1458 p_subtitle->psz_text = psz_text;
1459 msg_Dbg( p_demux, "%s", psz_text );
1463 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1465 VLC_UNUSED( i_idx );
1467 demux_sys_t *p_sys = p_demux->p_sys;
1468 text_t *txt = &p_sys->txt;
1469 char *psz_text = strdup( "" );
1471 if( !p_sys->mpsub.b_inited )
1473 p_sys->mpsub.f_total = 0.0;
1474 p_sys->mpsub.f_factor = 0.0;
1476 p_sys->mpsub.b_inited = true;
1485 const char *s = TextGetLine( txt );
1489 return VLC_EGENERIC;
1492 if( strstr( s, "FORMAT" ) )
1494 if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1496 p_sys->mpsub.f_factor = 100.0;
1500 psz_temp = malloc( strlen(s) );
1507 if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1510 f_fps = us_strtod( psz_temp, NULL );
1511 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1512 var_SetFloat( p_demux, "sub-fps", f_fps );
1514 p_sys->mpsub.f_factor = 1.0;
1521 f1 = us_strtod( s, &psz_temp );
1524 f2 = us_strtod( psz_temp, NULL );
1525 p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor;
1526 p_subtitle->i_start = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1527 p_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor;
1528 p_subtitle->i_stop = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1535 const char *s = TextGetLine( txt );
1540 return VLC_EGENERIC;
1543 int i_len = strlen( s );
1547 int i_old = strlen( psz_text );
1549 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1553 strcat( psz_text, s );
1554 strcat( psz_text, "\n" );
1557 p_subtitle->psz_text = psz_text;
1561 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1563 VLC_UNUSED( i_idx );
1565 demux_sys_t *p_sys = p_demux->p_sys;
1566 text_t *txt = &p_sys->txt;
1567 char *psz_text, *psz_orig;
1568 char *psz_text2, *psz_orig2;
1569 int h1, h2, m1, m2, s1, s2, f1, f2;
1571 if( !p_sys->jss.b_inited )
1573 p_sys->jss.i_comment = 0;
1574 p_sys->jss.i_time_resolution = 30;
1575 p_sys->jss.i_time_shift = 0;
1577 p_sys->jss.b_inited = true;
1580 /* Parse the main lines */
1583 const char *s = TextGetLine( txt );
1585 return VLC_EGENERIC;
1587 psz_orig = malloc( strlen( s ) + 1 );
1590 psz_text = psz_orig;
1592 /* Complete time lines */
1593 if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1594 &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1596 p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1597 (int64_t)( ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) )
1599 p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1600 (int64_t)( ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) )
1604 /* Short time lines */
1605 else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1607 p_subtitle->i_start = (int64_t)(
1608 ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1609 p_subtitle->i_stop = (int64_t)(
1610 ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1613 /* General Directive lines */
1614 /* Only TIME and SHIFT are supported so far */
1615 else if( s[0] == '#' )
1617 int h = 0, m =0, sec = 1, f = 1;
1621 strcpy( psz_text, s );
1623 switch( toupper( psz_text[1] ) )
1626 shift = isalpha( psz_text[2] ) ? 6 : 2 ;
1628 if( sscanf( &psz_text[shift], "%d", &h ) )
1630 /* Negative shifting */
1637 if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1639 if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1641 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1646 sscanf( &psz_text[shift], "%d:%d.%d",
1654 sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1657 p_sys->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec )
1658 * p_sys->jss.i_time_resolution + f ) * inv;
1663 shift = isalpha( psz_text[2] ) ? 8 : 2 ;
1665 sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
1672 /* Unkown type line, probably a comment */
1679 while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
1681 const char *s2 = TextGetLine( txt );
1686 return VLC_EGENERIC;
1689 int i_len = strlen( s2 );
1693 int i_old = strlen( psz_text );
1695 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 );
1699 psz_orig = psz_text;
1700 strcat( psz_text, s2 );
1703 /* Skip the blanks */
1704 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1706 /* Parse the directives */
1707 if( isalpha( *psz_text ) || *psz_text == '[' )
1709 while( *psz_text != ' ' )
1712 /* Directives are NOT parsed yet */
1713 /* This has probably a better place in a decoder ? */
1714 /* directive = malloc( strlen( psz_text ) + 1 );
1715 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1718 /* Skip the blanks after directives */
1719 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1721 /* Clean all the lines from inline comments and other stuffs */
1722 psz_orig2 = calloc( strlen( psz_text) + 1, 1 );
1723 psz_text2 = psz_orig2;
1725 for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1730 p_sys->jss.i_comment++;
1733 if( p_sys->jss.i_comment )
1735 p_sys->jss.i_comment = 0;
1736 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1740 if( !p_sys->jss.i_comment )
1748 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1750 if( !p_sys->jss.i_comment )
1757 if( (*(psz_text + 1 ) ) == 'n' )
1764 if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
1765 ( toupper(*(psz_text + 1 ) ) == 'F' ) )
1767 psz_text++; psz_text++;
1770 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1771 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1772 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1773 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1778 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1779 (*(psz_text + 1 ) ) == '\\' )
1781 else if( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n' ||
1782 *(psz_text + 1 ) == '\0' )
1788 if( !p_sys->jss.i_comment )
1790 *psz_text2 = *psz_text;
1797 p_subtitle->psz_text = psz_orig2;
1798 msg_Dbg( p_demux, "%s", p_subtitle->psz_text );
1803 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1805 VLC_UNUSED( i_idx );
1807 demux_sys_t *p_sys = p_demux->p_sys;
1808 text_t *txt = &p_sys->txt;
1816 const char *s = TextGetLine( txt );
1819 return VLC_EGENERIC;
1821 psz_text = malloc( strlen( s ) + 1 );
1825 if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1826 &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1828 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1829 (int64_t)m1 * 60*1000 +
1830 (int64_t)s1 * 1000 ) * 1000;
1831 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1832 (int64_t)m2 * 60*1000 +
1833 (int64_t)s2 * 1000 ) * 1000;
1839 /* replace | by \n */
1840 for( i = 0; psz_text[i] != '\0'; i++ )
1842 if( psz_text[i] == '|' )
1845 p_subtitle->psz_text = psz_text;
1849 static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
1851 if( strlen( psz ) == 0 ) return 0;
1852 if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
1853 sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
1854 sscanf( psz, "%d.%d", s, f ) == 2 ||
1855 sscanf( psz, "%d:%d", m, s ) == 2 ||
1856 sscanf( psz, "%d", s ) == 1 )
1858 return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
1859 + (int64_t)*f * 10 * 1000;
1861 else return VLC_EGENERIC;
1864 static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1866 VLC_UNUSED( i_idx );
1867 demux_sys_t *p_sys = p_demux->p_sys;
1868 text_t *txt = &p_sys->txt;
1869 char *psz_text = NULL;
1873 int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
1874 int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
1875 const char *s = TextGetLine( txt );
1879 return VLC_EGENERIC;
1881 psz_text = malloc( strlen( s ) + 1 );
1885 /* Find the good begining. This removes extra spaces at the beginning
1887 char *psz_temp = strcasestr( s, "<time");
1888 if( psz_temp != NULL )
1890 char psz_end[12], psz_begin[12];
1891 /* Line has begin and end */
1892 if( ( sscanf( psz_temp,
1893 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\n\r]",
1894 psz_begin, psz_end, psz_text) != 3 ) &&
1895 /* Line has begin and no end */
1897 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\n\r]",
1898 psz_begin, psz_text ) != 2) )
1899 /* Line is not recognized */
1905 int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
1906 p_subtitle->i_start = i_time >= 0 ? i_time : 0;
1908 i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
1909 p_subtitle->i_stop = i_time >= 0 ? i_time : -1;
1914 /* Get the following Lines */
1917 const char *s = TextGetLine( txt );
1922 return VLC_EGENERIC;
1925 int i_len = strlen( s );
1926 if( i_len == 0 ) break;
1928 if( strcasestr( s, "<time" ) ||
1929 strcasestr( s, "<clear/") )
1931 TextPreviousLine( txt );
1935 int i_old = strlen( psz_text );
1937 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1941 strcat( psz_text, s );
1942 strcat( psz_text, "\n" );
1945 /* Remove the starting ">" that remained after the sscanf */
1946 memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
1948 p_subtitle->psz_text = psz_text;
1953 static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1955 VLC_UNUSED( i_idx );
1957 demux_sys_t *p_sys = p_demux->p_sys;
1958 text_t *txt = &p_sys->txt;
1965 char *s = TextGetLine( txt );
1968 return VLC_EGENERIC;
1970 psz_text = malloc( strlen( s ) + 1 );
1974 if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
1975 &h1, &m1, &s1, psz_text ) == 4 )
1977 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1978 (int64_t)m1 * 60*1000 +
1979 (int64_t)s1 * 1000 ) * 1000;
1981 char *s = TextGetLine( txt );
1985 return VLC_EGENERIC;
1988 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
1989 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1990 (int64_t)m2 * 60*1000 +
1991 (int64_t)s2 * 1000 ) * 1000;
1993 p_subtitle->i_stop = -1;
1999 /* replace [br] by \n */
2001 while( ( p = strstr( psz_text, "[br]" ) ) )
2004 memmove( p, &p[3], strlen(&p[3])+1 );
2007 p_subtitle->psz_text = psz_text;
2011 static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2013 VLC_UNUSED( i_idx );
2015 demux_sys_t *p_sys = p_demux->p_sys;
2016 text_t *txt = &p_sys->txt;
2023 char *s = TextGetLine( txt );
2026 return VLC_EGENERIC;
2028 if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
2030 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2031 (int64_t)m1 * 60*1000 +
2032 (int64_t)s1 * 1000 ) * 1000;
2034 char *s = TextGetLine( txt );
2036 return VLC_EGENERIC;
2038 psz_text = strdup( s );
2042 s = TextGetLine( txt );
2046 return VLC_EGENERIC;
2049 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2050 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2051 (int64_t)m2 * 60*1000 +
2052 (int64_t)s2 * 1000 ) * 1000;
2054 p_subtitle->i_stop = -1;
2060 p_subtitle->psz_text = psz_text;