demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / codec / subsdec.c
blob592a10a74a410a3a711878d4a35279c0929b1d98
1 /*****************************************************************************
2 * subsdec.c : text subtitle decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2006 VLC authors and VideoLAN
5 * $Id$
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>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <limits.h>
35 #include <errno.h>
36 #include <ctype.h>
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_codec.h>
41 #include <vlc_charset.h>
42 #include <vlc_xml.h>
44 #include "substext.h"
46 /*****************************************************************************
47 * Module descriptor.
48 *****************************************************************************/
49 static const char *const ppsz_encodings[] = {
50 "",
51 "system",
52 "UTF-8",
53 "UTF-16",
54 "UTF-16BE",
55 "UTF-16LE",
56 "GB18030",
57 "ISO-8859-15",
58 "Windows-1252",
59 "IBM850",
60 "ISO-8859-2",
61 "Windows-1250",
62 "ISO-8859-3",
63 "ISO-8859-10",
64 "Windows-1251",
65 "KOI8-R",
66 "KOI8-U",
67 "ISO-8859-6",
68 "Windows-1256",
69 "ISO-8859-7",
70 "Windows-1253",
71 "ISO-8859-8",
72 "Windows-1255",
73 "ISO-8859-9",
74 "Windows-1254",
75 "ISO-8859-11",
76 "Windows-874",
77 "ISO-8859-13",
78 "Windows-1257",
79 "ISO-8859-14",
80 "ISO-8859-16",
81 "ISO-2022-CN-EXT",
82 "EUC-CN",
83 "ISO-2022-JP-2",
84 "EUC-JP",
85 "Shift_JIS",
86 "CP949",
87 "ISO-2022-KR",
88 "Big5",
89 "ISO-2022-TW",
90 "Big5-HKSCS",
91 "VISCII",
92 "Windows-1258",
95 static const char *const ppsz_encoding_names[] = {
96 /* xgettext:
97 The character encoding name in parenthesis corresponds to that used for
98 the GetACP translation. "Windows-1252" applies to Western European
99 languages using the Latin alphabet. */
100 N_("Default (Windows-1252)"),
101 N_("System codeset"),
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)"),
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 N_("Western European (IBM 00850)"),
113 /* 2 */
114 N_("Eastern European (Latin-2)"),
115 N_("Eastern European (Windows-1250)"),
116 /* 3 */
117 N_("Esperanto (Latin-3)"),
118 /* 4 */
119 N_("Nordic (Latin-6)"), /* Latin 6 supersedes Latin 4 */
120 /* 5 */
121 N_("Cyrillic (Windows-1251)"), /* ISO 8859-5 is not practically used */
122 N_("Russian (KOI8-R)"),
123 N_("Ukrainian (KOI8-U)"),
124 /* 6 */
125 N_("Arabic (ISO 8859-6)"),
126 N_("Arabic (Windows-1256)"),
127 /* 7 */
128 N_("Greek (ISO 8859-7)"),
129 N_("Greek (Windows-1253)"),
130 /* 8 */
131 N_("Hebrew (ISO 8859-8)"),
132 N_("Hebrew (Windows-1255)"),
133 /* 9 */
134 N_("Turkish (ISO 8859-9)"),
135 N_("Turkish (Windows-1254)"),
136 /* 10 -> 4 */
137 /* 11 */
138 N_("Thai (TIS 620-2533/ISO 8859-11)"),
139 N_("Thai (Windows-874)"),
140 /* 13 */
141 N_("Baltic (Latin-7)"),
142 N_("Baltic (Windows-1257)"),
143 /* 12 -> /dev/null */
144 /* 14 */
145 N_("Celtic (Latin-8)"),
146 /* 15 -> 1 */
147 /* 16 */
148 N_("South-Eastern European (Latin-10)"),
149 /* CJK families */
150 N_("Simplified Chinese (ISO-2022-CN-EXT)"),
151 N_("Simplified Chinese Unix (EUC-CN)"),
152 N_("Japanese (7-bits JIS/ISO-2022-JP-2)"),
153 N_("Japanese Unix (EUC-JP)"),
154 N_("Japanese (Shift JIS)"),
155 N_("Korean (EUC-KR/CP949)"),
156 N_("Korean (ISO-2022-KR)"),
157 N_("Traditional Chinese (Big5)"),
158 N_("Traditional Chinese Unix (EUC-TW)"),
159 N_("Hong-Kong Supplementary (HKSCS)"),
160 /* Other */
161 N_("Vietnamese (VISCII)"),
162 N_("Vietnamese (Windows-1258)"),
165 static const int pi_justification[] = { 0, 1, 2 };
166 static const char *const ppsz_justification_text[] = {
167 N_("Center"),N_("Left"),N_("Right")};
169 #define ENCODING_TEXT N_("Subtitle text encoding")
170 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
171 #define ALIGN_TEXT N_("Subtitle justification")
172 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
173 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitle autodetection")
174 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
175 "UTF-8 encoding within subtitle files.")
177 static int OpenDecoder ( vlc_object_t * );
178 static void CloseDecoder ( vlc_object_t * );
180 vlc_module_begin ()
181 set_shortname( N_("Subtitles"))
182 set_description( N_("Text subtitle decoder") )
183 set_capability( "spu decoder", 50 )
184 set_callbacks( OpenDecoder, CloseDecoder )
185 set_category( CAT_INPUT )
186 set_subcategory( SUBCAT_INPUT_SCODEC )
188 add_integer( "subsdec-align", 0, ALIGN_TEXT, ALIGN_LONGTEXT,
189 false )
190 change_integer_list( pi_justification, ppsz_justification_text )
191 add_string( "subsdec-encoding", "",
192 ENCODING_TEXT, ENCODING_LONGTEXT, false )
193 change_string_list( ppsz_encodings, ppsz_encoding_names )
194 add_bool( "subsdec-autodetect-utf8", true,
195 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, false )
196 vlc_module_end ()
198 /*****************************************************************************
199 * Local prototypes
200 *****************************************************************************/
201 #define NO_BREAKING_SPACE "&#160;"
203 struct decoder_sys_t
205 int i_align; /* Subtitles alignment on the vout */
207 vlc_iconv_t iconv_handle; /* handle to iconv instance */
208 bool b_autodetect_utf8;
212 static int DecodeBlock ( decoder_t *, block_t * );
213 static subpicture_t *ParseText ( decoder_t *, block_t * );
214 static text_segment_t *ParseSubtitles(int *pi_align, const char * );
216 /*****************************************************************************
217 * OpenDecoder: probe the decoder and return score
218 *****************************************************************************
219 * Tries to launch a decoder and return score so that the interface is able
220 * to chose.
221 *****************************************************************************/
222 static int OpenDecoder( vlc_object_t *p_this )
224 decoder_t *p_dec = (decoder_t*)p_this;
225 decoder_sys_t *p_sys;
227 switch( p_dec->fmt_in.i_codec )
229 case VLC_CODEC_SUBT:
230 case VLC_CODEC_ITU_T140:
231 break;
232 default:
233 return VLC_EGENERIC;
236 /* Allocate the memory needed to store the decoder's structure */
237 p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
238 if( p_sys == NULL )
239 return VLC_ENOMEM;
241 p_dec->pf_decode = DecodeBlock;
242 p_dec->fmt_out.i_codec = 0;
244 /* init of p_sys */
245 p_sys->i_align = 0;
246 p_sys->iconv_handle = (vlc_iconv_t)-1;
247 p_sys->b_autodetect_utf8 = false;
249 const char *encoding;
250 char *var = NULL;
252 /* First try demux-specified encoding */
253 if( p_dec->fmt_in.i_codec == VLC_CODEC_ITU_T140 )
254 encoding = "UTF-8"; /* IUT T.140 is always using UTF-8 */
255 else
256 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
258 encoding = p_dec->fmt_in.subs.psz_encoding;
259 msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
260 encoding);
262 else
264 /* Second, try configured encoding */
265 if ((var = var_InheritString (p_dec, "subsdec-encoding")) != NULL)
267 msg_Dbg (p_dec, "trying configured character encoding: %s", var);
268 if (!strcmp (var, "system"))
270 free (var);
271 var = NULL;
272 encoding = "";
273 /* ^ iconv() treats "" as nl_langinfo(CODESET) */
275 else
276 encoding = var;
278 else
279 /* Third, try "local" encoding */
281 /* xgettext:
282 The Windows ANSI code page most commonly used for this language.
283 VLC uses this as a guess of the subtitle files character set
284 (if UTF-8 and UTF-16 autodetection fails).
285 Western European languages normally use "CP1252", which is a
286 Microsoft-variant of ISO 8859-1. That suits the Latin alphabet.
287 Other scripts use other code pages.
289 This MUST be a valid iconv character set. If unsure, please refer
290 the VideoLAN translators mailing list. */
291 encoding = vlc_pgettext("GetACP", "CP1252");
292 msg_Dbg (p_dec, "trying default character encoding: %s", encoding);
295 /* Check UTF-8 autodetection */
296 if (var_InheritBool (p_dec, "subsdec-autodetect-utf8"))
298 msg_Dbg (p_dec, "using automatic UTF-8 detection");
299 p_sys->b_autodetect_utf8 = true;
303 if (strcasecmp (encoding, "UTF-8") && strcasecmp (encoding, "utf8"))
305 p_sys->iconv_handle = vlc_iconv_open ("UTF-8", encoding);
306 if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
307 msg_Err (p_dec, "cannot convert from %s: %s", encoding,
308 vlc_strerror_c(errno));
310 free (var);
312 p_sys->i_align = var_InheritInteger( p_dec, "subsdec-align" );
314 return VLC_SUCCESS;
317 /****************************************************************************
318 * DecodeBlock: the whole thing
319 ****************************************************************************
320 * This function must be fed with complete subtitles units.
321 ****************************************************************************/
322 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
324 subpicture_t *p_spu;
326 if( p_block == NULL ) /* No Drain */
327 return VLCDEC_SUCCESS;
329 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
331 block_Release( p_block );
332 return VLCDEC_SUCCESS;
335 p_spu = ParseText( p_dec, p_block );
337 block_Release( p_block );
338 if( p_spu != NULL )
339 decoder_QueueSub( p_dec, p_spu );
340 return VLCDEC_SUCCESS;
343 /*****************************************************************************
344 * CloseDecoder: clean up the decoder
345 *****************************************************************************/
346 static void CloseDecoder( vlc_object_t *p_this )
348 decoder_t *p_dec = (decoder_t *)p_this;
349 decoder_sys_t *p_sys = p_dec->p_sys;
351 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
352 vlc_iconv_close( p_sys->iconv_handle );
354 free( p_sys );
357 /*****************************************************************************
358 * ParseText: parse an text subtitle packet and send it to the video output
359 *****************************************************************************/
360 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
362 decoder_sys_t *p_sys = p_dec->p_sys;
363 subpicture_t *p_spu = NULL;
364 char *psz_subtitle = NULL;
366 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
367 return NULL;
369 /* We cannot display a subpicture with no date */
370 if( p_block->i_pts <= VLC_TS_INVALID )
372 msg_Warn( p_dec, "subtitle without a date" );
373 return NULL;
376 /* Check validity of packet data */
377 /* An "empty" line containing only \0 can be used to force
378 and ephemer picture from the screen */
379 if( p_block->i_buffer < 1 )
381 msg_Warn( p_dec, "no subtitle data" );
382 return NULL;
385 /* Should be resiliant against bad subtitles */
386 psz_subtitle = malloc( p_block->i_buffer + 1 );
387 if( psz_subtitle == NULL )
388 return NULL;
389 memcpy( psz_subtitle, p_block->p_buffer, p_block->i_buffer );
390 psz_subtitle[p_block->i_buffer] = '\0';
392 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
394 if (EnsureUTF8( psz_subtitle ) == NULL)
396 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
397 "Try manually setting a character-encoding "
398 "before you open the file." );
401 else
404 if( p_sys->b_autodetect_utf8 )
406 if( IsUTF8( psz_subtitle ) == NULL )
408 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
409 "disabling UTF-8 subtitles autodetection" );
410 p_sys->b_autodetect_utf8 = false;
414 if( !p_sys->b_autodetect_utf8 )
416 size_t inbytes_left = strlen( psz_subtitle );
417 size_t outbytes_left = 6 * inbytes_left;
418 char *psz_new_subtitle = xmalloc( outbytes_left + 1 );
419 char *psz_convert_buffer_out = psz_new_subtitle;
420 const char *psz_convert_buffer_in = psz_subtitle;
422 size_t ret = vlc_iconv( p_sys->iconv_handle,
423 &psz_convert_buffer_in, &inbytes_left,
424 &psz_convert_buffer_out, &outbytes_left );
426 *psz_convert_buffer_out++ = '\0';
427 free( psz_subtitle );
429 if( ( ret == (size_t)(-1) ) || inbytes_left )
431 free( psz_new_subtitle );
432 msg_Err( p_dec, "failed to convert subtitle encoding.\n"
433 "Try manually setting a character-encoding "
434 "before you open the file." );
435 return NULL;
438 psz_subtitle = realloc( psz_new_subtitle,
439 psz_convert_buffer_out - psz_new_subtitle );
440 if( !psz_subtitle )
441 psz_subtitle = psz_new_subtitle;
445 /* Create the subpicture unit */
446 p_spu = decoder_NewSubpictureText( p_dec );
447 if( !p_spu )
449 free( psz_subtitle );
450 return NULL;
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;
457 subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
459 p_spu_sys->region.inner_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
460 p_spu_sys->region.p_segments = ParseSubtitles( &p_spu_sys->region.inner_align, psz_subtitle );
462 free( psz_subtitle );
464 return p_spu;
467 static bool AppendCharacter( text_segment_t* p_segment, char c )
469 char* tmp;
470 if ( asprintf( &tmp, "%s%c", p_segment->psz_text ? p_segment->psz_text : "", c ) < 0 )
471 return false;
472 free( p_segment->psz_text );
473 p_segment->psz_text = tmp;
474 return true;
477 static bool AppendString( text_segment_t* p_segment, const char* psz_str )
479 char* tmp;
480 if ( asprintf( &tmp, "%s%s", p_segment->psz_text ? p_segment->psz_text : "", psz_str ) < 0 )
481 return false;
482 free( p_segment->psz_text );
483 p_segment->psz_text = tmp;
484 return true;
487 static char* ConsumeAttribute( const char** ppsz_subtitle, char** ppsz_attribute_value )
489 const char* psz_subtitle = *ppsz_subtitle;
490 char* psz_attribute_name;
491 *ppsz_attribute_value = NULL;
493 while (*psz_subtitle == ' ')
494 psz_subtitle++;
496 size_t attr_len = 0;
497 char delimiter;
499 while ( *psz_subtitle && isalpha( *psz_subtitle ) )
501 psz_subtitle++;
502 attr_len++;
504 if ( !*psz_subtitle || attr_len == 0 )
505 return NULL;
506 psz_attribute_name = malloc( attr_len + 1 );
507 if ( unlikely( !psz_attribute_name ) )
508 return NULL;
509 strncpy( psz_attribute_name, psz_subtitle - attr_len, attr_len );
510 psz_attribute_name[attr_len] = 0;
512 // Skip over to the attribute value
513 while ( *psz_subtitle && *psz_subtitle != '=' )
514 psz_subtitle++;
515 if ( !*psz_subtitle )
517 *ppsz_subtitle = psz_subtitle;
518 return psz_attribute_name;
520 // Skip the '=' sign
521 psz_subtitle++;
523 // Aknoledge the delimiter if any
524 while ( *psz_subtitle && isspace( *psz_subtitle) )
525 psz_subtitle++;
527 if ( *psz_subtitle == '\'' || *psz_subtitle == '"' )
529 // Save the delimiter and skip it
530 delimiter = *psz_subtitle;
531 psz_subtitle++;
533 else
534 delimiter = 0;
536 // Skip spaces, just in case
537 while ( *psz_subtitle && isspace( *psz_subtitle ) )
538 psz_subtitle++;
540 attr_len = 0;
541 while ( *psz_subtitle && ( ( delimiter != 0 && *psz_subtitle != delimiter ) ||
542 ( delimiter == 0 && ( !isspace(*psz_subtitle) && *psz_subtitle != '>' ) ) ) )
544 psz_subtitle++;
545 attr_len++;
547 if ( attr_len == 0 )
549 *ppsz_subtitle = psz_subtitle;
550 return psz_attribute_name;
552 if ( unlikely( !( *ppsz_attribute_value = malloc( attr_len + 1 ) ) ) )
554 free( psz_attribute_name );
555 return NULL;
557 strncpy( *ppsz_attribute_value, psz_subtitle - attr_len, attr_len );
558 (*ppsz_attribute_value)[attr_len] = 0;
559 // Finally, skip over the final delimiter
560 if (delimiter != 0 && *psz_subtitle)
561 psz_subtitle++;
562 *ppsz_subtitle = psz_subtitle;
563 return psz_attribute_name;
566 // Returns the next tag and consume the string up to after the tag name, or
567 // returns NULL and doesn't advance if the angle bracket was not a tag opening
568 // For instance, if psz_subtitle == "<some_tag attribute=value>"
569 // GetTag will return "some_tag", and will advance up to the first 'a' in "attribute"
570 // The returned value must be freed.
571 static char* GetTag( const char** ppsz_subtitle, bool b_closing )
573 const char* psz_subtitle = *ppsz_subtitle;
574 if ( *psz_subtitle != '<' )
575 return NULL;
576 // Skip the '<'
577 psz_subtitle++;
578 if ( b_closing && *psz_subtitle == '/' )
579 psz_subtitle++;
580 // Skip potential spaces
581 while ( *psz_subtitle == ' ' )
582 psz_subtitle++;
583 // Now we need to verify if what comes next is a valid tag:
584 if ( !isalpha( *psz_subtitle ) )
585 return NULL;
586 size_t tag_size = 1;
587 while ( isalnum( psz_subtitle[tag_size] ) || psz_subtitle[tag_size] == '_' )
588 tag_size++;
589 char* psz_tagname = malloc( ( tag_size + 1 ) * sizeof( *psz_tagname ) );
590 if ( unlikely( !psz_tagname ) )
591 return NULL;
592 strncpy( psz_tagname, psz_subtitle, tag_size );
593 psz_tagname[tag_size] = 0;
594 psz_subtitle += tag_size;
595 *ppsz_subtitle = psz_subtitle;
596 return psz_tagname;
599 static bool IsClosed( const char* psz_subtitle, const char* psz_tagname )
601 const char* psz_tagpos = strcasestr( psz_subtitle, psz_tagname );
602 if ( !psz_tagpos )
603 return false;
604 // Search for '</' and '>' immediatly before & after (minding the potential spaces)
605 const char* psz_endtag = psz_tagpos + strlen( psz_tagname );
606 while ( *psz_endtag == ' ' )
607 psz_endtag++;
608 if ( *psz_endtag != '>' )
609 return false;
610 // Skip back before the tag itself
611 psz_tagpos--;
612 while ( *psz_tagpos == ' ' && psz_tagpos > psz_subtitle )
613 psz_tagpos--;
614 if ( *psz_tagpos-- != '/' )
615 return false;
616 if ( *psz_tagpos != '<' )
617 return false;
618 return true;
621 typedef struct tag_stack tag_stack_t;
622 struct tag_stack
624 char* psz_tagname;
625 tag_stack_t *p_next;
628 static void AppendTag( tag_stack_t **pp_stack, char* psz_tagname )
630 tag_stack_t* p_elem = malloc( sizeof( *p_elem ) );
631 if ( unlikely( !p_elem ) )
632 return;
633 p_elem->p_next = *pp_stack;
634 p_elem->psz_tagname = psz_tagname;
635 *pp_stack = p_elem;
638 static bool HasTag( tag_stack_t **pp_stack, const char* psz_tagname )
640 tag_stack_t *p_prev = NULL;
641 for ( tag_stack_t* p_current = *pp_stack; p_current; p_current = p_current->p_next )
643 if ( !strcasecmp( psz_tagname, p_current->psz_tagname ) )
645 if ( p_current == *pp_stack )
647 *pp_stack = p_current->p_next;
649 else
651 p_prev->p_next = p_current->p_next;
653 free( p_current->psz_tagname );
654 free( p_current );
655 return true;
657 p_prev = p_current;
659 return false;
663 * mini style stack implementation
665 typedef struct style_stack style_stack_t;
666 struct style_stack
668 text_style_t* p_style;
669 style_stack_t* p_next;
672 static text_style_t* DuplicateAndPushStyle(style_stack_t** pp_stack)
674 text_style_t* p_dup = ( *pp_stack ) ? text_style_Duplicate( (*pp_stack)->p_style ) : text_style_Create( STYLE_NO_DEFAULTS );
675 if ( unlikely( !p_dup ) )
676 return NULL;
677 style_stack_t* p_entry = malloc( sizeof( *p_entry ) );
678 if ( unlikely( !p_entry ) )
680 free( p_dup );
681 return NULL;
683 // Give the style ownership to the segment.
684 p_entry->p_style = p_dup;
685 p_entry->p_next = *pp_stack;
686 *pp_stack = p_entry;
687 return p_dup;
690 static void PopStyle(style_stack_t** pp_stack)
692 style_stack_t* p_old = *pp_stack;
693 if ( !p_old )
694 return;
695 *pp_stack = p_old->p_next;
696 // Don't free the style, it is now owned by the text_segment_t
697 free( p_old );
700 static text_segment_t* NewTextSegmentPushStyle( text_segment_t* p_segment, style_stack_t** pp_stack )
702 text_segment_t* p_new = text_segment_New( NULL );
703 if ( unlikely( p_new == NULL ) )
704 return NULL;
705 text_style_t* p_style = DuplicateAndPushStyle( pp_stack );
706 p_new->style = p_style;
707 p_segment->p_next = p_new;
708 return p_new;
711 static text_segment_t* NewTextSegmentPopStyle( text_segment_t* p_segment, style_stack_t** pp_stack )
713 text_segment_t* p_new = text_segment_New( NULL );
714 if ( unlikely( p_new == NULL ) )
715 return NULL;
716 // We shouldn't have an empty stack since this happens when closing a tag,
717 // but better be safe than sorry if (/when) we encounter a broken subtitle file.
718 PopStyle( pp_stack );
719 text_style_t* p_dup = ( *pp_stack ) ? text_style_Duplicate( (*pp_stack)->p_style ) : text_style_Create( STYLE_NO_DEFAULTS );
720 p_new->style = p_dup;
721 p_segment->p_next = p_new;
722 return p_new;
725 static text_segment_t* ParseSubtitles( int *pi_align, const char *psz_subtitle )
727 text_segment_t* p_segment;
728 text_segment_t* p_first_segment;
729 style_stack_t* p_stack = NULL;
730 tag_stack_t* p_tag_stack = NULL;
732 //FIXME: Remove initial allocation? Might make the below code more complicated
733 p_first_segment = p_segment = text_segment_New( "" );
735 bool b_has_align = false;
737 /* */
738 while( *psz_subtitle )
740 /* HTML extensions */
741 if( *psz_subtitle == '<' )
743 char *psz_tagname = GetTag( &psz_subtitle, false );
744 if ( psz_tagname != NULL )
746 if( !strcasecmp( psz_tagname, "br" ) )
748 if ( !AppendCharacter( p_segment, '\n' ) )
750 free( psz_tagname );
751 goto fail;
754 else if( !strcasecmp( psz_tagname, "b" ) )
756 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
757 p_segment->style->i_style_flags |= STYLE_BOLD;
758 p_segment->style->i_features |= STYLE_HAS_FLAGS;
760 else if( !strcasecmp( psz_tagname, "i" ) )
762 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
763 p_segment->style->i_style_flags |= STYLE_ITALIC;
764 p_segment->style->i_features |= STYLE_HAS_FLAGS;
766 else if( !strcasecmp( psz_tagname, "u" ) )
768 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
769 p_segment->style->i_style_flags |= STYLE_UNDERLINE;
770 p_segment->style->i_features |= STYLE_HAS_FLAGS;
772 else if( !strcasecmp( psz_tagname, "s" ) )
774 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
775 p_segment->style->i_style_flags |= STYLE_STRIKEOUT;
776 p_segment->style->i_features |= STYLE_HAS_FLAGS;
778 else if( !strcasecmp( psz_tagname, "font" ) )
780 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
782 char* psz_attribute_name;
783 char* psz_attribute_value;
785 while( ( psz_attribute_name = ConsumeAttribute( &psz_subtitle, &psz_attribute_value ) ) )
787 if ( !psz_attribute_value )
789 free( psz_attribute_name );
790 continue;
792 if ( !strcasecmp( psz_attribute_name, "face" ) )
794 p_segment->style->psz_fontname = psz_attribute_value;
795 // We don't want to free the attribute value since it has become our fontname
796 psz_attribute_value = NULL;
798 else if ( !strcasecmp( psz_attribute_name, "family" ) )
800 p_segment->style->psz_monofontname = psz_attribute_value;
801 psz_attribute_value = NULL;
803 else if ( !strcasecmp( psz_attribute_name, "size" ) )
805 int size = atoi( psz_attribute_value );
806 if( size )
808 p_segment->style->i_font_size = size;
809 p_segment->style->f_font_relsize = STYLE_DEFAULT_REL_FONT_SIZE *
810 STYLE_DEFAULT_FONT_SIZE / p_segment->style->i_font_size;
813 else if ( !strcasecmp( psz_attribute_name, "color" ) )
815 p_segment->style->i_font_color = vlc_html_color( psz_attribute_value, NULL );
816 p_segment->style->i_features |= STYLE_HAS_FONT_COLOR;
818 else if ( !strcasecmp( psz_attribute_name, "outline-color" ) )
820 p_segment->style->i_outline_color = vlc_html_color( psz_attribute_value, NULL );
821 p_segment->style->i_features |= STYLE_HAS_OUTLINE_COLOR;
823 else if ( !strcasecmp( psz_attribute_name, "shadow-color" ) )
825 p_segment->style->i_shadow_color = vlc_html_color( psz_attribute_value, NULL );
826 p_segment->style->i_features |= STYLE_HAS_SHADOW_COLOR;
828 else if ( !strcasecmp( psz_attribute_name, "outline-level" ) )
830 p_segment->style->i_outline_width = atoi( psz_attribute_value );
832 else if ( !strcasecmp( psz_attribute_name, "shadow-level" ) )
834 p_segment->style->i_shadow_width = atoi( psz_attribute_value );
836 else if ( !strcasecmp( psz_attribute_name, "back-color" ) )
838 p_segment->style->i_background_color = vlc_html_color( psz_attribute_value, NULL );
839 p_segment->style->i_features |= STYLE_HAS_BACKGROUND_COLOR;
841 else if ( !strcasecmp( psz_attribute_name, "alpha" ) )
843 p_segment->style->i_font_alpha = atoi( psz_attribute_value );
844 p_segment->style->i_features |= STYLE_HAS_FONT_ALPHA;
847 free( psz_attribute_name );
848 free( psz_attribute_value );
851 else
853 // This is an unknown tag. We need to hide it if it's properly closed, and display it otherwise
854 if ( !IsClosed( psz_subtitle, psz_tagname ) )
856 AppendCharacter( p_segment, '<' );
857 AppendString( p_segment, psz_tagname );
858 AppendCharacter( p_segment, '>' );
860 else
862 AppendTag( &p_tag_stack, psz_tagname );
863 // We don't want to free the tagname now, it will be freed when the tag
864 // gets poped from the stack.
865 psz_tagname = NULL;
867 // In any case, fall through and skip to the closing tag.
869 // Skip potential spaces & end tag
870 while ( *psz_subtitle && *psz_subtitle != '>' )
871 psz_subtitle++;
872 if ( *psz_subtitle == '>' )
873 psz_subtitle++;
875 free( psz_tagname );
877 else if( !strncmp( psz_subtitle, "</", 2 ))
879 char* psz_tagname = GetTag( &psz_subtitle, true );
880 if ( psz_tagname != NULL )
882 if ( !strcasecmp( psz_tagname, "b" ) ||
883 !strcasecmp( psz_tagname, "i" ) ||
884 !strcasecmp( psz_tagname, "u" ) ||
885 !strcasecmp( psz_tagname, "s" ) ||
886 !strcasecmp( psz_tagname, "font" ) )
888 // A closing tag for one of the tags we handle, meaning
889 // we pushed a style onto the stack earlier
890 p_segment = NewTextSegmentPopStyle( p_segment, &p_stack );
892 else
894 // Unknown closing tag. If it is closing an unknown tag, ignore it. Otherwise, display it
895 if ( !HasTag( &p_tag_stack, psz_tagname ) )
897 AppendString( p_segment, "</" );
898 AppendString( p_segment, psz_tagname );
899 AppendCharacter( p_segment, '>' );
902 while ( *psz_subtitle == ' ' )
903 psz_subtitle++;
904 if ( *psz_subtitle == '>' )
905 psz_subtitle++;
906 free( psz_tagname );
909 else
911 /* We have an unknown tag, just append it, and move on.
912 * The rest of the string won't be recognized as a tag, and
913 * we will ignore unknown closing tag
915 AppendCharacter( p_segment, '<' );
916 psz_subtitle++;
919 /* SSA extensions */
920 else if( psz_subtitle[0] == '{' && psz_subtitle[1] == '\\' &&
921 strchr( psz_subtitle, '}' ) )
923 /* Check for forced alignment */
924 if( !b_has_align &&
925 !strncmp( psz_subtitle, "{\\an", 4 ) && psz_subtitle[4] >= '1' && psz_subtitle[4] <= '9' && psz_subtitle[5] == '}' )
927 static const int pi_vertical[3] = { SUBPICTURE_ALIGN_BOTTOM, 0, SUBPICTURE_ALIGN_TOP };
928 static const int pi_horizontal[3] = { SUBPICTURE_ALIGN_LEFT, 0, SUBPICTURE_ALIGN_RIGHT };
929 const int i_id = psz_subtitle[4] - '1';
931 b_has_align = true;
932 *pi_align = pi_vertical[i_id/3] | pi_horizontal[i_id%3];
934 /* TODO fr -> rotation */
936 /* Hide {\stupidity} */
937 psz_subtitle = strchr( psz_subtitle, '}' ) + 1;
939 /* MicroDVD extensions */
940 /* FIXME:
941 * - Currently, we don't do difference between X and x, and we should:
942 * Capital Letters applies to the whole text and not one line
943 * - We don't support Position and Coordinates
944 * - We don't support the DEFAULT flag (HEADER)
947 else if( psz_subtitle[0] == '{' && psz_subtitle[1] != 0 &&
948 psz_subtitle[2] == ':' && strchr( &psz_subtitle[2], '}' ) )
950 const char *psz_tag_end = strchr( &psz_subtitle[2], '}' );
951 size_t i_len = psz_tag_end - &psz_subtitle[3];
953 if( psz_subtitle[1] == 'Y' || psz_subtitle[1] == 'y' )
955 if( psz_subtitle[3] == 'i' )
957 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
958 p_segment->style->i_style_flags |= STYLE_ITALIC;
959 p_segment->style->i_features |= STYLE_HAS_FLAGS;
960 psz_subtitle++;
962 if( psz_subtitle[3] == 'b' )
964 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
965 p_segment->style->i_style_flags |= STYLE_BOLD;
966 p_segment->style->i_features |= STYLE_HAS_FLAGS;
967 psz_subtitle++;
969 if( psz_subtitle[3] == 'u' )
971 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
972 p_segment->style->i_style_flags |= STYLE_UNDERLINE;
973 p_segment->style->i_features |= STYLE_HAS_FLAGS;
974 psz_subtitle++;
977 else if( (psz_subtitle[1] == 'C' || psz_subtitle[1] == 'c' )
978 && psz_subtitle[3] == '$' && i_len >= 7 )
980 /* Yes, they use BBGGRR, instead of RRGGBB */
981 char psz_color[7];
982 psz_color[0] = psz_subtitle[8]; psz_color[1] = psz_subtitle[9];
983 psz_color[2] = psz_subtitle[6]; psz_color[3] = psz_subtitle[7];
984 psz_color[4] = psz_subtitle[4]; psz_color[5] = psz_subtitle[5];
985 psz_color[6] = '\0';
986 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
987 p_segment->style->i_font_color = vlc_html_color( psz_color, NULL );
988 p_segment->style->i_features |= STYLE_HAS_FONT_COLOR;
990 else if( psz_subtitle[1] == 'F' || psz_subtitle[1] == 'f' )
992 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
993 p_segment->style->psz_fontname = strndup( &psz_subtitle[3], i_len );
995 else if( psz_subtitle[1] == 'S' || psz_subtitle[1] == 's' )
997 int size = atoi( &psz_subtitle[3] );
998 if( size )
1000 p_segment = NewTextSegmentPushStyle( p_segment, &p_stack );
1001 p_segment->style->i_font_size = size;
1002 p_segment->style->f_font_relsize = STYLE_DEFAULT_REL_FONT_SIZE *
1003 STYLE_DEFAULT_FONT_SIZE / p_segment->style->i_font_size;
1007 /* Currently unsupported since we don't have access to the i_align flag here
1008 else if( psz_subtitle[1] == 'P' )
1010 if( psz_subtitle[3] == "1" )
1011 i_align = SUBPICTURE_ALIGN_TOP;
1012 else if( psz_subtitle[3] == "0" )
1013 i_align = SUBPICTURE_ALIGN_BOTTOM;
1014 } */
1015 // Hide other {x:y} atrocities, notably {o:x}
1016 psz_subtitle = psz_tag_end + 1;
1018 else
1020 if( *psz_subtitle == '\n' || !strncasecmp( psz_subtitle, "\\n", 2 ) )
1022 if ( !AppendCharacter( p_segment, '\n' ) )
1023 goto fail;
1024 if ( *psz_subtitle == '\n' )
1025 psz_subtitle++;
1026 else
1027 psz_subtitle += 2;
1029 else if( !strncasecmp( psz_subtitle, "\\h", 2 ) )
1031 if ( !AppendString( p_segment, "\xC2\xA0" ) )
1032 goto fail;
1033 psz_subtitle += 2;
1035 else
1037 //FIXME: Highly inneficient
1038 AppendCharacter( p_segment, *psz_subtitle );
1039 psz_subtitle++;
1043 while ( p_stack )
1044 PopStyle( &p_stack );
1045 while ( p_tag_stack )
1047 tag_stack_t *p_tag = p_tag_stack;
1048 p_tag_stack = p_tag_stack->p_next;
1049 free( p_tag->psz_tagname );
1050 free( p_tag );
1053 return p_first_segment;
1055 fail:
1056 text_segment_ChainDelete( p_first_segment );
1057 return NULL;