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