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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 static int OpenDecoder ( vlc_object_t * );
30 static void CloseDecoder ( vlc_object_t * );
32 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
33 static char *CreatePlainText( char * );
34 static int ParseImageAttachments( decoder_t *p_dec );
36 static subpicture_t *ParseText ( decoder_t *, block_t * );
37 static void ParseUSFHeader( decoder_t * );
38 static subpicture_region_t *ParseUSFString( decoder_t *, char *, subpicture_t * );
39 static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec, subpicture_t *p_spu, const char *psz_filename, int i_transparent_color );
41 /*****************************************************************************
43 *****************************************************************************/
46 set_capability( "decoder", 40 );
47 set_callbacks( OpenDecoder, CloseDecoder );
48 set_category( CAT_INPUT );
49 set_subcategory( SUBCAT_INPUT_SCODEC );
50 /* We inherit subsdec-align and subsdec-formatted from subsdec.c */
53 /*****************************************************************************
54 * OpenDecoder: probe the decoder and return score
55 *****************************************************************************
56 * Tries to launch a decoder and return score so that the interface is able
58 *****************************************************************************/
59 static int OpenDecoder( vlc_object_t *p_this )
61 decoder_t *p_dec = (decoder_t*)p_this;
65 if( p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') )
70 p_dec->pf_decode_sub = DecodeBlock;
72 /* Allocate the memory needed to store the decoder's structure */
73 if( ( p_dec->p_sys = p_sys =
74 (decoder_sys_t *)calloc(1, sizeof(decoder_sys_t)) ) == NULL )
76 msg_Err( p_dec, "out of memory" );
80 /* Unused fields of p_sys - not needed for USF decoding */
81 p_sys->b_ass = VLC_FALSE;
82 p_sys->iconv_handle = (vlc_iconv_t)-1;
83 p_sys->b_autodetect_utf8 = VLC_FALSE;
87 p_sys->i_original_height = -1;
88 p_sys->i_original_width = -1;
89 TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
90 TAB_INIT( p_sys->i_images, p_sys->pp_images );
92 /* USF subtitles are mandated to be UTF-8, so don't need vlc_iconv */
94 var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
95 var_Get( p_dec, "subsdec-align", &val );
96 p_sys->i_align = val.i_int;
98 ParseImageAttachments( p_dec );
100 if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
102 if( p_dec->fmt_in.i_extra > 0 )
103 ParseUSFHeader( p_dec );
109 /****************************************************************************
110 * DecodeBlock: the whole thing
111 ****************************************************************************
112 * This function must be fed with complete subtitles units.
113 ****************************************************************************/
114 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
116 subpicture_t *p_spu = NULL;
118 if( !pp_block || *pp_block == NULL ) return NULL;
120 p_spu = ParseText( p_dec, *pp_block );
122 block_Release( *pp_block );
128 /*****************************************************************************
129 * CloseDecoder: clean up the decoder
130 *****************************************************************************/
131 static void CloseDecoder( vlc_object_t *p_this )
133 decoder_t *p_dec = (decoder_t *)p_this;
134 decoder_sys_t *p_sys = p_dec->p_sys;
136 if( p_sys->pp_ssa_styles )
139 for( i = 0; i < p_sys->i_ssa_styles; i++ )
141 if( !p_sys->pp_ssa_styles[i] )
144 if( p_sys->pp_ssa_styles[i]->psz_stylename )
145 free( p_sys->pp_ssa_styles[i]->psz_stylename );
146 if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname )
147 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
148 if( p_sys->pp_ssa_styles[i] )
149 free( p_sys->pp_ssa_styles[i] );
151 TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
153 if( p_sys->pp_images )
156 for( i = 0; i < p_sys->i_images; i++ )
158 if( !p_sys->pp_images[i] )
161 if( p_sys->pp_images[i]->p_pic )
162 p_sys->pp_images[i]->p_pic->pf_release( p_sys->pp_images[i]->p_pic );
163 if( p_sys->pp_images[i]->psz_filename )
164 free( p_sys->pp_images[i]->psz_filename );
166 free( p_sys->pp_images[i] );
168 TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
174 /*****************************************************************************
175 * ParseText: parse an text subtitle packet and send it to the video output
176 *****************************************************************************/
177 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
179 decoder_sys_t *p_sys = p_dec->p_sys;
180 subpicture_t *p_spu = NULL;
181 char *psz_subtitle = NULL;
183 /* We cannot display a subpicture with no date */
184 if( p_block->i_pts == 0 )
186 msg_Warn( p_dec, "subtitle without a date" );
190 /* Check validity of packet data */
191 /* An "empty" line containing only \0 can be used to force
192 and ephemer picture from the screen */
193 if( p_block->i_buffer < 1 )
195 msg_Warn( p_dec, "no subtitle data" );
199 /* Should be resiliant against bad subtitles */
200 psz_subtitle = strndup( (const char *)p_block->p_buffer,
202 if( psz_subtitle == NULL )
205 /* USF Subtitles are mandated to be UTF-8 -- make sure it is */
206 if (EnsureUTF8( psz_subtitle ) == NULL)
208 msg_Err( p_dec, _("USF subtitles must be in UTF-8 format.\n"
209 "This stream contains USF subtitles which aren't.") );
212 /* Create the subpicture unit */
213 p_spu = p_dec->pf_spu_buffer_new( p_dec );
216 msg_Warn( p_dec, "can't get spu buffer" );
217 if( psz_subtitle ) free( psz_subtitle );
221 p_spu->b_pausable = VLC_TRUE;
223 /* Decode USF strings */
224 p_spu->p_region = ParseUSFString( p_dec, psz_subtitle, p_spu );
226 p_spu->i_start = p_block->i_pts;
227 p_spu->i_stop = p_block->i_pts + p_block->i_length;
228 p_spu->b_ephemer = (p_block->i_length == 0);
229 p_spu->b_absolute = VLC_FALSE;
230 p_spu->i_original_picture_width = p_sys->i_original_width;
231 p_spu->i_original_picture_height = p_sys->i_original_height;
233 if( psz_subtitle ) free( psz_subtitle );
238 static char *GrabAttributeValue( const char *psz_attribute,
239 const char *psz_tag_start )
241 if( psz_attribute && psz_tag_start )
243 char *psz_tag_end = strchr( psz_tag_start, '>' );
244 char *psz_found = strcasestr( psz_tag_start, psz_attribute );
248 psz_found += strlen( psz_attribute );
250 if(( *(psz_found++) == '=' ) &&
251 ( *(psz_found++) == '\"' ))
253 if( psz_found < psz_tag_end )
255 int i_len = strcspn( psz_found, "\"" );
256 return strndup( psz_found, i_len );
264 static ssa_style_t *ParseStyle( decoder_sys_t *p_sys, char *psz_subtitle )
266 ssa_style_t *p_style = NULL;
267 char *psz_style = GrabAttributeValue( "style", psz_subtitle );
273 for( i = 0; i < p_sys->i_ssa_styles; i++ )
275 if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
276 p_style = p_sys->pp_ssa_styles[i];
283 static int ParsePositionAttributeList( char *psz_subtitle, int *i_align,
288 char *psz_align = GrabAttributeValue( "alignment", psz_subtitle );
289 char *psz_margin_x = GrabAttributeValue( "horizontal-margin", psz_subtitle );
290 char *psz_margin_y = GrabAttributeValue( "vertical-margin", psz_subtitle );
292 char *psz_relative = GrabAttributeValue( "relative-to", psz_subtitle );
293 char *psz_rotate_x = GrabAttributeValue( "rotate-x", psz_subtitle );
294 char *psz_rotate_y = GrabAttributeValue( "rotate-y", psz_subtitle );
295 char *psz_rotate_z = GrabAttributeValue( "rotate-z", psz_subtitle );
298 *i_align = SUBPICTURE_ALIGN_BOTTOM;
304 if( !strcasecmp( "TopLeft", psz_align ) )
305 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
306 else if( !strcasecmp( "TopCenter", psz_align ) )
307 *i_align = SUBPICTURE_ALIGN_TOP;
308 else if( !strcasecmp( "TopRight", psz_align ) )
309 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
310 else if( !strcasecmp( "MiddleLeft", psz_align ) )
311 *i_align = SUBPICTURE_ALIGN_LEFT;
312 else if( !strcasecmp( "MiddleCenter", psz_align ) )
314 else if( !strcasecmp( "MiddleRight", psz_align ) )
315 *i_align = SUBPICTURE_ALIGN_RIGHT;
316 else if( !strcasecmp( "BottomLeft", psz_align ) )
317 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
318 else if( !strcasecmp( "BottomCenter", psz_align ) )
319 *i_align = SUBPICTURE_ALIGN_BOTTOM;
320 else if( !strcasecmp( "BottomRight", psz_align ) )
321 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
323 i_mask |= ATTRIBUTE_ALIGNMENT;
328 *i_x = atoi( psz_margin_x );
329 if( strchr( psz_margin_x, '%' ) )
330 i_mask |= ATTRIBUTE_X_PERCENT;
332 i_mask |= ATTRIBUTE_X;
334 free( psz_margin_x );
338 *i_y = atoi( psz_margin_y );
339 if( strchr( psz_margin_y, '%' ) )
340 i_mask |= ATTRIBUTE_Y_PERCENT;
342 i_mask |= ATTRIBUTE_Y;
344 free( psz_margin_y );
349 static void SetupPositions( subpicture_region_t *p_region, char *psz_subtitle )
355 i_mask = ParsePositionAttributeList( psz_subtitle, &i_align, &i_x, &i_y );
357 if( i_mask & ATTRIBUTE_ALIGNMENT )
358 p_region->i_align = i_align;
360 /* TODO: Setup % based offsets properly, without adversely affecting
361 * everything else in vlc. Will address with separate patch, to
362 * prevent this one being any more complicated.
364 if( i_mask & ATTRIBUTE_X )
366 else if( i_mask & ATTRIBUTE_X_PERCENT )
369 if( i_mask & ATTRIBUTE_Y )
371 else if( i_mask & ATTRIBUTE_Y_PERCENT )
375 static subpicture_region_t *CreateTextRegion( decoder_t *p_dec,
381 decoder_sys_t *p_sys = p_dec->p_sys;
382 subpicture_region_t *p_text_region;
385 /* Create a new subpicture region */
386 memset( &fmt, 0, sizeof(video_format_t) );
387 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
389 fmt.i_width = fmt.i_height = 0;
390 fmt.i_x_offset = fmt.i_y_offset = 0;
391 p_text_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
393 if( p_text_region != NULL )
395 ssa_style_t *p_style = NULL;
397 p_text_region->psz_text = NULL;
398 p_text_region->psz_html = strndup( psz_subtitle, i_len );
399 if( ! p_text_region->psz_html )
401 msg_Err( p_dec, "out of memory" );
402 p_spu->pf_destroy_region( VLC_OBJECT(p_dec), p_text_region );
406 p_style = ParseStyle( p_sys, p_text_region->psz_html );
411 for( i = 0; i < p_sys->i_ssa_styles; i++ )
413 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
414 p_style = p_sys->pp_ssa_styles[i];
420 msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename );
422 p_text_region->p_style = &p_style->font_style;
423 p_text_region->i_align = p_style->i_align;
425 /* TODO: Setup % based offsets properly, without adversely affecting
426 * everything else in vlc. Will address with separate patch,
427 * to prevent this one being any more complicated.
429 * p_style->i_margin_percent_h;
430 * p_style->i_margin_percent_v;
432 p_text_region->i_x = p_style->i_margin_h;
433 p_text_region->i_y = p_style->i_margin_v;
438 p_text_region->i_align = SUBPICTURE_ALIGN_BOTTOM | i_sys_align;
439 p_text_region->i_x = i_sys_align ? 20 : 0;
440 p_text_region->i_y = 10;
442 /* Look for position arguments which may override the style-based
445 SetupPositions( p_text_region, psz_subtitle );
447 p_text_region->p_next = NULL;
449 return p_text_region;
452 static int ParseImageAttachments( decoder_t *p_dec )
454 decoder_sys_t *p_sys = p_dec->p_sys;
455 input_attachment_t **pp_attachments;
456 int i_attachments_cnt;
459 if( VLC_SUCCESS != decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments_cnt ))
462 for( k = 0; k < i_attachments_cnt; k++ )
464 input_attachment_t *p_attach = pp_attachments[k];
466 vlc_fourcc_t type = 0;
468 if( ( !strcmp( p_attach->psz_mime, "image/bmp" ) ) || /* BMP */
469 ( !strcmp( p_attach->psz_mime, "image/x-bmp" ) ) ||
470 ( !strcmp( p_attach->psz_mime, "image/x-bitmap" ) ) ||
471 ( !strcmp( p_attach->psz_mime, "image/x-ms-bmp" ) ) )
473 type = VLC_FOURCC('b','m','p',' ');
475 else if( ( !strcmp( p_attach->psz_mime, "image/x-portable-anymap" ) ) || /* PNM */
476 ( !strcmp( p_attach->psz_mime, "image/x-portable-bitmap" ) ) || /* PBM */
477 ( !strcmp( p_attach->psz_mime, "image/x-portable-graymap" ) ) || /* PGM */
478 ( !strcmp( p_attach->psz_mime, "image/x-portable-pixmap" ) ) ) /* PPM */
480 type = VLC_FOURCC('p','n','m',' ');
482 else if ( !strcmp( p_attach->psz_mime, "image/gif" ) ) /* GIF */
483 type = VLC_FOURCC('g','i','f',' ');
484 else if ( !strcmp( p_attach->psz_mime, "image/jpeg" ) ) /* JPG, JPEG */
485 type = VLC_FOURCC('j','p','e','g');
486 else if ( !strcmp( p_attach->psz_mime, "image/pcx" ) ) /* PCX */
487 type = VLC_FOURCC('p','c','x',' ');
488 else if ( !strcmp( p_attach->psz_mime, "image/png" ) ) /* PNG */
489 type = VLC_FOURCC('p','n','g',' ');
490 else if ( !strcmp( p_attach->psz_mime, "image/tiff" ) ) /* TIF, TIFF */
491 type = VLC_FOURCC('t','i','f','f');
492 else if ( !strcmp( p_attach->psz_mime, "image/x-tga" ) ) /* TGA */
493 type = VLC_FOURCC('t','g','a',' ');
494 else if ( !strcmp( p_attach->psz_mime, "image/x-xpixmap") ) /* XPM */
495 type = VLC_FOURCC('x','p','m',' ');
498 ( p_attach->i_data > 0 ) &&
499 ( p_attach->p_data != NULL ) )
501 picture_t *p_pic = NULL;
502 image_handler_t *p_image;
504 p_image = image_HandlerCreate( p_dec );
505 if( p_image != NULL )
509 p_block = block_New( p_image->p_parent, p_attach->i_data );
511 if( p_block != NULL )
513 video_format_t fmt_in;
514 video_format_t fmt_out;
516 memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data );
518 memset( &fmt_in, 0, sizeof( video_format_t));
519 memset( &fmt_out, 0, sizeof( video_format_t));
521 fmt_in.i_chroma = type;
522 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
524 /* Find a suitable decoder module */
525 if( module_Exists( p_dec, "sdl_image" ) )
527 /* ffmpeg thinks it can handle bmp properly but it can't (at least
528 * not all of them), so use sdl_image if it is available */
532 var_Create( p_dec, "codec", VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
533 val.psz_string = (char*) "sdl_image";
534 var_Set( p_dec, "codec", val );
537 p_pic = image_Read( p_image, p_block, &fmt_in, &fmt_out );
538 var_Destroy( p_dec, "codec" );
541 image_HandlerDelete( p_image );
545 image_attach_t *p_picture = malloc( sizeof(image_attach_t) );
549 p_picture->psz_filename = strdup( p_attach->psz_name );
550 p_picture->p_pic = p_pic;
552 TAB_APPEND( p_sys->i_images, p_sys->pp_images, p_picture );
556 vlc_input_attachment_Delete( pp_attachments[ k ] );
558 free( pp_attachments );
563 static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
565 decoder_sys_t *p_sys = p_dec->p_sys;
567 ssa_style_t *p_style = NULL;
568 int i_style_level = 0;
569 int i_metadata_level = 0;
571 while ( xml_ReaderRead( p_xml_reader ) == 1 )
573 switch ( xml_ReaderNodeType( p_xml_reader ) )
575 case XML_READER_TEXT:
576 case XML_READER_NONE:
578 case XML_READER_ENDELEM:
579 psz_node = xml_ReaderName( p_xml_reader );
583 switch (i_style_level)
586 if( !strcasecmp( "metadata", psz_node ) && (i_metadata_level == 1) )
592 if( !strcasecmp( "styles", psz_node ) )
598 if( !strcasecmp( "style", psz_node ) )
600 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
610 case XML_READER_STARTELEM:
611 psz_node = xml_ReaderName( p_xml_reader );
616 if( !strcasecmp( "metadata", psz_node ) && (i_style_level == 0) )
620 else if( !strcasecmp( "resolution", psz_node ) &&
621 ( i_metadata_level == 1) )
623 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
625 char *psz_name = xml_ReaderName ( p_xml_reader );
626 char *psz_value = xml_ReaderValue ( p_xml_reader );
628 if( psz_name && psz_value )
630 if( !strcasecmp( "x", psz_name ) )
631 p_sys->i_original_width = atoi( psz_value );
632 else if( !strcasecmp( "y", psz_name ) )
633 p_sys->i_original_height = atoi( psz_value );
635 if( psz_name ) free( psz_name );
636 if( psz_value ) free( psz_value );
639 else if( !strcasecmp( "styles", psz_node ) && (i_style_level == 0) )
643 else if( !strcasecmp( "style", psz_node ) && (i_style_level == 1) )
647 p_style = calloc( 1, sizeof(ssa_style_t) );
650 msg_Err( p_dec, "out of memory" );
654 /* All styles are supposed to default to Default, and then
655 * one or more settings are over-ridden.
656 * At the moment this only effects styles defined AFTER
660 for( i = 0; i < p_sys->i_ssa_styles; i++ )
662 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
664 ssa_style_t *p_default_style = p_sys->pp_ssa_styles[i];
666 memcpy( p_style, p_default_style, sizeof( ssa_style_t ) );
667 p_style->font_style.psz_fontname = strdup( p_style->font_style.psz_fontname );
668 p_style->psz_stylename = NULL;
672 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
674 char *psz_name = xml_ReaderName ( p_xml_reader );
675 char *psz_value = xml_ReaderValue ( p_xml_reader );
677 if( psz_name && psz_value )
679 if( !strcasecmp( "name", psz_name ) )
680 p_style->psz_stylename = strdup( psz_value);
682 if( psz_name ) free( psz_name );
683 if( psz_value ) free( psz_value );
686 else if( !strcasecmp( "fontstyle", psz_node ) && (i_style_level == 2) )
688 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
690 char *psz_name = xml_ReaderName ( p_xml_reader );
691 char *psz_value = xml_ReaderValue ( p_xml_reader );
693 if( psz_name && psz_value )
695 if( !strcasecmp( "face", psz_name ) )
697 if( p_style->font_style.psz_fontname )
698 free( p_style->font_style.psz_fontname );
699 p_style->font_style.psz_fontname = strdup( psz_value );
701 else if( !strcasecmp( "size", psz_name ) )
703 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
705 int i_value = atoi( psz_value );
707 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
708 p_style->font_style.i_font_size +=
709 ( i_value * p_style->font_style.i_font_size ) / 10;
710 else if( i_value < -5 )
711 p_style->font_style.i_font_size = - i_value;
712 else if( i_value > 5 )
713 p_style->font_style.i_font_size = i_value;
716 p_style->font_style.i_font_size = atoi( psz_value );
718 else if( !strcasecmp( "italic", psz_name ) )
720 if( !strcasecmp( "yes", psz_value ))
721 p_style->font_style.i_style_flags |= STYLE_ITALIC;
723 p_style->font_style.i_style_flags &= ~STYLE_ITALIC;
725 else if( !strcasecmp( "weight", psz_name ) )
727 if( !strcasecmp( "bold", psz_value ))
728 p_style->font_style.i_style_flags |= STYLE_BOLD;
730 p_style->font_style.i_style_flags &= ~STYLE_BOLD;
732 else if( !strcasecmp( "underline", psz_name ) )
734 if( !strcasecmp( "yes", psz_value ))
735 p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
737 p_style->font_style.i_style_flags &= ~STYLE_UNDERLINE;
739 else if( !strcasecmp( "color", psz_name ) )
741 if( *psz_value == '#' )
743 unsigned long col = strtol(psz_value+1, NULL, 16);
744 p_style->font_style.i_font_color = (col & 0x00ffffff);
745 p_style->font_style.i_font_alpha = (col >> 24) & 0xff;
748 else if( !strcasecmp( "outline-color", psz_name ) )
750 if( *psz_value == '#' )
752 unsigned long col = strtol(psz_value+1, NULL, 16);
753 p_style->font_style.i_outline_color = (col & 0x00ffffff);
754 p_style->font_style.i_outline_alpha = (col >> 24) & 0xff;
757 else if( !strcasecmp( "outline-level", psz_name ) )
759 p_style->font_style.i_outline_width = atoi( psz_value );
761 else if( !strcasecmp( "shadow-color", psz_name ) )
763 if( *psz_value == '#' )
765 unsigned long col = strtol(psz_value+1, NULL, 16);
766 p_style->font_style.i_shadow_color = (col & 0x00ffffff);
767 p_style->font_style.i_shadow_alpha = (col >> 24) & 0xff;
770 else if( !strcasecmp( "shadow-level", psz_name ) )
772 p_style->font_style.i_shadow_width = atoi( psz_value );
774 else if( !strcasecmp( "back-color", psz_name ) )
776 if( *psz_value == '#' )
778 unsigned long col = strtol(psz_value+1, NULL, 16);
779 p_style->font_style.i_karaoke_background_color = (col & 0x00ffffff);
780 p_style->font_style.i_karaoke_background_alpha = (col >> 24) & 0xff;
783 else if( !strcasecmp( "spacing", psz_name ) )
785 p_style->font_style.i_spacing = atoi( psz_value );
788 if( psz_name ) free( psz_name );
789 if( psz_value ) free( psz_value );
792 else if( !strcasecmp( "position", psz_node ) && (i_style_level == 2) )
794 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
796 char *psz_name = xml_ReaderName ( p_xml_reader );
797 char *psz_value = xml_ReaderValue ( p_xml_reader );
799 if( psz_name && psz_value )
801 if( !strcasecmp( "alignment", psz_name ) )
803 if( !strcasecmp( "TopLeft", psz_value ) )
804 p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
805 else if( !strcasecmp( "TopCenter", psz_value ) )
806 p_style->i_align = SUBPICTURE_ALIGN_TOP;
807 else if( !strcasecmp( "TopRight", psz_value ) )
808 p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
809 else if( !strcasecmp( "MiddleLeft", psz_value ) )
810 p_style->i_align = SUBPICTURE_ALIGN_LEFT;
811 else if( !strcasecmp( "MiddleCenter", psz_value ) )
812 p_style->i_align = 0;
813 else if( !strcasecmp( "MiddleRight", psz_value ) )
814 p_style->i_align = SUBPICTURE_ALIGN_RIGHT;
815 else if( !strcasecmp( "BottomLeft", psz_value ) )
816 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
817 else if( !strcasecmp( "BottomCenter", psz_value ) )
818 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM;
819 else if( !strcasecmp( "BottomRight", psz_value ) )
820 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
822 else if( !strcasecmp( "horizontal-margin", psz_name ) )
824 if( strchr( psz_value, '%' ) )
826 p_style->i_margin_h = 0;
827 p_style->i_margin_percent_h = atoi( psz_value );
831 p_style->i_margin_h = atoi( psz_value );
832 p_style->i_margin_percent_h = 0;
835 else if( !strcasecmp( "vertical-margin", psz_name ) )
837 if( strchr( psz_value, '%' ) )
839 p_style->i_margin_v = 0;
840 p_style->i_margin_percent_v = atoi( psz_value );
844 p_style->i_margin_v = atoi( psz_value );
845 p_style->i_margin_percent_v = 0;
849 if( psz_name ) free( psz_name );
850 if( psz_value ) free( psz_value );
858 if( p_style ) free( p_style );
863 static subpicture_region_t *ParseUSFString( decoder_t *p_dec,
865 subpicture_t *p_spu_in )
867 decoder_sys_t *p_sys = p_dec->p_sys;
868 subpicture_t *p_spu = p_spu_in;
869 subpicture_region_t *p_region_first = NULL;
870 subpicture_region_t *p_region_upto = p_region_first;
872 while( *psz_subtitle )
874 if( *psz_subtitle == '<' )
876 char *psz_end = NULL;
878 if(( !strncasecmp( psz_subtitle, "<text ", 6 )) ||
879 ( !strncasecmp( psz_subtitle, "<text>", 6 )))
881 psz_end = strcasestr( psz_subtitle, "</text>" );
885 subpicture_region_t *p_text_region;
887 psz_end += strcspn( psz_end, ">" ) + 1;
889 p_text_region = CreateTextRegion( p_dec,
892 psz_end - psz_subtitle,
897 p_text_region->psz_text = CreatePlainText( p_text_region->psz_html );
899 if( ! var_CreateGetBool( p_dec, "subsdec-formatted" ) )
901 free( p_text_region->psz_html );
902 p_text_region->psz_html = NULL;
906 if( !p_region_first )
908 p_region_first = p_region_upto = p_text_region;
910 else if( p_text_region )
912 p_region_upto->p_next = p_text_region;
913 p_region_upto = p_region_upto->p_next;
917 else if(( !strncasecmp( psz_subtitle, "<karaoke ", 9 )) ||
918 ( !strncasecmp( psz_subtitle, "<karaoke>", 9 )))
920 psz_end = strcasestr( psz_subtitle, "</karaoke>" );
924 subpicture_region_t *p_text_region;
926 psz_end += strcspn( psz_end, ">" ) + 1;
928 p_text_region = CreateTextRegion( p_dec,
931 psz_end - psz_subtitle,
936 if( ! var_CreateGetBool( p_dec, "subsdec-formatted" ) )
938 free( p_text_region->psz_html );
939 p_text_region->psz_html = NULL;
942 if( !p_region_first )
944 p_region_first = p_region_upto = p_text_region;
946 else if( p_text_region )
948 p_region_upto->p_next = p_text_region;
949 p_region_upto = p_region_upto->p_next;
953 else if(( !strncasecmp( psz_subtitle, "<image ", 7 )) ||
954 ( !strncasecmp( psz_subtitle, "<image>", 7 )))
956 subpicture_region_t *p_image_region = NULL;
958 char *psz_end = strcasestr( psz_subtitle, "</image>" );
959 char *psz_content = strchr( psz_subtitle, '>' );
960 int i_transparent = -1;
962 /* If a colorkey parameter is specified, then we have to map
963 * that index in the picture through as transparent (it is
964 * required by the USF spec but is also recommended that if the
965 * creator really wants a transparent colour that they use a
966 * type like PNG that properly supports it; this goes doubly
967 * for VLC because the pictures are stored internally in YUV
968 * and the resulting colour-matching may not produce the
971 char *psz_tmp = GrabAttributeValue( "colorkey", psz_subtitle );
974 if( *psz_tmp == '#' )
975 i_transparent = strtol( psz_tmp + 1, NULL, 16 ) & 0x00ffffff;
978 if( psz_content && ( psz_content < psz_end ) )
980 char *psz_filename = strndup( &psz_content[1], psz_end - &psz_content[1] );
983 p_image_region = LoadEmbeddedImage( p_dec, p_spu,
984 psz_filename, i_transparent );
985 free( psz_filename );
989 if( psz_end ) psz_end += strcspn( psz_end, ">" ) + 1;
993 SetupPositions( p_image_region, psz_subtitle );
995 p_image_region->p_next = NULL;
996 p_image_region->psz_text = NULL;
997 p_image_region->psz_html = NULL;
1000 if( !p_region_first )
1002 p_region_first = p_region_upto = p_image_region;
1004 else if( p_image_region )
1006 p_region_upto->p_next = p_image_region;
1007 p_region_upto = p_region_upto->p_next;
1011 psz_subtitle = psz_end - 1;
1013 psz_subtitle += strcspn( psz_subtitle, ">" );
1019 return p_region_first;
1022 /*****************************************************************************
1023 * ParseUSFHeader: Retrieve global formatting information etc
1024 *****************************************************************************/
1025 static void ParseUSFHeader( decoder_t *p_dec )
1027 stream_t *p_sub = NULL;
1028 xml_t *p_xml = NULL;
1029 xml_reader_t *p_xml_reader = NULL;
1031 p_sub = stream_MemoryNew( VLC_OBJECT(p_dec),
1032 p_dec->fmt_in.p_extra,
1033 p_dec->fmt_in.i_extra,
1038 p_xml = xml_Create( p_dec );
1041 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
1044 /* Look for Root Node */
1045 if( xml_ReaderRead( p_xml_reader ) == 1 )
1047 char *psz_node = xml_ReaderName( p_xml_reader );
1049 if( !strcasecmp( "usfsubtitles", psz_node ) )
1050 ParseUSFHeaderTags( p_dec, p_xml_reader );
1055 xml_ReaderDelete( p_xml, p_xml_reader );
1057 xml_Delete( p_xml );
1059 stream_Delete( p_sub );
1062 /* Function now handles tags which has attribute values, and tries
1063 * to deal with &' commands too. It no longer modifies the string
1064 * in place, so that the original text can be reused
1066 static char *StripTags( char *psz_subtitle )
1068 char *psz_text_start;
1071 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
1072 if( !psz_text_start )
1075 while( *psz_subtitle )
1077 /* Mask out any pre-existing LFs in the subtitle */
1078 if( *psz_subtitle == '\n' )
1079 *psz_subtitle = ' ';
1081 if( *psz_subtitle == '<' )
1083 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
1086 psz_subtitle += strcspn( psz_subtitle, ">" );
1088 else if( *psz_subtitle == '&' )
1090 if( !strncasecmp( psz_subtitle, "<", 4 ))
1093 psz_subtitle += strcspn( psz_subtitle, ";" );
1095 else if( !strncasecmp( psz_subtitle, ">", 4 ))
1098 psz_subtitle += strcspn( psz_subtitle, ";" );
1100 else if( !strncasecmp( psz_subtitle, "&", 5 ))
1103 psz_subtitle += strcspn( psz_subtitle, ";" );
1105 else if( !strncasecmp( psz_subtitle, """, 6 ))
1108 psz_subtitle += strcspn( psz_subtitle, ";" );
1112 /* Assume it is just a normal ampersand */
1118 *psz_text++ = *psz_subtitle;
1124 psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
1126 return psz_text_start;
1129 /* Turn a HTML subtitle, turn into a plain-text version,
1130 * complete with sensible whitespace compaction
1133 static char *CreatePlainText( char *psz_subtitle )
1135 char *psz_text = StripTags( psz_subtitle );
1141 s = strpbrk( psz_text, "\t\r\n " );
1146 int i_whitespace = strspn( s, "\t\r\n " );
1148 /* Favour '\n' over other whitespaces - if one of these
1149 * occurs in the whitespace use a '\n' as our value,
1150 * otherwise just use a ' '
1152 for( k = 0; k < i_whitespace; k++ )
1153 if( s[k] == '\n' ) spc = '\n';
1155 if( i_whitespace > 1 )
1159 strlen( s ) - i_whitespace + 1 );
1163 s = strpbrk( s, "\t\r\n " );
1168 /****************************************************************************
1169 * download and resize image located at psz_url
1170 ***************************************************************************/
1171 static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec,
1172 subpicture_t *p_spu,
1173 const char *psz_filename,
1174 int i_transparent_color )
1176 decoder_sys_t *p_sys = p_dec->p_sys;
1177 subpicture_region_t *p_region;
1178 video_format_t fmt_out;
1180 picture_t *p_pic = NULL;
1182 for( k = 0; k < p_sys->i_images; k++ )
1184 if( p_sys->pp_images &&
1185 !strcmp( p_sys->pp_images[k]->psz_filename, psz_filename ) )
1187 p_pic = p_sys->pp_images[k]->p_pic;
1194 msg_Err( p_dec, "Unable to read image %s", psz_filename );
1198 /* Display the feed's image */
1199 memset( &fmt_out, 0, sizeof( video_format_t));
1201 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
1202 fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
1203 fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
1205 fmt_out.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
1207 fmt_out.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
1209 p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt_out );
1212 msg_Err( p_dec, "cannot allocate SPU region" );
1215 vout_CopyPicture( p_dec, &p_region->picture, p_pic );
1217 /* This isn't the best way to do this - if you really want transparency, then
1218 * you're much better off using an image type that supports it like PNG. The
1219 * spec requires this support though.
1221 if( i_transparent_color > 0 )
1223 uint8_t i_r = ( i_transparent_color >> 16 ) & 0xff;
1224 uint8_t i_g = ( i_transparent_color >> 8 ) & 0xff;
1225 uint8_t i_b = ( i_transparent_color ) & 0xff;
1226 uint8_t i_y = ( ( ( 66 * i_r + 129 * i_g + 25 * i_b + 128 ) >> 8 ) + 16 );
1227 uint8_t i_u = ( ( -38 * i_r - 74 * i_g + 112 * i_b + 128 ) >> 8 ) + 128 ;
1228 uint8_t i_v = ( ( 112 * i_r - 94 * i_g - 18 * i_b + 128 ) >> 8 ) + 128 ;
1230 if( ( p_region->picture.Y_PITCH == p_region->picture.U_PITCH ) &&
1231 ( p_region->picture.Y_PITCH == p_region->picture.V_PITCH ) &&
1232 ( p_region->picture.Y_PITCH == p_region->picture.A_PITCH ) )
1234 int i_lines = p_region->picture.p[ Y_PLANE ].i_lines;
1235 if( i_lines > p_region->picture.p[ U_PLANE ].i_lines )
1236 i_lines = p_region->picture.p[ U_PLANE ].i_lines;
1237 if( i_lines > p_region->picture.p[ V_PLANE ].i_lines )
1238 i_lines = p_region->picture.p[ V_PLANE ].i_lines;
1239 if( i_lines > p_region->picture.p[ A_PLANE ].i_lines )
1240 i_lines = p_region->picture.p[ A_PLANE ].i_lines;
1244 for( i = 0; i < p_region->picture.A_PITCH * i_lines; i++ )
1246 if(( p_region->picture.Y_PIXELS[ i ] == i_y ) &&
1247 ( p_region->picture.U_PIXELS[ i ] == i_u ) &&
1248 ( p_region->picture.V_PIXELS[ i ] == i_v ) )
1250 p_region->picture.A_PIXELS[ i ] = 1;