]> git.sesse.net Git - vlc/blob - modules/codec/subsdec.c
More explicit debug and error in subtitle charset selection
[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_codec.h>
32
33 #include <vlc_osd.h>
34 #include <vlc_filter.h>
35 #include <vlc_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 const char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
82     "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
83     "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
84     "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
85     "ISO-8859-6", "CP1256", "MacArabic", "",
86     "ISO-8859-7", "CP1253", "MacGreek", "",
87     "ISO-8859-8", "CP1255", "MacHebrew", "",
88     "ISO-8859-9", "CP1254", "MacTurkish", "",
89     "ISO-8859-13", "CP1257", "",
90     "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
91     "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
92     "ISO-2022-KR", "EUC-KR", "",
93     "MacThai", "KOI8-T", "",
94     "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
95     "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
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 SSA supports charset selection.
104 The following known charsets are used:
105
106 0 = Ansi - Western European
107 1 = default
108 2 = symbol
109 3 = invalid
110 77 = Mac
111 128 = Japanese (Shift JIS)
112 129 = Hangul
113 130 = Johab
114 134 = GB2312 Simplified Chinese
115 136 = Big5 Traditional Chinese
116 161 = Greek
117 162 = Turkish
118 163 = Vietnamese
119 177 = Hebrew
120 178 = Arabic
121 186 = Baltic
122 204 = Russian (Cyrillic)
123 222 = Thai
124 238 = Eastern European
125 254 = PC 437
126 */
127
128 static int  pi_justification[] = { 0, 1, 2 };
129 static const char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
130
131 #define ENCODING_TEXT N_("Subtitles text encoding")
132 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
133 #define ALIGN_TEXT N_("Subtitles justification")
134 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
135 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
136 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
137             "UTF-8 encoding within subtitles files.")
138 #define FORMAT_TEXT N_("Formatted Subtitles")
139 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
140  "VLC partly implements this, but you can choose to disable all formatting.")
141
142
143 vlc_module_begin();
144     set_shortname( _("Subtitles"));
145     set_description( _("Text subtitles decoder") );
146     set_capability( "decoder", 50 );
147     set_callbacks( OpenDecoder, CloseDecoder );
148     set_category( CAT_INPUT );
149     set_subcategory( SUBCAT_INPUT_SCODEC );
150
151     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
152                  VLC_FALSE );
153         change_integer_list( pi_justification, ppsz_justification_text, 0 );
154     add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
155                 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
156         change_string_list( ppsz_encodings, 0, 0 );
157     add_bool( "subsdec-autodetect-utf8", VLC_TRUE, NULL,
158               AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, VLC_FALSE );
159     add_bool( "subsdec-formatted", VLC_TRUE, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
160                  VLC_FALSE );
161 vlc_module_end();
162
163 /*****************************************************************************
164  * OpenDecoder: probe the decoder and return score
165  *****************************************************************************
166  * Tries to launch a decoder and return score so that the interface is able
167  * to chose.
168  *****************************************************************************/
169 static int OpenDecoder( vlc_object_t *p_this )
170 {
171     decoder_t     *p_dec = (decoder_t*)p_this;
172     decoder_sys_t *p_sys;
173     vlc_value_t    val;
174
175     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
176         p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
177     {
178         return VLC_EGENERIC;
179     }
180
181     p_dec->pf_decode_sub = DecodeBlock;
182
183     /* Allocate the memory needed to store the decoder's structure */
184     if( ( p_dec->p_sys = p_sys =
185           (decoder_sys_t *)calloc(1, sizeof(decoder_sys_t)) ) == NULL )
186     {
187         msg_Err( p_dec, "out of memory" );
188         return VLC_ENOMEM;
189     }
190
191     /* init of p_sys */
192     p_sys->i_align = 0;
193     p_sys->iconv_handle = (vlc_iconv_t)-1;
194     p_sys->b_autodetect_utf8 = VLC_FALSE;
195     p_sys->b_ass = VLC_FALSE;
196     p_sys->i_original_height = -1;
197     p_sys->i_original_width = -1;
198     p_sys->pp_ssa_styles = NULL;
199     p_sys->i_ssa_styles = 0;
200
201     char *psz_charset = NULL;
202     /* First try demux-specified encoding */
203     if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
204     {
205         psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
206         msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
207                  p_dec->fmt_in.subs.psz_encoding ?: "not specified");
208     }
209
210     /* Second, try configured encoding */
211     if (psz_charset == NULL)
212     {
213         psz_charset = var_CreateGetNonEmptyString (p_dec, "subsdec-encoding");
214         if ((psz_charset != NULL) && !strcasecmp (psz_charset, DEFAULT_NAME))
215         {
216             free (psz_charset);
217             psz_charset = NULL;
218         }
219
220         msg_Dbg (p_dec, "trying configured character encoding: %s",
221                  psz_charset ?: "not specified");
222     }
223
224     /* Third, try "local" encoding with optional UTF-8 autodetection */
225     if (psz_charset == NULL)
226     {
227         psz_charset = strdup (GetFallbackEncoding ());
228         msg_Dbg (p_dec, "trying default character encoding: %s",
229                  psz_charset ?: "not specified");
230
231         if (var_CreateGetBool (p_dec, "subsdec-autodetect-utf8"))
232         {
233             msg_Dbg (p_dec, "using automatic UTF-8 detection");
234             p_sys->b_autodetect_utf8 = VLC_TRUE;
235         }
236     }
237
238     if (psz_charset == NULL)
239     {
240         psz_charset = strdup ("UTF-8");
241         msg_Dbg (p_dec, "trying hard-coded character encoding: %s",
242                  psz_charset ?: "error");
243     }
244
245     if (psz_charset == NULL)
246     {
247         free (p_sys);
248         return VLC_ENOMEM;
249     }
250
251     if (strcasecmp (psz_charset, "UTF-8") && strcasecmp (psz_charset, "utf8"))
252     {
253         p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
254         if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
255             msg_Err (p_dec, "cannot convert from %s: %s", psz_charset,
256                      strerror (errno));
257     }
258     free (psz_charset);
259
260     var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
261     var_Get( p_dec, "subsdec-align", &val );
262     p_sys->i_align = val.i_int;
263
264     if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
265     {
266         if( p_dec->fmt_in.i_extra > 0 )
267             ParseSSAHeader( p_dec );
268     }
269
270     return VLC_SUCCESS;
271 }
272
273 /****************************************************************************
274  * DecodeBlock: the whole thing
275  ****************************************************************************
276  * This function must be fed with complete subtitles units.
277  ****************************************************************************/
278 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
279 {
280     subpicture_t *p_spu = NULL;
281
282     if( !pp_block || *pp_block == NULL ) return NULL;
283
284     p_spu = ParseText( p_dec, *pp_block );
285
286     block_Release( *pp_block );
287     *pp_block = NULL;
288
289     return p_spu;
290 }
291
292 /*****************************************************************************
293  * CloseDecoder: clean up the decoder
294  *****************************************************************************/
295 static void CloseDecoder( vlc_object_t *p_this )
296 {
297     decoder_t *p_dec = (decoder_t *)p_this;
298     decoder_sys_t *p_sys = p_dec->p_sys;
299
300     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
301     {
302         vlc_iconv_close( p_sys->iconv_handle );
303     }
304
305     if( p_sys->pp_ssa_styles )
306     {
307         int i;
308         for( i = 0; i < p_sys->i_ssa_styles; i++ )
309         {
310             if( p_sys->pp_ssa_styles[i]->psz_stylename ) free( p_sys->pp_ssa_styles[i]->psz_stylename );
311             p_sys->pp_ssa_styles[i]->psz_stylename = NULL;
312             if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname ) free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
313             p_sys->pp_ssa_styles[i]->font_style.psz_fontname = NULL;
314             if( p_sys->pp_ssa_styles[i] ) free( p_sys->pp_ssa_styles[i] ); p_sys->pp_ssa_styles[i] = NULL;
315         }
316         free( p_sys->pp_ssa_styles ); p_sys->pp_ssa_styles = NULL;
317     }
318
319     free( p_sys );
320 }
321
322 /*****************************************************************************
323  * ParseText: parse an text subtitle packet and send it to the video output
324  *****************************************************************************/
325 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
326 {
327     decoder_sys_t *p_sys = p_dec->p_sys;
328     subpicture_t *p_spu = NULL;
329     char *psz_subtitle = NULL;
330     video_format_t fmt;
331
332     /* We cannot display a subpicture with no date */
333     if( p_block->i_pts == 0 )
334     {
335         msg_Warn( p_dec, "subtitle without a date" );
336         return NULL;
337     }
338
339     /* Check validity of packet data */
340     /* An "empty" line containing only \0 can be used to force
341        and ephemer picture from the screen */
342     if( p_block->i_buffer < 1 )
343     {
344         msg_Warn( p_dec, "no subtitle data" );
345         return NULL;
346     }
347
348     /* Should be resiliant against bad subtitles */
349     psz_subtitle = strndup( (const char *)p_block->p_buffer,
350                             p_block->i_buffer );
351     if( psz_subtitle == NULL )
352         return NULL;
353
354     if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
355         EnsureUTF8( psz_subtitle );
356     else
357     {
358
359         if( p_sys->b_autodetect_utf8 )
360         {
361             if( IsUTF8( psz_subtitle ) == NULL )
362             {
363                 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
364                          "disabling UTF-8 subtitles autodetection" );
365                 p_sys->b_autodetect_utf8 = VLC_FALSE;
366             }
367         }
368
369         if( !p_sys->b_autodetect_utf8 )
370         {
371             size_t inbytes_left = strlen( psz_subtitle );
372             size_t outbytes_left = 6 * inbytes_left;
373             char *psz_new_subtitle = malloc( outbytes_left + 1 );
374             char *psz_convert_buffer_out = psz_new_subtitle;
375             const char *psz_convert_buffer_in = psz_subtitle;
376
377             size_t ret = vlc_iconv( p_sys->iconv_handle,
378                                     &psz_convert_buffer_in, &inbytes_left,
379                                     &psz_convert_buffer_out, &outbytes_left );
380
381             *psz_convert_buffer_out++ = '\0';
382             free( psz_subtitle );
383
384             if( ( ret == (size_t)(-1) ) || inbytes_left )
385             {
386                 free( psz_new_subtitle );
387                 msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
388                         "Try manually setting a character-encoding "
389                                 "before you open the file.") );
390                 return NULL;
391             }
392
393             psz_subtitle = realloc( psz_new_subtitle,
394                                     psz_convert_buffer_out - psz_new_subtitle );
395         }
396     }
397
398     /* Create the subpicture unit */
399     p_spu = p_dec->pf_spu_buffer_new( p_dec );
400     if( !p_spu )
401     {
402         msg_Warn( p_dec, "can't get spu buffer" );
403         if( psz_subtitle ) free( psz_subtitle );
404         return NULL;
405     }
406
407     p_spu->b_pausable = VLC_TRUE;
408
409     /* Create a new subpicture region */
410     memset( &fmt, 0, sizeof(video_format_t) );
411     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
412     fmt.i_aspect = 0;
413     fmt.i_width = fmt.i_height = 0;
414     fmt.i_x_offset = fmt.i_y_offset = 0;
415     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
416     if( !p_spu->p_region )
417     {
418         msg_Err( p_dec, "cannot allocate SPU region" );
419         if( psz_subtitle ) free( psz_subtitle );
420         p_dec->pf_spu_buffer_del( p_dec, p_spu );
421         return NULL;
422     }
423
424     /* Decode and format the subpicture unit */
425     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
426     {
427         /* Normal text subs, easy markup */
428         p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
429         p_spu->i_x = p_sys->i_align ? 20 : 0;
430         p_spu->i_y = 10;
431
432         /* Remove formatting from string */
433         StripTags( psz_subtitle );
434
435         p_spu->p_region->psz_text = psz_subtitle;
436         p_spu->i_start = p_block->i_pts;
437         p_spu->i_stop = p_block->i_pts + p_block->i_length;
438         p_spu->b_ephemer = (p_block->i_length == 0);
439         p_spu->b_absolute = VLC_FALSE;
440     }
441     else
442     {
443         /* Decode SSA strings */
444         ParseSSAString( p_dec, psz_subtitle, p_spu );
445         p_spu->i_start = p_block->i_pts;
446         p_spu->i_stop = p_block->i_pts + p_block->i_length;
447         p_spu->b_ephemer = (p_block->i_length == 0);
448         p_spu->b_absolute = VLC_FALSE;
449         p_spu->i_original_picture_width = p_sys->i_original_width;
450         p_spu->i_original_picture_height = p_sys->i_original_height;
451         if( psz_subtitle ) free( psz_subtitle );
452     }
453     return p_spu;
454 }
455
456 static void ParseSSAString( decoder_t *p_dec, char *psz_subtitle, subpicture_t *p_spu_in )
457 {
458     /* We expect MKV formatted SSA:
459      * ReadOrder, Layer, Style, CharacterName, MarginL, MarginR,
460      * MarginV, Effect, Text */
461     decoder_sys_t   *p_sys = p_dec->p_sys;
462     subpicture_t    *p_spu = p_spu_in;
463     ssa_style_t     *p_style = NULL;
464     char            *psz_new_subtitle = NULL;
465     char            *psz_buffer_sub = NULL;
466     char            *psz_style = NULL;
467     char            *psz_style_start = NULL;
468     char            *psz_style_end = NULL;
469     int             i_text = 0, i_comma = 0, i_strlen = 0, i;
470     int             i_margin_l = 0, i_margin_r = 0, i_margin_v = 0;
471
472     psz_buffer_sub = psz_subtitle;
473
474     i_comma = 0;
475     while( i_comma < 8 && *psz_buffer_sub != '\0' )
476     {
477         if( *psz_buffer_sub == ',' )
478         {
479             i_comma++;
480             if( i_comma == 2 ) psz_style_start = &psz_buffer_sub[1];
481             if( i_comma == 3 ) psz_style_end = &psz_buffer_sub[0];
482             if( i_comma == 4 ) i_margin_l = (int)strtol( psz_buffer_sub+1, NULL, 10 );
483             if( i_comma == 5 ) i_margin_r = (int)strtol( psz_buffer_sub+1, NULL, 10 );
484             if( i_comma == 6 ) i_margin_v = (int)strtol( psz_buffer_sub+1, NULL, 10 );
485         }
486         psz_buffer_sub++;
487     }
488
489     if( *psz_buffer_sub == '\0' && i_comma == 8 )
490     {
491         msg_Dbg( p_dec, "couldn't find all fields in this SSA line" );
492         return;
493     }
494
495     psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
496     i_text = 0;
497     while( psz_buffer_sub[0] != '\0' )
498     {
499         if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'n' )
500         {
501             psz_new_subtitle[i_text] = ' ';
502             i_text++;
503             psz_buffer_sub += 2;
504         }
505         else if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'N' )
506         {
507             psz_new_subtitle[i_text] = '\n';
508             i_text++;
509             psz_buffer_sub += 2;
510         }
511         else if( psz_buffer_sub[0] == '{' &&
512                  psz_buffer_sub[1] == '\\' )
513         {
514             /* SSA control code */
515             while( psz_buffer_sub[0] != '\0' &&
516                    psz_buffer_sub[0] != '}' )
517             {
518                 psz_buffer_sub++;
519             }
520             psz_buffer_sub++;
521         }
522         else
523         {
524             psz_new_subtitle[i_text] = psz_buffer_sub[0];
525             i_text++;
526             psz_buffer_sub++;
527         }
528     }
529     psz_new_subtitle[i_text] = '\0';
530
531     i_strlen = __MAX( psz_style_end - psz_style_start, 0);
532     psz_style = (char *)malloc( i_strlen + 1);
533     psz_style = memcpy( psz_style, psz_style_start, i_strlen );
534     psz_style[i_strlen] = '\0';
535
536     for( i = 0; i < p_sys->i_ssa_styles; i++ )
537     {
538         if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
539             p_style = p_sys->pp_ssa_styles[i];
540     }
541     if( psz_style ) free( psz_style );
542
543     p_spu->p_region->psz_text = psz_new_subtitle;
544     if( p_style == NULL )
545     {
546         p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
547         p_spu->i_x = p_sys->i_align ? 20 : 0;
548         p_spu->i_y = 10;
549     }
550     else
551     {
552         msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename);
553         p_spu->p_region->p_style = &p_style->font_style;
554         p_spu->i_flags = p_style->i_align;
555         if( p_style->i_align & SUBPICTURE_ALIGN_LEFT )
556         {
557             p_spu->i_x = (i_margin_l) ? i_margin_l : p_style->i_margin_h;
558         }
559         else if( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) 
560         {
561             p_spu->i_x = (i_margin_r) ? i_margin_r : p_style->i_margin_h;
562         }
563         p_spu->i_y = (i_margin_v) ? i_margin_v : p_style->i_margin_v;
564     }
565 }
566
567 static char* GotoNextLine( char *psz_text )
568 {
569     char *p_newline = psz_text;
570
571     while( p_newline[0] != '\0' )
572     {
573         if( p_newline[0] == '\n' || p_newline[0] == '\r' )
574         {
575             p_newline++;
576             while( p_newline[0] == '\n' || p_newline[0] == '\r' )
577                 p_newline++;
578             break;
579         }
580         else p_newline++;
581     }
582     return p_newline;
583 }
584
585 /*****************************************************************************
586  * ParseColor: SSA stores color in BBGGRR, in ASS it uses AABBGGRR
587  * The string value in the string can be a pure integer, or hexadecimal &HBBGGRR
588  *****************************************************************************/
589 static void ParseColor( decoder_t *p_dec, char *psz_color, int *pi_color, int *pi_alpha )
590 {
591     int i_color = 0;
592     if( !strncasecmp( psz_color, "&H", 2 ) )
593     {
594         /* textual HEX representation */
595         i_color = (int) strtol( psz_color+2, NULL, 16 );
596     }
597     else i_color = (int) strtol( psz_color, NULL, 0 );
598
599     *pi_color = 0;
600     *pi_color |= ( ( i_color & 0x000000FF ) << 16 ); /* Red */
601     *pi_color |= ( ( i_color & 0x0000FF00 ) );       /* Green */
602     *pi_color |= ( ( i_color & 0x00FF0000 ) >> 16 ); /* Blue */
603
604     if( pi_alpha != NULL )
605         *pi_alpha = ( i_color & 0xFF000000 ) >> 24;
606 }
607
608 /*****************************************************************************
609  * ParseSSAHeader: Retrieve global formatting information etc
610  *****************************************************************************/
611 static void ParseSSAHeader( decoder_t *p_dec )
612 {
613     decoder_sys_t *p_sys = p_dec->p_sys;
614     char *psz_parser = NULL;
615     char *psz_header = malloc( p_dec->fmt_in.i_extra+1 );
616     int i_section_type = 1;
617
618     memcpy( psz_header, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
619     psz_header[ p_dec->fmt_in.i_extra] = '\0';
620
621     /* Handle [Script Info] section */
622     psz_parser = strcasestr( psz_header, "[Script Info]" );
623     if( psz_parser == NULL ) goto eof;
624
625     psz_parser = GotoNextLine( psz_parser );
626
627     while( psz_parser[0] != '\0' )
628     {
629         int temp;
630         char buffer_text[MAX_LINE + 1];
631
632         if( psz_parser[0] == '!' || psz_parser[0] == ';' ) /* comment */;
633         else if( sscanf( psz_parser, "PlayResX: %d", &temp ) == 1 )
634             p_sys->i_original_width = ( temp > 0 ) ? temp : -1;
635         else if( sscanf( psz_parser, "PlayResY: %d", &temp ) == 1 )
636             p_sys->i_original_height = ( temp > 0 ) ? temp : -1;
637         else if( sscanf( psz_parser, "Script Type: %8192s", buffer_text ) == 1 )
638         {
639             if( !strcasecmp( buffer_text, "V4.00+" ) ) p_sys->b_ass = VLC_TRUE;
640         }
641         else if( !strncasecmp( psz_parser, "[V4 Styles]", 11 ) )
642             i_section_type = 1;
643         else if( !strncasecmp( psz_parser, "[V4+ Styles]", 12) )
644         {
645             i_section_type = 2;
646             p_sys->b_ass = VLC_TRUE;
647         }
648         else if( !strncasecmp( psz_parser, "[Events]", 8 ) )
649             i_section_type = 4;
650         else if( !strncasecmp( psz_parser, "Style:", 6 ) )
651         {
652             int i_font_size, i_bold, i_italic, i_border, i_outline, i_shadow, i_underline,
653                 i_strikeout, i_scale_x, i_scale_y, i_spacing, i_align, i_margin_l, i_margin_r, i_margin_v;
654
655             char psz_temp_stylename[MAX_LINE+1];
656             char psz_temp_fontname[MAX_LINE+1];
657             char psz_temp_color1[MAX_LINE+1];
658             char psz_temp_color2[MAX_LINE+1];
659             char psz_temp_color3[MAX_LINE+1];
660             char psz_temp_color4[MAX_LINE+1];
661
662             if( i_section_type == 1 ) /* V4 */
663             {
664                 if( sscanf( psz_parser, "Style: %8192[^,],%8192[^,],%d,%8192[^,],%8192[^,],%8192[^,],%8192[^,],%d,%d,%d,%d,%d,%d,%d,%d,%d%*[^\r\n]",
665                     psz_temp_stylename, psz_temp_fontname, &i_font_size,
666                     psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
667                     &i_border, &i_outline, &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 16 )
668                 {
669                     ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
670
671                     p_style->psz_stylename = strdup( psz_temp_stylename );
672                     p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
673                     p_style->font_style.i_font_size = i_font_size;
674
675                     ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, NULL );
676                     ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, NULL );
677                     p_style->font_style.i_outline_color = p_style->font_style.i_shadow_color;
678                     p_style->font_style.i_font_alpha = p_style->font_style.i_outline_alpha = p_style->font_style.i_shadow_alpha = 0x00;
679                     p_style->font_style.i_style_flags = 0;
680                     if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
681                     if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
682
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_shadow;
691                     p_style->font_style.i_outline_width = i_outline;
692
693                     p_style->i_align = 0;
694                     if( i_align == 1 || i_align == 5 || i_align == 9 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
695                     if( i_align == 3 || i_align == 7 || i_align == 11 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
696                     if( i_align < 4 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
697                     else if( i_align < 8 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP; 
698
699                     p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
700                     p_style->i_margin_v = i_margin_v;
701
702                     TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
703                 }
704                 else msg_Warn( p_dec, "SSA v4 styleline parsing failed" );
705             }
706             else if( i_section_type == 2 ) /* V4+ */
707             {
708                 /* Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour,
709                    Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline,
710                    Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
711                 */
712                 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]",
713                     psz_temp_stylename, psz_temp_fontname, &i_font_size,
714                     psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
715                     &i_underline, &i_strikeout, &i_scale_x, &i_scale_y, &i_spacing, &i_border, &i_outline,
716                     &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 21 )
717                 {
718                     ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
719
720                     p_style->psz_stylename = strdup( psz_temp_stylename );
721                     p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
722                     p_style->font_style.i_font_size = i_font_size;
723                     msg_Dbg( p_dec, psz_temp_color1 );
724                     ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, &p_style->font_style.i_font_alpha );
725                     ParseColor( p_dec, psz_temp_color3, &p_style->font_style.i_outline_color, &p_style->font_style.i_outline_alpha );
726                     ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, &p_style->font_style.i_shadow_alpha );
727
728                     p_style->font_style.i_style_flags = 0;
729                     if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
730                     if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
731                     if( i_underline ) p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
732                     if( i_strikeout ) p_style->font_style.i_style_flags |= STYLE_STRIKEOUT;
733                     if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
734                     else if( i_border == 3 )
735                     {
736                         p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
737                         p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
738                         p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
739                     }
740                     p_style->font_style.i_shadow_width  = ( i_border == 1 ) ? i_shadow : 0;
741                     p_style->font_style.i_outline_width = ( i_border == 1 ) ? i_outline : 0;
742                     p_style->font_style.i_spacing = i_spacing;
743                     //p_style->font_style.f_angle = f_angle;
744
745                     p_style->i_align = 0;
746                     if( i_align == 0x1 || i_align == 0x4 || i_align == 0x1 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
747                     if( i_align == 0x3 || i_align == 0x6 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
748                     if( i_align == 0x7 || i_align == 0x8 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP;
749                     if( i_align == 0x1 || i_align == 0x2 || i_align == 0x3 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
750                     p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
751                     p_style->i_margin_v = i_margin_v;
752
753                     /*TODO: Ignored: angle i_scale_x|y (fontscaling), i_encoding */
754                     TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
755                 }
756                 else msg_Dbg( p_dec, "SSA V4+ styleline parsing failed" );
757             }
758         }
759         psz_parser = GotoNextLine( psz_parser );
760     }
761
762 eof:
763     if( psz_header ) free( psz_header );
764     return;
765 }
766
767 static void StripTags( char *psz_text )
768 {
769     int i_left_moves = 0;
770     vlc_bool_t b_inside_tag = VLC_FALSE;
771     int i = 0;
772     int i_tag_start = -1;
773     while( psz_text[ i ] )
774     {
775         if( !b_inside_tag )
776         {
777             if( psz_text[ i ] == '<' )
778             {
779                 b_inside_tag = VLC_TRUE;
780                 i_tag_start = i;
781             }
782             psz_text[ i - i_left_moves ] = psz_text[ i ];
783         }
784         else
785         {
786             if( ( psz_text[ i ] == ' ' ) ||
787                 ( psz_text[ i ] == '\t' ) ||
788                 ( psz_text[ i ] == '\n' ) ||
789                 ( psz_text[ i ] == '\r' ) )
790             {
791                 b_inside_tag = VLC_FALSE;
792                 i_tag_start = -1;
793             }
794             else if( psz_text[ i ] == '>' )
795             {
796                 i_left_moves += i - i_tag_start + 1;
797                 i_tag_start = -1;
798                 b_inside_tag = VLC_FALSE;
799             }
800             else
801             {
802                 psz_text[ i - i_left_moves ] = psz_text[ i ];
803             }
804         }
805         i++;
806     }
807     psz_text[ i - i_left_moves ] = '\0';
808 }