1 /*****************************************************************************
2 * subsdec.c : text subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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>
33 #include "vlc_filter.h"
37 /*****************************************************************************
38 * decoder_sys_t : decoder descriptor
39 *****************************************************************************/
42 int i_align; /* Subtitles alignment on the vout */
43 vlc_iconv_t iconv_handle; /* handle to iconv instance */
46 /*****************************************************************************
48 *****************************************************************************/
49 static int OpenDecoder ( vlc_object_t * );
50 static void CloseDecoder ( vlc_object_t * );
52 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
53 static subpicture_t *ParseText ( decoder_t *, block_t * );
54 static void StripTags ( char * );
56 #define DEFAULT_NAME "System Default"
58 /*****************************************************************************
60 *****************************************************************************/
61 static char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
62 "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
63 "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
64 "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
65 "ISO-8859-6", "CP1256", "MacArabic", "",
66 "ISO-8859-7", "CP1253", "MacGreek", "",
67 "ISO-8859-8", "CP1255", "MacHebrew", "",
68 "ISO-8859-9", "CP1254", "MacTurkish", "",
69 "ISO-8859-13", "CP1257", "",
70 "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
71 "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
72 "ISO-2022-KR", "EUC-KR", "",
73 "MacThai", "KOI8-T", "",
74 "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
75 "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
77 "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
78 "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
79 "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
80 "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
81 "HPROMAN8", "NEXTSTEP" };
83 static int pi_justification[] = { 0, 1, 2 };
84 static char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
86 #define ENCODING_TEXT N_("Subtitles text encoding")
87 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
88 #define ALIGN_TEXT N_("Subtitles justification")
89 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
92 set_description( _("text subtitles decoder") );
93 set_capability( "decoder", 50 );
94 set_callbacks( OpenDecoder, CloseDecoder );
96 add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
98 change_integer_list( pi_justification, ppsz_justification_text, 0 );
99 add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
100 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
101 change_string_list( ppsz_encodings, 0, 0 );
104 /*****************************************************************************
105 * OpenDecoder: probe the decoder and return score
106 *****************************************************************************
107 * Tries to launch a decoder and return score so that the interface is able
109 *****************************************************************************/
110 static int OpenDecoder( vlc_object_t *p_this )
112 decoder_t *p_dec = (decoder_t*)p_this;
113 decoder_sys_t *p_sys;
116 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
117 p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
122 p_dec->pf_decode_sub = DecodeBlock;
124 /* Allocate the memory needed to store the decoder's structure */
125 if( ( p_dec->p_sys = p_sys =
126 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
128 msg_Err( p_dec, "out of memory" );
132 var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
133 var_Get( p_dec, "subsdec-align", &val );
134 p_sys->i_align = val.i_int;
136 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
138 msg_Dbg( p_dec, "using character encoding: %s",
139 p_dec->fmt_in.subs.psz_encoding );
140 p_sys->iconv_handle =
141 vlc_iconv_open( "UTF-8", p_dec->fmt_in.subs.psz_encoding );
145 var_Create( p_dec, "subsdec-encoding",
146 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
147 var_Get( p_dec, "subsdec-encoding", &val );
148 if( !strcmp( val.psz_string, DEFAULT_NAME ) )
150 char *psz_charset =(char*)malloc( 100 );
151 vlc_current_charset( &psz_charset );
152 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", psz_charset );
153 msg_Dbg( p_dec, "using character encoding: %s", psz_charset );
156 else if( val.psz_string )
158 msg_Dbg( p_dec, "using character encoding: %s", val.psz_string );
159 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", val.psz_string );
162 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
164 msg_Warn( p_dec, "unable to do requested conversion" );
167 if( val.psz_string ) free( val.psz_string );
173 /****************************************************************************
174 * DecodeBlock: the whole thing
175 ****************************************************************************
176 * This function must be fed with complete subtitles units.
177 ****************************************************************************/
178 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
182 if( !pp_block || *pp_block == NULL ) return NULL;
184 p_spu = ParseText( p_dec, *pp_block );
186 block_Release( *pp_block );
192 /*****************************************************************************
193 * CloseDecoder: clean up the decoder
194 *****************************************************************************/
195 static void CloseDecoder( vlc_object_t *p_this )
197 decoder_t *p_dec = (decoder_t *)p_this;
198 decoder_sys_t *p_sys = p_dec->p_sys;
200 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
202 vlc_iconv_close( p_sys->iconv_handle );
208 /*****************************************************************************
209 * ParseText: parse an text subtitle packet and send it to the video output
210 *****************************************************************************/
211 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
213 decoder_sys_t *p_sys = p_dec->p_sys;
214 subpicture_t *p_spu = 0;
216 int i_align_h, i_align_v;
219 /* We cannot display a subpicture with no date */
220 if( p_block->i_pts == 0 )
222 msg_Warn( p_dec, "subtitle without a date" );
226 /* Check validity of packet data */
227 if( p_block->i_buffer <= 1 || p_block->p_buffer[0] == '\0' )
229 msg_Warn( p_dec, "empty subtitle" );
233 /* Should be resiliant against bad subtitles */
234 psz_subtitle = strndup( p_block->p_buffer, p_block->i_buffer );
236 i_align_h = p_sys->i_align ? 20 : 0;
239 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
241 char *psz_new_subtitle;
242 char *psz_convert_buffer_out;
243 char *psz_convert_buffer_in;
244 size_t ret, inbytes_left, outbytes_left;
246 psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) );
247 psz_convert_buffer_out = psz_new_subtitle;
248 psz_convert_buffer_in = psz_subtitle;
249 inbytes_left = strlen( psz_subtitle );
250 outbytes_left = 6 * inbytes_left;
251 ret = vlc_iconv( p_sys->iconv_handle, &psz_convert_buffer_in,
252 &inbytes_left, &psz_convert_buffer_out,
254 *psz_convert_buffer_out = '\0';
258 msg_Warn( p_dec, "Failed to convert subtitle encoding, "
259 "dropping subtitle.\nTry setting a different "
260 "character-encoding for the subtitle." );
261 free( psz_subtitle );
266 free( psz_subtitle );
267 psz_subtitle = psz_new_subtitle;
271 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
273 /* Decode SSA strings */
274 /* We expect: ReadOrder, Layer, Style, Name, MarginL, MarginR,
275 * MarginV, Effect, Text */
276 char *psz_new_subtitle;
277 char *psz_buffer_sub;
281 psz_buffer_sub = psz_subtitle;
285 while( i_comma < 8 &&
286 *psz_buffer_sub != '\0' )
288 if( *psz_buffer_sub == ',' )
294 psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
296 while( psz_buffer_sub[0] != '\0' )
298 if( psz_buffer_sub[0] == '\\' && ( psz_buffer_sub[1] == 'n' ||
299 psz_buffer_sub[1] == 'N' ) )
301 psz_new_subtitle[i_text] = '\n';
305 else if( psz_buffer_sub[0] == '{' &&
306 psz_buffer_sub[1] == '\\' )
308 /* SSA control code */
309 while( psz_buffer_sub[0] != '\0' &&
310 psz_buffer_sub[0] != '}' )
318 psz_new_subtitle[i_text] = psz_buffer_sub[0];
323 psz_new_subtitle[i_text] = '\0';
324 free( psz_subtitle );
325 psz_subtitle = psz_new_subtitle;
330 StripTags( psz_subtitle );
332 p_spu = p_dec->pf_spu_buffer_new( p_dec );
335 msg_Warn( p_dec, "can't get spu buffer" );
336 free( psz_subtitle );
340 /* Create a new subpicture region */
341 memset( &fmt, 0, sizeof(video_format_t) );
342 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
344 fmt.i_width = fmt.i_height = 0;
345 fmt.i_x_offset = fmt.i_y_offset = 0;
346 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
347 if( !p_spu->p_region )
349 msg_Err( p_dec, "cannot allocate SPU region" );
350 free( psz_subtitle );
351 p_dec->pf_spu_buffer_del( p_dec, p_spu );
355 p_spu->p_region->psz_text = psz_subtitle;
356 p_spu->i_start = p_block->i_pts;
357 p_spu->i_stop = p_block->i_pts + p_block->i_length;
358 p_spu->b_ephemer = (p_block->i_length == 0);
359 p_spu->b_absolute = VLC_FALSE;
361 p_spu->i_flags = OSD_ALIGN_BOTTOM | p_sys->i_align;
362 p_spu->i_x = i_align_h;
363 p_spu->i_y = i_align_v;
368 static void StripTags( char *psz_text )
370 int i_left_moves = 0;
371 vlc_bool_t b_inside_tag = VLC_FALSE;
373 int i_tag_start = -1;
374 while( psz_text[ i ] )
378 if( psz_text[ i ] == '<' )
380 b_inside_tag = VLC_TRUE;
383 psz_text[ i - i_left_moves ] = psz_text[ i ];
387 if( ( psz_text[ i ] == ' ' ) ||
388 ( psz_text[ i ] == '\t' ) ||
389 ( psz_text[ i ] == '\n' ) ||
390 ( psz_text[ i ] == '\r' ) )
392 b_inside_tag = VLC_FALSE;
395 else if( psz_text[ i ] == '>' )
397 i_left_moves += i - i_tag_start + 1;
399 b_inside_tag = VLC_FALSE;
403 psz_text[ i - i_left_moves ] = psz_text[ i ];
408 psz_text[ i - i_left_moves ] = '\0';