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