/src/vlc/modules/codec/subsusf.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * subsusf.c : USF subtitles decoder |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2000-2006 VLC authors and VideoLAN |
5 | | * |
6 | | * Authors: Bernie Purcell <bitmap@videolan.org> |
7 | | * |
8 | | * This program is free software; you can redistribute it and/or modify it |
9 | | * under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation; either version 2.1 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program; if not, write to the Free Software Foundation, |
20 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
21 | | *****************************************************************************/ |
22 | | #ifdef HAVE_CONFIG_H |
23 | | # include "config.h" |
24 | | #endif |
25 | | #include <assert.h> |
26 | | |
27 | | #include <vlc_common.h> |
28 | | #include <vlc_arrays.h> |
29 | | #include <vlc_plugin.h> |
30 | | #include <vlc_modules.h> |
31 | | #include <vlc_codec.h> |
32 | | #include <vlc_input.h> |
33 | | #include <vlc_charset.h> |
34 | | #include <vlc_image.h> |
35 | | #include <vlc_xml.h> |
36 | | #include <vlc_stream.h> |
37 | | |
38 | | /***************************************************************************** |
39 | | * Module descriptor. |
40 | | *****************************************************************************/ |
41 | | static int OpenDecoder ( vlc_object_t * ); |
42 | | static void CloseDecoder ( vlc_object_t * ); |
43 | | |
44 | | #define FORMAT_TEXT N_("Formatted Subtitles") |
45 | | #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \ |
46 | | "VLC partly implements this, but you can choose to disable all formatting.") |
47 | | |
48 | 108 | vlc_module_begin () |
49 | 54 | set_capability( "spu decoder", 40 ) |
50 | 54 | set_shortname( N_("USFSubs")) |
51 | 54 | set_description( N_("USF subtitles decoder") ) |
52 | 108 | set_callbacks( OpenDecoder, CloseDecoder ) |
53 | 54 | set_subcategory( SUBCAT_INPUT_SCODEC ) |
54 | 54 | add_bool( "subsdec-formatted", true, FORMAT_TEXT, FORMAT_LONGTEXT ) |
55 | 54 | vlc_module_end () |
56 | | |
57 | | |
58 | | /***************************************************************************** |
59 | | * Local prototypes |
60 | | *****************************************************************************/ |
61 | | enum |
62 | | { |
63 | | ATTRIBUTE_ALIGNMENT = (1 << 0), |
64 | | ATTRIBUTE_X = (1 << 1), |
65 | | ATTRIBUTE_X_PERCENT = (1 << 2), |
66 | | ATTRIBUTE_Y = (1 << 3), |
67 | | ATTRIBUTE_Y_PERCENT = (1 << 4), |
68 | | ATTRIBUTE_REL_WINDOW = (1 << 5), |
69 | | }; |
70 | | |
71 | | typedef struct |
72 | | { |
73 | | char *psz_filename; |
74 | | picture_t *p_pic; |
75 | | } image_attach_t; |
76 | | |
77 | | typedef struct |
78 | | { |
79 | | char * psz_stylename; /* The name of the style, no comma's allowed */ |
80 | | text_style_t * p_style; |
81 | | int i_align; |
82 | | int i_margin_h; |
83 | | int i_margin_v; |
84 | | int i_margin_percent_h; |
85 | | int i_margin_percent_v; |
86 | | } ssa_style_t; |
87 | | |
88 | | typedef struct |
89 | | { |
90 | | int i_original_height; |
91 | | int i_original_width; |
92 | | int i_align; /* Subtitles alignment on the vout */ |
93 | | |
94 | | ssa_style_t **pp_ssa_styles; |
95 | | int i_ssa_styles; |
96 | | |
97 | | image_attach_t **pp_images; |
98 | | int i_images; |
99 | | } decoder_sys_t; |
100 | | |
101 | | static int DecodeBlock ( decoder_t *, block_t * ); |
102 | | static char *CreatePlainText( char * ); |
103 | | static char *StripTags( char *psz_subtitle ); |
104 | | static int ParseImageAttachments( decoder_t *p_dec ); |
105 | | |
106 | | static subpicture_t *ParseText ( decoder_t *, block_t * ); |
107 | | static void ParseUSFHeader( decoder_t * ); |
108 | | static void ParseUSFString( decoder_t *, char *, vlc_spu_regions * ); |
109 | | static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec, const char *psz_filename, int i_transparent_color ); |
110 | | |
111 | | /***************************************************************************** |
112 | | * OpenDecoder: probe the decoder and return score |
113 | | ***************************************************************************** |
114 | | * Tries to launch a decoder and return score so that the interface is able |
115 | | * to chose. |
116 | | *****************************************************************************/ |
117 | | static int OpenDecoder( vlc_object_t *p_this ) |
118 | 12.6k | { |
119 | 12.6k | decoder_t *p_dec = (decoder_t*)p_this; |
120 | 12.6k | decoder_sys_t *p_sys; |
121 | | |
122 | 12.6k | if( p_dec->fmt_in->i_codec != VLC_CODEC_USF ) |
123 | 12.5k | return VLC_EGENERIC; |
124 | | |
125 | | /* Allocate the memory needed to store the decoder's structure */ |
126 | 11 | if( ( p_dec->p_sys = p_sys = calloc(1, sizeof(decoder_sys_t)) ) == NULL ) |
127 | 0 | return VLC_ENOMEM; |
128 | | |
129 | 11 | p_dec->pf_decode = DecodeBlock; |
130 | 11 | p_dec->fmt_out.i_codec = 0; |
131 | | |
132 | | /* init of p_sys */ |
133 | 11 | TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles ); |
134 | 11 | TAB_INIT( p_sys->i_images, p_sys->pp_images ); |
135 | | |
136 | | /* USF subtitles are mandated to be UTF-8, so don't need vlc_iconv */ |
137 | | |
138 | 11 | p_sys->i_align = var_CreateGetInteger( p_dec, "subsdec-align" ); |
139 | | |
140 | 11 | ParseImageAttachments( p_dec ); |
141 | | |
142 | 11 | if( var_CreateGetBool( p_dec, "subsdec-formatted" ) ) |
143 | 11 | { |
144 | 11 | if( p_dec->fmt_in->i_extra > 0 ) |
145 | 0 | ParseUSFHeader( p_dec ); |
146 | 11 | } |
147 | | |
148 | 11 | return VLC_SUCCESS; |
149 | 11 | } |
150 | | |
151 | | /**************************************************************************** |
152 | | * DecodeBlock: the whole thing |
153 | | **************************************************************************** |
154 | | * This function must be fed with complete subtitles units. |
155 | | ****************************************************************************/ |
156 | | static int DecodeBlock( decoder_t *p_dec, block_t *p_block ) |
157 | 8.11k | { |
158 | 8.11k | subpicture_t *p_spu; |
159 | | |
160 | 8.11k | if( p_block == NULL ) /* No Drain */ |
161 | 4.06k | return VLCDEC_SUCCESS; |
162 | | |
163 | 4.05k | p_spu = ParseText( p_dec, p_block ); |
164 | | |
165 | 4.05k | block_Release( p_block ); |
166 | 4.05k | if( p_spu != NULL ) |
167 | 0 | decoder_QueueSub( p_dec, p_spu ); |
168 | 4.05k | return VLCDEC_SUCCESS; |
169 | 8.11k | } |
170 | | |
171 | | /***************************************************************************** |
172 | | * CloseDecoder: clean up the decoder |
173 | | *****************************************************************************/ |
174 | | static void CloseDecoder( vlc_object_t *p_this ) |
175 | 11 | { |
176 | 11 | decoder_t *p_dec = (decoder_t *)p_this; |
177 | 11 | decoder_sys_t *p_sys = p_dec->p_sys; |
178 | | |
179 | 11 | if( p_sys->pp_ssa_styles ) |
180 | 0 | { |
181 | 0 | for( int i = 0; i < p_sys->i_ssa_styles; i++ ) |
182 | 0 | { |
183 | 0 | if( !p_sys->pp_ssa_styles[i] ) |
184 | 0 | continue; |
185 | | |
186 | 0 | free( p_sys->pp_ssa_styles[i]->psz_stylename ); |
187 | 0 | text_style_Delete( p_sys->pp_ssa_styles[i]->p_style ); |
188 | 0 | free( p_sys->pp_ssa_styles[i] ); |
189 | 0 | } |
190 | 0 | TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles ); |
191 | 0 | } |
192 | 11 | if( p_sys->pp_images ) |
193 | 0 | { |
194 | 0 | for( int i = 0; i < p_sys->i_images; i++ ) |
195 | 0 | { |
196 | 0 | if( !p_sys->pp_images[i] ) |
197 | 0 | continue; |
198 | | |
199 | 0 | if( p_sys->pp_images[i]->p_pic ) |
200 | 0 | picture_Release( p_sys->pp_images[i]->p_pic ); |
201 | 0 | free( p_sys->pp_images[i]->psz_filename ); |
202 | |
|
203 | 0 | free( p_sys->pp_images[i] ); |
204 | 0 | } |
205 | 0 | TAB_CLEAN( p_sys->i_images, p_sys->pp_images ); |
206 | 0 | } |
207 | | |
208 | 11 | free( p_sys ); |
209 | 11 | } |
210 | | |
211 | | /***************************************************************************** |
212 | | * ParseText: parse an text subtitle packet and send it to the video output |
213 | | *****************************************************************************/ |
214 | | static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block ) |
215 | 4.05k | { |
216 | 4.05k | decoder_sys_t *p_sys = p_dec->p_sys; |
217 | 4.05k | subpicture_t *p_spu = NULL; |
218 | 4.05k | char *psz_subtitle = NULL; |
219 | | |
220 | 4.05k | if( p_block->i_flags & BLOCK_FLAG_CORRUPTED ) |
221 | 0 | return NULL; |
222 | | |
223 | | /* We cannot display a subpicture with no date */ |
224 | 4.05k | if( p_block->i_pts == VLC_TICK_INVALID ) |
225 | 0 | { |
226 | 0 | msg_Warn( p_dec, "subtitle without a date" ); |
227 | 0 | return NULL; |
228 | 0 | } |
229 | | |
230 | | /* Check validity of packet data */ |
231 | | /* An "empty" line containing only \0 can be used to force |
232 | | and ephemer picture from the screen */ |
233 | 4.05k | if( p_block->i_buffer < 1 ) |
234 | 4.05k | { |
235 | 4.05k | msg_Warn( p_dec, "no subtitle data" ); |
236 | 4.05k | return NULL; |
237 | 4.05k | } |
238 | | |
239 | | /* Should be resiliant against bad subtitles */ |
240 | 0 | psz_subtitle = strndup( (const char *)p_block->p_buffer, |
241 | 0 | p_block->i_buffer ); |
242 | 0 | if( psz_subtitle == NULL ) |
243 | 0 | return NULL; |
244 | | |
245 | | /* USF Subtitles are mandated to be UTF-8 -- make sure it is */ |
246 | 0 | if (EnsureUTF8( psz_subtitle ) == NULL) |
247 | 0 | { |
248 | 0 | msg_Err( p_dec, "USF subtitles must be in UTF-8 format.\n" |
249 | 0 | "This stream contains USF subtitles which aren't." ); |
250 | 0 | } |
251 | | |
252 | | /* Create the subpicture unit */ |
253 | 0 | p_spu = decoder_NewSubpicture( p_dec, NULL ); |
254 | 0 | if( !p_spu ) |
255 | 0 | { |
256 | 0 | msg_Warn( p_dec, "can't get spu buffer" ); |
257 | 0 | free( psz_subtitle ); |
258 | 0 | return NULL; |
259 | 0 | } |
260 | | |
261 | | /* Decode USF strings */ |
262 | 0 | ParseUSFString( p_dec, psz_subtitle, &p_spu->regions ); |
263 | |
|
264 | 0 | p_spu->i_start = p_block->i_pts; |
265 | 0 | p_spu->i_stop = p_block->i_pts + p_block->i_length; |
266 | 0 | p_spu->b_ephemer = (p_block->i_length == 0); |
267 | 0 | if (p_sys->i_original_width > 0 && p_sys->i_original_height > 0) |
268 | 0 | { |
269 | 0 | p_spu->i_original_picture_width = p_sys->i_original_width; |
270 | 0 | p_spu->i_original_picture_height = p_sys->i_original_height; |
271 | 0 | } |
272 | |
|
273 | 0 | free( psz_subtitle ); |
274 | |
|
275 | 0 | return p_spu; |
276 | 0 | } |
277 | | |
278 | | static char *GrabAttributeValue( const char *psz_attribute, |
279 | | const char *psz_tag_start ) |
280 | 0 | { |
281 | 0 | if( psz_attribute && psz_tag_start ) |
282 | 0 | { |
283 | 0 | char *psz_tag_end = strchr( psz_tag_start, '>' ); |
284 | 0 | char *psz_found = strcasestr( psz_tag_start, psz_attribute ); |
285 | |
|
286 | 0 | if( psz_found ) |
287 | 0 | { |
288 | 0 | psz_found += strlen( psz_attribute ); |
289 | |
|
290 | 0 | if(( *(psz_found++) == '=' ) && |
291 | 0 | ( *(psz_found++) == '\"' )) |
292 | 0 | { |
293 | 0 | if( psz_found < psz_tag_end ) |
294 | 0 | { |
295 | 0 | int i_len = strcspn( psz_found, "\"" ); |
296 | 0 | return strndup( psz_found, i_len ); |
297 | 0 | } |
298 | 0 | } |
299 | 0 | } |
300 | 0 | } |
301 | 0 | return NULL; |
302 | 0 | } |
303 | | |
304 | | static ssa_style_t *ParseStyle( decoder_sys_t *p_sys, char *psz_subtitle ) |
305 | 0 | { |
306 | 0 | ssa_style_t *p_ssa_style = NULL; |
307 | 0 | char *psz_style = GrabAttributeValue( "style", psz_subtitle ); |
308 | |
|
309 | 0 | if( psz_style ) |
310 | 0 | { |
311 | 0 | for( int i = 0; i < p_sys->i_ssa_styles; i++ ) |
312 | 0 | { |
313 | 0 | if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) ) |
314 | 0 | p_ssa_style = p_sys->pp_ssa_styles[i]; |
315 | 0 | } |
316 | 0 | free( psz_style ); |
317 | 0 | } |
318 | 0 | return p_ssa_style; |
319 | 0 | } |
320 | | |
321 | | static int ParsePositionAttributeList( char *psz_subtitle, int *i_align, |
322 | | int *i_x, int *i_y ) |
323 | 0 | { |
324 | 0 | int i_mask = 0; |
325 | |
|
326 | 0 | char *psz_align = GrabAttributeValue( "alignment", psz_subtitle ); |
327 | 0 | char *psz_margin_x = GrabAttributeValue( "horizontal-margin", psz_subtitle ); |
328 | 0 | char *psz_margin_y = GrabAttributeValue( "vertical-margin", psz_subtitle ); |
329 | 0 | char *psz_relative = GrabAttributeValue( "relative-to", psz_subtitle ); |
330 | | /* -- UNSUPPORTED |
331 | | char *psz_rotate_x = GrabAttributeValue( "rotate-x", psz_subtitle ); |
332 | | char *psz_rotate_y = GrabAttributeValue( "rotate-y", psz_subtitle ); |
333 | | char *psz_rotate_z = GrabAttributeValue( "rotate-z", psz_subtitle ); |
334 | | */ |
335 | |
|
336 | 0 | *i_align = SUBPICTURE_ALIGN_BOTTOM; |
337 | 0 | *i_x = 0; |
338 | 0 | *i_y = 0; |
339 | |
|
340 | 0 | if( psz_align ) |
341 | 0 | { |
342 | 0 | if( !strcasecmp( "TopLeft", psz_align ) ) |
343 | 0 | *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT; |
344 | 0 | else if( !strcasecmp( "TopCenter", psz_align ) ) |
345 | 0 | *i_align = SUBPICTURE_ALIGN_TOP; |
346 | 0 | else if( !strcasecmp( "TopRight", psz_align ) ) |
347 | 0 | *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT; |
348 | 0 | else if( !strcasecmp( "MiddleLeft", psz_align ) ) |
349 | 0 | *i_align = SUBPICTURE_ALIGN_LEFT; |
350 | 0 | else if( !strcasecmp( "MiddleCenter", psz_align ) ) |
351 | 0 | *i_align = 0; |
352 | 0 | else if( !strcasecmp( "MiddleRight", psz_align ) ) |
353 | 0 | *i_align = SUBPICTURE_ALIGN_RIGHT; |
354 | 0 | else if( !strcasecmp( "BottomLeft", psz_align ) ) |
355 | 0 | *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT; |
356 | 0 | else if( !strcasecmp( "BottomCenter", psz_align ) ) |
357 | 0 | *i_align = SUBPICTURE_ALIGN_BOTTOM; |
358 | 0 | else if( !strcasecmp( "BottomRight", psz_align ) ) |
359 | 0 | *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT; |
360 | |
|
361 | 0 | i_mask |= ATTRIBUTE_ALIGNMENT; |
362 | 0 | free( psz_align ); |
363 | 0 | } |
364 | 0 | if( psz_margin_x ) |
365 | 0 | { |
366 | 0 | *i_x = atoi( psz_margin_x ); |
367 | 0 | if( strchr( psz_margin_x, '%' ) ) |
368 | 0 | i_mask |= ATTRIBUTE_X_PERCENT; |
369 | 0 | else |
370 | 0 | i_mask |= ATTRIBUTE_X; |
371 | |
|
372 | 0 | free( psz_margin_x ); |
373 | 0 | } |
374 | 0 | if( psz_margin_y ) |
375 | 0 | { |
376 | 0 | *i_y = atoi( psz_margin_y ); |
377 | 0 | if( strchr( psz_margin_y, '%' ) ) |
378 | 0 | i_mask |= ATTRIBUTE_Y_PERCENT; |
379 | 0 | else |
380 | 0 | i_mask |= ATTRIBUTE_Y; |
381 | |
|
382 | 0 | free( psz_margin_y ); |
383 | 0 | } |
384 | 0 | if( psz_relative ) |
385 | 0 | { |
386 | 0 | if( !strcasecmp( "Window", psz_relative ) ) |
387 | 0 | i_mask |= ATTRIBUTE_REL_WINDOW; |
388 | |
|
389 | 0 | free( psz_relative ); |
390 | 0 | } |
391 | 0 | return i_mask; |
392 | 0 | } |
393 | | |
394 | | static void SetupPositions( subpicture_region_t *p_region, char *psz_subtitle ) |
395 | 0 | { |
396 | 0 | int i_mask = 0; |
397 | 0 | int i_align; |
398 | 0 | int i_x, i_y; |
399 | |
|
400 | 0 | i_mask = ParsePositionAttributeList( psz_subtitle, &i_align, &i_x, &i_y ); |
401 | |
|
402 | 0 | p_region->b_absolute = false; |
403 | 0 | if( i_mask & ATTRIBUTE_REL_WINDOW ) |
404 | 0 | p_region->b_in_window = true; |
405 | 0 | else |
406 | 0 | p_region->b_in_window = false; |
407 | 0 | if( i_mask & ATTRIBUTE_ALIGNMENT ) |
408 | 0 | p_region->i_align = i_align; |
409 | | |
410 | | /* TODO: Setup % based offsets properly, without adversely affecting |
411 | | * everything else in vlc. Will address with separate patch, to |
412 | | * prevent this one being any more complicated. |
413 | | */ |
414 | 0 | if( i_mask & ATTRIBUTE_X ) |
415 | 0 | p_region->i_x = i_x; |
416 | 0 | else if( i_mask & ATTRIBUTE_X_PERCENT ) |
417 | 0 | p_region->i_x = 0; |
418 | |
|
419 | 0 | if( i_mask & ATTRIBUTE_Y ) |
420 | 0 | p_region->i_y = i_y; |
421 | 0 | else if( i_mask & ATTRIBUTE_Y_PERCENT ) |
422 | 0 | p_region->i_y = 0; |
423 | 0 | } |
424 | | |
425 | | static subpicture_region_t *CreateTextRegion( decoder_t *p_dec, |
426 | | char *psz_subtitle, |
427 | | char *psz_plaintext, |
428 | | int i_sys_align ) |
429 | 0 | { |
430 | 0 | decoder_sys_t *p_sys = p_dec->p_sys; |
431 | 0 | subpicture_region_t *p_text_region; |
432 | | |
433 | | /* Create a new subpicture region */ |
434 | 0 | p_text_region = subpicture_region_NewText(); |
435 | 0 | if( p_text_region == NULL ) |
436 | 0 | return NULL; |
437 | | |
438 | 0 | ssa_style_t *p_ssa_style = NULL; |
439 | |
|
440 | 0 | p_ssa_style = ParseStyle( p_sys, psz_subtitle ); |
441 | 0 | if( !p_ssa_style ) |
442 | 0 | { |
443 | 0 | for( int i = 0; i < p_sys->i_ssa_styles; i++ ) |
444 | 0 | { |
445 | 0 | if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) ) |
446 | 0 | p_ssa_style = p_sys->pp_ssa_styles[i]; |
447 | 0 | } |
448 | 0 | } |
449 | | |
450 | | /* Set default or user align/margin. |
451 | | * Style overridden if no user value. */ |
452 | 0 | p_text_region->i_x = i_sys_align > 0 ? 20 : 0; |
453 | 0 | p_text_region->i_y = 10; |
454 | 0 | p_text_region->i_align = SUBPICTURE_ALIGN_BOTTOM | |
455 | 0 | ((i_sys_align > 0) ? i_sys_align : 0); |
456 | |
|
457 | 0 | if( p_ssa_style ) |
458 | 0 | { |
459 | 0 | msg_Dbg( p_dec, "style is: %s", p_ssa_style->psz_stylename ); |
460 | | |
461 | | /* TODO: Setup % based offsets properly, without adversely affecting |
462 | | * everything else in vlc. Will address with separate patch, |
463 | | * to prevent this one being any more complicated. |
464 | | |
465 | | * p_ssa_style->i_margin_percent_h; |
466 | | * p_ssa_style->i_margin_percent_v; |
467 | | */ |
468 | 0 | if( i_sys_align == -1 ) |
469 | 0 | { |
470 | 0 | p_text_region->i_align = p_ssa_style->i_align; |
471 | 0 | p_text_region->i_x = p_ssa_style->i_margin_h; |
472 | 0 | p_text_region->i_y = p_ssa_style->i_margin_v; |
473 | 0 | } |
474 | 0 | p_text_region->p_text = text_segment_NewInheritStyle( p_ssa_style->p_style ); |
475 | 0 | } |
476 | 0 | else |
477 | 0 | { |
478 | 0 | p_text_region->p_text = text_segment_New( NULL ); |
479 | 0 | } |
480 | |
|
481 | 0 | p_text_region->p_text->psz_text = psz_plaintext; |
482 | |
|
483 | 0 | return p_text_region; |
484 | 0 | } |
485 | | |
486 | | static int ParseImageAttachments( decoder_t *p_dec ) |
487 | 11 | { |
488 | 11 | decoder_sys_t *p_sys = p_dec->p_sys; |
489 | 11 | input_attachment_t **pp_attachments; |
490 | 11 | int i_attachments_cnt; |
491 | | |
492 | 11 | if( VLC_SUCCESS != decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments_cnt )) |
493 | 11 | return VLC_EGENERIC; |
494 | | |
495 | 0 | for( int k = 0; k < i_attachments_cnt; k++ ) |
496 | 0 | { |
497 | 0 | input_attachment_t *p_attach = pp_attachments[k]; |
498 | |
|
499 | 0 | vlc_fourcc_t type = image_Mime2Fourcc( p_attach->psz_mime ); |
500 | |
|
501 | 0 | if( ( type != 0 ) && |
502 | 0 | ( p_attach->i_data > 0 ) && |
503 | 0 | ( p_attach->p_data != NULL ) ) |
504 | 0 | { |
505 | 0 | picture_t *p_pic = NULL; |
506 | 0 | image_handler_t *p_image; |
507 | |
|
508 | 0 | p_image = image_HandlerCreate( p_dec ); |
509 | 0 | if( p_image != NULL ) |
510 | 0 | { |
511 | 0 | block_t *p_block; |
512 | |
|
513 | 0 | p_block = block_Alloc( p_attach->i_data ); |
514 | |
|
515 | 0 | if( p_block != NULL ) |
516 | 0 | { |
517 | 0 | es_format_t es_in; |
518 | 0 | video_format_t fmt_out; |
519 | |
|
520 | 0 | memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data ); |
521 | |
|
522 | 0 | es_format_Init( &es_in, VIDEO_ES, type ); |
523 | 0 | es_in.video.i_chroma = type; |
524 | 0 | video_format_Init( &fmt_out, VLC_CODEC_YUVA ); |
525 | |
|
526 | 0 | p_pic = image_Read( p_image, p_block, &es_in, &fmt_out ); |
527 | 0 | es_format_Clean( &es_in ); |
528 | 0 | video_format_Clean( &fmt_out ); |
529 | 0 | } |
530 | |
|
531 | 0 | image_HandlerDelete( p_image ); |
532 | 0 | } |
533 | 0 | if( p_pic ) |
534 | 0 | { |
535 | 0 | image_attach_t *p_picture = malloc( sizeof(image_attach_t) ); |
536 | |
|
537 | 0 | if( p_picture ) |
538 | 0 | { |
539 | 0 | p_picture->psz_filename = strdup( p_attach->psz_name ); |
540 | 0 | p_picture->p_pic = p_pic; |
541 | |
|
542 | 0 | TAB_APPEND( p_sys->i_images, p_sys->pp_images, p_picture ); |
543 | 0 | } |
544 | 0 | } |
545 | 0 | } |
546 | 0 | vlc_input_attachment_Release( pp_attachments[ k ] ); |
547 | 0 | } |
548 | 0 | free( pp_attachments ); |
549 | |
|
550 | 0 | return VLC_SUCCESS; |
551 | 0 | } |
552 | | |
553 | | static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader ) |
554 | 0 | { |
555 | 0 | decoder_sys_t *p_sys = p_dec->p_sys; |
556 | 0 | const char *node; |
557 | 0 | ssa_style_t *p_ssa_style = NULL; |
558 | 0 | int i_style_level = 0; |
559 | 0 | int i_metadata_level = 0; |
560 | 0 | int type; |
561 | |
|
562 | 0 | while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 ) |
563 | 0 | { |
564 | 0 | switch( type ) |
565 | 0 | { |
566 | 0 | case XML_READER_ENDELEM: |
567 | 0 | switch (i_style_level) |
568 | 0 | { |
569 | 0 | case 0: |
570 | 0 | if( !strcasecmp( "metadata", node ) && (i_metadata_level == 1) ) |
571 | 0 | i_metadata_level--; |
572 | 0 | break; |
573 | 0 | case 1: |
574 | 0 | if( !strcasecmp( "styles", node ) ) |
575 | 0 | i_style_level--; |
576 | 0 | break; |
577 | 0 | case 2: |
578 | 0 | if( !strcasecmp( "style", node ) ) |
579 | 0 | { |
580 | 0 | TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_ssa_style ); |
581 | | |
582 | 0 | p_ssa_style = NULL; |
583 | 0 | i_style_level--; |
584 | 0 | } |
585 | 0 | break; |
586 | 0 | } |
587 | 0 | break; |
588 | | |
589 | 0 | case XML_READER_STARTELEM: |
590 | 0 | if( !strcasecmp( "metadata", node ) && (i_style_level == 0) ) |
591 | 0 | i_metadata_level++; |
592 | 0 | else if( !strcasecmp( "resolution", node ) && |
593 | 0 | ( i_metadata_level == 1) ) |
594 | 0 | { |
595 | 0 | const char *attr, *val; |
596 | 0 | while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) ) |
597 | 0 | { |
598 | 0 | if( !strcasecmp( "x", attr ) ) |
599 | 0 | p_sys->i_original_width = atoi( val ); |
600 | 0 | else if( !strcasecmp( "y", attr ) ) |
601 | 0 | p_sys->i_original_height = atoi( val ); |
602 | 0 | } |
603 | 0 | } |
604 | 0 | else if( !strcasecmp( "styles", node ) && (i_style_level == 0) ) |
605 | 0 | { |
606 | 0 | i_style_level++; |
607 | 0 | } |
608 | 0 | else if( !strcasecmp( "style", node ) && (i_style_level == 1) ) |
609 | 0 | { |
610 | 0 | i_style_level++; |
611 | |
|
612 | 0 | p_ssa_style = calloc( 1, sizeof(ssa_style_t) ); |
613 | 0 | if( unlikely(!p_ssa_style) ) |
614 | 0 | return; |
615 | 0 | p_ssa_style->p_style = text_style_Create( STYLE_NO_DEFAULTS ); |
616 | 0 | if( unlikely(!p_ssa_style->p_style) ) |
617 | 0 | { |
618 | 0 | free(p_ssa_style); |
619 | 0 | return; |
620 | 0 | } |
621 | | /* All styles are supposed to default to Default, and then |
622 | | * one or more settings are over-ridden. |
623 | | * At the moment this only effects styles defined AFTER |
624 | | * Default in the XML |
625 | | */ |
626 | 0 | for( int i = 0; i < p_sys->i_ssa_styles; i++ ) |
627 | 0 | { |
628 | 0 | if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) ) |
629 | 0 | { |
630 | 0 | ssa_style_t *p_default_style = p_sys->pp_ssa_styles[i]; |
631 | 0 | text_style_t *p_orig_text_style = p_ssa_style->p_style; |
632 | |
|
633 | 0 | memcpy( p_ssa_style, p_default_style, sizeof( ssa_style_t ) ); |
634 | | |
635 | | // reset data-members that are not to be overwritten |
636 | 0 | p_ssa_style->p_style = p_orig_text_style; |
637 | 0 | p_ssa_style->psz_stylename = NULL; |
638 | | |
639 | | //FIXME: Make font_style a pointer. Actually we double copy some data here, |
640 | | // we use text_style_Copy to avoid copying psz_fontname, though . |
641 | 0 | text_style_Copy( p_ssa_style->p_style, p_default_style->p_style ); |
642 | 0 | } |
643 | 0 | } |
644 | |
|
645 | 0 | const char *attr, *val; |
646 | 0 | while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) ) |
647 | 0 | { |
648 | 0 | if( !strcasecmp( "name", attr ) ) |
649 | 0 | { |
650 | 0 | free( p_ssa_style->psz_stylename ); |
651 | 0 | p_ssa_style->psz_stylename = strdup( val ); |
652 | 0 | } |
653 | 0 | } |
654 | 0 | } |
655 | 0 | else if( !strcasecmp( "fontstyle", node ) && (i_style_level == 2) ) |
656 | 0 | { |
657 | 0 | const char *attr, *val; |
658 | 0 | while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) ) |
659 | 0 | { |
660 | 0 | if( !strcasecmp( "face", attr ) ) |
661 | 0 | { |
662 | 0 | free( p_ssa_style->p_style->psz_fontname ); |
663 | 0 | p_ssa_style->p_style->psz_fontname = strdup( val ); |
664 | 0 | } |
665 | 0 | else if( !strcasecmp( "size", attr ) ) |
666 | 0 | { |
667 | 0 | if( ( *val == '+' ) || ( *val == '-' ) ) |
668 | 0 | { |
669 | 0 | int i_value = atoi( val ); |
670 | |
|
671 | 0 | if( ( i_value >= -5 ) && ( i_value <= 5 ) ) |
672 | 0 | p_ssa_style->p_style->i_font_size += |
673 | 0 | ( i_value * p_ssa_style->p_style->i_font_size ) / 10; |
674 | 0 | else if( i_value < -5 ) |
675 | 0 | p_ssa_style->p_style->i_font_size = - i_value; |
676 | 0 | else if( i_value > 5 ) |
677 | 0 | p_ssa_style->p_style->i_font_size = i_value; |
678 | 0 | } |
679 | 0 | else |
680 | 0 | p_ssa_style->p_style->i_font_size = atoi( val ); |
681 | 0 | } |
682 | 0 | else if( !strcasecmp( "italic", attr ) ) |
683 | 0 | { |
684 | 0 | if( !strcasecmp( "yes", val )) |
685 | 0 | p_ssa_style->p_style->i_style_flags |= STYLE_ITALIC; |
686 | 0 | else |
687 | 0 | p_ssa_style->p_style->i_style_flags &= ~STYLE_ITALIC; |
688 | 0 | p_ssa_style->p_style->i_features |= STYLE_HAS_FLAGS; |
689 | 0 | } |
690 | 0 | else if( !strcasecmp( "weight", attr ) ) |
691 | 0 | { |
692 | 0 | if( !strcasecmp( "bold", val )) |
693 | 0 | p_ssa_style->p_style->i_style_flags |= STYLE_BOLD; |
694 | 0 | else |
695 | 0 | p_ssa_style->p_style->i_style_flags &= ~STYLE_BOLD; |
696 | 0 | p_ssa_style->p_style->i_features |= STYLE_HAS_FLAGS; |
697 | 0 | } |
698 | 0 | else if( !strcasecmp( "underline", attr ) ) |
699 | 0 | { |
700 | 0 | if( !strcasecmp( "yes", val )) |
701 | 0 | p_ssa_style->p_style->i_style_flags |= STYLE_UNDERLINE; |
702 | 0 | else |
703 | 0 | p_ssa_style->p_style->i_style_flags &= ~STYLE_UNDERLINE; |
704 | 0 | p_ssa_style->p_style->i_features |= STYLE_HAS_FLAGS; |
705 | 0 | } |
706 | 0 | else if( !strcasecmp( "color", attr ) ) |
707 | 0 | { |
708 | 0 | if( *val == '#' ) |
709 | 0 | { |
710 | 0 | unsigned long col = strtol(val+1, NULL, 16); |
711 | 0 | p_ssa_style->p_style->i_font_color = (col & 0x00ffffff); |
712 | 0 | p_ssa_style->p_style->i_font_alpha = (col >> 24) & 0xff; |
713 | 0 | p_ssa_style->p_style->i_features |= STYLE_HAS_FONT_COLOR |
714 | 0 | | STYLE_HAS_FONT_ALPHA; |
715 | 0 | } |
716 | 0 | } |
717 | 0 | else if( !strcasecmp( "outline-color", attr ) ) |
718 | 0 | { |
719 | 0 | if( *val == '#' ) |
720 | 0 | { |
721 | 0 | unsigned long col = strtol(val+1, NULL, 16); |
722 | 0 | p_ssa_style->p_style->i_outline_color = (col & 0x00ffffff); |
723 | 0 | p_ssa_style->p_style->i_outline_alpha = (col >> 24) & 0xff; |
724 | 0 | p_ssa_style->p_style->i_features |= STYLE_HAS_OUTLINE_COLOR |
725 | 0 | | STYLE_HAS_OUTLINE_ALPHA; |
726 | 0 | } |
727 | 0 | } |
728 | 0 | else if( !strcasecmp( "outline-level", attr ) ) |
729 | 0 | { |
730 | 0 | p_ssa_style->p_style->i_outline_width = atoi( val ); |
731 | 0 | } |
732 | 0 | else if( !strcasecmp( "shadow-color", attr ) ) |
733 | 0 | { |
734 | 0 | if( *val == '#' ) |
735 | 0 | { |
736 | 0 | unsigned long col = strtol(val+1, NULL, 16); |
737 | 0 | p_ssa_style->p_style->i_shadow_color = (col & 0x00ffffff); |
738 | 0 | p_ssa_style->p_style->i_shadow_alpha = (col >> 24) & 0xff; |
739 | 0 | p_ssa_style->p_style->i_features |= STYLE_HAS_SHADOW_COLOR |
740 | 0 | | STYLE_HAS_SHADOW_ALPHA; |
741 | 0 | } |
742 | 0 | } |
743 | 0 | else if( !strcasecmp( "shadow-level", attr ) ) |
744 | 0 | { |
745 | 0 | p_ssa_style->p_style->i_shadow_width = atoi( val ); |
746 | 0 | } |
747 | 0 | else if( !strcasecmp( "spacing", attr ) ) |
748 | 0 | { |
749 | 0 | p_ssa_style->p_style->i_spacing = atoi( val ); |
750 | 0 | } |
751 | 0 | } |
752 | 0 | } |
753 | 0 | else if( !strcasecmp( "position", node ) && (i_style_level == 2) ) |
754 | 0 | { |
755 | 0 | const char *attr, *val; |
756 | 0 | while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) ) |
757 | 0 | { |
758 | 0 | if( !strcasecmp( "alignment", attr ) ) |
759 | 0 | { |
760 | 0 | if( !strcasecmp( "TopLeft", val ) ) |
761 | 0 | p_ssa_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT; |
762 | 0 | else if( !strcasecmp( "TopCenter", val ) ) |
763 | 0 | p_ssa_style->i_align = SUBPICTURE_ALIGN_TOP; |
764 | 0 | else if( !strcasecmp( "TopRight", val ) ) |
765 | 0 | p_ssa_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT; |
766 | 0 | else if( !strcasecmp( "MiddleLeft", val ) ) |
767 | 0 | p_ssa_style->i_align = SUBPICTURE_ALIGN_LEFT; |
768 | 0 | else if( !strcasecmp( "MiddleCenter", val ) ) |
769 | 0 | p_ssa_style->i_align = 0; |
770 | 0 | else if( !strcasecmp( "MiddleRight", val ) ) |
771 | 0 | p_ssa_style->i_align = SUBPICTURE_ALIGN_RIGHT; |
772 | 0 | else if( !strcasecmp( "BottomLeft", val ) ) |
773 | 0 | p_ssa_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT; |
774 | 0 | else if( !strcasecmp( "BottomCenter", val ) ) |
775 | 0 | p_ssa_style->i_align = SUBPICTURE_ALIGN_BOTTOM; |
776 | 0 | else if( !strcasecmp( "BottomRight", val ) ) |
777 | 0 | p_ssa_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT; |
778 | 0 | } |
779 | 0 | else if( !strcasecmp( "horizontal-margin", attr ) ) |
780 | 0 | { |
781 | 0 | if( strchr( val, '%' ) ) |
782 | 0 | { |
783 | 0 | p_ssa_style->i_margin_h = 0; |
784 | 0 | p_ssa_style->i_margin_percent_h = atoi( val ); |
785 | 0 | } |
786 | 0 | else |
787 | 0 | { |
788 | 0 | p_ssa_style->i_margin_h = atoi( val ); |
789 | 0 | p_ssa_style->i_margin_percent_h = 0; |
790 | 0 | } |
791 | 0 | } |
792 | 0 | else if( !strcasecmp( "vertical-margin", attr ) ) |
793 | 0 | { |
794 | 0 | if( strchr( val, '%' ) ) |
795 | 0 | { |
796 | 0 | p_ssa_style->i_margin_v = 0; |
797 | 0 | p_ssa_style->i_margin_percent_v = atoi( val ); |
798 | 0 | } |
799 | 0 | else |
800 | 0 | { |
801 | 0 | p_ssa_style->i_margin_v = atoi( val ); |
802 | 0 | p_ssa_style->i_margin_percent_v = 0; |
803 | 0 | } |
804 | 0 | } |
805 | 0 | } |
806 | 0 | } |
807 | 0 | break; |
808 | 0 | } |
809 | 0 | } |
810 | 0 | free( p_ssa_style ); |
811 | 0 | } |
812 | | |
813 | | |
814 | | |
815 | | static void ParseUSFString( decoder_t *p_dec, |
816 | | char *psz_subtitle, |
817 | | vlc_spu_regions *regions ) |
818 | 0 | { |
819 | 0 | decoder_sys_t *p_sys = p_dec->p_sys; |
820 | |
|
821 | 0 | for( ; *psz_subtitle; psz_subtitle++ ) |
822 | 0 | { |
823 | 0 | if( *psz_subtitle != '<' ) |
824 | 0 | continue; |
825 | | |
826 | 0 | char *psz_end; |
827 | 0 | subpicture_region_t *p_region = NULL; |
828 | |
|
829 | 0 | if(( !strncasecmp( psz_subtitle, "<karaoke ", 9 )) || |
830 | 0 | ( !strncasecmp( psz_subtitle, "<karaoke>", 9 ))) |
831 | 0 | { |
832 | 0 | psz_end = strcasestr( psz_subtitle, "</karaoke>" ); |
833 | |
|
834 | 0 | if( psz_end ) |
835 | 0 | { |
836 | 0 | char *psz_knodes = strndup( &psz_subtitle[9], psz_end - &psz_subtitle[9] ); |
837 | 0 | if( psz_knodes ) |
838 | 0 | { |
839 | | /* remove timing <k> tags */ |
840 | 0 | char *psz_flat = CreatePlainText( psz_knodes ); |
841 | 0 | free( psz_knodes ); |
842 | 0 | if( psz_flat ) |
843 | 0 | { |
844 | 0 | p_region = CreateTextRegion( p_dec, |
845 | 0 | psz_flat, |
846 | 0 | psz_flat, |
847 | 0 | p_sys->i_align ); |
848 | 0 | if( !p_region ) |
849 | 0 | free( psz_flat ); |
850 | 0 | } |
851 | 0 | } |
852 | |
|
853 | 0 | psz_end += strcspn( psz_end, ">" ) + 1; |
854 | 0 | } |
855 | 0 | } |
856 | 0 | else if(( !strncasecmp( psz_subtitle, "<image ", 7 )) || |
857 | 0 | ( !strncasecmp( psz_subtitle, "<image>", 7 ))) |
858 | 0 | { |
859 | 0 | psz_end = strcasestr( psz_subtitle, "</image>" ); |
860 | 0 | char *psz_content = strchr( psz_subtitle, '>' ); |
861 | 0 | int i_transparent = -1; |
862 | | |
863 | | /* If a colorkey parameter is specified, then we have to map |
864 | | * that index in the picture through as transparent (it is |
865 | | * required by the USF spec but is also recommended that if the |
866 | | * creator really wants a transparent colour that they use a |
867 | | * type like PNG that properly supports it; this goes doubly |
868 | | * for VLC because the pictures are stored internally in YUV |
869 | | * and the resulting colour-matching may not produce the |
870 | | * desired results.) |
871 | | */ |
872 | 0 | char *psz_tmp = GrabAttributeValue( "colorkey", psz_subtitle ); |
873 | 0 | if( psz_tmp ) |
874 | 0 | { |
875 | 0 | if( *psz_tmp == '#' ) |
876 | 0 | i_transparent = strtol( psz_tmp + 1, NULL, 16 ) & 0x00ffffff; |
877 | 0 | free( psz_tmp ); |
878 | 0 | } |
879 | 0 | if( psz_content && ( psz_content < psz_end ) ) |
880 | 0 | { |
881 | 0 | char *psz_filename = strndup( &psz_content[1], psz_end - &psz_content[1] ); |
882 | 0 | if( psz_filename ) |
883 | 0 | { |
884 | 0 | p_region = LoadEmbeddedImage( p_dec, |
885 | 0 | psz_filename, i_transparent ); |
886 | 0 | free( psz_filename ); |
887 | 0 | } |
888 | 0 | } |
889 | |
|
890 | 0 | if( psz_end ) psz_end += strcspn( psz_end, ">" ) + 1; |
891 | 0 | } |
892 | 0 | else |
893 | 0 | { |
894 | 0 | psz_end = psz_subtitle + strlen( psz_subtitle ); |
895 | |
|
896 | 0 | char *psz_flat = CreatePlainText( psz_subtitle ); |
897 | 0 | if( psz_flat ) |
898 | 0 | { |
899 | 0 | p_region = CreateTextRegion( p_dec, |
900 | 0 | psz_subtitle, |
901 | 0 | psz_flat, |
902 | 0 | p_sys->i_align ); |
903 | 0 | if( !p_region ) |
904 | 0 | free( psz_flat ); |
905 | 0 | } |
906 | 0 | } |
907 | |
|
908 | 0 | if( p_region ) |
909 | 0 | { |
910 | 0 | vlc_spu_regions_push(regions, p_region); |
911 | | |
912 | | /* Look for position arguments which may override the style-based |
913 | | * defaults. |
914 | | */ |
915 | 0 | SetupPositions( p_region, psz_subtitle ); |
916 | 0 | } |
917 | |
|
918 | 0 | if( psz_end ) |
919 | 0 | psz_subtitle = psz_end - 1; |
920 | |
|
921 | 0 | psz_subtitle += strcspn( psz_subtitle, ">" ); |
922 | 0 | } |
923 | 0 | } |
924 | | |
925 | | /***************************************************************************** |
926 | | * ParseUSFHeader: Retrieve global formatting information etc |
927 | | *****************************************************************************/ |
928 | | static void ParseUSFHeader( decoder_t *p_dec ) |
929 | 0 | { |
930 | 0 | stream_t *p_sub = NULL; |
931 | 0 | xml_reader_t *p_xml_reader = NULL; |
932 | |
|
933 | 0 | p_sub = vlc_stream_MemoryNew( VLC_OBJECT(p_dec), |
934 | 0 | p_dec->fmt_in->p_extra, |
935 | 0 | p_dec->fmt_in->i_extra, |
936 | 0 | true ); |
937 | 0 | if( !p_sub ) |
938 | 0 | return; |
939 | | |
940 | 0 | p_xml_reader = xml_ReaderCreate( p_dec, p_sub ); |
941 | 0 | if( likely(p_xml_reader) ) |
942 | 0 | { |
943 | 0 | const char *node; |
944 | | |
945 | | /* Look for Root Node */ |
946 | 0 | if( xml_ReaderNextNode( p_xml_reader, &node ) == XML_READER_STARTELEM |
947 | 0 | && !strcasecmp( "usfsubtitles", node ) ) |
948 | 0 | ParseUSFHeaderTags( p_dec, p_xml_reader ); |
949 | |
|
950 | 0 | xml_ReaderDelete( p_xml_reader ); |
951 | 0 | } |
952 | 0 | vlc_stream_Delete( p_sub ); |
953 | 0 | } |
954 | | |
955 | | /* Function now handles tags which has attribute values, and tries |
956 | | * to deal with &' commands too. It no longer modifies the string |
957 | | * in place, so that the original text can be reused |
958 | | */ |
959 | | static char *StripTags( char *psz_subtitle ) |
960 | 0 | { |
961 | 0 | char *psz_text_start; |
962 | 0 | char *psz_text; |
963 | |
|
964 | 0 | psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 ); |
965 | 0 | if( !psz_text_start ) |
966 | 0 | return NULL; |
967 | | |
968 | 0 | while( *psz_subtitle ) |
969 | 0 | { |
970 | | /* Mask out any pre-existing LFs in the subtitle */ |
971 | 0 | if( *psz_subtitle == '\n' ) |
972 | 0 | *psz_subtitle = ' '; |
973 | |
|
974 | 0 | if( *psz_subtitle == '<' ) |
975 | 0 | { |
976 | 0 | if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 ) |
977 | 0 | *psz_text++ = '\n'; |
978 | |
|
979 | 0 | psz_subtitle += strcspn( psz_subtitle, ">" ); |
980 | 0 | } |
981 | 0 | else if( *psz_subtitle == '&' ) |
982 | 0 | { |
983 | 0 | if( !strncasecmp( psz_subtitle, "<", 4 )) |
984 | 0 | { |
985 | 0 | *psz_text++ = '<'; |
986 | 0 | psz_subtitle += strcspn( psz_subtitle, ";" ); |
987 | 0 | } |
988 | 0 | else if( !strncasecmp( psz_subtitle, ">", 4 )) |
989 | 0 | { |
990 | 0 | *psz_text++ = '>'; |
991 | 0 | psz_subtitle += strcspn( psz_subtitle, ";" ); |
992 | 0 | } |
993 | 0 | else if( !strncasecmp( psz_subtitle, "&", 5 )) |
994 | 0 | { |
995 | 0 | *psz_text++ = '&'; |
996 | 0 | psz_subtitle += strcspn( psz_subtitle, ";" ); |
997 | 0 | } |
998 | 0 | else if( !strncasecmp( psz_subtitle, """, 6 )) |
999 | 0 | { |
1000 | 0 | *psz_text++ = '\"'; |
1001 | 0 | psz_subtitle += strcspn( psz_subtitle, ";" ); |
1002 | 0 | } |
1003 | 0 | else |
1004 | 0 | { |
1005 | | /* Assume it is just a normal ampersand */ |
1006 | 0 | *psz_text++ = '&'; |
1007 | 0 | } |
1008 | 0 | } |
1009 | 0 | else |
1010 | 0 | { |
1011 | 0 | *psz_text++ = *psz_subtitle; |
1012 | 0 | } |
1013 | | |
1014 | | /* Security fix: Account for the case where input ends early */ |
1015 | 0 | if( *psz_subtitle == '\0' ) break; |
1016 | | |
1017 | 0 | psz_subtitle++; |
1018 | 0 | } |
1019 | 0 | *psz_text++ = '\0'; |
1020 | |
|
1021 | 0 | char *psz = realloc( psz_text_start, psz_text - psz_text_start ); |
1022 | 0 | return likely(psz != NULL) ? psz : psz_text_start; |
1023 | 0 | } |
1024 | | |
1025 | | /* Turn a HTML subtitle, turn into a plain-text version, |
1026 | | * complete with sensible whitespace compaction |
1027 | | */ |
1028 | | |
1029 | | static char *CreatePlainText( char *psz_subtitle ) |
1030 | 0 | { |
1031 | 0 | char *psz_text = StripTags( psz_subtitle ); |
1032 | 0 | char *s; |
1033 | |
|
1034 | 0 | if( !psz_text ) |
1035 | 0 | return NULL; |
1036 | | |
1037 | 0 | s = strpbrk( psz_text, "\t\r\n " ); |
1038 | 0 | while( s ) |
1039 | 0 | { |
1040 | 0 | char spc = ' '; |
1041 | 0 | int i_whitespace = strspn( s, "\t\r\n " ); |
1042 | | |
1043 | | /* Favour '\n' over other whitespaces - if one of these |
1044 | | * occurs in the whitespace use a '\n' as our value, |
1045 | | * otherwise just use a ' ' |
1046 | | */ |
1047 | 0 | for( int k = 0; k < i_whitespace; k++ ) |
1048 | 0 | if( s[k] == '\n' ) spc = '\n'; |
1049 | |
|
1050 | 0 | if( i_whitespace > 1 ) |
1051 | 0 | { |
1052 | 0 | memmove( &s[1], |
1053 | 0 | &s[i_whitespace], |
1054 | 0 | strlen( s ) - i_whitespace + 1 ); |
1055 | 0 | } |
1056 | 0 | *s++ = spc; |
1057 | |
|
1058 | 0 | s = strpbrk( s, "\t\r\n " ); |
1059 | 0 | } |
1060 | 0 | return psz_text; |
1061 | 0 | } |
1062 | | |
1063 | | /**************************************************************************** |
1064 | | * download and resize image located at psz_url |
1065 | | ***************************************************************************/ |
1066 | | static subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec, |
1067 | | const char *psz_filename, |
1068 | | int i_transparent_color ) |
1069 | 0 | { |
1070 | 0 | decoder_sys_t *p_sys = p_dec->p_sys; |
1071 | 0 | subpicture_region_t *p_region; |
1072 | 0 | video_format_t fmt_out; |
1073 | 0 | picture_t *p_pic = NULL; |
1074 | |
|
1075 | 0 | for( int k = 0; k < p_sys->i_images; k++ ) |
1076 | 0 | { |
1077 | 0 | if( p_sys->pp_images && |
1078 | 0 | !strcmp( p_sys->pp_images[k]->psz_filename, psz_filename ) ) |
1079 | 0 | { |
1080 | 0 | p_pic = p_sys->pp_images[k]->p_pic; |
1081 | 0 | break; |
1082 | 0 | } |
1083 | 0 | } |
1084 | |
|
1085 | 0 | if( !p_pic ) |
1086 | 0 | { |
1087 | 0 | msg_Err( p_dec, "Unable to read image %s", psz_filename ); |
1088 | 0 | return NULL; |
1089 | 0 | } |
1090 | | |
1091 | | /* Display the feed's image */ |
1092 | 0 | memset( &fmt_out, 0, sizeof( video_format_t)); |
1093 | |
|
1094 | 0 | fmt_out.i_chroma = VLC_CODEC_YUVA; |
1095 | 0 | fmt_out.i_sar_num = fmt_out.i_sar_den = 1; |
1096 | 0 | fmt_out.i_width = |
1097 | 0 | fmt_out.i_visible_width = p_pic->format.i_visible_width; |
1098 | 0 | fmt_out.i_height = |
1099 | 0 | fmt_out.i_visible_height = p_pic->format.i_visible_height; |
1100 | |
|
1101 | 0 | p_region = subpicture_region_New( &fmt_out ); |
1102 | 0 | if( !p_region ) |
1103 | 0 | { |
1104 | 0 | msg_Err( p_dec, "cannot allocate SPU region" ); |
1105 | 0 | return NULL; |
1106 | 0 | } |
1107 | 0 | p_region->i_x = 0; |
1108 | 0 | p_region->i_y = 0; |
1109 | 0 | assert( p_pic->format.i_chroma == VLC_CODEC_YUVA ); |
1110 | | /* FIXME the copy is probably not needed anymore */ |
1111 | 0 | picture_CopyPixels( p_region->p_picture, p_pic ); |
1112 | | |
1113 | | /* This isn't the best way to do this - if you really want transparency, then |
1114 | | * you're much better off using an image type that supports it like PNG. The |
1115 | | * spec requires this support though. |
1116 | | */ |
1117 | 0 | if( i_transparent_color > 0 ) |
1118 | 0 | { |
1119 | 0 | int i_r = ( i_transparent_color >> 16 ) & 0xff; |
1120 | 0 | int i_g = ( i_transparent_color >> 8 ) & 0xff; |
1121 | 0 | int i_b = ( i_transparent_color ) & 0xff; |
1122 | | |
1123 | | /* FIXME it cannot work as the yuv conversion code will probably NOT match |
1124 | | * this one */ |
1125 | 0 | int i_y = ( ( ( 66 * i_r + 129 * i_g + 25 * i_b + 128 ) >> 8 ) + 16 ); |
1126 | 0 | int i_u = ( ( -38 * i_r - 74 * i_g + 112 * i_b + 128 ) >> 8 ) + 128 ; |
1127 | 0 | int i_v = ( ( 112 * i_r - 94 * i_g - 18 * i_b + 128 ) >> 8 ) + 128 ; |
1128 | |
|
1129 | 0 | for( unsigned int y = 0; y < fmt_out.i_height; y++ ) |
1130 | 0 | { |
1131 | 0 | for( unsigned int x = 0; x < fmt_out.i_width; x++ ) |
1132 | 0 | { |
1133 | 0 | if( p_region->p_picture->Y_PIXELS[y*p_region->p_picture->Y_PITCH + x] != i_y || |
1134 | 0 | p_region->p_picture->U_PIXELS[y*p_region->p_picture->U_PITCH + x] != i_u || |
1135 | 0 | p_region->p_picture->V_PIXELS[y*p_region->p_picture->V_PITCH + x] != i_v ) |
1136 | 0 | continue; |
1137 | 0 | p_region->p_picture->A_PIXELS[y*p_region->p_picture->A_PITCH + x] = 0; |
1138 | |
|
1139 | 0 | } |
1140 | 0 | } |
1141 | 0 | } |
1142 | 0 | return p_region; |
1143 | 0 | } |