📄 subsdec.c
字号:
block_Release( *pp_block ); *pp_block = NULL; return p_spu;}/***************************************************************************** * CloseDecoder: clean up the decoder *****************************************************************************/static void CloseDecoder( vlc_object_t *p_this ){ decoder_t *p_dec = (decoder_t *)p_this; decoder_sys_t *p_sys = p_dec->p_sys; if( p_sys->iconv_handle != (vlc_iconv_t)-1 ) { vlc_iconv_close( p_sys->iconv_handle ); } if( p_sys->pp_ssa_styles ) { int i; for( i = 0; i < p_sys->i_ssa_styles; i++ ) { if( p_sys->pp_ssa_styles[i]->psz_stylename ) free( p_sys->pp_ssa_styles[i]->psz_stylename ); p_sys->pp_ssa_styles[i]->psz_stylename = NULL; if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname ) free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname ); p_sys->pp_ssa_styles[i]->font_style.psz_fontname = NULL; if( p_sys->pp_ssa_styles[i] ) free( p_sys->pp_ssa_styles[i] ); p_sys->pp_ssa_styles[i] = NULL; } free( p_sys->pp_ssa_styles ); p_sys->pp_ssa_styles = NULL; } free( p_sys );}/***************************************************************************** * ParseText: parse an text subtitle packet and send it to the video output *****************************************************************************/static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block ){ decoder_sys_t *p_sys = p_dec->p_sys; subpicture_t *p_spu = NULL; char *psz_subtitle = NULL; video_format_t fmt; /* We cannot display a subpicture with no date */ if( p_block->i_pts == 0 ) { msg_Warn( p_dec, "subtitle without a date" ); return NULL; } /* Check validity of packet data */ /* An "empty" line containing only \0 can be used to force and ephemer picture from the screen */ if( p_block->i_buffer < 1 ) { msg_Warn( p_dec, "no subtitle data" ); return NULL; } /* Should be resiliant against bad subtitles */ psz_subtitle = strndup( (const char *)p_block->p_buffer, p_block->i_buffer ); if( psz_subtitle == NULL ) return NULL; if( p_sys->iconv_handle == (vlc_iconv_t)-1 ) EnsureUTF8( psz_subtitle ); else { if( p_sys->b_autodetect_utf8 ) { if( IsUTF8( psz_subtitle ) == NULL ) { msg_Dbg( p_dec, "invalid UTF-8 sequence: " "disabling UTF-8 subtitles autodetection" ); p_sys->b_autodetect_utf8 = VLC_FALSE; } } if( !p_sys->b_autodetect_utf8 ) { size_t inbytes_left = strlen( psz_subtitle ); size_t outbytes_left = 6 * inbytes_left; char *psz_new_subtitle = malloc( outbytes_left + 1 ); char *psz_convert_buffer_out = psz_new_subtitle; const char *psz_convert_buffer_in = psz_subtitle; size_t ret = vlc_iconv( p_sys->iconv_handle, &psz_convert_buffer_in, &inbytes_left, &psz_convert_buffer_out, &outbytes_left ); *psz_convert_buffer_out++ = '\0'; free( psz_subtitle ); if( ( ret == (size_t)(-1) ) || inbytes_left ) { free( psz_new_subtitle ); msg_Err( p_dec, _("failed to convert subtitle encoding.\n" "Try manually setting a character-encoding " "before you open the file.") ); return NULL; } psz_subtitle = realloc( psz_new_subtitle, psz_convert_buffer_out - psz_new_subtitle ); } } /* Create the subpicture unit */ p_spu = p_dec->pf_spu_buffer_new( p_dec ); if( !p_spu ) { msg_Warn( p_dec, "can't get spu buffer" ); if( psz_subtitle ) free( psz_subtitle ); return NULL; } p_spu->b_pausable = VLC_TRUE; /* Create a new subpicture region */ memset( &fmt, 0, sizeof(video_format_t) ); fmt.i_chroma = VLC_FOURCC('T','E','X','T'); fmt.i_aspect = 0; fmt.i_width = fmt.i_height = 0; fmt.i_x_offset = fmt.i_y_offset = 0; p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt ); if( !p_spu->p_region ) { msg_Err( p_dec, "cannot allocate SPU region" ); if( psz_subtitle ) free( psz_subtitle ); p_dec->pf_spu_buffer_del( p_dec, p_spu ); return NULL; } /* Decode and format the subpicture unit */ if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') ) { /* Normal text subs, easy markup */ p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align; p_spu->i_x = p_sys->i_align ? 20 : 0; p_spu->i_y = 10; /* Remove formatting from string */ StripTags( psz_subtitle ); p_spu->p_region->psz_text = psz_subtitle; p_spu->i_start = p_block->i_pts; p_spu->i_stop = p_block->i_pts + p_block->i_length; p_spu->b_ephemer = (p_block->i_length == 0); p_spu->b_absolute = VLC_FALSE; } else { /* Decode SSA strings */ ParseSSAString( p_dec, psz_subtitle, p_spu ); p_spu->i_start = p_block->i_pts; p_spu->i_stop = p_block->i_pts + p_block->i_length; p_spu->b_ephemer = (p_block->i_length == 0); p_spu->b_absolute = VLC_FALSE; p_spu->i_original_picture_width = p_sys->i_original_width; p_spu->i_original_picture_height = p_sys->i_original_height; if( psz_subtitle ) free( psz_subtitle ); } return p_spu;}static void ParseSSAString( decoder_t *p_dec, char *psz_subtitle, subpicture_t *p_spu_in ){ /* We expect MKV formatted SSA: * ReadOrder, Layer, Style, CharacterName, MarginL, MarginR, * MarginV, Effect, Text */ decoder_sys_t *p_sys = p_dec->p_sys; subpicture_t *p_spu = p_spu_in; ssa_style_t *p_style = NULL; char *psz_new_subtitle = NULL; char *psz_buffer_sub = NULL; char *psz_style = NULL; char *psz_style_start = NULL; char *psz_style_end = NULL; int i_text = 0, i_comma = 0, i_strlen = 0, i; int i_margin_l = 0, i_margin_r = 0, i_margin_v = 0; psz_buffer_sub = psz_subtitle; i_comma = 0; while( i_comma < 8 && *psz_buffer_sub != '\0' ) { if( *psz_buffer_sub == ',' ) { i_comma++; if( i_comma == 2 ) psz_style_start = &psz_buffer_sub[1]; if( i_comma == 3 ) psz_style_end = &psz_buffer_sub[0]; if( i_comma == 4 ) i_margin_l = (int)strtol( psz_buffer_sub+1, NULL, 10 ); if( i_comma == 5 ) i_margin_r = (int)strtol( psz_buffer_sub+1, NULL, 10 ); if( i_comma == 6 ) i_margin_v = (int)strtol( psz_buffer_sub+1, NULL, 10 ); } psz_buffer_sub++; } if( *psz_buffer_sub == '\0' && i_comma == 8 ) { msg_Dbg( p_dec, "couldn't find all fields in this SSA line" ); return; } psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1); i_text = 0; while( psz_buffer_sub[0] != '\0' ) { if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'n' ) { psz_new_subtitle[i_text] = ' '; i_text++; psz_buffer_sub += 2; } else if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'N' ) { psz_new_subtitle[i_text] = '\n'; i_text++; psz_buffer_sub += 2; } else if( psz_buffer_sub[0] == '{' && psz_buffer_sub[1] == '\\' ) { /* SSA control code */ while( psz_buffer_sub[0] != '\0' && psz_buffer_sub[0] != '}' ) { psz_buffer_sub++; } psz_buffer_sub++; } else { psz_new_subtitle[i_text] = psz_buffer_sub[0]; i_text++; psz_buffer_sub++; } } psz_new_subtitle[i_text] = '\0'; i_strlen = __MAX( psz_style_end - psz_style_start, 0); psz_style = (char *)malloc( i_strlen + 1); psz_style = memcpy( psz_style, psz_style_start, i_strlen ); psz_style[i_strlen] = '\0'; for( i = 0; i < p_sys->i_ssa_styles; i++ ) { if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) ) p_style = p_sys->pp_ssa_styles[i]; } if( psz_style ) free( psz_style ); p_spu->p_region->psz_text = psz_new_subtitle; if( p_style == NULL ) { p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align; p_spu->i_x = p_sys->i_align ? 20 : 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -