+ int i_len = strlen( s );
+ if( i_len == 0 ) break;
+
+ if( strcasestr( s, "<time" ) ||
+ strcasestr( s, "<clear/") )
+ {
+ TextPreviousLine( txt );
+ break;
+ }
+
+ int i_old = strlen( psz_text );
+
+ psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
+ if( !psz_text )
+ return VLC_ENOMEM;
+
+ strcat( psz_text, s );
+ strcat( psz_text, "\n" );
+ }
+
+ /* Remove the starting ">" that remained after the sscanf */
+ memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
+
+ p_subtitle->psz_text = psz_text;
+
+ return VLC_SUCCESS;
+}
+
+static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
+{
+ VLC_UNUSED( i_idx );
+
+ demux_sys_t *p_sys = p_demux->p_sys;
+ text_t *txt = &p_sys->txt;
+ char *psz_text;
+
+ for( ;; )
+ {
+ int h1, m1, s1;
+ int h2, m2, s2;
+ char *s = TextGetLine( txt );
+
+ if( !s )
+ return VLC_EGENERIC;
+
+ psz_text = malloc( strlen( s ) + 1 );
+ if( !psz_text )
+ return VLC_ENOMEM;
+
+ if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
+ &h1, &m1, &s1, psz_text ) == 4 )
+ {
+ p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
+ (int64_t)m1 * 60*1000 +
+ (int64_t)s1 * 1000 ) * 1000;
+
+ char *s = TextGetLine( txt );
+ if( !s )
+ {
+ free( psz_text );
+ return VLC_EGENERIC;
+ }
+
+ if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
+ p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
+ (int64_t)m2 * 60*1000 +
+ (int64_t)s2 * 1000 ) * 1000;
+ else
+ p_subtitle->i_stop = -1;
+ break;
+ }
+ free( psz_text );
+ }
+
+ /* replace [br] by \n */
+ char *p;
+ while( ( p = strstr( psz_text, "[br]" ) ) )
+ {
+ *p++ = '\n';
+ memmove( p, &p[3], strlen(&p[3])+1 );
+ }
+
+ p_subtitle->psz_text = psz_text;
+ return VLC_SUCCESS;
+}
+
+static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
+{
+ VLC_UNUSED( i_idx );
+
+ demux_sys_t *p_sys = p_demux->p_sys;
+ text_t *txt = &p_sys->txt;
+ char *psz_text;
+
+ for( ;; )
+ {
+ int h1, m1, s1;
+ int h2, m2, s2;
+ char *s = TextGetLine( txt );
+
+ if( !s )
+ return VLC_EGENERIC;
+
+ if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
+ {
+ p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
+ (int64_t)m1 * 60*1000 +
+ (int64_t)s1 * 1000 ) * 1000;
+
+ char *s = TextGetLine( txt );
+ if( !s )
+ return VLC_EGENERIC;
+
+ psz_text = strdup( s );
+ if( !psz_text )
+ return VLC_ENOMEM;
+
+ s = TextGetLine( txt );
+ if( !s )
+ {
+ free( psz_text );
+ return VLC_EGENERIC;
+ }
+
+ if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
+ p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
+ (int64_t)m2 * 60*1000 +
+ (int64_t)s2 * 1000 ) * 1000;
+ else
+ p_subtitle->i_stop = -1;
+
+ break;
+ }
+ }
+
+ p_subtitle->psz_text = psz_text;
+
+ return VLC_SUCCESS;
+}
+/*Parsing WebVTT */
+static int ParseVTT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
+{
+ VLC_UNUSED( i_idx );
+
+ demux_sys_t *p_sys = p_demux->p_sys;
+ text_t *txt = &p_sys->txt;
+ char *psz_text;
+
+ for( ;; )
+ {
+ const char *s = TextGetLine( txt );
+ int h1 = 0, m1 = 0, s1 = 0, d1 = 0;
+ int h2 = 0, m2 = 0, s2 = 0, d2 = 0;
+
+ if( !s )
+ return VLC_EGENERIC;
+
+ if( sscanf( s,"%d:%d:%d.%d --> %d:%d:%d.%d",
+ &h1, &m1, &s1, &d1,
+ &h2, &m2, &s2, &d2 ) == 8 ||
+ sscanf( s,"%d:%d:%d.%d --> %d:%d.%d",
+ &h1, &m1, &s1, &d1,
+ &m2, &s2, &d2 ) == 7 ||
+ sscanf( s,"%d:%d.%d --> %d:%d:%d.%d",
+ &m1, &s1, &d1,
+ &h2, &m2, &s2, &d2 ) == 7 ||
+ sscanf( s,"%d:%d.%d --> %d:%d.%d",
+ &m1, &s1, &d1,
+ &m2, &s2, &d2 ) == 6 )
+ {
+ p_subtitle->i_start = ( (int64_t)h1 * 3600 * 1000 +
+ (int64_t)m1 * 60 * 1000 +
+ (int64_t)s1 * 1000 +
+ (int64_t)d1 ) * 1000;
+
+ p_subtitle->i_stop = ( (int64_t)h2 * 3600 * 1000 +
+ (int64_t)m2 * 60 * 1000 +
+ (int64_t)s2 * 1000 +
+ (int64_t)d2 ) * 1000;
+ if( p_subtitle->i_start < p_subtitle->i_stop )
+ break;
+ }
+ }
+
+ /* Now read text until an empty line */
+ psz_text = strdup("");
+ if( !psz_text )
+ return VLC_ENOMEM;
+
+ for( ;; )
+ {
+ const char *s = TextGetLine( txt );
+ int i_len;
+ int i_old;
+
+ i_len = s ? strlen( s ) : 0;
+ if( i_len <= 0 )
+ {
+ p_subtitle->psz_text = psz_text;
+ return VLC_SUCCESS;
+ }
+
+ i_old = strlen( psz_text );
+ psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
+ if( !psz_text )
+ return VLC_ENOMEM;
+
+ strcat( psz_text, s );
+ strcat( psz_text, "\n" );