1 /*****************************************************************************
2 * subsdec.c : text subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Samuel Hocevar <sam@zoy.org>
9 * Derk-Jan Hartman <hartman at videolan dot org>
10 * Bernie Purcell <b dot purcell at adbglobal dot com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <vlc_codec.h>
33 #include <vlc_input.h>
36 #include <vlc_filter.h>
37 #include <vlc_image.h>
38 #include <vlc_charset.h>
39 #include <vlc_stream.h>
44 #define NO_BREAKING_SPACE " "
48 ATTRIBUTE_ALIGNMENT = (1 << 0),
49 ATTRIBUTE_X = (1 << 1),
50 ATTRIBUTE_X_PERCENT = (1 << 2),
51 ATTRIBUTE_Y = (1 << 3),
52 ATTRIBUTE_Y_PERCENT = (1 << 4),
63 char * psz_stylename; /* The name of the style, no comma's allowed */
64 text_style_t font_style;
68 int i_margin_percent_h;
69 int i_margin_percent_v;
72 /*****************************************************************************
73 * decoder_sys_t : decoder descriptor
74 *****************************************************************************/
77 vlc_bool_t b_ass; /* The subs are ASS */
78 int i_original_height;
80 int i_align; /* Subtitles alignment on the vout */
81 vlc_iconv_t iconv_handle; /* handle to iconv instance */
82 vlc_bool_t b_autodetect_utf8;
84 ssa_style_t **pp_ssa_styles;
87 image_attach_t **pp_images;
91 /*****************************************************************************
93 *****************************************************************************/
94 static int OpenDecoder ( vlc_object_t * );
95 static void CloseDecoder ( vlc_object_t * );
97 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
98 static subpicture_t *ParseText ( decoder_t *, block_t * );
99 static void ParseSSAHeader ( decoder_t * );
100 static void ParseUSFHeader ( decoder_t * );
101 static void ParseUSFHeaderTags( decoder_t *, xml_reader_t * );
102 static void ParseSSAString ( decoder_t *, char *, subpicture_t * );
103 static subpicture_region_t *ParseUSFString ( decoder_t *, char *, subpicture_t * );
104 static void ParseColor ( decoder_t *, char *, int *, int * );
105 static char *StripTags ( char * );
106 static char *CreateHtmlSubtitle ( char * );
107 static char *CreatePlainText( char * );
108 static int ParseImageAttachments( decoder_t *p_dec );
109 static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec, subpicture_t *p_spu, const char *psz_filename, int i_transparent_color );
111 #define DEFAULT_NAME "Default"
112 #define MAX_LINE 8192
114 /*****************************************************************************
116 *****************************************************************************/
117 static const char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
118 "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
119 "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
120 "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
121 "ISO-8859-6", "CP1256", "MacArabic", "",
122 "ISO-8859-7", "CP1253", "MacGreek", "",
123 "ISO-8859-8", "CP1255", "MacHebrew", "",
124 "ISO-8859-9", "CP1254", "MacTurkish", "",
125 "ISO-8859-13", "CP1257", "",
126 "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
127 "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
128 "ISO-2022-KR", "EUC-KR", "",
129 "MacThai", "KOI8-T", "",
130 "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
131 "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
133 "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
134 "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
135 "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
136 "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
137 "HPROMAN8", "NEXTSTEP" };
139 SSA supports charset selection.
140 The following known charsets are used:
142 0 = Ansi - Western European
147 128 = Japanese (Shift JIS)
150 134 = GB2312 Simplified Chinese
151 136 = Big5 Traditional Chinese
158 204 = Russian (Cyrillic)
160 238 = Eastern European
164 static int pi_justification[] = { 0, 1, 2 };
165 static const char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
167 #define ENCODING_TEXT N_("Subtitles text encoding")
168 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
169 #define ALIGN_TEXT N_("Subtitles justification")
170 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
171 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
172 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
173 "UTF-8 encoding within subtitles files.")
174 #define FORMAT_TEXT N_("Formatted Subtitles")
175 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
176 "VLC partly implements this, but you can choose to disable all formatting.")
180 set_shortname( _("Subtitles"));
181 set_description( _("Text subtitles decoder") );
182 set_capability( "decoder", 50 );
183 set_callbacks( OpenDecoder, CloseDecoder );
184 set_category( CAT_INPUT );
185 set_subcategory( SUBCAT_INPUT_SCODEC );
187 add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
189 change_integer_list( pi_justification, ppsz_justification_text, 0 );
190 add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
191 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
192 change_string_list( ppsz_encodings, 0, 0 );
193 add_bool( "subsdec-autodetect-utf8", VLC_TRUE, NULL,
194 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, VLC_FALSE );
195 add_bool( "subsdec-formatted", VLC_TRUE, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
199 /*****************************************************************************
200 * OpenDecoder: probe the decoder and return score
201 *****************************************************************************
202 * Tries to launch a decoder and return score so that the interface is able
204 *****************************************************************************/
205 static int OpenDecoder( vlc_object_t *p_this )
207 decoder_t *p_dec = (decoder_t*)p_this;
208 decoder_sys_t *p_sys;
211 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
212 p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') &&
213 p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
218 p_dec->pf_decode_sub = DecodeBlock;
220 /* Allocate the memory needed to store the decoder's structure */
221 if( ( p_dec->p_sys = p_sys =
222 (decoder_sys_t *)calloc(1, sizeof(decoder_sys_t)) ) == NULL )
224 msg_Err( p_dec, "out of memory" );
230 p_sys->iconv_handle = (vlc_iconv_t)-1;
231 p_sys->b_autodetect_utf8 = VLC_FALSE;
232 p_sys->b_ass = VLC_FALSE;
233 p_sys->i_original_height = -1;
234 p_sys->i_original_width = -1;
235 TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
236 TAB_INIT( p_sys->i_images, p_sys->pp_images );
238 char *psz_charset = NULL;
239 /* First try demux-specified encoding */
240 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
242 psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
243 msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
244 p_dec->fmt_in.subs.psz_encoding ?: "not specified");
247 /* Second, try configured encoding */
248 if (psz_charset == NULL)
250 psz_charset = var_CreateGetNonEmptyString (p_dec, "subsdec-encoding");
251 if ((psz_charset != NULL) && !strcasecmp (psz_charset, DEFAULT_NAME))
257 msg_Dbg (p_dec, "trying configured character encoding: %s",
258 psz_charset ?: "not specified");
261 /* Third, try "local" encoding with optional UTF-8 autodetection */
262 if (psz_charset == NULL)
264 psz_charset = strdup (GetFallbackEncoding ());
265 msg_Dbg (p_dec, "trying default character encoding: %s",
266 psz_charset ?: "not specified");
268 if (var_CreateGetBool (p_dec, "subsdec-autodetect-utf8"))
270 msg_Dbg (p_dec, "using automatic UTF-8 detection");
271 p_sys->b_autodetect_utf8 = VLC_TRUE;
275 if (psz_charset == NULL)
277 psz_charset = strdup ("UTF-8");
278 msg_Dbg (p_dec, "trying hard-coded character encoding: %s",
279 psz_charset ?: "error");
282 if (psz_charset == NULL)
288 if (strcasecmp (psz_charset, "UTF-8") && strcasecmp (psz_charset, "utf8"))
290 p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
291 if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
292 msg_Err (p_dec, "cannot convert from %s: %s", psz_charset,
297 var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
298 var_Get( p_dec, "subsdec-align", &val );
299 p_sys->i_align = val.i_int;
301 ParseImageAttachments( p_dec );
303 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
305 if( p_dec->fmt_in.i_extra > 0 )
306 ParseSSAHeader( p_dec );
308 else if( p_dec->fmt_in.i_codec == VLC_FOURCC('u','s','f',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
310 if( p_dec->fmt_in.i_extra > 0 )
311 ParseUSFHeader( p_dec );
317 /****************************************************************************
318 * DecodeBlock: the whole thing
319 ****************************************************************************
320 * This function must be fed with complete subtitles units.
321 ****************************************************************************/
322 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
324 subpicture_t *p_spu = NULL;
326 if( !pp_block || *pp_block == NULL ) return NULL;
328 p_spu = ParseText( p_dec, *pp_block );
330 block_Release( *pp_block );
336 /*****************************************************************************
337 * CloseDecoder: clean up the decoder
338 *****************************************************************************/
339 static void CloseDecoder( vlc_object_t *p_this )
341 decoder_t *p_dec = (decoder_t *)p_this;
342 decoder_sys_t *p_sys = p_dec->p_sys;
344 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
345 vlc_iconv_close( p_sys->iconv_handle );
347 if( p_sys->pp_ssa_styles )
350 for( i = 0; i < p_sys->i_ssa_styles; i++ )
352 if( !p_sys->pp_ssa_styles[i] )
355 if( p_sys->pp_ssa_styles[i]->psz_stylename )
356 free( p_sys->pp_ssa_styles[i]->psz_stylename );
357 if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname )
358 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
359 if( p_sys->pp_ssa_styles[i] )
360 free( p_sys->pp_ssa_styles[i] );
362 TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
364 if( p_sys->pp_images )
367 for( i = 0; i < p_sys->i_images; i++ )
369 if( !p_sys->pp_images[i] )
372 if( p_sys->pp_images[i]->p_pic )
373 p_sys->pp_images[i]->p_pic->pf_release( p_sys->pp_images[i]->p_pic );
374 if( p_sys->pp_images[i]->psz_filename )
375 free( p_sys->pp_images[i]->psz_filename );
377 free( p_sys->pp_images[i] );
379 TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
385 /*****************************************************************************
386 * ParseText: parse an text subtitle packet and send it to the video output
387 *****************************************************************************/
388 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
390 decoder_sys_t *p_sys = p_dec->p_sys;
391 subpicture_t *p_spu = NULL;
392 char *psz_subtitle = NULL;
395 /* We cannot display a subpicture with no date */
396 if( p_block->i_pts == 0 )
398 msg_Warn( p_dec, "subtitle without a date" );
402 /* Check validity of packet data */
403 /* An "empty" line containing only \0 can be used to force
404 and ephemer picture from the screen */
405 if( p_block->i_buffer < 1 )
407 msg_Warn( p_dec, "no subtitle data" );
411 /* Should be resiliant against bad subtitles */
412 psz_subtitle = strndup( (const char *)p_block->p_buffer,
414 if( psz_subtitle == NULL )
417 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
419 if (EnsureUTF8( psz_subtitle ) == NULL)
421 msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
422 "Try manually setting a character-encoding "
423 "before you open the file.") );
429 if( p_sys->b_autodetect_utf8 )
431 if( IsUTF8( psz_subtitle ) == NULL )
433 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
434 "disabling UTF-8 subtitles autodetection" );
435 p_sys->b_autodetect_utf8 = VLC_FALSE;
439 if( !p_sys->b_autodetect_utf8 )
441 size_t inbytes_left = strlen( psz_subtitle );
442 size_t outbytes_left = 6 * inbytes_left;
443 char *psz_new_subtitle = malloc( outbytes_left + 1 );
444 char *psz_convert_buffer_out = psz_new_subtitle;
445 const char *psz_convert_buffer_in = psz_subtitle;
447 size_t ret = vlc_iconv( p_sys->iconv_handle,
448 &psz_convert_buffer_in, &inbytes_left,
449 &psz_convert_buffer_out, &outbytes_left );
451 *psz_convert_buffer_out++ = '\0';
452 free( psz_subtitle );
454 if( ( ret == (size_t)(-1) ) || inbytes_left )
456 free( psz_new_subtitle );
457 msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
458 "Try manually setting a character-encoding "
459 "before you open the file.") );
463 psz_subtitle = realloc( psz_new_subtitle,
464 psz_convert_buffer_out - psz_new_subtitle );
468 /* Create the subpicture unit */
469 p_spu = p_dec->pf_spu_buffer_new( p_dec );
472 msg_Warn( p_dec, "can't get spu buffer" );
473 if( psz_subtitle ) free( psz_subtitle );
477 p_spu->b_pausable = VLC_TRUE;
479 /* Create a new subpicture region */
480 memset( &fmt, 0, sizeof(video_format_t) );
481 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
483 fmt.i_width = fmt.i_height = 0;
484 fmt.i_x_offset = fmt.i_y_offset = 0;
485 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
486 if( !p_spu->p_region )
488 msg_Err( p_dec, "cannot allocate SPU region" );
489 if( psz_subtitle ) free( psz_subtitle );
490 p_dec->pf_spu_buffer_del( p_dec, p_spu );
494 /* Decode and format the subpicture unit */
495 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') &&
496 p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') )
498 /* Normal text subs, easy markup */
499 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
500 p_spu->i_x = p_sys->i_align ? 20 : 0;
503 /* Remove formatting from string */
505 p_spu->p_region->psz_text = StripTags( psz_subtitle );
506 p_spu->p_region->psz_html = CreateHtmlSubtitle( psz_subtitle );
508 p_spu->i_start = p_block->i_pts;
509 p_spu->i_stop = p_block->i_pts + p_block->i_length;
510 p_spu->b_ephemer = (p_block->i_length == 0);
511 p_spu->b_absolute = VLC_FALSE;
515 /* Decode SSA/USF strings */
516 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
517 ParseSSAString( p_dec, psz_subtitle, p_spu );
520 p_spu->pf_destroy_region( VLC_OBJECT(p_dec), p_spu->p_region );
521 p_spu->p_region = ParseUSFString( p_dec, psz_subtitle, p_spu );
524 p_spu->i_start = p_block->i_pts;
525 p_spu->i_stop = p_block->i_pts + p_block->i_length;
526 p_spu->b_ephemer = (p_block->i_length == 0);
527 p_spu->b_absolute = VLC_FALSE;
528 p_spu->i_original_picture_width = p_sys->i_original_width;
529 p_spu->i_original_picture_height = p_sys->i_original_height;
531 if( psz_subtitle ) free( psz_subtitle );
536 static char *GrabAttributeValue( const char *psz_attribute,
537 const char *psz_tag_start )
539 if( psz_attribute && psz_tag_start )
541 char *psz_tag_end = strchr( psz_tag_start, '>' );
542 char *psz_found = strcasestr( psz_tag_start, psz_attribute );
546 psz_found += strlen( psz_attribute );
548 if(( *(psz_found++) == '=' ) &&
549 ( *(psz_found++) == '\"' ))
551 if( psz_found < psz_tag_end )
553 int i_len = strcspn( psz_found, "\"" );
554 return strndup( psz_found, i_len );
562 static ssa_style_t *ParseStyle( decoder_sys_t *p_sys, char *psz_subtitle )
564 ssa_style_t *p_style = NULL;
565 char *psz_style = GrabAttributeValue( "style", psz_subtitle );
571 for( i = 0; i < p_sys->i_ssa_styles; i++ )
573 if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
574 p_style = p_sys->pp_ssa_styles[i];
581 static int ParsePositionAttributeList( char *psz_subtitle, int *i_align, int *i_x, int *i_y )
585 char *psz_align = GrabAttributeValue( "alignment", psz_subtitle );
586 char *psz_margin_x = GrabAttributeValue( "horizontal-margin", psz_subtitle );
587 char *psz_margin_y = GrabAttributeValue( "vertical-margin", psz_subtitle );
589 char *psz_relative = GrabAttributeValue( "relative-to", psz_subtitle );
590 char *psz_rotate_x = GrabAttributeValue( "rotate-x", psz_subtitle );
591 char *psz_rotate_y = GrabAttributeValue( "rotate-y", psz_subtitle );
592 char *psz_rotate_z = GrabAttributeValue( "rotate-z", psz_subtitle );
595 *i_align = SUBPICTURE_ALIGN_BOTTOM;
601 if( !strcasecmp( "TopLeft", psz_align ) )
602 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
603 else if( !strcasecmp( "TopCenter", psz_align ) )
604 *i_align = SUBPICTURE_ALIGN_TOP;
605 else if( !strcasecmp( "TopRight", psz_align ) )
606 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
607 else if( !strcasecmp( "MiddleLeft", psz_align ) )
608 *i_align = SUBPICTURE_ALIGN_LEFT;
609 else if( !strcasecmp( "MiddleCenter", psz_align ) )
611 else if( !strcasecmp( "MiddleRight", psz_align ) )
612 *i_align = SUBPICTURE_ALIGN_RIGHT;
613 else if( !strcasecmp( "BottomLeft", psz_align ) )
614 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
615 else if( !strcasecmp( "BottomCenter", psz_align ) )
616 *i_align = SUBPICTURE_ALIGN_BOTTOM;
617 else if( !strcasecmp( "BottomRight", psz_align ) )
618 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
620 i_mask |= ATTRIBUTE_ALIGNMENT;
625 *i_x = atoi( psz_margin_x );
626 if( strchr( psz_margin_x, '%' ) )
627 i_mask |= ATTRIBUTE_X_PERCENT;
629 i_mask |= ATTRIBUTE_X;
631 free( psz_margin_x );
635 *i_y = atoi( psz_margin_y );
636 if( strchr( psz_margin_y, '%' ) )
637 i_mask |= ATTRIBUTE_Y_PERCENT;
639 i_mask |= ATTRIBUTE_Y;
641 free( psz_margin_y );
646 static void SetupPositions( subpicture_region_t *p_region, char *psz_subtitle )
652 i_mask = ParsePositionAttributeList( psz_subtitle, &i_align, &i_x, &i_y );
654 if( i_mask & ATTRIBUTE_ALIGNMENT )
655 p_region->i_align = i_align;
657 /* TODO: Setup % based offsets properly, without adversely affecting
658 * everything else in vlc. Will address with separate patch, to
659 * prevent this one being any more complicated.
661 if( i_mask & ATTRIBUTE_X )
663 else if( i_mask & ATTRIBUTE_X_PERCENT )
666 if( i_mask & ATTRIBUTE_Y )
668 else if( i_mask & ATTRIBUTE_Y_PERCENT )
672 static subpicture_region_t *CreateTextRegion( decoder_t *p_dec,
678 decoder_sys_t *p_sys = p_dec->p_sys;
679 subpicture_region_t *p_text_region;
682 /* Create a new subpicture region */
683 memset( &fmt, 0, sizeof(video_format_t) );
684 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
686 fmt.i_width = fmt.i_height = 0;
687 fmt.i_x_offset = fmt.i_y_offset = 0;
688 p_text_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
690 if( p_text_region != NULL )
692 ssa_style_t *p_style = NULL;
694 p_text_region->psz_text = NULL;
695 p_text_region->psz_html = strndup( psz_subtitle, i_len );
696 if( ! p_text_region->psz_html )
698 msg_Err( p_dec, "out of memory" );
699 p_spu->pf_destroy_region( VLC_OBJECT(p_dec), p_text_region );
703 p_style = ParseStyle( p_sys, p_text_region->psz_html );
708 for( i = 0; i < p_sys->i_ssa_styles; i++ )
710 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
711 p_style = p_sys->pp_ssa_styles[i];
717 msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename );
719 p_text_region->p_style = &p_style->font_style;
720 p_text_region->i_align = p_style->i_align;
722 /* TODO: Setup % based offsets properly, without adversely affecting
723 * everything else in vlc. Will address with separate patch,
724 * to prevent this one being any more complicated.
726 * p_style->i_margin_percent_h;
727 * p_style->i_margin_percent_v;
729 p_text_region->i_x = p_style->i_margin_h;
730 p_text_region->i_y = p_style->i_margin_v;
735 p_text_region->i_align = SUBPICTURE_ALIGN_BOTTOM | i_sys_align;
736 p_text_region->i_x = i_sys_align ? 20 : 0;
737 p_text_region->i_y = 10;
739 /* Look for position arguments which may override the style-based
742 SetupPositions( p_text_region, psz_subtitle );
744 p_text_region->p_next = NULL;
746 return p_text_region;
749 static subpicture_region_t *ParseUSFString( decoder_t *p_dec, char *psz_subtitle, subpicture_t *p_spu_in )
751 decoder_sys_t *p_sys = p_dec->p_sys;
752 subpicture_t *p_spu = p_spu_in;
753 subpicture_region_t *p_region_first = NULL;
754 subpicture_region_t *p_region_upto = p_region_first;
756 while( *psz_subtitle )
758 if( *psz_subtitle == '<' )
760 char *psz_end = NULL;
762 if(( !strncasecmp( psz_subtitle, "<text ", 6 )) ||
763 ( !strncasecmp( psz_subtitle, "<text>", 6 )))
765 psz_end = strcasestr( psz_subtitle, "</text>" );
769 subpicture_region_t *p_text_region;
771 psz_end += strcspn( psz_end, ">" ) + 1;
773 p_text_region = CreateTextRegion( p_dec,
776 psz_end - psz_subtitle,
780 p_text_region->psz_text = CreatePlainText( p_text_region->psz_html );
782 if( !p_region_first )
784 p_region_first = p_region_upto = p_text_region;
786 else if( p_text_region )
788 p_region_upto->p_next = p_text_region;
789 p_region_upto = p_region_upto->p_next;
793 else if(( !strncasecmp( psz_subtitle, "<karaoke ", 9 )) ||
794 ( !strncasecmp( psz_subtitle, "<karaoke>", 9 )))
796 psz_end = strcasestr( psz_subtitle, "</karaoke>" );
800 subpicture_region_t *p_text_region;
802 psz_end += strcspn( psz_end, ">" ) + 1;
804 p_text_region = CreateTextRegion( p_dec,
807 psz_end - psz_subtitle,
810 if( !p_region_first )
812 p_region_first = p_region_upto = p_text_region;
814 else if( p_text_region )
816 p_region_upto->p_next = p_text_region;
817 p_region_upto = p_region_upto->p_next;
821 else if(( !strncasecmp( psz_subtitle, "<image ", 7 )) ||
822 ( !strncasecmp( psz_subtitle, "<image>", 7 )))
824 subpicture_region_t *p_image_region = NULL;
826 char *psz_end = strcasestr( psz_subtitle, "</image>" );
827 char *psz_content = strchr( psz_subtitle, '>' );
828 int i_transparent = -1;
830 /* If a colorkey parameter is specified, then we have to map
831 * that index in the picture through as transparent (it is
832 * required by the USF spec but is also recommended that if the
833 * creator really wants a transparent colour that they use a
834 * type like PNG that properly supports it; this goes doubly
835 * for VLC because the pictures are stored internally in YUV
836 * and the resulting colour-matching may not produce the
839 char *psz_tmp = GrabAttributeValue( "colorkey", psz_subtitle );
842 if( *psz_tmp == '#' )
843 i_transparent = strtol( psz_tmp + 1, NULL, 16 ) & 0x00ffffff;
846 if( psz_content && ( psz_content < psz_end ) )
848 char *psz_filename = strndup( &psz_content[1], psz_end - &psz_content[1] );
851 p_image_region = LoadEmbeddedImage( p_dec, p_spu, psz_filename, i_transparent );
852 free( psz_filename );
856 if( psz_end ) psz_end += strcspn( psz_end, ">" ) + 1;
860 SetupPositions( p_image_region, psz_subtitle );
862 p_image_region->p_next = NULL;
863 p_image_region->psz_text = NULL;
864 p_image_region->psz_html = NULL;
867 if( !p_region_first )
869 p_region_first = p_region_upto = p_image_region;
871 else if( p_image_region )
873 p_region_upto->p_next = p_image_region;
874 p_region_upto = p_region_upto->p_next;
878 psz_subtitle = psz_end - 1;
880 psz_subtitle += strcspn( psz_subtitle, ">" );
886 return p_region_first;
889 static void ParseSSAString( decoder_t *p_dec, char *psz_subtitle, subpicture_t *p_spu_in )
891 /* We expect MKV formatted SSA:
892 * ReadOrder, Layer, Style, CharacterName, MarginL, MarginR,
893 * MarginV, Effect, Text */
894 decoder_sys_t *p_sys = p_dec->p_sys;
895 subpicture_t *p_spu = p_spu_in;
896 ssa_style_t *p_style = NULL;
897 char *psz_new_subtitle = NULL;
898 char *psz_buffer_sub = NULL;
899 char *psz_style = NULL;
900 char *psz_style_start = NULL;
901 char *psz_style_end = NULL;
902 int i_text = 0, i_comma = 0, i_strlen = 0, i;
903 int i_margin_l = 0, i_margin_r = 0, i_margin_v = 0;
905 psz_buffer_sub = psz_subtitle;
907 p_spu->p_region->psz_html = NULL;
910 while( i_comma < 8 && *psz_buffer_sub != '\0' )
912 if( *psz_buffer_sub == ',' )
916 psz_style_start = &psz_buffer_sub[1];
917 else if( i_comma == 3 )
918 psz_style_end = &psz_buffer_sub[0];
919 else if( i_comma == 4 )
920 i_margin_l = (int)strtol( &psz_buffer_sub[1], NULL, 10 );
921 else if( i_comma == 5 )
922 i_margin_r = (int)strtol( &psz_buffer_sub[1], NULL, 10 );
923 else if( i_comma == 6 )
924 i_margin_v = (int)strtol( &psz_buffer_sub[1], NULL, 10 );
929 if( *psz_buffer_sub == '\0' && i_comma == 8 )
931 msg_Dbg( p_dec, "couldn't find all fields in this SSA line" );
935 psz_new_subtitle = malloc( strlen( psz_buffer_sub ) + 1);
937 while( psz_buffer_sub[0] != '\0' )
939 if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'n' )
941 psz_new_subtitle[i_text] = ' ';
945 else if( psz_buffer_sub[0] == '\\' && psz_buffer_sub[1] == 'N' )
947 psz_new_subtitle[i_text] = '\n';
951 else if( psz_buffer_sub[0] == '{' &&
952 psz_buffer_sub[1] == '\\' )
954 /* SSA control code */
955 while( psz_buffer_sub[0] != '\0' &&
956 psz_buffer_sub[0] != '}' )
964 psz_new_subtitle[i_text] = psz_buffer_sub[0];
969 psz_new_subtitle[i_text] = '\0';
971 i_strlen = __MAX( psz_style_end - psz_style_start, 0);
972 psz_style = strndup( psz_style_start, i_strlen );
974 for( i = 0; i < p_sys->i_ssa_styles; i++ )
976 if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
977 p_style = p_sys->pp_ssa_styles[i];
979 if( psz_style ) free( psz_style );
981 p_spu->p_region->psz_text = psz_new_subtitle;
982 if( p_style == NULL )
984 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
985 p_spu->i_x = p_sys->i_align ? 20 : 0;
990 msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename);
991 p_spu->p_region->p_style = &p_style->font_style;
992 p_spu->p_region->i_align = p_style->i_align;
993 if( p_style->i_align & SUBPICTURE_ALIGN_LEFT )
995 p_spu->i_x = (i_margin_l) ? i_margin_l : p_style->i_margin_h;
997 else if( p_style->i_align & SUBPICTURE_ALIGN_RIGHT )
999 p_spu->i_x = (i_margin_r) ? i_margin_r : p_style->i_margin_h;
1001 p_spu->i_y = (i_margin_v) ? i_margin_v : p_style->i_margin_v;
1005 static char* GotoNextLine( char *psz_text )
1007 char *p_newline = psz_text;
1009 while( p_newline[0] != '\0' )
1011 if( p_newline[0] == '\n' || p_newline[0] == '\r' )
1014 while( p_newline[0] == '\n' || p_newline[0] == '\r' )
1023 /*****************************************************************************
1024 * ParseColor: SSA stores color in BBGGRR, in ASS it uses AABBGGRR
1025 * The string value in the string can be a pure integer, or hexadecimal &HBBGGRR
1026 *****************************************************************************/
1027 static void ParseColor( decoder_t *p_dec, char *psz_color, int *pi_color, int *pi_alpha )
1030 if( !strncasecmp( psz_color, "&H", 2 ) )
1032 /* textual HEX representation */
1033 i_color = (int) strtol( psz_color+2, NULL, 16 );
1035 else i_color = (int) strtol( psz_color, NULL, 0 );
1038 *pi_color |= ( ( i_color & 0x000000FF ) << 16 ); /* Red */
1039 *pi_color |= ( ( i_color & 0x0000FF00 ) ); /* Green */
1040 *pi_color |= ( ( i_color & 0x00FF0000 ) >> 16 ); /* Blue */
1042 if( pi_alpha != NULL )
1043 *pi_alpha = ( i_color & 0xFF000000 ) >> 24;
1046 static int ParseImageAttachments( decoder_t *p_dec )
1048 decoder_sys_t *p_sys = p_dec->p_sys;
1049 input_attachment_t **pp_attachments;
1050 int i_attachments_cnt;
1053 if( VLC_SUCCESS != decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments_cnt ))
1054 return VLC_EGENERIC;
1056 for( k = 0; k < i_attachments_cnt; k++ )
1058 input_attachment_t *p_attach = pp_attachments[k];
1060 vlc_fourcc_t type = 0;
1062 if( ( !strcmp( p_attach->psz_mime, "image/bmp" ) ) || /* BMP */
1063 ( !strcmp( p_attach->psz_mime, "image/x-bmp" ) ) ||
1064 ( !strcmp( p_attach->psz_mime, "image/x-bitmap" ) ) ||
1065 ( !strcmp( p_attach->psz_mime, "image/x-ms-bmp" ) ) )
1067 type = VLC_FOURCC('b','m','p',' ');
1069 else if( ( !strcmp( p_attach->psz_mime, "image/x-portable-anymap" ) ) || /* PNM */
1070 ( !strcmp( p_attach->psz_mime, "image/x-portable-bitmap" ) ) || /* PBM */
1071 ( !strcmp( p_attach->psz_mime, "image/x-portable-graymap" ) ) || /* PGM */
1072 ( !strcmp( p_attach->psz_mime, "image/x-portable-pixmap" ) ) ) /* PPM */
1074 type = VLC_FOURCC('p','n','m',' ');
1076 else if ( !strcmp( p_attach->psz_mime, "image/gif" ) ) /* GIF */
1077 type = VLC_FOURCC('g','i','f',' ');
1078 else if ( !strcmp( p_attach->psz_mime, "image/jpeg" ) ) /* JPG, JPEG */
1079 type = VLC_FOURCC('j','p','e','g');
1080 else if ( !strcmp( p_attach->psz_mime, "image/pcx" ) ) /* PCX */
1081 type = VLC_FOURCC('p','c','x',' ');
1082 else if ( !strcmp( p_attach->psz_mime, "image/png" ) ) /* PNG */
1083 type = VLC_FOURCC('p','n','g',' ');
1084 else if ( !strcmp( p_attach->psz_mime, "image/tiff" ) ) /* TIF, TIFF */
1085 type = VLC_FOURCC('t','i','f','f');
1086 else if ( !strcmp( p_attach->psz_mime, "image/x-tga" ) ) /* TGA */
1087 type = VLC_FOURCC('t','g','a',' ');
1088 else if ( !strcmp( p_attach->psz_mime, "image/x-xpixmap") ) /* XPM */
1089 type = VLC_FOURCC('x','p','m',' ');
1091 if( ( type != 0 ) &&
1092 ( p_attach->i_data > 0 ) &&
1093 ( p_attach->p_data != NULL ) )
1095 picture_t *p_pic = NULL;
1096 image_handler_t *p_image;
1098 p_image = image_HandlerCreate( p_dec );
1099 if( p_image != NULL )
1103 p_block = block_New( p_image->p_parent, p_attach->i_data );
1105 if( p_block != NULL )
1107 video_format_t fmt_in;
1108 video_format_t fmt_out;
1110 memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data );
1112 memset( &fmt_in, 0, sizeof( video_format_t));
1113 memset( &fmt_out, 0, sizeof( video_format_t));
1115 fmt_in.i_chroma = type;
1116 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
1118 /* Find a suitable decoder module */
1119 if( module_Exists( p_dec, "SDL Image decoder" ) )
1121 /* ffmpeg thinks it can handle bmp properly but it can't (at least
1122 * not all of them), so use sdl_image if it is available */
1126 var_Create( p_dec, "codec", VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
1127 val.psz_string = (char*) "sdl_image";
1128 var_Set( p_dec, "codec", val );
1131 p_pic = image_Read( p_image, p_block, &fmt_in, &fmt_out );
1132 var_Destroy( p_dec, "codec" );
1135 image_HandlerDelete( p_image );
1139 image_attach_t *p_picture = malloc( sizeof(image_attach_t) );
1143 p_picture->psz_filename = strdup( p_attach->psz_name );
1144 p_picture->p_pic = p_pic;
1146 TAB_APPEND( p_sys->i_images, p_sys->pp_images, p_picture );
1150 vlc_input_attachment_Delete( pp_attachments[ k ] );
1152 free( pp_attachments );
1157 /*****************************************************************************
1158 * ParseUSFHeader: Retrieve global formatting information etc
1159 *****************************************************************************/
1160 static void ParseUSFHeader( decoder_t *p_dec )
1162 stream_t *p_sub = NULL;
1163 xml_t *p_xml = NULL;
1164 xml_reader_t *p_xml_reader = NULL;
1166 p_sub = stream_MemoryNew( VLC_OBJECT(p_dec),
1167 p_dec->fmt_in.p_extra,
1168 p_dec->fmt_in.i_extra,
1173 p_xml = xml_Create( p_dec );
1176 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
1179 /* Look for Root Node */
1180 if( xml_ReaderRead( p_xml_reader ) == 1 )
1182 char *psz_node = xml_ReaderName( p_xml_reader );
1184 if( !strcasecmp( "usfsubtitles", psz_node ) )
1185 ParseUSFHeaderTags( p_dec, p_xml_reader );
1190 xml_ReaderDelete( p_xml, p_xml_reader );
1192 xml_Delete( p_xml );
1194 stream_Delete( p_sub );
1197 static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
1199 decoder_sys_t *p_sys = p_dec->p_sys;
1201 ssa_style_t *p_style = NULL;
1202 int i_style_level = 0;
1203 int i_metadata_level = 0;
1205 while ( xml_ReaderRead( p_xml_reader ) == 1 )
1207 switch ( xml_ReaderNodeType( p_xml_reader ) )
1209 case XML_READER_TEXT:
1210 case XML_READER_NONE:
1212 case XML_READER_ENDELEM:
1213 psz_node = xml_ReaderName( p_xml_reader );
1217 switch (i_style_level)
1220 if( !strcasecmp( "metadata", psz_node ) && (i_metadata_level == 1) )
1226 if( !strcasecmp( "styles", psz_node ) )
1232 if( !strcasecmp( "style", psz_node ) )
1234 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
1244 case XML_READER_STARTELEM:
1245 psz_node = xml_ReaderName( p_xml_reader );
1250 if( !strcasecmp( "metadata", psz_node ) && (i_style_level == 0) )
1254 else if( !strcasecmp( "resolution", psz_node ) && (i_metadata_level == 1) )
1256 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
1258 char *psz_name = xml_ReaderName ( p_xml_reader );
1259 char *psz_value = xml_ReaderValue ( p_xml_reader );
1261 if( psz_name && psz_value )
1263 if( !strcasecmp( "x", psz_name ) )
1264 p_sys->i_original_width = atoi( psz_value );
1265 else if( !strcasecmp( "y", psz_name ) )
1266 p_sys->i_original_height = atoi( psz_value );
1268 if( psz_name ) free( psz_name );
1269 if( psz_value ) free( psz_value );
1272 else if( !strcasecmp( "styles", psz_node ) && (i_style_level == 0) )
1276 else if( !strcasecmp( "style", psz_node ) && (i_style_level == 1) )
1280 p_style = calloc( 1, sizeof(ssa_style_t) );
1283 msg_Err( p_dec, "out of memory" );
1287 /* All styles are supposed to default to Default, and then
1288 * one or more settings are over-ridden.
1289 * At the moment this only effects styles defined AFTER
1290 * Default in the XML
1293 for( i = 0; i < p_sys->i_ssa_styles; i++ )
1295 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
1297 ssa_style_t *p_default_style = p_sys->pp_ssa_styles[i];
1299 memcpy( p_style, p_default_style, sizeof( ssa_style_t ) );
1300 p_style->font_style.psz_fontname = strdup( p_style->font_style.psz_fontname );
1301 p_style->psz_stylename = NULL;
1305 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
1307 char *psz_name = xml_ReaderName ( p_xml_reader );
1308 char *psz_value = xml_ReaderValue ( p_xml_reader );
1310 if( psz_name && psz_value )
1312 if( !strcasecmp( "name", psz_name ) )
1313 p_style->psz_stylename = strdup( psz_value);
1315 if( psz_name ) free( psz_name );
1316 if( psz_value ) free( psz_value );
1319 else if( !strcasecmp( "fontstyle", psz_node ) && (i_style_level == 2) )
1321 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
1323 char *psz_name = xml_ReaderName ( p_xml_reader );
1324 char *psz_value = xml_ReaderValue ( p_xml_reader );
1326 if( psz_name && psz_value )
1328 if( !strcasecmp( "face", psz_name ) )
1330 if( p_style->font_style.psz_fontname ) free( p_style->font_style.psz_fontname );
1331 p_style->font_style.psz_fontname = strdup( psz_value );
1333 else if( !strcasecmp( "size", psz_name ) )
1335 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
1337 int i_value = atoi( psz_value );
1339 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
1340 p_style->font_style.i_font_size += ( i_value * p_style->font_style.i_font_size ) / 10;
1341 else if( i_value < -5 )
1342 p_style->font_style.i_font_size = - i_value;
1343 else if( i_value > 5 )
1344 p_style->font_style.i_font_size = i_value;
1347 p_style->font_style.i_font_size = atoi( psz_value );
1349 else if( !strcasecmp( "italic", psz_name ) )
1351 if( !strcasecmp( "yes", psz_value ))
1352 p_style->font_style.i_style_flags |= STYLE_ITALIC;
1354 p_style->font_style.i_style_flags &= ~STYLE_ITALIC;
1356 else if( !strcasecmp( "weight", psz_name ) )
1358 if( !strcasecmp( "bold", psz_value ))
1359 p_style->font_style.i_style_flags |= STYLE_BOLD;
1361 p_style->font_style.i_style_flags &= ~STYLE_BOLD;
1363 else if( !strcasecmp( "underline", psz_name ) )
1365 if( !strcasecmp( "yes", psz_value ))
1366 p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
1368 p_style->font_style.i_style_flags &= ~STYLE_UNDERLINE;
1370 else if( !strcasecmp( "color", psz_name ) )
1372 if( *psz_value == '#' )
1374 unsigned long col = strtol(psz_value+1, NULL, 16);
1375 p_style->font_style.i_font_color = (col & 0x00ffffff);
1376 p_style->font_style.i_font_alpha = (col >> 24) & 0xff;
1379 else if( !strcasecmp( "outline-color", psz_name ) )
1381 if( *psz_value == '#' )
1383 unsigned long col = strtol(psz_value+1, NULL, 16);
1384 p_style->font_style.i_outline_color = (col & 0x00ffffff);
1385 p_style->font_style.i_outline_alpha = (col >> 24) & 0xff;
1388 else if( !strcasecmp( "outline-level", psz_name ) )
1390 p_style->font_style.i_outline_width = atoi( psz_value );
1392 else if( !strcasecmp( "shadow-color", psz_name ) )
1394 if( *psz_value == '#' )
1396 unsigned long col = strtol(psz_value+1, NULL, 16);
1397 p_style->font_style.i_shadow_color = (col & 0x00ffffff);
1398 p_style->font_style.i_shadow_alpha = (col >> 24) & 0xff;
1401 else if( !strcasecmp( "shadow-level", psz_name ) )
1403 p_style->font_style.i_shadow_width = atoi( psz_value );
1405 else if( !strcasecmp( "back-color", psz_name ) )
1407 if( *psz_value == '#' )
1409 unsigned long col = strtol(psz_value+1, NULL, 16);
1410 p_style->font_style.i_karaoke_background_color = (col & 0x00ffffff);
1411 p_style->font_style.i_karaoke_background_alpha = (col >> 24) & 0xff;
1414 else if( !strcasecmp( "spacing", psz_name ) )
1416 p_style->font_style.i_spacing = atoi( psz_value );
1419 if( psz_name ) free( psz_name );
1420 if( psz_value ) free( psz_value );
1423 else if( !strcasecmp( "position", psz_node ) && (i_style_level == 2) )
1425 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
1427 char *psz_name = xml_ReaderName ( p_xml_reader );
1428 char *psz_value = xml_ReaderValue ( p_xml_reader );
1430 if( psz_name && psz_value )
1432 if( !strcasecmp( "alignment", psz_name ) )
1434 if( !strcasecmp( "TopLeft", psz_value ) )
1435 p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
1436 else if( !strcasecmp( "TopCenter", psz_value ) )
1437 p_style->i_align = SUBPICTURE_ALIGN_TOP;
1438 else if( !strcasecmp( "TopRight", psz_value ) )
1439 p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
1440 else if( !strcasecmp( "MiddleLeft", psz_value ) )
1441 p_style->i_align = SUBPICTURE_ALIGN_LEFT;
1442 else if( !strcasecmp( "MiddleCenter", psz_value ) )
1443 p_style->i_align = 0;
1444 else if( !strcasecmp( "MiddleRight", psz_value ) )
1445 p_style->i_align = SUBPICTURE_ALIGN_RIGHT;
1446 else if( !strcasecmp( "BottomLeft", psz_value ) )
1447 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
1448 else if( !strcasecmp( "BottomCenter", psz_value ) )
1449 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM;
1450 else if( !strcasecmp( "BottomRight", psz_value ) )
1451 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
1453 else if( !strcasecmp( "horizontal-margin", psz_name ) )
1455 if( strchr( psz_value, '%' ) )
1457 p_style->i_margin_h = 0;
1458 p_style->i_margin_percent_h = atoi( psz_value );
1462 p_style->i_margin_h = atoi( psz_value );
1463 p_style->i_margin_percent_h = 0;
1466 else if( !strcasecmp( "vertical-margin", psz_name ) )
1468 if( strchr( psz_value, '%' ) )
1470 p_style->i_margin_v = 0;
1471 p_style->i_margin_percent_v = atoi( psz_value );
1475 p_style->i_margin_v = atoi( psz_value );
1476 p_style->i_margin_percent_v = 0;
1480 if( psz_name ) free( psz_name );
1481 if( psz_value ) free( psz_value );
1489 if( p_style ) free( p_style );
1491 /*****************************************************************************
1492 * ParseSSAHeader: Retrieve global formatting information etc
1493 *****************************************************************************/
1494 static void ParseSSAHeader( decoder_t *p_dec )
1496 decoder_sys_t *p_sys = p_dec->p_sys;
1497 char *psz_parser = NULL;
1498 char *psz_header = malloc( p_dec->fmt_in.i_extra+1 );
1499 int i_section_type = 1;
1501 memcpy( psz_header, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
1502 psz_header[ p_dec->fmt_in.i_extra] = '\0';
1504 /* Handle [Script Info] section */
1505 psz_parser = strcasestr( psz_header, "[Script Info]" );
1506 if( psz_parser == NULL ) goto eof;
1508 psz_parser = GotoNextLine( psz_parser );
1510 while( psz_parser[0] != '\0' )
1513 char buffer_text[MAX_LINE + 1];
1515 if( psz_parser[0] == '!' || psz_parser[0] == ';' ) /* comment */;
1516 else if( sscanf( psz_parser, "PlayResX: %d", &temp ) == 1 )
1517 p_sys->i_original_width = ( temp > 0 ) ? temp : -1;
1518 else if( sscanf( psz_parser, "PlayResY: %d", &temp ) == 1 )
1519 p_sys->i_original_height = ( temp > 0 ) ? temp : -1;
1520 else if( sscanf( psz_parser, "Script Type: %8192s", buffer_text ) == 1 )
1522 if( !strcasecmp( buffer_text, "V4.00+" ) ) p_sys->b_ass = VLC_TRUE;
1524 else if( !strncasecmp( psz_parser, "[V4 Styles]", 11 ) )
1526 else if( !strncasecmp( psz_parser, "[V4+ Styles]", 12) )
1529 p_sys->b_ass = VLC_TRUE;
1531 else if( !strncasecmp( psz_parser, "[Events]", 8 ) )
1533 else if( !strncasecmp( psz_parser, "Style:", 6 ) )
1535 int i_font_size, i_bold, i_italic, i_border, i_outline, i_shadow, i_underline,
1536 i_strikeout, i_scale_x, i_scale_y, i_spacing, i_align, i_margin_l, i_margin_r, i_margin_v;
1538 char psz_temp_stylename[MAX_LINE+1];
1539 char psz_temp_fontname[MAX_LINE+1];
1540 char psz_temp_color1[MAX_LINE+1];
1541 char psz_temp_color2[MAX_LINE+1];
1542 char psz_temp_color3[MAX_LINE+1];
1543 char psz_temp_color4[MAX_LINE+1];
1545 if( i_section_type == 1 ) /* V4 */
1547 if( sscanf( psz_parser, "Style: %8192[^,],%8192[^,],%d,%8192[^,],%8192[^,],%8192[^,],%8192[^,],%d,%d,%d,%d,%d,%d,%d,%d,%d%*[^\r\n]",
1548 psz_temp_stylename, psz_temp_fontname, &i_font_size,
1549 psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
1550 &i_border, &i_outline, &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 16 )
1552 ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
1554 p_style->psz_stylename = strdup( psz_temp_stylename );
1555 p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
1556 p_style->font_style.i_font_size = i_font_size;
1558 ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, NULL );
1559 ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, NULL );
1560 p_style->font_style.i_outline_color = p_style->font_style.i_shadow_color;
1561 p_style->font_style.i_font_alpha = p_style->font_style.i_outline_alpha = p_style->font_style.i_shadow_alpha = 0x00;
1562 p_style->font_style.i_style_flags = 0;
1563 if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
1564 if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
1566 if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
1567 else if( i_border == 3 )
1569 p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
1570 p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
1571 p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
1573 p_style->font_style.i_shadow_width = i_shadow;
1574 p_style->font_style.i_outline_width = i_outline;
1576 p_style->i_align = 0;
1577 if( i_align == 1 || i_align == 5 || i_align == 9 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
1578 if( i_align == 3 || i_align == 7 || i_align == 11 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
1579 if( i_align < 4 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
1580 else if( i_align < 8 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP;
1582 p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
1583 p_style->i_margin_v = i_margin_v;
1584 p_style->i_margin_percent_h = 0;
1585 p_style->i_margin_percent_v = 0;
1587 p_style->font_style.i_karaoke_background_color = 0xffffff;
1588 p_style->font_style.i_karaoke_background_alpha = 0xff;
1590 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
1592 else msg_Warn( p_dec, "SSA v4 styleline parsing failed" );
1594 else if( i_section_type == 2 ) /* V4+ */
1596 /* Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour,
1597 Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline,
1598 Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
1600 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]",
1601 psz_temp_stylename, psz_temp_fontname, &i_font_size,
1602 psz_temp_color1, psz_temp_color2, psz_temp_color3, psz_temp_color4, &i_bold, &i_italic,
1603 &i_underline, &i_strikeout, &i_scale_x, &i_scale_y, &i_spacing, &i_border, &i_outline,
1604 &i_shadow, &i_align, &i_margin_l, &i_margin_r, &i_margin_v ) == 21 )
1606 ssa_style_t *p_style = malloc( sizeof(ssa_style_t) );
1608 p_style->psz_stylename = strdup( psz_temp_stylename );
1609 p_style->font_style.psz_fontname = strdup( psz_temp_fontname );
1610 p_style->font_style.i_font_size = i_font_size;
1611 msg_Dbg( p_dec, psz_temp_color1 );
1612 ParseColor( p_dec, psz_temp_color1, &p_style->font_style.i_font_color, &p_style->font_style.i_font_alpha );
1613 ParseColor( p_dec, psz_temp_color3, &p_style->font_style.i_outline_color, &p_style->font_style.i_outline_alpha );
1614 ParseColor( p_dec, psz_temp_color4, &p_style->font_style.i_shadow_color, &p_style->font_style.i_shadow_alpha );
1616 p_style->font_style.i_style_flags = 0;
1617 if( i_bold ) p_style->font_style.i_style_flags |= STYLE_BOLD;
1618 if( i_italic ) p_style->font_style.i_style_flags |= STYLE_ITALIC;
1619 if( i_underline ) p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
1620 if( i_strikeout ) p_style->font_style.i_style_flags |= STYLE_STRIKEOUT;
1621 if( i_border == 1 ) p_style->font_style.i_style_flags |= (STYLE_ITALIC | STYLE_OUTLINE);
1622 else if( i_border == 3 )
1624 p_style->font_style.i_style_flags |= STYLE_BACKGROUND;
1625 p_style->font_style.i_background_color = p_style->font_style.i_shadow_color;
1626 p_style->font_style.i_background_alpha = p_style->font_style.i_shadow_alpha;
1628 p_style->font_style.i_shadow_width = ( i_border == 1 ) ? i_shadow : 0;
1629 p_style->font_style.i_outline_width = ( i_border == 1 ) ? i_outline : 0;
1630 p_style->font_style.i_spacing = i_spacing;
1631 //p_style->font_style.f_angle = f_angle;
1633 p_style->i_align = 0;
1634 if( i_align == 0x1 || i_align == 0x4 || i_align == 0x7 ) p_style->i_align |= SUBPICTURE_ALIGN_LEFT;
1635 if( i_align == 0x3 || i_align == 0x6 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_RIGHT;
1636 if( i_align == 0x7 || i_align == 0x8 || i_align == 0x9 ) p_style->i_align |= SUBPICTURE_ALIGN_TOP;
1637 if( i_align == 0x1 || i_align == 0x2 || i_align == 0x3 ) p_style->i_align |= SUBPICTURE_ALIGN_BOTTOM;
1638 p_style->i_margin_h = ( p_style->i_align & SUBPICTURE_ALIGN_RIGHT ) ? i_margin_r : i_margin_l;
1639 p_style->i_margin_v = i_margin_v;
1640 p_style->i_margin_percent_h = 0;
1641 p_style->i_margin_percent_v = 0;
1643 p_style->font_style.i_karaoke_background_color = 0xffffff;
1644 p_style->font_style.i_karaoke_background_alpha = 0xff;
1646 /*TODO: Ignored: angle i_scale_x|y (fontscaling), i_encoding */
1647 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
1649 else msg_Dbg( p_dec, "SSA V4+ styleline parsing failed" );
1652 psz_parser = GotoNextLine( psz_parser );
1656 if( psz_header ) free( psz_header );
1660 /* Function now handles tags which has attribute values, and tries
1661 * to deal with &' commands too. It no longer modifies the string
1662 * in place, so that the original text can be reused
1664 static char *StripTags( char *psz_subtitle )
1666 char *psz_text_start;
1669 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
1670 if( !psz_text_start )
1673 while( *psz_subtitle )
1675 if( *psz_subtitle == '<' )
1677 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
1680 psz_subtitle += strcspn( psz_subtitle, ">" );
1682 else if( *psz_subtitle == '&' )
1684 if( !strncasecmp( psz_subtitle, "<", 4 ))
1687 psz_subtitle += strcspn( psz_subtitle, ";" );
1689 else if( !strncasecmp( psz_subtitle, ">", 4 ))
1692 psz_subtitle += strcspn( psz_subtitle, ";" );
1694 else if( !strncasecmp( psz_subtitle, "&", 5 ))
1697 psz_subtitle += strcspn( psz_subtitle, ";" );
1699 else if( !strncasecmp( psz_subtitle, """, 6 ))
1702 psz_subtitle += strcspn( psz_subtitle, ";" );
1706 /* Assume it is just a normal ampersand */
1712 *psz_text++ = *psz_subtitle;
1718 psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
1720 return psz_text_start;
1723 /* Try to respect any style tags present in the subtitle string. The main
1724 * problem here is a lack of adequate specs for the subtitle formats.
1725 * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
1726 * SAMI has a detailed spec, but extensive rework is needed in the demux
1727 * code to prevent all this style information being excised, as it presently
1729 * That leaves the others - none of which were (I guess) originally intended
1730 * to be carrying style information. Over time people have used them that way.
1731 * In the absence of specifications from which to work, the tags supported
1732 * have been restricted to the simple set permitted by the USF DTD, ie. :
1733 * Basic: <br>, <i>, <b>, <u>
1745 * There is also the further restriction that the subtitle be well-formed
1746 * as an XML entity, ie. the HTML sentence:
1747 * <b><i>Bold and Italics</b></i>
1748 * doesn't qualify because the tags aren't nested one inside the other.
1749 * <text> tags are automatically added to the output to ensure
1751 * If the text doesn't qualify for any reason, a NULL string is
1752 * returned, and the rendering engine will fall back to the
1753 * plain text version of the subtitle.
1755 static char *CreateHtmlSubtitle( char *psz_subtitle )
1757 char psz_tagStack[ 100 ];
1758 size_t i_buf_size = strlen( psz_subtitle ) + 100;
1759 char *psz_html_start = malloc( i_buf_size );
1761 psz_tagStack[ 0 ] = '\0';
1763 if( psz_html_start != NULL )
1765 char *psz_html = psz_html_start;
1767 strcpy( psz_html, "<text>" );
1770 while( *psz_subtitle )
1772 if( *psz_subtitle == '\n' )
1774 strcpy( psz_html, "<br/>" );
1778 else if( *psz_subtitle == '<' )
1780 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
1782 strcpy( psz_html, "<br/>" );
1786 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
1788 strcpy( psz_html, "<b>" );
1789 strcat( psz_tagStack, "b" );
1793 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
1795 strcpy( psz_html, "<i>" );
1796 strcat( psz_tagStack, "i" );
1800 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
1802 strcpy( psz_html, "<u>" );
1803 strcat( psz_tagStack, "u" );
1807 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
1809 const char *psz_attribs[] = { "face=\"", "family=\"", "size=\"",
1810 "color=\"", "outline-color=\"", "shadow-color=\"",
1811 "outline-level=\"", "shadow-level=\"", "back-color=\"",
1814 strcpy( psz_html, "<font " );
1815 strcat( psz_tagStack, "f" );
1819 while( *psz_subtitle != '>' )
1823 for( k=0; psz_attribs[ k ]; k++ )
1825 int i_len = strlen( psz_attribs[ k ] );
1827 if( !strncasecmp( psz_subtitle, psz_attribs[ k ], i_len ))
1829 i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
1831 strncpy( psz_html, psz_subtitle, i_len );
1833 psz_subtitle += i_len;
1837 if( psz_attribs[ k ] == NULL )
1839 /* Jump over unrecognised tag */
1840 int i_len = strcspn( psz_subtitle, "\"" ) + 1;
1842 i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
1843 psz_subtitle += i_len;
1845 while (*psz_subtitle == ' ')
1846 *psz_html++ = *psz_subtitle++;
1848 *psz_html++ = *psz_subtitle++;
1850 else if( !strncmp( psz_subtitle, "</", 2 ))
1852 vlc_bool_t b_match = VLC_FALSE;
1853 int i_len = strlen( psz_tagStack ) - 1;
1854 char *psz_lastTag = NULL;
1858 psz_lastTag = psz_tagStack + i_len;
1861 switch( *psz_lastTag )
1864 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
1868 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
1872 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
1876 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
1883 /* Not well formed -- kill everything */
1884 free( psz_html_start );
1885 psz_html_start = NULL;
1888 *psz_lastTag = '\0';
1889 strncpy( psz_html, psz_subtitle, i_len );
1891 psz_subtitle += i_len;
1895 psz_subtitle += strcspn( psz_subtitle, ">" );
1898 else if( *psz_subtitle == '&' )
1900 if( !strncasecmp( psz_subtitle, "<", 4 ))
1902 strcpy( psz_html, "<" );
1906 else if( !strncasecmp( psz_subtitle, ">", 4 ))
1908 strcpy( psz_html, ">" );
1912 else if( !strncasecmp( psz_subtitle, "&", 5 ))
1914 strcpy( psz_html, "&" );
1920 strcpy( psz_html, "&" );
1927 *psz_html = *psz_subtitle;
1928 if( psz_html > psz_html_start )
1930 /* Check for double whitespace */
1931 if((( *psz_html == ' ' ) ||
1932 ( *psz_html == '\t' )) &&
1933 (( *(psz_html-1) == ' ' ) ||
1934 ( *(psz_html-1) == '\t' )))
1936 strcpy( psz_html, NO_BREAKING_SPACE );
1937 psz_html += strlen( NO_BREAKING_SPACE ) - 1;
1944 if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 10 )
1946 int i_len = psz_html - psz_html_start;
1949 psz_html_start = realloc( psz_html_start, i_buf_size );
1950 psz_html = psz_html_start + i_len;
1954 strcpy( psz_html, "</text>" );
1957 if( psz_tagStack[ 0 ] != '\0' )
1959 /* Not well formed -- kill everything */
1960 free( psz_html_start );
1961 psz_html_start = NULL;
1963 else if( psz_html_start )
1965 /* Shrink the memory requirements */
1966 psz_html_start = realloc( psz_html_start, psz_html - psz_html_start + 1 );
1969 return psz_html_start;
1972 /* The reverse of the above function - given a HTML subtitle, turn it
1973 * into a plain-text version, complete with sensible whitespace compaction
1976 static char *CreatePlainText( char *psz_subtitle )
1978 char *psz_text = StripTags( psz_subtitle );
1984 s = strpbrk( psz_text, "\t\r\n " );
1989 int i_whitespace = strspn( s, "\t\r\n " );
1991 /* Favour '\n' over other whitespaces - if one of these
1992 * occurs in the whitespace use a '\n' as our value,
1993 * otherwise just use a ' '
1995 for( k = 0; k < i_whitespace; k++ )
1996 if( s[k] == '\n' ) spc = '\n';
1998 if( i_whitespace > 1 )
2002 strlen( s ) - i_whitespace + 1 );
2006 s = strpbrk( s, "\t\r\n " );
2011 /****************************************************************************
2012 * download and resize image located at psz_url
2013 ***************************************************************************/
2014 static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec, subpicture_t *p_spu, const char *psz_filename, int i_transparent_color )
2016 decoder_sys_t *p_sys = p_dec->p_sys;
2017 subpicture_region_t *p_region;
2018 video_format_t fmt_out;
2020 picture_t *p_pic = NULL;
2022 for( k = 0; k < p_sys->i_images; k++ )
2024 if( p_sys->pp_images &&
2025 !strcmp( p_sys->pp_images[k]->psz_filename, psz_filename ) )
2027 p_pic = p_sys->pp_images[k]->p_pic;
2034 msg_Err( p_dec, "Unable to read image %s", psz_filename );
2038 /* Display the feed's image */
2039 memset( &fmt_out, 0, sizeof( video_format_t));
2041 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
2042 fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
2043 fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
2045 fmt_out.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
2047 fmt_out.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
2049 p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt_out );
2052 msg_Err( p_dec, "cannot allocate SPU region" );
2055 vout_CopyPicture( p_dec, &p_region->picture, p_pic );
2057 /* This isn't the best way to do this - if you really want transparency, then
2058 * you're much better off using an image type that supports it like PNG. The
2059 * spec requires this support though.
2061 if( i_transparent_color > 0 )
2063 uint8_t i_r = ( i_transparent_color >> 16 ) & 0xff;
2064 uint8_t i_g = ( i_transparent_color >> 8 ) & 0xff;
2065 uint8_t i_b = ( i_transparent_color ) & 0xff;
2066 uint8_t i_y = ( ( ( 66 * i_r + 129 * i_g + 25 * i_b + 128 ) >> 8 ) + 16 );
2067 uint8_t i_u = ( ( -38 * i_r - 74 * i_g + 112 * i_b + 128 ) >> 8 ) + 128 ;
2068 uint8_t i_v = ( ( 112 * i_r - 94 * i_g - 18 * i_b + 128 ) >> 8 ) + 128 ;
2070 if( ( p_region->picture.Y_PITCH == p_region->picture.U_PITCH ) &&
2071 ( p_region->picture.Y_PITCH == p_region->picture.V_PITCH ) &&
2072 ( p_region->picture.Y_PITCH == p_region->picture.A_PITCH ) )
2074 int i_lines = p_region->picture.p[ Y_PLANE ].i_lines;
2075 if( i_lines > p_region->picture.p[ U_PLANE ].i_lines )
2076 i_lines = p_region->picture.p[ U_PLANE ].i_lines;
2077 if( i_lines > p_region->picture.p[ V_PLANE ].i_lines )
2078 i_lines = p_region->picture.p[ V_PLANE ].i_lines;
2079 if( i_lines > p_region->picture.p[ A_PLANE ].i_lines )
2080 i_lines = p_region->picture.p[ A_PLANE ].i_lines;
2084 for( i = 0; i < p_region->picture.A_PITCH * i_lines; i++ )
2086 if(( p_region->picture.Y_PIXELS[ i ] == i_y ) &&
2087 ( p_region->picture.U_PIXELS[ i ] == i_u ) &&
2088 ( p_region->picture.V_PIXELS[ i ] == i_v ) )
2090 p_region->picture.A_PIXELS[ i ] = 1;