📄 subsdec.c
字号:
/***************************************************************************** * subsdec.c : text subtitles decoder ***************************************************************************** * Copyright (C) 2000-2006 the VideoLAN team * $Id: subsdec.c 17770 2006-11-14 20:22:25Z hartman $ * * Authors: Gildas Bazin <gbazin@videolan.org> * Samuel Hocevar <sam@zoy.org> * Derk-Jan Hartman <hartman at videolan dot org> * * 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 * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#include <vlc/vlc.h>#include <vlc/vout.h>#include <vlc/decoder.h>#include "vlc_osd.h"#include "vlc_filter.h"#include "charset.h"typedef struct{ char * psz_stylename; /* The name of the style, no comma's allowed */ text_style_t font_style; int i_align; int i_margin_h; int i_margin_v;} ssa_style_t;/***************************************************************************** * decoder_sys_t : decoder descriptor *****************************************************************************/struct decoder_sys_t{ vlc_bool_t b_ass; /* The subs are ASS */ int i_original_height; int i_original_width; int i_align; /* Subtitles alignment on the vout */ vlc_iconv_t iconv_handle; /* handle to iconv instance */ vlc_bool_t b_autodetect_utf8; ssa_style_t **pp_ssa_styles; int i_ssa_styles;};/***************************************************************************** * Local prototypes *****************************************************************************/static int OpenDecoder ( vlc_object_t * );static void CloseDecoder ( vlc_object_t * );static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );static subpicture_t *ParseText ( decoder_t *, block_t * );static void ParseSSAHeader ( decoder_t * );static void ParseSSAString ( decoder_t *, char *, subpicture_t * );static void ParseColor ( decoder_t *, char *, int *, int * );static void StripTags ( char * );#define DEFAULT_NAME "Default"#define MAX_LINE 8192/***************************************************************************** * Module descriptor. *****************************************************************************/static char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "", "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "", "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "", "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "", "ISO-8859-6", "CP1256", "MacArabic", "", "ISO-8859-7", "CP1253", "MacGreek", "", "ISO-8859-8", "CP1255", "MacHebrew", "", "ISO-8859-9", "CP1254", "MacTurkish", "", "ISO-8859-13", "CP1257", "", "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "", "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "", "ISO-2022-KR", "EUC-KR", "", "MacThai", "KOI8-T", "", "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "", "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "", "Macintosh", "", "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE", "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "", "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8", "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN", "HPROMAN8", "NEXTSTEP" };/*SSA supports charset selection.The following known charsets are used:0 = Ansi - Western European1 = default2 = symbol3 = invalid77 = Mac128 = Japanese (Shift JIS)129 = Hangul130 = Johab134 = GB2312 Simplified Chinese136 = Big5 Traditional Chinese161 = Greek162 = Turkish163 = Vietnamese177 = Hebrew178 = Arabic186 = Baltic204 = Russian (Cyrillic)222 = Thai238 = Eastern European254 = PC 437*/static int pi_justification[] = { 0, 1, 2 };static char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};#define ENCODING_TEXT N_("Subtitles text encoding")#define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")#define ALIGN_TEXT N_("Subtitles justification")#define ALIGN_LONGTEXT N_("Set the justification of subtitles")#define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")#define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \ "UTF-8 encoding within subtitles files.")#define FORMAT_TEXT N_("Formatted Subtitles")#define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \ "VLC partly implements this, but you can choose to disable all formatting.")vlc_module_begin(); set_shortname( _("Subtitles")); set_description( _("Text subtitles decoder") ); set_capability( "decoder", 50 ); set_callbacks( OpenDecoder, CloseDecoder ); set_category( CAT_INPUT ); set_subcategory( SUBCAT_INPUT_SCODEC ); add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT, VLC_FALSE ); change_integer_list( pi_justification, ppsz_justification_text, 0 ); add_string( "subsdec-encoding", DEFAULT_NAME, NULL, ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE ); change_string_list( ppsz_encodings, 0, 0 ); add_bool( "subsdec-autodetect-utf8", VLC_TRUE, NULL, AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, VLC_FALSE ); add_bool( "subsdec-formatted", VLC_TRUE, NULL, FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );vlc_module_end();/***************************************************************************** * OpenDecoder: probe the decoder and return score ***************************************************************************** * Tries to launch a decoder and return score so that the interface is able * to chose. *****************************************************************************/static int OpenDecoder( vlc_object_t *p_this ){ decoder_t *p_dec = (decoder_t*)p_this; decoder_sys_t *p_sys; vlc_value_t val; if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') && p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') ) { return VLC_EGENERIC; } p_dec->pf_decode_sub = DecodeBlock; /* Allocate the memory needed to store the decoder's structure */ if( ( p_dec->p_sys = p_sys = (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL ) { msg_Err( p_dec, "out of memory" ); return VLC_ENOMEM; } /* init of p_sys */ p_sys->i_align = 0; p_sys->iconv_handle = (vlc_iconv_t)-1; p_sys->b_autodetect_utf8 = VLC_FALSE; p_sys->b_ass = VLC_FALSE; p_sys->i_original_height = -1; p_sys->i_original_width = -1; p_sys->pp_ssa_styles = NULL; p_sys->i_ssa_styles = 0; if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding ) { msg_Dbg( p_dec, "using demux suggested character encoding: %s", p_dec->fmt_in.subs.psz_encoding ); if( strcmp( p_dec->fmt_in.subs.psz_encoding, "UTF-8" ) ) p_sys->iconv_handle = vlc_iconv_open( "UTF-8", p_dec->fmt_in.subs.psz_encoding ); } else { var_Create( p_dec, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT ); var_Get( p_dec, "subsdec-encoding", &val ); if( !strcmp( val.psz_string, DEFAULT_NAME ) ) { const char *psz_charset = GetFallbackEncoding(); p_sys->b_autodetect_utf8 = var_CreateGetBool( p_dec, "subsdec-autodetect-utf8" ); p_sys->iconv_handle = vlc_iconv_open( "UTF-8", psz_charset ); msg_Dbg( p_dec, "using fallback character encoding: %s", psz_charset ); } else if( !strcmp( val.psz_string, "UTF-8" ) ) { msg_Dbg( p_dec, "using enforced character encoding: UTF-8" ); } else if( val.psz_string ) { msg_Dbg( p_dec, "using enforced character encoding: %s", val.psz_string ); p_sys->iconv_handle = vlc_iconv_open( "UTF-8", val.psz_string ); if( p_sys->iconv_handle == (vlc_iconv_t)-1 ) { msg_Warn( p_dec, "unable to do requested conversion" ); } } if( val.psz_string ) free( val.psz_string ); } var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); var_Get( p_dec, "subsdec-align", &val ); p_sys->i_align = val.i_int; if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) ) { if( p_dec->fmt_in.i_extra > 0 ) ParseSSAHeader( p_dec ); } return VLC_SUCCESS;}/**************************************************************************** * DecodeBlock: the whole thing **************************************************************************** * This function must be fed with complete subtitles units. ****************************************************************************/static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block ){ subpicture_t *p_spu = NULL; if( !pp_block || *pp_block == NULL ) return NULL; p_spu = ParseText( p_dec, *pp_block );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -