codec: ass: Don't force fonts that aren't shipped anymore in the winstore app
[vlc.git] / modules / codec / dvbsub.c
blob040c06f7a99b7b964b07fe4988862b8e6f7d7920
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
7 * $Id$
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 /*****************************************************************************
32 * Preamble
34 * FIXME:
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
64 * original spec.
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 ********************************************************************************************/
70 #ifdef HAVE_CONFIG_H
71 # include "config.h"
72 #endif
74 #include <vlc_common.h>
75 #include <vlc_plugin.h>
76 #include <vlc_codec.h>
77 #include <vlc_sout.h>
79 #include <vlc_bits.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 /*****************************************************************************
106 * Module descriptor.
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 * );
113 #ifdef ENABLE_SOUT
114 static int OpenEncoder ( vlc_object_t * );
115 static void CloseEncoder( vlc_object_t * );
116 static block_t *Encode ( encoder_t *, subpicture_t * );
117 #endif
119 vlc_module_begin ()
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 )
133 #ifdef ENABLE_SOUT
134 # define ENC_CFG_PREFIX "sout-dvbsub-"
135 add_submodule ()
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 )
142 #endif
143 vlc_module_end ()
145 static const char *const ppsz_enc_options[] = { "x", "y", NULL };
147 /****************************************************************************
148 * Local structures
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
156 int i_id;
157 int i_type;
158 int i_x;
159 int i_y;
160 int i_fg_pc;
161 int i_bg_pc;
162 char *psz_text; /* for string of characters objects */
164 } dvbsub_objectdef_t;
166 /* The entry in the palette CLUT */
167 typedef struct
169 uint8_t Y;
170 uint8_t Cr;
171 uint8_t Cb;
172 uint8_t T;
174 } dvbsub_color_t;
176 /* The displays dimensions [7.2.1] */
177 typedef struct dvbsub_display_s
179 uint8_t i_id;
180 uint8_t i_version;
182 int i_width;
183 int i_height;
185 bool b_windowed;
186 /* these values are only relevant if windowed */
187 int i_x;
188 int i_y;
189 int i_max_x;
190 int i_max_y;
192 } dvbsub_display_t;
194 /* [7.2.4] */
195 typedef struct dvbsub_clut_s
197 uint8_t i_id;
198 uint8_t i_version;
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;
205 } dvbsub_clut_t;
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
211 int i_id;
212 int i_version;
213 int i_x;
214 int i_y;
215 int i_width;
216 int i_height;
217 int i_level_comp;
218 int i_depth;
219 int i_clut;
221 uint8_t *p_pixbuf;
223 int i_object_defs;
224 dvbsub_objectdef_t *p_object_defs;
226 struct dvbsub_region_s *p_next;
228 } dvbsub_region_t;
230 /* The object definition gives the position of the object in a region */
231 typedef struct dvbsub_regiondef_s
233 int i_id;
234 int i_x;
235 int i_y;
237 } dvbsub_regiondef_t;
239 /* The page defines the list of regions [7.2.2] */
240 typedef struct
242 int i_id;
243 int i_timeout; /* in seconds */
244 int i_state;
245 int i_version;
247 int i_region_defs;
248 dvbsub_regiondef_t *p_region_defs;
250 } dvbsub_page_t;
252 struct decoder_sys_t
254 bs_t bs;
256 /* Decoder internal data */
257 int i_id;
258 int i_ancillary_id;
259 mtime_t i_pts;
261 bool b_absolute;
262 int i_spu_position;
263 int i_spu_x;
264 int i_spu_y;
266 bool b_page;
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 /*****************************************************************************
305 * Local prototypes
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
324 * to chose.
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;
330 int i_posx, i_posy;
332 if( p_dec->fmt_in.i_codec != VLC_CODEC_DVBS )
334 return VLC_EGENERIC;
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) );
340 if( !p_sys )
341 return VLC_ENOMEM;
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 );
375 return VLC_SUCCESS;
378 /*****************************************************************************
379 * Close:
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" );
390 free_all( p_dec );
391 free( p_sys );
394 /*****************************************************************************
395 * Flush:
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 /*****************************************************************************
405 * Decode:
406 *****************************************************************************/
407 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
409 decoder_sys_t *p_sys = p_dec->p_sys;
410 block_t *p_block;
411 subpicture_t *p_spu = NULL;
413 if( ( pp_block == NULL ) || ( *pp_block == NULL ) ) return NULL;
414 p_block = *pp_block;
415 *pp_block = NULL;
417 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
419 Flush( p_dec );
420 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
422 block_Release( p_block );
423 return NULL;
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 )
435 #ifdef DEBUG_DVBSUB
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" );
439 #endif
440 block_Release( p_block );
441 return NULL;
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 );
450 return NULL;
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 );
457 return NULL;
460 #ifdef DEBUG_DVBSUB
461 msg_Dbg( p_dec, "subtitle packet received: %"PRId64, p_sys->i_pts );
462 #endif
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 );
474 return NULL;
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 );
483 return p_spu;
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;
494 uint8_t i;
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;
500 /* 4 entries CLUT */
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;
521 if( !(i & 0x8) )
523 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
525 T = 0xFF;
527 else
529 R = (i & 0x1) ? 0xFF : 0;
530 G = (i & 0x2) ? 0xFF : 0;
531 B = (i & 0x4) ? 0xFF : 0;
534 else
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;
554 int i_type;
555 int i_page_id;
556 int i_size;
558 /* sync_byte (already checked) */
559 bs_skip( s, 8 );
561 /* segment type */
562 i_type = bs_read( s, 8 );
564 /* page id */
565 i_page_id = bs_read( s, 16 );
567 /* segment size */
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 ) )
573 #ifdef DEBUG_DVBSUB
574 msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
575 i_page_id, p_sys->i_id );
576 #endif
577 bs_skip( s, 8 * ( 2 + i_size ) );
578 return;
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 ) )
585 #ifdef DEBUG_DVBSUB
586 msg_Dbg( p_dec, "skipped invalid ancillary subtitle packet" );
587 #endif
588 bs_skip( s, 8 * ( 2 + i_size ) );
589 return;
592 #ifdef DEBUG_DVBSUB
593 if( i_page_id == p_sys->i_id )
594 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
595 else
596 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
597 #endif
599 switch( i_type )
601 case DVBSUB_ST_PAGE_COMPOSITION:
602 #ifdef DEBUG_DVBSUB
603 msg_Dbg( p_dec, "decode_page_composition" );
604 #endif
605 decode_page_composition( p_dec, s );
606 break;
608 case DVBSUB_ST_REGION_COMPOSITION:
609 #ifdef DEBUG_DVBSUB
610 msg_Dbg( p_dec, "decode_region_composition" );
611 #endif
612 decode_region_composition( p_dec, s );
613 break;
615 case DVBSUB_ST_CLUT_DEFINITION:
616 #ifdef DEBUG_DVBSUB
617 msg_Dbg( p_dec, "decode_clut" );
618 #endif
619 decode_clut( p_dec, s );
620 break;
622 case DVBSUB_ST_OBJECT_DATA:
623 #ifdef DEBUG_DVBSUB
624 msg_Dbg( p_dec, "decode_object" );
625 #endif
626 decode_object( p_dec, s );
627 break;
629 case DVBSUB_ST_DISPLAY_DEFINITION:
630 #ifdef DEBUG_DVBSUB
631 msg_Dbg( p_dec, "decode_display_definition" );
632 #endif
633 decode_display_definition( p_dec, s );
634 break;
636 case DVBSUB_ST_ENDOFDISPLAY:
637 #ifdef DEBUG_DVBSUB
638 msg_Dbg( p_dec, "end of display" );
639 #endif
640 bs_skip( s, 8 * ( 2 + i_size ) );
641 break;
643 case DVBSUB_ST_STUFFING:
644 #ifdef DEBUG_DVBSUB
645 msg_Dbg( p_dec, "skip stuffing" );
646 #endif
647 bs_skip( s, 8 * ( 2 + i_size ) );
648 break;
650 default:
651 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
652 bs_skip( s, 8 * ( 2 + i_size ) );
653 break;
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;
663 int i_id, i_version;
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 ) )
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 uint8_t i_id;
709 uint8_t i_type;
711 i_id = bs_read( s, 8 );
712 i_type = bs_read( s, 3 );
714 bs_skip( s, 4 );
716 if( bs_read( s, 1 ) )
718 y = bs_read( s, 8 );
719 cr = bs_read( s, 8 );
720 cb = bs_read( s, 8 );
721 t = bs_read( s, 8 );
722 i_processed_length += 6;
724 else
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). */
736 if( y == 0 )
738 cr = cb = 0;
739 t = 0xff;
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;
759 if( i_type & 0x01 )
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 */
784 #ifdef DEBUG_DVBSUB
785 msg_Dbg( p_dec, "page composition mode change" );
786 #endif
787 free_all( p_dec );
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" );
795 #if 0
796 /* Try to start decoding even without an acquisition page */
797 bs_skip( s, 8 * (i_segment_length - 2) );
798 return;
799 #endif
802 #ifdef DEBUG_DVBSUB
803 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
804 msg_Dbg( p_dec, "acquisition page composition" );
805 #endif
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) );
811 return;
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;
821 if( !p_sys->p_page )
823 #ifdef DEBUG_DVBSUB
824 msg_Dbg( p_dec, "new page" );
825 #endif
826 /* Allocate a new page */
827 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
828 if( !p_sys->p_page )
829 return;
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 );
852 #ifdef DEBUG_DVBSUB
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 );
856 #endif
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;
868 bool b_fill;
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 );
886 return;
889 if( !p_region )
891 #ifdef DEBUG_DVBSUB
892 msg_Dbg( p_dec, "new region: %i", i_id );
893 #endif
894 p_region = *pp_region = calloc( 1, sizeof(dvbsub_region_t) );
895 if( !p_region )
896 return;
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 );
910 #ifdef DEBUG_DVBSUB
911 msg_Dbg( p_dec, " width=%d height=%d", i_width, i_height );
912 #endif
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;
943 b_fill = true;
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 */
954 if( b_fill )
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;
1006 int i_version;
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 );
1016 return;
1019 #ifdef DEBUG_DVBSUB
1020 msg_Dbg( p_dec, "new display definition: %i", i_version );
1021 #endif
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 )
1032 #ifdef DEBUG_DVBSUB
1033 msg_Dbg( p_dec, "display definition with offsets (windowed)" );
1034 #endif
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;
1042 else
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 );
1058 #ifdef DEBUG_DVBSUB
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 );
1064 #endif
1067 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
1068 uint8_t *, 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 );
1091 return;
1094 /* Check if the object needs to be rendered in at least one
1095 * of the regions */
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;
1104 if( !p_region )
1106 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
1107 return;
1110 #ifdef DEBUG_DVBSUB
1111 msg_Dbg( p_dec, "new object: %i", i_id );
1112 #endif
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) );
1129 /* Sanity check */
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" );
1134 return;
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 );
1149 if( i_bottomfield )
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 );
1156 else
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 );
1167 else
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;
1173 /* Sanity check */
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" );
1178 return;
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++ )
1186 int j;
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;
1205 #ifdef DEBUG_DVBSUB
1206 msg_Dbg( p_dec, "end object: %i", i_id );
1207 #endif
1210 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
1211 int i_x, int i_y,
1212 uint8_t *p_field, int i_field )
1214 uint8_t *p_pixbuf;
1215 int i_offset = 0;
1216 bs_t bs;
1218 /* Sanity check */
1219 if( !p_region->p_pixbuf )
1221 msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
1222 return;
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 );
1228 return;
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 ) )
1236 /* Sanity check */
1237 if( i_y >= p_region->i_height ) return;
1239 switch( bs_read( &bs, 8 ) )
1241 case 0x10:
1242 dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1243 &i_offset );
1244 break;
1246 case 0x11:
1247 dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1248 &i_offset );
1249 break;
1251 case 0x12:
1252 dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
1253 &i_offset );
1254 break;
1256 case 0x20:
1257 case 0x21:
1258 case 0x22:
1259 /* We don't use map tables */
1260 break;
1262 case 0xf0: /* End of line code */
1263 p_pixbuf += 2*p_region->i_width;
1264 i_offset = 0; i_y += 2;
1265 break;
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 )
1281 i_count = 1;
1283 else
1285 if( bs_read( s, 1 ) == 0x01 ) // Switch1
1287 i_count = 3 + bs_read( s, 3 );
1288 i_color = bs_read( s, 2 );
1290 else
1292 if( bs_read( s, 1 ) == 0x00 ) //Switch2
1294 switch( bs_read( s, 2 ) ) //Switch3
1296 case 0x00:
1297 b_stop = true;
1298 break;
1299 case 0x01:
1300 i_count = 2;
1301 break;
1302 case 0x02:
1303 i_count = 12 + bs_read( s, 4 );
1304 i_color = bs_read( s, 2 );
1305 break;
1306 case 0x03:
1307 i_count = 29 + bs_read( s, 8 );
1308 i_color = bs_read( s, 2 );
1309 break;
1310 default:
1311 break;
1314 else
1316 /* 1 pixel color 0 */
1317 i_count = 1;
1322 if( !i_count ) continue;
1324 /* Sanity check */
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;
1333 bs_align( s );
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 )
1347 /* Add 1 pixel */
1348 i_count = 1;
1350 else
1352 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1354 if( bs_show( s, 3 ) != 0x00 )
1356 i_count = 2 + bs_read( s, 3 );
1358 else
1360 bs_skip( s, 3 );
1361 b_stop = true;
1364 else
1366 if( bs_read( s, 1 ) == 0x00) //Switch2
1368 i_count = 4 + bs_read( s, 2 );
1369 i_color = bs_read( s, 4 );
1371 else
1373 switch ( bs_read( s, 2 ) ) //Switch3
1375 case 0x0:
1376 i_count = 1;
1377 break;
1378 case 0x1:
1379 i_count = 2;
1380 break;
1381 case 0x2:
1382 i_count = 9 + bs_read( s, 4 );
1383 i_color = bs_read( s, 4 );
1384 break;
1385 case 0x3:
1386 i_count= 25 + bs_read( s, 8 );
1387 i_color = bs_read( s, 4 );
1388 break;
1394 if( !i_count ) continue;
1396 /* Sanity check */
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;
1405 bs_align( s );
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 )
1419 /* Add 1 pixel */
1420 i_count = 1;
1422 else
1424 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1426 if( bs_show( s, 7 ) != 0x00 )
1428 i_count = bs_read( s, 7 );
1430 else
1432 bs_skip( s, 7 );
1433 b_stop = true;
1436 else
1438 i_count = bs_read( s, 7 );
1439 i_color = bs_read( s, 8 );
1443 if( !i_count ) continue;
1445 /* Sanity check */
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;
1454 bs_align( s );
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;
1468 free( p_clut );
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 );
1479 free( p_reg );
1481 p_sys->p_regions = NULL;
1483 if( p_sys->p_page )
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;
1497 int i, j;
1498 int i_base_x;
1499 int i_base_y;
1501 /* Allocate the subpicture internal data. */
1502 p_spu = decoder_NewSubpicture( p_dec, NULL );
1503 if( !p_spu )
1504 return 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 */
1539 #ifdef DEBUG_DVBSUB
1540 if( p_sys->p_page )
1541 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1542 #endif
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;
1552 video_format_t fmt;
1553 video_palette_t palette;
1554 int i_pitch;
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;
1565 #ifdef DEBUG_DVBSUB
1566 /* if a region exists, then print it's size */
1567 if (p_region)
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 );
1574 else
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 );
1579 #endif
1581 if( !p_region )
1583 msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
1584 continue;
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;
1592 if( !p_clut )
1594 msg_Dbg( p_dec, "clut %i not found", p_region->i_clut );
1595 continue;
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 */
1604 fmt.i_sar_den = 1;
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 );
1624 if( !p_spu_region )
1626 msg_Err( p_dec, "cannot allocate SPU region" );
1627 continue;
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;
1644 p_dst += i_pitch;
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 )
1654 continue;
1656 /* Create new SPU region */
1657 video_format_Init( &fmt, VLC_CODEC_TEXT );
1658 fmt.i_sar_num = 1;
1659 fmt.i_sar_den = 1;
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;
1675 return p_spu;
1678 /*****************************************************************************
1679 * encoder_sys_t : encoder descriptor
1680 *****************************************************************************/
1681 typedef struct encoder_region_t
1683 int i_width;
1684 int i_height;
1686 } 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;
1694 int i_regions;
1695 encoder_region_t *p_regions;
1697 mtime_t i_pts;
1699 /* subpicture positioning */
1700 int i_offset_x;
1701 int i_offset_y;
1704 #ifdef ENABLE_SOUT
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 ) &&
1719 !p_enc->obj.force )
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 )
1726 return VLC_ENOMEM;
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" );
1744 return VLC_SUCCESS;
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 */
1764 #else
1765 int *pi_delta;
1766 #endif
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 );
1777 #endif
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-- )
1786 int a = i_pixels;
1787 int b = i_iterator;
1788 int c;
1790 while( b )
1792 c = a % b;
1793 a = b;
1794 b = c;
1797 if( a == 1 )
1799 break;
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 ; )
1811 uint8_t y, u, v, a;
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 )
1823 break;
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 )
1836 b_success = false;
1837 break;
1839 i += i_iterator;
1840 if( i > i_pixels )
1842 i -= i_pixels;
1846 if( b_success )
1848 break;
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 );
1855 #endif
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++ )
1862 pi_delta[ i ] = 0;
1864 #endif
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;
1874 int y, u, v, a;
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;
1888 #else
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 ];
1893 #endif
1895 /* Find best colour in palette */
1896 for( i_mindist = 99999999, i_best = 0, j = 0; j < p_fmt->p_palette->i_entries; j++ )
1898 int i_dist = 0;
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 )
1907 i_mindist = i_dist;
1908 i_best = j;
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);
1918 #else
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;
1931 #endif
1934 #ifndef RANDOM_DITHERING
1935 free( pi_delta );
1936 #endif
1938 /* pad palette */
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 );
1950 #endif
1952 return p_subpic;
1953 } /* End of hack */
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;
1963 block_t *p_block;
1965 if( !p_subpic || !p_subpic->p_region ) return NULL;
1967 /* FIXME: this is a hack to convert VLC_CODEC_YUVA into
1968 * VLC_CODEC_YUVP
1970 p_region = p_subpic->p_region;
1971 if( p_region->fmt.i_chroma == VLC_CODEC_YUVA )
1973 p_temp = YuvaYuvp( p_subpic );
1974 if( !p_temp )
1976 msg_Err( p_enc, "no picture in subpicture" );
1977 return NULL;
1979 p_region = p_subpic->p_region;
1982 /* Sanity check */
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 );
1989 return NULL;
1992 if( p_region->fmt.p_palette )
1994 switch( p_region->fmt.p_palette->i_entries )
1996 case 0:
1997 case 4:
1998 case 16:
1999 case 256:
2000 break;
2001 default:
2002 msg_Err( p_enc, "subpicture palette (%d) not handled",
2003 p_region->fmt.p_palette->i_entries );
2004 return NULL;
2007 /* End of hack */
2009 #ifdef DEBUG_DVBSUB
2010 msg_Dbg( p_enc, "encoding subpicture" );
2011 #endif
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; */
2054 #ifdef DEBUG_DVBSUB
2055 msg_Dbg( p_enc, "subpicture encoded properly" );
2056 #endif
2057 return p_block;
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 );
2073 free( p_sys );
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 */
2126 i_timeout = 0;
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 */
2149 else
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;
2163 /* Sanity check */
2164 if( !p_region ) return;
2166 if( p_region->fmt.i_chroma == VLC_CODEC_YUVP )
2168 p_pal = p_region->fmt.p_palette;
2170 else
2172 pal.i_entries = 4;
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;
2180 p_pal = &pal;
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;
2213 int i_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;
2219 bool b_text =
2220 ( p_region->fmt.i_chroma == VLC_CODEC_TEXT );
2222 if( !b_text )
2224 video_palette_t *p_pal = p_region->fmt.p_palette;
2226 if( !p_pal )
2228 msg_Err( p_enc, "subpicture has no palette - ignoring it" );
2229 break;
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 */
2271 if( b_text )
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,
2281 bool b_top );
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;
2287 int i_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 );
2308 break;
2309 case VLC_CODEC_TEXT:
2310 bs_write( s, 2, 1 );
2311 break;
2312 default:
2313 msg_Err( p_enc, "FOURCC %d not supported by encoder.", p_region->fmt.i_chroma );
2314 continue;
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 )
2322 int i_size, i;
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 );
2337 continue;
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 */
2345 /* Top field */
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 );
2351 /* Bottom field */
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 */
2358 bs_align_0( s );
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,
2367 int i_line );
2368 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2369 int i_line );
2370 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2371 int i_line );
2372 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
2373 subpicture_region_t *p_region,
2374 bool b_top )
2376 unsigned int i_line;
2378 /* Sanity check */
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;
2383 i_line += 2 )
2385 switch( p_region->fmt.p_palette->i_entries )
2387 case 0:
2388 break;
2390 case 4:
2391 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
2392 encode_pixel_line_2bp( s, p_region, i_line );
2393 break;
2395 case 16:
2396 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
2397 encode_pixel_line_4bp( s, p_region, i_line );
2398 break;
2400 case 256:
2401 bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
2402 encode_pixel_line_8bp( s, p_region, i_line );
2403 break;
2405 default:
2406 msg_Err( p_enc, "subpicture palette (%i) not handled",
2407 p_region->fmt.p_palette->i_entries );
2408 break;
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,
2416 int i_line )
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 ) )
2428 i_length++;
2429 continue;
2432 if( ( i_length == 1 ) || ( i_length == 11 ) || ( i_length == 28 ) )
2434 /* 2bit/pixel code */
2435 if( i_last_pixel )
2436 bs_write( s, 2, i_last_pixel );
2437 else
2439 bs_write( s, 2, 0 );
2440 bs_write( s, 1, 0 );
2441 bs_write( s, 1, 1 ); /* pseudo color 0 */
2443 i_length--;
2446 if( i_length == 2 )
2448 if( i_last_pixel )
2450 bs_write( s, 2, i_last_pixel );
2451 bs_write( s, 2, i_last_pixel );
2453 else
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 );
2470 else
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 );
2481 else
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];
2493 i_length = 1;
2496 /* Stop */
2497 bs_write( s, 2, 0 );
2498 bs_write( s, 1, 0 );
2499 bs_write( s, 1, 0 );
2500 bs_write( s, 2, 0 );
2502 /* Stuffing */
2503 bs_align_0( s );
2506 static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
2507 int i_line )
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 )
2519 i_length++;
2520 continue;
2523 if( ( i_length == 1 ) ||
2524 ( ( i_length == 3 ) && i_last_pixel ) ||
2525 ( i_length == 8 ) )
2527 /* 4bit/pixel code */
2528 if( i_last_pixel )
2529 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, 0 ); /* pseudo color 0 */
2537 i_length--;
2540 if( i_length == 2 )
2542 if( i_last_pixel )
2544 bs_write( s, 4, i_last_pixel );
2545 bs_write( s, 4, i_last_pixel );
2547 else
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 );
2566 if( i_length <= 7 )
2568 bs_write( s, 1, 0 );
2569 bs_write( s, 2, i_length - 4 );
2570 bs_write( s, 4, i_last_pixel );
2572 else
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 );
2582 else
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];
2594 i_length = 1;
2597 /* Stop */
2598 bs_write( s, 8, 0 );
2600 /* Stuffing */
2601 bs_align_0( s );
2604 static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
2605 int i_line )
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 ) )
2617 i_length++;
2618 continue;
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 );
2636 if( !i_last_pixel )
2638 bs_write( s, 1, 0 );
2639 bs_write( s, 7, i_length ); /* pseudo color 0 */
2641 else
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];
2652 i_length = 1;
2655 /* Stop */
2656 bs_write( s, 8, 0 );
2657 bs_write( s, 8, 0 );
2659 /* Stuffing */
2660 bs_align_0( s );
2663 #endif
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;