1 /*****************************************************************************
2 * subsdec.c : text subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2001 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 */
57 ssa_style_t **pp_ssa_styles;
61 /*****************************************************************************
63 *****************************************************************************/
64 static int OpenDecoder ( vlc_object_t * );
65 static void CloseDecoder ( vlc_object_t * );
67 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
68 static subpicture_t *ParseText ( decoder_t *, block_t * );
69 static void ParseSSAHeader ( decoder_t * );
70 static void ParseSSAString ( decoder_t *, char *, subpicture_t * );
71 static void ParseColor ( decoder_t *, char *, int *, int * );
72 static void StripTags ( char * );
74 #define DEFAULT_NAME "Default"
77 /*****************************************************************************
79 *****************************************************************************/
80 static char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
81 "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
82 "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
83 "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
84 "ISO-8859-6", "CP1256", "MacArabic", "",
85 "ISO-8859-7", "CP1253", "MacGreek", "",
86 "ISO-8859-8", "CP1255", "MacHebrew", "",
87 "ISO-8859-9", "CP1254", "MacTurkish", "",
88 "ISO-8859-13", "CP1257", "",
89 "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
90 "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
91 "ISO-2022-KR", "EUC-KR", "",
92 "MacThai", "KOI8-T", "",
93 "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
94 "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
96 "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
97 "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
98 "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
99 "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
100 "HPROMAN8", "NEXTSTEP" };
102 static int pi_justification[] = { 0, 1, 2 };
103 static char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
105 #define ENCODING_TEXT N_("Subtitles text encoding")
106 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
107 #define ALIGN_TEXT N_("Subtitles justification")
108 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
109 #define FORMAT_TEXT N_("Formatted Subtitles")
110 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting.\
111 VLC partly implements this, but you can choose to disable all formatting.")
115 set_shortname( _("Subtitles"));
116 set_description( _("Text subtitles decoder") );
117 set_capability( "decoder", 50 );
118 set_callbacks( OpenDecoder, CloseDecoder );
119 set_category( CAT_INPUT );
120 set_subcategory( SUBCAT_INPUT_SCODEC );
122 add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
124 change_integer_list( pi_justification, ppsz_justification_text, 0 );
125 add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
126 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
127 change_string_list( ppsz_encodings, 0, 0 );
128 add_bool( "subsdec-formatted", VLC_TRUE, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
132 /*****************************************************************************
133 * OpenDecoder: probe the decoder and return score
134 *****************************************************************************
135 * Tries to launch a decoder and return score so that the interface is able
137 *****************************************************************************/
138 static int OpenDecoder( vlc_object_t *p_this )
140 decoder_t *p_dec = (decoder_t*)p_this;
141 decoder_sys_t *p_sys;
144 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
145 p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
150 p_dec->pf_decode_sub = DecodeBlock;
152 /* Allocate the memory needed to store the decoder's structure */
153 if( ( p_dec->p_sys = p_sys =
154 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
156 msg_Err( p_dec, "out of memory" );
162 p_sys->iconv_handle = (vlc_iconv_t)-1;
163 p_sys->b_ass = VLC_FALSE;
164 p_sys->i_original_height = -1;
165 p_sys->i_original_width = -1;
166 p_sys->pp_ssa_styles = NULL;
167 p_sys->i_ssa_styles = 0;
169 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
171 msg_Dbg( p_dec, "using character encoding: %s",
172 p_dec->fmt_in.subs.psz_encoding );
173 if( strcmp( p_dec->fmt_in.subs.psz_encoding, "UTF-8" ) )
174 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", p_dec->fmt_in.subs.psz_encoding );
178 var_Create( p_dec, "subsdec-encoding",
179 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
180 var_Get( p_dec, "subsdec-encoding", &val );
181 if( !strcmp( val.psz_string, DEFAULT_NAME ) )
185 /* Most subtitles are not in UTF-8, which is the default on Mac OS X */
186 psz_charset = strdup( "ISO-8859-1" );
188 vlc_current_charset( &psz_charset );
190 if( psz_charset == NULL )
196 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", psz_charset );
197 msg_Dbg( p_dec, "using default character encoding: %s", psz_charset );
200 else if( !strcmp( val.psz_string, "UTF-8" ) )
202 msg_Dbg( p_dec, "using character encoding: UTF-8" );
204 else if( val.psz_string )
206 msg_Dbg( p_dec, "using character encoding: %s", val.psz_string );
207 p_sys->iconv_handle = vlc_iconv_open( "UTF-8", val.psz_string );
208 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
210 msg_Warn( p_dec, "unable to do requested conversion" );
213 if( val.psz_string ) free( val.psz_string );
216 var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
217 var_Get( p_dec, "subsdec-align", &val );
218 p_sys->i_align = val.i_int;
220 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
222 if( p_dec->fmt_in.i_extra > 0 )
223 ParseSSAHeader( p_dec );
229 /****************************************************************************
230 * DecodeBlock: the whole thing
231 ****************************************************************************
232 * This function must be fed with complete subtitles units.
233 ****************************************************************************/
234 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
236 subpicture_t *p_spu = NULL;
238 if( !pp_block || *pp_block == NULL ) return NULL;
240 p_spu = ParseText( p_dec, *pp_block );
242 block_Release( *pp_block );
248 /*****************************************************************************
249 * CloseDecoder: clean up the decoder
250 *****************************************************************************/
251 static void CloseDecoder( vlc_object_t *p_this )
253 decoder_t *p_dec = (decoder_t *)p_this;
254 decoder_sys_t *p_sys = p_dec->p_sys;
256 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
258 vlc_iconv_close( p_sys->iconv_handle );
261 if( p_sys->pp_ssa_styles )
264 for( i = 0; i < p_sys->i_ssa_styles; i++ )
266 if( p_sys->pp_ssa_styles[i]->psz_stylename ) free( p_sys->pp_ssa_styles[i]->psz_stylename );
267 p_sys->pp_ssa_styles[i]->psz_stylename = NULL;
268 if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname ) free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
269 p_sys->pp_ssa_styles[i]->font_style.psz_fontname = NULL;
270 if( p_sys->pp_ssa_styles[i] ) free( p_sys->pp_ssa_styles[i] ); p_sys->pp_ssa_styles[i] = NULL;
272 free( p_sys->pp_ssa_styles ); p_sys->pp_ssa_styles = NULL;
278 /*****************************************************************************
279 * ParseText: parse an text subtitle packet and send it to the video output
280 *****************************************************************************/
281 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
283 decoder_sys_t *p_sys = p_dec->p_sys;
284 subpicture_t *p_spu = NULL;
285 char *psz_subtitle = NULL;
288 /* We cannot display a subpicture with no date */
289 if( p_block->i_pts == 0 )
291 msg_Warn( p_dec, "subtitle without a date" );
295 /* Check validity of packet data */
296 if( p_block->i_buffer <= 1 || p_block->p_buffer[0] == '\0' )
298 msg_Warn( p_dec, "empty subtitle" );
302 /* Should be resiliant against bad subtitles */
303 psz_subtitle = strndup( (const char *)p_block->p_buffer,
306 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
308 char *psz_new_subtitle;
309 char *psz_convert_buffer_out;
310 char *psz_convert_buffer_in;
311 size_t ret, inbytes_left, outbytes_left;
313 psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) );
314 psz_convert_buffer_out = psz_new_subtitle;
315 psz_convert_buffer_in = psz_subtitle;
316 inbytes_left = strlen( psz_subtitle );
317 outbytes_left = 6 * inbytes_left;
318 ret = vlc_iconv( p_sys->iconv_handle, &psz_convert_buffer_in,
319 &inbytes_left, &psz_convert_buffer_out,
321 *psz_convert_buffer_out = '\0';
323 if( psz_subtitle ) free( psz_subtitle );
328 msg_Err( p_dec, _("Failed to convert subtitle encoding.\n"
329 "Try manually setting a character-encoding "
330 "before you open the file.") );
333 psz_subtitle = psz_new_subtitle;
336 /* Create the subpicture unit */
337 p_spu = p_dec->pf_spu_buffer_new( p_dec );
340 msg_Warn( p_dec, "can't get spu buffer" );
341 if( psz_subtitle ) free( psz_subtitle );
345 /* Create a new subpicture region */
346 memset( &fmt, 0, sizeof(video_format_t) );
347 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
349 fmt.i_width = fmt.i_height = 0;
350 fmt.i_x_offset = fmt.i_y_offset = 0;
351 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
352 if( !p_spu->p_region )
354 msg_Err( p_dec, "cannot allocate SPU region" );
355 if( psz_subtitle ) free( psz_subtitle );
356 p_dec->pf_spu_buffer_del( p_dec, p_spu );
360 /* Decode and format the subpicture unit */
361 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
363 /* Normal text subs, easy markup */
364 p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
365 p_spu->i_x = p_sys->i_align ? 20 : 0;
368 /* Remove formatting from string */
369 StripTags( psz_subtitle );
371 p_spu->p_region->psz_text = psz_subtitle;
372 p_spu->i_start = p_block->i_pts;
373 p_spu->i_stop = p_block->i_pts + p_block->i_length;
374 p_spu->b_ephemer = (p_block->i_length == 0);
375 p_spu->b_absolute = VLC_FALSE;
379 /* Decode SSA strings */
380 ParseSSAString( p_dec, psz_subtitle, p_spu );
381 p_spu->i_start = p_block->i_pts;
382 p_spu->i_stop = p_block->i_pts + p_block->i_length;
383 p_spu->b_ephemer = (p_block->i_length == 0);
384 p_spu->b_absolute = VLC_FALSE;
385 p_spu->i_original_picture_width = p_sys->i_original_width;
386 p_spu->i_original_picture_height = p_sys->i_original_height;
387 if( psz_subtitle ) free( psz_subtitle );
392 static void ParseSSAString( decoder_t *p_dec, char *psz_subtitle, subpicture_t *p_spu_in )
394 /* We expect MKV formatted SSA:
395 * ReadOrder, Layer, Style, CharacterName, MarginL, MarginR,
396 * MarginV, Effect, Text */
397 decoder_sys_t *p_sys = p_dec->p_sys;
398 subpicture_t *p_spu = p_spu_in;
399 ssa_style_t *p_style = NULL;
400 char *psz_new_subtitle = NULL;
401 char *psz_buffer_sub = NULL;
402 char *psz_style = NULL;
403 char *psz_style_start = NULL;
404 char *psz_style_end = NULL;
405 int i_text = 0, i_comma = 0, i_strlen = 0, i;
406 int i_margin_l = 0, i_margin_r = 0, i_margin_v = 0;
408 psz_buffer_sub = psz_subtitle;
411 while( i_comma < 8 && *psz_buffer_sub != '\0' )
413 if( *psz_buffer_sub == ',' )
416 if( i_comma == 2 ) psz_style_start = &psz_buffer_sub[1];
417 if( i_comma == 3 ) psz_style_end = &psz_buffer_sub[0];
418 if( i_comma == 4 ) i_margin_l = (int)strtol( psz_buffer_sub+1, NULL, 10 );
419 if( i_comma == 5 ) i_margin_r = (int)strtol( psz_buffer_sub+1, NULL, 10 );
420 if( i_comma == 6 ) i_margin_v = (int)strtol( psz_buffer_sub+1, NULL, 10 );
425 if( *psz_buffer_sub == '\0' && i_comma == 8 )
427 msg_Dbg( p_dec, "couldn't find all fields in this SSA line" );
431 psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
433 while( psz_buffer_sub[0] != '\0' )
435 if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'n' )
437 psz_new_subtitle[i_text] = ' ';
441 else if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'N' )
443 psz_new_subtitle[i_text] = '\n';
447 else if( psz_buffer_sub[0] == '{' &&
448 psz_buffer_sub[1] == '\\' )
450 /* SSA control code */
451 while( psz_buffer_sub[0] != '\0' &&
452 psz_buffer_sub[0] != '}' )
460 psz_new_subtitle[i_text] = psz_buffer_sub[0];
465 psz_new_subtitle[i_text] = '\0';
467 i_strlen = __MAX( psz_style_end - psz_style_start, 0);
468 psz_style = (char *)malloc( i_strlen + 1);
469 psz_style = memcpy( psz_style, psz_style_start, i_strlen );
470 psz_style[i_strlen] = '\0';
472 for( i = 0; i < p_sys->i_ssa_styles; i++ )
474 if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
475 p_style = p_sys->pp_ssa_styles[i];
477 if( psz_style ) free( psz_style );
479 p_spu->p_region->psz_text = psz_new_subtitle;
480 if( p_style == NULL )
482 p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
483 p_spu->i_x = p_sys->i_align ? 20 : 0;
488 msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename);
489 p_spu->p_region->p_style = &p_style->font_style;
490 p_spu->i_flags = p_style->i_align;
491 if( p_style->i_align & SUBPICTURE_ALIGN_LEFT )
493 p_spu->i_x = (i_margin_l) ? i_margin_l : p_style->i_margin_h;
495 else if( p_style->i_align & SUBPICTURE_ALIGN_RIGHT )
497 p_spu->i_x = (i_margin_r) ? i_margin_r : p_style->i_margin_h;
499 p_spu->i_y = (i_margin_v) ? i_margin_v : p_style->i_margin_v;
503 static char* GotoNextLine( char *psz_text )
505 char *p_newline = psz_text;
507 while( p_newline[0] != '\0' )
509 if( p_newline[0] == '\n' || p_newline[0] == '\r' )
512 while( p_newline[0] == '\n' || p_newline[0] == '\r' )
521 /*****************************************************************************
522 * ParseColor: SSA stores color in BBGGRR, in ASS it uses AABBGGRR
523 * The string value in the string can be a pure integer, or hexadecimal &HBBGGRR
524 *****************************************************************************/
525 static void ParseColor( decoder_t *p_dec, char *psz_color, int *pi_color, int *pi_alpha )
528 if( !strncasecmp( psz_color, "&H", 2 ) )
530 /* textual HEX representation */
531 i_color = (int) strtol( psz_color+2, NULL, 16 );
533 else i_color = (int) strtol( psz_color, NULL, 0 );
536 *pi_color |= ( ( i_color & 0x000000FF ) << 16 ); /* Red */
537 *pi_color |= ( ( i_color & 0x0000FF00 ) ); /* Green */
538 *pi_color |= ( ( i_color & 0x00FF0000 ) >> 16 ); /* Blue */
540 if( pi_alpha != NULL )
541 *pi_alpha = ( i_color & 0xFF000000 ) >> 24;
544 /*****************************************************************************
545 * ParseSSAHeader: Retrieve global formatting information etc
546 *****************************************************************************/
547 static void ParseSSAHeader( decoder_t *p_dec )
549 decoder_sys_t *p_sys = p_dec->p_sys;
550 char *psz_parser = NULL;
551 char *psz_header = malloc( p_dec->fmt_in.i_extra+1 );
552 int i_section_type = 1;
554 memcpy( psz_header, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
555 psz_header[ p_dec->fmt_in.i_extra] = '\0';
557 /* Handle [Script Info] section */
558 psz_parser = strcasestr( psz_header, "[Script Info]" );
559 if( psz_parser == NULL ) goto eof;
561 psz_parser = GotoNextLine( psz_parser );
563 while( psz_parser[0] != '\0' )
566 char buffer_text[MAX_LINE + 1];
568 if( psz_parser[0] == '!' || psz_parser[0] == ';' ) /* comment */;
569 else if( sscanf( psz_parser, "PlayResX: %d", &temp ) == 1 )
570 p_sys->i_original_width = ( temp > 0 ) ? temp : -1;
571 else if( sscanf( psz_parser, "PlayResY: %d", &temp ) == 1 )
572 p_sys->i_original_height = ( temp > 0 ) ? temp : -1;
573 else if( sscanf( psz_parser, "Script Type: %8192s", buffer_text ) == 1 )
575 if( !strcasecmp( buffer_text, "V4.00+" ) ) p_sys->b_ass = VLC_TRUE;
577 else if( !strncasecmp( psz_parser, "[V4 Styles]", 11 ) )
579 else if( !strncasecmp( psz_parser, "[V4+ Styles]", 12) )
582 p_sys->b_ass = VLC_TRUE;
584 else if( !strncasecmp( psz_parser, "[Events]", 8 ) )
586 else if( !strncasecmp( psz_parser, "Style:", 6 ) )
588 int i_font_size, i_bold, i_italic, i_border, i_outline, i_shadow, i_underline,
589 i_strikeout, i_scale_x, i_scale_y, i_spacing, i_align, i_margin_l, i_margin_r, i_margin_v;
591 char psz_temp_stylename[MAX_LINE+1];
592 char psz_temp_fontname[MAX_LINE+1];
593 char psz_temp_color1[MAX_LINE+1];
594 char psz_temp_color2[MAX_LINE+1];
595 char psz_temp_color3[MAX_LINE+1];
596 char psz_temp_color4[MAX_LINE+1];
598 if( i_section_type == 1 ) /* V4 */
600 if( sscanf( psz_parser, "Style: %8192[^,],%8192[^,],%d,%8192[^,],%8192[^,],%8192[^,],%8192[^,],%d,%d,%d,%d,%d,%d,%d,%d,%d%*[^\r\n]",
601 psz_temp_stylename, psz_temp_fontname, &i_font_size,
602 psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
603 &i_border, &i_outline, &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 16 )
605 ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
607 p_style->psz_stylename = strdup( psz_temp_stylename );
608 p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
609 p_style->font_style.i_font_size = i_font_size;
611 ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, NULL );
612 ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, NULL );
613 p_style->font_style.i_outline_color = p_style->font_style.i_shadow_color;
614 p_style->font_style.i_font_alpha = p_style->font_style.i_outline_alpha = p_style->font_style.i_shadow_alpha = 0x00;
615 p_style->font_style.i_style_flags = 0;
616 if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
617 if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
619 if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
620 else if( i_border == 3 )
622 p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
623 p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
624 p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
626 p_style->font_style.i_shadow_width = i_shadow;
627 p_style->font_style.i_outline_width = i_outline;
629 p_style->i_align = 0;
630 if( i_align == 1 || i_align == 5 || i_align == 9 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
631 if( i_align == 3 || i_align == 7 || i_align == 11 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
632 if( i_align < 4 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
633 else if( i_align < 8 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP;
635 p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
636 p_style->i_margin_v = i_margin_v;
638 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
640 else msg_Warn( p_dec, "SSA v4 styleline parsing failed" );
642 else if( i_section_type == 2 ) /* V4+ */
644 /* Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour,
645 Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline,
646 Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
648 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]",
649 psz_temp_stylename, psz_temp_fontname, &i_font_size,
650 psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
651 &i_underline, &i_strikeout, &i_scale_x, &i_scale_y, &i_spacing, &i_border, &i_outline,
652 &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 21 )
654 ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
656 p_style->psz_stylename = strdup( psz_temp_stylename );
657 p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
658 p_style->font_style.i_font_size = i_font_size;
659 msg_Dbg( p_dec, psz_temp_color1 );
660 ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, &p_style->font_style.i_font_alpha );
661 ParseColor( p_dec, psz_temp_color3, &p_style->font_style.i_outline_color, &p_style->font_style.i_outline_alpha );
662 ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, &p_style->font_style.i_shadow_alpha );
664 p_style->font_style.i_style_flags = 0;
665 if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
666 if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
667 if( i_underline ) p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
668 if( i_strikeout ) p_style->font_style.i_style_flags |= STYLE_STRIKEOUT;
669 if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
670 else if( i_border == 3 )
672 p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
673 p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
674 p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
676 p_style->font_style.i_shadow_width = ( i_border == 1 ) ? i_shadow : 0;
677 p_style->font_style.i_outline_width = ( i_border == 1 ) ? i_outline : 0;
678 p_style->font_style.i_spacing = i_spacing;
679 //p_style->font_style.f_angle = f_angle;
681 p_style->i_align = 0;
682 if( i_align == 0x1 || i_align == 0x4 || i_align == 0x1 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
683 if( i_align == 0x3 || i_align == 0x6 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
684 if( i_align == 0x7 || i_align == 0x8 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP;
685 if( i_align == 0x1 || i_align == 0x2 || i_align == 0x3 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
686 p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
687 p_style->i_margin_v = i_margin_v;
689 /*TODO: Ignored: angle i_scale_x|y (fontscaling), i_encoding */
690 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
692 else msg_Dbg( p_dec, "SSA V4+ styleline parsing failed" );
695 psz_parser = GotoNextLine( psz_parser );
699 if( psz_header ) free( psz_header );
703 static void StripTags( char *psz_text )
705 int i_left_moves = 0;
706 vlc_bool_t b_inside_tag = VLC_FALSE;
708 int i_tag_start = -1;
709 while( psz_text[ i ] )
713 if( psz_text[ i ] == '<' )
715 b_inside_tag = VLC_TRUE;
718 psz_text[ i - i_left_moves ] = psz_text[ i ];
722 if( ( psz_text[ i ] == ' ' ) ||
723 ( psz_text[ i ] == '\t' ) ||
724 ( psz_text[ i ] == '\n' ) ||
725 ( psz_text[ i ] == '\r' ) )
727 b_inside_tag = VLC_FALSE;
730 else if( psz_text[ i ] == '>' )
732 i_left_moves += i - i_tag_start + 1;
734 b_inside_tag = VLC_FALSE;
738 psz_text[ i - i_left_moves ] = psz_text[ i ];
743 psz_text[ i - i_left_moves ] = '\0';