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. Selecting \"auto\" means autodetection and should always work.")
57 #define SUB_DESCRIPTION_LONGTEXT \
58 N_("Override the default track description.")
60 static const char *const ppsz_sub_type[] =
62 "auto", "microdvd", "subrip", "subviewer", "ssa1",
63 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
64 "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
69 set_shortname( N_("Subtitles"))
70 set_description( N_("Text subtitles parser") )
71 set_capability( "demux", 0 )
72 set_category( CAT_INPUT )
73 set_subcategory( SUBCAT_INPUT_DEMUX )
74 add_float( "sub-fps", 0.0,
75 N_("Frames per second"),
76 SUB_FPS_LONGTEXT, true )
77 add_integer( "sub-delay", 0,
78 N_("Subtitles delay"),
79 SUB_DELAY_LONGTEXT, true )
80 add_string( "sub-type", "auto", N_("Subtitles format"),
81 SUB_TYPE_LONGTEXT, true )
82 change_string_list( ppsz_sub_type, ppsz_sub_type )
83 add_string( "sub-description", NULL, N_("Subtitles description"),
84 SUB_DESCRIPTION_LONGTEXT, true )
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 /* Detect Unicode while skipping the UTF-8 Byte Order Mark */
299 bool unicode = false;
300 const uint8_t *p_data;
301 if( stream_Peek( p_demux->s, &p_data, 3 ) >= 3
302 && !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
305 stream_Seek( p_demux->s, 3 ); /* skip BOM */
306 msg_Dbg( p_demux, "detected Unicode Byte Order Mark" );
309 /* Probe if unknown type */
310 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
315 msg_Dbg( p_demux, "autodetecting subtitle format" );
316 for( i_try = 0; i_try < 256; i_try++ )
321 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
324 if( strcasestr( s, "<SAMI>" ) )
326 p_sys->i_type = SUB_TYPE_SAMI;
329 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
330 sscanf( s, "{%d}{}", &i_dummy ) == 1)
332 p_sys->i_type = SUB_TYPE_MICRODVD;
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 p_sys->i_type = SUB_TYPE_SUBRIP;
344 "%d:%d:%d.%d --> %d:%d:%d.%d",
345 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
346 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
348 msg_Err( p_demux, "Detected invalid SubRip file, playing anyway" );
349 p_sys->i_type = SUB_TYPE_SUBRIP_DOT;
352 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
354 p_sys->i_type = SUB_TYPE_SSA1;
357 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
359 p_sys->i_type = SUB_TYPE_ASS;
362 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
364 p_sys->i_type = SUB_TYPE_SSA2_4;
367 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
369 p_sys->i_type = SUB_TYPE_SSA2_4;
372 else if( !strncasecmp( s, "Dialogue:", 9 ) )
374 p_sys->i_type = SUB_TYPE_ASS;
377 else if( strcasestr( s, "[INFORMATION]" ) )
379 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
382 else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d",
383 &i_dummy, &i_dummy, &i_dummy, &i_dummy,
384 &i_dummy, &i_dummy, &i_dummy ) == 7 ||
385 sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
387 p_sys->i_type = SUB_TYPE_JACOSUB;
390 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
391 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
393 p_sys->i_type = SUB_TYPE_VPLAYER;
396 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
397 &i_dummy, &i_dummy ) == 4 )
399 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
402 else if( sscanf( s, "[%d:%d:%d]%c",
403 &i_dummy, &i_dummy, &i_dummy, &p_dummy ) == 4 )
405 p_sys->i_type = SUB_TYPE_DKS;
408 else if( strstr( s, "*** START SCRIPT" ) )
410 p_sys->i_type = SUB_TYPE_SUBVIEW1;
413 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
414 sscanf( s, "[%d][]", &i_dummy ) == 1)
416 p_sys->i_type = SUB_TYPE_MPL2;
419 else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
420 ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
423 p_sys->i_type = SUB_TYPE_MPSUB;
426 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
428 p_sys->i_type = SUB_TYPE_AQT;
431 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
433 p_sys->i_type = SUB_TYPE_PJS;
436 else if( sscanf( s, "{%d:%d:%d}",
437 &i_dummy, &i_dummy, &i_dummy ) == 3 )
439 p_sys->i_type = SUB_TYPE_PSB;
442 else if( strcasestr( s, "<time" ) )
444 p_sys->i_type = SUB_TYPE_RT;
454 /* It will nearly always work even for non seekable stream thanks the
455 * caching system, and if it fails we lose just a few sub */
456 if( stream_Seek( p_demux->s, unicode ? 3 : 0 ) )
457 msg_Warn( p_demux, "failed to rewind" );
460 /* Quit on unknown subtitles */
461 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
463 stream_Seek( p_demux->s, 0 );
464 msg_Warn( p_demux, "failed to recognize subtitle type" );
471 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
473 msg_Dbg( p_demux, "detected %s format",
474 sub_read_subtitle_function[i].psz_name );
475 pf_read = sub_read_subtitle_function[i].pf_read;
480 msg_Dbg( p_demux, "loading all subtitles..." );
482 /* Load the whole file */
483 TextLoad( &p_sys->txt, p_demux->s );
488 if( p_sys->i_subtitles >= i_max )
491 if( !( p_sys->subtitle = realloc_or_free( p_sys->subtitle,
492 sizeof(subtitle_t) * i_max ) ) )
494 TextUnload( &p_sys->txt );
500 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
501 p_sys->i_subtitles ) )
504 p_sys->i_subtitles++;
507 TextUnload( &p_sys->txt );
509 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
511 /* Fix subtitle (order and time) *** */
512 p_sys->i_subtitle = 0;
514 if( p_sys->i_subtitles > 0 )
516 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
518 if( p_sys->i_length <= 0 )
519 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
522 /* *** add subtitle ES *** */
523 if( p_sys->i_type == SUB_TYPE_SSA1 ||
524 p_sys->i_type == SUB_TYPE_SSA2_4 ||
525 p_sys->i_type == SUB_TYPE_ASS )
528 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
531 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
533 fmt.subs.psz_encoding = strdup( "UTF-8" );
534 char *psz_description = var_InheritString( p_demux, "sub-description" );
535 if( psz_description && *psz_description )
536 fmt.psz_description = psz_description;
538 free( psz_description );
539 if( p_sys->psz_header != NULL )
541 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
542 fmt.p_extra = strdup( p_sys->psz_header );
544 p_sys->es = es_out_Add( p_demux->out, &fmt );
545 es_format_Clean( &fmt );
550 /*****************************************************************************
551 * Close: Close subtitle demux
552 *****************************************************************************/
553 static void Close( vlc_object_t *p_this )
555 demux_t *p_demux = (demux_t*)p_this;
556 demux_sys_t *p_sys = p_demux->p_sys;
559 for( i = 0; i < p_sys->i_subtitles; i++ )
560 free( p_sys->subtitle[i].psz_text );
561 free( p_sys->subtitle );
566 /*****************************************************************************
568 *****************************************************************************/
569 static int Control( demux_t *p_demux, int i_query, va_list args )
571 demux_sys_t *p_sys = p_demux->p_sys;
577 case DEMUX_GET_LENGTH:
578 pi64 = (int64_t*)va_arg( args, int64_t * );
579 *pi64 = p_sys->i_length;
583 pi64 = (int64_t*)va_arg( args, int64_t * );
584 if( p_sys->i_subtitle < p_sys->i_subtitles )
586 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
592 i64 = (int64_t)va_arg( args, int64_t );
593 p_sys->i_subtitle = 0;
594 while( p_sys->i_subtitle < p_sys->i_subtitles )
596 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
598 if( p_subtitle->i_start > i64 )
600 if( p_subtitle->i_stop > p_subtitle->i_start && p_subtitle->i_stop > i64 )
606 if( p_sys->i_subtitle >= p_sys->i_subtitles )
610 case DEMUX_GET_POSITION:
611 pf = (double*)va_arg( args, double * );
612 if( p_sys->i_subtitle >= p_sys->i_subtitles )
616 else if( p_sys->i_subtitles > 0 )
618 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
619 (double)p_sys->i_length;
627 case DEMUX_SET_POSITION:
628 f = (double)va_arg( args, double );
629 i64 = f * p_sys->i_length;
631 p_sys->i_subtitle = 0;
632 while( p_sys->i_subtitle < p_sys->i_subtitles &&
633 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
637 if( p_sys->i_subtitle >= p_sys->i_subtitles )
641 case DEMUX_SET_NEXT_DEMUX_TIME:
642 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
645 case DEMUX_GET_PTS_DELAY:
648 case DEMUX_GET_ATTACHMENTS:
649 case DEMUX_GET_TITLE_INFO:
650 case DEMUX_HAS_UNSUPPORTED_META:
651 case DEMUX_CAN_RECORD:
655 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
660 /*****************************************************************************
661 * Demux: Send subtitle to decoder
662 *****************************************************************************/
663 static int Demux( demux_t *p_demux )
665 demux_sys_t *p_sys = p_demux->p_sys;
668 if( p_sys->i_subtitle >= p_sys->i_subtitles )
671 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
672 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
674 /* Should not happen */
675 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
678 while( p_sys->i_subtitle < p_sys->i_subtitles &&
679 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
681 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
684 int i_len = strlen( p_subtitle->psz_text ) + 1;
686 if( i_len <= 1 || p_subtitle->i_start < 0 )
692 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
699 p_block->i_pts = VLC_TS_0 + p_subtitle->i_start;
700 if( p_subtitle->i_stop >= 0 && p_subtitle->i_stop >= p_subtitle->i_start )
701 p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start;
703 memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len );
705 es_out_Send( p_demux->out, p_sys->es, p_block );
711 p_sys->i_next_demux_date = 0;
716 /*****************************************************************************
717 * Fix: fix time stamp and order of subtitle
718 *****************************************************************************/
719 static void Fix( demux_t *p_demux )
721 demux_sys_t *p_sys = p_demux->p_sys;
724 /* *** fix order (to be sure...) *** */
725 /* We suppose that there are near in order and this durty bubble sort
726 * would not take too much time
731 for( int i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
733 if( p_sys->subtitle[i_index].i_start <
734 p_sys->subtitle[i_index - 1].i_start )
738 p_sys->subtitle + i_index - 1,
739 sizeof( subtitle_t ) );
740 memcpy( p_sys->subtitle + i_index - 1,
741 p_sys->subtitle + i_index,
742 sizeof( subtitle_t ) );
743 memcpy( p_sys->subtitle + i_index,
745 sizeof( subtitle_t ) );
752 static int TextLoad( text_t *txt, stream_t *s )
758 txt->i_line_count = 0;
760 txt->line = calloc( i_line_max, sizeof( char * ) );
764 /* load the complete file */
767 char *psz = stream_ReadLine( s );
772 txt->line[txt->i_line_count++] = psz;
773 if( txt->i_line_count >= i_line_max )
776 txt->line = realloc_or_free( txt->line, i_line_max * sizeof( char * ) );
782 if( txt->i_line_count <= 0 )
790 static void TextUnload( text_t *txt )
794 for( i = 0; i < txt->i_line_count; i++ )
796 free( txt->line[i] );
800 txt->i_line_count = 0;
803 static char *TextGetLine( text_t *txt )
805 if( txt->i_line >= txt->i_line_count )
808 return txt->line[txt->i_line++];
810 static void TextPreviousLine( text_t *txt )
812 if( txt->i_line > 0 )
816 /*****************************************************************************
817 * Specific Subtitle function
818 *****************************************************************************/
821 * {n1}{n2}Line1|Line2|Line3....
822 * where n1 and n2 are the video frame number (n2 can be empty)
824 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
828 demux_sys_t *p_sys = p_demux->p_sys;
829 text_t *txt = &p_sys->txt;
837 const char *s = TextGetLine( txt );
841 psz_text = malloc( strlen(s) + 1 );
847 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
848 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
851 if( i_start != 1 || i_stop != 1 )
854 /* We found a possible setting of the framerate "{1}{1}23.976" */
855 /* Check if it's usable, and if the sub-fps is not set */
856 f_fps = us_strtod( psz_text, NULL );
857 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
858 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
863 /* replace | by \n */
864 for( i = 0; psz_text[i] != '\0'; i++ )
866 if( psz_text[i] == '|' )
871 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
872 p_subtitle->i_stop = i_stop >= 0 ? (i_stop * p_sys->i_microsecperframe) : -1;
873 p_subtitle->psz_text = psz_text;
877 /* ParseSubRipSubViewer
880 * h1:m1:s1,d1 --> h2:m2:s2,d2
885 * Format SubViewer v1/v2
886 * h1:m1:s1.d1,h2:m2:s2.d2
891 * We ignore line number for SubRip
893 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
897 demux_sys_t *p_sys = p_demux->p_sys;
898 text_t *txt = &p_sys->txt;
903 const char *s = TextGetLine( txt );
904 int h1, m1, s1, d1, h2, m2, s2, d2;
909 if( sscanf( s, psz_fmt,
911 &h2, &m2, &s2, &d2 ) == 8 )
913 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
914 (int64_t)m1 * 60*1000 +
916 (int64_t)d1 ) * 1000;
918 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
919 (int64_t)m2 * 60*1000 +
921 (int64_t)d2 ) * 1000;
922 if( p_subtitle->i_start < p_subtitle->i_stop )
927 /* Now read text until an empty line */
928 psz_text = strdup("");
934 const char *s = TextGetLine( txt );
938 i_len = s ? strlen( s ) : 0;
941 p_subtitle->psz_text = psz_text;
945 i_old = strlen( psz_text );
946 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
951 strcat( psz_text, s );
952 strcat( psz_text, "\n" );
954 /* replace [br] by \n */
959 while( ( p = strstr( psz_text, "[br]" ) ) )
962 memmove( p, &p[3], strlen(&p[3])+1 );
969 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
973 return ParseSubRipSubViewer( p_demux, p_subtitle,
974 "%d:%d:%d,%d --> %d:%d:%d,%d",
978 * Special version for buggy file using '.' instead of ','
980 static int ParseSubRipDot( demux_t *p_demux, subtitle_t *p_subtitle,
984 return ParseSubRipSubViewer( p_demux, p_subtitle,
985 "%d:%d:%d.%d --> %d:%d:%d.%d",
990 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
995 return ParseSubRipSubViewer( p_demux, p_subtitle,
996 "%d:%d:%d.%d,%d:%d:%d.%d",
1002 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
1005 demux_sys_t *p_sys = p_demux->p_sys;
1006 text_t *txt = &p_sys->txt;
1010 const char *s = TextGetLine( txt );
1011 int h1, m1, s1, c1, h2, m2, s2, c2;
1012 char *psz_text, *psz_temp;
1016 return VLC_EGENERIC;
1018 /* We expect (SSA2-4):
1019 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1020 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1022 * 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.
1026 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1027 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1030 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
1031 psz_text = malloc( strlen(s) );
1036 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
1042 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1043 /* (Layer comes from ASS specs ... it's empty for SSA.) */
1044 if( p_sys->i_type == SUB_TYPE_SSA1 )
1046 /* SSA1 has only 8 commas before the text starts, not 9 */
1047 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
1052 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
1054 /* ReadOrder, Layer, %s(rest of fields) */
1055 if( asprintf( &psz_temp, "%d,%d,%s", i_idx, i_layer, psz_text ) == -1 )
1062 psz_text = psz_temp;
1065 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1066 (int64_t)m1 * 60*1000 +
1067 (int64_t)s1 * 1000 +
1068 (int64_t)c1 * 10 ) * 1000;
1069 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1070 (int64_t)m2 * 60*1000 +
1071 (int64_t)s2 * 1000 +
1072 (int64_t)c2 * 10 ) * 1000;
1073 p_subtitle->psz_text = psz_text;
1078 /* All the other stuff we add to the header field */
1080 if( asprintf( &psz_header, "%s%s\n",
1081 p_sys->psz_header ? p_sys->psz_header : "", s ) == -1 )
1083 p_sys->psz_header = psz_header;
1089 * h:m:s:Line1|Line2|Line3....
1091 * h:m:s Line1|Line2|Line3....
1093 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1096 VLC_UNUSED( i_idx );
1098 demux_sys_t *p_sys = p_demux->p_sys;
1099 text_t *txt = &p_sys->txt;
1105 const char *s = TextGetLine( txt );
1109 return VLC_EGENERIC;
1111 psz_text = malloc( strlen( s ) + 1 );
1115 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1116 &h1, &m1, &s1, psz_text ) == 4 )
1118 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1119 (int64_t)m1 * 60*1000 +
1120 (int64_t)s1 * 1000 ) * 1000;
1121 p_subtitle->i_stop = -1;
1127 /* replace | by \n */
1128 for( i = 0; psz_text[i] != '\0'; i++ )
1130 if( psz_text[i] == '|' )
1133 p_subtitle->psz_text = psz_text;
1139 static char *ParseSamiSearch( text_t *txt,
1140 char *psz_start, const char *psz_str )
1142 if( psz_start && strcasestr( psz_start, psz_str ) )
1144 char *s = strcasestr( psz_start, psz_str );
1145 return &s[strlen( psz_str )];
1150 char *p = TextGetLine( txt );
1154 if( strcasestr( p, psz_str ) )
1156 char *s = strcasestr( p, psz_str );
1157 return &s[strlen( psz_str )];
1161 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1163 VLC_UNUSED( i_idx );
1164 demux_sys_t *p_sys = p_demux->p_sys;
1165 text_t *txt = &p_sys->txt;
1170 unsigned int i_text;
1171 char text[8192]; /* Arbitrary but should be long enough */
1173 /* search "Start=" */
1174 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1175 return VLC_EGENERIC;
1177 /* get start value */
1178 i_start = strtol( s, &s, 0 );
1181 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1182 return VLC_EGENERIC;
1185 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1186 return VLC_EGENERIC;
1190 /* now get all txt until a "Start=" line */
1194 /* Search non empty line */
1195 while( s && *s == '\0' )
1196 s = TextGetLine( txt );
1202 if( !strncasecmp( s, "<br", 3 ) )
1206 else if( strcasestr( s, "Start=" ) )
1208 TextPreviousLine( txt );
1211 s = ParseSamiSearch( txt, s, ">" );
1213 else if( !strncmp( s, " ", 6 ) )
1218 else if( *s == '\t' )
1228 if( c != '\0' && i_text+1 < sizeof(text) )
1231 text[i_text] = '\0';
1235 p_subtitle->i_start = i_start * 1000;
1236 p_subtitle->i_stop = -1;
1237 p_subtitle->psz_text = strdup( text );
1249 * TODO it can have a header
1256 * LANG support would be cool
1257 * CODEPAGE is probably mandatory FIXME
1259 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1262 VLC_UNUSED( i_idx );
1264 demux_sys_t *p_sys = p_demux->p_sys;
1265 text_t *txt = &p_sys->txt;
1270 const char *s = TextGetLine( txt );
1274 return VLC_EGENERIC;
1278 &h1, &m1, &s1, &c1 ) == 4 )
1280 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1281 (int64_t)m1 * 60*1000 +
1282 (int64_t)s1 * 1000 +
1283 (int64_t)c1 * 10) * 1000;
1284 p_subtitle->i_stop = -1;
1289 /* Now read text until a line containing "}" */
1290 psz_text = strdup("");
1295 const char *s = TextGetLine( txt );
1302 return VLC_EGENERIC;
1305 i_len = strlen( s );
1306 if( i_len == 1 && s[0] == '}')
1308 p_subtitle->psz_text = psz_text;
1312 i_old = strlen( psz_text );
1313 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1316 strcat( psz_text, s );
1317 strcat( psz_text, "\n" );
1323 * [n1][n2]Line1|Line2|Line3...
1324 * where n1 and n2 are the video frame number (n2 can be empty)
1326 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1328 VLC_UNUSED( i_idx );
1330 demux_sys_t *p_sys = p_demux->p_sys;
1331 text_t *txt = &p_sys->txt;
1337 const char *s = TextGetLine( txt );
1342 return VLC_EGENERIC;
1344 psz_text = malloc( strlen(s) + 1 );
1350 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1351 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1353 p_subtitle->i_start = (int64_t)i_start * 100000;
1354 p_subtitle->i_stop = i_stop >= 0 ? ((int64_t)i_stop * 100000) : -1;
1360 for( i = 0; psz_text[i] != '\0'; )
1362 /* replace | by \n */
1363 if( psz_text[i] == '|' )
1367 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1368 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1372 p_subtitle->psz_text = psz_text;
1376 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1378 VLC_UNUSED( i_idx );
1380 demux_sys_t *p_sys = p_demux->p_sys;
1381 text_t *txt = &p_sys->txt;
1382 char *psz_text = strdup( "" );
1384 int i_firstline = 1;
1390 const char *s = TextGetLine( txt );
1395 return VLC_EGENERIC;
1399 if( sscanf (s, "-->> %d", &t) == 1)
1401 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1402 p_subtitle->i_stop = -1;
1404 /* Starting of a subtitle */
1409 /* We have been too far: end of the subtitle, begin of next */
1412 TextPreviousLine( txt );
1419 i_old = strlen( psz_text ) + 1;
1420 psz_text = realloc_or_free( psz_text, i_old + strlen( s ) + 1 );
1423 strcat( psz_text, s );
1424 strcat( psz_text, "\n" );
1425 if( txt->i_line == txt->i_line_count )
1429 p_subtitle->psz_text = psz_text;
1433 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1435 VLC_UNUSED( i_idx );
1437 demux_sys_t *p_sys = p_demux->p_sys;
1438 text_t *txt = &p_sys->txt;
1444 const char *s = TextGetLine( txt );
1448 return VLC_EGENERIC;
1450 psz_text = malloc( strlen(s) + 1 );
1455 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1457 /* 1/10th of second ? Frame based ? FIXME */
1458 p_subtitle->i_start = 10 * t1;
1459 p_subtitle->i_stop = 10 * t2;
1460 /* Remove latest " */
1461 psz_text[ strlen(psz_text) - 1 ] = '\0';
1468 /* replace | by \n */
1469 for( i = 0; psz_text[i] != '\0'; i++ )
1471 if( psz_text[i] == '|' )
1475 p_subtitle->psz_text = psz_text;
1476 msg_Dbg( p_demux, "%s", psz_text );
1480 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1482 VLC_UNUSED( i_idx );
1484 demux_sys_t *p_sys = p_demux->p_sys;
1485 text_t *txt = &p_sys->txt;
1486 char *psz_text = strdup( "" );
1488 if( !p_sys->mpsub.b_inited )
1490 p_sys->mpsub.f_total = 0.0;
1491 p_sys->mpsub.f_factor = 0.0;
1493 p_sys->mpsub.b_inited = true;
1502 const char *s = TextGetLine( txt );
1506 return VLC_EGENERIC;
1509 if( strstr( s, "FORMAT" ) )
1511 if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1513 p_sys->mpsub.f_factor = 100.0;
1517 psz_temp = malloc( strlen(s) );
1524 if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1527 f_fps = us_strtod( psz_temp, NULL );
1528 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1529 var_SetFloat( p_demux, "sub-fps", f_fps );
1531 p_sys->mpsub.f_factor = 1.0;
1538 f1 = us_strtod( s, &psz_temp );
1541 f2 = us_strtod( psz_temp, NULL );
1542 p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor;
1543 p_subtitle->i_start = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1544 p_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor;
1545 p_subtitle->i_stop = (int64_t)(10000.0 * p_sys->mpsub.f_total);
1552 const char *s = TextGetLine( txt );
1557 return VLC_EGENERIC;
1560 int i_len = strlen( s );
1564 int i_old = strlen( psz_text );
1566 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1570 strcat( psz_text, s );
1571 strcat( psz_text, "\n" );
1574 p_subtitle->psz_text = psz_text;
1578 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1580 VLC_UNUSED( i_idx );
1582 demux_sys_t *p_sys = p_demux->p_sys;
1583 text_t *txt = &p_sys->txt;
1584 char *psz_text, *psz_orig;
1585 char *psz_text2, *psz_orig2;
1586 int h1, h2, m1, m2, s1, s2, f1, f2;
1588 if( !p_sys->jss.b_inited )
1590 p_sys->jss.i_comment = 0;
1591 p_sys->jss.i_time_resolution = 30;
1592 p_sys->jss.i_time_shift = 0;
1594 p_sys->jss.b_inited = true;
1597 /* Parse the main lines */
1600 const char *s = TextGetLine( txt );
1602 return VLC_EGENERIC;
1604 psz_orig = malloc( strlen( s ) + 1 );
1607 psz_text = psz_orig;
1609 /* Complete time lines */
1610 if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1611 &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1613 p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1614 (int64_t)( ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) )
1616 p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1617 (int64_t)( ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) )
1621 /* Short time lines */
1622 else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1624 p_subtitle->i_start = (int64_t)(
1625 ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1626 p_subtitle->i_stop = (int64_t)(
1627 ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1630 /* General Directive lines */
1631 /* Only TIME and SHIFT are supported so far */
1632 else if( s[0] == '#' )
1634 int h = 0, m =0, sec = 1, f = 1;
1638 strcpy( psz_text, s );
1640 switch( toupper( (unsigned char)psz_text[1] ) )
1643 shift = isalpha( (unsigned char)psz_text[2] ) ? 6 : 2 ;
1645 if( sscanf( &psz_text[shift], "%d", &h ) )
1647 /* Negative shifting */
1654 if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1656 if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1658 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1663 sscanf( &psz_text[shift], "%d:%d.%d",
1671 sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1674 p_sys->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec )
1675 * p_sys->jss.i_time_resolution + f ) * inv;
1680 shift = isalpha( (unsigned char)psz_text[2] ) ? 8 : 2 ;
1682 sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
1689 /* Unkown type line, probably a comment */
1696 while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
1698 const char *s2 = TextGetLine( txt );
1703 return VLC_EGENERIC;
1706 int i_len = strlen( s2 );
1710 int i_old = strlen( psz_text );
1712 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 );
1716 psz_orig = psz_text;
1717 strcat( psz_text, s2 );
1720 /* Skip the blanks */
1721 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1723 /* Parse the directives */
1724 if( isalpha( (unsigned char)*psz_text ) || *psz_text == '[' )
1726 while( *psz_text != ' ' )
1729 /* Directives are NOT parsed yet */
1730 /* This has probably a better place in a decoder ? */
1731 /* directive = malloc( strlen( psz_text ) + 1 );
1732 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1735 /* Skip the blanks after directives */
1736 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1738 /* Clean all the lines from inline comments and other stuffs */
1739 psz_orig2 = calloc( strlen( psz_text) + 1, 1 );
1740 psz_text2 = psz_orig2;
1742 for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1747 p_sys->jss.i_comment++;
1750 if( p_sys->jss.i_comment )
1752 p_sys->jss.i_comment = 0;
1753 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1757 if( !p_sys->jss.i_comment )
1765 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1767 if( !p_sys->jss.i_comment )
1774 if( (*(psz_text + 1 ) ) == 'n' )
1781 if( ( toupper((unsigned char)*(psz_text + 1 ) ) == 'C' ) ||
1782 ( toupper((unsigned char)*(psz_text + 1 ) ) == 'F' ) )
1784 psz_text++; psz_text++;
1787 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1788 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1789 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1790 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1795 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1796 (*(psz_text + 1 ) ) == '\\' )
1798 else if( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n' ||
1799 *(psz_text + 1 ) == '\0' )
1805 if( !p_sys->jss.i_comment )
1807 *psz_text2 = *psz_text;
1814 p_subtitle->psz_text = psz_orig2;
1815 msg_Dbg( p_demux, "%s", p_subtitle->psz_text );
1820 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1822 VLC_UNUSED( i_idx );
1824 demux_sys_t *p_sys = p_demux->p_sys;
1825 text_t *txt = &p_sys->txt;
1833 const char *s = TextGetLine( txt );
1836 return VLC_EGENERIC;
1838 psz_text = malloc( strlen( s ) + 1 );
1842 if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1843 &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1845 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1846 (int64_t)m1 * 60*1000 +
1847 (int64_t)s1 * 1000 ) * 1000;
1848 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1849 (int64_t)m2 * 60*1000 +
1850 (int64_t)s2 * 1000 ) * 1000;
1856 /* replace | by \n */
1857 for( i = 0; psz_text[i] != '\0'; i++ )
1859 if( psz_text[i] == '|' )
1862 p_subtitle->psz_text = psz_text;
1866 static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
1868 if( *psz == '\0' ) return 0;
1869 if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
1870 sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
1871 sscanf( psz, "%d.%d", s, f ) == 2 ||
1872 sscanf( psz, "%d:%d", m, s ) == 2 ||
1873 sscanf( psz, "%d", s ) == 1 )
1875 return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
1876 + (int64_t)*f * 10 * 1000;
1878 else return VLC_EGENERIC;
1881 static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1883 VLC_UNUSED( i_idx );
1884 demux_sys_t *p_sys = p_demux->p_sys;
1885 text_t *txt = &p_sys->txt;
1886 char *psz_text = NULL;
1890 int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
1891 int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
1892 const char *s = TextGetLine( txt );
1896 return VLC_EGENERIC;
1898 psz_text = malloc( strlen( s ) + 1 );
1902 /* Find the good begining. This removes extra spaces at the beginning
1904 char *psz_temp = strcasestr( s, "<time");
1905 if( psz_temp != NULL )
1907 char psz_end[12], psz_begin[12];
1908 /* Line has begin and end */
1909 if( ( sscanf( psz_temp,
1910 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\n\r]",
1911 psz_begin, psz_end, psz_text) != 3 ) &&
1912 /* Line has begin and no end */
1914 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\n\r]",
1915 psz_begin, psz_text ) != 2) )
1916 /* Line is not recognized */
1922 int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
1923 p_subtitle->i_start = i_time >= 0 ? i_time : 0;
1925 i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
1926 p_subtitle->i_stop = i_time >= 0 ? i_time : -1;
1931 /* Get the following Lines */
1934 const char *s = TextGetLine( txt );
1939 return VLC_EGENERIC;
1942 int i_len = strlen( s );
1943 if( i_len == 0 ) break;
1945 if( strcasestr( s, "<time" ) ||
1946 strcasestr( s, "<clear/") )
1948 TextPreviousLine( txt );
1952 int i_old = strlen( psz_text );
1954 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1958 strcat( psz_text, s );
1959 strcat( psz_text, "\n" );
1962 /* Remove the starting ">" that remained after the sscanf */
1963 memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
1965 p_subtitle->psz_text = psz_text;
1970 static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1972 VLC_UNUSED( i_idx );
1974 demux_sys_t *p_sys = p_demux->p_sys;
1975 text_t *txt = &p_sys->txt;
1982 char *s = TextGetLine( txt );
1985 return VLC_EGENERIC;
1987 psz_text = malloc( strlen( s ) + 1 );
1991 if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
1992 &h1, &m1, &s1, psz_text ) == 4 )
1994 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1995 (int64_t)m1 * 60*1000 +
1996 (int64_t)s1 * 1000 ) * 1000;
1998 char *s = TextGetLine( txt );
2002 return VLC_EGENERIC;
2005 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2006 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2007 (int64_t)m2 * 60*1000 +
2008 (int64_t)s2 * 1000 ) * 1000;
2010 p_subtitle->i_stop = -1;
2016 /* replace [br] by \n */
2018 while( ( p = strstr( psz_text, "[br]" ) ) )
2021 memmove( p, &p[3], strlen(&p[3])+1 );
2024 p_subtitle->psz_text = psz_text;
2028 static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2030 VLC_UNUSED( i_idx );
2032 demux_sys_t *p_sys = p_demux->p_sys;
2033 text_t *txt = &p_sys->txt;
2040 char *s = TextGetLine( txt );
2043 return VLC_EGENERIC;
2045 if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
2047 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2048 (int64_t)m1 * 60*1000 +
2049 (int64_t)s1 * 1000 ) * 1000;
2051 char *s = TextGetLine( txt );
2053 return VLC_EGENERIC;
2055 psz_text = strdup( s );
2059 s = TextGetLine( txt );
2063 return VLC_EGENERIC;
2066 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2067 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2068 (int64_t)m2 * 60*1000 +
2069 (int64_t)s2 * 1000 ) * 1000;
2071 p_subtitle->i_stop = -1;
2077 p_subtitle->psz_text = psz_text;