]> git.sesse.net Git - vlc/blob - modules/codec/subsdec.c
* Use run-time detection of UTF-8 as current charset instead of hard-coding to be...
[vlc] / modules / codec / subsdec.c
1 /*****************************************************************************
2  * subsdec.c : text subtitles decoder
3  *****************************************************************************
4  * Copyright (C) 2000-2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Derk-Jan Hartman <hartman at videolan dot org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31 #include <vlc/decoder.h>
32
33 #include "vlc_osd.h"
34 #include "vlc_filter.h"
35 #include "charset.h"
36
37 typedef struct
38 {
39     char *          psz_stylename; /* The name of the style, no comma's allowed */
40     text_style_t    font_style;
41     int             i_align;
42     int             i_margin_h;
43     int             i_margin_v;
44 }  ssa_style_t;
45
46 /*****************************************************************************
47  * decoder_sys_t : decoder descriptor
48  *****************************************************************************/
49 struct decoder_sys_t
50 {
51     vlc_bool_t          b_ass;                           /* The subs are ASS */
52     int                 i_original_height;
53     int                 i_original_width;
54     int                 i_align;          /* Subtitles alignment on the vout */
55     vlc_iconv_t         iconv_handle;            /* handle to iconv instance */
56
57     ssa_style_t         **pp_ssa_styles;
58     int                 i_ssa_styles;
59 };
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static int  OpenDecoder   ( vlc_object_t * );
65 static void CloseDecoder  ( vlc_object_t * );
66
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 * );
73
74 #define DEFAULT_NAME "Default"
75 #define MAX_LINE 8192
76
77 /*****************************************************************************
78  * Module descriptor.
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", "",
95     "Macintosh", "",
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" };
101
102 static int  pi_justification[] = { 0, 1, 2 };
103 static char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
104
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.")
112
113
114 vlc_module_begin();
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 );
121
122     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
123                  VLC_FALSE );
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,
129                  VLC_FALSE );
130 vlc_module_end();
131
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
136  * to chose.
137  *****************************************************************************/
138 static int OpenDecoder( vlc_object_t *p_this )
139 {
140     decoder_t     *p_dec = (decoder_t*)p_this;
141     decoder_sys_t *p_sys;
142     vlc_value_t    val;
143
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',' ') )
146     {
147         return VLC_EGENERIC;
148     }
149
150     p_dec->pf_decode_sub = DecodeBlock;
151
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 )
155     {
156         msg_Err( p_dec, "out of memory" );
157         return VLC_ENOMEM;
158     }
159
160     /* init of p_sys */
161     p_sys->i_align = 0;
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;
168
169     if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
170     {
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 );
175     }
176     else
177     {
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 ) )
182         {
183             char *psz_charset;
184
185             if( vlc_current_charset( &psz_charset ) )
186             {
187                 /*
188                  * Most subtitles are not in UTF-8.
189                  * FIXME: This is western-centric. We should use a fallback
190                  * charset depending on the locale language instead.
191                  */
192                 if( psz_charset != NULL)
193                     free( psz_charset );
194                 psz_charset = strdup( "CP1252" );
195             }
196
197             if( psz_charset == NULL )
198             {
199                 free( p_sys );
200                 return VLC_ENOMEM;
201             }
202
203             p_sys->iconv_handle = vlc_iconv_open( "UTF-8", psz_charset );  
204             msg_Dbg( p_dec, "using default character encoding: %s", psz_charset );  
205             free( psz_charset );
206         }
207         else if( !strcmp( val.psz_string, "UTF-8" ) )
208         {
209             msg_Dbg( p_dec, "using character encoding: UTF-8" );
210         }
211         else if( val.psz_string )
212         {
213             msg_Dbg( p_dec, "using character encoding: %s", val.psz_string );
214             p_sys->iconv_handle = vlc_iconv_open( "UTF-8", val.psz_string );
215             if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
216             {
217                 msg_Warn( p_dec, "unable to do requested conversion" );
218             }
219         }
220         if( val.psz_string ) free( val.psz_string );
221     }
222
223     var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
224     var_Get( p_dec, "subsdec-align", &val );
225     p_sys->i_align = val.i_int;
226
227     if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
228     {
229         if( p_dec->fmt_in.i_extra > 0 )
230             ParseSSAHeader( p_dec );
231     }
232
233     return VLC_SUCCESS;
234 }
235
236 /****************************************************************************
237  * DecodeBlock: the whole thing
238  ****************************************************************************
239  * This function must be fed with complete subtitles units.
240  ****************************************************************************/
241 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
242 {
243     subpicture_t *p_spu = NULL;
244
245     if( !pp_block || *pp_block == NULL ) return NULL;
246
247     p_spu = ParseText( p_dec, *pp_block );
248
249     block_Release( *pp_block );
250     *pp_block = NULL;
251
252     return p_spu;
253 }
254
255 /*****************************************************************************
256  * CloseDecoder: clean up the decoder
257  *****************************************************************************/
258 static void CloseDecoder( vlc_object_t *p_this )
259 {
260     decoder_t *p_dec = (decoder_t *)p_this;
261     decoder_sys_t *p_sys = p_dec->p_sys;
262
263     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
264     {
265         vlc_iconv_close( p_sys->iconv_handle );
266     }
267     
268     if( p_sys->pp_ssa_styles )
269     {
270         int i;
271         for( i = 0; i < p_sys->i_ssa_styles; i++ )
272         {
273             if( p_sys->pp_ssa_styles[i]->psz_stylename ) free( p_sys->pp_ssa_styles[i]->psz_stylename );
274             p_sys->pp_ssa_styles[i]->psz_stylename = NULL;
275             if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname ) free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
276             p_sys->pp_ssa_styles[i]->font_style.psz_fontname = NULL;
277             if( p_sys->pp_ssa_styles[i] ) free( p_sys->pp_ssa_styles[i] ); p_sys->pp_ssa_styles[i] = NULL;
278         }
279         free( p_sys->pp_ssa_styles ); p_sys->pp_ssa_styles = NULL;
280     }
281
282     free( p_sys );
283 }
284
285 /*****************************************************************************
286  * ParseText: parse an text subtitle packet and send it to the video output
287  *****************************************************************************/
288 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
289 {
290     decoder_sys_t *p_sys = p_dec->p_sys;
291     subpicture_t *p_spu = NULL;
292     char *psz_subtitle = NULL;
293     video_format_t fmt;
294
295     /* We cannot display a subpicture with no date */
296     if( p_block->i_pts == 0 )
297     {
298         msg_Warn( p_dec, "subtitle without a date" );
299         return NULL;
300     }
301
302     /* Check validity of packet data */
303     if( p_block->i_buffer <= 1 || p_block->p_buffer[0] == '\0' )
304     {
305         msg_Warn( p_dec, "empty subtitle" );
306         return NULL;
307     }
308
309     /* Should be resiliant against bad subtitles */
310     psz_subtitle = strndup( (const char *)p_block->p_buffer,
311                             p_block->i_buffer );
312
313     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
314     {
315         char *psz_new_subtitle;
316         char *psz_convert_buffer_out;
317         char *psz_convert_buffer_in;
318         size_t ret, inbytes_left, outbytes_left;
319
320         psz_new_subtitle = malloc( 6 * strlen( psz_subtitle ) );
321         psz_convert_buffer_out = psz_new_subtitle;
322         psz_convert_buffer_in = psz_subtitle;
323         inbytes_left = strlen( psz_subtitle );
324         outbytes_left = 6 * inbytes_left;
325         ret = vlc_iconv( p_sys->iconv_handle, &psz_convert_buffer_in,
326                          &inbytes_left, &psz_convert_buffer_out,
327                          &outbytes_left );
328         *psz_convert_buffer_out = '\0';
329
330         if( psz_subtitle ) free( psz_subtitle );
331         psz_subtitle = NULL;
332
333         if( inbytes_left )
334         {
335             msg_Err( p_dec, _("Failed to convert subtitle encoding.\n"
336                       "Try manually setting a character-encoding "
337                       "before you open the file.") );
338             return NULL;
339         }
340         psz_subtitle = psz_new_subtitle;
341     }
342
343     /* Create the subpicture unit */
344     p_spu = p_dec->pf_spu_buffer_new( p_dec );
345     if( !p_spu )
346     {
347         msg_Warn( p_dec, "can't get spu buffer" );
348         if( psz_subtitle ) free( psz_subtitle );
349         return NULL;
350     }
351
352     /* Create a new subpicture region */
353     memset( &fmt, 0, sizeof(video_format_t) );
354     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
355     fmt.i_aspect = 0;
356     fmt.i_width = fmt.i_height = 0;
357     fmt.i_x_offset = fmt.i_y_offset = 0;
358     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
359     if( !p_spu->p_region )
360     {
361         msg_Err( p_dec, "cannot allocate SPU region" );
362         if( psz_subtitle ) free( psz_subtitle );
363         p_dec->pf_spu_buffer_del( p_dec, p_spu );
364         return NULL;
365     }
366
367     /* Decode and format the subpicture unit */
368     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
369     {
370         /* Normal text subs, easy markup */
371         p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
372         p_spu->i_x = p_sys->i_align ? 20 : 0;
373         p_spu->i_y = 10;
374         
375         /* Remove formatting from string */
376         StripTags( psz_subtitle );
377         
378         p_spu->p_region->psz_text = psz_subtitle;        
379         p_spu->i_start = p_block->i_pts;
380         p_spu->i_stop = p_block->i_pts + p_block->i_length;
381         p_spu->b_ephemer = (p_block->i_length == 0);
382         p_spu->b_absolute = VLC_FALSE;
383     }
384     else
385     {
386         /* Decode SSA strings */
387         ParseSSAString( p_dec, psz_subtitle, p_spu );
388         p_spu->i_start = p_block->i_pts;
389         p_spu->i_stop = p_block->i_pts + p_block->i_length;
390         p_spu->b_ephemer = (p_block->i_length == 0);
391         p_spu->b_absolute = VLC_FALSE;
392         p_spu->i_original_picture_width = p_sys->i_original_width;
393         p_spu->i_original_picture_height = p_sys->i_original_height;
394         if( psz_subtitle ) free( psz_subtitle );
395     }
396     return p_spu;
397 }
398
399 static void ParseSSAString( decoder_t *p_dec, char *psz_subtitle, subpicture_t *p_spu_in )
400 {
401     /* We expect MKV formatted SSA:
402      * ReadOrder, Layer, Style, CharacterName, MarginL, MarginR,
403      * MarginV, Effect, Text */
404     decoder_sys_t   *p_sys = p_dec->p_sys;
405     subpicture_t    *p_spu = p_spu_in;
406     ssa_style_t     *p_style = NULL;
407     char            *psz_new_subtitle = NULL;
408     char            *psz_buffer_sub = NULL;
409     char            *psz_style = NULL;
410     char            *psz_style_start = NULL;
411     char            *psz_style_end = NULL;
412     int             i_text = 0, i_comma = 0, i_strlen = 0, i;
413     int             i_margin_l = 0, i_margin_r = 0, i_margin_v = 0;
414
415     psz_buffer_sub = psz_subtitle;
416
417     i_comma = 0;
418     while( i_comma < 8 && *psz_buffer_sub != '\0' )
419     {
420         if( *psz_buffer_sub == ',' )
421         {
422             i_comma++;
423             if( i_comma == 2 ) psz_style_start = &psz_buffer_sub[1];
424             if( i_comma == 3 ) psz_style_end = &psz_buffer_sub[0];
425             if( i_comma == 4 ) i_margin_l = (int)strtol( psz_buffer_sub+1, NULL, 10 );
426             if( i_comma == 5 ) i_margin_r = (int)strtol( psz_buffer_sub+1, NULL, 10 );
427             if( i_comma == 6 ) i_margin_v = (int)strtol( psz_buffer_sub+1, NULL, 10 );
428         }
429         psz_buffer_sub++;
430     }
431
432     if( *psz_buffer_sub == '\0' && i_comma == 8 )
433     {
434         msg_Dbg( p_dec, "couldn't find all fields in this SSA line" );
435         return;
436     }
437
438     psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
439     i_text = 0;
440     while( psz_buffer_sub[0] != '\0' )
441     {
442         if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'n' )
443         {
444             psz_new_subtitle[i_text] = ' ';
445             i_text++;
446             psz_buffer_sub += 2;
447         }
448         else if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'N' )
449         {
450             psz_new_subtitle[i_text] = '\n';
451             i_text++;
452             psz_buffer_sub += 2;
453         }
454         else if( psz_buffer_sub[0] == '{' &&
455                  psz_buffer_sub[1] == '\\' )
456         {
457             /* SSA control code */
458             while( psz_buffer_sub[0] != '\0' &&
459                    psz_buffer_sub[0] != '}' )
460             {
461                 psz_buffer_sub++;
462             }
463             psz_buffer_sub++;
464         }
465         else
466         {
467             psz_new_subtitle[i_text] = psz_buffer_sub[0];
468             i_text++;
469             psz_buffer_sub++;
470         }
471     }
472     psz_new_subtitle[i_text] = '\0';
473
474     i_strlen = __MAX( psz_style_end - psz_style_start, 0);
475     psz_style = (char *)malloc( i_strlen + 1);
476     psz_style = memcpy( psz_style, psz_style_start, i_strlen );
477     psz_style[i_strlen] = '\0';
478
479     for( i = 0; i < p_sys->i_ssa_styles; i++ )
480     {
481         if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
482             p_style = p_sys->pp_ssa_styles[i];
483     }
484     if( psz_style ) free( psz_style );
485
486     p_spu->p_region->psz_text = psz_new_subtitle;
487     if( p_style == NULL )
488     {
489         p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
490         p_spu->i_x = p_sys->i_align ? 20 : 0;
491         p_spu->i_y = 10;
492     }
493     else
494     {
495         msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename);
496         p_spu->p_region->p_style = &p_style->font_style;
497         p_spu->i_flags = p_style->i_align;
498         if( p_style->i_align & SUBPICTURE_ALIGN_LEFT )
499         {
500             p_spu->i_x = (i_margin_l) ? i_margin_l : p_style->i_margin_h;
501         }
502         else if( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) 
503         {
504             p_spu->i_x = (i_margin_r) ? i_margin_r : p_style->i_margin_h;
505         }
506         p_spu->i_y = (i_margin_v) ? i_margin_v : p_style->i_margin_v;
507     }
508 }
509
510 static char* GotoNextLine( char *psz_text )
511 {
512     char *p_newline = psz_text;
513     
514     while( p_newline[0] != '\0' )
515     {
516         if( p_newline[0] == '\n' || p_newline[0] == '\r' )
517         {
518             p_newline++;
519             while( p_newline[0] == '\n' || p_newline[0] == '\r' )
520                 p_newline++;
521             break;
522         }
523         else p_newline++;
524     }
525     return p_newline;
526 }
527
528 /*****************************************************************************
529  * ParseColor: SSA stores color in BBGGRR, in ASS it uses AABBGGRR
530  * The string value in the string can be a pure integer, or hexadecimal &HBBGGRR
531  *****************************************************************************/
532 static void ParseColor( decoder_t *p_dec, char *psz_color, int *pi_color, int *pi_alpha )
533 {
534     int i_color = 0;
535     if( !strncasecmp( psz_color, "&H", 2 ) )
536     {
537         /* textual HEX representation */
538         i_color = (int) strtol( psz_color+2, NULL, 16 );
539     }
540     else i_color = (int) strtol( psz_color, NULL, 0 );
541     
542     *pi_color = 0;
543     *pi_color |= ( ( i_color & 0x000000FF ) << 16 ); /* Red */
544     *pi_color |= ( ( i_color & 0x0000FF00 ) );       /* Green */
545     *pi_color |= ( ( i_color & 0x00FF0000 ) >> 16 ); /* Blue */
546     
547     if( pi_alpha != NULL )
548         *pi_alpha = ( i_color & 0xFF000000 ) >> 24;
549 }
550
551 /*****************************************************************************
552  * ParseSSAHeader: Retrieve global formatting information etc
553  *****************************************************************************/
554 static void ParseSSAHeader( decoder_t *p_dec )
555 {
556     decoder_sys_t *p_sys = p_dec->p_sys;
557     char *psz_parser = NULL;
558     char *psz_header = malloc( p_dec->fmt_in.i_extra+1 );
559     int i_section_type = 1;
560     
561     memcpy( psz_header, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
562     psz_header[ p_dec->fmt_in.i_extra] = '\0';
563     
564     /* Handle [Script Info] section */
565     psz_parser = strcasestr( psz_header, "[Script Info]" );
566     if( psz_parser == NULL ) goto eof;
567
568     psz_parser = GotoNextLine( psz_parser );
569
570     while( psz_parser[0] != '\0' )
571     {
572         int temp;
573         char buffer_text[MAX_LINE + 1];
574         
575         if( psz_parser[0] == '!' || psz_parser[0] == ';' ) /* comment */;
576         else if( sscanf( psz_parser, "PlayResX: %d", &temp ) == 1 )
577             p_sys->i_original_width = ( temp > 0 ) ? temp : -1;
578         else if( sscanf( psz_parser, "PlayResY: %d", &temp ) == 1 )
579             p_sys->i_original_height = ( temp > 0 ) ? temp : -1;
580         else if( sscanf( psz_parser, "Script Type: %8192s", buffer_text ) == 1 )
581         {
582             if( !strcasecmp( buffer_text, "V4.00+" ) ) p_sys->b_ass = VLC_TRUE;
583         }
584         else if( !strncasecmp( psz_parser, "[V4 Styles]", 11 ) )
585             i_section_type = 1;
586         else if( !strncasecmp( psz_parser, "[V4+ Styles]", 12) )
587         {
588             i_section_type = 2;
589             p_sys->b_ass = VLC_TRUE;
590         }
591         else if( !strncasecmp( psz_parser, "[Events]", 8 ) )
592             i_section_type = 4;
593         else if( !strncasecmp( psz_parser, "Style:", 6 ) )
594         {
595             int i_font_size, i_bold, i_italic, i_border, i_outline, i_shadow, i_underline,
596                 i_strikeout, i_scale_x, i_scale_y, i_spacing, i_align, i_margin_l, i_margin_r, i_margin_v;
597
598             char psz_temp_stylename[MAX_LINE+1];
599             char psz_temp_fontname[MAX_LINE+1];
600             char psz_temp_color1[MAX_LINE+1];
601             char psz_temp_color2[MAX_LINE+1];
602             char psz_temp_color3[MAX_LINE+1];
603             char psz_temp_color4[MAX_LINE+1];
604
605             if( i_section_type == 1 ) /* V4 */
606             {
607                 if( sscanf( psz_parser, "Style: %8192[^,],%8192[^,],%d,%8192[^,],%8192[^,],%8192[^,],%8192[^,],%d,%d,%d,%d,%d,%d,%d,%d,%d%*[^\r\n]",
608                     psz_temp_stylename, psz_temp_fontname, &i_font_size,
609                     psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
610                     &i_border, &i_outline, &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 16 )
611                 {
612                     ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
613
614                     p_style->psz_stylename = strdup( psz_temp_stylename );
615                     p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
616                     p_style->font_style.i_font_size = i_font_size;
617
618                     ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, NULL );
619                     ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, NULL );
620                     p_style->font_style.i_outline_color = p_style->font_style.i_shadow_color;
621                     p_style->font_style.i_font_alpha = p_style->font_style.i_outline_alpha = p_style->font_style.i_shadow_alpha = 0x00;
622                     p_style->font_style.i_style_flags = 0;
623                     if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
624                     if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
625     
626                     if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
627                     else if( i_border == 3 )
628                     {
629                         p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
630                         p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
631                         p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
632                     }
633                     p_style->font_style.i_shadow_width = i_shadow;
634                     p_style->font_style.i_outline_width = i_outline;
635
636                     p_style->i_align = 0;
637                     if( i_align == 1 || i_align == 5 || i_align == 9 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
638                     if( i_align == 3 || i_align == 7 || i_align == 11 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
639                     if( i_align < 4 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
640                     else if( i_align < 8 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP; 
641
642                     p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
643                     p_style->i_margin_v = i_margin_v;
644
645                     TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
646                 }
647                 else msg_Warn( p_dec, "SSA v4 styleline parsing failed" );
648             }
649             else if( i_section_type == 2 ) /* V4+ */
650             {
651                 /* Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour,
652                    Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline,
653                    Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
654                 */
655                 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]",
656                     psz_temp_stylename, psz_temp_fontname, &i_font_size,
657                     psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
658                     &i_underline, &i_strikeout, &i_scale_x, &i_scale_y, &i_spacing, &i_border, &i_outline,
659                     &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 21 )
660                 {
661                     ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
662
663                     p_style->psz_stylename = strdup( psz_temp_stylename );
664                     p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
665                     p_style->font_style.i_font_size = i_font_size;
666                     msg_Dbg( p_dec, psz_temp_color1 );
667                     ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, &p_style->font_style.i_font_alpha );
668                     ParseColor( p_dec, psz_temp_color3, &p_style->font_style.i_outline_color, &p_style->font_style.i_outline_alpha );
669                     ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, &p_style->font_style.i_shadow_alpha );
670
671                     p_style->font_style.i_style_flags = 0;
672                     if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
673                     if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
674                     if( i_underline ) p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
675                     if( i_strikeout ) p_style->font_style.i_style_flags |= STYLE_STRIKEOUT;
676                     if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
677                     else if( i_border == 3 )
678                     {
679                         p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
680                         p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
681                         p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
682                     }
683                     p_style->font_style.i_shadow_width  = ( i_border == 1 ) ? i_shadow : 0;
684                     p_style->font_style.i_outline_width = ( i_border == 1 ) ? i_outline : 0;
685                     p_style->font_style.i_spacing = i_spacing;
686                     //p_style->font_style.f_angle = f_angle;
687                     
688                     p_style->i_align = 0;
689                     if( i_align == 0x1 || i_align == 0x4 || i_align == 0x1 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
690                     if( i_align == 0x3 || i_align == 0x6 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
691                     if( i_align == 0x7 || i_align == 0x8 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP;
692                     if( i_align == 0x1 || i_align == 0x2 || i_align == 0x3 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
693                     p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
694                     p_style->i_margin_v = i_margin_v;
695                     
696                     /*TODO: Ignored: angle i_scale_x|y (fontscaling), i_encoding */
697                     TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
698                 }
699                 else msg_Dbg( p_dec, "SSA V4+ styleline parsing failed" );
700             }
701         }
702         psz_parser = GotoNextLine( psz_parser );
703     }
704
705 eof:
706     if( psz_header ) free( psz_header );
707     return;
708 }
709
710 static void StripTags( char *psz_text )
711 {
712     int i_left_moves = 0;
713     vlc_bool_t b_inside_tag = VLC_FALSE;
714     int i = 0;
715     int i_tag_start = -1;
716     while( psz_text[ i ] )
717     {
718         if( !b_inside_tag )
719         {
720             if( psz_text[ i ] == '<' )
721             {
722                 b_inside_tag = VLC_TRUE;
723                 i_tag_start = i;
724             }
725             psz_text[ i - i_left_moves ] = psz_text[ i ];
726         }
727         else
728         {
729             if( ( psz_text[ i ] == ' ' ) ||
730                 ( psz_text[ i ] == '\t' ) ||
731                 ( psz_text[ i ] == '\n' ) ||
732                 ( psz_text[ i ] == '\r' ) )
733             {
734                 b_inside_tag = VLC_FALSE;
735                 i_tag_start = -1;
736             }
737             else if( psz_text[ i ] == '>' )
738             {
739                 i_left_moves += i - i_tag_start + 1;
740                 i_tag_start = -1;
741                 b_inside_tag = VLC_FALSE;
742             }
743             else
744             {
745                 psz_text[ i - i_left_moves ] = psz_text[ i ];
746             }
747         }
748         i++;
749     }
750     psz_text[ i - i_left_moves ] = '\0';
751 }