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/decoder.h>
34 #include "vlc_filter.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 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 static int pi_justification[] = { 0, 1, 2 };
104 static char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
106 #define ENCODING_TEXT N_("Subtitles text encoding")
107 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
108 #define ALIGN_TEXT N_("Subtitles justification")
109 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
110 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
111 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
112 "UTF-8 encoding within subtitles files.")
113 #define FORMAT_TEXT N_("Formatted Subtitles")
114 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
115 "VLC partly implements this, but you can choose to disable all formatting.")
119 set_shortname( _("Subtitles"));
120 set_description( _("Text subtitles decoder") );
121 set_capability( "decoder", 50 );
122 set_callbacks( OpenDecoder, CloseDecoder );
123 set_category( CAT_INPUT );
124 set_subcategory( SUBCAT_INPUT_SCODEC );
126 add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
128 change_integer_list( pi_justification, ppsz_justification_text, 0 );
129 add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
130 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
131 change_string_list( ppsz_encodings, 0, 0 );
132 add_bool( "subsdec-autodetect-utf8", VLC_TRUE, NULL,
133 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, VLC_FALSE );
134 add_bool( "subsdec-formatted", VLC_TRUE, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
138 /*****************************************************************************
139 * OpenDecoder: probe the decoder and return score
140 *****************************************************************************
141 * Tries to launch a decoder and return score so that the interface is able
143 *****************************************************************************/
144 static int OpenDecoder( vlc_object_t *p_this )
146 decoder_t *p_dec = (decoder_t*)p_this;
147 decoder_sys_t *p_sys;
150 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
151 p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
156 p_dec->pf_decode_sub = DecodeBlock;
158 /* Allocate the memory needed to store the decoder's structure */
159 if( ( p_dec->p_sys = p_sys =
160 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
162 msg_Err( p_dec, "out of memory" );
168 p_sys->iconv_handle = (vlc_iconv_t)-1;
169 p_sys->b_autodetect_utf8 = VLC_FALSE;
170 p_sys->b_ass = VLC_FALSE;
171 p_sys->i_original_height = -1;
172 p_sys->i_original_width = -1;
173 p_sys->pp_ssa_styles = NULL;
174 p_sys->i_ssa_styles = 0;
176 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
178 msg_Dbg( p_dec, "using character encoding: %s",
179 p_dec->fmt_in.subs.psz_encoding );
180 if( strcmp( p_dec->fmt_in.subs.psz_encoding, "UTF-8" ) )
181 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", p_dec->fmt_in.subs.psz_encoding );
185 var_Create( p_dec, "subsdec-encoding",
186 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
187 var_Get( p_dec, "subsdec-encoding", &val );
188 if( !strcmp( val.psz_string, DEFAULT_NAME ) )
190 const char *psz_charset = GetFallbackEncoding();
192 p_sys->b_autodetect_utf8 = var_CreateGetBool( p_dec,
193 "subsdec-autodetect-utf8" );
195 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", psz_charset );
196 msg_Dbg( p_dec, "using default character encoding: %s", psz_charset );
198 else if( !strcmp( val.psz_string, "UTF-8" ) )
200 msg_Dbg( p_dec, "using character encoding: UTF-8" );
202 else if( val.psz_string )
204 msg_Dbg( p_dec, "using character encoding: %s", val.psz_string );
205 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", val.psz_string );
206 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
208 msg_Warn( p_dec, "unable to do requested conversion" );
211 if( val.psz_string ) free( val.psz_string );
214 var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
215 var_Get( p_dec, "subsdec-align", &val );
216 p_sys->i_align = val.i_int;
218 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
220 if( p_dec->fmt_in.i_extra > 0 )
221 ParseSSAHeader( p_dec );
227 /****************************************************************************
228 * DecodeBlock: the whole thing
229 ****************************************************************************
230 * This function must be fed with complete subtitles units.
231 ****************************************************************************/
232 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
234 subpicture_t *p_spu = NULL;
236 if( !pp_block || *pp_block == NULL ) return NULL;
238 p_spu = ParseText( p_dec, *pp_block );
240 block_Release( *pp_block );
246 /*****************************************************************************
247 * CloseDecoder: clean up the decoder
248 *****************************************************************************/
249 static void CloseDecoder( vlc_object_t *p_this )
251 decoder_t *p_dec = (decoder_t *)p_this;
252 decoder_sys_t *p_sys = p_dec->p_sys;
254 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
256 vlc_iconv_close( p_sys->iconv_handle );
259 if( p_sys->pp_ssa_styles )
262 for( i = 0; i < p_sys->i_ssa_styles; i++ )
264 if( p_sys->pp_ssa_styles[i]->psz_stylename ) free( p_sys->pp_ssa_styles[i]->psz_stylename );
265 p_sys->pp_ssa_styles[i]->psz_stylename = NULL;
266 if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname ) free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
267 p_sys->pp_ssa_styles[i]->font_style.psz_fontname = NULL;
268 if( p_sys->pp_ssa_styles[i] ) free( p_sys->pp_ssa_styles[i] ); p_sys->pp_ssa_styles[i] = NULL;
270 free( p_sys->pp_ssa_styles ); p_sys->pp_ssa_styles = NULL;
276 /*****************************************************************************
277 * ParseText: parse an text subtitle packet and send it to the video output
278 *****************************************************************************/
279 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
281 decoder_sys_t *p_sys = p_dec->p_sys;
282 subpicture_t *p_spu = NULL;
283 char *psz_subtitle = NULL;
286 /* We cannot display a subpicture with no date */
287 if( p_block->i_pts == 0 )
289 msg_Warn( p_dec, "subtitle without a date" );
293 /* Check validity of packet data */
294 if( p_block->i_buffer <= 1 || p_block->p_buffer[0] == '\0' )
296 msg_Warn( p_dec, "empty subtitle" );
300 /* Should be resiliant against bad subtitles */
301 psz_subtitle = strndup( (const char *)p_block->p_buffer,
303 if( psz_subtitle == NULL )
306 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
307 EnsureUTF8( psz_subtitle );
311 if( p_sys->b_autodetect_utf8 )
313 if( IsUTF8( psz_subtitle ) == NULL )
315 msg_Dbg( p_dec, "Invalid UTF-8 sequence: "
316 "disabling UTF-8 subtitles autodetection" );
317 p_sys->b_autodetect_utf8 = VLC_FALSE;
321 if( !p_sys->b_autodetect_utf8 )
323 size_t inbytes_left = strlen( psz_subtitle );
324 size_t outbytes_left = 6 * inbytes_left;
325 char *psz_new_subtitle = malloc( outbytes_left + 1 );
326 char *psz_convert_buffer_out = psz_new_subtitle;
327 const char *psz_convert_buffer_in = psz_subtitle;
329 size_t ret = vlc_iconv( p_sys->iconv_handle,
330 &psz_convert_buffer_in, &inbytes_left,
331 &psz_convert_buffer_out, &outbytes_left );
333 *psz_convert_buffer_out++ = '\0';
334 free( psz_subtitle );
336 if( ( ret == (size_t)(-1) ) || inbytes_left )
338 free( psz_new_subtitle );
339 msg_Err( p_dec, _("Failed to convert subtitle encoding.\n"
340 "Try manually setting a character-encoding "
341 "before you open the file.") );
345 psz_subtitle = realloc( psz_new_subtitle,
346 psz_convert_buffer_out - psz_new_subtitle );
350 /* Create the subpicture unit */
351 p_spu = p_dec->pf_spu_buffer_new( p_dec );
354 msg_Warn( p_dec, "can't get spu buffer" );
355 if( psz_subtitle ) free( psz_subtitle );
359 /* Create a new subpicture region */
360 memset( &fmt, 0, sizeof(video_format_t) );
361 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
363 fmt.i_width = fmt.i_height = 0;
364 fmt.i_x_offset = fmt.i_y_offset = 0;
365 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
366 if( !p_spu->p_region )
368 msg_Err( p_dec, "cannot allocate SPU region" );
369 if( psz_subtitle ) free( psz_subtitle );
370 p_dec->pf_spu_buffer_del( p_dec, p_spu );
374 /* Decode and format the subpicture unit */
375 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
377 /* Normal text subs, easy markup */
378 p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
379 p_spu->i_x = p_sys->i_align ? 20 : 0;
382 /* Remove formatting from string */
383 StripTags( psz_subtitle );
385 p_spu->p_region->psz_text = psz_subtitle;
386 p_spu->i_start = p_block->i_pts;
387 p_spu->i_stop = p_block->i_pts + p_block->i_length;
388 p_spu->b_ephemer = (p_block->i_length == 0);
389 p_spu->b_absolute = VLC_FALSE;
393 /* Decode SSA strings */
394 ParseSSAString( p_dec, psz_subtitle, p_spu );
395 p_spu->i_start = p_block->i_pts;
396 p_spu->i_stop = p_block->i_pts + p_block->i_length;
397 p_spu->b_ephemer = (p_block->i_length == 0);
398 p_spu->b_absolute = VLC_FALSE;
399 p_spu->i_original_picture_width = p_sys->i_original_width;
400 p_spu->i_original_picture_height = p_sys->i_original_height;
401 if( psz_subtitle ) free( psz_subtitle );
406 static void ParseSSAString( decoder_t *p_dec, char *psz_subtitle, subpicture_t *p_spu_in )
408 /* We expect MKV formatted SSA:
409 * ReadOrder, Layer, Style, CharacterName, MarginL, MarginR,
410 * MarginV, Effect, Text */
411 decoder_sys_t *p_sys = p_dec->p_sys;
412 subpicture_t *p_spu = p_spu_in;
413 ssa_style_t *p_style = NULL;
414 char *psz_new_subtitle = NULL;
415 char *psz_buffer_sub = NULL;
416 char *psz_style = NULL;
417 char *psz_style_start = NULL;
418 char *psz_style_end = NULL;
419 int i_text = 0, i_comma = 0, i_strlen = 0, i;
420 int i_margin_l = 0, i_margin_r = 0, i_margin_v = 0;
422 psz_buffer_sub = psz_subtitle;
425 while( i_comma < 8 && *psz_buffer_sub != '\0' )
427 if( *psz_buffer_sub == ',' )
430 if( i_comma == 2 ) psz_style_start = &psz_buffer_sub[1];
431 if( i_comma == 3 ) psz_style_end = &psz_buffer_sub[0];
432 if( i_comma == 4 ) i_margin_l = (int)strtol( psz_buffer_sub+1, NULL, 10 );
433 if( i_comma == 5 ) i_margin_r = (int)strtol( psz_buffer_sub+1, NULL, 10 );
434 if( i_comma == 6 ) i_margin_v = (int)strtol( psz_buffer_sub+1, NULL, 10 );
439 if( *psz_buffer_sub == '\0' && i_comma == 8 )
441 msg_Dbg( p_dec, "couldn't find all fields in this SSA line" );
445 psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
447 while( psz_buffer_sub[0] != '\0' )
449 if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'n' )
451 psz_new_subtitle[i_text] = ' ';
455 else if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'N' )
457 psz_new_subtitle[i_text] = '\n';
461 else if( psz_buffer_sub[0] == '{' &&
462 psz_buffer_sub[1] == '\\' )
464 /* SSA control code */
465 while( psz_buffer_sub[0] != '\0' &&
466 psz_buffer_sub[0] != '}' )
474 psz_new_subtitle[i_text] = psz_buffer_sub[0];
479 psz_new_subtitle[i_text] = '\0';
481 i_strlen = __MAX( psz_style_end - psz_style_start, 0);
482 psz_style = (char *)malloc( i_strlen + 1);
483 psz_style = memcpy( psz_style, psz_style_start, i_strlen );
484 psz_style[i_strlen] = '\0';
486 for( i = 0; i < p_sys->i_ssa_styles; i++ )
488 if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
489 p_style = p_sys->pp_ssa_styles[i];
491 if( psz_style ) free( psz_style );
493 p_spu->p_region->psz_text = psz_new_subtitle;
494 if( p_style == NULL )
496 p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
497 p_spu->i_x = p_sys->i_align ? 20 : 0;
502 msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename);
503 p_spu->p_region->p_style = &p_style->font_style;
504 p_spu->i_flags = p_style->i_align;
505 if( p_style->i_align & SUBPICTURE_ALIGN_LEFT )
507 p_spu->i_x = (i_margin_l) ? i_margin_l : p_style->i_margin_h;
509 else if( p_style->i_align & SUBPICTURE_ALIGN_RIGHT )
511 p_spu->i_x = (i_margin_r) ? i_margin_r : p_style->i_margin_h;
513 p_spu->i_y = (i_margin_v) ? i_margin_v : p_style->i_margin_v;
517 static char* GotoNextLine( char *psz_text )
519 char *p_newline = psz_text;
521 while( p_newline[0] != '\0' )
523 if( p_newline[0] == '\n' || p_newline[0] == '\r' )
526 while( p_newline[0] == '\n' || p_newline[0] == '\r' )
535 /*****************************************************************************
536 * ParseColor: SSA stores color in BBGGRR, in ASS it uses AABBGGRR
537 * The string value in the string can be a pure integer, or hexadecimal &HBBGGRR
538 *****************************************************************************/
539 static void ParseColor( decoder_t *p_dec, char *psz_color, int *pi_color, int *pi_alpha )
542 if( !strncasecmp( psz_color, "&H", 2 ) )
544 /* textual HEX representation */
545 i_color = (int) strtol( psz_color+2, NULL, 16 );
547 else i_color = (int) strtol( psz_color, NULL, 0 );
550 *pi_color |= ( ( i_color & 0x000000FF ) << 16 ); /* Red */
551 *pi_color |= ( ( i_color & 0x0000FF00 ) ); /* Green */
552 *pi_color |= ( ( i_color & 0x00FF0000 ) >> 16 ); /* Blue */
554 if( pi_alpha != NULL )
555 *pi_alpha = ( i_color & 0xFF000000 ) >> 24;
558 /*****************************************************************************
559 * ParseSSAHeader: Retrieve global formatting information etc
560 *****************************************************************************/
561 static void ParseSSAHeader( decoder_t *p_dec )
563 decoder_sys_t *p_sys = p_dec->p_sys;
564 char *psz_parser = NULL;
565 char *psz_header = malloc( p_dec->fmt_in.i_extra+1 );
566 int i_section_type = 1;
568 memcpy( psz_header, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
569 psz_header[ p_dec->fmt_in.i_extra] = '\0';
571 /* Handle [Script Info] section */
572 psz_parser = strcasestr( psz_header, "[Script Info]" );
573 if( psz_parser == NULL ) goto eof;
575 psz_parser = GotoNextLine( psz_parser );
577 while( psz_parser[0] != '\0' )
580 char buffer_text[MAX_LINE + 1];
582 if( psz_parser[0] == '!' || psz_parser[0] == ';' ) /* comment */;
583 else if( sscanf( psz_parser, "PlayResX: %d", &temp ) == 1 )
584 p_sys->i_original_width = ( temp > 0 ) ? temp : -1;
585 else if( sscanf( psz_parser, "PlayResY: %d", &temp ) == 1 )
586 p_sys->i_original_height = ( temp > 0 ) ? temp : -1;
587 else if( sscanf( psz_parser, "Script Type: %8192s", buffer_text ) == 1 )
589 if( !strcasecmp( buffer_text, "V4.00+" ) ) p_sys->b_ass = VLC_TRUE;
591 else if( !strncasecmp( psz_parser, "[V4 Styles]", 11 ) )
593 else if( !strncasecmp( psz_parser, "[V4+ Styles]", 12) )
596 p_sys->b_ass = VLC_TRUE;
598 else if( !strncasecmp( psz_parser, "[Events]", 8 ) )
600 else if( !strncasecmp( psz_parser, "Style:", 6 ) )
602 int i_font_size, i_bold, i_italic, i_border, i_outline, i_shadow, i_underline,
603 i_strikeout, i_scale_x, i_scale_y, i_spacing, i_align, i_margin_l, i_margin_r, i_margin_v;
605 char psz_temp_stylename[MAX_LINE+1];
606 char psz_temp_fontname[MAX_LINE+1];
607 char psz_temp_color1[MAX_LINE+1];
608 char psz_temp_color2[MAX_LINE+1];
609 char psz_temp_color3[MAX_LINE+1];
610 char psz_temp_color4[MAX_LINE+1];
612 if( i_section_type == 1 ) /* V4 */
614 if( sscanf( psz_parser, "Style: %8192[^,],%8192[^,],%d,%8192[^,],%8192[^,],%8192[^,],%8192[^,],%d,%d,%d,%d,%d,%d,%d,%d,%d%*[^\r\n]",
615 psz_temp_stylename, psz_temp_fontname, &i_font_size,
616 psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
617 &i_border, &i_outline, &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 16 )
619 ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
621 p_style->psz_stylename = strdup( psz_temp_stylename );
622 p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
623 p_style->font_style.i_font_size = i_font_size;
625 ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, NULL );
626 ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, NULL );
627 p_style->font_style.i_outline_color = p_style->font_style.i_shadow_color;
628 p_style->font_style.i_font_alpha = p_style->font_style.i_outline_alpha = p_style->font_style.i_shadow_alpha = 0x00;
629 p_style->font_style.i_style_flags = 0;
630 if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
631 if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
633 if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
634 else if( i_border == 3 )
636 p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
637 p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
638 p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
640 p_style->font_style.i_shadow_width = i_shadow;
641 p_style->font_style.i_outline_width = i_outline;
643 p_style->i_align = 0;
644 if( i_align == 1 || i_align == 5 || i_align == 9 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
645 if( i_align == 3 || i_align == 7 || i_align == 11 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
646 if( i_align < 4 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
647 else if( i_align < 8 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP;
649 p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
650 p_style->i_margin_v = i_margin_v;
652 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
654 else msg_Warn( p_dec, "SSA v4 styleline parsing failed" );
656 else if( i_section_type == 2 ) /* V4+ */
658 /* Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour,
659 Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline,
660 Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
662 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]",
663 psz_temp_stylename, psz_temp_fontname, &i_font_size,
664 psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
665 &i_underline, &i_strikeout, &i_scale_x, &i_scale_y, &i_spacing, &i_border, &i_outline,
666 &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 21 )
668 ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
670 p_style->psz_stylename = strdup( psz_temp_stylename );
671 p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
672 p_style->font_style.i_font_size = i_font_size;
673 msg_Dbg( p_dec, psz_temp_color1 );
674 ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, &p_style->font_style.i_font_alpha );
675 ParseColor( p_dec, psz_temp_color3, &p_style->font_style.i_outline_color, &p_style->font_style.i_outline_alpha );
676 ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, &p_style->font_style.i_shadow_alpha );
678 p_style->font_style.i_style_flags = 0;
679 if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
680 if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
681 if( i_underline ) p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
682 if( i_strikeout ) p_style->font_style.i_style_flags |= STYLE_STRIKEOUT;
683 if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
684 else if( i_border == 3 )
686 p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
687 p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
688 p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
690 p_style->font_style.i_shadow_width = ( i_border == 1 ) ? i_shadow : 0;
691 p_style->font_style.i_outline_width = ( i_border == 1 ) ? i_outline : 0;
692 p_style->font_style.i_spacing = i_spacing;
693 //p_style->font_style.f_angle = f_angle;
695 p_style->i_align = 0;
696 if( i_align == 0x1 || i_align == 0x4 || i_align == 0x1 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
697 if( i_align == 0x3 || i_align == 0x6 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
698 if( i_align == 0x7 || i_align == 0x8 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP;
699 if( i_align == 0x1 || i_align == 0x2 || i_align == 0x3 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
700 p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
701 p_style->i_margin_v = i_margin_v;
703 /*TODO: Ignored: angle i_scale_x|y (fontscaling), i_encoding */
704 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
706 else msg_Dbg( p_dec, "SSA V4+ styleline parsing failed" );
709 psz_parser = GotoNextLine( psz_parser );
713 if( psz_header ) free( psz_header );
717 static void StripTags( char *psz_text )
719 int i_left_moves = 0;
720 vlc_bool_t b_inside_tag = VLC_FALSE;
722 int i_tag_start = -1;
723 while( psz_text[ i ] )
727 if( psz_text[ i ] == '<' )
729 b_inside_tag = VLC_TRUE;
732 psz_text[ i - i_left_moves ] = psz_text[ i ];
736 if( ( psz_text[ i ] == ' ' ) ||
737 ( psz_text[ i ] == '\t' ) ||
738 ( psz_text[ i ] == '\n' ) ||
739 ( psz_text[ i ] == '\r' ) )
741 b_inside_tag = VLC_FALSE;
744 else if( psz_text[ i ] == '>' )
746 i_left_moves += i - i_tag_start + 1;
748 b_inside_tag = VLC_FALSE;
752 psz_text[ i - i_left_moves ] = psz_text[ i ];
757 psz_text[ i - i_left_moves ] = '\0';