]> git.sesse.net Git - vlc/blob - modules/codec/subtitles/subsdec.c
Removed unused or redondant fields from subpicture.
[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( int *pi_align, 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, NULL );
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         return VLC_ENOMEM;
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 = false;
170     p_sys->b_ass = 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 = 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;
256     block_t *p_block;
257
258     if( !pp_block || *pp_block == NULL )
259         return NULL;
260
261     p_block = *pp_block;
262     if( p_block->i_rate != 0 )
263         p_block->i_length = p_block->i_length * p_block->i_rate / INPUT_RATE_DEFAULT;
264
265     p_spu = ParseText( p_dec, p_block );
266
267     block_Release( p_block );
268     *pp_block = NULL;
269
270     return p_spu;
271 }
272
273 /*****************************************************************************
274  * CloseDecoder: clean up the decoder
275  *****************************************************************************/
276 static void CloseDecoder( vlc_object_t *p_this )
277 {
278     decoder_t *p_dec = (decoder_t *)p_this;
279     decoder_sys_t *p_sys = p_dec->p_sys;
280
281     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
282         vlc_iconv_close( p_sys->iconv_handle );
283
284     if( p_sys->pp_ssa_styles )
285     {
286         int i;
287         for( i = 0; i < p_sys->i_ssa_styles; i++ )
288         {
289             if( !p_sys->pp_ssa_styles[i] )
290                 continue;
291
292             free( p_sys->pp_ssa_styles[i]->psz_stylename );
293             free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
294             free( p_sys->pp_ssa_styles[i] );
295         }
296         TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
297     }
298     if( p_sys->pp_images )
299     {
300         int i;
301         for( i = 0; i < p_sys->i_images; i++ )
302         {
303             if( !p_sys->pp_images[i] )
304                 continue;
305
306             if( p_sys->pp_images[i]->p_pic )
307                 picture_Release( p_sys->pp_images[i]->p_pic );
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 = 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         free( psz_subtitle );
407         return NULL;
408     }
409
410     /* Create a new subpicture region */
411     memset( &fmt, 0, sizeof(video_format_t) );
412     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
413     fmt.i_aspect = 0;
414     fmt.i_width = fmt.i_height = 0;
415     fmt.i_x_offset = fmt.i_y_offset = 0;
416     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
417     if( !p_spu->p_region )
418     {
419         msg_Err( p_dec, "cannot allocate SPU region" );
420         free( psz_subtitle );
421         p_dec->pf_spu_buffer_del( p_dec, p_spu );
422         return NULL;
423     }
424
425     /* Decode and format the subpicture unit */
426     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
427     {
428         /* Normal text subs, easy markup */
429         p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
430         p_spu->p_region->i_x = p_sys->i_align ? 20 : 0;
431         p_spu->p_region->i_y = 10;
432
433         /* Remove formatting from string */
434
435         p_spu->p_region->psz_text = StripTags( psz_subtitle );
436         if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
437         {
438             p_spu->p_region->psz_html = CreateHtmlSubtitle( &p_spu->p_region->i_align, psz_subtitle );
439         }
440
441         p_spu->i_start = p_block->i_pts;
442         p_spu->i_stop = p_block->i_pts + p_block->i_length;
443         p_spu->b_ephemer = (p_block->i_length == 0);
444         p_spu->b_absolute = false;
445     }
446     else
447     {
448         /* Decode SSA/USF strings */
449         if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
450             ParseSSAString( p_dec, psz_subtitle, p_spu );
451
452         p_spu->i_start = p_block->i_pts;
453         p_spu->i_stop = p_block->i_pts + p_block->i_length;
454         p_spu->b_ephemer = (p_block->i_length == 0);
455         p_spu->b_absolute = false;
456         p_spu->i_original_picture_width = p_sys->i_original_width;
457         p_spu->i_original_picture_height = p_sys->i_original_height;
458     }
459     free( psz_subtitle );
460
461     return p_spu;
462 }
463
464 char* GotoNextLine( char *psz_text )
465 {
466     char *p_newline = psz_text;
467
468     while( p_newline[0] != '\0' )
469     {
470         if( p_newline[0] == '\n' || p_newline[0] == '\r' )
471         {
472             p_newline++;
473             while( p_newline[0] == '\n' || p_newline[0] == '\r' )
474                 p_newline++;
475             break;
476         }
477         else p_newline++;
478     }
479     return p_newline;
480 }
481
482 /* Function now handles tags with attribute values, and tries
483  * to deal with &' commands too. It no longer modifies the string
484  * in place, so that the original text can be reused
485  */
486 static char *StripTags( char *psz_subtitle )
487 {
488     char *psz_text_start;
489     char *psz_text;
490
491     psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
492     if( !psz_text_start )
493         return NULL;
494
495     while( *psz_subtitle )
496     {
497         if( *psz_subtitle == '<' )
498         {
499             if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
500                 *psz_text++ = '\n';
501
502             psz_subtitle += strcspn( psz_subtitle, ">" );
503         }
504         else if( *psz_subtitle == '&' )
505         {
506             if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
507             {
508                 *psz_text++ = '<';
509                 psz_subtitle += strcspn( psz_subtitle, ";" );
510             }
511             else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
512             {
513                 *psz_text++ = '>';
514                 psz_subtitle += strcspn( psz_subtitle, ";" );
515             }
516             else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
517             {
518                 *psz_text++ = '&';
519                 psz_subtitle += strcspn( psz_subtitle, ";" );
520             }
521             else if( !strncasecmp( psz_subtitle, "&quot;", 6 ))
522             {
523                 *psz_text++ = '\"';
524                 psz_subtitle += strcspn( psz_subtitle, ";" );
525             }
526             else
527             {
528                 /* Assume it is just a normal ampersand */
529                 *psz_text++ = '&';
530             }
531         }
532         else
533         {
534             *psz_text++ = *psz_subtitle;
535         }
536
537         psz_subtitle++;
538     }
539     *psz_text = '\0';
540     psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
541
542     return psz_text_start;
543 }
544
545 /* Try to respect any style tags present in the subtitle string. The main
546  * problem here is a lack of adequate specs for the subtitle formats.
547  * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
548  * SAMI has a detailed spec, but extensive rework is needed in the demux
549  * code to prevent all this style information being excised, as it presently
550  * does.
551  * That leaves the others - none of which were (I guess) originally intended
552  * to be carrying style information. Over time people have used them that way.
553  * In the absence of specifications from which to work, the tags supported
554  * have been restricted to the simple set permitted by the USF DTD, ie. :
555  *  Basic: <br>, <i>, <b>, <u>
556  *  Extended: <font>
557  *    Attributes: face
558  *                family
559  *                size
560  *                color
561  *                outline-color
562  *                shadow-color
563  *                outline-level
564  *                shadow-level
565  *                back-color
566  *                alpha
567  * There is also the further restriction that the subtitle be well-formed
568  * as an XML entity, ie. the HTML sentence:
569  *        <b><i>Bold and Italics</b></i>
570  * doesn't qualify because the tags aren't nested one inside the other.
571  * <text> tags are automatically added to the output to ensure
572  * well-formedness.
573  * If the text doesn't qualify for any reason, a NULL string is
574  * returned, and the rendering engine will fall back to the
575  * plain text version of the subtitle.
576  */
577 static void HtmlNPut( char **ppsz_html, const char *psz_text, int i_max )
578 {
579     const int i_len = strlen(psz_text);
580
581     strncpy( *ppsz_html, psz_text, i_max );
582     *ppsz_html += __MIN(i_max,i_len);
583 }
584
585 static void HtmlPut( char **ppsz_html, const char *psz_text )
586 {
587     strcpy( *ppsz_html, psz_text );
588     *ppsz_html += strlen(psz_text);
589 }
590 static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_text )
591 {
592     HtmlPut( ppsz_html, psz_text );
593     *ppsz_subtitle += strlen(psz_text);
594 }
595
596 static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
597 {
598     char   *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
599     if( !psz_tag ) return NULL;
600     size_t  i_buf_size     = strlen( psz_subtitle ) + 100;
601     char   *psz_html_start = malloc( i_buf_size );
602
603     psz_tag[ 0 ] = '\0';
604
605     if( psz_html_start == NULL )
606     {
607         free( psz_tag );
608         return NULL;
609     }
610     
611     char *psz_html = psz_html_start;
612
613     strcpy( psz_html, "<text>" );
614     psz_html += 6;
615
616     /* Check for forced alignment */
617     if( !strncmp( psz_subtitle, "{\\an", 4 ) && psz_subtitle[4] >= '1' && psz_subtitle[4] <= '9' && psz_subtitle[5] == '}' )
618     {
619         static const int pi_vertical[3] = { SUBPICTURE_ALIGN_BOTTOM, 0, SUBPICTURE_ALIGN_TOP };
620         static const int pi_horizontal[3] = { SUBPICTURE_ALIGN_LEFT, 0, SUBPICTURE_ALIGN_RIGHT };
621         const int i_id = psz_subtitle[4] - '1';
622
623         *pi_align = pi_vertical[i_id/3] | pi_horizontal[i_id%3];
624
625         psz_subtitle += 6;
626     }
627
628     /* */
629     while( *psz_subtitle )
630     {
631         if( *psz_subtitle == '\n' )
632         {
633             HtmlPut( &psz_html, "<br/>" );
634             psz_subtitle++;
635         }
636         else if( *psz_subtitle == '<' )
637         {
638             if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
639             {
640                 HtmlCopy( &psz_html, &psz_subtitle, "<br/>" );
641             }
642             else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
643             {
644                 HtmlCopy( &psz_html, &psz_subtitle, "<b>" );
645                 strcat( psz_tag, "b" );
646             }
647             else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
648             {
649                 HtmlCopy( &psz_html, &psz_subtitle, "<i>" );
650                 strcat( psz_tag, "i" );
651             }
652             else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
653             {
654                 HtmlCopy( &psz_html, &psz_subtitle, "<u>" );
655                 strcat( psz_tag, "u" );
656             }
657             else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
658             {
659                 const char *psz_attribs[] = { "face=", "family=", "size=",
660                         "color=", "outline-color=", "shadow-color=",
661                         "outline-level=", "shadow-level=", "back-color=",
662                         "alpha=", NULL };
663
664                 HtmlCopy( &psz_html, &psz_subtitle, "<font " );
665                 strcat( psz_tag, "f" );
666
667                 while( *psz_subtitle != '>' )
668                 {
669                     int  k;
670
671                     for( k=0; psz_attribs[ k ]; k++ )
672                     {
673                         int i_len = strlen( psz_attribs[ k ] );
674
675                         if( !strncasecmp( psz_subtitle, psz_attribs[k], i_len ) )
676                         {
677                             /* */
678                             HtmlPut( &psz_html, psz_attribs[k] );
679                             psz_subtitle += i_len;
680
681                             /* */
682                             if( *psz_subtitle == '"' )
683                             {
684                                 psz_subtitle++;
685                                 i_len = strcspn( psz_subtitle, "\"" );
686                             }
687                             else
688                             {
689                                 i_len = strcspn( psz_subtitle, " \t>" );
690                             }
691                             HtmlPut( &psz_html, "\"" );
692                             if( !strcmp( psz_attribs[ k ], "color=" ) && *psz_subtitle >= '0' && *psz_subtitle <= '9' )
693                                 HtmlPut( &psz_html, "#" );
694                             HtmlNPut( &psz_html, psz_subtitle, i_len );
695                             HtmlPut( &psz_html, "\"" );
696
697                             psz_subtitle += i_len;
698                             if( *psz_subtitle == '\"' )
699                                 psz_subtitle++;
700                             break;
701                         }
702                     }
703                     if( psz_attribs[ k ] == NULL )
704                     {
705                         /* Jump over unrecognised tag */
706                         int i_len = strcspn( psz_subtitle, "\"" ) + 1;
707
708                         i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
709                         psz_subtitle += i_len;
710                     }
711                     while (*psz_subtitle == ' ')
712                         *psz_html++ = *psz_subtitle++;
713                 }
714                 *psz_html++ = *psz_subtitle++;
715             }
716             else if( !strncmp( psz_subtitle, "</", 2 ))
717             {
718                 bool   b_match     = false;
719                 int    i_len       = strlen( psz_tag ) - 1;
720                 char  *psz_lastTag = NULL;
721
722                 if( i_len >= 0 )
723                 {
724                     psz_lastTag = psz_tag + i_len;
725                     i_len = 0;
726
727                     switch( *psz_lastTag )
728                     {
729                     case 'b':
730                         b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
731                         i_len   = 4;
732                         break;
733                     case 'i':
734                         b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
735                         i_len   = 4;
736                         break;
737                     case 'u':
738                         b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
739                         i_len   = 4;
740                         break;
741                     case 'f':
742                         b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
743                         i_len   = 7;
744                         break;
745                     }
746                 }
747                 if( ! b_match )
748                 {
749                     /* Not well formed -- kill everything */
750                     free( psz_html_start );
751                     psz_html_start = NULL;
752                     break;
753                 }
754                 *psz_lastTag = '\0';
755                 strncpy( psz_html, psz_subtitle, i_len );
756                 psz_html += i_len;
757                 psz_subtitle += i_len;
758             }
759             else
760             {
761                 psz_subtitle += strcspn( psz_subtitle, ">" );
762             }
763         }
764         else if( *psz_subtitle == '&' )
765         {
766             if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
767             {
768                 HtmlCopy( &psz_html, &psz_subtitle, "&lt;" );
769             }
770             else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
771             {
772                 HtmlCopy( &psz_html, &psz_subtitle, "&gt;" );
773             }
774             else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
775             {
776                 HtmlCopy( &psz_html, &psz_subtitle, "&amp;" );
777             }
778             else
779             {
780                 HtmlPut( &psz_html, "&amp;" );
781                 psz_subtitle++;
782             }
783         }
784         else
785         {
786             *psz_html = *psz_subtitle;
787             if( psz_html > psz_html_start )
788             {
789                 /* Check for double whitespace */
790                 if( ( *psz_html == ' '  || *psz_html == '\t' ) &&
791                     ( *(psz_html-1) == ' ' || *(psz_html-1) == '\t' ) )
792                 {
793                     HtmlPut( &psz_html, NO_BREAKING_SPACE );
794                     psz_html--;
795                 }
796             }
797             psz_html++;
798             psz_subtitle++;
799         }
800
801         if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 50 )
802         {
803             int i_len = psz_html - psz_html_start;
804
805             i_buf_size += 200;
806             psz_html_start = realloc( psz_html_start, i_buf_size );
807             psz_html = psz_html_start + i_len;
808             *psz_html = '\0';
809         }
810     }
811     strcpy( psz_html, "</text>" );
812     psz_html += 7;
813
814     if( psz_tag[ 0 ] != '\0' )
815     {
816         /* Not well formed -- kill everything */
817         free( psz_html_start );
818         psz_html_start = NULL;
819     }
820     else if( psz_html_start )
821     {
822         /* Shrink the memory requirements */
823         psz_html_start = realloc( psz_html_start,  psz_html - psz_html_start + 1 );
824     }
825     free( psz_tag );
826
827     return psz_html_start;
828 }
829