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