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