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 *****************************************************************************/
35 #include <vlc_plugin.h>
36 #include <vlc_input.h>
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
44 #include <vlc_demux.h>
45 #include <vlc_charset.h>
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t *p_this );
51 static void Close( vlc_object_t *p_this );
53 #define SUB_DELAY_LONGTEXT \
54 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
55 #define SUB_FPS_LONGTEXT \
56 N_("Override the normal frames per second settings. " \
57 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
58 #define SUB_TYPE_LONGTEXT \
59 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
60 "\"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
61 "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\" "\
62 "\"mpsub\" \"jacosub\" \"psb\" and \"auto\" (meaning autodetection, this " \
63 "should always work).")
64 static const char *ppsz_sub_type[] =
66 "auto", "microdvd", "subrip", "subviewer", "ssa1",
67 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
68 "aqt", "pjs", "mpsub", "jacosub", "psb"
72 set_shortname( _("Subtitles"));
73 set_description( _("Text subtitles parser") );
74 set_capability( "demux", 0 );
75 set_category( CAT_INPUT );
76 set_subcategory( SUBCAT_INPUT_DEMUX );
77 add_float( "sub-fps", 0.0, NULL,
78 N_("Frames per second"),
79 SUB_FPS_LONGTEXT, true );
80 add_integer( "sub-delay", 0, NULL,
81 N_("Subtitles delay"),
82 SUB_DELAY_LONGTEXT, true );
83 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
84 SUB_TYPE_LONGTEXT, true );
85 change_string_list( ppsz_sub_type, NULL, NULL );
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,
122 static int TextLoad( text_t *, stream_t *s );
123 static void TextUnload( text_t * );
140 int64_t i_next_demux_date;
141 int64_t i_microsecperframe;
146 subtitle_t *subtitle;
151 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
152 static int ParseSubRip ( demux_t *, subtitle_t *, int );
153 static int ParseSubViewer ( demux_t *, subtitle_t *, int );
154 static int ParseSSA ( demux_t *, subtitle_t *, int );
155 static int ParseVplayer ( demux_t *, subtitle_t *, int );
156 static int ParseSami ( demux_t *, subtitle_t *, int );
157 static int ParseDVDSubtitle( demux_t *, subtitle_t *, int );
158 static int ParseMPL2 ( demux_t *, subtitle_t *, int );
159 static int ParseAQT ( demux_t *, subtitle_t *, int );
160 static int ParsePJS ( demux_t *, subtitle_t *, int );
161 static int ParseMPSub ( demux_t *, subtitle_t *, int );
162 static int ParseJSS ( demux_t *, subtitle_t *, int );
163 static int ParsePSB ( demux_t *, subtitle_t *, int );
164 static int ParseRealText ( demux_t *, subtitle_t *, int );
168 const char *psz_type_name;
170 const char *psz_name;
171 int (*pf_read)( demux_t *, subtitle_t*, int );
172 } sub_read_subtitle_function [] =
174 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
175 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
176 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
177 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
178 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
179 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
180 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
181 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
182 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
183 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
184 { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT },
185 { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS },
186 { "mpsub", SUB_TYPE_MPSUB, "MPSub", ParseMPSub },
187 { "jacosub", SUB_TYPE_JACOSUB, "JacoSub", ParseJSS },
188 { "psb", SUB_TYPE_PSB, "PowerDivx", ParsePSB },
189 { "realtext", SUB_TYPE_RT, "RealText", ParseRealText },
190 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
199 static int Demux( demux_t * );
200 static int Control( demux_t *, int, va_list );
202 /*static void Fix( demux_t * );*/
204 /*****************************************************************************
206 *****************************************************************************/
207 static int Open ( vlc_object_t *p_this )
209 demux_t *p_demux = (demux_t*)p_this;
214 int (*pf_read)( demux_t *, subtitle_t*, int );
217 if( !p_demux->b_force )
219 msg_Dbg( p_demux, "subtitle demux discarded" );
223 p_demux->pf_demux = Demux;
224 p_demux->pf_control = Control;
225 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
229 p_sys->psz_header = NULL;
230 p_sys->i_subtitle = 0;
231 p_sys->i_subtitles = 0;
232 p_sys->subtitle = NULL;
233 p_sys->i_microsecperframe = 40000;
236 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
238 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
240 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
242 /* Check for override of the fps */
243 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
246 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
247 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
250 /* Get or probe the type */
251 p_sys->i_type = SUB_TYPE_UNKNOWN;
252 psz_type = var_CreateGetString( p_demux, "sub-type" );
259 if( sub_read_subtitle_function[i].psz_type_name == NULL )
262 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
265 p_sys->i_type = sub_read_subtitle_function[i].i_type;
272 /* Probe if unknown type */
273 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
278 msg_Dbg( p_demux, "autodetecting subtitle format" );
279 for( i_try = 0; i_try < 256; i_try++ )
284 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
287 if( strcasestr( s, "<SAMI>" ) )
289 p_sys->i_type = SUB_TYPE_SAMI;
292 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
293 sscanf( s, "{%d}{}", &i_dummy ) == 1)
295 p_sys->i_type = SUB_TYPE_MICRODVD;
299 "%d:%d:%d,%d --> %d:%d:%d,%d",
300 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
301 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
303 p_sys->i_type = SUB_TYPE_SUBRIP;
306 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
308 p_sys->i_type = SUB_TYPE_SSA1;
311 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
313 p_sys->i_type = SUB_TYPE_ASS;
316 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
318 p_sys->i_type = SUB_TYPE_SSA2_4;
321 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
323 p_sys->i_type = SUB_TYPE_SSA2_4;
326 else if( !strncasecmp( s, "Dialogue:", 9 ) )
328 p_sys->i_type = SUB_TYPE_ASS;
331 else if( strcasestr( s, "[INFORMATION]" ) )
333 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
336 else if( sscanf( s, "%d:%d:%d.%d %d:%d:%d", &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy, &i_dummy ) == 7 ||
337 sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
339 p_sys->i_type = SUB_TYPE_JACOSUB;
341 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
342 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
344 p_sys->i_type = SUB_TYPE_VPLAYER;
347 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
348 &i_dummy, &i_dummy ) == 4 )
350 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
353 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
354 sscanf( s, "[%d][]", &i_dummy ) == 1)
356 p_sys->i_type = SUB_TYPE_MPL2;
359 else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
360 ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
363 p_sys->i_type = SUB_TYPE_MPSUB;
365 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
367 p_sys->i_type = SUB_TYPE_AQT;
369 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
371 p_sys->i_type = SUB_TYPE_PJS;
373 else if( sscanf( s, "{%d:%d:%d}",
374 &i_dummy, &i_dummy, &i_dummy ) == 3 )
376 p_sys->i_type = SUB_TYPE_PSB;
378 else if( strcasestr( s, "<time" ) )
380 p_sys->i_type = SUB_TYPE_RT;
389 /* It will nearly always work even for non seekable stream thanks the
390 * caching system, and if it fails we lose just a few sub */
391 if( stream_Seek( p_demux->s, 0 ) )
393 msg_Warn( p_demux, "failed to rewind" );
397 /* Quit on unknown subtitles */
398 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
400 msg_Err( p_demux, "failed to recognize subtitle type" );
407 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
409 msg_Dbg( p_demux, "detected %s format",
410 sub_read_subtitle_function[i].psz_name );
411 pf_read = sub_read_subtitle_function[i].pf_read;
416 msg_Dbg( p_demux, "loading all subtitles..." );
418 /* Load the whole file */
419 TextLoad( &p_sys->txt, p_demux->s );
424 if( p_sys->i_subtitles >= i_max )
427 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
428 sizeof(subtitle_t) * i_max ) ) )
430 msg_Err( p_demux, "out of memory");
431 free( p_sys->subtitle );
432 TextUnload( &p_sys->txt );
438 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
439 p_sys->i_subtitles ) )
442 p_sys->i_subtitles++;
445 TextUnload( &p_sys->txt );
447 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
449 /* Fix subtitle (order and time) *** */
450 p_sys->i_subtitle = 0;
452 if( p_sys->i_subtitles > 0 )
454 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
456 if( p_sys->i_length <= 0 )
457 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
460 /* *** add subtitle ES *** */
461 if( p_sys->i_type == SUB_TYPE_SSA1 ||
462 p_sys->i_type == SUB_TYPE_SSA2_4 ||
463 p_sys->i_type == SUB_TYPE_ASS )
465 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
469 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
471 if( p_sys->psz_header != NULL )
473 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
474 fmt.p_extra = strdup( p_sys->psz_header );
476 p_sys->es = es_out_Add( p_demux->out, &fmt );
481 /*****************************************************************************
482 * Close: Close subtitle demux
483 *****************************************************************************/
484 static void Close( vlc_object_t *p_this )
486 demux_t *p_demux = (demux_t*)p_this;
487 demux_sys_t *p_sys = p_demux->p_sys;
490 for( i = 0; i < p_sys->i_subtitles; i++ )
491 free( p_sys->subtitle[i].psz_text );
492 free( p_sys->subtitle );
497 /*****************************************************************************
499 *****************************************************************************/
500 static int Control( demux_t *p_demux, int i_query, va_list args )
502 demux_sys_t *p_sys = p_demux->p_sys;
508 case DEMUX_GET_LENGTH:
509 pi64 = (int64_t*)va_arg( args, int64_t * );
510 *pi64 = p_sys->i_length;
514 pi64 = (int64_t*)va_arg( args, int64_t * );
515 if( p_sys->i_subtitle < p_sys->i_subtitles )
517 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
523 i64 = (int64_t)va_arg( args, int64_t );
524 p_sys->i_subtitle = 0;
525 while( p_sys->i_subtitle < p_sys->i_subtitles &&
526 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
531 if( p_sys->i_subtitle >= p_sys->i_subtitles )
535 case DEMUX_GET_POSITION:
536 pf = (double*)va_arg( args, double * );
537 if( p_sys->i_subtitle >= p_sys->i_subtitles )
541 else if( p_sys->i_subtitles > 0 )
543 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
544 (double)p_sys->i_length;
552 case DEMUX_SET_POSITION:
553 f = (double)va_arg( args, double );
554 i64 = f * p_sys->i_length;
556 p_sys->i_subtitle = 0;
557 while( p_sys->i_subtitle < p_sys->i_subtitles &&
558 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
562 if( p_sys->i_subtitle >= p_sys->i_subtitles )
566 case DEMUX_SET_NEXT_DEMUX_TIME:
567 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
572 case DEMUX_GET_ATTACHMENTS:
573 case DEMUX_GET_TITLE_INFO:
574 case DEMUX_HAS_UNSUPPORTED_META:
578 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
583 /*****************************************************************************
584 * Demux: Send subtitle to decoder
585 *****************************************************************************/
586 static int Demux( demux_t *p_demux )
588 demux_sys_t *p_sys = p_demux->p_sys;
591 if( p_sys->i_subtitle >= p_sys->i_subtitles )
594 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
595 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
597 /* Should not happen */
598 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
601 while( p_sys->i_subtitle < p_sys->i_subtitles &&
602 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
605 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
614 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
620 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
626 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
627 p_block->i_dts = p_block->i_pts;
628 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
631 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
634 memcpy( p_block->p_buffer,
635 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
636 if( p_block->i_pts > 0 )
638 es_out_Send( p_demux->out, p_sys->es, p_block );
642 block_Release( p_block );
648 p_sys->i_next_demux_date = 0;
653 /*****************************************************************************
654 * Fix: fix time stamp and order of subtitle
655 *****************************************************************************/
656 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
657 static void Fix( demux_t *p_demux )
659 demux_sys_t *p_sys = p_demux->p_sys;
663 /* *** fix order (to be sure...) *** */
664 /* We suppose that there are near in order and this durty bubble sort
665 * wont take too much time
670 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
672 if( p_sys->subtitle[i_index].i_start <
673 p_sys->subtitle[i_index - 1].i_start )
677 p_sys->subtitle + i_index - 1,
678 sizeof( subtitle_t ) );
679 memcpy( p_sys->subtitle + i_index - 1,
680 p_sys->subtitle + i_index,
681 sizeof( subtitle_t ) );
682 memcpy( p_sys->subtitle + i_index,
684 sizeof( subtitle_t ) );
692 static int TextLoad( text_t *txt, stream_t *s )
698 txt->i_line_count = 0;
700 txt->line = calloc( i_line_max, sizeof( char * ) );
702 /* load the complete file */
705 char *psz = stream_ReadLine( s );
710 txt->line[txt->i_line_count++] = psz;
711 if( txt->i_line_count >= i_line_max )
714 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
718 if( txt->i_line_count <= 0 )
726 static void TextUnload( text_t *txt )
730 for( i = 0; i < txt->i_line_count; i++ )
732 free( txt->line[i] );
736 txt->i_line_count = 0;
739 static char *TextGetLine( text_t *txt )
741 if( txt->i_line >= txt->i_line_count )
744 return txt->line[txt->i_line++];
746 static void TextPreviousLine( text_t *txt )
748 if( txt->i_line > 0 )
752 /*****************************************************************************
753 * Specific Subtitle function
754 *****************************************************************************/
757 * {n1}{n2}Line1|Line2|Line3....
758 * where n1 and n2 are the video frame number (n2 can be empty)
760 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
764 demux_sys_t *p_sys = p_demux->p_sys;
765 text_t *txt = &p_sys->txt;
773 const char *s = TextGetLine( txt );
777 psz_text = malloc( strlen(s) + 1 );
783 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
784 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
787 if( i_start != 1 || i_stop != 1 )
790 /* We found a possible setting of the framerate "{1}{1}23.976" */
791 /* Check if it's usable, and if the sub-fps is not set */
792 f_fps = us_strtod( psz_text, NULL );
793 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
794 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
799 /* replace | by \n */
800 for( i = 0; psz_text[i] != '\0'; i++ )
802 if( psz_text[i] == '|' )
807 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
808 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
809 p_subtitle->psz_text = psz_text;
813 /* ParseSubRipSubViewer
816 * h1:m1:s1,d1 --> h2:m2:s2,d2
821 * Format SubViewer v1/v2
822 * h1:m1:s1.d1,h2:m2:s2.d2
827 * We ignore line number for SubRip
829 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
833 demux_sys_t *p_sys = p_demux->p_sys;
834 text_t *txt = &p_sys->txt;
839 const char *s = TextGetLine( txt );
840 int h1, m1, s1, d1, h2, m2, s2, d2;
845 if( sscanf( s, psz_fmt,
847 &h2, &m2, &s2, &d2 ) == 8 )
849 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
850 (int64_t)m1 * 60*1000 +
852 (int64_t)d1 ) * 1000;
854 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
855 (int64_t)m2 * 60*1000 +
857 (int64_t)d2 ) * 1000;
862 /* Now read text until an empty line */
863 psz_text = strdup("");
868 const char *s = TextGetLine( txt );
881 p_subtitle->psz_text = psz_text;
885 i_old = strlen( psz_text );
886 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
889 strcat( psz_text, s );
890 strcat( psz_text, "\n" );
892 /* replace [br] by \n */
897 while( ( p = strstr( psz_text, "[br]" ) ) )
900 memmove( p, &p[3], strlen(&p[3])+1 );
907 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
911 return ParseSubRipSubViewer( p_demux, p_subtitle,
912 "%d:%d:%d,%d --> %d:%d:%d,%d",
917 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
922 return ParseSubRipSubViewer( p_demux, p_subtitle,
923 "%d:%d:%d.%d,%d:%d:%d.%d",
929 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
932 demux_sys_t *p_sys = p_demux->p_sys;
933 text_t *txt = &p_sys->txt;
937 const char *s = TextGetLine( txt );
938 int h1, m1, s1, c1, h2, m2, s2, c2;
945 /* We expect (SSA2-4):
946 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
947 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
949 * 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.
953 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
954 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
957 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
958 psz_text = malloc( strlen(s) );
963 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
969 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
970 /* (Layer comes from ASS specs ... it's empty for SSA.) */
971 if( p_sys->i_type == SUB_TYPE_SSA1 )
973 /* SSA1 has only 8 commas before the text starts, not 9 */
974 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
979 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
981 /* ReadOrder, Layer, %s(rest of fields) */
982 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
983 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
984 memcpy( psz_text, temp, strlen(temp) );
987 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
988 (int64_t)m1 * 60*1000 +
990 (int64_t)c1 * 10 ) * 1000;
991 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
992 (int64_t)m2 * 60*1000 +
994 (int64_t)c2 * 10 ) * 1000;
995 p_subtitle->psz_text = psz_text;
1000 /* All the other stuff we add to the header field */
1001 if( !p_sys->psz_header )
1002 p_sys->psz_header = strdup( "" );
1003 if( !p_sys->psz_header )
1007 realloc( p_sys->psz_header,
1008 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
1009 strcat( p_sys->psz_header, s );
1010 strcat( p_sys->psz_header, "\n" );
1016 * h:m:s:Line1|Line2|Line3....
1018 * h:m:s Line1|Line2|Line3....
1020 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1023 VLC_UNUSED( i_idx );
1025 demux_sys_t *p_sys = p_demux->p_sys;
1026 text_t *txt = &p_sys->txt;
1032 const char *s = TextGetLine( txt );
1036 return VLC_EGENERIC;
1038 psz_text = malloc( strlen( s ) + 1 );
1042 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1043 &h1, &m1, &s1, psz_text ) == 4 )
1045 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1046 (int64_t)m1 * 60*1000 +
1047 (int64_t)s1 * 1000 ) * 1000;
1048 p_subtitle->i_stop = 0;
1054 /* replace | by \n */
1055 for( i = 0; psz_text[i] != '\0'; i++ )
1057 if( psz_text[i] == '|' )
1060 p_subtitle->psz_text = psz_text;
1066 static char *ParseSamiSearch( text_t *txt,
1067 char *psz_start, const char *psz_str )
1069 if( psz_start && strcasestr( psz_start, psz_str ) )
1071 char *s = strcasestr( psz_start, psz_str );
1072 return &s[strlen( psz_str )];
1077 char *p = TextGetLine( txt );
1081 if( strcasestr( p, psz_str ) )
1083 char *s = strcasestr( p, psz_str );
1084 return &s[strlen( psz_str )];
1088 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1090 VLC_UNUSED( i_idx );
1091 demux_sys_t *p_sys = p_demux->p_sys;
1092 text_t *txt = &p_sys->txt;
1097 unsigned int i_text;
1098 char text[8192]; /* Arbitrary but should be long enough */
1100 /* search "Start=" */
1101 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1102 return VLC_EGENERIC;
1104 /* get start value */
1105 i_start = strtol( s, &s, 0 );
1108 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1109 return VLC_EGENERIC;
1112 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1113 return VLC_EGENERIC;
1117 /* now get all txt until a "Start=" line */
1121 /* Search non empty line */
1122 while( s && *s == '\0' )
1123 s = TextGetLine( txt );
1129 if( !strncasecmp( s, "<br", 3 ) )
1133 else if( strcasestr( s, "Start=" ) )
1135 TextPreviousLine( txt );
1138 s = ParseSamiSearch( txt, s, ">" );
1140 else if( !strncmp( s, " ", 6 ) )
1145 else if( *s == '\t' )
1155 if( c != '\0' && i_text+1 < sizeof(text) )
1158 text[i_text] = '\0';
1162 p_subtitle->i_start = i_start * 1000;
1163 p_subtitle->i_stop = 0;
1164 p_subtitle->psz_text = strdup( text );
1176 * TODO it can have a header
1183 * LANG support would be cool
1184 * CODEPAGE is probably mandatory FIXME
1186 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1189 VLC_UNUSED( i_idx );
1191 demux_sys_t *p_sys = p_demux->p_sys;
1192 text_t *txt = &p_sys->txt;
1197 const char *s = TextGetLine( txt );
1201 return VLC_EGENERIC;
1205 &h1, &m1, &s1, &c1 ) == 4 )
1207 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1208 (int64_t)m1 * 60*1000 +
1209 (int64_t)s1 * 1000 +
1210 (int64_t)c1 * 10) * 1000;
1211 p_subtitle->i_stop = 0;
1216 /* Now read text until a line containing "}" */
1217 psz_text = strdup("");
1222 const char *s = TextGetLine( txt );
1229 return VLC_EGENERIC;
1232 i_len = strlen( s );
1233 if( i_len == 1 && s[0] == '}')
1235 p_subtitle->psz_text = psz_text;
1239 i_old = strlen( psz_text );
1240 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1243 strcat( psz_text, s );
1244 strcat( psz_text, "\n" );
1250 * [n1][n2]Line1|Line2|Line3...
1251 * where n1 and n2 are the video frame number (n2 can be empty)
1253 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1255 VLC_UNUSED( i_idx );
1257 demux_sys_t *p_sys = p_demux->p_sys;
1258 text_t *txt = &p_sys->txt;
1264 const char *s = TextGetLine( txt );
1269 return VLC_EGENERIC;
1271 psz_text = malloc( strlen(s) + 1 );
1277 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1278 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1280 p_subtitle->i_start = (int64_t)i_start * 100000;
1281 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1287 for( i = 0; psz_text[i] != '\0'; )
1289 /* replace | by \n */
1290 if( psz_text[i] == '|' )
1294 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1295 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1299 p_subtitle->psz_text = psz_text;
1303 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1305 VLC_UNUSED( i_idx );
1307 demux_sys_t *p_sys = p_demux->p_sys;
1308 text_t *txt = &p_sys->txt;
1309 char *psz_text = strdup( "" );
1311 int i_firstline = 1;
1317 const char *s = TextGetLine( txt );
1320 return VLC_EGENERIC;
1323 if( sscanf (s, "-->> %d", &t) == 1)
1325 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1326 p_subtitle->i_stop = 0;
1328 /* Starting of a subtitle */
1333 /* We have been too far: end of the subtitle, begin of next */
1343 i_old = strlen( psz_text ) + 1;
1344 psz_text = realloc( psz_text, i_old + strlen( s ) + 1 );
1347 strcat( psz_text, s );
1348 strcat( psz_text, "\n" );
1349 if( txt->i_line == txt->i_line_count )
1353 p_subtitle->psz_text = psz_text;
1357 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1359 VLC_UNUSED( i_idx );
1361 demux_sys_t *p_sys = p_demux->p_sys;
1362 text_t *txt = &p_sys->txt;
1368 const char *s = TextGetLine( txt );
1372 return VLC_EGENERIC;
1374 psz_text = malloc( strlen(s) + 1 );
1379 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1381 /* 1/10th of second ? Frame based ? FIXME */
1382 p_subtitle->i_start = 10 * t1;
1383 p_subtitle->i_stop = 10 * t2;
1384 /* Remove latest " */
1385 psz_text[ strlen(psz_text) - 1 ] = '\0';
1392 /* replace | by \n */
1393 for( i = 0; psz_text[i] != '\0'; i++ )
1395 if( psz_text[i] == '|' )
1399 p_subtitle->psz_text = psz_text;
1400 msg_Dbg( p_demux, "%s", psz_text );
1404 static float mpsub_total = 0.0;
1405 static float mpsub_factor = 0.0;
1407 static int ParseMPSub( 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;
1413 char *psz_text = strdup( "" );
1421 const char *s = TextGetLine( txt );
1423 return VLC_EGENERIC;
1425 if( strstr( s, "FORMAT" ) )
1427 if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1429 mpsub_factor = 100.0;
1433 psz_temp = malloc( strlen(s) );
1437 if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1440 f_fps = us_strtod( psz_temp, NULL );
1441 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1442 var_SetFloat( p_demux, "sub-fps", f_fps );
1451 if( sscanf (s, "%f %f", &f1, &f2 ) == 2 )
1453 mpsub_total += f1 * mpsub_factor;
1454 p_subtitle->i_start = (int64_t)(10000.0 * mpsub_total);
1455 mpsub_total += f2 * mpsub_factor;
1456 p_subtitle->i_stop = (int64_t)(10000.0 * mpsub_total);
1463 const char *s = TextGetLine( txt );
1466 return VLC_EGENERIC;
1468 int i_len = strlen( s );
1472 int i_old = strlen( psz_text );
1474 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1478 strcat( psz_text, s );
1479 strcat( psz_text, "\n" );
1482 p_subtitle->psz_text = psz_text;
1486 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1488 VLC_UNUSED( i_idx );
1490 demux_sys_t *p_sys = p_demux->p_sys;
1491 text_t *txt = &p_sys->txt;
1492 char *psz_text, *psz_orig;
1493 char *psz_text2, *psz_orig2;
1494 int h1, h2, m1, m2, s1, s2, f1, f2;
1495 static int i_comment = 0;
1497 static int jss_time_resolution = 30;
1498 static int jss_time_shift = 0;
1500 /* Parse the main lines */
1503 const char *s = TextGetLine( txt );
1505 return VLC_EGENERIC;
1507 psz_text = malloc( strlen( s ) + 1 );
1510 psz_orig = psz_text;
1512 /* Complete time lines */
1513 if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1514 &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1516 p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1517 (int64_t)( ( f1 + jss_time_shift ) / jss_time_resolution ) )
1519 p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1520 (int64_t)( ( f2 + jss_time_shift ) / jss_time_resolution ) )
1523 /* Short time lines */
1524 else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1526 p_subtitle->i_start = (int64_t)(
1527 ( f1 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
1528 p_subtitle->i_stop = (int64_t)(
1529 ( f2 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
1531 /* General Directive lines */
1532 /* Only TIME and SHIFT are supported so far */
1533 else if( s[0] == '#' )
1535 int h = 0, m =0, sec = 1, f = 1;
1539 strcpy( psz_text, s );
1541 switch( toupper( psz_text[1] ) )
1544 shift = isalpha( psz_text[2] ) ? 6 : 2 ;
1546 if( sscanf( &psz_text[shift], "%d", &h ) )
1548 /* Negative shifting */
1555 if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1557 if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1559 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1564 sscanf( &psz_text[shift], "%d:%d.%d",
1572 sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1575 jss_time_shift = ( ( h * 3600 + m * 60 + sec )
1576 * jss_time_resolution + f ) * inv;
1581 shift = isalpha( psz_text[2] ) ? 8 : 2 ;
1583 sscanf( &psz_text[shift], "%d", &jss_time_resolution );
1590 /* Unkown type line, probably a comment */
1596 /* Skip the blanks */
1597 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1599 /* Parse the directives */
1600 if( isalpha( *psz_text ) || *psz_text == '[' )
1602 while( *psz_text != ' ' )
1605 /* Directives are NOT parsed yet */
1606 /* This has probably a better place in a decoder ? */
1607 /* directive = malloc( strlen( psz_text ) + 1 );
1608 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1611 /* Skip the blanks after directives */
1612 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1615 /* Clean all the lines from inline comments and other stuffs */
1616 psz_text2 = calloc( strlen( psz_text) + 1, 1 );
1617 psz_orig2 = psz_text2;
1619 for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1630 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1642 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1651 if( (*(psz_text + 1 ) ) == 'n' )
1658 if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
1659 ( toupper(*(psz_text + 1 ) ) == 'F' ) )
1661 psz_text++; psz_text++;
1664 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1665 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1666 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1667 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1672 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1673 (*(psz_text + 1 ) ) == '\\' )
1675 else if( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n'
1676 || *(psz_text + 1 ) == '\0' )
1678 char *s2 = TextGetLine( txt );
1680 return VLC_EGENERIC;
1682 while ( *s2 == ' ' ) s2++;
1684 /* Here to parse the second line, we should add s2 to
1685 psz_text and go on the for( ) line 1556 in order to
1686 parse the next line.
1692 *psz_text2 = *psz_text;
1699 p_subtitle->psz_text = psz_orig2;
1705 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1707 VLC_UNUSED( i_idx );
1709 demux_sys_t *p_sys = p_demux->p_sys;
1710 text_t *txt = &p_sys->txt;
1718 const char *s = TextGetLine( txt );
1721 return VLC_EGENERIC;
1723 psz_text = malloc( strlen( s ) + 1 );
1727 if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1728 &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1730 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1731 (int64_t)m1 * 60*1000 +
1732 (int64_t)s1 * 1000 ) * 1000;
1733 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1734 (int64_t)m2 * 60*1000 +
1735 (int64_t)s2 * 1000 ) * 1000;
1741 /* replace | by \n */
1742 for( i = 0; psz_text[i] != '\0'; i++ )
1744 if( psz_text[i] == '|' )
1747 p_subtitle->psz_text = psz_text;
1751 static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f )
1753 if( strlen( psz ) == 0 ) return 0;
1754 if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 ||
1755 sscanf( psz, "%d:%d.%d", m, s, f ) == 3 ||
1756 sscanf( psz, "%d.%d", s, f ) == 2 ||
1757 sscanf( psz, "%d:%d", m, s ) == 2 ||
1758 sscanf( psz, "%d", s ) == 1 )
1760 return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000
1761 + (int64_t)*f * 10 * 1000;
1763 else return VLC_EGENERIC;
1766 static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1768 VLC_UNUSED( i_idx );
1769 demux_sys_t *p_sys = p_demux->p_sys;
1770 text_t *txt = &p_sys->txt;
1772 char psz_end[12]= "", psz_begin[12] = "";
1776 int h1 = 0, m1 = 0, s1 = 0, f1 = 0;
1777 int h2 = 0, m2 = 0, s2 = 0, f2 = 0;
1778 const char *s = TextGetLine( txt );
1781 return VLC_EGENERIC;
1783 psz_text = malloc( strlen( s ) + 1 );
1787 /* Find the good begining. This removes extra spaces at the beginning
1789 char *psz_temp = strcasestr( s, "<time");
1790 if( psz_temp != NULL )
1792 /* Line has begin and end */
1793 if( ( sscanf( psz_temp,
1794 "<%*[t|T]ime %*[b|B]egin=\"%[^\"]\" %*[e|E]nd=\"%[^\"]%*[^>]%[^\n\r]",
1795 psz_begin, psz_end, psz_text) != 3 ) &&
1796 /* Line has begin and no end */
1798 "<%*[t|T]ime %*[b|B]egin=\"%[^\"]\"%*[^>]%[^\n\r]",
1799 psz_begin, psz_text ) != 2) )
1800 /* Line is not recognized */
1808 int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 );
1811 p_subtitle->i_start = i_time;
1814 i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 );
1817 p_subtitle->i_stop = i_time;
1821 /* Line is not recognized */
1826 /* Get the following Lines */
1829 const char *s = TextGetLine( txt );
1832 return VLC_EGENERIC;
1834 int i_len = strlen( s );
1835 if( i_len == 0 ) break;
1837 if( strcasestr( s, "<time" ) ||
1838 strcasestr( s, "<clear/") )
1844 int i_old = strlen( psz_text );
1846 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1850 strcat( psz_text, s );
1851 strcat( psz_text, "\n" );
1854 /* Remove the starting ">" that remained after the sscanf */
1855 memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) );
1857 p_subtitle->psz_text = psz_text;