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