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-2009 the VideoLAN team
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>
13 * Derk-Jan Hartman <hartman #at# videolan dot org>
14 * Simon Hailes <simon _a_ screen.subtitling.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29 *****************************************************************************/
31 /*****************************************************************************
35 * DVB subtitles coded as strings of characters are not handled correctly.
36 * The character codes in the string should actually be indexes referring to a
37 * character table identified in the subtitle descriptor.
39 * The spec is quite vague in this area, but what is meant is perhaps that it
40 * refers to the character index in the codepage belonging to the language
41 * specified in the subtitle descriptor. Potentially it's designed for widechar
42 * (but not for UTF-*) codepages.
43 *****************************************************************************
45 *****************************************************************************
46 * Notes on DDS (Display Definition Segment)
47 * -----------------------------------------
48 * DDS (Display Definition Segment) tells the decoder how the subtitle image
49 * relates to the video image.
50 * For SD, the subtitle image is always considered to be for display at
51 * 720x576 (although it's assumed that for NTSC, this is 720x480, this
52 * is not documented well) Also, for SD, the subtitle image is drawn 'on
53 * the glass' (i.e. after video scaling, letterbox, etc.)
54 * For 'HD' (subs marked type 0x14/0x24 in PSI), a DDS must be present,
55 * and the subs area is drawn onto the video area (scales if necessary).
56 * The DDS tells the decoder what resolution the subtitle images were
57 * intended for, and hence how to scale the subtitle images for a
58 * particular video size
59 * i.e. if HD video is presented as letterbox, the subs will be in the
60 * same place on the video as if the video was presented on an HD set
61 * indeed, if the HD video was pillarboxed by the decoder, the subs may
62 * be cut off as well as the video. The intent here is that the subs can
63 * be placed accurately on the video - something which was missed in the
66 * A DDS may also specify a window - this is where the subs images are moved so that the (0,0)
67 * origin of decode is offset.
68 ********************************************************************************************/
74 #include <vlc_common.h>
75 #include <vlc_plugin.h>
76 #include <vlc_codec.h>
81 /* #define DEBUG_DVBSUB 1 */
83 #define POSX_TEXT N_("Decoding X coordinate")
84 #define POSX_LONGTEXT N_("X coordinate of the rendered subtitle")
86 #define POSY_TEXT N_("Decoding Y coordinate")
87 #define POSY_LONGTEXT N_("Y coordinate of the rendered subtitle")
89 #define POS_TEXT N_("Subpicture position")
90 #define POS_LONGTEXT N_( \
91 "You can enforce the subpicture position on the video " \
92 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
93 "also use combinations of these values, e.g. 6=top-right).")
95 #define ENC_POSX_TEXT N_("Encoding X coordinate")
96 #define ENC_POSX_LONGTEXT N_("X coordinate of the encoded subtitle" )
97 #define ENC_POSY_TEXT N_("Encoding Y coordinate")
98 #define ENC_POSY_LONGTEXT N_("Y coordinate of the encoded subtitle" )
100 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
101 static const char *const ppsz_pos_descriptions[] =
102 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
103 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
105 /*****************************************************************************
107 *****************************************************************************/
108 static int Open ( vlc_object_t * );
109 static void Close( vlc_object_t * );
110 static subpicture_t *Decode( decoder_t *, block_t ** );
112 static int OpenEncoder ( vlc_object_t * );
113 static void CloseEncoder( vlc_object_t * );
114 static block_t *Encode ( encoder_t *, subpicture_t * );
117 # define DVBSUB_CFG_PREFIX "dvbsub-"
118 set_description( N_("DVB subtitles decoder") )
119 set_shortname( N_("DVB subtitles") )
120 set_capability( "decoder", 50 )
121 set_category( CAT_INPUT )
122 set_subcategory( SUBCAT_INPUT_SCODEC )
123 set_callbacks( Open, Close )
125 add_integer( DVBSUB_CFG_PREFIX "position", 8, NULL, POS_TEXT, POS_LONGTEXT, true )
126 change_integer_list( pi_pos_values, ppsz_pos_descriptions, NULL )
127 add_integer( DVBSUB_CFG_PREFIX "x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, false )
128 add_integer( DVBSUB_CFG_PREFIX "y", -1, NULL, POSY_TEXT, POSY_LONGTEXT, false )
130 # define ENC_CFG_PREFIX "sout-dvbsub-"
132 set_description( N_("DVB subtitles encoder") )
133 set_capability( "encoder", 100 )
134 set_callbacks( OpenEncoder, CloseEncoder )
136 add_integer( ENC_CFG_PREFIX "x", -1, NULL, ENC_POSX_TEXT, ENC_POSX_LONGTEXT, false )
137 add_integer( ENC_CFG_PREFIX "y", -1, NULL, ENC_POSY_TEXT, ENC_POSY_LONGTEXT, false )
138 add_obsolete_integer( ENC_CFG_PREFIX "timeout" ) /* Suppressed since 0.8.5 */
141 static const char *const ppsz_enc_options[] = { "x", "y", NULL };
143 /****************************************************************************
145 ****************************************************************************
146 * Those structures refer closely to the ETSI 300 743 Object model
147 ****************************************************************************/
149 /* The object definition gives the position of the object in a region [7.2.5] */
150 typedef struct dvbsub_objectdef_s
158 char *psz_text; /* for string of characters objects */
160 } dvbsub_objectdef_t;
162 /* The entry in the palette CLUT */
172 /* The displays dimensions [7.2.1] */
173 typedef struct dvbsub_display_s
182 /* these values are only relevant if windowed */
191 typedef struct dvbsub_clut_s
195 dvbsub_color_t c_2b[4];
196 dvbsub_color_t c_4b[16];
197 dvbsub_color_t c_8b[256];
199 struct dvbsub_clut_s *p_next;
203 /* The Region is an aera on the image [7.2.3]
204 * with a list of the object definitions associated and a CLUT */
205 typedef struct dvbsub_region_s
220 dvbsub_objectdef_t *p_object_defs;
222 struct dvbsub_region_s *p_next;
226 /* The object definition gives the position of the object in a region */
227 typedef struct dvbsub_regiondef_s
233 } dvbsub_regiondef_t;
235 /* The page defines the list of regions [7.2.2] */
239 int i_timeout; /* in seconds */
244 dvbsub_regiondef_t *p_region_defs;
252 /* Decoder internal data */
263 dvbsub_page_t *p_page;
264 dvbsub_region_t *p_regions;
265 dvbsub_clut_t *p_cluts;
266 /* this is very small, so keep forever */
267 dvbsub_display_t display;
268 dvbsub_clut_t default_clut;
272 /* List of different SEGMENT TYPES */
273 /* According to EN 300-743, table 2 */
274 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
275 #define DVBSUB_ST_REGION_COMPOSITION 0x11
276 #define DVBSUB_ST_CLUT_DEFINITION 0x12
277 #define DVBSUB_ST_OBJECT_DATA 0x13
278 #define DVBSUB_ST_DISPLAY_DEFINITION 0x14
279 #define DVBSUB_ST_ENDOFDISPLAY 0x80
280 #define DVBSUB_ST_STUFFING 0xff
281 /* List of different OBJECT TYPES */
282 /* According to EN 300-743, table 6 */
283 #define DVBSUB_OT_BASIC_BITMAP 0x00
284 #define DVBSUB_OT_BASIC_CHAR 0x01
285 #define DVBSUB_OT_COMPOSITE_STRING 0x02
286 /* Pixel DATA TYPES */
287 /* According to EN 300-743, table 9 */
288 #define DVBSUB_DT_2BP_CODE_STRING 0x10
289 #define DVBSUB_DT_4BP_CODE_STRING 0x11
290 #define DVBSUB_DT_8BP_CODE_STRING 0x12
291 #define DVBSUB_DT_24_TABLE_DATA 0x20
292 #define DVBSUB_DT_28_TABLE_DATA 0x21
293 #define DVBSUB_DT_48_TABLE_DATA 0x22
294 #define DVBSUB_DT_END_LINE 0xf0
295 /* List of different Page Composition Segment state */
296 /* According to EN 300-743, 7.2.1 table 3 */
297 #define DVBSUB_PCS_STATE_ACQUISITION 0x01
298 #define DVBSUB_PCS_STATE_CHANGE 0x02
300 /*****************************************************************************
302 *****************************************************************************/
303 static void decode_segment( decoder_t *, bs_t * );
304 static void decode_page_composition( decoder_t *, bs_t * );
305 static void decode_region_composition( decoder_t *, bs_t * );
306 static void decode_object( decoder_t *, bs_t * );
307 static void decode_display_definition( decoder_t *, bs_t * );
308 static void decode_clut( decoder_t *, bs_t * );
309 static void free_all( decoder_t * );
311 static void default_clut_init( decoder_t * );
312 static void default_dds_init( decoder_t * );
314 static subpicture_t *render( decoder_t * );
316 /*****************************************************************************
317 * Open: probe the decoder and return score
318 *****************************************************************************
319 * Tries to launch a decoder and return score so that the interface is able
321 *****************************************************************************/
322 static int Open( vlc_object_t *p_this )
324 decoder_t *p_dec = (decoder_t *) p_this;
325 decoder_sys_t *p_sys;
328 if( p_dec->fmt_in.i_codec != VLC_CODEC_DVBS )
333 p_dec->pf_decode_sub = Decode;
334 p_sys = p_dec->p_sys = calloc( 1, sizeof(decoder_sys_t) );
338 p_sys->i_pts = (mtime_t) 0;
339 p_sys->i_id = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
340 p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
342 p_sys->p_regions = NULL;
343 p_sys->p_cluts = NULL;
344 p_sys->p_page = NULL;
346 /* configure for SD res in case DDS is not present */
347 default_dds_init( p_dec );
349 p_sys->i_spu_position = var_CreateGetInteger( p_this,
350 DVBSUB_CFG_PREFIX "position" );
351 i_posx = var_CreateGetInteger( p_this, DVBSUB_CFG_PREFIX "x" );
352 i_posy = var_CreateGetInteger( p_this, DVBSUB_CFG_PREFIX "y" );
354 /* Check if subpicture position was overridden */
355 p_sys->b_absolute = true;
356 p_sys->i_spu_x = p_sys->i_spu_y = 0;
358 if( ( i_posx >= 0 ) && ( i_posy >= 0 ) )
360 p_sys->b_absolute = true;
361 p_sys->i_spu_x = i_posx;
362 p_sys->i_spu_y = i_posy;
365 p_dec->fmt_out.i_cat = SPU_ES;
366 p_dec->fmt_out.i_codec = 0;
368 default_clut_init( p_dec );
373 /*****************************************************************************
375 *****************************************************************************/
376 static void Close( vlc_object_t *p_this )
378 decoder_t *p_dec = (decoder_t*) p_this;
379 decoder_sys_t *p_sys = p_dec->p_sys;
381 var_Destroy( p_this, DVBSUB_CFG_PREFIX "x" );
382 var_Destroy( p_this, DVBSUB_CFG_PREFIX "y" );
383 var_Destroy( p_this, DVBSUB_CFG_PREFIX "position" );
389 /*****************************************************************************
391 *****************************************************************************/
392 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
394 decoder_sys_t *p_sys = p_dec->p_sys;
396 subpicture_t *p_spu = NULL;
398 if( ( pp_block == NULL ) || ( *pp_block == NULL ) ) return NULL;
402 /* configure for SD res in case DDS is not present */
403 /* a change of PTS is a good indication we must get a new DDS */
404 if (p_sys->i_pts != p_block->i_pts)
405 default_dds_init( p_dec );
407 p_sys->i_pts = p_block->i_pts;
408 if( p_sys->i_pts <= 0 )
411 /* Some DVB channels send stuffing segments in non-dated packets so
412 * don't complain too loudly. */
413 msg_Warn( p_dec, "non dated subtitle" );
415 block_Release( p_block );
419 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
421 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
423 msg_Dbg( p_dec, "invalid data identifier" );
424 block_Release( p_block );
428 if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
430 msg_Dbg( p_dec, "invalid subtitle stream id" );
431 block_Release( p_block );
436 msg_Dbg( p_dec, "subtitle packet received: %"PRId64, p_sys->i_pts );
439 p_sys->b_page = false;
440 while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
442 decode_segment( p_dec, &p_sys->bs );
445 if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
447 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
448 block_Release( p_block );
452 /* Check if the page is to be displayed */
453 if( p_sys->p_page && p_sys->b_page )
454 p_spu = render( p_dec );
456 block_Release( p_block );
461 /* following functions are local */
463 /*****************************************************************************
464 * default_clut_init: default clut as defined in EN 300-743 section 10
465 *****************************************************************************/
466 static void default_clut_init( decoder_t *p_dec )
468 decoder_sys_t *p_sys = p_dec->p_sys;
471 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
472 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
473 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
476 for( i = 0; i < 4; i++ )
478 uint8_t R = 0, G = 0, B = 0, T = 0;
480 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
481 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
482 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
483 else R = G = B = 0x7F;
485 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
486 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
487 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
488 p_sys->default_clut.c_2b[i].T = T;
491 /* 16 entries CLUT */
492 for( i = 0; i < 16; i++ )
494 uint8_t R = 0, G = 0, B = 0, T = 0;
498 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
504 R = (i & 0x1) ? 0xFF : 0;
505 G = (i & 0x2) ? 0xFF : 0;
506 B = (i & 0x4) ? 0xFF : 0;
511 R = (i & 0x1) ? 0x7F : 0;
512 G = (i & 0x2) ? 0x7F : 0;
513 B = (i & 0x4) ? 0x7F : 0;
516 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
517 p_sys->default_clut.c_4b[i].Cr = RGB_TO_V(R,G,B);
518 p_sys->default_clut.c_4b[i].Cb = RGB_TO_U(R,G,B);
519 p_sys->default_clut.c_4b[i].T = T;
522 /* 256 entries CLUT */
523 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
526 static void decode_segment( decoder_t *p_dec, bs_t *s )
528 decoder_sys_t *p_sys = p_dec->p_sys;
533 /* sync_byte (already checked) */
537 i_type = bs_read( s, 8 );
540 i_page_id = bs_read( s, 16 );
543 i_size = bs_show( s, 16 );
545 if( ( i_page_id != p_sys->i_id ) &&
546 ( i_page_id != p_sys->i_ancillary_id ) )
549 msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
550 i_page_id, p_sys->i_id );
552 bs_skip( s, 8 * ( 2 + i_size ) );
556 if( ( p_sys->i_ancillary_id != p_sys->i_id ) &&
557 ( i_type == DVBSUB_ST_PAGE_COMPOSITION ) &&
558 ( i_page_id == p_sys->i_ancillary_id ) )
561 msg_Dbg( p_dec, "skipped invalid ancillary subtitle packet" );
563 bs_skip( s, 8 * ( 2 + i_size ) );
568 if( i_page_id == p_sys->i_id )
569 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
571 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
576 case DVBSUB_ST_PAGE_COMPOSITION:
578 msg_Dbg( p_dec, "decode_page_composition" );
580 decode_page_composition( p_dec, s );
583 case DVBSUB_ST_REGION_COMPOSITION:
585 msg_Dbg( p_dec, "decode_region_composition" );
587 decode_region_composition( p_dec, s );
590 case DVBSUB_ST_CLUT_DEFINITION:
592 msg_Dbg( p_dec, "decode_clut" );
594 decode_clut( p_dec, s );
597 case DVBSUB_ST_OBJECT_DATA:
599 msg_Dbg( p_dec, "decode_object" );
601 decode_object( p_dec, s );
604 case DVBSUB_ST_DISPLAY_DEFINITION:
606 msg_Dbg( p_dec, "decode_display_definition" );
608 decode_display_definition( p_dec, s );
611 case DVBSUB_ST_ENDOFDISPLAY:
613 msg_Dbg( p_dec, "end of display" );
615 bs_skip( s, 8 * ( 2 + i_size ) );
618 case DVBSUB_ST_STUFFING:
620 msg_Dbg( p_dec, "skip stuffing" );
622 bs_skip( s, 8 * ( 2 + i_size ) );
626 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
627 bs_skip( s, 8 * ( 2 + i_size ) );
632 static void decode_clut( decoder_t *p_dec, bs_t *s )
634 decoder_sys_t *p_sys = p_dec->p_sys;
635 uint16_t i_segment_length;
636 uint16_t i_processed_length;
637 dvbsub_clut_t *p_clut, *p_next;
640 i_segment_length = bs_read( s, 16 );
641 i_id = bs_read( s, 8 );
642 i_version = bs_read( s, 4 );
644 /* Check if we already have this clut */
645 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
647 if( p_clut->i_id == i_id ) break;
650 /* Check version number */
651 if( p_clut && ( p_clut->i_version == i_version ) )
654 bs_skip( s, 8 * i_segment_length - 12 );
661 msg_Dbg( p_dec, "new clut: %i", i_id );
663 p_clut = malloc( sizeof( dvbsub_clut_t ) );
666 p_clut->p_next = p_sys->p_cluts;
667 p_sys->p_cluts = p_clut;
670 /* Initialize to default clut */
671 p_next = p_clut->p_next;
672 *p_clut = p_sys->default_clut;
673 p_clut->p_next = p_next;
675 /* We don't have this version of the CLUT: Parse it */
676 p_clut->i_version = i_version;
678 bs_skip( s, 4 ); /* Reserved bits */
679 i_processed_length = 2;
680 while( i_processed_length < i_segment_length )
682 uint8_t y, cb, cr, t;
686 i_id = bs_read( s, 8 );
687 i_type = bs_read( s, 3 );
691 if( bs_read( s, 1 ) )
694 cr = bs_read( s, 8 );
695 cb = bs_read( s, 8 );
697 i_processed_length += 6;
701 y = bs_read( s, 6 ) << 2;
702 cr = bs_read( s, 4 ) << 4;
703 cb = bs_read( s, 4 ) << 4;
704 t = bs_read( s, 2 ) << 6;
705 i_processed_length += 4;
708 /* We are not entirely compliant here as full transparency is indicated
709 * with a luma value of zero, not a transparency value of 0xff
710 * (full transparency would actually be 0xff + 1). */
717 /* According to EN 300-743 section 7.2.3 note 1, type should
718 * not have more than 1 bit set to one, but some streams don't
719 * respect this note. */
720 if( ( i_type & 0x04 ) && ( i_id < 4 ) )
722 p_clut->c_2b[i_id].Y = y;
723 p_clut->c_2b[i_id].Cr = cr;
724 p_clut->c_2b[i_id].Cb = cb;
725 p_clut->c_2b[i_id].T = t;
727 if( ( i_type & 0x02 ) && ( i_id < 16 ) )
729 p_clut->c_4b[i_id].Y = y;
730 p_clut->c_4b[i_id].Cr = cr;
731 p_clut->c_4b[i_id].Cb = cb;
732 p_clut->c_4b[i_id].T = t;
736 p_clut->c_8b[i_id].Y = y;
737 p_clut->c_8b[i_id].Cr = cr;
738 p_clut->c_8b[i_id].Cb = cb;
739 p_clut->c_8b[i_id].T = t;
744 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
746 decoder_sys_t *p_sys = p_dec->p_sys;
747 int i_version, i_state, i_segment_length, i_timeout, i;
749 /* A page is composed by 0 or more region */
750 i_segment_length = bs_read( s, 16 );
751 i_timeout = bs_read( s, 8 );
752 i_version = bs_read( s, 4 );
753 i_state = bs_read( s, 2 );
754 bs_skip( s, 2 ); /* Reserved */
756 if( i_state == DVBSUB_PCS_STATE_CHANGE )
758 /* End of an epoch, reset decoder buffer */
760 msg_Dbg( p_dec, "page composition mode change" );
764 else if( !p_sys->p_page && ( i_state != DVBSUB_PCS_STATE_ACQUISITION ) &&
765 ( i_state != DVBSUB_PCS_STATE_CHANGE ) )
767 /* Not a full PCS, we need to wait for one */
768 msg_Dbg( p_dec, "didn't receive an acquisition page yet" );
771 /* Try to start decoding even without an acquisition page */
772 bs_skip( s, 8 * (i_segment_length - 2) );
778 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
779 msg_Dbg( p_dec, "acquisition page composition" );
782 /* Check version number */
783 if( p_sys->p_page && ( p_sys->p_page->i_version == i_version ) )
785 bs_skip( s, 8 * (i_segment_length - 2) );
788 else if( p_sys->p_page )
790 if( p_sys->p_page->i_region_defs )
791 free( p_sys->p_page->p_region_defs );
792 p_sys->p_page->p_region_defs = NULL;
793 p_sys->p_page->i_region_defs = 0;
799 msg_Dbg( p_dec, "new page" );
801 /* Allocate a new page */
802 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
807 p_sys->p_page->i_version = i_version;
808 p_sys->p_page->i_timeout = i_timeout;
809 p_sys->b_page = true;
811 /* Number of regions */
812 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
814 if( p_sys->p_page->i_region_defs == 0 ) return;
816 p_sys->p_page->p_region_defs =
817 malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
818 if( p_sys->p_page->p_region_defs )
820 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
822 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
823 bs_skip( s, 8 ); /* Reserved */
824 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
825 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
828 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
829 i, p_sys->p_page->p_region_defs[i].i_x,
830 p_sys->p_page->p_region_defs[i].i_y );
836 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
838 decoder_sys_t *p_sys = p_dec->p_sys;
839 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
840 int i_segment_length, i_processed_length, i_id, i_version;
841 int i_width, i_height, i_level_comp, i_depth, i_clut;
842 int i_8_bg, i_4_bg, i_2_bg;
845 i_segment_length = bs_read( s, 16 );
846 i_id = bs_read( s, 8 );
847 i_version = bs_read( s, 4 );
849 /* Check if we already have this region */
850 for( p_region = p_sys->p_regions; p_region != NULL;
851 p_region = p_region->p_next )
853 pp_region = &p_region->p_next;
854 if( p_region->i_id == i_id ) break;
857 /* Check version number */
858 if( p_region && ( p_region->i_version == i_version ) )
860 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
867 msg_Dbg( p_dec, "new region: %i", i_id );
869 p_region = *pp_region = calloc( 1, sizeof(dvbsub_region_t) );
872 p_region->p_object_defs = NULL;
873 p_region->p_pixbuf = NULL;
874 p_region->p_next = NULL;
877 /* Region attributes */
878 p_region->i_id = i_id;
879 p_region->i_version = i_version;
880 b_fill = bs_read( s, 1 );
881 bs_skip( s, 3 ); /* Reserved */
883 i_width = bs_read( s, 16 );
884 i_height = bs_read( s, 16 );
886 msg_Dbg( p_dec, " width=%d height=%d", i_width, i_height );
888 i_level_comp = bs_read( s, 3 );
889 i_depth = bs_read( s, 3 );
890 bs_skip( s, 2 ); /* Reserved */
891 i_clut = bs_read( s, 8 );
893 i_8_bg = bs_read( s, 8 );
894 i_4_bg = bs_read( s, 4 );
895 i_2_bg = bs_read( s, 2 );
896 bs_skip( s, 2 ); /* Reserved */
898 /* Free old object defs */
899 while( p_region->i_object_defs )
900 free( p_region->p_object_defs[--p_region->i_object_defs].psz_text );
902 free( p_region->p_object_defs );
903 p_region->p_object_defs = NULL;
905 /* Extra sanity checks */
906 if( ( p_region->i_width != i_width ) ||
907 ( p_region->i_height != i_height ) )
909 if( p_region->p_pixbuf )
911 msg_Dbg( p_dec, "region size changed (%dx%d->%dx%d)",
912 p_region->i_width, p_region->i_height, i_width, i_height );
913 free( p_region->p_pixbuf );
916 p_region->p_pixbuf = malloc( i_height * i_width );
917 p_region->i_depth = 0;
920 if( p_region->i_depth &&
921 ( ( p_region->i_depth != i_depth ) ||
922 ( p_region->i_level_comp != i_level_comp ) ||
923 ( p_region->i_clut != i_clut) ) )
925 msg_Dbg( p_dec, "region parameters changed (not allowed)" );
928 /* Erase background of region */
931 int i_background = ( p_region->i_depth == 1 ) ? i_2_bg :
932 ( ( p_region->i_depth == 2 ) ? i_4_bg : i_8_bg );
933 memset( p_region->p_pixbuf, i_background, i_width * i_height );
936 p_region->i_width = i_width;
937 p_region->i_height = i_height;
938 p_region->i_level_comp = i_level_comp;
939 p_region->i_depth = i_depth;
940 p_region->i_clut = i_clut;
942 /* List of objects in the region */
943 i_processed_length = 10;
944 while( i_processed_length < i_segment_length )
946 dvbsub_objectdef_t *p_obj;
948 /* We create a new object */
949 p_region->i_object_defs++;
950 p_region->p_object_defs =
951 realloc( p_region->p_object_defs,
952 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
954 /* We parse object properties */
955 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
956 p_obj->i_id = bs_read( s, 16 );
957 p_obj->i_type = bs_read( s, 2 );
958 bs_skip( s, 2 ); /* Provider */
959 p_obj->i_x = bs_read( s, 12 );
960 bs_skip( s, 4 ); /* Reserved */
961 p_obj->i_y = bs_read( s, 12 );
962 p_obj->psz_text = NULL;
964 i_processed_length += 6;
966 if( ( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ) ||
967 ( p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING ) )
969 p_obj->i_fg_pc = bs_read( s, 8 );
970 p_obj->i_bg_pc = bs_read( s, 8 );
971 i_processed_length += 2;
976 /* ETSI 300 743 [7.2.1] */
977 static void decode_display_definition( decoder_t *p_dec, bs_t *s )
979 decoder_sys_t *p_sys = p_dec->p_sys;
980 uint16_t i_segment_length;
981 uint16_t i_processed_length = 40;
984 i_segment_length = bs_read( s, 16 );
985 i_version = bs_read( s, 4 );
987 /* Check version number */
988 if( p_sys->display.i_version == i_version )
990 /* The definition did not change */
991 bs_skip( s, 8*i_segment_length - 4 );
996 msg_Dbg( p_dec, "new display definition: %i", i_version );
999 /* We don't have this version of the display definition: Parse it */
1000 p_sys->display.i_version = i_version;
1001 p_sys->display.b_windowed = bs_read( s, 1 );
1002 bs_skip( s, 3 ); /* Reserved bits */
1003 p_sys->display.i_width = bs_read( s, 16 )+1;
1004 p_sys->display.i_height = bs_read( s, 16 )+1;
1006 if( p_sys->display.b_windowed )
1009 msg_Dbg( p_dec, "display definition with offsets (windowed)" );
1011 /* Coordinates are measured from the top left corner */
1012 p_sys->display.i_x = bs_read( s, 16 );
1013 p_sys->display.i_max_x = bs_read( s, 16 );
1014 p_sys->display.i_y = bs_read( s, 16 );
1015 p_sys->display.i_max_y = bs_read( s, 16 );
1016 i_processed_length += 64;
1020 /* if not windowed, setup the window variables to good defaults */
1021 /* not necessary, but to avoid future confusion.. */
1022 p_sys->display.i_x = 0;
1023 p_sys->display.i_max_x = p_sys->display.i_width-1;
1024 p_sys->display.i_y = 0;
1025 p_sys->display.i_max_y = p_sys->display.i_height-1;
1028 if( i_processed_length != i_segment_length*8 )
1030 msg_Err( p_dec, "processed length %d bytes != segment length %d bytes",
1031 i_processed_length / 8 , i_segment_length );
1035 msg_Dbg( p_dec, "version: %d, width: %d, height: %d",
1036 p_sys->display.i_version, p_sys->display.i_width, p_sys->display.i_height );
1037 if( p_sys->display.b_windowed )
1038 msg_Dbg( p_dec, "xmin: %d, xmax: %d, ymin: %d, ymax: %d",
1039 p_sys->display.i_x, p_sys->display.i_max_x, p_sys->display.i_y, p_sys->display.i_max_y );
1043 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
1045 static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
1046 static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
1047 static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
1049 static void decode_object( decoder_t *p_dec, bs_t *s )
1051 decoder_sys_t *p_sys = p_dec->p_sys;
1052 dvbsub_region_t *p_region;
1053 int i_segment_length, i_coding_method, i_version, i_id, i;
1054 bool b_non_modify_color;
1056 /* ETSI 300-743 paragraph 7.2.4
1057 * sync_byte, segment_type and page_id have already been processed.
1059 i_segment_length = bs_read( s, 16 );
1060 i_id = bs_read( s, 16 );
1061 i_version = bs_read( s, 4 );
1062 i_coding_method = bs_read( s, 2 );
1064 if( i_coding_method > 1 )
1066 msg_Dbg( p_dec, "unknown DVB subtitling coding %d is not handled!", i_coding_method );
1067 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1071 /* Check if the object needs to be rendered in at least one
1073 for( p_region = p_sys->p_regions; p_region != NULL;
1074 p_region = p_region->p_next )
1076 for( i = 0; i < p_region->i_object_defs; i++ )
1077 if( p_region->p_object_defs[i].i_id == i_id ) break;
1079 if( i != p_region->i_object_defs ) break;
1083 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1088 msg_Dbg( p_dec, "new object: %i", i_id );
1091 b_non_modify_color = bs_read( s, 1 );
1092 bs_skip( s, 1 ); /* Reserved */
1094 if( i_coding_method == 0x00 )
1096 int i_topfield, i_bottomfield;
1097 uint8_t *p_topfield, *p_bottomfield;
1099 i_topfield = bs_read( s, 16 );
1100 i_bottomfield = bs_read( s, 16 );
1101 p_topfield = s->p_start + bs_pos( s ) / 8;
1102 p_bottomfield = p_topfield + i_topfield;
1104 bs_skip( s, 8 * (i_segment_length - 7) );
1107 if( ( i_segment_length < ( i_topfield + i_bottomfield + 7 ) ) ||
1108 ( ( p_topfield + i_topfield + i_bottomfield ) > s->p_end ) )
1110 msg_Dbg( p_dec, "corrupted object data" );
1114 for( p_region = p_sys->p_regions; p_region != NULL;
1115 p_region = p_region->p_next )
1117 for( i = 0; i < p_region->i_object_defs; i++ )
1119 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1121 dvbsub_render_pdata( p_dec, p_region,
1122 p_region->p_object_defs[i].i_x,
1123 p_region->p_object_defs[i].i_y,
1124 p_topfield, i_topfield );
1128 dvbsub_render_pdata( p_dec, p_region,
1129 p_region->p_object_defs[i].i_x,
1130 p_region->p_object_defs[i].i_y + 1,
1131 p_bottomfield, i_bottomfield );
1135 /* Duplicate the top field */
1136 dvbsub_render_pdata( p_dec, p_region,
1137 p_region->p_object_defs[i].i_x,
1138 p_region->p_object_defs[i].i_y + 1,
1139 p_topfield, i_topfield );
1146 /* DVB subtitling as characters */
1147 int i_number_of_codes = bs_read( s, 8 );
1148 uint8_t* p_start = s->p_start + bs_pos( s ) / 8;
1151 if( ( i_segment_length < ( i_number_of_codes*2 + 4 ) ) ||
1152 ( ( p_start + i_number_of_codes*2 ) > s->p_end ) )
1154 msg_Dbg( p_dec, "corrupted object data" );
1158 for( p_region = p_sys->p_regions; p_region != NULL;
1159 p_region = p_region->p_next )
1161 for( i = 0; i < p_region->i_object_defs; i++ )
1165 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1167 p_region->p_object_defs[i].psz_text =
1168 realloc( p_region->p_object_defs[i].psz_text,
1169 i_number_of_codes + 1 );
1171 /* FIXME 16bits -> char ??? See Preamble */
1172 for( j = 0; j < i_number_of_codes; j++ )
1174 p_region->p_object_defs[i].psz_text[j] = (char)(bs_read( s, 16 ) & 0xFF);
1176 /* Null terminate the string */
1177 p_region->p_object_defs[i].psz_text[j] = 0;
1183 msg_Dbg( p_dec, "end object: %i", i_id );
1187 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
1189 uint8_t *p_field, int i_field )
1196 if( !p_region->p_pixbuf )
1198 msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
1201 if( i_y < 0 || i_x < 0 || i_y >= p_region->i_height ||
1202 i_x >= p_region->i_width )
1204 msg_Dbg( p_dec, "invalid offset (%i,%i)", i_x, i_y );
1208 p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
1209 bs_init( &bs, p_field, i_field );
1211 while( !bs_eof( &bs ) )
1214 if( i_y >= p_region->i_height ) return;
1216 switch( bs_read( &bs, 8 ) )
1219 dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1224 dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1229 dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1236 /* We don't use map tables */
1239 case 0xf0: /* End of line code */
1240 p_pixbuf += 2*p_region->i_width;
1241 i_offset = 0; i_y += 2;
1247 static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1249 bool b_stop = false;
1251 while( !b_stop && !bs_eof( s ) )
1253 int i_count = 0, i_color = 0;
1255 i_color = bs_read( s, 2 );
1256 if( i_color != 0x00 )
1262 if( bs_read( s, 1 ) == 0x01 ) // Switch1
1264 i_count = 3 + bs_read( s, 3 );
1265 i_color = bs_read( s, 2 );
1269 if( bs_read( s, 1 ) == 0x00 ) //Switch2
1271 switch( bs_read( s, 2 ) ) //Switch3
1280 i_count = 12 + bs_read( s, 4 );
1281 i_color = bs_read( s, 2 );
1284 i_count = 29 + bs_read( s, 8 );
1285 i_color = bs_read( s, 2 );
1293 /* 1 pixel color 0 */
1299 if( !i_count ) continue;
1302 if( ( i_count + *pi_off ) > i_width ) break;
1304 if( i_count == 1 ) p[*pi_off] = i_color;
1305 else memset( ( p + *pi_off ), i_color, i_count );
1307 (*pi_off) += i_count;
1313 static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1315 bool b_stop = false;
1317 while( !b_stop && !bs_eof( s ) )
1319 int i_count = 0, i_color = 0;
1321 i_color = bs_read( s, 4 );
1322 if( i_color != 0x00 )
1329 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1331 if( bs_show( s, 3 ) != 0x00 )
1333 i_count = 2 + bs_read( s, 3 );
1343 if( bs_read( s, 1 ) == 0x00) //Switch2
1345 i_count = 4 + bs_read( s, 2 );
1346 i_color = bs_read( s, 4 );
1350 switch ( bs_read( s, 2 ) ) //Switch3
1359 i_count = 9 + bs_read( s, 4 );
1360 i_color = bs_read( s, 4 );
1363 i_count= 25 + bs_read( s, 8 );
1364 i_color = bs_read( s, 4 );
1371 if( !i_count ) continue;
1374 if( ( i_count + *pi_off ) > i_width ) break;
1376 if( i_count == 1 ) p[*pi_off] = i_color;
1377 else memset( ( p + *pi_off ), i_color, i_count );
1379 (*pi_off) += i_count;
1385 static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1387 bool b_stop = false;
1389 while( !b_stop && !bs_eof( s ) )
1391 int i_count = 0, i_color = 0;
1393 i_color = bs_read( s, 8 );
1394 if( i_color != 0x00 )
1401 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1403 if( bs_show( s, 7 ) != 0x00 )
1405 i_count = bs_read( s, 7 );
1415 i_count = bs_read( s, 7 );
1416 i_color = bs_read( s, 8 );
1420 if( !i_count ) continue;
1423 if( ( i_count + *pi_off ) > i_width ) break;
1425 if( i_count == 1 ) p[*pi_off] = i_color;
1426 else memset( ( p + *pi_off ), i_color, i_count );
1428 (*pi_off) += i_count;
1434 static void free_all( decoder_t *p_dec )
1436 decoder_sys_t *p_sys = p_dec->p_sys;
1437 dvbsub_region_t *p_reg, *p_reg_next;
1438 dvbsub_clut_t *p_clut, *p_clut_next;
1440 /*free( p_sys->p_display ); No longer malloced */
1442 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
1444 p_clut_next = p_clut->p_next;
1447 p_sys->p_cluts = NULL;
1449 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1453 p_reg_next = p_reg->p_next;
1454 for( i = 0; i < p_reg->i_object_defs; i++ )
1455 free( p_reg->p_object_defs[i].psz_text );
1456 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1457 free( p_reg->p_pixbuf );
1460 p_sys->p_regions = NULL;
1464 if( p_sys->p_page->i_region_defs )
1465 free( p_sys->p_page->p_region_defs );
1466 free( p_sys->p_page );
1468 p_sys->p_page = NULL;
1471 static subpicture_t *render( decoder_t *p_dec )
1473 decoder_sys_t *p_sys = p_dec->p_sys;
1474 subpicture_t *p_spu;
1475 subpicture_region_t **pp_spu_region;
1476 int i, j, i_timeout = 0;
1480 /* Allocate the subpicture internal data. */
1481 p_spu = decoder_NewSubpicture( p_dec );
1485 p_spu->b_absolute = p_sys->b_absolute;
1486 /* Set the pf_render callback */
1487 p_spu->i_start = (mtime_t) p_sys->i_pts;
1488 //p_spu->i_stop = (mtime_t) 0;
1489 p_spu->b_ephemer = true;
1490 //p_spu->b_fade = true;
1491 //p_spu->i_stop = p_spu->i_start + (mtime_t) (i_timeout * 1000000);
1493 /* Correct positioning of SPU */
1494 i_base_x = p_sys->i_spu_x;
1495 i_base_y = p_sys->i_spu_y;
1496 p_spu->i_original_picture_width = 720;
1497 p_spu->i_original_picture_height = 576;
1499 p_spu->i_original_picture_width = p_sys->display.i_width;
1500 p_spu->i_original_picture_height = p_sys->display.i_height;
1502 if( p_sys->display.b_windowed )
1504 /* From en_300743v01 - */
1505 /* the DDS is there to indicate intended size/position of text */
1506 /* the intended video area is ->i_width/height */
1507 /* the window is within this... SO... we should leave i_original_picture_width etc. as is */
1508 /* and ONLY change i_base_x. effectively i_max_x/y are only there to limit memory requirements*/
1509 /* we COULD use the upper limits to limit rendering to within these? */
1511 /* see notes on DDS at the top of the file */
1512 i_base_x += p_sys->display.i_x;
1513 i_base_y += p_sys->display.i_y;
1516 pp_spu_region = &p_spu->p_region;
1518 /* Loop on region definitions */
1521 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1524 for( i = 0; p_sys->p_page && ( i < p_sys->p_page->i_region_defs ); i++ )
1526 dvbsub_region_t *p_region;
1527 dvbsub_regiondef_t *p_regiondef;
1528 dvbsub_clut_t *p_clut;
1529 dvbsub_color_t *p_color;
1530 subpicture_region_t *p_spu_region;
1531 uint8_t *p_src, *p_dst;
1533 video_palette_t palette;
1536 i_timeout = p_sys->p_page->i_timeout;
1538 p_regiondef = &p_sys->p_page->p_region_defs[i];
1540 /* Find associated region */
1541 for( p_region = p_sys->p_regions; p_region != NULL;
1542 p_region = p_region->p_next )
1544 if( p_regiondef->i_id == p_region->i_id ) break;
1548 /* if a region exists, then print it's size */
1551 msg_Dbg( p_dec, "rendering region %i (%i,%i) to (%i,%i)", i,
1552 p_regiondef->i_x, p_regiondef->i_y,
1553 p_regiondef->i_x + p_region->i_width,
1554 p_regiondef->i_y + p_region->i_height );
1558 msg_Dbg( p_dec, "rendering region %i (%i,%i) (no region matched to render)", i,
1559 p_regiondef->i_x, p_regiondef->i_y );
1565 msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
1569 /* Find associated CLUT */
1570 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
1572 if( p_region->i_clut == p_clut->i_id ) break;
1576 msg_Dbg( p_dec, "clut %i not found", p_region->i_clut );
1580 /* FIXME: don't create a subpicture region with VLC CODEC YUVP
1581 * when it actually is a TEXT region */
1583 /* Create new SPU region */
1584 memset( &fmt, 0, sizeof(video_format_t) );
1585 fmt.i_chroma = VLC_CODEC_YUVP;
1586 fmt.i_aspect = 0; /* 0 means use aspect ratio of background video */
1587 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1588 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1589 fmt.i_x_offset = fmt.i_y_offset = 0;
1590 fmt.p_palette = &palette;
1591 fmt.p_palette->i_entries = ( p_region->i_depth == 1 ) ? 4 :
1592 ( ( p_region->i_depth == 2 ) ? 16 : 256 );
1593 p_color = ( p_region->i_depth == 1 ) ? p_clut->c_2b :
1594 ( ( p_region->i_depth == 2 ) ? p_clut->c_4b : p_clut->c_8b );
1595 for( j = 0; j < fmt.p_palette->i_entries; j++ )
1597 fmt.p_palette->palette[j][0] = p_color[j].Y;
1598 fmt.p_palette->palette[j][1] = p_color[j].Cb; /* U == Cb */
1599 fmt.p_palette->palette[j][2] = p_color[j].Cr; /* V == Cr */
1600 fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1603 p_spu_region = subpicture_region_New( &fmt );
1606 msg_Err( p_dec, "cannot allocate SPU region" );
1609 p_spu_region->i_x = i_base_x + p_regiondef->i_x;
1610 p_spu_region->i_y = i_base_y + p_regiondef->i_y;
1611 p_spu_region->i_align = p_sys->i_spu_position;
1612 *pp_spu_region = p_spu_region;
1613 pp_spu_region = &p_spu_region->p_next;
1615 p_src = p_region->p_pixbuf;
1616 p_dst = p_spu_region->p_picture->Y_PIXELS;
1617 i_pitch = p_spu_region->p_picture->Y_PITCH;
1619 /* Copy pixel buffer */
1620 for( j = 0; j < p_region->i_height; j++ )
1622 memcpy( p_dst, p_src, p_region->i_width );
1623 p_src += p_region->i_width;
1627 /* Check subtitles encoded as strings of characters
1628 * (since there are not rendered in the pixbuffer) */
1629 for( j = 0; j < p_region->i_object_defs; j++ )
1631 dvbsub_objectdef_t *p_object_def = &p_region->p_object_defs[j];
1633 if( ( p_object_def->i_type != 1 ) || !p_object_def->psz_text )
1636 /* Create new SPU region */
1637 memset( &fmt, 0, sizeof(video_format_t) );
1638 fmt.i_chroma = VLC_CODEC_TEXT;
1639 fmt.i_aspect = VOUT_ASPECT_FACTOR;
1640 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1641 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1642 fmt.i_x_offset = fmt.i_y_offset = 0;
1643 p_spu_region = subpicture_region_New( &fmt );
1646 msg_Err( p_dec, "cannot allocate SPU region" );
1650 p_spu_region->psz_text = strdup( p_object_def->psz_text );
1651 p_spu_region->i_x = i_base_x + p_regiondef->i_x + p_object_def->i_x;
1652 p_spu_region->i_y = i_base_y + p_regiondef->i_y + p_object_def->i_y;
1653 p_spu_region->i_align = p_sys->i_spu_position;
1654 *pp_spu_region = p_spu_region;
1655 pp_spu_region = &p_spu_region->p_next;
1662 /*****************************************************************************
1663 * encoder_sys_t : encoder descriptor
1664 *****************************************************************************/
1665 typedef struct encoder_region_t
1672 struct encoder_sys_t
1674 unsigned int i_page_ver;
1675 unsigned int i_region_ver;
1676 unsigned int i_clut_ver;
1679 encoder_region_t *p_regions;
1683 /* subpicture positioning */
1688 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1689 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1690 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1691 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1693 /*****************************************************************************
1694 * OpenEncoder: probe the encoder and return score
1695 *****************************************************************************/
1696 static int OpenEncoder( vlc_object_t *p_this )
1698 encoder_t *p_enc = (encoder_t *)p_this;
1699 encoder_sys_t *p_sys;
1701 if( ( p_enc->fmt_out.i_codec != VLC_CODEC_DVBS ) &&
1704 return VLC_EGENERIC;
1707 /* Allocate the memory needed to store the decoder's structure */
1708 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1710 p_enc->p_sys = p_sys;
1712 p_enc->pf_encode_sub = Encode;
1713 p_enc->fmt_out.i_codec = VLC_CODEC_DVBS;
1714 p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1;
1716 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1718 p_sys->i_page_ver = 0;
1719 p_sys->i_region_ver = 0;
1720 p_sys->i_clut_ver = 0;
1721 p_sys->i_regions = 0;
1722 p_sys->p_regions = 0;
1724 p_sys->i_offset_x = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "x" );
1725 p_sys->i_offset_y = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "y" );
1730 /* FIXME: this routine is a hack to convert VLC_CODEC_YUVA
1731 * into VLC_CODEC_YUVP
1733 static subpicture_t *YuvaYuvp( subpicture_t *p_subpic )
1735 subpicture_region_t *p_region = NULL;
1737 if( !p_subpic ) return NULL;
1739 for( p_region = p_subpic->p_region; p_region; p_region = p_region->p_next )
1741 video_format_t *p_fmt = &p_region->fmt;
1742 int i = 0, j = 0, n = 0, p = 0;
1743 int i_max_entries = 256;
1745 #ifdef RANDOM_DITHERING
1746 int i_seed = 0xdeadbeef; /* random seed */
1750 int i_pixels = p_region->p_picture->p[0].i_visible_lines
1751 * p_region->p_picture->p[0].i_pitch;
1752 int i_iterator = p_region->p_picture->p[0].i_visible_lines * 3 / 4
1753 * p_region->p_picture->p[0].i_pitch
1754 + p_region->p_picture->p[0].i_pitch * 1 / 3;
1755 int i_tolerance = 0;
1757 #ifdef DEBUG_DVBSUB1
1758 /* p_enc not valid here */
1759 msg_Dbg( p_enc, "YuvaYuvp: i_pixels=%d, i_iterator=%d", i_pixels, i_iterator );
1761 p_fmt->i_chroma = VLC_CODEC_YUVP;
1762 p_fmt->p_palette = (video_palette_t *) malloc( sizeof( video_palette_t ) );
1763 if( !p_fmt->p_palette ) break;
1764 p_fmt->p_palette->i_entries = 0;
1766 /* Find best iterator using Euclide’s algorithm */
1767 for( ; i_iterator > 1 ; i_iterator-- )
1786 /* Count colors, build best palette */
1787 for( i_tolerance = 0; i_tolerance < 128; i_tolerance++ )
1789 bool b_success = true;
1790 p_fmt->p_palette->i_entries = 0;
1792 for( i = 0; i < i_pixels ; )
1795 y = p_region->p_picture->p[0].p_pixels[i];
1796 u = p_region->p_picture->p[1].p_pixels[i];
1797 v = p_region->p_picture->p[2].p_pixels[i];
1798 a = p_region->p_picture->p[3].p_pixels[i];
1799 for( j = 0; j < p_fmt->p_palette->i_entries; j++ )
1801 if( abs((int)p_fmt->p_palette->palette[j][0] - (int)y) <= i_tolerance &&
1802 abs((int)p_fmt->p_palette->palette[j][1] - (int)u) <= i_tolerance &&
1803 abs((int)p_fmt->p_palette->palette[j][2] - (int)v) <= i_tolerance &&
1804 abs((int)p_fmt->p_palette->palette[j][3] - (int)a) <= i_tolerance / 2 )
1809 if( j == p_fmt->p_palette->i_entries )
1811 p_fmt->p_palette->palette[j][0] = y;
1812 p_fmt->p_palette->palette[j][1] = u;
1813 p_fmt->p_palette->palette[j][2] = v;
1814 p_fmt->p_palette->palette[j][3] = a;
1815 p_fmt->p_palette->i_entries++;
1817 if( p_fmt->p_palette->i_entries >= i_max_entries )
1835 #ifdef DEBUG_DVBSUB1
1836 /* p_enc not valid here */
1837 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1840 #ifndef RANDOM_DITHERING
1841 pi_delta = malloc( ( p_region->p_picture->p[0].i_pitch + 1 )
1842 * sizeof(int) * 4 );
1843 for( i = 0; i < (p_region->p_picture->p[0].i_pitch + 1) * 4 ; i++ )
1849 /* Fill image with our new colours */
1850 for( p = 0; p < p_region->p_picture->p[0].i_visible_lines ; p++ )
1852 int i_ydelta = 0, i_udelta = 0, i_vdelta = 0, i_adelta = 0;
1854 for( n = 0; n < p_region->p_picture->p[0].i_pitch ; n++ )
1856 int i_offset = p * p_region->p_picture->p[0].i_pitch + n;
1858 int i_mindist, i_best;
1860 y = (int)p_region->p_picture->p[0].p_pixels[i_offset];
1861 u = (int)p_region->p_picture->p[1].p_pixels[i_offset];
1862 v = (int)p_region->p_picture->p[2].p_pixels[i_offset];
1863 a = (int)p_region->p_picture->p[3].p_pixels[i_offset];
1865 /* Add dithering compensation */
1866 #ifdef RANDOM_DITHERING
1867 y += ((i_seed & 0xff) - 0x80) * i_tolerance / 0x80;
1868 u += (((i_seed >> 8) & 0xff) - 0x80) * i_tolerance / 0x80;
1869 v += (((i_seed >> 16) & 0xff) - 0x80) * i_tolerance / 0x80;
1870 a += (((i_seed >> 24) & 0xff) - 0x80) * i_tolerance / 0x80;
1872 y += i_ydelta + pi_delta[ n * 4 ];
1873 u += i_udelta + pi_delta[ n * 4 + 1 ];
1874 v += i_vdelta + pi_delta[ n * 4 + 2 ];
1875 a += i_adelta + pi_delta[ n * 4 + 3 ];
1878 /* Find best colour in palette */
1879 for( i_mindist = 99999999, i_best = 0, j = 0; j < p_fmt->p_palette->i_entries; j++ )
1883 i_dist += abs((int)p_fmt->p_palette->palette[j][0] - y);
1884 i_dist += abs((int)p_fmt->p_palette->palette[j][1] - u);
1885 i_dist += abs((int)p_fmt->p_palette->palette[j][2] - v);
1886 i_dist += 2 * abs((int)p_fmt->p_palette->palette[j][3] - a);
1888 if( i_dist < i_mindist )
1895 /* Set pixel to best color */
1896 p_region->p_picture->p[0].p_pixels[i_offset] = i_best;
1898 /* Update dithering state */
1899 #ifdef RANDOM_DITHERING
1900 i_seed = (i_seed * 0x1283837) ^ 0x789479 ^ (i_seed >> 13);
1902 i_ydelta = y - (int)p_fmt->p_palette->palette[i_best][0];
1903 i_udelta = u - (int)p_fmt->p_palette->palette[i_best][1];
1904 i_vdelta = v - (int)p_fmt->p_palette->palette[i_best][2];
1905 i_adelta = a - (int)p_fmt->p_palette->palette[i_best][3];
1906 pi_delta[ n * 4 ] = i_ydelta * 3 / 8;
1907 pi_delta[ n * 4 + 1 ] = i_udelta * 3 / 8;
1908 pi_delta[ n * 4 + 2 ] = i_vdelta * 3 / 8;
1909 pi_delta[ n * 4 + 3 ] = i_adelta * 3 / 8;
1910 i_ydelta = i_ydelta * 5 / 8;
1911 i_udelta = i_udelta * 5 / 8;
1912 i_vdelta = i_vdelta * 5 / 8;
1913 i_adelta = i_adelta * 5 / 8;
1917 #ifndef RANDOM_DITHERING
1922 for( i = p_fmt->p_palette->i_entries; i < i_max_entries; i++ )
1924 p_fmt->p_palette->palette[i][0] = 0;
1925 p_fmt->p_palette->palette[i][1] = 0;
1926 p_fmt->p_palette->palette[i][2] = 0;
1927 p_fmt->p_palette->palette[i][3] = 0;
1929 p_fmt->p_palette->i_entries = i_max_entries;
1930 #ifdef DEBUG_DVBSUB1
1931 /* p_enc not valid here */
1932 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1938 /****************************************************************************
1939 * Encode: the whole thing
1940 ****************************************************************************/
1941 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1943 subpicture_t *p_temp = NULL;
1944 subpicture_region_t *p_region = NULL;
1945 bs_t bits, *s = &bits;
1948 if( !p_subpic || !p_subpic->p_region ) return NULL;
1950 /* FIXME: this is a hack to convert VLC_CODEC_YUVA into
1953 p_region = p_subpic->p_region;
1954 if( p_region->fmt.i_chroma == VLC_CODEC_YUVA )
1956 p_temp = YuvaYuvp( p_subpic );
1959 msg_Err( p_enc, "no picture in subpicture" );
1962 p_region = p_subpic->p_region;
1966 if( !p_region ) return NULL;
1968 if( ( p_region->fmt.i_chroma != VLC_CODEC_TEXT ) &&
1969 ( p_region->fmt.i_chroma != VLC_CODEC_YUVP ) )
1972 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
1973 vlc_fourcc_to_char( p_region->fmt.i_chroma, &psz_fourcc );
1974 msg_Err( p_enc, "chroma %4s not supported", psz_fourcc );
1978 if( p_region->fmt.p_palette )
1980 switch( p_region->fmt.p_palette->i_entries )
1988 msg_Err( p_enc, "subpicture palette (%d) not handled",
1989 p_region->fmt.p_palette->i_entries );
1996 msg_Dbg( p_enc, "encoding subpicture" );
1998 p_block = block_New( p_enc, 64000 );
1999 bs_init( s, p_block->p_buffer, p_block->i_buffer );
2001 bs_write( s, 8, 0x20 ); /* Data identifier */
2002 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
2004 encode_page_composition( p_enc, s, p_subpic );
2005 encode_region_composition( p_enc, s, p_subpic );
2006 encode_clut( p_enc, s, p_subpic );
2007 encode_object( p_enc, s, p_subpic );
2009 /* End of display */
2010 bs_write( s, 8, 0x0f ); /* Sync byte */
2011 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
2012 bs_write( s, 16, 1 ); /* Page id */
2013 bs_write( s, 16, 0 ); /* Segment length */
2015 bs_write( s, 8, 0xff );/* End marker */
2016 p_block->i_buffer = bs_pos( s ) / 8;
2017 p_block->i_pts = p_block->i_dts = p_subpic->i_start;
2018 if( !p_subpic->b_ephemer && ( p_subpic->i_stop > p_subpic->i_start ) )
2020 block_t *p_block_stop;
2022 p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
2024 /* Send another (empty) subtitle to signal the end of display */
2025 p_block_stop = block_New( p_enc, 64000 );
2026 bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
2027 bs_write( s, 8, 0x20 ); /* Data identifier */
2028 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
2029 encode_page_composition( p_enc, s, 0 );
2030 bs_write( s, 8, 0x0f ); /* Sync byte */
2031 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
2032 bs_write( s, 16, 1 ); /* Page id */
2033 bs_write( s, 16, 0 ); /* Segment length */
2034 bs_write( s, 8, 0xff );/* End marker */
2035 p_block_stop->i_buffer = bs_pos( s ) / 8;
2036 p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
2037 block_ChainAppend( &p_block, p_block_stop );
2038 p_block_stop->i_length = 100000; /* p_subpic->i_stop - p_subpic->i_start; */
2041 msg_Dbg( p_enc, "subpicture encoded properly" );
2046 /*****************************************************************************
2047 * CloseEncoder: encoder destruction
2048 *****************************************************************************/
2049 static void CloseEncoder( vlc_object_t *p_this )
2051 encoder_t *p_enc = (encoder_t *)p_this;
2052 encoder_sys_t *p_sys = p_enc->p_sys;
2054 var_Destroy( p_this , ENC_CFG_PREFIX "x" );
2055 var_Destroy( p_this , ENC_CFG_PREFIX "y" );
2056 var_Destroy( p_this , ENC_CFG_PREFIX "timeout" );
2058 if( p_sys->i_regions ) free( p_sys->p_regions );
2062 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
2063 subpicture_t *p_subpic )
2065 encoder_sys_t *p_sys = p_enc->p_sys;
2066 subpicture_region_t *p_region;
2067 bool b_mode_change = false;
2068 int i_regions, i_timeout;
2070 bs_write( s, 8, 0x0f ); /* Sync byte */
2071 bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
2072 bs_write( s, 16, 1 ); /* Page id */
2074 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2075 p_region; p_region = p_region->p_next, i_regions++ )
2077 if( i_regions >= p_sys->i_regions )
2079 encoder_region_t region;
2080 region.i_width = region.i_height = 0;
2082 realloc( p_sys->p_regions, sizeof(encoder_region_t) *
2083 (p_sys->i_regions + 1) );
2084 p_sys->p_regions[p_sys->i_regions++] = region;
2087 if( ( p_sys->p_regions[i_regions].i_width <
2088 (int)p_region->fmt.i_visible_width ) ||
2089 ( p_sys->p_regions[i_regions].i_width >
2090 (int)p_region->fmt.i_visible_width ) )
2092 b_mode_change = true;
2093 msg_Dbg( p_enc, "region %i width change: %i -> %i",
2094 i_regions, p_sys->p_regions[i_regions].i_width,
2095 p_region->fmt.i_visible_width );
2096 p_sys->p_regions[i_regions].i_width =
2097 p_region->fmt.i_visible_width;
2099 if( p_sys->p_regions[i_regions].i_height <
2100 (int)p_region->fmt.i_visible_height )
2102 b_mode_change = true;
2103 msg_Dbg( p_enc, "region %i height change: %i -> %i",
2104 i_regions, p_sys->p_regions[i_regions].i_height,
2105 p_region->fmt.i_visible_height );
2106 p_sys->p_regions[i_regions].i_height =
2107 p_region->fmt.i_visible_height;
2111 bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
2114 if( p_subpic && !p_subpic->b_ephemer &&
2115 ( p_subpic->i_stop > p_subpic->i_start ) )
2117 i_timeout = (p_subpic->i_stop - p_subpic->i_start) / 1000000;
2120 bs_write( s, 8, i_timeout ); /* Timeout */
2121 bs_write( s, 4, p_sys->i_page_ver++ );
2122 bs_write( s, 2, b_mode_change ?
2123 DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
2124 bs_write( s, 2, 0 ); /* Reserved */
2126 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2127 p_region; p_region = p_region->p_next, i_regions++ )
2129 bs_write( s, 8, i_regions );
2130 bs_write( s, 8, 0 ); /* Reserved */
2131 if( (p_sys->i_offset_x > 0) && (p_sys->i_offset_y > 0) )
2133 bs_write( s, 16, p_sys->i_offset_x ); /* override x position */
2134 bs_write( s, 16, p_sys->i_offset_y ); /* override y position */
2138 bs_write( s, 16, p_region->i_x );
2139 bs_write( s, 16, p_region->i_y );
2144 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2146 encoder_sys_t *p_sys = p_enc->p_sys;
2147 subpicture_region_t *p_region = p_subpic->p_region;
2148 video_palette_t *p_pal, pal;
2152 if( !p_region ) return;
2154 if( p_region->fmt.i_chroma == VLC_CODEC_YUVP )
2156 p_pal = p_region->fmt.p_palette;
2161 for( i = 0; i < 4; i++ )
2163 pal.palette[i][0] = 0;
2164 pal.palette[i][1] = 0;
2165 pal.palette[i][2] = 0;
2166 pal.palette[i][3] = 0;
2171 bs_write( s, 8, 0x0f ); /* Sync byte */
2172 bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
2173 bs_write( s, 16, 1 ); /* Page id */
2175 bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
2176 bs_write( s, 8, 1 ); /* Clut id */
2177 bs_write( s, 4, p_sys->i_clut_ver++ );
2178 bs_write( s, 4, 0 ); /* Reserved */
2180 for( i = 0; i < p_pal->i_entries; i++ )
2182 bs_write( s, 8, i ); /* Clut entry id */
2183 bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
2184 bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
2185 bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
2186 bs_write( s, 4, 0 ); /* Reserved */
2187 bs_write( s, 1, 1 ); /* Full range flag */
2188 bs_write( s, 8, p_pal->palette[i][3] ? /* Y value */
2189 (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
2190 bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
2191 bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
2192 bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
2196 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
2197 subpicture_t *p_subpic )
2199 encoder_sys_t *p_sys = p_enc->p_sys;
2200 subpicture_region_t *p_region;
2203 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2204 p_region = p_region->p_next, i_region++ )
2206 int i_entries = 4, i_depth = 0x1, i_bg = 0;
2208 ( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
2212 video_palette_t *p_pal = p_region->fmt.p_palette;
2216 msg_Err( p_enc, "subpicture has no palette - ignoring it" );
2220 i_entries = p_pal->i_entries;
2221 i_depth = i_entries == 4 ? 0x1 : i_entries == 16 ? 0x2 : 0x3;
2223 for( i_bg = 0; i_bg < p_pal->i_entries; i_bg++ )
2225 if( !p_pal->palette[i_bg][3] ) break;
2229 bs_write( s, 8, 0x0f ); /* Sync byte */
2230 bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
2231 bs_write( s, 16, 1 ); /* Page id */
2233 bs_write( s, 16, 10 + 6 + (b_text ? 2 : 0) ); /* Segment length */
2234 bs_write( s, 8, i_region );
2235 bs_write( s, 4, p_sys->i_region_ver++ );
2237 /* Region attributes */
2238 bs_write( s, 1, i_bg < i_entries ); /* Fill */
2239 bs_write( s, 3, 0 ); /* Reserved */
2240 bs_write( s, 16, p_sys->p_regions[i_region].i_width );
2241 bs_write( s, 16, p_sys->p_regions[i_region].i_height );
2242 bs_write( s, 3, i_depth ); /* Region level of compatibility */
2243 bs_write( s, 3, i_depth ); /* Region depth */
2244 bs_write( s, 2, 0 ); /* Reserved */
2245 bs_write( s, 8, 1 ); /* Clut id */
2246 bs_write( s, 8, i_bg ); /* region 8bit pixel code */
2247 bs_write( s, 4, i_bg ); /* region 4bit pixel code */
2248 bs_write( s, 2, i_bg ); /* region 2bit pixel code */
2249 bs_write( s, 2, 0 ); /* Reserved */
2251 /* In our implementation we only have 1 object per region */
2252 bs_write( s, 16, i_region );
2253 bs_write( s, 2, b_text ? DVBSUB_OT_BASIC_CHAR:DVBSUB_OT_BASIC_BITMAP );
2254 bs_write( s, 2, 0 ); /* object provider flag */
2255 bs_write( s, 12, 0 );/* object horizontal position */
2256 bs_write( s, 4, 0 ); /* Reserved */
2257 bs_write( s, 12, 0 );/* object vertical position */
2261 bs_write( s, 8, 1 ); /* foreground pixel code */
2262 bs_write( s, 8, 0 ); /* background pixel code */
2267 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2268 subpicture_region_t *p_region,
2271 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2273 encoder_sys_t *p_sys = p_enc->p_sys;
2274 subpicture_region_t *p_region;
2277 int i_length_pos, i_update_pos, i_pixel_data_pos;
2279 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2280 p_region = p_region->p_next, i_region++ )
2282 bs_write( s, 8, 0x0f ); /* Sync byte */
2283 bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
2284 bs_write( s, 16, 1 ); /* Page id */
2286 i_length_pos = bs_pos( s );
2287 bs_write( s, 16, 0 ); /* Segment length */
2288 bs_write( s, 16, i_region ); /* Object id */
2289 bs_write( s, 4, p_sys->i_region_ver++ );
2291 /* object coding method */
2292 switch( p_region->fmt.i_chroma )
2294 case VLC_CODEC_YUVP:
2295 bs_write( s, 2, 0 );
2297 case VLC_CODEC_TEXT:
2298 bs_write( s, 2, 1 );
2301 msg_Err( p_enc, "FOURCC %d not supported by encoder.", p_region->fmt.i_chroma );
2305 bs_write( s, 1, 0 ); /* non modifying color flag */
2306 bs_write( s, 1, 0 ); /* Reserved */
2308 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
2312 if( !p_region->psz_text ) continue;
2314 i_size = __MIN( strlen( p_region->psz_text ), 256 );
2316 bs_write( s, 8, i_size ); /* number of characters in string */
2317 for( i = 0; i < i_size; i++ )
2319 bs_write( s, 16, p_region->psz_text[i] );
2322 /* Update segment length */
2323 SetWBE( &s->p_start[i_length_pos/8],
2324 (bs_pos(s) - i_length_pos)/8 -2 );
2328 /* Coding of a bitmap object */
2329 i_update_pos = bs_pos( s );
2330 bs_write( s, 16, 0 ); /* topfield data block length */
2331 bs_write( s, 16, 0 ); /* bottomfield data block length */
2334 i_pixel_data_pos = bs_pos( s );
2335 encode_pixel_data( p_enc, s, p_region, true );
2336 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2337 SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
2340 i_pixel_data_pos = bs_pos( s );
2341 encode_pixel_data( p_enc, s, p_region, false );
2342 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2343 SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
2345 /* Stuffing for word alignment */
2347 if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
2349 /* Update segment length */
2350 SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
2354 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2356 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2358 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2360 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2361 subpicture_region_t *p_region,
2364 unsigned int i_line;
2367 if( p_region->fmt.i_chroma != VLC_CODEC_YUVP ) return;
2369 /* Encode line by line */
2370 for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
2373 switch( p_region->fmt.p_palette->i_entries )
2379 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
2380 encode_pixel_line_2bp( s, p_region, i_line );
2384 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
2385 encode_pixel_line_4bp( s, p_region, i_line );
2389 bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
2390 encode_pixel_line_8bp( s, p_region, i_line );
2394 msg_Err( p_enc, "subpicture palette (%i) not handled",
2395 p_region->fmt.p_palette->i_entries );
2399 bs_write( s, 8, 0xf0 ); /* End of object line code */
2403 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2406 unsigned int i, i_length = 0;
2407 int i_pitch = p_region->p_picture->p->i_pitch;
2408 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2409 int i_last_pixel = p_data[0];
2411 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2413 if( ( i != p_region->fmt.i_visible_width ) &&
2414 ( p_data[i] == i_last_pixel ) && ( i_length != 284 ) )
2420 if( ( i_length == 1 ) || ( i_length == 11 ) || ( i_length == 28 ) )
2422 /* 2bit/pixel code */
2424 bs_write( s, 2, i_last_pixel );
2427 bs_write( s, 2, 0 );
2428 bs_write( s, 1, 0 );
2429 bs_write( s, 1, 1 ); /* pseudo color 0 */
2438 bs_write( s, 2, i_last_pixel );
2439 bs_write( s, 2, i_last_pixel );
2443 bs_write( s, 2, 0 );
2444 bs_write( s, 1, 0 );
2445 bs_write( s, 1, 0 );
2446 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2449 else if( i_length > 2 )
2451 bs_write( s, 2, 0 );
2452 if( i_length <= 10 )
2454 bs_write( s, 1, 1 );
2455 bs_write( s, 3, i_length - 3 );
2456 bs_write( s, 2, i_last_pixel );
2460 bs_write( s, 1, 0 );
2461 bs_write( s, 1, 0 );
2463 if( i_length <= 27 )
2465 bs_write( s, 2, 2 );
2466 bs_write( s, 4, i_length - 12 );
2467 bs_write( s, 2, i_last_pixel );
2471 bs_write( s, 2, 3 );
2472 bs_write( s, 8, i_length - 29 );
2473 bs_write( s, 2, i_last_pixel );
2478 if( i == p_region->fmt.i_visible_width ) break;
2480 i_last_pixel = p_data[i];
2485 bs_write( s, 2, 0 );
2486 bs_write( s, 1, 0 );
2487 bs_write( s, 1, 0 );
2488 bs_write( s, 2, 0 );
2494 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2497 unsigned int i, i_length = 0;
2498 int i_pitch = p_region->p_picture->p->i_pitch;
2499 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2500 int i_last_pixel = p_data[0];
2502 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2504 if( i != p_region->fmt.i_visible_width &&
2505 p_data[i] == i_last_pixel && i_length != 280 )
2511 if( ( i_length == 1 ) ||
2512 ( ( i_length == 3 ) && i_last_pixel ) ||
2515 /* 4bit/pixel code */
2517 bs_write( s, 4, i_last_pixel );
2520 bs_write( s, 4, 0 );
2521 bs_write( s, 1, 1 );
2522 bs_write( s, 1, 1 );
2523 bs_write( s, 2, 0 ); /* pseudo color 0 */
2532 bs_write( s, 4, i_last_pixel );
2533 bs_write( s, 4, i_last_pixel );
2537 bs_write( s, 4, 0 );
2538 bs_write( s, 1, 1 );
2539 bs_write( s, 1, 1 );
2540 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2543 else if( !i_last_pixel && ( i_length >= 3 ) && ( i_length <= 9 ) )
2545 bs_write( s, 4, 0 );
2546 bs_write( s, 1, 0 );
2547 bs_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
2549 else if( i_length > 2 )
2551 bs_write( s, 4, 0 );
2552 bs_write( s, 1, 1 );
2556 bs_write( s, 1, 0 );
2557 bs_write( s, 2, i_length - 4 );
2558 bs_write( s, 4, i_last_pixel );
2562 bs_write( s, 1, 1 );
2564 if( i_length <= 24 )
2566 bs_write( s, 2, 2 );
2567 bs_write( s, 4, i_length - 9 );
2568 bs_write( s, 4, i_last_pixel );
2572 bs_write( s, 2, 3 );
2573 bs_write( s, 8, i_length - 25 );
2574 bs_write( s, 4, i_last_pixel );
2579 if( i == p_region->fmt.i_visible_width ) break;
2581 i_last_pixel = p_data[i];
2586 bs_write( s, 8, 0 );
2592 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2595 unsigned int i, i_length = 0;
2596 int i_pitch = p_region->p_picture->p->i_pitch;
2597 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2598 int i_last_pixel = p_data[0];
2600 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2602 if( ( i != p_region->fmt.i_visible_width ) &&
2603 ( p_data[i] == i_last_pixel ) && ( i_length != 127 ) )
2609 if( ( i_length == 1 ) && i_last_pixel )
2611 /* 8bit/pixel code */
2612 bs_write( s, 8, i_last_pixel );
2614 else if( ( i_length == 2 ) && i_last_pixel )
2616 /* 8bit/pixel code */
2617 bs_write( s, 8, i_last_pixel );
2618 bs_write( s, 8, i_last_pixel );
2620 else if( i_length <= 127 )
2622 bs_write( s, 8, 0 );
2626 bs_write( s, 1, 0 );
2627 bs_write( s, 7, i_length ); /* pseudo color 0 */
2631 bs_write( s, 1, 1 );
2632 bs_write( s, 7, i_length );
2633 bs_write( s, 8, i_last_pixel );
2637 if( i == p_region->fmt.i_visible_width ) break;
2639 i_last_pixel = p_data[i];
2644 bs_write( s, 8, 0 );
2645 bs_write( s, 8, 0 );
2652 static void default_dds_init( decoder_t * p_dec )
2654 decoder_sys_t *p_sys = p_dec->p_sys;
2656 /* see notes on DDS at the top of the file */
2658 /* configure for SD res in case DDS is not present */
2659 p_sys->display.i_version = 0xff; /* an invalid version so it's always different */
2660 p_sys->display.i_width = 720;
2661 p_sys->display.i_height = 576;
2662 p_sys->display.b_windowed = false;