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