input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / substx3g.c
blobdf49395f9e8b007283eb9b9d0c448d6c6f368e78
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_callbacks( OpenEncoder, NULL )
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 == 0);
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 /* Just @4 */
492 /* BGColor @8, read top of 16 bits */
493 p_style->i_background_color = (p_extra[8] << 16) |
494 (p_extra[10] << 8) |
495 p_extra[12];
496 p_style->i_features |= STYLE_HAS_BACKGROUND_COLOR;
498 /* BoxRecord @14 */
499 /* Reserved 64 @22 */
500 /* Font # @30 */
502 /* Font Face @32 */
503 p_style->i_style_flags = ConvertToVLCFlags( GetWBE(&p_extra[32]) );
504 if( p_style->i_style_flags )
505 p_style->i_features |= STYLE_HAS_FLAGS;
506 /* Reserved 8 @34 */
507 /* Reserved 16 @35 */
508 /* FGColor @37 */
509 p_style->i_font_color = (p_extra[37] << 16) |
510 (p_extra[39] << 8) |
511 p_extra[41];
512 p_style->i_features |= STYLE_HAS_FONT_COLOR;
514 /* FontName Pascal (8 + string) @43 */
516 /*****************************************************************************
517 * Decoder entry/exit points
518 *****************************************************************************/
519 static void CloseDecoder( vlc_object_t *p_this )
521 decoder_t *p_dec = (decoder_t *) p_this;
522 text_style_Delete( (text_style_t *) p_dec->p_sys );
525 static int OpenDecoder( vlc_object_t *p_this )
527 decoder_t *p_dec = (decoder_t *) p_this;
529 if( p_dec->fmt_in.i_codec != VLC_CODEC_TX3G &&
530 p_dec->fmt_in.i_codec != VLC_CODEC_QTXT )
531 return VLC_EGENERIC;
533 p_dec->pf_decode = Decode;
535 p_dec->p_sys = text_style_Create( STYLE_NO_DEFAULTS );
536 if( !p_dec->p_sys )
537 return VLC_ENOMEM;
539 if( p_dec->fmt_in.i_codec == VLC_CODEC_TX3G )
540 ParseExtradataTx3g( p_dec );
541 else
542 ParseExtradataTextMedia( p_dec );
544 p_dec->fmt_out.i_codec = VLC_CODEC_TEXT;
546 return VLC_SUCCESS;
549 /*****************************************************************************
550 * Encoder entry/exit
551 *****************************************************************************/
552 #ifdef ENABLE_SOUT
553 static void FillExtradataTx3g( void **pp_extra, int *pi_extra )
555 size_t i_extra = 32 + 37;
556 uint8_t *p_extra = calloc( 1, i_extra );
557 if( p_extra )
559 p_extra[4] = 0x01;/* 1 center, horizontal */
560 p_extra[5] = 0xFF;/* -1 bottom, vertical */
561 SetDWBE( &p_extra[6], 0x000000FFU ); /* bgcolor */
562 p_extra[25] = STYLE_DEFAULT_FONT_SIZE;
563 SetDWBE( &p_extra[26], 0xFFFFFFFFU ); /* fgcolor */
565 /* FontTableBox */
566 SetDWBE(&p_extra[32], 8 + 2 + 6 + 11 + 10);
567 memcpy(&p_extra[36], "ftab", 4);
569 SetWBE(&p_extra[40], 3); /* entry count */
570 /* Font Record 0 */
571 p_extra[41] = 5;
572 memcpy(&p_extra[42], "Serif", 5);
573 /* Font Record 1 */
574 p_extra[47] = 10;
575 memcpy(&p_extra[48], "Sans-serif", 10);
576 /* Font Record 2 */
577 p_extra[58] = 9;
578 memcpy(&p_extra[59], "Monospace", 9);
580 *pp_extra = p_extra;
581 *pi_extra = i_extra;
585 static int OpenEncoder( vlc_object_t *p_this )
587 encoder_t *p_enc = (encoder_t *)p_this;
589 if( p_enc->fmt_out.i_codec != VLC_CODEC_TX3G )
590 return VLC_EGENERIC;
592 p_enc->fmt_in.i_codec = VLC_CODEC_TEXT;
594 p_enc->p_sys = NULL;
596 p_enc->pf_encode_sub = Encode;
597 p_enc->fmt_out.i_cat = SPU_ES;
599 if( !p_enc->fmt_out.i_extra )
600 FillExtradataTx3g( &p_enc->fmt_out.p_extra, &p_enc->fmt_out.i_extra );
602 return VLC_SUCCESS;
605 static int ConvertFromVLCFlags( const text_style_t *p_style )
607 int i_atomflags = 0;
608 if( p_style->i_features & STYLE_HAS_FLAGS )
610 if ( p_style->i_style_flags & STYLE_BOLD )
611 i_atomflags |= FONT_FACE_BOLD;
612 if ( p_style->i_style_flags & STYLE_ITALIC )
613 i_atomflags |= FONT_FACE_ITALIC;
614 if ( p_style->i_style_flags & STYLE_UNDERLINE )
615 i_atomflags |= FONT_FACE_UNDERLINE;
617 return i_atomflags;
620 static uint32_t ConvertFromVLCColor( const text_style_t *p_style )
622 uint32_t rgba = 0;
623 if( p_style->i_features & STYLE_HAS_FONT_COLOR )
624 rgba = ((uint32_t)p_style->i_font_color) << 8;
625 else
626 rgba = 0xFFFFFF00U;
627 if( p_style->i_features & STYLE_HAS_FONT_ALPHA )
628 rgba |= p_style->i_font_alpha;
629 else
630 rgba |= 0xFF;
631 return rgba;
634 static bool NeedStyling( const text_segment_t *p_segment )
636 const text_style_t *p_style = p_segment->style;
637 if( !p_style )
638 return false;
640 if( p_style->i_features & STYLE_HAS_FLAGS )
642 if( p_style->i_style_flags & (STYLE_BOLD|STYLE_ITALIC|STYLE_UNDERLINE) )
643 return true;
646 if( p_style->i_features & (STYLE_HAS_FONT_COLOR|STYLE_HAS_FONT_ALPHA) )
647 return true;
649 return false;
652 static block_t *GetStylBlock( const text_segment_t *p_segment, size_t i_styles )
654 size_t i_start = 0;
655 block_t *p_styl = block_Alloc( 10 + 12 * i_styles );
656 if( p_styl )
658 SetDWBE( p_styl->p_buffer, p_styl->i_buffer );
659 memcpy( &p_styl->p_buffer[4], "styl", 4 );
660 SetWBE( &p_styl->p_buffer[8], i_styles );
661 p_styl->i_buffer = 10;
662 for( ; p_segment; p_segment = p_segment->p_next )
664 size_t i_len = str8len( p_segment->psz_text );
665 if( NeedStyling( p_segment ) )
667 uint8_t *p = &p_styl->p_buffer[p_styl->i_buffer];
668 SetWBE( &p[0], i_start );
669 SetWBE( &p[2], i_start + i_len );
670 SetWBE( &p[4], 0 );
671 p[6] = ConvertFromVLCFlags( p_segment->style );
672 p[7] = STYLE_DEFAULT_FONT_SIZE;
673 SetDWBE(&p[8], ConvertFromVLCColor( p_segment->style ) );
674 p_styl->i_buffer += 12;
676 i_start += i_len;
679 return p_styl;
682 static block_t * Encode( encoder_t *p_enc, subpicture_t *p_spu )
684 VLC_UNUSED(p_enc);
685 const text_segment_t *p_segments = (p_spu->p_region)
686 ? p_spu->p_region->p_text
687 : NULL;
688 size_t i_len = 0;
689 size_t i_styles = 0;
691 for(const text_segment_t *p_segment = p_segments;
692 p_segment; p_segment = p_segment->p_next )
694 if( p_segment->style )
695 i_styles++;
696 i_len += strlen( p_segment->psz_text );
699 block_t *p_block = block_Alloc( i_len + 2 );
700 if( !p_block )
701 return NULL;
703 SetWBE(p_block->p_buffer, i_len);
704 p_block->i_buffer = 2;
705 for(const text_segment_t *p_segment = p_segments;
706 p_segment; p_segment = p_segment->p_next )
708 size_t i_seglen = strlen(p_segment->psz_text);
709 memcpy(&p_block->p_buffer[p_block->i_buffer],
710 p_segment->psz_text, i_seglen);
711 p_block->i_buffer += i_seglen;
713 p_block->i_dts = p_block->i_pts = p_spu->i_start;
714 p_block->i_length = p_spu->i_stop - p_spu->i_start;
716 if( i_styles > 0 )
717 p_block->p_next = GetStylBlock( p_segments, i_styles );
719 return block_ChainGather( p_block );
721 #endif