1 /*****************************************************************************
2 * subsdec.c : text subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
5 * $Id: subsdec.c 20996 2007-08-05 20:01:21Z jb $
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 <b dot purcell at adbglobal dot com>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************/
36 static int OpenDecoder ( vlc_object_t * );
37 static void CloseDecoder ( vlc_object_t * );
39 static subpicture_t *DecodeBlock ( decoder_t *, block_t ** );
40 static subpicture_t *ParseText ( decoder_t *, block_t * );
41 static char *StripTags ( char * );
42 static char *CreateHtmlSubtitle ( char * );
43 static int ParseImageAttachments( decoder_t *p_dec );
45 static int ParsePositionAttributeList( char *, int *, int *, int * );
47 /*****************************************************************************
49 *****************************************************************************/
50 static const char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
51 "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
52 "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
53 "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
54 "ISO-8859-6", "CP1256", "MacArabic", "",
55 "ISO-8859-7", "CP1253", "MacGreek", "",
56 "ISO-8859-8", "CP1255", "MacHebrew", "",
57 "ISO-8859-9", "CP1254", "MacTurkish", "",
58 "ISO-8859-13", "CP1257", "",
59 "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
60 "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
61 "ISO-2022-KR", "EUC-KR", "",
62 "MacThai", "KOI8-T", "",
63 "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
64 "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
66 "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
67 "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
68 "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
69 "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
70 "HPROMAN8", "NEXTSTEP" };
72 SSA supports charset selection.
73 The following known charsets are used:
75 0 = Ansi - Western European
80 128 = Japanese (Shift JIS)
83 134 = GB2312 Simplified Chinese
84 136 = Big5 Traditional Chinese
91 204 = Russian (Cyrillic)
93 238 = Eastern European
97 static int pi_justification[] = { 0, 1, 2 };
98 static const char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
100 #define ENCODING_TEXT N_("Subtitles text encoding")
101 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
102 #define ALIGN_TEXT N_("Subtitles justification")
103 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
104 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
105 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
106 "UTF-8 encoding within subtitles files.")
107 #define FORMAT_TEXT N_("Formatted Subtitles")
108 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
109 "VLC partly implements this, but you can choose to disable all formatting.")
113 set_shortname( _("Subtitles"));
114 set_description( _("Text subtitles decoder") );
115 set_capability( "decoder", 50 );
116 set_callbacks( OpenDecoder, CloseDecoder );
117 set_category( CAT_INPUT );
118 set_subcategory( SUBCAT_INPUT_SCODEC );
120 add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
122 change_integer_list( pi_justification, ppsz_justification_text, 0 );
123 add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
124 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
125 change_string_list( ppsz_encodings, 0, 0 );
126 add_bool( "subsdec-autodetect-utf8", VLC_TRUE, NULL,
127 AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, VLC_FALSE );
128 add_bool( "subsdec-formatted", VLC_TRUE, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
132 /*****************************************************************************
133 * OpenDecoder: probe the decoder and return score
134 *****************************************************************************
135 * Tries to launch a decoder and return score so that the interface is able
137 *****************************************************************************/
138 static int OpenDecoder( vlc_object_t *p_this )
140 decoder_t *p_dec = (decoder_t*)p_this;
141 decoder_sys_t *p_sys;
144 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
145 p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') &&
146 p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
151 p_dec->pf_decode_sub = DecodeBlock;
153 /* Allocate the memory needed to store the decoder's structure */
154 if( ( p_dec->p_sys = p_sys =
155 (decoder_sys_t *)calloc(1, sizeof(decoder_sys_t)) ) == NULL )
157 msg_Err( p_dec, "out of memory" );
163 p_sys->iconv_handle = (vlc_iconv_t)-1;
164 p_sys->b_autodetect_utf8 = VLC_FALSE;
165 p_sys->b_ass = VLC_FALSE;
166 p_sys->i_original_height = -1;
167 p_sys->i_original_width = -1;
168 TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
169 TAB_INIT( p_sys->i_images, p_sys->pp_images );
171 char *psz_charset = NULL;
172 /* First try demux-specified encoding */
173 if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
175 psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
176 msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
177 p_dec->fmt_in.subs.psz_encoding ?: "not specified");
180 /* Second, try configured encoding */
181 if (psz_charset == NULL)
183 psz_charset = var_CreateGetNonEmptyString (p_dec, "subsdec-encoding");
184 if ((psz_charset != NULL) && !strcasecmp (psz_charset, DEFAULT_NAME))
190 msg_Dbg (p_dec, "trying configured character encoding: %s",
191 psz_charset ?: "not specified");
194 /* Third, try "local" encoding with optional UTF-8 autodetection */
195 if (psz_charset == NULL)
197 psz_charset = strdup (GetFallbackEncoding ());
198 msg_Dbg (p_dec, "trying default character encoding: %s",
199 psz_charset ?: "not specified");
201 if (var_CreateGetBool (p_dec, "subsdec-autodetect-utf8"))
203 msg_Dbg (p_dec, "using automatic UTF-8 detection");
204 p_sys->b_autodetect_utf8 = VLC_TRUE;
208 if (psz_charset == NULL)
210 psz_charset = strdup ("UTF-8");
211 msg_Dbg (p_dec, "trying hard-coded character encoding: %s",
212 psz_charset ?: "error");
215 if (psz_charset == NULL)
221 if (strcasecmp (psz_charset, "UTF-8") && strcasecmp (psz_charset, "utf8"))
223 p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
224 if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
225 msg_Err (p_dec, "cannot convert from %s: %s", psz_charset,
230 var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
231 var_Get( p_dec, "subsdec-align", &val );
232 p_sys->i_align = val.i_int;
234 ParseImageAttachments( p_dec );
236 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
238 if( p_dec->fmt_in.i_extra > 0 )
239 ParseSSAHeader( p_dec );
241 else if( p_dec->fmt_in.i_codec == VLC_FOURCC('u','s','f',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
243 if( p_dec->fmt_in.i_extra > 0 )
244 ParseUSFHeader( p_dec );
250 /****************************************************************************
251 * DecodeBlock: the whole thing
252 ****************************************************************************
253 * This function must be fed with complete subtitles units.
254 ****************************************************************************/
255 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
257 subpicture_t *p_spu = NULL;
259 if( !pp_block || *pp_block == NULL ) return NULL;
261 p_spu = ParseText( p_dec, *pp_block );
263 block_Release( *pp_block );
269 /*****************************************************************************
270 * CloseDecoder: clean up the decoder
271 *****************************************************************************/
272 static void CloseDecoder( vlc_object_t *p_this )
274 decoder_t *p_dec = (decoder_t *)p_this;
275 decoder_sys_t *p_sys = p_dec->p_sys;
277 if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
278 vlc_iconv_close( p_sys->iconv_handle );
280 if( p_sys->pp_ssa_styles )
283 for( i = 0; i < p_sys->i_ssa_styles; i++ )
285 if( !p_sys->pp_ssa_styles[i] )
288 if( p_sys->pp_ssa_styles[i]->psz_stylename )
289 free( p_sys->pp_ssa_styles[i]->psz_stylename );
290 if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname )
291 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
292 if( p_sys->pp_ssa_styles[i] )
293 free( p_sys->pp_ssa_styles[i] );
295 TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
297 if( p_sys->pp_images )
300 for( i = 0; i < p_sys->i_images; i++ )
302 if( !p_sys->pp_images[i] )
305 if( p_sys->pp_images[i]->p_pic )
306 p_sys->pp_images[i]->p_pic->pf_release( p_sys->pp_images[i]->p_pic );
307 if( p_sys->pp_images[i]->psz_filename )
308 free( p_sys->pp_images[i]->psz_filename );
310 free( p_sys->pp_images[i] );
312 TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
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 )
323 decoder_sys_t *p_sys = p_dec->p_sys;
324 subpicture_t *p_spu = NULL;
325 char *psz_subtitle = NULL;
328 /* We cannot display a subpicture with no date */
329 if( p_block->i_pts == 0 )
331 msg_Warn( p_dec, "subtitle without a date" );
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 )
340 msg_Warn( p_dec, "no subtitle data" );
344 /* Should be resiliant against bad subtitles */
345 psz_subtitle = strndup( (const char *)p_block->p_buffer,
347 if( psz_subtitle == NULL )
350 if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
352 if (EnsureUTF8( psz_subtitle ) == NULL)
354 msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
355 "Try manually setting a character-encoding "
356 "before you open the file.") );
362 if( p_sys->b_autodetect_utf8 )
364 if( IsUTF8( psz_subtitle ) == NULL )
366 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
367 "disabling UTF-8 subtitles autodetection" );
368 p_sys->b_autodetect_utf8 = VLC_FALSE;
372 if( !p_sys->b_autodetect_utf8 )
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;
380 size_t ret = vlc_iconv( p_sys->iconv_handle,
381 &psz_convert_buffer_in, &inbytes_left,
382 &psz_convert_buffer_out, &outbytes_left );
384 *psz_convert_buffer_out++ = '\0';
385 free( psz_subtitle );
387 if( ( ret == (size_t)(-1) ) || inbytes_left )
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.") );
396 psz_subtitle = realloc( psz_new_subtitle,
397 psz_convert_buffer_out - psz_new_subtitle );
401 /* Create the subpicture unit */
402 p_spu = p_dec->pf_spu_buffer_new( p_dec );
405 msg_Warn( p_dec, "can't get spu buffer" );
406 if( psz_subtitle ) free( psz_subtitle );
410 p_spu->b_pausable = VLC_TRUE;
412 /* Create a new subpicture region */
413 memset( &fmt, 0, sizeof(video_format_t) );
414 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
416 fmt.i_width = fmt.i_height = 0;
417 fmt.i_x_offset = fmt.i_y_offset = 0;
418 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
419 if( !p_spu->p_region )
421 msg_Err( p_dec, "cannot allocate SPU region" );
422 if( psz_subtitle ) free( psz_subtitle );
423 p_dec->pf_spu_buffer_del( p_dec, p_spu );
427 /* Decode and format the subpicture unit */
428 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') &&
429 p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') )
431 /* Normal text subs, easy markup */
432 p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
433 p_spu->i_x = p_sys->i_align ? 20 : 0;
436 /* Remove formatting from string */
438 p_spu->p_region->psz_text = StripTags( psz_subtitle );
439 if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
441 p_spu->p_region->psz_html = CreateHtmlSubtitle( psz_subtitle );
444 p_spu->i_start = p_block->i_pts;
445 p_spu->i_stop = p_block->i_pts + p_block->i_length;
446 p_spu->b_ephemer = (p_block->i_length == 0);
447 p_spu->b_absolute = VLC_FALSE;
451 /* Decode SSA/USF strings */
452 if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
453 ParseSSAString( p_dec, psz_subtitle, p_spu );
456 p_spu->pf_destroy_region( VLC_OBJECT(p_dec), p_spu->p_region );
457 p_spu->p_region = ParseUSFString( p_dec, psz_subtitle, p_spu );
460 p_spu->i_start = p_block->i_pts;
461 p_spu->i_stop = p_block->i_pts + p_block->i_length;
462 p_spu->b_ephemer = (p_block->i_length == 0);
463 p_spu->b_absolute = VLC_FALSE;
464 p_spu->i_original_picture_width = p_sys->i_original_width;
465 p_spu->i_original_picture_height = p_sys->i_original_height;
467 if( psz_subtitle ) free( psz_subtitle );
472 char *GrabAttributeValue( const char *psz_attribute,
473 const char *psz_tag_start )
475 if( psz_attribute && psz_tag_start )
477 char *psz_tag_end = strchr( psz_tag_start, '>' );
478 char *psz_found = strcasestr( psz_tag_start, psz_attribute );
482 psz_found += strlen( psz_attribute );
484 if(( *(psz_found++) == '=' ) &&
485 ( *(psz_found++) == '\"' ))
487 if( psz_found < psz_tag_end )
489 int i_len = strcspn( psz_found, "\"" );
490 return strndup( psz_found, i_len );
498 static ssa_style_t *ParseStyle( decoder_sys_t *p_sys, char *psz_subtitle )
500 ssa_style_t *p_style = NULL;
501 char *psz_style = GrabAttributeValue( "style", psz_subtitle );
507 for( i = 0; i < p_sys->i_ssa_styles; i++ )
509 if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
510 p_style = p_sys->pp_ssa_styles[i];
517 static int ParsePositionAttributeList( char *psz_subtitle, int *i_align,
522 char *psz_align = GrabAttributeValue( "alignment", psz_subtitle );
523 char *psz_margin_x = GrabAttributeValue( "horizontal-margin", psz_subtitle );
524 char *psz_margin_y = GrabAttributeValue( "vertical-margin", psz_subtitle );
526 char *psz_relative = GrabAttributeValue( "relative-to", psz_subtitle );
527 char *psz_rotate_x = GrabAttributeValue( "rotate-x", psz_subtitle );
528 char *psz_rotate_y = GrabAttributeValue( "rotate-y", psz_subtitle );
529 char *psz_rotate_z = GrabAttributeValue( "rotate-z", psz_subtitle );
532 *i_align = SUBPICTURE_ALIGN_BOTTOM;
538 if( !strcasecmp( "TopLeft", psz_align ) )
539 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
540 else if( !strcasecmp( "TopCenter", psz_align ) )
541 *i_align = SUBPICTURE_ALIGN_TOP;
542 else if( !strcasecmp( "TopRight", psz_align ) )
543 *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
544 else if( !strcasecmp( "MiddleLeft", psz_align ) )
545 *i_align = SUBPICTURE_ALIGN_LEFT;
546 else if( !strcasecmp( "MiddleCenter", psz_align ) )
548 else if( !strcasecmp( "MiddleRight", psz_align ) )
549 *i_align = SUBPICTURE_ALIGN_RIGHT;
550 else if( !strcasecmp( "BottomLeft", psz_align ) )
551 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
552 else if( !strcasecmp( "BottomCenter", psz_align ) )
553 *i_align = SUBPICTURE_ALIGN_BOTTOM;
554 else if( !strcasecmp( "BottomRight", psz_align ) )
555 *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
557 i_mask |= ATTRIBUTE_ALIGNMENT;
562 *i_x = atoi( psz_margin_x );
563 if( strchr( psz_margin_x, '%' ) )
564 i_mask |= ATTRIBUTE_X_PERCENT;
566 i_mask |= ATTRIBUTE_X;
568 free( psz_margin_x );
572 *i_y = atoi( psz_margin_y );
573 if( strchr( psz_margin_y, '%' ) )
574 i_mask |= ATTRIBUTE_Y_PERCENT;
576 i_mask |= ATTRIBUTE_Y;
578 free( psz_margin_y );
583 void SetupPositions( subpicture_region_t *p_region, char *psz_subtitle )
589 i_mask = ParsePositionAttributeList( psz_subtitle, &i_align, &i_x, &i_y );
591 if( i_mask & ATTRIBUTE_ALIGNMENT )
592 p_region->i_align = i_align;
594 /* TODO: Setup % based offsets properly, without adversely affecting
595 * everything else in vlc. Will address with separate patch, to
596 * prevent this one being any more complicated.
598 if( i_mask & ATTRIBUTE_X )
600 else if( i_mask & ATTRIBUTE_X_PERCENT )
603 if( i_mask & ATTRIBUTE_Y )
605 else if( i_mask & ATTRIBUTE_Y_PERCENT )
609 subpicture_region_t *CreateTextRegion( decoder_t *p_dec,
615 decoder_sys_t *p_sys = p_dec->p_sys;
616 subpicture_region_t *p_text_region;
619 /* Create a new subpicture region */
620 memset( &fmt, 0, sizeof(video_format_t) );
621 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
623 fmt.i_width = fmt.i_height = 0;
624 fmt.i_x_offset = fmt.i_y_offset = 0;
625 p_text_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
627 if( p_text_region != NULL )
629 ssa_style_t *p_style = NULL;
631 p_text_region->psz_text = NULL;
632 p_text_region->psz_html = strndup( psz_subtitle, i_len );
633 if( ! p_text_region->psz_html )
635 msg_Err( p_dec, "out of memory" );
636 p_spu->pf_destroy_region( VLC_OBJECT(p_dec), p_text_region );
640 p_style = ParseStyle( p_sys, p_text_region->psz_html );
645 for( i = 0; i < p_sys->i_ssa_styles; i++ )
647 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
648 p_style = p_sys->pp_ssa_styles[i];
654 msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename );
656 p_text_region->p_style = &p_style->font_style;
657 p_text_region->i_align = p_style->i_align;
659 /* TODO: Setup % based offsets properly, without adversely affecting
660 * everything else in vlc. Will address with separate patch,
661 * to prevent this one being any more complicated.
663 * p_style->i_margin_percent_h;
664 * p_style->i_margin_percent_v;
666 p_text_region->i_x = p_style->i_margin_h;
667 p_text_region->i_y = p_style->i_margin_v;
672 p_text_region->i_align = SUBPICTURE_ALIGN_BOTTOM | i_sys_align;
673 p_text_region->i_x = i_sys_align ? 20 : 0;
674 p_text_region->i_y = 10;
676 /* Look for position arguments which may override the style-based
679 SetupPositions( p_text_region, psz_subtitle );
681 p_text_region->p_next = NULL;
683 return p_text_region;
686 static int ParseImageAttachments( decoder_t *p_dec )
688 decoder_sys_t *p_sys = p_dec->p_sys;
689 input_attachment_t **pp_attachments;
690 int i_attachments_cnt;
693 if( VLC_SUCCESS != decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments_cnt ))
696 for( k = 0; k < i_attachments_cnt; k++ )
698 input_attachment_t *p_attach = pp_attachments[k];
700 vlc_fourcc_t type = 0;
702 if( ( !strcmp( p_attach->psz_mime, "image/bmp" ) ) || /* BMP */
703 ( !strcmp( p_attach->psz_mime, "image/x-bmp" ) ) ||
704 ( !strcmp( p_attach->psz_mime, "image/x-bitmap" ) ) ||
705 ( !strcmp( p_attach->psz_mime, "image/x-ms-bmp" ) ) )
707 type = VLC_FOURCC('b','m','p',' ');
709 else if( ( !strcmp( p_attach->psz_mime, "image/x-portable-anymap" ) ) || /* PNM */
710 ( !strcmp( p_attach->psz_mime, "image/x-portable-bitmap" ) ) || /* PBM */
711 ( !strcmp( p_attach->psz_mime, "image/x-portable-graymap" ) ) || /* PGM */
712 ( !strcmp( p_attach->psz_mime, "image/x-portable-pixmap" ) ) ) /* PPM */
714 type = VLC_FOURCC('p','n','m',' ');
716 else if ( !strcmp( p_attach->psz_mime, "image/gif" ) ) /* GIF */
717 type = VLC_FOURCC('g','i','f',' ');
718 else if ( !strcmp( p_attach->psz_mime, "image/jpeg" ) ) /* JPG, JPEG */
719 type = VLC_FOURCC('j','p','e','g');
720 else if ( !strcmp( p_attach->psz_mime, "image/pcx" ) ) /* PCX */
721 type = VLC_FOURCC('p','c','x',' ');
722 else if ( !strcmp( p_attach->psz_mime, "image/png" ) ) /* PNG */
723 type = VLC_FOURCC('p','n','g',' ');
724 else if ( !strcmp( p_attach->psz_mime, "image/tiff" ) ) /* TIF, TIFF */
725 type = VLC_FOURCC('t','i','f','f');
726 else if ( !strcmp( p_attach->psz_mime, "image/x-tga" ) ) /* TGA */
727 type = VLC_FOURCC('t','g','a',' ');
728 else if ( !strcmp( p_attach->psz_mime, "image/x-xpixmap") ) /* XPM */
729 type = VLC_FOURCC('x','p','m',' ');
732 ( p_attach->i_data > 0 ) &&
733 ( p_attach->p_data != NULL ) )
735 picture_t *p_pic = NULL;
736 image_handler_t *p_image;
738 p_image = image_HandlerCreate( p_dec );
739 if( p_image != NULL )
743 p_block = block_New( p_image->p_parent, p_attach->i_data );
745 if( p_block != NULL )
747 video_format_t fmt_in;
748 video_format_t fmt_out;
750 memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data );
752 memset( &fmt_in, 0, sizeof( video_format_t));
753 memset( &fmt_out, 0, sizeof( video_format_t));
755 fmt_in.i_chroma = type;
756 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
758 /* Find a suitable decoder module */
759 if( module_Exists( p_dec, "sdl_image" ) )
761 /* ffmpeg thinks it can handle bmp properly but it can't (at least
762 * not all of them), so use sdl_image if it is available */
766 var_Create( p_dec, "codec", VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
767 val.psz_string = (char*) "sdl_image";
768 var_Set( p_dec, "codec", val );
771 p_pic = image_Read( p_image, p_block, &fmt_in, &fmt_out );
772 var_Destroy( p_dec, "codec" );
775 image_HandlerDelete( p_image );
779 image_attach_t *p_picture = malloc( sizeof(image_attach_t) );
783 p_picture->psz_filename = strdup( p_attach->psz_name );
784 p_picture->p_pic = p_pic;
786 TAB_APPEND( p_sys->i_images, p_sys->pp_images, p_picture );
790 vlc_input_attachment_Delete( pp_attachments[ k ] );
792 free( pp_attachments );
797 char* GotoNextLine( char *psz_text )
799 char *p_newline = psz_text;
801 while( p_newline[0] != '\0' )
803 if( p_newline[0] == '\n' || p_newline[0] == '\r' )
806 while( p_newline[0] == '\n' || p_newline[0] == '\r' )
815 /* Function now handles tags which has attribute values, and tries
816 * to deal with &' commands too. It no longer modifies the string
817 * in place, so that the original text can be reused
819 static char *StripTags( char *psz_subtitle )
821 char *psz_text_start;
824 psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
825 if( !psz_text_start )
828 while( *psz_subtitle )
830 if( *psz_subtitle == '<' )
832 if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
835 psz_subtitle += strcspn( psz_subtitle, ">" );
837 else if( *psz_subtitle == '&' )
839 if( !strncasecmp( psz_subtitle, "<", 4 ))
842 psz_subtitle += strcspn( psz_subtitle, ";" );
844 else if( !strncasecmp( psz_subtitle, ">", 4 ))
847 psz_subtitle += strcspn( psz_subtitle, ";" );
849 else if( !strncasecmp( psz_subtitle, "&", 5 ))
852 psz_subtitle += strcspn( psz_subtitle, ";" );
854 else if( !strncasecmp( psz_subtitle, """, 6 ))
857 psz_subtitle += strcspn( psz_subtitle, ";" );
861 /* Assume it is just a normal ampersand */
867 *psz_text++ = *psz_subtitle;
873 psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
875 return psz_text_start;
878 /* Try to respect any style tags present in the subtitle string. The main
879 * problem here is a lack of adequate specs for the subtitle formats.
880 * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
881 * SAMI has a detailed spec, but extensive rework is needed in the demux
882 * code to prevent all this style information being excised, as it presently
884 * That leaves the others - none of which were (I guess) originally intended
885 * to be carrying style information. Over time people have used them that way.
886 * In the absence of specifications from which to work, the tags supported
887 * have been restricted to the simple set permitted by the USF DTD, ie. :
888 * Basic: <br>, <i>, <b>, <u>
900 * There is also the further restriction that the subtitle be well-formed
901 * as an XML entity, ie. the HTML sentence:
902 * <b><i>Bold and Italics</b></i>
903 * doesn't qualify because the tags aren't nested one inside the other.
904 * <text> tags are automatically added to the output to ensure
906 * If the text doesn't qualify for any reason, a NULL string is
907 * returned, and the rendering engine will fall back to the
908 * plain text version of the subtitle.
910 static char *CreateHtmlSubtitle( char *psz_subtitle )
912 char psz_tagStack[ 100 ];
913 size_t i_buf_size = strlen( psz_subtitle ) + 100;
914 char *psz_html_start = malloc( i_buf_size );
916 psz_tagStack[ 0 ] = '\0';
918 if( psz_html_start != NULL )
920 char *psz_html = psz_html_start;
922 strcpy( psz_html, "<text>" );
925 while( *psz_subtitle )
927 if( *psz_subtitle == '\n' )
929 strcpy( psz_html, "<br/>" );
933 else if( *psz_subtitle == '<' )
935 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
937 strcpy( psz_html, "<br/>" );
941 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
943 strcpy( psz_html, "<b>" );
944 strcat( psz_tagStack, "b" );
948 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
950 strcpy( psz_html, "<i>" );
951 strcat( psz_tagStack, "i" );
955 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
957 strcpy( psz_html, "<u>" );
958 strcat( psz_tagStack, "u" );
962 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
964 const char *psz_attribs[] = { "face=\"", "family=\"", "size=\"",
965 "color=\"", "outline-color=\"", "shadow-color=\"",
966 "outline-level=\"", "shadow-level=\"", "back-color=\"",
969 strcpy( psz_html, "<font " );
970 strcat( psz_tagStack, "f" );
974 while( *psz_subtitle != '>' )
978 for( k=0; psz_attribs[ k ]; k++ )
980 int i_len = strlen( psz_attribs[ k ] );
982 if( !strncasecmp( psz_subtitle, psz_attribs[ k ], i_len ))
984 i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
986 strncpy( psz_html, psz_subtitle, i_len );
988 psz_subtitle += i_len;
992 if( psz_attribs[ k ] == NULL )
994 /* Jump over unrecognised tag */
995 int i_len = strcspn( psz_subtitle, "\"" ) + 1;
997 i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
998 psz_subtitle += i_len;
1000 while (*psz_subtitle == ' ')
1001 *psz_html++ = *psz_subtitle++;
1003 *psz_html++ = *psz_subtitle++;
1005 else if( !strncmp( psz_subtitle, "</", 2 ))
1007 vlc_bool_t b_match = VLC_FALSE;
1008 int i_len = strlen( psz_tagStack ) - 1;
1009 char *psz_lastTag = NULL;
1013 psz_lastTag = psz_tagStack + i_len;
1016 switch( *psz_lastTag )
1019 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
1023 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
1027 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
1031 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
1038 /* Not well formed -- kill everything */
1039 free( psz_html_start );
1040 psz_html_start = NULL;
1043 *psz_lastTag = '\0';
1044 strncpy( psz_html, psz_subtitle, i_len );
1046 psz_subtitle += i_len;
1050 psz_subtitle += strcspn( psz_subtitle, ">" );
1053 else if( *psz_subtitle == '&' )
1055 if( !strncasecmp( psz_subtitle, "<", 4 ))
1057 strcpy( psz_html, "<" );
1061 else if( !strncasecmp( psz_subtitle, ">", 4 ))
1063 strcpy( psz_html, ">" );
1067 else if( !strncasecmp( psz_subtitle, "&", 5 ))
1069 strcpy( psz_html, "&" );
1075 strcpy( psz_html, "&" );
1082 *psz_html = *psz_subtitle;
1083 if( psz_html > psz_html_start )
1085 /* Check for double whitespace */
1086 if((( *psz_html == ' ' ) ||
1087 ( *psz_html == '\t' )) &&
1088 (( *(psz_html-1) == ' ' ) ||
1089 ( *(psz_html-1) == '\t' )))
1091 strcpy( psz_html, NO_BREAKING_SPACE );
1092 psz_html += strlen( NO_BREAKING_SPACE ) - 1;
1099 if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 10 )
1101 int i_len = psz_html - psz_html_start;
1104 psz_html_start = realloc( psz_html_start, i_buf_size );
1105 psz_html = psz_html_start + i_len;
1109 strcpy( psz_html, "</text>" );
1112 if( psz_tagStack[ 0 ] != '\0' )
1114 /* Not well formed -- kill everything */
1115 free( psz_html_start );
1116 psz_html_start = NULL;
1118 else if( psz_html_start )
1120 /* Shrink the memory requirements */
1121 psz_html_start = realloc( psz_html_start, psz_html - psz_html_start + 1 );
1124 return psz_html_start;
1127 /* The reverse of the above function - given a HTML subtitle, turn it
1128 * into a plain-text version, complete with sensible whitespace compaction
1131 char *CreatePlainText( char *psz_subtitle )
1133 char *psz_text = StripTags( psz_subtitle );
1139 s = strpbrk( psz_text, "\t\r\n " );
1144 int i_whitespace = strspn( s, "\t\r\n " );
1146 /* Favour '\n' over other whitespaces - if one of these
1147 * occurs in the whitespace use a '\n' as our value,
1148 * otherwise just use a ' '
1150 for( k = 0; k < i_whitespace; k++ )
1151 if( s[k] == '\n' ) spc = '\n';
1153 if( i_whitespace > 1 )
1157 strlen( s ) - i_whitespace + 1 );
1161 s = strpbrk( s, "\t\r\n " );
1166 /****************************************************************************
1167 * download and resize image located at psz_url
1168 ***************************************************************************/
1169 subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec,
1170 subpicture_t *p_spu,
1171 const char *psz_filename,
1172 int i_transparent_color )
1174 decoder_sys_t *p_sys = p_dec->p_sys;
1175 subpicture_region_t *p_region;
1176 video_format_t fmt_out;
1178 picture_t *p_pic = NULL;
1180 for( k = 0; k < p_sys->i_images; k++ )
1182 if( p_sys->pp_images &&
1183 !strcmp( p_sys->pp_images[k]->psz_filename, psz_filename ) )
1185 p_pic = p_sys->pp_images[k]->p_pic;
1192 msg_Err( p_dec, "Unable to read image %s", psz_filename );
1196 /* Display the feed's image */
1197 memset( &fmt_out, 0, sizeof( video_format_t));
1199 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
1200 fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
1201 fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
1203 fmt_out.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
1205 fmt_out.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
1207 p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt_out );
1210 msg_Err( p_dec, "cannot allocate SPU region" );
1213 vout_CopyPicture( p_dec, &p_region->picture, p_pic );
1215 /* This isn't the best way to do this - if you really want transparency, then
1216 * you're much better off using an image type that supports it like PNG. The
1217 * spec requires this support though.
1219 if( i_transparent_color > 0 )
1221 uint8_t i_r = ( i_transparent_color >> 16 ) & 0xff;
1222 uint8_t i_g = ( i_transparent_color >> 8 ) & 0xff;
1223 uint8_t i_b = ( i_transparent_color ) & 0xff;
1224 uint8_t i_y = ( ( ( 66 * i_r + 129 * i_g + 25 * i_b + 128 ) >> 8 ) + 16 );
1225 uint8_t i_u = ( ( -38 * i_r - 74 * i_g + 112 * i_b + 128 ) >> 8 ) + 128 ;
1226 uint8_t i_v = ( ( 112 * i_r - 94 * i_g - 18 * i_b + 128 ) >> 8 ) + 128 ;
1228 if( ( p_region->picture.Y_PITCH == p_region->picture.U_PITCH ) &&
1229 ( p_region->picture.Y_PITCH == p_region->picture.V_PITCH ) &&
1230 ( p_region->picture.Y_PITCH == p_region->picture.A_PITCH ) )
1232 int i_lines = p_region->picture.p[ Y_PLANE ].i_lines;
1233 if( i_lines > p_region->picture.p[ U_PLANE ].i_lines )
1234 i_lines = p_region->picture.p[ U_PLANE ].i_lines;
1235 if( i_lines > p_region->picture.p[ V_PLANE ].i_lines )
1236 i_lines = p_region->picture.p[ V_PLANE ].i_lines;
1237 if( i_lines > p_region->picture.p[ A_PLANE ].i_lines )
1238 i_lines = p_region->picture.p[ A_PLANE ].i_lines;
1242 for( i = 0; i < p_region->picture.A_PITCH * i_lines; i++ )
1244 if(( p_region->picture.Y_PIXELS[ i ] == i_y ) &&
1245 ( p_region->picture.U_PIXELS[ i ] == i_u ) &&
1246 ( p_region->picture.V_PIXELS[ i ] == i_v ) )
1248 p_region->picture.A_PIXELS[ i ] = 1;