]> git.sesse.net Git - vlc/blob - modules/codec/subtitles/subsdec.c
f32849f4ab113b3adfd64334e1cbeb2392a66762
[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     "",
54     "UTF-8",
55     "UTF-16",
56     "UTF-16BE",
57     "UTF-16LE",
58     "GB18030",
59     "ISO-8859-15",
60     "Windows-1252",
61     "ISO-8859-2",
62     "Windows-1250",
63     "ISO-8859-3",
64     "ISO-8859-10",
65     "Windows-1251",
66     "KOI8-R",
67     "KOI8-U",
68     "ISO-8859-6",
69     "Windows-1256",
70     "ISO-8859-7",
71     "Windows-1253",
72     "ISO-8859-8",
73     "Windows-1255",
74     "ISO-8859-9",
75     "Windows-1254",
76     "ISO-8859-11",
77     "Windows-874",
78     "ISO-8859-13",
79     "Windows-1257",
80     "ISO-8859-14",
81     "ISO-8859-16",
82     "ISO-2022-CN-EXT",
83     "EUC-CN",
84     "ISO-2022-JP-2",
85     "EUC-JP",
86     "Shift_JIS",
87     "CP949",
88     "ISO-2022-KR",
89     "Big5",
90     "ISO-2022-TW",
91     "Big5-HKSCS",
92     "VISCII",
93     "Windows-1258",
94 };
95
96 static const char *const ppsz_encoding_names[] = {
97     /* xgettext:
98       The character encoding name in parenthesis corresponds to that used for
99       the GetACP translation. "Windows-1252" applies to Western European
100       languages using the Latin alphabet. */
101     N_("Default (Windows-1252)"),
102     N_("Universal (UTF-8)"),
103     N_("Universal (UTF-16)"),
104     N_("Universal (big endian UTF-16)"),
105     N_("Universal (little endian UTF-16)"),
106     N_("Universal, Chinese (GB18030)"),
107
108   /* ISO 8859 and the likes */
109     /* 1 */
110     N_("Western European (Latin-9)"), /* mostly superset of Latin-1 */
111     N_("Western European (Windows-1252)"),
112     /* 2 */
113     N_("Eastern European (Latin-2)"),
114     N_("Eastern European (Windows-1250)"),
115     /* 3 */
116     N_("Esperanto (Latin-3)"),
117     /* 4 */
118     N_("Nordic (Latin-6)"), /* Latin 6 supersedes Latin 4 */
119     /* 5 */
120     N_("Cyrillic (Windows-1251)"), /* ISO 8859-5 is not practically used */
121     N_("Russian (KOI8-R)"),
122     N_("Ukrainian (KOI8-U)"),
123     /* 6 */
124     N_("Arabic (ISO 8859-6)"),
125     N_("Arabic (Windows-1256)"),
126     /* 7 */
127     N_("Greek (ISO 8859-7)"),
128     N_("Greek (Windows-1253)"),
129     /* 8 */
130     N_("Hebrew (ISO 8859-8)"),
131     N_("Hebrew (Windows-1255)"),
132     /* 9 */
133     N_("Turkish (ISO 8859-9)"),
134     N_("Turkish (Windows-1254)"),
135     /* 10 -> 4 */
136     /* 11 */
137     N_("Thai (TIS 620-2533/ISO 8859-11)"),
138     N_("Thai (Windows-874)"),
139     /* 13 */
140     N_("Baltic (Latin-7)"),
141     N_("Baltic (Windows-1257)"),
142     /* 12 -> /dev/null */
143     /* 14 */
144     N_("Celtic (Latin-8)"),
145     /* 15 -> 1 */
146     /* 16 */
147     N_("South-Eastern European (Latin-10)"),
148   /* CJK families */
149     N_("Simplified Chinese (ISO-2022-CN-EXT)"),
150     N_("Simplified Chinese Unix (EUC-CN)"),
151     N_("Japanese (7-bits JIS/ISO-2022-JP-2)"),
152     N_("Japanese Unix (EUC-JP)"),
153     N_("Japanese (Shift JIS)"),
154     N_("Korean (EUC-KR/CP949)"),
155     N_("Korean (ISO-2022-KR)"),
156     N_("Traditional Chinese (Big5)"),
157     N_("Traditional Chinese Unix (EUC-TW)"),
158     N_("Hong-Kong Supplementary (HKSCS)"),
159   /* Other */
160     N_("Vietnamese (VISCII)"),
161     N_("Vietnamese (Windows-1258)"),
162 };
163 /*
164 SSA supports charset selection.
165 The following known charsets are used:
166
167 0 = Ansi - Western European
168 1 = default
169 2 = symbol
170 3 = invalid
171 77 = Mac
172 128 = Japanese (Shift JIS)
173 129 = Hangul
174 130 = Johab
175 134 = GB2312 Simplified Chinese
176 136 = Big5 Traditional Chinese
177 161 = Greek
178 162 = Turkish
179 163 = Vietnamese
180 177 = Hebrew
181 178 = Arabic
182 186 = Baltic
183 204 = Russian (Cyrillic)
184 222 = Thai
185 238 = Eastern European
186 254 = PC 437
187 */
188
189 static const int  pi_justification[] = { 0, 1, 2 };
190 static const char *const ppsz_justification_text[] = {
191     N_("Center"),N_("Left"),N_("Right")};
192
193 #define ENCODING_TEXT N_("Subtitles text encoding")
194 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
195 #define ALIGN_TEXT N_("Subtitles justification")
196 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
197 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
198 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
199             "UTF-8 encoding within subtitles files.")
200 #define FORMAT_TEXT N_("Formatted Subtitles")
201 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
202  "VLC partly implements this, but you can choose to disable all formatting.")
203
204
205 vlc_module_begin ()
206     set_shortname( N_("Subtitles"))
207     set_description( N_("Text subtitles decoder") )
208     set_capability( "decoder", 50 )
209     set_callbacks( OpenDecoder, CloseDecoder )
210     set_category( CAT_INPUT )
211     set_subcategory( SUBCAT_INPUT_SCODEC )
212
213     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
214                  false )
215         change_integer_list( pi_justification, ppsz_justification_text, NULL )
216     add_string( "subsdec-encoding", "", NULL,
217                 ENCODING_TEXT, ENCODING_LONGTEXT, false )
218         change_string_list( ppsz_encodings, ppsz_encoding_names, 0 )
219     add_bool( "subsdec-autodetect-utf8", true, NULL,
220               AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, false )
221     add_bool( "subsdec-formatted", true, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
222                  false )
223 vlc_module_end ()
224
225 /*****************************************************************************
226  * OpenDecoder: probe the decoder and return score
227  *****************************************************************************
228  * Tries to launch a decoder and return score so that the interface is able
229  * to chose.
230  *****************************************************************************/
231 static int OpenDecoder( vlc_object_t *p_this )
232 {
233     decoder_t     *p_dec = (decoder_t*)p_this;
234     decoder_sys_t *p_sys;
235
236     switch( p_dec->fmt_in.i_codec )
237     {
238         case VLC_CODEC_SUBT:
239         case VLC_CODEC_SSA:
240         case VLC_CODEC_ITU_T140:
241             break;
242         default:
243             return VLC_EGENERIC;
244     }
245
246     p_dec->pf_decode_sub = DecodeBlock;
247     p_dec->fmt_out.i_cat = SPU_ES;
248     p_dec->fmt_out.i_codec = 0;
249
250     /* Allocate the memory needed to store the decoder's structure */
251     p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
252     if( p_sys == NULL )
253         return VLC_ENOMEM;
254
255     /* init of p_sys */
256     p_sys->i_align = 0;
257     p_sys->iconv_handle = (vlc_iconv_t)-1;
258     p_sys->b_autodetect_utf8 = false;
259     p_sys->b_ass = false;
260     p_sys->i_original_height = -1;
261     p_sys->i_original_width = -1;
262     TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
263     TAB_INIT( p_sys->i_images, p_sys->pp_images );
264
265     char *psz_charset = NULL;
266
267     /* First try demux-specified encoding */
268     if( p_dec->fmt_in.i_codec == VLC_CODEC_ITU_T140 )
269         psz_charset = strdup( "UTF-8" ); /* IUT T.140 is always using UTF-8 */
270     else
271     if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
272     {
273         psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
274         msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
275                  p_dec->fmt_in.subs.psz_encoding ?
276                  p_dec->fmt_in.subs.psz_encoding : "not specified");
277     }
278
279     /* Second, try configured encoding */
280     if (psz_charset == NULL)
281     {
282         psz_charset = var_CreateGetNonEmptyString (p_dec, "subsdec-encoding");
283         msg_Dbg (p_dec, "trying configured character encoding: %s",
284                  psz_charset ? psz_charset : "not specified");
285     }
286
287     /* Third, try "local" encoding with optional UTF-8 autodetection */
288     if (psz_charset == NULL)
289     {
290         /* xgettext:
291            The Windows ANSI code page most commonly used for this language.
292            VLC uses this as a guess of the subtitle files character set
293            (if UTF-8 and UTF-16 autodetection fails).
294            Western European languages normally use "CP1252", which is a
295            Microsoft-variant of ISO 8859-1. That suits the Latin alphabet.
296            Other scripts use other code pages.
297
298            This MUST be a valid iconv character set. If unsure, please refer
299            the VideoLAN translators mailing list. */
300         const char *acp = vlc_pgettext("GetACP", "CP1252");
301
302         psz_charset = strdup (acp);
303         msg_Dbg (p_dec, "trying default character encoding: %s",
304                  psz_charset ? psz_charset : "not specified");
305
306         if (var_CreateGetBool (p_dec, "subsdec-autodetect-utf8"))
307         {
308             msg_Dbg (p_dec, "using automatic UTF-8 detection");
309             p_sys->b_autodetect_utf8 = true;
310         }
311     }
312
313     /* Forth, don't do character decoding, i.e. assume UTF-8 */
314     if (psz_charset == NULL)
315     {
316         psz_charset = strdup ("UTF-8");
317         msg_Dbg (p_dec, "using UTF-8 character encoding" );
318     }
319
320     if ((psz_charset != NULL)
321      && strcasecmp (psz_charset, "UTF-8")
322      && strcasecmp (psz_charset, "utf8"))
323     {
324         p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
325         if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
326             msg_Err (p_dec, "cannot convert from %s: %m", psz_charset);
327     }
328     free (psz_charset);
329
330     p_sys->i_align = var_CreateGetInteger( p_dec, "subsdec-align" );
331
332     if( p_dec->fmt_in.i_codec == VLC_CODEC_SSA
333      && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
334     {
335         if( p_dec->fmt_in.i_extra > 0 )
336             ParseSSAHeader( p_dec );
337     }
338
339     return VLC_SUCCESS;
340 }
341
342 /****************************************************************************
343  * DecodeBlock: the whole thing
344  ****************************************************************************
345  * This function must be fed with complete subtitles units.
346  ****************************************************************************/
347 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
348 {
349     subpicture_t *p_spu;
350     block_t *p_block;
351
352     if( !pp_block || *pp_block == NULL )
353         return NULL;
354
355     p_block = *pp_block;
356     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
357     {
358         block_Release( p_block );
359         return NULL;
360     }
361
362     p_spu = ParseText( p_dec, p_block );
363
364     block_Release( p_block );
365     *pp_block = NULL;
366
367     return p_spu;
368 }
369
370 /*****************************************************************************
371  * CloseDecoder: clean up the decoder
372  *****************************************************************************/
373 static void CloseDecoder( vlc_object_t *p_this )
374 {
375     decoder_t *p_dec = (decoder_t *)p_this;
376     decoder_sys_t *p_sys = p_dec->p_sys;
377
378     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
379         vlc_iconv_close( p_sys->iconv_handle );
380
381     if( p_sys->pp_ssa_styles )
382     {
383         int i;
384         for( i = 0; i < p_sys->i_ssa_styles; i++ )
385         {
386             if( !p_sys->pp_ssa_styles[i] )
387                 continue;
388
389             free( p_sys->pp_ssa_styles[i]->psz_stylename );
390             free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
391             free( p_sys->pp_ssa_styles[i] );
392         }
393         TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
394     }
395     if( p_sys->pp_images )
396     {
397         int i;
398         for( i = 0; i < p_sys->i_images; i++ )
399         {
400             if( !p_sys->pp_images[i] )
401                 continue;
402
403             if( p_sys->pp_images[i]->p_pic )
404                 picture_Release( p_sys->pp_images[i]->p_pic );
405             free( p_sys->pp_images[i]->psz_filename );
406
407             free( p_sys->pp_images[i] );
408         }
409         TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
410     }
411
412     free( p_sys );
413 }
414
415 /*****************************************************************************
416  * ParseText: parse an text subtitle packet and send it to the video output
417  *****************************************************************************/
418 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
419 {
420     decoder_sys_t *p_sys = p_dec->p_sys;
421     subpicture_t *p_spu = NULL;
422     char *psz_subtitle = NULL;
423     video_format_t fmt;
424
425     /* We cannot display a subpicture with no date */
426     if( p_block->i_pts <= VLC_TS_INVALID )
427     {
428         msg_Warn( p_dec, "subtitle without a date" );
429         return NULL;
430     }
431
432     /* Check validity of packet data */
433     /* An "empty" line containing only \0 can be used to force
434        and ephemer picture from the screen */
435     if( p_block->i_buffer < 1 )
436     {
437         msg_Warn( p_dec, "no subtitle data" );
438         return NULL;
439     }
440
441     /* Should be resiliant against bad subtitles */
442     psz_subtitle = malloc( p_block->i_buffer + 1 );
443     if( psz_subtitle == NULL )
444         return NULL;
445     memcpy( psz_subtitle, p_block->p_buffer, p_block->i_buffer );
446     psz_subtitle[p_block->i_buffer] = '\0';
447
448     if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
449     {
450         if (EnsureUTF8( psz_subtitle ) == NULL)
451         {
452             msg_Err( p_dec, "failed to convert subtitle encoding.\n"
453                      "Try manually setting a character-encoding "
454                      "before you open the file." );
455         }
456     }
457     else
458     {
459
460         if( p_sys->b_autodetect_utf8 )
461         {
462             if( IsUTF8( psz_subtitle ) == NULL )
463             {
464                 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
465                          "disabling UTF-8 subtitles autodetection" );
466                 p_sys->b_autodetect_utf8 = false;
467             }
468         }
469
470         if( !p_sys->b_autodetect_utf8 )
471         {
472             size_t inbytes_left = strlen( psz_subtitle );
473             size_t outbytes_left = 6 * inbytes_left;
474             char *psz_new_subtitle = xmalloc( outbytes_left + 1 );
475             char *psz_convert_buffer_out = psz_new_subtitle;
476             const char *psz_convert_buffer_in = psz_subtitle;
477
478             size_t ret = vlc_iconv( p_sys->iconv_handle,
479                                     &psz_convert_buffer_in, &inbytes_left,
480                                     &psz_convert_buffer_out, &outbytes_left );
481
482             *psz_convert_buffer_out++ = '\0';
483             free( psz_subtitle );
484
485             if( ( ret == (size_t)(-1) ) || inbytes_left )
486             {
487                 free( psz_new_subtitle );
488                 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
489                         "Try manually setting a character-encoding "
490                                 "before you open the file." );
491                 return NULL;
492             }
493
494             psz_subtitle = realloc( psz_new_subtitle,
495                                     psz_convert_buffer_out - psz_new_subtitle );
496             if( !psz_subtitle )
497                 psz_subtitle = psz_new_subtitle;
498         }
499     }
500
501     /* Create the subpicture unit */
502     p_spu = decoder_NewSubpicture( p_dec, NULL );
503     if( !p_spu )
504     {
505         msg_Warn( p_dec, "can't get spu buffer" );
506         free( psz_subtitle );
507         return NULL;
508     }
509
510     /* Create a new subpicture region */
511     memset( &fmt, 0, sizeof(video_format_t) );
512     fmt.i_chroma = VLC_CODEC_TEXT;
513     fmt.i_width = fmt.i_height = 0;
514     fmt.i_x_offset = fmt.i_y_offset = 0;
515     p_spu->p_region = subpicture_region_New( &fmt );
516     if( !p_spu->p_region )
517     {
518         msg_Err( p_dec, "cannot allocate SPU region" );
519         free( psz_subtitle );
520         decoder_DeleteSubpicture( p_dec, p_spu );
521         return NULL;
522     }
523
524     /* Decode and format the subpicture unit */
525     if( p_dec->fmt_in.i_codec != VLC_CODEC_SSA )
526     {
527         /* Normal text subs, easy markup */
528         p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
529         p_spu->p_region->i_x = p_sys->i_align ? 20 : 0;
530         p_spu->p_region->i_y = 10;
531
532         /* Remove formatting from string */
533
534         p_spu->p_region->psz_text = StripTags( psz_subtitle );
535         if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
536         {
537             p_spu->p_region->psz_html = CreateHtmlSubtitle( &p_spu->p_region->i_align, psz_subtitle );
538         }
539
540         p_spu->i_start = p_block->i_pts;
541         p_spu->i_stop = p_block->i_pts + p_block->i_length;
542         p_spu->b_ephemer = (p_block->i_length == 0);
543         p_spu->b_absolute = false;
544     }
545     else
546     {
547         /* Decode SSA/USF strings */
548         ParseSSAString( p_dec, psz_subtitle, p_spu );
549
550         p_spu->i_start = p_block->i_pts;
551         p_spu->i_stop = p_block->i_pts + p_block->i_length;
552         p_spu->b_ephemer = (p_block->i_length == 0);
553         p_spu->b_absolute = false;
554         p_spu->i_original_picture_width = p_sys->i_original_width;
555         p_spu->i_original_picture_height = p_sys->i_original_height;
556     }
557     free( psz_subtitle );
558
559     return p_spu;
560 }
561
562 char* GotoNextLine( char *psz_text )
563 {
564     char *p_newline = psz_text;
565
566     while( p_newline[0] != '\0' )
567     {
568         if( p_newline[0] == '\n' || p_newline[0] == '\r' )
569         {
570             p_newline++;
571             while( p_newline[0] == '\n' || p_newline[0] == '\r' )
572                 p_newline++;
573             break;
574         }
575         else p_newline++;
576     }
577     return p_newline;
578 }
579
580 /* Function now handles tags with attribute values, and tries
581  * to deal with &' commands too. It no longer modifies the string
582  * in place, so that the original text can be reused
583  */
584 static char *StripTags( char *psz_subtitle )
585 {
586     char *psz_text_start;
587     char *psz_text;
588
589     psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
590     if( !psz_text_start )
591         return NULL;
592
593     while( *psz_subtitle )
594     {
595         if( *psz_subtitle == '<' )
596         {
597             if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
598                 *psz_text++ = '\n';
599
600             psz_subtitle += strcspn( psz_subtitle, ">" );
601         }
602         else if( *psz_subtitle == '&' )
603         {
604             if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
605             {
606                 *psz_text++ = '<';
607                 psz_subtitle += strcspn( psz_subtitle, ";" );
608             }
609             else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
610             {
611                 *psz_text++ = '>';
612                 psz_subtitle += strcspn( psz_subtitle, ";" );
613             }
614             else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
615             {
616                 *psz_text++ = '&';
617                 psz_subtitle += strcspn( psz_subtitle, ";" );
618             }
619             else if( !strncasecmp( psz_subtitle, "&quot;", 6 ))
620             {
621                 *psz_text++ = '\"';
622                 psz_subtitle += strcspn( psz_subtitle, ";" );
623             }
624             else
625             {
626                 /* Assume it is just a normal ampersand */
627                 *psz_text++ = '&';
628             }
629         }
630         else
631         {
632             *psz_text++ = *psz_subtitle;
633         }
634
635         psz_subtitle++;
636     }
637     *psz_text = '\0';
638     char *psz = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
639     if( psz ) psz_text_start = psz;
640
641     return psz_text_start;
642 }
643
644 /* Try to respect any style tags present in the subtitle string. The main
645  * problem here is a lack of adequate specs for the subtitle formats.
646  * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
647  * SAMI has a detailed spec, but extensive rework is needed in the demux
648  * code to prevent all this style information being excised, as it presently
649  * does.
650  * That leaves the others - none of which were (I guess) originally intended
651  * to be carrying style information. Over time people have used them that way.
652  * In the absence of specifications from which to work, the tags supported
653  * have been restricted to the simple set permitted by the USF DTD, ie. :
654  *  Basic: <br>, <i>, <b>, <u>, <s>
655  *  Extended: <font>
656  *    Attributes: face
657  *                family
658  *                size
659  *                color
660  *                outline-color
661  *                shadow-color
662  *                outline-level
663  *                shadow-level
664  *                back-color
665  *                alpha
666  * There is also the further restriction that the subtitle be well-formed
667  * as an XML entity, ie. the HTML sentence:
668  *        <b><i>Bold and Italics</b></i>
669  * doesn't qualify because the tags aren't nested one inside the other.
670  * <text> tags are automatically added to the output to ensure
671  * well-formedness.
672  * If the text doesn't qualify for any reason, a NULL string is
673  * returned, and the rendering engine will fall back to the
674  * plain text version of the subtitle.
675  */
676 static void HtmlNPut( char **ppsz_html, const char *psz_text, int i_max )
677 {
678     const int i_len = strlen(psz_text);
679
680     strncpy( *ppsz_html, psz_text, i_max );
681     *ppsz_html += __MIN(i_max,i_len);
682 }
683
684 static void HtmlPut( char **ppsz_html, const char *psz_text )
685 {
686     strcpy( *ppsz_html, psz_text );
687     *ppsz_html += strlen(psz_text);
688 }
689 static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_text )
690 {
691     HtmlPut( ppsz_html, psz_text );
692     *ppsz_subtitle += strlen(psz_text);
693 }
694
695 static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
696 {
697     /* */
698     char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
699     if( !psz_tag )
700         return NULL;
701     psz_tag[ 0 ] = '\0';
702
703     /* */
704     //Oo + 100 ???
705     size_t i_buf_size = strlen( psz_subtitle ) + 100;
706     char   *psz_html_start = malloc( i_buf_size );
707     char   *psz_html = psz_html_start;
708     if( psz_html_start == NULL )
709     {
710         free( psz_tag );
711         return NULL;
712     }
713     psz_html[0] = '\0';
714
715     bool b_has_align = false;
716
717     HtmlPut( &psz_html, "<text>" );
718
719     /* */
720     while( *psz_subtitle )
721     {
722         if( *psz_subtitle == '\n' )
723         {
724             HtmlPut( &psz_html, "<br/>" );
725             psz_subtitle++;
726         }
727         else if( *psz_subtitle == '<' )
728         {
729             if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
730             {
731                 HtmlCopy( &psz_html, &psz_subtitle, "<br/>" );
732             }
733             else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
734             {
735                 HtmlCopy( &psz_html, &psz_subtitle, "<b>" );
736                 strcat( psz_tag, "b" );
737             }
738             else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
739             {
740                 HtmlCopy( &psz_html, &psz_subtitle, "<i>" );
741                 strcat( psz_tag, "i" );
742             }
743             else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
744             {
745                 HtmlCopy( &psz_html, &psz_subtitle, "<u>" );
746                 strcat( psz_tag, "u" );
747             }
748             else if( !strncasecmp( psz_subtitle, "<s>", 3 ) )
749             {
750                 HtmlCopy( &psz_html, &psz_subtitle, "<s>" );
751                 strcat( psz_tag, "s" );
752             }
753             else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
754             {
755                 const char *psz_attribs[] = { "face=", "family=", "size=",
756                         "color=", "outline-color=", "shadow-color=",
757                         "outline-level=", "shadow-level=", "back-color=",
758                         "alpha=", NULL };
759
760                 HtmlCopy( &psz_html, &psz_subtitle, "<font " );
761                 strcat( psz_tag, "f" );
762
763                 while( *psz_subtitle != '>' )
764                 {
765                     int  k;
766
767                     for( k=0; psz_attribs[ k ]; k++ )
768                     {
769                         int i_len = strlen( psz_attribs[ k ] );
770
771                         if( !strncasecmp( psz_subtitle, psz_attribs[k], i_len ) )
772                         {
773                             /* */
774                             HtmlPut( &psz_html, psz_attribs[k] );
775                             psz_subtitle += i_len;
776
777                             /* */
778                             if( *psz_subtitle == '"' )
779                             {
780                                 psz_subtitle++;
781                                 i_len = strcspn( psz_subtitle, "\"" );
782                             }
783                             else
784                             {
785                                 i_len = strcspn( psz_subtitle, " \t>" );
786                             }
787                             HtmlPut( &psz_html, "\"" );
788                             if( !strcmp( psz_attribs[ k ], "color=" ) && *psz_subtitle >= '0' && *psz_subtitle <= '9' )
789                                 HtmlPut( &psz_html, "#" );
790                             HtmlNPut( &psz_html, psz_subtitle, i_len );
791                             HtmlPut( &psz_html, "\"" );
792
793                             psz_subtitle += i_len;
794                             if( *psz_subtitle == '\"' )
795                                 psz_subtitle++;
796                             break;
797                         }
798                     }
799                     if( psz_attribs[ k ] == NULL )
800                     {
801                         /* Jump over unrecognised tag */
802                         int i_len = strcspn( psz_subtitle, "\"" );
803                         if( psz_subtitle[i_len] == '\"' )
804                         {
805                             i_len += 1 + strcspn( &psz_subtitle[i_len + 1], "\"" );
806                             if( psz_subtitle[i_len] == '\"' )
807                                 i_len++;
808                         }
809                         psz_subtitle += i_len;
810                     }
811                     while (*psz_subtitle == ' ')
812                         *psz_html++ = *psz_subtitle++;
813                 }
814                 *psz_html++ = *psz_subtitle++;
815             }
816             else if( !strncmp( psz_subtitle, "</", 2 ))
817             {
818                 bool   b_match     = false;
819                 bool   b_ignore    = false;
820                 int    i_len       = strlen( psz_tag ) - 1;
821                 char  *psz_lastTag = NULL;
822
823                 if( i_len >= 0 )
824                 {
825                     psz_lastTag = psz_tag + i_len;
826                     i_len = 0;
827
828                     switch( *psz_lastTag )
829                     {
830                     case 'b':
831                         b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
832                         i_len   = 4;
833                         break;
834                     case 'i':
835                         b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
836                         i_len   = 4;
837                         break;
838                     case 'u':
839                         b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
840                         i_len   = 4;
841                         break;
842                     case 's':
843                         b_match = !strncasecmp( psz_subtitle, "</s>", 4 );
844                         i_len   = 4;
845                         break;
846                     case 'f':
847                         b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
848                         i_len   = 7;
849                         break;
850                     case 'I':
851                         i_len = strcspn( psz_subtitle, ">" );
852                         b_match = psz_subtitle[i_len] == '>';
853                         b_ignore = true;
854                         if( b_match )
855                             i_len++;
856                         break;
857                     }
858                 }
859                 if( !b_match )
860                 {
861                     /* Not well formed -- kill everything */
862                     free( psz_html_start );
863                     psz_html_start = NULL;
864                     break;
865                 }
866                 *psz_lastTag = '\0';
867                 if( !b_ignore )
868                     HtmlNPut( &psz_html, psz_subtitle, i_len );
869
870                 psz_subtitle += i_len;
871             }
872             else if( ( psz_subtitle[1] < 'a' || psz_subtitle[1] > 'z' ) &&
873                      ( psz_subtitle[1] < 'A' || psz_subtitle[1] > 'Z' ) )
874             {
875                 /* We have a single < */
876                 HtmlPut( &psz_html, "&lt;" );
877                 psz_subtitle++;
878             }
879             else
880             {
881                 /* We have an unknown tag or a single < */
882
883                 /* Search for the next tag or end of tag or end of string */
884                 char *psz_stop = psz_subtitle + 1 + strcspn( &psz_subtitle[1], "<>" );
885                 char *psz_closing = strstr( psz_subtitle, "/>" );
886
887                 if( psz_closing && psz_closing < psz_stop )
888                 {
889                     /* We have a self closed tag, remove it */
890                     psz_subtitle = &psz_closing[2];
891                 }
892                 else if( *psz_stop == '>' )
893                 {
894                     char psz_match[256];
895
896                     snprintf( psz_match, sizeof(psz_match), "</%s", &psz_subtitle[1] );
897                     psz_match[strcspn( psz_match, " \t>" )] = '\0';
898
899                     if( strstr( psz_subtitle, psz_match ) )
900                     {
901                         /* We have the closing tag, ignore it TODO */
902                         psz_subtitle = &psz_stop[1];
903                         strcat( psz_tag, "I" );
904                     }
905                     else
906                     {
907                         int i_len = psz_stop + 1 - psz_subtitle;
908
909                         /* Copy the whole data */
910                         for( ; i_len > 0; i_len--, psz_subtitle++ )
911                         {
912                             if( *psz_subtitle == '<' )
913                                 HtmlPut( &psz_html, "&lt;" );
914                             else if( *psz_subtitle == '>' )
915                                 HtmlPut( &psz_html, "&gt;" );
916                             else
917                                 *psz_html++ = *psz_subtitle;
918                         }
919                     }
920                 }
921                 else
922                 {
923                     /* We have a single < */
924                     HtmlPut( &psz_html, "&lt;" );
925                     psz_subtitle++;
926                 }
927             }
928         }
929         else if( *psz_subtitle == '&' )
930         {
931             if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
932             {
933                 HtmlCopy( &psz_html, &psz_subtitle, "&lt;" );
934             }
935             else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
936             {
937                 HtmlCopy( &psz_html, &psz_subtitle, "&gt;" );
938             }
939             else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
940             {
941                 HtmlCopy( &psz_html, &psz_subtitle, "&amp;" );
942             }
943             else
944             {
945                 HtmlPut( &psz_html, "&amp;" );
946                 psz_subtitle++;
947             }
948         }
949         else if( *psz_subtitle == '>' )
950         {
951             HtmlPut( &psz_html, "&gt;" );
952             psz_subtitle++;
953         }
954         else if( psz_subtitle[0] == '{' && psz_subtitle[1] == '\\' &&
955                  strchr( psz_subtitle, '}' ) )
956         {
957             /* Check for forced alignment */
958             if( !b_has_align &&
959                 !strncmp( psz_subtitle, "{\\an", 4 ) && psz_subtitle[4] >= '1' && psz_subtitle[4] <= '9' && psz_subtitle[5] == '}' )
960             {
961                 static const int pi_vertical[3] = { SUBPICTURE_ALIGN_BOTTOM, 0, SUBPICTURE_ALIGN_TOP };
962                 static const int pi_horizontal[3] = { SUBPICTURE_ALIGN_LEFT, 0, SUBPICTURE_ALIGN_RIGHT };
963                 const int i_id = psz_subtitle[4] - '1';
964
965                 b_has_align = true;
966                 *pi_align = pi_vertical[i_id/3] | pi_horizontal[i_id%3];
967             }
968             /* TODO fr -> rotation */
969
970             /* Hide {\stupidity} */
971             psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
972         }
973         else if( psz_subtitle[0] == '{' && psz_subtitle[1] == 'Y'
974                 && psz_subtitle[2] == ':' && strchr( psz_subtitle, '}' ) )
975         {
976             /* Hide {Y:stupidity} */
977             psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
978         }
979         else if( psz_subtitle[0] == '\\' && psz_subtitle[1] )
980         {
981             if( psz_subtitle[1] == 'N' || psz_subtitle[1] == 'n' )
982             {
983                 HtmlPut( &psz_html, "<br/>" );
984                 psz_subtitle += 2;
985             }
986             else if( psz_subtitle[1] == 'h' )
987             {
988                 /* Non breakable space */
989                 HtmlPut( &psz_html, NO_BREAKING_SPACE );
990                 psz_subtitle += 2;
991             }
992             else
993             {
994                 HtmlPut( &psz_html, "\\" );
995                 psz_subtitle++;
996             }
997         }
998         else
999         {
1000             *psz_html = *psz_subtitle;
1001             if( psz_html > psz_html_start )
1002             {
1003                 /* Check for double whitespace */
1004                 if( ( *psz_html == ' '  || *psz_html == '\t' ) &&
1005                     ( *(psz_html-1) == ' ' || *(psz_html-1) == '\t' ) )
1006                 {
1007                     HtmlPut( &psz_html, NO_BREAKING_SPACE );
1008                     psz_html--;
1009                 }
1010             }
1011             psz_html++;
1012             psz_subtitle++;
1013         }
1014
1015         if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 50 )
1016         {
1017             const int i_len = psz_html - psz_html_start;
1018
1019             i_buf_size += 200;
1020             char *psz_new = realloc( psz_html_start, i_buf_size );
1021             if( !psz_new )
1022                 break;
1023             psz_html_start = psz_new;
1024             psz_html = &psz_new[i_len];
1025         }
1026     }
1027     if( psz_html_start )
1028     {
1029         static const char *psz_text_close = "</text>";
1030         static const char *psz_tag_long = "/font>";
1031
1032         /* Realloc for closing tags and shrink memory */
1033         const size_t i_length = (size_t)( psz_html - psz_html_start );
1034
1035         const size_t i_size = i_length + strlen(psz_tag_long) * strlen(psz_tag) + strlen(psz_text_close) + 1;
1036         char *psz_new = realloc( psz_html_start, i_size );
1037         if( psz_new )
1038         {
1039             psz_html_start = psz_new;
1040             psz_html = &psz_new[i_length];
1041
1042             /* Close not well formed subtitle */
1043             while( *psz_tag )
1044             {
1045                 /* */
1046                 char *psz_last = &psz_tag[strlen(psz_tag)-1];
1047                 switch( *psz_last )
1048                 {
1049                 case 'b':
1050                     HtmlPut( &psz_html, "</b>" );
1051                     break;
1052                 case 'i':
1053                     HtmlPut( &psz_html, "</i>" );
1054                     break;
1055                 case 'u':
1056                     HtmlPut( &psz_html, "</u>" );
1057                     break;
1058                 case 's':
1059                     HtmlPut( &psz_html, "</s>" );
1060                     break;
1061                 case 'f':
1062                     HtmlPut( &psz_html, "/font>" );
1063                     break;
1064                 case 'I':
1065                     break;
1066                 }
1067
1068                 *psz_last = '\0';
1069             }
1070             HtmlPut( &psz_html, psz_text_close );
1071         }
1072     }
1073     free( psz_tag );
1074
1075     return psz_html_start;
1076 }
1077