1 /*****************************************************************************
2 * subsdec.c : text subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Samuel Hocevar <sam@zoy.org>
9 * Derk-Jan Hartman <hartman at videolan dot org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <vlc_codec.h>
34 #include <vlc_filter.h>
35 #include <vlc_charset.h>
39 char * psz_stylename; /* The name of the style, no comma's allowed */
40 text_style_t font_style;
46 /*****************************************************************************
47 * decoder_sys_t : decoder descriptor
48 *****************************************************************************/
51 vlc_bool_t b_ass; /* The subs are ASS */
52 int i_original_height;
54 int i_align; /* Subtitles alignment on the vout */
55 vlc_iconv_t iconv_handle; /* handle to iconv instance */
56 vlc_bool_t b_autodetect_utf8;
58 ssa_style_t **pp_ssa_styles;
62 /*****************************************************************************
64 *****************************************************************************/
65 static int OpenDecoder ( vlc_object_t * );
66 static void CloseDecoder ( vlc_object_t * );
68 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
69 static subpicture_t *ParseText ( decoder_t *, block_t * );
70 static void ParseSSAHeader ( decoder_t * );
71 static void ParseSSAString ( decoder_t *, char *, subpicture_t * );
72 static void ParseColor ( decoder_t *, char *, int *, int * );
73 static void StripTags ( char * );
75 #define DEFAULT_NAME "Default"
78 /*****************************************************************************
80 *****************************************************************************/
81 static const char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
82 "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
83 "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
84 "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
85 "ISO-8859-6", "CP1256", "MacArabic", "",
86 "ISO-8859-7", "CP1253", "MacGreek", "",
87 "ISO-8859-8", "CP1255", "MacHebrew", "",
88 "ISO-8859-9", "CP1254", "MacTurkish", "",
89 "ISO-8859-13", "CP1257", "",
90 "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
91 "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
92 "ISO-2022-KR", "EUC-KR", "",
93 "MacThai", "KOI8-T", "",
94 "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
95 "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
97 "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
98 "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
99 "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
100 "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
101 "HPROMAN8", "NEXTSTEP" };
103 SSA supports charset selection.
104 The following known charsets are used:
106 0 = Ansi - Western European
111 128 = Japanese (Shift JIS)
114 134 = GB2312 Simplified Chinese
115 136 = Big5 Traditional Chinese
122 204 = Russian (Cyrillic)
124 238 = Eastern European
128 static int pi_justification[] = { 0, 1, 2 };
129 static const char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
131 #define ENCODING_TEXT N_("Subtitles text encoding")
132 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
133 #define ALIGN_TEXT N_("Subtitles justification")
134 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
135 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
136 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
137 "UTF-8 encoding within subtitles files.")
138 #define FORMAT_TEXT N_("Formatted Subtitles")
139 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
140 "VLC partly implements this, but you can choose to disable all formatting.")
144 set_shortname( _("Subtitles"));
145 set_description( _("Text subtitles decoder") );
146 set_capability( "decoder", 50 );
147 set_callbacks( OpenDecoder, CloseDecoder );
148 set_category( CAT_INPUT );
149 set_subcategory( SUBCAT_INPUT_SCODEC );
151 add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
153 change_integer_list( pi_justification, ppsz_justification_text, 0 );
154 add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
155 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
156 change_string_list( ppsz_encodings, 0, 0 );
157 add_bool( "subsdec-autodetect-utf8", VLC_TRUE, NULL,
158 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, VLC_FALSE );
159 add_bool( "subsdec-formatted", VLC_TRUE, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
163 /*****************************************************************************
164 * OpenDecoder: probe the decoder and return score
165 *****************************************************************************
166 * Tries to launch a decoder and return score so that the interface is able
168 *****************************************************************************/
169 static int OpenDecoder( vlc_object_t *p_this )
171 decoder_t *p_dec = (decoder_t*)p_this;
172 decoder_sys_t *p_sys;
175 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
176 p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
181 p_dec->pf_decode_sub = DecodeBlock;
183 /* Allocate the memory needed to store the decoder's structure */
184 if( ( p_dec->p_sys = p_sys =
185 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
187 msg_Err( p_dec, "out of memory" );
193 p_sys->iconv_handle = (vlc_iconv_t)-1;
194 p_sys->b_autodetect_utf8 = VLC_FALSE;
195 p_sys->b_ass = VLC_FALSE;
196 p_sys->i_original_height = -1;
197 p_sys->i_original_width = -1;
198 p_sys->pp_ssa_styles = NULL;
199 p_sys->i_ssa_styles = 0;
201 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
203 msg_Dbg( p_dec, "using demux suggested character encoding: %s",
204 p_dec->fmt_in.subs.psz_encoding );
205 if( strcmp( p_dec->fmt_in.subs.psz_encoding, "UTF-8" ) )
206 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", p_dec->fmt_in.subs.psz_encoding );
210 var_Create( p_dec, "subsdec-encoding",
211 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
212 var_Get( p_dec, "subsdec-encoding", &val );
213 if( !strcmp( val.psz_string, DEFAULT_NAME ) )
215 const char *psz_charset = GetFallbackEncoding();
217 p_sys->b_autodetect_utf8 = var_CreateGetBool( p_dec,
218 "subsdec-autodetect-utf8" );
220 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", psz_charset );
221 msg_Dbg( p_dec, "using fallback character encoding: %s", psz_charset );
223 else if( !strcmp( val.psz_string, "UTF-8" ) )
225 msg_Dbg( p_dec, "using enforced character encoding: UTF-8" );
227 else if( val.psz_string )
229 msg_Dbg( p_dec, "using enforced character encoding: %s", val.psz_string );
230 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", val.psz_string );
231 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
233 msg_Warn( p_dec, "unable to do requested conversion" );
236 if( val.psz_string ) free( val.psz_string );
239 var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
240 var_Get( p_dec, "subsdec-align", &val );
241 p_sys->i_align = val.i_int;
243 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
245 if( p_dec->fmt_in.i_extra > 0 )
246 ParseSSAHeader( p_dec );
252 /****************************************************************************
253 * DecodeBlock: the whole thing
254 ****************************************************************************
255 * This function must be fed with complete subtitles units.
256 ****************************************************************************/
257 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
259 subpicture_t *p_spu = NULL;
261 if( !pp_block || *pp_block == NULL ) return NULL;
263 p_spu = ParseText( p_dec, *pp_block );
265 block_Release( *pp_block );
271 /*****************************************************************************
272 * CloseDecoder: clean up the decoder
273 *****************************************************************************/
274 static void CloseDecoder( vlc_object_t *p_this )
276 decoder_t *p_dec = (decoder_t *)p_this;
277 decoder_sys_t *p_sys = p_dec->p_sys;
279 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
281 vlc_iconv_close( p_sys->iconv_handle );
284 if( p_sys->pp_ssa_styles )
287 for( i = 0; i < p_sys->i_ssa_styles; i++ )
289 if( p_sys->pp_ssa_styles[i]->psz_stylename ) free( p_sys->pp_ssa_styles[i]->psz_stylename );
290 p_sys->pp_ssa_styles[i]->psz_stylename = NULL;
291 if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname ) free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
292 p_sys->pp_ssa_styles[i]->font_style.psz_fontname = NULL;
293 if( p_sys->pp_ssa_styles[i] ) free( p_sys->pp_ssa_styles[i] ); p_sys->pp_ssa_styles[i] = NULL;
295 free( p_sys->pp_ssa_styles ); p_sys->pp_ssa_styles = NULL;
301 /*****************************************************************************
302 * ParseText: parse an text subtitle packet and send it to the video output
303 *****************************************************************************/
304 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
306 decoder_sys_t *p_sys = p_dec->p_sys;
307 subpicture_t *p_spu = NULL;
308 char *psz_subtitle = NULL;
311 /* We cannot display a subpicture with no date */
312 if( p_block->i_pts == 0 )
314 msg_Warn( p_dec, "subtitle without a date" );
318 /* Check validity of packet data */
319 /* An "empty" line containing only \0 can be used to force
320 and ephemer picture from the screen */
321 if( p_block->i_buffer < 1 )
323 msg_Warn( p_dec, "no subtitle data" );
327 /* Should be resiliant against bad subtitles */
328 psz_subtitle = strndup( (const char *)p_block->p_buffer,
330 if( psz_subtitle == NULL )
333 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
334 EnsureUTF8( psz_subtitle );
338 if( p_sys->b_autodetect_utf8 )
340 if( IsUTF8( psz_subtitle ) == NULL )
342 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
343 "disabling UTF-8 subtitles autodetection" );
344 p_sys->b_autodetect_utf8 = VLC_FALSE;
348 if( !p_sys->b_autodetect_utf8 )
350 size_t inbytes_left = strlen( psz_subtitle );
351 size_t outbytes_left = 6 * inbytes_left;
352 char *psz_new_subtitle = malloc( outbytes_left + 1 );
353 char *psz_convert_buffer_out = psz_new_subtitle;
354 const char *psz_convert_buffer_in = psz_subtitle;
356 size_t ret = vlc_iconv( p_sys->iconv_handle,
357 &psz_convert_buffer_in, &inbytes_left,
358 &psz_convert_buffer_out, &outbytes_left );
360 *psz_convert_buffer_out++ = '\0';
361 free( psz_subtitle );
363 if( ( ret == (size_t)(-1) ) || inbytes_left )
365 free( psz_new_subtitle );
366 msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
367 "Try manually setting a character-encoding "
368 "before you open the file.") );
372 psz_subtitle = realloc( psz_new_subtitle,
373 psz_convert_buffer_out - psz_new_subtitle );
377 /* Create the subpicture unit */
378 p_spu = p_dec->pf_spu_buffer_new( p_dec );
381 msg_Warn( p_dec, "can't get spu buffer" );
382 if( psz_subtitle ) free( psz_subtitle );
386 p_spu->b_pausable = VLC_TRUE;
388 /* Create a new subpicture region */
389 memset( &fmt, 0, sizeof(video_format_t) );
390 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
392 fmt.i_width = fmt.i_height = 0;
393 fmt.i_x_offset = fmt.i_y_offset = 0;
394 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
395 if( !p_spu->p_region )
397 msg_Err( p_dec, "cannot allocate SPU region" );
398 if( psz_subtitle ) free( psz_subtitle );
399 p_dec->pf_spu_buffer_del( p_dec, p_spu );
403 /* Decode and format the subpicture unit */
404 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
406 /* Normal text subs, easy markup */
407 p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
408 p_spu->i_x = p_sys->i_align ? 20 : 0;
411 /* Remove formatting from string */
412 StripTags( psz_subtitle );
414 p_spu->p_region->psz_text = psz_subtitle;
415 p_spu->i_start = p_block->i_pts;
416 p_spu->i_stop = p_block->i_pts + p_block->i_length;
417 p_spu->b_ephemer = (p_block->i_length == 0);
418 p_spu->b_absolute = VLC_FALSE;
422 /* Decode SSA strings */
423 ParseSSAString( p_dec, psz_subtitle, p_spu );
424 p_spu->i_start = p_block->i_pts;
425 p_spu->i_stop = p_block->i_pts + p_block->i_length;
426 p_spu->b_ephemer = (p_block->i_length == 0);
427 p_spu->b_absolute = VLC_FALSE;
428 p_spu->i_original_picture_width = p_sys->i_original_width;
429 p_spu->i_original_picture_height = p_sys->i_original_height;
430 if( psz_subtitle ) free( psz_subtitle );
435 static void ParseSSAString( decoder_t *p_dec, char *psz_subtitle, subpicture_t *p_spu_in )
437 /* We expect MKV formatted SSA:
438 * ReadOrder, Layer, Style, CharacterName, MarginL, MarginR,
439 * MarginV, Effect, Text */
440 decoder_sys_t *p_sys = p_dec->p_sys;
441 subpicture_t *p_spu = p_spu_in;
442 ssa_style_t *p_style = NULL;
443 char *psz_new_subtitle = NULL;
444 char *psz_buffer_sub = NULL;
445 char *psz_style = NULL;
446 char *psz_style_start = NULL;
447 char *psz_style_end = NULL;
448 int i_text = 0, i_comma = 0, i_strlen = 0, i;
449 int i_margin_l = 0, i_margin_r = 0, i_margin_v = 0;
451 psz_buffer_sub = psz_subtitle;
454 while( i_comma < 8 && *psz_buffer_sub != '\0' )
456 if( *psz_buffer_sub == ',' )
459 if( i_comma == 2 ) psz_style_start = &psz_buffer_sub[1];
460 if( i_comma == 3 ) psz_style_end = &psz_buffer_sub[0];
461 if( i_comma == 4 ) i_margin_l = (int)strtol( psz_buffer_sub+1, NULL, 10 );
462 if( i_comma == 5 ) i_margin_r = (int)strtol( psz_buffer_sub+1, NULL, 10 );
463 if( i_comma == 6 ) i_margin_v = (int)strtol( psz_buffer_sub+1, NULL, 10 );
468 if( *psz_buffer_sub == '\0' && i_comma == 8 )
470 msg_Dbg( p_dec, "couldn't find all fields in this SSA line" );
474 psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
476 while( psz_buffer_sub[0] != '\0' )
478 if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'n' )
480 psz_new_subtitle[i_text] = ' ';
484 else if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'N' )
486 psz_new_subtitle[i_text] = '\n';
490 else if( psz_buffer_sub[0] == '{' &&
491 psz_buffer_sub[1] == '\\' )
493 /* SSA control code */
494 while( psz_buffer_sub[0] != '\0' &&
495 psz_buffer_sub[0] != '}' )
503 psz_new_subtitle[i_text] = psz_buffer_sub[0];
508 psz_new_subtitle[i_text] = '\0';
510 i_strlen = __MAX( psz_style_end - psz_style_start, 0);
511 psz_style = (char *)malloc( i_strlen + 1);
512 psz_style = memcpy( psz_style, psz_style_start, i_strlen );
513 psz_style[i_strlen] = '\0';
515 for( i = 0; i < p_sys->i_ssa_styles; i++ )
517 if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
518 p_style = p_sys->pp_ssa_styles[i];
520 if( psz_style ) free( psz_style );
522 p_spu->p_region->psz_text = psz_new_subtitle;
523 if( p_style == NULL )
525 p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
526 p_spu->i_x = p_sys->i_align ? 20 : 0;
531 msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename);
532 p_spu->p_region->p_style = &p_style->font_style;
533 p_spu->i_flags = p_style->i_align;
534 if( p_style->i_align & SUBPICTURE_ALIGN_LEFT )
536 p_spu->i_x = (i_margin_l) ? i_margin_l : p_style->i_margin_h;
538 else if( p_style->i_align & SUBPICTURE_ALIGN_RIGHT )
540 p_spu->i_x = (i_margin_r) ? i_margin_r : p_style->i_margin_h;
542 p_spu->i_y = (i_margin_v) ? i_margin_v : p_style->i_margin_v;
546 static char* GotoNextLine( char *psz_text )
548 char *p_newline = psz_text;
550 while( p_newline[0] != '\0' )
552 if( p_newline[0] == '\n' || p_newline[0] == '\r' )
555 while( p_newline[0] == '\n' || p_newline[0] == '\r' )
564 /*****************************************************************************
565 * ParseColor: SSA stores color in BBGGRR, in ASS it uses AABBGGRR
566 * The string value in the string can be a pure integer, or hexadecimal &HBBGGRR
567 *****************************************************************************/
568 static void ParseColor( decoder_t *p_dec, char *psz_color, int *pi_color, int *pi_alpha )
571 if( !strncasecmp( psz_color, "&H", 2 ) )
573 /* textual HEX representation */
574 i_color = (int) strtol( psz_color+2, NULL, 16 );
576 else i_color = (int) strtol( psz_color, NULL, 0 );
579 *pi_color |= ( ( i_color & 0x000000FF ) << 16 ); /* Red */
580 *pi_color |= ( ( i_color & 0x0000FF00 ) ); /* Green */
581 *pi_color |= ( ( i_color & 0x00FF0000 ) >> 16 ); /* Blue */
583 if( pi_alpha != NULL )
584 *pi_alpha = ( i_color & 0xFF000000 ) >> 24;
587 /*****************************************************************************
588 * ParseSSAHeader: Retrieve global formatting information etc
589 *****************************************************************************/
590 static void ParseSSAHeader( decoder_t *p_dec )
592 decoder_sys_t *p_sys = p_dec->p_sys;
593 char *psz_parser = NULL;
594 char *psz_header = malloc( p_dec->fmt_in.i_extra+1 );
595 int i_section_type = 1;
597 memcpy( psz_header, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
598 psz_header[ p_dec->fmt_in.i_extra] = '\0';
600 /* Handle [Script Info] section */
601 psz_parser = strcasestr( psz_header, "[Script Info]" );
602 if( psz_parser == NULL ) goto eof;
604 psz_parser = GotoNextLine( psz_parser );
606 while( psz_parser[0] != '\0' )
609 char buffer_text[MAX_LINE + 1];
611 if( psz_parser[0] == '!' || psz_parser[0] == ';' ) /* comment */;
612 else if( sscanf( psz_parser, "PlayResX: %d", &temp ) == 1 )
613 p_sys->i_original_width = ( temp > 0 ) ? temp : -1;
614 else if( sscanf( psz_parser, "PlayResY: %d", &temp ) == 1 )
615 p_sys->i_original_height = ( temp > 0 ) ? temp : -1;
616 else if( sscanf( psz_parser, "Script Type: %8192s", buffer_text ) == 1 )
618 if( !strcasecmp( buffer_text, "V4.00+" ) ) p_sys->b_ass = VLC_TRUE;
620 else if( !strncasecmp( psz_parser, "[V4 Styles]", 11 ) )
622 else if( !strncasecmp( psz_parser, "[V4+ Styles]", 12) )
625 p_sys->b_ass = VLC_TRUE;
627 else if( !strncasecmp( psz_parser, "[Events]", 8 ) )
629 else if( !strncasecmp( psz_parser, "Style:", 6 ) )
631 int i_font_size, i_bold, i_italic, i_border, i_outline, i_shadow, i_underline,
632 i_strikeout, i_scale_x, i_scale_y, i_spacing, i_align, i_margin_l, i_margin_r, i_margin_v;
634 char psz_temp_stylename[MAX_LINE+1];
635 char psz_temp_fontname[MAX_LINE+1];
636 char psz_temp_color1[MAX_LINE+1];
637 char psz_temp_color2[MAX_LINE+1];
638 char psz_temp_color3[MAX_LINE+1];
639 char psz_temp_color4[MAX_LINE+1];
641 if( i_section_type == 1 ) /* V4 */
643 if( sscanf( psz_parser, "Style: %8192[^,],%8192[^,],%d,%8192[^,],%8192[^,],%8192[^,],%8192[^,],%d,%d,%d,%d,%d,%d,%d,%d,%d%*[^\r\n]",
644 psz_temp_stylename, psz_temp_fontname, &i_font_size,
645 psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
646 &i_border, &i_outline, &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 16 )
648 ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
650 p_style->psz_stylename = strdup( psz_temp_stylename );
651 p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
652 p_style->font_style.i_font_size = i_font_size;
654 ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, NULL );
655 ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, NULL );
656 p_style->font_style.i_outline_color = p_style->font_style.i_shadow_color;
657 p_style->font_style.i_font_alpha = p_style->font_style.i_outline_alpha = p_style->font_style.i_shadow_alpha = 0x00;
658 p_style->font_style.i_style_flags = 0;
659 if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
660 if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
662 if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
663 else if( i_border == 3 )
665 p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
666 p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
667 p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
669 p_style->font_style.i_shadow_width = i_shadow;
670 p_style->font_style.i_outline_width = i_outline;
672 p_style->i_align = 0;
673 if( i_align == 1 || i_align == 5 || i_align == 9 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
674 if( i_align == 3 || i_align == 7 || i_align == 11 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
675 if( i_align < 4 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
676 else if( i_align < 8 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP;
678 p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
679 p_style->i_margin_v = i_margin_v;
681 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
683 else msg_Warn( p_dec, "SSA v4 styleline parsing failed" );
685 else if( i_section_type == 2 ) /* V4+ */
687 /* Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour,
688 Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline,
689 Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
691 if( sscanf( psz_parser, "Style: %8192[^,],%8192[^,],%d,%8192[^,],%8192[^,],%8192[^,],%8192[^,],%d,%d,%d,%d,%d,%d,%d,%*f,%d,%d,%d,%d,%d,%d,%d%*[^\r\n]",
692 psz_temp_stylename, psz_temp_fontname, &i_font_size,
693 psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
694 &i_underline, &i_strikeout, &i_scale_x, &i_scale_y, &i_spacing, &i_border, &i_outline,
695 &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 21 )
697 ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
699 p_style->psz_stylename = strdup( psz_temp_stylename );
700 p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
701 p_style->font_style.i_font_size = i_font_size;
702 msg_Dbg( p_dec, psz_temp_color1 );
703 ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, &p_style->font_style.i_font_alpha );
704 ParseColor( p_dec, psz_temp_color3, &p_style->font_style.i_outline_color, &p_style->font_style.i_outline_alpha );
705 ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, &p_style->font_style.i_shadow_alpha );
707 p_style->font_style.i_style_flags = 0;
708 if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
709 if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
710 if( i_underline ) p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
711 if( i_strikeout ) p_style->font_style.i_style_flags |= STYLE_STRIKEOUT;
712 if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
713 else if( i_border == 3 )
715 p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
716 p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
717 p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
719 p_style->font_style.i_shadow_width = ( i_border == 1 ) ? i_shadow : 0;
720 p_style->font_style.i_outline_width = ( i_border == 1 ) ? i_outline : 0;
721 p_style->font_style.i_spacing = i_spacing;
722 //p_style->font_style.f_angle = f_angle;
724 p_style->i_align = 0;
725 if( i_align == 0x1 || i_align == 0x4 || i_align == 0x1 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
726 if( i_align == 0x3 || i_align == 0x6 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
727 if( i_align == 0x7 || i_align == 0x8 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP;
728 if( i_align == 0x1 || i_align == 0x2 || i_align == 0x3 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
729 p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
730 p_style->i_margin_v = i_margin_v;
732 /*TODO: Ignored: angle i_scale_x|y (fontscaling), i_encoding */
733 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
735 else msg_Dbg( p_dec, "SSA V4+ styleline parsing failed" );
738 psz_parser = GotoNextLine( psz_parser );
742 if( psz_header ) free( psz_header );
746 static void StripTags( char *psz_text )
748 int i_left_moves = 0;
749 vlc_bool_t b_inside_tag = VLC_FALSE;
751 int i_tag_start = -1;
752 while( psz_text[ i ] )
756 if( psz_text[ i ] == '<' )
758 b_inside_tag = VLC_TRUE;
761 psz_text[ i - i_left_moves ] = psz_text[ i ];
765 if( ( psz_text[ i ] == ' ' ) ||
766 ( psz_text[ i ] == '\t' ) ||
767 ( psz_text[ i ] == '\n' ) ||
768 ( psz_text[ i ] == '\r' ) )
770 b_inside_tag = VLC_FALSE;
773 else if( psz_text[ i ] == '>' )
775 i_left_moves += i - i_tag_start + 1;
777 b_inside_tag = VLC_FALSE;
781 psz_text[ i - i_left_moves ] = psz_text[ i ];
786 psz_text[ i - i_left_moves ] = '\0';