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\" 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"
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,
120 static int TextLoad( text_t *, stream_t *s );
121 static void TextUnload( text_t * );
138 int64_t i_next_demux_date;
139 int64_t i_microsecperframe;
144 subtitle_t *subtitle;
149 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
150 static int ParseSubRip ( demux_t *, subtitle_t *, int );
151 static int ParseSubViewer ( demux_t *, subtitle_t *, int );
152 static int ParseSSA ( demux_t *, subtitle_t *, int );
153 static int ParseVplayer ( demux_t *, subtitle_t *, int );
154 static int ParseSami ( demux_t *, subtitle_t *, int );
155 static int ParseDVDSubtitle( demux_t *, subtitle_t *, int );
156 static int ParseMPL2 ( demux_t *, subtitle_t *, int );
157 static int ParseAQT ( demux_t *, subtitle_t *, int );
158 static int ParsePJS ( demux_t *, subtitle_t *, int );
159 static int ParseMPSub ( demux_t *, subtitle_t *, int );
160 static int ParseJSS ( demux_t *, subtitle_t *, int );
164 const char *psz_type_name;
166 const char *psz_name;
167 int (*pf_read)( demux_t *, subtitle_t*, int );
168 } sub_read_subtitle_function [] =
170 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
171 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
172 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
173 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
174 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
175 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
176 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
177 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
178 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
179 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
180 { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT },
181 { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS },
182 { "mpsub", SUB_TYPE_MPSUB, "MPSub", ParseMPSub },
183 { "jacosub", SUB_TYPE_JACOSUB, "JacoSub", ParseJSS },
184 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
195 static int Demux( demux_t * );
196 static int Control( demux_t *, int, va_list );
198 /*static void Fix( demux_t * );*/
200 /*****************************************************************************
202 *****************************************************************************/
203 static int Open ( vlc_object_t *p_this )
205 demux_t *p_demux = (demux_t*)p_this;
210 int (*pf_read)( demux_t *, subtitle_t*, int );
213 if( !p_demux->b_force )
215 msg_Dbg( p_demux, "subtitle demux discarded" );
219 p_demux->pf_demux = Demux;
220 p_demux->pf_control = Control;
221 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
225 p_sys->psz_header = NULL;
226 p_sys->i_subtitle = 0;
227 p_sys->i_subtitles = 0;
228 p_sys->subtitle = NULL;
229 p_sys->i_microsecperframe = 40000;
232 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
234 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
236 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
238 /* Check for override of the fps */
239 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
242 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
243 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
246 /* Get or probe the type */
247 p_sys->i_type = SUB_TYPE_UNKNOWN;
248 psz_type = var_CreateGetString( p_demux, "sub-type" );
255 if( sub_read_subtitle_function[i].psz_type_name == NULL )
258 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
261 p_sys->i_type = sub_read_subtitle_function[i].i_type;
268 /* Probe if unknown type */
269 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
274 msg_Dbg( p_demux, "autodetecting subtitle format" );
275 for( i_try = 0; i_try < 256; i_try++ )
280 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
283 if( strcasestr( s, "<SAMI>" ) )
285 p_sys->i_type = SUB_TYPE_SAMI;
288 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
289 sscanf( s, "{%d}{}", &i_dummy ) == 1)
291 p_sys->i_type = SUB_TYPE_MICRODVD;
295 "%d:%d:%d,%d --> %d:%d:%d,%d",
296 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
297 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
299 p_sys->i_type = SUB_TYPE_SUBRIP;
302 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
304 p_sys->i_type = SUB_TYPE_SSA1;
307 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
309 p_sys->i_type = SUB_TYPE_ASS;
312 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
314 p_sys->i_type = SUB_TYPE_SSA2_4;
317 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
319 p_sys->i_type = SUB_TYPE_SSA2_4;
322 else if( !strncasecmp( s, "Dialogue:", 9 ) )
324 p_sys->i_type = SUB_TYPE_ASS;
327 else if( strcasestr( s, "[INFORMATION]" ) )
329 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
332 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 ||
333 sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
335 p_sys->i_type = SUB_TYPE_JACOSUB;
337 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
338 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
340 p_sys->i_type = SUB_TYPE_VPLAYER;
343 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
344 &i_dummy, &i_dummy ) == 4 )
346 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
349 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
350 sscanf( s, "[%d][]", &i_dummy ) == 1)
352 p_sys->i_type = SUB_TYPE_MPL2;
355 else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
356 ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
359 p_sys->i_type = SUB_TYPE_MPSUB;
361 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
363 p_sys->i_type = SUB_TYPE_AQT;
365 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
367 p_sys->i_type = SUB_TYPE_PJS;
376 /* It will nearly always work even for non seekable stream thanks the
377 * caching system, and if it fails we lose just a few sub */
378 if( stream_Seek( p_demux->s, 0 ) )
380 msg_Warn( p_demux, "failed to rewind" );
383 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
385 msg_Err( p_demux, "failed to recognize subtitle type" );
392 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
394 msg_Dbg( p_demux, "detected %s format",
395 sub_read_subtitle_function[i].psz_name );
396 pf_read = sub_read_subtitle_function[i].pf_read;
401 msg_Dbg( p_demux, "loading all subtitles..." );
403 /* Load the whole file */
404 TextLoad( &p_sys->txt, p_demux->s );
409 if( p_sys->i_subtitles >= i_max )
412 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
413 sizeof(subtitle_t) * i_max ) ) )
415 msg_Err( p_demux, "out of memory");
416 free( p_sys->subtitle );
417 TextUnload( &p_sys->txt );
423 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
424 p_sys->i_subtitles ) )
427 p_sys->i_subtitles++;
430 TextUnload( &p_sys->txt );
432 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
434 /* Fix subtitle (order and time) *** */
435 p_sys->i_subtitle = 0;
437 if( p_sys->i_subtitles > 0 )
439 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
441 if( p_sys->i_length <= 0 )
442 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
445 /* *** add subtitle ES *** */
446 if( p_sys->i_type == SUB_TYPE_SSA1 ||
447 p_sys->i_type == SUB_TYPE_SSA2_4 ||
448 p_sys->i_type == SUB_TYPE_ASS )
450 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
454 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
456 if( p_sys->psz_header != NULL )
458 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
459 fmt.p_extra = strdup( p_sys->psz_header );
461 p_sys->es = es_out_Add( p_demux->out, &fmt );
466 /*****************************************************************************
467 * Close: Close subtitle demux
468 *****************************************************************************/
469 static void Close( vlc_object_t *p_this )
471 demux_t *p_demux = (demux_t*)p_this;
472 demux_sys_t *p_sys = p_demux->p_sys;
475 for( i = 0; i < p_sys->i_subtitles; i++ )
476 free( p_sys->subtitle[i].psz_text );
477 free( p_sys->subtitle );
482 /*****************************************************************************
484 *****************************************************************************/
485 static int Control( demux_t *p_demux, int i_query, va_list args )
487 demux_sys_t *p_sys = p_demux->p_sys;
493 case DEMUX_GET_LENGTH:
494 pi64 = (int64_t*)va_arg( args, int64_t * );
495 *pi64 = p_sys->i_length;
499 pi64 = (int64_t*)va_arg( args, int64_t * );
500 if( p_sys->i_subtitle < p_sys->i_subtitles )
502 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
508 i64 = (int64_t)va_arg( args, int64_t );
509 p_sys->i_subtitle = 0;
510 while( p_sys->i_subtitle < p_sys->i_subtitles &&
511 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
516 if( p_sys->i_subtitle >= p_sys->i_subtitles )
520 case DEMUX_GET_POSITION:
521 pf = (double*)va_arg( args, double * );
522 if( p_sys->i_subtitle >= p_sys->i_subtitles )
526 else if( p_sys->i_subtitles > 0 )
528 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
529 (double)p_sys->i_length;
537 case DEMUX_SET_POSITION:
538 f = (double)va_arg( args, double );
539 i64 = f * p_sys->i_length;
541 p_sys->i_subtitle = 0;
542 while( p_sys->i_subtitle < p_sys->i_subtitles &&
543 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
547 if( p_sys->i_subtitle >= p_sys->i_subtitles )
551 case DEMUX_SET_NEXT_DEMUX_TIME:
552 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
557 case DEMUX_GET_ATTACHMENTS:
558 case DEMUX_GET_TITLE_INFO:
559 case DEMUX_HAS_UNSUPPORTED_META:
563 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
568 /*****************************************************************************
569 * Demux: Send subtitle to decoder
570 *****************************************************************************/
571 static int Demux( demux_t *p_demux )
573 demux_sys_t *p_sys = p_demux->p_sys;
576 if( p_sys->i_subtitle >= p_sys->i_subtitles )
579 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
580 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
582 /* Should not happen */
583 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
586 while( p_sys->i_subtitle < p_sys->i_subtitles &&
587 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
590 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
599 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
605 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
611 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
612 p_block->i_dts = p_block->i_pts;
613 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
616 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
619 memcpy( p_block->p_buffer,
620 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
621 if( p_block->i_pts > 0 )
623 es_out_Send( p_demux->out, p_sys->es, p_block );
627 block_Release( p_block );
633 p_sys->i_next_demux_date = 0;
638 /*****************************************************************************
639 * Fix: fix time stamp and order of subtitle
640 *****************************************************************************/
641 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
642 static void Fix( demux_t *p_demux )
644 demux_sys_t *p_sys = p_demux->p_sys;
648 /* *** fix order (to be sure...) *** */
649 /* We suppose that there are near in order and this durty bubble sort
650 * wont take too much time
655 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
657 if( p_sys->subtitle[i_index].i_start <
658 p_sys->subtitle[i_index - 1].i_start )
662 p_sys->subtitle + i_index - 1,
663 sizeof( subtitle_t ) );
664 memcpy( p_sys->subtitle + i_index - 1,
665 p_sys->subtitle + i_index,
666 sizeof( subtitle_t ) );
667 memcpy( p_sys->subtitle + i_index,
669 sizeof( subtitle_t ) );
677 static int TextLoad( text_t *txt, stream_t *s )
683 txt->i_line_count = 0;
685 txt->line = calloc( i_line_max, sizeof( char * ) );
687 /* load the complete file */
690 char *psz = stream_ReadLine( s );
695 txt->line[txt->i_line_count++] = psz;
696 if( txt->i_line_count >= i_line_max )
699 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
703 if( txt->i_line_count <= 0 )
711 static void TextUnload( text_t *txt )
715 for( i = 0; i < txt->i_line_count; i++ )
717 free( txt->line[i] );
721 txt->i_line_count = 0;
724 static char *TextGetLine( text_t *txt )
726 if( txt->i_line >= txt->i_line_count )
729 return txt->line[txt->i_line++];
731 static void TextPreviousLine( text_t *txt )
733 if( txt->i_line > 0 )
737 /*****************************************************************************
738 * Specific Subtitle function
739 *****************************************************************************/
742 * {n1}{n2}Line1|Line2|Line3....
743 * where n1 and n2 are the video frame number (n2 can be empty)
745 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
749 demux_sys_t *p_sys = p_demux->p_sys;
750 text_t *txt = &p_sys->txt;
758 const char *s = TextGetLine( txt );
762 psz_text = malloc( strlen(s) + 1 );
768 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
769 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
772 if( i_start != 1 || i_stop != 1 )
775 /* We found a possible setting of the framerate "{1}{1}23.976" */
776 /* Check if it's usable, and if the sub-fps is not set */
777 f_fps = us_strtod( psz_text, NULL );
778 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
779 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
784 /* replace | by \n */
785 for( i = 0; psz_text[i] != '\0'; i++ )
787 if( psz_text[i] == '|' )
792 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
793 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
794 p_subtitle->psz_text = psz_text;
798 /* ParseSubRipSubViewer
801 * h1:m1:s1,d1 --> h2:m2:s2,d2
806 * Format SubViewer v1/v2
807 * h1:m1:s1.d1,h2:m2:s2.d2
812 * We ignore line number for SubRip
814 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
818 demux_sys_t *p_sys = p_demux->p_sys;
819 text_t *txt = &p_sys->txt;
824 const char *s = TextGetLine( txt );
825 int h1, m1, s1, d1, h2, m2, s2, d2;
830 if( sscanf( s, psz_fmt,
832 &h2, &m2, &s2, &d2 ) == 8 )
834 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
835 (int64_t)m1 * 60*1000 +
837 (int64_t)d1 ) * 1000;
839 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
840 (int64_t)m2 * 60*1000 +
842 (int64_t)d2 ) * 1000;
847 /* Now read text until an empty line */
848 psz_text = strdup("");
853 const char *s = TextGetLine( txt );
866 p_subtitle->psz_text = psz_text;
870 i_old = strlen( psz_text );
871 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
874 strcat( psz_text, s );
875 strcat( psz_text, "\n" );
877 /* replace [br] by \n */
882 while( ( p = strstr( psz_text, "[br]" ) ) )
885 memmove( p, &p[3], strlen(&p[3])+1 );
892 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
896 return ParseSubRipSubViewer( p_demux, p_subtitle,
897 "%d:%d:%d,%d --> %d:%d:%d,%d",
902 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
907 return ParseSubRipSubViewer( p_demux, p_subtitle,
908 "%d:%d:%d.%d,%d:%d:%d.%d",
914 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
917 demux_sys_t *p_sys = p_demux->p_sys;
918 text_t *txt = &p_sys->txt;
922 const char *s = TextGetLine( txt );
923 int h1, m1, s1, c1, h2, m2, s2, c2;
930 /* We expect (SSA2-4):
931 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
932 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
934 * 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.
938 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
939 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
942 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
943 psz_text = malloc( strlen(s) );
948 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
954 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
955 /* (Layer comes from ASS specs ... it's empty for SSA.) */
956 if( p_sys->i_type == SUB_TYPE_SSA1 )
958 /* SSA1 has only 8 commas before the text starts, not 9 */
959 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
964 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
966 /* ReadOrder, Layer, %s(rest of fields) */
967 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
968 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
969 memcpy( psz_text, temp, strlen(temp) );
972 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
973 (int64_t)m1 * 60*1000 +
975 (int64_t)c1 * 10 ) * 1000;
976 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
977 (int64_t)m2 * 60*1000 +
979 (int64_t)c2 * 10 ) * 1000;
980 p_subtitle->psz_text = psz_text;
985 /* All the other stuff we add to the header field */
986 if( !p_sys->psz_header )
987 p_sys->psz_header = strdup( "" );
988 if( !p_sys->psz_header )
992 realloc( p_sys->psz_header,
993 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
994 strcat( p_sys->psz_header, s );
995 strcat( p_sys->psz_header, "\n" );
1001 * h:m:s:Line1|Line2|Line3....
1003 * h:m:s Line1|Line2|Line3....
1005 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1008 VLC_UNUSED( i_idx );
1010 demux_sys_t *p_sys = p_demux->p_sys;
1011 text_t *txt = &p_sys->txt;
1017 const char *s = TextGetLine( txt );
1021 return VLC_EGENERIC;
1023 psz_text = malloc( strlen( s ) + 1 );
1027 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1028 &h1, &m1, &s1, psz_text ) == 4 )
1030 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1031 (int64_t)m1 * 60*1000 +
1032 (int64_t)s1 * 1000 ) * 1000;
1033 p_subtitle->i_stop = 0;
1039 /* replace | by \n */
1040 for( i = 0; psz_text[i] != '\0'; i++ )
1042 if( psz_text[i] == '|' )
1045 p_subtitle->psz_text = psz_text;
1051 static char *ParseSamiSearch( text_t *txt,
1052 char *psz_start, const char *psz_str )
1054 if( psz_start && strcasestr( psz_start, psz_str ) )
1056 char *s = strcasestr( psz_start, psz_str );
1057 return &s[strlen( psz_str )];
1062 char *p = TextGetLine( txt );
1066 if( strcasestr( p, psz_str ) )
1068 char *s = strcasestr( p, psz_str );
1069 return &s[strlen( psz_str )];
1073 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1075 VLC_UNUSED( i_idx );
1076 demux_sys_t *p_sys = p_demux->p_sys;
1077 text_t *txt = &p_sys->txt;
1082 unsigned int i_text;
1083 char text[8192]; /* Arbitrary but should be long enough */
1085 /* search "Start=" */
1086 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1087 return VLC_EGENERIC;
1089 /* get start value */
1090 i_start = strtol( s, &s, 0 );
1093 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1094 return VLC_EGENERIC;
1097 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1098 return VLC_EGENERIC;
1102 /* now get all txt until a "Start=" line */
1106 /* Search non empty line */
1107 while( s && *s == '\0' )
1108 s = TextGetLine( txt );
1114 if( !strncasecmp( s, "<br", 3 ) )
1118 else if( strcasestr( s, "Start=" ) )
1120 TextPreviousLine( txt );
1123 s = ParseSamiSearch( txt, s, ">" );
1125 else if( !strncmp( s, " ", 6 ) )
1130 else if( *s == '\t' )
1140 if( c != '\0' && i_text+1 < sizeof(text) )
1143 text[i_text] = '\0';
1147 p_subtitle->i_start = i_start * 1000;
1148 p_subtitle->i_stop = 0;
1149 p_subtitle->psz_text = strdup( text );
1161 * TODO it can have a header
1168 * LANG support would be cool
1169 * CODEPAGE is probably mandatory FIXME
1171 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1174 VLC_UNUSED( i_idx );
1176 demux_sys_t *p_sys = p_demux->p_sys;
1177 text_t *txt = &p_sys->txt;
1182 const char *s = TextGetLine( txt );
1186 return VLC_EGENERIC;
1190 &h1, &m1, &s1, &c1 ) == 4 )
1192 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1193 (int64_t)m1 * 60*1000 +
1194 (int64_t)s1 * 1000 +
1195 (int64_t)c1 * 10) * 1000;
1196 p_subtitle->i_stop = 0;
1201 /* Now read text until a line containing "}" */
1202 psz_text = strdup("");
1207 const char *s = TextGetLine( txt );
1214 return VLC_EGENERIC;
1217 i_len = strlen( s );
1218 if( i_len == 1 && s[0] == '}')
1220 p_subtitle->psz_text = psz_text;
1224 i_old = strlen( psz_text );
1225 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1228 strcat( psz_text, s );
1229 strcat( psz_text, "\n" );
1235 * [n1][n2]Line1|Line2|Line3...
1236 * where n1 and n2 are the video frame number (n2 can be empty)
1238 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1240 VLC_UNUSED( i_idx );
1242 demux_sys_t *p_sys = p_demux->p_sys;
1243 text_t *txt = &p_sys->txt;
1249 const char *s = TextGetLine( txt );
1254 return VLC_EGENERIC;
1256 psz_text = malloc( strlen(s) + 1 );
1262 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1263 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1265 p_subtitle->i_start = (int64_t)i_start * 100000;
1266 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1272 for( i = 0; psz_text[i] != '\0'; )
1274 /* replace | by \n */
1275 if( psz_text[i] == '|' )
1279 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1280 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1284 p_subtitle->psz_text = psz_text;
1288 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1290 VLC_UNUSED( i_idx );
1292 demux_sys_t *p_sys = p_demux->p_sys;
1293 text_t *txt = &p_sys->txt;
1294 char *psz_text = strdup( "" );
1296 int i_firstline = 1;
1302 const char *s = TextGetLine( txt );
1305 return VLC_EGENERIC;
1308 if( sscanf (s, "-->> %d", &t) == 1)
1310 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1311 p_subtitle->i_stop = 0;
1313 /* Starting of a subtitle */
1318 /* We have been too far: end of the subtitle, begin of next */
1328 i_old = strlen( psz_text ) + 1;
1329 psz_text = realloc( psz_text, i_old + strlen( s ) + 1 );
1332 strcat( psz_text, s );
1333 strcat( psz_text, "\n" );
1334 if( txt->i_line == txt->i_line_count )
1338 p_subtitle->psz_text = psz_text;
1342 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1344 VLC_UNUSED( i_idx );
1346 demux_sys_t *p_sys = p_demux->p_sys;
1347 text_t *txt = &p_sys->txt;
1352 const char *s = TextGetLine( txt );
1356 return VLC_EGENERIC;
1358 psz_text = malloc( strlen(s) + 1 );
1363 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1365 /* 1/10th of second ? Frame based ? FIXME */
1366 p_subtitle->i_start = 10 * t1;
1367 p_subtitle->i_stop = 10 * t2;
1368 /* Remove latest " */
1369 psz_text[ strlen(psz_text) - 1 ] = '\0';
1375 p_subtitle->psz_text = psz_text;
1376 msg_Dbg( p_demux, "%s", psz_text );
1380 static float mpsub_total = 0.0;
1381 static float mpsub_factor = 0.0;
1383 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1385 VLC_UNUSED( i_idx );
1387 demux_sys_t *p_sys = p_demux->p_sys;
1388 text_t *txt = &p_sys->txt;
1389 char *psz_text = strdup( "" );
1397 const char *s = TextGetLine( txt );
1399 return VLC_EGENERIC;
1401 if( strstr( s, "FORMAT" ) )
1403 if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1405 mpsub_factor = 100.0;
1409 psz_temp = malloc( strlen(s) );
1413 if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1416 f_fps = us_strtod( psz_temp, NULL );
1417 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1418 var_SetFloat( p_demux, "sub-fps", f_fps );
1427 if( sscanf (s, "%f %f", &f1, &f2 ) == 2 )
1429 mpsub_total += f1 * mpsub_factor;
1430 p_subtitle->i_start = (int64_t)(10000.0 * mpsub_total);
1431 mpsub_total += f2 * mpsub_factor;
1432 p_subtitle->i_stop = (int64_t)(10000.0 * mpsub_total);
1439 const char *s = TextGetLine( txt );
1442 return VLC_EGENERIC;
1444 int i_len = strlen( s );
1448 int i_old = strlen( psz_text );
1450 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1454 strcat( psz_text, s );
1455 strcat( psz_text, "\n" );
1458 p_subtitle->psz_text = psz_text;
1462 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1464 VLC_UNUSED( i_idx );
1466 demux_sys_t *p_sys = p_demux->p_sys;
1467 text_t *txt = &p_sys->txt;
1468 char *psz_text, *psz_orig;
1469 char *psz_text2, *psz_orig2;
1470 int h1, h2, m1, m2, s1, s2, f1, f2;
1471 static int i_comment = 0;
1473 static int jss_time_resolution = 30;
1474 static int jss_time_shift = 0;
1476 /* Parse the main lines */
1479 const char *s = TextGetLine( txt );
1481 return VLC_EGENERIC;
1483 psz_text = malloc( strlen( s ) + 1 );
1486 psz_orig = psz_text;
1488 /* Complete time lines */
1489 if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1490 &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1492 p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1493 (int64_t)( ( f1 + jss_time_shift ) / jss_time_resolution ) )
1495 p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1496 (int64_t)( ( f2 + jss_time_shift ) / jss_time_resolution ) )
1499 /* Short time lines */
1500 else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1502 p_subtitle->i_start = (int64_t)(
1503 ( f1 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
1504 p_subtitle->i_stop = (int64_t)(
1505 ( f2 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
1507 /* General Directive lines */
1508 /* Only TIME and SHIFT are supported so far */
1509 else if( s[0] == '#' )
1511 int h = 0, m =0, sec = 1, f = 1;
1515 strcpy( psz_text, s );
1517 switch( toupper( psz_text[1] ) )
1520 shift = isalpha( psz_text[2] ) ? 6 : 2 ;
1522 if( sscanf( &psz_text[shift], "%d", &h ) )
1524 /* Negative shifting */
1531 if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1533 if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1535 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1540 sscanf( &psz_text[shift], "%d:%d.%d",
1548 sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1551 jss_time_shift = ( ( h * 3600 + m * 60 + sec )
1552 * jss_time_resolution + f ) * inv;
1557 shift = isalpha( psz_text[2] ) ? 8 : 2 ;
1559 sscanf( &psz_text[shift], "%d", &jss_time_resolution );
1566 /* Unkown type line, probably a comment */
1572 /* Skip the blanks */
1573 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1575 /* Parse the directives */
1576 if( isalpha( *psz_text ) || *psz_text == '[' )
1578 while( *psz_text != ' ' )
1581 /* Directives are NOT parsed yet */
1582 /* This has probably a better place in a decoder ? */
1583 /* directive = malloc( strlen( psz_text ) + 1 );
1584 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1587 /* Skip the blanks after directives */
1588 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1591 /* Clean all the lines from inline comments and other stuffs */
1592 psz_text2 = calloc( strlen( psz_text) + 1, 1 );
1593 psz_orig2 = psz_text2;
1595 for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1606 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1618 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1627 if( (*(psz_text + 1 ) ) == 'n' )
1634 if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
1635 ( toupper(*(psz_text + 1 ) ) == 'F' ) )
1637 psz_text++; psz_text++;
1640 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1641 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1642 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1643 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1648 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1649 (*(psz_text + 1 ) ) == '\\' )
1651 else if( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n'
1652 || *(psz_text + 1 ) == '\0' )
1654 char *s2 = TextGetLine( txt );
1656 return VLC_EGENERIC;
1658 while ( *s2 == ' ' ) s2++;
1660 /* Here to parse the second line, we should add s2 to
1661 psz_text and go on the for( ) line 1556 in order to
1662 parse the next line.
1668 *psz_text2 = *psz_text;
1675 p_subtitle->psz_text = psz_orig2;