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 *****************************************************************************/
28 #include <vlc_plugin.h>
31 /*****************************************************************************
33 *****************************************************************************/
34 static int OpenDecoder ( vlc_object_t * );
35 static void CloseDecoder ( vlc_object_t * );
37 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
38 static char *CreatePlainText( char * );
39 static int ParseImageAttachments( decoder_t *p_dec );
41 static subpicture_t *ParseText ( decoder_t *, block_t * );
42 static void ParseUSFHeader( decoder_t * );
43 static subpicture_region_t *ParseUSFString( decoder_t *, char *, subpicture_t * );
44 static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec, subpicture_t *p_spu, const char *psz_filename, int i_transparent_color );
46 /*****************************************************************************
48 *****************************************************************************/
51 set_capability( "decoder", 40 );
52 set_shortname( N_("USFSubs"));
53 set_description( N_("USF subtitles decoder") );
54 set_callbacks( OpenDecoder, CloseDecoder );
55 set_category( CAT_INPUT );
56 set_subcategory( SUBCAT_INPUT_SCODEC );
57 /* We inherit subsdec-align and subsdec-formatted from subsdec.c */
60 /*****************************************************************************
61 * OpenDecoder: probe the decoder and return score
62 *****************************************************************************
63 * Tries to launch a decoder and return score so that the interface is able
65 *****************************************************************************/
66 static int OpenDecoder( vlc_object_t *p_this )
68 decoder_t *p_dec = (decoder_t*)p_this;
71 if( p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') )
74 /* Allocate the memory needed to store the decoder's structure */
75 if( ( p_dec->p_sys = p_sys = calloc(1, sizeof(decoder_sys_t)) ) == NULL )
78 p_dec->pf_decode_sub = DecodeBlock;
80 /* Unused fields of p_sys - not needed for USF decoding */
82 p_sys->iconv_handle = (vlc_iconv_t)-1;
83 p_sys->b_autodetect_utf8 = false;
87 p_sys->i_original_height = 0;
88 p_sys->i_original_width = 0;
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 p_sys->i_align = var_CreateGetInteger( p_dec, "subsdec-align" );
96 ParseImageAttachments( p_dec );
98 if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
100 if( p_dec->fmt_in.i_extra > 0 )
101 ParseUSFHeader( p_dec );
107 /****************************************************************************
108 * DecodeBlock: the whole thing
109 ****************************************************************************
110 * This function must be fed with complete subtitles units.
111 ****************************************************************************/
112 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
117 if( !pp_block || *pp_block == NULL )
121 if( p_block->i_rate != 0 )
122 p_block->i_length = p_block->i_length * p_block->i_rate / INPUT_RATE_DEFAULT;
124 p_spu = ParseText( p_dec, p_block );
126 block_Release( p_block );
132 /*****************************************************************************
133 * CloseDecoder: clean up the decoder
134 *****************************************************************************/
135 static void CloseDecoder( vlc_object_t *p_this )
137 decoder_t *p_dec = (decoder_t *)p_this;
138 decoder_sys_t *p_sys = p_dec->p_sys;
140 if( p_sys->pp_ssa_styles )
143 for( i = 0; i < p_sys->i_ssa_styles; i++ )
145 if( !p_sys->pp_ssa_styles[i] )
148 free( p_sys->pp_ssa_styles[i]->psz_stylename );
149 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
150 free( p_sys->pp_ssa_styles[i] );
152 TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
154 if( p_sys->pp_images )
157 for( i = 0; i < p_sys->i_images; i++ )
159 if( !p_sys->pp_images[i] )
162 if( p_sys->pp_images[i]->p_pic )
163 picture_Release( p_sys->pp_images[i]->p_pic );
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 free( psz_subtitle );
221 /* Decode USF strings */
222 p_spu->p_region = ParseUSFString( p_dec, psz_subtitle, p_spu );
224 p_spu->i_start = p_block->i_pts;
225 p_spu->i_stop = p_block->i_pts + p_block->i_length;
226 p_spu->b_ephemer = (p_block->i_length == 0);
227 p_spu->b_absolute = false;
228 p_spu->i_original_picture_width = p_sys->i_original_width;
229 p_spu->i_original_picture_height = p_sys->i_original_height;
231 free( psz_subtitle );
236 static char *GrabAttributeValue( const char *psz_attribute,
237 const char *psz_tag_start )
239 if( psz_attribute && psz_tag_start )
241 char *psz_tag_end = strchr( psz_tag_start, '>' );
242 char *psz_found = strcasestr( psz_tag_start, psz_attribute );
246 psz_found += strlen( psz_attribute );
248 if(( *(psz_found++) == '=' ) &&
249 ( *(psz_found++) == '\"' ))
251 if( psz_found < psz_tag_end )
253 int i_len = strcspn( psz_found, "\"" );
254 return strndup( psz_found, i_len );
262 static ssa_style_t *ParseStyle( decoder_sys_t *p_sys, char *psz_subtitle )
264 ssa_style_t *p_style = NULL;
265 char *psz_style = GrabAttributeValue( "style", psz_subtitle );
271 for( i = 0; i < p_sys->i_ssa_styles; i++ )
273 if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
274 p_style = p_sys->pp_ssa_styles[i];
281 static int ParsePositionAttributeList( char *psz_subtitle, int *i_align,
286 char *psz_align = GrabAttributeValue( "alignment", psz_subtitle );
287 char *psz_margin_x = GrabAttributeValue( "horizontal-margin", psz_subtitle );
288 char *psz_margin_y = GrabAttributeValue( "vertical-margin", psz_subtitle );
290 char *psz_relative = GrabAttributeValue( "relative-to", psz_subtitle );
291 char *psz_rotate_x = GrabAttributeValue( "rotate-x", psz_subtitle );
292 char *psz_rotate_y = GrabAttributeValue( "rotate-y", psz_subtitle );
293 char *psz_rotate_z = GrabAttributeValue( "rotate-z", psz_subtitle );
296 *i_align = SUBPICTURE_ALIGN_BOTTOM;
302 if( !strcasecmp( "TopLeft", psz_align ) )
303 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
304 else if( !strcasecmp( "TopCenter", psz_align ) )
305 *i_align = SUBPICTURE_ALIGN_TOP;
306 else if( !strcasecmp( "TopRight", psz_align ) )
307 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
308 else if( !strcasecmp( "MiddleLeft", psz_align ) )
309 *i_align = SUBPICTURE_ALIGN_LEFT;
310 else if( !strcasecmp( "MiddleCenter", psz_align ) )
312 else if( !strcasecmp( "MiddleRight", psz_align ) )
313 *i_align = SUBPICTURE_ALIGN_RIGHT;
314 else if( !strcasecmp( "BottomLeft", psz_align ) )
315 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
316 else if( !strcasecmp( "BottomCenter", psz_align ) )
317 *i_align = SUBPICTURE_ALIGN_BOTTOM;
318 else if( !strcasecmp( "BottomRight", psz_align ) )
319 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
321 i_mask |= ATTRIBUTE_ALIGNMENT;
326 *i_x = atoi( psz_margin_x );
327 if( strchr( psz_margin_x, '%' ) )
328 i_mask |= ATTRIBUTE_X_PERCENT;
330 i_mask |= ATTRIBUTE_X;
332 free( psz_margin_x );
336 *i_y = atoi( psz_margin_y );
337 if( strchr( psz_margin_y, '%' ) )
338 i_mask |= ATTRIBUTE_Y_PERCENT;
340 i_mask |= ATTRIBUTE_Y;
342 free( psz_margin_y );
347 static void SetupPositions( subpicture_region_t *p_region, char *psz_subtitle )
353 i_mask = ParsePositionAttributeList( psz_subtitle, &i_align, &i_x, &i_y );
355 if( i_mask & ATTRIBUTE_ALIGNMENT )
356 p_region->i_align = i_align;
358 /* TODO: Setup % based offsets properly, without adversely affecting
359 * everything else in vlc. Will address with separate patch, to
360 * prevent this one being any more complicated.
362 if( i_mask & ATTRIBUTE_X )
364 else if( i_mask & ATTRIBUTE_X_PERCENT )
367 if( i_mask & ATTRIBUTE_Y )
369 else if( i_mask & ATTRIBUTE_Y_PERCENT )
373 static subpicture_region_t *CreateTextRegion( decoder_t *p_dec,
379 decoder_sys_t *p_sys = p_dec->p_sys;
380 subpicture_region_t *p_text_region;
383 /* Create a new subpicture region */
384 memset( &fmt, 0, sizeof(video_format_t) );
385 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
387 fmt.i_width = fmt.i_height = 0;
388 fmt.i_x_offset = fmt.i_y_offset = 0;
389 p_text_region = subpicture_region_New( &fmt );
391 if( p_text_region != NULL )
393 ssa_style_t *p_style = NULL;
395 p_text_region->psz_text = NULL;
396 p_text_region->psz_html = strndup( psz_subtitle, i_len );
397 if( ! p_text_region->psz_html )
399 subpicture_region_Delete( p_text_region );
403 p_style = ParseStyle( p_sys, p_text_region->psz_html );
408 for( i = 0; i < p_sys->i_ssa_styles; i++ )
410 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
411 p_style = p_sys->pp_ssa_styles[i];
417 msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename );
419 p_text_region->p_style = &p_style->font_style;
420 p_text_region->i_align = p_style->i_align;
422 /* TODO: Setup % based offsets properly, without adversely affecting
423 * everything else in vlc. Will address with separate patch,
424 * to prevent this one being any more complicated.
426 * p_style->i_margin_percent_h;
427 * p_style->i_margin_percent_v;
429 p_text_region->i_x = p_style->i_margin_h;
430 p_text_region->i_y = p_style->i_margin_v;
435 p_text_region->i_align = SUBPICTURE_ALIGN_BOTTOM | i_sys_align;
436 p_text_region->i_x = i_sys_align ? 20 : 0;
437 p_text_region->i_y = 10;
439 /* Look for position arguments which may override the style-based
442 SetupPositions( p_text_region, psz_subtitle );
444 p_text_region->p_next = NULL;
446 return p_text_region;
449 static int ParseImageAttachments( decoder_t *p_dec )
451 decoder_sys_t *p_sys = p_dec->p_sys;
452 input_attachment_t **pp_attachments;
453 int i_attachments_cnt;
456 if( VLC_SUCCESS != decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments_cnt ))
459 for( k = 0; k < i_attachments_cnt; k++ )
461 input_attachment_t *p_attach = pp_attachments[k];
463 vlc_fourcc_t type = 0;
465 if( ( !strcmp( p_attach->psz_mime, "image/bmp" ) ) || /* BMP */
466 ( !strcmp( p_attach->psz_mime, "image/x-bmp" ) ) ||
467 ( !strcmp( p_attach->psz_mime, "image/x-bitmap" ) ) ||
468 ( !strcmp( p_attach->psz_mime, "image/x-ms-bmp" ) ) )
470 type = VLC_FOURCC('b','m','p',' ');
472 else if( ( !strcmp( p_attach->psz_mime, "image/x-portable-anymap" ) ) || /* PNM */
473 ( !strcmp( p_attach->psz_mime, "image/x-portable-bitmap" ) ) || /* PBM */
474 ( !strcmp( p_attach->psz_mime, "image/x-portable-graymap" ) ) || /* PGM */
475 ( !strcmp( p_attach->psz_mime, "image/x-portable-pixmap" ) ) ) /* PPM */
477 type = VLC_FOURCC('p','n','m',' ');
479 else if ( !strcmp( p_attach->psz_mime, "image/gif" ) ) /* GIF */
480 type = VLC_FOURCC('g','i','f',' ');
481 else if ( !strcmp( p_attach->psz_mime, "image/jpeg" ) ) /* JPG, JPEG */
482 type = VLC_FOURCC('j','p','e','g');
483 else if ( !strcmp( p_attach->psz_mime, "image/pcx" ) ) /* PCX */
484 type = VLC_FOURCC('p','c','x',' ');
485 else if ( !strcmp( p_attach->psz_mime, "image/png" ) ) /* PNG */
486 type = VLC_FOURCC('p','n','g',' ');
487 else if ( !strcmp( p_attach->psz_mime, "image/tiff" ) ) /* TIF, TIFF */
488 type = VLC_FOURCC('t','i','f','f');
489 else if ( !strcmp( p_attach->psz_mime, "image/x-tga" ) ) /* TGA */
490 type = VLC_FOURCC('t','g','a',' ');
491 else if ( !strcmp( p_attach->psz_mime, "image/x-xpixmap") ) /* XPM */
492 type = VLC_FOURCC('x','p','m',' ');
495 ( p_attach->i_data > 0 ) &&
496 ( p_attach->p_data != NULL ) )
498 picture_t *p_pic = NULL;
499 image_handler_t *p_image;
501 p_image = image_HandlerCreate( p_dec );
502 if( p_image != NULL )
506 p_block = block_New( p_image->p_parent, p_attach->i_data );
508 if( p_block != NULL )
510 video_format_t fmt_in;
511 video_format_t fmt_out;
513 memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data );
515 memset( &fmt_in, 0, sizeof( video_format_t));
516 memset( &fmt_out, 0, sizeof( video_format_t));
518 fmt_in.i_chroma = type;
519 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
521 /* Find a suitable decoder module */
522 if( module_Exists( p_dec, "sdl_image" ) )
524 /* ffmpeg thinks it can handle bmp properly but it can't (at least
525 * not all of them), so use sdl_image if it is available */
529 var_Create( p_dec, "codec", VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
530 val.psz_string = (char*) "sdl_image";
531 var_Set( p_dec, "codec", val );
534 p_pic = image_Read( p_image, p_block, &fmt_in, &fmt_out );
535 var_Destroy( p_dec, "codec" );
538 image_HandlerDelete( p_image );
542 image_attach_t *p_picture = malloc( sizeof(image_attach_t) );
546 p_picture->psz_filename = strdup( p_attach->psz_name );
547 p_picture->p_pic = p_pic;
549 TAB_APPEND( p_sys->i_images, p_sys->pp_images, p_picture );
553 vlc_input_attachment_Delete( pp_attachments[ k ] );
555 free( pp_attachments );
560 static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
562 decoder_sys_t *p_sys = p_dec->p_sys;
564 ssa_style_t *p_style = NULL;
565 int i_style_level = 0;
566 int i_metadata_level = 0;
568 while ( xml_ReaderRead( p_xml_reader ) == 1 )
570 switch ( xml_ReaderNodeType( p_xml_reader ) )
572 case XML_READER_TEXT:
573 case XML_READER_NONE:
575 case XML_READER_ENDELEM:
576 psz_node = xml_ReaderName( p_xml_reader );
580 switch (i_style_level)
583 if( !strcasecmp( "metadata", psz_node ) && (i_metadata_level == 1) )
589 if( !strcasecmp( "styles", psz_node ) )
595 if( !strcasecmp( "style", psz_node ) )
597 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
607 case XML_READER_STARTELEM:
608 psz_node = xml_ReaderName( p_xml_reader );
613 if( !strcasecmp( "metadata", psz_node ) && (i_style_level == 0) )
617 else if( !strcasecmp( "resolution", psz_node ) &&
618 ( i_metadata_level == 1) )
620 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
622 char *psz_name = xml_ReaderName ( p_xml_reader );
623 char *psz_value = xml_ReaderValue ( p_xml_reader );
625 if( psz_name && psz_value )
627 if( !strcasecmp( "x", psz_name ) )
628 p_sys->i_original_width = atoi( psz_value );
629 else if( !strcasecmp( "y", psz_name ) )
630 p_sys->i_original_height = atoi( psz_value );
636 else if( !strcasecmp( "styles", psz_node ) && (i_style_level == 0) )
640 else if( !strcasecmp( "style", psz_node ) && (i_style_level == 1) )
644 p_style = calloc( 1, sizeof(ssa_style_t) );
650 /* All styles are supposed to default to Default, and then
651 * one or more settings are over-ridden.
652 * At the moment this only effects styles defined AFTER
656 for( i = 0; i < p_sys->i_ssa_styles; i++ )
658 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
660 ssa_style_t *p_default_style = p_sys->pp_ssa_styles[i];
662 memcpy( p_style, p_default_style, sizeof( ssa_style_t ) );
663 p_style->font_style.psz_fontname = strdup( p_style->font_style.psz_fontname );
664 p_style->psz_stylename = NULL;
668 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
670 char *psz_name = xml_ReaderName ( p_xml_reader );
671 char *psz_value = xml_ReaderValue ( p_xml_reader );
673 if( psz_name && psz_value )
675 if( !strcasecmp( "name", psz_name ) )
676 p_style->psz_stylename = strdup( psz_value);
682 else if( !strcasecmp( "fontstyle", psz_node ) && (i_style_level == 2) )
684 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
686 char *psz_name = xml_ReaderName ( p_xml_reader );
687 char *psz_value = xml_ReaderValue ( p_xml_reader );
689 if( psz_name && psz_value )
691 if( !strcasecmp( "face", psz_name ) )
693 free( p_style->font_style.psz_fontname );
694 p_style->font_style.psz_fontname = strdup( psz_value );
696 else if( !strcasecmp( "size", psz_name ) )
698 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
700 int i_value = atoi( psz_value );
702 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
703 p_style->font_style.i_font_size +=
704 ( i_value * p_style->font_style.i_font_size ) / 10;
705 else if( i_value < -5 )
706 p_style->font_style.i_font_size = - i_value;
707 else if( i_value > 5 )
708 p_style->font_style.i_font_size = i_value;
711 p_style->font_style.i_font_size = atoi( psz_value );
713 else if( !strcasecmp( "italic", psz_name ) )
715 if( !strcasecmp( "yes", psz_value ))
716 p_style->font_style.i_style_flags |= STYLE_ITALIC;
718 p_style->font_style.i_style_flags &= ~STYLE_ITALIC;
720 else if( !strcasecmp( "weight", psz_name ) )
722 if( !strcasecmp( "bold", psz_value ))
723 p_style->font_style.i_style_flags |= STYLE_BOLD;
725 p_style->font_style.i_style_flags &= ~STYLE_BOLD;
727 else if( !strcasecmp( "underline", psz_name ) )
729 if( !strcasecmp( "yes", psz_value ))
730 p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
732 p_style->font_style.i_style_flags &= ~STYLE_UNDERLINE;
734 else if( !strcasecmp( "color", psz_name ) )
736 if( *psz_value == '#' )
738 unsigned long col = strtol(psz_value+1, NULL, 16);
739 p_style->font_style.i_font_color = (col & 0x00ffffff);
740 p_style->font_style.i_font_alpha = (col >> 24) & 0xff;
743 else if( !strcasecmp( "outline-color", psz_name ) )
745 if( *psz_value == '#' )
747 unsigned long col = strtol(psz_value+1, NULL, 16);
748 p_style->font_style.i_outline_color = (col & 0x00ffffff);
749 p_style->font_style.i_outline_alpha = (col >> 24) & 0xff;
752 else if( !strcasecmp( "outline-level", psz_name ) )
754 p_style->font_style.i_outline_width = atoi( psz_value );
756 else if( !strcasecmp( "shadow-color", psz_name ) )
758 if( *psz_value == '#' )
760 unsigned long col = strtol(psz_value+1, NULL, 16);
761 p_style->font_style.i_shadow_color = (col & 0x00ffffff);
762 p_style->font_style.i_shadow_alpha = (col >> 24) & 0xff;
765 else if( !strcasecmp( "shadow-level", psz_name ) )
767 p_style->font_style.i_shadow_width = atoi( psz_value );
769 else if( !strcasecmp( "back-color", psz_name ) )
771 if( *psz_value == '#' )
773 unsigned long col = strtol(psz_value+1, NULL, 16);
774 p_style->font_style.i_karaoke_background_color = (col & 0x00ffffff);
775 p_style->font_style.i_karaoke_background_alpha = (col >> 24) & 0xff;
778 else if( !strcasecmp( "spacing", psz_name ) )
780 p_style->font_style.i_spacing = atoi( psz_value );
787 else if( !strcasecmp( "position", psz_node ) && (i_style_level == 2) )
789 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
791 char *psz_name = xml_ReaderName ( p_xml_reader );
792 char *psz_value = xml_ReaderValue ( p_xml_reader );
794 if( psz_name && psz_value )
796 if( !strcasecmp( "alignment", psz_name ) )
798 if( !strcasecmp( "TopLeft", psz_value ) )
799 p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
800 else if( !strcasecmp( "TopCenter", psz_value ) )
801 p_style->i_align = SUBPICTURE_ALIGN_TOP;
802 else if( !strcasecmp( "TopRight", psz_value ) )
803 p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
804 else if( !strcasecmp( "MiddleLeft", psz_value ) )
805 p_style->i_align = SUBPICTURE_ALIGN_LEFT;
806 else if( !strcasecmp( "MiddleCenter", psz_value ) )
807 p_style->i_align = 0;
808 else if( !strcasecmp( "MiddleRight", psz_value ) )
809 p_style->i_align = SUBPICTURE_ALIGN_RIGHT;
810 else if( !strcasecmp( "BottomLeft", psz_value ) )
811 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
812 else if( !strcasecmp( "BottomCenter", psz_value ) )
813 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM;
814 else if( !strcasecmp( "BottomRight", psz_value ) )
815 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
817 else if( !strcasecmp( "horizontal-margin", psz_name ) )
819 if( strchr( psz_value, '%' ) )
821 p_style->i_margin_h = 0;
822 p_style->i_margin_percent_h = atoi( psz_value );
826 p_style->i_margin_h = atoi( psz_value );
827 p_style->i_margin_percent_h = 0;
830 else if( !strcasecmp( "vertical-margin", psz_name ) )
832 if( strchr( psz_value, '%' ) )
834 p_style->i_margin_v = 0;
835 p_style->i_margin_percent_v = atoi( psz_value );
839 p_style->i_margin_v = atoi( psz_value );
840 p_style->i_margin_percent_v = 0;
858 static subpicture_region_t *ParseUSFString( decoder_t *p_dec,
860 subpicture_t *p_spu_in )
862 decoder_sys_t *p_sys = p_dec->p_sys;
863 subpicture_t *p_spu = p_spu_in;
864 subpicture_region_t *p_region_first = NULL;
865 subpicture_region_t *p_region_upto = p_region_first;
867 while( *psz_subtitle )
869 if( *psz_subtitle == '<' )
871 char *psz_end = NULL;
873 if(( !strncasecmp( psz_subtitle, "<text ", 6 )) ||
874 ( !strncasecmp( psz_subtitle, "<text>", 6 )))
876 psz_end = strcasestr( psz_subtitle, "</text>" );
880 subpicture_region_t *p_text_region;
882 psz_end += strcspn( psz_end, ">" ) + 1;
884 p_text_region = CreateTextRegion( p_dec,
887 psz_end - psz_subtitle,
892 p_text_region->psz_text = CreatePlainText( p_text_region->psz_html );
894 if( ! var_CreateGetBool( p_dec, "subsdec-formatted" ) )
896 free( p_text_region->psz_html );
897 p_text_region->psz_html = NULL;
901 if( !p_region_first )
903 p_region_first = p_region_upto = p_text_region;
905 else if( p_text_region )
907 p_region_upto->p_next = p_text_region;
908 p_region_upto = p_region_upto->p_next;
912 else if(( !strncasecmp( psz_subtitle, "<karaoke ", 9 )) ||
913 ( !strncasecmp( psz_subtitle, "<karaoke>", 9 )))
915 psz_end = strcasestr( psz_subtitle, "</karaoke>" );
919 subpicture_region_t *p_text_region;
921 psz_end += strcspn( psz_end, ">" ) + 1;
923 p_text_region = CreateTextRegion( p_dec,
926 psz_end - psz_subtitle,
931 if( ! var_CreateGetBool( p_dec, "subsdec-formatted" ) )
933 free( p_text_region->psz_html );
934 p_text_region->psz_html = NULL;
937 if( !p_region_first )
939 p_region_first = p_region_upto = p_text_region;
941 else if( p_text_region )
943 p_region_upto->p_next = p_text_region;
944 p_region_upto = p_region_upto->p_next;
948 else if(( !strncasecmp( psz_subtitle, "<image ", 7 )) ||
949 ( !strncasecmp( psz_subtitle, "<image>", 7 )))
951 subpicture_region_t *p_image_region = NULL;
953 char *psz_end = strcasestr( psz_subtitle, "</image>" );
954 char *psz_content = strchr( psz_subtitle, '>' );
955 int i_transparent = -1;
957 /* If a colorkey parameter is specified, then we have to map
958 * that index in the picture through as transparent (it is
959 * required by the USF spec but is also recommended that if the
960 * creator really wants a transparent colour that they use a
961 * type like PNG that properly supports it; this goes doubly
962 * for VLC because the pictures are stored internally in YUV
963 * and the resulting colour-matching may not produce the
966 char *psz_tmp = GrabAttributeValue( "colorkey", psz_subtitle );
969 if( *psz_tmp == '#' )
970 i_transparent = strtol( psz_tmp + 1, NULL, 16 ) & 0x00ffffff;
973 if( psz_content && ( psz_content < psz_end ) )
975 char *psz_filename = strndup( &psz_content[1], psz_end - &psz_content[1] );
978 p_image_region = LoadEmbeddedImage( p_dec, p_spu,
979 psz_filename, i_transparent );
980 free( psz_filename );
984 if( psz_end ) psz_end += strcspn( psz_end, ">" ) + 1;
988 SetupPositions( p_image_region, psz_subtitle );
990 p_image_region->p_next = NULL;
991 p_image_region->psz_text = NULL;
992 p_image_region->psz_html = NULL;
995 if( !p_region_first )
997 p_region_first = p_region_upto = p_image_region;
999 else if( p_image_region )
1001 p_region_upto->p_next = p_image_region;
1002 p_region_upto = p_region_upto->p_next;
1006 psz_subtitle = psz_end - 1;
1008 psz_subtitle += strcspn( psz_subtitle, ">" );
1014 return p_region_first;
1017 /*****************************************************************************
1018 * ParseUSFHeader: Retrieve global formatting information etc
1019 *****************************************************************************/
1020 static void ParseUSFHeader( decoder_t *p_dec )
1022 stream_t *p_sub = NULL;
1023 xml_t *p_xml = NULL;
1024 xml_reader_t *p_xml_reader = NULL;
1026 p_sub = stream_MemoryNew( VLC_OBJECT(p_dec),
1027 p_dec->fmt_in.p_extra,
1028 p_dec->fmt_in.i_extra,
1033 p_xml = xml_Create( p_dec );
1036 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
1039 /* Look for Root Node */
1040 if( xml_ReaderRead( p_xml_reader ) == 1 )
1042 char *psz_node = xml_ReaderName( p_xml_reader );
1044 if( !strcasecmp( "usfsubtitles", psz_node ) )
1045 ParseUSFHeaderTags( p_dec, p_xml_reader );
1050 xml_ReaderDelete( p_xml, p_xml_reader );
1052 xml_Delete( p_xml );
1054 stream_Delete( p_sub );
1057 /* Function now handles tags which has attribute values, and tries
1058 * to deal with &' commands too. It no longer modifies the string
1059 * in place, so that the original text can be reused
1061 static char *StripTags( char *psz_subtitle )
1063 char *psz_text_start;
1066 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
1067 if( !psz_text_start )
1070 while( *psz_subtitle )
1072 /* Mask out any pre-existing LFs in the subtitle */
1073 if( *psz_subtitle == '\n' )
1074 *psz_subtitle = ' ';
1076 if( *psz_subtitle == '<' )
1078 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
1081 psz_subtitle += strcspn( psz_subtitle, ">" );
1083 else if( *psz_subtitle == '&' )
1085 if( !strncasecmp( psz_subtitle, "<", 4 ))
1088 psz_subtitle += strcspn( psz_subtitle, ";" );
1090 else if( !strncasecmp( psz_subtitle, ">", 4 ))
1093 psz_subtitle += strcspn( psz_subtitle, ";" );
1095 else if( !strncasecmp( psz_subtitle, "&", 5 ))
1098 psz_subtitle += strcspn( psz_subtitle, ";" );
1100 else if( !strncasecmp( psz_subtitle, """, 6 ))
1103 psz_subtitle += strcspn( psz_subtitle, ";" );
1107 /* Assume it is just a normal ampersand */
1113 *psz_text++ = *psz_subtitle;
1119 psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
1121 return psz_text_start;
1124 /* Turn a HTML subtitle, turn into a plain-text version,
1125 * complete with sensible whitespace compaction
1128 static char *CreatePlainText( char *psz_subtitle )
1130 char *psz_text = StripTags( psz_subtitle );
1136 s = strpbrk( psz_text, "\t\r\n " );
1141 int i_whitespace = strspn( s, "\t\r\n " );
1143 /* Favour '\n' over other whitespaces - if one of these
1144 * occurs in the whitespace use a '\n' as our value,
1145 * otherwise just use a ' '
1147 for( k = 0; k < i_whitespace; k++ )
1148 if( s[k] == '\n' ) spc = '\n';
1150 if( i_whitespace > 1 )
1154 strlen( s ) - i_whitespace + 1 );
1158 s = strpbrk( s, "\t\r\n " );
1163 /****************************************************************************
1164 * download and resize image located at psz_url
1165 ***************************************************************************/
1166 static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec,
1167 subpicture_t *p_spu,
1168 const char *psz_filename,
1169 int i_transparent_color )
1171 decoder_sys_t *p_sys = p_dec->p_sys;
1172 subpicture_region_t *p_region;
1173 video_format_t fmt_out;
1175 picture_t *p_pic = NULL;
1177 for( k = 0; k < p_sys->i_images; k++ )
1179 if( p_sys->pp_images &&
1180 !strcmp( p_sys->pp_images[k]->psz_filename, psz_filename ) )
1182 p_pic = p_sys->pp_images[k]->p_pic;
1189 msg_Err( p_dec, "Unable to read image %s", psz_filename );
1193 /* Display the feed's image */
1194 memset( &fmt_out, 0, sizeof( video_format_t));
1196 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
1197 fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
1198 fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
1200 fmt_out.i_visible_width = p_pic->format.i_visible_width;
1202 fmt_out.i_visible_height = p_pic->format.i_visible_height;
1204 p_region = subpicture_region_New( &fmt_out );
1207 msg_Err( p_dec, "cannot allocate SPU region" );
1210 assert( p_pic->format.i_chroma == VLC_FOURCC('Y','U','V','A') );
1211 /* FIXME the copy is probably not needed anymore */
1212 picture_CopyPixels( p_region->p_picture, p_pic );
1214 /* This isn't the best way to do this - if you really want transparency, then
1215 * you're much better off using an image type that supports it like PNG. The
1216 * spec requires this support though.
1218 if( i_transparent_color > 0 )
1220 int i_r = ( i_transparent_color >> 16 ) & 0xff;
1221 int i_g = ( i_transparent_color >> 8 ) & 0xff;
1222 int i_b = ( i_transparent_color ) & 0xff;
1224 /* FIXME it cannot work as the yuv conversion code will probably NOT match
1226 int i_y = ( ( ( 66 * i_r + 129 * i_g + 25 * i_b + 128 ) >> 8 ) + 16 );
1227 int i_u = ( ( -38 * i_r - 74 * i_g + 112 * i_b + 128 ) >> 8 ) + 128 ;
1228 int i_v = ( ( 112 * i_r - 94 * i_g - 18 * i_b + 128 ) >> 8 ) + 128 ;
1230 assert( p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','A') );
1231 for( unsigned int y = 0; y < p_region->fmt.i_height; y++ )
1233 for( unsigned int x = 0; x < p_region->fmt.i_width; x++ )
1235 if( p_region->p_picture->Y_PIXELS[y*p_region->p_picture->Y_PITCH + x] != i_y ||
1236 p_region->p_picture->U_PIXELS[y*p_region->p_picture->U_PITCH + x] != i_u ||
1237 p_region->p_picture->V_PIXELS[y*p_region->p_picture->V_PITCH + x] != i_v )
1239 p_region->p_picture->A_PIXELS[y*p_region->p_picture->A_PITCH + x] = 0;