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