1 /*****************************************************************************
2 * dvbsub.c : DVB subtitles decoder
3 * DVB subtitles encoder (developed for Anevia, www.anevia.com)
4 *****************************************************************************
5 * Copyright (C) 2003 ANEVIA
6 * Copyright (C) 2003-2005 VideoLAN (Centrale Réseaux) and its contributors
9 * Authors: Gildas Bazin <gbazin@videolan.org>
10 * Damien LUCAS <damien.lucas@anevia.com>
11 * Laurent Aimar <fenrir@via.ecp.fr>
12 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
27 *****************************************************************************/
28 /*****************************************************************************
32 * DVB subtitles coded as strings of characters are not handled correctly.
33 * The character codes in the string should actually be indexes refering to a
34 * character table identified in the subtitle descriptor.
35 *****************************************************************************/
38 #include <vlc/decoder.h>
43 /* #define DEBUG_DVBSUB 1 */
45 #define POSX_TEXT N_("X coordinate of the subpicture")
46 #define POSX_LONGTEXT N_("You can reposition the subpicture by providing another value here." )
48 #define POSY_TEXT N_("Y coordinate of the subpicture")
49 #define POSY_LONGTEXT N_("You can reposition the subpicture by providing another value here." )
51 #define POS_TEXT N_("Subpicture position")
52 #define POS_LONGTEXT N_( \
53 "You can enforce the subpicture position on the video " \
54 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
55 "also use combinations of these values).")
57 #define ENC_POSX_TEXT N_("X coordinate of the encoded subpicture")
58 #define ENC_POSX_LONGTEXT N_("You can reposition the subpicture by providing another value here." )
60 #define ENC_POSY_TEXT N_("Y coordinate of encoded the subpicture")
61 #define ENC_POSY_LONGTEXT N_("You can reposition the subpicture by providing another value here." )
63 #define TIMEOUT_TEXT N_("Timeout of subpictures")
64 #define TIMEOUT_LONGTEXT N_( \
65 "Subpictures get a default timeout of 15 seconds added to their remaining time." \
66 "This will ensure that they are at least the specified time visible.")
68 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
69 static char *ppsz_pos_descriptions[] =
70 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
71 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Open ( vlc_object_t * );
77 static void Close( vlc_object_t * );
78 static subpicture_t *Decode( decoder_t *, block_t ** );
80 static int OpenEncoder ( vlc_object_t * );
81 static void CloseEncoder( vlc_object_t * );
82 static block_t *Encode ( encoder_t *, subpicture_t * );
85 # define DVBSUB_CFG_PREFIX "dvbsub-"
86 set_description( _("DVB subtitles decoder") );
87 set_capability( "decoder", 50 );
88 set_category( CAT_INPUT );
89 set_subcategory( SUBCAT_INPUT_SCODEC );
90 set_callbacks( Open, Close );
92 add_integer( DVBSUB_CFG_PREFIX "position", 8, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
93 change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
94 add_integer( DVBSUB_CFG_PREFIX "x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
95 add_integer( DVBSUB_CFG_PREFIX "y", -1, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
97 # define ENC_CFG_PREFIX "sout-dvbsub-"
99 set_description( _("DVB subtitles encoder") );
100 set_capability( "encoder", 100 );
101 set_callbacks( OpenEncoder, CloseEncoder );
103 add_integer( ENC_CFG_PREFIX "x", -1, NULL, ENC_POSX_TEXT, ENC_POSX_LONGTEXT, VLC_FALSE );
104 add_integer( ENC_CFG_PREFIX "y", -1, NULL, ENC_POSY_TEXT, ENC_POSY_LONGTEXT, VLC_FALSE );
105 add_integer( ENC_CFG_PREFIX "timeout", 15, NULL, TIMEOUT_TEXT, TIMEOUT_LONGTEXT, VLC_FALSE );
108 static const char *ppsz_enc_options[] = { NULL };
110 /****************************************************************************
112 ****************************************************************************
113 * Those structures refer closely to the ETSI 300 743 Object model
114 ****************************************************************************/
116 /* The object definition gives the position of the object in a region */
117 typedef struct dvbsub_objectdef_s
125 char *psz_text; /* for string of characters objects */
127 } dvbsub_objectdef_t;
129 /* The entry in the palette CLUT */
140 typedef struct dvbsub_clut_s
144 dvbsub_color_t c_2b[4];
145 dvbsub_color_t c_4b[16];
146 dvbsub_color_t c_8b[256];
148 struct dvbsub_clut_s *p_next;
152 /* The Region is an aera on the image
153 * with a list of the object definitions associated and a CLUT */
154 typedef struct dvbsub_region_s
169 dvbsub_objectdef_t *p_object_defs;
171 struct dvbsub_region_s *p_next;
175 /* The object definition gives the position of the object in a region */
176 typedef struct dvbsub_regiondef_s
182 } dvbsub_regiondef_t;
184 /* The page defines the list of regions */
188 int i_timeout; /* in seconds */
193 dvbsub_regiondef_t *p_region_defs;
201 /* Decoder internal data */
206 vlc_bool_t b_absolute;
212 dvbsub_page_t *p_page;
213 dvbsub_region_t *p_regions;
214 dvbsub_clut_t *p_cluts;
215 dvbsub_clut_t default_clut;
219 // List of different SEGMENT TYPES
220 // According to EN 300-743, table 2
221 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
222 #define DVBSUB_ST_REGION_COMPOSITION 0x11
223 #define DVBSUB_ST_CLUT_DEFINITION 0x12
224 #define DVBSUB_ST_OBJECT_DATA 0x13
225 #define DVBSUB_ST_ENDOFDISPLAY 0x80
226 #define DVBSUB_ST_STUFFING 0xff
227 // List of different OBJECT TYPES
228 // According to EN 300-743, table 6
229 #define DVBSUB_OT_BASIC_BITMAP 0x00
230 #define DVBSUB_OT_BASIC_CHAR 0x01
231 #define DVBSUB_OT_COMPOSITE_STRING 0x02
233 // According to EN 300-743, table 9
234 #define DVBSUB_DT_2BP_CODE_STRING 0x10
235 #define DVBSUB_DT_4BP_CODE_STRING 0x11
236 #define DVBSUB_DT_8BP_CODE_STRING 0x12
237 #define DVBSUB_DT_24_TABLE_DATA 0x20
238 #define DVBSUB_DT_28_TABLE_DATA 0x21
239 #define DVBSUB_DT_48_TABLE_DATA 0x22
240 #define DVBSUB_DT_END_LINE 0xf0
241 // List of different Page Composition Segment state
242 // According to EN 300-743, 7.2.1 table 3
243 #define DVBSUB_PCS_STATE_ACQUISITION 0x01
244 #define DVBSUB_PCS_STATE_CHANGE 0x10
246 /*****************************************************************************
248 *****************************************************************************/
249 static void decode_segment( decoder_t *, bs_t * );
250 static void decode_page_composition( decoder_t *, bs_t * );
251 static void decode_region_composition( decoder_t *, bs_t * );
252 static void decode_object( decoder_t *, bs_t * );
253 static void decode_clut( decoder_t *, bs_t * );
254 static void free_all( decoder_t * );
256 static void default_clut_init( decoder_t * );
258 static subpicture_t *render( decoder_t * );
260 /*****************************************************************************
261 * Open: probe the decoder and return score
262 *****************************************************************************
263 * Tries to launch a decoder and return score so that the interface is able
265 *****************************************************************************/
266 static int Open( vlc_object_t *p_this )
268 decoder_t *p_dec = (decoder_t *) p_this;
269 decoder_sys_t *p_sys;
273 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
278 p_dec->pf_decode_sub = Decode;
279 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
280 memset( p_sys, 0, sizeof(decoder_sys_t) );
282 p_sys->i_pts = (mtime_t) 0;
283 p_sys->i_id = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
284 p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
286 p_sys->p_regions = NULL;
287 p_sys->p_cluts = NULL;
288 p_sys->p_page = NULL;
290 var_Create( p_this, DVBSUB_CFG_PREFIX "position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
291 var_Get( p_this, DVBSUB_CFG_PREFIX "position", &val );
292 p_sys->i_spu_position = val.i_int;
293 var_Create( p_this, DVBSUB_CFG_PREFIX "x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
294 var_Get( p_this, DVBSUB_CFG_PREFIX "x", &val );
296 var_Create( p_this, DVBSUB_CFG_PREFIX "y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
297 var_Get( p_this, DVBSUB_CFG_PREFIX "y", &val );
300 /* Check if subpicture position was overridden */
301 p_sys->b_absolute = VLC_TRUE;
302 if( posx < 0 || posy < 0)
304 p_sys->b_absolute = VLC_FALSE;
308 else if( posx >= 0 || posy >= 0 )
310 p_sys->i_spu_x = posx;
311 p_sys->i_spu_y = posy;
313 else if( p_sys->i_spu_x < 0 || p_sys->i_spu_y < 0 )
315 p_sys->b_absolute = VLC_FALSE;
320 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
322 default_clut_init( p_dec );
327 /*****************************************************************************
329 *****************************************************************************/
330 static void Close( vlc_object_t *p_this )
332 decoder_t *p_dec = (decoder_t*) p_this;
333 decoder_sys_t *p_sys = p_dec->p_sys;
335 var_Destroy( p_this, DVBSUB_CFG_PREFIX "x" );
336 var_Destroy( p_this, DVBSUB_CFG_PREFIX "y" );
337 var_Destroy( p_this, DVBSUB_CFG_PREFIX "position" );
343 /*****************************************************************************
345 *****************************************************************************/
346 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
348 decoder_sys_t *p_sys = p_dec->p_sys;
350 subpicture_t *p_spu = NULL;
352 if( pp_block == NULL || *pp_block == NULL ) return NULL;
356 p_sys->i_pts = p_block->i_pts;
357 if( p_sys->i_pts <= 0 )
360 /* Some DVB channels send stuffing segments in non-dated packets so
361 * don't complain too loudly. */
362 msg_Warn( p_dec, "non dated subtitle" );
364 block_Release( p_block );
368 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
370 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
372 msg_Dbg( p_dec, "invalid data identifier" );
373 block_Release( p_block );
377 if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
379 msg_Dbg( p_dec, "invalid subtitle stream id" );
380 block_Release( p_block );
385 msg_Dbg( p_dec, "subtitle packet received: "I64Fd, p_sys->i_pts );
388 p_sys->b_page = VLC_FALSE;
389 while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
391 decode_segment( p_dec, &p_sys->bs );
394 if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
396 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
397 block_Release( p_block );
401 /* Check if the page is to be displayed */
402 if( p_sys->p_page && p_sys->b_page ) p_spu = render( p_dec );
404 block_Release( p_block );
409 /* following functions are local */
411 /*****************************************************************************
412 * default_clut_init: default clut as defined in EN 300-743 section 10
413 *****************************************************************************/
414 static void default_clut_init( decoder_t *p_dec )
416 decoder_sys_t *p_sys = p_dec->p_sys;
419 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
420 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
421 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
424 for( i = 0; i < 4; i++ )
426 uint8_t R = 0, G = 0, B = 0, T = 0;
428 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
429 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
430 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
431 else R = G = B = 0x7F;
433 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
434 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
435 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
436 p_sys->default_clut.c_2b[i].T = T;
439 /* 16 entries CLUT */
440 for( i = 0; i < 16; i++ )
442 uint8_t R = 0, G = 0, B = 0, T = 0;
446 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
452 R = (i & 0x1) ? 0xFF : 0;
453 G = (i & 0x2) ? 0xFF : 0;
454 B = (i & 0x4) ? 0xFF : 0;
459 R = (i & 0x1) ? 0x7F : 0;
460 G = (i & 0x2) ? 0x7F : 0;
461 B = (i & 0x4) ? 0x7F : 0;
464 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
465 p_sys->default_clut.c_4b[i].Cr = RGB_TO_U(R,G,B);
466 p_sys->default_clut.c_4b[i].Cb = RGB_TO_V(R,G,B);
467 p_sys->default_clut.c_4b[i].T = T;
470 /* 256 entries CLUT */
471 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
474 static void decode_segment( decoder_t *p_dec, bs_t *s )
476 decoder_sys_t *p_sys = p_dec->p_sys;
481 /* sync_byte (already checked) */
485 i_type = bs_read( s, 8 );
488 i_page_id = bs_read( s, 16 );
491 i_size = bs_show( s, 16 );
493 if( i_page_id != p_sys->i_id && i_page_id != p_sys->i_ancillary_id )
496 msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
497 i_page_id, p_sys->i_id );
499 bs_skip( s, 8 * ( 2 + i_size ) );
503 if( p_sys->i_ancillary_id != p_sys->i_id &&
504 i_type == DVBSUB_ST_PAGE_COMPOSITION &&
505 i_page_id == p_sys->i_ancillary_id )
508 msg_Dbg( p_dec, "skipped invalid ancillary subtitle packet" );
510 bs_skip( s, 8 * ( 2 + i_size ) );
515 if( i_page_id == p_sys->i_id )
516 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
518 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
523 case DVBSUB_ST_PAGE_COMPOSITION:
525 msg_Dbg( p_dec, "decode_page_composition" );
527 decode_page_composition( p_dec, s );
530 case DVBSUB_ST_REGION_COMPOSITION:
532 msg_Dbg( p_dec, "decode_region_composition" );
534 decode_region_composition( p_dec, s );
537 case DVBSUB_ST_CLUT_DEFINITION:
539 msg_Dbg( p_dec, "decode_clut" );
541 decode_clut( p_dec, s );
544 case DVBSUB_ST_OBJECT_DATA:
546 msg_Dbg( p_dec, "decode_object" );
548 decode_object( p_dec, s );
551 case DVBSUB_ST_ENDOFDISPLAY:
553 msg_Dbg( p_dec, "end of display" );
555 bs_skip( s, 8 * ( 2 + i_size ) );
558 case DVBSUB_ST_STUFFING:
560 msg_Dbg( p_dec, "skip stuffing" );
562 bs_skip( s, 8 * ( 2 + i_size ) );
566 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
567 bs_skip( s, 8 * ( 2 + i_size ) );
572 static void decode_clut( decoder_t *p_dec, bs_t *s )
574 decoder_sys_t *p_sys = p_dec->p_sys;
575 uint16_t i_segment_length;
576 uint16_t i_processed_length;
577 dvbsub_clut_t *p_clut, *p_next;
580 i_segment_length = bs_read( s, 16 );
581 i_id = bs_read( s, 8 );
582 i_version = bs_read( s, 4 );
584 /* Check if we already have this clut */
585 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
587 if( p_clut->i_id == i_id ) break;
590 /* Check version number */
591 if( p_clut && p_clut->i_version == i_version )
594 bs_skip( s, 8 * i_segment_length - 12 );
601 msg_Dbg( p_dec, "new clut: %i", i_id );
603 p_clut = malloc( sizeof(dvbsub_clut_t) );
604 p_clut->p_next = p_sys->p_cluts;
605 p_sys->p_cluts = p_clut;
608 /* Initialize to default clut */
609 p_next = p_clut->p_next;
610 *p_clut = p_sys->default_clut;
611 p_clut->p_next = p_next;
613 /* We don't have this version of the CLUT: Parse it */
614 p_clut->i_version = i_version;
616 bs_skip( s, 4 ); /* Reserved bits */
617 i_processed_length = 2;
618 while( i_processed_length < i_segment_length )
620 uint8_t y, cb, cr, t;
624 i_id = bs_read( s, 8 );
625 i_type = bs_read( s, 3 );
629 if( bs_read( s, 1 ) )
632 cr = bs_read( s, 8 );
633 cb = bs_read( s, 8 );
635 i_processed_length += 6;
639 y = bs_read( s, 6 ) << 2;
640 cr = bs_read( s, 4 ) << 4;
641 cb = bs_read( s, 4 ) << 4;
642 t = bs_read( s, 2 ) << 6;
643 i_processed_length += 4;
646 /* We are not entirely compliant here as full transparency is indicated
647 * with a luma value of zero, not a transparency value of 0xff
648 * (full transparency would actually be 0xff + 1). */
655 /* According to EN 300-743 section 7.2.3 note 1, type should
656 * not have more than 1 bit set to one, but some streams don't
657 * respect this note. */
658 if( i_type & 0x04 && i_id < 4 )
660 p_clut->c_2b[i_id].Y = y;
661 p_clut->c_2b[i_id].Cr = cr;
662 p_clut->c_2b[i_id].Cb = cb;
663 p_clut->c_2b[i_id].T = t;
665 if( i_type & 0x02 && i_id < 16 )
667 p_clut->c_4b[i_id].Y = y;
668 p_clut->c_4b[i_id].Cr = cr;
669 p_clut->c_4b[i_id].Cb = cb;
670 p_clut->c_4b[i_id].T = t;
674 p_clut->c_8b[i_id].Y = y;
675 p_clut->c_8b[i_id].Cr = cr;
676 p_clut->c_8b[i_id].Cb = cb;
677 p_clut->c_8b[i_id].T = t;
682 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
684 decoder_sys_t *p_sys = p_dec->p_sys;
685 int i_version, i_state, i_segment_length, i_timeout, i;
687 /* A page is composed by 0 or more region */
689 i_segment_length = bs_read( s, 16 );
690 i_timeout = bs_read( s, 8 );
691 i_version = bs_read( s, 4 );
692 i_state = bs_read( s, 2 );
693 bs_skip( s, 2 ); /* Reserved */
695 if( i_state == DVBSUB_PCS_STATE_CHANGE )
697 /* End of an epoch, reset decoder buffer */
699 msg_Dbg( p_dec, "page composition mode change" );
703 else if( !p_sys->p_page && i_state != DVBSUB_PCS_STATE_ACQUISITION &&
704 i_state != DVBSUB_PCS_STATE_CHANGE )
706 /* Not a full PCS, we need to wait for one */
707 msg_Dbg( p_dec, "didn't receive an acquisition page yet" );
710 /* Try to start decoding even without an acquisition page */
711 bs_skip( s, 8 * (i_segment_length - 2) );
717 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
718 msg_Dbg( p_dec, "acquisition page composition" );
721 /* Check version number */
722 if( p_sys->p_page && p_sys->p_page->i_version == i_version )
724 bs_skip( s, 8 * (i_segment_length - 2) );
727 else if( p_sys->p_page )
729 if( p_sys->p_page->i_region_defs )
730 free( p_sys->p_page->p_region_defs );
731 p_sys->p_page->i_region_defs = 0;
737 msg_Dbg( p_dec, "new page" );
739 /* Allocate a new page */
740 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
743 p_sys->p_page->i_version = i_version;
744 p_sys->p_page->i_timeout = i_timeout;
745 p_sys->b_page = VLC_TRUE;
747 /* Number of regions */
748 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
750 if( p_sys->p_page->i_region_defs == 0 ) return;
752 p_sys->p_page->p_region_defs =
753 malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
754 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
756 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
757 bs_skip( s, 8 ); /* Reserved */
758 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
759 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
762 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
763 i, p_sys->p_page->p_region_defs[i].i_x,
764 p_sys->p_page->p_region_defs[i].i_y );
769 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
771 decoder_sys_t *p_sys = p_dec->p_sys;
772 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
773 int i_segment_length, i_processed_length, i_id, i_version;
774 int i_width, i_height, i_level_comp, i_depth, i_clut;
775 int i_8_bg, i_4_bg, i_2_bg;
778 i_segment_length = bs_read( s, 16 );
779 i_id = bs_read( s, 8 );
780 i_version = bs_read( s, 4 );
782 /* Check if we already have this region */
783 for( p_region = p_sys->p_regions; p_region != NULL;
784 p_region = p_region->p_next )
786 pp_region = &p_region->p_next;
787 if( p_region->i_id == i_id ) break;
790 /* Check version number */
791 if( p_region && p_region->i_version == i_version )
793 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
800 msg_Dbg( p_dec, "new region: %i", i_id );
802 p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
803 memset( p_region, 0, sizeof(dvbsub_region_t) );
804 p_region->p_object_defs = NULL;
805 p_region->p_pixbuf = NULL;
806 p_region->p_next = NULL;
809 /* Region attributes */
810 p_region->i_id = i_id;
811 p_region->i_version = i_version;
812 b_fill = bs_read( s, 1 );
813 bs_skip( s, 3 ); /* Reserved */
815 i_width = bs_read( s, 16 );
816 i_height = bs_read( s, 16 );
817 i_level_comp = bs_read( s, 3 );
818 i_depth = bs_read( s, 3 );
819 bs_skip( s, 2 ); /* Reserved */
820 i_clut = bs_read( s, 8 );
822 i_8_bg = bs_read( s, 8 );
823 i_4_bg = bs_read( s, 4 );
824 i_2_bg = bs_read( s, 2 );
825 bs_skip( s, 2 ); /* Reserved */
827 /* Free old object defs */
828 while( p_region->i_object_defs )
830 int i = p_region->i_object_defs - 1;
831 if( p_region->p_object_defs[i].psz_text )
832 free( p_region->p_object_defs[i].psz_text );
833 if( !i ) free( p_region->p_object_defs );
835 p_region->i_object_defs--;
837 p_region->p_object_defs = NULL;
839 /* Extra sanity checks */
840 if( p_region->i_width != i_width || p_region->i_height != i_height )
842 if( p_region->p_pixbuf )
844 msg_Dbg( p_dec, "region size changed (not allowed)" );
845 free( p_region->p_pixbuf );
848 p_region->p_pixbuf = malloc( i_height * i_width );
849 p_region->i_depth = 0;
852 if( p_region->i_depth && (p_region->i_depth != i_depth ||
853 p_region->i_level_comp != i_level_comp || p_region->i_clut != i_clut) )
855 msg_Dbg( p_dec, "region parameters changed (not allowed)" );
858 /* Erase background of region */
861 int i_background = (p_region->i_depth == 1) ? i_2_bg :
862 (p_region->i_depth == 2) ? i_4_bg : i_8_bg;
863 memset( p_region->p_pixbuf, i_background, i_width * i_height );
866 p_region->i_width = i_width;
867 p_region->i_height = i_height;
868 p_region->i_level_comp = i_level_comp;
869 p_region->i_depth = i_depth;
870 p_region->i_clut = i_clut;
872 /* List of objects in the region */
873 i_processed_length = 10;
874 while( i_processed_length < i_segment_length )
876 dvbsub_objectdef_t *p_obj;
878 /* We create a new object */
879 p_region->i_object_defs++;
880 p_region->p_object_defs =
881 realloc( p_region->p_object_defs,
882 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
884 /* We parse object properties */
885 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
886 p_obj->i_id = bs_read( s, 16 );
887 p_obj->i_type = bs_read( s, 2 );
888 bs_skip( s, 2 ); /* Provider */
889 p_obj->i_x = bs_read( s, 12 );
890 bs_skip( s, 4 ); /* Reserved */
891 p_obj->i_y = bs_read( s, 12 );
894 i_processed_length += 6;
896 if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
897 p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
899 p_obj->i_fg_pc = bs_read( s, 8 );
900 p_obj->i_bg_pc = bs_read( s, 8 );
901 i_processed_length += 2;
906 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
908 static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
909 static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
910 static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
912 static void decode_object( decoder_t *p_dec, bs_t *s )
914 decoder_sys_t *p_sys = p_dec->p_sys;
915 dvbsub_region_t *p_region;
916 int i_segment_length, i_coding_method, i_version, i_id, i;
917 vlc_bool_t b_non_modify_color;
919 /* ETSI 300-743 paragraph 7.2.4
920 * sync_byte, segment_type and page_id have already been processed.
922 i_segment_length = bs_read( s, 16 );
923 i_id = bs_read( s, 16 );
924 i_version = bs_read( s, 4 );
925 i_coding_method = bs_read( s, 2 );
927 if( i_coding_method > 1 )
929 msg_Dbg( p_dec, "Unknown DVB subtitling coding %d is not handled!", i_coding_method );
930 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
934 /* Check if the object needs to be rendered in at least one
936 for( p_region = p_sys->p_regions; p_region != NULL;
937 p_region = p_region->p_next )
939 for( i = 0; i < p_region->i_object_defs; i++ )
940 if( p_region->p_object_defs[i].i_id == i_id ) break;
942 if( i != p_region->i_object_defs ) break;
946 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
951 msg_Dbg( p_dec, "new object: %i", i_id );
954 b_non_modify_color = bs_read( s, 1 );
955 bs_skip( s, 1 ); /* Reserved */
957 if( i_coding_method == 0x00 )
959 int i_topfield, i_bottomfield;
960 uint8_t *p_topfield, *p_bottomfield;
962 i_topfield = bs_read( s, 16 );
963 i_bottomfield = bs_read( s, 16 );
964 p_topfield = s->p_start + bs_pos( s ) / 8;
965 p_bottomfield = p_topfield + i_topfield;
967 bs_skip( s, 8 * (i_segment_length - 7) );
970 if( i_segment_length < i_topfield + i_bottomfield + 7 ||
971 p_topfield + i_topfield + i_bottomfield > s->p_end )
973 msg_Dbg( p_dec, "corrupted object data" );
977 for( p_region = p_sys->p_regions; p_region != NULL;
978 p_region = p_region->p_next )
980 for( i = 0; i < p_region->i_object_defs; i++ )
982 if( p_region->p_object_defs[i].i_id != i_id ) continue;
984 dvbsub_render_pdata( p_dec, p_region,
985 p_region->p_object_defs[i].i_x,
986 p_region->p_object_defs[i].i_y,
987 p_topfield, i_topfield );
991 dvbsub_render_pdata( p_dec, p_region,
992 p_region->p_object_defs[i].i_x,
993 p_region->p_object_defs[i].i_y + 1,
994 p_bottomfield, i_bottomfield );
998 /* Duplicate the top field */
999 dvbsub_render_pdata( p_dec, p_region,
1000 p_region->p_object_defs[i].i_x,
1001 p_region->p_object_defs[i].i_y + 1,
1002 p_topfield, i_topfield );
1009 /* DVB subtitling as characters */
1010 int i_number_of_codes = bs_read( s, 8 );
1011 uint8_t* p_start = s->p_start + bs_pos( s ) / 8;
1014 if( i_segment_length < i_number_of_codes*2 + 4 ||
1015 p_start + i_number_of_codes*2 > s->p_end )
1017 msg_Dbg( p_dec, "corrupted object data" );
1021 for( p_region = p_sys->p_regions; p_region != NULL;
1022 p_region = p_region->p_next )
1024 for( i = 0; i < p_region->i_object_defs; i++ )
1028 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1030 p_region->p_object_defs[i].psz_text =
1031 realloc( p_region->p_object_defs[i].psz_text,
1032 i_number_of_codes + 1 );
1034 for( j = 0; j < i_number_of_codes; j++ )
1036 p_region->p_object_defs[i].psz_text[j] = bs_read( s, 16 );
1038 p_region->p_object_defs[i].psz_text[j] = 0;
1044 msg_Dbg( p_dec, "end object: %i", i_id );
1048 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
1050 uint8_t *p_field, int i_field )
1057 if( !p_region->p_pixbuf )
1059 msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
1062 if( i_y < 0 || i_x < 0 || i_y >= p_region->i_height ||
1063 i_x >= p_region->i_width )
1065 msg_Dbg( p_dec, "invalid offset (%i,%i)", i_x, i_y );
1069 p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
1070 bs_init( &bs, p_field, i_field );
1072 while( !bs_eof( &bs ) )
1075 if( i_y >= p_region->i_height ) return;
1077 switch( bs_read( &bs, 8 ) )
1080 dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1085 dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1090 dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1097 /* We don't use map tables */
1100 case 0xf0: /* End of line code */
1101 p_pixbuf += 2*p_region->i_width;
1102 i_offset = 0; i_y += 2;
1108 static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1110 vlc_bool_t b_stop = 0;
1112 while( !b_stop && !bs_eof( s ) )
1114 int i_count = 0, i_color = 0;
1116 if( (i_color = bs_read( s, 2 )) != 0x00 )
1122 if( bs_read( s, 1 ) == 0x01 ) // Switch1
1124 i_count = 3 + bs_read( s, 3 );
1125 i_color = bs_read( s, 2 );
1129 if( bs_read( s, 1 ) == 0x00 ) //Switch2
1131 switch( bs_read( s, 2 ) ) //Switch3
1140 i_count = 12 + bs_read( s, 4 );
1141 i_color = bs_read( s, 2 );
1144 i_count = 29 + bs_read( s, 8 );
1145 i_color = bs_read( s, 2 );
1153 /* 1 pixel color 0 */
1159 if( !i_count ) continue;
1162 if( i_count + *pi_off > i_width ) break;
1164 if( i_count == 1 ) p[*pi_off] = i_color;
1165 else memset( p + *pi_off, i_color, i_count );
1167 (*pi_off) += i_count;
1173 static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1175 vlc_bool_t b_stop = 0;
1177 while( !b_stop && !bs_eof( s ) )
1179 int i_count = 0, i_color = 0;
1181 if( (i_color = bs_read( s, 4 )) != 0x00 )
1188 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1190 if( bs_show( s, 3 ) != 0x00 )
1192 i_count = 2 + bs_read( s, 3 );
1202 if( bs_read( s, 1 ) == 0x00) //Switch2
1204 i_count = 4 + bs_read( s, 2 );
1205 i_color = bs_read( s, 4 );
1209 switch ( bs_read( s, 2 ) ) //Switch3
1218 i_count = 9 + bs_read( s, 4 );
1219 i_color = bs_read( s, 4 );
1222 i_count= 25 + bs_read( s, 8 );
1223 i_color = bs_read( s, 4 );
1230 if( !i_count ) continue;
1233 if( i_count + *pi_off > i_width ) break;
1235 if( i_count == 1 ) p[*pi_off] = i_color;
1236 else memset( p + *pi_off, i_color, i_count );
1238 (*pi_off) += i_count;
1244 static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1246 vlc_bool_t b_stop = 0;
1248 while( !b_stop && !bs_eof( s ) )
1250 int i_count = 0, i_color = 0;
1252 if( (i_color = bs_read( s, 8 )) != 0x00 )
1259 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1261 if( bs_show( s, 7 ) != 0x00 )
1263 i_count = bs_read( s, 7 );
1273 i_count = bs_read( s, 7 );
1274 i_color = bs_read( s, 8 );
1278 if( !i_count ) continue;
1281 if( i_count + *pi_off > i_width ) break;
1283 if( i_count == 1 ) p[*pi_off] = i_color;
1284 else memset( p + *pi_off, i_color, i_count );
1286 (*pi_off) += i_count;
1292 static void free_all( decoder_t *p_dec )
1294 decoder_sys_t *p_sys = p_dec->p_sys;
1295 dvbsub_region_t *p_reg, *p_reg_next;
1296 dvbsub_clut_t *p_clut, *p_clut_next;
1298 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
1300 p_clut_next = p_clut->p_next;
1303 p_sys->p_cluts = NULL;
1305 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1309 p_reg_next = p_reg->p_next;
1310 for( i = 0; i < p_reg->i_object_defs; i++ )
1311 if( p_reg->p_object_defs[i].psz_text )
1312 free( p_reg->p_object_defs[i].psz_text );
1313 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1314 if( p_reg->p_pixbuf ) free( p_reg->p_pixbuf );
1317 p_sys->p_regions = NULL;
1321 if( p_sys->p_page->i_region_defs )
1322 free( p_sys->p_page->p_region_defs );
1323 free( p_sys->p_page );
1325 p_sys->p_page = NULL;
1328 static subpicture_t *render( decoder_t *p_dec )
1330 decoder_sys_t *p_sys = p_dec->p_sys;
1331 subpicture_t *p_spu;
1332 subpicture_region_t **pp_spu_region;
1333 int i, j, i_timeout = 0;
1335 /* Allocate the subpicture internal data. */
1336 p_spu = p_dec->pf_spu_buffer_new( p_dec );
1337 if( !p_spu ) return NULL;
1339 pp_spu_region = &p_spu->p_region;
1341 /* Loop on region definitions */
1344 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1347 for( i = 0; p_sys->p_page && i < p_sys->p_page->i_region_defs; i++ )
1349 dvbsub_region_t *p_region;
1350 dvbsub_regiondef_t *p_regiondef;
1351 dvbsub_clut_t *p_clut;
1352 dvbsub_color_t *p_color;
1353 subpicture_region_t *p_spu_region;
1354 uint8_t *p_src, *p_dst;
1358 i_timeout = p_sys->p_page->i_timeout;
1360 p_regiondef = &p_sys->p_page->p_region_defs[i];
1363 msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
1364 p_regiondef->i_x, p_regiondef->i_y );
1367 /* Find associated region */
1368 for( p_region = p_sys->p_regions; p_region != NULL;
1369 p_region = p_region->p_next )
1371 if( p_regiondef->i_id == p_region->i_id ) break;
1376 msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
1380 /* Find associated CLUT */
1381 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
1383 if( p_region->i_clut == p_clut->i_id ) break;
1387 msg_Dbg( p_dec, "clut %i not found", p_region->i_clut );
1391 /* Create new SPU region */
1392 memset( &fmt, 0, sizeof(video_format_t) );
1393 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
1394 fmt.i_aspect = 0; /* 0 means use aspect ratio of background video */
1395 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1396 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1397 fmt.i_x_offset = fmt.i_y_offset = 0;
1398 p_spu_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
1401 msg_Err( p_dec, "cannot allocate SPU region" );
1404 p_spu_region->i_x = p_regiondef->i_x;
1405 p_spu_region->i_y = p_regiondef->i_y;
1406 *pp_spu_region = p_spu_region;
1407 pp_spu_region = &p_spu_region->p_next;
1410 fmt.p_palette->i_entries = p_region->i_depth == 1 ? 4 :
1411 p_region->i_depth == 2 ? 16 : 256;
1412 p_color = (p_region->i_depth == 1) ? p_clut->c_2b :
1413 (p_region->i_depth == 2) ? p_clut->c_4b : p_clut->c_8b;
1414 for( j = 0; j < fmt.p_palette->i_entries; j++ )
1416 fmt.p_palette->palette[j][0] = p_color[j].Y;
1417 fmt.p_palette->palette[j][1] = p_color[j].Cr;
1418 fmt.p_palette->palette[j][2] = p_color[j].Cb;
1419 fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1422 p_src = p_region->p_pixbuf;
1423 p_dst = p_spu_region->picture.Y_PIXELS;
1424 i_pitch = p_spu_region->picture.Y_PITCH;
1426 /* Copy pixel buffer */
1427 for( j = 0; j < p_region->i_height; j++ )
1429 memcpy( p_dst, p_src, p_region->i_width );
1430 p_src += p_region->i_width;
1434 /* Check subtitles encoded as strings of characters
1435 * (since there are not rendered in the pixbuffer) */
1436 for( j = 0; j < p_region->i_object_defs; j++ )
1438 dvbsub_objectdef_t *p_object_def = &p_region->p_object_defs[i];
1440 if( p_object_def->i_type != 1 || !p_object_def->psz_text )
1443 /* Create new SPU region */
1444 memset( &fmt, 0, sizeof(video_format_t) );
1445 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
1446 fmt.i_aspect = VOUT_ASPECT_FACTOR;
1447 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1448 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1449 fmt.i_x_offset = fmt.i_y_offset = 0;
1450 p_spu_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
1453 msg_Err( p_dec, "cannot allocate SPU region" );
1457 p_spu_region->psz_text = strdup( p_object_def->psz_text );
1458 p_spu_region->i_x = p_regiondef->i_x + p_object_def->i_x;
1459 p_spu_region->i_y = p_regiondef->i_y + p_object_def->i_y;
1460 *pp_spu_region = p_spu_region;
1461 pp_spu_region = &p_spu_region->p_next;
1465 /* Set the pf_render callback */
1466 p_spu->i_start = p_sys->i_pts;
1467 p_spu->i_stop = p_spu->i_start + (mtime_t) (i_timeout * 1000000);
1468 p_spu->b_ephemer = VLC_TRUE;
1469 p_spu->b_fade = VLC_TRUE;
1471 /* Correct positioning of SPU */
1472 p_spu->b_absolute = p_sys->b_absolute;
1473 p_spu->i_flags = p_sys->i_spu_position;
1474 p_spu->i_x = p_sys->i_spu_x;
1475 p_spu->i_y = p_sys->i_spu_y;
1480 /*****************************************************************************
1481 * encoder_sys_t : encoder descriptor
1482 *****************************************************************************/
1483 typedef struct encoder_region_t
1490 struct encoder_sys_t
1492 unsigned int i_page_ver;
1493 unsigned int i_region_ver;
1494 unsigned int i_clut_ver;
1497 encoder_region_t *p_regions;
1501 /* subpicture positioning */
1504 int i_timeout_delay;
1507 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1508 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1509 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1510 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1512 /*****************************************************************************
1513 * OpenEncoder: probe the encoder and return score
1514 *****************************************************************************/
1515 static int OpenEncoder( vlc_object_t *p_this )
1517 encoder_t *p_enc = (encoder_t *)p_this;
1518 encoder_sys_t *p_sys;
1521 if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','v','b','s') &&
1524 return VLC_EGENERIC;
1527 /* Allocate the memory needed to store the decoder's structure */
1528 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1530 msg_Err( p_enc, "out of memory" );
1531 return VLC_EGENERIC;
1533 p_enc->p_sys = p_sys;
1535 p_enc->pf_encode_sub = Encode;
1536 p_enc->fmt_out.i_codec = VLC_FOURCC('d','v','b','s');
1537 p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1;
1539 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1541 p_sys->i_page_ver = 0;
1542 p_sys->i_region_ver = 0;
1543 p_sys->i_clut_ver = 0;
1544 p_sys->i_regions = 0;
1545 p_sys->p_regions = 0;
1547 var_Create( p_this, ENC_CFG_PREFIX "x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
1548 var_Get( p_this, ENC_CFG_PREFIX "x", &val );
1549 p_sys->i_offset_x = val.i_int;
1550 var_Create( p_this, ENC_CFG_PREFIX "y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
1551 var_Get( p_this, ENC_CFG_PREFIX "y", &val );
1552 p_sys->i_offset_y = val.i_int;
1553 var_Create( p_this, ENC_CFG_PREFIX "timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
1554 var_Get( p_this, ENC_CFG_PREFIX "timeout", &val );
1555 p_sys->i_timeout_delay = val.i_int;
1560 /****************************************************************************
1561 * Encode: the whole thing
1562 ****************************************************************************/
1563 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1565 bs_t bits, *s = &bits;
1568 if( !p_subpic || !p_subpic->p_region ) return 0;
1571 msg_Dbg( p_enc, "encoding subpicture" );
1573 p_block = block_New( p_enc, 64000 );
1574 bs_init( s, p_block->p_buffer, p_block->i_buffer );
1576 bs_write( s, 8, 0x20 ); /* Data identifier */
1577 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1579 encode_page_composition( p_enc, s, p_subpic );
1580 encode_region_composition( p_enc, s, p_subpic );
1581 encode_clut( p_enc, s, p_subpic );
1582 encode_object( p_enc, s, p_subpic );
1584 /* End of display */
1585 bs_write( s, 8, 0x0f ); /* Sync byte */
1586 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1587 bs_write( s, 16, 1 ); /* Page id */
1588 bs_write( s, 16, 0 ); /* Segment length */
1590 bs_write( s, 8, 0xff );/* End marker */
1591 p_block->i_buffer = bs_pos( s ) / 8;
1592 p_block->i_pts = p_block->i_dts = p_subpic->i_start;
1593 if( !p_subpic->b_ephemer && p_subpic->i_stop > p_subpic->i_start )
1595 block_t *p_block_stop;
1597 p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
1599 /* Send another (empty) subtitle to signal the end of display */
1600 p_block_stop = block_New( p_enc, 64000 );
1601 bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
1602 bs_write( s, 8, 0x20 ); /* Data identifier */
1603 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1604 encode_page_composition( p_enc, s, 0 );
1605 bs_write( s, 8, 0x0f ); /* Sync byte */
1606 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1607 bs_write( s, 16, 1 ); /* Page id */
1608 bs_write( s, 16, 0 ); /* Segment length */
1609 bs_write( s, 8, 0xff );/* End marker */
1610 p_block_stop->i_buffer = bs_pos( s ) / 8;
1611 p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
1612 block_ChainAppend( &p_block, p_block_stop );
1613 p_block_stop->i_length = 100000; /* p_subpic->i_stop - p_subpic->i_start; */
1616 msg_Dbg( p_enc, "subpicture encoded properly" );
1621 /*****************************************************************************
1622 * CloseEncoder: encoder destruction
1623 *****************************************************************************/
1624 static void CloseEncoder( vlc_object_t *p_this )
1626 encoder_t *p_enc = (encoder_t *)p_this;
1627 encoder_sys_t *p_sys = p_enc->p_sys;
1629 var_Destroy( p_this , ENC_CFG_PREFIX "x" );
1630 var_Destroy( p_this , ENC_CFG_PREFIX "y" );
1631 var_Destroy( p_this , ENC_CFG_PREFIX "timeout" );
1633 if( p_sys->i_regions ) free( p_sys->p_regions );
1637 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
1638 subpicture_t *p_subpic )
1640 encoder_sys_t *p_sys = p_enc->p_sys;
1641 subpicture_region_t *p_region;
1642 vlc_bool_t b_mode_change = VLC_FALSE;
1643 int i_regions, i_timeout;
1645 bs_write( s, 8, 0x0f ); /* Sync byte */
1646 bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
1647 bs_write( s, 16, 1 ); /* Page id */
1649 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
1650 p_region; p_region = p_region->p_next, i_regions++ )
1652 if( i_regions >= p_sys->i_regions )
1654 encoder_region_t region;
1655 region.i_width = region.i_height = 0;
1657 realloc( p_sys->p_regions, sizeof(encoder_region_t) *
1658 (p_sys->i_regions + 1) );
1659 p_sys->p_regions[p_sys->i_regions++] = region;
1662 if( p_sys->p_regions[i_regions].i_width <
1663 (int)p_region->fmt.i_visible_width )
1665 b_mode_change = VLC_TRUE;
1666 msg_Dbg( p_enc, "region %i width change: %i -> %i",
1667 i_regions, p_sys->p_regions[i_regions].i_width,
1668 p_region->fmt.i_visible_width );
1669 p_sys->p_regions[i_regions].i_width =
1670 p_region->fmt.i_visible_width;
1672 if( p_sys->p_regions[i_regions].i_height <
1673 (int)p_region->fmt.i_visible_height )
1675 b_mode_change = VLC_TRUE;
1676 msg_Dbg( p_enc, "region %i height change: %i -> %i",
1677 i_regions, p_sys->p_regions[i_regions].i_height,
1678 p_region->fmt.i_visible_height );
1679 p_sys->p_regions[i_regions].i_height =
1680 p_region->fmt.i_visible_height;
1684 bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
1687 if( p_subpic && !p_subpic->b_ephemer &&
1688 p_subpic->i_stop > p_subpic->i_start )
1690 i_timeout = (p_subpic->i_stop - p_subpic->i_start) / 1000000;
1693 bs_write( s, 8, i_timeout + p_sys->i_timeout_delay ); /* Timeout */
1694 bs_write( s, 4, p_sys->i_page_ver++ );
1695 bs_write( s, 2, b_mode_change ?
1696 DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
1697 bs_write( s, 2, 0 ); /* Reserved */
1699 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
1700 p_region; p_region = p_region->p_next, i_regions++ )
1702 bs_write( s, 8, i_regions );
1703 bs_write( s, 8, 0 ); /* Reserved */
1704 if( (p_sys->i_offset_x > 0) && (p_sys->i_offset_y > 0) )
1706 bs_write( s, 16, p_sys->i_offset_x ); /* override x position */
1707 bs_write( s, 16, p_sys->i_offset_y ); /* override y position */
1711 bs_write( s, 16, p_region->i_x );
1712 bs_write( s, 16, p_region->i_y );
1717 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1719 encoder_sys_t *p_sys = p_enc->p_sys;
1720 subpicture_region_t *p_region = p_subpic->p_region;
1721 video_palette_t *p_pal, pal;
1725 if( !p_region ) return;
1727 if( p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','P') )
1729 p_pal = p_region->fmt.p_palette;
1734 for( i = 0; i < 4; i++ )
1736 pal.palette[i][0] = 0;
1737 pal.palette[i][1] = 0;
1738 pal.palette[i][2] = 0;
1739 pal.palette[i][3] = 0;
1744 bs_write( s, 8, 0x0f ); /* Sync byte */
1745 bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
1746 bs_write( s, 16, 1 ); /* Page id */
1748 bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
1749 bs_write( s, 8, 1 ); /* Clut id */
1750 bs_write( s, 4, p_sys->i_clut_ver++ );
1751 bs_write( s, 4, 0 ); /* Reserved */
1753 for( i = 0; i < p_pal->i_entries; i++ )
1755 bs_write( s, 8, i ); /* Clut entry id */
1756 bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
1757 bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
1758 bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
1759 bs_write( s, 4, 0 ); /* Reserved */
1760 bs_write( s, 1, 1 ); /* Full range flag */
1761 bs_write( s, 8, p_pal->palette[i][3] ? /* Y value */
1762 (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
1763 bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
1764 bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
1765 bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
1769 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
1770 subpicture_t *p_subpic )
1772 encoder_sys_t *p_sys = p_enc->p_sys;
1773 subpicture_region_t *p_region;
1776 for( i_region = 0, p_region = p_subpic->p_region; p_region;
1777 p_region = p_region->p_next, i_region++ )
1779 int i_entries = 4, i_depth = 0x1, i_bg = 0;
1781 p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T');
1785 video_palette_t *p_pal = p_region->fmt.p_palette;
1789 msg_Err( p_enc, "subpicture has no palette - ignoring it" );
1793 i_entries = p_pal->i_entries;
1794 i_depth = i_entries == 4 ? 0x1 : i_entries == 16 ? 0x2 : 0x3;
1796 for( i_bg = 0; i_bg < p_pal->i_entries; i_bg++ )
1798 if( !p_pal->palette[i_bg][3] ) break;
1802 bs_write( s, 8, 0x0f ); /* Sync byte */
1803 bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
1804 bs_write( s, 16, 1 ); /* Page id */
1806 bs_write( s, 16, 10 + 6 + (b_text ? 2 : 0) ); /* Segment length */
1807 bs_write( s, 8, i_region );
1808 bs_write( s, 4, p_sys->i_region_ver++ );
1810 /* Region attributes */
1811 bs_write( s, 1, i_bg < i_entries ); /* Fill */
1812 bs_write( s, 3, 0 ); /* Reserved */
1813 bs_write( s, 16, p_sys->p_regions[i_region].i_width );
1814 bs_write( s, 16, p_sys->p_regions[i_region].i_height );
1815 bs_write( s, 3, i_depth ); /* Region level of compatibility */
1816 bs_write( s, 3, i_depth ); /* Region depth */
1817 bs_write( s, 2, 0 ); /* Reserved */
1818 bs_write( s, 8, 1 ); /* Clut id */
1819 bs_write( s, 8, i_bg ); /* region 8bit pixel code */
1820 bs_write( s, 4, i_bg ); /* region 4bit pixel code */
1821 bs_write( s, 2, i_bg ); /* region 2bit pixel code */
1822 bs_write( s, 2, 0 ); /* Reserved */
1824 /* In our implementation we only have 1 object per region */
1825 bs_write( s, 16, i_region );
1826 bs_write( s, 2, b_text ? DVBSUB_OT_BASIC_CHAR:DVBSUB_OT_BASIC_BITMAP );
1827 bs_write( s, 2, 0 ); /* object provider flag */
1828 bs_write( s, 12, 0 );/* object horizontal position */
1829 bs_write( s, 4, 0 ); /* Reserved */
1830 bs_write( s, 12, 0 );/* object vertical position */
1834 bs_write( s, 8, 1 ); /* foreground pixel code */
1835 bs_write( s, 8, 0 ); /* background pixel code */
1840 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1841 subpicture_region_t *p_region,
1844 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1846 encoder_sys_t *p_sys = p_enc->p_sys;
1847 subpicture_region_t *p_region;
1850 int i_length_pos, i_update_pos, i_pixel_data_pos;
1852 for( i_region = 0, p_region = p_subpic->p_region; p_region;
1853 p_region = p_region->p_next, i_region++ )
1855 bs_write( s, 8, 0x0f ); /* Sync byte */
1856 bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
1857 bs_write( s, 16, 1 ); /* Page id */
1859 i_length_pos = bs_pos( s );
1860 bs_write( s, 16, 0 ); /* Segment length */
1861 bs_write( s, 16, i_region ); /* Object id */
1862 bs_write( s, 4, p_sys->i_region_ver++ );
1864 /* object coding method */
1865 switch( p_region->fmt.i_chroma )
1867 case VLC_FOURCC( 'Y','U','V','P' ):
1868 bs_write( s, 2, 0 );
1870 case VLC_FOURCC( 'T','E','X','T' ):
1871 bs_write( s, 2, 1 );
1874 msg_Err( p_enc, "FOURCC %d not supported by encoder.", p_region->fmt.i_chroma );
1878 bs_write( s, 1, 0 ); /* non modifying color flag */
1879 bs_write( s, 1, 0 ); /* Reserved */
1881 if( p_region->fmt.i_chroma == VLC_FOURCC( 'T','E','X','T' ) )
1885 if( !p_region->psz_text ) continue;
1887 i_size = __MIN( strlen( p_region->psz_text ), 256 );
1889 bs_write( s, 8, i_size ); /* number of characters in string */
1890 for( i = 0; i < i_size; i++ )
1892 bs_write( s, 16, p_region->psz_text[i] );
1895 /* Update segment length */
1896 SetWBE( &s->p_start[i_length_pos/8],
1897 (bs_pos(s) - i_length_pos)/8 -2 );
1901 /* Coding of a bitmap object */
1902 i_update_pos = bs_pos( s );
1903 bs_write( s, 16, 0 ); /* topfield data block length */
1904 bs_write( s, 16, 0 ); /* bottomfield data block length */
1907 i_pixel_data_pos = bs_pos( s );
1908 encode_pixel_data( p_enc, s, p_region, VLC_TRUE );
1909 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1910 SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
1913 i_pixel_data_pos = bs_pos( s );
1914 encode_pixel_data( p_enc, s, p_region, VLC_FALSE );
1915 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1916 SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
1918 /* Stuffing for word alignment */
1920 if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
1922 /* Update segment length */
1923 SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
1927 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1928 subpicture_region_t *p_region,
1930 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1931 subpicture_region_t *p_region,
1933 static void encode_pixel_line_8bp( encoder_t *p_enc, bs_t *s,
1934 subpicture_region_t *p_region,
1936 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1937 subpicture_region_t *p_region,
1940 unsigned int i_line;
1943 if( p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1945 /* Encode line by line */
1946 for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
1949 switch( p_region->fmt.p_palette->i_entries )
1952 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
1953 encode_pixel_line_2bp( p_enc, s, p_region, i_line );
1957 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
1958 encode_pixel_line_4bp( p_enc, s, p_region, i_line );
1962 bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
1963 encode_pixel_line_8bp( p_enc, s, p_region, i_line );
1967 msg_Err( p_enc, "subpicture palette (%i) not handled",
1968 p_region->fmt.p_palette->i_entries );
1972 bs_write( s, 8, 0xf0 ); /* End of object line code */
1976 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1977 subpicture_region_t *p_region,
1980 unsigned int i, i_length = 0;
1981 int i_pitch = p_region->picture.p->i_pitch;
1982 uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1983 int i_last_pixel = p_data[0];
1985 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1987 if( i != p_region->fmt.i_visible_width &&
1988 p_data[i] == i_last_pixel && i_length != 284 )
1994 if( i_length == 1 || i_length == 11 || i_length == 28 )
1996 /* 2bit/pixel code */
1997 if( i_last_pixel ) bs_write( s, 2, i_last_pixel );
2000 bs_write( s, 2, 0 );
2001 bs_write( s, 1, 0 );
2002 bs_write( s, 1, 1 ); /* pseudo color 0 */
2011 bs_write( s, 2, i_last_pixel );
2012 bs_write( s, 2, i_last_pixel );
2016 bs_write( s, 2, 0 );
2017 bs_write( s, 1, 0 );
2018 bs_write( s, 1, 0 );
2019 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2022 else if( i_length > 2 )
2024 bs_write( s, 2, 0 );
2025 if( i_length <= 10 )
2027 bs_write( s, 1, 1 );
2028 bs_write( s, 3, i_length - 3 );
2029 bs_write( s, 2, i_last_pixel );
2033 bs_write( s, 1, 0 );
2034 bs_write( s, 1, 0 );
2036 if( i_length <= 27 )
2038 bs_write( s, 2, 2 );
2039 bs_write( s, 4, i_length - 12 );
2040 bs_write( s, 2, i_last_pixel );
2044 bs_write( s, 2, 3 );
2045 bs_write( s, 8, i_length - 29 );
2046 bs_write( s, 2, i_last_pixel );
2051 if( i == p_region->fmt.i_visible_width ) break;
2053 i_last_pixel = p_data[i];
2058 bs_write( s, 2, 0 );
2059 bs_write( s, 1, 0 );
2060 bs_write( s, 1, 0 );
2061 bs_write( s, 2, 0 );
2067 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
2068 subpicture_region_t *p_region,
2071 unsigned int i, i_length = 0;
2072 int i_pitch = p_region->picture.p->i_pitch;
2073 uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
2074 int i_last_pixel = p_data[0];
2076 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2078 if( i != p_region->fmt.i_visible_width &&
2079 p_data[i] == i_last_pixel && i_length != 280 )
2085 if( i_length == 1 || (i_length == 3 && i_last_pixel) || i_length == 8 )
2087 /* 4bit/pixel code */
2088 if( i_last_pixel ) bs_write( s, 4, i_last_pixel );
2091 bs_write( s, 4, 0 );
2092 bs_write( s, 1, 1 );
2093 bs_write( s, 1, 1 );
2094 bs_write( s, 2, 0 ); /* pseudo color 0 */
2103 bs_write( s, 4, i_last_pixel );
2104 bs_write( s, 4, i_last_pixel );
2108 bs_write( s, 4, 0 );
2109 bs_write( s, 1, 1 );
2110 bs_write( s, 1, 1 );
2111 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2114 else if( !i_last_pixel && i_length >= 3 && i_length <= 9 )
2116 bs_write( s, 4, 0 );
2117 bs_write( s, 1, 0 );
2118 bs_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
2120 else if( i_length > 2 )
2122 bs_write( s, 4, 0 );
2123 bs_write( s, 1, 1 );
2127 bs_write( s, 1, 0 );
2128 bs_write( s, 2, i_length - 4 );
2129 bs_write( s, 4, i_last_pixel );
2133 bs_write( s, 1, 1 );
2135 if( i_length <= 24 )
2137 bs_write( s, 2, 2 );
2138 bs_write( s, 4, i_length - 9 );
2139 bs_write( s, 4, i_last_pixel );
2143 bs_write( s, 2, 3 );
2144 bs_write( s, 8, i_length - 25 );
2145 bs_write( s, 4, i_last_pixel );
2150 if( i == p_region->fmt.i_visible_width ) break;
2152 i_last_pixel = p_data[i];
2157 bs_write( s, 8, 0 );
2163 static void encode_pixel_line_8bp( encoder_t *p_enc, bs_t *s,
2164 subpicture_region_t *p_region,
2167 unsigned int i, i_length = 0;
2168 int i_pitch = p_region->picture.p->i_pitch;
2169 uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
2170 int i_last_pixel = p_data[0];
2172 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2174 if( i != p_region->fmt.i_visible_width &&
2175 p_data[i] == i_last_pixel && i_length != 127 )
2181 if( i_length == 1 && i_last_pixel )
2183 /* 8bit/pixel code */
2184 bs_write( s, 8, i_last_pixel );
2186 else if( i_length == 2 && i_last_pixel )
2188 /* 8bit/pixel code */
2189 bs_write( s, 8, i_last_pixel );
2190 bs_write( s, 8, i_last_pixel );
2192 else if( i_length <= 127 )
2194 bs_write( s, 8, 0 );
2198 bs_write( s, 1, 0 );
2199 bs_write( s, 7, i_length ); /* pseudo color 0 */
2203 bs_write( s, 1, 1 );
2204 bs_write( s, 7, i_length );
2205 bs_write( s, 8, i_last_pixel );
2209 if( i == p_region->fmt.i_visible_width ) break;
2211 i_last_pixel = p_data[i];
2216 bs_write( s, 8, 0 );
2217 bs_write( s, 8, 0 );