1 /*****************************************************************************
2 * subsdec.c : text subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Samuel Hocevar <sam@zoy.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <vlc/decoder.h>
34 #if defined(HAVE_ICONV)
40 /*****************************************************************************
41 * decoder_sys_t : decoder descriptor
42 *****************************************************************************/
45 int i_align; /* Subtitles alignment on the vout */
47 #if defined(HAVE_ICONV)
48 iconv_t iconv_handle; /* handle to iconv instance */
52 /*****************************************************************************
54 *****************************************************************************/
55 static int OpenDecoder ( vlc_object_t * );
56 static void CloseDecoder ( vlc_object_t * );
58 static void DecodeBlock ( decoder_t *, block_t ** );
60 static void ParseText ( decoder_t *, block_t *, vout_thread_t * );
61 static void StripTags ( char * );
63 #define DEFAULT_NAME "System Default"
65 /*****************************************************************************
67 *****************************************************************************/
68 #if defined(HAVE_ICONV)
69 static char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
70 "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
71 "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
72 "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
73 "ISO-8859-6", "CP1256", "MacArabic", "",
74 "ISO-8859-7", "CP1253", "MacGreek", "",
75 "ISO-8859-8", "CP1255", "MacHebrew", "",
76 "ISO-8859-9", "CP1254", "MacTurkish", "",
77 "ISO-8859-13", "CP1257", "",
78 "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
79 "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
80 "ISO-2022-KR", "EUC-KR", "",
81 "MacThai", "KOI8-T", "",
82 "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
83 "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
85 "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
86 "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
87 "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
88 "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
89 "HPROMAN8", "NEXTSTEP" };
92 static int pi_justification[] = { 0, 1, 2 };
93 static char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
95 #define ENCODING_TEXT N_("Subtitles text encoding")
96 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
97 #define ALIGN_TEXT N_("Subtitles justification")
98 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
101 set_description( _("text subtitles decoder") );
102 set_capability( "decoder", 50 );
103 set_callbacks( OpenDecoder, CloseDecoder );
105 add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
107 change_integer_list( pi_justification, ppsz_justification_text, 0 );
108 #if defined(HAVE_ICONV)
109 add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
110 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
111 change_string_list( ppsz_encodings, 0, 0 );
115 /*****************************************************************************
116 * OpenDecoder: probe the decoder and return score
117 *****************************************************************************
118 * Tries to launch a decoder and return score so that the interface is able
120 *****************************************************************************/
121 static int OpenDecoder( vlc_object_t *p_this )
123 decoder_t *p_dec = (decoder_t*)p_this;
124 decoder_sys_t *p_sys;
127 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
128 p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
133 p_dec->pf_decode_sub = DecodeBlock;
135 /* Allocate the memory needed to store the decoder's structure */
136 if( ( p_dec->p_sys = p_sys =
137 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
139 msg_Err( p_dec, "out of memory" );
143 var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
144 var_Get( p_dec, "subsdec-align", &val );
145 p_sys->i_align = val.i_int;
147 #if defined(HAVE_ICONV)
148 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
150 msg_Dbg( p_dec, "using character encoding: %s",
151 p_dec->fmt_in.subs.psz_encoding );
152 p_sys->iconv_handle = iconv_open( "UTF-8",
153 p_dec->fmt_in.subs.psz_encoding );
157 var_Create( p_dec, "subsdec-encoding",
158 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
159 var_Get( p_dec, "subsdec-encoding", &val );
160 if( !strcmp( val.psz_string, DEFAULT_NAME ) )
162 char *psz_charset =(char*)malloc( 100 );
163 vlc_current_charset( &psz_charset );
164 p_sys->iconv_handle = iconv_open( "UTF-8", psz_charset );
165 msg_Dbg( p_dec, "using character encoding: %s", psz_charset );
168 else if( val.psz_string )
170 msg_Dbg( p_dec, "using character encoding: %s", val.psz_string );
171 p_sys->iconv_handle = iconv_open( "UTF-8", val.psz_string );
174 if( p_sys->iconv_handle == (iconv_t)-1 )
176 msg_Warn( p_dec, "unable to do requested conversion" );
179 if( val.psz_string ) free( val.psz_string );
183 msg_Dbg( p_dec, "no iconv support available" );
189 /****************************************************************************
190 * DecodeBlock: the whole thing
191 ****************************************************************************
192 * This function must be fed with complete subtitles units.
193 ****************************************************************************/
194 static void DecodeBlock( decoder_t *p_dec, block_t **pp_block )
196 vout_thread_t *p_vout;
198 if( !pp_block || *pp_block == NULL )
203 /* Here we are dealing with text subtitles */
204 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
207 ParseText( p_dec, *pp_block, p_vout );
208 vlc_object_release( p_vout );
212 msg_Warn( p_dec, "couldn't find a video output, trashing subtitle" );
215 block_Release( *pp_block );
219 /*****************************************************************************
220 * CloseDecoder: clean up the decoder
221 *****************************************************************************/
222 static void CloseDecoder( vlc_object_t *p_this )
224 decoder_t *p_dec = (decoder_t *)p_this;
225 decoder_sys_t *p_sys = p_dec->p_sys;
226 vout_thread_t *p_vout;
228 p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
229 if( p_vout != NULL && p_vout->p_subpicture != NULL )
231 subpicture_t *p_subpic;
234 for( i_subpic = 0; i_subpic < VOUT_MAX_SUBPICTURES; i_subpic++ )
236 p_subpic = &p_vout->p_subpicture[i_subpic];
238 if( p_subpic != NULL &&
239 ( p_subpic->i_status == RESERVED_SUBPICTURE
240 || p_subpic->i_status == READY_SUBPICTURE ) )
242 vout_DestroySubPicture( p_vout, p_subpic );
246 if( p_vout ) vlc_object_release( p_vout );
248 #if defined(HAVE_ICONV)
249 if( p_sys->iconv_handle != (iconv_t)-1 )
251 iconv_close( p_sys->iconv_handle );
258 /*****************************************************************************
259 * ParseText: parse an text subtitle packet and send it to the video output
260 *****************************************************************************/
261 static void ParseText( decoder_t *p_dec, block_t *p_block,
262 vout_thread_t *p_vout )
264 decoder_sys_t *p_sys = p_dec->p_sys;
266 int i_align_h, i_align_v;
268 /* We cannot display a subpicture with no date */
269 if( p_block->i_pts == 0 )
271 msg_Warn( p_dec, "subtitle without a date" );
275 /* Check validity of packet data */
276 if( p_block->i_buffer <= 1 || p_block->p_buffer[0] == '\0' )
278 msg_Warn( p_dec, "empty subtitle" );
282 /* Should be resiliant against bad subtitles */
283 psz_subtitle = strndup( p_block->p_buffer, p_block->i_buffer );
285 i_align_h = p_sys->i_align ? 20 : 0;
288 #if defined(HAVE_ICONV)
289 if( p_sys->iconv_handle != (iconv_t)-1 )
291 char *psz_new_subtitle;
292 char *psz_convert_buffer_out;
293 char *psz_convert_buffer_in;
294 size_t ret, inbytes_left, outbytes_left;
296 psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) );
297 psz_convert_buffer_out = psz_new_subtitle;
298 psz_convert_buffer_in = psz_subtitle;
299 inbytes_left = strlen( psz_subtitle );
300 outbytes_left = 6 * inbytes_left;
301 ret = iconv( p_sys->iconv_handle, &psz_convert_buffer_in,
302 &inbytes_left, &psz_convert_buffer_out, &outbytes_left );
303 *psz_convert_buffer_out = '\0';
307 msg_Warn( p_dec, "Failed to convert subtitle encoding, dropping subtitle.\nTry setting a different character-encoding for the subtitle." );
308 free( psz_subtitle );
313 free( psz_subtitle );
314 psz_subtitle = psz_new_subtitle;
319 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
321 /* Decode SSA strings */
322 /* We expect: ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text */
323 char *psz_new_subtitle;
324 char *psz_buffer_sub;
328 psz_buffer_sub = psz_subtitle;
332 while( i_comma < 8 &&
333 *psz_buffer_sub != '\0' )
335 if( *psz_buffer_sub == ',' )
341 psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
343 while( psz_buffer_sub[0] != '\0' )
345 if( psz_buffer_sub[0] == '\\' && ( psz_buffer_sub[1] =='n' || psz_buffer_sub[1] =='N' ) )
347 psz_new_subtitle[i_text] = '\n';
351 else if( psz_buffer_sub[0] == '{' && psz_buffer_sub[1] == '\\' )
353 /* SSA control code */
354 while( psz_buffer_sub[0] != '\0' && psz_buffer_sub[0] != '}' )
362 psz_new_subtitle[i_text] = psz_buffer_sub[0];
367 psz_new_subtitle[i_text] = '\0';
368 free( psz_subtitle );
369 psz_subtitle = psz_new_subtitle;
373 StripTags( psz_subtitle );
374 vout_ShowTextAbsolute( p_vout, SUBT1_CHAN, psz_subtitle, NULL,
375 OSD_ALIGN_BOTTOM | p_sys->i_align, i_align_h,
376 i_align_v, p_block->i_pts,
377 p_block->i_length ? p_block->i_pts + p_block->i_length : 0 );
379 free( psz_subtitle );
382 static void StripTags( char *psz_text )
384 int i_left_moves = 0;
385 vlc_bool_t b_inside_tag = VLC_FALSE;
387 int i_tag_start = -1;
388 while( psz_text[ i ] )
392 if( psz_text[ i ] == '<' )
394 b_inside_tag = VLC_TRUE;
397 psz_text[ i - i_left_moves ] = psz_text[ i ];
401 if( ( psz_text[ i ] == ' ' ) ||
402 ( psz_text[ i ] == '\t' ) ||
403 ( psz_text[ i ] == '\n' ) ||
404 ( psz_text[ i ] == '\r' ) )
406 b_inside_tag = VLC_FALSE;
409 else if( psz_text[ i ] == '>' )
411 i_left_moves += i - i_tag_start + 1;
413 b_inside_tag = VLC_FALSE;
417 psz_text[ i - i_left_moves ] = psz_text[ i ];
422 psz_text[ i - i_left_moves ] = '\0';