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 *****************************************************************************/
34 #include <vlc_common.h>
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\", \"subviewer\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\", " \
61 "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\", "\
62 "\"mpsub\", \"jacosub\", \"psb\", \"realtext\", \"dks\", \"subviewer1\", " \
63 " and \"auto\" (meaning autodetection, this should always work).")
65 static const char *const ppsz_sub_type
[] =
67 "auto", "microdvd", "subrip", "subviewer", "ssa1",
68 "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2",
69 "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks",
74 set_shortname( N_("Subtitles"));
75 set_description( N_("Text subtitles parser") );
76 set_capability( "demux", 0 );
77 set_category( CAT_INPUT
);
78 set_subcategory( SUBCAT_INPUT_DEMUX
);
79 add_float( "sub-fps", 0.0, NULL
,
80 N_("Frames per second"),
81 SUB_FPS_LONGTEXT
, true );
82 add_integer( "sub-delay", 0, NULL
,
83 N_("Subtitles delay"),
84 SUB_DELAY_LONGTEXT
, true );
85 add_string( "sub-type", "auto", NULL
, N_("Subtitles format"),
86 SUB_TYPE_LONGTEXT
, true );
87 change_string_list( ppsz_sub_type
, NULL
, NULL
);
88 set_callbacks( Open
, Close
);
90 add_shortcut( "subtitle" );
93 /*****************************************************************************
95 *****************************************************************************/
98 SUB_TYPE_UNKNOWN
= -1,
106 SUB_TYPE_SUBVIEWER
, /* SUBVIEWER 2 */
107 SUB_TYPE_DVDSUBTITLE
, /* Mplayer calls it subviewer2 */
116 SUB_TYPE_SUBVIEW1
/* SUBVIEWER 1 - mplayer calls it subrip09,
117 and Gnome subtitles SubViewer 1.0 */
127 static int TextLoad( text_t
*, stream_t
*s
);
128 static void TextUnload( text_t
* );
145 int64_t i_next_demux_date
;
146 int64_t i_microsecperframe
;
151 subtitle_t
*subtitle
;
161 int i_time_resolution
;
173 static int ParseMicroDvd ( demux_t
*, subtitle_t
*, int );
174 static int ParseSubRip ( demux_t
*, subtitle_t
*, int );
175 static int ParseSubViewer ( demux_t
*, subtitle_t
*, int );
176 static int ParseSSA ( demux_t
*, subtitle_t
*, int );
177 static int ParseVplayer ( demux_t
*, subtitle_t
*, int );
178 static int ParseSami ( demux_t
*, subtitle_t
*, int );
179 static int ParseDVDSubtitle( demux_t
*, subtitle_t
*, int );
180 static int ParseMPL2 ( demux_t
*, subtitle_t
*, int );
181 static int ParseAQT ( demux_t
*, subtitle_t
*, int );
182 static int ParsePJS ( demux_t
*, subtitle_t
*, int );
183 static int ParseMPSub ( demux_t
*, subtitle_t
*, int );
184 static int ParseJSS ( demux_t
*, subtitle_t
*, int );
185 static int ParsePSB ( demux_t
*, subtitle_t
*, int );
186 static int ParseRealText ( demux_t
*, subtitle_t
*, int );
187 static int ParseDKS ( demux_t
*, subtitle_t
*, int );
188 static int ParseSubViewer1 ( demux_t
*, subtitle_t
*, int );
192 const char *psz_type_name
;
194 const char *psz_name
;
195 int (*pf_read
)( demux_t
*, subtitle_t
*, int );
196 } sub_read_subtitle_function
[] =
198 { "microdvd", SUB_TYPE_MICRODVD
, "MicroDVD", ParseMicroDvd
},
199 { "subrip", SUB_TYPE_SUBRIP
, "SubRIP", ParseSubRip
},
200 { "subviewer", SUB_TYPE_SUBVIEWER
, "SubViewer", ParseSubViewer
},
201 { "ssa1", SUB_TYPE_SSA1
, "SSA-1", ParseSSA
},
202 { "ssa2-4", SUB_TYPE_SSA2_4
, "SSA-2/3/4", ParseSSA
},
203 { "ass", SUB_TYPE_ASS
, "SSA/ASS", ParseSSA
},
204 { "vplayer", SUB_TYPE_VPLAYER
, "VPlayer", ParseVplayer
},
205 { "sami", SUB_TYPE_SAMI
, "SAMI", ParseSami
},
206 { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE
, "DVDSubtitle", ParseDVDSubtitle
},
207 { "mpl2", SUB_TYPE_MPL2
, "MPL2", ParseMPL2
},
208 { "aqt", SUB_TYPE_AQT
, "AQTitle", ParseAQT
},
209 { "pjs", SUB_TYPE_PJS
, "PhoenixSub", ParsePJS
},
210 { "mpsub", SUB_TYPE_MPSUB
, "MPSub", ParseMPSub
},
211 { "jacosub", SUB_TYPE_JACOSUB
, "JacoSub", ParseJSS
},
212 { "psb", SUB_TYPE_PSB
, "PowerDivx", ParsePSB
},
213 { "realtext", SUB_TYPE_RT
, "RealText", ParseRealText
},
214 { "dks", SUB_TYPE_DKS
, "DKS", ParseDKS
},
215 { "subviewer1", SUB_TYPE_SUBVIEW1
, "Subviewer 1", ParseSubViewer1
},
216 { NULL
, SUB_TYPE_UNKNOWN
, "Unknown", NULL
}
218 /* When adding support for more formats, be sure to add their file extension
219 * to src/input/subtitles.c to enable auto-detection.
222 static int Demux( demux_t
* );
223 static int Control( demux_t
*, int, va_list );
225 /*static void Fix( demux_t * );*/
227 /*****************************************************************************
229 *****************************************************************************/
230 static int Open ( vlc_object_t
*p_this
)
232 demux_t
*p_demux
= (demux_t
*)p_this
;
237 int (*pf_read
)( demux_t
*, subtitle_t
*, int );
240 if( !p_demux
->b_force
)
242 msg_Dbg( p_demux
, "subtitle demux discarded" );
246 p_demux
->pf_demux
= Demux
;
247 p_demux
->pf_control
= Control
;
248 p_demux
->p_sys
= p_sys
= malloc( sizeof( demux_sys_t
) );
252 p_sys
->psz_header
= NULL
;
253 p_sys
->i_subtitle
= 0;
254 p_sys
->i_subtitles
= 0;
255 p_sys
->subtitle
= NULL
;
256 p_sys
->i_microsecperframe
= 40000;
258 p_sys
->jss
.b_inited
= false;
259 p_sys
->mpsub
.b_inited
= false;
262 f_fps
= var_CreateGetFloat( p_demux
, "sub-original-fps" ); /* FIXME */
264 p_sys
->i_microsecperframe
= (int64_t)( (float)1000000 / f_fps
);
266 msg_Dbg( p_demux
, "Movie fps: %f", f_fps
);
268 /* Check for override of the fps */
269 f_fps
= var_CreateGetFloat( p_demux
, "sub-fps" );
272 p_sys
->i_microsecperframe
= (int64_t)( (float)1000000 / f_fps
);
273 msg_Dbg( p_demux
, "Override subtitle fps %f", f_fps
);
276 /* Get or probe the type */
277 p_sys
->i_type
= SUB_TYPE_UNKNOWN
;
278 psz_type
= var_CreateGetString( p_demux
, "sub-type" );
279 if( psz_type
&& *psz_type
)
285 if( sub_read_subtitle_function
[i
].psz_type_name
== NULL
)
288 if( !strcmp( sub_read_subtitle_function
[i
].psz_type_name
,
291 p_sys
->i_type
= sub_read_subtitle_function
[i
].i_type
;
298 /* Probe if unknown type */
299 if( p_sys
->i_type
== SUB_TYPE_UNKNOWN
)
304 msg_Dbg( p_demux
, "autodetecting subtitle format" );
305 for( i_try
= 0; i_try
< 256; i_try
++ )
310 if( ( s
= stream_ReadLine( p_demux
->s
) ) == NULL
)
313 if( strcasestr( s
, "<SAMI>" ) )
315 p_sys
->i_type
= SUB_TYPE_SAMI
;
318 else if( sscanf( s
, "{%d}{%d}", &i_dummy
, &i_dummy
) == 2 ||
319 sscanf( s
, "{%d}{}", &i_dummy
) == 1)
321 p_sys
->i_type
= SUB_TYPE_MICRODVD
;
325 "%d:%d:%d,%d --> %d:%d:%d,%d",
326 &i_dummy
,&i_dummy
,&i_dummy
,&i_dummy
,
327 &i_dummy
,&i_dummy
,&i_dummy
,&i_dummy
) == 8 )
329 p_sys
->i_type
= SUB_TYPE_SUBRIP
;
332 else if( !strncasecmp( s
, "!: This is a Sub Station Alpha v1", 33 ) )
334 p_sys
->i_type
= SUB_TYPE_SSA1
;
337 else if( !strncasecmp( s
, "ScriptType: v4.00+", 18 ) )
339 p_sys
->i_type
= SUB_TYPE_ASS
;
342 else if( !strncasecmp( s
, "ScriptType: v4.00", 17 ) )
344 p_sys
->i_type
= SUB_TYPE_SSA2_4
;
347 else if( !strncasecmp( s
, "Dialogue: Marked", 16 ) )
349 p_sys
->i_type
= SUB_TYPE_SSA2_4
;
352 else if( !strncasecmp( s
, "Dialogue:", 9 ) )
354 p_sys
->i_type
= SUB_TYPE_ASS
;
357 else if( strcasestr( s
, "[INFORMATION]" ) )
359 p_sys
->i_type
= SUB_TYPE_SUBVIEWER
; /* I hope this will work */
362 else if( sscanf( s
, "%d:%d:%d.%d %d:%d:%d",
363 &i_dummy
, &i_dummy
, &i_dummy
, &i_dummy
,
364 &i_dummy
, &i_dummy
, &i_dummy
) == 7 ||
365 sscanf( s
, "@%d @%d", &i_dummy
, &i_dummy
) == 2)
367 p_sys
->i_type
= SUB_TYPE_JACOSUB
;
370 else if( sscanf( s
, "%d:%d:%d:", &i_dummy
, &i_dummy
, &i_dummy
) == 3 ||
371 sscanf( s
, "%d:%d:%d ", &i_dummy
, &i_dummy
, &i_dummy
) == 3 )
373 p_sys
->i_type
= SUB_TYPE_VPLAYER
;
376 else if( sscanf( s
, "{T %d:%d:%d:%d", &i_dummy
, &i_dummy
,
377 &i_dummy
, &i_dummy
) == 4 )
379 p_sys
->i_type
= SUB_TYPE_DVDSUBTITLE
;
382 else if( sscanf( s
, "[%d:%d:%d]%c",
383 &i_dummy
, &i_dummy
, &i_dummy
, &p_dummy
) == 4 )
385 p_sys
->i_type
= SUB_TYPE_DKS
;
388 else if( strstr( s
, "*** START SCRIPT" ) )
390 p_sys
->i_type
= SUB_TYPE_SUBVIEW1
;
393 else if( sscanf( s
, "[%d][%d]", &i_dummy
, &i_dummy
) == 2 ||
394 sscanf( s
, "[%d][]", &i_dummy
) == 1)
396 p_sys
->i_type
= SUB_TYPE_MPL2
;
399 else if( sscanf (s
, "FORMAT=%d", &i_dummy
) == 1 ||
400 ( sscanf (s
, "FORMAT=TIM%c", &p_dummy
) == 1
403 p_sys
->i_type
= SUB_TYPE_MPSUB
;
406 else if( sscanf( s
, "-->> %d", &i_dummy
) == 1 )
408 p_sys
->i_type
= SUB_TYPE_AQT
;
411 else if( sscanf( s
, "%d,%d,", &i_dummy
, &i_dummy
) == 2 )
413 p_sys
->i_type
= SUB_TYPE_PJS
;
416 else if( sscanf( s
, "{%d:%d:%d}",
417 &i_dummy
, &i_dummy
, &i_dummy
) == 3 )
419 p_sys
->i_type
= SUB_TYPE_PSB
;
422 else if( strcasestr( s
, "<time" ) )
424 p_sys
->i_type
= SUB_TYPE_RT
;
434 /* It will nearly always work even for non seekable stream thanks the
435 * caching system, and if it fails we lose just a few sub */
436 if( stream_Seek( p_demux
->s
, 0 ) )
438 msg_Warn( p_demux
, "failed to rewind" );
442 /* Quit on unknown subtitles */
443 if( p_sys
->i_type
== SUB_TYPE_UNKNOWN
)
445 msg_Warn( p_demux
, "failed to recognize subtitle type" );
452 if( sub_read_subtitle_function
[i
].i_type
== p_sys
->i_type
)
454 msg_Dbg( p_demux
, "detected %s format",
455 sub_read_subtitle_function
[i
].psz_name
);
456 pf_read
= sub_read_subtitle_function
[i
].pf_read
;
461 msg_Dbg( p_demux
, "loading all subtitles..." );
463 /* Load the whole file */
464 TextLoad( &p_sys
->txt
, p_demux
->s
);
469 if( p_sys
->i_subtitles
>= i_max
)
472 if( !( p_sys
->subtitle
= realloc( p_sys
->subtitle
,
473 sizeof(subtitle_t
) * i_max
) ) )
475 free( p_sys
->subtitle
);
476 TextUnload( &p_sys
->txt
);
482 if( pf_read( p_demux
, &p_sys
->subtitle
[p_sys
->i_subtitles
],
483 p_sys
->i_subtitles
) )
486 p_sys
->i_subtitles
++;
489 TextUnload( &p_sys
->txt
);
491 msg_Dbg(p_demux
, "loaded %d subtitles", p_sys
->i_subtitles
);
493 /* Fix subtitle (order and time) *** */
494 p_sys
->i_subtitle
= 0;
496 if( p_sys
->i_subtitles
> 0 )
498 p_sys
->i_length
= p_sys
->subtitle
[p_sys
->i_subtitles
-1].i_stop
;
500 if( p_sys
->i_length
<= 0 )
501 p_sys
->i_length
= p_sys
->subtitle
[p_sys
->i_subtitles
-1].i_start
+1;
504 /* *** add subtitle ES *** */
505 if( p_sys
->i_type
== SUB_TYPE_SSA1
||
506 p_sys
->i_type
== SUB_TYPE_SSA2_4
||
507 p_sys
->i_type
== SUB_TYPE_ASS
)
509 es_format_Init( &fmt
, SPU_ES
, VLC_FOURCC( 's','s','a',' ' ) );
513 es_format_Init( &fmt
, SPU_ES
, VLC_FOURCC( 's','u','b','t' ) );
515 if( p_sys
->psz_header
!= NULL
)
517 fmt
.i_extra
= strlen( p_sys
->psz_header
) + 1;
518 fmt
.p_extra
= strdup( p_sys
->psz_header
);
520 p_sys
->es
= es_out_Add( p_demux
->out
, &fmt
);
525 /*****************************************************************************
526 * Close: Close subtitle demux
527 *****************************************************************************/
528 static void Close( vlc_object_t
*p_this
)
530 demux_t
*p_demux
= (demux_t
*)p_this
;
531 demux_sys_t
*p_sys
= p_demux
->p_sys
;
534 for( i
= 0; i
< p_sys
->i_subtitles
; i
++ )
535 free( p_sys
->subtitle
[i
].psz_text
);
536 free( p_sys
->subtitle
);
541 /*****************************************************************************
543 *****************************************************************************/
544 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
546 demux_sys_t
*p_sys
= p_demux
->p_sys
;
552 case DEMUX_GET_LENGTH
:
553 pi64
= (int64_t*)va_arg( args
, int64_t * );
554 *pi64
= p_sys
->i_length
;
558 pi64
= (int64_t*)va_arg( args
, int64_t * );
559 if( p_sys
->i_subtitle
< p_sys
->i_subtitles
)
561 *pi64
= p_sys
->subtitle
[p_sys
->i_subtitle
].i_start
;
567 i64
= (int64_t)va_arg( args
, int64_t );
568 p_sys
->i_subtitle
= 0;
569 while( p_sys
->i_subtitle
< p_sys
->i_subtitles
)
571 const subtitle_t
*p_subtitle
= &p_sys
->subtitle
[p_sys
->i_subtitle
];
573 if( p_subtitle
->i_start
> i64
)
575 if( p_subtitle
->i_stop
> p_subtitle
->i_start
&& p_subtitle
->i_stop
> i64
)
581 if( p_sys
->i_subtitle
>= p_sys
->i_subtitles
)
585 case DEMUX_GET_POSITION
:
586 pf
= (double*)va_arg( args
, double * );
587 if( p_sys
->i_subtitle
>= p_sys
->i_subtitles
)
591 else if( p_sys
->i_subtitles
> 0 )
593 *pf
= (double)p_sys
->subtitle
[p_sys
->i_subtitle
].i_start
/
594 (double)p_sys
->i_length
;
602 case DEMUX_SET_POSITION
:
603 f
= (double)va_arg( args
, double );
604 i64
= f
* p_sys
->i_length
;
606 p_sys
->i_subtitle
= 0;
607 while( p_sys
->i_subtitle
< p_sys
->i_subtitles
&&
608 p_sys
->subtitle
[p_sys
->i_subtitle
].i_start
< i64
)
612 if( p_sys
->i_subtitle
>= p_sys
->i_subtitles
)
616 case DEMUX_SET_NEXT_DEMUX_TIME
:
617 p_sys
->i_next_demux_date
= (int64_t)va_arg( args
, int64_t );
622 case DEMUX_GET_ATTACHMENTS
:
623 case DEMUX_GET_TITLE_INFO
:
624 case DEMUX_HAS_UNSUPPORTED_META
:
625 case DEMUX_CAN_RECORD
:
629 msg_Err( p_demux
, "unknown query %d in subtitle control", i_query
);
634 /*****************************************************************************
635 * Demux: Send subtitle to decoder
636 *****************************************************************************/
637 static int Demux( demux_t
*p_demux
)
639 demux_sys_t
*p_sys
= p_demux
->p_sys
;
642 if( p_sys
->i_subtitle
>= p_sys
->i_subtitles
)
645 i_maxdate
= p_sys
->i_next_demux_date
- var_GetTime( p_demux
->p_parent
, "spu-delay" );;
646 if( i_maxdate
<= 0 && p_sys
->i_subtitle
< p_sys
->i_subtitles
)
648 /* Should not happen */
649 i_maxdate
= p_sys
->subtitle
[p_sys
->i_subtitle
].i_start
+ 1;
652 while( p_sys
->i_subtitle
< p_sys
->i_subtitles
&&
653 p_sys
->subtitle
[p_sys
->i_subtitle
].i_start
< i_maxdate
)
655 const subtitle_t
*p_subtitle
= &p_sys
->subtitle
[p_sys
->i_subtitle
];
658 int i_len
= strlen( p_subtitle
->psz_text
) + 1;
660 if( i_len
<= 1 || p_subtitle
->i_start
< 0 )
666 if( ( p_block
= block_New( p_demux
, i_len
) ) == NULL
)
673 p_block
->i_pts
= 1 + p_subtitle
->i_start
;
674 if( p_subtitle
->i_stop
> 0 && p_subtitle
->i_stop
>= p_subtitle
->i_start
)
675 p_block
->i_length
= p_subtitle
->i_stop
- p_subtitle
->i_start
;
677 memcpy( p_block
->p_buffer
, p_subtitle
->psz_text
, i_len
);
679 es_out_Send( p_demux
->out
, p_sys
->es
, p_block
);
685 p_sys
->i_next_demux_date
= 0;
690 /*****************************************************************************
691 * Fix: fix time stamp and order of subtitle
692 *****************************************************************************/
693 #ifdef USE_THIS_UNUSED_PIECE_OF_CODE
694 static void Fix( demux_t
*p_demux
)
696 demux_sys_t
*p_sys
= p_demux
->p_sys
;
700 /* *** fix order (to be sure...) *** */
701 /* We suppose that there are near in order and this durty bubble sort
702 * wont take too much time
707 for( i_index
= 1; i_index
< p_sys
->i_subtitles
; i_index
++ )
709 if( p_sys
->subtitle
[i_index
].i_start
<
710 p_sys
->subtitle
[i_index
- 1].i_start
)
714 p_sys
->subtitle
+ i_index
- 1,
715 sizeof( subtitle_t
) );
716 memcpy( p_sys
->subtitle
+ i_index
- 1,
717 p_sys
->subtitle
+ i_index
,
718 sizeof( subtitle_t
) );
719 memcpy( p_sys
->subtitle
+ i_index
,
721 sizeof( subtitle_t
) );
729 static int TextLoad( text_t
*txt
, stream_t
*s
)
735 txt
->i_line_count
= 0;
737 txt
->line
= calloc( i_line_max
, sizeof( char * ) );
739 /* load the complete file */
742 char *psz
= stream_ReadLine( s
);
747 txt
->line
[txt
->i_line_count
++] = psz
;
748 if( txt
->i_line_count
>= i_line_max
)
751 txt
->line
= realloc( txt
->line
, i_line_max
* sizeof( char * ) );
755 if( txt
->i_line_count
<= 0 )
763 static void TextUnload( text_t
*txt
)
767 for( i
= 0; i
< txt
->i_line_count
; i
++ )
769 free( txt
->line
[i
] );
773 txt
->i_line_count
= 0;
776 static char *TextGetLine( text_t
*txt
)
778 if( txt
->i_line
>= txt
->i_line_count
)
781 return txt
->line
[txt
->i_line
++];
783 static void TextPreviousLine( text_t
*txt
)
785 if( txt
->i_line
> 0 )
789 /*****************************************************************************
790 * Specific Subtitle function
791 *****************************************************************************/
794 * {n1}{n2}Line1|Line2|Line3....
795 * where n1 and n2 are the video frame number (n2 can be empty)
797 static int ParseMicroDvd( demux_t
*p_demux
, subtitle_t
*p_subtitle
,
801 demux_sys_t
*p_sys
= p_demux
->p_sys
;
802 text_t
*txt
= &p_sys
->txt
;
810 const char *s
= TextGetLine( txt
);
814 psz_text
= malloc( strlen(s
) + 1 );
820 if( sscanf( s
, "{%d}{}%[^\r\n]", &i_start
, psz_text
) == 2 ||
821 sscanf( s
, "{%d}{%d}%[^\r\n]", &i_start
, &i_stop
, psz_text
) == 3)
824 if( i_start
!= 1 || i_stop
!= 1 )
827 /* We found a possible setting of the framerate "{1}{1}23.976" */
828 /* Check if it's usable, and if the sub-fps is not set */
829 f_fps
= us_strtod( psz_text
, NULL
);
830 if( f_fps
> 0.0 && var_GetFloat( p_demux
, "sub-fps" ) <= 0.0 )
831 p_sys
->i_microsecperframe
= (int64_t)((float)1000000 / f_fps
);
836 /* replace | by \n */
837 for( i
= 0; psz_text
[i
] != '\0'; i
++ )
839 if( psz_text
[i
] == '|' )
844 p_subtitle
->i_start
= i_start
* p_sys
->i_microsecperframe
;
845 p_subtitle
->i_stop
= i_stop
* p_sys
->i_microsecperframe
;
846 p_subtitle
->psz_text
= psz_text
;
850 /* ParseSubRipSubViewer
853 * h1:m1:s1,d1 --> h2:m2:s2,d2
858 * Format SubViewer v1/v2
859 * h1:m1:s1.d1,h2:m2:s2.d2
864 * We ignore line number for SubRip
866 static int ParseSubRipSubViewer( demux_t
*p_demux
, subtitle_t
*p_subtitle
,
870 demux_sys_t
*p_sys
= p_demux
->p_sys
;
871 text_t
*txt
= &p_sys
->txt
;
876 const char *s
= TextGetLine( txt
);
877 int h1
, m1
, s1
, d1
, h2
, m2
, s2
, d2
;
882 if( sscanf( s
, psz_fmt
,
884 &h2
, &m2
, &s2
, &d2
) == 8 )
886 p_subtitle
->i_start
= ( (int64_t)h1
* 3600*1000 +
887 (int64_t)m1
* 60*1000 +
889 (int64_t)d1
) * 1000;
891 p_subtitle
->i_stop
= ( (int64_t)h2
* 3600*1000 +
892 (int64_t)m2
* 60*1000 +
894 (int64_t)d2
) * 1000;
895 if( p_subtitle
->i_start
< p_subtitle
->i_stop
)
900 /* Now read text until an empty line */
901 psz_text
= strdup("");
907 const char *s
= TextGetLine( txt
);
911 i_len
= s
? strlen( s
) : 0;
914 p_subtitle
->psz_text
= psz_text
;
918 i_old
= strlen( psz_text
);
919 psz_text
= realloc( psz_text
, i_old
+ i_len
+ 1 + 1 );
924 strcat( psz_text
, s
);
925 strcat( psz_text
, "\n" );
927 /* replace [br] by \n */
932 while( ( p
= strstr( psz_text
, "[br]" ) ) )
935 memmove( p
, &p
[3], strlen(&p
[3])+1 );
942 static int ParseSubRip( demux_t
*p_demux
, subtitle_t
*p_subtitle
,
946 return ParseSubRipSubViewer( p_demux
, p_subtitle
,
947 "%d:%d:%d,%d --> %d:%d:%d,%d",
952 static int ParseSubViewer( demux_t
*p_demux
, subtitle_t
*p_subtitle
,
957 return ParseSubRipSubViewer( p_demux
, p_subtitle
,
958 "%d:%d:%d.%d,%d:%d:%d.%d",
964 static int ParseSSA( demux_t
*p_demux
, subtitle_t
*p_subtitle
,
967 demux_sys_t
*p_sys
= p_demux
->p_sys
;
968 text_t
*txt
= &p_sys
->txt
;
972 const char *s
= TextGetLine( txt
);
973 int h1
, m1
, s1
, c1
, h2
, m2
, s2
, c2
;
980 /* We expect (SSA2-4):
981 * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
982 * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
984 * 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.
988 * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
989 * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ?
992 /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */
993 psz_text
= malloc( strlen(s
) );
998 "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]",
1004 /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
1005 /* (Layer comes from ASS specs ... it's empty for SSA.) */
1006 if( p_sys
->i_type
== SUB_TYPE_SSA1
)
1008 /* SSA1 has only 8 commas before the text starts, not 9 */
1009 memmove( &psz_text
[1], psz_text
, strlen(psz_text
)+1 );
1014 int i_layer
= ( p_sys
->i_type
== SUB_TYPE_ASS
) ? atoi( temp
) : 0;
1016 /* ReadOrder, Layer, %s(rest of fields) */
1017 snprintf( temp
, sizeof(temp
), "%d,%d,", i_idx
, i_layer
);
1018 memmove( psz_text
+ strlen(temp
), psz_text
, strlen(psz_text
)+1 );
1019 memcpy( psz_text
, temp
, strlen(temp
) );
1022 p_subtitle
->i_start
= ( (int64_t)h1
* 3600*1000 +
1023 (int64_t)m1
* 60*1000 +
1024 (int64_t)s1
* 1000 +
1025 (int64_t)c1
* 10 ) * 1000;
1026 p_subtitle
->i_stop
= ( (int64_t)h2
* 3600*1000 +
1027 (int64_t)m2
* 60*1000 +
1028 (int64_t)s2
* 1000 +
1029 (int64_t)c2
* 10 ) * 1000;
1030 p_subtitle
->psz_text
= psz_text
;
1035 /* All the other stuff we add to the header field */
1036 if( !p_sys
->psz_header
)
1037 p_sys
->psz_header
= strdup( "" );
1038 if( !p_sys
->psz_header
)
1042 realloc( p_sys
->psz_header
,
1043 strlen( p_sys
->psz_header
) + strlen( s
) + 2 );
1044 strcat( p_sys
->psz_header
, s
);
1045 strcat( p_sys
->psz_header
, "\n" );
1051 * h:m:s:Line1|Line2|Line3....
1053 * h:m:s Line1|Line2|Line3....
1055 static int ParseVplayer( demux_t
*p_demux
, subtitle_t
*p_subtitle
,
1058 VLC_UNUSED( i_idx
);
1060 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1061 text_t
*txt
= &p_sys
->txt
;
1067 const char *s
= TextGetLine( txt
);
1071 return VLC_EGENERIC
;
1073 psz_text
= malloc( strlen( s
) + 1 );
1077 if( sscanf( s
, "%d:%d:%d%*c%[^\r\n]",
1078 &h1
, &m1
, &s1
, psz_text
) == 4 )
1080 p_subtitle
->i_start
= ( (int64_t)h1
* 3600*1000 +
1081 (int64_t)m1
* 60*1000 +
1082 (int64_t)s1
* 1000 ) * 1000;
1083 p_subtitle
->i_stop
= 0;
1089 /* replace | by \n */
1090 for( i
= 0; psz_text
[i
] != '\0'; i
++ )
1092 if( psz_text
[i
] == '|' )
1095 p_subtitle
->psz_text
= psz_text
;
1101 static char *ParseSamiSearch( text_t
*txt
,
1102 char *psz_start
, const char *psz_str
)
1104 if( psz_start
&& strcasestr( psz_start
, psz_str
) )
1106 char *s
= strcasestr( psz_start
, psz_str
);
1107 return &s
[strlen( psz_str
)];
1112 char *p
= TextGetLine( txt
);
1116 if( strcasestr( p
, psz_str
) )
1118 char *s
= strcasestr( p
, psz_str
);
1119 return &s
[strlen( psz_str
)];
1123 static int ParseSami( demux_t
*p_demux
, subtitle_t
*p_subtitle
, int i_idx
)
1125 VLC_UNUSED( i_idx
);
1126 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1127 text_t
*txt
= &p_sys
->txt
;
1132 unsigned int i_text
;
1133 char text
[8192]; /* Arbitrary but should be long enough */
1135 /* search "Start=" */
1136 if( !( s
= ParseSamiSearch( txt
, NULL
, "Start=" ) ) )
1137 return VLC_EGENERIC
;
1139 /* get start value */
1140 i_start
= strtol( s
, &s
, 0 );
1143 if( !( s
= ParseSamiSearch( txt
, s
, "<P" ) ) )
1144 return VLC_EGENERIC
;
1147 if( !( s
= ParseSamiSearch( txt
, s
, ">" ) ) )
1148 return VLC_EGENERIC
;
1152 /* now get all txt until a "Start=" line */
1156 /* Search non empty line */
1157 while( s
&& *s
== '\0' )
1158 s
= TextGetLine( txt
);
1164 if( !strncasecmp( s
, "<br", 3 ) )
1168 else if( strcasestr( s
, "Start=" ) )
1170 TextPreviousLine( txt
);
1173 s
= ParseSamiSearch( txt
, s
, ">" );
1175 else if( !strncmp( s
, " ", 6 ) )
1180 else if( *s
== '\t' )
1190 if( c
!= '\0' && i_text
+1 < sizeof(text
) )
1193 text
[i_text
] = '\0';
1197 p_subtitle
->i_start
= i_start
* 1000;
1198 p_subtitle
->i_stop
= 0;
1199 p_subtitle
->psz_text
= strdup( text
);
1211 * TODO it can have a header
1218 * LANG support would be cool
1219 * CODEPAGE is probably mandatory FIXME
1221 static int ParseDVDSubtitle( demux_t
*p_demux
, subtitle_t
*p_subtitle
,
1224 VLC_UNUSED( i_idx
);
1226 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1227 text_t
*txt
= &p_sys
->txt
;
1232 const char *s
= TextGetLine( txt
);
1236 return VLC_EGENERIC
;
1240 &h1
, &m1
, &s1
, &c1
) == 4 )
1242 p_subtitle
->i_start
= ( (int64_t)h1
* 3600*1000 +
1243 (int64_t)m1
* 60*1000 +
1244 (int64_t)s1
* 1000 +
1245 (int64_t)c1
* 10) * 1000;
1246 p_subtitle
->i_stop
= 0;
1251 /* Now read text until a line containing "}" */
1252 psz_text
= strdup("");
1257 const char *s
= TextGetLine( txt
);
1264 return VLC_EGENERIC
;
1267 i_len
= strlen( s
);
1268 if( i_len
== 1 && s
[0] == '}')
1270 p_subtitle
->psz_text
= psz_text
;
1274 i_old
= strlen( psz_text
);
1275 psz_text
= realloc( psz_text
, i_old
+ i_len
+ 1 + 1 );
1278 strcat( psz_text
, s
);
1279 strcat( psz_text
, "\n" );
1285 * [n1][n2]Line1|Line2|Line3...
1286 * where n1 and n2 are the video frame number (n2 can be empty)
1288 static int ParseMPL2( 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
;
1299 const char *s
= TextGetLine( txt
);
1304 return VLC_EGENERIC
;
1306 psz_text
= malloc( strlen(s
) + 1 );
1312 if( sscanf( s
, "[%d][] %[^\r\n]", &i_start
, psz_text
) == 2 ||
1313 sscanf( s
, "[%d][%d] %[^\r\n]", &i_start
, &i_stop
, psz_text
) == 3)
1315 p_subtitle
->i_start
= (int64_t)i_start
* 100000;
1316 p_subtitle
->i_stop
= (int64_t)i_stop
* 100000;
1322 for( i
= 0; psz_text
[i
] != '\0'; )
1324 /* replace | by \n */
1325 if( psz_text
[i
] == '|' )
1329 if( psz_text
[i
] == '/' && ( i
== 0 || psz_text
[i
-1] == '\n' ) )
1330 memmove( &psz_text
[i
], &psz_text
[i
+1], strlen(&psz_text
[i
+1])+1 );
1334 p_subtitle
->psz_text
= psz_text
;
1338 static int ParseAQT( demux_t
*p_demux
, subtitle_t
*p_subtitle
, int i_idx
)
1340 VLC_UNUSED( i_idx
);
1342 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1343 text_t
*txt
= &p_sys
->txt
;
1344 char *psz_text
= strdup( "" );
1346 int i_firstline
= 1;
1352 const char *s
= TextGetLine( txt
);
1357 return VLC_EGENERIC
;
1361 if( sscanf (s
, "-->> %d", &t
) == 1)
1363 p_subtitle
->i_start
= (int64_t)t
; /* * FPS*/
1364 p_subtitle
->i_stop
= 0;
1366 /* Starting of a subtitle */
1371 /* We have been too far: end of the subtitle, begin of next */
1374 TextPreviousLine( txt
);
1381 i_old
= strlen( psz_text
) + 1;
1382 psz_text
= realloc( psz_text
, i_old
+ strlen( s
) + 1 );
1385 strcat( psz_text
, s
);
1386 strcat( psz_text
, "\n" );
1387 if( txt
->i_line
== txt
->i_line_count
)
1391 p_subtitle
->psz_text
= psz_text
;
1395 static int ParsePJS( demux_t
*p_demux
, subtitle_t
*p_subtitle
, int i_idx
)
1397 VLC_UNUSED( i_idx
);
1399 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1400 text_t
*txt
= &p_sys
->txt
;
1406 const char *s
= TextGetLine( txt
);
1410 return VLC_EGENERIC
;
1412 psz_text
= malloc( strlen(s
) + 1 );
1417 if( sscanf (s
, "%d,%d,\"%[^\n\r]", &t1
, &t2
, psz_text
) == 3 )
1419 /* 1/10th of second ? Frame based ? FIXME */
1420 p_subtitle
->i_start
= 10 * t1
;
1421 p_subtitle
->i_stop
= 10 * t2
;
1422 /* Remove latest " */
1423 psz_text
[ strlen(psz_text
) - 1 ] = '\0';
1430 /* replace | by \n */
1431 for( i
= 0; psz_text
[i
] != '\0'; i
++ )
1433 if( psz_text
[i
] == '|' )
1437 p_subtitle
->psz_text
= psz_text
;
1438 msg_Dbg( p_demux
, "%s", psz_text
);
1442 static int ParseMPSub( demux_t
*p_demux
, subtitle_t
*p_subtitle
, int i_idx
)
1444 VLC_UNUSED( i_idx
);
1446 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1447 text_t
*txt
= &p_sys
->txt
;
1448 char *psz_text
= strdup( "" );
1450 if( !p_sys
->mpsub
.b_inited
)
1452 p_sys
->mpsub
.f_total
= 0.0;
1453 p_sys
->mpsub
.f_factor
= 0.0;
1455 p_sys
->mpsub
.b_inited
= true;
1464 const char *s
= TextGetLine( txt
);
1468 return VLC_EGENERIC
;
1471 if( strstr( s
, "FORMAT" ) )
1473 if( sscanf (s
, "FORMAT=TIM%c", &p_dummy
) == 1 && p_dummy
== 'E')
1475 p_sys
->mpsub
.f_factor
= 100.0;
1479 psz_temp
= malloc( strlen(s
) );
1486 if( sscanf( s
, "FORMAT=%[^\r\n]", psz_temp
) )
1489 f_fps
= us_strtod( psz_temp
, NULL
);
1490 if( f_fps
> 0.0 && var_GetFloat( p_demux
, "sub-fps" ) <= 0.0 )
1491 var_SetFloat( p_demux
, "sub-fps", f_fps
);
1493 p_sys
->mpsub
.f_factor
= 1.0;
1500 f1
= us_strtod( s
, &psz_temp
);
1503 f2
= us_strtod( psz_temp
, NULL
);
1504 p_sys
->mpsub
.f_total
+= f1
* p_sys
->mpsub
.f_factor
;
1505 p_subtitle
->i_start
= (int64_t)(10000.0 * p_sys
->mpsub
.f_total
);
1506 p_sys
->mpsub
.f_total
+= f2
* p_sys
->mpsub
.f_factor
;
1507 p_subtitle
->i_stop
= (int64_t)(10000.0 * p_sys
->mpsub
.f_total
);
1514 const char *s
= TextGetLine( txt
);
1519 return VLC_EGENERIC
;
1522 int i_len
= strlen( s
);
1526 int i_old
= strlen( psz_text
);
1528 psz_text
= realloc( psz_text
, i_old
+ i_len
+ 1 + 1 );
1532 strcat( psz_text
, s
);
1533 strcat( psz_text
, "\n" );
1536 p_subtitle
->psz_text
= psz_text
;
1540 static int ParseJSS( demux_t
*p_demux
, subtitle_t
*p_subtitle
, int i_idx
)
1542 VLC_UNUSED( i_idx
);
1544 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1545 text_t
*txt
= &p_sys
->txt
;
1546 char *psz_text
, *psz_orig
;
1547 char *psz_text2
, *psz_orig2
;
1548 int h1
, h2
, m1
, m2
, s1
, s2
, f1
, f2
;
1550 if( !p_sys
->jss
.b_inited
)
1552 p_sys
->jss
.i_comment
= 0;
1553 p_sys
->jss
.i_time_resolution
= 30;
1554 p_sys
->jss
.i_time_shift
= 0;
1556 p_sys
->jss
.b_inited
= true;
1559 /* Parse the main lines */
1562 const char *s
= TextGetLine( txt
);
1564 return VLC_EGENERIC
;
1566 psz_orig
= malloc( strlen( s
) + 1 );
1569 psz_text
= psz_orig
;
1571 /* Complete time lines */
1572 if( sscanf( s
, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]",
1573 &h1
, &m1
, &s1
, &f1
, &h2
, &m2
, &s2
, &f2
, psz_text
) == 9 )
1575 p_subtitle
->i_start
= ( (int64_t)( h1
*3600 + m1
* 60 + s1
) +
1576 (int64_t)( ( f1
+ p_sys
->jss
.i_time_shift
) / p_sys
->jss
.i_time_resolution
) )
1578 p_subtitle
->i_stop
= ( (int64_t)( h2
*3600 + m2
* 60 + s2
) +
1579 (int64_t)( ( f2
+ p_sys
->jss
.i_time_shift
) / p_sys
->jss
.i_time_resolution
) )
1583 /* Short time lines */
1584 else if( sscanf( s
, "@%d @%d %[^\n\r]", &f1
, &f2
, psz_text
) == 3 )
1586 p_subtitle
->i_start
= (int64_t)(
1587 ( f1
+ p_sys
->jss
.i_time_shift
) / p_sys
->jss
.i_time_resolution
* 1000000.0 );
1588 p_subtitle
->i_stop
= (int64_t)(
1589 ( f2
+ p_sys
->jss
.i_time_shift
) / p_sys
->jss
.i_time_resolution
* 1000000.0 );
1592 /* General Directive lines */
1593 /* Only TIME and SHIFT are supported so far */
1594 else if( s
[0] == '#' )
1596 int h
= 0, m
=0, sec
= 1, f
= 1;
1600 strcpy( psz_text
, s
);
1602 switch( toupper( psz_text
[1] ) )
1605 shift
= isalpha( psz_text
[2] ) ? 6 : 2 ;
1607 if( sscanf( &psz_text
[shift
], "%d", &h
) )
1609 /* Negative shifting */
1616 if( sscanf( &psz_text
[shift
], "%*d:%d", &m
) )
1618 if( sscanf( &psz_text
[shift
], "%*d:%*d:%d", &sec
) )
1620 sscanf( &psz_text
[shift
], "%*d:%*d:%*d.%d", &f
);
1625 sscanf( &psz_text
[shift
], "%d:%d.%d",
1633 sscanf( &psz_text
[shift
], "%d.%d", &sec
, &f
);
1636 p_sys
->jss
.i_time_shift
= ( ( h
* 3600 + m
* 60 + sec
)
1637 * p_sys
->jss
.i_time_resolution
+ f
) * inv
;
1642 shift
= isalpha( psz_text
[2] ) ? 8 : 2 ;
1644 sscanf( &psz_text
[shift
], "%d", &p_sys
->jss
.i_time_resolution
);
1651 /* Unkown type line, probably a comment */
1658 while( psz_text
[ strlen( psz_text
) - 1 ] == '\\' )
1660 const char *s2
= TextGetLine( txt
);
1665 return VLC_EGENERIC
;
1668 int i_len
= strlen( s2
);
1672 int i_old
= strlen( psz_text
);
1674 psz_text
= realloc( psz_text
, i_old
+ i_len
+ 1 );
1678 psz_orig
= psz_text
;
1679 strcat( psz_text
, s2
);
1682 /* Skip the blanks */
1683 while( *psz_text
== ' ' || *psz_text
== '\t' ) psz_text
++;
1685 /* Parse the directives */
1686 if( isalpha( *psz_text
) || *psz_text
== '[' )
1688 while( *psz_text
!= ' ' )
1691 /* Directives are NOT parsed yet */
1692 /* This has probably a better place in a decoder ? */
1693 /* directive = malloc( strlen( psz_text ) + 1 );
1694 if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/
1697 /* Skip the blanks after directives */
1698 while( *psz_text
== ' ' || *psz_text
== '\t' ) psz_text
++;
1700 /* Clean all the lines from inline comments and other stuffs */
1701 psz_orig2
= calloc( strlen( psz_text
) + 1, 1 );
1702 psz_text2
= psz_orig2
;
1704 for( ; *psz_text
!= '\0' && *psz_text
!= '\n' && *psz_text
!= '\r'; )
1709 p_sys
->jss
.i_comment
++;
1712 if( p_sys
->jss
.i_comment
)
1714 p_sys
->jss
.i_comment
= 0;
1715 if( (*(psz_text
+ 1 ) ) == ' ' ) psz_text
++;
1719 if( !p_sys
->jss
.i_comment
)
1727 if( (*(psz_text
+ 1 ) ) == ' ' || (*(psz_text
+ 1 ) ) == '\t' )
1729 if( !p_sys
->jss
.i_comment
)
1736 if( (*(psz_text
+ 1 ) ) == 'n' )
1743 if( ( toupper(*(psz_text
+ 1 ) ) == 'C' ) ||
1744 ( toupper(*(psz_text
+ 1 ) ) == 'F' ) )
1746 psz_text
++; psz_text
++;
1749 if( (*(psz_text
+ 1 ) ) == 'B' || (*(psz_text
+ 1 ) ) == 'b' ||
1750 (*(psz_text
+ 1 ) ) == 'I' || (*(psz_text
+ 1 ) ) == 'i' ||
1751 (*(psz_text
+ 1 ) ) == 'U' || (*(psz_text
+ 1 ) ) == 'u' ||
1752 (*(psz_text
+ 1 ) ) == 'D' || (*(psz_text
+ 1 ) ) == 'N' )
1757 if( (*(psz_text
+ 1 ) ) == '~' || (*(psz_text
+ 1 ) ) == '{' ||
1758 (*(psz_text
+ 1 ) ) == '\\' )
1760 else if( *(psz_text
+ 1 ) == '\r' || *(psz_text
+ 1 ) == '\n' ||
1761 *(psz_text
+ 1 ) == '\0' )
1767 if( !p_sys
->jss
.i_comment
)
1769 *psz_text2
= *psz_text
;
1776 p_subtitle
->psz_text
= psz_orig2
;
1777 msg_Dbg( p_demux
, "%s", p_subtitle
->psz_text
);
1782 static int ParsePSB( demux_t
*p_demux
, subtitle_t
*p_subtitle
, int i_idx
)
1784 VLC_UNUSED( i_idx
);
1786 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1787 text_t
*txt
= &p_sys
->txt
;
1795 const char *s
= TextGetLine( txt
);
1798 return VLC_EGENERIC
;
1800 psz_text
= malloc( strlen( s
) + 1 );
1804 if( sscanf( s
, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]",
1805 &h1
, &m1
, &s1
, &h2
, &m2
, &s2
, psz_text
) == 7 )
1807 p_subtitle
->i_start
= ( (int64_t)h1
* 3600*1000 +
1808 (int64_t)m1
* 60*1000 +
1809 (int64_t)s1
* 1000 ) * 1000;
1810 p_subtitle
->i_stop
= ( (int64_t)h2
* 3600*1000 +
1811 (int64_t)m2
* 60*1000 +
1812 (int64_t)s2
* 1000 ) * 1000;
1818 /* replace | by \n */
1819 for( i
= 0; psz_text
[i
] != '\0'; i
++ )
1821 if( psz_text
[i
] == '|' )
1824 p_subtitle
->psz_text
= psz_text
;
1828 static int64_t ParseRealTime( char *psz
, int *h
, int *m
, int *s
, int *f
)
1830 if( strlen( psz
) == 0 ) return 0;
1831 if( sscanf( psz
, "%d:%d:%d.%d", h
, m
, s
, f
) == 4 ||
1832 sscanf( psz
, "%d:%d.%d", m
, s
, f
) == 3 ||
1833 sscanf( psz
, "%d.%d", s
, f
) == 2 ||
1834 sscanf( psz
, "%d:%d", m
, s
) == 2 ||
1835 sscanf( psz
, "%d", s
) == 1 )
1837 return (int64_t)((( *h
* 60 + *m
) * 60 ) + *s
) * 1000 * 1000
1838 + (int64_t)*f
* 10 * 1000;
1840 else return VLC_EGENERIC
;
1843 static int ParseRealText( demux_t
*p_demux
, subtitle_t
*p_subtitle
, int i_idx
)
1845 VLC_UNUSED( i_idx
);
1846 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1847 text_t
*txt
= &p_sys
->txt
;
1848 char *psz_text
= NULL
;
1849 char psz_end
[12]= "", psz_begin
[12] = "";
1853 int h1
= 0, m1
= 0, s1
= 0, f1
= 0;
1854 int h2
= 0, m2
= 0, s2
= 0, f2
= 0;
1855 const char *s
= TextGetLine( txt
);
1859 return VLC_EGENERIC
;
1861 psz_text
= malloc( strlen( s
) + 1 );
1865 /* Find the good begining. This removes extra spaces at the beginning
1867 char *psz_temp
= strcasestr( s
, "<time");
1868 if( psz_temp
!= NULL
)
1870 /* Line has begin and end */
1871 if( ( sscanf( psz_temp
,
1872 "<%*[t|T]ime %*[b|B]egin=\"%[^\"]\" %*[e|E]nd=\"%[^\"]%*[^>]%[^\n\r]",
1873 psz_begin
, psz_end
, psz_text
) != 3 ) &&
1874 /* Line has begin and no end */
1876 "<%*[t|T]ime %*[b|B]egin=\"%[^\"]\"%*[^>]%[^\n\r]",
1877 psz_begin
, psz_text
) != 2) )
1878 /* Line is not recognized */
1884 int64_t i_time
= ParseRealTime( psz_begin
, &h1
, &m1
, &s1
, &f1
);
1887 p_subtitle
->i_start
= i_time
;
1890 i_time
= ParseRealTime( psz_end
, &h2
, &m2
, &s2
, &f2
);
1893 p_subtitle
->i_stop
= i_time
;
1899 /* Get the following Lines */
1902 const char *s
= TextGetLine( txt
);
1907 return VLC_EGENERIC
;
1910 int i_len
= strlen( s
);
1911 if( i_len
== 0 ) break;
1913 if( strcasestr( s
, "<time" ) ||
1914 strcasestr( s
, "<clear/") )
1916 TextPreviousLine( txt
);
1920 int i_old
= strlen( psz_text
);
1922 psz_text
= realloc( psz_text
, i_old
+ i_len
+ 1 + 1 );
1926 strcat( psz_text
, s
);
1927 strcat( psz_text
, "\n" );
1930 /* Remove the starting ">" that remained after the sscanf */
1931 memmove( &psz_text
[0], &psz_text
[1], strlen( psz_text
) );
1933 p_subtitle
->psz_text
= psz_text
;
1938 static int ParseDKS( demux_t
*p_demux
, subtitle_t
*p_subtitle
, int i_idx
)
1940 VLC_UNUSED( i_idx
);
1942 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1943 text_t
*txt
= &p_sys
->txt
;
1950 char *s
= TextGetLine( txt
);
1953 return VLC_EGENERIC
;
1955 psz_text
= malloc( strlen( s
) + 1 );
1959 if( sscanf( s
, "[%d:%d:%d]%[^\r\n]",
1960 &h1
, &m1
, &s1
, psz_text
) == 4 )
1962 p_subtitle
->i_start
= ( (int64_t)h1
* 3600*1000 +
1963 (int64_t)m1
* 60*1000 +
1964 (int64_t)s1
* 1000 ) * 1000;
1966 char *s
= TextGetLine( txt
);
1970 return VLC_EGENERIC
;
1973 if( sscanf( s
, "[%d:%d:%d]", &h2
, &m2
, &s2
) == 3 )
1974 p_subtitle
->i_stop
= ( (int64_t)h2
* 3600*1000 +
1975 (int64_t)m2
* 60*1000 +
1976 (int64_t)s2
* 1000 ) * 1000;
1982 /* replace [br] by \n */
1984 while( ( p
= strstr( psz_text
, "[br]" ) ) )
1987 memmove( p
, &p
[3], strlen(&p
[3])+1 );
1990 p_subtitle
->psz_text
= psz_text
;
1994 static int ParseSubViewer1( demux_t
*p_demux
, subtitle_t
*p_subtitle
, int i_idx
)
1996 VLC_UNUSED( i_idx
);
1998 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1999 text_t
*txt
= &p_sys
->txt
;
2006 char *s
= TextGetLine( txt
);
2009 return VLC_EGENERIC
;
2011 if( sscanf( s
, "[%d:%d:%d]", &h1
, &m1
, &s1
) == 3 )
2013 p_subtitle
->i_start
= ( (int64_t)h1
* 3600*1000 +
2014 (int64_t)m1
* 60*1000 +
2015 (int64_t)s1
* 1000 ) * 1000;
2017 char *s
= TextGetLine( txt
);
2019 return VLC_EGENERIC
;
2021 psz_text
= strdup( s
);
2025 s
= TextGetLine( txt
);
2027 return VLC_EGENERIC
;
2029 if( sscanf( s
, "[%d:%d:%d]", &h2
, &m2
, &s2
) == 3 )
2030 p_subtitle
->i_stop
= ( (int64_t)h2
* 3600*1000 +
2031 (int64_t)m2
* 60*1000 +
2032 (int64_t)s2
* 1000 ) * 1000;
2037 p_subtitle
->psz_text
= psz_text
;