1 /*****************************************************************************
2 * subsusf.c : USF subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Bernie Purcell <bitmap@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
32 static int OpenDecoder ( vlc_object_t * );
33 static void CloseDecoder ( vlc_object_t * );
35 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
36 static char *CreatePlainText( char * );
37 static int ParseImageAttachments( decoder_t *p_dec );
39 static subpicture_t *ParseText ( decoder_t *, block_t * );
40 static void ParseUSFHeader( decoder_t * );
41 static subpicture_region_t *ParseUSFString( decoder_t *, char *, subpicture_t * );
42 static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec, subpicture_t *p_spu, const char *psz_filename, int i_transparent_color );
44 /*****************************************************************************
46 *****************************************************************************/
49 set_capability( "decoder", 40 );
50 set_callbacks( OpenDecoder, CloseDecoder );
51 set_category( CAT_INPUT );
52 set_subcategory( SUBCAT_INPUT_SCODEC );
53 /* We inherit subsdec-align and subsdec-formatted from subsdec.c */
56 /*****************************************************************************
57 * OpenDecoder: probe the decoder and return score
58 *****************************************************************************
59 * Tries to launch a decoder and return score so that the interface is able
61 *****************************************************************************/
62 static int OpenDecoder( vlc_object_t *p_this )
64 decoder_t *p_dec = (decoder_t*)p_this;
68 if( p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') )
73 p_dec->pf_decode_sub = DecodeBlock;
75 /* Allocate the memory needed to store the decoder's structure */
76 if( ( p_dec->p_sys = p_sys =
77 (decoder_sys_t *)calloc(1, sizeof(decoder_sys_t)) ) == NULL )
79 msg_Err( p_dec, "out of memory" );
83 /* Unused fields of p_sys - not needed for USF decoding */
84 p_sys->b_ass = VLC_FALSE;
85 p_sys->iconv_handle = (vlc_iconv_t)-1;
86 p_sys->b_autodetect_utf8 = VLC_FALSE;
90 p_sys->i_original_height = -1;
91 p_sys->i_original_width = -1;
92 TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
93 TAB_INIT( p_sys->i_images, p_sys->pp_images );
95 /* USF subtitles are mandated to be UTF-8, so don't need vlc_iconv */
97 var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
98 var_Get( p_dec, "subsdec-align", &val );
99 p_sys->i_align = val.i_int;
101 ParseImageAttachments( p_dec );
103 if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
105 if( p_dec->fmt_in.i_extra > 0 )
106 ParseUSFHeader( p_dec );
112 /****************************************************************************
113 * DecodeBlock: the whole thing
114 ****************************************************************************
115 * This function must be fed with complete subtitles units.
116 ****************************************************************************/
117 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
119 subpicture_t *p_spu = NULL;
121 if( !pp_block || *pp_block == NULL ) return NULL;
123 p_spu = ParseText( p_dec, *pp_block );
125 block_Release( *pp_block );
131 /*****************************************************************************
132 * CloseDecoder: clean up the decoder
133 *****************************************************************************/
134 static void CloseDecoder( vlc_object_t *p_this )
136 decoder_t *p_dec = (decoder_t *)p_this;
137 decoder_sys_t *p_sys = p_dec->p_sys;
139 if( p_sys->pp_ssa_styles )
142 for( i = 0; i < p_sys->i_ssa_styles; i++ )
144 if( !p_sys->pp_ssa_styles[i] )
147 if( p_sys->pp_ssa_styles[i]->psz_stylename )
148 free( p_sys->pp_ssa_styles[i]->psz_stylename );
149 if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname )
150 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
151 if( p_sys->pp_ssa_styles[i] )
152 free( p_sys->pp_ssa_styles[i] );
154 TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
156 if( p_sys->pp_images )
159 for( i = 0; i < p_sys->i_images; i++ )
161 if( !p_sys->pp_images[i] )
164 if( p_sys->pp_images[i]->p_pic )
165 p_sys->pp_images[i]->p_pic->pf_release( p_sys->pp_images[i]->p_pic );
166 if( p_sys->pp_images[i]->psz_filename )
167 free( p_sys->pp_images[i]->psz_filename );
169 free( p_sys->pp_images[i] );
171 TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
177 /*****************************************************************************
178 * ParseText: parse an text subtitle packet and send it to the video output
179 *****************************************************************************/
180 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
182 decoder_sys_t *p_sys = p_dec->p_sys;
183 subpicture_t *p_spu = NULL;
184 char *psz_subtitle = NULL;
186 /* We cannot display a subpicture with no date */
187 if( p_block->i_pts == 0 )
189 msg_Warn( p_dec, "subtitle without a date" );
193 /* Check validity of packet data */
194 /* An "empty" line containing only \0 can be used to force
195 and ephemer picture from the screen */
196 if( p_block->i_buffer < 1 )
198 msg_Warn( p_dec, "no subtitle data" );
202 /* Should be resiliant against bad subtitles */
203 psz_subtitle = strndup( (const char *)p_block->p_buffer,
205 if( psz_subtitle == NULL )
208 /* USF Subtitles are mandated to be UTF-8 -- make sure it is */
209 if (EnsureUTF8( psz_subtitle ) == NULL)
211 msg_Err( p_dec, _("USF subtitles must be in UTF-8 format.\n"
212 "This stream contains USF subtitles which aren't.") );
215 /* Create the subpicture unit */
216 p_spu = p_dec->pf_spu_buffer_new( p_dec );
219 msg_Warn( p_dec, "can't get spu buffer" );
220 if( psz_subtitle ) free( psz_subtitle );
224 p_spu->b_pausable = VLC_TRUE;
226 /* Decode USF strings */
227 p_spu->p_region = ParseUSFString( p_dec, psz_subtitle, p_spu );
229 p_spu->i_start = p_block->i_pts;
230 p_spu->i_stop = p_block->i_pts + p_block->i_length;
231 p_spu->b_ephemer = (p_block->i_length == 0);
232 p_spu->b_absolute = VLC_FALSE;
233 p_spu->i_original_picture_width = p_sys->i_original_width;
234 p_spu->i_original_picture_height = p_sys->i_original_height;
236 if( psz_subtitle ) free( psz_subtitle );
241 static char *GrabAttributeValue( const char *psz_attribute,
242 const char *psz_tag_start )
244 if( psz_attribute && psz_tag_start )
246 char *psz_tag_end = strchr( psz_tag_start, '>' );
247 char *psz_found = strcasestr( psz_tag_start, psz_attribute );
251 psz_found += strlen( psz_attribute );
253 if(( *(psz_found++) == '=' ) &&
254 ( *(psz_found++) == '\"' ))
256 if( psz_found < psz_tag_end )
258 int i_len = strcspn( psz_found, "\"" );
259 return strndup( psz_found, i_len );
267 static ssa_style_t *ParseStyle( decoder_sys_t *p_sys, char *psz_subtitle )
269 ssa_style_t *p_style = NULL;
270 char *psz_style = GrabAttributeValue( "style", psz_subtitle );
276 for( i = 0; i < p_sys->i_ssa_styles; i++ )
278 if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
279 p_style = p_sys->pp_ssa_styles[i];
286 static int ParsePositionAttributeList( char *psz_subtitle, int *i_align,
291 char *psz_align = GrabAttributeValue( "alignment", psz_subtitle );
292 char *psz_margin_x = GrabAttributeValue( "horizontal-margin", psz_subtitle );
293 char *psz_margin_y = GrabAttributeValue( "vertical-margin", psz_subtitle );
295 char *psz_relative = GrabAttributeValue( "relative-to", psz_subtitle );
296 char *psz_rotate_x = GrabAttributeValue( "rotate-x", psz_subtitle );
297 char *psz_rotate_y = GrabAttributeValue( "rotate-y", psz_subtitle );
298 char *psz_rotate_z = GrabAttributeValue( "rotate-z", psz_subtitle );
301 *i_align = SUBPICTURE_ALIGN_BOTTOM;
307 if( !strcasecmp( "TopLeft", psz_align ) )
308 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
309 else if( !strcasecmp( "TopCenter", psz_align ) )
310 *i_align = SUBPICTURE_ALIGN_TOP;
311 else if( !strcasecmp( "TopRight", psz_align ) )
312 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
313 else if( !strcasecmp( "MiddleLeft", psz_align ) )
314 *i_align = SUBPICTURE_ALIGN_LEFT;
315 else if( !strcasecmp( "MiddleCenter", psz_align ) )
317 else if( !strcasecmp( "MiddleRight", psz_align ) )
318 *i_align = SUBPICTURE_ALIGN_RIGHT;
319 else if( !strcasecmp( "BottomLeft", psz_align ) )
320 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
321 else if( !strcasecmp( "BottomCenter", psz_align ) )
322 *i_align = SUBPICTURE_ALIGN_BOTTOM;
323 else if( !strcasecmp( "BottomRight", psz_align ) )
324 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
326 i_mask |= ATTRIBUTE_ALIGNMENT;
331 *i_x = atoi( psz_margin_x );
332 if( strchr( psz_margin_x, '%' ) )
333 i_mask |= ATTRIBUTE_X_PERCENT;
335 i_mask |= ATTRIBUTE_X;
337 free( psz_margin_x );
341 *i_y = atoi( psz_margin_y );
342 if( strchr( psz_margin_y, '%' ) )
343 i_mask |= ATTRIBUTE_Y_PERCENT;
345 i_mask |= ATTRIBUTE_Y;
347 free( psz_margin_y );
352 static void SetupPositions( subpicture_region_t *p_region, char *psz_subtitle )
358 i_mask = ParsePositionAttributeList( psz_subtitle, &i_align, &i_x, &i_y );
360 if( i_mask & ATTRIBUTE_ALIGNMENT )
361 p_region->i_align = i_align;
363 /* TODO: Setup % based offsets properly, without adversely affecting
364 * everything else in vlc. Will address with separate patch, to
365 * prevent this one being any more complicated.
367 if( i_mask & ATTRIBUTE_X )
369 else if( i_mask & ATTRIBUTE_X_PERCENT )
372 if( i_mask & ATTRIBUTE_Y )
374 else if( i_mask & ATTRIBUTE_Y_PERCENT )
378 static subpicture_region_t *CreateTextRegion( decoder_t *p_dec,
384 decoder_sys_t *p_sys = p_dec->p_sys;
385 subpicture_region_t *p_text_region;
388 /* Create a new subpicture region */
389 memset( &fmt, 0, sizeof(video_format_t) );
390 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
392 fmt.i_width = fmt.i_height = 0;
393 fmt.i_x_offset = fmt.i_y_offset = 0;
394 p_text_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
396 if( p_text_region != NULL )
398 ssa_style_t *p_style = NULL;
400 p_text_region->psz_text = NULL;
401 p_text_region->psz_html = strndup( psz_subtitle, i_len );
402 if( ! p_text_region->psz_html )
404 msg_Err( p_dec, "out of memory" );
405 p_spu->pf_destroy_region( VLC_OBJECT(p_dec), p_text_region );
409 p_style = ParseStyle( p_sys, p_text_region->psz_html );
414 for( i = 0; i < p_sys->i_ssa_styles; i++ )
416 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
417 p_style = p_sys->pp_ssa_styles[i];
423 msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename );
425 p_text_region->p_style = &p_style->font_style;
426 p_text_region->i_align = p_style->i_align;
428 /* TODO: Setup % based offsets properly, without adversely affecting
429 * everything else in vlc. Will address with separate patch,
430 * to prevent this one being any more complicated.
432 * p_style->i_margin_percent_h;
433 * p_style->i_margin_percent_v;
435 p_text_region->i_x = p_style->i_margin_h;
436 p_text_region->i_y = p_style->i_margin_v;
441 p_text_region->i_align = SUBPICTURE_ALIGN_BOTTOM | i_sys_align;
442 p_text_region->i_x = i_sys_align ? 20 : 0;
443 p_text_region->i_y = 10;
445 /* Look for position arguments which may override the style-based
448 SetupPositions( p_text_region, psz_subtitle );
450 p_text_region->p_next = NULL;
452 return p_text_region;
455 static int ParseImageAttachments( decoder_t *p_dec )
457 decoder_sys_t *p_sys = p_dec->p_sys;
458 input_attachment_t **pp_attachments;
459 int i_attachments_cnt;
462 if( VLC_SUCCESS != decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments_cnt ))
465 for( k = 0; k < i_attachments_cnt; k++ )
467 input_attachment_t *p_attach = pp_attachments[k];
469 vlc_fourcc_t type = 0;
471 if( ( !strcmp( p_attach->psz_mime, "image/bmp" ) ) || /* BMP */
472 ( !strcmp( p_attach->psz_mime, "image/x-bmp" ) ) ||
473 ( !strcmp( p_attach->psz_mime, "image/x-bitmap" ) ) ||
474 ( !strcmp( p_attach->psz_mime, "image/x-ms-bmp" ) ) )
476 type = VLC_FOURCC('b','m','p',' ');
478 else if( ( !strcmp( p_attach->psz_mime, "image/x-portable-anymap" ) ) || /* PNM */
479 ( !strcmp( p_attach->psz_mime, "image/x-portable-bitmap" ) ) || /* PBM */
480 ( !strcmp( p_attach->psz_mime, "image/x-portable-graymap" ) ) || /* PGM */
481 ( !strcmp( p_attach->psz_mime, "image/x-portable-pixmap" ) ) ) /* PPM */
483 type = VLC_FOURCC('p','n','m',' ');
485 else if ( !strcmp( p_attach->psz_mime, "image/gif" ) ) /* GIF */
486 type = VLC_FOURCC('g','i','f',' ');
487 else if ( !strcmp( p_attach->psz_mime, "image/jpeg" ) ) /* JPG, JPEG */
488 type = VLC_FOURCC('j','p','e','g');
489 else if ( !strcmp( p_attach->psz_mime, "image/pcx" ) ) /* PCX */
490 type = VLC_FOURCC('p','c','x',' ');
491 else if ( !strcmp( p_attach->psz_mime, "image/png" ) ) /* PNG */
492 type = VLC_FOURCC('p','n','g',' ');
493 else if ( !strcmp( p_attach->psz_mime, "image/tiff" ) ) /* TIF, TIFF */
494 type = VLC_FOURCC('t','i','f','f');
495 else if ( !strcmp( p_attach->psz_mime, "image/x-tga" ) ) /* TGA */
496 type = VLC_FOURCC('t','g','a',' ');
497 else if ( !strcmp( p_attach->psz_mime, "image/x-xpixmap") ) /* XPM */
498 type = VLC_FOURCC('x','p','m',' ');
501 ( p_attach->i_data > 0 ) &&
502 ( p_attach->p_data != NULL ) )
504 picture_t *p_pic = NULL;
505 image_handler_t *p_image;
507 p_image = image_HandlerCreate( p_dec );
508 if( p_image != NULL )
512 p_block = block_New( p_image->p_parent, p_attach->i_data );
514 if( p_block != NULL )
516 video_format_t fmt_in;
517 video_format_t fmt_out;
519 memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data );
521 memset( &fmt_in, 0, sizeof( video_format_t));
522 memset( &fmt_out, 0, sizeof( video_format_t));
524 fmt_in.i_chroma = type;
525 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
527 /* Find a suitable decoder module */
528 if( module_Exists( p_dec, "sdl_image" ) )
530 /* ffmpeg thinks it can handle bmp properly but it can't (at least
531 * not all of them), so use sdl_image if it is available */
535 var_Create( p_dec, "codec", VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
536 val.psz_string = (char*) "sdl_image";
537 var_Set( p_dec, "codec", val );
540 p_pic = image_Read( p_image, p_block, &fmt_in, &fmt_out );
541 var_Destroy( p_dec, "codec" );
544 image_HandlerDelete( p_image );
548 image_attach_t *p_picture = malloc( sizeof(image_attach_t) );
552 p_picture->psz_filename = strdup( p_attach->psz_name );
553 p_picture->p_pic = p_pic;
555 TAB_APPEND( p_sys->i_images, p_sys->pp_images, p_picture );
559 vlc_input_attachment_Delete( pp_attachments[ k ] );
561 free( pp_attachments );
566 static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
568 decoder_sys_t *p_sys = p_dec->p_sys;
570 ssa_style_t *p_style = NULL;
571 int i_style_level = 0;
572 int i_metadata_level = 0;
574 while ( xml_ReaderRead( p_xml_reader ) == 1 )
576 switch ( xml_ReaderNodeType( p_xml_reader ) )
578 case XML_READER_TEXT:
579 case XML_READER_NONE:
581 case XML_READER_ENDELEM:
582 psz_node = xml_ReaderName( p_xml_reader );
586 switch (i_style_level)
589 if( !strcasecmp( "metadata", psz_node ) && (i_metadata_level == 1) )
595 if( !strcasecmp( "styles", psz_node ) )
601 if( !strcasecmp( "style", psz_node ) )
603 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
613 case XML_READER_STARTELEM:
614 psz_node = xml_ReaderName( p_xml_reader );
619 if( !strcasecmp( "metadata", psz_node ) && (i_style_level == 0) )
623 else if( !strcasecmp( "resolution", psz_node ) &&
624 ( i_metadata_level == 1) )
626 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
628 char *psz_name = xml_ReaderName ( p_xml_reader );
629 char *psz_value = xml_ReaderValue ( p_xml_reader );
631 if( psz_name && psz_value )
633 if( !strcasecmp( "x", psz_name ) )
634 p_sys->i_original_width = atoi( psz_value );
635 else if( !strcasecmp( "y", psz_name ) )
636 p_sys->i_original_height = atoi( psz_value );
638 if( psz_name ) free( psz_name );
639 if( psz_value ) free( psz_value );
642 else if( !strcasecmp( "styles", psz_node ) && (i_style_level == 0) )
646 else if( !strcasecmp( "style", psz_node ) && (i_style_level == 1) )
650 p_style = calloc( 1, sizeof(ssa_style_t) );
653 msg_Err( p_dec, "out of memory" );
657 /* All styles are supposed to default to Default, and then
658 * one or more settings are over-ridden.
659 * At the moment this only effects styles defined AFTER
663 for( i = 0; i < p_sys->i_ssa_styles; i++ )
665 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
667 ssa_style_t *p_default_style = p_sys->pp_ssa_styles[i];
669 memcpy( p_style, p_default_style, sizeof( ssa_style_t ) );
670 p_style->font_style.psz_fontname = strdup( p_style->font_style.psz_fontname );
671 p_style->psz_stylename = NULL;
675 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
677 char *psz_name = xml_ReaderName ( p_xml_reader );
678 char *psz_value = xml_ReaderValue ( p_xml_reader );
680 if( psz_name && psz_value )
682 if( !strcasecmp( "name", psz_name ) )
683 p_style->psz_stylename = strdup( psz_value);
685 if( psz_name ) free( psz_name );
686 if( psz_value ) free( psz_value );
689 else if( !strcasecmp( "fontstyle", psz_node ) && (i_style_level == 2) )
691 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
693 char *psz_name = xml_ReaderName ( p_xml_reader );
694 char *psz_value = xml_ReaderValue ( p_xml_reader );
696 if( psz_name && psz_value )
698 if( !strcasecmp( "face", psz_name ) )
700 if( p_style->font_style.psz_fontname )
701 free( p_style->font_style.psz_fontname );
702 p_style->font_style.psz_fontname = strdup( psz_value );
704 else if( !strcasecmp( "size", psz_name ) )
706 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
708 int i_value = atoi( psz_value );
710 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
711 p_style->font_style.i_font_size +=
712 ( i_value * p_style->font_style.i_font_size ) / 10;
713 else if( i_value < -5 )
714 p_style->font_style.i_font_size = - i_value;
715 else if( i_value > 5 )
716 p_style->font_style.i_font_size = i_value;
719 p_style->font_style.i_font_size = atoi( psz_value );
721 else if( !strcasecmp( "italic", psz_name ) )
723 if( !strcasecmp( "yes", psz_value ))
724 p_style->font_style.i_style_flags |= STYLE_ITALIC;
726 p_style->font_style.i_style_flags &= ~STYLE_ITALIC;
728 else if( !strcasecmp( "weight", psz_name ) )
730 if( !strcasecmp( "bold", psz_value ))
731 p_style->font_style.i_style_flags |= STYLE_BOLD;
733 p_style->font_style.i_style_flags &= ~STYLE_BOLD;
735 else if( !strcasecmp( "underline", psz_name ) )
737 if( !strcasecmp( "yes", psz_value ))
738 p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
740 p_style->font_style.i_style_flags &= ~STYLE_UNDERLINE;
742 else if( !strcasecmp( "color", psz_name ) )
744 if( *psz_value == '#' )
746 unsigned long col = strtol(psz_value+1, NULL, 16);
747 p_style->font_style.i_font_color = (col & 0x00ffffff);
748 p_style->font_style.i_font_alpha = (col >> 24) & 0xff;
751 else if( !strcasecmp( "outline-color", psz_name ) )
753 if( *psz_value == '#' )
755 unsigned long col = strtol(psz_value+1, NULL, 16);
756 p_style->font_style.i_outline_color = (col & 0x00ffffff);
757 p_style->font_style.i_outline_alpha = (col >> 24) & 0xff;
760 else if( !strcasecmp( "outline-level", psz_name ) )
762 p_style->font_style.i_outline_width = atoi( psz_value );
764 else if( !strcasecmp( "shadow-color", psz_name ) )
766 if( *psz_value == '#' )
768 unsigned long col = strtol(psz_value+1, NULL, 16);
769 p_style->font_style.i_shadow_color = (col & 0x00ffffff);
770 p_style->font_style.i_shadow_alpha = (col >> 24) & 0xff;
773 else if( !strcasecmp( "shadow-level", psz_name ) )
775 p_style->font_style.i_shadow_width = atoi( psz_value );
777 else if( !strcasecmp( "back-color", psz_name ) )
779 if( *psz_value == '#' )
781 unsigned long col = strtol(psz_value+1, NULL, 16);
782 p_style->font_style.i_karaoke_background_color = (col & 0x00ffffff);
783 p_style->font_style.i_karaoke_background_alpha = (col >> 24) & 0xff;
786 else if( !strcasecmp( "spacing", psz_name ) )
788 p_style->font_style.i_spacing = atoi( psz_value );
791 if( psz_name ) free( psz_name );
792 if( psz_value ) free( psz_value );
795 else if( !strcasecmp( "position", psz_node ) && (i_style_level == 2) )
797 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
799 char *psz_name = xml_ReaderName ( p_xml_reader );
800 char *psz_value = xml_ReaderValue ( p_xml_reader );
802 if( psz_name && psz_value )
804 if( !strcasecmp( "alignment", psz_name ) )
806 if( !strcasecmp( "TopLeft", psz_value ) )
807 p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
808 else if( !strcasecmp( "TopCenter", psz_value ) )
809 p_style->i_align = SUBPICTURE_ALIGN_TOP;
810 else if( !strcasecmp( "TopRight", psz_value ) )
811 p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
812 else if( !strcasecmp( "MiddleLeft", psz_value ) )
813 p_style->i_align = SUBPICTURE_ALIGN_LEFT;
814 else if( !strcasecmp( "MiddleCenter", psz_value ) )
815 p_style->i_align = 0;
816 else if( !strcasecmp( "MiddleRight", psz_value ) )
817 p_style->i_align = SUBPICTURE_ALIGN_RIGHT;
818 else if( !strcasecmp( "BottomLeft", psz_value ) )
819 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
820 else if( !strcasecmp( "BottomCenter", psz_value ) )
821 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM;
822 else if( !strcasecmp( "BottomRight", psz_value ) )
823 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
825 else if( !strcasecmp( "horizontal-margin", psz_name ) )
827 if( strchr( psz_value, '%' ) )
829 p_style->i_margin_h = 0;
830 p_style->i_margin_percent_h = atoi( psz_value );
834 p_style->i_margin_h = atoi( psz_value );
835 p_style->i_margin_percent_h = 0;
838 else if( !strcasecmp( "vertical-margin", psz_name ) )
840 if( strchr( psz_value, '%' ) )
842 p_style->i_margin_v = 0;
843 p_style->i_margin_percent_v = atoi( psz_value );
847 p_style->i_margin_v = atoi( psz_value );
848 p_style->i_margin_percent_v = 0;
852 if( psz_name ) free( psz_name );
853 if( psz_value ) free( psz_value );
861 if( p_style ) free( p_style );
866 static subpicture_region_t *ParseUSFString( decoder_t *p_dec,
868 subpicture_t *p_spu_in )
870 decoder_sys_t *p_sys = p_dec->p_sys;
871 subpicture_t *p_spu = p_spu_in;
872 subpicture_region_t *p_region_first = NULL;
873 subpicture_region_t *p_region_upto = p_region_first;
875 while( *psz_subtitle )
877 if( *psz_subtitle == '<' )
879 char *psz_end = NULL;
881 if(( !strncasecmp( psz_subtitle, "<text ", 6 )) ||
882 ( !strncasecmp( psz_subtitle, "<text>", 6 )))
884 psz_end = strcasestr( psz_subtitle, "</text>" );
888 subpicture_region_t *p_text_region;
890 psz_end += strcspn( psz_end, ">" ) + 1;
892 p_text_region = CreateTextRegion( p_dec,
895 psz_end - psz_subtitle,
900 p_text_region->psz_text = CreatePlainText( p_text_region->psz_html );
902 if( ! var_CreateGetBool( p_dec, "subsdec-formatted" ) )
904 free( p_text_region->psz_html );
905 p_text_region->psz_html = NULL;
909 if( !p_region_first )
911 p_region_first = p_region_upto = p_text_region;
913 else if( p_text_region )
915 p_region_upto->p_next = p_text_region;
916 p_region_upto = p_region_upto->p_next;
920 else if(( !strncasecmp( psz_subtitle, "<karaoke ", 9 )) ||
921 ( !strncasecmp( psz_subtitle, "<karaoke>", 9 )))
923 psz_end = strcasestr( psz_subtitle, "</karaoke>" );
927 subpicture_region_t *p_text_region;
929 psz_end += strcspn( psz_end, ">" ) + 1;
931 p_text_region = CreateTextRegion( p_dec,
934 psz_end - psz_subtitle,
939 if( ! var_CreateGetBool( p_dec, "subsdec-formatted" ) )
941 free( p_text_region->psz_html );
942 p_text_region->psz_html = NULL;
945 if( !p_region_first )
947 p_region_first = p_region_upto = p_text_region;
949 else if( p_text_region )
951 p_region_upto->p_next = p_text_region;
952 p_region_upto = p_region_upto->p_next;
956 else if(( !strncasecmp( psz_subtitle, "<image ", 7 )) ||
957 ( !strncasecmp( psz_subtitle, "<image>", 7 )))
959 subpicture_region_t *p_image_region = NULL;
961 char *psz_end = strcasestr( psz_subtitle, "</image>" );
962 char *psz_content = strchr( psz_subtitle, '>' );
963 int i_transparent = -1;
965 /* If a colorkey parameter is specified, then we have to map
966 * that index in the picture through as transparent (it is
967 * required by the USF spec but is also recommended that if the
968 * creator really wants a transparent colour that they use a
969 * type like PNG that properly supports it; this goes doubly
970 * for VLC because the pictures are stored internally in YUV
971 * and the resulting colour-matching may not produce the
974 char *psz_tmp = GrabAttributeValue( "colorkey", psz_subtitle );
977 if( *psz_tmp == '#' )
978 i_transparent = strtol( psz_tmp + 1, NULL, 16 ) & 0x00ffffff;
981 if( psz_content && ( psz_content < psz_end ) )
983 char *psz_filename = strndup( &psz_content[1], psz_end - &psz_content[1] );
986 p_image_region = LoadEmbeddedImage( p_dec, p_spu,
987 psz_filename, i_transparent );
988 free( psz_filename );
992 if( psz_end ) psz_end += strcspn( psz_end, ">" ) + 1;
996 SetupPositions( p_image_region, psz_subtitle );
998 p_image_region->p_next = NULL;
999 p_image_region->psz_text = NULL;
1000 p_image_region->psz_html = NULL;
1003 if( !p_region_first )
1005 p_region_first = p_region_upto = p_image_region;
1007 else if( p_image_region )
1009 p_region_upto->p_next = p_image_region;
1010 p_region_upto = p_region_upto->p_next;
1014 psz_subtitle = psz_end - 1;
1016 psz_subtitle += strcspn( psz_subtitle, ">" );
1022 return p_region_first;
1025 /*****************************************************************************
1026 * ParseUSFHeader: Retrieve global formatting information etc
1027 *****************************************************************************/
1028 static void ParseUSFHeader( decoder_t *p_dec )
1030 stream_t *p_sub = NULL;
1031 xml_t *p_xml = NULL;
1032 xml_reader_t *p_xml_reader = NULL;
1034 p_sub = stream_MemoryNew( VLC_OBJECT(p_dec),
1035 p_dec->fmt_in.p_extra,
1036 p_dec->fmt_in.i_extra,
1041 p_xml = xml_Create( p_dec );
1044 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
1047 /* Look for Root Node */
1048 if( xml_ReaderRead( p_xml_reader ) == 1 )
1050 char *psz_node = xml_ReaderName( p_xml_reader );
1052 if( !strcasecmp( "usfsubtitles", psz_node ) )
1053 ParseUSFHeaderTags( p_dec, p_xml_reader );
1058 xml_ReaderDelete( p_xml, p_xml_reader );
1060 xml_Delete( p_xml );
1062 stream_Delete( p_sub );
1065 /* Function now handles tags which has attribute values, and tries
1066 * to deal with &' commands too. It no longer modifies the string
1067 * in place, so that the original text can be reused
1069 static char *StripTags( char *psz_subtitle )
1071 char *psz_text_start;
1074 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
1075 if( !psz_text_start )
1078 while( *psz_subtitle )
1080 /* Mask out any pre-existing LFs in the subtitle */
1081 if( *psz_subtitle == '\n' )
1082 *psz_subtitle = ' ';
1084 if( *psz_subtitle == '<' )
1086 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
1089 psz_subtitle += strcspn( psz_subtitle, ">" );
1091 else if( *psz_subtitle == '&' )
1093 if( !strncasecmp( psz_subtitle, "<", 4 ))
1096 psz_subtitle += strcspn( psz_subtitle, ";" );
1098 else if( !strncasecmp( psz_subtitle, ">", 4 ))
1101 psz_subtitle += strcspn( psz_subtitle, ";" );
1103 else if( !strncasecmp( psz_subtitle, "&", 5 ))
1106 psz_subtitle += strcspn( psz_subtitle, ";" );
1108 else if( !strncasecmp( psz_subtitle, """, 6 ))
1111 psz_subtitle += strcspn( psz_subtitle, ";" );
1115 /* Assume it is just a normal ampersand */
1121 *psz_text++ = *psz_subtitle;
1127 psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
1129 return psz_text_start;
1132 /* Turn a HTML subtitle, turn into a plain-text version,
1133 * complete with sensible whitespace compaction
1136 static char *CreatePlainText( char *psz_subtitle )
1138 char *psz_text = StripTags( psz_subtitle );
1144 s = strpbrk( psz_text, "\t\r\n " );
1149 int i_whitespace = strspn( s, "\t\r\n " );
1151 /* Favour '\n' over other whitespaces - if one of these
1152 * occurs in the whitespace use a '\n' as our value,
1153 * otherwise just use a ' '
1155 for( k = 0; k < i_whitespace; k++ )
1156 if( s[k] == '\n' ) spc = '\n';
1158 if( i_whitespace > 1 )
1162 strlen( s ) - i_whitespace + 1 );
1166 s = strpbrk( s, "\t\r\n " );
1171 /****************************************************************************
1172 * download and resize image located at psz_url
1173 ***************************************************************************/
1174 static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec,
1175 subpicture_t *p_spu,
1176 const char *psz_filename,
1177 int i_transparent_color )
1179 decoder_sys_t *p_sys = p_dec->p_sys;
1180 subpicture_region_t *p_region;
1181 video_format_t fmt_out;
1183 picture_t *p_pic = NULL;
1185 for( k = 0; k < p_sys->i_images; k++ )
1187 if( p_sys->pp_images &&
1188 !strcmp( p_sys->pp_images[k]->psz_filename, psz_filename ) )
1190 p_pic = p_sys->pp_images[k]->p_pic;
1197 msg_Err( p_dec, "Unable to read image %s", psz_filename );
1201 /* Display the feed's image */
1202 memset( &fmt_out, 0, sizeof( video_format_t));
1204 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
1205 fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
1206 fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
1208 fmt_out.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
1210 fmt_out.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
1212 p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt_out );
1215 msg_Err( p_dec, "cannot allocate SPU region" );
1218 vout_CopyPicture( p_dec, &p_region->picture, p_pic );
1220 /* This isn't the best way to do this - if you really want transparency, then
1221 * you're much better off using an image type that supports it like PNG. The
1222 * spec requires this support though.
1224 if( i_transparent_color > 0 )
1226 uint8_t i_r = ( i_transparent_color >> 16 ) & 0xff;
1227 uint8_t i_g = ( i_transparent_color >> 8 ) & 0xff;
1228 uint8_t i_b = ( i_transparent_color ) & 0xff;
1229 uint8_t i_y = ( ( ( 66 * i_r + 129 * i_g + 25 * i_b + 128 ) >> 8 ) + 16 );
1230 uint8_t i_u = ( ( -38 * i_r - 74 * i_g + 112 * i_b + 128 ) >> 8 ) + 128 ;
1231 uint8_t i_v = ( ( 112 * i_r - 94 * i_g - 18 * i_b + 128 ) >> 8 ) + 128 ;
1233 if( ( p_region->picture.Y_PITCH == p_region->picture.U_PITCH ) &&
1234 ( p_region->picture.Y_PITCH == p_region->picture.V_PITCH ) &&
1235 ( p_region->picture.Y_PITCH == p_region->picture.A_PITCH ) )
1237 int i_lines = p_region->picture.p[ Y_PLANE ].i_lines;
1238 if( i_lines > p_region->picture.p[ U_PLANE ].i_lines )
1239 i_lines = p_region->picture.p[ U_PLANE ].i_lines;
1240 if( i_lines > p_region->picture.p[ V_PLANE ].i_lines )
1241 i_lines = p_region->picture.p[ V_PLANE ].i_lines;
1242 if( i_lines > p_region->picture.p[ A_PLANE ].i_lines )
1243 i_lines = p_region->picture.p[ A_PLANE ].i_lines;
1247 for( i = 0; i < p_region->picture.A_PITCH * i_lines; i++ )
1249 if(( p_region->picture.Y_PIXELS[ i ] == i_y ) &&
1250 ( p_region->picture.U_PIXELS[ i ] == i_u ) &&
1251 ( p_region->picture.V_PIXELS[ i ] == i_v ) )
1253 p_region->picture.A_PIXELS[ i ] = 1;