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