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