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>
41 #include <vlc_demux.h>
42 #include <vlc_charset.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t *p_this );
48 static void Close( vlc_object_t *p_this );
50 #define SUB_DELAY_LONGTEXT \
51 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
52 #define SUB_FPS_LONGTEXT \
53 N_("Override the normal frames per second settings. " \
54 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
55 #define SUB_TYPE_LONGTEXT \
56 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
57 "\"subrip\", \"subviewer\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\", " \
58 "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\", "\
59 "\"mpsub\", \"jacosub\", \"psb\", \"realtext\", \"dks\", \"subviewer1\", " \
60 " and \"auto\" (meaning autodetection, this should always work).")
62 static const char *const ppsz_sub_type[] =
64 "auto", "microdvd", "subrip", "subviewer", "ssa1",
65 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
66 "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
71 set_shortname( N_("Subtitles"))
72 set_description( N_("Text subtitles parser") )
73 set_capability( "demux", 0 )
74 set_category( CAT_INPUT )
75 set_subcategory( SUBCAT_INPUT_DEMUX )
76 add_float( "sub-fps", 0.0, NULL,
77 N_("Frames per second"),
78 SUB_FPS_LONGTEXT, true )
79 add_integer( "sub-delay", 0, NULL,
80 N_("Subtitles delay"),
81 SUB_DELAY_LONGTEXT, true )
82 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
83 SUB_TYPE_LONGTEXT, true )
84 change_string_list( ppsz_sub_type, NULL, NULL )
85 set_callbacks( Open, Close )
87 add_shortcut( "subtitle" )
90 /*****************************************************************************
92 *****************************************************************************/
95 SUB_TYPE_UNKNOWN = -1,
98 SUB_TYPE_SUBRIP_DOT, /* Invalid SubRip file (dot instead of comma) */
104 SUB_TYPE_SUBVIEWER, /* SUBVIEWER 2 */
105 SUB_TYPE_DVDSUBTITLE, /* Mplayer calls it subviewer2 */
114 SUB_TYPE_SUBVIEW1 /* SUBVIEWER 1 - mplayer calls it subrip09,
115 and Gnome subtitles SubViewer 1.0 */
125 static int TextLoad( text_t *, stream_t *s );
126 static void TextUnload( text_t * );
143 int64_t i_next_demux_date;
144 int64_t i_microsecperframe;
149 subtitle_t *subtitle;
159 int i_time_resolution;
171 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
172 static int ParseSubRip ( demux_t *, subtitle_t *, int );
173 static int ParseSubRipDot ( demux_t *, subtitle_t *, int );
174 static int ParseSubViewer ( demux_t *, subtitle_t *, int );
175 static int ParseSSA ( demux_t *, subtitle_t *, int );
176 static int ParseVplayer ( demux_t *, subtitle_t *, int );
177 static int ParseSami ( demux_t *, subtitle_t *, int );
178 static int ParseDVDSubtitle( demux_t *, subtitle_t *, int );
179 static int ParseMPL2 ( demux_t *, subtitle_t *, int );
180 static int ParseAQT ( demux_t *, subtitle_t *, int );
181 static int ParsePJS ( demux_t *, subtitle_t *, int );
182 static int ParseMPSub ( demux_t *, subtitle_t *, int );
183 static int ParseJSS ( demux_t *, subtitle_t *, int );
184 static int ParsePSB ( demux_t *, subtitle_t *, int );
185 static int ParseRealText ( demux_t *, subtitle_t *, int );
186 static int ParseDKS ( demux_t *, subtitle_t *, int );
187 static int ParseSubViewer1 ( demux_t *, subtitle_t *, int );
191 const char *psz_type_name;
193 const char *psz_name;
194 int (*pf_read)( demux_t *, subtitle_t*, int );
195 } sub_read_subtitle_function [] =
197 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
198 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
199 { "subrip-dot", SUB_TYPE_SUBRIP_DOT, "SubRIP(Dot)", ParseSubRipDot },
200 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
201 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
202 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
203 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
204 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
205 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
206 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
207 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
208 { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT },
209 { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS },
210 { "mpsub", SUB_TYPE_MPSUB, "MPSub", ParseMPSub },
211 { "jacosub", SUB_TYPE_JACOSUB, "JacoSub", ParseJSS },
212 { "psb", SUB_TYPE_PSB, "PowerDivx", ParsePSB },
213 { "realtext", SUB_TYPE_RT, "RealText", ParseRealText },
214 { "dks", SUB_TYPE_DKS, "DKS", ParseDKS },
215 { "subviewer1", SUB_TYPE_SUBVIEW1, "Subviewer 1", ParseSubViewer1 },
216 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
218 /* When adding support for more formats, be sure to add their file extension
219 * to src/input/subtitles.c to enable auto-detection.
222 static int Demux( demux_t * );
223 static int Control( demux_t *, int, va_list );
225 /*static void Fix( demux_t * );*/
227 /*****************************************************************************
229 *****************************************************************************/
230 static int Open ( vlc_object_t *p_this )
232 demux_t *p_demux = (demux_t*)p_this;
237 int (*pf_read)( demux_t *, subtitle_t*, int );
240 if( !p_demux->b_force )
242 msg_Dbg( p_demux, "subtitle demux discarded" );
246 p_demux->pf_demux = Demux;
247 p_demux->pf_control = Control;
248 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
252 p_sys->psz_header = NULL;
253 p_sys->i_subtitle = 0;
254 p_sys->i_subtitles = 0;
255 p_sys->subtitle = NULL;
256 p_sys->i_microsecperframe = 40000;
258 p_sys->jss.b_inited = false;
259 p_sys->mpsub.b_inited = false;
262 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); /* FIXME */
264 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
266 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
268 /* Check for override of the fps */
269 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
272 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
273 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
276 /* Get or probe the type */
277 p_sys->i_type = SUB_TYPE_UNKNOWN;
278 psz_type = var_CreateGetString( p_demux, "sub-type" );
279 if( psz_type && *psz_type )
285 if( sub_read_subtitle_function[i].psz_type_name == NULL )
288 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
291 p_sys->i_type = sub_read_subtitle_function[i].i_type;
298 /* Probe if unknown type */
299 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
304 msg_Dbg( p_demux, "autodetecting subtitle format" );
305 for( i_try = 0; i_try < 256; i_try++ )
310 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
313 if( strcasestr( s, "<SAMI>" ) )
315 p_sys->i_type = SUB_TYPE_SAMI;
318 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
319 sscanf( s, "{%d}{}", &i_dummy ) == 1)
321 p_sys->i_type = SUB_TYPE_MICRODVD;
325 "%d:%d:%d,%d --> %d:%d:%d,%d",
326 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
327 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
329 p_sys->i_type = SUB_TYPE_SUBRIP;
333 "%d:%d:%d.%d --> %d:%d:%d.%d",
334 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
335 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
337 msg_Err( p_demux, "Detected invalid SubRip file, playing anyway" );
338 p_sys->i_type = SUB_TYPE_SUBRIP_DOT;
341 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
343 p_sys->i_type = SUB_TYPE_SSA1;
346 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
348 p_sys->i_type = SUB_TYPE_ASS;
351 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
353 p_sys->i_type = SUB_TYPE_SSA2_4;
356 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
358 p_sys->i_type = SUB_TYPE_SSA2_4;
361 else if( !strncasecmp( s, "Dialogue:", 9 ) )
363 p_sys->i_type = SUB_TYPE_ASS;
366 else if( strcasestr( s, "[INFORMATION]" ) )
368 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
371 else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d",
372 &i_dummy, &i_dummy, &i_dummy, &i_dummy,
373 &i_dummy, &i_dummy, &i_dummy ) == 7 ||
374 sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
376 p_sys->i_type = SUB_TYPE_JACOSUB;
379 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
380 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
382 p_sys->i_type = SUB_TYPE_VPLAYER;
385 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
386 &i_dummy, &i_dummy ) == 4 )
388 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
391 else if( sscanf( s, "[%d:%d:%d]%c",
392 &i_dummy, &i_dummy, &i_dummy, &p_dummy ) == 4 )
394 p_sys->i_type = SUB_TYPE_DKS;
397 else if( strstr( s, "*** START SCRIPT" ) )
399 p_sys->i_type = SUB_TYPE_SUBVIEW1;
402 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
403 sscanf( s, "[%d][]", &i_dummy ) == 1)
405 p_sys->i_type = SUB_TYPE_MPL2;
408 else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
409 ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
412 p_sys->i_type = SUB_TYPE_MPSUB;
415 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
417 p_sys->i_type = SUB_TYPE_AQT;
420 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
422 p_sys->i_type = SUB_TYPE_PJS;
425 else if( sscanf( s, "{%d:%d:%d}",
426 &i_dummy, &i_dummy, &i_dummy ) == 3 )
428 p_sys->i_type = SUB_TYPE_PSB;
431 else if( strcasestr( s, "<time" ) )
433 p_sys->i_type = SUB_TYPE_RT;
443 /* It will nearly always work even for non seekable stream thanks the
444 * caching system, and if it fails we lose just a few sub */
445 if( stream_Seek( p_demux->s, 0 ) )
447 msg_Warn( p_demux, "failed to rewind" );
451 /* Quit on unknown subtitles */
452 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
454 msg_Warn( p_demux, "failed to recognize subtitle type" );
461 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
463 msg_Dbg( p_demux, "detected %s format",
464 sub_read_subtitle_function[i].psz_name );
465 pf_read = sub_read_subtitle_function[i].pf_read;
470 msg_Dbg( p_demux, "loading all subtitles..." );
472 /* Load the whole file */
473 TextLoad( &p_sys->txt, p_demux->s );
478 if( p_sys->i_subtitles >= i_max )
481 if( !( p_sys->subtitle = realloc_or_free( p_sys->subtitle,
482 sizeof(subtitle_t) * i_max ) ) )
484 TextUnload( &p_sys->txt );
490 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
491 p_sys->i_subtitles ) )
494 p_sys->i_subtitles++;
497 TextUnload( &p_sys->txt );
499 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
501 /* Fix subtitle (order and time) *** */
502 p_sys->i_subtitle = 0;
504 if( p_sys->i_subtitles > 0 )
506 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
508 if( p_sys->i_length <= 0 )
509 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
512 /* *** add subtitle ES *** */
513 if( p_sys->i_type == SUB_TYPE_SSA1 ||
514 p_sys->i_type == SUB_TYPE_SSA2_4 ||
515 p_sys->i_type == SUB_TYPE_ASS )
517 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
521 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
523 if( p_sys->psz_header != NULL )
525 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
526 fmt.p_extra = strdup( p_sys->psz_header );
528 p_sys->es = es_out_Add( p_demux->out, &fmt );
533 /*****************************************************************************
534 * Close: Close subtitle demux
535 *****************************************************************************/
536 static void Close( vlc_object_t *p_this )
538 demux_t *p_demux = (demux_t*)p_this;
539 demux_sys_t *p_sys = p_demux->p_sys;
542 for( i = 0; i < p_sys->i_subtitles; i++ )
543 free( p_sys->subtitle[i].psz_text );
544 free( p_sys->subtitle );
549 /*****************************************************************************
551 *****************************************************************************/
552 static int Control( demux_t *p_demux, int i_query, va_list args )
554 demux_sys_t *p_sys = p_demux->p_sys;
560 case DEMUX_GET_LENGTH:
561 pi64 = (int64_t*)va_arg( args, int64_t * );
562 *pi64 = p_sys->i_length;
566 pi64 = (int64_t*)va_arg( args, int64_t * );
567 if( p_sys->i_subtitle < p_sys->i_subtitles )
569 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
575 i64 = (int64_t)va_arg( args, int64_t );
576 p_sys->i_subtitle = 0;
577 while( p_sys->i_subtitle < p_sys->i_subtitles )
579 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
581 if( p_subtitle->i_start > i64 )
583 if( p_subtitle->i_stop > p_subtitle->i_start && p_subtitle->i_stop > i64 )
589 if( p_sys->i_subtitle >= p_sys->i_subtitles )
593 case DEMUX_GET_POSITION:
594 pf = (double*)va_arg( args, double * );
595 if( p_sys->i_subtitle >= p_sys->i_subtitles )
599 else if( p_sys->i_subtitles > 0 )
601 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
602 (double)p_sys->i_length;
610 case DEMUX_SET_POSITION:
611 f = (double)va_arg( args, double );
612 i64 = f * p_sys->i_length;
614 p_sys->i_subtitle = 0;
615 while( p_sys->i_subtitle < p_sys->i_subtitles &&
616 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
620 if( p_sys->i_subtitle >= p_sys->i_subtitles )
624 case DEMUX_SET_NEXT_DEMUX_TIME:
625 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
630 case DEMUX_GET_ATTACHMENTS:
631 case DEMUX_GET_TITLE_INFO:
632 case DEMUX_HAS_UNSUPPORTED_META:
633 case DEMUX_CAN_RECORD:
637 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
642 /*****************************************************************************
643 * Demux: Send subtitle to decoder
644 *****************************************************************************/
645 static int Demux( demux_t *p_demux )
647 demux_sys_t *p_sys = p_demux->p_sys;
650 if( p_sys->i_subtitle >= p_sys->i_subtitles )
653 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
654 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
656 /* Should not happen */
657 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
660 while( p_sys->i_subtitle < p_sys->i_subtitles &&
661 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
663 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
666 int i_len = strlen( p_subtitle->psz_text ) + 1;
668 if( i_len <= 1 || p_subtitle->i_start < 0 )
674 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
681 p_block->i_pts = VLC_TS_0 + p_subtitle->i_start;
682 if( p_subtitle->i_stop >= 0 && p_subtitle->i_stop >= p_subtitle->i_start )
683 p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start;
685 memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len );
687 es_out_Send( p_demux->out, p_sys->es, p_block );
693 p_sys->i_next_demux_date = 0;
698 /*****************************************************************************
699 * Fix: fix time stamp and order of subtitle
700 *****************************************************************************/
701 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
702 static void Fix( demux_t *p_demux )
704 demux_sys_t *p_sys = p_demux->p_sys;
708 /* *** fix order (to be sure...) *** */
709 /* We suppose that there are near in order and this durty bubble sort
710 * wont take too much time
715 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
717 if( p_sys->subtitle[i_index].i_start <
718 p_sys->subtitle[i_index - 1].i_start )
722 p_sys->subtitle + i_index - 1,
723 sizeof( subtitle_t ) );
724 memcpy( p_sys->subtitle + i_index - 1,
725 p_sys->subtitle + i_index,
726 sizeof( subtitle_t ) );
727 memcpy( p_sys->subtitle + i_index,
729 sizeof( subtitle_t ) );
737 static int TextLoad( text_t *txt, stream_t *s )
743 txt->i_line_count = 0;
745 txt->line = calloc( i_line_max, sizeof( char * ) );
749 /* load the complete file */
752 char *psz = stream_ReadLine( s );
757 txt->line[txt->i_line_count++] = psz;
758 if( txt->i_line_count >= i_line_max )
761 txt->line = realloc_or_free( txt->line, i_line_max * sizeof( char * ) );
767 if( txt->i_line_count <= 0 )
775 static void TextUnload( text_t *txt )
779 for( i = 0; i < txt->i_line_count; i++ )
781 free( txt->line[i] );
785 txt->i_line_count = 0;
788 static char *TextGetLine( text_t *txt )
790 if( txt->i_line >= txt->i_line_count )
793 return txt->line[txt->i_line++];
795 static void TextPreviousLine( text_t *txt )
797 if( txt->i_line > 0 )
801 /*****************************************************************************
802 * Specific Subtitle function
803 *****************************************************************************/
806 * {n1}{n2}Line1|Line2|Line3....
807 * where n1 and n2 are the video frame number (n2 can be empty)
809 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
813 demux_sys_t *p_sys = p_demux->p_sys;
814 text_t *txt = &p_sys->txt;
822 const char *s = TextGetLine( txt );
826 psz_text = malloc( strlen(s) + 1 );
832 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
833 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
836 if( i_start != 1 || i_stop != 1 )
839 /* We found a possible setting of the framerate "{1}{1}23.976" */
840 /* Check if it's usable, and if the sub-fps is not set */
841 f_fps = us_strtod( psz_text, NULL );
842 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
843 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
848 /* replace | by \n */
849 for( i = 0; psz_text[i] != '\0'; i++ )
851 if( psz_text[i] == '|' )
856 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
857 p_subtitle->i_stop = i_stop >= 0 ? (i_stop * p_sys->i_microsecperframe) : -1;
858 p_subtitle->psz_text = psz_text;
862 /* ParseSubRipSubViewer
865 * h1:m1:s1,d1 --> h2:m2:s2,d2
870 * Format SubViewer v1/v2
871 * h1:m1:s1.d1,h2:m2:s2.d2
876 * We ignore line number for SubRip
878 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
882 demux_sys_t *p_sys = p_demux->p_sys;
883 text_t *txt = &p_sys->txt;
888 const char *s = TextGetLine( txt );
889 int h1, m1, s1, d1, h2, m2, s2, d2;
894 if( sscanf( s, psz_fmt,
896 &h2, &m2, &s2, &d2 ) == 8 )
898 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
899 (int64_t)m1 * 60*1000 +
901 (int64_t)d1 ) * 1000;
903 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
904 (int64_t)m2 * 60*1000 +
906 (int64_t)d2 ) * 1000;
907 if( p_subtitle->i_start < p_subtitle->i_stop )
912 /* Now read text until an empty line */
913 psz_text = strdup("");
919 const char *s = TextGetLine( txt );
923 i_len = s ? strlen( s ) : 0;
926 p_subtitle->psz_text = psz_text;
930 i_old = strlen( psz_text );
931 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
936 strcat( psz_text, s );
937 strcat( psz_text, "\n" );
939 /* replace [br] by \n */
944 while( ( p = strstr( psz_text, "[br]" ) ) )
947 memmove( p, &p[3], strlen(&p[3])+1 );
954 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
958 return ParseSubRipSubViewer( p_demux, p_subtitle,
959 "%d:%d:%d,%d --> %d:%d:%d,%d",
963 * Special version for buggy file using '.' instead of ','
965 static int ParseSubRipDot( demux_t *p_demux, subtitle_t *p_subtitle,
969 return ParseSubRipSubViewer( p_demux, p_subtitle,
970 "%d:%d:%d.%d --> %d:%d:%d.%d",
975 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
980 return ParseSubRipSubViewer( p_demux, p_subtitle,
981 "%d:%d:%d.%d,%d:%d:%d.%d",
987 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
990 demux_sys_t *p_sys = p_demux->p_sys;
991 text_t *txt = &p_sys->txt;
995 const char *s = TextGetLine( txt );
996 int h1, m1, s1, c1, h2, m2, s2, c2;
1001 return VLC_EGENERIC;
1003 /* We expect (SSA2-4):
1004 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1005 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1007 * 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.
1011 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1012 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1015 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
1016 psz_text = malloc( strlen(s) );
1021 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
1027 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1028 /* (Layer comes from ASS specs ... it's empty for SSA.) */
1029 if( p_sys->i_type == SUB_TYPE_SSA1 )
1031 /* SSA1 has only 8 commas before the text starts, not 9 */
1032 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
1037 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
1039 /* ReadOrder, Layer, %s(rest of fields) */
1040 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
1041 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
1042 memcpy( psz_text, temp, strlen(temp) );
1045 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1046 (int64_t)m1 * 60*1000 +
1047 (int64_t)s1 * 1000 +
1048 (int64_t)c1 * 10 ) * 1000;
1049 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1050 (int64_t)m2 * 60*1000 +
1051 (int64_t)s2 * 1000 +
1052 (int64_t)c2 * 10 ) * 1000;
1053 p_subtitle->psz_text = psz_text;
1058 /* All the other stuff we add to the header field */
1060 if( asprintf( &psz_header, "%s%s\n",
1061 p_sys->psz_header ? p_sys->psz_header : "", s ) == -1 )
1063 p_sys->psz_header = psz_header;
1069 * h:m:s:Line1|Line2|Line3....
1071 * h:m:s Line1|Line2|Line3....
1073 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1076 VLC_UNUSED( i_idx );
1078 demux_sys_t *p_sys = p_demux->p_sys;
1079 text_t *txt = &p_sys->txt;
1085 const char *s = TextGetLine( txt );
1089 return VLC_EGENERIC;
1091 psz_text = malloc( strlen( s ) + 1 );
1095 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1096 &h1, &m1, &s1, psz_text ) == 4 )
1098 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1099 (int64_t)m1 * 60*1000 +
1100 (int64_t)s1 * 1000 ) * 1000;
1101 p_subtitle->i_stop = -1;
1107 /* replace | by \n */
1108 for( i = 0; psz_text[i] != '\0'; i++ )
1110 if( psz_text[i] == '|' )
1113 p_subtitle->psz_text = psz_text;
1119 static char *ParseSamiSearch( text_t *txt,
1120 char *psz_start, const char *psz_str )
1122 if( psz_start && strcasestr( psz_start, psz_str ) )
1124 char *s = strcasestr( psz_start, psz_str );
1125 return &s[strlen( psz_str )];
1130 char *p = TextGetLine( txt );
1134 if( strcasestr( p, psz_str ) )
1136 char *s = strcasestr( p, psz_str );
1137 return &s[strlen( psz_str )];
1141 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1143 VLC_UNUSED( i_idx );
1144 demux_sys_t *p_sys = p_demux->p_sys;
1145 text_t *txt = &p_sys->txt;
1150 unsigned int i_text;
1151 char text[8192]; /* Arbitrary but should be long enough */
1153 /* search "Start=" */
1154 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1155 return VLC_EGENERIC;
1157 /* get start value */
1158 i_start = strtol( s, &s, 0 );
1161 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1162 return VLC_EGENERIC;
1165 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1166 return VLC_EGENERIC;
1170 /* now get all txt until a "Start=" line */
1174 /* Search non empty line */
1175 while( s && *s == '\0' )
1176 s = TextGetLine( txt );
1182 if( !strncasecmp( s, "<br", 3 ) )
1186 else if( strcasestr( s, "Start=" ) )
1188 TextPreviousLine( txt );
1191 s = ParseSamiSearch( txt, s, ">" );
1193 else if( !strncmp( s, " ", 6 ) )
1198 else if( *s == '\t' )
1208 if( c != '\0' && i_text+1 < sizeof(text) )
1211 text[i_text] = '\0';
1215 p_subtitle->i_start = i_start * 1000;
1216 p_subtitle->i_stop = -1;
1217 p_subtitle->psz_text = strdup( text );
1229 * TODO it can have a header
1236 * LANG support would be cool
1237 * CODEPAGE is probably mandatory FIXME
1239 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1242 VLC_UNUSED( i_idx );
1244 demux_sys_t *p_sys = p_demux->p_sys;
1245 text_t *txt = &p_sys->txt;
1250 const char *s = TextGetLine( txt );
1254 return VLC_EGENERIC;
1258 &h1, &m1, &s1, &c1 ) == 4 )
1260 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1261 (int64_t)m1 * 60*1000 +
1262 (int64_t)s1 * 1000 +
1263 (int64_t)c1 * 10) * 1000;
1264 p_subtitle->i_stop = -1;
1269 /* Now read text until a line containing "}" */
1270 psz_text = strdup("");
1275 const char *s = TextGetLine( txt );
1282 return VLC_EGENERIC;
1285 i_len = strlen( s );
1286 if( i_len == 1 && s[0] == '}')
1288 p_subtitle->psz_text = psz_text;
1292 i_old = strlen( psz_text );
1293 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1296 strcat( psz_text, s );
1297 strcat( psz_text, "\n" );
1303 * [n1][n2]Line1|Line2|Line3...
1304 * where n1 and n2 are the video frame number (n2 can be empty)
1306 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1308 VLC_UNUSED( i_idx );
1310 demux_sys_t *p_sys = p_demux->p_sys;
1311 text_t *txt = &p_sys->txt;
1317 const char *s = TextGetLine( txt );
1322 return VLC_EGENERIC;
1324 psz_text = malloc( strlen(s) + 1 );
1330 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1331 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1333 p_subtitle->i_start = (int64_t)i_start * 100000;
1334 p_subtitle->i_stop = i_stop >= 0 ? ((int64_t)i_stop * 100000) : -1;
1340 for( i = 0; psz_text[i] != '\0'; )
1342 /* replace | by \n */
1343 if( psz_text[i] == '|' )
1347 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1348 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1352 p_subtitle->psz_text = psz_text;
1356 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1358 VLC_UNUSED( i_idx );
1360 demux_sys_t *p_sys = p_demux->p_sys;
1361 text_t *txt = &p_sys->txt;
1362 char *psz_text = strdup( "" );
1364 int i_firstline = 1;
1370 const char *s = TextGetLine( txt );
1375 return VLC_EGENERIC;
1379 if( sscanf (s, "-->> %d", &t) == 1)
1381 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1382 p_subtitle->i_stop = -1;
1384 /* Starting of a subtitle */
1389 /* We have been too far: end of the subtitle, begin of next */
1392 TextPreviousLine( txt );
1399 i_old = strlen( psz_text ) + 1;
1400 psz_text = realloc_or_free( psz_text, i_old + strlen( s ) + 1 );
1403 strcat( psz_text, s );
1404 strcat( psz_text, "\n" );
1405 if( txt->i_line == txt->i_line_count )
1409 p_subtitle->psz_text = psz_text;
1413 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1415 VLC_UNUSED( i_idx );
1417 demux_sys_t *p_sys = p_demux->p_sys;
1418 text_t *txt = &p_sys->txt;
1424 const char *s = TextGetLine( txt );
1428 return VLC_EGENERIC;
1430 psz_text = malloc( strlen(s) + 1 );
1435 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1437 /* 1/10th of second ? Frame based ? FIXME */
1438 p_subtitle->i_start = 10 * t1;
1439 p_subtitle->i_stop = 10 * t2;
1440 /* Remove latest " */
1441 psz_text[ strlen(psz_text) - 1 ] = '\0';
1448 /* replace | by \n */
1449 for( i = 0; psz_text[i] != '\0'; i++ )
1451 if( psz_text[i] == '|' )
1455 p_subtitle->psz_text = psz_text;
1456 msg_Dbg( p_demux, "%s", psz_text );
1460 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1462 VLC_UNUSED( i_idx );
1464 demux_sys_t *p_sys = p_demux->p_sys;
1465 text_t *txt = &p_sys->txt;
1466 char *psz_text = strdup( "" );
1468 if( !p_sys->mpsub.b_inited )
1470 p_sys->mpsub.f_total = 0.0;
1471 p_sys->mpsub.f_factor = 0.0;
1473 p_sys->mpsub.b_inited = true;
1482 const char *s = TextGetLine( txt );
1486 return VLC_EGENERIC;
1489 if( strstr( s, "FORMAT" ) )
1491 if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1493 p_sys->mpsub.f_factor = 100.0;
1497 psz_temp = malloc( strlen(s) );
1504 if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1507 f_fps = us_strtod( psz_temp, NULL );
1508 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1509 var_SetFloat( p_demux, "sub-fps", f_fps );
1511 p_sys->mpsub.f_factor = 1.0;
1518 f1 = us_strtod( s, &psz_temp );
1521 f2 = us_strtod( psz_temp, NULL );
1522 p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor;
1523 p_subtitle->i_start = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1524 p_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor;
1525 p_subtitle->i_stop = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1532 const char *s = TextGetLine( txt );
1537 return VLC_EGENERIC;
1540 int i_len = strlen( s );
1544 int i_old = strlen( psz_text );
1546 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1550 strcat( psz_text, s );
1551 strcat( psz_text, "\n" );
1554 p_subtitle->psz_text = psz_text;
1558 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1560 VLC_UNUSED( i_idx );
1562 demux_sys_t *p_sys = p_demux->p_sys;
1563 text_t *txt = &p_sys->txt;
1564 char *psz_text, *psz_orig;
1565 char *psz_text2, *psz_orig2;
1566 int h1, h2, m1, m2, s1, s2, f1, f2;
1568 if( !p_sys->jss.b_inited )
1570 p_sys->jss.i_comment = 0;
1571 p_sys->jss.i_time_resolution = 30;
1572 p_sys->jss.i_time_shift = 0;
1574 p_sys->jss.b_inited = true;
1577 /* Parse the main lines */
1580 const char *s = TextGetLine( txt );
1582 return VLC_EGENERIC;
1584 psz_orig = malloc( strlen( s ) + 1 );
1587 psz_text = psz_orig;
1589 /* Complete time lines */
1590 if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1591 &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1593 p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1594 (int64_t)( ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) )
1596 p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1597 (int64_t)( ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) )
1601 /* Short time lines */
1602 else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1604 p_subtitle->i_start = (int64_t)(
1605 ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1606 p_subtitle->i_stop = (int64_t)(
1607 ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1610 /* General Directive lines */
1611 /* Only TIME and SHIFT are supported so far */
1612 else if( s[0] == '#' )
1614 int h = 0, m =0, sec = 1, f = 1;
1618 strcpy( psz_text, s );
1620 switch( toupper( psz_text[1] ) )
1623 shift = isalpha( psz_text[2] ) ? 6 : 2 ;
1625 if( sscanf( &psz_text[shift], "%d", &h ) )
1627 /* Negative shifting */
1634 if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1636 if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1638 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1643 sscanf( &psz_text[shift], "%d:%d.%d",
1651 sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1654 p_sys->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec )
1655 * p_sys->jss.i_time_resolution + f ) * inv;
1660 shift = isalpha( psz_text[2] ) ? 8 : 2 ;
1662 sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
1669 /* Unkown type line, probably a comment */
1676 while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
1678 const char *s2 = TextGetLine( txt );
1683 return VLC_EGENERIC;
1686 int i_len = strlen( s2 );
1690 int i_old = strlen( psz_text );
1692 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 );
1696 psz_orig = psz_text;
1697 strcat( psz_text, s2 );
1700 /* Skip the blanks */
1701 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1703 /* Parse the directives */
1704 if( isalpha( *psz_text ) || *psz_text == '[' )
1706 while( *psz_text != ' ' )
1709 /* Directives are NOT parsed yet */
1710 /* This has probably a better place in a decoder ? */
1711 /* directive = malloc( strlen( psz_text ) + 1 );
1712 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1715 /* Skip the blanks after directives */
1716 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1718 /* Clean all the lines from inline comments and other stuffs */
1719 psz_orig2 = calloc( strlen( psz_text) + 1, 1 );
1720 psz_text2 = psz_orig2;
1722 for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1727 p_sys->jss.i_comment++;
1730 if( p_sys->jss.i_comment )
1732 p_sys->jss.i_comment = 0;
1733 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1737 if( !p_sys->jss.i_comment )
1745 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1747 if( !p_sys->jss.i_comment )
1754 if( (*(psz_text + 1 ) ) == 'n' )
1761 if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
1762 ( toupper(*(psz_text + 1 ) ) == 'F' ) )
1764 psz_text++; psz_text++;
1767 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1768 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1769 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1770 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1775 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1776 (*(psz_text + 1 ) ) == '\\' )
1778 else if( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n' ||
1779 *(psz_text + 1 ) == '\0' )
1785 if( !p_sys->jss.i_comment )
1787 *psz_text2 = *psz_text;
1794 p_subtitle->psz_text = psz_orig2;
1795 msg_Dbg( p_demux, "%s", p_subtitle->psz_text );
1800 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1802 VLC_UNUSED( i_idx );
1804 demux_sys_t *p_sys = p_demux->p_sys;
1805 text_t *txt = &p_sys->txt;
1813 const char *s = TextGetLine( txt );
1816 return VLC_EGENERIC;
1818 psz_text = malloc( strlen( s ) + 1 );
1822 if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1823 &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1825 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1826 (int64_t)m1 * 60*1000 +
1827 (int64_t)s1 * 1000 ) * 1000;
1828 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1829 (int64_t)m2 * 60*1000 +
1830 (int64_t)s2 * 1000 ) * 1000;
1836 /* replace | by \n */
1837 for( i = 0; psz_text[i] != '\0'; i++ )
1839 if( psz_text[i] == '|' )
1842 p_subtitle->psz_text = psz_text;
1846 static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
1848 if( strlen( psz ) == 0 ) return 0;
1849 if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
1850 sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
1851 sscanf( psz, "%d.%d", s, f ) == 2 ||
1852 sscanf( psz, "%d:%d", m, s ) == 2 ||
1853 sscanf( psz, "%d", s ) == 1 )
1855 return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
1856 + (int64_t)*f * 10 * 1000;
1858 else return VLC_EGENERIC;
1861 static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1863 VLC_UNUSED( i_idx );
1864 demux_sys_t *p_sys = p_demux->p_sys;
1865 text_t *txt = &p_sys->txt;
1866 char *psz_text = NULL;
1870 int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
1871 int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
1872 const char *s = TextGetLine( txt );
1876 return VLC_EGENERIC;
1878 psz_text = malloc( strlen( s ) + 1 );
1882 /* Find the good begining. This removes extra spaces at the beginning
1884 char *psz_temp = strcasestr( s, "<time");
1885 if( psz_temp != NULL )
1887 char psz_end[12], psz_begin[12];
1888 /* Line has begin and end */
1889 if( ( sscanf( psz_temp,
1890 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\n\r]",
1891 psz_begin, psz_end, psz_text) != 3 ) &&
1892 /* Line has begin and no end */
1894 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\n\r]",
1895 psz_begin, psz_text ) != 2) )
1896 /* Line is not recognized */
1902 int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
1903 p_subtitle->i_start = i_time >= 0 ? i_time : 0;
1905 i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
1906 p_subtitle->i_stop = i_time >= 0 ? i_time : -1;
1911 /* Get the following Lines */
1914 const char *s = TextGetLine( txt );
1919 return VLC_EGENERIC;
1922 int i_len = strlen( s );
1923 if( i_len == 0 ) break;
1925 if( strcasestr( s, "<time" ) ||
1926 strcasestr( s, "<clear/") )
1928 TextPreviousLine( txt );
1932 int i_old = strlen( psz_text );
1934 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1938 strcat( psz_text, s );
1939 strcat( psz_text, "\n" );
1942 /* Remove the starting ">" that remained after the sscanf */
1943 memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
1945 p_subtitle->psz_text = psz_text;
1950 static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1952 VLC_UNUSED( i_idx );
1954 demux_sys_t *p_sys = p_demux->p_sys;
1955 text_t *txt = &p_sys->txt;
1962 char *s = TextGetLine( txt );
1965 return VLC_EGENERIC;
1967 psz_text = malloc( strlen( s ) + 1 );
1971 if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
1972 &h1, &m1, &s1, psz_text ) == 4 )
1974 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1975 (int64_t)m1 * 60*1000 +
1976 (int64_t)s1 * 1000 ) * 1000;
1978 char *s = TextGetLine( txt );
1982 return VLC_EGENERIC;
1985 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
1986 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1987 (int64_t)m2 * 60*1000 +
1988 (int64_t)s2 * 1000 ) * 1000;
1990 p_subtitle->i_stop = -1;
1996 /* replace [br] by \n */
1998 while( ( p = strstr( psz_text, "[br]" ) ) )
2001 memmove( p, &p[3], strlen(&p[3])+1 );
2004 p_subtitle->psz_text = psz_text;
2008 static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2010 VLC_UNUSED( i_idx );
2012 demux_sys_t *p_sys = p_demux->p_sys;
2013 text_t *txt = &p_sys->txt;
2020 char *s = TextGetLine( txt );
2023 return VLC_EGENERIC;
2025 if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
2027 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2028 (int64_t)m1 * 60*1000 +
2029 (int64_t)s1 * 1000 ) * 1000;
2031 char *s = TextGetLine( txt );
2033 return VLC_EGENERIC;
2035 psz_text = strdup( s );
2039 s = TextGetLine( txt );
2043 return VLC_EGENERIC;
2046 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2047 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2048 (int64_t)m2 * 60*1000 +
2049 (int64_t)s2 * 1000 ) * 1000;
2051 p_subtitle->i_stop = -1;
2057 p_subtitle->psz_text = psz_text;