1 /*****************************************************************************
2 * subtitle.c: Demux for subtitle text files.
3 *****************************************************************************
4 * Copyright (C) 1999-2007 VLC authors and VideoLAN
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * 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>
42 #include <vlc_demux.h>
43 #include <vlc_charset.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t *p_this );
49 static void Close( vlc_object_t *p_this );
51 #define SUB_DELAY_LONGTEXT \
52 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
53 #define SUB_FPS_LONGTEXT \
54 N_("Override the normal frames per second settings. " \
55 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
56 #define SUB_TYPE_LONGTEXT \
57 N_("Force the subtiles format. Selecting \"auto\" means autodetection and should always work.")
58 #define SUB_DESCRIPTION_LONGTEXT \
59 N_("Override the default track description.")
61 static const char *const ppsz_sub_type[] =
63 "auto", "microdvd", "subrip", "subviewer", "ssa1",
64 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
65 "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
70 set_shortname( N_("Subtitles"))
71 set_description( N_("Text subtitle parser") )
72 set_capability( "demux", 0 )
73 set_category( CAT_INPUT )
74 set_subcategory( SUBCAT_INPUT_DEMUX )
75 add_float( "sub-fps", 0.0,
76 N_("Frames per Second"),
77 SUB_FPS_LONGTEXT, true )
78 add_integer( "sub-delay", 0,
80 SUB_DELAY_LONGTEXT, true )
81 add_string( "sub-type", "auto", N_("Subtitle format"),
82 SUB_TYPE_LONGTEXT, true )
83 change_string_list( ppsz_sub_type, ppsz_sub_type )
84 add_string( "sub-description", NULL, N_("Subtitle description"),
85 SUB_DESCRIPTION_LONGTEXT, true )
86 set_callbacks( Open, Close )
88 add_shortcut( "subtitle" )
91 /*****************************************************************************
93 *****************************************************************************/
96 SUB_TYPE_UNKNOWN = -1,
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 */
126 static int TextLoad( text_t *, stream_t *s );
127 static void TextUnload( text_t * );
144 int64_t i_next_demux_date;
145 int64_t i_microsecperframe;
150 subtitle_t *subtitle;
160 int i_time_resolution;
172 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
173 static int ParseSubRip ( 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 );
188 static int ParseVTT ( demux_t *, subtitle_t *, int );
192 const char *psz_type_name;
194 const char *psz_name;
195 int (*pf_read)( demux_t *, subtitle_t*, int );
196 } sub_read_subtitle_function [] =
198 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
199 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
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 { "text/vtt", SUB_TYPE_VTT, "WebVTT", ParseVTT },
217 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
219 /* When adding support for more formats, be sure to add their file extension
220 * to src/input/subtitles.c to enable auto-detection.
223 static int Demux( demux_t * );
224 static int Control( demux_t *, int, va_list );
226 static void Fix( demux_t * );
227 static char * get_language_from_filename( const char * );
229 /*****************************************************************************
231 *****************************************************************************/
232 static int Open ( vlc_object_t *p_this )
234 demux_t *p_demux = (demux_t*)p_this;
239 int (*pf_read)( demux_t *, subtitle_t*, int );
242 if( !p_demux->b_force )
244 msg_Dbg( p_demux, "subtitle demux discarded" );
248 p_demux->pf_demux = Demux;
249 p_demux->pf_control = Control;
250 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
254 p_sys->psz_header = NULL;
255 p_sys->i_subtitle = 0;
256 p_sys->i_subtitles = 0;
257 p_sys->subtitle = NULL;
258 p_sys->i_microsecperframe = 40000;
260 p_sys->jss.b_inited = false;
261 p_sys->mpsub.b_inited = false;
264 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); /* FIXME */
266 p_sys->i_microsecperframe = llroundf( 1000000.f / f_fps );
268 msg_Dbg( p_demux, "Movie fps: %f", (double) f_fps );
270 /* Check for override of the fps */
271 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
274 p_sys->i_microsecperframe = llroundf( 1000000.f / f_fps );
275 msg_Dbg( p_demux, "Override subtitle fps %f", (double) f_fps );
278 /* Get or probe the type */
279 p_sys->i_type = SUB_TYPE_UNKNOWN;
280 psz_type = var_CreateGetString( p_demux, "sub-type" );
281 if( psz_type && *psz_type )
287 if( sub_read_subtitle_function[i].psz_type_name == NULL )
290 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
293 p_sys->i_type = sub_read_subtitle_function[i].i_type;
300 /* Detect Unicode while skipping the UTF-8 Byte Order Mark */
301 bool unicode = false;
302 const uint8_t *p_data;
303 if( stream_Peek( p_demux->s, &p_data, 3 ) >= 3
304 && !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
307 stream_Seek( p_demux->s, 3 ); /* skip BOM */
308 msg_Dbg( p_demux, "detected Unicode Byte Order Mark" );
311 /* Probe if unknown type */
312 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
317 msg_Dbg( p_demux, "autodetecting subtitle format" );
318 for( i_try = 0; i_try < 256; i_try++ )
323 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
326 if( strcasestr( s, "<SAMI>" ) )
328 p_sys->i_type = SUB_TYPE_SAMI;
331 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
332 sscanf( s, "{%d}{}", &i_dummy ) == 1)
334 p_sys->i_type = SUB_TYPE_MICRODVD;
337 else if( sscanf( s, "%d:%d:%d,%d --> %d:%d:%d,%d",
338 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
339 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ||
340 sscanf( s, "%d:%d:%d --> %d:%d:%d,%d",
341 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
342 &i_dummy,&i_dummy,&i_dummy ) == 7 ||
343 sscanf( s, "%d:%d:%d,%d --> %d:%d:%d",
344 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
345 &i_dummy,&i_dummy,&i_dummy ) == 7 ||
346 sscanf( s, "%d:%d:%d.%d --> %d:%d:%d.%d",
347 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
348 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ||
349 sscanf( s, "%d:%d:%d --> %d:%d:%d.%d",
350 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
351 &i_dummy,&i_dummy,&i_dummy ) == 7 ||
352 sscanf( s, "%d:%d:%d.%d --> %d:%d:%d",
353 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
354 &i_dummy,&i_dummy,&i_dummy ) == 7 ||
355 sscanf( s, "%d:%d:%d --> %d:%d:%d",
356 &i_dummy,&i_dummy,&i_dummy,
357 &i_dummy,&i_dummy,&i_dummy ) == 6 )
359 p_sys->i_type = SUB_TYPE_SUBRIP;
362 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
364 p_sys->i_type = SUB_TYPE_SSA1;
367 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
369 p_sys->i_type = SUB_TYPE_ASS;
372 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
374 p_sys->i_type = SUB_TYPE_SSA2_4;
377 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
379 p_sys->i_type = SUB_TYPE_SSA2_4;
382 else if( !strncasecmp( s, "Dialogue:", 9 ) )
384 p_sys->i_type = SUB_TYPE_ASS;
387 else if( strcasestr( s, "[INFORMATION]" ) )
389 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
392 else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d",
393 &i_dummy, &i_dummy, &i_dummy, &i_dummy,
394 &i_dummy, &i_dummy, &i_dummy ) == 7 ||
395 sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
397 p_sys->i_type = SUB_TYPE_JACOSUB;
400 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
401 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
403 p_sys->i_type = SUB_TYPE_VPLAYER;
406 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
407 &i_dummy, &i_dummy ) == 4 )
409 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
412 else if( sscanf( s, "[%d:%d:%d]%c",
413 &i_dummy, &i_dummy, &i_dummy, &p_dummy ) == 4 )
415 p_sys->i_type = SUB_TYPE_DKS;
418 else if( strstr( s, "*** START SCRIPT" ) )
420 p_sys->i_type = SUB_TYPE_SUBVIEW1;
423 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
424 sscanf( s, "[%d][]", &i_dummy ) == 1)
426 p_sys->i_type = SUB_TYPE_MPL2;
429 else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
430 ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
433 p_sys->i_type = SUB_TYPE_MPSUB;
436 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
438 p_sys->i_type = SUB_TYPE_AQT;
441 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
443 p_sys->i_type = SUB_TYPE_PJS;
446 else if( sscanf( s, "{%d:%d:%d}",
447 &i_dummy, &i_dummy, &i_dummy ) == 3 )
449 p_sys->i_type = SUB_TYPE_PSB;
452 else if( strcasestr( s, "<time" ) )
454 p_sys->i_type = SUB_TYPE_RT;
457 else if( !strncasecmp( s, "WEBVTT",6 ) )
459 p_sys->i_type = SUB_TYPE_VTT;
469 /* It will nearly always work even for non seekable stream thanks the
470 * caching system, and if it fails we lose just a few sub */
471 if( stream_Seek( p_demux->s, unicode ? 3 : 0 ) )
472 msg_Warn( p_demux, "failed to rewind" );
475 /* Quit on unknown subtitles */
476 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
478 stream_Seek( p_demux->s, 0 );
479 msg_Warn( p_demux, "failed to recognize subtitle type" );
486 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
488 msg_Dbg( p_demux, "detected %s format",
489 sub_read_subtitle_function[i].psz_name );
490 pf_read = sub_read_subtitle_function[i].pf_read;
495 msg_Dbg( p_demux, "loading all subtitles..." );
497 /* Load the whole file */
498 TextLoad( &p_sys->txt, p_demux->s );
503 if( p_sys->i_subtitles >= i_max )
506 if( !( p_sys->subtitle = realloc_or_free( p_sys->subtitle,
507 sizeof(subtitle_t) * i_max ) ) )
509 TextUnload( &p_sys->txt );
515 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
516 p_sys->i_subtitles ) )
519 p_sys->i_subtitles++;
522 TextUnload( &p_sys->txt );
524 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
526 /* Fix subtitle (order and time) *** */
527 p_sys->i_subtitle = 0;
529 if( p_sys->i_subtitles > 0 )
531 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
533 if( p_sys->i_length <= 0 )
534 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
537 /* *** add subtitle ES *** */
538 if( p_sys->i_type == SUB_TYPE_SSA1 ||
539 p_sys->i_type == SUB_TYPE_SSA2_4 ||
540 p_sys->i_type == SUB_TYPE_ASS )
543 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA );
546 es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT );
548 /* Stupid language detection in the filename */
549 char * psz_language = get_language_from_filename( p_demux->psz_file );
553 fmt.psz_language = psz_language;
554 msg_Dbg( p_demux, "detected language %s of subtitle: %s", psz_language,
555 p_demux->psz_location );
559 fmt.subs.psz_encoding = strdup( "UTF-8" );
560 char *psz_description = var_InheritString( p_demux, "sub-description" );
561 if( psz_description && *psz_description )
562 fmt.psz_description = psz_description;
564 free( psz_description );
565 if( p_sys->psz_header != NULL )
567 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
568 fmt.p_extra = strdup( p_sys->psz_header );
570 p_sys->es = es_out_Add( p_demux->out, &fmt );
571 es_format_Clean( &fmt );
576 /*****************************************************************************
577 * Close: Close subtitle demux
578 *****************************************************************************/
579 static void Close( vlc_object_t *p_this )
581 demux_t *p_demux = (demux_t*)p_this;
582 demux_sys_t *p_sys = p_demux->p_sys;
585 for( i = 0; i < p_sys->i_subtitles; i++ )
586 free( p_sys->subtitle[i].psz_text );
587 free( p_sys->subtitle );
588 free( p_sys->psz_header );
593 /*****************************************************************************
595 *****************************************************************************/
596 static int Control( demux_t *p_demux, int i_query, va_list args )
598 demux_sys_t *p_sys = p_demux->p_sys;
604 case DEMUX_GET_LENGTH:
605 pi64 = (int64_t*)va_arg( args, int64_t * );
606 *pi64 = p_sys->i_length;
610 pi64 = (int64_t*)va_arg( args, int64_t * );
611 if( p_sys->i_subtitle < p_sys->i_subtitles )
613 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
619 i64 = (int64_t)va_arg( args, int64_t );
620 p_sys->i_subtitle = 0;
621 while( p_sys->i_subtitle < p_sys->i_subtitles )
623 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
625 if( p_subtitle->i_start > i64 )
627 if( p_subtitle->i_stop > p_subtitle->i_start && p_subtitle->i_stop > i64 )
633 if( p_sys->i_subtitle >= p_sys->i_subtitles )
637 case DEMUX_GET_POSITION:
638 pf = (double*)va_arg( args, double * );
639 if( p_sys->i_subtitle >= p_sys->i_subtitles )
643 else if( p_sys->i_subtitles > 0 )
645 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
646 (double)p_sys->i_length;
654 case DEMUX_SET_POSITION:
655 f = (double)va_arg( args, double );
656 i64 = f * p_sys->i_length;
658 p_sys->i_subtitle = 0;
659 while( p_sys->i_subtitle < p_sys->i_subtitles &&
660 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
664 if( p_sys->i_subtitle >= p_sys->i_subtitles )
668 case DEMUX_SET_NEXT_DEMUX_TIME:
669 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
672 case DEMUX_GET_PTS_DELAY:
675 case DEMUX_GET_ATTACHMENTS:
676 case DEMUX_GET_TITLE_INFO:
677 case DEMUX_HAS_UNSUPPORTED_META:
678 case DEMUX_CAN_RECORD:
682 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
687 /*****************************************************************************
688 * Demux: Send subtitle to decoder
689 *****************************************************************************/
690 static int Demux( demux_t *p_demux )
692 demux_sys_t *p_sys = p_demux->p_sys;
695 if( p_sys->i_subtitle >= p_sys->i_subtitles )
698 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
699 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
701 /* Should not happen */
702 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
705 while( p_sys->i_subtitle < p_sys->i_subtitles &&
706 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
708 const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle];
711 int i_len = strlen( p_subtitle->psz_text ) + 1;
713 if( i_len <= 1 || p_subtitle->i_start < 0 )
719 if( ( p_block = block_Alloc( i_len ) ) == NULL )
726 p_block->i_pts = VLC_TS_0 + p_subtitle->i_start;
727 if( p_subtitle->i_stop >= 0 && p_subtitle->i_stop >= p_subtitle->i_start )
728 p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start;
730 memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len );
732 es_out_Send( p_demux->out, p_sys->es, p_block );
738 p_sys->i_next_demux_date = 0;
743 /*****************************************************************************
744 * Fix: fix time stamp and order of subtitle
745 *****************************************************************************/
746 static void Fix( demux_t *p_demux )
748 demux_sys_t *p_sys = p_demux->p_sys;
751 /* *** fix order (to be sure...) *** */
752 /* We suppose that there are near in order and this durty bubble sort
753 * would not take too much time
758 for( int i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
760 if( p_sys->subtitle[i_index].i_start <
761 p_sys->subtitle[i_index - 1].i_start )
765 p_sys->subtitle + i_index - 1,
766 sizeof( subtitle_t ) );
767 memcpy( p_sys->subtitle + i_index - 1,
768 p_sys->subtitle + i_index,
769 sizeof( subtitle_t ) );
770 memcpy( p_sys->subtitle + i_index,
772 sizeof( subtitle_t ) );
779 static int TextLoad( text_t *txt, stream_t *s )
785 txt->i_line_count = 0;
787 txt->line = calloc( i_line_max, sizeof( char * ) );
791 /* load the complete file */
794 char *psz = stream_ReadLine( s );
799 txt->line[txt->i_line_count++] = psz;
800 if( txt->i_line_count >= i_line_max )
803 txt->line = realloc_or_free( txt->line, i_line_max * sizeof( char * ) );
809 if( txt->i_line_count <= 0 )
817 static void TextUnload( text_t *txt )
821 for( i = 0; i < txt->i_line_count; i++ )
823 free( txt->line[i] );
827 txt->i_line_count = 0;
830 static char *TextGetLine( text_t *txt )
832 if( txt->i_line >= txt->i_line_count )
835 return txt->line[txt->i_line++];
837 static void TextPreviousLine( text_t *txt )
839 if( txt->i_line > 0 )
843 /*****************************************************************************
844 * Specific Subtitle function
845 *****************************************************************************/
848 * {n1}{n2}Line1|Line2|Line3....
849 * where n1 and n2 are the video frame number (n2 can be empty)
851 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
855 demux_sys_t *p_sys = p_demux->p_sys;
856 text_t *txt = &p_sys->txt;
864 const char *s = TextGetLine( txt );
868 psz_text = malloc( strlen(s) + 1 );
874 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
875 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
877 if( i_start != 1 || i_stop != 1 )
880 /* We found a possible setting of the framerate "{1}{1}23.976" */
881 /* Check if it's usable, and if the sub-fps is not set */
882 float f_fps = us_strtof( psz_text, NULL );
883 if( f_fps > 0.f && var_GetFloat( p_demux, "sub-fps" ) <= 0.f )
884 p_sys->i_microsecperframe = llroundf(1000000.f / f_fps);
889 /* replace | by \n */
890 for( i = 0; psz_text[i] != '\0'; i++ )
892 if( psz_text[i] == '|' )
897 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
898 p_subtitle->i_stop = i_stop >= 0 ? (i_stop * p_sys->i_microsecperframe) : -1;
899 p_subtitle->psz_text = psz_text;
903 /* ParseSubRipSubViewer
906 * h1:m1:s1,d1 --> h2:m2:s2,d2
911 * Format SubViewer v1/v2
912 * h1:m1:s1.d1,h2:m2:s2.d2
917 * We ignore line number for SubRip
919 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
920 int (* pf_parse_timing)(subtitle_t *, const char *),
923 demux_sys_t *p_sys = p_demux->p_sys;
924 text_t *txt = &p_sys->txt;
929 const char *s = TextGetLine( txt );
934 if( pf_parse_timing( p_subtitle, s) == VLC_SUCCESS &&
935 p_subtitle->i_start < p_subtitle->i_stop )
941 /* Now read text until an empty line */
942 psz_text = strdup("");
948 const char *s = TextGetLine( txt );
952 i_len = s ? strlen( s ) : 0;
955 p_subtitle->psz_text = psz_text;
959 i_old = strlen( psz_text );
960 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
965 strcat( psz_text, s );
966 strcat( psz_text, "\n" );
968 /* replace [br] by \n */
973 while( ( p = strstr( psz_text, "[br]" ) ) )
976 memmove( p, &p[3], strlen(&p[3])+1 );
982 /* subtitle_ParseSubRipTimingValue
983 * Parses SubRip timing value.
985 static int subtitle_ParseSubRipTimingValue(int64_t *timing_value,
988 int h1, m1, s1, d1 = 0;
990 if ( sscanf( s, "%d:%d:%d,%d",
991 &h1, &m1, &s1, &d1 ) == 4 ||
992 sscanf( s, "%d:%d:%d.%d",
993 &h1, &m1, &s1, &d1 ) == 4 ||
994 sscanf( s, "%d:%d:%d",
995 &h1, &m1, &s1) == 3 )
997 (*timing_value) = ( (int64_t)h1 * 3600 * 1000 +
998 (int64_t)m1 * 60 * 1000 +
1000 (int64_t)d1 ) * 1000;
1005 return VLC_EGENERIC;
1008 /* subtitle_ParseSubRipTiming
1009 * Parses SubRip timing.
1011 static int subtitle_ParseSubRipTiming( subtitle_t *p_subtitle,
1014 int i_result = VLC_EGENERIC;
1015 char *psz_start, *psz_stop;
1016 psz_start = malloc( strlen(s) + 1 );
1017 psz_stop = malloc( strlen(s) + 1 );
1019 if( sscanf( s, "%s --> %s", psz_start, psz_stop) == 2 &&
1020 subtitle_ParseSubRipTimingValue( &p_subtitle->i_start, psz_start ) == VLC_SUCCESS &&
1021 subtitle_ParseSubRipTimingValue( &p_subtitle->i_stop, psz_stop ) == VLC_SUCCESS )
1023 i_result = VLC_SUCCESS;
1033 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
1036 VLC_UNUSED( i_idx );
1037 return ParseSubRipSubViewer( p_demux, p_subtitle,
1038 &subtitle_ParseSubRipTiming,
1042 /* subtitle_ParseSubViewerTiming
1043 * Parses SubViewer timing.
1045 static int subtitle_ParseSubViewerTiming( subtitle_t *p_subtitle,
1048 int h1, m1, s1, d1, h2, m2, s2, d2;
1050 if( sscanf( s, "%d:%d:%d.%d,%d:%d:%d.%d",
1051 &h1, &m1, &s1, &d1, &h2, &m2, &s2, &d2) == 8 )
1053 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1054 (int64_t)m1 * 60*1000 +
1055 (int64_t)s1 * 1000 +
1056 (int64_t)d1 ) * 1000;
1058 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1059 (int64_t)m2 * 60*1000 +
1060 (int64_t)s2 * 1000 +
1061 (int64_t)d2 ) * 1000;
1064 return VLC_EGENERIC;
1069 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
1072 VLC_UNUSED( i_idx );
1074 return ParseSubRipSubViewer( p_demux, p_subtitle,
1075 &subtitle_ParseSubViewerTiming,
1081 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
1084 demux_sys_t *p_sys = p_demux->p_sys;
1085 text_t *txt = &p_sys->txt;
1086 size_t header_len = 0;
1090 const char *s = TextGetLine( txt );
1091 int h1, m1, s1, c1, h2, m2, s2, c2;
1092 char *psz_text, *psz_temp;
1096 return VLC_EGENERIC;
1098 /* We expect (SSA2-4):
1099 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1100 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1102 * 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.
1106 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
1107 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
1110 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
1111 psz_text = malloc( strlen(s) );
1116 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
1122 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1123 /* (Layer comes from ASS specs ... it's empty for SSA.) */
1124 if( p_sys->i_type == SUB_TYPE_SSA1 )
1126 /* SSA1 has only 8 commas before the text starts, not 9 */
1127 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
1132 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
1134 /* ReadOrder, Layer, %s(rest of fields) */
1135 if( asprintf( &psz_temp, "%d,%d,%s", i_idx, i_layer, psz_text ) == -1 )
1142 psz_text = psz_temp;
1145 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1146 (int64_t)m1 * 60*1000 +
1147 (int64_t)s1 * 1000 +
1148 (int64_t)c1 * 10 ) * 1000;
1149 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1150 (int64_t)m2 * 60*1000 +
1151 (int64_t)s2 * 1000 +
1152 (int64_t)c2 * 10 ) * 1000;
1153 p_subtitle->psz_text = psz_text;
1158 /* All the other stuff we add to the header field */
1159 size_t s_len = strlen( s );
1160 p_sys->psz_header = realloc_or_free( p_sys->psz_header, header_len + s_len + 2 );
1161 if( !p_sys->psz_header )
1163 snprintf( p_sys->psz_header + header_len, s_len + 2, "%s\n", s );
1164 header_len += s_len + 1;
1170 * h:m:s:Line1|Line2|Line3....
1172 * h:m:s Line1|Line2|Line3....
1174 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1177 VLC_UNUSED( i_idx );
1179 demux_sys_t *p_sys = p_demux->p_sys;
1180 text_t *txt = &p_sys->txt;
1186 const char *s = TextGetLine( txt );
1190 return VLC_EGENERIC;
1192 psz_text = malloc( strlen( s ) + 1 );
1196 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1197 &h1, &m1, &s1, psz_text ) == 4 )
1199 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1200 (int64_t)m1 * 60*1000 +
1201 (int64_t)s1 * 1000 ) * 1000;
1202 p_subtitle->i_stop = -1;
1208 /* replace | by \n */
1209 for( i = 0; psz_text[i] != '\0'; i++ )
1211 if( psz_text[i] == '|' )
1214 p_subtitle->psz_text = psz_text;
1220 static char *ParseSamiSearch( text_t *txt,
1221 char *psz_start, const char *psz_str )
1223 if( psz_start && strcasestr( psz_start, psz_str ) )
1225 char *s = strcasestr( psz_start, psz_str );
1226 return &s[strlen( psz_str )];
1231 char *p = TextGetLine( txt );
1235 if( strcasestr( p, psz_str ) )
1237 char *s = strcasestr( p, psz_str );
1238 return &s[strlen( psz_str )];
1242 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1244 VLC_UNUSED( i_idx );
1245 demux_sys_t *p_sys = p_demux->p_sys;
1246 text_t *txt = &p_sys->txt;
1251 unsigned int i_text;
1252 char text[8192]; /* Arbitrary but should be long enough */
1254 /* search "Start=" */
1255 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1256 return VLC_EGENERIC;
1258 /* get start value */
1259 i_start = strtol( s, &s, 0 );
1262 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1263 return VLC_EGENERIC;
1266 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1267 return VLC_EGENERIC;
1271 /* now get all txt until a "Start=" line */
1275 /* Search non empty line */
1276 while( s && *s == '\0' )
1277 s = TextGetLine( txt );
1283 if( !strncasecmp( s, "<br", 3 ) )
1287 else if( strcasestr( s, "Start=" ) )
1289 TextPreviousLine( txt );
1292 s = ParseSamiSearch( txt, s, ">" );
1294 else if( !strncmp( s, " ", 6 ) )
1299 else if( *s == '\t' )
1309 if( c != '\0' && i_text+1 < sizeof(text) )
1312 text[i_text] = '\0';
1316 p_subtitle->i_start = i_start * 1000;
1317 p_subtitle->i_stop = -1;
1318 p_subtitle->psz_text = strdup( text );
1330 * TODO it can have a header
1337 * LANG support would be cool
1338 * CODEPAGE is probably mandatory FIXME
1340 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1343 VLC_UNUSED( i_idx );
1345 demux_sys_t *p_sys = p_demux->p_sys;
1346 text_t *txt = &p_sys->txt;
1351 const char *s = TextGetLine( txt );
1355 return VLC_EGENERIC;
1359 &h1, &m1, &s1, &c1 ) == 4 )
1361 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1362 (int64_t)m1 * 60*1000 +
1363 (int64_t)s1 * 1000 +
1364 (int64_t)c1 * 10) * 1000;
1365 p_subtitle->i_stop = -1;
1370 /* Now read text until a line containing "}" */
1371 psz_text = strdup("");
1376 const char *s = TextGetLine( txt );
1383 return VLC_EGENERIC;
1386 i_len = strlen( s );
1387 if( i_len == 1 && s[0] == '}')
1389 p_subtitle->psz_text = psz_text;
1393 i_old = strlen( psz_text );
1394 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1397 strcat( psz_text, s );
1398 strcat( psz_text, "\n" );
1404 * [n1][n2]Line1|Line2|Line3...
1405 * where n1 and n2 are the video frame number (n2 can be empty)
1407 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1409 VLC_UNUSED( i_idx );
1411 demux_sys_t *p_sys = p_demux->p_sys;
1412 text_t *txt = &p_sys->txt;
1418 const char *s = TextGetLine( txt );
1423 return VLC_EGENERIC;
1425 psz_text = malloc( strlen(s) + 1 );
1431 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1432 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1434 p_subtitle->i_start = (int64_t)i_start * 100000;
1435 p_subtitle->i_stop = i_stop >= 0 ? ((int64_t)i_stop * 100000) : -1;
1441 for( i = 0; psz_text[i] != '\0'; )
1443 /* replace | by \n */
1444 if( psz_text[i] == '|' )
1448 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1449 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1453 p_subtitle->psz_text = psz_text;
1457 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1459 VLC_UNUSED( i_idx );
1461 demux_sys_t *p_sys = p_demux->p_sys;
1462 text_t *txt = &p_sys->txt;
1463 char *psz_text = strdup( "" );
1465 int i_firstline = 1;
1471 const char *s = TextGetLine( txt );
1476 return VLC_EGENERIC;
1480 if( sscanf (s, "-->> %d", &t) == 1)
1482 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1483 p_subtitle->i_stop = -1;
1485 /* Starting of a subtitle */
1490 /* We have been too far: end of the subtitle, begin of next */
1493 TextPreviousLine( txt );
1500 i_old = strlen( psz_text ) + 1;
1501 psz_text = realloc_or_free( psz_text, i_old + strlen( s ) + 1 );
1504 strcat( psz_text, s );
1505 strcat( psz_text, "\n" );
1506 if( txt->i_line == txt->i_line_count )
1510 p_subtitle->psz_text = psz_text;
1514 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1516 VLC_UNUSED( i_idx );
1518 demux_sys_t *p_sys = p_demux->p_sys;
1519 text_t *txt = &p_sys->txt;
1525 const char *s = TextGetLine( txt );
1529 return VLC_EGENERIC;
1531 psz_text = malloc( strlen(s) + 1 );
1536 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1538 /* 1/10th of second ? Frame based ? FIXME */
1539 p_subtitle->i_start = 10 * t1;
1540 p_subtitle->i_stop = 10 * t2;
1541 /* Remove latest " */
1542 psz_text[ strlen(psz_text) - 1 ] = '\0';
1549 /* replace | by \n */
1550 for( i = 0; psz_text[i] != '\0'; i++ )
1552 if( psz_text[i] == '|' )
1556 p_subtitle->psz_text = psz_text;
1557 msg_Dbg( p_demux, "%s", psz_text );
1561 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1563 VLC_UNUSED( i_idx );
1565 demux_sys_t *p_sys = p_demux->p_sys;
1566 text_t *txt = &p_sys->txt;
1567 char *psz_text = strdup( "" );
1569 if( !p_sys->mpsub.b_inited )
1571 p_sys->mpsub.f_total = 0.0;
1572 p_sys->mpsub.f_factor = 0.0;
1574 p_sys->mpsub.b_inited = true;
1582 const char *s = TextGetLine( txt );
1586 return VLC_EGENERIC;
1589 if( strstr( s, "FORMAT" ) )
1591 if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1593 p_sys->mpsub.f_factor = 100.0;
1597 psz_temp = malloc( strlen(s) );
1604 if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1606 float f_fps = us_strtof( psz_temp, NULL );
1608 if( f_fps > 0.f && var_GetFloat( p_demux, "sub-fps" ) <= 0.f )
1609 var_SetFloat( p_demux, "sub-fps", f_fps );
1611 p_sys->mpsub.f_factor = 1.f;
1619 float f1 = us_strtof( s, &psz_temp );
1622 float f2 = us_strtof( psz_temp, NULL );
1623 p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor;
1624 p_subtitle->i_start = llroundf(10000.f * p_sys->mpsub.f_total);
1625 p_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor;
1626 p_subtitle->i_stop = llroundf(10000.f * p_sys->mpsub.f_total);
1633 const char *s = TextGetLine( txt );
1638 return VLC_EGENERIC;
1641 int i_len = strlen( s );
1645 int i_old = strlen( psz_text );
1647 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
1651 strcat( psz_text, s );
1652 strcat( psz_text, "\n" );
1655 p_subtitle->psz_text = psz_text;
1659 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1661 VLC_UNUSED( i_idx );
1663 demux_sys_t *p_sys = p_demux->p_sys;
1664 text_t *txt = &p_sys->txt;
1665 char *psz_text, *psz_orig;
1666 char *psz_text2, *psz_orig2;
1667 int h1, h2, m1, m2, s1, s2, f1, f2;
1669 if( !p_sys->jss.b_inited )
1671 p_sys->jss.i_comment = 0;
1672 p_sys->jss.i_time_resolution = 30;
1673 p_sys->jss.i_time_shift = 0;
1675 p_sys->jss.b_inited = true;
1678 /* Parse the main lines */
1681 const char *s = TextGetLine( txt );
1683 return VLC_EGENERIC;
1685 psz_orig = malloc( strlen( s ) + 1 );
1688 psz_text = psz_orig;
1690 /* Complete time lines */
1691 if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1692 &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1694 p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1695 (int64_t)( ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) )
1697 p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1698 (int64_t)( ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) )
1702 /* Short time lines */
1703 else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1705 p_subtitle->i_start = (int64_t)(
1706 ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1707 p_subtitle->i_stop = (int64_t)(
1708 ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 );
1711 /* General Directive lines */
1712 /* Only TIME and SHIFT are supported so far */
1713 else if( s[0] == '#' )
1715 int h = 0, m =0, sec = 1, f = 1;
1719 strcpy( psz_text, s );
1721 switch( toupper( (unsigned char)psz_text[1] ) )
1724 shift = isalpha( (unsigned char)psz_text[2] ) ? 6 : 2 ;
1726 if( sscanf( &psz_text[shift], "%d", &h ) )
1728 /* Negative shifting */
1735 if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1737 if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1739 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1744 sscanf( &psz_text[shift], "%d:%d.%d",
1752 sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1755 p_sys->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec )
1756 * p_sys->jss.i_time_resolution + f ) * inv;
1761 shift = isalpha( (unsigned char)psz_text[2] ) ? 8 : 2 ;
1763 sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution );
1770 /* Unkown type line, probably a comment */
1777 while( psz_text[ strlen( psz_text ) - 1 ] == '\\' )
1779 const char *s2 = TextGetLine( txt );
1784 return VLC_EGENERIC;
1787 int i_len = strlen( s2 );
1791 int i_old = strlen( psz_text );
1793 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 );
1797 psz_orig = psz_text;
1798 strcat( psz_text, s2 );
1801 /* Skip the blanks */
1802 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1804 /* Parse the directives */
1805 if( isalpha( (unsigned char)*psz_text ) || *psz_text == '[' )
1807 while( *psz_text != ' ' )
1810 /* Directives are NOT parsed yet */
1811 /* This has probably a better place in a decoder ? */
1812 /* directive = malloc( strlen( psz_text ) + 1 );
1813 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1816 /* Skip the blanks after directives */
1817 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1819 /* Clean all the lines from inline comments and other stuffs */
1820 psz_orig2 = calloc( strlen( psz_text) + 1, 1 );
1821 psz_text2 = psz_orig2;
1823 for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1828 p_sys->jss.i_comment++;
1831 if( p_sys->jss.i_comment )
1833 p_sys->jss.i_comment = 0;
1834 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1838 if( !p_sys->jss.i_comment )
1846 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1848 if( !p_sys->jss.i_comment )
1855 if( (*(psz_text + 1 ) ) == 'n' )
1862 if( ( toupper((unsigned char)*(psz_text + 1 ) ) == 'C' ) ||
1863 ( toupper((unsigned char)*(psz_text + 1 ) ) == 'F' ) )
1865 psz_text++; psz_text++;
1868 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1869 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1870 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1871 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1876 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1877 (*(psz_text + 1 ) ) == '\\' )
1879 else if( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n' ||
1880 *(psz_text + 1 ) == '\0' )
1886 if( !p_sys->jss.i_comment )
1888 *psz_text2 = *psz_text;
1895 p_subtitle->psz_text = psz_orig2;
1896 msg_Dbg( p_demux, "%s", p_subtitle->psz_text );
1901 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1903 VLC_UNUSED( i_idx );
1905 demux_sys_t *p_sys = p_demux->p_sys;
1906 text_t *txt = &p_sys->txt;
1914 const char *s = TextGetLine( txt );
1917 return VLC_EGENERIC;
1919 psz_text = malloc( strlen( s ) + 1 );
1923 if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1924 &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1926 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1927 (int64_t)m1 * 60*1000 +
1928 (int64_t)s1 * 1000 ) * 1000;
1929 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1930 (int64_t)m2 * 60*1000 +
1931 (int64_t)s2 * 1000 ) * 1000;
1937 /* replace | by \n */
1938 for( i = 0; psz_text[i] != '\0'; i++ )
1940 if( psz_text[i] == '|' )
1943 p_subtitle->psz_text = psz_text;
1947 static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
1949 if( *psz == '\0' ) return 0;
1950 if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
1951 sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
1952 sscanf( psz, "%d.%d", s, f ) == 2 ||
1953 sscanf( psz, "%d:%d", m, s ) == 2 ||
1954 sscanf( psz, "%d", s ) == 1 )
1956 return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
1957 + (int64_t)*f * 10 * 1000;
1959 else return VLC_EGENERIC;
1962 static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1964 VLC_UNUSED( i_idx );
1965 demux_sys_t *p_sys = p_demux->p_sys;
1966 text_t *txt = &p_sys->txt;
1967 char *psz_text = NULL;
1971 int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
1972 int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
1973 const char *s = TextGetLine( txt );
1977 return VLC_EGENERIC;
1979 psz_text = malloc( strlen( s ) + 1 );
1983 /* Find the good begining. This removes extra spaces at the beginning
1985 char *psz_temp = strcasestr( s, "<time");
1986 if( psz_temp != NULL )
1988 char psz_end[12], psz_begin[12];
1989 /* Line has begin and end */
1990 if( ( sscanf( psz_temp,
1991 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\n\r]",
1992 psz_begin, psz_end, psz_text) != 3 ) &&
1993 /* Line has begin and no end */
1995 "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\n\r]",
1996 psz_begin, psz_text ) != 2) )
1997 /* Line is not recognized */
2003 int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
2004 p_subtitle->i_start = i_time >= 0 ? i_time : 0;
2006 i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
2007 p_subtitle->i_stop = i_time >= 0 ? i_time : -1;
2012 /* Get the following Lines */
2015 const char *s = TextGetLine( txt );
2020 return VLC_EGENERIC;
2023 int i_len = strlen( s );
2024 if( i_len == 0 ) break;
2026 if( strcasestr( s, "<time" ) ||
2027 strcasestr( s, "<clear/") )
2029 TextPreviousLine( txt );
2033 int i_old = strlen( psz_text );
2035 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
2039 strcat( psz_text, s );
2040 strcat( psz_text, "\n" );
2043 /* Remove the starting ">" that remained after the sscanf */
2044 memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
2046 p_subtitle->psz_text = psz_text;
2051 static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2053 VLC_UNUSED( i_idx );
2055 demux_sys_t *p_sys = p_demux->p_sys;
2056 text_t *txt = &p_sys->txt;
2063 char *s = TextGetLine( txt );
2066 return VLC_EGENERIC;
2068 psz_text = malloc( strlen( s ) + 1 );
2072 if( sscanf( s, "[%d:%d:%d]%[^\r\n]",
2073 &h1, &m1, &s1, psz_text ) == 4 )
2075 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2076 (int64_t)m1 * 60*1000 +
2077 (int64_t)s1 * 1000 ) * 1000;
2079 char *s = TextGetLine( txt );
2083 return VLC_EGENERIC;
2086 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2087 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2088 (int64_t)m2 * 60*1000 +
2089 (int64_t)s2 * 1000 ) * 1000;
2091 p_subtitle->i_stop = -1;
2097 /* replace [br] by \n */
2099 while( ( p = strstr( psz_text, "[br]" ) ) )
2102 memmove( p, &p[3], strlen(&p[3])+1 );
2105 p_subtitle->psz_text = psz_text;
2109 static int ParseSubViewer1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2111 VLC_UNUSED( i_idx );
2113 demux_sys_t *p_sys = p_demux->p_sys;
2114 text_t *txt = &p_sys->txt;
2121 char *s = TextGetLine( txt );
2124 return VLC_EGENERIC;
2126 if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 )
2128 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
2129 (int64_t)m1 * 60*1000 +
2130 (int64_t)s1 * 1000 ) * 1000;
2132 char *s = TextGetLine( txt );
2134 return VLC_EGENERIC;
2136 psz_text = strdup( s );
2140 s = TextGetLine( txt );
2144 return VLC_EGENERIC;
2147 if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 )
2148 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
2149 (int64_t)m2 * 60*1000 +
2150 (int64_t)s2 * 1000 ) * 1000;
2152 p_subtitle->i_stop = -1;
2158 p_subtitle->psz_text = psz_text;
2163 static int ParseVTT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
2165 VLC_UNUSED( i_idx );
2167 demux_sys_t *p_sys = p_demux->p_sys;
2168 text_t *txt = &p_sys->txt;
2173 const char *s = TextGetLine( txt );
2174 int h1 = 0, m1 = 0, s1 = 0, d1 = 0;
2175 int h2 = 0, m2 = 0, s2 = 0, d2 = 0;
2178 return VLC_EGENERIC;
2180 if( sscanf( s,"%d:%d:%d.%d --> %d:%d:%d.%d",
2182 &h2, &m2, &s2, &d2 ) == 8 ||
2183 sscanf( s,"%d:%d:%d.%d --> %d:%d.%d",
2185 &m2, &s2, &d2 ) == 7 ||
2186 sscanf( s,"%d:%d.%d --> %d:%d:%d.%d",
2188 &h2, &m2, &s2, &d2 ) == 7 ||
2189 sscanf( s,"%d:%d.%d --> %d:%d.%d",
2191 &m2, &s2, &d2 ) == 6 )
2193 p_subtitle->i_start = ( (int64_t)h1 * 3600 * 1000 +
2194 (int64_t)m1 * 60 * 1000 +
2195 (int64_t)s1 * 1000 +
2196 (int64_t)d1 ) * 1000;
2198 p_subtitle->i_stop = ( (int64_t)h2 * 3600 * 1000 +
2199 (int64_t)m2 * 60 * 1000 +
2200 (int64_t)s2 * 1000 +
2201 (int64_t)d2 ) * 1000;
2202 if( p_subtitle->i_start < p_subtitle->i_stop )
2207 /* Now read text until an empty line */
2208 psz_text = strdup("");
2214 const char *s = TextGetLine( txt );
2218 i_len = s ? strlen( s ) : 0;
2221 p_subtitle->psz_text = psz_text;
2225 i_old = strlen( psz_text );
2226 psz_text = realloc_or_free( psz_text, i_old + i_len + 1 + 1 );
2230 strcat( psz_text, s );
2231 strcat( psz_text, "\n" );
2235 /* Matches filename.xx.srt */
2236 static char * get_language_from_filename( const char * psz_sub_file )
2238 char *psz_ret = NULL;
2239 char *psz_tmp, *psz_language_begin;
2241 if( !psz_sub_file ) return NULL;
2242 char *psz_work = strdup( psz_sub_file );
2244 /* Removing extension, but leaving the dot */
2245 psz_tmp = strrchr( psz_work, '.' );
2249 psz_language_begin = strrchr( psz_work, '.' );
2250 if( psz_language_begin )
2251 psz_ret = strdup(++psz_language_begin);