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>
29 #include <vlc_modules.h>
32 /*****************************************************************************
34 *****************************************************************************/
35 static int OpenDecoder ( vlc_object_t
* );
36 static void CloseDecoder ( vlc_object_t
* );
38 static subpicture_t
*DecodeBlock ( decoder_t
*, block_t
** );
39 static char *CreatePlainText( char * );
40 static int ParseImageAttachments( decoder_t
*p_dec
);
42 static subpicture_t
*ParseText ( decoder_t
*, block_t
* );
43 static void ParseUSFHeader( decoder_t
* );
44 static subpicture_region_t
*ParseUSFString( decoder_t
*, char * );
45 static subpicture_region_t
*LoadEmbeddedImage( decoder_t
*p_dec
, const char *psz_filename
, int i_transparent_color
);
47 /*****************************************************************************
49 *****************************************************************************/
52 set_capability( "decoder", 40 )
53 set_shortname( N_("USFSubs"))
54 set_description( N_("USF subtitles decoder") )
55 set_callbacks( OpenDecoder
, CloseDecoder
)
56 set_category( CAT_INPUT
)
57 set_subcategory( SUBCAT_INPUT_SCODEC
)
58 /* We inherit subsdec-align and subsdec-formatted from subsdec.c */
61 /*****************************************************************************
62 * OpenDecoder: probe the decoder and return score
63 *****************************************************************************
64 * Tries to launch a decoder and return score so that the interface is able
66 *****************************************************************************/
67 static int OpenDecoder( vlc_object_t
*p_this
)
69 decoder_t
*p_dec
= (decoder_t
*)p_this
;
72 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_USF
)
75 /* Allocate the memory needed to store the decoder's structure */
76 if( ( p_dec
->p_sys
= p_sys
= calloc(1, sizeof(decoder_sys_t
)) ) == NULL
)
79 p_dec
->pf_decode_sub
= DecodeBlock
;
80 p_dec
->fmt_out
.i_cat
= SPU_ES
;
81 p_dec
->fmt_out
.i_codec
= 0;
83 /* Unused fields of p_sys - not needed for USF decoding */
85 p_sys
->iconv_handle
= (vlc_iconv_t
)-1;
86 p_sys
->b_autodetect_utf8
= false;
90 p_sys
->i_original_height
= 0;
91 p_sys
->i_original_width
= 0;
92 TAB_INIT( p_sys
->i_ssa_styles
, p_sys
->pp_ssa_styles
);
93 TAB_INIT( p_sys
->i_images
, p_sys
->pp_images
);
95 /* USF subtitles are mandated to be UTF-8, so don't need vlc_iconv */
97 p_sys
->i_align
= var_CreateGetInteger( p_dec
, "subsdec-align" );
99 ParseImageAttachments( p_dec
);
101 if( var_CreateGetBool( p_dec
, "subsdec-formatted" ) )
103 if( p_dec
->fmt_in
.i_extra
> 0 )
104 ParseUSFHeader( p_dec
);
110 /****************************************************************************
111 * DecodeBlock: the whole thing
112 ****************************************************************************
113 * This function must be fed with complete subtitles units.
114 ****************************************************************************/
115 static subpicture_t
*DecodeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
120 if( !pp_block
|| *pp_block
== NULL
)
125 p_spu
= ParseText( p_dec
, p_block
);
127 block_Release( p_block
);
133 /*****************************************************************************
134 * CloseDecoder: clean up the decoder
135 *****************************************************************************/
136 static void CloseDecoder( vlc_object_t
*p_this
)
138 decoder_t
*p_dec
= (decoder_t
*)p_this
;
139 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
141 if( p_sys
->pp_ssa_styles
)
144 for( i
= 0; i
< p_sys
->i_ssa_styles
; i
++ )
146 if( !p_sys
->pp_ssa_styles
[i
] )
149 free( p_sys
->pp_ssa_styles
[i
]->psz_stylename
);
150 //FIXME: Make font_style a pointer and use text_style_* functions
151 free( p_sys
->pp_ssa_styles
[i
]->font_style
.psz_fontname
);
152 free( p_sys
->pp_ssa_styles
[i
] );
154 TAB_CLEAN( p_sys
->i_ssa_styles
, p_sys
->pp_ssa_styles
);
156 if( p_sys
->pp_images
)
159 for( i
= 0; i
< p_sys
->i_images
; i
++ )
161 if( !p_sys
->pp_images
[i
] )
164 if( p_sys
->pp_images
[i
]->p_pic
)
165 picture_Release( p_sys
->pp_images
[i
]->p_pic
);
166 free( p_sys
->pp_images
[i
]->psz_filename
);
168 free( p_sys
->pp_images
[i
] );
170 TAB_CLEAN( p_sys
->i_images
, p_sys
->pp_images
);
176 /*****************************************************************************
177 * ParseText: parse an text subtitle packet and send it to the video output
178 *****************************************************************************/
179 static subpicture_t
*ParseText( decoder_t
*p_dec
, block_t
*p_block
)
181 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
182 subpicture_t
*p_spu
= NULL
;
183 char *psz_subtitle
= NULL
;
185 /* We cannot display a subpicture with no date */
186 if( p_block
->i_pts
<= VLC_TS_INVALID
)
188 msg_Warn( p_dec
, "subtitle without a date" );
192 /* Check validity of packet data */
193 /* An "empty" line containing only \0 can be used to force
194 and ephemer picture from the screen */
195 if( p_block
->i_buffer
< 1 )
197 msg_Warn( p_dec
, "no subtitle data" );
201 /* Should be resiliant against bad subtitles */
202 psz_subtitle
= strndup( (const char *)p_block
->p_buffer
,
204 if( psz_subtitle
== NULL
)
207 /* USF Subtitles are mandated to be UTF-8 -- make sure it is */
208 if (EnsureUTF8( psz_subtitle
) == NULL
)
210 msg_Err( p_dec
, "USF subtitles must be in UTF-8 format.\n"
211 "This stream contains USF subtitles which aren't." );
214 /* Create the subpicture unit */
215 p_spu
= decoder_NewSubpicture( p_dec
, NULL
);
218 msg_Warn( p_dec
, "can't get spu buffer" );
219 free( psz_subtitle
);
223 /* Decode USF strings */
224 p_spu
->p_region
= ParseUSFString( p_dec
, psz_subtitle
);
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
= 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 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_ssa_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_ssa_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
,
380 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
381 subpicture_region_t
*p_text_region
;
384 /* Create a new subpicture region */
385 memset( &fmt
, 0, sizeof(video_format_t
) );
386 fmt
.i_chroma
= VLC_CODEC_TEXT
;
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_ssa_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_ssa_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_ssa_style
= p_sys
->pp_ssa_styles
[i
];
417 msg_Dbg( p_dec
, "style is: %s", p_ssa_style
->psz_stylename
);
419 p_text_region
->p_style
= text_style_Duplicate( &p_ssa_style
->font_style
);
420 p_text_region
->i_align
= p_ssa_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_ssa_style->i_margin_percent_h;
427 * p_ssa_style->i_margin_percent_v;
429 p_text_region
->i_x
= p_ssa_style
->i_margin_h
;
430 p_text_region
->i_y
= p_ssa_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
= image_Mime2Fourcc( p_attach
->psz_mime
);
466 ( p_attach
->i_data
> 0 ) &&
467 ( p_attach
->p_data
!= NULL
) )
469 picture_t
*p_pic
= NULL
;
470 image_handler_t
*p_image
;
472 p_image
= image_HandlerCreate( p_dec
);
473 if( p_image
!= NULL
)
477 p_block
= block_New( p_image
->p_parent
, p_attach
->i_data
);
479 if( p_block
!= NULL
)
481 video_format_t fmt_in
;
482 video_format_t fmt_out
;
484 memcpy( p_block
->p_buffer
, p_attach
->p_data
, p_attach
->i_data
);
486 memset( &fmt_in
, 0, sizeof( video_format_t
));
487 memset( &fmt_out
, 0, sizeof( video_format_t
));
489 fmt_in
.i_chroma
= type
;
490 fmt_out
.i_chroma
= VLC_CODEC_YUVA
;
492 /* Find a suitable decoder module */
493 if( module_exists( "sdl_image" ) )
495 /* ffmpeg thinks it can handle bmp properly but it can't (at least
496 * not all of them), so use sdl_image if it is available */
500 var_Create( p_dec
, "codec", VLC_VAR_MODULE
| VLC_VAR_DOINHERIT
);
501 val
.psz_string
= (char*) "sdl_image";
502 var_Set( p_dec
, "codec", val
);
505 p_pic
= image_Read( p_image
, p_block
, &fmt_in
, &fmt_out
);
506 var_Destroy( p_dec
, "codec" );
509 image_HandlerDelete( p_image
);
513 image_attach_t
*p_picture
= malloc( sizeof(image_attach_t
) );
517 p_picture
->psz_filename
= strdup( p_attach
->psz_name
);
518 p_picture
->p_pic
= p_pic
;
520 TAB_APPEND( p_sys
->i_images
, p_sys
->pp_images
, p_picture
);
524 vlc_input_attachment_Delete( pp_attachments
[ k
] );
526 free( pp_attachments
);
531 static void ParseUSFHeaderTags( decoder_t
*p_dec
, xml_reader_t
*p_xml_reader
)
533 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
535 ssa_style_t
*p_ssa_style
= NULL
;
536 int i_style_level
= 0;
537 int i_metadata_level
= 0;
539 while ( xml_ReaderRead( p_xml_reader
) == 1 )
541 switch ( xml_ReaderNodeType( p_xml_reader
) )
543 case XML_READER_TEXT
:
544 case XML_READER_NONE
:
546 case XML_READER_ENDELEM
:
547 psz_node
= xml_ReaderName( p_xml_reader
);
551 switch (i_style_level
)
554 if( !strcasecmp( "metadata", psz_node
) && (i_metadata_level
== 1) )
560 if( !strcasecmp( "styles", psz_node
) )
566 if( !strcasecmp( "style", psz_node
) )
568 TAB_APPEND( p_sys
->i_ssa_styles
, p_sys
->pp_ssa_styles
, p_ssa_style
);
578 case XML_READER_STARTELEM
:
579 psz_node
= xml_ReaderName( p_xml_reader
);
584 if( !strcasecmp( "metadata", psz_node
) && (i_style_level
== 0) )
588 else if( !strcasecmp( "resolution", psz_node
) &&
589 ( i_metadata_level
== 1) )
591 while ( xml_ReaderNextAttr( p_xml_reader
) == VLC_SUCCESS
)
593 char *psz_name
= xml_ReaderName ( p_xml_reader
);
594 char *psz_value
= xml_ReaderValue ( p_xml_reader
);
596 if( psz_name
&& psz_value
)
598 if( !strcasecmp( "x", psz_name
) )
599 p_sys
->i_original_width
= atoi( psz_value
);
600 else if( !strcasecmp( "y", psz_name
) )
601 p_sys
->i_original_height
= atoi( psz_value
);
607 else if( !strcasecmp( "styles", psz_node
) && (i_style_level
== 0) )
611 else if( !strcasecmp( "style", psz_node
) && (i_style_level
== 1) )
615 p_ssa_style
= calloc( 1, sizeof(ssa_style_t
) );
621 /* All styles are supposed to default to Default, and then
622 * one or more settings are over-ridden.
623 * At the moment this only effects styles defined AFTER
627 for( i
= 0; i
< p_sys
->i_ssa_styles
; i
++ )
629 if( !strcasecmp( p_sys
->pp_ssa_styles
[i
]->psz_stylename
, "Default" ) )
631 ssa_style_t
*p_default_style
= p_sys
->pp_ssa_styles
[i
];
633 memcpy( p_ssa_style
, p_default_style
, sizeof( ssa_style_t
) );
634 //FIXME: Make font_style a pointer. Actually we double copy some data here,
635 // we use text_style_Copy to avoid copying psz_fontname, though .
636 text_style_Copy( &p_ssa_style
->font_style
, &p_default_style
->font_style
);
637 p_ssa_style
->psz_stylename
= NULL
;
641 while ( xml_ReaderNextAttr( p_xml_reader
) == VLC_SUCCESS
)
643 char *psz_name
= xml_ReaderName ( p_xml_reader
);
644 char *psz_value
= xml_ReaderValue ( p_xml_reader
);
646 if( psz_name
&& psz_value
)
648 if( !strcasecmp( "name", psz_name
) )
649 p_ssa_style
->psz_stylename
= strdup( psz_value
);
655 else if( !strcasecmp( "fontstyle", psz_node
) && (i_style_level
== 2) )
657 while ( xml_ReaderNextAttr( p_xml_reader
) == VLC_SUCCESS
)
659 char *psz_name
= xml_ReaderName ( p_xml_reader
);
660 char *psz_value
= xml_ReaderValue ( p_xml_reader
);
662 if( psz_name
&& psz_value
)
664 if( !strcasecmp( "face", psz_name
) )
666 free( p_ssa_style
->font_style
.psz_fontname
);
667 p_ssa_style
->font_style
.psz_fontname
= strdup( psz_value
);
669 else if( !strcasecmp( "size", psz_name
) )
671 if( ( *psz_value
== '+' ) || ( *psz_value
== '-' ) )
673 int i_value
= atoi( psz_value
);
675 if( ( i_value
>= -5 ) && ( i_value
<= 5 ) )
676 p_ssa_style
->font_style
.i_font_size
+=
677 ( i_value
* p_ssa_style
->font_style
.i_font_size
) / 10;
678 else if( i_value
< -5 )
679 p_ssa_style
->font_style
.i_font_size
= - i_value
;
680 else if( i_value
> 5 )
681 p_ssa_style
->font_style
.i_font_size
= i_value
;
684 p_ssa_style
->font_style
.i_font_size
= atoi( psz_value
);
686 else if( !strcasecmp( "italic", psz_name
) )
688 if( !strcasecmp( "yes", psz_value
))
689 p_ssa_style
->font_style
.i_style_flags
|= STYLE_ITALIC
;
691 p_ssa_style
->font_style
.i_style_flags
&= ~STYLE_ITALIC
;
693 else if( !strcasecmp( "weight", psz_name
) )
695 if( !strcasecmp( "bold", psz_value
))
696 p_ssa_style
->font_style
.i_style_flags
|= STYLE_BOLD
;
698 p_ssa_style
->font_style
.i_style_flags
&= ~STYLE_BOLD
;
700 else if( !strcasecmp( "underline", psz_name
) )
702 if( !strcasecmp( "yes", psz_value
))
703 p_ssa_style
->font_style
.i_style_flags
|= STYLE_UNDERLINE
;
705 p_ssa_style
->font_style
.i_style_flags
&= ~STYLE_UNDERLINE
;
707 else if( !strcasecmp( "color", psz_name
) )
709 if( *psz_value
== '#' )
711 unsigned long col
= strtol(psz_value
+1, NULL
, 16);
712 p_ssa_style
->font_style
.i_font_color
= (col
& 0x00ffffff);
713 p_ssa_style
->font_style
.i_font_alpha
= (col
>> 24) & 0xff;
716 else if( !strcasecmp( "outline-color", psz_name
) )
718 if( *psz_value
== '#' )
720 unsigned long col
= strtol(psz_value
+1, NULL
, 16);
721 p_ssa_style
->font_style
.i_outline_color
= (col
& 0x00ffffff);
722 p_ssa_style
->font_style
.i_outline_alpha
= (col
>> 24) & 0xff;
725 else if( !strcasecmp( "outline-level", psz_name
) )
727 p_ssa_style
->font_style
.i_outline_width
= atoi( psz_value
);
729 else if( !strcasecmp( "shadow-color", psz_name
) )
731 if( *psz_value
== '#' )
733 unsigned long col
= strtol(psz_value
+1, NULL
, 16);
734 p_ssa_style
->font_style
.i_shadow_color
= (col
& 0x00ffffff);
735 p_ssa_style
->font_style
.i_shadow_alpha
= (col
>> 24) & 0xff;
738 else if( !strcasecmp( "shadow-level", psz_name
) )
740 p_ssa_style
->font_style
.i_shadow_width
= atoi( psz_value
);
742 else if( !strcasecmp( "back-color", psz_name
) )
744 if( *psz_value
== '#' )
746 unsigned long col
= strtol(psz_value
+1, NULL
, 16);
747 p_ssa_style
->font_style
.i_karaoke_background_color
= (col
& 0x00ffffff);
748 p_ssa_style
->font_style
.i_karaoke_background_alpha
= (col
>> 24) & 0xff;
751 else if( !strcasecmp( "spacing", psz_name
) )
753 p_ssa_style
->font_style
.i_spacing
= atoi( psz_value
);
760 else if( !strcasecmp( "position", psz_node
) && (i_style_level
== 2) )
762 while ( xml_ReaderNextAttr( p_xml_reader
) == VLC_SUCCESS
)
764 char *psz_name
= xml_ReaderName ( p_xml_reader
);
765 char *psz_value
= xml_ReaderValue ( p_xml_reader
);
767 if( psz_name
&& psz_value
)
769 if( !strcasecmp( "alignment", psz_name
) )
771 if( !strcasecmp( "TopLeft", psz_value
) )
772 p_ssa_style
->i_align
= SUBPICTURE_ALIGN_TOP
| SUBPICTURE_ALIGN_LEFT
;
773 else if( !strcasecmp( "TopCenter", psz_value
) )
774 p_ssa_style
->i_align
= SUBPICTURE_ALIGN_TOP
;
775 else if( !strcasecmp( "TopRight", psz_value
) )
776 p_ssa_style
->i_align
= SUBPICTURE_ALIGN_TOP
| SUBPICTURE_ALIGN_RIGHT
;
777 else if( !strcasecmp( "MiddleLeft", psz_value
) )
778 p_ssa_style
->i_align
= SUBPICTURE_ALIGN_LEFT
;
779 else if( !strcasecmp( "MiddleCenter", psz_value
) )
780 p_ssa_style
->i_align
= 0;
781 else if( !strcasecmp( "MiddleRight", psz_value
) )
782 p_ssa_style
->i_align
= SUBPICTURE_ALIGN_RIGHT
;
783 else if( !strcasecmp( "BottomLeft", psz_value
) )
784 p_ssa_style
->i_align
= SUBPICTURE_ALIGN_BOTTOM
| SUBPICTURE_ALIGN_LEFT
;
785 else if( !strcasecmp( "BottomCenter", psz_value
) )
786 p_ssa_style
->i_align
= SUBPICTURE_ALIGN_BOTTOM
;
787 else if( !strcasecmp( "BottomRight", psz_value
) )
788 p_ssa_style
->i_align
= SUBPICTURE_ALIGN_BOTTOM
| SUBPICTURE_ALIGN_RIGHT
;
790 else if( !strcasecmp( "horizontal-margin", psz_name
) )
792 if( strchr( psz_value
, '%' ) )
794 p_ssa_style
->i_margin_h
= 0;
795 p_ssa_style
->i_margin_percent_h
= atoi( psz_value
);
799 p_ssa_style
->i_margin_h
= atoi( psz_value
);
800 p_ssa_style
->i_margin_percent_h
= 0;
803 else if( !strcasecmp( "vertical-margin", psz_name
) )
805 if( strchr( psz_value
, '%' ) )
807 p_ssa_style
->i_margin_v
= 0;
808 p_ssa_style
->i_margin_percent_v
= atoi( psz_value
);
812 p_ssa_style
->i_margin_v
= atoi( psz_value
);
813 p_ssa_style
->i_margin_percent_v
= 0;
831 static subpicture_region_t
*ParseUSFString( decoder_t
*p_dec
,
834 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
835 subpicture_region_t
*p_region_first
= NULL
;
836 subpicture_region_t
*p_region_upto
= p_region_first
;
838 while( *psz_subtitle
)
840 if( *psz_subtitle
== '<' )
842 char *psz_end
= NULL
;
844 if(( !strncasecmp( psz_subtitle
, "<text ", 6 )) ||
845 ( !strncasecmp( psz_subtitle
, "<text>", 6 )))
847 psz_end
= strcasestr( psz_subtitle
, "</text>" );
851 subpicture_region_t
*p_text_region
;
853 psz_end
+= strcspn( psz_end
, ">" ) + 1;
855 p_text_region
= CreateTextRegion( p_dec
,
857 psz_end
- psz_subtitle
,
862 p_text_region
->psz_text
= CreatePlainText( p_text_region
->psz_html
);
864 if( ! var_CreateGetBool( p_dec
, "subsdec-formatted" ) )
866 free( p_text_region
->psz_html
);
867 p_text_region
->psz_html
= NULL
;
871 if( !p_region_first
)
873 p_region_first
= p_region_upto
= p_text_region
;
875 else if( p_text_region
)
877 p_region_upto
->p_next
= p_text_region
;
878 p_region_upto
= p_region_upto
->p_next
;
882 else if(( !strncasecmp( psz_subtitle
, "<karaoke ", 9 )) ||
883 ( !strncasecmp( psz_subtitle
, "<karaoke>", 9 )))
885 psz_end
= strcasestr( psz_subtitle
, "</karaoke>" );
889 subpicture_region_t
*p_text_region
;
891 psz_end
+= strcspn( psz_end
, ">" ) + 1;
893 p_text_region
= CreateTextRegion( p_dec
,
895 psz_end
- psz_subtitle
,
900 if( ! var_CreateGetBool( p_dec
, "subsdec-formatted" ) )
902 free( p_text_region
->psz_html
);
903 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
, "<image ", 7 )) ||
918 ( !strncasecmp( psz_subtitle
, "<image>", 7 )))
920 subpicture_region_t
*p_image_region
= NULL
;
922 char *psz_end
= strcasestr( psz_subtitle
, "</image>" );
923 char *psz_content
= strchr( psz_subtitle
, '>' );
924 int i_transparent
= -1;
926 /* If a colorkey parameter is specified, then we have to map
927 * that index in the picture through as transparent (it is
928 * required by the USF spec but is also recommended that if the
929 * creator really wants a transparent colour that they use a
930 * type like PNG that properly supports it; this goes doubly
931 * for VLC because the pictures are stored internally in YUV
932 * and the resulting colour-matching may not produce the
935 char *psz_tmp
= GrabAttributeValue( "colorkey", psz_subtitle
);
938 if( *psz_tmp
== '#' )
939 i_transparent
= strtol( psz_tmp
+ 1, NULL
, 16 ) & 0x00ffffff;
942 if( psz_content
&& ( psz_content
< psz_end
) )
944 char *psz_filename
= strndup( &psz_content
[1], psz_end
- &psz_content
[1] );
947 p_image_region
= LoadEmbeddedImage( p_dec
,
948 psz_filename
, i_transparent
);
949 free( psz_filename
);
953 if( psz_end
) psz_end
+= strcspn( psz_end
, ">" ) + 1;
957 SetupPositions( p_image_region
, psz_subtitle
);
959 p_image_region
->p_next
= NULL
;
960 p_image_region
->psz_text
= NULL
;
961 p_image_region
->psz_html
= NULL
;
964 if( !p_region_first
)
966 p_region_first
= p_region_upto
= p_image_region
;
968 else if( p_image_region
)
970 p_region_upto
->p_next
= p_image_region
;
971 p_region_upto
= p_region_upto
->p_next
;
975 psz_subtitle
= psz_end
- 1;
977 psz_subtitle
+= strcspn( psz_subtitle
, ">" );
983 return p_region_first
;
986 /*****************************************************************************
987 * ParseUSFHeader: Retrieve global formatting information etc
988 *****************************************************************************/
989 static void ParseUSFHeader( decoder_t
*p_dec
)
991 stream_t
*p_sub
= NULL
;
992 xml_reader_t
*p_xml_reader
= NULL
;
994 p_sub
= stream_MemoryNew( VLC_OBJECT(p_dec
),
995 p_dec
->fmt_in
.p_extra
,
996 p_dec
->fmt_in
.i_extra
,
1001 p_xml_reader
= xml_ReaderCreate( p_dec
, p_sub
);
1004 /* Look for Root Node */
1005 if( xml_ReaderRead( p_xml_reader
) == 1 )
1007 char *psz_node
= xml_ReaderName( p_xml_reader
);
1009 if( !strcasecmp( "usfsubtitles", psz_node
) )
1010 ParseUSFHeaderTags( p_dec
, p_xml_reader
);
1014 xml_ReaderDelete( p_xml_reader
);
1016 stream_Delete( p_sub
);
1019 /* Function now handles tags which has attribute values, and tries
1020 * to deal with &' commands too. It no longer modifies the string
1021 * in place, so that the original text can be reused
1023 static char *StripTags( char *psz_subtitle
)
1025 char *psz_text_start
;
1028 psz_text
= psz_text_start
= malloc( strlen( psz_subtitle
) + 1 );
1029 if( !psz_text_start
)
1032 while( *psz_subtitle
)
1034 /* Mask out any pre-existing LFs in the subtitle */
1035 if( *psz_subtitle
== '\n' )
1036 *psz_subtitle
= ' ';
1038 if( *psz_subtitle
== '<' )
1040 if( strncasecmp( psz_subtitle
, "<br/>", 5 ) == 0 )
1043 psz_subtitle
+= strcspn( psz_subtitle
, ">" );
1045 else if( *psz_subtitle
== '&' )
1047 if( !strncasecmp( psz_subtitle
, "<", 4 ))
1050 psz_subtitle
+= strcspn( psz_subtitle
, ";" );
1052 else if( !strncasecmp( psz_subtitle
, ">", 4 ))
1055 psz_subtitle
+= strcspn( psz_subtitle
, ";" );
1057 else if( !strncasecmp( psz_subtitle
, "&", 5 ))
1060 psz_subtitle
+= strcspn( psz_subtitle
, ";" );
1062 else if( !strncasecmp( psz_subtitle
, """, 6 ))
1065 psz_subtitle
+= strcspn( psz_subtitle
, ";" );
1069 /* Assume it is just a normal ampersand */
1075 *psz_text
++ = *psz_subtitle
;
1082 char *psz
= realloc( psz_text_start
, strlen( psz_text_start
) + 1 );
1083 if( psz
) psz_text_start
= psz
;
1085 return psz_text_start
;
1088 /* Turn a HTML subtitle, turn into a plain-text version,
1089 * complete with sensible whitespace compaction
1092 static char *CreatePlainText( char *psz_subtitle
)
1094 char *psz_text
= StripTags( psz_subtitle
);
1100 s
= strpbrk( psz_text
, "\t\r\n " );
1105 int i_whitespace
= strspn( s
, "\t\r\n " );
1107 /* Favour '\n' over other whitespaces - if one of these
1108 * occurs in the whitespace use a '\n' as our value,
1109 * otherwise just use a ' '
1111 for( k
= 0; k
< i_whitespace
; k
++ )
1112 if( s
[k
] == '\n' ) spc
= '\n';
1114 if( i_whitespace
> 1 )
1118 strlen( s
) - i_whitespace
+ 1 );
1122 s
= strpbrk( s
, "\t\r\n " );
1127 /****************************************************************************
1128 * download and resize image located at psz_url
1129 ***************************************************************************/
1130 static subpicture_region_t
*LoadEmbeddedImage( decoder_t
*p_dec
,
1131 const char *psz_filename
,
1132 int i_transparent_color
)
1134 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1135 subpicture_region_t
*p_region
;
1136 video_format_t fmt_out
;
1137 picture_t
*p_pic
= NULL
;
1139 for( int k
= 0; k
< p_sys
->i_images
; k
++ )
1141 if( p_sys
->pp_images
&&
1142 !strcmp( p_sys
->pp_images
[k
]->psz_filename
, psz_filename
) )
1144 p_pic
= p_sys
->pp_images
[k
]->p_pic
;
1151 msg_Err( p_dec
, "Unable to read image %s", psz_filename
);
1155 /* Display the feed's image */
1156 memset( &fmt_out
, 0, sizeof( video_format_t
));
1158 fmt_out
.i_chroma
= VLC_CODEC_YUVA
;
1159 fmt_out
.i_sar_num
= fmt_out
.i_sar_den
= 1;
1161 fmt_out
.i_visible_width
= p_pic
->format
.i_visible_width
;
1163 fmt_out
.i_visible_height
= p_pic
->format
.i_visible_height
;
1165 p_region
= subpicture_region_New( &fmt_out
);
1168 msg_Err( p_dec
, "cannot allocate SPU region" );
1171 assert( p_pic
->format
.i_chroma
== VLC_CODEC_YUVA
);
1172 /* FIXME the copy is probably not needed anymore */
1173 picture_CopyPixels( p_region
->p_picture
, p_pic
);
1175 /* This isn't the best way to do this - if you really want transparency, then
1176 * you're much better off using an image type that supports it like PNG. The
1177 * spec requires this support though.
1179 if( i_transparent_color
> 0 )
1181 int i_r
= ( i_transparent_color
>> 16 ) & 0xff;
1182 int i_g
= ( i_transparent_color
>> 8 ) & 0xff;
1183 int i_b
= ( i_transparent_color
) & 0xff;
1185 /* FIXME it cannot work as the yuv conversion code will probably NOT match
1187 int i_y
= ( ( ( 66 * i_r
+ 129 * i_g
+ 25 * i_b
+ 128 ) >> 8 ) + 16 );
1188 int i_u
= ( ( -38 * i_r
- 74 * i_g
+ 112 * i_b
+ 128 ) >> 8 ) + 128 ;
1189 int i_v
= ( ( 112 * i_r
- 94 * i_g
- 18 * i_b
+ 128 ) >> 8 ) + 128 ;
1191 assert( p_region
->fmt
.i_chroma
== VLC_CODEC_YUVA
);
1192 for( unsigned int y
= 0; y
< p_region
->fmt
.i_height
; y
++ )
1194 for( unsigned int x
= 0; x
< p_region
->fmt
.i_width
; x
++ )
1196 if( p_region
->p_picture
->Y_PIXELS
[y
*p_region
->p_picture
->Y_PITCH
+ x
] != i_y
||
1197 p_region
->p_picture
->U_PIXELS
[y
*p_region
->p_picture
->U_PITCH
+ x
] != i_u
||
1198 p_region
->p_picture
->V_PIXELS
[y
*p_region
->p_picture
->V_PITCH
+ x
] != i_v
)
1200 p_region
->p_picture
->A_PIXELS
[y
*p_region
->p_picture
->A_PITCH
+ x
] = 0;