]> git.sesse.net Git - vlc/blob - modules/codec/subtitles/subsdec.c
Add t140 FOURCC for always-UTF-8 subs
[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     switch( p_dec->fmt_in.i_codec )
143     {
144         case VLC_FOURCC('s','u','b','t'):
145         case VLC_FOURCC('s','s','a',' '):
146         case VLC_FOURCC('t','1','4','0'):
147             break;
148         default:
149             return VLC_EGENERIC;
150     }
151
152     p_dec->pf_decode_sub = DecodeBlock;
153
154     /* Allocate the memory needed to store the decoder's structure */
155     p_dec->p_sys = p_sys = malloc( sizeof( *p_sys ) );
156     if( p_sys == NULL )
157     {
158         msg_Err( p_dec, "out of memory" );
159         return VLC_ENOMEM;
160     }
161
162     /* init of p_sys */
163     memset( p_sys, 0, sizeof( *p_sys ) );
164     p_sys->i_align = 0;
165     p_sys->iconv_handle = (vlc_iconv_t)-1;
166     p_sys->b_autodetect_utf8 = VLC_FALSE;
167     p_sys->b_ass = VLC_FALSE;
168     p_sys->i_original_height = -1;
169     p_sys->i_original_width = -1;
170     TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
171     TAB_INIT( p_sys->i_images, p_sys->pp_images );
172
173     char *psz_charset = NULL;
174
175     if( p_dec->fmt_in.i_codec == VLC_FOURCC('t','1','4','0') )
176         psz_charset = strdup( "UTF-8" ); /* IUT T.140 is always using UTF-8 */
177     else
178     /* First try demux-specified encoding */
179     if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
180     {
181         psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
182         msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
183                  p_dec->fmt_in.subs.psz_encoding ?: "not specified");
184     }
185
186     /* Second, try configured encoding */
187     if (psz_charset == NULL)
188     {
189         psz_charset = var_CreateGetNonEmptyString (p_dec, "subsdec-encoding");
190         if ((psz_charset != NULL) && !strcasecmp (psz_charset, DEFAULT_NAME))
191         {
192             free (psz_charset);
193             psz_charset = NULL;
194         }
195
196         msg_Dbg (p_dec, "trying configured character encoding: %s",
197                  psz_charset ?: "not specified");
198     }
199
200     /* Third, try "local" encoding with optional UTF-8 autodetection */
201     if (psz_charset == NULL)
202     {
203         psz_charset = strdup (GetFallbackEncoding ());
204         msg_Dbg (p_dec, "trying default character encoding: %s",
205                  psz_charset ?: "not specified");
206
207         if (var_CreateGetBool (p_dec, "subsdec-autodetect-utf8"))
208         {
209             msg_Dbg (p_dec, "using automatic UTF-8 detection");
210             p_sys->b_autodetect_utf8 = VLC_TRUE;
211         }
212     }
213
214     if (psz_charset == NULL)
215     {
216         psz_charset = strdup ("UTF-8");
217         msg_Dbg (p_dec, "trying hard-coded character encoding: %s",
218                  psz_charset ? psz_charset : "error");
219     }
220
221     if (psz_charset == NULL)
222     {
223         free (p_sys);
224         return VLC_ENOMEM;
225     }
226
227     if (strcasecmp (psz_charset, "UTF-8") && strcasecmp (psz_charset, "utf8"))
228     {
229         p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
230         if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
231             msg_Err (p_dec, "cannot convert from %s: %s", psz_charset,
232                      strerror (errno));
233     }
234     free (psz_charset);
235
236     var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
237     var_Get( p_dec, "subsdec-align", &val );
238     p_sys->i_align = val.i_int;
239
240     if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ')
241      && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
242     {
243         if( p_dec->fmt_in.i_extra > 0 )
244             ParseSSAHeader( p_dec );
245     }
246
247     return VLC_SUCCESS;
248 }
249
250 /****************************************************************************
251  * DecodeBlock: the whole thing
252  ****************************************************************************
253  * This function must be fed with complete subtitles units.
254  ****************************************************************************/
255 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
256 {
257     subpicture_t *p_spu = NULL;
258
259     if( !pp_block || *pp_block == NULL ) return NULL;
260
261     p_spu = ParseText( p_dec, *pp_block );
262
263     block_Release( *pp_block );
264     *pp_block = NULL;
265
266     return p_spu;
267 }
268
269 /*****************************************************************************
270  * CloseDecoder: clean up the decoder
271  *****************************************************************************/
272 static void CloseDecoder( vlc_object_t *p_this )
273 {
274     decoder_t *p_dec = (decoder_t *)p_this;
275     decoder_sys_t *p_sys = p_dec->p_sys;
276
277     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
278         vlc_iconv_close( p_sys->iconv_handle );
279
280     if( p_sys->pp_ssa_styles )
281     {
282         int i;
283         for( i = 0; i < p_sys->i_ssa_styles; i++ )
284         {
285             if( !p_sys->pp_ssa_styles[i] )
286                 continue;
287
288             if( p_sys->pp_ssa_styles[i]->psz_stylename )
289                 free( p_sys->pp_ssa_styles[i]->psz_stylename );
290             if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname )
291                 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
292             if( p_sys->pp_ssa_styles[i] )
293                 free( p_sys->pp_ssa_styles[i] );
294         }
295         TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
296     }
297     if( p_sys->pp_images )
298     {
299         int i;
300         for( i = 0; i < p_sys->i_images; i++ )
301         {
302             if( !p_sys->pp_images[i] )
303                 continue;
304
305             if( p_sys->pp_images[i]->p_pic )
306                 p_sys->pp_images[i]->p_pic->pf_release( p_sys->pp_images[i]->p_pic );
307             if( p_sys->pp_images[i]->psz_filename )
308                 free( p_sys->pp_images[i]->psz_filename );
309
310             free( p_sys->pp_images[i] );
311         }
312         TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
313     }
314
315     free( p_sys );
316 }
317
318 /*****************************************************************************
319  * ParseText: parse an text subtitle packet and send it to the video output
320  *****************************************************************************/
321 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
322 {
323     decoder_sys_t *p_sys = p_dec->p_sys;
324     subpicture_t *p_spu = NULL;
325     char *psz_subtitle = NULL;
326     video_format_t fmt;
327
328     /* We cannot display a subpicture with no date */
329     if( p_block->i_pts == 0 )
330     {
331         msg_Warn( p_dec, "subtitle without a date" );
332         return NULL;
333     }
334
335     /* Check validity of packet data */
336     /* An "empty" line containing only \0 can be used to force
337        and ephemer picture from the screen */
338     if( p_block->i_buffer < 1 )
339     {
340         msg_Warn( p_dec, "no subtitle data" );
341         return NULL;
342     }
343
344     /* Should be resiliant against bad subtitles */
345     psz_subtitle = strndup( (const char *)p_block->p_buffer,
346                             p_block->i_buffer );
347     if( psz_subtitle == NULL )
348         return NULL;
349
350     if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
351     {
352         if (EnsureUTF8( psz_subtitle ) == NULL)
353         {
354             msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
355                      "Try manually setting a character-encoding "
356                      "before you open the file.") );
357         }
358     }
359     else
360     {
361
362         if( p_sys->b_autodetect_utf8 )
363         {
364             if( IsUTF8( psz_subtitle ) == NULL )
365             {
366                 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
367                          "disabling UTF-8 subtitles autodetection" );
368                 p_sys->b_autodetect_utf8 = VLC_FALSE;
369             }
370         }
371
372         if( !p_sys->b_autodetect_utf8 )
373         {
374             size_t inbytes_left = strlen( psz_subtitle );
375             size_t outbytes_left = 6 * inbytes_left;
376             char *psz_new_subtitle = malloc( outbytes_left + 1 );
377             char *psz_convert_buffer_out = psz_new_subtitle;
378             const char *psz_convert_buffer_in = psz_subtitle;
379
380             size_t ret = vlc_iconv( p_sys->iconv_handle,
381                                     &psz_convert_buffer_in, &inbytes_left,
382                                     &psz_convert_buffer_out, &outbytes_left );
383
384             *psz_convert_buffer_out++ = '\0';
385             free( psz_subtitle );
386
387             if( ( ret == (size_t)(-1) ) || inbytes_left )
388             {
389                 free( psz_new_subtitle );
390                 msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
391                         "Try manually setting a character-encoding "
392                                 "before you open the file.") );
393                 return NULL;
394             }
395
396             psz_subtitle = realloc( psz_new_subtitle,
397                                     psz_convert_buffer_out - psz_new_subtitle );
398         }
399     }
400
401     /* Create the subpicture unit */
402     p_spu = p_dec->pf_spu_buffer_new( p_dec );
403     if( !p_spu )
404     {
405         msg_Warn( p_dec, "can't get spu buffer" );
406         if( psz_subtitle ) free( psz_subtitle );
407         return NULL;
408     }
409
410     p_spu->b_pausable = VLC_TRUE;
411
412     /* Create a new subpicture region */
413     memset( &fmt, 0, sizeof(video_format_t) );
414     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
415     fmt.i_aspect = 0;
416     fmt.i_width = fmt.i_height = 0;
417     fmt.i_x_offset = fmt.i_y_offset = 0;
418     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
419     if( !p_spu->p_region )
420     {
421         msg_Err( p_dec, "cannot allocate SPU region" );
422         if( psz_subtitle ) free( psz_subtitle );
423         p_dec->pf_spu_buffer_del( p_dec, p_spu );
424         return NULL;
425     }
426
427     /* Decode and format the subpicture unit */
428     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
429     {
430         /* Normal text subs, easy markup */
431         p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
432         p_spu->i_x = p_sys->i_align ? 20 : 0;
433         p_spu->i_y = 10;
434
435         /* Remove formatting from string */
436
437         p_spu->p_region->psz_text = StripTags( psz_subtitle );
438         if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
439         {
440             p_spu->p_region->psz_html = CreateHtmlSubtitle( psz_subtitle );
441         }
442
443         p_spu->i_start = p_block->i_pts;
444         p_spu->i_stop = p_block->i_pts + p_block->i_length;
445         p_spu->b_ephemer = (p_block->i_length == 0);
446         p_spu->b_absolute = VLC_FALSE;
447     }
448     else
449     {
450         /* Decode SSA/USF strings */
451         if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
452             ParseSSAString( p_dec, psz_subtitle, p_spu );
453
454         p_spu->i_start = p_block->i_pts;
455         p_spu->i_stop = p_block->i_pts + p_block->i_length;
456         p_spu->b_ephemer = (p_block->i_length == 0);
457         p_spu->b_absolute = VLC_FALSE;
458         p_spu->i_original_picture_width = p_sys->i_original_width;
459         p_spu->i_original_picture_height = p_sys->i_original_height;
460     }
461     if( psz_subtitle ) free( psz_subtitle );
462
463     return p_spu;
464 }
465
466 char* GotoNextLine( char *psz_text )
467 {
468     char *p_newline = psz_text;
469
470     while( p_newline[0] != '\0' )
471     {
472         if( p_newline[0] == '\n' || p_newline[0] == '\r' )
473         {
474             p_newline++;
475             while( p_newline[0] == '\n' || p_newline[0] == '\r' )
476                 p_newline++;
477             break;
478         }
479         else p_newline++;
480     }
481     return p_newline;
482 }
483
484 /* Function now handles tags with attribute values, and tries
485  * to deal with &' commands too. It no longer modifies the string
486  * in place, so that the original text can be reused
487  */
488 static char *StripTags( char *psz_subtitle )
489 {
490     char *psz_text_start;
491     char *psz_text;
492
493     psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
494     if( !psz_text_start )
495         return NULL;
496
497     while( *psz_subtitle )
498     {
499         if( *psz_subtitle == '<' )
500         {
501             if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
502                 *psz_text++ = '\n';
503
504             psz_subtitle += strcspn( psz_subtitle, ">" );
505         }
506         else if( *psz_subtitle == '&' )
507         {
508             if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
509             {
510                 *psz_text++ = '<';
511                 psz_subtitle += strcspn( psz_subtitle, ";" );
512             }
513             else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
514             {
515                 *psz_text++ = '>';
516                 psz_subtitle += strcspn( psz_subtitle, ";" );
517             }
518             else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
519             {
520                 *psz_text++ = '&';
521                 psz_subtitle += strcspn( psz_subtitle, ";" );
522             }
523             else if( !strncasecmp( psz_subtitle, "&quot;", 6 ))
524             {
525                 *psz_text++ = '\"';
526                 psz_subtitle += strcspn( psz_subtitle, ";" );
527             }
528             else
529             {
530                 /* Assume it is just a normal ampersand */
531                 *psz_text++ = '&';
532             }
533         }
534         else
535         {
536             *psz_text++ = *psz_subtitle;
537         }
538
539         psz_subtitle++;
540     }
541     *psz_text = '\0';
542     psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
543
544     return psz_text_start;
545 }
546
547 /* Try to respect any style tags present in the subtitle string. The main
548  * problem here is a lack of adequate specs for the subtitle formats.
549  * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
550  * SAMI has a detailed spec, but extensive rework is needed in the demux
551  * code to prevent all this style information being excised, as it presently
552  * does.
553  * That leaves the others - none of which were (I guess) originally intended
554  * to be carrying style information. Over time people have used them that way.
555  * In the absence of specifications from which to work, the tags supported
556  * have been restricted to the simple set permitted by the USF DTD, ie. :
557  *  Basic: <br>, <i>, <b>, <u>
558  *  Extended: <font>
559  *    Attributes: face
560  *                family
561  *                size
562  *                color
563  *                outline-color
564  *                shadow-color
565  *                outline-level
566  *                shadow-level
567  *                back-color
568  *                alpha
569  * There is also the further restriction that the subtitle be well-formed
570  * as an XML entity, ie. the HTML sentence:
571  *        <b><i>Bold and Italics</b></i>
572  * doesn't qualify because the tags aren't nested one inside the other.
573  * <text> tags are automatically added to the output to ensure
574  * well-formedness.
575  * If the text doesn't qualify for any reason, a NULL string is
576  * returned, and the rendering engine will fall back to the
577  * plain text version of the subtitle.
578  */
579 static char *CreateHtmlSubtitle( char *psz_subtitle )
580 {
581     char    psz_tagStack[ 100 ];
582     size_t  i_buf_size     = strlen( psz_subtitle ) + 100;
583     char   *psz_html_start = malloc( i_buf_size );
584
585     psz_tagStack[ 0 ] = '\0';
586
587     if( psz_html_start != NULL )
588     {
589         char *psz_html = psz_html_start;
590
591         strcpy( psz_html, "<text>" );
592         psz_html += 6;
593
594         while( *psz_subtitle )
595         {
596             if( *psz_subtitle == '\n' )
597             {
598                 strcpy( psz_html, "<br/>" );
599                 psz_html += 5;
600                 psz_subtitle++;
601             }
602             else if( *psz_subtitle == '<' )
603             {
604                 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
605                 {
606                     strcpy( psz_html, "<br/>" );
607                     psz_html += 5;
608                     psz_subtitle += 5;
609                 }
610                 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
611                 {
612                     strcpy( psz_html, "<b>" );
613                     strcat( psz_tagStack, "b" );
614                     psz_html += 3;
615                     psz_subtitle += 3;
616                 }
617                 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
618                 {
619                     strcpy( psz_html, "<i>" );
620                     strcat( psz_tagStack, "i" );
621                     psz_html += 3;
622                     psz_subtitle += 3;
623                 }
624                 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
625                 {
626                     strcpy( psz_html, "<u>" );
627                     strcat( psz_tagStack, "u" );
628                     psz_html += 3;
629                     psz_subtitle += 3;
630                 }
631                 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
632                 {
633                     const char *psz_attribs[] = { "face=\"", "family=\"", "size=\"",
634                             "color=\"", "outline-color=\"", "shadow-color=\"",
635                             "outline-level=\"", "shadow-level=\"", "back-color=\"",
636                             "alpha=\"", NULL };
637
638                     strcpy( psz_html, "<font " );
639                     strcat( psz_tagStack, "f" );
640                     psz_html += 6;
641                     psz_subtitle += 6;
642
643                     while( *psz_subtitle != '>' )
644                     {
645                         int  k;
646
647                         for( k=0; psz_attribs[ k ]; k++ )
648                         {
649                             int i_len = strlen( psz_attribs[ k ] );
650
651                             if( !strncasecmp( psz_subtitle, psz_attribs[ k ], i_len ))
652                             {
653                                 i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
654
655                                 strncpy( psz_html, psz_subtitle, i_len );
656                                 psz_html += i_len;
657                                 psz_subtitle += i_len;
658                                 break;
659                             }
660                         }
661                         if( psz_attribs[ k ] == NULL )
662                         {
663                             /* Jump over unrecognised tag */
664                             int i_len = strcspn( psz_subtitle, "\"" ) + 1;
665
666                             i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
667                             psz_subtitle += i_len;
668                         }
669                         while (*psz_subtitle == ' ')
670                             *psz_html++ = *psz_subtitle++;
671                     }
672                     *psz_html++ = *psz_subtitle++;
673                 }
674                 else if( !strncmp( psz_subtitle, "</", 2 ))
675                 {
676                     vlc_bool_t  b_match     = VLC_FALSE;
677                     int         i_len       = strlen( psz_tagStack ) - 1;
678                     char       *psz_lastTag = NULL;
679
680                     if( i_len >= 0 )
681                     {
682                         psz_lastTag = psz_tagStack + i_len;
683                         i_len = 0;
684
685                         switch( *psz_lastTag )
686                         {
687                             case 'b':
688                                 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
689                                 i_len   = 4;
690                                 break;
691                             case 'i':
692                                 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
693                                 i_len   = 4;
694                                 break;
695                             case 'u':
696                                 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
697                                 i_len   = 4;
698                                 break;
699                             case 'f':
700                                 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
701                                 i_len   = 7;
702                                 break;
703                         }
704                     }
705                     if( ! b_match )
706                     {
707                         /* Not well formed -- kill everything */
708                         free( psz_html_start );
709                         psz_html_start = NULL;
710                         break;
711                     }
712                     *psz_lastTag = '\0';
713                     strncpy( psz_html, psz_subtitle, i_len );
714                     psz_html += i_len;
715                     psz_subtitle += i_len;
716                 }
717                 else
718                 {
719                     psz_subtitle += strcspn( psz_subtitle, ">" );
720                 }
721             }
722             else if( *psz_subtitle == '&' )
723             {
724                 if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
725                 {
726                     strcpy( psz_html, "&lt;" );
727                     psz_html += 4;
728                     psz_subtitle += 4;
729                 }
730                 else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
731                 {
732                     strcpy( psz_html, "&gt;" );
733                     psz_html += 4;
734                     psz_subtitle += 4;
735                 }
736                 else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
737                 {
738                     strcpy( psz_html, "&amp;" );
739                     psz_html += 5;
740                     psz_subtitle += 5;
741                 }
742                 else
743                 {
744                     strcpy( psz_html, "&amp;" );
745                     psz_html += 5;
746                     psz_subtitle++;
747                 }
748             }
749             else
750             {
751                 *psz_html = *psz_subtitle;
752                 if( psz_html > psz_html_start )
753                 {
754                     /* Check for double whitespace */
755                     if((( *psz_html == ' ' ) ||
756                         ( *psz_html == '\t' )) &&
757                        (( *(psz_html-1) == ' ' ) ||
758                         ( *(psz_html-1) == '\t' )))
759                     {
760                         strcpy( psz_html, NO_BREAKING_SPACE );
761                         psz_html += strlen( NO_BREAKING_SPACE ) - 1;
762                     }
763                 }
764                 psz_html++;
765                 psz_subtitle++;
766             }
767
768             if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 10 )
769             {
770                 int i_len = psz_html - psz_html_start;
771
772                 i_buf_size += 100;
773                 psz_html_start = realloc( psz_html_start, i_buf_size );
774                 psz_html = psz_html_start + i_len;
775                 *psz_html = '\0';
776             }
777         }
778         strcpy( psz_html, "</text>" );
779         psz_html += 7;
780
781         if( psz_tagStack[ 0 ] != '\0' )
782         {
783             /* Not well formed -- kill everything */
784             free( psz_html_start );
785             psz_html_start = NULL;
786         }
787         else if( psz_html_start )
788         {
789             /* Shrink the memory requirements */
790             psz_html_start = realloc( psz_html_start,  psz_html - psz_html_start + 1 );
791         }
792     }
793     return psz_html_start;
794 }