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