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>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_input.h>
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
42 #include <vlc_demux.h>
43 #include <vlc_charset.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t *p_this );
49 static void Close( vlc_object_t *p_this );
51 #define SUB_DELAY_LONGTEXT \
52 N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).")
53 #define SUB_FPS_LONGTEXT \
54 N_("Override the normal frames per second settings. " \
55 "This will only work with MicroDVD and SubRIP (SRT) subtitles.")
56 #define SUB_TYPE_LONGTEXT \
57 N_("Force the subtiles format. Valid values are : \"microdvd\", " \
58 "\"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \
59 "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\" and \"auto\" (meaning autodetection, this " \
60 "should always work).")
61 static const char *ppsz_sub_type[] =
63 "auto", "microdvd", "subrip", "subviewer", "ssa1",
64 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
69 set_shortname( _("Subtitles"));
70 set_description( _("Text subtitles parser") );
71 set_capability( "demux", 0 );
72 set_category( CAT_INPUT );
73 set_subcategory( SUBCAT_INPUT_DEMUX );
74 add_float( "sub-fps", 0.0, NULL,
75 N_("Frames per second"),
76 SUB_FPS_LONGTEXT, true );
77 add_integer( "sub-delay", 0, NULL,
78 N_("Subtitles delay"),
79 SUB_DELAY_LONGTEXT, true );
80 add_string( "sub-type", "auto", NULL, N_("Subtitles format"),
81 SUB_TYPE_LONGTEXT, true );
82 change_string_list( ppsz_sub_type, NULL, NULL );
83 set_callbacks( Open, Close );
85 add_shortcut( "subtitle" );
88 /*****************************************************************************
90 *****************************************************************************/
93 SUB_TYPE_UNKNOWN = -1,
102 SUB_TYPE_DVDSUBTITLE,
115 static int TextLoad( text_t *, stream_t *s );
116 static void TextUnload( text_t * );
133 int64_t i_next_demux_date;
134 int64_t i_microsecperframe;
139 subtitle_t *subtitle;
144 static int ParseMicroDvd ( demux_t *, subtitle_t *, int );
145 static int ParseSubRip ( demux_t *, subtitle_t *, int );
146 static int ParseSubViewer ( demux_t *, subtitle_t *, int );
147 static int ParseSSA ( demux_t *, subtitle_t *, int );
148 static int ParseVplayer ( demux_t *, subtitle_t *, int );
149 static int ParseSami ( demux_t *, subtitle_t *, int );
150 static int ParseDVDSubtitle( demux_t *, subtitle_t *, int );
151 static int ParseMPL2 ( demux_t *, subtitle_t *, int );
152 static int ParseAQT ( demux_t *, subtitle_t *, int );
153 static int ParsePJS ( demux_t *, subtitle_t *, int );
154 static int ParseMPSub ( demux_t *, subtitle_t *, int );
158 const char *psz_type_name;
160 const char *psz_name;
161 int (*pf_read)( demux_t *, subtitle_t*, int );
162 } sub_read_subtitle_function [] =
164 { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd },
165 { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip },
166 { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer },
167 { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA },
168 { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA },
169 { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA },
170 { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer },
171 { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami },
172 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle },
173 { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 },
174 { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT },
175 { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS },
176 { "mpsub", SUB_TYPE_MPSUB, "MPSub", ParseMPSub },
177 { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL }
180 static int Demux( demux_t * );
181 static int Control( demux_t *, int, va_list );
183 /*static void Fix( demux_t * );*/
185 /*****************************************************************************
187 *****************************************************************************/
188 static int Open ( vlc_object_t *p_this )
190 demux_t *p_demux = (demux_t*)p_this;
195 int (*pf_read)( demux_t *, subtitle_t*, int );
198 if( !p_demux->b_force )
200 msg_Dbg( p_demux, "subtitle demux discarded" );
204 p_demux->pf_demux = Demux;
205 p_demux->pf_control = Control;
206 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
207 p_sys->psz_header = NULL;
208 p_sys->i_subtitle = 0;
209 p_sys->i_subtitles = 0;
210 p_sys->subtitle = NULL;
211 p_sys->i_microsecperframe = 40000;
214 f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" );
216 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
218 msg_Dbg( p_demux, "Movie fps: %f", f_fps );
220 /* Check for override of the fps */
221 f_fps = var_CreateGetFloat( p_demux, "sub-fps" );
224 p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps );
225 msg_Dbg( p_demux, "Override subtitle fps %f", f_fps );
228 /* Get or probe the type */
229 p_sys->i_type = SUB_TYPE_UNKNOWN;
230 psz_type = var_CreateGetString( p_demux, "sub-type" );
237 if( sub_read_subtitle_function[i].psz_type_name == NULL )
240 if( !strcmp( sub_read_subtitle_function[i].psz_type_name,
243 p_sys->i_type = sub_read_subtitle_function[i].i_type;
250 /* Probe if unknown type */
251 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
256 msg_Dbg( p_demux, "autodetecting subtitle format" );
257 for( i_try = 0; i_try < 256; i_try++ )
262 if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
265 if( strcasestr( s, "<SAMI>" ) )
267 p_sys->i_type = SUB_TYPE_SAMI;
270 else if( sscanf( s, "{%d}{%d}", &i_dummy, &i_dummy ) == 2 ||
271 sscanf( s, "{%d}{}", &i_dummy ) == 1)
273 p_sys->i_type = SUB_TYPE_MICRODVD;
277 "%d:%d:%d,%d --> %d:%d:%d,%d",
278 &i_dummy,&i_dummy,&i_dummy,&i_dummy,
279 &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 )
281 p_sys->i_type = SUB_TYPE_SUBRIP;
284 else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) )
286 p_sys->i_type = SUB_TYPE_SSA1;
289 else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) )
291 p_sys->i_type = SUB_TYPE_ASS;
294 else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) )
296 p_sys->i_type = SUB_TYPE_SSA2_4;
299 else if( !strncasecmp( s, "Dialogue: Marked", 16 ) )
301 p_sys->i_type = SUB_TYPE_SSA2_4;
304 else if( !strncasecmp( s, "Dialogue:", 9 ) )
306 p_sys->i_type = SUB_TYPE_ASS;
309 else if( strcasestr( s, "[INFORMATION]" ) )
311 p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */
314 else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 ||
315 sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 )
317 p_sys->i_type = SUB_TYPE_VPLAYER;
320 else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy,
321 &i_dummy, &i_dummy ) == 4 )
323 p_sys->i_type = SUB_TYPE_DVDSUBTITLE;
326 else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 ||
327 sscanf( s, "[%d][]", &i_dummy ) == 1)
329 p_sys->i_type = SUB_TYPE_MPL2;
332 else if( sscanf( s, "%f %f", &f_dummy, &f_dummy ) == 2 )
334 p_sys->i_type = SUB_TYPE_MPSUB;
336 else if( sscanf( s, "-->> %d", &i_dummy) == 1 )
338 p_sys->i_type = SUB_TYPE_AQT;
340 else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 )
342 p_sys->i_type = SUB_TYPE_PJS;
351 /* It will nearly always work even for non seekable stream thanks the
352 * caching system, and if it fails we lose just a few sub */
353 if( stream_Seek( p_demux->s, 0 ) )
355 msg_Warn( p_demux, "failed to rewind" );
358 if( p_sys->i_type == SUB_TYPE_UNKNOWN )
360 msg_Err( p_demux, "failed to recognize subtitle type" );
367 if( sub_read_subtitle_function[i].i_type == p_sys->i_type )
369 msg_Dbg( p_demux, "detected %s format",
370 sub_read_subtitle_function[i].psz_name );
371 pf_read = sub_read_subtitle_function[i].pf_read;
376 msg_Dbg( p_demux, "loading all subtitles..." );
378 /* Load the whole file */
379 TextLoad( &p_sys->txt, p_demux->s );
384 if( p_sys->i_subtitles >= i_max )
387 if( !( p_sys->subtitle = realloc( p_sys->subtitle,
388 sizeof(subtitle_t) * i_max ) ) )
390 msg_Err( p_demux, "out of memory");
391 free( p_sys->subtitle );
392 TextUnload( &p_sys->txt );
398 if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles],
399 p_sys->i_subtitles ) )
402 p_sys->i_subtitles++;
405 TextUnload( &p_sys->txt );
407 msg_Dbg(p_demux, "loaded %d subtitles", p_sys->i_subtitles );
409 /* Fix subtitle (order and time) *** */
410 p_sys->i_subtitle = 0;
412 if( p_sys->i_subtitles > 0 )
414 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_stop;
416 if( p_sys->i_length <= 0 )
417 p_sys->i_length = p_sys->subtitle[p_sys->i_subtitles-1].i_start+1;
420 /* *** add subtitle ES *** */
421 if( p_sys->i_type == SUB_TYPE_SSA1 ||
422 p_sys->i_type == SUB_TYPE_SSA2_4 ||
423 p_sys->i_type == SUB_TYPE_ASS )
425 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) );
429 es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) );
431 if( p_sys->psz_header != NULL )
433 fmt.i_extra = strlen( p_sys->psz_header ) + 1;
434 fmt.p_extra = strdup( p_sys->psz_header );
436 p_sys->es = es_out_Add( p_demux->out, &fmt );
441 /*****************************************************************************
442 * Close: Close subtitle demux
443 *****************************************************************************/
444 static void Close( vlc_object_t *p_this )
446 demux_t *p_demux = (demux_t*)p_this;
447 demux_sys_t *p_sys = p_demux->p_sys;
450 for( i = 0; i < p_sys->i_subtitles; i++ )
451 free( p_sys->subtitle[i].psz_text );
452 free( p_sys->subtitle );
457 /*****************************************************************************
459 *****************************************************************************/
460 static int Control( demux_t *p_demux, int i_query, va_list args )
462 demux_sys_t *p_sys = p_demux->p_sys;
468 case DEMUX_GET_LENGTH:
469 pi64 = (int64_t*)va_arg( args, int64_t * );
470 *pi64 = p_sys->i_length;
474 pi64 = (int64_t*)va_arg( args, int64_t * );
475 if( p_sys->i_subtitle < p_sys->i_subtitles )
477 *pi64 = p_sys->subtitle[p_sys->i_subtitle].i_start;
483 i64 = (int64_t)va_arg( args, int64_t );
484 p_sys->i_subtitle = 0;
485 while( p_sys->i_subtitle < p_sys->i_subtitles &&
486 p_sys->subtitle[p_sys->i_subtitle].i_start < i64 )
491 if( p_sys->i_subtitle >= p_sys->i_subtitles )
495 case DEMUX_GET_POSITION:
496 pf = (double*)va_arg( args, double * );
497 if( p_sys->i_subtitle >= p_sys->i_subtitles )
501 else if( p_sys->i_subtitles > 0 )
503 *pf = (double)p_sys->subtitle[p_sys->i_subtitle].i_start /
504 (double)p_sys->i_length;
512 case DEMUX_SET_POSITION:
513 f = (double)va_arg( args, double );
514 i64 = f * p_sys->i_length;
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 )
522 if( p_sys->i_subtitle >= p_sys->i_subtitles )
526 case DEMUX_SET_NEXT_DEMUX_TIME:
527 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
532 case DEMUX_GET_ATTACHMENTS:
533 case DEMUX_GET_TITLE_INFO:
534 case DEMUX_HAS_UNSUPPORTED_META:
538 msg_Err( p_demux, "unknown query %d in subtitle control", i_query );
543 /*****************************************************************************
544 * Demux: Send subtitle to decoder
545 *****************************************************************************/
546 static int Demux( demux_t *p_demux )
548 demux_sys_t *p_sys = p_demux->p_sys;
551 if( p_sys->i_subtitle >= p_sys->i_subtitles )
554 i_maxdate = p_sys->i_next_demux_date - var_GetTime( p_demux->p_parent, "spu-delay" );;
555 if( i_maxdate <= 0 && p_sys->i_subtitle < p_sys->i_subtitles )
557 /* Should not happen */
558 i_maxdate = p_sys->subtitle[p_sys->i_subtitle].i_start + 1;
561 while( p_sys->i_subtitle < p_sys->i_subtitles &&
562 p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate )
565 int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1;
574 if( ( p_block = block_New( p_demux, i_len ) ) == NULL )
580 if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 )
586 p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start;
587 p_block->i_dts = p_block->i_pts;
588 if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 )
591 p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts;
594 memcpy( p_block->p_buffer,
595 p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len );
596 if( p_block->i_pts > 0 )
598 es_out_Send( p_demux->out, p_sys->es, p_block );
602 block_Release( p_block );
608 p_sys->i_next_demux_date = 0;
613 /*****************************************************************************
614 * Fix: fix time stamp and order of subtitle
615 *****************************************************************************/
616 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
617 static void Fix( demux_t *p_demux )
619 demux_sys_t *p_sys = p_demux->p_sys;
623 /* *** fix order (to be sure...) *** */
624 /* We suppose that there are near in order and this durty bubble sort
625 * wont take too much time
630 for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ )
632 if( p_sys->subtitle[i_index].i_start <
633 p_sys->subtitle[i_index - 1].i_start )
637 p_sys->subtitle + i_index - 1,
638 sizeof( subtitle_t ) );
639 memcpy( p_sys->subtitle + i_index - 1,
640 p_sys->subtitle + i_index,
641 sizeof( subtitle_t ) );
642 memcpy( p_sys->subtitle + i_index,
644 sizeof( subtitle_t ) );
652 static int TextLoad( text_t *txt, stream_t *s )
658 txt->i_line_count = 0;
660 txt->line = calloc( i_line_max, sizeof( char * ) );
662 /* load the complete file */
665 char *psz = stream_ReadLine( s );
670 txt->line[txt->i_line_count++] = psz;
671 if( txt->i_line_count >= i_line_max )
674 txt->line = realloc( txt->line, i_line_max * sizeof( char * ) );
678 if( txt->i_line_count <= 0 )
686 static void TextUnload( text_t *txt )
690 for( i = 0; i < txt->i_line_count; i++ )
692 free( txt->line[i] );
696 txt->i_line_count = 0;
699 static char *TextGetLine( text_t *txt )
701 if( txt->i_line >= txt->i_line_count )
704 return txt->line[txt->i_line++];
706 static void TextPreviousLine( text_t *txt )
708 if( txt->i_line > 0 )
712 /*****************************************************************************
713 * Specific Subtitle function
714 *****************************************************************************/
717 * {n1}{n2}Line1|Line2|Line3....
718 * where n1 and n2 are the video frame number (n2 can be empty)
720 static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle,
723 demux_sys_t *p_sys = p_demux->p_sys;
724 text_t *txt = &p_sys->txt;
732 const char *s = TextGetLine( txt );
736 psz_text = malloc( strlen(s) + 1 );
742 if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 ||
743 sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
746 if( i_start != 1 || i_stop != 1 )
749 /* We found a possible setting of the framerate "{1}{1}23.976" */
750 /* Check if it's usable, and if the sub-fps is not set */
751 f_fps = us_strtod( psz_text, NULL );
752 if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 )
753 p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps);
758 /* replace | by \n */
759 for( i = 0; psz_text[i] != '\0'; i++ )
761 if( psz_text[i] == '|' )
766 p_subtitle->i_start = i_start * p_sys->i_microsecperframe;
767 p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe;
768 p_subtitle->psz_text = psz_text;
772 /* ParseSubRipSubViewer
775 * h1:m1:s1,d1 --> h2:m2:s2,d2
780 * Format SubViewer v1/v2
781 * h1:m1:s1.d1,h2:m2:s2.d2
786 * We ignore line number for SubRip
788 static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
792 demux_sys_t *p_sys = p_demux->p_sys;
793 text_t *txt = &p_sys->txt;
798 const char *s = TextGetLine( txt );
799 int h1, m1, s1, d1, h2, m2, s2, d2;
804 if( sscanf( s, psz_fmt,
806 &h2, &m2, &s2, &d2 ) == 8 )
808 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
809 (int64_t)m1 * 60*1000 +
811 (int64_t)d1 ) * 1000;
813 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
814 (int64_t)m2 * 60*1000 +
816 (int64_t)d2 ) * 1000;
821 /* Now read text until an empty line */
822 psz_text = strdup("");
827 const char *s = TextGetLine( txt );
840 p_subtitle->psz_text = psz_text;
844 i_old = strlen( psz_text );
845 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
848 strcat( psz_text, s );
849 strcat( psz_text, "\n" );
851 /* replace [br] by \n */
856 while( ( p = strstr( psz_text, "[br]" ) ) )
859 memmove( p, &p[3], strlen(&p[3])+1 );
866 static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle,
869 return ParseSubRipSubViewer( p_demux, p_subtitle,
870 "%d:%d:%d,%d --> %d:%d:%d,%d",
875 static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle,
878 return ParseSubRipSubViewer( p_demux, p_subtitle,
879 "%d:%d:%d.%d,%d:%d:%d.%d",
885 static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle,
888 demux_sys_t *p_sys = p_demux->p_sys;
889 text_t *txt = &p_sys->txt;
893 const char *s = TextGetLine( txt );
894 int h1, m1, s1, c1, h2, m2, s2, c2;
901 /* We expect (SSA2-4):
902 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
903 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
905 * 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.
909 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
910 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
913 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
914 psz_text = malloc( strlen(s) );
919 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
925 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
926 /* (Layer comes from ASS specs ... it's empty for SSA.) */
927 if( p_sys->i_type == SUB_TYPE_SSA1 )
929 /* SSA1 has only 8 commas before the text starts, not 9 */
930 memmove( &psz_text[1], psz_text, strlen(psz_text)+1 );
935 int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0;
937 /* ReadOrder, Layer, %s(rest of fields) */
938 snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer );
939 memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 );
940 memcpy( psz_text, temp, strlen(temp) );
943 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
944 (int64_t)m1 * 60*1000 +
946 (int64_t)c1 * 10 ) * 1000;
947 p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 +
948 (int64_t)m2 * 60*1000 +
950 (int64_t)c2 * 10 ) * 1000;
951 p_subtitle->psz_text = psz_text;
956 /* All the other stuff we add to the header field */
957 if( !p_sys->psz_header )
958 p_sys->psz_header = strdup( "" );
959 if( !p_sys->psz_header )
963 realloc( p_sys->psz_header,
964 strlen( p_sys->psz_header ) + strlen( s ) + 2 );
965 strcat( p_sys->psz_header, s );
966 strcat( p_sys->psz_header, "\n" );
972 * h:m:s:Line1|Line2|Line3....
974 * h:m:s Line1|Line2|Line3....
976 static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle,
979 demux_sys_t *p_sys = p_demux->p_sys;
980 text_t *txt = &p_sys->txt;
986 const char *s = TextGetLine( txt );
992 psz_text = malloc( strlen( s ) + 1 );
996 if( sscanf( s, "%d:%d:%d%*c%[^\r\n]",
997 &h1, &m1, &s1, psz_text ) == 4 )
999 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1000 (int64_t)m1 * 60*1000 +
1001 (int64_t)s1 * 1000 ) * 1000;
1002 p_subtitle->i_stop = 0;
1008 /* replace | by \n */
1009 for( i = 0; psz_text[i] != '\0'; i++ )
1011 if( psz_text[i] == '|' )
1014 p_subtitle->psz_text = psz_text;
1020 static char *ParseSamiSearch( text_t *txt,
1021 char *psz_start, const char *psz_str )
1023 if( psz_start && strcasestr( psz_start, psz_str ) )
1025 char *s = strcasestr( psz_start, psz_str );
1026 return &s[strlen( psz_str )];
1031 char *p = TextGetLine( txt );
1035 if( strcasestr( p, psz_str ) )
1037 char *s = strcasestr( p, psz_str );
1038 return &s[strlen( psz_str )];
1042 static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1044 demux_sys_t *p_sys = p_demux->p_sys;
1045 text_t *txt = &p_sys->txt;
1050 unsigned int i_text;
1051 char text[8192]; /* Arbitrary but should be long enough */
1053 /* search "Start=" */
1054 if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) )
1055 return VLC_EGENERIC;
1057 /* get start value */
1058 i_start = strtol( s, &s, 0 );
1061 if( !( s = ParseSamiSearch( txt, s, "<P" ) ) )
1062 return VLC_EGENERIC;
1065 if( !( s = ParseSamiSearch( txt, s, ">" ) ) )
1066 return VLC_EGENERIC;
1070 /* now get all txt until a "Start=" line */
1074 /* Search non empty line */
1075 while( s && *s == '\0' )
1076 s = TextGetLine( txt );
1082 if( !strncasecmp( s, "<br", 3 ) )
1086 else if( strcasestr( s, "Start=" ) )
1088 TextPreviousLine( txt );
1091 s = ParseSamiSearch( txt, s, ">" );
1093 else if( !strncmp( s, " ", 6 ) )
1098 else if( *s == '\t' )
1108 if( c != '\0' && i_text+1 < sizeof(text) )
1111 text[i_text] = '\0';
1115 p_subtitle->i_start = i_start * 1000;
1116 p_subtitle->i_stop = 0;
1117 p_subtitle->psz_text = strdup( text );
1129 * TODO it can have a header
1136 * LANG support would be cool
1137 * CODEPAGE is probably mandatory FIXME
1139 static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle,
1142 demux_sys_t *p_sys = p_demux->p_sys;
1143 text_t *txt = &p_sys->txt;
1148 const char *s = TextGetLine( txt );
1152 return VLC_EGENERIC;
1156 &h1, &m1, &s1, &c1 ) == 4 )
1158 p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 +
1159 (int64_t)m1 * 60*1000 +
1160 (int64_t)s1 * 1000 +
1161 (int64_t)c1 * 10) * 1000;
1162 p_subtitle->i_stop = 0;
1167 /* Now read text until a line containing "}" */
1168 psz_text = strdup("");
1173 const char *s = TextGetLine( txt );
1180 return VLC_EGENERIC;
1183 i_len = strlen( s );
1184 if( i_len == 1 && s[0] == '}')
1186 p_subtitle->psz_text = psz_text;
1190 i_old = strlen( psz_text );
1191 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1194 strcat( psz_text, s );
1195 strcat( psz_text, "\n" );
1201 * [n1][n2]Line1|Line2|Line3...
1202 * where n1 and n2 are the video frame number (n2 can be empty)
1204 static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1206 demux_sys_t *p_sys = p_demux->p_sys;
1207 text_t *txt = &p_sys->txt;
1213 const char *s = TextGetLine( txt );
1218 return VLC_EGENERIC;
1220 psz_text = malloc( strlen(s) + 1 );
1226 if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 ||
1227 sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3)
1229 p_subtitle->i_start = (int64_t)i_start * 100000;
1230 p_subtitle->i_stop = (int64_t)i_stop * 100000;
1236 for( i = 0; psz_text[i] != '\0'; )
1238 /* replace | by \n */
1239 if( psz_text[i] == '|' )
1243 if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) )
1244 memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 );
1248 p_subtitle->psz_text = psz_text;
1252 static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1254 demux_sys_t *p_sys = p_demux->p_sys;
1255 text_t *txt = &p_sys->txt;
1256 char *psz_text = strdup( "" );
1258 int i_firstline = 1;
1264 const char *s = TextGetLine( txt );
1267 return VLC_EGENERIC;
1270 if( sscanf (s, "-->> %d", &t) == 1)
1272 p_subtitle->i_start = (int64_t)t; /* * FPS*/
1273 p_subtitle->i_stop = 0;
1275 /* Starting of a subtitle */
1280 /* We have been too far: end of the subtitle, begin of next */
1290 i_old = strlen( psz_text ) + 1;
1291 psz_text = realloc( psz_text, i_old + strlen( s ) + 1 );
1294 strcat( psz_text, s );
1295 strcat( psz_text, "\n" );
1296 if( txt->i_line == txt->i_line_count )
1300 p_subtitle->psz_text = psz_text;
1304 static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1306 demux_sys_t *p_sys = p_demux->p_sys;
1307 text_t *txt = &p_sys->txt;
1312 const char *s = TextGetLine( txt );
1316 return VLC_EGENERIC;
1318 psz_text = malloc( strlen(s) + 1 );
1321 if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 )
1323 /* 1/10th of second ? Frame based ? FIXME */
1324 p_subtitle->i_start = 10 * t1;
1325 p_subtitle->i_stop = 10 * t2;
1326 /* Remove latest " */
1327 psz_text[ strlen(psz_text) - 1 ] = '\0 ';
1333 p_subtitle->psz_text = psz_text;
1334 msg_Dbg( p_demux, "%s", psz_text );
1338 static float mpsub_total = 0;
1340 static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx )
1342 demux_sys_t *p_sys = p_demux->p_sys;
1343 text_t *txt = &p_sys->txt;
1344 char *psz_text = strdup( "" );
1348 const char *s = TextGetLine( txt );
1352 return VLC_EGENERIC;
1355 if( sscanf (s, "%f %f", &f1, &f2 ) == 2 )
1358 p_subtitle->i_start = (int64_t)(1000000.0 * mpsub_total);
1360 p_subtitle->i_stop = (int64_t)(1000000.0 * mpsub_total);
1367 const char *s = TextGetLine( txt );
1370 return VLC_EGENERIC;
1372 int i_len = strlen( s );
1376 int i_old = strlen( psz_text );
1378 psz_text = realloc( psz_text, i_old + i_len + 1 + 1 );
1382 strcat( psz_text, s );
1383 strcat( psz_text, "\n" );
1386 p_subtitle->psz_text = psz_text;