qml: remove context indicator
[vlc.git] / modules / codec / substx3g.c
blobf7639b386c5da91629837fc921734fdec7977caf
1 /*****************************************************************************
2 * substx3gsub.c : MP4 tx3g subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation, Inc.,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_codec.h>
28 #include <vlc_sout.h>
29 #include <vlc_charset.h>
31 #include "substext.h"
32 #include "../demux/mp4/minibox.h"
34 /*****************************************************************************
35 * Module descriptor.
36 *****************************************************************************/
37 static int OpenDecoder ( vlc_object_t * );
38 static void CloseDecoder ( vlc_object_t * );
39 static int Decode( decoder_t *, block_t * );
40 #ifdef ENABLE_SOUT
41 static int OpenEncoder ( vlc_object_t * );
42 static block_t * Encode( encoder_t *, subpicture_t * );
43 #endif
45 vlc_module_begin ()
46 set_description( N_("tx3g subtitles decoder") )
47 set_shortname( N_("tx3g subtitles") )
48 set_capability( "spu decoder", 100 )
49 set_category( CAT_INPUT )
50 set_subcategory( SUBCAT_INPUT_SCODEC )
51 set_callbacks( OpenDecoder, CloseDecoder )
52 #ifdef ENABLE_SOUT
53 add_submodule ()
54 set_description( N_("tx3g subtitles encoder") )
55 set_shortname( N_("tx3g subtitles encoder") )
56 set_capability( "encoder", 101 )
57 set_callback( OpenEncoder )
58 #endif
59 vlc_module_end ()
61 /****************************************************************************
62 * Local structs
63 ****************************************************************************/
65 /*****************************************************************************
66 * Local:
67 *****************************************************************************/
69 #define FONT_FACE_BOLD 0x1
70 #define FONT_FACE_ITALIC 0x2
71 #define FONT_FACE_UNDERLINE 0x4
73 static int ConvertToVLCFlags( int i_atomflags )
75 int i_vlcstyles_flags = 0;
76 if ( i_atomflags & FONT_FACE_BOLD )
77 i_vlcstyles_flags |= STYLE_BOLD;
78 if ( i_atomflags & FONT_FACE_ITALIC )
79 i_vlcstyles_flags |= STYLE_ITALIC;
80 if ( i_atomflags & FONT_FACE_UNDERLINE )
81 i_vlcstyles_flags |= STYLE_UNDERLINE;
82 return i_vlcstyles_flags;
85 static size_t str8len( const char *psz_string )
87 const char *psz_tmp = psz_string;
88 size_t i=0;
89 while ( *psz_tmp )
91 if ( (*psz_tmp & 0xC0) != 0x80 ) i++;
92 psz_tmp++;
94 return i;
97 static char * str8indup( const char *psz_string, size_t i_skip, size_t n )
99 while( i_skip && *psz_string )
101 if ( (*psz_string & 0xC0) != 0x80 ) i_skip--;
102 psz_string++;
104 if ( ! *psz_string || i_skip ) return NULL;
106 const char *psz_tmp = psz_string;
107 while( n && *psz_tmp )
109 if ( (*psz_tmp & 0xC0) != 0x80 ) n--;
110 psz_tmp++;
112 return strndup( psz_string, psz_tmp - psz_string );
115 typedef struct tx3g_segment_t tx3g_segment_t;
117 struct tx3g_segment_t
119 text_segment_t *s;
120 size_t i_size;
121 tx3g_segment_t *p_next3g;
124 static tx3g_segment_t * tx3g_segment_New( const char *psz_string )
126 tx3g_segment_t *p_seg = malloc( sizeof(tx3g_segment_t) );
127 if( p_seg )
129 p_seg->i_size = 0;
130 p_seg->p_next3g = NULL;
131 p_seg->s = text_segment_New( psz_string );
132 if( !p_seg->s )
134 free( p_seg );
135 p_seg = NULL;
138 return p_seg;
141 static void SegmentDoSplit( tx3g_segment_t *p_segment, uint16_t i_start, uint16_t i_end,
142 tx3g_segment_t **pp_segment_left,
143 tx3g_segment_t **pp_segment_middle,
144 tx3g_segment_t **pp_segment_right )
146 tx3g_segment_t *p_segment_left = NULL, *p_segment_right = NULL, *p_segment_middle = NULL;
148 if ( (p_segment->i_size - i_start < 1) || (p_segment->i_size - i_end < 1) )
149 goto error;
151 if ( i_start > 0 )
153 char* psz_text = str8indup( p_segment->s->psz_text, 0, i_start );
154 p_segment_left = tx3g_segment_New( psz_text );
155 free( psz_text );
156 if ( !p_segment_left ) goto error;
157 p_segment_left->s->style = text_style_Duplicate( p_segment->s->style );
158 p_segment_left->i_size = str8len( p_segment_left->s->psz_text );
161 char* psz_midtext = str8indup( p_segment->s->psz_text, i_start, i_end - i_start + 1 );
162 p_segment_middle = tx3g_segment_New( psz_midtext );
163 free( psz_midtext );
164 if ( !p_segment_middle ) goto error;
165 p_segment_middle->s->style = text_style_Duplicate( p_segment->s->style );
166 p_segment_middle->i_size = str8len( p_segment_middle->s->psz_text );
168 if ( i_end < (p_segment->i_size - 1) )
170 char* psz_text = str8indup( p_segment->s->psz_text, i_end + 1, p_segment->i_size - i_end - 1 );
171 p_segment_right = tx3g_segment_New( psz_text );
172 free( psz_text );
173 if ( !p_segment_right ) goto error;
174 p_segment_right->s->style = text_style_Duplicate( p_segment->s->style );
175 p_segment_right->i_size = str8len( p_segment_right->s->psz_text );
178 if ( p_segment_left ) p_segment_left->p_next3g = p_segment_middle;
179 if ( p_segment_right ) p_segment_middle->p_next3g = p_segment_right;
181 *pp_segment_left = p_segment_left;
182 *pp_segment_middle = p_segment_middle;
183 *pp_segment_right = p_segment_right;
185 return;
187 error:
188 if( p_segment_middle )
190 text_segment_Delete( p_segment_middle->s );
191 free( p_segment_middle );
193 if( p_segment_left )
195 text_segment_Delete( p_segment_left->s );
196 free( p_segment_left );
198 *pp_segment_left = *pp_segment_middle = *pp_segment_right = NULL;
201 static bool SegmentSplit( tx3g_segment_t *p_prev, tx3g_segment_t **pp_segment,
202 const uint16_t i_start, const uint16_t i_end,
203 const text_style_t *p_styles )
205 tx3g_segment_t *p_segment_left = NULL, *p_segment_middle = NULL, *p_segment_right = NULL;
207 if ( (*pp_segment)->i_size == 0 ) return false;
208 if ( i_start > i_end ) return false;
209 if ( (size_t)(i_end - i_start) > (*pp_segment)->i_size - 1 ) return false;
210 if ( i_end > (*pp_segment)->i_size - 1 ) return false;
212 SegmentDoSplit( *pp_segment, i_start, i_end, &p_segment_left, &p_segment_middle, &p_segment_right );
213 if ( !p_segment_middle )
215 /* Failed */
216 text_segment_Delete( p_segment_left->s );
217 free( p_segment_left );
218 text_segment_Delete( p_segment_right->s );
219 free( p_segment_right );
220 return false;
223 tx3g_segment_t *p_next3g = (*pp_segment)->p_next3g;
224 text_segment_Delete( (*pp_segment)->s );
225 free( *pp_segment );
226 *pp_segment = ( p_segment_left ) ? p_segment_left : p_segment_middle ;
227 if ( p_prev ) p_prev->p_next3g = *pp_segment;
229 if ( p_segment_right )
230 p_segment_right->p_next3g = p_next3g;
231 else
232 p_segment_middle->p_next3g = p_next3g;
234 if( p_segment_middle->s->style )
235 text_style_Merge( p_segment_middle->s->style, p_styles, true );
236 else
237 p_segment_middle->s->style = text_style_Duplicate( p_styles );
239 return true;
242 /* Creates a new segment using the given style and split existing ones according
243 to the start & end offsets */
244 static void ApplySegmentStyle( tx3g_segment_t **pp_segment, const uint16_t i_absstart,
245 const uint16_t i_absend, const text_style_t *p_styles )
247 /* find the matching segment */
248 uint16_t i_curstart = 0;
249 tx3g_segment_t *p_prev = NULL;
250 tx3g_segment_t *p_cur = *pp_segment;
251 while ( p_cur )
253 uint16_t i_curend = i_curstart + p_cur->i_size - 1;
254 if ( (i_absstart >= i_curstart) && (i_absend <= i_curend) )
256 /* segment found */
257 if ( !SegmentSplit( p_prev, &p_cur, i_absstart - i_curstart,
258 i_absend - i_curstart, p_styles ) )
259 return;
260 if ( !p_prev ) *pp_segment = p_cur;
261 break;
263 else
265 i_curstart += p_cur->i_size;
266 p_prev = p_cur;
267 p_cur = p_cur->p_next3g;
272 /* Do relative size conversion using default style size (from stsd),
273 as the line should always be 5%. Apply to each segment specific text size */
274 static void FontSizeConvert( const text_style_t *p_reference, text_style_t *p_style )
276 if( unlikely(!p_style) )
278 return;
280 else if( unlikely(!p_reference) || p_reference->i_font_size == 0 )
282 p_style->i_font_size = 0;
283 p_style->f_font_relsize = 5.0;
285 else
287 p_style->f_font_relsize = 5.0 * (float) p_style->i_font_size / p_reference->i_font_size;
288 p_style->i_font_size = 0;
292 /*****************************************************************************
293 * Decode:
294 *****************************************************************************/
295 static int Decode( decoder_t *p_dec, block_t *p_block )
297 subpicture_t *p_spu = NULL;
299 if( p_block == NULL ) /* No Drain */
300 return VLCDEC_SUCCESS;
302 if( ( p_block->i_flags & (BLOCK_FLAG_CORRUPTED) ) ||
303 p_block->i_buffer < sizeof(uint16_t) )
305 block_Release( p_block );
306 return VLCDEC_SUCCESS;
309 uint8_t *p_buf = p_block->p_buffer;
311 /* Read our raw string and create the styled segment for HTML */
312 uint16_t i_psz_bytelength = GetWBE( p_buf );
313 if( p_block->i_buffer < i_psz_bytelength + 2U )
315 block_Release( p_block );
316 return VLCDEC_SUCCESS;
319 const uint8_t *p_pszstart = p_block->p_buffer + sizeof(uint16_t);
320 char *psz_subtitle;
321 if ( i_psz_bytelength > 2 &&
322 ( !memcmp( p_pszstart, "\xFE\xFF", 2 ) || !memcmp( p_pszstart, "\xFF\xFE", 2 ) )
325 psz_subtitle = FromCharset( "UTF-16", p_pszstart, i_psz_bytelength );
326 if ( !psz_subtitle )
327 return VLCDEC_SUCCESS;
329 else
331 psz_subtitle = strndup( (const char*) p_pszstart, i_psz_bytelength );
332 if ( !psz_subtitle )
333 return VLCDEC_SUCCESS;
335 p_buf += i_psz_bytelength + sizeof(uint16_t);
337 for( uint16_t i=0; i < i_psz_bytelength; i++ )
338 if ( psz_subtitle[i] == '\r' ) psz_subtitle[i] = '\n';
340 tx3g_segment_t *p_segment3g = tx3g_segment_New( psz_subtitle );
341 p_segment3g->i_size = str8len( psz_subtitle );
342 free( psz_subtitle );
344 if ( !p_segment3g->s->psz_text )
346 text_segment_Delete( p_segment3g->s );
347 free( p_segment3g );
348 return VLCDEC_SUCCESS;
351 /* Create the subpicture unit */
352 p_spu = decoder_NewSubpictureText( p_dec );
353 if( !p_spu )
355 text_segment_Delete( p_segment3g->s );
356 free( p_segment3g );
357 return VLCDEC_SUCCESS;
360 subtext_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
361 const text_style_t *p_root_style = (text_style_t *) p_dec->p_sys;
363 mp4_box_iterator_t it;
364 mp4_box_iterator_Init( &it, p_buf,
365 p_block->i_buffer - (p_buf - p_block->p_buffer) );
366 /* Parse our styles */
367 if( p_dec->fmt_in.i_codec != VLC_CODEC_QTXT )
368 while( mp4_box_iterator_Next( &it ) )
370 switch( it.i_type )
373 case VLC_FOURCC('s','t','y','l'):
375 if( it.i_payload < 14 )
376 break;
378 uint16_t i_nbrecords = GetWBE(it.p_payload);
379 uint16_t i_cur_record = 0;
381 it.p_payload += 2; it.i_payload -= 2;
382 while( i_cur_record++ < i_nbrecords && it.i_payload >= 12 )
384 uint16_t i_start = __MIN( GetWBE(it.p_payload), i_psz_bytelength - 1 );
385 uint16_t i_end = GetWBE(it.p_payload + 2); /* index is past last char */
386 if( i_start < i_end )
388 i_end = VLC_CLIP( i_end - 1, i_start, i_psz_bytelength - 1 );
390 text_style_t *p_style = text_style_Create( STYLE_NO_DEFAULTS );
391 if( p_style )
393 if( (p_style->i_style_flags = ConvertToVLCFlags( it.p_payload[6] )) )
394 p_style->i_features |= STYLE_HAS_FLAGS;
395 p_style->i_font_size = it.p_payload[7];
396 p_style->i_font_color = GetDWBE(&it.p_payload[8]) >> 8;// RGBA -> RGB
397 p_style->i_font_alpha = GetDWBE(&it.p_payload[8]) & 0xFF;
398 p_style->i_features |= STYLE_HAS_FONT_COLOR | STYLE_HAS_FONT_ALPHA;
399 ApplySegmentStyle( &p_segment3g, i_start, i_end, p_style );
400 text_style_Delete( p_style );
404 it.p_payload += 12; it.i_payload -= 12;
406 } break;
409 default:
410 break;
415 p_spu->i_start = p_block->i_pts;
416 p_spu->i_stop = p_block->i_pts + p_block->i_length;
417 p_spu->b_ephemer = (p_block->i_length == VLC_TICK_INVALID);
418 p_spu->b_absolute = false;
420 p_spu_sys->region.align = SUBPICTURE_ALIGN_BOTTOM;
422 text_style_Merge( p_spu_sys->p_default_style, p_root_style, true );
423 FontSizeConvert( p_root_style, p_spu_sys->p_default_style );
425 /* Unwrap */
426 text_segment_t *p_text_segments = p_segment3g->s;
427 text_segment_t *p_cur = p_text_segments;
428 while( p_segment3g )
430 FontSizeConvert( p_root_style, p_segment3g->s->style );
432 tx3g_segment_t * p_old = p_segment3g;
433 p_segment3g = p_segment3g->p_next3g;
434 free( p_old );
435 if( p_segment3g )
436 p_cur->p_next = p_segment3g->s;
437 p_cur = p_cur->p_next;
440 p_spu_sys->region.p_segments = p_text_segments;
442 block_Release( p_block );
444 decoder_QueueSub( p_dec, p_spu );
445 return VLCDEC_SUCCESS;
448 /*****************************************************************************
449 * Extradata Parsing
450 *****************************************************************************/
451 static void ParseExtradataTx3g( decoder_t *p_dec )
453 text_style_t *p_style = (text_style_t *) p_dec->p_sys;
454 const uint8_t *p_extra = p_dec->fmt_in.p_extra;
456 if( p_dec->fmt_in.i_extra < 32 )
457 return;
459 /* DF @0 */
460 /* Just @4 */
462 /* BGColor @6 */
463 p_style->i_background_color = GetDWBE(&p_extra[6]) >> 8;
464 p_style->i_background_alpha = p_extra[9];
465 p_style->i_features |= STYLE_HAS_BACKGROUND_COLOR|STYLE_HAS_BACKGROUND_ALPHA;
467 /* BoxRecord @10 */
469 /* StyleRecord @18 */
470 p_style->i_style_flags = ConvertToVLCFlags( p_extra[24] );
471 if( p_style->i_style_flags )
472 p_style->i_features |= STYLE_HAS_FLAGS;
473 p_style->i_font_size = p_extra[25];
474 p_style->i_font_color = GetDWBE(&p_extra[26]) >> 8;// RGBA -> RGB
475 p_style->i_font_alpha = p_extra[29];
476 p_style->i_features |= STYLE_HAS_FONT_COLOR | STYLE_HAS_FONT_ALPHA;
478 /* FontTableBox @30 */
481 static void ParseExtradataTextMedia( decoder_t *p_dec )
483 text_style_t *p_style = (text_style_t *) p_dec->p_sys;
484 const uint8_t *p_extra = p_dec->fmt_in.p_extra;
486 if( p_dec->fmt_in.i_extra < 44 )
487 return;
489 /* DF @0 */
490 uint32_t i_flags = GetDWBE(p_extra);
491 if(i_flags & 0x1000) /* drop shadow */
493 p_style->i_style_flags |= STYLE_SHADOW;
494 p_style->i_features |= STYLE_HAS_SHADOW_COLOR|STYLE_HAS_FLAGS|STYLE_HAS_SHADOW_ALPHA;
495 p_style->i_shadow_color = 0xC0C0C0;
496 p_style->i_shadow_alpha = STYLE_ALPHA_OPAQUE;
498 if(i_flags & 0x4000) /* key text*/
500 /*Controls background color. If this flag is set to 1, the text media handler does not display the
501 background color, so that the text overlay background tracks.*/
502 p_style->i_style_flags &= ~STYLE_BACKGROUND;
505 /* Just @4 */
507 /* BGColor @8, read top of 16 bits */
508 p_style->i_background_color = (p_extra[8] << 16) |
509 (p_extra[10] << 8) |
510 p_extra[12];
511 p_style->i_features |= STYLE_HAS_BACKGROUND_COLOR | STYLE_HAS_BACKGROUND_ALPHA;
512 p_style->i_background_alpha = STYLE_ALPHA_OPAQUE;
514 /* BoxRecord @14 */
515 /* Reserved 64 @22 */
516 /* Font # @30 */
518 /* Font Face @32 */
519 p_style->i_style_flags |= ConvertToVLCFlags( GetWBE(&p_extra[32]) );
520 if( p_style->i_style_flags )
521 p_style->i_features |= STYLE_HAS_FLAGS;
522 /* Reserved 8 @34 */
523 /* Reserved 16 @35 */
524 /* FGColor @37 */
525 p_style->i_font_color = (p_extra[37] << 16) |
526 (p_extra[39] << 8) |
527 p_extra[41];
528 p_style->i_features |= STYLE_HAS_FONT_COLOR;
530 /* FontName Pascal (8 + string) @43 */
532 /*****************************************************************************
533 * Decoder entry/exit points
534 *****************************************************************************/
535 static void CloseDecoder( vlc_object_t *p_this )
537 decoder_t *p_dec = (decoder_t *) p_this;
538 text_style_Delete( (text_style_t *) p_dec->p_sys );
541 static int OpenDecoder( vlc_object_t *p_this )
543 decoder_t *p_dec = (decoder_t *) p_this;
545 if( p_dec->fmt_in.i_codec != VLC_CODEC_TX3G &&
546 p_dec->fmt_in.i_codec != VLC_CODEC_QTXT )
547 return VLC_EGENERIC;
549 p_dec->pf_decode = Decode;
551 p_dec->p_sys = text_style_Create( STYLE_NO_DEFAULTS );
552 if( !p_dec->p_sys )
553 return VLC_ENOMEM;
555 text_style_t *p_default_style = p_dec->p_sys;
556 p_default_style->i_style_flags |= STYLE_BACKGROUND;
557 p_default_style->i_features |= STYLE_HAS_FLAGS;
559 if( p_dec->fmt_in.i_codec == VLC_CODEC_TX3G )
560 ParseExtradataTx3g( p_dec );
561 else
562 ParseExtradataTextMedia( p_dec );
564 p_dec->fmt_out.i_codec = VLC_CODEC_TEXT;
566 return VLC_SUCCESS;
569 /*****************************************************************************
570 * Encoder entry/exit
571 *****************************************************************************/
572 #ifdef ENABLE_SOUT
573 static void FillExtradataTx3g( void **pp_extra, int *pi_extra )
575 size_t i_extra = 32 + 37;
576 uint8_t *p_extra = calloc( 1, i_extra );
577 if( p_extra )
579 p_extra[4] = 0x01;/* 1 center, horizontal */
580 p_extra[5] = 0xFF;/* -1 bottom, vertical */
581 SetDWBE( &p_extra[6], 0x000000FFU ); /* bgcolor */
582 p_extra[25] = STYLE_DEFAULT_FONT_SIZE;
583 SetDWBE( &p_extra[26], 0xFFFFFFFFU ); /* fgcolor */
585 /* FontTableBox */
586 SetDWBE(&p_extra[32], 8 + 2 + 6 + 11 + 10);
587 memcpy(&p_extra[36], "ftab", 4);
589 SetWBE(&p_extra[40], 3); /* entry count */
590 /* Font Record 0 */
591 p_extra[41] = 5;
592 memcpy(&p_extra[42], "Serif", 5);
593 /* Font Record 1 */
594 p_extra[47] = 10;
595 memcpy(&p_extra[48], "Sans-serif", 10);
596 /* Font Record 2 */
597 p_extra[58] = 9;
598 memcpy(&p_extra[59], "Monospace", 9);
600 *pp_extra = p_extra;
601 *pi_extra = i_extra;
605 static int OpenEncoder( vlc_object_t *p_this )
607 encoder_t *p_enc = (encoder_t *)p_this;
609 if( p_enc->fmt_out.i_codec != VLC_CODEC_TX3G )
610 return VLC_EGENERIC;
612 p_enc->fmt_in.i_codec = VLC_CODEC_TEXT;
614 p_enc->p_sys = NULL;
616 p_enc->pf_encode_sub = Encode;
617 p_enc->fmt_out.i_cat = SPU_ES;
619 if( !p_enc->fmt_out.i_extra )
620 FillExtradataTx3g( &p_enc->fmt_out.p_extra, &p_enc->fmt_out.i_extra );
622 return VLC_SUCCESS;
625 static int ConvertFromVLCFlags( const text_style_t *p_style )
627 int i_atomflags = 0;
628 if( p_style->i_features & STYLE_HAS_FLAGS )
630 if ( p_style->i_style_flags & STYLE_BOLD )
631 i_atomflags |= FONT_FACE_BOLD;
632 if ( p_style->i_style_flags & STYLE_ITALIC )
633 i_atomflags |= FONT_FACE_ITALIC;
634 if ( p_style->i_style_flags & STYLE_UNDERLINE )
635 i_atomflags |= FONT_FACE_UNDERLINE;
637 return i_atomflags;
640 static uint32_t ConvertFromVLCColor( const text_style_t *p_style )
642 uint32_t rgba = 0;
643 if( p_style->i_features & STYLE_HAS_FONT_COLOR )
644 rgba = ((uint32_t)p_style->i_font_color) << 8;
645 else
646 rgba = 0xFFFFFF00U;
647 if( p_style->i_features & STYLE_HAS_FONT_ALPHA )
648 rgba |= p_style->i_font_alpha;
649 else
650 rgba |= 0xFF;
651 return rgba;
654 static bool NeedStyling( const text_segment_t *p_segment )
656 const text_style_t *p_style = p_segment->style;
657 if( !p_style )
658 return false;
660 if( p_style->i_features & STYLE_HAS_FLAGS )
662 if( p_style->i_style_flags & (STYLE_BOLD|STYLE_ITALIC|STYLE_UNDERLINE) )
663 return true;
666 if( p_style->i_features & (STYLE_HAS_FONT_COLOR|STYLE_HAS_FONT_ALPHA) )
667 return true;
669 return false;
672 static block_t *GetStylBlock( const text_segment_t *p_segment, size_t i_styles )
674 size_t i_start = 0;
675 block_t *p_styl = block_Alloc( 10 + 12 * i_styles );
676 if( p_styl )
678 SetDWBE( p_styl->p_buffer, p_styl->i_buffer );
679 memcpy( &p_styl->p_buffer[4], "styl", 4 );
680 SetWBE( &p_styl->p_buffer[8], i_styles );
681 p_styl->i_buffer = 10;
682 for( ; p_segment; p_segment = p_segment->p_next )
684 size_t i_len = str8len( p_segment->psz_text );
685 if( NeedStyling( p_segment ) )
687 uint8_t *p = &p_styl->p_buffer[p_styl->i_buffer];
688 SetWBE( &p[0], i_start );
689 SetWBE( &p[2], i_start + i_len );
690 SetWBE( &p[4], 0 );
691 p[6] = ConvertFromVLCFlags( p_segment->style );
692 p[7] = STYLE_DEFAULT_FONT_SIZE;
693 SetDWBE(&p[8], ConvertFromVLCColor( p_segment->style ) );
694 p_styl->i_buffer += 12;
696 i_start += i_len;
699 return p_styl;
702 static block_t * Encode( encoder_t *p_enc, subpicture_t *p_spu )
704 VLC_UNUSED(p_enc);
705 const text_segment_t *p_segments = (p_spu->p_region)
706 ? p_spu->p_region->p_text
707 : NULL;
708 size_t i_len = 0;
709 size_t i_styles = 0;
711 for(const text_segment_t *p_segment = p_segments;
712 p_segment; p_segment = p_segment->p_next )
714 if( p_segment->style )
715 i_styles++;
716 i_len += strlen( p_segment->psz_text );
719 block_t *p_block = block_Alloc( i_len + 2 );
720 if( !p_block )
721 return NULL;
723 SetWBE(p_block->p_buffer, i_len);
724 p_block->i_buffer = 2;
725 for(const text_segment_t *p_segment = p_segments;
726 p_segment; p_segment = p_segment->p_next )
728 size_t i_seglen = strlen(p_segment->psz_text);
729 memcpy(&p_block->p_buffer[p_block->i_buffer],
730 p_segment->psz_text, i_seglen);
731 p_block->i_buffer += i_seglen;
733 p_block->i_dts = p_block->i_pts = p_spu->i_start;
734 p_block->i_length = p_spu->i_stop - p_spu->i_start;
736 if( i_styles > 0 )
737 p_block->p_next = GetStylBlock( p_segments, i_styles );
739 return block_ChainGather( p_block );
741 #endif