]> git.sesse.net Git - vlc/blob - modules/codec/subtitles/subsdec.c
Move more of the code for USF subtitles decoding out into separate
[vlc] / modules / codec / subtitles / subsdec.c
1 /*****************************************************************************
2  * subsdec.c : text subtitles decoder
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Derk-Jan Hartman <hartman at videolan dot org>
10  *          Bernie Purcell <bitmap@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #include "subsdec.h"
32
33 /*****************************************************************************
34  * Local prototypes
35  *****************************************************************************/
36 static int  OpenDecoder   ( vlc_object_t * );
37 static void CloseDecoder  ( vlc_object_t * );
38
39 static subpicture_t   *DecodeBlock   ( decoder_t *, block_t ** );
40 static subpicture_t   *ParseText     ( decoder_t *, block_t * );
41 static char           *StripTags      ( char * );
42 static char           *CreateHtmlSubtitle ( char * );
43
44
45 /*****************************************************************************
46  * Module descriptor.
47  *****************************************************************************/
48 static const char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
49     "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
50     "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
51     "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
52     "ISO-8859-6", "CP1256", "MacArabic", "",
53     "ISO-8859-7", "CP1253", "MacGreek", "",
54     "ISO-8859-8", "CP1255", "MacHebrew", "",
55     "ISO-8859-9", "CP1254", "MacTurkish", "",
56     "ISO-8859-13", "CP1257", "",
57     "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
58     "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
59     "ISO-2022-KR", "EUC-KR", "",
60     "MacThai", "KOI8-T", "",
61     "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
62     "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
63     "Macintosh", "",
64     "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
65     "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
66     "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
67     "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
68     "HPROMAN8", "NEXTSTEP" };
69 /*
70 SSA supports charset selection.
71 The following known charsets are used:
72
73 0 = Ansi - Western European
74 1 = default
75 2 = symbol
76 3 = invalid
77 77 = Mac
78 128 = Japanese (Shift JIS)
79 129 = Hangul
80 130 = Johab
81 134 = GB2312 Simplified Chinese
82 136 = Big5 Traditional Chinese
83 161 = Greek
84 162 = Turkish
85 163 = Vietnamese
86 177 = Hebrew
87 178 = Arabic
88 186 = Baltic
89 204 = Russian (Cyrillic)
90 222 = Thai
91 238 = Eastern European
92 254 = PC 437
93 */
94
95 static int  pi_justification[] = { 0, 1, 2 };
96 static const char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
97
98 #define ENCODING_TEXT N_("Subtitles text encoding")
99 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
100 #define ALIGN_TEXT N_("Subtitles justification")
101 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
102 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
103 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
104             "UTF-8 encoding within subtitles files.")
105 #define FORMAT_TEXT N_("Formatted Subtitles")
106 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
107  "VLC partly implements this, but you can choose to disable all formatting.")
108
109
110 vlc_module_begin();
111     set_shortname( _("Subtitles"));
112     set_description( _("Text subtitles decoder") );
113     set_capability( "decoder", 50 );
114     set_callbacks( OpenDecoder, CloseDecoder );
115     set_category( CAT_INPUT );
116     set_subcategory( SUBCAT_INPUT_SCODEC );
117
118     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
119                  VLC_FALSE );
120         change_integer_list( pi_justification, ppsz_justification_text, 0 );
121     add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
122                 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
123         change_string_list( ppsz_encodings, 0, 0 );
124     add_bool( "subsdec-autodetect-utf8", VLC_TRUE, NULL,
125               AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, VLC_FALSE );
126     add_bool( "subsdec-formatted", VLC_TRUE, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
127                  VLC_FALSE );
128 vlc_module_end();
129
130 /*****************************************************************************
131  * OpenDecoder: probe the decoder and return score
132  *****************************************************************************
133  * Tries to launch a decoder and return score so that the interface is able
134  * to chose.
135  *****************************************************************************/
136 static int OpenDecoder( vlc_object_t *p_this )
137 {
138     decoder_t     *p_dec = (decoder_t*)p_this;
139     decoder_sys_t *p_sys;
140     vlc_value_t    val;
141
142     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
143         p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
144     {
145         return VLC_EGENERIC;
146     }
147
148     p_dec->pf_decode_sub = DecodeBlock;
149
150     /* Allocate the memory needed to store the decoder's structure */
151     if( ( p_dec->p_sys = p_sys =
152           (decoder_sys_t *)calloc(1, sizeof(decoder_sys_t)) ) == NULL )
153     {
154         msg_Err( p_dec, "out of memory" );
155         return VLC_ENOMEM;
156     }
157
158     /* init of p_sys */
159     p_sys->i_align = 0;
160     p_sys->iconv_handle = (vlc_iconv_t)-1;
161     p_sys->b_autodetect_utf8 = VLC_FALSE;
162     p_sys->b_ass = VLC_FALSE;
163     p_sys->i_original_height = -1;
164     p_sys->i_original_width = -1;
165     TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
166     TAB_INIT( p_sys->i_images, p_sys->pp_images );
167
168     char *psz_charset = NULL;
169     /* First try demux-specified encoding */
170     if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
171     {
172         psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
173         msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
174                  p_dec->fmt_in.subs.psz_encoding ?: "not specified");
175     }
176
177     /* Second, try configured encoding */
178     if (psz_charset == NULL)
179     {
180         psz_charset = var_CreateGetNonEmptyString (p_dec, "subsdec-encoding");
181         if ((psz_charset != NULL) && !strcasecmp (psz_charset, DEFAULT_NAME))
182         {
183             free (psz_charset);
184             psz_charset = NULL;
185         }
186
187         msg_Dbg (p_dec, "trying configured character encoding: %s",
188                  psz_charset ?: "not specified");
189     }
190
191     /* Third, try "local" encoding with optional UTF-8 autodetection */
192     if (psz_charset == NULL)
193     {
194         psz_charset = strdup (GetFallbackEncoding ());
195         msg_Dbg (p_dec, "trying default character encoding: %s",
196                  psz_charset ?: "not specified");
197
198         if (var_CreateGetBool (p_dec, "subsdec-autodetect-utf8"))
199         {
200             msg_Dbg (p_dec, "using automatic UTF-8 detection");
201             p_sys->b_autodetect_utf8 = VLC_TRUE;
202         }
203     }
204
205     if (psz_charset == NULL)
206     {
207         psz_charset = strdup ("UTF-8");
208         msg_Dbg (p_dec, "trying hard-coded character encoding: %s",
209                  psz_charset ?: "error");
210     }
211
212     if (psz_charset == NULL)
213     {
214         free (p_sys);
215         return VLC_ENOMEM;
216     }
217
218     if (strcasecmp (psz_charset, "UTF-8") && strcasecmp (psz_charset, "utf8"))
219     {
220         p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
221         if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
222             msg_Err (p_dec, "cannot convert from %s: %s", psz_charset,
223                      strerror (errno));
224     }
225     free (psz_charset);
226
227     var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
228     var_Get( p_dec, "subsdec-align", &val );
229     p_sys->i_align = val.i_int;
230
231     if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
232     {
233         if( p_dec->fmt_in.i_extra > 0 )
234             ParseSSAHeader( p_dec );
235     }
236
237     return VLC_SUCCESS;
238 }
239
240 /****************************************************************************
241  * DecodeBlock: the whole thing
242  ****************************************************************************
243  * This function must be fed with complete subtitles units.
244  ****************************************************************************/
245 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
246 {
247     subpicture_t *p_spu = NULL;
248
249     if( !pp_block || *pp_block == NULL ) return NULL;
250
251     p_spu = ParseText( p_dec, *pp_block );
252
253     block_Release( *pp_block );
254     *pp_block = NULL;
255
256     return p_spu;
257 }
258
259 /*****************************************************************************
260  * CloseDecoder: clean up the decoder
261  *****************************************************************************/
262 static void CloseDecoder( vlc_object_t *p_this )
263 {
264     decoder_t *p_dec = (decoder_t *)p_this;
265     decoder_sys_t *p_sys = p_dec->p_sys;
266
267     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
268         vlc_iconv_close( p_sys->iconv_handle );
269
270     if( p_sys->pp_ssa_styles )
271     {
272         int i;
273         for( i = 0; i < p_sys->i_ssa_styles; i++ )
274         {
275             if( !p_sys->pp_ssa_styles[i] )
276                 continue;
277
278             if( p_sys->pp_ssa_styles[i]->psz_stylename )
279                 free( p_sys->pp_ssa_styles[i]->psz_stylename );
280             if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname )
281                 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
282             if( p_sys->pp_ssa_styles[i] )
283                 free( p_sys->pp_ssa_styles[i] );
284         }
285         TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
286     }
287     if( p_sys->pp_images )
288     {
289         int i;
290         for( i = 0; i < p_sys->i_images; i++ )
291         {
292             if( !p_sys->pp_images[i] )
293                 continue;
294
295             if( p_sys->pp_images[i]->p_pic )
296                 p_sys->pp_images[i]->p_pic->pf_release( p_sys->pp_images[i]->p_pic );
297             if( p_sys->pp_images[i]->psz_filename )
298                 free( p_sys->pp_images[i]->psz_filename );
299
300             free( p_sys->pp_images[i] );
301         }
302         TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
303     }
304
305     free( p_sys );
306 }
307
308 /*****************************************************************************
309  * ParseText: parse an text subtitle packet and send it to the video output
310  *****************************************************************************/
311 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
312 {
313     decoder_sys_t *p_sys = p_dec->p_sys;
314     subpicture_t *p_spu = NULL;
315     char *psz_subtitle = NULL;
316     video_format_t fmt;
317
318     /* We cannot display a subpicture with no date */
319     if( p_block->i_pts == 0 )
320     {
321         msg_Warn( p_dec, "subtitle without a date" );
322         return NULL;
323     }
324
325     /* Check validity of packet data */
326     /* An "empty" line containing only \0 can be used to force
327        and ephemer picture from the screen */
328     if( p_block->i_buffer < 1 )
329     {
330         msg_Warn( p_dec, "no subtitle data" );
331         return NULL;
332     }
333
334     /* Should be resiliant against bad subtitles */
335     psz_subtitle = strndup( (const char *)p_block->p_buffer,
336                             p_block->i_buffer );
337     if( psz_subtitle == NULL )
338         return NULL;
339
340     if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
341     {
342         if (EnsureUTF8( psz_subtitle ) == NULL)
343         {
344             msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
345                      "Try manually setting a character-encoding "
346                      "before you open the file.") );
347         }
348     }
349     else
350     {
351
352         if( p_sys->b_autodetect_utf8 )
353         {
354             if( IsUTF8( psz_subtitle ) == NULL )
355             {
356                 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
357                          "disabling UTF-8 subtitles autodetection" );
358                 p_sys->b_autodetect_utf8 = VLC_FALSE;
359             }
360         }
361
362         if( !p_sys->b_autodetect_utf8 )
363         {
364             size_t inbytes_left = strlen( psz_subtitle );
365             size_t outbytes_left = 6 * inbytes_left;
366             char *psz_new_subtitle = malloc( outbytes_left + 1 );
367             char *psz_convert_buffer_out = psz_new_subtitle;
368             const char *psz_convert_buffer_in = psz_subtitle;
369
370             size_t ret = vlc_iconv( p_sys->iconv_handle,
371                                     &psz_convert_buffer_in, &inbytes_left,
372                                     &psz_convert_buffer_out, &outbytes_left );
373
374             *psz_convert_buffer_out++ = '\0';
375             free( psz_subtitle );
376
377             if( ( ret == (size_t)(-1) ) || inbytes_left )
378             {
379                 free( psz_new_subtitle );
380                 msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
381                         "Try manually setting a character-encoding "
382                                 "before you open the file.") );
383                 return NULL;
384             }
385
386             psz_subtitle = realloc( psz_new_subtitle,
387                                     psz_convert_buffer_out - psz_new_subtitle );
388         }
389     }
390
391     /* Create the subpicture unit */
392     p_spu = p_dec->pf_spu_buffer_new( p_dec );
393     if( !p_spu )
394     {
395         msg_Warn( p_dec, "can't get spu buffer" );
396         if( psz_subtitle ) free( psz_subtitle );
397         return NULL;
398     }
399
400     p_spu->b_pausable = VLC_TRUE;
401
402     /* Create a new subpicture region */
403     memset( &fmt, 0, sizeof(video_format_t) );
404     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
405     fmt.i_aspect = 0;
406     fmt.i_width = fmt.i_height = 0;
407     fmt.i_x_offset = fmt.i_y_offset = 0;
408     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
409     if( !p_spu->p_region )
410     {
411         msg_Err( p_dec, "cannot allocate SPU region" );
412         if( psz_subtitle ) free( psz_subtitle );
413         p_dec->pf_spu_buffer_del( p_dec, p_spu );
414         return NULL;
415     }
416
417     /* Decode and format the subpicture unit */
418     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
419     {
420         /* Normal text subs, easy markup */
421         p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
422         p_spu->i_x = p_sys->i_align ? 20 : 0;
423         p_spu->i_y = 10;
424
425         /* Remove formatting from string */
426
427         p_spu->p_region->psz_text = StripTags( psz_subtitle );
428         if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
429         {
430             p_spu->p_region->psz_html = CreateHtmlSubtitle( psz_subtitle );
431         }
432
433         p_spu->i_start = p_block->i_pts;
434         p_spu->i_stop = p_block->i_pts + p_block->i_length;
435         p_spu->b_ephemer = (p_block->i_length == 0);
436         p_spu->b_absolute = VLC_FALSE;
437     }
438     else
439     {
440         /* Decode SSA/USF strings */
441         if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
442             ParseSSAString( p_dec, psz_subtitle, p_spu );
443
444         p_spu->i_start = p_block->i_pts;
445         p_spu->i_stop = p_block->i_pts + p_block->i_length;
446         p_spu->b_ephemer = (p_block->i_length == 0);
447         p_spu->b_absolute = VLC_FALSE;
448         p_spu->i_original_picture_width = p_sys->i_original_width;
449         p_spu->i_original_picture_height = p_sys->i_original_height;
450     }
451     if( psz_subtitle ) free( psz_subtitle );
452
453     return p_spu;
454 }
455
456 char* GotoNextLine( char *psz_text )
457 {
458     char *p_newline = psz_text;
459
460     while( p_newline[0] != '\0' )
461     {
462         if( p_newline[0] == '\n' || p_newline[0] == '\r' )
463         {
464             p_newline++;
465             while( p_newline[0] == '\n' || p_newline[0] == '\r' )
466                 p_newline++;
467             break;
468         }
469         else p_newline++;
470     }
471     return p_newline;
472 }
473
474 /* Function now handles tags which has attribute values, and tries
475  * to deal with &' commands too. It no longer modifies the string
476  * in place, so that the original text can be reused
477  */
478 static char *StripTags( char *psz_subtitle )
479 {
480     char *psz_text_start;
481     char *psz_text;
482
483     psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
484     if( !psz_text_start )
485         return NULL;
486
487     while( *psz_subtitle )
488     {
489         if( *psz_subtitle == '<' )
490         {
491             if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
492                 *psz_text++ = '\n';
493
494             psz_subtitle += strcspn( psz_subtitle, ">" );
495         }
496         else if( *psz_subtitle == '&' )
497         {
498             if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
499             {
500                 *psz_text++ = '<';
501                 psz_subtitle += strcspn( psz_subtitle, ";" );
502             }
503             else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
504             {
505                 *psz_text++ = '>';
506                 psz_subtitle += strcspn( psz_subtitle, ";" );
507             }
508             else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
509             {
510                 *psz_text++ = '&';
511                 psz_subtitle += strcspn( psz_subtitle, ";" );
512             }
513             else if( !strncasecmp( psz_subtitle, "&quot;", 6 ))
514             {
515                 *psz_text++ = '\"';
516                 psz_subtitle += strcspn( psz_subtitle, ";" );
517             }
518             else
519             {
520                 /* Assume it is just a normal ampersand */
521                 *psz_text++ = '&';
522             }
523         }
524         else
525         {
526             *psz_text++ = *psz_subtitle;
527         }
528
529         psz_subtitle++;
530     }
531     *psz_text = '\0';
532     psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
533
534     return psz_text_start;
535 }
536
537 /* Try to respect any style tags present in the subtitle string. The main
538  * problem here is a lack of adequate specs for the subtitle formats.
539  * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
540  * SAMI has a detailed spec, but extensive rework is needed in the demux
541  * code to prevent all this style information being excised, as it presently
542  * does.
543  * That leaves the others - none of which were (I guess) originally intended
544  * to be carrying style information. Over time people have used them that way.
545  * In the absence of specifications from which to work, the tags supported
546  * have been restricted to the simple set permitted by the USF DTD, ie. :
547  *  Basic: <br>, <i>, <b>, <u>
548  *  Extended: <font>
549  *    Attributes: face
550  *                family
551  *                size
552  *                color
553  *                outline-color
554  *                shadow-color
555  *                outline-level
556  *                shadow-level
557  *                back-color
558  *                alpha
559  * There is also the further restriction that the subtitle be well-formed
560  * as an XML entity, ie. the HTML sentence:
561  *        <b><i>Bold and Italics</b></i>
562  * doesn't qualify because the tags aren't nested one inside the other.
563  * <text> tags are automatically added to the output to ensure
564  * well-formedness.
565  * If the text doesn't qualify for any reason, a NULL string is
566  * returned, and the rendering engine will fall back to the
567  * plain text version of the subtitle.
568  */
569 static char *CreateHtmlSubtitle( char *psz_subtitle )
570 {
571     char    psz_tagStack[ 100 ];
572     size_t  i_buf_size     = strlen( psz_subtitle ) + 100;
573     char   *psz_html_start = malloc( i_buf_size );
574
575     psz_tagStack[ 0 ] = '\0';
576
577     if( psz_html_start != NULL )
578     {
579         char *psz_html = psz_html_start;
580
581         strcpy( psz_html, "<text>" );
582         psz_html += 6;
583
584         while( *psz_subtitle )
585         {
586             if( *psz_subtitle == '\n' )
587             {
588                 strcpy( psz_html, "<br/>" );
589                 psz_html += 5;
590                 psz_subtitle++;
591             }
592             else if( *psz_subtitle == '<' )
593             {
594                 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
595                 {
596                     strcpy( psz_html, "<br/>" );
597                     psz_html += 5;
598                     psz_subtitle += 5;
599                 }
600                 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
601                 {
602                     strcpy( psz_html, "<b>" );
603                     strcat( psz_tagStack, "b" );
604                     psz_html += 3;
605                     psz_subtitle += 3;
606                 }
607                 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
608                 {
609                     strcpy( psz_html, "<i>" );
610                     strcat( psz_tagStack, "i" );
611                     psz_html += 3;
612                     psz_subtitle += 3;
613                 }
614                 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
615                 {
616                     strcpy( psz_html, "<u>" );
617                     strcat( psz_tagStack, "u" );
618                     psz_html += 3;
619                     psz_subtitle += 3;
620                 }
621                 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
622                 {
623                     const char *psz_attribs[] = { "face=\"", "family=\"", "size=\"",
624                             "color=\"", "outline-color=\"", "shadow-color=\"",
625                             "outline-level=\"", "shadow-level=\"", "back-color=\"",
626                             "alpha=\"", NULL };
627
628                     strcpy( psz_html, "<font " );
629                     strcat( psz_tagStack, "f" );
630                     psz_html += 6;
631                     psz_subtitle += 6;
632
633                     while( *psz_subtitle != '>' )
634                     {
635                         int  k;
636
637                         for( k=0; psz_attribs[ k ]; k++ )
638                         {
639                             int i_len = strlen( psz_attribs[ k ] );
640
641                             if( !strncasecmp( psz_subtitle, psz_attribs[ k ], i_len ))
642                             {
643                                 i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
644
645                                 strncpy( psz_html, psz_subtitle, i_len );
646                                 psz_html += i_len;
647                                 psz_subtitle += i_len;
648                                 break;
649                             }
650                         }
651                         if( psz_attribs[ k ] == NULL )
652                         {
653                             /* Jump over unrecognised tag */
654                             int i_len = strcspn( psz_subtitle, "\"" ) + 1;
655
656                             i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
657                             psz_subtitle += i_len;
658                         }
659                         while (*psz_subtitle == ' ')
660                             *psz_html++ = *psz_subtitle++;
661                     }
662                     *psz_html++ = *psz_subtitle++;
663                 }
664                 else if( !strncmp( psz_subtitle, "</", 2 ))
665                 {
666                     vlc_bool_t  b_match     = VLC_FALSE;
667                     int         i_len       = strlen( psz_tagStack ) - 1;
668                     char       *psz_lastTag = NULL;
669
670                     if( i_len >= 0 )
671                     {
672                         psz_lastTag = psz_tagStack + i_len;
673                         i_len = 0;
674
675                         switch( *psz_lastTag )
676                         {
677                             case 'b':
678                                 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
679                                 i_len   = 4;
680                                 break;
681                             case 'i':
682                                 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
683                                 i_len   = 4;
684                                 break;
685                             case 'u':
686                                 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
687                                 i_len   = 4;
688                                 break;
689                             case 'f':
690                                 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
691                                 i_len   = 7;
692                                 break;
693                         }
694                     }
695                     if( ! b_match )
696                     {
697                         /* Not well formed -- kill everything */
698                         free( psz_html_start );
699                         psz_html_start = NULL;
700                         break;
701                     }
702                     *psz_lastTag = '\0';
703                     strncpy( psz_html, psz_subtitle, i_len );
704                     psz_html += i_len;
705                     psz_subtitle += i_len;
706                 }
707                 else
708                 {
709                     psz_subtitle += strcspn( psz_subtitle, ">" );
710                 }
711             }
712             else if( *psz_subtitle == '&' )
713             {
714                 if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
715                 {
716                     strcpy( psz_html, "&lt;" );
717                     psz_html += 4;
718                     psz_subtitle += 4;
719                 }
720                 else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
721                 {
722                     strcpy( psz_html, "&gt;" );
723                     psz_html += 4;
724                     psz_subtitle += 4;
725                 }
726                 else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
727                 {
728                     strcpy( psz_html, "&amp;" );
729                     psz_html += 5;
730                     psz_subtitle += 5;
731                 }
732                 else
733                 {
734                     strcpy( psz_html, "&amp;" );
735                     psz_html += 5;
736                     psz_subtitle++;
737                 }
738             }
739             else
740             {
741                 *psz_html = *psz_subtitle;
742                 if( psz_html > psz_html_start )
743                 {
744                     /* Check for double whitespace */
745                     if((( *psz_html == ' ' ) ||
746                         ( *psz_html == '\t' )) &&
747                        (( *(psz_html-1) == ' ' ) ||
748                         ( *(psz_html-1) == '\t' )))
749                     {
750                         strcpy( psz_html, NO_BREAKING_SPACE );
751                         psz_html += strlen( NO_BREAKING_SPACE ) - 1;
752                     }
753                 }
754                 psz_html++;
755                 psz_subtitle++;
756             }
757
758             if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 10 )
759             {
760                 int i_len = psz_html - psz_html_start;
761
762                 i_buf_size += 100;
763                 psz_html_start = realloc( psz_html_start, i_buf_size );
764                 psz_html = psz_html_start + i_len;
765                 *psz_html = '\0';
766             }
767         }
768         strcpy( psz_html, "</text>" );
769         psz_html += 7;
770
771         if( psz_tagStack[ 0 ] != '\0' )
772         {
773             /* Not well formed -- kill everything */
774             free( psz_html_start );
775             psz_html_start = NULL;
776         }
777         else if( psz_html_start )
778         {
779             /* Shrink the memory requirements */
780             psz_html_start = realloc( psz_html_start,  psz_html - psz_html_start + 1 );
781         }
782     }
783     return psz_html_start;
784 }