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 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_input.h>
39 #include <vlc_memory.h>
42 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
47 #include <vlc_demux.h>
48 #include <vlc_charset.h>
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Open ( vlc_object_t *p_this );
54 static void Close( vlc_object_t *p_this );
56 #define SUB_DELAY_LONGTEXT \
57 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
58 #define SUB_FPS_LONGTEXT \
59 N_("Override the normal frames per second settings. " \
60 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
61 #define SUB_TYPE_LONGTEXT \
62 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
63 "\"subrip\", \"subviewer\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\", " \
64 "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\", "\
65 "\"mpsub\", \"jacosub\", \"psb\", \"realtext\", \"dks\", \"subviewer1\", " \
66 " and \"auto\" (meaning autodetection, this should always work).")
68 static const char *const ppsz_sub_type[] =
70 "auto", "microdvd", "subrip", "subviewer", "ssa1",
71 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
72 "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
77 set_shortname( N_("Subtitles"))
78 set_description( N_("Text subtitles parser") )
79 set_capability( "demux", 0 )
80 set_category( CAT_INPUT )
81 set_subcategory( SUBCAT_INPUT_DEMUX )
82 add_float( "sub-fps", 0.0, NULL,
83 N_("Frames per second"),
84 SUB_FPS_LONGTEXT, true )
85 add_integer( "sub-delay", 0, NULL,
86 N_("Subtitles delay"),
87 SUB_DELAY_LONGTEXT, true )
88 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
89 SUB_TYPE_LONGTEXT, true )
90 change_string_list( ppsz_sub_type, NULL, NULL )
91 set_callbacks( Open, Close )
93 add_shortcut( "subtitle" )
96 /*****************************************************************************
98 *****************************************************************************/
101 SUB_TYPE_UNKNOWN = -1,
104 SUB_TYPE_SUBRIP_DOT, /* Invalid SubRip file (dot instead of comma) */
110 SUB_TYPE_SUBVIEWER, /* SUBVIEWER 2 */
111 SUB_TYPE_DVDSUBTITLE, /* Mplayer calls it subviewer2 */
120 SUB_TYPE_SUBVIEW1 /* SUBVIEWER 1 - mplayer calls it subrip09,
121 and Gnome subtitles SubViewer 1.0 */
131 static int TextLoad( text_t *, stream_t *s );
132 static void TextUnload( text_t * );
149 int64_t i_next_demux_date;
150 int64_t i_microsecperframe;
155 subtitle_t *subtitle;
165 int i_time_resolution;
177 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
178 static int ParseSubRip ( demux_t *, subtitle_t *, int );
179 static int ParseSubRipDot ( demux_t *, subtitle_t *, int );
180 static int ParseSubViewer ( demux_t *, subtitle_t *, int );
181 static int ParseSSA ( demux_t *, subtitle_t *, int );
182 static int ParseVplayer ( demux_t *, subtitle_t *, int );
183 static int ParseSami ( demux_t *, subtitle_t *, int );
184 static int ParseDVDSubtitle( demux_t *, subtitle_t *, int );
185 static int ParseMPL2 ( demux_t *, subtitle_t *, int );
186 static int ParseAQT ( demux_t *, subtitle_t *, int );
187 static int ParsePJS ( demux_t *, subtitle_t *, int );
188 static int ParseMPSub ( demux_t *, subtitle_t *, int );
189 static int ParseJSS ( demux_t *, subtitle_t *, int );
190 static int ParsePSB ( demux_t *, subtitle_t *, int );
191 static int ParseRealText ( demux_t *, subtitle_t *, int );
192 static int ParseDKS ( demux_t *, subtitle_t *, int );
193 static int ParseSubViewer1 ( demux_t *, subtitle_t *, int );
197 const char *psz_type_name;
199 const char *psz_name;
200 int (*pf_read)( demux_t *, subtitle_t*, int );
201 } sub_read_subtitle_function [] =
203 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
204 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
205 { "subrip-dot", SUB_TYPE_SUBRIP_DOT, "SubRIP(Dot)", ParseSubRipDot },
206 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
207 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
208 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
209 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
210 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
211 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
212 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
213 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
214 { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT },
215 { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS },
216 { "mpsub", SUB_TYPE_MPSUB, "MPSub", ParseMPSub },
217 { "jacosub", SUB_TYPE_JACOSUB, "JacoSub", ParseJSS },
218 { "psb", SUB_TYPE_PSB, "PowerDivx", ParsePSB },
219 { "realtext", SUB_TYPE_RT, "RealText", ParseRealText },
220 { "dks", SUB_TYPE_DKS, "DKS", ParseDKS },
221 { "subviewer1", SUB_TYPE_SUBVIEW1, "Subviewer 1", ParseSubViewer1 },
222 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
224 /* When adding support for more formats, be sure to add their file extension
225 * to src/input/subtitles.c to enable auto-detection.
228 static int Demux( demux_t * );
229 static int Control( demux_t *, int, va_list );
231 /*static void Fix( demux_t * );*/
233 /*****************************************************************************
235 *****************************************************************************/
236 static int Open ( vlc_object_t *p_this )
238 demux_t *p_demux = (demux_t*)p_this;
243 int (*pf_read)( demux_t *, subtitle_t*, int );
246 if( !p_demux->b_force )
248 msg_Dbg( p_demux, "subtitle demux discarded" );
252 p_demux->pf_demux = Demux;
253 p_demux->pf_control = Control;
254 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
258 p_sys->psz_header = NULL;
259 p_sys->i_subtitle = 0;
260 p_sys->i_subtitles = 0;
261 p_sys->subtitle = NULL;
262 p_sys->i_microsecperframe = 40000;
264 p_sys->jss.b_inited = false;
265 p_sys->mpsub.b_inited = false;
268 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); /* FIXME */
270 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
272 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
274 /* Check for override of the fps */
275 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
278 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
279 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
282 /* Get or probe the type */
283 p_sys->i_type = SUB_TYPE_UNKNOWN;
284 psz_type = var_CreateGetString( p_demux, "sub-type" );
285 if( psz_type && *psz_type )
291 if( sub_read_subtitle_function[i].psz_type_name == NULL )
294 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
297 p_sys->i_type = sub_read_subtitle_function[i].i_type;
304 /* Probe if unknown type */
305 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
310 msg_Dbg( p_demux, "autodetecting subtitle format" );
311 for( i_try = 0; i_try < 256; i_try++ )
316 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
319 if( strcasestr( s, "<SAMI>" ) )
321 p_sys->i_type = SUB_TYPE_SAMI;
324 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
325 sscanf( s, "{%d}{}", &i_dummy ) == 1)
327 p_sys->i_type = SUB_TYPE_MICRODVD;
331 "%d:%d:%d,%d --> %d:%d:%d,%d",
332 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
333 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
335 p_sys->i_type = SUB_TYPE_SUBRIP;
339 "%d:%d:%d.%d --> %d:%d:%d.%d",
340 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
341 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
343 msg_Err( p_demux, "Detected invalid SubRip file, playing anyway" );
344 p_sys->i_type = SUB_TYPE_SUBRIP_DOT;
347 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
349 p_sys->i_type = SUB_TYPE_SSA1;
352 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
354 p_sys->i_type = SUB_TYPE_ASS;
357 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
359 p_sys->i_type = SUB_TYPE_SSA2_4;
362 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
364 p_sys->i_type = SUB_TYPE_SSA2_4;
367 else if( !strncasecmp( s, "Dialogue:", 9 ) )
369 p_sys->i_type = SUB_TYPE_ASS;
372 else if( strcasestr( s, "[INFORMATION]" ) )
374 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
377 else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d",
378 &i_dummy, &i_dummy, &i_dummy, &i_dummy,
379 &i_dummy, &i_dummy, &i_dummy ) == 7 ||
380 sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
382 p_sys->i_type = SUB_TYPE_JACOSUB;
385 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
386 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
388 p_sys->i_type = SUB_TYPE_VPLAYER;
391 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
392 &i_dummy, &i_dummy ) == 4 )
394 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
397 else if( sscanf( s, "[%d:%d:%d]%c",
398 &i_dummy, &i_dummy, &i_dummy, &p_dummy ) == 4 )
400 p_sys->i_type = SUB_TYPE_DKS;
403 else if( strstr( s, "*** START SCRIPT" ) )
405 p_sys->i_type = SUB_TYPE_SUBVIEW1;
408 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
409 sscanf( s, "[%d][]", &i_dummy ) == 1)
411 p_sys->i_type = SUB_TYPE_MPL2;
414 else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
415 ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
418 p_sys->i_type = SUB_TYPE_MPSUB;
421 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
423 p_sys->i_type = SUB_TYPE_AQT;
426 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
428 p_sys->i_type = SUB_TYPE_PJS;
431 else if( sscanf( s, "{%d:%d:%d}",
432 &i_dummy, &i_dummy, &i_dummy ) == 3 )
434 p_sys->i_type = SUB_TYPE_PSB;
437 else if( strcasestr( s, "<time" ) )
439 p_sys->i_type = SUB_TYPE_RT;
449 /* It will nearly always work even for non seekable stream thanks the
450 * caching system, and if it fails we lose just a few sub */
451 if( stream_Seek( p_demux->s, 0 ) )
453 msg_Warn( p_demux, "failed to rewind" );
457 /* Quit on unknown subtitles */
458 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
460 msg_Warn( p_demux, "failed to recognize subtitle type" );
467 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
469 msg_Dbg( p_demux, "detected %s format",
470 sub_read_subtitle_function[i].psz_name );
471 pf_read = sub_read_subtitle_function[i].pf_read;
476 msg_Dbg( p_demux, "loading all subtitles..." );
478 /* Load the whole file */
479 TextLoad( &p_sys->txt, p_demux->s );
484 if( p_sys->i_subtitles >= i_max )
487 if( !( p_sys->subtitle = realloc_or_free( p_sys->subtitle,
488 sizeof(subtitle_t) * i_max ) ) )
490 TextUnload( &p_sys->txt );
496 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
497 p_sys->i_subtitles ) )
500 p_sys->i_subtitles++;
503 TextUnload( &p_sys->txt );
505 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
507 /* Fix subtitle (order and time) *** */
508 p_sys->i_subtitle = 0;
510 if( p_sys->i_subtitles > 0 )
512 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
514 if( p_sys->i_length <= 0 )
515 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
518 /* *** add subtitle ES *** */
519 if( p_sys->i_type == SUB_TYPE_SSA1 ||
520 p_sys->i_type == SUB_TYPE_SSA2_4 ||
521 p_sys->i_type == SUB_TYPE_ASS )
523 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
527 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
529 if( p_sys->psz_header != NULL )
531 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
532 fmt.p_extra = strdup( p_sys->psz_header );
534 p_sys->es = es_out_Add( p_demux->out, &fmt );
539 /*****************************************************************************
540 * Close: Close subtitle demux
541 *****************************************************************************/
542 static void Close( vlc_object_t *p_this )
544 demux_t *p_demux = (demux_t*)p_this;
545 demux_sys_t *p_sys = p_demux->p_sys;
548 for( i = 0; i < p_sys->i_subtitles; i++ )
549 free( p_sys->subtitle[i].psz_text );
550 free( p_sys->subtitle );
555 /*****************************************************************************
557 *****************************************************************************/
558 static int Control( demux_t *p_demux, int i_query, va_list args )
560 demux_sys_t *p_sys = p_demux->p_sys;
566 case DEMUX_GET_LENGTH:
567 pi64 = (int64_t*)va_arg( args, int64_t * );
568 *pi64 = p_sys->i_length;
572 pi64 = (int64_t*)va_arg( args, int64_t * );
573 if( p_sys->i_subtitle < p_sys->i_subtitles )
575 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
581 i64 = (int64_t)va_arg( args, int64_t );
582 p_sys->i_subtitle = 0;
583 while( p_sys->i_subtitle < p_sys->i_subtitles )
585 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
587 if( p_subtitle->i_start > i64 )
589 if( p_subtitle->i_stop > p_subtitle->i_start && p_subtitle->i_stop > i64 )
595 if( p_sys->i_subtitle >= p_sys->i_subtitles )
599 case DEMUX_GET_POSITION:
600 pf = (double*)va_arg( args, double * );
601 if( p_sys->i_subtitle >= p_sys->i_subtitles )
605 else if( p_sys->i_subtitles > 0 )
607 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
608 (double)p_sys->i_length;
616 case DEMUX_SET_POSITION:
617 f = (double)va_arg( args, double );
618 i64 = f * p_sys->i_length;
620 p_sys->i_subtitle = 0;
621 while( p_sys->i_subtitle < p_sys->i_subtitles &&
622 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
626 if( p_sys->i_subtitle >= p_sys->i_subtitles )
630 case DEMUX_SET_NEXT_DEMUX_TIME:
631 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
636 case DEMUX_GET_ATTACHMENTS:
637 case DEMUX_GET_TITLE_INFO:
638 case DEMUX_HAS_UNSUPPORTED_META:
639 case DEMUX_CAN_RECORD:
643 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
648 /*****************************************************************************
649 * Demux: Send subtitle to decoder
650 *****************************************************************************/
651 static int Demux( demux_t *p_demux )
653 demux_sys_t *p_sys = p_demux->p_sys;
656 if( p_sys->i_subtitle >= p_sys->i_subtitles )
659 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
660 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
662 /* Should not happen */
663 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
666 while( p_sys->i_subtitle < p_sys->i_subtitles &&
667 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
669 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
672 int i_len = strlen( p_subtitle->psz_text ) + 1;
674 if( i_len <= 1 || p_subtitle->i_start < 0 )
680 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
687 p_block->i_pts = 1 + p_subtitle->i_start;
688 if( p_subtitle->i_stop > 0 && p_subtitle->i_stop >= p_subtitle->i_start )
689 p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start;
691 memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len );
693 es_out_Send( p_demux->out, p_sys->es, p_block );
699 p_sys->i_next_demux_date = 0;
704 /*****************************************************************************
705 * Fix: fix time stamp and order of subtitle
706 *****************************************************************************/
707 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
708 static void Fix( demux_t *p_demux )
710 demux_sys_t *p_sys = p_demux->p_sys;
714 /* *** fix order (to be sure...) *** */
715 /* We suppose that there are near in order and this durty bubble sort
716 * wont take too much time
721 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
723 if( p_sys->subtitle[i_index].i_start <
724 p_sys->subtitle[i_index - 1].i_start )
728 p_sys->subtitle + i_index - 1,
729 sizeof( subtitle_t ) );
730 memcpy( p_sys->subtitle + i_index - 1,
731 p_sys->subtitle + i_index,
732 sizeof( subtitle_t ) );
733 memcpy( p_sys->subtitle + i_index,
735 sizeof( subtitle_t ) );
743 static int TextLoad( text_t *txt, stream_t *s )
749 txt->i_line_count = 0;
751 txt->line = calloc( i_line_max, sizeof( char * ) );
755 /* load the complete file */
758 char *psz = stream_ReadLine( s );
763 txt->line[txt->i_line_count++] = psz;
764 if( txt->i_line_count >= i_line_max )
767 txt->line = realloc_or_free( txt->line, i_line_max * sizeof( char * ) );
774 if( txt->i_line_count <= 0 )
782 static void TextUnload( text_t *txt )
786 for( i = 0; i < txt->i_line_count; i++ )
788 free( txt->line[i] );
792 txt->i_line_count = 0;
795 static char *TextGetLine( text_t *txt )
797 if( txt->i_line >= txt->i_line_count )
800 return txt->line[txt->i_line++];
802 static void TextPreviousLine( text_t *txt )
804 if( txt->i_line > 0 )
808 /*****************************************************************************
809 * Specific Subtitle function
810 *****************************************************************************/
813 * {n1}{n2}Line1|Line2|Line3....
814 * where n1 and n2 are the video frame number (n2 can be empty)
816 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
820 demux_sys_t *p_sys = p_demux->p_sys;
821 text_t *txt = &p_sys->txt;
829 const char *s = TextGetLine( txt );
833 psz_text = malloc( strlen(s) + 1 );
839 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
840 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
843 if( i_start != 1 || i_stop != 1 )
846 /* We found a possible setting of the framerate "{1}{1}23.976" */
847 /* Check if it's usable, and if the sub-fps is not set */
848 f_fps = us_strtod( psz_text, NULL );
849 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
850 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
855 /* replace | by \n */
856 for( i = 0; psz_text[i] != '\0'; i++ )
858 if( psz_text[i] == '|' )
863 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
864 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
865 p_subtitle->psz_text = psz_text;
869 /* ParseSubRipSubViewer
872 * h1:m1:s1,d1 --> h2:m2:s2,d2
877 * Format SubViewer v1/v2
878 * h1:m1:s1.d1,h2:m2:s2.d2
883 * We ignore line number for SubRip
885 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
889 demux_sys_t *p_sys = p_demux->p_sys;
890 text_t *txt = &p_sys->txt;
895 const char *s = TextGetLine( txt );
896 int h1, m1, s1, d1, h2, m2, s2, d2;
901 if( sscanf( s, psz_fmt,
903 &h2, &m2, &s2, &d2 ) == 8 )
905 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
906 (int64_t)m1 * 60*1000 +
908 (int64_t)d1 ) * 1000;
910 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
911 (int64_t)m2 * 60*1000 +
913 (int64_t)d2 ) * 1000;
914 if( p_subtitle->i_start < p_subtitle->i_stop )
919 /* Now read text until an empty line */
920 psz_text = strdup("");
926 const char *s = TextGetLine( txt );
930 i_len = s ? strlen( s ) : 0;
933 p_subtitle->psz_text = psz_text;
937 i_old = strlen( psz_text );
938 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
943 strcat( psz_text, s );
944 strcat( psz_text, "\n" );
946 /* replace [br] by \n */
951 while( ( p = strstr( psz_text, "[br]" ) ) )
954 memmove( p, &p[3], strlen(&p[3])+1 );
961 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
965 return ParseSubRipSubViewer( p_demux, p_subtitle,
966 "%d:%d:%d,%d --> %d:%d:%d,%d",
970 * Special version for buggy file using '.' instead of ','
972 static int ParseSubRipDot( demux_t *p_demux, subtitle_t *p_subtitle,
976 return ParseSubRipSubViewer( p_demux, p_subtitle,
977 "%d:%d:%d.%d --> %d:%d:%d.%d",
982 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
987 return ParseSubRipSubViewer( p_demux, p_subtitle,
988 "%d:%d:%d.%d,%d:%d:%d.%d",
994 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
997 demux_sys_t *p_sys = p_demux->p_sys;
998 text_t *txt = &p_sys->txt;
1002 const char *s = TextGetLine( txt );
1003 int h1, m1, s1, c1, h2, m2, s2, c2;
1008 return VLC_EGENERIC;
1010 /* We expect (SSA2-4):
1011 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1012 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1014 * 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.
1018 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1019 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1022 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
1023 psz_text = malloc( strlen(s) );
1028 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
1034 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1035 /* (Layer comes from ASS specs ... it's empty for SSA.) */
1036 if( p_sys->i_type == SUB_TYPE_SSA1 )
1038 /* SSA1 has only 8 commas before the text starts, not 9 */
1039 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
1044 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
1046 /* ReadOrder, Layer, %s(rest of fields) */
1047 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
1048 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
1049 memcpy( psz_text, temp, strlen(temp) );
1052 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1053 (int64_t)m1 * 60*1000 +
1054 (int64_t)s1 * 1000 +
1055 (int64_t)c1 * 10 ) * 1000;
1056 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1057 (int64_t)m2 * 60*1000 +
1058 (int64_t)s2 * 1000 +
1059 (int64_t)c2 * 10 ) * 1000;
1060 p_subtitle->psz_text = psz_text;
1065 /* All the other stuff we add to the header field */
1066 if( !p_sys->psz_header )
1067 p_sys->psz_header = strdup( "" );
1068 if( !p_sys->psz_header )
1071 p_sys->psz_header = realloc_or_free( p_sys->psz_header,
1072 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
1073 assert( p_sys->psz_header );
1074 strcat( p_sys->psz_header, s );
1075 strcat( p_sys->psz_header, "\n" );
1081 * h:m:s:Line1|Line2|Line3....
1083 * h:m:s Line1|Line2|Line3....
1085 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1088 VLC_UNUSED( i_idx );
1090 demux_sys_t *p_sys = p_demux->p_sys;
1091 text_t *txt = &p_sys->txt;
1097 const char *s = TextGetLine( txt );
1101 return VLC_EGENERIC;
1103 psz_text = malloc( strlen( s ) + 1 );
1107 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1108 &h1, &m1, &s1, psz_text ) == 4 )
1110 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1111 (int64_t)m1 * 60*1000 +
1112 (int64_t)s1 * 1000 ) * 1000;
1113 p_subtitle->i_stop = 0;
1119 /* replace | by \n */
1120 for( i = 0; psz_text[i] != '\0'; i++ )
1122 if( psz_text[i] == '|' )
1125 p_subtitle->psz_text = psz_text;
1131 static char *ParseSamiSearch( text_t *txt,
1132 char *psz_start, const char *psz_str )
1134 if( psz_start && strcasestr( psz_start, psz_str ) )
1136 char *s = strcasestr( psz_start, psz_str );
1137 return &s[strlen( psz_str )];
1142 char *p = TextGetLine( txt );
1146 if( strcasestr( p, psz_str ) )
1148 char *s = strcasestr( p, psz_str );
1149 return &s[strlen( psz_str )];
1153 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1155 VLC_UNUSED( i_idx );
1156 demux_sys_t *p_sys = p_demux->p_sys;
1157 text_t *txt = &p_sys->txt;
1162 unsigned int i_text;
1163 char text[8192]; /* Arbitrary but should be long enough */
1165 /* search "Start=" */
1166 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1167 return VLC_EGENERIC;
1169 /* get start value */
1170 i_start = strtol( s, &s, 0 );
1173 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1174 return VLC_EGENERIC;
1177 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1178 return VLC_EGENERIC;
1182 /* now get all txt until a "Start=" line */
1186 /* Search non empty line */
1187 while( s && *s == '\0' )
1188 s = TextGetLine( txt );
1194 if( !strncasecmp( s, "<br", 3 ) )
1198 else if( strcasestr( s, "Start=" ) )
1200 TextPreviousLine( txt );
1203 s = ParseSamiSearch( txt, s, ">" );
1205 else if( !strncmp( s, " ", 6 ) )
1210 else if( *s == '\t' )
1220 if( c != '\0' && i_text+1 < sizeof(text) )
1223 text[i_text] = '\0';
1227 p_subtitle->i_start = i_start * 1000;
1228 p_subtitle->i_stop = 0;
1229 p_subtitle->psz_text = strdup( text );
1241 * TODO it can have a header
1248 * LANG support would be cool
1249 * CODEPAGE is probably mandatory FIXME
1251 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1254 VLC_UNUSED( i_idx );
1256 demux_sys_t *p_sys = p_demux->p_sys;
1257 text_t *txt = &p_sys->txt;
1262 const char *s = TextGetLine( txt );
1266 return VLC_EGENERIC;
1270 &h1, &m1, &s1, &c1 ) == 4 )
1272 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1273 (int64_t)m1 * 60*1000 +
1274 (int64_t)s1 * 1000 +
1275 (int64_t)c1 * 10) * 1000;
1276 p_subtitle->i_stop = 0;
1281 /* Now read text until a line containing "}" */
1282 psz_text = strdup("");
1287 const char *s = TextGetLine( txt );
1294 return VLC_EGENERIC;
1297 i_len = strlen( s );
1298 if( i_len == 1 && s[0] == '}')
1300 p_subtitle->psz_text = psz_text;
1304 i_old = strlen( psz_text );
1305 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1308 strcat( psz_text, s );
1309 strcat( psz_text, "\n" );
1315 * [n1][n2]Line1|Line2|Line3...
1316 * where n1 and n2 are the video frame number (n2 can be empty)
1318 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1320 VLC_UNUSED( i_idx );
1322 demux_sys_t *p_sys = p_demux->p_sys;
1323 text_t *txt = &p_sys->txt;
1329 const char *s = TextGetLine( txt );
1334 return VLC_EGENERIC;
1336 psz_text = malloc( strlen(s) + 1 );
1342 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1343 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1345 p_subtitle->i_start = (int64_t)i_start * 100000;
1346 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1352 for( i = 0; psz_text[i] != '\0'; )
1354 /* replace | by \n */
1355 if( psz_text[i] == '|' )
1359 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1360 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1364 p_subtitle->psz_text = psz_text;
1368 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1370 VLC_UNUSED( i_idx );
1372 demux_sys_t *p_sys = p_demux->p_sys;
1373 text_t *txt = &p_sys->txt;
1374 char *psz_text = strdup( "" );
1376 int i_firstline = 1;
1382 const char *s = TextGetLine( txt );
1387 return VLC_EGENERIC;
1391 if( sscanf (s, "-->> %d", &t) == 1)
1393 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1394 p_subtitle->i_stop = 0;
1396 /* Starting of a subtitle */
1401 /* We have been too far: end of the subtitle, begin of next */
1404 TextPreviousLine( txt );
1411 i_old = strlen( psz_text ) + 1;
1412 psz_text = realloc_or_free( psz_text, i_old + strlen( s ) + 1 );
1415 strcat( psz_text, s );
1416 strcat( psz_text, "\n" );
1417 if( txt->i_line == txt->i_line_count )
1421 p_subtitle->psz_text = psz_text;
1425 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1427 VLC_UNUSED( i_idx );
1429 demux_sys_t *p_sys = p_demux->p_sys;
1430 text_t *txt = &p_sys->txt;
1436 const char *s = TextGetLine( txt );
1440 return VLC_EGENERIC;
1442 psz_text = malloc( strlen(s) + 1 );
1447 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1449 /* 1/10th of second ? Frame based ? FIXME */
1450 p_subtitle->i_start = 10 * t1;
1451 p_subtitle->i_stop = 10 * t2;
1452 /* Remove latest " */
1453 psz_text[ strlen(psz_text) - 1 ] = '\0';
1460 /* replace | by \n */
1461 for( i = 0; psz_text[i] != '\0'; i++ )
1463 if( psz_text[i] == '|' )
1467 p_subtitle->psz_text = psz_text;
1468 msg_Dbg( p_demux, "%s", psz_text );
1472 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1474 VLC_UNUSED( i_idx );
1476 demux_sys_t *p_sys = p_demux->p_sys;
1477 text_t *txt = &p_sys->txt;
1478 char *psz_text = strdup( "" );
1480 if( !p_sys->mpsub.b_inited )
1482 p_sys->mpsub.f_total = 0.0;
1483 p_sys->mpsub.f_factor = 0.0;
1485 p_sys->mpsub.b_inited = true;
1494 const char *s = TextGetLine( txt );
1498 return VLC_EGENERIC;
1501 if( strstr( s, "FORMAT" ) )
1503 if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1505 p_sys->mpsub.f_factor = 100.0;
1509 psz_temp = malloc( strlen(s) );
1516 if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1519 f_fps = us_strtod( psz_temp, NULL );
1520 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1521 var_SetFloat( p_demux, "sub-fps", f_fps );
1523 p_sys->mpsub.f_factor = 1.0;
1530 f1 = us_strtod( s, &psz_temp );
1533 f2 = us_strtod( psz_temp, NULL );
1534 p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor;
1535 p_subtitle->i_start = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1536 p_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor;
1537 p_subtitle->i_stop = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1544 const char *s = TextGetLine( txt );
1549 return VLC_EGENERIC;
1552 int i_len = strlen( s );
1556 int i_old = strlen( psz_text );
1558 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1562 strcat( psz_text, s );
1563 strcat( psz_text, "\n" );
1566 p_subtitle->psz_text = psz_text;
1570 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1572 VLC_UNUSED( i_idx );
1574 demux_sys_t *p_sys = p_demux->p_sys;
1575 text_t *txt = &p_sys->txt;
1576 char *psz_text, *psz_orig;
1577 char *psz_text2, *psz_orig2;
1578 int h1, h2, m1, m2, s1, s2, f1, f2;
1580 if( !p_sys->jss.b_inited )
1582 p_sys->jss.i_comment = 0;
1583 p_sys->jss.i_time_resolution = 30;
1584 p_sys->jss.i_time_shift = 0;
1586 p_sys->jss.b_inited = true;
1589 /* Parse the main lines */
1592 const char *s = TextGetLine( txt );
1594 return VLC_EGENERIC;
1596 psz_orig = malloc( strlen( s ) + 1 );
1599 psz_text = psz_orig;
1601 /* Complete time lines */
1602 if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1603 &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1605 p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1606 (int64_t)( ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) )
1608 p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1609 (int64_t)( ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) )
1613 /* Short time lines */
1614 else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1616 p_subtitle->i_start = (int64_t)(
1617 ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1618 p_subtitle->i_stop = (int64_t)(
1619 ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1622 /* General Directive lines */
1623 /* Only TIME and SHIFT are supported so far */
1624 else if( s[0] == '#' )
1626 int h = 0, m =0, sec = 1, f = 1;
1630 strcpy( psz_text, s );
1632 switch( toupper( psz_text[1] ) )
1635 shift = isalpha( psz_text[2] ) ? 6 : 2 ;
1637 if( sscanf( &psz_text[shift], "%d", &h ) )
1639 /* Negative shifting */
1646 if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1648 if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1650 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1655 sscanf( &psz_text[shift], "%d:%d.%d",
1663 sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1666 p_sys->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec )
1667 * p_sys->jss.i_time_resolution + f ) * inv;
1672 shift = isalpha( psz_text[2] ) ? 8 : 2 ;
1674 sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
1681 /* Unkown type line, probably a comment */
1688 while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
1690 const char *s2 = TextGetLine( txt );
1695 return VLC_EGENERIC;
1698 int i_len = strlen( s2 );
1702 int i_old = strlen( psz_text );
1704 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 );
1708 psz_orig = psz_text;
1709 strcat( psz_text, s2 );
1712 /* Skip the blanks */
1713 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1715 /* Parse the directives */
1716 if( isalpha( *psz_text ) || *psz_text == '[' )
1718 while( *psz_text != ' ' )
1721 /* Directives are NOT parsed yet */
1722 /* This has probably a better place in a decoder ? */
1723 /* directive = malloc( strlen( psz_text ) + 1 );
1724 assert( directive );
1725 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1728 /* Skip the blanks after directives */
1729 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1731 /* Clean all the lines from inline comments and other stuffs */
1732 psz_orig2 = calloc( strlen( psz_text) + 1, 1 );
1733 psz_text2 = psz_orig2;
1735 for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1740 p_sys->jss.i_comment++;
1743 if( p_sys->jss.i_comment )
1745 p_sys->jss.i_comment = 0;
1746 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1750 if( !p_sys->jss.i_comment )
1758 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1760 if( !p_sys->jss.i_comment )
1767 if( (*(psz_text + 1 ) ) == 'n' )
1774 if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
1775 ( toupper(*(psz_text + 1 ) ) == 'F' ) )
1777 psz_text++; psz_text++;
1780 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1781 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1782 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1783 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1788 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1789 (*(psz_text + 1 ) ) == '\\' )
1791 else if( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n' ||
1792 *(psz_text + 1 ) == '\0' )
1798 if( !p_sys->jss.i_comment )
1800 *psz_text2 = *psz_text;
1807 p_subtitle->psz_text = psz_orig2;
1808 msg_Dbg( p_demux, "%s", p_subtitle->psz_text );
1813 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1815 VLC_UNUSED( i_idx );
1817 demux_sys_t *p_sys = p_demux->p_sys;
1818 text_t *txt = &p_sys->txt;
1826 const char *s = TextGetLine( txt );
1829 return VLC_EGENERIC;
1831 psz_text = malloc( strlen( s ) + 1 );
1835 if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1836 &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1838 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1839 (int64_t)m1 * 60*1000 +
1840 (int64_t)s1 * 1000 ) * 1000;
1841 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1842 (int64_t)m2 * 60*1000 +
1843 (int64_t)s2 * 1000 ) * 1000;
1849 /* replace | by \n */
1850 for( i = 0; psz_text[i] != '\0'; i++ )
1852 if( psz_text[i] == '|' )
1855 p_subtitle->psz_text = psz_text;
1859 static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
1861 if( strlen( psz ) == 0 ) return 0;
1862 if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
1863 sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
1864 sscanf( psz, "%d.%d", s, f ) == 2 ||
1865 sscanf( psz, "%d:%d", m, s ) == 2 ||
1866 sscanf( psz, "%d", s ) == 1 )
1868 return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
1869 + (int64_t)*f * 10 * 1000;
1871 else return VLC_EGENERIC;
1874 static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1876 VLC_UNUSED( i_idx );
1877 demux_sys_t *p_sys = p_demux->p_sys;
1878 text_t *txt = &p_sys->txt;
1879 char *psz_text = NULL;
1883 int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
1884 int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
1885 const char *s = TextGetLine( txt );
1889 return VLC_EGENERIC;
1891 psz_text = malloc( strlen( s ) + 1 );
1895 /* Find the good begining. This removes extra spaces at the beginning
1897 char *psz_temp = strcasestr( s, "<time");
1898 if( psz_temp != NULL )
1900 char psz_end[12], psz_begin[12];
1901 /* Line has begin and end */
1902 if( ( sscanf( psz_temp,
1903 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\n\r]",
1904 psz_begin, psz_end, psz_text) != 3 ) &&
1905 /* Line has begin and no end */
1907 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\n\r]",
1908 psz_begin, psz_text ) != 2) )
1909 /* Line is not recognized */
1915 int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
1918 p_subtitle->i_start = i_time;
1921 i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
1924 p_subtitle->i_stop = i_time;
1930 /* Get the following Lines */
1933 const char *s = TextGetLine( txt );
1938 return VLC_EGENERIC;
1941 int i_len = strlen( s );
1942 if( i_len == 0 ) break;
1944 if( strcasestr( s, "<time" ) ||
1945 strcasestr( s, "<clear/") )
1947 TextPreviousLine( txt );
1951 int i_old = strlen( psz_text );
1953 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1957 strcat( psz_text, s );
1958 strcat( psz_text, "\n" );
1961 /* Remove the starting ">" that remained after the sscanf */
1962 memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
1964 p_subtitle->psz_text = psz_text;
1969 static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1971 VLC_UNUSED( i_idx );
1973 demux_sys_t *p_sys = p_demux->p_sys;
1974 text_t *txt = &p_sys->txt;
1981 char *s = TextGetLine( txt );
1984 return VLC_EGENERIC;
1986 psz_text = malloc( strlen( s ) + 1 );
1990 if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
1991 &h1, &m1, &s1, psz_text ) == 4 )
1993 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1994 (int64_t)m1 * 60*1000 +
1995 (int64_t)s1 * 1000 ) * 1000;
1997 char *s = TextGetLine( txt );
2001 return VLC_EGENERIC;
2004 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2005 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2006 (int64_t)m2 * 60*1000 +
2007 (int64_t)s2 * 1000 ) * 1000;
2013 /* replace [br] by \n */
2015 while( ( p = strstr( psz_text, "[br]" ) ) )
2018 memmove( p, &p[3], strlen(&p[3])+1 );
2021 p_subtitle->psz_text = psz_text;
2025 static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2027 VLC_UNUSED( i_idx );
2029 demux_sys_t *p_sys = p_demux->p_sys;
2030 text_t *txt = &p_sys->txt;
2037 char *s = TextGetLine( txt );
2040 return VLC_EGENERIC;
2042 if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
2044 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2045 (int64_t)m1 * 60*1000 +
2046 (int64_t)s1 * 1000 ) * 1000;
2048 char *s = TextGetLine( txt );
2050 return VLC_EGENERIC;
2052 psz_text = strdup( s );
2056 s = TextGetLine( txt );
2060 return VLC_EGENERIC;
2063 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2064 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2065 (int64_t)m2 * 60*1000 +
2066 (int64_t)s2 * 1000 ) * 1000;
2071 p_subtitle->psz_text = psz_text;