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