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