]> git.sesse.net Git - vlc/blob - modules/codec/subsdec.c
codec 2nd round exept fo x264 (refs #438)
[vlc] / modules / codec / subsdec.c
1 /*****************************************************************************
2  * subsdec.c : text subtitles decoder
3  *****************************************************************************
4  * Copyright (C) 2000-2006 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     vlc_bool_t          b_autodetect_utf8;
57
58     ssa_style_t         **pp_ssa_styles;
59     int                 i_ssa_styles;
60 };
61
62 /*****************************************************************************
63  * Local prototypes
64  *****************************************************************************/
65 static int  OpenDecoder   ( vlc_object_t * );
66 static void CloseDecoder  ( vlc_object_t * );
67
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 * );
74
75 #define DEFAULT_NAME "Default"
76 #define MAX_LINE 8192
77
78 /*****************************************************************************
79  * Module descriptor.
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", "",
96     "Macintosh", "",
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" };
102
103 static int  pi_justification[] = { 0, 1, 2 };
104 static char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
105
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.")
116
117
118 vlc_module_begin();
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 );
125
126     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
127                  VLC_FALSE );
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,
135                  VLC_FALSE );
136 vlc_module_end();
137
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
142  * to chose.
143  *****************************************************************************/
144 static int OpenDecoder( vlc_object_t *p_this )
145 {
146     decoder_t     *p_dec = (decoder_t*)p_this;
147     decoder_sys_t *p_sys;
148     vlc_value_t    val;
149
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',' ') )
152     {
153         return VLC_EGENERIC;
154     }
155
156     p_dec->pf_decode_sub = DecodeBlock;
157
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 )
161     {
162         msg_Err( p_dec, "out of memory" );
163         return VLC_ENOMEM;
164     }
165
166     /* init of p_sys */
167     p_sys->i_align = 0;
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;
175
176     if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
177     {
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 );
182     }
183     else
184     {
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 ) )
189         {
190             const char *psz_charset = GetFallbackEncoding();
191
192             p_sys->b_autodetect_utf8 = var_CreateGetBool( p_dec,
193                     "subsdec-autodetect-utf8" );
194
195             p_sys->iconv_handle = vlc_iconv_open( "UTF-8", psz_charset );
196             msg_Dbg( p_dec, "using default character encoding: %s", psz_charset );
197         }
198         else if( !strcmp( val.psz_string, "UTF-8" ) )
199         {
200             msg_Dbg( p_dec, "using character encoding: UTF-8" );
201         }
202         else if( val.psz_string )
203         {
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 )
207             {
208                 msg_Warn( p_dec, "unable to do requested conversion" );
209             }
210         }
211         if( val.psz_string ) free( val.psz_string );
212     }
213
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;
217
218     if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
219     {
220         if( p_dec->fmt_in.i_extra > 0 )
221             ParseSSAHeader( p_dec );
222     }
223
224     return VLC_SUCCESS;
225 }
226
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 )
233 {
234     subpicture_t *p_spu = NULL;
235
236     if( !pp_block || *pp_block == NULL ) return NULL;
237
238     p_spu = ParseText( p_dec, *pp_block );
239
240     block_Release( *pp_block );
241     *pp_block = NULL;
242
243     return p_spu;
244 }
245
246 /*****************************************************************************
247  * CloseDecoder: clean up the decoder
248  *****************************************************************************/
249 static void CloseDecoder( vlc_object_t *p_this )
250 {
251     decoder_t *p_dec = (decoder_t *)p_this;
252     decoder_sys_t *p_sys = p_dec->p_sys;
253
254     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
255     {
256         vlc_iconv_close( p_sys->iconv_handle );
257     }
258
259     if( p_sys->pp_ssa_styles )
260     {
261         int i;
262         for( i = 0; i < p_sys->i_ssa_styles; i++ )
263         {
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;
269         }
270         free( p_sys->pp_ssa_styles ); p_sys->pp_ssa_styles = NULL;
271     }
272
273     free( p_sys );
274 }
275
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 )
280 {
281     decoder_sys_t *p_sys = p_dec->p_sys;
282     subpicture_t *p_spu = NULL;
283     char *psz_subtitle = NULL;
284     video_format_t fmt;
285
286     /* We cannot display a subpicture with no date */
287     if( p_block->i_pts == 0 )
288     {
289         msg_Warn( p_dec, "subtitle without a date" );
290         return NULL;
291     }
292
293     /* Check validity of packet data */
294     if( p_block->i_buffer <= 1 || p_block->p_buffer[0] == '\0' )
295     {
296         msg_Warn( p_dec, "empty subtitle" );
297         return NULL;
298     }
299
300     /* Should be resiliant against bad subtitles */
301     psz_subtitle = strndup( (const char *)p_block->p_buffer,
302                             p_block->i_buffer );
303     if( psz_subtitle == NULL )
304         return NULL;
305
306     if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
307         EnsureUTF8( psz_subtitle );
308     else
309     {
310
311         if( p_sys->b_autodetect_utf8 )
312         {
313             if( IsUTF8( psz_subtitle ) == NULL )
314             {
315                 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
316                          "disabling UTF-8 subtitles autodetection" );
317                 p_sys->b_autodetect_utf8 = VLC_FALSE;
318             }
319         }
320
321         if( !p_sys->b_autodetect_utf8 )
322         {
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;
328
329             size_t ret = vlc_iconv( p_sys->iconv_handle,
330                                     &psz_convert_buffer_in, &inbytes_left,
331                                     &psz_convert_buffer_out, &outbytes_left );
332
333             *psz_convert_buffer_out++ = '\0';
334             free( psz_subtitle );
335
336             if( ( ret == (size_t)(-1) ) || inbytes_left )
337             {
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.") );
342                 return NULL;
343             }
344
345             psz_subtitle = realloc( psz_new_subtitle,
346                                     psz_convert_buffer_out - psz_new_subtitle );
347         }
348     }
349
350     /* Create the subpicture unit */
351     p_spu = p_dec->pf_spu_buffer_new( p_dec );
352     if( !p_spu )
353     {
354         msg_Warn( p_dec, "can't get spu buffer" );
355         if( psz_subtitle ) free( psz_subtitle );
356         return NULL;
357     }
358
359     /* Create a new subpicture region */
360     memset( &fmt, 0, sizeof(video_format_t) );
361     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
362     fmt.i_aspect = 0;
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 )
367     {
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 );
371         return NULL;
372     }
373
374     /* Decode and format the subpicture unit */
375     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
376     {
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;
380         p_spu->i_y = 10;
381
382         /* Remove formatting from string */
383         StripTags( psz_subtitle );
384
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;
390     }
391     else
392     {
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 );
402     }
403     return p_spu;
404 }
405
406 static void ParseSSAString( decoder_t *p_dec, char *psz_subtitle, subpicture_t *p_spu_in )
407 {
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;
421
422     psz_buffer_sub = psz_subtitle;
423
424     i_comma = 0;
425     while( i_comma < 8 && *psz_buffer_sub != '\0' )
426     {
427         if( *psz_buffer_sub == ',' )
428         {
429             i_comma++;
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 );
435         }
436         psz_buffer_sub++;
437     }
438
439     if( *psz_buffer_sub == '\0' && i_comma == 8 )
440     {
441         msg_Dbg( p_dec, "couldn't find all fields in this SSA line" );
442         return;
443     }
444
445     psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
446     i_text = 0;
447     while( psz_buffer_sub[0] != '\0' )
448     {
449         if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'n' )
450         {
451             psz_new_subtitle[i_text] = ' ';
452             i_text++;
453             psz_buffer_sub += 2;
454         }
455         else if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'N' )
456         {
457             psz_new_subtitle[i_text] = '\n';
458             i_text++;
459             psz_buffer_sub += 2;
460         }
461         else if( psz_buffer_sub[0] == '{' &&
462                  psz_buffer_sub[1] == '\\' )
463         {
464             /* SSA control code */
465             while( psz_buffer_sub[0] != '\0' &&
466                    psz_buffer_sub[0] != '}' )
467             {
468                 psz_buffer_sub++;
469             }
470             psz_buffer_sub++;
471         }
472         else
473         {
474             psz_new_subtitle[i_text] = psz_buffer_sub[0];
475             i_text++;
476             psz_buffer_sub++;
477         }
478     }
479     psz_new_subtitle[i_text] = '\0';
480
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';
485
486     for( i = 0; i < p_sys->i_ssa_styles; i++ )
487     {
488         if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
489             p_style = p_sys->pp_ssa_styles[i];
490     }
491     if( psz_style ) free( psz_style );
492
493     p_spu->p_region->psz_text = psz_new_subtitle;
494     if( p_style == NULL )
495     {
496         p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
497         p_spu->i_x = p_sys->i_align ? 20 : 0;
498         p_spu->i_y = 10;
499     }
500     else
501     {
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 )
506         {
507             p_spu->i_x = (i_margin_l) ? i_margin_l : p_style->i_margin_h;
508         }
509         else if( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) 
510         {
511             p_spu->i_x = (i_margin_r) ? i_margin_r : p_style->i_margin_h;
512         }
513         p_spu->i_y = (i_margin_v) ? i_margin_v : p_style->i_margin_v;
514     }
515 }
516
517 static char* GotoNextLine( char *psz_text )
518 {
519     char *p_newline = psz_text;
520
521     while( p_newline[0] != '\0' )
522     {
523         if( p_newline[0] == '\n' || p_newline[0] == '\r' )
524         {
525             p_newline++;
526             while( p_newline[0] == '\n' || p_newline[0] == '\r' )
527                 p_newline++;
528             break;
529         }
530         else p_newline++;
531     }
532     return p_newline;
533 }
534
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 )
540 {
541     int i_color = 0;
542     if( !strncasecmp( psz_color, "&H", 2 ) )
543     {
544         /* textual HEX representation */
545         i_color = (int) strtol( psz_color+2, NULL, 16 );
546     }
547     else i_color = (int) strtol( psz_color, NULL, 0 );
548
549     *pi_color = 0;
550     *pi_color |= ( ( i_color & 0x000000FF ) << 16 ); /* Red */
551     *pi_color |= ( ( i_color & 0x0000FF00 ) );       /* Green */
552     *pi_color |= ( ( i_color & 0x00FF0000 ) >> 16 ); /* Blue */
553
554     if( pi_alpha != NULL )
555         *pi_alpha = ( i_color & 0xFF000000 ) >> 24;
556 }
557
558 /*****************************************************************************
559  * ParseSSAHeader: Retrieve global formatting information etc
560  *****************************************************************************/
561 static void ParseSSAHeader( decoder_t *p_dec )
562 {
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;
567
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';
570
571     /* Handle [Script Info] section */
572     psz_parser = strcasestr( psz_header, "[Script Info]" );
573     if( psz_parser == NULL ) goto eof;
574
575     psz_parser = GotoNextLine( psz_parser );
576
577     while( psz_parser[0] != '\0' )
578     {
579         int temp;
580         char buffer_text[MAX_LINE + 1];
581
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 )
588         {
589             if( !strcasecmp( buffer_text, "V4.00+" ) ) p_sys->b_ass = VLC_TRUE;
590         }
591         else if( !strncasecmp( psz_parser, "[V4 Styles]", 11 ) )
592             i_section_type = 1;
593         else if( !strncasecmp( psz_parser, "[V4+ Styles]", 12) )
594         {
595             i_section_type = 2;
596             p_sys->b_ass = VLC_TRUE;
597         }
598         else if( !strncasecmp( psz_parser, "[Events]", 8 ) )
599             i_section_type = 4;
600         else if( !strncasecmp( psz_parser, "Style:", 6 ) )
601         {
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;
604
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];
611
612             if( i_section_type == 1 ) /* V4 */
613             {
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 )
618                 {
619                     ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
620
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;
624
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;
632
633                     if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
634                     else if( i_border == 3 )
635                     {
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;
639                     }
640                     p_style->font_style.i_shadow_width = i_shadow;
641                     p_style->font_style.i_outline_width = i_outline;
642
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; 
648
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;
651
652                     TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
653                 }
654                 else msg_Warn( p_dec, "SSA v4 styleline parsing failed" );
655             }
656             else if( i_section_type == 2 ) /* V4+ */
657             {
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
661                 */
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 )
667                 {
668                     ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
669
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 );
677
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 )
685                     {
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;
689                     }
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;
694
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;
702
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 );
705                 }
706                 else msg_Dbg( p_dec, "SSA V4+ styleline parsing failed" );
707             }
708         }
709         psz_parser = GotoNextLine( psz_parser );
710     }
711
712 eof:
713     if( psz_header ) free( psz_header );
714     return;
715 }
716
717 static void StripTags( char *psz_text )
718 {
719     int i_left_moves = 0;
720     vlc_bool_t b_inside_tag = VLC_FALSE;
721     int i = 0;
722     int i_tag_start = -1;
723     while( psz_text[ i ] )
724     {
725         if( !b_inside_tag )
726         {
727             if( psz_text[ i ] == '<' )
728             {
729                 b_inside_tag = VLC_TRUE;
730                 i_tag_start = i;
731             }
732             psz_text[ i - i_left_moves ] = psz_text[ i ];
733         }
734         else
735         {
736             if( ( psz_text[ i ] == ' ' ) ||
737                 ( psz_text[ i ] == '\t' ) ||
738                 ( psz_text[ i ] == '\n' ) ||
739                 ( psz_text[ i ] == '\r' ) )
740             {
741                 b_inside_tag = VLC_FALSE;
742                 i_tag_start = -1;
743             }
744             else if( psz_text[ i ] == '>' )
745             {
746                 i_left_moves += i - i_tag_start + 1;
747                 i_tag_start = -1;
748                 b_inside_tag = VLC_FALSE;
749             }
750             else
751             {
752                 psz_text[ i - i_left_moves ] = psz_text[ i ];
753             }
754         }
755         i++;
756     }
757     psz_text[ i - i_left_moves ] = '\0';
758 }