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 )
122 p_spu = ParseText( p_dec, p_block );
124 block_Release( p_block );
130 /*****************************************************************************
131 * CloseDecoder: clean up the decoder
132 *****************************************************************************/
133 static void CloseDecoder( vlc_object_t *p_this )
135 decoder_t *p_dec = (decoder_t *)p_this;
136 decoder_sys_t *p_sys = p_dec->p_sys;
138 if( p_sys->pp_ssa_styles )
141 for( i = 0; i < p_sys->i_ssa_styles; i++ )
143 if( !p_sys->pp_ssa_styles[i] )
146 free( p_sys->pp_ssa_styles[i]->psz_stylename );
147 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
148 free( p_sys->pp_ssa_styles[i] );
150 TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
152 if( p_sys->pp_images )
155 for( i = 0; i < p_sys->i_images; i++ )
157 if( !p_sys->pp_images[i] )
160 if( p_sys->pp_images[i]->p_pic )
161 picture_Release( p_sys->pp_images[i]->p_pic );
162 free( p_sys->pp_images[i]->psz_filename );
164 free( p_sys->pp_images[i] );
166 TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
172 /*****************************************************************************
173 * ParseText: parse an text subtitle packet and send it to the video output
174 *****************************************************************************/
175 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
177 decoder_sys_t *p_sys = p_dec->p_sys;
178 subpicture_t *p_spu = NULL;
179 char *psz_subtitle = NULL;
181 /* We cannot display a subpicture with no date */
182 if( p_block->i_pts == 0 )
184 msg_Warn( p_dec, "subtitle without a date" );
188 /* Check validity of packet data */
189 /* An "empty" line containing only \0 can be used to force
190 and ephemer picture from the screen */
191 if( p_block->i_buffer < 1 )
193 msg_Warn( p_dec, "no subtitle data" );
197 /* Should be resiliant against bad subtitles */
198 psz_subtitle = strndup( (const char *)p_block->p_buffer,
200 if( psz_subtitle == NULL )
203 /* USF Subtitles are mandated to be UTF-8 -- make sure it is */
204 if (EnsureUTF8( psz_subtitle ) == NULL)
206 msg_Err( p_dec, "USF subtitles must be in UTF-8 format.\n"
207 "This stream contains USF subtitles which aren't." );
210 /* Create the subpicture unit */
211 p_spu = p_dec->pf_spu_buffer_new( p_dec );
214 msg_Warn( p_dec, "can't get spu buffer" );
215 free( psz_subtitle );
219 /* Decode USF strings */
220 p_spu->p_region = ParseUSFString( p_dec, psz_subtitle, p_spu );
222 p_spu->i_start = p_block->i_pts;
223 p_spu->i_stop = p_block->i_pts + p_block->i_length;
224 p_spu->b_ephemer = (p_block->i_length == 0);
225 p_spu->b_absolute = false;
226 p_spu->i_original_picture_width = p_sys->i_original_width;
227 p_spu->i_original_picture_height = p_sys->i_original_height;
229 free( psz_subtitle );
234 static char *GrabAttributeValue( const char *psz_attribute,
235 const char *psz_tag_start )
237 if( psz_attribute && psz_tag_start )
239 char *psz_tag_end = strchr( psz_tag_start, '>' );
240 char *psz_found = strcasestr( psz_tag_start, psz_attribute );
244 psz_found += strlen( psz_attribute );
246 if(( *(psz_found++) == '=' ) &&
247 ( *(psz_found++) == '\"' ))
249 if( psz_found < psz_tag_end )
251 int i_len = strcspn( psz_found, "\"" );
252 return strndup( psz_found, i_len );
260 static ssa_style_t *ParseStyle( decoder_sys_t *p_sys, char *psz_subtitle )
262 ssa_style_t *p_style = NULL;
263 char *psz_style = GrabAttributeValue( "style", psz_subtitle );
269 for( i = 0; i < p_sys->i_ssa_styles; i++ )
271 if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
272 p_style = p_sys->pp_ssa_styles[i];
279 static int ParsePositionAttributeList( char *psz_subtitle, int *i_align,
284 char *psz_align = GrabAttributeValue( "alignment", psz_subtitle );
285 char *psz_margin_x = GrabAttributeValue( "horizontal-margin", psz_subtitle );
286 char *psz_margin_y = GrabAttributeValue( "vertical-margin", psz_subtitle );
288 char *psz_relative = GrabAttributeValue( "relative-to", psz_subtitle );
289 char *psz_rotate_x = GrabAttributeValue( "rotate-x", psz_subtitle );
290 char *psz_rotate_y = GrabAttributeValue( "rotate-y", psz_subtitle );
291 char *psz_rotate_z = GrabAttributeValue( "rotate-z", psz_subtitle );
294 *i_align = SUBPICTURE_ALIGN_BOTTOM;
300 if( !strcasecmp( "TopLeft", psz_align ) )
301 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
302 else if( !strcasecmp( "TopCenter", psz_align ) )
303 *i_align = SUBPICTURE_ALIGN_TOP;
304 else if( !strcasecmp( "TopRight", psz_align ) )
305 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
306 else if( !strcasecmp( "MiddleLeft", psz_align ) )
307 *i_align = SUBPICTURE_ALIGN_LEFT;
308 else if( !strcasecmp( "MiddleCenter", psz_align ) )
310 else if( !strcasecmp( "MiddleRight", psz_align ) )
311 *i_align = SUBPICTURE_ALIGN_RIGHT;
312 else if( !strcasecmp( "BottomLeft", psz_align ) )
313 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
314 else if( !strcasecmp( "BottomCenter", psz_align ) )
315 *i_align = SUBPICTURE_ALIGN_BOTTOM;
316 else if( !strcasecmp( "BottomRight", psz_align ) )
317 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
319 i_mask |= ATTRIBUTE_ALIGNMENT;
324 *i_x = atoi( psz_margin_x );
325 if( strchr( psz_margin_x, '%' ) )
326 i_mask |= ATTRIBUTE_X_PERCENT;
328 i_mask |= ATTRIBUTE_X;
330 free( psz_margin_x );
334 *i_y = atoi( psz_margin_y );
335 if( strchr( psz_margin_y, '%' ) )
336 i_mask |= ATTRIBUTE_Y_PERCENT;
338 i_mask |= ATTRIBUTE_Y;
340 free( psz_margin_y );
345 static void SetupPositions( subpicture_region_t *p_region, char *psz_subtitle )
351 i_mask = ParsePositionAttributeList( psz_subtitle, &i_align, &i_x, &i_y );
353 if( i_mask & ATTRIBUTE_ALIGNMENT )
354 p_region->i_align = i_align;
356 /* TODO: Setup % based offsets properly, without adversely affecting
357 * everything else in vlc. Will address with separate patch, to
358 * prevent this one being any more complicated.
360 if( i_mask & ATTRIBUTE_X )
362 else if( i_mask & ATTRIBUTE_X_PERCENT )
365 if( i_mask & ATTRIBUTE_Y )
367 else if( i_mask & ATTRIBUTE_Y_PERCENT )
371 static subpicture_region_t *CreateTextRegion( decoder_t *p_dec,
377 decoder_sys_t *p_sys = p_dec->p_sys;
378 subpicture_region_t *p_text_region;
381 /* Create a new subpicture region */
382 memset( &fmt, 0, sizeof(video_format_t) );
383 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
385 fmt.i_width = fmt.i_height = 0;
386 fmt.i_x_offset = fmt.i_y_offset = 0;
387 p_text_region = subpicture_region_New( &fmt );
389 if( p_text_region != NULL )
391 ssa_style_t *p_style = NULL;
393 p_text_region->psz_text = NULL;
394 p_text_region->psz_html = strndup( psz_subtitle, i_len );
395 if( ! p_text_region->psz_html )
397 subpicture_region_Delete( p_text_region );
401 p_style = ParseStyle( p_sys, p_text_region->psz_html );
406 for( i = 0; i < p_sys->i_ssa_styles; i++ )
408 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
409 p_style = p_sys->pp_ssa_styles[i];
415 msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename );
417 p_text_region->p_style = &p_style->font_style;
418 p_text_region->i_align = p_style->i_align;
420 /* TODO: Setup % based offsets properly, without adversely affecting
421 * everything else in vlc. Will address with separate patch,
422 * to prevent this one being any more complicated.
424 * p_style->i_margin_percent_h;
425 * p_style->i_margin_percent_v;
427 p_text_region->i_x = p_style->i_margin_h;
428 p_text_region->i_y = p_style->i_margin_v;
433 p_text_region->i_align = SUBPICTURE_ALIGN_BOTTOM | i_sys_align;
434 p_text_region->i_x = i_sys_align ? 20 : 0;
435 p_text_region->i_y = 10;
437 /* Look for position arguments which may override the style-based
440 SetupPositions( p_text_region, psz_subtitle );
442 p_text_region->p_next = NULL;
444 return p_text_region;
447 static int ParseImageAttachments( decoder_t *p_dec )
449 decoder_sys_t *p_sys = p_dec->p_sys;
450 input_attachment_t **pp_attachments;
451 int i_attachments_cnt;
454 if( VLC_SUCCESS != decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments_cnt ))
457 for( k = 0; k < i_attachments_cnt; k++ )
459 input_attachment_t *p_attach = pp_attachments[k];
461 vlc_fourcc_t type = 0;
463 if( ( !strcmp( p_attach->psz_mime, "image/bmp" ) ) || /* BMP */
464 ( !strcmp( p_attach->psz_mime, "image/x-bmp" ) ) ||
465 ( !strcmp( p_attach->psz_mime, "image/x-bitmap" ) ) ||
466 ( !strcmp( p_attach->psz_mime, "image/x-ms-bmp" ) ) )
468 type = VLC_FOURCC('b','m','p',' ');
470 else if( ( !strcmp( p_attach->psz_mime, "image/x-portable-anymap" ) ) || /* PNM */
471 ( !strcmp( p_attach->psz_mime, "image/x-portable-bitmap" ) ) || /* PBM */
472 ( !strcmp( p_attach->psz_mime, "image/x-portable-graymap" ) ) || /* PGM */
473 ( !strcmp( p_attach->psz_mime, "image/x-portable-pixmap" ) ) ) /* PPM */
475 type = VLC_FOURCC('p','n','m',' ');
477 else if ( !strcmp( p_attach->psz_mime, "image/gif" ) ) /* GIF */
478 type = VLC_FOURCC('g','i','f',' ');
479 else if ( !strcmp( p_attach->psz_mime, "image/jpeg" ) ) /* JPG, JPEG */
480 type = VLC_FOURCC('j','p','e','g');
481 else if ( !strcmp( p_attach->psz_mime, "image/pcx" ) ) /* PCX */
482 type = VLC_FOURCC('p','c','x',' ');
483 else if ( !strcmp( p_attach->psz_mime, "image/png" ) ) /* PNG */
484 type = VLC_FOURCC('p','n','g',' ');
485 else if ( !strcmp( p_attach->psz_mime, "image/tiff" ) ) /* TIF, TIFF */
486 type = VLC_FOURCC('t','i','f','f');
487 else if ( !strcmp( p_attach->psz_mime, "image/x-tga" ) ) /* TGA */
488 type = VLC_FOURCC('t','g','a',' ');
489 else if ( !strcmp( p_attach->psz_mime, "image/x-xpixmap") ) /* XPM */
490 type = VLC_FOURCC('x','p','m',' ');
493 ( p_attach->i_data > 0 ) &&
494 ( p_attach->p_data != NULL ) )
496 picture_t *p_pic = NULL;
497 image_handler_t *p_image;
499 p_image = image_HandlerCreate( p_dec );
500 if( p_image != NULL )
504 p_block = block_New( p_image->p_parent, p_attach->i_data );
506 if( p_block != NULL )
508 video_format_t fmt_in;
509 video_format_t fmt_out;
511 memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data );
513 memset( &fmt_in, 0, sizeof( video_format_t));
514 memset( &fmt_out, 0, sizeof( video_format_t));
516 fmt_in.i_chroma = type;
517 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
519 /* Find a suitable decoder module */
520 if( module_exists( "sdl_image" ) )
522 /* ffmpeg thinks it can handle bmp properly but it can't (at least
523 * not all of them), so use sdl_image if it is available */
527 var_Create( p_dec, "codec", VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
528 val.psz_string = (char*) "sdl_image";
529 var_Set( p_dec, "codec", val );
532 p_pic = image_Read( p_image, p_block, &fmt_in, &fmt_out );
533 var_Destroy( p_dec, "codec" );
536 image_HandlerDelete( p_image );
540 image_attach_t *p_picture = malloc( sizeof(image_attach_t) );
544 p_picture->psz_filename = strdup( p_attach->psz_name );
545 p_picture->p_pic = p_pic;
547 TAB_APPEND( p_sys->i_images, p_sys->pp_images, p_picture );
551 vlc_input_attachment_Delete( pp_attachments[ k ] );
553 free( pp_attachments );
558 static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
560 decoder_sys_t *p_sys = p_dec->p_sys;
562 ssa_style_t *p_style = NULL;
563 int i_style_level = 0;
564 int i_metadata_level = 0;
566 while ( xml_ReaderRead( p_xml_reader ) == 1 )
568 switch ( xml_ReaderNodeType( p_xml_reader ) )
570 case XML_READER_TEXT:
571 case XML_READER_NONE:
573 case XML_READER_ENDELEM:
574 psz_node = xml_ReaderName( p_xml_reader );
578 switch (i_style_level)
581 if( !strcasecmp( "metadata", psz_node ) && (i_metadata_level == 1) )
587 if( !strcasecmp( "styles", psz_node ) )
593 if( !strcasecmp( "style", psz_node ) )
595 TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
605 case XML_READER_STARTELEM:
606 psz_node = xml_ReaderName( p_xml_reader );
611 if( !strcasecmp( "metadata", psz_node ) && (i_style_level == 0) )
615 else if( !strcasecmp( "resolution", psz_node ) &&
616 ( i_metadata_level == 1) )
618 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
620 char *psz_name = xml_ReaderName ( p_xml_reader );
621 char *psz_value = xml_ReaderValue ( p_xml_reader );
623 if( psz_name && psz_value )
625 if( !strcasecmp( "x", psz_name ) )
626 p_sys->i_original_width = atoi( psz_value );
627 else if( !strcasecmp( "y", psz_name ) )
628 p_sys->i_original_height = atoi( psz_value );
634 else if( !strcasecmp( "styles", psz_node ) && (i_style_level == 0) )
638 else if( !strcasecmp( "style", psz_node ) && (i_style_level == 1) )
642 p_style = calloc( 1, sizeof(ssa_style_t) );
648 /* All styles are supposed to default to Default, and then
649 * one or more settings are over-ridden.
650 * At the moment this only effects styles defined AFTER
654 for( i = 0; i < p_sys->i_ssa_styles; i++ )
656 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
658 ssa_style_t *p_default_style = p_sys->pp_ssa_styles[i];
660 memcpy( p_style, p_default_style, sizeof( ssa_style_t ) );
661 p_style->font_style.psz_fontname = strdup( p_style->font_style.psz_fontname );
662 p_style->psz_stylename = NULL;
666 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
668 char *psz_name = xml_ReaderName ( p_xml_reader );
669 char *psz_value = xml_ReaderValue ( p_xml_reader );
671 if( psz_name && psz_value )
673 if( !strcasecmp( "name", psz_name ) )
674 p_style->psz_stylename = strdup( psz_value);
680 else if( !strcasecmp( "fontstyle", psz_node ) && (i_style_level == 2) )
682 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
684 char *psz_name = xml_ReaderName ( p_xml_reader );
685 char *psz_value = xml_ReaderValue ( p_xml_reader );
687 if( psz_name && psz_value )
689 if( !strcasecmp( "face", psz_name ) )
691 free( p_style->font_style.psz_fontname );
692 p_style->font_style.psz_fontname = strdup( psz_value );
694 else if( !strcasecmp( "size", psz_name ) )
696 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
698 int i_value = atoi( psz_value );
700 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
701 p_style->font_style.i_font_size +=
702 ( i_value * p_style->font_style.i_font_size ) / 10;
703 else if( i_value < -5 )
704 p_style->font_style.i_font_size = - i_value;
705 else if( i_value > 5 )
706 p_style->font_style.i_font_size = i_value;
709 p_style->font_style.i_font_size = atoi( psz_value );
711 else if( !strcasecmp( "italic", psz_name ) )
713 if( !strcasecmp( "yes", psz_value ))
714 p_style->font_style.i_style_flags |= STYLE_ITALIC;
716 p_style->font_style.i_style_flags &= ~STYLE_ITALIC;
718 else if( !strcasecmp( "weight", psz_name ) )
720 if( !strcasecmp( "bold", psz_value ))
721 p_style->font_style.i_style_flags |= STYLE_BOLD;
723 p_style->font_style.i_style_flags &= ~STYLE_BOLD;
725 else if( !strcasecmp( "underline", psz_name ) )
727 if( !strcasecmp( "yes", psz_value ))
728 p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
730 p_style->font_style.i_style_flags &= ~STYLE_UNDERLINE;
732 else if( !strcasecmp( "color", psz_name ) )
734 if( *psz_value == '#' )
736 unsigned long col = strtol(psz_value+1, NULL, 16);
737 p_style->font_style.i_font_color = (col & 0x00ffffff);
738 p_style->font_style.i_font_alpha = (col >> 24) & 0xff;
741 else if( !strcasecmp( "outline-color", psz_name ) )
743 if( *psz_value == '#' )
745 unsigned long col = strtol(psz_value+1, NULL, 16);
746 p_style->font_style.i_outline_color = (col & 0x00ffffff);
747 p_style->font_style.i_outline_alpha = (col >> 24) & 0xff;
750 else if( !strcasecmp( "outline-level", psz_name ) )
752 p_style->font_style.i_outline_width = atoi( psz_value );
754 else if( !strcasecmp( "shadow-color", psz_name ) )
756 if( *psz_value == '#' )
758 unsigned long col = strtol(psz_value+1, NULL, 16);
759 p_style->font_style.i_shadow_color = (col & 0x00ffffff);
760 p_style->font_style.i_shadow_alpha = (col >> 24) & 0xff;
763 else if( !strcasecmp( "shadow-level", psz_name ) )
765 p_style->font_style.i_shadow_width = atoi( psz_value );
767 else if( !strcasecmp( "back-color", psz_name ) )
769 if( *psz_value == '#' )
771 unsigned long col = strtol(psz_value+1, NULL, 16);
772 p_style->font_style.i_karaoke_background_color = (col & 0x00ffffff);
773 p_style->font_style.i_karaoke_background_alpha = (col >> 24) & 0xff;
776 else if( !strcasecmp( "spacing", psz_name ) )
778 p_style->font_style.i_spacing = atoi( psz_value );
785 else if( !strcasecmp( "position", psz_node ) && (i_style_level == 2) )
787 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
789 char *psz_name = xml_ReaderName ( p_xml_reader );
790 char *psz_value = xml_ReaderValue ( p_xml_reader );
792 if( psz_name && psz_value )
794 if( !strcasecmp( "alignment", psz_name ) )
796 if( !strcasecmp( "TopLeft", psz_value ) )
797 p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
798 else if( !strcasecmp( "TopCenter", psz_value ) )
799 p_style->i_align = SUBPICTURE_ALIGN_TOP;
800 else if( !strcasecmp( "TopRight", psz_value ) )
801 p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
802 else if( !strcasecmp( "MiddleLeft", psz_value ) )
803 p_style->i_align = SUBPICTURE_ALIGN_LEFT;
804 else if( !strcasecmp( "MiddleCenter", psz_value ) )
805 p_style->i_align = 0;
806 else if( !strcasecmp( "MiddleRight", psz_value ) )
807 p_style->i_align = SUBPICTURE_ALIGN_RIGHT;
808 else if( !strcasecmp( "BottomLeft", psz_value ) )
809 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
810 else if( !strcasecmp( "BottomCenter", psz_value ) )
811 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM;
812 else if( !strcasecmp( "BottomRight", psz_value ) )
813 p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
815 else if( !strcasecmp( "horizontal-margin", psz_name ) )
817 if( strchr( psz_value, '%' ) )
819 p_style->i_margin_h = 0;
820 p_style->i_margin_percent_h = atoi( psz_value );
824 p_style->i_margin_h = atoi( psz_value );
825 p_style->i_margin_percent_h = 0;
828 else if( !strcasecmp( "vertical-margin", psz_name ) )
830 if( strchr( psz_value, '%' ) )
832 p_style->i_margin_v = 0;
833 p_style->i_margin_percent_v = atoi( psz_value );
837 p_style->i_margin_v = atoi( psz_value );
838 p_style->i_margin_percent_v = 0;
856 static subpicture_region_t *ParseUSFString( decoder_t *p_dec,
858 subpicture_t *p_spu_in )
860 decoder_sys_t *p_sys = p_dec->p_sys;
861 subpicture_t *p_spu = p_spu_in;
862 subpicture_region_t *p_region_first = NULL;
863 subpicture_region_t *p_region_upto = p_region_first;
865 while( *psz_subtitle )
867 if( *psz_subtitle == '<' )
869 char *psz_end = NULL;
871 if(( !strncasecmp( psz_subtitle, "<text ", 6 )) ||
872 ( !strncasecmp( psz_subtitle, "<text>", 6 )))
874 psz_end = strcasestr( psz_subtitle, "</text>" );
878 subpicture_region_t *p_text_region;
880 psz_end += strcspn( psz_end, ">" ) + 1;
882 p_text_region = CreateTextRegion( p_dec,
885 psz_end - psz_subtitle,
890 p_text_region->psz_text = CreatePlainText( p_text_region->psz_html );
892 if( ! var_CreateGetBool( p_dec, "subsdec-formatted" ) )
894 free( p_text_region->psz_html );
895 p_text_region->psz_html = NULL;
899 if( !p_region_first )
901 p_region_first = p_region_upto = p_text_region;
903 else if( p_text_region )
905 p_region_upto->p_next = p_text_region;
906 p_region_upto = p_region_upto->p_next;
910 else if(( !strncasecmp( psz_subtitle, "<karaoke ", 9 )) ||
911 ( !strncasecmp( psz_subtitle, "<karaoke>", 9 )))
913 psz_end = strcasestr( psz_subtitle, "</karaoke>" );
917 subpicture_region_t *p_text_region;
919 psz_end += strcspn( psz_end, ">" ) + 1;
921 p_text_region = CreateTextRegion( p_dec,
924 psz_end - psz_subtitle,
929 if( ! var_CreateGetBool( p_dec, "subsdec-formatted" ) )
931 free( p_text_region->psz_html );
932 p_text_region->psz_html = NULL;
935 if( !p_region_first )
937 p_region_first = p_region_upto = p_text_region;
939 else if( p_text_region )
941 p_region_upto->p_next = p_text_region;
942 p_region_upto = p_region_upto->p_next;
946 else if(( !strncasecmp( psz_subtitle, "<image ", 7 )) ||
947 ( !strncasecmp( psz_subtitle, "<image>", 7 )))
949 subpicture_region_t *p_image_region = NULL;
951 char *psz_end = strcasestr( psz_subtitle, "</image>" );
952 char *psz_content = strchr( psz_subtitle, '>' );
953 int i_transparent = -1;
955 /* If a colorkey parameter is specified, then we have to map
956 * that index in the picture through as transparent (it is
957 * required by the USF spec but is also recommended that if the
958 * creator really wants a transparent colour that they use a
959 * type like PNG that properly supports it; this goes doubly
960 * for VLC because the pictures are stored internally in YUV
961 * and the resulting colour-matching may not produce the
964 char *psz_tmp = GrabAttributeValue( "colorkey", psz_subtitle );
967 if( *psz_tmp == '#' )
968 i_transparent = strtol( psz_tmp + 1, NULL, 16 ) & 0x00ffffff;
971 if( psz_content && ( psz_content < psz_end ) )
973 char *psz_filename = strndup( &psz_content[1], psz_end - &psz_content[1] );
976 p_image_region = LoadEmbeddedImage( p_dec, p_spu,
977 psz_filename, i_transparent );
978 free( psz_filename );
982 if( psz_end ) psz_end += strcspn( psz_end, ">" ) + 1;
986 SetupPositions( p_image_region, psz_subtitle );
988 p_image_region->p_next = NULL;
989 p_image_region->psz_text = NULL;
990 p_image_region->psz_html = NULL;
993 if( !p_region_first )
995 p_region_first = p_region_upto = p_image_region;
997 else if( p_image_region )
999 p_region_upto->p_next = p_image_region;
1000 p_region_upto = p_region_upto->p_next;
1004 psz_subtitle = psz_end - 1;
1006 psz_subtitle += strcspn( psz_subtitle, ">" );
1012 return p_region_first;
1015 /*****************************************************************************
1016 * ParseUSFHeader: Retrieve global formatting information etc
1017 *****************************************************************************/
1018 static void ParseUSFHeader( decoder_t *p_dec )
1020 stream_t *p_sub = NULL;
1021 xml_t *p_xml = NULL;
1022 xml_reader_t *p_xml_reader = NULL;
1024 p_sub = stream_MemoryNew( VLC_OBJECT(p_dec),
1025 p_dec->fmt_in.p_extra,
1026 p_dec->fmt_in.i_extra,
1031 p_xml = xml_Create( p_dec );
1034 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
1037 /* Look for Root Node */
1038 if( xml_ReaderRead( p_xml_reader ) == 1 )
1040 char *psz_node = xml_ReaderName( p_xml_reader );
1042 if( !strcasecmp( "usfsubtitles", psz_node ) )
1043 ParseUSFHeaderTags( p_dec, p_xml_reader );
1048 xml_ReaderDelete( p_xml, p_xml_reader );
1050 xml_Delete( p_xml );
1052 stream_Delete( p_sub );
1055 /* Function now handles tags which has attribute values, and tries
1056 * to deal with &' commands too. It no longer modifies the string
1057 * in place, so that the original text can be reused
1059 static char *StripTags( char *psz_subtitle )
1061 char *psz_text_start;
1064 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
1065 if( !psz_text_start )
1068 while( *psz_subtitle )
1070 /* Mask out any pre-existing LFs in the subtitle */
1071 if( *psz_subtitle == '\n' )
1072 *psz_subtitle = ' ';
1074 if( *psz_subtitle == '<' )
1076 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
1079 psz_subtitle += strcspn( psz_subtitle, ">" );
1081 else if( *psz_subtitle == '&' )
1083 if( !strncasecmp( psz_subtitle, "<", 4 ))
1086 psz_subtitle += strcspn( psz_subtitle, ";" );
1088 else if( !strncasecmp( psz_subtitle, ">", 4 ))
1091 psz_subtitle += strcspn( psz_subtitle, ";" );
1093 else if( !strncasecmp( psz_subtitle, "&", 5 ))
1096 psz_subtitle += strcspn( psz_subtitle, ";" );
1098 else if( !strncasecmp( psz_subtitle, """, 6 ))
1101 psz_subtitle += strcspn( psz_subtitle, ";" );
1105 /* Assume it is just a normal ampersand */
1111 *psz_text++ = *psz_subtitle;
1117 psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
1119 return psz_text_start;
1122 /* Turn a HTML subtitle, turn into a plain-text version,
1123 * complete with sensible whitespace compaction
1126 static char *CreatePlainText( char *psz_subtitle )
1128 char *psz_text = StripTags( psz_subtitle );
1134 s = strpbrk( psz_text, "\t\r\n " );
1139 int i_whitespace = strspn( s, "\t\r\n " );
1141 /* Favour '\n' over other whitespaces - if one of these
1142 * occurs in the whitespace use a '\n' as our value,
1143 * otherwise just use a ' '
1145 for( k = 0; k < i_whitespace; k++ )
1146 if( s[k] == '\n' ) spc = '\n';
1148 if( i_whitespace > 1 )
1152 strlen( s ) - i_whitespace + 1 );
1156 s = strpbrk( s, "\t\r\n " );
1161 /****************************************************************************
1162 * download and resize image located at psz_url
1163 ***************************************************************************/
1164 static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec,
1165 subpicture_t *p_spu,
1166 const char *psz_filename,
1167 int i_transparent_color )
1169 decoder_sys_t *p_sys = p_dec->p_sys;
1170 subpicture_region_t *p_region;
1171 video_format_t fmt_out;
1173 picture_t *p_pic = NULL;
1175 for( k = 0; k < p_sys->i_images; k++ )
1177 if( p_sys->pp_images &&
1178 !strcmp( p_sys->pp_images[k]->psz_filename, psz_filename ) )
1180 p_pic = p_sys->pp_images[k]->p_pic;
1187 msg_Err( p_dec, "Unable to read image %s", psz_filename );
1191 /* Display the feed's image */
1192 memset( &fmt_out, 0, sizeof( video_format_t));
1194 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
1195 fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
1196 fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
1198 fmt_out.i_visible_width = p_pic->format.i_visible_width;
1200 fmt_out.i_visible_height = p_pic->format.i_visible_height;
1202 p_region = subpicture_region_New( &fmt_out );
1205 msg_Err( p_dec, "cannot allocate SPU region" );
1208 assert( p_pic->format.i_chroma == VLC_FOURCC('Y','U','V','A') );
1209 /* FIXME the copy is probably not needed anymore */
1210 picture_CopyPixels( p_region->p_picture, p_pic );
1212 /* This isn't the best way to do this - if you really want transparency, then
1213 * you're much better off using an image type that supports it like PNG. The
1214 * spec requires this support though.
1216 if( i_transparent_color > 0 )
1218 int i_r = ( i_transparent_color >> 16 ) & 0xff;
1219 int i_g = ( i_transparent_color >> 8 ) & 0xff;
1220 int i_b = ( i_transparent_color ) & 0xff;
1222 /* FIXME it cannot work as the yuv conversion code will probably NOT match
1224 int i_y = ( ( ( 66 * i_r + 129 * i_g + 25 * i_b + 128 ) >> 8 ) + 16 );
1225 int i_u = ( ( -38 * i_r - 74 * i_g + 112 * i_b + 128 ) >> 8 ) + 128 ;
1226 int i_v = ( ( 112 * i_r - 94 * i_g - 18 * i_b + 128 ) >> 8 ) + 128 ;
1228 assert( p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','A') );
1229 for( unsigned int y = 0; y < p_region->fmt.i_height; y++ )
1231 for( unsigned int x = 0; x < p_region->fmt.i_width; x++ )
1233 if( p_region->p_picture->Y_PIXELS[y*p_region->p_picture->Y_PITCH + x] != i_y ||
1234 p_region->p_picture->U_PIXELS[y*p_region->p_picture->U_PITCH + x] != i_u ||
1235 p_region->p_picture->V_PIXELS[y*p_region->p_picture->V_PITCH + x] != i_v )
1237 p_region->p_picture->A_PIXELS[y*p_region->p_picture->A_PITCH + x] = 0;