contrib: cargo: use cargo/vendored-openssl if needed
[vlc.git] / modules / codec / dvbsub.c
blobb464314af3c077d5a1972195a187d914b9de7f30
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
8 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * Damien LUCAS <damien.lucas@anevia.com>
10 * Laurent Aimar <fenrir@via.ecp.fr>
11 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
12 * Derk-Jan Hartman <hartman #at# videolan dot org>
13 * Simon Hailes <simon _a_ screen.subtitling.com>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU Lesser General Public License as published by
17 * the Free Software Foundation; either version 2.1 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with this program; if not, write to the Free Software Foundation, Inc.,
27 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28 *****************************************************************************/
30 /*****************************************************************************
31 * Preamble
33 * FIXME:
34 * DVB subtitles coded as strings of characters are not handled correctly.
35 * The character codes in the string should actually be indexes referring to a
36 * character table identified in the subtitle descriptor.
38 * The spec is quite vague in this area, but what is meant is perhaps that it
39 * refers to the character index in the codepage belonging to the language
40 * specified in the subtitle descriptor. Potentially it's designed for widechar
41 * (but not for UTF-*) codepages.
42 *****************************************************************************
44 *****************************************************************************
45 * Notes on DDS (Display Definition Segment)
46 * -----------------------------------------
47 * DDS (Display Definition Segment) tells the decoder how the subtitle image
48 * relates to the video image.
49 * For SD, the subtitle image is always considered to be for display at
50 * 720x576 (although it's assumed that for NTSC, this is 720x480, this
51 * is not documented well) Also, for SD, the subtitle image is drawn 'on
52 * the glass' (i.e. after video scaling, letterbox, etc.)
53 * For 'HD' (subs marked type 0x14/0x24 in PSI), a DDS must be present,
54 * and the subs area is drawn onto the video area (scales if necessary).
55 * The DDS tells the decoder what resolution the subtitle images were
56 * intended for, and hence how to scale the subtitle images for a
57 * particular video size
58 * i.e. if HD video is presented as letterbox, the subs will be in the
59 * same place on the video as if the video was presented on an HD set
60 * indeed, if the HD video was pillarboxed by the decoder, the subs may
61 * be cut off as well as the video. The intent here is that the subs can
62 * be placed accurately on the video - something which was missed in the
63 * original spec.
65 * A DDS may also specify a window - this is where the subs images are moved so that the (0,0)
66 * origin of decode is offset.
67 ********************************************************************************************/
69 #ifdef HAVE_CONFIG_H
70 # include "config.h"
71 #endif
73 #include <vlc_common.h>
74 #include <vlc_plugin.h>
75 #include <vlc_codec.h>
76 #include <vlc_sout.h>
78 #include <vlc_bits.h>
80 /* #define DEBUG_DVBSUB 1 */
82 #define POSX_TEXT N_("Decoding X coordinate")
83 #define POSX_LONGTEXT N_("X coordinate of the rendered subtitle")
85 #define POSY_TEXT N_("Decoding Y coordinate")
86 #define POSY_LONGTEXT N_("Y coordinate of the rendered subtitle")
88 #define POS_TEXT N_("Subpicture position")
89 #define POS_LONGTEXT N_( \
90 "You can enforce the subpicture position on the video " \
91 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
92 "also use combinations of these values, e.g. 6=top-right).")
94 #define ENC_POSX_TEXT N_("Encoding X coordinate")
95 #define ENC_POSX_LONGTEXT N_("X coordinate of the encoded subtitle" )
96 #define ENC_POSY_TEXT N_("Encoding Y coordinate")
97 #define ENC_POSY_LONGTEXT N_("Y coordinate of the encoded subtitle" )
99 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
100 static const char *const ppsz_pos_descriptions[] =
101 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
102 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
104 /*****************************************************************************
105 * Module descriptor.
106 *****************************************************************************/
107 static int Open ( vlc_object_t * );
108 static void Close( vlc_object_t * );
109 static int Decode( decoder_t *, block_t * );
110 static void Flush( decoder_t * );
112 #ifdef ENABLE_SOUT
113 static int OpenEncoder ( vlc_object_t * );
114 static void CloseEncoder( vlc_object_t * );
115 static block_t *Encode ( encoder_t *, subpicture_t * );
116 #endif
118 vlc_module_begin ()
119 # define DVBSUB_CFG_PREFIX "dvbsub-"
120 set_description( N_("DVB subtitles decoder") )
121 set_shortname( N_("DVB subtitles") )
122 set_capability( "spu 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 )
132 #ifdef ENABLE_SOUT
133 # define ENC_CFG_PREFIX "sout-dvbsub-"
134 add_submodule ()
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 )
141 #endif
142 vlc_module_end ()
144 static const char *const ppsz_enc_options[] = { "x", "y", NULL };
146 /****************************************************************************
147 * Local structures
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
155 int i_id;
156 int i_type;
157 int i_x;
158 int i_y;
159 int i_fg_pc;
160 int i_bg_pc;
161 char *psz_text; /* for string of characters objects */
163 } dvbsub_objectdef_t;
165 /* The entry in the palette CLUT */
166 typedef struct
168 uint8_t Y;
169 uint8_t Cr;
170 uint8_t Cb;
171 uint8_t T;
173 } dvbsub_color_t;
175 /* The displays dimensions [7.2.1] */
176 typedef struct dvbsub_display_s
178 uint8_t i_id;
179 uint8_t i_version;
181 int i_width;
182 int i_height;
184 bool b_windowed;
185 /* these values are only relevant if windowed */
186 int i_x;
187 int i_y;
188 int i_max_x;
189 int i_max_y;
191 } dvbsub_display_t;
193 /* [7.2.4] */
194 typedef struct dvbsub_clut_s
196 uint8_t i_id;
197 uint8_t i_version;
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;
204 } dvbsub_clut_t;
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
210 int i_id;
211 int i_version;
212 int i_x;
213 int i_y;
214 int i_width;
215 int i_height;
216 int i_level_comp;
217 int i_depth;
218 int i_clut;
220 uint8_t *p_pixbuf;
222 int i_object_defs;
223 dvbsub_objectdef_t *p_object_defs;
225 struct dvbsub_region_s *p_next;
227 } dvbsub_region_t;
229 /* The object definition gives the position of the object in a region */
230 typedef struct dvbsub_regiondef_s
232 int i_id;
233 int i_x;
234 int i_y;
236 } dvbsub_regiondef_t;
238 /* The page defines the list of regions [7.2.2] */
239 typedef struct
241 int i_id;
242 int i_timeout; /* in seconds */
243 int i_state;
244 int i_version;
246 int i_region_defs;
247 dvbsub_regiondef_t *p_region_defs;
249 } dvbsub_page_t;
251 typedef struct
253 bs_t bs;
255 /* Decoder internal data */
256 int i_id;
257 int i_ancillary_id;
258 vlc_tick_t i_pts;
260 bool b_absolute;
261 int i_spu_position;
262 int i_spu_x;
263 int i_spu_y;
265 bool b_page;
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;
272 } decoder_sys_t;
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 /*****************************************************************************
304 * Local prototypes
305 *****************************************************************************/
306 static void decode_segment( decoder_t *, bs_t * );
307 static void decode_page_composition( decoder_t *, bs_t *, uint16_t );
308 static void decode_region_composition( decoder_t *, bs_t *, uint16_t );
309 static void decode_object( decoder_t *, bs_t *, uint16_t );
310 static void decode_display_definition( decoder_t *, bs_t *, uint16_t );
311 static void decode_clut( decoder_t *, bs_t *, uint16_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
323 * to chose.
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;
329 int i_posx, i_posy;
331 if( p_dec->fmt_in.i_codec != VLC_CODEC_DVBS )
333 return VLC_EGENERIC;
336 p_dec->pf_decode = Decode;
337 p_dec->pf_flush = Flush;
338 p_sys = p_dec->p_sys = calloc( 1, sizeof(decoder_sys_t) );
339 if( !p_sys )
340 return VLC_ENOMEM;
342 p_sys->i_pts = VLC_TICK_INVALID;
343 p_sys->i_id = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
344 p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
346 p_sys->p_regions = NULL;
347 p_sys->p_cluts = NULL;
348 p_sys->p_page = NULL;
350 /* configure for SD res in case DDS is not present */
351 default_dds_init( p_dec );
353 p_sys->i_spu_position = var_CreateGetInteger( p_this,
354 DVBSUB_CFG_PREFIX "position" );
355 i_posx = var_CreateGetInteger( p_this, DVBSUB_CFG_PREFIX "x" );
356 i_posy = var_CreateGetInteger( p_this, DVBSUB_CFG_PREFIX "y" );
358 /* Check if subpicture position was overridden */
359 p_sys->b_absolute = true;
360 p_sys->i_spu_x = p_sys->i_spu_y = 0;
362 if( ( i_posx >= 0 ) && ( i_posy >= 0 ) )
364 p_sys->b_absolute = true;
365 p_sys->i_spu_x = i_posx;
366 p_sys->i_spu_y = i_posy;
369 p_dec->fmt_out.i_codec = 0;
371 default_clut_init( p_dec );
373 return VLC_SUCCESS;
376 /*****************************************************************************
377 * Close:
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" );
388 free_all( p_dec );
389 free( p_sys );
392 /*****************************************************************************
393 * Flush:
394 *****************************************************************************/
395 static void Flush( decoder_t *p_dec )
397 decoder_sys_t *p_sys = p_dec->p_sys;
399 p_sys->i_pts = VLC_TICK_INVALID;
402 /*****************************************************************************
403 * Decode:
404 *****************************************************************************/
405 static int Decode( decoder_t *p_dec, block_t *p_block )
407 decoder_sys_t *p_sys = p_dec->p_sys;
409 if( p_block == NULL ) /* No Drain */
410 return VLCDEC_SUCCESS;
412 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
414 Flush( p_dec );
415 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
417 block_Release( p_block );
418 return VLCDEC_SUCCESS;
422 /* configure for SD res in case DDS is not present */
423 /* a change of PTS is a good indication we must get a new DDS */
424 if( p_sys->i_pts != p_block->i_pts )
425 default_dds_init( p_dec );
427 p_sys->i_pts = p_block->i_pts;
428 if( p_sys->i_pts == VLC_TICK_INVALID )
430 #ifdef DEBUG_DVBSUB
431 /* Some DVB channels send stuffing segments in non-dated packets so
432 * don't complain too loudly. */
433 msg_Warn( p_dec, "non dated subtitle" );
434 #endif
435 block_Release( p_block );
436 return VLCDEC_SUCCESS;
439 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
441 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
443 msg_Dbg( p_dec, "invalid data identifier" );
444 block_Release( p_block );
445 return VLCDEC_SUCCESS;
448 if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
450 msg_Dbg( p_dec, "invalid subtitle stream id" );
451 block_Release( p_block );
452 return VLCDEC_SUCCESS;
455 #ifdef DEBUG_DVBSUB
456 msg_Dbg( p_dec, "subtitle packet received: %"PRId64, p_sys->i_pts );
457 #endif
459 p_sys->b_page = false;
461 uint8_t i_sync_byte = bs_read( &p_sys->bs, 8 );
462 while( i_sync_byte == 0x0f ) /* Sync byte */
464 decode_segment( p_dec, &p_sys->bs );
465 i_sync_byte = bs_read( &p_sys->bs, 8 );
468 if( ( i_sync_byte & 0x3f ) != 0x3f ) /* End marker */
470 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
471 block_Release( p_block );
472 return VLCDEC_SUCCESS;
475 /* Check if the page is to be displayed */
476 if( p_sys->p_page && p_sys->b_page )
478 subpicture_t *p_spu = render( p_dec );
479 if( p_spu != NULL )
480 decoder_QueueSub( p_dec, p_spu );
483 block_Release( p_block );
485 return VLCDEC_SUCCESS;
488 /* following functions are local */
490 /*****************************************************************************
491 * default_clut_init: default clut as defined in EN 300-743 section 10
492 *****************************************************************************/
493 static void default_clut_init( decoder_t *p_dec )
495 decoder_sys_t *p_sys = p_dec->p_sys;
496 uint8_t i;
498 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
499 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
500 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
502 /* 4 entries CLUT */
503 for( i = 0; i < 4; i++ )
505 uint8_t R = 0, G = 0, B = 0, T = 0;
507 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
508 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
509 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
510 else R = G = B = 0x7F;
512 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
513 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
514 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
515 p_sys->default_clut.c_2b[i].T = T;
518 /* 16 entries CLUT */
519 for( i = 0; i < 16; i++ )
521 uint8_t R = 0, G = 0, B = 0, T = 0;
523 if( !(i & 0x8) )
525 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
527 T = 0xFF;
529 else
531 R = (i & 0x1) ? 0xFF : 0;
532 G = (i & 0x2) ? 0xFF : 0;
533 B = (i & 0x4) ? 0xFF : 0;
536 else
538 R = (i & 0x1) ? 0x7F : 0;
539 G = (i & 0x2) ? 0x7F : 0;
540 B = (i & 0x4) ? 0x7F : 0;
543 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
544 p_sys->default_clut.c_4b[i].Cr = RGB_TO_V(R,G,B);
545 p_sys->default_clut.c_4b[i].Cb = RGB_TO_U(R,G,B);
546 p_sys->default_clut.c_4b[i].T = T;
549 /* 256 entries CLUT */
550 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
553 static void decode_segment( decoder_t *p_dec, bs_t *s )
555 decoder_sys_t *p_sys = p_dec->p_sys;
556 int i_type;
557 int i_page_id;
558 int i_size;
560 /* sync_byte (already checked) */
561 //bs_skip( s, 8 );
563 /* segment type */
564 i_type = bs_read( s, 8 );
566 /* page id */
567 i_page_id = bs_read( s, 16 );
569 /* segment size */
570 i_size = bs_read( s, 16 );
572 if( ( i_page_id != p_sys->i_id ) &&
573 ( i_page_id != p_sys->i_ancillary_id ) )
575 #ifdef DEBUG_DVBSUB
576 msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
577 i_page_id, p_sys->i_id );
578 #endif
579 bs_skip( s, 8 * i_size );
580 return;
583 if( ( p_sys->i_ancillary_id != p_sys->i_id ) &&
584 ( i_type == DVBSUB_ST_PAGE_COMPOSITION ) &&
585 ( i_page_id == p_sys->i_ancillary_id ) )
587 #ifdef DEBUG_DVBSUB
588 msg_Dbg( p_dec, "skipped invalid ancillary subtitle packet" );
589 #endif
590 bs_skip( s, 8 * i_size );
591 return;
594 #ifdef DEBUG_DVBSUB
595 if( i_page_id == p_sys->i_id )
596 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
597 else
598 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
599 #endif
601 switch( i_type )
603 case DVBSUB_ST_PAGE_COMPOSITION:
604 #ifdef DEBUG_DVBSUB
605 msg_Dbg( p_dec, "decode_page_composition" );
606 #endif
607 decode_page_composition( p_dec, s, i_size );
608 break;
610 case DVBSUB_ST_REGION_COMPOSITION:
611 #ifdef DEBUG_DVBSUB
612 msg_Dbg( p_dec, "decode_region_composition" );
613 #endif
614 decode_region_composition( p_dec, s, i_size );
615 break;
617 case DVBSUB_ST_CLUT_DEFINITION:
618 #ifdef DEBUG_DVBSUB
619 msg_Dbg( p_dec, "decode_clut" );
620 #endif
621 decode_clut( p_dec, s, i_size );
622 break;
624 case DVBSUB_ST_OBJECT_DATA:
625 #ifdef DEBUG_DVBSUB
626 msg_Dbg( p_dec, "decode_object" );
627 #endif
628 decode_object( p_dec, s, i_size );
629 break;
631 case DVBSUB_ST_DISPLAY_DEFINITION:
632 #ifdef DEBUG_DVBSUB
633 msg_Dbg( p_dec, "decode_display_definition" );
634 #endif
635 decode_display_definition( p_dec, s, i_size );
636 break;
638 case DVBSUB_ST_ENDOFDISPLAY:
639 #ifdef DEBUG_DVBSUB
640 msg_Dbg( p_dec, "end of display" );
641 #endif
642 bs_skip( s, 8 * i_size );
643 break;
645 case DVBSUB_ST_STUFFING:
646 #ifdef DEBUG_DVBSUB
647 msg_Dbg( p_dec, "skip stuffing" );
648 #endif
649 bs_skip( s, 8 * i_size );
650 break;
652 default:
653 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
654 bs_skip( s, 8 * i_size );
655 break;
659 static void decode_clut( decoder_t *p_dec, bs_t *s, uint16_t i_segment_length )
661 decoder_sys_t *p_sys = p_dec->p_sys;
662 uint16_t i_processed_length;
663 dvbsub_clut_t *p_clut, *p_next;
664 int i_id, i_version;
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 ) )
678 /* Nothing to do */
679 bs_skip( s, 8 * i_segment_length - 12 );
680 return;
683 if( !p_clut )
685 #ifdef DEBUG_DVBSUB
686 msg_Dbg( p_dec, "new clut: %i", i_id );
687 #endif
688 p_clut = malloc( sizeof( dvbsub_clut_t ) );
689 if( !p_clut )
690 return;
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;
702 p_clut->i_id = i_id;
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;
708 uint_fast8_t cid = bs_read( s, 8 );
709 uint_fast8_t type = bs_read( s, 3 );
711 bs_skip( s, 4 );
713 if( bs_read( s, 1 ) )
715 y = bs_read( s, 8 );
716 cr = bs_read( s, 8 );
717 cb = bs_read( s, 8 );
718 t = bs_read( s, 8 );
719 i_processed_length += 6;
721 else
723 y = bs_read( s, 6 ) << 2;
724 cr = bs_read( s, 4 ) << 4;
725 cb = bs_read( s, 4 ) << 4;
726 t = bs_read( s, 2 ) << 6;
727 i_processed_length += 4;
730 /* We are not entirely compliant here as full transparency is indicated
731 * with a luma value of zero, not a transparency value of 0xff
732 * (full transparency would actually be 0xff + 1). */
733 if( y == 0 )
735 cr = cb = 0;
736 t = 0xff;
739 /* According to EN 300-743 section 7.2.3 note 1, type should
740 * not have more than 1 bit set to one, but some streams don't
741 * respect this note. */
742 if( ( type & 0x04 ) && ( cid < 4 ) )
744 p_clut->c_2b[cid].Y = y;
745 p_clut->c_2b[cid].Cr = cr;
746 p_clut->c_2b[cid].Cb = cb;
747 p_clut->c_2b[cid].T = t;
749 if( ( type & 0x02 ) && ( cid < 16 ) )
751 p_clut->c_4b[cid].Y = y;
752 p_clut->c_4b[cid].Cr = cr;
753 p_clut->c_4b[cid].Cb = cb;
754 p_clut->c_4b[cid].T = t;
756 if( type & 0x01 )
758 p_clut->c_8b[cid].Y = y;
759 p_clut->c_8b[cid].Cr = cr;
760 p_clut->c_8b[cid].Cb = cb;
761 p_clut->c_8b[cid].T = t;
766 static void decode_page_composition( decoder_t *p_dec, bs_t *s, uint16_t i_segment_length )
768 decoder_sys_t *p_sys = p_dec->p_sys;
769 int i_version, i_state, i_timeout, i;
771 /* A page is composed by 0 or more region */
772 i_timeout = bs_read( s, 8 );
773 i_version = bs_read( s, 4 );
774 i_state = bs_read( s, 2 );
775 bs_skip( s, 2 ); /* Reserved */
777 if( i_state == DVBSUB_PCS_STATE_CHANGE )
779 /* End of an epoch, reset decoder buffer */
780 #ifdef DEBUG_DVBSUB
781 msg_Dbg( p_dec, "page composition mode change" );
782 #endif
783 free_all( p_dec );
785 else if( !p_sys->p_page && ( i_state != DVBSUB_PCS_STATE_ACQUISITION ) &&
786 ( i_state != DVBSUB_PCS_STATE_CHANGE ) )
788 /* Not a full PCS, we need to wait for one */
789 msg_Dbg( p_dec, "didn't receive an acquisition page yet" );
791 #if 0
792 /* Try to start decoding even without an acquisition page */
793 bs_skip( s, 8 * (i_segment_length - 2) );
794 return;
795 #endif
798 #ifdef DEBUG_DVBSUB
799 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
800 msg_Dbg( p_dec, "acquisition page composition" );
801 #endif
803 /* Check version number */
804 if( p_sys->p_page && ( p_sys->p_page->i_version == i_version ) )
806 bs_skip( s, 8 * (i_segment_length - 2) );
807 return;
809 else if( p_sys->p_page )
811 if( p_sys->p_page->i_region_defs )
812 free( p_sys->p_page->p_region_defs );
813 p_sys->p_page->p_region_defs = NULL;
814 p_sys->p_page->i_region_defs = 0;
817 if( !p_sys->p_page )
819 #ifdef DEBUG_DVBSUB
820 msg_Dbg( p_dec, "new page" );
821 #endif
822 /* Allocate a new page */
823 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
824 if( !p_sys->p_page )
825 return;
828 p_sys->p_page->i_version = i_version;
829 p_sys->p_page->i_timeout = i_timeout;
830 p_sys->b_page = true;
832 /* Number of regions */
833 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
835 if( p_sys->p_page->i_region_defs == 0 ) return;
837 p_sys->p_page->p_region_defs =
838 vlc_alloc( p_sys->p_page->i_region_defs, sizeof(dvbsub_regiondef_t) );
839 if( p_sys->p_page->p_region_defs )
841 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
843 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
844 bs_skip( s, 8 ); /* Reserved */
845 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
846 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
848 #ifdef DEBUG_DVBSUB
849 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
850 i, p_sys->p_page->p_region_defs[i].i_x,
851 p_sys->p_page->p_region_defs[i].i_y );
852 #endif
857 static void decode_region_composition( decoder_t *p_dec, bs_t *s, uint16_t i_segment_length )
859 decoder_sys_t *p_sys = p_dec->p_sys;
860 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
861 int i_processed_length, i_id, i_version;
862 int i_width, i_height, i_level_comp, i_depth, i_clut;
863 int i_8_bg, i_4_bg, i_2_bg;
864 bool b_fill;
866 i_id = bs_read( s, 8 );
867 i_version = bs_read( s, 4 );
869 /* Check if we already have this region */
870 for( p_region = p_sys->p_regions; p_region != NULL;
871 p_region = p_region->p_next )
873 pp_region = &p_region->p_next;
874 if( p_region->i_id == i_id ) break;
877 /* Check version number */
878 if( p_region && ( p_region->i_version == i_version ) )
880 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
881 return;
884 if( !p_region )
886 #ifdef DEBUG_DVBSUB
887 msg_Dbg( p_dec, "new region: %i", i_id );
888 #endif
889 p_region = *pp_region = calloc( 1, sizeof(dvbsub_region_t) );
890 if( !p_region )
891 return;
892 p_region->p_object_defs = NULL;
893 p_region->p_pixbuf = NULL;
894 p_region->p_next = NULL;
897 /* Region attributes */
898 p_region->i_id = i_id;
899 p_region->i_version = i_version;
900 b_fill = bs_read( s, 1 );
901 bs_skip( s, 3 ); /* Reserved */
903 i_width = bs_read( s, 16 );
904 i_height = bs_read( s, 16 );
905 #ifdef DEBUG_DVBSUB
906 msg_Dbg( p_dec, " width=%d height=%d", i_width, i_height );
907 #endif
908 i_level_comp = bs_read( s, 3 );
909 i_depth = bs_read( s, 3 );
910 bs_skip( s, 2 ); /* Reserved */
911 i_clut = bs_read( s, 8 );
913 i_8_bg = bs_read( s, 8 );
914 i_4_bg = bs_read( s, 4 );
915 i_2_bg = bs_read( s, 2 );
916 bs_skip( s, 2 ); /* Reserved */
918 /* Free old object defs */
919 while( p_region->i_object_defs )
920 free( p_region->p_object_defs[--p_region->i_object_defs].psz_text );
922 free( p_region->p_object_defs );
923 p_region->p_object_defs = NULL;
925 /* Extra sanity checks */
926 if( ( p_region->i_width != i_width ) ||
927 ( p_region->i_height != i_height ) )
929 if( p_region->p_pixbuf )
931 msg_Dbg( p_dec, "region size changed (%dx%d->%dx%d)",
932 p_region->i_width, p_region->i_height, i_width, i_height );
933 free( p_region->p_pixbuf );
936 p_region->p_pixbuf = xmalloc( i_height * i_width );
937 p_region->i_depth = 0;
938 b_fill = true;
940 if( p_region->i_depth &&
941 ( ( p_region->i_depth != i_depth ) ||
942 ( p_region->i_level_comp != i_level_comp ) ||
943 ( p_region->i_clut != i_clut) ) )
945 msg_Dbg( p_dec, "region parameters changed (not allowed)" );
948 /* Erase background of region */
949 if( b_fill )
951 int i_background = ( i_depth == 1 ) ? i_2_bg :
952 ( ( i_depth == 2 ) ? i_4_bg : i_8_bg );
953 memset( p_region->p_pixbuf, i_background, i_width * i_height );
956 p_region->i_width = i_width;
957 p_region->i_height = i_height;
958 p_region->i_level_comp = i_level_comp;
959 p_region->i_depth = i_depth;
960 p_region->i_clut = i_clut;
962 /* List of objects in the region */
963 i_processed_length = 10;
964 while( i_processed_length < i_segment_length )
966 dvbsub_objectdef_t *p_obj;
968 /* We create a new object */
969 p_region->i_object_defs++;
970 p_region->p_object_defs = xrealloc( p_region->p_object_defs,
971 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
973 /* We parse object properties */
974 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
975 p_obj->i_id = bs_read( s, 16 );
976 p_obj->i_type = bs_read( s, 2 );
977 bs_skip( s, 2 ); /* Provider */
978 p_obj->i_x = bs_read( s, 12 );
979 bs_skip( s, 4 ); /* Reserved */
980 p_obj->i_y = bs_read( s, 12 );
981 p_obj->psz_text = NULL;
983 i_processed_length += 6;
985 if( ( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ) ||
986 ( p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING ) )
988 p_obj->i_fg_pc = bs_read( s, 8 );
989 p_obj->i_bg_pc = bs_read( s, 8 );
990 i_processed_length += 2;
995 /* ETSI 300 743 [7.2.1] */
996 static void decode_display_definition( decoder_t *p_dec, bs_t *s, uint16_t i_segment_length )
998 decoder_sys_t *p_sys = p_dec->p_sys;
999 uint16_t i_processed_length = 40;
1000 int i_version;
1002 i_version = bs_read( s, 4 );
1004 /* Check version number */
1005 if( p_sys->display.i_version == i_version )
1007 /* The definition did not change */
1008 bs_skip( s, 8*i_segment_length - 4 );
1009 return;
1012 #ifdef DEBUG_DVBSUB
1013 msg_Dbg( p_dec, "new display definition: %i", i_version );
1014 #endif
1016 /* We don't have this version of the display definition: Parse it */
1017 p_sys->display.i_version = i_version;
1018 p_sys->display.b_windowed = bs_read( s, 1 );
1019 bs_skip( s, 3 ); /* Reserved bits */
1020 p_sys->display.i_width = bs_read( s, 16 )+1;
1021 p_sys->display.i_height = bs_read( s, 16 )+1;
1023 if( p_sys->display.b_windowed )
1025 #ifdef DEBUG_DVBSUB
1026 msg_Dbg( p_dec, "display definition with offsets (windowed)" );
1027 #endif
1028 /* Coordinates are measured from the top left corner */
1029 p_sys->display.i_x = bs_read( s, 16 );
1030 p_sys->display.i_max_x = bs_read( s, 16 );
1031 p_sys->display.i_y = bs_read( s, 16 );
1032 p_sys->display.i_max_y = bs_read( s, 16 );
1033 i_processed_length += 64;
1035 else
1037 /* if not windowed, setup the window variables to good defaults */
1038 /* not necessary, but to avoid future confusion.. */
1039 p_sys->display.i_x = 0;
1040 p_sys->display.i_max_x = p_sys->display.i_width-1;
1041 p_sys->display.i_y = 0;
1042 p_sys->display.i_max_y = p_sys->display.i_height-1;
1045 if( i_processed_length != i_segment_length*8 )
1047 msg_Err( p_dec, "processed length %d bytes != segment length %d bytes",
1048 i_processed_length / 8 , i_segment_length );
1051 #ifdef DEBUG_DVBSUB
1052 msg_Dbg( p_dec, "version: %d, width: %d, height: %d",
1053 p_sys->display.i_version, p_sys->display.i_width, p_sys->display.i_height );
1054 if( p_sys->display.b_windowed )
1055 msg_Dbg( p_dec, "xmin: %d, xmax: %d, ymin: %d, ymax: %d",
1056 p_sys->display.i_x, p_sys->display.i_max_x, p_sys->display.i_y, p_sys->display.i_max_y );
1057 #endif
1060 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
1061 uint8_t *, int );
1062 static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
1063 static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
1064 static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
1066 static void decode_object( decoder_t *p_dec, bs_t *s, uint16_t i_segment_length )
1068 decoder_sys_t *p_sys = p_dec->p_sys;
1069 dvbsub_region_t *p_region;
1070 int i_coding_method, i_id, i;
1072 /* ETSI 300-743 paragraph 7.2.4
1073 * sync_byte, segment_type and page_id have already been processed.
1075 i_id = bs_read( s, 16 );
1076 bs_skip( s, 4 ); /* version */
1077 i_coding_method = bs_read( s, 2 );
1079 if( i_coding_method > 1 )
1081 msg_Dbg( p_dec, "unknown DVB subtitling coding %d is not handled!", i_coding_method );
1082 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1083 return;
1086 /* Check if the object needs to be rendered in at least one
1087 * of the regions */
1088 for( p_region = p_sys->p_regions; p_region != NULL;
1089 p_region = p_region->p_next )
1091 for( i = 0; i < p_region->i_object_defs; i++ )
1092 if( p_region->p_object_defs[i].i_id == i_id ) break;
1094 if( i != p_region->i_object_defs ) break;
1096 if( !p_region )
1098 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1099 return;
1102 #ifdef DEBUG_DVBSUB
1103 msg_Dbg( p_dec, "new object: %i", i_id );
1104 #endif
1106 bs_skip( s, 1 ); /* non_modify_color */
1107 bs_skip( s, 1 ); /* Reserved */
1109 if( i_coding_method == 0x00 )
1111 int i_topfield, i_bottomfield;
1112 uint8_t *p_topfield, *p_bottomfield;
1114 i_topfield = bs_read( s, 16 );
1115 i_bottomfield = bs_read( s, 16 );
1116 p_topfield = s->p_start + bs_pos( s ) / 8;
1117 p_bottomfield = p_topfield + i_topfield;
1119 bs_skip( s, 8 * (i_segment_length - 7) );
1121 /* Sanity check */
1122 if( ( i_segment_length < ( i_topfield + i_bottomfield + 7 ) ) ||
1123 ( ( p_topfield + i_topfield + i_bottomfield ) > s->p_end ) )
1125 msg_Dbg( p_dec, "corrupted object data" );
1126 return;
1129 for( p_region = p_sys->p_regions; p_region != NULL;
1130 p_region = p_region->p_next )
1132 for( i = 0; i < p_region->i_object_defs; i++ )
1134 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1136 dvbsub_render_pdata( p_dec, p_region,
1137 p_region->p_object_defs[i].i_x,
1138 p_region->p_object_defs[i].i_y,
1139 p_topfield, i_topfield );
1141 if( i_bottomfield )
1143 dvbsub_render_pdata( p_dec, p_region,
1144 p_region->p_object_defs[i].i_x,
1145 p_region->p_object_defs[i].i_y + 1,
1146 p_bottomfield, i_bottomfield );
1148 else
1150 /* Duplicate the top field */
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_topfield, i_topfield );
1159 else
1161 /* DVB subtitling as characters */
1162 int i_number_of_codes = bs_read( s, 8 );
1163 uint8_t* p_start = s->p_start + bs_pos( s ) / 8;
1165 /* Sanity check */
1166 if( ( i_segment_length < ( i_number_of_codes*2 + 4 ) ) ||
1167 ( ( p_start + i_number_of_codes*2 ) > s->p_end ) )
1169 msg_Dbg( p_dec, "corrupted object data" );
1170 return;
1173 for( p_region = p_sys->p_regions; p_region != NULL;
1174 p_region = p_region->p_next )
1176 for( i = 0; i < p_region->i_object_defs; i++ )
1178 int j;
1180 if( p_region->p_object_defs[i].i_id != i_id ) continue;
1182 p_region->p_object_defs[i].psz_text =
1183 xrealloc( p_region->p_object_defs[i].psz_text,
1184 i_number_of_codes + 1 );
1186 /* FIXME 16bits -> char ??? See Preamble */
1187 for( j = 0; j < i_number_of_codes; j++ )
1189 p_region->p_object_defs[i].psz_text[j] = (char)(bs_read( s, 16 ) & 0xFF);
1191 /* Null terminate the string */
1192 p_region->p_object_defs[i].psz_text[j] = 0;
1197 #ifdef DEBUG_DVBSUB
1198 msg_Dbg( p_dec, "end object: %i", i_id );
1199 #endif
1202 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
1203 int i_x, int i_y,
1204 uint8_t *p_field, int i_field )
1206 uint8_t *p_pixbuf;
1207 int i_offset = 0;
1208 bs_t bs;
1210 /* Sanity check */
1211 if( !p_region->p_pixbuf )
1213 msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
1214 return;
1216 if( i_y < 0 || i_x < 0 || i_y >= p_region->i_height ||
1217 i_x >= p_region->i_width )
1219 msg_Dbg( p_dec, "invalid offset (%i,%i)", i_x, i_y );
1220 return;
1223 p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
1224 bs_init( &bs, p_field, i_field );
1226 while( !bs_eof( &bs ) )
1228 /* Sanity check */
1229 if( i_y >= p_region->i_height ) return;
1231 switch( bs_read( &bs, 8 ) )
1233 case 0x10:
1234 dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1235 &i_offset );
1236 break;
1238 case 0x11:
1239 dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1240 &i_offset );
1241 break;
1243 case 0x12:
1244 dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1245 &i_offset );
1246 break;
1248 case 0x20:
1249 case 0x21:
1250 case 0x22:
1251 /* We don't use map tables */
1252 break;
1254 case 0xf0: /* End of line code */
1255 p_pixbuf += 2*p_region->i_width;
1256 i_offset = 0; i_y += 2;
1257 break;
1262 static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1264 bool b_stop = false;
1266 while( !b_stop && !bs_eof( s ) )
1268 int i_count = 0, i_color = 0;
1270 i_color = bs_read( s, 2 );
1271 if( i_color != 0x00 )
1273 i_count = 1;
1275 else
1277 if( bs_read( s, 1 ) == 0x01 ) // Switch1
1279 i_count = 3 + bs_read( s, 3 );
1280 i_color = bs_read( s, 2 );
1282 else
1284 if( bs_read( s, 1 ) == 0x00 ) //Switch2
1286 switch( bs_read( s, 2 ) ) //Switch3
1288 case 0x00:
1289 b_stop = true;
1290 break;
1291 case 0x01:
1292 i_count = 2;
1293 break;
1294 case 0x02:
1295 i_count = 12 + bs_read( s, 4 );
1296 i_color = bs_read( s, 2 );
1297 break;
1298 case 0x03:
1299 i_count = 29 + bs_read( s, 8 );
1300 i_color = bs_read( s, 2 );
1301 break;
1302 default:
1303 break;
1306 else
1308 /* 1 pixel color 0 */
1309 i_count = 1;
1314 if( !i_count ) continue;
1316 /* Sanity check */
1317 if( ( i_count + *pi_off ) > i_width ) break;
1319 if( i_count == 1 ) p[*pi_off] = i_color;
1320 else memset( ( p + *pi_off ), i_color, i_count );
1322 (*pi_off) += i_count;
1325 bs_align( s );
1328 static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1330 bool b_stop = false;
1332 while( !b_stop && !bs_eof( s ) )
1334 int i_count = 0, i_color = 0;
1336 i_color = bs_read( s, 4 );
1337 if( i_color != 0x00 )
1339 /* Add 1 pixel */
1340 i_count = 1;
1342 else
1344 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1346 i_count = bs_read( s, 3 );
1347 if( i_count != 0x00 )
1349 i_count += 2;
1351 else b_stop = true;
1353 else
1355 if( bs_read( s, 1 ) == 0x00) //Switch2
1357 i_count = 4 + bs_read( s, 2 );
1358 i_color = bs_read( s, 4 );
1360 else
1362 switch ( bs_read( s, 2 ) ) //Switch3
1364 case 0x0:
1365 i_count = 1;
1366 break;
1367 case 0x1:
1368 i_count = 2;
1369 break;
1370 case 0x2:
1371 i_count = 9 + bs_read( s, 4 );
1372 i_color = bs_read( s, 4 );
1373 break;
1374 case 0x3:
1375 i_count= 25 + bs_read( s, 8 );
1376 i_color = bs_read( s, 4 );
1377 break;
1383 if( !i_count ) continue;
1385 /* Sanity check */
1386 if( ( i_count + *pi_off ) > i_width ) break;
1388 if( i_count == 1 ) p[*pi_off] = i_color;
1389 else memset( ( p + *pi_off ), i_color, i_count );
1391 (*pi_off) += i_count;
1394 bs_align( s );
1397 static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1399 bool b_stop = false;
1401 while( !b_stop && !bs_eof( s ) )
1403 int i_count = 0, i_color = 0;
1405 i_color = bs_read( s, 8 );
1406 if( i_color != 0x00 )
1408 /* Add 1 pixel */
1409 i_count = 1;
1411 else
1413 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1415 i_count = bs_read( s, 7 );
1416 if( i_count == 0x00 )
1417 b_stop = true;
1419 else
1421 i_count = bs_read( s, 7 );
1422 i_color = bs_read( s, 8 );
1426 if( !i_count ) continue;
1428 /* Sanity check */
1429 if( ( i_count + *pi_off ) > i_width ) break;
1431 if( i_count == 1 ) p[*pi_off] = i_color;
1432 else memset( ( p + *pi_off ), i_color, i_count );
1434 (*pi_off) += i_count;
1437 bs_align( s );
1440 static void free_all( decoder_t *p_dec )
1442 decoder_sys_t *p_sys = p_dec->p_sys;
1443 dvbsub_region_t *p_reg, *p_reg_next;
1444 dvbsub_clut_t *p_clut, *p_clut_next;
1446 /*free( p_sys->p_display ); No longer malloced */
1448 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
1450 p_clut_next = p_clut->p_next;
1451 free( p_clut );
1453 p_sys->p_cluts = NULL;
1455 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1457 p_reg_next = p_reg->p_next;
1458 for( int i = 0; i < p_reg->i_object_defs; i++ )
1459 free( p_reg->p_object_defs[i].psz_text );
1460 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1461 free( p_reg->p_pixbuf );
1462 free( p_reg );
1464 p_sys->p_regions = NULL;
1466 if( p_sys->p_page )
1468 if( p_sys->p_page->i_region_defs )
1469 free( p_sys->p_page->p_region_defs );
1470 free( p_sys->p_page );
1472 p_sys->p_page = NULL;
1475 static subpicture_t *render( decoder_t *p_dec )
1477 decoder_sys_t *p_sys = p_dec->p_sys;
1478 subpicture_t *p_spu;
1479 subpicture_region_t **pp_spu_region;
1480 int i, j;
1481 int i_base_x;
1482 int i_base_y;
1484 /* Allocate the subpicture internal data. */
1485 p_spu = decoder_NewSubpicture( p_dec, NULL );
1486 if( !p_spu )
1487 return NULL;
1489 p_spu->b_absolute = p_sys->b_absolute;
1490 /* Set the pf_render callback */
1491 p_spu->i_start = p_sys->i_pts;
1492 //p_spu->i_stop = (vlc_tick_t) 0;
1493 p_spu->b_ephemer = true;
1494 //p_spu->b_fade = true;
1495 //p_spu->i_stop = p_spu->i_start + (vlc_tick_t) (i_timeout * 1000000);
1496 p_spu->b_subtitle = true;
1498 /* Correct positioning of SPU */
1499 i_base_x = p_sys->i_spu_x;
1500 i_base_y = p_sys->i_spu_y;
1502 p_spu->i_original_picture_width = p_sys->display.i_width;
1503 p_spu->i_original_picture_height = p_sys->display.i_height;
1505 if( p_sys->display.b_windowed )
1507 /* From en_300743v01 - */
1508 /* the DDS is there to indicate intended size/position of text */
1509 /* the intended video area is ->i_width/height */
1510 /* the window is within this... SO... we should leave i_original_picture_width etc. as is */
1511 /* and ONLY change i_base_x. effectively i_max_x/y are only there to limit memory requirements*/
1512 /* we COULD use the upper limits to limit rendering to within these? */
1514 /* see notes on DDS at the top of the file */
1515 i_base_x += p_sys->display.i_x;
1516 i_base_y += p_sys->display.i_y;
1519 pp_spu_region = &p_spu->p_region;
1521 /* Loop on region definitions */
1522 #ifdef DEBUG_DVBSUB
1523 if( p_sys->p_page )
1524 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1525 #endif
1527 for( i = 0; p_sys->p_page && ( i < p_sys->p_page->i_region_defs ); i++ )
1529 dvbsub_region_t *p_region;
1530 dvbsub_regiondef_t *p_regiondef;
1531 dvbsub_clut_t *p_clut;
1532 dvbsub_color_t *p_color;
1533 subpicture_region_t *p_spu_region;
1534 uint8_t *p_src, *p_dst;
1535 video_format_t fmt;
1536 video_palette_t palette;
1537 int i_pitch;
1539 p_regiondef = &p_sys->p_page->p_region_defs[i];
1541 /* Find associated region */
1542 for( p_region = p_sys->p_regions; p_region != NULL;
1543 p_region = p_region->p_next )
1545 if( p_regiondef->i_id == p_region->i_id ) break;
1548 #ifdef DEBUG_DVBSUB
1549 /* if a region exists, then print it's size */
1550 if (p_region)
1552 msg_Dbg( p_dec, "rendering region %i (%i,%i) to (%i,%i)", i,
1553 p_regiondef->i_x, p_regiondef->i_y,
1554 p_regiondef->i_x + p_region->i_width,
1555 p_regiondef->i_y + p_region->i_height );
1557 else
1559 msg_Dbg( p_dec, "rendering region %i (%i,%i) (no region matched to render)", i,
1560 p_regiondef->i_x, p_regiondef->i_y );
1562 #endif
1564 if( !p_region )
1566 msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
1567 continue;
1570 /* Find associated CLUT */
1571 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
1573 if( p_region->i_clut == p_clut->i_id ) break;
1575 if( !p_clut )
1577 msg_Dbg( p_dec, "clut %i not found", p_region->i_clut );
1578 continue;
1581 /* FIXME: don't create a subpicture region with VLC CODEC YUVP
1582 * when it actually is a TEXT region */
1584 /* Create new SPU region */
1585 video_format_Init( &fmt, VLC_CODEC_YUVP );
1586 fmt.i_sar_num = 0; /* 0 means use aspect ratio of background video */
1587 fmt.i_sar_den = 1;
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 );
1605 fmt.p_palette = NULL; /* was stack var */
1606 video_format_Clean( &fmt );
1607 if( !p_spu_region )
1609 msg_Err( p_dec, "cannot allocate SPU region" );
1610 continue;
1612 p_spu_region->i_x = i_base_x + p_regiondef->i_x;
1613 p_spu_region->i_y = i_base_y + p_regiondef->i_y;
1614 p_spu_region->i_align = p_sys->i_spu_position;
1615 *pp_spu_region = p_spu_region;
1616 pp_spu_region = &p_spu_region->p_next;
1618 p_src = p_region->p_pixbuf;
1619 p_dst = p_spu_region->p_picture->Y_PIXELS;
1620 i_pitch = p_spu_region->p_picture->Y_PITCH;
1622 /* Copy pixel buffer */
1623 for( j = 0; j < p_region->i_height; j++ )
1625 memcpy( p_dst, p_src, p_region->i_width );
1626 p_src += p_region->i_width;
1627 p_dst += i_pitch;
1630 /* Check subtitles encoded as strings of characters
1631 * (since there are not rendered in the pixbuffer) */
1632 for( j = 0; j < p_region->i_object_defs; j++ )
1634 dvbsub_objectdef_t *p_object_def = &p_region->p_object_defs[j];
1636 if( ( p_object_def->i_type != 1 ) || !p_object_def->psz_text )
1637 continue;
1639 /* Create new SPU region */
1640 video_format_Init( &fmt, VLC_CODEC_TEXT );
1641 fmt.i_sar_num = 1;
1642 fmt.i_sar_den = 1;
1643 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1644 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1645 fmt.i_x_offset = fmt.i_y_offset = 0;
1646 p_spu_region = subpicture_region_New( &fmt );
1647 video_format_Clean( &fmt );
1649 p_spu_region->p_text = text_segment_New( p_object_def->psz_text );
1650 p_spu_region->i_x = i_base_x + p_regiondef->i_x + p_object_def->i_x;
1651 p_spu_region->i_y = i_base_y + p_regiondef->i_y + p_object_def->i_y;
1652 p_spu_region->i_align = p_sys->i_spu_position;
1653 *pp_spu_region = p_spu_region;
1654 pp_spu_region = &p_spu_region->p_next;
1658 return p_spu;
1661 /*****************************************************************************
1662 * encoder_sys_t : encoder descriptor
1663 *****************************************************************************/
1664 typedef struct encoder_region_t
1666 int i_width;
1667 int i_height;
1669 } encoder_region_t;
1671 typedef struct
1673 unsigned int i_page_ver;
1674 unsigned int i_region_ver;
1675 unsigned int i_clut_ver;
1677 int i_regions;
1678 encoder_region_t *p_regions;
1680 vlc_tick_t i_pts;
1682 /* subpicture positioning */
1683 int i_offset_x;
1684 int i_offset_y;
1685 } encoder_sys_t;
1687 #ifdef ENABLE_SOUT
1688 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1689 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1690 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1691 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1693 /*****************************************************************************
1694 * OpenEncoder: probe the encoder and return score
1695 *****************************************************************************/
1696 static int OpenEncoder( vlc_object_t *p_this )
1698 encoder_t *p_enc = (encoder_t *)p_this;
1699 encoder_sys_t *p_sys;
1701 if( ( p_enc->fmt_out.i_codec != VLC_CODEC_DVBS ) &&
1702 !p_enc->obj.force )
1704 return VLC_EGENERIC;
1707 /* Allocate the memory needed to store the decoder's structure */
1708 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1709 return VLC_ENOMEM;
1710 p_enc->p_sys = p_sys;
1712 p_enc->pf_encode_sub = Encode;
1713 p_enc->fmt_out.i_codec = VLC_CODEC_DVBS;
1714 p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1;
1716 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1718 p_sys->i_page_ver = 0;
1719 p_sys->i_region_ver = 0;
1720 p_sys->i_clut_ver = 0;
1721 p_sys->i_regions = 0;
1722 p_sys->p_regions = 0;
1724 p_sys->i_offset_x = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "x" );
1725 p_sys->i_offset_y = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "y" );
1727 return VLC_SUCCESS;
1730 /* FIXME: this routine is a hack to convert VLC_CODEC_YUVA
1731 * into VLC_CODEC_YUVP
1733 static subpicture_t *YuvaYuvp( subpicture_t *p_subpic )
1735 subpicture_region_t *p_region = NULL;
1737 if( !p_subpic ) return NULL;
1739 for( p_region = p_subpic->p_region; p_region; p_region = p_region->p_next )
1741 video_format_t *p_fmt = &p_region->fmt;
1742 int i = 0, j = 0, n = 0, p = 0;
1743 int i_max_entries = 256;
1745 #ifdef RANDOM_DITHERING
1746 int i_seed = 0xdeadbeef; /* random seed */
1747 #else
1748 int *pi_delta;
1749 #endif
1750 int i_pixels = p_region->p_picture->p[0].i_visible_lines
1751 * p_region->p_picture->p[0].i_pitch;
1752 int i_iterator = p_region->p_picture->p[0].i_visible_lines * 3 / 4
1753 * p_region->p_picture->p[0].i_pitch
1754 + p_region->p_picture->p[0].i_pitch * 1 / 3;
1755 int i_tolerance = 0;
1757 #ifdef DEBUG_DVBSUB1
1758 /* p_enc not valid here */
1759 msg_Dbg( p_enc, "YuvaYuvp: i_pixels=%d, i_iterator=%d", i_pixels, i_iterator );
1760 #endif
1761 p_fmt->i_chroma = VLC_CODEC_YUVP;
1762 p_fmt->p_palette = (video_palette_t *) malloc( sizeof( video_palette_t ) );
1763 if( !p_fmt->p_palette ) break;
1764 p_fmt->p_palette->i_entries = 0;
1766 /* Find best iterator using Euclide’s algorithm */
1767 for( ; i_iterator > 1 ; i_iterator-- )
1769 int a = i_pixels;
1770 int b = i_iterator;
1771 int c;
1773 while( b )
1775 c = a % b;
1776 a = b;
1777 b = c;
1780 if( a == 1 )
1782 break;
1786 /* Count colors, build best palette */
1787 for( i_tolerance = 0; i_tolerance < 128; i_tolerance++ )
1789 bool b_success = true;
1790 p_fmt->p_palette->i_entries = 0;
1792 for( i = 0; i < i_pixels ; )
1794 uint8_t y, u, v, a;
1795 y = p_region->p_picture->p[0].p_pixels[i];
1796 u = p_region->p_picture->p[1].p_pixels[i];
1797 v = p_region->p_picture->p[2].p_pixels[i];
1798 a = p_region->p_picture->p[3].p_pixels[i];
1799 for( j = 0; j < p_fmt->p_palette->i_entries; j++ )
1801 if( abs((int)p_fmt->p_palette->palette[j][0] - (int)y) <= i_tolerance &&
1802 abs((int)p_fmt->p_palette->palette[j][1] - (int)u) <= i_tolerance &&
1803 abs((int)p_fmt->p_palette->palette[j][2] - (int)v) <= i_tolerance &&
1804 abs((int)p_fmt->p_palette->palette[j][3] - (int)a) <= i_tolerance / 2 )
1806 break;
1809 if( j == p_fmt->p_palette->i_entries )
1811 p_fmt->p_palette->palette[j][0] = y;
1812 p_fmt->p_palette->palette[j][1] = u;
1813 p_fmt->p_palette->palette[j][2] = v;
1814 p_fmt->p_palette->palette[j][3] = a;
1815 p_fmt->p_palette->i_entries++;
1817 if( p_fmt->p_palette->i_entries >= i_max_entries )
1819 b_success = false;
1820 break;
1822 i += i_iterator;
1823 if( i > i_pixels )
1825 i -= i_pixels;
1829 if( b_success )
1831 break;
1835 #ifdef DEBUG_DVBSUB1
1836 /* p_enc not valid here */
1837 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1838 #endif
1840 #ifndef RANDOM_DITHERING
1841 pi_delta = xmalloc( ( p_region->p_picture->p[0].i_pitch + 1 )
1842 * sizeof(int) * 4 );
1843 for( i = 0; i < (p_region->p_picture->p[0].i_pitch + 1) * 4 ; i++ )
1845 pi_delta[ i ] = 0;
1847 #endif
1849 /* Fill image with our new colours */
1850 for( p = 0; p < p_region->p_picture->p[0].i_visible_lines ; p++ )
1852 int i_ydelta = 0, i_udelta = 0, i_vdelta = 0, i_adelta = 0;
1854 for( n = 0; n < p_region->p_picture->p[0].i_pitch ; n++ )
1856 int i_offset = p * p_region->p_picture->p[0].i_pitch + n;
1857 int y, u, v, a;
1858 int i_mindist, i_best;
1860 y = (int)p_region->p_picture->p[0].p_pixels[i_offset];
1861 u = (int)p_region->p_picture->p[1].p_pixels[i_offset];
1862 v = (int)p_region->p_picture->p[2].p_pixels[i_offset];
1863 a = (int)p_region->p_picture->p[3].p_pixels[i_offset];
1865 /* Add dithering compensation */
1866 #ifdef RANDOM_DITHERING
1867 y += ((i_seed & 0xff) - 0x80) * i_tolerance / 0x80;
1868 u += (((i_seed >> 8) & 0xff) - 0x80) * i_tolerance / 0x80;
1869 v += (((i_seed >> 16) & 0xff) - 0x80) * i_tolerance / 0x80;
1870 a += (((i_seed >> 24) & 0xff) - 0x80) * i_tolerance / 0x80;
1871 #else
1872 y += i_ydelta + pi_delta[ n * 4 ];
1873 u += i_udelta + pi_delta[ n * 4 + 1 ];
1874 v += i_vdelta + pi_delta[ n * 4 + 2 ];
1875 a += i_adelta + pi_delta[ n * 4 + 3 ];
1876 #endif
1878 /* Find best colour in palette */
1879 for( i_mindist = 99999999, i_best = 0, j = 0; j < p_fmt->p_palette->i_entries; j++ )
1881 int i_dist = 0;
1883 i_dist += abs((int)p_fmt->p_palette->palette[j][0] - y);
1884 i_dist += abs((int)p_fmt->p_palette->palette[j][1] - u);
1885 i_dist += abs((int)p_fmt->p_palette->palette[j][2] - v);
1886 i_dist += 2 * abs((int)p_fmt->p_palette->palette[j][3] - a);
1888 if( i_dist < i_mindist )
1890 i_mindist = i_dist;
1891 i_best = j;
1895 /* Set pixel to best color */
1896 p_region->p_picture->p[0].p_pixels[i_offset] = i_best;
1898 /* Update dithering state */
1899 #ifdef RANDOM_DITHERING
1900 i_seed = (i_seed * 0x1283837) ^ 0x789479 ^ (i_seed >> 13);
1901 #else
1902 i_ydelta = y - (int)p_fmt->p_palette->palette[i_best][0];
1903 i_udelta = u - (int)p_fmt->p_palette->palette[i_best][1];
1904 i_vdelta = v - (int)p_fmt->p_palette->palette[i_best][2];
1905 i_adelta = a - (int)p_fmt->p_palette->palette[i_best][3];
1906 pi_delta[ n * 4 ] = i_ydelta * 3 / 8;
1907 pi_delta[ n * 4 + 1 ] = i_udelta * 3 / 8;
1908 pi_delta[ n * 4 + 2 ] = i_vdelta * 3 / 8;
1909 pi_delta[ n * 4 + 3 ] = i_adelta * 3 / 8;
1910 i_ydelta = i_ydelta * 5 / 8;
1911 i_udelta = i_udelta * 5 / 8;
1912 i_vdelta = i_vdelta * 5 / 8;
1913 i_adelta = i_adelta * 5 / 8;
1914 #endif
1917 #ifndef RANDOM_DITHERING
1918 free( pi_delta );
1919 #endif
1921 /* pad palette */
1922 for( i = p_fmt->p_palette->i_entries; i < i_max_entries; i++ )
1924 p_fmt->p_palette->palette[i][0] = 0;
1925 p_fmt->p_palette->palette[i][1] = 0;
1926 p_fmt->p_palette->palette[i][2] = 0;
1927 p_fmt->p_palette->palette[i][3] = 0;
1929 p_fmt->p_palette->i_entries = i_max_entries;
1930 #ifdef DEBUG_DVBSUB1
1931 /* p_enc not valid here */
1932 msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
1933 #endif
1935 return p_subpic;
1936 } /* End of hack */
1938 /****************************************************************************
1939 * Encode: the whole thing
1940 ****************************************************************************/
1941 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1943 subpicture_t *p_temp = NULL;
1944 subpicture_region_t *p_region = NULL;
1945 bs_t bits, *s = &bits;
1946 block_t *p_block;
1948 if( !p_subpic || !p_subpic->p_region ) return NULL;
1950 /* FIXME: this is a hack to convert VLC_CODEC_YUVA into
1951 * VLC_CODEC_YUVP
1953 p_region = p_subpic->p_region;
1954 if( p_region->fmt.i_chroma == VLC_CODEC_YUVA )
1956 p_temp = YuvaYuvp( p_subpic );
1957 if( !p_temp )
1959 msg_Err( p_enc, "no picture in subpicture" );
1960 return NULL;
1962 p_region = p_subpic->p_region;
1965 /* Sanity check */
1966 if( !p_region ) return NULL;
1968 if( ( p_region->fmt.i_chroma != VLC_CODEC_TEXT ) &&
1969 ( p_region->fmt.i_chroma != VLC_CODEC_YUVP ) )
1971 msg_Err( p_enc, "chroma %4.4s not supported", (char *)&p_region->fmt.i_chroma );
1972 return NULL;
1975 if( p_region->fmt.p_palette )
1977 switch( p_region->fmt.p_palette->i_entries )
1979 case 0:
1980 case 4:
1981 case 16:
1982 case 256:
1983 break;
1984 default:
1985 msg_Err( p_enc, "subpicture palette (%d) not handled",
1986 p_region->fmt.p_palette->i_entries );
1987 return NULL;
1990 /* End of hack */
1992 #ifdef DEBUG_DVBSUB
1993 msg_Dbg( p_enc, "encoding subpicture" );
1994 #endif
1995 p_block = block_Alloc( 64000 );
1996 bs_init( s, p_block->p_buffer, p_block->i_buffer );
1998 bs_write( s, 8, 0x20 ); /* Data identifier */
1999 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
2001 encode_page_composition( p_enc, s, p_subpic );
2002 encode_region_composition( p_enc, s, p_subpic );
2003 encode_clut( p_enc, s, p_subpic );
2004 encode_object( p_enc, s, p_subpic );
2006 /* End of display */
2007 bs_write( s, 8, 0x0f ); /* Sync byte */
2008 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
2009 bs_write( s, 16, 1 ); /* Page id */
2010 bs_write( s, 16, 0 ); /* Segment length */
2012 bs_write( s, 8, 0xff );/* End marker */
2013 p_block->i_buffer = bs_pos( s ) / 8;
2014 p_block->i_pts = p_block->i_dts = p_subpic->i_start;
2015 if( !p_subpic->b_ephemer && ( p_subpic->i_stop > p_subpic->i_start ) )
2017 block_t *p_block_stop;
2019 p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
2021 /* Send another (empty) subtitle to signal the end of display */
2022 p_block_stop = block_Alloc( 64000 );
2023 bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
2024 bs_write( s, 8, 0x20 ); /* Data identifier */
2025 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
2026 encode_page_composition( p_enc, s, 0 );
2027 bs_write( s, 8, 0x0f ); /* Sync byte */
2028 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
2029 bs_write( s, 16, 1 ); /* Page id */
2030 bs_write( s, 16, 0 ); /* Segment length */
2031 bs_write( s, 8, 0xff );/* End marker */
2032 p_block_stop->i_buffer = bs_pos( s ) / 8;
2033 p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
2034 block_ChainAppend( &p_block, p_block_stop );
2035 p_block_stop->i_length = VLC_TICK_FROM_MS(100); /* p_subpic->i_stop - p_subpic->i_start; */
2037 #ifdef DEBUG_DVBSUB
2038 msg_Dbg( p_enc, "subpicture encoded properly" );
2039 #endif
2040 return p_block;
2043 /*****************************************************************************
2044 * CloseEncoder: encoder destruction
2045 *****************************************************************************/
2046 static void CloseEncoder( vlc_object_t *p_this )
2048 encoder_t *p_enc = (encoder_t *)p_this;
2049 encoder_sys_t *p_sys = p_enc->p_sys;
2051 var_Destroy( p_this , ENC_CFG_PREFIX "x" );
2052 var_Destroy( p_this , ENC_CFG_PREFIX "y" );
2053 var_Destroy( p_this , ENC_CFG_PREFIX "timeout" );
2055 if( p_sys->i_regions ) free( p_sys->p_regions );
2056 free( p_sys );
2059 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
2060 subpicture_t *p_subpic )
2062 encoder_sys_t *p_sys = p_enc->p_sys;
2063 subpicture_region_t *p_region;
2064 bool b_mode_change = false;
2065 int i_regions, i_timeout;
2067 bs_write( s, 8, 0x0f ); /* Sync byte */
2068 bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
2069 bs_write( s, 16, 1 ); /* Page id */
2071 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2072 p_region; p_region = p_region->p_next, i_regions++ )
2074 if( i_regions >= p_sys->i_regions )
2076 encoder_region_t region;
2077 region.i_width = region.i_height = 0;
2078 p_sys->p_regions = xrealloc( p_sys->p_regions,
2079 sizeof(encoder_region_t) * (p_sys->i_regions + 1) );
2080 p_sys->p_regions[p_sys->i_regions++] = region;
2083 if( ( p_sys->p_regions[i_regions].i_width <
2084 (int)p_region->fmt.i_visible_width ) ||
2085 ( p_sys->p_regions[i_regions].i_width >
2086 (int)p_region->fmt.i_visible_width ) )
2088 b_mode_change = true;
2089 msg_Dbg( p_enc, "region %i width change: %i -> %i",
2090 i_regions, p_sys->p_regions[i_regions].i_width,
2091 p_region->fmt.i_visible_width );
2092 p_sys->p_regions[i_regions].i_width =
2093 p_region->fmt.i_visible_width;
2095 if( p_sys->p_regions[i_regions].i_height <
2096 (int)p_region->fmt.i_visible_height )
2098 b_mode_change = true;
2099 msg_Dbg( p_enc, "region %i height change: %i -> %i",
2100 i_regions, p_sys->p_regions[i_regions].i_height,
2101 p_region->fmt.i_visible_height );
2102 p_sys->p_regions[i_regions].i_height =
2103 p_region->fmt.i_visible_height;
2107 bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
2109 i_timeout = 0;
2110 if( p_subpic && !p_subpic->b_ephemer &&
2111 ( p_subpic->i_stop > p_subpic->i_start ) )
2113 i_timeout = SEC_FROM_VLC_TICK(p_subpic->i_stop - p_subpic->i_start);
2116 bs_write( s, 8, i_timeout ); /* Timeout */
2117 bs_write( s, 4, p_sys->i_page_ver++ );
2118 bs_write( s, 2, b_mode_change ?
2119 DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
2120 bs_write( s, 2, 0 ); /* Reserved */
2122 for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
2123 p_region; p_region = p_region->p_next, i_regions++ )
2125 bs_write( s, 8, i_regions );
2126 bs_write( s, 8, 0 ); /* Reserved */
2127 if( (p_sys->i_offset_x > 0) && (p_sys->i_offset_y > 0) )
2129 bs_write( s, 16, p_sys->i_offset_x ); /* override x position */
2130 bs_write( s, 16, p_sys->i_offset_y ); /* override y position */
2132 else
2134 bs_write( s, 16, p_region->i_x );
2135 bs_write( s, 16, p_region->i_y );
2140 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2142 encoder_sys_t *p_sys = p_enc->p_sys;
2143 subpicture_region_t *p_region = p_subpic->p_region;
2144 video_palette_t *p_pal, pal;
2146 /* Sanity check */
2147 if( !p_region ) return;
2149 if( p_region->fmt.i_chroma == VLC_CODEC_YUVP )
2151 p_pal = p_region->fmt.p_palette;
2153 else
2155 pal.i_entries = 4;
2156 for( int i = 0; i < 4; i++ )
2158 pal.palette[i][0] = 0;
2159 pal.palette[i][1] = 0;
2160 pal.palette[i][2] = 0;
2161 pal.palette[i][3] = 0;
2163 p_pal = &pal;
2166 bs_write( s, 8, 0x0f ); /* Sync byte */
2167 bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
2168 bs_write( s, 16, 1 ); /* Page id */
2170 bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
2171 bs_write( s, 8, 1 ); /* Clut id */
2172 bs_write( s, 4, p_sys->i_clut_ver++ );
2173 bs_write( s, 4, 0 ); /* Reserved */
2175 for( int i = 0; i < p_pal->i_entries; i++ )
2177 bs_write( s, 8, i ); /* Clut entry id */
2178 bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
2179 bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
2180 bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
2181 bs_write( s, 4, 0 ); /* Reserved */
2182 bs_write( s, 1, 1 ); /* Full range flag */
2183 bs_write( s, 8, p_pal->palette[i][3] ? /* Y value */
2184 (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
2185 bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
2186 bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
2187 bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
2191 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
2192 subpicture_t *p_subpic )
2194 encoder_sys_t *p_sys = p_enc->p_sys;
2195 subpicture_region_t *p_region;
2196 int i_region;
2198 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2199 p_region = p_region->p_next, i_region++ )
2201 int i_entries = 4, i_depth = 0x1, i_bg = 0;
2202 bool b_text =
2203 ( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
2205 if( !b_text )
2207 video_palette_t *p_pal = p_region->fmt.p_palette;
2209 if( !p_pal )
2211 msg_Err( p_enc, "subpicture has no palette - ignoring it" );
2212 break;
2215 i_entries = p_pal->i_entries;
2216 i_depth = i_entries == 4 ? 0x1 : i_entries == 16 ? 0x2 : 0x3;
2218 for( i_bg = 0; i_bg < p_pal->i_entries; i_bg++ )
2220 if( !p_pal->palette[i_bg][3] ) break;
2224 bs_write( s, 8, 0x0f ); /* Sync byte */
2225 bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
2226 bs_write( s, 16, 1 ); /* Page id */
2228 bs_write( s, 16, 10 + 6 + (b_text ? 2 : 0) ); /* Segment length */
2229 bs_write( s, 8, i_region );
2230 bs_write( s, 4, p_sys->i_region_ver++ );
2232 /* Region attributes */
2233 bs_write( s, 1, i_bg < i_entries ); /* Fill */
2234 bs_write( s, 3, 0 ); /* Reserved */
2235 bs_write( s, 16, p_sys->p_regions[i_region].i_width );
2236 bs_write( s, 16, p_sys->p_regions[i_region].i_height );
2237 bs_write( s, 3, i_depth ); /* Region level of compatibility */
2238 bs_write( s, 3, i_depth ); /* Region depth */
2239 bs_write( s, 2, 0 ); /* Reserved */
2240 bs_write( s, 8, 1 ); /* Clut id */
2241 bs_write( s, 8, i_bg ); /* region 8bit pixel code */
2242 bs_write( s, 4, i_bg ); /* region 4bit pixel code */
2243 bs_write( s, 2, i_bg ); /* region 2bit pixel code */
2244 bs_write( s, 2, 0 ); /* Reserved */
2246 /* In our implementation we only have 1 object per region */
2247 bs_write( s, 16, i_region );
2248 bs_write( s, 2, b_text ? DVBSUB_OT_BASIC_CHAR:DVBSUB_OT_BASIC_BITMAP );
2249 bs_write( s, 2, 0 ); /* object provider flag */
2250 bs_write( s, 12, 0 );/* object horizontal position */
2251 bs_write( s, 4, 0 ); /* Reserved */
2252 bs_write( s, 12, 0 );/* object vertical position */
2254 if( b_text )
2256 bs_write( s, 8, 1 ); /* foreground pixel code */
2257 bs_write( s, 8, 0 ); /* background pixel code */
2262 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2263 subpicture_region_t *p_region,
2264 bool b_top );
2266 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
2268 encoder_sys_t *p_sys = p_enc->p_sys;
2269 subpicture_region_t *p_region;
2270 int i_region;
2272 int i_length_pos, i_update_pos, i_pixel_data_pos;
2274 for( i_region = 0, p_region = p_subpic->p_region; p_region;
2275 p_region = p_region->p_next, i_region++ )
2277 bs_write( s, 8, 0x0f ); /* Sync byte */
2278 bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
2279 bs_write( s, 16, 1 ); /* Page id */
2281 i_length_pos = bs_pos( s );
2282 bs_write( s, 16, 0 ); /* Segment length */
2283 bs_write( s, 16, i_region ); /* Object id */
2284 bs_write( s, 4, p_sys->i_region_ver++ );
2286 /* object coding method */
2287 switch( p_region->fmt.i_chroma )
2289 case VLC_CODEC_YUVP:
2290 bs_write( s, 2, 0 );
2291 break;
2292 case VLC_CODEC_TEXT:
2293 bs_write( s, 2, 1 );
2294 break;
2295 default:
2296 msg_Err( p_enc, "FOURCC %d not supported by encoder.", p_region->fmt.i_chroma );
2297 continue;
2300 bs_write( s, 1, 0 ); /* non modifying color flag */
2301 bs_write( s, 1, 0 ); /* Reserved */
2303 if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
2305 int i_size, i;
2307 if( !p_region->p_text ) continue;
2309 i_size = __MIN( strlen( p_region->p_text->psz_text ), 256 );
2311 bs_write( s, 8, i_size ); /* number of characters in string */
2312 for( i = 0; i < i_size; i++ )
2314 bs_write( s, 16, p_region->p_text->psz_text[i] );
2317 /* Update segment length */
2318 SetWBE( &s->p_start[i_length_pos/8],
2319 (bs_pos(s) - i_length_pos)/8 -2 );
2320 continue;
2323 /* Coding of a bitmap object */
2324 i_update_pos = bs_pos( s );
2325 bs_write( s, 16, 0 ); /* topfield data block length */
2326 bs_write( s, 16, 0 ); /* bottomfield data block length */
2328 /* Top field */
2329 i_pixel_data_pos = bs_pos( s );
2330 encode_pixel_data( p_enc, s, p_region, true );
2331 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2332 SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
2334 /* Bottom field */
2335 i_pixel_data_pos = bs_pos( s );
2336 encode_pixel_data( p_enc, s, p_region, false );
2337 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
2338 SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
2340 /* Stuffing for word alignment */
2341 bs_align_0( s );
2342 if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
2344 /* Update segment length */
2345 SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
2349 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2350 int i_line );
2351 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2352 int i_line );
2353 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2354 int i_line );
2355 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2356 subpicture_region_t *p_region,
2357 bool b_top )
2359 unsigned int i_line;
2361 /* Sanity check */
2362 if( p_region->fmt.i_chroma != VLC_CODEC_YUVP ) return;
2364 /* Encode line by line */
2365 for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
2366 i_line += 2 )
2368 switch( p_region->fmt.p_palette->i_entries )
2370 case 0:
2371 break;
2373 case 4:
2374 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
2375 encode_pixel_line_2bp( s, p_region, i_line );
2376 break;
2378 case 16:
2379 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
2380 encode_pixel_line_4bp( s, p_region, i_line );
2381 break;
2383 case 256:
2384 bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
2385 encode_pixel_line_8bp( s, p_region, i_line );
2386 break;
2388 default:
2389 msg_Err( p_enc, "subpicture palette (%i) not handled",
2390 p_region->fmt.p_palette->i_entries );
2391 break;
2394 bs_write( s, 8, 0xf0 ); /* End of object line code */
2398 static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
2399 int i_line )
2401 unsigned int i, i_length = 0;
2402 int i_pitch = p_region->p_picture->p->i_pitch;
2403 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2404 int i_last_pixel = p_data[0];
2406 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2408 if( ( i != p_region->fmt.i_visible_width ) &&
2409 ( p_data[i] == i_last_pixel ) && ( i_length != 284 ) )
2411 i_length++;
2412 continue;
2415 if( ( i_length == 1 ) || ( i_length == 11 ) || ( i_length == 28 ) )
2417 /* 2bit/pixel code */
2418 if( i_last_pixel )
2419 bs_write( s, 2, i_last_pixel );
2420 else
2422 bs_write( s, 2, 0 );
2423 bs_write( s, 1, 0 );
2424 bs_write( s, 1, 1 ); /* pseudo color 0 */
2426 i_length--;
2429 if( i_length == 2 )
2431 if( i_last_pixel )
2433 bs_write( s, 2, i_last_pixel );
2434 bs_write( s, 2, i_last_pixel );
2436 else
2438 bs_write( s, 2, 0 );
2439 bs_write( s, 1, 0 );
2440 bs_write( s, 1, 0 );
2441 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2444 else if( i_length > 2 )
2446 bs_write( s, 2, 0 );
2447 if( i_length <= 10 )
2449 bs_write( s, 1, 1 );
2450 bs_write( s, 3, i_length - 3 );
2451 bs_write( s, 2, i_last_pixel );
2453 else
2455 bs_write( s, 1, 0 );
2456 bs_write( s, 1, 0 );
2458 if( i_length <= 27 )
2460 bs_write( s, 2, 2 );
2461 bs_write( s, 4, i_length - 12 );
2462 bs_write( s, 2, i_last_pixel );
2464 else
2466 bs_write( s, 2, 3 );
2467 bs_write( s, 8, i_length - 29 );
2468 bs_write( s, 2, i_last_pixel );
2473 if( i == p_region->fmt.i_visible_width ) break;
2475 i_last_pixel = p_data[i];
2476 i_length = 1;
2479 /* Stop */
2480 bs_write( s, 2, 0 );
2481 bs_write( s, 1, 0 );
2482 bs_write( s, 1, 0 );
2483 bs_write( s, 2, 0 );
2485 /* Stuffing */
2486 bs_align_0( s );
2489 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2490 int i_line )
2492 unsigned int i, i_length = 0;
2493 int i_pitch = p_region->p_picture->p->i_pitch;
2494 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2495 int i_last_pixel = p_data[0];
2497 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2499 if( i != p_region->fmt.i_visible_width &&
2500 p_data[i] == i_last_pixel && i_length != 280 )
2502 i_length++;
2503 continue;
2506 if( ( i_length == 1 ) ||
2507 ( ( i_length == 3 ) && i_last_pixel ) ||
2508 ( i_length == 8 ) )
2510 /* 4bit/pixel code */
2511 if( i_last_pixel )
2512 bs_write( s, 4, i_last_pixel );
2513 else
2515 bs_write( s, 4, 0 );
2516 bs_write( s, 1, 1 );
2517 bs_write( s, 1, 1 );
2518 bs_write( s, 2, 0 ); /* pseudo color 0 */
2520 i_length--;
2523 if( i_length == 2 )
2525 if( i_last_pixel )
2527 bs_write( s, 4, i_last_pixel );
2528 bs_write( s, 4, i_last_pixel );
2530 else
2532 bs_write( s, 4, 0 );
2533 bs_write( s, 1, 1 );
2534 bs_write( s, 1, 1 );
2535 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
2538 else if( !i_last_pixel && ( i_length >= 3 ) && ( i_length <= 9 ) )
2540 bs_write( s, 4, 0 );
2541 bs_write( s, 1, 0 );
2542 bs_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
2544 else if( i_length > 2 )
2546 bs_write( s, 4, 0 );
2547 bs_write( s, 1, 1 );
2549 if( i_length <= 7 )
2551 bs_write( s, 1, 0 );
2552 bs_write( s, 2, i_length - 4 );
2553 bs_write( s, 4, i_last_pixel );
2555 else
2557 bs_write( s, 1, 1 );
2559 if( i_length <= 24 )
2561 bs_write( s, 2, 2 );
2562 bs_write( s, 4, i_length - 9 );
2563 bs_write( s, 4, i_last_pixel );
2565 else
2567 bs_write( s, 2, 3 );
2568 bs_write( s, 8, i_length - 25 );
2569 bs_write( s, 4, i_last_pixel );
2574 if( i == p_region->fmt.i_visible_width ) break;
2576 i_last_pixel = p_data[i];
2577 i_length = 1;
2580 /* Stop */
2581 bs_write( s, 8, 0 );
2583 /* Stuffing */
2584 bs_align_0( s );
2587 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2588 int i_line )
2590 unsigned int i, i_length = 0;
2591 int i_pitch = p_region->p_picture->p->i_pitch;
2592 uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
2593 int i_last_pixel = p_data[0];
2595 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
2597 if( ( i != p_region->fmt.i_visible_width ) &&
2598 ( p_data[i] == i_last_pixel ) && ( i_length != 127 ) )
2600 i_length++;
2601 continue;
2604 if( ( i_length == 1 ) && i_last_pixel )
2606 /* 8bit/pixel code */
2607 bs_write( s, 8, i_last_pixel );
2609 else if( ( i_length == 2 ) && i_last_pixel )
2611 /* 8bit/pixel code */
2612 bs_write( s, 8, i_last_pixel );
2613 bs_write( s, 8, i_last_pixel );
2615 else if( i_length <= 127 )
2617 bs_write( s, 8, 0 );
2619 if( !i_last_pixel )
2621 bs_write( s, 1, 0 );
2622 bs_write( s, 7, i_length ); /* pseudo color 0 */
2624 else
2626 bs_write( s, 1, 1 );
2627 bs_write( s, 7, i_length );
2628 bs_write( s, 8, i_last_pixel );
2632 if( i == p_region->fmt.i_visible_width ) break;
2634 i_last_pixel = p_data[i];
2635 i_length = 1;
2638 /* Stop */
2639 bs_write( s, 8, 0 );
2640 bs_write( s, 8, 0 );
2642 /* Stuffing */
2643 bs_align_0( s );
2646 #endif
2648 static void default_dds_init( decoder_t * p_dec )
2650 decoder_sys_t *p_sys = p_dec->p_sys;
2652 /* see notes on DDS at the top of the file */
2654 /* configure for SD res in case DDS is not present */
2655 p_sys->display.i_version = 0xff; /* an invalid version so it's always different */
2656 p_sys->display.i_width = 720;
2657 p_sys->display.i_height = 576;
2658 p_sys->display.b_windowed = false;