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,
121 static int TextLoad( text_t *, stream_t *s );
122 static void TextUnload( text_t * );
139 int64_t i_next_demux_date;
140 int64_t i_microsecperframe;
145 subtitle_t *subtitle;
150 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
151 static int ParseSubRip ( demux_t *, subtitle_t *, int );
152 static int ParseSubViewer ( demux_t *, subtitle_t *, int );
153 static int ParseSSA ( demux_t *, subtitle_t *, int );
154 static int ParseVplayer ( demux_t *, subtitle_t *, int );
155 static int ParseSami ( demux_t *, subtitle_t *, int );
156 static int ParseDVDSubtitle( demux_t *, subtitle_t *, int );
157 static int ParseMPL2 ( demux_t *, subtitle_t *, int );
158 static int ParseAQT ( demux_t *, subtitle_t *, int );
159 static int ParsePJS ( demux_t *, subtitle_t *, int );
160 static int ParseMPSub ( demux_t *, subtitle_t *, int );
161 static int ParseJSS ( demux_t *, subtitle_t *, int );
162 static int ParsePSB ( demux_t *, subtitle_t *, int );
166 const char *psz_type_name;
168 const char *psz_name;
169 int (*pf_read)( demux_t *, subtitle_t*, int );
170 } sub_read_subtitle_function [] =
172 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
173 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
174 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
175 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
176 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
177 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
178 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
179 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
180 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
181 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
182 { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT },
183 { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS },
184 { "mpsub", SUB_TYPE_MPSUB, "MPSub", ParseMPSub },
185 { "jacosub", SUB_TYPE_JACOSUB, "JacoSub", ParseJSS },
186 { "psb", SUB_TYPE_PSB, "PowerDivx", ParsePSB },
187 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
198 static int Demux( demux_t * );
199 static int Control( demux_t *, int, va_list );
201 /*static void Fix( demux_t * );*/
203 /*****************************************************************************
205 *****************************************************************************/
206 static int Open ( vlc_object_t *p_this )
208 demux_t *p_demux = (demux_t*)p_this;
213 int (*pf_read)( demux_t *, subtitle_t*, int );
216 if( !p_demux->b_force )
218 msg_Dbg( p_demux, "subtitle demux discarded" );
222 p_demux->pf_demux = Demux;
223 p_demux->pf_control = Control;
224 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
228 p_sys->psz_header = NULL;
229 p_sys->i_subtitle = 0;
230 p_sys->i_subtitles = 0;
231 p_sys->subtitle = NULL;
232 p_sys->i_microsecperframe = 40000;
235 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
237 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
239 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
241 /* Check for override of the fps */
242 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
245 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
246 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
249 /* Get or probe the type */
250 p_sys->i_type = SUB_TYPE_UNKNOWN;
251 psz_type = var_CreateGetString( p_demux, "sub-type" );
258 if( sub_read_subtitle_function[i].psz_type_name == NULL )
261 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
264 p_sys->i_type = sub_read_subtitle_function[i].i_type;
271 /* Probe if unknown type */
272 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
277 msg_Dbg( p_demux, "autodetecting subtitle format" );
278 for( i_try = 0; i_try < 256; i_try++ )
283 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
286 if( strcasestr( s, "<SAMI>" ) )
288 p_sys->i_type = SUB_TYPE_SAMI;
291 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
292 sscanf( s, "{%d}{}", &i_dummy ) == 1)
294 p_sys->i_type = SUB_TYPE_MICRODVD;
298 "%d:%d:%d,%d --> %d:%d:%d,%d",
299 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
300 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
302 p_sys->i_type = SUB_TYPE_SUBRIP;
305 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
307 p_sys->i_type = SUB_TYPE_SSA1;
310 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
312 p_sys->i_type = SUB_TYPE_ASS;
315 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
317 p_sys->i_type = SUB_TYPE_SSA2_4;
320 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
322 p_sys->i_type = SUB_TYPE_SSA2_4;
325 else if( !strncasecmp( s, "Dialogue:", 9 ) )
327 p_sys->i_type = SUB_TYPE_ASS;
330 else if( strcasestr( s, "[INFORMATION]" ) )
332 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
335 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 ||
336 sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2)
338 p_sys->i_type = SUB_TYPE_JACOSUB;
340 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
341 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
343 p_sys->i_type = SUB_TYPE_VPLAYER;
346 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
347 &i_dummy, &i_dummy ) == 4 )
349 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
352 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
353 sscanf( s, "[%d][]", &i_dummy ) == 1)
355 p_sys->i_type = SUB_TYPE_MPL2;
358 else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 ||
359 ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1
362 p_sys->i_type = SUB_TYPE_MPSUB;
364 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
366 p_sys->i_type = SUB_TYPE_AQT;
368 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
370 p_sys->i_type = SUB_TYPE_PJS;
372 else if( sscanf( s, "{%d:%d:%d}", &i_dummy, &i_dummy, &i_dummy ) == 3 )
374 p_sys->i_type = SUB_TYPE_PSB;
383 /* It will nearly always work even for non seekable stream thanks the
384 * caching system, and if it fails we lose just a few sub */
385 if( stream_Seek( p_demux->s, 0 ) )
387 msg_Warn( p_demux, "failed to rewind" );
390 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
392 msg_Err( p_demux, "failed to recognize subtitle type" );
399 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
401 msg_Dbg( p_demux, "detected %s format",
402 sub_read_subtitle_function[i].psz_name );
403 pf_read = sub_read_subtitle_function[i].pf_read;
408 msg_Dbg( p_demux, "loading all subtitles..." );
410 /* Load the whole file */
411 TextLoad( &p_sys->txt, p_demux->s );
416 if( p_sys->i_subtitles >= i_max )
419 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
420 sizeof(subtitle_t) * i_max ) ) )
422 msg_Err( p_demux, "out of memory");
423 free( p_sys->subtitle );
424 TextUnload( &p_sys->txt );
430 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
431 p_sys->i_subtitles ) )
434 p_sys->i_subtitles++;
437 TextUnload( &p_sys->txt );
439 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
441 /* Fix subtitle (order and time) *** */
442 p_sys->i_subtitle = 0;
444 if( p_sys->i_subtitles > 0 )
446 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
448 if( p_sys->i_length <= 0 )
449 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
452 /* *** add subtitle ES *** */
453 if( p_sys->i_type == SUB_TYPE_SSA1 ||
454 p_sys->i_type == SUB_TYPE_SSA2_4 ||
455 p_sys->i_type == SUB_TYPE_ASS )
457 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
461 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
463 if( p_sys->psz_header != NULL )
465 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
466 fmt.p_extra = strdup( p_sys->psz_header );
468 p_sys->es = es_out_Add( p_demux->out, &fmt );
473 /*****************************************************************************
474 * Close: Close subtitle demux
475 *****************************************************************************/
476 static void Close( vlc_object_t *p_this )
478 demux_t *p_demux = (demux_t*)p_this;
479 demux_sys_t *p_sys = p_demux->p_sys;
482 for( i = 0; i < p_sys->i_subtitles; i++ )
483 free( p_sys->subtitle[i].psz_text );
484 free( p_sys->subtitle );
489 /*****************************************************************************
491 *****************************************************************************/
492 static int Control( demux_t *p_demux, int i_query, va_list args )
494 demux_sys_t *p_sys = p_demux->p_sys;
500 case DEMUX_GET_LENGTH:
501 pi64 = (int64_t*)va_arg( args, int64_t * );
502 *pi64 = p_sys->i_length;
506 pi64 = (int64_t*)va_arg( args, int64_t * );
507 if( p_sys->i_subtitle < p_sys->i_subtitles )
509 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
515 i64 = (int64_t)va_arg( args, int64_t );
516 p_sys->i_subtitle = 0;
517 while( p_sys->i_subtitle < p_sys->i_subtitles &&
518 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
523 if( p_sys->i_subtitle >= p_sys->i_subtitles )
527 case DEMUX_GET_POSITION:
528 pf = (double*)va_arg( args, double * );
529 if( p_sys->i_subtitle >= p_sys->i_subtitles )
533 else if( p_sys->i_subtitles > 0 )
535 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
536 (double)p_sys->i_length;
544 case DEMUX_SET_POSITION:
545 f = (double)va_arg( args, double );
546 i64 = f * p_sys->i_length;
548 p_sys->i_subtitle = 0;
549 while( p_sys->i_subtitle < p_sys->i_subtitles &&
550 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
554 if( p_sys->i_subtitle >= p_sys->i_subtitles )
558 case DEMUX_SET_NEXT_DEMUX_TIME:
559 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
564 case DEMUX_GET_ATTACHMENTS:
565 case DEMUX_GET_TITLE_INFO:
566 case DEMUX_HAS_UNSUPPORTED_META:
570 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
575 /*****************************************************************************
576 * Demux: Send subtitle to decoder
577 *****************************************************************************/
578 static int Demux( demux_t *p_demux )
580 demux_sys_t *p_sys = p_demux->p_sys;
583 if( p_sys->i_subtitle >= p_sys->i_subtitles )
586 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
587 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
589 /* Should not happen */
590 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
593 while( p_sys->i_subtitle < p_sys->i_subtitles &&
594 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
597 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
606 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
612 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
618 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
619 p_block->i_dts = p_block->i_pts;
620 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
623 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
626 memcpy( p_block->p_buffer,
627 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
628 if( p_block->i_pts > 0 )
630 es_out_Send( p_demux->out, p_sys->es, p_block );
634 block_Release( p_block );
640 p_sys->i_next_demux_date = 0;
645 /*****************************************************************************
646 * Fix: fix time stamp and order of subtitle
647 *****************************************************************************/
648 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
649 static void Fix( demux_t *p_demux )
651 demux_sys_t *p_sys = p_demux->p_sys;
655 /* *** fix order (to be sure...) *** */
656 /* We suppose that there are near in order and this durty bubble sort
657 * wont take too much time
662 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
664 if( p_sys->subtitle[i_index].i_start <
665 p_sys->subtitle[i_index - 1].i_start )
669 p_sys->subtitle + i_index - 1,
670 sizeof( subtitle_t ) );
671 memcpy( p_sys->subtitle + i_index - 1,
672 p_sys->subtitle + i_index,
673 sizeof( subtitle_t ) );
674 memcpy( p_sys->subtitle + i_index,
676 sizeof( subtitle_t ) );
684 static int TextLoad( text_t *txt, stream_t *s )
690 txt->i_line_count = 0;
692 txt->line = calloc( i_line_max, sizeof( char * ) );
694 /* load the complete file */
697 char *psz = stream_ReadLine( s );
702 txt->line[txt->i_line_count++] = psz;
703 if( txt->i_line_count >= i_line_max )
706 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
710 if( txt->i_line_count <= 0 )
718 static void TextUnload( text_t *txt )
722 for( i = 0; i < txt->i_line_count; i++ )
724 free( txt->line[i] );
728 txt->i_line_count = 0;
731 static char *TextGetLine( text_t *txt )
733 if( txt->i_line >= txt->i_line_count )
736 return txt->line[txt->i_line++];
738 static void TextPreviousLine( text_t *txt )
740 if( txt->i_line > 0 )
744 /*****************************************************************************
745 * Specific Subtitle function
746 *****************************************************************************/
749 * {n1}{n2}Line1|Line2|Line3....
750 * where n1 and n2 are the video frame number (n2 can be empty)
752 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
756 demux_sys_t *p_sys = p_demux->p_sys;
757 text_t *txt = &p_sys->txt;
765 const char *s = TextGetLine( txt );
769 psz_text = malloc( strlen(s) + 1 );
775 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
776 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
779 if( i_start != 1 || i_stop != 1 )
782 /* We found a possible setting of the framerate "{1}{1}23.976" */
783 /* Check if it's usable, and if the sub-fps is not set */
784 f_fps = us_strtod( psz_text, NULL );
785 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
786 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
791 /* replace | by \n */
792 for( i = 0; psz_text[i] != '\0'; i++ )
794 if( psz_text[i] == '|' )
799 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
800 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
801 p_subtitle->psz_text = psz_text;
805 /* ParseSubRipSubViewer
808 * h1:m1:s1,d1 --> h2:m2:s2,d2
813 * Format SubViewer v1/v2
814 * h1:m1:s1.d1,h2:m2:s2.d2
819 * We ignore line number for SubRip
821 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
825 demux_sys_t *p_sys = p_demux->p_sys;
826 text_t *txt = &p_sys->txt;
831 const char *s = TextGetLine( txt );
832 int h1, m1, s1, d1, h2, m2, s2, d2;
837 if( sscanf( s, psz_fmt,
839 &h2, &m2, &s2, &d2 ) == 8 )
841 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
842 (int64_t)m1 * 60*1000 +
844 (int64_t)d1 ) * 1000;
846 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
847 (int64_t)m2 * 60*1000 +
849 (int64_t)d2 ) * 1000;
854 /* Now read text until an empty line */
855 psz_text = strdup("");
860 const char *s = TextGetLine( txt );
873 p_subtitle->psz_text = psz_text;
877 i_old = strlen( psz_text );
878 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
881 strcat( psz_text, s );
882 strcat( psz_text, "\n" );
884 /* replace [br] by \n */
889 while( ( p = strstr( psz_text, "[br]" ) ) )
892 memmove( p, &p[3], strlen(&p[3])+1 );
899 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
903 return ParseSubRipSubViewer( p_demux, p_subtitle,
904 "%d:%d:%d,%d --> %d:%d:%d,%d",
909 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
914 return ParseSubRipSubViewer( p_demux, p_subtitle,
915 "%d:%d:%d.%d,%d:%d:%d.%d",
921 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
924 demux_sys_t *p_sys = p_demux->p_sys;
925 text_t *txt = &p_sys->txt;
929 const char *s = TextGetLine( txt );
930 int h1, m1, s1, c1, h2, m2, s2, c2;
937 /* We expect (SSA2-4):
938 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
939 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
941 * 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.
945 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
946 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
949 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
950 psz_text = malloc( strlen(s) );
955 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
961 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
962 /* (Layer comes from ASS specs ... it's empty for SSA.) */
963 if( p_sys->i_type == SUB_TYPE_SSA1 )
965 /* SSA1 has only 8 commas before the text starts, not 9 */
966 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
971 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
973 /* ReadOrder, Layer, %s(rest of fields) */
974 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
975 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
976 memcpy( psz_text, temp, strlen(temp) );
979 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
980 (int64_t)m1 * 60*1000 +
982 (int64_t)c1 * 10 ) * 1000;
983 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
984 (int64_t)m2 * 60*1000 +
986 (int64_t)c2 * 10 ) * 1000;
987 p_subtitle->psz_text = psz_text;
992 /* All the other stuff we add to the header field */
993 if( !p_sys->psz_header )
994 p_sys->psz_header = strdup( "" );
995 if( !p_sys->psz_header )
999 realloc( p_sys->psz_header,
1000 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
1001 strcat( p_sys->psz_header, s );
1002 strcat( p_sys->psz_header, "\n" );
1008 * h:m:s:Line1|Line2|Line3....
1010 * h:m:s Line1|Line2|Line3....
1012 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
1015 VLC_UNUSED( i_idx );
1017 demux_sys_t *p_sys = p_demux->p_sys;
1018 text_t *txt = &p_sys->txt;
1024 const char *s = TextGetLine( txt );
1028 return VLC_EGENERIC;
1030 psz_text = malloc( strlen( s ) + 1 );
1034 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
1035 &h1, &m1, &s1, psz_text ) == 4 )
1037 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1038 (int64_t)m1 * 60*1000 +
1039 (int64_t)s1 * 1000 ) * 1000;
1040 p_subtitle->i_stop = 0;
1046 /* replace | by \n */
1047 for( i = 0; psz_text[i] != '\0'; i++ )
1049 if( psz_text[i] == '|' )
1052 p_subtitle->psz_text = psz_text;
1058 static char *ParseSamiSearch( text_t *txt,
1059 char *psz_start, const char *psz_str )
1061 if( psz_start && strcasestr( psz_start, psz_str ) )
1063 char *s = strcasestr( psz_start, psz_str );
1064 return &s[strlen( psz_str )];
1069 char *p = TextGetLine( txt );
1073 if( strcasestr( p, psz_str ) )
1075 char *s = strcasestr( p, psz_str );
1076 return &s[strlen( psz_str )];
1080 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1082 VLC_UNUSED( i_idx );
1083 demux_sys_t *p_sys = p_demux->p_sys;
1084 text_t *txt = &p_sys->txt;
1089 unsigned int i_text;
1090 char text[8192]; /* Arbitrary but should be long enough */
1092 /* search "Start=" */
1093 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1094 return VLC_EGENERIC;
1096 /* get start value */
1097 i_start = strtol( s, &s, 0 );
1100 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1101 return VLC_EGENERIC;
1104 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1105 return VLC_EGENERIC;
1109 /* now get all txt until a "Start=" line */
1113 /* Search non empty line */
1114 while( s && *s == '\0' )
1115 s = TextGetLine( txt );
1121 if( !strncasecmp( s, "<br", 3 ) )
1125 else if( strcasestr( s, "Start=" ) )
1127 TextPreviousLine( txt );
1130 s = ParseSamiSearch( txt, s, ">" );
1132 else if( !strncmp( s, " ", 6 ) )
1137 else if( *s == '\t' )
1147 if( c != '\0' && i_text+1 < sizeof(text) )
1150 text[i_text] = '\0';
1154 p_subtitle->i_start = i_start * 1000;
1155 p_subtitle->i_stop = 0;
1156 p_subtitle->psz_text = strdup( text );
1168 * TODO it can have a header
1175 * LANG support would be cool
1176 * CODEPAGE is probably mandatory FIXME
1178 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1181 VLC_UNUSED( i_idx );
1183 demux_sys_t *p_sys = p_demux->p_sys;
1184 text_t *txt = &p_sys->txt;
1189 const char *s = TextGetLine( txt );
1193 return VLC_EGENERIC;
1197 &h1, &m1, &s1, &c1 ) == 4 )
1199 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1200 (int64_t)m1 * 60*1000 +
1201 (int64_t)s1 * 1000 +
1202 (int64_t)c1 * 10) * 1000;
1203 p_subtitle->i_stop = 0;
1208 /* Now read text until a line containing "}" */
1209 psz_text = strdup("");
1214 const char *s = TextGetLine( txt );
1221 return VLC_EGENERIC;
1224 i_len = strlen( s );
1225 if( i_len == 1 && s[0] == '}')
1227 p_subtitle->psz_text = psz_text;
1231 i_old = strlen( psz_text );
1232 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1235 strcat( psz_text, s );
1236 strcat( psz_text, "\n" );
1242 * [n1][n2]Line1|Line2|Line3...
1243 * where n1 and n2 are the video frame number (n2 can be empty)
1245 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1247 VLC_UNUSED( i_idx );
1249 demux_sys_t *p_sys = p_demux->p_sys;
1250 text_t *txt = &p_sys->txt;
1256 const char *s = TextGetLine( txt );
1261 return VLC_EGENERIC;
1263 psz_text = malloc( strlen(s) + 1 );
1269 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1270 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1272 p_subtitle->i_start = (int64_t)i_start * 100000;
1273 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1279 for( i = 0; psz_text[i] != '\0'; )
1281 /* replace | by \n */
1282 if( psz_text[i] == '|' )
1286 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1287 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1291 p_subtitle->psz_text = psz_text;
1295 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1297 VLC_UNUSED( i_idx );
1299 demux_sys_t *p_sys = p_demux->p_sys;
1300 text_t *txt = &p_sys->txt;
1301 char *psz_text = strdup( "" );
1303 int i_firstline = 1;
1309 const char *s = TextGetLine( txt );
1312 return VLC_EGENERIC;
1315 if( sscanf (s, "-->> %d", &t) == 1)
1317 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1318 p_subtitle->i_stop = 0;
1320 /* Starting of a subtitle */
1325 /* We have been too far: end of the subtitle, begin of next */
1335 i_old = strlen( psz_text ) + 1;
1336 psz_text = realloc( psz_text, i_old + strlen( s ) + 1 );
1339 strcat( psz_text, s );
1340 strcat( psz_text, "\n" );
1341 if( txt->i_line == txt->i_line_count )
1345 p_subtitle->psz_text = psz_text;
1349 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1351 VLC_UNUSED( i_idx );
1353 demux_sys_t *p_sys = p_demux->p_sys;
1354 text_t *txt = &p_sys->txt;
1360 const char *s = TextGetLine( txt );
1364 return VLC_EGENERIC;
1366 psz_text = malloc( strlen(s) + 1 );
1371 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1373 /* 1/10th of second ? Frame based ? FIXME */
1374 p_subtitle->i_start = 10 * t1;
1375 p_subtitle->i_stop = 10 * t2;
1376 /* Remove latest " */
1377 psz_text[ strlen(psz_text) - 1 ] = '\0';
1384 /* replace | by \n */
1385 for( i = 0; psz_text[i] != '\0'; i++ )
1387 if( psz_text[i] == '|' )
1391 p_subtitle->psz_text = psz_text;
1392 msg_Dbg( p_demux, "%s", psz_text );
1396 static float mpsub_total = 0.0;
1397 static float mpsub_factor = 0.0;
1399 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1401 VLC_UNUSED( i_idx );
1403 demux_sys_t *p_sys = p_demux->p_sys;
1404 text_t *txt = &p_sys->txt;
1405 char *psz_text = strdup( "" );
1413 const char *s = TextGetLine( txt );
1415 return VLC_EGENERIC;
1417 if( strstr( s, "FORMAT" ) )
1419 if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E')
1421 mpsub_factor = 100.0;
1425 psz_temp = malloc( strlen(s) );
1429 if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) )
1432 f_fps = us_strtod( psz_temp, NULL );
1433 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
1434 var_SetFloat( p_demux, "sub-fps", f_fps );
1443 if( sscanf (s, "%f %f", &f1, &f2 ) == 2 )
1445 mpsub_total += f1 * mpsub_factor;
1446 p_subtitle->i_start = (int64_t)(10000.0 * mpsub_total);
1447 mpsub_total += f2 * mpsub_factor;
1448 p_subtitle->i_stop = (int64_t)(10000.0 * mpsub_total);
1455 const char *s = TextGetLine( txt );
1458 return VLC_EGENERIC;
1460 int i_len = strlen( s );
1464 int i_old = strlen( psz_text );
1466 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1470 strcat( psz_text, s );
1471 strcat( psz_text, "\n" );
1474 p_subtitle->psz_text = psz_text;
1478 static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1480 VLC_UNUSED( i_idx );
1482 demux_sys_t *p_sys = p_demux->p_sys;
1483 text_t *txt = &p_sys->txt;
1484 char *psz_text, *psz_orig;
1485 char *psz_text2, *psz_orig2;
1486 int h1, h2, m1, m2, s1, s2, f1, f2;
1487 static int i_comment = 0;
1489 static int jss_time_resolution = 30;
1490 static int jss_time_shift = 0;
1492 /* Parse the main lines */
1495 const char *s = TextGetLine( txt );
1497 return VLC_EGENERIC;
1499 psz_text = malloc( strlen( s ) + 1 );
1502 psz_orig = psz_text;
1504 /* Complete time lines */
1505 if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1506 &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 )
1508 p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) +
1509 (int64_t)( ( f1 + jss_time_shift ) / jss_time_resolution ) )
1511 p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) +
1512 (int64_t)( ( f2 + jss_time_shift ) / jss_time_resolution ) )
1515 /* Short time lines */
1516 else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 )
1518 p_subtitle->i_start = (int64_t)(
1519 ( f1 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
1520 p_subtitle->i_stop = (int64_t)(
1521 ( f2 + jss_time_shift ) / jss_time_resolution * 1000000.0 );
1523 /* General Directive lines */
1524 /* Only TIME and SHIFT are supported so far */
1525 else if( s[0] == '#' )
1527 int h = 0, m =0, sec = 1, f = 1;
1531 strcpy( psz_text, s );
1533 switch( toupper( psz_text[1] ) )
1536 shift = isalpha( psz_text[2] ) ? 6 : 2 ;
1538 if( sscanf( &psz_text[shift], "%d", &h ) )
1540 /* Negative shifting */
1547 if( sscanf( &psz_text[shift], "%*d:%d", &m ) )
1549 if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) )
1551 sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f );
1556 sscanf( &psz_text[shift], "%d:%d.%d",
1564 sscanf( &psz_text[shift], "%d.%d", &sec, &f);
1567 jss_time_shift = ( ( h * 3600 + m * 60 + sec )
1568 * jss_time_resolution + f ) * inv;
1573 shift = isalpha( psz_text[2] ) ? 8 : 2 ;
1575 sscanf( &psz_text[shift], "%d", &jss_time_resolution );
1582 /* Unkown type line, probably a comment */
1588 /* Skip the blanks */
1589 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1591 /* Parse the directives */
1592 if( isalpha( *psz_text ) || *psz_text == '[' )
1594 while( *psz_text != ' ' )
1597 /* Directives are NOT parsed yet */
1598 /* This has probably a better place in a decoder ? */
1599 /* directive = malloc( strlen( psz_text ) + 1 );
1600 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1603 /* Skip the blanks after directives */
1604 while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++;
1607 /* Clean all the lines from inline comments and other stuffs */
1608 psz_text2 = calloc( strlen( psz_text) + 1, 1 );
1609 psz_orig2 = psz_text2;
1611 for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; )
1622 if( (*(psz_text + 1 ) ) == ' ' ) psz_text++;
1634 if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' )
1643 if( (*(psz_text + 1 ) ) == 'n' )
1650 if( ( toupper(*(psz_text + 1 ) ) == 'C' ) ||
1651 ( toupper(*(psz_text + 1 ) ) == 'F' ) )
1653 psz_text++; psz_text++;
1656 if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' ||
1657 (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' ||
1658 (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' ||
1659 (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' )
1664 if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' ||
1665 (*(psz_text + 1 ) ) == '\\' )
1667 else if( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n'
1668 || *(psz_text + 1 ) == '\0' )
1670 char *s2 = TextGetLine( txt );
1672 return VLC_EGENERIC;
1674 while ( *s2 == ' ' ) s2++;
1676 /* Here to parse the second line, we should add s2 to
1677 psz_text and go on the for( ) line 1556 in order to
1678 parse the next line.
1684 *psz_text2 = *psz_text;
1691 p_subtitle->psz_text = psz_orig2;
1697 static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1699 VLC_UNUSED( i_idx );
1701 demux_sys_t *p_sys = p_demux->p_sys;
1702 text_t *txt = &p_sys->txt;
1710 const char *s = TextGetLine( txt );
1713 return VLC_EGENERIC;
1715 psz_text = malloc( strlen( s ) + 1 );
1719 if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1720 &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 )
1722 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1723 (int64_t)m1 * 60*1000 +
1724 (int64_t)s1 * 1000 ) * 1000;
1725 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
1726 (int64_t)m2 * 60*1000 +
1727 (int64_t)s2 * 1000 ) * 1000;
1733 /* replace | by \n */
1734 for( i = 0; psz_text[i] != '\0'; i++ )
1736 if( psz_text[i] == '|' )
1739 p_subtitle->psz_text = psz_text;