X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdemux%2Fsubtitle.c;h=4faaa9cdcc473b7f4fdfc113fc0ff102b5cca1de;hb=152b1687c0ec112f3ab1360006d048d6b9cf7258;hp=5428b0bca3d9ee8778fedca58f7cf05360cc4a24;hpb=39aae2a3c9d265085b4a7bfde5bc1fb630860713;p=vlc diff --git a/modules/demux/subtitle.c b/modules/demux/subtitle.c index 5428b0bca3..4faaa9cdcc 100644 --- a/modules/demux/subtitle.c +++ b/modules/demux/subtitle.c @@ -31,7 +31,7 @@ # include "config.h" #endif -#include +#include #include #include @@ -57,36 +57,38 @@ static void Close( vlc_object_t *p_this ); "This will only work with MicroDVD and SubRIP (SRT) subtitles.") #define SUB_TYPE_LONGTEXT \ 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[] = + "\"subrip\", \"subviewer\", \"ssa1\", \"ssa2-4\", \"ass\", \"vplayer\", " \ + "\"sami\", \"dvdsubtitle\", \"mpl2\", \"aqt\", \"pjs\", "\ + "\"mpsub\", \"jacosub\", \"psb\", \"realtext\", \"dks\", \"subviewer1\", " \ + " and \"auto\" (meaning autodetection, this should always work).") + +static const char *const ppsz_sub_type[] = { "auto", "microdvd", "subrip", "subviewer", "ssa1", "ssa2-4", "ass", "vplayer", "sami", "dvdsubtitle", "mpl2", - "aqt", "pjs", "mpsub", "jacosub", "psb" + "aqt", "pjs", "mpsub", "jacosub", "psb", "realtext", "dks", + "subviewer1" }; -vlc_module_begin(); - set_shortname( _("Subtitles")); - set_description( _("Text subtitles parser") ); - set_capability( "demux", 0 ); - set_category( CAT_INPUT ); - set_subcategory( SUBCAT_INPUT_DEMUX ); +vlc_module_begin () + set_shortname( N_("Subtitles")) + set_description( N_("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, true ); + SUB_FPS_LONGTEXT, true ) add_integer( "sub-delay", 0, NULL, N_("Subtitles delay"), - SUB_DELAY_LONGTEXT, true ); + 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 ); + SUB_TYPE_LONGTEXT, true ) + change_string_list( ppsz_sub_type, NULL, NULL ) + set_callbacks( Open, Close ) - add_shortcut( "subtitle" ); -vlc_module_end(); + add_shortcut( "subtitle" ) +vlc_module_end () /***************************************************************************** * Prototypes: @@ -96,20 +98,24 @@ enum SUB_TYPE_UNKNOWN = -1, SUB_TYPE_MICRODVD, SUB_TYPE_SUBRIP, + SUB_TYPE_SUBRIP_DOT, /* Invalid SubRip file (dot instead of comma) */ SUB_TYPE_SSA1, SUB_TYPE_SSA2_4, SUB_TYPE_ASS, SUB_TYPE_VPLAYER, SUB_TYPE_SAMI, - SUB_TYPE_SUBVIEWER, //SUBVIEWER 2! - SUB_TYPE_DVDSUBTITLE, + SUB_TYPE_SUBVIEWER, /* SUBVIEWER 2 */ + SUB_TYPE_DVDSUBTITLE, /* Mplayer calls it subviewer2 */ SUB_TYPE_MPL2, SUB_TYPE_AQT, SUB_TYPE_PJS, SUB_TYPE_MPSUB, SUB_TYPE_JACOSUB, SUB_TYPE_PSB, - SUB_TYPE_RT + SUB_TYPE_RT, + SUB_TYPE_DKS, + SUB_TYPE_SUBVIEW1 /* SUBVIEWER 1 - mplayer calls it subrip09, + and Gnome subtitles SubViewer 1.0 */ }; typedef struct @@ -146,10 +152,28 @@ struct demux_sys_t subtitle_t *subtitle; int64_t i_length; + + /* */ + struct + { + bool b_inited; + + int i_comment; + int i_time_resolution; + int i_time_shift; + } jss; + struct + { + bool b_inited; + + float f_total; + float f_factor; + } mpsub; }; static int ParseMicroDvd ( demux_t *, subtitle_t *, int ); static int ParseSubRip ( demux_t *, subtitle_t *, int ); +static int ParseSubRipDot ( 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 ); @@ -162,8 +186,10 @@ 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 ParseSubViewer1 ( demux_t *, subtitle_t *, int ); -static struct +static const struct { const char *psz_type_name; int i_type; @@ -173,6 +199,7 @@ static struct { { "microdvd", SUB_TYPE_MICRODVD, "MicroDVD", ParseMicroDvd }, { "subrip", SUB_TYPE_SUBRIP, "SubRIP", ParseSubRip }, + { "subrip-dot", SUB_TYPE_SUBRIP_DOT, "SubRIP(Dot)", ParseSubRipDot }, { "subviewer", SUB_TYPE_SUBVIEWER, "SubViewer", ParseSubViewer }, { "ssa1", SUB_TYPE_SSA1, "SSA-1", ParseSSA }, { "ssa2-4", SUB_TYPE_SSA2_4, "SSA-2/3/4", ParseSSA }, @@ -187,14 +214,13 @@ static struct { "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", ParseSubViewer1 }, { NULL, SUB_TYPE_UNKNOWN, "Unknown", NULL } }; - -/* Missing Detect - SubViewer 1 - Subrip09 - */ - +/* When adding support for more formats, be sure to add their file extension + * to src/input/subtitles.c to enable auto-detection. + */ static int Demux( demux_t * ); static int Control( demux_t *, int, va_list ); @@ -232,8 +258,11 @@ static int Open ( vlc_object_t *p_this ) p_sys->subtitle = NULL; p_sys->i_microsecperframe = 40000; + p_sys->jss.b_inited = false; + p_sys->mpsub.b_inited = false; + /* Get the FPS */ - f_fps = var_CreateGetFloat( p_demux, "sub-original-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 ); @@ -250,7 +279,7 @@ static int Open ( vlc_object_t *p_this ) /* Get or probe the type */ p_sys->i_type = SUB_TYPE_UNKNOWN; psz_type = var_CreateGetString( p_demux, "sub-type" ); - if( *psz_type ) + if( psz_type && *psz_type ) { int i; @@ -303,6 +332,15 @@ static int Open ( vlc_object_t *p_this ) p_sys->i_type = SUB_TYPE_SUBRIP; break; } + else if( sscanf( s, + "%d:%d:%d.%d --> %d:%d:%d.%d", + &i_dummy,&i_dummy,&i_dummy,&i_dummy, + &i_dummy,&i_dummy,&i_dummy,&i_dummy ) == 8 ) + { + msg_Err( p_demux, "Detected invalid SubRip file, playing anyway" ); + p_sys->i_type = SUB_TYPE_SUBRIP_DOT; + break; + } else if( !strncasecmp( s, "!: This is a Sub Station Alpha v1", 33 ) ) { p_sys->i_type = SUB_TYPE_SSA1; @@ -333,10 +371,13 @@ 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) + 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 ) @@ -350,6 +391,17 @@ static int Open ( vlc_object_t *p_this ) 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) { @@ -361,23 +413,28 @@ static int Open ( vlc_object_t *p_this ) && 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 ); @@ -397,7 +454,7 @@ static int Open ( vlc_object_t *p_this ) /* Quit on unknown subtitles */ if( p_sys->i_type == SUB_TYPE_UNKNOWN ) { - msg_Err( p_demux, "failed to recognize subtitle type" ); + msg_Warn( p_demux, "failed to recognize subtitle type" ); free( p_sys ); return VLC_EGENERIC; } @@ -427,7 +484,6 @@ static int Open ( vlc_object_t *p_this ) if( !( p_sys->subtitle = realloc( p_sys->subtitle, sizeof(subtitle_t) * i_max ) ) ) { - msg_Err( p_demux, "out of memory"); free( p_sys->subtitle ); TextUnload( &p_sys->txt ); free( p_sys ); @@ -462,11 +518,11 @@ static int Open ( vlc_object_t *p_this ) 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',' ' ) ); + es_format_Init( &fmt, SPU_ES, VLC_CODEC_SSA ); } else { - es_format_Init( &fmt, SPU_ES, VLC_FOURCC( 's','u','b','t' ) ); + es_format_Init( &fmt, SPU_ES, VLC_CODEC_SUBT ); } if( p_sys->psz_header != NULL ) { @@ -522,9 +578,15 @@ static int Control( demux_t *p_demux, int i_query, va_list args ) case DEMUX_SET_TIME: i64 = (int64_t)va_arg( args, int64_t ); p_sys->i_subtitle = 0; - while( p_sys->i_subtitle < p_sys->i_subtitles && - p_sys->subtitle[p_sys->i_subtitle].i_start < i64 ) + while( p_sys->i_subtitle < p_sys->i_subtitles ) { + const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle]; + + if( p_subtitle->i_start > i64 ) + break; + if( p_subtitle->i_stop > p_subtitle->i_start && p_subtitle->i_stop > i64 ) + break; + p_sys->i_subtitle++; } @@ -572,6 +634,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args ) case DEMUX_GET_ATTACHMENTS: case DEMUX_GET_TITLE_INFO: case DEMUX_HAS_UNSUPPORTED_META: + case DEMUX_CAN_RECORD: return VLC_EGENERIC; default: @@ -601,12 +664,13 @@ static int Demux( demux_t *p_demux ) while( p_sys->i_subtitle < p_sys->i_subtitles && p_sys->subtitle[p_sys->i_subtitle].i_start < i_maxdate ) { + const subtitle_t *p_subtitle = &p_sys->subtitle[p_sys->i_subtitle]; + block_t *p_block; - int i_len = strlen( p_sys->subtitle[p_sys->i_subtitle].psz_text ) + 1; + int i_len = strlen( p_subtitle->psz_text ) + 1; - if( i_len <= 1 ) + if( i_len <= 1 || p_subtitle->i_start < 0 ) { - /* empty subtitle */ p_sys->i_subtitle++; continue; } @@ -617,30 +681,15 @@ static int Demux( demux_t *p_demux ) continue; } - if( p_sys->subtitle[p_sys->i_subtitle].i_start < 0 ) - { - p_sys->i_subtitle++; - continue; - } + p_block->i_dts = + p_block->i_pts = 1 + p_subtitle->i_start; + if( p_subtitle->i_stop > 0 && p_subtitle->i_stop >= p_subtitle->i_start ) + p_block->i_length = p_subtitle->i_stop - p_subtitle->i_start; - p_block->i_pts = p_sys->subtitle[p_sys->i_subtitle].i_start; - p_block->i_dts = p_block->i_pts; - if( p_sys->subtitle[p_sys->i_subtitle].i_stop > 0 ) - { - p_block->i_length = - p_sys->subtitle[p_sys->i_subtitle].i_stop - p_block->i_pts; - } + memcpy( p_block->p_buffer, p_subtitle->psz_text, i_len ); + + es_out_Send( p_demux->out, p_sys->es, p_block ); - memcpy( p_block->p_buffer, - p_sys->subtitle[p_sys->i_subtitle].psz_text, i_len ); - if( p_block->i_pts > 0 ) - { - es_out_Send( p_demux->out, p_sys->es, p_block ); - } - else - { - block_Release( p_block ); - } p_sys->i_subtitle++; } @@ -855,7 +904,8 @@ static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle, (int64_t)m2 * 60*1000 + (int64_t)s2 * 1000 + (int64_t)d2 ) * 1000; - break; + if( p_subtitle->i_start < p_subtitle->i_stop ) + break; } } @@ -863,19 +913,14 @@ static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle, 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; - } - - i_len = strlen( s ); + i_len = s ? strlen( s ) : 0; if( i_len <= 0 ) { p_subtitle->psz_text = psz_text; @@ -885,7 +930,9 @@ static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle, 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" ); @@ -893,7 +940,7 @@ static int ParseSubRipSubViewer( demux_t *p_demux, subtitle_t *p_subtitle, if( b_replace_br ) { char *p; - + while( ( p = strstr( psz_text, "[br]" ) ) ) { *p++ = '\n'; @@ -912,6 +959,17 @@ static int ParseSubRip( demux_t *p_demux, subtitle_t *p_subtitle, "%d:%d:%d,%d --> %d:%d:%d,%d", false ); } +/* ParseSubRipDot + * Special version for buggy file using '.' instead of ',' + */ +static int ParseSubRipDot( 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, @@ -1317,7 +1375,10 @@ static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) const char *s = TextGetLine( txt ); if( !s ) + { + free( psz_text ); return VLC_EGENERIC; + } /* Data Lines */ if( sscanf (s, "-->> %d", &t) == 1) @@ -1333,7 +1394,7 @@ static int ParseAQT( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) /* We have been too far: end of the subtitle, begin of next */ else { - txt->i_line--; + TextPreviousLine( txt ); break; } } @@ -1401,9 +1462,6 @@ static int ParsePJS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) 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 ); @@ -1412,6 +1470,14 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) text_t *txt = &p_sys->txt; char *psz_text = strdup( "" ); + if( !p_sys->mpsub.b_inited ) + { + p_sys->mpsub.f_total = 0.0; + p_sys->mpsub.f_factor = 0.0; + + p_sys->mpsub.b_inited = true; + } + for( ;; ) { float f1, f2; @@ -1420,19 +1486,25 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) const char *s = TextGetLine( txt ); if( !s ) + { + free( psz_text ); return VLC_EGENERIC; + } if( strstr( s, "FORMAT" ) ) { if( sscanf (s, "FORMAT=TIM%c", &p_dummy ) == 1 && p_dummy == 'E') { - mpsub_factor = 100.0; + p_sys->mpsub.f_factor = 100.0; break; } psz_temp = malloc( strlen(s) ); if( !psz_temp ) + { + free( psz_text ); return VLC_ENOMEM; + } if( sscanf( s, "FORMAT=%[^\r\n]", psz_temp ) ) { @@ -1441,19 +1513,21 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) 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; + p_sys->mpsub.f_factor = 1.0; free( psz_temp ); break; } free( psz_temp ); } /* Data Lines */ - if( sscanf (s, "%f %f", &f1, &f2 ) == 2 ) + f1 = us_strtod( s, &psz_temp ); + if( *psz_temp ) { - 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); + f2 = us_strtod( psz_temp, NULL ); + p_sys->mpsub.f_total += f1 * p_sys->mpsub.f_factor; + p_subtitle->i_start = (int64_t)(10000.0 * p_sys->mpsub.f_total); + p_sys->mpsub.f_total += f2 * p_sys->mpsub.f_factor; + p_subtitle->i_stop = (int64_t)(10000.0 * p_sys->mpsub.f_total); break; } } @@ -1463,7 +1537,10 @@ static int ParseMPSub( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) const char *s = TextGetLine( txt ); if( !s ) + { + free( psz_text ); return VLC_EGENERIC; + } int i_len = strlen( s ); if( i_len == 0 ) @@ -1492,10 +1569,15 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) 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; + if( !p_sys->jss.b_inited ) + { + p_sys->jss.i_comment = 0; + p_sys->jss.i_time_resolution = 30; + p_sys->jss.i_time_shift = 0; + + p_sys->jss.b_inited = true; + } /* Parse the main lines */ for( ;; ) @@ -1504,29 +1586,31 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) if( !s ) return VLC_EGENERIC; - psz_text = malloc( strlen( s ) + 1 ); - if( !psz_text ) + psz_orig = malloc( strlen( s ) + 1 ); + if( !psz_orig ) return VLC_ENOMEM; - psz_orig = psz_text; + 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 ) ) + (int64_t)( ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution ) ) * 1000000; p_subtitle->i_stop = ( (int64_t)( h2 *3600 + m2 * 60 + s2 ) + - (int64_t)( ( f2 + jss_time_shift ) / jss_time_resolution ) ) + (int64_t)( ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_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 ); + ( f1 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 ); p_subtitle->i_stop = (int64_t)( - ( f2 + jss_time_shift ) / jss_time_resolution * 1000000.0 ); + ( f2 + p_sys->jss.i_time_shift ) / p_sys->jss.i_time_resolution * 1000000.0 ); + break; } /* General Directive lines */ /* Only TIME and SHIFT are supported so far */ @@ -1572,134 +1656,150 @@ static int ParseJSS( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) sscanf( &psz_text[shift], "%d.%d", &sec, &f); sec *= inv; } - jss_time_shift = ( ( h * 3600 + m * 60 + sec ) - * jss_time_resolution + f ) * inv; + p_sys->jss.i_time_shift = ( ( h * 3600 + m * 60 + sec ) + * p_sys->jss.i_time_resolution + f ) * inv; } break; case 'T': shift = isalpha( psz_text[2] ) ? 8 : 2 ; - sscanf( &psz_text[shift], "%d", &jss_time_resolution ); + sscanf( &psz_text[shift], "%d", &p_sys->jss.i_time_resolution ); break; } - free( psz_text ); + free( psz_orig ); continue; } else /* Unkown type line, probably a comment */ { - free( psz_text ); + free( psz_orig ); continue; } + } + + while( psz_text[ strlen( psz_text ) - 1 ] == '\\' ) + { + const char *s2 = TextGetLine( txt ); - /* Skip the blanks */ - while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++; - - /* Parse the directives */ - if( isalpha( *psz_text ) || *psz_text == '[' ) + if( !s2 ) { - 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 )*/ + free( psz_orig ); + return VLC_EGENERIC; } - /* Skip the blanks after directives */ - while( *psz_text == ' ' || *psz_text == '\t' ) psz_text++; + int i_len = strlen( s2 ); + if( i_len == 0 ) + break; + int i_old = strlen( psz_text ); - /* Clean all the lines from inline comments and other stuffs */ - psz_text2 = calloc( strlen( psz_text) + 1, 1 ); - psz_orig2 = psz_text2; + 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++ ;}; - for( ; *psz_text != '\0' && *psz_text != '\n' && *psz_text != '\r'; ) + /* 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 ) { - switch( *psz_text ) + case '{': + p_sys->jss.i_comment++; + break; + case '}': + if( p_sys->jss.i_comment ) + { + p_sys->jss.i_comment = 0; + if( (*(psz_text + 1 ) ) == ' ' ) psz_text++; + } + break; + case '~': + if( !p_sys->jss.i_comment ) { - case '{': - i_comment++; + *psz_text2 = ' '; + psz_text2++; + } + break; + case ' ': + case '\t': + if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' ) break; - case '}': - if( i_comment ) - { - i_comment = 0; - if( (*(psz_text + 1 ) ) == ' ' ) psz_text++; - } + if( !p_sys->jss.i_comment ) + { + *psz_text2 = ' '; + psz_text2++; + } + break; + case '\\': + if( (*(psz_text + 1 ) ) == 'n' ) + { + *psz_text2 = '\n'; + psz_text++; + psz_text2++; break; - case '~': - if( !i_comment ) - { - *psz_text2 = ' '; - psz_text2++; - } + } + if( ( toupper(*(psz_text + 1 ) ) == 'C' ) || + ( toupper(*(psz_text + 1 ) ) == 'F' ) ) + { + psz_text++; psz_text++; break; - case ' ': - case '\t': - if( (*(psz_text + 1 ) ) == ' ' || (*(psz_text + 1 ) ) == '\t' ) - break; - if( !i_comment ) - { - *psz_text2 = ' '; - psz_text2++; - } + } + 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; - 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' ) - { - char *s2 = TextGetLine( txt ); - if( !s2 ) - return VLC_EGENERIC; - - while ( *s2 == ' ' ) s2++; - - /* Here to parse the second line, we should add s2 to - psz_text and go on the for( ) line 1556 in order to - parse the next line. - */ - } - default: - if( !i_comment ) - { - *psz_text2 = *psz_text; - psz_text2++; - } } - psz_text++; + 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( !p_sys->jss.i_comment ) + { + *psz_text2 = *psz_text; + psz_text2++; + } } - - p_subtitle->psz_text = psz_orig2; - free( psz_orig ); - return VLC_SUCCESS; + 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 ) @@ -1768,14 +1868,14 @@ 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] = ""; + char *psz_text = NULL; for( ;; ) { int h1 = 0, m1 = 0, s1 = 0, f1 = 0; int h2 = 0, m2 = 0, s2 = 0, f2 = 0; const char *s = TextGetLine( txt ); + free( psz_text ); if( !s ) return VLC_EGENERIC; @@ -1789,21 +1889,20 @@ static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) char *psz_temp = strcasestr( s, "]%[^\n\r]", + "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\" %*[e|E]nd=\"%11[^\"]%*[^>]%[^\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]", + "<%*[t|T]ime %*[b|B]egin=\"%11[^\"]\"%*[^>]%[^\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) @@ -1818,9 +1917,6 @@ static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) } break; } - /* Line is not recognized */ - else continue; - free( psz_text ); } /* Get the following Lines */ @@ -1829,7 +1925,10 @@ static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) const char *s = TextGetLine( txt ); if( !s ) + { + free( psz_text ); return VLC_EGENERIC; + } int i_len = strlen( s ); if( i_len == 0 ) break; @@ -1837,7 +1936,7 @@ static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) if( strcasestr( s, "i_line--; + TextPreviousLine( txt ); break; } @@ -1859,3 +1958,110 @@ static int ParseRealText( demux_t *p_demux, subtitle_t *p_subtitle, int i_idx ) 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 ) + { + free( psz_text ); + 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 ParseSubViewer1( 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 ) + { + free( psz_text ); + 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; +} +