]> git.sesse.net Git - vlc/blob - modules/codec/subtitles/subsusf.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / codec / subtitles / subsusf.c
1 /*****************************************************************************
2  * subsusf.c : USF subtitles decoder
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Bernie Purcell <bitmap@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "subsdec.h"
28 #include <vlc_plugin.h>
29 #include <assert.h>
30
31 /*****************************************************************************
32  * Local prototypes
33  *****************************************************************************/
34 static int  OpenDecoder   ( vlc_object_t * );
35 static void CloseDecoder  ( vlc_object_t * );
36
37 static subpicture_t *DecodeBlock   ( decoder_t *, block_t ** );
38 static char         *CreatePlainText( char * );
39 static int           ParseImageAttachments( decoder_t *p_dec );
40
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 );
45
46 /*****************************************************************************
47  * Module descriptor.
48  *****************************************************************************/
49
50 vlc_module_begin ()
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 */
58 vlc_module_end ()
59
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
64  * to chose.
65  *****************************************************************************/
66 static int OpenDecoder( vlc_object_t *p_this )
67 {
68     decoder_t     *p_dec = (decoder_t*)p_this;
69     decoder_sys_t *p_sys;
70
71     if( p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') )
72         return VLC_EGENERIC;
73
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 )
76         return VLC_ENOMEM;
77
78     p_dec->pf_decode_sub = DecodeBlock;
79     p_dec->fmt_out.i_cat = SPU_ES;
80     p_dec->fmt_out.i_codec = 0;
81
82     /* Unused fields of p_sys - not needed for USF decoding */
83     p_sys->b_ass = false;
84     p_sys->iconv_handle = (vlc_iconv_t)-1;
85     p_sys->b_autodetect_utf8 = false;
86
87     /* init of p_sys */
88     p_sys->i_align = 0;
89     p_sys->i_original_height = 0;
90     p_sys->i_original_width = 0;
91     TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
92     TAB_INIT( p_sys->i_images, p_sys->pp_images );
93
94     /* USF subtitles are mandated to be UTF-8, so don't need vlc_iconv */
95
96     p_sys->i_align = var_CreateGetInteger( p_dec, "subsdec-align" );
97
98     ParseImageAttachments( p_dec );
99
100     if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
101     {
102         if( p_dec->fmt_in.i_extra > 0 )
103             ParseUSFHeader( p_dec );
104     }
105
106     return VLC_SUCCESS;
107 }
108
109 /****************************************************************************
110  * DecodeBlock: the whole thing
111  ****************************************************************************
112  * This function must be fed with complete subtitles units.
113  ****************************************************************************/
114 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
115 {
116     subpicture_t *p_spu;
117     block_t *p_block;
118
119     if( !pp_block || *pp_block == NULL )
120         return NULL;
121
122     p_block = *pp_block;
123
124     p_spu = ParseText( p_dec, p_block );
125
126     block_Release( p_block );
127     *pp_block = NULL;
128
129     return p_spu;
130 }
131
132 /*****************************************************************************
133  * CloseDecoder: clean up the decoder
134  *****************************************************************************/
135 static void CloseDecoder( vlc_object_t *p_this )
136 {
137     decoder_t *p_dec = (decoder_t *)p_this;
138     decoder_sys_t *p_sys = p_dec->p_sys;
139
140     if( p_sys->pp_ssa_styles )
141     {
142         int i;
143         for( i = 0; i < p_sys->i_ssa_styles; i++ )
144         {
145             if( !p_sys->pp_ssa_styles[i] )
146                 continue;
147
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] );
151         }
152         TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
153     }
154     if( p_sys->pp_images )
155     {
156         int i;
157         for( i = 0; i < p_sys->i_images; i++ )
158         {
159             if( !p_sys->pp_images[i] )
160                 continue;
161
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 );
165
166             free( p_sys->pp_images[i] );
167         }
168         TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
169     }
170
171     free( p_sys );
172 }
173
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 )
178 {
179     decoder_sys_t *p_sys = p_dec->p_sys;
180     subpicture_t *p_spu = NULL;
181     char *psz_subtitle = NULL;
182
183     /* We cannot display a subpicture with no date */
184     if( p_block->i_pts == 0 )
185     {
186         msg_Warn( p_dec, "subtitle without a date" );
187         return NULL;
188     }
189
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 )
194     {
195         msg_Warn( p_dec, "no subtitle data" );
196         return NULL;
197     }
198
199     /* Should be resiliant against bad subtitles */
200     psz_subtitle = strndup( (const char *)p_block->p_buffer,
201                             p_block->i_buffer );
202     if( psz_subtitle == NULL )
203         return NULL;
204
205     /* USF Subtitles are mandated to be UTF-8 -- make sure it is */
206     if (EnsureUTF8( psz_subtitle ) == NULL)
207     {
208         msg_Err( p_dec, "USF subtitles must be in UTF-8 format.\n"
209                  "This stream contains USF subtitles which aren't." );
210     }
211
212     /* Create the subpicture unit */
213     p_spu = decoder_NewSubpicture( p_dec );
214     if( !p_spu )
215     {
216         msg_Warn( p_dec, "can't get spu buffer" );
217         free( psz_subtitle );
218         return NULL;
219     }
220
221     /* Decode USF strings */
222     p_spu->p_region = ParseUSFString( p_dec, psz_subtitle, p_spu );
223
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;
230
231     free( psz_subtitle );
232
233     return p_spu;
234 }
235
236 static char *GrabAttributeValue( const char *psz_attribute,
237                                  const char *psz_tag_start )
238 {
239     if( psz_attribute && psz_tag_start )
240     {
241         char *psz_tag_end = strchr( psz_tag_start, '>' );
242         char *psz_found   = strcasestr( psz_tag_start, psz_attribute );
243
244         if( psz_found )
245         {
246             psz_found += strlen( psz_attribute );
247
248             if(( *(psz_found++) == '=' ) &&
249                ( *(psz_found++) == '\"' ))
250             {
251                 if( psz_found < psz_tag_end )
252                 {
253                     int   i_len = strcspn( psz_found, "\"" );
254                     return strndup( psz_found, i_len );
255                 }
256             }
257         }
258     }
259     return NULL;
260 }
261
262 static ssa_style_t *ParseStyle( decoder_sys_t *p_sys, char *psz_subtitle )
263 {
264     ssa_style_t *p_style   = NULL;
265     char        *psz_style = GrabAttributeValue( "style", psz_subtitle );
266
267     if( psz_style )
268     {
269         int i;
270
271         for( i = 0; i < p_sys->i_ssa_styles; i++ )
272         {
273             if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
274                 p_style = p_sys->pp_ssa_styles[i];
275         }
276         free( psz_style );
277     }
278     return p_style;
279 }
280
281 static int ParsePositionAttributeList( char *psz_subtitle, int *i_align,
282                                        int *i_x, int *i_y )
283 {
284     int   i_mask = 0;
285
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 );
289     /* -- UNSUPPORTED
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 );
294     */
295
296     *i_align = SUBPICTURE_ALIGN_BOTTOM;
297     *i_x = 0;
298     *i_y = 0;
299
300     if( psz_align )
301     {
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 ) )
311             *i_align = 0;
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;
320
321         i_mask |= ATTRIBUTE_ALIGNMENT;
322         free( psz_align );
323     }
324     if( psz_margin_x )
325     {
326         *i_x = atoi( psz_margin_x );
327         if( strchr( psz_margin_x, '%' ) )
328             i_mask |= ATTRIBUTE_X_PERCENT;
329         else
330             i_mask |= ATTRIBUTE_X;
331
332         free( psz_margin_x );
333     }
334     if( psz_margin_y )
335     {
336         *i_y = atoi( psz_margin_y );
337         if( strchr( psz_margin_y, '%' ) )
338             i_mask |= ATTRIBUTE_Y_PERCENT;
339         else
340             i_mask |= ATTRIBUTE_Y;
341
342         free( psz_margin_y );
343     }
344     return i_mask;
345 }
346
347 static void SetupPositions( subpicture_region_t *p_region, char *psz_subtitle )
348 {
349     int           i_mask = 0;
350     int           i_align;
351     int           i_x, i_y;
352
353     i_mask = ParsePositionAttributeList( psz_subtitle, &i_align, &i_x, &i_y );
354
355     if( i_mask & ATTRIBUTE_ALIGNMENT )
356         p_region->i_align = i_align;
357
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.
361      */
362     if( i_mask & ATTRIBUTE_X )
363         p_region->i_x = i_x;
364     else if( i_mask & ATTRIBUTE_X_PERCENT )
365         p_region->i_x = 0;
366
367     if( i_mask & ATTRIBUTE_Y )
368         p_region->i_y = i_y;
369     else if( i_mask & ATTRIBUTE_Y_PERCENT )
370         p_region->i_y = 0;
371 }
372
373 static subpicture_region_t *CreateTextRegion( decoder_t *p_dec,
374                                               subpicture_t *p_spu,
375                                               char *psz_subtitle,
376                                               int i_len,
377                                               int i_sys_align )
378 {
379     decoder_sys_t        *p_sys = p_dec->p_sys;
380     subpicture_region_t  *p_text_region;
381     video_format_t        fmt;
382
383     /* Create a new subpicture region */
384     memset( &fmt, 0, sizeof(video_format_t) );
385     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
386     fmt.i_aspect = 0;
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 );
390
391     if( p_text_region != NULL )
392     {
393         ssa_style_t  *p_style = NULL;
394
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 )
398         {
399             subpicture_region_Delete( p_text_region );
400             return NULL;
401         }
402
403         p_style = ParseStyle( p_sys, p_text_region->psz_html );
404         if( !p_style )
405         {
406             int i;
407
408             for( i = 0; i < p_sys->i_ssa_styles; i++ )
409             {
410                 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
411                     p_style = p_sys->pp_ssa_styles[i];
412             }
413         }
414
415         if( p_style )
416         {
417             msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename );
418
419             p_text_region->p_style = &p_style->font_style;
420             p_text_region->i_align = p_style->i_align;
421
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.
425
426                      * p_style->i_margin_percent_h;
427                      * p_style->i_margin_percent_v;
428              */
429             p_text_region->i_x         = p_style->i_margin_h;
430             p_text_region->i_y         = p_style->i_margin_v;
431
432         }
433         else
434         {
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;
438         }
439         /* Look for position arguments which may override the style-based
440          * defaults.
441          */
442         SetupPositions( p_text_region, psz_subtitle );
443
444         p_text_region->p_next = NULL;
445     }
446     return p_text_region;
447 }
448
449 static int ParseImageAttachments( decoder_t *p_dec )
450 {
451     decoder_sys_t        *p_sys = p_dec->p_sys;
452     input_attachment_t  **pp_attachments;
453     int                   i_attachments_cnt;
454     int                   k = 0;
455
456     if( VLC_SUCCESS != decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments_cnt ))
457         return VLC_EGENERIC;
458
459     for( k = 0; k < i_attachments_cnt; k++ )
460     {
461         input_attachment_t *p_attach = pp_attachments[k];
462
463         vlc_fourcc_t type = image_Mime2Fourcc( p_attach->psz_mime );
464
465         if( ( type != 0 ) &&
466             ( p_attach->i_data > 0 ) &&
467             ( p_attach->p_data != NULL ) )
468         {
469             picture_t         *p_pic = NULL;
470             image_handler_t   *p_image;
471
472             p_image = image_HandlerCreate( p_dec );
473             if( p_image != NULL )
474             {
475                 block_t   *p_block;
476
477                 p_block = block_New( p_image->p_parent, p_attach->i_data );
478
479                 if( p_block != NULL )
480                 {
481                     video_format_t     fmt_in;
482                     video_format_t     fmt_out;
483
484                     memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data );
485
486                     memset( &fmt_in,  0, sizeof( video_format_t));
487                     memset( &fmt_out, 0, sizeof( video_format_t));
488
489                     fmt_in.i_chroma  = type;
490                     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
491
492                     /* Find a suitable decoder module */
493                     if( module_exists( "sdl_image" ) )
494                     {
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 */
497
498                         vlc_value_t val;
499
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 );
503                     }
504
505                     p_pic = image_Read( p_image, p_block, &fmt_in, &fmt_out );
506                     var_Destroy( p_dec, "codec" );
507                 }
508
509                 image_HandlerDelete( p_image );
510             }
511             if( p_pic )
512             {
513                 image_attach_t *p_picture = malloc( sizeof(image_attach_t) );
514
515                 if( p_picture )
516                 {
517                     p_picture->psz_filename = strdup( p_attach->psz_name );
518                     p_picture->p_pic = p_pic;
519
520                     TAB_APPEND( p_sys->i_images, p_sys->pp_images, p_picture );
521                 }
522             }
523         }
524         vlc_input_attachment_Delete( pp_attachments[ k ] );
525     }
526     free( pp_attachments );
527
528     return VLC_SUCCESS;
529 }
530
531 static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
532 {
533     decoder_sys_t *p_sys = p_dec->p_sys;
534     char *psz_node;
535     ssa_style_t *p_style = NULL;
536     int i_style_level = 0;
537     int i_metadata_level = 0;
538
539     while ( xml_ReaderRead( p_xml_reader ) == 1 )
540     {
541         switch ( xml_ReaderNodeType( p_xml_reader ) )
542         {
543             case XML_READER_TEXT:
544             case XML_READER_NONE:
545                 break;
546             case XML_READER_ENDELEM:
547                 psz_node = xml_ReaderName( p_xml_reader );
548
549                 if( !psz_node )
550                     break;
551                 switch (i_style_level)
552                 {
553                     case 0:
554                         if( !strcasecmp( "metadata", psz_node ) && (i_metadata_level == 1) )
555                         {
556                             i_metadata_level--;
557                         }
558                         break;
559                     case 1:
560                         if( !strcasecmp( "styles", psz_node ) )
561                         {
562                             i_style_level--;
563                         }
564                         break;
565                     case 2:
566                         if( !strcasecmp( "style", psz_node ) )
567                         {
568                             TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_style );
569
570                             p_style = NULL;
571                             i_style_level--;
572                         }
573                         break;
574                 }
575
576                 free( psz_node );
577                 break;
578             case XML_READER_STARTELEM:
579                 psz_node = xml_ReaderName( p_xml_reader );
580
581                 if( !psz_node )
582                     break;
583
584                 if( !strcasecmp( "metadata", psz_node ) && (i_style_level == 0) )
585                 {
586                     i_metadata_level++;
587                 }
588                 else if( !strcasecmp( "resolution", psz_node ) &&
589                          ( i_metadata_level == 1) )
590                 {
591                     while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
592                     {
593                         char *psz_name = xml_ReaderName ( p_xml_reader );
594                         char *psz_value = xml_ReaderValue ( p_xml_reader );
595
596                         if( psz_name && psz_value )
597                         {
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 );
602                         }
603                         free( psz_name );
604                         free( psz_value );
605                     }
606                 }
607                 else if( !strcasecmp( "styles", psz_node ) && (i_style_level == 0) )
608                 {
609                     i_style_level++;
610                 }
611                 else if( !strcasecmp( "style", psz_node ) && (i_style_level == 1) )
612                 {
613                     i_style_level++;
614
615                     p_style = calloc( 1, sizeof(ssa_style_t) );
616                     if( ! p_style )
617                     {
618                         free( psz_node );
619                         return;
620                     }
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
624                      * Default in the XML
625                      */
626                     int i;
627                     for( i = 0; i < p_sys->i_ssa_styles; i++ )
628                     {
629                         if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
630                         {
631                             ssa_style_t *p_default_style = p_sys->pp_ssa_styles[i];
632
633                             memcpy( p_style, p_default_style, sizeof( ssa_style_t ) );
634                             p_style->font_style.psz_fontname = strdup( p_style->font_style.psz_fontname );
635                             p_style->psz_stylename = NULL;
636                         }
637                     }
638
639                     while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
640                     {
641                         char *psz_name = xml_ReaderName ( p_xml_reader );
642                         char *psz_value = xml_ReaderValue ( p_xml_reader );
643
644                         if( psz_name && psz_value )
645                         {
646                             if( !strcasecmp( "name", psz_name ) )
647                                 p_style->psz_stylename = strdup( psz_value);
648                         }
649                         free( psz_name );
650                         free( psz_value );
651                     }
652                 }
653                 else if( !strcasecmp( "fontstyle", psz_node ) && (i_style_level == 2) )
654                 {
655                     while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
656                     {
657                         char *psz_name = xml_ReaderName ( p_xml_reader );
658                         char *psz_value = xml_ReaderValue ( p_xml_reader );
659
660                         if( psz_name && psz_value )
661                         {
662                             if( !strcasecmp( "face", psz_name ) )
663                             {
664                                 free( p_style->font_style.psz_fontname );
665                                 p_style->font_style.psz_fontname = strdup( psz_value );
666                             }
667                             else if( !strcasecmp( "size", psz_name ) )
668                             {
669                                 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
670                                 {
671                                     int i_value = atoi( psz_value );
672
673                                     if( ( i_value >= -5 ) && ( i_value <= 5 ) )
674                                         p_style->font_style.i_font_size  +=
675                                             ( i_value * p_style->font_style.i_font_size ) / 10;
676                                     else if( i_value < -5 )
677                                         p_style->font_style.i_font_size  = - i_value;
678                                     else if( i_value > 5 )
679                                         p_style->font_style.i_font_size  = i_value;
680                                 }
681                                 else
682                                     p_style->font_style.i_font_size  = atoi( psz_value );
683                             }
684                             else if( !strcasecmp( "italic", psz_name ) )
685                             {
686                                 if( !strcasecmp( "yes", psz_value ))
687                                     p_style->font_style.i_style_flags |= STYLE_ITALIC;
688                                 else
689                                     p_style->font_style.i_style_flags &= ~STYLE_ITALIC;
690                             }
691                             else if( !strcasecmp( "weight", psz_name ) )
692                             {
693                                 if( !strcasecmp( "bold", psz_value ))
694                                     p_style->font_style.i_style_flags |= STYLE_BOLD;
695                                 else
696                                     p_style->font_style.i_style_flags &= ~STYLE_BOLD;
697                             }
698                             else if( !strcasecmp( "underline", psz_name ) )
699                             {
700                                 if( !strcasecmp( "yes", psz_value ))
701                                     p_style->font_style.i_style_flags |= STYLE_UNDERLINE;
702                                 else
703                                     p_style->font_style.i_style_flags &= ~STYLE_UNDERLINE;
704                             }
705                             else if( !strcasecmp( "color", psz_name ) )
706                             {
707                                 if( *psz_value == '#' )
708                                 {
709                                     unsigned long col = strtol(psz_value+1, NULL, 16);
710                                     p_style->font_style.i_font_color = (col & 0x00ffffff);
711                                     p_style->font_style.i_font_alpha = (col >> 24) & 0xff;
712                                 }
713                             }
714                             else if( !strcasecmp( "outline-color", psz_name ) )
715                             {
716                                 if( *psz_value == '#' )
717                                 {
718                                     unsigned long col = strtol(psz_value+1, NULL, 16);
719                                     p_style->font_style.i_outline_color = (col & 0x00ffffff);
720                                     p_style->font_style.i_outline_alpha = (col >> 24) & 0xff;
721                                 }
722                             }
723                             else if( !strcasecmp( "outline-level", psz_name ) )
724                             {
725                                 p_style->font_style.i_outline_width = atoi( psz_value );
726                             }
727                             else if( !strcasecmp( "shadow-color", psz_name ) )
728                             {
729                                 if( *psz_value == '#' )
730                                 {
731                                     unsigned long col = strtol(psz_value+1, NULL, 16);
732                                     p_style->font_style.i_shadow_color = (col & 0x00ffffff);
733                                     p_style->font_style.i_shadow_alpha = (col >> 24) & 0xff;
734                                 }
735                             }
736                             else if( !strcasecmp( "shadow-level", psz_name ) )
737                             {
738                                 p_style->font_style.i_shadow_width = atoi( psz_value );
739                             }
740                             else if( !strcasecmp( "back-color", psz_name ) )
741                             {
742                                 if( *psz_value == '#' )
743                                 {
744                                     unsigned long col = strtol(psz_value+1, NULL, 16);
745                                     p_style->font_style.i_karaoke_background_color = (col & 0x00ffffff);
746                                     p_style->font_style.i_karaoke_background_alpha = (col >> 24) & 0xff;
747                                 }
748                             }
749                             else if( !strcasecmp( "spacing", psz_name ) )
750                             {
751                                 p_style->font_style.i_spacing = atoi( psz_value );
752                             }
753                         }
754                         free( psz_name );
755                         free( psz_value );
756                     }
757                 }
758                 else if( !strcasecmp( "position", psz_node ) && (i_style_level == 2) )
759                 {
760                     while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
761                     {
762                         char *psz_name = xml_ReaderName ( p_xml_reader );
763                         char *psz_value = xml_ReaderValue ( p_xml_reader );
764
765                         if( psz_name && psz_value )
766                         {
767                             if( !strcasecmp( "alignment", psz_name ) )
768                             {
769                                 if( !strcasecmp( "TopLeft", psz_value ) )
770                                     p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
771                                 else if( !strcasecmp( "TopCenter", psz_value ) )
772                                     p_style->i_align = SUBPICTURE_ALIGN_TOP;
773                                 else if( !strcasecmp( "TopRight", psz_value ) )
774                                     p_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
775                                 else if( !strcasecmp( "MiddleLeft", psz_value ) )
776                                     p_style->i_align = SUBPICTURE_ALIGN_LEFT;
777                                 else if( !strcasecmp( "MiddleCenter", psz_value ) )
778                                     p_style->i_align = 0;
779                                 else if( !strcasecmp( "MiddleRight", psz_value ) )
780                                     p_style->i_align = SUBPICTURE_ALIGN_RIGHT;
781                                 else if( !strcasecmp( "BottomLeft", psz_value ) )
782                                     p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
783                                 else if( !strcasecmp( "BottomCenter", psz_value ) )
784                                     p_style->i_align = SUBPICTURE_ALIGN_BOTTOM;
785                                 else if( !strcasecmp( "BottomRight", psz_value ) )
786                                     p_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
787                             }
788                             else if( !strcasecmp( "horizontal-margin", psz_name ) )
789                             {
790                                 if( strchr( psz_value, '%' ) )
791                                 {
792                                     p_style->i_margin_h = 0;
793                                     p_style->i_margin_percent_h = atoi( psz_value );
794                                 }
795                                 else
796                                 {
797                                     p_style->i_margin_h = atoi( psz_value );
798                                     p_style->i_margin_percent_h = 0;
799                                 }
800                             }
801                             else if( !strcasecmp( "vertical-margin", psz_name ) )
802                             {
803                                 if( strchr( psz_value, '%' ) )
804                                 {
805                                     p_style->i_margin_v = 0;
806                                     p_style->i_margin_percent_v = atoi( psz_value );
807                                 }
808                                 else
809                                 {
810                                     p_style->i_margin_v = atoi( psz_value );
811                                     p_style->i_margin_percent_v = 0;
812                                 }
813                             }
814                         }
815                         free( psz_name );
816                         free( psz_value );
817                     }
818                 }
819
820                 free( psz_node );
821                 break;
822         }
823     }
824     free( p_style );
825 }
826
827
828
829 static subpicture_region_t *ParseUSFString( decoder_t *p_dec,
830                                             char *psz_subtitle,
831                                             subpicture_t *p_spu_in )
832 {
833     decoder_sys_t        *p_sys = p_dec->p_sys;
834     subpicture_t         *p_spu = p_spu_in;
835     subpicture_region_t  *p_region_first = NULL;
836     subpicture_region_t  *p_region_upto  = p_region_first;
837
838     while( *psz_subtitle )
839     {
840         if( *psz_subtitle == '<' )
841         {
842             char *psz_end = NULL;
843
844             if(( !strncasecmp( psz_subtitle, "<text ", 6 )) ||
845                ( !strncasecmp( psz_subtitle, "<text>", 6 )))
846             {
847                 psz_end = strcasestr( psz_subtitle, "</text>" );
848
849                 if( psz_end )
850                 {
851                     subpicture_region_t  *p_text_region;
852
853                     psz_end += strcspn( psz_end, ">" ) + 1;
854
855                     p_text_region = CreateTextRegion( p_dec,
856                                                       p_spu,
857                                                       psz_subtitle,
858                                                       psz_end - psz_subtitle,
859                                                       p_sys->i_align );
860
861                     if( p_text_region )
862                     {
863                         p_text_region->psz_text = CreatePlainText( p_text_region->psz_html );
864
865                         if( ! var_CreateGetBool( p_dec, "subsdec-formatted" ) )
866                         {
867                             free( p_text_region->psz_html );
868                             p_text_region->psz_html = NULL;
869                         }
870                     }
871
872                     if( !p_region_first )
873                     {
874                         p_region_first = p_region_upto = p_text_region;
875                     }
876                     else if( p_text_region )
877                     {
878                         p_region_upto->p_next = p_text_region;
879                         p_region_upto = p_region_upto->p_next;
880                     }
881                 }
882             }
883             else if(( !strncasecmp( psz_subtitle, "<karaoke ", 9 )) ||
884                     ( !strncasecmp( psz_subtitle, "<karaoke>", 9 )))
885             {
886                 psz_end = strcasestr( psz_subtitle, "</karaoke>" );
887
888                 if( psz_end )
889                 {
890                     subpicture_region_t  *p_text_region;
891
892                     psz_end += strcspn( psz_end, ">" ) + 1;
893
894                     p_text_region = CreateTextRegion( p_dec,
895                                                       p_spu,
896                                                       psz_subtitle,
897                                                       psz_end - psz_subtitle,
898                                                       p_sys->i_align );
899
900                     if( p_text_region )
901                     {
902                         if( ! var_CreateGetBool( p_dec, "subsdec-formatted" ) )
903                         {
904                             free( p_text_region->psz_html );
905                             p_text_region->psz_html = NULL;
906                         }
907                     }
908                     if( !p_region_first )
909                     {
910                         p_region_first = p_region_upto = p_text_region;
911                     }
912                     else if( p_text_region )
913                     {
914                         p_region_upto->p_next = p_text_region;
915                         p_region_upto = p_region_upto->p_next;
916                     }
917                 }
918             }
919             else if(( !strncasecmp( psz_subtitle, "<image ", 7 )) ||
920                     ( !strncasecmp( psz_subtitle, "<image>", 7 )))
921             {
922                 subpicture_region_t *p_image_region = NULL;
923
924                 char *psz_end = strcasestr( psz_subtitle, "</image>" );
925                 char *psz_content = strchr( psz_subtitle, '>' );
926                 int   i_transparent = -1;
927
928                 /* If a colorkey parameter is specified, then we have to map
929                  * that index in the picture through as transparent (it is
930                  * required by the USF spec but is also recommended that if the
931                  * creator really wants a transparent colour that they use a
932                  * type like PNG that properly supports it; this goes doubly
933                  * for VLC because the pictures are stored internally in YUV
934                  * and the resulting colour-matching may not produce the
935                  * desired results.)
936                  */
937                 char *psz_tmp = GrabAttributeValue( "colorkey", psz_subtitle );
938                 if( psz_tmp )
939                 {
940                     if( *psz_tmp == '#' )
941                         i_transparent = strtol( psz_tmp + 1, NULL, 16 ) & 0x00ffffff;
942                     free( psz_tmp );
943                 }
944                 if( psz_content && ( psz_content < psz_end ) )
945                 {
946                     char *psz_filename = strndup( &psz_content[1], psz_end - &psz_content[1] );
947                     if( psz_filename )
948                     {
949                         p_image_region = LoadEmbeddedImage( p_dec, p_spu,
950                                             psz_filename, i_transparent );
951                         free( psz_filename );
952                     }
953                 }
954
955                 if( psz_end ) psz_end += strcspn( psz_end, ">" ) + 1;
956
957                 if( p_image_region )
958                 {
959                     SetupPositions( p_image_region, psz_subtitle );
960
961                     p_image_region->p_next   = NULL;
962                     p_image_region->psz_text = NULL;
963                     p_image_region->psz_html = NULL;
964
965                 }
966                 if( !p_region_first )
967                 {
968                     p_region_first = p_region_upto = p_image_region;
969                 }
970                 else if( p_image_region )
971                 {
972                     p_region_upto->p_next = p_image_region;
973                     p_region_upto = p_region_upto->p_next;
974                 }
975             }
976             if( psz_end )
977                 psz_subtitle = psz_end - 1;
978
979             psz_subtitle += strcspn( psz_subtitle, ">" );
980         }
981
982         psz_subtitle++;
983     }
984
985     return p_region_first;
986 }
987
988 /*****************************************************************************
989  * ParseUSFHeader: Retrieve global formatting information etc
990  *****************************************************************************/
991 static void ParseUSFHeader( decoder_t *p_dec )
992 {
993     stream_t      *p_sub = NULL;
994     xml_t         *p_xml = NULL;
995     xml_reader_t  *p_xml_reader = NULL;
996
997     p_sub = stream_MemoryNew( VLC_OBJECT(p_dec),
998                               p_dec->fmt_in.p_extra,
999                               p_dec->fmt_in.i_extra,
1000                               true );
1001     if( !p_sub )
1002         return;
1003
1004     p_xml = xml_Create( p_dec );
1005     if( p_xml )
1006     {
1007         p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
1008         if( p_xml_reader )
1009         {
1010             /* Look for Root Node */
1011             if( xml_ReaderRead( p_xml_reader ) == 1 )
1012             {
1013                 char *psz_node = xml_ReaderName( p_xml_reader );
1014
1015                 if( !strcasecmp( "usfsubtitles", psz_node ) )
1016                     ParseUSFHeaderTags( p_dec, p_xml_reader );
1017
1018                 free( psz_node );
1019             }
1020
1021             xml_ReaderDelete( p_xml, p_xml_reader );
1022         }
1023         xml_Delete( p_xml );
1024     }
1025     stream_Delete( p_sub );
1026 }
1027
1028 /* Function now handles tags which has attribute values, and tries
1029  * to deal with &' commands too. It no longer modifies the string
1030  * in place, so that the original text can be reused
1031  */
1032 static char *StripTags( char *psz_subtitle )
1033 {
1034     char *psz_text_start;
1035     char *psz_text;
1036
1037     psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
1038     if( !psz_text_start )
1039         return NULL;
1040
1041     while( *psz_subtitle )
1042     {
1043         /* Mask out any pre-existing LFs in the subtitle */
1044         if( *psz_subtitle == '\n' )
1045             *psz_subtitle = ' ';
1046
1047         if( *psz_subtitle == '<' )
1048         {
1049             if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
1050                 *psz_text++ = '\n';
1051
1052             psz_subtitle += strcspn( psz_subtitle, ">" );
1053         }
1054         else if( *psz_subtitle == '&' )
1055         {
1056             if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
1057             {
1058                 *psz_text++ = '<';
1059                 psz_subtitle += strcspn( psz_subtitle, ";" );
1060             }
1061             else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
1062             {
1063                 *psz_text++ = '>';
1064                 psz_subtitle += strcspn( psz_subtitle, ";" );
1065             }
1066             else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
1067             {
1068                 *psz_text++ = '&';
1069                 psz_subtitle += strcspn( psz_subtitle, ";" );
1070             }
1071             else if( !strncasecmp( psz_subtitle, "&quot;", 6 ))
1072             {
1073                 *psz_text++ = '\"';
1074                 psz_subtitle += strcspn( psz_subtitle, ";" );
1075             }
1076             else
1077             {
1078                 /* Assume it is just a normal ampersand */
1079                 *psz_text++ = '&';
1080             }
1081         }
1082         else
1083         {
1084             *psz_text++ = *psz_subtitle;
1085         }
1086
1087         psz_subtitle++;
1088     }
1089     *psz_text = '\0';
1090     psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
1091
1092     return psz_text_start;
1093 }
1094
1095 /* Turn a HTML subtitle, turn into a plain-text version,
1096  *  complete with sensible whitespace compaction
1097  */
1098
1099 static char *CreatePlainText( char *psz_subtitle )
1100 {
1101     char *psz_text = StripTags( psz_subtitle );
1102     char *s;
1103
1104     if( !psz_text )
1105         return NULL;
1106
1107     s = strpbrk( psz_text, "\t\r\n " );
1108     while( s )
1109     {
1110         int   k;
1111         char  spc = ' ';
1112         int   i_whitespace = strspn( s, "\t\r\n " );
1113
1114         /* Favour '\n' over other whitespaces - if one of these
1115          * occurs in the whitespace use a '\n' as our value,
1116          * otherwise just use a ' '
1117          */
1118         for( k = 0; k < i_whitespace; k++ )
1119             if( s[k] == '\n' ) spc = '\n';
1120
1121         if( i_whitespace > 1 )
1122         {
1123             memmove( &s[1],
1124                      &s[i_whitespace],
1125                      strlen( s ) - i_whitespace + 1 );
1126         }
1127         *s++ = spc;
1128
1129         s = strpbrk( s, "\t\r\n " );
1130     }
1131     return psz_text;
1132 }
1133
1134 /****************************************************************************
1135  * download and resize image located at psz_url
1136  ***************************************************************************/
1137 static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec,
1138                                                subpicture_t *p_spu,
1139                                                const char *psz_filename,
1140                                                int i_transparent_color )
1141 {
1142     decoder_sys_t         *p_sys = p_dec->p_sys;
1143     subpicture_region_t   *p_region;
1144     video_format_t         fmt_out;
1145     int                    k;
1146     picture_t             *p_pic = NULL;
1147
1148     for( k = 0; k < p_sys->i_images; k++ )
1149     {
1150         if( p_sys->pp_images &&
1151             !strcmp( p_sys->pp_images[k]->psz_filename, psz_filename ) )
1152         {
1153             p_pic = p_sys->pp_images[k]->p_pic;
1154             break;
1155         }
1156     }
1157
1158     if( !p_pic )
1159     {
1160         msg_Err( p_dec, "Unable to read image %s", psz_filename );
1161         return NULL;
1162     }
1163
1164     /* Display the feed's image */
1165     memset( &fmt_out, 0, sizeof( video_format_t));
1166
1167     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
1168     fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
1169     fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
1170     fmt_out.i_width =
1171         fmt_out.i_visible_width = p_pic->format.i_visible_width;
1172     fmt_out.i_height =
1173         fmt_out.i_visible_height = p_pic->format.i_visible_height;
1174
1175     p_region = subpicture_region_New( &fmt_out );
1176     if( !p_region )
1177     {
1178         msg_Err( p_dec, "cannot allocate SPU region" );
1179         return NULL;
1180     }
1181     assert( p_pic->format.i_chroma == VLC_FOURCC('Y','U','V','A') );
1182     /* FIXME the copy is probably not needed anymore */
1183     picture_CopyPixels( p_region->p_picture, p_pic );
1184
1185     /* This isn't the best way to do this - if you really want transparency, then
1186      * you're much better off using an image type that supports it like PNG. The
1187      * spec requires this support though.
1188      */
1189     if( i_transparent_color > 0 )
1190     {
1191         int i_r = ( i_transparent_color >> 16 ) & 0xff;
1192         int i_g = ( i_transparent_color >>  8 ) & 0xff;
1193         int i_b = ( i_transparent_color       ) & 0xff;
1194
1195         /* FIXME it cannot work as the yuv conversion code will probably NOT match
1196          * this one  */
1197         int i_y = ( ( (  66 * i_r + 129 * i_g +  25 * i_b + 128 ) >> 8 ) + 16 );
1198         int i_u =   ( ( -38 * i_r -  74 * i_g + 112 * i_b + 128 ) >> 8 ) + 128 ;
1199         int i_v =   ( ( 112 * i_r -  94 * i_g -  18 * i_b + 128 ) >> 8 ) + 128 ;
1200
1201         assert( p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','A') );
1202         for( unsigned int y = 0; y < p_region->fmt.i_height; y++ )
1203         {
1204             for( unsigned int x = 0; x < p_region->fmt.i_width; x++ )
1205             {
1206                 if( p_region->p_picture->Y_PIXELS[y*p_region->p_picture->Y_PITCH + x] != i_y ||
1207                     p_region->p_picture->U_PIXELS[y*p_region->p_picture->U_PITCH + x] != i_u ||
1208                     p_region->p_picture->V_PIXELS[y*p_region->p_picture->V_PITCH + x] != i_v )
1209                     continue;
1210                 p_region->p_picture->A_PIXELS[y*p_region->p_picture->A_PITCH + x] = 0;
1211
1212             }
1213         }
1214     }
1215     return p_region;
1216 }