X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdemux%2Fsubtitle.c;h=21ea8fa0144253cfa75369ab1f05d698bfd677c9;hb=3845f37d56912d6b82f6374a2d85fef06bb8e5c9;hp=ee80ed24c94617e440418a8159dca445e2ced3ed;hpb=ba9826cacf6f17a741a88e9346a6cd674d7f0e29;p=vlc diff --git a/modules/demux/subtitle.c b/modules/demux/subtitle.c index ee80ed24c9..21ea8fa014 100644 --- a/modules/demux/subtitle.c +++ b/modules/demux/subtitle.c @@ -1,11 +1,12 @@ /***************************************************************************** * subtitle.c: Demux for subtitle text files. ***************************************************************************** - * Copyright (C) 1999-2004 VideoLAN + * Copyright (C) 1999-2007 the VideoLAN team * $Id$ * * Authors: Laurent Aimar * Derk-Jan Hartman + * Jean-Baptiste Kempf * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,13 +20,20 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ /***************************************************************************** * Preamble *****************************************************************************/ -#include + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include #include #ifdef HAVE_SYS_TYPES_H @@ -33,9 +41,8 @@ #endif #include -#include -#include -#include "vlc_video.h" +#include +#include /***************************************************************************** * Module descriptor @@ -44,34 +51,38 @@ static int Open ( vlc_object_t *p_this ); static void Close( vlc_object_t *p_this ); #define SUB_DELAY_LONGTEXT \ - "Delay subtitles (in 1/10s)" + N_("Apply a delay to all subtitles (in 1/10s, eg 100 means 10s).") #define SUB_FPS_LONGTEXT \ - "Override frames per second. " \ - "It will only work with MicroDVD subtitles." + N_("Override the normal frames per second settings. " \ + "This will only work with MicroDVD and SubRIP (SRT) subtitles.") #define SUB_TYPE_LONGTEXT \ - "One from \"microdvd\", \"subrip\", \"ssa1\", \"ssa2-4\", \"vplayer\" " \ - "\"sami\" (auto for autodetection, it should always work)." -static char *ppsz_sub_type[] = + N_("Force the subtiles format. Valid values are : \"microdvd\", " \ + "\"subrip\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\" " \ + "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\" "\ + "\"mpsub\" \"jacosub\" \"psb\" and \"auto\" (meaning autodetection, this " \ + "should always work).") +static const char *ppsz_sub_type[] = { "auto", "microdvd", "subrip", "subviewer", "ssa1", - "ssa2-4", "vplayer", "sami" + "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2", + "aqt", "pjs", "mpsub", "jacosub", "psb" }; vlc_module_begin(); set_shortname( _("Subtitles")); - set_description( _("Text subtitles demux") ); - set_capability( "demux2", 0 ); + set_description( _("Text subtitles parser") ); + set_capability( "demux", 0 ); set_category( CAT_INPUT ); set_subcategory( SUBCAT_INPUT_DEMUX ); add_float( "sub-fps", 0.0, NULL, N_("Frames per second"), - SUB_FPS_LONGTEXT, VLC_TRUE ); + SUB_FPS_LONGTEXT, true ); add_integer( "sub-delay", 0, NULL, N_("Subtitles delay"), - SUB_DELAY_LONGTEXT, VLC_TRUE ); - add_string( "sub-type", "auto", NULL, "Subtitles fileformat", - SUB_TYPE_LONGTEXT, VLC_TRUE ); - change_string_list( ppsz_sub_type, 0, 0 ); + SUB_DELAY_LONGTEXT, true ); + add_string( "sub-type", "auto", NULL, N_("Subtitles format"), + SUB_TYPE_LONGTEXT, true ); + change_string_list( ppsz_sub_type, NULL, NULL ); set_callbacks( Open, Close ); add_shortcut( "subtitle" ); @@ -87,9 +98,20 @@ enum SUB_TYPE_SUBRIP, SUB_TYPE_SSA1, SUB_TYPE_SSA2_4, + SUB_TYPE_ASS, SUB_TYPE_VPLAYER, SUB_TYPE_SAMI, - SUB_TYPE_SUBVIEWER, + SUB_TYPE_SUBVIEWER, //SUBVIEWER 2! + SUB_TYPE_DVDSUBTITLE, + SUB_TYPE_MPL2, + SUB_TYPE_AQT, + SUB_TYPE_PJS, + SUB_TYPE_MPSUB, + SUB_TYPE_JACOSUB, + SUB_TYPE_PSB, + SUB_TYPE_RT, + SUB_TYPE_SUBVIEW1, + SUB_TYPE_DKS }; typedef struct @@ -98,6 +120,7 @@ typedef struct int i_line; char **line; } text_t; + static int TextLoad( text_t *, stream_t *s ); static void TextUnload( text_t * ); @@ -117,9 +140,7 @@ struct demux_sys_t es_out_id_t *es; int64_t i_next_demux_date; - int64_t i_microsecperframe; - int64_t i_original_mspf; char *psz_header; int i_subtitle; @@ -129,50 +150,71 @@ struct demux_sys_t int64_t i_length; }; -static int ParseMicroDvd ( demux_t *, subtitle_t * ); -static int ParseSubRip ( demux_t *, subtitle_t * ); -static int ParseSubViewer( demux_t *, subtitle_t * ); -static int ParseSSA ( demux_t *, subtitle_t * ); -static int ParseVplayer ( demux_t *, subtitle_t * ); -static int ParseSami ( demux_t *, subtitle_t * ); +static int ParseMicroDvd ( demux_t *, subtitle_t *, int ); +static int ParseSubRip ( demux_t *, subtitle_t *, int ); +static int ParseSubViewer ( demux_t *, subtitle_t *, int ); +static int ParseSSA ( demux_t *, subtitle_t *, int ); +static int ParseVplayer ( demux_t *, subtitle_t *, int ); +static int ParseSami ( demux_t *, subtitle_t *, int ); +static int ParseDVDSubtitle( demux_t *, subtitle_t *, int ); +static int ParseMPL2 ( demux_t *, subtitle_t *, int ); +static int ParseAQT ( demux_t *, subtitle_t *, int ); +static int ParsePJS ( demux_t *, subtitle_t *, int ); +static int ParseMPSub ( demux_t *, subtitle_t *, int ); +static int ParseJSS ( demux_t *, subtitle_t *, int ); +static int ParsePSB ( demux_t *, subtitle_t *, int ); +static int ParseRealText ( demux_t *, subtitle_t *, int ); +static int ParseDKS ( demux_t *, subtitle_t *, int ); +static int ParseSub1 ( demux_t *, subtitle_t *, int ); static struct { - char *psz_type_name; + const char *psz_type_name; int i_type; - char *psz_name; - int (*pf_read)( demux_t *, subtitle_t* ); + const char *psz_name; + int (*pf_read)( demux_t *, subtitle_t*, int ); } sub_read_subtitle_function [] = { - { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd }, - { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip }, - { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer",ParseSubViewer }, - { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA }, - { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4",ParseSSA }, - { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer }, - { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami }, - { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL } + { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd }, + { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip }, + { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer }, + { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA }, + { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA }, + { "ass", SUB_TYPE_ASS, "SSA/ASS", ParseSSA }, + { "vplayer", SUB_TYPE_VPLAYER, "VPlayer", ParseVplayer }, + { "sami", SUB_TYPE_SAMI, "SAMI", ParseSami }, + { "dvdsubtitle",SUB_TYPE_DVDSUBTITLE, "DVDSubtitle", ParseDVDSubtitle }, + { "mpl2", SUB_TYPE_MPL2, "MPL2", ParseMPL2 }, + { "aqt", SUB_TYPE_AQT, "AQTitle", ParseAQT }, + { "pjs", SUB_TYPE_PJS, "PhoenixSub", ParsePJS }, + { "mpsub", SUB_TYPE_MPSUB, "MPSub", ParseMPSub }, + { "jacosub", SUB_TYPE_JACOSUB, "JacoSub", ParseJSS }, + { "psb", SUB_TYPE_PSB, "PowerDivx", ParsePSB }, + { "realtext", SUB_TYPE_RT, "RealText", ParseRealText }, + { "dks", SUB_TYPE_DKS, "DKS", ParseDKS }, + { "subviewer1", SUB_TYPE_SUBVIEW1, "Subviewer 1", ParseSub1 }, + { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL } }; static int Demux( demux_t * ); static int Control( demux_t *, int, va_list ); -static void Fix( demux_t * ); +/*static void Fix( demux_t * );*/ /***************************************************************************** * Module initializer *****************************************************************************/ static int Open ( vlc_object_t *p_this ) { - demux_t *p_demux = (demux_t*)p_this; - demux_sys_t *p_sys; - es_format_t fmt; - float f_fps; - char *psz_type; - int (*pf_read)( demux_t *, subtitle_t* ); - int i, i_max; - - if( strcmp( p_demux->psz_demux, "subtitle" ) ) + demux_t *p_demux = (demux_t*)p_this; + demux_sys_t *p_sys; + es_format_t fmt; + float f_fps; + char *psz_type; + int (*pf_read)( demux_t *, subtitle_t*, int ); + int i, i_max; + + if( !p_demux->b_force ) { msg_Dbg( p_demux, "subtitle demux discarded" ); return VLC_EGENERIC; @@ -181,31 +223,28 @@ static int Open ( vlc_object_t *p_this ) p_demux->pf_demux = Demux; p_demux->pf_control = Control; p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) ); - p_sys->psz_header = NULL; - p_sys->i_subtitle = 0; - p_sys->i_subtitles= 0; - p_sys->subtitle = NULL; + if( p_sys == NULL ) + return VLC_ENOMEM; + p_sys->psz_header = NULL; + p_sys->i_subtitle = 0; + p_sys->i_subtitles = 0; + p_sys->subtitle = NULL; + p_sys->i_microsecperframe = 40000; /* Get the FPS */ - f_fps = var_CreateGetFloat( p_demux, "sub-fps" ); + f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); /* FIXME */ if( f_fps >= 1.0 ) - { p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps ); - } - else - { - p_sys->i_microsecperframe = 0; - } - f_fps = var_CreateGetFloat( p_demux, "sub-original-fps" ); + msg_Dbg( p_demux, "Movie fps: %f", f_fps ); + + /* Check for override of the fps */ + f_fps = var_CreateGetFloat( p_demux, "sub-fps" ); if( f_fps >= 1.0 ) { - p_sys->i_original_mspf = (int64_t)( (float)1000000 / f_fps ); - } - else - { - p_sys->i_original_mspf = 0; + p_sys->i_microsecperframe = (int64_t)( (float)1000000 / f_fps ); + msg_Dbg( p_demux, "Override subtitle fps %f", f_fps ); } /* Get or probe the type */ @@ -240,6 +279,7 @@ static int Open ( vlc_object_t *p_this ) for( i_try = 0; i_try < 256; i_try++ ) { int i_dummy; + char p_dummy; if( ( s = stream_ReadLine( p_demux->s ) ) == NULL ) break; @@ -263,28 +303,29 @@ static int Open ( vlc_object_t *p_this ) p_sys->i_type = SUB_TYPE_SUBRIP; break; } - else if( sscanf( s, - "!: This is a Sub Station Alpha v%d.x script.", - &i_dummy ) == 1) + else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) ) + { + p_sys->i_type = SUB_TYPE_SSA1; + break; + } + else if( !strncasecmp( s, "ScriptType: v4.00+", 18 ) ) { - if( i_dummy <= 1 ) - { - p_sys->i_type = SUB_TYPE_SSA1; - } - else - { - p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */ - } + p_sys->i_type = SUB_TYPE_ASS; break; } - else if( strcasestr( s, "This is a Sub Station Alpha v4 script" ) ) + else if( !strncasecmp( s, "ScriptType: v4.00", 17 ) ) { - p_sys->i_type = SUB_TYPE_SSA2_4; /* I hope this will work */ + p_sys->i_type = SUB_TYPE_SSA2_4; break; } else if( !strncasecmp( s, "Dialogue: Marked", 16 ) ) { - p_sys->i_type = SUB_TYPE_SSA2_4; /* could be wrong */ + p_sys->i_type = SUB_TYPE_SSA2_4; + break; + } + else if( !strncasecmp( s, "Dialogue:", 9 ) ) + { + p_sys->i_type = SUB_TYPE_ASS; break; } else if( strcasestr( s, "[INFORMATION]" ) ) @@ -292,29 +333,91 @@ static int Open ( vlc_object_t *p_this ) p_sys->i_type = SUB_TYPE_SUBVIEWER; /* I hope this will work */ break; } + 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 || + sscanf( s, "@%d @%d", &i_dummy, &i_dummy) == 2) + { + p_sys->i_type = SUB_TYPE_JACOSUB; + break; + } else if( sscanf( s, "%d:%d:%d:", &i_dummy, &i_dummy, &i_dummy ) == 3 || sscanf( s, "%d:%d:%d ", &i_dummy, &i_dummy, &i_dummy ) == 3 ) { p_sys->i_type = SUB_TYPE_VPLAYER; break; } + else if( sscanf( s, "{T %d:%d:%d:%d", &i_dummy, &i_dummy, + &i_dummy, &i_dummy ) == 4 ) + { + p_sys->i_type = SUB_TYPE_DVDSUBTITLE; + break; + } + else if( sscanf( s, "[%d:%d:%d]%c", + &i_dummy, &i_dummy, &i_dummy, &p_dummy ) == 4 ) + { + p_sys->i_type = SUB_TYPE_DKS; + break; + } + else if( strstr( s, "*** START SCRIPT" ) ) + { + p_sys->i_type = SUB_TYPE_SUBVIEW1; + break; + } + else if( sscanf( s, "[%d][%d]", &i_dummy, &i_dummy ) == 2 || + sscanf( s, "[%d][]", &i_dummy ) == 1) + { + p_sys->i_type = SUB_TYPE_MPL2; + break; + } + else if( sscanf (s, "FORMAT=%d", &i_dummy) == 1 || + ( sscanf (s, "FORMAT=TIM%c", &p_dummy) == 1 + && p_dummy =='E' ) ) + { + p_sys->i_type = SUB_TYPE_MPSUB; + break; + } + else if( sscanf( s, "-->> %d", &i_dummy) == 1 ) + { + p_sys->i_type = SUB_TYPE_AQT; + break; + } + else if( sscanf( s, "%d,%d,", &i_dummy, &i_dummy ) == 2 ) + { + p_sys->i_type = SUB_TYPE_PJS; + break; + } + else if( sscanf( s, "{%d:%d:%d}", + &i_dummy, &i_dummy, &i_dummy ) == 3 ) + { + p_sys->i_type = SUB_TYPE_PSB; + break; + } + else if( strcasestr( s, "i_type = SUB_TYPE_RT; + break; + } free( s ); s = NULL; } - if( s ) free( s ); + free( s ); /* It will nearly always work even for non seekable stream thanks the - * caching system, and if it fails we loose just a few sub */ + * caching system, and if it fails we lose just a few sub */ if( stream_Seek( p_demux->s, 0 ) ) { msg_Warn( p_demux, "failed to rewind" ); } } + + /* Quit on unknown subtitles */ if( p_sys->i_type == SUB_TYPE_UNKNOWN ) { msg_Err( p_demux, "failed to recognize subtitle type" ); + free( p_sys ); return VLC_EGENERIC; } @@ -344,11 +447,15 @@ static int Open ( vlc_object_t *p_this ) sizeof(subtitle_t) * i_max ) ) ) { msg_Err( p_demux, "out of memory"); + free( p_sys->subtitle ); + TextUnload( &p_sys->txt ); + free( p_sys ); return VLC_ENOMEM; } } - if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles] ) ) + if( pf_read( p_demux, &p_sys->subtitle[p_sys->i_subtitles], + p_sys->i_subtitles ) ) break; p_sys->i_subtitles++; @@ -371,7 +478,8 @@ static int Open ( vlc_object_t *p_this ) /* *** add subtitle ES *** */ if( p_sys->i_type == SUB_TYPE_SSA1 || - p_sys->i_type == SUB_TYPE_SSA2_4 ) + p_sys->i_type == SUB_TYPE_SSA2_4 || + p_sys->i_type == SUB_TYPE_ASS ) { es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','s','a',' ' ) ); } @@ -399,12 +507,8 @@ static void Close( vlc_object_t *p_this ) int i; for( i = 0; i < p_sys->i_subtitles; i++ ) - { - if( p_sys->subtitle[i].psz_text ) - free( p_sys->subtitle[i].psz_text ); - } - if( p_sys->subtitle ) - free( p_sys->subtitle ); + free( p_sys->subtitle[i].psz_text ); + free( p_sys->subtitle ); free( p_sys ); } @@ -484,11 +588,13 @@ static int Control( demux_t *p_demux, int i_query, va_list args ) case DEMUX_GET_FPS: case DEMUX_GET_META: + case DEMUX_GET_ATTACHMENTS: case DEMUX_GET_TITLE_INFO: + case DEMUX_HAS_UNSUPPORTED_META: return VLC_EGENERIC; default: - msg_Err( p_demux, "unknown query in subtitle control" ); + msg_Err( p_demux, "unknown query %d in subtitle control", i_query ); return VLC_EGENERIC; } } @@ -566,10 +672,11 @@ static int Demux( demux_t *p_demux ) /***************************************************************************** * Fix: fix time stamp and order of subtitle *****************************************************************************/ +#ifdef USE_THIS_UNUSED_PIECE_OF_CODE static void Fix( demux_t *p_demux ) { demux_sys_t *p_sys = p_demux->p_sys; - vlc_bool_t b_done; + bool b_done; int i_index; /* *** fix order (to be sure...) *** */ @@ -578,7 +685,7 @@ static void Fix( demux_t *p_demux ) */ do { - b_done = VLC_TRUE; + b_done = true; for( i_index = 1; i_index < p_sys->i_subtitles; i_index++ ) { if( p_sys->subtitle[i_index].i_start < @@ -594,11 +701,12 @@ static void Fix( demux_t *p_demux ) memcpy( p_sys->subtitle + i_index, &sub_xch, sizeof( subtitle_t ) ); - b_done = VLC_FALSE; + b_done = false; } } } while( !b_done ); } +#endif static int TextLoad( text_t *txt, stream_t *s ) { @@ -622,7 +730,7 @@ static int TextLoad( text_t *txt, stream_t *s ) if( txt->i_line_count >= i_line_max ) { i_line_max += 100; - txt->line = realloc( txt->line, i_line_max * sizeof( char*) ); + txt->line = realloc( txt->line, i_line_max * sizeof( char * ) ); } } @@ -663,520 +771,1226 @@ static void TextPreviousLine( text_t *txt ) /***************************************************************************** * Specific Subtitle function *****************************************************************************/ -#define MAX_LINE 8192 -static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle ) +/* ParseMicroDvd: + * Format: + * {n1}{n2}Line1|Line2|Line3.... + * where n1 and n2 are the video frame number (n2 can be empty) + */ +static int ParseMicroDvd( demux_t *p_demux, subtitle_t *p_subtitle, + int i_idx ) { + VLC_UNUSED( i_idx ); demux_sys_t *p_sys = p_demux->p_sys; text_t *txt = &p_sys->txt; - /* - * each line: - * {n1}{n2}Line1|Line2|Line3.... - * where n1 and n2 are the video frame number... - * - */ - char *s; - - char buffer_text[MAX_LINE + 1]; - int i_start; - int i_stop; - unsigned int i; - - int i_microsecperframe = 40000; /* default to 25 fps */ - if( p_sys->i_microsecperframe > 0 ) - i_microsecperframe = p_sys->i_microsecperframe; - - p_subtitle->i_start = 0; - p_subtitle->i_stop = 0; - p_subtitle->psz_text = NULL; + char *psz_text; + int i_start; + int i_stop; + int i; for( ;; ) { - if( ( s = TextGetLine( txt ) ) == NULL ) - { - return( VLC_EGENERIC ); - } + const char *s = TextGetLine( txt ); + if( !s ) + return VLC_EGENERIC; + + psz_text = malloc( strlen(s) + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + i_start = 0; i_stop = 0; - - memset( buffer_text, '\0', MAX_LINE ); - if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, buffer_text ) == 2 || - sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, buffer_text ) == 3) + if( sscanf( s, "{%d}{}%[^\r\n]", &i_start, psz_text ) == 2 || + sscanf( s, "{%d}{%d}%[^\r\n]", &i_start, &i_stop, psz_text ) == 3) { - break; + float f_fps; + if( i_start != 1 || i_stop != 1 ) + break; + + /* We found a possible setting of the framerate "{1}{1}23.976" */ + /* Check if it's usable, and if the sub-fps is not set */ + f_fps = us_strtod( psz_text, NULL ); + if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 ) + p_sys->i_microsecperframe = (int64_t)((float)1000000 / f_fps); } + free( psz_text ); } + /* replace | by \n */ - for( i = 0; i < strlen( buffer_text ); i++ ) + for( i = 0; psz_text[i] != '\0'; i++ ) { - if( buffer_text[i] == '|' ) - { - buffer_text[i] = '\n'; - } + if( psz_text[i] == '|' ) + psz_text[i] = '\n'; } - p_subtitle->i_start = (int64_t)i_start * i_microsecperframe; - p_subtitle->i_stop = (int64_t)i_stop * i_microsecperframe; - p_subtitle->psz_text = strndup( buffer_text, MAX_LINE ); - return( 0 ); + /* */ + p_subtitle->i_start = i_start * p_sys->i_microsecperframe; + p_subtitle->i_stop = i_stop * p_sys->i_microsecperframe; + p_subtitle->psz_text = psz_text; + return VLC_SUCCESS; } -static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle ) +/* ParseSubRipSubViewer + * Format SubRip + * n + * h1:m1:s1,d1 --> h2:m2:s2,d2 + * Line1 + * Line2 + * .... + * [Empty line] + * Format SubViewer v1/v2 + * h1:m1:s1.d1,h2:m2:s2.d2 + * Line1[br]Line2 + * Line3 + * ... + * [empty line] + * We ignore line number for SubRip + */ +static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle, + const char *psz_fmt, + bool b_replace_br ) { demux_sys_t *p_sys = p_demux->p_sys; text_t *txt = &p_sys->txt; - - /* - * n - * h1:m1:s1,d1 --> h2:m2:s2,d2 - * Line1 - * Line2 - * ... - * [empty line] - * - */ - char *s; - char buffer_text[ 10 * MAX_LINE]; - int i_buffer_text; - int64_t i_start; - int64_t i_stop; - - p_subtitle->i_start = 0; - p_subtitle->i_stop = 0; - p_subtitle->psz_text = NULL; + char *psz_text; for( ;; ) { + const char *s = TextGetLine( txt ); int h1, m1, s1, d1, h2, m2, s2, d2; - if( ( s = TextGetLine( txt ) ) == NULL ) - { - return( VLC_EGENERIC ); - } - if( sscanf( s, - "%d:%d:%d,%d --> %d:%d:%d,%d", + + if( !s ) + return VLC_EGENERIC; + + if( sscanf( s, psz_fmt, &h1, &m1, &s1, &d1, &h2, &m2, &s2, &d2 ) == 8 ) { - i_start = ( (int64_t)h1 * 3600*1000 + - (int64_t)m1 * 60*1000 + - (int64_t)s1 * 1000 + - (int64_t)d1 ) * 1000; - - i_stop = ( (int64_t)h2 * 3600*1000 + - (int64_t)m2 * 60*1000 + - (int64_t)s2 * 1000 + - (int64_t)d2 ) * 1000; - - /* Now read text until an empty line */ - for( i_buffer_text = 0;; ) - { - int i_len; - if( ( s = TextGetLine( txt ) ) == NULL ) - { - return( VLC_EGENERIC ); - } - - i_len = strlen( s ); - if( i_len <= 0 ) - { - /* empty line -> end of this subtitle */ - buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0'; - p_subtitle->i_start = i_start; - p_subtitle->i_stop = i_stop; - p_subtitle->psz_text = strdup( buffer_text ); - /* If framerate is available, use sub-fps */ - if( p_sys->i_microsecperframe != 0 && - p_sys->i_original_mspf != 0) - { - p_subtitle->i_start = (int64_t)i_start * - p_sys->i_microsecperframe/ - p_sys->i_original_mspf; - p_subtitle->i_stop = (int64_t)i_stop * - p_sys->i_microsecperframe / - p_sys->i_original_mspf; - } - return 0; - } - else - { - if( i_buffer_text + i_len + 1 < 10 * MAX_LINE ) - { - memcpy( buffer_text + i_buffer_text, - s, - i_len ); - i_buffer_text += i_len; - - buffer_text[i_buffer_text] = '\n'; - i_buffer_text++; - } - } - } + p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 + + (int64_t)m1 * 60*1000 + + (int64_t)s1 * 1000 + + (int64_t)d1 ) * 1000; + + p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 + + (int64_t)m2 * 60*1000 + + (int64_t)s2 * 1000 + + (int64_t)d2 ) * 1000; + break; } } -} - -static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle ) -{ - demux_sys_t *p_sys = p_demux->p_sys; - text_t *txt = &p_sys->txt; - - /* - * h1:m1:s1.d1,h2:m2:s2.d2 - * Line1[br]Line2 - * Line3 - * ... - * [empty line] - * ( works with subviewer and subviewer v2 ) - */ - char *s; - char buffer_text[ 10 * MAX_LINE]; - int i_buffer_text; - int64_t i_start; - int64_t i_stop; - - p_subtitle->i_start = 0; - p_subtitle->i_stop = 0; - p_subtitle->psz_text = NULL; + /* Now read text until an empty line */ + psz_text = strdup(""); + if( !psz_text ) + return VLC_ENOMEM; for( ;; ) { - int h1, m1, s1, d1, h2, m2, s2, d2; - if( ( s = TextGetLine( txt ) ) == NULL ) + const char *s = TextGetLine( txt ); + int i_len; + int i_old; + + if( !s ) { - return( VLC_EGENERIC ); + free( psz_text ); + return VLC_EGENERIC; } - if( sscanf( s, - "%d:%d:%d.%d,%d:%d:%d.%d", - &h1, &m1, &s1, &d1, - &h2, &m2, &s2, &d2 ) == 8 ) + + i_len = strlen( s ); + if( i_len <= 0 ) { - i_start = ( (int64_t)h1 * 3600*1000 + - (int64_t)m1 * 60*1000 + - (int64_t)s1 * 1000 + - (int64_t)d1 ) * 1000; - - i_stop = ( (int64_t)h2 * 3600*1000 + - (int64_t)m2 * 60*1000 + - (int64_t)s2 * 1000 + - (int64_t)d2 ) * 1000; - - /* Now read text until an empty line */ - for( i_buffer_text = 0;; ) + p_subtitle->psz_text = psz_text; + return VLC_SUCCESS; + } + + i_old = strlen( psz_text ); + psz_text = realloc( psz_text, i_old + i_len + 1 + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + strcat( psz_text, s ); + strcat( psz_text, "\n" ); + + /* replace [br] by \n */ + if( b_replace_br ) + { + char *p; + + while( ( p = strstr( psz_text, "[br]" ) ) ) { - int i_len, i; - if( ( s = TextGetLine( txt ) ) == NULL ) - { - return( VLC_EGENERIC ); - } - - i_len = strlen( s ); - if( i_len <= 0 ) - { - /* empty line -> end of this subtitle */ - buffer_text[__MAX( i_buffer_text - 1, 0 )] = '\0'; - p_subtitle->i_start = i_start; - p_subtitle->i_stop = i_stop; - - /* replace [br] by \n */ - for( i = 0; i < i_buffer_text - 3; i++ ) - { - if( buffer_text[i] == '[' && buffer_text[i+1] == 'b' && - buffer_text[i+2] == 'r' && buffer_text[i+3] == ']' ) - { - char *temp = buffer_text + i + 1; - buffer_text[i] = '\n'; - memmove( temp, temp+3, strlen( temp ) -3 ); - temp[strlen( temp )-3] = '\0'; - } - } - p_subtitle->psz_text = strdup( buffer_text ); - return( 0 ); - } - else - { - if( i_buffer_text + i_len + 1 < 10 * MAX_LINE ) - { - memcpy( buffer_text + i_buffer_text, - s, - i_len ); - i_buffer_text += i_len; - - buffer_text[i_buffer_text] = '\n'; - i_buffer_text++; - } - } + *p++ = '\n'; + memmove( p, &p[3], strlen(&p[3])+1 ); } } } } +/* ParseSubRip + */ +static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle, + int i_idx ) +{ + VLC_UNUSED( i_idx ); + return ParseSubRipSubViewer( p_demux, p_subtitle, + "%d:%d:%d,%d --> %d:%d:%d,%d", + false ); +} +/* ParseSubViewer + */ +static int ParseSubViewer( demux_t *p_demux, subtitle_t *p_subtitle, + int i_idx ) +{ + VLC_UNUSED( i_idx ); + return ParseSubRipSubViewer( p_demux, p_subtitle, + "%d:%d:%d.%d,%d:%d:%d.%d", + true ); +} -static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle ) +/* ParseSSA + */ +static int ParseSSA( demux_t *p_demux, subtitle_t *p_subtitle, + int i_idx ) { demux_sys_t *p_sys = p_demux->p_sys; text_t *txt = &p_sys->txt; - char buffer_text[ 10 * MAX_LINE]; - char *s; - int64_t i_start; - int64_t i_stop; - - p_subtitle->i_start = 0; - p_subtitle->i_stop = 0; - p_subtitle->psz_text = NULL; - for( ;; ) { + const char *s = TextGetLine( txt ); int h1, m1, s1, c1, h2, m2, s2, c2; - int i_dummy; + char *psz_text; + char temp[16]; - if( ( s = TextGetLine( txt ) ) == NULL ) - { - return( VLC_EGENERIC ); - } - p_subtitle->psz_text = malloc( strlen( s ) ); + if( !s ) + return VLC_EGENERIC; + + /* We expect (SSA2-4): + * Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text + * Dialogue: Marked=0,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ? + * + * 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. + */ + + /* For ASS: + * Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text + * Dialogue: Layer#,0:02:40.65,0:02:41.79,Wolf main,Cher,0000,0000,0000,,Et les enregistrements de ses ondes delta ? + */ + + /* The output text is - at least, not removing numbers - 18 chars shorter than the input text. */ + psz_text = malloc( strlen(s) ); + if( !psz_text ) + return VLC_ENOMEM; if( sscanf( s, - "Dialogue: Marked=%d,%d:%d:%d.%d,%d:%d:%d.%d%[^\r\n]", - &i_dummy, + "Dialogue: %15[^,],%d:%d:%d.%d,%d:%d:%d.%d,%[^\r\n]", + temp, &h1, &m1, &s1, &c1, &h2, &m2, &s2, &c2, - buffer_text ) == 10 ) + psz_text ) == 10 ) { - i_start = ( (int64_t)h1 * 3600*1000 + - (int64_t)m1 * 60*1000 + - (int64_t)s1 * 1000 + - (int64_t)c1 * 10 ) * 1000; - - i_stop = ( (int64_t)h2 * 3600*1000 + - (int64_t)m2 * 60*1000 + - (int64_t)s2 * 1000 + - (int64_t)c2 * 10 ) * 1000; - /* The dec expects: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */ + /* (Layer comes from ASS specs ... it's empty for SSA.) */ if( p_sys->i_type == SUB_TYPE_SSA1 ) { - sprintf( p_subtitle->psz_text, - ",%d%s", i_dummy, strdup( buffer_text) ); - } - else - { - sprintf( p_subtitle->psz_text, - ",%d,%s", i_dummy, strdup( buffer_text) ); - } - p_subtitle->i_start = i_start; - p_subtitle->i_stop = i_stop; - return 0; - } - else - { - /* All the other stuff we add to the header field */ - if( p_sys->psz_header != NULL ) - { - if( !( p_sys->psz_header = realloc( p_sys->psz_header, - strlen( p_sys->psz_header ) + strlen( s ) + 2 ) ) ) - { - msg_Err( p_demux, "out of memory"); - return VLC_ENOMEM; - } - p_sys->psz_header = strcat( p_sys->psz_header, strdup( s ) ); - p_sys->psz_header = strcat( p_sys->psz_header, "\n" ); + /* SSA1 has only 8 commas before the text starts, not 9 */ + memmove( &psz_text[1], psz_text, strlen(psz_text)+1 ); + psz_text[0] = ','; } else { - if( !( p_sys->psz_header = malloc( strlen( s ) + 2 ) ) ) - { - msg_Err( p_demux, "out of memory"); - return VLC_ENOMEM; - } - p_sys->psz_header = strdup( s ); - p_sys->psz_header = strcat( p_sys->psz_header, "\n" ); + int i_layer = ( p_sys->i_type == SUB_TYPE_ASS ) ? atoi( temp ) : 0; + + /* ReadOrder, Layer, %s(rest of fields) */ + snprintf( temp, sizeof(temp), "%d,%d,", i_idx, i_layer ); + memmove( psz_text + strlen(temp), psz_text, strlen(psz_text)+1 ); + memcpy( psz_text, temp, strlen(temp) ); } + + p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 + + (int64_t)m1 * 60*1000 + + (int64_t)s1 * 1000 + + (int64_t)c1 * 10 ) * 1000; + p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 + + (int64_t)m2 * 60*1000 + + (int64_t)s2 * 1000 + + (int64_t)c2 * 10 ) * 1000; + p_subtitle->psz_text = psz_text; + return VLC_SUCCESS; } + free( psz_text ); + + /* All the other stuff we add to the header field */ + if( !p_sys->psz_header ) + p_sys->psz_header = strdup( "" ); + if( !p_sys->psz_header ) + return VLC_ENOMEM; + + p_sys->psz_header = + realloc( p_sys->psz_header, + strlen( p_sys->psz_header ) + strlen( s ) + 2 ); + strcat( p_sys->psz_header, s ); + strcat( p_sys->psz_header, "\n" ); } } -static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle ) +/* ParseVplayer + * Format + * h:m:s:Line1|Line2|Line3.... + * or + * h:m:s Line1|Line2|Line3.... + */ +static int ParseVplayer( demux_t *p_demux, subtitle_t *p_subtitle, + int i_idx ) { + VLC_UNUSED( i_idx ); + demux_sys_t *p_sys = p_demux->p_sys; text_t *txt = &p_sys->txt; - - /* - * each line: - * h:m:s:Line1|Line2|Line3.... - * or - * h:m:s Line1|Line2|Line3.... - * - */ - char *p; - char buffer_text[MAX_LINE + 1]; - int64_t i_start; - unsigned int i; - - p_subtitle->i_start = 0; - p_subtitle->i_stop = 0; - p_subtitle->psz_text = NULL; + char *psz_text; + int i; for( ;; ) { - int h, m, s; - char c; + const char *s = TextGetLine( txt ); + int h1, m1, s1; - if( ( p = TextGetLine( txt ) ) == NULL ) - { - return( VLC_EGENERIC ); - } + if( !s ) + return VLC_EGENERIC; - i_start = 0; + psz_text = malloc( strlen( s ) + 1 ); + if( !psz_text ) + return VLC_ENOMEM; - memset( buffer_text, '\0', MAX_LINE ); - if( sscanf( p, "%d:%d:%d%[ :]%[^\r\n]", &h, &m, &s, &c, buffer_text ) == 5 ) + if( sscanf( s, "%d:%d:%d%*c%[^\r\n]", + &h1, &m1, &s1, psz_text ) == 4 ) { - i_start = ( (int64_t)h * 3600*1000 + - (int64_t)m * 60*1000 + - (int64_t)s * 1000 ) * 1000; + p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 + + (int64_t)m1 * 60*1000 + + (int64_t)s1 * 1000 ) * 1000; + p_subtitle->i_stop = 0; break; } + free( psz_text ); } /* replace | by \n */ - for( i = 0; i < strlen( buffer_text ); i++ ) + for( i = 0; psz_text[i] != '\0'; i++ ) { - if( buffer_text[i] == '|' ) - { - buffer_text[i] = '\n'; - } + if( psz_text[i] == '|' ) + psz_text[i] = '\n'; } - p_subtitle->i_start = i_start; - - p_subtitle->i_stop = 0; - p_subtitle->psz_text = strndup( buffer_text, MAX_LINE ); - return( 0 ); + p_subtitle->psz_text = psz_text; + return VLC_SUCCESS; } -static char *ParseSamiSearch( text_t *txt, char *psz_start, char *psz_str ) +/* ParseSami + */ +static char *ParseSamiSearch( text_t *txt, + char *psz_start, const char *psz_str ) { - if( psz_start ) + if( psz_start && strcasestr( psz_start, psz_str ) ) { - if( strcasestr( psz_start, psz_str ) ) - { - char *s = strcasestr( psz_start, psz_str ); - - s += strlen( psz_str ); - - return( s ); - } + char *s = strcasestr( psz_start, psz_str ); + return &s[strlen( psz_str )]; } + for( ;; ) { - char *p; - if( ( p = TextGetLine( txt ) ) == NULL ) - { + char *p = TextGetLine( txt ); + if( !p ) return NULL; - } + if( strcasestr( p, psz_str ) ) { char *s = strcasestr( p, psz_str ); - - s += strlen( psz_str ); - - return( s); + return &s[strlen( psz_str )]; } } } - -static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle ) +static int ParseSami( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) { + VLC_UNUSED( i_idx ); demux_sys_t *p_sys = p_demux->p_sys; text_t *txt = &p_sys->txt; - char *p; + char *s; int64_t i_start; - int i_text; - char buffer_text[10*MAX_LINE + 1]; - - p_subtitle->i_start = 0; - p_subtitle->i_stop = 0; - p_subtitle->psz_text = NULL; - -#define ADDC( c ) \ - if( i_text < 10*MAX_LINE ) \ - { \ - buffer_text[i_text++] = c; \ - buffer_text[i_text] = '\0'; \ - } + unsigned int i_text; + char text[8192]; /* Arbitrary but should be long enough */ /* search "Start=" */ - if( !( p = ParseSamiSearch( txt, NULL, "Start=" ) ) ) - { + if( !( s = ParseSamiSearch( txt, NULL, "Start=" ) ) ) return VLC_EGENERIC; - } /* get start value */ - i_start = strtol( p, &p, 0 ); + i_start = strtol( s, &s, 0 ); /* search

*/ - if( !( p = ParseSamiSearch( txt, p, ">" ) ) ) - { + if( !( s = ParseSamiSearch( txt, s, ">" ) ) ) return VLC_EGENERIC; - } i_text = 0; - buffer_text[0] = '\0'; + text[0] = '\0'; /* now get all txt until a "Start=" line */ for( ;; ) { - if( *p ) + char c = '\0'; + /* Search non empty line */ + while( s && *s == '\0' ) + s = TextGetLine( txt ); + if( !s ) + break; + + if( *s == '<' ) { - if( *p == '<' ) - { - if( !strncasecmp( p, "" ); - } - else if( !strncmp( p, " ", 6 ) ) + if( !strncasecmp( s, "" ); + } + else if( !strncmp( s, " ", 6 ) ) + { + c = ' '; + s += 6; + } + else if( *s == '\t' ) + { + c = ' '; + s++; } else { - p = TextGetLine( txt ); + c = *s; + s++; } - - if( p == NULL ) + if( c != '\0' && i_text+1 < sizeof(text) ) { - break; + text[i_text++] = c; + text[i_text] = '\0'; } } p_subtitle->i_start = i_start * 1000; p_subtitle->i_stop = 0; - p_subtitle->psz_text = strndup( buffer_text, 10*MAX_LINE ); + p_subtitle->psz_text = strdup( text ); + + return VLC_SUCCESS; +} + +/* ParseDVDSubtitle + * Format + * {T h1:m1:s1:c1 + * Line1 + * Line2 + * ... + * } + * TODO it can have a header + * { HEAD + * ... + * CODEPAGE=... + * FORMAT=... + * LANG=English + * } + * LANG support would be cool + * CODEPAGE is probably mandatory FIXME + */ +static int ParseDVDSubtitle( demux_t *p_demux, subtitle_t *p_subtitle, + int i_idx ) +{ + VLC_UNUSED( i_idx ); + + demux_sys_t *p_sys = p_demux->p_sys; + text_t *txt = &p_sys->txt; + char *psz_text; + + for( ;; ) + { + const char *s = TextGetLine( txt ); + int h1, m1, s1, c1; + + if( !s ) + return VLC_EGENERIC; + + if( sscanf( s, + "{T %d:%d:%d:%d", + &h1, &m1, &s1, &c1 ) == 4 ) + { + p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 + + (int64_t)m1 * 60*1000 + + (int64_t)s1 * 1000 + + (int64_t)c1 * 10) * 1000; + p_subtitle->i_stop = 0; + break; + } + } + + /* Now read text until a line containing "}" */ + psz_text = strdup(""); + if( !psz_text ) + return VLC_ENOMEM; + for( ;; ) + { + const char *s = TextGetLine( txt ); + int i_len; + int i_old; + + if( !s ) + { + free( psz_text ); + return VLC_EGENERIC; + } - return( VLC_SUCCESS ); -#undef ADDC + i_len = strlen( s ); + if( i_len == 1 && s[0] == '}') + { + p_subtitle->psz_text = psz_text; + return VLC_SUCCESS; + } + + i_old = strlen( psz_text ); + psz_text = realloc( psz_text, i_old + i_len + 1 + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + strcat( psz_text, s ); + strcat( psz_text, "\n" ); + } +} + +/* ParseMPL2 + * Format + * [n1][n2]Line1|Line2|Line3... + * where n1 and n2 are the video frame number (n2 can be empty) + */ +static int ParseMPL2( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) +{ + VLC_UNUSED( i_idx ); + + demux_sys_t *p_sys = p_demux->p_sys; + text_t *txt = &p_sys->txt; + char *psz_text; + int i; + + for( ;; ) + { + const char *s = TextGetLine( txt ); + int i_start; + int i_stop; + + if( !s ) + return VLC_EGENERIC; + + psz_text = malloc( strlen(s) + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + + i_start = 0; + i_stop = 0; + if( sscanf( s, "[%d][] %[^\r\n]", &i_start, psz_text ) == 2 || + sscanf( s, "[%d][%d] %[^\r\n]", &i_start, &i_stop, psz_text ) == 3) + { + p_subtitle->i_start = (int64_t)i_start * 100000; + p_subtitle->i_stop = (int64_t)i_stop * 100000; + break; + } + free( psz_text ); + } + + for( i = 0; psz_text[i] != '\0'; ) + { + /* replace | by \n */ + if( psz_text[i] == '|' ) + psz_text[i] = '\n'; + + /* Remove italic */ + if( psz_text[i] == '/' && ( i == 0 || psz_text[i-1] == '\n' ) ) + memmove( &psz_text[i], &psz_text[i+1], strlen(&psz_text[i+1])+1 ); + else + i++; + } + p_subtitle->psz_text = psz_text; + return VLC_SUCCESS; } + +static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) +{ + VLC_UNUSED( i_idx ); + + demux_sys_t *p_sys = p_demux->p_sys; + text_t *txt = &p_sys->txt; + char *psz_text = strdup( "" ); + int i_old = 0; + int i_firstline = 1; + + for( ;; ) + { + int t; /* Time */ + + const char *s = TextGetLine( txt ); + + if( !s ) + return VLC_EGENERIC; + + /* Data Lines */ + if( sscanf (s, "-->> %d", &t) == 1) + { + p_subtitle->i_start = (int64_t)t; /* * FPS*/ + p_subtitle->i_stop = 0; + + /* Starting of a subtitle */ + if( i_firstline ) + { + i_firstline = 0; + } + /* We have been too far: end of the subtitle, begin of next */ + else + { + TextPreviousLine( txt ); + break; + } + } + /* Text Lines */ + else + { + i_old = strlen( psz_text ) + 1; + psz_text = realloc( psz_text, i_old + strlen( s ) + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + strcat( psz_text, s ); + strcat( psz_text, "\n" ); + if( txt->i_line == txt->i_line_count ) + break; + } + } + p_subtitle->psz_text = psz_text; + return VLC_SUCCESS; +} + +static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) +{ + VLC_UNUSED( i_idx ); + + demux_sys_t *p_sys = p_demux->p_sys; + text_t *txt = &p_sys->txt; + char *psz_text; + int i; + + for( ;; ) + { + const char *s = TextGetLine( txt ); + int t1, t2; + + if( !s ) + return VLC_EGENERIC; + + psz_text = malloc( strlen(s) + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + + /* Data Lines */ + if( sscanf (s, "%d,%d,\"%[^\n\r]", &t1, &t2, psz_text ) == 3 ) + { + /* 1/10th of second ? Frame based ? FIXME */ + p_subtitle->i_start = 10 * t1; + p_subtitle->i_stop = 10 * t2; + /* Remove latest " */ + psz_text[ strlen(psz_text) - 1 ] = '\0'; + + break; + } + free( psz_text ); + } + + /* replace | by \n */ + for( i = 0; psz_text[i] != '\0'; i++ ) + { + if( psz_text[i] == '|' ) + psz_text[i] = '\n'; + } + + p_subtitle->psz_text = psz_text; + msg_Dbg( p_demux, "%s", psz_text ); + return VLC_SUCCESS; +} + +static float mpsub_total = 0.0; +static float mpsub_factor = 0.0; + +static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) +{ + VLC_UNUSED( i_idx ); + + demux_sys_t *p_sys = p_demux->p_sys; + text_t *txt = &p_sys->txt; + char *psz_text = strdup( "" ); + + for( ;; ) + { + float f1, f2; + char p_dummy; + char *psz_temp; + + const char *s = TextGetLine( txt ); + if( !s ) + return VLC_EGENERIC; + + if( strstr( s, "FORMAT" ) ) + { + if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E') + { + mpsub_factor = 100.0; + break; + } + + psz_temp = malloc( strlen(s) ); + if( !psz_temp ) + return VLC_ENOMEM; + + if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) ) + { + float f_fps; + f_fps = us_strtod( psz_temp, NULL ); + if( f_fps > 0.0 && var_GetFloat( p_demux, "sub-fps" ) <= 0.0 ) + var_SetFloat( p_demux, "sub-fps", f_fps ); + + mpsub_factor = 1.0; + free( psz_temp ); + break; + } + free( psz_temp ); + } + /* Data Lines */ + if( sscanf (s, "%f %f", &f1, &f2 ) == 2 ) + { + mpsub_total += f1 * mpsub_factor; + p_subtitle->i_start = (int64_t)(10000.0 * mpsub_total); + mpsub_total += f2 * mpsub_factor; + p_subtitle->i_stop = (int64_t)(10000.0 * mpsub_total); + break; + } + } + + for( ;; ) + { + const char *s = TextGetLine( txt ); + + if( !s ) + return VLC_EGENERIC; + + int i_len = strlen( s ); + if( i_len == 0 ) + break; + + int i_old = strlen( psz_text ); + + psz_text = realloc( psz_text, i_old + i_len + 1 + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + + strcat( psz_text, s ); + strcat( psz_text, "\n" ); + } + + p_subtitle->psz_text = psz_text; + return VLC_SUCCESS; +} + +static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) +{ + VLC_UNUSED( i_idx ); + + demux_sys_t *p_sys = p_demux->p_sys; + text_t *txt = &p_sys->txt; + char *psz_text, *psz_orig; + char *psz_text2, *psz_orig2; + int h1, h2, m1, m2, s1, s2, f1, f2; + + static int i_comment = 0; + + static int jss_time_resolution = 30; + static int jss_time_shift = 0; + + /* Parse the main lines */ + for( ;; ) + { + const char *s = TextGetLine( txt ); + if( !s ) + return VLC_EGENERIC; + + psz_orig = malloc( strlen( s ) + 1 ); + if( !psz_orig ) + return VLC_ENOMEM; + psz_text = psz_orig; + + /* Complete time lines */ + if( sscanf( s, "%d:%d:%d.%d %d:%d:%d.%d %[^\n\r]", + &h1, &m1, &s1, &f1, &h2, &m2, &s2, &f2, psz_text ) == 9 ) + { + p_subtitle->i_start = ( (int64_t)( h1 *3600 + m1 * 60 + s1 ) + + (int64_t)( ( f1 + jss_time_shift ) / jss_time_resolution ) ) + * 1000000; + p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) + + (int64_t)( ( f2 + jss_time_shift ) / jss_time_resolution ) ) + * 1000000; + break; + } + /* Short time lines */ + else if( sscanf( s, "@%d @%d %[^\n\r]", &f1, &f2, psz_text ) == 3 ) + { + p_subtitle->i_start = (int64_t)( + ( f1 + jss_time_shift ) / jss_time_resolution * 1000000.0 ); + p_subtitle->i_stop = (int64_t)( + ( f2 + jss_time_shift ) / jss_time_resolution * 1000000.0 ); + break; + } + /* General Directive lines */ + /* Only TIME and SHIFT are supported so far */ + else if( s[0] == '#' ) + { + int h = 0, m =0, sec = 1, f = 1; + unsigned shift = 1; + int inv = 1; + + strcpy( psz_text, s ); + + switch( toupper( psz_text[1] ) ) + { + case 'S': + shift = isalpha( psz_text[2] ) ? 6 : 2 ; + + if( sscanf( &psz_text[shift], "%d", &h ) ) + { + /* Negative shifting */ + if( h < 0 ) + { + h *= -1; + inv = -1; + } + + if( sscanf( &psz_text[shift], "%*d:%d", &m ) ) + { + if( sscanf( &psz_text[shift], "%*d:%*d:%d", &sec ) ) + { + sscanf( &psz_text[shift], "%*d:%*d:%*d.%d", &f ); + } + else + { + h = 0; + sscanf( &psz_text[shift], "%d:%d.%d", + &m, &sec, &f ); + m *= inv; + } + } + else + { + h = m = 0; + sscanf( &psz_text[shift], "%d.%d", &sec, &f); + sec *= inv; + } + jss_time_shift = ( ( h * 3600 + m * 60 + sec ) + * jss_time_resolution + f ) * inv; + } + break; + + case 'T': + shift = isalpha( psz_text[2] ) ? 8 : 2 ; + + sscanf( &psz_text[shift], "%d", &jss_time_resolution ); + break; + } + free( psz_orig ); + continue; + } + else + /* Unkown type line, probably a comment */ + { + free( psz_orig ); + continue; + } + } + + while( psz_text[ strlen( psz_text ) - 1 ] == '\\' ) + { + const char *s2 = TextGetLine( txt ); + + if( !s2 ) + return VLC_EGENERIC; + + int i_len = strlen( s2 ); + if( i_len == 0 ) + break; + + int i_old = strlen( psz_text ); + + psz_text = realloc( psz_text, i_old + i_len + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + + psz_orig = psz_text; + strcat( psz_text, s2 ); + } + + /* Skip the blanks */ + while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++; + + /* Parse the directives */ + if( isalpha( *psz_text ) || *psz_text == '[' ) + { + while( *psz_text != ' ' ) + { psz_text++ ;}; + + /* Directives are NOT parsed yet */ + /* This has probably a better place in a decoder ? */ + /* directive = malloc( strlen( psz_text ) + 1 ); + if( sscanf( psz_text, "%s %[^\n\r]", directive, psz_text2 ) == 2 )*/ + } + + /* Skip the blanks after directives */ + while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++; + + /* Clean all the lines from inline comments and other stuffs */ + psz_orig2 = calloc( strlen( psz_text) + 1, 1 ); + psz_text2 = psz_orig2; + + for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; ) + { + switch( *psz_text ) + { + case '{': + i_comment++; + break; + case '}': + if( i_comment ) + { + i_comment = 0; + if( (*(psz_text + 1 ) ) == ' ' ) psz_text++; + } + break; + case '~': + if( !i_comment ) + { + *psz_text2 = ' '; + psz_text2++; + } + break; + case ' ': + case '\t': + if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' ) + break; + if( !i_comment ) + { + *psz_text2 = ' '; + psz_text2++; + } + break; + case '\\': + if( (*(psz_text + 1 ) ) == 'n' ) + { + *psz_text2 = '\n'; + psz_text++; + psz_text2++; + break; + } + if( ( toupper(*(psz_text + 1 ) ) == 'C' ) || + ( toupper(*(psz_text + 1 ) ) == 'F' ) ) + { + psz_text++; psz_text++; + break; + } + if( (*(psz_text + 1 ) ) == 'B' || (*(psz_text + 1 ) ) == 'b' || + (*(psz_text + 1 ) ) == 'I' || (*(psz_text + 1 ) ) == 'i' || + (*(psz_text + 1 ) ) == 'U' || (*(psz_text + 1 ) ) == 'u' || + (*(psz_text + 1 ) ) == 'D' || (*(psz_text + 1 ) ) == 'N' ) + { + psz_text++; + break; + } + if( (*(psz_text + 1 ) ) == '~' || (*(psz_text + 1 ) ) == '{' || + (*(psz_text + 1 ) ) == '\\' ) + psz_text++; + else if( *(psz_text + 1 ) == '\r' || *(psz_text + 1 ) == '\n' || + *(psz_text + 1 ) == '\0' ) + { + psz_text++; + } + break; + default: + if( !i_comment ) + { + *psz_text2 = *psz_text; + psz_text2++; + } + } + psz_text++; + } + + p_subtitle->psz_text = psz_orig2; + msg_Dbg( p_demux, "%s", p_subtitle->psz_text ); + free( psz_orig ); + return VLC_SUCCESS; +} + +static int ParsePSB( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) +{ + VLC_UNUSED( i_idx ); + + demux_sys_t *p_sys = p_demux->p_sys; + text_t *txt = &p_sys->txt; + char *psz_text; + int i; + + for( ;; ) + { + int h1, m1, s1; + int h2, m2, s2; + const char *s = TextGetLine( txt ); + + if( !s ) + return VLC_EGENERIC; + + psz_text = malloc( strlen( s ) + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + + if( sscanf( s, "{%d:%d:%d}{%d:%d:%d}%[^\r\n]", + &h1, &m1, &s1, &h2, &m2, &s2, psz_text ) == 7 ) + { + p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 + + (int64_t)m1 * 60*1000 + + (int64_t)s1 * 1000 ) * 1000; + p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 + + (int64_t)m2 * 60*1000 + + (int64_t)s2 * 1000 ) * 1000; + break; + } + free( psz_text ); + } + + /* replace | by \n */ + for( i = 0; psz_text[i] != '\0'; i++ ) + { + if( psz_text[i] == '|' ) + psz_text[i] = '\n'; + } + p_subtitle->psz_text = psz_text; + return VLC_SUCCESS; +} + +static int64_t ParseRealTime( char *psz, int *h, int *m, int *s, int *f ) +{ + if( strlen( psz ) == 0 ) return 0; + if( sscanf( psz, "%d:%d:%d.%d", h, m, s, f ) == 4 || + sscanf( psz, "%d:%d.%d", m, s, f ) == 3 || + sscanf( psz, "%d.%d", s, f ) == 2 || + sscanf( psz, "%d:%d", m, s ) == 2 || + sscanf( psz, "%d", s ) == 1 ) + { + return (int64_t)((( *h * 60 + *m ) * 60 ) + *s ) * 1000 * 1000 + + (int64_t)*f * 10 * 1000; + } + else return VLC_EGENERIC; +} + +static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) +{ + VLC_UNUSED( i_idx ); + demux_sys_t *p_sys = p_demux->p_sys; + text_t *txt = &p_sys->txt; + char *psz_text; + char psz_end[12]= "", psz_begin[12] = ""; + + for( ;; ) + { + int h1 = 0, m1 = 0, s1 = 0, f1 = 0; + int h2 = 0, m2 = 0, s2 = 0, f2 = 0; + const char *s = TextGetLine( txt ); + + if( !s ) + return VLC_EGENERIC; + + psz_text = malloc( strlen( s ) + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + + /* Find the good begining. This removes extra spaces at the beginning + of the line.*/ + char *psz_temp = strcasestr( s, "]%[^\n\r]", + psz_begin, psz_end, psz_text) != 3 ) && + /* Line has begin and no end */ + ( sscanf( psz_temp, + "<%*[t|T]ime %*[b|B]egin=\"%[^\"]\"%*[^>]%[^\n\r]", + psz_begin, psz_text ) != 2) ) + /* Line is not recognized */ + { + free( psz_text ); + continue; + } + + /* Get the times */ + int64_t i_time = ParseRealTime( psz_begin, &h1, &m1, &s1, &f1 ); + if( i_time >= 0) + { + p_subtitle->i_start = i_time; + } + + i_time = ParseRealTime( psz_end, &h2, &m2, &s2, &f2 ); + if( i_time >= 0 ) + { + p_subtitle->i_stop = i_time; + } + break; + } + /* Line is not recognized */ + else continue; + free( psz_text ); + } + + /* Get the following Lines */ + for( ;; ) + { + const char *s = TextGetLine( txt ); + + if( !s ) + return VLC_EGENERIC; + + int i_len = strlen( s ); + if( i_len == 0 ) break; + + if( strcasestr( s, "" that remained after the sscanf */ + memmove( &psz_text[0], &psz_text[1], strlen( psz_text ) ); + + p_subtitle->psz_text = psz_text; + + return VLC_SUCCESS; +} + +static int ParseDKS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) +{ + VLC_UNUSED( i_idx ); + + demux_sys_t *p_sys = p_demux->p_sys; + text_t *txt = &p_sys->txt; + char *psz_text; + + for( ;; ) + { + int h1, m1, s1; + int h2, m2, s2; + char *s = TextGetLine( txt ); + + if( !s ) + return VLC_EGENERIC; + + psz_text = malloc( strlen( s ) + 1 ); + if( !psz_text ) + return VLC_ENOMEM; + + if( sscanf( s, "[%d:%d:%d]%[^\r\n]", + &h1, &m1, &s1, psz_text ) == 4 ) + { + p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 + + (int64_t)m1 * 60*1000 + + (int64_t)s1 * 1000 ) * 1000; + + char *s = TextGetLine( txt ); + if( !s ) + return VLC_EGENERIC; + + if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 ) + p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 + + (int64_t)m2 * 60*1000 + + (int64_t)s2 * 1000 ) * 1000; + break; + } + free( psz_text ); + } + + /* replace [br] by \n */ + char *p; + while( ( p = strstr( psz_text, "[br]" ) ) ) + { + *p++ = '\n'; + memmove( p, &p[3], strlen(&p[3])+1 ); + } + + p_subtitle->psz_text = psz_text; + return VLC_SUCCESS; +} + +static int ParseSub1( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) +{ + VLC_UNUSED( i_idx ); + + demux_sys_t *p_sys = p_demux->p_sys; + text_t *txt = &p_sys->txt; + char *psz_text; + + for( ;; ) + { + int h1, m1, s1; + int h2, m2, s2; + char *s = TextGetLine( txt ); + + if( !s ) + return VLC_EGENERIC; + + if( sscanf( s, "[%d:%d:%d]", &h1, &m1, &s1 ) == 3 ) + { + p_subtitle->i_start = ( (int64_t)h1 * 3600*1000 + + (int64_t)m1 * 60*1000 + + (int64_t)s1 * 1000 ) * 1000; + + char *s = TextGetLine( txt ); + if( !s ) + return VLC_EGENERIC; + + psz_text = strdup( s ); + if( !psz_text ) + return VLC_ENOMEM; + + s = TextGetLine( txt ); + if( !s ) + return VLC_EGENERIC; + + if( sscanf( s, "[%d:%d:%d]", &h2, &m2, &s2 ) == 3 ) + p_subtitle->i_stop = ( (int64_t)h2 * 3600*1000 + + (int64_t)m2 * 60*1000 + + (int64_t)s2 * 1000 ) * 1000; + break; + } + } + + p_subtitle->psz_text = psz_text; + + return VLC_SUCCESS; +} +