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