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
** );
111 static void Flush( decoder_t
* );
114 static int OpenEncoder ( vlc_object_t
* );
115 static void CloseEncoder( vlc_object_t
* );
116 static block_t
*Encode ( encoder_t
*, subpicture_t
* );
120 # define DVBSUB_CFG_PREFIX "dvbsub-"
121 set_description( N_("DVB subtitles decoder") )
122 set_shortname( N_("DVB subtitles") )
123 set_capability( "decoder", 80 )
124 set_category( CAT_INPUT
)
125 set_subcategory( SUBCAT_INPUT_SCODEC
)
126 set_callbacks( Open
, Close
)
128 add_integer( DVBSUB_CFG_PREFIX
"position", 8, POS_TEXT
, POS_LONGTEXT
, true )
129 change_integer_list( pi_pos_values
, ppsz_pos_descriptions
)
130 add_integer( DVBSUB_CFG_PREFIX
"x", -1, POSX_TEXT
, POSX_LONGTEXT
, false )
131 add_integer( DVBSUB_CFG_PREFIX
"y", -1, POSY_TEXT
, POSY_LONGTEXT
, false )
134 # define ENC_CFG_PREFIX "sout-dvbsub-"
136 set_description( N_("DVB subtitles encoder") )
137 set_capability( "encoder", 100 )
138 set_callbacks( OpenEncoder
, CloseEncoder
)
140 add_integer( ENC_CFG_PREFIX
"x", -1, ENC_POSX_TEXT
, ENC_POSX_LONGTEXT
, false )
141 add_integer( ENC_CFG_PREFIX
"y", -1, ENC_POSY_TEXT
, ENC_POSY_LONGTEXT
, false )
145 static const char *const ppsz_enc_options
[] = { "x", "y", NULL
};
147 /****************************************************************************
149 ****************************************************************************
150 * Those structures refer closely to the ETSI 300 743 Object model
151 ****************************************************************************/
153 /* The object definition gives the position of the object in a region [7.2.5] */
154 typedef struct dvbsub_objectdef_s
162 char *psz_text
; /* for string of characters objects */
164 } dvbsub_objectdef_t
;
166 /* The entry in the palette CLUT */
176 /* The displays dimensions [7.2.1] */
177 typedef struct dvbsub_display_s
186 /* these values are only relevant if windowed */
195 typedef struct dvbsub_clut_s
199 dvbsub_color_t c_2b
[4];
200 dvbsub_color_t c_4b
[16];
201 dvbsub_color_t c_8b
[256];
203 struct dvbsub_clut_s
*p_next
;
207 /* The Region is an aera on the image [7.2.3]
208 * with a list of the object definitions associated and a CLUT */
209 typedef struct dvbsub_region_s
224 dvbsub_objectdef_t
*p_object_defs
;
226 struct dvbsub_region_s
*p_next
;
230 /* The object definition gives the position of the object in a region */
231 typedef struct dvbsub_regiondef_s
237 } dvbsub_regiondef_t
;
239 /* The page defines the list of regions [7.2.2] */
243 int i_timeout
; /* in seconds */
248 dvbsub_regiondef_t
*p_region_defs
;
256 /* Decoder internal data */
267 dvbsub_page_t
*p_page
;
268 dvbsub_region_t
*p_regions
;
269 dvbsub_clut_t
*p_cluts
;
270 /* this is very small, so keep forever */
271 dvbsub_display_t display
;
272 dvbsub_clut_t default_clut
;
276 /* List of different SEGMENT TYPES */
277 /* According to EN 300-743, table 2 */
278 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
279 #define DVBSUB_ST_REGION_COMPOSITION 0x11
280 #define DVBSUB_ST_CLUT_DEFINITION 0x12
281 #define DVBSUB_ST_OBJECT_DATA 0x13
282 #define DVBSUB_ST_DISPLAY_DEFINITION 0x14
283 #define DVBSUB_ST_ENDOFDISPLAY 0x80
284 #define DVBSUB_ST_STUFFING 0xff
285 /* List of different OBJECT TYPES */
286 /* According to EN 300-743, table 6 */
287 #define DVBSUB_OT_BASIC_BITMAP 0x00
288 #define DVBSUB_OT_BASIC_CHAR 0x01
289 #define DVBSUB_OT_COMPOSITE_STRING 0x02
290 /* Pixel DATA TYPES */
291 /* According to EN 300-743, table 9 */
292 #define DVBSUB_DT_2BP_CODE_STRING 0x10
293 #define DVBSUB_DT_4BP_CODE_STRING 0x11
294 #define DVBSUB_DT_8BP_CODE_STRING 0x12
295 #define DVBSUB_DT_24_TABLE_DATA 0x20
296 #define DVBSUB_DT_28_TABLE_DATA 0x21
297 #define DVBSUB_DT_48_TABLE_DATA 0x22
298 #define DVBSUB_DT_END_LINE 0xf0
299 /* List of different Page Composition Segment state */
300 /* According to EN 300-743, 7.2.1 table 3 */
301 #define DVBSUB_PCS_STATE_ACQUISITION 0x01
302 #define DVBSUB_PCS_STATE_CHANGE 0x02
304 /*****************************************************************************
306 *****************************************************************************/
307 static void decode_segment( decoder_t
*, bs_t
* );
308 static void decode_page_composition( decoder_t
*, bs_t
* );
309 static void decode_region_composition( decoder_t
*, bs_t
* );
310 static void decode_object( decoder_t
*, bs_t
* );
311 static void decode_display_definition( decoder_t
*, bs_t
* );
312 static void decode_clut( decoder_t
*, bs_t
* );
313 static void free_all( decoder_t
* );
315 static void default_clut_init( decoder_t
* );
316 static void default_dds_init( decoder_t
* );
318 static subpicture_t
*render( decoder_t
* );
320 /*****************************************************************************
321 * Open: probe the decoder and return score
322 *****************************************************************************
323 * Tries to launch a decoder and return score so that the interface is able
325 *****************************************************************************/
326 static int Open( vlc_object_t
*p_this
)
328 decoder_t
*p_dec
= (decoder_t
*) p_this
;
329 decoder_sys_t
*p_sys
;
332 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_DVBS
)
337 p_dec
->pf_decode_sub
= Decode
;
338 p_dec
->pf_flush
= Flush
;
339 p_sys
= p_dec
->p_sys
= calloc( 1, sizeof(decoder_sys_t
) );
343 p_sys
->i_pts
= VLC_TS_INVALID
;
344 p_sys
->i_id
= p_dec
->fmt_in
.subs
.dvb
.i_id
& 0xFFFF;
345 p_sys
->i_ancillary_id
= p_dec
->fmt_in
.subs
.dvb
.i_id
>> 16;
347 p_sys
->p_regions
= NULL
;
348 p_sys
->p_cluts
= NULL
;
349 p_sys
->p_page
= NULL
;
351 /* configure for SD res in case DDS is not present */
352 default_dds_init( p_dec
);
354 p_sys
->i_spu_position
= var_CreateGetInteger( p_this
,
355 DVBSUB_CFG_PREFIX
"position" );
356 i_posx
= var_CreateGetInteger( p_this
, DVBSUB_CFG_PREFIX
"x" );
357 i_posy
= var_CreateGetInteger( p_this
, DVBSUB_CFG_PREFIX
"y" );
359 /* Check if subpicture position was overridden */
360 p_sys
->b_absolute
= true;
361 p_sys
->i_spu_x
= p_sys
->i_spu_y
= 0;
363 if( ( i_posx
>= 0 ) && ( i_posy
>= 0 ) )
365 p_sys
->b_absolute
= true;
366 p_sys
->i_spu_x
= i_posx
;
367 p_sys
->i_spu_y
= i_posy
;
370 p_dec
->fmt_out
.i_cat
= SPU_ES
;
371 p_dec
->fmt_out
.i_codec
= 0;
373 default_clut_init( p_dec
);
378 /*****************************************************************************
380 *****************************************************************************/
381 static void Close( vlc_object_t
*p_this
)
383 decoder_t
*p_dec
= (decoder_t
*) p_this
;
384 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
386 var_Destroy( p_this
, DVBSUB_CFG_PREFIX
"x" );
387 var_Destroy( p_this
, DVBSUB_CFG_PREFIX
"y" );
388 var_Destroy( p_this
, DVBSUB_CFG_PREFIX
"position" );
394 /*****************************************************************************
396 *****************************************************************************/
397 static void Flush( decoder_t
*p_dec
)
399 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
401 p_sys
->i_pts
= VLC_TS_INVALID
;
404 /*****************************************************************************
406 *****************************************************************************/
407 static subpicture_t
*Decode( decoder_t
*p_dec
, block_t
**pp_block
)
409 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
411 subpicture_t
*p_spu
= NULL
;
413 if( ( pp_block
== NULL
) || ( *pp_block
== NULL
) ) return NULL
;
417 if( p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
| BLOCK_FLAG_CORRUPTED
) )
420 if( p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
422 block_Release( p_block
);
427 /* configure for SD res in case DDS is not present */
428 /* a change of PTS is a good indication we must get a new DDS */
429 if( p_sys
->i_pts
!= p_block
->i_pts
)
430 default_dds_init( p_dec
);
432 p_sys
->i_pts
= p_block
->i_pts
;
433 if( p_sys
->i_pts
<= VLC_TS_INVALID
)
436 /* Some DVB channels send stuffing segments in non-dated packets so
437 * don't complain too loudly. */
438 msg_Warn( p_dec
, "non dated subtitle" );
440 block_Release( p_block
);
444 bs_init( &p_sys
->bs
, p_block
->p_buffer
, p_block
->i_buffer
);
446 if( bs_read( &p_sys
->bs
, 8 ) != 0x20 ) /* Data identifier */
448 msg_Dbg( p_dec
, "invalid data identifier" );
449 block_Release( p_block
);
453 if( bs_read( &p_sys
->bs
, 8 ) ) /* Subtitle stream id */
455 msg_Dbg( p_dec
, "invalid subtitle stream id" );
456 block_Release( p_block
);
461 msg_Dbg( p_dec
, "subtitle packet received: %"PRId64
, p_sys
->i_pts
);
464 p_sys
->b_page
= false;
465 while( bs_show( &p_sys
->bs
, 8 ) == 0x0f ) /* Sync byte */
467 decode_segment( p_dec
, &p_sys
->bs
);
470 if( ( bs_read( &p_sys
->bs
, 8 ) & 0x3f ) != 0x3f ) /* End marker */
472 msg_Warn( p_dec
, "end marker not found (corrupted subtitle ?)" );
473 block_Release( p_block
);
477 /* Check if the page is to be displayed */
478 if( p_sys
->p_page
&& p_sys
->b_page
)
479 p_spu
= render( p_dec
);
481 block_Release( p_block
);
486 /* following functions are local */
488 /*****************************************************************************
489 * default_clut_init: default clut as defined in EN 300-743 section 10
490 *****************************************************************************/
491 static void default_clut_init( decoder_t
*p_dec
)
493 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
496 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
497 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
498 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
501 for( i
= 0; i
< 4; i
++ )
503 uint8_t R
= 0, G
= 0, B
= 0, T
= 0;
505 if( !(i
& 0x2) && !(i
& 0x1) ) T
= 0xFF;
506 else if( !(i
& 0x2) && (i
& 0x1) ) R
= G
= B
= 0xFF;
507 else if( (i
& 0x2) && !(i
& 0x1) ) R
= G
= B
= 0;
508 else R
= G
= B
= 0x7F;
510 p_sys
->default_clut
.c_2b
[i
].Y
= RGB_TO_Y(R
,G
,B
);
511 p_sys
->default_clut
.c_2b
[i
].Cb
= RGB_TO_V(R
,G
,B
);
512 p_sys
->default_clut
.c_2b
[i
].Cr
= RGB_TO_U(R
,G
,B
);
513 p_sys
->default_clut
.c_2b
[i
].T
= T
;
516 /* 16 entries CLUT */
517 for( i
= 0; i
< 16; i
++ )
519 uint8_t R
= 0, G
= 0, B
= 0, T
= 0;
523 if( !(i
& 0x4) && !(i
& 0x2) && !(i
& 0x1) )
529 R
= (i
& 0x1) ? 0xFF : 0;
530 G
= (i
& 0x2) ? 0xFF : 0;
531 B
= (i
& 0x4) ? 0xFF : 0;
536 R
= (i
& 0x1) ? 0x7F : 0;
537 G
= (i
& 0x2) ? 0x7F : 0;
538 B
= (i
& 0x4) ? 0x7F : 0;
541 p_sys
->default_clut
.c_4b
[i
].Y
= RGB_TO_Y(R
,G
,B
);
542 p_sys
->default_clut
.c_4b
[i
].Cr
= RGB_TO_V(R
,G
,B
);
543 p_sys
->default_clut
.c_4b
[i
].Cb
= RGB_TO_U(R
,G
,B
);
544 p_sys
->default_clut
.c_4b
[i
].T
= T
;
547 /* 256 entries CLUT */
548 memset( p_sys
->default_clut
.c_8b
, 0xFF, 256 * sizeof(dvbsub_color_t
) );
551 static void decode_segment( decoder_t
*p_dec
, bs_t
*s
)
553 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
558 /* sync_byte (already checked) */
562 i_type
= bs_read( s
, 8 );
565 i_page_id
= bs_read( s
, 16 );
568 i_size
= bs_show( s
, 16 );
570 if( ( i_page_id
!= p_sys
->i_id
) &&
571 ( i_page_id
!= p_sys
->i_ancillary_id
) )
574 msg_Dbg( p_dec
, "subtitle skipped (page id: %i, %i)",
575 i_page_id
, p_sys
->i_id
);
577 bs_skip( s
, 8 * ( 2 + i_size
) );
581 if( ( p_sys
->i_ancillary_id
!= p_sys
->i_id
) &&
582 ( i_type
== DVBSUB_ST_PAGE_COMPOSITION
) &&
583 ( i_page_id
== p_sys
->i_ancillary_id
) )
586 msg_Dbg( p_dec
, "skipped invalid ancillary subtitle packet" );
588 bs_skip( s
, 8 * ( 2 + i_size
) );
593 if( i_page_id
== p_sys
->i_id
)
594 msg_Dbg( p_dec
, "segment (id: %i)", i_page_id
);
596 msg_Dbg( p_dec
, "ancillary segment (id: %i)", i_page_id
);
601 case DVBSUB_ST_PAGE_COMPOSITION
:
603 msg_Dbg( p_dec
, "decode_page_composition" );
605 decode_page_composition( p_dec
, s
);
608 case DVBSUB_ST_REGION_COMPOSITION
:
610 msg_Dbg( p_dec
, "decode_region_composition" );
612 decode_region_composition( p_dec
, s
);
615 case DVBSUB_ST_CLUT_DEFINITION
:
617 msg_Dbg( p_dec
, "decode_clut" );
619 decode_clut( p_dec
, s
);
622 case DVBSUB_ST_OBJECT_DATA
:
624 msg_Dbg( p_dec
, "decode_object" );
626 decode_object( p_dec
, s
);
629 case DVBSUB_ST_DISPLAY_DEFINITION
:
631 msg_Dbg( p_dec
, "decode_display_definition" );
633 decode_display_definition( p_dec
, s
);
636 case DVBSUB_ST_ENDOFDISPLAY
:
638 msg_Dbg( p_dec
, "end of display" );
640 bs_skip( s
, 8 * ( 2 + i_size
) );
643 case DVBSUB_ST_STUFFING
:
645 msg_Dbg( p_dec
, "skip stuffing" );
647 bs_skip( s
, 8 * ( 2 + i_size
) );
651 msg_Warn( p_dec
, "unsupported segment type: (%04x)", i_type
);
652 bs_skip( s
, 8 * ( 2 + i_size
) );
657 static void decode_clut( decoder_t
*p_dec
, bs_t
*s
)
659 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
660 uint16_t i_segment_length
;
661 uint16_t i_processed_length
;
662 dvbsub_clut_t
*p_clut
, *p_next
;
665 i_segment_length
= bs_read( s
, 16 );
666 i_id
= bs_read( s
, 8 );
667 i_version
= bs_read( s
, 4 );
669 /* Check if we already have this clut */
670 for( p_clut
= p_sys
->p_cluts
; p_clut
!= NULL
; p_clut
= p_clut
->p_next
)
672 if( p_clut
->i_id
== i_id
) break;
675 /* Check version number */
676 if( p_clut
&& ( p_clut
->i_version
== i_version
) )
679 bs_skip( s
, 8 * i_segment_length
- 12 );
686 msg_Dbg( p_dec
, "new clut: %i", i_id
);
688 p_clut
= malloc( sizeof( dvbsub_clut_t
) );
691 p_clut
->p_next
= p_sys
->p_cluts
;
692 p_sys
->p_cluts
= p_clut
;
695 /* Initialize to default clut */
696 p_next
= p_clut
->p_next
;
697 *p_clut
= p_sys
->default_clut
;
698 p_clut
->p_next
= p_next
;
700 /* We don't have this version of the CLUT: Parse it */
701 p_clut
->i_version
= i_version
;
703 bs_skip( s
, 4 ); /* Reserved bits */
704 i_processed_length
= 2;
705 while( i_processed_length
< i_segment_length
)
707 uint8_t y
, cb
, cr
, t
;
711 i_id
= bs_read( s
, 8 );
712 i_type
= bs_read( s
, 3 );
716 if( bs_read( s
, 1 ) )
719 cr
= bs_read( s
, 8 );
720 cb
= bs_read( s
, 8 );
722 i_processed_length
+= 6;
726 y
= bs_read( s
, 6 ) << 2;
727 cr
= bs_read( s
, 4 ) << 4;
728 cb
= bs_read( s
, 4 ) << 4;
729 t
= bs_read( s
, 2 ) << 6;
730 i_processed_length
+= 4;
733 /* We are not entirely compliant here as full transparency is indicated
734 * with a luma value of zero, not a transparency value of 0xff
735 * (full transparency would actually be 0xff + 1). */
742 /* According to EN 300-743 section 7.2.3 note 1, type should
743 * not have more than 1 bit set to one, but some streams don't
744 * respect this note. */
745 if( ( i_type
& 0x04 ) && ( i_id
< 4 ) )
747 p_clut
->c_2b
[i_id
].Y
= y
;
748 p_clut
->c_2b
[i_id
].Cr
= cr
;
749 p_clut
->c_2b
[i_id
].Cb
= cb
;
750 p_clut
->c_2b
[i_id
].T
= t
;
752 if( ( i_type
& 0x02 ) && ( i_id
< 16 ) )
754 p_clut
->c_4b
[i_id
].Y
= y
;
755 p_clut
->c_4b
[i_id
].Cr
= cr
;
756 p_clut
->c_4b
[i_id
].Cb
= cb
;
757 p_clut
->c_4b
[i_id
].T
= t
;
761 p_clut
->c_8b
[i_id
].Y
= y
;
762 p_clut
->c_8b
[i_id
].Cr
= cr
;
763 p_clut
->c_8b
[i_id
].Cb
= cb
;
764 p_clut
->c_8b
[i_id
].T
= t
;
769 static void decode_page_composition( decoder_t
*p_dec
, bs_t
*s
)
771 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
772 int i_version
, i_state
, i_segment_length
, i_timeout
, i
;
774 /* A page is composed by 0 or more region */
775 i_segment_length
= bs_read( s
, 16 );
776 i_timeout
= bs_read( s
, 8 );
777 i_version
= bs_read( s
, 4 );
778 i_state
= bs_read( s
, 2 );
779 bs_skip( s
, 2 ); /* Reserved */
781 if( i_state
== DVBSUB_PCS_STATE_CHANGE
)
783 /* End of an epoch, reset decoder buffer */
785 msg_Dbg( p_dec
, "page composition mode change" );
789 else if( !p_sys
->p_page
&& ( i_state
!= DVBSUB_PCS_STATE_ACQUISITION
) &&
790 ( i_state
!= DVBSUB_PCS_STATE_CHANGE
) )
792 /* Not a full PCS, we need to wait for one */
793 msg_Dbg( p_dec
, "didn't receive an acquisition page yet" );
796 /* Try to start decoding even without an acquisition page */
797 bs_skip( s
, 8 * (i_segment_length
- 2) );
803 if( i_state
== DVBSUB_PCS_STATE_ACQUISITION
)
804 msg_Dbg( p_dec
, "acquisition page composition" );
807 /* Check version number */
808 if( p_sys
->p_page
&& ( p_sys
->p_page
->i_version
== i_version
) )
810 bs_skip( s
, 8 * (i_segment_length
- 2) );
813 else if( p_sys
->p_page
)
815 if( p_sys
->p_page
->i_region_defs
)
816 free( p_sys
->p_page
->p_region_defs
);
817 p_sys
->p_page
->p_region_defs
= NULL
;
818 p_sys
->p_page
->i_region_defs
= 0;
824 msg_Dbg( p_dec
, "new page" );
826 /* Allocate a new page */
827 p_sys
->p_page
= malloc( sizeof(dvbsub_page_t
) );
832 p_sys
->p_page
->i_version
= i_version
;
833 p_sys
->p_page
->i_timeout
= i_timeout
;
834 p_sys
->b_page
= true;
836 /* Number of regions */
837 p_sys
->p_page
->i_region_defs
= (i_segment_length
- 2) / 6;
839 if( p_sys
->p_page
->i_region_defs
== 0 ) return;
841 p_sys
->p_page
->p_region_defs
=
842 malloc( p_sys
->p_page
->i_region_defs
* sizeof(dvbsub_regiondef_t
) );
843 if( p_sys
->p_page
->p_region_defs
)
845 for( i
= 0; i
< p_sys
->p_page
->i_region_defs
; i
++ )
847 p_sys
->p_page
->p_region_defs
[i
].i_id
= bs_read( s
, 8 );
848 bs_skip( s
, 8 ); /* Reserved */
849 p_sys
->p_page
->p_region_defs
[i
].i_x
= bs_read( s
, 16 );
850 p_sys
->p_page
->p_region_defs
[i
].i_y
= bs_read( s
, 16 );
853 msg_Dbg( p_dec
, "page_composition, region %i (%i,%i)",
854 i
, p_sys
->p_page
->p_region_defs
[i
].i_x
,
855 p_sys
->p_page
->p_region_defs
[i
].i_y
);
861 static void decode_region_composition( decoder_t
*p_dec
, bs_t
*s
)
863 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
864 dvbsub_region_t
*p_region
, **pp_region
= &p_sys
->p_regions
;
865 int i_segment_length
, i_processed_length
, i_id
, i_version
;
866 int i_width
, i_height
, i_level_comp
, i_depth
, i_clut
;
867 int i_8_bg
, i_4_bg
, i_2_bg
;
870 i_segment_length
= bs_read( s
, 16 );
871 i_id
= bs_read( s
, 8 );
872 i_version
= bs_read( s
, 4 );
874 /* Check if we already have this region */
875 for( p_region
= p_sys
->p_regions
; p_region
!= NULL
;
876 p_region
= p_region
->p_next
)
878 pp_region
= &p_region
->p_next
;
879 if( p_region
->i_id
== i_id
) break;
882 /* Check version number */
883 if( p_region
&& ( p_region
->i_version
== i_version
) )
885 bs_skip( s
, 8 * (i_segment_length
- 1) - 4 );
892 msg_Dbg( p_dec
, "new region: %i", i_id
);
894 p_region
= *pp_region
= calloc( 1, sizeof(dvbsub_region_t
) );
897 p_region
->p_object_defs
= NULL
;
898 p_region
->p_pixbuf
= NULL
;
899 p_region
->p_next
= NULL
;
902 /* Region attributes */
903 p_region
->i_id
= i_id
;
904 p_region
->i_version
= i_version
;
905 b_fill
= bs_read( s
, 1 );
906 bs_skip( s
, 3 ); /* Reserved */
908 i_width
= bs_read( s
, 16 );
909 i_height
= bs_read( s
, 16 );
911 msg_Dbg( p_dec
, " width=%d height=%d", i_width
, i_height
);
913 i_level_comp
= bs_read( s
, 3 );
914 i_depth
= bs_read( s
, 3 );
915 bs_skip( s
, 2 ); /* Reserved */
916 i_clut
= bs_read( s
, 8 );
918 i_8_bg
= bs_read( s
, 8 );
919 i_4_bg
= bs_read( s
, 4 );
920 i_2_bg
= bs_read( s
, 2 );
921 bs_skip( s
, 2 ); /* Reserved */
923 /* Free old object defs */
924 while( p_region
->i_object_defs
)
925 free( p_region
->p_object_defs
[--p_region
->i_object_defs
].psz_text
);
927 free( p_region
->p_object_defs
);
928 p_region
->p_object_defs
= NULL
;
930 /* Extra sanity checks */
931 if( ( p_region
->i_width
!= i_width
) ||
932 ( p_region
->i_height
!= i_height
) )
934 if( p_region
->p_pixbuf
)
936 msg_Dbg( p_dec
, "region size changed (%dx%d->%dx%d)",
937 p_region
->i_width
, p_region
->i_height
, i_width
, i_height
);
938 free( p_region
->p_pixbuf
);
941 p_region
->p_pixbuf
= xmalloc( i_height
* i_width
);
942 p_region
->i_depth
= 0;
945 if( p_region
->i_depth
&&
946 ( ( p_region
->i_depth
!= i_depth
) ||
947 ( p_region
->i_level_comp
!= i_level_comp
) ||
948 ( p_region
->i_clut
!= i_clut
) ) )
950 msg_Dbg( p_dec
, "region parameters changed (not allowed)" );
953 /* Erase background of region */
956 int i_background
= ( p_region
->i_depth
== 1 ) ? i_2_bg
:
957 ( ( p_region
->i_depth
== 2 ) ? i_4_bg
: i_8_bg
);
958 memset( p_region
->p_pixbuf
, i_background
, i_width
* i_height
);
961 p_region
->i_width
= i_width
;
962 p_region
->i_height
= i_height
;
963 p_region
->i_level_comp
= i_level_comp
;
964 p_region
->i_depth
= i_depth
;
965 p_region
->i_clut
= i_clut
;
967 /* List of objects in the region */
968 i_processed_length
= 10;
969 while( i_processed_length
< i_segment_length
)
971 dvbsub_objectdef_t
*p_obj
;
973 /* We create a new object */
974 p_region
->i_object_defs
++;
975 p_region
->p_object_defs
= xrealloc( p_region
->p_object_defs
,
976 sizeof(dvbsub_objectdef_t
) * p_region
->i_object_defs
);
978 /* We parse object properties */
979 p_obj
= &p_region
->p_object_defs
[p_region
->i_object_defs
- 1];
980 p_obj
->i_id
= bs_read( s
, 16 );
981 p_obj
->i_type
= bs_read( s
, 2 );
982 bs_skip( s
, 2 ); /* Provider */
983 p_obj
->i_x
= bs_read( s
, 12 );
984 bs_skip( s
, 4 ); /* Reserved */
985 p_obj
->i_y
= bs_read( s
, 12 );
986 p_obj
->psz_text
= NULL
;
988 i_processed_length
+= 6;
990 if( ( p_obj
->i_type
== DVBSUB_OT_BASIC_CHAR
) ||
991 ( p_obj
->i_type
== DVBSUB_OT_COMPOSITE_STRING
) )
993 p_obj
->i_fg_pc
= bs_read( s
, 8 );
994 p_obj
->i_bg_pc
= bs_read( s
, 8 );
995 i_processed_length
+= 2;
1000 /* ETSI 300 743 [7.2.1] */
1001 static void decode_display_definition( decoder_t
*p_dec
, bs_t
*s
)
1003 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1004 uint16_t i_segment_length
;
1005 uint16_t i_processed_length
= 40;
1008 i_segment_length
= bs_read( s
, 16 );
1009 i_version
= bs_read( s
, 4 );
1011 /* Check version number */
1012 if( p_sys
->display
.i_version
== i_version
)
1014 /* The definition did not change */
1015 bs_skip( s
, 8*i_segment_length
- 4 );
1020 msg_Dbg( p_dec
, "new display definition: %i", i_version
);
1023 /* We don't have this version of the display definition: Parse it */
1024 p_sys
->display
.i_version
= i_version
;
1025 p_sys
->display
.b_windowed
= bs_read( s
, 1 );
1026 bs_skip( s
, 3 ); /* Reserved bits */
1027 p_sys
->display
.i_width
= bs_read( s
, 16 )+1;
1028 p_sys
->display
.i_height
= bs_read( s
, 16 )+1;
1030 if( p_sys
->display
.b_windowed
)
1033 msg_Dbg( p_dec
, "display definition with offsets (windowed)" );
1035 /* Coordinates are measured from the top left corner */
1036 p_sys
->display
.i_x
= bs_read( s
, 16 );
1037 p_sys
->display
.i_max_x
= bs_read( s
, 16 );
1038 p_sys
->display
.i_y
= bs_read( s
, 16 );
1039 p_sys
->display
.i_max_y
= bs_read( s
, 16 );
1040 i_processed_length
+= 64;
1044 /* if not windowed, setup the window variables to good defaults */
1045 /* not necessary, but to avoid future confusion.. */
1046 p_sys
->display
.i_x
= 0;
1047 p_sys
->display
.i_max_x
= p_sys
->display
.i_width
-1;
1048 p_sys
->display
.i_y
= 0;
1049 p_sys
->display
.i_max_y
= p_sys
->display
.i_height
-1;
1052 if( i_processed_length
!= i_segment_length
*8 )
1054 msg_Err( p_dec
, "processed length %d bytes != segment length %d bytes",
1055 i_processed_length
/ 8 , i_segment_length
);
1059 msg_Dbg( p_dec
, "version: %d, width: %d, height: %d",
1060 p_sys
->display
.i_version
, p_sys
->display
.i_width
, p_sys
->display
.i_height
);
1061 if( p_sys
->display
.b_windowed
)
1062 msg_Dbg( p_dec
, "xmin: %d, xmax: %d, ymin: %d, ymax: %d",
1063 p_sys
->display
.i_x
, p_sys
->display
.i_max_x
, p_sys
->display
.i_y
, p_sys
->display
.i_max_y
);
1067 static void dvbsub_render_pdata( decoder_t
*, dvbsub_region_t
*, int, int,
1069 static void dvbsub_pdata2bpp( bs_t
*, uint8_t *, int, int * );
1070 static void dvbsub_pdata4bpp( bs_t
*, uint8_t *, int, int * );
1071 static void dvbsub_pdata8bpp( bs_t
*, uint8_t *, int, int * );
1073 static void decode_object( decoder_t
*p_dec
, bs_t
*s
)
1075 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1076 dvbsub_region_t
*p_region
;
1077 int i_segment_length
, i_coding_method
, i_id
, i
;
1079 /* ETSI 300-743 paragraph 7.2.4
1080 * sync_byte, segment_type and page_id have already been processed.
1082 i_segment_length
= bs_read( s
, 16 );
1083 i_id
= bs_read( s
, 16 );
1084 bs_skip( s
, 4 ); /* version */
1085 i_coding_method
= bs_read( s
, 2 );
1087 if( i_coding_method
> 1 )
1089 msg_Dbg( p_dec
, "unknown DVB subtitling coding %d is not handled!", i_coding_method
);
1090 bs_skip( s
, 8 * (i_segment_length
- 2) - 6 );
1094 /* Check if the object needs to be rendered in at least one
1096 for( p_region
= p_sys
->p_regions
; p_region
!= NULL
;
1097 p_region
= p_region
->p_next
)
1099 for( i
= 0; i
< p_region
->i_object_defs
; i
++ )
1100 if( p_region
->p_object_defs
[i
].i_id
== i_id
) break;
1102 if( i
!= p_region
->i_object_defs
) break;
1106 bs_skip( s
, 8 * (i_segment_length
- 2) - 6 );
1111 msg_Dbg( p_dec
, "new object: %i", i_id
);
1114 bs_skip( s
, 1 ); /* non_modify_color */
1115 bs_skip( s
, 1 ); /* Reserved */
1117 if( i_coding_method
== 0x00 )
1119 int i_topfield
, i_bottomfield
;
1120 uint8_t *p_topfield
, *p_bottomfield
;
1122 i_topfield
= bs_read( s
, 16 );
1123 i_bottomfield
= bs_read( s
, 16 );
1124 p_topfield
= s
->p_start
+ bs_pos( s
) / 8;
1125 p_bottomfield
= p_topfield
+ i_topfield
;
1127 bs_skip( s
, 8 * (i_segment_length
- 7) );
1130 if( ( i_segment_length
< ( i_topfield
+ i_bottomfield
+ 7 ) ) ||
1131 ( ( p_topfield
+ i_topfield
+ i_bottomfield
) > s
->p_end
) )
1133 msg_Dbg( p_dec
, "corrupted object data" );
1137 for( p_region
= p_sys
->p_regions
; p_region
!= NULL
;
1138 p_region
= p_region
->p_next
)
1140 for( i
= 0; i
< p_region
->i_object_defs
; i
++ )
1142 if( p_region
->p_object_defs
[i
].i_id
!= i_id
) continue;
1144 dvbsub_render_pdata( p_dec
, p_region
,
1145 p_region
->p_object_defs
[i
].i_x
,
1146 p_region
->p_object_defs
[i
].i_y
,
1147 p_topfield
, i_topfield
);
1151 dvbsub_render_pdata( p_dec
, p_region
,
1152 p_region
->p_object_defs
[i
].i_x
,
1153 p_region
->p_object_defs
[i
].i_y
+ 1,
1154 p_bottomfield
, i_bottomfield
);
1158 /* Duplicate the top field */
1159 dvbsub_render_pdata( p_dec
, p_region
,
1160 p_region
->p_object_defs
[i
].i_x
,
1161 p_region
->p_object_defs
[i
].i_y
+ 1,
1162 p_topfield
, i_topfield
);
1169 /* DVB subtitling as characters */
1170 int i_number_of_codes
= bs_read( s
, 8 );
1171 uint8_t* p_start
= s
->p_start
+ bs_pos( s
) / 8;
1174 if( ( i_segment_length
< ( i_number_of_codes
*2 + 4 ) ) ||
1175 ( ( p_start
+ i_number_of_codes
*2 ) > s
->p_end
) )
1177 msg_Dbg( p_dec
, "corrupted object data" );
1181 for( p_region
= p_sys
->p_regions
; p_region
!= NULL
;
1182 p_region
= p_region
->p_next
)
1184 for( i
= 0; i
< p_region
->i_object_defs
; i
++ )
1188 if( p_region
->p_object_defs
[i
].i_id
!= i_id
) continue;
1190 p_region
->p_object_defs
[i
].psz_text
=
1191 xrealloc( p_region
->p_object_defs
[i
].psz_text
,
1192 i_number_of_codes
+ 1 );
1194 /* FIXME 16bits -> char ??? See Preamble */
1195 for( j
= 0; j
< i_number_of_codes
; j
++ )
1197 p_region
->p_object_defs
[i
].psz_text
[j
] = (char)(bs_read( s
, 16 ) & 0xFF);
1199 /* Null terminate the string */
1200 p_region
->p_object_defs
[i
].psz_text
[j
] = 0;
1206 msg_Dbg( p_dec
, "end object: %i", i_id
);
1210 static void dvbsub_render_pdata( decoder_t
*p_dec
, dvbsub_region_t
*p_region
,
1212 uint8_t *p_field
, int i_field
)
1219 if( !p_region
->p_pixbuf
)
1221 msg_Err( p_dec
, "region %i has no pixel buffer!", p_region
->i_id
);
1224 if( i_y
< 0 || i_x
< 0 || i_y
>= p_region
->i_height
||
1225 i_x
>= p_region
->i_width
)
1227 msg_Dbg( p_dec
, "invalid offset (%i,%i)", i_x
, i_y
);
1231 p_pixbuf
= p_region
->p_pixbuf
+ i_y
* p_region
->i_width
;
1232 bs_init( &bs
, p_field
, i_field
);
1234 while( !bs_eof( &bs
) )
1237 if( i_y
>= p_region
->i_height
) return;
1239 switch( bs_read( &bs
, 8 ) )
1242 dvbsub_pdata2bpp( &bs
, p_pixbuf
+ i_x
, p_region
->i_width
- i_x
,
1247 dvbsub_pdata4bpp( &bs
, p_pixbuf
+ i_x
, p_region
->i_width
- i_x
,
1252 dvbsub_pdata8bpp( &bs
, p_pixbuf
+ i_x
, p_region
->i_width
- i_x
,
1259 /* We don't use map tables */
1262 case 0xf0: /* End of line code */
1263 p_pixbuf
+= 2*p_region
->i_width
;
1264 i_offset
= 0; i_y
+= 2;
1270 static void dvbsub_pdata2bpp( bs_t
*s
, uint8_t *p
, int i_width
, int *pi_off
)
1272 bool b_stop
= false;
1274 while( !b_stop
&& !bs_eof( s
) )
1276 int i_count
= 0, i_color
= 0;
1278 i_color
= bs_read( s
, 2 );
1279 if( i_color
!= 0x00 )
1285 if( bs_read( s
, 1 ) == 0x01 ) // Switch1
1287 i_count
= 3 + bs_read( s
, 3 );
1288 i_color
= bs_read( s
, 2 );
1292 if( bs_read( s
, 1 ) == 0x00 ) //Switch2
1294 switch( bs_read( s
, 2 ) ) //Switch3
1303 i_count
= 12 + bs_read( s
, 4 );
1304 i_color
= bs_read( s
, 2 );
1307 i_count
= 29 + bs_read( s
, 8 );
1308 i_color
= bs_read( s
, 2 );
1316 /* 1 pixel color 0 */
1322 if( !i_count
) continue;
1325 if( ( i_count
+ *pi_off
) > i_width
) break;
1327 if( i_count
== 1 ) p
[*pi_off
] = i_color
;
1328 else memset( ( p
+ *pi_off
), i_color
, i_count
);
1330 (*pi_off
) += i_count
;
1336 static void dvbsub_pdata4bpp( bs_t
*s
, uint8_t *p
, int i_width
, int *pi_off
)
1338 bool b_stop
= false;
1340 while( !b_stop
&& !bs_eof( s
) )
1342 int i_count
= 0, i_color
= 0;
1344 i_color
= bs_read( s
, 4 );
1345 if( i_color
!= 0x00 )
1352 if( bs_read( s
, 1 ) == 0x00 ) // Switch1
1354 if( bs_show( s
, 3 ) != 0x00 )
1356 i_count
= 2 + bs_read( s
, 3 );
1366 if( bs_read( s
, 1 ) == 0x00) //Switch2
1368 i_count
= 4 + bs_read( s
, 2 );
1369 i_color
= bs_read( s
, 4 );
1373 switch ( bs_read( s
, 2 ) ) //Switch3
1382 i_count
= 9 + bs_read( s
, 4 );
1383 i_color
= bs_read( s
, 4 );
1386 i_count
= 25 + bs_read( s
, 8 );
1387 i_color
= bs_read( s
, 4 );
1394 if( !i_count
) continue;
1397 if( ( i_count
+ *pi_off
) > i_width
) break;
1399 if( i_count
== 1 ) p
[*pi_off
] = i_color
;
1400 else memset( ( p
+ *pi_off
), i_color
, i_count
);
1402 (*pi_off
) += i_count
;
1408 static void dvbsub_pdata8bpp( bs_t
*s
, uint8_t *p
, int i_width
, int *pi_off
)
1410 bool b_stop
= false;
1412 while( !b_stop
&& !bs_eof( s
) )
1414 int i_count
= 0, i_color
= 0;
1416 i_color
= bs_read( s
, 8 );
1417 if( i_color
!= 0x00 )
1424 if( bs_read( s
, 1 ) == 0x00 ) // Switch1
1426 if( bs_show( s
, 7 ) != 0x00 )
1428 i_count
= bs_read( s
, 7 );
1438 i_count
= bs_read( s
, 7 );
1439 i_color
= bs_read( s
, 8 );
1443 if( !i_count
) continue;
1446 if( ( i_count
+ *pi_off
) > i_width
) break;
1448 if( i_count
== 1 ) p
[*pi_off
] = i_color
;
1449 else memset( ( p
+ *pi_off
), i_color
, i_count
);
1451 (*pi_off
) += i_count
;
1457 static void free_all( decoder_t
*p_dec
)
1459 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1460 dvbsub_region_t
*p_reg
, *p_reg_next
;
1461 dvbsub_clut_t
*p_clut
, *p_clut_next
;
1463 /*free( p_sys->p_display ); No longer malloced */
1465 for( p_clut
= p_sys
->p_cluts
; p_clut
!= NULL
; p_clut
= p_clut_next
)
1467 p_clut_next
= p_clut
->p_next
;
1470 p_sys
->p_cluts
= NULL
;
1472 for( p_reg
= p_sys
->p_regions
; p_reg
!= NULL
; p_reg
= p_reg_next
)
1474 p_reg_next
= p_reg
->p_next
;
1475 for( int i
= 0; i
< p_reg
->i_object_defs
; i
++ )
1476 free( p_reg
->p_object_defs
[i
].psz_text
);
1477 if( p_reg
->i_object_defs
) free( p_reg
->p_object_defs
);
1478 free( p_reg
->p_pixbuf
);
1481 p_sys
->p_regions
= NULL
;
1485 if( p_sys
->p_page
->i_region_defs
)
1486 free( p_sys
->p_page
->p_region_defs
);
1487 free( p_sys
->p_page
);
1489 p_sys
->p_page
= NULL
;
1492 static subpicture_t
*render( decoder_t
*p_dec
)
1494 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1495 subpicture_t
*p_spu
;
1496 subpicture_region_t
**pp_spu_region
;
1501 /* Allocate the subpicture internal data. */
1502 p_spu
= decoder_NewSubpicture( p_dec
, NULL
);
1506 p_spu
->b_absolute
= p_sys
->b_absolute
;
1507 /* Set the pf_render callback */
1508 p_spu
->i_start
= p_sys
->i_pts
;
1509 //p_spu->i_stop = (mtime_t) 0;
1510 p_spu
->b_ephemer
= true;
1511 //p_spu->b_fade = true;
1512 //p_spu->i_stop = p_spu->i_start + (mtime_t) (i_timeout * 1000000);
1513 p_spu
->b_subtitle
= true;
1515 /* Correct positioning of SPU */
1516 i_base_x
= p_sys
->i_spu_x
;
1517 i_base_y
= p_sys
->i_spu_y
;
1519 p_spu
->i_original_picture_width
= p_sys
->display
.i_width
;
1520 p_spu
->i_original_picture_height
= p_sys
->display
.i_height
;
1522 if( p_sys
->display
.b_windowed
)
1524 /* From en_300743v01 - */
1525 /* the DDS is there to indicate intended size/position of text */
1526 /* the intended video area is ->i_width/height */
1527 /* the window is within this... SO... we should leave i_original_picture_width etc. as is */
1528 /* and ONLY change i_base_x. effectively i_max_x/y are only there to limit memory requirements*/
1529 /* we COULD use the upper limits to limit rendering to within these? */
1531 /* see notes on DDS at the top of the file */
1532 i_base_x
+= p_sys
->display
.i_x
;
1533 i_base_y
+= p_sys
->display
.i_y
;
1536 pp_spu_region
= &p_spu
->p_region
;
1538 /* Loop on region definitions */
1541 msg_Dbg( p_dec
, "rendering %i regions", p_sys
->p_page
->i_region_defs
);
1544 for( i
= 0; p_sys
->p_page
&& ( i
< p_sys
->p_page
->i_region_defs
); i
++ )
1546 dvbsub_region_t
*p_region
;
1547 dvbsub_regiondef_t
*p_regiondef
;
1548 dvbsub_clut_t
*p_clut
;
1549 dvbsub_color_t
*p_color
;
1550 subpicture_region_t
*p_spu_region
;
1551 uint8_t *p_src
, *p_dst
;
1553 video_palette_t palette
;
1556 p_regiondef
= &p_sys
->p_page
->p_region_defs
[i
];
1558 /* Find associated region */
1559 for( p_region
= p_sys
->p_regions
; p_region
!= NULL
;
1560 p_region
= p_region
->p_next
)
1562 if( p_regiondef
->i_id
== p_region
->i_id
) break;
1566 /* if a region exists, then print it's size */
1569 msg_Dbg( p_dec
, "rendering region %i (%i,%i) to (%i,%i)", i
,
1570 p_regiondef
->i_x
, p_regiondef
->i_y
,
1571 p_regiondef
->i_x
+ p_region
->i_width
,
1572 p_regiondef
->i_y
+ p_region
->i_height
);
1576 msg_Dbg( p_dec
, "rendering region %i (%i,%i) (no region matched to render)", i
,
1577 p_regiondef
->i_x
, p_regiondef
->i_y
);
1583 msg_Dbg( p_dec
, "region %i not found", p_regiondef
->i_id
);
1587 /* Find associated CLUT */
1588 for( p_clut
= p_sys
->p_cluts
; p_clut
!= NULL
; p_clut
= p_clut
->p_next
)
1590 if( p_region
->i_clut
== p_clut
->i_id
) break;
1594 msg_Dbg( p_dec
, "clut %i not found", p_region
->i_clut
);
1598 /* FIXME: don't create a subpicture region with VLC CODEC YUVP
1599 * when it actually is a TEXT region */
1601 /* Create new SPU region */
1602 video_format_Init( &fmt
, VLC_CODEC_YUVP
);
1603 fmt
.i_sar_num
= 0; /* 0 means use aspect ratio of background video */
1605 fmt
.i_width
= fmt
.i_visible_width
= p_region
->i_width
;
1606 fmt
.i_height
= fmt
.i_visible_height
= p_region
->i_height
;
1607 fmt
.i_x_offset
= fmt
.i_y_offset
= 0;
1608 fmt
.p_palette
= &palette
;
1609 fmt
.p_palette
->i_entries
= ( p_region
->i_depth
== 1 ) ? 4 :
1610 ( ( p_region
->i_depth
== 2 ) ? 16 : 256 );
1611 p_color
= ( p_region
->i_depth
== 1 ) ? p_clut
->c_2b
:
1612 ( ( p_region
->i_depth
== 2 ) ? p_clut
->c_4b
: p_clut
->c_8b
);
1613 for( j
= 0; j
< fmt
.p_palette
->i_entries
; j
++ )
1615 fmt
.p_palette
->palette
[j
][0] = p_color
[j
].Y
;
1616 fmt
.p_palette
->palette
[j
][1] = p_color
[j
].Cb
; /* U == Cb */
1617 fmt
.p_palette
->palette
[j
][2] = p_color
[j
].Cr
; /* V == Cr */
1618 fmt
.p_palette
->palette
[j
][3] = 0xff - p_color
[j
].T
;
1621 p_spu_region
= subpicture_region_New( &fmt
);
1622 fmt
.p_palette
= NULL
; /* was stack var */
1623 video_format_Clean( &fmt
);
1626 msg_Err( p_dec
, "cannot allocate SPU region" );
1629 p_spu_region
->i_x
= i_base_x
+ p_regiondef
->i_x
;
1630 p_spu_region
->i_y
= i_base_y
+ p_regiondef
->i_y
;
1631 p_spu_region
->i_align
= p_sys
->i_spu_position
;
1632 *pp_spu_region
= p_spu_region
;
1633 pp_spu_region
= &p_spu_region
->p_next
;
1635 p_src
= p_region
->p_pixbuf
;
1636 p_dst
= p_spu_region
->p_picture
->Y_PIXELS
;
1637 i_pitch
= p_spu_region
->p_picture
->Y_PITCH
;
1639 /* Copy pixel buffer */
1640 for( j
= 0; j
< p_region
->i_height
; j
++ )
1642 memcpy( p_dst
, p_src
, p_region
->i_width
);
1643 p_src
+= p_region
->i_width
;
1647 /* Check subtitles encoded as strings of characters
1648 * (since there are not rendered in the pixbuffer) */
1649 for( j
= 0; j
< p_region
->i_object_defs
; j
++ )
1651 dvbsub_objectdef_t
*p_object_def
= &p_region
->p_object_defs
[j
];
1653 if( ( p_object_def
->i_type
!= 1 ) || !p_object_def
->psz_text
)
1656 /* Create new SPU region */
1657 video_format_Init( &fmt
, VLC_CODEC_TEXT
);
1660 fmt
.i_width
= fmt
.i_visible_width
= p_region
->i_width
;
1661 fmt
.i_height
= fmt
.i_visible_height
= p_region
->i_height
;
1662 fmt
.i_x_offset
= fmt
.i_y_offset
= 0;
1663 p_spu_region
= subpicture_region_New( &fmt
);
1664 video_format_Clean( &fmt
);
1666 p_spu_region
->p_text
= text_segment_New( p_object_def
->psz_text
);
1667 p_spu_region
->i_x
= i_base_x
+ p_regiondef
->i_x
+ p_object_def
->i_x
;
1668 p_spu_region
->i_y
= i_base_y
+ p_regiondef
->i_y
+ p_object_def
->i_y
;
1669 p_spu_region
->i_align
= p_sys
->i_spu_position
;
1670 *pp_spu_region
= p_spu_region
;
1671 pp_spu_region
= &p_spu_region
->p_next
;
1678 /*****************************************************************************
1679 * encoder_sys_t : encoder descriptor
1680 *****************************************************************************/
1681 typedef struct encoder_region_t
1688 struct encoder_sys_t
1690 unsigned int i_page_ver
;
1691 unsigned int i_region_ver
;
1692 unsigned int i_clut_ver
;
1695 encoder_region_t
*p_regions
;
1699 /* subpicture positioning */
1705 static void encode_page_composition( encoder_t
*, bs_t
*, subpicture_t
* );
1706 static void encode_clut( encoder_t
*, bs_t
*, subpicture_t
* );
1707 static void encode_region_composition( encoder_t
*, bs_t
*, subpicture_t
* );
1708 static void encode_object( encoder_t
*, bs_t
*, subpicture_t
* );
1710 /*****************************************************************************
1711 * OpenEncoder: probe the encoder and return score
1712 *****************************************************************************/
1713 static int OpenEncoder( vlc_object_t
*p_this
)
1715 encoder_t
*p_enc
= (encoder_t
*)p_this
;
1716 encoder_sys_t
*p_sys
;
1718 if( ( p_enc
->fmt_out
.i_codec
!= VLC_CODEC_DVBS
) &&
1721 return VLC_EGENERIC
;
1724 /* Allocate the memory needed to store the decoder's structure */
1725 if( ( p_sys
= (encoder_sys_t
*)malloc(sizeof(encoder_sys_t
)) ) == NULL
)
1727 p_enc
->p_sys
= p_sys
;
1729 p_enc
->pf_encode_sub
= Encode
;
1730 p_enc
->fmt_out
.i_codec
= VLC_CODEC_DVBS
;
1731 p_enc
->fmt_out
.subs
.dvb
.i_id
= 1 << 16 | 1;
1733 config_ChainParse( p_enc
, ENC_CFG_PREFIX
, ppsz_enc_options
, p_enc
->p_cfg
);
1735 p_sys
->i_page_ver
= 0;
1736 p_sys
->i_region_ver
= 0;
1737 p_sys
->i_clut_ver
= 0;
1738 p_sys
->i_regions
= 0;
1739 p_sys
->p_regions
= 0;
1741 p_sys
->i_offset_x
= var_CreateGetInteger( p_this
, ENC_CFG_PREFIX
"x" );
1742 p_sys
->i_offset_y
= var_CreateGetInteger( p_this
, ENC_CFG_PREFIX
"y" );
1747 /* FIXME: this routine is a hack to convert VLC_CODEC_YUVA
1748 * into VLC_CODEC_YUVP
1750 static subpicture_t
*YuvaYuvp( subpicture_t
*p_subpic
)
1752 subpicture_region_t
*p_region
= NULL
;
1754 if( !p_subpic
) return NULL
;
1756 for( p_region
= p_subpic
->p_region
; p_region
; p_region
= p_region
->p_next
)
1758 video_format_t
*p_fmt
= &p_region
->fmt
;
1759 int i
= 0, j
= 0, n
= 0, p
= 0;
1760 int i_max_entries
= 256;
1762 #ifdef RANDOM_DITHERING
1763 int i_seed
= 0xdeadbeef; /* random seed */
1767 int i_pixels
= p_region
->p_picture
->p
[0].i_visible_lines
1768 * p_region
->p_picture
->p
[0].i_pitch
;
1769 int i_iterator
= p_region
->p_picture
->p
[0].i_visible_lines
* 3 / 4
1770 * p_region
->p_picture
->p
[0].i_pitch
1771 + p_region
->p_picture
->p
[0].i_pitch
* 1 / 3;
1772 int i_tolerance
= 0;
1774 #ifdef DEBUG_DVBSUB1
1775 /* p_enc not valid here */
1776 msg_Dbg( p_enc
, "YuvaYuvp: i_pixels=%d, i_iterator=%d", i_pixels
, i_iterator
);
1778 p_fmt
->i_chroma
= VLC_CODEC_YUVP
;
1779 p_fmt
->p_palette
= (video_palette_t
*) malloc( sizeof( video_palette_t
) );
1780 if( !p_fmt
->p_palette
) break;
1781 p_fmt
->p_palette
->i_entries
= 0;
1783 /* Find best iterator using Euclide’s algorithm */
1784 for( ; i_iterator
> 1 ; i_iterator
-- )
1803 /* Count colors, build best palette */
1804 for( i_tolerance
= 0; i_tolerance
< 128; i_tolerance
++ )
1806 bool b_success
= true;
1807 p_fmt
->p_palette
->i_entries
= 0;
1809 for( i
= 0; i
< i_pixels
; )
1812 y
= p_region
->p_picture
->p
[0].p_pixels
[i
];
1813 u
= p_region
->p_picture
->p
[1].p_pixels
[i
];
1814 v
= p_region
->p_picture
->p
[2].p_pixels
[i
];
1815 a
= p_region
->p_picture
->p
[3].p_pixels
[i
];
1816 for( j
= 0; j
< p_fmt
->p_palette
->i_entries
; j
++ )
1818 if( abs((int)p_fmt
->p_palette
->palette
[j
][0] - (int)y
) <= i_tolerance
&&
1819 abs((int)p_fmt
->p_palette
->palette
[j
][1] - (int)u
) <= i_tolerance
&&
1820 abs((int)p_fmt
->p_palette
->palette
[j
][2] - (int)v
) <= i_tolerance
&&
1821 abs((int)p_fmt
->p_palette
->palette
[j
][3] - (int)a
) <= i_tolerance
/ 2 )
1826 if( j
== p_fmt
->p_palette
->i_entries
)
1828 p_fmt
->p_palette
->palette
[j
][0] = y
;
1829 p_fmt
->p_palette
->palette
[j
][1] = u
;
1830 p_fmt
->p_palette
->palette
[j
][2] = v
;
1831 p_fmt
->p_palette
->palette
[j
][3] = a
;
1832 p_fmt
->p_palette
->i_entries
++;
1834 if( p_fmt
->p_palette
->i_entries
>= i_max_entries
)
1852 #ifdef DEBUG_DVBSUB1
1853 /* p_enc not valid here */
1854 msg_Dbg( p_enc
, "best palette has %d colors", p_fmt
->p_palette
->i_entries
);
1857 #ifndef RANDOM_DITHERING
1858 pi_delta
= xmalloc( ( p_region
->p_picture
->p
[0].i_pitch
+ 1 )
1859 * sizeof(int) * 4 );
1860 for( i
= 0; i
< (p_region
->p_picture
->p
[0].i_pitch
+ 1) * 4 ; i
++ )
1866 /* Fill image with our new colours */
1867 for( p
= 0; p
< p_region
->p_picture
->p
[0].i_visible_lines
; p
++ )
1869 int i_ydelta
= 0, i_udelta
= 0, i_vdelta
= 0, i_adelta
= 0;
1871 for( n
= 0; n
< p_region
->p_picture
->p
[0].i_pitch
; n
++ )
1873 int i_offset
= p
* p_region
->p_picture
->p
[0].i_pitch
+ n
;
1875 int i_mindist
, i_best
;
1877 y
= (int)p_region
->p_picture
->p
[0].p_pixels
[i_offset
];
1878 u
= (int)p_region
->p_picture
->p
[1].p_pixels
[i_offset
];
1879 v
= (int)p_region
->p_picture
->p
[2].p_pixels
[i_offset
];
1880 a
= (int)p_region
->p_picture
->p
[3].p_pixels
[i_offset
];
1882 /* Add dithering compensation */
1883 #ifdef RANDOM_DITHERING
1884 y
+= ((i_seed
& 0xff) - 0x80) * i_tolerance
/ 0x80;
1885 u
+= (((i_seed
>> 8) & 0xff) - 0x80) * i_tolerance
/ 0x80;
1886 v
+= (((i_seed
>> 16) & 0xff) - 0x80) * i_tolerance
/ 0x80;
1887 a
+= (((i_seed
>> 24) & 0xff) - 0x80) * i_tolerance
/ 0x80;
1889 y
+= i_ydelta
+ pi_delta
[ n
* 4 ];
1890 u
+= i_udelta
+ pi_delta
[ n
* 4 + 1 ];
1891 v
+= i_vdelta
+ pi_delta
[ n
* 4 + 2 ];
1892 a
+= i_adelta
+ pi_delta
[ n
* 4 + 3 ];
1895 /* Find best colour in palette */
1896 for( i_mindist
= 99999999, i_best
= 0, j
= 0; j
< p_fmt
->p_palette
->i_entries
; j
++ )
1900 i_dist
+= abs((int)p_fmt
->p_palette
->palette
[j
][0] - y
);
1901 i_dist
+= abs((int)p_fmt
->p_palette
->palette
[j
][1] - u
);
1902 i_dist
+= abs((int)p_fmt
->p_palette
->palette
[j
][2] - v
);
1903 i_dist
+= 2 * abs((int)p_fmt
->p_palette
->palette
[j
][3] - a
);
1905 if( i_dist
< i_mindist
)
1912 /* Set pixel to best color */
1913 p_region
->p_picture
->p
[0].p_pixels
[i_offset
] = i_best
;
1915 /* Update dithering state */
1916 #ifdef RANDOM_DITHERING
1917 i_seed
= (i_seed
* 0x1283837) ^ 0x789479 ^ (i_seed
>> 13);
1919 i_ydelta
= y
- (int)p_fmt
->p_palette
->palette
[i_best
][0];
1920 i_udelta
= u
- (int)p_fmt
->p_palette
->palette
[i_best
][1];
1921 i_vdelta
= v
- (int)p_fmt
->p_palette
->palette
[i_best
][2];
1922 i_adelta
= a
- (int)p_fmt
->p_palette
->palette
[i_best
][3];
1923 pi_delta
[ n
* 4 ] = i_ydelta
* 3 / 8;
1924 pi_delta
[ n
* 4 + 1 ] = i_udelta
* 3 / 8;
1925 pi_delta
[ n
* 4 + 2 ] = i_vdelta
* 3 / 8;
1926 pi_delta
[ n
* 4 + 3 ] = i_adelta
* 3 / 8;
1927 i_ydelta
= i_ydelta
* 5 / 8;
1928 i_udelta
= i_udelta
* 5 / 8;
1929 i_vdelta
= i_vdelta
* 5 / 8;
1930 i_adelta
= i_adelta
* 5 / 8;
1934 #ifndef RANDOM_DITHERING
1939 for( i
= p_fmt
->p_palette
->i_entries
; i
< i_max_entries
; i
++ )
1941 p_fmt
->p_palette
->palette
[i
][0] = 0;
1942 p_fmt
->p_palette
->palette
[i
][1] = 0;
1943 p_fmt
->p_palette
->palette
[i
][2] = 0;
1944 p_fmt
->p_palette
->palette
[i
][3] = 0;
1946 p_fmt
->p_palette
->i_entries
= i_max_entries
;
1947 #ifdef DEBUG_DVBSUB1
1948 /* p_enc not valid here */
1949 msg_Dbg( p_enc
, "best palette has %d colors", p_fmt
->p_palette
->i_entries
);
1955 /****************************************************************************
1956 * Encode: the whole thing
1957 ****************************************************************************/
1958 static block_t
*Encode( encoder_t
*p_enc
, subpicture_t
*p_subpic
)
1960 subpicture_t
*p_temp
= NULL
;
1961 subpicture_region_t
*p_region
= NULL
;
1962 bs_t bits
, *s
= &bits
;
1965 if( !p_subpic
|| !p_subpic
->p_region
) return NULL
;
1967 /* FIXME: this is a hack to convert VLC_CODEC_YUVA into
1970 p_region
= p_subpic
->p_region
;
1971 if( p_region
->fmt
.i_chroma
== VLC_CODEC_YUVA
)
1973 p_temp
= YuvaYuvp( p_subpic
);
1976 msg_Err( p_enc
, "no picture in subpicture" );
1979 p_region
= p_subpic
->p_region
;
1983 if( !p_region
) return NULL
;
1985 if( ( p_region
->fmt
.i_chroma
!= VLC_CODEC_TEXT
) &&
1986 ( p_region
->fmt
.i_chroma
!= VLC_CODEC_YUVP
) )
1988 msg_Err( p_enc
, "chroma %4.4s not supported", (char *)&p_region
->fmt
.i_chroma
);
1992 if( p_region
->fmt
.p_palette
)
1994 switch( p_region
->fmt
.p_palette
->i_entries
)
2002 msg_Err( p_enc
, "subpicture palette (%d) not handled",
2003 p_region
->fmt
.p_palette
->i_entries
);
2010 msg_Dbg( p_enc
, "encoding subpicture" );
2012 p_block
= block_Alloc( 64000 );
2013 bs_init( s
, p_block
->p_buffer
, p_block
->i_buffer
);
2015 bs_write( s
, 8, 0x20 ); /* Data identifier */
2016 bs_write( s
, 8, 0x0 ); /* Subtitle stream id */
2018 encode_page_composition( p_enc
, s
, p_subpic
);
2019 encode_region_composition( p_enc
, s
, p_subpic
);
2020 encode_clut( p_enc
, s
, p_subpic
);
2021 encode_object( p_enc
, s
, p_subpic
);
2023 /* End of display */
2024 bs_write( s
, 8, 0x0f ); /* Sync byte */
2025 bs_write( s
, 8, DVBSUB_ST_ENDOFDISPLAY
); /* Segment type */
2026 bs_write( s
, 16, 1 ); /* Page id */
2027 bs_write( s
, 16, 0 ); /* Segment length */
2029 bs_write( s
, 8, 0xff );/* End marker */
2030 p_block
->i_buffer
= bs_pos( s
) / 8;
2031 p_block
->i_pts
= p_block
->i_dts
= p_subpic
->i_start
;
2032 if( !p_subpic
->b_ephemer
&& ( p_subpic
->i_stop
> p_subpic
->i_start
) )
2034 block_t
*p_block_stop
;
2036 p_block
->i_length
= p_subpic
->i_stop
- p_subpic
->i_start
;
2038 /* Send another (empty) subtitle to signal the end of display */
2039 p_block_stop
= block_Alloc( 64000 );
2040 bs_init( s
, p_block_stop
->p_buffer
, p_block_stop
->i_buffer
);
2041 bs_write( s
, 8, 0x20 ); /* Data identifier */
2042 bs_write( s
, 8, 0x0 ); /* Subtitle stream id */
2043 encode_page_composition( p_enc
, s
, 0 );
2044 bs_write( s
, 8, 0x0f ); /* Sync byte */
2045 bs_write( s
, 8, DVBSUB_ST_ENDOFDISPLAY
); /* Segment type */
2046 bs_write( s
, 16, 1 ); /* Page id */
2047 bs_write( s
, 16, 0 ); /* Segment length */
2048 bs_write( s
, 8, 0xff );/* End marker */
2049 p_block_stop
->i_buffer
= bs_pos( s
) / 8;
2050 p_block_stop
->i_pts
= p_block_stop
->i_dts
= p_subpic
->i_stop
;
2051 block_ChainAppend( &p_block
, p_block_stop
);
2052 p_block_stop
->i_length
= 100000; /* p_subpic->i_stop - p_subpic->i_start; */
2055 msg_Dbg( p_enc
, "subpicture encoded properly" );
2060 /*****************************************************************************
2061 * CloseEncoder: encoder destruction
2062 *****************************************************************************/
2063 static void CloseEncoder( vlc_object_t
*p_this
)
2065 encoder_t
*p_enc
= (encoder_t
*)p_this
;
2066 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
2068 var_Destroy( p_this
, ENC_CFG_PREFIX
"x" );
2069 var_Destroy( p_this
, ENC_CFG_PREFIX
"y" );
2070 var_Destroy( p_this
, ENC_CFG_PREFIX
"timeout" );
2072 if( p_sys
->i_regions
) free( p_sys
->p_regions
);
2076 static void encode_page_composition( encoder_t
*p_enc
, bs_t
*s
,
2077 subpicture_t
*p_subpic
)
2079 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
2080 subpicture_region_t
*p_region
;
2081 bool b_mode_change
= false;
2082 int i_regions
, i_timeout
;
2084 bs_write( s
, 8, 0x0f ); /* Sync byte */
2085 bs_write( s
, 8, DVBSUB_ST_PAGE_COMPOSITION
); /* Segment type */
2086 bs_write( s
, 16, 1 ); /* Page id */
2088 for( i_regions
= 0, p_region
= p_subpic
? p_subpic
->p_region
: 0;
2089 p_region
; p_region
= p_region
->p_next
, i_regions
++ )
2091 if( i_regions
>= p_sys
->i_regions
)
2093 encoder_region_t region
;
2094 region
.i_width
= region
.i_height
= 0;
2095 p_sys
->p_regions
= xrealloc( p_sys
->p_regions
,
2096 sizeof(encoder_region_t
) * (p_sys
->i_regions
+ 1) );
2097 p_sys
->p_regions
[p_sys
->i_regions
++] = region
;
2100 if( ( p_sys
->p_regions
[i_regions
].i_width
<
2101 (int)p_region
->fmt
.i_visible_width
) ||
2102 ( p_sys
->p_regions
[i_regions
].i_width
>
2103 (int)p_region
->fmt
.i_visible_width
) )
2105 b_mode_change
= true;
2106 msg_Dbg( p_enc
, "region %i width change: %i -> %i",
2107 i_regions
, p_sys
->p_regions
[i_regions
].i_width
,
2108 p_region
->fmt
.i_visible_width
);
2109 p_sys
->p_regions
[i_regions
].i_width
=
2110 p_region
->fmt
.i_visible_width
;
2112 if( p_sys
->p_regions
[i_regions
].i_height
<
2113 (int)p_region
->fmt
.i_visible_height
)
2115 b_mode_change
= true;
2116 msg_Dbg( p_enc
, "region %i height change: %i -> %i",
2117 i_regions
, p_sys
->p_regions
[i_regions
].i_height
,
2118 p_region
->fmt
.i_visible_height
);
2119 p_sys
->p_regions
[i_regions
].i_height
=
2120 p_region
->fmt
.i_visible_height
;
2124 bs_write( s
, 16, i_regions
* 6 + 2 ); /* Segment length */
2127 if( p_subpic
&& !p_subpic
->b_ephemer
&&
2128 ( p_subpic
->i_stop
> p_subpic
->i_start
) )
2130 i_timeout
= (p_subpic
->i_stop
- p_subpic
->i_start
) / 1000000;
2133 bs_write( s
, 8, i_timeout
); /* Timeout */
2134 bs_write( s
, 4, p_sys
->i_page_ver
++ );
2135 bs_write( s
, 2, b_mode_change
?
2136 DVBSUB_PCS_STATE_CHANGE
: DVBSUB_PCS_STATE_ACQUISITION
);
2137 bs_write( s
, 2, 0 ); /* Reserved */
2139 for( i_regions
= 0, p_region
= p_subpic
? p_subpic
->p_region
: 0;
2140 p_region
; p_region
= p_region
->p_next
, i_regions
++ )
2142 bs_write( s
, 8, i_regions
);
2143 bs_write( s
, 8, 0 ); /* Reserved */
2144 if( (p_sys
->i_offset_x
> 0) && (p_sys
->i_offset_y
> 0) )
2146 bs_write( s
, 16, p_sys
->i_offset_x
); /* override x position */
2147 bs_write( s
, 16, p_sys
->i_offset_y
); /* override y position */
2151 bs_write( s
, 16, p_region
->i_x
);
2152 bs_write( s
, 16, p_region
->i_y
);
2157 static void encode_clut( encoder_t
*p_enc
, bs_t
*s
, subpicture_t
*p_subpic
)
2159 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
2160 subpicture_region_t
*p_region
= p_subpic
->p_region
;
2161 video_palette_t
*p_pal
, pal
;
2164 if( !p_region
) return;
2166 if( p_region
->fmt
.i_chroma
== VLC_CODEC_YUVP
)
2168 p_pal
= p_region
->fmt
.p_palette
;
2173 for( int i
= 0; i
< 4; i
++ )
2175 pal
.palette
[i
][0] = 0;
2176 pal
.palette
[i
][1] = 0;
2177 pal
.palette
[i
][2] = 0;
2178 pal
.palette
[i
][3] = 0;
2183 bs_write( s
, 8, 0x0f ); /* Sync byte */
2184 bs_write( s
, 8, DVBSUB_ST_CLUT_DEFINITION
); /* Segment type */
2185 bs_write( s
, 16, 1 ); /* Page id */
2187 bs_write( s
, 16, p_pal
->i_entries
* 6 + 2 ); /* Segment length */
2188 bs_write( s
, 8, 1 ); /* Clut id */
2189 bs_write( s
, 4, p_sys
->i_clut_ver
++ );
2190 bs_write( s
, 4, 0 ); /* Reserved */
2192 for( int i
= 0; i
< p_pal
->i_entries
; i
++ )
2194 bs_write( s
, 8, i
); /* Clut entry id */
2195 bs_write( s
, 1, p_pal
->i_entries
== 4 ); /* 2bit/entry flag */
2196 bs_write( s
, 1, p_pal
->i_entries
== 16 ); /* 4bit/entry flag */
2197 bs_write( s
, 1, p_pal
->i_entries
== 256 ); /* 8bit/entry flag */
2198 bs_write( s
, 4, 0 ); /* Reserved */
2199 bs_write( s
, 1, 1 ); /* Full range flag */
2200 bs_write( s
, 8, p_pal
->palette
[i
][3] ? /* Y value */
2201 (p_pal
->palette
[i
][0] ? p_pal
->palette
[i
][0] : 16) : 0 );
2202 bs_write( s
, 8, p_pal
->palette
[i
][1] ); /* Cr value */
2203 bs_write( s
, 8, p_pal
->palette
[i
][2] ); /* Cb value */
2204 bs_write( s
, 8, 0xff - p_pal
->palette
[i
][3] ); /* T value */
2208 static void encode_region_composition( encoder_t
*p_enc
, bs_t
*s
,
2209 subpicture_t
*p_subpic
)
2211 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
2212 subpicture_region_t
*p_region
;
2215 for( i_region
= 0, p_region
= p_subpic
->p_region
; p_region
;
2216 p_region
= p_region
->p_next
, i_region
++ )
2218 int i_entries
= 4, i_depth
= 0x1, i_bg
= 0;
2220 ( p_region
->fmt
.i_chroma
== VLC_CODEC_TEXT
);
2224 video_palette_t
*p_pal
= p_region
->fmt
.p_palette
;
2228 msg_Err( p_enc
, "subpicture has no palette - ignoring it" );
2232 i_entries
= p_pal
->i_entries
;
2233 i_depth
= i_entries
== 4 ? 0x1 : i_entries
== 16 ? 0x2 : 0x3;
2235 for( i_bg
= 0; i_bg
< p_pal
->i_entries
; i_bg
++ )
2237 if( !p_pal
->palette
[i_bg
][3] ) break;
2241 bs_write( s
, 8, 0x0f ); /* Sync byte */
2242 bs_write( s
, 8, DVBSUB_ST_REGION_COMPOSITION
); /* Segment type */
2243 bs_write( s
, 16, 1 ); /* Page id */
2245 bs_write( s
, 16, 10 + 6 + (b_text
? 2 : 0) ); /* Segment length */
2246 bs_write( s
, 8, i_region
);
2247 bs_write( s
, 4, p_sys
->i_region_ver
++ );
2249 /* Region attributes */
2250 bs_write( s
, 1, i_bg
< i_entries
); /* Fill */
2251 bs_write( s
, 3, 0 ); /* Reserved */
2252 bs_write( s
, 16, p_sys
->p_regions
[i_region
].i_width
);
2253 bs_write( s
, 16, p_sys
->p_regions
[i_region
].i_height
);
2254 bs_write( s
, 3, i_depth
); /* Region level of compatibility */
2255 bs_write( s
, 3, i_depth
); /* Region depth */
2256 bs_write( s
, 2, 0 ); /* Reserved */
2257 bs_write( s
, 8, 1 ); /* Clut id */
2258 bs_write( s
, 8, i_bg
); /* region 8bit pixel code */
2259 bs_write( s
, 4, i_bg
); /* region 4bit pixel code */
2260 bs_write( s
, 2, i_bg
); /* region 2bit pixel code */
2261 bs_write( s
, 2, 0 ); /* Reserved */
2263 /* In our implementation we only have 1 object per region */
2264 bs_write( s
, 16, i_region
);
2265 bs_write( s
, 2, b_text
? DVBSUB_OT_BASIC_CHAR
:DVBSUB_OT_BASIC_BITMAP
);
2266 bs_write( s
, 2, 0 ); /* object provider flag */
2267 bs_write( s
, 12, 0 );/* object horizontal position */
2268 bs_write( s
, 4, 0 ); /* Reserved */
2269 bs_write( s
, 12, 0 );/* object vertical position */
2273 bs_write( s
, 8, 1 ); /* foreground pixel code */
2274 bs_write( s
, 8, 0 ); /* background pixel code */
2279 static void encode_pixel_data( encoder_t
*p_enc
, bs_t
*s
,
2280 subpicture_region_t
*p_region
,
2283 static void encode_object( encoder_t
*p_enc
, bs_t
*s
, subpicture_t
*p_subpic
)
2285 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
2286 subpicture_region_t
*p_region
;
2289 int i_length_pos
, i_update_pos
, i_pixel_data_pos
;
2291 for( i_region
= 0, p_region
= p_subpic
->p_region
; p_region
;
2292 p_region
= p_region
->p_next
, i_region
++ )
2294 bs_write( s
, 8, 0x0f ); /* Sync byte */
2295 bs_write( s
, 8, DVBSUB_ST_OBJECT_DATA
); /* Segment type */
2296 bs_write( s
, 16, 1 ); /* Page id */
2298 i_length_pos
= bs_pos( s
);
2299 bs_write( s
, 16, 0 ); /* Segment length */
2300 bs_write( s
, 16, i_region
); /* Object id */
2301 bs_write( s
, 4, p_sys
->i_region_ver
++ );
2303 /* object coding method */
2304 switch( p_region
->fmt
.i_chroma
)
2306 case VLC_CODEC_YUVP
:
2307 bs_write( s
, 2, 0 );
2309 case VLC_CODEC_TEXT
:
2310 bs_write( s
, 2, 1 );
2313 msg_Err( p_enc
, "FOURCC %d not supported by encoder.", p_region
->fmt
.i_chroma
);
2317 bs_write( s
, 1, 0 ); /* non modifying color flag */
2318 bs_write( s
, 1, 0 ); /* Reserved */
2320 if( p_region
->fmt
.i_chroma
== VLC_CODEC_TEXT
)
2324 if( !p_region
->p_text
) continue;
2326 i_size
= __MIN( strlen( p_region
->p_text
->psz_text
), 256 );
2328 bs_write( s
, 8, i_size
); /* number of characters in string */
2329 for( i
= 0; i
< i_size
; i
++ )
2331 bs_write( s
, 16, p_region
->p_text
->psz_text
[i
] );
2334 /* Update segment length */
2335 SetWBE( &s
->p_start
[i_length_pos
/8],
2336 (bs_pos(s
) - i_length_pos
)/8 -2 );
2340 /* Coding of a bitmap object */
2341 i_update_pos
= bs_pos( s
);
2342 bs_write( s
, 16, 0 ); /* topfield data block length */
2343 bs_write( s
, 16, 0 ); /* bottomfield data block length */
2346 i_pixel_data_pos
= bs_pos( s
);
2347 encode_pixel_data( p_enc
, s
, p_region
, true );
2348 i_pixel_data_pos
= ( bs_pos( s
) - i_pixel_data_pos
) / 8;
2349 SetWBE( &s
->p_start
[i_update_pos
/8], i_pixel_data_pos
);
2352 i_pixel_data_pos
= bs_pos( s
);
2353 encode_pixel_data( p_enc
, s
, p_region
, false );
2354 i_pixel_data_pos
= ( bs_pos( s
) - i_pixel_data_pos
) / 8;
2355 SetWBE( &s
->p_start
[i_update_pos
/8+2], i_pixel_data_pos
);
2357 /* Stuffing for word alignment */
2359 if( bs_pos( s
) % 16 ) bs_write( s
, 8, 0 );
2361 /* Update segment length */
2362 SetWBE( &s
->p_start
[i_length_pos
/8], (bs_pos(s
) - i_length_pos
)/8 -2 );
2366 static void encode_pixel_line_2bp( bs_t
*s
, subpicture_region_t
*p_region
,
2368 static void encode_pixel_line_4bp( bs_t
*s
, subpicture_region_t
*p_region
,
2370 static void encode_pixel_line_8bp( bs_t
*s
, subpicture_region_t
*p_region
,
2372 static void encode_pixel_data( encoder_t
*p_enc
, bs_t
*s
,
2373 subpicture_region_t
*p_region
,
2376 unsigned int i_line
;
2379 if( p_region
->fmt
.i_chroma
!= VLC_CODEC_YUVP
) return;
2381 /* Encode line by line */
2382 for( i_line
= !b_top
; i_line
< p_region
->fmt
.i_visible_height
;
2385 switch( p_region
->fmt
.p_palette
->i_entries
)
2391 bs_write( s
, 8, 0x10 ); /* 2 bit/pixel code string */
2392 encode_pixel_line_2bp( s
, p_region
, i_line
);
2396 bs_write( s
, 8, 0x11 ); /* 4 bit/pixel code string */
2397 encode_pixel_line_4bp( s
, p_region
, i_line
);
2401 bs_write( s
, 8, 0x12 ); /* 8 bit/pixel code string */
2402 encode_pixel_line_8bp( s
, p_region
, i_line
);
2406 msg_Err( p_enc
, "subpicture palette (%i) not handled",
2407 p_region
->fmt
.p_palette
->i_entries
);
2411 bs_write( s
, 8, 0xf0 ); /* End of object line code */
2415 static void encode_pixel_line_2bp( bs_t
*s
, subpicture_region_t
*p_region
,
2418 unsigned int i
, i_length
= 0;
2419 int i_pitch
= p_region
->p_picture
->p
->i_pitch
;
2420 uint8_t *p_data
= &p_region
->p_picture
->p
->p_pixels
[ i_pitch
* i_line
];
2421 int i_last_pixel
= p_data
[0];
2423 for( i
= 0; i
<= p_region
->fmt
.i_visible_width
; i
++ )
2425 if( ( i
!= p_region
->fmt
.i_visible_width
) &&
2426 ( p_data
[i
] == i_last_pixel
) && ( i_length
!= 284 ) )
2432 if( ( i_length
== 1 ) || ( i_length
== 11 ) || ( i_length
== 28 ) )
2434 /* 2bit/pixel code */
2436 bs_write( s
, 2, i_last_pixel
);
2439 bs_write( s
, 2, 0 );
2440 bs_write( s
, 1, 0 );
2441 bs_write( s
, 1, 1 ); /* pseudo color 0 */
2450 bs_write( s
, 2, i_last_pixel
);
2451 bs_write( s
, 2, i_last_pixel
);
2455 bs_write( s
, 2, 0 );
2456 bs_write( s
, 1, 0 );
2457 bs_write( s
, 1, 0 );
2458 bs_write( s
, 2, 1 ); /* 2 * pseudo color 0 */
2461 else if( i_length
> 2 )
2463 bs_write( s
, 2, 0 );
2464 if( i_length
<= 10 )
2466 bs_write( s
, 1, 1 );
2467 bs_write( s
, 3, i_length
- 3 );
2468 bs_write( s
, 2, i_last_pixel
);
2472 bs_write( s
, 1, 0 );
2473 bs_write( s
, 1, 0 );
2475 if( i_length
<= 27 )
2477 bs_write( s
, 2, 2 );
2478 bs_write( s
, 4, i_length
- 12 );
2479 bs_write( s
, 2, i_last_pixel
);
2483 bs_write( s
, 2, 3 );
2484 bs_write( s
, 8, i_length
- 29 );
2485 bs_write( s
, 2, i_last_pixel
);
2490 if( i
== p_region
->fmt
.i_visible_width
) break;
2492 i_last_pixel
= p_data
[i
];
2497 bs_write( s
, 2, 0 );
2498 bs_write( s
, 1, 0 );
2499 bs_write( s
, 1, 0 );
2500 bs_write( s
, 2, 0 );
2506 static void encode_pixel_line_4bp( bs_t
*s
, subpicture_region_t
*p_region
,
2509 unsigned int i
, i_length
= 0;
2510 int i_pitch
= p_region
->p_picture
->p
->i_pitch
;
2511 uint8_t *p_data
= &p_region
->p_picture
->p
->p_pixels
[ i_pitch
* i_line
];
2512 int i_last_pixel
= p_data
[0];
2514 for( i
= 0; i
<= p_region
->fmt
.i_visible_width
; i
++ )
2516 if( i
!= p_region
->fmt
.i_visible_width
&&
2517 p_data
[i
] == i_last_pixel
&& i_length
!= 280 )
2523 if( ( i_length
== 1 ) ||
2524 ( ( i_length
== 3 ) && i_last_pixel
) ||
2527 /* 4bit/pixel code */
2529 bs_write( s
, 4, i_last_pixel
);
2532 bs_write( s
, 4, 0 );
2533 bs_write( s
, 1, 1 );
2534 bs_write( s
, 1, 1 );
2535 bs_write( s
, 2, 0 ); /* pseudo color 0 */
2544 bs_write( s
, 4, i_last_pixel
);
2545 bs_write( s
, 4, i_last_pixel
);
2549 bs_write( s
, 4, 0 );
2550 bs_write( s
, 1, 1 );
2551 bs_write( s
, 1, 1 );
2552 bs_write( s
, 2, 1 ); /* 2 * pseudo color 0 */
2555 else if( !i_last_pixel
&& ( i_length
>= 3 ) && ( i_length
<= 9 ) )
2557 bs_write( s
, 4, 0 );
2558 bs_write( s
, 1, 0 );
2559 bs_write( s
, 3, i_length
- 2 ); /* (i_length - 2) * color 0 */
2561 else if( i_length
> 2 )
2563 bs_write( s
, 4, 0 );
2564 bs_write( s
, 1, 1 );
2568 bs_write( s
, 1, 0 );
2569 bs_write( s
, 2, i_length
- 4 );
2570 bs_write( s
, 4, i_last_pixel
);
2574 bs_write( s
, 1, 1 );
2576 if( i_length
<= 24 )
2578 bs_write( s
, 2, 2 );
2579 bs_write( s
, 4, i_length
- 9 );
2580 bs_write( s
, 4, i_last_pixel
);
2584 bs_write( s
, 2, 3 );
2585 bs_write( s
, 8, i_length
- 25 );
2586 bs_write( s
, 4, i_last_pixel
);
2591 if( i
== p_region
->fmt
.i_visible_width
) break;
2593 i_last_pixel
= p_data
[i
];
2598 bs_write( s
, 8, 0 );
2604 static void encode_pixel_line_8bp( bs_t
*s
, subpicture_region_t
*p_region
,
2607 unsigned int i
, i_length
= 0;
2608 int i_pitch
= p_region
->p_picture
->p
->i_pitch
;
2609 uint8_t *p_data
= &p_region
->p_picture
->p
->p_pixels
[ i_pitch
* i_line
];
2610 int i_last_pixel
= p_data
[0];
2612 for( i
= 0; i
<= p_region
->fmt
.i_visible_width
; i
++ )
2614 if( ( i
!= p_region
->fmt
.i_visible_width
) &&
2615 ( p_data
[i
] == i_last_pixel
) && ( i_length
!= 127 ) )
2621 if( ( i_length
== 1 ) && i_last_pixel
)
2623 /* 8bit/pixel code */
2624 bs_write( s
, 8, i_last_pixel
);
2626 else if( ( i_length
== 2 ) && i_last_pixel
)
2628 /* 8bit/pixel code */
2629 bs_write( s
, 8, i_last_pixel
);
2630 bs_write( s
, 8, i_last_pixel
);
2632 else if( i_length
<= 127 )
2634 bs_write( s
, 8, 0 );
2638 bs_write( s
, 1, 0 );
2639 bs_write( s
, 7, i_length
); /* pseudo color 0 */
2643 bs_write( s
, 1, 1 );
2644 bs_write( s
, 7, i_length
);
2645 bs_write( s
, 8, i_last_pixel
);
2649 if( i
== p_region
->fmt
.i_visible_width
) break;
2651 i_last_pixel
= p_data
[i
];
2656 bs_write( s
, 8, 0 );
2657 bs_write( s
, 8, 0 );
2665 static void default_dds_init( decoder_t
* p_dec
)
2667 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
2669 /* see notes on DDS at the top of the file */
2671 /* configure for SD res in case DDS is not present */
2672 p_sys
->display
.i_version
= 0xff; /* an invalid version so it's always different */
2673 p_sys
->display
.i_width
= 720;
2674 p_sys
->display
.i_height
= 576;
2675 p_sys
->display
.b_windowed
= false;