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 VLC authors and VideoLAN
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 it
17 * under the terms of the GNU Lesser General Public License as published by
18 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this program; if not, write to the Free Software Foundation, Inc.,
28 * Inc., 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 ** );
113 static int OpenEncoder ( vlc_object_t * );
114 static void CloseEncoder( vlc_object_t * );
115 static block_t *Encode ( encoder_t *, subpicture_t * );
119 # define DVBSUB_CFG_PREFIX "dvbsub-"
120 set_description( N_("DVB subtitles decoder") )
121 set_shortname( N_("DVB subtitles") )
122 set_capability( "decoder", 80 )
123 set_category( CAT_INPUT )
124 set_subcategory( SUBCAT_INPUT_SCODEC )
125 set_callbacks( Open, Close )
127 add_integer( DVBSUB_CFG_PREFIX "position", 8, POS_TEXT, POS_LONGTEXT, true )
128 change_integer_list( pi_pos_values, ppsz_pos_descriptions )
129 add_integer( DVBSUB_CFG_PREFIX "x", -1, POSX_TEXT, POSX_LONGTEXT, false )
130 add_integer( DVBSUB_CFG_PREFIX "y", -1, POSY_TEXT, POSY_LONGTEXT, false )
133 # define ENC_CFG_PREFIX "sout-dvbsub-"
135 set_description( N_("DVB subtitles encoder") )
136 set_capability( "encoder", 100 )
137 set_callbacks( OpenEncoder, CloseEncoder )
139 add_integer( ENC_CFG_PREFIX "x", -1, ENC_POSX_TEXT, ENC_POSX_LONGTEXT, false )
140 add_integer( ENC_CFG_PREFIX "y", -1, ENC_POSY_TEXT, ENC_POSY_LONGTEXT, false )
144 static const char *const ppsz_enc_options[] = { "x", "y", NULL };
146 /****************************************************************************
148 ****************************************************************************
149 * Those structures refer closely to the ETSI 300 743 Object model
150 ****************************************************************************/
152 /* The object definition gives the position of the object in a region [7.2.5] */
153 typedef struct dvbsub_objectdef_s
161 char *psz_text; /* for string of characters objects */
163 } dvbsub_objectdef_t;
165 /* The entry in the palette CLUT */
175 /* The displays dimensions [7.2.1] */
176 typedef struct dvbsub_display_s
185 /* these values are only relevant if windowed */
194 typedef struct dvbsub_clut_s
198 dvbsub_color_t c_2b[4];
199 dvbsub_color_t c_4b[16];
200 dvbsub_color_t c_8b[256];
202 struct dvbsub_clut_s *p_next;
206 /* The Region is an aera on the image [7.2.3]
207 * with a list of the object definitions associated and a CLUT */
208 typedef struct dvbsub_region_s
223 dvbsub_objectdef_t *p_object_defs;
225 struct dvbsub_region_s *p_next;
229 /* The object definition gives the position of the object in a region */
230 typedef struct dvbsub_regiondef_s
236 } dvbsub_regiondef_t;
238 /* The page defines the list of regions [7.2.2] */
242 int i_timeout; /* in seconds */
247 dvbsub_regiondef_t *p_region_defs;
255 /* Decoder internal data */
266 dvbsub_page_t *p_page;
267 dvbsub_region_t *p_regions;
268 dvbsub_clut_t *p_cluts;
269 /* this is very small, so keep forever */
270 dvbsub_display_t display;
271 dvbsub_clut_t default_clut;
275 /* List of different SEGMENT TYPES */
276 /* According to EN 300-743, table 2 */
277 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
278 #define DVBSUB_ST_REGION_COMPOSITION 0x11
279 #define DVBSUB_ST_CLUT_DEFINITION 0x12
280 #define DVBSUB_ST_OBJECT_DATA 0x13
281 #define DVBSUB_ST_DISPLAY_DEFINITION 0x14
282 #define DVBSUB_ST_ENDOFDISPLAY 0x80
283 #define DVBSUB_ST_STUFFING 0xff
284 /* List of different OBJECT TYPES */
285 /* According to EN 300-743, table 6 */
286 #define DVBSUB_OT_BASIC_BITMAP 0x00
287 #define DVBSUB_OT_BASIC_CHAR 0x01
288 #define DVBSUB_OT_COMPOSITE_STRING 0x02
289 /* Pixel DATA TYPES */
290 /* According to EN 300-743, table 9 */
291 #define DVBSUB_DT_2BP_CODE_STRING 0x10
292 #define DVBSUB_DT_4BP_CODE_STRING 0x11
293 #define DVBSUB_DT_8BP_CODE_STRING 0x12
294 #define DVBSUB_DT_24_TABLE_DATA 0x20
295 #define DVBSUB_DT_28_TABLE_DATA 0x21
296 #define DVBSUB_DT_48_TABLE_DATA 0x22
297 #define DVBSUB_DT_END_LINE 0xf0
298 /* List of different Page Composition Segment state */
299 /* According to EN 300-743, 7.2.1 table 3 */
300 #define DVBSUB_PCS_STATE_ACQUISITION 0x01
301 #define DVBSUB_PCS_STATE_CHANGE 0x02
303 /*****************************************************************************
305 *****************************************************************************/
306 static void decode_segment( decoder_t *, bs_t * );
307 static void decode_page_composition( decoder_t *, bs_t * );
308 static void decode_region_composition( decoder_t *, bs_t * );
309 static void decode_object( decoder_t *, bs_t * );
310 static void decode_display_definition( decoder_t *, bs_t * );
311 static void decode_clut( decoder_t *, bs_t * );
312 static void free_all( decoder_t * );
314 static void default_clut_init( decoder_t * );
315 static void default_dds_init( decoder_t * );
317 static subpicture_t *render( decoder_t * );
319 /*****************************************************************************
320 * Open: probe the decoder and return score
321 *****************************************************************************
322 * Tries to launch a decoder and return score so that the interface is able
324 *****************************************************************************/
325 static int Open( vlc_object_t *p_this )
327 decoder_t *p_dec = (decoder_t *) p_this;
328 decoder_sys_t *p_sys;
331 if( p_dec->fmt_in.i_codec != VLC_CODEC_DVBS )
336 p_dec->pf_decode_sub = Decode;
337 p_sys = p_dec->p_sys = calloc( 1, sizeof(decoder_sys_t) );
341 p_sys->i_pts = VLC_TS_INVALID;
342 p_sys->i_id = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
343 p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
345 p_sys->p_regions = NULL;
346 p_sys->p_cluts = NULL;
347 p_sys->p_page = NULL;
349 /* configure for SD res in case DDS is not present */
350 default_dds_init( p_dec );
352 p_sys->i_spu_position = var_CreateGetInteger( p_this,
353 DVBSUB_CFG_PREFIX "position" );
354 i_posx = var_CreateGetInteger( p_this, DVBSUB_CFG_PREFIX "x" );
355 i_posy = var_CreateGetInteger( p_this, DVBSUB_CFG_PREFIX "y" );
357 /* Check if subpicture position was overridden */
358 p_sys->b_absolute = true;
359 p_sys->i_spu_x = p_sys->i_spu_y = 0;
361 if( ( i_posx >= 0 ) && ( i_posy >= 0 ) )
363 p_sys->b_absolute = true;
364 p_sys->i_spu_x = i_posx;
365 p_sys->i_spu_y = i_posy;
368 p_dec->fmt_out.i_cat = SPU_ES;
369 p_dec->fmt_out.i_codec = 0;
371 default_clut_init( p_dec );
376 /*****************************************************************************
378 *****************************************************************************/
379 static void Close( vlc_object_t *p_this )
381 decoder_t *p_dec = (decoder_t*) p_this;
382 decoder_sys_t *p_sys = p_dec->p_sys;
384 var_Destroy( p_this, DVBSUB_CFG_PREFIX "x" );
385 var_Destroy( p_this, DVBSUB_CFG_PREFIX "y" );
386 var_Destroy( p_this, DVBSUB_CFG_PREFIX "position" );
392 /*****************************************************************************
394 *****************************************************************************/
395 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
397 decoder_sys_t *p_sys = p_dec->p_sys;
399 subpicture_t *p_spu = NULL;
401 if( ( pp_block == NULL ) || ( *pp_block == NULL ) ) return NULL;
405 /* configure for SD res in case DDS is not present */
406 /* a change of PTS is a good indication we must get a new DDS */
407 if (p_sys->i_pts != p_block->i_pts)
408 default_dds_init( p_dec );
410 p_sys->i_pts = p_block->i_pts;
411 if( p_sys->i_pts <= VLC_TS_INVALID )
414 /* Some DVB channels send stuffing segments in non-dated packets so
415 * don't complain too loudly. */
416 msg_Warn( p_dec, "non dated subtitle" );
418 block_Release( p_block );
422 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
424 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
426 msg_Dbg( p_dec, "invalid data identifier" );
427 block_Release( p_block );
431 if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
433 msg_Dbg( p_dec, "invalid subtitle stream id" );
434 block_Release( p_block );
439 msg_Dbg( p_dec, "subtitle packet received: %"PRId64, p_sys->i_pts );
442 p_sys->b_page = false;
443 while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
445 decode_segment( p_dec, &p_sys->bs );
448 if( ( bs_read( &p_sys->bs, 8 ) & 0x3f ) != 0x3f ) /* End marker */
450 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
451 block_Release( p_block );
455 /* Check if the page is to be displayed */
456 if( p_sys->p_page && p_sys->b_page )
457 p_spu = render( p_dec );
459 block_Release( p_block );
464 /* following functions are local */
466 /*****************************************************************************
467 * default_clut_init: default clut as defined in EN 300-743 section 10
468 *****************************************************************************/
469 static void default_clut_init( decoder_t *p_dec )
471 decoder_sys_t *p_sys = p_dec->p_sys;
474 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
475 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
476 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
479 for( i = 0; i < 4; i++ )
481 uint8_t R = 0, G = 0, B = 0, T = 0;
483 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
484 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
485 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
486 else R = G = B = 0x7F;
488 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
489 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
490 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
491 p_sys->default_clut.c_2b[i].T = T;
494 /* 16 entries CLUT */
495 for( i = 0; i < 16; i++ )
497 uint8_t R = 0, G = 0, B = 0, T = 0;
501 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
507 R = (i & 0x1) ? 0xFF : 0;
508 G = (i & 0x2) ? 0xFF : 0;
509 B = (i & 0x4) ? 0xFF : 0;
514 R = (i & 0x1) ? 0x7F : 0;
515 G = (i & 0x2) ? 0x7F : 0;
516 B = (i & 0x4) ? 0x7F : 0;
519 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
520 p_sys->default_clut.c_4b[i].Cr = RGB_TO_V(R,G,B);
521 p_sys->default_clut.c_4b[i].Cb = RGB_TO_U(R,G,B);
522 p_sys->default_clut.c_4b[i].T = T;
525 /* 256 entries CLUT */
526 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
529 static void decode_segment( decoder_t *p_dec, bs_t *s )
531 decoder_sys_t *p_sys = p_dec->p_sys;
536 /* sync_byte (already checked) */
540 i_type = bs_read( s, 8 );
543 i_page_id = bs_read( s, 16 );
546 i_size = bs_show( s, 16 );
548 if( ( i_page_id != p_sys->i_id ) &&
549 ( i_page_id != p_sys->i_ancillary_id ) )
552 msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
553 i_page_id, p_sys->i_id );
555 bs_skip( s, 8 * ( 2 + i_size ) );
559 if( ( p_sys->i_ancillary_id != p_sys->i_id ) &&
560 ( i_type == DVBSUB_ST_PAGE_COMPOSITION ) &&
561 ( i_page_id == p_sys->i_ancillary_id ) )
564 msg_Dbg( p_dec, "skipped invalid ancillary subtitle packet" );
566 bs_skip( s, 8 * ( 2 + i_size ) );
571 if( i_page_id == p_sys->i_id )
572 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
574 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
579 case DVBSUB_ST_PAGE_COMPOSITION:
581 msg_Dbg( p_dec, "decode_page_composition" );
583 decode_page_composition( p_dec, s );
586 case DVBSUB_ST_REGION_COMPOSITION:
588 msg_Dbg( p_dec, "decode_region_composition" );
590 decode_region_composition( p_dec, s );
593 case DVBSUB_ST_CLUT_DEFINITION:
595 msg_Dbg( p_dec, "decode_clut" );
597 decode_clut( p_dec, s );
600 case DVBSUB_ST_OBJECT_DATA:
602 msg_Dbg( p_dec, "decode_object" );
604 decode_object( p_dec, s );
607 case DVBSUB_ST_DISPLAY_DEFINITION:
609 msg_Dbg( p_dec, "decode_display_definition" );
611 decode_display_definition( p_dec, s );
614 case DVBSUB_ST_ENDOFDISPLAY:
616 msg_Dbg( p_dec, "end of display" );
618 bs_skip( s, 8 * ( 2 + i_size ) );
621 case DVBSUB_ST_STUFFING:
623 msg_Dbg( p_dec, "skip stuffing" );
625 bs_skip( s, 8 * ( 2 + i_size ) );
629 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
630 bs_skip( s, 8 * ( 2 + i_size ) );
635 static void decode_clut( decoder_t *p_dec, bs_t *s )
637 decoder_sys_t *p_sys = p_dec->p_sys;
638 uint16_t i_segment_length;
639 uint16_t i_processed_length;
640 dvbsub_clut_t *p_clut, *p_next;
643 i_segment_length = bs_read( s, 16 );
644 i_id = bs_read( s, 8 );
645 i_version = bs_read( s, 4 );
647 /* Check if we already have this clut */
648 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
650 if( p_clut->i_id == i_id ) break;
653 /* Check version number */
654 if( p_clut && ( p_clut->i_version == i_version ) )
657 bs_skip( s, 8 * i_segment_length - 12 );
664 msg_Dbg( p_dec, "new clut: %i", i_id );
666 p_clut = malloc( sizeof( dvbsub_clut_t ) );
669 p_clut->p_next = p_sys->p_cluts;
670 p_sys->p_cluts = p_clut;
673 /* Initialize to default clut */
674 p_next = p_clut->p_next;
675 *p_clut = p_sys->default_clut;
676 p_clut->p_next = p_next;
678 /* We don't have this version of the CLUT: Parse it */
679 p_clut->i_version = i_version;
681 bs_skip( s, 4 ); /* Reserved bits */
682 i_processed_length = 2;
683 while( i_processed_length < i_segment_length )
685 uint8_t y, cb, cr, t;
689 i_id = bs_read( s, 8 );
690 i_type = bs_read( s, 3 );
694 if( bs_read( s, 1 ) )
697 cr = bs_read( s, 8 );
698 cb = bs_read( s, 8 );
700 i_processed_length += 6;
704 y = bs_read( s, 6 ) << 2;
705 cr = bs_read( s, 4 ) << 4;
706 cb = bs_read( s, 4 ) << 4;
707 t = bs_read( s, 2 ) << 6;
708 i_processed_length += 4;
711 /* We are not entirely compliant here as full transparency is indicated
712 * with a luma value of zero, not a transparency value of 0xff
713 * (full transparency would actually be 0xff + 1). */
720 /* According to EN 300-743 section 7.2.3 note 1, type should
721 * not have more than 1 bit set to one, but some streams don't
722 * respect this note. */
723 if( ( i_type & 0x04 ) && ( i_id < 4 ) )
725 p_clut->c_2b[i_id].Y = y;
726 p_clut->c_2b[i_id].Cr = cr;
727 p_clut->c_2b[i_id].Cb = cb;
728 p_clut->c_2b[i_id].T = t;
730 if( ( i_type & 0x02 ) && ( i_id < 16 ) )
732 p_clut->c_4b[i_id].Y = y;
733 p_clut->c_4b[i_id].Cr = cr;
734 p_clut->c_4b[i_id].Cb = cb;
735 p_clut->c_4b[i_id].T = t;
739 p_clut->c_8b[i_id].Y = y;
740 p_clut->c_8b[i_id].Cr = cr;
741 p_clut->c_8b[i_id].Cb = cb;
742 p_clut->c_8b[i_id].T = t;
747 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
749 decoder_sys_t *p_sys = p_dec->p_sys;
750 int i_version, i_state, i_segment_length, i_timeout, i;
752 /* A page is composed by 0 or more region */
753 i_segment_length = bs_read( s, 16 );
754 i_timeout = bs_read( s, 8 );
755 i_version = bs_read( s, 4 );
756 i_state = bs_read( s, 2 );
757 bs_skip( s, 2 ); /* Reserved */
759 if( i_state == DVBSUB_PCS_STATE_CHANGE )
761 /* End of an epoch, reset decoder buffer */
763 msg_Dbg( p_dec, "page composition mode change" );
767 else if( !p_sys->p_page && ( i_state != DVBSUB_PCS_STATE_ACQUISITION ) &&
768 ( i_state != DVBSUB_PCS_STATE_CHANGE ) )
770 /* Not a full PCS, we need to wait for one */
771 msg_Dbg( p_dec, "didn't receive an acquisition page yet" );
774 /* Try to start decoding even without an acquisition page */
775 bs_skip( s, 8 * (i_segment_length - 2) );
781 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
782 msg_Dbg( p_dec, "acquisition page composition" );
785 /* Check version number */
786 if( p_sys->p_page && ( p_sys->p_page->i_version == i_version ) )
788 bs_skip( s, 8 * (i_segment_length - 2) );
791 else if( p_sys->p_page )
793 if( p_sys->p_page->i_region_defs )
794 free( p_sys->p_page->p_region_defs );
795 p_sys->p_page->p_region_defs = NULL;
796 p_sys->p_page->i_region_defs = 0;
802 msg_Dbg( p_dec, "new page" );
804 /* Allocate a new page */
805 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
810 p_sys->p_page->i_version = i_version;
811 p_sys->p_page->i_timeout = i_timeout;
812 p_sys->b_page = true;
814 /* Number of regions */
815 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
817 if( p_sys->p_page->i_region_defs == 0 ) return;
819 p_sys->p_page->p_region_defs =
820 malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_regiondef_t) );
821 if( p_sys->p_page->p_region_defs )
823 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
825 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
826 bs_skip( s, 8 ); /* Reserved */
827 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
828 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
831 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
832 i, p_sys->p_page->p_region_defs[i].i_x,
833 p_sys->p_page->p_region_defs[i].i_y );
839 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
841 decoder_sys_t *p_sys = p_dec->p_sys;
842 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
843 int i_segment_length, i_processed_length, i_id, i_version;
844 int i_width, i_height, i_level_comp, i_depth, i_clut;
845 int i_8_bg, i_4_bg, i_2_bg;
848 i_segment_length = bs_read( s, 16 );
849 i_id = bs_read( s, 8 );
850 i_version = bs_read( s, 4 );
852 /* Check if we already have this region */
853 for( p_region = p_sys->p_regions; p_region != NULL;
854 p_region = p_region->p_next )
856 pp_region = &p_region->p_next;
857 if( p_region->i_id == i_id ) break;
860 /* Check version number */
861 if( p_region && ( p_region->i_version == i_version ) )
863 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
870 msg_Dbg( p_dec, "new region: %i", i_id );
872 p_region = *pp_region = calloc( 1, sizeof(dvbsub_region_t) );
875 p_region->p_object_defs = NULL;
876 p_region->p_pixbuf = NULL;
877 p_region->p_next = NULL;
880 /* Region attributes */
881 p_region->i_id = i_id;
882 p_region->i_version = i_version;
883 b_fill = bs_read( s, 1 );
884 bs_skip( s, 3 ); /* Reserved */
886 i_width = bs_read( s, 16 );
887 i_height = bs_read( s, 16 );
889 msg_Dbg( p_dec, " width=%d height=%d", i_width, i_height );
891 i_level_comp = bs_read( s, 3 );
892 i_depth = bs_read( s, 3 );
893 bs_skip( s, 2 ); /* Reserved */
894 i_clut = bs_read( s, 8 );
896 i_8_bg = bs_read( s, 8 );
897 i_4_bg = bs_read( s, 4 );
898 i_2_bg = bs_read( s, 2 );
899 bs_skip( s, 2 ); /* Reserved */
901 /* Free old object defs */
902 while( p_region->i_object_defs )
903 free( p_region->p_object_defs[--p_region->i_object_defs].psz_text );
905 free( p_region->p_object_defs );
906 p_region->p_object_defs = NULL;
908 /* Extra sanity checks */
909 if( ( p_region->i_width != i_width ) ||
910 ( p_region->i_height != i_height ) )
912 if( p_region->p_pixbuf )
914 msg_Dbg( p_dec, "region size changed (%dx%d->%dx%d)",
915 p_region->i_width, p_region->i_height, i_width, i_height );
916 free( p_region->p_pixbuf );
919 p_region->p_pixbuf = xmalloc( i_height * i_width );
920 p_region->i_depth = 0;
923 if( p_region->i_depth &&
924 ( ( p_region->i_depth != i_depth ) ||
925 ( p_region->i_level_comp != i_level_comp ) ||
926 ( p_region->i_clut != i_clut) ) )
928 msg_Dbg( p_dec, "region parameters changed (not allowed)" );
931 /* Erase background of region */
934 int i_background = ( p_region->i_depth == 1 ) ? i_2_bg :
935 ( ( p_region->i_depth == 2 ) ? i_4_bg : i_8_bg );
936 memset( p_region->p_pixbuf, i_background, i_width * i_height );
939 p_region->i_width = i_width;
940 p_region->i_height = i_height;
941 p_region->i_level_comp = i_level_comp;
942 p_region->i_depth = i_depth;
943 p_region->i_clut = i_clut;
945 /* List of objects in the region */
946 i_processed_length = 10;
947 while( i_processed_length < i_segment_length )
949 dvbsub_objectdef_t *p_obj;
951 /* We create a new object */
952 p_region->i_object_defs++;
953 p_region->p_object_defs = xrealloc( p_region->p_object_defs,
954 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
956 /* We parse object properties */
957 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
958 p_obj->i_id = bs_read( s, 16 );
959 p_obj->i_type = bs_read( s, 2 );
960 bs_skip( s, 2 ); /* Provider */
961 p_obj->i_x = bs_read( s, 12 );
962 bs_skip( s, 4 ); /* Reserved */
963 p_obj->i_y = bs_read( s, 12 );
964 p_obj->psz_text = NULL;
966 i_processed_length += 6;
968 if( ( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ) ||
969 ( p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING ) )
971 p_obj->i_fg_pc = bs_read( s, 8 );
972 p_obj->i_bg_pc = bs_read( s, 8 );
973 i_processed_length += 2;
978 /* ETSI 300 743 [7.2.1] */
979 static void decode_display_definition( decoder_t *p_dec, bs_t *s )
981 decoder_sys_t *p_sys = p_dec->p_sys;
982 uint16_t i_segment_length;
983 uint16_t i_processed_length = 40;
986 i_segment_length = bs_read( s, 16 );
987 i_version = bs_read( s, 4 );
989 /* Check version number */
990 if( p_sys->display.i_version == i_version )
992 /* The definition did not change */
993 bs_skip( s, 8*i_segment_length - 4 );
998 msg_Dbg( p_dec, "new display definition: %i", i_version );
1001 /* We don't have this version of the display definition: Parse it */
1002 p_sys->display.i_version = i_version;
1003 p_sys->display.b_windowed = bs_read( s, 1 );
1004 bs_skip( s, 3 ); /* Reserved bits */
1005 p_sys->display.i_width = bs_read( s, 16 )+1;
1006 p_sys->display.i_height = bs_read( s, 16 )+1;
1008 if( p_sys->display.b_windowed )
1011 msg_Dbg( p_dec, "display definition with offsets (windowed)" );
1013 /* Coordinates are measured from the top left corner */
1014 p_sys->display.i_x = bs_read( s, 16 );
1015 p_sys->display.i_max_x = bs_read( s, 16 );
1016 p_sys->display.i_y = bs_read( s, 16 );
1017 p_sys->display.i_max_y = bs_read( s, 16 );
1018 i_processed_length += 64;
1022 /* if not windowed, setup the window variables to good defaults */
1023 /* not necessary, but to avoid future confusion.. */
1024 p_sys->display.i_x = 0;
1025 p_sys->display.i_max_x = p_sys->display.i_width-1;
1026 p_sys->display.i_y = 0;
1027 p_sys->display.i_max_y = p_sys->display.i_height-1;
1030 if( i_processed_length != i_segment_length*8 )
1032 msg_Err( p_dec, "processed length %d bytes != segment length %d bytes",
1033 i_processed_length / 8 , i_segment_length );
1037 msg_Dbg( p_dec, "version: %d, width: %d, height: %d",
1038 p_sys->display.i_version, p_sys->display.i_width, p_sys->display.i_height );
1039 if( p_sys->display.b_windowed )
1040 msg_Dbg( p_dec, "xmin: %d, xmax: %d, ymin: %d, ymax: %d",
1041 p_sys->display.i_x, p_sys->display.i_max_x, p_sys->display.i_y, p_sys->display.i_max_y );
1045 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
1047 static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
1048 static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
1049 static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
1051 static void decode_object( decoder_t *p_dec, bs_t *s )
1053 decoder_sys_t *p_sys = p_dec->p_sys;
1054 dvbsub_region_t *p_region;
1055 int i_segment_length, i_coding_method, i_id, i;
1057 /* ETSI 300-743 paragraph 7.2.4
1058 * sync_byte, segment_type and page_id have already been processed.
1060 i_segment_length = bs_read( s, 16 );
1061 i_id = bs_read( s, 16 );
1062 bs_skip( s, 4 ); /* version */
1063 i_coding_method = bs_read( s, 2 );
1065 if( i_coding_method > 1 )
1067 msg_Dbg( p_dec, "unknown DVB subtitling coding %d is not handled!", i_coding_method );
1068 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1072 /* Check if the object needs to be rendered in at least one
1074 for( p_region = p_sys->p_regions; p_region != NULL;
1075 p_region = p_region->p_next )
1077 for( i = 0; i < p_region->i_object_defs; i++ )
1078 if( p_region->p_object_defs[i].i_id == i_id ) break;
1080 if( i != p_region->i_object_defs ) break;
1084 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1089 msg_Dbg( p_dec, "new object: %i", i_id );
1092 bs_skip( s, 1 ); /* non_modify_color */
1093 bs_skip( s, 1 ); /* Reserved */
1095 if( i_coding_method == 0x00 )
1097 int i_topfield, i_bottomfield;
1098 uint8_t *p_topfield, *p_bottomfield;
1100 i_topfield = bs_read( s, 16 );
1101 i_bottomfield = bs_read( s, 16 );
1102 p_topfield = s->p_start + bs_pos( s ) / 8;
1103 p_bottomfield = p_topfield + i_topfield;
1105 bs_skip( s, 8 * (i_segment_length - 7) );
1108 if( ( i_segment_length < ( i_topfield + i_bottomfield + 7 ) ) ||
1109 ( ( p_topfield + i_topfield + i_bottomfield ) > s->p_end ) )
1111 msg_Dbg( p_dec, "corrupted object data" );
1115 for( p_region = p_sys->p_regions; p_region != NULL;
1116 p_region = p_region->p_next )
1118 for( i = 0; i < p_region->i_object_defs; i++ )
1120 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1122 dvbsub_render_pdata( p_dec, p_region,
1123 p_region->p_object_defs[i].i_x,
1124 p_region->p_object_defs[i].i_y,
1125 p_topfield, i_topfield );
1129 dvbsub_render_pdata( p_dec, p_region,
1130 p_region->p_object_defs[i].i_x,
1131 p_region->p_object_defs[i].i_y + 1,
1132 p_bottomfield, i_bottomfield );
1136 /* Duplicate the top field */
1137 dvbsub_render_pdata( p_dec, p_region,
1138 p_region->p_object_defs[i].i_x,
1139 p_region->p_object_defs[i].i_y + 1,
1140 p_topfield, i_topfield );
1147 /* DVB subtitling as characters */
1148 int i_number_of_codes = bs_read( s, 8 );
1149 uint8_t* p_start = s->p_start + bs_pos( s ) / 8;
1152 if( ( i_segment_length < ( i_number_of_codes*2 + 4 ) ) ||
1153 ( ( p_start + i_number_of_codes*2 ) > s->p_end ) )
1155 msg_Dbg( p_dec, "corrupted object data" );
1159 for( p_region = p_sys->p_regions; p_region != NULL;
1160 p_region = p_region->p_next )
1162 for( i = 0; i < p_region->i_object_defs; i++ )
1166 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1168 p_region->p_object_defs[i].psz_text =
1169 xrealloc( p_region->p_object_defs[i].psz_text,
1170 i_number_of_codes + 1 );
1172 /* FIXME 16bits -> char ??? See Preamble */
1173 for( j = 0; j < i_number_of_codes; j++ )
1175 p_region->p_object_defs[i].psz_text[j] = (char)(bs_read( s, 16 ) & 0xFF);
1177 /* Null terminate the string */
1178 p_region->p_object_defs[i].psz_text[j] = 0;
1184 msg_Dbg( p_dec, "end object: %i", i_id );
1188 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
1190 uint8_t *p_field, int i_field )
1197 if( !p_region->p_pixbuf )
1199 msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
1202 if( i_y < 0 || i_x < 0 || i_y >= p_region->i_height ||
1203 i_x >= p_region->i_width )
1205 msg_Dbg( p_dec, "invalid offset (%i,%i)", i_x, i_y );
1209 p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
1210 bs_init( &bs, p_field, i_field );
1212 while( !bs_eof( &bs ) )
1215 if( i_y >= p_region->i_height ) return;
1217 switch( bs_read( &bs, 8 ) )
1220 dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1225 dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1230 dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1237 /* We don't use map tables */
1240 case 0xf0: /* End of line code */
1241 p_pixbuf += 2*p_region->i_width;
1242 i_offset = 0; i_y += 2;
1248 static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1250 bool b_stop = false;
1252 while( !b_stop && !bs_eof( s ) )
1254 int i_count = 0, i_color = 0;
1256 i_color = bs_read( s, 2 );
1257 if( i_color != 0x00 )
1263 if( bs_read( s, 1 ) == 0x01 ) // Switch1
1265 i_count = 3 + bs_read( s, 3 );
1266 i_color = bs_read( s, 2 );
1270 if( bs_read( s, 1 ) == 0x00 ) //Switch2
1272 switch( bs_read( s, 2 ) ) //Switch3
1281 i_count = 12 + bs_read( s, 4 );
1282 i_color = bs_read( s, 2 );
1285 i_count = 29 + bs_read( s, 8 );
1286 i_color = bs_read( s, 2 );
1294 /* 1 pixel color 0 */
1300 if( !i_count ) continue;
1303 if( ( i_count + *pi_off ) > i_width ) break;
1305 if( i_count == 1 ) p[*pi_off] = i_color;
1306 else memset( ( p + *pi_off ), i_color, i_count );
1308 (*pi_off) += i_count;
1314 static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1316 bool b_stop = false;
1318 while( !b_stop && !bs_eof( s ) )
1320 int i_count = 0, i_color = 0;
1322 i_color = bs_read( s, 4 );
1323 if( i_color != 0x00 )
1330 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1332 if( bs_show( s, 3 ) != 0x00 )
1334 i_count = 2 + bs_read( s, 3 );
1344 if( bs_read( s, 1 ) == 0x00) //Switch2
1346 i_count = 4 + bs_read( s, 2 );
1347 i_color = bs_read( s, 4 );
1351 switch ( bs_read( s, 2 ) ) //Switch3
1360 i_count = 9 + bs_read( s, 4 );
1361 i_color = bs_read( s, 4 );
1364 i_count= 25 + bs_read( s, 8 );
1365 i_color = bs_read( s, 4 );
1372 if( !i_count ) continue;
1375 if( ( i_count + *pi_off ) > i_width ) break;
1377 if( i_count == 1 ) p[*pi_off] = i_color;
1378 else memset( ( p + *pi_off ), i_color, i_count );
1380 (*pi_off) += i_count;
1386 static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1388 bool b_stop = false;
1390 while( !b_stop && !bs_eof( s ) )
1392 int i_count = 0, i_color = 0;
1394 i_color = bs_read( s, 8 );
1395 if( i_color != 0x00 )
1402 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1404 if( bs_show( s, 7 ) != 0x00 )
1406 i_count = bs_read( s, 7 );
1416 i_count = bs_read( s, 7 );
1417 i_color = bs_read( s, 8 );
1421 if( !i_count ) continue;
1424 if( ( i_count + *pi_off ) > i_width ) break;
1426 if( i_count == 1 ) p[*pi_off] = i_color;
1427 else memset( ( p + *pi_off ), i_color, i_count );
1429 (*pi_off) += i_count;
1435 static void free_all( decoder_t *p_dec )
1437 decoder_sys_t *p_sys = p_dec->p_sys;
1438 dvbsub_region_t *p_reg, *p_reg_next;
1439 dvbsub_clut_t *p_clut, *p_clut_next;
1441 /*free( p_sys->p_display ); No longer malloced */
1443 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
1445 p_clut_next = p_clut->p_next;
1448 p_sys->p_cluts = NULL;
1450 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1454 p_reg_next = p_reg->p_next;
1455 for( i = 0; i < p_reg->i_object_defs; i++ )
1456 free( p_reg->p_object_defs[i].psz_text );
1457 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1458 free( p_reg->p_pixbuf );
1461 p_sys->p_regions = NULL;
1465 if( p_sys->p_page->i_region_defs )
1466 free( p_sys->p_page->p_region_defs );
1467 free( p_sys->p_page );
1469 p_sys->p_page = NULL;
1472 static subpicture_t *render( decoder_t *p_dec )
1474 decoder_sys_t *p_sys = p_dec->p_sys;
1475 subpicture_t *p_spu;
1476 subpicture_region_t **pp_spu_region;
1481 /* Allocate the subpicture internal data. */
1482 p_spu = decoder_NewSubpicture( p_dec, NULL );
1486 p_spu->b_absolute = p_sys->b_absolute;
1487 /* Set the pf_render callback */
1488 p_spu->i_start = p_sys->i_pts;
1489 //p_spu->i_stop = (mtime_t) 0;
1490 p_spu->b_ephemer = true;
1491 //p_spu->b_fade = true;
1492 //p_spu->i_stop = p_spu->i_start + (mtime_t) (i_timeout * 1000000);
1493 p_spu->b_subtitle = true;
1495 /* Correct positioning of SPU */
1496 i_base_x = p_sys->i_spu_x;
1497 i_base_y = p_sys->i_spu_y;
1498 p_spu->i_original_picture_width = 720;
1499 p_spu->i_original_picture_height = 576;
1501 p_spu->i_original_picture_width = p_sys->display.i_width;
1502 p_spu->i_original_picture_height = p_sys->display.i_height;
1504 if( p_sys->display.b_windowed )
1506 /* From en_300743v01 - */
1507 /* the DDS is there to indicate intended size/position of text */
1508 /* the intended video area is ->i_width/height */
1509 /* the window is within this... SO... we should leave i_original_picture_width etc. as is */
1510 /* and ONLY change i_base_x. effectively i_max_x/y are only there to limit memory requirements*/
1511 /* we COULD use the upper limits to limit rendering to within these? */
1513 /* see notes on DDS at the top of the file */
1514 i_base_x += p_sys->display.i_x;
1515 i_base_y += p_sys->display.i_y;
1518 pp_spu_region = &p_spu->p_region;
1520 /* Loop on region definitions */
1523 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1526 for( i = 0; p_sys->p_page && ( i < p_sys->p_page->i_region_defs ); i++ )
1528 dvbsub_region_t *p_region;
1529 dvbsub_regiondef_t *p_regiondef;
1530 dvbsub_clut_t *p_clut;
1531 dvbsub_color_t *p_color;
1532 subpicture_region_t *p_spu_region;
1533 uint8_t *p_src, *p_dst;
1535 video_palette_t palette;
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_sar_num = 0; /* 0 means use aspect ratio of background video */
1588 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1589 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1590 fmt.i_x_offset = fmt.i_y_offset = 0;
1591 fmt.p_palette = &palette;
1592 fmt.p_palette->i_entries = ( p_region->i_depth == 1 ) ? 4 :
1593 ( ( p_region->i_depth == 2 ) ? 16 : 256 );
1594 p_color = ( p_region->i_depth == 1 ) ? p_clut->c_2b :
1595 ( ( p_region->i_depth == 2 ) ? p_clut->c_4b : p_clut->c_8b );
1596 for( j = 0; j < fmt.p_palette->i_entries; j++ )
1598 fmt.p_palette->palette[j][0] = p_color[j].Y;
1599 fmt.p_palette->palette[j][1] = p_color[j].Cb; /* U == Cb */
1600 fmt.p_palette->palette[j][2] = p_color[j].Cr; /* V == Cr */
1601 fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1604 p_spu_region = subpicture_region_New( &fmt );
1607 msg_Err( p_dec, "cannot allocate SPU region" );
1610 p_spu_region->i_x = i_base_x + p_regiondef->i_x;
1611 p_spu_region->i_y = i_base_y + p_regiondef->i_y;
1612 p_spu_region->i_align = p_sys->i_spu_position;
1613 *pp_spu_region = p_spu_region;
1614 pp_spu_region = &p_spu_region->p_next;
1616 p_src = p_region->p_pixbuf;
1617 p_dst = p_spu_region->p_picture->Y_PIXELS;
1618 i_pitch = p_spu_region->p_picture->Y_PITCH;
1620 /* Copy pixel buffer */
1621 for( j = 0; j < p_region->i_height; j++ )
1623 memcpy( p_dst, p_src, p_region->i_width );
1624 p_src += p_region->i_width;
1628 /* Check subtitles encoded as strings of characters
1629 * (since there are not rendered in the pixbuffer) */
1630 for( j = 0; j < p_region->i_object_defs; j++ )
1632 dvbsub_objectdef_t *p_object_def = &p_region->p_object_defs[j];
1634 if( ( p_object_def->i_type != 1 ) || !p_object_def->psz_text )
1637 /* Create new SPU region */
1638 memset( &fmt, 0, sizeof(video_format_t) );
1639 fmt.i_chroma = VLC_CODEC_TEXT;
1642 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1643 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1644 fmt.i_x_offset = fmt.i_y_offset = 0;
1645 p_spu_region = subpicture_region_New( &fmt );
1647 p_spu_region->psz_text = strdup( p_object_def->psz_text );
1648 p_spu_region->i_x = i_base_x + p_regiondef->i_x + p_object_def->i_x;
1649 p_spu_region->i_y = i_base_y + p_regiondef->i_y + p_object_def->i_y;
1650 p_spu_region->i_align = p_sys->i_spu_position;
1651 *pp_spu_region = p_spu_region;
1652 pp_spu_region = &p_spu_region->p_next;
1659 /*****************************************************************************
1660 * encoder_sys_t : encoder descriptor
1661 *****************************************************************************/
1662 typedef struct encoder_region_t
1669 struct encoder_sys_t
1671 unsigned int i_page_ver;
1672 unsigned int i_region_ver;
1673 unsigned int i_clut_ver;
1676 encoder_region_t *p_regions;
1680 /* subpicture positioning */
1686 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1687 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1688 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1689 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1691 /*****************************************************************************
1692 * OpenEncoder: probe the encoder and return score
1693 *****************************************************************************/
1694 static int OpenEncoder( vlc_object_t *p_this )
1696 encoder_t *p_enc = (encoder_t *)p_this;
1697 encoder_sys_t *p_sys;
1699 if( ( p_enc->fmt_out.i_codec != VLC_CODEC_DVBS ) &&
1702 return VLC_EGENERIC;
1705 /* Allocate the memory needed to store the decoder's structure */
1706 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1708 p_enc->p_sys = p_sys;
1710 p_enc->pf_encode_sub = Encode;
1711 p_enc->fmt_out.i_codec = VLC_CODEC_DVBS;
1712 p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1;
1714 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1716 p_sys->i_page_ver = 0;
1717 p_sys->i_region_ver = 0;
1718 p_sys->i_clut_ver = 0;
1719 p_sys->i_regions = 0;
1720 p_sys->p_regions = 0;
1722 p_sys->i_offset_x = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "x" );
1723 p_sys->i_offset_y = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "y" );
1728 /* FIXME: this routine is a hack to convert VLC_CODEC_YUVA
1729 * into VLC_CODEC_YUVP
1731 static subpicture_t *YuvaYuvp( subpicture_t *p_subpic )
1733 subpicture_region_t *p_region = NULL;
1735 if( !p_subpic ) return NULL;
1737 for( p_region = p_subpic->p_region; p_region; p_region = p_region->p_next )
1739 video_format_t *p_fmt = &p_region->fmt;
1740 int i = 0, j = 0, n = 0, p = 0;
1741 int i_max_entries = 256;
1743 #ifdef RANDOM_DITHERING
1744 int i_seed = 0xdeadbeef; /* random seed */
1748 int i_pixels = p_region->p_picture->p[0].i_visible_lines
1749 * p_region->p_picture->p[0].i_pitch;
1750 int i_iterator = p_region->p_picture->p[0].i_visible_lines * 3 / 4
1751 * p_region->p_picture->p[0].i_pitch
1752 + p_region->p_picture->p[0].i_pitch * 1 / 3;
1753 int i_tolerance = 0;
1755 #ifdef DEBUG_DVBSUB1
1756 /* p_enc not valid here */
1757 msg_Dbg( p_enc, "YuvaYuvp: i_pixels=%d, i_iterator=%d", i_pixels, i_iterator );
1759 p_fmt->i_chroma = VLC_CODEC_YUVP;
1760 p_fmt->p_palette = (video_palette_t *) malloc( sizeof( video_palette_t ) );
1761 if( !p_fmt->p_palette ) break;
1762 p_fmt->p_palette->i_entries = 0;
1764 /* Find best iterator using Euclide’s algorithm */
1765 for( ; i_iterator > 1 ; i_iterator-- )
1784 /* Count colors, build best palette */
1785 for( i_tolerance = 0; i_tolerance < 128; i_tolerance++ )
1787 bool b_success = true;
1788 p_fmt->p_palette->i_entries = 0;
1790 for( i = 0; i < i_pixels ; )
1793 y = p_region->p_picture->p[0].p_pixels[i];
1794 u = p_region->p_picture->p[1].p_pixels[i];
1795 v = p_region->p_picture->p[2].p_pixels[i];
1796 a = p_region->p_picture->p[3].p_pixels[i];
1797 for( j = 0; j < p_fmt->p_palette->i_entries; j++ )
1799 if( abs((int)p_fmt->p_palette->palette[j][0] - (int)y) <= i_tolerance &&
1800 abs((int)p_fmt->p_palette->palette[j][1] - (int)u) <= i_tolerance &&
1801 abs((int)p_fmt->p_palette->palette[j][2] - (int)v) <= i_tolerance &&
1802 abs((int)p_fmt->p_palette->palette[j][3] - (int)a) <= i_tolerance / 2 )
1807 if( j == p_fmt->p_palette->i_entries )
1809 p_fmt->p_palette->palette[j][0] = y;
1810 p_fmt->p_palette->palette[j][1] = u;
1811 p_fmt->p_palette->palette[j][2] = v;
1812 p_fmt->p_palette->palette[j][3] = a;
1813 p_fmt->p_palette->i_entries++;
1815 if( p_fmt->p_palette->i_entries >= i_max_entries )
1833 #ifdef DEBUG_DVBSUB1
1834 /* p_enc not valid here */
1835 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1838 #ifndef RANDOM_DITHERING
1839 pi_delta = xmalloc( ( p_region->p_picture->p[0].i_pitch + 1 )
1840 * sizeof(int) * 4 );
1841 for( i = 0; i < (p_region->p_picture->p[0].i_pitch + 1) * 4 ; i++ )
1847 /* Fill image with our new colours */
1848 for( p = 0; p < p_region->p_picture->p[0].i_visible_lines ; p++ )
1850 int i_ydelta = 0, i_udelta = 0, i_vdelta = 0, i_adelta = 0;
1852 for( n = 0; n < p_region->p_picture->p[0].i_pitch ; n++ )
1854 int i_offset = p * p_region->p_picture->p[0].i_pitch + n;
1856 int i_mindist, i_best;
1858 y = (int)p_region->p_picture->p[0].p_pixels[i_offset];
1859 u = (int)p_region->p_picture->p[1].p_pixels[i_offset];
1860 v = (int)p_region->p_picture->p[2].p_pixels[i_offset];
1861 a = (int)p_region->p_picture->p[3].p_pixels[i_offset];
1863 /* Add dithering compensation */
1864 #ifdef RANDOM_DITHERING
1865 y += ((i_seed & 0xff) - 0x80) * i_tolerance / 0x80;
1866 u += (((i_seed >> 8) & 0xff) - 0x80) * i_tolerance / 0x80;
1867 v += (((i_seed >> 16) & 0xff) - 0x80) * i_tolerance / 0x80;
1868 a += (((i_seed >> 24) & 0xff) - 0x80) * i_tolerance / 0x80;
1870 y += i_ydelta + pi_delta[ n * 4 ];
1871 u += i_udelta + pi_delta[ n * 4 + 1 ];
1872 v += i_vdelta + pi_delta[ n * 4 + 2 ];
1873 a += i_adelta + pi_delta[ n * 4 + 3 ];
1876 /* Find best colour in palette */
1877 for( i_mindist = 99999999, i_best = 0, j = 0; j < p_fmt->p_palette->i_entries; j++ )
1881 i_dist += abs((int)p_fmt->p_palette->palette[j][0] - y);
1882 i_dist += abs((int)p_fmt->p_palette->palette[j][1] - u);
1883 i_dist += abs((int)p_fmt->p_palette->palette[j][2] - v);
1884 i_dist += 2 * abs((int)p_fmt->p_palette->palette[j][3] - a);
1886 if( i_dist < i_mindist )
1893 /* Set pixel to best color */
1894 p_region->p_picture->p[0].p_pixels[i_offset] = i_best;
1896 /* Update dithering state */
1897 #ifdef RANDOM_DITHERING
1898 i_seed = (i_seed * 0x1283837) ^ 0x789479 ^ (i_seed >> 13);
1900 i_ydelta = y - (int)p_fmt->p_palette->palette[i_best][0];
1901 i_udelta = u - (int)p_fmt->p_palette->palette[i_best][1];
1902 i_vdelta = v - (int)p_fmt->p_palette->palette[i_best][2];
1903 i_adelta = a - (int)p_fmt->p_palette->palette[i_best][3];
1904 pi_delta[ n * 4 ] = i_ydelta * 3 / 8;
1905 pi_delta[ n * 4 + 1 ] = i_udelta * 3 / 8;
1906 pi_delta[ n * 4 + 2 ] = i_vdelta * 3 / 8;
1907 pi_delta[ n * 4 + 3 ] = i_adelta * 3 / 8;
1908 i_ydelta = i_ydelta * 5 / 8;
1909 i_udelta = i_udelta * 5 / 8;
1910 i_vdelta = i_vdelta * 5 / 8;
1911 i_adelta = i_adelta * 5 / 8;
1915 #ifndef RANDOM_DITHERING
1920 for( i = p_fmt->p_palette->i_entries; i < i_max_entries; i++ )
1922 p_fmt->p_palette->palette[i][0] = 0;
1923 p_fmt->p_palette->palette[i][1] = 0;
1924 p_fmt->p_palette->palette[i][2] = 0;
1925 p_fmt->p_palette->palette[i][3] = 0;
1927 p_fmt->p_palette->i_entries = i_max_entries;
1928 #ifdef DEBUG_DVBSUB1
1929 /* p_enc not valid here */
1930 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1936 /****************************************************************************
1937 * Encode: the whole thing
1938 ****************************************************************************/
1939 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1941 subpicture_t *p_temp = NULL;
1942 subpicture_region_t *p_region = NULL;
1943 bs_t bits, *s = &bits;
1946 if( !p_subpic || !p_subpic->p_region ) return NULL;
1948 /* FIXME: this is a hack to convert VLC_CODEC_YUVA into
1951 p_region = p_subpic->p_region;
1952 if( p_region->fmt.i_chroma == VLC_CODEC_YUVA )
1954 p_temp = YuvaYuvp( p_subpic );
1957 msg_Err( p_enc, "no picture in subpicture" );
1960 p_region = p_subpic->p_region;
1964 if( !p_region ) return NULL;
1966 if( ( p_region->fmt.i_chroma != VLC_CODEC_TEXT ) &&
1967 ( p_region->fmt.i_chroma != VLC_CODEC_YUVP ) )
1970 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
1971 vlc_fourcc_to_char( p_region->fmt.i_chroma, &psz_fourcc );
1972 msg_Err( p_enc, "chroma %4.4s not supported", psz_fourcc );
1976 if( p_region->fmt.p_palette )
1978 switch( p_region->fmt.p_palette->i_entries )
1986 msg_Err( p_enc, "subpicture palette (%d) not handled",
1987 p_region->fmt.p_palette->i_entries );
1994 msg_Dbg( p_enc, "encoding subpicture" );
1996 p_block = block_Alloc( 64000 );
1997 bs_init( s, p_block->p_buffer, p_block->i_buffer );
1999 bs_write( s, 8, 0x20 ); /* Data identifier */
2000 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
2002 encode_page_composition( p_enc, s, p_subpic );
2003 encode_region_composition( p_enc, s, p_subpic );
2004 encode_clut( p_enc, s, p_subpic );
2005 encode_object( p_enc, s, p_subpic );
2007 /* End of display */
2008 bs_write( s, 8, 0x0f ); /* Sync byte */
2009 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
2010 bs_write( s, 16, 1 ); /* Page id */
2011 bs_write( s, 16, 0 ); /* Segment length */
2013 bs_write( s, 8, 0xff );/* End marker */
2014 p_block->i_buffer = bs_pos( s ) / 8;
2015 p_block->i_pts = p_block->i_dts = p_subpic->i_start;
2016 if( !p_subpic->b_ephemer && ( p_subpic->i_stop > p_subpic->i_start ) )
2018 block_t *p_block_stop;
2020 p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
2022 /* Send another (empty) subtitle to signal the end of display */
2023 p_block_stop = block_Alloc( 64000 );
2024 bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
2025 bs_write( s, 8, 0x20 ); /* Data identifier */
2026 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
2027 encode_page_composition( p_enc, s, 0 );
2028 bs_write( s, 8, 0x0f ); /* Sync byte */
2029 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
2030 bs_write( s, 16, 1 ); /* Page id */
2031 bs_write( s, 16, 0 ); /* Segment length */
2032 bs_write( s, 8, 0xff );/* End marker */
2033 p_block_stop->i_buffer = bs_pos( s ) / 8;
2034 p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
2035 block_ChainAppend( &p_block, p_block_stop );
2036 p_block_stop->i_length = 100000; /* p_subpic->i_stop - p_subpic->i_start; */
2039 msg_Dbg( p_enc, "subpicture encoded properly" );
2044 /*****************************************************************************
2045 * CloseEncoder: encoder destruction
2046 *****************************************************************************/
2047 static void CloseEncoder( vlc_object_t *p_this )
2049 encoder_t *p_enc = (encoder_t *)p_this;
2050 encoder_sys_t *p_sys = p_enc->p_sys;
2052 var_Destroy( p_this , ENC_CFG_PREFIX "x" );
2053 var_Destroy( p_this , ENC_CFG_PREFIX "y" );
2054 var_Destroy( p_this , ENC_CFG_PREFIX "timeout" );
2056 if( p_sys->i_regions ) free( p_sys->p_regions );
2060 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
2061 subpicture_t *p_subpic )
2063 encoder_sys_t *p_sys = p_enc->p_sys;
2064 subpicture_region_t *p_region;
2065 bool b_mode_change = false;
2066 int i_regions, i_timeout;
2068 bs_write( s, 8, 0x0f ); /* Sync byte */
2069 bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
2070 bs_write( s, 16, 1 ); /* Page id */
2072 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2073 p_region; p_region = p_region->p_next, i_regions++ )
2075 if( i_regions >= p_sys->i_regions )
2077 encoder_region_t region;
2078 region.i_width = region.i_height = 0;
2079 p_sys->p_regions = xrealloc( p_sys->p_regions,
2080 sizeof(encoder_region_t) * (p_sys->i_regions + 1) );
2081 p_sys->p_regions[p_sys->i_regions++] = region;
2084 if( ( p_sys->p_regions[i_regions].i_width <
2085 (int)p_region->fmt.i_visible_width ) ||
2086 ( p_sys->p_regions[i_regions].i_width >
2087 (int)p_region->fmt.i_visible_width ) )
2089 b_mode_change = true;
2090 msg_Dbg( p_enc, "region %i width change: %i -> %i",
2091 i_regions, p_sys->p_regions[i_regions].i_width,
2092 p_region->fmt.i_visible_width );
2093 p_sys->p_regions[i_regions].i_width =
2094 p_region->fmt.i_visible_width;
2096 if( p_sys->p_regions[i_regions].i_height <
2097 (int)p_region->fmt.i_visible_height )
2099 b_mode_change = true;
2100 msg_Dbg( p_enc, "region %i height change: %i -> %i",
2101 i_regions, p_sys->p_regions[i_regions].i_height,
2102 p_region->fmt.i_visible_height );
2103 p_sys->p_regions[i_regions].i_height =
2104 p_region->fmt.i_visible_height;
2108 bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
2111 if( p_subpic && !p_subpic->b_ephemer &&
2112 ( p_subpic->i_stop > p_subpic->i_start ) )
2114 i_timeout = (p_subpic->i_stop - p_subpic->i_start) / 1000000;
2117 bs_write( s, 8, i_timeout ); /* Timeout */
2118 bs_write( s, 4, p_sys->i_page_ver++ );
2119 bs_write( s, 2, b_mode_change ?
2120 DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
2121 bs_write( s, 2, 0 ); /* Reserved */
2123 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2124 p_region; p_region = p_region->p_next, i_regions++ )
2126 bs_write( s, 8, i_regions );
2127 bs_write( s, 8, 0 ); /* Reserved */
2128 if( (p_sys->i_offset_x > 0) && (p_sys->i_offset_y > 0) )
2130 bs_write( s, 16, p_sys->i_offset_x ); /* override x position */
2131 bs_write( s, 16, p_sys->i_offset_y ); /* override y position */
2135 bs_write( s, 16, p_region->i_x );
2136 bs_write( s, 16, p_region->i_y );
2141 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2143 encoder_sys_t *p_sys = p_enc->p_sys;
2144 subpicture_region_t *p_region = p_subpic->p_region;
2145 video_palette_t *p_pal, pal;
2149 if( !p_region ) return;
2151 if( p_region->fmt.i_chroma == VLC_CODEC_YUVP )
2153 p_pal = p_region->fmt.p_palette;
2158 for( i = 0; i < 4; i++ )
2160 pal.palette[i][0] = 0;
2161 pal.palette[i][1] = 0;
2162 pal.palette[i][2] = 0;
2163 pal.palette[i][3] = 0;
2168 bs_write( s, 8, 0x0f ); /* Sync byte */
2169 bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
2170 bs_write( s, 16, 1 ); /* Page id */
2172 bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
2173 bs_write( s, 8, 1 ); /* Clut id */
2174 bs_write( s, 4, p_sys->i_clut_ver++ );
2175 bs_write( s, 4, 0 ); /* Reserved */
2177 for( i = 0; i < p_pal->i_entries; i++ )
2179 bs_write( s, 8, i ); /* Clut entry id */
2180 bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
2181 bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
2182 bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
2183 bs_write( s, 4, 0 ); /* Reserved */
2184 bs_write( s, 1, 1 ); /* Full range flag */
2185 bs_write( s, 8, p_pal->palette[i][3] ? /* Y value */
2186 (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
2187 bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
2188 bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
2189 bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
2193 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
2194 subpicture_t *p_subpic )
2196 encoder_sys_t *p_sys = p_enc->p_sys;
2197 subpicture_region_t *p_region;
2200 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2201 p_region = p_region->p_next, i_region++ )
2203 int i_entries = 4, i_depth = 0x1, i_bg = 0;
2205 ( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
2209 video_palette_t *p_pal = p_region->fmt.p_palette;
2213 msg_Err( p_enc, "subpicture has no palette - ignoring it" );
2217 i_entries = p_pal->i_entries;
2218 i_depth = i_entries == 4 ? 0x1 : i_entries == 16 ? 0x2 : 0x3;
2220 for( i_bg = 0; i_bg < p_pal->i_entries; i_bg++ )
2222 if( !p_pal->palette[i_bg][3] ) break;
2226 bs_write( s, 8, 0x0f ); /* Sync byte */
2227 bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
2228 bs_write( s, 16, 1 ); /* Page id */
2230 bs_write( s, 16, 10 + 6 + (b_text ? 2 : 0) ); /* Segment length */
2231 bs_write( s, 8, i_region );
2232 bs_write( s, 4, p_sys->i_region_ver++ );
2234 /* Region attributes */
2235 bs_write( s, 1, i_bg < i_entries ); /* Fill */
2236 bs_write( s, 3, 0 ); /* Reserved */
2237 bs_write( s, 16, p_sys->p_regions[i_region].i_width );
2238 bs_write( s, 16, p_sys->p_regions[i_region].i_height );
2239 bs_write( s, 3, i_depth ); /* Region level of compatibility */
2240 bs_write( s, 3, i_depth ); /* Region depth */
2241 bs_write( s, 2, 0 ); /* Reserved */
2242 bs_write( s, 8, 1 ); /* Clut id */
2243 bs_write( s, 8, i_bg ); /* region 8bit pixel code */
2244 bs_write( s, 4, i_bg ); /* region 4bit pixel code */
2245 bs_write( s, 2, i_bg ); /* region 2bit pixel code */
2246 bs_write( s, 2, 0 ); /* Reserved */
2248 /* In our implementation we only have 1 object per region */
2249 bs_write( s, 16, i_region );
2250 bs_write( s, 2, b_text ? DVBSUB_OT_BASIC_CHAR:DVBSUB_OT_BASIC_BITMAP );
2251 bs_write( s, 2, 0 ); /* object provider flag */
2252 bs_write( s, 12, 0 );/* object horizontal position */
2253 bs_write( s, 4, 0 ); /* Reserved */
2254 bs_write( s, 12, 0 );/* object vertical position */
2258 bs_write( s, 8, 1 ); /* foreground pixel code */
2259 bs_write( s, 8, 0 ); /* background pixel code */
2264 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2265 subpicture_region_t *p_region,
2268 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2270 encoder_sys_t *p_sys = p_enc->p_sys;
2271 subpicture_region_t *p_region;
2274 int i_length_pos, i_update_pos, i_pixel_data_pos;
2276 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2277 p_region = p_region->p_next, i_region++ )
2279 bs_write( s, 8, 0x0f ); /* Sync byte */
2280 bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
2281 bs_write( s, 16, 1 ); /* Page id */
2283 i_length_pos = bs_pos( s );
2284 bs_write( s, 16, 0 ); /* Segment length */
2285 bs_write( s, 16, i_region ); /* Object id */
2286 bs_write( s, 4, p_sys->i_region_ver++ );
2288 /* object coding method */
2289 switch( p_region->fmt.i_chroma )
2291 case VLC_CODEC_YUVP:
2292 bs_write( s, 2, 0 );
2294 case VLC_CODEC_TEXT:
2295 bs_write( s, 2, 1 );
2298 msg_Err( p_enc, "FOURCC %d not supported by encoder.", p_region->fmt.i_chroma );
2302 bs_write( s, 1, 0 ); /* non modifying color flag */
2303 bs_write( s, 1, 0 ); /* Reserved */
2305 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
2309 if( !p_region->psz_text ) continue;
2311 i_size = __MIN( strlen( p_region->psz_text ), 256 );
2313 bs_write( s, 8, i_size ); /* number of characters in string */
2314 for( i = 0; i < i_size; i++ )
2316 bs_write( s, 16, p_region->psz_text[i] );
2319 /* Update segment length */
2320 SetWBE( &s->p_start[i_length_pos/8],
2321 (bs_pos(s) - i_length_pos)/8 -2 );
2325 /* Coding of a bitmap object */
2326 i_update_pos = bs_pos( s );
2327 bs_write( s, 16, 0 ); /* topfield data block length */
2328 bs_write( s, 16, 0 ); /* bottomfield data block length */
2331 i_pixel_data_pos = bs_pos( s );
2332 encode_pixel_data( p_enc, s, p_region, true );
2333 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2334 SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
2337 i_pixel_data_pos = bs_pos( s );
2338 encode_pixel_data( p_enc, s, p_region, false );
2339 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2340 SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
2342 /* Stuffing for word alignment */
2344 if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
2346 /* Update segment length */
2347 SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
2351 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2353 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2355 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2357 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2358 subpicture_region_t *p_region,
2361 unsigned int i_line;
2364 if( p_region->fmt.i_chroma != VLC_CODEC_YUVP ) return;
2366 /* Encode line by line */
2367 for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
2370 switch( p_region->fmt.p_palette->i_entries )
2376 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
2377 encode_pixel_line_2bp( s, p_region, i_line );
2381 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
2382 encode_pixel_line_4bp( s, p_region, i_line );
2386 bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
2387 encode_pixel_line_8bp( s, p_region, i_line );
2391 msg_Err( p_enc, "subpicture palette (%i) not handled",
2392 p_region->fmt.p_palette->i_entries );
2396 bs_write( s, 8, 0xf0 ); /* End of object line code */
2400 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2403 unsigned int i, i_length = 0;
2404 int i_pitch = p_region->p_picture->p->i_pitch;
2405 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2406 int i_last_pixel = p_data[0];
2408 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2410 if( ( i != p_region->fmt.i_visible_width ) &&
2411 ( p_data[i] == i_last_pixel ) && ( i_length != 284 ) )
2417 if( ( i_length == 1 ) || ( i_length == 11 ) || ( i_length == 28 ) )
2419 /* 2bit/pixel code */
2421 bs_write( s, 2, i_last_pixel );
2424 bs_write( s, 2, 0 );
2425 bs_write( s, 1, 0 );
2426 bs_write( s, 1, 1 ); /* pseudo color 0 */
2435 bs_write( s, 2, i_last_pixel );
2436 bs_write( s, 2, i_last_pixel );
2440 bs_write( s, 2, 0 );
2441 bs_write( s, 1, 0 );
2442 bs_write( s, 1, 0 );
2443 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2446 else if( i_length > 2 )
2448 bs_write( s, 2, 0 );
2449 if( i_length <= 10 )
2451 bs_write( s, 1, 1 );
2452 bs_write( s, 3, i_length - 3 );
2453 bs_write( s, 2, i_last_pixel );
2457 bs_write( s, 1, 0 );
2458 bs_write( s, 1, 0 );
2460 if( i_length <= 27 )
2462 bs_write( s, 2, 2 );
2463 bs_write( s, 4, i_length - 12 );
2464 bs_write( s, 2, i_last_pixel );
2468 bs_write( s, 2, 3 );
2469 bs_write( s, 8, i_length - 29 );
2470 bs_write( s, 2, i_last_pixel );
2475 if( i == p_region->fmt.i_visible_width ) break;
2477 i_last_pixel = p_data[i];
2482 bs_write( s, 2, 0 );
2483 bs_write( s, 1, 0 );
2484 bs_write( s, 1, 0 );
2485 bs_write( s, 2, 0 );
2491 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2494 unsigned int i, i_length = 0;
2495 int i_pitch = p_region->p_picture->p->i_pitch;
2496 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2497 int i_last_pixel = p_data[0];
2499 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2501 if( i != p_region->fmt.i_visible_width &&
2502 p_data[i] == i_last_pixel && i_length != 280 )
2508 if( ( i_length == 1 ) ||
2509 ( ( i_length == 3 ) && i_last_pixel ) ||
2512 /* 4bit/pixel code */
2514 bs_write( s, 4, i_last_pixel );
2517 bs_write( s, 4, 0 );
2518 bs_write( s, 1, 1 );
2519 bs_write( s, 1, 1 );
2520 bs_write( s, 2, 0 ); /* pseudo color 0 */
2529 bs_write( s, 4, i_last_pixel );
2530 bs_write( s, 4, i_last_pixel );
2534 bs_write( s, 4, 0 );
2535 bs_write( s, 1, 1 );
2536 bs_write( s, 1, 1 );
2537 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2540 else if( !i_last_pixel && ( i_length >= 3 ) && ( i_length <= 9 ) )
2542 bs_write( s, 4, 0 );
2543 bs_write( s, 1, 0 );
2544 bs_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
2546 else if( i_length > 2 )
2548 bs_write( s, 4, 0 );
2549 bs_write( s, 1, 1 );
2553 bs_write( s, 1, 0 );
2554 bs_write( s, 2, i_length - 4 );
2555 bs_write( s, 4, i_last_pixel );
2559 bs_write( s, 1, 1 );
2561 if( i_length <= 24 )
2563 bs_write( s, 2, 2 );
2564 bs_write( s, 4, i_length - 9 );
2565 bs_write( s, 4, i_last_pixel );
2569 bs_write( s, 2, 3 );
2570 bs_write( s, 8, i_length - 25 );
2571 bs_write( s, 4, i_last_pixel );
2576 if( i == p_region->fmt.i_visible_width ) break;
2578 i_last_pixel = p_data[i];
2583 bs_write( s, 8, 0 );
2589 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2592 unsigned int i, i_length = 0;
2593 int i_pitch = p_region->p_picture->p->i_pitch;
2594 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2595 int i_last_pixel = p_data[0];
2597 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2599 if( ( i != p_region->fmt.i_visible_width ) &&
2600 ( p_data[i] == i_last_pixel ) && ( i_length != 127 ) )
2606 if( ( i_length == 1 ) && i_last_pixel )
2608 /* 8bit/pixel code */
2609 bs_write( s, 8, i_last_pixel );
2611 else if( ( i_length == 2 ) && i_last_pixel )
2613 /* 8bit/pixel code */
2614 bs_write( s, 8, i_last_pixel );
2615 bs_write( s, 8, i_last_pixel );
2617 else if( i_length <= 127 )
2619 bs_write( s, 8, 0 );
2623 bs_write( s, 1, 0 );
2624 bs_write( s, 7, i_length ); /* pseudo color 0 */
2628 bs_write( s, 1, 1 );
2629 bs_write( s, 7, i_length );
2630 bs_write( s, 8, i_last_pixel );
2634 if( i == p_region->fmt.i_visible_width ) break;
2636 i_last_pixel = p_data[i];
2641 bs_write( s, 8, 0 );
2642 bs_write( s, 8, 0 );
2650 static void default_dds_init( decoder_t * p_dec )
2652 decoder_sys_t *p_sys = p_dec->p_sys;
2654 /* see notes on DDS at the top of the file */
2656 /* configure for SD res in case DDS is not present */
2657 p_sys->display.i_version = 0xff; /* an invalid version so it's always different */
2658 p_sys->display.i_width = 720;
2659 p_sys->display.i_height = 576;
2660 p_sys->display.b_windowed = false;